@ai-sdk/xai 0.0.0-1c33ba03-20260114162300
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 +1891 -0
- package/LICENSE +13 -0
- package/README.md +36 -0
- package/dist/index.d.mts +279 -0
- package/dist/index.d.ts +279 -0
- package/dist/index.js +2281 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2287 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +68 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2287 @@
|
|
|
1
|
+
// src/xai-provider.ts
|
|
2
|
+
import {
|
|
3
|
+
OpenAICompatibleImageModel
|
|
4
|
+
} from "@ai-sdk/openai-compatible";
|
|
5
|
+
import {
|
|
6
|
+
NoSuchModelError
|
|
7
|
+
} from "@ai-sdk/provider";
|
|
8
|
+
import {
|
|
9
|
+
generateId,
|
|
10
|
+
loadApiKey,
|
|
11
|
+
withoutTrailingSlash,
|
|
12
|
+
withUserAgentSuffix
|
|
13
|
+
} from "@ai-sdk/provider-utils";
|
|
14
|
+
|
|
15
|
+
// src/xai-chat-language-model.ts
|
|
16
|
+
import {
|
|
17
|
+
APICallError
|
|
18
|
+
} from "@ai-sdk/provider";
|
|
19
|
+
import {
|
|
20
|
+
combineHeaders,
|
|
21
|
+
createEventSourceResponseHandler,
|
|
22
|
+
createJsonResponseHandler,
|
|
23
|
+
extractResponseHeaders,
|
|
24
|
+
parseProviderOptions,
|
|
25
|
+
postJsonToApi,
|
|
26
|
+
safeParseJSON
|
|
27
|
+
} from "@ai-sdk/provider-utils";
|
|
28
|
+
import { z as z3 } from "zod/v4";
|
|
29
|
+
|
|
30
|
+
// src/convert-to-xai-chat-messages.ts
|
|
31
|
+
import {
|
|
32
|
+
UnsupportedFunctionalityError
|
|
33
|
+
} from "@ai-sdk/provider";
|
|
34
|
+
import { convertToBase64 } from "@ai-sdk/provider-utils";
|
|
35
|
+
function convertToXaiChatMessages(prompt) {
|
|
36
|
+
var _a;
|
|
37
|
+
const messages = [];
|
|
38
|
+
const warnings = [];
|
|
39
|
+
for (const { role, content } of prompt) {
|
|
40
|
+
switch (role) {
|
|
41
|
+
case "system": {
|
|
42
|
+
messages.push({ role: "system", content });
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
case "user": {
|
|
46
|
+
if (content.length === 1 && content[0].type === "text") {
|
|
47
|
+
messages.push({ role: "user", content: content[0].text });
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
messages.push({
|
|
51
|
+
role: "user",
|
|
52
|
+
content: content.map((part) => {
|
|
53
|
+
switch (part.type) {
|
|
54
|
+
case "text": {
|
|
55
|
+
return { type: "text", text: part.text };
|
|
56
|
+
}
|
|
57
|
+
case "file": {
|
|
58
|
+
if (part.mediaType.startsWith("image/")) {
|
|
59
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
|
60
|
+
return {
|
|
61
|
+
type: "image_url",
|
|
62
|
+
image_url: {
|
|
63
|
+
url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
} else {
|
|
67
|
+
throw new UnsupportedFunctionalityError({
|
|
68
|
+
functionality: `file part media type ${part.mediaType}`
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
});
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
case "assistant": {
|
|
78
|
+
let text = "";
|
|
79
|
+
const toolCalls = [];
|
|
80
|
+
for (const part of content) {
|
|
81
|
+
switch (part.type) {
|
|
82
|
+
case "text": {
|
|
83
|
+
text += part.text;
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
case "tool-call": {
|
|
87
|
+
toolCalls.push({
|
|
88
|
+
id: part.toolCallId,
|
|
89
|
+
type: "function",
|
|
90
|
+
function: {
|
|
91
|
+
name: part.toolName,
|
|
92
|
+
arguments: JSON.stringify(part.input)
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
messages.push({
|
|
100
|
+
role: "assistant",
|
|
101
|
+
content: text,
|
|
102
|
+
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
103
|
+
});
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case "tool": {
|
|
107
|
+
for (const toolResponse of content) {
|
|
108
|
+
if (toolResponse.type === "tool-approval-response") {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const output = toolResponse.output;
|
|
112
|
+
let contentValue;
|
|
113
|
+
switch (output.type) {
|
|
114
|
+
case "text":
|
|
115
|
+
case "error-text":
|
|
116
|
+
contentValue = output.value;
|
|
117
|
+
break;
|
|
118
|
+
case "execution-denied":
|
|
119
|
+
contentValue = (_a = output.reason) != null ? _a : "Tool execution denied.";
|
|
120
|
+
break;
|
|
121
|
+
case "content":
|
|
122
|
+
case "json":
|
|
123
|
+
case "error-json":
|
|
124
|
+
contentValue = JSON.stringify(output.value);
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
messages.push({
|
|
128
|
+
role: "tool",
|
|
129
|
+
tool_call_id: toolResponse.toolCallId,
|
|
130
|
+
content: contentValue
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
default: {
|
|
136
|
+
const _exhaustiveCheck = role;
|
|
137
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return { messages, warnings };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// src/convert-xai-chat-usage.ts
|
|
145
|
+
function convertXaiChatUsage(usage) {
|
|
146
|
+
var _a, _b, _c, _d;
|
|
147
|
+
const cacheReadTokens = (_b = (_a = usage.prompt_tokens_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0;
|
|
148
|
+
const reasoningTokens = (_d = (_c = usage.completion_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0;
|
|
149
|
+
return {
|
|
150
|
+
inputTokens: {
|
|
151
|
+
total: usage.prompt_tokens,
|
|
152
|
+
noCache: usage.prompt_tokens - cacheReadTokens,
|
|
153
|
+
cacheRead: cacheReadTokens,
|
|
154
|
+
cacheWrite: void 0
|
|
155
|
+
},
|
|
156
|
+
outputTokens: {
|
|
157
|
+
total: usage.completion_tokens,
|
|
158
|
+
text: usage.completion_tokens - reasoningTokens,
|
|
159
|
+
reasoning: reasoningTokens
|
|
160
|
+
},
|
|
161
|
+
raw: usage
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// src/get-response-metadata.ts
|
|
166
|
+
function getResponseMetadata({
|
|
167
|
+
id,
|
|
168
|
+
model,
|
|
169
|
+
created,
|
|
170
|
+
created_at
|
|
171
|
+
}) {
|
|
172
|
+
const unixTime = created != null ? created : created_at;
|
|
173
|
+
return {
|
|
174
|
+
id: id != null ? id : void 0,
|
|
175
|
+
modelId: model != null ? model : void 0,
|
|
176
|
+
timestamp: unixTime != null ? new Date(unixTime * 1e3) : void 0
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// src/map-xai-finish-reason.ts
|
|
181
|
+
function mapXaiFinishReason(finishReason) {
|
|
182
|
+
switch (finishReason) {
|
|
183
|
+
case "stop":
|
|
184
|
+
return "stop";
|
|
185
|
+
case "length":
|
|
186
|
+
return "length";
|
|
187
|
+
case "tool_calls":
|
|
188
|
+
case "function_call":
|
|
189
|
+
return "tool-calls";
|
|
190
|
+
case "content_filter":
|
|
191
|
+
return "content-filter";
|
|
192
|
+
default:
|
|
193
|
+
return "other";
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// src/xai-chat-options.ts
|
|
198
|
+
import { z } from "zod/v4";
|
|
199
|
+
var webSourceSchema = z.object({
|
|
200
|
+
type: z.literal("web"),
|
|
201
|
+
country: z.string().length(2).optional(),
|
|
202
|
+
excludedWebsites: z.array(z.string()).max(5).optional(),
|
|
203
|
+
allowedWebsites: z.array(z.string()).max(5).optional(),
|
|
204
|
+
safeSearch: z.boolean().optional()
|
|
205
|
+
});
|
|
206
|
+
var xSourceSchema = z.object({
|
|
207
|
+
type: z.literal("x"),
|
|
208
|
+
excludedXHandles: z.array(z.string()).optional(),
|
|
209
|
+
includedXHandles: z.array(z.string()).optional(),
|
|
210
|
+
postFavoriteCount: z.number().int().optional(),
|
|
211
|
+
postViewCount: z.number().int().optional(),
|
|
212
|
+
/**
|
|
213
|
+
* @deprecated use `includedXHandles` instead
|
|
214
|
+
*/
|
|
215
|
+
xHandles: z.array(z.string()).optional()
|
|
216
|
+
});
|
|
217
|
+
var newsSourceSchema = z.object({
|
|
218
|
+
type: z.literal("news"),
|
|
219
|
+
country: z.string().length(2).optional(),
|
|
220
|
+
excludedWebsites: z.array(z.string()).max(5).optional(),
|
|
221
|
+
safeSearch: z.boolean().optional()
|
|
222
|
+
});
|
|
223
|
+
var rssSourceSchema = z.object({
|
|
224
|
+
type: z.literal("rss"),
|
|
225
|
+
links: z.array(z.string().url()).max(1)
|
|
226
|
+
// currently only supports one RSS link
|
|
227
|
+
});
|
|
228
|
+
var searchSourceSchema = z.discriminatedUnion("type", [
|
|
229
|
+
webSourceSchema,
|
|
230
|
+
xSourceSchema,
|
|
231
|
+
newsSourceSchema,
|
|
232
|
+
rssSourceSchema
|
|
233
|
+
]);
|
|
234
|
+
var xaiProviderOptions = z.object({
|
|
235
|
+
reasoningEffort: z.enum(["low", "high"]).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
|
+
}
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (toolChoice == null) {
|
|
328
|
+
return { tools: xaiTools2, toolChoice: void 0, toolWarnings };
|
|
329
|
+
}
|
|
330
|
+
const type = toolChoice.type;
|
|
331
|
+
switch (type) {
|
|
332
|
+
case "auto":
|
|
333
|
+
case "none":
|
|
334
|
+
return { tools: xaiTools2, toolChoice: type, toolWarnings };
|
|
335
|
+
case "required":
|
|
336
|
+
return { tools: xaiTools2, toolChoice: "required", toolWarnings };
|
|
337
|
+
case "tool":
|
|
338
|
+
return {
|
|
339
|
+
tools: xaiTools2,
|
|
340
|
+
toolChoice: {
|
|
341
|
+
type: "function",
|
|
342
|
+
function: { name: toolChoice.toolName }
|
|
343
|
+
},
|
|
344
|
+
toolWarnings
|
|
345
|
+
};
|
|
346
|
+
default: {
|
|
347
|
+
const _exhaustiveCheck = type;
|
|
348
|
+
throw new UnsupportedFunctionalityError2({
|
|
349
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// src/xai-chat-language-model.ts
|
|
356
|
+
var XaiChatLanguageModel = class {
|
|
357
|
+
constructor(modelId, config) {
|
|
358
|
+
this.specificationVersion = "v3";
|
|
359
|
+
this.supportedUrls = {
|
|
360
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
361
|
+
};
|
|
362
|
+
this.modelId = modelId;
|
|
363
|
+
this.config = config;
|
|
364
|
+
}
|
|
365
|
+
get provider() {
|
|
366
|
+
return this.config.provider;
|
|
367
|
+
}
|
|
368
|
+
async getArgs({
|
|
369
|
+
prompt,
|
|
370
|
+
maxOutputTokens,
|
|
371
|
+
temperature,
|
|
372
|
+
topP,
|
|
373
|
+
topK,
|
|
374
|
+
frequencyPenalty,
|
|
375
|
+
presencePenalty,
|
|
376
|
+
stopSequences,
|
|
377
|
+
seed,
|
|
378
|
+
responseFormat,
|
|
379
|
+
providerOptions,
|
|
380
|
+
tools,
|
|
381
|
+
toolChoice
|
|
382
|
+
}) {
|
|
383
|
+
var _a, _b, _c;
|
|
384
|
+
const warnings = [];
|
|
385
|
+
const options = (_a = await parseProviderOptions({
|
|
386
|
+
provider: "xai",
|
|
387
|
+
providerOptions,
|
|
388
|
+
schema: xaiProviderOptions
|
|
389
|
+
})) != null ? _a : {};
|
|
390
|
+
if (topK != null) {
|
|
391
|
+
warnings.push({ type: "unsupported", feature: "topK" });
|
|
392
|
+
}
|
|
393
|
+
if (frequencyPenalty != null) {
|
|
394
|
+
warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
|
|
395
|
+
}
|
|
396
|
+
if (presencePenalty != null) {
|
|
397
|
+
warnings.push({ type: "unsupported", feature: "presencePenalty" });
|
|
398
|
+
}
|
|
399
|
+
if (stopSequences != null) {
|
|
400
|
+
warnings.push({ type: "unsupported", feature: "stopSequences" });
|
|
401
|
+
}
|
|
402
|
+
const { messages, warnings: messageWarnings } = convertToXaiChatMessages(prompt);
|
|
403
|
+
warnings.push(...messageWarnings);
|
|
404
|
+
const {
|
|
405
|
+
tools: xaiTools2,
|
|
406
|
+
toolChoice: xaiToolChoice,
|
|
407
|
+
toolWarnings
|
|
408
|
+
} = prepareTools({
|
|
409
|
+
tools,
|
|
410
|
+
toolChoice
|
|
411
|
+
});
|
|
412
|
+
warnings.push(...toolWarnings);
|
|
413
|
+
const baseArgs = {
|
|
414
|
+
// model id
|
|
415
|
+
model: this.modelId,
|
|
416
|
+
// standard generation settings
|
|
417
|
+
max_completion_tokens: maxOutputTokens,
|
|
418
|
+
temperature,
|
|
419
|
+
top_p: topP,
|
|
420
|
+
seed,
|
|
421
|
+
reasoning_effort: options.reasoningEffort,
|
|
422
|
+
// parallel function calling
|
|
423
|
+
parallel_function_calling: options.parallel_function_calling,
|
|
424
|
+
// response format
|
|
425
|
+
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? responseFormat.schema != null ? {
|
|
426
|
+
type: "json_schema",
|
|
427
|
+
json_schema: {
|
|
428
|
+
name: (_b = responseFormat.name) != null ? _b : "response",
|
|
429
|
+
schema: responseFormat.schema,
|
|
430
|
+
strict: true
|
|
431
|
+
}
|
|
432
|
+
} : { type: "json_object" } : void 0,
|
|
433
|
+
// search parameters
|
|
434
|
+
search_parameters: options.searchParameters ? {
|
|
435
|
+
mode: options.searchParameters.mode,
|
|
436
|
+
return_citations: options.searchParameters.returnCitations,
|
|
437
|
+
from_date: options.searchParameters.fromDate,
|
|
438
|
+
to_date: options.searchParameters.toDate,
|
|
439
|
+
max_search_results: options.searchParameters.maxSearchResults,
|
|
440
|
+
sources: (_c = options.searchParameters.sources) == null ? void 0 : _c.map((source) => {
|
|
441
|
+
var _a2;
|
|
442
|
+
return {
|
|
443
|
+
type: source.type,
|
|
444
|
+
...source.type === "web" && {
|
|
445
|
+
country: source.country,
|
|
446
|
+
excluded_websites: source.excludedWebsites,
|
|
447
|
+
allowed_websites: source.allowedWebsites,
|
|
448
|
+
safe_search: source.safeSearch
|
|
449
|
+
},
|
|
450
|
+
...source.type === "x" && {
|
|
451
|
+
excluded_x_handles: source.excludedXHandles,
|
|
452
|
+
included_x_handles: (_a2 = source.includedXHandles) != null ? _a2 : source.xHandles,
|
|
453
|
+
post_favorite_count: source.postFavoriteCount,
|
|
454
|
+
post_view_count: source.postViewCount
|
|
455
|
+
},
|
|
456
|
+
...source.type === "news" && {
|
|
457
|
+
country: source.country,
|
|
458
|
+
excluded_websites: source.excludedWebsites,
|
|
459
|
+
safe_search: source.safeSearch
|
|
460
|
+
},
|
|
461
|
+
...source.type === "rss" && {
|
|
462
|
+
links: source.links
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
})
|
|
466
|
+
} : void 0,
|
|
467
|
+
// messages in xai format
|
|
468
|
+
messages,
|
|
469
|
+
// tools in xai format
|
|
470
|
+
tools: xaiTools2,
|
|
471
|
+
tool_choice: xaiToolChoice
|
|
472
|
+
};
|
|
473
|
+
return {
|
|
474
|
+
args: baseArgs,
|
|
475
|
+
warnings
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
async doGenerate(options) {
|
|
479
|
+
var _a, _b;
|
|
480
|
+
const { args: body, warnings } = await this.getArgs(options);
|
|
481
|
+
const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
|
|
482
|
+
const {
|
|
483
|
+
responseHeaders,
|
|
484
|
+
value: response,
|
|
485
|
+
rawValue: rawResponse
|
|
486
|
+
} = await postJsonToApi({
|
|
487
|
+
url,
|
|
488
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
489
|
+
body,
|
|
490
|
+
failedResponseHandler: xaiFailedResponseHandler,
|
|
491
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
492
|
+
xaiChatResponseSchema
|
|
493
|
+
),
|
|
494
|
+
abortSignal: options.abortSignal,
|
|
495
|
+
fetch: this.config.fetch
|
|
496
|
+
});
|
|
497
|
+
if (response.error != null) {
|
|
498
|
+
throw new APICallError({
|
|
499
|
+
message: response.error,
|
|
500
|
+
url,
|
|
501
|
+
requestBodyValues: body,
|
|
502
|
+
statusCode: 200,
|
|
503
|
+
responseHeaders,
|
|
504
|
+
responseBody: JSON.stringify(rawResponse),
|
|
505
|
+
isRetryable: response.code === "The service is currently unavailable"
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
const choice = response.choices[0];
|
|
509
|
+
const content = [];
|
|
510
|
+
if (choice.message.content != null && choice.message.content.length > 0) {
|
|
511
|
+
let text = choice.message.content;
|
|
512
|
+
const lastMessage = body.messages[body.messages.length - 1];
|
|
513
|
+
if ((lastMessage == null ? void 0 : lastMessage.role) === "assistant" && text === lastMessage.content) {
|
|
514
|
+
text = "";
|
|
515
|
+
}
|
|
516
|
+
if (text.length > 0) {
|
|
517
|
+
content.push({ type: "text", text });
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
if (choice.message.reasoning_content != null && choice.message.reasoning_content.length > 0) {
|
|
521
|
+
content.push({
|
|
522
|
+
type: "reasoning",
|
|
523
|
+
text: choice.message.reasoning_content
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
if (choice.message.tool_calls != null) {
|
|
527
|
+
for (const toolCall of choice.message.tool_calls) {
|
|
528
|
+
content.push({
|
|
529
|
+
type: "tool-call",
|
|
530
|
+
toolCallId: toolCall.id,
|
|
531
|
+
toolName: toolCall.function.name,
|
|
532
|
+
input: toolCall.function.arguments
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
if (response.citations != null) {
|
|
537
|
+
for (const url2 of response.citations) {
|
|
538
|
+
content.push({
|
|
539
|
+
type: "source",
|
|
540
|
+
sourceType: "url",
|
|
541
|
+
id: this.config.generateId(),
|
|
542
|
+
url: url2
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
return {
|
|
547
|
+
content,
|
|
548
|
+
finishReason: {
|
|
549
|
+
unified: mapXaiFinishReason(choice.finish_reason),
|
|
550
|
+
raw: (_b = choice.finish_reason) != null ? _b : void 0
|
|
551
|
+
},
|
|
552
|
+
usage: convertXaiChatUsage(response.usage),
|
|
553
|
+
// defined when there is no error
|
|
554
|
+
request: { body },
|
|
555
|
+
response: {
|
|
556
|
+
...getResponseMetadata(response),
|
|
557
|
+
headers: responseHeaders,
|
|
558
|
+
body: rawResponse
|
|
559
|
+
},
|
|
560
|
+
warnings
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
async doStream(options) {
|
|
564
|
+
var _a;
|
|
565
|
+
const { args, warnings } = await this.getArgs(options);
|
|
566
|
+
const body = {
|
|
567
|
+
...args,
|
|
568
|
+
stream: true,
|
|
569
|
+
stream_options: {
|
|
570
|
+
include_usage: true
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
|
|
574
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
575
|
+
url,
|
|
576
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
577
|
+
body,
|
|
578
|
+
failedResponseHandler: xaiFailedResponseHandler,
|
|
579
|
+
successfulResponseHandler: async ({ response: response2 }) => {
|
|
580
|
+
const responseHeaders2 = extractResponseHeaders(response2);
|
|
581
|
+
const contentType = response2.headers.get("content-type");
|
|
582
|
+
if (contentType == null ? void 0 : contentType.includes("application/json")) {
|
|
583
|
+
const responseBody = await response2.text();
|
|
584
|
+
const parsedError = await safeParseJSON({
|
|
585
|
+
text: responseBody,
|
|
586
|
+
schema: xaiStreamErrorSchema
|
|
587
|
+
});
|
|
588
|
+
if (parsedError.success) {
|
|
589
|
+
throw new APICallError({
|
|
590
|
+
message: parsedError.value.error,
|
|
591
|
+
url,
|
|
592
|
+
requestBodyValues: body,
|
|
593
|
+
statusCode: 200,
|
|
594
|
+
responseHeaders: responseHeaders2,
|
|
595
|
+
responseBody,
|
|
596
|
+
isRetryable: parsedError.value.code === "The service is currently unavailable"
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
throw new APICallError({
|
|
600
|
+
message: "Invalid JSON response",
|
|
601
|
+
url,
|
|
602
|
+
requestBodyValues: body,
|
|
603
|
+
statusCode: 200,
|
|
604
|
+
responseHeaders: responseHeaders2,
|
|
605
|
+
responseBody
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
return createEventSourceResponseHandler(xaiChatChunkSchema)({
|
|
609
|
+
response: response2,
|
|
610
|
+
url,
|
|
611
|
+
requestBodyValues: body
|
|
612
|
+
});
|
|
613
|
+
},
|
|
614
|
+
abortSignal: options.abortSignal,
|
|
615
|
+
fetch: this.config.fetch
|
|
616
|
+
});
|
|
617
|
+
let finishReason = {
|
|
618
|
+
unified: "other",
|
|
619
|
+
raw: void 0
|
|
620
|
+
};
|
|
621
|
+
let usage = void 0;
|
|
622
|
+
let isFirstChunk = true;
|
|
623
|
+
const contentBlocks = {};
|
|
624
|
+
const lastReasoningDeltas = {};
|
|
625
|
+
let activeReasoningBlockId = void 0;
|
|
626
|
+
const self = this;
|
|
627
|
+
return {
|
|
628
|
+
stream: response.pipeThrough(
|
|
629
|
+
new TransformStream({
|
|
630
|
+
start(controller) {
|
|
631
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
632
|
+
},
|
|
633
|
+
transform(chunk, controller) {
|
|
634
|
+
if (options.includeRawChunks) {
|
|
635
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
636
|
+
}
|
|
637
|
+
if (!chunk.success) {
|
|
638
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
const value = chunk.value;
|
|
642
|
+
if (isFirstChunk) {
|
|
643
|
+
controller.enqueue({
|
|
644
|
+
type: "response-metadata",
|
|
645
|
+
...getResponseMetadata(value)
|
|
646
|
+
});
|
|
647
|
+
isFirstChunk = false;
|
|
648
|
+
}
|
|
649
|
+
if (value.citations != null) {
|
|
650
|
+
for (const url2 of value.citations) {
|
|
651
|
+
controller.enqueue({
|
|
652
|
+
type: "source",
|
|
653
|
+
sourceType: "url",
|
|
654
|
+
id: self.config.generateId(),
|
|
655
|
+
url: url2
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
if (value.usage != null) {
|
|
660
|
+
usage = convertXaiChatUsage(value.usage);
|
|
661
|
+
}
|
|
662
|
+
const choice = value.choices[0];
|
|
663
|
+
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
664
|
+
finishReason = {
|
|
665
|
+
unified: mapXaiFinishReason(choice.finish_reason),
|
|
666
|
+
raw: choice.finish_reason
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
if ((choice == null ? void 0 : choice.delta) == null) {
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
const delta = choice.delta;
|
|
673
|
+
const choiceIndex = choice.index;
|
|
674
|
+
if (delta.content != null && delta.content.length > 0) {
|
|
675
|
+
const textContent = delta.content;
|
|
676
|
+
if (activeReasoningBlockId != null && !contentBlocks[activeReasoningBlockId].ended) {
|
|
677
|
+
controller.enqueue({
|
|
678
|
+
type: "reasoning-end",
|
|
679
|
+
id: activeReasoningBlockId
|
|
680
|
+
});
|
|
681
|
+
contentBlocks[activeReasoningBlockId].ended = true;
|
|
682
|
+
activeReasoningBlockId = void 0;
|
|
683
|
+
}
|
|
684
|
+
const lastMessage = body.messages[body.messages.length - 1];
|
|
685
|
+
if ((lastMessage == null ? void 0 : lastMessage.role) === "assistant" && textContent === lastMessage.content) {
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
const blockId = `text-${value.id || choiceIndex}`;
|
|
689
|
+
if (contentBlocks[blockId] == null) {
|
|
690
|
+
contentBlocks[blockId] = { type: "text", ended: false };
|
|
691
|
+
controller.enqueue({
|
|
692
|
+
type: "text-start",
|
|
693
|
+
id: blockId
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
controller.enqueue({
|
|
697
|
+
type: "text-delta",
|
|
698
|
+
id: blockId,
|
|
699
|
+
delta: textContent
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
if (delta.reasoning_content != null && delta.reasoning_content.length > 0) {
|
|
703
|
+
const blockId = `reasoning-${value.id || choiceIndex}`;
|
|
704
|
+
if (lastReasoningDeltas[blockId] === delta.reasoning_content) {
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
lastReasoningDeltas[blockId] = delta.reasoning_content;
|
|
708
|
+
if (contentBlocks[blockId] == null) {
|
|
709
|
+
contentBlocks[blockId] = { type: "reasoning", ended: false };
|
|
710
|
+
activeReasoningBlockId = blockId;
|
|
711
|
+
controller.enqueue({
|
|
712
|
+
type: "reasoning-start",
|
|
713
|
+
id: blockId
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
controller.enqueue({
|
|
717
|
+
type: "reasoning-delta",
|
|
718
|
+
id: blockId,
|
|
719
|
+
delta: delta.reasoning_content
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
if (delta.tool_calls != null) {
|
|
723
|
+
if (activeReasoningBlockId != null && !contentBlocks[activeReasoningBlockId].ended) {
|
|
724
|
+
controller.enqueue({
|
|
725
|
+
type: "reasoning-end",
|
|
726
|
+
id: activeReasoningBlockId
|
|
727
|
+
});
|
|
728
|
+
contentBlocks[activeReasoningBlockId].ended = true;
|
|
729
|
+
activeReasoningBlockId = void 0;
|
|
730
|
+
}
|
|
731
|
+
for (const toolCall of delta.tool_calls) {
|
|
732
|
+
const toolCallId = toolCall.id;
|
|
733
|
+
controller.enqueue({
|
|
734
|
+
type: "tool-input-start",
|
|
735
|
+
id: toolCallId,
|
|
736
|
+
toolName: toolCall.function.name
|
|
737
|
+
});
|
|
738
|
+
controller.enqueue({
|
|
739
|
+
type: "tool-input-delta",
|
|
740
|
+
id: toolCallId,
|
|
741
|
+
delta: toolCall.function.arguments
|
|
742
|
+
});
|
|
743
|
+
controller.enqueue({
|
|
744
|
+
type: "tool-input-end",
|
|
745
|
+
id: toolCallId
|
|
746
|
+
});
|
|
747
|
+
controller.enqueue({
|
|
748
|
+
type: "tool-call",
|
|
749
|
+
toolCallId,
|
|
750
|
+
toolName: toolCall.function.name,
|
|
751
|
+
input: toolCall.function.arguments
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
},
|
|
756
|
+
flush(controller) {
|
|
757
|
+
for (const [blockId, block] of Object.entries(contentBlocks)) {
|
|
758
|
+
if (!block.ended) {
|
|
759
|
+
controller.enqueue({
|
|
760
|
+
type: block.type === "text" ? "text-end" : "reasoning-end",
|
|
761
|
+
id: blockId
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
controller.enqueue({ type: "finish", finishReason, usage });
|
|
766
|
+
}
|
|
767
|
+
})
|
|
768
|
+
),
|
|
769
|
+
request: { body },
|
|
770
|
+
response: { headers: responseHeaders }
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
var xaiUsageSchema = z3.object({
|
|
775
|
+
prompt_tokens: z3.number(),
|
|
776
|
+
completion_tokens: z3.number(),
|
|
777
|
+
total_tokens: z3.number(),
|
|
778
|
+
prompt_tokens_details: z3.object({
|
|
779
|
+
text_tokens: z3.number().nullish(),
|
|
780
|
+
audio_tokens: z3.number().nullish(),
|
|
781
|
+
image_tokens: z3.number().nullish(),
|
|
782
|
+
cached_tokens: z3.number().nullish()
|
|
783
|
+
}).nullish(),
|
|
784
|
+
completion_tokens_details: z3.object({
|
|
785
|
+
reasoning_tokens: z3.number().nullish(),
|
|
786
|
+
audio_tokens: z3.number().nullish(),
|
|
787
|
+
accepted_prediction_tokens: z3.number().nullish(),
|
|
788
|
+
rejected_prediction_tokens: z3.number().nullish()
|
|
789
|
+
}).nullish()
|
|
790
|
+
});
|
|
791
|
+
var xaiChatResponseSchema = z3.object({
|
|
792
|
+
id: z3.string().nullish(),
|
|
793
|
+
created: z3.number().nullish(),
|
|
794
|
+
model: z3.string().nullish(),
|
|
795
|
+
choices: z3.array(
|
|
796
|
+
z3.object({
|
|
797
|
+
message: z3.object({
|
|
798
|
+
role: z3.literal("assistant"),
|
|
799
|
+
content: z3.string().nullish(),
|
|
800
|
+
reasoning_content: z3.string().nullish(),
|
|
801
|
+
tool_calls: z3.array(
|
|
802
|
+
z3.object({
|
|
803
|
+
id: z3.string(),
|
|
804
|
+
type: z3.literal("function"),
|
|
805
|
+
function: z3.object({
|
|
806
|
+
name: z3.string(),
|
|
807
|
+
arguments: z3.string()
|
|
808
|
+
})
|
|
809
|
+
})
|
|
810
|
+
).nullish()
|
|
811
|
+
}),
|
|
812
|
+
index: z3.number(),
|
|
813
|
+
finish_reason: z3.string().nullish()
|
|
814
|
+
})
|
|
815
|
+
).nullish(),
|
|
816
|
+
object: z3.literal("chat.completion").nullish(),
|
|
817
|
+
usage: xaiUsageSchema.nullish(),
|
|
818
|
+
citations: z3.array(z3.string().url()).nullish(),
|
|
819
|
+
code: z3.string().nullish(),
|
|
820
|
+
error: z3.string().nullish()
|
|
821
|
+
});
|
|
822
|
+
var xaiChatChunkSchema = z3.object({
|
|
823
|
+
id: z3.string().nullish(),
|
|
824
|
+
created: z3.number().nullish(),
|
|
825
|
+
model: z3.string().nullish(),
|
|
826
|
+
choices: z3.array(
|
|
827
|
+
z3.object({
|
|
828
|
+
delta: z3.object({
|
|
829
|
+
role: z3.enum(["assistant"]).optional(),
|
|
830
|
+
content: z3.string().nullish(),
|
|
831
|
+
reasoning_content: z3.string().nullish(),
|
|
832
|
+
tool_calls: z3.array(
|
|
833
|
+
z3.object({
|
|
834
|
+
id: z3.string(),
|
|
835
|
+
type: z3.literal("function"),
|
|
836
|
+
function: z3.object({
|
|
837
|
+
name: z3.string(),
|
|
838
|
+
arguments: z3.string()
|
|
839
|
+
})
|
|
840
|
+
})
|
|
841
|
+
).nullish()
|
|
842
|
+
}),
|
|
843
|
+
finish_reason: z3.string().nullish(),
|
|
844
|
+
index: z3.number()
|
|
845
|
+
})
|
|
846
|
+
),
|
|
847
|
+
usage: xaiUsageSchema.nullish(),
|
|
848
|
+
citations: z3.array(z3.string().url()).nullish()
|
|
849
|
+
});
|
|
850
|
+
var xaiStreamErrorSchema = z3.object({
|
|
851
|
+
code: z3.string(),
|
|
852
|
+
error: z3.string()
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
// src/responses/xai-responses-language-model.ts
|
|
856
|
+
import {
|
|
857
|
+
combineHeaders as combineHeaders2,
|
|
858
|
+
createEventSourceResponseHandler as createEventSourceResponseHandler2,
|
|
859
|
+
createJsonResponseHandler as createJsonResponseHandler2,
|
|
860
|
+
parseProviderOptions as parseProviderOptions2,
|
|
861
|
+
postJsonToApi as postJsonToApi2
|
|
862
|
+
} from "@ai-sdk/provider-utils";
|
|
863
|
+
|
|
864
|
+
// src/responses/convert-to-xai-responses-input.ts
|
|
865
|
+
import {
|
|
866
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
867
|
+
} from "@ai-sdk/provider";
|
|
868
|
+
import { convertToBase64 as convertToBase642 } from "@ai-sdk/provider-utils";
|
|
869
|
+
async function convertToXaiResponsesInput({
|
|
870
|
+
prompt
|
|
871
|
+
}) {
|
|
872
|
+
var _a, _b, _c, _d, _e;
|
|
873
|
+
const input = [];
|
|
874
|
+
const inputWarnings = [];
|
|
875
|
+
for (const message of prompt) {
|
|
876
|
+
switch (message.role) {
|
|
877
|
+
case "system": {
|
|
878
|
+
input.push({
|
|
879
|
+
role: "system",
|
|
880
|
+
content: message.content
|
|
881
|
+
});
|
|
882
|
+
break;
|
|
883
|
+
}
|
|
884
|
+
case "user": {
|
|
885
|
+
const contentParts = [];
|
|
886
|
+
for (const block of message.content) {
|
|
887
|
+
switch (block.type) {
|
|
888
|
+
case "text": {
|
|
889
|
+
contentParts.push({ type: "input_text", text: block.text });
|
|
890
|
+
break;
|
|
891
|
+
}
|
|
892
|
+
case "file": {
|
|
893
|
+
if (block.mediaType.startsWith("image/")) {
|
|
894
|
+
const mediaType = block.mediaType === "image/*" ? "image/jpeg" : block.mediaType;
|
|
895
|
+
const imageUrl = block.data instanceof URL ? block.data.toString() : `data:${mediaType};base64,${convertToBase642(block.data)}`;
|
|
896
|
+
contentParts.push({ type: "input_image", image_url: imageUrl });
|
|
897
|
+
} else {
|
|
898
|
+
throw new UnsupportedFunctionalityError3({
|
|
899
|
+
functionality: `file part media type ${block.mediaType}`
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
break;
|
|
903
|
+
}
|
|
904
|
+
default: {
|
|
905
|
+
const _exhaustiveCheck = block;
|
|
906
|
+
inputWarnings.push({
|
|
907
|
+
type: "other",
|
|
908
|
+
message: "xAI Responses API does not support this content type in user messages"
|
|
909
|
+
});
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
input.push({
|
|
914
|
+
role: "user",
|
|
915
|
+
content: contentParts
|
|
916
|
+
});
|
|
917
|
+
break;
|
|
918
|
+
}
|
|
919
|
+
case "assistant": {
|
|
920
|
+
for (const part of message.content) {
|
|
921
|
+
switch (part.type) {
|
|
922
|
+
case "text": {
|
|
923
|
+
const id = typeof ((_b = (_a = part.providerOptions) == null ? void 0 : _a.xai) == null ? void 0 : _b.itemId) === "string" ? part.providerOptions.xai.itemId : void 0;
|
|
924
|
+
input.push({
|
|
925
|
+
role: "assistant",
|
|
926
|
+
content: part.text,
|
|
927
|
+
id
|
|
928
|
+
});
|
|
929
|
+
break;
|
|
930
|
+
}
|
|
931
|
+
case "tool-call": {
|
|
932
|
+
if (part.providerExecuted) {
|
|
933
|
+
break;
|
|
934
|
+
}
|
|
935
|
+
const id = typeof ((_d = (_c = part.providerOptions) == null ? void 0 : _c.xai) == null ? void 0 : _d.itemId) === "string" ? part.providerOptions.xai.itemId : void 0;
|
|
936
|
+
input.push({
|
|
937
|
+
type: "function_call",
|
|
938
|
+
id: id != null ? id : part.toolCallId,
|
|
939
|
+
call_id: part.toolCallId,
|
|
940
|
+
name: part.toolName,
|
|
941
|
+
arguments: JSON.stringify(part.input),
|
|
942
|
+
status: "completed"
|
|
943
|
+
});
|
|
944
|
+
break;
|
|
945
|
+
}
|
|
946
|
+
case "tool-result": {
|
|
947
|
+
break;
|
|
948
|
+
}
|
|
949
|
+
case "reasoning":
|
|
950
|
+
case "file": {
|
|
951
|
+
inputWarnings.push({
|
|
952
|
+
type: "other",
|
|
953
|
+
message: `xAI Responses API does not support ${part.type} in assistant messages`
|
|
954
|
+
});
|
|
955
|
+
break;
|
|
956
|
+
}
|
|
957
|
+
default: {
|
|
958
|
+
const _exhaustiveCheck = part;
|
|
959
|
+
inputWarnings.push({
|
|
960
|
+
type: "other",
|
|
961
|
+
message: "xAI Responses API does not support this content type in assistant messages"
|
|
962
|
+
});
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
break;
|
|
967
|
+
}
|
|
968
|
+
case "tool": {
|
|
969
|
+
for (const part of message.content) {
|
|
970
|
+
if (part.type === "tool-approval-response") {
|
|
971
|
+
continue;
|
|
972
|
+
}
|
|
973
|
+
const output = part.output;
|
|
974
|
+
let outputValue;
|
|
975
|
+
switch (output.type) {
|
|
976
|
+
case "text":
|
|
977
|
+
case "error-text":
|
|
978
|
+
outputValue = output.value;
|
|
979
|
+
break;
|
|
980
|
+
case "execution-denied":
|
|
981
|
+
outputValue = (_e = output.reason) != null ? _e : "tool execution denied";
|
|
982
|
+
break;
|
|
983
|
+
case "json":
|
|
984
|
+
case "error-json":
|
|
985
|
+
outputValue = JSON.stringify(output.value);
|
|
986
|
+
break;
|
|
987
|
+
case "content":
|
|
988
|
+
outputValue = output.value.map((item) => {
|
|
989
|
+
if (item.type === "text") {
|
|
990
|
+
return item.text;
|
|
991
|
+
}
|
|
992
|
+
return "";
|
|
993
|
+
}).join("");
|
|
994
|
+
break;
|
|
995
|
+
default: {
|
|
996
|
+
const _exhaustiveCheck = output;
|
|
997
|
+
outputValue = "";
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
input.push({
|
|
1001
|
+
type: "function_call_output",
|
|
1002
|
+
call_id: part.toolCallId,
|
|
1003
|
+
output: outputValue
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
break;
|
|
1007
|
+
}
|
|
1008
|
+
default: {
|
|
1009
|
+
const _exhaustiveCheck = message;
|
|
1010
|
+
inputWarnings.push({
|
|
1011
|
+
type: "other",
|
|
1012
|
+
message: "unsupported message role"
|
|
1013
|
+
});
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
return { input, inputWarnings };
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
// src/responses/convert-xai-responses-usage.ts
|
|
1021
|
+
function convertXaiResponsesUsage(usage) {
|
|
1022
|
+
var _a, _b, _c, _d;
|
|
1023
|
+
const cacheReadTokens = (_b = (_a = usage.input_tokens_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0;
|
|
1024
|
+
const reasoningTokens = (_d = (_c = usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0;
|
|
1025
|
+
return {
|
|
1026
|
+
inputTokens: {
|
|
1027
|
+
total: usage.input_tokens,
|
|
1028
|
+
noCache: usage.input_tokens - cacheReadTokens,
|
|
1029
|
+
cacheRead: cacheReadTokens,
|
|
1030
|
+
cacheWrite: void 0
|
|
1031
|
+
},
|
|
1032
|
+
outputTokens: {
|
|
1033
|
+
total: usage.output_tokens,
|
|
1034
|
+
text: usage.output_tokens - reasoningTokens,
|
|
1035
|
+
reasoning: reasoningTokens
|
|
1036
|
+
},
|
|
1037
|
+
raw: usage
|
|
1038
|
+
};
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
// src/responses/map-xai-responses-finish-reason.ts
|
|
1042
|
+
function mapXaiResponsesFinishReason(finishReason) {
|
|
1043
|
+
switch (finishReason) {
|
|
1044
|
+
case "stop":
|
|
1045
|
+
case "completed":
|
|
1046
|
+
return "stop";
|
|
1047
|
+
case "length":
|
|
1048
|
+
return "length";
|
|
1049
|
+
case "tool_calls":
|
|
1050
|
+
case "function_call":
|
|
1051
|
+
return "tool-calls";
|
|
1052
|
+
case "content_filter":
|
|
1053
|
+
return "content-filter";
|
|
1054
|
+
default:
|
|
1055
|
+
return "other";
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
// src/responses/xai-responses-api.ts
|
|
1060
|
+
import { z as z4 } from "zod/v4";
|
|
1061
|
+
var annotationSchema = z4.union([
|
|
1062
|
+
z4.object({
|
|
1063
|
+
type: z4.literal("url_citation"),
|
|
1064
|
+
url: z4.string(),
|
|
1065
|
+
title: z4.string().optional()
|
|
1066
|
+
}),
|
|
1067
|
+
z4.object({
|
|
1068
|
+
type: z4.string()
|
|
1069
|
+
})
|
|
1070
|
+
]);
|
|
1071
|
+
var messageContentPartSchema = z4.object({
|
|
1072
|
+
type: z4.string(),
|
|
1073
|
+
text: z4.string().optional(),
|
|
1074
|
+
logprobs: z4.array(z4.any()).optional(),
|
|
1075
|
+
annotations: z4.array(annotationSchema).optional()
|
|
1076
|
+
});
|
|
1077
|
+
var reasoningSummaryPartSchema = z4.object({
|
|
1078
|
+
type: z4.string(),
|
|
1079
|
+
text: z4.string()
|
|
1080
|
+
});
|
|
1081
|
+
var toolCallSchema = z4.object({
|
|
1082
|
+
name: z4.string().optional(),
|
|
1083
|
+
arguments: z4.string().optional(),
|
|
1084
|
+
input: z4.string().optional(),
|
|
1085
|
+
call_id: z4.string().optional(),
|
|
1086
|
+
id: z4.string(),
|
|
1087
|
+
status: z4.string(),
|
|
1088
|
+
action: z4.any().optional()
|
|
1089
|
+
});
|
|
1090
|
+
var outputItemSchema = z4.discriminatedUnion("type", [
|
|
1091
|
+
z4.object({
|
|
1092
|
+
type: z4.literal("web_search_call"),
|
|
1093
|
+
...toolCallSchema.shape
|
|
1094
|
+
}),
|
|
1095
|
+
z4.object({
|
|
1096
|
+
type: z4.literal("x_search_call"),
|
|
1097
|
+
...toolCallSchema.shape
|
|
1098
|
+
}),
|
|
1099
|
+
z4.object({
|
|
1100
|
+
type: z4.literal("code_interpreter_call"),
|
|
1101
|
+
...toolCallSchema.shape
|
|
1102
|
+
}),
|
|
1103
|
+
z4.object({
|
|
1104
|
+
type: z4.literal("code_execution_call"),
|
|
1105
|
+
...toolCallSchema.shape
|
|
1106
|
+
}),
|
|
1107
|
+
z4.object({
|
|
1108
|
+
type: z4.literal("view_image_call"),
|
|
1109
|
+
...toolCallSchema.shape
|
|
1110
|
+
}),
|
|
1111
|
+
z4.object({
|
|
1112
|
+
type: z4.literal("view_x_video_call"),
|
|
1113
|
+
...toolCallSchema.shape
|
|
1114
|
+
}),
|
|
1115
|
+
z4.object({
|
|
1116
|
+
type: z4.literal("custom_tool_call"),
|
|
1117
|
+
...toolCallSchema.shape
|
|
1118
|
+
}),
|
|
1119
|
+
z4.object({
|
|
1120
|
+
type: z4.literal("message"),
|
|
1121
|
+
role: z4.string(),
|
|
1122
|
+
content: z4.array(messageContentPartSchema),
|
|
1123
|
+
id: z4.string(),
|
|
1124
|
+
status: z4.string()
|
|
1125
|
+
}),
|
|
1126
|
+
z4.object({
|
|
1127
|
+
type: z4.literal("function_call"),
|
|
1128
|
+
name: z4.string(),
|
|
1129
|
+
arguments: z4.string(),
|
|
1130
|
+
call_id: z4.string(),
|
|
1131
|
+
id: z4.string()
|
|
1132
|
+
}),
|
|
1133
|
+
z4.object({
|
|
1134
|
+
type: z4.literal("reasoning"),
|
|
1135
|
+
id: z4.string(),
|
|
1136
|
+
summary: z4.array(reasoningSummaryPartSchema),
|
|
1137
|
+
status: z4.string(),
|
|
1138
|
+
encrypted_content: z4.string().nullish()
|
|
1139
|
+
})
|
|
1140
|
+
]);
|
|
1141
|
+
var xaiResponsesUsageSchema = z4.object({
|
|
1142
|
+
input_tokens: z4.number(),
|
|
1143
|
+
output_tokens: z4.number(),
|
|
1144
|
+
total_tokens: z4.number().optional(),
|
|
1145
|
+
input_tokens_details: z4.object({
|
|
1146
|
+
cached_tokens: z4.number().optional()
|
|
1147
|
+
}).optional(),
|
|
1148
|
+
output_tokens_details: z4.object({
|
|
1149
|
+
reasoning_tokens: z4.number().optional()
|
|
1150
|
+
}).optional(),
|
|
1151
|
+
num_sources_used: z4.number().optional(),
|
|
1152
|
+
num_server_side_tools_used: z4.number().optional()
|
|
1153
|
+
});
|
|
1154
|
+
var xaiResponsesResponseSchema = z4.object({
|
|
1155
|
+
id: z4.string().nullish(),
|
|
1156
|
+
created_at: z4.number().nullish(),
|
|
1157
|
+
model: z4.string().nullish(),
|
|
1158
|
+
object: z4.literal("response"),
|
|
1159
|
+
output: z4.array(outputItemSchema),
|
|
1160
|
+
usage: xaiResponsesUsageSchema,
|
|
1161
|
+
status: z4.string()
|
|
1162
|
+
});
|
|
1163
|
+
var xaiResponsesChunkSchema = z4.union([
|
|
1164
|
+
z4.object({
|
|
1165
|
+
type: z4.literal("response.created"),
|
|
1166
|
+
response: xaiResponsesResponseSchema.partial({ usage: true, status: true })
|
|
1167
|
+
}),
|
|
1168
|
+
z4.object({
|
|
1169
|
+
type: z4.literal("response.in_progress"),
|
|
1170
|
+
response: xaiResponsesResponseSchema.partial({ usage: true, status: true })
|
|
1171
|
+
}),
|
|
1172
|
+
z4.object({
|
|
1173
|
+
type: z4.literal("response.output_item.added"),
|
|
1174
|
+
item: outputItemSchema,
|
|
1175
|
+
output_index: z4.number()
|
|
1176
|
+
}),
|
|
1177
|
+
z4.object({
|
|
1178
|
+
type: z4.literal("response.output_item.done"),
|
|
1179
|
+
item: outputItemSchema,
|
|
1180
|
+
output_index: z4.number()
|
|
1181
|
+
}),
|
|
1182
|
+
z4.object({
|
|
1183
|
+
type: z4.literal("response.content_part.added"),
|
|
1184
|
+
item_id: z4.string(),
|
|
1185
|
+
output_index: z4.number(),
|
|
1186
|
+
content_index: z4.number(),
|
|
1187
|
+
part: messageContentPartSchema
|
|
1188
|
+
}),
|
|
1189
|
+
z4.object({
|
|
1190
|
+
type: z4.literal("response.content_part.done"),
|
|
1191
|
+
item_id: z4.string(),
|
|
1192
|
+
output_index: z4.number(),
|
|
1193
|
+
content_index: z4.number(),
|
|
1194
|
+
part: messageContentPartSchema
|
|
1195
|
+
}),
|
|
1196
|
+
z4.object({
|
|
1197
|
+
type: z4.literal("response.output_text.delta"),
|
|
1198
|
+
item_id: z4.string(),
|
|
1199
|
+
output_index: z4.number(),
|
|
1200
|
+
content_index: z4.number(),
|
|
1201
|
+
delta: z4.string(),
|
|
1202
|
+
logprobs: z4.array(z4.any()).optional()
|
|
1203
|
+
}),
|
|
1204
|
+
z4.object({
|
|
1205
|
+
type: z4.literal("response.output_text.done"),
|
|
1206
|
+
item_id: z4.string(),
|
|
1207
|
+
output_index: z4.number(),
|
|
1208
|
+
content_index: z4.number(),
|
|
1209
|
+
text: z4.string(),
|
|
1210
|
+
logprobs: z4.array(z4.any()).optional(),
|
|
1211
|
+
annotations: z4.array(annotationSchema).optional()
|
|
1212
|
+
}),
|
|
1213
|
+
z4.object({
|
|
1214
|
+
type: z4.literal("response.output_text.annotation.added"),
|
|
1215
|
+
item_id: z4.string(),
|
|
1216
|
+
output_index: z4.number(),
|
|
1217
|
+
content_index: z4.number(),
|
|
1218
|
+
annotation_index: z4.number(),
|
|
1219
|
+
annotation: annotationSchema
|
|
1220
|
+
}),
|
|
1221
|
+
z4.object({
|
|
1222
|
+
type: z4.literal("response.reasoning_summary_part.added"),
|
|
1223
|
+
item_id: z4.string(),
|
|
1224
|
+
output_index: z4.number(),
|
|
1225
|
+
summary_index: z4.number(),
|
|
1226
|
+
part: reasoningSummaryPartSchema
|
|
1227
|
+
}),
|
|
1228
|
+
z4.object({
|
|
1229
|
+
type: z4.literal("response.reasoning_summary_part.done"),
|
|
1230
|
+
item_id: z4.string(),
|
|
1231
|
+
output_index: z4.number(),
|
|
1232
|
+
summary_index: z4.number(),
|
|
1233
|
+
part: reasoningSummaryPartSchema
|
|
1234
|
+
}),
|
|
1235
|
+
z4.object({
|
|
1236
|
+
type: z4.literal("response.reasoning_summary_text.delta"),
|
|
1237
|
+
item_id: z4.string(),
|
|
1238
|
+
output_index: z4.number(),
|
|
1239
|
+
summary_index: z4.number(),
|
|
1240
|
+
delta: z4.string()
|
|
1241
|
+
}),
|
|
1242
|
+
z4.object({
|
|
1243
|
+
type: z4.literal("response.reasoning_summary_text.done"),
|
|
1244
|
+
item_id: z4.string(),
|
|
1245
|
+
output_index: z4.number(),
|
|
1246
|
+
summary_index: z4.number(),
|
|
1247
|
+
text: z4.string()
|
|
1248
|
+
}),
|
|
1249
|
+
z4.object({
|
|
1250
|
+
type: z4.literal("response.web_search_call.in_progress"),
|
|
1251
|
+
item_id: z4.string(),
|
|
1252
|
+
output_index: z4.number()
|
|
1253
|
+
}),
|
|
1254
|
+
z4.object({
|
|
1255
|
+
type: z4.literal("response.web_search_call.searching"),
|
|
1256
|
+
item_id: z4.string(),
|
|
1257
|
+
output_index: z4.number()
|
|
1258
|
+
}),
|
|
1259
|
+
z4.object({
|
|
1260
|
+
type: z4.literal("response.web_search_call.completed"),
|
|
1261
|
+
item_id: z4.string(),
|
|
1262
|
+
output_index: z4.number()
|
|
1263
|
+
}),
|
|
1264
|
+
z4.object({
|
|
1265
|
+
type: z4.literal("response.x_search_call.in_progress"),
|
|
1266
|
+
item_id: z4.string(),
|
|
1267
|
+
output_index: z4.number()
|
|
1268
|
+
}),
|
|
1269
|
+
z4.object({
|
|
1270
|
+
type: z4.literal("response.x_search_call.searching"),
|
|
1271
|
+
item_id: z4.string(),
|
|
1272
|
+
output_index: z4.number()
|
|
1273
|
+
}),
|
|
1274
|
+
z4.object({
|
|
1275
|
+
type: z4.literal("response.x_search_call.completed"),
|
|
1276
|
+
item_id: z4.string(),
|
|
1277
|
+
output_index: z4.number()
|
|
1278
|
+
}),
|
|
1279
|
+
z4.object({
|
|
1280
|
+
type: z4.literal("response.custom_tool_call_input.done"),
|
|
1281
|
+
item_id: z4.string(),
|
|
1282
|
+
output_index: z4.number()
|
|
1283
|
+
}),
|
|
1284
|
+
z4.object({
|
|
1285
|
+
type: z4.literal("response.custom_tool_call_input.delta"),
|
|
1286
|
+
item_id: z4.string(),
|
|
1287
|
+
output_index: z4.number()
|
|
1288
|
+
}),
|
|
1289
|
+
z4.object({
|
|
1290
|
+
type: z4.literal("response.code_execution_call.in_progress"),
|
|
1291
|
+
item_id: z4.string(),
|
|
1292
|
+
output_index: z4.number()
|
|
1293
|
+
}),
|
|
1294
|
+
z4.object({
|
|
1295
|
+
type: z4.literal("response.code_execution_call.executing"),
|
|
1296
|
+
item_id: z4.string(),
|
|
1297
|
+
output_index: z4.number()
|
|
1298
|
+
}),
|
|
1299
|
+
z4.object({
|
|
1300
|
+
type: z4.literal("response.code_execution_call.completed"),
|
|
1301
|
+
item_id: z4.string(),
|
|
1302
|
+
output_index: z4.number()
|
|
1303
|
+
}),
|
|
1304
|
+
z4.object({
|
|
1305
|
+
type: z4.literal("response.code_interpreter_call.in_progress"),
|
|
1306
|
+
item_id: z4.string(),
|
|
1307
|
+
output_index: z4.number()
|
|
1308
|
+
}),
|
|
1309
|
+
z4.object({
|
|
1310
|
+
type: z4.literal("response.code_interpreter_call.executing"),
|
|
1311
|
+
item_id: z4.string(),
|
|
1312
|
+
output_index: z4.number()
|
|
1313
|
+
}),
|
|
1314
|
+
z4.object({
|
|
1315
|
+
type: z4.literal("response.code_interpreter_call.interpreting"),
|
|
1316
|
+
item_id: z4.string(),
|
|
1317
|
+
output_index: z4.number()
|
|
1318
|
+
}),
|
|
1319
|
+
z4.object({
|
|
1320
|
+
type: z4.literal("response.code_interpreter_call.completed"),
|
|
1321
|
+
item_id: z4.string(),
|
|
1322
|
+
output_index: z4.number()
|
|
1323
|
+
}),
|
|
1324
|
+
// Code interpreter code streaming events
|
|
1325
|
+
z4.object({
|
|
1326
|
+
type: z4.literal("response.code_interpreter_call_code.delta"),
|
|
1327
|
+
item_id: z4.string(),
|
|
1328
|
+
output_index: z4.number(),
|
|
1329
|
+
delta: z4.string()
|
|
1330
|
+
}),
|
|
1331
|
+
z4.object({
|
|
1332
|
+
type: z4.literal("response.code_interpreter_call_code.done"),
|
|
1333
|
+
item_id: z4.string(),
|
|
1334
|
+
output_index: z4.number(),
|
|
1335
|
+
code: z4.string()
|
|
1336
|
+
}),
|
|
1337
|
+
z4.object({
|
|
1338
|
+
type: z4.literal("response.done"),
|
|
1339
|
+
response: xaiResponsesResponseSchema
|
|
1340
|
+
}),
|
|
1341
|
+
z4.object({
|
|
1342
|
+
type: z4.literal("response.completed"),
|
|
1343
|
+
response: xaiResponsesResponseSchema
|
|
1344
|
+
})
|
|
1345
|
+
]);
|
|
1346
|
+
|
|
1347
|
+
// src/responses/xai-responses-options.ts
|
|
1348
|
+
import { z as z5 } from "zod/v4";
|
|
1349
|
+
var xaiResponsesProviderOptions = z5.object({
|
|
1350
|
+
/**
|
|
1351
|
+
* Constrains how hard a reasoning model thinks before responding.
|
|
1352
|
+
* Possible values are `low` (uses fewer reasoning tokens), `medium` and `high` (uses more reasoning tokens).
|
|
1353
|
+
*/
|
|
1354
|
+
reasoningEffort: z5.enum(["low", "medium", "high"]).optional(),
|
|
1355
|
+
/**
|
|
1356
|
+
* Whether to store the input message(s) and model response for later retrieval.
|
|
1357
|
+
* @default true
|
|
1358
|
+
*/
|
|
1359
|
+
store: z5.boolean().optional(),
|
|
1360
|
+
/**
|
|
1361
|
+
* The ID of the previous response from the model.
|
|
1362
|
+
*/
|
|
1363
|
+
previousResponseId: z5.string().optional()
|
|
1364
|
+
});
|
|
1365
|
+
|
|
1366
|
+
// src/responses/xai-responses-prepare-tools.ts
|
|
1367
|
+
import {
|
|
1368
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError4
|
|
1369
|
+
} from "@ai-sdk/provider";
|
|
1370
|
+
import { validateTypes } from "@ai-sdk/provider-utils";
|
|
1371
|
+
|
|
1372
|
+
// src/tool/web-search.ts
|
|
1373
|
+
import {
|
|
1374
|
+
createProviderToolFactoryWithOutputSchema,
|
|
1375
|
+
lazySchema,
|
|
1376
|
+
zodSchema
|
|
1377
|
+
} from "@ai-sdk/provider-utils";
|
|
1378
|
+
import { z as z6 } from "zod/v4";
|
|
1379
|
+
var webSearchArgsSchema = lazySchema(
|
|
1380
|
+
() => zodSchema(
|
|
1381
|
+
z6.object({
|
|
1382
|
+
allowedDomains: z6.array(z6.string()).max(5).optional(),
|
|
1383
|
+
excludedDomains: z6.array(z6.string()).max(5).optional(),
|
|
1384
|
+
enableImageUnderstanding: z6.boolean().optional()
|
|
1385
|
+
})
|
|
1386
|
+
)
|
|
1387
|
+
);
|
|
1388
|
+
var webSearchOutputSchema = lazySchema(
|
|
1389
|
+
() => zodSchema(
|
|
1390
|
+
z6.object({
|
|
1391
|
+
query: z6.string(),
|
|
1392
|
+
sources: z6.array(
|
|
1393
|
+
z6.object({
|
|
1394
|
+
title: z6.string(),
|
|
1395
|
+
url: z6.string(),
|
|
1396
|
+
snippet: z6.string()
|
|
1397
|
+
})
|
|
1398
|
+
)
|
|
1399
|
+
})
|
|
1400
|
+
)
|
|
1401
|
+
);
|
|
1402
|
+
var webSearchToolFactory = createProviderToolFactoryWithOutputSchema({
|
|
1403
|
+
id: "xai.web_search",
|
|
1404
|
+
inputSchema: lazySchema(() => zodSchema(z6.object({}))),
|
|
1405
|
+
outputSchema: webSearchOutputSchema
|
|
1406
|
+
});
|
|
1407
|
+
var webSearch = (args = {}) => webSearchToolFactory(args);
|
|
1408
|
+
|
|
1409
|
+
// src/tool/x-search.ts
|
|
1410
|
+
import {
|
|
1411
|
+
createProviderToolFactoryWithOutputSchema as createProviderToolFactoryWithOutputSchema2,
|
|
1412
|
+
lazySchema as lazySchema2,
|
|
1413
|
+
zodSchema as zodSchema2
|
|
1414
|
+
} from "@ai-sdk/provider-utils";
|
|
1415
|
+
import { z as z7 } from "zod/v4";
|
|
1416
|
+
var xSearchArgsSchema = lazySchema2(
|
|
1417
|
+
() => zodSchema2(
|
|
1418
|
+
z7.object({
|
|
1419
|
+
allowedXHandles: z7.array(z7.string()).max(10).optional(),
|
|
1420
|
+
excludedXHandles: z7.array(z7.string()).max(10).optional(),
|
|
1421
|
+
fromDate: z7.string().optional(),
|
|
1422
|
+
toDate: z7.string().optional(),
|
|
1423
|
+
enableImageUnderstanding: z7.boolean().optional(),
|
|
1424
|
+
enableVideoUnderstanding: z7.boolean().optional()
|
|
1425
|
+
})
|
|
1426
|
+
)
|
|
1427
|
+
);
|
|
1428
|
+
var xSearchOutputSchema = lazySchema2(
|
|
1429
|
+
() => zodSchema2(
|
|
1430
|
+
z7.object({
|
|
1431
|
+
query: z7.string(),
|
|
1432
|
+
posts: z7.array(
|
|
1433
|
+
z7.object({
|
|
1434
|
+
author: z7.string(),
|
|
1435
|
+
text: z7.string(),
|
|
1436
|
+
url: z7.string(),
|
|
1437
|
+
likes: z7.number()
|
|
1438
|
+
})
|
|
1439
|
+
)
|
|
1440
|
+
})
|
|
1441
|
+
)
|
|
1442
|
+
);
|
|
1443
|
+
var xSearchToolFactory = createProviderToolFactoryWithOutputSchema2({
|
|
1444
|
+
id: "xai.x_search",
|
|
1445
|
+
inputSchema: lazySchema2(() => zodSchema2(z7.object({}))),
|
|
1446
|
+
outputSchema: xSearchOutputSchema
|
|
1447
|
+
});
|
|
1448
|
+
var xSearch = (args = {}) => xSearchToolFactory(args);
|
|
1449
|
+
|
|
1450
|
+
// src/responses/xai-responses-prepare-tools.ts
|
|
1451
|
+
async function prepareResponsesTools({
|
|
1452
|
+
tools,
|
|
1453
|
+
toolChoice
|
|
1454
|
+
}) {
|
|
1455
|
+
const normalizedTools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
1456
|
+
const toolWarnings = [];
|
|
1457
|
+
if (normalizedTools == null) {
|
|
1458
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
1459
|
+
}
|
|
1460
|
+
const xaiTools2 = [];
|
|
1461
|
+
const toolByName = /* @__PURE__ */ new Map();
|
|
1462
|
+
for (const tool of normalizedTools) {
|
|
1463
|
+
toolByName.set(tool.name, tool);
|
|
1464
|
+
if (tool.type === "provider") {
|
|
1465
|
+
switch (tool.id) {
|
|
1466
|
+
case "xai.web_search": {
|
|
1467
|
+
const args = await validateTypes({
|
|
1468
|
+
value: tool.args,
|
|
1469
|
+
schema: webSearchArgsSchema
|
|
1470
|
+
});
|
|
1471
|
+
xaiTools2.push({
|
|
1472
|
+
type: "web_search",
|
|
1473
|
+
allowed_domains: args.allowedDomains,
|
|
1474
|
+
excluded_domains: args.excludedDomains,
|
|
1475
|
+
enable_image_understanding: args.enableImageUnderstanding
|
|
1476
|
+
});
|
|
1477
|
+
break;
|
|
1478
|
+
}
|
|
1479
|
+
case "xai.x_search": {
|
|
1480
|
+
const args = await validateTypes({
|
|
1481
|
+
value: tool.args,
|
|
1482
|
+
schema: xSearchArgsSchema
|
|
1483
|
+
});
|
|
1484
|
+
xaiTools2.push({
|
|
1485
|
+
type: "x_search",
|
|
1486
|
+
allowed_x_handles: args.allowedXHandles,
|
|
1487
|
+
excluded_x_handles: args.excludedXHandles,
|
|
1488
|
+
from_date: args.fromDate,
|
|
1489
|
+
to_date: args.toDate,
|
|
1490
|
+
enable_image_understanding: args.enableImageUnderstanding,
|
|
1491
|
+
enable_video_understanding: args.enableVideoUnderstanding
|
|
1492
|
+
});
|
|
1493
|
+
break;
|
|
1494
|
+
}
|
|
1495
|
+
case "xai.code_execution": {
|
|
1496
|
+
xaiTools2.push({
|
|
1497
|
+
type: "code_interpreter"
|
|
1498
|
+
});
|
|
1499
|
+
break;
|
|
1500
|
+
}
|
|
1501
|
+
case "xai.view_image": {
|
|
1502
|
+
xaiTools2.push({
|
|
1503
|
+
type: "view_image"
|
|
1504
|
+
});
|
|
1505
|
+
break;
|
|
1506
|
+
}
|
|
1507
|
+
case "xai.view_x_video": {
|
|
1508
|
+
xaiTools2.push({
|
|
1509
|
+
type: "view_x_video"
|
|
1510
|
+
});
|
|
1511
|
+
break;
|
|
1512
|
+
}
|
|
1513
|
+
case "xai.file_search": {
|
|
1514
|
+
xaiTools2.push({
|
|
1515
|
+
type: "file_search"
|
|
1516
|
+
});
|
|
1517
|
+
break;
|
|
1518
|
+
}
|
|
1519
|
+
case "xai.mcp": {
|
|
1520
|
+
xaiTools2.push({
|
|
1521
|
+
type: "mcp"
|
|
1522
|
+
});
|
|
1523
|
+
break;
|
|
1524
|
+
}
|
|
1525
|
+
default: {
|
|
1526
|
+
toolWarnings.push({
|
|
1527
|
+
type: "unsupported",
|
|
1528
|
+
feature: `provider-defined tool ${tool.name}`
|
|
1529
|
+
});
|
|
1530
|
+
break;
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
} else {
|
|
1534
|
+
xaiTools2.push({
|
|
1535
|
+
type: "function",
|
|
1536
|
+
name: tool.name,
|
|
1537
|
+
description: tool.description,
|
|
1538
|
+
parameters: tool.inputSchema
|
|
1539
|
+
});
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
if (toolChoice == null) {
|
|
1543
|
+
return { tools: xaiTools2, toolChoice: void 0, toolWarnings };
|
|
1544
|
+
}
|
|
1545
|
+
const type = toolChoice.type;
|
|
1546
|
+
switch (type) {
|
|
1547
|
+
case "auto":
|
|
1548
|
+
case "none":
|
|
1549
|
+
return { tools: xaiTools2, toolChoice: type, toolWarnings };
|
|
1550
|
+
case "required":
|
|
1551
|
+
return { tools: xaiTools2, toolChoice: "required", toolWarnings };
|
|
1552
|
+
case "tool": {
|
|
1553
|
+
const selectedTool = toolByName.get(toolChoice.toolName);
|
|
1554
|
+
if (selectedTool == null) {
|
|
1555
|
+
return {
|
|
1556
|
+
tools: xaiTools2,
|
|
1557
|
+
toolChoice: void 0,
|
|
1558
|
+
toolWarnings
|
|
1559
|
+
};
|
|
1560
|
+
}
|
|
1561
|
+
if (selectedTool.type === "provider") {
|
|
1562
|
+
switch (selectedTool.id) {
|
|
1563
|
+
case "xai.web_search":
|
|
1564
|
+
return {
|
|
1565
|
+
tools: xaiTools2,
|
|
1566
|
+
toolChoice: { type: "web_search" },
|
|
1567
|
+
toolWarnings
|
|
1568
|
+
};
|
|
1569
|
+
case "xai.x_search":
|
|
1570
|
+
return {
|
|
1571
|
+
tools: xaiTools2,
|
|
1572
|
+
toolChoice: { type: "x_search" },
|
|
1573
|
+
toolWarnings
|
|
1574
|
+
};
|
|
1575
|
+
case "xai.code_execution":
|
|
1576
|
+
return {
|
|
1577
|
+
tools: xaiTools2,
|
|
1578
|
+
toolChoice: { type: "code_interpreter" },
|
|
1579
|
+
toolWarnings
|
|
1580
|
+
};
|
|
1581
|
+
case "xai.view_image":
|
|
1582
|
+
return {
|
|
1583
|
+
tools: xaiTools2,
|
|
1584
|
+
toolChoice: { type: "view_image" },
|
|
1585
|
+
toolWarnings
|
|
1586
|
+
};
|
|
1587
|
+
case "xai.view_x_video":
|
|
1588
|
+
return {
|
|
1589
|
+
tools: xaiTools2,
|
|
1590
|
+
toolChoice: { type: "view_x_video" },
|
|
1591
|
+
toolWarnings
|
|
1592
|
+
};
|
|
1593
|
+
case "xai.file_search":
|
|
1594
|
+
return {
|
|
1595
|
+
tools: xaiTools2,
|
|
1596
|
+
toolChoice: { type: "file_search" },
|
|
1597
|
+
toolWarnings
|
|
1598
|
+
};
|
|
1599
|
+
case "xai.mcp":
|
|
1600
|
+
return {
|
|
1601
|
+
tools: xaiTools2,
|
|
1602
|
+
toolChoice: { type: "mcp" },
|
|
1603
|
+
toolWarnings
|
|
1604
|
+
};
|
|
1605
|
+
default:
|
|
1606
|
+
toolWarnings.push({
|
|
1607
|
+
type: "unsupported",
|
|
1608
|
+
feature: `provider-defined tool ${selectedTool.name}`
|
|
1609
|
+
});
|
|
1610
|
+
return { tools: xaiTools2, toolChoice: void 0, toolWarnings };
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
return {
|
|
1614
|
+
tools: xaiTools2,
|
|
1615
|
+
toolChoice: { type: "function", name: selectedTool.name },
|
|
1616
|
+
toolWarnings
|
|
1617
|
+
};
|
|
1618
|
+
}
|
|
1619
|
+
default: {
|
|
1620
|
+
const _exhaustiveCheck = type;
|
|
1621
|
+
throw new UnsupportedFunctionalityError4({
|
|
1622
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
1623
|
+
});
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
|
|
1628
|
+
// src/responses/xai-responses-language-model.ts
|
|
1629
|
+
var XaiResponsesLanguageModel = class {
|
|
1630
|
+
constructor(modelId, config) {
|
|
1631
|
+
this.specificationVersion = "v3";
|
|
1632
|
+
this.supportedUrls = {
|
|
1633
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
1634
|
+
};
|
|
1635
|
+
this.modelId = modelId;
|
|
1636
|
+
this.config = config;
|
|
1637
|
+
}
|
|
1638
|
+
get provider() {
|
|
1639
|
+
return this.config.provider;
|
|
1640
|
+
}
|
|
1641
|
+
async getArgs({
|
|
1642
|
+
prompt,
|
|
1643
|
+
maxOutputTokens,
|
|
1644
|
+
temperature,
|
|
1645
|
+
topP,
|
|
1646
|
+
stopSequences,
|
|
1647
|
+
seed,
|
|
1648
|
+
responseFormat,
|
|
1649
|
+
providerOptions,
|
|
1650
|
+
tools,
|
|
1651
|
+
toolChoice
|
|
1652
|
+
}) {
|
|
1653
|
+
var _a, _b, _c, _d, _e;
|
|
1654
|
+
const warnings = [];
|
|
1655
|
+
const options = (_a = await parseProviderOptions2({
|
|
1656
|
+
provider: "xai",
|
|
1657
|
+
providerOptions,
|
|
1658
|
+
schema: xaiResponsesProviderOptions
|
|
1659
|
+
})) != null ? _a : {};
|
|
1660
|
+
if (stopSequences != null) {
|
|
1661
|
+
warnings.push({ type: "unsupported", feature: "stopSequences" });
|
|
1662
|
+
}
|
|
1663
|
+
const webSearchToolName = (_b = tools == null ? void 0 : tools.find(
|
|
1664
|
+
(tool) => tool.type === "provider" && tool.id === "xai.web_search"
|
|
1665
|
+
)) == null ? void 0 : _b.name;
|
|
1666
|
+
const xSearchToolName = (_c = tools == null ? void 0 : tools.find(
|
|
1667
|
+
(tool) => tool.type === "provider" && tool.id === "xai.x_search"
|
|
1668
|
+
)) == null ? void 0 : _c.name;
|
|
1669
|
+
const codeExecutionToolName = (_d = tools == null ? void 0 : tools.find(
|
|
1670
|
+
(tool) => tool.type === "provider" && tool.id === "xai.code_execution"
|
|
1671
|
+
)) == null ? void 0 : _d.name;
|
|
1672
|
+
const { input, inputWarnings } = await convertToXaiResponsesInput({
|
|
1673
|
+
prompt,
|
|
1674
|
+
store: true
|
|
1675
|
+
});
|
|
1676
|
+
warnings.push(...inputWarnings);
|
|
1677
|
+
const {
|
|
1678
|
+
tools: xaiTools2,
|
|
1679
|
+
toolChoice: xaiToolChoice,
|
|
1680
|
+
toolWarnings
|
|
1681
|
+
} = await prepareResponsesTools({
|
|
1682
|
+
tools,
|
|
1683
|
+
toolChoice
|
|
1684
|
+
});
|
|
1685
|
+
warnings.push(...toolWarnings);
|
|
1686
|
+
const baseArgs = {
|
|
1687
|
+
model: this.modelId,
|
|
1688
|
+
input,
|
|
1689
|
+
max_output_tokens: maxOutputTokens,
|
|
1690
|
+
temperature,
|
|
1691
|
+
top_p: topP,
|
|
1692
|
+
seed,
|
|
1693
|
+
...(responseFormat == null ? void 0 : responseFormat.type) === "json" && {
|
|
1694
|
+
text: {
|
|
1695
|
+
format: responseFormat.schema != null ? {
|
|
1696
|
+
type: "json_schema",
|
|
1697
|
+
strict: true,
|
|
1698
|
+
name: (_e = responseFormat.name) != null ? _e : "response",
|
|
1699
|
+
description: responseFormat.description,
|
|
1700
|
+
schema: responseFormat.schema
|
|
1701
|
+
} : { type: "json_object" }
|
|
1702
|
+
}
|
|
1703
|
+
},
|
|
1704
|
+
...options.reasoningEffort != null && {
|
|
1705
|
+
reasoning: { effort: options.reasoningEffort }
|
|
1706
|
+
},
|
|
1707
|
+
...options.store === false && {
|
|
1708
|
+
store: options.store,
|
|
1709
|
+
include: ["reasoning.encrypted_content"]
|
|
1710
|
+
},
|
|
1711
|
+
...options.previousResponseId != null && {
|
|
1712
|
+
previous_response_id: options.previousResponseId
|
|
1713
|
+
}
|
|
1714
|
+
};
|
|
1715
|
+
if (xaiTools2 && xaiTools2.length > 0) {
|
|
1716
|
+
baseArgs.tools = xaiTools2;
|
|
1717
|
+
}
|
|
1718
|
+
if (xaiToolChoice != null) {
|
|
1719
|
+
baseArgs.tool_choice = xaiToolChoice;
|
|
1720
|
+
}
|
|
1721
|
+
return {
|
|
1722
|
+
args: baseArgs,
|
|
1723
|
+
warnings,
|
|
1724
|
+
webSearchToolName,
|
|
1725
|
+
xSearchToolName,
|
|
1726
|
+
codeExecutionToolName
|
|
1727
|
+
};
|
|
1728
|
+
}
|
|
1729
|
+
async doGenerate(options) {
|
|
1730
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1731
|
+
const {
|
|
1732
|
+
args: body,
|
|
1733
|
+
warnings,
|
|
1734
|
+
webSearchToolName,
|
|
1735
|
+
xSearchToolName,
|
|
1736
|
+
codeExecutionToolName
|
|
1737
|
+
} = await this.getArgs(options);
|
|
1738
|
+
const {
|
|
1739
|
+
responseHeaders,
|
|
1740
|
+
value: response,
|
|
1741
|
+
rawValue: rawResponse
|
|
1742
|
+
} = await postJsonToApi2({
|
|
1743
|
+
url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/responses`,
|
|
1744
|
+
headers: combineHeaders2(this.config.headers(), options.headers),
|
|
1745
|
+
body,
|
|
1746
|
+
failedResponseHandler: xaiFailedResponseHandler,
|
|
1747
|
+
successfulResponseHandler: createJsonResponseHandler2(
|
|
1748
|
+
xaiResponsesResponseSchema
|
|
1749
|
+
),
|
|
1750
|
+
abortSignal: options.abortSignal,
|
|
1751
|
+
fetch: this.config.fetch
|
|
1752
|
+
});
|
|
1753
|
+
const content = [];
|
|
1754
|
+
const webSearchSubTools = [
|
|
1755
|
+
"web_search",
|
|
1756
|
+
"web_search_with_snippets",
|
|
1757
|
+
"browse_page"
|
|
1758
|
+
];
|
|
1759
|
+
const xSearchSubTools = [
|
|
1760
|
+
"x_user_search",
|
|
1761
|
+
"x_keyword_search",
|
|
1762
|
+
"x_semantic_search",
|
|
1763
|
+
"x_thread_fetch"
|
|
1764
|
+
];
|
|
1765
|
+
for (const part of response.output) {
|
|
1766
|
+
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") {
|
|
1767
|
+
let toolName = (_b = part.name) != null ? _b : "";
|
|
1768
|
+
if (webSearchSubTools.includes((_c = part.name) != null ? _c : "") || part.type === "web_search_call") {
|
|
1769
|
+
toolName = webSearchToolName != null ? webSearchToolName : "web_search";
|
|
1770
|
+
} else if (xSearchSubTools.includes((_d = part.name) != null ? _d : "") || part.type === "x_search_call") {
|
|
1771
|
+
toolName = xSearchToolName != null ? xSearchToolName : "x_search";
|
|
1772
|
+
} else if (part.name === "code_execution" || part.type === "code_interpreter_call" || part.type === "code_execution_call") {
|
|
1773
|
+
toolName = codeExecutionToolName != null ? codeExecutionToolName : "code_execution";
|
|
1774
|
+
}
|
|
1775
|
+
const toolInput = part.type === "custom_tool_call" ? (_e = part.input) != null ? _e : "" : (_f = part.arguments) != null ? _f : "";
|
|
1776
|
+
content.push({
|
|
1777
|
+
type: "tool-call",
|
|
1778
|
+
toolCallId: part.id,
|
|
1779
|
+
toolName,
|
|
1780
|
+
input: toolInput,
|
|
1781
|
+
providerExecuted: true
|
|
1782
|
+
});
|
|
1783
|
+
continue;
|
|
1784
|
+
}
|
|
1785
|
+
switch (part.type) {
|
|
1786
|
+
case "message": {
|
|
1787
|
+
for (const contentPart of part.content) {
|
|
1788
|
+
if (contentPart.text) {
|
|
1789
|
+
content.push({
|
|
1790
|
+
type: "text",
|
|
1791
|
+
text: contentPart.text
|
|
1792
|
+
});
|
|
1793
|
+
}
|
|
1794
|
+
if (contentPart.annotations) {
|
|
1795
|
+
for (const annotation of contentPart.annotations) {
|
|
1796
|
+
if (annotation.type === "url_citation" && "url" in annotation) {
|
|
1797
|
+
content.push({
|
|
1798
|
+
type: "source",
|
|
1799
|
+
sourceType: "url",
|
|
1800
|
+
id: this.config.generateId(),
|
|
1801
|
+
url: annotation.url,
|
|
1802
|
+
title: (_g = annotation.title) != null ? _g : annotation.url
|
|
1803
|
+
});
|
|
1804
|
+
}
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
}
|
|
1808
|
+
break;
|
|
1809
|
+
}
|
|
1810
|
+
case "function_call": {
|
|
1811
|
+
content.push({
|
|
1812
|
+
type: "tool-call",
|
|
1813
|
+
toolCallId: part.call_id,
|
|
1814
|
+
toolName: part.name,
|
|
1815
|
+
input: part.arguments
|
|
1816
|
+
});
|
|
1817
|
+
break;
|
|
1818
|
+
}
|
|
1819
|
+
case "reasoning": {
|
|
1820
|
+
const summaryTexts = part.summary.map((s) => s.text).filter((text) => text && text.length > 0);
|
|
1821
|
+
if (summaryTexts.length > 0) {
|
|
1822
|
+
const reasoningText = summaryTexts.join("");
|
|
1823
|
+
if (part.encrypted_content || part.id) {
|
|
1824
|
+
content.push({
|
|
1825
|
+
type: "reasoning",
|
|
1826
|
+
text: reasoningText,
|
|
1827
|
+
providerMetadata: {
|
|
1828
|
+
xai: {
|
|
1829
|
+
...part.encrypted_content && {
|
|
1830
|
+
reasoningEncryptedContent: part.encrypted_content
|
|
1831
|
+
},
|
|
1832
|
+
...part.id && { itemId: part.id }
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
});
|
|
1836
|
+
} else {
|
|
1837
|
+
content.push({
|
|
1838
|
+
type: "reasoning",
|
|
1839
|
+
text: reasoningText
|
|
1840
|
+
});
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
break;
|
|
1844
|
+
}
|
|
1845
|
+
default: {
|
|
1846
|
+
break;
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
return {
|
|
1851
|
+
content,
|
|
1852
|
+
finishReason: {
|
|
1853
|
+
unified: mapXaiResponsesFinishReason(response.status),
|
|
1854
|
+
raw: (_h = response.status) != null ? _h : void 0
|
|
1855
|
+
},
|
|
1856
|
+
usage: convertXaiResponsesUsage(response.usage),
|
|
1857
|
+
request: { body },
|
|
1858
|
+
response: {
|
|
1859
|
+
...getResponseMetadata(response),
|
|
1860
|
+
headers: responseHeaders,
|
|
1861
|
+
body: rawResponse
|
|
1862
|
+
},
|
|
1863
|
+
warnings
|
|
1864
|
+
};
|
|
1865
|
+
}
|
|
1866
|
+
async doStream(options) {
|
|
1867
|
+
var _a;
|
|
1868
|
+
const {
|
|
1869
|
+
args,
|
|
1870
|
+
warnings,
|
|
1871
|
+
webSearchToolName,
|
|
1872
|
+
xSearchToolName,
|
|
1873
|
+
codeExecutionToolName
|
|
1874
|
+
} = await this.getArgs(options);
|
|
1875
|
+
const body = {
|
|
1876
|
+
...args,
|
|
1877
|
+
stream: true
|
|
1878
|
+
};
|
|
1879
|
+
const { responseHeaders, value: response } = await postJsonToApi2({
|
|
1880
|
+
url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/responses`,
|
|
1881
|
+
headers: combineHeaders2(this.config.headers(), options.headers),
|
|
1882
|
+
body,
|
|
1883
|
+
failedResponseHandler: xaiFailedResponseHandler,
|
|
1884
|
+
successfulResponseHandler: createEventSourceResponseHandler2(
|
|
1885
|
+
xaiResponsesChunkSchema
|
|
1886
|
+
),
|
|
1887
|
+
abortSignal: options.abortSignal,
|
|
1888
|
+
fetch: this.config.fetch
|
|
1889
|
+
});
|
|
1890
|
+
let finishReason = {
|
|
1891
|
+
unified: "other",
|
|
1892
|
+
raw: void 0
|
|
1893
|
+
};
|
|
1894
|
+
let usage = void 0;
|
|
1895
|
+
let isFirstChunk = true;
|
|
1896
|
+
const contentBlocks = {};
|
|
1897
|
+
const seenToolCalls = /* @__PURE__ */ new Set();
|
|
1898
|
+
const activeReasoning = {};
|
|
1899
|
+
const self = this;
|
|
1900
|
+
return {
|
|
1901
|
+
stream: response.pipeThrough(
|
|
1902
|
+
new TransformStream({
|
|
1903
|
+
start(controller) {
|
|
1904
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
1905
|
+
},
|
|
1906
|
+
transform(chunk, controller) {
|
|
1907
|
+
var _a2, _b, _c, _d, _e, _f, _g, _h;
|
|
1908
|
+
if (options.includeRawChunks) {
|
|
1909
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
1910
|
+
}
|
|
1911
|
+
if (!chunk.success) {
|
|
1912
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
|
1913
|
+
return;
|
|
1914
|
+
}
|
|
1915
|
+
const event = chunk.value;
|
|
1916
|
+
if (event.type === "response.created" || event.type === "response.in_progress") {
|
|
1917
|
+
if (isFirstChunk) {
|
|
1918
|
+
controller.enqueue({
|
|
1919
|
+
type: "response-metadata",
|
|
1920
|
+
...getResponseMetadata(event.response)
|
|
1921
|
+
});
|
|
1922
|
+
isFirstChunk = false;
|
|
1923
|
+
}
|
|
1924
|
+
return;
|
|
1925
|
+
}
|
|
1926
|
+
if (event.type === "response.reasoning_summary_part.added") {
|
|
1927
|
+
const blockId = `reasoning-${event.item_id}`;
|
|
1928
|
+
controller.enqueue({
|
|
1929
|
+
type: "reasoning-start",
|
|
1930
|
+
id: blockId,
|
|
1931
|
+
providerMetadata: {
|
|
1932
|
+
xai: {
|
|
1933
|
+
itemId: event.item_id
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1936
|
+
});
|
|
1937
|
+
}
|
|
1938
|
+
if (event.type === "response.reasoning_summary_text.delta") {
|
|
1939
|
+
const blockId = `reasoning-${event.item_id}`;
|
|
1940
|
+
controller.enqueue({
|
|
1941
|
+
type: "reasoning-delta",
|
|
1942
|
+
id: blockId,
|
|
1943
|
+
delta: event.delta,
|
|
1944
|
+
providerMetadata: {
|
|
1945
|
+
xai: {
|
|
1946
|
+
itemId: event.item_id
|
|
1947
|
+
}
|
|
1948
|
+
}
|
|
1949
|
+
});
|
|
1950
|
+
return;
|
|
1951
|
+
}
|
|
1952
|
+
if (event.type === "response.reasoning_summary_text.done") {
|
|
1953
|
+
return;
|
|
1954
|
+
}
|
|
1955
|
+
if (event.type === "response.output_text.delta") {
|
|
1956
|
+
const blockId = `text-${event.item_id}`;
|
|
1957
|
+
if (contentBlocks[blockId] == null) {
|
|
1958
|
+
contentBlocks[blockId] = { type: "text" };
|
|
1959
|
+
controller.enqueue({
|
|
1960
|
+
type: "text-start",
|
|
1961
|
+
id: blockId
|
|
1962
|
+
});
|
|
1963
|
+
}
|
|
1964
|
+
controller.enqueue({
|
|
1965
|
+
type: "text-delta",
|
|
1966
|
+
id: blockId,
|
|
1967
|
+
delta: event.delta
|
|
1968
|
+
});
|
|
1969
|
+
return;
|
|
1970
|
+
}
|
|
1971
|
+
if (event.type === "response.output_text.done") {
|
|
1972
|
+
if (event.annotations) {
|
|
1973
|
+
for (const annotation of event.annotations) {
|
|
1974
|
+
if (annotation.type === "url_citation" && "url" in annotation) {
|
|
1975
|
+
controller.enqueue({
|
|
1976
|
+
type: "source",
|
|
1977
|
+
sourceType: "url",
|
|
1978
|
+
id: self.config.generateId(),
|
|
1979
|
+
url: annotation.url,
|
|
1980
|
+
title: (_a2 = annotation.title) != null ? _a2 : annotation.url
|
|
1981
|
+
});
|
|
1982
|
+
}
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1985
|
+
return;
|
|
1986
|
+
}
|
|
1987
|
+
if (event.type === "response.output_text.annotation.added") {
|
|
1988
|
+
const annotation = event.annotation;
|
|
1989
|
+
if (annotation.type === "url_citation" && "url" in annotation) {
|
|
1990
|
+
controller.enqueue({
|
|
1991
|
+
type: "source",
|
|
1992
|
+
sourceType: "url",
|
|
1993
|
+
id: self.config.generateId(),
|
|
1994
|
+
url: annotation.url,
|
|
1995
|
+
title: (_b = annotation.title) != null ? _b : annotation.url
|
|
1996
|
+
});
|
|
1997
|
+
}
|
|
1998
|
+
return;
|
|
1999
|
+
}
|
|
2000
|
+
if (event.type === "response.done" || event.type === "response.completed") {
|
|
2001
|
+
const response2 = event.response;
|
|
2002
|
+
if (response2.usage) {
|
|
2003
|
+
usage = convertXaiResponsesUsage(response2.usage);
|
|
2004
|
+
}
|
|
2005
|
+
if (response2.status) {
|
|
2006
|
+
finishReason = {
|
|
2007
|
+
unified: mapXaiResponsesFinishReason(response2.status),
|
|
2008
|
+
raw: response2.status
|
|
2009
|
+
};
|
|
2010
|
+
}
|
|
2011
|
+
return;
|
|
2012
|
+
}
|
|
2013
|
+
if (event.type === "response.output_item.added" || event.type === "response.output_item.done") {
|
|
2014
|
+
const part = event.item;
|
|
2015
|
+
if (part.type === "reasoning") {
|
|
2016
|
+
if (event.type === "response.output_item.done") {
|
|
2017
|
+
controller.enqueue({
|
|
2018
|
+
type: "reasoning-end",
|
|
2019
|
+
id: `reasoning-${part.id}`,
|
|
2020
|
+
providerMetadata: {
|
|
2021
|
+
xai: {
|
|
2022
|
+
...part.encrypted_content && {
|
|
2023
|
+
reasoningEncryptedContent: part.encrypted_content
|
|
2024
|
+
},
|
|
2025
|
+
...part.id && { itemId: part.id }
|
|
2026
|
+
}
|
|
2027
|
+
}
|
|
2028
|
+
});
|
|
2029
|
+
delete activeReasoning[part.id];
|
|
2030
|
+
}
|
|
2031
|
+
return;
|
|
2032
|
+
}
|
|
2033
|
+
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") {
|
|
2034
|
+
const webSearchSubTools = [
|
|
2035
|
+
"web_search",
|
|
2036
|
+
"web_search_with_snippets",
|
|
2037
|
+
"browse_page"
|
|
2038
|
+
];
|
|
2039
|
+
const xSearchSubTools = [
|
|
2040
|
+
"x_user_search",
|
|
2041
|
+
"x_keyword_search",
|
|
2042
|
+
"x_semantic_search",
|
|
2043
|
+
"x_thread_fetch"
|
|
2044
|
+
];
|
|
2045
|
+
let toolName = (_c = part.name) != null ? _c : "";
|
|
2046
|
+
if (webSearchSubTools.includes((_d = part.name) != null ? _d : "") || part.type === "web_search_call") {
|
|
2047
|
+
toolName = webSearchToolName != null ? webSearchToolName : "web_search";
|
|
2048
|
+
} else if (xSearchSubTools.includes((_e = part.name) != null ? _e : "") || part.type === "x_search_call") {
|
|
2049
|
+
toolName = xSearchToolName != null ? xSearchToolName : "x_search";
|
|
2050
|
+
} else if (part.name === "code_execution" || part.type === "code_interpreter_call" || part.type === "code_execution_call") {
|
|
2051
|
+
toolName = codeExecutionToolName != null ? codeExecutionToolName : "code_execution";
|
|
2052
|
+
}
|
|
2053
|
+
const toolInput = part.type === "custom_tool_call" ? (_f = part.input) != null ? _f : "" : (_g = part.arguments) != null ? _g : "";
|
|
2054
|
+
const shouldEmit = part.type === "custom_tool_call" ? event.type === "response.output_item.done" : !seenToolCalls.has(part.id);
|
|
2055
|
+
if (shouldEmit && !seenToolCalls.has(part.id)) {
|
|
2056
|
+
seenToolCalls.add(part.id);
|
|
2057
|
+
controller.enqueue({
|
|
2058
|
+
type: "tool-input-start",
|
|
2059
|
+
id: part.id,
|
|
2060
|
+
toolName
|
|
2061
|
+
});
|
|
2062
|
+
controller.enqueue({
|
|
2063
|
+
type: "tool-input-delta",
|
|
2064
|
+
id: part.id,
|
|
2065
|
+
delta: toolInput
|
|
2066
|
+
});
|
|
2067
|
+
controller.enqueue({
|
|
2068
|
+
type: "tool-input-end",
|
|
2069
|
+
id: part.id
|
|
2070
|
+
});
|
|
2071
|
+
controller.enqueue({
|
|
2072
|
+
type: "tool-call",
|
|
2073
|
+
toolCallId: part.id,
|
|
2074
|
+
toolName,
|
|
2075
|
+
input: toolInput,
|
|
2076
|
+
providerExecuted: true
|
|
2077
|
+
});
|
|
2078
|
+
}
|
|
2079
|
+
return;
|
|
2080
|
+
}
|
|
2081
|
+
if (part.type === "message") {
|
|
2082
|
+
for (const contentPart of part.content) {
|
|
2083
|
+
if (contentPart.text && contentPart.text.length > 0) {
|
|
2084
|
+
const blockId = `text-${part.id}`;
|
|
2085
|
+
if (contentBlocks[blockId] == null) {
|
|
2086
|
+
contentBlocks[blockId] = { type: "text" };
|
|
2087
|
+
controller.enqueue({
|
|
2088
|
+
type: "text-start",
|
|
2089
|
+
id: blockId
|
|
2090
|
+
});
|
|
2091
|
+
controller.enqueue({
|
|
2092
|
+
type: "text-delta",
|
|
2093
|
+
id: blockId,
|
|
2094
|
+
delta: contentPart.text
|
|
2095
|
+
});
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
if (contentPart.annotations) {
|
|
2099
|
+
for (const annotation of contentPart.annotations) {
|
|
2100
|
+
if (annotation.type === "url_citation" && "url" in annotation) {
|
|
2101
|
+
controller.enqueue({
|
|
2102
|
+
type: "source",
|
|
2103
|
+
sourceType: "url",
|
|
2104
|
+
id: self.config.generateId(),
|
|
2105
|
+
url: annotation.url,
|
|
2106
|
+
title: (_h = annotation.title) != null ? _h : annotation.url
|
|
2107
|
+
});
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
} else if (part.type === "function_call") {
|
|
2113
|
+
if (!seenToolCalls.has(part.call_id)) {
|
|
2114
|
+
seenToolCalls.add(part.call_id);
|
|
2115
|
+
controller.enqueue({
|
|
2116
|
+
type: "tool-input-start",
|
|
2117
|
+
id: part.call_id,
|
|
2118
|
+
toolName: part.name
|
|
2119
|
+
});
|
|
2120
|
+
controller.enqueue({
|
|
2121
|
+
type: "tool-input-delta",
|
|
2122
|
+
id: part.call_id,
|
|
2123
|
+
delta: part.arguments
|
|
2124
|
+
});
|
|
2125
|
+
controller.enqueue({
|
|
2126
|
+
type: "tool-input-end",
|
|
2127
|
+
id: part.call_id
|
|
2128
|
+
});
|
|
2129
|
+
controller.enqueue({
|
|
2130
|
+
type: "tool-call",
|
|
2131
|
+
toolCallId: part.call_id,
|
|
2132
|
+
toolName: part.name,
|
|
2133
|
+
input: part.arguments
|
|
2134
|
+
});
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2138
|
+
},
|
|
2139
|
+
flush(controller) {
|
|
2140
|
+
for (const [blockId, block] of Object.entries(contentBlocks)) {
|
|
2141
|
+
if (block.type === "text") {
|
|
2142
|
+
controller.enqueue({
|
|
2143
|
+
type: "text-end",
|
|
2144
|
+
id: blockId
|
|
2145
|
+
});
|
|
2146
|
+
}
|
|
2147
|
+
}
|
|
2148
|
+
controller.enqueue({ type: "finish", finishReason, usage });
|
|
2149
|
+
}
|
|
2150
|
+
})
|
|
2151
|
+
),
|
|
2152
|
+
request: { body },
|
|
2153
|
+
response: { headers: responseHeaders }
|
|
2154
|
+
};
|
|
2155
|
+
}
|
|
2156
|
+
};
|
|
2157
|
+
|
|
2158
|
+
// src/tool/code-execution.ts
|
|
2159
|
+
import { createProviderToolFactoryWithOutputSchema as createProviderToolFactoryWithOutputSchema3 } from "@ai-sdk/provider-utils";
|
|
2160
|
+
import { z as z8 } from "zod/v4";
|
|
2161
|
+
var codeExecutionOutputSchema = z8.object({
|
|
2162
|
+
output: z8.string().describe("the output of the code execution"),
|
|
2163
|
+
error: z8.string().optional().describe("any error that occurred")
|
|
2164
|
+
});
|
|
2165
|
+
var codeExecutionToolFactory = createProviderToolFactoryWithOutputSchema3({
|
|
2166
|
+
id: "xai.code_execution",
|
|
2167
|
+
inputSchema: z8.object({}).describe("no input parameters"),
|
|
2168
|
+
outputSchema: codeExecutionOutputSchema
|
|
2169
|
+
});
|
|
2170
|
+
var codeExecution = (args = {}) => codeExecutionToolFactory(args);
|
|
2171
|
+
|
|
2172
|
+
// src/tool/view-image.ts
|
|
2173
|
+
import { createProviderToolFactoryWithOutputSchema as createProviderToolFactoryWithOutputSchema4 } from "@ai-sdk/provider-utils";
|
|
2174
|
+
import { z as z9 } from "zod/v4";
|
|
2175
|
+
var viewImageOutputSchema = z9.object({
|
|
2176
|
+
description: z9.string().describe("description of the image"),
|
|
2177
|
+
objects: z9.array(z9.string()).optional().describe("objects detected in the image")
|
|
2178
|
+
});
|
|
2179
|
+
var viewImageToolFactory = createProviderToolFactoryWithOutputSchema4({
|
|
2180
|
+
id: "xai.view_image",
|
|
2181
|
+
inputSchema: z9.object({}).describe("no input parameters"),
|
|
2182
|
+
outputSchema: viewImageOutputSchema
|
|
2183
|
+
});
|
|
2184
|
+
var viewImage = (args = {}) => viewImageToolFactory(args);
|
|
2185
|
+
|
|
2186
|
+
// src/tool/view-x-video.ts
|
|
2187
|
+
import { createProviderToolFactoryWithOutputSchema as createProviderToolFactoryWithOutputSchema5 } from "@ai-sdk/provider-utils";
|
|
2188
|
+
import { z as z10 } from "zod/v4";
|
|
2189
|
+
var viewXVideoOutputSchema = z10.object({
|
|
2190
|
+
transcript: z10.string().optional().describe("transcript of the video"),
|
|
2191
|
+
description: z10.string().describe("description of the video content"),
|
|
2192
|
+
duration: z10.number().optional().describe("duration in seconds")
|
|
2193
|
+
});
|
|
2194
|
+
var viewXVideoToolFactory = createProviderToolFactoryWithOutputSchema5({
|
|
2195
|
+
id: "xai.view_x_video",
|
|
2196
|
+
inputSchema: z10.object({}).describe("no input parameters"),
|
|
2197
|
+
outputSchema: viewXVideoOutputSchema
|
|
2198
|
+
});
|
|
2199
|
+
var viewXVideo = (args = {}) => viewXVideoToolFactory(args);
|
|
2200
|
+
|
|
2201
|
+
// src/tool/index.ts
|
|
2202
|
+
var xaiTools = {
|
|
2203
|
+
codeExecution,
|
|
2204
|
+
viewImage,
|
|
2205
|
+
viewXVideo,
|
|
2206
|
+
webSearch,
|
|
2207
|
+
xSearch
|
|
2208
|
+
};
|
|
2209
|
+
|
|
2210
|
+
// src/version.ts
|
|
2211
|
+
var VERSION = true ? "0.0.0-1c33ba03-20260114162300" : "0.0.0-test";
|
|
2212
|
+
|
|
2213
|
+
// src/xai-provider.ts
|
|
2214
|
+
var xaiErrorStructure = {
|
|
2215
|
+
errorSchema: xaiErrorDataSchema,
|
|
2216
|
+
errorToMessage: (data) => data.error.message
|
|
2217
|
+
};
|
|
2218
|
+
function createXai(options = {}) {
|
|
2219
|
+
var _a;
|
|
2220
|
+
const baseURL = withoutTrailingSlash(
|
|
2221
|
+
(_a = options.baseURL) != null ? _a : "https://api.x.ai/v1"
|
|
2222
|
+
);
|
|
2223
|
+
const getHeaders = () => withUserAgentSuffix(
|
|
2224
|
+
{
|
|
2225
|
+
Authorization: `Bearer ${loadApiKey({
|
|
2226
|
+
apiKey: options.apiKey,
|
|
2227
|
+
environmentVariableName: "XAI_API_KEY",
|
|
2228
|
+
description: "xAI API key"
|
|
2229
|
+
})}`,
|
|
2230
|
+
...options.headers
|
|
2231
|
+
},
|
|
2232
|
+
`ai-sdk/xai/${VERSION}`
|
|
2233
|
+
);
|
|
2234
|
+
const createChatLanguageModel = (modelId) => {
|
|
2235
|
+
return new XaiChatLanguageModel(modelId, {
|
|
2236
|
+
provider: "xai.chat",
|
|
2237
|
+
baseURL,
|
|
2238
|
+
headers: getHeaders,
|
|
2239
|
+
generateId,
|
|
2240
|
+
fetch: options.fetch
|
|
2241
|
+
});
|
|
2242
|
+
};
|
|
2243
|
+
const createResponsesLanguageModel = (modelId) => {
|
|
2244
|
+
return new XaiResponsesLanguageModel(modelId, {
|
|
2245
|
+
provider: "xai.responses",
|
|
2246
|
+
baseURL,
|
|
2247
|
+
headers: getHeaders,
|
|
2248
|
+
generateId,
|
|
2249
|
+
fetch: options.fetch
|
|
2250
|
+
});
|
|
2251
|
+
};
|
|
2252
|
+
const createImageModel = (modelId) => {
|
|
2253
|
+
return new OpenAICompatibleImageModel(modelId, {
|
|
2254
|
+
provider: "xai.image",
|
|
2255
|
+
url: ({ path }) => `${baseURL}${path}`,
|
|
2256
|
+
headers: getHeaders,
|
|
2257
|
+
fetch: options.fetch,
|
|
2258
|
+
errorStructure: xaiErrorStructure
|
|
2259
|
+
});
|
|
2260
|
+
};
|
|
2261
|
+
const provider = (modelId) => createChatLanguageModel(modelId);
|
|
2262
|
+
provider.specificationVersion = "v3";
|
|
2263
|
+
provider.languageModel = createChatLanguageModel;
|
|
2264
|
+
provider.chat = createChatLanguageModel;
|
|
2265
|
+
provider.responses = createResponsesLanguageModel;
|
|
2266
|
+
provider.embeddingModel = (modelId) => {
|
|
2267
|
+
throw new NoSuchModelError({ modelId, modelType: "embeddingModel" });
|
|
2268
|
+
};
|
|
2269
|
+
provider.textEmbeddingModel = provider.embeddingModel;
|
|
2270
|
+
provider.imageModel = createImageModel;
|
|
2271
|
+
provider.image = createImageModel;
|
|
2272
|
+
provider.tools = xaiTools;
|
|
2273
|
+
return provider;
|
|
2274
|
+
}
|
|
2275
|
+
var xai = createXai();
|
|
2276
|
+
export {
|
|
2277
|
+
VERSION,
|
|
2278
|
+
codeExecution,
|
|
2279
|
+
createXai,
|
|
2280
|
+
viewImage,
|
|
2281
|
+
viewXVideo,
|
|
2282
|
+
webSearch,
|
|
2283
|
+
xSearch,
|
|
2284
|
+
xai,
|
|
2285
|
+
xaiTools
|
|
2286
|
+
};
|
|
2287
|
+
//# sourceMappingURL=index.mjs.map
|