|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { openaiUpstreamAdapter } from "./openai"; |
| 3 | + |
| 4 | +describe("openaiUpstreamAdapter reasoning compatibility", () => { |
| 5 | + test("parses non-stream reasoning field into thinking block", async () => { |
| 6 | + const response = new Response( |
| 7 | + JSON.stringify({ |
| 8 | + id: "chatcmpl-1", |
| 9 | + object: "chat.completion", |
| 10 | + created: 1700000000, |
| 11 | + model: "test-model", |
| 12 | + choices: [ |
| 13 | + { |
| 14 | + index: 0, |
| 15 | + message: { |
| 16 | + role: "assistant", |
| 17 | + content: "final answer", |
| 18 | + reasoning: "chain of thought summary", |
| 19 | + }, |
| 20 | + finish_reason: "stop", |
| 21 | + }, |
| 22 | + ], |
| 23 | + usage: { |
| 24 | + prompt_tokens: 1, |
| 25 | + completion_tokens: 2, |
| 26 | + total_tokens: 3, |
| 27 | + }, |
| 28 | + }), |
| 29 | + ); |
| 30 | + |
| 31 | + const parsed = await openaiUpstreamAdapter.parseResponse(response); |
| 32 | + expect(parsed.content).toEqual([ |
| 33 | + { type: "thinking", thinking: "chain of thought summary" }, |
| 34 | + { type: "text", text: "final answer" }, |
| 35 | + ]); |
| 36 | + }); |
| 37 | + |
| 38 | + test("prefers reasoning_content over reasoning when both exist", async () => { |
| 39 | + const response = new Response( |
| 40 | + JSON.stringify({ |
| 41 | + id: "chatcmpl-2", |
| 42 | + object: "chat.completion", |
| 43 | + created: 1700000001, |
| 44 | + model: "test-model", |
| 45 | + choices: [ |
| 46 | + { |
| 47 | + index: 0, |
| 48 | + message: { |
| 49 | + role: "assistant", |
| 50 | + content: "final answer", |
| 51 | + reasoning_content: "preferred reasoning content", |
| 52 | + reasoning: "fallback reasoning", |
| 53 | + }, |
| 54 | + finish_reason: "stop", |
| 55 | + }, |
| 56 | + ], |
| 57 | + usage: { |
| 58 | + prompt_tokens: 1, |
| 59 | + completion_tokens: 2, |
| 60 | + total_tokens: 3, |
| 61 | + }, |
| 62 | + }), |
| 63 | + ); |
| 64 | + |
| 65 | + const parsed = await openaiUpstreamAdapter.parseResponse(response); |
| 66 | + expect(parsed.content).toEqual([ |
| 67 | + { type: "thinking", thinking: "preferred reasoning content" }, |
| 68 | + { type: "text", text: "final answer" }, |
| 69 | + ]); |
| 70 | + }); |
| 71 | + |
| 72 | + test("parses stream delta reasoning field into thinking_delta", async () => { |
| 73 | + const stream = [ |
| 74 | + 'data: {"id":"chatcmpl-3","object":"chat.completion.chunk","created":1700000002,"model":"test-model","choices":[{"index":0,"delta":{"role":"assistant","reasoning":"stream reasoning"},"finish_reason":null}]}', |
| 75 | + 'data: {"id":"chatcmpl-3","object":"chat.completion.chunk","created":1700000002,"model":"test-model","choices":[{"index":0,"delta":{"content":"stream text"},"finish_reason":"stop"}]}', |
| 76 | + "data: [DONE]", |
| 77 | + ].join("\n"); |
| 78 | + |
| 79 | + const response = new Response(stream); |
| 80 | + const chunks: Array<unknown> = []; |
| 81 | + for await (const chunk of openaiUpstreamAdapter.parseStreamResponse( |
| 82 | + response, |
| 83 | + )) { |
| 84 | + chunks.push(chunk); |
| 85 | + } |
| 86 | + |
| 87 | + expect(chunks).toContainEqual({ |
| 88 | + type: "content_block_delta", |
| 89 | + index: 0, |
| 90 | + delta: { type: "thinking_delta", thinking: "stream reasoning" }, |
| 91 | + }); |
| 92 | + expect(chunks).toContainEqual({ |
| 93 | + type: "content_block_delta", |
| 94 | + index: 0, |
| 95 | + delta: { type: "text_delta", text: "stream text" }, |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments