@ai-sdk/xai 5.0.7 → 5.0.10
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 +22 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +424 -311
- package/dist/index.js.map +1 -1
- package/docs/01-xai.mdx +93 -34
- package/package.json +3 -3
- package/src/responses/xai-responses-api.ts +8 -1
- package/src/responses/xai-responses-language-model-options.ts +27 -1
- package/src/responses/xai-responses-language-model.ts +38 -14
- package/src/xai-video-model-options.ts +27 -0
- package/src/xai-video-model.ts +179 -53
package/dist/index.js
CHANGED
|
@@ -101,7 +101,6 @@ var XaiImageModel = class _XaiImageModel {
|
|
|
101
101
|
files,
|
|
102
102
|
mask
|
|
103
103
|
}) {
|
|
104
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
105
104
|
const warnings = [];
|
|
106
105
|
if (size != null) {
|
|
107
106
|
warnings.push({
|
|
@@ -139,22 +138,22 @@ var XaiImageModel = class _XaiImageModel {
|
|
|
139
138
|
if (aspectRatio != null) {
|
|
140
139
|
body.aspect_ratio = aspectRatio;
|
|
141
140
|
}
|
|
142
|
-
if (
|
|
141
|
+
if (xaiOptions?.output_format != null) {
|
|
143
142
|
body.output_format = xaiOptions.output_format;
|
|
144
143
|
}
|
|
145
|
-
if (
|
|
144
|
+
if (xaiOptions?.sync_mode != null) {
|
|
146
145
|
body.sync_mode = xaiOptions.sync_mode;
|
|
147
146
|
}
|
|
148
|
-
if (
|
|
147
|
+
if (xaiOptions?.aspect_ratio != null && aspectRatio == null) {
|
|
149
148
|
body.aspect_ratio = xaiOptions.aspect_ratio;
|
|
150
149
|
}
|
|
151
|
-
if (
|
|
150
|
+
if (xaiOptions?.resolution != null) {
|
|
152
151
|
body.resolution = xaiOptions.resolution;
|
|
153
152
|
}
|
|
154
|
-
if (
|
|
153
|
+
if (xaiOptions?.quality != null) {
|
|
155
154
|
body.quality = xaiOptions.quality;
|
|
156
155
|
}
|
|
157
|
-
if (
|
|
156
|
+
if (xaiOptions?.user != null) {
|
|
158
157
|
body.user = xaiOptions.user;
|
|
159
158
|
}
|
|
160
159
|
if (imageUrls.length === 1) {
|
|
@@ -162,11 +161,11 @@ var XaiImageModel = class _XaiImageModel {
|
|
|
162
161
|
} else if (imageUrls.length > 1) {
|
|
163
162
|
body.images = imageUrls.map((url) => ({ url, type: "image_url" }));
|
|
164
163
|
}
|
|
165
|
-
const baseURL =
|
|
166
|
-
const currentDate =
|
|
164
|
+
const baseURL = this.config.baseURL ?? "https://api.x.ai/v1";
|
|
165
|
+
const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
|
|
167
166
|
const { value: response, responseHeaders } = await postJsonToApi({
|
|
168
167
|
url: `${baseURL}${endpoint}`,
|
|
169
|
-
headers: combineHeaders(
|
|
168
|
+
headers: combineHeaders(this.config.headers?.(), headers),
|
|
170
169
|
body,
|
|
171
170
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
172
171
|
successfulResponseHandler: createJsonResponseHandler(
|
|
@@ -199,7 +198,7 @@ var XaiImageModel = class _XaiImageModel {
|
|
|
199
198
|
images: response.data.map((item) => ({
|
|
200
199
|
...item.revised_prompt ? { revisedPrompt: item.revised_prompt } : {}
|
|
201
200
|
})),
|
|
202
|
-
...
|
|
201
|
+
...response.usage?.cost_in_usd_ticks != null ? { costInUsdTicks: response.usage.cost_in_usd_ticks } : {}
|
|
203
202
|
}
|
|
204
203
|
}
|
|
205
204
|
};
|
|
@@ -267,7 +266,7 @@ function getResponseMetadata({
|
|
|
267
266
|
return createLanguageModelResponseMetadata({
|
|
268
267
|
id,
|
|
269
268
|
model,
|
|
270
|
-
created: created
|
|
269
|
+
created: created ?? created_at
|
|
271
270
|
});
|
|
272
271
|
}
|
|
273
272
|
|
|
@@ -369,7 +368,6 @@ var xaiFilePartProviderOptions = z5.object({
|
|
|
369
368
|
async function convertToXaiResponsesInput({
|
|
370
369
|
prompt
|
|
371
370
|
}) {
|
|
372
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
373
371
|
const input = [];
|
|
374
372
|
const inputWarnings = [];
|
|
375
373
|
for (const message of prompt) {
|
|
@@ -418,7 +416,7 @@ async function convertToXaiResponsesInput({
|
|
|
418
416
|
contentParts.push({
|
|
419
417
|
type: "input_image",
|
|
420
418
|
image_url: imageUrl,
|
|
421
|
-
...
|
|
419
|
+
...filePartOptions?.imageDetail != null && {
|
|
422
420
|
detail: filePartOptions.imageDetail
|
|
423
421
|
}
|
|
424
422
|
});
|
|
@@ -456,7 +454,7 @@ async function convertToXaiResponsesInput({
|
|
|
456
454
|
for (const part of message.content) {
|
|
457
455
|
switch (part.type) {
|
|
458
456
|
case "text": {
|
|
459
|
-
const id = typeof
|
|
457
|
+
const id = typeof part.providerOptions?.xai?.itemId === "string" ? part.providerOptions.xai.itemId : void 0;
|
|
460
458
|
input.push({
|
|
461
459
|
role: "assistant",
|
|
462
460
|
content: part.text,
|
|
@@ -468,10 +466,10 @@ async function convertToXaiResponsesInput({
|
|
|
468
466
|
if (part.providerExecuted) {
|
|
469
467
|
break;
|
|
470
468
|
}
|
|
471
|
-
const id = typeof
|
|
469
|
+
const id = typeof part.providerOptions?.xai?.itemId === "string" ? part.providerOptions.xai.itemId : void 0;
|
|
472
470
|
input.push({
|
|
473
471
|
type: "function_call",
|
|
474
|
-
id: id
|
|
472
|
+
id: id ?? part.toolCallId,
|
|
475
473
|
call_id: part.toolCallId,
|
|
476
474
|
name: part.toolName,
|
|
477
475
|
arguments: JSON.stringify(part.input),
|
|
@@ -483,8 +481,8 @@ async function convertToXaiResponsesInput({
|
|
|
483
481
|
break;
|
|
484
482
|
}
|
|
485
483
|
case "reasoning": {
|
|
486
|
-
const itemId = typeof
|
|
487
|
-
const encryptedContent = typeof
|
|
484
|
+
const itemId = typeof part.providerOptions?.xai?.itemId === "string" ? part.providerOptions.xai.itemId : void 0;
|
|
485
|
+
const encryptedContent = typeof part.providerOptions?.xai?.reasoningEncryptedContent === "string" ? part.providerOptions.xai.reasoningEncryptedContent : void 0;
|
|
488
486
|
if (itemId != null || encryptedContent != null) {
|
|
489
487
|
const summaryParts = [];
|
|
490
488
|
if (part.text.length > 0) {
|
|
@@ -495,7 +493,7 @@ async function convertToXaiResponsesInput({
|
|
|
495
493
|
}
|
|
496
494
|
input.push({
|
|
497
495
|
type: "reasoning",
|
|
498
|
-
id: itemId
|
|
496
|
+
id: itemId ?? "",
|
|
499
497
|
summary: summaryParts,
|
|
500
498
|
status: "completed",
|
|
501
499
|
...encryptedContent != null && {
|
|
@@ -543,7 +541,7 @@ async function convertToXaiResponsesInput({
|
|
|
543
541
|
outputValue = output.value;
|
|
544
542
|
break;
|
|
545
543
|
case "execution-denied":
|
|
546
|
-
outputValue =
|
|
544
|
+
outputValue = output.reason ?? "tool execution denied";
|
|
547
545
|
break;
|
|
548
546
|
case "json":
|
|
549
547
|
case "error-json":
|
|
@@ -605,9 +603,8 @@ async function convertToXaiResponsesInput({
|
|
|
605
603
|
|
|
606
604
|
// src/responses/convert-xai-responses-usage.ts
|
|
607
605
|
function convertXaiResponsesUsage(usage) {
|
|
608
|
-
|
|
609
|
-
const
|
|
610
|
-
const reasoningTokens = (_d = (_c = usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0;
|
|
606
|
+
const cacheReadTokens = usage.input_tokens_details?.cached_tokens ?? 0;
|
|
607
|
+
const reasoningTokens = usage.output_tokens_details?.reasoning_tokens ?? 0;
|
|
611
608
|
const inputTokensIncludesCached = cacheReadTokens <= usage.input_tokens;
|
|
612
609
|
return {
|
|
613
610
|
inputTokens: {
|
|
@@ -806,7 +803,9 @@ var xaiResponsesResponseSchema = z6.object({
|
|
|
806
803
|
output: z6.array(outputItemSchema),
|
|
807
804
|
usage: xaiResponsesUsageSchema.nullish(),
|
|
808
805
|
status: z6.string(),
|
|
809
|
-
service_tier: z6.string().nullish()
|
|
806
|
+
service_tier: z6.string().nullish(),
|
|
807
|
+
prompt_cache_key: z6.string().nullish(),
|
|
808
|
+
safety_identifier: z6.string().nullish()
|
|
810
809
|
});
|
|
811
810
|
var xaiResponsesChunkSchema = z6.union([
|
|
812
811
|
z6.object({
|
|
@@ -1090,7 +1089,9 @@ var xaiResponsesChunkSchema = z6.union([
|
|
|
1090
1089
|
response: z6.object({
|
|
1091
1090
|
incomplete_details: z6.object({ reason: z6.string() }).nullish(),
|
|
1092
1091
|
usage: xaiResponsesUsageSchema.nullish(),
|
|
1093
|
-
service_tier: z6.string().nullish()
|
|
1092
|
+
service_tier: z6.string().nullish(),
|
|
1093
|
+
prompt_cache_key: z6.string().nullish(),
|
|
1094
|
+
safety_identifier: z6.string().nullish()
|
|
1094
1095
|
})
|
|
1095
1096
|
}),
|
|
1096
1097
|
z6.object({
|
|
@@ -1133,9 +1134,23 @@ var xaiLanguageModelResponsesOptions = z7.object({
|
|
|
1133
1134
|
* @see https://docs.x.ai/docs/guides/reasoning
|
|
1134
1135
|
*/
|
|
1135
1136
|
reasoningEffort: z7.enum(["none", "low", "medium", "high", "xhigh"]).optional(),
|
|
1137
|
+
/**
|
|
1138
|
+
* @deprecated xAI ignores the requested summary level and always returns a
|
|
1139
|
+
* detailed reasoning summary.
|
|
1140
|
+
*/
|
|
1136
1141
|
reasoningSummary: z7.enum(["auto", "concise", "detailed"]).optional(),
|
|
1137
1142
|
logprobs: z7.boolean().optional(),
|
|
1138
1143
|
topLogprobs: z7.number().int().min(0).max(8).optional(),
|
|
1144
|
+
/** Min-p sampling threshold between 0 and 1. */
|
|
1145
|
+
minP: z7.number().min(0).max(1).optional(),
|
|
1146
|
+
/** Maximum number of agentic tool-calling turns. */
|
|
1147
|
+
maxTurns: z7.number().int().optional(),
|
|
1148
|
+
/** Whether the model may call tools in parallel. @default true */
|
|
1149
|
+
parallelToolCalls: z7.boolean().optional(),
|
|
1150
|
+
/** Cache key used to route requests with shared prompt prefixes. */
|
|
1151
|
+
promptCacheKey: z7.string().optional(),
|
|
1152
|
+
/** Stable identifier used to attribute policy violations to an end user. */
|
|
1153
|
+
safetyIdentifier: z7.string().optional(),
|
|
1139
1154
|
serviceTier: z7.enum(["default", "priority"]).optional(),
|
|
1140
1155
|
/**
|
|
1141
1156
|
* Whether to store the input message(s) and model response for later retrieval.
|
|
@@ -1148,11 +1163,21 @@ var xaiLanguageModelResponsesOptions = z7.object({
|
|
|
1148
1163
|
* The ID of the previous response from the model.
|
|
1149
1164
|
*/
|
|
1150
1165
|
previousResponseId: z7.string().optional(),
|
|
1166
|
+
/** A unique identifier for the end user, used for abuse monitoring. */
|
|
1167
|
+
user: z7.string().optional(),
|
|
1151
1168
|
/**
|
|
1152
1169
|
* Specify additional output data to include in the model response.
|
|
1153
1170
|
* Example values: 'file_search_call.results'.
|
|
1154
1171
|
*/
|
|
1155
|
-
include: z7.array(
|
|
1172
|
+
include: z7.array(
|
|
1173
|
+
z7.enum([
|
|
1174
|
+
"file_search_call.results",
|
|
1175
|
+
"web_search_call.action.sources",
|
|
1176
|
+
"code_interpreter_call.outputs",
|
|
1177
|
+
"reasoning.encrypted_content",
|
|
1178
|
+
"no_inline_citations"
|
|
1179
|
+
])
|
|
1180
|
+
).nullish()
|
|
1156
1181
|
});
|
|
1157
1182
|
|
|
1158
1183
|
// src/responses/xai-responses-prepare-tools.ts
|
|
@@ -1356,7 +1381,7 @@ async function prepareResponsesTools({
|
|
|
1356
1381
|
tools,
|
|
1357
1382
|
toolChoice
|
|
1358
1383
|
}) {
|
|
1359
|
-
const normalizedTools =
|
|
1384
|
+
const normalizedTools = tools?.length ? tools : void 0;
|
|
1360
1385
|
const toolWarnings = [];
|
|
1361
1386
|
if (normalizedTools == null) {
|
|
1362
1387
|
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
@@ -1524,7 +1549,7 @@ function createXaiResponsesStreamError({
|
|
|
1524
1549
|
return createProviderStreamError({
|
|
1525
1550
|
message,
|
|
1526
1551
|
type: eventType,
|
|
1527
|
-
code: code
|
|
1552
|
+
code: code ?? void 0,
|
|
1528
1553
|
...statusCode != null ? {
|
|
1529
1554
|
statusCode,
|
|
1530
1555
|
isRetryable: isRetryableStatusCode(statusCode)
|
|
@@ -1618,16 +1643,12 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
1618
1643
|
reasoning
|
|
1619
1644
|
}
|
|
1620
1645
|
}) {
|
|
1621
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1622
1646
|
const warnings = [];
|
|
1623
|
-
const options =
|
|
1647
|
+
const options = await parseProviderOptions3({
|
|
1624
1648
|
provider: "xai",
|
|
1625
1649
|
providerOptions,
|
|
1626
1650
|
schema: xaiLanguageModelResponsesOptions
|
|
1627
|
-
})
|
|
1628
|
-
if (topK != null) {
|
|
1629
|
-
warnings.push({ type: "unsupported", feature: "topK" });
|
|
1630
|
-
}
|
|
1651
|
+
}) ?? {};
|
|
1631
1652
|
if (frequencyPenalty != null) {
|
|
1632
1653
|
warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
|
|
1633
1654
|
}
|
|
@@ -1637,27 +1658,27 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
1637
1658
|
if (stopSequences != null) {
|
|
1638
1659
|
warnings.push({ type: "unsupported", feature: "stopSequences" });
|
|
1639
1660
|
}
|
|
1640
|
-
const webSearchToolName =
|
|
1661
|
+
const webSearchToolName = tools?.find(
|
|
1641
1662
|
(tool) => tool.type === "provider" && tool.id === "xai.web_search"
|
|
1642
|
-
)
|
|
1643
|
-
const xSearchToolName =
|
|
1663
|
+
)?.name;
|
|
1664
|
+
const xSearchToolName = tools?.find(
|
|
1644
1665
|
(tool) => tool.type === "provider" && tool.id === "xai.x_search"
|
|
1645
|
-
)
|
|
1646
|
-
const codeExecutionToolName =
|
|
1666
|
+
)?.name;
|
|
1667
|
+
const codeExecutionToolName = tools?.find(
|
|
1647
1668
|
(tool) => tool.type === "provider" && tool.id === "xai.code_execution"
|
|
1648
|
-
)
|
|
1649
|
-
const mcpToolName =
|
|
1669
|
+
)?.name;
|
|
1670
|
+
const mcpToolName = tools?.find(
|
|
1650
1671
|
(tool) => tool.type === "provider" && tool.id === "xai.mcp"
|
|
1651
|
-
)
|
|
1652
|
-
const fileSearchToolName =
|
|
1672
|
+
)?.name;
|
|
1673
|
+
const fileSearchToolName = tools?.find(
|
|
1653
1674
|
(tool) => tool.type === "provider" && tool.id === "xai.file_search"
|
|
1654
|
-
)
|
|
1655
|
-
const imageGenerationToolName =
|
|
1675
|
+
)?.name;
|
|
1676
|
+
const imageGenerationToolName = tools?.find(
|
|
1656
1677
|
(tool) => tool.type === "provider" && tool.id === "xai.image_generation"
|
|
1657
|
-
)
|
|
1678
|
+
)?.name;
|
|
1658
1679
|
const { input, inputWarnings } = await convertToXaiResponsesInput({
|
|
1659
1680
|
prompt,
|
|
1660
|
-
store:
|
|
1681
|
+
store: options.store ?? true
|
|
1661
1682
|
});
|
|
1662
1683
|
warnings.push(...inputWarnings);
|
|
1663
1684
|
const {
|
|
@@ -1709,26 +1730,23 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
1709
1730
|
max_output_tokens: maxOutputTokens,
|
|
1710
1731
|
temperature,
|
|
1711
1732
|
top_p: topP,
|
|
1733
|
+
top_k: topK,
|
|
1734
|
+
min_p: options.minP,
|
|
1712
1735
|
seed,
|
|
1713
|
-
...
|
|
1736
|
+
...responseFormat?.type === "json" && {
|
|
1714
1737
|
text: {
|
|
1715
1738
|
format: responseFormat.schema != null ? {
|
|
1716
1739
|
type: "json_schema",
|
|
1717
1740
|
strict: true,
|
|
1718
|
-
name:
|
|
1741
|
+
name: responseFormat.name ?? "response",
|
|
1719
1742
|
description: responseFormat.description,
|
|
1720
1743
|
schema: responseFormat.schema
|
|
1721
1744
|
} : { type: "json_object" }
|
|
1722
1745
|
}
|
|
1723
1746
|
},
|
|
1724
|
-
...
|
|
1747
|
+
...resolvedReasoningEffort != null && {
|
|
1725
1748
|
reasoning: {
|
|
1726
|
-
|
|
1727
|
-
effort: resolvedReasoningEffort
|
|
1728
|
-
},
|
|
1729
|
-
...options.reasoningSummary != null && {
|
|
1730
|
-
summary: options.reasoningSummary
|
|
1731
|
-
}
|
|
1749
|
+
effort: resolvedReasoningEffort
|
|
1732
1750
|
}
|
|
1733
1751
|
},
|
|
1734
1752
|
...options.store === false && {
|
|
@@ -1740,8 +1758,23 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
1740
1758
|
...options.previousResponseId != null && {
|
|
1741
1759
|
previous_response_id: options.previousResponseId
|
|
1742
1760
|
},
|
|
1761
|
+
...options.maxTurns != null && {
|
|
1762
|
+
max_turns: options.maxTurns
|
|
1763
|
+
},
|
|
1764
|
+
...options.parallelToolCalls != null && {
|
|
1765
|
+
parallel_tool_calls: options.parallelToolCalls
|
|
1766
|
+
},
|
|
1767
|
+
...options.promptCacheKey != null && {
|
|
1768
|
+
prompt_cache_key: options.promptCacheKey
|
|
1769
|
+
},
|
|
1770
|
+
...options.safetyIdentifier != null && {
|
|
1771
|
+
safety_identifier: options.safetyIdentifier
|
|
1772
|
+
},
|
|
1743
1773
|
...options.serviceTier != null && {
|
|
1744
1774
|
service_tier: options.serviceTier
|
|
1775
|
+
},
|
|
1776
|
+
...options.user != null && {
|
|
1777
|
+
user: options.user
|
|
1745
1778
|
}
|
|
1746
1779
|
};
|
|
1747
1780
|
if (xaiTools2 && xaiTools2.length > 0) {
|
|
@@ -1768,7 +1801,6 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
1768
1801
|
});
|
|
1769
1802
|
}
|
|
1770
1803
|
async doGenerate(options) {
|
|
1771
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
|
|
1772
1804
|
const {
|
|
1773
1805
|
args: body,
|
|
1774
1806
|
warnings,
|
|
@@ -1784,8 +1816,8 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
1784
1816
|
value: response,
|
|
1785
1817
|
rawValue: rawResponse
|
|
1786
1818
|
} = await postJsonToApi2({
|
|
1787
|
-
url: `${
|
|
1788
|
-
headers: combineHeaders2(
|
|
1819
|
+
url: `${this.config.baseURL ?? "https://api.x.ai/v1"}/responses`,
|
|
1820
|
+
headers: combineHeaders2(this.config.headers?.(), options.headers),
|
|
1789
1821
|
body,
|
|
1790
1822
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
1791
1823
|
successfulResponseHandler: createJsonResponseHandler2(
|
|
@@ -1809,7 +1841,7 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
1809
1841
|
];
|
|
1810
1842
|
for (const part of response.output) {
|
|
1811
1843
|
if (part.type === "file_search_call") {
|
|
1812
|
-
const toolName = fileSearchToolName
|
|
1844
|
+
const toolName = fileSearchToolName ?? "file_search";
|
|
1813
1845
|
content.push({
|
|
1814
1846
|
type: "tool-call",
|
|
1815
1847
|
toolCallId: part.id,
|
|
@@ -1822,19 +1854,19 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
1822
1854
|
toolCallId: part.id,
|
|
1823
1855
|
toolName,
|
|
1824
1856
|
result: {
|
|
1825
|
-
queries:
|
|
1826
|
-
results:
|
|
1857
|
+
queries: part.queries ?? [],
|
|
1858
|
+
results: part.results?.map((result) => ({
|
|
1827
1859
|
fileId: result.file_id,
|
|
1828
1860
|
filename: result.filename,
|
|
1829
1861
|
score: result.score,
|
|
1830
1862
|
text: result.text
|
|
1831
|
-
}))
|
|
1863
|
+
})) ?? null
|
|
1832
1864
|
}
|
|
1833
1865
|
});
|
|
1834
1866
|
continue;
|
|
1835
1867
|
}
|
|
1836
1868
|
if (part.type === "image_generation_call") {
|
|
1837
|
-
const toolName = imageGenerationToolName
|
|
1869
|
+
const toolName = imageGenerationToolName ?? "image_generation";
|
|
1838
1870
|
content.push({
|
|
1839
1871
|
type: "tool-call",
|
|
1840
1872
|
toolCallId: part.id,
|
|
@@ -1864,17 +1896,17 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
1864
1896
|
continue;
|
|
1865
1897
|
}
|
|
1866
1898
|
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") {
|
|
1867
|
-
let toolName =
|
|
1868
|
-
if (webSearchSubTools.includes(
|
|
1869
|
-
toolName = webSearchToolName
|
|
1870
|
-
} else if (xSearchSubTools.includes(
|
|
1871
|
-
toolName = xSearchToolName
|
|
1899
|
+
let toolName = part.name ?? "";
|
|
1900
|
+
if (webSearchSubTools.includes(part.name ?? "") || part.type === "web_search_call") {
|
|
1901
|
+
toolName = webSearchToolName ?? "web_search";
|
|
1902
|
+
} else if (xSearchSubTools.includes(part.name ?? "") || part.type === "x_search_call") {
|
|
1903
|
+
toolName = xSearchToolName ?? "x_search";
|
|
1872
1904
|
} else if (part.name === "code_execution" || part.type === "code_interpreter_call" || part.type === "code_execution_call") {
|
|
1873
|
-
toolName = codeExecutionToolName
|
|
1905
|
+
toolName = codeExecutionToolName ?? "code_execution";
|
|
1874
1906
|
} else if (part.type === "mcp_call") {
|
|
1875
|
-
toolName =
|
|
1907
|
+
toolName = mcpToolName ?? part.name ?? "mcp";
|
|
1876
1908
|
}
|
|
1877
|
-
const toolInput = part.type === "custom_tool_call" ?
|
|
1909
|
+
const toolInput = part.type === "custom_tool_call" ? part.input ?? "" : part.type === "mcp_call" ? part.arguments ?? "" : part.arguments ?? "";
|
|
1878
1910
|
content.push({
|
|
1879
1911
|
type: "tool-call",
|
|
1880
1912
|
toolCallId: part.id,
|
|
@@ -1909,7 +1941,7 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
1909
1941
|
sourceType: "url",
|
|
1910
1942
|
id: this.config.generateId(),
|
|
1911
1943
|
url: annotation.url,
|
|
1912
|
-
title:
|
|
1944
|
+
title: annotation.title ?? annotation.url
|
|
1913
1945
|
});
|
|
1914
1946
|
}
|
|
1915
1947
|
}
|
|
@@ -1928,7 +1960,7 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
1928
1960
|
break;
|
|
1929
1961
|
}
|
|
1930
1962
|
case "reasoning": {
|
|
1931
|
-
const texts = part.summary.length > 0 ? part.summary.map((s) => s.text) : (
|
|
1963
|
+
const texts = part.summary.length > 0 ? part.summary.map((s) => s.text) : (part.content ?? []).map((c) => c.text);
|
|
1932
1964
|
const reasoningText = texts.filter((text) => text && text.length > 0).join("");
|
|
1933
1965
|
if (reasoningText || part.encrypted_content) {
|
|
1934
1966
|
const hasMetadata = part.encrypted_content || part.id;
|
|
@@ -1958,20 +1990,26 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
1958
1990
|
content,
|
|
1959
1991
|
finishReason: {
|
|
1960
1992
|
unified: hasFunctionCall ? "tool-calls" : mapXaiResponsesFinishReason(response.status),
|
|
1961
|
-
raw:
|
|
1993
|
+
raw: response.status ?? void 0
|
|
1962
1994
|
},
|
|
1963
1995
|
usage: response.usage ? convertXaiResponsesUsage(response.usage) : {
|
|
1964
1996
|
inputTokens: { total: 0, noCache: 0, cacheRead: 0, cacheWrite: 0 },
|
|
1965
1997
|
outputTokens: { total: 0, text: 0, reasoning: 0 }
|
|
1966
1998
|
},
|
|
1967
|
-
...(
|
|
1999
|
+
...(response.usage?.cost_in_usd_ticks != null || response.service_tier != null || response.prompt_cache_key != null || response.safety_identifier != null) && {
|
|
1968
2000
|
providerMetadata: {
|
|
1969
2001
|
xai: {
|
|
1970
|
-
...
|
|
2002
|
+
...response.usage?.cost_in_usd_ticks != null && {
|
|
1971
2003
|
costInUsdTicks: response.usage.cost_in_usd_ticks
|
|
1972
2004
|
},
|
|
1973
2005
|
...response.service_tier != null && {
|
|
1974
2006
|
serviceTier: response.service_tier
|
|
2007
|
+
},
|
|
2008
|
+
...response.prompt_cache_key != null && {
|
|
2009
|
+
promptCacheKey: response.prompt_cache_key
|
|
2010
|
+
},
|
|
2011
|
+
...response.safety_identifier != null && {
|
|
2012
|
+
safetyIdentifier: response.safety_identifier
|
|
1975
2013
|
}
|
|
1976
2014
|
}
|
|
1977
2015
|
}
|
|
@@ -1986,7 +2024,6 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
1986
2024
|
};
|
|
1987
2025
|
}
|
|
1988
2026
|
async doStream(options) {
|
|
1989
|
-
var _a, _b, _c;
|
|
1990
2027
|
const {
|
|
1991
2028
|
args,
|
|
1992
2029
|
warnings,
|
|
@@ -2002,8 +2039,8 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
2002
2039
|
stream: true
|
|
2003
2040
|
};
|
|
2004
2041
|
const { responseHeaders, value: response } = await postJsonToApi2({
|
|
2005
|
-
url: `${
|
|
2006
|
-
headers: combineHeaders2(
|
|
2042
|
+
url: `${this.config.baseURL ?? "https://api.x.ai/v1"}/responses`,
|
|
2043
|
+
headers: combineHeaders2(this.config.headers?.(), options.headers),
|
|
2007
2044
|
body,
|
|
2008
2045
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
2009
2046
|
successfulResponseHandler: createEventSourceResponseHandler(
|
|
@@ -2020,6 +2057,8 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
2020
2057
|
let usage = void 0;
|
|
2021
2058
|
let costInUsdTicks = void 0;
|
|
2022
2059
|
let serviceTier = void 0;
|
|
2060
|
+
let promptCacheKey = void 0;
|
|
2061
|
+
let safetyIdentifier = void 0;
|
|
2023
2062
|
let isFirstChunk = true;
|
|
2024
2063
|
const contentBlocks = {};
|
|
2025
2064
|
const seenToolCalls = /* @__PURE__ */ new Set();
|
|
@@ -2033,7 +2072,6 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
2033
2072
|
controller.enqueue({ type: "stream-start", warnings });
|
|
2034
2073
|
},
|
|
2035
2074
|
transform(chunk, controller) {
|
|
2036
|
-
var _a2, _b2, _c2, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
|
|
2037
2075
|
if (options.includeRawChunks) {
|
|
2038
2076
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
2039
2077
|
}
|
|
@@ -2138,7 +2176,7 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
2138
2176
|
sourceType: "url",
|
|
2139
2177
|
id: self.config.generateId(),
|
|
2140
2178
|
url: annotation.url,
|
|
2141
|
-
title:
|
|
2179
|
+
title: annotation.title ?? annotation.url
|
|
2142
2180
|
});
|
|
2143
2181
|
}
|
|
2144
2182
|
}
|
|
@@ -2153,7 +2191,7 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
2153
2191
|
sourceType: "url",
|
|
2154
2192
|
id: self.config.generateId(),
|
|
2155
2193
|
url: annotation.url,
|
|
2156
|
-
title:
|
|
2194
|
+
title: annotation.title ?? annotation.url
|
|
2157
2195
|
});
|
|
2158
2196
|
}
|
|
2159
2197
|
return;
|
|
@@ -2162,14 +2200,16 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
2162
2200
|
const response2 = event.response;
|
|
2163
2201
|
if (response2.usage) {
|
|
2164
2202
|
usage = convertXaiResponsesUsage(response2.usage);
|
|
2165
|
-
costInUsdTicks =
|
|
2203
|
+
costInUsdTicks = response2.usage.cost_in_usd_ticks ?? void 0;
|
|
2166
2204
|
}
|
|
2167
|
-
serviceTier =
|
|
2205
|
+
serviceTier = response2.service_tier ?? void 0;
|
|
2206
|
+
promptCacheKey = response2.prompt_cache_key ?? void 0;
|
|
2207
|
+
safetyIdentifier = response2.safety_identifier ?? void 0;
|
|
2168
2208
|
if (event.type === "response.incomplete") {
|
|
2169
|
-
const reason = "incomplete_details" in response2 ?
|
|
2209
|
+
const reason = "incomplete_details" in response2 ? response2.incomplete_details?.reason : void 0;
|
|
2170
2210
|
finishReason = {
|
|
2171
2211
|
unified: reason ? mapXaiResponsesFinishReason(reason) : "other",
|
|
2172
|
-
raw: reason
|
|
2212
|
+
raw: reason ?? "incomplete"
|
|
2173
2213
|
};
|
|
2174
2214
|
} else if ("status" in response2 && response2.status) {
|
|
2175
2215
|
finishReason = {
|
|
@@ -2180,10 +2220,10 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
2180
2220
|
return;
|
|
2181
2221
|
}
|
|
2182
2222
|
if (event.type === "response.failed") {
|
|
2183
|
-
const reason =
|
|
2223
|
+
const reason = event.response.incomplete_details?.reason;
|
|
2184
2224
|
finishReason = {
|
|
2185
2225
|
unified: reason ? mapXaiResponsesFinishReason(reason) : "error",
|
|
2186
|
-
raw: reason
|
|
2226
|
+
raw: reason ?? "error"
|
|
2187
2227
|
};
|
|
2188
2228
|
if (event.response.usage) {
|
|
2189
2229
|
usage = convertXaiResponsesUsage(event.response.usage);
|
|
@@ -2233,7 +2273,7 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
2233
2273
|
if (event.type === "response.image_generation_call.in_progress" || event.type === "response.image_generation_call.generating" || event.type === "response.image_generation_call.completed") {
|
|
2234
2274
|
if (!seenToolCalls.has(event.item_id)) {
|
|
2235
2275
|
seenToolCalls.add(event.item_id);
|
|
2236
|
-
const toolName = imageGenerationToolName
|
|
2276
|
+
const toolName = imageGenerationToolName ?? "image_generation";
|
|
2237
2277
|
controller.enqueue({
|
|
2238
2278
|
type: "tool-input-start",
|
|
2239
2279
|
id: event.item_id,
|
|
@@ -2292,7 +2332,7 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
2292
2332
|
return;
|
|
2293
2333
|
}
|
|
2294
2334
|
if (part.type === "file_search_call") {
|
|
2295
|
-
const toolName = fileSearchToolName
|
|
2335
|
+
const toolName = fileSearchToolName ?? "file_search";
|
|
2296
2336
|
if (!seenToolCalls.has(part.id)) {
|
|
2297
2337
|
seenToolCalls.add(part.id);
|
|
2298
2338
|
controller.enqueue({
|
|
@@ -2323,20 +2363,20 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
2323
2363
|
toolCallId: part.id,
|
|
2324
2364
|
toolName,
|
|
2325
2365
|
result: {
|
|
2326
|
-
queries:
|
|
2327
|
-
results:
|
|
2366
|
+
queries: part.queries ?? [],
|
|
2367
|
+
results: part.results?.map((result) => ({
|
|
2328
2368
|
fileId: result.file_id,
|
|
2329
2369
|
filename: result.filename,
|
|
2330
2370
|
score: result.score,
|
|
2331
2371
|
text: result.text
|
|
2332
|
-
}))
|
|
2372
|
+
})) ?? null
|
|
2333
2373
|
}
|
|
2334
2374
|
});
|
|
2335
2375
|
}
|
|
2336
2376
|
return;
|
|
2337
2377
|
}
|
|
2338
2378
|
if (part.type === "image_generation_call") {
|
|
2339
|
-
const toolName = imageGenerationToolName
|
|
2379
|
+
const toolName = imageGenerationToolName ?? "image_generation";
|
|
2340
2380
|
if (!seenToolCalls.has(part.id)) {
|
|
2341
2381
|
seenToolCalls.add(part.id);
|
|
2342
2382
|
controller.enqueue({
|
|
@@ -2396,17 +2436,17 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
2396
2436
|
"x_semantic_search",
|
|
2397
2437
|
"x_thread_fetch"
|
|
2398
2438
|
];
|
|
2399
|
-
let toolName =
|
|
2400
|
-
if (webSearchSubTools.includes(
|
|
2401
|
-
toolName = webSearchToolName
|
|
2402
|
-
} else if (xSearchSubTools.includes(
|
|
2403
|
-
toolName = xSearchToolName
|
|
2439
|
+
let toolName = part.name ?? "";
|
|
2440
|
+
if (webSearchSubTools.includes(part.name ?? "") || part.type === "web_search_call") {
|
|
2441
|
+
toolName = webSearchToolName ?? "web_search";
|
|
2442
|
+
} else if (xSearchSubTools.includes(part.name ?? "") || part.type === "x_search_call") {
|
|
2443
|
+
toolName = xSearchToolName ?? "x_search";
|
|
2404
2444
|
} else if (part.name === "code_execution" || part.type === "code_interpreter_call" || part.type === "code_execution_call") {
|
|
2405
|
-
toolName = codeExecutionToolName
|
|
2445
|
+
toolName = codeExecutionToolName ?? "code_execution";
|
|
2406
2446
|
} else if (part.type === "mcp_call") {
|
|
2407
|
-
toolName =
|
|
2447
|
+
toolName = mcpToolName ?? part.name ?? "mcp";
|
|
2408
2448
|
}
|
|
2409
|
-
const toolInput = part.type === "custom_tool_call" ?
|
|
2449
|
+
const toolInput = part.type === "custom_tool_call" ? part.input ?? "" : part.type === "mcp_call" ? part.arguments ?? "" : part.arguments ?? "";
|
|
2410
2450
|
const shouldEmit = part.type === "custom_tool_call" ? event.type === "response.output_item.done" : !seenToolCalls.has(part.id);
|
|
2411
2451
|
if (shouldEmit && !seenToolCalls.has(part.id)) {
|
|
2412
2452
|
seenToolCalls.add(part.id);
|
|
@@ -2467,7 +2507,7 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
2467
2507
|
sourceType: "url",
|
|
2468
2508
|
id: self.config.generateId(),
|
|
2469
2509
|
url: annotation.url,
|
|
2470
|
-
title:
|
|
2510
|
+
title: annotation.title ?? annotation.url
|
|
2471
2511
|
});
|
|
2472
2512
|
}
|
|
2473
2513
|
}
|
|
@@ -2513,7 +2553,7 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
2513
2553
|
controller.enqueue({
|
|
2514
2554
|
type: "finish",
|
|
2515
2555
|
finishReason,
|
|
2516
|
-
usage: usage
|
|
2556
|
+
usage: usage ?? {
|
|
2517
2557
|
inputTokens: {
|
|
2518
2558
|
total: 0,
|
|
2519
2559
|
noCache: 0,
|
|
@@ -2522,11 +2562,13 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
2522
2562
|
},
|
|
2523
2563
|
outputTokens: { total: 0, text: 0, reasoning: 0 }
|
|
2524
2564
|
},
|
|
2525
|
-
...(costInUsdTicks != null || serviceTier != null) && {
|
|
2565
|
+
...(costInUsdTicks != null || serviceTier != null || promptCacheKey != null || safetyIdentifier != null) && {
|
|
2526
2566
|
providerMetadata: {
|
|
2527
2567
|
xai: {
|
|
2528
2568
|
...costInUsdTicks != null && { costInUsdTicks },
|
|
2529
|
-
...serviceTier != null && { serviceTier }
|
|
2569
|
+
...serviceTier != null && { serviceTier },
|
|
2570
|
+
...promptCacheKey != null && { promptCacheKey },
|
|
2571
|
+
...safetyIdentifier != null && { safetyIdentifier }
|
|
2530
2572
|
}
|
|
2531
2573
|
}
|
|
2532
2574
|
}
|
|
@@ -2540,11 +2582,10 @@ var XaiResponsesLanguageModel = class _XaiResponsesLanguageModel {
|
|
|
2540
2582
|
}
|
|
2541
2583
|
};
|
|
2542
2584
|
function mapWebSearchAction(action) {
|
|
2543
|
-
var _a;
|
|
2544
2585
|
const parsed = webSearchWireActionSchema.safeParse(action);
|
|
2545
2586
|
if (!parsed.success) return {};
|
|
2546
2587
|
const a = parsed.data;
|
|
2547
|
-
const sources =
|
|
2588
|
+
const sources = a.sources?.flatMap((s) => {
|
|
2548
2589
|
const source = webSearchWireSourceSchema.safeParse(s);
|
|
2549
2590
|
return source.success ? [source.data] : [];
|
|
2550
2591
|
});
|
|
@@ -2702,7 +2743,6 @@ var XaiBatch = class {
|
|
|
2702
2743
|
this.provider = options.provider;
|
|
2703
2744
|
}
|
|
2704
2745
|
async doStartBatch(options) {
|
|
2705
|
-
var _a, _b;
|
|
2706
2746
|
assertSupportedBatchRequests(options.requests);
|
|
2707
2747
|
const fileParts = [];
|
|
2708
2748
|
const warnings = options.webhookUrl == null ? [] : [
|
|
@@ -2738,7 +2778,7 @@ var XaiBatch = class {
|
|
|
2738
2778
|
const file = new Blob(fileParts, { type: "application/jsonl" });
|
|
2739
2779
|
fileParts.length = 0;
|
|
2740
2780
|
const formData = new FormData();
|
|
2741
|
-
if (
|
|
2781
|
+
if (batchOptions?.inputFileExpiresAfter != null) {
|
|
2742
2782
|
formData.append(
|
|
2743
2783
|
"expires_after",
|
|
2744
2784
|
String(batchOptions.inputFileExpiresAfter)
|
|
@@ -2746,7 +2786,7 @@ var XaiBatch = class {
|
|
|
2746
2786
|
}
|
|
2747
2787
|
formData.append("file", file, filename);
|
|
2748
2788
|
const headers = combineHeaders3(
|
|
2749
|
-
|
|
2789
|
+
this.options.config.headers?.(),
|
|
2750
2790
|
options.headers
|
|
2751
2791
|
);
|
|
2752
2792
|
const { value: uploadedFile } = await postFormDataToApi({
|
|
@@ -2794,12 +2834,11 @@ var XaiBatch = class {
|
|
|
2794
2834
|
return convertXaiBatchStatus(await this.retrieveBatch(options));
|
|
2795
2835
|
}
|
|
2796
2836
|
async doCancelBatch(options) {
|
|
2797
|
-
var _a, _b;
|
|
2798
2837
|
await postJsonToApi3({
|
|
2799
2838
|
url: this.getUrl(
|
|
2800
2839
|
`/batches/${encodeURIComponent(options.batchId)}:cancel`
|
|
2801
2840
|
),
|
|
2802
|
-
headers: combineHeaders3(
|
|
2841
|
+
headers: combineHeaders3(this.options.config.headers?.(), options.headers),
|
|
2803
2842
|
body: {},
|
|
2804
2843
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
2805
2844
|
successfulResponseHandler: createJsonResponseHandler3(
|
|
@@ -2811,7 +2850,6 @@ var XaiBatch = class {
|
|
|
2811
2850
|
return {};
|
|
2812
2851
|
}
|
|
2813
2852
|
async doListBatches(options) {
|
|
2814
|
-
var _a, _b;
|
|
2815
2853
|
const url = new URL(this.getUrl("/batches"));
|
|
2816
2854
|
if (options.limit != null) {
|
|
2817
2855
|
url.searchParams.set("limit", String(options.limit));
|
|
@@ -2821,7 +2859,7 @@ var XaiBatch = class {
|
|
|
2821
2859
|
}
|
|
2822
2860
|
const { value: page } = await getFromApi2({
|
|
2823
2861
|
url: url.toString(),
|
|
2824
|
-
headers: combineHeaders3(
|
|
2862
|
+
headers: combineHeaders3(this.options.config.headers?.(), options.headers),
|
|
2825
2863
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
2826
2864
|
successfulResponseHandler: createJsonResponseHandler3(
|
|
2827
2865
|
xaiBatchListResponseSchema
|
|
@@ -2851,10 +2889,9 @@ var XaiBatch = class {
|
|
|
2851
2889
|
);
|
|
2852
2890
|
}
|
|
2853
2891
|
async retrieveBatch(options) {
|
|
2854
|
-
var _a, _b;
|
|
2855
2892
|
const { value: batch } = await getFromApi2({
|
|
2856
2893
|
url: this.getUrl(`/batches/${encodeURIComponent(options.batchId)}`),
|
|
2857
|
-
headers: combineHeaders3(
|
|
2894
|
+
headers: combineHeaders3(this.options.config.headers?.(), options.headers),
|
|
2858
2895
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
2859
2896
|
successfulResponseHandler: createJsonResponseHandler3(
|
|
2860
2897
|
xaiBatchResponseSchema
|
|
@@ -2866,7 +2903,6 @@ var XaiBatch = class {
|
|
|
2866
2903
|
return batch;
|
|
2867
2904
|
}
|
|
2868
2905
|
async *iterateBatchResults(options) {
|
|
2869
|
-
var _a, _b, _c;
|
|
2870
2906
|
let paginationToken;
|
|
2871
2907
|
do {
|
|
2872
2908
|
const query = new URLSearchParams({
|
|
@@ -2880,7 +2916,7 @@ var XaiBatch = class {
|
|
|
2880
2916
|
`/batches/${encodeURIComponent(options.batchId)}/results?${query}`
|
|
2881
2917
|
),
|
|
2882
2918
|
headers: combineHeaders3(
|
|
2883
|
-
|
|
2919
|
+
this.options.config.headers?.(),
|
|
2884
2920
|
options.headers
|
|
2885
2921
|
),
|
|
2886
2922
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
@@ -2894,23 +2930,22 @@ var XaiBatch = class {
|
|
|
2894
2930
|
for (const result of page.results) {
|
|
2895
2931
|
yield await this.convertBatchResult(result, options.abortSignal);
|
|
2896
2932
|
}
|
|
2897
|
-
paginationToken =
|
|
2933
|
+
paginationToken = page.pagination_token ?? void 0;
|
|
2898
2934
|
} while (paginationToken != null);
|
|
2899
2935
|
}
|
|
2900
2936
|
async convertBatchResult(result, abortSignal) {
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
if (((_c = (_b = result.error_message) == null ? void 0 : _b.length) != null ? _c : 0) > 0 || (error == null ? void 0 : error.code) != null && error.code !== 0 && error.code !== "0" || (error == null ? void 0 : error.code) == null && ((_e = (_d = error == null ? void 0 : error.message) == null ? void 0 : _d.length) != null ? _e : 0) > 0) {
|
|
2937
|
+
const error = result.batch_result?.error;
|
|
2938
|
+
if ((result.error_message?.length ?? 0) > 0 || error?.code != null && error.code !== 0 && error.code !== "0" || error?.code == null && (error?.message?.length ?? 0) > 0) {
|
|
2904
2939
|
const convertedError = convertXaiBatchError(result);
|
|
2905
2940
|
return {
|
|
2906
2941
|
type: "text",
|
|
2907
2942
|
id: result.batch_request_id,
|
|
2908
|
-
status: isXaiCancellationError(error
|
|
2943
|
+
status: isXaiCancellationError(error?.code) ? "cancelled" : "failed",
|
|
2909
2944
|
error: convertedError
|
|
2910
2945
|
};
|
|
2911
2946
|
}
|
|
2912
|
-
const response =
|
|
2913
|
-
if (
|
|
2947
|
+
const response = result.batch_result?.response;
|
|
2948
|
+
if (response?.chat_get_completion != null) {
|
|
2914
2949
|
const validation = await safeValidateTypes({
|
|
2915
2950
|
value: response.chat_get_completion,
|
|
2916
2951
|
schema: zodSchema7(xaiBatchTextResponseSchema)
|
|
@@ -2931,7 +2966,7 @@ var XaiBatch = class {
|
|
|
2931
2966
|
error: conversion.error
|
|
2932
2967
|
};
|
|
2933
2968
|
}
|
|
2934
|
-
if (
|
|
2969
|
+
if (response?.image_generation != null) {
|
|
2935
2970
|
const validation = await safeValidateTypes({
|
|
2936
2971
|
value: response.image_generation,
|
|
2937
2972
|
schema: zodSchema7(xaiBatchImageResponseSchema)
|
|
@@ -2986,7 +3021,7 @@ var XaiBatch = class {
|
|
|
2986
3021
|
providerOptions,
|
|
2987
3022
|
schema: xaiImageModelOptions
|
|
2988
3023
|
});
|
|
2989
|
-
const imageUrls = (files
|
|
3024
|
+
const imageUrls = (files ?? []).map(convertImageModelFileToDataUri2);
|
|
2990
3025
|
const body = {
|
|
2991
3026
|
model: request.modelId,
|
|
2992
3027
|
prompt,
|
|
@@ -2994,27 +3029,26 @@ var XaiBatch = class {
|
|
|
2994
3029
|
response_format: "b64_json"
|
|
2995
3030
|
};
|
|
2996
3031
|
if (aspectRatio != null) body.aspect_ratio = aspectRatio;
|
|
2997
|
-
if (
|
|
3032
|
+
if (xaiOptions?.output_format != null)
|
|
2998
3033
|
body.output_format = xaiOptions.output_format;
|
|
2999
|
-
if (
|
|
3000
|
-
if (
|
|
3034
|
+
if (xaiOptions?.sync_mode != null) body.sync_mode = xaiOptions.sync_mode;
|
|
3035
|
+
if (xaiOptions?.aspect_ratio != null && aspectRatio == null)
|
|
3001
3036
|
body.aspect_ratio = xaiOptions.aspect_ratio;
|
|
3002
|
-
if (
|
|
3003
|
-
if (
|
|
3004
|
-
if (
|
|
3037
|
+
if (xaiOptions?.resolution != null) body.resolution = xaiOptions.resolution;
|
|
3038
|
+
if (xaiOptions?.quality != null) body.quality = xaiOptions.quality;
|
|
3039
|
+
if (xaiOptions?.user != null) body.user = xaiOptions.user;
|
|
3005
3040
|
if (imageUrls.length === 1) {
|
|
3006
3041
|
body.image = { url: imageUrls[0], type: "image_url" };
|
|
3007
3042
|
} else if (imageUrls.length > 1) {
|
|
3008
3043
|
body.images = imageUrls.map((url) => ({ url, type: "image_url" }));
|
|
3009
3044
|
}
|
|
3010
3045
|
return {
|
|
3011
|
-
endpoint:
|
|
3046
|
+
endpoint: files?.length ? "/v1/images/edits" : "/v1/images/generations",
|
|
3012
3047
|
body,
|
|
3013
3048
|
warnings
|
|
3014
3049
|
};
|
|
3015
3050
|
}
|
|
3016
3051
|
async convertImageBatchResponse(response, abortSignal) {
|
|
3017
|
-
var _a;
|
|
3018
3052
|
const hasAllBase64 = response.data.every((image) => image.b64_json != null);
|
|
3019
3053
|
const images = hasAllBase64 ? response.data.map((image) => image.b64_json) : await Promise.all(
|
|
3020
3054
|
response.data.map(async (image) => {
|
|
@@ -3048,23 +3082,21 @@ var XaiBatch = class {
|
|
|
3048
3082
|
images: response.data.map(
|
|
3049
3083
|
(item) => item.revised_prompt != null ? { revisedPrompt: item.revised_prompt } : {}
|
|
3050
3084
|
),
|
|
3051
|
-
...
|
|
3085
|
+
...response.usage?.cost_in_usd_ticks != null ? { costInUsdTicks: response.usage.cost_in_usd_ticks } : {}
|
|
3052
3086
|
}
|
|
3053
3087
|
}
|
|
3054
3088
|
};
|
|
3055
3089
|
}
|
|
3056
3090
|
getUrl(path) {
|
|
3057
|
-
|
|
3058
|
-
return `${(_a = this.options.config.baseURL) != null ? _a : "https://api.x.ai/v1"}${path}`;
|
|
3091
|
+
return `${this.options.config.baseURL ?? "https://api.x.ai/v1"}${path}`;
|
|
3059
3092
|
}
|
|
3060
3093
|
};
|
|
3061
3094
|
function convertXaiBatchStatus(batch) {
|
|
3062
|
-
var _a, _b, _c, _d, _e, _f;
|
|
3063
3095
|
const requestCounts = normalizeBatchRequestCounts({
|
|
3064
|
-
total:
|
|
3065
|
-
pending:
|
|
3066
|
-
completed:
|
|
3067
|
-
failed:
|
|
3096
|
+
total: batch.state?.num_requests,
|
|
3097
|
+
pending: batch.state?.num_pending,
|
|
3098
|
+
completed: batch.state?.num_success,
|
|
3099
|
+
failed: batch.state?.num_error != null && batch.state?.num_cancelled != null ? batch.state.num_error + batch.state.num_cancelled : void 0
|
|
3068
3100
|
});
|
|
3069
3101
|
const isCancelled = batch.cancel_time != null || batch.cancel_by_xai_message != null;
|
|
3070
3102
|
const isExpired = isPastDate(batch.expire_time);
|
|
@@ -3074,7 +3106,7 @@ function convertXaiBatchStatus(batch) {
|
|
|
3074
3106
|
...requestCounts != null ? { requestCounts } : {},
|
|
3075
3107
|
...isCancelled ? {
|
|
3076
3108
|
error: {
|
|
3077
|
-
message:
|
|
3109
|
+
message: batch.cancel_by_xai_message ?? `xAI batch "${batch.batch_id}" was cancelled.`,
|
|
3078
3110
|
code: "batch_cancelled"
|
|
3079
3111
|
}
|
|
3080
3112
|
} : isExpired ? {
|
|
@@ -3095,11 +3127,10 @@ function isPastDate(value) {
|
|
|
3095
3127
|
return !Number.isNaN(timestamp) && timestamp <= Date.now();
|
|
3096
3128
|
}
|
|
3097
3129
|
function convertXaiBatchError(result) {
|
|
3098
|
-
|
|
3099
|
-
const error = (_a = result.batch_result) == null ? void 0 : _a.error;
|
|
3130
|
+
const error = result.batch_result?.error;
|
|
3100
3131
|
return {
|
|
3101
|
-
message: result.error_message ||
|
|
3102
|
-
...
|
|
3132
|
+
message: result.error_message || error?.message || "xAI batch request failed.",
|
|
3133
|
+
...error?.code != null && error.code !== 0 && error.code !== "0" ? { code: String(error.code) } : {}
|
|
3103
3134
|
};
|
|
3104
3135
|
}
|
|
3105
3136
|
function isXaiCancellationError(code) {
|
|
@@ -3129,7 +3160,6 @@ function invalidXaiImageBatchResult(id) {
|
|
|
3129
3160
|
};
|
|
3130
3161
|
}
|
|
3131
3162
|
function convertXaiBatchTextResponse(response) {
|
|
3132
|
-
var _a, _b, _c, _d, _e, _f;
|
|
3133
3163
|
if (response.error != null) {
|
|
3134
3164
|
return {
|
|
3135
3165
|
success: false,
|
|
@@ -3152,17 +3182,14 @@ function convertXaiBatchTextResponse(response) {
|
|
|
3152
3182
|
const content = [];
|
|
3153
3183
|
const providerExecutedToolCallIds = new Set(
|
|
3154
3184
|
choices.filter((choice) => choice.message.role === "tool").flatMap(
|
|
3155
|
-
(choice) =>
|
|
3156
|
-
var _a2;
|
|
3157
|
-
return ((_a2 = choice.message.tool_calls) != null ? _a2 : []).map((toolCall) => toolCall.id);
|
|
3158
|
-
}
|
|
3185
|
+
(choice) => (choice.message.tool_calls ?? []).map((toolCall) => toolCall.id)
|
|
3159
3186
|
)
|
|
3160
3187
|
);
|
|
3161
3188
|
let lastAssistantChoice;
|
|
3162
3189
|
for (const choice of choices) {
|
|
3163
3190
|
if (choice.message.role === "tool") {
|
|
3164
3191
|
if (choice.message.content != null) {
|
|
3165
|
-
for (const toolCall of
|
|
3192
|
+
for (const toolCall of choice.message.tool_calls ?? []) {
|
|
3166
3193
|
content.push({
|
|
3167
3194
|
type: "tool-result",
|
|
3168
3195
|
toolCallId: toolCall.id,
|
|
@@ -3184,7 +3211,7 @@ function convertXaiBatchTextResponse(response) {
|
|
|
3184
3211
|
text: choice.message.reasoning_content
|
|
3185
3212
|
});
|
|
3186
3213
|
}
|
|
3187
|
-
for (const toolCall of
|
|
3214
|
+
for (const toolCall of choice.message.tool_calls ?? []) {
|
|
3188
3215
|
content.push({
|
|
3189
3216
|
type: "tool-call",
|
|
3190
3217
|
toolCallId: toolCall.id,
|
|
@@ -3194,7 +3221,7 @@ function convertXaiBatchTextResponse(response) {
|
|
|
3194
3221
|
});
|
|
3195
3222
|
}
|
|
3196
3223
|
}
|
|
3197
|
-
for (const url of
|
|
3224
|
+
for (const url of response.citations ?? []) {
|
|
3198
3225
|
content.push({
|
|
3199
3226
|
type: "source",
|
|
3200
3227
|
sourceType: "url",
|
|
@@ -3207,16 +3234,16 @@ function convertXaiBatchTextResponse(response) {
|
|
|
3207
3234
|
result: {
|
|
3208
3235
|
content,
|
|
3209
3236
|
finishReason: {
|
|
3210
|
-
unified: mapXaiFinishReason(lastAssistantChoice
|
|
3211
|
-
raw:
|
|
3237
|
+
unified: mapXaiFinishReason(lastAssistantChoice?.finish_reason),
|
|
3238
|
+
raw: lastAssistantChoice?.finish_reason ?? void 0
|
|
3212
3239
|
},
|
|
3213
3240
|
usage: response.usage ? convertXaiBatchTextUsage(response.usage) : createNullLanguageModelUsage(),
|
|
3214
3241
|
response: getResponseMetadata(response),
|
|
3215
3242
|
warnings: [],
|
|
3216
|
-
...(
|
|
3243
|
+
...(response.usage?.cost_in_usd_ticks != null || response.service_tier != null) && {
|
|
3217
3244
|
providerMetadata: {
|
|
3218
3245
|
xai: {
|
|
3219
|
-
...
|
|
3246
|
+
...response.usage?.cost_in_usd_ticks != null ? { costInUsdTicks: response.usage.cost_in_usd_ticks } : {},
|
|
3220
3247
|
...response.service_tier != null ? { serviceTier: response.service_tier } : {}
|
|
3221
3248
|
}
|
|
3222
3249
|
}
|
|
@@ -3225,9 +3252,8 @@ function convertXaiBatchTextResponse(response) {
|
|
|
3225
3252
|
};
|
|
3226
3253
|
}
|
|
3227
3254
|
function convertXaiBatchTextUsage(usage) {
|
|
3228
|
-
|
|
3229
|
-
const
|
|
3230
|
-
const reasoningTokens = (_d = (_c = usage.completion_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0;
|
|
3255
|
+
const cacheReadTokens = usage.prompt_tokens_details?.cached_tokens ?? 0;
|
|
3256
|
+
const reasoningTokens = usage.completion_tokens_details?.reasoning_tokens ?? 0;
|
|
3231
3257
|
const promptTokensIncludesCached = cacheReadTokens <= usage.prompt_tokens;
|
|
3232
3258
|
return {
|
|
3233
3259
|
inputTokens: {
|
|
@@ -3247,14 +3273,13 @@ function convertXaiBatchTextUsage(usage) {
|
|
|
3247
3273
|
|
|
3248
3274
|
// src/realtime/xai-realtime-event-mapper.ts
|
|
3249
3275
|
function parseXaiRealtimeServerEvent(raw) {
|
|
3250
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
|
|
3251
3276
|
const event = raw;
|
|
3252
3277
|
const type = event.type;
|
|
3253
3278
|
switch (type) {
|
|
3254
3279
|
case "session.created":
|
|
3255
3280
|
return {
|
|
3256
3281
|
type: "session-created",
|
|
3257
|
-
sessionId:
|
|
3282
|
+
sessionId: event.session?.id,
|
|
3258
3283
|
raw
|
|
3259
3284
|
};
|
|
3260
3285
|
case "session.updated":
|
|
@@ -3283,7 +3308,7 @@ function parseXaiRealtimeServerEvent(raw) {
|
|
|
3283
3308
|
case "conversation.item.added":
|
|
3284
3309
|
return {
|
|
3285
3310
|
type: "conversation-item-added",
|
|
3286
|
-
itemId:
|
|
3311
|
+
itemId: event.item?.id ?? event.item_id,
|
|
3287
3312
|
item: event.item,
|
|
3288
3313
|
raw
|
|
3289
3314
|
};
|
|
@@ -3291,34 +3316,34 @@ function parseXaiRealtimeServerEvent(raw) {
|
|
|
3291
3316
|
return {
|
|
3292
3317
|
type: "input-transcription-completed",
|
|
3293
3318
|
itemId: event.item_id,
|
|
3294
|
-
transcript:
|
|
3319
|
+
transcript: event.transcript ?? "",
|
|
3295
3320
|
raw
|
|
3296
3321
|
};
|
|
3297
3322
|
case "response.created":
|
|
3298
3323
|
return {
|
|
3299
3324
|
type: "response-created",
|
|
3300
|
-
responseId:
|
|
3325
|
+
responseId: event.response?.id ?? event.response_id,
|
|
3301
3326
|
raw
|
|
3302
3327
|
};
|
|
3303
3328
|
case "response.done":
|
|
3304
3329
|
return {
|
|
3305
3330
|
type: "response-done",
|
|
3306
|
-
responseId:
|
|
3307
|
-
status:
|
|
3331
|
+
responseId: event.response?.id ?? event.response_id,
|
|
3332
|
+
status: event.response?.status ?? "completed",
|
|
3308
3333
|
raw
|
|
3309
3334
|
};
|
|
3310
3335
|
case "response.output_item.added":
|
|
3311
3336
|
return {
|
|
3312
3337
|
type: "output-item-added",
|
|
3313
3338
|
responseId: event.response_id,
|
|
3314
|
-
itemId:
|
|
3339
|
+
itemId: event.item?.id ?? event.item_id,
|
|
3315
3340
|
raw
|
|
3316
3341
|
};
|
|
3317
3342
|
case "response.output_item.done":
|
|
3318
3343
|
return {
|
|
3319
3344
|
type: "output-item-done",
|
|
3320
3345
|
responseId: event.response_id,
|
|
3321
|
-
itemId:
|
|
3346
|
+
itemId: event.item?.id ?? event.item_id,
|
|
3322
3347
|
raw
|
|
3323
3348
|
};
|
|
3324
3349
|
case "response.content_part.added":
|
|
@@ -3413,8 +3438,8 @@ function parseXaiRealtimeServerEvent(raw) {
|
|
|
3413
3438
|
case "error":
|
|
3414
3439
|
return {
|
|
3415
3440
|
type: "error",
|
|
3416
|
-
message:
|
|
3417
|
-
code:
|
|
3441
|
+
message: event.error?.message ?? event.message ?? "Unknown error",
|
|
3442
|
+
code: event.error?.code ?? event.code,
|
|
3418
3443
|
raw
|
|
3419
3444
|
};
|
|
3420
3445
|
default:
|
|
@@ -3487,7 +3512,6 @@ function serializeXaiRealtimeClientEvent(event) {
|
|
|
3487
3512
|
}
|
|
3488
3513
|
}
|
|
3489
3514
|
function buildXaiSessionConfig(config) {
|
|
3490
|
-
var _a;
|
|
3491
3515
|
const session = {};
|
|
3492
3516
|
if (config.instructions != null) {
|
|
3493
3517
|
session.instructions = config.instructions;
|
|
@@ -3545,7 +3569,7 @@ function buildXaiSessionConfig(config) {
|
|
|
3545
3569
|
if (config.providerOptions != null) {
|
|
3546
3570
|
const xaiOptions = config.providerOptions;
|
|
3547
3571
|
if (Array.isArray(xaiOptions.tools)) {
|
|
3548
|
-
const existingTools =
|
|
3572
|
+
const existingTools = session.tools ?? [];
|
|
3549
3573
|
session.tools = [...existingTools, ...xaiOptions.tools];
|
|
3550
3574
|
}
|
|
3551
3575
|
for (const [key, value] of Object.entries(xaiOptions)) {
|
|
@@ -3566,8 +3590,7 @@ var XaiRealtimeModel = class {
|
|
|
3566
3590
|
this.config = config;
|
|
3567
3591
|
}
|
|
3568
3592
|
async doCreateClientSecret(options) {
|
|
3569
|
-
|
|
3570
|
-
const fetchFn = (_a = this.config.fetch) != null ? _a : fetch;
|
|
3593
|
+
const fetchFn = this.config.fetch ?? fetch;
|
|
3571
3594
|
const url = `${this.config.baseURL}/realtime/client_secrets`;
|
|
3572
3595
|
const body = {};
|
|
3573
3596
|
if (options.expiresAfterSeconds != null) {
|
|
@@ -3670,7 +3693,7 @@ var xaiTools = {
|
|
|
3670
3693
|
};
|
|
3671
3694
|
|
|
3672
3695
|
// src/version.ts
|
|
3673
|
-
var VERSION = true ? "5.0.
|
|
3696
|
+
var VERSION = true ? "5.0.10" : "0.0.0-test";
|
|
3674
3697
|
|
|
3675
3698
|
// src/files/xai-files.ts
|
|
3676
3699
|
import {
|
|
@@ -3743,7 +3766,6 @@ var XaiFiles = class {
|
|
|
3743
3766
|
headers,
|
|
3744
3767
|
providerOptions
|
|
3745
3768
|
}) {
|
|
3746
|
-
var _a, _b;
|
|
3747
3769
|
let xaiOptions;
|
|
3748
3770
|
try {
|
|
3749
3771
|
xaiOptions = await parseProviderOptions5({
|
|
@@ -3763,14 +3785,14 @@ var XaiFiles = class {
|
|
|
3763
3785
|
let response;
|
|
3764
3786
|
if (data.type === "stream") {
|
|
3765
3787
|
const parts = [];
|
|
3766
|
-
if (
|
|
3788
|
+
if (xaiOptions?.expiresAfter != null) {
|
|
3767
3789
|
parts.push({
|
|
3768
3790
|
type: "field",
|
|
3769
3791
|
name: "expires_after",
|
|
3770
3792
|
value: String(xaiOptions.expiresAfter)
|
|
3771
3793
|
});
|
|
3772
3794
|
}
|
|
3773
|
-
if (
|
|
3795
|
+
if (xaiOptions?.teamId != null) {
|
|
3774
3796
|
parts.push({
|
|
3775
3797
|
type: "field",
|
|
3776
3798
|
name: "team_id",
|
|
@@ -3801,10 +3823,10 @@ var XaiFiles = class {
|
|
|
3801
3823
|
type: mediaType
|
|
3802
3824
|
});
|
|
3803
3825
|
const formData = new FormData();
|
|
3804
|
-
if (
|
|
3826
|
+
if (xaiOptions?.expiresAfter != null) {
|
|
3805
3827
|
formData.append("expires_after", String(xaiOptions.expiresAfter));
|
|
3806
3828
|
}
|
|
3807
|
-
if (
|
|
3829
|
+
if (xaiOptions?.teamId != null) {
|
|
3808
3830
|
formData.append("team_id", xaiOptions.teamId);
|
|
3809
3831
|
}
|
|
3810
3832
|
if (filename != null) {
|
|
@@ -3827,7 +3849,7 @@ var XaiFiles = class {
|
|
|
3827
3849
|
return {
|
|
3828
3850
|
warnings: [],
|
|
3829
3851
|
providerReference: { xai: response.id },
|
|
3830
|
-
...
|
|
3852
|
+
...response.filename ?? filename ? { filename: response.filename ?? filename } : {},
|
|
3831
3853
|
...mediaType != null ? { mediaType } : {},
|
|
3832
3854
|
...response.bytes != null ? { byteSize: response.bytes } : {},
|
|
3833
3855
|
...response.created_at != null ? { createdAt: new Date(response.created_at * 1e3) } : {},
|
|
@@ -3871,7 +3893,6 @@ var XaiFiles = class {
|
|
|
3871
3893
|
abortSignal,
|
|
3872
3894
|
headers
|
|
3873
3895
|
}) {
|
|
3874
|
-
var _a;
|
|
3875
3896
|
const fileId = this.getFileId(file);
|
|
3876
3897
|
const { value: content, responseHeaders } = await getFromApi3({
|
|
3877
3898
|
url: `${this.config.baseURL}/files/${encodePathSegment(fileId)}/content`,
|
|
@@ -3882,7 +3903,7 @@ var XaiFiles = class {
|
|
|
3882
3903
|
fetch: this.config.fetch,
|
|
3883
3904
|
validateUrl: false
|
|
3884
3905
|
});
|
|
3885
|
-
const mediaType =
|
|
3906
|
+
const mediaType = responseHeaders?.["content-type"]?.split(";")[0].trim();
|
|
3886
3907
|
return {
|
|
3887
3908
|
warnings: [],
|
|
3888
3909
|
content,
|
|
@@ -3947,6 +3968,20 @@ import { z as z18 } from "zod/v4";
|
|
|
3947
3968
|
var nonEmptyStringSchema = z18.string().min(1);
|
|
3948
3969
|
var resolutionSchema = z18.enum(["480p", "720p", "1080p"]);
|
|
3949
3970
|
var modeSchema = z18.enum(["edit-video", "extend-video", "reference-to-video"]);
|
|
3971
|
+
var keyframeSchema = z18.object({
|
|
3972
|
+
imageUrl: nonEmptyStringSchema,
|
|
3973
|
+
timestampSeconds: z18.number().positive()
|
|
3974
|
+
});
|
|
3975
|
+
var storageOptionsSchema = z18.object({
|
|
3976
|
+
filename: nonEmptyStringSchema,
|
|
3977
|
+
expiresAfter: z18.number().int().positive().max(2592e3).optional(),
|
|
3978
|
+
publicUrl: z18.union([
|
|
3979
|
+
z18.boolean(),
|
|
3980
|
+
z18.object({
|
|
3981
|
+
expiresAfter: z18.number().int().min(3600).max(2592e3).optional()
|
|
3982
|
+
})
|
|
3983
|
+
]).optional()
|
|
3984
|
+
});
|
|
3950
3985
|
var baseFields = {
|
|
3951
3986
|
pollIntervalMs: z18.number().positive().nullish(),
|
|
3952
3987
|
pollTimeoutMs: z18.number().positive().nullish(),
|
|
@@ -3957,6 +3992,8 @@ var runtimeSchema = z18.looseObject({
|
|
|
3957
3992
|
videoUrl: nonEmptyStringSchema.optional(),
|
|
3958
3993
|
referenceImageUrls: z18.array(nonEmptyStringSchema).min(1).max(7).optional(),
|
|
3959
3994
|
referenceVoiceIds: z18.array(nonEmptyStringSchema).max(3).optional(),
|
|
3995
|
+
keyframes: z18.array(keyframeSchema).max(4).optional(),
|
|
3996
|
+
storageOptions: storageOptionsSchema.optional(),
|
|
3960
3997
|
user: z18.string().optional(),
|
|
3961
3998
|
...baseFields
|
|
3962
3999
|
});
|
|
@@ -3976,19 +4013,17 @@ var RESOLUTION_MAP = {
|
|
|
3976
4013
|
"640x480": "480p"
|
|
3977
4014
|
};
|
|
3978
4015
|
function getFirstFrameImage(options) {
|
|
3979
|
-
|
|
3980
|
-
return (_b = (_a = options.frameImages) == null ? void 0 : _a.find((frame) => frame.frameType === "first_frame")) == null ? void 0 : _b.image;
|
|
4016
|
+
return options.frameImages?.find((frame) => frame.frameType === "first_frame")?.image;
|
|
3981
4017
|
}
|
|
3982
4018
|
function getLastFrameImage(options) {
|
|
3983
|
-
|
|
3984
|
-
return (_b = (_a = options.frameImages) == null ? void 0 : _a.find((frame) => frame.frameType === "last_frame")) == null ? void 0 : _b.image;
|
|
4019
|
+
return options.frameImages?.find((frame) => frame.frameType === "last_frame")?.image;
|
|
3985
4020
|
}
|
|
3986
4021
|
function resolveStartImage(options) {
|
|
3987
|
-
|
|
3988
|
-
return (_a = getFirstFrameImage(options)) != null ? _a : options.image;
|
|
4022
|
+
return getFirstFrameImage(options) ?? options.image;
|
|
3989
4023
|
}
|
|
3990
4024
|
var isVideoFile = (file) => file.mediaType != null && getTopLevelMediaType2(file.mediaType) === "video";
|
|
3991
4025
|
var isImageReference = (file) => file.mediaType == null || getTopLevelMediaType2(file.mediaType) === "image";
|
|
4026
|
+
var isAudioReference = (file) => file.mediaType != null && getTopLevelMediaType2(file.mediaType) === "audio";
|
|
3992
4027
|
function fileToXaiUrl(file) {
|
|
3993
4028
|
if (file.type === "url") {
|
|
3994
4029
|
return file.url;
|
|
@@ -4000,11 +4035,14 @@ function resolveReferences(options, xaiOptions, warnings) {
|
|
|
4000
4035
|
if (options.inputReferences != null && options.inputReferences.length > 0) {
|
|
4001
4036
|
const imageFiles = [];
|
|
4002
4037
|
for (const reference of options.inputReferences) {
|
|
4038
|
+
if (isAudioReference(reference)) {
|
|
4039
|
+
continue;
|
|
4040
|
+
}
|
|
4003
4041
|
if (!isImageReference(reference)) {
|
|
4004
4042
|
warnings.push({
|
|
4005
4043
|
type: "unsupported",
|
|
4006
4044
|
feature: "inputReferences",
|
|
4007
|
-
details:
|
|
4045
|
+
details: 'xAI reference-to-video does not accept video references. The video reference was ignored. Use providerOptions.xai.mode "extend-video" to continue from a video.'
|
|
4008
4046
|
});
|
|
4009
4047
|
continue;
|
|
4010
4048
|
}
|
|
@@ -4012,24 +4050,29 @@ function resolveReferences(options, xaiOptions, warnings) {
|
|
|
4012
4050
|
}
|
|
4013
4051
|
return imageFiles.length > 0 ? imageFiles.map((reference) => ({ url: fileToXaiUrl(reference) })) : void 0;
|
|
4014
4052
|
}
|
|
4015
|
-
if (
|
|
4053
|
+
if (xaiOptions?.referenceImageUrls != null && xaiOptions.referenceImageUrls.length > 0) {
|
|
4016
4054
|
return xaiOptions.referenceImageUrls.map((url) => ({ url }));
|
|
4017
4055
|
}
|
|
4018
4056
|
}
|
|
4057
|
+
function resolveReferenceAudios(options) {
|
|
4058
|
+
const audioReferences = options.inputReferences?.filter(isAudioReference);
|
|
4059
|
+
return audioReferences != null && audioReferences.length > 0 ? audioReferences.map((reference) => ({ url: fileToXaiUrl(reference) })) : void 0;
|
|
4060
|
+
}
|
|
4019
4061
|
function hasImageInputReference(options) {
|
|
4020
|
-
|
|
4021
|
-
|
|
4062
|
+
return options.inputReferences?.some(isImageReference) ?? false;
|
|
4063
|
+
}
|
|
4064
|
+
function hasAudioInputReference(options) {
|
|
4065
|
+
return options.inputReferences?.some(isAudioReference) ?? false;
|
|
4022
4066
|
}
|
|
4023
4067
|
function resolveVideoMode(options, xaiOptions) {
|
|
4024
|
-
if (
|
|
4068
|
+
if (xaiOptions?.mode != null) {
|
|
4025
4069
|
return xaiOptions.mode;
|
|
4026
4070
|
}
|
|
4027
|
-
if (
|
|
4071
|
+
if (xaiOptions?.videoUrl != null) {
|
|
4028
4072
|
return "edit-video";
|
|
4029
4073
|
}
|
|
4030
|
-
const
|
|
4031
|
-
|
|
4032
|
-
if (!hasFrameImages && (hasImageInputReference(options) || hasLegacyReferenceUrls)) {
|
|
4074
|
+
const hasLegacyReferenceUrls = xaiOptions?.referenceImageUrls != null && xaiOptions.referenceImageUrls.length > 0;
|
|
4075
|
+
if (hasImageInputReference(options) || hasAudioInputReference(options) || hasLegacyReferenceUrls) {
|
|
4033
4076
|
return "reference-to-video";
|
|
4034
4077
|
}
|
|
4035
4078
|
}
|
|
@@ -4089,7 +4132,7 @@ var XaiVideoModel = class {
|
|
|
4089
4132
|
details: "xAI video editing does not support custom aspect ratio."
|
|
4090
4133
|
});
|
|
4091
4134
|
}
|
|
4092
|
-
if (isEdit && (
|
|
4135
|
+
if (isEdit && (xaiOptions?.resolution != null || options.resolution != null)) {
|
|
4093
4136
|
warnings.push({
|
|
4094
4137
|
type: "unsupported",
|
|
4095
4138
|
feature: "resolution",
|
|
@@ -4103,7 +4146,7 @@ var XaiVideoModel = class {
|
|
|
4103
4146
|
details: "xAI video extension does not support custom aspect ratio."
|
|
4104
4147
|
});
|
|
4105
4148
|
}
|
|
4106
|
-
if (isExtension && (
|
|
4149
|
+
if (isExtension && (xaiOptions?.resolution != null || options.resolution != null)) {
|
|
4107
4150
|
warnings.push({
|
|
4108
4151
|
type: "unsupported",
|
|
4109
4152
|
feature: "resolution",
|
|
@@ -4123,7 +4166,7 @@ var XaiVideoModel = class {
|
|
|
4123
4166
|
if (allowAspectRatio && options.aspectRatio != null) {
|
|
4124
4167
|
body.aspect_ratio = options.aspectRatio;
|
|
4125
4168
|
}
|
|
4126
|
-
if (allowResolution &&
|
|
4169
|
+
if (allowResolution && xaiOptions?.resolution != null) {
|
|
4127
4170
|
body.resolution = xaiOptions.resolution;
|
|
4128
4171
|
} else if (allowResolution && options.resolution != null) {
|
|
4129
4172
|
const mapped = RESOLUTION_MAP[options.resolution];
|
|
@@ -4137,6 +4180,41 @@ var XaiVideoModel = class {
|
|
|
4137
4180
|
});
|
|
4138
4181
|
}
|
|
4139
4182
|
}
|
|
4183
|
+
if (options.generateAudio != null && !isEdit && !isExtension) {
|
|
4184
|
+
body.generate_audio = options.generateAudio;
|
|
4185
|
+
} else if (options.generateAudio != null) {
|
|
4186
|
+
warnings.push({
|
|
4187
|
+
type: "unsupported",
|
|
4188
|
+
feature: "generateAudio",
|
|
4189
|
+
details: `xAI ${isEdit ? "video editing" : "video extension"} does not support generateAudio.`
|
|
4190
|
+
});
|
|
4191
|
+
}
|
|
4192
|
+
if (xaiOptions?.storageOptions != null) {
|
|
4193
|
+
const { filename, expiresAfter, publicUrl } = xaiOptions.storageOptions;
|
|
4194
|
+
body.storage_options = {
|
|
4195
|
+
filename,
|
|
4196
|
+
...expiresAfter != null ? { expires_after: expiresAfter } : {},
|
|
4197
|
+
...publicUrl != null ? {
|
|
4198
|
+
public_url: typeof publicUrl === "boolean" ? publicUrl : {
|
|
4199
|
+
...publicUrl.expiresAfter != null ? { expires_after: publicUrl.expiresAfter } : {}
|
|
4200
|
+
}
|
|
4201
|
+
} : {}
|
|
4202
|
+
};
|
|
4203
|
+
}
|
|
4204
|
+
if (xaiOptions?.keyframes != null && xaiOptions.keyframes.length > 0) {
|
|
4205
|
+
if (this.modelId !== "grok-imagine-video-1.5" || isEdit || isExtension) {
|
|
4206
|
+
warnings.push({
|
|
4207
|
+
type: "unsupported",
|
|
4208
|
+
feature: "keyframes",
|
|
4209
|
+
details: this.modelId !== "grok-imagine-video-1.5" ? 'xAI only supports keyframes with "grok-imagine-video-1.5".' : `xAI ${isEdit ? "video editing" : "video extension"} does not support keyframes.`
|
|
4210
|
+
});
|
|
4211
|
+
} else {
|
|
4212
|
+
body.keyframes = xaiOptions.keyframes.map((keyframe) => ({
|
|
4213
|
+
image: { url: keyframe.imageUrl },
|
|
4214
|
+
timestamp_s: keyframe.timestampSeconds
|
|
4215
|
+
}));
|
|
4216
|
+
}
|
|
4217
|
+
}
|
|
4140
4218
|
if (isEdit) {
|
|
4141
4219
|
body.video = { url: xaiOptions.videoUrl };
|
|
4142
4220
|
}
|
|
@@ -4158,28 +4236,45 @@ var XaiVideoModel = class {
|
|
|
4158
4236
|
}
|
|
4159
4237
|
const lastFrameImage = getLastFrameImage(options);
|
|
4160
4238
|
if (lastFrameImage != null) {
|
|
4161
|
-
|
|
4162
|
-
|
|
4163
|
-
|
|
4164
|
-
|
|
4165
|
-
|
|
4239
|
+
if (this.modelId !== "grok-imagine-video-1.5" || isEdit || isExtension || isVideoFile(lastFrameImage)) {
|
|
4240
|
+
warnings.push({
|
|
4241
|
+
type: "unsupported",
|
|
4242
|
+
feature: "frameImages",
|
|
4243
|
+
details: this.modelId !== "grok-imagine-video-1.5" ? 'xAI only supports last_frame with "grok-imagine-video-1.5". The last frame was ignored.' : "xAI only accepts an image last_frame for video generation. The last frame was ignored."
|
|
4244
|
+
});
|
|
4245
|
+
} else {
|
|
4246
|
+
body.last_frame = { url: fileToXaiUrl(lastFrameImage) };
|
|
4247
|
+
}
|
|
4166
4248
|
}
|
|
4167
4249
|
if (hasReferenceImages) {
|
|
4168
4250
|
const referenceImages = resolveReferences(options, xaiOptions, warnings);
|
|
4251
|
+
const referenceAudios = resolveReferenceAudios(options);
|
|
4169
4252
|
if (referenceImages != null) {
|
|
4170
4253
|
body.reference_images = referenceImages;
|
|
4171
|
-
} else {
|
|
4254
|
+
} else if (referenceAudios == null) {
|
|
4172
4255
|
warnings.push({
|
|
4173
4256
|
type: "unsupported",
|
|
4174
4257
|
feature: "referenceImages",
|
|
4175
4258
|
details: "xAI reference-to-video requires at least one image reference. The video will be generated without reference images."
|
|
4176
4259
|
});
|
|
4177
4260
|
}
|
|
4178
|
-
const referenceVoiceIds = xaiOptions
|
|
4179
|
-
|
|
4180
|
-
|
|
4181
|
-
|
|
4182
|
-
|
|
4261
|
+
const referenceVoiceIds = xaiOptions?.referenceVoiceIds;
|
|
4262
|
+
const referenceVoices = referenceVoiceIds?.map((voiceId) => ({
|
|
4263
|
+
voice_id: voiceId
|
|
4264
|
+
}));
|
|
4265
|
+
const referenceAudioInputs = [
|
|
4266
|
+
...referenceAudios ?? [],
|
|
4267
|
+
...referenceVoices ?? []
|
|
4268
|
+
];
|
|
4269
|
+
if (referenceAudioInputs.length > 0) {
|
|
4270
|
+
if (referenceAudioInputs.length > 3) {
|
|
4271
|
+
warnings.push({
|
|
4272
|
+
type: "unsupported",
|
|
4273
|
+
feature: "inputReferences",
|
|
4274
|
+
details: "xAI reference-to-video supports at most 3 audio references. Only the first 3 were used."
|
|
4275
|
+
});
|
|
4276
|
+
}
|
|
4277
|
+
body.reference_audios = referenceAudioInputs.slice(0, 3);
|
|
4183
4278
|
}
|
|
4184
4279
|
if (body.resolution === "1080p") {
|
|
4185
4280
|
warnings.push({
|
|
@@ -4201,17 +4296,17 @@ var XaiVideoModel = class {
|
|
|
4201
4296
|
warnings.push({
|
|
4202
4297
|
type: "unsupported",
|
|
4203
4298
|
feature: "inputReferences",
|
|
4204
|
-
details: hasImageInputReference(options) ? "xAI only supports inputReferences for reference-to-video generation. The
|
|
4299
|
+
details: hasImageInputReference(options) || hasAudioInputReference(options) ? "xAI only supports inputReferences for reference-to-video generation. The references were ignored." : "xAI reference-to-video requires at least one image or audio reference. The references were ignored."
|
|
4205
4300
|
});
|
|
4206
4301
|
}
|
|
4207
|
-
if (
|
|
4302
|
+
if (xaiOptions?.referenceVoiceIds != null && xaiOptions.referenceVoiceIds.length > 0 && !hasReferenceImages) {
|
|
4208
4303
|
warnings.push({
|
|
4209
4304
|
type: "unsupported",
|
|
4210
4305
|
feature: "referenceVoiceIds",
|
|
4211
4306
|
details: "xAI only supports reference voices for reference-to-video generation. The reference voices were ignored."
|
|
4212
4307
|
});
|
|
4213
4308
|
}
|
|
4214
|
-
if (!isExtension &&
|
|
4309
|
+
if (!isExtension && xaiOptions?.user !== void 0) {
|
|
4215
4310
|
body.user = xaiOptions.user;
|
|
4216
4311
|
}
|
|
4217
4312
|
if (xaiOptions != null) {
|
|
@@ -4224,6 +4319,8 @@ var XaiVideoModel = class {
|
|
|
4224
4319
|
"videoUrl",
|
|
4225
4320
|
"referenceImageUrls",
|
|
4226
4321
|
"referenceVoiceIds",
|
|
4322
|
+
"keyframes",
|
|
4323
|
+
"storageOptions",
|
|
4227
4324
|
"user"
|
|
4228
4325
|
].includes(key)) {
|
|
4229
4326
|
body[key] = value;
|
|
@@ -4241,10 +4338,9 @@ var XaiVideoModel = class {
|
|
|
4241
4338
|
};
|
|
4242
4339
|
}
|
|
4243
4340
|
async doStart(options) {
|
|
4244
|
-
|
|
4245
|
-
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
4341
|
+
const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
|
|
4246
4342
|
const { body, warnings, isEdit, isExtension } = await this.buildRequestBody(options);
|
|
4247
|
-
const baseURL =
|
|
4343
|
+
const baseURL = this.config.baseURL ?? "https://api.x.ai/v1";
|
|
4248
4344
|
let endpoint;
|
|
4249
4345
|
if (isEdit) {
|
|
4250
4346
|
endpoint = `${baseURL}/videos/edits`;
|
|
@@ -4282,10 +4378,9 @@ var XaiVideoModel = class {
|
|
|
4282
4378
|
};
|
|
4283
4379
|
}
|
|
4284
4380
|
async doStatus(options) {
|
|
4285
|
-
|
|
4286
|
-
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
4381
|
+
const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
|
|
4287
4382
|
const { requestId } = options.operation;
|
|
4288
|
-
const baseURL =
|
|
4383
|
+
const baseURL = this.config.baseURL ?? "https://api.x.ai/v1";
|
|
4289
4384
|
const { value: statusResponse, responseHeaders } = await getFromApi4({
|
|
4290
4385
|
url: `${baseURL}/videos/${encodePathSegment2(requestId)}`,
|
|
4291
4386
|
validateUrl: false,
|
|
@@ -4307,7 +4402,7 @@ var XaiVideoModel = class {
|
|
|
4307
4402
|
};
|
|
4308
4403
|
}
|
|
4309
4404
|
if (statusResponse.status === "failed") {
|
|
4310
|
-
const errorDetails =
|
|
4405
|
+
const errorDetails = statusResponse.error?.message ?? statusResponse.error?.code;
|
|
4311
4406
|
return {
|
|
4312
4407
|
status: "error",
|
|
4313
4408
|
error: errorDetails != null ? `Video generation failed: ${errorDetails}` : "Video generation failed.",
|
|
@@ -4318,8 +4413,9 @@ var XaiVideoModel = class {
|
|
|
4318
4413
|
}
|
|
4319
4414
|
};
|
|
4320
4415
|
}
|
|
4321
|
-
if (statusResponse.status === "done" || statusResponse.status == null &&
|
|
4322
|
-
|
|
4416
|
+
if (statusResponse.status === "done" || statusResponse.status == null && statusResponse.video?.url) {
|
|
4417
|
+
const video = statusResponse.video;
|
|
4418
|
+
if (video?.respect_moderation === false) {
|
|
4323
4419
|
return {
|
|
4324
4420
|
status: "error",
|
|
4325
4421
|
error: "Video generation was blocked due to a content policy violation.",
|
|
@@ -4330,7 +4426,8 @@ var XaiVideoModel = class {
|
|
|
4330
4426
|
}
|
|
4331
4427
|
};
|
|
4332
4428
|
}
|
|
4333
|
-
|
|
4429
|
+
const videoUrl = video?.url ?? video?.file_output?.public_url ?? void 0;
|
|
4430
|
+
if (!videoUrl) {
|
|
4334
4431
|
return {
|
|
4335
4432
|
status: "error",
|
|
4336
4433
|
error: "Video generation completed but no video URL was returned.",
|
|
@@ -4343,13 +4440,7 @@ var XaiVideoModel = class {
|
|
|
4343
4440
|
}
|
|
4344
4441
|
return {
|
|
4345
4442
|
status: "completed",
|
|
4346
|
-
videos: [
|
|
4347
|
-
{
|
|
4348
|
-
type: "url",
|
|
4349
|
-
url: statusResponse.video.url,
|
|
4350
|
-
mediaType: "video/mp4"
|
|
4351
|
-
}
|
|
4352
|
-
],
|
|
4443
|
+
videos: [{ type: "url", url: videoUrl, mediaType: "video/mp4" }],
|
|
4353
4444
|
warnings: [],
|
|
4354
4445
|
response: {
|
|
4355
4446
|
timestamp: currentDate,
|
|
@@ -4359,10 +4450,29 @@ var XaiVideoModel = class {
|
|
|
4359
4450
|
providerMetadata: {
|
|
4360
4451
|
xai: {
|
|
4361
4452
|
requestId,
|
|
4362
|
-
videoUrl
|
|
4363
|
-
...
|
|
4364
|
-
...
|
|
4365
|
-
...statusResponse.progress != null ? { progress: statusResponse.progress } : {}
|
|
4453
|
+
videoUrl,
|
|
4454
|
+
...video?.duration != null ? { duration: video.duration } : {},
|
|
4455
|
+
...statusResponse.usage?.cost_in_usd_ticks != null ? { costInUsdTicks: statusResponse.usage.cost_in_usd_ticks } : {},
|
|
4456
|
+
...statusResponse.progress != null ? { progress: statusResponse.progress } : {},
|
|
4457
|
+
...video?.file_output != null ? {
|
|
4458
|
+
fileOutput: {
|
|
4459
|
+
fileId: video.file_output.file_id,
|
|
4460
|
+
filename: video.file_output.filename,
|
|
4461
|
+
...video.file_output.expires_at != null ? {
|
|
4462
|
+
expiresAt: video.file_output.expires_at
|
|
4463
|
+
} : {},
|
|
4464
|
+
...video.file_output.public_url != null ? {
|
|
4465
|
+
publicUrl: video.file_output.public_url
|
|
4466
|
+
} : {},
|
|
4467
|
+
...video.file_output.public_url_error != null ? {
|
|
4468
|
+
publicUrlError: video.file_output.public_url_error
|
|
4469
|
+
} : {},
|
|
4470
|
+
...video.file_output.public_url_expires_at != null ? {
|
|
4471
|
+
publicUrlExpiresAt: video.file_output.public_url_expires_at
|
|
4472
|
+
} : {}
|
|
4473
|
+
}
|
|
4474
|
+
} : {},
|
|
4475
|
+
...video?.storage_error != null ? { storageError: video.storage_error } : {}
|
|
4366
4476
|
}
|
|
4367
4477
|
}
|
|
4368
4478
|
};
|
|
@@ -4383,9 +4493,18 @@ var xaiCreateVideoResponseSchema = z19.object({
|
|
|
4383
4493
|
var xaiVideoStatusResponseSchema = z19.object({
|
|
4384
4494
|
status: z19.string().nullish(),
|
|
4385
4495
|
video: z19.object({
|
|
4386
|
-
url: z19.string(),
|
|
4496
|
+
url: z19.string().nullish(),
|
|
4387
4497
|
duration: z19.number().nullish(),
|
|
4388
|
-
respect_moderation: z19.boolean().nullish()
|
|
4498
|
+
respect_moderation: z19.boolean().nullish(),
|
|
4499
|
+
file_output: z19.object({
|
|
4500
|
+
file_id: z19.string(),
|
|
4501
|
+
filename: z19.string(),
|
|
4502
|
+
expires_at: z19.number().nullish(),
|
|
4503
|
+
public_url: z19.string().nullish(),
|
|
4504
|
+
public_url_error: z19.string().nullish(),
|
|
4505
|
+
public_url_expires_at: z19.number().nullish()
|
|
4506
|
+
}).nullish(),
|
|
4507
|
+
storage_error: z19.string().nullish()
|
|
4389
4508
|
}).nullish(),
|
|
4390
4509
|
model: z19.string().nullish(),
|
|
4391
4510
|
usage: z19.object({
|
|
@@ -4582,10 +4701,10 @@ var XaiSpeechModel = class _XaiSpeechModel {
|
|
|
4582
4701
|
const output_format = {
|
|
4583
4702
|
codec
|
|
4584
4703
|
};
|
|
4585
|
-
if (
|
|
4704
|
+
if (xaiOptions?.sampleRate != null) {
|
|
4586
4705
|
output_format.sample_rate = xaiOptions.sampleRate;
|
|
4587
4706
|
}
|
|
4588
|
-
if (
|
|
4707
|
+
if (xaiOptions?.bitRate != null) {
|
|
4589
4708
|
if (codec === "mp3") {
|
|
4590
4709
|
output_format.bit_rate = xaiOptions.bitRate;
|
|
4591
4710
|
} else {
|
|
@@ -4602,20 +4721,19 @@ var XaiSpeechModel = class _XaiSpeechModel {
|
|
|
4602
4721
|
language,
|
|
4603
4722
|
output_format,
|
|
4604
4723
|
speed,
|
|
4605
|
-
optimize_streaming_latency: xaiOptions
|
|
4606
|
-
text_normalization: xaiOptions
|
|
4607
|
-
with_timestamps: xaiOptions
|
|
4608
|
-
replace: xaiOptions
|
|
4724
|
+
optimize_streaming_latency: xaiOptions?.optimizeStreamingLatency,
|
|
4725
|
+
text_normalization: xaiOptions?.textNormalization,
|
|
4726
|
+
with_timestamps: xaiOptions?.withTimestamps,
|
|
4727
|
+
replace: xaiOptions?.replace
|
|
4609
4728
|
};
|
|
4610
4729
|
return {
|
|
4611
4730
|
requestBody,
|
|
4612
4731
|
warnings,
|
|
4613
|
-
withTimestamps:
|
|
4732
|
+
withTimestamps: xaiOptions?.withTimestamps === true
|
|
4614
4733
|
};
|
|
4615
4734
|
}
|
|
4616
4735
|
async doGenerate(options) {
|
|
4617
|
-
|
|
4618
|
-
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
4736
|
+
const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
|
|
4619
4737
|
const { requestBody, warnings, withTimestamps } = await this.getArgs(options);
|
|
4620
4738
|
const { value, responseHeaders, rawValue } = await postJsonToApi5({
|
|
4621
4739
|
url: `${this.config.baseURL}/tts`,
|
|
@@ -4637,7 +4755,7 @@ var XaiSpeechModel = class _XaiSpeechModel {
|
|
|
4637
4755
|
envelope = value;
|
|
4638
4756
|
audio = envelope.audio != null ? convertBase64ToUint8Array2(envelope.audio) : new Uint8Array(0);
|
|
4639
4757
|
}
|
|
4640
|
-
const traceId = responseHeaders
|
|
4758
|
+
const traceId = responseHeaders?.["x-trace-id"];
|
|
4641
4759
|
return {
|
|
4642
4760
|
audio,
|
|
4643
4761
|
warnings,
|
|
@@ -4653,9 +4771,9 @@ var XaiSpeechModel = class _XaiSpeechModel {
|
|
|
4653
4771
|
providerMetadata: {
|
|
4654
4772
|
xai: {
|
|
4655
4773
|
...traceId != null ? { traceId } : {},
|
|
4656
|
-
...
|
|
4657
|
-
...
|
|
4658
|
-
...
|
|
4774
|
+
...envelope?.duration != null ? { duration: envelope.duration } : {},
|
|
4775
|
+
...envelope?.content_type != null ? { contentType: envelope.content_type } : {},
|
|
4776
|
+
...envelope?.audio_timestamps != null ? {
|
|
4659
4777
|
audioTimestamps: {
|
|
4660
4778
|
graphChars: envelope.audio_timestamps.graph_chars,
|
|
4661
4779
|
graphTimes: envelope.audio_timestamps.graph_times
|
|
@@ -4806,21 +4924,21 @@ var XaiTranscriptionModel = class _XaiTranscriptionModel {
|
|
|
4806
4924
|
});
|
|
4807
4925
|
const formData = new FormData();
|
|
4808
4926
|
const transcriptionOptions = {
|
|
4809
|
-
audio_format: xaiOptions
|
|
4810
|
-
sample_rate: xaiOptions
|
|
4811
|
-
language: xaiOptions
|
|
4812
|
-
format: xaiOptions
|
|
4813
|
-
multichannel: xaiOptions
|
|
4814
|
-
channels: xaiOptions
|
|
4815
|
-
diarize: xaiOptions
|
|
4816
|
-
filler_words: xaiOptions
|
|
4927
|
+
audio_format: xaiOptions?.audioFormat,
|
|
4928
|
+
sample_rate: xaiOptions?.sampleRate,
|
|
4929
|
+
language: xaiOptions?.language,
|
|
4930
|
+
format: xaiOptions?.format,
|
|
4931
|
+
multichannel: xaiOptions?.multichannel,
|
|
4932
|
+
channels: xaiOptions?.channels,
|
|
4933
|
+
diarize: xaiOptions?.diarize,
|
|
4934
|
+
filler_words: xaiOptions?.fillerWords
|
|
4817
4935
|
};
|
|
4818
4936
|
for (const [key, value] of Object.entries(transcriptionOptions)) {
|
|
4819
4937
|
if (value != null) {
|
|
4820
4938
|
formData.append(key, String(value));
|
|
4821
4939
|
}
|
|
4822
4940
|
}
|
|
4823
|
-
if (
|
|
4941
|
+
if (xaiOptions?.keyterm != null) {
|
|
4824
4942
|
const keyterms = Array.isArray(xaiOptions.keyterm) ? xaiOptions.keyterm : [xaiOptions.keyterm];
|
|
4825
4943
|
for (const keyterm of keyterms) {
|
|
4826
4944
|
formData.append("keyterm", keyterm);
|
|
@@ -4836,16 +4954,15 @@ var XaiTranscriptionModel = class _XaiTranscriptionModel {
|
|
|
4836
4954
|
return { formData, warnings };
|
|
4837
4955
|
}
|
|
4838
4956
|
async doGenerate(options) {
|
|
4839
|
-
|
|
4840
|
-
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
4957
|
+
const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
|
|
4841
4958
|
const { formData, warnings } = await this.getArgs(options);
|
|
4842
4959
|
const {
|
|
4843
4960
|
value: response,
|
|
4844
4961
|
responseHeaders,
|
|
4845
4962
|
rawValue: rawResponse
|
|
4846
4963
|
} = await postFormDataToApi3({
|
|
4847
|
-
url: `${
|
|
4848
|
-
headers: combineHeaders7(
|
|
4964
|
+
url: `${this.config.baseURL ?? "https://api.x.ai/v1"}/stt`,
|
|
4965
|
+
headers: combineHeaders7(this.config.headers?.(), options.headers),
|
|
4849
4966
|
formData,
|
|
4850
4967
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
4851
4968
|
successfulResponseHandler: createJsonResponseHandler7(
|
|
@@ -4856,13 +4973,13 @@ var XaiTranscriptionModel = class _XaiTranscriptionModel {
|
|
|
4856
4973
|
});
|
|
4857
4974
|
return {
|
|
4858
4975
|
text: response.text,
|
|
4859
|
-
segments:
|
|
4976
|
+
segments: response.words?.map((word) => ({
|
|
4860
4977
|
text: word.text,
|
|
4861
4978
|
startSecond: word.start,
|
|
4862
4979
|
endSecond: word.end
|
|
4863
|
-
}))
|
|
4980
|
+
})) ?? [],
|
|
4864
4981
|
language: response.language || void 0,
|
|
4865
|
-
durationInSeconds:
|
|
4982
|
+
durationInSeconds: response.duration ?? void 0,
|
|
4866
4983
|
warnings,
|
|
4867
4984
|
response: {
|
|
4868
4985
|
timestamp: currentDate,
|
|
@@ -4873,39 +4990,38 @@ var XaiTranscriptionModel = class _XaiTranscriptionModel {
|
|
|
4873
4990
|
};
|
|
4874
4991
|
}
|
|
4875
4992
|
async doStream(options) {
|
|
4876
|
-
|
|
4877
|
-
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
4993
|
+
const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
|
|
4878
4994
|
const warnings = [];
|
|
4879
4995
|
const xaiOptions = await parseProviderOptions8({
|
|
4880
4996
|
provider: "xai",
|
|
4881
4997
|
providerOptions: options.providerOptions,
|
|
4882
4998
|
schema: xaiTranscriptionModelOptionsSchema
|
|
4883
4999
|
});
|
|
4884
|
-
if (
|
|
5000
|
+
if (xaiOptions?.multichannel === true && xaiOptions.channels == null) {
|
|
4885
5001
|
throw new InvalidArgumentError3({
|
|
4886
5002
|
argument: "providerOptions",
|
|
4887
5003
|
message: "providerOptions.xai.channels is required when providerOptions.xai.multichannel is true"
|
|
4888
5004
|
});
|
|
4889
5005
|
}
|
|
4890
|
-
if (
|
|
5006
|
+
if (xaiOptions?.format != null) {
|
|
4891
5007
|
warnings.push({
|
|
4892
5008
|
type: "unsupported",
|
|
4893
5009
|
feature: "providerOptions.xai.format",
|
|
4894
5010
|
details: "xAI streaming transcription does not support format."
|
|
4895
5011
|
});
|
|
4896
5012
|
}
|
|
4897
|
-
if (
|
|
5013
|
+
if (xaiOptions?.audioFormat == null && !isKnownInputAudioFormat(options.inputAudioFormat.type)) {
|
|
4898
5014
|
warnings.push({
|
|
4899
5015
|
type: "other",
|
|
4900
5016
|
message: `Unrecognized inputAudioFormat.type "${options.inputAudioFormat.type}"; falling back to raw PCM encoding. Use audio/pcm, audio/pcmu, or audio/pcma, or set providerOptions.xai.audioFormat explicitly.`
|
|
4901
5017
|
});
|
|
4902
5018
|
}
|
|
4903
5019
|
const url = buildXaiStreamingTranscriptionUrl({
|
|
4904
|
-
baseURL:
|
|
5020
|
+
baseURL: this.config.baseURL ?? "https://api.x.ai/v1",
|
|
4905
5021
|
inputAudioFormat: options.inputAudioFormat,
|
|
4906
5022
|
providerOptions: xaiOptions
|
|
4907
5023
|
});
|
|
4908
|
-
const headers = combineHeaders7(
|
|
5024
|
+
const headers = combineHeaders7(this.config.headers?.(), options.headers);
|
|
4909
5025
|
return {
|
|
4910
5026
|
request: { body: url.toString() },
|
|
4911
5027
|
response: {
|
|
@@ -4917,8 +5033,8 @@ var XaiTranscriptionModel = class _XaiTranscriptionModel {
|
|
|
4917
5033
|
url,
|
|
4918
5034
|
headers,
|
|
4919
5035
|
warnings,
|
|
4920
|
-
language:
|
|
4921
|
-
expectedDoneCount:
|
|
5036
|
+
language: xaiOptions?.language ?? void 0,
|
|
5037
|
+
expectedDoneCount: xaiOptions?.multichannel === true ? xaiOptions.channels : 1,
|
|
4922
5038
|
audio: options.audio,
|
|
4923
5039
|
abortSignal: options.abortSignal,
|
|
4924
5040
|
includeRawChunks: options.includeRawChunks
|
|
@@ -4956,7 +5072,7 @@ function createXaiStreamingTranscriptionStream({
|
|
|
4956
5072
|
void audio.cancel().catch(() => {
|
|
4957
5073
|
});
|
|
4958
5074
|
}
|
|
4959
|
-
connection
|
|
5075
|
+
connection?.close(closeCode);
|
|
4960
5076
|
};
|
|
4961
5077
|
const finishWithError = (error) => {
|
|
4962
5078
|
if (finished) return;
|
|
@@ -5005,7 +5121,6 @@ function createXaiStreamingTranscriptionStream({
|
|
|
5005
5121
|
onAbort: finishWithError,
|
|
5006
5122
|
onProcessingError: finishWithError,
|
|
5007
5123
|
onMessageText: async (text) => {
|
|
5008
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
5009
5124
|
const parsed = await safeParseJSON2({ text });
|
|
5010
5125
|
if (!parsed.success) return;
|
|
5011
5126
|
const raw = parsed.value;
|
|
@@ -5015,7 +5130,7 @@ function createXaiStreamingTranscriptionStream({
|
|
|
5015
5130
|
switch (raw.type) {
|
|
5016
5131
|
case "transcript.created": {
|
|
5017
5132
|
controller.enqueue({ type: "stream-start", warnings });
|
|
5018
|
-
const socket = connection
|
|
5133
|
+
const socket = connection?.socket;
|
|
5019
5134
|
if (socket == null) {
|
|
5020
5135
|
finishWithError(new Error("WebSocket is not connected."));
|
|
5021
5136
|
break;
|
|
@@ -5025,12 +5140,12 @@ function createXaiStreamingTranscriptionStream({
|
|
|
5025
5140
|
}
|
|
5026
5141
|
case "transcript.partial": {
|
|
5027
5142
|
const id = channelId(raw.channel_index);
|
|
5028
|
-
const channelIndex =
|
|
5143
|
+
const channelIndex = raw.channel_index ?? 0;
|
|
5029
5144
|
if (raw.is_final && raw.speech_final) {
|
|
5030
5145
|
const timing = timingFromXaiEvent(raw);
|
|
5031
5146
|
if (raw.text) {
|
|
5032
5147
|
finalizedTexts.set(channelIndex, [
|
|
5033
|
-
...
|
|
5148
|
+
...finalizedTexts.get(channelIndex) ?? [],
|
|
5034
5149
|
raw.text
|
|
5035
5150
|
]);
|
|
5036
5151
|
}
|
|
@@ -5038,16 +5153,16 @@ function createXaiStreamingTranscriptionStream({
|
|
|
5038
5153
|
controller.enqueue({
|
|
5039
5154
|
type: "transcript-final",
|
|
5040
5155
|
id,
|
|
5041
|
-
text:
|
|
5156
|
+
text: raw.text ?? "",
|
|
5042
5157
|
...timing,
|
|
5043
5158
|
channelIndex: raw.channel_index
|
|
5044
5159
|
});
|
|
5045
5160
|
} else {
|
|
5046
|
-
pendingTexts.set(channelIndex,
|
|
5161
|
+
pendingTexts.set(channelIndex, raw.text ?? "");
|
|
5047
5162
|
controller.enqueue({
|
|
5048
5163
|
type: "transcript-partial",
|
|
5049
5164
|
id,
|
|
5050
|
-
text:
|
|
5165
|
+
text: raw.text ?? "",
|
|
5051
5166
|
startSecond: raw.start,
|
|
5052
5167
|
durationInSeconds: raw.duration,
|
|
5053
5168
|
channelIndex: raw.channel_index
|
|
@@ -5056,18 +5171,18 @@ function createXaiStreamingTranscriptionStream({
|
|
|
5056
5171
|
break;
|
|
5057
5172
|
}
|
|
5058
5173
|
case "transcript.done": {
|
|
5059
|
-
const channelIndex =
|
|
5174
|
+
const channelIndex = raw.channel_index ?? 0;
|
|
5060
5175
|
const accumulated = [
|
|
5061
|
-
...
|
|
5176
|
+
...finalizedTexts.get(channelIndex) ?? [],
|
|
5062
5177
|
...pendingTexts.get(channelIndex) ? [pendingTexts.get(channelIndex)] : []
|
|
5063
5178
|
].join(" ");
|
|
5064
5179
|
doneTexts.set(channelIndex, raw.text || accumulated);
|
|
5065
|
-
doneDuration =
|
|
5180
|
+
doneDuration = raw.duration ?? doneDuration;
|
|
5066
5181
|
maybeFinish();
|
|
5067
5182
|
break;
|
|
5068
5183
|
}
|
|
5069
5184
|
case "error": {
|
|
5070
|
-
finishWithError(new Error(
|
|
5185
|
+
finishWithError(new Error(raw.message ?? "xAI STT error"));
|
|
5071
5186
|
break;
|
|
5072
5187
|
}
|
|
5073
5188
|
}
|
|
@@ -5099,40 +5214,39 @@ function buildXaiStreamingTranscriptionUrl({
|
|
|
5099
5214
|
inputAudioFormat,
|
|
5100
5215
|
providerOptions
|
|
5101
5216
|
}) {
|
|
5102
|
-
var _a, _b, _c, _d, _e, _f;
|
|
5103
5217
|
const url = toWebSocketUrl(`${baseURL}/stt`);
|
|
5104
5218
|
appendSearchParam(
|
|
5105
5219
|
url,
|
|
5106
5220
|
"sample_rate",
|
|
5107
|
-
|
|
5221
|
+
providerOptions?.sampleRate ?? inputAudioFormat.rate
|
|
5108
5222
|
);
|
|
5109
5223
|
appendSearchParam(
|
|
5110
5224
|
url,
|
|
5111
5225
|
"encoding",
|
|
5112
|
-
|
|
5226
|
+
providerOptions?.audioFormat ?? encodingFromInputAudioFormat(inputAudioFormat.type)
|
|
5113
5227
|
);
|
|
5114
|
-
appendSearchParam(url, "language", providerOptions
|
|
5115
|
-
appendSearchParam(url, "diarize", providerOptions
|
|
5116
|
-
appendSearchParam(url, "filler_words", providerOptions
|
|
5117
|
-
appendSearchParam(url, "multichannel", providerOptions
|
|
5118
|
-
appendSearchParam(url, "channels", providerOptions
|
|
5228
|
+
appendSearchParam(url, "language", providerOptions?.language);
|
|
5229
|
+
appendSearchParam(url, "diarize", providerOptions?.diarize);
|
|
5230
|
+
appendSearchParam(url, "filler_words", providerOptions?.fillerWords);
|
|
5231
|
+
appendSearchParam(url, "multichannel", providerOptions?.multichannel);
|
|
5232
|
+
appendSearchParam(url, "channels", providerOptions?.channels);
|
|
5119
5233
|
appendSearchParam(
|
|
5120
5234
|
url,
|
|
5121
5235
|
"interim_results",
|
|
5122
|
-
|
|
5236
|
+
providerOptions?.streaming?.interimResults
|
|
5123
5237
|
);
|
|
5124
5238
|
appendSearchParam(
|
|
5125
5239
|
url,
|
|
5126
5240
|
"endpointing",
|
|
5127
|
-
|
|
5241
|
+
providerOptions?.streaming?.endpointing
|
|
5128
5242
|
);
|
|
5129
|
-
appendSearchParam(url, "smart_turn",
|
|
5243
|
+
appendSearchParam(url, "smart_turn", providerOptions?.streaming?.smartTurn);
|
|
5130
5244
|
appendSearchParam(
|
|
5131
5245
|
url,
|
|
5132
5246
|
"smart_turn_timeout",
|
|
5133
|
-
|
|
5247
|
+
providerOptions?.streaming?.smartTurnTimeout
|
|
5134
5248
|
);
|
|
5135
|
-
if (
|
|
5249
|
+
if (providerOptions?.keyterm != null) {
|
|
5136
5250
|
const keyterms = Array.isArray(providerOptions.keyterm) ? providerOptions.keyterm : [providerOptions.keyterm];
|
|
5137
5251
|
for (const keyterm of keyterms) {
|
|
5138
5252
|
url.searchParams.append("keyterm", keyterm);
|
|
@@ -5182,9 +5296,8 @@ var xaiTranscriptionResponseSchema = z23.object({
|
|
|
5182
5296
|
|
|
5183
5297
|
// src/xai-provider.ts
|
|
5184
5298
|
function createXai(options = {}) {
|
|
5185
|
-
var _a;
|
|
5186
5299
|
const baseURL = withoutTrailingSlash(
|
|
5187
|
-
|
|
5300
|
+
options.baseURL ?? "https://api.x.ai/v1"
|
|
5188
5301
|
);
|
|
5189
5302
|
const getHeaders = () => withUserAgentSuffix(
|
|
5190
5303
|
{
|
|
@@ -5225,7 +5338,7 @@ function createXai(options = {}) {
|
|
|
5225
5338
|
const createRealtimeModel = (modelId) => {
|
|
5226
5339
|
return new XaiRealtimeModel(modelId, {
|
|
5227
5340
|
provider: "xai.realtime",
|
|
5228
|
-
baseURL: baseURL
|
|
5341
|
+
baseURL: baseURL ?? "https://api.x.ai/v1",
|
|
5229
5342
|
headers: getHeaders,
|
|
5230
5343
|
fetch: options.fetch
|
|
5231
5344
|
});
|