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,228 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { isMainThread, parentPort, workerData } from 'node:worker_threads';
|
|
4
|
+
import { PLUGIN_DAY_FILE_RE, PROXY_BODY_REF_RE, PROXY_DAY_FILE_RE } from './traffic-files.js';
|
|
5
|
+
const listFiles = (dir, re) => {
|
|
6
|
+
try {
|
|
7
|
+
return fs.readdirSync(dir).filter((f) => re.test(f)).sort();
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return []; // missing dir = nothing to delete
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const statOrNull = (file) => {
|
|
14
|
+
try {
|
|
15
|
+
return fs.statSync(file);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
const unlinkQuiet = (file) => {
|
|
22
|
+
try {
|
|
23
|
+
fs.unlinkSync(file);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
// Best effort: a locked file leaves the row deleted and a stray blob.
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Rewrite one JSONL file without the rows `shouldDrop` accepts; returns them.
|
|
31
|
+
*
|
|
32
|
+
* Writers on the MAIN thread (the proxy persistence queue) and in other
|
|
33
|
+
* processes (plugin feeds) keep appending to the same path, so the rewrite
|
|
34
|
+
* lands through `<file>.ocr-delete-tmp` + rename. The byte count read must
|
|
35
|
+
* equal the size on disk immediately before the rename; a file that grew
|
|
36
|
+
* while it was read is re-read (bounded) so an append that raced the read is
|
|
37
|
+
* judged instead of replaced. Only an append that opens the old path inside
|
|
38
|
+
* the stat→rename window can be lost — one syscall wide.
|
|
39
|
+
*
|
|
40
|
+
* A line that does not parse is a torn tail (crash mid-append) and is kept:
|
|
41
|
+
* it is not ours to judge.
|
|
42
|
+
*/
|
|
43
|
+
function rewriteJsonl(file, shouldDrop) {
|
|
44
|
+
for (let attempt = 0; attempt < 5; attempt++) {
|
|
45
|
+
const before = statOrNull(file);
|
|
46
|
+
if (!before)
|
|
47
|
+
return [];
|
|
48
|
+
let buf;
|
|
49
|
+
try {
|
|
50
|
+
buf = fs.readFileSync(file);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
const after = statOrNull(file);
|
|
56
|
+
if (!after || after.size !== buf.length || after.mtimeMs !== before.mtimeMs)
|
|
57
|
+
continue;
|
|
58
|
+
const kept = [];
|
|
59
|
+
const dropped = [];
|
|
60
|
+
for (const line of buf.toString('utf8').split(/\r?\n/)) {
|
|
61
|
+
if (!line)
|
|
62
|
+
continue;
|
|
63
|
+
let row;
|
|
64
|
+
try {
|
|
65
|
+
row = JSON.parse(line);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
kept.push(line);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (shouldDrop(row))
|
|
72
|
+
dropped.push(row);
|
|
73
|
+
else
|
|
74
|
+
kept.push(line);
|
|
75
|
+
}
|
|
76
|
+
if (dropped.length === 0)
|
|
77
|
+
return dropped;
|
|
78
|
+
const tmp = `${file}.ocr-delete-tmp`;
|
|
79
|
+
try {
|
|
80
|
+
if (kept.length > 0)
|
|
81
|
+
fs.writeFileSync(tmp, kept.join('\n') + '\n');
|
|
82
|
+
const final = statOrNull(file);
|
|
83
|
+
if (!final || final.size !== buf.length || final.mtimeMs !== after.mtimeMs) {
|
|
84
|
+
unlinkQuiet(tmp);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (kept.length > 0)
|
|
88
|
+
fs.renameSync(tmp, file);
|
|
89
|
+
else
|
|
90
|
+
fs.unlinkSync(file);
|
|
91
|
+
return dropped;
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
unlinkQuiet(tmp);
|
|
95
|
+
return []; // locked file (Windows) — the row survives; the operator retries
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
100
|
+
const matchesProxyFilter = (row, f) => {
|
|
101
|
+
if (f.kind && row.kind !== f.kind)
|
|
102
|
+
return false;
|
|
103
|
+
if (f.method && row.method !== f.method)
|
|
104
|
+
return false;
|
|
105
|
+
if (f.status && String(row.status ?? '') !== f.status)
|
|
106
|
+
return false;
|
|
107
|
+
if (f.host && row.host !== f.host)
|
|
108
|
+
return false;
|
|
109
|
+
return true;
|
|
110
|
+
};
|
|
111
|
+
const matchesFeedFilter = (row, f) => {
|
|
112
|
+
if (f.method && row.method !== f.method)
|
|
113
|
+
return false;
|
|
114
|
+
if (f.host && (row.authority ?? '').toLowerCase() !== f.host)
|
|
115
|
+
return false;
|
|
116
|
+
if (f.status && String(row.status ?? '') !== f.status)
|
|
117
|
+
return false;
|
|
118
|
+
return true;
|
|
119
|
+
};
|
|
120
|
+
/** Drop the sidecars of deleted plugin rows: `<bodies>/<day>/<id>-<side>.bin`. */
|
|
121
|
+
function removePluginBodyFiles(dir, ids) {
|
|
122
|
+
const bodiesRoot = path.join(dir, 'bodies');
|
|
123
|
+
let days;
|
|
124
|
+
try {
|
|
125
|
+
days = fs.readdirSync(bodiesRoot);
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
for (const day of days) {
|
|
131
|
+
const dayDir = path.join(bodiesRoot, day);
|
|
132
|
+
let names;
|
|
133
|
+
try {
|
|
134
|
+
names = fs.readdirSync(dayDir);
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
for (const name of names) {
|
|
140
|
+
if (!/-(?:req|res)\.bin$/.test(name))
|
|
141
|
+
continue;
|
|
142
|
+
if (ids.has(name.replace(/-(?:req|res)\.bin$/, '')))
|
|
143
|
+
unlinkQuiet(path.join(dayDir, name));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The whole persisted rewrite, synchronously (the caller is already off the
|
|
149
|
+
* event loop). Exported so tests drive the real rewrite in-process instead of
|
|
150
|
+
* spawning a thread for every case.
|
|
151
|
+
*/
|
|
152
|
+
export function runTrafficDelete(target, onProgress) {
|
|
153
|
+
const { proxyDir, pluginDirs, filters } = target;
|
|
154
|
+
const jobs = [];
|
|
155
|
+
if (proxyDir) {
|
|
156
|
+
for (const f of listFiles(proxyDir, PROXY_DAY_FILE_RE))
|
|
157
|
+
jobs.push({ pluginId: null, file: path.join(proxyDir, f) });
|
|
158
|
+
}
|
|
159
|
+
// Plugin rows are ALWAYS `http` (the console's kindOf): a CONNECT/WS filter
|
|
160
|
+
// cannot match them, while the default http filter must.
|
|
161
|
+
if (!filters.kind || filters.kind === 'http') {
|
|
162
|
+
for (const p of pluginDirs) {
|
|
163
|
+
for (const f of listFiles(p.dir, PLUGIN_DAY_FILE_RE))
|
|
164
|
+
jobs.push({ pluginId: p.id, file: path.join(p.dir, f) });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const deleted = { proxy: 0, plugins: {} };
|
|
168
|
+
const bodyRefs = new Set();
|
|
169
|
+
const pluginBodyIds = new Map();
|
|
170
|
+
let filesDone = 0;
|
|
171
|
+
for (const job of jobs) {
|
|
172
|
+
if (job.pluginId === null) {
|
|
173
|
+
for (const row of rewriteJsonl(job.file, (r) => matchesProxyFilter(r, filters))) {
|
|
174
|
+
deleted.proxy++;
|
|
175
|
+
if (typeof row.reqBodyFile === 'string')
|
|
176
|
+
bodyRefs.add(row.reqBodyFile);
|
|
177
|
+
if (typeof row.resBodyFile === 'string')
|
|
178
|
+
bodyRefs.add(row.resBodyFile);
|
|
179
|
+
}
|
|
180
|
+
for (const ref of bodyRefs) {
|
|
181
|
+
const m = PROXY_BODY_REF_RE.exec(ref);
|
|
182
|
+
if (!m)
|
|
183
|
+
continue;
|
|
184
|
+
unlinkQuiet(path.join(proxyDir, 'bodies', m[1], `${m[2]}.bin`));
|
|
185
|
+
}
|
|
186
|
+
bodyRefs.clear();
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
for (const row of rewriteJsonl(job.file, (r) => matchesFeedFilter(r, filters))) {
|
|
190
|
+
deleted.plugins[job.pluginId] = (deleted.plugins[job.pluginId] ?? 0) + 1;
|
|
191
|
+
if (typeof row.id === 'string' && row.id) {
|
|
192
|
+
let ids = pluginBodyIds.get(job.pluginId);
|
|
193
|
+
if (!ids)
|
|
194
|
+
pluginBodyIds.set(job.pluginId, (ids = new Set()));
|
|
195
|
+
ids.add(row.id);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
filesDone++;
|
|
200
|
+
onProgress?.(filesDone, jobs.length, { proxy: deleted.proxy, plugins: { ...deleted.plugins } });
|
|
201
|
+
}
|
|
202
|
+
for (const [pluginId, ids] of pluginBodyIds) {
|
|
203
|
+
const plugin = pluginDirs.find((p) => p.id === pluginId);
|
|
204
|
+
if (plugin)
|
|
205
|
+
removePluginBodyFiles(plugin.dir, ids);
|
|
206
|
+
}
|
|
207
|
+
return deleted;
|
|
208
|
+
}
|
|
209
|
+
// Worker entry. Imported in-process (tests) this is inert; the main thread
|
|
210
|
+
// must never run the rewrite inline — that is the whole point of the thread.
|
|
211
|
+
if (!isMainThread && parentPort) {
|
|
212
|
+
const port = parentPort;
|
|
213
|
+
const target = workerData;
|
|
214
|
+
let filesTotal = 0;
|
|
215
|
+
try {
|
|
216
|
+
const deleted = runTrafficDelete(target, (filesDone, total, snapshot) => {
|
|
217
|
+
filesTotal = total;
|
|
218
|
+
port.postMessage({ type: 'progress', filesDone, filesTotal, deleted: snapshot });
|
|
219
|
+
});
|
|
220
|
+
port.postMessage({ type: 'done', filesDone: filesTotal, filesTotal, deleted });
|
|
221
|
+
}
|
|
222
|
+
catch (err) {
|
|
223
|
+
port.postMessage({ type: 'error', message: err instanceof Error ? err.message : String(err) });
|
|
224
|
+
}
|
|
225
|
+
finally {
|
|
226
|
+
port.close();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Day-file and body-ref grammars shared by the ingress access-log reader/writer
|
|
3
|
+
* and the background history-delete worker. The worker runs in its own thread
|
|
4
|
+
* (and its own compiled entrypoint in the desktop sidecar), so it cannot reach
|
|
5
|
+
* the runtime state those modules hold — only these pure grammars travel.
|
|
6
|
+
*/
|
|
7
|
+
export declare const PROXY_DAY_FILE_RE: RegExp;
|
|
8
|
+
export declare const PLUGIN_DAY_FILE_RE: RegExp;
|
|
9
|
+
/** `<YYYYMMDD>/<proxy event id>-<side>.bin` — a proxy-log body sidecar ref. */
|
|
10
|
+
export declare const PROXY_BODY_REF_RE: RegExp;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Day-file and body-ref grammars shared by the ingress access-log reader/writer
|
|
3
|
+
* and the background history-delete worker. The worker runs in its own thread
|
|
4
|
+
* (and its own compiled entrypoint in the desktop sidecar), so it cannot reach
|
|
5
|
+
* the runtime state those modules hold — only these pure grammars travel.
|
|
6
|
+
*/
|
|
7
|
+
export const PROXY_DAY_FILE_RE = /^access-log-\d{8}\.jsonl$/;
|
|
8
|
+
export const PLUGIN_DAY_FILE_RE = /^\d{4}-\d{2}-\d{2}\.jsonl$/;
|
|
9
|
+
/** `<YYYYMMDD>/<proxy event id>-<side>.bin` — a proxy-log body sidecar ref. */
|
|
10
|
+
export const PROXY_BODY_REF_RE = /^(\d{8})\/(pa-[a-z0-9-]+-(?:req|res))\.bin$/;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import tls from 'node:tls';
|
|
2
|
+
import type { IngressProxyConfig } from '../config/types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Upstream TLS trust for the proxy's ORIGIN legs (ADR-0019).
|
|
5
|
+
*
|
|
6
|
+
* The ingress proxy validates the REAL target certificate on every origin leg
|
|
7
|
+
* it originates (h1 direct, h1 through the outbound proxy, the h2 relay and the
|
|
8
|
+
* Upgrade relay) — that is the whole point of not stripping verification. The
|
|
9
|
+
* consequence is that an origin signed by a PRIVATE CA is unreachable: a
|
|
10
|
+
* corporate TLS-inspection chain, an internal service, a staging box with a
|
|
11
|
+
* self-signed cert. `ingressProxy.upstreamTls.caFile` appends that CA to the
|
|
12
|
+
* system trust store (never replaces it); `insecure` disables verification
|
|
13
|
+
* entirely and exists only as an operator escape hatch.
|
|
14
|
+
*
|
|
15
|
+
* Two properties this module is responsible for:
|
|
16
|
+
* - HOT APPLY. The options are resolved per request off the live config ref,
|
|
17
|
+
* so a console save takes effect without a restart. The CA bundle is cached
|
|
18
|
+
* and re-stat'ed on a short TTL, so rotating the file on disk is picked up
|
|
19
|
+
* too (the TTL is a syscall budget, not a correctness knob).
|
|
20
|
+
* - SESSION SAFETY. `key` is a discriminator for the h2 session pool: a trust
|
|
21
|
+
* change must never reuse a session established under the old policy.
|
|
22
|
+
*
|
|
23
|
+
* A missing/unreadable `caFile` warns ONCE per path and falls back to the system
|
|
24
|
+
* trust store — it does NOT silently disable verification. Fail closed, loudly.
|
|
25
|
+
*/
|
|
26
|
+
type UpstreamTlsConfig = NonNullable<IngressProxyConfig['upstreamTls']>;
|
|
27
|
+
/** The resolved options + the pool-key discriminator, as one value. */
|
|
28
|
+
export interface UpstreamTlsOptions {
|
|
29
|
+
/** `undefined` = system trust store (the historical behaviour). */
|
|
30
|
+
opts?: tls.ConnectionOptions;
|
|
31
|
+
/** Pool-key discriminator: `explicit` (test seam) / `sys` / `verify|<ca>@<mtime>` / `insecure`. */
|
|
32
|
+
key: string;
|
|
33
|
+
}
|
|
34
|
+
/** Wire the live `ingressProxy.upstreamTls` reader (index.ts is the only writer). */
|
|
35
|
+
export declare function setUpstreamTlsReader(next: (() => UpstreamTlsConfig | undefined) | undefined): void;
|
|
36
|
+
/**
|
|
37
|
+
* The TLS options for one origin leg, from the LIVE config.
|
|
38
|
+
*
|
|
39
|
+
* `explicit` is the test seam (`IngressProxyDeps.upstreamTls`). It is MERGED
|
|
40
|
+
* over the live options field by field, so a test can inject a `lookup` (the
|
|
41
|
+
* only way to reach a fake origin without inventing DNS entries) while still
|
|
42
|
+
* exercising the real `caFile` / `insecure` resolution. Production never sets
|
|
43
|
+
* it, so the live reader is the only path there.
|
|
44
|
+
*/
|
|
45
|
+
export declare function resolveUpstreamTls(explicit?: tls.ConnectionOptions): UpstreamTlsOptions;
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import tls from 'node:tls';
|
|
3
|
+
let reader;
|
|
4
|
+
/** Wire the live `ingressProxy.upstreamTls` reader (index.ts is the only writer). */
|
|
5
|
+
export function setUpstreamTlsReader(next) {
|
|
6
|
+
reader = next;
|
|
7
|
+
}
|
|
8
|
+
const bundles = new Map();
|
|
9
|
+
/** Re-stat budget: picks up a rotated CA file without a stat per request. */
|
|
10
|
+
const CA_RESTAT_MS = 5_000;
|
|
11
|
+
const warned = new Set();
|
|
12
|
+
function warnOnce(caFile, err) {
|
|
13
|
+
const key = `${caFile}:${String(err?.code ?? '')}`;
|
|
14
|
+
if (warned.has(key))
|
|
15
|
+
return;
|
|
16
|
+
warned.add(key);
|
|
17
|
+
console.warn(`[IngressProxy] upstreamTls.caFile "${caFile}" could not be read (${err?.message || err}) — ` +
|
|
18
|
+
'falling back to the system trust store. Upstream certificates will NOT be trusted from that CA.');
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Read + cache the trust list: the SYSTEM roots followed by the operator's PEM.
|
|
22
|
+
*
|
|
23
|
+
* `ca` is not additive in `tls.connect` — passing it REPLACES the default root
|
|
24
|
+
* store (Node and Bun alike), so a bundle set here would otherwise make every
|
|
25
|
+
* publicly-signed origin untrusted, the exact opposite of "append". The roots
|
|
26
|
+
* are therefore spelled out explicitly. `tls.rootCertificates` is available on
|
|
27
|
+
* Bun 1.3.14 (145 roots, verified).
|
|
28
|
+
*
|
|
29
|
+
* `NODE_EXTRA_CA_CERTS` is NOT part of `tls.rootCertificates`, so an install
|
|
30
|
+
* that relies on it should name that file here instead.
|
|
31
|
+
*
|
|
32
|
+
* Returns `undefined` when the file is unreadable (the system store stays).
|
|
33
|
+
*/
|
|
34
|
+
function loadCaBundle(caFile) {
|
|
35
|
+
try {
|
|
36
|
+
const st = fs.statSync(caFile);
|
|
37
|
+
const hit = bundles.get(caFile);
|
|
38
|
+
if (hit && hit.mtimeMs === st.mtimeMs && hit.size === st.size && Date.now() - hit.readAt < CA_RESTAT_MS) {
|
|
39
|
+
return hit.ca;
|
|
40
|
+
}
|
|
41
|
+
const pem = fs.readFileSync(caFile, 'utf8');
|
|
42
|
+
const roots = tls.rootCertificates;
|
|
43
|
+
if (!roots?.length) {
|
|
44
|
+
// No readable default store: the bundle would silently REPLACE trust.
|
|
45
|
+
warnOnce(caFile, new Error('the runtime exposes no system root certificates'));
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
const ca = [...roots, pem];
|
|
49
|
+
bundles.set(caFile, { mtimeMs: st.mtimeMs, size: st.size, ca, readAt: Date.now() });
|
|
50
|
+
return ca;
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
warnOnce(caFile, err);
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The TLS options for one origin leg, from the LIVE config.
|
|
59
|
+
*
|
|
60
|
+
* `explicit` is the test seam (`IngressProxyDeps.upstreamTls`). It is MERGED
|
|
61
|
+
* over the live options field by field, so a test can inject a `lookup` (the
|
|
62
|
+
* only way to reach a fake origin without inventing DNS entries) while still
|
|
63
|
+
* exercising the real `caFile` / `insecure` resolution. Production never sets
|
|
64
|
+
* it, so the live reader is the only path there.
|
|
65
|
+
*/
|
|
66
|
+
export function resolveUpstreamTls(explicit) {
|
|
67
|
+
const live = liveUpstreamTls();
|
|
68
|
+
if (!explicit)
|
|
69
|
+
return live;
|
|
70
|
+
return { opts: { ...(live.opts || {}), ...explicit }, key: `${live.key}+explicit` };
|
|
71
|
+
}
|
|
72
|
+
/** The config-only resolution (no test seam). */
|
|
73
|
+
function liveUpstreamTls() {
|
|
74
|
+
const cfg = reader?.();
|
|
75
|
+
const caFile = cfg?.caFile?.trim();
|
|
76
|
+
const insecure = cfg?.insecure === true;
|
|
77
|
+
if (!caFile && !insecure)
|
|
78
|
+
return { key: 'sys' };
|
|
79
|
+
const opts = {};
|
|
80
|
+
const parts = [];
|
|
81
|
+
if (insecure) {
|
|
82
|
+
opts.rejectUnauthorized = false;
|
|
83
|
+
parts.push('insecure');
|
|
84
|
+
// A trust list is pointless (and costs a full CA parse per connection) when
|
|
85
|
+
// verification is off, so `insecure` wins over `caFile` — but the key keeps
|
|
86
|
+
// the path so flipping the flag back re-keys the pool.
|
|
87
|
+
if (caFile)
|
|
88
|
+
parts.push(`${caFile}@insecure`);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
parts.push('verify');
|
|
92
|
+
}
|
|
93
|
+
if (caFile && !insecure) {
|
|
94
|
+
const ca = loadCaBundle(caFile);
|
|
95
|
+
if (ca) {
|
|
96
|
+
opts.ca = ca;
|
|
97
|
+
const stamp = bundles.get(caFile)?.mtimeMs ?? 'unloaded';
|
|
98
|
+
parts.push(`${caFile}@${stamp}`);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
parts.push(`${caFile}@unreadable`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return { opts: Object.keys(opts).length ? opts : undefined, key: parts.join('|') };
|
|
105
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket frame observation (RFC 6455) — ADR-0020.
|
|
3
|
+
*
|
|
4
|
+
* The proxy relays WebSocket bytes verbatim: an upgrade is a single hop, so the
|
|
5
|
+
* client's own handshake and frames are exactly what the origin must see, and
|
|
6
|
+
* nothing is re-serialized or modified. That is a deliberate boundary — vendor
|
|
7
|
+
* SEMANTICS on a claimed host belong to a client-gateway plugin (ADR-0017), not
|
|
8
|
+
* to the core proxy.
|
|
9
|
+
*
|
|
10
|
+
* What the core still owes the operator is OBSERVATION: "the socket is open" is
|
|
11
|
+
* not an answer when a realtime LLM API misbehaves. This module is a streaming,
|
|
12
|
+
* allocation-light frame counter that rides the same chunks the relay forwards
|
|
13
|
+
* — it never buffers a message, never rewrites a byte, and never throws on a
|
|
14
|
+
* hostile stream (an unparseable tail simply stops the count).
|
|
15
|
+
*
|
|
16
|
+
* Directions: `c2s` = client → origin (masked per RFC 6455), `s2c` = origin →
|
|
17
|
+
* client (unmasked). The mask bit is not enforced: this is an observer, not a
|
|
18
|
+
* validator.
|
|
19
|
+
*/
|
|
20
|
+
export type WsDirection = 'c2s' | 's2c';
|
|
21
|
+
export interface WsObservation {
|
|
22
|
+
frames: number;
|
|
23
|
+
/** Data frames (opcode 1/2, continuation excluded) — one per message. */
|
|
24
|
+
messages: number;
|
|
25
|
+
text: number;
|
|
26
|
+
binary: number;
|
|
27
|
+
ping: number;
|
|
28
|
+
pong: number;
|
|
29
|
+
/** Close frame, when one was seen. */
|
|
30
|
+
close?: {
|
|
31
|
+
code?: number;
|
|
32
|
+
reason?: string;
|
|
33
|
+
};
|
|
34
|
+
/** Payload bytes (headers excluded), both directions. */
|
|
35
|
+
payloadBytes: number;
|
|
36
|
+
/** true = the frame budget ran out; the counters are a prefix, not a total. */
|
|
37
|
+
truncated: boolean;
|
|
38
|
+
}
|
|
39
|
+
export interface WsObserver {
|
|
40
|
+
push(dir: WsDirection, chunk: Buffer): void;
|
|
41
|
+
observation(): WsObservation;
|
|
42
|
+
/** One-line ledger suffix: `ws: 12 msg (9 text / 3 binary), 1 ping, close 1000`. */
|
|
43
|
+
note(): string;
|
|
44
|
+
}
|
|
45
|
+
export declare function createWsObserver(): WsObserver;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket frame observation (RFC 6455) — ADR-0020.
|
|
3
|
+
*
|
|
4
|
+
* The proxy relays WebSocket bytes verbatim: an upgrade is a single hop, so the
|
|
5
|
+
* client's own handshake and frames are exactly what the origin must see, and
|
|
6
|
+
* nothing is re-serialized or modified. That is a deliberate boundary — vendor
|
|
7
|
+
* SEMANTICS on a claimed host belong to a client-gateway plugin (ADR-0017), not
|
|
8
|
+
* to the core proxy.
|
|
9
|
+
*
|
|
10
|
+
* What the core still owes the operator is OBSERVATION: "the socket is open" is
|
|
11
|
+
* not an answer when a realtime LLM API misbehaves. This module is a streaming,
|
|
12
|
+
* allocation-light frame counter that rides the same chunks the relay forwards
|
|
13
|
+
* — it never buffers a message, never rewrites a byte, and never throws on a
|
|
14
|
+
* hostile stream (an unparseable tail simply stops the count).
|
|
15
|
+
*
|
|
16
|
+
* Directions: `c2s` = client → origin (masked per RFC 6455), `s2c` = origin →
|
|
17
|
+
* client (unmasked). The mask bit is not enforced: this is an observer, not a
|
|
18
|
+
* validator.
|
|
19
|
+
*/
|
|
20
|
+
/** Frame budget: bounded CPU on a hostile or enormous stream. */
|
|
21
|
+
const MAX_OBSERVED_FRAMES = 10_000;
|
|
22
|
+
/** Per-direction buffer bound — a frame larger than this stops the count. */
|
|
23
|
+
const MAX_PENDING_BYTES = 1 << 20;
|
|
24
|
+
const OP_CONTINUATION = 0x0;
|
|
25
|
+
const OP_TEXT = 0x1;
|
|
26
|
+
const OP_BINARY = 0x2;
|
|
27
|
+
const OP_CLOSE = 0x8;
|
|
28
|
+
const OP_PING = 0x9;
|
|
29
|
+
const OP_PONG = 0xa;
|
|
30
|
+
function emptyState() {
|
|
31
|
+
return { buf: Buffer.alloc(0), frames: 0, messages: 0, text: 0, binary: 0, ping: 0, pong: 0, payloadBytes: 0, stopped: false };
|
|
32
|
+
}
|
|
33
|
+
/** Consume every COMPLETE frame in `st.buf`; leave the partial tail in place. */
|
|
34
|
+
function drain(st, budget) {
|
|
35
|
+
while (!st.stopped) {
|
|
36
|
+
const buf = st.buf;
|
|
37
|
+
if (buf.length < 2)
|
|
38
|
+
return;
|
|
39
|
+
const opcode = buf[0] & 0x0f;
|
|
40
|
+
const masked = (buf[1] & 0x80) !== 0;
|
|
41
|
+
let len = buf[1] & 0x7f;
|
|
42
|
+
let offset = 2;
|
|
43
|
+
if (len === 126) {
|
|
44
|
+
if (buf.length < 4)
|
|
45
|
+
return;
|
|
46
|
+
len = buf.readUInt16BE(2);
|
|
47
|
+
offset = 4;
|
|
48
|
+
}
|
|
49
|
+
else if (len === 127) {
|
|
50
|
+
if (buf.length < 10)
|
|
51
|
+
return;
|
|
52
|
+
const big = buf.readBigUInt64BE(2);
|
|
53
|
+
if (big > BigInt(MAX_PENDING_BYTES)) {
|
|
54
|
+
st.stopped = true;
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
len = Number(big);
|
|
58
|
+
offset = 10;
|
|
59
|
+
}
|
|
60
|
+
if (masked)
|
|
61
|
+
offset += 4;
|
|
62
|
+
if (buf.length < offset + len) {
|
|
63
|
+
// Partial frame: keep buffering, but never without bound.
|
|
64
|
+
if (st.buf.length > MAX_PENDING_BYTES)
|
|
65
|
+
st.stopped = true;
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (!budget()) {
|
|
69
|
+
st.stopped = true;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
st.frames += 1;
|
|
73
|
+
st.payloadBytes += len;
|
|
74
|
+
if (opcode === OP_TEXT) {
|
|
75
|
+
st.text += 1;
|
|
76
|
+
st.messages += 1;
|
|
77
|
+
}
|
|
78
|
+
else if (opcode === OP_BINARY) {
|
|
79
|
+
st.binary += 1;
|
|
80
|
+
st.messages += 1;
|
|
81
|
+
}
|
|
82
|
+
else if (opcode === OP_PING) {
|
|
83
|
+
st.ping += 1;
|
|
84
|
+
}
|
|
85
|
+
else if (opcode === OP_PONG) {
|
|
86
|
+
st.pong += 1;
|
|
87
|
+
}
|
|
88
|
+
else if (opcode === OP_CLOSE) {
|
|
89
|
+
const payload = buf.subarray(offset, offset + len);
|
|
90
|
+
st.close = {
|
|
91
|
+
...(payload.length >= 2 ? { code: payload.readUInt16BE(0) } : {}),
|
|
92
|
+
...(payload.length > 2 ? { reason: payload.subarray(2).toString('utf8') } : {}),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
else if (opcode !== OP_CONTINUATION) {
|
|
96
|
+
// Unknown opcode (reserved): keep the byte accounting, stop interpreting.
|
|
97
|
+
st.stopped = true;
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
st.buf = buf.subarray(offset + len);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
export function createWsObserver() {
|
|
104
|
+
const c2s = emptyState();
|
|
105
|
+
const s2c = emptyState();
|
|
106
|
+
let total = 0;
|
|
107
|
+
let truncated = false;
|
|
108
|
+
const budget = () => {
|
|
109
|
+
total += 1;
|
|
110
|
+
if (total > MAX_OBSERVED_FRAMES) {
|
|
111
|
+
truncated = true;
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
};
|
|
116
|
+
return {
|
|
117
|
+
push(dir, chunk) {
|
|
118
|
+
const st = dir === 'c2s' ? c2s : s2c;
|
|
119
|
+
if (st.stopped)
|
|
120
|
+
return;
|
|
121
|
+
st.buf = st.buf.length ? Buffer.concat([st.buf, chunk]) : chunk;
|
|
122
|
+
drain(st, budget);
|
|
123
|
+
},
|
|
124
|
+
observation() {
|
|
125
|
+
return {
|
|
126
|
+
frames: c2s.frames + s2c.frames,
|
|
127
|
+
messages: c2s.messages + s2c.messages,
|
|
128
|
+
text: c2s.text + s2c.text,
|
|
129
|
+
binary: c2s.binary + s2c.binary,
|
|
130
|
+
ping: c2s.ping + s2c.ping,
|
|
131
|
+
pong: c2s.pong + s2c.pong,
|
|
132
|
+
...(c2s.close || s2c.close ? { close: c2s.close ?? s2c.close } : {}),
|
|
133
|
+
payloadBytes: c2s.payloadBytes + s2c.payloadBytes,
|
|
134
|
+
truncated: truncated || c2s.stopped || s2c.stopped,
|
|
135
|
+
};
|
|
136
|
+
},
|
|
137
|
+
note() {
|
|
138
|
+
const o = this.observation();
|
|
139
|
+
if (o.frames === 0)
|
|
140
|
+
return 'ws: no frames';
|
|
141
|
+
const bits = [`ws: ${o.messages} msg (${o.text} text / ${o.binary} binary)`];
|
|
142
|
+
if (o.ping)
|
|
143
|
+
bits.push(`${o.ping} ping`);
|
|
144
|
+
if (o.pong)
|
|
145
|
+
bits.push(`${o.pong} pong`);
|
|
146
|
+
if (o.close)
|
|
147
|
+
bits.push(`close ${o.close.code ?? 'no code'}`);
|
|
148
|
+
if (o.truncated)
|
|
149
|
+
bits.push('truncated');
|
|
150
|
+
return bits.join(', ');
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { IrUpstreamError } from '../errors.js';
|
|
2
2
|
import { anthropicUsageFromWire } from './anthropic-messages.js';
|
|
3
|
+
import { upstreamToolCallId } from '../tool-identity.js';
|
|
3
4
|
function record(value) {
|
|
4
5
|
return value && typeof value === 'object' ? value : {};
|
|
5
6
|
}
|
|
@@ -59,7 +60,11 @@ export class AnthropicMessagesStreamDecoder {
|
|
|
59
60
|
events.push({ type: 'response.output_text.delta', index: itemIndex, partIndex: 0, text: part.text });
|
|
60
61
|
}
|
|
61
62
|
else if (block.type === 'tool_use') {
|
|
62
|
-
|
|
63
|
+
// The streaming leg names an unnamed call exactly the way the buffered leg does
|
|
64
|
+
// (`anthropicResponseToIR`), so a call the client must answer always carries something
|
|
65
|
+
// to answer with on both legs. An empty id here would survive to the outbound gate and
|
|
66
|
+
// the client's tool result could not name the call it belongs to.
|
|
67
|
+
const id = upstreamToolCallId(typeof block.id === 'string' ? block.id : undefined);
|
|
63
68
|
const item = { type: 'tool_call', id, callId: id, name: String(block.name ?? ''), arguments: '' };
|
|
64
69
|
state = { wireIndex, itemIndex, item, kind: 'tool' };
|
|
65
70
|
events.push({ type: 'response.output_item.added', index: itemIndex, item: { ...item, arguments: '' } });
|
|
@@ -2,6 +2,7 @@ import crypto from 'node:crypto';
|
|
|
2
2
|
import { EFFORT_LADDER, EFFORT_TO_BUDGET } from '../../types/router.js';
|
|
3
3
|
import { IrUnsupportedError } from '../errors.js';
|
|
4
4
|
import { DATA_URL_RE } from '../media.js';
|
|
5
|
+
import { upstreamToolCallId } from '../tool-identity.js';
|
|
5
6
|
import { assertCanonicalRequestSupported, assertCanonicalResponseSupported, assertCanonicalStreamEventSupported, } from '../validate.js';
|
|
6
7
|
const PROTOCOL = 'anthropic-messages';
|
|
7
8
|
const DEFAULT_MAX_TOKENS = 4096;
|
|
@@ -191,10 +192,11 @@ function itemsFromMessage(role, rawContent) {
|
|
|
191
192
|
const block = record(raw);
|
|
192
193
|
if (block.type === 'tool_use') {
|
|
193
194
|
flush();
|
|
195
|
+
const callId = upstreamToolCallId(block.id);
|
|
194
196
|
items.push({
|
|
195
197
|
type: 'tool_call',
|
|
196
|
-
id:
|
|
197
|
-
callId
|
|
198
|
+
id: callId,
|
|
199
|
+
callId,
|
|
198
200
|
name: String(block.name ?? ''),
|
|
199
201
|
arguments: JSON.stringify(block.input ?? {}),
|
|
200
202
|
...(cacheOf(block.cache_control) ? { cache: cacheOf(block.cache_control) } : {}),
|
|
@@ -484,8 +486,12 @@ export function canonicalToAnthropicPayload(ir, model) {
|
|
|
484
486
|
? ir.reasoning.budgetTokens
|
|
485
487
|
: undefined;
|
|
486
488
|
if ((effort && effort !== 'none') || explicitBudget !== undefined) {
|
|
487
|
-
// Anthropic enforces a 1024-token minimum for thinking.budget_tokens
|
|
488
|
-
|
|
489
|
+
// Anthropic enforces a 1024-token minimum for thinking.budget_tokens: a
|
|
490
|
+
// smaller value is rejected with HTTP 400. The floor applies to EVERY path
|
|
491
|
+
// — a catalog/`max_thinking_tokens` budget as well as the effort ladder —
|
|
492
|
+
// so an explicit value below 1024 is raised, not forwarded ({ type:'disabled' }
|
|
493
|
+
// is the only legitimate way to ask for "off").
|
|
494
|
+
const budget = Math.max(1024, explicitBudget ?? EFFORT_TO_BUDGET[effort]);
|
|
489
495
|
if (budget > 0) {
|
|
490
496
|
maxTokens = Math.max(maxTokens, budget + 1024);
|
|
491
497
|
thinking = { type: 'enabled', budget_tokens: budget };
|
|
@@ -567,8 +573,9 @@ export function anthropicResponseToIR(data) {
|
|
|
567
573
|
for (const raw of Array.isArray(body.content) ? body.content : []) {
|
|
568
574
|
const block = record(raw);
|
|
569
575
|
if (block.type === 'tool_use') {
|
|
576
|
+
const callId = upstreamToolCallId(block.id);
|
|
570
577
|
output.push({
|
|
571
|
-
type: 'tool_call', id:
|
|
578
|
+
type: 'tool_call', id: callId, callId,
|
|
572
579
|
name: String(block.name ?? ''), arguments: JSON.stringify(block.input ?? {}),
|
|
573
580
|
});
|
|
574
581
|
}
|