@ai-sdk/xai 0.0.0-64aae7dd-20260114144918 → 0.0.0-98261322-20260122142521

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.
Files changed (47) hide show
  1. package/CHANGELOG.md +64 -5
  2. package/dist/index.js +1 -1
  3. package/dist/index.mjs +1 -1
  4. package/docs/01-xai.mdx +697 -0
  5. package/package.json +11 -6
  6. package/src/convert-to-xai-chat-messages.test.ts +243 -0
  7. package/src/convert-to-xai-chat-messages.ts +142 -0
  8. package/src/convert-xai-chat-usage.test.ts +240 -0
  9. package/src/convert-xai-chat-usage.ts +23 -0
  10. package/src/get-response-metadata.ts +19 -0
  11. package/src/index.ts +14 -0
  12. package/src/map-xai-finish-reason.ts +19 -0
  13. package/src/responses/__fixtures__/xai-code-execution-tool.1.json +68 -0
  14. package/src/responses/__fixtures__/xai-text-streaming.1.chunks.txt +698 -0
  15. package/src/responses/__fixtures__/xai-text-with-reasoning-streaming-store-false.1.chunks.txt +655 -0
  16. package/src/responses/__fixtures__/xai-text-with-reasoning-streaming.1.chunks.txt +679 -0
  17. package/src/responses/__fixtures__/xai-web-search-tool.1.chunks.txt +274 -0
  18. package/src/responses/__fixtures__/xai-web-search-tool.1.json +90 -0
  19. package/src/responses/__fixtures__/xai-x-search-tool.1.json +149 -0
  20. package/src/responses/__fixtures__/xai-x-search-tool.chunks.txt +1757 -0
  21. package/src/responses/__snapshots__/xai-responses-language-model.test.ts.snap +21929 -0
  22. package/src/responses/convert-to-xai-responses-input.test.ts +463 -0
  23. package/src/responses/convert-to-xai-responses-input.ts +206 -0
  24. package/src/responses/convert-xai-responses-usage.ts +24 -0
  25. package/src/responses/map-xai-responses-finish-reason.ts +20 -0
  26. package/src/responses/xai-responses-api.ts +393 -0
  27. package/src/responses/xai-responses-language-model.test.ts +1803 -0
  28. package/src/responses/xai-responses-language-model.ts +732 -0
  29. package/src/responses/xai-responses-options.ts +34 -0
  30. package/src/responses/xai-responses-prepare-tools.test.ts +497 -0
  31. package/src/responses/xai-responses-prepare-tools.ts +226 -0
  32. package/src/tool/code-execution.ts +17 -0
  33. package/src/tool/index.ts +15 -0
  34. package/src/tool/view-image.ts +20 -0
  35. package/src/tool/view-x-video.ts +18 -0
  36. package/src/tool/web-search.ts +56 -0
  37. package/src/tool/x-search.ts +63 -0
  38. package/src/version.ts +6 -0
  39. package/src/xai-chat-language-model.test.ts +1805 -0
  40. package/src/xai-chat-language-model.ts +681 -0
  41. package/src/xai-chat-options.ts +131 -0
  42. package/src/xai-chat-prompt.ts +44 -0
  43. package/src/xai-error.ts +19 -0
  44. package/src/xai-image-settings.ts +1 -0
  45. package/src/xai-prepare-tools.ts +95 -0
  46. package/src/xai-provider.test.ts +167 -0
  47. package/src/xai-provider.ts +162 -0
