GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(responses): backfill missing status and created_at for strict dec… · bet4it/opencodex@6aaadc1

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/server/responses
    • responses-field-backfill.ts
  • tests
    • responses-field-backfill.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/server/responses/responses-field-backfill.ts‎

Copy file name to clipboardExpand all lines: src/server/responses/responses-field-backfill.ts
+107-15Lines changed: 107 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ function nextSyntheticItemSlot(): ItemIdSlot {
9191
return { kind: "fallback", ordinal: syntheticItemOrdinal };
9292
}
9393

94+
/**
95+
* Backfill `status` on a message output item if missing.
96+
*
97+
* The Responses API spec defines `status` as a required field on
98+
* `OutputMessage`. Some upstream relays omit it, which causes strict
99+
* deserializers (e.g. grok-build's serde types) to fail with
100+
* `missing field 'status'`. Only message items carry this field in the
101+
* Responses schema; reasoning, function_call, and other item types do not.
102+
*
103+
* The value is inferred from the event context: `output_item.added` and
104+
* `response.created` / `response.in_progress` mean the message is still
105+
* being generated (`in_progress`); `output_item.done` and
106+
* `response.completed` / `response.incomplete` mean the message is
107+
* finalized (`completed` / `incomplete` respectively).
108+
*
109+
* Returns the same object reference if no change is needed.
110+
*/
111+
function backfillItemStatus(item: Record<string, unknown>, inferredStatus: string): Record<string, unknown> {
112+
if (item.type !== "message") return item;
113+
if ("status" in item) return item;
114+
return { ...item, status: inferredStatus };
115+
}
116+
94117
/**
95118
* Backfill annotations: [] on an output_text content part if missing.
96119
* Returns the same object reference if no change is needed.
@@ -122,10 +145,10 @@ function backfillContentArray(content: unknown): unknown {
122145

123146
/**
124147
* Walk an output item and backfill output_text parts in its content.
125-
* Also backfills a missing required id on the item itself.
148+
* Also backfills a missing required id and status on the item itself.
126149
* Returns the same object reference if nothing changed.
127150
*/
128-
function backfillOutputItem(item: unknown, slot: ItemIdSlot): unknown {
151+
function backfillOutputItem(item: unknown, slot: ItemIdSlot, inferredStatus: string): unknown {
129152
if (!isPlainObject(item)) return item;
130153
// The compact wire family is the `/v1/responses/compact` format, not a Responses output item.
131154
// Those items have no `id` in that contract, so synthesizing one changes a response body the
@@ -136,34 +159,90 @@ function backfillOutputItem(item: unknown, slot: ItemIdSlot): unknown {
136159
const content = item.content;
137160
const repaired = backfillContentArray(content);
138161
const withId = backfillItemId(item, slot);
139-
if (repaired === content && withId === item) return item;
140-
return { ...withId, ...(repaired === content ? {} : { content: repaired }) };
162+
const withStatus = backfillItemStatus(withId, inferredStatus);
163+
if (repaired === content && withStatus === item) return item;
164+
return { ...withStatus, ...(repaired === content ? {} : { content: repaired }) };
141165
}
142166

143167
/**
144168
* Walk a response object's output[] and backfill output_text parts.
169+
* Also backfills `created_at` on the response itself when absent.
170+
*
171+
* `createdAt` is captured once per rewrite factory (SSE) or per call (JSON)
172+
* so every event in the same stream carries the same timestamp, even if the
173+
* stream spans a second boundary.
174+
*
175+
* `inferredItemStatus` is the status to backfill on message items that lack
176+
* one — derived from the event type so `output_item.added` / `response.created`
177+
* gets `in_progress` while `output_item.done` / `response.completed` gets
178+
* `completed`.
179+
*
145180
* Returns the same object reference if nothing changed.
146181
*/
147-
function backfillResponseOutput(response: unknown): unknown {
182+
function backfillResponseOutput(response: unknown, createdAt: number, inferredItemStatus: string): unknown {
148183
if (!isPlainObject(response)) return response;
149-
const output = response.output;
150-
if (!Array.isArray(output)) return response;
184+
let current = response;
185+
186+
// Backfill created_at on the Response object. Strict Responses decoders (e.g. grok-build's
187+
// serde types) require `created_at: u64` — no `#[serde(default)]` — so an upstream relay that
188+
// omits it causes `missing field 'created_at'`. Use a timestamp captured once per rewrite
189+
// factory so every event in the same stream agrees, even if the stream spans a second
190+
// boundary.
191+
if (!("created_at" in current)) {
192+
current = { ...current, created_at: createdAt };
193+
}
194+
195+
const output = current.output;
196+
if (!Array.isArray(output)) return current === response ? response : current;
151197
let changed = false;
152198
const repaired = output.map((item, idx) => {
153199
if (!isPlainObject(item)) return item;
154-
const next = backfillOutputItem(item, { kind: "index", index: idx });
200+
const next = backfillOutputItem(item, { kind: "index", index: idx }, inferredItemStatus);
155201
if (next !== item) changed = true;
156202
return next;
157203
});
158-
return changed ? { ...response, output: repaired } : response;
204+
if (!changed && current === response) return response;
205+
return { ...current, output: repaired };
206+
}
207+
208+
/**
209+
* Infer the status to backfill on a message item from the event type.
210+
*
211+
* `output_item.added` means the item is still being generated (`in_progress`);
212+
* `output_item.done` means it is finalized (`completed`). Response-level events
213+
* infer from the response's own status field — which is authoritative when present.
214+
* If the response status is also absent, the event type itself determines the phase:
215+
* `response.created` / `response.in_progress` → `in_progress`,
216+
* `response.completed` → `completed`, `response.incomplete` → `incomplete`.
217+
*/
218+
function inferredStatusForEventType(eventType: string): string {
219+
if (eventType === "response.output_item.added") return "in_progress";
220+
if (eventType === "response.output_item.done") return "completed";
221+
if (eventType === "response.created" || eventType === "response.in_progress") return "in_progress";
222+
if (eventType === "response.incomplete") return "incomplete";
223+
return "completed";
224+
}
225+
226+
/**
227+
* Map a response-level lifecycle status to a valid OutputMessage status.
228+
*
229+
* `OutputMessage.status` accepts only `in_progress`, `completed`, or
230+
* `incomplete`. Response-level statuses like `failed`, `cancelled`, or
231+
* `queued` have no message-level equivalent and must not be written to a
232+
* message item — a strict decoder would reject them.
233+
*/
234+
function messageStatusFromResponseStatus(status: string): string | null {
235+
if (status === "in_progress" || status === "completed" || status === "incomplete") return status;
236+
return null;
159237
}
160238

161239
/**
162240
* Statelessly rewrite one SSE event: backfill annotations
163241
* on any output_text content part found in the event payload.
164242
*/
165-
function rewriteEvent(event: Record<string, unknown>): Record<string, unknown> {
243+
function rewriteEvent(event: Record<string, unknown>, createdAt: number): Record<string, unknown> {
166244
const type = typeof event.type === "string" ? event.type : "";
245+
const inferredItemStatus = inferredStatusForEventType(type);
167246
let next = event;
168247
let changed = false;
169248

@@ -177,8 +256,8 @@ function rewriteEvent(event: Record<string, unknown>): Record<string, unknown> {
177256
// is not recoverable in that case, but a unique id is what strict decoders require, and a
178257
// well-formed stream still gets the stable index-derived id.
179258
const item = typeof rawIndex === "number" && Number.isInteger(rawIndex) && rawIndex >= 0
180-
? backfillOutputItem(event.item, { kind: "index", index: rawIndex })
181-
: backfillOutputItem(event.item, nextSyntheticItemSlot());
259+
? backfillOutputItem(event.item, { kind: "index", index: rawIndex }, inferredItemStatus)
260+
: backfillOutputItem(event.item, nextSyntheticItemSlot(), inferredItemStatus);
182261
if (item !== event.item) {
183262
next = { ...next, item };
184263
changed = true;
@@ -198,7 +277,13 @@ function rewriteEvent(event: Record<string, unknown>): Record<string, unknown> {
198277
// response.created / in_progress / completed / incomplete / failed:
199278
// response.output[].content[] -> output_text parts
200279
if (isPlainObject(event.response)) {
201-
const response = backfillResponseOutput(event.response);
280+
// For response-level events, prefer the response's own status when it is a valid
281+
// OutputMessage status. Response lifecycle statuses like "failed" or "cancelled"
282+
// have no message-level equivalent — fall back to the event-type inference instead.
283+
const responseStatus = typeof event.response.status === "string"
284+
? messageStatusFromResponseStatus(event.response.status) ?? inferredItemStatus
285+
: inferredItemStatus;
286+
const response = backfillResponseOutput(event.response, createdAt, responseStatus);
202287
if (response !== event.response) {
203288
next = { ...next, response };
204289
changed = true;
@@ -213,8 +298,13 @@ function rewriteEvent(event: Record<string, unknown>): Record<string, unknown> {
213298
* on output_text content parts. Unconditional: the field is a required
214299
* canonical Responses field, so adding it when absent is safe for all
215300
* clients.
301+
*
302+
* `created_at` is captured once at factory creation time so every event in
303+
* the same stream carries the same timestamp, even if the stream spans a
304+
* second boundary.
216305
*/
217306
export function createResponsesFieldBackfillBlockRewrite(): SseBlockRewrite {
307+
const createdAt = Math.floor(Date.now() / 1000);
218308
const rewrite: SseBlockRewrite = (block: string): readonly string[] => {
219309
const payload = sseDataPayload(block);
220310
if (payload === null) return [block];
@@ -225,7 +315,7 @@ export function createResponsesFieldBackfillBlockRewrite(): SseBlockRewrite {
225315
return [block];
226316
}
227317
if (!isPlainObject(event)) return [block];
228-
const rewritten = rewriteEvent(event);
318+
const rewritten = rewriteEvent(event, createdAt);
229319
if (rewritten === event) return [block];
230320
return [replaceSseDataPayload(block, JSON.stringify(rewritten))];
231321
};
@@ -245,7 +335,9 @@ export function backfillResponsesFieldsJson(payload: string): string {
245335
return payload;
246336
}
247337
if (!isPlainObject(response)) return payload;
248-
const repaired = backfillResponseOutput(response);
338+
const createdAt = Math.floor(Date.now() / 1000);
339+
// For a non-streaming response, the response is already terminal, so items get "completed".
340+
const repaired = backfillResponseOutput(response, createdAt, "completed");
249341
if (repaired === response) return payload;
250342
return JSON.stringify(repaired);
251343
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.