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

provider-supports.ts

252 lines7.9 KB

1/**
2 * `supports` blocks for newly synced native-provider models.
3 *
4 * Input modalities and `supported_parameters` come from the native
5 * modelschemas catalog (OpenRouter fills a field only when the native row
6 * is empty). Server-tool lists are the current first-party vocabulary for
7 * openai, anthropic, gemini and grok, written only when the catalog lists
8 * `tools` or `tool_choice`. Anthropic thinking flags follow the same
9 * parameter split as `buildAnthropicProviderOptionsType`.
10 */
11​
12export const SYNCED_PROVIDERS = [
13 'openai',
14 'anthropic',
15 'gemini',
16 'grok',
17 'groq',
18 'mistral',
19 'byteplus',
20] as const
21​
22export type SyncedProvider = (typeof SYNCED_PROVIDERS)[number]
23​
24export interface ProviderSupportsInput {
25 provider: SyncedProvider
26 inputModalities: Array<string>
27 outputModalities?: Array<string>
28 supportedParameters?: Array<string>
29 reasoningMandatory?: boolean
31​
32// New Claude models take computer use only through the computer toolset.
33// `computer` is the generic tool from `@tanstack/ai-computer-use`. Every
34// model with a computer version gets it.
35const ANTHROPIC_SERVER_TOOLS = [
36 'web_search',
37 'web_fetch',
38 'code_execution',
39 'computer',
40 'computer_toolset',
41 'bash',
42 'text_editor',
43 'memory',
44] as const
45​
46// `computer` goes with `computer_use`, as in the Claude list above.
47const OPENAI_SERVER_TOOLS = [
48 'web_search',
49 'web_search_preview',
50 'file_search',
51 'image_generation',
52 'code_interpreter',
53 'mcp',
54 'computer_use',
55 'computer',
56 'local_shell',
57 'shell',
58 'apply_patch',
59] as const
60​
61const GEMINI_SERVER_TOOLS = [
62 'code_execution',
63 'file_search',
64 'google_search',
65 'url_context',
66] as const
67​
68const GROK_SERVER_TOOLS = [
69 'web_search',
70 'x_search',
71 'file_search',
72 'mcp',
73] as const
74​
75function hasParam(params: Array<string>, names: Array<string>): boolean {
76 return names.some((name) => params.includes(name))
78​
79function quoteList(values: Array<string>): string {
80 return `[${values.map((value) => `'${value}'`).join(', ')}]`
82​
83interface AnthropicProviderOptionsInput {
84 supportedParameters?: Array<string>
85 hasCachedPricing?: boolean
87​
88const ANTHROPIC_BASE_OPTIONS = [
89 'AnthropicContainerOptions',
90 'AnthropicContextManagementOptions',
91 'AnthropicMCPOptions',
92 'AnthropicServiceTierOptions',
93 'AnthropicStopSequencesOptions',
94] as const
95​
96/**
97 * Per-model Anthropic provider-options intersection, inferred from the
98 * modelschemas capabilities (OpenRouter's when the native row has none).
99 * Does not copy another model's tool list. Thinking is not here:
100 * `chat({ reasoning })` sets it, with the levels of each model's `reasoning`
101 * field in `model-meta.ts`.
102 *
103 * - sampling listed → sampling options.
104 * - no sampling (Sonnet 5, Opus 4.7+, Fable 5) → `max_tokens` only.
105 */
106export function buildAnthropicProviderOptionsType(
107 input: AnthropicProviderOptionsInput,
108): string {
109 const params = input.supportedParameters ?? []
110 const hasSampling = hasParam(params, ['temperature', 'top_p', 'top_k'])
111​
112 const parts: Array<string> = [...ANTHROPIC_BASE_OPTIONS]
113 if (input.hasCachedPricing) {
114 parts.unshift('AnthropicCacheControlOptions')
115 }
116​
117 parts.push('AnthropicToolChoiceOptions')
118 parts.push(
119 hasSampling ? 'AnthropicSamplingOptions' : 'AnthropicMaxTokensOptions',
120 )
121​
122 return parts.join(' & ')
124​
125function toolsLine(hasTools: boolean, tools: ReadonlyArray<string>): string {
126 return ` tools: ${hasTools ? quoteList([...tools]) : '[]'},`
128​
129/**
130 * Thinking flags for a new Anthropic model. Same split as
131 * `buildAnthropicProviderOptionsType`: mandatory or reasoning-without-sampling
132 * is adaptive-only, reasoning-plus-sampling keeps budget thinking too, and
133 * no reasoning param is budget thinking. `priority_tier` is on every current
134 * hand-written Claude entry that lists thinking flags.
135 */
136export function buildAnthropicSupportsFlags(input: {
137 supportedParameters?: Array<string>
138 reasoningMandatory?: boolean
139}): Array<string> {
140 const params = input.supportedParameters ?? []
141 const hasSampling = hasParam(params, ['temperature', 'top_p', 'top_k'])
142 const hasReasoning = hasParam(params, [
143 'include_reasoning',
144 'reasoning',
145 'reasoning_effort',
146 ])
147 const adaptiveOnly =
148 input.reasoningMandatory === true || (hasReasoning && !hasSampling)
149 const lines = [` extended_thinking: ${adaptiveOnly ? 'false' : 'true'},`]
150 if (adaptiveOnly || hasReasoning) {
151 lines.push(` adaptive_thinking: true,`)
152 }
153 lines.push(` priority_tier: true,`)
154 return lines
156​
157export function buildProviderSupportsBody(
158 input: ProviderSupportsInput,
159): string {
160 const params = input.supportedParameters ?? []
161 const inputList = quoteList(input.inputModalities)
162 const hasTools = hasParam(params, ['tools', 'tool_choice'])
163 const hasStructured = hasParam(params, [
164 'response_format',
165 'structured_outputs',
166 ])
167 const hasReasoning = hasParam(params, [
168 'include_reasoning',
169 'reasoning',
170 'reasoning_effort',
171 ])
172​
173 switch (input.provider) {
174 case 'openai': {
175 const features = ['streaming']
176 if (hasTools) features.push('function_calling')
177 if (hasStructured) features.push('structured_outputs')
178 return [
179 ` input: ${inputList},`,
180 ` output: ['text'],`,
181 ` endpoints: ['chat', 'chat-completions'],`,
182 ` features: ${quoteList(features)},`,
183 toolsLine(hasTools, OPENAI_SERVER_TOOLS),
184 ].join('\n')
185 }
186 case 'anthropic':
187 return [
188 ` input: ${inputList},`,
189 ...buildAnthropicSupportsFlags(input),
190 toolsLine(hasTools, ANTHROPIC_SERVER_TOOLS),
191 ].join('\n')
192 case 'gemini': {
193 const capabilities: Array<string> = []
194 if (hasTools) capabilities.push('function_calling')
195 if (hasStructured) capabilities.push('structured_output')
196 if (hasReasoning) capabilities.push('thinking')
197 const lines = [` input: ${inputList},`, ` output: ['text'],`]
198 if (capabilities.length > 0) {
199 lines.push(` capabilities: ${quoteList(capabilities)},`)
200 }
201 lines.push(toolsLine(hasTools, GEMINI_SERVER_TOOLS))
202 return lines.join('\n')
203 }
204 case 'grok': {
205 const capabilities: Array<string> = []
206 if (hasReasoning) capabilities.push('reasoning')
207 if (hasStructured) capabilities.push('structured_outputs')
208 if (hasTools) capabilities.push('tool_calling')
209 const lines = [` input: ${inputList},`, ` output: ['text'],`]
210 if (capabilities.length > 0) {
211 lines.push(` capabilities: ${quoteList(capabilities)},`)
212 }
213 lines.push(toolsLine(hasTools, GROK_SERVER_TOOLS))
214 return lines.join('\n')
215 }
216 case 'groq':
217 case 'mistral': {
218 const features = ['streaming']
219 if (hasTools) features.push('tools')
220 if (hasStructured) {
221 features.push('json_object', 'json_schema')
222 }
223 if (hasReasoning) features.push('reasoning')
224 if (input.inputModalities.includes('image')) features.push('vision')
225 const lines = [
226 ` input: ${inputList},`,
227 ` output: ['text'],`,
228 ` endpoints: ['chat'],`,
229 ` features: ${quoteList(features)},`,
230 ]
231 // Groq hosted tools (browser_search, …) are per-model; never guess them.
232 if (input.provider === 'groq') lines.push(` tools: [] as const,`)
233 return lines.join('\n')
234 }
235 case 'byteplus': {
236 // No `structured_outputs`: the runtime gate is the live-probed
237 // BYTEPLUS_STRUCTURED_OUTPUT_CHAT_MODELS list, which a sync cannot update.
238 const capabilities: Array<string> = []
239 if (hasReasoning) capabilities.push('reasoning')
240 if (hasTools) capabilities.push('tool_calling')
241 const output = quoteList(
242 input.outputModalities?.length ? input.outputModalities : ['text'],
243 )
244 const lines = [` input: ${inputList},`, ` output: ${output},`]
245 if (capabilities.length > 0) {
246 lines.push(` capabilities: ${quoteList(capabilities)},`)
247 }
248 lines.push(` tools: [] as const,`)
249 return lines.join('\n')
250 }
251 }