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

provider-supports.ts

221 lines6.6 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​
20const ANTHROPIC_SERVER_TOOLS = [
21 'web_search',
22 'web_fetch',
23 'code_execution',
24 'computer_use',
25 'bash',
26 'text_editor',
27 'memory',
28] as const
29​
30const OPENAI_SERVER_TOOLS = [
31 'web_search',
32 'web_search_preview',
33 'file_search',
34 'image_generation',
35 'code_interpreter',
36 'mcp',
37 'computer_use',
38 'local_shell',
39 'shell',
40 'apply_patch',
41] as const
42​
43const GEMINI_SERVER_TOOLS = [
44 'code_execution',
45 'file_search',
46 'google_search',
47 'url_context',
48] as const
49​
50const GROK_SERVER_TOOLS = [
51 'web_search',
52 'x_search',
53 'file_search',
54 'mcp',
55] as const
56​
57function hasParam(params: Array<string>, names: Array<string>): boolean {
58 return names.some((name) => params.includes(name))
60​
61function quoteList(values: Array<string>): string {
62 return `[${values.map((value) => `'${value}'`).join(', ')}]`
64​
65interface AnthropicProviderOptionsInput {
66 supportedParameters?: Array<string>
67 reasoningMandatory?: boolean
68 hasCachedPricing?: boolean
70​
71const ANTHROPIC_BASE_OPTIONS = [
72 'AnthropicContainerOptions',
73 'AnthropicContextManagementOptions',
74 'AnthropicMCPOptions',
75 'AnthropicServiceTierOptions',
76 'AnthropicStopSequencesOptions',
77] as const
78​
79/**
80 * Per-model Anthropic provider-options intersection, inferred from the
81 * OpenRouter catalog. Does not copy another model's tool list.
82 *
83 * - `reasoning.mandatory` → adaptive-only thinking (Fable 5 / 5.1).
84 * - reasoning params without sampling → adaptive-or-disabled (Sonnet 5,
85 * Opus 4.7+).
86 * - reasoning + sampling → adaptive union plus sampling (Opus/Sonnet 4.6).
87 * - no reasoning → budget-based thinking plus sampling when listed.
88 */
89export function buildAnthropicProviderOptionsType(
90 input: AnthropicProviderOptionsInput,
91): string {
92 const params = input.supportedParameters ?? []
93 const hasSampling = hasParam(params, ['temperature', 'top_p', 'top_k'])
94 const hasReasoning = hasParam(params, [
95 'include_reasoning',
96 'reasoning',
97 'reasoning_effort',
98 ])
99​
100 const parts: Array<string> = [...ANTHROPIC_BASE_OPTIONS]
101 if (input.hasCachedPricing) {
102 parts.unshift('AnthropicCacheControlOptions')
103 }
104​
105 if (input.reasoningMandatory) {
106 parts.push('AnthropicAdaptiveOnlyThinkingOptions')
107 } else if (hasReasoning && hasSampling) {
108 parts.push('AnthropicAdaptiveThinkingOptions')
109 } else if (hasReasoning) {
110 parts.push('AnthropicAdaptiveOrDisabledThinkingOptions')
111 } else {
112 parts.push('AnthropicThinkingOptions')
113 }
114​
115 parts.push('AnthropicToolChoiceOptions')
116​
117 if (hasSampling) {
118 parts.push('AnthropicSamplingOptions')
119 } else {
120 parts.push('AnthropicMaxTokensOptions')
121 if (hasReasoning || input.reasoningMandatory) {
122 parts.push('AnthropicOutputConfigOptions')
123 }
124 }
125​
126 return parts.join(' & ')
128​
129function toolsLine(hasTools: boolean, tools: ReadonlyArray<string>): string {
130 return ` tools: ${hasTools ? quoteList([...tools]) : '[]'},`
132​
133/**
134 * Thinking flags for a new Anthropic model. Same split as
135 * `buildAnthropicProviderOptionsType`: mandatory or reasoning-without-sampling
136 * is adaptive-only, reasoning-plus-sampling keeps budget thinking too, and
137 * no reasoning param is budget thinking. `priority_tier` is on every current
138 * hand-written Claude entry that lists thinking flags.
139 */
140export function buildAnthropicSupportsFlags(input: {
141 supportedParameters?: Array<string>
142 reasoningMandatory?: boolean
143}): Array<string> {
144 const params = input.supportedParameters ?? []
145 const hasSampling = hasParam(params, ['temperature', 'top_p', 'top_k'])
146 const hasReasoning = hasParam(params, [
147 'include_reasoning',
148 'reasoning',
149 'reasoning_effort',
150 ])
151 const adaptiveOnly =
152 input.reasoningMandatory === true || (hasReasoning && !hasSampling)
153 const lines = [` extended_thinking: ${adaptiveOnly ? 'false' : 'true'},`]
154 if (adaptiveOnly || hasReasoning) {
155 lines.push(` adaptive_thinking: true,`)
156 }
157 lines.push(` priority_tier: true,`)
158 return lines
160​
161export function buildProviderSupportsBody(
162 input: ProviderSupportsInput,
163): string {
164 const params = input.supportedParameters ?? []
165 const inputList = quoteList(input.inputModalities)
166 const hasTools = hasParam(params, ['tools', 'tool_choice'])
167 const hasStructured = hasParam(params, [
168 'response_format',
169 'structured_outputs',
170 ])
171 const hasReasoning = hasParam(params, [
172 'include_reasoning',
173 'reasoning',
174 'reasoning_effort',
175 ])
176​
177 switch (input.provider) {
178 case 'openai': {
179 const features = ['streaming']
180 if (hasTools) features.push('function_calling')
181 if (hasStructured) features.push('structured_outputs')
182 return [
183 ` input: ${inputList},`,
184 ` output: ['text'],`,
185 ` endpoints: ['chat', 'chat-completions'],`,
186 ` features: ${quoteList(features)},`,
187 toolsLine(hasTools, OPENAI_SERVER_TOOLS),
188 ].join('\n')
189 }
190 case 'anthropic':
191 return [
192 ` input: ${inputList},`,
193 ...buildAnthropicSupportsFlags(input),
194 toolsLine(hasTools, ANTHROPIC_SERVER_TOOLS),
195 ].join('\n')
196 case 'gemini': {
197 const capabilities: Array<string> = []
198 if (hasTools) capabilities.push('function_calling')
199 if (hasStructured) capabilities.push('structured_output')
200 if (hasReasoning) capabilities.push('thinking')
201 const lines = [` input: ${inputList},`, ` output: ['text'],`]
202 if (capabilities.length > 0) {
203 lines.push(` capabilities: ${quoteList(capabilities)},`)
204 }
205 lines.push(toolsLine(hasTools, GEMINI_SERVER_TOOLS))
206 return lines.join('\n')
207 }
208 case 'grok': {
209 const capabilities: Array<string> = []
210 if (hasReasoning) capabilities.push('reasoning')
211 if (hasStructured) capabilities.push('structured_outputs')
212 if (hasTools) capabilities.push('tool_calling')
213 const lines = [` input: ${inputList},`, ` output: ['text'],`]
214 if (capabilities.length > 0) {
215 lines.push(` capabilities: ${quoteList(capabilities)},`)
216 }
217 lines.push(toolsLine(hasTools, GROK_SERVER_TOOLS))
218 return lines.join('\n')
219 }
220 }