CommonService.java 3.16 KB
package com.glsk.service;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClientException;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 通用服务
 */
@Service
@Slf4j

public class CommonService {
    @Tool(description = "像人类一样先思考再行动。")
    public String thinkAndPlan(
            @Parameter(description = "你对用户核心需求的理解") String userIntent,
            @Parameter(description = "当前状态分析,包括已有信息和缺失信息") String currentSituation,
            @Parameter(description = "详细的执行步骤列表,每步都要具体可执行") List<String> plan,
            @Parameter(description = "基于规划确定的下一个具体行动") String nextAction
    ) {
        try {
            // 构建思考结果的字符串表示
            StringBuilder result = new StringBuilder();
            result.append("🤔 思考与规划结果:\n\n");
            result.append("📋 用户意图理解: ").append(userIntent).append("\n\n");
            result.append("🔍 当前状况分析: ").append(currentSituation).append("\n\n");
            result.append("📝 执行计划:\n");
            for (int i = 0; i < plan.size(); i++) {
                result.append("  ").append(i + 1).append(". ").append(plan.get(i)).append("\n");
            }
            result.append("\n➡️ 下一步行动: ").append(nextAction);

            log.info("思考规划完成: userIntent={}, steps={}", userIntent, plan.size());
            return result.toString();

        } catch (Exception e) {
            log.error("思考规划工具执行失败", e);
            throw new RuntimeException("思考规划失败: " + e.getMessage(), e);
        }
    }

    @Tool(description = "检查函数参数。")
    public String checkFunctionParameters (
            @Parameter(description = "工具名称") String toolName,
            @Parameter(description = "工具参数描述") String parametersDetails
    ) {
        try {
            // 构建思考结果的字符串表示
            StringBuilder result = new StringBuilder();
            result.append("🤔 检查函数参数:\n\n");
            result.append("📋 工具名称: ").append(toolName).append("\n\n");
            result.append("🔍 工具参数描述: ").append(parametersDetails).append("\n\n");

            log.info("检查函数参数完成: toolName={}, parametersDetails={}");
            return result.toString();

        } catch (Exception e) {
            log.error("检查函数参数工具执行失败", e);
            throw new RuntimeException("检查函数参数失败: " + e.getMessage(), e);
        }
    }



    public static void main(String[] args) {
        CommonService client = new CommonService();

    }
}