@bitkyc08/opencodex 2.99.0 → 2.100.1

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 (276) hide show
  1. package/AGENTS_INSTALL.md +19 -1
  2. package/README.md +37 -33
  3. package/SPONSORS.md +5 -1
  4. package/bin/ocx.mjs +1 -1
  5. package/gui/dist/assets/{App-C1hGwaN1.css → App-COo8Yopd.css} +1 -1
  6. package/gui/dist/assets/App-g3Cm2xhZ.js +59 -0
  7. package/gui/dist/assets/Tray-BMDkAiXy.js +1 -0
  8. package/gui/dist/assets/brand-mark-nr3ziR-T.js +1 -0
  9. package/gui/dist/assets/index-Bziu8gTy.css +2 -0
  10. package/gui/dist/assets/index-OYzhQUNP.js +94 -0
  11. package/gui/dist/assets/{segment-thumb-features-fYEMxBaW.js → segment-thumb-features-DKCVUUTi.js} +1 -1
  12. package/gui/dist/classic/assets/App-CgPBKUP-.js +58 -0
  13. package/gui/dist/classic/assets/{Tray-BqZueZOx.js → Tray-BedmOsE7.js} +1 -1
  14. package/gui/dist/classic/assets/index-BR-HhNrp.js +94 -0
  15. package/gui/dist/classic/assets/quota-summary-CXKhvSmQ.js +1 -0
  16. package/gui/dist/classic/index.html +1 -1
  17. package/gui/dist/index.html +2 -2
  18. package/native/ocx-child/Cargo.lock +215 -0
  19. package/native/ocx-child/Cargo.toml +31 -0
  20. package/native/ocx-child/src/cli.rs +1901 -0
  21. package/native/ocx-child/src/codex_home.rs +429 -0
  22. package/native/ocx-child/src/codex_settings.rs +231 -0
  23. package/native/ocx-child/src/daemon.rs +363 -0
  24. package/native/ocx-child/src/http_wire.rs +619 -0
  25. package/native/ocx-child/src/journal.rs +977 -0
  26. package/native/ocx-child/src/lib.rs +22 -0
  27. package/native/ocx-child/src/main.rs +9 -0
  28. package/native/ocx-child/src/parity.rs +102 -0
  29. package/native/ocx-child/src/platform.rs +1487 -0
  30. package/native/ocx-child/src/relay.rs +1288 -0
  31. package/native/ocx-child/src/relay_auth.rs +166 -0
  32. package/native/ocx-child/src/routes.rs +236 -0
  33. package/native/ocx-child/src/state.rs +190 -0
  34. package/native/ocx-child/src/test_hooks.rs +58 -0
  35. package/native/ocx-child/src/toml_surgeon.rs +1156 -0
  36. package/package.json +8 -2
  37. package/src/adapters/anthropic-image-normalize.ts +1 -1
  38. package/src/adapters/anthropic.ts +28 -2
  39. package/src/adapters/claude-cli/adapter.ts +5 -25
  40. package/src/adapters/claude-cli/instruction-images.ts +31 -0
  41. package/src/adapters/claude-cli/stable-replay.ts +42 -14
  42. package/src/adapters/claude-cli/usage-admission.ts +53 -64
  43. package/src/adapters/client-fingerprint.ts +1 -1
  44. package/src/adapters/coding-agent/protocol.ts +1 -1
  45. package/src/adapters/devin/live-models.ts +3 -0
  46. package/src/adapters/google-http.ts +3 -0
  47. package/src/adapters/google-vertex-key-project.ts +10 -2
  48. package/src/adapters/google.ts +40 -8
  49. package/src/adapters/kiro/usage.ts +3 -13
  50. package/src/adapters/openai-chat/mistral-image-limit.ts +28 -15
  51. package/src/adapters/openai-chat-images.ts +100 -0
  52. package/src/adapters/openai-chat.ts +6 -9
  53. package/src/adapters/openai-responses/passthrough.ts +5 -2
  54. package/src/adapters/openai-responses/request-strips.ts +32 -0
  55. package/src/adapters/openai-responses/tool-schema.ts +9 -4
  56. package/src/chat/inbound.ts +1 -1
  57. package/src/claude/auth-detect.ts +26 -8
  58. package/src/claude/inbound-content-options.ts +18 -3
  59. package/src/claude/inbound.ts +5 -2
  60. package/src/cli/bundled-bun-diagnostic.ts +91 -0
  61. package/src/cli/capabilities-agents-routing.ts +2 -2
  62. package/src/cli/capabilities-observe-system.ts +60 -0
  63. package/src/cli/combo-input.ts +6 -0
  64. package/src/cli/combo-result.ts +1 -1
  65. package/src/cli/combo.ts +4 -0
  66. package/src/cli/config-command.ts +16 -2
  67. package/src/cli/connect.ts +12 -10
  68. package/src/cli/dispatch.ts +4 -0
  69. package/src/cli/doctor.ts +11 -0
  70. package/src/cli/help.ts +1 -0
  71. package/src/cli/init.ts +4 -0
  72. package/src/cli/link-enrollment.ts +152 -25
  73. package/src/cli/link-local-control.ts +49 -0
  74. package/src/cli/link.ts +37 -10
  75. package/src/cli/provider-lifecycle-runtime.ts +4 -2
  76. package/src/cli/registry.ts +13 -0
  77. package/src/cli/star-prompt.ts +2 -0
  78. package/src/cli/status.ts +1 -1
  79. package/src/cli/telemetry-profile.ts +206 -0
  80. package/src/cli/telemetry.ts +260 -0
  81. package/src/client/hub-relay.ts +26 -0
  82. package/src/client/machine-api.ts +3 -0
  83. package/src/clients/config-export/t3code.ts +44 -5
  84. package/src/clients/omo-role-models.ts +1 -1
  85. package/src/codex/catalog/bundled.ts +44 -2
  86. package/src/codex/runtime.ts +12 -0
  87. package/src/codex/shim-restore-lock.ts +67 -30
  88. package/src/combos/types.ts +21 -1
  89. package/src/config/atomic-write.ts +24 -1
  90. package/src/config/diagnostics.ts +15 -0
  91. package/src/config/live-reconcile.ts +17 -0
  92. package/src/config/load-degrade.ts +16 -0
  93. package/src/config/provider-validation.ts +29 -0
  94. package/src/config/schema/config-schema.ts +24 -0
  95. package/src/config/schema/leaf-validators.ts +17 -0
  96. package/src/config/telemetry-carry.ts +30 -0
  97. package/src/config.ts +13 -1
  98. package/src/generated/compatibility-version.json +422 -170
  99. package/src/generated/model-metadata.ts +3 -3
  100. package/src/integrations/aside-profiles.ts +7 -1
  101. package/src/integrations/owned-refresh.ts +9 -2
  102. package/src/lab/artifacts/secure-fs.ts +12 -11
  103. package/src/lab/fabric/scratch.ts +11 -9
  104. package/src/lab/public/community.ts +2 -3
  105. package/src/lab/public/file-safety.ts +18 -20
  106. package/src/lab/public/mutation-lock.ts +109 -337
  107. package/src/lab/public/private-file.ts +4 -3
  108. package/src/lib/bun-runtime-preflight.ts +1 -1
  109. package/src/lib/codex-catalog-read-only.ts +122 -0
  110. package/src/lib/config-ownership.ts +56 -18
  111. package/src/lib/errors.ts +11 -0
  112. package/src/lib/gcp-adc.ts +167 -48
  113. package/src/lib/jsonc.ts +4 -2
  114. package/src/lib/local-control-socket.ts +88 -0
  115. package/src/lib/privacy.ts +15 -0
  116. package/src/lib/provider-client-headers.ts +3 -0
  117. package/src/lib/token-estimate.ts +13 -2
  118. package/src/link/child-binary-source.ts +228 -0
  119. package/src/link/ephemeral-child.ts +45 -0
  120. package/src/link/remote-plant.ts +306 -0
  121. package/src/link/remote-windows.ts +59 -5
  122. package/src/link/ssh-argv.ts +71 -8
  123. package/src/link/status-projection.ts +2 -0
  124. package/src/link/store.ts +5 -1
  125. package/src/oauth/anthropic-model-quota.ts +12 -0
  126. package/src/oauth/anthropic-routing.ts +11 -4
  127. package/src/oauth/claude-keychain-read.ts +165 -0
  128. package/src/oauth/kiro-iam-identity-center.ts +4 -1
  129. package/src/oauth/local-token-detect.ts +14 -17
  130. package/src/protocols/envelope.ts +20 -2
  131. package/src/providers/api-key-selection.ts +8 -2
  132. package/src/providers/claude-cli-identity.ts +9 -3
  133. package/src/providers/claude-cli-usage-state.ts +93 -0
  134. package/src/providers/claude-cli-usage.ts +3 -2
  135. package/src/providers/github-copilot-auto.ts +25 -5
  136. package/src/providers/meta-muse-plan-refusal.ts +94 -0
  137. package/src/providers/model-rename-fields.ts +3 -0
  138. package/src/providers/quota/anthropic-family-headers.ts +8 -1
  139. package/src/providers/quota/anthropic-usage-windows.ts +49 -2
  140. package/src/providers/quota/vendor-probes-key.ts +23 -4
  141. package/src/providers/quota/vendor-probes-oauth.ts +59 -0
  142. package/src/providers/quota/workbuddy.ts +2 -2
  143. package/src/providers/quota-types.ts +7 -0
  144. package/src/providers/quota.ts +25 -18
  145. package/src/providers/registry/entries-core.ts +7 -4
  146. package/src/providers/registry/entries-extended.ts +42 -8
  147. package/src/providers/registry/model-ids.ts +1 -0
  148. package/src/providers/registry/model-seeds.ts +10 -2
  149. package/src/providers/registry/types.ts +6 -0
  150. package/src/providers/resolved-model-policy.ts +4 -0
  151. package/src/providers/routed-config.ts +30 -3
  152. package/src/providers/stale-no-vision-seed-migration.ts +7 -0
  153. package/src/providers/vertex-environment-authority.ts +50 -0
  154. package/src/providers/workbuddy-credit-balance.ts +38 -6
  155. package/src/quota-pool/assemble.ts +14 -6
  156. package/src/responses/retained-compaction.ts +31 -5
  157. package/src/router.ts +1 -0
  158. package/src/server/adapter-resolve.ts +11 -6
  159. package/src/server/audio-dictation.ts +21 -7
  160. package/src/server/auth-cors.ts +19 -0
  161. package/src/server/background-lifecycle.ts +71 -0
  162. package/src/server/chat-completions.ts +2 -0
  163. package/src/server/claude-messages.ts +30 -3
  164. package/src/server/index/live-sideband.ts +15 -1
  165. package/src/server/index/local-control-listener.ts +515 -0
  166. package/src/server/index/optional-listeners.ts +21 -1
  167. package/src/server/index/serve-options.ts +4 -3
  168. package/src/server/index/websocket-handler.ts +2 -0
  169. package/src/server/index.ts +3 -0
  170. package/src/server/management/combo-routes.ts +6 -1
  171. package/src/server/management/context.ts +1 -0
  172. package/src/server/management/link-child-preflight.ts +74 -0
  173. package/src/server/management/link-enrollment.ts +190 -25
  174. package/src/server/management/link-route-state.ts +6 -1
  175. package/src/server/management/link-routes.ts +27 -9
  176. package/src/server/management/model-rows.ts +4 -0
  177. package/src/server/management/oauth-account-routes.ts +9 -4
  178. package/src/server/management/provider-overwrite-carry.ts +9 -2
  179. package/src/server/management/provider-routes.ts +14 -0
  180. package/src/server/management/provider-thinking-display-patch.ts +54 -0
  181. package/src/server/management/route-registry.ts +7 -0
  182. package/src/server/management/session-routes.ts +4 -1
  183. package/src/server/management/telemetry-routes.ts +37 -0
  184. package/src/server/management/usage-aggregate-cache.ts +290 -67
  185. package/src/server/management-api.ts +11 -2
  186. package/src/server/messages-native-preflight.ts +166 -0
  187. package/src/server/messages-native.ts +143 -43
  188. package/src/server/messages-shared.ts +14 -0
  189. package/src/server/proxy-liveness.ts +99 -6
  190. package/src/server/request-log-conversation.ts +18 -2
  191. package/src/server/request-log.ts +7 -2
  192. package/src/server/responses/adapter-continuation.ts +1 -0
  193. package/src/server/responses/adapter-dispatch.ts +11 -28
  194. package/src/server/responses/adapter-failure-message.ts +68 -0
  195. package/src/server/responses/combo-stream-preflight.ts +14 -2
  196. package/src/server/responses/compact.ts +13 -1
  197. package/src/server/responses/core-auth.ts +26 -5
  198. package/src/server/responses/core-codex-account.ts +8 -1
  199. package/src/server/responses/core-combo-failure.ts +6 -1
  200. package/src/server/responses/core-combo-native.ts +150 -37
  201. package/src/server/responses/core-combo.ts +15 -9
  202. package/src/server/responses/core-normalize.ts +1 -0
  203. package/src/server/responses/core-options.ts +7 -1
  204. package/src/server/responses/core-pipeline.ts +21 -1
  205. package/src/server/responses/fetch-helpers.ts +5 -0
  206. package/src/server/responses/opengateway-completion-guard.ts +7 -3
  207. package/src/server/responses/passthrough-delivery.ts +8 -1
  208. package/src/server/responses/passthrough-error.ts +22 -0
  209. package/src/server/responses/request-prepare.ts +7 -1
  210. package/src/server/responses/request-transport.ts +28 -5
  211. package/src/server/responses/run-turn-execution.ts +13 -0
  212. package/src/server/sse-frame-buffer.ts +5 -0
  213. package/src/server/ws-bridge.ts +10 -0
  214. package/src/service/launchd.ts +78 -4
  215. package/src/telemetry/aggregate.ts +348 -0
  216. package/src/telemetry/consent.ts +128 -0
  217. package/src/telemetry/contract.ts +403 -0
  218. package/src/telemetry/disclosure.ts +19 -0
  219. package/src/telemetry/install-id.ts +226 -0
  220. package/src/telemetry/payload.ts +129 -0
  221. package/src/telemetry/profile/client.ts +150 -0
  222. package/src/telemetry/profile/contract.ts +288 -0
  223. package/src/telemetry/profile/disclosure.ts +14 -0
  224. package/src/telemetry/profile/paths.ts +13 -0
  225. package/src/telemetry/profile/routes.ts +99 -0
  226. package/src/telemetry/profile/service.ts +234 -0
  227. package/src/telemetry/profile/state.ts +189 -0
  228. package/src/telemetry/profile/sync.ts +276 -0
  229. package/src/telemetry/routes.ts +183 -0
  230. package/src/telemetry/scheduler.ts +386 -0
  231. package/src/telemetry/sender.ts +97 -0
  232. package/src/telemetry/state.ts +334 -0
  233. package/src/types/combo-jev-effort-mode.ts +11 -0
  234. package/src/types/config.ts +3 -0
  235. package/src/types/provider.ts +11 -1
  236. package/src/types/request.ts +2 -0
  237. package/src/types/telemetry.ts +15 -0
  238. package/src/types/wire.ts +10 -0
  239. package/src/types.ts +5 -0
  240. package/src/update/index.ts +7 -1
  241. package/src/update/minisign.d.mts +17 -0
  242. package/src/update/minisign.mjs +88 -0
  243. package/src/update/signed-supply.d.mts +59 -0
  244. package/src/update/signed-supply.mjs +289 -0
  245. package/src/update/trust-root.json +38 -0
  246. package/src/update/update-bounded-json.d.mts +4 -0
  247. package/src/update/update-bounded-json.mjs +113 -0
  248. package/src/update/update-bytes.d.mts +2 -0
  249. package/src/update/update-bytes.mjs +23 -0
  250. package/src/update/update-candidate-id.d.mts +2 -0
  251. package/src/update/update-candidate-id.mjs +23 -0
  252. package/src/update/update-checkpoint-codec.d.mts +33 -0
  253. package/src/update/update-checkpoint-codec.mjs +217 -0
  254. package/src/update/update-contracts.d.mts +24 -0
  255. package/src/update/update-contracts.mjs +207 -0
  256. package/src/update/update-signature-threshold.d.mts +12 -0
  257. package/src/update/update-signature-threshold.mjs +47 -0
  258. package/src/update/update-signed-version.d.mts +5 -0
  259. package/src/update/update-signed-version.mjs +25 -0
  260. package/src/update/update-trust-anchor.d.mts +2 -0
  261. package/src/update/update-trust-anchor.mjs +2 -0
  262. package/src/usage/cache-diagnostic.ts +195 -50
  263. package/src/usage/expected-prices.ts +20 -2
  264. package/src/usage/filter-index.ts +1469 -0
  265. package/src/usage/ledger-scanner.ts +245 -4
  266. package/src/usage/source-filter.ts +10 -2
  267. package/src/usage/summary.ts +16 -0
  268. package/src/web-search/parse.ts +265 -38
  269. package/gui/dist/assets/App-BepstgEN.js +0 -59
  270. package/gui/dist/assets/Tray-BinxbEZo.js +0 -1
  271. package/gui/dist/assets/brand-mark-wzPe3R-J.js +0 -1
  272. package/gui/dist/assets/index-C-9X2yaI.js +0 -94
  273. package/gui/dist/assets/index-bnUWypyz.css +0 -2
  274. package/gui/dist/classic/assets/App-DX88_GCW.js +0 -58
  275. package/gui/dist/classic/assets/index-fSP71pLB.js +0 -94
  276. package/gui/dist/classic/assets/quota-summary-y2fl0WBv.js +0 -1
