Open History to see the last commit
1import { brandProviderTool } from '@tanstack/ai'
2import { assertUniqueToolNames } from '@tanstack/ai/adapter-internals'
3import { convertFunctionToolToResponsesFormat } from '@tanstack/openai-base'
4import type { ProviderTool, Tool } from '@tanstack/ai'
5import type { ResponsesFunctionTool } from '@tanstack/openai-base'
6import type { GrokProviderToolKind } from '../model-meta'
7
8export type FunctionTool = ResponsesFunctionTool
9
10export { convertFunctionToolToResponsesFormat as convertFunctionToolToAdapterFormat }
11
12export type GrokProviderTool<TKind extends GrokProviderToolKind> = ProviderTool<
13 'grok',
14 TKind
15>
16
17type GrokToolKindMarker<TKind extends GrokProviderToolKind> = `grok.${TKind}`
18
19export interface GrokWebSearchToolConfig {
20 type: 'web_search'
21 filters?: {
22 allowed_domains?: Array<string>
23 excluded_domains?: Array<string>
24 }
25 enable_image_understanding?: boolean
26 enable_image_search?: boolean
27}
28
29export interface GrokXSearchToolConfig {
30 type: 'x_search'
31 allowed_x_handles?: Array<string>
32 excluded_x_handles?: Array<string>
33 from_date?: string
34 to_date?: string
35 enable_image_understanding?: boolean
36 enable_video_understanding?: boolean
37}
38
39export interface GrokFileSearchToolConfig {
40 type: 'file_search'
41 vector_store_ids: Array<string>
42 max_num_results?: number
43}
44
45export interface GrokMCPToolConfig {
46 type: 'mcp'
47 server_label: string
48 server_url: string
49 allowed_tools?: Array<string>
50 server_description?: string
51 authorization?: string
52 headers?: Record<string, string>
53}
54
55export type GrokServerTool =
56 | GrokWebSearchToolConfig
57 | GrokXSearchToolConfig
58 | GrokFileSearchToolConfig
59 | GrokMCPToolConfig
60
61type GrokProviderToolMetadata<TKind extends GrokProviderToolKind> = Extract<
62 GrokServerTool,
63 { type: TKind }
64> & {
65 __kind: GrokToolKindMarker<TKind>
66}
67
68export type GrokResponsesTool = GrokServerTool | ResponsesFunctionTool
69
70function providerTool<TKind extends GrokProviderToolKind>(
71 kind: TKind,
72 description: string,
73 metadata: Extract<GrokServerTool, { type: TKind }>,
74): GrokProviderTool<TKind> {
75 return brandProviderTool<GrokProviderTool<TKind>>({
76 name: kind,
77 description,
78 metadata: {
79 __kind: `grok.${kind}`,
80 ...metadata,
81 },
82 })
83}
84
85export function grokWebSearchTool(
86 config: Omit<GrokWebSearchToolConfig, 'type'> = {},
87): GrokProviderTool<'web_search'> {
88 if (
89 config.filters?.allowed_domains !== undefined &&
90 config.filters.excluded_domains !== undefined
91 ) {
92 throw new Error(
93 'allowed_domains and excluded_domains cannot both be provided.',
94 )
95 }
96 if (
97 config.filters?.allowed_domains !== undefined &&
98 config.filters.allowed_domains.length > 5
99 ) {
100 throw new Error('allowed_domains supports at most 5 domains.')
101 }
102 if (
103 config.filters?.excluded_domains !== undefined &&
104 config.filters.excluded_domains.length > 5
105 ) {
106 throw new Error('excluded_domains supports at most 5 domains.')
107 }
108 return providerTool('web_search', 'Search the web', {
109 type: 'web_search',
110 ...config,
111 })
112}
113
114export function grokXSearchTool(
115 config: Omit<GrokXSearchToolConfig, 'type'> = {},
116): GrokProviderTool<'x_search'> {
117 if (
118 config.allowed_x_handles !== undefined &&
119 config.excluded_x_handles !== undefined
120 ) {
121 throw new Error(
122 'allowed_x_handles and excluded_x_handles cannot both be provided.',
123 )
124 }
125 if (
126 config.allowed_x_handles !== undefined &&
127 config.allowed_x_handles.length > 20
128 ) {
129 throw new Error('allowed_x_handles supports at most 20 handles.')
130 }
131 if (
132 config.excluded_x_handles !== undefined &&
133 config.excluded_x_handles.length > 20
134 ) {
135 throw new Error('excluded_x_handles supports at most 20 handles.')
136 }
137 return providerTool('x_search', 'Search X posts', {
138 type: 'x_search',
139 ...config,
140 })
141}
142
143export function grokFileSearchTool(
144 config: Omit<GrokFileSearchToolConfig, 'type'>,
145): GrokProviderTool<'file_search'> {
146 if (config.vector_store_ids.length === 0) {
147 throw new Error('vector_store_ids must contain at least one collection id.')
148 }
149 if (config.max_num_results !== undefined) {
150 if (config.max_num_results < 1 || config.max_num_results > 50) {
151 throw new Error('max_num_results must be between 1 and 50.')
152 }
153 }
154 return providerTool('file_search', 'Search xAI file collections', {
155 type: 'file_search',
156 ...config,
157 })
158}
159
160export function grokMCPTool(
161 config: Omit<GrokMCPToolConfig, 'type'>,
162): GrokProviderTool<'mcp'> {
163 if (!config.server_url) {
164 throw new Error('server_url must be provided.')
165 }
166 return providerTool('mcp', config.server_description || 'Remote MCP server', {
167 type: 'mcp',
168 ...config,
169 })
170}
171
172function getGrokProviderToolKind(tool: Tool): GrokProviderToolKind | undefined {
173 const kind = (tool.metadata as { __kind?: unknown } | undefined)?.__kind
174 switch (kind) {
175 case 'grok.web_search':
176 return 'web_search'
177 case 'grok.x_search':
178 return 'x_search'
179 case 'grok.file_search':
180 return 'file_search'
181 case 'grok.mcp':
182 return 'mcp'
183 default:
184 return undefined
185 }
186}
187
188function convertGrokProviderToolToAdapterFormat(
189 tool: Tool,
190 kind: GrokProviderToolKind,
191): GrokServerTool {
192 const metadata = tool.metadata as GrokProviderToolMetadata<typeof kind>
193 if (metadata.type !== kind) {
194 throw new Error(
195 `convertGrokProviderToolToAdapterFormat: tool "${tool.name}" has mismatched Grok tool metadata.`,
196 )
197 }
198 const { __kind: _kind, ...toolConfig } = metadata
199 void _kind
200 return toolConfig
201}
202
203export function convertToolsToProviderFormat(
204 tools: Array<Tool>,
205): Array<GrokResponsesTool> {
206 assertUniqueToolNames(tools)
207 return tools.map((tool) => {
208 const grokProviderToolKind = getGrokProviderToolKind(tool)
209 if (grokProviderToolKind) {
210 return convertGrokProviderToolToAdapterFormat(tool, grokProviderToolKind)
211 }
212 return convertFunctionToolToResponsesFormat(tool)
213 })
214}