Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

reasoning-request.test.ts

110 lines3.3 KB

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 }
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 threadId: 'thread-1',
31 logger,
32 model,
33 messages: [{ role: 'user', content: 'hi' }],
34 ...(reasoning ? { reasoning } : {}),
35 })) {
36 // Drain the stream.
37 }
38 const [body] = create.mock.calls[0] ?? []
39 return (body as Record<string, unknown>).reasoning
41​
42describe('OpenAI chat({ reasoning })', () => {
43 it('sends the effort and asks for a summary', async () => {
44 expect(
45 await reasoningFor('gpt-5.2', { level: 'high', summary: true }),
46 ).toEqual({
47 effort: 'high',
48 summary: 'auto',
49 })
50 })
51​
52 it('sends no summary when summary is false', async () => {
53 expect(
54 await reasoningFor('gpt-5.2', { level: 'low', summary: false }),
55 ).toEqual({
56 effort: 'low',
57 })
58 })
59​
60 it('sends none for off, on a model that can stop thinking', async () => {
61 expect(
62 await reasoningFor('gpt-5.2', { level: 'off', summary: true }),
63 ).toEqual({
64 effort: 'none',
65 })
66 })
67​
68 it('clamps a level the model does not have', async () => {
69 // gpt-5.2 tops out at xhigh.
70 expect(
71 await reasoningFor('gpt-5.2', { level: 'max', summary: true }),
72 ).toEqual({
73 effort: 'xhigh',
74 summary: 'auto',
75 })
76 })
77​
78 it('sends nothing without the option, or for a model that does not reason', async () => {
79 expect(await reasoningFor('gpt-5.2', undefined)).toBeUndefined()
80 expect(
81 await reasoningFor('gpt-4.1', { level: 'high', summary: true }),
82 ).toBeUndefined()
83 })
84})
85​
86describe('OpenAI Chat Completions chat({ reasoning })', () => {
87 class ChatAdapter extends OpenAIChatCompletionsTextAdapter<'gpt-5.2'> {
88 get sdk() {
89 return this.client
90 }
91 }
92​
93 it('sends reasoning_effort, clamped to the model levels', async () => {
94 const adapter = new ChatAdapter({ apiKey: 'test' }, 'gpt-5.2')
95 const create = vi.fn().mockResolvedValue(noEvents())
96 adapter.sdk.chat.completions.create =
97 create as typeof adapter.sdk.chat.completions.create
98 for await (const _chunk of adapter.chatStream({
99 threadId: 'thread-1',
100 logger,
101 model: 'gpt-5.2',
102 messages: [{ role: 'user', content: 'hi' }],
103 reasoning: { level: 'max', summary: true },
104 })) {
105 // Drain the stream.
106 }
107 const [body] = create.mock.calls[0] ?? []
108 expect(body).toMatchObject({ reasoning_effort: 'xhigh' })
109 })
110})