@@ -27,6 +27,7 @@ interface OutputTextBlock {
27
27
  annotations?: AnnotationLike[];
28
28
  }
29
29
  interface OutputItem {
30
+ id?: string;
30
31
  type?: string;
31
32
  content?: OutputTextBlock[];
32
33
  }
@@ -34,53 +35,224 @@ interface OutputItem {
34
35
  * Completed output text and citations. `parts` holds each output_text block's text keyed by
35
36
  * `output_index:content_index`, and `incomplete` marks skipped malformed entries.
36
37
  */
37
- type CompletedOutput = WebSearchResult & { incomplete: boolean; parts: Map<string, string> };
38
+ type CompletedOutput = WebSearchResult & {
39
+ incomplete: boolean;
40
+ parts: Map<string, string>;
41
+ slots: Map<string, { itemId?: string; outputIndex: number; contentIndex: number; singleContent: boolean }>;
42
+ /** Unique message positions survive malformed content; null marks a repeated item ID. */
43
+ items: Map<string, { outputIndex: number; missingContent: boolean } | null>;
44
+ };
38
45
  /** Streamed text for one `output_text` content part. */
39
46
  interface StreamedPart {
47
+ key: string;
48
+ order: number;
40
49
  delta: string;
41
50
  done: string | null;
51
+ itemId?: string;
52
+ outputIndex?: number;
53
+ contentIndex?: number;
54
+ promotedFromContent?: boolean;
42
55
  }
43
56
 
44
57
  /** The streamed part for events that carry no valid `output_index`/`content_index`. */
45
58
  const UNINDEXED_PART = "unindexed";
46
59
 
60
+ // Identity buckets allow a unique compatible match without scanning prior parts on every frame.
61
+ type PartLookup = Map<string, Set<StreamedPart>>;
62
+
63
+ function partBucket(itemId: string, contentIndex: number | undefined, exact: boolean, unindexed: boolean, output?: number | null): string {
64
+ return JSON.stringify([itemId, contentIndex ?? null, exact, unindexed, output === undefined ? "any" : output]);
65
+ }
66
+
67
+ function indexPart(lookup: PartLookup, part: StreamedPart, add: boolean): void {
68
+ const positionBuckets = part.outputIndex !== undefined
69
+ ? [["output", part.outputIndex], ["output-content", part.outputIndex, part.contentIndex ?? null]]
70
+ : [["unpositioned-content", part.contentIndex ?? null]];
71
+ for (const positionBucket of positionBuckets) {
72
+ const key = JSON.stringify(positionBucket);
73
+ let bucket = lookup.get(key);
74
+ if (add) {
75
+ if (!bucket) { bucket = new Set(); lookup.set(key, bucket); }
76
+ bucket.add(part);
77
+ } else if (bucket) {
78
+ bucket.delete(part);
79
+ if (!bucket.size) lookup.delete(key);
80
+ }
81
+ }
82
+ if (!part.itemId) return;
83
+ for (const exact of [false, true]) {
84
+ for (const unindexed of isIndexedPartKey(part.key) ? [false] : [false, true]) {
85
+ for (const output of [undefined, part.outputIndex ?? null]) {
86
+ const key = partBucket(part.itemId, exact ? part.contentIndex : undefined, exact, unindexed, output);
87
+ let bucket = lookup.get(key);
88
+ if (add) {
89
+ if (!bucket) { bucket = new Set(); lookup.set(key, bucket); }
90
+ bucket.add(part);
91
+ } else if (bucket) {
92
+ bucket.delete(part);
93
+ if (!bucket.size) lookup.delete(key);
94
+ }
95
+ }
96
+ }
97
+ }
98
+ }
99
+
100
+ function identifiedPart(lookup: PartLookup, itemId: string, contentIndex: number | undefined, unindexed: boolean, outputIndex?: number): StreamedPart | undefined {
101
+ const outputs = outputIndex === undefined ? [undefined] : [outputIndex, null];
102
+ const buckets = outputs.flatMap(output => contentIndex === undefined
103
+ ? [lookup.get(partBucket(itemId, undefined, false, unindexed, output))]
104
+ : [lookup.get(partBucket(itemId, contentIndex, true, unindexed, output)), lookup.get(partBucket(itemId, undefined, true, unindexed, output))]);
105
+ if (buckets.reduce((count, bucket) => count + (bucket?.size ?? 0), 0) !== 1) return undefined;
106
+ return buckets.find(bucket => bucket?.size)?.values().next().value;
107
+ }
108
+
47
109
  /**
48
- * Key a streamed text event by its content part. An event without valid indexes joins the only
49
- * indexed part when exactly one exists. The first indexed event identifies any preceding
50
- * unindexed accumulator; once multiple indexed parts exist, unindexed text stays separate.
110
+ * Match explicit item identity before applying the legacy single-part fallback. Indexed parts
111
+ * retain their numeric keys so incomplete completed outputs can fill individual missing parts.
51
112
  */
52
- function streamedPartKey(data: Record<string, unknown>, parts: Map<string, StreamedPart>): string {
113
+ function streamedPartKey(data: Record<string, unknown>, parts: Map<string, StreamedPart>, lookup: PartLookup, nextOrder: number): string {
53
114
  const output = data.output_index;
54
115
  const content = data.content_index;
55
- if (Number.isInteger(output) && Number.isInteger(content)) {
56
- const key = `${output}:${content}`;
57
- if (parts.size === 1 && parts.has(UNINDEXED_PART)) {
58
- parts.set(key, parts.get(UNINDEXED_PART)!);
59
- parts.delete(UNINDEXED_PART);
116
+ const outputIndex = Number.isInteger(output) ? output as number : undefined;
117
+ const contentIndex = Number.isInteger(content) ? content as number : undefined;
118
+ const itemId = typeof data.item_id === "string" && data.item_id ? data.item_id : undefined;
119
+ const indexedKey = outputIndex !== undefined && contentIndex !== undefined ? `${outputIndex}:${contentIndex}` : undefined;
120
+ const outputOnly = outputIndex === undefined ? undefined : parts.get(`output:${outputIndex}`);
121
+ const compatible = (part: StreamedPart): boolean =>
122
+ (!itemId || !part.itemId || itemId === part.itemId)
123
+ && (outputIndex === undefined || part.outputIndex === undefined || outputIndex === part.outputIndex)
124
+ && (contentIndex === undefined || part.contentIndex === undefined || contentIndex === part.contentIndex);
125
+ const indexedPart = indexedKey ? parts.get(indexedKey) : undefined;
126
+ let match = itemId ? identifiedPart(lookup, itemId, contentIndex, !!indexedKey, outputIndex) : undefined;
127
+ if (!match && itemId && indexedPart?.itemId === itemId
128
+ && identifiedPart(lookup, itemId, contentIndex, false, outputIndex) === indexedPart) match = indexedPart;
129
+ if (match && !compatible(match)) match = undefined;
130
+ const outputBucket = outputIndex === undefined ? undefined : lookup.get(JSON.stringify(["output", outputIndex]));
131
+ const outputContentBucket = outputIndex === undefined || contentIndex === undefined ? undefined
132
+ : lookup.get(JSON.stringify(["output-content", outputIndex, contentIndex]));
133
+ const outputUnknownContentBucket = outputIndex === undefined ? undefined
134
+ : lookup.get(JSON.stringify(["output-content", outputIndex, null]));
135
+ const outputCandidates = contentIndex === undefined ? outputBucket?.size ?? 0
136
+ : (outputContentBucket?.size ?? 0) + (outputUnknownContentBucket?.size ?? 0);
137
+ // A linking snapshot can identify an anonymous position even when either frame omitted the
138
+ // content index. Only inspect the buckets when their total proves there are at most two parts.
139
+ if (data.type === "response.output_text.done" && itemId && match
140
+ && outputCandidates === (match.outputIndex === undefined ? 1 : 2)) {
141
+ const matchedContent = match.contentIndex;
142
+ const buckets = contentIndex === undefined ? [outputBucket] : [outputContentBucket, outputUnknownContentBucket];
143
+ const anonymous = buckets.flatMap(bucket => bucket ? [...bucket] : [])
144
+ .find(part => part !== match && !part.itemId && compatible(part)
145
+ && (matchedContent === undefined || part.contentIndex === undefined || matchedContent === part.contentIndex));
146
+ if (anonymous) {
147
+ indexPart(lookup, match, false);
148
+ parts.delete(match.key);
149
+ // The linking frame can omit a field that the identified draft already supplied.
150
+ if (anonymous.contentIndex === undefined && matchedContent !== undefined) {
151
+ indexPart(lookup, anonymous, false);
152
+ anonymous.contentIndex = matchedContent;
153
+ indexPart(lookup, anonymous, true);
154
+ }
155
+ return anonymous.key;
156
+ }
157
+ }
158
+ if (indexedPart && compatible(indexedPart)) {
159
+ if (match && match !== indexedPart) return match.key;
160
+ if ((itemId && indexedPart.itemId === itemId) || outputCandidates === 1) return indexedKey!;
161
+ }
162
+ if (outputCandidates > 1 && (indexedPart || outputOnly) && !match) return `conflict:${nextOrder}`;
163
+ if (!match && outputOnly && compatible(outputOnly) && outputCandidates === 1) match = outputOnly;
164
+ if (!match && outputIndex !== undefined && contentIndex === undefined) {
165
+ const only = outputBucket?.size === 1 ? outputBucket.values().next().value : undefined;
166
+ if (only && compatible(only)) match = only;
167
+ }
168
+ if (!match && !itemId && indexedKey && !indexedPart && outputCandidates === 0) {
169
+ const bucket = lookup.get(JSON.stringify(["unpositioned-content", contentIndex]));
170
+ const unknown = lookup.get(JSON.stringify(["unpositioned-content", null]));
171
+ if ((bucket?.size ?? 0) + (unknown?.size ?? 0) === 1) {
172
+ const candidate = (bucket ?? unknown)?.values().next().value;
173
+ if (candidate?.contentIndex !== undefined || candidate?.itemId || parts.size === 1) match = candidate;
174
+ }
175
+ }
176
+ // A content-only part can acquire its item or output identity after other parts arrived.
177
+ const contentOnly = contentIndex === undefined ? undefined : parts.get(`content:${contentIndex}`);
178
+ if (!match && (itemId || !indexedKey) && contentOnly && compatible(contentOnly)) match = contentOnly;
179
+ if (!match && parts.size === 1) {
180
+ const only = parts.values().next().value;
181
+ if (only && compatible(only) && (!indexedKey || !isIndexedPartKey(only.key))
182
+ && (!itemId || !only.itemId)) match = only;
183
+ }
184
+ if (match) {
185
+ const mergedOutput = outputIndex ?? match.outputIndex;
186
+ const mergedContent = contentIndex ?? match.contentIndex;
187
+ const mergedKey = mergedOutput === undefined ? undefined : mergedContent === undefined
188
+ ? `output:${mergedOutput}` : `${mergedOutput}:${mergedContent}`;
189
+ if (mergedKey && !parts.has(mergedKey) && match.key !== mergedKey) {
190
+ indexPart(lookup, match, false);
191
+ parts.delete(match.key);
192
+ if (match.key.startsWith("content:")) match.promotedFromContent = true;
193
+ match.key = mergedKey;
194
+ parts.set(mergedKey, match);
195
+ indexPart(lookup, match, true);
196
+ return mergedKey;
60
197
  }
61
- return key;
198
+ return match.key;
62
199
  }
63
- const indexed = [...parts.keys()].filter(key => key !== UNINDEXED_PART);
64
- return indexed.length === 1 ? indexed[0]! : UNINDEXED_PART;
200
+ if (indexedKey && !indexedPart) return indexedKey;
201
+ const key = outputIndex !== undefined && !outputOnly ? `output:${outputIndex}`
202
+ : itemId ? `item:${JSON.stringify([itemId, contentIndex ?? null])}`
203
+ : contentIndex !== undefined ? `content:${contentIndex}` : UNINDEXED_PART;
204
+ const existing = parts.get(key);
205
+ return !existing || compatible(existing) ? key : `conflict:${nextOrder}`;
206
+ }
207
+
208
+ function isIndexedPartKey(key: string): boolean {
209
+ return key !== UNINDEXED_PART && !key.startsWith("item:") && !key.startsWith("content:") && !key.startsWith("output:") && !key.startsWith("conflict:");
65
210
  }
66
211
 
67
- /** Order `output_index:content_index` keys numerically. */
68
- function comparePartKeys(left: string, right: string): number {
69
- const [leftOutput = 0, leftContent = 0] = left.split(":").map(Number);
70
- const [rightOutput = 0, rightContent = 0] = right.split(":").map(Number);
71
- return leftOutput - rightOutput || leftContent - rightContent;
212
+ /** Order known output indexes numerically; an unknown content index follows known siblings. */
213
+ function comparePartKeys(left: string, right: string, streamed: Map<string, StreamedPart>): number {
214
+ const position = (key: string): [number, number] => {
215
+ const part = streamed.get(key);
216
+ if (part?.outputIndex !== undefined) return [part.outputIndex, part.contentIndex ?? Infinity];
217
+ return key.split(":").map(Number) as [number, number];
218
+ };
219
+ const [leftOutput, leftContent] = position(left);
220
+ const [rightOutput, rightContent] = position(right);
221
+ return leftOutput - rightOutput || leftContent - rightContent
222
+ || (streamed.get(left)?.order ?? -1) - (streamed.get(right)?.order ?? -1);
72
223
  }
73
224
 
74
225
  /** One part's streamed text: its done text when present, otherwise its deltas. */
75
226
  function streamedPartText(part: StreamedPart): string {
76
- return part.done?.trim() ? part.done : part.delta;
227
+ return part.done ?? part.delta;
228
+ }
229
+
230
+ /** Keep earlier content-only siblings before a part promoted to an indexed identity. */
231
+ function orderedPartKeys(completed: Iterable<string>, streamed: Map<string, StreamedPart>): string[] {
232
+ const ordered = (key: string): boolean => isIndexedPartKey(key) || streamed.get(key)?.outputIndex !== undefined;
233
+ const indexed = [...new Set([...completed, ...streamed.keys()].filter(ordered))]
234
+ .sort((left, right) => comparePartKeys(left, right, streamed));
235
+ const contentOnly = [...streamed].filter(([key]) => key.startsWith("content:") && !ordered(key))
236
+ .sort((a, b) => a[1].order - b[1].order);
237
+ const keys: string[] = [];
238
+ let next = 0;
239
+ for (const key of indexed) {
240
+ const part = streamed.get(key);
241
+ if (part?.promotedFromContent) {
242
+ while (next < contentOnly.length && contentOnly[next]![1].order < part.order) keys.push(contentOnly[next++]![0]);
243
+ }
244
+ keys.push(key);
245
+ }
246
+ const emitted = new Set(contentOnly.slice(0, next).map(([key]) => key));
247
+ const remaining = [...streamed].filter(([key]) => !ordered(key) && !emitted.has(key));
248
+ remaining.sort((a, b) => a[1].order - b[1].order);
249
+ keys.push(...remaining.map(([key]) => key));
250
+ return keys;
77
251
  }
78
252
 
79
- /** Join indexed parts in index order, followed by any unindexed text. */
253
+ /** Join parts with known output indexes in order, then other parts in arrival order. */
80
254
  function assembleStreamedText(parts: Map<string, StreamedPart>): string {
81
- const keys = [...parts.keys()].filter(key => key !== UNINDEXED_PART).sort(comparePartKeys);
82
- if (parts.has(UNINDEXED_PART)) keys.push(UNINDEXED_PART);
83
- return keys.map(key => streamedPartText(parts.get(key)!)).join("");
255
+ return orderedPartKeys([], parts).map(key => streamedPartText(parts.get(key)!)).join("");
84
256
  }
85
257
 
86
258
  /**
@@ -88,7 +260,7 @@ function assembleStreamedText(parts: Map<string, StreamedPart>): string {
88
260
  * completed output has it, and otherwise takes that part's streamed text.
89
261
  */
90
262
  function mergeIncompleteOutput(completed: Map<string, string>, streamed: Map<string, StreamedPart>): string {
91
- const keys = [...new Set([...completed.keys(), ...streamed.keys()])].sort(comparePartKeys);
263
+ const keys = orderedPartKeys(completed.keys(), streamed);
92
264
  return keys.map(key => {
93
265
  const text = completed.get(key);
94
266
  if (text?.trim()) return text;
@@ -97,6 +269,45 @@ function mergeIncompleteOutput(completed: Map<string, string>, streamed: Map<str
97
269
  }).join("");
98
270
  }
99
271
 
272
+ /** Match completed slots to unique compatible streamed parts before filling missing output. */
273
+ function reconcileCompletedParts(final: CompletedOutput, streamed: Map<string, StreamedPart>, lookup: PartLookup): Map<string, StreamedPart> {
274
+ const reconciled = new Map(streamed);
275
+ const matched = new Set<StreamedPart>();
276
+ for (const [key, identity] of final.slots) {
277
+ const existing = reconciled.get(key);
278
+ let part = identity.itemId ? identifiedPart(lookup, identity.itemId, identity.contentIndex, true) : undefined;
279
+ if (existing) {
280
+ const atContent = lookup.get(JSON.stringify(["output-content", identity.outputIndex, identity.contentIndex]));
281
+ const atUnknownContent = lookup.get(JSON.stringify(["output-content", identity.outputIndex, null]));
282
+ const ambiguous = !identity.itemId && (atContent?.size ?? 0) + (atUnknownContent?.size ?? 0) > 1;
283
+ const different = identity.itemId && existing.itemId && identity.itemId !== existing.itemId;
284
+ const otherIdentified = identity.itemId && !existing.itemId && part && part !== existing;
285
+ if (!ambiguous && !different && !otherIdentified) continue;
286
+ // Preserve a conflicting or ambiguous stream beside the completed slot.
287
+ reconciled.delete(key);
288
+ reconciled.set(`conflict:completed:${existing.order}`, existing);
289
+ }
290
+ if (!part && identity.singleContent) {
291
+ const bucket = lookup.get(JSON.stringify(["output", identity.outputIndex]));
292
+ const candidate = bucket?.size === 1 ? bucket.values().next().value : undefined;
293
+ if (candidate?.key.startsWith("output:") && (!identity.itemId || !candidate.itemId || identity.itemId === candidate.itemId)) part = candidate;
294
+ }
295
+ if (part?.outputIndex !== undefined && part.outputIndex !== identity.outputIndex) continue;
296
+ if (!part || matched.has(part) || (part.contentIndex === undefined && !identity.singleContent)) continue;
297
+ matched.add(part);
298
+ reconciled.delete(part.key);
299
+ reconciled.set(key, { ...part, key });
300
+ }
301
+ for (const [key, part] of reconciled) {
302
+ const item = part.itemId ? final.items.get(part.itemId) : undefined;
303
+ if (item?.missingContent && part.outputIndex === undefined && !isIndexedPartKey(key)) {
304
+ // The message identifies an output position, but cannot invent a content index.
305
+ reconciled.set(key, { ...part, outputIndex: item.outputIndex });
306
+ }
307
+ }
308
+ return reconciled;
309
+ }
310
+
100
311
  // Keep this compatibility export for non-Responses sidecar executors and bounded error bodies.
101
312
  export const MAX_SIDECAR_RESPONSE_BYTES = 64 * 1024;
102
313
  // Responses SSE can spend far more wire bytes on JSON framing than on useful model output. Keep a
@@ -315,22 +526,30 @@ function fromOutputArray(output: OutputItem[], seen: Set<string>): CompletedOutp
315
526
  let text = "";
316
527
  let incomplete = false;
317
528
  const parts = new Map<string, string>();
529
+ const slots: CompletedOutput["slots"] = new Map();
530
+ const items: CompletedOutput["items"] = new Map();
318
531
  const sources: WebSearchSource[] = [];
319
532
  for (const [outputIndex, item] of output.entries()) {
320
533
  if (!item || typeof item !== "object") { incomplete = true; continue; }
321
534
  if (item.type !== "message") continue;
535
+ if (typeof item.id === "string" && item.id) {
536
+ items.set(item.id, items.has(item.id) ? null : { outputIndex, missingContent: !Array.isArray(item.content) });
537
+ }
322
538
  if (!Array.isArray(item.content)) { incomplete = true; continue; }
323
539
  for (const [contentIndex, block] of item.content.entries()) {
324
540
  if (!block || typeof block !== "object") { incomplete = true; continue; }
325
541
  if (block.type === "output_text") {
542
+ const key = `${outputIndex}:${contentIndex}`;
543
+ slots.set(key, { itemId: typeof item.id === "string" && item.id ? item.id : undefined,
544
+ outputIndex, contentIndex, singleContent: item.content.length === 1 });
326
545
  if (typeof block.text !== "string") { incomplete = true; continue; }
327
546
  text += block.text;
328
- parts.set(`${outputIndex}:${contentIndex}`, block.text);
547
+ parts.set(key, block.text);
329
548
  for (const ann of Array.isArray(block.annotations) ? block.annotations : []) collectAnnotation(ann, sources, seen);
330
549
  }
331
550
  }
332
551
  }
333
- return { text, sources, incomplete, parts };
552
+ return { text, sources, incomplete, parts, slots, items };
334
553
  }
335
554
 
336
555
  export function cancelReaderWithoutWaiting(
@@ -358,6 +577,8 @@ export async function parseSidecarSSE(response: Response): Promise<WebSearchResu
358
577
  let buffer = "";
359
578
  let responseBytes = 0;
360
579
  const seen = new Set<string>();
580
+ const partLookup: PartLookup = new Map();
581
+ let nextPartOrder = 0;
361
582
  // Holder object — fields are mutated inside the closure, so they can't live as narrowed locals.
362
583
  const acc: {
363
584
  parts: Map<string, StreamedPart>;
@@ -379,9 +600,14 @@ export async function parseSidecarSSE(response: Response): Promise<WebSearchResu
379
600
 
380
601
  /** The accumulator for the content part that `data` belongs to. */
381
602
  const streamedPart = (data: Record<string, unknown>): StreamedPart => {
382
- const key = streamedPartKey(data, acc.parts);
603
+ const key = streamedPartKey(data, acc.parts, partLookup, nextPartOrder);
383
604
  let part = acc.parts.get(key);
384
- if (!part) { part = { delta: "", done: null }; acc.parts.set(key, part); }
605
+ if (!part) { part = { key, order: nextPartOrder++, delta: "", done: null }; acc.parts.set(key, part); }
606
+ indexPart(partLookup, part, false);
607
+ if (typeof data.item_id === "string" && data.item_id) part.itemId = data.item_id;
608
+ if (Number.isInteger(data.output_index)) part.outputIndex = data.output_index as number;
609
+ if (Number.isInteger(data.content_index)) part.contentIndex = data.content_index as number;
610
+ indexPart(partLookup, part, true);
385
611
  return part;
386
612
  };
387
613
 
@@ -396,7 +622,7 @@ export async function parseSidecarSSE(response: Response): Promise<WebSearchResu
396
622
  };
397
623
 
398
624
  const handle = (payload: string): void => {
399
- if (!payload) return;
625
+ if (!payload || acc.terminalEvent) return;
400
626
  if (payload === "[DONE]") {
401
627
  acc.terminalEvent = true;
402
628
  return;
@@ -424,15 +650,12 @@ export async function parseSidecarSSE(response: Response): Promise<WebSearchResu
424
650
  if (acceptDecodedChars(data.delta.length)) streamedPart(data).delta += data.delta;
425
651
  } else if (type === "response.output_text.done" && typeof data.text === "string") {
426
652
  // The `done` event carries the full, authoritative text for one content part.
427
- if (acceptDecodedChars(data.text.length)) {
428
- const part = streamedPart(data);
429
- part.done = (part.done ?? "") + data.text;
430
- }
653
+ if (acceptDecodedChars(data.text.length)) streamedPart(data).done = data.text;
431
654
  } else if (type === "response.completed" || type === "response.done") {
432
655
  acc.terminalEvent = true;
433
656
  const resp = data.response as { output?: OutputItem[] } | undefined;
434
657
  if (Array.isArray(resp?.output)) {
435
- const final = fromOutputArray(resp.output, seen);
658
+ const final = fromOutputArray(resp.output, new Set<string>());
436
659
  if (acceptDecodedChars(final.text.length)) acc.final = final;
437
660
  }
438
661
  } else if (type === "response.failed" || type === "response.incomplete" || type === "error") {
@@ -500,17 +723,21 @@ export async function parseSidecarSSE(response: Response): Promise<WebSearchResu
500
723
 
501
724
  // Prefer the authoritative completed output[], then the done text, then accumulated deltas.
502
725
  // Each streamed content part contributes its done text, or its deltas when the done event never
503
- // arrived. A completed output that skipped malformed entries may be partial: indexed streams fill
504
- // only the parts it lacks, while an unindexed stream replaces it as a whole.
726
+ // arrived. A completed output that skipped malformed entries may be partial: identified parts
727
+ // can be matched by item ID, and indexed streams fill only the parts that output lacks.
505
728
  const streamedText = assembleStreamedText(acc.parts);
506
729
  const completedText = acc.final?.text.trim() ? acc.final.text : "";
507
730
  let text = completedText || streamedText;
508
731
  if (acc.final?.incomplete) {
509
- const repaired = acc.parts.has(UNINDEXED_PART) ? streamedText : mergeIncompleteOutput(acc.final.parts, acc.parts);
732
+ const reconciled = reconcileCompletedParts(acc.final, acc.parts, partLookup);
733
+ const repaired = [...reconciled].some(([key, part]) => isIndexedPartKey(key) || part.outputIndex !== undefined)
734
+ ? mergeIncompleteOutput(acc.final.parts, reconciled) : streamedText;
510
735
  if (repaired.trim()) text = repaired;
511
736
  }
512
737
  // Merge sources from the final output[] and the streaming annotation events.
513
- const sources = safeWebSearchSources(acc.final?.sources ?? []);
738
+ const finalSources = acc.final?.sources.map(source => source.title !== undefined ? source
739
+ : acc.streamSources.find(streamed => streamed.url === source.url) ?? source);
740
+ const sources = safeWebSearchSources(finalSources ?? []);
514
741
  for (const s of acc.streamSources) {
515
742
  appendSafeWebSearchSource(sources, s);
516
743
  }