@bitkyc08/opencodex 2.97.0 → 2.98.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/bin/ocx.mjs +1 -1
- package/gui/dist/assets/App-6TFtZsUF.css +1 -0
- package/gui/dist/assets/App-FQ0OtiMJ.js +59 -0
- package/gui/dist/assets/Tray-D2d5wqLq.js +1 -0
- package/gui/dist/assets/WantedSansVariable.split.0-Dwvb1WRx.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.1-1wKD2RxI.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.10-CAKA6LIP.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.11-k5H85R4b.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.12-BHboLsUv.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.13-9GV377ix.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.14-DovDXnrj.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.15-DTdykCig.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.16-CKz2ccgK.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.17-Z2RD_KRZ.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.18-BcKtfonJ.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.19-DEQPOSZn.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.2-DryboyuA.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.20-Bia5JMls.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.21-Cr4jFw-E.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.22-DjjB69TZ.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.23-CvUzJskt.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.24-i_eMg5ox.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.25-2aKHwPX-.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.26-B6wXoCEL.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.27-D4h1WfGM.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.28-kvRPCStd.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.29-DHAiwleD.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.3-7H--eyZi.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.30-C66cfAY2.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.31-FDmtiNim.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.32-DouHt2Dd.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.33-B8HVvkJX.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.34-BtkRu1FN.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.35-DBylQbtk.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.36-Sg089Kgh.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.37-CYtvbriS.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.38-DKkSLf8Z.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.39-CecotKZD.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.4-Bv8O3ZMb.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.40-DdCvkmOU.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.41-ESn674Hc.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.42-DL9V7NNV.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.43-yipO0cyk.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.44-6rTmLZhm.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.45-BJAj8lrm.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.46-BDTzMn2c.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.47-Bxd4wGgi.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.48-DxnFBLnH.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.49-DnVD0aOD.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.5-B1wI2pp5.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.50-D9JlkZd6.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.51-D9Vw6Bu3.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.52-ClEJk4kn.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.53-BSXU0vh-.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.54-EcuxkR9v.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.55-CYiFhQX8.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.56--Gk9g8BK.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.57-DnkOPZcy.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.58-9MB8504z.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.59-ByxIsN72.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.6-Bmk0dRVw.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.60-Bxwx_aWQ.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.61-DA2z5sm7.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.62-C5iJPJZn.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.63-DAqrmaEc.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.64-BgXFsljb.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.65-DKiWujvu.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.66-8po4vUZ4.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.67-Cy5qyI9O.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.68-BKny7Fy3.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.69-B4VU4u_b.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.7-BuqwNpgA.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.70-BCd4n346.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.71-CkQppaWn.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.72-Db7vaoFs.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.73-DI80TYWl.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.74-tMCtDAHB.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.75-Ciw0D5WG.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.76-DoLjFgp-.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.77-CpT2uvMy.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.78-CkcJ4iZ1.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.79-DsOUJjEM.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.8-DsjypPd6.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.80-C0VwHTte.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.81-DTelu57i.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.82-BiMndGN1.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.83-CVcC6eDG.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.84-DlhZVt_O.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.85-S8I2D3pA.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.86-DozXXpH5.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.87-Dsn0Vuuo.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.88-iNwxxDCr.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.89-B63u_9CZ.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.9-Bi3IzpMI.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.90-ByeH1Pqf.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.91-UQLlagpm.woff2 +0 -0
- package/gui/dist/assets/brand-mark-A03mw_9y.js +1 -0
- package/gui/dist/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
- package/gui/dist/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
- package/gui/dist/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
- package/gui/dist/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
- package/gui/dist/assets/geist-mono-cyrillic-ext-wght-normal-X_5orZeX.woff2 +0 -0
- package/gui/dist/assets/geist-mono-cyrillic-wght-normal-DiZS0aHC.woff2 +0 -0
- package/gui/dist/assets/geist-mono-latin-ext-wght-normal-Bwz-egvJ.woff2 +0 -0
- package/gui/dist/assets/geist-mono-latin-wght-normal-XN7g48iV.woff2 +0 -0
- package/gui/dist/assets/geist-mono-symbols2-wght-normal-CO5SzqOn.woff2 +0 -0
- package/gui/dist/assets/geist-mono-vietnamese-wght-normal-DadHysG0.woff2 +0 -0
- package/gui/dist/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
- package/gui/dist/assets/index-5REODpG3.css +2 -0
- package/gui/dist/assets/index-BjTIN4to.js +94 -0
- package/gui/dist/assets/segment-thumb-features-Bf6vpwf8.js +1 -0
- package/gui/dist/classic/assets/App-C9Eg3nUo.js +58 -0
- package/gui/dist/{assets/Tray-CEw0y0jR.js → classic/assets/Tray-B8P8cokE.js} +1 -1
- package/gui/dist/classic/assets/index-DFAFnven.js +94 -0
- package/gui/dist/classic/assets/quota-summary-C-4HP4_5.js +1 -0
- package/gui/dist/classic/index.html +47 -0
- package/gui/dist/favicon.png +0 -0
- package/gui/dist/favicon.svg +14 -0
- package/gui/dist/index.html +15 -2
- package/package.json +1 -1
- package/src/adapters/anthropic.ts +17 -8
- package/src/adapters/google.ts +7 -7
- package/src/adapters/kiro/adapter.ts +18 -3
- package/src/adapters/kiro/conversation.ts +8 -8
- package/src/adapters/kiro/payload.ts +45 -12
- package/src/adapters/kiro/stream.ts +169 -12
- package/src/adapters/kiro/structured-output-json.ts +191 -0
- package/src/adapters/kiro/structured-output-schema.ts +542 -0
- package/src/adapters/kiro/structured-output.ts +367 -0
- package/src/adapters/kiro-constants.ts +9 -0
- package/src/adapters/kiro-tools.ts +14 -9
- package/src/adapters/kiro-wire.ts +9 -5
- package/src/adapters/openai-chat/messages.ts +58 -5
- package/src/adapters/openai-chat/mistral-image-limit.ts +50 -0
- package/src/adapters/openai-chat/passthrough.ts +5 -3
- package/src/adapters/openai-chat/tool-schema.ts +83 -13
- package/src/adapters/openai-responses/opengateway-input.ts +18 -6
- package/src/adapters/openai-responses/tool-output-recovery.ts +79 -0
- package/src/adapters/qoder/live-models.ts +115 -1
- package/src/claude/failure-verdict.ts +21 -0
- package/src/claude/inbound-content-options.ts +15 -2
- package/src/claude/inbound-model-options.ts +2 -1
- package/src/claude/inbound-records.ts +2 -0
- package/src/claude/inbound.ts +17 -11
- package/src/claude/outbound.ts +52 -23
- package/src/cli/capabilities-access-remote.ts +13 -0
- package/src/cli/capabilities-base.ts +1 -0
- package/src/cli/capabilities-provider-models.ts +3 -3
- package/src/cli/codex-shim-autorestore.ts +7 -0
- package/src/cli/config-command.ts +11 -2
- package/src/cli/connect.ts +17 -2
- package/src/cli/dispatch.ts +3 -1
- package/src/cli/doctor.ts +95 -7
- package/src/cli/index.ts +12 -7
- package/src/cli/link-apply-restart.ts +9 -2
- package/src/cli/models.ts +124 -38
- package/src/cli/registry.ts +1 -1
- package/src/cli/restart-failure.ts +17 -0
- package/src/cli/stop-approval.ts +2 -2
- package/src/cli/sync-command.ts +1 -1
- package/src/cli/sync-fanout.ts +15 -4
- package/src/cli/system-restart-client.ts +5 -0
- package/src/cli/uninstall-client-state.ts +12 -1
- package/src/cli/update-restart-child.ts +13 -3
- package/src/cli/update-restart-home.ts +6 -3
- package/src/cli/update-restart.ts +36 -19
- package/src/client/connect.ts +4 -1
- package/src/client/hub-client.ts +5 -0
- package/src/client/link-management-relay.ts +39 -2
- package/src/client/link-readiness.ts +3 -1
- package/src/client/machine-api.ts +2 -1
- package/src/client/machine-listener.ts +7 -0
- package/src/client/orphan-token-recovery.ts +322 -0
- package/src/codex/catalog/provider-models.ts +12 -4
- package/src/codex/native-profile-api.ts +27 -1
- package/src/codex/shim-diagnostics.ts +7 -0
- package/src/codex/shim-legacy-record.ts +290 -0
- package/src/codex/shim-windows-refresh.ts +9 -2
- package/src/codex/shim-wrapper-write.ts +7 -0
- package/src/codex/shim.ts +80 -21
- package/src/config/diagnostics.ts +9 -9
- package/src/config/load-degrade.ts +27 -27
- package/src/config/provider-validation.ts +9 -2
- package/src/config/removed-opencode-free-backup.ts +6 -3
- package/src/config/tracked-runtime-inspection.ts +3 -3
- package/src/generated/compatibility-version.json +219 -147
- package/src/images/loop.ts +2 -2
- package/src/lib/client-error-body.ts +37 -6
- package/src/lib/config-ownership-removal.ts +422 -0
- package/src/lib/config-ownership.ts +646 -81
- package/src/lib/context-overflow-verdict.ts +3 -3
- package/src/lib/crash-guard.ts +4 -2
- package/src/lib/desktop-cli-handoff.d.mts +3 -4
- package/src/lib/desktop-cli-handoff.mjs +7 -16
- package/src/lib/errors.ts +1 -0
- package/src/lib/local-management-capability.ts +3 -1
- package/src/lib/messages-error-body.ts +162 -0
- package/src/lib/messages-sse-error-presentation.ts +93 -0
- package/src/lib/passive-read.ts +9 -4
- package/src/lib/socks5-fetch.ts +21 -9
- package/src/lib/upstream-retry.ts +81 -1
- package/src/lib/windows-owner-acl.ts +9 -3
- package/src/lib/winsw.ts +5 -5
- package/src/lib/workflow-budget.ts +3 -1
- package/src/oauth/index.ts +7 -0
- package/src/oauth/store.ts +20 -10
- package/src/protocols/codecs/messages-ir.ts +15 -9
- package/src/protocols/encoders/adapter-events.ts +26 -7
- package/src/protocols/encoders/messages.ts +72 -28
- package/src/providers/antigravity-models.ts +45 -12
- package/src/providers/free-directory.ts +2 -2
- package/src/providers/reasoning-metadata.ts +2 -1
- package/src/providers/registry/entries-extended.ts +9 -0
- package/src/reasoning-effort.ts +7 -2
- package/src/remote/hub-usage.ts +3 -0
- package/src/server/admission-model-scope.ts +1 -1
- package/src/server/admitted-http-turn.ts +2 -1
- package/src/server/anthropic-rate-limit-headers.ts +75 -3
- package/src/server/claude-messages.ts +130 -67
- package/src/server/gui-static.ts +4 -1
- package/src/server/hub-usage.ts +7 -2
- package/src/server/index/link-management.ts +8 -0
- package/src/server/index/ollama-routes.ts +2 -0
- package/src/server/index/serve-options.ts +23 -20
- package/src/server/index/startup-warnings.ts +5 -12
- package/src/server/index/websocket-handler.ts +1 -0
- package/src/server/inference/client-encoder-delivery.ts +5 -1
- package/src/server/management/logs-usage-routes.ts +89 -4
- package/src/server/management/provider-routes.ts +2 -22
- package/src/server/management/qoder-provider-probe.ts +105 -0
- package/src/server/management/route-registry.ts +3 -0
- package/src/server/management/usage-aggregate-cache.ts +15 -5
- package/src/server/management/usage-source-facets.ts +38 -0
- package/src/server/messages-native-tool-guard.ts +11 -3
- package/src/server/messages-native.ts +111 -40
- package/src/server/messages-response-headers.ts +25 -11
- package/src/server/messages-shared.ts +94 -64
- package/src/server/messages-sse-error-filter.ts +199 -0
- package/src/server/request-log.ts +12 -0
- package/src/server/responses/adapter-continuation.ts +11 -5
- package/src/server/responses/adapter-delivery.ts +14 -8
- package/src/server/responses/adapter-dispatch.ts +8 -8
- package/src/server/responses/anthropic-thinking-recovery.ts +2 -2
- package/src/server/responses/codex-auth-error.ts +8 -4
- package/src/server/responses/codex-ws-wire.ts +19 -3
- package/src/server/responses/compact.ts +4 -1
- package/src/server/responses/core-auth.ts +1 -1
- package/src/server/responses/core-combo-failure.ts +2 -0
- package/src/server/responses/core-combo.ts +2 -0
- package/src/server/responses/core-errors.ts +5 -4
- package/src/server/responses/core-options.ts +2 -0
- package/src/server/responses/empty-completion-guard.ts +4 -1
- package/src/server/responses/pacing-overload.ts +1 -1
- package/src/server/responses/passthrough-dispatch.ts +5 -5
- package/src/server/responses/request-prepare.ts +3 -1
- package/src/server/responses/request-transport.ts +9 -8
- package/src/server/responses/response-effects.ts +3 -2
- package/src/server/responses/terminal-guard.ts +30 -0
- package/src/server/restart-replacement.ts +3 -1
- package/src/server/sse-payload-rewrite.ts +23 -11
- package/src/server/workflow-refusal.ts +1 -1
- package/src/server/ws-bridge.ts +4 -0
- package/src/service/definition.ts +36 -4
- package/src/service/launchd.ts +89 -32
- package/src/service/logging.ts +34 -7
- package/src/service/plist-xml.ts +59 -0
- package/src/service/state.ts +8 -0
- package/src/service/systemd-env.ts +2 -0
- package/src/service/systemd.ts +31 -7
- package/src/service/windows-taskxml.ts +16 -7
- package/src/service-manager-probe.ts +4 -10
- package/src/types/provider.ts +2 -0
- package/src/types/request.ts +13 -1
- package/src/update/config-preflight.ts +48 -0
- package/src/update/index.ts +15 -1
- package/src/usage/client.ts +43 -0
- package/src/usage/error-detail.ts +1 -1
- package/src/usage/log.ts +4 -0
- package/src/usage/source-facets.ts +51 -0
- package/src/usage/source-filter.ts +65 -0
- package/src/usage/summary.ts +50 -8
- package/src/web-search/anthropic-executor.ts +105 -24
- package/src/web-search/executor.ts +5 -3
- package/src/web-search/loop.ts +2 -1
- package/src/web-search/progress-stream.ts +64 -5
- package/gui/dist/assets/App-DuNhggE1.js +0 -58
- package/gui/dist/assets/index-CynE_Rue.js +0 -94
- package/gui/dist/assets/quota-summary-VHFSlUuM.js +0 -1
- /package/gui/dist/{assets → classic/assets}/App-B4j-Txmb.css +0 -0
- /package/gui/dist/{assets → classic/assets}/index-CHWwDo9L.css +0 -0
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import type { MessagesSseErrorFilter } from "./messages-sse-error-filter";
|
|
2
|
+
import { MessagesSseFrameContext, messagesSseLocalFrame, type MessagesSseLocalOrigin } from "../lib/messages-sse-error-presentation";
|
|
3
|
+
import { markProxyError } from "../lib/client-error-body";
|
|
1
4
|
/**
|
|
2
5
|
* Accounting and measurement pieces shared by the two Claude Messages lanes.
|
|
3
6
|
*
|
|
@@ -110,6 +113,8 @@ export function tapAnthropicSseForLog(
|
|
|
110
113
|
logCtx: RequestLogContext,
|
|
111
114
|
finalize: (status: number, meta: PassthroughFinalizeMeta) => void,
|
|
112
115
|
guard?: PassthroughBodyGuard,
|
|
116
|
+
frames?: MessagesSseFrameContext,
|
|
117
|
+
clientFilter?: MessagesSseErrorFilter,
|
|
113
118
|
): ReadableStream<Uint8Array> {
|
|
114
119
|
const decoder = new TextDecoder();
|
|
115
120
|
const encoder = new TextEncoder();
|
|
@@ -236,16 +241,32 @@ export function tapAnthropicSseForLog(
|
|
|
236
241
|
let bodyBytes = 0;
|
|
237
242
|
let tapController: ReadableStreamDefaultController<Uint8Array> | undefined;
|
|
238
243
|
|
|
244
|
+
let deliveryFailed = false;
|
|
245
|
+
const enqueue = (bytes: Uint8Array): number => {
|
|
246
|
+
let count = 0;
|
|
247
|
+
try {
|
|
248
|
+
if (clientFilter) clientFilter.push(bytes, value => { tapController!.enqueue(value); count++; });
|
|
249
|
+
else { tapController!.enqueue(bytes); count++; }
|
|
250
|
+
return count;
|
|
251
|
+
} catch (error) {
|
|
252
|
+
deliveryFailed = clientFilter !== undefined;
|
|
253
|
+
clientFilter?.dispose();
|
|
254
|
+
throw error;
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
const close = () => { clientFilter?.finish(value => tapController!.enqueue(value)); tapController!.close(); };
|
|
239
258
|
const recordUsage = () => {
|
|
240
259
|
logCtx.usage = anthropicUsageToOcx(Object.keys(usageAcc).length > 0 ? usageAcc : undefined);
|
|
241
260
|
};
|
|
242
|
-
const closeWithErrorFrame = (errType: string, message: string) => {
|
|
261
|
+
const closeWithErrorFrame = (errType: string, message: string, origin: MessagesSseLocalOrigin) => {
|
|
243
262
|
const payload = JSON.stringify({ type: "error", error: { type: errType, message } });
|
|
244
263
|
try {
|
|
245
264
|
// Leading blank line terminates any partial SSE block so the frame parses cleanly
|
|
246
265
|
// (relaySseWithFailedTail policy, Anthropic wire shape).
|
|
247
|
-
|
|
248
|
-
|
|
266
|
+
const bytes = encoder.encode(`\n\nevent: error\ndata: ${payload}\n\n`);
|
|
267
|
+
frames?.register(bytes, { client: messagesSseLocalFrame(origin, message) });
|
|
268
|
+
enqueue(bytes);
|
|
269
|
+
close();
|
|
249
270
|
} catch { /* client already torn down */ }
|
|
250
271
|
};
|
|
251
272
|
const failBody = (closeReason: "body_stall" | "body_overflow", errType: string, message: string) => {
|
|
@@ -261,8 +282,8 @@ export function tapAnthropicSseForLog(
|
|
|
261
282
|
// including the restored blank line for a terminal found only in the tail.
|
|
262
283
|
finalize(200, { closeReason: "terminal" });
|
|
263
284
|
try {
|
|
264
|
-
if (terminalInTail)
|
|
265
|
-
|
|
285
|
+
if (terminalInTail) enqueue(encoder.encode("\n\n"));
|
|
286
|
+
close();
|
|
266
287
|
} catch { /* client already torn down */ }
|
|
267
288
|
} else {
|
|
268
289
|
// A cut-short turn, logged as the Responses relay logs a stall-timeout incomplete
|
|
@@ -271,7 +292,7 @@ export function tapAnthropicSseForLog(
|
|
|
271
292
|
recordLocalFailureDetail(logCtx, message, closeReason === "body_stall"
|
|
272
293
|
? { code: "body_stall", value: guard?.stallMs ?? 0 } : { code: "body_overflow", value: guard?.maxBytes ?? 0 });
|
|
273
294
|
finalize(502, { terminalStatus: "incomplete", closeReason });
|
|
274
|
-
closeWithErrorFrame(errType, message);
|
|
295
|
+
closeWithErrorFrame(errType, message, closeReason === "body_stall" ? "stall" : "cap");
|
|
275
296
|
}
|
|
276
297
|
reader.cancel(new DOMException(message, closeReason === "body_stall" ? "TimeoutError" : "QuotaExceededError")).catch(() => {});
|
|
277
298
|
};
|
|
@@ -292,6 +313,7 @@ export function tapAnthropicSseForLog(
|
|
|
292
313
|
idle.cancel();
|
|
293
314
|
detachAbort();
|
|
294
315
|
finalize(499, { closeReason: "client_cancel" });
|
|
316
|
+
clientFilter?.dispose();
|
|
295
317
|
try { tapController?.close(); } catch { /* downstream already torn down */ }
|
|
296
318
|
reader.cancel(guard?.reqSignal?.reason).catch(() => {});
|
|
297
319
|
};
|
|
@@ -313,64 +335,66 @@ export function tapAnthropicSseForLog(
|
|
|
313
335
|
async pull(controller) {
|
|
314
336
|
if (settled) return;
|
|
315
337
|
try {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
338
|
+
for (;;) {
|
|
339
|
+
idle.reset();
|
|
340
|
+
const { done, value } = await reader.read();
|
|
341
|
+
idle.pause();
|
|
342
|
+
if (settled) return; // stall/overflow/abort won the race while we awaited
|
|
343
|
+
if (done) {
|
|
344
|
+
// Bun can resolve a cancelled read as done before the abort listener runs: a client
|
|
345
|
+
// that left is a cancel, not a cut upstream.
|
|
346
|
+
if (guard?.reqSignal?.aborted) {
|
|
347
|
+
onClientAbort();
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
settled = true;
|
|
351
|
+
idle.cancel();
|
|
352
|
+
detachAbort();
|
|
353
|
+
const { terminalInTail, tailClean } = flushTail();
|
|
354
|
+
recordUsage();
|
|
355
|
+
// A clean EOF ends the turn only when the client already has its terminal, or a
|
|
356
|
+
// complete non-error stop_reason with nothing cut after it (compatible providers may
|
|
357
|
+
// omit message_stop; the translated adapter accepts the same shape). The opt-in
|
|
358
|
+
// anthropicEofTolerance profile keeps a content-only EOF as it always was.
|
|
359
|
+
const failure = terminalSeen
|
|
360
|
+
? null
|
|
361
|
+
: lastDeliveredStop === "error"
|
|
362
|
+
? 'upstream ended the turn with stop_reason "error"'
|
|
363
|
+
: !tailClean
|
|
364
|
+
? "anthropic passthrough upstream stream ended before message_stop"
|
|
365
|
+
: lastDeliveredStop !== null || guard?.eofTolerant === true
|
|
366
|
+
? null
|
|
367
|
+
: "anthropic passthrough upstream stream ended before message_stop";
|
|
368
|
+
if (failure === null) {
|
|
369
|
+
finalize(200, { closeReason: "terminal" });
|
|
370
|
+
if (terminalInTail) enqueue(encoder.encode("\n\n"));
|
|
371
|
+
close();
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
// A body that stopped short is a failed turn, logged and surfaced like a mid-stream
|
|
375
|
+
// reset (below), so the client never reads a partial answer as a finished message.
|
|
376
|
+
logCtx.transportPhase = "mid_stream";
|
|
377
|
+
logCtx.terminalSource = "synthetic";
|
|
378
|
+
recordLocalFailureDetail(logCtx, failure, { code: lastDeliveredStop === "error" ? "stream_error_stop" : "stream_ended_early" });
|
|
379
|
+
if (logCtx.activeAttempt) logCtx.activeAttempt.streamAborted = true;
|
|
380
|
+
finalize(502, { terminalStatus: "failed", closeReason: "terminal" });
|
|
381
|
+
closeWithErrorFrame("api_error", failure, lastDeliveredStop === "error" ? "stop-error" : "eof");
|
|
349
382
|
return;
|
|
350
383
|
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
if (value.byteLength > 0) {
|
|
362
|
-
bodyBytes += value.byteLength;
|
|
363
|
-
if (guard && guard.maxBytes > 0 && bodyBytes > guard.maxBytes) {
|
|
364
|
-
failBody(
|
|
365
|
-
"body_overflow",
|
|
366
|
-
"api_error",
|
|
367
|
-
`anthropic passthrough body exceeded ${guard.maxBytes} bytes`,
|
|
368
|
-
);
|
|
369
|
-
return;
|
|
384
|
+
if (value.byteLength > 0) {
|
|
385
|
+
bodyBytes += value.byteLength;
|
|
386
|
+
if (guard && guard.maxBytes > 0 && bodyBytes > guard.maxBytes) {
|
|
387
|
+
failBody(
|
|
388
|
+
"body_overflow",
|
|
389
|
+
"api_error",
|
|
390
|
+
`anthropic passthrough body exceeded ${guard.maxBytes} bytes`,
|
|
391
|
+
);
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
370
394
|
}
|
|
395
|
+
inspect(value);
|
|
396
|
+
if (enqueue(value) > 0) return;
|
|
371
397
|
}
|
|
372
|
-
inspect(value);
|
|
373
|
-
controller.enqueue(value);
|
|
374
398
|
} catch (err) {
|
|
375
399
|
if (settled) return;
|
|
376
400
|
// Bun can settle a fetch body read before it dispatches the abort listeners
|
|
@@ -389,6 +413,7 @@ export function tapAnthropicSseForLog(
|
|
|
389
413
|
// A local cap, not an upstream failure: the non-streaming native Messages fold
|
|
390
414
|
// maps this error to a 413 itself, so it still errors the stream.
|
|
391
415
|
finalize(200, { closeReason: "terminal" });
|
|
416
|
+
clientFilter?.dispose();
|
|
392
417
|
try { controller.error(err); } catch { /* torn down */ }
|
|
393
418
|
return;
|
|
394
419
|
}
|
|
@@ -399,8 +424,11 @@ export function tapAnthropicSseForLog(
|
|
|
399
424
|
try {
|
|
400
425
|
// An SSE parser drops an event that EOF cuts off before its blank line, so restore
|
|
401
426
|
// the delimiter when the terminal was only found in that unterminated tail.
|
|
402
|
-
if (
|
|
403
|
-
|
|
427
|
+
if (deliveryFailed) controller.error(err);
|
|
428
|
+
else {
|
|
429
|
+
if (terminalInTail) enqueue(encoder.encode("\n\n"));
|
|
430
|
+
close();
|
|
431
|
+
}
|
|
404
432
|
} catch { /* torn down */ }
|
|
405
433
|
reader.cancel(err).catch(() => {});
|
|
406
434
|
return;
|
|
@@ -418,11 +446,13 @@ export function tapAnthropicSseForLog(
|
|
|
418
446
|
logCtx.upstreamError = message.slice(0, 500);
|
|
419
447
|
if (logCtx.activeAttempt) logCtx.activeAttempt.streamAborted = true;
|
|
420
448
|
finalize(502, { terminalStatus: "failed", closeReason: "terminal" });
|
|
421
|
-
|
|
449
|
+
if (deliveryFailed) { clientFilter?.dispose(); try { controller.error(err); } catch { /* torn down */ } }
|
|
450
|
+
else closeWithErrorFrame("api_error", message, "read");
|
|
422
451
|
reader.cancel(err).catch(() => {});
|
|
423
452
|
}
|
|
424
453
|
},
|
|
425
454
|
cancel(reason) {
|
|
455
|
+
clientFilter?.dispose();
|
|
426
456
|
if (!settled) {
|
|
427
457
|
settled = true;
|
|
428
458
|
idle.cancel();
|
|
@@ -467,7 +497,7 @@ export function sanitizePassthroughToolCallIds(messages: unknown[]): void {
|
|
|
467
497
|
const field = fieldOf(block);
|
|
468
498
|
if (!field || typeof block[field] !== "string") continue;
|
|
469
499
|
const wire = callIds.allocate(block[field] as string);
|
|
470
|
-
if (wire === undefined) throw new AnthropicRequestError(
|
|
500
|
+
if (wire === undefined) throw markProxyError(new AnthropicRequestError(field === "id" ? "tool_use block has an empty id" : "tool_result block has an empty tool_use_id"));
|
|
471
501
|
block[field] = wire;
|
|
472
502
|
}
|
|
473
503
|
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MESSAGES_SSE_ERROR_HOLD_BYTES, MessagesSseFrameContext, messagesSseErrorPayload, messagesSseOversizePayload,
|
|
3
|
+
} from "../lib/messages-sse-error-presentation";
|
|
4
|
+
import { sseFieldValue } from "../lib/sse-decoder";
|
|
5
|
+
|
|
6
|
+
const encoder = new TextEncoder();
|
|
7
|
+
type Newline = "\n" | "\r" | "\r\n";
|
|
8
|
+
/** Final errors emit only data fields after the already-public, closed error declaration. */
|
|
9
|
+
function ownedSuffix(bytes: Uint8Array, space: boolean, newline: Newline): Uint8Array {
|
|
10
|
+
const text = new TextDecoder().decode(bytes);
|
|
11
|
+
const lines = [...text.matchAll(/([^\r\n]*)(\r\n|\r|\n|$)/g)].filter(match => match[0] !== "");
|
|
12
|
+
const data = lines.map(line => sseFieldValue(line[1]!, "data")).filter(value => value !== null).join("\n");
|
|
13
|
+
// An empty named error still dispatches the SDK's generic APIError; add no parseable data.
|
|
14
|
+
if (data === "") return encoder.encode(lines.filter(line => sseFieldValue(line[1]!, "data") !== null).map(line => line[0]).join(""));
|
|
15
|
+
const payload = messagesSseErrorPayload(data);
|
|
16
|
+
let replaced = false;
|
|
17
|
+
return encoder.encode(lines.map(line => {
|
|
18
|
+
if (sseFieldValue(line[1]!, "data") === null) return "";
|
|
19
|
+
if (replaced) return "";
|
|
20
|
+
replaced = true;
|
|
21
|
+
return `data:${space ? " " : ""}${payload}${line[2] ? newline : ""}`;
|
|
22
|
+
}).join(""));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface MessagesSseErrorFilter {
|
|
26
|
+
push(value: Uint8Array, emit: (bytes: Uint8Array) => void): void;
|
|
27
|
+
finish(emit: (bytes: Uint8Array) => void): void;
|
|
28
|
+
dispose(): void;
|
|
29
|
+
}
|
|
30
|
+
/** Final client-only delivery on the existing enqueue turn; no new queue, pump or admission. */
|
|
31
|
+
export function createMessagesSseErrorFilter(context = new MessagesSseFrameContext()): MessagesSseErrorFilter {
|
|
32
|
+
let disposed = false;
|
|
33
|
+
let hold: Uint8Array | undefined, held = 0, oversize = false;
|
|
34
|
+
let eligibility: "unseen" | "first" | "pass" | "error" = "unseen";
|
|
35
|
+
let lastError = false, headerSpace = true, newline: Newline = "\n";
|
|
36
|
+
let lineLength = 0, comment = false, namePosition = 0, nameMatches = true;
|
|
37
|
+
let inValue = false, eventField = false, optionalSpace = true, valuePosition = 0, valueMatches = true, fieldSpace = false;
|
|
38
|
+
let skipLf = false, headerCr = false, suppressLf = false;
|
|
39
|
+
let bomPosition = 0, atStreamStart = true;
|
|
40
|
+
const clearHold = () => { hold = undefined; held = 0; oversize = false; };
|
|
41
|
+
const resetLine = () => {
|
|
42
|
+
lineLength = 0; comment = false; namePosition = 0; nameMatches = true;
|
|
43
|
+
inValue = false; eventField = false; optionalSpace = true; valuePosition = 0; valueMatches = true; fieldSpace = false;
|
|
44
|
+
};
|
|
45
|
+
const resetBlock = () => { eligibility = "unseen"; lastError = false; clearHold(); };
|
|
46
|
+
const retain = (byte: number) => {
|
|
47
|
+
if (oversize) return;
|
|
48
|
+
if (held === MESSAGES_SSE_ERROR_HOLD_BYTES) { hold = undefined; held = 0; oversize = true; return; }
|
|
49
|
+
hold ??= new Uint8Array(MESSAGES_SSE_ERROR_HOLD_BYTES);
|
|
50
|
+
hold[held++] = byte;
|
|
51
|
+
};
|
|
52
|
+
const fieldByte = (byte: number) => {
|
|
53
|
+
if (lineLength++ === 0) {
|
|
54
|
+
comment = byte === 58;
|
|
55
|
+
if (!comment && eligibility === "unseen") eligibility = "first";
|
|
56
|
+
}
|
|
57
|
+
if (comment) return;
|
|
58
|
+
if (!inValue) {
|
|
59
|
+
if (byte === 58) { inValue = true; eventField = nameMatches && namePosition === 5; }
|
|
60
|
+
else { nameMatches &&= byte === "event".charCodeAt(namePosition); namePosition = Math.min(6, namePosition + 1); }
|
|
61
|
+
} else {
|
|
62
|
+
if (optionalSpace) { optionalSpace = false; if (byte === 32) { fieldSpace = true; return; } }
|
|
63
|
+
valueMatches &&= byte === "error".charCodeAt(valuePosition);
|
|
64
|
+
valuePosition = Math.min(6, valuePosition + 1);
|
|
65
|
+
}
|
|
66
|
+
// Mismatching first fields become permanently ordinary immediately.
|
|
67
|
+
if (eligibility === "first" && (!nameMatches || inValue && (!eventField || !valueMatches))) eligibility = "pass";
|
|
68
|
+
};
|
|
69
|
+
const finishField = (ending: number) => {
|
|
70
|
+
if (comment) return;
|
|
71
|
+
const error = eventField && valueMatches && valuePosition === 5;
|
|
72
|
+
if (eligibility === "first") {
|
|
73
|
+
eligibility = error ? "error" : "pass";
|
|
74
|
+
if (error) {
|
|
75
|
+
lastError = true; headerSpace = fieldSpace; newline = ending === 13 ? "\r" : "\n";
|
|
76
|
+
headerCr = ending === 13;
|
|
77
|
+
}
|
|
78
|
+
} else if (eligibility === "error" && (eventField || !inValue && nameMatches && namePosition === 5)) lastError = error;
|
|
79
|
+
};
|
|
80
|
+
const finishError = (emit: (bytes: Uint8Array) => void, delimiter: boolean, ending?: number): boolean => {
|
|
81
|
+
const forced = oversize;
|
|
82
|
+
if (forced) {
|
|
83
|
+
emit(encoder.encode(`data:${headerSpace ? " " : ""}${messagesSseOversizePayload()}${delimiter ? newline + newline : ""}`));
|
|
84
|
+
} else {
|
|
85
|
+
const suffix = hold?.subarray(0, held) ?? new Uint8Array();
|
|
86
|
+
emit(lastError ? ownedSuffix(suffix, headerSpace, newline) : suffix);
|
|
87
|
+
if (delimiter) emit(lastError ? encoder.encode(newline) : new Uint8Array([ending!]));
|
|
88
|
+
}
|
|
89
|
+
const suppressContinuation = delimiter && (forced || lastError);
|
|
90
|
+
resetBlock();
|
|
91
|
+
return suppressContinuation;
|
|
92
|
+
};
|
|
93
|
+
const scan = (value: Uint8Array, emit: (bytes: Uint8Array) => void) => {
|
|
94
|
+
let start = 0;
|
|
95
|
+
const forward = (end: number) => { if (end > start) emit(start === 0 && end === value.length ? value : value.subarray(start, end)); start = end; };
|
|
96
|
+
for (let i = 0; i < value.length; i++) {
|
|
97
|
+
const byte = value[i]!;
|
|
98
|
+
if (atStreamStart) {
|
|
99
|
+
const bom = [239, 187, 191];
|
|
100
|
+
if (byte === bom[bomPosition]) { bomPosition++; if (bomPosition === 3) atStreamStart = false; continue; }
|
|
101
|
+
atStreamStart = false;
|
|
102
|
+
if (bomPosition > 0) eligibility = "pass";
|
|
103
|
+
}
|
|
104
|
+
if (skipLf) {
|
|
105
|
+
skipLf = false;
|
|
106
|
+
if (byte === 10) {
|
|
107
|
+
const declarationLf = headerCr;
|
|
108
|
+
if (headerCr) { newline = "\r\n"; headerCr = false; }
|
|
109
|
+
if (suppressLf) { forward(i); start = i + 1; suppressLf = false; }
|
|
110
|
+
else if (eligibility === "error" && !declarationLf) {
|
|
111
|
+
// The first declaration's optional LF is already public; suffix CRLF belongs to the hold.
|
|
112
|
+
forward(i); retain(byte); start = i + 1;
|
|
113
|
+
}
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
headerCr = false; suppressLf = false;
|
|
117
|
+
}
|
|
118
|
+
const holding = eligibility === "error";
|
|
119
|
+
if (byte === 10 || byte === 13) {
|
|
120
|
+
skipLf = byte === 13;
|
|
121
|
+
if (lineLength === 0) {
|
|
122
|
+
if (holding) {
|
|
123
|
+
forward(i); suppressLf = finishError(emit, true, byte); start = i + 1;
|
|
124
|
+
} else resetBlock();
|
|
125
|
+
} else {
|
|
126
|
+
if (holding) { forward(i); retain(byte); start = i + 1; }
|
|
127
|
+
finishField(byte);
|
|
128
|
+
// A just-confirmed declaration remains part of this original public prefix.
|
|
129
|
+
if (!holding && eligibility === "error") forward(i + 1);
|
|
130
|
+
}
|
|
131
|
+
resetLine();
|
|
132
|
+
} else {
|
|
133
|
+
if (holding) { forward(i); retain(byte); start = i + 1; }
|
|
134
|
+
fieldByte(byte);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
forward(value.length);
|
|
138
|
+
};
|
|
139
|
+
return {
|
|
140
|
+
push(value, deliver) {
|
|
141
|
+
if (disposed) return;
|
|
142
|
+
if (value.length === 0 && eligibility !== "error") { deliver(value); return; }
|
|
143
|
+
const emit = (bytes: Uint8Array) => { if (bytes.length) deliver(bytes); };
|
|
144
|
+
try {
|
|
145
|
+
const origin = context.take(value);
|
|
146
|
+
if (origin && "client" in origin) {
|
|
147
|
+
const prefix = value[0] === 10 && value[1] === 10 ? 2 : 0;
|
|
148
|
+
const ordinary = eligibility !== "error";
|
|
149
|
+
if (prefix) scan(value.subarray(0, prefix), ordinary ? () => {} : emit);
|
|
150
|
+
emit(ordinary ? origin.client : origin.client.subarray(prefix));
|
|
151
|
+
resetBlock(); resetLine(); skipLf = false;
|
|
152
|
+
} else if (origin?.echoRenamedError) {
|
|
153
|
+
const header = encoder.encode("event: message_start\n");
|
|
154
|
+
if (eligibility !== "unseen" || lineLength || !header.every((byte, index) => value[index] === byte)
|
|
155
|
+
|| value.at(-1) !== 10 || value.at(-2) !== 10) throw new Error("The proxy could not reach or read the provider.");
|
|
156
|
+
emit(value.subarray(0, header.length));
|
|
157
|
+
const suffix = value.subarray(header.length, value.length - 1);
|
|
158
|
+
emit(suffix.length > MESSAGES_SSE_ERROR_HOLD_BYTES ? encoder.encode(`data: ${messagesSseOversizePayload()}\n`) : ownedSuffix(suffix, true, "\n"));
|
|
159
|
+
emit(encoder.encode("\n"));
|
|
160
|
+
resetBlock(); resetLine(); skipLf = false;
|
|
161
|
+
} else scan(value, emit);
|
|
162
|
+
} catch (error) { clearHold(); disposed = true; throw error; }
|
|
163
|
+
},
|
|
164
|
+
finish(emit) {
|
|
165
|
+
if (disposed) return;
|
|
166
|
+
try {
|
|
167
|
+
if (eligibility === "error") {
|
|
168
|
+
if (lineLength) finishField(10);
|
|
169
|
+
finishError(bytes => { if (bytes.length) emit(bytes); }, false);
|
|
170
|
+
}
|
|
171
|
+
} finally { clearHold(); disposed = true; }
|
|
172
|
+
},
|
|
173
|
+
dispose() { disposed = true; resetBlock(); resetLine(); skipLf = false; headerCr = false; suppressLf = false; },
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Standalone reader adapter for callers without an existing final enqueue stage. */
|
|
178
|
+
export function filterMessagesSseErrors(source: ReadableStream<Uint8Array>, context?: MessagesSseFrameContext): ReadableStream<Uint8Array> {
|
|
179
|
+
const reader = source.getReader(), filter = createMessagesSseErrorFilter(context);
|
|
180
|
+
let cancelled = false;
|
|
181
|
+
return new ReadableStream<Uint8Array>({
|
|
182
|
+
async pull(controller) {
|
|
183
|
+
let emitted = false;
|
|
184
|
+
const emit = (bytes: Uint8Array) => { controller.enqueue(bytes); emitted = true; };
|
|
185
|
+
try {
|
|
186
|
+
do {
|
|
187
|
+
const { done, value } = await reader.read();
|
|
188
|
+
if (cancelled) return;
|
|
189
|
+
if (done) { filter.finish(emit); controller.close(); return; }
|
|
190
|
+
filter.push(value, emit);
|
|
191
|
+
} while (!emitted);
|
|
192
|
+
} catch (error) {
|
|
193
|
+
filter.dispose();
|
|
194
|
+
if (!cancelled) { cancelled = true; void reader.cancel(error).catch(() => {}); controller.error(error); }
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
cancel(reason) { cancelled = true; filter.dispose(); return reader.cancel(reason); },
|
|
198
|
+
}, { highWaterMark: 0 });
|
|
199
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from "node:fs";
|
|
2
2
|
import { randomBytes } from "node:crypto";
|
|
3
|
+
import { normalizeRecordedClient, type UsageClient } from "../usage/client";
|
|
3
4
|
import { stampApiKeyAccountLabel, usesApiKeyAccount } from "../providers/label";
|
|
4
5
|
import { KEY_ACCOUNT_LOG_LABEL_RE } from "../codex/account-label";
|
|
5
6
|
import { attemptAccountChanged, sealRequestAttemptIdentity } from "./request-log-account-rotation";
|
|
@@ -119,6 +120,7 @@ export function cacheDiagnosticsObserving(): boolean {
|
|
|
119
120
|
export interface RequestLogContext extends UpstreamErrorDiagnostics {
|
|
120
121
|
model: string;
|
|
121
122
|
provider: string;
|
|
123
|
+
client?: UsageClient;
|
|
122
124
|
/** Optional process-lifetime aggregate sink, injected by the server composition owner. */
|
|
123
125
|
requestMetricsRecorder?: RequestMetricsRecorder;
|
|
124
126
|
/** Bounded terminal enum observed while inspecting a buffered response body. */
|
|
@@ -302,6 +304,7 @@ export function rebindCacheDiagnosticBody(
|
|
|
302
304
|
|
|
303
305
|
export interface RequestLogEntry extends UpstreamErrorDiagnostics {
|
|
304
306
|
requestId: string;
|
|
307
|
+
client?: UsageClient;
|
|
305
308
|
/** The logical request this row belongs to (#4546); absent on rows written without a budget. */
|
|
306
309
|
logicalRequestId?: string;
|
|
307
310
|
timestamp: number;
|
|
@@ -490,6 +493,7 @@ export function requestLogEntryFromPersistedUsage(entry: PersistedUsageEntry): R
|
|
|
490
493
|
timestamp: entry.timestamp,
|
|
491
494
|
model: entry.model,
|
|
492
495
|
provider: entry.provider,
|
|
496
|
+
...(Object.hasOwn(entry, "client") ? { client: normalizeRecordedClient(entry.client) ?? "unknown" } : {}),
|
|
493
497
|
...(entry.firstOutputMs !== undefined ? { firstOutputMs: entry.firstOutputMs } : {}),
|
|
494
498
|
...(entry.genStartMs !== undefined ? { genStartMs: entry.genStartMs } : {}),
|
|
495
499
|
...(entry.lastOutputMs !== undefined ? { lastOutputMs: entry.lastOutputMs } : {}),
|
|
@@ -626,7 +630,13 @@ export function rehydrateRequestLogsAfterLedgerReplacement(
|
|
|
626
630
|
export function addRequestLog(entry: RequestLogEntry) {
|
|
627
631
|
// Rows keep proxy-owned error facts only; upstream error text and tokens stop here (both the
|
|
628
632
|
// retained ring row and the usage.jsonl projection below read the projected entry).
|
|
633
|
+
const attemptNormalizationDropped = entry.attemptNormalizationDropped === true, client = normalizeRecordedClient(entry.client) ?? "unknown";
|
|
634
|
+
const clientDescriptor = Object.getOwnPropertyDescriptor(entry, "client");
|
|
635
|
+
if (!Object.isFrozen(entry) || !clientDescriptor?.enumerable || !("value" in clientDescriptor) || clientDescriptor.value !== client || Object.values(Object.getOwnPropertyDescriptors(entry)).some(descriptor => !("value" in descriptor))) {
|
|
636
|
+
entry = { ...entry, ...errorDetailFor(entry), client, ...(attemptNormalizationDropped ? { attemptNormalizationDropped: true } : {}) };
|
|
637
|
+
}
|
|
629
638
|
entry = projectErrorDetail(entry);
|
|
639
|
+
if (attemptNormalizationDropped && entry.attemptNormalizationDropped !== true) entry = { ...entry, attemptNormalizationDropped: true };
|
|
630
640
|
// Sanitize ONCE, at the ingress, and use that one value for both destinations.
|
|
631
641
|
//
|
|
632
642
|
// `addFinalRequestLog` is not the only way in: `addRequestLog` is exported and callable
|
|
@@ -679,6 +689,7 @@ export function addRequestLog(entry: RequestLogEntry) {
|
|
|
679
689
|
timestamp: entry.timestamp,
|
|
680
690
|
provider: entry.provider,
|
|
681
691
|
model: entry.model,
|
|
692
|
+
client: entry.client ?? "unknown",
|
|
682
693
|
...(isKnownUsageSurface(entry.surface) ? { surface: entry.surface } : {}),
|
|
683
694
|
// This function REBUILDS the persisted row field by field rather than
|
|
684
695
|
// spreading it, so a field missing here reaches /api/logs and never
|
|
@@ -1558,6 +1569,7 @@ function writeFinalRequestLog(
|
|
|
1558
1569
|
timestamp: start,
|
|
1559
1570
|
model: isCombo ? logCtx.requestedModel! : logCtx.model,
|
|
1560
1571
|
provider: isCombo ? "combo" : logCtx.provider,
|
|
1572
|
+
client: normalizeRecordedClient(logCtx.client) ?? "unknown",
|
|
1561
1573
|
...(logCtx.surface ? { surface: logCtx.surface } : {}),
|
|
1562
1574
|
...(logCtx.apiKeyId ? { apiKeyId: logCtx.apiKeyId } : {}),
|
|
1563
1575
|
...(logCtx.admissionKind ? { admissionKind: logCtx.admissionKind } : {}),
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isProxyError, rememberProxyErrorMessage } from "../../lib/client-error-body";
|
|
1
|
+
import { carryProxyError, proxyErrorPresentation, isProxyError, rememberProxyErrorMessage } from "../../lib/client-error-body";
|
|
2
2
|
import { bindAnthropicRejectionResponse } from "../../adapters/anthropic-rejection-memo";
|
|
3
3
|
import { rotateAnthropicAccountOnResponseForInstance } from "../../oauth/anthropic-account-refusal";
|
|
4
4
|
import type { ResponsesRequestContext } from "./core-options";
|
|
@@ -128,6 +128,8 @@ export function createAdapterContinuations(
|
|
|
128
128
|
| "thinkingSignatureRecovery"
|
|
129
129
|
>,
|
|
130
130
|
) {
|
|
131
|
+
// A request-local symbol survives event spreads without being serialized or forgeable by a provider.
|
|
132
|
+
const continuationBodyStall = Symbol("continuation-body-stall");
|
|
131
133
|
const { options, logCtx, config } = requestContext;
|
|
132
134
|
const {
|
|
133
135
|
oauthDispatch,
|
|
@@ -158,7 +160,7 @@ export function createAdapterContinuations(
|
|
|
158
160
|
}
|
|
159
161
|
const refusal = unwrapUpstreamRetryEvidenceError(error);
|
|
160
162
|
if (refusal instanceof AdmissionModelDeniedError || refusal instanceof SendBudgetExhaustedError) rememberProxyErrorMessage(logCtx, refusal.message, { status: refusal instanceof AdmissionModelDeniedError ? 403 : 429,
|
|
161
|
-
type: refusal instanceof AdmissionModelDeniedError ? MODEL_NOT_ALLOWED_FOR_KEY : "rate_limit_error", code: refusal instanceof AdmissionModelDeniedError ? MODEL_NOT_ALLOWED_FOR_KEY : refusal.code });
|
|
163
|
+
type: refusal instanceof AdmissionModelDeniedError ? MODEL_NOT_ALLOWED_FOR_KEY : "rate_limit_error", code: refusal instanceof AdmissionModelDeniedError ? MODEL_NOT_ALLOWED_FOR_KEY : refusal.code, presentation: refusal instanceof AdmissionModelDeniedError ? "scope" : "send" });
|
|
162
164
|
if (refusal instanceof AdmissionModelDeniedError) {
|
|
163
165
|
return {
|
|
164
166
|
type: "error", message: refusal.message, status: 403,
|
|
@@ -696,14 +698,14 @@ export function createAdapterContinuations(
|
|
|
696
698
|
}
|
|
697
699
|
|
|
698
700
|
if (!response.ok) {
|
|
699
|
-
if (route.providerName === "kiro") response = await normalizeFinalKiroHttpError(response, upstream.signal);
|
|
701
|
+
if (route.providerName === "kiro") response = carryProxyError(response, await normalizeFinalKiroHttpError(response, upstream.signal));
|
|
700
702
|
const errorText = await readDisplaySafeErrorText(response, upstream.signal, "unknown error");
|
|
701
703
|
const normalized = normalizeUpstreamErrorText(errorText, "unknown error");
|
|
702
704
|
const message = normalized.cyberPolicy
|
|
703
705
|
? normalized.message ?? (isCyberPolicyCode(normalized.code) ? CYBER_POLICY_FALLBACK_MESSAGE : normalized.safeText)
|
|
704
706
|
: `Provider continuation error ${response.status}: ${normalized.safeText}`;
|
|
705
707
|
if (isProxyError(response)) rememberProxyErrorMessage(logCtx, message, { status: normalized.cyberPolicy ? 400 : response.status,
|
|
706
|
-
type: normalized.cyberPolicy ? normalized.type ?? CYBER_POLICY_ERROR_CODE : undefined, code: normalized.cyberPolicy ? CYBER_POLICY_ERROR_CODE : undefined });
|
|
708
|
+
type: normalized.cyberPolicy ? normalized.type ?? CYBER_POLICY_ERROR_CODE : undefined, code: normalized.cyberPolicy ? CYBER_POLICY_ERROR_CODE : undefined, presentation: proxyErrorPresentation(response) });
|
|
707
709
|
yield {
|
|
708
710
|
type: "error",
|
|
709
711
|
status: normalized.cyberPolicy ? 400 : response.status,
|
|
@@ -749,7 +751,10 @@ export function createAdapterContinuations(
|
|
|
749
751
|
// source can synchronously abort the shared signal, which would otherwise report
|
|
750
752
|
// this timeout as a client cancellation.
|
|
751
753
|
if (error instanceof ResponseBodyInactivityError) {
|
|
752
|
-
|
|
754
|
+
const event: AdapterEvent = { type: "error", message: error.message, status: 504,
|
|
755
|
+
errorType: "upstream_error", code: "upstream_stall_timeout" };
|
|
756
|
+
Object.defineProperty(event, continuationBodyStall, { value: true, enumerable: true });
|
|
757
|
+
yield event;
|
|
753
758
|
} else if (options.abortSignal?.aborted || upstream.signal.aborted) {
|
|
754
759
|
yield { type: "error", message: "client closed request during terminal continuation", status: 499 };
|
|
755
760
|
} else {
|
|
@@ -783,6 +788,7 @@ export function createAdapterContinuations(
|
|
|
783
788
|
terminalGuardEnabled,
|
|
784
789
|
fetchTerminalGuardContinuation,
|
|
785
790
|
fetchGuardedEmptyCompletionRetry,
|
|
791
|
+
isContinuationBodyStall: (event: AdapterEvent): boolean => continuationBodyStall in event,
|
|
786
792
|
};
|
|
787
793
|
}
|
|
788
794
|
|