Open History to see the last commit
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 logger,
37 model,
38 messages: [{ role: 'user', content: 'hi' }],
39 ...(reasoning ? { reasoning } : {}),
40 })) {
41 // Drain the stream.
42 }
43 const [payload] = mocks.generateContentStream.mock.calls.at(-1) ?? []
44 return payload.config.thinkingConfig
45}
46
47const on = (level: ReasoningRequest['level']): ReasoningRequest => ({
48 level,
49 summary: true,
50})
51
52describe('Gemini chat({ reasoning }) request shape', () => {
53 beforeEach(() => {
54 vi.clearAllMocks()
55 })
56
57 it('sends no thinkingConfig without a reasoning request', async () => {
58 expect(await thinkingConfig('gemini-3.1-pro-preview', undefined)).toBe(
59 undefined,
60 )
61 })
62
63 it('Gemini 3: thinkingLevel from the level', async () => {
64 expect(
65 await thinkingConfig('gemini-3-flash-preview', on('minimal')),
66 ).toEqual({ includeThoughts: true, thinkingLevel: 'MINIMAL' })
67 // gemini-3.1-pro-preview has no minimal: it clamps up to LOW.
68 expect(
69 await thinkingConfig('gemini-3.1-pro-preview', {
70 level: 'minimal',
71 summary: false,
72 }),
73 ).toEqual({ includeThoughts: false, thinkingLevel: 'LOW' })
74 })
75
76 it('Gemini 2.5: thinkingBudget from pi table, or budgetTokens', async () => {
77 expect(await thinkingConfig('gemini-2.5-pro', on('high'))).toEqual({
78 includeThoughts: true,
79 thinkingBudget: 32768,
80 })
81 expect(await thinkingConfig('gemini-2.5-flash', on('high'))).toEqual({
82 includeThoughts: true,
83 thinkingBudget: 24576,
84 })
85 expect(
86 await thinkingConfig('gemini-2.5-pro', {
87 level: 'low',
88 summary: true,
89 budgetTokens: 3000,
90 }),
91 ).toEqual({ includeThoughts: true, thinkingBudget: 3000 })
92 })
93
94 it('off: a zero budget', async () => {
95 expect(await thinkingConfig('gemini-2.5-flash', on('off'))).toEqual({
96 thinkingBudget: 0,
97 })
98 })
99
100 it('off on Gemini 2.5 Pro, which cannot stop thinking: the minimal budget', async () => {
101 expect(await thinkingConfig('gemini-2.5-pro', on('off'))).toEqual({
102 includeThoughts: true,
103 thinkingBudget: 128,
104 })
105 })
106})
107
108describe('Gemini Interactions chat({ reasoning }) request shape', () => {
109 it('sends thinking_level and thinking_summaries', async () => {
110 mocks.interactionsCreate.mockResolvedValue(noChunks())
111 const adapter = new GeminiTextInteractionsAdapter(
112 { apiKey: 'test' },
113 'gemini-3.8-flash',
114 )
115 for await (const _chunk of adapter.chatStream({
116 logger,
117 model: 'gemini-3.8-flash',
118 messages: [{ role: 'user', content: 'hi' }],
119 reasoning: { level: 'medium', summary: false },
120 })) {
121 // Drain the stream.
122 }
123 const [body] = mocks.interactionsCreate.mock.calls[0] ?? []
124 expect(body.generation_config).toEqual({
125 thinking_level: 'medium',
126 thinking_summaries: 'none',
127 })
128 })
129})