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

provider-supports.ts

250 lines7.8 KB

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