agentstudio 0.1.18 → 0.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -505
- package/bin/agentstudio.js +305 -0
- package/bin/agentstudio.js.map +1 -0
- package/bin/serviceManager.d.ts +16 -0
- package/bin/serviceManager.d.ts.map +1 -0
- package/bin/serviceManager.js +434 -0
- package/bin/serviceManager.js.map +1 -0
- package/config/index.d.ts +74 -0
- package/config/index.d.ts.map +1 -0
- package/config/index.js +166 -0
- package/config/index.js.map +1 -0
- package/config/paths.d.ts +81 -0
- package/config/paths.d.ts.map +1 -0
- package/config/paths.js +98 -0
- package/config/paths.js.map +1 -0
- package/index.d.ts +4 -0
- package/index.d.ts.map +1 -0
- package/index.js +369 -0
- package/index.js.map +1 -0
- package/jobs/taskTimeoutMonitor.d.ts +25 -0
- package/jobs/taskTimeoutMonitor.d.ts.map +1 -0
- package/jobs/taskTimeoutMonitor.js +94 -0
- package/jobs/taskTimeoutMonitor.js.map +1 -0
- package/middleware/a2aAuth.d.ts +38 -0
- package/middleware/a2aAuth.d.ts.map +1 -0
- package/middleware/a2aAuth.js +134 -0
- package/middleware/a2aAuth.js.map +1 -0
- package/{backend/dist/middleware → middleware}/auth.d.ts +3 -1
- package/middleware/auth.d.ts.map +1 -0
- package/middleware/auth.js +35 -0
- package/middleware/auth.js.map +1 -0
- package/middleware/httpsOnly.d.ts +38 -0
- package/middleware/httpsOnly.d.ts.map +1 -0
- package/middleware/httpsOnly.js +105 -0
- package/middleware/httpsOnly.js.map +1 -0
- package/middleware/rateLimiting.d.ts +37 -0
- package/middleware/rateLimiting.d.ts.map +1 -0
- package/middleware/rateLimiting.js +118 -0
- package/middleware/rateLimiting.js.map +1 -0
- package/package.json +43 -38
- package/public/assets/AgentsPage-Nvg2xu6K.js +10 -0
- package/public/assets/Button-Co56C389.js +1 -0
- package/public/assets/ChatPage-D6sol0ad.js +442 -0
- package/public/assets/CommandForm-Bk7F_HU8.js +7 -0
- package/public/assets/CommandsPage-DZHyOdKd.js +1 -0
- package/public/assets/DashboardPage-29qC7Ev1.js +15 -0
- package/public/assets/FileBrowser-BYweUPH4.js +6 -0
- package/public/assets/FileExplorer-DNYj6Tsx.js +1 -0
- package/public/assets/GeneralSettingsPage-D9Xa7Nd8.js +1 -0
- package/public/assets/LandingPage-CjnT-cvO.js +1 -0
- package/public/assets/LoginPage-DcaGNCXH.js +16 -0
- package/public/assets/McpAdminSettingsPage-DgR3E2Bd.js +7 -0
- package/public/assets/McpPage-CtWf1CoL.js +40 -0
- package/public/assets/MemorySettingsPage-JTry4Ccp.js +1 -0
- package/public/assets/PluginsPage-D-BHjQ3P.js +1 -0
- package/public/assets/ProjectSelector-DSXOjMQC.js +1 -0
- package/public/assets/ProjectsPage-lLUk4-Xa.js +21 -0
- package/public/assets/ScheduledTasksPage-5hPbd9Vs.js +1 -0
- package/public/assets/SettingsLayout-DGJspXLp.js +1 -0
- package/public/assets/SkillsPage-DBw0m7D2.js +18 -0
- package/{backend/dist/frontend/assets/SubagentForm-DXtTTIKg.js → public/assets/SubagentForm-BdUK1U4y.js} +2 -2
- package/public/assets/SubagentsPage-Dnq70IFd.js +1 -0
- package/{backend/dist/frontend/assets/ToastTestPage-DT4wuN5C.js → public/assets/ToastTestPage-BfJQRFIm.js} +1 -1
- package/public/assets/ToolsList-Dl5F2fWk.js +1 -0
- package/public/assets/UnifiedToolSelector-CNbPsiuq.js +1 -0
- package/public/assets/VersionSettingsPage-BfoCcham.js +5 -0
- package/public/assets/_basePickBy-7C_e0Xv1.js +1 -0
- package/public/assets/_baseUniq-eGJNLBzx.js +1 -0
- package/public/assets/agents-ChrA1R0y.js +1 -0
- package/public/assets/arc-CuA752eE.js +1 -0
- package/public/assets/architectureDiagram-VXUJARFQ-BnPYh1OW.js +36 -0
- package/public/assets/blockDiagram-VD42YOAC-72nsMt_i.js +122 -0
- package/public/assets/c4Diagram-YG6GDRKO-BNJSAXcg.js +10 -0
- package/public/assets/channel-CIunGC5m.js +1 -0
- package/public/assets/chunk-4BX2VUAB-RPxkCWhH.js +1 -0
- package/public/assets/chunk-55IACEB6-3ePDt0kp.js +1 -0
- package/public/assets/chunk-B4BG7PRW-CBg_BBfl.js +165 -0
- package/public/assets/chunk-DI55MBZ5-DIh69TUJ.js +220 -0
- package/public/assets/chunk-FMBD7UC4-BbjwhyTe.js +15 -0
- package/public/assets/chunk-QN33PNHL-BpH-o_YR.js +1 -0
- package/public/assets/chunk-QZHKN3VN-DYB7rh5Q.js +1 -0
- package/public/assets/chunk-TZMSLE5B-DPCYEVb3.js +1 -0
- package/public/assets/classDiagram-2ON5EDUG-aUQHFsTA.js +1 -0
- package/public/assets/classDiagram-v2-WZHVMYZB-aUQHFsTA.js +1 -0
- package/public/assets/clone-Ckf7tA1V.js +1 -0
- package/public/assets/cose-bilkent-S5V4N54A-CnpfmL-Y.js +1 -0
- package/public/assets/cytoscape.esm-DtBltrT8.js +331 -0
- package/public/assets/dagre-6UL2VRFP-DR9x77Xf.js +4 -0
- package/{backend/dist/frontend/assets/data-structures-DLJedtzx.js → public/assets/data-structures-C0h9Oap1.js} +1 -1
- package/public/assets/defaultLocale-C4B-KCzX.js +1 -0
- package/public/assets/diagram-PSM6KHXK-SONPsQNx.js +24 -0
- package/public/assets/diagram-QEK2KX5R-BKYFSfC1.js +43 -0
- package/public/assets/diagram-S2PKOQOG-CkM0APZj.js +24 -0
- package/public/assets/erDiagram-Q2GNP2WA-BX1DpOGx.js +60 -0
- package/public/assets/flowDiagram-NV44I4VS-HAAlzNbq.js +162 -0
- package/public/assets/ganttDiagram-LVOFAZNH-BqzWexqa.js +267 -0
- package/public/assets/gitGraphDiagram-NY62KEGX-CIPmSp43.js +65 -0
- package/public/assets/graph-m515btDj.js +1 -0
- package/public/assets/index-Bn3v3S9-.js +293 -0
- package/public/assets/index-DWieeYj4.css +1 -0
- package/public/assets/infoDiagram-F6ZHWCRC-Pn4yNWrF.js +2 -0
- package/public/assets/init-Gi6I4Gst.js +1 -0
- package/public/assets/journeyDiagram-XKPGCS4Q-BfzTomS0.js +139 -0
- package/public/assets/kanban-definition-3W4ZIXB7-Cgju7b-L.js +89 -0
- package/public/assets/katex-qrhCpa0F.js +261 -0
- package/public/assets/layout-BKQfQSxJ.js +1 -0
- package/public/assets/linear-DigtLz3B.js +1 -0
- package/public/assets/mindmap-definition-VGOIOE7T-qopraVFy.js +68 -0
- package/public/assets/monaco-editor-DHKm5-VF.js +19 -0
- package/public/assets/ordinal-Cboi1Yqb.js +1 -0
- package/public/assets/pieDiagram-ADFJNKIX-BcoaAI-L.js +30 -0
- package/public/assets/quadrantDiagram-AYHSOK5B-D-DwcoSd.js +7 -0
- package/public/assets/requirementDiagram-UZGBJVZJ-DbQCpx77.js +64 -0
- package/public/assets/sankeyDiagram-TZEHDZUN-BVk8387S.js +10 -0
- package/public/assets/sequenceDiagram-WL72ISMW-BdzICjxO.js +145 -0
- package/public/assets/stateDiagram-FKZM4ZOC-CYY-uUvJ.js +1 -0
- package/public/assets/stateDiagram-v2-4FDKWEC3-Dh2Kvomq.js +1 -0
- package/{backend/dist/frontend/assets/syntax-highlighting-YWvMU4Hm.js → public/assets/syntax-highlighting-CnREyncB.js} +5 -5
- package/public/assets/tabManager-B2LQO_Ll.js +30 -0
- package/{backend/dist/frontend/assets/table-D6q1rytw.js → public/assets/table-D0L2RL5i.js} +1 -1
- package/public/assets/timeline-definition-IT6M3QCI-BAvjPYvX.js +61 -0
- package/public/assets/tools-IcPNZlPj.js +1 -0
- package/public/assets/treemap-KMMF4GRG-DtkpVA56.js +128 -0
- package/{backend/dist/frontend/assets/ui-components-Cw21Epuw.js → public/assets/ui-components-D1St49qC.js} +231 -96
- package/public/assets/useAgents-BnDTkOG8.js +2 -0
- package/public/assets/useClaudeVersions-CD59tFWM.js +1 -0
- package/public/assets/useCommands-mvMu3mMD.js +1 -0
- package/public/assets/useProjects-YXOjaOwL.js +1 -0
- package/public/assets/xychartDiagram-PRI3JC2R-ByBTDWE2.js +7 -0
- package/{backend/dist/frontend → public}/index.html +8 -8
- package/routes/__tests__/a2a.integration.test.d.ts +11 -0
- package/routes/__tests__/a2a.integration.test.d.ts.map +1 -0
- package/routes/__tests__/a2a.integration.test.js +314 -0
- package/routes/__tests__/a2a.integration.test.js.map +1 -0
- package/routes/__tests__/a2a.test.d.ts +6 -0
- package/routes/__tests__/a2a.test.d.ts.map +1 -0
- package/routes/__tests__/a2a.test.js +622 -0
- package/routes/__tests__/a2a.test.js.map +1 -0
- package/routes/__tests__/agents.test.d.ts +6 -0
- package/routes/__tests__/agents.test.d.ts.map +1 -0
- package/routes/__tests__/agents.test.js +315 -0
- package/routes/__tests__/agents.test.js.map +1 -0
- package/routes/__tests__/sessions.test.d.ts +7 -0
- package/routes/__tests__/sessions.test.d.ts.map +1 -0
- package/routes/__tests__/sessions.test.js +330 -0
- package/routes/__tests__/sessions.test.js.map +1 -0
- package/routes/a2a.d.ts +18 -0
- package/routes/a2a.d.ts.map +1 -0
- package/routes/a2a.js +649 -0
- package/routes/a2a.js.map +1 -0
- package/routes/a2a.streaming.test.d.ts +2 -0
- package/routes/a2a.streaming.test.d.ts.map +1 -0
- package/routes/a2a.streaming.test.js +167 -0
- package/routes/a2a.streaming.test.js.map +1 -0
- package/routes/a2aManagement.d.ts +21 -0
- package/routes/a2aManagement.d.ts.map +1 -0
- package/routes/a2aManagement.js +466 -0
- package/routes/a2aManagement.js.map +1 -0
- package/{backend/dist/routes → routes}/agents.d.ts.map +1 -1
- package/routes/agents.js +997 -0
- package/routes/agents.js.map +1 -0
- package/{backend/dist/routes → routes}/auth.d.ts.map +1 -1
- package/routes/auth.js +135 -0
- package/routes/auth.js.map +1 -0
- package/{backend/dist/routes → routes}/commands.d.ts.map +1 -1
- package/{backend/dist/routes → routes}/commands.js +115 -31
- package/routes/commands.js.map +1 -0
- package/routes/config.d.ts +4 -0
- package/routes/config.d.ts.map +1 -0
- package/routes/config.js +211 -0
- package/routes/config.js.map +1 -0
- package/{backend/dist/routes → routes}/files.d.ts.map +1 -1
- package/{backend/dist/routes → routes}/files.js +103 -43
- package/routes/files.js.map +1 -0
- package/routes/mcp.d.ts +27 -0
- package/routes/mcp.d.ts.map +1 -0
- package/{backend/dist/routes → routes}/mcp.js +195 -105
- package/routes/mcp.js.map +1 -0
- package/routes/mcpAdmin.d.ts +16 -0
- package/routes/mcpAdmin.d.ts.map +1 -0
- package/routes/mcpAdmin.js +308 -0
- package/routes/mcpAdmin.js.map +1 -0
- package/routes/mcpAdminManagement.d.ts +17 -0
- package/routes/mcpAdminManagement.d.ts.map +1 -0
- package/routes/mcpAdminManagement.js +345 -0
- package/routes/mcpAdminManagement.js.map +1 -0
- package/routes/media.d.ts.map +1 -0
- package/{backend/dist/routes → routes}/media.js +52 -42
- package/routes/media.js.map +1 -0
- package/routes/mediaAuth.d.ts +8 -0
- package/routes/mediaAuth.d.ts.map +1 -0
- package/routes/mediaAuth.js +136 -0
- package/routes/mediaAuth.js.map +1 -0
- package/routes/plugins.d.ts +4 -0
- package/routes/plugins.d.ts.map +1 -0
- package/routes/plugins.js +339 -0
- package/routes/plugins.js.map +1 -0
- package/{backend/dist/routes → routes}/projects.d.ts.map +1 -1
- package/routes/projects.js +884 -0
- package/routes/projects.js.map +1 -0
- package/routes/scheduledTasks.d.ts +9 -0
- package/routes/scheduledTasks.d.ts.map +1 -0
- package/routes/scheduledTasks.js +320 -0
- package/routes/scheduledTasks.js.map +1 -0
- package/{backend/dist/routes → routes}/sessions.d.ts.map +1 -1
- package/{backend/dist/routes → routes}/sessions.js +258 -39
- package/routes/sessions.js.map +1 -0
- package/{backend/dist/routes → routes}/settings.d.ts.map +1 -1
- package/{backend/dist/routes → routes}/settings.js +82 -241
- package/routes/settings.js.map +1 -0
- package/{backend/dist/routes/mcp.d.ts → routes/skills.d.ts} +1 -1
- package/routes/skills.d.ts.map +1 -0
- package/routes/skills.js +272 -0
- package/routes/skills.js.map +1 -0
- package/routes/slack.d.ts +10 -0
- package/routes/slack.d.ts.map +1 -0
- package/routes/slack.js +189 -0
- package/routes/slack.js.map +1 -0
- package/{backend/dist/routes → routes}/slides.d.ts.map +1 -1
- package/{backend/dist/routes → routes}/slides.js +29 -27
- package/routes/slides.js.map +1 -0
- package/{backend/dist/routes → routes}/subagents.js +27 -22
- package/routes/subagents.js.map +1 -0
- package/schemas/a2a.d.ts +858 -0
- package/schemas/a2a.d.ts.map +1 -0
- package/schemas/a2a.js +300 -0
- package/schemas/a2a.js.map +1 -0
- package/scripts/postinstall.js +10 -0
- package/services/__tests__/pluginInstaller.test.d.ts +5 -0
- package/services/__tests__/pluginInstaller.test.d.ts.map +1 -0
- package/services/__tests__/pluginInstaller.test.js +290 -0
- package/services/__tests__/pluginInstaller.test.js.map +1 -0
- package/services/__tests__/pluginParser.test.d.ts +5 -0
- package/services/__tests__/pluginParser.test.d.ts.map +1 -0
- package/services/__tests__/pluginParser.test.js +272 -0
- package/services/__tests__/pluginParser.test.js.map +1 -0
- package/services/__tests__/pluginPaths.test.d.ts +5 -0
- package/services/__tests__/pluginPaths.test.d.ts.map +1 -0
- package/services/__tests__/pluginPaths.test.js +221 -0
- package/services/__tests__/pluginPaths.test.js.map +1 -0
- package/services/__tests__/pluginScanner.test.d.ts +5 -0
- package/services/__tests__/pluginScanner.test.d.ts.map +1 -0
- package/services/__tests__/pluginScanner.test.js +272 -0
- package/services/__tests__/pluginScanner.test.js.map +1 -0
- package/services/__tests__/pluginSymlink.test.d.ts +5 -0
- package/services/__tests__/pluginSymlink.test.d.ts.map +1 -0
- package/services/__tests__/pluginSymlink.test.js +318 -0
- package/services/__tests__/pluginSymlink.test.js.map +1 -0
- package/services/__tests__/slackAIService.test.d.ts +5 -0
- package/services/__tests__/slackAIService.test.d.ts.map +1 -0
- package/services/__tests__/slackAIService.test.js +477 -0
- package/services/__tests__/slackAIService.test.js.map +1 -0
- package/services/__tests__/slackThreadMapper.test.d.ts +5 -0
- package/services/__tests__/slackThreadMapper.test.d.ts.map +1 -0
- package/services/__tests__/slackThreadMapper.test.js +194 -0
- package/services/__tests__/slackThreadMapper.test.js.map +1 -0
- package/services/a2a/__tests__/a2aClientTool.integration.test.d.ts +6 -0
- package/services/a2a/__tests__/a2aClientTool.integration.test.d.ts.map +1 -0
- package/services/a2a/__tests__/a2aClientTool.integration.test.js +264 -0
- package/services/a2a/__tests__/a2aClientTool.integration.test.js.map +1 -0
- package/services/a2a/__tests__/a2aClientTool.test.d.ts +6 -0
- package/services/a2a/__tests__/a2aClientTool.test.d.ts.map +1 -0
- package/services/a2a/__tests__/a2aClientTool.test.js +418 -0
- package/services/a2a/__tests__/a2aClientTool.test.js.map +1 -0
- package/services/a2a/__tests__/a2aConfigService.test.d.ts +6 -0
- package/services/a2a/__tests__/a2aConfigService.test.d.ts.map +1 -0
- package/services/a2a/__tests__/a2aConfigService.test.js +431 -0
- package/services/a2a/__tests__/a2aConfigService.test.js.map +1 -0
- package/services/a2a/__tests__/a2aConfigServicePath.test.d.ts +2 -0
- package/services/a2a/__tests__/a2aConfigServicePath.test.d.ts.map +1 -0
- package/services/a2a/__tests__/a2aConfigServicePath.test.js +49 -0
- package/services/a2a/__tests__/a2aConfigServicePath.test.js.map +1 -0
- package/services/a2a/__tests__/a2aSdkMcp.test.d.ts +10 -0
- package/services/a2a/__tests__/a2aSdkMcp.test.d.ts.map +1 -0
- package/services/a2a/__tests__/a2aSdkMcp.test.js +239 -0
- package/services/a2a/__tests__/a2aSdkMcp.test.js.map +1 -0
- package/services/a2a/__tests__/agentCardService.test.d.ts +6 -0
- package/services/a2a/__tests__/agentCardService.test.d.ts.map +1 -0
- package/services/a2a/__tests__/agentCardService.test.js +292 -0
- package/services/a2a/__tests__/agentCardService.test.js.map +1 -0
- package/services/a2a/__tests__/apiKeyService.test.d.ts +6 -0
- package/services/a2a/__tests__/apiKeyService.test.d.ts.map +1 -0
- package/services/a2a/__tests__/apiKeyService.test.js +284 -0
- package/services/a2a/__tests__/apiKeyService.test.js.map +1 -0
- package/services/a2a/__tests__/buildQueryOptionsIntegration.test.d.ts +2 -0
- package/services/a2a/__tests__/buildQueryOptionsIntegration.test.d.ts.map +1 -0
- package/services/a2a/__tests__/buildQueryOptionsIntegration.test.js +70 -0
- package/services/a2a/__tests__/buildQueryOptionsIntegration.test.js.map +1 -0
- package/services/a2a/__tests__/dynamic_config_verification.test.d.ts +2 -0
- package/services/a2a/__tests__/dynamic_config_verification.test.d.ts.map +1 -0
- package/services/a2a/__tests__/dynamic_config_verification.test.js +67 -0
- package/services/a2a/__tests__/dynamic_config_verification.test.js.map +1 -0
- package/services/a2a/__tests__/integrateA2AMcpServer.test.d.ts +2 -0
- package/services/a2a/__tests__/integrateA2AMcpServer.test.d.ts.map +1 -0
- package/services/a2a/__tests__/integrateA2AMcpServer.test.js +112 -0
- package/services/a2a/__tests__/integrateA2AMcpServer.test.js.map +1 -0
- package/services/a2a/__tests__/taskManager.integration.test.d.ts +7 -0
- package/services/a2a/__tests__/taskManager.integration.test.d.ts.map +1 -0
- package/services/a2a/__tests__/taskManager.integration.test.js +346 -0
- package/services/a2a/__tests__/taskManager.integration.test.js.map +1 -0
- package/services/a2a/__tests__/taskManager.test.d.ts +7 -0
- package/services/a2a/__tests__/taskManager.test.d.ts.map +1 -0
- package/services/a2a/__tests__/taskManager.test.js +423 -0
- package/services/a2a/__tests__/taskManager.test.js.map +1 -0
- package/services/a2a/a2aClientTool.d.ts +73 -0
- package/services/a2a/a2aClientTool.d.ts.map +1 -0
- package/services/a2a/a2aClientTool.js +572 -0
- package/services/a2a/a2aClientTool.js.map +1 -0
- package/services/a2a/a2aConfigService.d.ts +50 -0
- package/services/a2a/a2aConfigService.d.ts.map +1 -0
- package/services/a2a/a2aConfigService.js +186 -0
- package/services/a2a/a2aConfigService.js.map +1 -0
- package/services/a2a/a2aHistoryService.d.ts +32 -0
- package/services/a2a/a2aHistoryService.d.ts.map +1 -0
- package/services/a2a/a2aHistoryService.js +108 -0
- package/services/a2a/a2aHistoryService.js.map +1 -0
- package/services/a2a/a2aIntegration.d.ts +9 -0
- package/services/a2a/a2aIntegration.d.ts.map +1 -0
- package/services/a2a/a2aIntegration.js +41 -0
- package/services/a2a/a2aIntegration.js.map +1 -0
- package/services/a2a/a2aQueryService.d.ts +71 -0
- package/services/a2a/a2aQueryService.d.ts.map +1 -0
- package/services/a2a/a2aQueryService.js +166 -0
- package/services/a2a/a2aQueryService.js.map +1 -0
- package/services/a2a/a2aSdkMcp.d.ts +51 -0
- package/services/a2a/a2aSdkMcp.d.ts.map +1 -0
- package/services/a2a/a2aSdkMcp.js +185 -0
- package/services/a2a/a2aSdkMcp.js.map +1 -0
- package/services/a2a/a2aStreamEvents.d.ts +94 -0
- package/services/a2a/a2aStreamEvents.d.ts.map +1 -0
- package/services/a2a/a2aStreamEvents.js +92 -0
- package/services/a2a/a2aStreamEvents.js.map +1 -0
- package/services/a2a/agentCardService.d.ts +34 -0
- package/services/a2a/agentCardService.d.ts.map +1 -0
- package/services/a2a/agentCardService.js +228 -0
- package/services/a2a/agentCardService.js.map +1 -0
- package/services/a2a/agentMappingService.d.ts +53 -0
- package/services/a2a/agentMappingService.d.ts.map +1 -0
- package/services/a2a/agentMappingService.js +185 -0
- package/services/a2a/agentMappingService.js.map +1 -0
- package/services/a2a/apiKeyService.d.ts +101 -0
- package/services/a2a/apiKeyService.d.ts.map +1 -0
- package/services/a2a/apiKeyService.js +314 -0
- package/services/a2a/apiKeyService.js.map +1 -0
- package/services/a2a/taskCleanup.d.ts +30 -0
- package/services/a2a/taskCleanup.d.ts.map +1 -0
- package/services/a2a/taskCleanup.js +184 -0
- package/services/a2a/taskCleanup.js.map +1 -0
- package/services/a2a/taskManager.d.ts +86 -0
- package/services/a2a/taskManager.d.ts.map +1 -0
- package/services/a2a/taskManager.js +263 -0
- package/services/a2a/taskManager.js.map +1 -0
- package/services/agentStorage.d.ts +27 -0
- package/services/agentStorage.d.ts.map +1 -0
- package/services/agentStorage.js +487 -0
- package/services/agentStorage.js.map +1 -0
- package/services/askUserQuestion/askUserQuestionIntegration.d.ts +24 -0
- package/services/askUserQuestion/askUserQuestionIntegration.d.ts.map +1 -0
- package/services/askUserQuestion/askUserQuestionIntegration.js +52 -0
- package/services/askUserQuestion/askUserQuestionIntegration.js.map +1 -0
- package/services/askUserQuestion/askUserQuestionMcp.d.ts +103 -0
- package/services/askUserQuestion/askUserQuestionMcp.d.ts.map +1 -0
- package/services/askUserQuestion/askUserQuestionMcp.js +129 -0
- package/services/askUserQuestion/askUserQuestionMcp.js.map +1 -0
- package/services/askUserQuestion/index.d.ts +13 -0
- package/services/askUserQuestion/index.d.ts.map +1 -0
- package/services/askUserQuestion/index.js +35 -0
- package/services/askUserQuestion/index.js.map +1 -0
- package/services/askUserQuestion/init.d.ts +17 -0
- package/services/askUserQuestion/init.d.ts.map +1 -0
- package/services/askUserQuestion/init.js +47 -0
- package/services/askUserQuestion/init.js.map +1 -0
- package/services/askUserQuestion/notificationChannel.d.ts +97 -0
- package/services/askUserQuestion/notificationChannel.d.ts.map +1 -0
- package/services/askUserQuestion/notificationChannel.js +147 -0
- package/services/askUserQuestion/notificationChannel.js.map +1 -0
- package/services/askUserQuestion/slackNotificationChannel.d.ts +35 -0
- package/services/askUserQuestion/slackNotificationChannel.d.ts.map +1 -0
- package/services/askUserQuestion/slackNotificationChannel.js +129 -0
- package/services/askUserQuestion/slackNotificationChannel.js.map +1 -0
- package/services/askUserQuestion/sseNotificationChannel.d.ts +36 -0
- package/services/askUserQuestion/sseNotificationChannel.d.ts.map +1 -0
- package/services/askUserQuestion/sseNotificationChannel.js +88 -0
- package/services/askUserQuestion/sseNotificationChannel.js.map +1 -0
- package/services/askUserQuestion/userInputRegistry.d.ts +107 -0
- package/services/askUserQuestion/userInputRegistry.d.ts.map +1 -0
- package/services/askUserQuestion/userInputRegistry.js +253 -0
- package/services/askUserQuestion/userInputRegistry.js.map +1 -0
- package/{backend/dist/services → services}/claudeSession.d.ts +20 -5
- package/services/claudeSession.d.ts.map +1 -0
- package/{backend/dist/services → services}/claudeSession.js +123 -41
- package/services/claudeSession.js.map +1 -0
- package/services/claudeVersionStorage.d.ts +20 -0
- package/services/claudeVersionStorage.d.ts.map +1 -0
- package/services/claudeVersionStorage.js +331 -0
- package/services/claudeVersionStorage.js.map +1 -0
- package/services/mcpAdmin/__tests__/adminApiKeyService.test.d.ts +5 -0
- package/services/mcpAdmin/__tests__/adminApiKeyService.test.d.ts.map +1 -0
- package/services/mcpAdmin/__tests__/adminApiKeyService.test.js +289 -0
- package/services/mcpAdmin/__tests__/adminApiKeyService.test.js.map +1 -0
- package/services/mcpAdmin/__tests__/mcpAdminRoutes.test.d.ts +5 -0
- package/services/mcpAdmin/__tests__/mcpAdminRoutes.test.d.ts.map +1 -0
- package/services/mcpAdmin/__tests__/mcpAdminRoutes.test.js +345 -0
- package/services/mcpAdmin/__tests__/mcpAdminRoutes.test.js.map +1 -0
- package/services/mcpAdmin/__tests__/mcpAdminServer.test.d.ts +5 -0
- package/services/mcpAdmin/__tests__/mcpAdminServer.test.d.ts.map +1 -0
- package/services/mcpAdmin/__tests__/mcpAdminServer.test.js +453 -0
- package/services/mcpAdmin/__tests__/mcpAdminServer.test.js.map +1 -0
- package/services/mcpAdmin/__tests__/tools.test.d.ts +5 -0
- package/services/mcpAdmin/__tests__/tools.test.d.ts.map +1 -0
- package/services/mcpAdmin/__tests__/tools.test.js +371 -0
- package/services/mcpAdmin/__tests__/tools.test.js.map +1 -0
- package/services/mcpAdmin/adminApiKeyService.d.ts +61 -0
- package/services/mcpAdmin/adminApiKeyService.d.ts.map +1 -0
- package/services/mcpAdmin/adminApiKeyService.js +270 -0
- package/services/mcpAdmin/adminApiKeyService.js.map +1 -0
- package/services/mcpAdmin/index.d.ts +10 -0
- package/services/mcpAdmin/index.d.ts.map +1 -0
- package/services/mcpAdmin/index.js +43 -0
- package/services/mcpAdmin/index.js.map +1 -0
- package/services/mcpAdmin/mcpAdminServer.d.ts +76 -0
- package/services/mcpAdmin/mcpAdminServer.d.ts.map +1 -0
- package/services/mcpAdmin/mcpAdminServer.js +243 -0
- package/services/mcpAdmin/mcpAdminServer.js.map +1 -0
- package/services/mcpAdmin/tools/agentTools.d.ts +27 -0
- package/services/mcpAdmin/tools/agentTools.d.ts.map +1 -0
- package/services/mcpAdmin/tools/agentTools.js +359 -0
- package/services/mcpAdmin/tools/agentTools.js.map +1 -0
- package/services/mcpAdmin/tools/index.d.ts +15 -0
- package/services/mcpAdmin/tools/index.d.ts.map +1 -0
- package/services/mcpAdmin/tools/index.js +30 -0
- package/services/mcpAdmin/tools/index.js.map +1 -0
- package/services/mcpAdmin/tools/mcpServerTools.d.ts +27 -0
- package/services/mcpAdmin/tools/mcpServerTools.d.ts.map +1 -0
- package/services/mcpAdmin/tools/mcpServerTools.js +334 -0
- package/services/mcpAdmin/tools/mcpServerTools.js.map +1 -0
- package/services/mcpAdmin/tools/projectTools.d.ts +27 -0
- package/services/mcpAdmin/tools/projectTools.d.ts.map +1 -0
- package/services/mcpAdmin/tools/projectTools.js +353 -0
- package/services/mcpAdmin/tools/projectTools.js.map +1 -0
- package/services/mcpAdmin/tools/systemTools.d.ts +23 -0
- package/services/mcpAdmin/tools/systemTools.d.ts.map +1 -0
- package/services/mcpAdmin/tools/systemTools.js +241 -0
- package/services/mcpAdmin/tools/systemTools.js.map +1 -0
- package/services/mcpAdmin/types.d.ts +124 -0
- package/services/mcpAdmin/types.d.ts.map +1 -0
- package/services/mcpAdmin/types.js +18 -0
- package/services/mcpAdmin/types.js.map +1 -0
- package/{backend/dist/services → services}/messageQueue.d.ts.map +1 -1
- package/{backend/dist/services → services}/messageQueue.js +16 -3
- package/services/messageQueue.js.map +1 -0
- package/services/pluginInstaller.d.ts +58 -0
- package/services/pluginInstaller.d.ts.map +1 -0
- package/services/pluginInstaller.js +321 -0
- package/services/pluginInstaller.js.map +1 -0
- package/services/pluginParser.d.ts +45 -0
- package/services/pluginParser.d.ts.map +1 -0
- package/services/pluginParser.js +437 -0
- package/services/pluginParser.js.map +1 -0
- package/services/pluginPaths.d.ts +80 -0
- package/services/pluginPaths.d.ts.map +1 -0
- package/services/pluginPaths.js +274 -0
- package/services/pluginPaths.js.map +1 -0
- package/services/pluginScanner.d.ts +36 -0
- package/services/pluginScanner.d.ts.map +1 -0
- package/services/pluginScanner.js +251 -0
- package/services/pluginScanner.js.map +1 -0
- package/services/pluginSymlink.d.ts +54 -0
- package/services/pluginSymlink.d.ts.map +1 -0
- package/services/pluginSymlink.js +223 -0
- package/services/pluginSymlink.js.map +1 -0
- package/services/projectMetadataStorage.d.ts +114 -0
- package/services/projectMetadataStorage.d.ts.map +1 -0
- package/services/projectMetadataStorage.js +711 -0
- package/services/projectMetadataStorage.js.map +1 -0
- package/services/scheduledTaskStorage.d.ts +52 -0
- package/services/scheduledTaskStorage.d.ts.map +1 -0
- package/services/scheduledTaskStorage.js +285 -0
- package/services/scheduledTaskStorage.js.map +1 -0
- package/services/schedulerService.d.ts +81 -0
- package/services/schedulerService.d.ts.map +1 -0
- package/services/schedulerService.js +743 -0
- package/services/schedulerService.js.map +1 -0
- package/{backend/dist/services → services}/sessionManager.d.ts +6 -4
- package/services/sessionManager.d.ts.map +1 -0
- package/{backend/dist/services → services}/sessionManager.js +77 -16
- package/services/sessionManager.js.map +1 -0
- package/services/skillStorage.d.ts +60 -0
- package/services/skillStorage.d.ts.map +1 -0
- package/services/skillStorage.js +398 -0
- package/services/skillStorage.js.map +1 -0
- package/services/slackAIService.d.ts +81 -0
- package/services/slackAIService.d.ts.map +1 -0
- package/services/slackAIService.js +1091 -0
- package/services/slackAIService.js.map +1 -0
- package/services/slackClient.d.ts +46 -0
- package/services/slackClient.d.ts.map +1 -0
- package/services/slackClient.js +85 -0
- package/services/slackClient.js.map +1 -0
- package/services/slackSessionLock.d.ts +79 -0
- package/services/slackSessionLock.d.ts.map +1 -0
- package/services/slackSessionLock.js +353 -0
- package/services/slackSessionLock.js.map +1 -0
- package/services/slackThreadMapper.d.ts +57 -0
- package/services/slackThreadMapper.d.ts.map +1 -0
- package/services/slackThreadMapper.js +140 -0
- package/services/slackThreadMapper.js.map +1 -0
- package/types/a2a.d.ts +275 -0
- package/types/a2a.d.ts.map +1 -0
- package/types/a2a.js +23 -0
- package/types/a2a.js.map +1 -0
- package/types/agents.d.ts +95 -0
- package/types/agents.d.ts.map +1 -0
- package/types/agents.js +46 -0
- package/types/agents.js.map +1 -0
- package/types/claude-history.d.ts +62 -0
- package/types/claude-history.d.ts.map +1 -0
- package/types/claude-history.js +4 -0
- package/types/claude-history.js.map +1 -0
- package/types/claude-versions.d.ts +36 -0
- package/types/claude-versions.d.ts.map +1 -0
- package/types/claude-versions.js +3 -0
- package/types/claude-versions.js.map +1 -0
- package/types/commands.d.ts +50 -0
- package/types/commands.d.ts.map +1 -0
- package/types/commands.js +23 -0
- package/types/commands.js.map +1 -0
- package/types/plugins.d.ts +144 -0
- package/types/plugins.d.ts.map +1 -0
- package/types/plugins.js +8 -0
- package/types/plugins.js.map +1 -0
- package/types/projects.d.ts +42 -0
- package/types/projects.d.ts.map +1 -0
- package/types/projects.js +4 -0
- package/types/projects.js.map +1 -0
- package/types/scheduledTasks.d.ts +164 -0
- package/types/scheduledTasks.d.ts.map +1 -0
- package/types/scheduledTasks.js +17 -0
- package/types/scheduledTasks.js.map +1 -0
- package/types/skills.d.ts +91 -0
- package/types/skills.d.ts.map +1 -0
- package/types/skills.js +6 -0
- package/types/skills.js.map +1 -0
- package/types/slack.d.ts +97 -0
- package/types/slack.d.ts.map +1 -0
- package/types/slack.js +8 -0
- package/types/slack.js.map +1 -0
- package/types/streaming.d.ts +12 -0
- package/types/streaming.d.ts.map +1 -0
- package/types/streaming.js +8 -0
- package/types/streaming.js.map +1 -0
- package/types/subagents.d.ts +26 -0
- package/types/subagents.d.ts.map +1 -0
- package/types/subagents.js +3 -0
- package/types/subagents.js.map +1 -0
- package/utils/__tests__/claudeUtils.test.d.ts +5 -0
- package/utils/__tests__/claudeUtils.test.d.ts.map +1 -0
- package/utils/__tests__/claudeUtils.test.js +282 -0
- package/utils/__tests__/claudeUtils.test.js.map +1 -0
- package/utils/__tests__/sessionUtils.test.d.ts +5 -0
- package/utils/__tests__/sessionUtils.test.d.ts.map +1 -0
- package/utils/__tests__/sessionUtils.test.js +295 -0
- package/utils/__tests__/sessionUtils.test.js.map +1 -0
- package/utils/agentCardCache.d.ts +93 -0
- package/utils/agentCardCache.d.ts.map +1 -0
- package/utils/agentCardCache.js +212 -0
- package/utils/agentCardCache.js.map +1 -0
- package/utils/claudeUtils.d.ts +54 -0
- package/utils/claudeUtils.d.ts.map +1 -0
- package/utils/claudeUtils.js +387 -0
- package/utils/claudeUtils.js.map +1 -0
- package/utils/fileUtils.d.ts +2 -0
- package/utils/fileUtils.d.ts.map +1 -0
- package/utils/fileUtils.js +11 -0
- package/utils/fileUtils.js.map +1 -0
- package/utils/jwt.d.ts +30 -0
- package/utils/jwt.d.ts.map +1 -0
- package/utils/jwt.js +95 -0
- package/utils/jwt.js.map +1 -0
- package/utils/sessionUtils.d.ts +64 -0
- package/utils/sessionUtils.d.ts.map +1 -0
- package/utils/sessionUtils.js +266 -0
- package/utils/sessionUtils.js.map +1 -0
- package/README.zh-CN.md +0 -525
- package/backend/dist/bin/agentstudio.js +0 -120
- package/backend/dist/bin/agentstudio.js.map +0 -1
- package/backend/dist/frontend/assets/AgentsPage-Dqb_aqAA.js +0 -1
- package/backend/dist/frontend/assets/ChatPage-L8Paywyc.js +0 -91
- package/backend/dist/frontend/assets/CommandForm-DLl7EIMS.js +0 -7
- package/backend/dist/frontend/assets/CommandsPage-Bzavq0Ec.js +0 -1
- package/backend/dist/frontend/assets/DashboardPage-B3o4AYFT.js +0 -15
- package/backend/dist/frontend/assets/FileBrowser-DL3ayaqb.js +0 -1
- package/backend/dist/frontend/assets/GeneralSettingsPage-CBN_de-V.js +0 -1
- package/backend/dist/frontend/assets/LandingPage-Dl4ioKos.js +0 -1
- package/backend/dist/frontend/assets/LoginPage-4QqRdiSi.js +0 -12
- package/backend/dist/frontend/assets/McpPage-CY3tYiqj.js +0 -39
- package/backend/dist/frontend/assets/MemorySettingsPage-DGxrok5K.js +0 -1
- package/backend/dist/frontend/assets/ProjectSelector-hgmGYVFh.js +0 -1
- package/backend/dist/frontend/assets/ProjectsPage-D399IM0c.js +0 -14
- package/backend/dist/frontend/assets/SettingsLayout-CL_K-lzJ.js +0 -1
- package/backend/dist/frontend/assets/SubagentsPage-Chbhj8p2.js +0 -1
- package/backend/dist/frontend/assets/UnifiedToolSelector-CsM9qBvs.js +0 -1
- package/backend/dist/frontend/assets/VersionSettingsPage-74Q-LVgA.js +0 -5
- package/backend/dist/frontend/assets/agents-ClAzIJTw.js +0 -1
- package/backend/dist/frontend/assets/authFetch-BATQyPG5.js +0 -1
- package/backend/dist/frontend/assets/index-B9YHa7XT.css +0 -1
- package/backend/dist/frontend/assets/index-B_CTNvca.js +0 -268
- package/backend/dist/frontend/assets/monaco-editor-C7Z4sOhS.js +0 -19
- package/backend/dist/frontend/assets/tabManager-DV8urRBM.js +0 -30
- package/backend/dist/frontend/assets/tools-C4EPanYi.js +0 -1
- package/backend/dist/frontend/assets/useAgents-DwnOE1_k.js +0 -2
- package/backend/dist/frontend/assets/useClaudeVersions-CQdGnCqv.js +0 -1
- package/backend/dist/frontend/assets/useCommands-CCVaurbt.js +0 -1
- package/backend/dist/index.d.ts +0 -3
- package/backend/dist/index.d.ts.map +0 -1
- package/backend/dist/index.js +0 -157
- package/backend/dist/index.js.map +0 -1
- package/backend/dist/middleware/auth.d.ts.map +0 -1
- package/backend/dist/middleware/auth.js +0 -21
- package/backend/dist/middleware/auth.js.map +0 -1
- package/backend/dist/routes/agents.js +0 -803
- package/backend/dist/routes/agents.js.map +0 -1
- package/backend/dist/routes/auth.js +0 -60
- package/backend/dist/routes/auth.js.map +0 -1
- package/backend/dist/routes/commands.js.map +0 -1
- package/backend/dist/routes/files.js.map +0 -1
- package/backend/dist/routes/mcp.d.ts.map +0 -1
- package/backend/dist/routes/mcp.js.map +0 -1
- package/backend/dist/routes/media.d.ts.map +0 -1
- package/backend/dist/routes/media.js.map +0 -1
- package/backend/dist/routes/projects.js +0 -528
- package/backend/dist/routes/projects.js.map +0 -1
- package/backend/dist/routes/sessions.js.map +0 -1
- package/backend/dist/routes/settings.js.map +0 -1
- package/backend/dist/routes/slides.js.map +0 -1
- package/backend/dist/routes/subagents.js.map +0 -1
- package/backend/dist/services/claudeSession.d.ts.map +0 -1
- package/backend/dist/services/claudeSession.js.map +0 -1
- package/backend/dist/services/messageQueue.js.map +0 -1
- package/backend/dist/services/sessionManager.d.ts.map +0 -1
- package/backend/dist/services/sessionManager.js.map +0 -1
- package/backend/dist/utils/jwt.d.ts +0 -15
- package/backend/dist/utils/jwt.d.ts.map +0 -1
- package/backend/dist/utils/jwt.js +0 -28
- package/backend/dist/utils/jwt.js.map +0 -1
- /package/{backend/dist/bin → bin}/agentstudio.d.ts +0 -0
- /package/{backend/dist/bin → bin}/agentstudio.d.ts.map +0 -0
- /package/{backend/dist/frontend → public}/assets/ChatPage-BvQmXfcP.css +0 -0
- /package/{backend/dist/frontend → public}/assets/agents-DwCY2K8p.css +0 -0
- /package/{backend/dist/frontend → public}/assets/dateFormat-CXa8VnEC.js +0 -0
- /package/{backend/dist/frontend → public}/cc-studio.png +0 -0
- /package/{backend/dist/frontend → public}/vite.svg +0 -0
- /package/{backend/dist/routes → routes}/agents.d.ts +0 -0
- /package/{backend/dist/routes → routes}/auth.d.ts +0 -0
- /package/{backend/dist/routes → routes}/commands.d.ts +0 -0
- /package/{backend/dist/routes → routes}/files.d.ts +0 -0
- /package/{backend/dist/routes → routes}/media.d.ts +0 -0
- /package/{backend/dist/routes → routes}/projects.d.ts +0 -0
- /package/{backend/dist/routes → routes}/sessions.d.ts +0 -0
- /package/{backend/dist/routes → routes}/settings.d.ts +0 -0
- /package/{backend/dist/routes → routes}/slides.d.ts +0 -0
- /package/{backend/dist/routes → routes}/subagents.d.ts +0 -0
- /package/{backend/dist/routes → routes}/subagents.d.ts.map +0 -0
- /package/{backend/dist/services → services}/messageQueue.d.ts +0 -0
package/routes/agents.js
ADDED
|
@@ -0,0 +1,997 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const express_1 = __importDefault(require("express"));
|
|
7
|
+
const zod_1 = require("zod");
|
|
8
|
+
const agentStorage_1 = require("../services/agentStorage");
|
|
9
|
+
const sessionManager_1 = require("../services/sessionManager");
|
|
10
|
+
const claudeUtils_js_1 = require("../utils/claudeUtils.js");
|
|
11
|
+
const sessionUtils_js_1 = require("../utils/sessionUtils.js");
|
|
12
|
+
const index_js_1 = require("../services/askUserQuestion/index.js");
|
|
13
|
+
const a2aStreamEvents_js_1 = require("../services/a2a/a2aStreamEvents.js");
|
|
14
|
+
// 类型守卫函数
|
|
15
|
+
function isSDKSystemMessage(message) {
|
|
16
|
+
return message && message.type === 'system';
|
|
17
|
+
}
|
|
18
|
+
function isSDKResultMessage(message) {
|
|
19
|
+
return message && message.type === 'result';
|
|
20
|
+
}
|
|
21
|
+
// isSDKPartialAssistantMessage removed - not currently used
|
|
22
|
+
function isSDKCompactBoundaryMessage(message) {
|
|
23
|
+
return message && message.type === 'system' && message.subtype === 'compact_boundary';
|
|
24
|
+
}
|
|
25
|
+
const router = express_1.default.Router();
|
|
26
|
+
// Storage instances
|
|
27
|
+
const globalAgentStorage = new agentStorage_1.AgentStorage();
|
|
28
|
+
// Validation schemas
|
|
29
|
+
// 定义 SystemPrompt schema,支持字符串或预设对象格式
|
|
30
|
+
const PresetSystemPromptSchema = zod_1.z.object({
|
|
31
|
+
type: zod_1.z.literal('preset'),
|
|
32
|
+
preset: zod_1.z.literal('claude_code'),
|
|
33
|
+
append: zod_1.z.string().optional()
|
|
34
|
+
});
|
|
35
|
+
const SystemPromptSchema = zod_1.z.union([
|
|
36
|
+
zod_1.z.string().min(1),
|
|
37
|
+
PresetSystemPromptSchema
|
|
38
|
+
]);
|
|
39
|
+
const CreateAgentSchema = zod_1.z.object({
|
|
40
|
+
id: zod_1.z.string().min(1).regex(/^[a-z0-9-_]+$/, 'ID must contain only lowercase letters, numbers, hyphens, and underscores'),
|
|
41
|
+
name: zod_1.z.string().min(1),
|
|
42
|
+
description: zod_1.z.string(),
|
|
43
|
+
systemPrompt: SystemPromptSchema,
|
|
44
|
+
// maxTurns 可以是数字(1-100)、null(不限制)或 undefined(使用默认值)
|
|
45
|
+
maxTurns: zod_1.z.union([zod_1.z.number().min(1).max(100), zod_1.z.null()]).optional().default(25),
|
|
46
|
+
permissionMode: zod_1.z.enum(['default', 'acceptEdits', 'bypassPermissions', 'plan']).optional().default('acceptEdits'),
|
|
47
|
+
model: zod_1.z.string().min(1).optional().default('claude-3-5-sonnet-20241022'),
|
|
48
|
+
allowedTools: zod_1.z.array(zod_1.z.object({
|
|
49
|
+
name: zod_1.z.string(),
|
|
50
|
+
enabled: zod_1.z.boolean(),
|
|
51
|
+
permissions: zod_1.z.object({
|
|
52
|
+
requireConfirmation: zod_1.z.boolean().optional(),
|
|
53
|
+
allowedPaths: zod_1.z.array(zod_1.z.string()).optional(),
|
|
54
|
+
blockedPaths: zod_1.z.array(zod_1.z.string()).optional(),
|
|
55
|
+
}).optional()
|
|
56
|
+
})),
|
|
57
|
+
ui: zod_1.z.object({
|
|
58
|
+
icon: zod_1.z.string().optional().default('🤖'),
|
|
59
|
+
primaryColor: zod_1.z.string().optional().default('#3B82F6'),
|
|
60
|
+
headerTitle: zod_1.z.string(),
|
|
61
|
+
headerDescription: zod_1.z.string(),
|
|
62
|
+
welcomeMessage: zod_1.z.string().optional(),
|
|
63
|
+
customComponent: zod_1.z.string().optional()
|
|
64
|
+
}),
|
|
65
|
+
workingDirectory: zod_1.z.string().optional(),
|
|
66
|
+
dataDirectory: zod_1.z.string().optional(),
|
|
67
|
+
fileTypes: zod_1.z.array(zod_1.z.string()).optional(),
|
|
68
|
+
author: zod_1.z.string().min(1),
|
|
69
|
+
homepage: zod_1.z.string().url().optional(),
|
|
70
|
+
tags: zod_1.z.array(zod_1.z.string()).optional().default([]),
|
|
71
|
+
enabled: zod_1.z.boolean().optional().default(true)
|
|
72
|
+
});
|
|
73
|
+
const UpdateAgentSchema = CreateAgentSchema.partial().omit({ id: true });
|
|
74
|
+
// 获取活跃会话列表 (需要在通用获取agents路由之前)
|
|
75
|
+
router.get('/sessions', (req, res) => {
|
|
76
|
+
try {
|
|
77
|
+
const activeCount = sessionManager_1.sessionManager.getActiveSessionCount();
|
|
78
|
+
const sessionsInfo = sessionManager_1.sessionManager.getSessionsInfo();
|
|
79
|
+
res.json({
|
|
80
|
+
activeSessionCount: activeCount,
|
|
81
|
+
sessions: sessionsInfo,
|
|
82
|
+
message: `${activeCount} active Claude sessions`
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
console.error('Failed to get sessions:', error);
|
|
87
|
+
res.status(500).json({ error: 'Failed to retrieve session info' });
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
// 手动关闭指定会话
|
|
91
|
+
router.delete('/sessions/:sessionId', async (req, res) => {
|
|
92
|
+
try {
|
|
93
|
+
const { sessionId } = req.params;
|
|
94
|
+
const removed = await sessionManager_1.sessionManager.removeSession(sessionId);
|
|
95
|
+
if (removed) {
|
|
96
|
+
res.json({ success: true, message: `Session ${sessionId} closed` });
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
res.status(404).json({ error: 'Session not found' });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
console.error('Failed to close session:', error);
|
|
104
|
+
res.status(500).json({ error: 'Failed to close session' });
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
// 中断指定会话的当前请求
|
|
108
|
+
router.post('/sessions/:sessionId/interrupt', async (req, res) => {
|
|
109
|
+
try {
|
|
110
|
+
const { sessionId } = req.params;
|
|
111
|
+
console.log(`🛑 API: Interrupt request for session: ${sessionId}`);
|
|
112
|
+
const result = await sessionManager_1.sessionManager.interruptSession(sessionId);
|
|
113
|
+
if (result.success) {
|
|
114
|
+
res.json({
|
|
115
|
+
success: true,
|
|
116
|
+
message: `Session ${sessionId} interrupted successfully`
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
res.status(result.error === 'Session not found' ? 404 : 500).json({
|
|
121
|
+
success: false,
|
|
122
|
+
error: result.error || 'Failed to interrupt session'
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
console.error('Failed to interrupt session:', error);
|
|
128
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
129
|
+
res.status(500).json({
|
|
130
|
+
success: false,
|
|
131
|
+
error: 'Failed to interrupt session',
|
|
132
|
+
details: errorMessage
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
// Get all agents
|
|
137
|
+
router.get('/', (req, res) => {
|
|
138
|
+
try {
|
|
139
|
+
const { enabled } = req.query;
|
|
140
|
+
let agents = globalAgentStorage.getAllAgents();
|
|
141
|
+
// Filter by enabled status
|
|
142
|
+
if (enabled !== undefined) {
|
|
143
|
+
const isEnabled = enabled === 'true';
|
|
144
|
+
agents = agents.filter(agent => agent.enabled === isEnabled);
|
|
145
|
+
}
|
|
146
|
+
// Filter by component type
|
|
147
|
+
// componentType filtering removed - no longer needed
|
|
148
|
+
res.json({ agents });
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
console.error('Failed to get agents:', error);
|
|
152
|
+
res.status(500).json({ error: 'Failed to retrieve agents' });
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
// Get specific agent
|
|
156
|
+
router.get('/:agentId', (req, res) => {
|
|
157
|
+
try {
|
|
158
|
+
const { agentId } = req.params;
|
|
159
|
+
const agent = globalAgentStorage.getAgent(agentId);
|
|
160
|
+
if (!agent) {
|
|
161
|
+
return res.status(404).json({ error: 'Agent not found' });
|
|
162
|
+
}
|
|
163
|
+
res.json({ agent });
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
console.error('Failed to get agent:', error);
|
|
167
|
+
res.status(500).json({ error: 'Failed to retrieve agent' });
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
// Create new agent
|
|
171
|
+
router.post('/', (req, res) => {
|
|
172
|
+
try {
|
|
173
|
+
const validation = CreateAgentSchema.safeParse(req.body);
|
|
174
|
+
if (!validation.success) {
|
|
175
|
+
return res.status(400).json({ error: 'Invalid agent data', details: validation.error });
|
|
176
|
+
}
|
|
177
|
+
const agentData = validation.data;
|
|
178
|
+
// Check if agent ID already exists
|
|
179
|
+
const existingAgent = globalAgentStorage.getAgent(agentData.id);
|
|
180
|
+
if (existingAgent) {
|
|
181
|
+
return res.status(409).json({ error: 'Agent with this ID already exists' });
|
|
182
|
+
}
|
|
183
|
+
const agent = globalAgentStorage.createAgent({
|
|
184
|
+
...agentData,
|
|
185
|
+
version: '1.0.0',
|
|
186
|
+
model: 'claude-sonnet-4-20250514',
|
|
187
|
+
source: 'local'
|
|
188
|
+
});
|
|
189
|
+
res.json({ agent, message: 'Agent created successfully' });
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
console.error('Failed to create agent:', error);
|
|
193
|
+
res.status(500).json({ error: 'Failed to create agent' });
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
// Update agent
|
|
197
|
+
router.put('/:agentId', (req, res) => {
|
|
198
|
+
try {
|
|
199
|
+
const { agentId } = req.params;
|
|
200
|
+
const validation = UpdateAgentSchema.safeParse(req.body);
|
|
201
|
+
if (!validation.success) {
|
|
202
|
+
return res.status(400).json({ error: 'Invalid agent data', details: validation.error });
|
|
203
|
+
}
|
|
204
|
+
const existingAgent = globalAgentStorage.getAgent(agentId);
|
|
205
|
+
if (!existingAgent) {
|
|
206
|
+
return res.status(404).json({ error: 'Agent not found' });
|
|
207
|
+
}
|
|
208
|
+
// 过滤并转换 validation.data,将 maxTurns: null 转换为 undefined
|
|
209
|
+
const updateData = { ...validation.data };
|
|
210
|
+
if (updateData.maxTurns === null) {
|
|
211
|
+
updateData.maxTurns = undefined;
|
|
212
|
+
}
|
|
213
|
+
// 构建更新后的 agent
|
|
214
|
+
const updatedAgent = {
|
|
215
|
+
...existingAgent,
|
|
216
|
+
...updateData,
|
|
217
|
+
id: agentId, // Ensure ID doesn't change
|
|
218
|
+
updatedAt: new Date().toISOString()
|
|
219
|
+
};
|
|
220
|
+
globalAgentStorage.saveAgent(updatedAgent);
|
|
221
|
+
res.json({ agent: updatedAgent, message: 'Agent updated successfully' });
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
console.error('Failed to update agent:', error);
|
|
225
|
+
res.status(500).json({ error: 'Failed to update agent' });
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
// Delete agent
|
|
229
|
+
router.delete('/:agentId', (req, res) => {
|
|
230
|
+
try {
|
|
231
|
+
const { agentId } = req.params;
|
|
232
|
+
console.log(`🗑️ [ROUTE DEBUG] DELETE request for agent: ${agentId}`);
|
|
233
|
+
const deleted = globalAgentStorage.deleteAgent(agentId);
|
|
234
|
+
console.log(`🗑️ [ROUTE DEBUG] Delete result:`, deleted);
|
|
235
|
+
if (!deleted) {
|
|
236
|
+
console.log(`❌ [ROUTE DEBUG] Agent not found: ${agentId}`);
|
|
237
|
+
return res.status(404).json({ error: 'Agent not found' });
|
|
238
|
+
}
|
|
239
|
+
console.log(`✅ [ROUTE DEBUG] Agent deleted successfully: ${agentId}`);
|
|
240
|
+
res.json({ success: true, message: 'Agent deleted successfully' });
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
console.error('❌ [ROUTE DEBUG] Failed to delete agent:', error);
|
|
244
|
+
res.status(500).json({ error: 'Failed to delete agent' });
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
// Validation schemas for chat
|
|
248
|
+
const ImageSchema = zod_1.z.object({
|
|
249
|
+
id: zod_1.z.string(),
|
|
250
|
+
data: zod_1.z.string(), // base64 encoded image data
|
|
251
|
+
mediaType: zod_1.z.enum(['image/jpeg', 'image/png', 'image/gif', 'image/webp']),
|
|
252
|
+
filename: zod_1.z.string().optional()
|
|
253
|
+
});
|
|
254
|
+
const ChatRequestSchema = zod_1.z.object({
|
|
255
|
+
message: zod_1.z.string(),
|
|
256
|
+
images: zod_1.z.array(ImageSchema).optional(),
|
|
257
|
+
agentId: zod_1.z.string().min(1),
|
|
258
|
+
sessionId: zod_1.z.string().optional().nullable(),
|
|
259
|
+
projectPath: zod_1.z.string().optional(),
|
|
260
|
+
mcpTools: zod_1.z.array(zod_1.z.string()).optional(),
|
|
261
|
+
permissionMode: zod_1.z.enum(['default', 'acceptEdits', 'bypassPermissions', 'plan']).optional(),
|
|
262
|
+
model: zod_1.z.string().optional(),
|
|
263
|
+
claudeVersion: zod_1.z.string().optional(), // Claude版本ID
|
|
264
|
+
channel: zod_1.z.enum(['web', 'slack']).optional().default('web'), // Channel for streaming control
|
|
265
|
+
context: zod_1.z.object({
|
|
266
|
+
currentSlide: zod_1.z.number().optional().nullable(),
|
|
267
|
+
slideContent: zod_1.z.string().optional(),
|
|
268
|
+
allSlides: zod_1.z.array(zod_1.z.object({
|
|
269
|
+
index: zod_1.z.number(),
|
|
270
|
+
title: zod_1.z.string(),
|
|
271
|
+
path: zod_1.z.string(),
|
|
272
|
+
exists: zod_1.z.boolean().optional()
|
|
273
|
+
})).optional(),
|
|
274
|
+
// Generic context for other agent types
|
|
275
|
+
currentItem: zod_1.z.any().optional(),
|
|
276
|
+
allItems: zod_1.z.array(zod_1.z.any()).optional(),
|
|
277
|
+
customContext: zod_1.z.record(zod_1.z.any()).optional()
|
|
278
|
+
}).optional(),
|
|
279
|
+
envVars: zod_1.z.record(zod_1.z.string()).optional()
|
|
280
|
+
}).refine(data => {
|
|
281
|
+
// Either message text or images must be provided
|
|
282
|
+
return data.message.trim().length > 0 || (data.images && data.images.length > 0);
|
|
283
|
+
}, {
|
|
284
|
+
message: "Either message text or images must be provided"
|
|
285
|
+
});
|
|
286
|
+
// Helper functions for chat endpoint
|
|
287
|
+
/**
|
|
288
|
+
* 设置 SSE 连接管理
|
|
289
|
+
*/
|
|
290
|
+
function setupSSEConnectionManagement(req, res, agentId) {
|
|
291
|
+
// 连接管理变量
|
|
292
|
+
let isConnectionClosed = false;
|
|
293
|
+
let connectionTimeout = null;
|
|
294
|
+
let currentRequestId = null;
|
|
295
|
+
let claudeSession; // 会话实例,稍后赋值
|
|
296
|
+
// 安全关闭连接的函数
|
|
297
|
+
const safeCloseConnection = (reason) => {
|
|
298
|
+
if (isConnectionClosed)
|
|
299
|
+
return;
|
|
300
|
+
isConnectionClosed = true;
|
|
301
|
+
console.log(`🔚 Closing SSE connection for agent ${agentId}: ${reason}`);
|
|
302
|
+
// 清理超时定时器
|
|
303
|
+
if (connectionTimeout) {
|
|
304
|
+
clearTimeout(connectionTimeout);
|
|
305
|
+
connectionTimeout = null;
|
|
306
|
+
}
|
|
307
|
+
// 清理 Claude 请求回调
|
|
308
|
+
if (currentRequestId && claudeSession) {
|
|
309
|
+
claudeSession.cancelRequest(currentRequestId);
|
|
310
|
+
if (reason === 'request completed') {
|
|
311
|
+
console.log(`✅ Cleaned up Claude request ${currentRequestId}: ${reason}`);
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
console.log(`🚫 Cancelled Claude request ${currentRequestId} due to: ${reason}`);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
// 确保连接关闭
|
|
318
|
+
if (!res.headersSent) {
|
|
319
|
+
try {
|
|
320
|
+
res.write(`data: ${JSON.stringify({
|
|
321
|
+
type: 'connection_closed',
|
|
322
|
+
reason: reason,
|
|
323
|
+
timestamp: Date.now()
|
|
324
|
+
})}\n\n`);
|
|
325
|
+
}
|
|
326
|
+
catch (writeError) {
|
|
327
|
+
console.error('Failed to write connection close event:', writeError);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
try {
|
|
331
|
+
if (!res.destroyed) {
|
|
332
|
+
res.end();
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
catch (endError) {
|
|
336
|
+
console.error('Failed to end response:', endError);
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
// 监听客户端断开连接 - 只在响应阶段监听
|
|
340
|
+
res.on('close', () => {
|
|
341
|
+
if (!isConnectionClosed) {
|
|
342
|
+
safeCloseConnection('client disconnected');
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
// 监听请求完成
|
|
346
|
+
req.on('end', () => {
|
|
347
|
+
console.log('📤 Request data received completely');
|
|
348
|
+
});
|
|
349
|
+
// 监听连接错误
|
|
350
|
+
req.on('error', (error) => {
|
|
351
|
+
console.error('SSE request error:', error);
|
|
352
|
+
safeCloseConnection(`request error: ${error.message}`);
|
|
353
|
+
});
|
|
354
|
+
// 监听响应错误
|
|
355
|
+
res.on('error', (error) => {
|
|
356
|
+
console.error('SSE response error:', error);
|
|
357
|
+
safeCloseConnection(`response error: ${error.message}`);
|
|
358
|
+
});
|
|
359
|
+
// 设置连接超时保护(30分钟)
|
|
360
|
+
const CONNECTION_TIMEOUT_MS = 30 * 60 * 1000;
|
|
361
|
+
connectionTimeout = setTimeout(() => {
|
|
362
|
+
safeCloseConnection('connection timeout');
|
|
363
|
+
}, CONNECTION_TIMEOUT_MS);
|
|
364
|
+
return {
|
|
365
|
+
isConnectionClosed: () => isConnectionClosed,
|
|
366
|
+
safeCloseConnection,
|
|
367
|
+
setCurrentRequestId: (id) => { currentRequestId = id; },
|
|
368
|
+
setClaudeSession: (session) => { claudeSession = session; }
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
// POST /api/agents/chat - Agent-based AI chat using Claude Code SDK with session management
|
|
372
|
+
router.post('/chat', async (req, res) => {
|
|
373
|
+
// 重试逻辑:最多重试1次
|
|
374
|
+
let retryCount = 0;
|
|
375
|
+
const MAX_RETRIES = 1;
|
|
376
|
+
try {
|
|
377
|
+
console.log('Chat request received:', req.body);
|
|
378
|
+
// 输出当前Session Manager的状态
|
|
379
|
+
console.log('📊 SessionManager状态 - 收到/chat消息时:');
|
|
380
|
+
console.log(` 活跃会话总数: ${sessionManager_1.sessionManager.getActiveSessionCount()}`);
|
|
381
|
+
const sessionsInfo = sessionManager_1.sessionManager.getSessionsInfo();
|
|
382
|
+
console.log(' 会话详情:');
|
|
383
|
+
sessionsInfo.forEach(session => {
|
|
384
|
+
console.log(` - SessionId: ${session.sessionId}`);
|
|
385
|
+
console.log(` AgentId: ${session.agentId}`);
|
|
386
|
+
console.log(` 状态: ${session.status}`);
|
|
387
|
+
console.log(` 是否活跃: ${session.isActive}`);
|
|
388
|
+
console.log(` 空闲时间: ${Math.round(session.idleTimeMs / 1000)}秒`);
|
|
389
|
+
console.log(` 最后活动: ${new Date(session.lastActivity).toISOString()}`);
|
|
390
|
+
});
|
|
391
|
+
// 验证请求数据
|
|
392
|
+
const validation = ChatRequestSchema.safeParse(req.body);
|
|
393
|
+
if (!validation.success) {
|
|
394
|
+
console.log('Validation failed:', validation.error);
|
|
395
|
+
return res.status(400).json({ error: 'Invalid request body', details: validation.error });
|
|
396
|
+
}
|
|
397
|
+
const { message, images, agentId, sessionId: initialSessionId, projectPath, mcpTools, permissionMode, model, claudeVersion, channel, envVars } = validation.data;
|
|
398
|
+
let sessionId = initialSessionId;
|
|
399
|
+
console.log('[Backend] Received chat request:', {
|
|
400
|
+
agentId,
|
|
401
|
+
sessionId,
|
|
402
|
+
envVarsKeys: envVars ? Object.keys(envVars) : [],
|
|
403
|
+
envVars
|
|
404
|
+
});
|
|
405
|
+
// Configure partial message streaming based on channel
|
|
406
|
+
const includePartialMessages = channel === 'web';
|
|
407
|
+
console.log(`📡 Channel: ${channel}, includePartialMessages: ${includePartialMessages}`);
|
|
408
|
+
// 获取 agent 配置
|
|
409
|
+
const agent = globalAgentStorage.getAgent(agentId);
|
|
410
|
+
if (!agent) {
|
|
411
|
+
return res.status(404).json({ error: 'Agent not found' });
|
|
412
|
+
}
|
|
413
|
+
if (!agent.enabled) {
|
|
414
|
+
return res.status(403).json({ error: 'Agent is disabled' });
|
|
415
|
+
}
|
|
416
|
+
// 设置 SSE 响应头
|
|
417
|
+
res.setHeader('Content-Type', 'text/event-stream');
|
|
418
|
+
res.setHeader('Cache-Control', 'no-cache');
|
|
419
|
+
res.setHeader('Connection', 'keep-alive');
|
|
420
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
421
|
+
res.setHeader('Access-Control-Allow-Headers', 'Cache-Control');
|
|
422
|
+
// 设置连接管理
|
|
423
|
+
const connectionManager = setupSSEConnectionManagement(req, res, agentId);
|
|
424
|
+
// 🎤 初始化 AskUserQuestion 模块(只会初始化一次)
|
|
425
|
+
(0, index_js_1.initAskUserQuestionModule)();
|
|
426
|
+
// 🎤 生成 SSE channel ID(用于通知渠道管理)
|
|
427
|
+
const sseChannelId = (0, index_js_1.generateSSEChannelId)();
|
|
428
|
+
// 注意:SSE channel 需要 sessionId,但新会话还没有 sessionId
|
|
429
|
+
// 我们使用临时 ID,稍后在收到 Claude SDK 的 sessionId 时更新
|
|
430
|
+
const tempSessionId = sessionId || `temp_${Date.now()}`;
|
|
431
|
+
// 创建 SSE channel,传入 onClose 回调用于自动注销和清理
|
|
432
|
+
const sseChannel = new index_js_1.SSENotificationChannel(sseChannelId, tempSessionId, agentId, res, () => {
|
|
433
|
+
// 连接关闭时自动注销渠道,防止内存泄漏
|
|
434
|
+
index_js_1.notificationChannelManager.unregisterChannel(sseChannelId);
|
|
435
|
+
// 🎤 取消该 session 的所有等待中的用户输入请求
|
|
436
|
+
// 使用 sseChannel.sessionId 获取最新的 sessionId(可能已从 temp 更新为真实 ID)
|
|
437
|
+
const currentSessionId = sseChannel.sessionId;
|
|
438
|
+
const cancelledCount = index_js_1.userInputRegistry.cancelAllBySession(currentSessionId, 'SSE connection closed');
|
|
439
|
+
if (cancelledCount > 0) {
|
|
440
|
+
console.log(`🎤 [AskUserQuestion] Cancelled ${cancelledCount} pending inputs for session: ${currentSessionId}`);
|
|
441
|
+
}
|
|
442
|
+
});
|
|
443
|
+
index_js_1.notificationChannelManager.registerChannel(sseChannel);
|
|
444
|
+
console.log(`📡 [AskUserQuestion] Registered SSE channel: ${sseChannelId}`);
|
|
445
|
+
// =================================================================================
|
|
446
|
+
// A2A Stream Event Subscription
|
|
447
|
+
// Subscribe to A2A stream events to forward sessionId to frontend
|
|
448
|
+
// This allows frontend to connect to history stream early for real-time display
|
|
449
|
+
// =================================================================================
|
|
450
|
+
const a2aStreamStartHandler = (event) => {
|
|
451
|
+
// Only forward events for the same project
|
|
452
|
+
if (event.projectId === projectPath) {
|
|
453
|
+
try {
|
|
454
|
+
if (!res.destroyed && !connectionManager.isConnectionClosed()) {
|
|
455
|
+
res.write(`data: ${JSON.stringify({
|
|
456
|
+
type: 'a2a_stream_start',
|
|
457
|
+
sessionId: event.sessionId,
|
|
458
|
+
contextId: event.contextId, // A2A standard contextId
|
|
459
|
+
taskId: event.taskId, // A2A standard taskId
|
|
460
|
+
agentUrl: event.agentUrl,
|
|
461
|
+
message: event.message,
|
|
462
|
+
timestamp: event.timestamp,
|
|
463
|
+
})}\n\n`);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
catch (writeError) {
|
|
467
|
+
console.error('Failed to write A2A stream start event:', writeError);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
const a2aStreamDataHandler = (event) => {
|
|
472
|
+
// Only forward events for the same project
|
|
473
|
+
if (event.projectId === projectPath) {
|
|
474
|
+
try {
|
|
475
|
+
if (!res.destroyed && !connectionManager.isConnectionClosed()) {
|
|
476
|
+
res.write(`data: ${JSON.stringify({
|
|
477
|
+
type: 'a2a_stream_data',
|
|
478
|
+
sessionId: event.sessionId,
|
|
479
|
+
agentUrl: event.agentUrl, // Agent URL for frontend matching
|
|
480
|
+
event: event.event, // The actual A2A standard event
|
|
481
|
+
timestamp: event.timestamp,
|
|
482
|
+
})}\n\n`);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
catch (writeError) {
|
|
486
|
+
console.error('Failed to write A2A stream data event:', writeError);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
const a2aStreamEndHandler = (event) => {
|
|
491
|
+
// Only forward events for the same project
|
|
492
|
+
if (event.projectId === projectPath) {
|
|
493
|
+
try {
|
|
494
|
+
if (!res.destroyed && !connectionManager.isConnectionClosed()) {
|
|
495
|
+
res.write(`data: ${JSON.stringify({
|
|
496
|
+
type: 'a2a_stream_end',
|
|
497
|
+
sessionId: event.sessionId,
|
|
498
|
+
success: event.success,
|
|
499
|
+
error: event.error,
|
|
500
|
+
finalState: event.finalState, // A2A standard TaskState
|
|
501
|
+
timestamp: event.timestamp,
|
|
502
|
+
})}\n\n`);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
catch (writeError) {
|
|
506
|
+
console.error('Failed to write A2A stream end event:', writeError);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
// Subscribe to A2A stream events
|
|
511
|
+
a2aStreamEvents_js_1.a2aStreamEventEmitter.on('a2a_stream_start', a2aStreamStartHandler);
|
|
512
|
+
a2aStreamEvents_js_1.a2aStreamEventEmitter.on('a2a_stream_data', a2aStreamDataHandler);
|
|
513
|
+
a2aStreamEvents_js_1.a2aStreamEventEmitter.on('a2a_stream_end', a2aStreamEndHandler);
|
|
514
|
+
// Clean up subscription when connection closes
|
|
515
|
+
res.on('close', () => {
|
|
516
|
+
a2aStreamEvents_js_1.a2aStreamEventEmitter.off('a2a_stream_start', a2aStreamStartHandler);
|
|
517
|
+
a2aStreamEvents_js_1.a2aStreamEventEmitter.off('a2a_stream_data', a2aStreamDataHandler);
|
|
518
|
+
a2aStreamEvents_js_1.a2aStreamEventEmitter.off('a2a_stream_end', a2aStreamEndHandler);
|
|
519
|
+
});
|
|
520
|
+
// =================================================================================
|
|
521
|
+
// 重试循环:处理会话失败的情况
|
|
522
|
+
while (retryCount <= MAX_RETRIES) {
|
|
523
|
+
try {
|
|
524
|
+
console.log(`🔄 Attempt ${retryCount + 1}/${MAX_RETRIES + 1} for session: ${sessionId || 'new'}`);
|
|
525
|
+
// 构建查询选项(包含 AskUserQuestion MCP 工具)
|
|
526
|
+
// 使用 tempSessionId 作为 MCP 工具的 sessionId(新会话还没有真实 sessionId)
|
|
527
|
+
const { queryOptions, askUserSessionRef } = await (0, claudeUtils_js_1.buildQueryOptions)(agent, projectPath, mcpTools, permissionMode, model, claudeVersion, undefined, envVars, tempSessionId, agentId);
|
|
528
|
+
// 📊 输出传到 query 中的模型参数
|
|
529
|
+
console.log('📊 [Chat API] QueryOptions 模型参数:');
|
|
530
|
+
console.log(` 请求中的 model 参数: ${model || '(未指定)'}`);
|
|
531
|
+
console.log(` 请求中的 claudeVersion: ${claudeVersion || '(未指定)'}`);
|
|
532
|
+
console.log(` Agent 配置的 model: ${agent.model || '(未指定)'}`);
|
|
533
|
+
console.log(` 最终 queryOptions.model: ${queryOptions.model}`);
|
|
534
|
+
console.log(` queryOptions.pathToClaudeCodeExecutable: ${queryOptions.pathToClaudeCodeExecutable || '(未指定)'}`);
|
|
535
|
+
console.log(` queryOptions.cwd: ${queryOptions.cwd}`);
|
|
536
|
+
console.log(` queryOptions.permissionMode: ${queryOptions.permissionMode}`);
|
|
537
|
+
// ⚡ CRITICAL: Add includePartialMessages BEFORE creating session
|
|
538
|
+
// This must be set before handleSessionManagement because ClaudeSession
|
|
539
|
+
// uses these options to configure the Claude SDK query
|
|
540
|
+
queryOptions.includePartialMessages = includePartialMessages;
|
|
541
|
+
// 处理会话管理
|
|
542
|
+
const { claudeSession, actualSessionId: initialSessionId } = await (0, sessionUtils_js_1.handleSessionManagement)(agentId, sessionId || null, projectPath, queryOptions, claudeVersion, model);
|
|
543
|
+
let actualSessionId = initialSessionId;
|
|
544
|
+
// 📊 输出 Session 初始化后的信息
|
|
545
|
+
console.log('📊 [Chat API] Session 初始化后的信息:');
|
|
546
|
+
console.log(` Session ID: ${claudeSession.getClaudeSessionId?.() || '(无法获取)'}`);
|
|
547
|
+
console.log(` actualSessionId: ${actualSessionId || '(新会话)'}`);
|
|
548
|
+
console.log(` Agent ID: ${agentId}`);
|
|
549
|
+
// 尝试获取 session 的内部配置
|
|
550
|
+
try {
|
|
551
|
+
const sessionOptions = claudeSession.getOptions?.() || claudeSession.options || queryOptions;
|
|
552
|
+
console.log(` Session 使用的 model: ${sessionOptions?.model || '(未知)'}`);
|
|
553
|
+
console.log(` Session pathToClaudeCodeExecutable: ${sessionOptions?.pathToClaudeCodeExecutable || '(未知)'}`);
|
|
554
|
+
}
|
|
555
|
+
catch (e) {
|
|
556
|
+
console.log(` 无法获取 Session 内部配置`);
|
|
557
|
+
}
|
|
558
|
+
// 设置会话到连接管理器
|
|
559
|
+
connectionManager.setClaudeSession(claudeSession);
|
|
560
|
+
// 获取最终的模型名称(从queryOptions中获取,因为buildQueryOptions已经处理了优先级)
|
|
561
|
+
const finalModel = queryOptions.model || 'sonnet';
|
|
562
|
+
// 构建用户消息(传递claudeVersion以便查询isVision配置)
|
|
563
|
+
const userMessage = await (0, sessionUtils_js_1.buildUserMessageContent)(message, images, finalModel, projectPath, claudeVersion);
|
|
564
|
+
// 为这个特定请求创建一个独立的query调用,但复用session context
|
|
565
|
+
const currentSessionId = claudeSession.getClaudeSessionId();
|
|
566
|
+
// 使用会话的 sendMessage 方法发送消息
|
|
567
|
+
let compactMessageBuffer = []; // 缓存 compact 相关消息
|
|
568
|
+
const currentRequestId = await claudeSession.sendMessage(userMessage, (sdkMessage) => {
|
|
569
|
+
if (isSDKSystemMessage(sdkMessage) && sdkMessage.subtype === "init") {
|
|
570
|
+
// 📊 打印完整的 system.init 消息体,用于调试模型使用情况
|
|
571
|
+
console.log('📊 [Chat API] System Init Message 完整消息体:');
|
|
572
|
+
console.log(JSON.stringify(sdkMessage, null, 2));
|
|
573
|
+
// 检查 MCP 服务器连接状态
|
|
574
|
+
if (sdkMessage.mcp_servers && Array.isArray(sdkMessage.mcp_servers)) {
|
|
575
|
+
const failedServers = sdkMessage.mcp_servers.filter((s) => s.status !== "connected");
|
|
576
|
+
if (failedServers.length > 0) {
|
|
577
|
+
console.warn("🚨 [MCP] Failed to connect MCP servers:", failedServers.map((s) => ({
|
|
578
|
+
name: s.name,
|
|
579
|
+
status: s.status,
|
|
580
|
+
error: s.error
|
|
581
|
+
})));
|
|
582
|
+
// 发送 MCP 状态通知给前端
|
|
583
|
+
const mcpStatusEvent = {
|
|
584
|
+
type: 'mcp_status',
|
|
585
|
+
subtype: 'connection_failed',
|
|
586
|
+
failedServers: failedServers,
|
|
587
|
+
timestamp: Date.now(),
|
|
588
|
+
agentId: agentId,
|
|
589
|
+
sessionId: actualSessionId || currentSessionId
|
|
590
|
+
};
|
|
591
|
+
try {
|
|
592
|
+
if (!res.destroyed && !connectionManager.isConnectionClosed()) {
|
|
593
|
+
res.write(`data: ${JSON.stringify(mcpStatusEvent)}\n\n`);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
catch (writeError) {
|
|
597
|
+
console.error('Failed to write MCP status event:', writeError);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
else {
|
|
601
|
+
// 所有 MCP 服务器连接成功
|
|
602
|
+
const connectedServers = sdkMessage.mcp_servers.filter((s) => s.status === "connected");
|
|
603
|
+
if (connectedServers.length > 0) {
|
|
604
|
+
console.log("✅ [MCP] Successfully connected MCP servers:", connectedServers.map((s) => s.name));
|
|
605
|
+
// 发送成功连接通知给前端
|
|
606
|
+
const mcpStatusEvent = {
|
|
607
|
+
type: 'mcp_status',
|
|
608
|
+
subtype: 'connection_success',
|
|
609
|
+
connectedServers: connectedServers,
|
|
610
|
+
timestamp: Date.now(),
|
|
611
|
+
agentId: agentId,
|
|
612
|
+
sessionId: actualSessionId || currentSessionId
|
|
613
|
+
};
|
|
614
|
+
try {
|
|
615
|
+
if (!res.destroyed && !connectionManager.isConnectionClosed()) {
|
|
616
|
+
res.write(`data: ${JSON.stringify(mcpStatusEvent)}\n\n`);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
catch (writeError) {
|
|
620
|
+
console.error('Failed to write MCP success event:', writeError);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
// 🚨 MCP 工具日志观察 - 检查执行错误
|
|
627
|
+
if (isSDKResultMessage(sdkMessage) && sdkMessage.subtype === "error_during_execution") {
|
|
628
|
+
const errorMessage = sdkMessage; // 临时类型断言以访问错误详情
|
|
629
|
+
console.error("❌ [MCP] Execution failed:", {
|
|
630
|
+
error: errorMessage.error,
|
|
631
|
+
details: errorMessage.details,
|
|
632
|
+
tool: errorMessage.tool,
|
|
633
|
+
timestamp: Date.now()
|
|
634
|
+
});
|
|
635
|
+
// 发送执行错误通知给前端
|
|
636
|
+
const mcpErrorEvent = {
|
|
637
|
+
type: 'mcp_error',
|
|
638
|
+
subtype: 'execution_failed',
|
|
639
|
+
error: errorMessage.error,
|
|
640
|
+
details: errorMessage.details,
|
|
641
|
+
tool: errorMessage.tool,
|
|
642
|
+
timestamp: Date.now(),
|
|
643
|
+
agentId: agentId,
|
|
644
|
+
sessionId: actualSessionId || currentSessionId
|
|
645
|
+
};
|
|
646
|
+
try {
|
|
647
|
+
if (!res.destroyed && !connectionManager.isConnectionClosed()) {
|
|
648
|
+
res.write(`data: ${JSON.stringify(mcpErrorEvent)}\n\n`);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
catch (writeError) {
|
|
652
|
+
console.error('Failed to write MCP error event:', writeError);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
// 🔍 添加详细日志来观察消息结构
|
|
656
|
+
if (message === '/compact') {
|
|
657
|
+
const msgWithContent = sdkMessage; // 临时使用 any 访问 message 属性
|
|
658
|
+
console.log('📦 [COMPACT] Received SDK message:', {
|
|
659
|
+
type: sdkMessage.type,
|
|
660
|
+
subtype: sdkMessage.subtype,
|
|
661
|
+
hasMessage: !!msgWithContent.message,
|
|
662
|
+
messageType: typeof msgWithContent.message,
|
|
663
|
+
messageContentType: msgWithContent.message?.content ? typeof msgWithContent.message.content : 'no content',
|
|
664
|
+
messageContentLength: Array.isArray(msgWithContent.message?.content) ? msgWithContent.message.content.length : 'not array',
|
|
665
|
+
firstBlock: Array.isArray(msgWithContent.message?.content) && msgWithContent.message.content.length > 0
|
|
666
|
+
? { type: msgWithContent.message.content[0].type, hasText: !!msgWithContent.message.content[0].text, textPreview: msgWithContent.message.content[0].text?.substring(0, 100) }
|
|
667
|
+
: 'no blocks'
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
// 处理 /compact 命令的特殊消息序列
|
|
671
|
+
if (message === '/compact' && isSDKCompactBoundaryMessage(sdkMessage)) {
|
|
672
|
+
compactMessageBuffer.push(sdkMessage);
|
|
673
|
+
console.log('📦 [COMPACT] Detected compact_boundary, buffering messages...');
|
|
674
|
+
return; // 不发送给前端,等待完整的消息序列
|
|
675
|
+
}
|
|
676
|
+
// 如果在 compact 模式下,缓存消息直到找到完整序列
|
|
677
|
+
if (compactMessageBuffer.length > 0) {
|
|
678
|
+
compactMessageBuffer.push(sdkMessage);
|
|
679
|
+
// 检查是否有足够的消息来构成完整的 compact 序列
|
|
680
|
+
if (compactMessageBuffer.length >= 5) {
|
|
681
|
+
console.log('📦 [COMPACT] Processing complete compact sequence...');
|
|
682
|
+
// 提取摘要内容(第二个消息应该是 isCompactSummary)
|
|
683
|
+
const summaryMsg = compactMessageBuffer.find(msg => msg.isCompactSummary);
|
|
684
|
+
let compactContent = '会话上下文已压缩';
|
|
685
|
+
if (summaryMsg?.message?.content) {
|
|
686
|
+
if (Array.isArray(summaryMsg.message.content)) {
|
|
687
|
+
const textBlock = summaryMsg.message.content.find((block) => block.type === 'text');
|
|
688
|
+
compactContent = textBlock?.text || compactContent;
|
|
689
|
+
}
|
|
690
|
+
else if (typeof summaryMsg.message.content === 'string') {
|
|
691
|
+
compactContent = summaryMsg.message.content;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
// 创建 compact summary 消息发送给前端
|
|
695
|
+
const compactSummaryMessage = {
|
|
696
|
+
type: 'assistant',
|
|
697
|
+
role: 'assistant',
|
|
698
|
+
content: [
|
|
699
|
+
{
|
|
700
|
+
type: 'compactSummary',
|
|
701
|
+
text: compactContent
|
|
702
|
+
}
|
|
703
|
+
],
|
|
704
|
+
agentId: agentId,
|
|
705
|
+
sessionId: actualSessionId || currentSessionId,
|
|
706
|
+
timestamp: Date.now(),
|
|
707
|
+
isCompactSummary: true
|
|
708
|
+
};
|
|
709
|
+
console.log('📦 [COMPACT] Sending compact summary to frontend:', compactContent.substring(0, 100));
|
|
710
|
+
try {
|
|
711
|
+
if (!res.destroyed && !connectionManager.isConnectionClosed()) {
|
|
712
|
+
res.write(`data: ${JSON.stringify(compactSummaryMessage)}\n\n`);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
catch (writeError) {
|
|
716
|
+
console.error('Failed to write compact summary:', writeError);
|
|
717
|
+
}
|
|
718
|
+
// 清空缓存
|
|
719
|
+
compactMessageBuffer = [];
|
|
720
|
+
return; // 不继续处理原始消息
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
// 检查连接是否已关闭
|
|
724
|
+
if (connectionManager.isConnectionClosed()) {
|
|
725
|
+
console.log(`⚠️ Skipping response for closed connection, agent: ${agentId}`);
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
// 当收到 init 消息时,确认会话 ID
|
|
729
|
+
const responseSessionId = sdkMessage.session_id;
|
|
730
|
+
if (isSDKSystemMessage(sdkMessage) && sdkMessage.subtype === 'init' && responseSessionId) {
|
|
731
|
+
if (!actualSessionId || !currentSessionId) {
|
|
732
|
+
// 新会话:保存session ID
|
|
733
|
+
claudeSession.setClaudeSessionId(responseSessionId);
|
|
734
|
+
sessionManager_1.sessionManager.confirmSessionId(claudeSession, responseSessionId);
|
|
735
|
+
console.log(`✅ Confirmed session ${responseSessionId} for agent: ${agentId}`);
|
|
736
|
+
// 🎤 更新 NotificationChannel、UserInputRegistry 和 MCP Server 的 sessionId
|
|
737
|
+
if (tempSessionId !== responseSessionId) {
|
|
738
|
+
index_js_1.notificationChannelManager.updateChannelSession(sseChannelId, responseSessionId);
|
|
739
|
+
index_js_1.userInputRegistry.updateSessionId(tempSessionId, responseSessionId);
|
|
740
|
+
// 更新 AskUserQuestion MCP Server 使用的 session ID
|
|
741
|
+
if (askUserSessionRef) {
|
|
742
|
+
askUserSessionRef.current = responseSessionId;
|
|
743
|
+
}
|
|
744
|
+
console.log(`📡 [AskUserQuestion] Updated session: ${tempSessionId} -> ${responseSessionId}`);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
else if (currentSessionId && responseSessionId !== currentSessionId) {
|
|
748
|
+
// Resume场景:Claude SDK返回了新的session ID,需要通知前端
|
|
749
|
+
console.log(`🔄 Session resumed: ${currentSessionId} -> ${responseSessionId} for agent: ${agentId}`);
|
|
750
|
+
// 更新会话管理器中的session ID映射
|
|
751
|
+
sessionManager_1.sessionManager.replaceSessionId(claudeSession, currentSessionId, responseSessionId);
|
|
752
|
+
claudeSession.setClaudeSessionId(responseSessionId);
|
|
753
|
+
// 发送session resume通知给前端
|
|
754
|
+
const resumeNotification = {
|
|
755
|
+
type: 'session_resumed',
|
|
756
|
+
subtype: 'new_branch',
|
|
757
|
+
originalSessionId: currentSessionId,
|
|
758
|
+
newSessionId: responseSessionId,
|
|
759
|
+
sessionId: responseSessionId,
|
|
760
|
+
message: `会话已从历史记录恢复并创建新分支。原始会话ID: ${currentSessionId},新会话ID: ${responseSessionId}`,
|
|
761
|
+
timestamp: Date.now()
|
|
762
|
+
};
|
|
763
|
+
try {
|
|
764
|
+
if (!res.destroyed && !connectionManager.isConnectionClosed()) {
|
|
765
|
+
res.write(`data: ${JSON.stringify(resumeNotification)}\n\n`);
|
|
766
|
+
console.log(`🔄 Sent session resume notification: ${currentSessionId} -> ${responseSessionId}`);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
catch (writeError) {
|
|
770
|
+
console.error('Failed to write session resume notification:', writeError);
|
|
771
|
+
}
|
|
772
|
+
// 更新实际的session ID为新的ID
|
|
773
|
+
actualSessionId = responseSessionId;
|
|
774
|
+
}
|
|
775
|
+
else {
|
|
776
|
+
// 继续会话:使用现有session ID
|
|
777
|
+
console.log(`♻️ Continued session ${currentSessionId} for agent: ${agentId}`);
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
// 🎯 检测子Agent消息:通过 parent_tool_use_id 字段判断
|
|
781
|
+
const msgAny = sdkMessage;
|
|
782
|
+
const isSidechain = !!msgAny.parent_tool_use_id;
|
|
783
|
+
const parentToolUseId = msgAny.parent_tool_use_id;
|
|
784
|
+
if (isSidechain) {
|
|
785
|
+
const contentBlocks = msgAny.message?.content || [];
|
|
786
|
+
const blockTypes = contentBlocks.map((b) => b.type);
|
|
787
|
+
console.log('🎯 [SIDECHAIN] Sub-agent message:', {
|
|
788
|
+
type: sdkMessage.type,
|
|
789
|
+
parentToolUseId,
|
|
790
|
+
blockTypes,
|
|
791
|
+
// 如果有文本内容,打印前100字符
|
|
792
|
+
textPreview: contentBlocks.find((b) => b.type === 'text')?.text?.substring(0, 100),
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
const eventData = {
|
|
796
|
+
...sdkMessage,
|
|
797
|
+
agentId: agentId,
|
|
798
|
+
sessionId: actualSessionId || responseSessionId || currentSessionId,
|
|
799
|
+
timestamp: Date.now(),
|
|
800
|
+
// 🎯 添加子Agent标识
|
|
801
|
+
isSidechain,
|
|
802
|
+
parentToolUseId,
|
|
803
|
+
};
|
|
804
|
+
// 确保返回的 session_id 字段与 sessionId 一致
|
|
805
|
+
if (actualSessionId || currentSessionId) {
|
|
806
|
+
eventData.session_id = actualSessionId || currentSessionId;
|
|
807
|
+
}
|
|
808
|
+
// 🎤 AskUserQuestion 工具调用说明(事件驱动架构):
|
|
809
|
+
// 1. MCP 工具调用 userInputRegistry.waitForUserInput()
|
|
810
|
+
// 2. UserInputRegistry 发出 'awaiting_input' 事件
|
|
811
|
+
// 3. NotificationChannelManager 通过活跃渠道(SSE/Slack等)发送通知
|
|
812
|
+
// 4. 用户响应后,调用 /agents/user-response API
|
|
813
|
+
// 5. MCP 工具返回,Claude 继续执行
|
|
814
|
+
// 不需要在这里检测工具调用或关闭连接
|
|
815
|
+
try {
|
|
816
|
+
if (!res.destroyed && !connectionManager.isConnectionClosed()) {
|
|
817
|
+
res.write(`data: ${JSON.stringify(eventData)}\n\n`);
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
catch (writeError) {
|
|
821
|
+
console.error('Failed to write SSE data:', writeError);
|
|
822
|
+
const errorMessage = writeError instanceof Error ? writeError.message : 'unknown write error';
|
|
823
|
+
connectionManager.safeCloseConnection(`write error: ${errorMessage}`);
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
// 当收到 result 事件时,检查是否为错误类型
|
|
827
|
+
if (isSDKResultMessage(sdkMessage)) {
|
|
828
|
+
const resultMsg = sdkMessage; // 类型断言以访问 subtype 和 errors 字段
|
|
829
|
+
// 检查是否为错误类型的 result
|
|
830
|
+
if (resultMsg.subtype !== 'success') {
|
|
831
|
+
console.error(`❌ Received error result (subtype: ${resultMsg.subtype}):`, resultMsg.errors);
|
|
832
|
+
// 发送错误事件给前端
|
|
833
|
+
const errorEvent = {
|
|
834
|
+
type: 'error',
|
|
835
|
+
error: 'Claude API request failed',
|
|
836
|
+
message: resultMsg.errors?.join('\n') || 'Unknown error occurred',
|
|
837
|
+
subtype: resultMsg.subtype,
|
|
838
|
+
timestamp: Date.now(),
|
|
839
|
+
agentId: agentId,
|
|
840
|
+
sessionId: actualSessionId || currentSessionId
|
|
841
|
+
};
|
|
842
|
+
try {
|
|
843
|
+
if (!res.destroyed && !connectionManager.isConnectionClosed()) {
|
|
844
|
+
res.write(`data: ${JSON.stringify(errorEvent)}\n\n`);
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
catch (writeError) {
|
|
848
|
+
console.error('Failed to write error event:', writeError);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
console.log(`✅ Received result event (subtype: ${resultMsg.subtype}), closing SSE connection for sessionId: ${actualSessionId || currentSessionId}`);
|
|
852
|
+
connectionManager.safeCloseConnection('request completed');
|
|
853
|
+
}
|
|
854
|
+
});
|
|
855
|
+
// 设置当前请求ID到连接管理器
|
|
856
|
+
connectionManager.setCurrentRequestId(currentRequestId);
|
|
857
|
+
console.log(`📨 Started Claude request for agent: ${agentId}, sessionId: ${currentSessionId || 'new'}, requestId: ${currentRequestId}`);
|
|
858
|
+
// 如果成功发送消息,跳出重试循环
|
|
859
|
+
break;
|
|
860
|
+
}
|
|
861
|
+
catch (sessionError) {
|
|
862
|
+
console.error(`❌ Claude session error (attempt ${retryCount + 1}/${MAX_RETRIES + 1}):`, sessionError);
|
|
863
|
+
const errorMessage = sessionError instanceof Error ? sessionError.message : 'Unknown error';
|
|
864
|
+
const originalSessionId = sessionId; // 使用外部作用域的sessionId
|
|
865
|
+
// 检查是否应该重试
|
|
866
|
+
const shouldRetry = retryCount < MAX_RETRIES && originalSessionId !== null;
|
|
867
|
+
if (shouldRetry && originalSessionId) {
|
|
868
|
+
// 尝试重试:从SessionManager中移除失败的会话
|
|
869
|
+
console.log(`🔄 Attempting to recover from session failure for session: ${originalSessionId}`);
|
|
870
|
+
console.log(` Error details: ${errorMessage}`);
|
|
871
|
+
try {
|
|
872
|
+
// 从SessionManager中移除失败的会话
|
|
873
|
+
const removed = await sessionManager_1.sessionManager.removeSession(originalSessionId);
|
|
874
|
+
if (removed) {
|
|
875
|
+
console.log(`✅ Removed failed session ${originalSessionId} from SessionManager`);
|
|
876
|
+
}
|
|
877
|
+
else {
|
|
878
|
+
console.log(`⚠️ Session ${originalSessionId} was not found in SessionManager (may have been cleaned up already)`);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
catch (removeError) {
|
|
882
|
+
console.error(`⚠️ Failed to remove session ${originalSessionId}:`, removeError);
|
|
883
|
+
}
|
|
884
|
+
// 将sessionId设为null,下次循环将创建新会话
|
|
885
|
+
sessionId = null;
|
|
886
|
+
retryCount++;
|
|
887
|
+
console.log(`🔄 Retrying with new session (attempt ${retryCount + 1}/${MAX_RETRIES + 1})...`);
|
|
888
|
+
continue; // 继续下一次循环
|
|
889
|
+
}
|
|
890
|
+
// 不再重试,发送错误给前端
|
|
891
|
+
console.log(`❌ Maximum retries reached or no sessionId to retry. Sending error to frontend.`);
|
|
892
|
+
if (!connectionManager.isConnectionClosed()) {
|
|
893
|
+
try {
|
|
894
|
+
res.write(`data: ${JSON.stringify({
|
|
895
|
+
type: 'error',
|
|
896
|
+
error: 'Claude session failed',
|
|
897
|
+
message: errorMessage,
|
|
898
|
+
timestamp: Date.now(),
|
|
899
|
+
retriesExhausted: retryCount >= MAX_RETRIES
|
|
900
|
+
})}\n\n`);
|
|
901
|
+
}
|
|
902
|
+
catch (writeError) {
|
|
903
|
+
console.error('Failed to write error message:', writeError);
|
|
904
|
+
}
|
|
905
|
+
connectionManager.safeCloseConnection(`session error: ${errorMessage}`);
|
|
906
|
+
}
|
|
907
|
+
break; // 跳出重试循环
|
|
908
|
+
}
|
|
909
|
+
} // End of while loop
|
|
910
|
+
}
|
|
911
|
+
catch (error) {
|
|
912
|
+
console.error('Error in AI chat:', error);
|
|
913
|
+
// 使用安全关闭连接函数(如果在 try 块内部定义的话)
|
|
914
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
915
|
+
if (!res.headersSent) {
|
|
916
|
+
// 如果还没有设置为 SSE,返回 JSON 错误
|
|
917
|
+
res.status(500).json({ error: 'AI request failed', message: errorMessage });
|
|
918
|
+
}
|
|
919
|
+
else {
|
|
920
|
+
// 如果已经是 SSE 连接,发送错误事件并关闭
|
|
921
|
+
try {
|
|
922
|
+
if (!res.destroyed) {
|
|
923
|
+
res.write(`data: ${JSON.stringify({
|
|
924
|
+
type: 'error',
|
|
925
|
+
error: 'AI request failed',
|
|
926
|
+
message: errorMessage,
|
|
927
|
+
timestamp: Date.now()
|
|
928
|
+
})}\n\n`);
|
|
929
|
+
res.end();
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
catch (writeError) {
|
|
933
|
+
console.error('Failed to write final error message:', writeError);
|
|
934
|
+
try {
|
|
935
|
+
if (!res.destroyed) {
|
|
936
|
+
res.end();
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
catch (endError) {
|
|
940
|
+
console.error('Failed to end response in error handler:', endError);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
});
|
|
946
|
+
// =================================================================================
|
|
947
|
+
// 🎤 AskUserQuestion: Submit User Response API
|
|
948
|
+
// =================================================================================
|
|
949
|
+
// 当用户在前端交互组件中提交答案时,前端调用此 API
|
|
950
|
+
// 这会 resolve MCP 工具中正在等待的 Promise,使工具返回用户答案
|
|
951
|
+
const UserResponseSchema = zod_1.z.object({
|
|
952
|
+
toolUseId: zod_1.z.string().min(1, 'toolUseId is required'),
|
|
953
|
+
response: zod_1.z.string().min(1, 'response is required'),
|
|
954
|
+
// 可选的验证参数,用于防止伪造响应
|
|
955
|
+
sessionId: zod_1.z.string().optional(),
|
|
956
|
+
agentId: zod_1.z.string().optional()
|
|
957
|
+
});
|
|
958
|
+
router.post('/user-response', async (req, res) => {
|
|
959
|
+
try {
|
|
960
|
+
const validation = UserResponseSchema.safeParse(req.body);
|
|
961
|
+
if (!validation.success) {
|
|
962
|
+
return res.status(400).json({
|
|
963
|
+
error: 'Invalid request body',
|
|
964
|
+
details: validation.error.errors
|
|
965
|
+
});
|
|
966
|
+
}
|
|
967
|
+
const { toolUseId, response, sessionId, agentId } = validation.data;
|
|
968
|
+
console.log(`🎤 [AskUserQuestion] Received user response for tool: ${toolUseId}`);
|
|
969
|
+
// 使用带验证的提交方法,防止伪造响应
|
|
970
|
+
const result = index_js_1.userInputRegistry.validateAndSubmitUserResponse(toolUseId, response, sessionId, agentId);
|
|
971
|
+
if (result.success) {
|
|
972
|
+
console.log(`✅ [AskUserQuestion] User response submitted successfully for tool: ${toolUseId}`);
|
|
973
|
+
res.json({
|
|
974
|
+
success: true,
|
|
975
|
+
message: 'User response submitted successfully'
|
|
976
|
+
});
|
|
977
|
+
}
|
|
978
|
+
else {
|
|
979
|
+
console.warn(`⚠️ [AskUserQuestion] Failed to submit response for tool: ${toolUseId}, error: ${result.error}`);
|
|
980
|
+
// 根据错误类型返回不同的状态码
|
|
981
|
+
const statusCode = result.error === 'No pending input found for this tool use ID' ? 404 : 403;
|
|
982
|
+
res.status(statusCode).json({
|
|
983
|
+
success: false,
|
|
984
|
+
error: result.error
|
|
985
|
+
});
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
catch (error) {
|
|
989
|
+
console.error('❌ [AskUserQuestion] Error processing user response:', error);
|
|
990
|
+
res.status(500).json({
|
|
991
|
+
success: false,
|
|
992
|
+
error: 'Internal server error'
|
|
993
|
+
});
|
|
994
|
+
}
|
|
995
|
+
});
|
|
996
|
+
exports.default = router;
|
|
997
|
+
//# sourceMappingURL=agents.js.map
|