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 model: 'm',
47 logger,
48 messages: [{ role: 'user', content: 'hi' }],
49 ...(reasoning ? { reasoning } : {}),
50 }
51}
52
53describe('modelReasoning hook: Chat Completions', () => {
54 it('sends reasoning_effort, clamped to the model levels', () => {
55 const request = new ChatAdapter(DATA).request(
56 options({ level: 'medium', summary: true }),
57 )
58 expect(request).toMatchObject({ reasoning_effort: 'high' })
59 })
60
61 it('sends the off value', () => {
62 const request = new ChatAdapter(DATA).request(
63 options({ level: 'off', summary: true }),
64 )
65 expect(request).toMatchObject({ reasoning_effort: 'none' })
66 })
67
68 it('sends nothing without model data or without a request', () => {
69 expect(
70 new ChatAdapter().request(options({ level: 'high', summary: true })),
71 ).not.toHaveProperty('reasoning_effort')
72 expect(new ChatAdapter(DATA).request(options())).not.toHaveProperty(
73 'reasoning_effort',
74 )
75 })
76})
77
78describe('modelReasoning hook: Responses', () => {
79 it('sends reasoning.effort and a summary', () => {
80 const request = new ResponsesAdapter(DATA).request(
81 options({ level: 'high', summary: true }),
82 )
83 expect(request.reasoning).toEqual({ effort: 'high', summary: 'auto' })
84 })
85
86 it('sends no summary when summary is false or the level is off', () => {
87 expect(
88 new ResponsesAdapter(DATA).request(
89 options({ level: 'high', summary: false }),
90 ).reasoning,
91 ).toEqual({ effort: 'high' })
92 expect(
93 new ResponsesAdapter(DATA).request(
94 options({ level: 'off', summary: true }),
95 ).reasoning,
96 ).toEqual({ effort: 'none' })
97 })
98
99 it('sends nothing without model data', () => {
100 expect(
101 new ResponsesAdapter().request(options({ level: 'high', summary: true })),
102 ).not.toHaveProperty('reasoning')
103 })
104})