@bitkyc08/opencodex 2.55.0-preview.20260914 → 2.56.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.
Files changed (167) hide show
  1. package/gui/dist/assets/{index-DH2PUHqr.js → index-D4zuyIxQ.js} +1 -1
  2. package/gui/dist/index.html +1 -1
  3. package/package.json +2 -1
  4. package/src/adapters/base.ts +21 -0
  5. package/src/adapters/cursor/transport-retry.ts +46 -1
  6. package/src/adapters/cursor.ts +4 -0
  7. package/src/adapters/kiro/adapter.ts +42 -1
  8. package/src/adapters/kiro-retry.ts +23 -4
  9. package/src/adapters/openai-chat/errors.ts +116 -0
  10. package/src/adapters/openai-chat/messages.ts +346 -0
  11. package/src/adapters/openai-chat/passthrough.ts +146 -0
  12. package/src/adapters/openai-chat/response-events.ts +117 -0
  13. package/src/adapters/openai-chat/tool-call-validation.ts +200 -0
  14. package/src/adapters/openai-chat/tool-schema.ts +477 -0
  15. package/src/adapters/openai-chat/wire.ts +50 -0
  16. package/src/adapters/openai-chat.ts +33 -1445
  17. package/src/adapters/openai-responses/canonical-forward.ts +202 -0
  18. package/src/adapters/openai-responses/image-gen.ts +406 -0
  19. package/src/adapters/openai-responses/internal.ts +3 -0
  20. package/src/adapters/openai-responses/passthrough.ts +611 -0
  21. package/src/adapters/openai-responses/prompt-cache.ts +83 -0
  22. package/src/adapters/openai-responses/reasoning.ts +220 -0
  23. package/src/adapters/openai-responses/request-strips.ts +185 -0
  24. package/src/adapters/openai-responses/tool-output-recovery.ts +509 -0
  25. package/src/adapters/openai-responses/tool-schema.ts +293 -0
  26. package/src/adapters/openai-responses/web-search.ts +156 -0
  27. package/src/adapters/openai-responses.ts +4 -2625
  28. package/src/bridge/errors.ts +34 -0
  29. package/src/bridge/internal.ts +174 -0
  30. package/src/bridge/response-json.ts +624 -0
  31. package/src/bridge/sse.ts +1444 -0
  32. package/src/bridge.ts +5 -2204
  33. package/src/chat/inbound.ts +12 -1
  34. package/src/codex/account-lifecycle.ts +3 -0
  35. package/src/codex/account-store.ts +71 -9
  36. package/src/codex/auth-api/account-list.ts +507 -0
  37. package/src/codex/auth-api/http.ts +32 -0
  38. package/src/codex/auth-api/login-flow.ts +554 -0
  39. package/src/codex/auth-api/login-state.ts +64 -0
  40. package/src/codex/auth-api/main-account-probe.ts +331 -0
  41. package/src/codex/auth-api/pool-mode-gate.ts +274 -0
  42. package/src/codex/auth-api/pool-quota-probe.ts +512 -0
  43. package/src/codex/auth-api/reset-credit-service.ts +422 -0
  44. package/src/codex/auth-api/routes.ts +425 -0
  45. package/src/codex/auth-api/runtime-config.ts +48 -0
  46. package/src/codex/auth-api.ts +27 -3118
  47. package/src/codex/auth-context.ts +95 -28
  48. package/src/codex/catalog/auto-review.ts +507 -0
  49. package/src/codex/catalog/build-entries.ts +981 -0
  50. package/src/codex/catalog/combo-member.ts +375 -0
  51. package/src/codex/catalog/derive-entry.ts +229 -0
  52. package/src/codex/catalog/effort.ts +0 -1
  53. package/src/codex/catalog/gated-native-warn.ts +63 -0
  54. package/src/codex/catalog/gather-capture.ts +533 -0
  55. package/src/codex/catalog/model-hints.ts +691 -0
  56. package/src/codex/catalog/model-visibility.ts +304 -0
  57. package/src/codex/catalog/provider-fetch.ts +52 -2942
  58. package/src/codex/catalog/provider-models.ts +685 -0
  59. package/src/codex/catalog/restore.ts +132 -0
  60. package/src/codex/catalog/retained-sync.ts +706 -0
  61. package/src/codex/catalog/routed-gather.ts +858 -0
  62. package/src/codex/catalog/subagent-roster.ts +176 -0
  63. package/src/codex/catalog/sync.ts +52 -2698
  64. package/src/codex/inject/config-toml.ts +563 -0
  65. package/src/codex/inject/remove.ts +192 -0
  66. package/src/codex/inject/restore.ts +540 -0
  67. package/src/codex/inject/routing-classify.ts +109 -0
  68. package/src/codex/inject/routing-target.ts +125 -0
  69. package/src/codex/inject.ts +81 -1436
  70. package/src/codex/lineage.ts +458 -0
  71. package/src/codex/pool-refresh-backoff.ts +152 -0
  72. package/src/codex/routing/active-account.ts +194 -0
  73. package/src/codex/routing/cooldown-math.ts +275 -0
  74. package/src/codex/routing/health-store.ts +402 -0
  75. package/src/codex/routing/probe-lease.ts +358 -0
  76. package/src/codex/routing/selection.ts +703 -0
  77. package/src/codex/routing/thread-affinity.ts +538 -0
  78. package/src/codex/routing.ts +353 -2234
  79. package/src/codex/shim-fingerprint.ts +223 -0
  80. package/src/codex/shim-inspect.ts +175 -0
  81. package/src/codex/shim-probe.ts +367 -0
  82. package/src/codex/shim-restore-lock.ts +169 -0
  83. package/src/codex/shim-state-file.ts +151 -0
  84. package/src/codex/shim-templates.ts +265 -0
  85. package/src/codex/shim.ts +48 -1268
  86. package/src/config/diagnostics.ts +705 -0
  87. package/src/config/feature-flags.ts +55 -0
  88. package/src/config/live-reconcile.ts +403 -0
  89. package/src/config/load-degrade.ts +880 -0
  90. package/src/config/mutation-lock.ts +244 -0
  91. package/src/config/openai-tier-backup.ts +268 -0
  92. package/src/config/persist-unlocked.ts +92 -0
  93. package/src/config/proxy-env.ts +188 -0
  94. package/src/config/salvage.ts +244 -0
  95. package/src/config/schema/config-schema.ts +640 -0
  96. package/src/config/schema/leaf-validators.ts +855 -0
  97. package/src/config/warn-memo.ts +28 -0
  98. package/src/config.ts +234 -4481
  99. package/src/generated/compatibility-version.json +539 -39
  100. package/src/lib/request-execution-budget.ts +69 -20
  101. package/src/lib/spend-reservation-ledger.ts +940 -0
  102. package/src/lib/upstream-retry.ts +55 -11
  103. package/src/lib/workflow-budget.ts +553 -30
  104. package/src/providers/quota/account-cache.ts +441 -0
  105. package/src/providers/quota/antigravity.ts +295 -0
  106. package/src/providers/quota/report-cache.ts +320 -0
  107. package/src/providers/quota/vendor-probes-key.ts +1243 -0
  108. package/src/providers/quota/vendor-probes-oauth.ts +590 -0
  109. package/src/providers/quota.ts +324 -3079
  110. package/src/providers/registry/entries-core.ts +1221 -0
  111. package/src/providers/registry/entries-extended.ts +1204 -0
  112. package/src/providers/registry/model-seeds.ts +908 -0
  113. package/src/providers/registry/types.ts +352 -0
  114. package/src/providers/registry.ts +24 -3536
  115. package/src/responses/continuation-ownership.ts +29 -0
  116. package/src/responses/state/replay-fingerprint.ts +80 -0
  117. package/src/responses/state/snapshot-codec.ts +104 -0
  118. package/src/responses/state/spill-failure.ts +118 -0
  119. package/src/responses/state/spill-queue.ts +665 -0
  120. package/src/responses/state/temp-recovery.ts +257 -0
  121. package/src/responses/state.ts +82 -1143
  122. package/src/routing/identity-domains.ts +449 -0
  123. package/src/routing/probe-lease.ts +511 -0
  124. package/src/server/index/bounded-request.ts +88 -0
  125. package/src/server/index/live-sideband.ts +565 -0
  126. package/src/server/index/serve-options.ts +1766 -0
  127. package/src/server/index/startup-warnings.ts +213 -0
  128. package/src/server/index/websocket-handler.ts +335 -0
  129. package/src/server/index.ts +40 -2547
  130. package/src/server/management/route-registry.ts +26 -23
  131. package/src/server/management/shared.ts +8 -5
  132. package/src/server/management/workflow-budget-routes.ts +133 -0
  133. package/src/server/management-api.ts +12 -0
  134. package/src/server/request-log-conversation.ts +9 -7
  135. package/src/server/request-log.ts +245 -1
  136. package/src/server/responses/account-change-state.ts +233 -0
  137. package/src/server/responses/adapter-continuation.ts +514 -0
  138. package/src/server/responses/adapter-delivery.ts +214 -0
  139. package/src/server/responses/adapter-dispatch.ts +971 -0
  140. package/src/server/responses/compact.ts +59 -4
  141. package/src/server/responses/completion-policy.ts +33 -0
  142. package/src/server/responses/core-auth.ts +527 -0
  143. package/src/server/responses/core-codex-account.ts +859 -0
  144. package/src/server/responses/core-combo-failure.ts +210 -0
  145. package/src/server/responses/core-combo.ts +707 -0
  146. package/src/server/responses/core-errors.ts +152 -0
  147. package/src/server/responses/core-lifetime.ts +95 -0
  148. package/src/server/responses/core-normalize.ts +350 -0
  149. package/src/server/responses/core-opaque-recovery.ts +380 -0
  150. package/src/server/responses/core-options.ts +159 -0
  151. package/src/server/responses/core-replay.ts +225 -0
  152. package/src/server/responses/core.ts +192 -8893
  153. package/src/server/responses/passthrough-delivery.ts +856 -0
  154. package/src/server/responses/passthrough-dispatch.ts +1476 -0
  155. package/src/server/responses/passthrough-execution.ts +54 -0
  156. package/src/server/responses/request-prepare.ts +970 -0
  157. package/src/server/responses/request-send-budget.ts +164 -0
  158. package/src/server/responses/request-sidecar-auth.ts +149 -0
  159. package/src/server/responses/request-transport.ts +744 -0
  160. package/src/server/responses/response-effects.ts +157 -0
  161. package/src/server/responses/run-turn-execution.ts +448 -0
  162. package/src/server/responses/sidecar-execution.ts +469 -0
  163. package/src/server/responses-image-gen-repair.ts +1 -1
  164. package/src/server/workflow-refusal.ts +84 -0
  165. package/src/types/config.ts +30 -0
  166. package/src/usage/log.ts +146 -0
  167. package/src/usage/summary.ts +171 -21
package/src/bridge.ts CHANGED
@@ -1,2206 +1,7 @@
1
- import type {
2
- AdapterEvent,
3
- OcxMessagePhase,
4
- OcxProviderContinuationState,
5
- OcxProviderOpaqueToolCallMetadata,
6
- OcxReasoningReplayScopeRef,
7
- OcxUsage,
8
- } from "./types";
9
- import { coerceIntegerToolArguments } from "./lib/tool-argument-integers";
10
- import {
11
- adapterFailureFromMessage,
12
- classifyError,
13
- cyberPolicyErrorType,
14
- CYBER_POLICY_ERROR_CODE,
15
- isCyberPolicyCode,
16
- type OcxErrorPayload,
17
- } from "./lib/errors";
18
- import { redactSecretString } from "./lib/redact";
19
- import { mayBecomePatchEnvelope, repairFreeformToolInput } from "./responses/apply-patch-envelope";
20
- import { encodeCompactionSummary } from "./responses/compaction";
21
- import { compileCodeModeHelperInput, resolveCodeModeHelperName } from "./responses/code-mode-helper-compat";
22
- import { isTruncatedStopReason, truncationReasonFor } from "./responses/truncated-stop-reason";
23
- import { encodeReasoningEnvelope, type ReasoningEnvelope } from "./responses/reasoning-envelope";
24
- import { rememberReasoningForCall } from "./responses/reasoning-replay-cache";
25
- import {
26
- rememberAndSerializeExtraContent,
27
- rememberExtraContentForReplay,
28
- awaitThoughtSignatureDurability,
29
- } from "./responses/thought-signature-replay";
30
- import { resolveStallTimeoutSec } from "./stall-timeout";
31
- import {
32
- createCitationMarkerFilter,
33
- stripCitationMarkers,
34
- type CitationMarkerFilter,
35
- } from "./responses/citation-markers";
36
- import { declaresCodeModeExec, normalizeDeclaredToolName } from "./types";
37
- import { usageDisplayTotalTokens } from "./usage/totals";
38
- import { appendSafeWebSearchSource, safeWebSearchSources } from "./web-search/sources";
39
- import {
40
- isTranslatorBudgetExceededError,
41
- releaseTranslatedEvent,
42
- createTranslatorBudget,
43
- type TranslatorBudget,
44
- type TranslatorBufferKind,
45
- } from "./lib/translator-budget";
46
-
47
- function uuid(): string {
48
- return crypto.randomUUID().replace(/-/g, "");
49
- }
50
-
51
- /** Test-only: bound the abandoned-owned-budget watchdog delay (null restores). */
52
- let ownedBudgetAbandonedMs = 10 * 60 * 1000;
53
- const OWNED_BUDGET_ABANDONED_DEFAULT_MS = ownedBudgetAbandonedMs;
54
- export function setOwnedBudgetAbandonedMsForTests(ms: number | null): void {
55
- ownedBudgetAbandonedMs = ms ?? OWNED_BUDGET_ABANDONED_DEFAULT_MS;
56
- }
57
-
58
- function sseEvent(name: string, data: Record<string, unknown>): string {
59
- return `event: ${name}\ndata: ${JSON.stringify(data)}\n\n`;
60
- }
61
-
62
- function isRecord(value: unknown): value is Record<string, unknown> {
63
- return value !== null && typeof value === "object" && !Array.isArray(value);
64
- }
65
-
66
- function responsesUsage(usage: OcxUsage | undefined): Record<string, unknown> {
67
- // input_tokens_details / output_tokens_details are ALWAYS emitted (zero defaults):
68
- // strict Responses clients deserialize them as required fields — grok-build's pinned
69
- // async-openai fork (rev 95b52ebd, response_usage.rs) has non-Option InputTokenDetails/
70
- // OutputTokenDetails, so omitting them turns a successful turn into a hard exit after
71
- // response.completed ("missing field `input_tokens_details`", verified live 2026-07-23).
72
- if (!usage) {
73
- return {
74
- input_tokens: 0,
75
- output_tokens: 0,
76
- total_tokens: 0,
77
- input_tokens_details: { cached_tokens: 0 },
78
- output_tokens_details: { reasoning_tokens: 0 },
79
- };
80
- }
81
- // inputTokens is already inclusive of cache read/write (types.ts convention). Stateful
82
- // providers may report an absolute active-context checkpoint separately from their
83
- // per-attempt usage. Split that checkpoint into input + output without adding output twice.
84
- const inputTokens = usage.contextTotalTokens !== undefined
85
- ? Math.max(0, usage.contextTotalTokens - usage.outputTokens)
86
- : usage.inputTokens;
87
- // openai/codex#41980 parity: unknown upstream usage fields (subscription metadata, future
88
- // counters) pass through the rebuild. Normalized values stay authoritative for the known
89
- // keys (they are derived from the same raw values, so this never disagrees with upstream).
90
- const raw: Record<string, unknown> = usage.rawUsage ?? {};
91
- // cache_write_tokens is a KNOWN key: it is emitted only from the validated normalized
92
- // value below, never copied through raw (an unknown-shaped value must not leak into the
93
- // normalized contract).
94
- const rawInputDetails = isRecord(raw.input_tokens_details)
95
- ? Object.fromEntries(Object.entries(raw.input_tokens_details as Record<string, unknown>)
96
- .filter(([key]) => key !== "cache_write_tokens"))
97
- : {} as Record<string, unknown>;
98
- const rawOutputDetails = isRecord(raw.output_tokens_details)
99
- ? raw.output_tokens_details as Record<string, unknown>
100
- : {} as Record<string, unknown>;
101
- const out: Record<string, unknown> = {
102
- ...Object.fromEntries(Object.entries(raw).filter(([key]) =>
103
- key !== "input_tokens" && key !== "output_tokens" && key !== "total_tokens"
104
- && key !== "input_tokens_details" && key !== "output_tokens_details")),
105
- input_tokens: inputTokens,
106
- output_tokens: usage.outputTokens,
107
- total_tokens: usage.contextTotalTokens !== undefined
108
- ? usage.contextTotalTokens
109
- : usageDisplayTotalTokens(usage) ?? inputTokens + usage.outputTokens,
110
- };
111
- // cached_tokens carries cache READS only, matching OpenAI semantics, and is always present
112
- // (zero default) for strict clients. Clamp to inputTokens so a provider's absolute
113
- // checkpoint can never report more cache reads than input.
114
- const inputDetails: Record<string, unknown> = {
115
- ...rawInputDetails,
116
- cached_tokens: Math.min(usage.cachedInputTokens ?? 0, inputTokens),
117
- };
118
- if (usage.cacheCreationInputTokens !== undefined) {
119
- const cacheRead = typeof inputDetails.cached_tokens === "number" ? inputDetails.cached_tokens : 0;
120
- inputDetails.cache_write_tokens = Math.min(
121
- usage.cacheCreationInputTokens,
122
- Math.max(0, inputTokens - cacheRead),
123
- );
124
- }
125
- out.input_tokens_details = inputDetails;
126
- out.output_tokens_details = { ...rawOutputDetails, reasoning_tokens: usage.reasoningOutputTokens ?? 0 };
127
- return out;
128
- }
129
-
130
- function responseError(status: number, type: string, message: string): OcxErrorPayload {
131
- return classifyError(status, type, message);
132
- }
133
-
134
- /**
135
- * Whether assembled function-call arguments are usable JSON.
136
- * An empty buffer is valid (no-arg tools send no deltas). Non-empty must parse —
137
- * once fragments have been streamed to the client they cannot be repaired the way
138
- * non-stream adapters degrade a bad payload to `{}`.
139
- */
140
- function toolCallArgumentsUsable(args: string): boolean {
141
- if (args.length === 0) return true;
142
- const trimmed = args.trim();
143
- if (!trimmed) return false;
144
- try {
145
- JSON.parse(args);
146
- return true;
147
- } catch {
148
- return false;
149
- }
150
- }
151
-
152
- function adapterFailureFromEvent(event: Extract<AdapterEvent, { type: "error" }>): { httpStatus: number; error: OcxErrorPayload } {
153
- const message = redactSecretString(event.message);
154
- if (event.status === undefined && event.errorType === undefined && event.code === undefined) {
155
- return adapterFailureFromMessage(message);
156
- }
157
- const fallback = adapterFailureFromMessage(message);
158
- let httpStatus = event.status ?? fallback.httpStatus;
159
- const error = classifyError(httpStatus, event.errorType ?? fallback.error.type, message);
160
- if (event.errorType !== undefined) error.type = event.errorType;
161
- if (event.code !== undefined) error.code = event.code;
162
- // Codex maps cyber_policy on HTTP 400 (body) or mid-stream code; never leave it as 502.
163
- if (isCyberPolicyCode(error.code) || isCyberPolicyCode(event.code)) {
164
- error.code = CYBER_POLICY_ERROR_CODE;
165
- error.type = cyberPolicyErrorType(event.errorType);
166
- httpStatus = 400;
167
- }
168
- return { httpStatus, error };
169
- }
170
-
171
1
  export { adapterFailureFromMessage } from "./lib/errors";
