Skip to content

Commit eea5e3b

Browse files
committed
polish deepseek responses repair review details
Deep-clone registry repair arrays, preserve reasoning status metadata, flush retained reasoning into terminal snapshots, document rewrite behavior in ja/ko/ru, and cover composition trailer plus residual reasoning flush.
1 parent 9f25360 commit eea5e3b

8 files changed

Lines changed: 114 additions & 16 deletions

File tree

‎docs-site/src/content/docs/ja/reference/configuration/providers.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ Anthropic アカウント ポリシーのリスクを理解していない限り
205205
}
206206
```
207207

208-
プレースホルダー リストは完全に一致します。通常/ステートフル応答プロバイダーのフィールドを未設定のままにして、パススルーがバイトごとに同一になるようにします。
208+
プレースホルダー リストは完全に一致します。DeepSeek Responses のように UUID 形式の response/item ID と生の `reasoning_text` ストリームを返すゲートウェイでは `rewriteNonCanonicalIds` を有効にします。OpenCodex はそれらの ID を Codex 向けプレフィックスへ書き換え、reasoning を `encrypted_content` に正規化しつつ replay 用の平文 `content` を保持し、終端の `[DONE]` を補完します。通常/ステートフルな Responses プロバイダーではこのフィールドを未設定のままにして、パススルーがバイト単位で同一になるようにします。
209209

210210
## Cursor プロバイダー (`adapter: "cursor"`)
211211

‎docs-site/src/content/docs/ko/reference/configuration/providers.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ Anthropic 계정 정책 위험을 이해하지 못한다면 이 기능은 꺼두
208208
}
209209
```
210210

211-
자리표시자 목록은 정확히 일치해야 합니다. 일반적인 상태 유지형 Responses 공급자에서는 필드를 설정하지 마십시오. 그래야 passthrough가 바이트 단위로 동일하게 유지됩니다.
211+
자리표시자 목록은 정확히 일치해야 합니다. DeepSeek Responses처럼 UUID 형식 response/item id와 원본 `reasoning_text` 스트림을 반환하는 게이트웨이에서는 `rewriteNonCanonicalIds`를 켜십시오. OpenCodex는 해당 id를 Codex 친화적 prefix로 다시 쓰고, reasoning을 `encrypted_content`로 정규화하면서 replay용 평문 `content`는 유지하며, 종료용 `[DONE]`을 보장합니다. 일반/상태 유지형 Responses 공급자에서는 필드를 설정하지 마십시오. 그래야 passthrough가 바이트 단위로 동일하게 유지됩니다.
212212

213213
## Cursor 공급자 (`adapter: "cursor"`)
214214

