@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.
- package/AGENTS_INSTALL.md +19 -1
- package/README.md +37 -33
- package/SPONSORS.md +5 -1
- package/bin/ocx.mjs +1 -1
- package/gui/dist/assets/{App-C1hGwaN1.css → App-COo8Yopd.css} +1 -1
- package/gui/dist/assets/App-g3Cm2xhZ.js +59 -0
- package/gui/dist/assets/Tray-BMDkAiXy.js +1 -0
- package/gui/dist/assets/brand-mark-nr3ziR-T.js +1 -0
- package/gui/dist/assets/index-Bziu8gTy.css +2 -0
- package/gui/dist/assets/index-OYzhQUNP.js +94 -0
- package/gui/dist/assets/{segment-thumb-features-fYEMxBaW.js → segment-thumb-features-DKCVUUTi.js} +1 -1
- package/gui/dist/classic/assets/App-CgPBKUP-.js +58 -0
- package/gui/dist/classic/assets/{Tray-BqZueZOx.js → Tray-BedmOsE7.js} +1 -1
- package/gui/dist/classic/assets/index-BR-HhNrp.js +94 -0
- package/gui/dist/classic/assets/quota-summary-CXKhvSmQ.js +1 -0
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/native/ocx-child/Cargo.lock +215 -0
- package/native/ocx-child/Cargo.toml +31 -0
- package/native/ocx-child/src/cli.rs +1901 -0
- package/native/ocx-child/src/codex_home.rs +429 -0
- package/native/ocx-child/src/codex_settings.rs +231 -0
- package/native/ocx-child/src/daemon.rs +363 -0
- package/native/ocx-child/src/http_wire.rs +619 -0
- package/native/ocx-child/src/journal.rs +977 -0
- package/native/ocx-child/src/lib.rs +22 -0
- package/native/ocx-child/src/main.rs +9 -0
- package/native/ocx-child/src/parity.rs +102 -0
- package/native/ocx-child/src/platform.rs +1487 -0
- package/native/ocx-child/src/relay.rs +1288 -0
- package/native/ocx-child/src/relay_auth.rs +166 -0
- package/native/ocx-child/src/routes.rs +236 -0
- package/native/ocx-child/src/state.rs +190 -0
- package/native/ocx-child/src/test_hooks.rs +58 -0
- package/native/ocx-child/src/toml_surgeon.rs +1156 -0
- package/package.json +8 -2
- package/src/adapters/anthropic-image-normalize.ts +1 -1
- package/src/adapters/anthropic.ts +28 -2
- package/src/adapters/claude-cli/adapter.ts +5 -25
- package/src/adapters/claude-cli/instruction-images.ts +31 -0
- package/src/adapters/claude-cli/stable-replay.ts +42 -14
- package/src/adapters/claude-cli/usage-admission.ts +53 -64
- package/src/adapters/client-fingerprint.ts +1 -1
- package/src/adapters/coding-agent/protocol.ts +1 -1
- package/src/adapters/devin/live-models.ts +3 -0
- package/src/adapters/google-http.ts +3 -0
- package/src/adapters/google-vertex-key-project.ts +10 -2
- package/src/adapters/google.ts +40 -8
- package/src/adapters/kiro/usage.ts +3 -13
- package/src/adapters/openai-chat/mistral-image-limit.ts +28 -15
- package/src/adapters/openai-chat-images.ts +100 -0
- package/src/adapters/openai-chat.ts +6 -9
- package/src/adapters/openai-responses/passthrough.ts +5 -2
- package/src/adapters/openai-responses/request-strips.ts +32 -0
- package/src/adapters/openai-responses/tool-schema.ts +9 -4
- package/src/chat/inbound.ts +1 -1
- package/src/claude/auth-detect.ts +26 -8
- package/src/claude/inbound-content-options.ts +18 -3
- package/src/claude/inbound.ts +5 -2
- package/src/cli/bundled-bun-diagnostic.ts +91 -0
- package/src/cli/capabilities-agents-routing.ts +2 -2
- package/src/cli/capabilities-observe-system.ts +60 -0
- package/src/cli/combo-input.ts +6 -0
- package/src/cli/combo-result.ts +1 -1
- package/src/cli/combo.ts +4 -0
- package/src/cli/config-command.ts +16 -2
- package/src/cli/connect.ts +12 -10
- package/src/cli/dispatch.ts +4 -0
- package/src/cli/doctor.ts +11 -0
- package/src/cli/help.ts +1 -0
- package/src/cli/init.ts +4 -0
- package/src/cli/link-enrollment.ts +152 -25
- package/src/cli/link-local-control.ts +49 -0
- package/src/cli/link.ts +37 -10
- package/src/cli/provider-lifecycle-runtime.ts +4 -2
- package/src/cli/registry.ts +13 -0
- package/src/cli/star-prompt.ts +2 -0
- package/src/cli/status.ts +1 -1
- package/src/cli/telemetry-profile.ts +206 -0
- package/src/cli/telemetry.ts +260 -0
- package/src/client/hub-relay.ts +26 -0
- package/src/client/machine-api.ts +3 -0
- package/src/clients/config-export/t3code.ts +44 -5
- package/src/clients/omo-role-models.ts +1 -1
- package/src/codex/catalog/bundled.ts +44 -2
- package/src/codex/runtime.ts +12 -0
- package/src/codex/shim-restore-lock.ts +67 -30
- package/src/combos/types.ts +21 -1
- package/src/config/atomic-write.ts +24 -1
- package/src/config/diagnostics.ts +15 -0
- package/src/config/live-reconcile.ts +17 -0
- package/src/config/load-degrade.ts +16 -0
- package/src/config/provider-validation.ts +29 -0
- package/src/config/schema/config-schema.ts +24 -0
- package/src/config/schema/leaf-validators.ts +17 -0
- package/src/config/telemetry-carry.ts +30 -0
- package/src/config.ts +13 -1
- package/src/generated/compatibility-version.json +422 -170
- package/src/generated/model-metadata.ts +3 -3
- package/src/integrations/aside-profiles.ts +7 -1
- package/src/integrations/owned-refresh.ts +9 -2
- package/src/lab/artifacts/secure-fs.ts +12 -11
- package/src/lab/fabric/scratch.ts +11 -9
- package/src/lab/public/community.ts +2 -3
- package/src/lab/public/file-safety.ts +18 -20
- package/src/lab/public/mutation-lock.ts +109 -337
- package/src/lab/public/private-file.ts +4 -3
- package/src/lib/bun-runtime-preflight.ts +1 -1
- package/src/lib/codex-catalog-read-only.ts +122 -0
- package/src/lib/config-ownership.ts +56 -18
- package/src/lib/errors.ts +11 -0
- package/src/lib/gcp-adc.ts +167 -48
- package/src/lib/jsonc.ts +4 -2
- package/src/lib/local-control-socket.ts +88 -0
- package/src/lib/privacy.ts +15 -0
- package/src/lib/provider-client-headers.ts +3 -0
- package/src/lib/token-estimate.ts +13 -2
- package/src/link/child-binary-source.ts +228 -0
- package/src/link/ephemeral-child.ts +45 -0
- package/src/link/remote-plant.ts +306 -0
- package/src/link/remote-windows.ts +59 -5
- package/src/link/ssh-argv.ts +71 -8
- package/src/link/status-projection.ts +2 -0
- package/src/link/store.ts +5 -1
- package/src/oauth/anthropic-model-quota.ts +12 -0
- package/src/oauth/anthropic-routing.ts +11 -4
- package/src/oauth/claude-keychain-read.ts +165 -0
- package/src/oauth/kiro-iam-identity-center.ts +4 -1
- package/src/oauth/local-token-detect.ts +14 -17
- package/src/protocols/envelope.ts +20 -2
- package/src/providers/api-key-selection.ts +8 -2
- package/src/providers/claude-cli-identity.ts +9 -3
- package/src/providers/claude-cli-usage-state.ts +93 -0
- package/src/providers/claude-cli-usage.ts +3 -2
- package/src/providers/github-copilot-auto.ts +25 -5
- package/src/providers/meta-muse-plan-refusal.ts +94 -0
- package/src/providers/model-rename-fields.ts +3 -0
- package/src/providers/quota/anthropic-family-headers.ts +8 -1
- package/src/providers/quota/anthropic-usage-windows.ts +49 -2
- package/src/providers/quota/vendor-probes-key.ts +23 -4
- package/src/providers/quota/vendor-probes-oauth.ts +59 -0
- package/src/providers/quota/workbuddy.ts +2 -2
- package/src/providers/quota-types.ts +7 -0
- package/src/providers/quota.ts +25 -18
- package/src/providers/registry/entries-core.ts +7 -4
- package/src/providers/registry/entries-extended.ts +42 -8
- package/src/providers/registry/model-ids.ts +1 -0
- package/src/providers/registry/model-seeds.ts +10 -2
- package/src/providers/registry/types.ts +6 -0
- package/src/providers/resolved-model-policy.ts +4 -0
- package/src/providers/routed-config.ts +30 -3
- package/src/providers/stale-no-vision-seed-migration.ts +7 -0
- package/src/providers/vertex-environment-authority.ts +50 -0
- package/src/providers/workbuddy-credit-balance.ts +38 -6
- package/src/quota-pool/assemble.ts +14 -6
- package/src/responses/retained-compaction.ts +31 -5
- package/src/router.ts +1 -0
- package/src/server/adapter-resolve.ts +11 -6
- package/src/server/audio-dictation.ts +21 -7
- package/src/server/auth-cors.ts +19 -0
- package/src/server/background-lifecycle.ts +71 -0
- package/src/server/chat-completions.ts +2 -0
- package/src/server/claude-messages.ts +30 -3
- package/src/server/index/live-sideband.ts +15 -1
- package/src/server/index/local-control-listener.ts +515 -0
- package/src/server/index/optional-listeners.ts +21 -1
- package/src/server/index/serve-options.ts +4 -3
- package/src/server/index/websocket-handler.ts +2 -0
- package/src/server/index.ts +3 -0
- package/src/server/management/combo-routes.ts +6 -1
- package/src/server/management/context.ts +1 -0
- package/src/server/management/link-child-preflight.ts +74 -0
- package/src/server/management/link-enrollment.ts +190 -25
- package/src/server/management/link-route-state.ts +6 -1
- package/src/server/management/link-routes.ts +27 -9
- package/src/server/management/model-rows.ts +4 -0
- package/src/server/management/oauth-account-routes.ts +9 -4
- package/src/server/management/provider-overwrite-carry.ts +9 -2
- package/src/server/management/provider-routes.ts +14 -0
- package/src/server/management/provider-thinking-display-patch.ts +54 -0
- package/src/server/management/route-registry.ts +7 -0
- package/src/server/management/session-routes.ts +4 -1
- package/src/server/management/telemetry-routes.ts +37 -0
- package/src/server/management/usage-aggregate-cache.ts +290 -67
- package/src/server/management-api.ts +11 -2
- package/src/server/messages-native-preflight.ts +166 -0
- package/src/server/messages-native.ts +143 -43
- package/src/server/messages-shared.ts +14 -0
- package/src/server/proxy-liveness.ts +99 -6
- package/src/server/request-log-conversation.ts +18 -2
- package/src/server/request-log.ts +7 -2
- package/src/server/responses/adapter-continuation.ts +1 -0
- package/src/server/responses/adapter-dispatch.ts +11 -28
- package/src/server/responses/adapter-failure-message.ts +68 -0
- package/src/server/responses/combo-stream-preflight.ts +14 -2
- package/src/server/responses/compact.ts +13 -1
- package/src/server/responses/core-auth.ts +26 -5
- package/src/server/responses/core-codex-account.ts +8 -1
- package/src/server/responses/core-combo-failure.ts +6 -1
- package/src/server/responses/core-combo-native.ts +150 -37
- package/src/server/responses/core-combo.ts +15 -9
- package/src/server/responses/core-normalize.ts +1 -0
- package/src/server/responses/core-options.ts +7 -1
- package/src/server/responses/core-pipeline.ts +21 -1
- package/src/server/responses/fetch-helpers.ts +5 -0
- package/src/server/responses/opengateway-completion-guard.ts +7 -3
- package/src/server/responses/passthrough-delivery.ts +8 -1
- package/src/server/responses/passthrough-error.ts +22 -0
- package/src/server/responses/request-prepare.ts +7 -1
- package/src/server/responses/request-transport.ts +28 -5
- package/src/server/responses/run-turn-execution.ts +13 -0
- package/src/server/sse-frame-buffer.ts +5 -0
- package/src/server/ws-bridge.ts +10 -0
- package/src/service/launchd.ts +78 -4
- package/src/telemetry/aggregate.ts +348 -0
- package/src/telemetry/consent.ts +128 -0
- package/src/telemetry/contract.ts +403 -0
- package/src/telemetry/disclosure.ts +19 -0
- package/src/telemetry/install-id.ts +226 -0
- package/src/telemetry/payload.ts +129 -0
- package/src/telemetry/profile/client.ts +150 -0
- package/src/telemetry/profile/contract.ts +288 -0
- package/src/telemetry/profile/disclosure.ts +14 -0
- package/src/telemetry/profile/paths.ts +13 -0
- package/src/telemetry/profile/routes.ts +99 -0
- package/src/telemetry/profile/service.ts +234 -0
- package/src/telemetry/profile/state.ts +189 -0
- package/src/telemetry/profile/sync.ts +276 -0
- package/src/telemetry/routes.ts +183 -0
- package/src/telemetry/scheduler.ts +386 -0
- package/src/telemetry/sender.ts +97 -0
- package/src/telemetry/state.ts +334 -0
- package/src/types/combo-jev-effort-mode.ts +11 -0
- package/src/types/config.ts +3 -0
- package/src/types/provider.ts +11 -1
- package/src/types/request.ts +2 -0
- package/src/types/telemetry.ts +15 -0
- package/src/types/wire.ts +10 -0
- package/src/types.ts +5 -0
- package/src/update/index.ts +7 -1
- package/src/update/minisign.d.mts +17 -0
- package/src/update/minisign.mjs +88 -0
- package/src/update/signed-supply.d.mts +59 -0
- package/src/update/signed-supply.mjs +289 -0
- package/src/update/trust-root.json +38 -0
- package/src/update/update-bounded-json.d.mts +4 -0
- package/src/update/update-bounded-json.mjs +113 -0
- package/src/update/update-bytes.d.mts +2 -0
- package/src/update/update-bytes.mjs +23 -0
- package/src/update/update-candidate-id.d.mts +2 -0
- package/src/update/update-candidate-id.mjs +23 -0
- package/src/update/update-checkpoint-codec.d.mts +33 -0
- package/src/update/update-checkpoint-codec.mjs +217 -0
- package/src/update/update-contracts.d.mts +24 -0
- package/src/update/update-contracts.mjs +207 -0
- package/src/update/update-signature-threshold.d.mts +12 -0
- package/src/update/update-signature-threshold.mjs +47 -0
- package/src/update/update-signed-version.d.mts +5 -0
- package/src/update/update-signed-version.mjs +25 -0
- package/src/update/update-trust-anchor.d.mts +2 -0
- package/src/update/update-trust-anchor.mjs +2 -0
- package/src/usage/cache-diagnostic.ts +195 -50
- package/src/usage/expected-prices.ts +20 -2
- package/src/usage/filter-index.ts +1469 -0
- package/src/usage/ledger-scanner.ts +245 -4
- package/src/usage/source-filter.ts +10 -2
- package/src/usage/summary.ts +16 -0
- package/src/web-search/parse.ts +265 -38
- package/gui/dist/assets/App-BepstgEN.js +0 -59
- package/gui/dist/assets/Tray-BinxbEZo.js +0 -1
- package/gui/dist/assets/brand-mark-wzPe3R-J.js +0 -1
- package/gui/dist/assets/index-C-9X2yaI.js +0 -94
- package/gui/dist/assets/index-bnUWypyz.css +0 -2
- package/gui/dist/classic/assets/App-DX88_GCW.js +0 -58
- package/gui/dist/classic/assets/index-fSP71pLB.js +0 -94
- package/gui/dist/classic/assets/quota-summary-y2fl0WBv.js +0 -1
package/src/web-search/parse.ts
CHANGED
|
@@ -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 & {
|
|
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
|
-
*
|
|
49
|
-
*
|
|
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
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
64
|
-
|
|
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
|
|
68
|
-
function comparePartKeys(left: string, right: string): number {
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
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 =
|
|
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(
|
|
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,
|
|
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:
|
|
504
|
-
//
|
|
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
|
|
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
|
|
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
|
}
|