@ai-sdk/xai 4.0.0-beta.4 → 4.0.0-beta.41
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 +319 -8
- package/README.md +2 -0
- package/dist/index.d.ts +126 -50
- package/dist/index.js +1169 -749
- package/dist/index.js.map +1 -1
- package/docs/01-xai.mdx +173 -381
- package/package.json +8 -10
- package/src/convert-to-xai-chat-messages.ts +22 -6
- package/src/convert-xai-chat-usage.ts +2 -2
- package/src/files/xai-files-api.ts +16 -0
- package/src/files/xai-files-options.ts +15 -0
- package/src/files/xai-files.ts +93 -0
- package/src/index.ts +1 -0
- package/src/map-xai-finish-reason.ts +2 -2
- package/src/responses/convert-to-xai-responses-input.ts +63 -8
- package/src/responses/convert-xai-responses-usage.ts +2 -2
- 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-language-model.ts +134 -48
- package/src/responses/xai-responses-options.ts +6 -0
- package/src/responses/xai-responses-prepare-tools.ts +6 -6
- package/src/xai-chat-language-model.ts +61 -25
- package/src/xai-chat-options.ts +3 -6
- package/src/xai-chat-prompt.ts +2 -1
- package/src/xai-image-model.ts +25 -8
- package/src/xai-image-settings.ts +0 -2
- package/src/xai-prepare-tools.ts +6 -6
- package/src/xai-provider.ts +34 -21
- package/src/xai-video-model.ts +127 -20
- package/src/xai-video-options.ts +136 -14
- package/dist/index.d.mts +0 -375
- package/dist/index.mjs +0 -3061
- package/dist/index.mjs.map +0 -1
package/dist/index.mjs
DELETED
|
@@ -1,3061 +0,0 @@
|
|
|
1
|
-
// src/xai-provider.ts
|
|
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";
|
|
11
|
-
|
|
12
|
-
// src/xai-chat-language-model.ts
|
|
13
|
-
import {
|
|
14
|
-
APICallError
|
|
15
|
-
} from "@ai-sdk/provider";
|
|
16
|
-
import {
|
|
17
|
-
combineHeaders,
|
|
18
|
-
createEventSourceResponseHandler,
|
|
19
|
-
createJsonResponseHandler,
|
|
20
|
-
extractResponseHeaders,
|
|
21
|
-
parseProviderOptions,
|
|
22
|
-
postJsonToApi,
|
|
23
|
-
safeParseJSON
|
|
24
|
-
} from "@ai-sdk/provider-utils";
|
|
25
|
-
import { z as z3 } from "zod/v4";
|
|
26
|
-
|
|
27
|
-
// src/convert-to-xai-chat-messages.ts
|
|
28
|
-
import {
|
|
29
|
-
UnsupportedFunctionalityError
|
|
30
|
-
} from "@ai-sdk/provider";
|
|
31
|
-
import { convertToBase64 } from "@ai-sdk/provider-utils";
|
|
32
|
-
function convertToXaiChatMessages(prompt) {
|
|
33
|
-
var _a;
|
|
34
|
-
const messages = [];
|
|
35
|
-
const warnings = [];
|
|
36
|
-
for (const { role, content } of prompt) {
|
|
37
|
-
switch (role) {
|
|
38
|
-
case "system": {
|
|
39
|
-
messages.push({ role: "system", content });
|
|
40
|
-
break;
|
|
41
|
-
}
|
|
42
|
-
case "user": {
|
|
43
|
-
if (content.length === 1 && content[0].type === "text") {
|
|
44
|
-
messages.push({ role: "user", content: content[0].text });
|
|
45
|
-
break;
|
|
46
|
-
}
|
|
47
|
-
messages.push({
|
|
48
|
-
role: "user",
|
|
49
|
-
content: content.map((part) => {
|
|
50
|
-
switch (part.type) {
|
|
51
|
-
case "text": {
|
|
52
|
-
return { type: "text", text: part.text };
|
|
53
|
-
}
|
|
54
|
-
case "file": {
|
|
55
|
-
if (part.mediaType.startsWith("image/")) {
|
|
56
|
-
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
|
57
|
-
return {
|
|
58
|
-
type: "image_url",
|
|
59
|
-
image_url: {
|
|
60
|
-
url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
} else {
|
|
64
|
-
throw new UnsupportedFunctionalityError({
|
|
65
|
-
functionality: `file part media type ${part.mediaType}`
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
})
|
|
71
|
-
});
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
case "assistant": {
|
|
75
|
-
let text = "";
|
|
76
|
-
const toolCalls = [];
|
|
77
|
-
for (const part of content) {
|
|
78
|
-
switch (part.type) {
|
|
79
|
-
case "text": {
|
|
80
|
-
text += part.text;
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
case "tool-call": {
|
|
84
|
-
toolCalls.push({
|
|
85
|
-
id: part.toolCallId,
|
|
86
|
-
type: "function",
|
|
87
|
-
function: {
|
|
88
|
-
name: part.toolName,
|
|
89
|
-
arguments: JSON.stringify(part.input)
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
break;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
messages.push({
|
|
97
|
-
role: "assistant",
|
|
98
|
-
content: text,
|
|
99
|
-
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
100
|
-
});
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
case "tool": {
|
|
104
|
-
for (const toolResponse of content) {
|
|
105
|
-
if (toolResponse.type === "tool-approval-response") {
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
const output = toolResponse.output;
|
|
109
|
-
let contentValue;
|
|
110
|
-
switch (output.type) {
|
|
111
|
-
case "text":
|
|
112
|
-
case "error-text":
|
|
113
|
-
contentValue = output.value;
|
|
114
|
-
break;
|
|
115
|
-
case "execution-denied":
|
|
116
|
-
contentValue = (_a = output.reason) != null ? _a : "Tool execution denied.";
|
|
117
|
-
break;
|
|
118
|
-
case "content":
|
|
119
|
-
case "json":
|
|
120
|
-
case "error-json":
|
|
121
|
-
contentValue = JSON.stringify(output.value);
|
|
122
|
-
break;
|
|
123
|
-
}
|
|
124
|
-
messages.push({
|
|
125
|
-
role: "tool",
|
|
126
|
-
tool_call_id: toolResponse.toolCallId,
|
|
127
|
-
content: contentValue
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
break;
|
|
131
|
-
}
|
|
132
|
-
default: {
|
|
133
|
-
const _exhaustiveCheck = role;
|
|
134
|
-
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return { messages, warnings };
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// src/convert-xai-chat-usage.ts
|
|
142
|
-
function convertXaiChatUsage(usage) {
|
|
143
|
-
var _a, _b, _c, _d;
|
|
144
|
-
const cacheReadTokens = (_b = (_a = usage.prompt_tokens_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0;
|
|
145
|
-
const reasoningTokens = (_d = (_c = usage.completion_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0;
|
|
146
|
-
const promptTokensIncludesCached = cacheReadTokens <= usage.prompt_tokens;
|
|
147
|
-
return {
|
|
148
|
-
inputTokens: {
|
|
149
|
-
total: promptTokensIncludesCached ? usage.prompt_tokens : usage.prompt_tokens + cacheReadTokens,
|
|
150
|
-
noCache: promptTokensIncludesCached ? usage.prompt_tokens - cacheReadTokens : usage.prompt_tokens,
|
|
151
|
-
cacheRead: cacheReadTokens,
|
|
152
|
-
cacheWrite: void 0
|
|
153
|
-
},
|
|
154
|
-
outputTokens: {
|
|
155
|
-
total: usage.completion_tokens + reasoningTokens,
|
|
156
|
-
text: usage.completion_tokens,
|
|
157
|
-
reasoning: reasoningTokens
|
|
158
|
-
},
|
|
159
|
-
raw: usage
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// src/get-response-metadata.ts
|
|
164
|
-
function getResponseMetadata({
|
|
165
|
-
id,
|
|
166
|
-
model,
|
|
167
|
-
created,
|
|
168
|
-
created_at
|
|
169
|
-
}) {
|
|
170
|
-
const unixTime = created != null ? created : created_at;
|
|
171
|
-
return {
|
|
172
|
-
id: id != null ? id : void 0,
|
|
173
|
-
modelId: model != null ? model : void 0,
|
|
174
|
-
timestamp: unixTime != null ? new Date(unixTime * 1e3) : void 0
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// src/map-xai-finish-reason.ts
|
|
179
|
-
function mapXaiFinishReason(finishReason) {
|
|
180
|
-
switch (finishReason) {
|
|
181
|
-
case "stop":
|
|
182
|
-
return "stop";
|
|
183
|
-
case "length":
|
|
184
|
-
return "length";
|
|
185
|
-
case "tool_calls":
|
|
186
|
-
case "function_call":
|
|
187
|
-
return "tool-calls";
|
|
188
|
-
case "content_filter":
|
|
189
|
-
return "content-filter";
|
|
190
|
-
default:
|
|
191
|
-
return "other";
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// src/xai-chat-options.ts
|
|
196
|
-
import { z } from "zod/v4";
|
|
197
|
-
var webSourceSchema = z.object({
|
|
198
|
-
type: z.literal("web"),
|
|
199
|
-
country: z.string().length(2).optional(),
|
|
200
|
-
excludedWebsites: z.array(z.string()).max(5).optional(),
|
|
201
|
-
allowedWebsites: z.array(z.string()).max(5).optional(),
|
|
202
|
-
safeSearch: z.boolean().optional()
|
|
203
|
-
});
|
|
204
|
-
var xSourceSchema = z.object({
|
|
205
|
-
type: z.literal("x"),
|
|
206
|
-
excludedXHandles: z.array(z.string()).optional(),
|
|
207
|
-
includedXHandles: z.array(z.string()).optional(),
|
|
208
|
-
postFavoriteCount: z.number().int().optional(),
|
|
209
|
-
postViewCount: z.number().int().optional(),
|
|
210
|
-
/**
|
|
211
|
-
* @deprecated use `includedXHandles` instead
|
|
212
|
-
*/
|
|
213
|
-
xHandles: z.array(z.string()).optional()
|
|
214
|
-
});
|
|
215
|
-
var newsSourceSchema = z.object({
|
|
216
|
-
type: z.literal("news"),
|
|
217
|
-
country: z.string().length(2).optional(),
|
|
218
|
-
excludedWebsites: z.array(z.string()).max(5).optional(),
|
|
219
|
-
safeSearch: z.boolean().optional()
|
|
220
|
-
});
|
|
221
|
-
var rssSourceSchema = z.object({
|
|
222
|
-
type: z.literal("rss"),
|
|
223
|
-
links: z.array(z.string().url()).max(1)
|
|
224
|
-
// currently only supports one RSS link
|
|
225
|
-
});
|
|
226
|
-
var searchSourceSchema = z.discriminatedUnion("type", [
|
|
227
|
-
webSourceSchema,
|
|
228
|
-
xSourceSchema,
|
|
229
|
-
newsSourceSchema,
|
|
230
|
-
rssSourceSchema
|
|
231
|
-
]);
|
|
232
|
-
var xaiLanguageModelChatOptions = z.object({
|
|
233
|
-
reasoningEffort: z.enum(["low", "high"]).optional(),
|
|
234
|
-
logprobs: z.boolean().optional(),
|
|
235
|
-
topLogprobs: z.number().int().min(0).max(8).optional(),
|
|
236
|
-
/**
|
|
237
|
-
* Whether to enable parallel function calling during tool use.
|
|
238
|
-
* When true, the model can call multiple functions in parallel.
|
|
239
|
-
* When false, the model will call functions sequentially.
|
|
240
|
-
* Defaults to true.
|
|
241
|
-
*/
|
|
242
|
-
parallel_function_calling: z.boolean().optional(),
|
|
243
|
-
searchParameters: z.object({
|
|
244
|
-
/**
|
|
245
|
-
* search mode preference
|
|
246
|
-
* - "off": disables search completely
|
|
247
|
-
* - "auto": model decides whether to search (default)
|
|
248
|
-
* - "on": always enables search
|
|
249
|
-
*/
|
|
250
|
-
mode: z.enum(["off", "auto", "on"]),
|
|
251
|
-
/**
|
|
252
|
-
* whether to return citations in the response
|
|
253
|
-
* defaults to true
|
|
254
|
-
*/
|
|
255
|
-
returnCitations: z.boolean().optional(),
|
|
256
|
-
/**
|
|
257
|
-
* start date for search data (ISO8601 format: YYYY-MM-DD)
|
|
258
|
-
*/
|
|
259
|
-
fromDate: z.string().optional(),
|
|
260
|
-
/**
|
|
261
|
-
* end date for search data (ISO8601 format: YYYY-MM-DD)
|
|
262
|
-
*/
|
|
263
|
-
toDate: z.string().optional(),
|
|
264
|
-
/**
|
|
265
|
-
* maximum number of search results to consider
|
|
266
|
-
* defaults to 20
|
|
267
|
-
*/
|
|
268
|
-
maxSearchResults: z.number().min(1).max(50).optional(),
|
|
269
|
-
/**
|
|
270
|
-
* data sources to search from.
|
|
271
|
-
* defaults to [{ type: 'web' }, { type: 'x' }] if not specified.
|
|
272
|
-
*
|
|
273
|
-
* @example
|
|
274
|
-
* sources: [{ type: 'web', country: 'US' }, { type: 'x' }]
|
|
275
|
-
*/
|
|
276
|
-
sources: z.array(searchSourceSchema).optional()
|
|
277
|
-
}).optional()
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
// src/xai-error.ts
|
|
281
|
-
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
282
|
-
import { z as z2 } from "zod/v4";
|
|
283
|
-
var xaiErrorDataSchema = z2.object({
|
|
284
|
-
error: z2.object({
|
|
285
|
-
message: z2.string(),
|
|
286
|
-
type: z2.string().nullish(),
|
|
287
|
-
param: z2.any().nullish(),
|
|
288
|
-
code: z2.union([z2.string(), z2.number()]).nullish()
|
|
289
|
-
})
|
|
290
|
-
});
|
|
291
|
-
var xaiFailedResponseHandler = createJsonErrorResponseHandler({
|
|
292
|
-
errorSchema: xaiErrorDataSchema,
|
|
293
|
-
errorToMessage: (data) => data.error.message
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
// src/xai-prepare-tools.ts
|
|
297
|
-
import {
|
|
298
|
-
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
299
|
-
} from "@ai-sdk/provider";
|
|
300
|
-
function prepareTools({
|
|
301
|
-
tools,
|
|
302
|
-
toolChoice
|
|
303
|
-
}) {
|
|
304
|
-
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
305
|
-
const toolWarnings = [];
|
|
306
|
-
if (tools == null) {
|
|
307
|
-
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
308
|
-
}
|
|
309
|
-
const xaiTools2 = [];
|
|
310
|
-
for (const tool of tools) {
|
|
311
|
-
if (tool.type === "provider") {
|
|
312
|
-
toolWarnings.push({
|
|
313
|
-
type: "unsupported",
|
|
314
|
-
feature: `provider-defined tool ${tool.name}`
|
|
315
|
-
});
|
|
316
|
-
} else {
|
|
317
|
-
xaiTools2.push({
|
|
318
|
-
type: "function",
|
|
319
|
-
function: {
|
|
320
|
-
name: tool.name,
|
|
321
|
-
description: tool.description,
|
|
322
|
-
parameters: tool.inputSchema,
|
|
323
|
-
...tool.strict != null ? { strict: tool.strict } : {}
|
|
324
|
-
}
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
if (toolChoice == null) {
|
|
329
|
-
return { tools: xaiTools2, toolChoice: void 0, toolWarnings };
|
|
330
|
-
}
|
|
331
|
-
const type = toolChoice.type;
|
|
332
|
-
switch (type) {
|
|
333
|
-
case "auto":
|
|
334
|
-
case "none":
|
|
335
|
-
return { tools: xaiTools2, toolChoice: type, toolWarnings };
|
|
336
|
-
case "required":
|
|
337
|
-
return { tools: xaiTools2, toolChoice: "required", toolWarnings };
|
|
338
|
-
case "tool":
|
|
339
|
-
return {
|
|
340
|
-
tools: xaiTools2,
|
|
341
|
-
toolChoice: {
|
|
342
|
-
type: "function",
|
|
343
|
-
function: { name: toolChoice.toolName }
|
|
344
|
-
},
|
|
345
|
-
toolWarnings
|
|
346
|
-
};
|
|
347
|
-
default: {
|
|
348
|
-
const _exhaustiveCheck = type;
|
|
349
|
-
throw new UnsupportedFunctionalityError2({
|
|
350
|
-
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
351
|
-
});
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
// src/xai-chat-language-model.ts
|
|
357
|
-
var XaiChatLanguageModel = class {
|
|
358
|
-
constructor(modelId, config) {
|
|
359
|
-
this.specificationVersion = "v3";
|
|
360
|
-
this.supportedUrls = {
|
|
361
|
-
"image/*": [/^https?:\/\/.*$/]
|
|
362
|
-
};
|
|
363
|
-
this.modelId = modelId;
|
|
364
|
-
this.config = config;
|
|
365
|
-
}
|
|
366
|
-
get provider() {
|
|
367
|
-
return this.config.provider;
|
|
368
|
-
}
|
|
369
|
-
async getArgs({
|
|
370
|
-
prompt,
|
|
371
|
-
maxOutputTokens,
|
|
372
|
-
temperature,
|
|
373
|
-
topP,
|
|
374
|
-
topK,
|
|
375
|
-
frequencyPenalty,
|
|
376
|
-
presencePenalty,
|
|
377
|
-
stopSequences,
|
|
378
|
-
seed,
|
|
379
|
-
responseFormat,
|
|
380
|
-
providerOptions,
|
|
381
|
-
tools,
|
|
382
|
-
toolChoice
|
|
383
|
-
}) {
|
|
384
|
-
var _a, _b, _c;
|
|
385
|
-
const warnings = [];
|
|
386
|
-
const options = (_a = await parseProviderOptions({
|
|
387
|
-
provider: "xai",
|
|
388
|
-
providerOptions,
|
|
389
|
-
schema: xaiLanguageModelChatOptions
|
|
390
|
-
})) != null ? _a : {};
|
|
391
|
-
if (topK != null) {
|
|
392
|
-
warnings.push({ type: "unsupported", feature: "topK" });
|
|
393
|
-
}
|
|
394
|
-
if (frequencyPenalty != null) {
|
|
395
|
-
warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
|
|
396
|
-
}
|
|
397
|
-
if (presencePenalty != null) {
|
|
398
|
-
warnings.push({ type: "unsupported", feature: "presencePenalty" });
|
|
399
|
-
}
|
|
400
|
-
if (stopSequences != null) {
|
|
401
|
-
warnings.push({ type: "unsupported", feature: "stopSequences" });
|
|
402
|
-
}
|
|
403
|
-
const { messages, warnings: messageWarnings } = convertToXaiChatMessages(prompt);
|
|
404
|
-
warnings.push(...messageWarnings);
|
|
405
|
-
const {
|
|
406
|
-
tools: xaiTools2,
|
|
407
|
-
toolChoice: xaiToolChoice,
|
|
408
|
-
toolWarnings
|
|
409
|
-
} = prepareTools({
|
|
410
|
-
tools,
|
|
411
|
-
toolChoice
|
|
412
|
-
});
|
|
413
|
-
warnings.push(...toolWarnings);
|
|
414
|
-
const baseArgs = {
|
|
415
|
-
// model id
|
|
416
|
-
model: this.modelId,
|
|
417
|
-
// standard generation settings
|
|
418
|
-
logprobs: options.logprobs === true || options.topLogprobs != null ? true : void 0,
|
|
419
|
-
top_logprobs: options.topLogprobs,
|
|
420
|
-
max_completion_tokens: maxOutputTokens,
|
|
421
|
-
temperature,
|
|
422
|
-
top_p: topP,
|
|
423
|
-
seed,
|
|
424
|
-
reasoning_effort: options.reasoningEffort,
|
|
425
|
-
// parallel function calling
|
|
426
|
-
parallel_function_calling: options.parallel_function_calling,
|
|
427
|
-
// response format
|
|
428
|
-
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? responseFormat.schema != null ? {
|
|
429
|
-
type: "json_schema",
|
|
430
|
-
json_schema: {
|
|
431
|
-
name: (_b = responseFormat.name) != null ? _b : "response",
|
|
432
|
-
schema: responseFormat.schema,
|
|
433
|
-
strict: true
|
|
434
|
-
}
|
|
435
|
-
} : { type: "json_object" } : void 0,
|
|
436
|
-
// search parameters
|
|
437
|
-
search_parameters: options.searchParameters ? {
|
|
438
|
-
mode: options.searchParameters.mode,
|
|
439
|
-
return_citations: options.searchParameters.returnCitations,
|
|
440
|
-
from_date: options.searchParameters.fromDate,
|
|
441
|
-
to_date: options.searchParameters.toDate,
|
|
442
|
-
max_search_results: options.searchParameters.maxSearchResults,
|
|
443
|
-
sources: (_c = options.searchParameters.sources) == null ? void 0 : _c.map((source) => {
|
|
444
|
-
var _a2;
|
|
445
|
-
return {
|
|
446
|
-
type: source.type,
|
|
447
|
-
...source.type === "web" && {
|
|
448
|
-
country: source.country,
|
|
449
|
-
excluded_websites: source.excludedWebsites,
|
|
450
|
-
allowed_websites: source.allowedWebsites,
|
|
451
|
-
safe_search: source.safeSearch
|
|
452
|
-
},
|
|
453
|
-
...source.type === "x" && {
|
|
454
|
-
excluded_x_handles: source.excludedXHandles,
|
|
455
|
-
included_x_handles: (_a2 = source.includedXHandles) != null ? _a2 : source.xHandles,
|
|
456
|
-
post_favorite_count: source.postFavoriteCount,
|
|
457
|
-
post_view_count: source.postViewCount
|
|
458
|
-
},
|
|
459
|
-
...source.type === "news" && {
|
|
460
|
-
country: source.country,
|
|
461
|
-
excluded_websites: source.excludedWebsites,
|
|
462
|
-
safe_search: source.safeSearch
|
|
463
|
-
},
|
|
464
|
-
...source.type === "rss" && {
|
|
465
|
-
links: source.links
|
|
466
|
-
}
|
|
467
|
-
};
|
|
468
|
-
})
|
|
469
|
-
} : void 0,
|
|
470
|
-
// messages in xai format
|
|
471
|
-
messages,
|
|
472
|
-
// tools in xai format
|
|
473
|
-
tools: xaiTools2,
|
|
474
|
-
tool_choice: xaiToolChoice
|
|
475
|
-
};
|
|
476
|
-
return {
|
|
477
|
-
args: baseArgs,
|
|
478
|
-
warnings
|
|
479
|
-
};
|
|
480
|
-
}
|
|
481
|
-
async doGenerate(options) {
|
|
482
|
-
var _a, _b;
|
|
483
|
-
const { args: body, warnings } = await this.getArgs(options);
|
|
484
|
-
const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
|
|
485
|
-
const {
|
|
486
|
-
responseHeaders,
|
|
487
|
-
value: response,
|
|
488
|
-
rawValue: rawResponse
|
|
489
|
-
} = await postJsonToApi({
|
|
490
|
-
url,
|
|
491
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
492
|
-
body,
|
|
493
|
-
failedResponseHandler: xaiFailedResponseHandler,
|
|
494
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
495
|
-
xaiChatResponseSchema
|
|
496
|
-
),
|
|
497
|
-
abortSignal: options.abortSignal,
|
|
498
|
-
fetch: this.config.fetch
|
|
499
|
-
});
|
|
500
|
-
if (response.error != null) {
|
|
501
|
-
throw new APICallError({
|
|
502
|
-
message: response.error,
|
|
503
|
-
url,
|
|
504
|
-
requestBodyValues: body,
|
|
505
|
-
statusCode: 200,
|
|
506
|
-
responseHeaders,
|
|
507
|
-
responseBody: JSON.stringify(rawResponse),
|
|
508
|
-
isRetryable: response.code === "The service is currently unavailable"
|
|
509
|
-
});
|
|
510
|
-
}
|
|
511
|
-
const choice = response.choices[0];
|
|
512
|
-
const content = [];
|
|
513
|
-
if (choice.message.content != null && choice.message.content.length > 0) {
|
|
514
|
-
let text = choice.message.content;
|
|
515
|
-
const lastMessage = body.messages[body.messages.length - 1];
|
|
516
|
-
if ((lastMessage == null ? void 0 : lastMessage.role) === "assistant" && text === lastMessage.content) {
|
|
517
|
-
text = "";
|
|
518
|
-
}
|
|
519
|
-
if (text.length > 0) {
|
|
520
|
-
content.push({ type: "text", text });
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
if (choice.message.reasoning_content != null && choice.message.reasoning_content.length > 0) {
|
|
524
|
-
content.push({
|
|
525
|
-
type: "reasoning",
|
|
526
|
-
text: choice.message.reasoning_content
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
if (choice.message.tool_calls != null) {
|
|
530
|
-
for (const toolCall of choice.message.tool_calls) {
|
|
531
|
-
content.push({
|
|
532
|
-
type: "tool-call",
|
|
533
|
-
toolCallId: toolCall.id,
|
|
534
|
-
toolName: toolCall.function.name,
|
|
535
|
-
input: toolCall.function.arguments
|
|
536
|
-
});
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
if (response.citations != null) {
|
|
540
|
-
for (const url2 of response.citations) {
|
|
541
|
-
content.push({
|
|
542
|
-
type: "source",
|
|
543
|
-
sourceType: "url",
|
|
544
|
-
id: this.config.generateId(),
|
|
545
|
-
url: url2
|
|
546
|
-
});
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
return {
|
|
550
|
-
content,
|
|
551
|
-
finishReason: {
|
|
552
|
-
unified: mapXaiFinishReason(choice.finish_reason),
|
|
553
|
-
raw: (_b = choice.finish_reason) != null ? _b : void 0
|
|
554
|
-
},
|
|
555
|
-
usage: response.usage ? convertXaiChatUsage(response.usage) : {
|
|
556
|
-
inputTokens: { total: 0, noCache: 0, cacheRead: 0, cacheWrite: 0 },
|
|
557
|
-
outputTokens: { total: 0, text: 0, reasoning: 0 }
|
|
558
|
-
},
|
|
559
|
-
request: { body },
|
|
560
|
-
response: {
|
|
561
|
-
...getResponseMetadata(response),
|
|
562
|
-
headers: responseHeaders,
|
|
563
|
-
body: rawResponse
|
|
564
|
-
},
|
|
565
|
-
warnings
|
|
566
|
-
};
|
|
567
|
-
}
|
|
568
|
-
async doStream(options) {
|
|
569
|
-
var _a;
|
|
570
|
-
const { args, warnings } = await this.getArgs(options);
|
|
571
|
-
const body = {
|
|
572
|
-
...args,
|
|
573
|
-
stream: true,
|
|
574
|
-
stream_options: {
|
|
575
|
-
include_usage: true
|
|
576
|
-
}
|
|
577
|
-
};
|
|
578
|
-
const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
|
|
579
|
-
const { responseHeaders, value: response } = await postJsonToApi({
|
|
580
|
-
url,
|
|
581
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
582
|
-
body,
|
|
583
|
-
failedResponseHandler: xaiFailedResponseHandler,
|
|
584
|
-
successfulResponseHandler: async ({ response: response2 }) => {
|
|
585
|
-
const responseHeaders2 = extractResponseHeaders(response2);
|
|
586
|
-
const contentType = response2.headers.get("content-type");
|
|
587
|
-
if (contentType == null ? void 0 : contentType.includes("application/json")) {
|
|
588
|
-
const responseBody = await response2.text();
|
|
589
|
-
const parsedError = await safeParseJSON({
|
|
590
|
-
text: responseBody,
|
|
591
|
-
schema: xaiStreamErrorSchema
|
|
592
|
-
});
|
|
593
|
-
if (parsedError.success) {
|
|
594
|
-
throw new APICallError({
|
|
595
|
-
message: parsedError.value.error,
|
|
596
|
-
url,
|
|
597
|
-
requestBodyValues: body,
|
|
598
|
-
statusCode: 200,
|
|
599
|
-
responseHeaders: responseHeaders2,
|
|
600
|
-
responseBody,
|
|
601
|
-
isRetryable: parsedError.value.code === "The service is currently unavailable"
|
|
602
|
-
});
|
|
603
|
-
}
|
|
604
|
-
throw new APICallError({
|
|
605
|
-
message: "Invalid JSON response",
|
|
606
|
-
url,
|
|
607
|
-
requestBodyValues: body,
|
|
608
|
-
statusCode: 200,
|
|
609
|
-
responseHeaders: responseHeaders2,
|
|
610
|
-
responseBody
|
|
611
|
-
});
|
|
612
|
-
}
|
|
613
|
-
return createEventSourceResponseHandler(xaiChatChunkSchema)({
|
|
614
|
-
response: response2,
|
|
615
|
-
url,
|
|
616
|
-
requestBodyValues: body
|
|
617
|
-
});
|
|
618
|
-
},
|
|
619
|
-
abortSignal: options.abortSignal,
|
|
620
|
-
fetch: this.config.fetch
|
|
621
|
-
});
|
|
622
|
-
let finishReason = {
|
|
623
|
-
unified: "other",
|
|
624
|
-
raw: void 0
|
|
625
|
-
};
|
|
626
|
-
let usage = void 0;
|
|
627
|
-
let isFirstChunk = true;
|
|
628
|
-
const contentBlocks = {};
|
|
629
|
-
const lastReasoningDeltas = {};
|
|
630
|
-
let activeReasoningBlockId = void 0;
|
|
631
|
-
const self = this;
|
|
632
|
-
return {
|
|
633
|
-
stream: response.pipeThrough(
|
|
634
|
-
new TransformStream({
|
|
635
|
-
start(controller) {
|
|
636
|
-
controller.enqueue({ type: "stream-start", warnings });
|
|
637
|
-
},
|
|
638
|
-
transform(chunk, controller) {
|
|
639
|
-
if (options.includeRawChunks) {
|
|
640
|
-
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
641
|
-
}
|
|
642
|
-
if (!chunk.success) {
|
|
643
|
-
controller.enqueue({ type: "error", error: chunk.error });
|
|
644
|
-
return;
|
|
645
|
-
}
|
|
646
|
-
const value = chunk.value;
|
|
647
|
-
if (isFirstChunk) {
|
|
648
|
-
controller.enqueue({
|
|
649
|
-
type: "response-metadata",
|
|
650
|
-
...getResponseMetadata(value)
|
|
651
|
-
});
|
|
652
|
-
isFirstChunk = false;
|
|
653
|
-
}
|
|
654
|
-
if (value.citations != null) {
|
|
655
|
-
for (const url2 of value.citations) {
|
|
656
|
-
controller.enqueue({
|
|
657
|
-
type: "source",
|
|
658
|
-
sourceType: "url",
|
|
659
|
-
id: self.config.generateId(),
|
|
660
|
-
url: url2
|
|
661
|
-
});
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
if (value.usage != null) {
|
|
665
|
-
usage = convertXaiChatUsage(value.usage);
|
|
666
|
-
}
|
|
667
|
-
const choice = value.choices[0];
|
|
668
|
-
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
669
|
-
finishReason = {
|
|
670
|
-
unified: mapXaiFinishReason(choice.finish_reason),
|
|
671
|
-
raw: choice.finish_reason
|
|
672
|
-
};
|
|
673
|
-
}
|
|
674
|
-
if ((choice == null ? void 0 : choice.delta) == null) {
|
|
675
|
-
return;
|
|
676
|
-
}
|
|
677
|
-
const delta = choice.delta;
|
|
678
|
-
const choiceIndex = choice.index;
|
|
679
|
-
if (delta.content != null && delta.content.length > 0) {
|
|
680
|
-
const textContent = delta.content;
|
|
681
|
-
if (activeReasoningBlockId != null && !contentBlocks[activeReasoningBlockId].ended) {
|
|
682
|
-
controller.enqueue({
|
|
683
|
-
type: "reasoning-end",
|
|
684
|
-
id: activeReasoningBlockId
|
|
685
|
-
});
|
|
686
|
-
contentBlocks[activeReasoningBlockId].ended = true;
|
|
687
|
-
activeReasoningBlockId = void 0;
|
|
688
|
-
}
|
|
689
|
-
const lastMessage = body.messages[body.messages.length - 1];
|
|
690
|
-
if ((lastMessage == null ? void 0 : lastMessage.role) === "assistant" && textContent === lastMessage.content) {
|
|
691
|
-
return;
|
|
692
|
-
}
|
|
693
|
-
const blockId = `text-${value.id || choiceIndex}`;
|
|
694
|
-
if (contentBlocks[blockId] == null) {
|
|
695
|
-
contentBlocks[blockId] = { type: "text", ended: false };
|
|
696
|
-
controller.enqueue({
|
|
697
|
-
type: "text-start",
|
|
698
|
-
id: blockId
|
|
699
|
-
});
|
|
700
|
-
}
|
|
701
|
-
controller.enqueue({
|
|
702
|
-
type: "text-delta",
|
|
703
|
-
id: blockId,
|
|
704
|
-
delta: textContent
|
|
705
|
-
});
|
|
706
|
-
}
|
|
707
|
-
if (delta.reasoning_content != null && delta.reasoning_content.length > 0) {
|
|
708
|
-
const blockId = `reasoning-${value.id || choiceIndex}`;
|
|
709
|
-
if (lastReasoningDeltas[blockId] === delta.reasoning_content) {
|
|
710
|
-
return;
|
|
711
|
-
}
|
|
712
|
-
lastReasoningDeltas[blockId] = delta.reasoning_content;
|
|
713
|
-
if (contentBlocks[blockId] == null) {
|
|
714
|
-
contentBlocks[blockId] = { type: "reasoning", ended: false };
|
|
715
|
-
activeReasoningBlockId = blockId;
|
|
716
|
-
controller.enqueue({
|
|
717
|
-
type: "reasoning-start",
|
|
718
|
-
id: blockId
|
|
719
|
-
});
|
|
720
|
-
}
|
|
721
|
-
controller.enqueue({
|
|
722
|
-
type: "reasoning-delta",
|
|
723
|
-
id: blockId,
|
|
724
|
-
delta: delta.reasoning_content
|
|
725
|
-
});
|
|
726
|
-
}
|
|
727
|
-
if (delta.tool_calls != null) {
|
|
728
|
-
if (activeReasoningBlockId != null && !contentBlocks[activeReasoningBlockId].ended) {
|
|
729
|
-
controller.enqueue({
|
|
730
|
-
type: "reasoning-end",
|
|
731
|
-
id: activeReasoningBlockId
|
|
732
|
-
});
|
|
733
|
-
contentBlocks[activeReasoningBlockId].ended = true;
|
|
734
|
-
activeReasoningBlockId = void 0;
|
|
735
|
-
}
|
|
736
|
-
for (const toolCall of delta.tool_calls) {
|
|
737
|
-
const toolCallId = toolCall.id;
|
|
738
|
-
controller.enqueue({
|
|
739
|
-
type: "tool-input-start",
|
|
740
|
-
id: toolCallId,
|
|
741
|
-
toolName: toolCall.function.name
|
|
742
|
-
});
|
|
743
|
-
controller.enqueue({
|
|
744
|
-
type: "tool-input-delta",
|
|
745
|
-
id: toolCallId,
|
|
746
|
-
delta: toolCall.function.arguments
|
|
747
|
-
});
|
|
748
|
-
controller.enqueue({
|
|
749
|
-
type: "tool-input-end",
|
|
750
|
-
id: toolCallId
|
|
751
|
-
});
|
|
752
|
-
controller.enqueue({
|
|
753
|
-
type: "tool-call",
|
|
754
|
-
toolCallId,
|
|
755
|
-
toolName: toolCall.function.name,
|
|
756
|
-
input: toolCall.function.arguments
|
|
757
|
-
});
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
},
|
|
761
|
-
flush(controller) {
|
|
762
|
-
for (const [blockId, block] of Object.entries(contentBlocks)) {
|
|
763
|
-
if (!block.ended) {
|
|
764
|
-
controller.enqueue({
|
|
765
|
-
type: block.type === "text" ? "text-end" : "reasoning-end",
|
|
766
|
-
id: blockId
|
|
767
|
-
});
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
controller.enqueue({
|
|
771
|
-
type: "finish",
|
|
772
|
-
finishReason,
|
|
773
|
-
usage: usage != null ? usage : {
|
|
774
|
-
inputTokens: {
|
|
775
|
-
total: 0,
|
|
776
|
-
noCache: 0,
|
|
777
|
-
cacheRead: 0,
|
|
778
|
-
cacheWrite: 0
|
|
779
|
-
},
|
|
780
|
-
outputTokens: { total: 0, text: 0, reasoning: 0 }
|
|
781
|
-
}
|
|
782
|
-
});
|
|
783
|
-
}
|
|
784
|
-
})
|
|
785
|
-
),
|
|
786
|
-
request: { body },
|
|
787
|
-
response: { headers: responseHeaders }
|
|
788
|
-
};
|
|
789
|
-
}
|
|
790
|
-
};
|
|
791
|
-
var xaiUsageSchema = z3.object({
|
|
792
|
-
prompt_tokens: z3.number(),
|
|
793
|
-
completion_tokens: z3.number(),
|
|
794
|
-
total_tokens: z3.number(),
|
|
795
|
-
prompt_tokens_details: z3.object({
|
|
796
|
-
text_tokens: z3.number().nullish(),
|
|
797
|
-
audio_tokens: z3.number().nullish(),
|
|
798
|
-
image_tokens: z3.number().nullish(),
|
|
799
|
-
cached_tokens: z3.number().nullish()
|
|
800
|
-
}).nullish(),
|
|
801
|
-
completion_tokens_details: z3.object({
|
|
802
|
-
reasoning_tokens: z3.number().nullish(),
|
|
803
|
-
audio_tokens: z3.number().nullish(),
|
|
804
|
-
accepted_prediction_tokens: z3.number().nullish(),
|
|
805
|
-
rejected_prediction_tokens: z3.number().nullish()
|
|
806
|
-
}).nullish()
|
|
807
|
-
});
|
|
808
|
-
var xaiChatResponseSchema = z3.object({
|
|
809
|
-
id: z3.string().nullish(),
|
|
810
|
-
created: z3.number().nullish(),
|
|
811
|
-
model: z3.string().nullish(),
|
|
812
|
-
choices: z3.array(
|
|
813
|
-
z3.object({
|
|
814
|
-
message: z3.object({
|
|
815
|
-
role: z3.literal("assistant"),
|
|
816
|
-
content: z3.string().nullish(),
|
|
817
|
-
reasoning_content: z3.string().nullish(),
|
|
818
|
-
tool_calls: z3.array(
|
|
819
|
-
z3.object({
|
|
820
|
-
id: z3.string(),
|
|
821
|
-
type: z3.literal("function"),
|
|
822
|
-
function: z3.object({
|
|
823
|
-
name: z3.string(),
|
|
824
|
-
arguments: z3.string()
|
|
825
|
-
})
|
|
826
|
-
})
|
|
827
|
-
).nullish()
|
|
828
|
-
}),
|
|
829
|
-
index: z3.number(),
|
|
830
|
-
finish_reason: z3.string().nullish()
|
|
831
|
-
})
|
|
832
|
-
).nullish(),
|
|
833
|
-
object: z3.literal("chat.completion").nullish(),
|
|
834
|
-
usage: xaiUsageSchema.nullish(),
|
|
835
|
-
citations: z3.array(z3.string().url()).nullish(),
|
|
836
|
-
code: z3.string().nullish(),
|
|
837
|
-
error: z3.string().nullish()
|
|
838
|
-
});
|
|
839
|
-
var xaiChatChunkSchema = z3.object({
|
|
840
|
-
id: z3.string().nullish(),
|
|
841
|
-
created: z3.number().nullish(),
|
|
842
|
-
model: z3.string().nullish(),
|
|
843
|
-
choices: z3.array(
|
|
844
|
-
z3.object({
|
|
845
|
-
delta: z3.object({
|
|
846
|
-
role: z3.enum(["assistant"]).optional(),
|
|
847
|
-
content: z3.string().nullish(),
|
|
848
|
-
reasoning_content: z3.string().nullish(),
|
|
849
|
-
tool_calls: z3.array(
|
|
850
|
-
z3.object({
|
|
851
|
-
id: z3.string(),
|
|
852
|
-
type: z3.literal("function"),
|
|
853
|
-
function: z3.object({
|
|
854
|
-
name: z3.string(),
|
|
855
|
-
arguments: z3.string()
|
|
856
|
-
})
|
|
857
|
-
})
|
|
858
|
-
).nullish()
|
|
859
|
-
}),
|
|
860
|
-
finish_reason: z3.string().nullish(),
|
|
861
|
-
index: z3.number()
|
|
862
|
-
})
|
|
863
|
-
),
|
|
864
|
-
usage: xaiUsageSchema.nullish(),
|
|
865
|
-
citations: z3.array(z3.string().url()).nullish()
|
|
866
|
-
});
|
|
867
|
-
var xaiStreamErrorSchema = z3.object({
|
|
868
|
-
code: z3.string(),
|
|
869
|
-
error: z3.string()
|
|
870
|
-
});
|
|
871
|
-
|
|
872
|
-
// src/xai-image-model.ts
|
|
873
|
-
import {
|
|
874
|
-
combineHeaders as combineHeaders2,
|
|
875
|
-
convertImageModelFileToDataUri,
|
|
876
|
-
createBinaryResponseHandler,
|
|
877
|
-
createJsonResponseHandler as createJsonResponseHandler2,
|
|
878
|
-
createStatusCodeErrorResponseHandler,
|
|
879
|
-
getFromApi,
|
|
880
|
-
parseProviderOptions as parseProviderOptions2,
|
|
881
|
-
postJsonToApi as postJsonToApi2
|
|
882
|
-
} from "@ai-sdk/provider-utils";
|
|
883
|
-
import { z as z5 } from "zod/v4";
|
|
884
|
-
|
|
885
|
-
// src/xai-image-options.ts
|
|
886
|
-
import { z as z4 } from "zod/v4";
|
|
887
|
-
var xaiImageModelOptions = z4.object({
|
|
888
|
-
aspect_ratio: z4.string().optional(),
|
|
889
|
-
output_format: z4.string().optional(),
|
|
890
|
-
sync_mode: z4.boolean().optional(),
|
|
891
|
-
resolution: z4.enum(["1k", "2k"]).optional(),
|
|
892
|
-
quality: z4.enum(["low", "medium", "high"]).optional(),
|
|
893
|
-
user: z4.string().optional()
|
|
894
|
-
});
|
|
895
|
-
|
|
896
|
-
// src/xai-image-model.ts
|
|
897
|
-
var XaiImageModel = class {
|
|
898
|
-
constructor(modelId, config) {
|
|
899
|
-
this.modelId = modelId;
|
|
900
|
-
this.config = config;
|
|
901
|
-
this.specificationVersion = "v3";
|
|
902
|
-
this.maxImagesPerCall = 3;
|
|
903
|
-
}
|
|
904
|
-
get provider() {
|
|
905
|
-
return this.config.provider;
|
|
906
|
-
}
|
|
907
|
-
async doGenerate({
|
|
908
|
-
prompt,
|
|
909
|
-
n,
|
|
910
|
-
size,
|
|
911
|
-
aspectRatio,
|
|
912
|
-
seed,
|
|
913
|
-
providerOptions,
|
|
914
|
-
headers,
|
|
915
|
-
abortSignal,
|
|
916
|
-
files,
|
|
917
|
-
mask
|
|
918
|
-
}) {
|
|
919
|
-
var _a, _b, _c, _d, _e;
|
|
920
|
-
const warnings = [];
|
|
921
|
-
if (size != null) {
|
|
922
|
-
warnings.push({
|
|
923
|
-
type: "unsupported",
|
|
924
|
-
feature: "size",
|
|
925
|
-
details: "This model does not support the `size` option. Use `aspectRatio` instead."
|
|
926
|
-
});
|
|
927
|
-
}
|
|
928
|
-
if (seed != null) {
|
|
929
|
-
warnings.push({
|
|
930
|
-
type: "unsupported",
|
|
931
|
-
feature: "seed"
|
|
932
|
-
});
|
|
933
|
-
}
|
|
934
|
-
if (mask != null) {
|
|
935
|
-
warnings.push({
|
|
936
|
-
type: "unsupported",
|
|
937
|
-
feature: "mask"
|
|
938
|
-
});
|
|
939
|
-
}
|
|
940
|
-
const xaiOptions = await parseProviderOptions2({
|
|
941
|
-
provider: "xai",
|
|
942
|
-
providerOptions,
|
|
943
|
-
schema: xaiImageModelOptions
|
|
944
|
-
});
|
|
945
|
-
const hasFiles = files != null && files.length > 0;
|
|
946
|
-
const imageUrls = hasFiles ? files.map((file) => convertImageModelFileToDataUri(file)) : [];
|
|
947
|
-
const endpoint = hasFiles ? "/images/edits" : "/images/generations";
|
|
948
|
-
const body = {
|
|
949
|
-
model: this.modelId,
|
|
950
|
-
prompt,
|
|
951
|
-
n,
|
|
952
|
-
response_format: "b64_json"
|
|
953
|
-
};
|
|
954
|
-
if (aspectRatio != null) {
|
|
955
|
-
body.aspect_ratio = aspectRatio;
|
|
956
|
-
}
|
|
957
|
-
if ((xaiOptions == null ? void 0 : xaiOptions.output_format) != null) {
|
|
958
|
-
body.output_format = xaiOptions.output_format;
|
|
959
|
-
}
|
|
960
|
-
if ((xaiOptions == null ? void 0 : xaiOptions.sync_mode) != null) {
|
|
961
|
-
body.sync_mode = xaiOptions.sync_mode;
|
|
962
|
-
}
|
|
963
|
-
if ((xaiOptions == null ? void 0 : xaiOptions.aspect_ratio) != null && aspectRatio == null) {
|
|
964
|
-
body.aspect_ratio = xaiOptions.aspect_ratio;
|
|
965
|
-
}
|
|
966
|
-
if ((xaiOptions == null ? void 0 : xaiOptions.resolution) != null) {
|
|
967
|
-
body.resolution = xaiOptions.resolution;
|
|
968
|
-
}
|
|
969
|
-
if ((xaiOptions == null ? void 0 : xaiOptions.quality) != null) {
|
|
970
|
-
body.quality = xaiOptions.quality;
|
|
971
|
-
}
|
|
972
|
-
if ((xaiOptions == null ? void 0 : xaiOptions.user) != null) {
|
|
973
|
-
body.user = xaiOptions.user;
|
|
974
|
-
}
|
|
975
|
-
if (imageUrls.length === 1) {
|
|
976
|
-
body.image = { url: imageUrls[0], type: "image_url" };
|
|
977
|
-
} else if (imageUrls.length > 1) {
|
|
978
|
-
body.images = imageUrls.map((url) => ({ url, type: "image_url" }));
|
|
979
|
-
}
|
|
980
|
-
const baseURL = (_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1";
|
|
981
|
-
const currentDate = (_d = (_c = (_b = this.config._internal) == null ? void 0 : _b.currentDate) == null ? void 0 : _c.call(_b)) != null ? _d : /* @__PURE__ */ new Date();
|
|
982
|
-
const { value: response, responseHeaders } = await postJsonToApi2({
|
|
983
|
-
url: `${baseURL}${endpoint}`,
|
|
984
|
-
headers: combineHeaders2(this.config.headers(), headers),
|
|
985
|
-
body,
|
|
986
|
-
failedResponseHandler: xaiFailedResponseHandler,
|
|
987
|
-
successfulResponseHandler: createJsonResponseHandler2(
|
|
988
|
-
xaiImageResponseSchema
|
|
989
|
-
),
|
|
990
|
-
abortSignal,
|
|
991
|
-
fetch: this.config.fetch
|
|
992
|
-
});
|
|
993
|
-
const hasAllBase64 = response.data.every((image) => image.b64_json != null);
|
|
994
|
-
const images = hasAllBase64 ? response.data.map((image) => image.b64_json) : await Promise.all(
|
|
995
|
-
response.data.map(
|
|
996
|
-
(image) => this.downloadImage(image.url, abortSignal)
|
|
997
|
-
)
|
|
998
|
-
);
|
|
999
|
-
return {
|
|
1000
|
-
images,
|
|
1001
|
-
warnings,
|
|
1002
|
-
response: {
|
|
1003
|
-
timestamp: currentDate,
|
|
1004
|
-
modelId: this.modelId,
|
|
1005
|
-
headers: responseHeaders
|
|
1006
|
-
},
|
|
1007
|
-
providerMetadata: {
|
|
1008
|
-
xai: {
|
|
1009
|
-
images: response.data.map((item) => ({
|
|
1010
|
-
...item.revised_prompt ? { revisedPrompt: item.revised_prompt } : {}
|
|
1011
|
-
})),
|
|
1012
|
-
...((_e = response.usage) == null ? void 0 : _e.cost_in_usd_ticks) != null ? { costInUsdTicks: response.usage.cost_in_usd_ticks } : {}
|
|
1013
|
-
}
|
|
1014
|
-
}
|
|
1015
|
-
};
|
|
1016
|
-
}
|
|
1017
|
-
async downloadImage(url, abortSignal) {
|
|
1018
|
-
const { value } = await getFromApi({
|
|
1019
|
-
url,
|
|
1020
|
-
abortSignal,
|
|
1021
|
-
failedResponseHandler: createStatusCodeErrorResponseHandler(),
|
|
1022
|
-
successfulResponseHandler: createBinaryResponseHandler(),
|
|
1023
|
-
fetch: this.config.fetch
|
|
1024
|
-
});
|
|
1025
|
-
return value;
|
|
1026
|
-
}
|
|
1027
|
-
};
|
|
1028
|
-
var xaiImageResponseSchema = z5.object({
|
|
1029
|
-
data: z5.array(
|
|
1030
|
-
z5.object({
|
|
1031
|
-
url: z5.string().nullish(),
|
|
1032
|
-
b64_json: z5.string().nullish(),
|
|
1033
|
-
revised_prompt: z5.string().nullish()
|
|
1034
|
-
})
|
|
1035
|
-
),
|
|
1036
|
-
usage: z5.object({
|
|
1037
|
-
cost_in_usd_ticks: z5.number().nullish()
|
|
1038
|
-
}).nullish()
|
|
1039
|
-
});
|
|
1040
|
-
|
|
1041
|
-
// src/responses/xai-responses-language-model.ts
|
|
1042
|
-
import {
|
|
1043
|
-
combineHeaders as combineHeaders3,
|
|
1044
|
-
createEventSourceResponseHandler as createEventSourceResponseHandler2,
|
|
1045
|
-
createJsonResponseHandler as createJsonResponseHandler3,
|
|
1046
|
-
parseProviderOptions as parseProviderOptions3,
|
|
1047
|
-
postJsonToApi as postJsonToApi3
|
|
1048
|
-
} from "@ai-sdk/provider-utils";
|
|
1049
|
-
|
|
1050
|
-
// src/responses/convert-to-xai-responses-input.ts
|
|
1051
|
-
import {
|
|
1052
|
-
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
1053
|
-
} from "@ai-sdk/provider";
|
|
1054
|
-
import { convertToBase64 as convertToBase642 } from "@ai-sdk/provider-utils";
|
|
1055
|
-
async function convertToXaiResponsesInput({
|
|
1056
|
-
prompt
|
|
1057
|
-
}) {
|
|
1058
|
-
var _a, _b, _c, _d, _e;
|
|
1059
|
-
const input = [];
|
|
1060
|
-
const inputWarnings = [];
|
|
1061
|
-
for (const message of prompt) {
|
|
1062
|
-
switch (message.role) {
|
|
1063
|
-
case "system": {
|
|
1064
|
-
input.push({
|
|
1065
|
-
role: "system",
|
|
1066
|
-
content: message.content
|
|
1067
|
-
});
|
|
1068
|
-
break;
|
|
1069
|
-
}
|
|
1070
|
-
case "user": {
|
|
1071
|
-
const contentParts = [];
|
|
1072
|
-
for (const block of message.content) {
|
|
1073
|
-
switch (block.type) {
|
|
1074
|
-
case "text": {
|
|
1075
|
-
contentParts.push({ type: "input_text", text: block.text });
|
|
1076
|
-
break;
|
|
1077
|
-
}
|
|
1078
|
-
case "file": {
|
|
1079
|
-
if (block.mediaType.startsWith("image/")) {
|
|
1080
|
-
const mediaType = block.mediaType === "image/*" ? "image/jpeg" : block.mediaType;
|
|
1081
|
-
const imageUrl = block.data instanceof URL ? block.data.toString() : `data:${mediaType};base64,${convertToBase642(block.data)}`;
|
|
1082
|
-
contentParts.push({ type: "input_image", image_url: imageUrl });
|
|
1083
|
-
} else {
|
|
1084
|
-
throw new UnsupportedFunctionalityError3({
|
|
1085
|
-
functionality: `file part media type ${block.mediaType}`
|
|
1086
|
-
});
|
|
1087
|
-
}
|
|
1088
|
-
break;
|
|
1089
|
-
}
|
|
1090
|
-
default: {
|
|
1091
|
-
const _exhaustiveCheck = block;
|
|
1092
|
-
inputWarnings.push({
|
|
1093
|
-
type: "other",
|
|
1094
|
-
message: "xAI Responses API does not support this content type in user messages"
|
|
1095
|
-
});
|
|
1096
|
-
}
|
|
1097
|
-
}
|
|
1098
|
-
}
|
|
1099
|
-
input.push({
|
|
1100
|
-
role: "user",
|
|
1101
|
-
content: contentParts
|
|
1102
|
-
});
|
|
1103
|
-
break;
|
|
1104
|
-
}
|
|
1105
|
-
case "assistant": {
|
|
1106
|
-
for (const part of message.content) {
|
|
1107
|
-
switch (part.type) {
|
|
1108
|
-
case "text": {
|
|
1109
|
-
const id = typeof ((_b = (_a = part.providerOptions) == null ? void 0 : _a.xai) == null ? void 0 : _b.itemId) === "string" ? part.providerOptions.xai.itemId : void 0;
|
|
1110
|
-
input.push({
|
|
1111
|
-
role: "assistant",
|
|
1112
|
-
content: part.text,
|
|
1113
|
-
id
|
|
1114
|
-
});
|
|
1115
|
-
break;
|
|
1116
|
-
}
|
|
1117
|
-
case "tool-call": {
|
|
1118
|
-
if (part.providerExecuted) {
|
|
1119
|
-
break;
|
|
1120
|
-
}
|
|
1121
|
-
const id = typeof ((_d = (_c = part.providerOptions) == null ? void 0 : _c.xai) == null ? void 0 : _d.itemId) === "string" ? part.providerOptions.xai.itemId : void 0;
|
|
1122
|
-
input.push({
|
|
1123
|
-
type: "function_call",
|
|
1124
|
-
id: id != null ? id : part.toolCallId,
|
|
1125
|
-
call_id: part.toolCallId,
|
|
1126
|
-
name: part.toolName,
|
|
1127
|
-
arguments: JSON.stringify(part.input),
|
|
1128
|
-
status: "completed"
|
|
1129
|
-
});
|
|
1130
|
-
break;
|
|
1131
|
-
}
|
|
1132
|
-
case "tool-result": {
|
|
1133
|
-
break;
|
|
1134
|
-
}
|
|
1135
|
-
case "reasoning":
|
|
1136
|
-
case "file": {
|
|
1137
|
-
inputWarnings.push({
|
|
1138
|
-
type: "other",
|
|
1139
|
-
message: `xAI Responses API does not support ${part.type} in assistant messages`
|
|
1140
|
-
});
|
|
1141
|
-
break;
|
|
1142
|
-
}
|
|
1143
|
-
default: {
|
|
1144
|
-
const _exhaustiveCheck = part;
|
|
1145
|
-
inputWarnings.push({
|
|
1146
|
-
type: "other",
|
|
1147
|
-
message: "xAI Responses API does not support this content type in assistant messages"
|
|
1148
|
-
});
|
|
1149
|
-
}
|
|
1150
|
-
}
|
|
1151
|
-
}
|
|
1152
|
-
break;
|
|
1153
|
-
}
|
|
1154
|
-
case "tool": {
|
|
1155
|
-
for (const part of message.content) {
|
|
1156
|
-
if (part.type === "tool-approval-response") {
|
|
1157
|
-
continue;
|
|
1158
|
-
}
|
|
1159
|
-
const output = part.output;
|
|
1160
|
-
let outputValue;
|
|
1161
|
-
switch (output.type) {
|
|
1162
|
-
case "text":
|
|
1163
|
-
case "error-text":
|
|
1164
|
-
outputValue = output.value;
|
|
1165
|
-
break;
|
|
1166
|
-
case "execution-denied":
|
|
1167
|
-
outputValue = (_e = output.reason) != null ? _e : "tool execution denied";
|
|
1168
|
-
break;
|
|
1169
|
-
case "json":
|
|
1170
|
-
case "error-json":
|
|
1171
|
-
outputValue = JSON.stringify(output.value);
|
|
1172
|
-
break;
|
|
1173
|
-
case "content":
|
|
1174
|
-
outputValue = output.value.map((item) => {
|
|
1175
|
-
if (item.type === "text") {
|
|
1176
|
-
return item.text;
|
|
1177
|
-
}
|
|
1178
|
-
return "";
|
|
1179
|
-
}).join("");
|
|
1180
|
-
break;
|
|
1181
|
-
default: {
|
|
1182
|
-
const _exhaustiveCheck = output;
|
|
1183
|
-
outputValue = "";
|
|
1184
|
-
}
|
|
1185
|
-
}
|
|
1186
|
-
input.push({
|
|
1187
|
-
type: "function_call_output",
|
|
1188
|
-
call_id: part.toolCallId,
|
|
1189
|
-
output: outputValue
|
|
1190
|
-
});
|
|
1191
|
-
}
|
|
1192
|
-
break;
|
|
1193
|
-
}
|
|
1194
|
-
default: {
|
|
1195
|
-
const _exhaustiveCheck = message;
|
|
1196
|
-
inputWarnings.push({
|
|
1197
|
-
type: "other",
|
|
1198
|
-
message: "unsupported message role"
|
|
1199
|
-
});
|
|
1200
|
-
}
|
|
1201
|
-
}
|
|
1202
|
-
}
|
|
1203
|
-
return { input, inputWarnings };
|
|
1204
|
-
}
|
|
1205
|
-
|
|
1206
|
-
// src/responses/convert-xai-responses-usage.ts
|
|
1207
|
-
function convertXaiResponsesUsage(usage) {
|
|
1208
|
-
var _a, _b, _c, _d;
|
|
1209
|
-
const cacheReadTokens = (_b = (_a = usage.input_tokens_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0;
|
|
1210
|
-
const reasoningTokens = (_d = (_c = usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0;
|
|
1211
|
-
const inputTokensIncludesCached = cacheReadTokens <= usage.input_tokens;
|
|
1212
|
-
return {
|
|
1213
|
-
inputTokens: {
|
|
1214
|
-
total: inputTokensIncludesCached ? usage.input_tokens : usage.input_tokens + cacheReadTokens,
|
|
1215
|
-
noCache: inputTokensIncludesCached ? usage.input_tokens - cacheReadTokens : usage.input_tokens,
|
|
1216
|
-
cacheRead: cacheReadTokens,
|
|
1217
|
-
cacheWrite: void 0
|
|
1218
|
-
},
|
|
1219
|
-
outputTokens: {
|
|
1220
|
-
total: usage.output_tokens,
|
|
1221
|
-
text: usage.output_tokens - reasoningTokens,
|
|
1222
|
-
reasoning: reasoningTokens
|
|
1223
|
-
},
|
|
1224
|
-
raw: usage
|
|
1225
|
-
};
|
|
1226
|
-
}
|
|
1227
|
-
|
|
1228
|
-
// src/responses/map-xai-responses-finish-reason.ts
|
|
1229
|
-
function mapXaiResponsesFinishReason(finishReason) {
|
|
1230
|
-
switch (finishReason) {
|
|
1231
|
-
case "stop":
|
|
1232
|
-
case "completed":
|
|
1233
|
-
return "stop";
|
|
1234
|
-
case "length":
|
|
1235
|
-
return "length";
|
|
1236
|
-
case "tool_calls":
|
|
1237
|
-
case "function_call":
|
|
1238
|
-
return "tool-calls";
|
|
1239
|
-
case "content_filter":
|
|
1240
|
-
return "content-filter";
|
|
1241
|
-
default:
|
|
1242
|
-
return "other";
|
|
1243
|
-
}
|
|
1244
|
-
}
|
|
1245
|
-
|
|
1246
|
-
// src/responses/xai-responses-api.ts
|
|
1247
|
-
import { z as z6 } from "zod/v4";
|
|
1248
|
-
var annotationSchema = z6.union([
|
|
1249
|
-
z6.object({
|
|
1250
|
-
type: z6.literal("url_citation"),
|
|
1251
|
-
url: z6.string(),
|
|
1252
|
-
title: z6.string().optional()
|
|
1253
|
-
}),
|
|
1254
|
-
z6.object({
|
|
1255
|
-
type: z6.string()
|
|
1256
|
-
})
|
|
1257
|
-
]);
|
|
1258
|
-
var messageContentPartSchema = z6.object({
|
|
1259
|
-
type: z6.string(),
|
|
1260
|
-
text: z6.string().optional(),
|
|
1261
|
-
logprobs: z6.array(z6.any()).optional(),
|
|
1262
|
-
annotations: z6.array(annotationSchema).optional()
|
|
1263
|
-
});
|
|
1264
|
-
var reasoningSummaryPartSchema = z6.object({
|
|
1265
|
-
type: z6.string(),
|
|
1266
|
-
text: z6.string()
|
|
1267
|
-
});
|
|
1268
|
-
var toolCallSchema = z6.object({
|
|
1269
|
-
name: z6.string().optional(),
|
|
1270
|
-
arguments: z6.string().optional(),
|
|
1271
|
-
input: z6.string().optional(),
|
|
1272
|
-
call_id: z6.string().optional(),
|
|
1273
|
-
id: z6.string(),
|
|
1274
|
-
status: z6.string(),
|
|
1275
|
-
action: z6.any().optional()
|
|
1276
|
-
});
|
|
1277
|
-
var mcpCallSchema = z6.object({
|
|
1278
|
-
name: z6.string().optional(),
|
|
1279
|
-
arguments: z6.string().optional(),
|
|
1280
|
-
output: z6.string().optional(),
|
|
1281
|
-
error: z6.string().optional(),
|
|
1282
|
-
id: z6.string(),
|
|
1283
|
-
status: z6.string(),
|
|
1284
|
-
server_label: z6.string().optional()
|
|
1285
|
-
});
|
|
1286
|
-
var outputItemSchema = z6.discriminatedUnion("type", [
|
|
1287
|
-
z6.object({
|
|
1288
|
-
type: z6.literal("web_search_call"),
|
|
1289
|
-
...toolCallSchema.shape
|
|
1290
|
-
}),
|
|
1291
|
-
z6.object({
|
|
1292
|
-
type: z6.literal("x_search_call"),
|
|
1293
|
-
...toolCallSchema.shape
|
|
1294
|
-
}),
|
|
1295
|
-
z6.object({
|
|
1296
|
-
type: z6.literal("code_interpreter_call"),
|
|
1297
|
-
...toolCallSchema.shape
|
|
1298
|
-
}),
|
|
1299
|
-
z6.object({
|
|
1300
|
-
type: z6.literal("code_execution_call"),
|
|
1301
|
-
...toolCallSchema.shape
|
|
1302
|
-
}),
|
|
1303
|
-
z6.object({
|
|
1304
|
-
type: z6.literal("view_image_call"),
|
|
1305
|
-
...toolCallSchema.shape
|
|
1306
|
-
}),
|
|
1307
|
-
z6.object({
|
|
1308
|
-
type: z6.literal("view_x_video_call"),
|
|
1309
|
-
...toolCallSchema.shape
|
|
1310
|
-
}),
|
|
1311
|
-
z6.object({
|
|
1312
|
-
type: z6.literal("file_search_call"),
|
|
1313
|
-
id: z6.string(),
|
|
1314
|
-
status: z6.string(),
|
|
1315
|
-
queries: z6.array(z6.string()).optional(),
|
|
1316
|
-
results: z6.array(
|
|
1317
|
-
z6.object({
|
|
1318
|
-
file_id: z6.string(),
|
|
1319
|
-
filename: z6.string(),
|
|
1320
|
-
score: z6.number(),
|
|
1321
|
-
text: z6.string()
|
|
1322
|
-
})
|
|
1323
|
-
).nullish()
|
|
1324
|
-
}),
|
|
1325
|
-
z6.object({
|
|
1326
|
-
type: z6.literal("custom_tool_call"),
|
|
1327
|
-
...toolCallSchema.shape
|
|
1328
|
-
}),
|
|
1329
|
-
z6.object({
|
|
1330
|
-
type: z6.literal("mcp_call"),
|
|
1331
|
-
...mcpCallSchema.shape
|
|
1332
|
-
}),
|
|
1333
|
-
z6.object({
|
|
1334
|
-
type: z6.literal("message"),
|
|
1335
|
-
role: z6.string(),
|
|
1336
|
-
content: z6.array(messageContentPartSchema),
|
|
1337
|
-
id: z6.string(),
|
|
1338
|
-
status: z6.string()
|
|
1339
|
-
}),
|
|
1340
|
-
z6.object({
|
|
1341
|
-
type: z6.literal("function_call"),
|
|
1342
|
-
name: z6.string(),
|
|
1343
|
-
arguments: z6.string(),
|
|
1344
|
-
call_id: z6.string(),
|
|
1345
|
-
id: z6.string()
|
|
1346
|
-
}),
|
|
1347
|
-
z6.object({
|
|
1348
|
-
type: z6.literal("reasoning"),
|
|
1349
|
-
id: z6.string(),
|
|
1350
|
-
summary: z6.array(reasoningSummaryPartSchema),
|
|
1351
|
-
status: z6.string(),
|
|
1352
|
-
encrypted_content: z6.string().nullish()
|
|
1353
|
-
})
|
|
1354
|
-
]);
|
|
1355
|
-
var xaiResponsesUsageSchema = z6.object({
|
|
1356
|
-
input_tokens: z6.number(),
|
|
1357
|
-
output_tokens: z6.number(),
|
|
1358
|
-
total_tokens: z6.number().optional(),
|
|
1359
|
-
input_tokens_details: z6.object({
|
|
1360
|
-
cached_tokens: z6.number().optional()
|
|
1361
|
-
}).optional(),
|
|
1362
|
-
output_tokens_details: z6.object({
|
|
1363
|
-
reasoning_tokens: z6.number().optional()
|
|
1364
|
-
}).optional(),
|
|
1365
|
-
num_sources_used: z6.number().optional(),
|
|
1366
|
-
num_server_side_tools_used: z6.number().optional()
|
|
1367
|
-
});
|
|
1368
|
-
var xaiResponsesResponseSchema = z6.object({
|
|
1369
|
-
id: z6.string().nullish(),
|
|
1370
|
-
created_at: z6.number().nullish(),
|
|
1371
|
-
model: z6.string().nullish(),
|
|
1372
|
-
object: z6.literal("response"),
|
|
1373
|
-
output: z6.array(outputItemSchema),
|
|
1374
|
-
usage: xaiResponsesUsageSchema.nullish(),
|
|
1375
|
-
status: z6.string()
|
|
1376
|
-
});
|
|
1377
|
-
var xaiResponsesChunkSchema = z6.union([
|
|
1378
|
-
z6.object({
|
|
1379
|
-
type: z6.literal("response.created"),
|
|
1380
|
-
response: xaiResponsesResponseSchema.partial({ usage: true, status: true })
|
|
1381
|
-
}),
|
|
1382
|
-
z6.object({
|
|
1383
|
-
type: z6.literal("response.in_progress"),
|
|
1384
|
-
response: xaiResponsesResponseSchema.partial({ usage: true, status: true })
|
|
1385
|
-
}),
|
|
1386
|
-
z6.object({
|
|
1387
|
-
type: z6.literal("response.output_item.added"),
|
|
1388
|
-
item: outputItemSchema,
|
|
1389
|
-
output_index: z6.number()
|
|
1390
|
-
}),
|
|
1391
|
-
z6.object({
|
|
1392
|
-
type: z6.literal("response.output_item.done"),
|
|
1393
|
-
item: outputItemSchema,
|
|
1394
|
-
output_index: z6.number()
|
|
1395
|
-
}),
|
|
1396
|
-
z6.object({
|
|
1397
|
-
type: z6.literal("response.content_part.added"),
|
|
1398
|
-
item_id: z6.string(),
|
|
1399
|
-
output_index: z6.number(),
|
|
1400
|
-
content_index: z6.number(),
|
|
1401
|
-
part: messageContentPartSchema
|
|
1402
|
-
}),
|
|
1403
|
-
z6.object({
|
|
1404
|
-
type: z6.literal("response.content_part.done"),
|
|
1405
|
-
item_id: z6.string(),
|
|
1406
|
-
output_index: z6.number(),
|
|
1407
|
-
content_index: z6.number(),
|
|
1408
|
-
part: messageContentPartSchema
|
|
1409
|
-
}),
|
|
1410
|
-
z6.object({
|
|
1411
|
-
type: z6.literal("response.output_text.delta"),
|
|
1412
|
-
item_id: z6.string(),
|
|
1413
|
-
output_index: z6.number(),
|
|
1414
|
-
content_index: z6.number(),
|
|
1415
|
-
delta: z6.string(),
|
|
1416
|
-
logprobs: z6.array(z6.any()).optional()
|
|
1417
|
-
}),
|
|
1418
|
-
z6.object({
|
|
1419
|
-
type: z6.literal("response.output_text.done"),
|
|
1420
|
-
item_id: z6.string(),
|
|
1421
|
-
output_index: z6.number(),
|
|
1422
|
-
content_index: z6.number(),
|
|
1423
|
-
text: z6.string(),
|
|
1424
|
-
logprobs: z6.array(z6.any()).optional(),
|
|
1425
|
-
annotations: z6.array(annotationSchema).optional()
|
|
1426
|
-
}),
|
|
1427
|
-
z6.object({
|
|
1428
|
-
type: z6.literal("response.output_text.annotation.added"),
|
|
1429
|
-
item_id: z6.string(),
|
|
1430
|
-
output_index: z6.number(),
|
|
1431
|
-
content_index: z6.number(),
|
|
1432
|
-
annotation_index: z6.number(),
|
|
1433
|
-
annotation: annotationSchema
|
|
1434
|
-
}),
|
|
1435
|
-
z6.object({
|
|
1436
|
-
type: z6.literal("response.reasoning_summary_part.added"),
|
|
1437
|
-
item_id: z6.string(),
|
|
1438
|
-
output_index: z6.number(),
|
|
1439
|
-
summary_index: z6.number(),
|
|
1440
|
-
part: reasoningSummaryPartSchema
|
|
1441
|
-
}),
|
|
1442
|
-
z6.object({
|
|
1443
|
-
type: z6.literal("response.reasoning_summary_part.done"),
|
|
1444
|
-
item_id: z6.string(),
|
|
1445
|
-
output_index: z6.number(),
|
|
1446
|
-
summary_index: z6.number(),
|
|
1447
|
-
part: reasoningSummaryPartSchema
|
|
1448
|
-
}),
|
|
1449
|
-
z6.object({
|
|
1450
|
-
type: z6.literal("response.reasoning_summary_text.delta"),
|
|
1451
|
-
item_id: z6.string(),
|
|
1452
|
-
output_index: z6.number(),
|
|
1453
|
-
summary_index: z6.number(),
|
|
1454
|
-
delta: z6.string()
|
|
1455
|
-
}),
|
|
1456
|
-
z6.object({
|
|
1457
|
-
type: z6.literal("response.reasoning_summary_text.done"),
|
|
1458
|
-
item_id: z6.string(),
|
|
1459
|
-
output_index: z6.number(),
|
|
1460
|
-
summary_index: z6.number(),
|
|
1461
|
-
text: z6.string()
|
|
1462
|
-
}),
|
|
1463
|
-
z6.object({
|
|
1464
|
-
type: z6.literal("response.reasoning_text.delta"),
|
|
1465
|
-
item_id: z6.string(),
|
|
1466
|
-
output_index: z6.number(),
|
|
1467
|
-
content_index: z6.number(),
|
|
1468
|
-
delta: z6.string()
|
|
1469
|
-
}),
|
|
1470
|
-
z6.object({
|
|
1471
|
-
type: z6.literal("response.reasoning_text.done"),
|
|
1472
|
-
item_id: z6.string(),
|
|
1473
|
-
output_index: z6.number(),
|
|
1474
|
-
content_index: z6.number(),
|
|
1475
|
-
text: z6.string()
|
|
1476
|
-
}),
|
|
1477
|
-
z6.object({
|
|
1478
|
-
type: z6.literal("response.web_search_call.in_progress"),
|
|
1479
|
-
item_id: z6.string(),
|
|
1480
|
-
output_index: z6.number()
|
|
1481
|
-
}),
|
|
1482
|
-
z6.object({
|
|
1483
|
-
type: z6.literal("response.web_search_call.searching"),
|
|
1484
|
-
item_id: z6.string(),
|
|
1485
|
-
output_index: z6.number()
|
|
1486
|
-
}),
|
|
1487
|
-
z6.object({
|
|
1488
|
-
type: z6.literal("response.web_search_call.completed"),
|
|
1489
|
-
item_id: z6.string(),
|
|
1490
|
-
output_index: z6.number()
|
|
1491
|
-
}),
|
|
1492
|
-
z6.object({
|
|
1493
|
-
type: z6.literal("response.x_search_call.in_progress"),
|
|
1494
|
-
item_id: z6.string(),
|
|
1495
|
-
output_index: z6.number()
|
|
1496
|
-
}),
|
|
1497
|
-
z6.object({
|
|
1498
|
-
type: z6.literal("response.x_search_call.searching"),
|
|
1499
|
-
item_id: z6.string(),
|
|
1500
|
-
output_index: z6.number()
|
|
1501
|
-
}),
|
|
1502
|
-
z6.object({
|
|
1503
|
-
type: z6.literal("response.x_search_call.completed"),
|
|
1504
|
-
item_id: z6.string(),
|
|
1505
|
-
output_index: z6.number()
|
|
1506
|
-
}),
|
|
1507
|
-
z6.object({
|
|
1508
|
-
type: z6.literal("response.file_search_call.in_progress"),
|
|
1509
|
-
item_id: z6.string(),
|
|
1510
|
-
output_index: z6.number()
|
|
1511
|
-
}),
|
|
1512
|
-
z6.object({
|
|
1513
|
-
type: z6.literal("response.file_search_call.searching"),
|
|
1514
|
-
item_id: z6.string(),
|
|
1515
|
-
output_index: z6.number()
|
|
1516
|
-
}),
|
|
1517
|
-
z6.object({
|
|
1518
|
-
type: z6.literal("response.file_search_call.completed"),
|
|
1519
|
-
item_id: z6.string(),
|
|
1520
|
-
output_index: z6.number()
|
|
1521
|
-
}),
|
|
1522
|
-
z6.object({
|
|
1523
|
-
type: z6.literal("response.code_execution_call.in_progress"),
|
|
1524
|
-
item_id: z6.string(),
|
|
1525
|
-
output_index: z6.number()
|
|
1526
|
-
}),
|
|
1527
|
-
z6.object({
|
|
1528
|
-
type: z6.literal("response.code_execution_call.executing"),
|
|
1529
|
-
item_id: z6.string(),
|
|
1530
|
-
output_index: z6.number()
|
|
1531
|
-
}),
|
|
1532
|
-
z6.object({
|
|
1533
|
-
type: z6.literal("response.code_execution_call.completed"),
|
|
1534
|
-
item_id: z6.string(),
|
|
1535
|
-
output_index: z6.number()
|
|
1536
|
-
}),
|
|
1537
|
-
z6.object({
|
|
1538
|
-
type: z6.literal("response.code_interpreter_call.in_progress"),
|
|
1539
|
-
item_id: z6.string(),
|
|
1540
|
-
output_index: z6.number()
|
|
1541
|
-
}),
|
|
1542
|
-
z6.object({
|
|
1543
|
-
type: z6.literal("response.code_interpreter_call.executing"),
|
|
1544
|
-
item_id: z6.string(),
|
|
1545
|
-
output_index: z6.number()
|
|
1546
|
-
}),
|
|
1547
|
-
z6.object({
|
|
1548
|
-
type: z6.literal("response.code_interpreter_call.interpreting"),
|
|
1549
|
-
item_id: z6.string(),
|
|
1550
|
-
output_index: z6.number()
|
|
1551
|
-
}),
|
|
1552
|
-
z6.object({
|
|
1553
|
-
type: z6.literal("response.code_interpreter_call.completed"),
|
|
1554
|
-
item_id: z6.string(),
|
|
1555
|
-
output_index: z6.number()
|
|
1556
|
-
}),
|
|
1557
|
-
// Code interpreter code streaming events
|
|
1558
|
-
z6.object({
|
|
1559
|
-
type: z6.literal("response.code_interpreter_call_code.delta"),
|
|
1560
|
-
item_id: z6.string(),
|
|
1561
|
-
output_index: z6.number(),
|
|
1562
|
-
delta: z6.string()
|
|
1563
|
-
}),
|
|
1564
|
-
z6.object({
|
|
1565
|
-
type: z6.literal("response.code_interpreter_call_code.done"),
|
|
1566
|
-
item_id: z6.string(),
|
|
1567
|
-
output_index: z6.number(),
|
|
1568
|
-
code: z6.string()
|
|
1569
|
-
}),
|
|
1570
|
-
z6.object({
|
|
1571
|
-
type: z6.literal("response.custom_tool_call_input.delta"),
|
|
1572
|
-
item_id: z6.string(),
|
|
1573
|
-
output_index: z6.number(),
|
|
1574
|
-
delta: z6.string()
|
|
1575
|
-
}),
|
|
1576
|
-
z6.object({
|
|
1577
|
-
type: z6.literal("response.custom_tool_call_input.done"),
|
|
1578
|
-
item_id: z6.string(),
|
|
1579
|
-
output_index: z6.number(),
|
|
1580
|
-
input: z6.string()
|
|
1581
|
-
}),
|
|
1582
|
-
// Function call arguments streaming events (standard function tools)
|
|
1583
|
-
z6.object({
|
|
1584
|
-
type: z6.literal("response.function_call_arguments.delta"),
|
|
1585
|
-
item_id: z6.string(),
|
|
1586
|
-
output_index: z6.number(),
|
|
1587
|
-
delta: z6.string()
|
|
1588
|
-
}),
|
|
1589
|
-
z6.object({
|
|
1590
|
-
type: z6.literal("response.function_call_arguments.done"),
|
|
1591
|
-
item_id: z6.string(),
|
|
1592
|
-
output_index: z6.number(),
|
|
1593
|
-
arguments: z6.string()
|
|
1594
|
-
}),
|
|
1595
|
-
z6.object({
|
|
1596
|
-
type: z6.literal("response.mcp_call.in_progress"),
|
|
1597
|
-
item_id: z6.string(),
|
|
1598
|
-
output_index: z6.number()
|
|
1599
|
-
}),
|
|
1600
|
-
z6.object({
|
|
1601
|
-
type: z6.literal("response.mcp_call.executing"),
|
|
1602
|
-
item_id: z6.string(),
|
|
1603
|
-
output_index: z6.number()
|
|
1604
|
-
}),
|
|
1605
|
-
z6.object({
|
|
1606
|
-
type: z6.literal("response.mcp_call.completed"),
|
|
1607
|
-
item_id: z6.string(),
|
|
1608
|
-
output_index: z6.number()
|
|
1609
|
-
}),
|
|
1610
|
-
z6.object({
|
|
1611
|
-
type: z6.literal("response.mcp_call.failed"),
|
|
1612
|
-
item_id: z6.string(),
|
|
1613
|
-
output_index: z6.number()
|
|
1614
|
-
}),
|
|
1615
|
-
z6.object({
|
|
1616
|
-
type: z6.literal("response.mcp_call_arguments.delta"),
|
|
1617
|
-
item_id: z6.string(),
|
|
1618
|
-
output_index: z6.number(),
|
|
1619
|
-
delta: z6.string()
|
|
1620
|
-
}),
|
|
1621
|
-
z6.object({
|
|
1622
|
-
type: z6.literal("response.mcp_call_arguments.done"),
|
|
1623
|
-
item_id: z6.string(),
|
|
1624
|
-
output_index: z6.number(),
|
|
1625
|
-
arguments: z6.string().optional()
|
|
1626
|
-
}),
|
|
1627
|
-
z6.object({
|
|
1628
|
-
type: z6.literal("response.mcp_call_output.delta"),
|
|
1629
|
-
item_id: z6.string(),
|
|
1630
|
-
output_index: z6.number(),
|
|
1631
|
-
delta: z6.string()
|
|
1632
|
-
}),
|
|
1633
|
-
z6.object({
|
|
1634
|
-
type: z6.literal("response.mcp_call_output.done"),
|
|
1635
|
-
item_id: z6.string(),
|
|
1636
|
-
output_index: z6.number(),
|
|
1637
|
-
output: z6.string().optional()
|
|
1638
|
-
}),
|
|
1639
|
-
z6.object({
|
|
1640
|
-
type: z6.literal("response.done"),
|
|
1641
|
-
response: xaiResponsesResponseSchema
|
|
1642
|
-
}),
|
|
1643
|
-
z6.object({
|
|
1644
|
-
type: z6.literal("response.completed"),
|
|
1645
|
-
response: xaiResponsesResponseSchema
|
|
1646
|
-
})
|
|
1647
|
-
]);
|
|
1648
|
-
|
|
1649
|
-
// src/responses/xai-responses-options.ts
|
|
1650
|
-
import { z as z7 } from "zod/v4";
|
|
1651
|
-
var xaiLanguageModelResponsesOptions = z7.object({
|
|
1652
|
-
/**
|
|
1653
|
-
* Constrains how hard a reasoning model thinks before responding.
|
|
1654
|
-
* Possible values are `low` (uses fewer reasoning tokens), `medium` and `high` (uses more reasoning tokens).
|
|
1655
|
-
*/
|
|
1656
|
-
reasoningEffort: z7.enum(["low", "medium", "high"]).optional(),
|
|
1657
|
-
logprobs: z7.boolean().optional(),
|
|
1658
|
-
topLogprobs: z7.number().int().min(0).max(8).optional(),
|
|
1659
|
-
/**
|
|
1660
|
-
* Whether to store the input message(s) and model response for later retrieval.
|
|
1661
|
-
* @default true
|
|
1662
|
-
*/
|
|
1663
|
-
store: z7.boolean().optional(),
|
|
1664
|
-
/**
|
|
1665
|
-
* The ID of the previous response from the model.
|
|
1666
|
-
*/
|
|
1667
|
-
previousResponseId: z7.string().optional(),
|
|
1668
|
-
/**
|
|
1669
|
-
* Specify additional output data to include in the model response.
|
|
1670
|
-
* Example values: 'file_search_call.results'.
|
|
1671
|
-
*/
|
|
1672
|
-
include: z7.array(z7.enum(["file_search_call.results"])).nullish()
|
|
1673
|
-
});
|
|
1674
|
-
|
|
1675
|
-
// src/responses/xai-responses-prepare-tools.ts
|
|
1676
|
-
import {
|
|
1677
|
-
UnsupportedFunctionalityError as UnsupportedFunctionalityError4
|
|
1678
|
-
} from "@ai-sdk/provider";
|
|
1679
|
-
import { validateTypes } from "@ai-sdk/provider-utils";
|
|
1680
|
-
|
|
1681
|
-
// src/tool/file-search.ts
|
|
1682
|
-
import {
|
|
1683
|
-
createProviderToolFactoryWithOutputSchema,
|
|
1684
|
-
lazySchema,
|
|
1685
|
-
zodSchema
|
|
1686
|
-
} from "@ai-sdk/provider-utils";
|
|
1687
|
-
import { z as z8 } from "zod/v4";
|
|
1688
|
-
var fileSearchArgsSchema = lazySchema(
|
|
1689
|
-
() => zodSchema(
|
|
1690
|
-
z8.object({
|
|
1691
|
-
vectorStoreIds: z8.array(z8.string()),
|
|
1692
|
-
maxNumResults: z8.number().optional()
|
|
1693
|
-
})
|
|
1694
|
-
)
|
|
1695
|
-
);
|
|
1696
|
-
var fileSearchOutputSchema = lazySchema(
|
|
1697
|
-
() => zodSchema(
|
|
1698
|
-
z8.object({
|
|
1699
|
-
queries: z8.array(z8.string()),
|
|
1700
|
-
results: z8.array(
|
|
1701
|
-
z8.object({
|
|
1702
|
-
fileId: z8.string(),
|
|
1703
|
-
filename: z8.string(),
|
|
1704
|
-
score: z8.number().min(0).max(1),
|
|
1705
|
-
text: z8.string()
|
|
1706
|
-
})
|
|
1707
|
-
).nullable()
|
|
1708
|
-
})
|
|
1709
|
-
)
|
|
1710
|
-
);
|
|
1711
|
-
var fileSearchToolFactory = createProviderToolFactoryWithOutputSchema({
|
|
1712
|
-
id: "xai.file_search",
|
|
1713
|
-
inputSchema: lazySchema(() => zodSchema(z8.object({}))),
|
|
1714
|
-
outputSchema: fileSearchOutputSchema
|
|
1715
|
-
});
|
|
1716
|
-
var fileSearch = (args) => fileSearchToolFactory(args);
|
|
1717
|
-
|
|
1718
|
-
// src/tool/mcp-server.ts
|
|
1719
|
-
import {
|
|
1720
|
-
createProviderToolFactoryWithOutputSchema as createProviderToolFactoryWithOutputSchema2,
|
|
1721
|
-
lazySchema as lazySchema2,
|
|
1722
|
-
zodSchema as zodSchema2
|
|
1723
|
-
} from "@ai-sdk/provider-utils";
|
|
1724
|
-
import { z as z9 } from "zod/v4";
|
|
1725
|
-
var mcpServerArgsSchema = lazySchema2(
|
|
1726
|
-
() => zodSchema2(
|
|
1727
|
-
z9.object({
|
|
1728
|
-
serverUrl: z9.string().describe("The URL of the MCP server"),
|
|
1729
|
-
serverLabel: z9.string().optional().describe("A label for the MCP server"),
|
|
1730
|
-
serverDescription: z9.string().optional().describe("Description of the MCP server"),
|
|
1731
|
-
allowedTools: z9.array(z9.string()).optional().describe("List of allowed tool names"),
|
|
1732
|
-
headers: z9.record(z9.string(), z9.string()).optional().describe("Custom headers to send"),
|
|
1733
|
-
authorization: z9.string().optional().describe("Authorization header value")
|
|
1734
|
-
})
|
|
1735
|
-
)
|
|
1736
|
-
);
|
|
1737
|
-
var mcpServerOutputSchema = lazySchema2(
|
|
1738
|
-
() => zodSchema2(
|
|
1739
|
-
z9.object({
|
|
1740
|
-
name: z9.string(),
|
|
1741
|
-
arguments: z9.string(),
|
|
1742
|
-
result: z9.unknown()
|
|
1743
|
-
})
|
|
1744
|
-
)
|
|
1745
|
-
);
|
|
1746
|
-
var mcpServerToolFactory = createProviderToolFactoryWithOutputSchema2({
|
|
1747
|
-
id: "xai.mcp",
|
|
1748
|
-
inputSchema: lazySchema2(() => zodSchema2(z9.object({}))),
|
|
1749
|
-
outputSchema: mcpServerOutputSchema
|
|
1750
|
-
});
|
|
1751
|
-
var mcpServer = (args) => mcpServerToolFactory(args);
|
|
1752
|
-
|
|
1753
|
-
// src/tool/web-search.ts
|
|
1754
|
-
import {
|
|
1755
|
-
createProviderToolFactoryWithOutputSchema as createProviderToolFactoryWithOutputSchema3,
|
|
1756
|
-
lazySchema as lazySchema3,
|
|
1757
|
-
zodSchema as zodSchema3
|
|
1758
|
-
} from "@ai-sdk/provider-utils";
|
|
1759
|
-
import { z as z10 } from "zod/v4";
|
|
1760
|
-
var webSearchArgsSchema = lazySchema3(
|
|
1761
|
-
() => zodSchema3(
|
|
1762
|
-
z10.object({
|
|
1763
|
-
allowedDomains: z10.array(z10.string()).max(5).optional(),
|
|
1764
|
-
excludedDomains: z10.array(z10.string()).max(5).optional(),
|
|
1765
|
-
enableImageUnderstanding: z10.boolean().optional()
|
|
1766
|
-
})
|
|
1767
|
-
)
|
|
1768
|
-
);
|
|
1769
|
-
var webSearchOutputSchema = lazySchema3(
|
|
1770
|
-
() => zodSchema3(
|
|
1771
|
-
z10.object({
|
|
1772
|
-
query: z10.string(),
|
|
1773
|
-
sources: z10.array(
|
|
1774
|
-
z10.object({
|
|
1775
|
-
title: z10.string(),
|
|
1776
|
-
url: z10.string(),
|
|
1777
|
-
snippet: z10.string()
|
|
1778
|
-
})
|
|
1779
|
-
)
|
|
1780
|
-
})
|
|
1781
|
-
)
|
|
1782
|
-
);
|
|
1783
|
-
var webSearchToolFactory = createProviderToolFactoryWithOutputSchema3({
|
|
1784
|
-
id: "xai.web_search",
|
|
1785
|
-
inputSchema: lazySchema3(() => zodSchema3(z10.object({}))),
|
|
1786
|
-
outputSchema: webSearchOutputSchema
|
|
1787
|
-
});
|
|
1788
|
-
var webSearch = (args = {}) => webSearchToolFactory(args);
|
|
1789
|
-
|
|
1790
|
-
// src/tool/x-search.ts
|
|
1791
|
-
import {
|
|
1792
|
-
createProviderToolFactoryWithOutputSchema as createProviderToolFactoryWithOutputSchema4,
|
|
1793
|
-
lazySchema as lazySchema4,
|
|
1794
|
-
zodSchema as zodSchema4
|
|
1795
|
-
} from "@ai-sdk/provider-utils";
|
|
1796
|
-
import { z as z11 } from "zod/v4";
|
|
1797
|
-
var xSearchArgsSchema = lazySchema4(
|
|
1798
|
-
() => zodSchema4(
|
|
1799
|
-
z11.object({
|
|
1800
|
-
allowedXHandles: z11.array(z11.string()).max(10).optional(),
|
|
1801
|
-
excludedXHandles: z11.array(z11.string()).max(10).optional(),
|
|
1802
|
-
fromDate: z11.string().optional(),
|
|
1803
|
-
toDate: z11.string().optional(),
|
|
1804
|
-
enableImageUnderstanding: z11.boolean().optional(),
|
|
1805
|
-
enableVideoUnderstanding: z11.boolean().optional()
|
|
1806
|
-
})
|
|
1807
|
-
)
|
|
1808
|
-
);
|
|
1809
|
-
var xSearchOutputSchema = lazySchema4(
|
|
1810
|
-
() => zodSchema4(
|
|
1811
|
-
z11.object({
|
|
1812
|
-
query: z11.string(),
|
|
1813
|
-
posts: z11.array(
|
|
1814
|
-
z11.object({
|
|
1815
|
-
author: z11.string(),
|
|
1816
|
-
text: z11.string(),
|
|
1817
|
-
url: z11.string(),
|
|
1818
|
-
likes: z11.number()
|
|
1819
|
-
})
|
|
1820
|
-
)
|
|
1821
|
-
})
|
|
1822
|
-
)
|
|
1823
|
-
);
|
|
1824
|
-
var xSearchToolFactory = createProviderToolFactoryWithOutputSchema4({
|
|
1825
|
-
id: "xai.x_search",
|
|
1826
|
-
inputSchema: lazySchema4(() => zodSchema4(z11.object({}))),
|
|
1827
|
-
outputSchema: xSearchOutputSchema
|
|
1828
|
-
});
|
|
1829
|
-
var xSearch = (args = {}) => xSearchToolFactory(args);
|
|
1830
|
-
|
|
1831
|
-
// src/responses/xai-responses-prepare-tools.ts
|
|
1832
|
-
async function prepareResponsesTools({
|
|
1833
|
-
tools,
|
|
1834
|
-
toolChoice
|
|
1835
|
-
}) {
|
|
1836
|
-
const normalizedTools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
1837
|
-
const toolWarnings = [];
|
|
1838
|
-
if (normalizedTools == null) {
|
|
1839
|
-
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
1840
|
-
}
|
|
1841
|
-
const xaiTools2 = [];
|
|
1842
|
-
const toolByName = /* @__PURE__ */ new Map();
|
|
1843
|
-
for (const tool of normalizedTools) {
|
|
1844
|
-
toolByName.set(tool.name, tool);
|
|
1845
|
-
if (tool.type === "provider") {
|
|
1846
|
-
switch (tool.id) {
|
|
1847
|
-
case "xai.web_search": {
|
|
1848
|
-
const args = await validateTypes({
|
|
1849
|
-
value: tool.args,
|
|
1850
|
-
schema: webSearchArgsSchema
|
|
1851
|
-
});
|
|
1852
|
-
xaiTools2.push({
|
|
1853
|
-
type: "web_search",
|
|
1854
|
-
allowed_domains: args.allowedDomains,
|
|
1855
|
-
excluded_domains: args.excludedDomains,
|
|
1856
|
-
enable_image_understanding: args.enableImageUnderstanding
|
|
1857
|
-
});
|
|
1858
|
-
break;
|
|
1859
|
-
}
|
|
1860
|
-
case "xai.x_search": {
|
|
1861
|
-
const args = await validateTypes({
|
|
1862
|
-
value: tool.args,
|
|
1863
|
-
schema: xSearchArgsSchema
|
|
1864
|
-
});
|
|
1865
|
-
xaiTools2.push({
|
|
1866
|
-
type: "x_search",
|
|
1867
|
-
allowed_x_handles: args.allowedXHandles,
|
|
1868
|
-
excluded_x_handles: args.excludedXHandles,
|
|
1869
|
-
from_date: args.fromDate,
|
|
1870
|
-
to_date: args.toDate,
|
|
1871
|
-
enable_image_understanding: args.enableImageUnderstanding,
|
|
1872
|
-
enable_video_understanding: args.enableVideoUnderstanding
|
|
1873
|
-
});
|
|
1874
|
-
break;
|
|
1875
|
-
}
|
|
1876
|
-
case "xai.code_execution": {
|
|
1877
|
-
xaiTools2.push({
|
|
1878
|
-
type: "code_interpreter"
|
|
1879
|
-
});
|
|
1880
|
-
break;
|
|
1881
|
-
}
|
|
1882
|
-
case "xai.view_image": {
|
|
1883
|
-
xaiTools2.push({
|
|
1884
|
-
type: "view_image"
|
|
1885
|
-
});
|
|
1886
|
-
break;
|
|
1887
|
-
}
|
|
1888
|
-
case "xai.view_x_video": {
|
|
1889
|
-
xaiTools2.push({
|
|
1890
|
-
type: "view_x_video"
|
|
1891
|
-
});
|
|
1892
|
-
break;
|
|
1893
|
-
}
|
|
1894
|
-
case "xai.file_search": {
|
|
1895
|
-
const args = await validateTypes({
|
|
1896
|
-
value: tool.args,
|
|
1897
|
-
schema: fileSearchArgsSchema
|
|
1898
|
-
});
|
|
1899
|
-
xaiTools2.push({
|
|
1900
|
-
type: "file_search",
|
|
1901
|
-
vector_store_ids: args.vectorStoreIds,
|
|
1902
|
-
max_num_results: args.maxNumResults
|
|
1903
|
-
});
|
|
1904
|
-
break;
|
|
1905
|
-
}
|
|
1906
|
-
case "xai.mcp": {
|
|
1907
|
-
const args = await validateTypes({
|
|
1908
|
-
value: tool.args,
|
|
1909
|
-
schema: mcpServerArgsSchema
|
|
1910
|
-
});
|
|
1911
|
-
xaiTools2.push({
|
|
1912
|
-
type: "mcp",
|
|
1913
|
-
server_url: args.serverUrl,
|
|
1914
|
-
server_label: args.serverLabel,
|
|
1915
|
-
server_description: args.serverDescription,
|
|
1916
|
-
allowed_tools: args.allowedTools,
|
|
1917
|
-
headers: args.headers,
|
|
1918
|
-
authorization: args.authorization
|
|
1919
|
-
});
|
|
1920
|
-
break;
|
|
1921
|
-
}
|
|
1922
|
-
default: {
|
|
1923
|
-
toolWarnings.push({
|
|
1924
|
-
type: "unsupported",
|
|
1925
|
-
feature: `provider-defined tool ${tool.name}`
|
|
1926
|
-
});
|
|
1927
|
-
break;
|
|
1928
|
-
}
|
|
1929
|
-
}
|
|
1930
|
-
} else {
|
|
1931
|
-
xaiTools2.push({
|
|
1932
|
-
type: "function",
|
|
1933
|
-
name: tool.name,
|
|
1934
|
-
description: tool.description,
|
|
1935
|
-
parameters: tool.inputSchema,
|
|
1936
|
-
...tool.strict != null ? { strict: tool.strict } : {}
|
|
1937
|
-
});
|
|
1938
|
-
}
|
|
1939
|
-
}
|
|
1940
|
-
if (toolChoice == null) {
|
|
1941
|
-
return { tools: xaiTools2, toolChoice: void 0, toolWarnings };
|
|
1942
|
-
}
|
|
1943
|
-
const type = toolChoice.type;
|
|
1944
|
-
switch (type) {
|
|
1945
|
-
case "auto":
|
|
1946
|
-
case "none":
|
|
1947
|
-
return { tools: xaiTools2, toolChoice: type, toolWarnings };
|
|
1948
|
-
case "required":
|
|
1949
|
-
return { tools: xaiTools2, toolChoice: "required", toolWarnings };
|
|
1950
|
-
case "tool": {
|
|
1951
|
-
const selectedTool = toolByName.get(toolChoice.toolName);
|
|
1952
|
-
if (selectedTool == null) {
|
|
1953
|
-
return {
|
|
1954
|
-
tools: xaiTools2,
|
|
1955
|
-
toolChoice: void 0,
|
|
1956
|
-
toolWarnings
|
|
1957
|
-
};
|
|
1958
|
-
}
|
|
1959
|
-
if (selectedTool.type === "provider") {
|
|
1960
|
-
toolWarnings.push({
|
|
1961
|
-
type: "unsupported",
|
|
1962
|
-
feature: `toolChoice for server-side tool "${selectedTool.name}"`
|
|
1963
|
-
});
|
|
1964
|
-
return { tools: xaiTools2, toolChoice: void 0, toolWarnings };
|
|
1965
|
-
}
|
|
1966
|
-
return {
|
|
1967
|
-
tools: xaiTools2,
|
|
1968
|
-
toolChoice: { type: "function", name: selectedTool.name },
|
|
1969
|
-
toolWarnings
|
|
1970
|
-
};
|
|
1971
|
-
}
|
|
1972
|
-
default: {
|
|
1973
|
-
const _exhaustiveCheck = type;
|
|
1974
|
-
throw new UnsupportedFunctionalityError4({
|
|
1975
|
-
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
1976
|
-
});
|
|
1977
|
-
}
|
|
1978
|
-
}
|
|
1979
|
-
}
|
|
1980
|
-
|
|
1981
|
-
// src/responses/xai-responses-language-model.ts
|
|
1982
|
-
var XaiResponsesLanguageModel = class {
|
|
1983
|
-
constructor(modelId, config) {
|
|
1984
|
-
this.specificationVersion = "v3";
|
|
1985
|
-
this.supportedUrls = {
|
|
1986
|
-
"image/*": [/^https?:\/\/.*$/]
|
|
1987
|
-
};
|
|
1988
|
-
this.modelId = modelId;
|
|
1989
|
-
this.config = config;
|
|
1990
|
-
}
|
|
1991
|
-
get provider() {
|
|
1992
|
-
return this.config.provider;
|
|
1993
|
-
}
|
|
1994
|
-
async getArgs({
|
|
1995
|
-
prompt,
|
|
1996
|
-
maxOutputTokens,
|
|
1997
|
-
temperature,
|
|
1998
|
-
topP,
|
|
1999
|
-
stopSequences,
|
|
2000
|
-
seed,
|
|
2001
|
-
responseFormat,
|
|
2002
|
-
providerOptions,
|
|
2003
|
-
tools,
|
|
2004
|
-
toolChoice
|
|
2005
|
-
}) {
|
|
2006
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
2007
|
-
const warnings = [];
|
|
2008
|
-
const options = (_a = await parseProviderOptions3({
|
|
2009
|
-
provider: "xai",
|
|
2010
|
-
providerOptions,
|
|
2011
|
-
schema: xaiLanguageModelResponsesOptions
|
|
2012
|
-
})) != null ? _a : {};
|
|
2013
|
-
if (stopSequences != null) {
|
|
2014
|
-
warnings.push({ type: "unsupported", feature: "stopSequences" });
|
|
2015
|
-
}
|
|
2016
|
-
const webSearchToolName = (_b = tools == null ? void 0 : tools.find(
|
|
2017
|
-
(tool) => tool.type === "provider" && tool.id === "xai.web_search"
|
|
2018
|
-
)) == null ? void 0 : _b.name;
|
|
2019
|
-
const xSearchToolName = (_c = tools == null ? void 0 : tools.find(
|
|
2020
|
-
(tool) => tool.type === "provider" && tool.id === "xai.x_search"
|
|
2021
|
-
)) == null ? void 0 : _c.name;
|
|
2022
|
-
const codeExecutionToolName = (_d = tools == null ? void 0 : tools.find(
|
|
2023
|
-
(tool) => tool.type === "provider" && tool.id === "xai.code_execution"
|
|
2024
|
-
)) == null ? void 0 : _d.name;
|
|
2025
|
-
const mcpToolName = (_e = tools == null ? void 0 : tools.find(
|
|
2026
|
-
(tool) => tool.type === "provider" && tool.id === "xai.mcp"
|
|
2027
|
-
)) == null ? void 0 : _e.name;
|
|
2028
|
-
const fileSearchToolName = (_f = tools == null ? void 0 : tools.find(
|
|
2029
|
-
(tool) => tool.type === "provider" && tool.id === "xai.file_search"
|
|
2030
|
-
)) == null ? void 0 : _f.name;
|
|
2031
|
-
const { input, inputWarnings } = await convertToXaiResponsesInput({
|
|
2032
|
-
prompt,
|
|
2033
|
-
store: true
|
|
2034
|
-
});
|
|
2035
|
-
warnings.push(...inputWarnings);
|
|
2036
|
-
const {
|
|
2037
|
-
tools: xaiTools2,
|
|
2038
|
-
toolChoice: xaiToolChoice,
|
|
2039
|
-
toolWarnings
|
|
2040
|
-
} = await prepareResponsesTools({
|
|
2041
|
-
tools,
|
|
2042
|
-
toolChoice
|
|
2043
|
-
});
|
|
2044
|
-
warnings.push(...toolWarnings);
|
|
2045
|
-
let include = options.include ? [...options.include] : void 0;
|
|
2046
|
-
if (options.store === false) {
|
|
2047
|
-
if (include == null) {
|
|
2048
|
-
include = ["reasoning.encrypted_content"];
|
|
2049
|
-
} else {
|
|
2050
|
-
include = [...include, "reasoning.encrypted_content"];
|
|
2051
|
-
}
|
|
2052
|
-
}
|
|
2053
|
-
const baseArgs = {
|
|
2054
|
-
model: this.modelId,
|
|
2055
|
-
input,
|
|
2056
|
-
logprobs: options.logprobs === true || options.topLogprobs != null ? true : void 0,
|
|
2057
|
-
top_logprobs: options.topLogprobs,
|
|
2058
|
-
max_output_tokens: maxOutputTokens,
|
|
2059
|
-
temperature,
|
|
2060
|
-
top_p: topP,
|
|
2061
|
-
seed,
|
|
2062
|
-
...(responseFormat == null ? void 0 : responseFormat.type) === "json" && {
|
|
2063
|
-
text: {
|
|
2064
|
-
format: responseFormat.schema != null ? {
|
|
2065
|
-
type: "json_schema",
|
|
2066
|
-
strict: true,
|
|
2067
|
-
name: (_g = responseFormat.name) != null ? _g : "response",
|
|
2068
|
-
description: responseFormat.description,
|
|
2069
|
-
schema: responseFormat.schema
|
|
2070
|
-
} : { type: "json_object" }
|
|
2071
|
-
}
|
|
2072
|
-
},
|
|
2073
|
-
...options.reasoningEffort != null && {
|
|
2074
|
-
reasoning: { effort: options.reasoningEffort }
|
|
2075
|
-
},
|
|
2076
|
-
...options.store === false && {
|
|
2077
|
-
store: options.store
|
|
2078
|
-
},
|
|
2079
|
-
...include != null && {
|
|
2080
|
-
include
|
|
2081
|
-
},
|
|
2082
|
-
...options.previousResponseId != null && {
|
|
2083
|
-
previous_response_id: options.previousResponseId
|
|
2084
|
-
}
|
|
2085
|
-
};
|
|
2086
|
-
if (xaiTools2 && xaiTools2.length > 0) {
|
|
2087
|
-
baseArgs.tools = xaiTools2;
|
|
2088
|
-
}
|
|
2089
|
-
if (xaiToolChoice != null) {
|
|
2090
|
-
baseArgs.tool_choice = xaiToolChoice;
|
|
2091
|
-
}
|
|
2092
|
-
return {
|
|
2093
|
-
args: baseArgs,
|
|
2094
|
-
warnings,
|
|
2095
|
-
webSearchToolName,
|
|
2096
|
-
xSearchToolName,
|
|
2097
|
-
codeExecutionToolName,
|
|
2098
|
-
mcpToolName,
|
|
2099
|
-
fileSearchToolName
|
|
2100
|
-
};
|
|
2101
|
-
}
|
|
2102
|
-
async doGenerate(options) {
|
|
2103
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
2104
|
-
const {
|
|
2105
|
-
args: body,
|
|
2106
|
-
warnings,
|
|
2107
|
-
webSearchToolName,
|
|
2108
|
-
xSearchToolName,
|
|
2109
|
-
codeExecutionToolName,
|
|
2110
|
-
mcpToolName,
|
|
2111
|
-
fileSearchToolName
|
|
2112
|
-
} = await this.getArgs(options);
|
|
2113
|
-
const {
|
|
2114
|
-
responseHeaders,
|
|
2115
|
-
value: response,
|
|
2116
|
-
rawValue: rawResponse
|
|
2117
|
-
} = await postJsonToApi3({
|
|
2118
|
-
url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/responses`,
|
|
2119
|
-
headers: combineHeaders3(this.config.headers(), options.headers),
|
|
2120
|
-
body,
|
|
2121
|
-
failedResponseHandler: xaiFailedResponseHandler,
|
|
2122
|
-
successfulResponseHandler: createJsonResponseHandler3(
|
|
2123
|
-
xaiResponsesResponseSchema
|
|
2124
|
-
),
|
|
2125
|
-
abortSignal: options.abortSignal,
|
|
2126
|
-
fetch: this.config.fetch
|
|
2127
|
-
});
|
|
2128
|
-
const content = [];
|
|
2129
|
-
const webSearchSubTools = [
|
|
2130
|
-
"web_search",
|
|
2131
|
-
"web_search_with_snippets",
|
|
2132
|
-
"browse_page"
|
|
2133
|
-
];
|
|
2134
|
-
const xSearchSubTools = [
|
|
2135
|
-
"x_user_search",
|
|
2136
|
-
"x_keyword_search",
|
|
2137
|
-
"x_semantic_search",
|
|
2138
|
-
"x_thread_fetch"
|
|
2139
|
-
];
|
|
2140
|
-
for (const part of response.output) {
|
|
2141
|
-
if (part.type === "file_search_call") {
|
|
2142
|
-
const toolName = fileSearchToolName != null ? fileSearchToolName : "file_search";
|
|
2143
|
-
content.push({
|
|
2144
|
-
type: "tool-call",
|
|
2145
|
-
toolCallId: part.id,
|
|
2146
|
-
toolName,
|
|
2147
|
-
input: "",
|
|
2148
|
-
providerExecuted: true
|
|
2149
|
-
});
|
|
2150
|
-
content.push({
|
|
2151
|
-
type: "tool-result",
|
|
2152
|
-
toolCallId: part.id,
|
|
2153
|
-
toolName,
|
|
2154
|
-
result: {
|
|
2155
|
-
queries: (_b = part.queries) != null ? _b : [],
|
|
2156
|
-
results: (_d = (_c = part.results) == null ? void 0 : _c.map((result) => ({
|
|
2157
|
-
fileId: result.file_id,
|
|
2158
|
-
filename: result.filename,
|
|
2159
|
-
score: result.score,
|
|
2160
|
-
text: result.text
|
|
2161
|
-
}))) != null ? _d : null
|
|
2162
|
-
}
|
|
2163
|
-
});
|
|
2164
|
-
continue;
|
|
2165
|
-
}
|
|
2166
|
-
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") {
|
|
2167
|
-
let toolName = (_e = part.name) != null ? _e : "";
|
|
2168
|
-
if (webSearchSubTools.includes((_f = part.name) != null ? _f : "") || part.type === "web_search_call") {
|
|
2169
|
-
toolName = webSearchToolName != null ? webSearchToolName : "web_search";
|
|
2170
|
-
} else if (xSearchSubTools.includes((_g = part.name) != null ? _g : "") || part.type === "x_search_call") {
|
|
2171
|
-
toolName = xSearchToolName != null ? xSearchToolName : "x_search";
|
|
2172
|
-
} else if (part.name === "code_execution" || part.type === "code_interpreter_call" || part.type === "code_execution_call") {
|
|
2173
|
-
toolName = codeExecutionToolName != null ? codeExecutionToolName : "code_execution";
|
|
2174
|
-
} else if (part.type === "mcp_call") {
|
|
2175
|
-
toolName = (_h = mcpToolName != null ? mcpToolName : part.name) != null ? _h : "mcp";
|
|
2176
|
-
}
|
|
2177
|
-
const toolInput = part.type === "custom_tool_call" ? (_i = part.input) != null ? _i : "" : part.type === "mcp_call" ? (_j = part.arguments) != null ? _j : "" : (_k = part.arguments) != null ? _k : "";
|
|
2178
|
-
content.push({
|
|
2179
|
-
type: "tool-call",
|
|
2180
|
-
toolCallId: part.id,
|
|
2181
|
-
toolName,
|
|
2182
|
-
input: toolInput,
|
|
2183
|
-
providerExecuted: true
|
|
2184
|
-
});
|
|
2185
|
-
continue;
|
|
2186
|
-
}
|
|
2187
|
-
switch (part.type) {
|
|
2188
|
-
case "message": {
|
|
2189
|
-
for (const contentPart of part.content) {
|
|
2190
|
-
if (contentPart.text) {
|
|
2191
|
-
content.push({
|
|
2192
|
-
type: "text",
|
|
2193
|
-
text: contentPart.text
|
|
2194
|
-
});
|
|
2195
|
-
}
|
|
2196
|
-
if (contentPart.annotations) {
|
|
2197
|
-
for (const annotation of contentPart.annotations) {
|
|
2198
|
-
if (annotation.type === "url_citation" && "url" in annotation) {
|
|
2199
|
-
content.push({
|
|
2200
|
-
type: "source",
|
|
2201
|
-
sourceType: "url",
|
|
2202
|
-
id: this.config.generateId(),
|
|
2203
|
-
url: annotation.url,
|
|
2204
|
-
title: (_l = annotation.title) != null ? _l : annotation.url
|
|
2205
|
-
});
|
|
2206
|
-
}
|
|
2207
|
-
}
|
|
2208
|
-
}
|
|
2209
|
-
}
|
|
2210
|
-
break;
|
|
2211
|
-
}
|
|
2212
|
-
case "function_call": {
|
|
2213
|
-
content.push({
|
|
2214
|
-
type: "tool-call",
|
|
2215
|
-
toolCallId: part.call_id,
|
|
2216
|
-
toolName: part.name,
|
|
2217
|
-
input: part.arguments
|
|
2218
|
-
});
|
|
2219
|
-
break;
|
|
2220
|
-
}
|
|
2221
|
-
case "reasoning": {
|
|
2222
|
-
const summaryTexts = part.summary.map((s) => s.text).filter((text) => text && text.length > 0);
|
|
2223
|
-
if (summaryTexts.length > 0) {
|
|
2224
|
-
const reasoningText = summaryTexts.join("");
|
|
2225
|
-
if (part.encrypted_content || part.id) {
|
|
2226
|
-
content.push({
|
|
2227
|
-
type: "reasoning",
|
|
2228
|
-
text: reasoningText,
|
|
2229
|
-
providerMetadata: {
|
|
2230
|
-
xai: {
|
|
2231
|
-
...part.encrypted_content && {
|
|
2232
|
-
reasoningEncryptedContent: part.encrypted_content
|
|
2233
|
-
},
|
|
2234
|
-
...part.id && { itemId: part.id }
|
|
2235
|
-
}
|
|
2236
|
-
}
|
|
2237
|
-
});
|
|
2238
|
-
} else {
|
|
2239
|
-
content.push({
|
|
2240
|
-
type: "reasoning",
|
|
2241
|
-
text: reasoningText
|
|
2242
|
-
});
|
|
2243
|
-
}
|
|
2244
|
-
}
|
|
2245
|
-
break;
|
|
2246
|
-
}
|
|
2247
|
-
default: {
|
|
2248
|
-
break;
|
|
2249
|
-
}
|
|
2250
|
-
}
|
|
2251
|
-
}
|
|
2252
|
-
return {
|
|
2253
|
-
content,
|
|
2254
|
-
finishReason: {
|
|
2255
|
-
unified: mapXaiResponsesFinishReason(response.status),
|
|
2256
|
-
raw: (_m = response.status) != null ? _m : void 0
|
|
2257
|
-
},
|
|
2258
|
-
usage: response.usage ? convertXaiResponsesUsage(response.usage) : {
|
|
2259
|
-
inputTokens: { total: 0, noCache: 0, cacheRead: 0, cacheWrite: 0 },
|
|
2260
|
-
outputTokens: { total: 0, text: 0, reasoning: 0 }
|
|
2261
|
-
},
|
|
2262
|
-
request: { body },
|
|
2263
|
-
response: {
|
|
2264
|
-
...getResponseMetadata(response),
|
|
2265
|
-
headers: responseHeaders,
|
|
2266
|
-
body: rawResponse
|
|
2267
|
-
},
|
|
2268
|
-
warnings
|
|
2269
|
-
};
|
|
2270
|
-
}
|
|
2271
|
-
async doStream(options) {
|
|
2272
|
-
var _a;
|
|
2273
|
-
const {
|
|
2274
|
-
args,
|
|
2275
|
-
warnings,
|
|
2276
|
-
webSearchToolName,
|
|
2277
|
-
xSearchToolName,
|
|
2278
|
-
codeExecutionToolName,
|
|
2279
|
-
mcpToolName,
|
|
2280
|
-
fileSearchToolName
|
|
2281
|
-
} = await this.getArgs(options);
|
|
2282
|
-
const body = {
|
|
2283
|
-
...args,
|
|
2284
|
-
stream: true
|
|
2285
|
-
};
|
|
2286
|
-
const { responseHeaders, value: response } = await postJsonToApi3({
|
|
2287
|
-
url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/responses`,
|
|
2288
|
-
headers: combineHeaders3(this.config.headers(), options.headers),
|
|
2289
|
-
body,
|
|
2290
|
-
failedResponseHandler: xaiFailedResponseHandler,
|
|
2291
|
-
successfulResponseHandler: createEventSourceResponseHandler2(
|
|
2292
|
-
xaiResponsesChunkSchema
|
|
2293
|
-
),
|
|
2294
|
-
abortSignal: options.abortSignal,
|
|
2295
|
-
fetch: this.config.fetch
|
|
2296
|
-
});
|
|
2297
|
-
let finishReason = {
|
|
2298
|
-
unified: "other",
|
|
2299
|
-
raw: void 0
|
|
2300
|
-
};
|
|
2301
|
-
let usage = void 0;
|
|
2302
|
-
let isFirstChunk = true;
|
|
2303
|
-
const contentBlocks = {};
|
|
2304
|
-
const seenToolCalls = /* @__PURE__ */ new Set();
|
|
2305
|
-
const ongoingToolCalls = {};
|
|
2306
|
-
const activeReasoning = {};
|
|
2307
|
-
const self = this;
|
|
2308
|
-
return {
|
|
2309
|
-
stream: response.pipeThrough(
|
|
2310
|
-
new TransformStream({
|
|
2311
|
-
start(controller) {
|
|
2312
|
-
controller.enqueue({ type: "stream-start", warnings });
|
|
2313
|
-
},
|
|
2314
|
-
transform(chunk, controller) {
|
|
2315
|
-
var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
2316
|
-
if (options.includeRawChunks) {
|
|
2317
|
-
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
2318
|
-
}
|
|
2319
|
-
if (!chunk.success) {
|
|
2320
|
-
controller.enqueue({ type: "error", error: chunk.error });
|
|
2321
|
-
return;
|
|
2322
|
-
}
|
|
2323
|
-
const event = chunk.value;
|
|
2324
|
-
if (event.type === "response.created" || event.type === "response.in_progress") {
|
|
2325
|
-
if (isFirstChunk) {
|
|
2326
|
-
controller.enqueue({
|
|
2327
|
-
type: "response-metadata",
|
|
2328
|
-
...getResponseMetadata(event.response)
|
|
2329
|
-
});
|
|
2330
|
-
isFirstChunk = false;
|
|
2331
|
-
}
|
|
2332
|
-
return;
|
|
2333
|
-
}
|
|
2334
|
-
if (event.type === "response.reasoning_summary_part.added") {
|
|
2335
|
-
const blockId = `reasoning-${event.item_id}`;
|
|
2336
|
-
activeReasoning[event.item_id] = {};
|
|
2337
|
-
controller.enqueue({
|
|
2338
|
-
type: "reasoning-start",
|
|
2339
|
-
id: blockId,
|
|
2340
|
-
providerMetadata: {
|
|
2341
|
-
xai: {
|
|
2342
|
-
itemId: event.item_id
|
|
2343
|
-
}
|
|
2344
|
-
}
|
|
2345
|
-
});
|
|
2346
|
-
}
|
|
2347
|
-
if (event.type === "response.reasoning_summary_text.delta") {
|
|
2348
|
-
const blockId = `reasoning-${event.item_id}`;
|
|
2349
|
-
controller.enqueue({
|
|
2350
|
-
type: "reasoning-delta",
|
|
2351
|
-
id: blockId,
|
|
2352
|
-
delta: event.delta,
|
|
2353
|
-
providerMetadata: {
|
|
2354
|
-
xai: {
|
|
2355
|
-
itemId: event.item_id
|
|
2356
|
-
}
|
|
2357
|
-
}
|
|
2358
|
-
});
|
|
2359
|
-
return;
|
|
2360
|
-
}
|
|
2361
|
-
if (event.type === "response.reasoning_summary_text.done") {
|
|
2362
|
-
return;
|
|
2363
|
-
}
|
|
2364
|
-
if (event.type === "response.reasoning_text.delta") {
|
|
2365
|
-
const blockId = `reasoning-${event.item_id}`;
|
|
2366
|
-
if (activeReasoning[event.item_id] == null) {
|
|
2367
|
-
activeReasoning[event.item_id] = {};
|
|
2368
|
-
controller.enqueue({
|
|
2369
|
-
type: "reasoning-start",
|
|
2370
|
-
id: blockId,
|
|
2371
|
-
providerMetadata: {
|
|
2372
|
-
xai: {
|
|
2373
|
-
itemId: event.item_id
|
|
2374
|
-
}
|
|
2375
|
-
}
|
|
2376
|
-
});
|
|
2377
|
-
}
|
|
2378
|
-
controller.enqueue({
|
|
2379
|
-
type: "reasoning-delta",
|
|
2380
|
-
id: blockId,
|
|
2381
|
-
delta: event.delta,
|
|
2382
|
-
providerMetadata: {
|
|
2383
|
-
xai: {
|
|
2384
|
-
itemId: event.item_id
|
|
2385
|
-
}
|
|
2386
|
-
}
|
|
2387
|
-
});
|
|
2388
|
-
return;
|
|
2389
|
-
}
|
|
2390
|
-
if (event.type === "response.reasoning_text.done") {
|
|
2391
|
-
return;
|
|
2392
|
-
}
|
|
2393
|
-
if (event.type === "response.output_text.delta") {
|
|
2394
|
-
const blockId = `text-${event.item_id}`;
|
|
2395
|
-
if (contentBlocks[blockId] == null) {
|
|
2396
|
-
contentBlocks[blockId] = { type: "text" };
|
|
2397
|
-
controller.enqueue({
|
|
2398
|
-
type: "text-start",
|
|
2399
|
-
id: blockId
|
|
2400
|
-
});
|
|
2401
|
-
}
|
|
2402
|
-
controller.enqueue({
|
|
2403
|
-
type: "text-delta",
|
|
2404
|
-
id: blockId,
|
|
2405
|
-
delta: event.delta
|
|
2406
|
-
});
|
|
2407
|
-
return;
|
|
2408
|
-
}
|
|
2409
|
-
if (event.type === "response.output_text.done") {
|
|
2410
|
-
if (event.annotations) {
|
|
2411
|
-
for (const annotation of event.annotations) {
|
|
2412
|
-
if (annotation.type === "url_citation" && "url" in annotation) {
|
|
2413
|
-
controller.enqueue({
|
|
2414
|
-
type: "source",
|
|
2415
|
-
sourceType: "url",
|
|
2416
|
-
id: self.config.generateId(),
|
|
2417
|
-
url: annotation.url,
|
|
2418
|
-
title: (_a2 = annotation.title) != null ? _a2 : annotation.url
|
|
2419
|
-
});
|
|
2420
|
-
}
|
|
2421
|
-
}
|
|
2422
|
-
}
|
|
2423
|
-
return;
|
|
2424
|
-
}
|
|
2425
|
-
if (event.type === "response.output_text.annotation.added") {
|
|
2426
|
-
const annotation = event.annotation;
|
|
2427
|
-
if (annotation.type === "url_citation" && "url" in annotation) {
|
|
2428
|
-
controller.enqueue({
|
|
2429
|
-
type: "source",
|
|
2430
|
-
sourceType: "url",
|
|
2431
|
-
id: self.config.generateId(),
|
|
2432
|
-
url: annotation.url,
|
|
2433
|
-
title: (_b = annotation.title) != null ? _b : annotation.url
|
|
2434
|
-
});
|
|
2435
|
-
}
|
|
2436
|
-
return;
|
|
2437
|
-
}
|
|
2438
|
-
if (event.type === "response.done" || event.type === "response.completed") {
|
|
2439
|
-
const response2 = event.response;
|
|
2440
|
-
if (response2.usage) {
|
|
2441
|
-
usage = convertXaiResponsesUsage(response2.usage);
|
|
2442
|
-
}
|
|
2443
|
-
if (response2.status) {
|
|
2444
|
-
finishReason = {
|
|
2445
|
-
unified: mapXaiResponsesFinishReason(response2.status),
|
|
2446
|
-
raw: response2.status
|
|
2447
|
-
};
|
|
2448
|
-
}
|
|
2449
|
-
return;
|
|
2450
|
-
}
|
|
2451
|
-
if (event.type === "response.custom_tool_call_input.delta" || event.type === "response.custom_tool_call_input.done") {
|
|
2452
|
-
return;
|
|
2453
|
-
}
|
|
2454
|
-
if (event.type === "response.function_call_arguments.delta") {
|
|
2455
|
-
const toolCall = ongoingToolCalls[event.output_index];
|
|
2456
|
-
if (toolCall != null) {
|
|
2457
|
-
controller.enqueue({
|
|
2458
|
-
type: "tool-input-delta",
|
|
2459
|
-
id: toolCall.toolCallId,
|
|
2460
|
-
delta: event.delta
|
|
2461
|
-
});
|
|
2462
|
-
}
|
|
2463
|
-
return;
|
|
2464
|
-
}
|
|
2465
|
-
if (event.type === "response.function_call_arguments.done") {
|
|
2466
|
-
return;
|
|
2467
|
-
}
|
|
2468
|
-
if (event.type === "response.output_item.added" || event.type === "response.output_item.done") {
|
|
2469
|
-
const part = event.item;
|
|
2470
|
-
if (part.type === "reasoning") {
|
|
2471
|
-
if (event.type === "response.output_item.done") {
|
|
2472
|
-
const blockId = `reasoning-${part.id}`;
|
|
2473
|
-
if (!(part.id in activeReasoning)) {
|
|
2474
|
-
activeReasoning[part.id] = {};
|
|
2475
|
-
controller.enqueue({
|
|
2476
|
-
type: "reasoning-start",
|
|
2477
|
-
id: blockId,
|
|
2478
|
-
providerMetadata: {
|
|
2479
|
-
xai: {
|
|
2480
|
-
...part.id && { itemId: part.id }
|
|
2481
|
-
}
|
|
2482
|
-
}
|
|
2483
|
-
});
|
|
2484
|
-
}
|
|
2485
|
-
controller.enqueue({
|
|
2486
|
-
type: "reasoning-end",
|
|
2487
|
-
id: blockId,
|
|
2488
|
-
providerMetadata: {
|
|
2489
|
-
xai: {
|
|
2490
|
-
...part.encrypted_content && {
|
|
2491
|
-
reasoningEncryptedContent: part.encrypted_content
|
|
2492
|
-
},
|
|
2493
|
-
...part.id && { itemId: part.id }
|
|
2494
|
-
}
|
|
2495
|
-
}
|
|
2496
|
-
});
|
|
2497
|
-
delete activeReasoning[part.id];
|
|
2498
|
-
}
|
|
2499
|
-
return;
|
|
2500
|
-
}
|
|
2501
|
-
if (part.type === "file_search_call") {
|
|
2502
|
-
const toolName = fileSearchToolName != null ? fileSearchToolName : "file_search";
|
|
2503
|
-
if (!seenToolCalls.has(part.id)) {
|
|
2504
|
-
seenToolCalls.add(part.id);
|
|
2505
|
-
controller.enqueue({
|
|
2506
|
-
type: "tool-input-start",
|
|
2507
|
-
id: part.id,
|
|
2508
|
-
toolName
|
|
2509
|
-
});
|
|
2510
|
-
controller.enqueue({
|
|
2511
|
-
type: "tool-input-delta",
|
|
2512
|
-
id: part.id,
|
|
2513
|
-
delta: ""
|
|
2514
|
-
});
|
|
2515
|
-
controller.enqueue({
|
|
2516
|
-
type: "tool-input-end",
|
|
2517
|
-
id: part.id
|
|
2518
|
-
});
|
|
2519
|
-
controller.enqueue({
|
|
2520
|
-
type: "tool-call",
|
|
2521
|
-
toolCallId: part.id,
|
|
2522
|
-
toolName,
|
|
2523
|
-
input: "",
|
|
2524
|
-
providerExecuted: true
|
|
2525
|
-
});
|
|
2526
|
-
}
|
|
2527
|
-
if (event.type === "response.output_item.done") {
|
|
2528
|
-
controller.enqueue({
|
|
2529
|
-
type: "tool-result",
|
|
2530
|
-
toolCallId: part.id,
|
|
2531
|
-
toolName,
|
|
2532
|
-
result: {
|
|
2533
|
-
queries: (_c = part.queries) != null ? _c : [],
|
|
2534
|
-
results: (_e = (_d = part.results) == null ? void 0 : _d.map((result) => ({
|
|
2535
|
-
fileId: result.file_id,
|
|
2536
|
-
filename: result.filename,
|
|
2537
|
-
score: result.score,
|
|
2538
|
-
text: result.text
|
|
2539
|
-
}))) != null ? _e : null
|
|
2540
|
-
}
|
|
2541
|
-
});
|
|
2542
|
-
}
|
|
2543
|
-
return;
|
|
2544
|
-
}
|
|
2545
|
-
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") {
|
|
2546
|
-
const webSearchSubTools = [
|
|
2547
|
-
"web_search",
|
|
2548
|
-
"web_search_with_snippets",
|
|
2549
|
-
"browse_page"
|
|
2550
|
-
];
|
|
2551
|
-
const xSearchSubTools = [
|
|
2552
|
-
"x_user_search",
|
|
2553
|
-
"x_keyword_search",
|
|
2554
|
-
"x_semantic_search",
|
|
2555
|
-
"x_thread_fetch"
|
|
2556
|
-
];
|
|
2557
|
-
let toolName = (_f = part.name) != null ? _f : "";
|
|
2558
|
-
if (webSearchSubTools.includes((_g = part.name) != null ? _g : "") || part.type === "web_search_call") {
|
|
2559
|
-
toolName = webSearchToolName != null ? webSearchToolName : "web_search";
|
|
2560
|
-
} else if (xSearchSubTools.includes((_h = part.name) != null ? _h : "") || part.type === "x_search_call") {
|
|
2561
|
-
toolName = xSearchToolName != null ? xSearchToolName : "x_search";
|
|
2562
|
-
} else if (part.name === "code_execution" || part.type === "code_interpreter_call" || part.type === "code_execution_call") {
|
|
2563
|
-
toolName = codeExecutionToolName != null ? codeExecutionToolName : "code_execution";
|
|
2564
|
-
} else if (part.type === "mcp_call") {
|
|
2565
|
-
toolName = (_i = mcpToolName != null ? mcpToolName : part.name) != null ? _i : "mcp";
|
|
2566
|
-
}
|
|
2567
|
-
const toolInput = part.type === "custom_tool_call" ? (_j = part.input) != null ? _j : "" : part.type === "mcp_call" ? (_k = part.arguments) != null ? _k : "" : (_l = part.arguments) != null ? _l : "";
|
|
2568
|
-
const shouldEmit = part.type === "custom_tool_call" ? event.type === "response.output_item.done" : !seenToolCalls.has(part.id);
|
|
2569
|
-
if (shouldEmit && !seenToolCalls.has(part.id)) {
|
|
2570
|
-
seenToolCalls.add(part.id);
|
|
2571
|
-
controller.enqueue({
|
|
2572
|
-
type: "tool-input-start",
|
|
2573
|
-
id: part.id,
|
|
2574
|
-
toolName
|
|
2575
|
-
});
|
|
2576
|
-
controller.enqueue({
|
|
2577
|
-
type: "tool-input-delta",
|
|
2578
|
-
id: part.id,
|
|
2579
|
-
delta: toolInput
|
|
2580
|
-
});
|
|
2581
|
-
controller.enqueue({
|
|
2582
|
-
type: "tool-input-end",
|
|
2583
|
-
id: part.id
|
|
2584
|
-
});
|
|
2585
|
-
controller.enqueue({
|
|
2586
|
-
type: "tool-call",
|
|
2587
|
-
toolCallId: part.id,
|
|
2588
|
-
toolName,
|
|
2589
|
-
input: toolInput,
|
|
2590
|
-
providerExecuted: true
|
|
2591
|
-
});
|
|
2592
|
-
}
|
|
2593
|
-
return;
|
|
2594
|
-
}
|
|
2595
|
-
if (part.type === "message") {
|
|
2596
|
-
for (const contentPart of part.content) {
|
|
2597
|
-
if (contentPart.text && contentPart.text.length > 0) {
|
|
2598
|
-
const blockId = `text-${part.id}`;
|
|
2599
|
-
if (contentBlocks[blockId] == null) {
|
|
2600
|
-
contentBlocks[blockId] = { type: "text" };
|
|
2601
|
-
controller.enqueue({
|
|
2602
|
-
type: "text-start",
|
|
2603
|
-
id: blockId
|
|
2604
|
-
});
|
|
2605
|
-
controller.enqueue({
|
|
2606
|
-
type: "text-delta",
|
|
2607
|
-
id: blockId,
|
|
2608
|
-
delta: contentPart.text
|
|
2609
|
-
});
|
|
2610
|
-
}
|
|
2611
|
-
}
|
|
2612
|
-
if (contentPart.annotations) {
|
|
2613
|
-
for (const annotation of contentPart.annotations) {
|
|
2614
|
-
if (annotation.type === "url_citation" && "url" in annotation) {
|
|
2615
|
-
controller.enqueue({
|
|
2616
|
-
type: "source",
|
|
2617
|
-
sourceType: "url",
|
|
2618
|
-
id: self.config.generateId(),
|
|
2619
|
-
url: annotation.url,
|
|
2620
|
-
title: (_m = annotation.title) != null ? _m : annotation.url
|
|
2621
|
-
});
|
|
2622
|
-
}
|
|
2623
|
-
}
|
|
2624
|
-
}
|
|
2625
|
-
}
|
|
2626
|
-
} else if (part.type === "function_call") {
|
|
2627
|
-
if (event.type === "response.output_item.added") {
|
|
2628
|
-
ongoingToolCalls[event.output_index] = {
|
|
2629
|
-
toolName: part.name,
|
|
2630
|
-
toolCallId: part.call_id
|
|
2631
|
-
};
|
|
2632
|
-
controller.enqueue({
|
|
2633
|
-
type: "tool-input-start",
|
|
2634
|
-
id: part.call_id,
|
|
2635
|
-
toolName: part.name
|
|
2636
|
-
});
|
|
2637
|
-
} else if (event.type === "response.output_item.done") {
|
|
2638
|
-
ongoingToolCalls[event.output_index] = void 0;
|
|
2639
|
-
controller.enqueue({
|
|
2640
|
-
type: "tool-input-end",
|
|
2641
|
-
id: part.call_id
|
|
2642
|
-
});
|
|
2643
|
-
controller.enqueue({
|
|
2644
|
-
type: "tool-call",
|
|
2645
|
-
toolCallId: part.call_id,
|
|
2646
|
-
toolName: part.name,
|
|
2647
|
-
input: part.arguments
|
|
2648
|
-
});
|
|
2649
|
-
}
|
|
2650
|
-
}
|
|
2651
|
-
}
|
|
2652
|
-
},
|
|
2653
|
-
flush(controller) {
|
|
2654
|
-
for (const [blockId, block] of Object.entries(contentBlocks)) {
|
|
2655
|
-
if (block.type === "text") {
|
|
2656
|
-
controller.enqueue({
|
|
2657
|
-
type: "text-end",
|
|
2658
|
-
id: blockId
|
|
2659
|
-
});
|
|
2660
|
-
}
|
|
2661
|
-
}
|
|
2662
|
-
controller.enqueue({
|
|
2663
|
-
type: "finish",
|
|
2664
|
-
finishReason,
|
|
2665
|
-
usage: usage != null ? usage : {
|
|
2666
|
-
inputTokens: {
|
|
2667
|
-
total: 0,
|
|
2668
|
-
noCache: 0,
|
|
2669
|
-
cacheRead: 0,
|
|
2670
|
-
cacheWrite: 0
|
|
2671
|
-
},
|
|
2672
|
-
outputTokens: { total: 0, text: 0, reasoning: 0 }
|
|
2673
|
-
}
|
|
2674
|
-
});
|
|
2675
|
-
}
|
|
2676
|
-
})
|
|
2677
|
-
),
|
|
2678
|
-
request: { body },
|
|
2679
|
-
response: { headers: responseHeaders }
|
|
2680
|
-
};
|
|
2681
|
-
}
|
|
2682
|
-
};
|
|
2683
|
-
|
|
2684
|
-
// src/tool/code-execution.ts
|
|
2685
|
-
import { createProviderToolFactoryWithOutputSchema as createProviderToolFactoryWithOutputSchema5 } from "@ai-sdk/provider-utils";
|
|
2686
|
-
import { z as z12 } from "zod/v4";
|
|
2687
|
-
var codeExecutionOutputSchema = z12.object({
|
|
2688
|
-
output: z12.string().describe("the output of the code execution"),
|
|
2689
|
-
error: z12.string().optional().describe("any error that occurred")
|
|
2690
|
-
});
|
|
2691
|
-
var codeExecutionToolFactory = createProviderToolFactoryWithOutputSchema5({
|
|
2692
|
-
id: "xai.code_execution",
|
|
2693
|
-
inputSchema: z12.object({}).describe("no input parameters"),
|
|
2694
|
-
outputSchema: codeExecutionOutputSchema
|
|
2695
|
-
});
|
|
2696
|
-
var codeExecution = (args = {}) => codeExecutionToolFactory(args);
|
|
2697
|
-
|
|
2698
|
-
// src/tool/view-image.ts
|
|
2699
|
-
import { createProviderToolFactoryWithOutputSchema as createProviderToolFactoryWithOutputSchema6 } from "@ai-sdk/provider-utils";
|
|
2700
|
-
import { z as z13 } from "zod/v4";
|
|
2701
|
-
var viewImageOutputSchema = z13.object({
|
|
2702
|
-
description: z13.string().describe("description of the image"),
|
|
2703
|
-
objects: z13.array(z13.string()).optional().describe("objects detected in the image")
|
|
2704
|
-
});
|
|
2705
|
-
var viewImageToolFactory = createProviderToolFactoryWithOutputSchema6({
|
|
2706
|
-
id: "xai.view_image",
|
|
2707
|
-
inputSchema: z13.object({}).describe("no input parameters"),
|
|
2708
|
-
outputSchema: viewImageOutputSchema
|
|
2709
|
-
});
|
|
2710
|
-
var viewImage = (args = {}) => viewImageToolFactory(args);
|
|
2711
|
-
|
|
2712
|
-
// src/tool/view-x-video.ts
|
|
2713
|
-
import { createProviderToolFactoryWithOutputSchema as createProviderToolFactoryWithOutputSchema7 } from "@ai-sdk/provider-utils";
|
|
2714
|
-
import { z as z14 } from "zod/v4";
|
|
2715
|
-
var viewXVideoOutputSchema = z14.object({
|
|
2716
|
-
transcript: z14.string().optional().describe("transcript of the video"),
|
|
2717
|
-
description: z14.string().describe("description of the video content"),
|
|
2718
|
-
duration: z14.number().optional().describe("duration in seconds")
|
|
2719
|
-
});
|
|
2720
|
-
var viewXVideoToolFactory = createProviderToolFactoryWithOutputSchema7({
|
|
2721
|
-
id: "xai.view_x_video",
|
|
2722
|
-
inputSchema: z14.object({}).describe("no input parameters"),
|
|
2723
|
-
outputSchema: viewXVideoOutputSchema
|
|
2724
|
-
});
|
|
2725
|
-
var viewXVideo = (args = {}) => viewXVideoToolFactory(args);
|
|
2726
|
-
|
|
2727
|
-
// src/tool/index.ts
|
|
2728
|
-
var xaiTools = {
|
|
2729
|
-
codeExecution,
|
|
2730
|
-
fileSearch,
|
|
2731
|
-
mcpServer,
|
|
2732
|
-
viewImage,
|
|
2733
|
-
viewXVideo,
|
|
2734
|
-
webSearch,
|
|
2735
|
-
xSearch
|
|
2736
|
-
};
|
|
2737
|
-
|
|
2738
|
-
// src/version.ts
|
|
2739
|
-
var VERSION = true ? "4.0.0-beta.4" : "0.0.0-test";
|
|
2740
|
-
|
|
2741
|
-
// src/xai-video-model.ts
|
|
2742
|
-
import {
|
|
2743
|
-
AISDKError
|
|
2744
|
-
} from "@ai-sdk/provider";
|
|
2745
|
-
import {
|
|
2746
|
-
combineHeaders as combineHeaders4,
|
|
2747
|
-
convertUint8ArrayToBase64,
|
|
2748
|
-
createJsonResponseHandler as createJsonResponseHandler4,
|
|
2749
|
-
delay,
|
|
2750
|
-
getFromApi as getFromApi2,
|
|
2751
|
-
parseProviderOptions as parseProviderOptions4,
|
|
2752
|
-
postJsonToApi as postJsonToApi4
|
|
2753
|
-
} from "@ai-sdk/provider-utils";
|
|
2754
|
-
import { z as z16 } from "zod/v4";
|
|
2755
|
-
|
|
2756
|
-
// src/xai-video-options.ts
|
|
2757
|
-
import { lazySchema as lazySchema5, zodSchema as zodSchema5 } from "@ai-sdk/provider-utils";
|
|
2758
|
-
import { z as z15 } from "zod/v4";
|
|
2759
|
-
var xaiVideoModelOptionsSchema = lazySchema5(
|
|
2760
|
-
() => zodSchema5(
|
|
2761
|
-
z15.object({
|
|
2762
|
-
pollIntervalMs: z15.number().positive().nullish(),
|
|
2763
|
-
pollTimeoutMs: z15.number().positive().nullish(),
|
|
2764
|
-
resolution: z15.enum(["480p", "720p"]).nullish(),
|
|
2765
|
-
videoUrl: z15.string().nullish()
|
|
2766
|
-
}).passthrough()
|
|
2767
|
-
)
|
|
2768
|
-
);
|
|
2769
|
-
|
|
2770
|
-
// src/xai-video-model.ts
|
|
2771
|
-
var RESOLUTION_MAP = {
|
|
2772
|
-
"1280x720": "720p",
|
|
2773
|
-
"854x480": "480p",
|
|
2774
|
-
"640x480": "480p"
|
|
2775
|
-
};
|
|
2776
|
-
var XaiVideoModel = class {
|
|
2777
|
-
constructor(modelId, config) {
|
|
2778
|
-
this.modelId = modelId;
|
|
2779
|
-
this.config = config;
|
|
2780
|
-
this.specificationVersion = "v3";
|
|
2781
|
-
this.maxVideosPerCall = 1;
|
|
2782
|
-
}
|
|
2783
|
-
get provider() {
|
|
2784
|
-
return this.config.provider;
|
|
2785
|
-
}
|
|
2786
|
-
async doGenerate(options) {
|
|
2787
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
2788
|
-
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
2789
|
-
const warnings = [];
|
|
2790
|
-
const xaiOptions = await parseProviderOptions4({
|
|
2791
|
-
provider: "xai",
|
|
2792
|
-
providerOptions: options.providerOptions,
|
|
2793
|
-
schema: xaiVideoModelOptionsSchema
|
|
2794
|
-
});
|
|
2795
|
-
const isEdit = (xaiOptions == null ? void 0 : xaiOptions.videoUrl) != null;
|
|
2796
|
-
if (options.fps != null) {
|
|
2797
|
-
warnings.push({
|
|
2798
|
-
type: "unsupported",
|
|
2799
|
-
feature: "fps",
|
|
2800
|
-
details: "xAI video models do not support custom FPS."
|
|
2801
|
-
});
|
|
2802
|
-
}
|
|
2803
|
-
if (options.seed != null) {
|
|
2804
|
-
warnings.push({
|
|
2805
|
-
type: "unsupported",
|
|
2806
|
-
feature: "seed",
|
|
2807
|
-
details: "xAI video models do not support seed."
|
|
2808
|
-
});
|
|
2809
|
-
}
|
|
2810
|
-
if (options.n != null && options.n > 1) {
|
|
2811
|
-
warnings.push({
|
|
2812
|
-
type: "unsupported",
|
|
2813
|
-
feature: "n",
|
|
2814
|
-
details: "xAI video models do not support generating multiple videos per call. Only 1 video will be generated."
|
|
2815
|
-
});
|
|
2816
|
-
}
|
|
2817
|
-
if (isEdit && options.duration != null) {
|
|
2818
|
-
warnings.push({
|
|
2819
|
-
type: "unsupported",
|
|
2820
|
-
feature: "duration",
|
|
2821
|
-
details: "xAI video editing does not support custom duration."
|
|
2822
|
-
});
|
|
2823
|
-
}
|
|
2824
|
-
if (isEdit && options.aspectRatio != null) {
|
|
2825
|
-
warnings.push({
|
|
2826
|
-
type: "unsupported",
|
|
2827
|
-
feature: "aspectRatio",
|
|
2828
|
-
details: "xAI video editing does not support custom aspect ratio."
|
|
2829
|
-
});
|
|
2830
|
-
}
|
|
2831
|
-
if (isEdit && ((xaiOptions == null ? void 0 : xaiOptions.resolution) != null || options.resolution != null)) {
|
|
2832
|
-
warnings.push({
|
|
2833
|
-
type: "unsupported",
|
|
2834
|
-
feature: "resolution",
|
|
2835
|
-
details: "xAI video editing does not support custom resolution."
|
|
2836
|
-
});
|
|
2837
|
-
}
|
|
2838
|
-
const body = {
|
|
2839
|
-
model: this.modelId,
|
|
2840
|
-
prompt: options.prompt
|
|
2841
|
-
};
|
|
2842
|
-
if (!isEdit && options.duration != null) {
|
|
2843
|
-
body.duration = options.duration;
|
|
2844
|
-
}
|
|
2845
|
-
if (!isEdit && options.aspectRatio != null) {
|
|
2846
|
-
body.aspect_ratio = options.aspectRatio;
|
|
2847
|
-
}
|
|
2848
|
-
if (!isEdit && (xaiOptions == null ? void 0 : xaiOptions.resolution) != null) {
|
|
2849
|
-
body.resolution = xaiOptions.resolution;
|
|
2850
|
-
} else if (!isEdit && options.resolution != null) {
|
|
2851
|
-
const mapped = RESOLUTION_MAP[options.resolution];
|
|
2852
|
-
if (mapped != null) {
|
|
2853
|
-
body.resolution = mapped;
|
|
2854
|
-
} else {
|
|
2855
|
-
warnings.push({
|
|
2856
|
-
type: "unsupported",
|
|
2857
|
-
feature: "resolution",
|
|
2858
|
-
details: `Unrecognized resolution "${options.resolution}". Use providerOptions.xai.resolution with "480p" or "720p" instead.`
|
|
2859
|
-
});
|
|
2860
|
-
}
|
|
2861
|
-
}
|
|
2862
|
-
if ((xaiOptions == null ? void 0 : xaiOptions.videoUrl) != null) {
|
|
2863
|
-
body.video = { url: xaiOptions.videoUrl };
|
|
2864
|
-
}
|
|
2865
|
-
if (options.image != null) {
|
|
2866
|
-
if (options.image.type === "url") {
|
|
2867
|
-
body.image = { url: options.image.url };
|
|
2868
|
-
} else {
|
|
2869
|
-
const base64Data = typeof options.image.data === "string" ? options.image.data : convertUint8ArrayToBase64(options.image.data);
|
|
2870
|
-
body.image = {
|
|
2871
|
-
url: `data:${options.image.mediaType};base64,${base64Data}`
|
|
2872
|
-
};
|
|
2873
|
-
}
|
|
2874
|
-
}
|
|
2875
|
-
if (xaiOptions != null) {
|
|
2876
|
-
for (const [key, value] of Object.entries(xaiOptions)) {
|
|
2877
|
-
if (![
|
|
2878
|
-
"pollIntervalMs",
|
|
2879
|
-
"pollTimeoutMs",
|
|
2880
|
-
"resolution",
|
|
2881
|
-
"videoUrl"
|
|
2882
|
-
].includes(key)) {
|
|
2883
|
-
body[key] = value;
|
|
2884
|
-
}
|
|
2885
|
-
}
|
|
2886
|
-
}
|
|
2887
|
-
const baseURL = (_d = this.config.baseURL) != null ? _d : "https://api.x.ai/v1";
|
|
2888
|
-
const { value: createResponse } = await postJsonToApi4({
|
|
2889
|
-
url: `${baseURL}/videos/${isEdit ? "edits" : "generations"}`,
|
|
2890
|
-
headers: combineHeaders4(this.config.headers(), options.headers),
|
|
2891
|
-
body,
|
|
2892
|
-
failedResponseHandler: xaiFailedResponseHandler,
|
|
2893
|
-
successfulResponseHandler: createJsonResponseHandler4(
|
|
2894
|
-
xaiCreateVideoResponseSchema
|
|
2895
|
-
),
|
|
2896
|
-
abortSignal: options.abortSignal,
|
|
2897
|
-
fetch: this.config.fetch
|
|
2898
|
-
});
|
|
2899
|
-
const requestId = createResponse.request_id;
|
|
2900
|
-
if (!requestId) {
|
|
2901
|
-
throw new AISDKError({
|
|
2902
|
-
name: "XAI_VIDEO_GENERATION_ERROR",
|
|
2903
|
-
message: `No request_id returned from xAI API. Response: ${JSON.stringify(createResponse)}`
|
|
2904
|
-
});
|
|
2905
|
-
}
|
|
2906
|
-
const pollIntervalMs = (_e = xaiOptions == null ? void 0 : xaiOptions.pollIntervalMs) != null ? _e : 5e3;
|
|
2907
|
-
const pollTimeoutMs = (_f = xaiOptions == null ? void 0 : xaiOptions.pollTimeoutMs) != null ? _f : 6e5;
|
|
2908
|
-
const startTime = Date.now();
|
|
2909
|
-
let responseHeaders;
|
|
2910
|
-
while (true) {
|
|
2911
|
-
await delay(pollIntervalMs, { abortSignal: options.abortSignal });
|
|
2912
|
-
if (Date.now() - startTime > pollTimeoutMs) {
|
|
2913
|
-
throw new AISDKError({
|
|
2914
|
-
name: "XAI_VIDEO_GENERATION_TIMEOUT",
|
|
2915
|
-
message: `Video generation timed out after ${pollTimeoutMs}ms`
|
|
2916
|
-
});
|
|
2917
|
-
}
|
|
2918
|
-
const { value: statusResponse, responseHeaders: pollHeaders } = await getFromApi2({
|
|
2919
|
-
url: `${baseURL}/videos/${requestId}`,
|
|
2920
|
-
headers: combineHeaders4(this.config.headers(), options.headers),
|
|
2921
|
-
successfulResponseHandler: createJsonResponseHandler4(
|
|
2922
|
-
xaiVideoStatusResponseSchema
|
|
2923
|
-
),
|
|
2924
|
-
failedResponseHandler: xaiFailedResponseHandler,
|
|
2925
|
-
abortSignal: options.abortSignal,
|
|
2926
|
-
fetch: this.config.fetch
|
|
2927
|
-
});
|
|
2928
|
-
responseHeaders = pollHeaders;
|
|
2929
|
-
if (statusResponse.status === "done" || statusResponse.status == null && ((_g = statusResponse.video) == null ? void 0 : _g.url)) {
|
|
2930
|
-
if (!((_h = statusResponse.video) == null ? void 0 : _h.url)) {
|
|
2931
|
-
throw new AISDKError({
|
|
2932
|
-
name: "XAI_VIDEO_GENERATION_ERROR",
|
|
2933
|
-
message: "Video generation completed but no video URL was returned."
|
|
2934
|
-
});
|
|
2935
|
-
}
|
|
2936
|
-
return {
|
|
2937
|
-
videos: [
|
|
2938
|
-
{
|
|
2939
|
-
type: "url",
|
|
2940
|
-
url: statusResponse.video.url,
|
|
2941
|
-
mediaType: "video/mp4"
|
|
2942
|
-
}
|
|
2943
|
-
],
|
|
2944
|
-
warnings,
|
|
2945
|
-
response: {
|
|
2946
|
-
timestamp: currentDate,
|
|
2947
|
-
modelId: this.modelId,
|
|
2948
|
-
headers: responseHeaders
|
|
2949
|
-
},
|
|
2950
|
-
providerMetadata: {
|
|
2951
|
-
xai: {
|
|
2952
|
-
requestId,
|
|
2953
|
-
videoUrl: statusResponse.video.url,
|
|
2954
|
-
...statusResponse.video.duration != null ? { duration: statusResponse.video.duration } : {}
|
|
2955
|
-
}
|
|
2956
|
-
}
|
|
2957
|
-
};
|
|
2958
|
-
}
|
|
2959
|
-
if (statusResponse.status === "expired") {
|
|
2960
|
-
throw new AISDKError({
|
|
2961
|
-
name: "XAI_VIDEO_GENERATION_EXPIRED",
|
|
2962
|
-
message: "Video generation request expired."
|
|
2963
|
-
});
|
|
2964
|
-
}
|
|
2965
|
-
}
|
|
2966
|
-
}
|
|
2967
|
-
};
|
|
2968
|
-
var xaiCreateVideoResponseSchema = z16.object({
|
|
2969
|
-
request_id: z16.string().nullish()
|
|
2970
|
-
});
|
|
2971
|
-
var xaiVideoStatusResponseSchema = z16.object({
|
|
2972
|
-
status: z16.string().nullish(),
|
|
2973
|
-
video: z16.object({
|
|
2974
|
-
url: z16.string(),
|
|
2975
|
-
duration: z16.number().nullish(),
|
|
2976
|
-
respect_moderation: z16.boolean().nullish()
|
|
2977
|
-
}).nullish(),
|
|
2978
|
-
model: z16.string().nullish()
|
|
2979
|
-
});
|
|
2980
|
-
|
|
2981
|
-
// src/xai-provider.ts
|
|
2982
|
-
function createXai(options = {}) {
|
|
2983
|
-
var _a;
|
|
2984
|
-
const baseURL = withoutTrailingSlash(
|
|
2985
|
-
(_a = options.baseURL) != null ? _a : "https://api.x.ai/v1"
|
|
2986
|
-
);
|
|
2987
|
-
const getHeaders = () => withUserAgentSuffix(
|
|
2988
|
-
{
|
|
2989
|
-
Authorization: `Bearer ${loadApiKey({
|
|
2990
|
-
apiKey: options.apiKey,
|
|
2991
|
-
environmentVariableName: "XAI_API_KEY",
|
|
2992
|
-
description: "xAI API key"
|
|
2993
|
-
})}`,
|
|
2994
|
-
...options.headers
|
|
2995
|
-
},
|
|
2996
|
-
`ai-sdk/xai/${VERSION}`
|
|
2997
|
-
);
|
|
2998
|
-
const createChatLanguageModel = (modelId) => {
|
|
2999
|
-
return new XaiChatLanguageModel(modelId, {
|
|
3000
|
-
provider: "xai.chat",
|
|
3001
|
-
baseURL,
|
|
3002
|
-
headers: getHeaders,
|
|
3003
|
-
generateId,
|
|
3004
|
-
fetch: options.fetch
|
|
3005
|
-
});
|
|
3006
|
-
};
|
|
3007
|
-
const createResponsesLanguageModel = (modelId) => {
|
|
3008
|
-
return new XaiResponsesLanguageModel(modelId, {
|
|
3009
|
-
provider: "xai.responses",
|
|
3010
|
-
baseURL,
|
|
3011
|
-
headers: getHeaders,
|
|
3012
|
-
generateId,
|
|
3013
|
-
fetch: options.fetch
|
|
3014
|
-
});
|
|
3015
|
-
};
|
|
3016
|
-
const createImageModel = (modelId) => {
|
|
3017
|
-
return new XaiImageModel(modelId, {
|
|
3018
|
-
provider: "xai.image",
|
|
3019
|
-
baseURL,
|
|
3020
|
-
headers: getHeaders,
|
|
3021
|
-
fetch: options.fetch
|
|
3022
|
-
});
|
|
3023
|
-
};
|
|
3024
|
-
const createVideoModel = (modelId) => {
|
|
3025
|
-
return new XaiVideoModel(modelId, {
|
|
3026
|
-
provider: "xai.video",
|
|
3027
|
-
baseURL,
|
|
3028
|
-
headers: getHeaders,
|
|
3029
|
-
fetch: options.fetch
|
|
3030
|
-
});
|
|
3031
|
-
};
|
|
3032
|
-
const provider = (modelId) => createChatLanguageModel(modelId);
|
|
3033
|
-
provider.specificationVersion = "v3";
|
|
3034
|
-
provider.languageModel = createChatLanguageModel;
|
|
3035
|
-
provider.chat = createChatLanguageModel;
|
|
3036
|
-
provider.responses = createResponsesLanguageModel;
|
|
3037
|
-
provider.embeddingModel = (modelId) => {
|
|
3038
|
-
throw new NoSuchModelError({ modelId, modelType: "embeddingModel" });
|
|
3039
|
-
};
|
|
3040
|
-
provider.textEmbeddingModel = provider.embeddingModel;
|
|
3041
|
-
provider.imageModel = createImageModel;
|
|
3042
|
-
provider.image = createImageModel;
|
|
3043
|
-
provider.videoModel = createVideoModel;
|
|
3044
|
-
provider.video = createVideoModel;
|
|
3045
|
-
provider.tools = xaiTools;
|
|
3046
|
-
return provider;
|
|
3047
|
-
}
|
|
3048
|
-
var xai = createXai();
|
|
3049
|
-
export {
|
|
3050
|
-
VERSION,
|
|
3051
|
-
codeExecution,
|
|
3052
|
-
createXai,
|
|
3053
|
-
mcpServer,
|
|
3054
|
-
viewImage,
|
|
3055
|
-
viewXVideo,
|
|
3056
|
-
webSearch,
|
|
3057
|
-
xSearch,
|
|
3058
|
-
xai,
|
|
3059
|
-
xaiTools
|
|
3060
|
-
};
|
|
3061
|
-
//# sourceMappingURL=index.mjs.map
|