| | |
| | | import com.zy.ai.entity.ChatCompletionRequest; |
| | | import com.zy.ai.entity.ChatCompletionResponse; |
| | | import com.zy.ai.entity.WcsDiagnosisRequest; |
| | | import com.zy.ai.mcp.controller.McpController; |
| | | import com.zy.ai.mcp.service.SpringAiMcpToolManager; |
| | | import com.zy.ai.utils.AiPromptUtils; |
| | | import com.zy.ai.utils.AiUtils; |
| | | import com.zy.common.utils.RedisUtil; |
| | |
| | | @Autowired |
| | | private AiUtils aiUtils; |
| | | @Autowired(required = false) |
| | | private McpController mcpController; |
| | | private SpringAiMcpToolManager mcpToolManager; |
| | | |
| | | public void diagnoseStream(WcsDiagnosisRequest request, SseEmitter emitter) { |
| | | List<ChatCompletionRequest.Message> messages = new ArrayList<>(); |
| | |
| | | SseEmitter emitter, |
| | | String chatId) { |
| | | try { |
| | | if (mcpController == null) return false; |
| | | List<Object> tools = buildOpenAiTools(); |
| | | if (mcpToolManager == null) return false; |
| | | List<Object> tools = mcpToolManager.buildOpenAiTools(); |
| | | if (tools.isEmpty()) return false; |
| | | |
| | | baseMessages.add(systemPrompt); |
| | |
| | | } |
| | | Object output; |
| | | try { |
| | | output = mcpController.callTool(toolName, args); |
| | | output = mcpToolManager.callTool(toolName, args); |
| | | } catch (Exception e) { |
| | | java.util.LinkedHashMap<String, Object> err = new java.util.LinkedHashMap<String, Object>(); |
| | | err.put("tool", toolName); |
| | |
| | | } catch (Exception e) { |
| | | log.warn("SSE send failed", e); |
| | | } |
| | | } |
| | | |
| | | private List<Object> buildOpenAiTools() { |
| | | if (mcpController == null) return java.util.Collections.emptyList(); |
| | | List<Map<String, Object>> mcpTools = mcpController.listTools(); |
| | | if (mcpTools == null || mcpTools.isEmpty()) return java.util.Collections.emptyList(); |
| | | |
| | | List<Object> tools = new ArrayList<>(); |
| | | for (Map<String, Object> t : mcpTools) { |
| | | if (t == null) continue; |
| | | Object name = t.get("name"); |
| | | if (name == null) continue; |
| | | Object inputSchema = t.get("inputSchema"); |
| | | java.util.LinkedHashMap<String, Object> function = new java.util.LinkedHashMap<String, Object>(); |
| | | function.put("name", String.valueOf(name)); |
| | | Object desc = t.get("description"); |
| | | if (desc != null) function.put("description", String.valueOf(desc)); |
| | | function.put("parameters", inputSchema == null ? new java.util.LinkedHashMap<String, Object>() : inputSchema); |
| | | |
| | | java.util.LinkedHashMap<String, Object> tool = new java.util.LinkedHashMap<String, Object>(); |
| | | tool.put("type", "function"); |
| | | tool.put("function", function); |
| | | tools.add(tool); |
| | | } |
| | | return tools; |
| | | } |
| | | |
| | | private void sendLargeText(SseEmitter emitter, String text) { |