Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/loop/adapter-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ function buildMessageItemSequence(itemId: string, outputIndex: number, text: str
return Buffer.from(
[
`event: response.output_item.added\ndata: ${JSON.stringify({ type: "response.output_item.added", output_index: outputIndex, item })}\n\n`,
`event: response.content_part.added\ndata: ${JSON.stringify({ type: "response.content_part.added", item_id: itemId, output_index: outputIndex, part })}\n\n`,
`event: response.output_text.delta\ndata: ${JSON.stringify({ type: "response.output_text.delta", item_id: itemId, output_index: outputIndex, delta: text })}\n\n`,
`event: response.output_text.done\ndata: ${JSON.stringify({ type: "response.output_text.done", item_id: itemId, output_index: outputIndex, text })}\n\n`,
`event: response.content_part.done\ndata: ${JSON.stringify({ type: "response.content_part.done", item_id: itemId, output_index: outputIndex, part: { type: "output_text", text } })}\n\n`,
`event: response.content_part.added\ndata: ${JSON.stringify({ type: "response.content_part.added", item_id: itemId, output_index: outputIndex, content_index: 0, part })}\n\n`,
`event: response.output_text.delta\ndata: ${JSON.stringify({ type: "response.output_text.delta", item_id: itemId, output_index: outputIndex, content_index: 0, delta: text })}\n\n`,
`event: response.output_text.done\ndata: ${JSON.stringify({ type: "response.output_text.done", item_id: itemId, output_index: outputIndex, content_index: 0, text })}\n\n`,
`event: response.content_part.done\ndata: ${JSON.stringify({ type: "response.content_part.done", item_id: itemId, output_index: outputIndex, content_index: 0, part: { type: "output_text", text } })}\n\n`,
`event: response.output_item.done\ndata: ${JSON.stringify({ type: "response.output_item.done", output_index: outputIndex, item: doneItem })}\n\n`,
].join(""),
"utf8",
Expand Down Expand Up @@ -413,12 +413,12 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
// identity filters, byte-verbatim (never rewritten; matches the
// openai adapter's reasoning_filter and #1960/KDD#10).
const reasoningFilters = new Map<string, { filter: TagEchoFilter; ref: { itemId: string; outputIndex: number; summaryIndex: number } }>();
let lastTextRef: { itemId: string; outputIndex: number } | null = null;
let lastTextRef: { itemId: string; outputIndex: number; contentIndex: unknown } | null = null;
const flushFilter = function* (): Generator<ParsedStreamEvent> {
const tail = tagFilter.flush();
if (tail.length > 0) {
const raw = lastTextRef
? rebuildResponsesEvent("response.output_text.delta", { type: "response.output_text.delta", item_id: lastTextRef.itemId, output_index: lastTextRef.outputIndex, delta: tail })
? rebuildResponsesEvent("response.output_text.delta", { type: "response.output_text.delta", item_id: lastTextRef.itemId, output_index: lastTextRef.outputIndex, content_index: lastTextRef.contentIndex, delta: tail })
: undefined;
yield { kind: "text", delta: tail, ...(raw ? { raw } : {}) } as ParsedStreamEvent;
}
Expand Down Expand Up @@ -545,7 +545,7 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
const itemId = typeof obj.item_id === "string" ? obj.item_id : "";
const outputIndex = typeof obj.output_index === "number" ? obj.output_index : 0;
const mapped = remapped.get(itemId);
lastTextRef = { itemId: mapped ? mapped.id : itemId, outputIndex: mapped ? mapped.index : outputIndex };
lastTextRef = { itemId: mapped ? mapped.id : itemId, outputIndex: mapped ? mapped.index : outputIndex, contentIndex: obj.content_index };
const clean = tagFilter.push(delta);
if (clean.length > 0) {
if (mapped) {
Expand Down
6 changes: 3 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4612,13 +4612,13 @@ export async function pipePluginResponsesWithStrip(
if (proseAcc.length === 0 || !session) return;
scanModelOutputForPhantomAttachments(session.id, proseAcc);
};
let lastDeltaMeta: { item_id?: unknown; output_index?: unknown } | null = null;
let lastDeltaMeta: { item_id?: unknown; output_index?: unknown; content_index?: unknown } | null = null;
const flushTail = (after: string) => {
const tail = tagFilter.flush();
if (tail.length > 0) {
visibleTextChars += tail.length;
answerProse += tail;
const meta = inRetry && heldItemId !== undefined ? { item_id: heldItemId, output_index: heldOutputIndex } : (lastDeltaMeta ?? {});
const meta = inRetry && heldItemId !== undefined ? { ...lastDeltaMeta, item_id: heldItemId, output_index: heldOutputIndex } : (lastDeltaMeta ?? {});
appendReplyDelta(meta, tail);
return `data: ${JSON.stringify({ type: "response.output_text.delta", ...meta, delta: tail })}\n\n` + after;
}
Expand Down Expand Up @@ -5070,7 +5070,7 @@ export async function pipePluginResponsesWithStrip(
continue;
}
const clean = tagFilter.push(delta);
lastDeltaMeta = { item_id: ev["item_id"], output_index: ev["output_index"] };
lastDeltaMeta = { item_id: ev["item_id"], output_index: ev["output_index"], content_index: ev["content_index"] };
if (!inRetry && heldItemId === undefined && ev["item_id"] !== undefined) {
heldItemId = ev["item_id"];
heldOutputIndex = ev["output_index"];
Expand Down
8 changes: 4 additions & 4 deletions src/stream-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ function legacyCompletionChunk(protocol: Protocol, visible: string, state?: Lega
const oi = 0;
const errorItem = { type: "message", id: itemId, role: "assistant", content: [{ type: "output_text", text: visible }] };
return `event: response.output_item.added\ndata: ${JSON.stringify({ type: "response.output_item.added", output_index: oi, item: { type: "message", id: itemId, role: "assistant", content: [] } })}\n\n` +
`event: response.content_part.added\ndata: ${JSON.stringify({ type: "response.content_part.added", item_id: itemId, output_index: oi, part: { type: "output_text", text: "" } })}\n\n` +
`event: response.output_text.delta\ndata: ${JSON.stringify({ type: "response.output_text.delta", item_id: itemId, output_index: oi, delta: visible })}\n\n` +
`event: response.output_text.done\ndata: ${JSON.stringify({ type: "response.output_text.done", item_id: itemId, output_index: oi, text: visible })}\n\n` +
`event: response.content_part.done\ndata: ${JSON.stringify({ type: "response.content_part.done", item_id: itemId, output_index: oi, part: { type: "output_text", text: visible } })}\n\n` +
`event: response.content_part.added\ndata: ${JSON.stringify({ type: "response.content_part.added", item_id: itemId, output_index: oi, content_index: 0, part: { type: "output_text", text: "" } })}\n\n` +
`event: response.output_text.delta\ndata: ${JSON.stringify({ type: "response.output_text.delta", item_id: itemId, output_index: oi, content_index: 0, delta: visible })}\n\n` +
`event: response.output_text.done\ndata: ${JSON.stringify({ type: "response.output_text.done", item_id: itemId, output_index: oi, content_index: 0, text: visible })}\n\n` +
`event: response.content_part.done\ndata: ${JSON.stringify({ type: "response.content_part.done", item_id: itemId, output_index: oi, content_index: 0, part: { type: "output_text", text: visible } })}\n\n` +
`event: response.output_item.done\ndata: ${JSON.stringify({ type: "response.output_item.done", output_index: oi, item: errorItem })}\n\n` +
`event: response.completed\ndata: ${JSON.stringify({ type: "response.completed", response: { status: "completed", output: [errorItem] } })}\n\n`;
}
Expand Down
1 change: 1 addition & 0 deletions tests/proxy-stream-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ test("emitStreamError: responses errorShape=\"completion\" emits a full item lif
for (const block of itemEvents) {
assert.match(block, new RegExp(`"item_id":"${itemId}"`));
assert.match(block, /"output_index":0/);
assert.match(block, /"content_index":0/);
}
// The completed response contains the error item in its output array.
const completed = out.split("\n\n").find((b) => b.includes("response.completed"));
Expand Down
134 changes: 134 additions & 0 deletions tests/responses-content-index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import type { ServerResponse } from "node:http";
import { createRequire } from "node:module";
import { createResponsesAdapter } from "../src/loop/adapter-responses.ts";
import { pipePluginResponsesWithStrip } from "../src/plugin.ts";
import { emitStreamError } from "../src/stream-error.ts";
import { validateResponsesContentIndex } from "./wire-contract-fakes.ts";

function frame(event: Record<string, unknown>): string {
return `event: ${String(event.type)}\ndata: ${JSON.stringify(event)}\n\n`;
}

function created(): string {
return frame({ type: "response.created", response: { id: "resp_fixture", object: "response", status: "in_progress", output: [] } });
}

function payloads(raw: string): Record<string, unknown>[] {
return raw.split("\n").filter((line) => line.startsWith("data: ")).map((line) => JSON.parse(line.slice(6)) as Record<string, unknown>);
}

function collector(): { res: ServerResponse; chunks: string[] } {
const chunks: string[] = [];
const res = {
write(chunk: string | Buffer) { chunks.push(chunk.toString()); return true; },
end() { return this; },
once() { return this; },
destroyed: false,
writableEnded: false,
} as unknown as ServerResponse;
return { res, chunks };
}

async function consume(raw: string) {
const requireSdk = createRequire(import.meta.resolve("@earendil-works/pi-coding-agent"));
const { default: OpenAI } = await import(requireSdk.resolve("openai"));
const client = new OpenAI({
apiKey: "synthetic-test-key",
fetch: async () => new Response(raw, { headers: { "content-type": "text/event-stream" } }),
});
const stream = client.responses.stream({ model: "synthetic", input: "synthetic" });
const deltas: Array<{ delta: string; content_index: number; output_index: number }> = [];
stream.on("response.output_text.delta", (event: { delta: string; content_index: number; output_index: number }) => deltas.push(event));
const response = await stream.finalResponse();
return { deltas, response };
}

function upstreamText(text: string, contentIndex: number, id = "msg_fixture"): string {
const earlier = Array.from({ length: contentIndex }, () => ({ type: "output_text", text: "earlier part" }));
const content = [...earlier, { type: "output_text", text }];
const item = { type: "message", id, role: "assistant", content };
const ref = { item_id: id, output_index: 0, content_index: contentIndex };
return created() + [
{ type: "response.output_item.added", output_index: 0, item: { ...item, content: [] } },
...earlier.map((part, index) => ({ type: "response.content_part.added", item_id: id, output_index: 0, content_index: index, part })),
{ type: "response.content_part.added", ...ref, part: { type: "output_text", text: "" } },
{ type: "response.output_text.delta", ...ref, delta: text },
{ type: "response.output_text.done", ...ref, text },
{ type: "response.content_part.done", ...ref, part: { type: "output_text", text } },
{ type: "response.output_item.done", output_index: 0, item },
{ type: "response.completed", response: { id: "resp_fixture", status: "completed", output: [item] } },
].map(frame).join("");
}

test("official Responses SDK consumes a legacy stream-error completion", async () => {
const { res, chunks } = collector();
emitStreamError(res, "responses", "kaboom", undefined, "completion");
const { deltas, response } = await consume(created() + chunks.join(""));
assert.deepEqual(deltas.map((event) => event.delta), ["\n❌ [ACP] stream error: kaboom"]);
assert.equal(response.status, "completed");
assert.equal(response.output[0].content[0].text, deltas[0].delta);
});

test("official Responses SDK still rejects the default native error", async () => {
const { res, chunks } = collector();
emitStreamError(res, "responses", "kaboom");
await assert.rejects(consume(created() + chunks.join("")), { type: "error", code: "stream_error", message: "[acp-proxy: kaboom]" });
});

for (const kind of ["text", "marker"] as const) {
test(`official Responses SDK consumes synthesized ${kind} content`, async () => {
const adapter = createResponsesAdapter();
const chunk = kind === "text" ? adapter.emitText("generated text") : adapter.emitMarker("acp_status", "ready");
const events = payloads(chunk.toString());
const item = events.find((event) => event.type === "response.output_item.done")?.item;
assert.ok(item);
const { deltas } = await consume(created() + chunk + frame({ type: "response.completed", response: { id: "resp_fixture", status: "completed", output: [item] } }));
assert.equal(deltas.length, 1);
assert.equal(deltas[0].content_index, 0);
if (kind === "text") assert.equal(deltas[0].delta, "generated text");
else assert.match(deltas[0].delta, /\[ACP\]/);
assert.deepEqual(events.flatMap(validateResponsesContentIndex), []);
});
}

for (const mode of ["proxy", "plugin"] as const) {
for (const contentIndex of [0, 1]) {
test(`${mode} Responses text flush preserves content index ${contentIndex}`, async () => {
const input = upstreamText("Length: 10 m", contentIndex);
const { res, chunks } = collector();
if (mode === "plugin") {
await pipePluginResponsesWithStrip(new Response(input).body!, res);
} else {
const adapter = createResponsesAdapter();
for await (const event of adapter.parseStream(new Response(input).body!, 1)) {
if (event.kind === "meta") chunks.push(event.chunk.toString());
else if (event.kind === "text" && event.raw) chunks.push(event.raw.toString());
}
chunks.push(adapter.emitCompletion().toString());
}
const { deltas, response } = await consume(chunks.join(""));
assert.deepEqual(deltas.map((event) => event.delta), ["Length: 10 ", "m"]);
for (const delta of deltas) assert.equal(delta.content_index, contentIndex);
assert.equal(response.output[0].content[contentIndex].text, "Length: 10 m");
if (contentIndex > 0) assert.equal(response.output[0].content[0].text, "earlier part");
assert.deepEqual(payloads(chunks.join("")).flatMap(validateResponsesContentIndex), []);
});
}
}

test("plugin Responses retry flush preserves its content index", async () => {
const { res, chunks } = collector();
let retries = 0;
const echo = "\x3cacp tokens=\"1\" type=\"text\"\x3em00001\x3c/acp\x3e";
await pipePluginResponsesWithStrip(new Response(upstreamText(echo, 0)).body!, res, undefined, undefined, async () => {
retries++;
return new Response(upstreamText("Length: 10 m", 0, "msg_retry")).body;
});
assert.equal(retries, 1);
const { deltas } = await consume(chunks.join(""));
assert.equal(deltas.map((event) => event.delta).join(""), "Length: 10 m");
for (const delta of deltas) assert.equal(delta.content_index, 0);
assert.deepEqual(payloads(chunks.join("")).flatMap(validateResponsesContentIndex), []);
});
16 changes: 16 additions & 0 deletions tests/wire-contract-fakes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ interface WireRule {
}

export const WIRE_RULES: readonly WireRule[] = [
{
id: "WC-026",
wire: "responses",
lane: "responses-sse",
summary: "Responses text/content-part SSE events carry a non-negative integer content_index identifying the part inside the indexed output item",
provenance: "OpenAI openai-node v6.26.0 requires content_index in ResponseTextDeltaEvent/ResponseTextDoneEvent/ResponseContentPartAddedEvent/ResponseContentPartDoneEvent (https://github.com/openai/openai-node/blob/v6.26.0/src/resources/responses/responses.ts); ResponseStream indexes output.content[event.content_index] (https://github.com/openai/openai-node/blob/v6.26.0/src/lib/responses/ResponseStream.ts). Reproduced against that official SDK: bili's synthesized messages, legacy stream-error completions and filtered text tails omitted indices, so finalResponse() rejected with 'missing content at index undefined'. Gates exercise the actual SDK in proxy/plugin paths and the emitted error frames; the fake validates its outgoing SSE frames with the same constraint.",
},
{
id: "WC-017",
wire: "responses",
Expand Down Expand Up @@ -772,6 +779,8 @@ interface FakeUpstream {
}

function sse(res: http.ServerResponse, events: Array<[string, unknown]>, opts?: { done?: boolean }): void {
const violations = events.flatMap(([, data]) => validateResponsesContentIndex(data));
if (violations.length) throw new Error(violations.join("; "));
res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache", connection: "keep-alive" });
// OpenAI streams carry no `event:` line at all — only Anthropic/Gemini name their events.
for (const [ev, data] of events) {
Expand Down Expand Up @@ -811,6 +820,13 @@ function openAiChatReply(res: http.ServerResponse, parsed: Record<string, unknow
}));
}

export function validateResponsesContentIndex(event: unknown): string[] {
if (!isPlainObject(event) || !["response.content_part.added", "response.output_text.delta", "response.output_text.done", "response.content_part.done"].includes(String(event.type))) return [];
return typeof event.content_index === "number" && Number.isInteger(event.content_index) && event.content_index >= 0
? []
: ["WC-026 Responses content-part events require a non-negative integer content_index"];
}

function responsesReply(res: http.ServerResponse, parsed: Record<string, unknown>, text: string): void {
const msgId = "msg_fake";
const msg = { type: "message", id: msgId, role: "assistant", content: [{ type: "output_text", text }] };
Expand Down
Loading
Loading