import type { Api, AssistantMessage, AssistantMessageEvent, AssistantMessageEventStream, Context, Model, } from "@earendil-works/pi-ai"; import { afterEach, describe, expect, it, vi } from "vitest"; import { streamQoder } from "../stream.js"; /** * Build a single SSE `data:` line carrying a Qoder envelope: * { headers, body: , statusCodeValue, statusCode } * The server wraps the OpenAI-style chunk inside `body` as a JSON string. */ function sseEnvelope(body: object, statusCodeValue = 200, statusCode = "OK"): string { return ( "data:" + JSON.stringify({ headers: { "Content-Type": ["application/json"] }, body: JSON.stringify(body), statusCodeValue, statusCode, }) + "\n\n" ); } const DONE_SSE = "data:" + JSON.stringify({ headers: { "Content-Type": ["application/json"] }, body: "[DONE]", statusCodeValue: 200, statusCode: "OK", }) + "\n\n"; function chunk(delta: object, extra: object = {}): object { return { choices: [{ delta, index: 0 }], created: 1, id: "test-id", model: "auto", object: "chat.completion.chunk", ...extra, }; } function finishChunk(finish_reason: string, extra: object = {}): object { return { choices: [{ finish_reason, index: 0 }], created: 1, id: "test-id", model: "auto", object: "chat.completion.chunk", usage: { completion_tokens: 1, prompt_tokens: 1, total_tokens: 2 }, ...extra, }; } const SUCCESS_SSE = sseEnvelope(chunk({ role: "assistant" })) + sseEnvelope(chunk({ reasoning_content: "The user wants OK.", role: "assistant" })) + sseEnvelope(chunk({ content: "OK", role: "assistant" })) + sseEnvelope(finishChunk("stop")) + DONE_SSE; const BLOCKED_SSE = sseEnvelope( { code: "provider_error", message: "Session blocked", request_id: "r", type: "provider_error" }, 406, "Not Acceptable", ); function mockFetch(body: string): typeof fetch { const response = new Response(body, { status: 200, headers: { "content-type": "text/event-stream" }, }); return vi.fn(async () => response) as unknown as typeof fetch; } function makeModel(): Model { return { id: "ultimate", api: "qoder-api" as Api, provider: "qoder" } as Model; } function makeContext(): Context { return { systemPrompt: "test", messages: [{ role: "user", content: "hi" }], tools: [], } as unknown as Context; } async function consume(stream: AssistantMessageEventStream): Promise { const events: AssistantMessageEvent[] = []; for await (const ev of stream) { events.push(ev); if (ev.type === "done" || ev.type === "error") break; } return events; } describe("streamQoder", () => { const originalFetch = globalThis.fetch; afterEach(() => { globalThis.fetch = originalFetch; vi.restoreAllMocks(); }); it("parses a successful SSE stream into text + stop", async () => { globalThis.fetch = mockFetch(SUCCESS_SSE); const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" }); const events = await consume(stream); const done = events.find((e) => e.type === "done"); expect(done, "expected a done event").toBeDefined(); const msg = (done as { message: AssistantMessage }).message; expect(msg.stopReason).toBe("stop"); const text = msg.content.find((c) => c.type === "text"); expect(text && "text" in text ? text.text : "").toBe("OK"); }); it("surfaces an upstream 406 'Session blocked' as an error event, not a silent stop", async () => { globalThis.fetch = mockFetch(BLOCKED_SSE); const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" }); const events = await consume(stream); const err = events.find((e) => e.type === "error"); expect(err, "expected an error event").toBeDefined(); const msg = (err as { error: AssistantMessage }).error; expect(msg.stopReason).toBe("error"); expect(msg.errorMessage).toMatch(/Session blocked/); expect(msg.errorMessage).toMatch(/406/); // Must NOT emit a silent done/stop. expect(events.find((e) => e.type === "done")).toBeUndefined(); }); it("preserves finish_reason=length instead of overwriting to stop", async () => { const sse = sseEnvelope(chunk({ content: "partial", role: "assistant" })) + sseEnvelope(finishChunk("length")) + DONE_SSE; globalThis.fetch = mockFetch(sse); const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" }); const events = await consume(stream); const done = events.find((e) => e.type === "done"); const msg = (done as { message: AssistantMessage }).message; expect(msg.stopReason).toBe("length"); }); it("captures usage, responseId and responseModel from the finish chunk", async () => { const sse = sseEnvelope(chunk({ content: "OK", role: "assistant" })) + sseEnvelope( finishChunk("stop", { id: "chatcmpl-abc123", model: "qmodel_latest", usage: { prompt_tokens: 42, completion_tokens: 7, total_tokens: 49, completion_tokens_details: { reasoning_tokens: 3 }, // prompt_tokens (42) INCLUDES cached_tokens (5) per OpenAI // semantics; pi-core expects `input` to exclude them // (promptTokens = input + cacheRead + cacheWrite), so input = // 42 - 5 - 10 = 27. cacheable_tokens is a capacity metric, not a // write count, and must not be mapped to cacheWrite. prompt_tokens_details: { cacheable_tokens: 99, cache_write_tokens: 10, cached_tokens: 5 }, }, }), ) + DONE_SSE; globalThis.fetch = mockFetch(sse); const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" }); const events = await consume(stream); const done = events.find((e) => e.type === "done"); const msg = (done as { message: AssistantMessage }).message; expect(msg.responseId).toBe("chatcmpl-abc123"); expect(msg.responseModel).toBe("qmodel_latest"); expect(msg.usage.input).toBe(27); expect(msg.usage.output).toBe(7); expect(msg.usage.totalTokens).toBe(49); expect(msg.usage.cacheRead).toBe(5); expect(msg.usage.cacheWrite).toBe(10); }); it("emits a done event with reason=length when finish_reason is length", async () => { const sse = sseEnvelope(chunk({ content: "partial", role: "assistant" })) + sseEnvelope(finishChunk("length")) + DONE_SSE; globalThis.fetch = mockFetch(sse); const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" }); const events = await consume(stream); const done = events.find((e) => e.type === "done"); expect(done, "expected a done event").toBeDefined(); expect((done as { reason: string }).reason).toBe("length"); }); it("reports a tool_use stop reason when the stream emits tool calls", async () => { const sse = sseEnvelope( chunk({ tool_calls: [ { index: 0, id: "call_1", function: { name: "bash", arguments: '{"command":"ls"}' }, }, ], }), ) + sseEnvelope(finishChunk("tool_calls")) + DONE_SSE; globalThis.fetch = mockFetch(sse); const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" }); const events = await consume(stream); const done = events.find((e) => e.type === "done"); const msg = (done as { message: AssistantMessage }).message; expect(msg.stopReason).toBe("toolUse"); const toolCall = msg.content.find((c) => c.type === "toolCall"); expect(toolCall).toBeDefined(); }); });