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

provider-supports.ts

204 lines6.2 KB

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