|
| 1 | +/** |
| 2 | + * Stateless SSE block rewrite that backfills the `annotations` field on |
| 3 | + * `output_text` content parts in Responses SSE events. |
| 4 | + * |
| 5 | + * The Responses API spec defines `annotations` as a required field on |
| 6 | + * `OutputTextContent` (it is `Vec<Annotation>`, not `Option<Vec>`). Some |
| 7 | + * upstream relays omit it when there are no annotations, which is technically |
| 8 | + * spec-non-compliant. Strict deserializers — any client that follows the |
| 9 | + * schema without `#[serde(default)]` on that field — fail with |
| 10 | + * `missing field `annotations`` when the field is absent. |
| 11 | + * |
| 12 | + * This rewrite scans every SSE event for output_text content parts — whether |
| 13 | + * they appear in item.content[], part, or response.output[].content[] — and |
| 14 | + * adds annotations: [] if missing. |
| 15 | + * |
| 16 | + * Stateless: no retained buffers, no lifecycle tracking, no fail-closed. |
| 17 | + * Existing values are always authoritative; only absent fields are added. |
| 18 | + * The field is always valid on the wire, so adding it when absent is safe |
| 19 | + * for all clients including Codex CLI/App. |
| 20 | + */ |
| 21 | + |
| 22 | +import { |
| 23 | + replaceSseDataPayload, |
| 24 | + sseDataPayload, |
| 25 | + type SseBlockRewrite, |
| 26 | +} from "../sse-payload-rewrite"; |
| 27 | + |
| 28 | +function isPlainObject(value: unknown): value is Record<string, unknown> { |
| 29 | + return !!value && typeof value === "object" && !Array.isArray(value); |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Backfill annotations: [] on an output_text content part if missing. |
| 34 | + * Returns the same object reference if no change is needed. |
| 35 | + */ |
| 36 | +function backfillOutputTextPart(part: Record<string, unknown>): Record<string, unknown> { |
| 37 | + if (part.type !== "output_text") return part; |
| 38 | + // Only add annotations when the key is entirely absent — preserve any |
| 39 | + // existing value (even null or a malformed type) so we never overwrite |
| 40 | + // what the upstream actually sent. |
| 41 | + if ("annotations" in part) return part; |
| 42 | + return { ...part, annotations: [] }; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Walk a content array and backfill output_text parts. |
| 47 | + * Returns the same array reference if nothing changed. |
| 48 | + */ |
| 49 | +function backfillContentArray(content: unknown): unknown { |
| 50 | + if (!Array.isArray(content)) return content; |
| 51 | + let changed = false; |
| 52 | + const repaired = content.map((part) => { |
| 53 | + if (!isPlainObject(part)) return part; |
| 54 | + const next = backfillOutputTextPart(part); |
| 55 | + if (next !== part) changed = true; |
| 56 | + return next; |
| 57 | + }); |
| 58 | + return changed ? repaired : content; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Walk an output item and backfill output_text parts in its content. |
| 63 | + * Returns the same object reference if nothing changed. |
| 64 | + */ |
| 65 | +function backfillOutputItem(item: unknown): unknown { |
| 66 | + if (!isPlainObject(item)) return item; |
| 67 | + const content = item.content; |
| 68 | + const repaired = backfillContentArray(content); |
| 69 | + if (repaired === content) return item; |
| 70 | + return { ...item, content: repaired }; |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Walk a response object's output[] and backfill output_text parts. |
| 75 | + * Returns the same object reference if nothing changed. |
| 76 | + */ |
| 77 | +function backfillResponseOutput(response: unknown): unknown { |
| 78 | + if (!isPlainObject(response)) return response; |
| 79 | + const output = response.output; |
| 80 | + if (!Array.isArray(output)) return response; |
| 81 | + let changed = false; |
| 82 | + const repaired = output.map((item) => { |
| 83 | + if (!isPlainObject(item)) return item; |
| 84 | + const next = backfillOutputItem(item); |
| 85 | + if (next !== item) changed = true; |
| 86 | + return next; |
| 87 | + }); |
| 88 | + return changed ? { ...response, output: repaired } : response; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Statelessly rewrite one SSE event: backfill annotations |
| 93 | + * on any output_text content part found in the event payload. |
| 94 | + */ |
| 95 | +function rewriteEvent(event: Record<string, unknown>): Record<string, unknown> { |
| 96 | + const type = typeof event.type === "string" ? event.type : ""; |
| 97 | + let next = event; |
| 98 | + let changed = false; |
| 99 | + |
| 100 | + // output_item.added / output_item.done: item.content[] -> output_text parts |
| 101 | + if ((type === "response.output_item.added" || type === "response.output_item.done") |
| 102 | + && isPlainObject(event.item)) { |
| 103 | + const item = backfillOutputItem(event.item); |
| 104 | + if (item !== event.item) { |
| 105 | + next = { ...next, item }; |
| 106 | + changed = true; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // content_part.added / content_part.done: part -> output_text |
| 111 | + if ((type === "response.content_part.added" || type === "response.content_part.done") |
| 112 | + && isPlainObject(event.part)) { |
| 113 | + const part = backfillOutputTextPart(event.part); |
| 114 | + if (part !== event.part) { |
| 115 | + next = { ...next, part }; |
| 116 | + changed = true; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // response.created / in_progress / completed / incomplete / failed: |
| 121 | + // response.output[].content[] -> output_text parts |
| 122 | + if (isPlainObject(event.response)) { |
| 123 | + const response = backfillResponseOutput(event.response); |
| 124 | + if (response !== event.response) { |
| 125 | + next = { ...next, response }; |
| 126 | + changed = true; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return changed ? next : event; |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Create a stateless SSE block rewrite that backfills annotations and |
| 135 | + * on output_text content parts. Unconditional: the field is a required |
| 136 | + * canonical Responses field, so adding it when absent is safe for all |
| 137 | + * clients. |
| 138 | + */ |
| 139 | +export function createResponsesFieldBackfillBlockRewrite(): SseBlockRewrite { |
| 140 | + const rewrite: SseBlockRewrite = (block: string): readonly string[] => { |
| 141 | + const payload = sseDataPayload(block); |
| 142 | + if (payload === null) return [block]; |
| 143 | + let event: unknown; |
| 144 | + try { |
| 145 | + event = JSON.parse(payload); |
| 146 | + } catch { |
| 147 | + return [block]; |
| 148 | + } |
| 149 | + if (!isPlainObject(event)) return [block]; |
| 150 | + const rewritten = rewriteEvent(event); |
| 151 | + if (rewritten === event) return [block]; |
| 152 | + return [replaceSseDataPayload(block, JSON.stringify(rewritten))]; |
| 153 | + }; |
| 154 | + return rewrite; |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * Backfill annotations on a non-streaming Responses JSON |
| 159 | + * object. Mirrors the SSE block rewrite for the bounded-JSON passthrough |
| 160 | + * path. Returns the original string if no change is needed. |
| 161 | + */ |
| 162 | +export function backfillResponsesFieldsJson(payload: string): string { |
| 163 | + let response: unknown; |
| 164 | + try { |
| 165 | + response = JSON.parse(payload); |
| 166 | + } catch { |
| 167 | + return payload; |
| 168 | + } |
| 169 | + if (!isPlainObject(response)) return payload; |
| 170 | + const repaired = backfillResponseOutput(response); |
| 171 | + if (repaired === response) return payload; |
| 172 | + return JSON.stringify(repaired); |
| 173 | +} |
0 commit comments