@mastra/core 1.0.0-beta.0 → 1.0.0-beta.2

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 (100) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/agent/index.cjs +6 -6
  3. package/dist/agent/index.js +1 -1
  4. package/dist/{chunk-QCQLOMJM.cjs → chunk-22443P6A.cjs} +22 -21
  5. package/dist/chunk-22443P6A.cjs.map +1 -0
  6. package/dist/{chunk-YIK3ASEG.cjs → chunk-2ZVKF4HP.cjs} +117 -42
  7. package/dist/chunk-2ZVKF4HP.cjs.map +1 -0
  8. package/dist/{chunk-26SQQNMU.js → chunk-7CBEP2ZQ.js} +36 -12
  9. package/dist/chunk-7CBEP2ZQ.js.map +1 -0
  10. package/dist/{chunk-7SKXKUYT.js → chunk-7PO6SEJF.js} +6 -3
  11. package/dist/chunk-7PO6SEJF.js.map +1 -0
  12. package/dist/{chunk-S5MJLXMG.cjs → chunk-CB575O6L.cjs} +10 -2
  13. package/dist/chunk-CB575O6L.cjs.map +1 -0
  14. package/dist/chunk-HDJFSJCK.js +2237 -0
  15. package/dist/chunk-HDJFSJCK.js.map +1 -0
  16. package/dist/{chunk-BU4IAJWF.js → chunk-I4CXL4SR.js} +3 -3
  17. package/dist/{chunk-BU4IAJWF.js.map → chunk-I4CXL4SR.js.map} +1 -1
  18. package/dist/{chunk-VV753WCB.cjs → chunk-IQO7ANVS.cjs} +8 -8
  19. package/dist/{chunk-VV753WCB.cjs.map → chunk-IQO7ANVS.cjs.map} +1 -1
  20. package/dist/{chunk-L7XKOKOW.js → chunk-JPGVRWWL.js} +3 -3
  21. package/dist/chunk-JPGVRWWL.js.map +1 -0
  22. package/dist/{chunk-BXOL277H.cjs → chunk-JYYQQEBH.cjs} +7 -4
  23. package/dist/chunk-JYYQQEBH.cjs.map +1 -0
  24. package/dist/chunk-LWBQ4P4N.cjs +2240 -0
  25. package/dist/chunk-LWBQ4P4N.cjs.map +1 -0
  26. package/dist/{chunk-VJUZZB2I.js → chunk-SNPVZPLB.js} +4 -4
  27. package/dist/{chunk-VJUZZB2I.js.map → chunk-SNPVZPLB.js.map} +1 -1
  28. package/dist/{chunk-32CTMD2C.js → chunk-W7UH2PWL.js} +108 -33
  29. package/dist/chunk-W7UH2PWL.js.map +1 -0
  30. package/dist/{chunk-FD734TPS.cjs → chunk-YCVEJ3UN.cjs} +37 -13
  31. package/dist/chunk-YCVEJ3UN.cjs.map +1 -0
  32. package/dist/{chunk-P6APHXPZ.js → chunk-ZGHTOYHW.js} +5 -4
  33. package/dist/chunk-ZGHTOYHW.js.map +1 -0
  34. package/dist/{chunk-QUZGDSWE.cjs → chunk-ZWNI5IWX.cjs} +11 -11
  35. package/dist/{chunk-QUZGDSWE.cjs.map → chunk-ZWNI5IWX.cjs.map} +1 -1
  36. package/dist/evals/index.cjs +4 -4
  37. package/dist/evals/index.js +1 -1
  38. package/dist/evals/scoreTraces/index.cjs +3 -3
  39. package/dist/evals/scoreTraces/index.js +1 -1
  40. package/dist/index.cjs +2 -2
  41. package/dist/index.js +1 -1
  42. package/dist/llm/index.cjs +7 -7
  43. package/dist/llm/index.js +1 -1
  44. package/dist/llm/model/gateways/constants.d.ts.map +1 -1
  45. package/dist/llm/model/gateways/models-dev.d.ts.map +1 -1
  46. package/dist/llm/model/provider-types.generated.d.ts +48 -10
  47. package/dist/loop/index.cjs +2 -2
  48. package/dist/loop/index.js +1 -1
  49. package/dist/mastra/index.cjs +2 -2
  50. package/dist/mastra/index.d.ts.map +1 -1
  51. package/dist/mastra/index.js +1 -1
  52. package/dist/memory/index.cjs +2 -2
  53. package/dist/memory/index.js +1 -1
  54. package/dist/models-dev-DNBKXHT4.js +3 -0
  55. package/dist/{models-dev-7U4NRMM3.js.map → models-dev-DNBKXHT4.js.map} +1 -1
  56. package/dist/models-dev-YBEEQIX6.cjs +12 -0
  57. package/dist/{models-dev-VKSAQPRK.cjs.map → models-dev-YBEEQIX6.cjs.map} +1 -1
  58. package/dist/netlify-7G2L5VSH.js +3 -0
  59. package/dist/{netlify-42ZNWIDQ.js.map → netlify-7G2L5VSH.js.map} +1 -1
  60. package/dist/netlify-GWNGSIRZ.cjs +12 -0
  61. package/dist/{netlify-2IDXTNFW.cjs.map → netlify-GWNGSIRZ.cjs.map} +1 -1
  62. package/dist/processors/index.cjs +11 -11
  63. package/dist/processors/index.js +1 -1
  64. package/dist/provider-registry.json +101 -26
  65. package/dist/relevance/index.cjs +2 -2
  66. package/dist/relevance/index.js +1 -1
  67. package/dist/stream/index.cjs +8 -8
  68. package/dist/stream/index.js +1 -1
  69. package/dist/workflows/default.d.ts.map +1 -1
  70. package/dist/workflows/evented/index.cjs +10 -10
  71. package/dist/workflows/evented/index.js +1 -1
  72. package/dist/workflows/evented/step-executor.d.ts.map +1 -1
  73. package/dist/workflows/index.cjs +16 -16
  74. package/dist/workflows/index.js +1 -1
  75. package/dist/workflows/step.d.ts +1 -1
  76. package/dist/workflows/step.d.ts.map +1 -1
  77. package/dist/workflows/types.d.ts +11 -5
  78. package/dist/workflows/types.d.ts.map +1 -1
  79. package/dist/workflows/workflow.d.ts +1 -0
  80. package/dist/workflows/workflow.d.ts.map +1 -1
  81. package/package.json +5 -4
  82. package/src/llm/model/provider-types.generated.d.ts +48 -10
  83. package/dist/chunk-26SQQNMU.js.map +0 -1
  84. package/dist/chunk-32CTMD2C.js.map +0 -1
  85. package/dist/chunk-7SKXKUYT.js.map +0 -1
  86. package/dist/chunk-BNBRQS7N.js +0 -910
  87. package/dist/chunk-BNBRQS7N.js.map +0 -1
  88. package/dist/chunk-BXOL277H.cjs.map +0 -1
  89. package/dist/chunk-FD734TPS.cjs.map +0 -1
  90. package/dist/chunk-IU2SZXJQ.cjs +0 -913
  91. package/dist/chunk-IU2SZXJQ.cjs.map +0 -1
  92. package/dist/chunk-L7XKOKOW.js.map +0 -1
  93. package/dist/chunk-P6APHXPZ.js.map +0 -1
  94. package/dist/chunk-QCQLOMJM.cjs.map +0 -1
  95. package/dist/chunk-S5MJLXMG.cjs.map +0 -1
  96. package/dist/chunk-YIK3ASEG.cjs.map +0 -1
  97. package/dist/models-dev-7U4NRMM3.js +0 -3
  98. package/dist/models-dev-VKSAQPRK.cjs +0 -12
  99. package/dist/netlify-2IDXTNFW.cjs +0 -12
  100. package/dist/netlify-42ZNWIDQ.js +0 -3
