Commit 77cb03bb authored by alex yao's avatar alex yao

feat:Agent插件-邮件通知

parent ffea274a
package cn.com.poc.common.listener;
import cn.com.poc.thirdparty.resource.demand.ai.function.notification_reminder.NotificationReminderConstant;
import cn.com.yict.framemax.frame.entity.EmailSenderEntity;
import cn.com.yict.framemax.frame.service.EmailSenderService;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
/**
* @author Helen
* @date 2021/11/12 14:46
*/
@Component
public class RedisKeyExpiredListener extends KeyExpirationEventMessageListener {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public RedisKeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
super(listenerContainer);
}
@Resource
private EmailSenderService emailSenderService;
@Override
public void onMessage(Message message, byte[] pattern) {
// 获取过期的key, 格式:CALLBACK_KEY-TYPE-{json}
String expiredKey = message.toString();
//前缀校验
if (expiredKey.contains(NotificationReminderConstant.REDIS_KEY)) {
int i = expiredKey.indexOf(":");
String json = expiredKey.substring(i + 1);
if (StringUtils.isNoneBlank(json)) {
JSONObject jsonObject = JSONObject.parseObject(json);
String email = jsonObject.getString("email");
String content = jsonObject.getString("content");
EmailSenderEntity entity = new EmailSenderEntity();
List<String> list = new LinkedList<>();
list.add(email);
entity.setTo(list);
entity.setBody(content);
entity.setSubject("Agent Application Notification");
try {
emailSenderService.sendMail(entity, null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
......
......@@ -7,6 +7,7 @@ import cn.com.poc.thirdparty.resource.demand.ai.function.html_reader.HtmlReaderF
import cn.com.poc.thirdparty.resource.demand.ai.function.image_ocr.ImageOCRFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.long_memory.SetLongMemoryFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.memory_variable_writer.MemoryVariableWriterFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.notification_reminder.NotificationReminderFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.top_search.WeiboTopSearchFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.web_seach.WebSearchFunction;
......@@ -21,6 +22,7 @@ public enum LargeModelFunctionEnum {
image_ocr(ImageOCRFunction.class),
weibo_search_top(WeiboTopSearchFunction.class),
notification_reminder(NotificationReminderFunction.class),
bing_web_search(null),
;
......
package cn.com.poc.thirdparty.resource.demand.ai.function.notification_reminder;
/**
* @author alex.yao
* @date 2025/1/16
*/
public interface NotificationReminderConstant {
String REDIS_KEY = "AGENT_PLUGIN_NOTIFICATION:";
}
package cn.com.poc.thirdparty.resource.demand.ai.function.notification_reminder;
import cn.com.poc.agent_application.entity.Variable;
import cn.com.poc.common.service.RedisService;
import cn.com.poc.common.utils.*;
import cn.com.poc.support.security.oauth.entity.UserBaseEntity;
import cn.com.poc.thirdparty.resource.demand.ai.function.AbstractLargeModelFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.entity.FunctionLLMConfig;
import cn.com.poc.thirdparty.resource.demand.ai.function.entity.Parameters;
import cn.com.poc.thirdparty.resource.demand.ai.function.entity.Properties;
import cn.com.poc.user.aggregation.MemberInfoService;
import cn.com.poc.user.entity.MemberInfoEntity;
import cn.com.poc.user.service.BizMemberInfoService;
import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
* @author alex.yao
* @date 2025/1/16
*/
@Component
public class NotificationReminderFunction extends AbstractLargeModelFunction {
private final String DESC = "该方法是通过建立定时任务,向用户推送用户设定的提醒内容。现在的日期时间是:";
@Resource
private RedisService redisService;
@Resource
private BizMemberInfoService bizMemberInfoService;
private FunctionLLMConfig functionLLMConfig;
@Override
public String doFunction(String content, String identifier) {
UserBaseEntity userBaseEntity = BlContext.getCurrentUserNotException();
if (userBaseEntity == null) {
return "FAIL";
}
try {
MemberInfoEntity memberInfoEntity = bizMemberInfoService.getById(userBaseEntity.getUserId().intValue());
String email = memberInfoEntity.getEmail();
if (StringUtils.isBlank(email)) {
return "用户尚未配置邮箱,告知用户插件执行失败,原因是用户需要完善个人邮箱信息后再使用。";
}
JSONObject jsonObject = JSONObject.parseObject(content);
jsonObject.put("email", email);
Date date = jsonObject.getDate("datetime");
if (date.before(DateUtils.getCurrDateTime())) {
return "告知用户插件执行失败,原因是通知的日期时间已经超过当前时间。";
}
long expiredTime = DateUtils.diffTwoDate(date, DateUtils.getCurrDateTime()) / 1000;
redisService.set(NotificationReminderConstant.REDIS_KEY + jsonObject.toJSONString(), null, expiredTime);
return "SUCCESS";
} catch (Exception e) {
return "FAIL";
}
}
@Override
public String getDesc() {
return DESC;
}
@Override
public List<String> getLLMConfig() {
functionLLMConfig = new FunctionLLMConfig
.FunctionLLMConfigBuilder()
.name("notification_reminder")
.description(DESC + DateUtils.formatDate(DateUtils.getCurrDateTime(), DateUtils.yyyy_MM_dd_HH_mm_ss))
.parameters(new Parameters("object")
.addProperties("content", new Properties("string", "提醒内容"))
.addProperties("datetime", new Properties("date", "任务执行的日期时间"))
).build();
return ListUtil.toList(JsonUtils.serialize(functionLLMConfig));
}
@Override
public List<String> getLLMConfig(List<Variable> variableStructure) {
return getLLMConfig();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment