|
@@ -1,13 +1,12 @@
|
|
|
package com.pavis.admin.aigc.core.agent.tool;
|
|
|
|
|
|
-import com.alibaba.fastjson2.JSON;
|
|
|
-import com.alibaba.fastjson2.TypeReference;
|
|
|
import com.pavis.admin.aigc.model.resp.AgentPlanResp;
|
|
|
import com.pavis.admin.aigc.model.resp.AgentPlanStepResp;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.ai.openai.api.OpenAiApi.FunctionTool;
|
|
|
import org.springframework.ai.tool.function.FunctionToolCallback;
|
|
|
import org.springframework.ai.tool.metadata.ToolMetadata;
|
|
|
+import org.springframework.core.ParameterizedTypeReference;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
@@ -18,7 +17,7 @@ import java.util.function.Function;
|
|
|
* 计划工具类
|
|
|
*/
|
|
|
@Slf4j
|
|
|
-public class PlanningTool implements Function<String, ToolExecuteResult> {
|
|
|
+public class PlanningTool implements Function<Map<String, Object>, ToolExecuteResult> {
|
|
|
|
|
|
private AgentPlanResp currentPlan;
|
|
|
|
|
@@ -39,11 +38,11 @@ public class PlanningTool implements Function<String, ToolExecuteResult> {
|
|
|
"enum": [
|
|
|
"create"
|
|
|
],
|
|
|
- "type": "long"
|
|
|
+ "type": "string"
|
|
|
},
|
|
|
"plan_id": {
|
|
|
"description": "Unique identifier for the plan",
|
|
|
- "type": "string"
|
|
|
+ "type": "integer"
|
|
|
},
|
|
|
"title": {
|
|
|
"description": "Title for the plan",
|
|
@@ -83,23 +82,53 @@ public class PlanningTool implements Function<String, ToolExecuteResult> {
|
|
|
}
|
|
|
|
|
|
public FunctionToolCallback getFunctionToolCallback() {
|
|
|
+ // 创建保留泛型信息的类型引用
|
|
|
+ ParameterizedTypeReference<Map<String, Object>> typeRef =
|
|
|
+ new ParameterizedTypeReference<Map<String, Object>>() {
|
|
|
+ };
|
|
|
+
|
|
|
return FunctionToolCallback.builder(name, this)
|
|
|
.description(description)
|
|
|
.inputSchema(PARAMETERS)
|
|
|
- .inputType(String.class)
|
|
|
+ .inputType(typeRef) // 使用 ParameterizedTypeReference
|
|
|
.toolMetadata(ToolMetadata.builder().returnDirect(true).build())
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
- public ToolExecuteResult run(String toolInput) {
|
|
|
+ public ToolExecuteResult run(Map<String, Object> toolInput) {
|
|
|
try {
|
|
|
- Map<String, Object> input = JSON.parseObject(toolInput, new TypeReference<Map<String, Object>>() {
|
|
|
- });
|
|
|
- String command = (String) input.get("command");
|
|
|
- Long planId = (Long) input.get("plan_id");
|
|
|
- String title = (String) input.get("title");
|
|
|
- List<String> steps = JSON.parseObject(JSON.toJSONString(input.get("steps")), new TypeReference<>() {
|
|
|
- });
|
|
|
+ log.info("Planning tool parsed input: {}", toolInput);
|
|
|
+ String command = (String) toolInput.get("command");
|
|
|
+
|
|
|
+ // 安全处理 plan_id
|
|
|
+ Long planId = null;
|
|
|
+ Object planIdObj = toolInput.get("plan_id");
|
|
|
+ if (planIdObj != null) {
|
|
|
+ if (planIdObj instanceof Number) {
|
|
|
+ planId = ((Number) planIdObj).longValue();
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ planId = Long.parseLong(planIdObj.toString());
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ log.warn("Invalid plan_id format: {}", planIdObj);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String title = (String) toolInput.get("title");
|
|
|
+
|
|
|
+ // 安全处理 steps
|
|
|
+ List<String> steps = new ArrayList<>();
|
|
|
+ Object stepsObj = toolInput.get("steps");
|
|
|
+ if (stepsObj instanceof List) {
|
|
|
+ for (Object item : (List<?>) stepsObj) {
|
|
|
+ if (item != null) {
|
|
|
+ steps.add(item.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("Executing command: {}, planId: {}, title: {}, steps: {}", command, planId, title, steps);
|
|
|
return switch (command) {
|
|
|
case "create" -> createPlan(planId, title, steps);
|
|
|
// case "update" -> updatePlan(planId, title, steps);
|
|
@@ -139,8 +168,9 @@ public class PlanningTool implements Function<String, ToolExecuteResult> {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ToolExecuteResult apply(String input) {
|
|
|
- return run(input);
|
|
|
+ public ToolExecuteResult apply(Map<String, Object> toolInput) {
|
|
|
+ log.info("Planning tool raw input: {}", toolInput);
|
|
|
+ return run(toolInput);
|
|
|
}
|
|
|
|
|
|
}
|