Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import OpenAI from 'openai'
3import { resolveDebugOption } from '@tanstack/ai/adapter-internals'
4import { OpenAIBaseChatCompletionsTextAdapter } from '../src/adapters/chat-completions-text'
5import { OpenAIBaseResponsesTextAdapter } from '../src/adapters/responses-text'
6import type {
7 ModelReasoning,
8 ReasoningRequest,
9 TextOptions,
10} from '@tanstack/ai'
11
12const logger = resolveDebugOption(false)
13const client = new OpenAI({ apiKey: 'test-api-key' })
14
15const DATA: ModelReasoning = {
16 budget: false,
17 levels: { off: 'none', low: 'low', high: 'high' },
18}
19
20class ChatAdapter extends OpenAIBaseChatCompletionsTextAdapter<string> {
21 constructor(private readonly data?: ModelReasoning) {
22 super('m', 'test', client)
23 }
24 protected override modelReasoning() {
25 return this.data
26 }
27 request(options: TextOptions) {
28 return this.mapOptionsToRequest(options)
29 }
30}
31
32class ResponsesAdapter extends OpenAIBaseResponsesTextAdapter<string> {
33 constructor(private readonly data?: ModelReasoning) {
34 super('m', 'test', client)
35 }
36 protected override modelReasoning() {
37 return this.data
38 }
39 request(options: TextOptions) {
40 return this.mapOptionsToRequest(options)
41 }
42}
43
44function options(reasoning?: ReasoningRequest): TextOptions {
45 return {
46 threadId: 'thread-1',
47 model: 'm',
48 logger,
49 messages: [{ role: 'user', content: 'hi' }],
50 ...(reasoning ? { reasoning } : {}),
51 }
52}
53
54describe('modelReasoning hook: Chat Completions', () => {
55 it('sends reasoning_effort, clamped to the model levels', () => {
56 const request = new ChatAdapter(DATA).request(
57 options({ level: 'medium', summary: true }),
58 )
59 expect(request).toMatchObject({ reasoning_effort: 'high' })
60 })
61
62 it('sends the off value', () => {
63 const request = new ChatAdapter(DATA).request(
64 options({ level: 'off', summary: true }),
65 )
66 expect(request).toMatchObject({ reasoning_effort: 'none' })
67 })
68
69 it('sends nothing without model data or without a request', () => {
70 expect(
71 new ChatAdapter().request(options({ level: 'high', summary: true })),
72 ).not.toHaveProperty('reasoning_effort')
73 expect(new ChatAdapter(DATA).request(options())).not.toHaveProperty(
74 'reasoning_effort',
75 )
76 })
77})
78
79describe('modelReasoning hook: Responses', () => {
80 it('sends reasoning.effort and a summary', () => {
81 const request = new ResponsesAdapter(DATA).request(
82 options({ level: 'high', summary: true }),
83 )
84 expect(request.reasoning).toEqual({ effort: 'high', summary: 'auto' })
85 })
86
87 it('sends no summary when summary is false or the level is off', () => {
88 expect(
89 new ResponsesAdapter(DATA).request(
90 options({ level: 'high', summary: false }),
91 ).reasoning,
92 ).toEqual({ effort: 'high' })
93 expect(
94 new ResponsesAdapter(DATA).request(
95 options({ level: 'off', summary: true }),
96 ).reasoning,
97 ).toEqual({ effort: 'none' })
98 })
99
100 it('sends nothing without model data', () => {
101 expect(
102 new ResponsesAdapter().request(options({ level: 'high', summary: true })),
103 ).not.toHaveProperty('reasoning')
104 })
105})