‎docs-site/src/content/docs/ru/reference/configuration/providers.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ Beijing, а `alibaba-token-plan-intl` обслуживает междунаро
253253
}
254254
```
255255

256-
Списки placeholder'ов сравниваются по exact-match. Для обычных/stateful Responses-провайдеров это
256+
Списки placeholder'ов сравниваются по exact-match. Для gateway вроде DeepSeek Responses, которые отдают UUID-style response/item id и сырые `reasoning_text` потоки, включайте `rewriteNonCanonicalIds`: OpenCodex переписывает id в Codex-friendly prefix, нормализует reasoning в `encrypted_content` (сохраняя plaintext `content` для replay) и гарантирует terminal `[DONE]`. Для обычных/stateful Responses-провайдеров это
257257
поле оставляйте unset, чтобы passthrough оставался byte-for-byte идентичным.
258258

259259
## Провайдер Cursor (`adapter: "cursor"`)

‎src/providers/derive.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,14 @@ export function enrichProviderFromRegistry(name: string, prov: OcxProviderConfig
269269
// the entry so an explicit user value stays distinguishable from the default.
270270
if (prov.supportsServiceTier === undefined && entry.supportsServiceTier !== undefined) prov.supportsServiceTier = entry.supportsServiceTier;
271271
if (prov.preserveResponsesReasoningContent === undefined && entry.preserveResponsesReasoningContent !== undefined) prov.preserveResponsesReasoningContent = entry.preserveResponsesReasoningContent;
272-
if (prov.responsesItemIdRepair === undefined && entry.responsesItemIdRepair !== undefined) prov.responsesItemIdRepair = { ...entry.responsesItemIdRepair };
272+
if (prov.responsesItemIdRepair === undefined && entry.responsesItemIdRepair !== undefined) {
273+
const repair = entry.responsesItemIdRepair;
274+
prov.responsesItemIdRepair = {
275+
...repair,
276+
...(repair.message ? { message: [...repair.message] } : {}),
277+
...(repair.reasoning ? { reasoning: [...repair.reasoning] } : {}),
278+
};
279+
}
273280
if (!prov.autoToolChoiceOnlyModels && seed.autoToolChoiceOnlyModels) prov.autoToolChoiceOnlyModels = [...seed.autoToolChoiceOnlyModels];
274281
if (!prov.preserveReasoningContentModels && seed.preserveReasoningContentModels) prov.preserveReasoningContentModels = [...seed.preserveReasoningContentModels];
275282
if (!prov.reasoningSplitModels && seed.reasoningSplitModels) prov.reasoningSplitModels = [...seed.reasoningSplitModels];

‎src/server/responses-item-id-repair.ts‎

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,12 @@ function normalizeReasoningItem(
235235
// Snapshot content-derived text into the budgeted map only when we still need it later.
236236
if (!terminal) setReasoningText(state, outputIndex, text);
237237
}
238-
const next: Record<string, unknown> = {
239-
type: "reasoning",
240-
id: mapped,
241-
summary: Array.isArray(item.summary) ? item.summary : [],
242-
};
238+
// Preserve upstream metadata such as status; only proxy-owned reasoning fields are rebuilt.
239+
const next: Record<string, unknown> = { ...item, type: "reasoning" };
240+
if (mapped !== undefined) next.id = mapped;
241+
next.summary = Array.isArray(item.summary) ? item.summary : [];
242+
delete next.content;
243+
delete next.encrypted_content;
243244
if (terminal || text) {
244245
// Codex consumes encrypted_content; DeepSeek tool-call continuations need plaintext
245246
// reasoning_text after sanitizeReasoningInputContent strips the proxy-only ocxr1 envelope.
@@ -337,6 +338,44 @@ function rewriteItemIdField(
337338
return { event: next, changed: true };
338339
}
339340

341+
function flushRetainedReasoningIntoResponse(
342+
state: ResponsesItemIdRepairState,
343+
response: Record<string, unknown>,
344+
): { response: Record<string, unknown>; changed: boolean } {
345+
if (!state.rewriteNonCanonicalIds || state.reasoningTextByOutputIndex.size === 0) {
346+
return { response, changed: false };
347+
}
348+
const output = Array.isArray(response.output) ? [...response.output] : [];
349+
let changed = false;
350+
for (const [outputIndex, text] of [...state.reasoningTextByOutputIndex.entries()]) {
351+
if (!text) {
352+
releaseReasoningText(state, outputIndex);
353+
continue;
354+
}
355+
const existing = isPlainObject(output[outputIndex]) ? output[outputIndex] as Record<string, unknown> : null;
356+
if (existing?.type === "reasoning") {
357+
const rewritten = normalizeReasoningItem(state, outputIndex, existing, true);
358+
output[outputIndex] = rewritten;
359+
changed = true;
360+
continue;
361+
}
362+
// No reasoning item was emitted for this retained stream; synthesize one so the
363+
// terminal snapshot still carries the accumulated reasoning text.
364+
const synthetic = normalizeReasoningItem(state, outputIndex, {
365+
type: "reasoning",
366+
summary: [],
367+
content: [{ type: "reasoning_text", text }],
368+
}, true);
369+
if (outputIndex < output.length) output[outputIndex] = synthetic;
370+
else {
371+
while (output.length < outputIndex) output.push({ type: "message", role: "assistant", content: [] });
372+
output.push(synthetic);
373+
}
374+
changed = true;
375+
}
376+
return changed ? { response: { ...response, output }, changed: true } : { response, changed: false };
377+
}
378+
340379
function rewriteResponseSnapshot(
341380
state: ResponsesItemIdRepairState,
342381
response: Record<string, unknown>,
@@ -425,11 +464,22 @@ function repairEventPayload(
425464
}
426465
}
427466
if (isPlainObject(event.response)) {
428-
const rewritten = rewriteResponseSnapshot(state, event.response);
429-
if (rewritten.changed) {
467+
let response = event.response;
468+
if (type && TERMINAL_RESPONSE_TYPES.has(type) && state.rewriteNonCanonicalIds) {
469+
const flushed = flushRetainedReasoningIntoResponse(state, response);
470+
response = flushed.response;
471+
changed = changed || flushed.changed;
472+
}
473+
const rewritten = rewriteResponseSnapshot(state, response);
474+
if (rewritten.changed || response !== event.response) {
430475
nextEvent = { ...nextEvent, response: rewritten.response };
431476
changed = true;
432477
}
478+
} else if (type && TERMINAL_RESPONSE_TYPES.has(type) && state.rewriteNonCanonicalIds && state.reasoningTextByOutputIndex.size > 0) {
479+
// Terminal events without a response snapshot still need retained reasoning released.
480+
for (const outputIndex of [...state.reasoningTextByOutputIndex.keys()]) {
481+
releaseReasoningText(state, outputIndex);
482+
}
433483
}
434484

435485
if (type && TERMINAL_RESPONSE_TYPES.has(type)) state.sawTerminal = true;

‎src/server/responses/core.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,9 @@ async function handleResponsesInner(
16461646
// When item-id repair rewrites response ids, the inspector still sees the raw
16471647
// upstream UUID while Codex chains previous_response_id with the client-visible
16481648
// resp_ocx_* id. Alias both keys into the local continuation cache.
1649+
// mapClientResponseId is assigned only on the SSE rewrite path below. The JSON
1650+
// response path currently does not apply Responses item-id repair; if JSON repair
1651+
// is added later, initialize this alias before that branch records continuation state.
16491652
let mapClientResponseId: ((rawId: string | undefined) => string | undefined) | undefined;
16501653
const rememberPassthroughResponse = passthroughRecordEligible
16511654
? (response: { id?: unknown; output?: unknown; status?: unknown }) => {

‎tests/responses-item-id-repair-deepseek.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,41 @@ describe("DeepSeek Responses item-id repair", () => {
187187
expect(repaired.match(/data: \[DONE\]/g)).toHaveLength(1);
188188
});
189189

190+
test("preserves reasoning status metadata", async () => {
191+
const upstream = [
192+
`event: response.output_item.added\ndata: {"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"8da7b778-aff2-4d83-bfc3-8cd09ee79b34","status":"in_progress","summary":[]}}\n\n`,
193+
`event: response.reasoning_text.delta\ndata: {"type":"response.reasoning_text.delta","output_index":0,"item_id":"8da7b778-aff2-4d83-bfc3-8cd09ee79b34","delta":"think"}\n\n`,
194+
`event: response.output_item.done\ndata: {"type":"response.output_item.done","output_index":0,"item":{"type":"reasoning","id":"8da7b778-aff2-4d83-bfc3-8cd09ee79b34","status":"completed","summary":[]}}\n\n`,
195+
`event: response.completed\ndata: {"type":"response.completed","response":{"id":"58786d76-18be-43d9-b6f5-8922796fbe28","status":"completed","output":[{"type":"reasoning","id":"8da7b778-aff2-4d83-bfc3-8cd09ee79b34","status":"completed","summary":[]}]}}\n\n`,
196+
].join("");
197+
const events = await parseSse(await readAll(relaySseWithResponsesItemIdRepair(streamFromText(upstream), {
198+
rewriteNonCanonicalIds: true,
199+
})));
200+
const added = events.find(e => e.type === "response.output_item.added")!.item as Record<string, unknown>;
201+
const done = events.find(e => e.type === "response.output_item.done")!.item as Record<string, unknown>;
202+
const completed = events.find(e => e.type === "response.completed")!.response as { output: Record<string, unknown>[] };
203+
expect(added.status).toBe("in_progress");
204+
expect(done.status).toBe("completed");
205+
expect(completed.output[0].status).toBe("completed");
206+
expect(String(done.encrypted_content)).toMatch(/^ocxr1:/);
207+
});
208+
209+
test("flushes retained reasoning text into terminal snapshot when item.done is missing", async () => {
210+
const upstream = [
211+
`event: response.output_item.added\ndata: {"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"8da7b778-aff2-4d83-bfc3-8cd09ee79b34","status":"in_progress","summary":[]}}\n\n`,
212+
`event: response.reasoning_text.delta\ndata: {"type":"response.reasoning_text.delta","output_index":0,"item_id":"8da7b778-aff2-4d83-bfc3-8cd09ee79b34","delta":"orphan"}\n\n`,
213+
`event: response.completed\ndata: {"type":"response.completed","response":{"id":"58786d76-18be-43d9-b6f5-8922796fbe28","status":"completed","output":[]}}\n\n`,
214+
].join("");
215+
const events = await parseSse(await readAll(relaySseWithResponsesItemIdRepair(streamFromText(upstream), {
216+
rewriteNonCanonicalIds: true,
217+
})));
218+
const completed = events.find(e => e.type === "response.completed")!.response as { output: Record<string, unknown>[] };
219+
expect(completed.output).toHaveLength(1);
220+
expect(completed.output[0].type).toBe("reasoning");
221+
expect(String(completed.output[0].encrypted_content)).toMatch(/^ocxr1:/);
222+
expect((completed.output[0].content as Array<{ text: string }>)[0].text).toBe("orphan");
223+
});
224+
190225
test("keeps shared placeholder aliases type-scoped", async () => {
191226
const upstream = [
192227
'event: response.output_item.added\ndata: {"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"shared_placeholder"}}\n\n',

‎tests/sse-payload-rewrite.test.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import { describe, expect, test } from "bun:test";
55
import { createImageGenCallRestoreRewrite } from "../src/server/responses-image-gen-repair";
6-
import { createResponsesItemIdPayloadRewrite } from "../src/server/responses-item-id-repair";
6+
import { createResponsesItemIdRepairHandlers } from "../src/server/responses-item-id-repair";
77
import {
88
composeSsePayloadRewrites,
99
relaySseWithPayloadRewrite,
@@ -51,7 +51,7 @@ describe("SSE payload rewrite composition", () => {
5151
const imageGen = createImageGenCallRestoreRewrite(
5252
new Map([["image_gen__imagegen", { namespace: "image_gen", name: "imagegen" }]]),
5353
)!;
54-
const itemId = createResponsesItemIdPayloadRewrite({
54+
const itemId = createResponsesItemIdRepairHandlers({
5555
message: ["msg_0"],
5656
repairMissingTerminalIds: true,
5757
});
@@ -63,13 +63,16 @@ describe("SSE payload rewrite composition", () => {
6363
},
6464
(payload) => {
6565
itemIdCalls += 1;
66-
return itemId(payload);
66+
return itemId.rewrite(payload);
6767
},
6868
);
6969

7070
const budget = createTestTranslatorBudget();
71-
const out = await readAll(relaySseWithPayloadRewrite(streamFromText(upstream), composed, budget));
71+
const out = await readAll(relaySseWithPayloadRewrite(streamFromText(upstream), composed, budget, {
72+
trailer: itemId.trailer,
73+
}));
7274
budget.dispose();
75+
expect(out).toContain("data: [DONE]");
7376
expect(imageGenCalls).toBe(3);
7477
expect(itemIdCalls).toBe(3);
7578
expect(imageGenCalls).toBe(itemIdCalls);
@@ -78,7 +81,7 @@ describe("SSE payload rewrite composition", () => {
7881
.trim()
7982
.split(/\r?\n\r?\n/)
8083
.map(block => block.split(/\r?\n/).find(line => line.startsWith("data:"))?.slice(5).trim())
81-
.filter((payload): payload is string => !!payload)
84+
.filter((payload): payload is string => !!payload && payload !== "[DONE]")
8285
.map(payload => JSON.parse(payload) as Record<string, unknown>);
8386

8487
const messageAdded = events[0].item as Record<string, unknown>;

0 commit comments

Comments
 (0)