@ai-sdk/xai 2.0.0-alpha.8 → 2.0.0-alpha.9

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/dist/index.js CHANGED
@@ -27,10 +27,11 @@ module.exports = __toCommonJS(src_exports);
27
27
 
28
28
  // src/xai-provider.ts
29
29
  var import_openai_compatible = require("@ai-sdk/openai-compatible");
30
- var import_provider = require("@ai-sdk/provider");
31
- var import_provider_utils = require("@ai-sdk/provider-utils");
30
+ var import_provider3 = require("@ai-sdk/provider");
31
+ var import_provider_utils4 = require("@ai-sdk/provider-utils");
32
32
 
33
33
  // src/xai-chat-options.ts
34
+ var import_zod = require("zod");
34
35
  function supportsStructuredOutputs(modelId) {
35
36
  return [
36
37
  "grok-3",
@@ -49,26 +50,671 @@ function supportsStructuredOutputs(modelId) {
49
50
  "grok-2-vision-1212"
50
51
  ].includes(modelId);
51
52
  }
53
+ var webSourceSchema = import_zod.z.object({
54
+ type: import_zod.z.literal("web"),
55
+ country: import_zod.z.string().length(2).optional(),
56
+ excludedWebsites: import_zod.z.array(import_zod.z.string()).max(5).optional(),
57
+ allowedWebsites: import_zod.z.array(import_zod.z.string()).max(5).optional(),
58
+ safeSearch: import_zod.z.boolean().optional()
59
+ });
60
+ var xSourceSchema = import_zod.z.object({
61
+ type: import_zod.z.literal("x"),
62
+ xHandles: import_zod.z.array(import_zod.z.string()).optional()
63
+ });
64
+ var newsSourceSchema = import_zod.z.object({
65
+ type: import_zod.z.literal("news"),
66
+ country: import_zod.z.string().length(2).optional(),
67
+ excludedWebsites: import_zod.z.array(import_zod.z.string()).max(5).optional(),
68
+ safeSearch: import_zod.z.boolean().optional()
69
+ });
70
+ var rssSourceSchema = import_zod.z.object({
71
+ type: import_zod.z.literal("rss"),
72
+ links: import_zod.z.array(import_zod.z.string().url()).max(1)
73
+ // currently only supports one RSS link
74
+ });
75
+ var searchSourceSchema = import_zod.z.discriminatedUnion("type", [
76
+ webSourceSchema,
77
+ xSourceSchema,
78
+ newsSourceSchema,
79
+ rssSourceSchema
80
+ ]);
81
+ var xaiProviderOptions = import_zod.z.object({
82
+ /**
83
+ * reasoning effort for reasoning models
84
+ * only supported by grok-3-mini and grok-3-mini-fast models
85
+ */
86
+ reasoningEffort: import_zod.z.enum(["low", "high"]).optional(),
87
+ searchParameters: import_zod.z.object({
88
+ /**
89
+ * search mode preference
90
+ * - "off": disables search completely
91
+ * - "auto": model decides whether to search (default)
92
+ * - "on": always enables search
93
+ */
94
+ mode: import_zod.z.enum(["off", "auto", "on"]),
95
+ /**
96
+ * whether to return citations in the response
97
+ * defaults to true
98
+ */
99
+ returnCitations: import_zod.z.boolean().optional(),
100
+ /**
101
+ * start date for search data (ISO8601 format: YYYY-MM-DD)
102
+ */
103
+ fromDate: import_zod.z.string().optional(),
104
+ /**
105
+ * end date for search data (ISO8601 format: YYYY-MM-DD)
106
+ */
107
+ toDate: import_zod.z.string().optional(),
108
+ /**
109
+ * maximum number of search results to consider
110
+ * defaults to 20
111
+ */
112
+ maxSearchResults: import_zod.z.number().min(1).max(50).optional(),
113
+ /**
114
+ * data sources to search from
115
+ * defaults to ["web", "x"] if not specified
116
+ */
117
+ sources: import_zod.z.array(searchSourceSchema).optional()
118
+ }).optional()
119
+ });
52
120
 
53
121
  // src/xai-error.ts
54
- var import_zod = require("zod");
55
- var xaiErrorSchema = import_zod.z.object({
56
- code: import_zod.z.string(),
57
- error: import_zod.z.string()
122
+ var import_provider_utils = require("@ai-sdk/provider-utils");
123
+ var import_zod2 = require("zod");
124
+ var xaiErrorDataSchema = import_zod2.z.object({
125
+ error: import_zod2.z.object({
126
+ message: import_zod2.z.string(),
127
+ type: import_zod2.z.string().nullish(),
128
+ param: import_zod2.z.any().nullish(),
129
+ code: import_zod2.z.union([import_zod2.z.string(), import_zod2.z.number()]).nullish()
130
+ })
131
+ });
132
+ var xaiFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
133
+ errorSchema: xaiErrorDataSchema,
134
+ errorToMessage: (data) => data.error.message
135
+ });
136
+
137
+ // src/xai-chat-language-model.ts
138
+ var import_provider_utils3 = require("@ai-sdk/provider-utils");
139
+ var import_zod3 = require("zod");
140
+
141
+ // src/convert-to-xai-chat-messages.ts
142
+ var import_provider = require("@ai-sdk/provider");
143
+ var import_provider_utils2 = require("@ai-sdk/provider-utils");
144
+ function convertToXaiChatMessages(prompt) {
145
+ const messages = [];
146
+ const warnings = [];
147
+ for (const { role, content } of prompt) {
148
+ switch (role) {
149
+ case "system": {
150
+ messages.push({ role: "system", content });
151
+ break;
152
+ }
153
+ case "user": {
154
+ if (content.length === 1 && content[0].type === "text") {
155
+ messages.push({ role: "user", content: content[0].text });
156
+ break;
157
+ }
158
+ messages.push({
159
+ role: "user",
160
+ content: content.map((part) => {
161
+ switch (part.type) {
162
+ case "text": {
163
+ return { type: "text", text: part.text };
164
+ }
165
+ case "file": {
166
+ if (part.mediaType.startsWith("image/")) {
167
+ const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
168
+ return {
169
+ type: "image_url",
170
+ image_url: {
171
+ url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${(0, import_provider_utils2.convertToBase64)(part.data)}`
172
+ }
173
+ };
174
+ } else {
175
+ throw new import_provider.UnsupportedFunctionalityError({
176
+ functionality: `file part media type ${part.mediaType}`
177
+ });
178
+ }
179
+ }
180
+ }
181
+ })
182
+ });
183
+ break;
184
+ }
185
+ case "assistant": {
186
+ let text = "";
187
+ const toolCalls = [];
188
+ for (const part of content) {
189
+ switch (part.type) {
190
+ case "text": {
191
+ text += part.text;
192
+ break;
193
+ }
194
+ case "tool-call": {
195
+ toolCalls.push({
196
+ id: part.toolCallId,
197
+ type: "function",
198
+ function: {
199
+ name: part.toolName,
200
+ arguments: JSON.stringify(part.args)
201
+ }
202
+ });
203
+ break;
204
+ }
205
+ }
206
+ }
207
+ messages.push({
208
+ role: "assistant",
209
+ content: text,
210
+ tool_calls: toolCalls.length > 0 ? toolCalls : void 0
211
+ });
212
+ break;
213
+ }
214
+ case "tool": {
215
+ for (const toolResponse of content) {
216
+ messages.push({
217
+ role: "tool",
218
+ tool_call_id: toolResponse.toolCallId,
219
+ content: JSON.stringify(toolResponse.result)
220
+ });
221
+ }
222
+ break;
223
+ }
224
+ default: {
225
+ const _exhaustiveCheck = role;
226
+ throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
227
+ }
228
+ }
229
+ }
230
+ return { messages, warnings };
231
+ }
232
+
233
+ // src/get-response-metadata.ts
234
+ function getResponseMetadata({
235
+ id,
236
+ model,
237
+ created
238
+ }) {
239
+ return {
240
+ id: id != null ? id : void 0,
241
+ modelId: model != null ? model : void 0,
242
+ timestamp: created != null ? new Date(created * 1e3) : void 0
243
+ };
244
+ }
245
+
246
+ // src/map-xai-finish-reason.ts
247
+ function mapXaiFinishReason(finishReason) {
248
+ switch (finishReason) {
249
+ case "stop":
250
+ return "stop";
251
+ case "length":
252
+ return "length";
253
+ case "tool_calls":
254
+ case "function_call":
255
+ return "tool-calls";
256
+ case "content_filter":
257
+ return "content-filter";
258
+ default:
259
+ return "unknown";
260
+ }
261
+ }
262
+
263
+ // src/xai-prepare-tools.ts
264
+ var import_provider2 = require("@ai-sdk/provider");
265
+ function prepareTools({
266
+ tools,
267
+ toolChoice
268
+ }) {
269
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
270
+ const toolWarnings = [];
271
+ if (tools == null) {
272
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
273
+ }
274
+ const xaiTools = [];
275
+ for (const tool of tools) {
276
+ if (tool.type === "provider-defined") {
277
+ toolWarnings.push({ type: "unsupported-tool", tool });
278
+ } else {
279
+ xaiTools.push({
280
+ type: "function",
281
+ function: {
282
+ name: tool.name,
283
+ description: tool.description,
284
+ parameters: tool.parameters
285
+ }
286
+ });
287
+ }
288
+ }
289
+ if (toolChoice == null) {
290
+ return { tools: xaiTools, toolChoice: void 0, toolWarnings };
291
+ }
292
+ const type = toolChoice.type;
293
+ switch (type) {
294
+ case "auto":
295
+ case "none":
296
+ return { tools: xaiTools, toolChoice: type, toolWarnings };
297
+ case "required":
298
+ return { tools: xaiTools, toolChoice: "required", toolWarnings };
299
+ case "tool":
300
+ return {
301
+ tools: xaiTools,
302
+ toolChoice: {
303
+ type: "function",
304
+ function: { name: toolChoice.toolName }
305
+ },
306
+ toolWarnings
307
+ };
308
+ default: {
309
+ const _exhaustiveCheck = type;
310
+ throw new import_provider2.UnsupportedFunctionalityError({
311
+ functionality: `tool choice type: ${_exhaustiveCheck}`
312
+ });
313
+ }
314
+ }
315
+ }
316
+
317
+ // src/xai-chat-language-model.ts
318
+ var XaiChatLanguageModel = class {
319
+ constructor(modelId, config) {
320
+ this.specificationVersion = "v2";
321
+ this.supportedUrls = {
322
+ "image/*": [/^https?:\/\/.*$/]
323
+ };
324
+ this.modelId = modelId;
325
+ this.config = config;
326
+ }
327
+ get provider() {
328
+ return this.config.provider;
329
+ }
330
+ async getArgs({
331
+ prompt,
332
+ maxOutputTokens,
333
+ temperature,
334
+ topP,
335
+ topK,
336
+ frequencyPenalty,
337
+ presencePenalty,
338
+ stopSequences,
339
+ seed,
340
+ responseFormat,
341
+ providerOptions,
342
+ tools,
343
+ toolChoice
344
+ }) {
345
+ var _a, _b, _c;
346
+ const warnings = [];
347
+ const options = (_a = await (0, import_provider_utils3.parseProviderOptions)({
348
+ provider: "xai",
349
+ providerOptions,
350
+ schema: xaiProviderOptions
351
+ })) != null ? _a : {};
352
+ if (topK != null) {
353
+ warnings.push({
354
+ type: "unsupported-setting",
355
+ setting: "topK"
356
+ });
357
+ }
358
+ if (frequencyPenalty != null) {
359
+ warnings.push({
360
+ type: "unsupported-setting",
361
+ setting: "frequencyPenalty"
362
+ });
363
+ }
364
+ if (presencePenalty != null) {
365
+ warnings.push({
366
+ type: "unsupported-setting",
367
+ setting: "presencePenalty"
368
+ });
369
+ }
370
+ if (stopSequences != null) {
371
+ warnings.push({
372
+ type: "unsupported-setting",
373
+ setting: "stopSequences"
374
+ });
375
+ }
376
+ if (responseFormat != null && responseFormat.type === "json" && responseFormat.schema != null) {
377
+ warnings.push({
378
+ type: "unsupported-setting",
379
+ setting: "responseFormat",
380
+ details: "JSON response format schema is not supported"
381
+ });
382
+ }
383
+ const { messages, warnings: messageWarnings } = convertToXaiChatMessages(prompt);
384
+ warnings.push(...messageWarnings);
385
+ const {
386
+ tools: xaiTools,
387
+ toolChoice: xaiToolChoice,
388
+ toolWarnings
389
+ } = prepareTools({
390
+ tools,
391
+ toolChoice
392
+ });
393
+ warnings.push(...toolWarnings);
394
+ const baseArgs = {
395
+ // model id
396
+ model: this.modelId,
397
+ // standard generation settings
398
+ max_tokens: maxOutputTokens,
399
+ temperature,
400
+ top_p: topP,
401
+ seed,
402
+ reasoning_effort: options.reasoningEffort,
403
+ // response format
404
+ response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? responseFormat.schema != null ? {
405
+ type: "json_schema",
406
+ json_schema: {
407
+ name: (_b = responseFormat.name) != null ? _b : "response",
408
+ schema: responseFormat.schema,
409
+ strict: true
410
+ }
411
+ } : { type: "json_object" } : void 0,
412
+ // search parameters
413
+ search_parameters: options.searchParameters ? {
414
+ mode: options.searchParameters.mode,
415
+ return_citations: options.searchParameters.returnCitations,
416
+ from_date: options.searchParameters.fromDate,
417
+ to_date: options.searchParameters.toDate,
418
+ max_search_results: options.searchParameters.maxSearchResults,
419
+ sources: (_c = options.searchParameters.sources) == null ? void 0 : _c.map((source) => ({
420
+ type: source.type,
421
+ ...source.type === "web" && {
422
+ country: source.country,
423
+ excluded_websites: source.excludedWebsites,
424
+ allowed_websites: source.allowedWebsites,
425
+ safe_search: source.safeSearch
426
+ },
427
+ ...source.type === "x" && {
428
+ x_handles: source.xHandles
429
+ },
430
+ ...source.type === "news" && {
431
+ country: source.country,
432
+ excluded_websites: source.excludedWebsites,
433
+ safe_search: source.safeSearch
434
+ },
435
+ ...source.type === "rss" && {
436
+ links: source.links
437
+ }
438
+ }))
439
+ } : void 0,
440
+ // messages in xai format
441
+ messages,
442
+ // tools in xai format
443
+ tools: xaiTools,
444
+ tool_choice: xaiToolChoice
445
+ };
446
+ return {
447
+ args: baseArgs,
448
+ warnings
449
+ };
450
+ }
451
+ async doGenerate(options) {
452
+ var _a, _b, _c;
453
+ const { args: body, warnings } = await this.getArgs(options);
454
+ const {
455
+ responseHeaders,
456
+ value: response,
457
+ rawValue: rawResponse
458
+ } = await (0, import_provider_utils3.postJsonToApi)({
459
+ url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`,
460
+ headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
461
+ body,
462
+ failedResponseHandler: xaiFailedResponseHandler,
463
+ successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
464
+ xaiChatResponseSchema
465
+ ),
466
+ abortSignal: options.abortSignal,
467
+ fetch: this.config.fetch
468
+ });
469
+ const choice = response.choices[0];
470
+ const content = [];
471
+ if (choice.message.content != null && choice.message.content.length > 0) {
472
+ let text = choice.message.content;
473
+ const lastMessage = body.messages[body.messages.length - 1];
474
+ if ((lastMessage == null ? void 0 : lastMessage.role) === "assistant" && text === lastMessage.content) {
475
+ text = "";
476
+ }
477
+ if (text.length > 0) {
478
+ content.push({ type: "text", text });
479
+ }
480
+ }
481
+ if (choice.message.reasoning_content != null && choice.message.reasoning_content.length > 0) {
482
+ content.push({
483
+ type: "reasoning",
484
+ text: choice.message.reasoning_content
485
+ });
486
+ }
487
+ if (choice.message.tool_calls != null) {
488
+ for (const toolCall of choice.message.tool_calls) {
489
+ content.push({
490
+ type: "tool-call",
491
+ toolCallType: "function",
492
+ toolCallId: toolCall.id,
493
+ toolName: toolCall.function.name,
494
+ args: toolCall.function.arguments
495
+ });
496
+ }
497
+ }
498
+ if (response.citations != null) {
499
+ for (const url of response.citations) {
500
+ content.push({
501
+ type: "source",
502
+ sourceType: "url",
503
+ id: this.config.generateId(),
504
+ url
505
+ });
506
+ }
507
+ }
508
+ return {
509
+ content,
510
+ finishReason: mapXaiFinishReason(choice.finish_reason),
511
+ usage: {
512
+ inputTokens: response.usage.prompt_tokens,
513
+ outputTokens: response.usage.completion_tokens,
514
+ totalTokens: response.usage.total_tokens,
515
+ reasoningTokens: (_c = (_b = response.usage.completion_tokens_details) == null ? void 0 : _b.reasoning_tokens) != null ? _c : void 0
516
+ },
517
+ request: { body },
518
+ response: {
519
+ ...getResponseMetadata(response),
520
+ headers: responseHeaders,
521
+ body: rawResponse
522
+ },
523
+ warnings
524
+ };
525
+ }
526
+ async doStream(options) {
527
+ var _a;
528
+ const { args, warnings } = await this.getArgs(options);
529
+ const body = {
530
+ ...args,
531
+ stream: true,
532
+ stream_options: {
533
+ include_usage: true
534
+ }
535
+ };
536
+ const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
537
+ url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`,
538
+ headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
539
+ body,
540
+ failedResponseHandler: xaiFailedResponseHandler,
541
+ successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(xaiChatChunkSchema),
542
+ abortSignal: options.abortSignal,
543
+ fetch: this.config.fetch
544
+ });
545
+ let finishReason = "unknown";
546
+ const usage = {
547
+ inputTokens: void 0,
548
+ outputTokens: void 0,
549
+ totalTokens: void 0
550
+ };
551
+ let isFirstChunk = true;
552
+ const self = this;
553
+ return {
554
+ stream: response.pipeThrough(
555
+ new TransformStream({
556
+ start(controller) {
557
+ controller.enqueue({ type: "stream-start", warnings });
558
+ },
559
+ transform(chunk, controller) {
560
+ var _a2, _b;
561
+ if (!chunk.success) {
562
+ controller.enqueue({ type: "error", error: chunk.error });
563
+ return;
564
+ }
565
+ const value = chunk.value;
566
+ if (isFirstChunk) {
567
+ controller.enqueue({
568
+ type: "response-metadata",
569
+ ...getResponseMetadata(value)
570
+ });
571
+ isFirstChunk = false;
572
+ }
573
+ if (value.citations != null) {
574
+ for (const url of value.citations) {
575
+ controller.enqueue({
576
+ type: "source",
577
+ sourceType: "url",
578
+ id: self.config.generateId(),
579
+ url
580
+ });
581
+ }
582
+ }
583
+ if (value.usage != null) {
584
+ usage.inputTokens = value.usage.prompt_tokens;
585
+ usage.outputTokens = value.usage.completion_tokens;
586
+ usage.totalTokens = value.usage.total_tokens;
587
+ usage.reasoningTokens = (_b = (_a2 = value.usage.completion_tokens_details) == null ? void 0 : _a2.reasoning_tokens) != null ? _b : void 0;
588
+ }
589
+ const choice = value.choices[0];
590
+ if ((choice == null ? void 0 : choice.finish_reason) != null) {
591
+ finishReason = mapXaiFinishReason(choice.finish_reason);
592
+ }
593
+ if ((choice == null ? void 0 : choice.delta) == null) {
594
+ return;
595
+ }
596
+ const delta = choice.delta;
597
+ if (delta.content != null && delta.content.length > 0) {
598
+ let textContent = delta.content;
599
+ const lastMessage = body.messages[body.messages.length - 1];
600
+ if ((lastMessage == null ? void 0 : lastMessage.role) === "assistant" && textContent === lastMessage.content) {
601
+ return;
602
+ }
603
+ controller.enqueue({ type: "text", text: textContent });
604
+ }
605
+ if (delta.reasoning_content != null && delta.reasoning_content.length > 0) {
606
+ controller.enqueue({
607
+ type: "reasoning",
608
+ text: delta.reasoning_content
609
+ });
610
+ }
611
+ if (delta.tool_calls != null) {
612
+ for (const toolCall of delta.tool_calls) {
613
+ controller.enqueue({
614
+ type: "tool-call-delta",
615
+ toolCallType: "function",
616
+ toolCallId: toolCall.id,
617
+ toolName: toolCall.function.name,
618
+ argsTextDelta: toolCall.function.arguments
619
+ });
620
+ controller.enqueue({
621
+ type: "tool-call",
622
+ toolCallType: "function",
623
+ toolCallId: toolCall.id,
624
+ toolName: toolCall.function.name,
625
+ args: toolCall.function.arguments
626
+ });
627
+ }
628
+ }
629
+ },
630
+ flush(controller) {
631
+ controller.enqueue({ type: "finish", finishReason, usage });
632
+ }
633
+ })
634
+ ),
635
+ request: { body },
636
+ response: { headers: responseHeaders }
637
+ };
638
+ }
639
+ };
640
+ var xaiUsageSchema = import_zod3.z.object({
641
+ prompt_tokens: import_zod3.z.number(),
642
+ completion_tokens: import_zod3.z.number(),
643
+ total_tokens: import_zod3.z.number(),
644
+ completion_tokens_details: import_zod3.z.object({
645
+ reasoning_tokens: import_zod3.z.number().nullish()
646
+ }).nullish()
647
+ });
648
+ var xaiChatResponseSchema = import_zod3.z.object({
649
+ id: import_zod3.z.string().nullish(),
650
+ created: import_zod3.z.number().nullish(),
651
+ model: import_zod3.z.string().nullish(),
652
+ choices: import_zod3.z.array(
653
+ import_zod3.z.object({
654
+ message: import_zod3.z.object({
655
+ role: import_zod3.z.literal("assistant"),
656
+ content: import_zod3.z.string().nullish(),
657
+ reasoning_content: import_zod3.z.string().nullish(),
658
+ tool_calls: import_zod3.z.array(
659
+ import_zod3.z.object({
660
+ id: import_zod3.z.string(),
661
+ type: import_zod3.z.literal("function"),
662
+ function: import_zod3.z.object({
663
+ name: import_zod3.z.string(),
664
+ arguments: import_zod3.z.string()
665
+ })
666
+ })
667
+ ).nullish()
668
+ }),
669
+ index: import_zod3.z.number(),
670
+ finish_reason: import_zod3.z.string().nullish()
671
+ })
672
+ ),
673
+ object: import_zod3.z.literal("chat.completion"),
674
+ usage: xaiUsageSchema,
675
+ citations: import_zod3.z.array(import_zod3.z.string().url()).nullish()
676
+ });
677
+ var xaiChatChunkSchema = import_zod3.z.object({
678
+ id: import_zod3.z.string().nullish(),
679
+ created: import_zod3.z.number().nullish(),
680
+ model: import_zod3.z.string().nullish(),
681
+ choices: import_zod3.z.array(
682
+ import_zod3.z.object({
683
+ delta: import_zod3.z.object({
684
+ role: import_zod3.z.enum(["assistant"]).optional(),
685
+ content: import_zod3.z.string().nullish(),
686
+ reasoning_content: import_zod3.z.string().nullish(),
687
+ tool_calls: import_zod3.z.array(
688
+ import_zod3.z.object({
689
+ id: import_zod3.z.string(),
690
+ type: import_zod3.z.literal("function"),
691
+ function: import_zod3.z.object({
692
+ name: import_zod3.z.string(),
693
+ arguments: import_zod3.z.string()
694
+ })
695
+ })
696
+ ).nullish()
697
+ }),
698
+ finish_reason: import_zod3.z.string().nullish(),
699
+ index: import_zod3.z.number()
700
+ })
701
+ ),
702
+ usage: xaiUsageSchema.nullish(),
703
+ citations: import_zod3.z.array(import_zod3.z.string().url()).nullish()
58
704
  });
59
705
 
60
706
  // src/xai-provider.ts
61
707
  var xaiErrorStructure = {
62
- errorSchema: xaiErrorSchema,
63
- errorToMessage: (data) => data.error
708
+ errorSchema: xaiErrorDataSchema,
709
+ errorToMessage: (data) => data.error.message
64
710
  };
65
711
  function createXai(options = {}) {
66
712
  var _a;
67
- const baseURL = (0, import_provider_utils.withoutTrailingSlash)(
713
+ const baseURL = (0, import_provider_utils4.withoutTrailingSlash)(
68
714
  (_a = options.baseURL) != null ? _a : "https://api.x.ai/v1"
69
715
  );
70
716
  const getHeaders = () => ({
71
- Authorization: `Bearer ${(0, import_provider_utils.loadApiKey)({
717
+ Authorization: `Bearer ${(0, import_provider_utils4.loadApiKey)({
72
718
  apiKey: options.apiKey,
73
719
  environmentVariableName: "XAI_API_KEY",
74
720
  description: "xAI API key"
@@ -77,14 +723,12 @@ function createXai(options = {}) {
77
723
  });
78
724
  const createLanguageModel = (modelId) => {
79
725
  const structuredOutputs = supportsStructuredOutputs(modelId);
80
- return new import_openai_compatible.OpenAICompatibleChatLanguageModel(modelId, {
726
+ return new XaiChatLanguageModel(modelId, {
81
727
  provider: "xai.chat",
82
- url: ({ path }) => `${baseURL}${path}`,
728
+ baseURL,
83
729
  headers: getHeaders,
84
- fetch: options.fetch,
85
- errorStructure: xaiErrorStructure,
86
- supportsStructuredOutputs: structuredOutputs,
87
- includeUsage: true
730
+ generateId: import_provider_utils4.generateId,
731
+ fetch: options.fetch
88
732
  });
89
733
  };
90
734
  const createImageModel = (modelId) => {
@@ -100,7 +744,7 @@ function createXai(options = {}) {
100
744
  provider.languageModel = createLanguageModel;
101
745
  provider.chat = createLanguageModel;
102
746
  provider.textEmbeddingModel = (modelId) => {
103
- throw new import_provider.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
747
+ throw new import_provider3.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
104
748
  };
105
749
  provider.imageModel = createImageModel;
106
750
  provider.image = createImageModel;