@@ -0,0 +1,393 @@
1
+ import { z } from 'zod/v4';
2
+
3
+ export type XaiResponsesInput = Array<XaiResponsesInputItem>;
4
+
5
+ export type XaiResponsesInputItem =
6
+ | XaiResponsesSystemMessage
7
+ | XaiResponsesUserMessage
8
+ | XaiResponsesAssistantMessage
9
+ | XaiResponsesFunctionCallOutput
10
+ | XaiResponsesReasoning
11
+ | XaiResponsesToolCall;
12
+
13
+ export type XaiResponsesSystemMessage = {
14
+ role: 'system' | 'developer';
15
+ content: string;
16
+ };
17
+
18
+ export type XaiResponsesUserMessageContentPart =
19
+ | { type: 'input_text'; text: string }
20
+ | { type: 'input_image'; image_url: string };
21
+
22
+ export type XaiResponsesUserMessage = {
23
+ role: 'user';
24
+ content: Array<XaiResponsesUserMessageContentPart>;
25
+ };
26
+
27
+ export type XaiResponsesAssistantMessage = {
28
+ role: 'assistant';
29
+ content: string;
30
+ id?: string;
31
+ };
32
+
33
+ export type XaiResponsesFunctionCallOutput = {
34
+ type: 'function_call_output';
35
+ call_id: string;
36
+ output: string;
37
+ };
38
+
39
+ export type XaiResponsesReasoning = {
40
+ type: 'reasoning';
41
+ id: string;
42
+ summary: Array<{
43
+ type: 'summary_text';
44
+ text: string;
45
+ }>;
46
+ status: string;
47
+ encrypted_content?: string | null;
48
+ };
49
+
50
+ export type XaiResponsesToolCall = {
51
+ type:
52
+ | 'function_call'
53
+ | 'web_search_call'
54
+ | 'x_search_call'
55
+ | 'code_interpreter_call'
56
+ | 'custom_tool_call';
57
+ id: string;
58
+ call_id?: string;
59
+ name?: string;
60
+ arguments?: string;
61
+ input?: string;
62
+ status: string;
63
+ action?: any;
64
+ };
65
+
66
+ export type XaiResponsesTool =
67
+ | {
68
+ type: 'web_search';
69
+ allowed_domains?: string[];
70
+ excluded_domains?: string[];
71
+ enable_image_understanding?: boolean;
72
+ }
73
+ | {
74
+ type: 'x_search';
75
+ allowed_x_handles?: string[];
76
+ excluded_x_handles?: string[];
77
+ from_date?: string;
78
+ to_date?: string;
79
+ enable_image_understanding?: boolean;
80
+ enable_video_understanding?: boolean;
81
+ }
82
+ | { type: 'code_interpreter' }
83
+ | { type: 'view_image' }
84
+ | { type: 'view_x_video' }
85
+ | { type: 'file_search' }
86
+ | { type: 'mcp' }
87
+ | {
88
+ type: 'function';
89
+ name: string;
90
+ description?: string;
91
+ parameters: unknown;
92
+ };
93
+
94
+ const annotationSchema = z.union([
95
+ z.object({
96
+ type: z.literal('url_citation'),
97
+ url: z.string(),
98
+ title: z.string().optional(),
99
+ }),
100
+ z.object({
101
+ type: z.string(),
102
+ }),
103
+ ]);
104
+
105
+ const messageContentPartSchema = z.object({
106
+ type: z.string(),
107
+ text: z.string().optional(),
108
+ logprobs: z.array(z.any()).optional(),
109
+ annotations: z.array(annotationSchema).optional(),
110
+ });
111
+
112
+ const reasoningSummaryPartSchema = z.object({
113
+ type: z.string(),
114
+ text: z.string(),
115
+ });
116
+
117
+ const toolCallSchema = z.object({
118
+ name: z.string().optional(),
119
+ arguments: z.string().optional(),
120
+ input: z.string().optional(),
121
+ call_id: z.string().optional(),
122
+ id: z.string(),
123
+ status: z.string(),
124
+ action: z.any().optional(),
125
+ });
126
+
127
+ const outputItemSchema = z.discriminatedUnion('type', [
128
+ z.object({
129
+ type: z.literal('web_search_call'),
130
+ ...toolCallSchema.shape,
131
+ }),
132
+ z.object({
133
+ type: z.literal('x_search_call'),
134
+ ...toolCallSchema.shape,
135
+ }),
136
+ z.object({
137
+ type: z.literal('code_interpreter_call'),
138
+ ...toolCallSchema.shape,
139
+ }),
140
+ z.object({
141
+ type: z.literal('code_execution_call'),
142
+ ...toolCallSchema.shape,
143
+ }),
144
+ z.object({
145
+ type: z.literal('view_image_call'),
146
+ ...toolCallSchema.shape,
147
+ }),
148
+ z.object({
149
+ type: z.literal('view_x_video_call'),
150
+ ...toolCallSchema.shape,
151
+ }),
152
+ z.object({
153
+ type: z.literal('custom_tool_call'),
154
+ ...toolCallSchema.shape,
155
+ }),
156
+ z.object({
157
+ type: z.literal('message'),
158
+ role: z.string(),
159
+ content: z.array(messageContentPartSchema),
160
+ id: z.string(),
161
+ status: z.string(),
162
+ }),
163
+ z.object({
164
+ type: z.literal('function_call'),
165
+ name: z.string(),
166
+ arguments: z.string(),
167
+ call_id: z.string(),
168
+ id: z.string(),
169
+ }),
170
+ z.object({
171
+ type: z.literal('reasoning'),
172
+ id: z.string(),
173
+ summary: z.array(reasoningSummaryPartSchema),
174
+ status: z.string(),
175
+ encrypted_content: z.string().nullish(),
176
+ }),
177
+ ]);
178
+
179
+ export const xaiResponsesUsageSchema = z.object({
180
+ input_tokens: z.number(),
181
+ output_tokens: z.number(),
182
+ total_tokens: z.number().optional(),
183
+ input_tokens_details: z
184
+ .object({
185
+ cached_tokens: z.number().optional(),
186
+ })
187
+ .optional(),
188
+ output_tokens_details: z
189
+ .object({
190
+ reasoning_tokens: z.number().optional(),
191
+ })
192
+ .optional(),
193
+ num_sources_used: z.number().optional(),
194
+ num_server_side_tools_used: z.number().optional(),
195
+ });
196
+
197
+ export const xaiResponsesResponseSchema = z.object({
198
+ id: z.string().nullish(),
199
+ created_at: z.number().nullish(),
200
+ model: z.string().nullish(),
201
+ object: z.literal('response'),
202
+ output: z.array(outputItemSchema),
203
+ usage: xaiResponsesUsageSchema,
204
+ status: z.string(),
205
+ });
206
+
207
+ export const xaiResponsesChunkSchema = z.union([
208
+ z.object({
209
+ type: z.literal('response.created'),
210
+ response: xaiResponsesResponseSchema.partial({ usage: true, status: true }),
211
+ }),
212
+ z.object({
213
+ type: z.literal('response.in_progress'),
214
+ response: xaiResponsesResponseSchema.partial({ usage: true, status: true }),
215
+ }),
216
+ z.object({
217
+ type: z.literal('response.output_item.added'),
218
+ item: outputItemSchema,
219
+ output_index: z.number(),
220
+ }),
221
+ z.object({
222
+ type: z.literal('response.output_item.done'),
223
+ item: outputItemSchema,
224
+ output_index: z.number(),
225
+ }),
226
+ z.object({
227
+ type: z.literal('response.content_part.added'),
228
+ item_id: z.string(),
229
+ output_index: z.number(),
230
+ content_index: z.number(),
231
+ part: messageContentPartSchema,
232
+ }),
233
+ z.object({
234
+ type: z.literal('response.content_part.done'),
235
+ item_id: z.string(),
236
+ output_index: z.number(),
237
+ content_index: z.number(),
238
+ part: messageContentPartSchema,
239
+ }),
240
+ z.object({
241
+ type: z.literal('response.output_text.delta'),
242
+ item_id: z.string(),
243
+ output_index: z.number(),
244
+ content_index: z.number(),
245
+ delta: z.string(),
246
+ logprobs: z.array(z.any()).optional(),
247
+ }),
248
+ z.object({
249
+ type: z.literal('response.output_text.done'),
250
+ item_id: z.string(),
251
+ output_index: z.number(),
252
+ content_index: z.number(),
253
+ text: z.string(),
254
+ logprobs: z.array(z.any()).optional(),
255
+ annotations: z.array(annotationSchema).optional(),
256
+ }),
257
+ z.object({
258
+ type: z.literal('response.output_text.annotation.added'),
259
+ item_id: z.string(),
260
+ output_index: z.number(),
261
+ content_index: z.number(),
262
+ annotation_index: z.number(),
263
+ annotation: annotationSchema,
264
+ }),
265
+ z.object({
266
+ type: z.literal('response.reasoning_summary_part.added'),
267
+ item_id: z.string(),
268
+ output_index: z.number(),
269
+ summary_index: z.number(),
270
+ part: reasoningSummaryPartSchema,
271
+ }),
272
+ z.object({
273
+ type: z.literal('response.reasoning_summary_part.done'),
274
+ item_id: z.string(),
275
+ output_index: z.number(),
276
+ summary_index: z.number(),
277
+ part: reasoningSummaryPartSchema,
278
+ }),
279
+ z.object({
280
+ type: z.literal('response.reasoning_summary_text.delta'),
281
+ item_id: z.string(),
282
+ output_index: z.number(),
283
+ summary_index: z.number(),
284
+ delta: z.string(),
285
+ }),
286
+ z.object({
287
+ type: z.literal('response.reasoning_summary_text.done'),
288
+ item_id: z.string(),
289
+ output_index: z.number(),
290
+ summary_index: z.number(),
291
+ text: z.string(),
292
+ }),
293
+ z.object({
294
+ type: z.literal('response.web_search_call.in_progress'),
295
+ item_id: z.string(),
296
+ output_index: z.number(),
297
+ }),
298
+ z.object({
299
+ type: z.literal('response.web_search_call.searching'),
300
+ item_id: z.string(),
301
+ output_index: z.number(),
302
+ }),
303
+ z.object({
304
+ type: z.literal('response.web_search_call.completed'),
305
+ item_id: z.string(),
306
+ output_index: z.number(),
307
+ }),
308
+ z.object({
309
+ type: z.literal('response.x_search_call.in_progress'),
310
+ item_id: z.string(),
311
+ output_index: z.number(),
312
+ }),
313
+ z.object({
314
+ type: z.literal('response.x_search_call.searching'),
315
+ item_id: z.string(),
316
+ output_index: z.number(),
317
+ }),
318
+ z.object({
319
+ type: z.literal('response.x_search_call.completed'),
320
+ item_id: z.string(),
321
+ output_index: z.number(),
322
+ }),
323
+ z.object({
324
+ type: z.literal('response.custom_tool_call_input.done'),
325
+ item_id: z.string(),
326
+ output_index: z.number(),
327
+ }),
328
+ z.object({
329
+ type: z.literal('response.custom_tool_call_input.delta'),
330
+ item_id: z.string(),
331
+ output_index: z.number(),
332
+ }),
333
+ z.object({
334
+ type: z.literal('response.code_execution_call.in_progress'),
335
+ item_id: z.string(),
336
+ output_index: z.number(),
337
+ }),
338
+ z.object({
339
+ type: z.literal('response.code_execution_call.executing'),
340
+ item_id: z.string(),
341
+ output_index: z.number(),
342
+ }),
343
+ z.object({
344
+ type: z.literal('response.code_execution_call.completed'),
345
+ item_id: z.string(),
346
+ output_index: z.number(),
347
+ }),
348
+ z.object({
349
+ type: z.literal('response.code_interpreter_call.in_progress'),
350
+ item_id: z.string(),
351
+ output_index: z.number(),
352
+ }),
353
+ z.object({
354
+ type: z.literal('response.code_interpreter_call.executing'),
355
+ item_id: z.string(),
356
+ output_index: z.number(),
357
+ }),
358
+ z.object({
359
+ type: z.literal('response.code_interpreter_call.interpreting'),
360
+ item_id: z.string(),
361
+ output_index: z.number(),
362
+ }),
363
+ z.object({
364
+ type: z.literal('response.code_interpreter_call.completed'),
365
+ item_id: z.string(),
366
+ output_index: z.number(),
367
+ }),
368
+ // Code interpreter code streaming events
369
+ z.object({
370
+ type: z.literal('response.code_interpreter_call_code.delta'),
371
+ item_id: z.string(),
372
+ output_index: z.number(),
373
+ delta: z.string(),
374
+ }),
375
+ z.object({
376
+ type: z.literal('response.code_interpreter_call_code.done'),
377
+ item_id: z.string(),
378
+ output_index: z.number(),
379
+ code: z.string(),
380
+ }),
381
+ z.object({
382
+ type: z.literal('response.done'),
383
+ response: xaiResponsesResponseSchema,
384
+ }),
385
+ z.object({
386
+ type: z.literal('response.completed'),
387
+ response: xaiResponsesResponseSchema,
388
+ }),
389
+ ]);
390
+
391
+ export type XaiResponsesResponse = z.infer<typeof xaiResponsesResponseSchema>;
392
+ export type XaiResponsesChunk = z.infer<typeof xaiResponsesChunkSchema>;
393
+ export type XaiResponsesUsage = z.infer<typeof xaiResponsesUsageSchema>;