@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,293 @@
|
|
|
1
|
+
import { describe, test, expect, beforeEach, afterEach } from "bun:test";
|
|
2
|
+
import { LLMHooks, type LLMBeforeInvokeHook, type LLMAfterInvokeHook, type LLMStreamHook, type LLMHookContext } from "./hooks";
|
|
3
|
+
import { globalHookManager } from "../hook/global-hook-manager";
|
|
4
|
+
import type { LLMInvokeRequest, LLMInvokeResult, LLMOutput, StreamEvent } from "./types";
|
|
5
|
+
|
|
6
|
+
describe("LLMHooks", () => {
|
|
7
|
+
let hooks: LLMHooks;
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
hooks = new LLMHooks();
|
|
11
|
+
// 清理 globalHookManager 中的 hooks
|
|
12
|
+
globalHookManager.clear();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
// 清理 globalHookManager 中的 hooks
|
|
17
|
+
globalHookManager.clear();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe("constructor", () => {
|
|
21
|
+
test("should create instance with empty hooks", () => {
|
|
22
|
+
expect(hooks.getHooks()).toEqual([]);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("register", () => {
|
|
27
|
+
test("should register before invoke hook", () => {
|
|
28
|
+
const hook: LLMBeforeInvokeHook = async (context) => {
|
|
29
|
+
// 可以在此处修改 context.request
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
hooks.register("before-invoke", hook);
|
|
33
|
+
expect(hooks.getHooks()).toHaveLength(1);
|
|
34
|
+
expect(hooks.getHooks()[0].type).toBe("before-invoke");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("should register after invoke hook", () => {
|
|
38
|
+
const hook: LLMAfterInvokeHook = async (context) => {
|
|
39
|
+
// 可以在此处处理结果
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
hooks.register("after-invoke", hook);
|
|
43
|
+
expect(hooks.getHooks()).toHaveLength(1);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("should register stream hook", () => {
|
|
47
|
+
const hook: LLMStreamHook = async (context, event) => {
|
|
48
|
+
// 可以在此处处理流事件
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
hooks.register("stream", hook);
|
|
52
|
+
expect(hooks.getHooks()).toHaveLength(1);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("should register multiple hooks", () => {
|
|
56
|
+
hooks.register("before-invoke", async () => {});
|
|
57
|
+
hooks.register("after-invoke", async () => {});
|
|
58
|
+
hooks.register("stream", async () => {});
|
|
59
|
+
|
|
60
|
+
expect(hooks.getHooks()).toHaveLength(3);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("should register named hooks", () => {
|
|
64
|
+
hooks.register("before-invoke", async () => {}, "custom-before-hook");
|
|
65
|
+
hooks.register("after-invoke", async () => {}, "custom-after-hook");
|
|
66
|
+
|
|
67
|
+
const allHooks = hooks.getHooks();
|
|
68
|
+
expect(allHooks[0].name).toBe("custom-before-hook");
|
|
69
|
+
expect(allHooks[1].name).toBe("custom-after-hook");
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe("unregister", () => {
|
|
74
|
+
test("should unregister hook by reference", () => {
|
|
75
|
+
const hook = async () => {};
|
|
76
|
+
hooks.register("before-invoke", hook);
|
|
77
|
+
hooks.register("before-invoke", async () => {});
|
|
78
|
+
|
|
79
|
+
expect(hooks.getHooks()).toHaveLength(2);
|
|
80
|
+
|
|
81
|
+
hooks.unregister(hook);
|
|
82
|
+
expect(hooks.getHooks()).toHaveLength(1);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("should unregister hook by name", () => {
|
|
86
|
+
hooks.register("before-invoke", async () => {}, "hook1");
|
|
87
|
+
hooks.register("before-invoke", async () => {}, "hook2");
|
|
88
|
+
|
|
89
|
+
expect(hooks.getHooks()).toHaveLength(2);
|
|
90
|
+
|
|
91
|
+
hooks.unregisterByName("hook1");
|
|
92
|
+
expect(hooks.getHooks()).toHaveLength(1);
|
|
93
|
+
expect(hooks.getHooks()[0].name).toBe("hook2");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("should unregister all hooks of a type", () => {
|
|
97
|
+
hooks.register("before-invoke", async () => {});
|
|
98
|
+
hooks.register("after-invoke", async () => {});
|
|
99
|
+
hooks.register("stream", async () => {});
|
|
100
|
+
|
|
101
|
+
hooks.unregisterByType("before-invoke");
|
|
102
|
+
expect(hooks.getHooks()).toHaveLength(2);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe("clear", () => {
|
|
107
|
+
test("should clear all hooks", () => {
|
|
108
|
+
hooks.register("before-invoke", async () => {});
|
|
109
|
+
hooks.register("after-invoke", async () => {});
|
|
110
|
+
|
|
111
|
+
hooks.clear();
|
|
112
|
+
expect(hooks.getHooks()).toEqual([]);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("executeBeforeInvoke", () => {
|
|
117
|
+
test("should execute before-invoke hooks in order", async () => {
|
|
118
|
+
const executionOrder: string[] = [];
|
|
119
|
+
|
|
120
|
+
hooks.register("before-invoke", async () => {
|
|
121
|
+
executionOrder.push("hook1");
|
|
122
|
+
});
|
|
123
|
+
hooks.register("before-invoke", async () => {
|
|
124
|
+
executionOrder.push("hook2");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const context: LLMHookContext = {
|
|
128
|
+
request: { messages: [] },
|
|
129
|
+
providerId: "openai",
|
|
130
|
+
model: "gpt-4",
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// 使用新的直接调用 globalHookManager 的方式
|
|
134
|
+
await globalHookManager.execute("llm:before.invoke", context);
|
|
135
|
+
|
|
136
|
+
expect(executionOrder).toEqual(["hook1", "hook2"]);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test("should allow hooks to modify context", async () => {
|
|
140
|
+
hooks.register("before-invoke", async (ctx) => {
|
|
141
|
+
ctx.request.messages = [
|
|
142
|
+
{ role: "system", content: "You are helpful" },
|
|
143
|
+
...ctx.request.messages,
|
|
144
|
+
];
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const context: LLMHookContext = {
|
|
148
|
+
request: { messages: [{ role: "user", content: "Hello" }] },
|
|
149
|
+
providerId: "openai",
|
|
150
|
+
model: "gpt-4",
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
await globalHookManager.execute("llm:before.invoke", context);
|
|
154
|
+
|
|
155
|
+
expect(context.request.messages).toHaveLength(2);
|
|
156
|
+
expect(context.request.messages[0].role).toBe("system");
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("should isolate errors and continue executing remaining hooks", async () => {
|
|
160
|
+
const executionOrder: string[] = [];
|
|
161
|
+
|
|
162
|
+
hooks.register("before-invoke", async () => {
|
|
163
|
+
executionOrder.push("hook1");
|
|
164
|
+
});
|
|
165
|
+
hooks.register("before-invoke", async () => {
|
|
166
|
+
throw new Error("Hook error");
|
|
167
|
+
});
|
|
168
|
+
hooks.register("before-invoke", async () => {
|
|
169
|
+
executionOrder.push("hook3");
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
const context: LLMHookContext = {
|
|
173
|
+
request: { messages: [] },
|
|
174
|
+
providerId: "openai",
|
|
175
|
+
model: "gpt-4",
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// HookManager 会捕获错误并继续执行(错误隔离)
|
|
179
|
+
await globalHookManager.execute("llm:before.invoke", context);
|
|
180
|
+
// 错误被隔离,后续 hooks 继续执行
|
|
181
|
+
expect(executionOrder).toEqual(["hook1", "hook3"]);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
describe("executeAfterInvoke", () => {
|
|
186
|
+
test("should execute after-invoke hooks in order", async () => {
|
|
187
|
+
const executionOrder: string[] = [];
|
|
188
|
+
|
|
189
|
+
hooks.register("after-invoke", async (ctx) => {
|
|
190
|
+
executionOrder.push("hook1");
|
|
191
|
+
});
|
|
192
|
+
hooks.register("after-invoke", async (ctx) => {
|
|
193
|
+
executionOrder.push("hook2");
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const context: LLMHookContext = {
|
|
197
|
+
request: { messages: [] },
|
|
198
|
+
providerId: "openai",
|
|
199
|
+
model: "gpt-4",
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const result: LLMInvokeResult = {
|
|
203
|
+
output: { content: "test", finishReason: "stop" },
|
|
204
|
+
providerId: "openai",
|
|
205
|
+
model: "gpt-4",
|
|
206
|
+
latencyMs: 100,
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
await globalHookManager.execute("llm:after.invoke", { ...context, result });
|
|
210
|
+
|
|
211
|
+
expect(executionOrder).toEqual(["hook1", "hook2"]);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("should allow hooks to modify result", async () => {
|
|
215
|
+
hooks.register("after-invoke", async (ctx, result) => {
|
|
216
|
+
result.output.content = result.output.content + " [modified]";
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const context: LLMHookContext = {
|
|
220
|
+
request: { messages: [] },
|
|
221
|
+
providerId: "openai",
|
|
222
|
+
model: "gpt-4",
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const result: LLMInvokeResult = {
|
|
226
|
+
output: { content: "original", finishReason: "stop" },
|
|
227
|
+
providerId: "openai",
|
|
228
|
+
model: "gpt-4",
|
|
229
|
+
latencyMs: 100,
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
await globalHookManager.execute("llm:after.invoke", { ...context, result });
|
|
233
|
+
|
|
234
|
+
expect(result.output.content).toBe("original [modified]");
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
describe("executeStream", () => {
|
|
239
|
+
test("should execute stream hooks", async () => {
|
|
240
|
+
const events: StreamEvent[] = [];
|
|
241
|
+
|
|
242
|
+
hooks.register("stream", async (ctx, event) => {
|
|
243
|
+
events.push(event);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
const context: LLMHookContext = {
|
|
247
|
+
request: { messages: [] },
|
|
248
|
+
providerId: "openai",
|
|
249
|
+
model: "gpt-4",
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const event: StreamEvent = { type: "text", content: "Hello" };
|
|
253
|
+
await hooks.executeStream(context, event);
|
|
254
|
+
|
|
255
|
+
expect(events).toHaveLength(1);
|
|
256
|
+
expect(events[0]).toEqual(event);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test("should allow hooks to modify stream event", async () => {
|
|
260
|
+
hooks.register("stream", async (ctx, event) => {
|
|
261
|
+
if (event.type === "text") {
|
|
262
|
+
event.content = event.content.toUpperCase();
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
const context: LLMHookContext = {
|
|
267
|
+
request: { messages: [] },
|
|
268
|
+
providerId: "openai",
|
|
269
|
+
model: "gpt-4",
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const event: StreamEvent = { type: "text", content: "hello" };
|
|
273
|
+
await hooks.executeStream(context, event);
|
|
274
|
+
|
|
275
|
+
expect(event.content).toBe("HELLO");
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
describe("getHooksByType", () => {
|
|
280
|
+
test("should return hooks of specific type", () => {
|
|
281
|
+
hooks.register("before-invoke", async () => {});
|
|
282
|
+
hooks.register("after-invoke", async () => {});
|
|
283
|
+
hooks.register("before-invoke", async () => {});
|
|
284
|
+
hooks.register("stream", async () => {});
|
|
285
|
+
|
|
286
|
+
const beforeHooks = hooks.getHooksByType("before-invoke");
|
|
287
|
+
expect(beforeHooks).toHaveLength(2);
|
|
288
|
+
|
|
289
|
+
const afterHooks = hooks.getHooksByType("after-invoke");
|
|
290
|
+
expect(afterHooks).toHaveLength(1);
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
});
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import type { LLMInvokeRequest, LLMInvokeResult, LLMOutput, StreamEvent } from "./types";
|
|
2
|
+
import { globalHookManager, LLMHookPoints } from "../hook/global-hook-manager";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* LLM Hook 类型(兼容旧接口)
|
|
6
|
+
*/
|
|
7
|
+
export type LLMHookType = "before-invoke" | "after-invoke" | "stream";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* LLM Hook 上下文
|
|
11
|
+
*/
|
|
12
|
+
export interface LLMHookContext {
|
|
13
|
+
/** 请求 */
|
|
14
|
+
request: LLMInvokeRequest;
|
|
15
|
+
/** Provider ID */
|
|
16
|
+
providerId: string;
|
|
17
|
+
/** 模型名称 */
|
|
18
|
+
model: string;
|
|
19
|
+
/** 元数据 */
|
|
20
|
+
metadata?: Record<string, unknown>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Before Invoke Hook
|
|
25
|
+
* 在 LLM 调用之前执行,可以修改请求
|
|
26
|
+
*/
|
|
27
|
+
export type LLMBeforeInvokeHook = (context: LLMHookContext) => Promise<void>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* After Invoke Hook
|
|
31
|
+
* 在 LLM 调用之后执行,可以处理结果
|
|
32
|
+
*/
|
|
33
|
+
export type LLMAfterInvokeHook = (
|
|
34
|
+
context: LLMHookContext,
|
|
35
|
+
result: LLMInvokeResult
|
|
36
|
+
) => Promise<void>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Stream Hook
|
|
40
|
+
* 在流式调用时处理每个事件
|
|
41
|
+
*/
|
|
42
|
+
export type LLMStreamHook = (
|
|
43
|
+
context: LLMHookContext,
|
|
44
|
+
event: StreamEvent
|
|
45
|
+
) => Promise<void>;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Hook 注册项
|
|
49
|
+
*/
|
|
50
|
+
export interface LLMHookRegistration {
|
|
51
|
+
/** Hook 类型 */
|
|
52
|
+
type: LLMHookType;
|
|
53
|
+
/** Hook 名称(可选,用于标识) */
|
|
54
|
+
name?: string;
|
|
55
|
+
/** Before Invoke Hook */
|
|
56
|
+
beforeInvoke?: LLMBeforeInvokeHook;
|
|
57
|
+
/** After Invoke Hook */
|
|
58
|
+
afterInvoke?: LLMAfterInvokeHook;
|
|
59
|
+
/** Stream Hook */
|
|
60
|
+
stream?: LLMStreamHook;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* LLMHookManager
|
|
65
|
+
*
|
|
66
|
+
* 代理到 globalHookManager 的 LLM Hook 管理器
|
|
67
|
+
*
|
|
68
|
+
* @deprecated 请直接使用 globalHookManager.register() 注册 hooks
|
|
69
|
+
*/
|
|
70
|
+
export class LLMHookManager {
|
|
71
|
+
private registrations: LLMHookRegistration[] = [];
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 注册 Hook
|
|
75
|
+
*/
|
|
76
|
+
register(
|
|
77
|
+
type: LLMHookType,
|
|
78
|
+
handler: LLMBeforeInvokeHook | LLMAfterInvokeHook | LLMStreamHook,
|
|
79
|
+
name?: string
|
|
80
|
+
): void {
|
|
81
|
+
const registration: LLMHookRegistration = {
|
|
82
|
+
type,
|
|
83
|
+
name,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
switch (type) {
|
|
87
|
+
case "before-invoke":
|
|
88
|
+
registration.beforeInvoke = handler as LLMBeforeInvokeHook;
|
|
89
|
+
break;
|
|
90
|
+
case "after-invoke":
|
|
91
|
+
registration.afterInvoke = handler as LLMAfterInvokeHook;
|
|
92
|
+
break;
|
|
93
|
+
case "stream":
|
|
94
|
+
registration.stream = handler as LLMStreamHook;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
this.registrations.push(registration);
|
|
99
|
+
|
|
100
|
+
// 注册到 globalHookManager
|
|
101
|
+
this.registerToGlobal(registration);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 注册单个 registration 到 globalHookManager
|
|
106
|
+
*/
|
|
107
|
+
private registerToGlobal(reg: LLMHookRegistration): void {
|
|
108
|
+
const hookName = `llm:${reg.name || `anon-${reg.type}-${this.registrations.indexOf(reg)}`}`;
|
|
109
|
+
|
|
110
|
+
if (reg.beforeInvoke) {
|
|
111
|
+
globalHookManager.register(LLMHookPoints.BEFORE_INVOKE, {
|
|
112
|
+
name: hookName,
|
|
113
|
+
priority: 0,
|
|
114
|
+
execute: async (ctx) => {
|
|
115
|
+
await reg.beforeInvoke!(ctx.data as LLMHookContext);
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (reg.afterInvoke) {
|
|
121
|
+
globalHookManager.register(LLMHookPoints.AFTER_INVOKE, {
|
|
122
|
+
name: hookName,
|
|
123
|
+
priority: 0,
|
|
124
|
+
execute: async (ctx) => {
|
|
125
|
+
const { request, providerId, model, metadata, result } = ctx.data as LLMHookContext & { result: LLMInvokeResult };
|
|
126
|
+
await reg.afterInvoke!({ request, providerId, model, metadata }, result);
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (reg.stream) {
|
|
132
|
+
globalHookManager.register(LLMHookPoints.ON_STREAM, {
|
|
133
|
+
name: hookName,
|
|
134
|
+
priority: 0,
|
|
135
|
+
execute: async (ctx) => {
|
|
136
|
+
const { request, providerId, model, metadata, event } = ctx.data as LLMHookContext & { event: StreamEvent };
|
|
137
|
+
await reg.stream!({ request, providerId, model, metadata }, event);
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 取消注册 Hook
|
|
145
|
+
*/
|
|
146
|
+
unregister(hook: LLMBeforeInvokeHook | LLMAfterInvokeHook | LLMStreamHook): void {
|
|
147
|
+
const index = this.registrations.findIndex((h) => {
|
|
148
|
+
return h.beforeInvoke === hook || h.afterInvoke === hook || h.stream === hook;
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
if (index !== -1) {
|
|
152
|
+
const reg = this.registrations[index];
|
|
153
|
+
this.unregisterFromGlobal(reg);
|
|
154
|
+
this.registrations.splice(index, 1);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 从 globalHookManager 取消注册
|
|
160
|
+
*/
|
|
161
|
+
private unregisterFromGlobal(reg: LLMHookRegistration): void {
|
|
162
|
+
const hookName = `llm:${reg.name || `anon-${reg.type}`}`;
|
|
163
|
+
globalHookManager.unregister(LLMHookPoints.BEFORE_INVOKE, hookName);
|
|
164
|
+
globalHookManager.unregister(LLMHookPoints.AFTER_INVOKE, hookName);
|
|
165
|
+
globalHookManager.unregister(LLMHookPoints.ON_STREAM, hookName);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 按名称取消注册 Hook
|
|
170
|
+
*/
|
|
171
|
+
unregisterByName(name: string): void {
|
|
172
|
+
const hookName = `llm:${name}`;
|
|
173
|
+
globalHookManager.unregister(LLMHookPoints.BEFORE_INVOKE, hookName);
|
|
174
|
+
globalHookManager.unregister(LLMHookPoints.AFTER_INVOKE, hookName);
|
|
175
|
+
globalHookManager.unregister(LLMHookPoints.ON_STREAM, hookName);
|
|
176
|
+
this.registrations = this.registrations.filter((h) => h.name !== name);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 按类型取消注册所有 Hook
|
|
181
|
+
*/
|
|
182
|
+
unregisterByType(type: LLMHookType): void {
|
|
183
|
+
const filtered = this.registrations.filter((h) => h.type === type);
|
|
184
|
+
for (const reg of filtered) {
|
|
185
|
+
this.unregisterFromGlobal(reg);
|
|
186
|
+
}
|
|
187
|
+
this.registrations = this.registrations.filter((h) => h.type !== type);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* 清空所有 Hook
|
|
192
|
+
*/
|
|
193
|
+
clear(): void {
|
|
194
|
+
for (const reg of this.registrations) {
|
|
195
|
+
this.unregisterFromGlobal(reg);
|
|
196
|
+
}
|
|
197
|
+
this.registrations = [];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* 获取所有 Hook
|
|
202
|
+
*/
|
|
203
|
+
getHooks(): LLMHookRegistration[] {
|
|
204
|
+
return [...this.registrations];
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* 按类型获取 Hook
|
|
209
|
+
*/
|
|
210
|
+
getHooksByType(type: LLMHookType): LLMHookRegistration[] {
|
|
211
|
+
return this.registrations.filter((h) => h.type === type);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* LLMHooks
|
|
217
|
+
*
|
|
218
|
+
* 兼容旧接口,内部委托给 globalHookManager
|
|
219
|
+
*
|
|
220
|
+
* @deprecated 请直接使用 globalHookManager
|
|
221
|
+
*/
|
|
222
|
+
export class LLMHooks {
|
|
223
|
+
private hookManager = new LLMHookManager();
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* 注册 Hook
|
|
227
|
+
*/
|
|
228
|
+
register(
|
|
229
|
+
type: LLMHookType,
|
|
230
|
+
handler: LLMBeforeInvokeHook | LLMAfterInvokeHook | LLMStreamHook,
|
|
231
|
+
name?: string
|
|
232
|
+
): void {
|
|
233
|
+
this.hookManager.register(type, handler, name);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 取消注册 Hook
|
|
238
|
+
*/
|
|
239
|
+
unregister(hook: LLMBeforeInvokeHook | LLMAfterInvokeHook | LLMStreamHook): void {
|
|
240
|
+
this.hookManager.unregister(hook);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* 按名称取消注册 Hook
|
|
245
|
+
*/
|
|
246
|
+
unregisterByName(name: string): void {
|
|
247
|
+
this.hookManager.unregisterByName(name);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* 按类型取消注册所有 Hook
|
|
252
|
+
*/
|
|
253
|
+
unregisterByType(type: LLMHookType): void {
|
|
254
|
+
this.hookManager.unregisterByType(type);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* 清空所有 Hook
|
|
259
|
+
*/
|
|
260
|
+
clear(): void {
|
|
261
|
+
this.hookManager.clear();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* 获取所有 Hook
|
|
266
|
+
*/
|
|
267
|
+
getHooks(): LLMHookRegistration[] {
|
|
268
|
+
return this.hookManager.getHooks();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* 按类型获取 Hook
|
|
273
|
+
*/
|
|
274
|
+
getHooksByType(type: LLMHookType): LLMHookRegistration[] {
|
|
275
|
+
return this.hookManager.getHooksByType(type);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* 执行 Before Invoke Hooks
|
|
280
|
+
*
|
|
281
|
+
* @deprecated 请使用 globalHookManager.execute(LLMHookPoints.BEFORE_INVOKE, ...)
|
|
282
|
+
*/
|
|
283
|
+
async executeBeforeInvoke(context: LLMHookContext): Promise<void> {
|
|
284
|
+
await globalHookManager.execute(LLMHookPoints.BEFORE_INVOKE, context);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* 执行 After Invoke Hooks
|
|
289
|
+
*
|
|
290
|
+
* @deprecated 请使用 globalHookManager.execute(LLMHookPoints.AFTER_INVOKE, ...)
|
|
291
|
+
*/
|
|
292
|
+
async executeAfterInvoke(
|
|
293
|
+
context: LLMHookContext,
|
|
294
|
+
result: LLMInvokeResult
|
|
295
|
+
): Promise<void> {
|
|
296
|
+
await globalHookManager.execute(
|
|
297
|
+
LLMHookPoints.AFTER_INVOKE,
|
|
298
|
+
{ ...context, result }
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* 执行 Stream Hooks
|
|
304
|
+
*
|
|
305
|
+
* @deprecated 请使用 globalHookManager.execute(LLMHookPoints.ON_STREAM, ...)
|
|
306
|
+
*/
|
|
307
|
+
async executeStream(
|
|
308
|
+
context: LLMHookContext,
|
|
309
|
+
event: StreamEvent
|
|
310
|
+
): Promise<void> {
|
|
311
|
+
await globalHookManager.execute(
|
|
312
|
+
LLMHookPoints.ON_STREAM,
|
|
313
|
+
{ ...context, event }
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM Component
|
|
3
|
+
* 提供 LLM 调用能力
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Types
|
|
7
|
+
export {
|
|
8
|
+
type LLMMessage,
|
|
9
|
+
type LLMOutput,
|
|
10
|
+
type UsageInfo,
|
|
11
|
+
type ToolCall,
|
|
12
|
+
type ToolResult,
|
|
13
|
+
type ToolInfo,
|
|
14
|
+
type LLMConfig,
|
|
15
|
+
type LLMProvider,
|
|
16
|
+
type ProviderConfig,
|
|
17
|
+
type ProviderType,
|
|
18
|
+
type LLMInvokeRequest,
|
|
19
|
+
type LLMInvokeResult,
|
|
20
|
+
type StreamEvent,
|
|
21
|
+
} from "./types";
|
|
22
|
+
|
|
23
|
+
export {
|
|
24
|
+
LLMConfigSchema,
|
|
25
|
+
ProviderConfigSchema,
|
|
26
|
+
ProviderCapabilitiesSchema,
|
|
27
|
+
ModelLimitsSchema,
|
|
28
|
+
LLMDefaultConfigSchema,
|
|
29
|
+
} from "./types";
|
|
30
|
+
|
|
31
|
+
// Provider
|
|
32
|
+
export { ProviderManager } from "./provider";
|
|
33
|
+
|
|
34
|
+
// Transform
|
|
35
|
+
export { LLMTransform } from "./transform";
|
|
36
|
+
|
|
37
|
+
// Invoke (AI SDK integration)
|
|
38
|
+
export {
|
|
39
|
+
invoke,
|
|
40
|
+
invokeNonStream,
|
|
41
|
+
parseModelString,
|
|
42
|
+
createInvokeConfig,
|
|
43
|
+
type InvokeConfig,
|
|
44
|
+
type InvokeOptions,
|
|
45
|
+
type LLMToolContext,
|
|
46
|
+
type ConvertedUsageInfo,
|
|
47
|
+
} from "./invoke";
|
|
48
|
+
|
|
49
|
+
// Hooks
|
|
50
|
+
export {
|
|
51
|
+
type LLMHookType,
|
|
52
|
+
type LLMHookContext,
|
|
53
|
+
type LLMBeforeInvokeHook,
|
|
54
|
+
type LLMAfterInvokeHook,
|
|
55
|
+
type LLMStreamHook,
|
|
56
|
+
type LLMHookRegistration,
|
|
57
|
+
LLMHooks,
|
|
58
|
+
} from "./hooks";
|
|
59
|
+
|
|
60
|
+
// Component
|
|
61
|
+
export { LLMComponent, type LLMComponentOptions } from "./llm";
|