@ai-sdk/xai 4.0.0-beta.7 → 4.0.0-beta.75
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/CHANGELOG.md +660 -9
- package/README.md +2 -0
- package/dist/index.d.ts +213 -68
- package/dist/index.js +2074 -781
- package/dist/index.js.map +1 -1
- package/docs/01-xai.mdx +445 -54
- package/package.json +15 -15
- package/src/convert-to-xai-chat-messages.ts +48 -27
- package/src/convert-xai-chat-usage.ts +3 -3
- package/src/files/xai-files-api.ts +16 -0
- package/src/files/xai-files-options.ts +19 -0
- package/src/files/xai-files.ts +94 -0
- package/src/index.ts +9 -4
- package/src/map-xai-finish-reason.ts +2 -2
- package/src/realtime/index.ts +2 -0
- package/src/realtime/xai-realtime-event-mapper.ts +399 -0
- package/src/realtime/xai-realtime-model-options.ts +3 -0
- package/src/realtime/xai-realtime-model.ts +101 -0
- package/src/remove-additional-properties.ts +24 -0
- package/src/responses/convert-to-xai-responses-input.ts +100 -23
- package/src/responses/convert-xai-responses-usage.ts +3 -3
- package/src/responses/map-xai-responses-finish-reason.ts +3 -2
- package/src/responses/xai-responses-api.ts +31 -1
- package/src/responses/{xai-responses-options.ts → xai-responses-language-model-options.ts} +12 -7
- package/src/responses/xai-responses-language-model.ts +157 -60
- package/src/responses/xai-responses-prepare-tools.ts +10 -8
- package/src/tool/code-execution.ts +2 -2
- package/src/tool/file-search.ts +2 -2
- package/src/tool/mcp-server.ts +2 -2
- package/src/tool/view-image.ts +2 -2
- package/src/tool/view-x-video.ts +2 -2
- package/src/tool/web-search.ts +4 -2
- package/src/tool/x-search.ts +2 -2
- package/src/{xai-chat-options.ts → xai-chat-language-model-options.ts} +28 -13
- package/src/xai-chat-language-model.ts +65 -29
- package/src/xai-chat-prompt.ts +2 -1
- package/src/xai-error.ts +13 -3
- package/src/xai-image-model.ts +28 -11
- package/src/xai-prepare-tools.ts +9 -8
- package/src/xai-provider.ts +115 -19
- package/src/xai-speech-model-options.ts +55 -0
- package/src/xai-speech-model.ts +167 -0
- package/src/xai-transcription-model-options.ts +70 -0
- package/src/xai-transcription-model.ts +166 -0
- package/src/xai-video-model-options.ts +145 -0
- package/src/xai-video-model.ts +129 -22
- package/dist/index.d.mts +0 -377
- package/dist/index.mjs +0 -3070
- package/dist/index.mjs.map +0 -1
- package/src/xai-video-options.ts +0 -23
- /package/src/{xai-image-options.ts → xai-image-model-options.ts} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,50 +1,44 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
VERSION: () => VERSION,
|
|
24
|
-
codeExecution: () => codeExecution,
|
|
25
|
-
createXai: () => createXai,
|
|
26
|
-
mcpServer: () => mcpServer,
|
|
27
|
-
viewImage: () => viewImage,
|
|
28
|
-
viewXVideo: () => viewXVideo,
|
|
29
|
-
webSearch: () => webSearch,
|
|
30
|
-
xSearch: () => xSearch,
|
|
31
|
-
xai: () => xai,
|
|
32
|
-
xaiTools: () => xaiTools
|
|
33
|
-
});
|
|
34
|
-
module.exports = __toCommonJS(src_exports);
|
|
35
|
-
|
|
36
1
|
// src/xai-provider.ts
|
|
37
|
-
|
|
38
|
-
|
|
2
|
+
import {
|
|
3
|
+
NoSuchModelError
|
|
4
|
+
} from "@ai-sdk/provider";
|
|
5
|
+
import {
|
|
6
|
+
generateId,
|
|
7
|
+
loadApiKey,
|
|
8
|
+
withoutTrailingSlash,
|
|
9
|
+
withUserAgentSuffix
|
|
10
|
+
} from "@ai-sdk/provider-utils";
|
|
39
11
|
|
|
40
12
|
// src/xai-chat-language-model.ts
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
13
|
+
import {
|
|
14
|
+
APICallError
|
|
15
|
+
} from "@ai-sdk/provider";
|
|
16
|
+
import {
|
|
17
|
+
combineHeaders,
|
|
18
|
+
createEventSourceResponseHandler,
|
|
19
|
+
createJsonResponseHandler,
|
|
20
|
+
extractResponseHeaders,
|
|
21
|
+
isCustomReasoning,
|
|
22
|
+
mapReasoningToProviderEffort,
|
|
23
|
+
parseProviderOptions,
|
|
24
|
+
postJsonToApi,
|
|
25
|
+
safeParseJSON,
|
|
26
|
+
serializeModelOptions,
|
|
27
|
+
WORKFLOW_SERIALIZE,
|
|
28
|
+
WORKFLOW_DESERIALIZE
|
|
29
|
+
} from "@ai-sdk/provider-utils";
|
|
30
|
+
import { z as z3 } from "zod/v4";
|
|
44
31
|
|
|
45
32
|
// src/convert-to-xai-chat-messages.ts
|
|
46
|
-
|
|
47
|
-
|
|
33
|
+
import {
|
|
34
|
+
UnsupportedFunctionalityError
|
|
35
|
+
} from "@ai-sdk/provider";
|
|
36
|
+
import {
|
|
37
|
+
convertToBase64,
|
|
38
|
+
getTopLevelMediaType,
|
|
39
|
+
resolveFullMediaType,
|
|
40
|
+
resolveProviderReference
|
|
41
|
+
} from "@ai-sdk/provider-utils";
|
|
48
42
|
function convertToXaiChatMessages(prompt) {
|
|
49
43
|
var _a;
|
|
50
44
|
const messages = [];
|
|
@@ -68,18 +62,38 @@ function convertToXaiChatMessages(prompt) {
|
|
|
68
62
|
return { type: "text", text: part.text };
|
|
69
63
|
}
|
|
70
64
|
case "file": {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
65
|
+
switch (part.data.type) {
|
|
66
|
+
case "reference": {
|
|
67
|
+
return {
|
|
68
|
+
type: "file",
|
|
69
|
+
file: {
|
|
70
|
+
file_id: resolveProviderReference({
|
|
71
|
+
reference: part.data.reference,
|
|
72
|
+
provider: "xai"
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
case "text": {
|
|
78
|
+
throw new UnsupportedFunctionalityError({
|
|
79
|
+
functionality: "text file parts"
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
case "url":
|
|
83
|
+
case "data": {
|
|
84
|
+
if (getTopLevelMediaType(part.mediaType) === "image") {
|
|
85
|
+
return {
|
|
86
|
+
type: "image_url",
|
|
87
|
+
image_url: {
|
|
88
|
+
url: part.data.type === "url" ? part.data.url.toString() : `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
} else {
|
|
92
|
+
throw new UnsupportedFunctionalityError({
|
|
93
|
+
functionality: `file part media type ${part.mediaType}`
|
|
94
|
+
});
|
|
77
95
|
}
|
|
78
|
-
}
|
|
79
|
-
} else {
|
|
80
|
-
throw new import_provider.UnsupportedFunctionalityError({
|
|
81
|
-
functionality: `file part media type ${part.mediaType}`
|
|
82
|
-
});
|
|
96
|
+
}
|
|
83
97
|
}
|
|
84
98
|
}
|
|
85
99
|
}
|
|
@@ -129,7 +143,7 @@ function convertToXaiChatMessages(prompt) {
|
|
|
129
143
|
contentValue = output.value;
|
|
130
144
|
break;
|
|
131
145
|
case "execution-denied":
|
|
132
|
-
contentValue = (_a = output.reason) != null ? _a : "Tool execution denied.";
|
|
146
|
+
contentValue = (_a = output.reason) != null ? _a : "Tool call execution denied.";
|
|
133
147
|
break;
|
|
134
148
|
case "content":
|
|
135
149
|
case "json":
|
|
@@ -208,80 +222,103 @@ function mapXaiFinishReason(finishReason) {
|
|
|
208
222
|
}
|
|
209
223
|
}
|
|
210
224
|
|
|
211
|
-
// src/xai-chat-options.ts
|
|
212
|
-
|
|
213
|
-
var webSourceSchema =
|
|
214
|
-
type:
|
|
215
|
-
country:
|
|
216
|
-
excludedWebsites:
|
|
217
|
-
allowedWebsites:
|
|
218
|
-
safeSearch:
|
|
225
|
+
// src/xai-chat-language-model-options.ts
|
|
226
|
+
import { z } from "zod/v4";
|
|
227
|
+
var webSourceSchema = z.object({
|
|
228
|
+
type: z.literal("web"),
|
|
229
|
+
country: z.string().length(2).optional(),
|
|
230
|
+
excludedWebsites: z.array(z.string()).max(5).optional(),
|
|
231
|
+
allowedWebsites: z.array(z.string()).max(5).optional(),
|
|
232
|
+
safeSearch: z.boolean().optional()
|
|
219
233
|
});
|
|
220
|
-
var xSourceSchema =
|
|
221
|
-
type:
|
|
222
|
-
excludedXHandles:
|
|
223
|
-
includedXHandles:
|
|
224
|
-
postFavoriteCount:
|
|
225
|
-
postViewCount:
|
|
234
|
+
var xSourceSchema = z.object({
|
|
235
|
+
type: z.literal("x"),
|
|
236
|
+
excludedXHandles: z.array(z.string()).optional(),
|
|
237
|
+
includedXHandles: z.array(z.string()).optional(),
|
|
238
|
+
postFavoriteCount: z.number().int().optional(),
|
|
239
|
+
postViewCount: z.number().int().optional(),
|
|
226
240
|
/**
|
|
227
241
|
* @deprecated use `includedXHandles` instead
|
|
228
242
|
*/
|
|
229
|
-
xHandles:
|
|
243
|
+
xHandles: z.array(z.string()).optional()
|
|
230
244
|
});
|
|
231
|
-
var newsSourceSchema =
|
|
232
|
-
type:
|
|
233
|
-
country:
|
|
234
|
-
excludedWebsites:
|
|
235
|
-
safeSearch:
|
|
245
|
+
var newsSourceSchema = z.object({
|
|
246
|
+
type: z.literal("news"),
|
|
247
|
+
country: z.string().length(2).optional(),
|
|
248
|
+
excludedWebsites: z.array(z.string()).max(5).optional(),
|
|
249
|
+
safeSearch: z.boolean().optional()
|
|
236
250
|
});
|
|
237
|
-
var rssSourceSchema =
|
|
238
|
-
type:
|
|
239
|
-
links:
|
|
251
|
+
var rssSourceSchema = z.object({
|
|
252
|
+
type: z.literal("rss"),
|
|
253
|
+
links: z.array(z.string().url()).max(1)
|
|
240
254
|
// currently only supports one RSS link
|
|
241
255
|
});
|
|
242
|
-
var searchSourceSchema =
|
|
256
|
+
var searchSourceSchema = z.discriminatedUnion("type", [
|
|
243
257
|
webSourceSchema,
|
|
244
258
|
xSourceSchema,
|
|
245
259
|
newsSourceSchema,
|
|
246
260
|
rssSourceSchema
|
|
247
261
|
]);
|
|
248
|
-
var xaiLanguageModelChatOptions =
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
262
|
+
var xaiLanguageModelChatOptions = z.object({
|
|
263
|
+
/**
|
|
264
|
+
* Constrains how hard a reasoning model thinks before responding.
|
|
265
|
+
*
|
|
266
|
+
* - `none`: Disables reasoning entirely (supported by `grok-4.3` and newer
|
|
267
|
+
* reasoning models). When set, no thinking tokens are used.
|
|
268
|
+
* - `low` (default): Uses some reasoning tokens, but still fast.
|
|
269
|
+
* - `medium`: More thinking for less-latency-sensitive applications.
|
|
270
|
+
* - `high`: Uses more reasoning tokens for deeper thinking.
|
|
271
|
+
*
|
|
272
|
+
* Note: Not every Grok model accepts every value. Refer to xAI's docs for
|
|
273
|
+
* the values supported by your selected model.
|
|
274
|
+
*
|
|
275
|
+
* @see https://docs.x.ai/docs/guides/reasoning
|
|
276
|
+
*/
|
|
277
|
+
reasoningEffort: z.enum(["none", "low", "medium", "high"]).optional(),
|
|
278
|
+
logprobs: z.boolean().optional(),
|
|
279
|
+
topLogprobs: z.number().int().min(0).max(8).optional(),
|
|
252
280
|
/**
|
|
253
281
|
* Whether to enable parallel function calling during tool use.
|
|
254
282
|
* When true, the model can call multiple functions in parallel.
|
|
255
283
|
* When false, the model will call functions sequentially.
|
|
256
284
|
* Defaults to true.
|
|
257
285
|
*/
|
|
258
|
-
parallel_function_calling:
|
|
259
|
-
|
|
286
|
+
parallel_function_calling: z.boolean().optional(),
|
|
287
|
+
/**
|
|
288
|
+
* @deprecated xAI has deprecated Live Search (`search_parameters`) in favor
|
|
289
|
+
* of the Agent Tools API. Requests using this option now return a "Live
|
|
290
|
+
* search is deprecated" error. Use the `web_search` / `x_search` tools
|
|
291
|
+
* instead (e.g. `xai.tools.webSearch()`, `xai.tools.xSearch()`) with
|
|
292
|
+
* `xai.responses(modelId)`.
|
|
293
|
+
*
|
|
294
|
+
* @see https://docs.x.ai/docs/guides/tools/overview
|
|
295
|
+
*/
|
|
296
|
+
searchParameters: z.object({
|
|
260
297
|
/**
|
|
261
298
|
* search mode preference
|
|
262
299
|
* - "off": disables search completely
|
|
263
300
|
* - "auto": model decides whether to search (default)
|
|
264
301
|
* - "on": always enables search
|
|
265
302
|
*/
|
|
266
|
-
mode:
|
|
303
|
+
mode: z.enum(["off", "auto", "on"]),
|
|
267
304
|
/**
|
|
268
305
|
* whether to return citations in the response
|
|
269
306
|
* defaults to true
|
|
270
307
|
*/
|
|
271
|
-
returnCitations:
|
|
308
|
+
returnCitations: z.boolean().optional(),
|
|
272
309
|
/**
|
|
273
310
|
* start date for search data (ISO8601 format: YYYY-MM-DD)
|
|
274
311
|
*/
|
|
275
|
-
fromDate:
|
|
312
|
+
fromDate: z.string().optional(),
|
|
276
313
|
/**
|
|
277
314
|
* end date for search data (ISO8601 format: YYYY-MM-DD)
|
|
278
315
|
*/
|
|
279
|
-
toDate:
|
|
316
|
+
toDate: z.string().optional(),
|
|
280
317
|
/**
|
|
281
318
|
* maximum number of search results to consider
|
|
282
319
|
* defaults to 20
|
|
283
320
|
*/
|
|
284
|
-
maxSearchResults:
|
|
321
|
+
maxSearchResults: z.number().min(1).max(50).optional(),
|
|
285
322
|
/**
|
|
286
323
|
* data sources to search from.
|
|
287
324
|
* defaults to [{ type: 'web' }, { type: 'x' }] if not specified.
|
|
@@ -289,28 +326,58 @@ var xaiLanguageModelChatOptions = import_v4.z.object({
|
|
|
289
326
|
* @example
|
|
290
327
|
* sources: [{ type: 'web', country: 'US' }, { type: 'x' }]
|
|
291
328
|
*/
|
|
292
|
-
sources:
|
|
329
|
+
sources: z.array(searchSourceSchema).optional()
|
|
293
330
|
}).optional()
|
|
294
331
|
});
|
|
295
332
|
|
|
296
333
|
// src/xai-error.ts
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
var
|
|
300
|
-
error:
|
|
301
|
-
message:
|
|
302
|
-
type:
|
|
303
|
-
param:
|
|
304
|
-
code:
|
|
334
|
+
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
335
|
+
import { z as z2 } from "zod/v4";
|
|
336
|
+
var chatCompletionsErrorSchema = z2.object({
|
|
337
|
+
error: z2.object({
|
|
338
|
+
message: z2.string(),
|
|
339
|
+
type: z2.string().nullish(),
|
|
340
|
+
param: z2.any().nullish(),
|
|
341
|
+
code: z2.union([z2.string(), z2.number()]).nullish()
|
|
305
342
|
})
|
|
306
343
|
});
|
|
307
|
-
var
|
|
344
|
+
var responsesErrorSchema = z2.object({
|
|
345
|
+
code: z2.string(),
|
|
346
|
+
error: z2.string()
|
|
347
|
+
});
|
|
348
|
+
var xaiErrorDataSchema = z2.union([
|
|
349
|
+
chatCompletionsErrorSchema,
|
|
350
|
+
responsesErrorSchema
|
|
351
|
+
]);
|
|
352
|
+
var xaiFailedResponseHandler = createJsonErrorResponseHandler({
|
|
308
353
|
errorSchema: xaiErrorDataSchema,
|
|
309
|
-
errorToMessage: (data) => data.error.message
|
|
354
|
+
errorToMessage: (data) => "code" in data ? `${data.code}: ${data.error}` : data.error.message
|
|
310
355
|
});
|
|
311
356
|
|
|
312
357
|
// src/xai-prepare-tools.ts
|
|
313
|
-
|
|
358
|
+
import {
|
|
359
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
360
|
+
} from "@ai-sdk/provider";
|
|
361
|
+
|
|
362
|
+
// src/remove-additional-properties.ts
|
|
363
|
+
function removeAdditionalPropertiesFalse(value) {
|
|
364
|
+
if (Array.isArray(value)) {
|
|
365
|
+
return value.map(removeAdditionalPropertiesFalse);
|
|
366
|
+
}
|
|
367
|
+
if (value == null || typeof value !== "object") {
|
|
368
|
+
return value;
|
|
369
|
+
}
|
|
370
|
+
const result = {};
|
|
371
|
+
for (const [key, propertyValue] of Object.entries(value)) {
|
|
372
|
+
if (key === "additionalProperties" && propertyValue === false) {
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
result[key] = removeAdditionalPropertiesFalse(propertyValue);
|
|
376
|
+
}
|
|
377
|
+
return result;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// src/xai-prepare-tools.ts
|
|
314
381
|
function prepareTools({
|
|
315
382
|
tools,
|
|
316
383
|
toolChoice
|
|
@@ -333,7 +400,7 @@ function prepareTools({
|
|
|
333
400
|
function: {
|
|
334
401
|
name: tool.name,
|
|
335
402
|
description: tool.description,
|
|
336
|
-
parameters: tool.inputSchema,
|
|
403
|
+
parameters: removeAdditionalPropertiesFalse(tool.inputSchema),
|
|
337
404
|
...tool.strict != null ? { strict: tool.strict } : {}
|
|
338
405
|
}
|
|
339
406
|
});
|
|
@@ -360,7 +427,7 @@ function prepareTools({
|
|
|
360
427
|
};
|
|
361
428
|
default: {
|
|
362
429
|
const _exhaustiveCheck = type;
|
|
363
|
-
throw new
|
|
430
|
+
throw new UnsupportedFunctionalityError2({
|
|
364
431
|
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
365
432
|
});
|
|
366
433
|
}
|
|
@@ -368,15 +435,24 @@ function prepareTools({
|
|
|
368
435
|
}
|
|
369
436
|
|
|
370
437
|
// src/xai-chat-language-model.ts
|
|
371
|
-
var XaiChatLanguageModel = class {
|
|
438
|
+
var XaiChatLanguageModel = class _XaiChatLanguageModel {
|
|
372
439
|
constructor(modelId, config) {
|
|
373
|
-
this.specificationVersion = "
|
|
440
|
+
this.specificationVersion = "v4";
|
|
374
441
|
this.supportedUrls = {
|
|
375
442
|
"image/*": [/^https?:\/\/.*$/]
|
|
376
443
|
};
|
|
377
444
|
this.modelId = modelId;
|
|
378
445
|
this.config = config;
|
|
379
446
|
}
|
|
447
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
448
|
+
return serializeModelOptions({
|
|
449
|
+
modelId: model.modelId,
|
|
450
|
+
config: model.config
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
454
|
+
return new _XaiChatLanguageModel(options.modelId, options.config);
|
|
455
|
+
}
|
|
380
456
|
get provider() {
|
|
381
457
|
return this.config.provider;
|
|
382
458
|
}
|
|
@@ -390,14 +466,15 @@ var XaiChatLanguageModel = class {
|
|
|
390
466
|
presencePenalty,
|
|
391
467
|
stopSequences,
|
|
392
468
|
seed,
|
|
469
|
+
reasoning,
|
|
393
470
|
responseFormat,
|
|
394
471
|
providerOptions,
|
|
395
472
|
tools,
|
|
396
473
|
toolChoice
|
|
397
474
|
}) {
|
|
398
|
-
var _a, _b, _c;
|
|
475
|
+
var _a, _b, _c, _d;
|
|
399
476
|
const warnings = [];
|
|
400
|
-
const options = (_a = await
|
|
477
|
+
const options = (_a = await parseProviderOptions({
|
|
401
478
|
provider: "xai",
|
|
402
479
|
providerOptions,
|
|
403
480
|
schema: xaiLanguageModelChatOptions
|
|
@@ -435,14 +512,24 @@ var XaiChatLanguageModel = class {
|
|
|
435
512
|
temperature,
|
|
436
513
|
top_p: topP,
|
|
437
514
|
seed,
|
|
438
|
-
reasoning_effort: options.reasoningEffort
|
|
515
|
+
reasoning_effort: (_b = options.reasoningEffort) != null ? _b : isCustomReasoning(reasoning) ? reasoning === "none" ? void 0 : mapReasoningToProviderEffort({
|
|
516
|
+
reasoning,
|
|
517
|
+
effortMap: {
|
|
518
|
+
minimal: "low",
|
|
519
|
+
low: "low",
|
|
520
|
+
medium: "medium",
|
|
521
|
+
high: "high",
|
|
522
|
+
xhigh: "high"
|
|
523
|
+
},
|
|
524
|
+
warnings
|
|
525
|
+
}) : void 0,
|
|
439
526
|
// parallel function calling
|
|
440
527
|
parallel_function_calling: options.parallel_function_calling,
|
|
441
528
|
// response format
|
|
442
529
|
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? responseFormat.schema != null ? {
|
|
443
530
|
type: "json_schema",
|
|
444
531
|
json_schema: {
|
|
445
|
-
name: (
|
|
532
|
+
name: (_c = responseFormat.name) != null ? _c : "response",
|
|
446
533
|
schema: responseFormat.schema,
|
|
447
534
|
strict: true
|
|
448
535
|
}
|
|
@@ -454,7 +541,7 @@ var XaiChatLanguageModel = class {
|
|
|
454
541
|
from_date: options.searchParameters.fromDate,
|
|
455
542
|
to_date: options.searchParameters.toDate,
|
|
456
543
|
max_search_results: options.searchParameters.maxSearchResults,
|
|
457
|
-
sources: (
|
|
544
|
+
sources: (_d = options.searchParameters.sources) == null ? void 0 : _d.map((source) => {
|
|
458
545
|
var _a2;
|
|
459
546
|
return {
|
|
460
547
|
type: source.type,
|
|
@@ -493,26 +580,26 @@ var XaiChatLanguageModel = class {
|
|
|
493
580
|
};
|
|
494
581
|
}
|
|
495
582
|
async doGenerate(options) {
|
|
496
|
-
var _a, _b;
|
|
583
|
+
var _a, _b, _c, _d;
|
|
497
584
|
const { args: body, warnings } = await this.getArgs(options);
|
|
498
585
|
const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
|
|
499
586
|
const {
|
|
500
587
|
responseHeaders,
|
|
501
588
|
value: response,
|
|
502
589
|
rawValue: rawResponse
|
|
503
|
-
} = await
|
|
590
|
+
} = await postJsonToApi({
|
|
504
591
|
url,
|
|
505
|
-
headers: (
|
|
592
|
+
headers: combineHeaders((_c = (_b = this.config).headers) == null ? void 0 : _c.call(_b), options.headers),
|
|
506
593
|
body,
|
|
507
594
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
508
|
-
successfulResponseHandler:
|
|
595
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
509
596
|
xaiChatResponseSchema
|
|
510
597
|
),
|
|
511
598
|
abortSignal: options.abortSignal,
|
|
512
599
|
fetch: this.config.fetch
|
|
513
600
|
});
|
|
514
601
|
if (response.error != null) {
|
|
515
|
-
throw new
|
|
602
|
+
throw new APICallError({
|
|
516
603
|
message: response.error,
|
|
517
604
|
url,
|
|
518
605
|
requestBodyValues: body,
|
|
@@ -564,7 +651,7 @@ var XaiChatLanguageModel = class {
|
|
|
564
651
|
content,
|
|
565
652
|
finishReason: {
|
|
566
653
|
unified: mapXaiFinishReason(choice.finish_reason),
|
|
567
|
-
raw: (
|
|
654
|
+
raw: (_d = choice.finish_reason) != null ? _d : void 0
|
|
568
655
|
},
|
|
569
656
|
usage: response.usage ? convertXaiChatUsage(response.usage) : {
|
|
570
657
|
inputTokens: { total: 0, noCache: 0, cacheRead: 0, cacheWrite: 0 },
|
|
@@ -580,7 +667,7 @@ var XaiChatLanguageModel = class {
|
|
|
580
667
|
};
|
|
581
668
|
}
|
|
582
669
|
async doStream(options) {
|
|
583
|
-
var _a;
|
|
670
|
+
var _a, _b, _c;
|
|
584
671
|
const { args, warnings } = await this.getArgs(options);
|
|
585
672
|
const body = {
|
|
586
673
|
...args,
|
|
@@ -590,22 +677,22 @@ var XaiChatLanguageModel = class {
|
|
|
590
677
|
}
|
|
591
678
|
};
|
|
592
679
|
const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
|
|
593
|
-
const { responseHeaders, value: response } = await
|
|
680
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
594
681
|
url,
|
|
595
|
-
headers: (
|
|
682
|
+
headers: combineHeaders((_c = (_b = this.config).headers) == null ? void 0 : _c.call(_b), options.headers),
|
|
596
683
|
body,
|
|
597
684
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
598
685
|
successfulResponseHandler: async ({ response: response2 }) => {
|
|
599
|
-
const responseHeaders2 =
|
|
686
|
+
const responseHeaders2 = extractResponseHeaders(response2);
|
|
600
687
|
const contentType = response2.headers.get("content-type");
|
|
601
688
|
if (contentType == null ? void 0 : contentType.includes("application/json")) {
|
|
602
689
|
const responseBody = await response2.text();
|
|
603
|
-
const parsedError = await
|
|
690
|
+
const parsedError = await safeParseJSON({
|
|
604
691
|
text: responseBody,
|
|
605
692
|
schema: xaiStreamErrorSchema
|
|
606
693
|
});
|
|
607
694
|
if (parsedError.success) {
|
|
608
|
-
throw new
|
|
695
|
+
throw new APICallError({
|
|
609
696
|
message: parsedError.value.error,
|
|
610
697
|
url,
|
|
611
698
|
requestBodyValues: body,
|
|
@@ -615,7 +702,7 @@ var XaiChatLanguageModel = class {
|
|
|
615
702
|
isRetryable: parsedError.value.code === "The service is currently unavailable"
|
|
616
703
|
});
|
|
617
704
|
}
|
|
618
|
-
throw new
|
|
705
|
+
throw new APICallError({
|
|
619
706
|
message: "Invalid JSON response",
|
|
620
707
|
url,
|
|
621
708
|
requestBodyValues: body,
|
|
@@ -624,7 +711,7 @@ var XaiChatLanguageModel = class {
|
|
|
624
711
|
responseBody
|
|
625
712
|
});
|
|
626
713
|
}
|
|
627
|
-
return
|
|
714
|
+
return createEventSourceResponseHandler(xaiChatChunkSchema)({
|
|
628
715
|
response: response2,
|
|
629
716
|
url,
|
|
630
717
|
requestBodyValues: body
|
|
@@ -802,113 +889,134 @@ var XaiChatLanguageModel = class {
|
|
|
802
889
|
};
|
|
803
890
|
}
|
|
804
891
|
};
|
|
805
|
-
var xaiUsageSchema =
|
|
806
|
-
prompt_tokens:
|
|
807
|
-
completion_tokens:
|
|
808
|
-
total_tokens:
|
|
809
|
-
prompt_tokens_details:
|
|
810
|
-
text_tokens:
|
|
811
|
-
audio_tokens:
|
|
812
|
-
image_tokens:
|
|
813
|
-
cached_tokens:
|
|
892
|
+
var xaiUsageSchema = z3.object({
|
|
893
|
+
prompt_tokens: z3.number(),
|
|
894
|
+
completion_tokens: z3.number(),
|
|
895
|
+
total_tokens: z3.number(),
|
|
896
|
+
prompt_tokens_details: z3.object({
|
|
897
|
+
text_tokens: z3.number().nullish(),
|
|
898
|
+
audio_tokens: z3.number().nullish(),
|
|
899
|
+
image_tokens: z3.number().nullish(),
|
|
900
|
+
cached_tokens: z3.number().nullish()
|
|
814
901
|
}).nullish(),
|
|
815
|
-
completion_tokens_details:
|
|
816
|
-
reasoning_tokens:
|
|
817
|
-
audio_tokens:
|
|
818
|
-
accepted_prediction_tokens:
|
|
819
|
-
rejected_prediction_tokens:
|
|
902
|
+
completion_tokens_details: z3.object({
|
|
903
|
+
reasoning_tokens: z3.number().nullish(),
|
|
904
|
+
audio_tokens: z3.number().nullish(),
|
|
905
|
+
accepted_prediction_tokens: z3.number().nullish(),
|
|
906
|
+
rejected_prediction_tokens: z3.number().nullish()
|
|
820
907
|
}).nullish()
|
|
821
908
|
});
|
|
822
|
-
var xaiChatResponseSchema =
|
|
823
|
-
id:
|
|
824
|
-
created:
|
|
825
|
-
model:
|
|
826
|
-
choices:
|
|
827
|
-
|
|
828
|
-
message:
|
|
829
|
-
role:
|
|
830
|
-
content:
|
|
831
|
-
reasoning_content:
|
|
832
|
-
tool_calls:
|
|
833
|
-
|
|
834
|
-
id:
|
|
835
|
-
type:
|
|
836
|
-
function:
|
|
837
|
-
name:
|
|
838
|
-
arguments:
|
|
909
|
+
var xaiChatResponseSchema = z3.object({
|
|
910
|
+
id: z3.string().nullish(),
|
|
911
|
+
created: z3.number().nullish(),
|
|
912
|
+
model: z3.string().nullish(),
|
|
913
|
+
choices: z3.array(
|
|
914
|
+
z3.object({
|
|
915
|
+
message: z3.object({
|
|
916
|
+
role: z3.literal("assistant"),
|
|
917
|
+
content: z3.string().nullish(),
|
|
918
|
+
reasoning_content: z3.string().nullish(),
|
|
919
|
+
tool_calls: z3.array(
|
|
920
|
+
z3.object({
|
|
921
|
+
id: z3.string(),
|
|
922
|
+
type: z3.literal("function"),
|
|
923
|
+
function: z3.object({
|
|
924
|
+
name: z3.string(),
|
|
925
|
+
arguments: z3.string()
|
|
839
926
|
})
|
|
840
927
|
})
|
|
841
928
|
).nullish()
|
|
842
929
|
}),
|
|
843
|
-
index:
|
|
844
|
-
finish_reason:
|
|
930
|
+
index: z3.number(),
|
|
931
|
+
finish_reason: z3.string().nullish()
|
|
845
932
|
})
|
|
846
933
|
).nullish(),
|
|
847
|
-
object:
|
|
934
|
+
object: z3.literal("chat.completion").nullish(),
|
|
848
935
|
usage: xaiUsageSchema.nullish(),
|
|
849
|
-
citations:
|
|
850
|
-
code:
|
|
851
|
-
error:
|
|
936
|
+
citations: z3.array(z3.string().url()).nullish(),
|
|
937
|
+
code: z3.string().nullish(),
|
|
938
|
+
error: z3.string().nullish()
|
|
852
939
|
});
|
|
853
|
-
var xaiChatChunkSchema =
|
|
854
|
-
id:
|
|
855
|
-
created:
|
|
856
|
-
model:
|
|
857
|
-
choices:
|
|
858
|
-
|
|
859
|
-
delta:
|
|
860
|
-
role:
|
|
861
|
-
content:
|
|
862
|
-
reasoning_content:
|
|
863
|
-
tool_calls:
|
|
864
|
-
|
|
865
|
-
id:
|
|
866
|
-
type:
|
|
867
|
-
function:
|
|
868
|
-
name:
|
|
869
|
-
arguments:
|
|
940
|
+
var xaiChatChunkSchema = z3.object({
|
|
941
|
+
id: z3.string().nullish(),
|
|
942
|
+
created: z3.number().nullish(),
|
|
943
|
+
model: z3.string().nullish(),
|
|
944
|
+
choices: z3.array(
|
|
945
|
+
z3.object({
|
|
946
|
+
delta: z3.object({
|
|
947
|
+
role: z3.enum(["assistant"]).optional(),
|
|
948
|
+
content: z3.string().nullish(),
|
|
949
|
+
reasoning_content: z3.string().nullish(),
|
|
950
|
+
tool_calls: z3.array(
|
|
951
|
+
z3.object({
|
|
952
|
+
id: z3.string(),
|
|
953
|
+
type: z3.literal("function"),
|
|
954
|
+
function: z3.object({
|
|
955
|
+
name: z3.string(),
|
|
956
|
+
arguments: z3.string()
|
|
870
957
|
})
|
|
871
958
|
})
|
|
872
959
|
).nullish()
|
|
873
960
|
}),
|
|
874
|
-
finish_reason:
|
|
875
|
-
index:
|
|
961
|
+
finish_reason: z3.string().nullish(),
|
|
962
|
+
index: z3.number()
|
|
876
963
|
})
|
|
877
964
|
),
|
|
878
965
|
usage: xaiUsageSchema.nullish(),
|
|
879
|
-
citations:
|
|
966
|
+
citations: z3.array(z3.string().url()).nullish()
|
|
880
967
|
});
|
|
881
|
-
var xaiStreamErrorSchema =
|
|
882
|
-
code:
|
|
883
|
-
error:
|
|
968
|
+
var xaiStreamErrorSchema = z3.object({
|
|
969
|
+
code: z3.string(),
|
|
970
|
+
error: z3.string()
|
|
884
971
|
});
|
|
885
972
|
|
|
886
973
|
// src/xai-image-model.ts
|
|
887
|
-
|
|
888
|
-
|
|
974
|
+
import {
|
|
975
|
+
combineHeaders as combineHeaders2,
|
|
976
|
+
convertImageModelFileToDataUri,
|
|
977
|
+
createBinaryResponseHandler,
|
|
978
|
+
createJsonResponseHandler as createJsonResponseHandler2,
|
|
979
|
+
createStatusCodeErrorResponseHandler,
|
|
980
|
+
getFromApi,
|
|
981
|
+
parseProviderOptions as parseProviderOptions2,
|
|
982
|
+
postJsonToApi as postJsonToApi2,
|
|
983
|
+
serializeModelOptions as serializeModelOptions2,
|
|
984
|
+
WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE2,
|
|
985
|
+
WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE2
|
|
986
|
+
} from "@ai-sdk/provider-utils";
|
|
987
|
+
import { z as z5 } from "zod/v4";
|
|
889
988
|
|
|
890
|
-
// src/xai-image-options.ts
|
|
891
|
-
|
|
892
|
-
var xaiImageModelOptions =
|
|
893
|
-
aspect_ratio:
|
|
894
|
-
output_format:
|
|
895
|
-
sync_mode:
|
|
896
|
-
resolution:
|
|
897
|
-
quality:
|
|
898
|
-
user:
|
|
989
|
+
// src/xai-image-model-options.ts
|
|
990
|
+
import { z as z4 } from "zod/v4";
|
|
991
|
+
var xaiImageModelOptions = z4.object({
|
|
992
|
+
aspect_ratio: z4.string().optional(),
|
|
993
|
+
output_format: z4.string().optional(),
|
|
994
|
+
sync_mode: z4.boolean().optional(),
|
|
995
|
+
resolution: z4.enum(["1k", "2k"]).optional(),
|
|
996
|
+
quality: z4.enum(["low", "medium", "high"]).optional(),
|
|
997
|
+
user: z4.string().optional()
|
|
899
998
|
});
|
|
900
999
|
|
|
901
1000
|
// src/xai-image-model.ts
|
|
902
|
-
var XaiImageModel = class {
|
|
1001
|
+
var XaiImageModel = class _XaiImageModel {
|
|
903
1002
|
constructor(modelId, config) {
|
|
904
1003
|
this.modelId = modelId;
|
|
905
1004
|
this.config = config;
|
|
906
|
-
this.specificationVersion = "
|
|
1005
|
+
this.specificationVersion = "v4";
|
|
907
1006
|
this.maxImagesPerCall = 3;
|
|
908
1007
|
}
|
|
909
1008
|
get provider() {
|
|
910
1009
|
return this.config.provider;
|
|
911
1010
|
}
|
|
1011
|
+
static [WORKFLOW_SERIALIZE2](model) {
|
|
1012
|
+
return serializeModelOptions2({
|
|
1013
|
+
modelId: model.modelId,
|
|
1014
|
+
config: model.config
|
|
1015
|
+
});
|
|
1016
|
+
}
|
|
1017
|
+
static [WORKFLOW_DESERIALIZE2](options) {
|
|
1018
|
+
return new _XaiImageModel(options.modelId, options.config);
|
|
1019
|
+
}
|
|
912
1020
|
async doGenerate({
|
|
913
1021
|
prompt,
|
|
914
1022
|
n,
|
|
@@ -921,7 +1029,7 @@ var XaiImageModel = class {
|
|
|
921
1029
|
files,
|
|
922
1030
|
mask
|
|
923
1031
|
}) {
|
|
924
|
-
var _a, _b, _c, _d, _e;
|
|
1032
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
925
1033
|
const warnings = [];
|
|
926
1034
|
if (size != null) {
|
|
927
1035
|
warnings.push({
|
|
@@ -942,13 +1050,13 @@ var XaiImageModel = class {
|
|
|
942
1050
|
feature: "mask"
|
|
943
1051
|
});
|
|
944
1052
|
}
|
|
945
|
-
const xaiOptions = await (
|
|
1053
|
+
const xaiOptions = await parseProviderOptions2({
|
|
946
1054
|
provider: "xai",
|
|
947
1055
|
providerOptions,
|
|
948
1056
|
schema: xaiImageModelOptions
|
|
949
1057
|
});
|
|
950
1058
|
const hasFiles = files != null && files.length > 0;
|
|
951
|
-
const imageUrls = hasFiles ? files.map((file) =>
|
|
1059
|
+
const imageUrls = hasFiles ? files.map((file) => convertImageModelFileToDataUri(file)) : [];
|
|
952
1060
|
const endpoint = hasFiles ? "/images/edits" : "/images/generations";
|
|
953
1061
|
const body = {
|
|
954
1062
|
model: this.modelId,
|
|
@@ -984,12 +1092,12 @@ var XaiImageModel = class {
|
|
|
984
1092
|
}
|
|
985
1093
|
const baseURL = (_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1";
|
|
986
1094
|
const currentDate = (_d = (_c = (_b = this.config._internal) == null ? void 0 : _b.currentDate) == null ? void 0 : _c.call(_b)) != null ? _d : /* @__PURE__ */ new Date();
|
|
987
|
-
const { value: response, responseHeaders } = await (
|
|
1095
|
+
const { value: response, responseHeaders } = await postJsonToApi2({
|
|
988
1096
|
url: `${baseURL}${endpoint}`,
|
|
989
|
-
headers: (
|
|
1097
|
+
headers: combineHeaders2((_f = (_e = this.config).headers) == null ? void 0 : _f.call(_e), headers),
|
|
990
1098
|
body,
|
|
991
1099
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
992
|
-
successfulResponseHandler: (
|
|
1100
|
+
successfulResponseHandler: createJsonResponseHandler2(
|
|
993
1101
|
xaiImageResponseSchema
|
|
994
1102
|
),
|
|
995
1103
|
abortSignal,
|
|
@@ -1014,45 +1122,63 @@ var XaiImageModel = class {
|
|
|
1014
1122
|
images: response.data.map((item) => ({
|
|
1015
1123
|
...item.revised_prompt ? { revisedPrompt: item.revised_prompt } : {}
|
|
1016
1124
|
})),
|
|
1017
|
-
...((
|
|
1125
|
+
...((_g = response.usage) == null ? void 0 : _g.cost_in_usd_ticks) != null ? { costInUsdTicks: response.usage.cost_in_usd_ticks } : {}
|
|
1018
1126
|
}
|
|
1019
1127
|
}
|
|
1020
1128
|
};
|
|
1021
1129
|
}
|
|
1022
1130
|
async downloadImage(url, abortSignal) {
|
|
1023
|
-
const { value } = await
|
|
1131
|
+
const { value } = await getFromApi({
|
|
1024
1132
|
url,
|
|
1025
1133
|
abortSignal,
|
|
1026
|
-
failedResponseHandler:
|
|
1027
|
-
successfulResponseHandler:
|
|
1134
|
+
failedResponseHandler: createStatusCodeErrorResponseHandler(),
|
|
1135
|
+
successfulResponseHandler: createBinaryResponseHandler(),
|
|
1028
1136
|
fetch: this.config.fetch
|
|
1029
1137
|
});
|
|
1030
1138
|
return value;
|
|
1031
1139
|
}
|
|
1032
1140
|
};
|
|
1033
|
-
var xaiImageResponseSchema =
|
|
1034
|
-
data:
|
|
1035
|
-
|
|
1036
|
-
url:
|
|
1037
|
-
b64_json:
|
|
1038
|
-
revised_prompt:
|
|
1141
|
+
var xaiImageResponseSchema = z5.object({
|
|
1142
|
+
data: z5.array(
|
|
1143
|
+
z5.object({
|
|
1144
|
+
url: z5.string().nullish(),
|
|
1145
|
+
b64_json: z5.string().nullish(),
|
|
1146
|
+
revised_prompt: z5.string().nullish()
|
|
1039
1147
|
})
|
|
1040
1148
|
),
|
|
1041
|
-
usage:
|
|
1042
|
-
cost_in_usd_ticks:
|
|
1149
|
+
usage: z5.object({
|
|
1150
|
+
cost_in_usd_ticks: z5.number().nullish()
|
|
1043
1151
|
}).nullish()
|
|
1044
1152
|
});
|
|
1045
1153
|
|
|
1046
1154
|
// src/responses/xai-responses-language-model.ts
|
|
1047
|
-
|
|
1155
|
+
import {
|
|
1156
|
+
combineHeaders as combineHeaders3,
|
|
1157
|
+
createEventSourceResponseHandler as createEventSourceResponseHandler2,
|
|
1158
|
+
createJsonResponseHandler as createJsonResponseHandler3,
|
|
1159
|
+
isCustomReasoning as isCustomReasoning2,
|
|
1160
|
+
mapReasoningToProviderEffort as mapReasoningToProviderEffort2,
|
|
1161
|
+
parseProviderOptions as parseProviderOptions3,
|
|
1162
|
+
postJsonToApi as postJsonToApi3,
|
|
1163
|
+
serializeModelOptions as serializeModelOptions3,
|
|
1164
|
+
WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE3,
|
|
1165
|
+
WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE3
|
|
1166
|
+
} from "@ai-sdk/provider-utils";
|
|
1048
1167
|
|
|
1049
1168
|
// src/responses/convert-to-xai-responses-input.ts
|
|
1050
|
-
|
|
1051
|
-
|
|
1169
|
+
import {
|
|
1170
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
1171
|
+
} from "@ai-sdk/provider";
|
|
1172
|
+
import {
|
|
1173
|
+
convertToBase64 as convertToBase642,
|
|
1174
|
+
getTopLevelMediaType as getTopLevelMediaType2,
|
|
1175
|
+
resolveFullMediaType as resolveFullMediaType2,
|
|
1176
|
+
resolveProviderReference as resolveProviderReference2
|
|
1177
|
+
} from "@ai-sdk/provider-utils";
|
|
1052
1178
|
async function convertToXaiResponsesInput({
|
|
1053
1179
|
prompt
|
|
1054
1180
|
}) {
|
|
1055
|
-
var _a, _b, _c, _d, _e;
|
|
1181
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1056
1182
|
const input = [];
|
|
1057
1183
|
const inputWarnings = [];
|
|
1058
1184
|
for (const message of prompt) {
|
|
@@ -1073,14 +1199,42 @@ async function convertToXaiResponsesInput({
|
|
|
1073
1199
|
break;
|
|
1074
1200
|
}
|
|
1075
1201
|
case "file": {
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1202
|
+
switch (block.data.type) {
|
|
1203
|
+
case "reference": {
|
|
1204
|
+
contentParts.push({
|
|
1205
|
+
type: "input_file",
|
|
1206
|
+
file_id: resolveProviderReference2({
|
|
1207
|
+
reference: block.data.reference,
|
|
1208
|
+
provider: "xai"
|
|
1209
|
+
})
|
|
1210
|
+
});
|
|
1211
|
+
break;
|
|
1212
|
+
}
|
|
1213
|
+
case "text": {
|
|
1214
|
+
throw new UnsupportedFunctionalityError3({
|
|
1215
|
+
functionality: "text file parts"
|
|
1216
|
+
});
|
|
1217
|
+
}
|
|
1218
|
+
case "url":
|
|
1219
|
+
case "data": {
|
|
1220
|
+
if (getTopLevelMediaType2(block.mediaType) === "image") {
|
|
1221
|
+
const imageUrl = block.data.type === "url" ? block.data.url.toString() : `data:${resolveFullMediaType2({ part: block })};base64,${convertToBase642(block.data.data)}`;
|
|
1222
|
+
contentParts.push({
|
|
1223
|
+
type: "input_image",
|
|
1224
|
+
image_url: imageUrl
|
|
1225
|
+
});
|
|
1226
|
+
} else if (block.data.type === "url") {
|
|
1227
|
+
contentParts.push({
|
|
1228
|
+
type: "input_file",
|
|
1229
|
+
file_url: block.data.url.toString()
|
|
1230
|
+
});
|
|
1231
|
+
} else {
|
|
1232
|
+
throw new UnsupportedFunctionalityError3({
|
|
1233
|
+
functionality: `file part media type ${block.mediaType} as inline data (xAI Responses requires a URL or a Files API reference for non-image files)`
|
|
1234
|
+
});
|
|
1235
|
+
}
|
|
1236
|
+
break;
|
|
1237
|
+
}
|
|
1084
1238
|
}
|
|
1085
1239
|
break;
|
|
1086
1240
|
}
|
|
@@ -1129,7 +1283,36 @@ async function convertToXaiResponsesInput({
|
|
|
1129
1283
|
case "tool-result": {
|
|
1130
1284
|
break;
|
|
1131
1285
|
}
|
|
1132
|
-
case "reasoning":
|
|
1286
|
+
case "reasoning": {
|
|
1287
|
+
const itemId = typeof ((_f = (_e = part.providerOptions) == null ? void 0 : _e.xai) == null ? void 0 : _f.itemId) === "string" ? part.providerOptions.xai.itemId : void 0;
|
|
1288
|
+
const encryptedContent = typeof ((_h = (_g = part.providerOptions) == null ? void 0 : _g.xai) == null ? void 0 : _h.reasoningEncryptedContent) === "string" ? part.providerOptions.xai.reasoningEncryptedContent : void 0;
|
|
1289
|
+
if (itemId != null || encryptedContent != null) {
|
|
1290
|
+
const summaryParts = [];
|
|
1291
|
+
if (part.text.length > 0) {
|
|
1292
|
+
summaryParts.push({
|
|
1293
|
+
type: "summary_text",
|
|
1294
|
+
text: part.text
|
|
1295
|
+
});
|
|
1296
|
+
}
|
|
1297
|
+
input.push({
|
|
1298
|
+
type: "reasoning",
|
|
1299
|
+
id: itemId != null ? itemId : "",
|
|
1300
|
+
summary: summaryParts,
|
|
1301
|
+
status: "completed",
|
|
1302
|
+
...encryptedContent != null && {
|
|
1303
|
+
encrypted_content: encryptedContent
|
|
1304
|
+
}
|
|
1305
|
+
});
|
|
1306
|
+
} else {
|
|
1307
|
+
inputWarnings.push({
|
|
1308
|
+
type: "other",
|
|
1309
|
+
message: "Reasoning parts without itemId or encrypted content cannot be sent back to xAI. Skipping."
|
|
1310
|
+
});
|
|
1311
|
+
}
|
|
1312
|
+
break;
|
|
1313
|
+
}
|
|
1314
|
+
case "reasoning-file":
|
|
1315
|
+
case "custom":
|
|
1133
1316
|
case "file": {
|
|
1134
1317
|
inputWarnings.push({
|
|
1135
1318
|
type: "other",
|
|
@@ -1161,7 +1344,7 @@ async function convertToXaiResponsesInput({
|
|
|
1161
1344
|
outputValue = output.value;
|
|
1162
1345
|
break;
|
|
1163
1346
|
case "execution-denied":
|
|
1164
|
-
outputValue = (
|
|
1347
|
+
outputValue = (_i = output.reason) != null ? _i : "tool execution denied";
|
|
1165
1348
|
break;
|
|
1166
1349
|
case "json":
|
|
1167
1350
|
case "error-json":
|
|
@@ -1229,6 +1412,7 @@ function mapXaiResponsesFinishReason(finishReason) {
|
|
|
1229
1412
|
case "completed":
|
|
1230
1413
|
return "stop";
|
|
1231
1414
|
case "length":
|
|
1415
|
+
case "max_output_tokens":
|
|
1232
1416
|
return "length";
|
|
1233
1417
|
case "tool_calls":
|
|
1234
1418
|
case "function_call":
|
|
@@ -1241,570 +1425,620 @@ function mapXaiResponsesFinishReason(finishReason) {
|
|
|
1241
1425
|
}
|
|
1242
1426
|
|
|
1243
1427
|
// src/responses/xai-responses-api.ts
|
|
1244
|
-
|
|
1245
|
-
var annotationSchema =
|
|
1246
|
-
|
|
1247
|
-
type:
|
|
1248
|
-
url:
|
|
1249
|
-
title:
|
|
1428
|
+
import { z as z6 } from "zod/v4";
|
|
1429
|
+
var annotationSchema = z6.union([
|
|
1430
|
+
z6.object({
|
|
1431
|
+
type: z6.literal("url_citation"),
|
|
1432
|
+
url: z6.string(),
|
|
1433
|
+
title: z6.string().optional()
|
|
1250
1434
|
}),
|
|
1251
|
-
|
|
1252
|
-
type:
|
|
1435
|
+
z6.object({
|
|
1436
|
+
type: z6.string()
|
|
1253
1437
|
})
|
|
1254
1438
|
]);
|
|
1255
|
-
var messageContentPartSchema =
|
|
1256
|
-
type:
|
|
1257
|
-
text:
|
|
1258
|
-
logprobs:
|
|
1259
|
-
annotations:
|
|
1439
|
+
var messageContentPartSchema = z6.object({
|
|
1440
|
+
type: z6.string(),
|
|
1441
|
+
text: z6.string().optional(),
|
|
1442
|
+
logprobs: z6.array(z6.any()).optional(),
|
|
1443
|
+
annotations: z6.array(annotationSchema).optional()
|
|
1260
1444
|
});
|
|
1261
|
-
var reasoningSummaryPartSchema =
|
|
1262
|
-
type:
|
|
1263
|
-
text:
|
|
1445
|
+
var reasoningSummaryPartSchema = z6.object({
|
|
1446
|
+
type: z6.string(),
|
|
1447
|
+
text: z6.string()
|
|
1264
1448
|
});
|
|
1265
|
-
var toolCallSchema =
|
|
1266
|
-
name:
|
|
1267
|
-
arguments:
|
|
1268
|
-
input:
|
|
1269
|
-
call_id:
|
|
1270
|
-
id:
|
|
1271
|
-
status:
|
|
1272
|
-
action:
|
|
1449
|
+
var toolCallSchema = z6.object({
|
|
1450
|
+
name: z6.string().optional(),
|
|
1451
|
+
arguments: z6.string().optional(),
|
|
1452
|
+
input: z6.string().optional(),
|
|
1453
|
+
call_id: z6.string().optional(),
|
|
1454
|
+
id: z6.string(),
|
|
1455
|
+
status: z6.string(),
|
|
1456
|
+
action: z6.any().optional()
|
|
1273
1457
|
});
|
|
1274
|
-
var mcpCallSchema =
|
|
1275
|
-
name:
|
|
1276
|
-
arguments:
|
|
1277
|
-
output:
|
|
1278
|
-
error:
|
|
1279
|
-
id:
|
|
1280
|
-
status:
|
|
1281
|
-
server_label:
|
|
1458
|
+
var mcpCallSchema = z6.object({
|
|
1459
|
+
name: z6.string().optional(),
|
|
1460
|
+
arguments: z6.string().optional(),
|
|
1461
|
+
output: z6.string().optional(),
|
|
1462
|
+
error: z6.string().optional(),
|
|
1463
|
+
id: z6.string(),
|
|
1464
|
+
status: z6.string(),
|
|
1465
|
+
server_label: z6.string().optional()
|
|
1282
1466
|
});
|
|
1283
|
-
var outputItemSchema =
|
|
1284
|
-
|
|
1285
|
-
type:
|
|
1467
|
+
var outputItemSchema = z6.discriminatedUnion("type", [
|
|
1468
|
+
z6.object({
|
|
1469
|
+
type: z6.literal("web_search_call"),
|
|
1286
1470
|
...toolCallSchema.shape
|
|
1287
1471
|
}),
|
|
1288
|
-
|
|
1289
|
-
type:
|
|
1472
|
+
z6.object({
|
|
1473
|
+
type: z6.literal("x_search_call"),
|
|
1290
1474
|
...toolCallSchema.shape
|
|
1291
1475
|
}),
|
|
1292
|
-
|
|
1293
|
-
type:
|
|
1476
|
+
z6.object({
|
|
1477
|
+
type: z6.literal("code_interpreter_call"),
|
|
1294
1478
|
...toolCallSchema.shape
|
|
1295
1479
|
}),
|
|
1296
|
-
|
|
1297
|
-
type:
|
|
1480
|
+
z6.object({
|
|
1481
|
+
type: z6.literal("code_execution_call"),
|
|
1298
1482
|
...toolCallSchema.shape
|
|
1299
1483
|
}),
|
|
1300
|
-
|
|
1301
|
-
type:
|
|
1484
|
+
z6.object({
|
|
1485
|
+
type: z6.literal("view_image_call"),
|
|
1302
1486
|
...toolCallSchema.shape
|
|
1303
1487
|
}),
|
|
1304
|
-
|
|
1305
|
-
type:
|
|
1488
|
+
z6.object({
|
|
1489
|
+
type: z6.literal("view_x_video_call"),
|
|
1306
1490
|
...toolCallSchema.shape
|
|
1307
1491
|
}),
|
|
1308
|
-
|
|
1309
|
-
type:
|
|
1310
|
-
id:
|
|
1311
|
-
status:
|
|
1312
|
-
queries:
|
|
1313
|
-
results:
|
|
1314
|
-
|
|
1315
|
-
file_id:
|
|
1316
|
-
filename:
|
|
1317
|
-
score:
|
|
1318
|
-
text:
|
|
1492
|
+
z6.object({
|
|
1493
|
+
type: z6.literal("file_search_call"),
|
|
1494
|
+
id: z6.string(),
|
|
1495
|
+
status: z6.string(),
|
|
1496
|
+
queries: z6.array(z6.string()).optional(),
|
|
1497
|
+
results: z6.array(
|
|
1498
|
+
z6.object({
|
|
1499
|
+
file_id: z6.string(),
|
|
1500
|
+
filename: z6.string(),
|
|
1501
|
+
score: z6.number(),
|
|
1502
|
+
text: z6.string()
|
|
1319
1503
|
})
|
|
1320
1504
|
).nullish()
|
|
1321
1505
|
}),
|
|
1322
|
-
|
|
1323
|
-
type:
|
|
1506
|
+
z6.object({
|
|
1507
|
+
type: z6.literal("custom_tool_call"),
|
|
1324
1508
|
...toolCallSchema.shape
|
|
1325
1509
|
}),
|
|
1326
|
-
|
|
1327
|
-
type:
|
|
1510
|
+
z6.object({
|
|
1511
|
+
type: z6.literal("mcp_call"),
|
|
1328
1512
|
...mcpCallSchema.shape
|
|
1329
1513
|
}),
|
|
1330
|
-
|
|
1331
|
-
type:
|
|
1332
|
-
role:
|
|
1333
|
-
content:
|
|
1334
|
-
id:
|
|
1335
|
-
status:
|
|
1514
|
+
z6.object({
|
|
1515
|
+
type: z6.literal("message"),
|
|
1516
|
+
role: z6.string(),
|
|
1517
|
+
content: z6.array(messageContentPartSchema),
|
|
1518
|
+
id: z6.string(),
|
|
1519
|
+
status: z6.string()
|
|
1336
1520
|
}),
|
|
1337
|
-
|
|
1338
|
-
type:
|
|
1339
|
-
name:
|
|
1340
|
-
arguments:
|
|
1341
|
-
call_id:
|
|
1342
|
-
id:
|
|
1521
|
+
z6.object({
|
|
1522
|
+
type: z6.literal("function_call"),
|
|
1523
|
+
name: z6.string(),
|
|
1524
|
+
arguments: z6.string(),
|
|
1525
|
+
call_id: z6.string(),
|
|
1526
|
+
id: z6.string()
|
|
1343
1527
|
}),
|
|
1344
|
-
|
|
1345
|
-
type:
|
|
1346
|
-
id:
|
|
1347
|
-
summary:
|
|
1348
|
-
content:
|
|
1349
|
-
status:
|
|
1350
|
-
encrypted_content:
|
|
1528
|
+
z6.object({
|
|
1529
|
+
type: z6.literal("reasoning"),
|
|
1530
|
+
id: z6.string(),
|
|
1531
|
+
summary: z6.array(reasoningSummaryPartSchema),
|
|
1532
|
+
content: z6.array(z6.object({ type: z6.string(), text: z6.string() })).nullish(),
|
|
1533
|
+
status: z6.string(),
|
|
1534
|
+
encrypted_content: z6.string().nullish()
|
|
1351
1535
|
})
|
|
1352
1536
|
]);
|
|
1353
|
-
var xaiResponsesUsageSchema =
|
|
1354
|
-
input_tokens:
|
|
1355
|
-
output_tokens:
|
|
1356
|
-
total_tokens:
|
|
1357
|
-
input_tokens_details:
|
|
1358
|
-
cached_tokens:
|
|
1537
|
+
var xaiResponsesUsageSchema = z6.object({
|
|
1538
|
+
input_tokens: z6.number(),
|
|
1539
|
+
output_tokens: z6.number(),
|
|
1540
|
+
total_tokens: z6.number().optional(),
|
|
1541
|
+
input_tokens_details: z6.object({
|
|
1542
|
+
cached_tokens: z6.number().optional()
|
|
1359
1543
|
}).optional(),
|
|
1360
|
-
output_tokens_details:
|
|
1361
|
-
reasoning_tokens:
|
|
1544
|
+
output_tokens_details: z6.object({
|
|
1545
|
+
reasoning_tokens: z6.number().optional()
|
|
1362
1546
|
}).optional(),
|
|
1363
|
-
num_sources_used:
|
|
1364
|
-
num_server_side_tools_used:
|
|
1547
|
+
num_sources_used: z6.number().optional(),
|
|
1548
|
+
num_server_side_tools_used: z6.number().optional(),
|
|
1549
|
+
cost_in_usd_ticks: z6.number().nullish()
|
|
1365
1550
|
});
|
|
1366
|
-
var xaiResponsesResponseSchema =
|
|
1367
|
-
id:
|
|
1368
|
-
created_at:
|
|
1369
|
-
model:
|
|
1370
|
-
object:
|
|
1371
|
-
output:
|
|
1551
|
+
var xaiResponsesResponseSchema = z6.object({
|
|
1552
|
+
id: z6.string().nullish(),
|
|
1553
|
+
created_at: z6.number().nullish(),
|
|
1554
|
+
model: z6.string().nullish(),
|
|
1555
|
+
object: z6.literal("response"),
|
|
1556
|
+
output: z6.array(outputItemSchema),
|
|
1372
1557
|
usage: xaiResponsesUsageSchema.nullish(),
|
|
1373
|
-
status:
|
|
1558
|
+
status: z6.string()
|
|
1374
1559
|
});
|
|
1375
|
-
var xaiResponsesChunkSchema =
|
|
1376
|
-
|
|
1377
|
-
type:
|
|
1560
|
+
var xaiResponsesChunkSchema = z6.union([
|
|
1561
|
+
z6.object({
|
|
1562
|
+
type: z6.literal("response.created"),
|
|
1378
1563
|
response: xaiResponsesResponseSchema.partial({ usage: true, status: true })
|
|
1379
1564
|
}),
|
|
1380
|
-
|
|
1381
|
-
type:
|
|
1565
|
+
z6.object({
|
|
1566
|
+
type: z6.literal("response.in_progress"),
|
|
1382
1567
|
response: xaiResponsesResponseSchema.partial({ usage: true, status: true })
|
|
1383
1568
|
}),
|
|
1384
|
-
|
|
1385
|
-
type:
|
|
1569
|
+
z6.object({
|
|
1570
|
+
type: z6.literal("response.output_item.added"),
|
|
1386
1571
|
item: outputItemSchema,
|
|
1387
|
-
output_index:
|
|
1572
|
+
output_index: z6.number()
|
|
1388
1573
|
}),
|
|
1389
|
-
|
|
1390
|
-
type:
|
|
1574
|
+
z6.object({
|
|
1575
|
+
type: z6.literal("response.output_item.done"),
|
|
1391
1576
|
item: outputItemSchema,
|
|
1392
|
-
output_index:
|
|
1577
|
+
output_index: z6.number()
|
|
1393
1578
|
}),
|
|
1394
|
-
|
|
1395
|
-
type:
|
|
1396
|
-
item_id:
|
|
1397
|
-
output_index:
|
|
1398
|
-
content_index:
|
|
1579
|
+
z6.object({
|
|
1580
|
+
type: z6.literal("response.content_part.added"),
|
|
1581
|
+
item_id: z6.string(),
|
|
1582
|
+
output_index: z6.number(),
|
|
1583
|
+
content_index: z6.number(),
|
|
1399
1584
|
part: messageContentPartSchema
|
|
1400
1585
|
}),
|
|
1401
|
-
|
|
1402
|
-
type:
|
|
1403
|
-
item_id:
|
|
1404
|
-
output_index:
|
|
1405
|
-
content_index:
|
|
1586
|
+
z6.object({
|
|
1587
|
+
type: z6.literal("response.content_part.done"),
|
|
1588
|
+
item_id: z6.string(),
|
|
1589
|
+
output_index: z6.number(),
|
|
1590
|
+
content_index: z6.number(),
|
|
1406
1591
|
part: messageContentPartSchema
|
|
1407
1592
|
}),
|
|
1408
|
-
|
|
1409
|
-
type:
|
|
1410
|
-
item_id:
|
|
1411
|
-
output_index:
|
|
1412
|
-
content_index:
|
|
1413
|
-
delta:
|
|
1414
|
-
logprobs:
|
|
1593
|
+
z6.object({
|
|
1594
|
+
type: z6.literal("response.output_text.delta"),
|
|
1595
|
+
item_id: z6.string(),
|
|
1596
|
+
output_index: z6.number(),
|
|
1597
|
+
content_index: z6.number(),
|
|
1598
|
+
delta: z6.string(),
|
|
1599
|
+
logprobs: z6.array(z6.any()).optional()
|
|
1415
1600
|
}),
|
|
1416
|
-
|
|
1417
|
-
type:
|
|
1418
|
-
item_id:
|
|
1419
|
-
output_index:
|
|
1420
|
-
content_index:
|
|
1421
|
-
text:
|
|
1422
|
-
logprobs:
|
|
1423
|
-
annotations:
|
|
1601
|
+
z6.object({
|
|
1602
|
+
type: z6.literal("response.output_text.done"),
|
|
1603
|
+
item_id: z6.string(),
|
|
1604
|
+
output_index: z6.number(),
|
|
1605
|
+
content_index: z6.number(),
|
|
1606
|
+
text: z6.string(),
|
|
1607
|
+
logprobs: z6.array(z6.any()).optional(),
|
|
1608
|
+
annotations: z6.array(annotationSchema).optional()
|
|
1424
1609
|
}),
|
|
1425
|
-
|
|
1426
|
-
type:
|
|
1427
|
-
item_id:
|
|
1428
|
-
output_index:
|
|
1429
|
-
content_index:
|
|
1430
|
-
annotation_index:
|
|
1610
|
+
z6.object({
|
|
1611
|
+
type: z6.literal("response.output_text.annotation.added"),
|
|
1612
|
+
item_id: z6.string(),
|
|
1613
|
+
output_index: z6.number(),
|
|
1614
|
+
content_index: z6.number(),
|
|
1615
|
+
annotation_index: z6.number(),
|
|
1431
1616
|
annotation: annotationSchema
|
|
1432
1617
|
}),
|
|
1433
|
-
|
|
1434
|
-
type:
|
|
1435
|
-
item_id:
|
|
1436
|
-
output_index:
|
|
1437
|
-
summary_index:
|
|
1618
|
+
z6.object({
|
|
1619
|
+
type: z6.literal("response.reasoning_summary_part.added"),
|
|
1620
|
+
item_id: z6.string(),
|
|
1621
|
+
output_index: z6.number(),
|
|
1622
|
+
summary_index: z6.number(),
|
|
1438
1623
|
part: reasoningSummaryPartSchema
|
|
1439
1624
|
}),
|
|
1440
|
-
|
|
1441
|
-
type:
|
|
1442
|
-
item_id:
|
|
1443
|
-
output_index:
|
|
1444
|
-
summary_index:
|
|
1625
|
+
z6.object({
|
|
1626
|
+
type: z6.literal("response.reasoning_summary_part.done"),
|
|
1627
|
+
item_id: z6.string(),
|
|
1628
|
+
output_index: z6.number(),
|
|
1629
|
+
summary_index: z6.number(),
|
|
1445
1630
|
part: reasoningSummaryPartSchema
|
|
1446
1631
|
}),
|
|
1447
|
-
|
|
1448
|
-
type:
|
|
1449
|
-
item_id:
|
|
1450
|
-
output_index:
|
|
1451
|
-
summary_index:
|
|
1452
|
-
delta:
|
|
1632
|
+
z6.object({
|
|
1633
|
+
type: z6.literal("response.reasoning_summary_text.delta"),
|
|
1634
|
+
item_id: z6.string(),
|
|
1635
|
+
output_index: z6.number(),
|
|
1636
|
+
summary_index: z6.number(),
|
|
1637
|
+
delta: z6.string()
|
|
1453
1638
|
}),
|
|
1454
|
-
|
|
1455
|
-
type:
|
|
1456
|
-
item_id:
|
|
1457
|
-
output_index:
|
|
1458
|
-
summary_index:
|
|
1459
|
-
text:
|
|
1639
|
+
z6.object({
|
|
1640
|
+
type: z6.literal("response.reasoning_summary_text.done"),
|
|
1641
|
+
item_id: z6.string(),
|
|
1642
|
+
output_index: z6.number(),
|
|
1643
|
+
summary_index: z6.number(),
|
|
1644
|
+
text: z6.string()
|
|
1460
1645
|
}),
|
|
1461
|
-
|
|
1462
|
-
type:
|
|
1463
|
-
item_id:
|
|
1464
|
-
output_index:
|
|
1465
|
-
content_index:
|
|
1466
|
-
delta:
|
|
1646
|
+
z6.object({
|
|
1647
|
+
type: z6.literal("response.reasoning_text.delta"),
|
|
1648
|
+
item_id: z6.string(),
|
|
1649
|
+
output_index: z6.number(),
|
|
1650
|
+
content_index: z6.number(),
|
|
1651
|
+
delta: z6.string()
|
|
1467
1652
|
}),
|
|
1468
|
-
|
|
1469
|
-
type:
|
|
1470
|
-
item_id:
|
|
1471
|
-
output_index:
|
|
1472
|
-
content_index:
|
|
1473
|
-
text:
|
|
1653
|
+
z6.object({
|
|
1654
|
+
type: z6.literal("response.reasoning_text.done"),
|
|
1655
|
+
item_id: z6.string(),
|
|
1656
|
+
output_index: z6.number(),
|
|
1657
|
+
content_index: z6.number(),
|
|
1658
|
+
text: z6.string()
|
|
1474
1659
|
}),
|
|
1475
|
-
|
|
1476
|
-
type:
|
|
1477
|
-
item_id:
|
|
1478
|
-
output_index:
|
|
1660
|
+
z6.object({
|
|
1661
|
+
type: z6.literal("response.web_search_call.in_progress"),
|
|
1662
|
+
item_id: z6.string(),
|
|
1663
|
+
output_index: z6.number()
|
|
1479
1664
|
}),
|
|
1480
|
-
|
|
1481
|
-
type:
|
|
1482
|
-
item_id:
|
|
1483
|
-
output_index:
|
|
1665
|
+
z6.object({
|
|
1666
|
+
type: z6.literal("response.web_search_call.searching"),
|
|
1667
|
+
item_id: z6.string(),
|
|
1668
|
+
output_index: z6.number()
|
|
1484
1669
|
}),
|
|
1485
|
-
|
|
1486
|
-
type:
|
|
1487
|
-
item_id:
|
|
1488
|
-
output_index:
|
|
1670
|
+
z6.object({
|
|
1671
|
+
type: z6.literal("response.web_search_call.completed"),
|
|
1672
|
+
item_id: z6.string(),
|
|
1673
|
+
output_index: z6.number()
|
|
1489
1674
|
}),
|
|
1490
|
-
|
|
1491
|
-
type:
|
|
1492
|
-
item_id:
|
|
1493
|
-
output_index:
|
|
1675
|
+
z6.object({
|
|
1676
|
+
type: z6.literal("response.x_search_call.in_progress"),
|
|
1677
|
+
item_id: z6.string(),
|
|
1678
|
+
output_index: z6.number()
|
|
1494
1679
|
}),
|
|
1495
|
-
|
|
1496
|
-
type:
|
|
1497
|
-
item_id:
|
|
1498
|
-
output_index:
|
|
1680
|
+
z6.object({
|
|
1681
|
+
type: z6.literal("response.x_search_call.searching"),
|
|
1682
|
+
item_id: z6.string(),
|
|
1683
|
+
output_index: z6.number()
|
|
1499
1684
|
}),
|
|
1500
|
-
|
|
1501
|
-
type:
|
|
1502
|
-
item_id:
|
|
1503
|
-
output_index:
|
|
1685
|
+
z6.object({
|
|
1686
|
+
type: z6.literal("response.x_search_call.completed"),
|
|
1687
|
+
item_id: z6.string(),
|
|
1688
|
+
output_index: z6.number()
|
|
1504
1689
|
}),
|
|
1505
|
-
|
|
1506
|
-
type:
|
|
1507
|
-
item_id:
|
|
1508
|
-
output_index:
|
|
1690
|
+
z6.object({
|
|
1691
|
+
type: z6.literal("response.file_search_call.in_progress"),
|
|
1692
|
+
item_id: z6.string(),
|
|
1693
|
+
output_index: z6.number()
|
|
1509
1694
|
}),
|
|
1510
|
-
|
|
1511
|
-
type:
|
|
1512
|
-
item_id:
|
|
1513
|
-
output_index:
|
|
1695
|
+
z6.object({
|
|
1696
|
+
type: z6.literal("response.file_search_call.searching"),
|
|
1697
|
+
item_id: z6.string(),
|
|
1698
|
+
output_index: z6.number()
|
|
1514
1699
|
}),
|
|
1515
|
-
|
|
1516
|
-
type:
|
|
1517
|
-
item_id:
|
|
1518
|
-
output_index:
|
|
1700
|
+
z6.object({
|
|
1701
|
+
type: z6.literal("response.file_search_call.completed"),
|
|
1702
|
+
item_id: z6.string(),
|
|
1703
|
+
output_index: z6.number()
|
|
1519
1704
|
}),
|
|
1520
|
-
|
|
1521
|
-
type:
|
|
1522
|
-
item_id:
|
|
1523
|
-
output_index:
|
|
1705
|
+
z6.object({
|
|
1706
|
+
type: z6.literal("response.code_execution_call.in_progress"),
|
|
1707
|
+
item_id: z6.string(),
|
|
1708
|
+
output_index: z6.number()
|
|
1524
1709
|
}),
|
|
1525
|
-
|
|
1526
|
-
type:
|
|
1527
|
-
item_id:
|
|
1528
|
-
output_index:
|
|
1710
|
+
z6.object({
|
|
1711
|
+
type: z6.literal("response.code_execution_call.executing"),
|
|
1712
|
+
item_id: z6.string(),
|
|
1713
|
+
output_index: z6.number()
|
|
1529
1714
|
}),
|
|
1530
|
-
|
|
1531
|
-
type:
|
|
1532
|
-
item_id:
|
|
1533
|
-
output_index:
|
|
1715
|
+
z6.object({
|
|
1716
|
+
type: z6.literal("response.code_execution_call.completed"),
|
|
1717
|
+
item_id: z6.string(),
|
|
1718
|
+
output_index: z6.number()
|
|
1534
1719
|
}),
|
|
1535
|
-
|
|
1536
|
-
type:
|
|
1537
|
-
item_id:
|
|
1538
|
-
output_index:
|
|
1720
|
+
z6.object({
|
|
1721
|
+
type: z6.literal("response.code_interpreter_call.in_progress"),
|
|
1722
|
+
item_id: z6.string(),
|
|
1723
|
+
output_index: z6.number()
|
|
1539
1724
|
}),
|
|
1540
|
-
|
|
1541
|
-
type:
|
|
1542
|
-
item_id:
|
|
1543
|
-
output_index:
|
|
1725
|
+
z6.object({
|
|
1726
|
+
type: z6.literal("response.code_interpreter_call.executing"),
|
|
1727
|
+
item_id: z6.string(),
|
|
1728
|
+
output_index: z6.number()
|
|
1544
1729
|
}),
|
|
1545
|
-
|
|
1546
|
-
type:
|
|
1547
|
-
item_id:
|
|
1548
|
-
output_index:
|
|
1730
|
+
z6.object({
|
|
1731
|
+
type: z6.literal("response.code_interpreter_call.interpreting"),
|
|
1732
|
+
item_id: z6.string(),
|
|
1733
|
+
output_index: z6.number()
|
|
1549
1734
|
}),
|
|
1550
|
-
|
|
1551
|
-
type:
|
|
1552
|
-
item_id:
|
|
1553
|
-
output_index:
|
|
1735
|
+
z6.object({
|
|
1736
|
+
type: z6.literal("response.code_interpreter_call.completed"),
|
|
1737
|
+
item_id: z6.string(),
|
|
1738
|
+
output_index: z6.number()
|
|
1554
1739
|
}),
|
|
1555
1740
|
// Code interpreter code streaming events
|
|
1556
|
-
|
|
1557
|
-
type:
|
|
1558
|
-
item_id:
|
|
1559
|
-
output_index:
|
|
1560
|
-
delta:
|
|
1741
|
+
z6.object({
|
|
1742
|
+
type: z6.literal("response.code_interpreter_call_code.delta"),
|
|
1743
|
+
item_id: z6.string(),
|
|
1744
|
+
output_index: z6.number(),
|
|
1745
|
+
delta: z6.string()
|
|
1561
1746
|
}),
|
|
1562
|
-
|
|
1563
|
-
type:
|
|
1564
|
-
item_id:
|
|
1565
|
-
output_index:
|
|
1566
|
-
code:
|
|
1747
|
+
z6.object({
|
|
1748
|
+
type: z6.literal("response.code_interpreter_call_code.done"),
|
|
1749
|
+
item_id: z6.string(),
|
|
1750
|
+
output_index: z6.number(),
|
|
1751
|
+
code: z6.string()
|
|
1567
1752
|
}),
|
|
1568
|
-
|
|
1569
|
-
type:
|
|
1570
|
-
item_id:
|
|
1571
|
-
output_index:
|
|
1572
|
-
delta:
|
|
1753
|
+
z6.object({
|
|
1754
|
+
type: z6.literal("response.custom_tool_call_input.delta"),
|
|
1755
|
+
item_id: z6.string(),
|
|
1756
|
+
output_index: z6.number(),
|
|
1757
|
+
delta: z6.string()
|
|
1573
1758
|
}),
|
|
1574
|
-
|
|
1575
|
-
type:
|
|
1576
|
-
item_id:
|
|
1577
|
-
output_index:
|
|
1578
|
-
input:
|
|
1759
|
+
z6.object({
|
|
1760
|
+
type: z6.literal("response.custom_tool_call_input.done"),
|
|
1761
|
+
item_id: z6.string(),
|
|
1762
|
+
output_index: z6.number(),
|
|
1763
|
+
input: z6.string()
|
|
1579
1764
|
}),
|
|
1580
1765
|
// Function call arguments streaming events (standard function tools)
|
|
1581
|
-
|
|
1582
|
-
type:
|
|
1583
|
-
item_id:
|
|
1584
|
-
output_index:
|
|
1585
|
-
delta:
|
|
1766
|
+
z6.object({
|
|
1767
|
+
type: z6.literal("response.function_call_arguments.delta"),
|
|
1768
|
+
item_id: z6.string(),
|
|
1769
|
+
output_index: z6.number(),
|
|
1770
|
+
delta: z6.string()
|
|
1771
|
+
}),
|
|
1772
|
+
z6.object({
|
|
1773
|
+
type: z6.literal("response.function_call_arguments.done"),
|
|
1774
|
+
item_id: z6.string(),
|
|
1775
|
+
output_index: z6.number(),
|
|
1776
|
+
arguments: z6.string()
|
|
1586
1777
|
}),
|
|
1587
|
-
|
|
1588
|
-
type:
|
|
1589
|
-
item_id:
|
|
1590
|
-
output_index:
|
|
1591
|
-
arguments: import_v46.z.string()
|
|
1778
|
+
z6.object({
|
|
1779
|
+
type: z6.literal("response.mcp_call.in_progress"),
|
|
1780
|
+
item_id: z6.string(),
|
|
1781
|
+
output_index: z6.number()
|
|
1592
1782
|
}),
|
|
1593
|
-
|
|
1594
|
-
type:
|
|
1595
|
-
item_id:
|
|
1596
|
-
output_index:
|
|
1783
|
+
z6.object({
|
|
1784
|
+
type: z6.literal("response.mcp_call.executing"),
|
|
1785
|
+
item_id: z6.string(),
|
|
1786
|
+
output_index: z6.number()
|
|
1597
1787
|
}),
|
|
1598
|
-
|
|
1599
|
-
type:
|
|
1600
|
-
item_id:
|
|
1601
|
-
output_index:
|
|
1788
|
+
z6.object({
|
|
1789
|
+
type: z6.literal("response.mcp_call.completed"),
|
|
1790
|
+
item_id: z6.string(),
|
|
1791
|
+
output_index: z6.number()
|
|
1602
1792
|
}),
|
|
1603
|
-
|
|
1604
|
-
type:
|
|
1605
|
-
item_id:
|
|
1606
|
-
output_index:
|
|
1793
|
+
z6.object({
|
|
1794
|
+
type: z6.literal("response.mcp_call.failed"),
|
|
1795
|
+
item_id: z6.string(),
|
|
1796
|
+
output_index: z6.number()
|
|
1607
1797
|
}),
|
|
1608
|
-
|
|
1609
|
-
type:
|
|
1610
|
-
item_id:
|
|
1611
|
-
output_index:
|
|
1798
|
+
z6.object({
|
|
1799
|
+
type: z6.literal("response.mcp_call_arguments.delta"),
|
|
1800
|
+
item_id: z6.string(),
|
|
1801
|
+
output_index: z6.number(),
|
|
1802
|
+
delta: z6.string()
|
|
1612
1803
|
}),
|
|
1613
|
-
|
|
1614
|
-
type:
|
|
1615
|
-
item_id:
|
|
1616
|
-
output_index:
|
|
1617
|
-
|
|
1804
|
+
z6.object({
|
|
1805
|
+
type: z6.literal("response.mcp_call_arguments.done"),
|
|
1806
|
+
item_id: z6.string(),
|
|
1807
|
+
output_index: z6.number(),
|
|
1808
|
+
arguments: z6.string().optional()
|
|
1618
1809
|
}),
|
|
1619
|
-
|
|
1620
|
-
type:
|
|
1621
|
-
item_id:
|
|
1622
|
-
output_index:
|
|
1623
|
-
|
|
1810
|
+
z6.object({
|
|
1811
|
+
type: z6.literal("response.mcp_call_output.delta"),
|
|
1812
|
+
item_id: z6.string(),
|
|
1813
|
+
output_index: z6.number(),
|
|
1814
|
+
delta: z6.string()
|
|
1624
1815
|
}),
|
|
1625
|
-
|
|
1626
|
-
type:
|
|
1627
|
-
item_id:
|
|
1628
|
-
output_index:
|
|
1629
|
-
|
|
1816
|
+
z6.object({
|
|
1817
|
+
type: z6.literal("response.mcp_call_output.done"),
|
|
1818
|
+
item_id: z6.string(),
|
|
1819
|
+
output_index: z6.number(),
|
|
1820
|
+
output: z6.string().optional()
|
|
1630
1821
|
}),
|
|
1631
|
-
|
|
1632
|
-
type:
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1822
|
+
z6.object({
|
|
1823
|
+
type: z6.literal("response.incomplete"),
|
|
1824
|
+
response: z6.object({
|
|
1825
|
+
incomplete_details: z6.object({ reason: z6.string() }).nullish(),
|
|
1826
|
+
usage: xaiResponsesUsageSchema.nullish()
|
|
1827
|
+
})
|
|
1636
1828
|
}),
|
|
1637
|
-
|
|
1638
|
-
type:
|
|
1829
|
+
z6.object({
|
|
1830
|
+
type: z6.literal("response.failed"),
|
|
1831
|
+
response: z6.object({
|
|
1832
|
+
error: z6.object({
|
|
1833
|
+
code: z6.string().nullish(),
|
|
1834
|
+
message: z6.string()
|
|
1835
|
+
}).nullish(),
|
|
1836
|
+
incomplete_details: z6.object({ reason: z6.string() }).nullish(),
|
|
1837
|
+
usage: xaiResponsesUsageSchema.nullish()
|
|
1838
|
+
})
|
|
1839
|
+
}),
|
|
1840
|
+
z6.object({
|
|
1841
|
+
type: z6.literal("error"),
|
|
1842
|
+
code: z6.string().nullish(),
|
|
1843
|
+
message: z6.string(),
|
|
1844
|
+
param: z6.string().nullish()
|
|
1845
|
+
}),
|
|
1846
|
+
z6.object({
|
|
1847
|
+
type: z6.literal("response.done"),
|
|
1639
1848
|
response: xaiResponsesResponseSchema
|
|
1640
1849
|
}),
|
|
1641
|
-
|
|
1642
|
-
type:
|
|
1850
|
+
z6.object({
|
|
1851
|
+
type: z6.literal("response.completed"),
|
|
1643
1852
|
response: xaiResponsesResponseSchema
|
|
1644
1853
|
})
|
|
1645
1854
|
]);
|
|
1646
1855
|
|
|
1647
|
-
// src/responses/xai-responses-options.ts
|
|
1648
|
-
|
|
1649
|
-
var xaiLanguageModelResponsesOptions =
|
|
1856
|
+
// src/responses/xai-responses-language-model-options.ts
|
|
1857
|
+
import { z as z7 } from "zod/v4";
|
|
1858
|
+
var xaiLanguageModelResponsesOptions = z7.object({
|
|
1650
1859
|
/**
|
|
1651
1860
|
* Constrains how hard a reasoning model thinks before responding.
|
|
1652
|
-
* Possible values are `
|
|
1861
|
+
* Possible values are `none` (disables reasoning entirely; supported by
|
|
1862
|
+
* `grok-4.3` and newer reasoning models), `low` (uses fewer reasoning
|
|
1863
|
+
* tokens), `medium`, and `high` (uses more reasoning tokens).
|
|
1864
|
+
*
|
|
1865
|
+
* @see https://docs.x.ai/docs/guides/reasoning
|
|
1653
1866
|
*/
|
|
1654
|
-
reasoningEffort:
|
|
1655
|
-
reasoningSummary:
|
|
1656
|
-
logprobs:
|
|
1657
|
-
topLogprobs:
|
|
1867
|
+
reasoningEffort: z7.enum(["none", "low", "medium", "high"]).optional(),
|
|
1868
|
+
reasoningSummary: z7.enum(["auto", "concise", "detailed"]).optional(),
|
|
1869
|
+
logprobs: z7.boolean().optional(),
|
|
1870
|
+
topLogprobs: z7.number().int().min(0).max(8).optional(),
|
|
1658
1871
|
/**
|
|
1659
1872
|
* Whether to store the input message(s) and model response for later retrieval.
|
|
1873
|
+
* Must be set to `false` for teams with Zero Data Retention (ZDR) enabled,
|
|
1874
|
+
* otherwise the API will return an error.
|
|
1660
1875
|
* @default true
|
|
1661
1876
|
*/
|
|
1662
|
-
store:
|
|
1877
|
+
store: z7.boolean().optional(),
|
|
1663
1878
|
/**
|
|
1664
1879
|
* The ID of the previous response from the model.
|
|
1665
1880
|
*/
|
|
1666
|
-
previousResponseId:
|
|
1881
|
+
previousResponseId: z7.string().optional(),
|
|
1667
1882
|
/**
|
|
1668
1883
|
* Specify additional output data to include in the model response.
|
|
1669
1884
|
* Example values: 'file_search_call.results'.
|
|
1670
1885
|
*/
|
|
1671
|
-
include:
|
|
1886
|
+
include: z7.array(z7.enum(["file_search_call.results"])).nullish()
|
|
1672
1887
|
});
|
|
1673
1888
|
|
|
1674
1889
|
// src/responses/xai-responses-prepare-tools.ts
|
|
1675
|
-
|
|
1676
|
-
|
|
1890
|
+
import {
|
|
1891
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError4
|
|
1892
|
+
} from "@ai-sdk/provider";
|
|
1893
|
+
import { validateTypes } from "@ai-sdk/provider-utils";
|
|
1677
1894
|
|
|
1678
1895
|
// src/tool/file-search.ts
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1896
|
+
import {
|
|
1897
|
+
createProviderExecutedToolFactory,
|
|
1898
|
+
lazySchema,
|
|
1899
|
+
zodSchema
|
|
1900
|
+
} from "@ai-sdk/provider-utils";
|
|
1901
|
+
import { z as z8 } from "zod/v4";
|
|
1902
|
+
var fileSearchArgsSchema = lazySchema(
|
|
1903
|
+
() => zodSchema(
|
|
1904
|
+
z8.object({
|
|
1905
|
+
vectorStoreIds: z8.array(z8.string()),
|
|
1906
|
+
maxNumResults: z8.number().optional()
|
|
1686
1907
|
})
|
|
1687
1908
|
)
|
|
1688
1909
|
);
|
|
1689
|
-
var fileSearchOutputSchema =
|
|
1690
|
-
() =>
|
|
1691
|
-
|
|
1692
|
-
queries:
|
|
1693
|
-
results:
|
|
1694
|
-
|
|
1695
|
-
fileId:
|
|
1696
|
-
filename:
|
|
1697
|
-
score:
|
|
1698
|
-
text:
|
|
1910
|
+
var fileSearchOutputSchema = lazySchema(
|
|
1911
|
+
() => zodSchema(
|
|
1912
|
+
z8.object({
|
|
1913
|
+
queries: z8.array(z8.string()),
|
|
1914
|
+
results: z8.array(
|
|
1915
|
+
z8.object({
|
|
1916
|
+
fileId: z8.string(),
|
|
1917
|
+
filename: z8.string(),
|
|
1918
|
+
score: z8.number().min(0).max(1),
|
|
1919
|
+
text: z8.string()
|
|
1699
1920
|
})
|
|
1700
1921
|
).nullable()
|
|
1701
1922
|
})
|
|
1702
1923
|
)
|
|
1703
1924
|
);
|
|
1704
|
-
var fileSearchToolFactory = (
|
|
1925
|
+
var fileSearchToolFactory = createProviderExecutedToolFactory({
|
|
1705
1926
|
id: "xai.file_search",
|
|
1706
|
-
inputSchema:
|
|
1927
|
+
inputSchema: lazySchema(() => zodSchema(z8.object({}))),
|
|
1707
1928
|
outputSchema: fileSearchOutputSchema
|
|
1708
1929
|
});
|
|
1709
1930
|
var fileSearch = (args) => fileSearchToolFactory(args);
|
|
1710
1931
|
|
|
1711
1932
|
// src/tool/mcp-server.ts
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1933
|
+
import {
|
|
1934
|
+
createProviderExecutedToolFactory as createProviderExecutedToolFactory2,
|
|
1935
|
+
lazySchema as lazySchema2,
|
|
1936
|
+
zodSchema as zodSchema2
|
|
1937
|
+
} from "@ai-sdk/provider-utils";
|
|
1938
|
+
import { z as z9 } from "zod/v4";
|
|
1939
|
+
var mcpServerArgsSchema = lazySchema2(
|
|
1940
|
+
() => zodSchema2(
|
|
1941
|
+
z9.object({
|
|
1942
|
+
serverUrl: z9.string().describe("The URL of the MCP server"),
|
|
1943
|
+
serverLabel: z9.string().optional().describe("A label for the MCP server"),
|
|
1944
|
+
serverDescription: z9.string().optional().describe("Description of the MCP server"),
|
|
1945
|
+
allowedTools: z9.array(z9.string()).optional().describe("List of allowed tool names"),
|
|
1946
|
+
headers: z9.record(z9.string(), z9.string()).optional().describe("Custom headers to send"),
|
|
1947
|
+
authorization: z9.string().optional().describe("Authorization header value")
|
|
1723
1948
|
})
|
|
1724
1949
|
)
|
|
1725
1950
|
);
|
|
1726
|
-
var mcpServerOutputSchema = (
|
|
1727
|
-
() => (
|
|
1728
|
-
|
|
1729
|
-
name:
|
|
1730
|
-
arguments:
|
|
1731
|
-
result:
|
|
1951
|
+
var mcpServerOutputSchema = lazySchema2(
|
|
1952
|
+
() => zodSchema2(
|
|
1953
|
+
z9.object({
|
|
1954
|
+
name: z9.string(),
|
|
1955
|
+
arguments: z9.string(),
|
|
1956
|
+
result: z9.unknown()
|
|
1732
1957
|
})
|
|
1733
1958
|
)
|
|
1734
1959
|
);
|
|
1735
|
-
var mcpServerToolFactory = (
|
|
1960
|
+
var mcpServerToolFactory = createProviderExecutedToolFactory2({
|
|
1736
1961
|
id: "xai.mcp",
|
|
1737
|
-
inputSchema: (
|
|
1962
|
+
inputSchema: lazySchema2(() => zodSchema2(z9.object({}))),
|
|
1738
1963
|
outputSchema: mcpServerOutputSchema
|
|
1739
1964
|
});
|
|
1740
1965
|
var mcpServer = (args) => mcpServerToolFactory(args);
|
|
1741
1966
|
|
|
1742
1967
|
// src/tool/web-search.ts
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1968
|
+
import {
|
|
1969
|
+
createProviderExecutedToolFactory as createProviderExecutedToolFactory3,
|
|
1970
|
+
lazySchema as lazySchema3,
|
|
1971
|
+
zodSchema as zodSchema3
|
|
1972
|
+
} from "@ai-sdk/provider-utils";
|
|
1973
|
+
import { z as z10 } from "zod/v4";
|
|
1974
|
+
var webSearchArgsSchema = lazySchema3(
|
|
1975
|
+
() => zodSchema3(
|
|
1976
|
+
z10.object({
|
|
1977
|
+
allowedDomains: z10.array(z10.string()).max(5).optional(),
|
|
1978
|
+
excludedDomains: z10.array(z10.string()).max(5).optional(),
|
|
1979
|
+
enableImageSearch: z10.boolean().optional(),
|
|
1980
|
+
enableImageUnderstanding: z10.boolean().optional()
|
|
1751
1981
|
})
|
|
1752
1982
|
)
|
|
1753
1983
|
);
|
|
1754
|
-
var webSearchOutputSchema = (
|
|
1755
|
-
() => (
|
|
1756
|
-
|
|
1757
|
-
query:
|
|
1758
|
-
sources:
|
|
1759
|
-
|
|
1760
|
-
title:
|
|
1761
|
-
url:
|
|
1762
|
-
snippet:
|
|
1984
|
+
var webSearchOutputSchema = lazySchema3(
|
|
1985
|
+
() => zodSchema3(
|
|
1986
|
+
z10.object({
|
|
1987
|
+
query: z10.string(),
|
|
1988
|
+
sources: z10.array(
|
|
1989
|
+
z10.object({
|
|
1990
|
+
title: z10.string(),
|
|
1991
|
+
url: z10.string(),
|
|
1992
|
+
snippet: z10.string()
|
|
1763
1993
|
})
|
|
1764
1994
|
)
|
|
1765
1995
|
})
|
|
1766
1996
|
)
|
|
1767
1997
|
);
|
|
1768
|
-
var webSearchToolFactory = (
|
|
1998
|
+
var webSearchToolFactory = createProviderExecutedToolFactory3({
|
|
1769
1999
|
id: "xai.web_search",
|
|
1770
|
-
inputSchema: (
|
|
2000
|
+
inputSchema: lazySchema3(() => zodSchema3(z10.object({}))),
|
|
1771
2001
|
outputSchema: webSearchOutputSchema
|
|
1772
2002
|
});
|
|
1773
2003
|
var webSearch = (args = {}) => webSearchToolFactory(args);
|
|
1774
2004
|
|
|
1775
2005
|
// src/tool/x-search.ts
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
2006
|
+
import {
|
|
2007
|
+
createProviderExecutedToolFactory as createProviderExecutedToolFactory4,
|
|
2008
|
+
lazySchema as lazySchema4,
|
|
2009
|
+
zodSchema as zodSchema4
|
|
2010
|
+
} from "@ai-sdk/provider-utils";
|
|
2011
|
+
import { z as z11 } from "zod/v4";
|
|
2012
|
+
var xSearchArgsSchema = lazySchema4(
|
|
2013
|
+
() => zodSchema4(
|
|
2014
|
+
z11.object({
|
|
2015
|
+
allowedXHandles: z11.array(z11.string()).max(10).optional(),
|
|
2016
|
+
excludedXHandles: z11.array(z11.string()).max(10).optional(),
|
|
2017
|
+
fromDate: z11.string().optional(),
|
|
2018
|
+
toDate: z11.string().optional(),
|
|
2019
|
+
enableImageUnderstanding: z11.boolean().optional(),
|
|
2020
|
+
enableVideoUnderstanding: z11.boolean().optional()
|
|
1787
2021
|
})
|
|
1788
2022
|
)
|
|
1789
2023
|
);
|
|
1790
|
-
var xSearchOutputSchema = (
|
|
1791
|
-
() => (
|
|
1792
|
-
|
|
1793
|
-
query:
|
|
1794
|
-
posts:
|
|
1795
|
-
|
|
1796
|
-
author:
|
|
1797
|
-
text:
|
|
1798
|
-
url:
|
|
1799
|
-
likes:
|
|
2024
|
+
var xSearchOutputSchema = lazySchema4(
|
|
2025
|
+
() => zodSchema4(
|
|
2026
|
+
z11.object({
|
|
2027
|
+
query: z11.string(),
|
|
2028
|
+
posts: z11.array(
|
|
2029
|
+
z11.object({
|
|
2030
|
+
author: z11.string(),
|
|
2031
|
+
text: z11.string(),
|
|
2032
|
+
url: z11.string(),
|
|
2033
|
+
likes: z11.number()
|
|
1800
2034
|
})
|
|
1801
2035
|
)
|
|
1802
2036
|
})
|
|
1803
2037
|
)
|
|
1804
2038
|
);
|
|
1805
|
-
var xSearchToolFactory = (
|
|
2039
|
+
var xSearchToolFactory = createProviderExecutedToolFactory4({
|
|
1806
2040
|
id: "xai.x_search",
|
|
1807
|
-
inputSchema: (
|
|
2041
|
+
inputSchema: lazySchema4(() => zodSchema4(z11.object({}))),
|
|
1808
2042
|
outputSchema: xSearchOutputSchema
|
|
1809
2043
|
});
|
|
1810
2044
|
var xSearch = (args = {}) => xSearchToolFactory(args);
|
|
@@ -1826,7 +2060,7 @@ async function prepareResponsesTools({
|
|
|
1826
2060
|
if (tool.type === "provider") {
|
|
1827
2061
|
switch (tool.id) {
|
|
1828
2062
|
case "xai.web_search": {
|
|
1829
|
-
const args = await
|
|
2063
|
+
const args = await validateTypes({
|
|
1830
2064
|
value: tool.args,
|
|
1831
2065
|
schema: webSearchArgsSchema
|
|
1832
2066
|
});
|
|
@@ -1834,12 +2068,13 @@ async function prepareResponsesTools({
|
|
|
1834
2068
|
type: "web_search",
|
|
1835
2069
|
allowed_domains: args.allowedDomains,
|
|
1836
2070
|
excluded_domains: args.excludedDomains,
|
|
2071
|
+
enable_image_search: args.enableImageSearch,
|
|
1837
2072
|
enable_image_understanding: args.enableImageUnderstanding
|
|
1838
2073
|
});
|
|
1839
2074
|
break;
|
|
1840
2075
|
}
|
|
1841
2076
|
case "xai.x_search": {
|
|
1842
|
-
const args = await
|
|
2077
|
+
const args = await validateTypes({
|
|
1843
2078
|
value: tool.args,
|
|
1844
2079
|
schema: xSearchArgsSchema
|
|
1845
2080
|
});
|
|
@@ -1873,7 +2108,7 @@ async function prepareResponsesTools({
|
|
|
1873
2108
|
break;
|
|
1874
2109
|
}
|
|
1875
2110
|
case "xai.file_search": {
|
|
1876
|
-
const args = await
|
|
2111
|
+
const args = await validateTypes({
|
|
1877
2112
|
value: tool.args,
|
|
1878
2113
|
schema: fileSearchArgsSchema
|
|
1879
2114
|
});
|
|
@@ -1885,7 +2120,7 @@ async function prepareResponsesTools({
|
|
|
1885
2120
|
break;
|
|
1886
2121
|
}
|
|
1887
2122
|
case "xai.mcp": {
|
|
1888
|
-
const args = await
|
|
2123
|
+
const args = await validateTypes({
|
|
1889
2124
|
value: tool.args,
|
|
1890
2125
|
schema: mcpServerArgsSchema
|
|
1891
2126
|
});
|
|
@@ -1913,7 +2148,7 @@ async function prepareResponsesTools({
|
|
|
1913
2148
|
type: "function",
|
|
1914
2149
|
name: tool.name,
|
|
1915
2150
|
description: tool.description,
|
|
1916
|
-
parameters: tool.inputSchema,
|
|
2151
|
+
parameters: removeAdditionalPropertiesFalse(tool.inputSchema),
|
|
1917
2152
|
...tool.strict != null ? { strict: tool.strict } : {}
|
|
1918
2153
|
});
|
|
1919
2154
|
}
|
|
@@ -1952,7 +2187,7 @@ async function prepareResponsesTools({
|
|
|
1952
2187
|
}
|
|
1953
2188
|
default: {
|
|
1954
2189
|
const _exhaustiveCheck = type;
|
|
1955
|
-
throw new
|
|
2190
|
+
throw new UnsupportedFunctionalityError4({
|
|
1956
2191
|
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
1957
2192
|
});
|
|
1958
2193
|
}
|
|
@@ -1960,15 +2195,29 @@ async function prepareResponsesTools({
|
|
|
1960
2195
|
}
|
|
1961
2196
|
|
|
1962
2197
|
// src/responses/xai-responses-language-model.ts
|
|
1963
|
-
var XaiResponsesLanguageModel = class {
|
|
2198
|
+
var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
1964
2199
|
constructor(modelId, config) {
|
|
1965
|
-
this.specificationVersion = "
|
|
2200
|
+
this.specificationVersion = "v4";
|
|
1966
2201
|
this.supportedUrls = {
|
|
1967
|
-
"image/*": [/^https?:\/\/.*$/]
|
|
2202
|
+
"image/*": [/^https?:\/\/.*$/],
|
|
2203
|
+
// xAI's Responses API accepts non-image documents (PDF, plain text, CSV, etc.) as
|
|
2204
|
+
// `{ type: 'input_file', file_url }`. Keeping these URLs intact here lets them pass
|
|
2205
|
+
// through to the converter instead of being downloaded to bytes by the SDK.
|
|
2206
|
+
"application/pdf": [/^https?:\/\/.*$/],
|
|
2207
|
+
"text/*": [/^https?:\/\/.*$/]
|
|
1968
2208
|
};
|
|
1969
2209
|
this.modelId = modelId;
|
|
1970
2210
|
this.config = config;
|
|
1971
2211
|
}
|
|
2212
|
+
static [WORKFLOW_SERIALIZE3](model) {
|
|
2213
|
+
return serializeModelOptions3({
|
|
2214
|
+
modelId: model.modelId,
|
|
2215
|
+
config: model.config
|
|
2216
|
+
});
|
|
2217
|
+
}
|
|
2218
|
+
static [WORKFLOW_DESERIALIZE3](options) {
|
|
2219
|
+
return new _XaiResponsesLanguageModel(options.modelId, options.config);
|
|
2220
|
+
}
|
|
1972
2221
|
get provider() {
|
|
1973
2222
|
return this.config.provider;
|
|
1974
2223
|
}
|
|
@@ -1982,11 +2231,12 @@ var XaiResponsesLanguageModel = class {
|
|
|
1982
2231
|
responseFormat,
|
|
1983
2232
|
providerOptions,
|
|
1984
2233
|
tools,
|
|
1985
|
-
toolChoice
|
|
2234
|
+
toolChoice,
|
|
2235
|
+
reasoning
|
|
1986
2236
|
}) {
|
|
1987
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
2237
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1988
2238
|
const warnings = [];
|
|
1989
|
-
const options = (_a = await (
|
|
2239
|
+
const options = (_a = await parseProviderOptions3({
|
|
1990
2240
|
provider: "xai",
|
|
1991
2241
|
providerOptions,
|
|
1992
2242
|
schema: xaiLanguageModelResponsesOptions
|
|
@@ -2011,7 +2261,7 @@ var XaiResponsesLanguageModel = class {
|
|
|
2011
2261
|
)) == null ? void 0 : _f.name;
|
|
2012
2262
|
const { input, inputWarnings } = await convertToXaiResponsesInput({
|
|
2013
2263
|
prompt,
|
|
2014
|
-
store: true
|
|
2264
|
+
store: (_g = options.store) != null ? _g : true
|
|
2015
2265
|
});
|
|
2016
2266
|
warnings.push(...inputWarnings);
|
|
2017
2267
|
const {
|
|
@@ -2031,6 +2281,17 @@ var XaiResponsesLanguageModel = class {
|
|
|
2031
2281
|
include = [...include, "reasoning.encrypted_content"];
|
|
2032
2282
|
}
|
|
2033
2283
|
}
|
|
2284
|
+
const resolvedReasoningEffort = (_h = options.reasoningEffort) != null ? _h : isCustomReasoning2(reasoning) ? reasoning === "none" ? void 0 : mapReasoningToProviderEffort2({
|
|
2285
|
+
reasoning,
|
|
2286
|
+
effortMap: {
|
|
2287
|
+
minimal: "low",
|
|
2288
|
+
low: "low",
|
|
2289
|
+
medium: "medium",
|
|
2290
|
+
high: "high",
|
|
2291
|
+
xhigh: "high"
|
|
2292
|
+
},
|
|
2293
|
+
warnings
|
|
2294
|
+
}) : void 0;
|
|
2034
2295
|
const baseArgs = {
|
|
2035
2296
|
model: this.modelId,
|
|
2036
2297
|
input,
|
|
@@ -2045,16 +2306,16 @@ var XaiResponsesLanguageModel = class {
|
|
|
2045
2306
|
format: responseFormat.schema != null ? {
|
|
2046
2307
|
type: "json_schema",
|
|
2047
2308
|
strict: true,
|
|
2048
|
-
name: (
|
|
2309
|
+
name: (_i = responseFormat.name) != null ? _i : "response",
|
|
2049
2310
|
description: responseFormat.description,
|
|
2050
2311
|
schema: responseFormat.schema
|
|
2051
2312
|
} : { type: "json_object" }
|
|
2052
2313
|
}
|
|
2053
2314
|
},
|
|
2054
|
-
...(
|
|
2315
|
+
...(resolvedReasoningEffort != null || options.reasoningSummary != null) && {
|
|
2055
2316
|
reasoning: {
|
|
2056
|
-
...
|
|
2057
|
-
effort:
|
|
2317
|
+
...resolvedReasoningEffort != null && {
|
|
2318
|
+
effort: resolvedReasoningEffort
|
|
2058
2319
|
},
|
|
2059
2320
|
...options.reasoningSummary != null && {
|
|
2060
2321
|
summary: options.reasoningSummary
|
|
@@ -2088,7 +2349,7 @@ var XaiResponsesLanguageModel = class {
|
|
|
2088
2349
|
};
|
|
2089
2350
|
}
|
|
2090
2351
|
async doGenerate(options) {
|
|
2091
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
2352
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
|
|
2092
2353
|
const {
|
|
2093
2354
|
args: body,
|
|
2094
2355
|
warnings,
|
|
@@ -2102,18 +2363,19 @@ var XaiResponsesLanguageModel = class {
|
|
|
2102
2363
|
responseHeaders,
|
|
2103
2364
|
value: response,
|
|
2104
2365
|
rawValue: rawResponse
|
|
2105
|
-
} = await (
|
|
2366
|
+
} = await postJsonToApi3({
|
|
2106
2367
|
url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/responses`,
|
|
2107
|
-
headers: (
|
|
2368
|
+
headers: combineHeaders3((_c = (_b = this.config).headers) == null ? void 0 : _c.call(_b), options.headers),
|
|
2108
2369
|
body,
|
|
2109
2370
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
2110
|
-
successfulResponseHandler: (
|
|
2371
|
+
successfulResponseHandler: createJsonResponseHandler3(
|
|
2111
2372
|
xaiResponsesResponseSchema
|
|
2112
2373
|
),
|
|
2113
2374
|
abortSignal: options.abortSignal,
|
|
2114
2375
|
fetch: this.config.fetch
|
|
2115
2376
|
});
|
|
2116
2377
|
const content = [];
|
|
2378
|
+
let hasFunctionCall = false;
|
|
2117
2379
|
const webSearchSubTools = [
|
|
2118
2380
|
"web_search",
|
|
2119
2381
|
"web_search_with_snippets",
|
|
@@ -2140,29 +2402,29 @@ var XaiResponsesLanguageModel = class {
|
|
|
2140
2402
|
toolCallId: part.id,
|
|
2141
2403
|
toolName,
|
|
2142
2404
|
result: {
|
|
2143
|
-
queries: (
|
|
2144
|
-
results: (
|
|
2405
|
+
queries: (_d = part.queries) != null ? _d : [],
|
|
2406
|
+
results: (_f = (_e = part.results) == null ? void 0 : _e.map((result) => ({
|
|
2145
2407
|
fileId: result.file_id,
|
|
2146
2408
|
filename: result.filename,
|
|
2147
2409
|
score: result.score,
|
|
2148
2410
|
text: result.text
|
|
2149
|
-
}))) != null ?
|
|
2411
|
+
}))) != null ? _f : null
|
|
2150
2412
|
}
|
|
2151
2413
|
});
|
|
2152
2414
|
continue;
|
|
2153
2415
|
}
|
|
2154
2416
|
if (part.type === "web_search_call" || part.type === "x_search_call" || part.type === "code_interpreter_call" || part.type === "code_execution_call" || part.type === "view_image_call" || part.type === "view_x_video_call" || part.type === "custom_tool_call" || part.type === "mcp_call") {
|
|
2155
|
-
let toolName = (
|
|
2156
|
-
if (webSearchSubTools.includes((
|
|
2417
|
+
let toolName = (_g = part.name) != null ? _g : "";
|
|
2418
|
+
if (webSearchSubTools.includes((_h = part.name) != null ? _h : "") || part.type === "web_search_call") {
|
|
2157
2419
|
toolName = webSearchToolName != null ? webSearchToolName : "web_search";
|
|
2158
|
-
} else if (xSearchSubTools.includes((
|
|
2420
|
+
} else if (xSearchSubTools.includes((_i = part.name) != null ? _i : "") || part.type === "x_search_call") {
|
|
2159
2421
|
toolName = xSearchToolName != null ? xSearchToolName : "x_search";
|
|
2160
2422
|
} else if (part.name === "code_execution" || part.type === "code_interpreter_call" || part.type === "code_execution_call") {
|
|
2161
2423
|
toolName = codeExecutionToolName != null ? codeExecutionToolName : "code_execution";
|
|
2162
2424
|
} else if (part.type === "mcp_call") {
|
|
2163
|
-
toolName = (
|
|
2425
|
+
toolName = (_j = mcpToolName != null ? mcpToolName : part.name) != null ? _j : "mcp";
|
|
2164
2426
|
}
|
|
2165
|
-
const toolInput = part.type === "custom_tool_call" ? (
|
|
2427
|
+
const toolInput = part.type === "custom_tool_call" ? (_k = part.input) != null ? _k : "" : part.type === "mcp_call" ? (_l = part.arguments) != null ? _l : "" : (_m = part.arguments) != null ? _m : "";
|
|
2166
2428
|
content.push({
|
|
2167
2429
|
type: "tool-call",
|
|
2168
2430
|
toolCallId: part.id,
|
|
@@ -2189,7 +2451,7 @@ var XaiResponsesLanguageModel = class {
|
|
|
2189
2451
|
sourceType: "url",
|
|
2190
2452
|
id: this.config.generateId(),
|
|
2191
2453
|
url: annotation.url,
|
|
2192
|
-
title: (
|
|
2454
|
+
title: (_n = annotation.title) != null ? _n : annotation.url
|
|
2193
2455
|
});
|
|
2194
2456
|
}
|
|
2195
2457
|
}
|
|
@@ -2198,6 +2460,7 @@ var XaiResponsesLanguageModel = class {
|
|
|
2198
2460
|
break;
|
|
2199
2461
|
}
|
|
2200
2462
|
case "function_call": {
|
|
2463
|
+
hasFunctionCall = true;
|
|
2201
2464
|
content.push({
|
|
2202
2465
|
type: "tool-call",
|
|
2203
2466
|
toolCallId: part.call_id,
|
|
@@ -2207,13 +2470,14 @@ var XaiResponsesLanguageModel = class {
|
|
|
2207
2470
|
break;
|
|
2208
2471
|
}
|
|
2209
2472
|
case "reasoning": {
|
|
2210
|
-
const texts = part.summary.length > 0 ? part.summary.map((s) => s.text) : ((
|
|
2473
|
+
const texts = part.summary.length > 0 ? part.summary.map((s) => s.text) : ((_o = part.content) != null ? _o : []).map((c) => c.text);
|
|
2211
2474
|
const reasoningText = texts.filter((text) => text && text.length > 0).join("");
|
|
2212
|
-
if (reasoningText) {
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2475
|
+
if (reasoningText || part.encrypted_content) {
|
|
2476
|
+
const hasMetadata = part.encrypted_content || part.id;
|
|
2477
|
+
content.push({
|
|
2478
|
+
type: "reasoning",
|
|
2479
|
+
text: reasoningText,
|
|
2480
|
+
...hasMetadata && {
|
|
2217
2481
|
providerMetadata: {
|
|
2218
2482
|
xai: {
|
|
2219
2483
|
...part.encrypted_content && {
|
|
@@ -2222,13 +2486,8 @@ var XaiResponsesLanguageModel = class {
|
|
|
2222
2486
|
...part.id && { itemId: part.id }
|
|
2223
2487
|
}
|
|
2224
2488
|
}
|
|
2225
|
-
}
|
|
2226
|
-
}
|
|
2227
|
-
content.push({
|
|
2228
|
-
type: "reasoning",
|
|
2229
|
-
text: reasoningText
|
|
2230
|
-
});
|
|
2231
|
-
}
|
|
2489
|
+
}
|
|
2490
|
+
});
|
|
2232
2491
|
}
|
|
2233
2492
|
break;
|
|
2234
2493
|
}
|
|
@@ -2240,13 +2499,20 @@ var XaiResponsesLanguageModel = class {
|
|
|
2240
2499
|
return {
|
|
2241
2500
|
content,
|
|
2242
2501
|
finishReason: {
|
|
2243
|
-
unified: mapXaiResponsesFinishReason(response.status),
|
|
2244
|
-
raw: (
|
|
2502
|
+
unified: hasFunctionCall ? "tool-calls" : mapXaiResponsesFinishReason(response.status),
|
|
2503
|
+
raw: (_p = response.status) != null ? _p : void 0
|
|
2245
2504
|
},
|
|
2246
2505
|
usage: response.usage ? convertXaiResponsesUsage(response.usage) : {
|
|
2247
2506
|
inputTokens: { total: 0, noCache: 0, cacheRead: 0, cacheWrite: 0 },
|
|
2248
2507
|
outputTokens: { total: 0, text: 0, reasoning: 0 }
|
|
2249
2508
|
},
|
|
2509
|
+
...((_q = response.usage) == null ? void 0 : _q.cost_in_usd_ticks) != null && {
|
|
2510
|
+
providerMetadata: {
|
|
2511
|
+
xai: {
|
|
2512
|
+
costInUsdTicks: response.usage.cost_in_usd_ticks
|
|
2513
|
+
}
|
|
2514
|
+
}
|
|
2515
|
+
},
|
|
2250
2516
|
request: { body },
|
|
2251
2517
|
response: {
|
|
2252
2518
|
...getResponseMetadata(response),
|
|
@@ -2257,7 +2523,7 @@ var XaiResponsesLanguageModel = class {
|
|
|
2257
2523
|
};
|
|
2258
2524
|
}
|
|
2259
2525
|
async doStream(options) {
|
|
2260
|
-
var _a;
|
|
2526
|
+
var _a, _b, _c;
|
|
2261
2527
|
const {
|
|
2262
2528
|
args,
|
|
2263
2529
|
warnings,
|
|
@@ -2271,12 +2537,12 @@ var XaiResponsesLanguageModel = class {
|
|
|
2271
2537
|
...args,
|
|
2272
2538
|
stream: true
|
|
2273
2539
|
};
|
|
2274
|
-
const { responseHeaders, value: response } = await (
|
|
2540
|
+
const { responseHeaders, value: response } = await postJsonToApi3({
|
|
2275
2541
|
url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/responses`,
|
|
2276
|
-
headers: (
|
|
2542
|
+
headers: combineHeaders3((_c = (_b = this.config).headers) == null ? void 0 : _c.call(_b), options.headers),
|
|
2277
2543
|
body,
|
|
2278
2544
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
2279
|
-
successfulResponseHandler: (
|
|
2545
|
+
successfulResponseHandler: createEventSourceResponseHandler2(
|
|
2280
2546
|
xaiResponsesChunkSchema
|
|
2281
2547
|
),
|
|
2282
2548
|
abortSignal: options.abortSignal,
|
|
@@ -2286,7 +2552,9 @@ var XaiResponsesLanguageModel = class {
|
|
|
2286
2552
|
unified: "other",
|
|
2287
2553
|
raw: void 0
|
|
2288
2554
|
};
|
|
2555
|
+
let hasFunctionCall = false;
|
|
2289
2556
|
let usage = void 0;
|
|
2557
|
+
let costInUsdTicks = void 0;
|
|
2290
2558
|
let isFirstChunk = true;
|
|
2291
2559
|
const contentBlocks = {};
|
|
2292
2560
|
const seenToolCalls = /* @__PURE__ */ new Set();
|
|
@@ -2300,7 +2568,7 @@ var XaiResponsesLanguageModel = class {
|
|
|
2300
2568
|
controller.enqueue({ type: "stream-start", warnings });
|
|
2301
2569
|
},
|
|
2302
2570
|
transform(chunk, controller) {
|
|
2303
|
-
var _a2,
|
|
2571
|
+
var _a2, _b2, _c2, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
|
|
2304
2572
|
if (options.includeRawChunks) {
|
|
2305
2573
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
2306
2574
|
}
|
|
@@ -2321,16 +2589,18 @@ var XaiResponsesLanguageModel = class {
|
|
|
2321
2589
|
}
|
|
2322
2590
|
if (event.type === "response.reasoning_summary_part.added") {
|
|
2323
2591
|
const blockId = `reasoning-${event.item_id}`;
|
|
2324
|
-
activeReasoning[event.item_id]
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2592
|
+
if (activeReasoning[event.item_id] == null) {
|
|
2593
|
+
activeReasoning[event.item_id] = {};
|
|
2594
|
+
controller.enqueue({
|
|
2595
|
+
type: "reasoning-start",
|
|
2596
|
+
id: blockId,
|
|
2597
|
+
providerMetadata: {
|
|
2598
|
+
xai: {
|
|
2599
|
+
itemId: event.item_id
|
|
2600
|
+
}
|
|
2331
2601
|
}
|
|
2332
|
-
}
|
|
2333
|
-
}
|
|
2602
|
+
});
|
|
2603
|
+
}
|
|
2334
2604
|
}
|
|
2335
2605
|
if (event.type === "response.reasoning_summary_text.delta") {
|
|
2336
2606
|
const blockId = `reasoning-${event.item_id}`;
|
|
@@ -2418,24 +2688,46 @@ var XaiResponsesLanguageModel = class {
|
|
|
2418
2688
|
sourceType: "url",
|
|
2419
2689
|
id: self.config.generateId(),
|
|
2420
2690
|
url: annotation.url,
|
|
2421
|
-
title: (
|
|
2691
|
+
title: (_b2 = annotation.title) != null ? _b2 : annotation.url
|
|
2422
2692
|
});
|
|
2423
2693
|
}
|
|
2424
2694
|
return;
|
|
2425
2695
|
}
|
|
2426
|
-
if (event.type === "response.done" || event.type === "response.completed") {
|
|
2696
|
+
if (event.type === "response.done" || event.type === "response.completed" || event.type === "response.incomplete") {
|
|
2427
2697
|
const response2 = event.response;
|
|
2428
2698
|
if (response2.usage) {
|
|
2429
2699
|
usage = convertXaiResponsesUsage(response2.usage);
|
|
2700
|
+
costInUsdTicks = (_c2 = response2.usage.cost_in_usd_ticks) != null ? _c2 : void 0;
|
|
2430
2701
|
}
|
|
2431
|
-
if (
|
|
2702
|
+
if (event.type === "response.incomplete") {
|
|
2703
|
+
const reason = "incomplete_details" in response2 ? (_d = response2.incomplete_details) == null ? void 0 : _d.reason : void 0;
|
|
2704
|
+
finishReason = {
|
|
2705
|
+
unified: reason ? mapXaiResponsesFinishReason(reason) : "other",
|
|
2706
|
+
raw: reason != null ? reason : "incomplete"
|
|
2707
|
+
};
|
|
2708
|
+
} else if ("status" in response2 && response2.status) {
|
|
2432
2709
|
finishReason = {
|
|
2433
|
-
unified: mapXaiResponsesFinishReason(response2.status),
|
|
2710
|
+
unified: hasFunctionCall ? "tool-calls" : mapXaiResponsesFinishReason(response2.status),
|
|
2434
2711
|
raw: response2.status
|
|
2435
2712
|
};
|
|
2436
2713
|
}
|
|
2437
2714
|
return;
|
|
2438
2715
|
}
|
|
2716
|
+
if (event.type === "response.failed") {
|
|
2717
|
+
const reason = (_e = event.response.incomplete_details) == null ? void 0 : _e.reason;
|
|
2718
|
+
finishReason = {
|
|
2719
|
+
unified: reason ? mapXaiResponsesFinishReason(reason) : "error",
|
|
2720
|
+
raw: reason != null ? reason : "error"
|
|
2721
|
+
};
|
|
2722
|
+
if (event.response.usage) {
|
|
2723
|
+
usage = convertXaiResponsesUsage(event.response.usage);
|
|
2724
|
+
}
|
|
2725
|
+
return;
|
|
2726
|
+
}
|
|
2727
|
+
if (event.type === "error") {
|
|
2728
|
+
controller.enqueue({ type: "error", error: event });
|
|
2729
|
+
return;
|
|
2730
|
+
}
|
|
2439
2731
|
if (event.type === "response.custom_tool_call_input.delta" || event.type === "response.custom_tool_call_input.done") {
|
|
2440
2732
|
return;
|
|
2441
2733
|
}
|
|
@@ -2518,13 +2810,13 @@ var XaiResponsesLanguageModel = class {
|
|
|
2518
2810
|
toolCallId: part.id,
|
|
2519
2811
|
toolName,
|
|
2520
2812
|
result: {
|
|
2521
|
-
queries: (
|
|
2522
|
-
results: (
|
|
2813
|
+
queries: (_f = part.queries) != null ? _f : [],
|
|
2814
|
+
results: (_h = (_g = part.results) == null ? void 0 : _g.map((result) => ({
|
|
2523
2815
|
fileId: result.file_id,
|
|
2524
2816
|
filename: result.filename,
|
|
2525
2817
|
score: result.score,
|
|
2526
2818
|
text: result.text
|
|
2527
|
-
}))) != null ?
|
|
2819
|
+
}))) != null ? _h : null
|
|
2528
2820
|
}
|
|
2529
2821
|
});
|
|
2530
2822
|
}
|
|
@@ -2542,17 +2834,17 @@ var XaiResponsesLanguageModel = class {
|
|
|
2542
2834
|
"x_semantic_search",
|
|
2543
2835
|
"x_thread_fetch"
|
|
2544
2836
|
];
|
|
2545
|
-
let toolName = (
|
|
2546
|
-
if (webSearchSubTools.includes((
|
|
2837
|
+
let toolName = (_i = part.name) != null ? _i : "";
|
|
2838
|
+
if (webSearchSubTools.includes((_j = part.name) != null ? _j : "") || part.type === "web_search_call") {
|
|
2547
2839
|
toolName = webSearchToolName != null ? webSearchToolName : "web_search";
|
|
2548
|
-
} else if (xSearchSubTools.includes((
|
|
2840
|
+
} else if (xSearchSubTools.includes((_k = part.name) != null ? _k : "") || part.type === "x_search_call") {
|
|
2549
2841
|
toolName = xSearchToolName != null ? xSearchToolName : "x_search";
|
|
2550
2842
|
} else if (part.name === "code_execution" || part.type === "code_interpreter_call" || part.type === "code_execution_call") {
|
|
2551
2843
|
toolName = codeExecutionToolName != null ? codeExecutionToolName : "code_execution";
|
|
2552
2844
|
} else if (part.type === "mcp_call") {
|
|
2553
|
-
toolName = (
|
|
2845
|
+
toolName = (_l = mcpToolName != null ? mcpToolName : part.name) != null ? _l : "mcp";
|
|
2554
2846
|
}
|
|
2555
|
-
const toolInput = part.type === "custom_tool_call" ? (
|
|
2847
|
+
const toolInput = part.type === "custom_tool_call" ? (_m = part.input) != null ? _m : "" : part.type === "mcp_call" ? (_n = part.arguments) != null ? _n : "" : (_o = part.arguments) != null ? _o : "";
|
|
2556
2848
|
const shouldEmit = part.type === "custom_tool_call" ? event.type === "response.output_item.done" : !seenToolCalls.has(part.id);
|
|
2557
2849
|
if (shouldEmit && !seenToolCalls.has(part.id)) {
|
|
2558
2850
|
seenToolCalls.add(part.id);
|
|
@@ -2605,7 +2897,7 @@ var XaiResponsesLanguageModel = class {
|
|
|
2605
2897
|
sourceType: "url",
|
|
2606
2898
|
id: self.config.generateId(),
|
|
2607
2899
|
url: annotation.url,
|
|
2608
|
-
title: (
|
|
2900
|
+
title: (_p = annotation.title) != null ? _p : annotation.url
|
|
2609
2901
|
});
|
|
2610
2902
|
}
|
|
2611
2903
|
}
|
|
@@ -2623,6 +2915,7 @@ var XaiResponsesLanguageModel = class {
|
|
|
2623
2915
|
toolName: part.name
|
|
2624
2916
|
});
|
|
2625
2917
|
} else if (event.type === "response.output_item.done") {
|
|
2918
|
+
hasFunctionCall = true;
|
|
2626
2919
|
ongoingToolCalls[event.output_index] = void 0;
|
|
2627
2920
|
controller.enqueue({
|
|
2628
2921
|
type: "tool-input-end",
|
|
@@ -2658,6 +2951,13 @@ var XaiResponsesLanguageModel = class {
|
|
|
2658
2951
|
cacheWrite: 0
|
|
2659
2952
|
},
|
|
2660
2953
|
outputTokens: { total: 0, text: 0, reasoning: 0 }
|
|
2954
|
+
},
|
|
2955
|
+
...costInUsdTicks != null && {
|
|
2956
|
+
providerMetadata: {
|
|
2957
|
+
xai: {
|
|
2958
|
+
costInUsdTicks
|
|
2959
|
+
}
|
|
2960
|
+
}
|
|
2661
2961
|
}
|
|
2662
2962
|
});
|
|
2663
2963
|
}
|
|
@@ -2669,45 +2969,414 @@ var XaiResponsesLanguageModel = class {
|
|
|
2669
2969
|
}
|
|
2670
2970
|
};
|
|
2671
2971
|
|
|
2972
|
+
// src/realtime/xai-realtime-event-mapper.ts
|
|
2973
|
+
function parseXaiRealtimeServerEvent(raw) {
|
|
2974
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
|
|
2975
|
+
const event = raw;
|
|
2976
|
+
const type = event.type;
|
|
2977
|
+
switch (type) {
|
|
2978
|
+
case "session.created":
|
|
2979
|
+
return {
|
|
2980
|
+
type: "session-created",
|
|
2981
|
+
sessionId: (_a = event.session) == null ? void 0 : _a.id,
|
|
2982
|
+
raw
|
|
2983
|
+
};
|
|
2984
|
+
case "session.updated":
|
|
2985
|
+
return { type: "session-updated", raw };
|
|
2986
|
+
case "conversation.created":
|
|
2987
|
+
return { type: "custom", rawType: type, raw };
|
|
2988
|
+
case "input_audio_buffer.speech_started":
|
|
2989
|
+
return {
|
|
2990
|
+
type: "speech-started",
|
|
2991
|
+
itemId: event.item_id,
|
|
2992
|
+
raw
|
|
2993
|
+
};
|
|
2994
|
+
case "input_audio_buffer.speech_stopped":
|
|
2995
|
+
return {
|
|
2996
|
+
type: "speech-stopped",
|
|
2997
|
+
itemId: event.item_id,
|
|
2998
|
+
raw
|
|
2999
|
+
};
|
|
3000
|
+
case "input_audio_buffer.committed":
|
|
3001
|
+
return {
|
|
3002
|
+
type: "audio-committed",
|
|
3003
|
+
itemId: event.item_id,
|
|
3004
|
+
previousItemId: event.previous_item_id,
|
|
3005
|
+
raw
|
|
3006
|
+
};
|
|
3007
|
+
case "conversation.item.added":
|
|
3008
|
+
return {
|
|
3009
|
+
type: "conversation-item-added",
|
|
3010
|
+
itemId: (_c = (_b = event.item) == null ? void 0 : _b.id) != null ? _c : event.item_id,
|
|
3011
|
+
item: event.item,
|
|
3012
|
+
raw
|
|
3013
|
+
};
|
|
3014
|
+
case "conversation.item.input_audio_transcription.completed":
|
|
3015
|
+
return {
|
|
3016
|
+
type: "input-transcription-completed",
|
|
3017
|
+
itemId: event.item_id,
|
|
3018
|
+
transcript: (_d = event.transcript) != null ? _d : "",
|
|
3019
|
+
raw
|
|
3020
|
+
};
|
|
3021
|
+
case "response.created":
|
|
3022
|
+
return {
|
|
3023
|
+
type: "response-created",
|
|
3024
|
+
responseId: (_f = (_e = event.response) == null ? void 0 : _e.id) != null ? _f : event.response_id,
|
|
3025
|
+
raw
|
|
3026
|
+
};
|
|
3027
|
+
case "response.done":
|
|
3028
|
+
return {
|
|
3029
|
+
type: "response-done",
|
|
3030
|
+
responseId: (_h = (_g = event.response) == null ? void 0 : _g.id) != null ? _h : event.response_id,
|
|
3031
|
+
status: (_j = (_i = event.response) == null ? void 0 : _i.status) != null ? _j : "completed",
|
|
3032
|
+
raw
|
|
3033
|
+
};
|
|
3034
|
+
case "response.output_item.added":
|
|
3035
|
+
return {
|
|
3036
|
+
type: "output-item-added",
|
|
3037
|
+
responseId: event.response_id,
|
|
3038
|
+
itemId: (_l = (_k = event.item) == null ? void 0 : _k.id) != null ? _l : event.item_id,
|
|
3039
|
+
raw
|
|
3040
|
+
};
|
|
3041
|
+
case "response.output_item.done":
|
|
3042
|
+
return {
|
|
3043
|
+
type: "output-item-done",
|
|
3044
|
+
responseId: event.response_id,
|
|
3045
|
+
itemId: (_n = (_m = event.item) == null ? void 0 : _m.id) != null ? _n : event.item_id,
|
|
3046
|
+
raw
|
|
3047
|
+
};
|
|
3048
|
+
case "response.content_part.added":
|
|
3049
|
+
return {
|
|
3050
|
+
type: "content-part-added",
|
|
3051
|
+
responseId: event.response_id,
|
|
3052
|
+
itemId: event.item_id,
|
|
3053
|
+
raw
|
|
3054
|
+
};
|
|
3055
|
+
case "response.content_part.done":
|
|
3056
|
+
return {
|
|
3057
|
+
type: "content-part-done",
|
|
3058
|
+
responseId: event.response_id,
|
|
3059
|
+
itemId: event.item_id,
|
|
3060
|
+
raw
|
|
3061
|
+
};
|
|
3062
|
+
case "response.output_audio.delta":
|
|
3063
|
+
return {
|
|
3064
|
+
type: "audio-delta",
|
|
3065
|
+
responseId: event.response_id,
|
|
3066
|
+
itemId: event.item_id,
|
|
3067
|
+
delta: event.delta,
|
|
3068
|
+
raw
|
|
3069
|
+
};
|
|
3070
|
+
case "response.output_audio.done":
|
|
3071
|
+
return {
|
|
3072
|
+
type: "audio-done",
|
|
3073
|
+
responseId: event.response_id,
|
|
3074
|
+
itemId: event.item_id,
|
|
3075
|
+
raw
|
|
3076
|
+
};
|
|
3077
|
+
case "response.output_audio_transcript.delta":
|
|
3078
|
+
return {
|
|
3079
|
+
type: "audio-transcript-delta",
|
|
3080
|
+
responseId: event.response_id,
|
|
3081
|
+
itemId: event.item_id,
|
|
3082
|
+
delta: event.delta,
|
|
3083
|
+
raw
|
|
3084
|
+
};
|
|
3085
|
+
case "response.output_audio_transcript.done":
|
|
3086
|
+
return {
|
|
3087
|
+
type: "audio-transcript-done",
|
|
3088
|
+
responseId: event.response_id,
|
|
3089
|
+
itemId: event.item_id,
|
|
3090
|
+
transcript: event.transcript,
|
|
3091
|
+
raw
|
|
3092
|
+
};
|
|
3093
|
+
case "response.text.delta":
|
|
3094
|
+
return {
|
|
3095
|
+
type: "text-delta",
|
|
3096
|
+
responseId: event.response_id,
|
|
3097
|
+
itemId: event.item_id,
|
|
3098
|
+
delta: event.delta,
|
|
3099
|
+
raw
|
|
3100
|
+
};
|
|
3101
|
+
case "response.text.done":
|
|
3102
|
+
return {
|
|
3103
|
+
type: "text-done",
|
|
3104
|
+
responseId: event.response_id,
|
|
3105
|
+
itemId: event.item_id,
|
|
3106
|
+
text: event.text,
|
|
3107
|
+
raw
|
|
3108
|
+
};
|
|
3109
|
+
case "response.function_call_arguments.delta":
|
|
3110
|
+
return {
|
|
3111
|
+
type: "function-call-arguments-delta",
|
|
3112
|
+
responseId: event.response_id,
|
|
3113
|
+
itemId: event.item_id,
|
|
3114
|
+
callId: event.call_id,
|
|
3115
|
+
delta: event.delta,
|
|
3116
|
+
raw
|
|
3117
|
+
};
|
|
3118
|
+
case "response.function_call_arguments.done":
|
|
3119
|
+
return {
|
|
3120
|
+
type: "function-call-arguments-done",
|
|
3121
|
+
responseId: event.response_id,
|
|
3122
|
+
itemId: event.item_id,
|
|
3123
|
+
callId: event.call_id,
|
|
3124
|
+
name: event.name,
|
|
3125
|
+
arguments: event.arguments,
|
|
3126
|
+
raw
|
|
3127
|
+
};
|
|
3128
|
+
case "mcp_list_tools.in_progress":
|
|
3129
|
+
case "mcp_list_tools.completed":
|
|
3130
|
+
case "mcp_list_tools.failed":
|
|
3131
|
+
case "response.mcp_call_arguments.delta":
|
|
3132
|
+
case "response.mcp_call_arguments.done":
|
|
3133
|
+
case "response.mcp_call.in_progress":
|
|
3134
|
+
case "response.mcp_call.completed":
|
|
3135
|
+
case "response.mcp_call.failed":
|
|
3136
|
+
return { type: "custom", rawType: type, raw };
|
|
3137
|
+
case "error":
|
|
3138
|
+
return {
|
|
3139
|
+
type: "error",
|
|
3140
|
+
message: (_q = (_p = (_o = event.error) == null ? void 0 : _o.message) != null ? _p : event.message) != null ? _q : "Unknown error",
|
|
3141
|
+
code: (_s = (_r = event.error) == null ? void 0 : _r.code) != null ? _s : event.code,
|
|
3142
|
+
raw
|
|
3143
|
+
};
|
|
3144
|
+
default:
|
|
3145
|
+
return { type: "custom", rawType: type, raw };
|
|
3146
|
+
}
|
|
3147
|
+
}
|
|
3148
|
+
function serializeXaiRealtimeClientEvent(event) {
|
|
3149
|
+
switch (event.type) {
|
|
3150
|
+
case "session-update":
|
|
3151
|
+
return {
|
|
3152
|
+
type: "session.update",
|
|
3153
|
+
session: buildXaiSessionConfig(event.config)
|
|
3154
|
+
};
|
|
3155
|
+
case "input-audio-append":
|
|
3156
|
+
return {
|
|
3157
|
+
type: "input_audio_buffer.append",
|
|
3158
|
+
audio: event.audio
|
|
3159
|
+
};
|
|
3160
|
+
case "input-audio-commit":
|
|
3161
|
+
return { type: "input_audio_buffer.commit" };
|
|
3162
|
+
case "input-audio-clear":
|
|
3163
|
+
return { type: "input_audio_buffer.clear" };
|
|
3164
|
+
case "conversation-item-create": {
|
|
3165
|
+
const item = event.item;
|
|
3166
|
+
switch (item.type) {
|
|
3167
|
+
case "text-message":
|
|
3168
|
+
return {
|
|
3169
|
+
type: "conversation.item.create",
|
|
3170
|
+
item: {
|
|
3171
|
+
type: "message",
|
|
3172
|
+
role: item.role,
|
|
3173
|
+
content: [{ type: "input_text", text: item.text }]
|
|
3174
|
+
}
|
|
3175
|
+
};
|
|
3176
|
+
case "audio-message":
|
|
3177
|
+
return {
|
|
3178
|
+
type: "conversation.item.create",
|
|
3179
|
+
item: {
|
|
3180
|
+
type: "message",
|
|
3181
|
+
role: item.role,
|
|
3182
|
+
content: [{ type: "input_audio", audio: item.audio }]
|
|
3183
|
+
}
|
|
3184
|
+
};
|
|
3185
|
+
case "function-call-output":
|
|
3186
|
+
return {
|
|
3187
|
+
type: "conversation.item.create",
|
|
3188
|
+
item: {
|
|
3189
|
+
type: "function_call_output",
|
|
3190
|
+
call_id: item.callId,
|
|
3191
|
+
output: item.output
|
|
3192
|
+
}
|
|
3193
|
+
};
|
|
3194
|
+
}
|
|
3195
|
+
break;
|
|
3196
|
+
}
|
|
3197
|
+
case "conversation-item-truncate":
|
|
3198
|
+
return void 0;
|
|
3199
|
+
case "response-create":
|
|
3200
|
+
return {
|
|
3201
|
+
type: "response.create",
|
|
3202
|
+
...event.options != null ? {
|
|
3203
|
+
response: {
|
|
3204
|
+
...event.options.modalities != null ? { modalities: event.options.modalities } : {},
|
|
3205
|
+
...event.options.instructions != null ? { instructions: event.options.instructions } : {}
|
|
3206
|
+
}
|
|
3207
|
+
} : {}
|
|
3208
|
+
};
|
|
3209
|
+
case "response-cancel":
|
|
3210
|
+
return { type: "response.cancel" };
|
|
3211
|
+
}
|
|
3212
|
+
}
|
|
3213
|
+
function buildXaiSessionConfig(config) {
|
|
3214
|
+
var _a;
|
|
3215
|
+
const session = {};
|
|
3216
|
+
if (config.instructions != null) {
|
|
3217
|
+
session.instructions = config.instructions;
|
|
3218
|
+
}
|
|
3219
|
+
if (config.voice != null) {
|
|
3220
|
+
session.voice = config.voice;
|
|
3221
|
+
}
|
|
3222
|
+
const audio = {};
|
|
3223
|
+
if (config.inputAudioFormat != null) {
|
|
3224
|
+
audio.input = {
|
|
3225
|
+
format: {
|
|
3226
|
+
type: config.inputAudioFormat.type,
|
|
3227
|
+
...config.inputAudioFormat.rate != null ? { rate: config.inputAudioFormat.rate } : {}
|
|
3228
|
+
}
|
|
3229
|
+
};
|
|
3230
|
+
}
|
|
3231
|
+
if (config.outputAudioFormat != null) {
|
|
3232
|
+
audio.output = {
|
|
3233
|
+
format: {
|
|
3234
|
+
type: config.outputAudioFormat.type,
|
|
3235
|
+
...config.outputAudioFormat.rate != null ? { rate: config.outputAudioFormat.rate } : {}
|
|
3236
|
+
}
|
|
3237
|
+
};
|
|
3238
|
+
}
|
|
3239
|
+
if (Object.keys(audio).length > 0) {
|
|
3240
|
+
session.audio = audio;
|
|
3241
|
+
}
|
|
3242
|
+
if (config.turnDetection != null) {
|
|
3243
|
+
if (config.turnDetection.type === "disabled") {
|
|
3244
|
+
session.turn_detection = null;
|
|
3245
|
+
} else {
|
|
3246
|
+
const td = {
|
|
3247
|
+
type: "server_vad"
|
|
3248
|
+
};
|
|
3249
|
+
if (config.turnDetection.threshold != null) {
|
|
3250
|
+
td.threshold = config.turnDetection.threshold;
|
|
3251
|
+
}
|
|
3252
|
+
if (config.turnDetection.silenceDurationMs != null) {
|
|
3253
|
+
td.silence_duration_ms = config.turnDetection.silenceDurationMs;
|
|
3254
|
+
}
|
|
3255
|
+
if (config.turnDetection.prefixPaddingMs != null) {
|
|
3256
|
+
td.prefix_padding_ms = config.turnDetection.prefixPaddingMs;
|
|
3257
|
+
}
|
|
3258
|
+
session.turn_detection = td;
|
|
3259
|
+
}
|
|
3260
|
+
}
|
|
3261
|
+
if (config.tools != null && config.tools.length > 0) {
|
|
3262
|
+
session.tools = config.tools.map((tool) => ({
|
|
3263
|
+
type: tool.type,
|
|
3264
|
+
name: tool.name,
|
|
3265
|
+
description: tool.description,
|
|
3266
|
+
parameters: tool.parameters
|
|
3267
|
+
}));
|
|
3268
|
+
}
|
|
3269
|
+
if (config.providerOptions != null) {
|
|
3270
|
+
const xaiOptions = config.providerOptions;
|
|
3271
|
+
if (Array.isArray(xaiOptions.tools)) {
|
|
3272
|
+
const existingTools = (_a = session.tools) != null ? _a : [];
|
|
3273
|
+
session.tools = [...existingTools, ...xaiOptions.tools];
|
|
3274
|
+
}
|
|
3275
|
+
for (const [key, value] of Object.entries(xaiOptions)) {
|
|
3276
|
+
if (key !== "tools") {
|
|
3277
|
+
session[key] = value;
|
|
3278
|
+
}
|
|
3279
|
+
}
|
|
3280
|
+
}
|
|
3281
|
+
return session;
|
|
3282
|
+
}
|
|
3283
|
+
|
|
3284
|
+
// src/realtime/xai-realtime-model.ts
|
|
3285
|
+
var XaiRealtimeModel = class {
|
|
3286
|
+
constructor(modelId, config) {
|
|
3287
|
+
this.specificationVersion = "v4";
|
|
3288
|
+
this.modelId = modelId;
|
|
3289
|
+
this.provider = config.provider;
|
|
3290
|
+
this.config = config;
|
|
3291
|
+
}
|
|
3292
|
+
async doCreateClientSecret(options) {
|
|
3293
|
+
var _a;
|
|
3294
|
+
const fetchFn = (_a = this.config.fetch) != null ? _a : fetch;
|
|
3295
|
+
const url = `${this.config.baseURL}/realtime/client_secrets`;
|
|
3296
|
+
const body = {};
|
|
3297
|
+
if (options.expiresAfterSeconds != null) {
|
|
3298
|
+
body.expires_after = { seconds: options.expiresAfterSeconds };
|
|
3299
|
+
}
|
|
3300
|
+
const response = await fetchFn(url, {
|
|
3301
|
+
method: "POST",
|
|
3302
|
+
headers: {
|
|
3303
|
+
...this.config.headers(),
|
|
3304
|
+
"Content-Type": "application/json"
|
|
3305
|
+
},
|
|
3306
|
+
body: JSON.stringify(body)
|
|
3307
|
+
});
|
|
3308
|
+
if (!response.ok) {
|
|
3309
|
+
const text = await response.text();
|
|
3310
|
+
throw new Error(
|
|
3311
|
+
`xAI realtime client secret request failed: ${response.status} ${text}`
|
|
3312
|
+
);
|
|
3313
|
+
}
|
|
3314
|
+
const data = await response.json();
|
|
3315
|
+
return {
|
|
3316
|
+
token: data.value,
|
|
3317
|
+
// xAI selects the voice model from the `model` query parameter on the
|
|
3318
|
+
// WebSocket URL. Without it the model choice is silently ignored and the
|
|
3319
|
+
// server falls back to its default voice model.
|
|
3320
|
+
url: `wss://${new URL(this.config.baseURL).host}/v1/realtime?model=${encodeURIComponent(this.modelId)}`,
|
|
3321
|
+
expiresAt: data.expires_at
|
|
3322
|
+
};
|
|
3323
|
+
}
|
|
3324
|
+
getWebSocketConfig(options) {
|
|
3325
|
+
return {
|
|
3326
|
+
url: options.url,
|
|
3327
|
+
protocols: [`xai-client-secret.${options.token}`]
|
|
3328
|
+
};
|
|
3329
|
+
}
|
|
3330
|
+
parseServerEvent(raw) {
|
|
3331
|
+
return parseXaiRealtimeServerEvent(raw);
|
|
3332
|
+
}
|
|
3333
|
+
serializeClientEvent(event) {
|
|
3334
|
+
return serializeXaiRealtimeClientEvent(event);
|
|
3335
|
+
}
|
|
3336
|
+
buildSessionConfig(config) {
|
|
3337
|
+
return buildXaiSessionConfig(config);
|
|
3338
|
+
}
|
|
3339
|
+
};
|
|
3340
|
+
|
|
2672
3341
|
// src/tool/code-execution.ts
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
var codeExecutionOutputSchema =
|
|
2676
|
-
output:
|
|
2677
|
-
error:
|
|
3342
|
+
import { createProviderExecutedToolFactory as createProviderExecutedToolFactory5 } from "@ai-sdk/provider-utils";
|
|
3343
|
+
import { z as z12 } from "zod/v4";
|
|
3344
|
+
var codeExecutionOutputSchema = z12.object({
|
|
3345
|
+
output: z12.string().describe("the output of the code execution"),
|
|
3346
|
+
error: z12.string().optional().describe("any error that occurred")
|
|
2678
3347
|
});
|
|
2679
|
-
var codeExecutionToolFactory = (
|
|
3348
|
+
var codeExecutionToolFactory = createProviderExecutedToolFactory5({
|
|
2680
3349
|
id: "xai.code_execution",
|
|
2681
|
-
inputSchema:
|
|
3350
|
+
inputSchema: z12.object({}).describe("no input parameters"),
|
|
2682
3351
|
outputSchema: codeExecutionOutputSchema
|
|
2683
3352
|
});
|
|
2684
3353
|
var codeExecution = (args = {}) => codeExecutionToolFactory(args);
|
|
2685
3354
|
|
|
2686
3355
|
// src/tool/view-image.ts
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
var viewImageOutputSchema =
|
|
2690
|
-
description:
|
|
2691
|
-
objects:
|
|
3356
|
+
import { createProviderExecutedToolFactory as createProviderExecutedToolFactory6 } from "@ai-sdk/provider-utils";
|
|
3357
|
+
import { z as z13 } from "zod/v4";
|
|
3358
|
+
var viewImageOutputSchema = z13.object({
|
|
3359
|
+
description: z13.string().describe("description of the image"),
|
|
3360
|
+
objects: z13.array(z13.string()).optional().describe("objects detected in the image")
|
|
2692
3361
|
});
|
|
2693
|
-
var viewImageToolFactory = (
|
|
3362
|
+
var viewImageToolFactory = createProviderExecutedToolFactory6({
|
|
2694
3363
|
id: "xai.view_image",
|
|
2695
|
-
inputSchema:
|
|
3364
|
+
inputSchema: z13.object({}).describe("no input parameters"),
|
|
2696
3365
|
outputSchema: viewImageOutputSchema
|
|
2697
3366
|
});
|
|
2698
3367
|
var viewImage = (args = {}) => viewImageToolFactory(args);
|
|
2699
3368
|
|
|
2700
3369
|
// src/tool/view-x-video.ts
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
var viewXVideoOutputSchema =
|
|
2704
|
-
transcript:
|
|
2705
|
-
description:
|
|
2706
|
-
duration:
|
|
3370
|
+
import { createProviderExecutedToolFactory as createProviderExecutedToolFactory7 } from "@ai-sdk/provider-utils";
|
|
3371
|
+
import { z as z14 } from "zod/v4";
|
|
3372
|
+
var viewXVideoOutputSchema = z14.object({
|
|
3373
|
+
transcript: z14.string().optional().describe("transcript of the video"),
|
|
3374
|
+
description: z14.string().describe("description of the video content"),
|
|
3375
|
+
duration: z14.number().optional().describe("duration in seconds")
|
|
2707
3376
|
});
|
|
2708
|
-
var viewXVideoToolFactory = (
|
|
3377
|
+
var viewXVideoToolFactory = createProviderExecutedToolFactory7({
|
|
2709
3378
|
id: "xai.view_x_video",
|
|
2710
|
-
inputSchema:
|
|
3379
|
+
inputSchema: z14.object({}).describe("no input parameters"),
|
|
2711
3380
|
outputSchema: viewXVideoOutputSchema
|
|
2712
3381
|
});
|
|
2713
3382
|
var viewXVideo = (args = {}) => viewXVideoToolFactory(args);
|
|
@@ -2724,53 +3393,216 @@ var xaiTools = {
|
|
|
2724
3393
|
};
|
|
2725
3394
|
|
|
2726
3395
|
// src/version.ts
|
|
2727
|
-
var VERSION = true ? "4.0.0-beta.
|
|
3396
|
+
var VERSION = true ? "4.0.0-beta.75" : "0.0.0-test";
|
|
2728
3397
|
|
|
2729
|
-
// src/xai-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
3398
|
+
// src/files/xai-files.ts
|
|
3399
|
+
import {
|
|
3400
|
+
combineHeaders as combineHeaders4,
|
|
3401
|
+
convertInlineFileDataToUint8Array,
|
|
3402
|
+
createJsonResponseHandler as createJsonResponseHandler4,
|
|
3403
|
+
parseProviderOptions as parseProviderOptions4,
|
|
3404
|
+
postFormDataToApi
|
|
3405
|
+
} from "@ai-sdk/provider-utils";
|
|
3406
|
+
|
|
3407
|
+
// src/files/xai-files-api.ts
|
|
3408
|
+
import { lazySchema as lazySchema5, zodSchema as zodSchema5 } from "@ai-sdk/provider-utils";
|
|
3409
|
+
import { z as z15 } from "zod/v4";
|
|
3410
|
+
var xaiFilesResponseSchema = lazySchema5(
|
|
3411
|
+
() => zodSchema5(
|
|
3412
|
+
z15.object({
|
|
3413
|
+
id: z15.string(),
|
|
3414
|
+
object: z15.string().nullish(),
|
|
3415
|
+
bytes: z15.number().nullish(),
|
|
3416
|
+
created_at: z15.number().nullish(),
|
|
3417
|
+
filename: z15.string().nullish(),
|
|
3418
|
+
purpose: z15.string().nullish(),
|
|
3419
|
+
status: z15.string().nullish()
|
|
3420
|
+
})
|
|
3421
|
+
)
|
|
3422
|
+
);
|
|
2733
3423
|
|
|
2734
|
-
// src/xai-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
3424
|
+
// src/files/xai-files-options.ts
|
|
3425
|
+
import {
|
|
3426
|
+
lazySchema as lazySchema6,
|
|
3427
|
+
zodSchema as zodSchema6
|
|
3428
|
+
} from "@ai-sdk/provider-utils";
|
|
3429
|
+
import { z as z16 } from "zod/v4";
|
|
3430
|
+
var xaiFilesOptionsSchema = lazySchema6(
|
|
3431
|
+
() => zodSchema6(
|
|
3432
|
+
z16.object({
|
|
3433
|
+
teamId: z16.string().optional(),
|
|
3434
|
+
filePath: z16.string().optional()
|
|
2744
3435
|
}).passthrough()
|
|
2745
3436
|
)
|
|
2746
3437
|
);
|
|
2747
3438
|
|
|
3439
|
+
// src/files/xai-files.ts
|
|
3440
|
+
var XaiFiles = class {
|
|
3441
|
+
constructor(config) {
|
|
3442
|
+
this.config = config;
|
|
3443
|
+
this.specificationVersion = "v4";
|
|
3444
|
+
}
|
|
3445
|
+
get provider() {
|
|
3446
|
+
return this.config.provider;
|
|
3447
|
+
}
|
|
3448
|
+
async uploadFile({
|
|
3449
|
+
data,
|
|
3450
|
+
mediaType,
|
|
3451
|
+
filename,
|
|
3452
|
+
providerOptions
|
|
3453
|
+
}) {
|
|
3454
|
+
var _a, _b;
|
|
3455
|
+
const xaiOptions = await parseProviderOptions4({
|
|
3456
|
+
provider: "xai",
|
|
3457
|
+
providerOptions,
|
|
3458
|
+
schema: xaiFilesOptionsSchema
|
|
3459
|
+
});
|
|
3460
|
+
const fileBytes = convertInlineFileDataToUint8Array(data);
|
|
3461
|
+
const blob = new Blob([fileBytes], {
|
|
3462
|
+
type: mediaType
|
|
3463
|
+
});
|
|
3464
|
+
const formData = new FormData();
|
|
3465
|
+
if (filename != null) {
|
|
3466
|
+
formData.append("file", blob, filename);
|
|
3467
|
+
} else {
|
|
3468
|
+
formData.append("file", blob);
|
|
3469
|
+
}
|
|
3470
|
+
if ((xaiOptions == null ? void 0 : xaiOptions.teamId) != null) {
|
|
3471
|
+
formData.append("team_id", xaiOptions.teamId);
|
|
3472
|
+
}
|
|
3473
|
+
const { value: response } = await postFormDataToApi({
|
|
3474
|
+
url: `${this.config.baseURL}/files`,
|
|
3475
|
+
headers: combineHeaders4(this.config.headers()),
|
|
3476
|
+
formData,
|
|
3477
|
+
failedResponseHandler: xaiFailedResponseHandler,
|
|
3478
|
+
successfulResponseHandler: createJsonResponseHandler4(
|
|
3479
|
+
xaiFilesResponseSchema
|
|
3480
|
+
),
|
|
3481
|
+
fetch: this.config.fetch
|
|
3482
|
+
});
|
|
3483
|
+
return {
|
|
3484
|
+
warnings: [],
|
|
3485
|
+
providerReference: { xai: response.id },
|
|
3486
|
+
...((_a = response.filename) != null ? _a : filename) ? { filename: (_b = response.filename) != null ? _b : filename } : {},
|
|
3487
|
+
...mediaType != null ? { mediaType } : {},
|
|
3488
|
+
providerMetadata: {
|
|
3489
|
+
xai: {
|
|
3490
|
+
...response.filename != null ? { filename: response.filename } : {},
|
|
3491
|
+
...response.bytes != null ? { bytes: response.bytes } : {},
|
|
3492
|
+
...response.created_at != null ? { createdAt: response.created_at } : {}
|
|
3493
|
+
}
|
|
3494
|
+
}
|
|
3495
|
+
};
|
|
3496
|
+
}
|
|
3497
|
+
};
|
|
3498
|
+
|
|
3499
|
+
// src/xai-video-model.ts
|
|
3500
|
+
import {
|
|
3501
|
+
AISDKError
|
|
3502
|
+
} from "@ai-sdk/provider";
|
|
3503
|
+
import {
|
|
3504
|
+
combineHeaders as combineHeaders5,
|
|
3505
|
+
convertUint8ArrayToBase64,
|
|
3506
|
+
createJsonResponseHandler as createJsonResponseHandler5,
|
|
3507
|
+
delay,
|
|
3508
|
+
getFromApi as getFromApi2,
|
|
3509
|
+
parseProviderOptions as parseProviderOptions5,
|
|
3510
|
+
postJsonToApi as postJsonToApi4
|
|
3511
|
+
} from "@ai-sdk/provider-utils";
|
|
3512
|
+
import { z as z18 } from "zod/v4";
|
|
3513
|
+
|
|
3514
|
+
// src/xai-video-model-options.ts
|
|
3515
|
+
import { lazySchema as lazySchema7, zodSchema as zodSchema7 } from "@ai-sdk/provider-utils";
|
|
3516
|
+
import { z as z17 } from "zod/v4";
|
|
3517
|
+
var nonEmptyStringSchema = z17.string().min(1);
|
|
3518
|
+
var resolutionSchema = z17.enum(["480p", "720p"]);
|
|
3519
|
+
var modeSchema = z17.enum(["edit-video", "extend-video", "reference-to-video"]);
|
|
3520
|
+
var baseFields = {
|
|
3521
|
+
pollIntervalMs: z17.number().positive().nullish(),
|
|
3522
|
+
pollTimeoutMs: z17.number().positive().nullish(),
|
|
3523
|
+
resolution: resolutionSchema.nullish()
|
|
3524
|
+
};
|
|
3525
|
+
var editVideoSchema = z17.object({
|
|
3526
|
+
...baseFields,
|
|
3527
|
+
mode: z17.literal("edit-video"),
|
|
3528
|
+
videoUrl: nonEmptyStringSchema,
|
|
3529
|
+
referenceImageUrls: z17.undefined().optional()
|
|
3530
|
+
});
|
|
3531
|
+
var extendVideoSchema = z17.object({
|
|
3532
|
+
...baseFields,
|
|
3533
|
+
mode: z17.literal("extend-video"),
|
|
3534
|
+
videoUrl: nonEmptyStringSchema,
|
|
3535
|
+
referenceImageUrls: z17.undefined().optional()
|
|
3536
|
+
});
|
|
3537
|
+
var referenceToVideoSchema = z17.object({
|
|
3538
|
+
...baseFields,
|
|
3539
|
+
mode: z17.literal("reference-to-video"),
|
|
3540
|
+
referenceImageUrls: z17.array(nonEmptyStringSchema).min(1).max(7),
|
|
3541
|
+
videoUrl: z17.undefined().optional()
|
|
3542
|
+
});
|
|
3543
|
+
var autoDetectSchema = z17.object({
|
|
3544
|
+
...baseFields,
|
|
3545
|
+
mode: z17.undefined().optional(),
|
|
3546
|
+
videoUrl: nonEmptyStringSchema.optional(),
|
|
3547
|
+
referenceImageUrls: z17.array(nonEmptyStringSchema).min(1).max(7).optional()
|
|
3548
|
+
});
|
|
3549
|
+
var xaiVideoModelOptions = z17.union([
|
|
3550
|
+
editVideoSchema,
|
|
3551
|
+
extendVideoSchema,
|
|
3552
|
+
referenceToVideoSchema,
|
|
3553
|
+
autoDetectSchema
|
|
3554
|
+
]);
|
|
3555
|
+
var runtimeSchema = z17.object({
|
|
3556
|
+
mode: modeSchema.optional(),
|
|
3557
|
+
videoUrl: nonEmptyStringSchema.optional(),
|
|
3558
|
+
referenceImageUrls: z17.array(nonEmptyStringSchema).min(1).max(7).optional(),
|
|
3559
|
+
...baseFields
|
|
3560
|
+
}).passthrough();
|
|
3561
|
+
var xaiVideoModelOptionsSchema = lazySchema7(
|
|
3562
|
+
() => zodSchema7(runtimeSchema)
|
|
3563
|
+
);
|
|
3564
|
+
|
|
2748
3565
|
// src/xai-video-model.ts
|
|
2749
3566
|
var RESOLUTION_MAP = {
|
|
2750
3567
|
"1280x720": "720p",
|
|
2751
3568
|
"854x480": "480p",
|
|
2752
3569
|
"640x480": "480p"
|
|
2753
3570
|
};
|
|
3571
|
+
function resolveVideoMode(options) {
|
|
3572
|
+
if ((options == null ? void 0 : options.mode) != null) {
|
|
3573
|
+
return options.mode;
|
|
3574
|
+
}
|
|
3575
|
+
if ((options == null ? void 0 : options.videoUrl) != null) {
|
|
3576
|
+
return "edit-video";
|
|
3577
|
+
}
|
|
3578
|
+
if ((options == null ? void 0 : options.referenceImageUrls) != null && options.referenceImageUrls.length > 0) {
|
|
3579
|
+
return "reference-to-video";
|
|
3580
|
+
}
|
|
3581
|
+
return void 0;
|
|
3582
|
+
}
|
|
2754
3583
|
var XaiVideoModel = class {
|
|
2755
3584
|
constructor(modelId, config) {
|
|
2756
3585
|
this.modelId = modelId;
|
|
2757
3586
|
this.config = config;
|
|
2758
|
-
this.specificationVersion = "
|
|
3587
|
+
this.specificationVersion = "v4";
|
|
2759
3588
|
this.maxVideosPerCall = 1;
|
|
2760
3589
|
}
|
|
2761
3590
|
get provider() {
|
|
2762
3591
|
return this.config.provider;
|
|
2763
3592
|
}
|
|
2764
3593
|
async doGenerate(options) {
|
|
2765
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
3594
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
2766
3595
|
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
2767
3596
|
const warnings = [];
|
|
2768
|
-
const xaiOptions = await (
|
|
3597
|
+
const xaiOptions = await parseProviderOptions5({
|
|
2769
3598
|
provider: "xai",
|
|
2770
3599
|
providerOptions: options.providerOptions,
|
|
2771
3600
|
schema: xaiVideoModelOptionsSchema
|
|
2772
3601
|
});
|
|
2773
|
-
const
|
|
3602
|
+
const effectiveMode = resolveVideoMode(xaiOptions);
|
|
3603
|
+
const isEdit = effectiveMode === "edit-video";
|
|
3604
|
+
const isExtension = effectiveMode === "extend-video";
|
|
3605
|
+
const hasReferenceImages = effectiveMode === "reference-to-video";
|
|
2774
3606
|
if (options.fps != null) {
|
|
2775
3607
|
warnings.push({
|
|
2776
3608
|
type: "unsupported",
|
|
@@ -2813,19 +3645,36 @@ var XaiVideoModel = class {
|
|
|
2813
3645
|
details: "xAI video editing does not support custom resolution."
|
|
2814
3646
|
});
|
|
2815
3647
|
}
|
|
3648
|
+
if (isExtension && options.aspectRatio != null) {
|
|
3649
|
+
warnings.push({
|
|
3650
|
+
type: "unsupported",
|
|
3651
|
+
feature: "aspectRatio",
|
|
3652
|
+
details: "xAI video extension does not support custom aspect ratio."
|
|
3653
|
+
});
|
|
3654
|
+
}
|
|
3655
|
+
if (isExtension && ((xaiOptions == null ? void 0 : xaiOptions.resolution) != null || options.resolution != null)) {
|
|
3656
|
+
warnings.push({
|
|
3657
|
+
type: "unsupported",
|
|
3658
|
+
feature: "resolution",
|
|
3659
|
+
details: "xAI video extension does not support custom resolution."
|
|
3660
|
+
});
|
|
3661
|
+
}
|
|
2816
3662
|
const body = {
|
|
2817
3663
|
model: this.modelId,
|
|
2818
3664
|
prompt: options.prompt
|
|
2819
3665
|
};
|
|
2820
|
-
|
|
3666
|
+
const allowDuration = !isEdit;
|
|
3667
|
+
const allowAspectRatio = !isEdit && !isExtension;
|
|
3668
|
+
const allowResolution = !isEdit && !isExtension;
|
|
3669
|
+
if (allowDuration && options.duration != null) {
|
|
2821
3670
|
body.duration = options.duration;
|
|
2822
3671
|
}
|
|
2823
|
-
if (
|
|
3672
|
+
if (allowAspectRatio && options.aspectRatio != null) {
|
|
2824
3673
|
body.aspect_ratio = options.aspectRatio;
|
|
2825
3674
|
}
|
|
2826
|
-
if (
|
|
3675
|
+
if (allowResolution && (xaiOptions == null ? void 0 : xaiOptions.resolution) != null) {
|
|
2827
3676
|
body.resolution = xaiOptions.resolution;
|
|
2828
|
-
} else if (
|
|
3677
|
+
} else if (allowResolution && options.resolution != null) {
|
|
2829
3678
|
const mapped = RESOLUTION_MAP[options.resolution];
|
|
2830
3679
|
if (mapped != null) {
|
|
2831
3680
|
body.resolution = mapped;
|
|
@@ -2837,38 +3686,56 @@ var XaiVideoModel = class {
|
|
|
2837
3686
|
});
|
|
2838
3687
|
}
|
|
2839
3688
|
}
|
|
2840
|
-
if (
|
|
3689
|
+
if (isEdit) {
|
|
3690
|
+
body.video = { url: xaiOptions.videoUrl };
|
|
3691
|
+
}
|
|
3692
|
+
if (isExtension) {
|
|
2841
3693
|
body.video = { url: xaiOptions.videoUrl };
|
|
2842
3694
|
}
|
|
2843
3695
|
if (options.image != null) {
|
|
2844
3696
|
if (options.image.type === "url") {
|
|
2845
3697
|
body.image = { url: options.image.url };
|
|
2846
3698
|
} else {
|
|
2847
|
-
const base64Data = typeof options.image.data === "string" ? options.image.data :
|
|
3699
|
+
const base64Data = typeof options.image.data === "string" ? options.image.data : convertUint8ArrayToBase64(options.image.data);
|
|
2848
3700
|
body.image = {
|
|
2849
3701
|
url: `data:${options.image.mediaType};base64,${base64Data}`
|
|
2850
3702
|
};
|
|
2851
3703
|
}
|
|
2852
3704
|
}
|
|
3705
|
+
if (hasReferenceImages) {
|
|
3706
|
+
body.reference_images = xaiOptions.referenceImageUrls.map((url) => ({
|
|
3707
|
+
url
|
|
3708
|
+
}));
|
|
3709
|
+
}
|
|
2853
3710
|
if (xaiOptions != null) {
|
|
2854
3711
|
for (const [key, value] of Object.entries(xaiOptions)) {
|
|
2855
3712
|
if (![
|
|
3713
|
+
"mode",
|
|
2856
3714
|
"pollIntervalMs",
|
|
2857
3715
|
"pollTimeoutMs",
|
|
2858
3716
|
"resolution",
|
|
2859
|
-
"videoUrl"
|
|
3717
|
+
"videoUrl",
|
|
3718
|
+
"referenceImageUrls"
|
|
2860
3719
|
].includes(key)) {
|
|
2861
3720
|
body[key] = value;
|
|
2862
3721
|
}
|
|
2863
3722
|
}
|
|
2864
3723
|
}
|
|
2865
3724
|
const baseURL = (_d = this.config.baseURL) != null ? _d : "https://api.x.ai/v1";
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
3725
|
+
let endpoint;
|
|
3726
|
+
if (isEdit) {
|
|
3727
|
+
endpoint = `${baseURL}/videos/edits`;
|
|
3728
|
+
} else if (isExtension) {
|
|
3729
|
+
endpoint = `${baseURL}/videos/extensions`;
|
|
3730
|
+
} else {
|
|
3731
|
+
endpoint = `${baseURL}/videos/generations`;
|
|
3732
|
+
}
|
|
3733
|
+
const { value: createResponse } = await postJsonToApi4({
|
|
3734
|
+
url: endpoint,
|
|
3735
|
+
headers: combineHeaders5(this.config.headers(), options.headers),
|
|
2869
3736
|
body,
|
|
2870
3737
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
2871
|
-
successfulResponseHandler: (
|
|
3738
|
+
successfulResponseHandler: createJsonResponseHandler5(
|
|
2872
3739
|
xaiCreateVideoResponseSchema
|
|
2873
3740
|
),
|
|
2874
3741
|
abortSignal: options.abortSignal,
|
|
@@ -2876,7 +3743,7 @@ var XaiVideoModel = class {
|
|
|
2876
3743
|
});
|
|
2877
3744
|
const requestId = createResponse.request_id;
|
|
2878
3745
|
if (!requestId) {
|
|
2879
|
-
throw new
|
|
3746
|
+
throw new AISDKError({
|
|
2880
3747
|
name: "XAI_VIDEO_GENERATION_ERROR",
|
|
2881
3748
|
message: `No request_id returned from xAI API. Response: ${JSON.stringify(createResponse)}`
|
|
2882
3749
|
});
|
|
@@ -2886,17 +3753,17 @@ var XaiVideoModel = class {
|
|
|
2886
3753
|
const startTime = Date.now();
|
|
2887
3754
|
let responseHeaders;
|
|
2888
3755
|
while (true) {
|
|
2889
|
-
await
|
|
3756
|
+
await delay(pollIntervalMs, { abortSignal: options.abortSignal });
|
|
2890
3757
|
if (Date.now() - startTime > pollTimeoutMs) {
|
|
2891
|
-
throw new
|
|
3758
|
+
throw new AISDKError({
|
|
2892
3759
|
name: "XAI_VIDEO_GENERATION_TIMEOUT",
|
|
2893
3760
|
message: `Video generation timed out after ${pollTimeoutMs}ms`
|
|
2894
3761
|
});
|
|
2895
3762
|
}
|
|
2896
|
-
const { value: statusResponse, responseHeaders: pollHeaders } = await (
|
|
3763
|
+
const { value: statusResponse, responseHeaders: pollHeaders } = await getFromApi2({
|
|
2897
3764
|
url: `${baseURL}/videos/${requestId}`,
|
|
2898
|
-
headers: (
|
|
2899
|
-
successfulResponseHandler: (
|
|
3765
|
+
headers: combineHeaders5(this.config.headers(), options.headers),
|
|
3766
|
+
successfulResponseHandler: createJsonResponseHandler5(
|
|
2900
3767
|
xaiVideoStatusResponseSchema
|
|
2901
3768
|
),
|
|
2902
3769
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
@@ -2905,8 +3772,14 @@ var XaiVideoModel = class {
|
|
|
2905
3772
|
});
|
|
2906
3773
|
responseHeaders = pollHeaders;
|
|
2907
3774
|
if (statusResponse.status === "done" || statusResponse.status == null && ((_g = statusResponse.video) == null ? void 0 : _g.url)) {
|
|
2908
|
-
if (
|
|
2909
|
-
throw new
|
|
3775
|
+
if (((_h = statusResponse.video) == null ? void 0 : _h.respect_moderation) === false) {
|
|
3776
|
+
throw new AISDKError({
|
|
3777
|
+
name: "XAI_VIDEO_MODERATION_ERROR",
|
|
3778
|
+
message: "Video generation was blocked due to a content policy violation."
|
|
3779
|
+
});
|
|
3780
|
+
}
|
|
3781
|
+
if (!((_i = statusResponse.video) == null ? void 0 : _i.url)) {
|
|
3782
|
+
throw new AISDKError({
|
|
2910
3783
|
name: "XAI_VIDEO_GENERATION_ERROR",
|
|
2911
3784
|
message: "Video generation completed but no video URL was returned."
|
|
2912
3785
|
});
|
|
@@ -2929,42 +3802,409 @@ var XaiVideoModel = class {
|
|
|
2929
3802
|
xai: {
|
|
2930
3803
|
requestId,
|
|
2931
3804
|
videoUrl: statusResponse.video.url,
|
|
2932
|
-
...statusResponse.video.duration != null ? { duration: statusResponse.video.duration } : {}
|
|
3805
|
+
...statusResponse.video.duration != null ? { duration: statusResponse.video.duration } : {},
|
|
3806
|
+
...((_j = statusResponse.usage) == null ? void 0 : _j.cost_in_usd_ticks) != null ? { costInUsdTicks: statusResponse.usage.cost_in_usd_ticks } : {},
|
|
3807
|
+
...statusResponse.progress != null ? { progress: statusResponse.progress } : {}
|
|
2933
3808
|
}
|
|
2934
3809
|
}
|
|
2935
3810
|
};
|
|
2936
3811
|
}
|
|
2937
3812
|
if (statusResponse.status === "expired") {
|
|
2938
|
-
throw new
|
|
3813
|
+
throw new AISDKError({
|
|
2939
3814
|
name: "XAI_VIDEO_GENERATION_EXPIRED",
|
|
2940
3815
|
message: "Video generation request expired."
|
|
2941
3816
|
});
|
|
2942
3817
|
}
|
|
3818
|
+
if (statusResponse.status === "failed") {
|
|
3819
|
+
throw new AISDKError({
|
|
3820
|
+
name: "XAI_VIDEO_GENERATION_FAILED",
|
|
3821
|
+
message: "Video generation failed."
|
|
3822
|
+
});
|
|
3823
|
+
}
|
|
2943
3824
|
}
|
|
2944
3825
|
}
|
|
2945
3826
|
};
|
|
2946
|
-
var xaiCreateVideoResponseSchema =
|
|
2947
|
-
request_id:
|
|
3827
|
+
var xaiCreateVideoResponseSchema = z18.object({
|
|
3828
|
+
request_id: z18.string().nullish()
|
|
2948
3829
|
});
|
|
2949
|
-
var xaiVideoStatusResponseSchema =
|
|
2950
|
-
status:
|
|
2951
|
-
video:
|
|
2952
|
-
url:
|
|
2953
|
-
duration:
|
|
2954
|
-
respect_moderation:
|
|
3830
|
+
var xaiVideoStatusResponseSchema = z18.object({
|
|
3831
|
+
status: z18.string().nullish(),
|
|
3832
|
+
video: z18.object({
|
|
3833
|
+
url: z18.string(),
|
|
3834
|
+
duration: z18.number().nullish(),
|
|
3835
|
+
respect_moderation: z18.boolean().nullish()
|
|
2955
3836
|
}).nullish(),
|
|
2956
|
-
model:
|
|
3837
|
+
model: z18.string().nullish(),
|
|
3838
|
+
usage: z18.object({
|
|
3839
|
+
cost_in_usd_ticks: z18.number().nullish()
|
|
3840
|
+
}).nullish(),
|
|
3841
|
+
progress: z18.number().nullish(),
|
|
3842
|
+
error: z18.object({
|
|
3843
|
+
code: z18.string().nullish(),
|
|
3844
|
+
message: z18.string().nullish()
|
|
3845
|
+
}).nullish()
|
|
3846
|
+
});
|
|
3847
|
+
|
|
3848
|
+
// src/xai-speech-model.ts
|
|
3849
|
+
import {
|
|
3850
|
+
combineHeaders as combineHeaders6,
|
|
3851
|
+
createBinaryResponseHandler as createBinaryResponseHandler2,
|
|
3852
|
+
parseProviderOptions as parseProviderOptions6,
|
|
3853
|
+
postJsonToApi as postJsonToApi5,
|
|
3854
|
+
resolve,
|
|
3855
|
+
serializeModelOptions as serializeModelOptions4,
|
|
3856
|
+
WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE4,
|
|
3857
|
+
WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE4
|
|
3858
|
+
} from "@ai-sdk/provider-utils";
|
|
3859
|
+
|
|
3860
|
+
// src/xai-speech-model-options.ts
|
|
3861
|
+
import {
|
|
3862
|
+
lazySchema as lazySchema8,
|
|
3863
|
+
zodSchema as zodSchema8
|
|
3864
|
+
} from "@ai-sdk/provider-utils";
|
|
3865
|
+
import { z as z19 } from "zod/v4";
|
|
3866
|
+
var xaiSpeechModelOptionsSchema = lazySchema8(
|
|
3867
|
+
() => zodSchema8(
|
|
3868
|
+
z19.object({
|
|
3869
|
+
/**
|
|
3870
|
+
* Sample rate of the generated audio in Hz.
|
|
3871
|
+
*/
|
|
3872
|
+
sampleRate: z19.union([
|
|
3873
|
+
z19.literal(8e3),
|
|
3874
|
+
z19.literal(16e3),
|
|
3875
|
+
z19.literal(22050),
|
|
3876
|
+
z19.literal(24e3),
|
|
3877
|
+
z19.literal(44100),
|
|
3878
|
+
z19.literal(48e3)
|
|
3879
|
+
]).nullish(),
|
|
3880
|
+
/**
|
|
3881
|
+
* MP3 bit rate in bits per second. Only applies when outputFormat is mp3.
|
|
3882
|
+
*/
|
|
3883
|
+
bitRate: z19.union([
|
|
3884
|
+
z19.literal(32e3),
|
|
3885
|
+
z19.literal(64e3),
|
|
3886
|
+
z19.literal(96e3),
|
|
3887
|
+
z19.literal(128e3),
|
|
3888
|
+
z19.literal(192e3)
|
|
3889
|
+
]).nullish(),
|
|
3890
|
+
/**
|
|
3891
|
+
* Reduce time to first audio chunk, trading some quality for latency.
|
|
3892
|
+
*/
|
|
3893
|
+
optimizeStreamingLatency: z19.union([z19.literal(0), z19.literal(1), z19.literal(2)]).nullish(),
|
|
3894
|
+
/**
|
|
3895
|
+
* Normalize written-form text into spoken-form text before synthesis.
|
|
3896
|
+
*/
|
|
3897
|
+
textNormalization: z19.boolean().nullish()
|
|
3898
|
+
})
|
|
3899
|
+
)
|
|
3900
|
+
);
|
|
3901
|
+
|
|
3902
|
+
// src/xai-speech-model.ts
|
|
3903
|
+
var XaiSpeechModel = class _XaiSpeechModel {
|
|
3904
|
+
constructor(modelId, config) {
|
|
3905
|
+
this.modelId = modelId;
|
|
3906
|
+
this.config = config;
|
|
3907
|
+
this.specificationVersion = "v4";
|
|
3908
|
+
}
|
|
3909
|
+
static [WORKFLOW_SERIALIZE4](model) {
|
|
3910
|
+
return serializeModelOptions4({
|
|
3911
|
+
modelId: model.modelId,
|
|
3912
|
+
config: model.config
|
|
3913
|
+
});
|
|
3914
|
+
}
|
|
3915
|
+
static [WORKFLOW_DESERIALIZE4](options) {
|
|
3916
|
+
return new _XaiSpeechModel(options.modelId, options.config);
|
|
3917
|
+
}
|
|
3918
|
+
get provider() {
|
|
3919
|
+
return this.config.provider;
|
|
3920
|
+
}
|
|
3921
|
+
async getArgs({
|
|
3922
|
+
text,
|
|
3923
|
+
voice = "eve",
|
|
3924
|
+
outputFormat = "mp3",
|
|
3925
|
+
instructions,
|
|
3926
|
+
speed,
|
|
3927
|
+
language = "auto",
|
|
3928
|
+
providerOptions
|
|
3929
|
+
}) {
|
|
3930
|
+
const warnings = [];
|
|
3931
|
+
const xaiOptions = await parseProviderOptions6({
|
|
3932
|
+
provider: "xai",
|
|
3933
|
+
providerOptions,
|
|
3934
|
+
schema: xaiSpeechModelOptionsSchema
|
|
3935
|
+
});
|
|
3936
|
+
let codec = "mp3";
|
|
3937
|
+
if (["mp3", "wav", "pcm", "mulaw", "alaw"].includes(outputFormat)) {
|
|
3938
|
+
codec = outputFormat;
|
|
3939
|
+
} else {
|
|
3940
|
+
warnings.push({
|
|
3941
|
+
type: "unsupported",
|
|
3942
|
+
feature: "outputFormat",
|
|
3943
|
+
details: `Unsupported output format: ${outputFormat}. Using mp3 instead.`
|
|
3944
|
+
});
|
|
3945
|
+
}
|
|
3946
|
+
if (instructions != null) {
|
|
3947
|
+
warnings.push({
|
|
3948
|
+
type: "unsupported",
|
|
3949
|
+
feature: "instructions",
|
|
3950
|
+
details: "xAI speech models do not support the `instructions` option. Use xAI speech tags in `text` to control delivery."
|
|
3951
|
+
});
|
|
3952
|
+
}
|
|
3953
|
+
const output_format = {
|
|
3954
|
+
codec
|
|
3955
|
+
};
|
|
3956
|
+
if ((xaiOptions == null ? void 0 : xaiOptions.sampleRate) != null) {
|
|
3957
|
+
output_format.sample_rate = xaiOptions.sampleRate;
|
|
3958
|
+
}
|
|
3959
|
+
if ((xaiOptions == null ? void 0 : xaiOptions.bitRate) != null) {
|
|
3960
|
+
if (codec === "mp3") {
|
|
3961
|
+
output_format.bit_rate = xaiOptions.bitRate;
|
|
3962
|
+
} else {
|
|
3963
|
+
warnings.push({
|
|
3964
|
+
type: "unsupported",
|
|
3965
|
+
feature: "providerOptions",
|
|
3966
|
+
details: "xAI `bitRate` is supported only for mp3 output. It was ignored."
|
|
3967
|
+
});
|
|
3968
|
+
}
|
|
3969
|
+
}
|
|
3970
|
+
const requestBody = {
|
|
3971
|
+
text,
|
|
3972
|
+
voice_id: voice,
|
|
3973
|
+
language,
|
|
3974
|
+
output_format,
|
|
3975
|
+
speed,
|
|
3976
|
+
optimize_streaming_latency: xaiOptions == null ? void 0 : xaiOptions.optimizeStreamingLatency,
|
|
3977
|
+
text_normalization: xaiOptions == null ? void 0 : xaiOptions.textNormalization
|
|
3978
|
+
};
|
|
3979
|
+
return { requestBody, warnings };
|
|
3980
|
+
}
|
|
3981
|
+
async doGenerate(options) {
|
|
3982
|
+
var _a, _b, _c;
|
|
3983
|
+
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
3984
|
+
const { requestBody, warnings } = await this.getArgs(options);
|
|
3985
|
+
const {
|
|
3986
|
+
value: audio,
|
|
3987
|
+
responseHeaders,
|
|
3988
|
+
rawValue: rawResponse
|
|
3989
|
+
} = await postJsonToApi5({
|
|
3990
|
+
url: `${this.config.baseURL}/tts`,
|
|
3991
|
+
headers: combineHeaders6(
|
|
3992
|
+
this.config.headers ? await resolve(this.config.headers) : void 0,
|
|
3993
|
+
options.headers
|
|
3994
|
+
),
|
|
3995
|
+
body: requestBody,
|
|
3996
|
+
failedResponseHandler: xaiFailedResponseHandler,
|
|
3997
|
+
successfulResponseHandler: createBinaryResponseHandler2(),
|
|
3998
|
+
abortSignal: options.abortSignal,
|
|
3999
|
+
fetch: this.config.fetch
|
|
4000
|
+
});
|
|
4001
|
+
return {
|
|
4002
|
+
audio,
|
|
4003
|
+
warnings,
|
|
4004
|
+
request: {
|
|
4005
|
+
body: JSON.stringify(requestBody)
|
|
4006
|
+
},
|
|
4007
|
+
response: {
|
|
4008
|
+
timestamp: currentDate,
|
|
4009
|
+
modelId: this.modelId,
|
|
4010
|
+
headers: responseHeaders,
|
|
4011
|
+
body: rawResponse
|
|
4012
|
+
}
|
|
4013
|
+
};
|
|
4014
|
+
}
|
|
4015
|
+
};
|
|
4016
|
+
|
|
4017
|
+
// src/xai-transcription-model.ts
|
|
4018
|
+
import {
|
|
4019
|
+
combineHeaders as combineHeaders7,
|
|
4020
|
+
convertBase64ToUint8Array,
|
|
4021
|
+
createJsonResponseHandler as createJsonResponseHandler6,
|
|
4022
|
+
mediaTypeToExtension,
|
|
4023
|
+
parseProviderOptions as parseProviderOptions7,
|
|
4024
|
+
postFormDataToApi as postFormDataToApi2,
|
|
4025
|
+
serializeModelOptions as serializeModelOptions5,
|
|
4026
|
+
WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE5,
|
|
4027
|
+
WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE5
|
|
4028
|
+
} from "@ai-sdk/provider-utils";
|
|
4029
|
+
import { z as z21 } from "zod/v4";
|
|
4030
|
+
|
|
4031
|
+
// src/xai-transcription-model-options.ts
|
|
4032
|
+
import {
|
|
4033
|
+
lazySchema as lazySchema9,
|
|
4034
|
+
zodSchema as zodSchema9
|
|
4035
|
+
} from "@ai-sdk/provider-utils";
|
|
4036
|
+
import { z as z20 } from "zod/v4";
|
|
4037
|
+
var xaiTranscriptionModelOptionsSchema = lazySchema9(
|
|
4038
|
+
() => zodSchema9(
|
|
4039
|
+
z20.object({
|
|
4040
|
+
/**
|
|
4041
|
+
* Audio encoding for raw, headerless input audio.
|
|
4042
|
+
*/
|
|
4043
|
+
audioFormat: z20.enum(["pcm", "mulaw", "alaw"]).nullish(),
|
|
4044
|
+
/**
|
|
4045
|
+
* Sample rate of the input audio in Hz.
|
|
4046
|
+
*/
|
|
4047
|
+
sampleRate: z20.union([
|
|
4048
|
+
z20.literal(8e3),
|
|
4049
|
+
z20.literal(16e3),
|
|
4050
|
+
z20.literal(22050),
|
|
4051
|
+
z20.literal(24e3),
|
|
4052
|
+
z20.literal(44100),
|
|
4053
|
+
z20.literal(48e3)
|
|
4054
|
+
]).nullish(),
|
|
4055
|
+
/**
|
|
4056
|
+
* Language code used for inverse text normalization.
|
|
4057
|
+
*/
|
|
4058
|
+
language: z20.string().nullish(),
|
|
4059
|
+
/**
|
|
4060
|
+
* Enable inverse text normalization. Requires `language`.
|
|
4061
|
+
*/
|
|
4062
|
+
format: z20.boolean().nullish(),
|
|
4063
|
+
/**
|
|
4064
|
+
* Enable per-channel transcription for multichannel audio.
|
|
4065
|
+
*/
|
|
4066
|
+
multichannel: z20.boolean().nullish(),
|
|
4067
|
+
/**
|
|
4068
|
+
* Number of interleaved audio channels.
|
|
4069
|
+
*/
|
|
4070
|
+
channels: z20.number().int().min(2).max(8).nullish(),
|
|
4071
|
+
/**
|
|
4072
|
+
* Enable speaker diarization.
|
|
4073
|
+
*/
|
|
4074
|
+
diarize: z20.boolean().nullish(),
|
|
4075
|
+
/**
|
|
4076
|
+
* Terms to bias transcription toward.
|
|
4077
|
+
*/
|
|
4078
|
+
keyterm: z20.union([z20.string(), z20.array(z20.string())]).nullish(),
|
|
4079
|
+
/**
|
|
4080
|
+
* Include filler words such as "uh" and "um" in the transcript.
|
|
4081
|
+
*/
|
|
4082
|
+
fillerWords: z20.boolean().nullish()
|
|
4083
|
+
})
|
|
4084
|
+
)
|
|
4085
|
+
);
|
|
4086
|
+
|
|
4087
|
+
// src/xai-transcription-model.ts
|
|
4088
|
+
var XaiTranscriptionModel = class _XaiTranscriptionModel {
|
|
4089
|
+
constructor(modelId, config) {
|
|
4090
|
+
this.modelId = modelId;
|
|
4091
|
+
this.config = config;
|
|
4092
|
+
this.specificationVersion = "v4";
|
|
4093
|
+
}
|
|
4094
|
+
static [WORKFLOW_SERIALIZE5](model) {
|
|
4095
|
+
return serializeModelOptions5({
|
|
4096
|
+
modelId: model.modelId,
|
|
4097
|
+
config: model.config
|
|
4098
|
+
});
|
|
4099
|
+
}
|
|
4100
|
+
static [WORKFLOW_DESERIALIZE5](options) {
|
|
4101
|
+
return new _XaiTranscriptionModel(options.modelId, options.config);
|
|
4102
|
+
}
|
|
4103
|
+
get provider() {
|
|
4104
|
+
return this.config.provider;
|
|
4105
|
+
}
|
|
4106
|
+
async getArgs({
|
|
4107
|
+
audio,
|
|
4108
|
+
mediaType,
|
|
4109
|
+
providerOptions
|
|
4110
|
+
}) {
|
|
4111
|
+
const warnings = [];
|
|
4112
|
+
const xaiOptions = await parseProviderOptions7({
|
|
4113
|
+
provider: "xai",
|
|
4114
|
+
providerOptions,
|
|
4115
|
+
schema: xaiTranscriptionModelOptionsSchema
|
|
4116
|
+
});
|
|
4117
|
+
const formData = new FormData();
|
|
4118
|
+
const transcriptionOptions = {
|
|
4119
|
+
audio_format: xaiOptions == null ? void 0 : xaiOptions.audioFormat,
|
|
4120
|
+
sample_rate: xaiOptions == null ? void 0 : xaiOptions.sampleRate,
|
|
4121
|
+
language: xaiOptions == null ? void 0 : xaiOptions.language,
|
|
4122
|
+
format: xaiOptions == null ? void 0 : xaiOptions.format,
|
|
4123
|
+
multichannel: xaiOptions == null ? void 0 : xaiOptions.multichannel,
|
|
4124
|
+
channels: xaiOptions == null ? void 0 : xaiOptions.channels,
|
|
4125
|
+
diarize: xaiOptions == null ? void 0 : xaiOptions.diarize,
|
|
4126
|
+
filler_words: xaiOptions == null ? void 0 : xaiOptions.fillerWords
|
|
4127
|
+
};
|
|
4128
|
+
for (const [key, value] of Object.entries(transcriptionOptions)) {
|
|
4129
|
+
if (value != null) {
|
|
4130
|
+
formData.append(key, String(value));
|
|
4131
|
+
}
|
|
4132
|
+
}
|
|
4133
|
+
if ((xaiOptions == null ? void 0 : xaiOptions.keyterm) != null) {
|
|
4134
|
+
const keyterms = Array.isArray(xaiOptions.keyterm) ? xaiOptions.keyterm : [xaiOptions.keyterm];
|
|
4135
|
+
for (const keyterm of keyterms) {
|
|
4136
|
+
formData.append("keyterm", keyterm);
|
|
4137
|
+
}
|
|
4138
|
+
}
|
|
4139
|
+
const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([convertBase64ToUint8Array(audio)]);
|
|
4140
|
+
const fileExtension = mediaTypeToExtension(mediaType);
|
|
4141
|
+
formData.append(
|
|
4142
|
+
"file",
|
|
4143
|
+
new File([blob], "audio", { type: mediaType }),
|
|
4144
|
+
`audio.${fileExtension}`
|
|
4145
|
+
);
|
|
4146
|
+
return { formData, warnings };
|
|
4147
|
+
}
|
|
4148
|
+
async doGenerate(options) {
|
|
4149
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
4150
|
+
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
4151
|
+
const { formData, warnings } = await this.getArgs(options);
|
|
4152
|
+
const {
|
|
4153
|
+
value: response,
|
|
4154
|
+
responseHeaders,
|
|
4155
|
+
rawValue: rawResponse
|
|
4156
|
+
} = await postFormDataToApi2({
|
|
4157
|
+
url: `${(_d = this.config.baseURL) != null ? _d : "https://api.x.ai/v1"}/stt`,
|
|
4158
|
+
headers: combineHeaders7((_f = (_e = this.config).headers) == null ? void 0 : _f.call(_e), options.headers),
|
|
4159
|
+
formData,
|
|
4160
|
+
failedResponseHandler: xaiFailedResponseHandler,
|
|
4161
|
+
successfulResponseHandler: createJsonResponseHandler6(
|
|
4162
|
+
xaiTranscriptionResponseSchema
|
|
4163
|
+
),
|
|
4164
|
+
abortSignal: options.abortSignal,
|
|
4165
|
+
fetch: this.config.fetch
|
|
4166
|
+
});
|
|
4167
|
+
return {
|
|
4168
|
+
text: response.text,
|
|
4169
|
+
segments: (_h = (_g = response.words) == null ? void 0 : _g.map((word) => ({
|
|
4170
|
+
text: word.text,
|
|
4171
|
+
startSecond: word.start,
|
|
4172
|
+
endSecond: word.end
|
|
4173
|
+
}))) != null ? _h : [],
|
|
4174
|
+
language: response.language || void 0,
|
|
4175
|
+
durationInSeconds: (_i = response.duration) != null ? _i : void 0,
|
|
4176
|
+
warnings,
|
|
4177
|
+
response: {
|
|
4178
|
+
timestamp: currentDate,
|
|
4179
|
+
modelId: this.modelId,
|
|
4180
|
+
headers: responseHeaders,
|
|
4181
|
+
body: rawResponse
|
|
4182
|
+
}
|
|
4183
|
+
};
|
|
4184
|
+
}
|
|
4185
|
+
};
|
|
4186
|
+
var xaiTranscriptionResponseSchema = z21.object({
|
|
4187
|
+
text: z21.string(),
|
|
4188
|
+
language: z21.string().nullish(),
|
|
4189
|
+
duration: z21.number().nullish(),
|
|
4190
|
+
words: z21.array(
|
|
4191
|
+
z21.object({
|
|
4192
|
+
text: z21.string(),
|
|
4193
|
+
start: z21.number(),
|
|
4194
|
+
end: z21.number()
|
|
4195
|
+
})
|
|
4196
|
+
).nullish()
|
|
2957
4197
|
});
|
|
2958
4198
|
|
|
2959
4199
|
// src/xai-provider.ts
|
|
2960
4200
|
function createXai(options = {}) {
|
|
2961
4201
|
var _a;
|
|
2962
|
-
const baseURL =
|
|
4202
|
+
const baseURL = withoutTrailingSlash(
|
|
2963
4203
|
(_a = options.baseURL) != null ? _a : "https://api.x.ai/v1"
|
|
2964
4204
|
);
|
|
2965
|
-
const getHeaders = () =>
|
|
4205
|
+
const getHeaders = () => withUserAgentSuffix(
|
|
2966
4206
|
{
|
|
2967
|
-
Authorization: `Bearer ${
|
|
4207
|
+
Authorization: `Bearer ${loadApiKey({
|
|
2968
4208
|
apiKey: options.apiKey,
|
|
2969
4209
|
environmentVariableName: "XAI_API_KEY",
|
|
2970
4210
|
description: "xAI API key"
|
|
@@ -2978,7 +4218,7 @@ function createXai(options = {}) {
|
|
|
2978
4218
|
provider: "xai.chat",
|
|
2979
4219
|
baseURL,
|
|
2980
4220
|
headers: getHeaders,
|
|
2981
|
-
generateId
|
|
4221
|
+
generateId,
|
|
2982
4222
|
fetch: options.fetch
|
|
2983
4223
|
});
|
|
2984
4224
|
};
|
|
@@ -2987,7 +4227,7 @@ function createXai(options = {}) {
|
|
|
2987
4227
|
provider: "xai.responses",
|
|
2988
4228
|
baseURL,
|
|
2989
4229
|
headers: getHeaders,
|
|
2990
|
-
generateId
|
|
4230
|
+
generateId,
|
|
2991
4231
|
fetch: options.fetch
|
|
2992
4232
|
});
|
|
2993
4233
|
};
|
|
@@ -3007,25 +4247,78 @@ function createXai(options = {}) {
|
|
|
3007
4247
|
fetch: options.fetch
|
|
3008
4248
|
});
|
|
3009
4249
|
};
|
|
4250
|
+
const createRealtimeModel = (modelId) => {
|
|
4251
|
+
return new XaiRealtimeModel(modelId, {
|
|
4252
|
+
provider: "xai.realtime",
|
|
4253
|
+
baseURL: baseURL != null ? baseURL : "https://api.x.ai/v1",
|
|
4254
|
+
headers: getHeaders,
|
|
4255
|
+
fetch: options.fetch
|
|
4256
|
+
});
|
|
4257
|
+
};
|
|
4258
|
+
const createSpeechModel = () => {
|
|
4259
|
+
return new XaiSpeechModel("", {
|
|
4260
|
+
provider: "xai.speech",
|
|
4261
|
+
baseURL,
|
|
4262
|
+
headers: getHeaders,
|
|
4263
|
+
fetch: options.fetch
|
|
4264
|
+
});
|
|
4265
|
+
};
|
|
4266
|
+
const createTranscriptionModel = () => {
|
|
4267
|
+
return new XaiTranscriptionModel("", {
|
|
4268
|
+
provider: "xai.transcription",
|
|
4269
|
+
baseURL,
|
|
4270
|
+
headers: getHeaders,
|
|
4271
|
+
fetch: options.fetch
|
|
4272
|
+
});
|
|
4273
|
+
};
|
|
4274
|
+
const experimentalRealtimeFactory = Object.assign(
|
|
4275
|
+
(modelId) => createRealtimeModel(modelId),
|
|
4276
|
+
{
|
|
4277
|
+
getToken: async (tokenOptions) => {
|
|
4278
|
+
const model = createRealtimeModel(tokenOptions.model);
|
|
4279
|
+
const secret = await model.doCreateClientSecret({
|
|
4280
|
+
sessionConfig: tokenOptions.sessionConfig,
|
|
4281
|
+
expiresAfterSeconds: tokenOptions.expiresAfterSeconds
|
|
4282
|
+
});
|
|
4283
|
+
return {
|
|
4284
|
+
token: secret.token,
|
|
4285
|
+
url: secret.url,
|
|
4286
|
+
expiresAt: secret.expiresAt
|
|
4287
|
+
};
|
|
4288
|
+
}
|
|
4289
|
+
}
|
|
4290
|
+
);
|
|
4291
|
+
const createFiles = () => new XaiFiles({
|
|
4292
|
+
provider: "xai.files",
|
|
4293
|
+
baseURL,
|
|
4294
|
+
headers: getHeaders,
|
|
4295
|
+
fetch: options.fetch
|
|
4296
|
+
});
|
|
3010
4297
|
const provider = (modelId) => createResponsesLanguageModel(modelId);
|
|
3011
|
-
provider.specificationVersion = "
|
|
4298
|
+
provider.specificationVersion = "v4";
|
|
3012
4299
|
provider.languageModel = createResponsesLanguageModel;
|
|
3013
4300
|
provider.chat = createChatLanguageModel;
|
|
3014
4301
|
provider.responses = createResponsesLanguageModel;
|
|
3015
4302
|
provider.embeddingModel = (modelId) => {
|
|
3016
|
-
throw new
|
|
4303
|
+
throw new NoSuchModelError({ modelId, modelType: "embeddingModel" });
|
|
3017
4304
|
};
|
|
3018
4305
|
provider.textEmbeddingModel = provider.embeddingModel;
|
|
3019
4306
|
provider.imageModel = createImageModel;
|
|
3020
4307
|
provider.image = createImageModel;
|
|
3021
4308
|
provider.videoModel = createVideoModel;
|
|
3022
4309
|
provider.video = createVideoModel;
|
|
4310
|
+
provider.experimental_realtime = experimentalRealtimeFactory;
|
|
4311
|
+
provider.speechModel = createSpeechModel;
|
|
4312
|
+
provider.speech = createSpeechModel;
|
|
4313
|
+
provider.transcriptionModel = createTranscriptionModel;
|
|
4314
|
+
provider.transcription = createTranscriptionModel;
|
|
4315
|
+
provider.files = createFiles;
|
|
3023
4316
|
provider.tools = xaiTools;
|
|
3024
4317
|
return provider;
|
|
3025
4318
|
}
|
|
3026
4319
|
var xai = createXai();
|
|
3027
|
-
|
|
3028
|
-
|
|
4320
|
+
export {
|
|
4321
|
+
XaiRealtimeModel as Experimental_XaiRealtimeModel,
|
|
3029
4322
|
VERSION,
|
|
3030
4323
|
codeExecution,
|
|
3031
4324
|
createXai,
|
|
@@ -3036,5 +4329,5 @@ var xai = createXai();
|
|
|
3036
4329
|
xSearch,
|
|
3037
4330
|
xai,
|
|
3038
4331
|
xaiTools
|
|
3039
|
-
}
|
|
4332
|
+
};
|
|
3040
4333
|
//# sourceMappingURL=index.js.map
|