omnicell-router 0.8.4 → 0.10.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/README.md +26 -7
- package/README.zh-CN.md +18 -5
- package/backend/dist/capture/recorder.d.ts +4 -10
- package/backend/dist/capture/recorder.js +6 -58
- package/backend/dist/cli/clients/base.d.ts +36 -11
- package/backend/dist/cli/clients/base.js +31 -15
- package/backend/dist/cli/clients/claude.d.ts +9 -0
- package/backend/dist/cli/clients/claude.js +6 -0
- package/backend/dist/cli/clients/codex.d.ts +8 -0
- package/backend/dist/cli/clients/codex.js +5 -0
- package/backend/dist/cli/clients/core-adapters.d.ts +1 -1
- package/backend/dist/cli/clients/core-adapters.js +1 -1
- package/backend/dist/cli/clients/dsh.d.ts +8 -0
- package/backend/dist/cli/clients/dsh.js +5 -0
- package/backend/dist/cli/clients/grok.d.ts +15 -1
- package/backend/dist/cli/clients/grok.js +16 -2
- package/backend/dist/cli/clients/index.d.ts +13 -1
- package/backend/dist/cli/clients/index.js +97 -19
- package/backend/dist/cli/clients/kimi.d.ts +6 -0
- package/backend/dist/cli/clients/kimi.js +3 -0
- package/backend/dist/cli/clients/launcher.d.ts +1 -1
- package/backend/dist/cli/clients/model-meta.js +6 -3
- package/backend/dist/cli/clients/omp.d.ts +6 -0
- package/backend/dist/cli/clients/omp.js +3 -0
- package/backend/dist/cli/clients/openclaw.d.ts +6 -0
- package/backend/dist/cli/clients/openclaw.js +3 -0
- package/backend/dist/cli/clients/opencode.d.ts +8 -0
- package/backend/dist/cli/clients/opencode.js +5 -0
- package/backend/dist/cli/clients/pi.d.ts +6 -0
- package/backend/dist/cli/clients/pi.js +3 -0
- package/backend/dist/cli/clients/update-check.d.ts +37 -0
- package/backend/dist/cli/clients/update-check.js +309 -0
- package/backend/dist/cli/clients/workbuddy.d.ts +19 -0
- package/backend/dist/cli/clients/workbuddy.js +66 -0
- package/backend/dist/cli/index.js +5 -4
- package/backend/dist/cli/paths.js +8 -0
- package/backend/dist/cli/types.d.ts +92 -16
- package/backend/dist/config/comments-en.js +31 -26
- package/backend/dist/config/comments-zh.js +29 -24
- package/backend/dist/config/defaults.d.ts +194 -33
- package/backend/dist/config/defaults.js +193 -32
- package/backend/dist/config/env.d.ts +10 -0
- package/backend/dist/config/env.js +3 -2
- package/backend/dist/config/index.d.ts +7 -0
- package/backend/dist/config/index.js +184 -43
- package/backend/dist/config/migrations/v0-to-v1.d.ts +2 -2
- package/backend/dist/config/migrations/v10-to-v11.d.ts +38 -0
- package/backend/dist/config/migrations/v10-to-v11.js +43 -0
- package/backend/dist/config/migrations/v6-to-v7.d.ts +45 -0
- package/backend/dist/config/migrations/v6-to-v7.js +97 -0
- package/backend/dist/config/migrations/v7-to-v8.d.ts +36 -0
- package/backend/dist/config/migrations/v7-to-v8.js +43 -0
- package/backend/dist/config/migrations/v8-to-v9.d.ts +38 -0
- package/backend/dist/config/migrations/v8-to-v9.js +43 -0
- package/backend/dist/config/migrations/v9-to-v10.d.ts +51 -0
- package/backend/dist/config/migrations/v9-to-v10.js +71 -0
- package/backend/dist/config/runtime.js +1 -1
- package/backend/dist/config/schema.d.ts +3 -3
- package/backend/dist/config/schema.js +14 -4
- package/backend/dist/config/types.d.ts +167 -38
- package/backend/dist/egress-proxy/asset-download.d.ts +54 -0
- package/backend/dist/egress-proxy/asset-download.js +420 -0
- package/backend/dist/egress-proxy/manual-node-spec.d.ts +92 -0
- package/backend/dist/egress-proxy/manual-node-spec.js +408 -0
- package/backend/dist/egress-proxy/mihomo-binary.d.ts +54 -0
- package/backend/dist/egress-proxy/mihomo-binary.js +278 -0
- package/backend/dist/egress-proxy/mihomo-client.d.ts +94 -0
- package/backend/dist/egress-proxy/mihomo-client.js +308 -0
- package/backend/dist/egress-proxy/mihomo-config.d.ts +40 -0
- package/backend/dist/egress-proxy/mihomo-config.js +161 -0
- package/backend/dist/egress-proxy/mihomo-geo.d.ts +87 -0
- package/backend/dist/egress-proxy/mihomo-geo.js +213 -0
- package/backend/dist/egress-proxy/mihomo-logs.d.ts +43 -0
- package/backend/dist/egress-proxy/mihomo-logs.js +84 -0
- package/backend/dist/egress-proxy/mihomo-pac.d.ts +37 -0
- package/backend/dist/egress-proxy/mihomo-pac.js +75 -0
- package/backend/dist/egress-proxy/mihomo-rules-import.d.ts +43 -0
- package/backend/dist/egress-proxy/mihomo-rules-import.js +193 -0
- package/backend/dist/egress-proxy/mihomo-sidecar.d.ts +81 -0
- package/backend/dist/egress-proxy/mihomo-sidecar.js +637 -0
- package/backend/dist/forward-proxy/egress.d.ts +50 -11
- package/backend/dist/forward-proxy/egress.js +50 -17
- package/backend/dist/forward-proxy/h2-forward.js +36 -10
- package/backend/dist/forward-proxy/index.d.ts +11 -8
- package/backend/dist/forward-proxy/index.js +22 -60
- package/backend/dist/forward-proxy/interceptor.d.ts +3 -2
- package/backend/dist/forward-proxy/interceptor.js +4 -4
- package/backend/dist/forward-proxy/relay.js +3 -3
- package/backend/dist/forward-proxy/rules.d.ts +4 -4
- package/backend/dist/forward-proxy/rules.js +25 -4
- package/backend/dist/forward-proxy/server.d.ts +0 -16
- package/backend/dist/forward-proxy/server.js +124 -156
- package/backend/dist/harness/host/hosts.js +3 -2
- package/backend/dist/index.js +53 -18
- package/backend/dist/ingress-proxy/access-log.d.ts +176 -0
- package/backend/dist/ingress-proxy/access-log.js +774 -0
- package/backend/dist/ingress-proxy/ca-inventory.d.ts +106 -0
- package/backend/dist/ingress-proxy/ca-inventory.js +525 -0
- package/backend/dist/ingress-proxy/ca-trust.d.ts +96 -0
- package/backend/dist/ingress-proxy/ca-trust.js +315 -0
- package/backend/dist/ingress-proxy/ca.d.ts +104 -0
- package/backend/dist/ingress-proxy/ca.js +273 -0
- package/backend/dist/ingress-proxy/data-dir.d.ts +28 -0
- package/backend/dist/ingress-proxy/data-dir.js +45 -0
- package/backend/dist/ingress-proxy/egress.d.ts +151 -0
- package/backend/dist/ingress-proxy/egress.js +364 -0
- package/backend/dist/ingress-proxy/h2-forward.d.ts +66 -0
- package/backend/dist/ingress-proxy/h2-forward.js +561 -0
- package/backend/dist/ingress-proxy/index.d.ts +24 -0
- package/backend/dist/ingress-proxy/index.js +144 -0
- package/backend/dist/ingress-proxy/interceptor.d.ts +25 -0
- package/backend/dist/ingress-proxy/interceptor.js +52 -0
- package/backend/dist/ingress-proxy/ordered-relay.d.ts +29 -0
- package/backend/dist/ingress-proxy/ordered-relay.js +46 -0
- package/backend/dist/ingress-proxy/relay.d.ts +54 -0
- package/backend/dist/ingress-proxy/relay.js +188 -0
- package/backend/dist/ingress-proxy/rules.d.ts +195 -0
- package/backend/dist/ingress-proxy/rules.js +348 -0
- package/backend/dist/ingress-proxy/server.d.ts +153 -0
- package/backend/dist/ingress-proxy/server.js +2065 -0
- package/backend/dist/ingress-proxy/sni.d.ts +35 -0
- package/backend/dist/ingress-proxy/sni.js +124 -0
- package/backend/dist/ingress-proxy/status.d.ts +32 -0
- package/backend/dist/ingress-proxy/status.js +15 -0
- package/backend/dist/ingress-proxy/system-proxy.d.ts +112 -0
- package/backend/dist/ingress-proxy/system-proxy.js +481 -0
- package/backend/dist/ingress-proxy/traffic-delete.d.ts +21 -0
- package/backend/dist/ingress-proxy/traffic-delete.js +64 -0
- package/backend/dist/ingress-proxy/traffic-delete.worker.d.ts +44 -0
- package/backend/dist/ingress-proxy/traffic-delete.worker.js +228 -0
- package/backend/dist/ingress-proxy/traffic-files.d.ts +10 -0
- package/backend/dist/ingress-proxy/traffic-files.js +10 -0
- package/backend/dist/ingress-proxy/upstream-tls.d.ts +46 -0
- package/backend/dist/ingress-proxy/upstream-tls.js +105 -0
- package/backend/dist/ingress-proxy/ws-frames.d.ts +45 -0
- package/backend/dist/ingress-proxy/ws-frames.js +153 -0
- package/backend/dist/ir/adapters/anthropic-messages-stream.js +6 -1
- package/backend/dist/ir/adapters/anthropic-messages.js +12 -5
- package/backend/dist/ir/adapters/google-gemini-stream.js +7 -2
- package/backend/dist/ir/adapters/google-gemini.js +5 -2
- package/backend/dist/ir/adapters/openai-chat-stream.js +5 -1
- package/backend/dist/ir/adapters/openai-chat.d.ts +25 -0
- package/backend/dist/ir/adapters/openai-chat.js +45 -3
- package/backend/dist/ir/adapters/openai-responses-output.d.ts +1 -0
- package/backend/dist/ir/adapters/openai-responses-output.js +67 -21
- package/backend/dist/ir/adapters/openai-responses-stream.d.ts +13 -0
- package/backend/dist/ir/adapters/openai-responses-stream.js +34 -1
- package/backend/dist/ir/adapters/openai-responses.d.ts +82 -24
- package/backend/dist/ir/adapters/openai-responses.js +146 -37
- package/backend/dist/ir/media.d.ts +20 -0
- package/backend/dist/ir/media.js +41 -0
- package/backend/dist/ir/tool-identity.d.ts +35 -0
- package/backend/dist/ir/tool-identity.js +40 -0
- package/backend/dist/ir/types.d.ts +9 -1
- package/backend/dist/local/build-jobs.d.ts +66 -0
- package/backend/dist/local/build-jobs.js +243 -0
- package/backend/dist/local/detect.d.ts +32 -0
- package/backend/dist/local/detect.js +145 -0
- package/backend/dist/local/draft-match.d.ts +58 -0
- package/backend/dist/local/draft-match.js +124 -0
- package/backend/dist/local/engine-runtime.d.ts +522 -0
- package/backend/dist/local/engine-runtime.js +1 -0
- package/backend/dist/local/engines.d.ts +241 -0
- package/backend/dist/local/engines.js +651 -0
- package/backend/dist/local/gguf.d.ts +35 -0
- package/backend/dist/local/gguf.js +314 -0
- package/backend/dist/local/hf.d.ts +120 -0
- package/backend/dist/local/hf.js +304 -0
- package/backend/dist/local/install.d.ts +90 -0
- package/backend/dist/local/install.js +421 -0
- package/backend/dist/local/llamacpp.d.ts +55 -0
- package/backend/dist/local/llamacpp.js +177 -0
- package/backend/dist/local/local-provider.d.ts +81 -0
- package/backend/dist/local/local-provider.js +301 -0
- package/backend/dist/local/machine-stats.d.ts +87 -0
- package/backend/dist/local/machine-stats.js +332 -0
- package/backend/dist/local/model-store.d.ts +347 -0
- package/backend/dist/local/model-store.js +1215 -0
- package/backend/dist/local/part-sweep.d.ts +32 -0
- package/backend/dist/local/part-sweep.js +156 -0
- package/backend/dist/local/plan.d.ts +65 -0
- package/backend/dist/local/plan.js +146 -0
- package/backend/dist/local/resources.d.ts +49 -0
- package/backend/dist/local/resources.js +64 -0
- package/backend/dist/local/run-stats.d.ts +95 -0
- package/backend/dist/local/run-stats.js +195 -0
- package/backend/dist/local/runner.d.ts +235 -0
- package/backend/dist/local/runner.js +1392 -0
- package/backend/dist/local/settings.d.ts +79 -0
- package/backend/dist/local/settings.js +180 -0
- package/backend/dist/local/strata-catalog.d.ts +30 -0
- package/backend/dist/local/strata-catalog.js +511 -0
- package/backend/dist/local/strata-drafting.d.ts +46 -0
- package/backend/dist/local/strata-drafting.js +265 -0
- package/backend/dist/local/strata.d.ts +278 -0
- package/backend/dist/local/strata.js +1392 -0
- package/backend/dist/local/tar.d.ts +20 -0
- package/backend/dist/local/tar.js +367 -0
- package/backend/dist/local/transport.d.ts +35 -0
- package/backend/dist/local/transport.js +89 -0
- package/backend/dist/local/tune.d.ts +58 -0
- package/backend/dist/local/tune.js +324 -0
- package/backend/dist/observability/http-exchange.d.ts +2 -2
- package/backend/dist/opencode/catalog/auto-pull.d.ts +14 -3
- package/backend/dist/opencode/catalog/auto-pull.js +276 -70
- package/backend/dist/opencode/catalog/ocr-store.js +3 -1
- package/backend/dist/opencode/catalog/overrides-store.d.ts +10 -1
- package/backend/dist/opencode/catalog/overrides-store.js +6 -0
- package/backend/dist/opencode/catalog/reasoning-declaration.d.ts +35 -0
- package/backend/dist/opencode/catalog/reasoning-declaration.js +66 -0
- package/backend/dist/opencode/catalog/repository.js +25 -2
- package/backend/dist/opencode/catalog/sources/models-dev.js +13 -3
- package/backend/dist/opencode/catalog/types.d.ts +35 -3
- package/backend/dist/opencode/catalog/types.js +38 -1
- package/backend/dist/opencode/credentials/ocr-store.js +11 -2
- package/backend/dist/opencode/credentials/sync.js +12 -3
- package/backend/dist/opencode/credentials/types.d.ts +31 -4
- package/backend/dist/opencode/credentials/types.js +25 -0
- package/backend/dist/opencode/probe.d.ts +21 -4
- package/backend/dist/opencode/probe.js +34 -10
- package/backend/dist/pipeline/directives.d.ts +9 -4
- package/backend/dist/pipeline/directives.js +14 -10
- package/backend/dist/pipeline/orchestrator.d.ts +42 -13
- package/backend/dist/pipeline/orchestrator.js +497 -165
- package/backend/dist/plugins/environment-ack.d.ts +51 -0
- package/backend/dist/plugins/environment-ack.js +81 -0
- package/backend/dist/plugins/gateways.d.ts +6 -6
- package/backend/dist/plugins/gateways.js +2 -2
- package/backend/dist/plugins/host.d.ts +98 -14
- package/backend/dist/plugins/host.js +622 -87
- package/backend/dist/plugins/install-run.d.ts +27 -0
- package/backend/dist/plugins/install-run.js +99 -0
- package/backend/dist/plugins/manifest.d.ts +84 -32
- package/backend/dist/plugins/manifest.js +149 -45
- package/backend/dist/plugins/market-install.d.ts +18 -0
- package/backend/dist/plugins/market-install.js +111 -0
- package/backend/dist/plugins/market-source.d.ts +100 -0
- package/backend/dist/plugins/market-source.js +449 -0
- package/backend/dist/plugins/market-updates.d.ts +70 -0
- package/backend/dist/plugins/market-updates.js +97 -0
- package/backend/dist/plugins/market.d.ts +677 -0
- package/backend/dist/plugins/market.js +2113 -0
- package/backend/dist/plugins/source-index.d.ts +155 -0
- package/backend/dist/plugins/source-index.js +217 -0
- package/backend/dist/plugins/types.d.ts +402 -37
- package/backend/dist/projects/worktrees.d.ts +88 -0
- package/backend/dist/projects/worktrees.js +220 -0
- package/backend/dist/providers/boot-direct.d.ts +41 -7
- package/backend/dist/providers/boot-direct.js +252 -36
- package/backend/dist/providers/compat/tool-markup-recovery.js +31 -6
- package/backend/dist/providers/executors/dispatch.d.ts +17 -7
- package/backend/dist/providers/executors/dispatch.js +28 -9
- package/backend/dist/providers/executors/openai-chat.js +14 -2
- package/backend/dist/providers/executors/plugin-adapter.d.ts +23 -1
- package/backend/dist/providers/executors/plugin-adapter.js +59 -3
- package/backend/dist/providers/member-match.d.ts +32 -9
- package/backend/dist/providers/member-match.js +65 -21
- package/backend/dist/providers/model-resolution.d.ts +62 -0
- package/backend/dist/providers/model-resolution.js +62 -0
- package/backend/dist/providers/price-order.d.ts +51 -0
- package/backend/dist/providers/price-order.js +54 -0
- package/backend/dist/providers/provider-takedown.d.ts +5 -2
- package/backend/dist/providers/registry.d.ts +84 -25
- package/backend/dist/providers/registry.js +209 -114
- package/backend/dist/providers/selection.d.ts +91 -0
- package/backend/dist/providers/selection.js +158 -0
- package/backend/dist/providers/virtual-meta.d.ts +33 -2
- package/backend/dist/providers/virtual-meta.js +74 -18
- package/backend/dist/quota/cli-login.d.ts +25 -4
- package/backend/dist/quota/cli-login.js +35 -5
- package/backend/dist/quota/coverage-generation.d.ts +24 -0
- package/backend/dist/quota/coverage-generation.js +29 -0
- package/backend/dist/quota/declarations.js +8 -4
- package/backend/dist/quota/engine.js +2 -2
- package/backend/dist/quota/prober.d.ts +43 -2
- package/backend/dist/quota/prober.js +97 -22
- package/backend/dist/ratelimit/rate-limiter.d.ts +1 -1
- package/backend/dist/resilience/error-classifier.js +29 -0
- package/backend/dist/router/index.d.ts +1 -1
- package/backend/dist/router/index.js +17 -1
- package/backend/dist/router/layer2-decision.d.ts +1 -1
- package/backend/dist/router/layer2-decision.js +2 -2
- package/backend/dist/routes/console.js +928 -310
- package/backend/dist/routes/environments.js +1 -1
- package/backend/dist/routes/local-models.d.ts +2 -0
- package/backend/dist/routes/local-models.js +1414 -0
- package/backend/dist/routes/mihomo.d.ts +2 -2
- package/backend/dist/routes/mihomo.js +20 -11
- package/backend/dist/routes/projects.js +121 -0
- package/backend/dist/server.js +318 -13
- package/backend/dist/session/session-manager.d.ts +42 -5
- package/backend/dist/session/session-manager.js +64 -11
- package/backend/dist/session/session-row.d.ts +29 -8
- package/backend/dist/session/session-row.js +18 -13
- package/backend/dist/snapshot/bundle.d.ts +40 -0
- package/backend/dist/snapshot/bundle.js +177 -0
- package/backend/dist/snapshot/handoff/compress.d.ts +46 -0
- package/backend/dist/snapshot/handoff/compress.js +77 -0
- package/backend/dist/snapshot/handoff/index.d.ts +119 -0
- package/backend/dist/snapshot/handoff/index.js +160 -0
- package/backend/dist/snapshot/handoff/jsonl-writer.d.ts +78 -0
- package/backend/dist/snapshot/handoff/jsonl-writer.js +175 -0
- package/backend/dist/snapshot/handoff/opencode-writer.d.ts +51 -0
- package/backend/dist/snapshot/handoff/opencode-writer.js +214 -0
- package/backend/dist/snapshot/handoff/summarize.d.ts +39 -0
- package/backend/dist/snapshot/handoff/summarize.js +118 -0
- package/backend/dist/snapshot/handoff/transcript.d.ts +71 -0
- package/backend/dist/snapshot/handoff/transcript.js +167 -0
- package/backend/dist/snapshot/media-fs.d.ts +10 -0
- package/backend/dist/snapshot/media-fs.js +50 -0
- package/backend/dist/snapshot/media.d.ts +32 -0
- package/backend/dist/snapshot/media.js +87 -0
- package/backend/dist/snapshot/store.d.ts +226 -0
- package/backend/dist/snapshot/store.js +887 -0
- package/backend/dist/snapshot/tree.d.ts +112 -0
- package/backend/dist/snapshot/tree.js +207 -0
- package/backend/dist/snapshot/types.d.ts +212 -0
- package/backend/dist/snapshot/types.js +14 -0
- package/backend/dist/snapshot/view.d.ts +19 -0
- package/backend/dist/snapshot/view.js +57 -0
- package/backend/dist/snapshot/write-worker.d.ts +67 -0
- package/backend/dist/snapshot/write-worker.js +129 -0
- package/backend/dist/surfaces/body-admission.d.ts +67 -2
- package/backend/dist/surfaces/body-admission.js +218 -13
- package/backend/dist/surfaces/chat-pipeline.js +8 -0
- package/backend/dist/surfaces/decision-pipeline.js +10 -7
- package/backend/dist/surfaces/gateway-catalog.d.ts +26 -0
- package/backend/dist/surfaces/gateway-catalog.js +202 -0
- package/backend/dist/surfaces/gateway-chat.d.ts +7 -11
- package/backend/dist/surfaces/gateway-chat.js +50 -12
- package/backend/dist/surfaces/ocr-headers.d.ts +6 -4
- package/backend/dist/surfaces/ocr-headers.js +11 -7
- package/backend/dist/surfaces/systemone.js +1 -1
- package/backend/dist/surfaces/transport-fastify.js +32 -5
- package/backend/dist/surfaces/transport-node-http.d.ts +9 -3
- package/backend/dist/surfaces/transport-node-http.js +6 -19
- package/backend/dist/surfaces/types.d.ts +11 -3
- package/backend/dist/trace/persist.js +21 -1
- package/backend/dist/trace/tracker.d.ts +35 -3
- package/backend/dist/trace/tracker.js +23 -0
- package/backend/dist/types/openai.d.ts +8 -0
- package/backend/dist/types/router.d.ts +17 -6
- package/backend/dist/types/router.js +14 -2
- package/backend/dist/utils/client-families.d.ts +66 -0
- package/backend/dist/utils/client-families.js +127 -0
- package/backend/dist/utils/event-ring.d.ts +1 -1
- package/backend/dist/utils/event-ring.js +1 -1
- package/backend/dist/utils/proxy-node.js +1 -1
- package/backend/dist/utils/proxy.d.ts +135 -11
- package/backend/dist/utils/proxy.js +144 -29
- package/backend/dist/utils/run-command.d.ts +21 -1
- package/backend/dist/utils/run-command.js +64 -4
- package/backend/dist/utils/server-liveness.d.ts +3 -2
- package/backend/dist/utils/server-liveness.js +3 -2
- package/backend/dist/utils/session-file.d.ts +28 -0
- package/backend/dist/utils/session-file.js +34 -0
- package/frontend/dist/assets/antigravity.color-DXFfVr6X.svg +39 -0
- package/frontend/dist/assets/gemini-cli.color-BfquLd1p.svg +1 -0
- package/frontend/dist/assets/index-CTZNvn1d.js +1707 -0
- package/frontend/dist/assets/{index-KI1OW5x6.css → index-CqDSOPQ0.css} +1 -1
- package/frontend/dist/index.html +2 -2
- package/package.json +3 -1
- package/frontend/dist/assets/index-Dk76uc7e.js +0 -1522
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { clipTitle } from './transcript.js';
|
|
7
|
+
const randomChars = (n) => {
|
|
8
|
+
const alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
9
|
+
const bytes = crypto.randomBytes(n);
|
|
10
|
+
let out = '';
|
|
11
|
+
for (let i = 0; i < n; i++)
|
|
12
|
+
out += alphabet[bytes[i] % alphabet.length];
|
|
13
|
+
return out;
|
|
14
|
+
};
|
|
15
|
+
/** Default opencode share dir, matching its own XDG-on-Windows layout. */
|
|
16
|
+
export function defaultOpencodeShareDir() {
|
|
17
|
+
const home = os.homedir();
|
|
18
|
+
const xdg = process.env.XDG_DATA_HOME;
|
|
19
|
+
return xdg ? path.join(xdg, 'opencode') : path.join(home, '.local', 'share', 'opencode');
|
|
20
|
+
}
|
|
21
|
+
function renderAssistantParts(msg, modelRef, now) {
|
|
22
|
+
const parts = [];
|
|
23
|
+
let index = 0;
|
|
24
|
+
const nextId = () => `prt_${randomChars(18)}${(index++).toString(36)}`;
|
|
25
|
+
for (const part of msg.content) {
|
|
26
|
+
if (part.type === 'text') {
|
|
27
|
+
parts.push({ id: nextId(), type: 'text', text: part.text });
|
|
28
|
+
}
|
|
29
|
+
else if (part.type === 'thinking') {
|
|
30
|
+
parts.push({ id: nextId(), type: 'reasoning', text: part.thinking });
|
|
31
|
+
}
|
|
32
|
+
else if (part.type === 'toolCall') {
|
|
33
|
+
// opencode's tool part folds the call AND its (already historical) result
|
|
34
|
+
// into `state`; `executed: false` marks it as replayed, not live. `time`
|
|
35
|
+
// is REQUIRED by the import schema (absent → 400 `Missing key
|
|
36
|
+
// at ["messages"][N]["content"][I]["time"]`, measured on v2.4.3).
|
|
37
|
+
parts.push({
|
|
38
|
+
id: part.id,
|
|
39
|
+
type: 'tool',
|
|
40
|
+
name: part.name,
|
|
41
|
+
executed: false,
|
|
42
|
+
state: { status: 'completed', input: part.arguments, content: [] },
|
|
43
|
+
time: { created: now, ran: now, completed: now },
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
else if (part.type === 'image') {
|
|
47
|
+
// v2.4.3's assistant content union is `text | reasoning | tool` only —
|
|
48
|
+
// there is NO assistant file part (schema rejection measured on import),
|
|
49
|
+
// so an assistant image has no representation in this target. User-side
|
|
50
|
+
// images travel in the user message's `files[]` instead (see buildExport).
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
void modelRef;
|
|
54
|
+
return parts;
|
|
55
|
+
}
|
|
56
|
+
/** Attach a toolResult's output to the matching tool part already emitted. */
|
|
57
|
+
function attachToolResults(parts, results) {
|
|
58
|
+
for (const part of parts) {
|
|
59
|
+
if (part.type !== 'tool' || !part.id)
|
|
60
|
+
continue;
|
|
61
|
+
const result = results.get(String(part.id));
|
|
62
|
+
if (!result)
|
|
63
|
+
continue;
|
|
64
|
+
const text = result.content
|
|
65
|
+
.filter((p) => p.type === 'text')
|
|
66
|
+
.map((p) => p.text)
|
|
67
|
+
.join('\n');
|
|
68
|
+
part.state = {
|
|
69
|
+
status: result.isError ? 'error' : 'completed',
|
|
70
|
+
input: part.state?.input ?? {},
|
|
71
|
+
content: text ? [{ type: 'text', text }] : [],
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function buildExport(sessionId, opts, model) {
|
|
76
|
+
const now = opts.now ?? Date.now();
|
|
77
|
+
const iso = now;
|
|
78
|
+
const title = clipTitle(opts.transcript.title) || 'Imported conversation';
|
|
79
|
+
// The importer REQUIRES `projectID` and `location` (measured: omitting either
|
|
80
|
+
// fails with a bare "Bad Request"; the value itself is not validated against
|
|
81
|
+
// a directory hash — any stable id is accepted). We derive it deterministically
|
|
82
|
+
// from the working directory so repeated migrations into one project converge
|
|
83
|
+
// on one project row instead of spraying a new one per session.
|
|
84
|
+
const projectID = crypto.createHash('sha1').update(path.resolve(opts.workingDir)).digest('hex');
|
|
85
|
+
// opencode messages are a FLAT sequence: user text turns, then assistant turns
|
|
86
|
+
// whose parts may include tool calls; a toolResult is folded into the tool
|
|
87
|
+
// part of the assistant turn it answers.
|
|
88
|
+
const messages = [];
|
|
89
|
+
const pending = [];
|
|
90
|
+
const results = new Map();
|
|
91
|
+
const flushAssistant = () => {
|
|
92
|
+
if (!pending.length)
|
|
93
|
+
return;
|
|
94
|
+
// Split into one assistant message per contiguous run so opencode's
|
|
95
|
+
// "one assistant message, many parts" model lines up with our turns.
|
|
96
|
+
const parts = [];
|
|
97
|
+
for (const msg of pending)
|
|
98
|
+
parts.push(...renderAssistantParts(msg, model, now));
|
|
99
|
+
attachToolResults(parts, results);
|
|
100
|
+
if (parts.length) {
|
|
101
|
+
messages.push({
|
|
102
|
+
id: `msg_${randomChars(20)}`,
|
|
103
|
+
time: { created: now, completed: now },
|
|
104
|
+
type: 'assistant',
|
|
105
|
+
agent: 'build',
|
|
106
|
+
model: { id: model.id, providerID: model.providerID, variant: 'default' },
|
|
107
|
+
content: parts,
|
|
108
|
+
finish: parts.some((p) => p.type === 'tool') ? 'tool-calls' : 'stop',
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
pending.length = 0;
|
|
112
|
+
};
|
|
113
|
+
for (const msg of opts.transcript.messages) {
|
|
114
|
+
if (msg.role === 'user') {
|
|
115
|
+
flushAssistant();
|
|
116
|
+
const text = msg.content.map((p) => (p.type === 'text' ? p.text : '')).join('\n').trim();
|
|
117
|
+
// A user attachment is `{data, mime, source:{type:'inline'}, name}` —
|
|
118
|
+
// the shape a native `--file` attachment exports (measured). A
|
|
119
|
+
// `{type,url}` file part is rejected with `Missing key at
|
|
120
|
+
// ["files"][N]["data"]`.
|
|
121
|
+
const files = msg.content
|
|
122
|
+
.filter((p) => p.type === 'image')
|
|
123
|
+
.map((p, i) => ({
|
|
124
|
+
data: p.data,
|
|
125
|
+
mime: p.mimeType,
|
|
126
|
+
source: { type: 'inline' },
|
|
127
|
+
name: `image-${i + 1}.${(p.mimeType.split('/')[1] ?? 'bin').split('+')[0]}`,
|
|
128
|
+
}));
|
|
129
|
+
messages.push({
|
|
130
|
+
id: `msg_${randomChars(20)}`,
|
|
131
|
+
time: { created: now },
|
|
132
|
+
type: 'user',
|
|
133
|
+
text,
|
|
134
|
+
...(files.length ? { files } : { files: [] }),
|
|
135
|
+
agents: [],
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
else if (msg.role === 'toolResult') {
|
|
139
|
+
if (msg.toolCallId)
|
|
140
|
+
results.set(msg.toolCallId, msg);
|
|
141
|
+
}
|
|
142
|
+
else if (msg.role === 'assistant') {
|
|
143
|
+
pending.push(msg);
|
|
144
|
+
}
|
|
145
|
+
// system: opencode has a `system` message type, but the gateway's system
|
|
146
|
+
// prompt is per-request; omitting it avoids a stale prompt in the new session.
|
|
147
|
+
}
|
|
148
|
+
flushAssistant();
|
|
149
|
+
return {
|
|
150
|
+
info: {
|
|
151
|
+
id: sessionId,
|
|
152
|
+
// REQUIRED by the importer (see the note on `projectID` above).
|
|
153
|
+
projectID,
|
|
154
|
+
agent: 'build',
|
|
155
|
+
model: { id: model.id, providerID: model.providerID, variant: 'default' },
|
|
156
|
+
cost: 0,
|
|
157
|
+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
|
|
158
|
+
outcome: 'succeeded',
|
|
159
|
+
time: { created: iso, updated: iso },
|
|
160
|
+
title,
|
|
161
|
+
location: { directory: opts.workingDir },
|
|
162
|
+
},
|
|
163
|
+
messages,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function defaultRun(args) {
|
|
167
|
+
// Node's spawnSync: the gateway runs on both Bun and Node, and the opencode
|
|
168
|
+
// CLI is an external binary either way. A non-zero status or a spawn error
|
|
169
|
+
// both surface as a failure the caller reports verbatim.
|
|
170
|
+
const proc = spawnSync('opencode', args, { encoding: 'utf8', timeout: 120_000 });
|
|
171
|
+
return {
|
|
172
|
+
code: proc.status ?? (proc.error ? 1 : 0),
|
|
173
|
+
stdout: proc.stdout ?? '',
|
|
174
|
+
stderr: proc.stderr ?? (proc.error ? String(proc.error.message) : ''),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Write the session by generating the export shape and importing it. Throws with
|
|
179
|
+
* the CLI's own message when the import is refused, so the console can show the
|
|
180
|
+
* real cause instead of a generic failure.
|
|
181
|
+
*/
|
|
182
|
+
export function writeOpencodeSession(opts) {
|
|
183
|
+
const ref = opts.transcript.models[0] || 'omnicell-router/auto';
|
|
184
|
+
const slash = ref.indexOf('/');
|
|
185
|
+
const model = {
|
|
186
|
+
providerID: slash > 0 ? ref.slice(0, slash) : 'omnicell-router',
|
|
187
|
+
id: slash > 0 ? ref.slice(slash + 1) : ref,
|
|
188
|
+
};
|
|
189
|
+
const sessionId = `ses_${randomChars(20)}`;
|
|
190
|
+
const payload = buildExport(sessionId, opts, model);
|
|
191
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ocr-oc-import-'));
|
|
192
|
+
const file = path.join(tmpDir, `${sessionId}.json`);
|
|
193
|
+
fs.writeFileSync(file, JSON.stringify(payload, null, 2), 'utf8');
|
|
194
|
+
const run = opts.run ?? defaultRun;
|
|
195
|
+
const dir = path.resolve(opts.workingDir);
|
|
196
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
197
|
+
const result = run(['session', 'import', file, '--directory', dir]);
|
|
198
|
+
try {
|
|
199
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
// best effort
|
|
203
|
+
}
|
|
204
|
+
const output = `${result.stdout}\n${result.stderr}`;
|
|
205
|
+
if (result.code !== 0 || /Failed to import|already exists/i.test(output)) {
|
|
206
|
+
throw new Error(`opencode import failed: ${output.trim().split('\n').slice(0, 3).join(' ')}`);
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
sessionId,
|
|
210
|
+
file: path.join(opts.shareDir, 'opencode.db'),
|
|
211
|
+
messages: payload.messages.length,
|
|
212
|
+
skippedMedia: opts.transcript.unresolvedMedia,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { HandoffTranscript } from './transcript.js';
|
|
2
|
+
/**
|
|
3
|
+
* Model-driven compaction (ADR-0039 §8, `compact` mode).
|
|
4
|
+
*
|
|
5
|
+
* This is the agent-client semantics Ken named the mode after: a model READS
|
|
6
|
+
* the conversation and writes a structured handoff summary that REPLACES it —
|
|
7
|
+
* deterministic trimming is only the input bound, never the summary itself.
|
|
8
|
+
* The call goes through the gateway's own chat surface (the translation
|
|
9
|
+
* plane's loopback pattern): the operator picks the summarizer model, cost and
|
|
10
|
+
* traces land under a reserved internal session, and no vendor credential ever
|
|
11
|
+
* reaches this module.
|
|
12
|
+
*/
|
|
13
|
+
/** Reserved loopback session id — hidden from the sessions list by default. */
|
|
14
|
+
export declare const COMPACTION_SESSION = "ocr-internal:handoff-compact";
|
|
15
|
+
/** Errors travel as values — `index.ts` owns the HTTP-status mapping. */
|
|
16
|
+
export interface SummarizeError {
|
|
17
|
+
error: string;
|
|
18
|
+
status: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The gateway call seam. `index.ts` receives this through `HandoffRequest`
|
|
22
|
+
* (server wires the real implementation; tests inject a fake), the same
|
|
23
|
+
* dependency-injection shape the opencode writer uses for its CLI runner.
|
|
24
|
+
*/
|
|
25
|
+
export type HandoffSummarizer = (opts: {
|
|
26
|
+
history: string;
|
|
27
|
+
model: string;
|
|
28
|
+
}) => Promise<{
|
|
29
|
+
summary: string;
|
|
30
|
+
} | SummarizeError>;
|
|
31
|
+
/**
|
|
32
|
+
* The summarizer's view of the conversation: the mechanically trimmed
|
|
33
|
+
* transcript rendered as readable text. Trimming here bounds the INPUT (drop
|
|
34
|
+
* thinking, placeholder images, cap tool results) — the compression that
|
|
35
|
+
* matters is the model's summary.
|
|
36
|
+
*/
|
|
37
|
+
export declare function renderHistory(transcript: HandoffTranscript): string;
|
|
38
|
+
/** The real summarizer: one chat call through the gateway loopback. */
|
|
39
|
+
export declare const summarizeHandoff: HandoffSummarizer;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { defaultGatewayPort } from '../../cli/clients/base.js';
|
|
2
|
+
import { firstEnabledKey } from '../../translation/index.js';
|
|
3
|
+
import { INTERNAL_SESSION_PREFIX } from '../../session/session-manager.js';
|
|
4
|
+
import { compressTranscript } from './compress.js';
|
|
5
|
+
/**
|
|
6
|
+
* Model-driven compaction (ADR-0039 §8, `compact` mode).
|
|
7
|
+
*
|
|
8
|
+
* This is the agent-client semantics Ken named the mode after: a model READS
|
|
9
|
+
* the conversation and writes a structured handoff summary that REPLACES it —
|
|
10
|
+
* deterministic trimming is only the input bound, never the summary itself.
|
|
11
|
+
* The call goes through the gateway's own chat surface (the translation
|
|
12
|
+
* plane's loopback pattern): the operator picks the summarizer model, cost and
|
|
13
|
+
* traces land under a reserved internal session, and no vendor credential ever
|
|
14
|
+
* reaches this module.
|
|
15
|
+
*/
|
|
16
|
+
/** Reserved loopback session id — hidden from the sessions list by default. */
|
|
17
|
+
export const COMPACTION_SESSION = `${INTERNAL_SESSION_PREFIX}handoff-compact`;
|
|
18
|
+
const COMPACT_TIMEOUT_MS = 180_000;
|
|
19
|
+
/** Summarizer input keeps more tool detail than the trim mode's file output. */
|
|
20
|
+
const INPUT_TOOL_RESULT_LIMIT = 2000;
|
|
21
|
+
const SYSTEM_PROMPT = `You are compacting a conversation so another agent session, in a different client, can continue it without seeing the original history. Write a faithful, self-contained handoff summary.
|
|
22
|
+
|
|
23
|
+
Cover, in this order, skipping a section only when the conversation truly has nothing for it:
|
|
24
|
+
1. Primary intent and goals — what the user is trying to accomplish.
|
|
25
|
+
2. Key technical concepts and decisions — with the reasons behind each decision.
|
|
26
|
+
3. Files, code, commands and artifacts touched — exact paths, identifiers and values when known.
|
|
27
|
+
4. Errors encountered and the fixes applied.
|
|
28
|
+
5. The user's explicit requests and preferences — quote them when they constrain behavior.
|
|
29
|
+
6. Pending tasks and the current state of the work.
|
|
30
|
+
7. The exact next step the conversation was about to take.
|
|
31
|
+
|
|
32
|
+
Rules:
|
|
33
|
+
- Be exhaustive on substance (paths, names, numbers, decisions) and compress prose, never facts.
|
|
34
|
+
- Do not invent anything that is not present in the history.
|
|
35
|
+
- Write the summary in the language the conversation was conducted in.
|
|
36
|
+
- Output only the summary, no preamble.`;
|
|
37
|
+
/** Render one trimmed message as the plain text the summarizer reads. */
|
|
38
|
+
function renderMessage(msg) {
|
|
39
|
+
const lines = [];
|
|
40
|
+
for (const part of msg.content) {
|
|
41
|
+
if (part.type === 'text') {
|
|
42
|
+
if (part.text)
|
|
43
|
+
lines.push(part.text);
|
|
44
|
+
}
|
|
45
|
+
else if (part.type === 'toolCall') {
|
|
46
|
+
const args = typeof part.arguments === 'string' ? part.arguments : JSON.stringify(part.arguments ?? {});
|
|
47
|
+
lines.push(`→ tool call: ${part.name} ${args}`);
|
|
48
|
+
}
|
|
49
|
+
// thinking / image never reach here: the input trim removed/placeholder-ed them.
|
|
50
|
+
}
|
|
51
|
+
if (!lines.length)
|
|
52
|
+
return null;
|
|
53
|
+
const header = msg.role === 'toolResult'
|
|
54
|
+
? `### TOOL RESULT (${msg.toolName ?? 'unknown'}${msg.isError ? ', error' : ''})`
|
|
55
|
+
: `### ${msg.role.toUpperCase()}`;
|
|
56
|
+
return `${header}\n${lines.join('\n')}`;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The summarizer's view of the conversation: the mechanically trimmed
|
|
60
|
+
* transcript rendered as readable text. Trimming here bounds the INPUT (drop
|
|
61
|
+
* thinking, placeholder images, cap tool results) — the compression that
|
|
62
|
+
* matters is the model's summary.
|
|
63
|
+
*/
|
|
64
|
+
export function renderHistory(transcript) {
|
|
65
|
+
const { transcript: bounded } = compressTranscript(transcript, { toolResultLimit: INPUT_TOOL_RESULT_LIMIT });
|
|
66
|
+
return bounded.messages
|
|
67
|
+
.map(renderMessage)
|
|
68
|
+
.filter((s) => Boolean(s))
|
|
69
|
+
.join('\n\n');
|
|
70
|
+
}
|
|
71
|
+
/** The real summarizer: one chat call through the gateway loopback. */
|
|
72
|
+
export const summarizeHandoff = async ({ history, model }) => {
|
|
73
|
+
const key = firstEnabledKey();
|
|
74
|
+
if (!key) {
|
|
75
|
+
return { error: 'compaction needs an enabled gateway API key — create one in the console first', status: 503 };
|
|
76
|
+
}
|
|
77
|
+
let response;
|
|
78
|
+
try {
|
|
79
|
+
response = await fetch(`http://127.0.0.1:${defaultGatewayPort()}/v1/chat/completions`, {
|
|
80
|
+
method: 'POST',
|
|
81
|
+
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${key}` },
|
|
82
|
+
body: JSON.stringify({
|
|
83
|
+
model,
|
|
84
|
+
temperature: 0,
|
|
85
|
+
messages: [
|
|
86
|
+
{ role: 'system', content: SYSTEM_PROMPT },
|
|
87
|
+
{ role: 'user', content: history },
|
|
88
|
+
],
|
|
89
|
+
// Pin the trace under the reserved internal session so the operator can
|
|
90
|
+
// find (and price) compactions without them polluting the sessions list.
|
|
91
|
+
router_options: { session_id: COMPACTION_SESSION },
|
|
92
|
+
}),
|
|
93
|
+
signal: AbortSignal.timeout(COMPACT_TIMEOUT_MS),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
return { error: `compaction call to '${model}' failed: ${err.message}`, status: 502 };
|
|
98
|
+
}
|
|
99
|
+
if (!response.ok) {
|
|
100
|
+
const detail = (await response.text().catch(() => '')).slice(0, 500) || response.statusText;
|
|
101
|
+
return { error: `compaction model '${model}' returned HTTP ${response.status} — ${detail}`, status: 502 };
|
|
102
|
+
}
|
|
103
|
+
let data;
|
|
104
|
+
try {
|
|
105
|
+
data = (await response.json());
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return { error: `compaction model '${model}' returned a non-JSON body`, status: 502 };
|
|
109
|
+
}
|
|
110
|
+
const text = data.choices?.[0]?.message?.content;
|
|
111
|
+
if (typeof text !== 'string' || !text.trim()) {
|
|
112
|
+
return { error: `compaction model '${model}' returned an empty summary`, status: 502 };
|
|
113
|
+
}
|
|
114
|
+
if (data.choices?.[0]?.finish_reason === 'length') {
|
|
115
|
+
return { error: `compaction model '${model}' hit its output limit — the summary is truncated`, status: 502 };
|
|
116
|
+
}
|
|
117
|
+
return { summary: text.trim() };
|
|
118
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { SnapshotTurn } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Target-neutral handoff message list (ADR-0039).
|
|
4
|
+
*
|
|
5
|
+
* The three supported clients disagree on their on-disk shapes, but they agree
|
|
6
|
+
* on the SEMANTICS: a linear transcript of user turns, assistant turns carrying
|
|
7
|
+
* text/thinking/tool-calls, and tool results. This module builds that neutral
|
|
8
|
+
* list once from the snapshot plane; each writer then renders it into its own
|
|
9
|
+
* format. Building it once is what keeps the three targets from drifting.
|
|
10
|
+
*
|
|
11
|
+
* The neutral parts deliberately mirror pi/omp's `content[]` vocabulary
|
|
12
|
+
* (`text` / `thinking` / `toolCall` / `image`) because that is the richest of
|
|
13
|
+
* the three; opencode's flatter shape is a projection of it, never the reverse.
|
|
14
|
+
*/
|
|
15
|
+
export type HandoffPart = {
|
|
16
|
+
type: 'text';
|
|
17
|
+
text: string;
|
|
18
|
+
} | {
|
|
19
|
+
type: 'thinking';
|
|
20
|
+
thinking: string;
|
|
21
|
+
thinkingSignature?: string;
|
|
22
|
+
} | {
|
|
23
|
+
type: 'toolCall';
|
|
24
|
+
id: string;
|
|
25
|
+
name: string;
|
|
26
|
+
arguments: unknown;
|
|
27
|
+
} | {
|
|
28
|
+
type: 'image';
|
|
29
|
+
data: string;
|
|
30
|
+
mimeType: string;
|
|
31
|
+
};
|
|
32
|
+
export interface HandoffMessage {
|
|
33
|
+
role: 'user' | 'assistant' | 'toolResult' | 'system';
|
|
34
|
+
content: HandoffPart[];
|
|
35
|
+
/** toolResult only: the call it answers. */
|
|
36
|
+
toolCallId?: string;
|
|
37
|
+
toolName?: string;
|
|
38
|
+
isError?: boolean;
|
|
39
|
+
/** Original timestamp (ms) when known. */
|
|
40
|
+
ts: number;
|
|
41
|
+
}
|
|
42
|
+
export interface HandoffTranscript {
|
|
43
|
+
/** First user text — the client's session title seed. */
|
|
44
|
+
title: string;
|
|
45
|
+
messages: HandoffMessage[];
|
|
46
|
+
/** Model refs actually served, in order (first = initial model). */
|
|
47
|
+
models: string[];
|
|
48
|
+
/** Offloaded media still referenced by an unresolved ref (writer must inline it). */
|
|
49
|
+
unresolvedMedia: string[];
|
|
50
|
+
}
|
|
51
|
+
/** A media resolver supplied by the store (sha.ext -> raw base64). */
|
|
52
|
+
export type MediaResolver = (key: string) => {
|
|
53
|
+
data: string;
|
|
54
|
+
mimeType: string;
|
|
55
|
+
} | null;
|
|
56
|
+
/**
|
|
57
|
+
* Flatten the last COMPLETED record's accumulated history plus its own output
|
|
58
|
+
* into a neutral transcript. That record already carries the FULL prior history
|
|
59
|
+
* (the client resent it), so nothing is folded across records — this is the
|
|
60
|
+
* same discipline as `resumeView`, applied to three targets.
|
|
61
|
+
*
|
|
62
|
+
* ABORTED / FAILED records are excluded as the migration ANCHOR (ADR-0040):
|
|
63
|
+
* their output is a fragment the model never finished, so handing it to another
|
|
64
|
+
* client would present an unfinished answer as history. The anchor is the last
|
|
65
|
+
* COMPLETED record; a session with none has nothing to migrate. (Aborted
|
|
66
|
+
* records remain visible in the console — this only governs what LEAVES the
|
|
67
|
+
* gateway.)
|
|
68
|
+
*/
|
|
69
|
+
export declare function buildHandoffTranscript(turns: readonly SnapshotTurn[], resolve: MediaResolver): HandoffTranscript;
|
|
70
|
+
/** Short title for a client's session header. */
|
|
71
|
+
export declare function clipTitle(text: string, max?: number): string;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
const REF_PREFIX = 'ocr-snapshot-media:';
|
|
2
|
+
function inlineMedia(part, resolve, missing) {
|
|
3
|
+
const source = part.source;
|
|
4
|
+
if (!source)
|
|
5
|
+
return null;
|
|
6
|
+
if (source.kind === 'base64' && typeof source.data === 'string') {
|
|
7
|
+
const raw = source.data.startsWith(REF_PREFIX) ? source.data.slice(REF_PREFIX.length) : null;
|
|
8
|
+
if (raw) {
|
|
9
|
+
const hit = resolve(raw);
|
|
10
|
+
if (!hit) {
|
|
11
|
+
missing.add(raw);
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
return { type: 'image', data: hit.data, mimeType: hit.mimeType };
|
|
15
|
+
}
|
|
16
|
+
return { type: 'image', data: source.data, mimeType: source.mediaType ?? 'application/octet-stream' };
|
|
17
|
+
}
|
|
18
|
+
// A remote URL cannot be inlined offline; drop it rather than write a dangling
|
|
19
|
+
// link into another client's history.
|
|
20
|
+
if (source.kind === 'url')
|
|
21
|
+
return null;
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
function partsOf(content, resolve, missing) {
|
|
25
|
+
const out = [];
|
|
26
|
+
for (const part of content) {
|
|
27
|
+
if (part.type === 'text') {
|
|
28
|
+
if (part.text)
|
|
29
|
+
out.push({ type: 'text', text: part.text });
|
|
30
|
+
}
|
|
31
|
+
else if (part.type === 'refusal') {
|
|
32
|
+
if (part.text)
|
|
33
|
+
out.push({ type: 'text', text: `[refusal] ${part.text}` });
|
|
34
|
+
}
|
|
35
|
+
else if (part.type === 'image' || part.type === 'file' || part.type === 'audio') {
|
|
36
|
+
const media = inlineMedia(part, resolve, missing);
|
|
37
|
+
if (media)
|
|
38
|
+
out.push(media);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
function parseArgs(args) {
|
|
44
|
+
try {
|
|
45
|
+
return JSON.parse(args);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return args;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** Convert one canonical item into zero or more neutral messages. */
|
|
52
|
+
function messageFromItem(item, resolve, missing) {
|
|
53
|
+
switch (item.type) {
|
|
54
|
+
case 'message': {
|
|
55
|
+
const content = partsOf(item.content, resolve, missing);
|
|
56
|
+
if (!content.length)
|
|
57
|
+
return null;
|
|
58
|
+
const role = item.role === 'assistant' ? 'assistant' : item.role === 'user' ? 'user' : 'system';
|
|
59
|
+
return { role, content, ts: 0 };
|
|
60
|
+
}
|
|
61
|
+
case 'tool_call':
|
|
62
|
+
return {
|
|
63
|
+
role: 'assistant',
|
|
64
|
+
content: [{ type: 'toolCall', id: item.callId ?? item.id, name: item.name, arguments: parseArgs(item.arguments) }],
|
|
65
|
+
ts: 0,
|
|
66
|
+
};
|
|
67
|
+
case 'tool_result': {
|
|
68
|
+
const content = partsOf(item.content, resolve, missing);
|
|
69
|
+
return {
|
|
70
|
+
role: 'toolResult',
|
|
71
|
+
content: content.length ? content : [{ type: 'text', text: '' }],
|
|
72
|
+
toolCallId: item.callId,
|
|
73
|
+
isError: item.isError === true,
|
|
74
|
+
ts: 0,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
case 'reasoning': {
|
|
78
|
+
const text = item.text ?? (item.summary ?? []).join('\n');
|
|
79
|
+
if (!text)
|
|
80
|
+
return null;
|
|
81
|
+
return {
|
|
82
|
+
role: 'assistant',
|
|
83
|
+
content: [{ type: 'thinking', thinking: text, ...(item.signature ? { thinkingSignature: item.signature } : {}) }],
|
|
84
|
+
ts: 0,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
default:
|
|
88
|
+
// item_reference / extension: not portable to another client's history.
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** Tool names by call id, so a toolResult can name the tool it answers. */
|
|
93
|
+
function toolNamesById(input) {
|
|
94
|
+
const names = new Map();
|
|
95
|
+
for (const item of input) {
|
|
96
|
+
if (item.type === 'tool_call')
|
|
97
|
+
names.set(item.callId ?? item.id, item.name);
|
|
98
|
+
}
|
|
99
|
+
return names;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Flatten the last COMPLETED record's accumulated history plus its own output
|
|
103
|
+
* into a neutral transcript. That record already carries the FULL prior history
|
|
104
|
+
* (the client resent it), so nothing is folded across records — this is the
|
|
105
|
+
* same discipline as `resumeView`, applied to three targets.
|
|
106
|
+
*
|
|
107
|
+
* ABORTED / FAILED records are excluded as the migration ANCHOR (ADR-0040):
|
|
108
|
+
* their output is a fragment the model never finished, so handing it to another
|
|
109
|
+
* client would present an unfinished answer as history. The anchor is the last
|
|
110
|
+
* COMPLETED record; a session with none has nothing to migrate. (Aborted
|
|
111
|
+
* records remain visible in the console — this only governs what LEAVES the
|
|
112
|
+
* gateway.)
|
|
113
|
+
*/
|
|
114
|
+
export function buildHandoffTranscript(turns, resolve) {
|
|
115
|
+
const completed = turns.filter((t) => (t.outcome ?? 'completed') === 'completed');
|
|
116
|
+
if (!completed.length)
|
|
117
|
+
return { title: '', messages: [], models: [], unresolvedMedia: [] };
|
|
118
|
+
const last = completed[completed.length - 1];
|
|
119
|
+
const missing = new Set();
|
|
120
|
+
const toolNames = toolNamesById(last.request.input);
|
|
121
|
+
const messages = [];
|
|
122
|
+
// Input items, in canonical order. `reasoning` is skipped on the INPUT side:
|
|
123
|
+
// clients do not resend prior thinking, and re-inserting it as a new message
|
|
124
|
+
// would duplicate what the assistant turn already carries.
|
|
125
|
+
for (const item of last.request.input) {
|
|
126
|
+
if (item.type === 'reasoning')
|
|
127
|
+
continue;
|
|
128
|
+
const msg = messageFromItem(item, resolve, missing);
|
|
129
|
+
if (msg)
|
|
130
|
+
messages.push({ ...msg, ts: last.ts });
|
|
131
|
+
}
|
|
132
|
+
// This turn's output, appended as the assistant reply.
|
|
133
|
+
const outputToolNames = new Map();
|
|
134
|
+
for (const item of last.response.output) {
|
|
135
|
+
if (item.type === 'tool_call')
|
|
136
|
+
outputToolNames.set(item.callId ?? item.id, item.name);
|
|
137
|
+
const msg = messageFromItem(item, resolve, missing);
|
|
138
|
+
if (!msg)
|
|
139
|
+
continue;
|
|
140
|
+
if (msg.role === 'toolResult' && msg.toolCallId) {
|
|
141
|
+
msg.toolName = outputToolNames.get(msg.toolCallId) ?? toolNames.get(msg.toolCallId);
|
|
142
|
+
}
|
|
143
|
+
messages.push({ ...msg, ts: last.ts });
|
|
144
|
+
}
|
|
145
|
+
// Title = first user text across the whole session (fall back to the last turn).
|
|
146
|
+
let title = '';
|
|
147
|
+
for (const turn of turns) {
|
|
148
|
+
for (const item of turn.request.input) {
|
|
149
|
+
if (item.type === 'message' && item.role === 'user') {
|
|
150
|
+
const text = item.content.map((p) => (p.type === 'text' ? p.text : '')).join('').trim();
|
|
151
|
+
if (text) {
|
|
152
|
+
title = text;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (title)
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
const models = [...new Set(completed.map((t) => t.servedModel).filter(Boolean))];
|
|
161
|
+
return { title, messages, models, unresolvedMedia: [...missing] };
|
|
162
|
+
}
|
|
163
|
+
/** Short title for a client's session header. */
|
|
164
|
+
export function clipTitle(text, max = 80) {
|
|
165
|
+
const line = text.split(/\r?\n/).map((s) => s.trim()).find(Boolean) ?? '';
|
|
166
|
+
return line.length > max ? line.slice(0, max - 1) + '…' : line;
|
|
167
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content-addressed media write, shared by the main-thread store and its
|
|
3
|
+
* write worker so the two can never disagree about layout or dedup.
|
|
4
|
+
*
|
|
5
|
+
* Deduped by `existsSync`: a screenshot resent across 20 turns lands once.
|
|
6
|
+
* Atomic tmp+rename so a crash never leaves a half-written payload under a
|
|
7
|
+
* valid digest name.
|
|
8
|
+
*/
|
|
9
|
+
export declare function storeMediaPayload(rootDir: string, raw: Buffer, sha256: string, ext: string): boolean;
|
|
10
|
+
export declare function ensureDirSync(dir: string): void;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { renameWithRetry } from '../utils/atomic-write.js';
|
|
4
|
+
/**
|
|
5
|
+
* Content-addressed media write, shared by the main-thread store and its
|
|
6
|
+
* write worker so the two can never disagree about layout or dedup.
|
|
7
|
+
*
|
|
8
|
+
* Deduped by `existsSync`: a screenshot resent across 20 turns lands once.
|
|
9
|
+
* Atomic tmp+rename so a crash never leaves a half-written payload under a
|
|
10
|
+
* valid digest name.
|
|
11
|
+
*/
|
|
12
|
+
export function storeMediaPayload(rootDir, raw, sha256, ext) {
|
|
13
|
+
const dir = path.join(rootDir, 'media');
|
|
14
|
+
const name = `${sha256}.${ext}`;
|
|
15
|
+
const fp = path.join(dir, name);
|
|
16
|
+
try {
|
|
17
|
+
if (fs.existsSync(fp))
|
|
18
|
+
return true;
|
|
19
|
+
ensureDirSync(dir);
|
|
20
|
+
const tmp = `${fp}.${process.pid}.${crypto.randomUUID().slice(0, 8)}.tmp`;
|
|
21
|
+
try {
|
|
22
|
+
fs.writeFileSync(tmp, raw);
|
|
23
|
+
renameWithRetry(tmp, fp);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
try {
|
|
27
|
+
fs.rmSync(tmp, { force: true });
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// tmp already gone
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
console.warn(`[snapshot] media store error for ${name}: ${err.message}`);
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export function ensureDirSync(dir) {
|
|
42
|
+
if (fs.existsSync(dir))
|
|
43
|
+
return;
|
|
44
|
+
try {
|
|
45
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// the append that follows fails open with a warn
|
|
49
|
+
}
|
|
50
|
+
}
|