Open History to see the last commit
1import { describe, expect, it, vi } from 'vitest'
2import { resolveDebugOption } from '@tanstack/ai/adapter-internals'
3import { OpenAITextAdapter } from '../src/adapters/text'
4import { OpenAIChatCompletionsTextAdapter } from '../src/adapters/text-chat-completions'
5import type { OpenAIChatModel } from '../src/model-meta'
6import type { ReasoningRequest } from '@tanstack/ai'
7
8const logger = resolveDebugOption(false)
9
10/** The adapter, with its SDK client in reach, so a test can stub it. */
11class TestAdapter<
12 TModel extends OpenAIChatModel,
13> extends OpenAITextAdapter<TModel> {
14 get sdk() {
15 return this.client
16 }
17}
18
19async function* noEvents() {}
20
21/** The `reasoning` field of the Responses request for one call. */
22async function reasoningFor(
23 model: OpenAIChatModel,
24 reasoning: ReasoningRequest | undefined,
25) {
26 const adapter = new TestAdapter({ apiKey: 'test' }, model)
27 const create = vi.fn().mockResolvedValue(noEvents())
28 adapter.sdk.responses.create = create as typeof adapter.sdk.responses.create
29 for await (const _chunk of adapter.chatStream({
30 logger,
31 model,
32 messages: [{ role: 'user', content: 'hi' }],
33 ...(reasoning ? { reasoning } : {}),
34 })) {
35 // Drain the stream.
36 }
37 const [body] = create.mock.calls[0] ?? []
38 return (body as Record<string, unknown>).reasoning
39}
40
41describe('OpenAI chat({ reasoning })', () => {
42 it('sends the effort and asks for a summary', async () => {
43 expect(
44 await reasoningFor('gpt-5.2', { level: 'high', summary: true }),
45 ).toEqual({
46 effort: 'high',
47 summary: 'auto',
48 })
49 })
50
51 it('sends no summary when summary is false', async () => {
52 expect(
53 await reasoningFor('gpt-5.2', { level: 'low', summary: false }),
54 ).toEqual({
55 effort: 'low',
56 })
57 })
58
59 it('sends none for off, on a model that can stop thinking', async () => {
60 expect(
61 await reasoningFor('gpt-5.2', { level: 'off', summary: true }),
62 ).toEqual({
63 effort: 'none',
64 })
65 })
66
67 it('clamps a level the model does not have', async () => {
68 // gpt-5.2 tops out at xhigh.
69 expect(
70 await reasoningFor('gpt-5.2', { level: 'max', summary: true }),
71 ).toEqual({
72 effort: 'xhigh',
73 summary: 'auto',
74 })
75 })
76
77 it('sends nothing without the option, or for a model that does not reason', async () => {
78 expect(await reasoningFor('gpt-5.2', undefined)).toBeUndefined()
79 expect(
80 await reasoningFor('gpt-4.1', { level: 'high', summary: true }),
81 ).toBeUndefined()
82 })
83})
84
85describe('OpenAI Chat Completions chat({ reasoning })', () => {
86 class ChatAdapter extends OpenAIChatCompletionsTextAdapter<'gpt-5.2'> {
87 get sdk() {
88 return this.client
89 }
90 }
91
92 it('sends reasoning_effort, clamped to the model levels', async () => {
93 const adapter = new ChatAdapter({ apiKey: 'test' }, 'gpt-5.2')
94 const create = vi.fn().mockResolvedValue(noEvents())
95 adapter.sdk.chat.completions.create =
96 create as typeof adapter.sdk.chat.completions.create
97 for await (const _chunk of adapter.chatStream({
98 logger,
99 model: 'gpt-5.2',
100 messages: [{ role: 'user', content: 'hi' }],
101 reasoning: { level: 'max', summary: true },
102 })) {
103 // Drain the stream.
104 }
105 const [body] = create.mock.calls[0] ?? []
106 expect(body).toMatchObject({ reasoning_effort: 'xhigh' })
107 })
108})