@ai-sdk/xai 0.0.0-1c33ba03-20260114162300 → 0.0.0-4115c213-20260122152721

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,34 @@
1
+ import { z } from 'zod/v4';
2
+
3
+ export type XaiResponsesModelId =
4
+ | 'grok-4-1'
5
+ | 'grok-4-1-fast-reasoning'
6
+ | 'grok-4-1-fast-non-reasoning'
7
+ | 'grok-4'
8
+ | 'grok-4-fast'
9
+ | 'grok-4-fast-non-reasoning'
10
+ | (string & {});
11
+
12
+ /**
13
+ * @see https://docs.x.ai/docs/api-reference#create-new-response
14
+ */
15
+ export const xaiResponsesProviderOptions = z.object({
16
+ /**
17
+ * Constrains how hard a reasoning model thinks before responding.
18
+ * Possible values are `low` (uses fewer reasoning tokens), `medium` and `high` (uses more reasoning tokens).
19
+ */
20
+ reasoningEffort: z.enum(['low', 'medium', 'high']).optional(),
21
+ /**
22
+ * Whether to store the input message(s) and model response for later retrieval.
23
+ * @default true
24
+ */
25
+ store: z.boolean().optional(),
26
+ /**
27
+ * The ID of the previous response from the model.
28
+ */
29
+ previousResponseId: z.string().optional(),
30
+ });
31
+
32
+ export type XaiResponsesProviderOptions = z.infer<
33
+ typeof xaiResponsesProviderOptions
34
+ >;
@@ -0,0 +1,226 @@
1
+ import {
2
+ LanguageModelV3CallOptions,
3
+ SharedV3Warning,
4
+ UnsupportedFunctionalityError,
5
+ } from '@ai-sdk/provider';
6
+ import { validateTypes } from '@ai-sdk/provider-utils';
7
+ import { webSearchArgsSchema } from '../tool/web-search';
8
+ import { xSearchArgsSchema } from '../tool/x-search';
9
+ import { XaiResponsesTool } from './xai-responses-api';
10
+
11
+ type XaiResponsesToolChoice =
12
+ | 'auto'
13
+ | 'none'
14
+ | 'required'
15
+ | { type: 'web_search' }
16
+ | { type: 'x_search' }
17
+ | { type: 'code_interpreter' }
18
+ | { type: 'view_image' }
19
+ | { type: 'view_x_video' }
20
+ | { type: 'file_search' }
21
+ | { type: 'mcp' }
22
+ | { type: 'function'; name: string };
23
+
24
+ export async function prepareResponsesTools({
25
+ tools,
26
+ toolChoice,
27
+ }: {
28
+ tools: LanguageModelV3CallOptions['tools'];
29
+ toolChoice?: LanguageModelV3CallOptions['toolChoice'];
30
+ }): Promise<{
31
+ tools: Array<XaiResponsesTool> | undefined;
32
+ toolChoice: XaiResponsesToolChoice | undefined;
33
+ toolWarnings: SharedV3Warning[];
34
+ }> {
35
+ const normalizedTools = tools?.length ? tools : undefined;
36
+
37
+ const toolWarnings: SharedV3Warning[] = [];
38
+
39
+ if (normalizedTools == null) {
40
+ return { tools: undefined, toolChoice: undefined, toolWarnings };
41
+ }
42
+
43
+ const xaiTools: Array<XaiResponsesTool> = [];
44
+ const toolByName = new Map<string, (typeof normalizedTools)[number]>();
45
+
46
+ for (const tool of normalizedTools) {
47
+ toolByName.set(tool.name, tool);
48
+
49
+ if (tool.type === 'provider') {
50
+ switch (tool.id) {
51
+ case 'xai.web_search': {
52
+ const args = await validateTypes({
53
+ value: tool.args,
54
+ schema: webSearchArgsSchema,
55
+ });
56
+
57
+ xaiTools.push({
58
+ type: 'web_search',
59
+ allowed_domains: args.allowedDomains,
60
+ excluded_domains: args.excludedDomains,
61
+ enable_image_understanding: args.enableImageUnderstanding,
62
+ });
63
+ break;
64
+ }
65
+
66
+ case 'xai.x_search': {
67
+ const args = await validateTypes({
68
+ value: tool.args,
69
+ schema: xSearchArgsSchema,
70
+ });
71
+
72
+ xaiTools.push({
73
+ type: 'x_search',
74
+ allowed_x_handles: args.allowedXHandles,
75
+ excluded_x_handles: args.excludedXHandles,
76
+ from_date: args.fromDate,
77
+ to_date: args.toDate,
78
+ enable_image_understanding: args.enableImageUnderstanding,
79
+ enable_video_understanding: args.enableVideoUnderstanding,
80
+ });
81
+ break;
82
+ }
83
+
84
+ case 'xai.code_execution': {
85
+ xaiTools.push({
86
+ type: 'code_interpreter',
87
+ });
88
+ break;
89
+ }
90
+
91
+ case 'xai.view_image': {
92
+ xaiTools.push({
93
+ type: 'view_image',
94
+ });
95
+ break;
96
+ }
97
+
98
+ case 'xai.view_x_video': {
99
+ xaiTools.push({
100
+ type: 'view_x_video',
101
+ });
102
+ break;
103
+ }
104
+
105
+ case 'xai.file_search': {
106
+ xaiTools.push({
107
+ type: 'file_search',
108
+ });
109
+ break;
110
+ }
111
+
112
+ case 'xai.mcp': {
113
+ xaiTools.push({
114
+ type: 'mcp',
115
+ });
116
+ break;
117
+ }
118
+
119
+ default: {
120
+ toolWarnings.push({
121
+ type: 'unsupported',
122
+ feature: `provider-defined tool ${tool.name}`,
123
+ });
124
+ break;
125
+ }
126
+ }
127
+ } else {
128
+ xaiTools.push({
129
+ type: 'function',
130
+ name: tool.name,
131
+ description: tool.description,
132
+ parameters: tool.inputSchema,
133
+ });
134
+ }
135
+ }
136
+
137
+ if (toolChoice == null) {
138
+ return { tools: xaiTools, toolChoice: undefined, toolWarnings };
139
+ }
140
+
141
+ const type = toolChoice.type;
142
+
143
+ switch (type) {
144
+ case 'auto':
145
+ case 'none':
146
+ return { tools: xaiTools, toolChoice: type, toolWarnings };
147
+ case 'required':
148
+ return { tools: xaiTools, toolChoice: 'required', toolWarnings };
149
+ case 'tool': {
150
+ const selectedTool = toolByName.get(toolChoice.toolName);
151
+
152
+ if (selectedTool == null) {
153
+ return {
154
+ tools: xaiTools,
155
+ toolChoice: undefined,
156
+ toolWarnings,
157
+ };
158
+ }
159
+
160
+ if (selectedTool.type === 'provider') {
161
+ switch (selectedTool.id) {
162
+ case 'xai.web_search':
163
+ return {
164
+ tools: xaiTools,
165
+ toolChoice: { type: 'web_search' },
166
+ toolWarnings,
167
+ };
168
+ case 'xai.x_search':
169
+ return {
170
+ tools: xaiTools,
171
+ toolChoice: { type: 'x_search' },
172
+ toolWarnings,
173
+ };
174
+ case 'xai.code_execution':
175
+ return {
176
+ tools: xaiTools,
177
+ toolChoice: { type: 'code_interpreter' },
178
+ toolWarnings,
179
+ };
180
+ case 'xai.view_image':
181
+ return {
182
+ tools: xaiTools,
183
+ toolChoice: { type: 'view_image' },
184
+ toolWarnings,
185
+ };
186
+ case 'xai.view_x_video':
187
+ return {
188
+ tools: xaiTools,
189
+ toolChoice: { type: 'view_x_video' },
190
+ toolWarnings,
191
+ };
192
+ case 'xai.file_search':
193
+ return {
194
+ tools: xaiTools,
195
+ toolChoice: { type: 'file_search' },
196
+ toolWarnings,
197
+ };
198
+ case 'xai.mcp':
199
+ return {
200
+ tools: xaiTools,
201
+ toolChoice: { type: 'mcp' },
202
+ toolWarnings,
203
+ };
204
+ default:
205
+ toolWarnings.push({
206
+ type: 'unsupported',
207
+ feature: `provider-defined tool ${selectedTool.name}`,
208
+ });
209
+ return { tools: xaiTools, toolChoice: undefined, toolWarnings };
210
+ }
211
+ }
212
+
213
+ return {
214
+ tools: xaiTools,
215
+ toolChoice: { type: 'function', name: selectedTool.name },
216
+ toolWarnings,
217
+ };
218
+ }
219
+ default: {
220
+ const _exhaustiveCheck: never = type;
221
+ throw new UnsupportedFunctionalityError({
222
+ functionality: `tool choice type: ${_exhaustiveCheck}`,
223
+ });
224
+ }
225
+ }
226
+ }
@@ -0,0 +1,17 @@
1
+ import { createProviderToolFactoryWithOutputSchema } from '@ai-sdk/provider-utils';
2
+ import { z } from 'zod/v4';
3
+
4
+ const codeExecutionOutputSchema = z.object({
5
+ output: z.string().describe('the output of the code execution'),
6
+ error: z.string().optional().describe('any error that occurred'),
7
+ });
8
+
9
+ const codeExecutionToolFactory = createProviderToolFactoryWithOutputSchema({
10
+ id: 'xai.code_execution',
11
+ inputSchema: z.object({}).describe('no input parameters'),
12
+ outputSchema: codeExecutionOutputSchema,
13
+ });
14
+
15
+ export const codeExecution = (
16
+ args: Parameters<typeof codeExecutionToolFactory>[0] = {},
17
+ ) => codeExecutionToolFactory(args);
@@ -0,0 +1,15 @@
1
+ import { codeExecution } from './code-execution';
2
+ import { viewImage } from './view-image';
3
+ import { viewXVideo } from './view-x-video';
4
+ import { webSearch } from './web-search';
5
+ import { xSearch } from './x-search';
6
+
7
+ export { codeExecution, viewImage, viewXVideo, webSearch, xSearch };
8
+
9
+ export const xaiTools = {
10
+ codeExecution,
11
+ viewImage,
12
+ viewXVideo,
13
+ webSearch,
14
+ xSearch,
15
+ };
@@ -0,0 +1,20 @@
1
+ import { createProviderToolFactoryWithOutputSchema } from '@ai-sdk/provider-utils';
2
+ import { z } from 'zod/v4';
3
+
4
+ const viewImageOutputSchema = z.object({
5
+ description: z.string().describe('description of the image'),
6
+ objects: z
7
+ .array(z.string())
8
+ .optional()
9
+ .describe('objects detected in the image'),
10
+ });
11
+
12
+ const viewImageToolFactory = createProviderToolFactoryWithOutputSchema({
13
+ id: 'xai.view_image',
14
+ inputSchema: z.object({}).describe('no input parameters'),
15
+ outputSchema: viewImageOutputSchema,
16
+ });
17
+
18
+ export const viewImage = (
19
+ args: Parameters<typeof viewImageToolFactory>[0] = {},
20
+ ) => viewImageToolFactory(args);
@@ -0,0 +1,18 @@
1
+ import { createProviderToolFactoryWithOutputSchema } from '@ai-sdk/provider-utils';
2
+ import { z } from 'zod/v4';
3
+
4
+ const viewXVideoOutputSchema = z.object({
5
+ transcript: z.string().optional().describe('transcript of the video'),
6
+ description: z.string().describe('description of the video content'),
7
+ duration: z.number().optional().describe('duration in seconds'),
8
+ });
9
+
10
+ const viewXVideoToolFactory = createProviderToolFactoryWithOutputSchema({
11
+ id: 'xai.view_x_video',
12
+ inputSchema: z.object({}).describe('no input parameters'),
13
+ outputSchema: viewXVideoOutputSchema,
14
+ });
15
+
16
+ export const viewXVideo = (
17
+ args: Parameters<typeof viewXVideoToolFactory>[0] = {},
18
+ ) => viewXVideoToolFactory(args);
@@ -0,0 +1,56 @@
1
+ import {
2
+ createProviderToolFactoryWithOutputSchema,
3
+ lazySchema,
4
+ zodSchema,
5
+ } from '@ai-sdk/provider-utils';
6
+ import { z } from 'zod/v4';
7
+
8
+ export const webSearchArgsSchema = lazySchema(() =>
9
+ zodSchema(
10
+ z.object({
11
+ allowedDomains: z.array(z.string()).max(5).optional(),
12
+ excludedDomains: z.array(z.string()).max(5).optional(),
13
+ enableImageUnderstanding: z.boolean().optional(),
14
+ }),
15
+ ),
16
+ );
17
+
18
+ const webSearchOutputSchema = lazySchema(() =>
19
+ zodSchema(
20
+ z.object({
21
+ query: z.string(),
22
+ sources: z.array(
23
+ z.object({
24
+ title: z.string(),
25
+ url: z.string(),
26
+ snippet: z.string(),
27
+ }),
28
+ ),
29
+ }),
30
+ ),
31
+ );
32
+
33
+ const webSearchToolFactory = createProviderToolFactoryWithOutputSchema<
34
+ Record<string, never>,
35
+ {
36
+ query: string;
37
+ sources: Array<{
38
+ title: string;
39
+ url: string;
40
+ snippet: string;
41
+ }>;
42
+ },
43
+ {
44
+ allowedDomains?: string[];
45
+ excludedDomains?: string[];
46
+ enableImageUnderstanding?: boolean;
47
+ }
48
+ >({
49
+ id: 'xai.web_search',
50
+ inputSchema: lazySchema(() => zodSchema(z.object({}))),
51
+ outputSchema: webSearchOutputSchema,
52
+ });
53
+
54
+ export const webSearch = (
55
+ args: Parameters<typeof webSearchToolFactory>[0] = {},
56
+ ) => webSearchToolFactory(args);
@@ -0,0 +1,63 @@
1
+ import {
2
+ createProviderToolFactoryWithOutputSchema,
3
+ lazySchema,
4
+ zodSchema,
5
+ } from '@ai-sdk/provider-utils';
6
+ import { z } from 'zod/v4';
7
+
8
+ export const xSearchArgsSchema = lazySchema(() =>
9
+ zodSchema(
10
+ z.object({
11
+ allowedXHandles: z.array(z.string()).max(10).optional(),
12
+ excludedXHandles: z.array(z.string()).max(10).optional(),
13
+ fromDate: z.string().optional(),
14
+ toDate: z.string().optional(),
15
+ enableImageUnderstanding: z.boolean().optional(),
16
+ enableVideoUnderstanding: z.boolean().optional(),
17
+ }),
18
+ ),
19
+ );
20
+
21
+ const xSearchOutputSchema = lazySchema(() =>
22
+ zodSchema(
23
+ z.object({
24
+ query: z.string(),
25
+ posts: z.array(
26
+ z.object({
27
+ author: z.string(),
28
+ text: z.string(),
29
+ url: z.string(),
30
+ likes: z.number(),
31
+ }),
32
+ ),
33
+ }),
34
+ ),
35
+ );
36
+
37
+ const xSearchToolFactory = createProviderToolFactoryWithOutputSchema<
38
+ Record<string, never>,
39
+ {
40
+ query: string;
41
+ posts: Array<{
42
+ author: string;
43
+ text: string;
44
+ url: string;
45
+ likes: number;
46
+ }>;
47
+ },
48
+ {
49
+ allowedXHandles?: string[];
50
+ excludedXHandles?: string[];
51
+ fromDate?: string;
52
+ toDate?: string;
53
+ enableImageUnderstanding?: boolean;
54
+ enableVideoUnderstanding?: boolean;
55
+ }
56
+ >({
57
+ id: 'xai.x_search',
58
+ inputSchema: lazySchema(() => zodSchema(z.object({}))),
59
+ outputSchema: xSearchOutputSchema,
60
+ });
61
+
62
+ export const xSearch = (args: Parameters<typeof xSearchToolFactory>[0] = {}) =>
63
+ xSearchToolFactory(args);
package/src/version.ts ADDED
@@ -0,0 +1,6 @@
1
+ // Version string of this package injected at build time.
2
+ declare const __PACKAGE_VERSION__: string | undefined;
3
+ export const VERSION: string =
4
+ typeof __PACKAGE_VERSION__ !== 'undefined'
5
+ ? __PACKAGE_VERSION__
6
+ : '0.0.0-test';