172
2
 
173
- /**
174
- * Build the native `WebSearchAction::Search` payload from the queries that ran.
175
- *
176
- * Every action carries BOTH keys: `{ query, queries }`, where `query` is the first
177
- * member. Empty → `{ query: "", queries: [""] }`.
178
- *
179
- * Carrying both is load-bearing in both directions. DeepSeek's native Responses parser
180
- * makes `queries` a required field, and Console Go's upstream validator makes `query` a
181
- * required field — so a replayed `web_search_call` carried in the history of every
182
- * subsequent turn fails deserialization with `missing field 'queries'` (#930) or 400s
183
- * with `missing required field 'query'` unless both keys are present. Carrying both keys
184
- * in every case satisfies both strict parsers; the trade-off is that a multi-query batch
185
- * loses the "<first> ..." ellipsis in codex-rs and shows the first query as the label.
186
- *
187
- * That trade is deliberate: a cosmetic label against a conversation that 400s on every
188
- * subsequent turn. Do not restore the old batch-omits-`query` shape to win the ellipsis
189
- * back — it reopens #3071.
190
- *
191
- * This fixes items created from here on. History recorded before it is repaired at the
192
- * replay boundary by `backfillWebSearchQueries()` in the Responses adapter.
193
- */
194
- function webSearchAction(queries: string[]): Record<string, unknown> {
195
- const first = queries[0] ?? "";
196
- return { type: "search", query: first, queries: queries.length > 0 ? queries : [first] };
197
- }
198
-
199
- interface OutputItem {
200
- type: string;
201
- id: string;
202
- [key: string]: unknown;
203
- }
204
-
205
- export type ResponsesTerminalStatus = "completed" | "failed" | "incomplete";
206
-
207
- /** Accumulates string fragments and their total byte length without concatenating. */
208
- interface StringChunks {
209
- chunks: string[];
210
- bytes: number;
211
- }
212
- const emptyChunks = (): StringChunks => ({ chunks: [], bytes: 0 });
213
- const joinChunks = (sc: StringChunks): string => sc.chunks.join("");
214
-
215
- export function bridgeToResponsesSSE(
216
- events: AsyncIterable<AdapterEvent>,
217
- modelId: string,
218
- toolNsMap?: Map<string, { namespace: string; name: string; freeform?: true }>,
219
- freeformToolNames?: Set<string>,
220
- toolSearchToolNames?: Set<string>,
221
- onCancel?: () => void,
222
- heartbeatMs = 2_000,
223
- options?: {
224
- responseId?: string;
225
- stallTimeoutSec?: number;
226
- hideThinkingSummary?: boolean;
227
- /**
228
- * Remote compaction v2 turn: accumulate all assistant text and, on done, emit ONE synthetic
229
- * `{type:"compaction", encrypted_content:"ocx1:"+base64(text)}` output item before
230
- * response.completed — codex-rs collect_compaction_output requires exactly one.
231
- */
232
- compaction?: boolean;
233
- /** One-shot: first non-empty text/thinking/raw-reasoning delta observed (WP4 TTFT). */
234
- onFirstOutput?: () => void;
235
- onTerminal?: (status: ResponsesTerminalStatus) => void;
236
- onCompletedResponse?: (response: Record<string, unknown>, providerState?: OcxProviderContinuationState) => void;
237
- /**
238
- * Raw adapter-reported usage at the terminal event, BEFORE wire normalization.
239
- * responsesUsage() always emits token-detail objects with zero defaults for strict
240
- * clients (grok-build), which makes the wire unusable as a provenance source: the
241
- * request log must not read synthetic zeros as measured cache/reasoning numbers
242
- * (cache_detail_missing would be silently suppressed). Callers set logCtx.usage
243
- * from this callback instead of re-parsing the bridged SSE.
244
- */
245
- onUsage?: (usage: OcxUsage | undefined) => void;
246
- /** Request-visible tool names. When present, an upstream call outside this set fails closed. */
247
- declaredToolNames?: ReadonlySet<string>;
248
- /** Declared parameter schema per tool name; repairs integral-float integer args (#1611). */
249
- toolParameterSchemas?: ReadonlyMap<string, Record<string, unknown>>;
250
- /**
251
- * Wire keep-alive shape. Codex-rs parses at the EVENT level (timeout(idle_timeout,
252
- * stream.next()) over an eventsource_stream), so an SSE comment line dispatches no event
253
- * and does NOT re-arm its idle timer — the keep-alive must be a typed frame the parser
254
- * ignores via its catch-all (110 RCA, 30_patch-direction.md). grok-build's strict
255
- * async-openai fork is the opposite: it dies on the unknown `response.heartbeat`
256
- * variant but, being eventsource-based at the byte level, its idle handling tolerates
257
- * comment lines. Default stays the typed frame; the grok surface opts into comments.
258
- */
259
- heartbeatStyle?: "typed" | "comment";
260
- translatorBudget?: TranslatorBudget;
261
- /**
262
- * Conversation identity for the reasoning replay cache (issue #950).
263
- * Provider call ids are not globally unique; scoping by thread keeps one
264
- * conversation's reasoning out of another's continuations.
265
- */
266
- replayCacheScope?: OcxReasoningReplayScopeRef;
267
- /**
268
- * Test seam for the wire/stall beat loop. Production omits this and uses the
269
- * global timers; injecting here must not change scheduling semantics.
270
- */
271
- timers?: {
272
- setInterval: (handler: () => void, ms: number) => unknown;
273
- clearInterval: (id: unknown) => void;
274
- };
275
- },
276
- ): ReadableStream<Uint8Array> {
277
- const replayCacheScope = options?.replayCacheScope;
278
- const setBeatInterval = options?.timers?.setInterval ?? ((handler: () => void, ms: number) => setInterval(handler, ms));
279
- const clearBeatInterval = options?.timers?.clearInterval ?? ((id: unknown) => clearInterval(id as ReturnType<typeof setInterval>));
280
- // Freeform/custom tools (apply_patch, code-mode exec) carry their body in `input`; the
281
- // model is given a function with `{input:string}`, so unwrap it here when relaying back
282
- // as a custom_tool_call. Decorated apply_patch envelopes are repaired at this boundary.
283
- const freeformInput = (
284
- args: string,
285
- toolName: string,
286
- namespace?: string,
287
- codeModeHelperName?: string,
288
- ): string => {
289
- const helper = resolveCodeModeHelperName(codeModeHelperName, toolName, args, namespace, options?.declaredToolNames);
290
- return helper
291
- ? compileCodeModeHelperInput(args, helper)
292
- : repairFreeformToolInput(args, toolName, namespace);
293
- };
294
- // Best-effort unwrap of a PARTIAL freeform arg buffer for live input streaming
295
- // (`response.custom_tool_call_input.delta` — codex-rs uses it for UI preview only;
296
- // the completed custom_tool_call item stays authoritative). Compact `{"input":"...`
297
- // buffers get their string value progressively unescaped; anything else streams raw.
298
- const FREEFORM_WRAP_PREFIX = '{"input":"';
299
- const freeformPartialInput = (args: string): string => {
300
- if (!args.startsWith(FREEFORM_WRAP_PREFIX)) return args;
301
- const body = args.slice(FREEFORM_WRAP_PREFIX.length);
302
- let out = "";
303
- for (let i = 0; i < body.length; i++) {
304
- const c = body[i];
305
- if (c === '"') break; // unescaped closing quote: value complete
306
- if (c === "\\") {
307
- const n = body[i + 1];
308
- if (n === undefined) break; // escape split across chunks: wait for more
309
- i++;
310
- if (n === "n") out += "\n";
311
- else if (n === "t") out += "\t";
312
- else if (n === "r") out += "\r";
313
- else if (n === "u") {
314
- const hex = body.slice(i + 1, i + 5);
315
- if (hex.length === 4 && /^[0-9a-fA-F]{4}$/.test(hex)) { out += String.fromCharCode(parseInt(hex, 16)); i += 4; }
316
- else break; // incomplete \uXXXX: wait for more
317
- } else out += n; // \" \\ \/ etc.
318
- } else out += c;
319
- }
320
- return out;
321
- };
322
- // tool_search_call carries arguments as a JSON object ({query, limit}); parse the model's arg string.
323
- const parseArgsObj = (args: string): Record<string, unknown> => {
324
- try { const o = JSON.parse(args); return o && typeof o === "object" ? o : {}; } catch { return {}; }
325
- };
326
- const encoder = new TextEncoder();
327
- // Default-budget safety net: omission is SAFE (default turn limits), never
328
- // unbounded. Production callers always pass one; an owned default is disposed
329
- // at terminal/cancel below.
330
- const ownsBudget = !options?.translatorBudget;
331
- const budget = options?.translatorBudget ?? createTranslatorBudget();
332
- // Idempotent: safe to call at every stream-death path; disposal must come
333
- // AFTER the final charges (emitDone), never inside reportTerminal.
334
- const disposeOwnedBudget = () => { if (ownsBudget) budget.dispose(); };
335
- // A dropped stream (never read, never cancelled) reaches no terminal path,
336
- // so the owned budget would sit in liveBudgets for the process lifetime.
337
- // One unref'd watchdog per owned budget bounds that to a timeout and clears
338
- // itself on any settle (the delay is test-overridable).
339
- const ownedWatchdog = ownsBudget
340
- ? setTimeout(() => disposeOwnedBudget(), ownedBudgetAbandonedMs)
341
- : undefined;
342
- ownedWatchdog?.unref?.();
343
- const clearOwnedWatchdog = () => {
344
- if (ownedWatchdog !== undefined) clearTimeout(ownedWatchdog);
345
- };
346
- const bytesOf = (value: string): number => Buffer.byteLength(value);
347
- const appendString = (
348
- previous: StringChunks,
349
- fragment: string,
350
- kind: TranslatorBufferKind,
351
- callId?: string,
352
- ): StringChunks => {
353
- const fragmentBytes = bytesOf(fragment);
354
- if (fragmentBytes === 0) return previous;
355
- const nextBytes = previous.bytes + fragmentBytes;
356
- const scope = { kind, ...(callId ? { callId } : {}) };
357
- const reservation = budget.reserveTransient(nextBytes, scope);
358
- try {
359
- previous.chunks.push(fragment);
360
- const result: StringChunks = { chunks: previous.chunks, bytes: nextBytes };
361
- reservation.commitRetained();
362
- budget.releaseRetained(previous.bytes, scope);
363
- return result;
364
- } catch (error) {
365
- reservation.release();
366
- throw error;
367
- }
368
- };
369
- // Tool-call arguments deliberately use plain string concatenation because
370
- // downstream parsers and intermediate inspectors perform incremental JSON reads mid-stream.
371
- // Converting tool args to StringChunks would require frequent join operations.
372
- const appendStringDirect = (
373
- previous: string,
374
- previousBytes: number,
375
- fragment: string,
376
- kind: TranslatorBufferKind,
377
- callId?: string,
378
- ): { value: string; bytes: number } => {
379
- const fragmentBytes = bytesOf(fragment);
380
- const nextBytes = previousBytes + fragmentBytes;
381
- const scope = { kind, ...(callId ? { callId } : {}) };
382
- const reservation = budget.reserveTransient(nextBytes, scope);
383
- try {
384
- const value = previous + fragment;
385
- reservation.commitRetained();
386
- budget.releaseRetained(previousBytes, scope);
387
- return { value, bytes: nextBytes };
388
- } catch (error) {
389
- reservation.release();
390
- throw error;
391
- }
392
- };
393
- const replaceRetainedString = (previousBytes: number, next: string, kind: TranslatorBufferKind): number => {
394
- const nextBytes = bytesOf(next);
395
- if (!budget) return nextBytes;
396
- const reservation = budget.reserveTransient(nextBytes, { kind });
397
- reservation.commitRetained();
398
- budget.releaseRetained(previousBytes, { kind });
399
- return nextBytes;
400
- };
401
- const chargeValue = (value: unknown, kind: TranslatorBufferKind): number => {
402
- const bytes = bytesOf(JSON.stringify(value));
403
- budget?.chargeRetained(bytes, { kind });
404
- return bytes;
405
- };
406
- const responseId = options?.responseId ?? `resp_${uuid()}`;
407
- let seq = 0;
408
- // Set once the client is gone (cancel) or an enqueue throws on a torn-down controller, so we
409
- // never enqueue again and never throw a second time inside start() — the RC2 double-throw that
410
- // otherwise surfaced as proxy-side stream noise on every client disconnect.
411
- let closed = false;
412
- let clientCancelled = false;
413
- let terminalReported = false;
414
- const reportTerminal = (status: ResponsesTerminalStatus) => {
415
- if (terminalReported || clientCancelled || closed) return;
416
- terminalReported = true;
417
- try { options?.onTerminal?.(status); } catch { /* terminal metrics must not break the stream */ }
418
- clearOwnedWatchdog();
419
- };
420
- // RC3 keep-alive: Codex's idle timer is timeout(idle_timeout, stream.next()) over an
421
- // eventsource_stream, which parses at the EVENT level — a comment-only frame dispatches no
422
- // event, so it does NOT re-arm the timer (110 RCA). The default keep-alive is therefore a
423
- // typed `response.heartbeat` frame the codex-rs parser ignores via `_ => Ok(None)`. The
424
- // grok surface (strict async-openai decoder that dies on unknown variants) opts into SSE
425
- // comment lines instead via options.heartbeatStyle. Emit whenever the *wire* has been
426
- // silent, even if invisible adapter heartbeats are still flowing (web-search buffering +
427
- // raw-byte progress). Upstream activity only resets the stall watchdog.
428
- let upstreamActivity = false;
429
- let wireActivity = false;
430
- let beat: unknown;
431
- let controller: ReadableStreamDefaultController<Uint8Array>;
432
- let emittedFrames = 0;
433
- let gated = false;
434
- let stepping = false;
435
- let terminateForTranslatorOverflow: ((error: unknown) => void) | undefined;
436
- const emit = (name: string, data: Record<string, unknown>) => {
437
- if (closed) return;
438
- wireActivity = true;
439
- try {
440
- const frameText = sseEvent(name, { type: name, sequence_number: seq++, ...data });
441
- const frameBytes = bytesOf(frameText);
442
- const reservation = budget?.reserveTransient(frameBytes, { kind: "live_transient" });
443
- const frame = encoder.encode(frameText);
444
- reservation?.commitRetained();
445
- controller.enqueue(frame);
446
- budget?.releaseRetained(frameBytes, { kind: "live_transient" });
447
- emittedFrames++;
448
- } catch (error) {
449
- if (isTranslatorBudgetExceededError(error)) {
450
- terminateForTranslatorOverflow?.(error);
451
- return;
452
- }
453
- closed = true;
454
- disposeOwnedBudget();
455
- }
456
- };
457
- const emitDone = () => {
458
- if (closed) return;
459
- try {
460
- const done = "data: [DONE]\n\n";
461
- const doneBytes = bytesOf(done);
462
- const reservation = budget?.reserveTransient(doneBytes, { kind: "live_transient" });
463
- const frame = encoder.encode(done);
464
- reservation?.commitRetained();
465
- controller.enqueue(frame);
466
- budget?.releaseRetained(doneBytes, { kind: "live_transient" });
467
- emittedFrames++;
468
- } catch (error) {
469
- if (isTranslatorBudgetExceededError(error)) {
470
- terminateForTranslatorOverflow?.(error);
471
- return;
472
- }
473
- closed = true;
474
- }
475
- };
476
-
477
- const createdAt = Math.floor(Date.now() / 1000);
478
- let outputIndex = 0;
479
- const finishedItems: OutputItem[] = [];
480
- const retainFinishedItem = (item: OutputItem, replacedBytes = 0, kind: TranslatorBufferKind = "retained_collectors") => {
481
- const itemBytes = bytesOf(JSON.stringify(item));
482
- const reservation = budget?.reserveTransient(itemBytes, { kind });
483
- finishedItems.push(item);
484
- reservation?.commitRetained();
485
- if (replacedBytes > 0) budget?.releaseRetained(replacedBytes, { kind });
486
- };
487
-
488
- const responseSnapshot = (status: string, output: OutputItem[], endTurn?: boolean) => ({
489
- id: responseId, object: "response", created_at: createdAt,
490
- status, model: modelId, output, usage: null,
491
- ...(endTurn !== undefined ? { end_turn: endTurn } : {}),
492
- });
493
-
494
- const heartbeatFrame = options?.heartbeatStyle === "comment"
495
- ? encoder.encode(': opencodex heartbeat\n\n')
496
- : encoder.encode('event: response.heartbeat\ndata: {"type":"response.heartbeat"}\n\n');
497
- let stallTicks = 0;
498
- const stallSec = resolveStallTimeoutSec(options?.stallTimeoutSec);
499
- const maxStallTicks = Math.ceil((stallSec * 1000) / heartbeatMs);
500
-
501
- let currentMsg: {
502
- itemId: string;
503
- outputIndex: number;
504
- text: StringChunks;
505
- citationFilter: CitationMarkerFilter;
506
- phase?: OcxMessagePhase;
507
- } | null = null;
508
- let currentReasoning: { itemId: string; outputIndex: number; text: StringChunks } | null = null;
509
- let currentRawReasoning: { itemId: string; outputIndex: number; text: StringChunks } | null = null;
510
- // Anthropic extended-thinking round-trip state: the signature signs the CURRENT thinking
511
- // block; redacted blocks are opaque payloads replayed verbatim. Attached to the reasoning
512
- // item as an ocxr1 encrypted_content envelope on close. hiddenThinkingText collects the
513
- // suppressed text under hideThinkingSummary so the signed text still round-trips.
514
- let pendingSignature: string | undefined;
515
- let pendingSignatureBytes = 0;
516
- let pendingRedacted: string[] = [];
517
- let hiddenThinking = emptyChunks();
518
- const takeReasoningEnvelope = (hiddenText?: string): string | undefined => {
519
- if (!pendingSignature && pendingRedacted.length === 0) return undefined;
520
- const envelope: ReasoningEnvelope = {};
521
- if (pendingSignature) envelope.sig = pendingSignature;
522
- if (pendingRedacted.length > 0) envelope.red = pendingRedacted;
523
- if (hiddenText) envelope.txt = hiddenText;
524
- const previousBytes = pendingSignatureBytes
525
- + pendingRedacted.reduce((sum, value) => sum + bytesOf(value), 0)
526
- + (hiddenText ? hiddenThinking.bytes : 0);
527
- const encoded = encodeReasoningEnvelope(envelope, budget);
528
- const reservation = budget?.reserveTransient(bytesOf(encoded), { kind: "reasoning" });
529
- pendingSignature = undefined;
530
- pendingSignatureBytes = 0;
531
- pendingRedacted = [];
532
- reservation?.commitRetained();
533
- budget?.releaseRetained(previousBytes, { kind: "reasoning" });
534
- return encoded;
535
- };
536
- // hideThinkingSummary path: no visible reasoning item exists, but a signed thinking block
537
- // must still round-trip — emit an envelope-only reasoning item (empty summary, no text leak).
538
- const flushHiddenReasoningEnvelope = () => {
539
- const hiddenText = joinChunks(hiddenThinking);
540
- const encrypted = takeReasoningEnvelope(hiddenText || undefined);
541
- hiddenThinking = emptyChunks();
542
- if (!encrypted) return;
543
- const itemId = `rs_${uuid()}`;
544
- const item = { type: "reasoning", id: itemId, summary: [] as never[], encrypted_content: encrypted };
545
- emit("response.output_item.added", { output_index: outputIndex, item });
546
- emit("response.output_item.done", { output_index: outputIndex, item });
547
- retainFinishedItem(item as OutputItem, bytesOf(encrypted), "reasoning");
548
- outputIndex++;
549
- };
550
- // hideThinkingSummary for RAW reasoning (openai-chat reasoning_content, kiro tags): no
551
- // visible reasoning item is emitted — the app renders nothing, so tool cells keep grouping
552
- // like native models — but the text still round-trips in a txt-only ocxr1 envelope so
553
- // preserveReasoningContentModels replay (GLM interleaved thinking) keeps working. Direct
554
- // encodeReasoningEnvelope: takeReasoningEnvelope's sig/red guard would drop txt-only.
555
- let hiddenRawReasoning = emptyChunks();
556
- // Raw reasoning text flushed most recently, waiting for the tool call it
557
- // preceded. Recorded into the replay cache on tool_call_start so a later
558
- // continuation can re-attach it when history lost the reasoning item
559
- // (issue #950). Kept until new reasoning/text arrives: parallel tool
560
- // calls share the same preceding reasoning block.
561
- let rawReasoningForNextToolCall = "";
562
- const flushHiddenRawReasoning = () => {
563
- const hiddenRawText = joinChunks(hiddenRawReasoning);
564
- if (!hiddenRawText) return;
565
- rawReasoningForNextToolCall = hiddenRawText;
566
- const previousBytes = hiddenRawReasoning.bytes;
567
- const encrypted = encodeReasoningEnvelope({ txt: hiddenRawText }, budget);
568
- const reservation = budget?.reserveTransient(bytesOf(encrypted), { kind: "reasoning" });
569
- hiddenRawReasoning = emptyChunks();
570
- reservation?.commitRetained();
571
- budget?.releaseRetained(previousBytes, { kind: "reasoning" });
572
- const itemId = `rs_${uuid()}`;
573
- const item = { type: "reasoning", id: itemId, summary: [] as never[], encrypted_content: encrypted };
574
- emit("response.output_item.added", { output_index: outputIndex, item });
575
- emit("response.output_item.done", { output_index: outputIndex, item });
576
- retainFinishedItem(item as OutputItem, bytesOf(encrypted), "reasoning");
577
- outputIndex++;
578
- };
579
- // Kiro reasoning round-trip. Kiro sends its encrypted blob at the END of a turn, while the
580
- // assistant message is still open, so this CANNOT emit on arrival: the open message still
581
- // owns `outputIndex` (it only advances on close), and an item emitted here would both reuse
582
- // that index and land BEFORE the message — where the parser's backwards pairing drops it as
583
- // orphaned. Stash it and flush after `done` has closed every open item instead.
584
- let pendingKiroRedacted: string | undefined;
585
- let pendingKiroRedactedBytes = 0;
586
- const flushKiroRedactedReasoning = () => {
587
- if (!pendingKiroRedacted) return;
588
- const previousBytes = pendingKiroRedactedBytes;
589
- const encrypted = encodeReasoningEnvelope({ krc: pendingKiroRedacted }, budget);
590
- const reservation = budget?.reserveTransient(bytesOf(encrypted), { kind: "reasoning" });
591
- pendingKiroRedacted = undefined;
592
- pendingKiroRedactedBytes = 0;
593
- reservation?.commitRetained();
594
- budget?.releaseRetained(previousBytes, { kind: "reasoning" });
595
- const itemId = `rs_${uuid()}`;
596
- const item = { type: "reasoning", id: itemId, summary: [] as never[], encrypted_content: encrypted };
597
- emit("response.output_item.added", { output_index: outputIndex, item });
598
- emit("response.output_item.done", { output_index: outputIndex, item });
599
- retainFinishedItem(item as OutputItem, bytesOf(encrypted), "reasoning");
600
- outputIndex++;
601
- };
602
- // Full assistant text of a compaction turn (across message boundaries) — becomes the
603
- // synthetic compaction item's payload on done.
604
- let compaction = emptyChunks();
605
- 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;
606
- // Open native web-search cell (between begin and end). Holds the output index allocated on
607
- // begin so the matching done reuses it; closed as `failed` if the stream terminates early.
608
- let currentWebSearch: { itemId: string; eventId: string; outputIndex: number } | null = null;
609
- // Sources from completed web searches, awaiting the next assistant message. Attached as
610
- // url_citation annotations on that message (the desktop app's Sources chip), then cleared so
611
- // they bind to exactly one message. Deduped by URL across multiple searches in the turn.
612
- let pendingWebSources: { url: string; title?: string }[] = [];
613
- let pendingWebSourceBytes = 0;
614
- const releasePendingWebSources = () => {
615
- if (pendingWebSources.length === 0) return;
616
- pendingWebSources = [];
617
- budget?.releaseRetained(pendingWebSourceBytes, { kind: "tool_search_sources" });
618
- pendingWebSourceBytes = 0;
619
- };
620
- const takeWebAnnotations = (): { type: string; url: string; title?: string; start_index: number; end_index: number }[] => {
621
- if (pendingWebSources.length === 0) return [];
622
- const anns = pendingWebSources.map(s => ({
623
- type: "url_citation", url: s.url, ...(s.title ? { title: s.title } : {}), start_index: 0, end_index: 0,
624
- }));
625
- const annotationBytes = bytesOf(JSON.stringify(anns));
626
- const reservation = budget?.reserveTransient(annotationBytes, { kind: "retained_collectors" });
627
- reservation?.commitRetained();
628
- releasePendingWebSources();
629
- return anns;
630
- };
631
-
632
- const closeCurrentMessage = (inferredPhase?: OcxMessagePhase) => {
633
- if (!currentMsg) return;
634
- // Release anything the citation filter was holding for this message, then strip the
635
- // accumulated text: closeCurrentMessage re-sends it in output_text.done and
636
- // output_item.done, so filtering only the deltas would leave the markers in both.
637
- const trailing = currentMsg.citationFilter.flush();
638
- if (trailing) {
639
- emit("response.output_text.delta", {
640
- item_id: currentMsg.itemId, output_index: currentMsg.outputIndex,
641
- content_index: 0, delta: trailing,
642
- });
643
- }
644
- const messageText = stripCitationMarkers(joinChunks(currentMsg.text));
645
- // Chat Completions has no message-phase field. Keep its live item provisional, then
646
- // classify it only when the next adapter event proves whether this text led into more
647
- // work or completed the turn. Explicit adapter phases always outrank this inference.
648
- const phase = currentMsg.phase ?? inferredPhase;
649
- // Bind any pending web-search citations to this assistant message (then they clear).
650
- const annotations = takeWebAnnotations();
651
- // Finalize the text part (Responses protocol). Without these .done events Codex never
652
- // commits the content part and renders the message as truncated / cut off.
653
- emit("response.output_text.done", {
654
- item_id: currentMsg.itemId, output_index: currentMsg.outputIndex, content_index: 0, text: messageText,
655
- });
656
- emit("response.content_part.done", {
657
- item_id: currentMsg.itemId, output_index: currentMsg.outputIndex, content_index: 0,
658
- part: { type: "output_text", text: messageText, annotations },
659
- });
660
- const item = {
661
- type: "message", id: currentMsg.itemId, status: "completed", role: "assistant",
662
- content: [{ type: "output_text", text: messageText, annotations }],
663
- ...(phase ? { phase } : {}),
664
- };
665
- emit("response.output_item.done", { output_index: currentMsg.outputIndex, item });
666
- retainFinishedItem(item as OutputItem, currentMsg.text.bytes + bytesOf(JSON.stringify(annotations)));
667
- outputIndex++;
668
- currentMsg = null;
669
- };
670
-
671
- const closeCurrentReasoning = () => {
672
- if (!currentReasoning) return;
673
- const reasoningText = joinChunks(currentReasoning.text);
674
- emit("response.reasoning_summary_text.done", {
675
- item_id: currentReasoning.itemId, output_index: currentReasoning.outputIndex, summary_index: 0, text: reasoningText,
676
- });
677
- emit("response.reasoning_summary_part.done", {
678
- item_id: currentReasoning.itemId, output_index: currentReasoning.outputIndex, summary_index: 0,
679
- part: { type: "summary_text", text: reasoningText },
680
- });
681
- const encrypted = takeReasoningEnvelope();
682
- const item = {
683
- type: "reasoning", id: currentReasoning.itemId,
684
- summary: [{ type: "summary_text", text: reasoningText }],
685
- ...(encrypted ? { encrypted_content: encrypted } : {}),
686
- };
687
- emit("response.output_item.done", { output_index: currentReasoning.outputIndex, item });
688
- retainFinishedItem(item as OutputItem, currentReasoning.text.bytes + bytesOf(encrypted ?? ""), "reasoning");
689
- outputIndex++;
690
- currentReasoning = null;
691
- };
692
-
693
- const closeCurrentRawReasoning = () => {
694
- if (!currentRawReasoning) return;
695
- const rawText = joinChunks(currentRawReasoning.text);
696
- rawReasoningForNextToolCall = rawText;
697
- emit("response.reasoning_text.done", {
698
- item_id: currentRawReasoning.itemId, output_index: currentRawReasoning.outputIndex, content_index: 0, text: rawText,
699
- });
700
- const item = {
701
- type: "reasoning", id: currentRawReasoning.itemId,
702
- summary: [] as never[],
703
- content: [{ type: "reasoning_text", text: rawText }],
704
- };
705
- emit("response.output_item.done", { output_index: currentRawReasoning.outputIndex, item });
706
- retainFinishedItem(item as OutputItem, currentRawReasoning.text.bytes, "reasoning");
707
- outputIndex++;
708
- currentRawReasoning = null;
709
- };
710
-
711
- const closeCurrentToolCall = () => {
712
- if (!currentToolCall) return;
713
- // Empty input (no-arg tools like computer_use get_app_state / list_apps) must serialize as
714
- // "{}", never "" — Codex echoes the call back as a function_call next turn, and JSON.parse("")
715
- // would 400 the whole session ("invalid JSON arguments"), poisoning all later turns.
716
- // #1611: Grok serializes integer arguments through a float, so `120000.0`
717
- // reaches Codex and is REJECTED before the tool runs. Repair integral floats
718
- // against the declared schema; a non-integral value stays an error.
719
- const argsStr = coerceIntegerToolArguments(
720
- currentToolCall.args || "{}",
721
- options?.toolParameterSchemas?.get(currentToolCall.name),
722
- currentToolCall.namespace === undefined ? currentToolCall.name : undefined,
723
- );
724
- // Finalize streamed function-call arguments so Codex commits the call (incl. MCP / computer_use).
725
- if (!currentToolCall.freeform && !currentToolCall.toolSearch) {
726
- emit("response.function_call_arguments.done", {
727
- item_id: currentToolCall.itemId, output_index: currentToolCall.outputIndex, arguments: argsStr,
728
- });
729
- }
730
- if (currentToolCall.freeform) {
731
- emit("response.custom_tool_call_input.done", {
732
- item_id: currentToolCall.itemId, output_index: currentToolCall.outputIndex,
733
- ...(currentToolCall.namespace ? { namespace: currentToolCall.namespace } : {}),
734
- input: freeformInput(currentToolCall.args, currentToolCall.name, currentToolCall.namespace, currentToolCall.codeModeHelperName),
735
- });
736
- }
737
- // Freeform tools serialize as custom_tool_call without extra_content; remember the
738
- // signature server-side regardless so the replayed call can be re-signed (#1735).
739
- void rememberExtraContentForReplay(currentToolCall.callId, currentToolCall.providerMetadata, replayCacheScope);
740
- const item = currentToolCall.toolSearch
741
- ? {
742
- type: "tool_search_call", id: currentToolCall.itemId,
743
- call_id: currentToolCall.callId, execution: "client",
744
- arguments: parseArgsObj(currentToolCall.args), status: "completed",
745
- }
746
- : currentToolCall.freeform
747
- ? {
748
- type: "custom_tool_call", id: currentToolCall.itemId,
749
- call_id: currentToolCall.callId, name: currentToolCall.name,
750
- ...(currentToolCall.namespace ? { namespace: currentToolCall.namespace } : {}),
751
- input: freeformInput(currentToolCall.args, currentToolCall.name, currentToolCall.namespace, currentToolCall.codeModeHelperName), status: "completed",
752
- }
753
- : {
754
- type: "function_call", id: currentToolCall.itemId,
755
- call_id: currentToolCall.callId, name: currentToolCall.name,
756
- arguments: argsStr, status: "completed",
757
- ...(currentToolCall.namespace ? { namespace: currentToolCall.namespace } : {}),
758
- // Provider-opaque metadata (issue #1735) rides the item so a client that replays
759
- // this history can hand the signature back on the part it belongs to. The proxy
760
- // also remembers it server-side for clients that never echo extra_content.
761
- ...(rememberAndSerializeExtraContent(currentToolCall.callId, currentToolCall.providerMetadata, replayCacheScope).extra ?? {}),
762
- };
763
- emit("response.output_item.done", { output_index: currentToolCall.outputIndex, item });
764
- retainFinishedItem(item as OutputItem);
765
- budget?.closeCall(currentToolCall.callId);
766
- outputIndex++;
767
- currentToolCall = null;
768
- };
769
-
770
- // Terminal-error / incomplete path for an open tool call (#765 remainder).
771
- // Closing via closeCurrentToolCall() would emit function_call_arguments.done and
772
- // status:"completed" BEFORE response.failed — the client still sees an issued call.
773
- // Cancel instead: no *.done argument frames, status:"incomplete" (same pattern as an
774
- // in-flight web_search_call closing as "failed"). Args still serialize as "{}" when
775
- // empty so echoed items cannot poison the next turn with JSON.parse("").
776
- const failCurrentToolCall = () => {
777
- if (!currentToolCall) return;
778
- const argsStr = currentToolCall.args || "{}";
779
- void rememberExtraContentForReplay(currentToolCall.callId, currentToolCall.providerMetadata, replayCacheScope);
780
- const item = currentToolCall.toolSearch
781
- ? {
782
- type: "tool_search_call", id: currentToolCall.itemId,
783
- call_id: currentToolCall.callId, execution: "client",
784
- arguments: parseArgsObj(currentToolCall.args), status: "incomplete",
785
- }
786
- : currentToolCall.freeform
787
- ? {
788
- type: "custom_tool_call", id: currentToolCall.itemId,
789
- call_id: currentToolCall.callId, name: currentToolCall.name,
790
- ...(currentToolCall.namespace ? { namespace: currentToolCall.namespace } : {}),
791
- input: freeformInput(currentToolCall.args, currentToolCall.name, currentToolCall.namespace, currentToolCall.codeModeHelperName), status: "incomplete",
792
- }
793
- : {
794
- type: "function_call", id: currentToolCall.itemId,
795
- call_id: currentToolCall.callId, name: currentToolCall.name,
796
- arguments: argsStr, status: "incomplete",
797
- ...(currentToolCall.namespace ? { namespace: currentToolCall.namespace } : {}),
798
- // An incomplete call can still be persisted and replayed (max_output_tokens), so it
799
- // carries the same metadata as the completed item — otherwise SSE and buffered JSON
800
- // would disagree about whether the signature survives.
801
- ...(rememberAndSerializeExtraContent(currentToolCall.callId, currentToolCall.providerMetadata, replayCacheScope).extra ?? {}),
802
- };
803
- emit("response.output_item.done", { output_index: currentToolCall.outputIndex, item });
804
- retainFinishedItem(item as OutputItem);
805
- budget?.closeCall(currentToolCall.callId);
806
- outputIndex++;
807
- currentToolCall = null;
808
- };
809
-
810
- const abortCurrentToolCallForTranslatorOverflow = () => {
811
- if (!currentToolCall) return;
812
- budget?.closeCall(currentToolCall.callId);
813
- currentToolCall = null;
814
- };
815
-
816
- // Finalize an open web-search cell. `status` is "completed" on a normal end, or "failed" when
817
- // the stream terminates (error/incomplete) while a search was still in flight, so Codex never
818
- // leaves a "Searching the web" spinner spinning forever.
819
- // `sources` rides on the done item (additive field; codex-rs serde ignores unknown fields) so
820
- // downstream translators (claude outbound) can fill web_search_tool_result content.
821
- const closeCurrentWebSearch = (status: "completed" | "failed", queries: string[], sources?: { url: string; title?: string }[]) => {
822
- if (!currentWebSearch) return;
823
- const item = {
824
- type: "web_search_call", id: currentWebSearch.itemId, status,
825
- action: webSearchAction(queries),
826
- ...(sources && sources.length > 0 ? { sources } : {}),
827
- };
828
- emit("response.output_item.done", { output_index: currentWebSearch.outputIndex, item });
829
- retainFinishedItem(item as OutputItem);
830
- outputIndex++;
831
- currentWebSearch = null;
832
- };
833
-
834
- // RC1: guarantee the Responses stream always ends with exactly one terminal event. Set true
835
- // when a done/error/catch terminal is emitted; if the adapter generator returns without one
836
- // we synthesize a terminal below, so Codex never hits the parser's
837
- // "stream closed before response.completed" (responses.rs) -> ApiError::Stream.
838
- // That synthesized terminal is response.incomplete with reason "adapter_eof", NOT
839
- // response.completed: a generator that returns without a terminal event is a truncated
840
- // stream, and reporting it as a clean finish is the failure mode this whole path exists
841
- // to avoid. The comment said "completed" long after the code stopped doing that.
842
- let terminated = false;
843
- let firstOutputReported = false;
844
- const reportFirstOutput = (event: AdapterEvent): void => {
845
- if (firstOutputReported) return;
846
- const nonEmpty = event.type === "text_delta"
847
- ? event.text.length > 0
848
- : event.type === "thinking_delta"
849
- ? event.thinking.length > 0
850
- : event.type === "reasoning_raw_delta"
851
- ? event.text.length > 0
852
- : false;
853
- if (!nonEmpty) return;
854
- firstOutputReported = true;
855
- try { options?.onFirstOutput?.(); } catch { /* metrics must not break the stream */ }
856
- };
857
- const it = events[Symbol.asyncIterator]();
858
- let iteratorStarted = false;
859
- let iteratorReturned = false;
860
- let upstreamDone = false;
861
- const returnIterator = () => {
862
- if (iteratorReturned) return;
863
- iteratorReturned = true;
864
- const finishReturn = () => {
865
- try {
866
- void it.return?.()?.catch(() => {});
867
- } catch {
868
- /* synchronous iterator cleanup failure is also best-effort */
869
- }
870
- };
871
- // Async-generator return() before the first next() does not enter the generator, so its
872
- // finally blocks cannot cancel prepared upstream bodies. The cancel hook has already
873
- // aborted the turn; bootstrap one cleanup step, then close the iterator without awaiting it.
874
- if (!iteratorStarted) {
875
- iteratorStarted = true;
876
- try {
877
- void it.next().then(finishReturn, () => {}).catch(() => {});
878
- } catch {
879
- /* synchronous iterator start failure is also best-effort */
880
- }
881
- return;
882
- }
883
- finishReturn();
884
- };
885
- let upstreamCancelled = false;
886
- const cancelUpstreamOnce = () => {
887
- if (upstreamCancelled) return;
888
- upstreamCancelled = true;
889
- try { onCancel?.(); } catch { /* cancellation must not strand the client stream */ }
890
- returnIterator();
891
- };
892
- let handlingTranslatorOverflow = false;
893
- terminateForTranslatorOverflow = _error => {
894
- if (handlingTranslatorOverflow || terminated || clientCancelled || closed) return;
895
- handlingTranslatorOverflow = true;
896
- abortCurrentToolCallForTranslatorOverflow();
897
- currentWebSearch = null;
898
- releasePendingWebSources();
899
- const failure = adapterFailureFromEvent({
900
- type: "error",
901
- status: 502,
902
- errorType: "upstream_error",
903
- code: "translation_buffer_limit",
904
- message: "upstream translation buffer exceeded the safe limit",
905
- }).error;
906
- const failedFrame = sseEvent("response.failed", {
907
- type: "response.failed",
908
- sequence_number: seq++,
909
- response: {
910
- ...responseSnapshot("failed", finishedItems),
911
- error: failure,
912
- last_error: failure,
913
- },
914
- });
915
- try {
916
- controller.enqueue(encoder.encode(failedFrame));
917
- controller.enqueue(encoder.encode("data: [DONE]\n\n"));
918
- emittedFrames += 2;
919
- } catch {
920
- /* client already tore down the stream */
921
- }
922
- reportTerminal("failed");
923
- terminated = true;
924
- cancelUpstreamOnce();
925
- if (beat !== undefined) clearBeatInterval(beat);
926
- beat = undefined;
927
- try { controller.close(); } catch { /* already closed */ }
928
- closed = true;
929
- disposeOwnedBudget();
930
- gated = true;
931
- stepping = false;
932
- };
933
- const attemptTerminationCleanup = (action: () => void): boolean => {
934
- try {
935
- action();
936
- return !terminated && !closed;
937
- } catch (error) {
938
- if (!isTranslatorBudgetExceededError(error)) throw error;
939
- terminateForTranslatorOverflow(error);
940
- return false;
941
- }
942
- };
943
- const step = async () => {
944
- if (stepping || closed) return;
945
- stepping = true;
946
- gated = false;
947
- const emittedAtStart = emittedFrames;
948
- try {
949
- while (!terminated && !closed && emittedFrames === emittedAtStart) {
950
- iteratorStarted = true;
951
- const next = await it.next();
952
- // A cancel during this await disposes the owned budget; a late event
953
- // must never be processed or charged against it. Exit step() outright:
954
- // falling into EOF synthesis would let closeCurrentMessage() charge
955
- // finished-item retention against the disposed budget.
956
- if (closed || clientCancelled) {
957
- gated = true;
958
- stepping = false;
959
- return;
960
- }
961
- if (next.done) { upstreamDone = true; break; }
962
- const event = next.value;
963
- let terminalEvent = false;
964
- // Invisible adapter heartbeats (and buffered web-search progress) count as upstream
965
- // liveness only — they must not suppress wire keepalives that re-arm Codex idle timers.
966
- upstreamActivity = true;
967
- stallTicks = 0;
968
- reportFirstOutput(event);
969
- // Compaction turns emit ONLY the synthetic compaction item + response.completed. The
970
- // summary text is accumulated silently: emitting it as a normal assistant message would
971
- // duplicate the summary if this response is ever replayed via previous_response_id
972
- // expansion (rememberResponseState stores input + output). Codex ignores extra items but
973
- // its compaction UI renders nothing mid-turn, so nothing is lost visually.
974
- if (options?.compaction) {
975
- if (event.type === "text_delta") {
976
- compaction = appendString(
977
- compaction,
978
- event.text,
979
- "retained_collectors",
980
- );
981
- continue;
982
- }
983
- if (event.type !== "done" && event.type !== "incomplete" && event.type !== "error") continue;
984
- }
985
- // Anthropic signature_delta supplies the latest signature, not an append-only
986
- // fragment (anthropic-sdk-typescript MessageStream). Keep consecutive updates
987
- // together; the next semantic event belongs to the following block.
988
- if (pendingSignature !== undefined && event.type !== "thinking_signature" && event.type !== "heartbeat") {
989
- if (currentReasoning) closeCurrentReasoning();
990
- else flushHiddenReasoningEnvelope();
991
- }
992
- switch (event.type) {
993
- case "assistant_boundary": {
994
- // A guarded continuation starts a fresh assistant output item while keeping the
995
- // intermediate, suspicious text in the same Responses turn.
996
- if (currentMsg) closeCurrentMessage("commentary");
997
- if (currentReasoning) closeCurrentReasoning();
998
- if (currentRawReasoning) closeCurrentRawReasoning();
999
- flushHiddenRawReasoning();
1000
- rawReasoningForNextToolCall = "";
1001
- if (currentToolCall) closeCurrentToolCall();
1002
- flushHiddenReasoningEnvelope();
1003
- break;
1004
- }
1005
- case "text_delta": {
1006
- if (currentReasoning) closeCurrentReasoning();
1007
- if (currentRawReasoning) closeCurrentRawReasoning();
1008
- flushHiddenRawReasoning();
1009
- // Reasoning consumed by a REAL text turn, not a tool call: no cache target.
1010
- // Empty text deltas must not wipe reasoning that precedes a tool call
1011
- // (chat-completions providers emit empty content deltas mid-tool-turn).
1012
- if (event.text.length > 0) rawReasoningForNextToolCall = "";
1013
- if (currentToolCall) closeCurrentToolCall();
1014
- // Only flush on an explicit phase change. A later delta that omits `phase` must
1015
- // keep appending to the current message rather than wiping the earlier phase.
1016
- if (currentMsg && event.phase !== undefined && currentMsg.phase !== event.phase) {
1017
- closeCurrentMessage("commentary");
1018
- }
1019
- if (!currentMsg) {
1020
- const itemId = `msg_${uuid()}`;
1021
- const item = {
1022
- type: "message", id: itemId, status: "in_progress", role: "assistant",
1023
- content: [] as { type: string; text: string; annotations: never[] }[],
1024
- ...(event.phase ? { phase: event.phase } : {}),
1025
- };
1026
- emit("response.output_item.added", { output_index: outputIndex, item });
1027
- emit("response.content_part.added", {
1028
- item_id: itemId, output_index: outputIndex, content_index: 0,
1029
- part: { type: "output_text", text: "", annotations: [] },
1030
- });
1031
- currentMsg = {
1032
- itemId, outputIndex, text: emptyChunks(),
1033
- citationFilter: createCitationMarkerFilter(),
1034
- ...(event.phase ? { phase: event.phase } : {}),
1035
- };
1036
- }
1037
- currentMsg.text = appendString(
1038
- currentMsg.text,
1039
- event.text,
1040
- "retained_collectors",
1041
- );
1042
- // A citation span can straddle a delta boundary, so the filter withholds an
1043
- // unterminated tail and releases it at close (#3150). The accumulator above
1044
- // keeps the raw text; it is stripped once in closeCurrentMessage.
1045
- const visible = currentMsg.citationFilter.push(event.text);
1046
- if (visible) {
1047
- emit("response.output_text.delta", {
1048
- item_id: currentMsg.itemId, output_index: currentMsg.outputIndex,
1049
- content_index: 0, delta: visible,
1050
- });
1051
- }
1052
- break;
1053
- }
1054
- case "thinking_delta": {
1055
- if (options?.hideThinkingSummary) {
1056
- // The hidden branch returns early, so flush any raw reasoning
1057
- // that preceded the thinking block and clear the replay-cache
1058
- // candidate — otherwise a stale reasoning_raw_delta would be
1059
- // recorded for a LATER tool call (CodeRabbit on #971).
1060
- flushHiddenRawReasoning();
1061
- rawReasoningForNextToolCall = "";
1062
- hiddenThinking = appendString(
1063
- hiddenThinking,
1064
- event.thinking,
1065
- "reasoning",
1066
- );
1067
- break;
1068
- }
1069
- if (currentMsg) closeCurrentMessage("commentary");
1070
- if (currentRawReasoning) closeCurrentRawReasoning();
1071
- flushHiddenRawReasoning();
1072
- if (event.thinking.length > 0) rawReasoningForNextToolCall = "";
1073
- if (currentToolCall) closeCurrentToolCall();
1074
- if (!currentReasoning) {
1075
- const itemId = `rs_${uuid()}`;
1076
- const item = { type: "reasoning", id: itemId, summary: [] as { type: string; text: string }[] };
1077
- emit("response.output_item.added", { output_index: outputIndex, item });
1078
- emit("response.reasoning_summary_part.added", {
1079
- item_id: itemId, output_index: outputIndex, summary_index: 0,
1080
- part: { type: "summary_text", text: "" },
1081
- });
1082
- currentReasoning = { itemId, outputIndex, text: emptyChunks() };
1083
- }
1084
- currentReasoning.text = appendString(
1085
- currentReasoning.text,
1086
- event.thinking,
1087
- "reasoning",
1088
- );
1089
- emit("response.reasoning_summary_text.delta", {
1090
- item_id: currentReasoning.itemId, output_index: currentReasoning.outputIndex,
1091
- summary_index: 0, delta: event.thinking,
1092
- });
1093
- break;
1094
- }
1095
- case "thinking_signature": {
1096
- pendingSignatureBytes = replaceRetainedString(pendingSignatureBytes, event.signature, "reasoning");
1097
- pendingSignature = event.signature;
1098
- // Delay closing until the next semantic event so a signature update cannot
1099
- // create another block or become attached to the following thinking text.
1100
- break;
1101
- }
1102
- case "redacted_thinking": {
1103
- if (currentMsg) closeCurrentMessage("commentary");
1104
- if (currentReasoning) closeCurrentReasoning();
1105
- if (currentRawReasoning) closeCurrentRawReasoning();
1106
- flushHiddenRawReasoning();
1107
- if (currentToolCall) closeCurrentToolCall();
1108
- budget?.chargeRetained(bytesOf(event.data), { kind: "reasoning" });
1109
- pendingRedacted.push(event.data);
1110
- // A redacted block is complete at content_block_start. Emit it here,
1111
- // not with a later thinking block or after a tool call at turn end.
1112
- flushHiddenReasoningEnvelope();
1113
- break;
1114
- }
1115
- case "kiro_redacted_reasoning": {
1116
- // Stash only — see flushKiroRedactedReasoning. One blob per turn, so last wins.
1117
- pendingKiroRedactedBytes = replaceRetainedString(pendingKiroRedactedBytes, event.data, "reasoning");
1118
- pendingKiroRedacted = event.data;
1119
- break;
1120
- }
1121
- case "reasoning_raw_delta": {
1122
- if (options?.hideThinkingSummary) {
1123
- hiddenRawReasoning = appendString(
1124
- hiddenRawReasoning,
1125
- event.text,
1126
- "reasoning",
1127
- );
1128
- break;
1129
- }
1130
- if (currentMsg) closeCurrentMessage("commentary");
1131
- if (currentReasoning) closeCurrentReasoning();
1132
- if (currentToolCall) closeCurrentToolCall();
1133
- if (!currentRawReasoning) {
1134
- const itemId = `rs_${uuid()}`;
1135
- const item = { type: "reasoning", id: itemId, summary: [] as { type: string; text: string }[] };
1136
- emit("response.output_item.added", { output_index: outputIndex, item });
1137
- currentRawReasoning = { itemId, outputIndex, text: emptyChunks() };
1138
- }
1139
- currentRawReasoning.text = appendString(
1140
- currentRawReasoning.text,
1141
- event.text,
1142
- "reasoning",
1143
- );
1144
- // Raw reasoning (openai-chat reasoning_content, kiro tags) rides the CONTENT
1145
- // channel. Clients control raw-reasoning display; this text is not a
1146
- // provider-authored summary.
1147
- emit("response.reasoning_text.delta", {
1148
- item_id: currentRawReasoning.itemId, output_index: currentRawReasoning.outputIndex,
1149
- content_index: 0, delta: event.text,
1150
- });
1151
- break;
1152
- }
1153
- case "tool_call_start": {
1154
- if (currentMsg) closeCurrentMessage("commentary");
1155
- if (currentReasoning) closeCurrentReasoning();
1156
- if (currentRawReasoning) closeCurrentRawReasoning();
1157
- flushHiddenRawReasoning();
1158
- if (rawReasoningForNextToolCall) {
1159
- rememberReasoningForCall(event.id, rawReasoningForNextToolCall, replayCacheScope);
1160
- }
1161
- if (currentToolCall) closeCurrentToolCall();
1162
- const effectiveName = normalizeDeclaredToolName(event.name, options?.declaredToolNames);
1163
- const codeModeHelperName = effectiveName === "exec" && event.name !== effectiveName
1164
- ? event.name
1165
- : undefined;
1166
- const mapped = toolNsMap?.get(effectiveName);
1167
- const realName = mapped?.name ?? effectiveName;
1168
- if (options?.declaredToolNames && !options.declaredToolNames.has(effectiveName)) {
1169
- const failure = responseError(
1170
- 502,
1171
- "upstream_error",
1172
- `routed provider emitted undeclared client tool "${effectiveName}"; only request-declared tools may be called`,
1173
- );
1174
- emit("response.failed", {
1175
- response: {
1176
- ...responseSnapshot("failed", finishedItems),
1177
- error: failure,
1178
- last_error: failure,
1179
- },
1180
- });
1181
- reportTerminal("failed");
1182
- terminalEvent = true;
1183
- break;
1184
- }
1185
- const ns = mapped?.namespace;
1186
- const toolSearch = toolSearchToolNames?.has(realName) ?? false;
1187
- const freeform = !toolSearch && (mapped
1188
- ? mapped.freeform === true
1189
- : (freeformToolNames?.has(realName) ?? false));
1190
- const itemId = `${toolSearch ? "tsc" : freeform ? "ctc" : "fc"}_${uuid()}`;
1191
- const item = toolSearch
1192
- ? { type: "tool_search_call", id: itemId, call_id: event.id, execution: "client", arguments: {}, status: "in_progress" }
1193
- : freeform
1194
- ? { type: "custom_tool_call", id: itemId, call_id: event.id, name: realName, ...(ns ? { namespace: ns } : {}), input: "", status: "in_progress" }
1195
- : { type: "function_call", id: itemId, call_id: event.id, name: realName, arguments: "", status: "in_progress", ...(ns ? { namespace: ns } : {}) };
1196
- emit("response.output_item.added", { output_index: outputIndex, item });
1197
- currentToolCall = { itemId, outputIndex, callId: event.id, name: realName, args: "", argsBytes: 0, namespace: ns, freeform, toolSearch, codeModeHelperName, providerMetadata: event.providerMetadata };
1198
- budget?.openCall(event.id);
1199
- break;
1200
- }
1201
- case "tool_call_delta": {
1202
- if (currentToolCall) {
1203
- ({ value: currentToolCall.args, bytes: currentToolCall.argsBytes } = appendStringDirect(
1204
- currentToolCall.args,
1205
- currentToolCall.argsBytes,
1206
- event.arguments,
1207
- "tool_args",
1208
- currentToolCall.callId,
1209
- ));
1210
- if (!currentToolCall.freeform && !currentToolCall.toolSearch) {
1211
- emit("response.function_call_arguments.delta", {
1212
- item_id: currentToolCall.itemId, output_index: currentToolCall.outputIndex,
1213
- delta: event.arguments,
1214
- });
1215
- }
1216
- if (currentToolCall.freeform && !currentToolCall.codeModeHelperName) {
1217
- // Hold while the buffer is still an ambiguous prefix of the JSON wrapper,
1218
- // then stream only the unwrapped input suffix (never rewind on mode flips).
1219
- if (!FREEFORM_WRAP_PREFIX.startsWith(currentToolCall.args)) {
1220
- const full = freeformPartialInput(currentToolCall.args);
1221
- const emitted = currentToolCall.inputEmitted ?? "";
1222
- // Also hold a buffer that could still become a complete patch envelope:
1223
- // at completion such a body is recompiled into an apply_patch helper call,
1224
- // and streaming the envelope bytes first would be that same rewind.
1225
- const mayCompile = declaresCodeModeExec(options?.declaredToolNames)
1226
- && !currentToolCall.namespace
1227
- && currentToolCall.name === "exec";
1228
- if (!(mayCompile && mayBecomePatchEnvelope(full)) && full.startsWith(emitted) && full.length > emitted.length) {
1229
- emit("response.custom_tool_call_input.delta", {
1230
- item_id: currentToolCall.itemId, output_index: currentToolCall.outputIndex,
1231
- delta: full.slice(emitted.length),
1232
- });
1233
- currentToolCall.inputEmitted = full;
1234
- }
1235
- }
1236
- }
1237
- }
1238
- break;
1239
- }
1240
- case "tool_call_end": {
1241
- // Fragments already streamed cannot be repaired. Refuse to complete a function call
1242
- // whose assembled arguments do not parse — cancel the item and fail the turn so the
1243
- // client never sees status:"completed" for unusable args (#765 stream remainder).
1244
- if (
1245
- currentToolCall
1246
- && !currentToolCall.freeform
1247
- && !currentToolCall.toolSearch
1248
- && !toolCallArgumentsUsable(currentToolCall.args)
1249
- ) {
1250
- failCurrentToolCall();
1251
- const failure = responseError(
1252
- 502,
1253
- "upstream_error",
1254
- "upstream stream produced malformed tool call arguments",
1255
- );
1256
- emit("response.failed", {
1257
- response: {
1258
- ...responseSnapshot("failed", finishedItems),
1259
- error: failure,
1260
- last_error: failure,
1261
- },
1262
- });
1263
- reportTerminal("failed");
1264
- terminalEvent = true;
1265
- break;
1266
- }
1267
- closeCurrentToolCall();
1268
- break;
1269
- }
1270
- case "web_search_call_begin": {
1271
- // Open the native search cell so Codex shows the "Searching the web" spinner WHILE the
1272
- // sidecar runs. Close any other open item first, allocate this item's output index, and
1273
- // hold it open until the matching `web_search_call_end` (or a terminal close).
1274
- if (currentMsg) closeCurrentMessage("commentary");
1275
- if (currentReasoning) closeCurrentReasoning();
1276
- if (currentRawReasoning) closeCurrentRawReasoning();
1277
- flushHiddenRawReasoning();
1278
- if (currentToolCall) closeCurrentToolCall();
1279
- if (currentWebSearch) closeCurrentWebSearch("completed", []);
1280
- const wsItemId = `ws_${uuid()}`;
1281
- emit("response.output_item.added", {
1282
- output_index: outputIndex,
1283
- item: { type: "web_search_call", id: wsItemId, status: "in_progress" },
1284
- });
1285
- currentWebSearch = { itemId: wsItemId, eventId: event.id, outputIndex };
1286
- break;
1287
- }
1288
- case "web_search_call_end": {
1289
- // The sidecar resolved — finalize the cell as "Searched <query>". If no begin opened
1290
- // (defensive), synthesize the added frame first so the done has a matching item.
1291
- if (!currentWebSearch || currentWebSearch.eventId !== event.id) {
1292
- if (currentWebSearch) closeCurrentWebSearch("completed", []);
1293
- const wsItemId2 = `ws_${uuid()}`;
1294
- emit("response.output_item.added", {
1295
- output_index: outputIndex,
1296
- item: { type: "web_search_call", id: wsItemId2, status: "in_progress" },
1297
- });
1298
- currentWebSearch = { itemId: wsItemId2, eventId: event.id, outputIndex };
1299
- }
1300
- const safeSources = safeWebSearchSources(event.sources);
1301
- closeCurrentWebSearch(event.status ?? "completed", event.queries, safeSources);
1302
- // Queue this search's sources for the next assistant message (dedup by URL).
1303
- if (safeSources.length > 0) {
1304
- for (const source of safeSources) {
1305
- if (appendSafeWebSearchSource(pendingWebSources, source)) {
1306
- pendingWebSourceBytes += chargeValue(source, "tool_search_sources");
1307
- }
1308
- }
1309
- }
1310
- break;
1311
- }
1312
- case "done": {
1313
- if (currentMsg) closeCurrentMessage(event.stopReason ? undefined : "final_answer");
1314
- if (currentReasoning) closeCurrentReasoning();
1315
- if (currentRawReasoning) closeCurrentRawReasoning();
1316
- flushHiddenRawReasoning();
1317
- if (currentToolCall) {
1318
- if (isTruncatedStopReason(event.stopReason)) failCurrentToolCall();
1319
- else closeCurrentToolCall();
1320
- }
1321
- // A search still in flight when upstream truncates never returned results, so it
1322
- // takes the same "failed" status as the error/incomplete terminals below.
1323
- if (currentWebSearch) closeCurrentWebSearch(isTruncatedStopReason(event.stopReason) ? "failed" : "completed", []);
1324
- releasePendingWebSources();
1325
- // Redacted-only turns (or hidden thinking without a trailing signature event) still
1326
- // need their envelope-only reasoning item so the blocks replay next turn.
1327
- flushHiddenReasoningEnvelope();
1328
- // After every close above, so the blob lands AFTER the assistant message it belongs
1329
- // to and the parser's backwards pairing finds it.
1330
- flushKiroRedactedReasoning();
1331
- // Truncated turns must never install replacement history (#422). The buffered path
1332
- // has always checked this; streaming emitted the item BEFORE reading stopReason, so
1333
- // a max_tokens/content_filter turn shipped a half-written summary and then declared
1334
- // itself incomplete — the same hazard, one branch over.
1335
- if (options?.compaction && !isTruncatedStopReason(event.stopReason)) {
1336
- // Exactly one compaction item per turn; codex-rs takes the first and fatals on 0.
1337
- const item = {
1338
- type: "compaction", id: `cmp_${uuid()}`,
1339
- encrypted_content: event.compactionEncryptedContent ?? encodeCompactionSummary(joinChunks(compaction)),
1340
- };
1341
- emit("response.output_item.done", { output_index: outputIndex, item });
1342
- retainFinishedItem(item as OutputItem, event.compactionEncryptedContent
1343
- ? bytesOf(event.compactionEncryptedContent)
1344
- : compaction.bytes);
1345
- outputIndex++;
1346
- }
1347
- // Recognize every adapter's truncation vocabulary, not just the canonical pair.
1348
- // Suppression and terminal status must agree: withholding the compaction item while
1349
- // still reporting success hands codex-rs a completed response with zero compaction
1350
- // items, which it treats as fatal.
1351
- if (truncationReasonFor(event.stopReason)) {
1352
- // Upstream stopped before a normal completion. Surface as incomplete so the
1353
- // client can distinguish a truncated/filtered turn from a finished one.
1354
- // #1926 gap 2: bound the window in which a handed-out thought signature is
1355
- // not yet durable before the turn becomes externally terminal.
1356
- await awaitThoughtSignatureDurability();
1357
- const response = {
1358
- ...responseSnapshot("incomplete", finishedItems, event.endTurn),
1359
- usage: responsesUsage(event.usage),
1360
- incomplete_details: {
1361
- reason: truncationReasonFor(event.stopReason) ?? "content_filter",
1362
- },
1363
- };
1364
- // Cache max-output partials so previous_response_id replay can continue them;
1365
- // rememberResponseState rejects content-filtered incomplete responses.
1366
- options?.onCompletedResponse?.(response, event.providerState);
1367
- options?.onUsage?.(event.usage);
1368
- emit("response.incomplete", { response });
1369
- reportTerminal("incomplete");
1370
- } else {
1371
- await awaitThoughtSignatureDurability();
1372
- const response = { ...responseSnapshot("completed", finishedItems, event.endTurn), usage: responsesUsage(event.usage) };
1373
- options?.onCompletedResponse?.(response, event.providerState);
1374
- options?.onUsage?.(event.usage);
1375
- emit("response.completed", {
1376
- response,
1377
- });
1378
- reportTerminal("completed");
1379
- }
1380
- terminalEvent = true;
1381
- break;
1382
- }
1383
- case "incomplete": {
1384
- if (currentMsg) closeCurrentMessage();
1385
- if (currentReasoning) closeCurrentReasoning();
1386
- if (currentRawReasoning) closeCurrentRawReasoning();
1387
- flushHiddenRawReasoning();
1388
- if (currentToolCall) failCurrentToolCall();
1389
- if (currentWebSearch) closeCurrentWebSearch("failed", []);
1390
- releasePendingWebSources();
1391
- flushHiddenReasoningEnvelope();
1392
- options?.onUsage?.(event.usage);
1393
- await awaitThoughtSignatureDurability();
1394
- emit("response.incomplete", {
1395
- response: {
1396
- ...responseSnapshot("incomplete", finishedItems, event.endTurn),
1397
- usage: responsesUsage(event.usage),
1398
- incomplete_details: {
1399
- reason: event.reason,
1400
- ...(event.message ? { message: event.message } : {}),
1401
- ...(event.retryable !== undefined ? { retryable: event.retryable } : {}),
1402
- },
1403
- },
1404
- });
1405
- reportTerminal("incomplete");
1406
- terminalEvent = true;
1407
- break;
1408
- }
1409
- case "error": {
1410
- if (event.code === "translation_buffer_limit") {
1411
- terminateForTranslatorOverflow(event);
1412
- return;
1413
- }
1414
- if (currentMsg) closeCurrentMessage();
1415
- if (currentReasoning) closeCurrentReasoning();
1416
- if (currentRawReasoning) closeCurrentRawReasoning();
1417
- flushHiddenRawReasoning();
1418
- if (currentToolCall) failCurrentToolCall();
1419
- if (currentWebSearch) closeCurrentWebSearch("failed", []);
1420
- releasePendingWebSources();
1421
- const failure = adapterFailureFromEvent(event);
1422
- if (event.usage) options?.onUsage?.(event.usage);
1423
- await awaitThoughtSignatureDurability();
1424
- emit("response.failed", {
1425
- response: {
1426
- ...responseSnapshot("failed", finishedItems),
1427
- // Partial consumption from a mid-stream upstream failure: surfaced so the request
1428
- // log can record real tokens instead of usageStatus "unreported" with 0.
1429
- ...(event.usage ? { usage: responsesUsage(event.usage) } : {}),
1430
- error: failure.error,
1431
- last_error: failure.error,
1432
- ...(isCyberPolicyCode(failure.error.code)
1433
- ? { retryable: false }
1434
- : event.retryable !== undefined ? { retryable: event.retryable } : {}),
1435
- },
1436
- });
1437
- reportTerminal("failed");
1438
- terminalEvent = true;
1439
- break;
1440
- }
1441
- }
1442
- if (terminalEvent) {
1443
- cancelUpstreamOnce();
1444
- terminated = true;
1445
- break;
1446
- }
1447
- }
1448
- } catch (err) {
1449
- if (isTranslatorBudgetExceededError(err)) {
1450
- terminateForTranslatorOverflow(err);
1451
- return;
1452
- }
1453
- if (!terminated) {
1454
- if (!attemptTerminationCleanup(() => {
1455
- flushHiddenRawReasoning();
1456
- if (currentToolCall) failCurrentToolCall();
1457
- if (currentWebSearch) closeCurrentWebSearch("failed", []);
1458
- releasePendingWebSources();
1459
- })) return;
1460
- const failure = responseError(
1461
- 500,
1462
- "proxy_error",
1463
- redactSecretString(err instanceof Error ? err.message : String(err)),
1464
- );
1465
- emit("response.failed", {
1466
- response: {
1467
- ...responseSnapshot("failed", finishedItems),
1468
- error: failure,
1469
- last_error: failure,
1470
- ...(isCyberPolicyCode(failure.code) ? { retryable: false } : {}),
1471
- },
1472
- });
1473
- reportTerminal("failed");
1474
- cancelUpstreamOnce();
1475
- terminated = true;
1476
- }
1477
- }
1478
-
1479
- if (!terminated && !upstreamDone) {
1480
- gated = true;
1481
- stepping = false;
1482
- return;
1483
- }
1484
- if (beat !== undefined) { clearBeatInterval(beat); beat = undefined; }
1485
-
1486
- if (!terminated) {
1487
- // The adapter generator ended without an explicit done/error event. Mark as incomplete
1488
- // rather than completed so Codex can distinguish a clean finish from a truncated stream.
1489
- if (!attemptTerminationCleanup(() => {
1490
- if (currentMsg) closeCurrentMessage();
1491
- if (currentReasoning) closeCurrentReasoning();
1492
- if (currentRawReasoning) closeCurrentRawReasoning();
1493
- flushHiddenRawReasoning();
1494
- if (currentToolCall) failCurrentToolCall();
1495
- if (currentWebSearch) closeCurrentWebSearch("failed", []);
1496
- releasePendingWebSources();
1497
- })) return;
1498
- options?.onUsage?.(undefined);
1499
- await awaitThoughtSignatureDurability();
1500
- emit("response.incomplete", {
1501
- response: {
1502
- ...responseSnapshot("incomplete", finishedItems),
1503
- usage: responsesUsage(undefined),
1504
- incomplete_details: { reason: "adapter_eof" },
1505
- },
1506
- });
1507
- reportTerminal("incomplete");
1508
- terminated = true;
1509
- }
1510
-
1511
- emitDone();
1512
- try {
1513
- controller.close();
1514
- } catch {
1515
- /* already closed (e.g. client cancelled) */
1516
- }
1517
- closed = true;
1518
- disposeOwnedBudget();
1519
- gated = true;
1520
- stepping = false;
1521
- };
1522
-
1523
- const startStream = () => {
1524
- emit("response.created", { response: responseSnapshot("in_progress", []) });
1525
- // Responses spec parity: clients expect an explicit in_progress frame after created.
1526
- emit("response.in_progress", { response: responseSnapshot("in_progress", []) });
1527
- // The default ReadableStream strategy has HWM=1. Once one event's frames fill that
1528
- // queue, pull stepping pauses; no custom FIFO or queuing strategy is layered on top.
1529
- gated = true;
1530
- beat = setBeatInterval(() => {
1531
- if (closed || gated) return;
1532
- if (upstreamActivity) {
1533
- upstreamActivity = false;
1534
- stallTicks = 0;
1535
- } else if (++stallTicks >= maxStallTicks) {
1536
- if (!attemptTerminationCleanup(() => {
1537
- if (currentMsg) closeCurrentMessage();
1538
- if (currentReasoning) closeCurrentReasoning();
1539
- if (currentRawReasoning) closeCurrentRawReasoning();
1540
- flushHiddenRawReasoning();
1541
- if (currentToolCall) failCurrentToolCall();
1542
- if (currentWebSearch) closeCurrentWebSearch("failed", []);
1543
- releasePendingWebSources();
1544
- })) return;
1545
- // #1926 gap 2 residual: this beat callback is synchronous, so the durability
1546
- // barrier is not awaited on the stall-timeout kill path. The in-memory store is
1547
- // already updated; only a crash between here and the queued write loses it,
1548
- // which is the pre-#1926 status quo for an already-abnormal termination.
1549
- emit("response.incomplete", {
1550
- response: {
1551
- ...responseSnapshot("incomplete", finishedItems),
1552
- incomplete_details: { reason: "upstream_stall_timeout" },
1553
- },
1554
- });
1555
- reportTerminal("incomplete");
1556
- cancelUpstreamOnce();
1557
- terminated = true;
1558
- emitDone();
1559
- if (beat !== undefined) clearBeatInterval(beat);
1560
- beat = undefined;
1561
- try { controller.close(); } catch { /* already closed */ }
1562
- closed = true;
1563
- disposeOwnedBudget();
1564
- return;
1565
- }
1566
- // Wire silence is independent of upstream adapter heartbeats.
1567
- if (wireActivity) {
1568
- wireActivity = false;
1569
- return;
1570
- }
1571
- try {
1572
- controller.enqueue(heartbeatFrame);
1573
- emittedFrames++;
1574
- } catch {
1575
- closed = true;
1576
- disposeOwnedBudget();
1577
- }
1578
- }, heartbeatMs);
1579
- };
1580
-
1581
- return new ReadableStream<Uint8Array>({
1582
- start(streamController) {
1583
- controller = streamController;
1584
- startStream();
1585
- },
1586
- pull() {
1587
- return step();
1588
- },
1589
- cancel() {
1590
- // Client (Codex) disconnected. Stop emitting and let the caller abort the upstream fetch so a
1591
- // cancelled turn does not leak the upstream stream or keep draining tokens (RC2).
1592
- clientCancelled = true;
1593
- closed = true;
1594
- clearOwnedWatchdog();
1595
- if (beat !== undefined) clearBeatInterval(beat);
1596
- cancelUpstreamOnce();
1597
- releasePendingWebSources();
1598
- disposeOwnedBudget();
1599
- },
1600
- });
1601
- }
1602
-
1603
- export function buildResponseJSON(
1604
- events: AdapterEvent[],
1605
- modelId: string,
1606
- options?: Parameters<typeof buildResponseJSONWithBudget>[2],
1607
- ): Record<string, unknown> {
1608
- // Default-budget safety net: a caller that omits the budget gets a bounded
1609
- // default (disposed with the call), never the unbounded append path.
1610
- if (options?.translatorBudget) return buildResponseJSONWithBudget(events, modelId, options);
1611
- const budget = createTranslatorBudget();
1612
- try {
1613
- return buildResponseJSONWithBudget(events, modelId, { ...options, translatorBudget: budget });
1614
- } finally {
1615
- budget.dispose();
1616
- }
1617
- }
1618
-
1619
- function buildResponseJSONWithBudget(
1620
- events: AdapterEvent[],
1621
- modelId: string,
1622
- options?: {
1623
- hideThinkingSummary?: boolean;
1624
- toolNsMap?: Map<string, { namespace: string; name: string; freeform?: true }>;
1625
- /** Request-visible tool names. When present, an upstream call outside this set fails closed. */
1626
- declaredToolNames?: ReadonlySet<string>;
1627
- /** Declared parameter schema per tool name; repairs integral-float integer args (#1611). */
1628
- toolParameterSchemas?: ReadonlyMap<string, Record<string, unknown>>;
1629
- freeformToolNames?: Set<string>;
1630
- toolSearchToolNames?: Set<string>;
1631
- /** Remote compaction v2 turn — append one synthetic compaction output item (see bridgeToResponsesSSE). */
1632
- compaction?: boolean;
1633
- onProviderState?: (state: OcxProviderContinuationState) => void;
1634
- /** Raw adapter-reported usage before wire normalization (see bridgeToResponsesSSE onUsage). */
1635
- onUsage?: (usage: OcxUsage | undefined) => void;
1636
- translatorBudget?: TranslatorBudget;
1637
- /** Conversation identity for the reasoning replay cache (issue #950). */
1638
- replayCacheScope?: OcxReasoningReplayScopeRef;
1639
- },
1640
- ): Record<string, unknown> {
1641
- const responseId = `resp_${uuid()}`;
1642
- const replayCacheScope = options?.replayCacheScope;
1643
- const output: OutputItem[] = [];
1644
- const budget = options?.translatorBudget;
1645
- const encoder = new TextEncoder();
1646
- const bytesOf = (value: string): number => Buffer.byteLength(value);
1647
- const appendBatchString = (
1648
- previous: StringChunks,
1649
- fragment: string,
1650
- kind: TranslatorBufferKind,
1651
- callId?: string,
1652
- ): StringChunks => {
1653
- const fragmentBytes = bytesOf(fragment);
1654
- if (fragmentBytes === 0) return previous;
1655
- const nextBytes = previous.bytes + fragmentBytes;
1656
- if (!budget) {
1657
- previous.chunks.push(fragment);
1658
- return { chunks: previous.chunks, bytes: nextBytes };
1659
- }
1660
- const scope = { kind, ...(callId ? { callId } : {}) };
1661
- const reservation = budget.reserveTransient(nextBytes, scope);
1662
- try {
1663
- previous.chunks.push(fragment);
1664
- const result: StringChunks = { chunks: previous.chunks, bytes: nextBytes };
1665
- reservation.commitRetained();
1666
- budget.releaseRetained(previous.bytes, scope);
1667
- return result;
1668
- } catch (error) {
1669
- reservation.release();
1670
- throw error;
1671
- }
1672
- };
1673
- // Batch counterpart: tool-call arguments require direct string representation for immediate
1674
- // JSON serialization compatibility.
1675
- const appendBatchStringDirect = (
1676
- previous: string,
1677
- previousBytes: number,
1678
- fragment: string,
1679
- kind: TranslatorBufferKind,
1680
- callId?: string,
1681
- ): { value: string; bytes: number } => {
1682
- const nextBytes = previousBytes + bytesOf(fragment);
1683
- if (!budget) return { value: previous + fragment, bytes: nextBytes };
1684
- const scope = { kind, ...(callId ? { callId } : {}) };
1685
- const reservation = budget.reserveTransient(nextBytes, scope);
1686
- try {
1687
- const value = previous + fragment;
1688
- reservation.commitRetained();
1689
- budget.releaseRetained(previousBytes, scope);
1690
- return { value, bytes: nextBytes };
1691
- } catch (error) {
1692
- reservation.release();
1693
- throw error;
1694
- }
1695
- };
1696
- const replaceBatchRetainedString = (previousBytes: number, next: string, kind: TranslatorBufferKind): number => {
1697
- const nextBytes = bytesOf(next);
1698
- if (!budget) return nextBytes;
1699
- const reservation = budget.reserveTransient(nextBytes, { kind });
1700
- reservation.commitRetained();
1701
- budget.releaseRetained(previousBytes, { kind });
1702
- return nextBytes;
1703
- };
1704
- const pushOutput = (item: OutputItem, replacedBytes = 0, kind: TranslatorBufferKind = "retained_collectors") => {
1705
- const reservation = budget?.reserveTransient(bytesOf(JSON.stringify(item)), { kind });
1706
- output.push(item);
1707
- reservation?.commitRetained();
1708
- if (replacedBytes > 0) budget?.releaseRetained(replacedBytes, { kind });
1709
- };
1710
- let usage: OcxUsage | undefined;
1711
- let errorEvent: Extract<AdapterEvent, { type: "error" }> | undefined;
1712
- let incompleteEvent: Extract<AdapterEvent, { type: "incomplete" }> | undefined;
1713
- let endTurn: boolean | undefined;
1714
- let stopReason: string | undefined;
1715
- // The adapter's stop reason exactly as it arrived. `stopReason` above is deliberately narrowed
1716
- // to the two reasons that map onto a Responses `incomplete_details`; the raw value is what the
1717
- // truncation guard needs, because adapters disagree on vocabulary (`length`, `refusal`, ...).
1718
- let rawStopReason: string | undefined;
1719
- let cleanDone = false;
1720
- // Whether the adapter emitted ANY terminal (done/error/incomplete). Distinct from `cleanDone`,
1721
- // which is only true for a `done` without a stop reason. A buffered turn whose adapter simply
1722
- // stopped emitting has no terminal at all, and must not be reported as a success.
1723
- let sawTerminal = false;
1724
- let batchCompaction = emptyChunks();
1725
- let compactionEncryptedContent: string | undefined;
1726
-
1727
- let currentText = emptyChunks();
1728
- let currentTextPhase: OcxMessagePhase | undefined;
1729
- let currentSummaryReasoning = emptyChunks();
1730
- let currentRawReasoning = emptyChunks();
1731
- // Same replay-cache handoff as the streaming path (issue #950): the most
1732
- // recently flushed raw reasoning waits for the tool call it preceded.
1733
- let rawReasoningForNextToolCall = "";
1734
- // Anthropic extended-thinking round-trip (batch): see bridgeToResponsesSSE counterpart.
1735
- let batchSignature: string | undefined;
1736
- let batchSignatureBytes = 0;
1737
- let batchRedacted: string[] = [];
1738
- let batchRedactedBytes = 0;
1739
- // Kiro reasoning blob, held until after the trailing flushes so it lands AFTER the assistant
1740
- // message (see the streaming path). Retained because it outlives releaseTranslatedEvent.
1741
- let batchKiroRedacted: string | undefined;
1742
- let batchKiroRedactedBytes = 0;
1743
- let currentToolCallId = "";
1744
- let currentToolCallName = "";
1745
- let currentToolCallCodeModeHelperName: string | undefined;
1746
- let currentToolCallArgs = "";
1747
- let currentToolCallProviderMetadata: OcxProviderOpaqueToolCallMetadata | undefined;
1748
- let currentToolCallArgsBytes = 0;
1749
- // Web-search citations awaiting the next assistant message (attached as url_citation annotations).
1750
- let pendingWebSources: { url: string; title?: string }[] = [];
1751
-
1752
- const freeformInput = (
1753
- args: string,
1754
- toolName: string,
1755
- namespace?: string,
1756
- codeModeHelperName?: string,
1757
- ): string => {
1758
- const helper = resolveCodeModeHelperName(codeModeHelperName, toolName, args, namespace, options?.declaredToolNames);
1759
- return helper
1760
- ? compileCodeModeHelperInput(args, helper)
1761
- : repairFreeformToolInput(args, toolName, namespace);
1762
- };
1763
- const parseArgsObj = (args: string): Record<string, unknown> => {
1764
- try { const o = JSON.parse(args); return o && typeof o === "object" ? o : {}; } catch { return {}; }
1765
- };
1766
-
1767
- const flushText = (inferredPhase?: OcxMessagePhase) => {
1768
- const currentTextStr = joinChunks(currentText);
1769
- if (!currentTextStr) return;
1770
- const phase = currentTextPhase ?? inferredPhase;
1771
- // ChatGPT-backend citation markers arrive as literal private-use characters that the
1772
- // Codex TUI prints verbatim (#3150). Strip them here rather than at the accumulator so
1773
- // the retained byte accounting above still describes what the upstream actually sent.
1774
- const text = stripCitationMarkers(currentTextStr);
1775
- const sourceBytes = pendingWebSources.reduce((sum, source) => sum + bytesOf(JSON.stringify(source)), 0);
1776
- const annotations = pendingWebSources.map(s => ({
1777
- type: "url_citation", url: s.url, ...(s.title ? { title: s.title } : {}), start_index: 0, end_index: 0,
1778
- }));
1779
- pendingWebSources = [];
1780
- const item = {
1781
- type: "message", id: `msg_${uuid()}`, role: "assistant", status: "completed",
1782
- content: [{ type: "output_text", text, annotations }],
1783
- ...(phase ? { phase } : {}),
1784
- } as OutputItem;
1785
- pushOutput(item, currentText.bytes);
1786
- budget?.releaseRetained(sourceBytes, { kind: "tool_search_sources" });
1787
- currentText = emptyChunks();
1788
- currentTextPhase = undefined;
1789
- };
1790
- const flushSummaryReasoning = () => {
1791
- const summaryText = joinChunks(currentSummaryReasoning);
1792
- if (!summaryText && !batchSignature && batchRedacted.length === 0) return;
1793
- const envelope: ReasoningEnvelope = {};
1794
- if (batchSignature) envelope.sig = batchSignature;
1795
- if (batchRedacted.length > 0) envelope.red = batchRedacted;
1796
- const hidden = options?.hideThinkingSummary === true;
1797
- if (hidden && summaryText && (envelope.sig || envelope.red)) envelope.txt = summaryText;
1798
- const encrypted = envelope.sig || envelope.red || envelope.txt ? encodeReasoningEnvelope(envelope, budget) : undefined;
1799
- const sourceBytes = currentSummaryReasoning.bytes + batchSignatureBytes + batchRedactedBytes;
1800
- batchSignature = undefined;
1801
- batchSignatureBytes = 0;
1802
- batchRedacted = [];
1803
- batchRedactedBytes = 0;
1804
- if (hidden && !encrypted) {
1805
- budget?.releaseRetained(sourceBytes, { kind: "reasoning" });
1806
- currentSummaryReasoning = emptyChunks();
1807
- return;
1808
- }
1809
- const item = {
1810
- type: "reasoning", id: `rs_${uuid()}`,
1811
- summary: !hidden && summaryText ? [{ type: "summary_text", text: summaryText }] : [],
1812
- ...(encrypted ? { encrypted_content: encrypted } : {}),
1813
- } as OutputItem;
1814
- pushOutput(item, sourceBytes, "reasoning");
1815
- currentSummaryReasoning = emptyChunks();
1816
- };
1817
- const flushRawReasoning = () => {
1818
- const rawText = joinChunks(currentRawReasoning);
1819
- if (!rawText) return;
1820
- rawReasoningForNextToolCall = rawText;
1821
- if (options?.hideThinkingSummary === true) {
1822
- // Same contract as the streaming path: no visible reasoning, txt-only envelope round-trip.
1823
- pushOutput({
1824
- type: "reasoning", id: `rs_${uuid()}`, summary: [],
1825
- encrypted_content: encodeReasoningEnvelope({ txt: rawText }, budget),
1826
- }, currentRawReasoning.bytes, "reasoning");
1827
- currentRawReasoning = emptyChunks();
1828
- return;
1829
- }
1830
- pushOutput({
1831
- type: "reasoning", id: `rs_${uuid()}`,
1832
- summary: [],
1833
- content: [{ type: "reasoning_text", text: rawText }],
1834
- }, currentRawReasoning.bytes, "reasoning");
1835
- currentRawReasoning = emptyChunks();
1836
- };
1837
- const flushToolCall = (status: "completed" | "incomplete" = "completed") => {
1838
- if (!currentToolCallId) return;
1839
- const mapped = options?.toolNsMap?.get(currentToolCallName);
1840
- const realName = mapped?.name ?? currentToolCallName;
1841
- const ns = mapped?.namespace;
1842
- const toolSearch = options?.toolSearchToolNames?.has(realName) ?? false;
1843
- const freeform = !toolSearch && (mapped
1844
- ? mapped.freeform === true
1845
- : (options?.freeformToolNames?.has(realName) ?? false));
1846
- // #1611: same integral-float repair as the streaming path. Keyed by the wire name
1847
- // the request declared, which is the pre-namespace-mapping `currentToolCallName`.
1848
- const coercedArgs = coerceIntegerToolArguments(
1849
- currentToolCallArgs,
1850
- options?.toolParameterSchemas?.get(currentToolCallName),
1851
- ns === undefined ? realName : undefined,
1852
- );
1853
- // Freeform tools serialize as custom_tool_call without extra_content; remember the
1854
- // signature server-side regardless so the replayed call can be re-signed (#1735).
1855
- void rememberExtraContentForReplay(currentToolCallId, currentToolCallProviderMetadata, replayCacheScope);
1856
- if (toolSearch) {
1857
- pushOutput({
1858
- type: "tool_search_call", id: `tsc_${uuid()}`,
1859
- call_id: currentToolCallId, execution: "client",
1860
- arguments: parseArgsObj(coercedArgs), status,
1861
- });
1862
- } else if (freeform) {
1863
- pushOutput({
1864
- type: "custom_tool_call", id: `ctc_${uuid()}`,
1865
- call_id: currentToolCallId, name: realName,
1866
- ...(ns ? { namespace: ns } : {}),
1867
- input: freeformInput(currentToolCallArgs, realName, ns, currentToolCallCodeModeHelperName), status,
1868
- });
1869
- } else {
1870
- pushOutput({
1871
- type: "function_call", id: `fc_${uuid()}`,
1872
- call_id: currentToolCallId, name: realName,
1873
- arguments: coercedArgs || "{}", status,
1874
- ...(ns ? { namespace: ns } : {}),
1875
- ...(rememberAndSerializeExtraContent(currentToolCallId, currentToolCallProviderMetadata, replayCacheScope).extra ?? {}),
1876
- });
1877
- }
1878
- budget?.closeCall(currentToolCallId);
1879
- currentToolCallId = "";
1880
- currentToolCallName = "";
1881
- currentToolCallCodeModeHelperName = undefined;
1882
- currentToolCallProviderMetadata = undefined;
1883
- currentToolCallArgs = "";
1884
- currentToolCallArgsBytes = 0;
1885
- };
1886
-
1887
- for (const e of events) {
1888
- if (errorEvent) {
1889
- // Match streaming: once the turn fails, later parallel calls must not become executable
1890
- // completed output. Still release every retained event in order and preserve terminal usage.
1891
- if (e.type === "error" || e.type === "incomplete" || e.type === "done") {
1892
- usage = e.usage ?? usage;
1893
- }
1894
- if (budget) releaseTranslatedEvent(e, budget);
1895
- continue;
1896
- }
1897
- if (batchSignature !== undefined && e.type !== "thinking_signature" && e.type !== "heartbeat") {
1898
- flushSummaryReasoning();
1899
- }
1900
- switch (e.type) {
1901
- case "assistant_boundary":
1902
- flushText("commentary");
1903
- flushSummaryReasoning();
1904
- flushRawReasoning();
1905
- rawReasoningForNextToolCall = "";
1906
- flushToolCall();
1907
- break;
1908
- case "text_delta":
1909
- // Only flush on an explicit phase change. A later delta that omits `phase` must keep
1910
- // appending under the previously established phase.
1911
- if (currentText.bytes > 0 && e.phase !== undefined && currentTextPhase !== e.phase) flushText("commentary");
1912
- if (currentSummaryReasoning.bytes > 0) flushSummaryReasoning();
1913
- if (currentRawReasoning.bytes > 0) flushRawReasoning();
1914
- // Empty text deltas (batch chat responses always carry content, often "") must
1915
- // not wipe reasoning that precedes a tool call (#950 non-streaming path).
1916
- if (e.text.length > 0) rawReasoningForNextToolCall = "";
1917
- if (currentToolCallId) flushToolCall();
1918
- // Compaction turns keep the summary out of normal message output (replay dedup — see
1919
- // bridgeToResponsesSSE); it ships only inside the synthetic compaction item below.
1920
- if (options?.compaction) {
1921
- batchCompaction = appendBatchString(
1922
- batchCompaction, e.text, "retained_collectors",
1923
- );
1924
- }
1925
- else {
1926
- if (e.phase !== undefined) currentTextPhase = e.phase;
1927
- currentText = appendBatchString(
1928
- currentText, e.text, "retained_collectors",
1929
- );
1930
- }
1931
- break;
1932
- case "thinking_delta":
1933
- if (currentText.bytes > 0) flushText("commentary");
1934
- if (currentRawReasoning.bytes > 0) flushRawReasoning();
1935
- if (e.thinking.length > 0) rawReasoningForNextToolCall = "";
1936
- if (currentToolCallId) flushToolCall();
1937
- {
1938
- currentSummaryReasoning = appendBatchString(
1939
- currentSummaryReasoning, e.thinking, "reasoning",
1940
- );
1941
- }
1942
- break;
1943
- case "thinking_signature":
1944
- // Like streaming, retain the latest signature update until the next semantic
1945
- // event. Flushing every update would manufacture signature-only siblings.
1946
- batchSignatureBytes = replaceBatchRetainedString(batchSignatureBytes, e.signature, "reasoning");
1947
- batchSignature = e.signature;
1948
- break;
1949
- case "redacted_thinking":
1950
- flushText("commentary");
1951
- flushSummaryReasoning();
1952
- flushRawReasoning();
1953
- flushToolCall();
1954
- {
1955
- const dataBytes = bytesOf(e.data);
1956
- budget?.chargeRetained(dataBytes, { kind: "reasoning" });
1957
- batchRedactedBytes += dataBytes;
1958
- }
1959
- batchRedacted.push(e.data);
1960
- flushSummaryReasoning();
1961
- break;
1962
- case "kiro_redacted_reasoning":
1963
- // Stash only — pushed after the trailing flushes. One blob per turn, so last wins.
1964
- {
1965
- const dataBytes = bytesOf(e.data);
1966
- budget?.chargeRetained(dataBytes, { kind: "reasoning" });
1967
- if (batchKiroRedactedBytes > 0) budget?.releaseRetained(batchKiroRedactedBytes, { kind: "reasoning" });
1968
- batchKiroRedactedBytes = dataBytes;
1969
- }
1970
- batchKiroRedacted = e.data;
1971
- break;
1972
- case "reasoning_raw_delta":
1973
- if (currentText.bytes > 0) flushText("commentary");
1974
- if (currentSummaryReasoning.bytes > 0) flushSummaryReasoning();
1975
- if (currentToolCallId) flushToolCall();
1976
- {
1977
- currentRawReasoning = appendBatchString(
1978
- currentRawReasoning, e.text, "reasoning",
1979
- );
1980
- }
1981
- break;
1982
- case "tool_call_start": {
1983
- if (currentText.bytes > 0) flushText("commentary");
1984
- if (currentSummaryReasoning.bytes > 0) flushSummaryReasoning();
1985
- if (currentRawReasoning.bytes > 0) flushRawReasoning();
1986
- if (rawReasoningForNextToolCall) {
1987
- rememberReasoningForCall(e.id, rawReasoningForNextToolCall, replayCacheScope);
1988
- }
1989
- flushToolCall();
1990
- const effectiveName = normalizeDeclaredToolName(e.name, options?.declaredToolNames);
1991
- if (options?.declaredToolNames && !options.declaredToolNames.has(effectiveName)) {
1992
- errorEvent = {
1993
- type: "error",
1994
- message: `routed provider emitted undeclared client tool "${effectiveName}"; only request-declared tools may be called`,
1995
- status: 502,
1996
- errorType: "upstream_error",
1997
- };
1998
- break;
1999
- }
2000
- currentToolCallId = e.id;
2001
- budget?.openCall(e.id);
2002
- currentToolCallName = effectiveName;
2003
- currentToolCallCodeModeHelperName = effectiveName === "exec" && e.name !== effectiveName
2004
- ? e.name
2005
- : undefined;
2006
- currentToolCallArgs = "";
2007
- currentToolCallArgsBytes = 0;
2008
- currentToolCallProviderMetadata = e.providerMetadata;
2009
- break;
2010
- }
2011
- case "tool_call_delta":
2012
- {
2013
- ({ value: currentToolCallArgs, bytes: currentToolCallArgsBytes } = appendBatchStringDirect(
2014
- currentToolCallArgs, currentToolCallArgsBytes, e.arguments, "tool_args", currentToolCallId,
2015
- ));
2016
- }
2017
- break;
2018
- case "tool_call_end":
2019
- if (!toolCallArgumentsUsable(currentToolCallArgs) && currentToolCallId) {
2020
- // Mirror the streaming path: refuse to complete unusable arguments.
2021
- const mapped = options?.toolNsMap?.get(currentToolCallName);
2022
- const realName = mapped?.name ?? currentToolCallName;
2023
- const toolSearch = options?.toolSearchToolNames?.has(realName) ?? false;
2024
- const freeform = !toolSearch && (mapped
2025
- ? mapped.freeform === true
2026
- : (options?.freeformToolNames?.has(realName) ?? false));
2027
- if (!freeform && !toolSearch) {
2028
- flushToolCall("incomplete");
2029
- errorEvent = {
2030
- type: "error",
2031
- message: "upstream stream produced malformed tool call arguments",
2032
- status: 502,
2033
- errorType: "upstream_error",
2034
- };
2035
- break;
2036
- }
2037
- }
2038
- flushToolCall();
2039
- break;
2040
- case "web_search_call_begin":
2041
- // Batch/non-streaming output has no in_progress phase to animate — the search cell is a
2042
- // single finalized item, emitted on `end`. Begin is a no-op here.
2043
- break;
2044
- case "web_search_call_end": {
2045
- if (currentText.bytes > 0) flushText("commentary");
2046
- if (currentSummaryReasoning.bytes > 0) flushSummaryReasoning();
2047
- if (currentRawReasoning.bytes > 0) flushRawReasoning();
2048
- flushToolCall();
2049
- const safeSources = safeWebSearchSources(e.sources);
2050
- pushOutput({
2051
- type: "web_search_call", id: `ws_${uuid()}`, status: e.status ?? "completed",
2052
- action: webSearchAction(e.queries),
2053
- ...(safeSources.length > 0 ? { sources: safeSources } : {}),
2054
- });
2055
- if (safeSources.length > 0) {
2056
- for (const source of safeSources) {
2057
- if (appendSafeWebSearchSource(pendingWebSources, source)) {
2058
- budget?.chargeRetained(bytesOf(JSON.stringify(source)), { kind: "tool_search_sources" });
2059
- }
2060
- }
2061
- }
2062
- break;
2063
- }
2064
- case "error":
2065
- errorEvent = e;
2066
- sawTerminal = true;
2067
- usage = e.usage ?? usage;
2068
- break;
2069
- case "incomplete":
2070
- incompleteEvent = e;
2071
- sawTerminal = true;
2072
- endTurn = e.endTurn;
2073
- if (e.providerState) options?.onProviderState?.(e.providerState);
2074
- break;
2075
- case "done":
2076
- usage = e.usage;
2077
- compactionEncryptedContent = e.compactionEncryptedContent;
2078
- sawTerminal = true;
2079
- endTurn = e.endTurn;
2080
- cleanDone = e.stopReason === undefined;
2081
- rawStopReason = e.stopReason;
2082
- if (e.providerState) options?.onProviderState?.(e.providerState);
2083
- // Match streaming: max_tokens and content_filter both terminate as incomplete.
2084
- // Normalize every adapter's truncation vocabulary to the canonical pair, so a raw
2085
- // `length` or `refusal` reaches the status/incomplete_details logic below instead of
2086
- // silently reading as a clean stop.
2087
- {
2088
- const truncation = truncationReasonFor(e.stopReason);
2089
- if (truncation) stopReason = truncation === "max_output_tokens" ? "max_tokens" : "content_filter";
2090
- }
2091
- break;
2092
- }
2093
- if (budget) releaseTranslatedEvent(e, budget);
2094
- }
2095
- flushText(cleanDone && !errorEvent && !incompleteEvent ? "final_answer" : undefined);
2096
- if (pendingWebSources.length > 0) {
2097
- const sourceBytes = pendingWebSources.reduce((sum, source) => sum + bytesOf(JSON.stringify(source)), 0);
2098
- pendingWebSources = [];
2099
- budget?.releaseRetained(sourceBytes, { kind: "tool_search_sources" });
2100
- }
2101
- flushSummaryReasoning();
2102
- flushRawReasoning();
2103
- // Open tool call on a failed/incomplete turn must not land as status:"completed" — and neither
2104
- // must one left open by a stream that stopped without any terminal at all. That case previously
2105
- // fell through to "completed", handing back a function_call whose arguments were half-written
2106
- // JSON, inside a turn also marked completed.
2107
- if (currentToolCallId) {
2108
- flushToolCall(errorEvent || incompleteEvent || !sawTerminal || isTruncatedStopReason(rawStopReason)
2109
- ? "incomplete" : "completed");
2110
- }
2111
- if (batchKiroRedacted) {
2112
- // pushOutput reserves the item itself and releases the retained raw blob it replaces.
2113
- pushOutput({
2114
- type: "reasoning", id: `rs_${uuid()}`, summary: [],
2115
- encrypted_content: encodeReasoningEnvelope({ krc: batchKiroRedacted }, budget),
2116
- }, batchKiroRedactedBytes, "reasoning");
2117
- batchKiroRedacted = undefined;
2118
- batchKiroRedactedBytes = 0;
2119
- }
2120
- // A truncated turn must never be installed as replacement history: emit the
2121
- // compaction item only when the turn actually completed (#422).
2122
- if (
2123
- options?.compaction
2124
- && !errorEvent
2125
- && !incompleteEvent
2126
- // A stream that stopped without any terminal did not complete either. The original guard
2127
- // could only see explicit failure events, so an adapter EOF slipped past it and installed a
2128
- // truncated summary as replacement history — the exact #422 hazard, reached by a route that
2129
- // did not exist when the guard was written.
2130
- && sawTerminal
2131
- && !isTruncatedStopReason(rawStopReason)
2132
- ) {
2133
- const item = {
2134
- type: "compaction", id: `cmp_${uuid()}`,
2135
- encrypted_content: compactionEncryptedContent ?? encodeCompactionSummary(joinChunks(batchCompaction)),
2136
- };
2137
- pushOutput(item, compactionEncryptedContent ? bytesOf(compactionEncryptedContent) : batchCompaction.bytes);
2138
- }
2139
-
2140
- const failure = errorEvent ? adapterFailureFromEvent(errorEvent) : undefined;
2141
- const status = errorEvent
2142
- ? "failed"
2143
- : incompleteEvent || stopReason === "max_tokens" || stopReason === "content_filter"
2144
- ? "incomplete"
2145
- : sawTerminal
2146
- ? "completed"
2147
- // The adapter stopped emitting without any terminal, so the turn was cut short. Streaming
2148
- // already reports this as response.incomplete / adapter_eof (see the !terminated branch);
2149
- // defaulting the buffered path to "completed" handed callers a truncated turn — including
2150
- // one carrying a never-closed tool call with half-written JSON arguments — as a success.
2151
- : "incomplete";
2152
- options?.onUsage?.(incompleteEvent?.usage ?? usage);
2153
- return {
2154
- id: responseId, object: "response",
2155
- created_at: Math.floor(Date.now() / 1000),
2156
- status,
2157
- model: modelId, output,
2158
- ...(endTurn !== undefined ? { end_turn: endTurn } : {}),
2159
- ...(failure ? { error: failure.error, last_error: failure.error } : {}),
2160
- ...(failure && isCyberPolicyCode(failure.error.code)
2161
- ? { retryable: false }
2162
- : errorEvent?.retryable !== undefined ? { retryable: errorEvent.retryable } : {}),
2163
- ...(incompleteEvent ? {
2164
- incomplete_details: {
2165
- reason: incompleteEvent.reason,
2166
- ...(incompleteEvent.message ? { message: incompleteEvent.message } : {}),
2167
- ...(incompleteEvent.retryable !== undefined ? { retryable: incompleteEvent.retryable } : {}),
2168
- },
2169
- } : stopReason === "max_tokens" ? {
2170
- incomplete_details: { reason: "max_output_tokens" },
2171
- } : stopReason === "content_filter" ? {
2172
- incomplete_details: { reason: "content_filter" },
2173
- } : !sawTerminal ? {
2174
- // Same reason string the streaming path uses, so a caller sees one signal for one condition
2175
- // regardless of which surface it asked for.
2176
- incomplete_details: { reason: "adapter_eof" },
2177
- } : {}),
2178
- usage: responsesUsage(incompleteEvent?.usage ?? usage),
2179
- };
2180
- }
2181
-
2182
- export function formatErrorResponse(
2183
- status: number,
2184
- type: string,
2185
- message: string,
2186
- options?: { code?: string | null; retryAfter?: string | null },
2187
- ): Response {
2188
- const error = classifyError(status, type, message);
2189
- if (isCyberPolicyCode(options?.code)) {
2190
- error.code = CYBER_POLICY_ERROR_CODE;
2191
- error.type = cyberPolicyErrorType(type);
2192
- }
2193
- const finalStatus = error.code === CYBER_POLICY_ERROR_CODE ? 400 : status;
2194
- const headers = new Headers({ "Content-Type": "application/json" });
2195
- const retryAfter = options?.retryAfter?.trim();
2196
- if (error.code !== CYBER_POLICY_ERROR_CODE
2197
- && retryAfter
2198
- && retryAfter.length > 0
2199
- && retryAfter.length <= 128) {
2200
- headers.set("Retry-After", retryAfter);
2201
- }
2202
- return new Response(JSON.stringify({ error }), {
2203
- status: finalStatus,
2204
- headers,
2205
- });
2206
- }
3
+ export { formatErrorResponse } from "./bridge/errors";
4
+ export { setOwnedBudgetAbandonedMsForTests } from "./bridge/internal";
5
+ export { buildResponseJSON } from "./bridge/response-json";
6
+ export { bridgeToResponsesSSE } from "./bridge/sse";
7
+ export type { ResponsesTerminalStatus } from "./bridge/sse";