Skip to content
Merged
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
27 changes: 27 additions & 0 deletions src/adapters/codexResponses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,33 @@ describe('unsupported-model fallback', () => {
});
});

describe('Responses stream terminal events', () => {
it('propagates an incomplete 200 stream instead of returning an empty successful response', async () => {
const fetchMock = vi.fn(async () => new Response(
[
'data: {"type":"response.incomplete","response":{"incomplete_details":{"reason":"max_output_tokens"}}}',
'data: [DONE]',
'',
].join('\n'),
{ status: 200, headers: { 'Content-Type': 'text/event-stream' } },
));
vi.stubGlobal('fetch', fetchMock);

type CreateApiCaller = (
initialToken: string,
accountId: string,
store: unknown,
model: string,
) => (messages: ChatMessage[], tools: ToolDefinition[]) => Promise<unknown>;
const adapter = new CodexResponsesAdapter() as unknown as { createApiCaller: CreateApiCaller };

await expect(adapter.createApiCaller('token', 'account', {}, 'gpt-5.6-terra')(
[{ role: 'user', content: 'Return a short answer.' }],
[],
)).rejects.toThrow('incomplete');
});
});

describe('chatToResponsesInput', () => {
it('lifts system messages into instructions and keeps user/assistant as input', () => {
const messages: ChatMessage[] = [
Expand Down
10 changes: 10 additions & 0 deletions src/adapters/codexResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ interface SseEvent {
arguments?: string;
response?: {
model?: string;
incomplete_details?: { reason?: string };
error?: { message?: string };
usage?: { input_tokens?: number; output_tokens?: number; input_tokens_details?: { cached_tokens?: number } };
};
}
Expand Down Expand Up @@ -241,6 +243,7 @@ async function consumeResponsesStream(

const decoder = new TextDecoder();
let buffer = '';
let terminalError: string | undefined;
// Reasoning summary streams token-by-token; buffer and emit whole lines so the
// live log shows readable thoughts instead of one-word-per-line spam.
let reasoningBuf = '';
Expand All @@ -257,6 +260,11 @@ async function consumeResponsesStream(
const handle = (ev: SseEvent | null) => {
if (!ev) return;
events.push(ev);
if (ev.type === 'response.incomplete') {
terminalError = `Responses stream incomplete${ev.response?.incomplete_details?.reason ? `: ${ev.response.incomplete_details.reason}` : ''}`;
} else if (ev.type === 'response.failed') {
terminalError = `Responses stream failed${ev.response?.error?.message ? `: ${ev.response.error.message}` : ''}`;
}
if (onToken && ev.type === 'response.output_text.delta' && ev.delta) onToken(ev.delta);
if (onReasoning && ev.type === 'response.reasoning_summary_text.delta' && ev.delta) {
reasoningBuf += ev.delta;
Expand All @@ -278,6 +286,8 @@ async function consumeResponsesStream(
handle(parseSseLine(buffer));
flushReasoning(true);

if (terminalError) throw new Error(terminalError);

return reduceResponsesEvents(events);
}

Expand Down