@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,491 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview PromptComponent Tests
|
|
3
|
+
*
|
|
4
|
+
* TDD: Write tests first, then implement
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, test, expect, beforeEach, afterEach } from "bun:test";
|
|
8
|
+
import { PromptComponent } from "./prompt-component";
|
|
9
|
+
import { ConfigComponent } from "../../config/config-component";
|
|
10
|
+
import { writeFile, rm, mkdir } from "fs/promises";
|
|
11
|
+
import { join } from "path";
|
|
12
|
+
import { vi } from "bun:test";
|
|
13
|
+
|
|
14
|
+
// Mock Environment
|
|
15
|
+
const createMockEnv = (configComponent?: ConfigComponent) => ({
|
|
16
|
+
name: "mock-env",
|
|
17
|
+
version: "1.0.0",
|
|
18
|
+
getConfig: () => ({ name: "mock-env", version: "1.0.0", enabled: true, env: null as any }),
|
|
19
|
+
registerComponent: () => {},
|
|
20
|
+
unregisterComponent: () => {},
|
|
21
|
+
getComponent: (name: string) => name === "config" ? configComponent : undefined,
|
|
22
|
+
hasComponent: (name: string) => name === "config" && !!configComponent,
|
|
23
|
+
listComponents: () => [],
|
|
24
|
+
handle_query: async () => "",
|
|
25
|
+
handle_action: async () => ({ success: true, output: "" }),
|
|
26
|
+
subscribe: () => () => {},
|
|
27
|
+
subscribeTo: () => () => {},
|
|
28
|
+
subscribeAll: () => () => {},
|
|
29
|
+
pushEnvEvent: () => {},
|
|
30
|
+
logger: vi.fn(),
|
|
31
|
+
trace: vi.fn(),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// 创建临时目录用于测试
|
|
35
|
+
const testDir = join(process.cwd(), "test-prompts-temp");
|
|
36
|
+
|
|
37
|
+
async function setupTestDir() {
|
|
38
|
+
await mkdir(testDir, { recursive: true });
|
|
39
|
+
await mkdir(join(testDir, "subdir"), { recursive: true });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function cleanupTestDir() {
|
|
43
|
+
try {
|
|
44
|
+
await rm(testDir, { recursive: true, force: true });
|
|
45
|
+
} catch {}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
describe("PromptComponent", () => {
|
|
49
|
+
let component: PromptComponent;
|
|
50
|
+
let mockEnv: ReturnType<typeof createMockEnv>;
|
|
51
|
+
let configComponent: ConfigComponent;
|
|
52
|
+
|
|
53
|
+
beforeEach(async () => {
|
|
54
|
+
component = new PromptComponent();
|
|
55
|
+
configComponent = new ConfigComponent();
|
|
56
|
+
mockEnv = createMockEnv(configComponent);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
afterEach(async () => {
|
|
60
|
+
if (component) {
|
|
61
|
+
await component.stop();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// ============================================================================
|
|
66
|
+
// Component basics
|
|
67
|
+
// ============================================================================
|
|
68
|
+
describe("Component basics", () => {
|
|
69
|
+
test("should have correct name", () => {
|
|
70
|
+
expect(component.name).toBe("prompt");
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("should have version", () => {
|
|
74
|
+
expect(component.version).toBe("1.0.0");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("should start with created status", () => {
|
|
78
|
+
expect(component.getStatus()).toBe("created");
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// ============================================================================
|
|
83
|
+
// init
|
|
84
|
+
// ============================================================================
|
|
85
|
+
describe("init", () => {
|
|
86
|
+
test("should initialize with defaults", async () => {
|
|
87
|
+
await component.init({ name: "prompt", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent } });
|
|
88
|
+
expect(component.getStatus()).toBe("running");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("should initialize with config options", async () => {
|
|
92
|
+
await component.init({
|
|
93
|
+
name: "prompt",
|
|
94
|
+
version: "1.0.0",
|
|
95
|
+
enabled: true,
|
|
96
|
+
env: mockEnv,
|
|
97
|
+
options: {
|
|
98
|
+
configComponent,
|
|
99
|
+
defaultName: "custom-default",
|
|
100
|
+
variablePrefix: "[[",
|
|
101
|
+
variableSuffix: "]]",
|
|
102
|
+
strictMode: false,
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
expect(component.getStatus()).toBe("running");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("should load built-in prompts if prompts dir exists", async () => {
|
|
109
|
+
await component.init({ name: "prompt", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent } });
|
|
110
|
+
// 应该加载内置的 default prompt
|
|
111
|
+
expect(component.has("default")).toBe(true);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("should throw error if ConfigComponent is missing", async () => {
|
|
115
|
+
const badEnv = createMockEnv(undefined);
|
|
116
|
+
const badComponent = new PromptComponent();
|
|
117
|
+
await expect(
|
|
118
|
+
badComponent.init({ name: "prompt", version: "1.0.0", enabled: true, env: badEnv, options: {} })
|
|
119
|
+
).rejects.toThrow("ConfigComponent is required");
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// ============================================================================
|
|
124
|
+
// add
|
|
125
|
+
// ============================================================================
|
|
126
|
+
describe("add", () => {
|
|
127
|
+
beforeEach(async () => {
|
|
128
|
+
await component.init({ name: "prompt", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent } });
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("should add a prompt with name and content", () => {
|
|
132
|
+
component.add("test", "Hello {{name}}!");
|
|
133
|
+
|
|
134
|
+
expect(component.has("test")).toBe(true);
|
|
135
|
+
expect(component.get("test")).toBe("Hello {{name}}!");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("should add multiple prompts", () => {
|
|
139
|
+
component.add("prompt1", "Content 1");
|
|
140
|
+
component.add("prompt2", "Content 2");
|
|
141
|
+
component.add("prompt3", "Content 3");
|
|
142
|
+
|
|
143
|
+
expect(component.size()).toBeGreaterThanOrEqual(3);
|
|
144
|
+
expect(component.list()).toContain("prompt1");
|
|
145
|
+
expect(component.list()).toContain("prompt2");
|
|
146
|
+
expect(component.list()).toContain("prompt3");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("should update existing prompt content", () => {
|
|
150
|
+
component.add("test", "Original");
|
|
151
|
+
component.add("test", "Updated");
|
|
152
|
+
|
|
153
|
+
expect(component.get("test")).toBe("Updated");
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// ============================================================================
|
|
158
|
+
// get
|
|
159
|
+
// ============================================================================
|
|
160
|
+
describe("get", () => {
|
|
161
|
+
beforeEach(async () => {
|
|
162
|
+
await component.init({ name: "prompt", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent } });
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("should return undefined for non-existent prompt", () => {
|
|
166
|
+
expect(component.get("non-existent")).toBeUndefined();
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test("should return content for existing prompt", () => {
|
|
170
|
+
component.add("test", "Test content");
|
|
171
|
+
expect(component.get("test")).toBe("Test content");
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// ============================================================================
|
|
176
|
+
// has / delete
|
|
177
|
+
// ============================================================================
|
|
178
|
+
describe("has / delete", () => {
|
|
179
|
+
beforeEach(async () => {
|
|
180
|
+
await component.init({ name: "prompt", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent } });
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test("should return true for existing prompt", () => {
|
|
184
|
+
component.add("test", "Content");
|
|
185
|
+
expect(component.has("test")).toBe(true);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test("should return false for non-existent prompt", () => {
|
|
189
|
+
expect(component.has("non-existent")).toBe(false);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test("should delete existing prompt", () => {
|
|
193
|
+
component.add("test", "Content");
|
|
194
|
+
expect(component.delete("test")).toBe(true);
|
|
195
|
+
expect(component.has("test")).toBe(false);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test("should return false when deleting non-existent prompt", () => {
|
|
199
|
+
expect(component.delete("non-existent")).toBe(false);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// ============================================================================
|
|
204
|
+
// list / size
|
|
205
|
+
// ============================================================================
|
|
206
|
+
describe("list / size", () => {
|
|
207
|
+
beforeEach(async () => {
|
|
208
|
+
await component.init({ name: "prompt", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent } });
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
test("should list all prompt names", () => {
|
|
212
|
+
component.add("a", "Content A");
|
|
213
|
+
component.add("b", "Content B");
|
|
214
|
+
|
|
215
|
+
const list = component.list();
|
|
216
|
+
expect(list).toContain("a");
|
|
217
|
+
expect(list).toContain("b");
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test("should return correct size", () => {
|
|
221
|
+
const initialSize = component.size();
|
|
222
|
+
component.add("new1", "Content");
|
|
223
|
+
component.add("new2", "Content");
|
|
224
|
+
|
|
225
|
+
expect(component.size()).toBe(initialSize + 2);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// ============================================================================
|
|
230
|
+
// getPrompt (render)
|
|
231
|
+
// ============================================================================
|
|
232
|
+
describe("getPrompt", () => {
|
|
233
|
+
beforeEach(async () => {
|
|
234
|
+
await component.init({ name: "prompt", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent } });
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test("should get prompt by name", async () => {
|
|
238
|
+
component.add("test", "Hello World");
|
|
239
|
+
const content = await component.getPrompt("test");
|
|
240
|
+
expect(content).toBe("Hello World");
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test("should render variables in prompt", async () => {
|
|
244
|
+
component.add("greeting", "Hello {{name}}!");
|
|
245
|
+
const content = await component.getPrompt("greeting", { name: "Alice" });
|
|
246
|
+
expect(content).toBe("Hello Alice!");
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test("should render multiple variables", async () => {
|
|
250
|
+
component.add("template", "{{greeting}}, {{name}}! Today is {{date}}.");
|
|
251
|
+
const content = await component.getPrompt("template", {
|
|
252
|
+
greeting: "Hello",
|
|
253
|
+
name: "Bob",
|
|
254
|
+
date: "2026-03-27",
|
|
255
|
+
});
|
|
256
|
+
expect(content).toBe("Hello, Bob! Today is 2026-03-27.");
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test("should keep undefined variables as-is", async () => {
|
|
260
|
+
component.add("template", "Hello {{name}}, your email is {{email}}");
|
|
261
|
+
const content = await component.getPrompt("template", { name: "Alice" });
|
|
262
|
+
expect(content).toBe("Hello Alice, your email is {{email}}");
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
test("should fallback to default when prompt not found", async () => {
|
|
266
|
+
// 默认的 "default" prompt 已加载,所以会返回 default 的内容
|
|
267
|
+
// 这个测试验证当请求不存在的 prompt 时,会使用 default
|
|
268
|
+
const defaultContent = await component.getPrompt("default");
|
|
269
|
+
const fallbackContent = await component.getPrompt("non-existent");
|
|
270
|
+
|
|
271
|
+
// 由于 default 已加载,fallback 应该返回 default 的内容
|
|
272
|
+
expect(fallbackContent).toBe(defaultContent);
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
// ============================================================================
|
|
277
|
+
// render
|
|
278
|
+
// ============================================================================
|
|
279
|
+
describe("render", () => {
|
|
280
|
+
beforeEach(async () => {
|
|
281
|
+
await component.init({ name: "prompt", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent } });
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
test("should render template with variables", async () => {
|
|
285
|
+
const result = await component.render("Hello {{name}}!", { name: "World" });
|
|
286
|
+
expect(result).toBe("Hello World!");
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
test("should handle empty variables", async () => {
|
|
290
|
+
const result = await component.render("Hello {{name}}!", {});
|
|
291
|
+
expect(result).toBe("Hello {{name}}!");
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test("should handle custom variable delimiters", async () => {
|
|
295
|
+
await component.init({
|
|
296
|
+
name: "prompt",
|
|
297
|
+
version: "1.0.0",
|
|
298
|
+
enabled: true,
|
|
299
|
+
env: mockEnv,
|
|
300
|
+
options: {
|
|
301
|
+
configComponent,
|
|
302
|
+
variablePrefix: "[[",
|
|
303
|
+
variableSuffix: "]]",
|
|
304
|
+
},
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
const result = await component.render("Hello [[name]]!", { name: "World" });
|
|
308
|
+
expect(result).toBe("Hello World!");
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
// ============================================================================
|
|
313
|
+
// loadFromFile
|
|
314
|
+
// ============================================================================
|
|
315
|
+
describe("loadFromFile", () => {
|
|
316
|
+
beforeEach(async () => {
|
|
317
|
+
await setupTestDir();
|
|
318
|
+
await component.init({ name: "prompt", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent } });
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
afterEach(async () => {
|
|
322
|
+
await cleanupTestDir();
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
test("should load prompt from file", async () => {
|
|
326
|
+
const filePath = join(testDir, "test.md");
|
|
327
|
+
await writeFile(filePath, "Content from file");
|
|
328
|
+
|
|
329
|
+
const result = await component.loadFromFile(filePath);
|
|
330
|
+
expect(result).toBe(true);
|
|
331
|
+
expect(component.has("test")).toBe(true);
|
|
332
|
+
expect(component.get("test")).toBe("Content from file");
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
test("should use filename as prompt name by default", async () => {
|
|
336
|
+
const filePath = join(testDir, "my-prompt.md");
|
|
337
|
+
await writeFile(filePath, "My prompt content");
|
|
338
|
+
|
|
339
|
+
await component.loadFromFile(filePath);
|
|
340
|
+
expect(component.has("my-prompt")).toBe(true);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
test("should use custom name when provided", async () => {
|
|
344
|
+
const filePath = join(testDir, "test.md");
|
|
345
|
+
await writeFile(filePath, "Content");
|
|
346
|
+
|
|
347
|
+
await component.loadFromFile(filePath, "custom-name");
|
|
348
|
+
expect(component.has("custom-name")).toBe(true);
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
test("should return false when file not found", async () => {
|
|
352
|
+
const result = await component.loadFromFile("/non/existent/file.md");
|
|
353
|
+
expect(result).toBe(false);
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
// ============================================================================
|
|
358
|
+
// loadFromDirectory
|
|
359
|
+
// ============================================================================
|
|
360
|
+
describe("loadFromDirectory", () => {
|
|
361
|
+
beforeEach(async () => {
|
|
362
|
+
await setupTestDir();
|
|
363
|
+
await component.init({ name: "prompt", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent } });
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
afterEach(async () => {
|
|
367
|
+
await cleanupTestDir();
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
test("should load all .md files from directory", async () => {
|
|
371
|
+
await writeFile(join(testDir, "prompt1.md"), "Content 1");
|
|
372
|
+
await writeFile(join(testDir, "prompt2.md"), "Content 2");
|
|
373
|
+
|
|
374
|
+
const count = await component.loadFromDirectory(testDir);
|
|
375
|
+
expect(count).toBeGreaterThanOrEqual(2);
|
|
376
|
+
expect(component.has("prompt1")).toBe(true);
|
|
377
|
+
expect(component.has("prompt2")).toBe(true);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
test("should load from subdirectory with recursive", async () => {
|
|
381
|
+
await writeFile(join(testDir, "subdir", "nested.md"), "Nested content");
|
|
382
|
+
|
|
383
|
+
const count = await component.loadFromDirectory(testDir, ".md", true);
|
|
384
|
+
expect(component.has("subdir-nested")).toBe(true);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
test("should not load from subdirectory without recursive", async () => {
|
|
388
|
+
await writeFile(join(testDir, "subdir", "nested.md"), "Nested content");
|
|
389
|
+
|
|
390
|
+
await component.loadFromDirectory(testDir, ".md", false);
|
|
391
|
+
expect(component.has("nested")).toBe(false);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
test("should filter by extension", async () => {
|
|
395
|
+
await writeFile(join(testDir, "valid.txt"), "Should not load");
|
|
396
|
+
await writeFile(join(testDir, "valid.md"), "Should load");
|
|
397
|
+
|
|
398
|
+
const count = await component.loadFromDirectory(testDir, ".md", false);
|
|
399
|
+
expect(component.has("valid")).toBe(true);
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
// ============================================================================
|
|
404
|
+
// Hooks
|
|
405
|
+
// ============================================================================
|
|
406
|
+
describe("Hooks", () => {
|
|
407
|
+
beforeEach(async () => {
|
|
408
|
+
await component.init({ name: "prompt", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent } });
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
test("should execute before.render hook", async () => {
|
|
412
|
+
let hookCalled = false;
|
|
413
|
+
|
|
414
|
+
component.addHook(
|
|
415
|
+
"prompt.before-render",
|
|
416
|
+
"test-before",
|
|
417
|
+
async (ctx) => {
|
|
418
|
+
hookCalled = true;
|
|
419
|
+
const data = ctx.data as { name: string; originalContent: string };
|
|
420
|
+
expect(data.name).toBe("test");
|
|
421
|
+
expect(data.originalContent).toBe("Original");
|
|
422
|
+
}
|
|
423
|
+
);
|
|
424
|
+
|
|
425
|
+
component.add("test", "Original");
|
|
426
|
+
await component.getPrompt("test");
|
|
427
|
+
|
|
428
|
+
expect(hookCalled).toBe(true);
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
test("should execute after.render hook", async () => {
|
|
432
|
+
let hookCalled = false;
|
|
433
|
+
|
|
434
|
+
component.addHook(
|
|
435
|
+
"prompt.after-render",
|
|
436
|
+
"test-after",
|
|
437
|
+
async (ctx) => {
|
|
438
|
+
hookCalled = true;
|
|
439
|
+
const data = ctx.data as { name: string };
|
|
440
|
+
expect(data.name).toBe("test");
|
|
441
|
+
}
|
|
442
|
+
);
|
|
443
|
+
|
|
444
|
+
component.add("test", "Original");
|
|
445
|
+
await component.getPrompt("test", { name: "Alice" });
|
|
446
|
+
|
|
447
|
+
expect(hookCalled).toBe(true);
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
test("should allow hook to modify rendered content", async () => {
|
|
451
|
+
component.addHook(
|
|
452
|
+
"prompt.after-render",
|
|
453
|
+
"modifier",
|
|
454
|
+
async (ctx) => {
|
|
455
|
+
const data = ctx.data as { renderedContent: string };
|
|
456
|
+
data.renderedContent = data.renderedContent + " [modified]";
|
|
457
|
+
}
|
|
458
|
+
);
|
|
459
|
+
|
|
460
|
+
component.add("test", "Hello");
|
|
461
|
+
const content = await component.getPrompt("test");
|
|
462
|
+
|
|
463
|
+
expect(content).toBe("Hello [modified]");
|
|
464
|
+
});
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
// ============================================================================
|
|
468
|
+
// extractVariables
|
|
469
|
+
// ============================================================================
|
|
470
|
+
describe("extractVariables", () => {
|
|
471
|
+
beforeEach(async () => {
|
|
472
|
+
await component.init({ name: "prompt", version: "1.0.0", enabled: true, env: mockEnv, options: { configComponent } });
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
test("should extract variable names from template", () => {
|
|
476
|
+
const vars = component.extractVariables("Hello {{name}}, today is {{date}}!");
|
|
477
|
+
expect(vars).toContain("name");
|
|
478
|
+
expect(vars).toContain("date");
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
test("should return unique variable names", () => {
|
|
482
|
+
const vars = component.extractVariables("{{a}} {{b}} {{a}}");
|
|
483
|
+
expect(vars).toEqual(["a", "b"]);
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
test("should return empty array for template without variables", () => {
|
|
487
|
+
const vars = component.extractVariables("No variables here");
|
|
488
|
+
expect(vars).toEqual([]);
|
|
489
|
+
});
|
|
490
|
+
});
|
|
491
|
+
});
|