|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { createOpenAIChatAdapter } from "../src/adapters/openai-chat"; |
| 3 | +import type { AdapterEvent } from "../src/types"; |
| 4 | + |
| 5 | +const provider = { adapter: "openai-chat", baseUrl: "https://example.test/v1", apiKey: "key" }; |
| 6 | + |
| 7 | +async function collect(gen: AsyncGenerator<AdapterEvent>): Promise<AdapterEvent[]> { |
| 8 | + const out: AdapterEvent[] = []; |
| 9 | + for await (const e of gen) out.push(e); |
| 10 | + return out; |
| 11 | +} |
| 12 | + |
| 13 | +function lastDone(events: AdapterEvent[]): { type: "done"; stopReason?: string } | undefined { |
| 14 | + return events.find(e => e.type === "done") as { type: "done"; stopReason?: string } | undefined; |
| 15 | +} |
| 16 | + |
| 17 | +describe("openai-chat streaming finish_reason -> stopReason propagation", () => { |
| 18 | + test("finish_reason=stop -> done with stopReason=stop", async () => { |
| 19 | + const response = new Response('data: {"choices":[{"delta":{"content":"hi"},"finish_reason":"stop"}]}\n\n'); |
| 20 | + const events = await collect(createOpenAIChatAdapter(provider).parseStream(response)); |
| 21 | + const done = lastDone(events); |
| 22 | + expect(done).toBeDefined(); |
| 23 | + expect(done!.stopReason).toBe("stop"); |
| 24 | + }); |
| 25 | + |
| 26 | + test("finish_reason=length -> done with stopReason=length", async () => { |
| 27 | + const response = new Response('data: {"choices":[{"delta":{"content":"truncated"},"finish_reason":"length"}]}\n\n'); |
| 28 | + const events = await collect(createOpenAIChatAdapter(provider).parseStream(response)); |
| 29 | + const done = lastDone(events); |
| 30 | + expect(done).toBeDefined(); |
| 31 | + expect(done!.stopReason).toBe("length"); |
| 32 | + }); |
| 33 | + |
| 34 | + test("finish_reason=max_tokens -> done with stopReason=max_tokens", async () => { |
| 35 | + const response = new Response('data: {"choices":[{"delta":{"content":"x"},"finish_reason":"max_tokens"}]}\n\n'); |
| 36 | + const events = await collect(createOpenAIChatAdapter(provider).parseStream(response)); |
| 37 | + const done = lastDone(events); |
| 38 | + expect(done).toBeDefined(); |
| 39 | + expect(done!.stopReason).toBe("max_tokens"); |
| 40 | + }); |
| 41 | + |
| 42 | + test("finish_reason=tool_calls -> done with stopReason=tool_calls", async () => { |
| 43 | + const response = new Response('data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","function":{"name":"f","arguments":"{}"}}]},"finish_reason":"tool_calls"}]}\n\n'); |
| 44 | + const events = await collect(createOpenAIChatAdapter(provider).parseStream(response)); |
| 45 | + const done = lastDone(events); |
| 46 | + expect(done).toBeDefined(); |
| 47 | + expect(done!.stopReason).toBe("tool_calls"); |
| 48 | + }); |
| 49 | + |
| 50 | + test("finish_reason=content_filter -> done with stopReason=content_filter", async () => { |
| 51 | + const response = new Response('data: {"choices":[{"delta":{"content":"filtered"},"finish_reason":"content_filter"}]}\n\n'); |
| 52 | + const events = await collect(createOpenAIChatAdapter(provider).parseStream(response)); |
| 53 | + const done = lastDone(events); |
| 54 | + expect(done).toBeDefined(); |
| 55 | + expect(done!.stopReason).toBe("content_filter"); |
| 56 | + }); |
| 57 | + |
| 58 | + test("[DONE] after finish_reason=length preserves the captured stopReason", async () => { |
| 59 | + const response = new Response([ |
| 60 | + 'data: {"choices":[{"delta":{"content":"hi"},"finish_reason":"length"}]}\n\n', |
| 61 | + "data: [DONE]\n\n", |
| 62 | + ].join("")); |
| 63 | + const events = await collect(createOpenAIChatAdapter(provider).parseStream(response)); |
| 64 | + const done = lastDone(events); |
| 65 | + expect(done).toBeDefined(); |
| 66 | + expect(done!.stopReason).toBe("length"); |
| 67 | + }); |
| 68 | + |
| 69 | + test("[DONE] without any finish_reason -> done with stopReason=undefined", async () => { |
| 70 | + const response = new Response([ |
| 71 | + 'data: {"choices":[{"delta":{"content":"hi"}}]}\n\n', |
| 72 | + "data: [DONE]\n\n", |
| 73 | + ].join("")); |
| 74 | + const events = await collect(createOpenAIChatAdapter(provider).parseStream(response)); |
| 75 | + const done = lastDone(events); |
| 76 | + expect(done).toBeDefined(); |
| 77 | + expect(done!.stopReason).toBeUndefined(); |
| 78 | + }); |
| 79 | + |
| 80 | + test("EOF without finish_reason and without [DONE] -> error (not done)", async () => { |
| 81 | + const response = new Response('data: {"choices":[{"delta":{"content":"par"}}]}\n\n'); |
| 82 | + const events = await collect(createOpenAIChatAdapter(provider).parseStream(response)); |
| 83 | + const done = events.find(e => e.type === "done"); |
| 84 | + expect(done).toBeUndefined(); |
| 85 | + expect(events.at(-1)?.type).toBe("error"); |
| 86 | + }); |
| 87 | + |
| 88 | + test("usage-only final frame without finish_reason -> done with stopReason=undefined", async () => { |
| 89 | + const response = new Response( |
| 90 | + 'data: {"choices":[{"delta":{"content":"hi"}}]}\n\n' + |
| 91 | + 'data: {"choices":[],"usage":{"prompt_tokens":1,"completion_tokens":2,"total_tokens":3}}' |
| 92 | + ); |
| 93 | + const events = await collect(createOpenAIChatAdapter(provider).parseStream(response)); |
| 94 | + const done = lastDone(events); |
| 95 | + expect(done).toBeDefined(); |
| 96 | + expect(done!.stopReason).toBeUndefined(); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe("openai-chat non-streaming finish_reason -> stopReason propagation", () => { |
| 101 | + test("finish_reason=stop -> done with stopReason=stop", async () => { |
| 102 | + const json = JSON.stringify({ |
| 103 | + choices: [{ message: { content: "hi" }, finish_reason: "stop" }], |
| 104 | + usage: { prompt_tokens: 5, completion_tokens: 2, total_tokens: 7 }, |
| 105 | + }); |
| 106 | + const events = await createOpenAIChatAdapter(provider).parseResponse(new Response(json)); |
| 107 | + const done = lastDone(events); |
| 108 | + expect(done).toBeDefined(); |
| 109 | + expect(done!.stopReason).toBe("stop"); |
| 110 | + }); |
| 111 | + |
| 112 | + test("finish_reason=length -> done with stopReason=length", async () => { |
| 113 | + const json = JSON.stringify({ |
| 114 | + choices: [{ message: { content: "truncated" }, finish_reason: "length" }], |
| 115 | + usage: { prompt_tokens: 5, completion_tokens: 4096, total_tokens: 4101 }, |
| 116 | + }); |
| 117 | + const events = await createOpenAIChatAdapter(provider).parseResponse(new Response(json)); |
| 118 | + const done = lastDone(events); |
| 119 | + expect(done).toBeDefined(); |
| 120 | + expect(done!.stopReason).toBe("length"); |
| 121 | + }); |
| 122 | + |
| 123 | + test("finish_reason=max_tokens -> done with stopReason=max_tokens", async () => { |
| 124 | + const json = JSON.stringify({ |
| 125 | + choices: [{ message: { content: "truncated" }, finish_reason: "max_tokens" }], |
| 126 | + usage: { prompt_tokens: 5, completion_tokens: 4096, total_tokens: 4101 }, |
| 127 | + }); |
| 128 | + const events = await createOpenAIChatAdapter(provider).parseResponse(new Response(json)); |
| 129 | + const done = lastDone(events); |
| 130 | + expect(done).toBeDefined(); |
| 131 | + expect(done!.stopReason).toBe("max_tokens"); |
| 132 | + }); |
| 133 | + |
| 134 | + test("finish_reason=content_filter -> done with stopReason=content_filter", async () => { |
| 135 | + const json = JSON.stringify({ |
| 136 | + choices: [{ message: { content: "" }, finish_reason: "content_filter" }], |
| 137 | + usage: { prompt_tokens: 5, completion_tokens: 0, total_tokens: 5 }, |
| 138 | + }); |
| 139 | + const events = await createOpenAIChatAdapter(provider).parseResponse(new Response(json)); |
| 140 | + const done = lastDone(events); |
| 141 | + expect(done).toBeDefined(); |
| 142 | + expect(done!.stopReason).toBe("content_filter"); |
| 143 | + }); |
| 144 | + |
| 145 | + test("finish_reason=tool_calls -> done with stopReason=tool_calls", async () => { |
| 146 | + const json = JSON.stringify({ |
| 147 | + choices: [{ message: { tool_calls: [{ id: "c1", function: { name: "f", arguments: "{}" } }] }, finish_reason: "tool_calls" }], |
| 148 | + usage: { prompt_tokens: 5, completion_tokens: 10, total_tokens: 15 }, |
| 149 | + }); |
| 150 | + const events = await createOpenAIChatAdapter(provider).parseResponse(new Response(json)); |
| 151 | + const done = lastDone(events); |
| 152 | + expect(done).toBeDefined(); |
| 153 | + expect(done!.stopReason).toBe("tool_calls"); |
| 154 | + }); |
| 155 | + |
| 156 | + test("missing finish_reason -> done with stopReason=undefined", async () => { |
| 157 | + const json = JSON.stringify({ |
| 158 | + choices: [{ message: { content: "no finish reason" } }], |
| 159 | + usage: { prompt_tokens: 5, completion_tokens: 3, total_tokens: 8 }, |
| 160 | + }); |
| 161 | + const events = await createOpenAIChatAdapter(provider).parseResponse(new Response(json)); |
| 162 | + const done = lastDone(events); |
| 163 | + expect(done).toBeDefined(); |
| 164 | + expect(done!.stopReason).toBeUndefined(); |
| 165 | + }); |
| 166 | +}); |
0 commit comments