@ai-sdk/xai 3.0.127 → 3.0.129

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/xai",
3
- "version": "3.0.127",
3
+ "version": "3.0.129",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -29,9 +29,9 @@
29
29
  }
30
30
  },
31
31
  "dependencies": {
32
- "@ai-sdk/openai-compatible": "2.0.72",
32
+ "@ai-sdk/openai-compatible": "2.0.73",
33
33
  "@ai-sdk/provider": "3.0.15",
34
- "@ai-sdk/provider-utils": "4.0.48"
34
+ "@ai-sdk/provider-utils": "4.0.49"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/node": "20.17.24",
@@ -163,6 +163,31 @@ const toolCallSchema = z.object({
163
163
  action: z.any().optional(),
164
164
  });
165
165
 
166
+ export const webSearchWireSourceSchema = z.object({
167
+ type: z.literal('url'),
168
+ url: z.string(),
169
+ });
170
+
171
+ export const webSearchWireActionSchema = z.discriminatedUnion('type', [
172
+ z.object({
173
+ type: z.literal('search'),
174
+ query: z.string().nullish(),
175
+ queries: z.array(z.string()).nullish(),
176
+ sources: z.array(z.unknown()).nullish(),
177
+ }),
178
+ z.object({
179
+ type: z.literal('open_page'),
180
+ url: z.string().nullish(),
181
+ sources: z.array(z.unknown()).nullish(),
182
+ }),
183
+ z.object({
184
+ type: z.literal('find_in_page'),
185
+ url: z.string().nullish(),
186
+ pattern: z.string().nullish(),
187
+ sources: z.array(z.unknown()).nullish(),
188
+ }),
189
+ ]);
190
+
166
191
  const mcpCallSchema = z.object({
167
192
  name: z.string().optional(),
168
193
  arguments: z.string().optional(),
@@ -16,15 +16,19 @@ import {
16
16
  parseProviderOptions,
17
17
  postJsonToApi,
18
18
  type FetchFunction,
19
+ type InferSchema,
19
20
  type ParseResult,
20
21
  } from '@ai-sdk/provider-utils';
21
22
  import type { z } from 'zod/v4';
22
23
  import { getResponseMetadata } from '../get-response-metadata';
24
+ import type { webSearchOutputSchema } from '../tool/web-search';
23
25
  import { xaiFailedResponseHandler } from '../xai-error';
24
26
  import { convertToXaiResponsesInput } from './convert-to-xai-responses-input';
25
27
  import { convertXaiResponsesUsage } from './convert-xai-responses-usage';
26
28
  import { mapXaiResponsesFinishReason } from './map-xai-responses-finish-reason';
27
29
  import {
30
+ webSearchWireActionSchema,
31
+ webSearchWireSourceSchema,
28
32
  xaiResponsesChunkSchema,
29
33
  xaiResponsesResponseSchema,
30
34
  type XaiResponsesIncludeOptions,
@@ -342,6 +346,15 @@ export class XaiResponsesLanguageModel implements LanguageModelV3 {
342
346
  providerExecuted: true,
343
347
  });
344
348
 
349
+ if (part.type === 'web_search_call') {
350
+ content.push({
351
+ type: 'tool-result',
352
+ toolCallId: part.id,
353
+ toolName,
354
+ result: mapWebSearchAction(part.action),
355
+ });
356
+ }
357
+
345
358
  continue;
346
359
  }
347
360
 
@@ -932,7 +945,10 @@ export class XaiResponsesLanguageModel implements LanguageModelV3 {
932
945
  type: 'tool-result',
933
946
  toolCallId: part.id,
934
947
  toolName,
935
- result: {},
948
+ result:
949
+ part.type === 'web_search_call'
950
+ ? mapWebSearchAction(part.action)
951
+ : {},
936
952
  });
937
953
  }
938
954
 
@@ -1045,3 +1061,36 @@ export class XaiResponsesLanguageModel implements LanguageModelV3 {
1045
1061
  };
1046
1062
  }
1047
1063
  }
1064
+
1065
+ function mapWebSearchAction(
1066
+ action: unknown,
1067
+ ): InferSchema<typeof webSearchOutputSchema> {
1068
+ const parsed = webSearchWireActionSchema.safeParse(action);
1069
+ if (!parsed.success) return {};
1070
+
1071
+ const a = parsed.data;
1072
+ const sources = a.sources?.flatMap(s => {
1073
+ const source = webSearchWireSourceSchema.safeParse(s);
1074
+ return source.success ? [source.data] : [];
1075
+ });
1076
+ const sourcesExtra = sources != null && sources.length > 0 ? { sources } : {};
1077
+
1078
+ switch (a.type) {
1079
+ case 'search':
1080
+ return {
1081
+ action: {
1082
+ type: 'search',
1083
+ ...(a.query != null && { query: a.query }),
1084
+ ...(a.queries != null && { queries: a.queries }),
1085
+ },
1086
+ ...sourcesExtra,
1087
+ };
1088
+ case 'open_page':
1089
+ return { action: { type: 'openPage', url: a.url }, ...sourcesExtra };
1090
+ case 'find_in_page':
1091
+ return {
1092
+ action: { type: 'findInPage', url: a.url, pattern: a.pattern },
1093
+ ...sourcesExtra,
1094
+ };
1095
+ }
1096
+ }
@@ -16,17 +16,30 @@ export const webSearchArgsSchema = lazySchema(() =>
16
16
  ),
17
17
  );
18
18
 
19
- const webSearchOutputSchema = lazySchema(() =>
19
+ export const webSearchOutputSchema = lazySchema(() =>
20
20
  zodSchema(
21
21
  z.object({
22
- query: z.string(),
23
- sources: z.array(
24
- z.object({
25
- title: z.string(),
26
- url: z.string(),
27
- snippet: z.string(),
28
- }),
29
- ),
22
+ action: z
23
+ .discriminatedUnion('type', [
24
+ z.object({
25
+ type: z.literal('search'),
26
+ query: z.string().optional(),
27
+ queries: z.array(z.string()).optional(),
28
+ }),
29
+ z.object({
30
+ type: z.literal('openPage'),
31
+ url: z.string().nullish(),
32
+ }),
33
+ z.object({
34
+ type: z.literal('findInPage'),
35
+ url: z.string().nullish(),
36
+ pattern: z.string().nullish(),
37
+ }),
38
+ ])
39
+ .optional(),
40
+ sources: z
41
+ .array(z.object({ type: z.literal('url'), url: z.string() }))
42
+ .optional(),
30
43
  }),
31
44
  ),
32
45
  );
@@ -34,12 +47,22 @@ const webSearchOutputSchema = lazySchema(() =>
34
47
  const webSearchToolFactory = createProviderToolFactoryWithOutputSchema<
35
48
  {},
36
49
  {
37
- query: string;
38
- sources: Array<{
39
- title: string;
40
- url: string;
41
- snippet: string;
42
- }>;
50
+ action?:
51
+ | {
52
+ type: 'search';
53
+ query?: string;
54
+ queries?: string[];
55
+ }
56
+ | {
57
+ type: 'openPage';
58
+ url?: string | null;
59
+ }
60
+ | {
61
+ type: 'findInPage';
62
+ url?: string | null;
63
+ pattern?: string | null;
64
+ };
65
+ sources?: Array<{ type: 'url'; url: string }>;
43
66
  },
44
67
  {
45
68
  allowedDomains?: string[];