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

native-insert.test.ts

187 lines5.5 KB

1import { describe, expect, it } from 'vitest'
2import { applyChatModelCatalogInserts } from './native-insert'
3​
4const STUB = `const FOO = {
5 id: 'claude-foo',
6 supports: { input: ['text'], tools: [] },
7 max_output_tokens: 1000,
8}
9​
10export const ANTHROPIC_MODELS = [
11 FOO.id,
12] as const
13​
14export type AnthropicChatModelProviderOptionsByName = {
15 [FOO.id]: AnthropicSamplingOptions
17​
18export type AnthropicChatModelToolCapabilitiesByName = {
19 [FOO.id]: typeof FOO.supports.tools
21​
22export type AnthropicModelInputModalitiesByName = {
23 [FOO.id]: typeof FOO.supports.input
25​
26export const ANTHROPIC_MODEL_INPUT_MODALITIES: Readonly<
27 Record<string, ReadonlyArray<Modality>>
28> = {
29 [FOO.id]: FOO.supports.input,
30} satisfies AnthropicModelInputModalitiesByName
31​
32const ANTHROPIC_MODEL_MAX_OUTPUT_TOKENS: Record<string, number> = {
33 [FOO.id]: FOO.max_output_tokens,
35​
36export const ANTHROPIC_COMBINED_TOOLS_AND_SCHEMA_MODELS = new Set<string>([
37 FOO.id,
38])
40​
41const ANTHROPIC_CONFIG = {
42 chatArrayName: 'ANTHROPIC_MODELS',
43 arrayRef: '.id' as const,
44 providerOptionsTypeName: 'AnthropicChatModelProviderOptionsByName',
45 inputModalitiesTypeName: 'AnthropicModelInputModalitiesByName',
46 toolCapabilitiesTypeName: 'AnthropicChatModelToolCapabilitiesByName',
47 maxOutputTokensMapName: 'ANTHROPIC_MODEL_MAX_OUTPUT_TOKENS',
48 combinedToolsAndSchemaSetName: 'ANTHROPIC_COMBINED_TOOLS_AND_SCHEMA_MODELS',
49 providerOptionsIsMappedType: false,
51​
52describe('applyChatModelCatalogInserts', () => {
53 it('writes the tool-capabilities row for a new Anthropic model', () => {
54 const result = applyChatModelCatalogInserts(STUB, ANTHROPIC_CONFIG, [
55 {
56 constName: 'CLAUDE_FABLE_5_1',
57 providerOptionsEntry:
58 'AnthropicToolChoiceOptions & AnthropicMaxTokensOptions',
59 hasMaxOutputTokens: true,
60 acceptsCombinedToolsAndSchema: true,
61 },
62 ])
63​
64 expect(result).toContain('CLAUDE_FABLE_5_1.id,')
65 expect(result).toContain(
66 '[CLAUDE_FABLE_5_1.id]: typeof CLAUDE_FABLE_5_1.supports.tools',
67 )
68 expect(result).toContain(
69 '[CLAUDE_FABLE_5_1.id]: typeof CLAUDE_FABLE_5_1.supports.input',
70 )
71 expect(result).toContain(
72 '[CLAUDE_FABLE_5_1.id]: AnthropicToolChoiceOptions & AnthropicMaxTokensOptions',
73 )
74 expect(result).toContain(
75 '[CLAUDE_FABLE_5_1.id]: CLAUDE_FABLE_5_1.max_output_tokens,',
76 )
77 expect(result).toContain(
78 'export const ANTHROPIC_COMBINED_TOOLS_AND_SCHEMA_MODELS = new Set<string>([\n CLAUDE_FABLE_5_1.id,',
79 )
80 })
81​
82 it('keeps the reasoning maps as they are', () => {
83 const reasoning = `
84const ANTHROPIC_REASONING = {
85 [FOO.id]: FOO.reasoning,
87​
88export type AnthropicModelReasoningByName = {
89 [K in keyof typeof ANTHROPIC_REASONING]: ModelReasoningCapability<
90 (typeof ANTHROPIC_REASONING)[K]
91 >
93​
94export const ANTHROPIC_MODEL_REASONING: Readonly<
95 Record<string, ModelReasoning>
96> = ANTHROPIC_REASONING
98 const result = applyChatModelCatalogInserts(
99 STUB + reasoning,
100 ANTHROPIC_CONFIG,
101 [
102 {
103 constName: 'CLAUDE_FABLE_5_1',
104 providerOptionsEntry: 'AnthropicMaxTokensOptions',
105 hasMaxOutputTokens: true,
106 acceptsCombinedToolsAndSchema: true,
107 },
108 ],
109 )
110​
111 expect(result.endsWith(reasoning)).toBe(true)
112 })
113​
114 it('leaves a -fast Anthropic model out of the combined set', () => {
115 const result = applyChatModelCatalogInserts(STUB, ANTHROPIC_CONFIG, [
116 {
117 constName: 'CLAUDE_OPUS_5_FAST',
118 providerOptionsEntry: 'AnthropicSamplingOptions',
119 hasMaxOutputTokens: true,
120 acceptsCombinedToolsAndSchema: false,
121 },
122 ])
123​
124 expect(result).toContain('CLAUDE_OPUS_5_FAST.id,')
125 const set = result.slice(
126 result.indexOf('ANTHROPIC_COMBINED_TOOLS_AND_SCHEMA_MODELS'),
127 )
128 expect(set).not.toContain('CLAUDE_OPUS_5_FAST')
129 })
130​
131 it('writes the tool-capabilities row for OpenAI and Gemini (.name refs)', () => {
132 const openaiStub = `export const OPENAI_CHAT_MODELS = [
133 GPT5.name,
134] as const
135​
136export type OpenAIChatModelProviderOptionsByName = {
137 [GPT5.name]: OpenAIBaseOptions
139​
140export type OpenAIChatModelToolCapabilitiesByName = {
141 [GPT5.name]: typeof GPT5.supports.tools
143​
144export type OpenAIModelInputModalitiesByName = {
145 [GPT5.name]: typeof GPT5.supports.input
148 const result = applyChatModelCatalogInserts(
149 openaiStub,
150 {
151 chatArrayName: 'OPENAI_CHAT_MODELS',
152 arrayRef: '.name',
153 providerOptionsTypeName: 'OpenAIChatModelProviderOptionsByName',
154 inputModalitiesTypeName: 'OpenAIModelInputModalitiesByName',
155 toolCapabilitiesTypeName: 'OpenAIChatModelToolCapabilitiesByName',
156 providerOptionsIsMappedType: false,
157 },
158 [
159 {
160 constName: 'GPT6',
161 providerOptionsEntry: 'OpenAIBaseOptions',
162 hasMaxOutputTokens: false,
163 },
164 ],
165 )
166​
167 expect(result).toContain('[GPT6.name]: typeof GPT6.supports.tools')
168 expect(result).toContain('GPT6.name,')
169 // This stub has no runtime input-modalities map, so none is written.
170 expect(result).not.toContain('GPT6.supports.input,')
171 })
172​
173 it('writes the input-modalities row into the type map and the runtime map', () => {
174 const result = applyChatModelCatalogInserts(STUB, ANTHROPIC_CONFIG, [
175 {
176 constName: 'CLAUDE_FABLE_5_1',
177 providerOptionsEntry: 'AnthropicSamplingOptions',
178 hasMaxOutputTokens: true,
179 },
180 ])
181​
182 expect(result).toContain(`> = {
183 [FOO.id]: FOO.supports.input,
184 [CLAUDE_FABLE_5_1.id]: CLAUDE_FABLE_5_1.supports.input,
185} satisfies AnthropicModelInputModalitiesByName`)
186 })
187})