| | |
| | | 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; |
| | | import com.zy.core.enums.RedisKeyType; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | |
| | | private static final long CHAT_TTL_SECONDS = 7L * 24 * 3600; |
| | | |
| | | @Value("${llm.platform}") |
| | | private String platform; |
| | | @Autowired |
| | | private LlmChatService llmChatService; |
| | | @Autowired |
| | |
| | | @Autowired |
| | | private AiUtils aiUtils; |
| | | @Autowired(required = false) |
| | | private McpController mcpController; |
| | | @Autowired |
| | | private PythonService pythonService; |
| | | private SpringAiMcpToolManager mcpToolManager; |
| | | |
| | | public void diagnoseStream(WcsDiagnosisRequest request, SseEmitter emitter) { |
| | | List<ChatCompletionRequest.Message> messages = new ArrayList<>(); |
| | |
| | | try { |
| | | try { emitter.send(SseEmitter.event().data("【AI】运行已停止(异常)")); } catch (Exception ignore) {} |
| | | log.error("AI diagnose stream stopped: error", e); |
| | | emitter.completeWithError(e); |
| | | emitter.complete(); |
| | | } catch (Exception ignore) {} |
| | | }); |
| | | } |
| | |
| | | String chatId, |
| | | boolean reset, |
| | | SseEmitter emitter) { |
| | | if (platform.equals("python")) { |
| | | pythonService.runPython(prompt, chatId, emitter); |
| | | return; |
| | | } |
| | | |
| | | List<ChatCompletionRequest.Message> messages = new ArrayList<>(); |
| | | |
| | | List<ChatCompletionRequest.Message> history = null; |
| | |
| | | emitter.complete(); |
| | | } catch (Exception ignore) {} |
| | | }, e -> { |
| | | try { emitter.completeWithError(e); } catch (Exception ignore) {} |
| | | try { |
| | | try { emitter.send(SseEmitter.event().data("【AI】运行已停止(异常)")); } catch (Exception ignore) {} |
| | | emitter.complete(); |
| | | } catch (Exception ignore) {} |
| | | }); |
| | | } |
| | | |
| | |
| | | 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); |
| | |
| | | try { |
| | | sse(emitter, "\\n\\n【AI】运行已停止(异常)\\n\\n"); |
| | | log.error("AI MCP diagnose stopped: error", e); |
| | | emitter.completeWithError(e); |
| | | emitter.complete(); |
| | | } catch (Exception ignore) {} |
| | | return true; |
| | | } |
| | |
| | | } 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) { |