package com.company.agentfoundation.agent; import com.company.agentfoundation.domain.ChatRequest; import com.company.agentfoundation.domain.ChatResponse; import com.company.agentfoundation.security.AgentContext; import com.company.agentfoundation.service.ChatService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.util.List; @Service public class AgentOrchestratorService { private static final Logger log = LoggerFactory.getLogger(AgentOrchestratorService.class); private final IntentRecognitionService intentRecognitionService; private final OperationReportService operationReportService; private final ChatService chatService; public AgentOrchestratorService( IntentRecognitionService intentRecognitionService, OperationReportService operationReportService, ChatService chatService ) { this.intentRecognitionService = intentRecognitionService; this.operationReportService = operationReportService; this.chatService = chatService; } public String handleMessage(String agentId, String fromUser, String userText) { AgentIntent intent = intentRecognitionService.recognize(agentId, userText); log.info("agent orchestrator route fromUser={} intent={} group={}", fromUser, intent.code(), intent.mcpGroup()); if (intent != AgentIntent.GENERAL_CHAT) { return toWecomText(operationReportService.handle(intent, AgentRequestContext.of(agentId, fromUser, userText))); } ChatRequest request = new ChatRequest( null, List.of( new ChatRequest.Message("system", "You are an enterprise CRM assistant. Reply concisely in Chinese."), new ChatRequest.Message("user", userText) ), 0.3 ); ChatResponse response = chatService.chat(new AgentContext(agentId), request); return response.content(); } private String toWecomText(AgentCapabilityResponse response) { return """ \u3010%s\u3011 %s \u80fd\u529b\u5206\u7ec4\uff1a%s \u80fd\u529b\u7f16\u7801\uff1a%s \u6570\u636e\u6e90\uff1a%s \u72b6\u6001\uff1a%s """.formatted( response.title(), response.message(), response.mcpGroup(), response.intentCode(), String.join("\u3001", response.dataSources()), response.status() ).trim(); } }