@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,1389 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview AgentComponent Tests
|
|
3
|
+
*
|
|
4
|
+
* 使用 TDD 方法测试 AgentComponent
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, it, expect, vi, beforeEach, afterEach, test } from "vitest";
|
|
8
|
+
import { AgentComponent } from "./agent-component";
|
|
9
|
+
import type { AgentContext } from "./types";
|
|
10
|
+
import type { Environment } from "../interface";
|
|
11
|
+
import { ConfigComponent } from "../../config/config-component";
|
|
12
|
+
import * as fsSync from "fs";
|
|
13
|
+
import * as path from "path";
|
|
14
|
+
import * as os from "os";
|
|
15
|
+
|
|
16
|
+
// ============================================================================
|
|
17
|
+
// Mock Environment
|
|
18
|
+
// ============================================================================
|
|
19
|
+
|
|
20
|
+
function createMockEnvironment(): Environment {
|
|
21
|
+
return {
|
|
22
|
+
name: "test-env",
|
|
23
|
+
version: "1.0.0",
|
|
24
|
+
getConfig: vi.fn(() => ({ name: "test-env", version: "1.0.0", enabled: true })),
|
|
25
|
+
registerComponent: vi.fn(),
|
|
26
|
+
unregisterComponent: vi.fn(),
|
|
27
|
+
getComponent: vi.fn(),
|
|
28
|
+
listComponents: vi.fn(() => []),
|
|
29
|
+
handle_query: vi.fn(),
|
|
30
|
+
handle_action: vi.fn(),
|
|
31
|
+
subscribe: vi.fn(() => () => {}),
|
|
32
|
+
subscribeTo: vi.fn(() => () => {}),
|
|
33
|
+
subscribeAll: vi.fn(() => () => {}),
|
|
34
|
+
pushEnvEvent: vi.fn(),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ============================================================================
|
|
39
|
+
// Tests
|
|
40
|
+
// ============================================================================
|
|
41
|
+
|
|
42
|
+
describe("AgentComponent", () => {
|
|
43
|
+
let agentComponent: AgentComponent;
|
|
44
|
+
let mockEnv: Environment;
|
|
45
|
+
let configComponent: ConfigComponent;
|
|
46
|
+
|
|
47
|
+
beforeEach(() => {
|
|
48
|
+
vi.clearAllMocks();
|
|
49
|
+
agentComponent = new AgentComponent();
|
|
50
|
+
configComponent = new ConfigComponent();
|
|
51
|
+
|
|
52
|
+
// Create mockEnv with configComponent
|
|
53
|
+
mockEnv = {
|
|
54
|
+
name: "test-env",
|
|
55
|
+
version: "1.0.0",
|
|
56
|
+
getConfig: vi.fn(() => ({ name: "test-env", version: "1.0.0", enabled: true })),
|
|
57
|
+
registerComponent: vi.fn(),
|
|
58
|
+
unregisterComponent: vi.fn(),
|
|
59
|
+
getComponent: vi.fn((name: string) => {
|
|
60
|
+
if (name === "config") return configComponent;
|
|
61
|
+
return undefined;
|
|
62
|
+
}),
|
|
63
|
+
hasComponent: vi.fn((name: string) => name === "config"),
|
|
64
|
+
listComponents: vi.fn(() => []),
|
|
65
|
+
handle_query: vi.fn(),
|
|
66
|
+
handle_action: vi.fn(),
|
|
67
|
+
subscribe: vi.fn(() => () => {}),
|
|
68
|
+
subscribeTo: vi.fn(() => () => {}),
|
|
69
|
+
subscribeAll: vi.fn(() => () => {}),
|
|
70
|
+
pushEnvEvent: vi.fn(),
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
afterEach(() => {
|
|
75
|
+
// Clean up environment variables
|
|
76
|
+
delete process.env.AGENT_AGENT_DEFAULT_AGENT_MAX_ITERATIONS;
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe("constructor and initialization", () => {
|
|
80
|
+
it("should create AgentComponent instance", () => {
|
|
81
|
+
expect(agentComponent).toBeDefined();
|
|
82
|
+
expect(agentComponent.name).toBe("agent");
|
|
83
|
+
expect(agentComponent.version).toBe("1.0.0");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("should initialize with default config", async () => {
|
|
87
|
+
await agentComponent.init({ env: mockEnv, options: { configComponent } });
|
|
88
|
+
expect(agentComponent.getStatus()).toBe("running");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("should init with env (env is optional)", async () => {
|
|
92
|
+
// env is optional, but configComponent is required
|
|
93
|
+
const component = new AgentComponent();
|
|
94
|
+
await expect(component.init({ env: mockEnv, options: { configComponent } })).resolves.toBeUndefined();
|
|
95
|
+
expect(component.getStatus()).toBe("running");
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe("Agent registration", () => {
|
|
100
|
+
beforeEach(async () => {
|
|
101
|
+
await agentComponent.init({ env: mockEnv, options: { configComponent } });
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("should register a new agent", () => {
|
|
105
|
+
const agent = agentComponent.registerAgent("test-agent", {
|
|
106
|
+
type: "primary",
|
|
107
|
+
systemPrompt: "You are a helpful assistant.",
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
expect(agent).toBeDefined();
|
|
111
|
+
expect(agent.name).toBe("test-agent");
|
|
112
|
+
expect(agent.config.type).toBe("primary");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("should get registered agent", () => {
|
|
116
|
+
agentComponent.registerAgent("test-agent", { type: "primary" });
|
|
117
|
+
|
|
118
|
+
const agent = agentComponent.getAgent("test-agent");
|
|
119
|
+
expect(agent).toBeDefined();
|
|
120
|
+
expect(agent!.name).toBe("test-agent");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("should return undefined for non-existent agent", () => {
|
|
124
|
+
const agent = agentComponent.getAgent("non-existent");
|
|
125
|
+
expect(agent).toBeUndefined();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("should list all agents", () => {
|
|
129
|
+
agentComponent.registerAgent("agent1", { type: "primary" });
|
|
130
|
+
agentComponent.registerAgent("agent2", { type: "sub" });
|
|
131
|
+
|
|
132
|
+
const agents = agentComponent.listAgents();
|
|
133
|
+
expect(agents).toHaveLength(2);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("should unregister agent", () => {
|
|
137
|
+
agentComponent.registerAgent("test-agent", { type: "primary" });
|
|
138
|
+
const deleted = agentComponent.unregisterAgent("test-agent");
|
|
139
|
+
|
|
140
|
+
expect(deleted).toBe(true);
|
|
141
|
+
expect(agentComponent.getAgent("test-agent")).toBeUndefined();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("should return false when unregistering non-existent agent", () => {
|
|
145
|
+
const deleted = agentComponent.unregisterAgent("non-existent");
|
|
146
|
+
expect(deleted).toBe(false);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("should use passed config over defaultAgent (PromptComponent priority)", async () => {
|
|
150
|
+
// 模拟 ConfigComponent 中有 systemPrompt 配置
|
|
151
|
+
const configWithPrompt = new ConfigComponent();
|
|
152
|
+
await configWithPrompt.set("agent.defaultAgent.systemPrompt", "From ConfigComponent");
|
|
153
|
+
await configWithPrompt.set("agent.defaultAgent.maxIterations", 50);
|
|
154
|
+
|
|
155
|
+
// 通过构造函数传入 config,模拟从配置文件加载的配置
|
|
156
|
+
agentComponent = new AgentComponent({
|
|
157
|
+
config: {
|
|
158
|
+
defaultAgent: {
|
|
159
|
+
systemPrompt: "From ConfigComponent (defaultAgent)",
|
|
160
|
+
maxIterations: 50,
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
await agentComponent.init({ env: mockEnv, options: { configComponent: configWithPrompt } });
|
|
165
|
+
|
|
166
|
+
// 模拟 environment.ts 从 PromptComponent 获取的 systemPrompt
|
|
167
|
+
const promptFromPromptComponent = "From PromptComponent";
|
|
168
|
+
|
|
169
|
+
// 注册 agent,传入 PromptComponent 的 systemPrompt
|
|
170
|
+
const agent = agentComponent.registerAgent("default", {
|
|
171
|
+
type: "primary",
|
|
172
|
+
systemPrompt: promptFromPromptComponent,
|
|
173
|
+
maxIterations: 100, // 传入的 maxIterations 应该覆盖 defaultAgent 的 50
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// 验证:传入的 config (PromptComponent) 优先级最高
|
|
177
|
+
expect(agent.config.systemPrompt).toBe(promptFromPromptComponent);
|
|
178
|
+
expect(agent.config.maxIterations).toBe(100); // 传入的覆盖 defaultAgent 的 50
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("should fill missing config from defaultAgent", async () => {
|
|
182
|
+
// 当传入的 config 缺失某些字段时,从 defaultAgent 填充
|
|
183
|
+
// 通过构造函数传入默认值
|
|
184
|
+
agentComponent = new AgentComponent({
|
|
185
|
+
config: {
|
|
186
|
+
defaultAgent: {
|
|
187
|
+
systemPrompt: "Default System Prompt",
|
|
188
|
+
maxIterations: 50,
|
|
189
|
+
model: "gpt-4",
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
await agentComponent.init({ env: mockEnv, options: { configComponent } });
|
|
194
|
+
|
|
195
|
+
// 只传入 type,其他字段缺失
|
|
196
|
+
const agent = agentComponent.registerAgent("test-agent", {
|
|
197
|
+
type: "primary",
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// 验证:从 defaultAgent 填充缺失的字段
|
|
201
|
+
expect(agent.config.systemPrompt).toBe("Default System Prompt");
|
|
202
|
+
expect(agent.config.maxIterations).toBe(50);
|
|
203
|
+
expect(agent.config.model).toBe("gpt-4");
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
describe("Plugin registration", () => {
|
|
208
|
+
beforeEach(async () => {
|
|
209
|
+
await agentComponent.init({ env: mockEnv, options: { configComponent } });
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("should register plugin to agent", () => {
|
|
213
|
+
agentComponent.registerAgent("test-agent", { type: "primary" });
|
|
214
|
+
|
|
215
|
+
const plugin = {
|
|
216
|
+
name: "test-plugin",
|
|
217
|
+
version: "1.0.0",
|
|
218
|
+
hooks: [{ point: "agent:before.llm" as const }],
|
|
219
|
+
execute: vi.fn(async () => ({ continue: true })),
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
agentComponent.registerPlugin("test-agent", plugin);
|
|
223
|
+
|
|
224
|
+
const agent = agentComponent.getAgent("test-agent");
|
|
225
|
+
expect(agent!.plugins.has("test-plugin")).toBe(true);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("should throw error when registering plugin to non-existent agent", () => {
|
|
229
|
+
const plugin = {
|
|
230
|
+
name: "test-plugin",
|
|
231
|
+
version: "1.0.0",
|
|
232
|
+
hooks: [{ point: "agent:before.llm" as const }],
|
|
233
|
+
execute: vi.fn(),
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
expect(() => agentComponent.registerPlugin("non-existent", plugin)).toThrow();
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("should unregister plugin", () => {
|
|
240
|
+
agentComponent.registerAgent("test-agent", { type: "primary" });
|
|
241
|
+
|
|
242
|
+
const plugin = {
|
|
243
|
+
name: "test-plugin",
|
|
244
|
+
version: "1.0.0",
|
|
245
|
+
hooks: [{ point: "agent:before.llm" as const }],
|
|
246
|
+
execute: vi.fn(),
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
agentComponent.registerPlugin("test-agent", plugin);
|
|
250
|
+
const deleted = agentComponent.unregisterPlugin("test-agent", "test-plugin");
|
|
251
|
+
|
|
252
|
+
expect(deleted).toBe(true);
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
describe("abort functionality", () => {
|
|
257
|
+
beforeEach(async () => {
|
|
258
|
+
await agentComponent.init({ env: mockEnv, options: { configComponent } });
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it("should abort running agent", async () => {
|
|
262
|
+
agentComponent.registerAgent("test-agent", { type: "primary" });
|
|
263
|
+
|
|
264
|
+
// Start aborting
|
|
265
|
+
agentComponent.abort("test-agent");
|
|
266
|
+
|
|
267
|
+
// Check abort was registered
|
|
268
|
+
// This would need integration with actual run
|
|
269
|
+
expect(true).toBe(true);
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
describe("Agent execution (run)", () => {
|
|
274
|
+
beforeEach(async () => {
|
|
275
|
+
await agentComponent.init({ env: mockEnv, options: { configComponent } });
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it("should throw error for non-existent agent", async () => {
|
|
279
|
+
await expect(agentComponent.run("non-existent", "test query")).rejects.toThrow();
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it("should execute agent and return result", async () => {
|
|
283
|
+
agentComponent.registerAgent("test-agent", {
|
|
284
|
+
type: "primary",
|
|
285
|
+
systemPrompt: "You are a helpful assistant.",
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
const result = await agentComponent.run("test-agent", "Say hello");
|
|
289
|
+
|
|
290
|
+
expect(result).toBeDefined();
|
|
291
|
+
expect(result.iterations).toBe(1);
|
|
292
|
+
expect(result.finalText).toBeDefined();
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("should respect maxIterations config", async () => {
|
|
296
|
+
agentComponent.registerAgent("test-agent", {
|
|
297
|
+
type: "primary",
|
|
298
|
+
maxIterations: 5,
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
// Mock LLM to always return tool calls
|
|
302
|
+
const mockLLMComponent = {
|
|
303
|
+
invoke: vi.fn(() => Promise.resolve({
|
|
304
|
+
output: {
|
|
305
|
+
content: "",
|
|
306
|
+
toolCalls: [{
|
|
307
|
+
id: "call_1",
|
|
308
|
+
function: { name: "test_tool", arguments: "{}" }
|
|
309
|
+
}]
|
|
310
|
+
}
|
|
311
|
+
}))
|
|
312
|
+
};
|
|
313
|
+
(mockEnv.getComponent as any).mockReturnValue(mockLLMComponent);
|
|
314
|
+
|
|
315
|
+
const result = await agentComponent.run("test-agent", "Test max iterations");
|
|
316
|
+
|
|
317
|
+
expect(result.iterations).toBeLessThanOrEqual(5);
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
describe("Plugin hooks", () => {
|
|
322
|
+
beforeEach(async () => {
|
|
323
|
+
await agentComponent.init({ env: mockEnv, options: { configComponent } });
|
|
324
|
+
agentComponent.registerAgent("test-agent", { type: "primary" });
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it("should execute before.llm hook", async () => {
|
|
328
|
+
const hookCalled = vi.fn();
|
|
329
|
+
|
|
330
|
+
const plugin = {
|
|
331
|
+
name: "before-llm-hook",
|
|
332
|
+
version: "1.0.0",
|
|
333
|
+
hooks: [{ point: "agent:before.llm" as const }],
|
|
334
|
+
execute: hookCalled,
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
agentComponent.registerPlugin("test-agent", plugin);
|
|
338
|
+
await agentComponent.run("test-agent", "Test hook");
|
|
339
|
+
|
|
340
|
+
expect(hookCalled).toHaveBeenCalled();
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
it("should allow hook to stop execution", async () => {
|
|
344
|
+
const plugin = {
|
|
345
|
+
name: "stopper",
|
|
346
|
+
version: "1.0.0",
|
|
347
|
+
hooks: [{ point: "agent:before.llm" as const }],
|
|
348
|
+
execute: vi.fn(() => Promise.resolve({ continue: false })),
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
agentComponent.registerPlugin("test-agent", plugin);
|
|
352
|
+
const result = await agentComponent.run("test-agent", "Test stop");
|
|
353
|
+
|
|
354
|
+
expect(result.stopped).toBe(true);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it("should allow hook to modify messages", async () => {
|
|
358
|
+
let capturedCtx: any;
|
|
359
|
+
|
|
360
|
+
const plugin = {
|
|
361
|
+
name: "message-modifier",
|
|
362
|
+
version: "1.0.0",
|
|
363
|
+
hooks: [{ point: "agent:before.llm" as const }],
|
|
364
|
+
execute: vi.fn((ctx: any) => {
|
|
365
|
+
capturedCtx = ctx;
|
|
366
|
+
return { continue: true };
|
|
367
|
+
}),
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
agentComponent.registerPlugin("test-agent", plugin);
|
|
371
|
+
await agentComponent.run("test-agent", "Test");
|
|
372
|
+
|
|
373
|
+
expect(capturedCtx.messages).toBeDefined();
|
|
374
|
+
expect(capturedCtx.messages.length).toBeGreaterThan(0);
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
describe("Multiple queries in same session", () => {
|
|
379
|
+
beforeEach(async () => {
|
|
380
|
+
await agentComponent.init({ env: mockEnv, options: { configComponent } });
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
it("should add user message for every query (not skip when session has history)", async () => {
|
|
384
|
+
// Create a mock session component that returns existing messages
|
|
385
|
+
const sessionMessages: any[] = [
|
|
386
|
+
{ role: "user", content: "first query" },
|
|
387
|
+
{ role: "assistant", content: "first response" },
|
|
388
|
+
];
|
|
389
|
+
|
|
390
|
+
const mockSessionComponent = {
|
|
391
|
+
addMessage: vi.fn(async () => {}),
|
|
392
|
+
getContext: vi.fn(async () => ({
|
|
393
|
+
messages: sessionMessages,
|
|
394
|
+
checkpoint: undefined,
|
|
395
|
+
})),
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
// Update mock env to return session component
|
|
399
|
+
(mockEnv.getComponent as any).mockImplementation((name: string) => {
|
|
400
|
+
if (name === "session") return mockSessionComponent;
|
|
401
|
+
if (name === "config") return configComponent;
|
|
402
|
+
return undefined;
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
agentComponent.registerAgent("test-agent", {
|
|
406
|
+
type: "primary",
|
|
407
|
+
systemPrompt: "You are a helpful assistant.",
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
// First run - session is empty
|
|
411
|
+
const result1 = await agentComponent.run("test-agent", "first query", { sessionId: "session_1" });
|
|
412
|
+
expect(result1).toBeDefined();
|
|
413
|
+
|
|
414
|
+
// Verify user message was added to session
|
|
415
|
+
expect(mockSessionComponent.addMessage).toHaveBeenCalled();
|
|
416
|
+
const firstCallArgs = (mockSessionComponent.addMessage as any).mock.calls[0];
|
|
417
|
+
expect(firstCallArgs[1].role).toBe("user");
|
|
418
|
+
expect(firstCallArgs[1].content).toBe("first query");
|
|
419
|
+
|
|
420
|
+
// Second run - session has history, but user message SHOULD still be added
|
|
421
|
+
const result2 = await agentComponent.run("test-agent", "second query", { sessionId: "session_1" });
|
|
422
|
+
expect(result2).toBeDefined();
|
|
423
|
+
|
|
424
|
+
// Key assertion: user message should be added for the second query
|
|
425
|
+
const allCalls = (mockSessionComponent.addMessage as any).mock.calls;
|
|
426
|
+
const userMessages = allCalls
|
|
427
|
+
.filter((call: any[]) => call[1].role === "user")
|
|
428
|
+
.map((call: any[]) => call[1].content);
|
|
429
|
+
|
|
430
|
+
// This is the critical test - second query's user message MUST be added
|
|
431
|
+
expect(userMessages).toContain("second query");
|
|
432
|
+
});
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
describe("Tool handling", () => {
|
|
436
|
+
// Note: Tool execution is tested via executeTool method directly
|
|
437
|
+
// since mock LLM integration requires more complex setup
|
|
438
|
+
|
|
439
|
+
it("should have default tools available", () => {
|
|
440
|
+
const mockTool = {
|
|
441
|
+
name: "test_tool",
|
|
442
|
+
description: "A test tool",
|
|
443
|
+
execute: vi.fn(() => Promise.resolve({ success: true, output: "tool result" })),
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
agentComponent.setDefaultTools([mockTool]);
|
|
447
|
+
agentComponent.registerAgent("test-agent", { type: "primary" });
|
|
448
|
+
|
|
449
|
+
const agent = agentComponent.getAgent("test-agent")!;
|
|
450
|
+
const tools = agentComponent.getAvailableTools(agent);
|
|
451
|
+
|
|
452
|
+
expect(tools.find(t => t.name === "test_tool")).toBeDefined();
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
it("should filter tools by name", () => {
|
|
456
|
+
const mockTool1 = { name: "tool1", execute: vi.fn() };
|
|
457
|
+
const mockTool2 = { name: "tool2", execute: vi.fn() };
|
|
458
|
+
|
|
459
|
+
agentComponent.setDefaultTools([mockTool1, mockTool2]);
|
|
460
|
+
agentComponent.registerAgent("test-agent", { type: "primary" });
|
|
461
|
+
|
|
462
|
+
const agent = agentComponent.getAgent("test-agent")!;
|
|
463
|
+
const tools = agentComponent.getAvailableTools(agent);
|
|
464
|
+
|
|
465
|
+
expect(tools).toHaveLength(2);
|
|
466
|
+
expect(tools.map(t => t.name)).toContain("tool1");
|
|
467
|
+
expect(tools.map(t => t.name)).toContain("tool2");
|
|
468
|
+
});
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
describe("Default tools", () => {
|
|
472
|
+
beforeEach(async () => {
|
|
473
|
+
await agentComponent.init({ env: mockEnv, options: { configComponent } });
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
it("should set and get default tools", () => {
|
|
477
|
+
const tools = [
|
|
478
|
+
{ name: "tool1", execute: vi.fn() },
|
|
479
|
+
{ name: "tool2", execute: vi.fn() },
|
|
480
|
+
];
|
|
481
|
+
|
|
482
|
+
agentComponent.setDefaultTools(tools);
|
|
483
|
+
agentComponent.registerAgent("test-agent", { type: "primary" });
|
|
484
|
+
|
|
485
|
+
const agent = agentComponent.getAgent("test-agent")!;
|
|
486
|
+
const availableTools = agentComponent.getAvailableTools(agent);
|
|
487
|
+
|
|
488
|
+
expect(availableTools).toHaveLength(2);
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
it("should filter tools by allowedTools", () => {
|
|
492
|
+
agentComponent.setDefaultTools([
|
|
493
|
+
{ name: "tool1", execute: vi.fn() },
|
|
494
|
+
{ name: "tool2", execute: vi.fn() },
|
|
495
|
+
{ name: "tool3", execute: vi.fn() },
|
|
496
|
+
]);
|
|
497
|
+
|
|
498
|
+
agentComponent.registerAgent("test-agent", {
|
|
499
|
+
type: "primary",
|
|
500
|
+
allowedTools: ["tool1", "tool2"],
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
const agent = agentComponent.getAgent("test-agent")!;
|
|
504
|
+
const availableTools = agentComponent.getAvailableTools(agent);
|
|
505
|
+
|
|
506
|
+
expect(availableTools).toHaveLength(2);
|
|
507
|
+
expect(availableTools.every(t => ["tool1", "tool2"].includes(t.name))).toBe(true);
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
it("should filter tools by deniedTools", () => {
|
|
511
|
+
agentComponent.setDefaultTools([
|
|
512
|
+
{ name: "tool1", execute: vi.fn() },
|
|
513
|
+
{ name: "tool2", execute: vi.fn() },
|
|
514
|
+
{ name: "tool3", execute: vi.fn() },
|
|
515
|
+
]);
|
|
516
|
+
|
|
517
|
+
agentComponent.registerAgent("test-agent", {
|
|
518
|
+
type: "primary",
|
|
519
|
+
deniedTools: ["tool3"],
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
const agent = agentComponent.getAgent("test-agent")!;
|
|
523
|
+
const availableTools = agentComponent.getAvailableTools(agent);
|
|
524
|
+
|
|
525
|
+
expect(availableTools).toHaveLength(2);
|
|
526
|
+
expect(availableTools.find(t => t.name === "tool3")).toBeUndefined();
|
|
527
|
+
});
|
|
528
|
+
});
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
// ============================================================================
|
|
532
|
+
// AgentComponent Config Loading Tests (TDD)
|
|
533
|
+
// ============================================================================
|
|
534
|
+
|
|
535
|
+
describe("AgentComponent Config Loading", () => {
|
|
536
|
+
let component: AgentComponent;
|
|
537
|
+
let configComponent: ConfigComponent;
|
|
538
|
+
let mockEnv: Environment;
|
|
539
|
+
let tempDir: string;
|
|
540
|
+
|
|
541
|
+
beforeEach(() => {
|
|
542
|
+
component = new AgentComponent();
|
|
543
|
+
configComponent = new ConfigComponent();
|
|
544
|
+
|
|
545
|
+
// Create temp directory for config files
|
|
546
|
+
tempDir = fsSync.mkdtempSync(path.join(os.tmpdir(), "agent-config-test-"));
|
|
547
|
+
|
|
548
|
+
// Set XDG_DATA_HOME to temp directory so ConfigComponent can find config files
|
|
549
|
+
configComponent.setXdgDataHome(tempDir);
|
|
550
|
+
|
|
551
|
+
// Mock environment
|
|
552
|
+
mockEnv = {
|
|
553
|
+
name: "test-env",
|
|
554
|
+
version: "1.0.0",
|
|
555
|
+
getComponent: vi.fn((name: string) => {
|
|
556
|
+
if (name === "config") return configComponent;
|
|
557
|
+
return undefined;
|
|
558
|
+
}),
|
|
559
|
+
hasComponent: vi.fn((name: string) => name === "config"),
|
|
560
|
+
logger: vi.fn(),
|
|
561
|
+
trace: vi.fn(),
|
|
562
|
+
} as unknown as Environment;
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
afterEach(() => {
|
|
566
|
+
vi.restoreAllMocks();
|
|
567
|
+
// Clean up environment variables
|
|
568
|
+
delete process.env.AGENT_AGENT_DEFAULT_AGENT_MAX_ITERATIONS;
|
|
569
|
+
// Clean up temp directory
|
|
570
|
+
try {
|
|
571
|
+
fsSync.rmSync(tempDir, { recursive: true });
|
|
572
|
+
} catch {}
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
describe("AgentComponentOptions", () => {
|
|
576
|
+
test("should accept configComponent only", async () => {
|
|
577
|
+
await component.init({
|
|
578
|
+
name: "agent",
|
|
579
|
+
version: "1.0.0",
|
|
580
|
+
enabled: true,
|
|
581
|
+
env: mockEnv,
|
|
582
|
+
options: { configComponent }
|
|
583
|
+
});
|
|
584
|
+
expect(component.getStatus()).toBe("running");
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
test("should accept configPath option", async () => {
|
|
588
|
+
fsSync.writeFileSync(path.join(tempDir, "config.jsonc"), JSON.stringify({
|
|
589
|
+
agent: {
|
|
590
|
+
defaultAgent: {
|
|
591
|
+
maxIterations: 50
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}), "utf-8");
|
|
595
|
+
|
|
596
|
+
await component.init({
|
|
597
|
+
name: "agent",
|
|
598
|
+
version: "1.0.0",
|
|
599
|
+
enabled: true,
|
|
600
|
+
env: mockEnv,
|
|
601
|
+
options: { configComponent, configPath: "config.jsonc" }
|
|
602
|
+
});
|
|
603
|
+
expect(component.getStatus()).toBe("running");
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
test("should accept envPrefix option", async () => {
|
|
607
|
+
process.env.AGENT_AGENT_DEFAULT_AGENT_MAX_ITERATIONS = "75";
|
|
608
|
+
|
|
609
|
+
await component.init({
|
|
610
|
+
name: "agent",
|
|
611
|
+
version: "1.0.0",
|
|
612
|
+
enabled: true,
|
|
613
|
+
env: mockEnv,
|
|
614
|
+
options: { configComponent, envPrefix: "AGENT" }
|
|
615
|
+
});
|
|
616
|
+
expect(component.getStatus()).toBe("running");
|
|
617
|
+
|
|
618
|
+
delete process.env.AGENT_AGENT_DEFAULT_AGENT_MAX_ITERATIONS;
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
test("should accept config option with direct object", async () => {
|
|
622
|
+
await component.init({
|
|
623
|
+
name: "agent",
|
|
624
|
+
version: "1.0.0",
|
|
625
|
+
enabled: true,
|
|
626
|
+
env: mockEnv,
|
|
627
|
+
options: {
|
|
628
|
+
configComponent,
|
|
629
|
+
config: {
|
|
630
|
+
defaultAgent: {
|
|
631
|
+
maxIterations: 25
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
});
|
|
636
|
+
expect(component.getStatus()).toBe("running");
|
|
637
|
+
});
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
describe("config priority: Object > Env > File", () => {
|
|
641
|
+
test("should Object config override Env config", async () => {
|
|
642
|
+
// Set environment variable
|
|
643
|
+
process.env.AGENT_AGENT_DEFAULT_AGENT_MAX_ITERATIONS = "50";
|
|
644
|
+
|
|
645
|
+
// Create file config
|
|
646
|
+
fsSync.writeFileSync(path.join(tempDir, "config.jsonc"), JSON.stringify({
|
|
647
|
+
agent: { defaultAgent: { maxIterations: 100 } }
|
|
648
|
+
}), "utf-8");
|
|
649
|
+
|
|
650
|
+
await component.init({
|
|
651
|
+
name: "agent",
|
|
652
|
+
version: "1.0.0",
|
|
653
|
+
enabled: true,
|
|
654
|
+
env: mockEnv,
|
|
655
|
+
options: {
|
|
656
|
+
configComponent,
|
|
657
|
+
configPath: "config.jsonc",
|
|
658
|
+
envPrefix: "AGENT",
|
|
659
|
+
config: { defaultAgent: { maxIterations: 25 } } // Object has highest priority
|
|
660
|
+
}
|
|
661
|
+
});
|
|
662
|
+
|
|
663
|
+
// Object config should have highest priority
|
|
664
|
+
expect(configComponent.get("agent.defaultAgent.maxIterations")).toBe(25);
|
|
665
|
+
|
|
666
|
+
delete process.env.AGENT_AGENT_DEFAULT_AGENT_MAX_ITERATIONS;
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
test("should Env config override File config", async () => {
|
|
670
|
+
// Set environment variable
|
|
671
|
+
process.env.AGENT_AGENT_DEFAULT_AGENT_MAX_ITERATIONS = "50";
|
|
672
|
+
|
|
673
|
+
// Create file config
|
|
674
|
+
fsSync.writeFileSync(path.join(tempDir, "config.jsonc"), JSON.stringify({
|
|
675
|
+
agent: { defaultAgent: { maxIterations: 100 } }
|
|
676
|
+
}), "utf-8");
|
|
677
|
+
|
|
678
|
+
await component.init({
|
|
679
|
+
name: "agent",
|
|
680
|
+
version: "1.0.0",
|
|
681
|
+
enabled: true,
|
|
682
|
+
env: mockEnv,
|
|
683
|
+
options: {
|
|
684
|
+
configComponent,
|
|
685
|
+
configPath: "config.jsonc",
|
|
686
|
+
envPrefix: "AGENT"
|
|
687
|
+
}
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
// Env should override file
|
|
691
|
+
expect(configComponent.get("agent.defaultAgent.maxIterations")).toBe("50");
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
test("should File config be loaded when present", async () => {
|
|
695
|
+
fsSync.writeFileSync(path.join(tempDir, "config.jsonc"), JSON.stringify({
|
|
696
|
+
agent: {
|
|
697
|
+
defaultAgent: {
|
|
698
|
+
maxIterations: 100,
|
|
699
|
+
maxErrorRetries: 5
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}), "utf-8");
|
|
703
|
+
|
|
704
|
+
await component.init({
|
|
705
|
+
name: "agent",
|
|
706
|
+
version: "1.0.0",
|
|
707
|
+
enabled: true,
|
|
708
|
+
env: mockEnv,
|
|
709
|
+
options: {
|
|
710
|
+
configComponent,
|
|
711
|
+
configPath: "config.jsonc"
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
|
|
715
|
+
expect(configComponent.get("agent.defaultAgent.maxIterations")).toBe(100);
|
|
716
|
+
expect(configComponent.get("agent.defaultAgent.maxErrorRetries")).toBe(5);
|
|
717
|
+
});
|
|
718
|
+
|
|
719
|
+
test("should use default values when no config provided", async () => {
|
|
720
|
+
await component.init({
|
|
721
|
+
name: "agent",
|
|
722
|
+
version: "1.0.0",
|
|
723
|
+
enabled: true,
|
|
724
|
+
env: mockEnv,
|
|
725
|
+
options: { configComponent }
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
// Default values should be used (配置统一化后应返回默认值)
|
|
729
|
+
// 注意:AGENT_DEFAULTS 中定义的是 200
|
|
730
|
+
expect(configComponent.get("agent.defaultAgent.maxIterations")).toBe(200);
|
|
731
|
+
// maxErrorRetries 没有默认值,所以应该是 undefined
|
|
732
|
+
expect(configComponent.get("agent.defaultAgent.maxErrorRetries")).toBeUndefined();
|
|
733
|
+
});
|
|
734
|
+
});
|
|
735
|
+
|
|
736
|
+
describe("config hot reload", () => {
|
|
737
|
+
test("should register config watcher for hot reload", async () => {
|
|
738
|
+
const watcherFn = vi.fn();
|
|
739
|
+
vi.spyOn(configComponent, "watch").mockReturnValue(watcherFn);
|
|
740
|
+
|
|
741
|
+
await component.init({
|
|
742
|
+
name: "agent",
|
|
743
|
+
version: "1.0.0",
|
|
744
|
+
enabled: true,
|
|
745
|
+
env: mockEnv,
|
|
746
|
+
options: { configComponent }
|
|
747
|
+
});
|
|
748
|
+
|
|
749
|
+
expect(configComponent.watch).toHaveBeenCalledWith("agent.*", expect.any(Function));
|
|
750
|
+
});
|
|
751
|
+
});
|
|
752
|
+
|
|
753
|
+
// ============================================================================
|
|
754
|
+
// TDD: Tool Results Completeness Tests
|
|
755
|
+
//
|
|
756
|
+
// Bug: AI SDK v6 requires 1:1 correspondence between tool-calls and tool-results
|
|
757
|
+
// Error: "Tool results are missing for tool calls call_function_xxx_1, _2, _3"
|
|
758
|
+
//
|
|
759
|
+
// When processing multiple tool calls, if abort/doom-loop/hook-stopped occurs
|
|
760
|
+
// mid-processing, remaining tool calls don't get results, causing AI SDK errors.
|
|
761
|
+
// ============================================================================
|
|
762
|
+
|
|
763
|
+
describe("Tool Results Completeness (TDD)", () => {
|
|
764
|
+
let agentComponent: AgentComponent;
|
|
765
|
+
let mockEnv: Environment;
|
|
766
|
+
let configComponent: ConfigComponent;
|
|
767
|
+
|
|
768
|
+
beforeEach(async () => {
|
|
769
|
+
vi.clearAllMocks();
|
|
770
|
+
agentComponent = new AgentComponent();
|
|
771
|
+
configComponent = new ConfigComponent();
|
|
772
|
+
|
|
773
|
+
mockEnv = {
|
|
774
|
+
name: "test-env",
|
|
775
|
+
version: "1.0.0",
|
|
776
|
+
getConfig: vi.fn(() => ({ name: "test-env", version: "1.0.0", enabled: true })),
|
|
777
|
+
registerComponent: vi.fn(),
|
|
778
|
+
unregisterComponent: vi.fn(),
|
|
779
|
+
getComponent: vi.fn((name: string) => {
|
|
780
|
+
if (name === "config") return configComponent;
|
|
781
|
+
return undefined;
|
|
782
|
+
}),
|
|
783
|
+
hasComponent: vi.fn((name: string) => name === "config"),
|
|
784
|
+
listComponents: vi.fn(() => []),
|
|
785
|
+
handle_query: vi.fn(),
|
|
786
|
+
handle_action: vi.fn(),
|
|
787
|
+
subscribe: vi.fn(() => () => {}),
|
|
788
|
+
subscribeTo: vi.fn(() => () => {}),
|
|
789
|
+
subscribeAll: vi.fn(() => () => {}),
|
|
790
|
+
pushEnvEvent: vi.fn(),
|
|
791
|
+
};
|
|
792
|
+
|
|
793
|
+
await agentComponent.init({ env: mockEnv, options: { configComponent } });
|
|
794
|
+
});
|
|
795
|
+
|
|
796
|
+
afterEach(() => {
|
|
797
|
+
delete process.env.AGENT_AGENT_DEFAULT_AGENT_MAX_ITERATIONS;
|
|
798
|
+
});
|
|
799
|
+
|
|
800
|
+
describe("addRemainingToolResults helper", () => {
|
|
801
|
+
it("should add placeholder results for all remaining tool calls", () => {
|
|
802
|
+
// Register agent
|
|
803
|
+
agentComponent.registerAgent("test-agent", { type: "primary" });
|
|
804
|
+
|
|
805
|
+
// Access private method for testing
|
|
806
|
+
const agent = agentComponent.getAgent("test-agent")!;
|
|
807
|
+
const messages: any[] = [];
|
|
808
|
+
|
|
809
|
+
const allToolCalls = [
|
|
810
|
+
{ id: "call_1", function: { name: "tool1" } },
|
|
811
|
+
{ id: "call_2", function: { name: "tool2" } },
|
|
812
|
+
{ id: "call_3", function: { name: "tool3" } },
|
|
813
|
+
];
|
|
814
|
+
|
|
815
|
+
// Call the helper (we'll test via run() method behavior instead)
|
|
816
|
+
// For now, just verify the structure
|
|
817
|
+
expect(allToolCalls).toHaveLength(3);
|
|
818
|
+
});
|
|
819
|
+
});
|
|
820
|
+
|
|
821
|
+
describe("abort during tool call processing", () => {
|
|
822
|
+
it("should add placeholder results for unprocessed tool calls when abort occurs", async () => {
|
|
823
|
+
agentComponent.registerAgent("test-agent", {
|
|
824
|
+
type: "primary",
|
|
825
|
+
maxIterations: 10,
|
|
826
|
+
});
|
|
827
|
+
|
|
828
|
+
// Mock LLMComponent that returns 3 tool calls
|
|
829
|
+
const mockLLMComponent = {
|
|
830
|
+
invoke: vi.fn(() => Promise.resolve({
|
|
831
|
+
output: {
|
|
832
|
+
content: "",
|
|
833
|
+
toolCalls: [
|
|
834
|
+
{ id: "call_1", function: { name: "tool1", arguments: "{}" } },
|
|
835
|
+
{ id: "call_2", function: { name: "tool2", arguments: "{}" } },
|
|
836
|
+
{ id: "call_3", function: { name: "tool3", arguments: "{}" } },
|
|
837
|
+
]
|
|
838
|
+
}
|
|
839
|
+
}))
|
|
840
|
+
};
|
|
841
|
+
(mockEnv.getComponent as any).mockReturnValue(mockLLMComponent);
|
|
842
|
+
|
|
843
|
+
// Mock tool execution - execute first tool, then abort
|
|
844
|
+
const mockTool = {
|
|
845
|
+
name: "tool1",
|
|
846
|
+
execute: vi.fn(async () => {
|
|
847
|
+
// Simulate abort after first tool
|
|
848
|
+
agentComponent.abort("test-agent");
|
|
849
|
+
return { success: true, output: "result1" };
|
|
850
|
+
}),
|
|
851
|
+
};
|
|
852
|
+
const mockTool2 = { name: "tool2", execute: vi.fn() };
|
|
853
|
+
const mockTool3 = { name: "tool3", execute: vi.fn() };
|
|
854
|
+
|
|
855
|
+
agentComponent.setDefaultTools([mockTool, mockTool2, mockTool3]);
|
|
856
|
+
|
|
857
|
+
// Run agent
|
|
858
|
+
const result = await agentComponent.run("test-agent", "Execute multiple tools and abort");
|
|
859
|
+
|
|
860
|
+
// Verify: result should indicate stopped
|
|
861
|
+
expect(result.stopped).toBe(true);
|
|
862
|
+
// The key test: run() should complete without throwing MissingToolResultsError
|
|
863
|
+
// This is verified by the fact that we reach here without error
|
|
864
|
+
expect(true).toBe(true);
|
|
865
|
+
|
|
866
|
+
// The key assertion: there should be no MissingToolResultsError
|
|
867
|
+
// This is verified by the fact that run() completed without throwing
|
|
868
|
+
expect(true).toBe(true); // If we reach here, the bug is fixed
|
|
869
|
+
});
|
|
870
|
+
});
|
|
871
|
+
|
|
872
|
+
describe("doom loop detection during tool call processing", () => {
|
|
873
|
+
it("should add placeholder results for unprocessed tool calls when doom loop detected", async () => {
|
|
874
|
+
agentComponent.registerAgent("test-agent", {
|
|
875
|
+
type: "primary",
|
|
876
|
+
maxIterations: 10,
|
|
877
|
+
doomLoopThreshold: 2, // Trigger doom loop quickly
|
|
878
|
+
});
|
|
879
|
+
|
|
880
|
+
// Mock LLMComponent that returns same tool call repeatedly
|
|
881
|
+
const mockLLMComponent = {
|
|
882
|
+
invoke: vi.fn(() => Promise.resolve({
|
|
883
|
+
output: {
|
|
884
|
+
content: "",
|
|
885
|
+
toolCalls: [
|
|
886
|
+
{ id: "call_1", function: { name: "repeat_tool", arguments: '{"key":"value"}' } },
|
|
887
|
+
{ id: "call_2", function: { name: "repeat_tool", arguments: '{"key":"value"}' } },
|
|
888
|
+
{ id: "call_3", function: { name: "repeat_tool", arguments: '{"key":"value"}' } },
|
|
889
|
+
]
|
|
890
|
+
}
|
|
891
|
+
}))
|
|
892
|
+
};
|
|
893
|
+
(mockEnv.getComponent as any).mockReturnValue(mockLLMComponent);
|
|
894
|
+
|
|
895
|
+
// Mock tool that returns same result (causing doom loop)
|
|
896
|
+
const mockTool = {
|
|
897
|
+
name: "repeat_tool",
|
|
898
|
+
execute: vi.fn(() => Promise.resolve({ success: true, output: "same_result" })),
|
|
899
|
+
};
|
|
900
|
+
agentComponent.setDefaultTools([mockTool]);
|
|
901
|
+
|
|
902
|
+
// Run agent - should trigger doom loop detection
|
|
903
|
+
// After doom loop, remaining tool calls should get placeholder results
|
|
904
|
+
// This should not throw MissingToolResultsError
|
|
905
|
+
try {
|
|
906
|
+
const result = await agentComponent.run("test-agent", "Trigger doom loop");
|
|
907
|
+
// If we reach here without error, the fix is working
|
|
908
|
+
expect(result.error).toContain("Doom loop");
|
|
909
|
+
} catch (error: any) {
|
|
910
|
+
// Should NOT contain MissingToolResultsError
|
|
911
|
+
expect(error.message).not.toContain("Tool results are missing");
|
|
912
|
+
}
|
|
913
|
+
});
|
|
914
|
+
|
|
915
|
+
it("should reset doom loop count across iterations (not accumulate across iterations)", async () => {
|
|
916
|
+
agentComponent.registerAgent("test-agent", {
|
|
917
|
+
type: "primary",
|
|
918
|
+
maxIterations: 10,
|
|
919
|
+
doomLoopThreshold: 3, // Need 3+ same calls within one iteration to trigger
|
|
920
|
+
});
|
|
921
|
+
|
|
922
|
+
let invokeCount = 0;
|
|
923
|
+
|
|
924
|
+
// Mock LLMComponent:
|
|
925
|
+
// - Iteration 1: returns same tool call once (count=1, not enough to trigger)
|
|
926
|
+
// - Iteration 2: returns same tool call once (count should reset to 1, not 2!)
|
|
927
|
+
// - Iteration 3: returns same tool call once (count should reset to 1, not 3!)
|
|
928
|
+
// If doom loop count accumulates across iterations, iteration 3 would trigger doom loop
|
|
929
|
+
// If doom loop count resets per iteration, it should NOT trigger doom loop
|
|
930
|
+
const mockLLMComponent = {
|
|
931
|
+
invoke: vi.fn(() => {
|
|
932
|
+
invokeCount++;
|
|
933
|
+
return Promise.resolve({
|
|
934
|
+
output: {
|
|
935
|
+
content: invokeCount >= 4 ? "done" : "", // Stop after 3 iterations
|
|
936
|
+
toolCalls: [
|
|
937
|
+
{ id: `call_${invokeCount}`, function: { name: "repeat_tool", arguments: '{"key":"value"}' } },
|
|
938
|
+
]
|
|
939
|
+
}
|
|
940
|
+
});
|
|
941
|
+
})
|
|
942
|
+
};
|
|
943
|
+
(mockEnv.getComponent as any).mockReturnValue(mockLLMComponent);
|
|
944
|
+
|
|
945
|
+
// Mock tool that returns same result
|
|
946
|
+
const mockTool = {
|
|
947
|
+
name: "repeat_tool",
|
|
948
|
+
execute: vi.fn(() => Promise.resolve({ success: true, output: "same_result" })),
|
|
949
|
+
};
|
|
950
|
+
agentComponent.setDefaultTools([mockTool]);
|
|
951
|
+
|
|
952
|
+
const result = await agentComponent.run("test-agent", "Test doom loop reset across iterations");
|
|
953
|
+
|
|
954
|
+
// Should NOT trigger doom loop because count should reset each iteration
|
|
955
|
+
// If this test fails, it means doom loop count accumulated across iterations
|
|
956
|
+
expect(result.error).toBeUndefined();
|
|
957
|
+
// Should complete with max iterations reached or text response
|
|
958
|
+
expect(result.iterations).toBeGreaterThanOrEqual(3);
|
|
959
|
+
});
|
|
960
|
+
});
|
|
961
|
+
|
|
962
|
+
describe("hook _stopped during tool call processing", () => {
|
|
963
|
+
it("should add placeholder results when before.tool hook stops execution", async () => {
|
|
964
|
+
agentComponent.registerAgent("test-agent", {
|
|
965
|
+
type: "primary",
|
|
966
|
+
maxIterations: 10,
|
|
967
|
+
});
|
|
968
|
+
|
|
969
|
+
// Mock LLMComponent that returns 3 tool calls
|
|
970
|
+
const mockLLMComponent = {
|
|
971
|
+
invoke: vi.fn(() => Promise.resolve({
|
|
972
|
+
output: {
|
|
973
|
+
content: "",
|
|
974
|
+
toolCalls: [
|
|
975
|
+
{ id: "call_1", function: { name: "tool1", arguments: "{}" } },
|
|
976
|
+
{ id: "call_2", function: { name: "tool2", arguments: "{}" } },
|
|
977
|
+
{ id: "call_3", function: { name: "tool3", arguments: "{}" } },
|
|
978
|
+
]
|
|
979
|
+
}
|
|
980
|
+
}))
|
|
981
|
+
};
|
|
982
|
+
(mockEnv.getComponent as any).mockReturnValue(mockLLMComponent);
|
|
983
|
+
|
|
984
|
+
// Mock tool
|
|
985
|
+
const mockTool = { name: "tool1", execute: vi.fn() };
|
|
986
|
+
const mockTool2 = { name: "tool2", execute: vi.fn() };
|
|
987
|
+
const mockTool3 = { name: "tool3", execute: vi.fn() };
|
|
988
|
+
agentComponent.setDefaultTools([mockTool, mockTool2, mockTool3]);
|
|
989
|
+
|
|
990
|
+
// Register plugin that stops execution after first tool
|
|
991
|
+
const plugin = {
|
|
992
|
+
name: "stop-after-first-tool",
|
|
993
|
+
version: "1.0.0",
|
|
994
|
+
hooks: [{ point: "agent:before.tool" as const }],
|
|
995
|
+
execute: vi.fn((ctx: any) => {
|
|
996
|
+
// Stop after processing tool1
|
|
997
|
+
if (ctx.currentToolCall?.name === "tool2") {
|
|
998
|
+
return { continue: false };
|
|
999
|
+
}
|
|
1000
|
+
return { continue: true };
|
|
1001
|
+
}),
|
|
1002
|
+
};
|
|
1003
|
+
agentComponent.registerPlugin("test-agent", plugin);
|
|
1004
|
+
|
|
1005
|
+
// Run agent - should not throw MissingToolResultsError
|
|
1006
|
+
try {
|
|
1007
|
+
const result = await agentComponent.run("test-agent", "Execute with stopping hook");
|
|
1008
|
+
// If we reach here without error, the fix is working
|
|
1009
|
+
expect(result.stopped).toBe(true);
|
|
1010
|
+
} catch (error: any) {
|
|
1011
|
+
// Should NOT contain MissingToolResultsError
|
|
1012
|
+
expect(error.message).not.toContain("Tool results are missing");
|
|
1013
|
+
}
|
|
1014
|
+
});
|
|
1015
|
+
});
|
|
1016
|
+
|
|
1017
|
+
describe("_stopped state propagation to next iteration", () => {
|
|
1018
|
+
it("should break while loop when _stopped is set by hook in previous iteration", async () => {
|
|
1019
|
+
agentComponent.registerAgent("test-agent", {
|
|
1020
|
+
type: "primary",
|
|
1021
|
+
maxIterations: 10,
|
|
1022
|
+
});
|
|
1023
|
+
|
|
1024
|
+
let iteration = 0;
|
|
1025
|
+
const mockLLMComponent = {
|
|
1026
|
+
invoke: vi.fn(() => {
|
|
1027
|
+
iteration++;
|
|
1028
|
+
if (iteration === 1) {
|
|
1029
|
+
// First iteration: returns tool call and sets _stopped via hook
|
|
1030
|
+
return Promise.resolve({
|
|
1031
|
+
output: {
|
|
1032
|
+
content: "",
|
|
1033
|
+
toolCalls: [
|
|
1034
|
+
{ id: "call_1", function: { name: "tool1", arguments: "{}" } },
|
|
1035
|
+
]
|
|
1036
|
+
}
|
|
1037
|
+
});
|
|
1038
|
+
}
|
|
1039
|
+
// Second iteration should NOT happen
|
|
1040
|
+
return Promise.resolve({
|
|
1041
|
+
output: {
|
|
1042
|
+
content: "should not reach here",
|
|
1043
|
+
toolCalls: []
|
|
1044
|
+
}
|
|
1045
|
+
});
|
|
1046
|
+
})
|
|
1047
|
+
};
|
|
1048
|
+
(mockEnv.getComponent as any).mockReturnValue(mockLLMComponent);
|
|
1049
|
+
|
|
1050
|
+
const mockTool = {
|
|
1051
|
+
name: "tool1",
|
|
1052
|
+
execute: vi.fn(() => Promise.resolve({ success: true, output: "result" }))
|
|
1053
|
+
};
|
|
1054
|
+
agentComponent.setDefaultTools([mockTool]);
|
|
1055
|
+
|
|
1056
|
+
// Register hook that sets _stopped after first iteration's on.iteration
|
|
1057
|
+
let firstIterationComplete = false;
|
|
1058
|
+
const plugin = {
|
|
1059
|
+
name: "stop-after-first-iteration",
|
|
1060
|
+
version: "1.0.0",
|
|
1061
|
+
hooks: [{ point: "agent:on.iteration" as const }],
|
|
1062
|
+
execute: vi.fn((ctx: any) => {
|
|
1063
|
+
if (!firstIterationComplete) {
|
|
1064
|
+
firstIterationComplete = true;
|
|
1065
|
+
ctx._stopped = true;
|
|
1066
|
+
ctx._stopReason = "stopped by hook";
|
|
1067
|
+
}
|
|
1068
|
+
return { continue: true };
|
|
1069
|
+
}),
|
|
1070
|
+
};
|
|
1071
|
+
agentComponent.registerPlugin("test-agent", plugin);
|
|
1072
|
+
|
|
1073
|
+
// Run agent
|
|
1074
|
+
const result = await agentComponent.run("test-agent", "Test _stopped propagation");
|
|
1075
|
+
|
|
1076
|
+
// Verify: should only run 1 iteration (not 2)
|
|
1077
|
+
expect(result.iterations).toBe(1);
|
|
1078
|
+
expect(mockLLMComponent.invoke).toHaveBeenCalledTimes(1);
|
|
1079
|
+
});
|
|
1080
|
+
});
|
|
1081
|
+
});
|
|
1082
|
+
|
|
1083
|
+
describe("getConversationHistory", () => {
|
|
1084
|
+
let mockSessionComponent: any;
|
|
1085
|
+
let agent: AgentComponent;
|
|
1086
|
+
|
|
1087
|
+
beforeEach(async () => {
|
|
1088
|
+
agent = new AgentComponent();
|
|
1089
|
+
await agent.init({ env: mockEnv, options: { configComponent } });
|
|
1090
|
+
|
|
1091
|
+
// Create mock session component with getContext (new implementation)
|
|
1092
|
+
mockSessionComponent = {
|
|
1093
|
+
getContext: vi.fn(),
|
|
1094
|
+
};
|
|
1095
|
+
|
|
1096
|
+
// Update mockEnv to return our mock session component
|
|
1097
|
+
(mockEnv.getComponent as ReturnType<typeof vi.fn>).mockImplementation((name: string) => {
|
|
1098
|
+
if (name === "session") return mockSessionComponent;
|
|
1099
|
+
if (name === "config") return configComponent;
|
|
1100
|
+
return undefined;
|
|
1101
|
+
});
|
|
1102
|
+
});
|
|
1103
|
+
|
|
1104
|
+
it("应该返回空数组当会话没有消息", async () => {
|
|
1105
|
+
mockSessionComponent.getContext.mockResolvedValue({
|
|
1106
|
+
messages: []
|
|
1107
|
+
});
|
|
1108
|
+
|
|
1109
|
+
const history = await (agent as any).getConversationHistory(
|
|
1110
|
+
mockSessionComponent,
|
|
1111
|
+
"session_123",
|
|
1112
|
+
{ minUserMessages: 5 }
|
|
1113
|
+
);
|
|
1114
|
+
|
|
1115
|
+
expect(history).toEqual([]);
|
|
1116
|
+
});
|
|
1117
|
+
|
|
1118
|
+
it("应该使用 getContext 获取消息并从 user 消息边界截取", async () => {
|
|
1119
|
+
const sessionId = "session_123";
|
|
1120
|
+
|
|
1121
|
+
// getContext 已经处理了 minUserMessages 逻辑,直接返回从 user 边界截取后的消息
|
|
1122
|
+
const mockMessages = [
|
|
1123
|
+
{ id: "msg_3", role: "user", content: "User 2", metadata: {} },
|
|
1124
|
+
{ id: "msg_4", role: "assistant", content: "Assistant 2", metadata: {} },
|
|
1125
|
+
{ id: "msg_5", role: "user", content: "User 3", metadata: {} },
|
|
1126
|
+
{ id: "msg_6", role: "assistant", content: "Assistant 3", metadata: {} },
|
|
1127
|
+
];
|
|
1128
|
+
mockSessionComponent.getContext.mockResolvedValue({
|
|
1129
|
+
messages: mockMessages
|
|
1130
|
+
});
|
|
1131
|
+
|
|
1132
|
+
const history = await (agent as any).getConversationHistory(
|
|
1133
|
+
mockSessionComponent,
|
|
1134
|
+
sessionId,
|
|
1135
|
+
{ minUserMessages: 2 }
|
|
1136
|
+
);
|
|
1137
|
+
|
|
1138
|
+
// Verify: getContext should be called with fullHistory=false and minUserMessages
|
|
1139
|
+
expect(mockSessionComponent.getContext).toHaveBeenCalledWith(sessionId, {
|
|
1140
|
+
fullHistory: false,
|
|
1141
|
+
minUserMessages: 2,
|
|
1142
|
+
});
|
|
1143
|
+
|
|
1144
|
+
expect(history).toHaveLength(4);
|
|
1145
|
+
expect(history.map((m: any) => m.content)).toEqual([
|
|
1146
|
+
"User 2", "Assistant 2", "User 3", "Assistant 3"
|
|
1147
|
+
]);
|
|
1148
|
+
});
|
|
1149
|
+
|
|
1150
|
+
it("应该正确处理 filterHistory=true 时过滤 tool 消息", async () => {
|
|
1151
|
+
const mockMessages = [
|
|
1152
|
+
{ id: "msg_0", role: "user", content: "User 1", metadata: {} },
|
|
1153
|
+
{ id: "msg_1", role: "assistant", content: "Assistant 1", metadata: {} },
|
|
1154
|
+
{ id: "msg_2", role: "tool", content: "tool result", metadata: { type: "tool_result" } },
|
|
1155
|
+
{ id: "msg_3", role: "assistant", content: "Assistant 2", metadata: {} },
|
|
1156
|
+
];
|
|
1157
|
+
mockSessionComponent.getContext.mockResolvedValue({
|
|
1158
|
+
messages: mockMessages
|
|
1159
|
+
});
|
|
1160
|
+
|
|
1161
|
+
const history = await (agent as any).getConversationHistory(
|
|
1162
|
+
mockSessionComponent,
|
|
1163
|
+
"session_123",
|
|
1164
|
+
{ filterHistory: true }
|
|
1165
|
+
);
|
|
1166
|
+
|
|
1167
|
+
// Should filter out tool messages
|
|
1168
|
+
expect(history).toHaveLength(3);
|
|
1169
|
+
expect(history.map((m: any) => m.role)).toEqual(["user", "assistant", "assistant"]);
|
|
1170
|
+
expect(history.find((m: any) => m.role === "tool")).toBeUndefined();
|
|
1171
|
+
});
|
|
1172
|
+
|
|
1173
|
+
it("应该正确处理 filterHistory=false 时保留所有消息", async () => {
|
|
1174
|
+
const mockMessages = [
|
|
1175
|
+
{ id: "msg_0", role: "user", content: "User 1", metadata: {} },
|
|
1176
|
+
{ id: "msg_1", role: "assistant", content: "Assistant 1", metadata: {} },
|
|
1177
|
+
{ id: "msg_2", role: "tool", content: "tool result", metadata: { type: "tool_result" } },
|
|
1178
|
+
{ id: "msg_3", role: "assistant", content: "Assistant 2", metadata: {} },
|
|
1179
|
+
];
|
|
1180
|
+
mockSessionComponent.getContext.mockResolvedValue({
|
|
1181
|
+
messages: mockMessages
|
|
1182
|
+
});
|
|
1183
|
+
|
|
1184
|
+
const history = await (agent as any).getConversationHistory(
|
|
1185
|
+
mockSessionComponent,
|
|
1186
|
+
"session_123",
|
|
1187
|
+
{ filterHistory: false }
|
|
1188
|
+
);
|
|
1189
|
+
|
|
1190
|
+
// Should keep all messages including tool
|
|
1191
|
+
expect(history).toHaveLength(4);
|
|
1192
|
+
expect(history.map((m: any) => m.role)).toEqual(["user", "assistant", "tool", "assistant"]);
|
|
1193
|
+
});
|
|
1194
|
+
|
|
1195
|
+
it("默认 filterHistory=false(不指定时)", async () => {
|
|
1196
|
+
const mockMessages = [
|
|
1197
|
+
{ id: "msg_0", role: "user", content: "User 1", metadata: {} },
|
|
1198
|
+
{ id: "msg_1", role: "assistant", content: "Assistant 1", metadata: {} },
|
|
1199
|
+
{ id: "msg_2", role: "tool", content: "tool result", metadata: { type: "tool_result" } },
|
|
1200
|
+
{ id: "msg_3", role: "assistant", content: "Assistant 2", metadata: {} },
|
|
1201
|
+
];
|
|
1202
|
+
mockSessionComponent.getContext.mockResolvedValue({
|
|
1203
|
+
messages: mockMessages
|
|
1204
|
+
});
|
|
1205
|
+
|
|
1206
|
+
// 不指定 filterHistory 参数
|
|
1207
|
+
const history = await (agent as any).getConversationHistory(
|
|
1208
|
+
mockSessionComponent,
|
|
1209
|
+
"session_123",
|
|
1210
|
+
{}
|
|
1211
|
+
);
|
|
1212
|
+
|
|
1213
|
+
// 默认应该保留所有消息(包括 tool 消息)
|
|
1214
|
+
expect(history).toHaveLength(4);
|
|
1215
|
+
expect(history.map((m: any) => m.role)).toEqual(["user", "assistant", "tool", "assistant"]);
|
|
1216
|
+
});
|
|
1217
|
+
|
|
1218
|
+
it("应该正确处理消息少于 minUserMessages 的情况", async () => {
|
|
1219
|
+
const mockMessages = [
|
|
1220
|
+
{ id: "msg_0", role: "user", content: "User 1", metadata: {} },
|
|
1221
|
+
{ id: "msg_1", role: "assistant", content: "Assistant 1", metadata: {} },
|
|
1222
|
+
];
|
|
1223
|
+
mockSessionComponent.getContext.mockResolvedValue({
|
|
1224
|
+
messages: mockMessages
|
|
1225
|
+
});
|
|
1226
|
+
|
|
1227
|
+
// 当消息少于 minUserMessages 时,getContext 会返回所有消息
|
|
1228
|
+
const history = await (agent as any).getConversationHistory(
|
|
1229
|
+
mockSessionComponent,
|
|
1230
|
+
"session_123",
|
|
1231
|
+
{ minUserMessages: 5 }
|
|
1232
|
+
);
|
|
1233
|
+
|
|
1234
|
+
expect(mockSessionComponent.getContext).toHaveBeenCalledWith("session_123", {
|
|
1235
|
+
fullHistory: false,
|
|
1236
|
+
minUserMessages: 5,
|
|
1237
|
+
});
|
|
1238
|
+
expect(history).toHaveLength(2);
|
|
1239
|
+
});
|
|
1240
|
+
});
|
|
1241
|
+
});
|
|
1242
|
+
|
|
1243
|
+
// ============================================================================
|
|
1244
|
+
// toLLMMessage Tests
|
|
1245
|
+
// ============================================================================
|
|
1246
|
+
|
|
1247
|
+
describe("toLLMMessage (message conversion)", () => {
|
|
1248
|
+
// Helper function extracted from agent-component.ts for testing
|
|
1249
|
+
function toLLMMessage(msg: any): any {
|
|
1250
|
+
let content: string;
|
|
1251
|
+
let toolCallId: string | undefined;
|
|
1252
|
+
let toolName: string | undefined;
|
|
1253
|
+
let toolCalls: any[] | undefined;
|
|
1254
|
+
|
|
1255
|
+
if (typeof msg.content === "string") {
|
|
1256
|
+
content = msg.content;
|
|
1257
|
+
if (msg.role === "tool") {
|
|
1258
|
+
toolCallId = msg.toolCallId;
|
|
1259
|
+
toolName = msg.toolName;
|
|
1260
|
+
}
|
|
1261
|
+
if (msg.role === "assistant" && msg.toolCalls) {
|
|
1262
|
+
toolCalls = msg.toolCalls;
|
|
1263
|
+
}
|
|
1264
|
+
} else if (Array.isArray(msg.content)) {
|
|
1265
|
+
if (msg.role === "tool") {
|
|
1266
|
+
const toolResult = msg.content.find((part: any) => part.type === "tool-result");
|
|
1267
|
+
const output = toolResult?.output;
|
|
1268
|
+
if (typeof output === "string") {
|
|
1269
|
+
content = output;
|
|
1270
|
+
} else if (output && typeof output === "object" && "value" in output) {
|
|
1271
|
+
content = output.value?.toString() || "";
|
|
1272
|
+
} else {
|
|
1273
|
+
content = JSON.stringify(output) || "";
|
|
1274
|
+
}
|
|
1275
|
+
toolCallId = toolResult?.toolCallId;
|
|
1276
|
+
toolName = toolResult?.toolName;
|
|
1277
|
+
} else {
|
|
1278
|
+
const textParts = msg.content.filter((part: any) => part.type === "text");
|
|
1279
|
+
const toolCallParts = msg.content.filter((part: any) => part.type === "tool-call");
|
|
1280
|
+
|
|
1281
|
+
content = textParts.map((part: any) => part.text || "").join("");
|
|
1282
|
+
|
|
1283
|
+
// Fixed: handle flat tool-call format from toModelMessage
|
|
1284
|
+
if (msg.role === "assistant" && toolCallParts.length > 0) {
|
|
1285
|
+
toolCalls = toolCallParts.map((part: any) => {
|
|
1286
|
+
const argsString = typeof part.input === "string"
|
|
1287
|
+
? part.input
|
|
1288
|
+
: JSON.stringify(part.input || {});
|
|
1289
|
+
const name = part.toolName || "unknown";
|
|
1290
|
+
|
|
1291
|
+
return {
|
|
1292
|
+
id: part.toolCallId,
|
|
1293
|
+
name: name,
|
|
1294
|
+
arguments: argsString,
|
|
1295
|
+
function: {
|
|
1296
|
+
name: name,
|
|
1297
|
+
arguments: argsString,
|
|
1298
|
+
},
|
|
1299
|
+
};
|
|
1300
|
+
});
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
} else {
|
|
1304
|
+
content = "";
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
return {
|
|
1308
|
+
role: msg.role,
|
|
1309
|
+
content,
|
|
1310
|
+
...(toolCallId ? { toolCallId } : {}),
|
|
1311
|
+
...(toolName ? { name: toolName } : {}),
|
|
1312
|
+
...(toolCalls && toolCalls.length > 0 ? { toolCalls } : {}),
|
|
1313
|
+
};
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
it("should convert assistant message with tool-call parts (flat format from toModelMessage)", () => {
|
|
1317
|
+
// Simulate the format generated by SessionMessageConverter.toModelMessage
|
|
1318
|
+
const modelMessage = {
|
|
1319
|
+
role: "assistant",
|
|
1320
|
+
content: [
|
|
1321
|
+
{ type: "text", text: "I'll search for that" },
|
|
1322
|
+
{ type: "tool-call", toolCallId: "call_123", toolName: "search", input: { query: "test" } }
|
|
1323
|
+
]
|
|
1324
|
+
};
|
|
1325
|
+
|
|
1326
|
+
const result = toLLMMessage(modelMessage);
|
|
1327
|
+
|
|
1328
|
+
expect(result.role).toBe("assistant");
|
|
1329
|
+
expect(result.content).toBe("I'll search for that");
|
|
1330
|
+
expect(result.toolCalls).toBeDefined();
|
|
1331
|
+
expect(result.toolCalls).toHaveLength(1);
|
|
1332
|
+
expect(result.toolCalls[0]).toEqual({
|
|
1333
|
+
id: "call_123",
|
|
1334
|
+
name: "search",
|
|
1335
|
+
arguments: '{"query":"test"}',
|
|
1336
|
+
function: {
|
|
1337
|
+
name: "search",
|
|
1338
|
+
arguments: '{"query":"test"}',
|
|
1339
|
+
},
|
|
1340
|
+
});
|
|
1341
|
+
});
|
|
1342
|
+
|
|
1343
|
+
it("should convert tool message with tool-result part", () => {
|
|
1344
|
+
const modelMessage = {
|
|
1345
|
+
role: "tool",
|
|
1346
|
+
content: [
|
|
1347
|
+
{ type: "tool-result", toolCallId: "call_123", toolName: "search", output: "search results" }
|
|
1348
|
+
]
|
|
1349
|
+
};
|
|
1350
|
+
|
|
1351
|
+
const result = toLLMMessage(modelMessage);
|
|
1352
|
+
|
|
1353
|
+
expect(result.role).toBe("tool");
|
|
1354
|
+
expect(result.content).toBe("search results");
|
|
1355
|
+
expect(result.toolCallId).toBe("call_123");
|
|
1356
|
+
expect(result.name).toBe("search");
|
|
1357
|
+
});
|
|
1358
|
+
|
|
1359
|
+
it("should convert tool message with nested output format", () => {
|
|
1360
|
+
const modelMessage = {
|
|
1361
|
+
role: "tool",
|
|
1362
|
+
content: [
|
|
1363
|
+
{ type: "tool-result", toolCallId: "call_456", toolName: "bash", output: { type: "text", value: "command output" } }
|
|
1364
|
+
]
|
|
1365
|
+
};
|
|
1366
|
+
|
|
1367
|
+
const result = toLLMMessage(modelMessage);
|
|
1368
|
+
|
|
1369
|
+
expect(result.role).toBe("tool");
|
|
1370
|
+
expect(result.content).toBe("command output");
|
|
1371
|
+
});
|
|
1372
|
+
|
|
1373
|
+
it("should preserve text content when tool-call parts are present", () => {
|
|
1374
|
+
const modelMessage = {
|
|
1375
|
+
role: "assistant",
|
|
1376
|
+
content: [
|
|
1377
|
+
{ type: "reasoning", text: "I need to run a command" },
|
|
1378
|
+
{ type: "text", text: "Running the command now" },
|
|
1379
|
+
{ type: "tool-call", toolCallId: "call_789", toolName: "bash", input: { command: "ls" } }
|
|
1380
|
+
]
|
|
1381
|
+
};
|
|
1382
|
+
|
|
1383
|
+
const result = toLLMMessage(modelMessage);
|
|
1384
|
+
|
|
1385
|
+
expect(result.content).toBe("Running the command now");
|
|
1386
|
+
expect(result.toolCalls).toHaveLength(1);
|
|
1387
|
+
expect(result.toolCalls[0].name).toBe("bash");
|
|
1388
|
+
});
|
|
1389
|
+
});
|