Open History to see the last commit
1import { describe, expect, it, vi } from 'vitest'
2import OpenAI from 'openai'
3import { EventType } from '@tanstack/ai'
4import { resolveDebugOption } from '@tanstack/ai/adapter-internals'
5import { OpenAICompatibleChatAdapter } from '../src/compatible/adapter'
6import type { AdapterYieldChunk } from '@tanstack/ai'
7
8const testLogger = resolveDebugOption(false)
9
10function createAsyncIterable<T>(chunks: Array<T>): AsyncIterable<T> {
11 return {
12 [Symbol.asyncIterator]() {
13 let index = 0
14 return {
15 async next() {
16 if (index < chunks.length) {
17 return { value: chunks[index++]!, done: false }
18 }
19 return { value: undefined as T, done: true }
20 },
21 }
22 },
23 }
24}
25
26function makeAdapter(streamChunks: Array<Record<string, unknown>>) {
27 const client = new OpenAI({ apiKey: 'test-api-key' })
28 client.chat.completions.create = vi
29 .fn()
30 .mockResolvedValue(
31 createAsyncIterable(streamChunks),
32 ) as typeof client.chat.completions.create
33 return new OpenAICompatibleChatAdapter(
34 client,
35 'deepseek-reasoner',
36 'deepseek',
37 )
38}
39
40async function collect(streamChunks: Array<Record<string, unknown>>) {
41 const adapter = makeAdapter(streamChunks)
42 const chunks: Array<AdapterYieldChunk> = []
43 for await (const chunk of adapter.chatStream({
44 logger: testLogger,
45 model: 'deepseek-reasoner',
46 messages: [{ role: 'user', content: 'What is 2 + 2?' }],
47 })) {
48 chunks.push(chunk)
49 }
50 return chunks
51}
52
53function reasoningText(chunks: Array<AdapterYieldChunk>) {
54 return chunks
55 .filter(
56 (chunk): chunk is Extract<AdapterYieldChunk, { delta: string }> =>
57 chunk.type === EventType.REASONING_MESSAGE_CONTENT,
58 )
59 .map((chunk) => chunk.delta)
60 .join('')
61}
62
63/**
64 * Issue #982: an OpenAI-compatible reasoning provider streams its thinking on
65 * `delta.reasoning_content` (or `delta.reasoning`), which is outside the OpenAI
66 * wire format. The generic adapter had no reasoning hook, so the thinking was
67 * dropped and the documented workaround was to monkey-patch the prototype.
68 */
69describe('OpenAICompatibleChatAdapter reasoning', () => {
70 const textChunks = [
71 { choices: [{ index: 0, delta: { content: '4' } }] },
72 { choices: [{ index: 0, delta: {}, finish_reason: 'stop' }] },
73 ]
74
75 it('streams reasoning delivered as reasoning_content', async () => {
76 const chunks = await collect([
77 { choices: [{ index: 0, delta: { reasoning_content: 'Two plus two' } }] },
78 { choices: [{ index: 0, delta: { reasoning_content: ' is four.' } }] },
79 ...textChunks,
80 ])
81
82 expect(
83 chunks.some((chunk) => chunk.type === EventType.REASONING_START),
84 ).toBe(true)
85 expect(reasoningText(chunks)).toBe('Two plus two is four.')
86 })
87
88 it('streams reasoning delivered as reasoning', async () => {
89 const chunks = await collect([
90 { choices: [{ index: 0, delta: { reasoning: 'Thinking...' } }] },
91 ...textChunks,
92 ])
93
94 expect(reasoningText(chunks)).toBe('Thinking...')
95 })
96
97 it('prefers reasoning_content when a provider sends both', async () => {
98 const chunks = await collect([
99 {
100 choices: [
101 {
102 index: 0,
103 delta: { reasoning_content: 'canonical', reasoning: 'duplicate' },
104 },
105 ],
106 },
107 ...textChunks,
108 ])
109
110 expect(reasoningText(chunks)).toBe('canonical')
111 })
112
113 it('emits no reasoning events for a provider that sends neither field', async () => {
114 const chunks = await collect(textChunks)
115
116 expect(
117 chunks.some(
118 (chunk) =>
119 chunk.type === EventType.REASONING_START ||
120 chunk.type === EventType.REASONING_MESSAGE_CONTENT,
121 ),
122 ).toBe(false)
123 expect(
124 chunks.some((chunk) => chunk.type === EventType.TEXT_MESSAGE_CONTENT),
125 ).toBe(true)
126 })
127})