@graychennpm/codebridge 0.0.0-stage → 0.1.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/CHANGELOG.md +14 -0
- package/LICENSE +21 -0
- package/README.md +217 -2
- package/README.zh-CN.md +222 -0
- package/apps/web/public/brand/agent-logo-carbon.svg +1 -0
- package/apps/web/public/brand/agent-logo.svg +1 -0
- package/docs/zh-CN/npm-release.md +77 -0
- package/node_modules/@codebridge/agent-registry/dist/index.d.ts +32 -0
- package/node_modules/@codebridge/agent-registry/dist/index.js +177 -0
- package/node_modules/@codebridge/agent-registry/dist/setup.d.ts +9 -0
- package/node_modules/@codebridge/agent-registry/dist/setup.js +115 -0
- package/node_modules/@codebridge/agent-registry/package.json +17 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-active-session.d.ts +12 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-active-session.js +57 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-claude-activity.d.ts +9 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-claude-activity.js +49 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-config-options.d.ts +46 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-config-options.js +220 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-doctor.d.ts +3 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-doctor.js +18 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-event-mapper.d.ts +5 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-event-mapper.js +136 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-kill.d.ts +10 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-kill.js +24 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-race.d.ts +6 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-race.js +29 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-session-list.d.ts +32 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-session-list.js +218 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-session-pool.d.ts +85 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-session-pool.js +165 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-session-runner.d.ts +78 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-session-runner.js +550 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-spawn-profiles.d.ts +9 -0
- package/node_modules/@codebridge/backends/dist/acp/acp-spawn-profiles.js +28 -0
- package/node_modules/@codebridge/backends/dist/acp/headless-client.d.ts +19 -0
- package/node_modules/@codebridge/backends/dist/acp/headless-client.js +47 -0
- package/node_modules/@codebridge/backends/dist/acp/index.d.ts +9 -0
- package/node_modules/@codebridge/backends/dist/acp/index.js +9 -0
- package/node_modules/@codebridge/backends/dist/agent-setup.d.ts +60 -0
- package/node_modules/@codebridge/backends/dist/agent-setup.js +504 -0
- package/node_modules/@codebridge/backends/dist/attachment-prompt.d.ts +8 -0
- package/node_modules/@codebridge/backends/dist/attachment-prompt.js +28 -0
- package/node_modules/@codebridge/backends/dist/auth-error-hints.d.ts +8 -0
- package/node_modules/@codebridge/backends/dist/auth-error-hints.js +33 -0
- package/node_modules/@codebridge/backends/dist/codex-skill-commands.d.ts +3 -0
- package/node_modules/@codebridge/backends/dist/codex-skill-commands.js +88 -0
- package/node_modules/@codebridge/backends/dist/index.d.ts +18 -0
- package/node_modules/@codebridge/backends/dist/index.js +34 -0
- package/node_modules/@codebridge/backends/dist/pi-providers.d.ts +45 -0
- package/node_modules/@codebridge/backends/dist/pi-providers.js +148 -0
- package/node_modules/@codebridge/backends/dist/pi-session-runner.d.ts +85 -0
- package/node_modules/@codebridge/backends/dist/pi-session-runner.js +460 -0
- package/node_modules/@codebridge/backends/dist/provider-connection.d.ts +17 -0
- package/node_modules/@codebridge/backends/dist/provider-connection.js +99 -0
- package/node_modules/@codebridge/backends/dist/session-discovery.d.ts +23 -0
- package/node_modules/@codebridge/backends/dist/session-discovery.js +340 -0
- package/node_modules/@codebridge/backends/dist/skill-control-plane.d.ts +147 -0
- package/node_modules/@codebridge/backends/dist/skill-control-plane.js +832 -0
- package/node_modules/@codebridge/backends/package.json +22 -0
- package/node_modules/@codebridge/bridge/dist/alert-notification-rules.d.ts +14 -0
- package/node_modules/@codebridge/bridge/dist/alert-notification-rules.js +37 -0
- package/node_modules/@codebridge/bridge/dist/channel-config.d.ts +4 -0
- package/node_modules/@codebridge/bridge/dist/channel-config.js +9 -0
- package/node_modules/@codebridge/bridge/dist/channel-ingress.d.ts +5 -0
- package/node_modules/@codebridge/bridge/dist/channel-ingress.js +403 -0
- package/node_modules/@codebridge/bridge/dist/cli.d.ts +3 -0
- package/node_modules/@codebridge/bridge/dist/cli.js +581 -0
- package/node_modules/@codebridge/bridge/dist/deployment.d.ts +46 -0
- package/node_modules/@codebridge/bridge/dist/deployment.js +174 -0
- package/node_modules/@codebridge/bridge/dist/event-loop-lag.d.ts +19 -0
- package/node_modules/@codebridge/bridge/dist/event-loop-lag.js +33 -0
- package/node_modules/@codebridge/bridge/dist/feishu-alert-monitor.d.ts +70 -0
- package/node_modules/@codebridge/bridge/dist/feishu-alert-monitor.js +591 -0
- package/node_modules/@codebridge/bridge/dist/init-wizard.d.ts +52 -0
- package/node_modules/@codebridge/bridge/dist/init-wizard.js +571 -0
- package/node_modules/@codebridge/bridge/dist/mcp-api.d.ts +4 -0
- package/node_modules/@codebridge/bridge/dist/mcp-api.js +100 -0
- package/node_modules/@codebridge/bridge/dist/outbound-api.d.ts +31 -0
- package/node_modules/@codebridge/bridge/dist/outbound-api.js +184 -0
- package/node_modules/@codebridge/bridge/dist/outbound-publishers.d.ts +54 -0
- package/node_modules/@codebridge/bridge/dist/outbound-publishers.js +213 -0
- package/node_modules/@codebridge/bridge/dist/project-api.d.ts +4 -0
- package/node_modules/@codebridge/bridge/dist/project-api.js +161 -0
- package/node_modules/@codebridge/bridge/dist/secrets-command.d.ts +13 -0
- package/node_modules/@codebridge/bridge/dist/secrets-command.js +34 -0
- package/node_modules/@codebridge/bridge/dist/session-api.d.ts +26 -0
- package/node_modules/@codebridge/bridge/dist/session-api.js +1378 -0
- package/node_modules/@codebridge/bridge/dist/session-history-import.d.ts +24 -0
- package/node_modules/@codebridge/bridge/dist/session-history-import.js +82 -0
- package/node_modules/@codebridge/bridge/dist/session-runtime-api.d.ts +19 -0
- package/node_modules/@codebridge/bridge/dist/session-runtime-api.js +453 -0
- package/node_modules/@codebridge/bridge/dist/session-runtime-migration.d.ts +21 -0
- package/node_modules/@codebridge/bridge/dist/session-runtime-migration.js +43 -0
- package/node_modules/@codebridge/bridge/dist/session-runtime-types.d.ts +85 -0
- package/node_modules/@codebridge/bridge/dist/session-runtime-types.js +98 -0
- package/node_modules/@codebridge/bridge/dist/skill-api.d.ts +28 -0
- package/node_modules/@codebridge/bridge/dist/skill-api.js +129 -0
- package/node_modules/@codebridge/bridge/dist/startup-surfaces.d.ts +15 -0
- package/node_modules/@codebridge/bridge/dist/startup-surfaces.js +13 -0
- package/node_modules/@codebridge/bridge/dist/web-frontend.d.ts +8 -0
- package/node_modules/@codebridge/bridge/dist/web-frontend.js +76 -0
- package/node_modules/@codebridge/bridge/dist/work-item-api.d.ts +6 -0
- package/node_modules/@codebridge/bridge/dist/work-item-api.js +414 -0
- package/node_modules/@codebridge/bridge/package.json +28 -0
- package/node_modules/@codebridge/channel-feishu/dist/alert-reactions.d.ts +6 -0
- package/node_modules/@codebridge/channel-feishu/dist/alert-reactions.js +58 -0
- package/node_modules/@codebridge/channel-feishu/dist/alert-types.d.ts +35 -0
- package/node_modules/@codebridge/channel-feishu/dist/alert-types.js +2 -0
- package/node_modules/@codebridge/channel-feishu/dist/bridge.d.ts +130 -0
- package/node_modules/@codebridge/channel-feishu/dist/bridge.js +1231 -0
- package/node_modules/@codebridge/channel-feishu/dist/cardkit-writer.d.ts +48 -0
- package/node_modules/@codebridge/channel-feishu/dist/cardkit-writer.js +153 -0
- package/node_modules/@codebridge/channel-feishu/dist/chain-topics.d.ts +20 -0
- package/node_modules/@codebridge/channel-feishu/dist/chain-topics.js +35 -0
- package/node_modules/@codebridge/channel-feishu/dist/coalescing-card-writer.d.ts +12 -0
- package/node_modules/@codebridge/channel-feishu/dist/coalescing-card-writer.js +53 -0
- package/node_modules/@codebridge/channel-feishu/dist/delivery-reconciler.d.ts +19 -0
- package/node_modules/@codebridge/channel-feishu/dist/delivery-reconciler.js +53 -0
- package/node_modules/@codebridge/channel-feishu/dist/durable-markdown-card.d.ts +19 -0
- package/node_modules/@codebridge/channel-feishu/dist/durable-markdown-card.js +77 -0
- package/node_modules/@codebridge/channel-feishu/dist/feishu-extra-events.d.ts +27 -0
- package/node_modules/@codebridge/channel-feishu/dist/feishu-extra-events.js +24 -0
- package/node_modules/@codebridge/channel-feishu/dist/feishu-http.d.ts +4 -0
- package/node_modules/@codebridge/channel-feishu/dist/feishu-http.js +25 -0
- package/node_modules/@codebridge/channel-feishu/dist/feishu-inbound-context.d.ts +13 -0
- package/node_modules/@codebridge/channel-feishu/dist/feishu-inbound-context.js +45 -0
- package/node_modules/@codebridge/channel-feishu/dist/feishu-inbound-media.d.ts +22 -0
- package/node_modules/@codebridge/channel-feishu/dist/feishu-inbound-media.js +273 -0
- package/node_modules/@codebridge/channel-feishu/dist/feishu-mention-gate.d.ts +13 -0
- package/node_modules/@codebridge/channel-feishu/dist/feishu-mention-gate.js +22 -0
- package/node_modules/@codebridge/channel-feishu/dist/feishu-outbound-file.d.ts +12 -0
- package/node_modules/@codebridge/channel-feishu/dist/feishu-outbound-file.js +38 -0
- package/node_modules/@codebridge/channel-feishu/dist/feishu-quoted-message.d.ts +30 -0
- package/node_modules/@codebridge/channel-feishu/dist/feishu-quoted-message.js +183 -0
- package/node_modules/@codebridge/channel-feishu/dist/index.d.ts +7 -0
- package/node_modules/@codebridge/channel-feishu/dist/index.js +3 -0
- package/node_modules/@codebridge/channel-feishu/dist/redacting-logger.d.ts +17 -0
- package/node_modules/@codebridge/channel-feishu/dist/redacting-logger.js +41 -0
- package/node_modules/@codebridge/channel-feishu/dist/run-status.d.ts +39 -0
- package/node_modules/@codebridge/channel-feishu/dist/run-status.js +166 -0
- package/node_modules/@codebridge/channel-feishu/dist/session-watcher.d.ts +98 -0
- package/node_modules/@codebridge/channel-feishu/dist/session-watcher.js +655 -0
- package/node_modules/@codebridge/channel-feishu/package.json +19 -0
- package/node_modules/@codebridge/channel-telegram/dist/coalescing-message-writer.d.ts +14 -0
- package/node_modules/@codebridge/channel-telegram/dist/coalescing-message-writer.js +56 -0
- package/node_modules/@codebridge/channel-telegram/dist/index.d.ts +3 -0
- package/node_modules/@codebridge/channel-telegram/dist/index.js +3 -0
- package/node_modules/@codebridge/channel-telegram/dist/telegram-api.d.ts +62 -0
- package/node_modules/@codebridge/channel-telegram/dist/telegram-api.js +72 -0
- package/node_modules/@codebridge/channel-telegram/dist/telegram-bridge.d.ts +64 -0
- package/node_modules/@codebridge/channel-telegram/dist/telegram-bridge.js +469 -0
- package/node_modules/@codebridge/channel-telegram/dist/telegram-session-watcher.d.ts +41 -0
- package/node_modules/@codebridge/channel-telegram/dist/telegram-session-watcher.js +184 -0
- package/node_modules/@codebridge/channel-telegram/package.json +18 -0
- package/node_modules/@codebridge/core/dist/config-schema.d.ts +1195 -0
- package/node_modules/@codebridge/core/dist/config-schema.js +293 -0
- package/node_modules/@codebridge/core/dist/config-store.d.ts +48 -0
- package/node_modules/@codebridge/core/dist/config-store.js +207 -0
- package/node_modules/@codebridge/core/dist/index.d.ts +11 -0
- package/node_modules/@codebridge/core/dist/index.js +10 -0
- package/node_modules/@codebridge/core/dist/json-file-store.d.ts +16 -0
- package/node_modules/@codebridge/core/dist/json-file-store.js +46 -0
- package/node_modules/@codebridge/core/dist/mcp-types.d.ts +7 -0
- package/node_modules/@codebridge/core/dist/mcp-types.js +2 -0
- package/node_modules/@codebridge/core/dist/mentions.d.ts +36 -0
- package/node_modules/@codebridge/core/dist/mentions.js +138 -0
- package/node_modules/@codebridge/core/dist/secret-migration.d.ts +14 -0
- package/node_modules/@codebridge/core/dist/secret-migration.js +55 -0
- package/node_modules/@codebridge/core/dist/secret-refs.d.ts +34 -0
- package/node_modules/@codebridge/core/dist/secret-refs.js +142 -0
- package/node_modules/@codebridge/core/dist/session-event-wire.d.ts +17 -0
- package/node_modules/@codebridge/core/dist/session-event-wire.js +28 -0
- package/node_modules/@codebridge/core/dist/types.d.ts +363 -0
- package/node_modules/@codebridge/core/dist/types.js +22 -0
- package/node_modules/@codebridge/core/dist/workspace-key.d.ts +11 -0
- package/node_modules/@codebridge/core/dist/workspace-key.js +25 -0
- package/node_modules/@codebridge/core/package.json +22 -0
- package/node_modules/@codebridge/mcp-runtime/dist/.tsbuildinfo +1 -0
- package/node_modules/@codebridge/mcp-runtime/dist/index.d.ts +92 -0
- package/node_modules/@codebridge/mcp-runtime/dist/index.js +377 -0
- package/node_modules/@codebridge/mcp-runtime/package.json +19 -0
- package/node_modules/@codebridge/policy/dist/capability-runtime.d.ts +123 -0
- package/node_modules/@codebridge/policy/dist/capability-runtime.js +195 -0
- package/node_modules/@codebridge/policy/dist/demo-capabilities.d.ts +4 -0
- package/node_modules/@codebridge/policy/dist/demo-capabilities.js +134 -0
- package/node_modules/@codebridge/policy/dist/equity-capabilities.d.ts +8 -0
- package/node_modules/@codebridge/policy/dist/equity-capabilities.js +35 -0
- package/node_modules/@codebridge/policy/dist/index.d.ts +107 -0
- package/node_modules/@codebridge/policy/dist/index.js +317 -0
- package/node_modules/@codebridge/policy/package.json +17 -0
- package/node_modules/@codebridge/project-catalog/dist/index.d.ts +145 -0
- package/node_modules/@codebridge/project-catalog/dist/index.js +699 -0
- package/node_modules/@codebridge/project-catalog/package.json +18 -0
- package/node_modules/@codebridge/router/dist/channel-stream-projector.d.ts +19 -0
- package/node_modules/@codebridge/router/dist/channel-stream-projector.js +68 -0
- package/node_modules/@codebridge/router/dist/command-help.d.ts +14 -0
- package/node_modules/@codebridge/router/dist/command-help.js +151 -0
- package/node_modules/@codebridge/router/dist/feishu-stream-presenter.d.ts +18 -0
- package/node_modules/@codebridge/router/dist/feishu-stream-presenter.js +193 -0
- package/node_modules/@codebridge/router/dist/index.d.ts +9 -0
- package/node_modules/@codebridge/router/dist/index.js +9 -0
- package/node_modules/@codebridge/router/dist/model-effort.d.ts +5 -0
- package/node_modules/@codebridge/router/dist/model-effort.js +33 -0
- package/node_modules/@codebridge/router/dist/orchestrator.d.ts +56 -0
- package/node_modules/@codebridge/router/dist/orchestrator.js +290 -0
- package/node_modules/@codebridge/router/dist/session-list-format.d.ts +19 -0
- package/node_modules/@codebridge/router/dist/session-list-format.js +97 -0
- package/node_modules/@codebridge/router/dist/session-rotation.d.ts +9 -0
- package/node_modules/@codebridge/router/dist/session-rotation.js +31 -0
- package/node_modules/@codebridge/router/dist/session-router.d.ts +59 -0
- package/node_modules/@codebridge/router/dist/session-router.js +182 -0
- package/node_modules/@codebridge/router/dist/slash-commands.d.ts +78 -0
- package/node_modules/@codebridge/router/dist/slash-commands.js +874 -0
- package/node_modules/@codebridge/router/package.json +18 -0
- package/node_modules/@codebridge/run-executor/dist/agent-event-aggregator.d.ts +18 -0
- package/node_modules/@codebridge/run-executor/dist/agent-event-aggregator.js +88 -0
- package/node_modules/@codebridge/run-executor/dist/index.d.ts +55 -0
- package/node_modules/@codebridge/run-executor/dist/index.js +754 -0
- package/node_modules/@codebridge/run-executor/dist/run-heartbeat.d.ts +14 -0
- package/node_modules/@codebridge/run-executor/dist/run-heartbeat.js +24 -0
- package/node_modules/@codebridge/run-executor/package.json +20 -0
- package/node_modules/@codebridge/runner-client/dist/index.d.ts +142 -0
- package/node_modules/@codebridge/runner-client/dist/index.js +439 -0
- package/node_modules/@codebridge/runner-client/package.json +18 -0
- package/node_modules/@codebridge/runner-host/dist/cli.d.ts +3 -0
- package/node_modules/@codebridge/runner-host/dist/cli.js +58 -0
- package/node_modules/@codebridge/runner-host/dist/codex-session-ownership.d.ts +13 -0
- package/node_modules/@codebridge/runner-host/dist/codex-session-ownership.js +79 -0
- package/node_modules/@codebridge/runner-host/dist/deployment-mcp-server.d.ts +10 -0
- package/node_modules/@codebridge/runner-host/dist/deployment-mcp-server.js +54 -0
- package/node_modules/@codebridge/runner-host/dist/fcb-script.d.ts +3 -0
- package/node_modules/@codebridge/runner-host/dist/fcb-script.js +99 -0
- package/node_modules/@codebridge/runner-host/dist/index.d.ts +3 -0
- package/node_modules/@codebridge/runner-host/dist/index.js +2 -0
- package/node_modules/@codebridge/runner-host/dist/materialize-attachments.d.ts +4 -0
- package/node_modules/@codebridge/runner-host/dist/materialize-attachments.js +62 -0
- package/node_modules/@codebridge/runner-host/dist/server.d.ts +150 -0
- package/node_modules/@codebridge/runner-host/dist/server.js +1295 -0
- package/node_modules/@codebridge/runner-host/dist/session-lease.d.ts +12 -0
- package/node_modules/@codebridge/runner-host/dist/session-lease.js +146 -0
- package/node_modules/@codebridge/runner-host/package.json +26 -0
- package/node_modules/@codebridge/session-catalog/dist/index.d.ts +124 -0
- package/node_modules/@codebridge/session-catalog/dist/index.js +411 -0
- package/node_modules/@codebridge/session-catalog/package.json +17 -0
- package/node_modules/@codebridge/session-coordinator/dist/coordinator.d.ts +111 -0
- package/node_modules/@codebridge/session-coordinator/dist/coordinator.js +364 -0
- package/node_modules/@codebridge/session-coordinator/dist/index.d.ts +5 -0
- package/node_modules/@codebridge/session-coordinator/dist/index.js +5 -0
- package/node_modules/@codebridge/session-coordinator/dist/lease.d.ts +14 -0
- package/node_modules/@codebridge/session-coordinator/dist/lease.js +26 -0
- package/node_modules/@codebridge/session-coordinator/dist/queued-claim.d.ts +20 -0
- package/node_modules/@codebridge/session-coordinator/dist/queued-claim.js +45 -0
- package/node_modules/@codebridge/session-coordinator/dist/recovery.d.ts +31 -0
- package/node_modules/@codebridge/session-coordinator/dist/recovery.js +114 -0
- package/node_modules/@codebridge/session-coordinator/package.json +17 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
- package/node_modules/@codebridge/web/dist/assets/abnfDiagram-N423BO3Z-C-JdFv0U.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/arc-DsNROjzU.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/architecture-TIHT7OUA-ZEBHG2JZ.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/architectureDiagram-T3A2C74G-OsrxcwPM.js +36 -0
- package/node_modules/@codebridge/web/dist/assets/array-BifhSqXX.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/blockDiagram-VBNYF7ZC-DRgfNeNW.js +132 -0
- package/node_modules/@codebridge/web/dist/assets/c4Diagram-5PPSVZJV-BtOGKTh8.js +10 -0
- package/node_modules/@codebridge/web/dist/assets/channel-MhXyjxUS.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-2GRJ4B5K-Dy1gJ7sY.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-2Q5K7J3B-C1jixKkw.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-4I5QYGJK-1pOLR_SG.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-5RXB4S5H-CP43rgsO.js +231 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-5VM5RSS4-ZNzvKenW.js +15 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-6Q2QTUOP-Bi3Xrp6W.js +88 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-7BUUIJ7U-Bb538aSH.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-7Z6QIM7H-CILAW2Nk.js +10 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-GF5L2VYU-CAp09yhq.js +206 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-I66GZJ75-B_rJXmNu.js +128 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-JQJVKLGR-OVeOJFCx.js +156 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-JWPE2WC7-DVXcaiue.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-KBJHAD2P-uaupp0k5.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-KEIR6QF5-BICK3FdT.js +161 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-NSK5VX7P-fIHF5wwn.js +2 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-QR6OTTB3-DxkJuF2b.js +62 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-RYQCIY6F-CC5j3u-X.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-UBXNYLIW-DB35O-6R.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-W5SLKNZC-bkRBsbq-.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-WRU74C26-DwvEoD-1.js +70 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-XXDRQBXY-DRyYA-HY.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/chunk-Y2CYZVJY-DsF7k-Jl.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/classDiagram-JCYQIIEL-B8r7z8ii.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/classDiagram-v2-OCEON4UE-B8r7z8ii.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/cose-bilkent-JH36ORCC-CLkwlrX7.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/cynefin-VYW2F7L2-C3_Llw02.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/cynefinDiagram-MW4NZA55-D5v7UnJy.js +62 -0
- package/node_modules/@codebridge/web/dist/assets/cytoscape.esm-B-NFISlW.js +321 -0
- package/node_modules/@codebridge/web/dist/assets/dagre-Buvkdvvj.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/dagre-VZM6K2ZE-Cr0IibwW.js +4 -0
- package/node_modules/@codebridge/web/dist/assets/defaultLocale-BFoDCU3G.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/diagram-7IWD3JNH-CpQXDIzA.js +30 -0
- package/node_modules/@codebridge/web/dist/assets/diagram-B4RE2ZJO-VaOkH0Ya.js +3 -0
- package/node_modules/@codebridge/web/dist/assets/diagram-LBJQPF4R-D0iXyNks.js +24 -0
- package/node_modules/@codebridge/web/dist/assets/diagram-Q27KOJAE-CtqYk01P.js +24 -0
- package/node_modules/@codebridge/web/dist/assets/diagram-UB23O5K3-CFnd3H1_.js +41 -0
- package/node_modules/@codebridge/web/dist/assets/dist-D2L7nRdk.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/ebnfDiagram-BXEA7PRR-B0GIbJYt.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/erDiagram-JOGREHBK-BApaLGVA.js +85 -0
- package/node_modules/@codebridge/web/dist/assets/eventmodeling-45OFAUF4-DqGGjcld.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/flowDiagram-UKHOOZJN-I_qlizLH.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/ganttDiagram-PKOTCBZU-B-QZ-J82.js +292 -0
- package/node_modules/@codebridge/web/dist/assets/gitGraph-TEB2WS4Q-DK2SJ2Ne.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/gitGraphDiagram-DS77QQ5N-B6Z-pu0D.js +106 -0
- package/node_modules/@codebridge/web/dist/assets/graphlib-DS17s2tU.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/index-8BzpyygS.js +559 -0
- package/node_modules/@codebridge/web/dist/assets/index-Bzaz89LE.css +1 -0
- package/node_modules/@codebridge/web/dist/assets/info-DKCQHKI2-CGWxt_Py.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/infoDiagram-6WML65LV-77XjrPZs.js +2 -0
- package/node_modules/@codebridge/web/dist/assets/init-C-OQMol4.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/ishikawaDiagram-WSZJBQD7-P1bZAz2x.js +70 -0
- package/node_modules/@codebridge/web/dist/assets/journeyDiagram-NVQOT4AX-C4rwMcBY.js +139 -0
- package/node_modules/@codebridge/web/dist/assets/kanban-definition-27J2QSJJ-mtw4a9bc.js +89 -0
- package/node_modules/@codebridge/web/dist/assets/line-DP4RkYqm.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/linear-DMg3PPio.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/map-BaFkSB1l.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/mermaid-parser.core-HY84kHw1.js +7 -0
- package/node_modules/@codebridge/web/dist/assets/mermaid.core-DdgCdsnM.js +42 -0
- package/node_modules/@codebridge/web/dist/assets/mindmap-definition-FAOFIHXS-Cw1EnbhZ.js +96 -0
- package/node_modules/@codebridge/web/dist/assets/ordinal-BDEzSJ7C.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/packet-7NZHBO7P-CEbrbJH3.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/path-fybaL0A-.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/pegDiagram-VL7TDLO6-CEc1Gedb.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/pie-RZYD4A2V-DPXVs-IW.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/pieDiagram-7S7Q4E2Y-hoQeGMFH.js +39 -0
- package/node_modules/@codebridge/web/dist/assets/quadrantDiagram-CIZ2JOQS-C-Nmxx0f.js +7 -0
- package/node_modules/@codebridge/web/dist/assets/radar-I7S5WNFK-BVd3X3Pd.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/railroad-3IZDKUUU-CxLxzr_v.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/railroad-abnf-AHOZXSZD-MA1zPI84.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/railroad-ebnf-EBAXGLYW-Bqug_umD.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/railroad-peg-LSFZ7HO6-DRcRFwpq.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/railroadDiagram-AXF67PYL-B007ZkL9.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/requirementDiagram-LRYGKXZP-LrTCcEi5.js +84 -0
- package/node_modules/@codebridge/web/dist/assets/rough.esm-Dy-Kn_BL.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/sankeyDiagram-W5VNT64P-Ch4mzHck.js +40 -0
- package/node_modules/@codebridge/web/dist/assets/sequenceDiagram-SI44F4Z6-CHg4OzLX.js +162 -0
- package/node_modules/@codebridge/web/dist/assets/sizeCapture-X5ZJPWSS-B0uUizjq.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/src-BrO6_odD.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/stateDiagram-OKZ733FA-7cVkr5nR.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/stateDiagram-v2-UEYNNEHI-CTy_mAXb.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/swimlanes-SLNWSIFB-BwSVF5qy.js +2 -0
- package/node_modules/@codebridge/web/dist/assets/swimlanesDiagram-ULZ7WXOC-BBQnfZ1j.js +8 -0
- package/node_modules/@codebridge/web/dist/assets/timeline-definition-Z64GVDOM-BaHEkVDV.js +120 -0
- package/node_modules/@codebridge/web/dist/assets/treeView-QDETBFTQ-CF0kLN62.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/treemap-6X3UGDF4-BB8A8uux.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/vennDiagram-T6HMQDX7-BFK12Iro.js +34 -0
- package/node_modules/@codebridge/web/dist/assets/wardley-OPB4EBWU-C_3y61zk.js +1 -0
- package/node_modules/@codebridge/web/dist/assets/wardleyDiagram-T6FBY63Y-W2oGu4WK.js +78 -0
- package/node_modules/@codebridge/web/dist/assets/xychartDiagram-ELKLHX3M-BiVRnbvz.js +7 -0
- package/node_modules/@codebridge/web/dist/brand/agent-logo-carbon.svg +1 -0
- package/node_modules/@codebridge/web/dist/brand/agent-logo.svg +1 -0
- package/node_modules/@codebridge/web/dist/brand/agnet-logo-carbon.svg +1 -0
- package/node_modules/@codebridge/web/dist/brand/agnet-logo.svg +1 -0
- package/node_modules/@codebridge/web/dist/brand/agnet-mark-carbon.svg +1 -0
- package/node_modules/@codebridge/web/dist/brand/agnet-mark.svg +1 -0
- package/node_modules/@codebridge/web/dist/fonts/CommitMono-400-Regular.otf +0 -0
- package/node_modules/@codebridge/web/dist/fonts/CommitMono-700-Regular.otf +0 -0
- package/node_modules/@codebridge/web/dist/fonts/CommitMono-OFL.txt +21 -0
- package/node_modules/@codebridge/web/dist/fonts/DepartureMono-OFL.txt +21 -0
- package/node_modules/@codebridge/web/dist/fonts/DepartureMono-Regular.woff2 +0 -0
- package/node_modules/@codebridge/web/dist/index.html +25 -0
- package/node_modules/@codebridge/web/package.json +7 -0
- package/node_modules/@codebridge/work-items/dist/index.d.ts +371 -0
- package/node_modules/@codebridge/work-items/dist/index.js +1784 -0
- package/node_modules/@codebridge/work-items/dist/session-projector.d.ts +4 -0
- package/node_modules/@codebridge/work-items/dist/session-projector.js +506 -0
- package/node_modules/@codebridge/work-items/dist/session-runtime.d.ts +274 -0
- package/node_modules/@codebridge/work-items/dist/session-runtime.js +1103 -0
- package/node_modules/@codebridge/work-items/dist/session-schema.d.ts +9 -0
- package/node_modules/@codebridge/work-items/dist/session-schema.js +292 -0
- package/node_modules/@codebridge/work-items/package.json +20 -0
- package/package.json +75 -4
|
@@ -0,0 +1,1103 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { projectSessionEvent } from "./session-projector.js";
|
|
3
|
+
export function parseSessionTurnMessage(value) {
|
|
4
|
+
const message = JSON.parse(value);
|
|
5
|
+
if (message.executionKind !== "agent" && message.executionKind !== "flow") {
|
|
6
|
+
throw new Error("Invalid Session Turn execution kind");
|
|
7
|
+
}
|
|
8
|
+
return message;
|
|
9
|
+
}
|
|
10
|
+
const transactionBrand = Symbol("codebridge.session-transaction");
|
|
11
|
+
export function createSqliteSessionRuntimeTransaction(database) {
|
|
12
|
+
let active = true;
|
|
13
|
+
const assertActive = () => {
|
|
14
|
+
// active 闭包防「事务结束后继续用」;database.isTransaction 防「根本没进事务就调工厂」
|
|
15
|
+
if (!active || !database.isTransaction) {
|
|
16
|
+
throw new Error("session operation requires an active transaction");
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const transaction = {
|
|
20
|
+
[transactionBrand]: true,
|
|
21
|
+
getRuntime(sessionId) {
|
|
22
|
+
const row = database
|
|
23
|
+
.prepare("SELECT * FROM session_runtime WHERE session_id = ?")
|
|
24
|
+
.get(sessionId);
|
|
25
|
+
return row ? toSessionRuntime(row) : undefined;
|
|
26
|
+
},
|
|
27
|
+
ensureRuntime(sessionId) {
|
|
28
|
+
assertActive();
|
|
29
|
+
const existing = transaction.getRuntime(sessionId);
|
|
30
|
+
if (existing)
|
|
31
|
+
return existing;
|
|
32
|
+
const now = new Date().toISOString();
|
|
33
|
+
database
|
|
34
|
+
.prepare(`INSERT INTO session_runtime (
|
|
35
|
+
session_id, active_run_id, queue_state, queue_pause_reason,
|
|
36
|
+
last_event_sequence, version, updated_at
|
|
37
|
+
) VALUES (?, NULL, 'ready', NULL, 0, 1, ?)`)
|
|
38
|
+
.run(sessionId, now);
|
|
39
|
+
return transaction.getRuntime(sessionId);
|
|
40
|
+
},
|
|
41
|
+
getOrCreateWorkItem(sessionId, input) {
|
|
42
|
+
assertActive();
|
|
43
|
+
const existing = database
|
|
44
|
+
.prepare("SELECT id FROM work_items WHERE session_id = ?")
|
|
45
|
+
.get(sessionId);
|
|
46
|
+
if (existing?.id) {
|
|
47
|
+
if (input.identifiers !== undefined) {
|
|
48
|
+
database
|
|
49
|
+
.prepare(`UPDATE work_items
|
|
50
|
+
SET identifiers = ?, updated_at = ?
|
|
51
|
+
WHERE id = ?`)
|
|
52
|
+
.run(JSON.stringify(input.identifiers), new Date().toISOString(), String(existing.id));
|
|
53
|
+
}
|
|
54
|
+
transaction.ensureRuntime(sessionId);
|
|
55
|
+
return String(existing.id);
|
|
56
|
+
}
|
|
57
|
+
const workItemId = createId("wi");
|
|
58
|
+
const now = new Date().toISOString();
|
|
59
|
+
database
|
|
60
|
+
.prepare(`INSERT INTO work_items (
|
|
61
|
+
id, schema_version, title, status, mode, conversation_id,
|
|
62
|
+
session_id, agent_id, workflow_id, workflow_revision,
|
|
63
|
+
workspace_scope, identifiers, context_revision, risk_level,
|
|
64
|
+
created_at, updated_at
|
|
65
|
+
) VALUES (?, 1, ?, 'created', ?, ?, ?, ?, NULL, NULL, ?, ?, 1, ?, ?, ?)`)
|
|
66
|
+
.run(workItemId, input.title, input.mode, input.conversationId, sessionId, input.agentId, JSON.stringify(input.workspaceScope), JSON.stringify(input.identifiers ?? {}), input.riskLevel, now, now);
|
|
67
|
+
transaction.ensureRuntime(sessionId);
|
|
68
|
+
transaction.appendEvent({
|
|
69
|
+
workItemId,
|
|
70
|
+
sessionId,
|
|
71
|
+
type: "WORK_ITEM_CREATED",
|
|
72
|
+
actor: "system",
|
|
73
|
+
});
|
|
74
|
+
return workItemId;
|
|
75
|
+
},
|
|
76
|
+
insertMessageAttachment(input) {
|
|
77
|
+
assertActive();
|
|
78
|
+
const now = new Date().toISOString();
|
|
79
|
+
const content = Buffer.from(input.dataBase64, "base64");
|
|
80
|
+
const contentHash = `sha256:${createHash("sha256")
|
|
81
|
+
.update(content)
|
|
82
|
+
.digest("hex")}`;
|
|
83
|
+
database
|
|
84
|
+
.prepare(`INSERT INTO message_attachments (
|
|
85
|
+
id, schema_version, work_item_id, name, mime_type,
|
|
86
|
+
data_base64, byte_size, content_hash, created_at
|
|
87
|
+
) VALUES (?, 1, ?, ?, ?, ?, ?, ?, ?)`)
|
|
88
|
+
.run(input.id, input.workItemId, input.name, input.mimeType, input.dataBase64, content.byteLength, contentHash, now);
|
|
89
|
+
return {
|
|
90
|
+
schemaVersion: 1,
|
|
91
|
+
id: input.id,
|
|
92
|
+
workItemId: input.workItemId,
|
|
93
|
+
name: input.name,
|
|
94
|
+
mimeType: input.mimeType,
|
|
95
|
+
dataBase64: input.dataBase64,
|
|
96
|
+
byteSize: content.byteLength,
|
|
97
|
+
contentHash,
|
|
98
|
+
createdAt: now,
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
getIdempotencyResponse(namespace, key) {
|
|
102
|
+
const row = database
|
|
103
|
+
.prepare(`SELECT response FROM idempotency_responses
|
|
104
|
+
WHERE namespace = ? AND idempotency_key = ?`)
|
|
105
|
+
.get(namespace, key);
|
|
106
|
+
return row?.response
|
|
107
|
+
? JSON.parse(row.response)
|
|
108
|
+
: undefined;
|
|
109
|
+
},
|
|
110
|
+
putIdempotencyResponse(namespace, key, response) {
|
|
111
|
+
assertActive();
|
|
112
|
+
database
|
|
113
|
+
.prepare(`INSERT OR IGNORE INTO idempotency_responses (
|
|
114
|
+
namespace, idempotency_key, response, created_at
|
|
115
|
+
) VALUES (?, ?, ?, ?)`)
|
|
116
|
+
.run(namespace, key, JSON.stringify(response), new Date().toISOString());
|
|
117
|
+
},
|
|
118
|
+
countQueuedTurns(sessionId) {
|
|
119
|
+
const row = database
|
|
120
|
+
.prepare(`SELECT COUNT(*) AS count FROM session_turns
|
|
121
|
+
WHERE session_id = ? AND status = 'queued'`)
|
|
122
|
+
.get(sessionId);
|
|
123
|
+
return Number(row?.count ?? 0);
|
|
124
|
+
},
|
|
125
|
+
insertTurn(sessionId, message) {
|
|
126
|
+
assertActive();
|
|
127
|
+
transaction.ensureRuntime(sessionId);
|
|
128
|
+
const position = database
|
|
129
|
+
.prepare(`SELECT COALESCE(MAX(queue_position), 0) + 1 AS next_position
|
|
130
|
+
FROM session_turns WHERE session_id = ?`)
|
|
131
|
+
.get(sessionId);
|
|
132
|
+
const now = new Date().toISOString();
|
|
133
|
+
const turnId = createId("turn");
|
|
134
|
+
database
|
|
135
|
+
.prepare(`INSERT INTO session_turns (
|
|
136
|
+
turn_id, session_id, queue_position, status, message_json,
|
|
137
|
+
version, dispatched_run_id, created_at, dispatched_at,
|
|
138
|
+
cancelled_at
|
|
139
|
+
) VALUES (?, ?, ?, 'queued', ?, 1, NULL, ?, NULL, NULL)`)
|
|
140
|
+
.run(turnId, sessionId, Number(position?.next_position ?? 1), JSON.stringify(message), now);
|
|
141
|
+
return transaction.getTurn(turnId);
|
|
142
|
+
},
|
|
143
|
+
getTurn(turnId) {
|
|
144
|
+
const row = database
|
|
145
|
+
.prepare("SELECT * FROM session_turns WHERE turn_id = ?")
|
|
146
|
+
.get(turnId);
|
|
147
|
+
return row ? toSessionTurn(row) : undefined;
|
|
148
|
+
},
|
|
149
|
+
nextQueuedTurn(sessionId) {
|
|
150
|
+
const row = database
|
|
151
|
+
.prepare(`SELECT * FROM session_turns
|
|
152
|
+
WHERE session_id = ? AND status = 'queued'
|
|
153
|
+
ORDER BY queue_position ASC
|
|
154
|
+
LIMIT 1`)
|
|
155
|
+
.get(sessionId);
|
|
156
|
+
return row ? toSessionTurn(row) : undefined;
|
|
157
|
+
},
|
|
158
|
+
getRun(runId) {
|
|
159
|
+
const row = database
|
|
160
|
+
.prepare("SELECT * FROM runs WHERE id = ?")
|
|
161
|
+
.get(runId);
|
|
162
|
+
return row ? toRun(row) : undefined;
|
|
163
|
+
},
|
|
164
|
+
getWorkItemForSession(sessionId) {
|
|
165
|
+
const row = database
|
|
166
|
+
.prepare(`SELECT id, mode, agent_id FROM work_items
|
|
167
|
+
WHERE session_id = ?`)
|
|
168
|
+
.get(sessionId);
|
|
169
|
+
return row
|
|
170
|
+
? {
|
|
171
|
+
id: String(row.id),
|
|
172
|
+
mode: String(row.mode),
|
|
173
|
+
agentId: nullableString(row.agent_id),
|
|
174
|
+
}
|
|
175
|
+
: undefined;
|
|
176
|
+
},
|
|
177
|
+
dispatchTurn(turnId, input) {
|
|
178
|
+
assertActive();
|
|
179
|
+
const turn = transaction.getTurn(turnId);
|
|
180
|
+
if (!turn)
|
|
181
|
+
throw new Error(`Turn not found: ${turnId}`);
|
|
182
|
+
if (turn.status !== "queued") {
|
|
183
|
+
throw new Error(`Turn is not queued: ${turnId}`);
|
|
184
|
+
}
|
|
185
|
+
if (turn.sessionId !== input.sessionId) {
|
|
186
|
+
throw new Error("Turn Session does not match Run Session");
|
|
187
|
+
}
|
|
188
|
+
const workItem = database
|
|
189
|
+
.prepare(`SELECT agent_id, session_id FROM work_items WHERE id = ?`)
|
|
190
|
+
.get(input.workItemId);
|
|
191
|
+
if (!workItem) {
|
|
192
|
+
throw new Error(`WorkItem not found: ${input.workItemId}`);
|
|
193
|
+
}
|
|
194
|
+
if (String(workItem.session_id) !== input.sessionId) {
|
|
195
|
+
throw new Error("WorkItem Session does not match Run Session");
|
|
196
|
+
}
|
|
197
|
+
const now = new Date().toISOString();
|
|
198
|
+
// R3:runs.agent_id 以 WorkItem 为准;input.agentId 有值必须相等,禁止 ?? 选边。
|
|
199
|
+
const workItemAgentId = nullableString(workItem.agent_id);
|
|
200
|
+
const agentId = resolveRunAgentId(input.agentId, workItemAgentId);
|
|
201
|
+
const frozenPlan = turn.message.plan;
|
|
202
|
+
if (frozenPlan) {
|
|
203
|
+
if (input.planId !== frozenPlan.planId) {
|
|
204
|
+
throw new Error("Turn Plan does not match Run Plan");
|
|
205
|
+
}
|
|
206
|
+
const existingPlan = database
|
|
207
|
+
.prepare("SELECT run_id FROM plans WHERE plan_id = ?")
|
|
208
|
+
.get(frozenPlan.planId);
|
|
209
|
+
if (existingPlan?.run_id
|
|
210
|
+
&& existingPlan.run_id !== input.id) {
|
|
211
|
+
const prior = transaction.getRun(String(existingPlan.run_id));
|
|
212
|
+
const terminal = prior && [
|
|
213
|
+
"succeeded",
|
|
214
|
+
"failed",
|
|
215
|
+
"cancelled",
|
|
216
|
+
"interrupted",
|
|
217
|
+
].includes(prior.status);
|
|
218
|
+
if (!terminal) {
|
|
219
|
+
throw new Error(`Plan ${frozenPlan.planId} is bound to another Run`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
database
|
|
223
|
+
.prepare(`INSERT INTO plans (
|
|
224
|
+
plan_id, schema_version, source, workflow_id,
|
|
225
|
+
definition_revision, plan_ir_hash, session_id, run_id, steps,
|
|
226
|
+
created_at
|
|
227
|
+
) VALUES (?, 1, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
228
|
+
ON CONFLICT(plan_id) DO UPDATE SET
|
|
229
|
+
source = excluded.source,
|
|
230
|
+
workflow_id = excluded.workflow_id,
|
|
231
|
+
definition_revision = excluded.definition_revision,
|
|
232
|
+
plan_ir_hash = excluded.plan_ir_hash,
|
|
233
|
+
session_id = excluded.session_id,
|
|
234
|
+
run_id = excluded.run_id,
|
|
235
|
+
steps = excluded.steps`)
|
|
236
|
+
.run(frozenPlan.planId, frozenPlan.source, frozenPlan.workflowId, frozenPlan.definitionRevision, frozenPlan.planIrHash, input.sessionId, input.id, JSON.stringify(frozenPlan.steps), now);
|
|
237
|
+
}
|
|
238
|
+
else if (input.planId) {
|
|
239
|
+
const existingPlan = database
|
|
240
|
+
.prepare("SELECT plan_id FROM plans WHERE plan_id = ?")
|
|
241
|
+
.get(input.planId);
|
|
242
|
+
if (!existingPlan)
|
|
243
|
+
throw new Error(`Plan not found: ${input.planId}`);
|
|
244
|
+
}
|
|
245
|
+
database
|
|
246
|
+
.prepare(`INSERT INTO runs (
|
|
247
|
+
id, schema_version, work_item_id, session_id, turn_id, mode,
|
|
248
|
+
status, execution_kind, agent_id, plan_id, plan_ir_hash, workflow_revision,
|
|
249
|
+
terminal_reason, replay_safety, lease_owner, lease_expires_at,
|
|
250
|
+
cancel_requested_at, cancel_deadline_at, provider_session_id,
|
|
251
|
+
created_at, updated_at
|
|
252
|
+
) VALUES (?, 1, ?, ?, ?, ?, 'queued', ?, ?, ?, ?, ?, NULL, 'safe',
|
|
253
|
+
NULL, NULL, NULL, NULL, ?, ?, ?)`)
|
|
254
|
+
.run(input.id, input.workItemId, input.sessionId, input.turnId, input.mode, input.executionKind, agentId, input.planId, input.planIrHash, input.workflowRevision, input.providerSessionId ?? null, now, now);
|
|
255
|
+
const result = database
|
|
256
|
+
.prepare(`UPDATE session_turns
|
|
257
|
+
SET status = 'dispatched', version = version + 1,
|
|
258
|
+
dispatched_run_id = ?, dispatched_at = ?
|
|
259
|
+
WHERE turn_id = ? AND status = 'queued'`)
|
|
260
|
+
.run(input.id, now, turnId);
|
|
261
|
+
if (Number(result.changes) !== 1) {
|
|
262
|
+
throw new Error(`Turn is not queued: ${turnId}`);
|
|
263
|
+
}
|
|
264
|
+
transaction.markDeliveryDispatched(turnId, input.id);
|
|
265
|
+
transaction.appendEvent({
|
|
266
|
+
workItemId: input.workItemId,
|
|
267
|
+
sessionId: input.sessionId,
|
|
268
|
+
runId: input.id,
|
|
269
|
+
type: "TURN_DISPATCHED",
|
|
270
|
+
actor: "system",
|
|
271
|
+
target: turnId,
|
|
272
|
+
payload: { turn_id: turnId },
|
|
273
|
+
});
|
|
274
|
+
transaction.appendEvent({
|
|
275
|
+
workItemId: input.workItemId,
|
|
276
|
+
sessionId: input.sessionId,
|
|
277
|
+
runId: input.id,
|
|
278
|
+
type: "MESSAGE_RECEIVED",
|
|
279
|
+
actor: "user",
|
|
280
|
+
target: turnId,
|
|
281
|
+
payload: {
|
|
282
|
+
message: turn.message.text,
|
|
283
|
+
attachment_ids: turn.message.attachmentIds,
|
|
284
|
+
actor_ref: turn.message.actorRef ?? null,
|
|
285
|
+
},
|
|
286
|
+
});
|
|
287
|
+
transaction.appendEvent({
|
|
288
|
+
workItemId: input.workItemId,
|
|
289
|
+
sessionId: input.sessionId,
|
|
290
|
+
runId: input.id,
|
|
291
|
+
type: "RUN_CREATED",
|
|
292
|
+
actor: "system",
|
|
293
|
+
target: input.id,
|
|
294
|
+
payload: {
|
|
295
|
+
mode: input.mode,
|
|
296
|
+
agent_id: input.agentId,
|
|
297
|
+
plan_id: input.planId,
|
|
298
|
+
workflow_revision: input.workflowRevision,
|
|
299
|
+
execution_kind: input.executionKind,
|
|
300
|
+
},
|
|
301
|
+
});
|
|
302
|
+
if (input.planId) {
|
|
303
|
+
const plan = database
|
|
304
|
+
.prepare("SELECT * FROM plans WHERE plan_id = ?")
|
|
305
|
+
.get(input.planId);
|
|
306
|
+
if (!plan)
|
|
307
|
+
throw new Error(`Plan not found: ${input.planId}`);
|
|
308
|
+
transaction.appendEvent({
|
|
309
|
+
workItemId: input.workItemId,
|
|
310
|
+
sessionId: input.sessionId,
|
|
311
|
+
runId: input.id,
|
|
312
|
+
type: "PLAN_VALIDATED",
|
|
313
|
+
actor: "system",
|
|
314
|
+
target: input.planId,
|
|
315
|
+
payload: {
|
|
316
|
+
workflow_id: plan.workflow_id,
|
|
317
|
+
definition_revision: plan.definition_revision,
|
|
318
|
+
source: plan.source,
|
|
319
|
+
step_count: JSON.parse(String(plan.steps)).length,
|
|
320
|
+
},
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
return {
|
|
324
|
+
turn: transaction.getTurn(turnId),
|
|
325
|
+
run: toRun(database
|
|
326
|
+
.prepare("SELECT * FROM runs WHERE id = ?")
|
|
327
|
+
.get(input.id)),
|
|
328
|
+
};
|
|
329
|
+
},
|
|
330
|
+
dispatchNextTurn(sessionId) {
|
|
331
|
+
assertActive();
|
|
332
|
+
const turn = transaction.nextQueuedTurn(sessionId);
|
|
333
|
+
if (!turn)
|
|
334
|
+
return null;
|
|
335
|
+
const workItem = transaction.getWorkItemForSession(sessionId);
|
|
336
|
+
if (!workItem) {
|
|
337
|
+
throw new Error(`Session WorkItem not found: ${sessionId}`);
|
|
338
|
+
}
|
|
339
|
+
const plan = turn.message.plan;
|
|
340
|
+
const dispatched = transaction.dispatchTurn(turn.turnId, {
|
|
341
|
+
id: createId("run"),
|
|
342
|
+
workItemId: workItem.id,
|
|
343
|
+
sessionId,
|
|
344
|
+
turnId: turn.turnId,
|
|
345
|
+
mode: workItem.mode,
|
|
346
|
+
executionKind: turn.message.executionKind,
|
|
347
|
+
agentId: workItem.agentId,
|
|
348
|
+
planId: plan?.planId ?? null,
|
|
349
|
+
planIrHash: plan?.planIrHash ?? null,
|
|
350
|
+
workflowRevision: plan?.definitionRevision ?? null,
|
|
351
|
+
providerSessionId: transaction.getSessionProviderSessionId(sessionId),
|
|
352
|
+
});
|
|
353
|
+
transaction.updateRuntime(sessionId, {
|
|
354
|
+
activeRunId: dispatched.run.id,
|
|
355
|
+
});
|
|
356
|
+
return dispatched;
|
|
357
|
+
},
|
|
358
|
+
cancelTurn(turnId, expectedVersion) {
|
|
359
|
+
assertActive();
|
|
360
|
+
const now = new Date().toISOString();
|
|
361
|
+
const result = database
|
|
362
|
+
.prepare(`UPDATE session_turns
|
|
363
|
+
SET status = 'cancelled', version = version + 1,
|
|
364
|
+
cancelled_at = ?
|
|
365
|
+
WHERE turn_id = ? AND status = 'queued' AND version = ?`)
|
|
366
|
+
.run(now, turnId, expectedVersion);
|
|
367
|
+
return Number(result.changes) === 1
|
|
368
|
+
? transaction.getTurn(turnId)
|
|
369
|
+
: undefined;
|
|
370
|
+
},
|
|
371
|
+
updateRun(runId, patch) {
|
|
372
|
+
assertActive();
|
|
373
|
+
const current = transaction.getRun(runId);
|
|
374
|
+
if (!current)
|
|
375
|
+
throw new Error(`Run not found: ${runId}`);
|
|
376
|
+
const next = {
|
|
377
|
+
...current,
|
|
378
|
+
...patch,
|
|
379
|
+
updatedAt: new Date().toISOString(),
|
|
380
|
+
};
|
|
381
|
+
database
|
|
382
|
+
.prepare(`UPDATE runs
|
|
383
|
+
SET status = ?, terminal_reason = ?, replay_safety = ?,
|
|
384
|
+
lease_owner = ?, lease_expires_at = ?,
|
|
385
|
+
cancel_requested_at = ?, cancel_deadline_at = ?,
|
|
386
|
+
provider_session_id = ?,
|
|
387
|
+
updated_at = ?
|
|
388
|
+
WHERE id = ?`)
|
|
389
|
+
.run(next.status, next.terminalReason, next.replaySafety, next.leaseOwner, next.leaseExpiresAt, next.cancelRequestedAt, next.cancelDeadlineAt, next.providerSessionId ?? null, next.updatedAt, runId);
|
|
390
|
+
return transaction.getRun(runId);
|
|
391
|
+
},
|
|
392
|
+
claimRun(runId, owner, now, expiresAt) {
|
|
393
|
+
assertActive();
|
|
394
|
+
const result = database
|
|
395
|
+
.prepare(`UPDATE runs
|
|
396
|
+
SET status = 'running', lease_owner = ?, lease_expires_at = ?,
|
|
397
|
+
updated_at = ?
|
|
398
|
+
WHERE id = ? AND status = 'queued' AND lease_owner IS NULL`)
|
|
399
|
+
.run(owner, expiresAt, now, runId);
|
|
400
|
+
return Number(result.changes) === 1
|
|
401
|
+
? transaction.getRun(runId) ?? null
|
|
402
|
+
: null;
|
|
403
|
+
},
|
|
404
|
+
renewRunLease(runId, owner, now, expiresAt) {
|
|
405
|
+
assertActive();
|
|
406
|
+
const result = database
|
|
407
|
+
.prepare(`UPDATE runs
|
|
408
|
+
SET lease_expires_at = ?, updated_at = ?
|
|
409
|
+
WHERE id = ? AND status = 'running' AND lease_owner = ?
|
|
410
|
+
AND lease_expires_at >= ?`)
|
|
411
|
+
.run(expiresAt, new Date().toISOString(), runId, owner, now);
|
|
412
|
+
return Number(result.changes) === 1
|
|
413
|
+
? transaction.getRun(runId) ?? null
|
|
414
|
+
: null;
|
|
415
|
+
},
|
|
416
|
+
reclaimRunLease(runId, owner, expiresAt) {
|
|
417
|
+
assertActive();
|
|
418
|
+
// 与 renewRunLease 相同,但不要求 lease_expires_at >= now:允许 Run 的
|
|
419
|
+
// 原持有者在自己仍在执行(isExecuting)时,把已过期但尚未被恢复扫描
|
|
420
|
+
// 接管的租约续回来,而不是被自己误判中断。
|
|
421
|
+
const result = database
|
|
422
|
+
.prepare(`UPDATE runs
|
|
423
|
+
SET lease_expires_at = ?, updated_at = ?
|
|
424
|
+
WHERE id = ? AND status = 'running' AND lease_owner = ?`)
|
|
425
|
+
.run(expiresAt, new Date().toISOString(), runId, owner);
|
|
426
|
+
return Number(result.changes) === 1
|
|
427
|
+
? transaction.getRun(runId) ?? null
|
|
428
|
+
: null;
|
|
429
|
+
},
|
|
430
|
+
listExpiredRunningRuns(now, limit) {
|
|
431
|
+
const boundedLimit = Math.max(1, Math.min(1_000, Math.floor(limit)));
|
|
432
|
+
const rows = database
|
|
433
|
+
.prepare(`SELECT * FROM runs
|
|
434
|
+
WHERE status = 'running'
|
|
435
|
+
AND lease_expires_at IS NOT NULL
|
|
436
|
+
AND lease_expires_at < ?
|
|
437
|
+
ORDER BY lease_expires_at ASC
|
|
438
|
+
LIMIT ?`)
|
|
439
|
+
.all(now, boundedLimit);
|
|
440
|
+
return rows.map(toRun);
|
|
441
|
+
},
|
|
442
|
+
listCancellationDeadlineRuns(now, limit) {
|
|
443
|
+
const boundedLimit = Math.max(1, Math.min(1_000, Math.floor(limit)));
|
|
444
|
+
const rows = database
|
|
445
|
+
.prepare(`SELECT * FROM runs
|
|
446
|
+
WHERE status = 'running'
|
|
447
|
+
AND cancel_deadline_at IS NOT NULL
|
|
448
|
+
AND cancel_deadline_at <= ?
|
|
449
|
+
ORDER BY cancel_deadline_at ASC
|
|
450
|
+
LIMIT ?`)
|
|
451
|
+
.all(now, boundedLimit);
|
|
452
|
+
return rows.map(toRun);
|
|
453
|
+
},
|
|
454
|
+
updateRuntime(sessionId, patch) {
|
|
455
|
+
assertActive();
|
|
456
|
+
const current = transaction.ensureRuntime(sessionId);
|
|
457
|
+
const next = {
|
|
458
|
+
activeRunId: patch.activeRunId === undefined
|
|
459
|
+
? current.activeRunId
|
|
460
|
+
: patch.activeRunId,
|
|
461
|
+
queueState: patch.queueState ?? current.queueState,
|
|
462
|
+
queuePauseReason: patch.queuePauseReason === undefined
|
|
463
|
+
? current.queuePauseReason
|
|
464
|
+
: patch.queuePauseReason,
|
|
465
|
+
};
|
|
466
|
+
database
|
|
467
|
+
.prepare(`UPDATE session_runtime
|
|
468
|
+
SET active_run_id = ?, queue_state = ?, queue_pause_reason = ?,
|
|
469
|
+
version = version + 1, updated_at = ?
|
|
470
|
+
WHERE session_id = ?`)
|
|
471
|
+
.run(next.activeRunId, next.queueState, next.queuePauseReason, new Date().toISOString(), sessionId);
|
|
472
|
+
return transaction.getRuntime(sessionId);
|
|
473
|
+
},
|
|
474
|
+
getProviderHistoryImportCursor(sessionId, providerSessionId) {
|
|
475
|
+
const row = database
|
|
476
|
+
.prepare(`SELECT provider_digest, imported_position
|
|
477
|
+
FROM provider_history_imports
|
|
478
|
+
WHERE session_id = ? AND provider_session_id = ?`)
|
|
479
|
+
.get(sessionId, providerSessionId);
|
|
480
|
+
return row
|
|
481
|
+
? {
|
|
482
|
+
providerDigest: String(row.provider_digest),
|
|
483
|
+
importedPosition: Number(row.imported_position),
|
|
484
|
+
}
|
|
485
|
+
: undefined;
|
|
486
|
+
},
|
|
487
|
+
nextSessionQueuePosition(sessionId) {
|
|
488
|
+
const row = database
|
|
489
|
+
.prepare(`SELECT COALESCE(MAX(queue_position), 0) + 1 AS next_position
|
|
490
|
+
FROM session_turns WHERE session_id = ?`)
|
|
491
|
+
.get(sessionId);
|
|
492
|
+
return Number(row?.next_position ?? 1);
|
|
493
|
+
},
|
|
494
|
+
insertImportedTurn(input) {
|
|
495
|
+
assertActive();
|
|
496
|
+
database
|
|
497
|
+
.prepare(`INSERT INTO session_turns (
|
|
498
|
+
turn_id, session_id, queue_position, status, message_json,
|
|
499
|
+
version, dispatched_run_id, created_at, dispatched_at,
|
|
500
|
+
cancelled_at
|
|
501
|
+
) VALUES (?, ?, ?, 'dispatched', ?, 2, ?, ?, ?, NULL)`)
|
|
502
|
+
.run(input.turnId, input.sessionId, input.queuePosition, JSON.stringify(input.message), input.runId, input.createdAt, input.dispatchedAt);
|
|
503
|
+
},
|
|
504
|
+
insertImportedRun(input) {
|
|
505
|
+
assertActive();
|
|
506
|
+
database
|
|
507
|
+
.prepare(`INSERT INTO runs (
|
|
508
|
+
id, schema_version, work_item_id, session_id, turn_id, mode,
|
|
509
|
+
status, execution_kind, agent_id, plan_id, plan_ir_hash, workflow_revision,
|
|
510
|
+
terminal_reason, replay_safety, lease_owner, lease_expires_at,
|
|
511
|
+
cancel_requested_at, cancel_deadline_at, created_at, updated_at
|
|
512
|
+
) VALUES (?, 1, ?, ?, ?, 'auto', 'running', 'agent', NULL, NULL, NULL,
|
|
513
|
+
NULL, NULL, 'safe', NULL, NULL, NULL, NULL, ?, ?)`)
|
|
514
|
+
.run(input.runId, input.workItemId, input.sessionId, input.turnId, input.createdAt, input.updatedAt);
|
|
515
|
+
},
|
|
516
|
+
upsertProviderHistoryImport(input) {
|
|
517
|
+
assertActive();
|
|
518
|
+
database
|
|
519
|
+
.prepare(`INSERT INTO provider_history_imports (
|
|
520
|
+
session_id, provider_session_id, provider_digest,
|
|
521
|
+
imported_position, imported_at
|
|
522
|
+
) VALUES (?, ?, ?, ?, ?)
|
|
523
|
+
ON CONFLICT(session_id, provider_session_id) DO UPDATE SET
|
|
524
|
+
provider_digest = excluded.provider_digest,
|
|
525
|
+
imported_position = excluded.imported_position,
|
|
526
|
+
imported_at = excluded.imported_at`)
|
|
527
|
+
.run(input.sessionId, input.providerSessionId, input.providerDigest, input.importedPosition, input.importedAt);
|
|
528
|
+
},
|
|
529
|
+
insertChannelDelivery(input) {
|
|
530
|
+
assertActive();
|
|
531
|
+
const now = new Date().toISOString();
|
|
532
|
+
database
|
|
533
|
+
.prepare(`INSERT INTO channel_turn_delivery (
|
|
534
|
+
turn_id, session_id, channel, conversation_id,
|
|
535
|
+
reply_to_message_id, show_thinking, surface_message_id, claim_owner,
|
|
536
|
+
claim_expires_at, accepted_sequence, run_id, run_terminal_at,
|
|
537
|
+
status, created_at, updated_at
|
|
538
|
+
) VALUES (?, ?, ?, ?, ?, ?, NULL, NULL, NULL, ?, ?, NULL, ?, ?, ?)`)
|
|
539
|
+
.run(input.turnId, input.sessionId, input.channel, input.conversationId, input.replyToMessageId, input.showThinking ? 1 : 0, input.acceptedSequence, input.runId, input.status, now, now);
|
|
540
|
+
},
|
|
541
|
+
markDeliveryDispatched(turnId, runId) {
|
|
542
|
+
assertActive();
|
|
543
|
+
const now = new Date().toISOString();
|
|
544
|
+
database
|
|
545
|
+
.prepare(`UPDATE channel_turn_delivery
|
|
546
|
+
SET run_id = ?, status = 'dispatched', updated_at = ?
|
|
547
|
+
WHERE turn_id = ?`)
|
|
548
|
+
.run(runId, now, turnId);
|
|
549
|
+
},
|
|
550
|
+
markDeliveryRunTerminal(runId, now) {
|
|
551
|
+
assertActive();
|
|
552
|
+
database
|
|
553
|
+
.prepare(`UPDATE channel_turn_delivery
|
|
554
|
+
SET run_terminal_at = ?, updated_at = ?
|
|
555
|
+
WHERE run_id = ?`)
|
|
556
|
+
.run(now, now, runId);
|
|
557
|
+
},
|
|
558
|
+
claimDelivery(turnId, owner, now, expiresAt) {
|
|
559
|
+
assertActive();
|
|
560
|
+
const result = database
|
|
561
|
+
.prepare(`UPDATE channel_turn_delivery
|
|
562
|
+
SET status = 'delivering', claim_owner = ?, claim_expires_at = ?,
|
|
563
|
+
updated_at = ?
|
|
564
|
+
WHERE turn_id = ?
|
|
565
|
+
AND (
|
|
566
|
+
(status = 'dispatched' AND run_id IS NOT NULL)
|
|
567
|
+
OR (
|
|
568
|
+
status = 'delivering'
|
|
569
|
+
AND surface_message_id IS NULL
|
|
570
|
+
AND (claim_expires_at IS NULL OR claim_expires_at <= ?)
|
|
571
|
+
)
|
|
572
|
+
)`)
|
|
573
|
+
.run(owner, expiresAt, now, turnId, now);
|
|
574
|
+
return Number(result.changes) === 1;
|
|
575
|
+
},
|
|
576
|
+
ackDelivery(turnId, owner, surfaceMessageId, surfaceCardId) {
|
|
577
|
+
assertActive();
|
|
578
|
+
const row = database
|
|
579
|
+
.prepare(`SELECT surface_message_id, surface_card_id, claim_owner, status
|
|
580
|
+
FROM channel_turn_delivery WHERE turn_id = ?`)
|
|
581
|
+
.get(turnId);
|
|
582
|
+
if (!row
|
|
583
|
+
|| row.status !== "delivering"
|
|
584
|
+
|| row.claim_owner !== owner) {
|
|
585
|
+
return false;
|
|
586
|
+
}
|
|
587
|
+
if (row.surface_message_id !== null
|
|
588
|
+
&& row.surface_message_id !== undefined) {
|
|
589
|
+
if (String(row.surface_message_id) !== surfaceMessageId)
|
|
590
|
+
return false;
|
|
591
|
+
if (row.surface_card_id !== null
|
|
592
|
+
&& row.surface_card_id !== undefined) {
|
|
593
|
+
return surfaceCardId === undefined
|
|
594
|
+
|| String(row.surface_card_id) === surfaceCardId;
|
|
595
|
+
}
|
|
596
|
+
if (surfaceCardId === undefined)
|
|
597
|
+
return true;
|
|
598
|
+
const now = new Date().toISOString();
|
|
599
|
+
const result = database
|
|
600
|
+
.prepare(`UPDATE channel_turn_delivery
|
|
601
|
+
SET surface_card_id = ?, updated_at = ?
|
|
602
|
+
WHERE turn_id = ? AND surface_card_id IS NULL`)
|
|
603
|
+
.run(surfaceCardId, now, turnId);
|
|
604
|
+
return Number(result.changes) === 1;
|
|
605
|
+
}
|
|
606
|
+
const now = new Date().toISOString();
|
|
607
|
+
database
|
|
608
|
+
.prepare(`UPDATE channel_turn_delivery
|
|
609
|
+
SET surface_message_id = ?, surface_card_id = ?, updated_at = ?
|
|
610
|
+
WHERE turn_id = ? AND surface_message_id IS NULL`)
|
|
611
|
+
.run(surfaceMessageId, surfaceCardId ?? null, now, turnId);
|
|
612
|
+
return true;
|
|
613
|
+
},
|
|
614
|
+
completeDelivery(turnId, owner) {
|
|
615
|
+
assertActive();
|
|
616
|
+
const row = database
|
|
617
|
+
.prepare(`SELECT status, claim_owner
|
|
618
|
+
FROM channel_turn_delivery WHERE turn_id = ?`)
|
|
619
|
+
.get(turnId);
|
|
620
|
+
if (!row)
|
|
621
|
+
return false;
|
|
622
|
+
if (row.status === "completed") {
|
|
623
|
+
return row.claim_owner === owner;
|
|
624
|
+
}
|
|
625
|
+
const now = new Date().toISOString();
|
|
626
|
+
const result = database
|
|
627
|
+
.prepare(`UPDATE channel_turn_delivery
|
|
628
|
+
SET status = 'completed', updated_at = ?
|
|
629
|
+
WHERE turn_id = ?
|
|
630
|
+
AND status = 'delivering'
|
|
631
|
+
AND claim_owner = ?
|
|
632
|
+
AND surface_message_id IS NOT NULL
|
|
633
|
+
AND run_terminal_at IS NOT NULL`)
|
|
634
|
+
.run(now, turnId, owner);
|
|
635
|
+
return Number(result.changes) === 1;
|
|
636
|
+
},
|
|
637
|
+
listDeliveries(channel, surfaceMessageId) {
|
|
638
|
+
const rows = database
|
|
639
|
+
.prepare(`SELECT d.*,
|
|
640
|
+
r.status AS run_snapshot_status,
|
|
641
|
+
r.created_at AS run_snapshot_created_at,
|
|
642
|
+
r.updated_at AS run_snapshot_updated_at,
|
|
643
|
+
r.lease_expires_at AS run_snapshot_lease_expires_at,
|
|
644
|
+
r.terminal_reason AS run_snapshot_terminal_reason,
|
|
645
|
+
sr.active_run_id AS run_snapshot_active_run_id,
|
|
646
|
+
sr.queue_state AS run_snapshot_queue_state
|
|
647
|
+
FROM channel_turn_delivery d
|
|
648
|
+
LEFT JOIN runs r ON r.id = d.run_id
|
|
649
|
+
LEFT JOIN session_runtime sr ON sr.session_id = d.session_id
|
|
650
|
+
WHERE d.channel = ? AND ${surfaceMessageId !== undefined
|
|
651
|
+
? "d.surface_message_id = ?" : "d.status != 'completed'"}
|
|
652
|
+
ORDER BY d.accepted_sequence ASC`)
|
|
653
|
+
.all(...(surfaceMessageId !== undefined ? [channel, surfaceMessageId] : [channel]));
|
|
654
|
+
return rows.map(toChannelDeliveryRow);
|
|
655
|
+
},
|
|
656
|
+
setSessionProviderSessionId(sessionId, providerSessionId) {
|
|
657
|
+
assertActive();
|
|
658
|
+
// 运行时行可能尚未创建(消息入口在 submitTurn 之前同步),先 ensure。
|
|
659
|
+
transaction.ensureRuntime(sessionId);
|
|
660
|
+
database
|
|
661
|
+
.prepare("UPDATE session_runtime SET provider_session_id = ?, updated_at = ? WHERE session_id = ?")
|
|
662
|
+
.run(providerSessionId, new Date().toISOString(), sessionId);
|
|
663
|
+
},
|
|
664
|
+
getSessionProviderSessionId(sessionId) {
|
|
665
|
+
const row = database
|
|
666
|
+
.prepare("SELECT provider_session_id FROM session_runtime WHERE session_id = ?")
|
|
667
|
+
.get(sessionId);
|
|
668
|
+
return row ? nullableString(row.provider_session_id) : null;
|
|
669
|
+
},
|
|
670
|
+
claimProviderSession(input) {
|
|
671
|
+
assertActive();
|
|
672
|
+
const result = database
|
|
673
|
+
.prepare(`INSERT INTO provider_session_leases (
|
|
674
|
+
agent_id, provider_session_id, lease_owner, lease_expires_at
|
|
675
|
+
) VALUES (?, ?, ?, ?)
|
|
676
|
+
ON CONFLICT(agent_id, provider_session_id) DO UPDATE SET
|
|
677
|
+
lease_owner = excluded.lease_owner,
|
|
678
|
+
lease_expires_at = excluded.lease_expires_at
|
|
679
|
+
WHERE provider_session_leases.lease_expires_at < ?
|
|
680
|
+
OR provider_session_leases.lease_owner = excluded.lease_owner`)
|
|
681
|
+
.run(input.agentId, input.providerSessionId, input.runId, input.expiresAt, input.now);
|
|
682
|
+
return Number(result.changes) === 1;
|
|
683
|
+
},
|
|
684
|
+
renewProviderSession(input) {
|
|
685
|
+
assertActive();
|
|
686
|
+
const result = database
|
|
687
|
+
.prepare(`UPDATE provider_session_leases
|
|
688
|
+
SET lease_expires_at = ?
|
|
689
|
+
WHERE agent_id = ? AND provider_session_id = ? AND lease_owner = ?`)
|
|
690
|
+
.run(input.expiresAt, input.agentId, input.providerSessionId, input.runId);
|
|
691
|
+
return Number(result.changes) === 1;
|
|
692
|
+
},
|
|
693
|
+
releaseProviderSession(input) {
|
|
694
|
+
assertActive();
|
|
695
|
+
const result = database
|
|
696
|
+
.prepare(`DELETE FROM provider_session_leases
|
|
697
|
+
WHERE agent_id = ? AND provider_session_id = ? AND lease_owner = ?`)
|
|
698
|
+
.run(input.agentId, input.providerSessionId, input.runId);
|
|
699
|
+
return Number(result.changes) === 1;
|
|
700
|
+
},
|
|
701
|
+
findLiveProviderLease(agentId, providerSessionId, now) {
|
|
702
|
+
const row = database
|
|
703
|
+
.prepare(`SELECT lease_owner FROM provider_session_leases
|
|
704
|
+
WHERE agent_id = ? AND provider_session_id = ? AND lease_expires_at >= ?`)
|
|
705
|
+
.get(agentId, providerSessionId, now);
|
|
706
|
+
return row ? { runId: String(row.lease_owner) } : undefined;
|
|
707
|
+
},
|
|
708
|
+
appendEvent(input) {
|
|
709
|
+
assertActive();
|
|
710
|
+
return appendSessionEventInTransaction(database, input);
|
|
711
|
+
},
|
|
712
|
+
};
|
|
713
|
+
return { transaction, deactivate: () => { active = false; } };
|
|
714
|
+
}
|
|
715
|
+
export function appendSessionEventInTransaction(database, input) {
|
|
716
|
+
if (!database.isTransaction) {
|
|
717
|
+
throw new Error("session operation requires an active transaction");
|
|
718
|
+
}
|
|
719
|
+
const workItem = database
|
|
720
|
+
.prepare("SELECT session_id FROM work_items WHERE id = ?")
|
|
721
|
+
.get(input.workItemId);
|
|
722
|
+
if (!workItem)
|
|
723
|
+
throw new Error(`WorkItem not found: ${input.workItemId}`);
|
|
724
|
+
if (workItem.session_id !== input.sessionId) {
|
|
725
|
+
throw new Error("WorkItem Session does not match event Session");
|
|
726
|
+
}
|
|
727
|
+
const executionKind = resolveRunExecutionKind(database, input.runId ?? null);
|
|
728
|
+
if (input.runId && isExecutionEvent(input.type)) {
|
|
729
|
+
const run = database
|
|
730
|
+
.prepare("SELECT status FROM runs WHERE id = ?")
|
|
731
|
+
.get(input.runId);
|
|
732
|
+
if (!run)
|
|
733
|
+
throw new Error(`Run not found: ${input.runId}`);
|
|
734
|
+
if (run.status === "succeeded"
|
|
735
|
+
|| run.status === "failed"
|
|
736
|
+
|| run.status === "cancelled"
|
|
737
|
+
|| run.status === "interrupted") {
|
|
738
|
+
throw new Error("terminal Run cannot accept execution events");
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
const sequenceRow = database
|
|
742
|
+
.prepare(`SELECT COALESCE(MAX(sequence), 0) + 1 AS next_sequence
|
|
743
|
+
FROM domain_events
|
|
744
|
+
WHERE work_item_id = ?`)
|
|
745
|
+
.get(input.workItemId);
|
|
746
|
+
const event = {
|
|
747
|
+
schemaVersion: 1,
|
|
748
|
+
eventId: createId("evt"),
|
|
749
|
+
sequence: Number(sequenceRow?.next_sequence ?? 1),
|
|
750
|
+
workItemId: input.workItemId,
|
|
751
|
+
runId: input.runId ?? null,
|
|
752
|
+
executionKind,
|
|
753
|
+
type: input.type,
|
|
754
|
+
occurredAt: new Date().toISOString(),
|
|
755
|
+
actor: input.actor,
|
|
756
|
+
target: input.target ?? null,
|
|
757
|
+
inputHash: input.inputHash ?? null,
|
|
758
|
+
resultRef: input.resultRef ?? null,
|
|
759
|
+
payload: { ...(input.payload ?? {}) },
|
|
760
|
+
};
|
|
761
|
+
database
|
|
762
|
+
.prepare(`INSERT INTO domain_events (
|
|
763
|
+
event_id, schema_version, sequence, work_item_id, run_id, execution_kind, type,
|
|
764
|
+
occurred_at, actor, target, input_hash, result_ref, payload
|
|
765
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
766
|
+
.run(event.eventId, event.schemaVersion, event.sequence, event.workItemId, event.runId, event.executionKind, event.type, event.occurredAt, event.actor, event.target, event.inputHash, event.resultRef, JSON.stringify(event.payload));
|
|
767
|
+
database
|
|
768
|
+
.prepare(`UPDATE session_runtime
|
|
769
|
+
SET last_event_sequence = ?, updated_at = ?
|
|
770
|
+
WHERE session_id = ?`)
|
|
771
|
+
.run(event.sequence, event.occurredAt, input.sessionId);
|
|
772
|
+
const workItemStatus = statusForEvent(event.type);
|
|
773
|
+
if (workItemStatus) {
|
|
774
|
+
database
|
|
775
|
+
.prepare("UPDATE work_items SET status = ?, updated_at = ? WHERE id = ?")
|
|
776
|
+
.run(workItemStatus, event.occurredAt, event.workItemId);
|
|
777
|
+
}
|
|
778
|
+
projectSessionEvent(database, input.sessionId, event);
|
|
779
|
+
return event;
|
|
780
|
+
}
|
|
781
|
+
export function resolveRunExecutionKind(database, runId) {
|
|
782
|
+
if (runId === null)
|
|
783
|
+
return null;
|
|
784
|
+
const row = database
|
|
785
|
+
.prepare("SELECT execution_kind FROM runs WHERE id = ?")
|
|
786
|
+
.get(runId);
|
|
787
|
+
if (!row)
|
|
788
|
+
throw new Error(`Run not found: ${runId}`);
|
|
789
|
+
if (row.execution_kind === "agent" || row.execution_kind === "flow") {
|
|
790
|
+
return row.execution_kind;
|
|
791
|
+
}
|
|
792
|
+
throw new Error(`Invalid Run execution kind: ${runId}`);
|
|
793
|
+
}
|
|
794
|
+
export function importProviderHistory(transaction, input) {
|
|
795
|
+
const namespace = `session:history-import:${input.sessionId}`;
|
|
796
|
+
const cached = transaction.getIdempotencyResponse(namespace, input.idempotencyKey);
|
|
797
|
+
if (cached)
|
|
798
|
+
return cached;
|
|
799
|
+
const cursor = transaction.getProviderHistoryImportCursor(input.sessionId, input.providerSessionId);
|
|
800
|
+
if (cursor
|
|
801
|
+
&& (cursor.importedPosition !== input.priorPosition
|
|
802
|
+
|| cursor.providerDigest !== input.priorDigest)) {
|
|
803
|
+
throw new Error("provider_history_cursor_conflict");
|
|
804
|
+
}
|
|
805
|
+
if (!cursor && input.priorPosition !== 0) {
|
|
806
|
+
throw new Error("provider_history_cursor_conflict");
|
|
807
|
+
}
|
|
808
|
+
const workItemId = transaction.getOrCreateWorkItem(input.sessionId, {
|
|
809
|
+
title: "Imported Session",
|
|
810
|
+
mode: "auto",
|
|
811
|
+
conversationId: `conv_${input.sessionId.replace(/^sess_/, "")}`,
|
|
812
|
+
agentId: null,
|
|
813
|
+
workspaceScope: [],
|
|
814
|
+
riskLevel: "read_only",
|
|
815
|
+
});
|
|
816
|
+
let active;
|
|
817
|
+
let importedTurns = 0;
|
|
818
|
+
const finishActive = () => {
|
|
819
|
+
if (!active)
|
|
820
|
+
return;
|
|
821
|
+
transaction.updateRun(active.runId, {
|
|
822
|
+
status: "succeeded",
|
|
823
|
+
leaseOwner: null,
|
|
824
|
+
leaseExpiresAt: null,
|
|
825
|
+
});
|
|
826
|
+
transaction.appendEvent({
|
|
827
|
+
workItemId,
|
|
828
|
+
sessionId: input.sessionId,
|
|
829
|
+
runId: active.runId,
|
|
830
|
+
type: "RUN_SUCCEEDED",
|
|
831
|
+
actor: "system",
|
|
832
|
+
target: active.runId,
|
|
833
|
+
payload: { imported: true },
|
|
834
|
+
});
|
|
835
|
+
transaction.updateRuntime(input.sessionId, {
|
|
836
|
+
activeRunId: null,
|
|
837
|
+
queueState: "ready",
|
|
838
|
+
queuePauseReason: null,
|
|
839
|
+
});
|
|
840
|
+
active = undefined;
|
|
841
|
+
};
|
|
842
|
+
const startTurn = (providerPosition, message) => {
|
|
843
|
+
const identity = createHash("sha256")
|
|
844
|
+
.update(`${input.providerSessionId}\0${providerPosition}`)
|
|
845
|
+
.digest("hex")
|
|
846
|
+
.slice(0, 32);
|
|
847
|
+
const turnId = `turn_import_${identity}`;
|
|
848
|
+
const runId = `run_import_${identity}`;
|
|
849
|
+
const now = new Date().toISOString();
|
|
850
|
+
const position = transaction.nextSessionQueuePosition(input.sessionId);
|
|
851
|
+
transaction.insertImportedTurn({
|
|
852
|
+
turnId,
|
|
853
|
+
sessionId: input.sessionId,
|
|
854
|
+
queuePosition: position,
|
|
855
|
+
message: {
|
|
856
|
+
text: message ?? "",
|
|
857
|
+
attachmentIds: [],
|
|
858
|
+
executionKind: "agent",
|
|
859
|
+
model: null,
|
|
860
|
+
effort: null,
|
|
861
|
+
permissionMode: null,
|
|
862
|
+
plan: null,
|
|
863
|
+
},
|
|
864
|
+
runId,
|
|
865
|
+
createdAt: now,
|
|
866
|
+
dispatchedAt: now,
|
|
867
|
+
});
|
|
868
|
+
transaction.insertImportedRun({
|
|
869
|
+
runId,
|
|
870
|
+
workItemId,
|
|
871
|
+
sessionId: input.sessionId,
|
|
872
|
+
turnId,
|
|
873
|
+
createdAt: now,
|
|
874
|
+
updatedAt: now,
|
|
875
|
+
});
|
|
876
|
+
transaction.updateRuntime(input.sessionId, {
|
|
877
|
+
activeRunId: runId,
|
|
878
|
+
queueState: "ready",
|
|
879
|
+
queuePauseReason: null,
|
|
880
|
+
});
|
|
881
|
+
transaction.appendEvent({
|
|
882
|
+
workItemId,
|
|
883
|
+
sessionId: input.sessionId,
|
|
884
|
+
runId,
|
|
885
|
+
type: "TURN_DISPATCHED",
|
|
886
|
+
actor: "system",
|
|
887
|
+
target: turnId,
|
|
888
|
+
payload: { turn_id: turnId, imported: true },
|
|
889
|
+
});
|
|
890
|
+
if (message !== undefined) {
|
|
891
|
+
transaction.appendEvent({
|
|
892
|
+
workItemId,
|
|
893
|
+
sessionId: input.sessionId,
|
|
894
|
+
runId,
|
|
895
|
+
type: "MESSAGE_RECEIVED",
|
|
896
|
+
actor: "user",
|
|
897
|
+
target: turnId,
|
|
898
|
+
payload: { message, attachment_ids: [], imported: true },
|
|
899
|
+
});
|
|
900
|
+
}
|
|
901
|
+
transaction.appendEvent({
|
|
902
|
+
workItemId,
|
|
903
|
+
sessionId: input.sessionId,
|
|
904
|
+
runId,
|
|
905
|
+
type: "RUN_CREATED",
|
|
906
|
+
actor: "system",
|
|
907
|
+
target: runId,
|
|
908
|
+
payload: { mode: "auto", imported: true },
|
|
909
|
+
});
|
|
910
|
+
active = { turnId, runId };
|
|
911
|
+
importedTurns += 1;
|
|
912
|
+
};
|
|
913
|
+
for (const [offset, entry] of input.events.entries()) {
|
|
914
|
+
const providerPosition = input.priorPosition + offset;
|
|
915
|
+
if (entry.kind === "message") {
|
|
916
|
+
finishActive();
|
|
917
|
+
startTurn(providerPosition, entry.text);
|
|
918
|
+
continue;
|
|
919
|
+
}
|
|
920
|
+
if (!active)
|
|
921
|
+
startTurn(providerPosition);
|
|
922
|
+
transaction.appendEvent({
|
|
923
|
+
workItemId,
|
|
924
|
+
sessionId: input.sessionId,
|
|
925
|
+
runId: active.runId,
|
|
926
|
+
type: "AGENT_EVENT",
|
|
927
|
+
actor: "agent",
|
|
928
|
+
target: String(entry.event.type ?? "agent_event"),
|
|
929
|
+
payload: { event: entry.event, imported: true },
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
finishActive();
|
|
933
|
+
const importedPosition = input.priorPosition + input.events.length;
|
|
934
|
+
transaction.upsertProviderHistoryImport({
|
|
935
|
+
sessionId: input.sessionId,
|
|
936
|
+
providerSessionId: input.providerSessionId,
|
|
937
|
+
providerDigest: input.nextDigest,
|
|
938
|
+
importedPosition,
|
|
939
|
+
importedAt: new Date().toISOString(),
|
|
940
|
+
});
|
|
941
|
+
const result = {
|
|
942
|
+
importedEvents: input.events.length,
|
|
943
|
+
importedTurns,
|
|
944
|
+
lastEventSequence: transaction.getRuntime(input.sessionId).lastEventSequence,
|
|
945
|
+
};
|
|
946
|
+
transaction.putIdempotencyResponse(namespace, input.idempotencyKey, result);
|
|
947
|
+
return result;
|
|
948
|
+
}
|
|
949
|
+
function statusForEvent(eventType) {
|
|
950
|
+
switch (eventType) {
|
|
951
|
+
case "PLAN_PROPOSED":
|
|
952
|
+
return "planned";
|
|
953
|
+
case "APPROVAL_REQUESTED":
|
|
954
|
+
return "awaiting_approval";
|
|
955
|
+
case "RUN_CANCELLED":
|
|
956
|
+
return "cancelled";
|
|
957
|
+
case "STEP_STARTED":
|
|
958
|
+
return "executing";
|
|
959
|
+
case "VERIFICATION_COMPLETED":
|
|
960
|
+
return "verifying";
|
|
961
|
+
case "STEP_FAILED":
|
|
962
|
+
return "failed";
|
|
963
|
+
case "WORK_ITEM_COMPLETED":
|
|
964
|
+
return "completed";
|
|
965
|
+
default:
|
|
966
|
+
return undefined;
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
function isExecutionEvent(eventType) {
|
|
970
|
+
return eventType === "AGENT_EVENT"
|
|
971
|
+
|| eventType === "APPROVAL_REQUESTED"
|
|
972
|
+
|| eventType === "STEP_STARTED"
|
|
973
|
+
|| eventType === "STEP_SUCCEEDED"
|
|
974
|
+
|| eventType === "STEP_SKIPPED"
|
|
975
|
+
|| eventType === "STEP_RETRYING"
|
|
976
|
+
|| eventType === "STEP_FAILED";
|
|
977
|
+
}
|
|
978
|
+
function toSessionRuntime(row) {
|
|
979
|
+
return {
|
|
980
|
+
sessionId: String(row.session_id),
|
|
981
|
+
activeRunId: nullableString(row.active_run_id),
|
|
982
|
+
queueState: String(row.queue_state),
|
|
983
|
+
queuePauseReason: nullableString(row.queue_pause_reason),
|
|
984
|
+
lastEventSequence: Number(row.last_event_sequence),
|
|
985
|
+
version: Number(row.version),
|
|
986
|
+
updatedAt: String(row.updated_at),
|
|
987
|
+
};
|
|
988
|
+
}
|
|
989
|
+
function toSessionTurn(row) {
|
|
990
|
+
return {
|
|
991
|
+
turnId: String(row.turn_id),
|
|
992
|
+
sessionId: String(row.session_id),
|
|
993
|
+
queuePosition: Number(row.queue_position),
|
|
994
|
+
status: String(row.status),
|
|
995
|
+
message: parseSessionTurnMessage(String(row.message_json)),
|
|
996
|
+
version: Number(row.version),
|
|
997
|
+
dispatchedRunId: nullableString(row.dispatched_run_id),
|
|
998
|
+
createdAt: String(row.created_at),
|
|
999
|
+
dispatchedAt: nullableString(row.dispatched_at),
|
|
1000
|
+
cancelledAt: nullableString(row.cancelled_at),
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
1003
|
+
function toRun(row) {
|
|
1004
|
+
return {
|
|
1005
|
+
schemaVersion: Number(row.schema_version),
|
|
1006
|
+
id: String(row.id),
|
|
1007
|
+
workItemId: String(row.work_item_id),
|
|
1008
|
+
sessionId: nullableString(row.session_id),
|
|
1009
|
+
turnId: nullableString(row.turn_id),
|
|
1010
|
+
mode: String(row.mode),
|
|
1011
|
+
status: String(row.status),
|
|
1012
|
+
executionKind: executionKind(row.execution_kind, String(row.id)),
|
|
1013
|
+
agentId: nullableString(row.agent_id),
|
|
1014
|
+
planId: nullableString(row.plan_id),
|
|
1015
|
+
planIrHash: nullableString(row.plan_ir_hash),
|
|
1016
|
+
workflowRevision: nullableString(row.workflow_revision),
|
|
1017
|
+
terminalReason: nullableString(row.terminal_reason),
|
|
1018
|
+
replaySafety: String(row.replay_safety),
|
|
1019
|
+
leaseOwner: nullableString(row.lease_owner),
|
|
1020
|
+
leaseExpiresAt: nullableString(row.lease_expires_at),
|
|
1021
|
+
cancelRequestedAt: nullableString(row.cancel_requested_at),
|
|
1022
|
+
cancelDeadlineAt: nullableString(row.cancel_deadline_at),
|
|
1023
|
+
providerSessionId: nullableString(row.provider_session_id),
|
|
1024
|
+
createdAt: String(row.created_at),
|
|
1025
|
+
updatedAt: String(row.updated_at),
|
|
1026
|
+
};
|
|
1027
|
+
}
|
|
1028
|
+
function executionKind(value, runId) {
|
|
1029
|
+
if (value === "agent" || value === "flow")
|
|
1030
|
+
return value;
|
|
1031
|
+
throw new Error(`Invalid Run execution kind: ${runId}`);
|
|
1032
|
+
}
|
|
1033
|
+
function nullableString(value) {
|
|
1034
|
+
return value === null || value === undefined ? null : String(value);
|
|
1035
|
+
}
|
|
1036
|
+
function channelRuntimeRunStatus(value) {
|
|
1037
|
+
switch (value) {
|
|
1038
|
+
case "queued":
|
|
1039
|
+
case "running":
|
|
1040
|
+
case "waiting":
|
|
1041
|
+
case "succeeded":
|
|
1042
|
+
case "failed":
|
|
1043
|
+
case "cancelled":
|
|
1044
|
+
case "interrupted":
|
|
1045
|
+
return value;
|
|
1046
|
+
default:
|
|
1047
|
+
throw new Error(`invalid channel Runtime Run status: ${String(value)}`);
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
function channelSessionQueueState(value) {
|
|
1051
|
+
if (value === "ready" || value === "paused")
|
|
1052
|
+
return value;
|
|
1053
|
+
throw new Error(`invalid channel Session queue state: ${String(value)}`);
|
|
1054
|
+
}
|
|
1055
|
+
function toChannelDeliveryRunSnapshot(row) {
|
|
1056
|
+
if (row.run_snapshot_status === null || row.run_snapshot_status === undefined) {
|
|
1057
|
+
return null;
|
|
1058
|
+
}
|
|
1059
|
+
return {
|
|
1060
|
+
status: channelRuntimeRunStatus(row.run_snapshot_status),
|
|
1061
|
+
createdAt: String(row.run_snapshot_created_at),
|
|
1062
|
+
updatedAt: String(row.run_snapshot_updated_at),
|
|
1063
|
+
leaseExpiresAt: nullableString(row.run_snapshot_lease_expires_at),
|
|
1064
|
+
terminalReason: nullableString(row.run_snapshot_terminal_reason),
|
|
1065
|
+
sessionActiveRunId: nullableString(row.run_snapshot_active_run_id),
|
|
1066
|
+
sessionQueueState: channelSessionQueueState(row.run_snapshot_queue_state),
|
|
1067
|
+
};
|
|
1068
|
+
}
|
|
1069
|
+
/** R3:runs.agent_id 以 WorkItem 为准;input.agentId 有值则必须相等。 */
|
|
1070
|
+
function resolveRunAgentId(requested, workItemAgentId) {
|
|
1071
|
+
const normalized = requested ?? null;
|
|
1072
|
+
if (normalized !== null
|
|
1073
|
+
&& workItemAgentId !== null
|
|
1074
|
+
&& normalized !== workItemAgentId) {
|
|
1075
|
+
throw new Error(`Run agent mismatch: requested ${normalized} but WorkItem agent is ${workItemAgentId}`);
|
|
1076
|
+
}
|
|
1077
|
+
return workItemAgentId ?? normalized;
|
|
1078
|
+
}
|
|
1079
|
+
function toChannelDeliveryRow(row) {
|
|
1080
|
+
return {
|
|
1081
|
+
turnId: String(row.turn_id),
|
|
1082
|
+
sessionId: String(row.session_id),
|
|
1083
|
+
channel: String(row.channel),
|
|
1084
|
+
conversationId: String(row.conversation_id),
|
|
1085
|
+
replyToMessageId: String(row.reply_to_message_id),
|
|
1086
|
+
showThinking: Number(row.show_thinking) === 1,
|
|
1087
|
+
surfaceMessageId: nullableString(row.surface_message_id),
|
|
1088
|
+
surfaceCardId: nullableString(row.surface_card_id),
|
|
1089
|
+
claimOwner: nullableString(row.claim_owner),
|
|
1090
|
+
claimExpiresAt: nullableString(row.claim_expires_at),
|
|
1091
|
+
acceptedSequence: Number(row.accepted_sequence),
|
|
1092
|
+
runId: nullableString(row.run_id),
|
|
1093
|
+
runTerminalAt: nullableString(row.run_terminal_at),
|
|
1094
|
+
status: String(row.status),
|
|
1095
|
+
createdAt: String(row.created_at),
|
|
1096
|
+
updatedAt: String(row.updated_at),
|
|
1097
|
+
runSnapshot: toChannelDeliveryRunSnapshot(row),
|
|
1098
|
+
};
|
|
1099
|
+
}
|
|
1100
|
+
function createId(prefix) {
|
|
1101
|
+
return `${prefix}_${randomUUID().replaceAll("-", "")}`;
|
|
1102
|
+
}
|
|
1103
|
+
//# sourceMappingURL=session-runtime.js.map
|