@@ -1,910 +0,0 @@
1
- import { createJsonErrorResponseHandler, withoutTrailingSlash, generateId, withUserAgentSuffix, parseProviderOptions, postJsonToApi, createJsonResponseHandler, combineHeaders, createEventSourceResponseHandler, loadApiKey, convertToBase64, UnsupportedFunctionalityError, NoSuchModelError, MastraModelGateway, createOpenAICompatible, createAnthropic, createGoogleGenerativeAI, createOpenAI, OpenAICompatibleImageModel } from './chunk-L7XKOKOW.js';
2
- import { z } from 'zod/v4';
3
- import { createOpenRouter } from '@openrouter/ai-sdk-provider-v5';
4
-
5
- function convertToXaiChatMessages(prompt) {
6
- const messages = [];
7
- const warnings = [];
8
- for (const { role, content } of prompt) {
9
- switch (role) {
10
- case "system": {
11
- messages.push({ role: "system", content });
12
- break;
13
- }
14
- case "user": {
15
- if (content.length === 1 && content[0].type === "text") {
16
- messages.push({ role: "user", content: content[0].text });
17
- break;
18
- }
19
- messages.push({
20
- role: "user",
21
- content: content.map((part) => {
22
- switch (part.type) {
23
- case "text": {
24
- return { type: "text", text: part.text };
25
- }
26
- case "file": {
27
- if (part.mediaType.startsWith("image/")) {
28
- const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
29
- return {
30
- type: "image_url",
31
- image_url: {
32
- url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`
33
- }
34
- };
35
- } else {
36
- throw new UnsupportedFunctionalityError({
37
- functionality: `file part media type ${part.mediaType}`
38
- });
39
- }
40
- }
41
- }
42
- })
43
- });
44
- break;
45
- }
46
- case "assistant": {
47
- let text = "";
48
- const toolCalls = [];
49
- for (const part of content) {
50
- switch (part.type) {
51
- case "text": {
52
- text += part.text;
53
- break;
54
- }
55
- case "tool-call": {
56
- toolCalls.push({
57
- id: part.toolCallId,
58
- type: "function",
59
- function: {
60
- name: part.toolName,
61
- arguments: JSON.stringify(part.input)
62
- }
63
- });
64
- break;
65
- }
66
- }
67
- }
68
- messages.push({
69
- role: "assistant",
70
- content: text,
71
- tool_calls: toolCalls.length > 0 ? toolCalls : void 0
72
- });
73
- break;
74
- }
75
- case "tool": {
76
- for (const toolResponse of content) {
77
- const output = toolResponse.output;
78
- let contentValue;
79
- switch (output.type) {
80
- case "text":
81
- case "error-text":
82
- contentValue = output.value;
83
- break;
84
- case "content":
85
- case "json":
86
- case "error-json":
87
- contentValue = JSON.stringify(output.value);
88
- break;
89
- }
90
- messages.push({
91
- role: "tool",
92
- tool_call_id: toolResponse.toolCallId,
93
- content: contentValue
94
- });
95
- }
96
- break;
97
- }
98
- default: {
99
- const _exhaustiveCheck = role;
100
- throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
101
- }
102
- }
103
- }
104
- return { messages, warnings };
105
- }
106
- function getResponseMetadata({
107
- id,
108
- model,
109
- created
110
- }) {
111
- return {
112
- id: id != null ? id : void 0,
113
- modelId: model != null ? model : void 0,
114
- timestamp: created != null ? new Date(created * 1e3) : void 0
115
- };
116
- }
117
- function mapXaiFinishReason(finishReason) {
118
- switch (finishReason) {
119
- case "stop":
120
- return "stop";
121
- case "length":
122
- return "length";
123
- case "tool_calls":
124
- case "function_call":
125
- return "tool-calls";
126
- case "content_filter":
127
- return "content-filter";
128
- default:
129
- return "unknown";
130
- }
131
- }
132
- var webSourceSchema = z.object({
133
- type: z.literal("web"),
134
- country: z.string().length(2).optional(),
135
- excludedWebsites: z.array(z.string()).max(5).optional(),
136
- allowedWebsites: z.array(z.string()).max(5).optional(),
137
- safeSearch: z.boolean().optional()
138
- });
139
- var xSourceSchema = z.object({
140
- type: z.literal("x"),
141
- excludedXHandles: z.array(z.string()).optional(),
142
- includedXHandles: z.array(z.string()).optional(),
143
- postFavoriteCount: z.number().int().optional(),
144
- postViewCount: z.number().int().optional(),
145
- /**
146
- * @deprecated use `includedXHandles` instead
147
- */
148
- xHandles: z.array(z.string()).optional()
149
- });
150
- var newsSourceSchema = z.object({
151
- type: z.literal("news"),
152
- country: z.string().length(2).optional(),
153
- excludedWebsites: z.array(z.string()).max(5).optional(),
154
- safeSearch: z.boolean().optional()
155
- });
156
- var rssSourceSchema = z.object({
157
- type: z.literal("rss"),
158
- links: z.array(z.string().url()).max(1)
159
- // currently only supports one RSS link
160
- });
161
- var searchSourceSchema = z.discriminatedUnion("type", [
162
- webSourceSchema,
163
- xSourceSchema,
164
- newsSourceSchema,
165
- rssSourceSchema
166
- ]);
167
- var xaiProviderOptions = z.object({
168
- reasoningEffort: z.enum(["low", "high"]).optional(),
169
- searchParameters: z.object({
170
- /**
171
- * search mode preference
172
- * - "off": disables search completely
173
- * - "auto": model decides whether to search (default)
174
- * - "on": always enables search
175
- */
176
- mode: z.enum(["off", "auto", "on"]),
177
- /**
178
- * whether to return citations in the response
179
- * defaults to true
180
- */
181
- returnCitations: z.boolean().optional(),
182
- /**
183
- * start date for search data (ISO8601 format: YYYY-MM-DD)
184
- */
185
- fromDate: z.string().optional(),
186
- /**
187
- * end date for search data (ISO8601 format: YYYY-MM-DD)
188
- */
189
- toDate: z.string().optional(),
190
- /**
191
- * maximum number of search results to consider
192
- * defaults to 20
193
- */
194
- maxSearchResults: z.number().min(1).max(50).optional(),
195
- /**
196
- * data sources to search from
197
- * defaults to ["web", "x"] if not specified
198
- */
199
- sources: z.array(searchSourceSchema).optional()
200
- }).optional()
201
- });
202
- var xaiErrorDataSchema = z.object({
203
- error: z.object({
204
- message: z.string(),
205
- type: z.string().nullish(),
206
- param: z.any().nullish(),
207
- code: z.union([z.string(), z.number()]).nullish()
208
- })
209
- });
210
- var xaiFailedResponseHandler = createJsonErrorResponseHandler({
211
- errorSchema: xaiErrorDataSchema,
212
- errorToMessage: (data) => data.error.message
213
- });
214
- function prepareTools({
215
- tools,
216
- toolChoice
217
- }) {
218
- tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
219
- const toolWarnings = [];
220
- if (tools == null) {
221
- return { tools: void 0, toolChoice: void 0, toolWarnings };
222
- }
223
- const xaiTools = [];
224
- for (const tool of tools) {
225
- if (tool.type === "provider-defined") {
226
- toolWarnings.push({ type: "unsupported-tool", tool });
227
- } else {
228
- xaiTools.push({
229
- type: "function",
230
- function: {
231
- name: tool.name,
232
- description: tool.description,
233
- parameters: tool.inputSchema
234
- }
235
- });
236
- }
237
- }
238
- if (toolChoice == null) {
239
- return { tools: xaiTools, toolChoice: void 0, toolWarnings };
240
- }
241
- const type = toolChoice.type;
242
- switch (type) {
243
- case "auto":
244
- case "none":
245
- return { tools: xaiTools, toolChoice: type, toolWarnings };
246
- case "required":
247
- return { tools: xaiTools, toolChoice: "required", toolWarnings };
248
- case "tool":
249
- return {
250
- tools: xaiTools,
251
- toolChoice: {
252
- type: "function",
253
- function: { name: toolChoice.toolName }
254
- },
255
- toolWarnings
256
- };
257
- default: {
258
- const _exhaustiveCheck = type;
259
- throw new UnsupportedFunctionalityError({
260
- functionality: `tool choice type: ${_exhaustiveCheck}`
261
- });
262
- }
263
- }
264
- }
265
- var XaiChatLanguageModel = class {
266
- constructor(modelId, config) {
267
- this.specificationVersion = "v2";
268
- this.supportedUrls = {
269
- "image/*": [/^https?:\/\/.*$/]
270
- };
271
- this.modelId = modelId;
272
- this.config = config;
273
- }
274
- get provider() {
275
- return this.config.provider;
276
- }
277
- async getArgs({
278
- prompt,
279
- maxOutputTokens,
280
- temperature,
281
- topP,
282
- topK,
283
- frequencyPenalty,
284
- presencePenalty,
285
- stopSequences,
286
- seed,
287
- responseFormat,
288
- providerOptions,
289
- tools,
290
- toolChoice
291
- }) {
292
- var _a, _b, _c;
293
- const warnings = [];
294
- const options = (_a = await parseProviderOptions({
295
- provider: "xai",
296
- providerOptions,
297
- schema: xaiProviderOptions
298
- })) != null ? _a : {};
299
- if (topK != null) {
300
- warnings.push({
301
- type: "unsupported-setting",
302
- setting: "topK"
303
- });
304
- }
305
- if (frequencyPenalty != null) {
306
- warnings.push({
307
- type: "unsupported-setting",
308
- setting: "frequencyPenalty"
309
- });
310
- }
311
- if (presencePenalty != null) {
312
- warnings.push({
313
- type: "unsupported-setting",
314
- setting: "presencePenalty"
315
- });
316
- }
317
- if (stopSequences != null) {
318
- warnings.push({
319
- type: "unsupported-setting",
320
- setting: "stopSequences"
321
- });
322
- }
323
- if (responseFormat != null && responseFormat.type === "json" && responseFormat.schema != null) {
324
- warnings.push({
325
- type: "unsupported-setting",
326
- setting: "responseFormat",
327
- details: "JSON response format schema is not supported"
328
- });
329
- }
330
- const { messages, warnings: messageWarnings } = convertToXaiChatMessages(prompt);
331
- warnings.push(...messageWarnings);
332
- const {
333
- tools: xaiTools,
334
- toolChoice: xaiToolChoice,
335
- toolWarnings
336
- } = prepareTools({
337
- tools,
338
- toolChoice
339
- });
340
- warnings.push(...toolWarnings);
341
- const baseArgs = {
342
- // model id
343
- model: this.modelId,
344
- // standard generation settings
345
- max_tokens: maxOutputTokens,
346
- temperature,
347
- top_p: topP,
348
- seed,
349
- reasoning_effort: options.reasoningEffort,
350
- // response format
351
- response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? responseFormat.schema != null ? {
352
- type: "json_schema",
353
- json_schema: {
354
- name: (_b = responseFormat.name) != null ? _b : "response",
355
- schema: responseFormat.schema,
356
- strict: true
357
- }
358
- } : { type: "json_object" } : void 0,
359
- // search parameters
360
- search_parameters: options.searchParameters ? {
361
- mode: options.searchParameters.mode,
362
- return_citations: options.searchParameters.returnCitations,
363
- from_date: options.searchParameters.fromDate,
364
- to_date: options.searchParameters.toDate,
365
- max_search_results: options.searchParameters.maxSearchResults,
366
- sources: (_c = options.searchParameters.sources) == null ? void 0 : _c.map((source) => {
367
- var _a2;
368
- return {
369
- type: source.type,
370
- ...source.type === "web" && {
371
- country: source.country,
372
- excluded_websites: source.excludedWebsites,
373
- allowed_websites: source.allowedWebsites,
374
- safe_search: source.safeSearch
375
- },
376
- ...source.type === "x" && {
377
- excluded_x_handles: source.excludedXHandles,
378
- included_x_handles: (_a2 = source.includedXHandles) != null ? _a2 : source.xHandles,
379
- post_favorite_count: source.postFavoriteCount,
380
- post_view_count: source.postViewCount
381
- },
382
- ...source.type === "news" && {
383
- country: source.country,
384
- excluded_websites: source.excludedWebsites,
385
- safe_search: source.safeSearch
386
- },
387
- ...source.type === "rss" && {
388
- links: source.links
389
- }
390
- };
391
- })
392
- } : void 0,
393
- // messages in xai format
394
- messages,
395
- // tools in xai format
396
- tools: xaiTools,
397
- tool_choice: xaiToolChoice
398
- };
399
- return {
400
- args: baseArgs,
401
- warnings
402
- };
403
- }
404
- async doGenerate(options) {
405
- var _a, _b, _c;
406
- const { args: body, warnings } = await this.getArgs(options);
407
- const {
408
- responseHeaders,
409
- value: response,
410
- rawValue: rawResponse
411
- } = await postJsonToApi({
412
- url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`,
413
- headers: combineHeaders(this.config.headers(), options.headers),
414
- body,
415
- failedResponseHandler: xaiFailedResponseHandler,
416
- successfulResponseHandler: createJsonResponseHandler(
417
- xaiChatResponseSchema
418
- ),
419
- abortSignal: options.abortSignal,
420
- fetch: this.config.fetch
421
- });
422
- const choice = response.choices[0];
423
- const content = [];
424
- if (choice.message.content != null && choice.message.content.length > 0) {
425
- let text = choice.message.content;
426
- const lastMessage = body.messages[body.messages.length - 1];
427
- if ((lastMessage == null ? void 0 : lastMessage.role) === "assistant" && text === lastMessage.content) {
428
- text = "";
429
- }
430
- if (text.length > 0) {
431
- content.push({ type: "text", text });
432
- }
433
- }
434
- if (choice.message.reasoning_content != null && choice.message.reasoning_content.length > 0) {
435
- content.push({
436
- type: "reasoning",
437
- text: choice.message.reasoning_content
438
- });
439
- }
440
- if (choice.message.tool_calls != null) {
441
- for (const toolCall of choice.message.tool_calls) {
442
- content.push({
443
- type: "tool-call",
444
- toolCallId: toolCall.id,
445
- toolName: toolCall.function.name,
446
- input: toolCall.function.arguments
447
- });
448
- }
449
- }
450
- if (response.citations != null) {
451
- for (const url of response.citations) {
452
- content.push({
453
- type: "source",
454
- sourceType: "url",
455
- id: this.config.generateId(),
456
- url
457
- });
458
- }
459
- }
460
- return {
461
- content,
462
- finishReason: mapXaiFinishReason(choice.finish_reason),
463
- usage: {
464
- inputTokens: response.usage.prompt_tokens,
465
- outputTokens: response.usage.completion_tokens,
466
- totalTokens: response.usage.total_tokens,
467
- reasoningTokens: (_c = (_b = response.usage.completion_tokens_details) == null ? void 0 : _b.reasoning_tokens) != null ? _c : void 0
468
- },
469
- request: { body },
470
- response: {
471
- ...getResponseMetadata(response),
472
- headers: responseHeaders,
473
- body: rawResponse
474
- },
475
- warnings
476
- };
477
- }
478
- async doStream(options) {
479
- var _a;
480
- const { args, warnings } = await this.getArgs(options);
481
- const body = {
482
- ...args,
483
- stream: true,
484
- stream_options: {
485
- include_usage: true
486
- }
487
- };
488
- const { responseHeaders, value: response } = await postJsonToApi({
489
- url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`,
490
- headers: combineHeaders(this.config.headers(), options.headers),
491
- body,
492
- failedResponseHandler: xaiFailedResponseHandler,
493
- successfulResponseHandler: createEventSourceResponseHandler(xaiChatChunkSchema),
494
- abortSignal: options.abortSignal,
495
- fetch: this.config.fetch
496
- });
497
- let finishReason = "unknown";
498
- const usage = {
499
- inputTokens: void 0,
500
- outputTokens: void 0,
501
- totalTokens: void 0
502
- };
503
- let isFirstChunk = true;
504
- const contentBlocks = {};
505
- const lastReasoningDeltas = {};
506
- const self = this;
507
- return {
508
- stream: response.pipeThrough(
509
- new TransformStream({
510
- start(controller) {
511
- controller.enqueue({ type: "stream-start", warnings });
512
- },
513
- transform(chunk, controller) {
514
- var _a2, _b;
515
- if (options.includeRawChunks) {
516
- controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
517
- }
518
- if (!chunk.success) {
519
- controller.enqueue({ type: "error", error: chunk.error });
520
- return;
521
- }
522
- const value = chunk.value;
523
- if (isFirstChunk) {
524
- controller.enqueue({
525
- type: "response-metadata",
526
- ...getResponseMetadata(value)
527
- });
528
- isFirstChunk = false;
529
- }
530
- if (value.citations != null) {
531
- for (const url of value.citations) {
532
- controller.enqueue({
533
- type: "source",
534
- sourceType: "url",
535
- id: self.config.generateId(),
536
- url
537
- });
538
- }
539
- }
540
- if (value.usage != null) {
541
- usage.inputTokens = value.usage.prompt_tokens;
542
- usage.outputTokens = value.usage.completion_tokens;
543
- usage.totalTokens = value.usage.total_tokens;
544
- usage.reasoningTokens = (_b = (_a2 = value.usage.completion_tokens_details) == null ? void 0 : _a2.reasoning_tokens) != null ? _b : void 0;
545
- }
546
- const choice = value.choices[0];
547
- if ((choice == null ? void 0 : choice.finish_reason) != null) {
548
- finishReason = mapXaiFinishReason(choice.finish_reason);
549
- }
550
- if ((choice == null ? void 0 : choice.delta) == null) {
551
- return;
552
- }
553
- const delta = choice.delta;
554
- const choiceIndex = choice.index;
555
- if (delta.content != null && delta.content.length > 0) {
556
- const textContent = delta.content;
557
- const lastMessage = body.messages[body.messages.length - 1];
558
- if ((lastMessage == null ? void 0 : lastMessage.role) === "assistant" && textContent === lastMessage.content) {
559
- return;
560
- }
561
- const blockId = `text-${value.id || choiceIndex}`;
562
- if (contentBlocks[blockId] == null) {
563
- contentBlocks[blockId] = { type: "text" };
564
- controller.enqueue({
565
- type: "text-start",
566
- id: blockId
567
- });
568
- }
569
- controller.enqueue({
570
- type: "text-delta",
571
- id: blockId,
572
- delta: textContent
573
- });
574
- }
575
- if (delta.reasoning_content != null && delta.reasoning_content.length > 0) {
576
- const blockId = `reasoning-${value.id || choiceIndex}`;
577
- if (lastReasoningDeltas[blockId] === delta.reasoning_content) {
578
- return;
579
- }
580
- lastReasoningDeltas[blockId] = delta.reasoning_content;
581
- if (contentBlocks[blockId] == null) {
582
- contentBlocks[blockId] = { type: "reasoning" };
583
- controller.enqueue({
584
- type: "reasoning-start",
585
- id: blockId
586
- });
587
- }
588
- controller.enqueue({
589
- type: "reasoning-delta",
590
- id: blockId,
591
- delta: delta.reasoning_content
592
- });
593
- }
594
- if (delta.tool_calls != null) {
595
- for (const toolCall of delta.tool_calls) {
596
- const toolCallId = toolCall.id;
597
- controller.enqueue({
598
- type: "tool-input-start",
599
- id: toolCallId,
600
- toolName: toolCall.function.name
601
- });
602
- controller.enqueue({
603
- type: "tool-input-delta",
604
- id: toolCallId,
605
- delta: toolCall.function.arguments
606
- });
607
- controller.enqueue({
608
- type: "tool-input-end",
609
- id: toolCallId
610
- });
611
- controller.enqueue({
612
- type: "tool-call",
613
- toolCallId,
614
- toolName: toolCall.function.name,
615
- input: toolCall.function.arguments
616
- });
617
- }
618
- }
619
- },
620
- flush(controller) {
621
- for (const [blockId, block] of Object.entries(contentBlocks)) {
622
- controller.enqueue({
623
- type: block.type === "text" ? "text-end" : "reasoning-end",
624
- id: blockId
625
- });
626
- }
627
- controller.enqueue({ type: "finish", finishReason, usage });
628
- }
629
- })
630
- ),
631
- request: { body },
632
- response: { headers: responseHeaders }
633
- };
634
- }
635
- };
636
- var xaiUsageSchema = z.object({
637
- prompt_tokens: z.number(),
638
- completion_tokens: z.number(),
639
- total_tokens: z.number(),
640
- completion_tokens_details: z.object({
641
- reasoning_tokens: z.number().nullish()
642
- }).nullish()
643
- });
644
- var xaiChatResponseSchema = z.object({
645
- id: z.string().nullish(),
646
- created: z.number().nullish(),
647
- model: z.string().nullish(),
648
- choices: z.array(
649
- z.object({
650
- message: z.object({
651
- role: z.literal("assistant"),
652
- content: z.string().nullish(),
653
- reasoning_content: z.string().nullish(),
654
- tool_calls: z.array(
655
- z.object({
656
- id: z.string(),
657
- type: z.literal("function"),
658
- function: z.object({
659
- name: z.string(),
660
- arguments: z.string()
661
- })
662
- })
663
- ).nullish()
664
- }),
665
- index: z.number(),
666
- finish_reason: z.string().nullish()
667
- })
668
- ),
669
- object: z.literal("chat.completion"),
670
- usage: xaiUsageSchema,
671
- citations: z.array(z.string().url()).nullish()
672
- });
673
- var xaiChatChunkSchema = z.object({
674
- id: z.string().nullish(),
675
- created: z.number().nullish(),
676
- model: z.string().nullish(),
677
- choices: z.array(
678
- z.object({
679
- delta: z.object({
680
- role: z.enum(["assistant"]).optional(),
681
- content: z.string().nullish(),
682
- reasoning_content: z.string().nullish(),
683
- tool_calls: z.array(
684
- z.object({
685
- id: z.string(),
686
- type: z.literal("function"),
687
- function: z.object({
688
- name: z.string(),
689
- arguments: z.string()
690
- })
691
- })
692
- ).nullish()
693
- }),
694
- finish_reason: z.string().nullish(),
695
- index: z.number()
696
- })
697
- ),
698
- usage: xaiUsageSchema.nullish(),
699
- citations: z.array(z.string().url()).nullish()
700
- });
701
- var VERSION = "2.0.26" ;
702
- var xaiErrorStructure = {
703
- errorSchema: xaiErrorDataSchema,
704
- errorToMessage: (data) => data.error.message
705
- };
706
- function createXai(options = {}) {
707
- var _a;
708
- const baseURL = withoutTrailingSlash(
709
- (_a = options.baseURL) != null ? _a : "https://api.x.ai/v1"
710
- );
711
- const getHeaders = () => withUserAgentSuffix(
712
- {
713
- Authorization: `Bearer ${loadApiKey({
714
- apiKey: options.apiKey,
715
- environmentVariableName: "XAI_API_KEY",
716
- description: "xAI API key"
717
- })}`,
718
- ...options.headers
719
- },
720
- `ai-sdk/xai/${VERSION}`
721
- );
722
- const createLanguageModel = (modelId) => {
723
- return new XaiChatLanguageModel(modelId, {
724
- provider: "xai.chat",
725
- baseURL,
726
- headers: getHeaders,
727
- generateId,
728
- fetch: options.fetch
729
- });
730
- };
731
- const createImageModel = (modelId) => {
732
- return new OpenAICompatibleImageModel(modelId, {
733
- provider: "xai.image",
734
- url: ({ path }) => `${baseURL}${path}`,
735
- headers: getHeaders,
736
- fetch: options.fetch,
737
- errorStructure: xaiErrorStructure
738
- });
739
- };
740
- const provider = (modelId) => createLanguageModel(modelId);
741
- provider.languageModel = createLanguageModel;
742
- provider.chat = createLanguageModel;
743
- provider.textEmbeddingModel = (modelId) => {
744
- throw new NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
745
- };
746
- provider.imageModel = createImageModel;
747
- provider.image = createImageModel;
748
- return provider;
749
- }
750
- createXai();
751
-
752
- // src/llm/model/gateway-resolver.ts
753
- function parseModelRouterId(routerId, gatewayPrefix) {
754
- if (gatewayPrefix && !routerId.startsWith(`${gatewayPrefix}/`)) {
755
- throw new Error(`Expected ${gatewayPrefix}/ in model router ID ${routerId}`);
756
- }
757
- const idParts = routerId.split("/");
758
- if (gatewayPrefix && idParts.length < 3) {
759
- throw new Error(
760
- `Expected atleast 3 id parts ${gatewayPrefix}/provider/model, but only saw ${idParts.length} in ${routerId}`
761
- );
762
- }
763
- const providerId = idParts.at(gatewayPrefix ? 1 : 0);
764
- const modelId = idParts.slice(gatewayPrefix ? 2 : 1).join(`/`);
765
- if (!routerId.includes(`/`) || !providerId || !modelId) {
766
- throw new Error(
767
- `Attempted to parse provider/model from ${routerId} but this ID doesn't appear to contain a provider`
768
- );
769
- }
770
- return {
771
- providerId,
772
- modelId
773
- };
774
- }
775
-
776
- // src/llm/model/gateways/constants.ts
777
- var PROVIDERS_WITH_INSTALLED_PACKAGES = ["anthropic", "google", "openai", "openrouter", "xai"];
778
- var EXCLUDED_PROVIDERS = ["github-copilot"];
779
-
780
- // src/llm/model/gateways/models-dev.ts
781
- var OPENAI_COMPATIBLE_OVERRIDES = {
782
- cerebras: {
783
- url: "https://api.cerebras.ai/v1"
784
- },
785
- mistral: {
786
- url: "https://api.mistral.ai/v1"
787
- },
788
- groq: {
789
- url: "https://api.groq.com/openai/v1"
790
- },
791
- togetherai: {
792
- url: "https://api.together.xyz/v1"
793
- },
794
- deepinfra: {
795
- url: "https://api.deepinfra.com/v1/openai"
796
- },
797
- perplexity: {
798
- url: "https://api.perplexity.ai"
799
- },
800
- vercel: {
801
- url: "https://ai-gateway.vercel.sh/v1",
802
- apiKeyEnvVar: "AI_GATEWAY_API_KEY"
803
- }
804
- };
805
- var ModelsDevGateway = class extends MastraModelGateway {
806
- name = "models.dev";
807
- prefix = void 0;
808
- // No prefix for registry gateway
809
- providerConfigs = {};
810
- constructor(providerConfigs) {
811
- super();
812
- if (providerConfigs) this.providerConfigs = providerConfigs;
813
- }
814
- async fetchProviders() {
815
- const response = await fetch("https://models.dev/api.json");
816
- if (!response.ok) {
817
- throw new Error(`Failed to fetch from models.dev: ${response.statusText}`);
818
- }
819
- const data = await response.json();
820
- const providerConfigs = {};
821
- for (const [providerId, providerInfo] of Object.entries(data)) {
822
- if (EXCLUDED_PROVIDERS.includes(providerId)) continue;
823
- if (!providerInfo || typeof providerInfo !== "object" || !providerInfo.models) continue;
824
- const normalizedId = providerId;
825
- const isOpenAICompatible = providerInfo.npm === "@ai-sdk/openai-compatible" || providerInfo.npm === "@ai-sdk/gateway" || // Vercel AI Gateway is OpenAI-compatible
826
- normalizedId in OPENAI_COMPATIBLE_OVERRIDES;
827
- const hasInstalledPackage = PROVIDERS_WITH_INSTALLED_PACKAGES.includes(providerId);
828
- const hasApiAndEnv = providerInfo.api && providerInfo.env && providerInfo.env.length > 0;
829
- if (isOpenAICompatible || hasInstalledPackage || hasApiAndEnv) {
830
- const modelIds = Object.keys(providerInfo.models).sort();
831
- const url = providerInfo.api || OPENAI_COMPATIBLE_OVERRIDES[normalizedId]?.url;
832
- if (!hasInstalledPackage && !url) {
833
- continue;
834
- }
835
- const apiKeyEnvVar = providerInfo.env?.[0] || `${normalizedId.toUpperCase().replace(/-/g, "_")}_API_KEY`;
836
- const apiKeyHeader = !hasInstalledPackage ? OPENAI_COMPATIBLE_OVERRIDES[normalizedId]?.apiKeyHeader || "Authorization" : void 0;
837
- providerConfigs[normalizedId] = {
838
- url,
839
- apiKeyEnvVar,
840
- apiKeyHeader,
841
- name: providerInfo.name || providerId.charAt(0).toUpperCase() + providerId.slice(1),
842
- models: modelIds,
843
- docUrl: providerInfo.doc,
844
- // Include documentation URL if available
845
- gateway: `models.dev`
846
- };
847
- }
848
- }
849
- this.providerConfigs = providerConfigs;
850
- return providerConfigs;
851
- }
852
- buildUrl(routerId, envVars) {
853
- const { providerId } = parseModelRouterId(routerId);
854
- const config = this.providerConfigs[providerId];
855
- if (!config?.url) {
856
- return;
857
- }
858
- const baseUrlEnvVar = `${providerId.toUpperCase().replace(/-/g, "_")}_BASE_URL`;
859
- const customBaseUrl = envVars?.[baseUrlEnvVar] || process.env[baseUrlEnvVar];
860
- return customBaseUrl || config.url;
861
- }
862
- getApiKey(modelId) {
863
- const [provider, model] = modelId.split("/");
864
- if (!provider || !model) {
865
- throw new Error(`Could not identify provider from model id ${modelId}`);
866
- }
867
- const config = this.providerConfigs[provider];
868
- if (!config) {
869
- throw new Error(`Could not find config for provider ${provider} with model id ${modelId}`);
870
- }
871
- const apiKey = typeof config.apiKeyEnvVar === `string` ? process.env[config.apiKeyEnvVar] : void 0;
872
- if (!apiKey) {
873
- throw new Error(`Could not find API key process.env.${config.apiKeyEnvVar} for model id ${modelId}`);
874
- }
875
- return Promise.resolve(apiKey);
876
- }
877
- async resolveLanguageModel({
878
- modelId,
879
- providerId,
880
- apiKey
881
- }) {
882
- const baseURL = this.buildUrl(`${providerId}/${modelId}`);
883
- switch (providerId) {
884
- case "openai":
885
- return createOpenAI({ apiKey }).responses(modelId);
886
- case "gemini":
887
- case "google":
888
- return createGoogleGenerativeAI({
889
- apiKey
890
- }).chat(modelId);
891
- case "anthropic":
892
- return createAnthropic({ apiKey })(modelId);
893
- case "openrouter":
894
- return createOpenRouter({ apiKey })(modelId);
895
- case "xai":
896
- return createXai({
897
- apiKey
898
- })(modelId);
899
- default:
900
- if (!baseURL) throw new Error(`No API URL found for ${providerId}/${modelId}`);
901
- return createOpenAICompatible({ name: providerId, apiKey, baseURL, supportsStructuredOutputs: true }).chatModel(
902
- modelId
903
- );
904
- }
905
- }
906
- };
907
-
908
- export { ModelsDevGateway, parseModelRouterId };
909
- //# sourceMappingURL=chunk-BNBRQS7N.js.map
910
- //# sourceMappingURL=chunk-BNBRQS7N.js.map