@ai-setting/roy-agent-core 1.0.0
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/dist/index.js +99145 -0
- package/package.json +114 -0
- package/src/config/config-component.test.ts +627 -0
- package/src/config/config-component.ts +906 -0
- package/src/config/config-parser.test.ts +319 -0
- package/src/config/config-parser.ts +203 -0
- package/src/config/decentralized-config.test.ts +740 -0
- package/src/config/env-key.ts +210 -0
- package/src/config/env-source.test.ts +252 -0
- package/src/config/env-source.ts +301 -0
- package/src/config/file-source.test.ts +357 -0
- package/src/config/file-source.ts +421 -0
- package/src/config/index.ts +24 -0
- package/src/config/protocol-resolver.test.ts +217 -0
- package/src/config/protocol-resolver.ts +228 -0
- package/src/env/agent/agent-component.abort.test.ts +511 -0
- package/src/env/agent/agent-component.record-session.test.ts +349 -0
- package/src/env/agent/agent-component.test.ts +1389 -0
- package/src/env/agent/agent-component.tool-error.test.ts +327 -0
- package/src/env/agent/agent-component.ts +1711 -0
- package/src/env/agent/agent-config-registration.test.ts +226 -0
- package/src/env/agent/agent-config-registration.ts +46 -0
- package/src/env/agent/agent-reminder-plugin.integration.test.ts +243 -0
- package/src/env/agent/index.ts +10 -0
- package/src/env/agent/summary-agent.parse-hint.test.ts +360 -0
- package/src/env/agent/summary-agent.ts +508 -0
- package/src/env/agent/types.ts +536 -0
- package/src/env/commands/commands-component.test.ts +364 -0
- package/src/env/commands/commands-component.ts +604 -0
- package/src/env/commands/commands-config-registration.test.ts +198 -0
- package/src/env/commands/commands-config-registration.ts +38 -0
- package/src/env/commands/index.ts +21 -0
- package/src/env/commands/parser.test.ts +203 -0
- package/src/env/commands/parser.ts +115 -0
- package/src/env/commands/types.ts +184 -0
- package/src/env/commands-prompt-integration.test.ts +243 -0
- package/src/env/component-env.test.ts +119 -0
- package/src/env/component.ts +335 -0
- package/src/env/constants.test.ts +72 -0
- package/src/env/constants.ts +123 -0
- package/src/env/debug/debug-component.test.ts +114 -0
- package/src/env/debug/debug-component.ts +547 -0
- package/src/env/debug/formatters/index.ts +9 -0
- package/src/env/debug/formatters/repl-formatter.test.ts +139 -0
- package/src/env/debug/formatters/repl-formatter.ts +358 -0
- package/src/env/debug/formatters/trace-formatter.test.ts +119 -0
- package/src/env/debug/formatters/trace-formatter.ts +191 -0
- package/src/env/debug/formatters/tree-formatter.test.ts +107 -0
- package/src/env/debug/formatters/tree-formatter.ts +325 -0
- package/src/env/debug/index.ts +38 -0
- package/src/env/debug/parser/regex-parser.test.ts +201 -0
- package/src/env/debug/parser/regex-parser.ts +196 -0
- package/src/env/debug/parser/span-builder.test.ts +241 -0
- package/src/env/debug/parser/span-builder.ts +386 -0
- package/src/env/debug/reader/log-reader.test.ts +170 -0
- package/src/env/debug/reader/log-reader.ts +186 -0
- package/src/env/debug/reader/span-db-reader.test.ts +118 -0
- package/src/env/debug/reader/span-db-reader.ts +201 -0
- package/src/env/debug/types.test.ts +187 -0
- package/src/env/debug/types.ts +171 -0
- package/src/env/environment-init.test.ts +183 -0
- package/src/env/environment-lifecycle.test.ts +516 -0
- package/src/env/environment-service.test.ts +332 -0
- package/src/env/environment.handle-query.test.ts +96 -0
- package/src/env/environment.test.ts +232 -0
- package/src/env/environment.ts +708 -0
- package/src/env/errors.test.ts +165 -0
- package/src/env/errors.ts +157 -0
- package/src/env/event-source/event-source-agent-handler.test.ts +193 -0
- package/src/env/event-source/event-source-agent-handler.ts +111 -0
- package/src/env/event-source/event-source-component.process-cleanup.test.ts +236 -0
- package/src/env/event-source/event-source-component.stop.test.ts +346 -0
- package/src/env/event-source/event-source-component.test.ts +1207 -0
- package/src/env/event-source/event-source-component.ts +1379 -0
- package/src/env/event-source/event-source-config-registration.test.ts +242 -0
- package/src/env/event-source/event-source-config-registration.ts +37 -0
- package/src/env/event-source/event-source-integration.test.ts +320 -0
- package/src/env/event-source/event-source-platform.test.ts +630 -0
- package/src/env/event-source/types.ts +298 -0
- package/src/env/hook/global-hook-manager.ts +162 -0
- package/src/env/hook/hook-manager.test.ts +374 -0
- package/src/env/hook/hook-manager.ts +309 -0
- package/src/env/hook/index.ts +38 -0
- package/src/env/hook/types.ts +138 -0
- package/src/env/index.ts +144 -0
- package/src/env/interface.ts +203 -0
- package/src/env/llm/hooks.test.ts +293 -0
- package/src/env/llm/hooks.ts +316 -0
- package/src/env/llm/index.ts +61 -0
- package/src/env/llm/invoke-threshold-check.test.ts +88 -0
- package/src/env/llm/invoke-timeout.test.ts +54 -0
- package/src/env/llm/invoke.test.ts +71 -0
- package/src/env/llm/invoke.ts +1039 -0
- package/src/env/llm/llm-config.test.ts +523 -0
- package/src/env/llm/llm.test.ts +233 -0
- package/src/env/llm/llm.ts +568 -0
- package/src/env/llm/provider.test.ts +182 -0
- package/src/env/llm/provider.ts +108 -0
- package/src/env/llm/transform.test.ts +251 -0
- package/src/env/llm/transform.ts +286 -0
- package/src/env/llm/types.test.ts +580 -0
- package/src/env/llm/types.ts +424 -0
- package/src/env/log-trace/decorator-otel.test.ts +182 -0
- package/src/env/log-trace/decorator.ts +230 -0
- package/src/env/log-trace/index.ts +79 -0
- package/src/env/log-trace/log-trace-component.test.ts +242 -0
- package/src/env/log-trace/log-trace-component.ts +497 -0
- package/src/env/log-trace/log-trace-config-registration.test.ts +348 -0
- package/src/env/log-trace/log-trace-config-registration.ts +45 -0
- package/src/env/log-trace/logger.test.ts +149 -0
- package/src/env/log-trace/logger.ts +522 -0
- package/src/env/log-trace/opentelemetry/cli-propagation.test.ts +147 -0
- package/src/env/log-trace/opentelemetry/cli-propagation.ts +194 -0
- package/src/env/log-trace/opentelemetry/integration.test.ts +668 -0
- package/src/env/log-trace/opentelemetry/mod.ts +25 -0
- package/src/env/log-trace/opentelemetry/propagation-env.test.ts +181 -0
- package/src/env/log-trace/opentelemetry/propagation-env.ts +136 -0
- package/src/env/log-trace/opentelemetry/propagation.test.ts +259 -0
- package/src/env/log-trace/opentelemetry/propagation.ts +215 -0
- package/src/env/log-trace/opentelemetry/tracer-provider-context.test.ts +166 -0
- package/src/env/log-trace/opentelemetry/tracer-provider.test.ts +379 -0
- package/src/env/log-trace/opentelemetry/tracer-provider.ts +612 -0
- package/src/env/log-trace/span-storage.test.ts +145 -0
- package/src/env/log-trace/span-storage.ts +230 -0
- package/src/env/log-trace/trace-context.test.ts +187 -0
- package/src/env/log-trace/trace-context.ts +162 -0
- package/src/env/log-trace/types.test.ts +63 -0
- package/src/env/log-trace/types.ts +172 -0
- package/src/env/mcp/README.md +244 -0
- package/src/env/mcp/__integration__/mcp-component.integration.test.ts +373 -0
- package/src/env/mcp/config.test.ts +74 -0
- package/src/env/mcp/config.ts +116 -0
- package/src/env/mcp/index.ts +41 -0
- package/src/env/mcp/loader.test.ts +161 -0
- package/src/env/mcp/loader.ts +209 -0
- package/src/env/mcp/mcp-component.test.ts +111 -0
- package/src/env/mcp/mcp-component.ts +358 -0
- package/src/env/mcp/mcp-config-registration.test.ts +304 -0
- package/src/env/mcp/mcp-config-registration.ts +50 -0
- package/src/env/mcp/scanner.test.ts +170 -0
- package/src/env/mcp/scanner.ts +246 -0
- package/src/env/mcp/tool/adapter.test.ts +520 -0
- package/src/env/mcp/tool/adapter.ts +521 -0
- package/src/env/mcp/tool/index.ts +5 -0
- package/src/env/mcp/types.test.ts +171 -0
- package/src/env/mcp/types.ts +79 -0
- package/src/env/memory/README.md +177 -0
- package/src/env/memory/built-in/index.ts +59 -0
- package/src/env/memory/built-in/recall-memory.ts +103 -0
- package/src/env/memory/built-in/record-memory.ts +148 -0
- package/src/env/memory/index.ts +20 -0
- package/src/env/memory/memory-component.test.ts +239 -0
- package/src/env/memory/memory-component.ts +503 -0
- package/src/env/memory/memory-config-registration.test.ts +67 -0
- package/src/env/memory/memory-config-registration.ts +48 -0
- package/src/env/memory/memory-config.ts +45 -0
- package/src/env/memory/memory-file.test.ts +268 -0
- package/src/env/memory/plugin/index.ts +48 -0
- package/src/env/memory/plugin/memory-agent.test.ts +249 -0
- package/src/env/memory/plugin/memory-agent.ts +365 -0
- package/src/env/memory/plugin/memory-manager.ts +198 -0
- package/src/env/memory/plugin/memory-plugin-agent.test.ts +145 -0
- package/src/env/memory/plugin/memory-plugin.ts +210 -0
- package/src/env/memory/plugin/plugin-simplified.test.ts +51 -0
- package/src/env/memory/plugin/recall-memory.test.ts +106 -0
- package/src/env/memory/plugin/recall-memory.ts +53 -0
- package/src/env/memory/plugin/types.ts +101 -0
- package/src/env/memory/tools/memory-agent-tools.ts +228 -0
- package/src/env/memory/types.ts +85 -0
- package/src/env/paths.ts +118 -0
- package/src/env/prompt/index.ts +18 -0
- package/src/env/prompt/memory-prompts.test.ts +91 -0
- package/src/env/prompt/prompt-component.test.ts +491 -0
- package/src/env/prompt/prompt-component.ts +619 -0
- package/src/env/prompt/prompt-config-registration.test.ts +213 -0
- package/src/env/prompt/prompt-config-registration.ts +39 -0
- package/src/env/prompt/prompts-index.ts +504 -0
- package/src/env/prompt/renderer.ts +67 -0
- package/src/env/prompt/types.ts +136 -0
- package/src/env/session/hooks.ts +18 -0
- package/src/env/session/index.ts +37 -0
- package/src/env/session/search-query-parser.test.ts +425 -0
- package/src/env/session/search-query-parser.ts +171 -0
- package/src/env/session/session-checkpoint.test.ts +523 -0
- package/src/env/session/session-component.extract-recent-messages.test.ts +209 -0
- package/src/env/session/session-component.test.ts +132 -0
- package/src/env/session/session-component.ts +1249 -0
- package/src/env/session/session-config-registration.test.ts +138 -0
- package/src/env/session/session-config-registration.ts +52 -0
- package/src/env/session/session-message-converter.test.ts +763 -0
- package/src/env/session/session-message-converter.ts +415 -0
- package/src/env/session/session-message-e2e.test.ts +448 -0
- package/src/env/session/session-search.test.ts +391 -0
- package/src/env/session/session-store.test.ts +362 -0
- package/src/env/session/session-store.ts +141 -0
- package/src/env/session/storage/index.ts +6 -0
- package/src/env/session/storage/memory.ts +502 -0
- package/src/env/session/storage/sqlite.ts +794 -0
- package/src/env/session/types.ts +742 -0
- package/src/env/skill/config.ts +39 -0
- package/src/env/skill/index.ts +6 -0
- package/src/env/skill/parser.test.ts +116 -0
- package/src/env/skill/parser.ts +77 -0
- package/src/env/skill/scanner.test.ts +211 -0
- package/src/env/skill/scanner.ts +119 -0
- package/src/env/skill/skill-component.test.ts +234 -0
- package/src/env/skill/skill-component.ts +352 -0
- package/src/env/skill/skill-config-registration.test.ts +60 -0
- package/src/env/skill/skill-config-registration.ts +43 -0
- package/src/env/skill/tool/index.ts +1 -0
- package/src/env/skill/tool/skill-tool.test.ts +100 -0
- package/src/env/skill/tool/skill-tool.ts +72 -0
- package/src/env/skill/types.ts +64 -0
- package/src/env/task/delegate/delegate-tool.test.ts +498 -0
- package/src/env/task/delegate/delegate-tool.ts +1014 -0
- package/src/env/task/delegate/index.ts +18 -0
- package/src/env/task/delegate/stop-tool.test.ts +140 -0
- package/src/env/task/delegate/stop-tool.ts +119 -0
- package/src/env/task/delegate/task-events.test.ts +178 -0
- package/src/env/task/delegate/task-events.ts +143 -0
- package/src/env/task/hooks/contexts.test.ts +92 -0
- package/src/env/task/hooks/contexts.ts +192 -0
- package/src/env/task/hooks/index.ts +23 -0
- package/src/env/task/hooks/task-hook-points.test.ts +32 -0
- package/src/env/task/hooks/task-hook-points.ts +54 -0
- package/src/env/task/index.ts +7 -0
- package/src/env/task/plugins/index.ts +13 -0
- package/src/env/task/plugins/task-plugin.test.ts +74 -0
- package/src/env/task/plugins/task-plugin.ts +89 -0
- package/src/env/task/plugins/task-tag-plugin.test.ts +377 -0
- package/src/env/task/plugins/task-tag-plugin.ts +319 -0
- package/src/env/task/plugins/task-workflow-extractor.integration.test.ts +226 -0
- package/src/env/task/plugins/workflow-extractor-agent.test.ts +107 -0
- package/src/env/task/plugins/workflow-extractor-agent.ts +225 -0
- package/src/env/task/storage/index.ts +6 -0
- package/src/env/task/storage/sqlite-task-store.test.ts +283 -0
- package/src/env/task/storage/sqlite-task-store.ts +903 -0
- package/src/env/task/storage/task-search.test.ts +291 -0
- package/src/env/task/tag-service.test.ts +198 -0
- package/src/env/task/tag-service.ts +264 -0
- package/src/env/task/task-component.test.ts +193 -0
- package/src/env/task/task-component.ts +658 -0
- package/src/env/task/task-config-registration.test.ts +57 -0
- package/src/env/task/task-config-registration.ts +37 -0
- package/src/env/task/task-types.test.ts +137 -0
- package/src/env/task/tools/complete-tool.ts +44 -0
- package/src/env/task/tools/create-tool.ts +49 -0
- package/src/env/task/tools/delete-tool.ts +43 -0
- package/src/env/task/tools/get-tool.ts +59 -0
- package/src/env/task/tools/index.ts +10 -0
- package/src/env/task/tools/list-tool.ts +40 -0
- package/src/env/task/tools/operation/create-tool.ts +48 -0
- package/src/env/task/tools/operation/delete-tool.ts +43 -0
- package/src/env/task/tools/operation/get-tool.ts +43 -0
- package/src/env/task/tools/operation/index.ts +9 -0
- package/src/env/task/tools/operation/list-tool.ts +40 -0
- package/src/env/task/tools/operation/operation-tools.test.ts +274 -0
- package/src/env/task/tools/operation/operation-types.ts +75 -0
- package/src/env/task/tools/operation/update-tool.ts +47 -0
- package/src/env/task/tools/task-tools.test.ts +203 -0
- package/src/env/task/tools/task-types.test.ts +75 -0
- package/src/env/task/tools/task-types.ts +68 -0
- package/src/env/task/tools/update-tool.ts +70 -0
- package/src/env/task/types.ts +160 -0
- package/src/env/tool/built-in/bash.ts +201 -0
- package/src/env/tool/built-in/echo.ts +29 -0
- package/src/env/tool/built-in/edit-file.test.ts +136 -0
- package/src/env/tool/built-in/edit-file.ts +92 -0
- package/src/env/tool/built-in/glob.test.ts +94 -0
- package/src/env/tool/built-in/glob.ts +65 -0
- package/src/env/tool/built-in/grep.test.ts +122 -0
- package/src/env/tool/built-in/grep.ts +108 -0
- package/src/env/tool/built-in/index.ts +44 -0
- package/src/env/tool/built-in/read-file.test.ts +84 -0
- package/src/env/tool/built-in/read-file.ts +75 -0
- package/src/env/tool/built-in/write-file.test.ts +119 -0
- package/src/env/tool/built-in/write-file.ts +68 -0
- package/src/env/tool/index.ts +24 -0
- package/src/env/tool/registry.test.ts +257 -0
- package/src/env/tool/registry.ts +167 -0
- package/src/env/tool/tool-component.test.ts +559 -0
- package/src/env/tool/tool-component.ts +563 -0
- package/src/env/tool/tool-config-registration.test.ts +249 -0
- package/src/env/tool/tool-config-registration.ts +46 -0
- package/src/env/tool/types.ts +267 -0
- package/src/env/tool/validator.test.ts +143 -0
- package/src/env/tool/validator.ts +44 -0
- package/src/env/types.ts +180 -0
- package/src/env/workflow/ask-user-tool-registration.test.ts +216 -0
- package/src/env/workflow/complex-workflow.integration.test.ts +1900 -0
- package/src/env/workflow/decorators/decorator-node.ts +229 -0
- package/src/env/workflow/decorators/decorator.test.ts +196 -0
- package/src/env/workflow/decorators/edge.ts +82 -0
- package/src/env/workflow/decorators/index.ts +31 -0
- package/src/env/workflow/decorators/node-as.ts +98 -0
- package/src/env/workflow/decorators/workflow.ts +54 -0
- package/src/env/workflow/engine/dag-manager.test.ts +570 -0
- package/src/env/workflow/engine/dag-manager.ts +594 -0
- package/src/env/workflow/engine/engine.ts +1422 -0
- package/src/env/workflow/engine/event-bus.test.ts +359 -0
- package/src/env/workflow/engine/event-bus.ts +156 -0
- package/src/env/workflow/engine/executor-agent-session.test.ts +84 -0
- package/src/env/workflow/engine/executor.test.ts +619 -0
- package/src/env/workflow/engine/executor.ts +593 -0
- package/src/env/workflow/engine/index.ts +24 -0
- package/src/env/workflow/engine/node-registry.test.ts +560 -0
- package/src/env/workflow/engine/node-registry.ts +289 -0
- package/src/env/workflow/engine/resume-removed.test.ts +22 -0
- package/src/env/workflow/engine/scheduler.test.ts +715 -0
- package/src/env/workflow/engine/scheduler.ts +318 -0
- package/src/env/workflow/engine/workflow-engine.test.ts +815 -0
- package/src/env/workflow/extractor/workflow-converter.ts +306 -0
- package/src/env/workflow/fixtures.ts +380 -0
- package/src/env/workflow/index.ts +38 -0
- package/src/env/workflow/integration/run-resume-unified.test.ts +186 -0
- package/src/env/workflow/integration/service-integration.test.ts +267 -0
- package/src/env/workflow/metadata/keys.ts +12 -0
- package/src/env/workflow/nodes/agent-component-adapter.test.ts +318 -0
- package/src/env/workflow/nodes/agent-component-adapter.ts +448 -0
- package/src/env/workflow/nodes/agent-node.test.ts +371 -0
- package/src/env/workflow/nodes/agent-node.ts +598 -0
- package/src/env/workflow/nodes/ask-user-node.ts +113 -0
- package/src/env/workflow/nodes/condition-node.ts +200 -0
- package/src/env/workflow/nodes/index.ts +9 -0
- package/src/env/workflow/nodes/merge-node.ts +141 -0
- package/src/env/workflow/nodes/skill-node.test.ts +253 -0
- package/src/env/workflow/nodes/skill-node.ts +393 -0
- package/src/env/workflow/nodes/tool-node.test.ts +251 -0
- package/src/env/workflow/nodes/tool-node.ts +493 -0
- package/src/env/workflow/nodes/workflow-llm-history.test.ts +455 -0
- package/src/env/workflow/nodes/workflow-node.test.ts +315 -0
- package/src/env/workflow/nodes/workflow-node.ts +311 -0
- package/src/env/workflow/service/index.ts +27 -0
- package/src/env/workflow/service/registry.test.ts +133 -0
- package/src/env/workflow/service/registry.ts +71 -0
- package/src/env/workflow/service/workflow-service.test.ts +310 -0
- package/src/env/workflow/service/workflow-service.ts +393 -0
- package/src/env/workflow/storage/index.ts +28 -0
- package/src/env/workflow/storage/mock-repositories.ts +385 -0
- package/src/env/workflow/storage/sqlite.test.ts +179 -0
- package/src/env/workflow/storage/sqlite.ts +163 -0
- package/src/env/workflow/storage/workflow-repo.test.ts +780 -0
- package/src/env/workflow/storage/workflow-repo.ts +342 -0
- package/src/env/workflow/tools/ask-user-tool.ts +82 -0
- package/src/env/workflow/tools/index.ts +26 -0
- package/src/env/workflow/tools/run-workflow.test.ts +352 -0
- package/src/env/workflow/tools/run-workflow.ts +214 -0
- package/src/env/workflow/types/context.ts +18 -0
- package/src/env/workflow/types/decorators-types.ts +198 -0
- package/src/env/workflow/types/event.test.ts +515 -0
- package/src/env/workflow/types/event.ts +193 -0
- package/src/env/workflow/types/index.ts +49 -0
- package/src/env/workflow/types/run.test.ts +437 -0
- package/src/env/workflow/types/run.ts +173 -0
- package/src/env/workflow/types/workflow-hil.ts +114 -0
- package/src/env/workflow/types/workflow-message.test.ts +138 -0
- package/src/env/workflow/types/workflow-message.ts +196 -0
- package/src/env/workflow/types/workflow-session.test.ts +95 -0
- package/src/env/workflow/types/workflow-session.ts +59 -0
- package/src/env/workflow/types/workflow.test.ts +495 -0
- package/src/env/workflow/types/workflow.ts +195 -0
- package/src/env/workflow/types_compat.ts +51 -0
- package/src/env/workflow/utils/create-workflow.ts +47 -0
- package/src/env/workflow/utils/execution-state.ts +245 -0
- package/src/env/workflow/utils/index.ts +18 -0
- package/src/env/workflow/utils/node-registry-helper.ts +58 -0
- package/src/env/workflow/utils/recovery-validator.test.ts +460 -0
- package/src/env/workflow/utils/recovery-validator.ts +377 -0
- package/src/env/workflow/utils/session-parser.test.ts +111 -0
- package/src/env/workflow/utils/session-parser.ts +94 -0
- package/src/env/workflow/utils/session-recovery.test.ts +334 -0
- package/src/env/workflow/utils/session-recovery.ts +188 -0
- package/src/env/workflow/utils/template-resolver.test.ts +258 -0
- package/src/env/workflow/utils/template-resolver.ts +436 -0
- package/src/env/workflow/utils/validation-rules.ts +149 -0
- package/src/env/workflow/workflow-component.ts +544 -0
- package/src/index.ts +422 -0
- package/src/utils/id.ts +21 -0
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Summary Agent
|
|
3
|
+
*
|
|
4
|
+
* 专门用于生成 session checkpoint 的 Agent
|
|
5
|
+
* 通过 PromptComponent 加载 compact prompt
|
|
6
|
+
* 调用 LLM 提取关键信息
|
|
7
|
+
*
|
|
8
|
+
* 支持 scenarioHint 场景化提示,用于指导不同场景下的压缩重点提取
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { createLogger } from "../log-trace/logger";
|
|
12
|
+
import { TracedAs } from "../log-trace/decorator";
|
|
13
|
+
import type {
|
|
14
|
+
SummaryAgentConfig,
|
|
15
|
+
SummaryAgentRunOptions,
|
|
16
|
+
SummaryAgentResult,
|
|
17
|
+
CompactHintGenerationOptions,
|
|
18
|
+
CompactHintGenerationResult,
|
|
19
|
+
} from "./types";
|
|
20
|
+
import type { PromptComponent } from "../prompt/prompt-component";
|
|
21
|
+
import type { LLMComponent } from "../llm/llm";
|
|
22
|
+
|
|
23
|
+
const logger = createLogger("agent:summary");
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Summary Agent
|
|
27
|
+
*
|
|
28
|
+
* 专门用于生成 session checkpoint 的 Agent
|
|
29
|
+
*/
|
|
30
|
+
export class SummaryAgent {
|
|
31
|
+
private promptComponent: PromptComponent;
|
|
32
|
+
private llmComponent: LLMComponent;
|
|
33
|
+
private config: SummaryAgentConfig;
|
|
34
|
+
|
|
35
|
+
constructor(
|
|
36
|
+
promptComponent: PromptComponent,
|
|
37
|
+
llmComponent: LLMComponent,
|
|
38
|
+
config?: Partial<SummaryAgentConfig>
|
|
39
|
+
) {
|
|
40
|
+
this.promptComponent = promptComponent;
|
|
41
|
+
this.llmComponent = llmComponent;
|
|
42
|
+
|
|
43
|
+
this.config = {
|
|
44
|
+
type: "summary",
|
|
45
|
+
promptName: "session/compact",
|
|
46
|
+
maxIterations: 1,
|
|
47
|
+
model: undefined, // 不指定模型,使用 LLMComponent 的默认模型
|
|
48
|
+
...config,
|
|
49
|
+
} as SummaryAgentConfig;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 生成场景化压缩提示 (Compact Hint)
|
|
54
|
+
*
|
|
55
|
+
* 根据会话历史分析,生成一个场景化的压缩提示,用于指导后续的 checkpoint 生成。
|
|
56
|
+
* 这个方法用于自动触发压缩的两阶段流程中的第一阶段。
|
|
57
|
+
*
|
|
58
|
+
* @param options 生成选项
|
|
59
|
+
* @returns 场景化的压缩提示
|
|
60
|
+
*/
|
|
61
|
+
@TracedAs("summary-agent.generateCompactHint")
|
|
62
|
+
async generateCompactHint(options: CompactHintGenerationOptions): Promise<CompactHintGenerationResult> {
|
|
63
|
+
const { messages, sessionContext } = options;
|
|
64
|
+
|
|
65
|
+
logger.info("[SummaryAgent] Generating compact hint for session");
|
|
66
|
+
|
|
67
|
+
// 1. 格式化消息
|
|
68
|
+
const formattedMessages = this.formatMessages(messages);
|
|
69
|
+
|
|
70
|
+
// 2. 构建提示生成 prompt
|
|
71
|
+
const systemPrompt = this.getHintGenerationSystemPrompt();
|
|
72
|
+
const userPrompt = this.buildHintGenerationUserPrompt(formattedMessages, sessionContext);
|
|
73
|
+
|
|
74
|
+
// 3. 调用 LLM
|
|
75
|
+
let response: any;
|
|
76
|
+
try {
|
|
77
|
+
response = await this.llmComponent.invoke({
|
|
78
|
+
messages: [
|
|
79
|
+
{ role: "system", content: systemPrompt },
|
|
80
|
+
{ role: "user", content: userPrompt },
|
|
81
|
+
],
|
|
82
|
+
skipThresholdCheck: true,
|
|
83
|
+
});
|
|
84
|
+
} catch (error) {
|
|
85
|
+
logger.error(`[SummaryAgent] Hint generation LLM invoke failed: ${error}`);
|
|
86
|
+
throw new Error(`LLM invoke failed: ${error}`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 4. 解析响应
|
|
90
|
+
const responseContent = response.output?.content || response.content || "";
|
|
91
|
+
const hint = this.parseHintResponse(responseContent);
|
|
92
|
+
|
|
93
|
+
logger.info("[SummaryAgent] Compact hint generated", { hint: hint.substring(0, 100) });
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
hint,
|
|
97
|
+
rawResponse: responseContent,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 运行 Summary Agent
|
|
103
|
+
*
|
|
104
|
+
* @param options 运行选项
|
|
105
|
+
* @returns Summary Agent 结果
|
|
106
|
+
*/
|
|
107
|
+
@TracedAs("summary-agent.run", { recordParams: true, recordResult: true })
|
|
108
|
+
async run(options: SummaryAgentRunOptions): Promise<SummaryAgentResult> {
|
|
109
|
+
const { messages, userContext, outputFormat, scenarioHint } = options;
|
|
110
|
+
|
|
111
|
+
logger.info("[SummaryAgent] Starting checkpoint generation", {
|
|
112
|
+
hasScenarioHint: !!scenarioHint
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// 1. 格式化消息
|
|
116
|
+
const formattedMessages = this.formatMessages(messages);
|
|
117
|
+
|
|
118
|
+
// 2. 渲染 prompt - 支持 scenarioHint
|
|
119
|
+
const systemPrompt = this.getSystemPrompt(scenarioHint);
|
|
120
|
+
const userPrompt = this.buildUserPrompt(formattedMessages, userContext);
|
|
121
|
+
|
|
122
|
+
// 3. 调用 LLM(直接调用,不走 ReAct 循环)
|
|
123
|
+
// 注意:跳过阈值检查,避免循环触发压缩
|
|
124
|
+
let response: any;
|
|
125
|
+
try {
|
|
126
|
+
response = await this.llmComponent.invoke({
|
|
127
|
+
messages: [
|
|
128
|
+
{ role: "system", content: systemPrompt },
|
|
129
|
+
{ role: "user", content: userPrompt },
|
|
130
|
+
],
|
|
131
|
+
model: this.config.model,
|
|
132
|
+
skipThresholdCheck: true, // 跳过阈值检查,避免循环触发压缩
|
|
133
|
+
});
|
|
134
|
+
} catch (error) {
|
|
135
|
+
logger.error(`[SummaryAgent] LLM invoke failed: ${error}`);
|
|
136
|
+
throw new Error(`LLM invoke failed: ${error}`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// 4. 解析响应
|
|
140
|
+
// LLMInvokeResult 返回 { output: LLMOutput, providerId, model, latencyMs }
|
|
141
|
+
const responseContent = response.output?.content || response.content || "";
|
|
142
|
+
|
|
143
|
+
if (outputFormat === "json") {
|
|
144
|
+
const result = this.parseJsonResponse(responseContent);
|
|
145
|
+
logger.info(`[SummaryAgent] Checkpoint generated: "${result.title}"`);
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
title: result.title || "Untitled Checkpoint",
|
|
149
|
+
processKeyPoints: result.processKeyPoints || [],
|
|
150
|
+
currentState: result.currentState || "",
|
|
151
|
+
nextSteps: result.nextSteps || [],
|
|
152
|
+
userIntents: result.userIntents || [],
|
|
153
|
+
rawResponse: responseContent,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
throw new Error(`Unsupported output format: ${outputFormat}`);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 获取系统提示
|
|
162
|
+
*
|
|
163
|
+
* @param scenarioHint 场景化提示(可选)
|
|
164
|
+
*/
|
|
165
|
+
private getSystemPrompt(scenarioHint?: string): string {
|
|
166
|
+
let prompt = `You are a professional session analyst assistant. Your task is to extract key information from a conversation and generate a structured checkpoint.
|
|
167
|
+
|
|
168
|
+
A checkpoint captures the progress and state of a session at a point in time. It should contain:
|
|
169
|
+
|
|
170
|
+
1. **Process Key Points**: The key steps, discoveries, or decisions made during this part of the conversation
|
|
171
|
+
2. **Current State**: The current progress and status at the end of the conversation
|
|
172
|
+
3. **Next Steps**: Follow-up items and what needs to be done next
|
|
173
|
+
4. **User Intents**: The underlying goals and intentions of the user (what they want to accomplish)
|
|
174
|
+
|
|
175
|
+
Be concise and accurate. Avoid redundancy. Each section should be appropriately sized - not too long.`;
|
|
176
|
+
|
|
177
|
+
// 如果提供了 scenarioHint,将其添加到系统提示中
|
|
178
|
+
if (scenarioHint && scenarioHint.trim()) {
|
|
179
|
+
prompt += `\n\n## Scenario Context\n\n${scenarioHint.trim()}`;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return prompt;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 获取提示生成的系统提示
|
|
187
|
+
*
|
|
188
|
+
* 这个方法生成的 hint 是一个 "压缩指导 Prompt",用于告诉 Compact 阶段:
|
|
189
|
+
* "在当前这个场景/目标/任务下,生成 checkpoint 时应该重点关注什么"
|
|
190
|
+
*
|
|
191
|
+
* 注意:这里生成的是"指导 Prompt",不是"压缩要点本身"
|
|
192
|
+
*
|
|
193
|
+
* 指导 Prompt 应包含:
|
|
194
|
+
* 1. 当前正在进行什么工作
|
|
195
|
+
* 2. 进展是什么
|
|
196
|
+
* 3. 压缩时需要专注什么信息的提取
|
|
197
|
+
* 4. 这些信息对后续任务推进很重要,不能丢失
|
|
198
|
+
*/
|
|
199
|
+
@TracedAs("summary-agent.getHintGenerationSystemPrompt")
|
|
200
|
+
private getHintGenerationSystemPrompt(): string {
|
|
201
|
+
return `You are a compact guidance prompt engineer. Your task is to analyze a conversation and generate a "compact guidance prompt" that will instruct the checkpoint generator on what information to extract and preserve during session compression.
|
|
202
|
+
|
|
203
|
+
**Purpose**: The guidance prompt will be used by the checkpoint generator to understand:
|
|
204
|
+
1. What work is currently in progress
|
|
205
|
+
2. What progress has been made
|
|
206
|
+
3. What information MUST be preserved during compression (this is critical for task continuation)
|
|
207
|
+
4. What information should be the focus of extraction
|
|
208
|
+
|
|
209
|
+
**Key Principle**: The preserved information should enable seamless task continuation. If critical details are lost during compression, the user will have to re-explain context when continuing this work later.
|
|
210
|
+
|
|
211
|
+
**Output Format**: A structured guidance prompt (3-4 sentences) covering:
|
|
212
|
+
1. Current work type and what the user is trying to accomplish
|
|
213
|
+
2. Current progress and key achievements
|
|
214
|
+
3. CRITICAL information that MUST be preserved (decisions, findings, remaining issues)
|
|
215
|
+
4. What the checkpoint generator should focus on when extracting information
|
|
216
|
+
|
|
217
|
+
**Example good outputs**:
|
|
218
|
+
1. "Bug investigation: User is debugging LSP diagnostics not updating after file edits. Progress: identified root cause (getDiagnostics needs to read latest file content and trigger didChange notification), implemented fix, CI verified. CRITICAL to preserve: the fix implementation details (file path, code changes), that 2 agent-node-integration tests still fail due to test assertion timing issue (expects 'running' but completes in ~78ms) - this is unrelated to LSP work. Focus extraction on: root cause analysis, fix implementation, known remaining test issues."
|
|
219
|
+
2. "Feature development: User is implementing JWT authentication for auth-service. Progress: designed auth flow, implemented token refresh logic, middleware partially done. CRITICAL to preserve: auth flow decisions (token storage, refresh strategy), completed middleware components, modules pending implementation. Focus extraction on: architectural decisions, completed components, pending work."
|
|
220
|
+
3. "Refactoring: User is migrating data layer to repository pattern. Progress: migrated UserRepository and OrderRepository, 3 more pending. CRITICAL to preserve: pattern decisions (interface definitions, dependency injection approach), completed repositories, modules pending migration. Focus extraction on: pattern implementation details, completed vs pending modules."`;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* 构建提示生成的用户提示
|
|
225
|
+
*/
|
|
226
|
+
@TracedAs("summary-agent.buildHintGenerationUserPrompt")
|
|
227
|
+
private buildHintGenerationUserPrompt(
|
|
228
|
+
messages: string,
|
|
229
|
+
sessionContext?: {
|
|
230
|
+
activeTaskId?: string;
|
|
231
|
+
activeTaskTitle?: string;
|
|
232
|
+
workingDirectory?: string;
|
|
233
|
+
recentActions?: string[];
|
|
234
|
+
}
|
|
235
|
+
): string {
|
|
236
|
+
let prompt = `Analyze the following conversation and generate a compact guidance prompt for checkpoint generation.
|
|
237
|
+
|
|
238
|
+
## Your Task
|
|
239
|
+
Generate a structured guidance prompt (3-4 sentences) that will help the checkpoint generator understand:
|
|
240
|
+
1. What work is currently in progress
|
|
241
|
+
2. What progress has been made
|
|
242
|
+
3. What information MUST be preserved during compression (critical for task continuation)
|
|
243
|
+
4. What the checkpoint generator should focus on when extracting information
|
|
244
|
+
|
|
245
|
+
`;
|
|
246
|
+
|
|
247
|
+
if (sessionContext) {
|
|
248
|
+
const ctxLines: string[] = ["## Session Context"];
|
|
249
|
+
|
|
250
|
+
if (sessionContext.activeTaskTitle) {
|
|
251
|
+
ctxLines.push(`- Active Task: ${sessionContext.activeTaskTitle}`);
|
|
252
|
+
}
|
|
253
|
+
if (sessionContext.workingDirectory) {
|
|
254
|
+
ctxLines.push(`- Working Directory: ${sessionContext.workingDirectory}`);
|
|
255
|
+
}
|
|
256
|
+
if (sessionContext.recentActions && sessionContext.recentActions.length > 0) {
|
|
257
|
+
ctxLines.push(`- Recent Actions: ${sessionContext.recentActions.slice(-3).join(" → ")}`);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (ctxLines.length > 1) {
|
|
261
|
+
prompt += ctxLines.join("\n") + "\n\n";
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
prompt += `## Conversation to Analyze\n\n${messages}\n\n`;
|
|
266
|
+
prompt += `---\n\n`;
|
|
267
|
+
prompt += `## Output Format\n\n`;
|
|
268
|
+
prompt += `Generate a structured guidance prompt covering:\n`;
|
|
269
|
+
prompt += `1. Current work type and what the user is trying to accomplish\n`;
|
|
270
|
+
prompt += `2. Current progress and key achievements\n`;
|
|
271
|
+
prompt += `3. CRITICAL information that MUST be preserved (decisions, findings, remaining issues)\n`;
|
|
272
|
+
prompt += `4. What to focus on during extraction\n\n`;
|
|
273
|
+
prompt += `Wrap the output in a JSON object with a \`guidance_prompt\` key:\n\n`;
|
|
274
|
+
prompt += `\`\`\`json\n`;
|
|
275
|
+
prompt += `{\n`;
|
|
276
|
+
prompt += ` "guidance_prompt": "Work type: ... Progress: ... CRITICAL to preserve: ... Focus extraction on: ..."\n`;
|
|
277
|
+
prompt += `}\n`;
|
|
278
|
+
prompt += `\`\`\`\n\n`;
|
|
279
|
+
prompt += `IMPORTANT: The preserved information should enable seamless task continuation. If critical details are lost, the user will need to re-explain context when continuing this work.`;
|
|
280
|
+
|
|
281
|
+
return prompt;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 构建用户提示
|
|
286
|
+
*/
|
|
287
|
+
private buildUserPrompt(messages: string, userContext?: string): string {
|
|
288
|
+
let prompt = "Please analyze the following conversation and generate a checkpoint:\n\n";
|
|
289
|
+
|
|
290
|
+
if (userContext && userContext !== "无额外上下文") {
|
|
291
|
+
prompt += `## Context\n${userContext}\n\n`;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
prompt += `## Conversation Content\n\n${messages}\n\n`;
|
|
295
|
+
prompt += `---\n\n`;
|
|
296
|
+
prompt += `## Output Format\n\nPlease output in the following JSON format:\n\n\`\`\`json\n{
|
|
297
|
+
"title": "Brief checkpoint title (max 30 characters)",
|
|
298
|
+
"processKeyPoints": [
|
|
299
|
+
"Point 1: Description of key discovery or decision",
|
|
300
|
+
"Point 2: Description of important step or conclusion",
|
|
301
|
+
"Point 3: ..."
|
|
302
|
+
],
|
|
303
|
+
"currentState": "Current state description explaining progress and status (50-100 characters)",
|
|
304
|
+
"nextSteps": [
|
|
305
|
+
"Next follow-up item 1",
|
|
306
|
+
"Next follow-up item 2"
|
|
307
|
+
],
|
|
308
|
+
"userIntents": [
|
|
309
|
+
"User intent 1: what the user wants to accomplish",
|
|
310
|
+
"User intent 2: another goal or request",
|
|
311
|
+
"User intent 3: ..."
|
|
312
|
+
]
|
|
313
|
+
}
|
|
314
|
+
\`\`\`\n\n`;
|
|
315
|
+
prompt += `## Intent Extraction Guidelines\n\n`;
|
|
316
|
+
prompt += `- Extract user intents from the ENTIRE conversation flow (both user and assistant messages)\n`;
|
|
317
|
+
prompt += `- Focus on what the user WANTS to accomplish, not just what was discussed\n`;
|
|
318
|
+
prompt += `- Include both explicit requests and inferred goals\n`;
|
|
319
|
+
prompt += `- Aim for 2-5 intents, prioritize the most important ones\n`;
|
|
320
|
+
prompt += `- Be concise and specific in intent descriptions\n`;
|
|
321
|
+
prompt += `- Examples: "Fix authentication bug", "Optimize database performance", "Add unit tests for auth module"\n\n`;
|
|
322
|
+
prompt += `Make sure the JSON output is valid and can be parsed.`;
|
|
323
|
+
|
|
324
|
+
return prompt;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* 格式化消息用于 prompt
|
|
329
|
+
*/
|
|
330
|
+
private formatMessages(messages: Array<{ role: string; content: string }>): string {
|
|
331
|
+
return messages
|
|
332
|
+
.map((m, i) => `[${i}] ${m.role}: ${m.content}`)
|
|
333
|
+
.join("\n\n");
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* 解析 JSON 响应
|
|
338
|
+
*/
|
|
339
|
+
private parseJsonResponse(content: string): any {
|
|
340
|
+
// 尝试提取 JSON 代码块
|
|
341
|
+
const jsonMatch = content.match(/```json\s*([\s\S]*?)\s*```/)
|
|
342
|
+
|| content.match(/```\s*([\s\S]*?)\s*```/)
|
|
343
|
+
|| [null, content];
|
|
344
|
+
|
|
345
|
+
const jsonStr = jsonMatch[1] || content;
|
|
346
|
+
|
|
347
|
+
try {
|
|
348
|
+
return JSON.parse(jsonStr.trim());
|
|
349
|
+
} catch {
|
|
350
|
+
// 如果解析失败,尝试修复常见问题
|
|
351
|
+
const fixed = jsonStr
|
|
352
|
+
.replace(/,\s*}/g, "}")
|
|
353
|
+
.replace(/,\s*]/g, "]")
|
|
354
|
+
.replace(/'/g, '"')
|
|
355
|
+
.trim();
|
|
356
|
+
|
|
357
|
+
try {
|
|
358
|
+
return JSON.parse(fixed);
|
|
359
|
+
} catch (e) {
|
|
360
|
+
logger.error(`[SummaryAgent] Failed to parse JSON: ${content}`);
|
|
361
|
+
throw new Error(`Failed to parse checkpoint JSON: ${e}`);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* 解析提示响应
|
|
368
|
+
*
|
|
369
|
+
* 直接返回清理后的自然语言提示
|
|
370
|
+
*/
|
|
371
|
+
@TracedAs("summary-agent.parseHintResponse")
|
|
372
|
+
private parseHintResponse(content: string): string {
|
|
373
|
+
return parseCompactHintResponse(content);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// ============================================================================
|
|
378
|
+
// 导出用于测试的纯函数
|
|
379
|
+
// ============================================================================
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* 解析 Compact Hint 响应
|
|
383
|
+
*
|
|
384
|
+
* 纯函数,可独立测试
|
|
385
|
+
*
|
|
386
|
+
* 支持的 LLM 输出格式:
|
|
387
|
+
* 1. 标准 JSON: {"guidance_prompt": "..."}
|
|
388
|
+
* 2. text + JSON 混合: "Some explanation\n{...}\nMore text"
|
|
389
|
+
* 3. ```json ... ``` 包裹
|
|
390
|
+
* 4. ``` ... ``` 包裹(无语言标识)
|
|
391
|
+
* 5. markdown 包裹的纯文本
|
|
392
|
+
* 6. 纯文本(降级处理)
|
|
393
|
+
*
|
|
394
|
+
* @param content LLM 返回的原始内容
|
|
395
|
+
* @returns 解析后的 guidance_prompt 字符串
|
|
396
|
+
*/
|
|
397
|
+
export function parseCompactHintResponse(content: string): string {
|
|
398
|
+
// 1. 尝试提取 JSON 并解析 guidance_prompt
|
|
399
|
+
const jsonResult = extractGuidanceFromJson(content);
|
|
400
|
+
// 使用显式 null 检查,因为空字符串也是有效返回值
|
|
401
|
+
if (jsonResult !== null) {
|
|
402
|
+
// 2. 验证提取结果
|
|
403
|
+
const trimmed = jsonResult.trim();
|
|
404
|
+
if (!trimmed) {
|
|
405
|
+
return "[Hint extraction failed]";
|
|
406
|
+
}
|
|
407
|
+
// 3. 限制长度
|
|
408
|
+
if (trimmed.length > 500) {
|
|
409
|
+
return trimmed.substring(0, 500) + "...";
|
|
410
|
+
}
|
|
411
|
+
return trimmed;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// 4. 降级处理:提取纯文本(移除代码块)
|
|
415
|
+
let text = content
|
|
416
|
+
.replace(/```json\s*([\s\S]*?)\s*```/g, "$1")
|
|
417
|
+
.replace(/```markdown\s*([\s\S]*?)\s*```/g, "$1")
|
|
418
|
+
.replace(/```\s*([\s\S]*?)\s*```/g, "$1")
|
|
419
|
+
.trim();
|
|
420
|
+
|
|
421
|
+
if (!text) {
|
|
422
|
+
return "[Hint extraction failed]";
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// 5. 限制长度
|
|
426
|
+
if (text.length > 500) {
|
|
427
|
+
return text.substring(0, 500) + "...";
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
return text;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* 从 LLM 输出中提取 JSON 并解析 guidance_prompt
|
|
435
|
+
*
|
|
436
|
+
* 尝试多种提取策略:
|
|
437
|
+
* 1. 匹配 ```json ... ``` 包裹的 JSON
|
|
438
|
+
* 2. 匹配 ``` ... ``` 包裹的 JSON
|
|
439
|
+
* 3. 匹配裸 JSON 对象
|
|
440
|
+
*
|
|
441
|
+
* @returns guidance_prompt 字符串,或特殊标记 "EMPTY" 表示字段存在但值为空,或 null 表示未找到
|
|
442
|
+
*/
|
|
443
|
+
function extractGuidanceFromJson(content: string): string | null {
|
|
444
|
+
// 策略1: 提取 ```json ... ```
|
|
445
|
+
const jsonBlockMatch = content.match(/```json\s*([\s\S]*?)\s*```/);
|
|
446
|
+
if (jsonBlockMatch) {
|
|
447
|
+
const parsed = tryParseJson(jsonBlockMatch[1]);
|
|
448
|
+
const guidancePrompt = parsed?.guidance_prompt;
|
|
449
|
+
if (typeof guidancePrompt === "string") {
|
|
450
|
+
return guidancePrompt; // 包括空字符串
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// 策略2: 提取 ``` ... ``` (无语言标识)
|
|
455
|
+
const plainBlockMatch = content.match(/```\s*([\s\S]*?)\s*```/);
|
|
456
|
+
if (plainBlockMatch) {
|
|
457
|
+
const parsed = tryParseJson(plainBlockMatch[1]);
|
|
458
|
+
const guidancePrompt = parsed?.guidance_prompt;
|
|
459
|
+
if (typeof guidancePrompt === "string") {
|
|
460
|
+
return guidancePrompt; // 包括空字符串
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// 策略3: 匹配整个 JSON 对象(处理 text + JSON 混合情况)
|
|
465
|
+
// 查找 {...} 模式
|
|
466
|
+
const jsonMatch = content.match(/\{[\s\S]*"guidance_prompt"[\s\S]*\}/);
|
|
467
|
+
if (jsonMatch) {
|
|
468
|
+
const parsed = tryParseJson(jsonMatch[0]);
|
|
469
|
+
const guidancePrompt = parsed?.guidance_prompt;
|
|
470
|
+
if (typeof guidancePrompt === "string") {
|
|
471
|
+
return guidancePrompt; // 包括空字符串
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// 策略4: 尝试解析整个 content 作为 JSON
|
|
476
|
+
const parsed = tryParseJson(content.trim());
|
|
477
|
+
const guidancePrompt = parsed?.guidance_prompt;
|
|
478
|
+
if (typeof guidancePrompt === "string") {
|
|
479
|
+
return guidancePrompt; // 包括空字符串
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
return null;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* 尝试解析 JSON,容错处理
|
|
487
|
+
*/
|
|
488
|
+
function tryParseJson(str: string): Record<string, unknown> | null {
|
|
489
|
+
if (!str || !str.trim()) {
|
|
490
|
+
return null;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
try {
|
|
494
|
+
return JSON.parse(str.trim());
|
|
495
|
+
} catch {
|
|
496
|
+
// JSON 解析失败,尝试修复常见问题
|
|
497
|
+
try {
|
|
498
|
+
// 移除尾随逗号
|
|
499
|
+
const fixed = str
|
|
500
|
+
.replace(/,\s*}/g, "}")
|
|
501
|
+
.replace(/,\s*]/g, "]")
|
|
502
|
+
.trim();
|
|
503
|
+
return JSON.parse(fixed);
|
|
504
|
+
} catch {
|
|
505
|
+
return null;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|