@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,559 @@
|
|
|
1
|
+
import { describe, test, expect, beforeEach, afterEach, vi } from "bun:test";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { ToolComponent } from "./tool-component";
|
|
4
|
+
import type { Tool, ToolResult, ToolExecuteRequest, ToolContext } from "./types";
|
|
5
|
+
import { ConfigComponent } from "../../config/config-component";
|
|
6
|
+
import * as fsSync from "fs";
|
|
7
|
+
import * as path from "path";
|
|
8
|
+
import * as os from "os";
|
|
9
|
+
import type { Environment } from "../interface";
|
|
10
|
+
|
|
11
|
+
// Mock Environment
|
|
12
|
+
const createMockEnv = (configComponent?: ConfigComponent) => ({
|
|
13
|
+
name: "mock-env",
|
|
14
|
+
version: "1.0.0",
|
|
15
|
+
getConfig: () => ({ name: "mock-env", version: "1.0.0", enabled: true, env: null as any }),
|
|
16
|
+
registerComponent: () => {},
|
|
17
|
+
unregisterComponent: () => {},
|
|
18
|
+
getComponent: (name: string) => name === "config" ? configComponent : undefined,
|
|
19
|
+
hasComponent: (name: string) => name === "config" && !!configComponent,
|
|
20
|
+
listComponents: () => [],
|
|
21
|
+
handle_query: async () => "",
|
|
22
|
+
handle_action: async () => ({ success: true, output: "" }),
|
|
23
|
+
subscribe: () => () => {},
|
|
24
|
+
subscribeTo: () => () => {},
|
|
25
|
+
subscribeAll: () => () => {},
|
|
26
|
+
pushEnvEvent: () => {},
|
|
27
|
+
logger: vi.fn(),
|
|
28
|
+
trace: vi.fn(),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe("ToolComponent", () => {
|
|
32
|
+
let component: ToolComponent;
|
|
33
|
+
let mockEnv: ReturnType<typeof createMockEnv>;
|
|
34
|
+
let configComponent: ConfigComponent;
|
|
35
|
+
|
|
36
|
+
// Helper to create a mock tool
|
|
37
|
+
const createMockTool = (
|
|
38
|
+
name: string,
|
|
39
|
+
executeFn?: (args: any, ctx: ToolContext) => Promise<ToolResult>
|
|
40
|
+
): Tool => ({
|
|
41
|
+
name,
|
|
42
|
+
description: `Description for ${name}`,
|
|
43
|
+
parameters: z.object({
|
|
44
|
+
input: z.string().describe("Test input"),
|
|
45
|
+
}),
|
|
46
|
+
execute: executeFn || (async () => ({
|
|
47
|
+
success: true,
|
|
48
|
+
output: "ok",
|
|
49
|
+
metadata: { execution_time_ms: 0 },
|
|
50
|
+
})),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
component = new ToolComponent();
|
|
55
|
+
configComponent = new ConfigComponent();
|
|
56
|
+
mockEnv = createMockEnv(configComponent);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("Component lifecycle", () => {
|
|
60
|
+
test("should have correct name and version", () => {
|
|
61
|
+
expect(component.name).toBe("tool");
|
|
62
|
+
expect(component.version).toBe("1.0.0");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("should start with created status", () => {
|
|
66
|
+
expect(component.getStatus()).toBe("created");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("should initialize with tools", async () => {
|
|
70
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
71
|
+
expect(component.getStatus()).toBe("running");
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("register", () => {
|
|
76
|
+
test("should register a tool", async () => {
|
|
77
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
78
|
+
const tool = createMockTool("test-tool");
|
|
79
|
+
|
|
80
|
+
component.register(tool, "built-in");
|
|
81
|
+
|
|
82
|
+
expect(component.hasTool("test-tool")).toBe(true);
|
|
83
|
+
expect(component.getTool("test-tool")?.name).toBe("test-tool");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("should register multiple tools", async () => {
|
|
87
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
88
|
+
const tool1 = createMockTool("tool1");
|
|
89
|
+
const tool2 = createMockTool("tool2");
|
|
90
|
+
|
|
91
|
+
component.registerMany([tool1, tool2], "built-in");
|
|
92
|
+
|
|
93
|
+
expect(component.getToolCount()).toBe(2);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("should overwrite existing tool", async () => {
|
|
97
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
98
|
+
const tool1 = createMockTool("test-tool");
|
|
99
|
+
const tool2 = createMockTool("test-tool");
|
|
100
|
+
|
|
101
|
+
component.register(tool1, "built-in");
|
|
102
|
+
component.register(tool2, "dynamic");
|
|
103
|
+
|
|
104
|
+
expect(component.getTool("test-tool")?.name).toBe("test-tool");
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe("unregister", () => {
|
|
109
|
+
test("should unregister a tool", async () => {
|
|
110
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
111
|
+
const tool = createMockTool("test-tool");
|
|
112
|
+
component.register(tool, "built-in");
|
|
113
|
+
|
|
114
|
+
expect(component.unregister("test-tool")).toBe(true);
|
|
115
|
+
expect(component.hasTool("test-tool")).toBe(false);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("should return false when unregistering non-existent tool", async () => {
|
|
119
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
120
|
+
|
|
121
|
+
expect(component.unregister("non-existent")).toBe(false);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("execute", () => {
|
|
126
|
+
test("should execute a tool", async () => {
|
|
127
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
128
|
+
const tool = createMockTool("test-tool");
|
|
129
|
+
component.register(tool, "built-in");
|
|
130
|
+
|
|
131
|
+
const request: ToolExecuteRequest = {
|
|
132
|
+
name: "test-tool",
|
|
133
|
+
args: { input: "hello" },
|
|
134
|
+
context: {},
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const result = await component.execute(request);
|
|
138
|
+
|
|
139
|
+
expect(result.success).toBe(true);
|
|
140
|
+
expect(result.output).toBe("ok");
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("should return error for non-existent tool", async () => {
|
|
144
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
145
|
+
|
|
146
|
+
const request: ToolExecuteRequest = {
|
|
147
|
+
name: "non-existent",
|
|
148
|
+
args: {},
|
|
149
|
+
context: {},
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const result = await component.execute(request);
|
|
153
|
+
|
|
154
|
+
expect(result.success).toBe(false);
|
|
155
|
+
expect(result.error).toContain("not found");
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("should validate arguments before execution", async () => {
|
|
159
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
160
|
+
const tool = createMockTool("test-tool");
|
|
161
|
+
component.register(tool, "built-in");
|
|
162
|
+
|
|
163
|
+
const request: ToolExecuteRequest = {
|
|
164
|
+
name: "test-tool",
|
|
165
|
+
args: { input: 123 }, // Should be string
|
|
166
|
+
context: {},
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const result = await component.execute(request);
|
|
170
|
+
|
|
171
|
+
expect(result.success).toBe(false);
|
|
172
|
+
expect(result.error).toContain("Invalid arguments");
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("should pass context to tool", async () => {
|
|
176
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
177
|
+
let receivedCtx: ToolContext | undefined;
|
|
178
|
+
|
|
179
|
+
const tool = createMockTool("test-tool", async (_, ctx) => {
|
|
180
|
+
receivedCtx = ctx;
|
|
181
|
+
return { success: true, output: "ok" };
|
|
182
|
+
});
|
|
183
|
+
component.register(tool, "built-in");
|
|
184
|
+
|
|
185
|
+
const request: ToolExecuteRequest = {
|
|
186
|
+
name: "test-tool",
|
|
187
|
+
args: { input: "test" },
|
|
188
|
+
context: { workdir: "/tmp", session_id: "test-session" },
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
await component.execute(request);
|
|
192
|
+
|
|
193
|
+
expect(receivedCtx?.workdir).toBe("/tmp");
|
|
194
|
+
expect(receivedCtx?.session_id).toBe("test-session");
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test("should handle tool execution errors", async () => {
|
|
198
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
199
|
+
const tool: Tool = {
|
|
200
|
+
name: "error-tool",
|
|
201
|
+
description: "Tool that throws error",
|
|
202
|
+
parameters: z.object({}),
|
|
203
|
+
execute: async () => {
|
|
204
|
+
throw new Error("Execution failed");
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
component.register(tool, "built-in");
|
|
208
|
+
|
|
209
|
+
const request: ToolExecuteRequest = {
|
|
210
|
+
name: "error-tool",
|
|
211
|
+
args: {},
|
|
212
|
+
context: {},
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const result = await component.execute(request);
|
|
216
|
+
|
|
217
|
+
expect(result.success).toBe(false);
|
|
218
|
+
expect(result.error).toContain("Execution failed");
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
describe("listTools", () => {
|
|
223
|
+
test("should list all registered tools", async () => {
|
|
224
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
225
|
+
component.register(createMockTool("tool1"), "built-in");
|
|
226
|
+
component.register(createMockTool("tool2"), "built-in");
|
|
227
|
+
|
|
228
|
+
const tools = component.listTools();
|
|
229
|
+
|
|
230
|
+
expect(tools).toHaveLength(2);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test("should filter tools by category", async () => {
|
|
234
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
235
|
+
const tool1 = createMockTool("tool1");
|
|
236
|
+
tool1.metadata = { category: "system" };
|
|
237
|
+
const tool2 = createMockTool("tool2");
|
|
238
|
+
tool2.metadata = { category: "file" };
|
|
239
|
+
component.register(tool1, "built-in");
|
|
240
|
+
component.register(tool2, "built-in");
|
|
241
|
+
|
|
242
|
+
const tools = component.listTools({ category: "system" });
|
|
243
|
+
|
|
244
|
+
expect(tools).toHaveLength(1);
|
|
245
|
+
expect(tools[0].name).toBe("tool1");
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
describe("hooks", () => {
|
|
250
|
+
test("should call before.execute hooks", async () => {
|
|
251
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
252
|
+
let hookCalled = false;
|
|
253
|
+
|
|
254
|
+
// 使用 globalHookManager 注册 hook(ToolComponent.execute() 调用 globalHookManager)
|
|
255
|
+
const { globalHookManager } = await import("../hook/global-hook-manager");
|
|
256
|
+
globalHookManager.register("tool:before.execute", {
|
|
257
|
+
name: "test-before-hook",
|
|
258
|
+
priority: 0,
|
|
259
|
+
execute: async (ctx) => {
|
|
260
|
+
hookCalled = true;
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
const tool = createMockTool("test-tool");
|
|
265
|
+
component.register(tool, "built-in");
|
|
266
|
+
|
|
267
|
+
await component.execute({
|
|
268
|
+
name: "test-tool",
|
|
269
|
+
args: { input: "test" },
|
|
270
|
+
context: {},
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
expect(hookCalled).toBe(true);
|
|
274
|
+
|
|
275
|
+
// 清理
|
|
276
|
+
globalHookManager.unregister("tool:before.execute", "test-before-hook");
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
test("should call after.execute hooks", async () => {
|
|
280
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
281
|
+
let hookCalled = false;
|
|
282
|
+
|
|
283
|
+
// 使用 globalHookManager 注册 hook
|
|
284
|
+
const { globalHookManager } = await import("../hook/global-hook-manager");
|
|
285
|
+
globalHookManager.register("tool:after.execute", {
|
|
286
|
+
name: "test-after-hook",
|
|
287
|
+
priority: 0,
|
|
288
|
+
execute: async (ctx) => {
|
|
289
|
+
hookCalled = true;
|
|
290
|
+
},
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
const tool = createMockTool("test-tool");
|
|
294
|
+
component.register(tool, "built-in");
|
|
295
|
+
|
|
296
|
+
await component.execute({
|
|
297
|
+
name: "test-tool",
|
|
298
|
+
args: { input: "test" },
|
|
299
|
+
context: {},
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
expect(hookCalled).toBe(true);
|
|
303
|
+
|
|
304
|
+
// 清理
|
|
305
|
+
globalHookManager.unregister("tool:after.execute", "test-after-hook");
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
test("should skip hooks when skipHooks is true", async () => {
|
|
309
|
+
await component.init({ name: "tool", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent, config: { builtInTools: { enabled: false } } } });
|
|
310
|
+
let hookCalled = false;
|
|
311
|
+
|
|
312
|
+
// 使用 globalHookManager 注册 hook
|
|
313
|
+
const { globalHookManager } = await import("../hook/global-hook-manager");
|
|
314
|
+
globalHookManager.register("tool:before.execute", {
|
|
315
|
+
name: "test-before-hook",
|
|
316
|
+
priority: 0,
|
|
317
|
+
execute: async (ctx) => {
|
|
318
|
+
hookCalled = true;
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
const tool = createMockTool("test-tool");
|
|
323
|
+
component.register(tool, "built-in");
|
|
324
|
+
|
|
325
|
+
await component.execute({
|
|
326
|
+
name: "test-tool",
|
|
327
|
+
args: { input: "test" },
|
|
328
|
+
context: {},
|
|
329
|
+
skipHooks: true,
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
expect(hookCalled).toBe(false);
|
|
333
|
+
|
|
334
|
+
// 清理
|
|
335
|
+
globalHookManager.unregister("tool:before.execute", "test-before-hook");
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
// ============================================================================
|
|
341
|
+
// ToolComponent Config Loading Tests (TDD)
|
|
342
|
+
// ============================================================================
|
|
343
|
+
|
|
344
|
+
describe("ToolComponent Config Loading", () => {
|
|
345
|
+
let component: ToolComponent;
|
|
346
|
+
let configComponent: ConfigComponent;
|
|
347
|
+
let mockEnv: Environment;
|
|
348
|
+
let tempDir: string;
|
|
349
|
+
|
|
350
|
+
beforeEach(() => {
|
|
351
|
+
component = new ToolComponent();
|
|
352
|
+
configComponent = new ConfigComponent();
|
|
353
|
+
|
|
354
|
+
// Create temp directory for config files
|
|
355
|
+
tempDir = fsSync.mkdtempSync(path.join(os.tmpdir(), "tool-config-test-"));
|
|
356
|
+
|
|
357
|
+
// Set XDG_DATA_HOME to temp directory so ConfigComponent can find config files
|
|
358
|
+
configComponent.setXdgDataHome(tempDir);
|
|
359
|
+
|
|
360
|
+
// Mock environment
|
|
361
|
+
mockEnv = {
|
|
362
|
+
name: "test-env",
|
|
363
|
+
version: "1.0.0",
|
|
364
|
+
getComponent: vi.fn((name: string) => {
|
|
365
|
+
if (name === "config") return configComponent;
|
|
366
|
+
return undefined;
|
|
367
|
+
}),
|
|
368
|
+
hasComponent: vi.fn((name: string) => name === "config"),
|
|
369
|
+
logger: vi.fn(),
|
|
370
|
+
trace: vi.fn(),
|
|
371
|
+
} as unknown as Environment;
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
afterEach(() => {
|
|
375
|
+
vi.restoreAllMocks();
|
|
376
|
+
// Clean up environment variables
|
|
377
|
+
delete process.env.TOOL_TOOL_EXECUTION_TIMEOUT;
|
|
378
|
+
delete process.env.TOOL_EXECUTION_TIMEOUT;
|
|
379
|
+
// Clean up temp directory
|
|
380
|
+
try {
|
|
381
|
+
fsSync.rmSync(tempDir, { recursive: true });
|
|
382
|
+
} catch {}
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
describe("ToolComponentOptions", () => {
|
|
386
|
+
test("should accept configComponent only", async () => {
|
|
387
|
+
await component.init({
|
|
388
|
+
name: "tool",
|
|
389
|
+
version: "1.0.0",
|
|
390
|
+
enabled: true,
|
|
391
|
+
env: mockEnv,
|
|
392
|
+
options: { configComponent }
|
|
393
|
+
});
|
|
394
|
+
expect(component.getStatus()).toBe("running");
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
test("should accept configPath option", async () => {
|
|
398
|
+
fsSync.writeFileSync(path.join(tempDir, "config.jsonc"), JSON.stringify({
|
|
399
|
+
tool: {
|
|
400
|
+
execution: {
|
|
401
|
+
timeout: 30000
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}), "utf-8");
|
|
405
|
+
|
|
406
|
+
await component.init({
|
|
407
|
+
name: "tool",
|
|
408
|
+
version: "1.0.0",
|
|
409
|
+
enabled: true,
|
|
410
|
+
env: mockEnv,
|
|
411
|
+
options: { configComponent, configPath: "config.jsonc" }
|
|
412
|
+
});
|
|
413
|
+
expect(component.getStatus()).toBe("running");
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
test("should accept envPrefix option", async () => {
|
|
417
|
+
process.env.TOOL_TOOL_EXECUTION_TIMEOUT = "45000";
|
|
418
|
+
|
|
419
|
+
await component.init({
|
|
420
|
+
name: "tool",
|
|
421
|
+
version: "1.0.0",
|
|
422
|
+
enabled: true,
|
|
423
|
+
env: mockEnv,
|
|
424
|
+
options: { configComponent, envPrefix: "TOOL" }
|
|
425
|
+
});
|
|
426
|
+
expect(component.getStatus()).toBe("running");
|
|
427
|
+
|
|
428
|
+
delete process.env.TOOL_TOOL_EXECUTION_TIMEOUT;
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
test("should accept config option with direct object", async () => {
|
|
432
|
+
await component.init({
|
|
433
|
+
name: "tool",
|
|
434
|
+
version: "1.0.0",
|
|
435
|
+
enabled: true,
|
|
436
|
+
env: mockEnv,
|
|
437
|
+
options: {
|
|
438
|
+
configComponent,
|
|
439
|
+
config: {
|
|
440
|
+
execution: {
|
|
441
|
+
timeout: 15000
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
expect(component.getStatus()).toBe("running");
|
|
447
|
+
});
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
describe("config priority: Object > Env > File", () => {
|
|
451
|
+
test("should Object config override Env config", async () => {
|
|
452
|
+
// Set environment variable
|
|
453
|
+
process.env.TOOL_TOOL_EXECUTION_TIMEOUT = "50000";
|
|
454
|
+
|
|
455
|
+
// Create file config
|
|
456
|
+
fsSync.writeFileSync(path.join(tempDir, "config.jsonc"), JSON.stringify({
|
|
457
|
+
tool: { execution: { timeout: 100000 } }
|
|
458
|
+
}), "utf-8");
|
|
459
|
+
|
|
460
|
+
await component.init({
|
|
461
|
+
name: "tool",
|
|
462
|
+
version: "1.0.0",
|
|
463
|
+
enabled: true,
|
|
464
|
+
env: mockEnv,
|
|
465
|
+
options: {
|
|
466
|
+
configComponent,
|
|
467
|
+
configPath: "config.jsonc",
|
|
468
|
+
envPrefix: "TOOL",
|
|
469
|
+
config: { execution: { timeout: 25000 } } // Object has highest priority
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
// Object config should have highest priority
|
|
474
|
+
expect(configComponent.get("tool.execution.timeout")).toBe(25000);
|
|
475
|
+
|
|
476
|
+
delete process.env.TOOL_TOOL_EXECUTION_TIMEOUT;
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
test("should Env config override File config", async () => {
|
|
480
|
+
// Set environment variable
|
|
481
|
+
process.env.TOOL_TOOL_EXECUTION_TIMEOUT = "50000";
|
|
482
|
+
|
|
483
|
+
// Create file config
|
|
484
|
+
fsSync.writeFileSync(path.join(tempDir, "config.jsonc"), JSON.stringify({
|
|
485
|
+
tool: { execution: { timeout: 100000 } }
|
|
486
|
+
}), "utf-8");
|
|
487
|
+
|
|
488
|
+
await component.init({
|
|
489
|
+
name: "tool",
|
|
490
|
+
version: "1.0.0",
|
|
491
|
+
enabled: true,
|
|
492
|
+
env: mockEnv,
|
|
493
|
+
options: {
|
|
494
|
+
configComponent,
|
|
495
|
+
configPath: "config.jsonc",
|
|
496
|
+
envPrefix: "TOOL"
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
// Env should override file
|
|
501
|
+
expect(configComponent.get("tool.execution.timeout")).toBe("50000");
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
test("should File config be loaded when present", async () => {
|
|
505
|
+
fsSync.writeFileSync(path.join(tempDir, "config.jsonc"), JSON.stringify({
|
|
506
|
+
tool: {
|
|
507
|
+
execution: {
|
|
508
|
+
timeout: 100000,
|
|
509
|
+
maxConcurrency: 8
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}), "utf-8");
|
|
513
|
+
|
|
514
|
+
await component.init({
|
|
515
|
+
name: "tool",
|
|
516
|
+
version: "1.0.0",
|
|
517
|
+
enabled: true,
|
|
518
|
+
env: mockEnv,
|
|
519
|
+
options: {
|
|
520
|
+
configComponent,
|
|
521
|
+
configPath: "config.jsonc"
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
expect(configComponent.get("tool.execution.timeout")).toBe(100000);
|
|
526
|
+
expect(configComponent.get("tool.execution.maxConcurrency")).toBe(8);
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
test("should use default values when no config provided", async () => {
|
|
530
|
+
await component.init({
|
|
531
|
+
name: "tool",
|
|
532
|
+
version: "1.0.0",
|
|
533
|
+
enabled: true,
|
|
534
|
+
env: mockEnv,
|
|
535
|
+
options: { configComponent }
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
// Default values should be set in ConfigComponent (新机制会设置默认值)
|
|
539
|
+
expect(configComponent.get("tool.execution.timeout")).toBe(30000);
|
|
540
|
+
});
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
describe("config hot reload", () => {
|
|
544
|
+
test("should register config watcher for hot reload", async () => {
|
|
545
|
+
const watcherFn = vi.fn();
|
|
546
|
+
vi.spyOn(configComponent, "watch").mockReturnValue(watcherFn);
|
|
547
|
+
|
|
548
|
+
await component.init({
|
|
549
|
+
name: "tool",
|
|
550
|
+
version: "1.0.0",
|
|
551
|
+
enabled: true,
|
|
552
|
+
env: mockEnv,
|
|
553
|
+
options: { configComponent }
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
expect(configComponent.watch).toHaveBeenCalledWith("tool.*", expect.any(Function));
|
|
557
|
+
});
|
|
558
|
+
});
|
|
559
|
+
});
|