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

reasoning-request.test.ts

131 lines4.0 KB

1import { beforeEach, describe, expect, it, vi } from 'vitest'
2import { resolveDebugOption } from '@tanstack/ai/adapter-internals'
3import { GeminiTextAdapter } from '../src/adapters/text'
4import { GeminiTextInteractionsAdapter } from '../src/experimental/text-interactions/adapter'
5import type { ReasoningRequest } from '@tanstack/ai'
6import type { GeminiModels } from '../src/model-meta'
7​
8const mocks = vi.hoisted(() => ({
9 generateContentStream: vi.fn(),
10 interactionsCreate: vi.fn(),
11}))
12​
13vi.mock('@google/genai', async () => {
14 const actual = await vi.importActual<any>('@google/genai')
15 class MockGoogleGenAI {
16 models = { generateContentStream: mocks.generateContentStream }
17 get interactions() {
18 return { create: mocks.interactionsCreate }
19 }
20 }
21 return { ...actual, GoogleGenAI: MockGoogleGenAI }
22})
23​
24const logger = resolveDebugOption(false)
25​
26async function* noChunks() {}
27​
28/** Run one call and return the `thinkingConfig` it sent. */
29async function thinkingConfig(
30 model: GeminiModels,
31 reasoning: ReasoningRequest | undefined,
32) {
33 mocks.generateContentStream.mockResolvedValue(noChunks())
34 const adapter = new GeminiTextAdapter({ apiKey: 'test' }, model)
35 for await (const _chunk of adapter.chatStream({
36 threadId: 'thread-1',
37 logger,
38 model,
39 messages: [{ role: 'user', content: 'hi' }],
40 ...(reasoning ? { reasoning } : {}),
41 })) {
42 // Drain the stream.
43 }
44 const [payload] = mocks.generateContentStream.mock.calls.at(-1) ?? []
45 return payload.config.thinkingConfig
47​
48const on = (level: ReasoningRequest['level']): ReasoningRequest => ({
49 level,
50 summary: true,
51})
52​
53describe('Gemini chat({ reasoning }) request shape', () => {
54 beforeEach(() => {
55 vi.clearAllMocks()
56 })
57​
58 it('sends no thinkingConfig without a reasoning request', async () => {
59 expect(await thinkingConfig('gemini-3.1-pro-preview', undefined)).toBe(
60 undefined,
61 )
62 })
63​
64 it('Gemini 3: thinkingLevel from the level', async () => {
65 expect(
66 await thinkingConfig('gemini-3-flash-preview', on('minimal')),
67 ).toEqual({ includeThoughts: true, thinkingLevel: 'MINIMAL' })
68 // gemini-3.1-pro-preview has no minimal: it clamps up to LOW.
69 expect(
70 await thinkingConfig('gemini-3.1-pro-preview', {
71 level: 'minimal',
72 summary: false,
73 }),
74 ).toEqual({ includeThoughts: false, thinkingLevel: 'LOW' })
75 })
76​
77 it('Gemini 2.5: thinkingBudget from pi table, or budgetTokens', async () => {
78 expect(await thinkingConfig('gemini-2.5-pro', on('high'))).toEqual({
79 includeThoughts: true,
80 thinkingBudget: 32768,
81 })
82 expect(await thinkingConfig('gemini-2.5-flash', on('high'))).toEqual({
83 includeThoughts: true,
84 thinkingBudget: 24576,
85 })
86 expect(
87 await thinkingConfig('gemini-2.5-pro', {
88 level: 'low',
89 summary: true,
90 budgetTokens: 3000,
91 }),
92 ).toEqual({ includeThoughts: true, thinkingBudget: 3000 })
93 })
94​
95 it('off: a zero budget', async () => {
96 expect(await thinkingConfig('gemini-2.5-flash', on('off'))).toEqual({
97 thinkingBudget: 0,
98 })
99 })
100​
101 it('off on Gemini 2.5 Pro, which cannot stop thinking: the minimal budget', async () => {
102 expect(await thinkingConfig('gemini-2.5-pro', on('off'))).toEqual({
103 includeThoughts: true,
104 thinkingBudget: 128,
105 })
106 })
107})
108​
109describe('Gemini Interactions chat({ reasoning }) request shape', () => {
110 it('sends thinking_level and thinking_summaries', async () => {
111 mocks.interactionsCreate.mockResolvedValue(noChunks())
112 const adapter = new GeminiTextInteractionsAdapter(
113 { apiKey: 'test' },
114 'gemini-3.8-flash',
115 )
116 for await (const _chunk of adapter.chatStream({
117 threadId: 'thread-1',
118 logger,
119 model: 'gemini-3.8-flash',
120 messages: [{ role: 'user', content: 'hi' }],
121 reasoning: { level: 'medium', summary: false },
122 })) {
123 // Drain the stream.
124 }
125 const [body] = mocks.interactionsCreate.mock.calls[0] ?? []
126 expect(body.generation_config).toEqual({
127 thinking_level: 'medium',
128 thinking_summaries: 'none',
129 })
130 })
131})