@bitkyc08/opencodex 2.96.0 → 2.97.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 +214 -19
- package/gui/dist/assets/{App-bwugJRKn.js → App-DuNhggE1.js} +15 -15
- package/gui/dist/assets/{Tray-BOPQyDbH.js → Tray-CEw0y0jR.js} +1 -1
- package/gui/dist/assets/index-CynE_Rue.js +94 -0
- package/gui/dist/assets/{quota-summary-6HsrHQAy.js → quota-summary-VHFSlUuM.js} +1 -1
- package/gui/dist/index.html +1 -1
- package/package.json +1 -1
- package/src/adapters/command-code.ts +15 -16
- package/src/adapters/cursor/transport-retry.ts +2 -2
- package/src/adapters/devin/cloud-direct/chat.ts +15 -6
- package/src/adapters/kiro/stream.ts +63 -4
- package/src/adapters/kiro-constants.ts +15 -1
- package/src/adapters/kiro-events.ts +31 -1
- package/src/adapters/kiro-retry.ts +3 -3
- package/src/adapters/ollama-native.ts +11 -1
- package/src/adapters/openai-responses/empty-assistant.ts +15 -3
- package/src/adapters/openai-responses/passthrough.ts +5 -4
- package/src/adapters/openai-responses/reasoning.ts +9 -4
- package/src/adapters/openai-responses/request-strips.ts +1 -1
- package/src/adapters/physical-send.ts +3 -3
- package/src/adapters/zed-openai-stream.ts +2 -1
- package/src/bridge/errors.ts +11 -2
- package/src/bridge/response-json.ts +2 -1
- package/src/bridge/sse.ts +14 -6
- package/src/chat/failure-verdict.ts +21 -0
- package/src/chat/outbound.ts +139 -21
- package/src/claude/outbound.ts +49 -2
- package/src/cli/capabilities-base.ts +25 -3
- package/src/cli/capabilities-command.ts +12 -3
- package/src/cli/capability-contracts.ts +2 -0
- package/src/cli/codex-launch-auth.ts +30 -1
- package/src/cli/codex-launcher.ts +12 -5
- package/src/cli/codex-shim-autorestore.ts +5 -4
- package/src/cli/dispatch.ts +11 -176
- package/src/cli/help.ts +2 -2
- package/src/cli/index.ts +5 -1
- package/src/cli/link.ts +1 -1
- package/src/cli/registry.ts +11 -3
- package/src/cli/root.ts +8 -0
- package/src/cli/sync-command.ts +243 -0
- package/src/cli/sync-fanout-process.ts +92 -0
- package/src/cli/sync-fanout.ts +150 -0
- package/src/cli/sync-flags.ts +10 -0
- package/src/cli/sync-link-status.ts +39 -0
- package/src/cli/sync-update-guard.ts +18 -0
- package/src/client/connect.ts +31 -14
- package/src/client/link-home.ts +3 -2
- package/src/client/machine-listener.ts +3 -0
- package/src/client/restore-ownership.ts +19 -1
- package/src/client/runtime-restart.ts +12 -4
- package/src/client/runtime.ts +40 -11
- package/src/client/stop-routing.ts +62 -0
- package/src/client/sync-runtime.ts +65 -0
- package/src/clients/config-export.ts +32 -3
- package/src/codex/auth-context.ts +49 -103
- package/src/codex/catalog/antigravity-discovery.ts +16 -5
- package/src/codex/catalog/provider-models.ts +6 -8
- package/src/codex/forward-transport-headers.ts +35 -6
- package/src/codex/inject/config-toml.ts +88 -0
- package/src/codex/inject/multi-agent-v2.ts +30 -4
- package/src/codex/inject/plan.ts +34 -7
- package/src/codex/inject-coordination.ts +11 -0
- package/src/codex/inject.ts +18 -7
- package/src/codex/lineage.ts +13 -7
- package/src/codex/quota.ts +22 -6
- package/src/codex/request-owned-affinity-key.ts +11 -0
- package/src/codex/request-owned-affinity.ts +118 -0
- package/src/codex/request-owned-main-pin.ts +86 -0
- package/src/codex/role-capability.ts +52 -41
- package/src/codex/routing/active-account.ts +13 -5
- package/src/codex/routing/selection.ts +40 -1
- package/src/codex/routing/thread-affinity-store.ts +916 -0
- package/src/codex/routing/thread-affinity.ts +90 -12
- package/src/codex/toml-source-lines.ts +72 -13
- package/src/combos/failover.ts +6 -109
- package/src/combos/request.ts +2 -2
- package/src/combos/resolve.ts +29 -11
- package/src/config/process-state.ts +5 -0
- package/src/config/schema/leaf-validators.ts +1 -0
- package/src/generated/compatibility-version.json +433 -165
- package/src/images/loop.ts +2 -1
- package/src/lib/client-error-body.ts +184 -0
- package/src/lib/client-retry-after.ts +66 -0
- package/src/lib/config-ownership.ts +4 -0
- package/src/lib/context-overflow-verdict.ts +113 -0
- package/src/lib/desktop-cli-handoff.d.mts +1 -1
- package/src/lib/desktop-cli-handoff.mjs +11 -1
- package/src/lib/empty-tool-catalog.ts +25 -3
- package/src/lib/error-destination.ts +47 -0
- package/src/lib/failure-detail.ts +66 -0
- package/src/lib/model-identity.ts +44 -0
- package/src/lib/private-sqlite-path.ts +124 -0
- package/src/lib/response-service-tier.ts +17 -0
- package/src/lib/retry-after.ts +3 -0
- package/src/lib/spend-ledger-owner.ts +43 -4
- package/src/lib/upstream-condition.ts +142 -0
- package/src/lib/upstream-retry.ts +14 -11
- package/src/lib/windows-owner-acl.ts +13 -6
- package/src/link/remote-platform.ts +14 -0
- package/src/link/remote-windows.ts +51 -9
- package/src/link/ssh-argv.ts +63 -10
- package/src/oauth/generic-account-failover.ts +13 -11
- package/src/oauth/index.ts +36 -74
- package/src/oauth/oauth-errors.ts +67 -0
- package/src/oauth/store.ts +1 -1
- package/src/protocols/codecs/ollama.ts +197 -0
- package/src/protocols/encoders/adapter-events.ts +4 -0
- package/src/protocols/encoders/chat.ts +18 -10
- package/src/protocols/trace.ts +18 -0
- package/src/providers/fastwire.ts +3 -2
- package/src/providers/quota/anthropic-usage-windows.ts +13 -3
- package/src/providers/quota/vendor-probes-key.ts +60 -24
- package/src/providers/request-pacing.ts +3 -2
- package/src/responses/apply-patch-envelope.ts +45 -19
- package/src/responses/compaction-history-framing.ts +5 -1
- package/src/responses/freeform-wrapper-scan.ts +5 -3
- package/src/responses/progressive-freeform-input.ts +354 -45
- package/src/routing/fallback-policy.ts +3 -1
- package/src/server/admission-model-scope.ts +17 -6
- package/src/server/admitted-http-turn.ts +56 -0
- package/src/server/auth-cors.ts +5 -0
- package/src/server/chat-completions.ts +45 -20
- package/src/server/chat-native-sse.ts +52 -7
- package/src/server/chat-native.ts +60 -35
- package/src/server/claude-bridge-pool-session.ts +59 -0
- package/src/server/claude-messages.ts +51 -24
- package/src/server/context-history.ts +5 -1
- package/src/server/inbound-body-admission.ts +11 -4
- package/src/server/index/ollama-routes.ts +89 -0
- package/src/server/index/serve-options.ts +21 -4
- package/src/server/index/websocket-handler.ts +4 -2
- package/src/server/index.ts +6 -36
- package/src/server/inference/client-encoder-delivery.ts +4 -2
- package/src/server/json-serialized-bytes.ts +44 -0
- package/src/server/management/agent-settings-routes.ts +24 -21
- package/src/server/management/codex-cli-update-routes.ts +2 -1
- package/src/server/management/route-registry.ts +1 -1
- package/src/server/messages-native-scope.ts +2 -1
- package/src/server/messages-native-tool-guard.ts +1 -1
- package/src/server/messages-native.ts +12 -7
- package/src/server/messages-shared.ts +6 -2
- package/src/server/native-final-log.ts +93 -0
- package/src/server/ollama-inbound.ts +349 -0
- package/src/server/ollama-native-paths.ts +21 -0
- package/src/server/ollama-route-policy.ts +73 -0
- package/src/server/relay.ts +22 -3
- package/src/server/request-decompress.ts +4 -2
- package/src/server/request-log-failure-attribution.ts +22 -0
- package/src/server/request-log-failure-detail.ts +80 -0
- package/src/server/request-log-terminal-status.ts +4 -2
- package/src/server/request-log.ts +79 -42
- package/src/server/response-log-body.ts +6 -1
- package/src/server/responses/account-change-state.ts +17 -6
- package/src/server/responses/adapter-continuation.ts +9 -4
- package/src/server/responses/adapter-delivery.ts +5 -4
- package/src/server/responses/adapter-dispatch.ts +31 -30
- package/src/server/responses/anthropic-thinking-recovery.ts +3 -0
- package/src/server/responses/codex-auth-error.ts +19 -18
- package/src/server/responses/codex-ws-exchange.ts +11 -8
- package/src/server/responses/codex-ws-metadata.ts +94 -21
- package/src/server/responses/codex-ws-request.ts +3 -1
- package/src/server/responses/compact.ts +53 -23
- package/src/server/responses/compaction-recovery.ts +4 -2
- package/src/server/responses/compaction-routing.ts +9 -4
- package/src/server/responses/core-auth.ts +32 -14
- package/src/server/responses/core-codex-account.ts +17 -6
- package/src/server/responses/core-combo-failure.ts +5 -1
- package/src/server/responses/core-combo-native.ts +2 -1
- package/src/server/responses/core-combo.ts +41 -27
- package/src/server/responses/core-errors.ts +22 -20
- package/src/server/responses/core-lifetime.ts +6 -3
- package/src/server/responses/core-normalize.ts +1 -0
- package/src/server/responses/core-options.ts +7 -0
- package/src/server/responses/core-pipeline.ts +4 -3
- package/src/server/responses/fetch-helpers.ts +7 -1
- package/src/server/responses/input-admission.ts +28 -2
- package/src/server/responses/native-injection.ts +1 -1
- package/src/server/responses/native-response-json.ts +13 -4
- package/src/server/responses/native-response-output.ts +1 -2
- package/src/server/responses/non-replayable-error.ts +2 -1
- package/src/server/responses/pacing-overload.ts +3 -2
- package/src/server/responses/passthrough-delivery.ts +55 -14
- package/src/server/responses/passthrough-dispatch.ts +19 -17
- package/src/server/responses/passthrough-error.ts +9 -9
- package/src/server/responses/policy-fallback.ts +2 -1
- package/src/server/responses/request-prepare.ts +67 -48
- package/src/server/responses/request-transport.ts +35 -31
- package/src/server/responses/run-turn-execution.ts +6 -6
- package/src/server/responses/sidecar-execution.ts +16 -12
- package/src/server/responses/spend-storage-error.ts +2 -1
- package/src/server/responses/ws-upstream.ts +9 -0
- package/src/server/responses-custom-tool-repair.ts +14 -3
- package/src/server/responses-terminal-repair.ts +5 -2
- package/src/server/responses-undeclared-tool-guard.ts +39 -16
- package/src/server/safe-response-headers.ts +162 -13
- package/src/server/turn-state-handles.ts +164 -0
- package/src/server/workflow-refusal.ts +2 -1
- package/src/service/ownership-mutation-lease.d.mts +11 -2
- package/src/service/ownership-mutation-lease.mjs +53 -11
- package/src/types/config.ts +2 -0
- package/src/update/index.ts +133 -25
- package/src/update/npm-install.d.mts +8 -0
- package/src/update/npm-install.mjs +20 -1
- package/src/update/orchestrated-admission.d.mts +4 -0
- package/src/update/orchestrated-admission.mjs +12 -0
- package/src/update/orchestrated-arguments.d.mts +12 -0
- package/src/update/orchestrated-arguments.mjs +64 -0
- package/src/update/orchestrated-child.d.mts +12 -0
- package/src/update/orchestrated-child.mjs +70 -0
- package/src/update/orchestrated-entry.ts +234 -0
- package/src/update/orchestrated-identity-probe-entry.ts +20 -0
- package/src/update/orchestrated-identity.ts +53 -0
- package/src/update/orchestrated-live-identity.ts +48 -0
- package/src/update/orchestrated-node-driver.mjs +99 -0
- package/src/update/orchestrated-outcome.d.mts +21 -0
- package/src/update/orchestrated-outcome.mjs +40 -0
- package/src/update/orchestrated-plan.ts +49 -0
- package/src/update/orchestrated-pnpm.d.mts +8 -0
- package/src/update/orchestrated-pnpm.mjs +33 -0
- package/src/update/orchestrated-session.d.mts +37 -0
- package/src/update/orchestrated-session.mjs +264 -0
- package/src/update/orchestrated-token.d.mts +10 -0
- package/src/update/orchestrated-token.mjs +21 -0
- package/src/update/orchestrated-token.ts +3 -0
- package/src/update/orchestrated-tree.d.mts +1 -0
- package/src/update/orchestrated-tree.mjs +14 -0
- package/src/update/pnpm-global-install.d.mts +6 -0
- package/src/update/pnpm-global-install.mjs +9 -2
- package/src/update/posix-install-supervisor-entry.mjs +6 -0
- package/src/update/posix-install-supervisor.d.mts +10 -0
- package/src/update/posix-install-supervisor.mjs +88 -0
- package/src/update/sync-update-mutex.d.mts +10 -0
- package/src/update/sync-update-mutex.mjs +164 -0
- package/src/update/sync-update-mutex.ts +1 -0
- package/src/update/test-home-guard.mjs +33 -0
- package/src/update/transactional-install.mjs +2 -2
- package/src/usage/error-detail.ts +225 -0
- package/src/usage/log.ts +60 -27
- package/src/web-search/loop.ts +31 -5
- package/gui/dist/assets/index-ebuVUtB7.js +0 -94
|
@@ -16,11 +16,23 @@ function isEmptyAssistantMessage(item: unknown): boolean {
|
|
|
16
16
|
return item.content === "" || (Array.isArray(item.content) && item.content.every(isEmptyTextPart));
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
/** Exactly Kimi Code's native Responses endpoint; lookalike hosts and other Kimi paths do not match. */
|
|
20
|
+
export function isKimiCodingResponsesUrl(responseUrl: string): boolean {
|
|
21
|
+
let url: URL;
|
|
22
|
+
try { url = new URL(responseUrl); } catch { return false; }
|
|
23
|
+
return url.hostname === "api.kimi.com" && /^\/coding\/v1\/responses\/?$/.test(url.pathname);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The same endpoint over HTTPS on the default port only, for request rewrites scoped to Kimi Code itself. */
|
|
27
|
+
export function isKimiCodingResponsesHttpsUrl(responseUrl: string): boolean {
|
|
28
|
+
if (!isKimiCodingResponsesUrl(responseUrl)) return false;
|
|
29
|
+
const url = new URL(responseUrl);
|
|
30
|
+
return url.protocol === "https:" && url.port === "";
|
|
31
|
+
}
|
|
32
|
+
|
|
19
33
|
/** Kimi Code rejects content-empty assistant messages even on its native Responses endpoint. */
|
|
20
34
|
export function stripKimiEmptyAssistantMessages(body: unknown, responseUrl: string): unknown {
|
|
21
|
-
|
|
22
|
-
try { url = new URL(responseUrl); } catch { return body; }
|
|
23
|
-
if (url.hostname !== "api.kimi.com" || !/^\/coding\/v1\/responses\/?$/.test(url.pathname)) return body;
|
|
35
|
+
if (!isKimiCodingResponsesUrl(responseUrl)) return body;
|
|
24
36
|
if (!isPlainObject(body) || !Array.isArray(body.input)) return body;
|
|
25
37
|
const referencedIds = new Set<string>();
|
|
26
38
|
for (const item of body.input) {
|
|
@@ -57,7 +57,7 @@ import { observeOutbound } from "../../usage/cache-diagnostic";
|
|
|
57
57
|
import { normalizeMuseToolCatalog } from "./muse-tool-catalog";
|
|
58
58
|
import { normalizeMuseToolChoice } from "./muse-tool-choice";
|
|
59
59
|
import { declaresHostedWebSearch, projectHostedSearchHistory, renderCompactionSearchHistoryItems } from "./compaction-search-history";
|
|
60
|
-
import { stripKimiEmptyAssistantMessages } from "./empty-assistant";
|
|
60
|
+
import { isKimiCodingResponsesHttpsUrl, stripKimiEmptyAssistantMessages } from "./empty-assistant";
|
|
61
61
|
import { normalizeOpenGatewayResponsesInput } from "./opengateway-input";
|
|
62
62
|
|
|
63
63
|
/**
|
|
@@ -358,12 +358,13 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
|
|
|
358
358
|
// OpenCode Go speaks the same dialect, which the final model id and URL identify.
|
|
359
359
|
outBody = normalizeXaiResponsesWebSearch(outBody, provider, { modelId: parsed.modelId, responseUrl: url });
|
|
360
360
|
outBody = injectXaiResponsesXSearch(outBody, provider, parsed._replayPrefixLen);
|
|
361
|
-
// xAI, Meta and
|
|
362
|
-
// Codex Desktop asks for it on every turn (#7878).
|
|
361
|
+
// xAI, Meta, Azure and Kimi Code (#8075) answer 400 for `output.encrypted_content`
|
|
362
|
+
// instead of ignoring it; Codex Desktop asks for it on every turn (#7878).
|
|
363
363
|
// Azure also rejects this exact include entry, including on plain Responses routes.
|
|
364
364
|
const isAzureResponses = provider.adapter === "azure" || provider.adapter === "azure-openai"
|
|
365
365
|
|| new URL(url).hostname.toLowerCase().endsWith(".openai.azure.com");
|
|
366
|
-
if (isXaiResponsesDestination(provider) || isMetaAiResponsesDestination(url) || isAzureResponses
|
|
366
|
+
if (isXaiResponsesDestination(provider) || isMetaAiResponsesDestination(url) || isAzureResponses
|
|
367
|
+
|| isKimiCodingResponsesHttpsUrl(url)) {
|
|
367
368
|
outBody = stripUnsupportedOutputInclude(outBody);
|
|
368
369
|
}
|
|
369
370
|
// xAI and explicitly classified compatible gateways reject these OpenAI web_search
|
|
@@ -174,10 +174,15 @@ export function sanitizeReasoningInputContent(
|
|
|
174
174
|
|
|
175
175
|
/** Assistant-authored history items: what DeepSeek merges into one assistant message. */
|
|
176
176
|
function isAssistantTurnItem(item: unknown): boolean {
|
|
177
|
-
if (!isPlainObject(item)
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
177
|
+
if (!isPlainObject(item)) return false;
|
|
178
|
+
// Responses input accepts the message shorthand without `type`. ZCode sends its assistant
|
|
179
|
+
// text that way, directly ahead of the same turn's function calls; reading only a typed
|
|
180
|
+
// `message` would split the turn and place the placeholder backfill after the text, which
|
|
181
|
+
// DeepSeek refuses with the same reasoning_text 400.
|
|
182
|
+
const type = typeof item.type === "string" ? item.type : "role" in item ? "message" : undefined;
|
|
183
|
+
if (type === "reasoning") return true;
|
|
184
|
+
if (type === "message") return item.role === "assistant";
|
|
185
|
+
return type?.endsWith("_call") === true;
|
|
181
186
|
}
|
|
182
187
|
|
|
183
188
|
function isClientToolCallItem(item: unknown): boolean {
|
|
@@ -8,7 +8,7 @@ import { activateDeferredTool } from "./tool-schema";
|
|
|
8
8
|
import { stripOpenAiOnlyWebSearchFields } from "./web-search";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* Codex requests output.encrypted_content, which xAI, Meta and
|
|
11
|
+
* Codex requests output.encrypted_content, which xAI, Meta, Azure and Kimi Code reject with HTTP 400.
|
|
12
12
|
* The caller gates this on the destination. Preserve reasoning.encrypted_content
|
|
13
13
|
* for stateless replay and leave unknown or malformed values for upstream validation.
|
|
14
14
|
*/
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { AdapterFetchContext } from "./base";
|
|
2
2
|
import type { SendClass } from "../lib/request-execution-budget";
|
|
3
3
|
import type { AttemptRecoveryKind } from "../usage/log";
|
|
4
|
-
import { abortError, SendBudgetExhaustedError } from "../lib/upstream-retry";
|
|
4
|
+
import { abortError, SendBudgetExhaustedError, errorDestinationLabel } from "../lib/upstream-retry";
|
|
5
5
|
import {
|
|
6
6
|
releaseProviderRequestSlot,
|
|
7
7
|
sendTrackingRequestSlot,
|
|
@@ -30,14 +30,14 @@ export function createAdapterPhysicalSend(ctx: AdapterFetchContext = {}, fallbac
|
|
|
30
30
|
const decision = ctx.sendBudget?.reserveDispatch({
|
|
31
31
|
sendClass: options.sendClass ?? "transient", targetKey: options.url,
|
|
32
32
|
});
|
|
33
|
-
if (decision && !decision.allowed) throw new SendBudgetExhaustedError(options.url);
|
|
33
|
+
if (decision && !decision.allowed) throw new SendBudgetExhaustedError(errorDestinationLabel(options.url));
|
|
34
34
|
const permit = decision?.allowed ? decision.permit : undefined;
|
|
35
35
|
let dispatched = false;
|
|
36
36
|
const physicalExecutor = (async (input, init) => {
|
|
37
37
|
if (ctx.abortSignal?.aborted) throw abortError(ctx.abortSignal);
|
|
38
38
|
if (init?.signal?.aborted) throw abortError(init.signal);
|
|
39
39
|
const run = async (): Promise<Response> => {
|
|
40
|
-
if (dispatched || (permit && !permit.use())) throw new SendBudgetExhaustedError(options.url);
|
|
40
|
+
if (dispatched || (permit && !permit.use())) throw new SendBudgetExhaustedError(errorDestinationLabel(options.url));
|
|
41
41
|
dispatched = true;
|
|
42
42
|
ordinal += 1;
|
|
43
43
|
ctx.onPhysicalSend?.({ ordinal, ...(options.recovery ? { recovery: options.recovery } : {}) });
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { markProxyError } from "../lib/client-error-body";
|
|
1
2
|
import type { AdapterEvent, OcxMessagePhase, OcxUsage } from "../types";
|
|
2
3
|
import type { TranslatorBudget } from "../lib/translator-budget";
|
|
3
4
|
import { decodeServerSentEvents } from "../lib/sse-decoder";
|
|
@@ -700,7 +701,7 @@ export async function* translateZedOpenAiStream(
|
|
|
700
701
|
yield* flushUnclosed(false);
|
|
701
702
|
if (terminal) return;
|
|
702
703
|
if (unfinished) {
|
|
703
|
-
yield { type: "incomplete", reason: "adapter_eof", ...(usage ? { usage } : {}) };
|
|
704
|
+
yield markProxyError({ type: "incomplete" as const, reason: "adapter_eof", ...(usage ? { usage } : {}) });
|
|
704
705
|
return;
|
|
705
706
|
}
|
|
706
707
|
const done: AdapterEvent = {
|
package/src/bridge/errors.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
REPLAY_REFUSED_STATUS,
|
|
7
7
|
retainReplayRefusal,
|
|
8
8
|
} from "../lib/upstream-retry";
|
|
9
|
+
import { formatClientRetryAfter } from "../lib/client-retry-after";
|
|
9
10
|
import {
|
|
10
11
|
adapterFailureFromMessage,
|
|
11
12
|
classifyError,
|
|
@@ -34,6 +35,12 @@ export function upstreamCodeForComboFailure(response: Response, bodyCode: string
|
|
|
34
35
|
: bodyCode;
|
|
35
36
|
}
|
|
36
37
|
|
|
38
|
+
/** Preserve formatted quota-code provenance when lifecycle helpers rebuild a response. */
|
|
39
|
+
export function carryFormattedQuotaSource(source: Response, target: Response): Response {
|
|
40
|
+
if (formattedQuotaSources.has(source)) formattedQuotaSources.set(target, formattedQuotaSources.get(source)!);
|
|
41
|
+
return target;
|
|
42
|
+
}
|
|
43
|
+
|
|
37
44
|
export function formatErrorResponse(
|
|
38
45
|
status: number,
|
|
39
46
|
type: string,
|
|
@@ -93,7 +100,8 @@ export function formatErrorResponse(
|
|
|
93
100
|
: error.code === SPEND_LEDGER_STORAGE_UNAVAILABLE_CODE ? 503
|
|
94
101
|
: isReplayRefusalCode(error.code) ? REPLAY_REFUSED_STATUS : status;
|
|
95
102
|
const headers = new Headers({ "Content-Type": "application/json" });
|
|
96
|
-
|
|
103
|
+
// Re-emitted as an integer this proxy formats; the incoming text is never echoed.
|
|
104
|
+
const retryAfter = formatClientRetryAfter(options?.retryAfter);
|
|
97
105
|
if (error.code !== CYBER_POLICY_ERROR_CODE
|
|
98
106
|
&& !replayBlocked
|
|
99
107
|
&& retryAfter
|
|
@@ -155,6 +163,7 @@ export function formatUsageLimitReachedResponse(message: string, resetsAtMs: num
|
|
|
155
163
|
...(resetsAtMs !== null ? { resets_at: Math.ceil(resetsAtMs / 1000) } : {}),
|
|
156
164
|
};
|
|
157
165
|
const headers = new Headers({ "Content-Type": "application/json" });
|
|
158
|
-
|
|
166
|
+
const advice = retryAfterSeconds !== null ? formatClientRetryAfter(retryAfterSeconds) : undefined;
|
|
167
|
+
if (advice !== undefined) headers.set("Retry-After", advice);
|
|
159
168
|
return new Response(JSON.stringify({ error }), { status: 429, headers });
|
|
160
169
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { UNDECLARED_TOOL_CALL_MESSAGE } from "../lib/client-error-body";
|
|
1
2
|
import { anthropicProgressItems } from "../responses/anthropic-progress";
|
|
2
3
|
import type {
|
|
3
4
|
AdapterEvent,
|
|
@@ -494,7 +495,7 @@ function buildResponseJSONWithBudget(
|
|
|
494
495
|
) {
|
|
495
496
|
errorEvent = {
|
|
496
497
|
type: "error",
|
|
497
|
-
message:
|
|
498
|
+
message: UNDECLARED_TOOL_CALL_MESSAGE,
|
|
498
499
|
status: 502,
|
|
499
500
|
errorType: "upstream_error",
|
|
500
501
|
};
|
package/src/bridge/sse.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { UNDECLARED_TOOL_CALL_MESSAGE, isProxyError, rememberProxyIncomplete, carryProxyIncompleteContext } from "../lib/client-error-body";
|
|
1
2
|
import { anthropicProgressItems } from "../responses/anthropic-progress";
|
|
2
3
|
import type {
|
|
3
4
|
AdapterEvent,
|
|
@@ -23,7 +24,7 @@ import {
|
|
|
23
24
|
mayBecomePatchEnvelope,
|
|
24
25
|
repairFreeformToolInput,
|
|
25
26
|
} from "../responses/apply-patch-envelope";
|
|
26
|
-
import {
|
|
27
|
+
import { ProgressiveFreeformInputReader } from "../responses/progressive-freeform-input";
|
|
27
28
|
import { encodeCompactionSummary, releaseCompactionCiphertextLease } from "../responses/compaction";
|
|
28
29
|
import { compileCodeModeHelperInput, resolveCodeModeHelperName } from "../responses/code-mode-helper-compat";
|
|
29
30
|
import { mayBecomeCodeModeShellInput } from "../responses/code-mode-shell-input";
|
|
@@ -76,6 +77,7 @@ export function bridgeToResponsesSSE(
|
|
|
76
77
|
heartbeatMs = 2_000,
|
|
77
78
|
options?: MaxOutputCompletionOptions & {
|
|
78
79
|
responseId?: string;
|
|
80
|
+
proxyErrorContext?: object;
|
|
79
81
|
/** Trusted structural grouping only; never provider state or execution authority. */
|
|
80
82
|
outputItemIds?: { next(kind: "message" | "reasoning" | "function_call" | "custom_tool_call" | "tool_search_call"): string };
|
|
81
83
|
|
|
@@ -165,6 +167,7 @@ export function bridgeToResponsesSSE(
|
|
|
165
167
|
};
|
|
166
168
|
},
|
|
167
169
|
): ReadableStream<Uint8Array> {
|
|
170
|
+
const proxyIncompleteContext = options?.proxyErrorContext ?? {};
|
|
168
171
|
const replayCacheScope = options?.replayCacheScope;
|
|
169
172
|
const maxOutputCompletion = createMaxOutputCompletion(options);
|
|
170
173
|
const outputItemId = (kind: "message" | "reasoning" | "function_call" | "custom_tool_call" | "tool_search_call", prefix: string): string =>
|
|
@@ -477,7 +480,7 @@ export function bridgeToResponsesSSE(
|
|
|
477
480
|
// Full assistant text of a compaction turn (across message boundaries) — becomes the
|
|
478
481
|
// synthetic compaction item's payload on done.
|
|
479
482
|
let compaction = emptyChunks();
|
|
480
|
-
let currentToolCall: { itemId: string; outputIndex: number; callId: string; name: string; args: string; argsBytes: number; namespace?: string; freeform?: boolean; toolSearch?: boolean; inputEmitted?: string; codeModeHelperName?: string; providerMetadata?: OcxProviderOpaqueToolCallMetadata } | null = null;
|
|
483
|
+
let currentToolCall: { itemId: string; outputIndex: number; callId: string; name: string; args: string; argsBytes: number; namespace?: string; freeform?: boolean; toolSearch?: boolean; inputEmitted?: string; freeformReader?: ProgressiveFreeformInputReader; codeModeHelperName?: string; providerMetadata?: OcxProviderOpaqueToolCallMetadata } | null = null;
|
|
481
484
|
// Open native web-search cell (between begin and end). Holds the output index allocated on
|
|
482
485
|
// begin so the matching done reuses it; closed as `failed` if the stream terminates early.
|
|
483
486
|
let currentWebSearch: { itemId: string; eventId: string; outputIndex: number } | null = null;
|
|
@@ -1092,7 +1095,7 @@ export function bridgeToResponsesSSE(
|
|
|
1092
1095
|
const failure = responseError(
|
|
1093
1096
|
502,
|
|
1094
1097
|
"upstream_error",
|
|
1095
|
-
|
|
1098
|
+
UNDECLARED_TOOL_CALL_MESSAGE,
|
|
1096
1099
|
);
|
|
1097
1100
|
emit("response.failed", {
|
|
1098
1101
|
response: {
|
|
@@ -1154,7 +1157,9 @@ export function bridgeToResponsesSSE(
|
|
|
1154
1157
|
// is the same disagreement in the other direction.
|
|
1155
1158
|
const ownsFreeformGrammar = currentToolCall.namespace === undefined
|
|
1156
1159
|
|| currentToolCall.namespace === "functions";
|
|
1157
|
-
|
|
1160
|
+
// One reader per call resumes where the previous delta stopped.
|
|
1161
|
+
currentToolCall.freeformReader ??= new ProgressiveFreeformInputReader();
|
|
1162
|
+
const full = currentToolCall.freeformReader.update(
|
|
1158
1163
|
currentToolCall.args,
|
|
1159
1164
|
ownsFreeformGrammar ? currentToolCall.name : "",
|
|
1160
1165
|
);
|
|
@@ -1330,6 +1335,7 @@ export function bridgeToResponsesSSE(
|
|
|
1330
1335
|
break;
|
|
1331
1336
|
}
|
|
1332
1337
|
case "incomplete": {
|
|
1338
|
+
if (isProxyError(event) && event.reason === "adapter_eof") rememberProxyIncomplete(proxyIncompleteContext, "adapter_eof");
|
|
1333
1339
|
if (currentMsg) closeCurrentMessage();
|
|
1334
1340
|
if (currentReasoning) closeCurrentReasoning();
|
|
1335
1341
|
if (currentRawReasoning) closeCurrentRawReasoning();
|
|
@@ -1470,6 +1476,7 @@ export function bridgeToResponsesSSE(
|
|
|
1470
1476
|
})) return;
|
|
1471
1477
|
options?.onUsage?.(undefined);
|
|
1472
1478
|
await awaitThoughtSignatureDurability();
|
|
1479
|
+
rememberProxyIncomplete(proxyIncompleteContext, "adapter_eof");
|
|
1473
1480
|
emit("response.incomplete", {
|
|
1474
1481
|
response: {
|
|
1475
1482
|
...responseSnapshot("incomplete", finishedItems),
|
|
@@ -1526,6 +1533,7 @@ export function bridgeToResponsesSSE(
|
|
|
1526
1533
|
// barrier is not awaited on the stall-timeout kill path. The in-memory store is
|
|
1527
1534
|
// already updated; only a crash between here and the queued write loses it,
|
|
1528
1535
|
// which is the pre-#1926 status quo for an already-abnormal termination.
|
|
1536
|
+
rememberProxyIncomplete(proxyIncompleteContext, "upstream_stall_timeout");
|
|
1529
1537
|
emit("response.incomplete", {
|
|
1530
1538
|
response: {
|
|
1531
1539
|
...responseSnapshot("incomplete", finishedItems),
|
|
@@ -1558,7 +1566,7 @@ export function bridgeToResponsesSSE(
|
|
|
1558
1566
|
}, heartbeatMs);
|
|
1559
1567
|
};
|
|
1560
1568
|
|
|
1561
|
-
return new ReadableStream<Uint8Array>({
|
|
1569
|
+
return carryProxyIncompleteContext(proxyIncompleteContext, new ReadableStream<Uint8Array>({
|
|
1562
1570
|
start(streamController) {
|
|
1563
1571
|
controller = streamController;
|
|
1564
1572
|
startStream();
|
|
@@ -1577,5 +1585,5 @@ export function bridgeToResponsesSSE(
|
|
|
1577
1585
|
releasePendingWebSources();
|
|
1578
1586
|
disposeOwnedBudget();
|
|
1579
1587
|
},
|
|
1580
|
-
});
|
|
1588
|
+
}));
|
|
1581
1589
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ClientErrorContract } from "../lib/client-error-body";
|
|
2
|
+
import { isCyberPolicyMessage, isTerminalRefusalCode } from "../lib/errors";
|
|
3
|
+
export interface FailureVerdict { status: number; type: string; code: string | null; message: string }
|
|
4
|
+
/** Passed explicitly through producer, lifetime wrappers and collector for one response. */
|
|
5
|
+
export interface FailureVerdictSink { verdict?: FailureVerdict; contract?: ClientErrorContract; proxyAuthored?: boolean }
|
|
6
|
+
export function legacyStreamErrorStatus(message: string): number {
|
|
7
|
+
const lower = message.toLowerCase();
|
|
8
|
+
if (isCyberPolicyMessage(lower)) return 400;
|
|
9
|
+
if (lower.includes("truncated")) return 502;
|
|
10
|
+
if (lower.includes("rate") || lower.includes("429")) return 429;
|
|
11
|
+
if (lower.includes("unauthor") || lower.includes("401") || lower.includes("api key")) return 401;
|
|
12
|
+
if (lower.includes("not found") || lower.includes("404")) return 404;
|
|
13
|
+
if (lower.includes("invalid") || lower.includes("400")) return 400;
|
|
14
|
+
return 502;
|
|
15
|
+
}
|
|
16
|
+
export function legacyFailureVerdict(payload: Record<string, unknown>): FailureVerdict {
|
|
17
|
+
const error = payload.error as { message: string; type: string; code: string | null };
|
|
18
|
+
const { message, code } = error;
|
|
19
|
+
return { message, code, type: code === "translation_buffer_limit" ? "upstream_error" : error.type,
|
|
20
|
+
status: code === "translation_buffer_limit" ? 502 : isTerminalRefusalCode(code) || isCyberPolicyMessage(message) ? 400 : legacyStreamErrorStatus(message) };
|
|
21
|
+
}
|
package/src/chat/outbound.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { boundedServiceTier } from "../lib/response-service-tier";
|
|
2
|
+
import { isLikelyRealAnthropicThinkingSignature } from "../responses/reasoning-envelope";
|
|
3
|
+
import { ownedChatErrorPayload, CLIENT_ERROR_CONTRACTS, hasProxyErrorMessage, hasProxyIncomplete, type ClientErrorContract } from "../lib/client-error-body";
|
|
4
|
+
import { legacyFailureVerdict, legacyStreamErrorStatus as streamErrorStatus, type FailureVerdictSink } from "./failure-verdict";
|
|
1
5
|
/**
|
|
2
6
|
* Chat Completions outbound: internal /v1/responses output -> OpenAI Chat Completions shapes.
|
|
3
7
|
*
|
|
@@ -126,16 +130,6 @@ export function isChatCompletionsStreamError(err: unknown): err is ChatCompletio
|
|
|
126
130
|
return err instanceof ChatCompletionsStreamError;
|
|
127
131
|
}
|
|
128
132
|
|
|
129
|
-
function streamErrorStatus(message: string): number {
|
|
130
|
-
const lower = message.toLowerCase();
|
|
131
|
-
if (isCyberPolicyMessage(lower)) return 400;
|
|
132
|
-
if (lower.includes("truncated")) return 502;
|
|
133
|
-
if (lower.includes("rate") || lower.includes("429")) return 429;
|
|
134
|
-
if (lower.includes("unauthor") || lower.includes("401") || lower.includes("api key")) return 401;
|
|
135
|
-
if (lower.includes("not found") || lower.includes("404")) return 404;
|
|
136
|
-
if (lower.includes("invalid") || lower.includes("400")) return 400;
|
|
137
|
-
return 502;
|
|
138
|
-
}
|
|
139
133
|
|
|
140
134
|
export function dataFrame(payload: Rec | "[DONE]"): string {
|
|
141
135
|
if (payload === "[DONE]") return "data: [DONE]\n\n";
|
|
@@ -169,6 +163,8 @@ export function appendedUtf8Bytes(previous: string, previousBytes: number, fragm
|
|
|
169
163
|
|
|
170
164
|
/** Details a streamed Chat failure carries into its error frame. */
|
|
171
165
|
export interface ChatCompletionsStreamFailure {
|
|
166
|
+
contract?: ClientErrorContract;
|
|
167
|
+
proxyAuthored?: boolean;
|
|
172
168
|
code?: string | null;
|
|
173
169
|
type?: string;
|
|
174
170
|
status?: number;
|
|
@@ -179,7 +175,7 @@ export interface ChatCompletionsStreamFailure {
|
|
|
179
175
|
* fixed, bounded failures that must be delivered even when the translation budget is exhausted.
|
|
180
176
|
* Provider text is redacted; the two fixed failures carry no provider text at all.
|
|
181
177
|
*/
|
|
182
|
-
|
|
178
|
+
function legacyChatCompletionsStreamErrorPayload(
|
|
183
179
|
message: string,
|
|
184
180
|
details?: ChatCompletionsStreamFailure,
|
|
185
181
|
): { payload: Rec; bounded: boolean } {
|
|
@@ -224,6 +220,25 @@ export function chatCompletionsStreamErrorPayload(
|
|
|
224
220
|
};
|
|
225
221
|
}
|
|
226
222
|
|
|
223
|
+
export function chatCompletionsStreamErrorPayload(message: string, details?: ChatCompletionsStreamFailure) {
|
|
224
|
+
const legacy = legacyChatCompletionsStreamErrorPayload(message, details);
|
|
225
|
+
const verdict = legacyFailureVerdict(legacy.payload);
|
|
226
|
+
const error = legacy.payload.error as { message: string; type: string; code: string | null };
|
|
227
|
+
return { bounded: legacy.bounded, verdict, payload: details?.proxyAuthored ? legacy.payload : ownedChatErrorPayload({
|
|
228
|
+
status: verdict.status, type: error.type, code: error.code, contract: details?.contract,
|
|
229
|
+
evidence: { status: verdict.status, type: error.type, code: error.code ?? undefined, message },
|
|
230
|
+
}) };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function ownedChatErrorResponse(status: number, message: string, type?: string, code?: string | null, contract?: ClientErrorContract, proxyAuthored = false): Response {
|
|
234
|
+
const legacyBody = chatCompletionsErrorBody(status, message, type, code);
|
|
235
|
+
const legacy = legacyBody.error as { type: string; code: string | null };
|
|
236
|
+
const finalStatus = legacy.code === CYBER_POLICY_ERROR_CODE ? 400 : status;
|
|
237
|
+
return new Response(JSON.stringify(proxyAuthored ? legacyBody : ownedChatErrorPayload({ status: finalStatus, type: legacy.type, code: legacy.code, contract,
|
|
238
|
+
evidence: { status: finalStatus, type, code: code ?? undefined, message } })),
|
|
239
|
+
{ status: finalStatus, headers: { "Content-Type": "application/json" } });
|
|
240
|
+
}
|
|
241
|
+
|
|
227
242
|
/** How a Responses `response.failed` error object becomes a streamed Chat failure. */
|
|
228
243
|
export function chatCompletionsFailedResponse(error: Rec): { message: string; details: ChatCompletionsStreamFailure } {
|
|
229
244
|
const message = typeof error.message === "string" ? error.message : "upstream request failed";
|
|
@@ -250,7 +265,7 @@ export function chatCompletionsFailedResponse(error: Rec): { message: string; de
|
|
|
250
265
|
export function chatCompletionsIncompleteOutcome(
|
|
251
266
|
reason: unknown,
|
|
252
267
|
message: unknown,
|
|
253
|
-
): { finishReason: "length" | "content_filter" } | { failMessage: string } {
|
|
268
|
+
): { finishReason: "length" | "content_filter" } | { failMessage: string; originalFailMessage: string } {
|
|
254
269
|
// Upstream Responses endpoints do not all use OpenAI's `max_output_tokens`: Volcengine Ark
|
|
255
270
|
// (Coding Plan `/responses`) sends `incomplete_details.reason: "length"` for a hit output cap.
|
|
256
271
|
// Read the reason through the shared truncation vocabulary so such a turn ends with
|
|
@@ -261,7 +276,8 @@ export function chatCompletionsIncompleteOutcome(
|
|
|
261
276
|
if (truncation === "content_filter") return { finishReason: "content_filter" };
|
|
262
277
|
const why = typeof reason === "string" ? reason : "unknown";
|
|
263
278
|
return {
|
|
264
|
-
failMessage:
|
|
279
|
+
failMessage: CLIENT_ERROR_CONTRACTS.stream_incomplete,
|
|
280
|
+
originalFailMessage: typeof message === "string" && message.length > 0
|
|
265
281
|
? message
|
|
266
282
|
: `upstream stream ended early (${why})`,
|
|
267
283
|
};
|
|
@@ -281,7 +297,7 @@ function refusalTranslationError(): ChatCompletionsStreamError {
|
|
|
281
297
|
export function responsesSseToChatCompletionsSse(
|
|
282
298
|
upstream: ReadableStream<Uint8Array>,
|
|
283
299
|
model: string,
|
|
284
|
-
opts: { translatorBudget: TranslatorBudget },
|
|
300
|
+
opts: { translatorBudget: TranslatorBudget; failureVerdictSink?: FailureVerdictSink; proxyErrorContext?: object },
|
|
285
301
|
): ReadableStream<Uint8Array> {
|
|
286
302
|
const translatorBudget = opts.translatorBudget;
|
|
287
303
|
const encoder = new TextEncoder();
|
|
@@ -610,7 +626,7 @@ export function responsesSseToChatCompletionsSse(
|
|
|
610
626
|
terminated = true;
|
|
611
627
|
releaseRefusals();
|
|
612
628
|
};
|
|
613
|
-
const fail = (message: string, details?:
|
|
629
|
+
const fail = (message: string, details?: ChatCompletionsStreamFailure) => {
|
|
614
630
|
if (terminated) return;
|
|
615
631
|
terminated = true;
|
|
616
632
|
failed = true;
|
|
@@ -623,13 +639,14 @@ export function responsesSseToChatCompletionsSse(
|
|
|
623
639
|
// Deliver the error frame then close the stream abnormally (no [DONE]).
|
|
624
640
|
// Do not controller.error() — that can drop already-enqueued bytes from consumers
|
|
625
641
|
// like response.text().
|
|
626
|
-
const { payload, bounded } = chatCompletionsStreamErrorPayload(message, details);
|
|
642
|
+
const { payload, bounded, verdict } = chatCompletionsStreamErrorPayload(message, details);
|
|
627
643
|
try {
|
|
628
644
|
const frame = encoder.encode(dataFrame(payload));
|
|
629
645
|
// These fixed, bounded failures must survive even when decoder-owned input
|
|
630
646
|
// still fills the budget. They contain no provider text or IDs.
|
|
631
647
|
if (bounded) controller.enqueue(frame);
|
|
632
648
|
else enqueueLiveFrame(frame);
|
|
649
|
+
if (opts.failureVerdictSink) { opts.failureVerdictSink.verdict = verdict; opts.failureVerdictSink.contract = details?.contract; opts.failureVerdictSink.proxyAuthored = details?.proxyAuthored; }
|
|
633
650
|
emittedFrames++;
|
|
634
651
|
} catch {
|
|
635
652
|
/* controller may already be closed */
|
|
@@ -789,14 +806,15 @@ export function responsesSseToChatCompletionsSse(
|
|
|
789
806
|
// upstream_stall_timeout / adapter_eof / proxy-synthesized incompletes are
|
|
790
807
|
// failures, not early finishes: emit an error frame and close WITHOUT
|
|
791
808
|
// [DONE] instead of a success-looking stop/tool_calls + [DONE].
|
|
792
|
-
fail(outcome.
|
|
809
|
+
fail(outcome.originalFailMessage, { contract: "stream_incomplete",
|
|
810
|
+
proxyAuthored: hasProxyIncomplete(opts.proxyErrorContext ?? upstream, details.reason, details.message) });
|
|
793
811
|
}
|
|
794
812
|
break;
|
|
795
813
|
}
|
|
796
814
|
case "response.failed": {
|
|
797
815
|
const response = isRec(data.response) ? data.response : {};
|
|
798
816
|
const failure = chatCompletionsFailedResponse(isRec(response.error) ? response.error : {});
|
|
799
|
-
fail(failure.message, failure.details);
|
|
817
|
+
fail(failure.message, { ...failure.details, proxyAuthored: hasProxyErrorMessage(opts.proxyErrorContext, failure.message, failure.details) });
|
|
800
818
|
break;
|
|
801
819
|
}
|
|
802
820
|
default:
|
|
@@ -1003,11 +1021,105 @@ export function responsesJsonToChatCompletion(json: unknown, model: string, tran
|
|
|
1003
1021
|
};
|
|
1004
1022
|
}
|
|
1005
1023
|
|
|
1024
|
+
function flatNumericFields(value: unknown, fields: readonly string[]): Rec {
|
|
1025
|
+
const out: Rec = {};
|
|
1026
|
+
if (isRec(value)) for (const field of fields) {
|
|
1027
|
+
const number = value[field];
|
|
1028
|
+
if (typeof number === "number" && Number.isFinite(number) && number >= 0) out[field] = number;
|
|
1029
|
+
}
|
|
1030
|
+
return out;
|
|
1031
|
+
}
|
|
1032
|
+
function flatCollectorUsage(value: unknown): Rec | undefined {
|
|
1033
|
+
if (!isRec(value)) return undefined;
|
|
1034
|
+
const out = flatNumericFields(value, ["prompt_tokens", "completion_tokens", "total_tokens"]);
|
|
1035
|
+
for (const [field, counters] of [
|
|
1036
|
+
["prompt_tokens_details", ["cached_tokens", "audio_tokens"]],
|
|
1037
|
+
["completion_tokens_details", ["reasoning_tokens", "audio_tokens", "accepted_prediction_tokens", "rejected_prediction_tokens"]],
|
|
1038
|
+
] as const) {
|
|
1039
|
+
const details = flatNumericFields(value[field], counters);
|
|
1040
|
+
if (Object.keys(details).length > 0) out[field] = details;
|
|
1041
|
+
}
|
|
1042
|
+
return out;
|
|
1043
|
+
}
|
|
1044
|
+
const FLAT_FINISH_REASONS: ReadonlyMap<string, string> = new Map([
|
|
1045
|
+
["stop", "stop"], ["length", "length"], ["tool_calls", "tool_calls"], ["content_filter", "content_filter"],
|
|
1046
|
+
["function_call", "function_call"], ["error", "error"],
|
|
1047
|
+
]);
|
|
1048
|
+
function flatCollectorChoices(value: unknown): Rec[] | undefined {
|
|
1049
|
+
if (!Array.isArray(value)) return undefined;
|
|
1050
|
+
const first = value[0];
|
|
1051
|
+
if (!isRec(first)) return [];
|
|
1052
|
+
const choice: Rec = {};
|
|
1053
|
+
if (typeof first.finish_reason === "string") choice.finish_reason = FLAT_FINISH_REASONS.get(first.finish_reason) ?? "error";
|
|
1054
|
+
if (!isRec(first.delta)) return [choice];
|
|
1055
|
+
const original = first.delta;
|
|
1056
|
+
const delta: Rec = {};
|
|
1057
|
+
for (const field of ["content", "reasoning_content"]) if (typeof original[field] === "string") delta[field] = original[field];
|
|
1058
|
+
if (original.refusal !== undefined) {
|
|
1059
|
+
// Preserve the collector's invalid-refusal verdict without retaining an invalid object.
|
|
1060
|
+
delta.refusal = typeof original.refusal === "string" || original.refusal === null ? original.refusal : false;
|
|
1061
|
+
}
|
|
1062
|
+
if (Array.isArray(original.reasoning_details)) delta.reasoning_details = original.reasoning_details.filter(isRec).flatMap(detail => {
|
|
1063
|
+
const type = detail.type === "reasoning.text" ? "reasoning.text" : detail.type === "reasoning.encrypted" ? "reasoning.encrypted"
|
|
1064
|
+
: detail.type === "reasoning.summary" ? "reasoning.summary" : undefined;
|
|
1065
|
+
if (!type) return [];
|
|
1066
|
+
const entry: Rec = { type };
|
|
1067
|
+
if (type === "reasoning.text") {
|
|
1068
|
+
if (typeof detail.text === "string") entry.text = detail.text;
|
|
1069
|
+
if (typeof detail.signature === "string" && isLikelyRealAnthropicThinkingSignature(detail.signature)) entry.signature = detail.signature;
|
|
1070
|
+
} else if (type === "reasoning.summary") {
|
|
1071
|
+
if (typeof detail.summary !== "string") return [];
|
|
1072
|
+
entry.summary = detail.summary;
|
|
1073
|
+
} else if (typeof detail.data === "string") entry.data = detail.data;
|
|
1074
|
+
if (typeof detail.format === "string" && /^[A-Za-z][A-Za-z0-9._-]{0,95}-v[1-9][0-9]{0,3}$/.test(detail.format)) entry.format = detail.format;
|
|
1075
|
+
if (typeof detail.id === "string" && detail.id.length <= 128
|
|
1076
|
+
&& /^(?:reasoning[-_]|summary[-_]|rs[-_])[A-Za-z0-9._:-]+$/.test(detail.id)) entry.id = detail.id;
|
|
1077
|
+
if (typeof detail.index === "number" && Number.isSafeInteger(detail.index) && detail.index >= 0) entry.index = detail.index;
|
|
1078
|
+
return [entry];
|
|
1079
|
+
});
|
|
1080
|
+
if (Array.isArray(original.tool_calls)) delta.tool_calls = original.tool_calls.filter(isRec).map(tool => {
|
|
1081
|
+
const call: Rec = {};
|
|
1082
|
+
if (typeof tool.index === "number" && Number.isSafeInteger(tool.index) && tool.index >= 0) call.index = tool.index;
|
|
1083
|
+
if (typeof tool.id === "string") call.id = tool.id;
|
|
1084
|
+
const fn: Rec = {};
|
|
1085
|
+
if (isRec(tool.function)) for (const field of ["name", "arguments"]) if (typeof tool.function[field] === "string") fn[field] = tool.function[field];
|
|
1086
|
+
call.function = fn;
|
|
1087
|
+
return call;
|
|
1088
|
+
});
|
|
1089
|
+
choice.delta = delta;
|
|
1090
|
+
return [choice];
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
/** Client fields from ordinary chunks needed only by a buffered native collector. */
|
|
1094
|
+
export class ChatCompletionsCollectorSource {
|
|
1095
|
+
private pending: Array<{ wire: string; fields: string; bytes: number }> = [];
|
|
1096
|
+
constructor(private readonly budget: TranslatorBudget) {}
|
|
1097
|
+
retain(wire: string, chunk: Rec): void {
|
|
1098
|
+
const fields = JSON.stringify({ usage: flatCollectorUsage(chunk.usage), service_tier: boundedServiceTier(chunk.service_tier), choices: flatCollectorChoices(chunk.choices) });
|
|
1099
|
+
const bytes = Buffer.byteLength(wire) + Buffer.byteLength(fields) + 64;
|
|
1100
|
+
this.budget.chargeRetained(bytes, { kind: "retained_collectors" });
|
|
1101
|
+
this.pending.push({ wire, fields, bytes });
|
|
1102
|
+
}
|
|
1103
|
+
take(wire: string): Rec | undefined {
|
|
1104
|
+
const next = this.pending[0];
|
|
1105
|
+
if (!next || next.wire !== wire) return undefined;
|
|
1106
|
+
this.pending.shift();
|
|
1107
|
+
this.budget.releaseRetained(next.bytes, { kind: "retained_collectors" });
|
|
1108
|
+
return JSON.parse(next.fields) as Rec;
|
|
1109
|
+
}
|
|
1110
|
+
clear(): void {
|
|
1111
|
+
for (const entry of this.pending) this.budget.releaseRetained(entry.bytes, { kind: "retained_collectors" });
|
|
1112
|
+
this.pending.length = 0;
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1006
1116
|
/** Fold a Chat Completions SSE stream into a final completion JSON. */
|
|
1007
1117
|
export async function collectChatCompletion(
|
|
1008
1118
|
stream: ReadableStream<Uint8Array>,
|
|
1009
1119
|
model: string,
|
|
1010
1120
|
translatorBudget: TranslatorBudget,
|
|
1121
|
+
failureVerdictSink?: FailureVerdictSink,
|
|
1122
|
+
collectorSource?: ChatCompletionsCollectorSource,
|
|
1011
1123
|
): Promise<Rec> {
|
|
1012
1124
|
const reader = stream.getReader();
|
|
1013
1125
|
const decoder = new TextDecoder();
|
|
@@ -1055,10 +1167,15 @@ export async function collectChatCompletion(
|
|
|
1055
1167
|
for (let frame = buffer.next(); frame; frame = buffer.next()) {
|
|
1056
1168
|
const data = sseDataPayload(frame.block)?.trim();
|
|
1057
1169
|
if (!data || data === "[DONE]") continue;
|
|
1058
|
-
let
|
|
1059
|
-
try {
|
|
1060
|
-
if (!isRec(
|
|
1170
|
+
let decoded: unknown;
|
|
1171
|
+
try { decoded = JSON.parse(data); } catch { continue; }
|
|
1172
|
+
if (!isRec(decoded)) continue;
|
|
1173
|
+
const parsed = collectorSource?.take(data) ?? decoded;
|
|
1061
1174
|
if (isRec(parsed.error)) {
|
|
1175
|
+
if (failureVerdictSink?.verdict) {
|
|
1176
|
+
const verdict = failureVerdictSink.verdict;
|
|
1177
|
+
throw new ChatCompletionsStreamError(verdict.message, verdict);
|
|
1178
|
+
}
|
|
1062
1179
|
const message = typeof parsed.error.message === "string"
|
|
1063
1180
|
? parsed.error.message
|
|
1064
1181
|
: "upstream request failed";
|
|
@@ -1157,6 +1274,7 @@ export async function collectChatCompletion(
|
|
|
1157
1274
|
// Preserve the previous EOF contract: only delimiter-terminated events are collected.
|
|
1158
1275
|
// A partial final frame is discarded, with its retained input ownership released.
|
|
1159
1276
|
buffer.clear();
|
|
1277
|
+
collectorSource?.clear();
|
|
1160
1278
|
reader.releaseLock();
|
|
1161
1279
|
}
|
|
1162
1280
|
|
package/src/claude/outbound.ts
CHANGED
|
@@ -132,6 +132,53 @@ export function isThroughputLimitText(message: string): boolean {
|
|
|
132
132
|
return THROUGHPUT_LIMIT_TEXT_RE.test(message);
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
+
/** Codes that say nothing about the cause, so the message decides (#8127). */
|
|
136
|
+
const GENERIC_FAILURE_CODES: ReadonlySet<string> = new Set(["server_error", "upstream_error", "api_error", "internal_error"]);
|
|
137
|
+
const NON_OVERFLOW_ERROR_TYPES: ReadonlySet<string> = new Set(["rate_limit_error", "authentication_error", "permission_error"]);
|
|
138
|
+
const NON_OVERFLOW_STATUSES: ReadonlySet<number> = new Set([401, 403, 429]);
|
|
139
|
+
/**
|
|
140
|
+
* Wording that asserts the request itself is over a size limit. Narrower than
|
|
141
|
+
* CONTEXT_OVERFLOW_TEXT_RE on purpose: without a code, a message that only mentions the context
|
|
142
|
+
* window (for example a configuration failure or a validation error) must not make Claude Code
|
|
143
|
+
* compact. Quoted values are removed first, a stated maximum counts only when the stated request
|
|
144
|
+
* is larger, and a negated verdict ("does not exceed") never counts.
|
|
145
|
+
*/
|
|
146
|
+
const TOO_LONG_TEXT_RE = /\b(?:prompt|input) is too long\b([^.\n]{0,60})/i;
|
|
147
|
+
const CONDITIONAL_LEAD_RE = /\b(?:if|whether|unless)\b[^.\n]{0,40}$/i;
|
|
148
|
+
const STATED_COUNTS_RE = /maximum context length is (\d+)[^]{0,200}?\b(?:resulted in|you requested|requested) (\d+) tokens/i;
|
|
149
|
+
const EXCEEDS_TEXT_RE = /\b(?:input|prompt|request|messages?|conversation)\b[^.\n]{0,60}\bexceed(?:s|ed|ing)?\b[^.\n]{0,40}\b(?:context (?:window|length)|maximum context|token limit)/i;
|
|
150
|
+
const NEGATION_RE = /\b(?:not|never|no|neither|nor)\b|n['\u2019]t\b/i;
|
|
151
|
+
/** A quoted value inside a message (a field, enum or tool name), ASCII or typographic; an apostrophe inside a word does not open one. */
|
|
152
|
+
const QUOTED_SPAN_RE = /(?<!\w)(['"`])[^'"`\n]{1,200}\1(?!\w)|\u2018[^\u2019\n]{1,200}\u2019|\u201c[^\u201d\n]{1,200}\u201d/g;
|
|
153
|
+
|
|
154
|
+
function assertsOverflow(message: string): boolean {
|
|
155
|
+
const text = message.replace(QUOTED_SPAN_RE, " ");
|
|
156
|
+
const tooLong = TOO_LONG_TEXT_RE.exec(text);
|
|
157
|
+
if (tooLong && !NEGATION_RE.test(tooLong[1] ?? "") && !CONDITIONAL_LEAD_RE.test(text.slice(0, tooLong.index))) return true;
|
|
158
|
+
const counts = STATED_COUNTS_RE.exec(text);
|
|
159
|
+
if (counts && Number(counts[2]) > Number(counts[1])) return true;
|
|
160
|
+
const exceeds = EXCEEDS_TEXT_RE.exec(text);
|
|
161
|
+
return exceeds !== null && !NEGATION_RE.test(exceeds[0]);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Whether a translated refusal is an oversized-input refusal. A coded context_length_exceeded
|
|
166
|
+
* always is. Some Responses upstreams send the refusal uncoded or as a generic server_error
|
|
167
|
+
* ("Your input exceeds the context window of this model"); as a 502 Claude Code retries it
|
|
168
|
+
* instead of compacting (#8127). Those count only when the wording is an overflow, is not a
|
|
169
|
+
* throughput limit, and the type is not a rate-limit or credential refusal. Any other specific
|
|
170
|
+
* code keeps its own classification.
|
|
171
|
+
*/
|
|
172
|
+
export function isContextOverflowError(error: { type?: unknown; code?: unknown }, message: string): boolean {
|
|
173
|
+
const code = error.code;
|
|
174
|
+
if (code === "context_length_exceeded") return true;
|
|
175
|
+
if (code != null && !(typeof code === "string" && GENERIC_FAILURE_CODES.has(code))) return false;
|
|
176
|
+
if (typeof error.type === "string" && NON_OVERFLOW_ERROR_TYPES.has(error.type)) return false;
|
|
177
|
+
const status = (error as { status?: unknown }).status;
|
|
178
|
+
if (typeof status === "number" && NON_OVERFLOW_STATUSES.has(status)) return false;
|
|
179
|
+
return assertsOverflow(message) && !isThroughputLimitText(message);
|
|
180
|
+
}
|
|
181
|
+
|
|
135
182
|
export function anthropicErrorBody(status: number, message: string, type?: string, code?: string): Rec {
|
|
136
183
|
const overflow = code === "context_length_exceeded" && !isThroughputLimitText(message);
|
|
137
184
|
const text = overflow ? claudePromptTooLongMessage(message) : message;
|
|
@@ -333,7 +380,7 @@ export function anthropicIncompleteOutcome(
|
|
|
333
380
|
*/
|
|
334
381
|
export function anthropicFailedStatus(error: Rec, message: string): number {
|
|
335
382
|
const code = typeof error.code === "string" ? error.code : undefined;
|
|
336
|
-
if (
|
|
383
|
+
if (isContextOverflowError(error, message)) return 400;
|
|
337
384
|
return code === "translation_buffer_limit"
|
|
338
385
|
? 413
|
|
339
386
|
: typeof error.status === "number"
|
|
@@ -852,7 +899,7 @@ export function responsesSseToAnthropicSse(
|
|
|
852
899
|
status,
|
|
853
900
|
message,
|
|
854
901
|
!deterministic,
|
|
855
|
-
deterministic
|
|
902
|
+
deterministic ? code : isContextOverflowError(error, message) ? "context_length_exceeded" : undefined,
|
|
856
903
|
);
|
|
857
904
|
break;
|
|
858
905
|
}
|