@renxqoo/renx-code 0.0.3 → 0.0.5
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/README.md +58 -223
- package/bin/renx.cjs +50 -0
- package/package.json +12 -111
- package/src/App.tsx +297 -0
- package/src/agent/runtime/event-format.ts +258 -0
- package/src/agent/runtime/model-types.ts +13 -0
- package/src/agent/runtime/runtime.context-usage.test.ts +192 -0
- package/src/agent/runtime/runtime.error-handling.test.ts +235 -0
- package/src/agent/runtime/runtime.simple.test.ts +16 -0
- package/src/agent/runtime/runtime.test.ts +296 -0
- package/src/agent/runtime/runtime.ts +875 -0
- package/src/agent/runtime/runtime.usage-forwarding.test.ts +228 -0
- package/src/agent/runtime/source-modules.test.ts +38 -0
- package/src/agent/runtime/source-modules.ts +370 -0
- package/src/agent/runtime/tool-call-buffer.test.ts +65 -0
- package/src/agent/runtime/tool-call-buffer.ts +60 -0
- package/src/agent/runtime/tool-confirmation.test.ts +56 -0
- package/src/agent/runtime/tool-confirmation.ts +15 -0
- package/src/agent/runtime/types.ts +99 -0
- package/src/commands/slash-commands.test.ts +216 -0
- package/src/commands/slash-commands.ts +64 -0
- package/src/components/chat/assistant-reply.test.tsx +47 -0
- package/src/components/chat/assistant-reply.tsx +136 -0
- package/src/components/chat/assistant-segment.test.ts +99 -0
- package/src/components/chat/assistant-segment.tsx +125 -0
- package/src/components/chat/assistant-tool-group.tsx +900 -0
- package/src/components/chat/code-block.test.tsx +206 -0
- package/src/components/chat/code-block.tsx +313 -0
- package/src/components/chat/prompt-card.tsx +81 -0
- package/src/components/chat/segment-groups.test.ts +52 -0
- package/src/components/chat/segment-groups.ts +106 -0
- package/src/components/chat/turn-item.tsx +39 -0
- package/src/components/conversation-panel.tsx +43 -0
- package/src/components/file-mention-menu.tsx +77 -0
- package/src/components/file-picker-dialog.tsx +206 -0
- package/src/components/footer-hints.tsx +75 -0
- package/src/components/model-picker-dialog.tsx +248 -0
- package/src/components/prompt.tsx +233 -0
- package/src/components/slash-command-menu.tsx +65 -0
- package/src/components/tool-confirm-dialog-content.test.ts +103 -0
- package/src/components/tool-confirm-dialog-content.ts +186 -0
- package/src/components/tool-confirm-dialog.tsx +187 -0
- package/src/components/tool-display-config.ts +119 -0
- package/src/context-usage-regressions.test.ts +26 -0
- package/src/files/attachment-capabilities.test.ts +30 -0
- package/src/files/attachment-capabilities.ts +50 -0
- package/src/files/attachment-content.ts +153 -0
- package/src/files/file-mention-query.test.ts +34 -0
- package/src/files/file-mention-query.ts +32 -0
- package/src/files/prompt-display.ts +13 -0
- package/src/files/types.ts +5 -0
- package/src/files/workspace-files.ts +63 -0
- package/src/hooks/agent-event-handlers.test.ts +207 -0
- package/src/hooks/agent-event-handlers.ts +196 -0
- package/src/hooks/chat-local-replies.fixed.test.ts +119 -0
- package/src/hooks/chat-local-replies.test.ts +153 -0
- package/src/hooks/chat-local-replies.ts +63 -0
- package/src/hooks/turn-updater.test.ts +70 -0
- package/src/hooks/turn-updater.ts +166 -0
- package/src/hooks/use-agent-chat.context.test.ts +10 -0
- package/src/hooks/use-agent-chat.status.test.ts +14 -0
- package/src/hooks/use-agent-chat.test.ts +80 -0
- package/src/hooks/use-agent-chat.ts +621 -0
- package/src/hooks/use-file-mention-menu.ts +196 -0
- package/src/hooks/use-file-picker.ts +185 -0
- package/src/hooks/use-model-picker.ts +196 -0
- package/src/hooks/use-slash-command-menu.ts +154 -0
- package/src/index.tsx +55 -0
- package/src/runtime/clipboard.test.ts +43 -0
- package/src/runtime/clipboard.ts +89 -0
- package/src/runtime/exit.test.ts +177 -0
- package/src/runtime/exit.ts +98 -0
- package/src/runtime/runtime-support.test.ts +31 -0
- package/src/runtime/terminal-theme.test.ts +55 -0
- package/src/runtime/terminal-theme.ts +196 -0
- package/src/types/chat.ts +32 -0
- package/src/types/message-content.ts +48 -0
- package/src/ui/open-code-theme.ts +176 -0
- package/src/ui/opencode-markdown.ts +211 -0
- package/src/ui/theme.simple.test.ts +52 -0
- package/src/ui/theme.test.ts +151 -0
- package/src/ui/theme.ts +152 -0
- package/src/utils/time.test.ts +144 -0
- package/src/utils/time.ts +7 -0
- package/tsconfig.json +30 -0
- package/vendor/agent-root/src/agent/ENTERPRISE_ACCEPTANCE_CHECKLIST.md +95 -0
- package/vendor/agent-root/src/agent/ENTERPRISE_REALTIME.html +1345 -0
- package/vendor/agent-root/src/agent/ENTERPRISE_REALTIME.md +1353 -0
- package/vendor/agent-root/src/agent/ERROR_CONTRACT.md +60 -0
- package/vendor/agent-root/src/agent/TEST_COVERAGE_ANALYSIS.md +278 -0
- package/vendor/agent-root/src/agent/__test__/error-contract.test.ts +72 -0
- package/vendor/agent-root/src/agent/__test__/types.test.ts +137 -0
- package/vendor/agent-root/src/agent/agent/__test__/abort-runtime.test.ts +83 -0
- package/vendor/agent-root/src/agent/agent/__test__/callback-safety.test.ts +34 -0
- package/vendor/agent-root/src/agent/agent/__test__/compaction.test.ts +323 -0
- package/vendor/agent-root/src/agent/agent/__test__/concurrency.test.ts +290 -0
- package/vendor/agent-root/src/agent/agent/__test__/error-normalizer.test.ts +377 -0
- package/vendor/agent-root/src/agent/agent/__test__/error.test.ts +212 -0
- package/vendor/agent-root/src/agent/agent/__test__/fault-injection.test.ts +295 -0
- package/vendor/agent-root/src/agent/agent/__test__/index.test.ts +3607 -0
- package/vendor/agent-root/src/agent/agent/__test__/logger.test.ts +35 -0
- package/vendor/agent-root/src/agent/agent/__test__/message-utils.test.ts +517 -0
- package/vendor/agent-root/src/agent/agent/__test__/telemetry.test.ts +97 -0
- package/vendor/agent-root/src/agent/agent/__test__/timeout-budget.test.ts +479 -0
- package/vendor/agent-root/src/agent/agent/__test__/tool-call-merge.test.ts +80 -0
- package/vendor/agent-root/src/agent/agent/__test__/tool-execution-ledger.test.ts +76 -0
- package/vendor/agent-root/src/agent/agent/__test__/write-buffer.test.ts +173 -0
- package/vendor/agent-root/src/agent/agent/__test__/write-file-session.test.ts +109 -0
- package/vendor/agent-root/src/agent/agent/abort-runtime.ts +71 -0
- package/vendor/agent-root/src/agent/agent/callback-safety.ts +33 -0
- package/vendor/agent-root/src/agent/agent/compaction.ts +291 -0
- package/vendor/agent-root/src/agent/agent/concurrency.ts +103 -0
- package/vendor/agent-root/src/agent/agent/error-normalizer.ts +190 -0
- package/vendor/agent-root/src/agent/agent/error.ts +198 -0
- package/vendor/agent-root/src/agent/agent/index.ts +1772 -0
- package/vendor/agent-root/src/agent/agent/logger.ts +65 -0
- package/vendor/agent-root/src/agent/agent/message-utils.ts +101 -0
- package/vendor/agent-root/src/agent/agent/stream-events.ts +61 -0
- package/vendor/agent-root/src/agent/agent/telemetry.ts +123 -0
- package/vendor/agent-root/src/agent/agent/timeout-budget.ts +227 -0
- package/vendor/agent-root/src/agent/agent/tool-call-merge.ts +111 -0
- package/vendor/agent-root/src/agent/agent/tool-execution-ledger.ts +164 -0
- package/vendor/agent-root/src/agent/agent/write-buffer.ts +188 -0
- package/vendor/agent-root/src/agent/agent/write-file-session.ts +238 -0
- package/vendor/agent-root/src/agent/app/__test__/agent-app-service.test.ts +1053 -0
- package/vendor/agent-root/src/agent/app/__test__/minimal-agent-application.test.ts +158 -0
- package/vendor/agent-root/src/agent/app/__test__/sqlite-agent-app-store.test.ts +437 -0
- package/vendor/agent-root/src/agent/app/agent-app-service.ts +748 -0
- package/vendor/agent-root/src/agent/app/contracts.ts +109 -0
- package/vendor/agent-root/src/agent/app/index.ts +5 -0
- package/vendor/agent-root/src/agent/app/minimal-agent-application.ts +151 -0
- package/vendor/agent-root/src/agent/app/ports.ts +72 -0
- package/vendor/agent-root/src/agent/app/sqlite-agent-app-store.ts +1182 -0
- package/vendor/agent-root/src/agent/app/sqlite-client.ts +177 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/00-README.md +36 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/01-scope-and-goals.md +33 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/02-architecture-overview.md +40 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/03-domain-model-and-contracts.md +91 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/04-ports-and-interfaces.md +116 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/05-run-orchestration-and-state-machine.md +52 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/06-cli-commands-and-ux.md +53 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/07-storage-design-local.md +52 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/08-error-and-observability.md +40 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/09-security-and-policy-boundary.md +19 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/10-test-plan-and-acceptance.md +28 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/11-implementation-phases.md +26 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/12-open-questions-and-risks.md +30 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/13-sqlite-schema-fields-and-rationale.md +567 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/14-project-flow-mermaid.md +583 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/15-openclaw-style-project-blueprint.md +972 -0
- package/vendor/agent-root/src/agent/error-contract.ts +154 -0
- package/{dist/agent/prompts/system.js → vendor/agent-root/src/agent/prompts/system.ts} +47 -23
- package/vendor/agent-root/src/agent/prompts/system1.ts +208 -0
- package/vendor/agent-root/src/agent/storage/__test__/file-history-store.test.ts +98 -0
- package/vendor/agent-root/src/agent/storage/file-history-store.ts +313 -0
- package/vendor/agent-root/src/agent/storage/file-storage-config.ts +94 -0
- package/vendor/agent-root/src/agent/storage/file-system.ts +31 -0
- package/vendor/agent-root/src/agent/storage/file-write-service.ts +21 -0
- package/vendor/agent-root/src/agent/tool/__test__/base-tool.test.ts +413 -0
- package/vendor/agent-root/src/agent/tool/__test__/bash-policy.test.ts +356 -0
- package/vendor/agent-root/src/agent/tool/__test__/bash.mocked-coverage.test.ts +375 -0
- package/vendor/agent-root/src/agent/tool/__test__/bash.test.ts +372 -0
- package/vendor/agent-root/src/agent/tool/__test__/error.test.ts +108 -0
- package/vendor/agent-root/src/agent/tool/__test__/file-edit-tool.test.ts +258 -0
- package/vendor/agent-root/src/agent/tool/__test__/file-history-tools.test.ts +121 -0
- package/vendor/agent-root/src/agent/tool/__test__/file-read-tool.test.ts +210 -0
- package/vendor/agent-root/src/agent/tool/__test__/glob.test.ts +139 -0
- package/vendor/agent-root/src/agent/tool/__test__/grep.mocked-coverage.test.ts +456 -0
- package/vendor/agent-root/src/agent/tool/__test__/grep.test.ts +192 -0
- package/vendor/agent-root/src/agent/tool/__test__/lsp.test.ts +300 -0
- package/vendor/agent-root/src/agent/tool/__test__/outside-workspace-confirmation.test.ts +214 -0
- package/vendor/agent-root/src/agent/tool/__test__/path-security.test.ts +336 -0
- package/vendor/agent-root/src/agent/tool/__test__/skill-loader.test.ts +494 -0
- package/vendor/agent-root/src/agent/tool/__test__/skill-parser.test.ts +543 -0
- package/vendor/agent-root/src/agent/tool/__test__/skill-tool.test.ts +172 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-concurrency-and-version.test.ts +116 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-create-get-list-update.test.ts +267 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-create.test.ts +519 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-errors.test.ts +225 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-output-blocking.test.ts +223 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-output.test.ts +184 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-parent-abort.test.ts +287 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-real-runner-adapter.test.ts +190 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-run-lifecycle.test.ts +352 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-store-runner-branches.test.ts +395 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-store.test.ts +391 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-subagent-config-integration.test.ts +176 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-subagent-config.test.ts +68 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-tools-core-edges.test.ts +630 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-tools-runtime-edges.test.ts +732 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-types.test.ts +494 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-utils-branches.test.ts +175 -0
- package/vendor/agent-root/src/agent/tool/__test__/tool-manager.test.ts +505 -0
- package/vendor/agent-root/src/agent/tool/__test__/types.test.ts +55 -0
- package/vendor/agent-root/src/agent/tool/__test__/web-fetch.test.ts +244 -0
- package/vendor/agent-root/src/agent/tool/__test__/web-search.test.ts +290 -0
- package/vendor/agent-root/src/agent/tool/__test__/write-file.test.ts +368 -0
- package/vendor/agent-root/src/agent/tool/base-tool.ts +345 -0
- package/vendor/agent-root/src/agent/tool/bash-policy.ts +636 -0
- package/vendor/agent-root/src/agent/tool/bash.ts +688 -0
- package/vendor/agent-root/src/agent/tool/error.ts +131 -0
- package/vendor/agent-root/src/agent/tool/file-edit-tool.ts +264 -0
- package/vendor/agent-root/src/agent/tool/file-history-list.ts +103 -0
- package/vendor/agent-root/src/agent/tool/file-history-restore.ts +149 -0
- package/vendor/agent-root/src/agent/tool/file-read-tool.ts +211 -0
- package/vendor/agent-root/src/agent/tool/glob.ts +171 -0
- package/vendor/agent-root/src/agent/tool/grep.ts +496 -0
- package/vendor/agent-root/src/agent/tool/lsp.ts +481 -0
- package/vendor/agent-root/src/agent/tool/path-security.ts +117 -0
- package/vendor/agent-root/src/agent/tool/search/common.ts +153 -0
- package/vendor/agent-root/src/agent/tool/skill/index.ts +13 -0
- package/vendor/agent-root/src/agent/tool/skill/loader.ts +229 -0
- package/vendor/agent-root/src/agent/tool/skill/parser.ts +124 -0
- package/vendor/agent-root/src/agent/tool/skill/types.ts +27 -0
- package/vendor/agent-root/src/agent/tool/skill-tool.ts +143 -0
- package/vendor/agent-root/src/agent/tool/task-create.ts +186 -0
- package/vendor/agent-root/src/agent/tool/task-errors.ts +42 -0
- package/vendor/agent-root/src/agent/tool/task-get.ts +116 -0
- package/vendor/agent-root/src/agent/tool/task-graph.ts +78 -0
- package/vendor/agent-root/src/agent/tool/task-list.ts +141 -0
- package/vendor/agent-root/src/agent/tool/task-mock-runner-adapter.ts +232 -0
- package/vendor/agent-root/src/agent/tool/task-output.ts +223 -0
- package/vendor/agent-root/src/agent/tool/task-parent-abort.ts +115 -0
- package/vendor/agent-root/src/agent/tool/task-real-runner-adapter.ts +336 -0
- package/vendor/agent-root/src/agent/tool/task-runner-adapter.ts +55 -0
- package/vendor/agent-root/src/agent/tool/task-stop.ts +187 -0
- package/vendor/agent-root/src/agent/tool/task-store.ts +217 -0
- package/vendor/agent-root/src/agent/tool/task-subagent-config.ts +149 -0
- package/vendor/agent-root/src/agent/tool/task-types.ts +264 -0
- package/vendor/agent-root/src/agent/tool/task-update.ts +315 -0
- package/vendor/agent-root/src/agent/tool/task.ts +209 -0
- package/vendor/agent-root/src/agent/tool/tool-manager.ts +362 -0
- package/vendor/agent-root/src/agent/tool/tool-prompts.ts +242 -0
- package/vendor/agent-root/src/agent/tool/types.ts +116 -0
- package/vendor/agent-root/src/agent/tool/web-fetch.ts +227 -0
- package/vendor/agent-root/src/agent/tool/web-search.ts +208 -0
- package/vendor/agent-root/src/agent/tool/write-file.ts +497 -0
- package/vendor/agent-root/src/agent/types.ts +232 -0
- package/vendor/agent-root/src/agent/utils/__tests__/index.test.ts +18 -0
- package/vendor/agent-root/src/agent/utils/__tests__/message-utils.test.ts +610 -0
- package/vendor/agent-root/src/agent/utils/__tests__/message.test.ts +223 -0
- package/vendor/agent-root/src/agent/utils/__tests__/token.test.ts +42 -0
- package/vendor/agent-root/src/agent/utils/index.ts +16 -0
- package/vendor/agent-root/src/agent/utils/message.ts +171 -0
- package/vendor/agent-root/src/agent/utils/token.ts +28 -0
- package/vendor/agent-root/src/config/__tests__/load-config-to-env.test.ts +129 -0
- package/vendor/agent-root/src/config/__tests__/loader.test.ts +247 -0
- package/vendor/agent-root/src/config/__tests__/runtime.test.ts +88 -0
- package/vendor/agent-root/src/config/index.ts +54 -0
- package/vendor/agent-root/src/config/loader.ts +431 -0
- package/vendor/agent-root/src/config/paths.ts +30 -0
- package/vendor/agent-root/src/config/runtime.ts +163 -0
- package/vendor/agent-root/src/config/types.ts +70 -0
- package/vendor/agent-root/src/logger/index.ts +57 -0
- package/vendor/agent-root/src/logger/logger.ts +819 -0
- package/vendor/agent-root/src/logger/types.ts +150 -0
- package/vendor/agent-root/src/providers/__tests__/errors.test.ts +441 -0
- package/vendor/agent-root/src/providers/__tests__/index.test.ts +16 -0
- package/vendor/agent-root/src/providers/__tests__/openai-compatible.options.test.ts +318 -0
- package/vendor/agent-root/src/providers/__tests__/openai-compatible.test.ts +600 -0
- package/vendor/agent-root/src/providers/__tests__/registry.test.ts +449 -0
- package/vendor/agent-root/src/providers/__tests__/responses-adapter.test.ts +298 -0
- package/vendor/agent-root/src/providers/adapters/__tests__/anthropic.test.ts +354 -0
- package/vendor/agent-root/src/providers/adapters/__tests__/kimi.test.ts +58 -0
- package/vendor/agent-root/src/providers/adapters/__tests__/standard.test.ts +261 -0
- package/vendor/agent-root/src/providers/adapters/anthropic.ts +572 -0
- package/vendor/agent-root/src/providers/adapters/base.ts +131 -0
- package/vendor/agent-root/src/providers/adapters/kimi.ts +48 -0
- package/vendor/agent-root/src/providers/adapters/responses.ts +732 -0
- package/vendor/agent-root/src/providers/adapters/standard.ts +120 -0
- package/vendor/agent-root/src/providers/http/__tests__/client.timeout.test.ts +313 -0
- package/vendor/agent-root/src/providers/http/client.ts +289 -0
- package/vendor/agent-root/src/providers/http/stream-parser.ts +109 -0
- package/vendor/agent-root/src/providers/index.ts +76 -0
- package/vendor/agent-root/src/providers/kimi-headers.ts +177 -0
- package/vendor/agent-root/src/providers/openai-compatible.ts +387 -0
- package/vendor/agent-root/src/providers/registry/model-config.ts +230 -0
- package/vendor/agent-root/src/providers/registry/provider-factory.ts +123 -0
- package/vendor/agent-root/src/providers/registry.ts +135 -0
- package/vendor/agent-root/src/providers/types/api.ts +284 -0
- package/vendor/agent-root/src/providers/types/config.ts +58 -0
- package/vendor/agent-root/src/providers/types/errors.ts +323 -0
- package/vendor/agent-root/src/providers/types/index.ts +72 -0
- package/vendor/agent-root/src/providers/types/provider.ts +45 -0
- package/vendor/agent-root/src/providers/types/registry.ts +88 -0
- package/LICENSE +0 -21
- package/dist/App.d.ts +0 -2
- package/dist/App.d.ts.map +0 -1
- package/dist/App.js +0 -170
- package/dist/App.js.map +0 -1
- package/dist/agent/prompts/system.d.ts +0 -24
- package/dist/agent/prompts/system.d.ts.map +0 -1
- package/dist/agent/prompts/system.js.map +0 -1
- package/dist/agent/runtime/event-format.d.ts +0 -17
- package/dist/agent/runtime/event-format.d.ts.map +0 -1
- package/dist/agent/runtime/event-format.js +0 -194
- package/dist/agent/runtime/event-format.js.map +0 -1
- package/dist/agent/runtime/model-types.d.ts +0 -13
- package/dist/agent/runtime/model-types.d.ts.map +0 -1
- package/dist/agent/runtime/model-types.js +0 -1
- package/dist/agent/runtime/model-types.js.map +0 -1
- package/dist/agent/runtime/runtime.d.ts +0 -16
- package/dist/agent/runtime/runtime.d.ts.map +0 -1
- package/dist/agent/runtime/runtime.js +0 -691
- package/dist/agent/runtime/runtime.js.map +0 -1
- package/dist/agent/runtime/source-modules.d.ts +0 -176
- package/dist/agent/runtime/source-modules.d.ts.map +0 -1
- package/dist/agent/runtime/source-modules.js +0 -110
- package/dist/agent/runtime/source-modules.js.map +0 -1
- package/dist/agent/runtime/tool-call-buffer.d.ts +0 -12
- package/dist/agent/runtime/tool-call-buffer.d.ts.map +0 -1
- package/dist/agent/runtime/tool-call-buffer.js +0 -48
- package/dist/agent/runtime/tool-call-buffer.js.map +0 -1
- package/dist/agent/runtime/tool-confirmation.d.ts +0 -3
- package/dist/agent/runtime/tool-confirmation.d.ts.map +0 -1
- package/dist/agent/runtime/tool-confirmation.js +0 -9
- package/dist/agent/runtime/tool-confirmation.js.map +0 -1
- package/dist/agent/runtime/types.d.ts +0 -86
- package/dist/agent/runtime/types.d.ts.map +0 -1
- package/dist/agent/runtime/types.js +0 -1
- package/dist/agent/runtime/types.js.map +0 -1
- package/dist/cli.d.ts +0 -3
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js +0 -12
- package/dist/cli.js.map +0 -1
- package/dist/commands/slash-commands.d.ts +0 -11
- package/dist/commands/slash-commands.d.ts.map +0 -1
- package/dist/commands/slash-commands.js +0 -48
- package/dist/commands/slash-commands.js.map +0 -1
- package/dist/components/chat/assistant-reply.d.ts +0 -13
- package/dist/components/chat/assistant-reply.d.ts.map +0 -1
- package/dist/components/chat/assistant-reply.js +0 -78
- package/dist/components/chat/assistant-reply.js.map +0 -1
- package/dist/components/chat/assistant-segment.d.ts +0 -8
- package/dist/components/chat/assistant-segment.d.ts.map +0 -1
- package/dist/components/chat/assistant-segment.js +0 -54
- package/dist/components/chat/assistant-segment.js.map +0 -1
- package/dist/components/chat/assistant-tool-group.d.ts +0 -7
- package/dist/components/chat/assistant-tool-group.d.ts.map +0 -1
- package/dist/components/chat/assistant-tool-group.js +0 -695
- package/dist/components/chat/assistant-tool-group.js.map +0 -1
- package/dist/components/chat/code-block.d.ts +0 -16
- package/dist/components/chat/code-block.d.ts.map +0 -1
- package/dist/components/chat/code-block.js +0 -194
- package/dist/components/chat/code-block.js.map +0 -1
- package/dist/components/chat/prompt-card.d.ts +0 -9
- package/dist/components/chat/prompt-card.d.ts.map +0 -1
- package/dist/components/chat/prompt-card.js +0 -18
- package/dist/components/chat/prompt-card.js.map +0 -1
- package/dist/components/chat/segment-groups.d.ts +0 -24
- package/dist/components/chat/segment-groups.d.ts.map +0 -1
- package/dist/components/chat/segment-groups.js +0 -69
- package/dist/components/chat/segment-groups.js.map +0 -1
- package/dist/components/chat/turn-item.d.ts +0 -9
- package/dist/components/chat/turn-item.d.ts.map +0 -1
- package/dist/components/chat/turn-item.js +0 -11
- package/dist/components/chat/turn-item.js.map +0 -1
- package/dist/components/conversation-panel.d.ts +0 -8
- package/dist/components/conversation-panel.d.ts.map +0 -1
- package/dist/components/conversation-panel.js +0 -8
- package/dist/components/conversation-panel.js.map +0 -1
- package/dist/components/file-mention-menu.d.ts +0 -11
- package/dist/components/file-mention-menu.d.ts.map +0 -1
- package/dist/components/file-mention-menu.js +0 -15
- package/dist/components/file-mention-menu.js.map +0 -1
- package/dist/components/file-picker-dialog.d.ts +0 -21
- package/dist/components/file-picker-dialog.d.ts.map +0 -1
- package/dist/components/file-picker-dialog.js +0 -48
- package/dist/components/file-picker-dialog.js.map +0 -1
- package/dist/components/footer-hints.d.ts +0 -7
- package/dist/components/footer-hints.d.ts.map +0 -1
- package/dist/components/footer-hints.js +0 -29
- package/dist/components/footer-hints.js.map +0 -1
- package/dist/components/model-picker-dialog.d.ts +0 -20
- package/dist/components/model-picker-dialog.d.ts.map +0 -1
- package/dist/components/model-picker-dialog.js +0 -72
- package/dist/components/model-picker-dialog.js.map +0 -1
- package/dist/components/prompt.d.ts +0 -18
- package/dist/components/prompt.d.ts.map +0 -1
- package/dist/components/prompt.js +0 -96
- package/dist/components/prompt.js.map +0 -1
- package/dist/components/slash-command-menu.d.ts +0 -9
- package/dist/components/slash-command-menu.d.ts.map +0 -1
- package/dist/components/slash-command-menu.js +0 -20
- package/dist/components/slash-command-menu.js.map +0 -1
- package/dist/components/tool-confirm-dialog-content.d.ts +0 -15
- package/dist/components/tool-confirm-dialog-content.d.ts.map +0 -1
- package/dist/components/tool-confirm-dialog-content.js +0 -143
- package/dist/components/tool-confirm-dialog-content.js.map +0 -1
- package/dist/components/tool-confirm-dialog.d.ts +0 -12
- package/dist/components/tool-confirm-dialog.d.ts.map +0 -1
- package/dist/components/tool-confirm-dialog.js +0 -21
- package/dist/components/tool-confirm-dialog.js.map +0 -1
- package/dist/components/tool-display-config.d.ts +0 -11
- package/dist/components/tool-display-config.d.ts.map +0 -1
- package/dist/components/tool-display-config.js +0 -94
- package/dist/components/tool-display-config.js.map +0 -1
- package/dist/config/paths.d.ts +0 -7
- package/dist/config/paths.d.ts.map +0 -1
- package/dist/config/paths.js +0 -24
- package/dist/config/paths.js.map +0 -1
- package/dist/files/attachment-capabilities.d.ts +0 -19
- package/dist/files/attachment-capabilities.d.ts.map +0 -1
- package/dist/files/attachment-capabilities.js +0 -26
- package/dist/files/attachment-capabilities.js.map +0 -1
- package/dist/files/attachment-content.d.ts +0 -5
- package/dist/files/attachment-content.d.ts.map +0 -1
- package/dist/files/attachment-content.js +0 -117
- package/dist/files/attachment-content.js.map +0 -1
- package/dist/files/file-mention-query.d.ts +0 -9
- package/dist/files/file-mention-query.d.ts.map +0 -1
- package/dist/files/file-mention-query.js +0 -23
- package/dist/files/file-mention-query.js.map +0 -1
- package/dist/files/prompt-display.d.ts +0 -3
- package/dist/files/prompt-display.d.ts.map +0 -1
- package/dist/files/prompt-display.js +0 -11
- package/dist/files/prompt-display.js.map +0 -1
- package/dist/files/types.d.ts +0 -6
- package/dist/files/types.d.ts.map +0 -1
- package/dist/files/types.js +0 -1
- package/dist/files/types.js.map +0 -1
- package/dist/files/workspace-files.d.ts +0 -3
- package/dist/files/workspace-files.d.ts.map +0 -1
- package/dist/files/workspace-files.js +0 -48
- package/dist/files/workspace-files.js.map +0 -1
- package/dist/hooks/agent-event-handlers.d.ts +0 -11
- package/dist/hooks/agent-event-handlers.d.ts.map +0 -1
- package/dist/hooks/agent-event-handlers.js +0 -137
- package/dist/hooks/agent-event-handlers.js.map +0 -1
- package/dist/hooks/chat-local-replies.d.ts +0 -9
- package/dist/hooks/chat-local-replies.d.ts.map +0 -1
- package/dist/hooks/chat-local-replies.js +0 -54
- package/dist/hooks/chat-local-replies.js.map +0 -1
- package/dist/hooks/turn-updater.d.ts +0 -9
- package/dist/hooks/turn-updater.d.ts.map +0 -1
- package/dist/hooks/turn-updater.js +0 -103
- package/dist/hooks/turn-updater.js.map +0 -1
- package/dist/hooks/use-agent-chat.d.ts +0 -29
- package/dist/hooks/use-agent-chat.d.ts.map +0 -1
- package/dist/hooks/use-agent-chat.js +0 -455
- package/dist/hooks/use-agent-chat.js.map +0 -1
- package/dist/hooks/use-file-mention-menu.d.ts +0 -22
- package/dist/hooks/use-file-mention-menu.d.ts.map +0 -1
- package/dist/hooks/use-file-mention-menu.js +0 -137
- package/dist/hooks/use-file-mention-menu.js.map +0 -1
- package/dist/hooks/use-file-picker.d.ts +0 -21
- package/dist/hooks/use-file-picker.d.ts.map +0 -1
- package/dist/hooks/use-file-picker.js +0 -145
- package/dist/hooks/use-file-picker.js.map +0 -1
- package/dist/hooks/use-model-picker.d.ts +0 -23
- package/dist/hooks/use-model-picker.d.ts.map +0 -1
- package/dist/hooks/use-model-picker.js +0 -151
- package/dist/hooks/use-model-picker.js.map +0 -1
- package/dist/hooks/use-slash-command-menu.d.ts +0 -19
- package/dist/hooks/use-slash-command-menu.d.ts.map +0 -1
- package/dist/hooks/use-slash-command-menu.js +0 -101
- package/dist/hooks/use-slash-command-menu.js.map +0 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -6
- package/dist/index.js.map +0 -1
- package/dist/run-cli-app.d.ts +0 -2
- package/dist/run-cli-app.d.ts.map +0 -1
- package/dist/run-cli-app.js +0 -41
- package/dist/run-cli-app.js.map +0 -1
- package/dist/runtime/clipboard.d.ts +0 -10
- package/dist/runtime/clipboard.d.ts.map +0 -1
- package/dist/runtime/clipboard.js +0 -64
- package/dist/runtime/clipboard.js.map +0 -1
- package/dist/runtime/exit.d.ts +0 -7
- package/dist/runtime/exit.d.ts.map +0 -1
- package/dist/runtime/exit.js +0 -85
- package/dist/runtime/exit.js.map +0 -1
- package/dist/runtime/runtime-support.d.ts +0 -4
- package/dist/runtime/runtime-support.d.ts.map +0 -1
- package/dist/runtime/runtime-support.js +0 -19
- package/dist/runtime/runtime-support.js.map +0 -1
- package/dist/runtime/terminal-theme.d.ts +0 -25
- package/dist/runtime/terminal-theme.d.ts.map +0 -1
- package/dist/runtime/terminal-theme.js +0 -148
- package/dist/runtime/terminal-theme.js.map +0 -1
- package/dist/types/chat.d.ts +0 -29
- package/dist/types/chat.d.ts.map +0 -1
- package/dist/types/chat.js +0 -1
- package/dist/types/chat.js.map +0 -1
- package/dist/types/message-content.d.ts +0 -38
- package/dist/types/message-content.d.ts.map +0 -1
- package/dist/types/message-content.js +0 -1
- package/dist/types/message-content.js.map +0 -1
- package/dist/ui/open-code-theme.d.ts +0 -58
- package/dist/ui/open-code-theme.d.ts.map +0 -1
- package/dist/ui/open-code-theme.js +0 -113
- package/dist/ui/open-code-theme.js.map +0 -1
- package/dist/ui/opencode-markdown.d.ts +0 -7
- package/dist/ui/opencode-markdown.d.ts.map +0 -1
- package/dist/ui/opencode-markdown.js +0 -169
- package/dist/ui/opencode-markdown.js.map +0 -1
- package/dist/ui/theme.d.ts +0 -68
- package/dist/ui/theme.d.ts.map +0 -1
- package/dist/ui/theme.js +0 -80
- package/dist/ui/theme.js.map +0 -1
- package/dist/utils/time.d.ts +0 -2
- package/dist/utils/time.d.ts.map +0 -1
- package/dist/utils/time.js +0 -7
- package/dist/utils/time.js.map +0 -1
|
@@ -0,0 +1,1353 @@
|
|
|
1
|
+
# 企业级无状态 Agent 实时存储方案
|
|
2
|
+
|
|
3
|
+
## 一、问题分析
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
当前设计的问题:
|
|
7
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
8
|
+
│ │
|
|
9
|
+
│ Agent 执行 (可能 10 分钟+) │
|
|
10
|
+
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
|
|
11
|
+
│ │ Step 1 │→ │ Step 2 │→ │ Step 3 │→ │ Step N │ │
|
|
12
|
+
│ │ │ │ │ │ │ │ │ │
|
|
13
|
+
│ │ 消息在 │ │ 消息在 │ │ 消息在 │ │ 消息在 │ │
|
|
14
|
+
│ │ 内存 │ │ 内存 │ │ 内存 │ │ 内存 │ │
|
|
15
|
+
│ └─────────┘ └─────────┘ └─────────┘ └─────────┘ │
|
|
16
|
+
│ │ │
|
|
17
|
+
│ ▼ │
|
|
18
|
+
│ ❌ 只有完成时才存储 │
|
|
19
|
+
│ │
|
|
20
|
+
│ 问题: │
|
|
21
|
+
│ - Agent 崩溃 = 所有消息丢失 │
|
|
22
|
+
│ - 页面关闭 = 请求中断 │
|
|
23
|
+
│ - 无法实时查看执行进度 │
|
|
24
|
+
│ - 无法从中间步骤恢复 │
|
|
25
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## 二、解决方案概述
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
核心思路: 实时存储 + 后台执行 + 检查点恢复
|
|
34
|
+
|
|
35
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
36
|
+
│ │
|
|
37
|
+
│ 1. 实时存储 │
|
|
38
|
+
│ - 每条消息产生时立即存储 │
|
|
39
|
+
│ - 每个 Step 完成时保存检查点 │
|
|
40
|
+
│ │
|
|
41
|
+
│ 2. 后台执行 (Fire and Forget) │
|
|
42
|
+
│ - API 立即返回 executionId │
|
|
43
|
+
│ - Worker 后台执行 │
|
|
44
|
+
│ - 页面关闭不受影响 │
|
|
45
|
+
│ │
|
|
46
|
+
│ 3. 故障恢复 │
|
|
47
|
+
│ - 检查点记录执行位置 │
|
|
48
|
+
│ - 从 lastMessageId 恢复 │
|
|
49
|
+
│ │
|
|
50
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## 三、核心概念
|
|
56
|
+
|
|
57
|
+
| 概念 | 说明 |
|
|
58
|
+
|------|------|
|
|
59
|
+
| **sessionId** | 整个对话(用户和 AI 之间的会话) |
|
|
60
|
+
| **executionId** | 一次 agent.run()(用户的一次请求) |
|
|
61
|
+
| **stepIndex** | 每次循环(内部步骤) |
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
Session: sess_001 (用户整个对话)
|
|
65
|
+
│
|
|
66
|
+
└── Execution: exec_001 (用户第一次请求)
|
|
67
|
+
│
|
|
68
|
+
├── Step 1 (stepIndex=1)
|
|
69
|
+
├── Step 2 (stepIndex=2)
|
|
70
|
+
└── Step 3 (stepIndex=3)
|
|
71
|
+
│
|
|
72
|
+
└── 返回结果
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## 四、实时存储架构
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
81
|
+
│ 实时存储架构 │
|
|
82
|
+
├─────────────────────────────────────────────────────────────────┤
|
|
83
|
+
│ │
|
|
84
|
+
│ ┌───────────────────────────────────────────────────────────┐ │
|
|
85
|
+
│ │ 回调驱动 │ │
|
|
86
|
+
│ │ │ │
|
|
87
|
+
│ │ onMessage: 每条消息产生时触发 │ │
|
|
88
|
+
│ │ - Redis 实时写入 (< 1ms) │ │
|
|
89
|
+
│ │ - Kafka 异步持久化 │ │
|
|
90
|
+
│ │ - SSE 推送客户端 │ │
|
|
91
|
+
│ │ │ │
|
|
92
|
+
│ │ onCheckpoint: 每个 Step 完成时触发 │ │
|
|
93
|
+
│ │ - 保存执行位置 (stepIndex + lastMessageId) │ │
|
|
94
|
+
│ │ - 用于故障恢复 │ │
|
|
95
|
+
│ │ │ │
|
|
96
|
+
│ └───────────────────────────────────────────────────────────┘ │
|
|
97
|
+
│ │ │
|
|
98
|
+
│ ▼ │
|
|
99
|
+
│ ┌───────────────────────────────────────────────────────────┐ │
|
|
100
|
+
│ │ 存储层 │ │
|
|
101
|
+
│ │ │ │
|
|
102
|
+
│ │ ┌─────────────┐ ┌─────────────┐ │ │
|
|
103
|
+
│ │ │ Redis │ │ Kafka │ │ │
|
|
104
|
+
│ │ │ (缓存/检查点)│ │ (持久化) │ │ │
|
|
105
|
+
│ │ └─────────────┘ └─────────────┘ │ │
|
|
106
|
+
│ │ │ │
|
|
107
|
+
│ └───────────────────────────────────────────────────────────┘ │
|
|
108
|
+
│ │
|
|
109
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## 五、Agent 回调机制设计
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
// ============================================================
|
|
118
|
+
// 消息类型定义
|
|
119
|
+
// ============================================================
|
|
120
|
+
|
|
121
|
+
interface Message {
|
|
122
|
+
messageId: string;
|
|
123
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
124
|
+
content: string;
|
|
125
|
+
tool_call_id?: string;
|
|
126
|
+
tool_calls?: ToolCall[];
|
|
127
|
+
timestamp: number;
|
|
128
|
+
metadata?: Record<string, any>;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
interface ToolCall {
|
|
132
|
+
id: string;
|
|
133
|
+
type: 'function';
|
|
134
|
+
name: string;
|
|
135
|
+
arguments: string;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
interface Tool {
|
|
139
|
+
name: string;
|
|
140
|
+
description: string;
|
|
141
|
+
parameters: any;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ============================================================
|
|
145
|
+
// Agent 输入输出接口
|
|
146
|
+
// ============================================================
|
|
147
|
+
|
|
148
|
+
interface AgentInput {
|
|
149
|
+
executionId: string; // 任务 ID
|
|
150
|
+
conversationId: string; // 会话 ID
|
|
151
|
+
messages: Message[]; // Context 消息列表
|
|
152
|
+
systemPrompt?: string; // 系统提示
|
|
153
|
+
tools?: Tool[]; // 可用工具
|
|
154
|
+
config?: LLMConfig; // LLM 配置
|
|
155
|
+
maxSteps?: number; // 最大步数
|
|
156
|
+
startStep?: number; // 起始步骤 (用于恢复)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
interface AgentOutput {
|
|
160
|
+
messages: Message[]; // 包含新增消息
|
|
161
|
+
finishReason: 'stop' | 'max_steps' | 'error';
|
|
162
|
+
steps: number; // 执行的步数
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
interface LLMConfig {
|
|
166
|
+
model?: string;
|
|
167
|
+
temperature?: number;
|
|
168
|
+
maxTokens?: number;
|
|
169
|
+
topP?: number;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
interface LLMResponse {
|
|
173
|
+
message: Message;
|
|
174
|
+
toolCalls?: ToolCall[];
|
|
175
|
+
usage?: {
|
|
176
|
+
prompt_tokens: number;
|
|
177
|
+
completion_tokens: number;
|
|
178
|
+
total_tokens: number;
|
|
179
|
+
};
|
|
180
|
+
finishReason?: string;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// ============================================================
|
|
184
|
+
// 回调接口
|
|
185
|
+
// ============================================================
|
|
186
|
+
|
|
187
|
+
interface AgentCallbacks {
|
|
188
|
+
// 每产生一条新消息时回调 (实时存储)
|
|
189
|
+
onMessage: (message: Message) => void | Promise<void>;
|
|
190
|
+
|
|
191
|
+
// 每个 Step 完成时回调 (检查点)
|
|
192
|
+
onCheckpoint: (checkpoint: ExecutionCheckpoint) => void | Promise<void>;
|
|
193
|
+
|
|
194
|
+
// 进度更新回调
|
|
195
|
+
onProgress?: (progress: ExecutionProgress) => void | Promise<void>;
|
|
196
|
+
|
|
197
|
+
// 错误回调
|
|
198
|
+
onError?: (error: Error) => void | Promise<void>;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
interface ExecutionCheckpoint {
|
|
202
|
+
executionId: string;
|
|
203
|
+
stepIndex: number; // 当前步骤
|
|
204
|
+
lastMessageId: string; // 最后一条消息 ID (用于恢复)
|
|
205
|
+
lastMessageTime: number; // 时间戳
|
|
206
|
+
canResume: boolean; // 是否可恢复
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
interface ExecutionProgress {
|
|
210
|
+
executionId: string;
|
|
211
|
+
stepIndex: number;
|
|
212
|
+
currentAction: 'llm' | 'tool' | 'waiting';
|
|
213
|
+
messageCount: number;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// ============================================================
|
|
217
|
+
// LLM Provider 接口
|
|
218
|
+
// ============================================================
|
|
219
|
+
|
|
220
|
+
interface LLMProvider {
|
|
221
|
+
// 生成响应 (非流式)
|
|
222
|
+
generate(messages: Message[], config?: LLMConfig): Promise<LLMResponse>;
|
|
223
|
+
|
|
224
|
+
// 生成响应 (流式)
|
|
225
|
+
generateStream(
|
|
226
|
+
messages: Message[],
|
|
227
|
+
config?: LLMConfig
|
|
228
|
+
): AsyncGenerator<Chunk>;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
interface Chunk {
|
|
232
|
+
id: string;
|
|
233
|
+
choices: Array<{
|
|
234
|
+
index: number;
|
|
235
|
+
delta: {
|
|
236
|
+
content?: string;
|
|
237
|
+
tool_calls?: ToolCall[];
|
|
238
|
+
};
|
|
239
|
+
finish_reason?: string;
|
|
240
|
+
}>;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// ============================================================
|
|
244
|
+
// Tool Executor 接口
|
|
245
|
+
// ============================================================
|
|
246
|
+
|
|
247
|
+
interface ToolExecutor {
|
|
248
|
+
execute(toolCall: ToolCall): Promise<Message>;
|
|
249
|
+
registerTool(tool: Tool, handler: Function): void;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// ============================================================
|
|
253
|
+
// 无状态 Agent 完整实现
|
|
254
|
+
// ============================================================
|
|
255
|
+
|
|
256
|
+
class StatelessAgent {
|
|
257
|
+
private llmProvider: LLMProvider;
|
|
258
|
+
private toolExecutor: ToolExecutor;
|
|
259
|
+
|
|
260
|
+
constructor(llmProvider: LLMProvider, toolExecutor: ToolExecutor) {
|
|
261
|
+
this.llmProvider = llmProvider;
|
|
262
|
+
this.toolExecutor = toolExecutor;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async run(input: AgentInput, callbacks?: AgentCallbacks): Promise<AgentOutput> {
|
|
266
|
+
let {
|
|
267
|
+
messages,
|
|
268
|
+
maxSteps = 100,
|
|
269
|
+
startStep = 1
|
|
270
|
+
} = input;
|
|
271
|
+
|
|
272
|
+
let stepIndex = startStep - 1;
|
|
273
|
+
let finishReason: 'stop' | 'max_steps' | 'error' = 'stop';
|
|
274
|
+
|
|
275
|
+
// 主循环
|
|
276
|
+
while (stepIndex < maxSteps) {
|
|
277
|
+
stepIndex++;
|
|
278
|
+
|
|
279
|
+
try {
|
|
280
|
+
// 回调: 进度
|
|
281
|
+
callbacks?.onProgress?.({
|
|
282
|
+
executionId: input.executionId,
|
|
283
|
+
stepIndex,
|
|
284
|
+
currentAction: 'llm',
|
|
285
|
+
messageCount: messages.length
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
// 1. 调用 LLM
|
|
289
|
+
const response = await this.llmProvider.generate(messages, input.config);
|
|
290
|
+
|
|
291
|
+
// 2. 添加助手消息
|
|
292
|
+
const assistantMessage = response.message;
|
|
293
|
+
messages.push(assistantMessage);
|
|
294
|
+
|
|
295
|
+
// 回调: 新消息 (实时存储)
|
|
296
|
+
await this.safeCallback(callbacks?.onMessage, assistantMessage);
|
|
297
|
+
|
|
298
|
+
// 3. 检查工具调用
|
|
299
|
+
if (response.toolCalls && response.toolCalls.length > 0) {
|
|
300
|
+
// 回调: 进度
|
|
301
|
+
callbacks?.onProgress?.({
|
|
302
|
+
executionId: input.executionId,
|
|
303
|
+
stepIndex,
|
|
304
|
+
currentAction: 'tool',
|
|
305
|
+
messageCount: messages.length
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
// 4. 执行工具调用
|
|
309
|
+
for (const toolCall of response.toolCalls) {
|
|
310
|
+
const toolResult = await this.toolExecutor.execute(toolCall);
|
|
311
|
+
messages.push(toolResult);
|
|
312
|
+
|
|
313
|
+
// 回调: 工具结果 (实时存储)
|
|
314
|
+
await this.safeCallback(callbacks?.onMessage, toolResult);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// 回调: 检查点 (每个 Step 完成)
|
|
318
|
+
const lastMessage = messages[messages.length - 1];
|
|
319
|
+
const checkpoint: ExecutionCheckpoint = {
|
|
320
|
+
executionId: input.executionId,
|
|
321
|
+
stepIndex,
|
|
322
|
+
lastMessageId: lastMessage?.messageId || '',
|
|
323
|
+
lastMessageTime: Date.now(),
|
|
324
|
+
canResume: true
|
|
325
|
+
};
|
|
326
|
+
await this.safeCallback(callbacks?.onCheckpoint, checkpoint);
|
|
327
|
+
|
|
328
|
+
// 继续下一轮
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// 5. 无工具调用,完成
|
|
333
|
+
finishReason = 'stop';
|
|
334
|
+
break;
|
|
335
|
+
|
|
336
|
+
} catch (error) {
|
|
337
|
+
console.error(`[Agent] Step ${stepIndex} error:`, error);
|
|
338
|
+
|
|
339
|
+
// 回调: 错误
|
|
340
|
+
await this.safeCallback(callbacks?.onError, error as Error);
|
|
341
|
+
|
|
342
|
+
// 检查是否是可恢复错误
|
|
343
|
+
if (this.isRetryableError(error)) {
|
|
344
|
+
// 可以重试,继续循环
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
finishReason = 'error';
|
|
349
|
+
break;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// 检查是否是达到最大步数
|
|
354
|
+
if (stepIndex >= maxSteps) {
|
|
355
|
+
finishReason = 'max_steps';
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
return {
|
|
359
|
+
messages,
|
|
360
|
+
finishReason,
|
|
361
|
+
steps: stepIndex - startStep + 1
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// 安全执行回调 (防止回调抛出异常)
|
|
366
|
+
private async safeCallback<T>(
|
|
367
|
+
callback: ((arg: T) => void | Promise<void>) | undefined,
|
|
368
|
+
arg: T
|
|
369
|
+
): Promise<void> {
|
|
370
|
+
if (!callback) return;
|
|
371
|
+
|
|
372
|
+
try {
|
|
373
|
+
await callback(arg);
|
|
374
|
+
} catch (error) {
|
|
375
|
+
console.error('[Agent] Callback error:', error);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// 判断是否可重试
|
|
380
|
+
private isRetryableError(error: any): boolean {
|
|
381
|
+
const retryableCodes = ['RATE_LIMIT', 'TIMEOUT', 'NETWORK_ERROR'];
|
|
382
|
+
return error?.code && retryableCodes.includes(error.code);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// ============================================================
|
|
386
|
+
// 流式版本: runStream
|
|
387
|
+
// 适用于需要实时显示每个 chunk 的场景
|
|
388
|
+
// ============================================================
|
|
389
|
+
|
|
390
|
+
async *runStream(
|
|
391
|
+
input: AgentInput,
|
|
392
|
+
callbacks?: AgentCallbacks
|
|
393
|
+
): AsyncGenerator<StreamEvent, any, unknown> {
|
|
394
|
+
let {
|
|
395
|
+
messages,
|
|
396
|
+
maxSteps = 100,
|
|
397
|
+
startStep = 1
|
|
398
|
+
} = input;
|
|
399
|
+
|
|
400
|
+
let stepIndex = startStep - 1;
|
|
401
|
+
|
|
402
|
+
while (stepIndex < maxSteps) {
|
|
403
|
+
stepIndex++;
|
|
404
|
+
|
|
405
|
+
try {
|
|
406
|
+
// 回调: 进度
|
|
407
|
+
yield {
|
|
408
|
+
type: 'progress',
|
|
409
|
+
data: {
|
|
410
|
+
executionId: input.executionId,
|
|
411
|
+
stepIndex,
|
|
412
|
+
currentAction: 'llm',
|
|
413
|
+
messageCount: messages.length
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
// 1. 流式调用 LLM
|
|
418
|
+
const stream = this.llmProvider.generateStream(messages, input.config);
|
|
419
|
+
|
|
420
|
+
// 构建助手消息
|
|
421
|
+
const assistantMessage: Message = {
|
|
422
|
+
messageId: generateId('msg_'),
|
|
423
|
+
role: 'assistant',
|
|
424
|
+
content: '',
|
|
425
|
+
timestamp: Date.now()
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
let toolCalls: ToolCall[] = [];
|
|
429
|
+
|
|
430
|
+
// 2. 处理流式响应
|
|
431
|
+
for await (const chunk of stream) {
|
|
432
|
+
const delta = chunk.choices[0]?.delta;
|
|
433
|
+
|
|
434
|
+
// 更新 content
|
|
435
|
+
if (delta?.content) {
|
|
436
|
+
assistantMessage.content += delta.content;
|
|
437
|
+
|
|
438
|
+
// 回调: 内容更新
|
|
439
|
+
yield {
|
|
440
|
+
type: 'chunk',
|
|
441
|
+
data: {
|
|
442
|
+
messageId: assistantMessage.messageId,
|
|
443
|
+
content: delta.content,
|
|
444
|
+
delta: true
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// 更新 tool_calls
|
|
450
|
+
if (delta?.tool_calls) {
|
|
451
|
+
toolCalls = this.mergeToolCalls(toolCalls, delta.tool_calls);
|
|
452
|
+
|
|
453
|
+
// 回调: 工具调用
|
|
454
|
+
yield {
|
|
455
|
+
type: 'tool_call',
|
|
456
|
+
data: {
|
|
457
|
+
messageId: assistantMessage.messageId,
|
|
458
|
+
toolCalls
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// 检查完成
|
|
464
|
+
if (chunk.choices[0]?.finish_reason) {
|
|
465
|
+
break;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// 3. 完成消息构建
|
|
470
|
+
assistantMessage.tool_calls = toolCalls.length > 0 ? toolCalls : undefined;
|
|
471
|
+
messages.push(assistantMessage);
|
|
472
|
+
|
|
473
|
+
// 回调: 新消息
|
|
474
|
+
await this.safeCallback(callbacks?.onMessage, assistantMessage);
|
|
475
|
+
|
|
476
|
+
// 4. 处理工具调用
|
|
477
|
+
if (toolCalls.length > 0) {
|
|
478
|
+
for (const toolCall of toolCalls) {
|
|
479
|
+
yield {
|
|
480
|
+
type: 'progress',
|
|
481
|
+
data: {
|
|
482
|
+
stepIndex,
|
|
483
|
+
currentAction: 'tool',
|
|
484
|
+
messageCount: messages.length
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
// 执行工具
|
|
489
|
+
const toolResult = await this.toolExecutor.execute(toolCall);
|
|
490
|
+
messages.push(toolResult);
|
|
491
|
+
|
|
492
|
+
// 回调: 工具结果
|
|
493
|
+
await this.safeCallback(callbacks?.onMessage, toolResult);
|
|
494
|
+
|
|
495
|
+
// 推送工具结果
|
|
496
|
+
yield {
|
|
497
|
+
type: 'tool_result',
|
|
498
|
+
data: toolResult
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// 回调: 检查点
|
|
503
|
+
const lastMessage = messages[messages.length - 1];
|
|
504
|
+
const checkpoint: ExecutionCheckpoint = {
|
|
505
|
+
executionId: input.executionId,
|
|
506
|
+
stepIndex,
|
|
507
|
+
lastMessageId: lastMessage?.messageId || '',
|
|
508
|
+
lastMessageTime: Date.now(),
|
|
509
|
+
canResume: true
|
|
510
|
+
};
|
|
511
|
+
await this.safeCallback(callbacks?.onCheckpoint, checkpoint);
|
|
512
|
+
|
|
513
|
+
// 推送检查点
|
|
514
|
+
yield {
|
|
515
|
+
type: 'checkpoint',
|
|
516
|
+
data: checkpoint
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// 5. 无工具调用,完成
|
|
523
|
+
yield {
|
|
524
|
+
type: 'done',
|
|
525
|
+
data: {
|
|
526
|
+
finishReason: 'stop',
|
|
527
|
+
steps: stepIndex - startStep + 1
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
break;
|
|
532
|
+
|
|
533
|
+
} catch (error) {
|
|
534
|
+
console.error(`[Agent] Step ${stepIndex} error:`, error);
|
|
535
|
+
|
|
536
|
+
await this.safeCallback(callbacks?.onError, error as Error);
|
|
537
|
+
|
|
538
|
+
yield {
|
|
539
|
+
type: 'error',
|
|
540
|
+
data: { message: (error as Error).message }
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
break;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// 合并工具调用
|
|
549
|
+
private mergeToolCalls(
|
|
550
|
+
existing: ToolCall[],
|
|
551
|
+
newCalls: ToolCall[]
|
|
552
|
+
): ToolCall[] {
|
|
553
|
+
for (const newCall of newCalls) {
|
|
554
|
+
const existingCall = existing.find(c => c.id === newCall.id);
|
|
555
|
+
|
|
556
|
+
if (existingCall) {
|
|
557
|
+
existingCall.arguments += newCall.arguments;
|
|
558
|
+
} else {
|
|
559
|
+
existing.push({ ...newCall });
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return existing;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
interface StreamEvent {
|
|
567
|
+
type: 'chunk' | 'tool_call' | 'tool_result' | 'progress' | 'checkpoint' | 'done' | 'error';
|
|
568
|
+
data: any;
|
|
569
|
+
}
|
|
570
|
+
```
|
|
571
|
+
|
|
572
|
+
## 六、实时存储实现
|
|
573
|
+
|
|
574
|
+
```typescript
|
|
575
|
+
class RealTimeStorage {
|
|
576
|
+
// 保存消息到 Redis (毫秒级延迟)
|
|
577
|
+
async saveMessage(conversationId: string, message: Message): Promise<void> {
|
|
578
|
+
const key = `conversation:${conversationId}:messages`;
|
|
579
|
+
await this.redis.rpush(key, JSON.stringify(message));
|
|
580
|
+
await this.redis.expire(key, 1800); // 30 分钟过期
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
// 保存检查点 (只存位置,不重复存储消息)
|
|
584
|
+
async saveCheckpoint(checkpoint: ExecutionCheckpoint): Promise<void> {
|
|
585
|
+
const key = `execution:${checkpoint.executionId}:checkpoint`;
|
|
586
|
+
await this.redis.hset(key, {
|
|
587
|
+
stepIndex: checkpoint.stepIndex.toString(),
|
|
588
|
+
lastMessageId: checkpoint.lastMessageId,
|
|
589
|
+
lastMessageTime: checkpoint.lastMessageTime.toString(),
|
|
590
|
+
canResume: checkpoint.canResume ? '1' : '0'
|
|
591
|
+
}, { EX: 86400 });
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// 获取检查点
|
|
595
|
+
async getLatestCheckpoint(executionId: string): Promise<ExecutionCheckpoint | null> {
|
|
596
|
+
const key = `execution:${executionId}:checkpoint`;
|
|
597
|
+
const data = await this.redis.hgetall(key);
|
|
598
|
+
if (!data) return null;
|
|
599
|
+
|
|
600
|
+
return {
|
|
601
|
+
executionId,
|
|
602
|
+
stepIndex: parseInt(data.stepIndex),
|
|
603
|
+
lastMessageId: data.lastMessageId,
|
|
604
|
+
lastMessageTime: parseInt(data.lastMessageTime),
|
|
605
|
+
canResume: data.canResume === '1'
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
// 获取检查点后的消息 (用于恢复)
|
|
610
|
+
async getMessagesAfterCheckpoint(
|
|
611
|
+
conversationId: string,
|
|
612
|
+
lastMessageId: string
|
|
613
|
+
): Promise<Message[]> {
|
|
614
|
+
const allMessages = await this.redis.lrange(
|
|
615
|
+
`conversation:${conversationId}:messages`, 0, -1
|
|
616
|
+
);
|
|
617
|
+
|
|
618
|
+
const messages: Message[] = [];
|
|
619
|
+
let found = false;
|
|
620
|
+
|
|
621
|
+
for (const msgStr of allMessages) {
|
|
622
|
+
const msg = JSON.parse(msgStr);
|
|
623
|
+
if (msg.messageId === lastMessageId) {
|
|
624
|
+
found = true;
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
if (found) {
|
|
628
|
+
messages.push(msg);
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
return messages;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
```
|
|
636
|
+
|
|
637
|
+
---
|
|
638
|
+
|
|
639
|
+
## 七、后台执行机制
|
|
640
|
+
|
|
641
|
+
### 问题
|
|
642
|
+
|
|
643
|
+
```
|
|
644
|
+
传统模式 (阻塞):
|
|
645
|
+
用户 ──▶ API ──▶ Agent 执行 ──▶ 等待完成 ──▶ 返回结果
|
|
646
|
+
│
|
|
647
|
+
└─ 页面关闭 = 请求中断 ❌
|
|
648
|
+
|
|
649
|
+
后台模式 (非阻塞):
|
|
650
|
+
用户 ──▶ API ──▶ 创建 Task ──▶ 返回 Task ID ──▶ 立即返回 ✓
|
|
651
|
+
│
|
|
652
|
+
▼
|
|
653
|
+
┌─────────┐
|
|
654
|
+
│ Task │ 后台执行
|
|
655
|
+
│ Queue │◀── 页面关闭不受影响
|
|
656
|
+
└─────────┘
|
|
657
|
+
│
|
|
658
|
+
▼
|
|
659
|
+
存储结果 ── 用户可查询
|
|
660
|
+
```
|
|
661
|
+
|
|
662
|
+
### 执行流程
|
|
663
|
+
|
|
664
|
+
```
|
|
665
|
+
1. 用户发起请求
|
|
666
|
+
POST /api/v1/executions
|
|
667
|
+
{ conversationId, message: "帮我写排序算法" }
|
|
668
|
+
|
|
669
|
+
Response: { executionId: "exec_001", status: "CREATED" } ← 立即返回!
|
|
670
|
+
|
|
671
|
+
2. 创建任务并放入队列
|
|
672
|
+
TaskQueue.push({ executionId, message, createdAt })
|
|
673
|
+
更新状态: CREATED → QUEUED
|
|
674
|
+
|
|
675
|
+
3. Worker 消费任务
|
|
676
|
+
- 更新状态: QUEUED → RUNNING
|
|
677
|
+
- 执行 Agent
|
|
678
|
+
- onMessage: 实时存储 + SSE 推送
|
|
679
|
+
- onCheckpoint: 保存检查点
|
|
680
|
+
|
|
681
|
+
4. 执行完成
|
|
682
|
+
更新状态: RUNNING → COMPLETED
|
|
683
|
+
保存最终结果
|
|
684
|
+
|
|
685
|
+
5. 用户查询结果
|
|
686
|
+
GET /api/v1/executions/exec_001
|
|
687
|
+
{ status: "COMPLETED", messages: [...] }
|
|
688
|
+
```
|
|
689
|
+
|
|
690
|
+
### API 设计
|
|
691
|
+
|
|
692
|
+
```typescript
|
|
693
|
+
// 创建执行 (立即返回)
|
|
694
|
+
POST /api/v1/executions
|
|
695
|
+
Body: { conversationId, message }
|
|
696
|
+
Response: { executionId, status: 'CREATED' }
|
|
697
|
+
|
|
698
|
+
// 查询执行状态和结果
|
|
699
|
+
GET /api/v1/executions/{executionId}
|
|
700
|
+
Response: {
|
|
701
|
+
executionId, conversationId,
|
|
702
|
+
status: 'CREATED' | 'QUEUED' | 'RUNNING' | 'COMPLETED' | 'FAILED',
|
|
703
|
+
stepIndex?, result?, messages?, error?
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
// 实时消息流 (SSE)
|
|
707
|
+
GET /api/v1/executions/{executionId}/stream
|
|
708
|
+
|
|
709
|
+
// 恢复执行 (崩溃后)
|
|
710
|
+
POST /api/v1/executions/{executionId}/resume
|
|
711
|
+
```
|
|
712
|
+
|
|
713
|
+
### Worker 实现
|
|
714
|
+
|
|
715
|
+
```typescript
|
|
716
|
+
// ============================================================
|
|
717
|
+
// 1. 依赖注入配置
|
|
718
|
+
// ============================================================
|
|
719
|
+
|
|
720
|
+
// 创建依赖容器
|
|
721
|
+
class DIContainer {
|
|
722
|
+
: private services Map<string, any> = new Map();
|
|
723
|
+
|
|
724
|
+
// 注册服务
|
|
725
|
+
register<T>(name: string, instance: T): void {
|
|
726
|
+
this.services.set(name, instance);
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
// 获取服务
|
|
730
|
+
get<T>(name: string): T {
|
|
731
|
+
return this.services.get(name);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// 初始化容器
|
|
736
|
+
const container = new DIContainer();
|
|
737
|
+
|
|
738
|
+
// 注册 Redis 客户端
|
|
739
|
+
const redis = new Redis({ host: 'localhost', port: 6379 });
|
|
740
|
+
container.register('redis', redis);
|
|
741
|
+
|
|
742
|
+
// 注册消息队列
|
|
743
|
+
const taskQueue = new TaskQueue(redis);
|
|
744
|
+
container.register('taskQueue', taskQueue);
|
|
745
|
+
|
|
746
|
+
// 注册存储服务
|
|
747
|
+
const messageStorage = new MessageStorage(redis, kafka);
|
|
748
|
+
container.register('messageStorage', messageStorage);
|
|
749
|
+
|
|
750
|
+
const checkpointService = new CheckpointService(redis);
|
|
751
|
+
container.register('checkpointService', checkpointService);
|
|
752
|
+
|
|
753
|
+
const executionService = new ExecutionService(redis);
|
|
754
|
+
container.register('executionService', executionService);
|
|
755
|
+
|
|
756
|
+
// 注册 Context 服务
|
|
757
|
+
const contextService = new ContextService(redis, clickhouse);
|
|
758
|
+
container.register('contextService', contextService);
|
|
759
|
+
|
|
760
|
+
// 注册 SSE 发布器
|
|
761
|
+
const ssePublisher = new SSEPublisher();
|
|
762
|
+
container.register('ssePublisher', ssePublisher);
|
|
763
|
+
|
|
764
|
+
// 注册 LLM 提供商
|
|
765
|
+
const llmProvider = new OpenAIProvider({ apiKey: process.env.OPENAI_KEY });
|
|
766
|
+
container.register('llmProvider', llmProvider);
|
|
767
|
+
|
|
768
|
+
// 注册工具执行器
|
|
769
|
+
const toolExecutor = new ToolExecutor();
|
|
770
|
+
container.register('toolExecutor', toolExecutor);
|
|
771
|
+
|
|
772
|
+
// 创建无状态 Agent
|
|
773
|
+
const agent = new StatelessAgent(llmProvider, toolExecutor);
|
|
774
|
+
container.register('agent', agent);
|
|
775
|
+
|
|
776
|
+
// ============================================================
|
|
777
|
+
// 2. ExecutionWorker 完整实现
|
|
778
|
+
// ============================================================
|
|
779
|
+
|
|
780
|
+
class ExecutionWorker {
|
|
781
|
+
private queue: TaskQueue;
|
|
782
|
+
private agent: StatelessAgent;
|
|
783
|
+
private executionService: ExecutionService;
|
|
784
|
+
private messageStorage: MessageStorage;
|
|
785
|
+
private checkpointService: CheckpointService;
|
|
786
|
+
private contextService: ContextService;
|
|
787
|
+
private ssePublisher: SSEPublisher;
|
|
788
|
+
|
|
789
|
+
// 构造函数注入依赖
|
|
790
|
+
constructor(
|
|
791
|
+
queue: TaskQueue,
|
|
792
|
+
agent: StatelessAgent,
|
|
793
|
+
executionService: ExecutionService,
|
|
794
|
+
messageStorage: MessageStorage,
|
|
795
|
+
checkpointService: CheckpointService,
|
|
796
|
+
contextService: ContextService,
|
|
797
|
+
ssePublisher: SSEPublisher
|
|
798
|
+
) {
|
|
799
|
+
this.queue = queue;
|
|
800
|
+
this.agent = agent;
|
|
801
|
+
this.executionService = executionService;
|
|
802
|
+
this.messageStorage = messageStorage;
|
|
803
|
+
this.checkpointService = checkpointService;
|
|
804
|
+
this.contextService = contextService;
|
|
805
|
+
this.ssePublisher = ssePublisher;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
// 启动 Worker
|
|
809
|
+
async start(): Promise<void> {
|
|
810
|
+
console.log('[Worker] Starting...');
|
|
811
|
+
|
|
812
|
+
while (true) {
|
|
813
|
+
try {
|
|
814
|
+
// 从队列获取任务 (阻塞等待)
|
|
815
|
+
// key: 队列名称, timeout: 超时时间(0=无限等待)
|
|
816
|
+
const task = await this.queue.brpop('task_queue', 0);
|
|
817
|
+
|
|
818
|
+
if (task) {
|
|
819
|
+
console.log(`[Worker] Received task: ${task.executionId}`);
|
|
820
|
+
await this.processTask(task);
|
|
821
|
+
}
|
|
822
|
+
} catch (error) {
|
|
823
|
+
console.error('[Worker] Error:', error);
|
|
824
|
+
// 短暂等待后继续
|
|
825
|
+
await this.sleep(1000);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
// 处理单个任务
|
|
831
|
+
async processTask(task: Task): Promise<void> {
|
|
832
|
+
const { executionId, conversationId, message } = task;
|
|
833
|
+
|
|
834
|
+
console.log(`[Worker] Processing task: ${executionId}`);
|
|
835
|
+
|
|
836
|
+
// 1. 尝试获取锁 (防止重复执行)
|
|
837
|
+
const lockAcquired = await this.executionService.acquireLock(executionId, 'worker-1');
|
|
838
|
+
if (!lockAcquired) {
|
|
839
|
+
console.log(`[Worker] Task ${executionId} is being processed by another worker`);
|
|
840
|
+
return;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
try {
|
|
844
|
+
// 2. 更新状态为 RUNNING
|
|
845
|
+
await this.executionService.updateStatus(executionId, 'RUNNING');
|
|
846
|
+
|
|
847
|
+
// 3. 检查是否有未完成的检查点
|
|
848
|
+
const checkpoint = await this.checkpointService.getLatestCheckpoint(executionId);
|
|
849
|
+
|
|
850
|
+
let messages: Message[] = [];
|
|
851
|
+
let startStep = 1;
|
|
852
|
+
|
|
853
|
+
if (checkpoint && checkpoint.canResume) {
|
|
854
|
+
// 4a. 恢复执行
|
|
855
|
+
console.log(`[Worker] Resuming from checkpoint: step ${checkpoint.stepIndex}`);
|
|
856
|
+
|
|
857
|
+
// 加载上下文
|
|
858
|
+
const context = await this.contextService.load(conversationId);
|
|
859
|
+
|
|
860
|
+
// 获取检查点后的消息
|
|
861
|
+
const newMessages = await this.messageStorage.getMessagesAfterCheckpoint(
|
|
862
|
+
conversationId,
|
|
863
|
+
checkpoint.lastMessageId
|
|
864
|
+
);
|
|
865
|
+
|
|
866
|
+
messages = [...context.messages, ...newMessages];
|
|
867
|
+
startStep = checkpoint.stepIndex + 1;
|
|
868
|
+
|
|
869
|
+
} else {
|
|
870
|
+
// 4b. 全新执行
|
|
871
|
+
console.log(`[Worker] Starting new execution`);
|
|
872
|
+
|
|
873
|
+
// 加载上下文
|
|
874
|
+
const context = await this.contextService.load(conversationId);
|
|
875
|
+
|
|
876
|
+
// 添加用户消息
|
|
877
|
+
const userMessage: Message = {
|
|
878
|
+
messageId: generateId('msg_'),
|
|
879
|
+
role: 'user',
|
|
880
|
+
content: message.content,
|
|
881
|
+
timestamp: Date.now()
|
|
882
|
+
};
|
|
883
|
+
|
|
884
|
+
messages = [...context.messages, userMessage];
|
|
885
|
+
|
|
886
|
+
// 实时存储用户消息
|
|
887
|
+
await this.messageStorage.save(conversationId, userMessage);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// 5. 获取系统提示和工具配置
|
|
891
|
+
const context = await this.contextService.load(conversationId);
|
|
892
|
+
|
|
893
|
+
// 6. 构造 Agent 输入
|
|
894
|
+
const input: AgentInput = {
|
|
895
|
+
executionId,
|
|
896
|
+
conversationId,
|
|
897
|
+
messages,
|
|
898
|
+
systemPrompt: context.systemPrompt,
|
|
899
|
+
tools: context.tools,
|
|
900
|
+
startStep, // 从指定步骤开始
|
|
901
|
+
callbacks: {
|
|
902
|
+
// 实时消息回调
|
|
903
|
+
onMessage: async (msg: Message) => {
|
|
904
|
+
// 存储消息到 Redis + Kafka
|
|
905
|
+
await this.messageStorage.save(conversationId, msg);
|
|
906
|
+
|
|
907
|
+
// SSE 推送给在线用户
|
|
908
|
+
this.ssePublisher.publish(executionId, {
|
|
909
|
+
type: 'message',
|
|
910
|
+
data: msg
|
|
911
|
+
});
|
|
912
|
+
|
|
913
|
+
console.log(`[Worker] Message saved: ${msg.messageId}`);
|
|
914
|
+
},
|
|
915
|
+
|
|
916
|
+
// 检查点回调
|
|
917
|
+
onCheckpoint: async (checkpoint: ExecutionCheckpoint) => {
|
|
918
|
+
// 保存检查点
|
|
919
|
+
await this.checkpointService.saveCheckpoint(checkpoint);
|
|
920
|
+
|
|
921
|
+
// 更新执行进度
|
|
922
|
+
await this.executionService.updateProgress(executionId, {
|
|
923
|
+
stepIndex: checkpoint.stepIndex,
|
|
924
|
+
lastMessageId: checkpoint.lastMessageId
|
|
925
|
+
});
|
|
926
|
+
|
|
927
|
+
console.log(`[Worker] Checkpoint saved: step ${checkpoint.stepIndex}`);
|
|
928
|
+
},
|
|
929
|
+
|
|
930
|
+
// 进度回调
|
|
931
|
+
onProgress: async (progress: ExecutionProgress) => {
|
|
932
|
+
// 更新进度
|
|
933
|
+
await this.executionService.updateProgress(executionId, {
|
|
934
|
+
currentAction: progress.currentAction,
|
|
935
|
+
stepIndex: progress.stepIndex,
|
|
936
|
+
messageCount: progress.messageCount
|
|
937
|
+
});
|
|
938
|
+
|
|
939
|
+
// SSE 推送进度
|
|
940
|
+
this.ssePublisher.publish(executionId, {
|
|
941
|
+
type: 'progress',
|
|
942
|
+
data: progress
|
|
943
|
+
});
|
|
944
|
+
},
|
|
945
|
+
|
|
946
|
+
// 错误回调
|
|
947
|
+
onError: async (error: Error) => {
|
|
948
|
+
console.error(`[Worker] Error:`, error);
|
|
949
|
+
|
|
950
|
+
// 保存错误检查点
|
|
951
|
+
await this.checkpointService.saveCheckpoint({
|
|
952
|
+
executionId,
|
|
953
|
+
stepIndex: 0,
|
|
954
|
+
lastMessageId: '',
|
|
955
|
+
lastMessageTime: Date.now(),
|
|
956
|
+
canResume: false
|
|
957
|
+
});
|
|
958
|
+
|
|
959
|
+
// 更新状态为失败
|
|
960
|
+
await this.executionService.updateStatus(executionId, 'FAILED', {
|
|
961
|
+
error: error.message
|
|
962
|
+
});
|
|
963
|
+
|
|
964
|
+
// SSE 推送错误
|
|
965
|
+
this.ssePublisher.publish(executionId, {
|
|
966
|
+
type: 'error',
|
|
967
|
+
data: { message: error.message }
|
|
968
|
+
});
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
};
|
|
972
|
+
|
|
973
|
+
// 7. 执行 Agent
|
|
974
|
+
console.log(`[Worker] Starting agent execution from step ${startStep}`);
|
|
975
|
+
const result = await this.agent.run(input);
|
|
976
|
+
|
|
977
|
+
// 8. 执行完成
|
|
978
|
+
const finalMessage = result.messages[result.messages.length - 1];
|
|
979
|
+
|
|
980
|
+
await this.executionService.updateStatus(executionId, 'COMPLETED', {
|
|
981
|
+
result: finalMessage?.content,
|
|
982
|
+
steps: result.steps,
|
|
983
|
+
finishReason: result.finishReason
|
|
984
|
+
});
|
|
985
|
+
|
|
986
|
+
// SSE 推送完成
|
|
987
|
+
this.ssePublisher.publish(executionId, {
|
|
988
|
+
type: 'done',
|
|
989
|
+
data: {
|
|
990
|
+
result: finalMessage?.content,
|
|
991
|
+
steps: result.steps
|
|
992
|
+
}
|
|
993
|
+
});
|
|
994
|
+
|
|
995
|
+
console.log(`[Worker] Task completed: ${executionId}`);
|
|
996
|
+
|
|
997
|
+
} catch (error) {
|
|
998
|
+
// 执行失败
|
|
999
|
+
console.error(`[Worker] Task failed: ${executionId}`, error);
|
|
1000
|
+
|
|
1001
|
+
await this.executionService.updateStatus(executionId, 'FAILED', {
|
|
1002
|
+
error: (error as Error).message
|
|
1003
|
+
});
|
|
1004
|
+
|
|
1005
|
+
} finally {
|
|
1006
|
+
// 释放锁
|
|
1007
|
+
await this.executionService.releaseLock(executionId);
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
// 辅助方法: 睡眠
|
|
1012
|
+
private sleep(ms: number): Promise<void> {
|
|
1013
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
// ============================================================
|
|
1018
|
+
// 3. 启动 Worker
|
|
1019
|
+
// ============================================================
|
|
1020
|
+
|
|
1021
|
+
async function main() {
|
|
1022
|
+
// 从容器获取依赖
|
|
1023
|
+
const queue = container.get<TaskQueue>('taskQueue');
|
|
1024
|
+
const agent = container.get<StatelessAgent>('agent');
|
|
1025
|
+
const executionService = container.get<ExecutionService>('executionService');
|
|
1026
|
+
const messageStorage = container.get<MessageStorage>('messageStorage');
|
|
1027
|
+
const checkpointService = container.get<CheckpointService>('checkpointService');
|
|
1028
|
+
const contextService = container.get<ContextService>('contextService');
|
|
1029
|
+
const ssePublisher = container.get<SSEPublisher>('ssePublisher');
|
|
1030
|
+
|
|
1031
|
+
// 创建 Worker 实例
|
|
1032
|
+
const worker = new ExecutionWorker(
|
|
1033
|
+
queue,
|
|
1034
|
+
agent,
|
|
1035
|
+
executionService,
|
|
1036
|
+
messageStorage,
|
|
1037
|
+
checkpointService,
|
|
1038
|
+
contextService,
|
|
1039
|
+
ssePublisher
|
|
1040
|
+
);
|
|
1041
|
+
|
|
1042
|
+
// 启动
|
|
1043
|
+
await worker.start();
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
// 运行
|
|
1047
|
+
main().catch(console.error);
|
|
1048
|
+
```
|
|
1049
|
+
|
|
1050
|
+
---
|
|
1051
|
+
|
|
1052
|
+
### 依赖服务详细说明
|
|
1053
|
+
|
|
1054
|
+
#### 1. TaskQueue (任务队列)
|
|
1055
|
+
|
|
1056
|
+
```typescript
|
|
1057
|
+
class TaskQueue {
|
|
1058
|
+
constructor(private redis: Redis) {}
|
|
1059
|
+
|
|
1060
|
+
// 放入任务 (LPUSH)
|
|
1061
|
+
async push(task: Task): Promise<void> {
|
|
1062
|
+
await this.redis.lpush('task_queue', JSON.stringify(task));
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
// 阻塞取出任务 (BRPOP)
|
|
1066
|
+
async brpop(timeout: number = 0): Promise<Task | null> {
|
|
1067
|
+
const result = await this.redis.brpop('task_queue', timeout);
|
|
1068
|
+
if (result) {
|
|
1069
|
+
return JSON.parse(result[1]);
|
|
1070
|
+
}
|
|
1071
|
+
return null;
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
```
|
|
1075
|
+
|
|
1076
|
+
#### 2. ExecutionService (执行状态管理)
|
|
1077
|
+
|
|
1078
|
+
```typescript
|
|
1079
|
+
class ExecutionService {
|
|
1080
|
+
constructor(private redis: Redis) {}
|
|
1081
|
+
|
|
1082
|
+
// 更新状态
|
|
1083
|
+
async updateStatus(executionId: string, status: string, extra?: any): Promise<void> {
|
|
1084
|
+
const key = `execution:${executionId}`;
|
|
1085
|
+
const update: any = { status, updatedAt: Date.now(), ...extra };
|
|
1086
|
+
|
|
1087
|
+
if (status === 'COMPLETED' || status === 'FAILED') {
|
|
1088
|
+
update.completedAt = Date.now();
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
await this.redis.hset(key, update);
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
// 获取锁 (防止重复执行)
|
|
1095
|
+
async acquireLock(executionId: string, workerId: string): Promise<boolean> {
|
|
1096
|
+
const key = `lock:execution:${executionId}`;
|
|
1097
|
+
const result = await this.redis.set(key, workerId, {
|
|
1098
|
+
NX: true, // 只有不存在时设置
|
|
1099
|
+
EX: 300 // 5分钟超时
|
|
1100
|
+
});
|
|
1101
|
+
return result === 'OK';
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
// 释放锁
|
|
1105
|
+
async releaseLock(executionId: string): Promise<void> {
|
|
1106
|
+
await this.redis.del(`lock:execution:${executionId}`);
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
```
|
|
1110
|
+
|
|
1111
|
+
#### 3. MessageStorage (消息存储)
|
|
1112
|
+
|
|
1113
|
+
```typescript
|
|
1114
|
+
class MessageStorage {
|
|
1115
|
+
constructor(
|
|
1116
|
+
private redis: Redis,
|
|
1117
|
+
private kafka: Kafka
|
|
1118
|
+
) {}
|
|
1119
|
+
|
|
1120
|
+
// 保存消息
|
|
1121
|
+
async save(conversationId: string, message: Message): Promise<void> {
|
|
1122
|
+
const key = `conversation:${conversationId}:messages`;
|
|
1123
|
+
|
|
1124
|
+
// 1. 实时写入 Redis
|
|
1125
|
+
await this.redis.rpush(key, JSON.stringify(message));
|
|
1126
|
+
await this.redis.expire(key, 1800); // 30分钟过期
|
|
1127
|
+
|
|
1128
|
+
// 2. 发送到 Kafka (异步)
|
|
1129
|
+
await this.kafka.send({
|
|
1130
|
+
topic: 'messages',
|
|
1131
|
+
messages: [{
|
|
1132
|
+
key: conversationId,
|
|
1133
|
+
value: { event: 'message_created', conversationId, message }
|
|
1134
|
+
}]
|
|
1135
|
+
});
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
```
|
|
1139
|
+
|
|
1140
|
+
#### 4. CheckpointService (检查点服务)
|
|
1141
|
+
|
|
1142
|
+
```typescript
|
|
1143
|
+
class CheckpointService {
|
|
1144
|
+
constructor(private redis: Redis) {}
|
|
1145
|
+
|
|
1146
|
+
// 保存检查点
|
|
1147
|
+
async saveCheckpoint(checkpoint: ExecutionCheckpoint): Promise<void> {
|
|
1148
|
+
const key = `execution:${checkpoint.executionId}:checkpoint`;
|
|
1149
|
+
|
|
1150
|
+
await this.redis.hset(key, {
|
|
1151
|
+
stepIndex: checkpoint.stepIndex.toString(),
|
|
1152
|
+
lastMessageId: checkpoint.lastMessageId,
|
|
1153
|
+
lastMessageTime: checkpoint.lastMessageTime.toString(),
|
|
1154
|
+
canResume: checkpoint.canResume ? '1' : '0'
|
|
1155
|
+
}, { EX: 86400 });
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
// 获取检查点
|
|
1159
|
+
async getLatestCheckpoint(executionId: string): Promise<ExecutionCheckpoint | null> {
|
|
1160
|
+
const key = `execution:${executionId}:checkpoint`;
|
|
1161
|
+
const data = await this.redis.hgetall(key);
|
|
1162
|
+
|
|
1163
|
+
if (!data || Object.keys(data).length === 0) {
|
|
1164
|
+
return null;
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
return {
|
|
1168
|
+
executionId,
|
|
1169
|
+
stepIndex: parseInt(data.stepIndex),
|
|
1170
|
+
lastMessageId: data.lastMessageId,
|
|
1171
|
+
lastMessageTime: parseInt(data.lastMessageTime),
|
|
1172
|
+
canResume: data.canResume === '1'
|
|
1173
|
+
};
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
```
|
|
1177
|
+
|
|
1178
|
+
#### 5. ContextService (上下文服务)
|
|
1179
|
+
|
|
1180
|
+
```typescript
|
|
1181
|
+
class ContextService {
|
|
1182
|
+
constructor(
|
|
1183
|
+
private redis: Redis,
|
|
1184
|
+
private clickhouse: ClickHouse
|
|
1185
|
+
) {}
|
|
1186
|
+
|
|
1187
|
+
// 加载上下文
|
|
1188
|
+
async load(conversationId: string): Promise<ConversationContext> {
|
|
1189
|
+
const cacheKey = `conversation:${conversationId}:context`;
|
|
1190
|
+
|
|
1191
|
+
// 1. 先从 Redis 缓存获取
|
|
1192
|
+
const cached = await this.redis.get(cacheKey);
|
|
1193
|
+
if (cached) {
|
|
1194
|
+
return JSON.parse(cached);
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
// 2. 缓存不存在,从 ClickHouse 加载
|
|
1198
|
+
const messages = await this.clickhouse.query(`
|
|
1199
|
+
SELECT * FROM messages
|
|
1200
|
+
WHERE conversation_id = '${conversationId}'
|
|
1201
|
+
ORDER BY timestamp ASC
|
|
1202
|
+
LIMIT 1000
|
|
1203
|
+
`);
|
|
1204
|
+
|
|
1205
|
+
// 3. 加载系统提示和工具配置
|
|
1206
|
+
const conversation = await this.clickhouse.query(`
|
|
1207
|
+
SELECT * FROM conversations
|
|
1208
|
+
WHERE id = '${conversationId}'
|
|
1209
|
+
`);
|
|
1210
|
+
|
|
1211
|
+
const context: ConversationContext = {
|
|
1212
|
+
messages,
|
|
1213
|
+
systemPrompt: conversation[0]?.system_prompt,
|
|
1214
|
+
tools: conversation[0]?.tools
|
|
1215
|
+
};
|
|
1216
|
+
|
|
1217
|
+
// 4. 写入缓存
|
|
1218
|
+
await this.redis.set(cacheKey, JSON.stringify(context), { EX: 300 });
|
|
1219
|
+
|
|
1220
|
+
return context;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
```
|
|
1224
|
+
|
|
1225
|
+
---
|
|
1226
|
+
|
|
1227
|
+
## 八、故障恢复机制
|
|
1228
|
+
|
|
1229
|
+
### 场景 1: Agent 进程崩溃
|
|
1230
|
+
|
|
1231
|
+
```
|
|
1232
|
+
发生时间: Step 3 ~ Step 4 之间
|
|
1233
|
+
|
|
1234
|
+
已存储:
|
|
1235
|
+
✅ Step 1 消息 (Redis + Kafka)
|
|
1236
|
+
✅ Step 2 消息 (Redis + Kafka)
|
|
1237
|
+
✅ Step 3 消息 (Redis + Kafka)
|
|
1238
|
+
✅ Step 4 消息 (部分)
|
|
1239
|
+
✅ 检查点: stepIndex=3, lastMessageId=msg_3
|
|
1240
|
+
|
|
1241
|
+
丢失: Step 4 未完成的工具调用结果
|
|
1242
|
+
|
|
1243
|
+
恢复:
|
|
1244
|
+
1. 从检查点获取 lastMessageId=msg_3
|
|
1245
|
+
2. 查询 msg_3 之后的消息
|
|
1246
|
+
3. 从 Step 4 继续执行
|
|
1247
|
+
```
|
|
1248
|
+
|
|
1249
|
+
### 场景 2: Redis 崩溃
|
|
1250
|
+
|
|
1251
|
+
```
|
|
1252
|
+
已存储:
|
|
1253
|
+
✅ Kafka 保留所有消息 (7 天)
|
|
1254
|
+
|
|
1255
|
+
恢复:
|
|
1256
|
+
1. 从 Kafka 重放消息
|
|
1257
|
+
2. 重建 Redis 缓存
|
|
1258
|
+
```
|
|
1259
|
+
|
|
1260
|
+
### 场景 3: 整个节点崩溃
|
|
1261
|
+
|
|
1262
|
+
```
|
|
1263
|
+
恢复:
|
|
1264
|
+
1. Task Manager 检测到超时
|
|
1265
|
+
2. 从检查点恢复
|
|
1266
|
+
3. 调度到新节点继续执行
|
|
1267
|
+
```
|
|
1268
|
+
|
|
1269
|
+
---
|
|
1270
|
+
|
|
1271
|
+
## 九、完整数据流
|
|
1272
|
+
|
|
1273
|
+
```
|
|
1274
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
1275
|
+
│ 完整数据流 │
|
|
1276
|
+
├─────────────────────────────────────────────────────────────────┤
|
|
1277
|
+
│ │
|
|
1278
|
+
│ 用户请求 │
|
|
1279
|
+
│ │ │
|
|
1280
|
+
│ ▼ │
|
|
1281
|
+
│ API Gateway (鉴权/限流) │
|
|
1282
|
+
│ │ │
|
|
1283
|
+
│ ▼ │
|
|
1284
|
+
│ Controller (创建 Task) │
|
|
1285
|
+
│ │ │
|
|
1286
|
+
│ │ 立即返回 executionId │
|
|
1287
|
+
│ ▼ │
|
|
1288
|
+
│ Task Queue (Redis) │
|
|
1289
|
+
│ │ │
|
|
1290
|
+
│ ▼ │
|
|
1291
|
+
│ Worker (执行 Agent) ────────────────────────────────────── │
|
|
1292
|
+
│ │ │
|
|
1293
|
+
│ ├─ onMessage ──▶ Redis + Kafka + SSE │
|
|
1294
|
+
│ │ │
|
|
1295
|
+
│ ├─ onCheckpoint ──▶ Redis │
|
|
1296
|
+
│ │ │
|
|
1297
|
+
│ ▼ │
|
|
1298
|
+
│ 执行完成 │
|
|
1299
|
+
│ │ │
|
|
1300
|
+
│ ▼ │
|
|
1301
|
+
│ 用户查询结果 (REST API / SSE) │
|
|
1302
|
+
│ │
|
|
1303
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
1304
|
+
```
|
|
1305
|
+
|
|
1306
|
+
---
|
|
1307
|
+
|
|
1308
|
+
## 十、技术选型
|
|
1309
|
+
|
|
1310
|
+
| 层级 | 组件 | 用途 |
|
|
1311
|
+
|------|------|------|
|
|
1312
|
+
| API | Express/Spring Boot | REST API + SSE |
|
|
1313
|
+
| 任务队列 | Redis List | 任务缓冲 |
|
|
1314
|
+
| 缓存 | Redis | 消息 + 检查点 |
|
|
1315
|
+
| 消息队列 | Kafka | 异步持久化 |
|
|
1316
|
+
| 主存储 | ClickHouse | 消息历史 |
|
|
1317
|
+
| 搜索 | Elasticsearch | 全文搜索 |
|
|
1318
|
+
|
|
1319
|
+
---
|
|
1320
|
+
|
|
1321
|
+
## 十一、总结
|
|
1322
|
+
|
|
1323
|
+
```
|
|
1324
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
1325
|
+
│ 方案总结 │
|
|
1326
|
+
├─────────────────────────────────────────────────────────────────┤
|
|
1327
|
+
│ │
|
|
1328
|
+
│ 1. 无状态 Agent │
|
|
1329
|
+
│ - 输入: sessionId + messages + callbacks │
|
|
1330
|
+
│ - 输出: new_messages + finishReason │
|
|
1331
|
+
│ - 内部通过回调实时存储 │
|
|
1332
|
+
│ │
|
|
1333
|
+
│ 2. 实时存储 │
|
|
1334
|
+
│ - onMessage: 每条消息实时存储 (Redis + Kafka) │
|
|
1335
|
+
│ - onCheckpoint: 记录位置 (stepIndex + lastMessageId) │
|
|
1336
|
+
│ │
|
|
1337
|
+
│ 3. 后台执行 │
|
|
1338
|
+
│ - API 立即返回 executionId │
|
|
1339
|
+
│ - Task Queue 缓冲任务 │
|
|
1340
|
+
│ - Worker 后台消费 │
|
|
1341
|
+
│ │
|
|
1342
|
+
│ 4. 用户交互 │
|
|
1343
|
+
│ - SSE 实时推送 (在线用户) │
|
|
1344
|
+
│ - REST API 查询 (离线用户) │
|
|
1345
|
+
│ - 支持页面关闭后继续执行 │
|
|
1346
|
+
│ │
|
|
1347
|
+
│ 5. 故障恢复 │
|
|
1348
|
+
│ - 检查点恢复: 从 lastMessageId 继续 │
|
|
1349
|
+
│ - Worker 挂了: Task Queue 重试 │
|
|
1350
|
+
│ - Redis 挂了: Kafka 重放 │
|
|
1351
|
+
│ │
|
|
1352
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
1353
|
+
```
|