|
@@ -1,18 +1,23 @@
|
|
|
package com.pavis.admin.aigc.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
import com.pavis.admin.aigc.core.agent.planner.PlanContext;
|
|
|
import com.pavis.admin.aigc.core.agent.tool.PlanningTool;
|
|
|
import com.pavis.admin.aigc.core.llm.LlmService;
|
|
|
import com.pavis.admin.aigc.mapper.AgentPlanMapper;
|
|
|
import com.pavis.admin.aigc.model.entity.AgentPlanDO;
|
|
|
import com.pavis.admin.aigc.model.query.AgentPlanQuery;
|
|
|
+import com.pavis.admin.aigc.model.query.AgentPlanStepQuery;
|
|
|
import com.pavis.admin.aigc.model.query.AgentQuery;
|
|
|
import com.pavis.admin.aigc.model.req.AgentPlanReq;
|
|
|
import com.pavis.admin.aigc.model.resp.AgentPlanDetailResp;
|
|
|
import com.pavis.admin.aigc.model.resp.AgentPlanResp;
|
|
|
+import com.pavis.admin.aigc.model.resp.AgentPlanStepResp;
|
|
|
import com.pavis.admin.aigc.model.resp.AgentResp;
|
|
|
import com.pavis.admin.aigc.service.AgentPlanService;
|
|
|
+import com.pavis.admin.aigc.service.AgentPlanStepService;
|
|
|
import com.pavis.admin.aigc.service.AgentService;
|
|
|
+import com.pavis.admin.common.context.UserContextHolder;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.ai.chat.client.ChatClient;
|
|
@@ -22,6 +27,8 @@ import org.springframework.stereotype.Service;
|
|
|
import top.continew.starter.extension.crud.model.query.SortQuery;
|
|
|
import top.continew.starter.extension.crud.service.BaseServiceImpl;
|
|
|
|
|
|
+import java.time.Duration;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
@@ -39,25 +46,38 @@ public class AgentPlanServiceImpl extends BaseServiceImpl<AgentPlanMapper, Agent
|
|
|
|
|
|
private final AgentService agentService;
|
|
|
|
|
|
+ private final AgentPlanStepService agentPlanStepService;
|
|
|
+
|
|
|
@Override
|
|
|
- public PlanContext createPlan(PlanContext context) {
|
|
|
- Long planId = context.getPlanId();
|
|
|
+ public PlanContext createPlan(AgentPlanReq req) {
|
|
|
+ // 获取userId
|
|
|
+ Long userId = UserContextHolder.getUserId();
|
|
|
+ req.setUserId(userId);
|
|
|
+ // 先做持久化获取planId
|
|
|
+ Long planId = super.create(req);
|
|
|
if (planId == null) {
|
|
|
throw new IllegalArgumentException("Plan ID cannot be null or empty");
|
|
|
}
|
|
|
+ // 创建规划上下文
|
|
|
+ PlanContext context = new PlanContext();
|
|
|
+ context.setPlanId(planId);
|
|
|
+ context.setConversationId(req.getConversationId());
|
|
|
+ context.setMessageId(req.getMessageId());
|
|
|
+ context.setUserMessage(req.getMessageContent());
|
|
|
+ AgentPlanResp currentPlan = this.getByPlanId(planId);
|
|
|
try {
|
|
|
// 构建所有智能体信息
|
|
|
String agentsInfo = buildAgentsInfo();
|
|
|
- AgentPlanResp currentPlan = null;
|
|
|
// 生成计划提示
|
|
|
String planPrompt = generatePlanPrompt(context.getUserMessage(), agentsInfo, planId);
|
|
|
+ currentPlan.setThink_input(planPrompt);
|
|
|
// 使用 LLM 生成计划
|
|
|
PromptTemplate promptTemplate = new PromptTemplate(planPrompt);
|
|
|
Prompt prompt = promptTemplate.create();
|
|
|
|
|
|
// 创建 规划工具 PlanningTool
|
|
|
PlanningTool planningTool = new PlanningTool();
|
|
|
-
|
|
|
+ currentPlan.setStartTime(LocalDateTime.now());
|
|
|
ChatClient.CallResponseSpec response = llmService.getPlanningChatClient()
|
|
|
.prompt(prompt)
|
|
|
.toolCallbacks(List.of(planningTool.getFunctionToolCallback()))
|
|
@@ -67,13 +87,15 @@ public class AgentPlanServiceImpl extends BaseServiceImpl<AgentPlanMapper, Agent
|
|
|
String outputText = response.chatResponse().getResult().getOutput().getText();
|
|
|
// 检查计划是否创建成功
|
|
|
if (planId.equals(planningTool.getCurrentPlanId())) {
|
|
|
- currentPlan = planningTool.getCurrentPlan();
|
|
|
- log.info("Plan created successfully: {}", currentPlan);
|
|
|
- currentPlan.setThink_input(planPrompt);
|
|
|
currentPlan.setThink_output(outputText);
|
|
|
+ currentPlan.setEndTime(LocalDateTime.now());
|
|
|
+ currentPlan.setDuration(Duration.between(currentPlan.getStartTime(),currentPlan.getEndTime()).getSeconds());
|
|
|
+ currentPlan.setTitle(planningTool.getCurrentPlan().getTitle());
|
|
|
+ currentPlan.setSteps(planningTool.getCurrentPlan().getSteps());
|
|
|
}
|
|
|
- context.setPlan(currentPlan);
|
|
|
-
|
|
|
+ // 更新或存储计划信息
|
|
|
+ AgentPlanResp savedPlan = this.saveOrUpdate(currentPlan);
|
|
|
+ context.setPlan(savedPlan);
|
|
|
} catch (Exception e) {
|
|
|
log.error("Error creating plan for request: {}", context.getUserMessage(), e);
|
|
|
// 处理异常情况
|
|
@@ -84,7 +106,43 @@ public class AgentPlanServiceImpl extends BaseServiceImpl<AgentPlanMapper, Agent
|
|
|
|
|
|
@Override
|
|
|
public PlanContext executePlan(PlanContext context) {
|
|
|
- return null;
|
|
|
+ // 获取规划信息
|
|
|
+ AgentPlanResp currentPlan = context.getPlan();
|
|
|
+ // 获取当前规划的所有执行步骤
|
|
|
+ List<AgentPlanStepResp> currentPlanSteps = currentPlan.getSteps();
|
|
|
+ // 开始按照计划步骤生成具体的执行内容
|
|
|
+ for (AgentPlanStepResp currentPlanStep : currentPlanSteps) {
|
|
|
+ log.info("Current Plan step: {}", currentPlanStep);
|
|
|
+ }
|
|
|
+ context.setSuccess(true);
|
|
|
+ return context;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public AgentPlanResp getByPlanId(Long planId) {
|
|
|
+ return baseMapper.lambdaQuery()
|
|
|
+ .eq(AgentPlanDO::getId, planId)
|
|
|
+ .oneOpt()
|
|
|
+ .map(agentPlanDO -> BeanUtil.copyProperties(agentPlanDO, AgentPlanResp.class))
|
|
|
+ .orElse(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public AgentPlanResp saveOrUpdate(AgentPlanResp resp) {
|
|
|
+ AgentPlanDO agentPlanDO = baseMapper.selectById(resp.getId());
|
|
|
+ BeanUtil.copyProperties(resp, agentPlanDO);
|
|
|
+ log.info("Copied plan: {}", agentPlanDO);
|
|
|
+ baseMapper.updateById(agentPlanDO);
|
|
|
+ // 保存计划步骤
|
|
|
+ boolean result = agentPlanStepService.saveBatch(resp.getSteps());
|
|
|
+ if (result) {
|
|
|
+ AgentPlanStepQuery query = new AgentPlanStepQuery();
|
|
|
+ query.setPlanId(resp.getId());
|
|
|
+ SortQuery sortQuery = new SortQuery();
|
|
|
+ List<AgentPlanStepResp> list = agentPlanStepService.list(query, sortQuery);
|
|
|
+ resp.setSteps(list);
|
|
|
+ }
|
|
|
+ return resp;
|
|
|
}
|
|
|
|
|
|
/**
|