@ai-sdk/xai 2.0.34 → 2.0.36
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 +12 -0
- package/dist/index.d.mts +170 -1
- package/dist/index.d.ts +170 -1
- package/dist/index.js +1199 -24
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1201 -16
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -21,15 +21,21 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
VERSION: () => VERSION,
|
|
24
|
+
codeExecution: () => codeExecution,
|
|
24
25
|
createXai: () => createXai,
|
|
25
|
-
|
|
26
|
+
viewImage: () => viewImage,
|
|
27
|
+
viewXVideo: () => viewXVideo,
|
|
28
|
+
webSearch: () => webSearch,
|
|
29
|
+
xSearch: () => xSearch,
|
|
30
|
+
xai: () => xai,
|
|
31
|
+
xaiTools: () => xaiTools
|
|
26
32
|
});
|
|
27
33
|
module.exports = __toCommonJS(src_exports);
|
|
28
34
|
|
|
29
35
|
// src/xai-provider.ts
|
|
30
36
|
var import_openai_compatible = require("@ai-sdk/openai-compatible");
|
|
31
|
-
var
|
|
32
|
-
var
|
|
37
|
+
var import_provider4 = require("@ai-sdk/provider");
|
|
38
|
+
var import_provider_utils11 = require("@ai-sdk/provider-utils");
|
|
33
39
|
|
|
34
40
|
// src/xai-chat-language-model.ts
|
|
35
41
|
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
@@ -144,12 +150,14 @@ function convertToXaiChatMessages(prompt) {
|
|
|
144
150
|
function getResponseMetadata({
|
|
145
151
|
id,
|
|
146
152
|
model,
|
|
147
|
-
created
|
|
153
|
+
created,
|
|
154
|
+
created_at
|
|
148
155
|
}) {
|
|
156
|
+
const unixTime = created != null ? created : created_at;
|
|
149
157
|
return {
|
|
150
158
|
id: id != null ? id : void 0,
|
|
151
159
|
modelId: model != null ? model : void 0,
|
|
152
|
-
timestamp:
|
|
160
|
+
timestamp: unixTime != null ? new Date(unixTime * 1e3) : void 0
|
|
153
161
|
};
|
|
154
162
|
}
|
|
155
163
|
|
|
@@ -277,12 +285,12 @@ function prepareTools({
|
|
|
277
285
|
if (tools == null) {
|
|
278
286
|
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
279
287
|
}
|
|
280
|
-
const
|
|
288
|
+
const xaiTools2 = [];
|
|
281
289
|
for (const tool of tools) {
|
|
282
290
|
if (tool.type === "provider-defined") {
|
|
283
291
|
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
284
292
|
} else {
|
|
285
|
-
|
|
293
|
+
xaiTools2.push({
|
|
286
294
|
type: "function",
|
|
287
295
|
function: {
|
|
288
296
|
name: tool.name,
|
|
@@ -293,18 +301,18 @@ function prepareTools({
|
|
|
293
301
|
}
|
|
294
302
|
}
|
|
295
303
|
if (toolChoice == null) {
|
|
296
|
-
return { tools:
|
|
304
|
+
return { tools: xaiTools2, toolChoice: void 0, toolWarnings };
|
|
297
305
|
}
|
|
298
306
|
const type = toolChoice.type;
|
|
299
307
|
switch (type) {
|
|
300
308
|
case "auto":
|
|
301
309
|
case "none":
|
|
302
|
-
return { tools:
|
|
310
|
+
return { tools: xaiTools2, toolChoice: type, toolWarnings };
|
|
303
311
|
case "required":
|
|
304
|
-
return { tools:
|
|
312
|
+
return { tools: xaiTools2, toolChoice: "required", toolWarnings };
|
|
305
313
|
case "tool":
|
|
306
314
|
return {
|
|
307
|
-
tools:
|
|
315
|
+
tools: xaiTools2,
|
|
308
316
|
toolChoice: {
|
|
309
317
|
type: "function",
|
|
310
318
|
function: { name: toolChoice.toolName }
|
|
@@ -382,7 +390,7 @@ var XaiChatLanguageModel = class {
|
|
|
382
390
|
const { messages, warnings: messageWarnings } = convertToXaiChatMessages(prompt);
|
|
383
391
|
warnings.push(...messageWarnings);
|
|
384
392
|
const {
|
|
385
|
-
tools:
|
|
393
|
+
tools: xaiTools2,
|
|
386
394
|
toolChoice: xaiToolChoice,
|
|
387
395
|
toolWarnings
|
|
388
396
|
} = prepareTools({
|
|
@@ -447,7 +455,7 @@ var XaiChatLanguageModel = class {
|
|
|
447
455
|
// messages in xai format
|
|
448
456
|
messages,
|
|
449
457
|
// tools in xai format
|
|
450
|
-
tools:
|
|
458
|
+
tools: xaiTools2,
|
|
451
459
|
tool_choice: xaiToolChoice
|
|
452
460
|
};
|
|
453
461
|
return {
|
|
@@ -766,8 +774,1157 @@ var xaiChatChunkSchema = import_v43.z.object({
|
|
|
766
774
|
citations: import_v43.z.array(import_v43.z.string().url()).nullish()
|
|
767
775
|
});
|
|
768
776
|
|
|
777
|
+
// src/responses/xai-responses-language-model.ts
|
|
778
|
+
var import_provider_utils7 = require("@ai-sdk/provider-utils");
|
|
779
|
+
|
|
780
|
+
// src/responses/xai-responses-api.ts
|
|
781
|
+
var import_v44 = require("zod/v4");
|
|
782
|
+
var annotationSchema = import_v44.z.union([
|
|
783
|
+
import_v44.z.object({
|
|
784
|
+
type: import_v44.z.literal("url_citation"),
|
|
785
|
+
url: import_v44.z.string(),
|
|
786
|
+
title: import_v44.z.string().optional()
|
|
787
|
+
}),
|
|
788
|
+
import_v44.z.object({
|
|
789
|
+
type: import_v44.z.string()
|
|
790
|
+
})
|
|
791
|
+
]);
|
|
792
|
+
var messageContentPartSchema = import_v44.z.object({
|
|
793
|
+
type: import_v44.z.string(),
|
|
794
|
+
text: import_v44.z.string().optional(),
|
|
795
|
+
logprobs: import_v44.z.array(import_v44.z.any()).optional(),
|
|
796
|
+
annotations: import_v44.z.array(annotationSchema).optional()
|
|
797
|
+
});
|
|
798
|
+
var reasoningSummaryPartSchema = import_v44.z.object({
|
|
799
|
+
type: import_v44.z.string(),
|
|
800
|
+
text: import_v44.z.string()
|
|
801
|
+
});
|
|
802
|
+
var toolCallSchema = import_v44.z.object({
|
|
803
|
+
name: import_v44.z.string(),
|
|
804
|
+
arguments: import_v44.z.string(),
|
|
805
|
+
call_id: import_v44.z.string(),
|
|
806
|
+
id: import_v44.z.string(),
|
|
807
|
+
status: import_v44.z.string()
|
|
808
|
+
});
|
|
809
|
+
var outputItemSchema = import_v44.z.discriminatedUnion("type", [
|
|
810
|
+
import_v44.z.object({
|
|
811
|
+
type: import_v44.z.literal("web_search_call"),
|
|
812
|
+
...toolCallSchema.shape
|
|
813
|
+
}),
|
|
814
|
+
import_v44.z.object({
|
|
815
|
+
type: import_v44.z.literal("x_search_call"),
|
|
816
|
+
...toolCallSchema.shape
|
|
817
|
+
}),
|
|
818
|
+
import_v44.z.object({
|
|
819
|
+
type: import_v44.z.literal("code_interpreter_call"),
|
|
820
|
+
...toolCallSchema.shape
|
|
821
|
+
}),
|
|
822
|
+
import_v44.z.object({
|
|
823
|
+
type: import_v44.z.literal("code_execution_call"),
|
|
824
|
+
...toolCallSchema.shape
|
|
825
|
+
}),
|
|
826
|
+
import_v44.z.object({
|
|
827
|
+
type: import_v44.z.literal("view_image_call"),
|
|
828
|
+
...toolCallSchema.shape
|
|
829
|
+
}),
|
|
830
|
+
import_v44.z.object({
|
|
831
|
+
type: import_v44.z.literal("view_x_video_call"),
|
|
832
|
+
...toolCallSchema.shape
|
|
833
|
+
}),
|
|
834
|
+
import_v44.z.object({
|
|
835
|
+
type: import_v44.z.literal("message"),
|
|
836
|
+
role: import_v44.z.string(),
|
|
837
|
+
content: import_v44.z.array(messageContentPartSchema),
|
|
838
|
+
id: import_v44.z.string(),
|
|
839
|
+
status: import_v44.z.string()
|
|
840
|
+
}),
|
|
841
|
+
import_v44.z.object({
|
|
842
|
+
type: import_v44.z.literal("function_call"),
|
|
843
|
+
name: import_v44.z.string(),
|
|
844
|
+
arguments: import_v44.z.string(),
|
|
845
|
+
call_id: import_v44.z.string(),
|
|
846
|
+
id: import_v44.z.string()
|
|
847
|
+
}),
|
|
848
|
+
import_v44.z.object({
|
|
849
|
+
type: import_v44.z.literal("reasoning"),
|
|
850
|
+
id: import_v44.z.string(),
|
|
851
|
+
summary: import_v44.z.array(reasoningSummaryPartSchema),
|
|
852
|
+
status: import_v44.z.string()
|
|
853
|
+
})
|
|
854
|
+
]);
|
|
855
|
+
var xaiResponsesUsageSchema = import_v44.z.object({
|
|
856
|
+
input_tokens: import_v44.z.number(),
|
|
857
|
+
output_tokens: import_v44.z.number(),
|
|
858
|
+
total_tokens: import_v44.z.number().optional(),
|
|
859
|
+
input_tokens_details: import_v44.z.object({
|
|
860
|
+
cached_tokens: import_v44.z.number().optional()
|
|
861
|
+
}).optional(),
|
|
862
|
+
output_tokens_details: import_v44.z.object({
|
|
863
|
+
reasoning_tokens: import_v44.z.number().optional()
|
|
864
|
+
}).optional(),
|
|
865
|
+
num_sources_used: import_v44.z.number().optional(),
|
|
866
|
+
num_server_side_tools_used: import_v44.z.number().optional()
|
|
867
|
+
});
|
|
868
|
+
var xaiResponsesResponseSchema = import_v44.z.object({
|
|
869
|
+
id: import_v44.z.string().nullish(),
|
|
870
|
+
created_at: import_v44.z.number().nullish(),
|
|
871
|
+
model: import_v44.z.string().nullish(),
|
|
872
|
+
object: import_v44.z.literal("response"),
|
|
873
|
+
output: import_v44.z.array(outputItemSchema),
|
|
874
|
+
usage: xaiResponsesUsageSchema,
|
|
875
|
+
status: import_v44.z.string()
|
|
876
|
+
});
|
|
877
|
+
var xaiResponsesChunkSchema = import_v44.z.union([
|
|
878
|
+
import_v44.z.object({
|
|
879
|
+
type: import_v44.z.literal("response.created"),
|
|
880
|
+
response: xaiResponsesResponseSchema.partial({ usage: true, status: true })
|
|
881
|
+
}),
|
|
882
|
+
import_v44.z.object({
|
|
883
|
+
type: import_v44.z.literal("response.in_progress"),
|
|
884
|
+
response: xaiResponsesResponseSchema.partial({ usage: true, status: true })
|
|
885
|
+
}),
|
|
886
|
+
import_v44.z.object({
|
|
887
|
+
type: import_v44.z.literal("response.output_item.added"),
|
|
888
|
+
item: outputItemSchema,
|
|
889
|
+
output_index: import_v44.z.number()
|
|
890
|
+
}),
|
|
891
|
+
import_v44.z.object({
|
|
892
|
+
type: import_v44.z.literal("response.output_item.done"),
|
|
893
|
+
item: outputItemSchema,
|
|
894
|
+
output_index: import_v44.z.number()
|
|
895
|
+
}),
|
|
896
|
+
import_v44.z.object({
|
|
897
|
+
type: import_v44.z.literal("response.content_part.added"),
|
|
898
|
+
item_id: import_v44.z.string(),
|
|
899
|
+
output_index: import_v44.z.number(),
|
|
900
|
+
content_index: import_v44.z.number(),
|
|
901
|
+
part: messageContentPartSchema
|
|
902
|
+
}),
|
|
903
|
+
import_v44.z.object({
|
|
904
|
+
type: import_v44.z.literal("response.content_part.done"),
|
|
905
|
+
item_id: import_v44.z.string(),
|
|
906
|
+
output_index: import_v44.z.number(),
|
|
907
|
+
content_index: import_v44.z.number(),
|
|
908
|
+
part: messageContentPartSchema
|
|
909
|
+
}),
|
|
910
|
+
import_v44.z.object({
|
|
911
|
+
type: import_v44.z.literal("response.output_text.delta"),
|
|
912
|
+
item_id: import_v44.z.string(),
|
|
913
|
+
output_index: import_v44.z.number(),
|
|
914
|
+
content_index: import_v44.z.number(),
|
|
915
|
+
delta: import_v44.z.string(),
|
|
916
|
+
logprobs: import_v44.z.array(import_v44.z.any()).optional()
|
|
917
|
+
}),
|
|
918
|
+
import_v44.z.object({
|
|
919
|
+
type: import_v44.z.literal("response.output_text.done"),
|
|
920
|
+
item_id: import_v44.z.string(),
|
|
921
|
+
output_index: import_v44.z.number(),
|
|
922
|
+
content_index: import_v44.z.number(),
|
|
923
|
+
text: import_v44.z.string(),
|
|
924
|
+
logprobs: import_v44.z.array(import_v44.z.any()).optional(),
|
|
925
|
+
annotations: import_v44.z.array(annotationSchema).optional()
|
|
926
|
+
}),
|
|
927
|
+
import_v44.z.object({
|
|
928
|
+
type: import_v44.z.literal("response.output_text.annotation.added"),
|
|
929
|
+
item_id: import_v44.z.string(),
|
|
930
|
+
output_index: import_v44.z.number(),
|
|
931
|
+
content_index: import_v44.z.number(),
|
|
932
|
+
annotation_index: import_v44.z.number(),
|
|
933
|
+
annotation: annotationSchema
|
|
934
|
+
}),
|
|
935
|
+
import_v44.z.object({
|
|
936
|
+
type: import_v44.z.literal("response.reasoning_summary_part.added"),
|
|
937
|
+
item_id: import_v44.z.string(),
|
|
938
|
+
output_index: import_v44.z.number(),
|
|
939
|
+
summary_index: import_v44.z.number(),
|
|
940
|
+
part: reasoningSummaryPartSchema
|
|
941
|
+
}),
|
|
942
|
+
import_v44.z.object({
|
|
943
|
+
type: import_v44.z.literal("response.reasoning_summary_part.done"),
|
|
944
|
+
item_id: import_v44.z.string(),
|
|
945
|
+
output_index: import_v44.z.number(),
|
|
946
|
+
summary_index: import_v44.z.number(),
|
|
947
|
+
part: reasoningSummaryPartSchema
|
|
948
|
+
}),
|
|
949
|
+
import_v44.z.object({
|
|
950
|
+
type: import_v44.z.literal("response.reasoning_summary_text.delta"),
|
|
951
|
+
item_id: import_v44.z.string(),
|
|
952
|
+
output_index: import_v44.z.number(),
|
|
953
|
+
summary_index: import_v44.z.number(),
|
|
954
|
+
delta: import_v44.z.string()
|
|
955
|
+
}),
|
|
956
|
+
import_v44.z.object({
|
|
957
|
+
type: import_v44.z.literal("response.reasoning_summary_text.done"),
|
|
958
|
+
item_id: import_v44.z.string(),
|
|
959
|
+
output_index: import_v44.z.number(),
|
|
960
|
+
summary_index: import_v44.z.number(),
|
|
961
|
+
text: import_v44.z.string()
|
|
962
|
+
}),
|
|
963
|
+
import_v44.z.object({
|
|
964
|
+
type: import_v44.z.literal("response.done"),
|
|
965
|
+
response: xaiResponsesResponseSchema
|
|
966
|
+
}),
|
|
967
|
+
import_v44.z.object({
|
|
968
|
+
type: import_v44.z.literal("response.completed"),
|
|
969
|
+
response: xaiResponsesResponseSchema
|
|
970
|
+
})
|
|
971
|
+
]);
|
|
972
|
+
|
|
973
|
+
// src/responses/map-xai-responses-finish-reason.ts
|
|
974
|
+
function mapXaiResponsesFinishReason(finishReason) {
|
|
975
|
+
switch (finishReason) {
|
|
976
|
+
case "stop":
|
|
977
|
+
case "completed":
|
|
978
|
+
return "stop";
|
|
979
|
+
case "length":
|
|
980
|
+
return "length";
|
|
981
|
+
case "tool_calls":
|
|
982
|
+
case "function_call":
|
|
983
|
+
return "tool-calls";
|
|
984
|
+
case "content_filter":
|
|
985
|
+
return "content-filter";
|
|
986
|
+
default:
|
|
987
|
+
return "unknown";
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
// src/responses/xai-responses-options.ts
|
|
992
|
+
var import_v45 = require("zod/v4");
|
|
993
|
+
var xaiResponsesProviderOptions = import_v45.z.object({
|
|
994
|
+
/**
|
|
995
|
+
* Constrains how hard a reasoning model thinks before responding.
|
|
996
|
+
* Possible values are `low` (uses fewer reasoning tokens), `medium` and `high` (uses more reasoning tokens).
|
|
997
|
+
*/
|
|
998
|
+
reasoningEffort: import_v45.z.enum(["low", "medium", "high"]).optional(),
|
|
999
|
+
/**
|
|
1000
|
+
* Whether to store the input message(s) and model response for later retrieval.
|
|
1001
|
+
* @default true
|
|
1002
|
+
*/
|
|
1003
|
+
store: import_v45.z.boolean().optional(),
|
|
1004
|
+
/**
|
|
1005
|
+
* The ID of the previous response from the model.
|
|
1006
|
+
*/
|
|
1007
|
+
previousResponseId: import_v45.z.string().optional()
|
|
1008
|
+
});
|
|
1009
|
+
|
|
1010
|
+
// src/responses/convert-to-xai-responses-input.ts
|
|
1011
|
+
async function convertToXaiResponsesInput({
|
|
1012
|
+
prompt
|
|
1013
|
+
}) {
|
|
1014
|
+
var _a, _b, _c, _d;
|
|
1015
|
+
const input = [];
|
|
1016
|
+
const inputWarnings = [];
|
|
1017
|
+
for (const message of prompt) {
|
|
1018
|
+
switch (message.role) {
|
|
1019
|
+
case "system": {
|
|
1020
|
+
input.push({
|
|
1021
|
+
role: "system",
|
|
1022
|
+
content: message.content
|
|
1023
|
+
});
|
|
1024
|
+
break;
|
|
1025
|
+
}
|
|
1026
|
+
case "user": {
|
|
1027
|
+
let userContent = "";
|
|
1028
|
+
for (const block of message.content) {
|
|
1029
|
+
switch (block.type) {
|
|
1030
|
+
case "text": {
|
|
1031
|
+
userContent += block.text;
|
|
1032
|
+
break;
|
|
1033
|
+
}
|
|
1034
|
+
case "file": {
|
|
1035
|
+
inputWarnings.push({
|
|
1036
|
+
type: "other",
|
|
1037
|
+
message: `xAI Responses API does not support ${block.type} in user messages`
|
|
1038
|
+
});
|
|
1039
|
+
break;
|
|
1040
|
+
}
|
|
1041
|
+
default: {
|
|
1042
|
+
const _exhaustiveCheck = block;
|
|
1043
|
+
inputWarnings.push({
|
|
1044
|
+
type: "other",
|
|
1045
|
+
message: "xAI Responses API does not support this content type in user messages"
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
input.push({
|
|
1051
|
+
role: "user",
|
|
1052
|
+
content: userContent
|
|
1053
|
+
});
|
|
1054
|
+
break;
|
|
1055
|
+
}
|
|
1056
|
+
case "assistant": {
|
|
1057
|
+
for (const part of message.content) {
|
|
1058
|
+
switch (part.type) {
|
|
1059
|
+
case "text": {
|
|
1060
|
+
const id = typeof ((_b = (_a = part.providerOptions) == null ? void 0 : _a.xai) == null ? void 0 : _b.itemId) === "string" ? part.providerOptions.xai.itemId : void 0;
|
|
1061
|
+
input.push({
|
|
1062
|
+
role: "assistant",
|
|
1063
|
+
content: part.text,
|
|
1064
|
+
id
|
|
1065
|
+
});
|
|
1066
|
+
break;
|
|
1067
|
+
}
|
|
1068
|
+
case "tool-call": {
|
|
1069
|
+
if (part.providerExecuted) {
|
|
1070
|
+
break;
|
|
1071
|
+
}
|
|
1072
|
+
const id = typeof ((_d = (_c = part.providerOptions) == null ? void 0 : _c.xai) == null ? void 0 : _d.itemId) === "string" ? part.providerOptions.xai.itemId : void 0;
|
|
1073
|
+
input.push({
|
|
1074
|
+
type: "function_call",
|
|
1075
|
+
id: id != null ? id : part.toolCallId,
|
|
1076
|
+
call_id: part.toolCallId,
|
|
1077
|
+
name: part.toolName,
|
|
1078
|
+
arguments: JSON.stringify(part.input),
|
|
1079
|
+
status: "completed"
|
|
1080
|
+
});
|
|
1081
|
+
break;
|
|
1082
|
+
}
|
|
1083
|
+
case "tool-result": {
|
|
1084
|
+
break;
|
|
1085
|
+
}
|
|
1086
|
+
case "reasoning":
|
|
1087
|
+
case "file": {
|
|
1088
|
+
inputWarnings.push({
|
|
1089
|
+
type: "other",
|
|
1090
|
+
message: `xAI Responses API does not support ${part.type} in assistant messages`
|
|
1091
|
+
});
|
|
1092
|
+
break;
|
|
1093
|
+
}
|
|
1094
|
+
default: {
|
|
1095
|
+
const _exhaustiveCheck = part;
|
|
1096
|
+
inputWarnings.push({
|
|
1097
|
+
type: "other",
|
|
1098
|
+
message: "xAI Responses API does not support this content type in assistant messages"
|
|
1099
|
+
});
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
break;
|
|
1104
|
+
}
|
|
1105
|
+
case "tool": {
|
|
1106
|
+
for (const part of message.content) {
|
|
1107
|
+
const output = part.output;
|
|
1108
|
+
let outputValue;
|
|
1109
|
+
switch (output.type) {
|
|
1110
|
+
case "text":
|
|
1111
|
+
case "error-text":
|
|
1112
|
+
outputValue = output.value;
|
|
1113
|
+
break;
|
|
1114
|
+
case "json":
|
|
1115
|
+
case "error-json":
|
|
1116
|
+
outputValue = JSON.stringify(output.value);
|
|
1117
|
+
break;
|
|
1118
|
+
case "content":
|
|
1119
|
+
outputValue = output.value.map((item) => {
|
|
1120
|
+
if (item.type === "text") {
|
|
1121
|
+
return item.text;
|
|
1122
|
+
}
|
|
1123
|
+
return "";
|
|
1124
|
+
}).join("");
|
|
1125
|
+
break;
|
|
1126
|
+
default: {
|
|
1127
|
+
const _exhaustiveCheck = output;
|
|
1128
|
+
outputValue = "";
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
input.push({
|
|
1132
|
+
type: "function_call_output",
|
|
1133
|
+
call_id: part.toolCallId,
|
|
1134
|
+
output: outputValue
|
|
1135
|
+
});
|
|
1136
|
+
}
|
|
1137
|
+
break;
|
|
1138
|
+
}
|
|
1139
|
+
default: {
|
|
1140
|
+
const _exhaustiveCheck = message;
|
|
1141
|
+
inputWarnings.push({
|
|
1142
|
+
type: "other",
|
|
1143
|
+
message: "unsupported message role"
|
|
1144
|
+
});
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
return { input, inputWarnings };
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
// src/responses/xai-responses-prepare-tools.ts
|
|
1152
|
+
var import_provider3 = require("@ai-sdk/provider");
|
|
1153
|
+
var import_provider_utils6 = require("@ai-sdk/provider-utils");
|
|
1154
|
+
|
|
1155
|
+
// src/tool/web-search.ts
|
|
1156
|
+
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
|
1157
|
+
var import_v46 = require("zod/v4");
|
|
1158
|
+
var webSearchArgsSchema = (0, import_provider_utils4.lazySchema)(
|
|
1159
|
+
() => (0, import_provider_utils4.zodSchema)(
|
|
1160
|
+
import_v46.z.object({
|
|
1161
|
+
allowedDomains: import_v46.z.array(import_v46.z.string()).max(5).optional(),
|
|
1162
|
+
excludedDomains: import_v46.z.array(import_v46.z.string()).max(5).optional(),
|
|
1163
|
+
enableImageUnderstanding: import_v46.z.boolean().optional()
|
|
1164
|
+
})
|
|
1165
|
+
)
|
|
1166
|
+
);
|
|
1167
|
+
var webSearchOutputSchema = (0, import_provider_utils4.lazySchema)(
|
|
1168
|
+
() => (0, import_provider_utils4.zodSchema)(
|
|
1169
|
+
import_v46.z.object({
|
|
1170
|
+
query: import_v46.z.string(),
|
|
1171
|
+
sources: import_v46.z.array(
|
|
1172
|
+
import_v46.z.object({
|
|
1173
|
+
title: import_v46.z.string(),
|
|
1174
|
+
url: import_v46.z.string(),
|
|
1175
|
+
snippet: import_v46.z.string()
|
|
1176
|
+
})
|
|
1177
|
+
)
|
|
1178
|
+
})
|
|
1179
|
+
)
|
|
1180
|
+
);
|
|
1181
|
+
var webSearchToolFactory = (0, import_provider_utils4.createProviderDefinedToolFactoryWithOutputSchema)({
|
|
1182
|
+
id: "xai.web_search",
|
|
1183
|
+
name: "web_search",
|
|
1184
|
+
inputSchema: (0, import_provider_utils4.lazySchema)(() => (0, import_provider_utils4.zodSchema)(import_v46.z.object({}))),
|
|
1185
|
+
outputSchema: webSearchOutputSchema
|
|
1186
|
+
});
|
|
1187
|
+
var webSearch = (args = {}) => webSearchToolFactory(args);
|
|
1188
|
+
|
|
1189
|
+
// src/tool/x-search.ts
|
|
1190
|
+
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
|
1191
|
+
var import_v47 = require("zod/v4");
|
|
1192
|
+
var xSearchArgsSchema = (0, import_provider_utils5.lazySchema)(
|
|
1193
|
+
() => (0, import_provider_utils5.zodSchema)(
|
|
1194
|
+
import_v47.z.object({
|
|
1195
|
+
allowedXHandles: import_v47.z.array(import_v47.z.string()).max(10).optional(),
|
|
1196
|
+
excludedXHandles: import_v47.z.array(import_v47.z.string()).max(10).optional(),
|
|
1197
|
+
fromDate: import_v47.z.string().optional(),
|
|
1198
|
+
toDate: import_v47.z.string().optional(),
|
|
1199
|
+
enableImageUnderstanding: import_v47.z.boolean().optional(),
|
|
1200
|
+
enableVideoUnderstanding: import_v47.z.boolean().optional()
|
|
1201
|
+
})
|
|
1202
|
+
)
|
|
1203
|
+
);
|
|
1204
|
+
var xSearchOutputSchema = (0, import_provider_utils5.lazySchema)(
|
|
1205
|
+
() => (0, import_provider_utils5.zodSchema)(
|
|
1206
|
+
import_v47.z.object({
|
|
1207
|
+
query: import_v47.z.string(),
|
|
1208
|
+
posts: import_v47.z.array(
|
|
1209
|
+
import_v47.z.object({
|
|
1210
|
+
author: import_v47.z.string(),
|
|
1211
|
+
text: import_v47.z.string(),
|
|
1212
|
+
url: import_v47.z.string(),
|
|
1213
|
+
likes: import_v47.z.number()
|
|
1214
|
+
})
|
|
1215
|
+
)
|
|
1216
|
+
})
|
|
1217
|
+
)
|
|
1218
|
+
);
|
|
1219
|
+
var xSearchToolFactory = (0, import_provider_utils5.createProviderDefinedToolFactoryWithOutputSchema)({
|
|
1220
|
+
id: "xai.x_search",
|
|
1221
|
+
name: "x_search",
|
|
1222
|
+
inputSchema: (0, import_provider_utils5.lazySchema)(() => (0, import_provider_utils5.zodSchema)(import_v47.z.object({}))),
|
|
1223
|
+
outputSchema: xSearchOutputSchema
|
|
1224
|
+
});
|
|
1225
|
+
var xSearch = (args = {}) => xSearchToolFactory(args);
|
|
1226
|
+
|
|
1227
|
+
// src/responses/xai-responses-prepare-tools.ts
|
|
1228
|
+
async function prepareResponsesTools({
|
|
1229
|
+
tools,
|
|
1230
|
+
toolChoice
|
|
1231
|
+
}) {
|
|
1232
|
+
const normalizedTools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
1233
|
+
const toolWarnings = [];
|
|
1234
|
+
if (normalizedTools == null) {
|
|
1235
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
1236
|
+
}
|
|
1237
|
+
const xaiTools2 = [];
|
|
1238
|
+
const toolByName = /* @__PURE__ */ new Map();
|
|
1239
|
+
for (const tool of normalizedTools) {
|
|
1240
|
+
toolByName.set(tool.name, tool);
|
|
1241
|
+
if (tool.type === "provider-defined") {
|
|
1242
|
+
switch (tool.id) {
|
|
1243
|
+
case "xai.web_search": {
|
|
1244
|
+
const args = await (0, import_provider_utils6.validateTypes)({
|
|
1245
|
+
value: tool.args,
|
|
1246
|
+
schema: webSearchArgsSchema
|
|
1247
|
+
});
|
|
1248
|
+
xaiTools2.push({
|
|
1249
|
+
type: "web_search",
|
|
1250
|
+
allowed_domains: args.allowedDomains,
|
|
1251
|
+
excluded_domains: args.excludedDomains,
|
|
1252
|
+
enable_image_understanding: args.enableImageUnderstanding
|
|
1253
|
+
});
|
|
1254
|
+
break;
|
|
1255
|
+
}
|
|
1256
|
+
case "xai.x_search": {
|
|
1257
|
+
const args = await (0, import_provider_utils6.validateTypes)({
|
|
1258
|
+
value: tool.args,
|
|
1259
|
+
schema: xSearchArgsSchema
|
|
1260
|
+
});
|
|
1261
|
+
xaiTools2.push({
|
|
1262
|
+
type: "x_search",
|
|
1263
|
+
allowed_x_handles: args.allowedXHandles,
|
|
1264
|
+
excluded_x_handles: args.excludedXHandles,
|
|
1265
|
+
from_date: args.fromDate,
|
|
1266
|
+
to_date: args.toDate,
|
|
1267
|
+
enable_image_understanding: args.enableImageUnderstanding,
|
|
1268
|
+
enable_video_understanding: args.enableVideoUnderstanding
|
|
1269
|
+
});
|
|
1270
|
+
break;
|
|
1271
|
+
}
|
|
1272
|
+
case "xai.code_execution": {
|
|
1273
|
+
xaiTools2.push({
|
|
1274
|
+
type: "code_interpreter"
|
|
1275
|
+
});
|
|
1276
|
+
break;
|
|
1277
|
+
}
|
|
1278
|
+
case "xai.view_image": {
|
|
1279
|
+
xaiTools2.push({
|
|
1280
|
+
type: "view_image"
|
|
1281
|
+
});
|
|
1282
|
+
break;
|
|
1283
|
+
}
|
|
1284
|
+
case "xai.view_x_video": {
|
|
1285
|
+
xaiTools2.push({
|
|
1286
|
+
type: "view_x_video"
|
|
1287
|
+
});
|
|
1288
|
+
break;
|
|
1289
|
+
}
|
|
1290
|
+
case "xai.file_search": {
|
|
1291
|
+
xaiTools2.push({
|
|
1292
|
+
type: "file_search"
|
|
1293
|
+
});
|
|
1294
|
+
break;
|
|
1295
|
+
}
|
|
1296
|
+
case "xai.mcp": {
|
|
1297
|
+
xaiTools2.push({
|
|
1298
|
+
type: "mcp"
|
|
1299
|
+
});
|
|
1300
|
+
break;
|
|
1301
|
+
}
|
|
1302
|
+
default: {
|
|
1303
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
1304
|
+
break;
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
} else {
|
|
1308
|
+
xaiTools2.push({
|
|
1309
|
+
type: "function",
|
|
1310
|
+
function: {
|
|
1311
|
+
name: tool.name,
|
|
1312
|
+
description: tool.description,
|
|
1313
|
+
parameters: tool.inputSchema
|
|
1314
|
+
}
|
|
1315
|
+
});
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
if (toolChoice == null) {
|
|
1319
|
+
return { tools: xaiTools2, toolChoice: void 0, toolWarnings };
|
|
1320
|
+
}
|
|
1321
|
+
const type = toolChoice.type;
|
|
1322
|
+
switch (type) {
|
|
1323
|
+
case "auto":
|
|
1324
|
+
case "none":
|
|
1325
|
+
return { tools: xaiTools2, toolChoice: type, toolWarnings };
|
|
1326
|
+
case "required":
|
|
1327
|
+
return { tools: xaiTools2, toolChoice: "required", toolWarnings };
|
|
1328
|
+
case "tool": {
|
|
1329
|
+
const selectedTool = toolByName.get(toolChoice.toolName);
|
|
1330
|
+
if (selectedTool == null) {
|
|
1331
|
+
return {
|
|
1332
|
+
tools: xaiTools2,
|
|
1333
|
+
toolChoice: void 0,
|
|
1334
|
+
toolWarnings
|
|
1335
|
+
};
|
|
1336
|
+
}
|
|
1337
|
+
if (selectedTool.type === "provider-defined") {
|
|
1338
|
+
switch (selectedTool.id) {
|
|
1339
|
+
case "xai.web_search":
|
|
1340
|
+
return {
|
|
1341
|
+
tools: xaiTools2,
|
|
1342
|
+
toolChoice: { type: "web_search" },
|
|
1343
|
+
toolWarnings
|
|
1344
|
+
};
|
|
1345
|
+
case "xai.x_search":
|
|
1346
|
+
return {
|
|
1347
|
+
tools: xaiTools2,
|
|
1348
|
+
toolChoice: { type: "x_search" },
|
|
1349
|
+
toolWarnings
|
|
1350
|
+
};
|
|
1351
|
+
case "xai.code_execution":
|
|
1352
|
+
return {
|
|
1353
|
+
tools: xaiTools2,
|
|
1354
|
+
toolChoice: { type: "code_interpreter" },
|
|
1355
|
+
toolWarnings
|
|
1356
|
+
};
|
|
1357
|
+
case "xai.view_image":
|
|
1358
|
+
return {
|
|
1359
|
+
tools: xaiTools2,
|
|
1360
|
+
toolChoice: { type: "view_image" },
|
|
1361
|
+
toolWarnings
|
|
1362
|
+
};
|
|
1363
|
+
case "xai.view_x_video":
|
|
1364
|
+
return {
|
|
1365
|
+
tools: xaiTools2,
|
|
1366
|
+
toolChoice: { type: "view_x_video" },
|
|
1367
|
+
toolWarnings
|
|
1368
|
+
};
|
|
1369
|
+
case "xai.file_search":
|
|
1370
|
+
return {
|
|
1371
|
+
tools: xaiTools2,
|
|
1372
|
+
toolChoice: { type: "file_search" },
|
|
1373
|
+
toolWarnings
|
|
1374
|
+
};
|
|
1375
|
+
case "xai.mcp":
|
|
1376
|
+
return {
|
|
1377
|
+
tools: xaiTools2,
|
|
1378
|
+
toolChoice: { type: "mcp" },
|
|
1379
|
+
toolWarnings
|
|
1380
|
+
};
|
|
1381
|
+
default:
|
|
1382
|
+
toolWarnings.push({ type: "unsupported-tool", tool: selectedTool });
|
|
1383
|
+
return { tools: xaiTools2, toolChoice: void 0, toolWarnings };
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
return {
|
|
1387
|
+
tools: xaiTools2,
|
|
1388
|
+
toolChoice: { type: "function", name: selectedTool.name },
|
|
1389
|
+
toolWarnings
|
|
1390
|
+
};
|
|
1391
|
+
}
|
|
1392
|
+
default: {
|
|
1393
|
+
const _exhaustiveCheck = type;
|
|
1394
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
1395
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
1396
|
+
});
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
// src/responses/xai-responses-language-model.ts
|
|
1402
|
+
var XaiResponsesLanguageModel = class {
|
|
1403
|
+
constructor(modelId, config) {
|
|
1404
|
+
this.specificationVersion = "v2";
|
|
1405
|
+
this.supportedUrls = {
|
|
1406
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
1407
|
+
};
|
|
1408
|
+
this.modelId = modelId;
|
|
1409
|
+
this.config = config;
|
|
1410
|
+
}
|
|
1411
|
+
get provider() {
|
|
1412
|
+
return this.config.provider;
|
|
1413
|
+
}
|
|
1414
|
+
async getArgs({
|
|
1415
|
+
prompt,
|
|
1416
|
+
maxOutputTokens,
|
|
1417
|
+
temperature,
|
|
1418
|
+
topP,
|
|
1419
|
+
stopSequences,
|
|
1420
|
+
seed,
|
|
1421
|
+
providerOptions,
|
|
1422
|
+
tools,
|
|
1423
|
+
toolChoice
|
|
1424
|
+
}) {
|
|
1425
|
+
var _a, _b, _c, _d;
|
|
1426
|
+
const warnings = [];
|
|
1427
|
+
const options = (_a = await (0, import_provider_utils7.parseProviderOptions)({
|
|
1428
|
+
provider: "xai",
|
|
1429
|
+
providerOptions,
|
|
1430
|
+
schema: xaiResponsesProviderOptions
|
|
1431
|
+
})) != null ? _a : {};
|
|
1432
|
+
if (stopSequences != null) {
|
|
1433
|
+
warnings.push({
|
|
1434
|
+
type: "unsupported-setting",
|
|
1435
|
+
setting: "stopSequences"
|
|
1436
|
+
});
|
|
1437
|
+
}
|
|
1438
|
+
const webSearchToolName = (_b = tools == null ? void 0 : tools.find(
|
|
1439
|
+
(tool) => tool.type === "provider-defined" && tool.id === "xai.web_search"
|
|
1440
|
+
)) == null ? void 0 : _b.name;
|
|
1441
|
+
const xSearchToolName = (_c = tools == null ? void 0 : tools.find(
|
|
1442
|
+
(tool) => tool.type === "provider-defined" && tool.id === "xai.x_search"
|
|
1443
|
+
)) == null ? void 0 : _c.name;
|
|
1444
|
+
const codeExecutionToolName = (_d = tools == null ? void 0 : tools.find(
|
|
1445
|
+
(tool) => tool.type === "provider-defined" && tool.id === "xai.code_execution"
|
|
1446
|
+
)) == null ? void 0 : _d.name;
|
|
1447
|
+
const { input, inputWarnings } = await convertToXaiResponsesInput({
|
|
1448
|
+
prompt,
|
|
1449
|
+
store: true
|
|
1450
|
+
});
|
|
1451
|
+
warnings.push(...inputWarnings);
|
|
1452
|
+
const {
|
|
1453
|
+
tools: xaiTools2,
|
|
1454
|
+
toolChoice: xaiToolChoice,
|
|
1455
|
+
toolWarnings
|
|
1456
|
+
} = await prepareResponsesTools({
|
|
1457
|
+
tools,
|
|
1458
|
+
toolChoice
|
|
1459
|
+
});
|
|
1460
|
+
warnings.push(...toolWarnings);
|
|
1461
|
+
const baseArgs = {
|
|
1462
|
+
model: this.modelId,
|
|
1463
|
+
input,
|
|
1464
|
+
max_tokens: maxOutputTokens,
|
|
1465
|
+
temperature,
|
|
1466
|
+
top_p: topP,
|
|
1467
|
+
seed,
|
|
1468
|
+
...options.reasoningEffort != null && {
|
|
1469
|
+
reasoning: { effort: options.reasoningEffort }
|
|
1470
|
+
},
|
|
1471
|
+
...options.store === false && {
|
|
1472
|
+
store: options.store,
|
|
1473
|
+
include: ["reasoning.encrypted_content"]
|
|
1474
|
+
},
|
|
1475
|
+
...options.previousResponseId != null && {
|
|
1476
|
+
previous_response_id: options.previousResponseId
|
|
1477
|
+
}
|
|
1478
|
+
};
|
|
1479
|
+
if (xaiTools2 && xaiTools2.length > 0) {
|
|
1480
|
+
baseArgs.tools = xaiTools2;
|
|
1481
|
+
}
|
|
1482
|
+
if (xaiToolChoice != null) {
|
|
1483
|
+
baseArgs.tool_choice = xaiToolChoice;
|
|
1484
|
+
}
|
|
1485
|
+
return {
|
|
1486
|
+
args: baseArgs,
|
|
1487
|
+
warnings,
|
|
1488
|
+
webSearchToolName,
|
|
1489
|
+
xSearchToolName,
|
|
1490
|
+
codeExecutionToolName
|
|
1491
|
+
};
|
|
1492
|
+
}
|
|
1493
|
+
async doGenerate(options) {
|
|
1494
|
+
var _a, _b, _c;
|
|
1495
|
+
const {
|
|
1496
|
+
args: body,
|
|
1497
|
+
warnings,
|
|
1498
|
+
webSearchToolName,
|
|
1499
|
+
xSearchToolName,
|
|
1500
|
+
codeExecutionToolName
|
|
1501
|
+
} = await this.getArgs(options);
|
|
1502
|
+
const {
|
|
1503
|
+
responseHeaders,
|
|
1504
|
+
value: response,
|
|
1505
|
+
rawValue: rawResponse
|
|
1506
|
+
} = await (0, import_provider_utils7.postJsonToApi)({
|
|
1507
|
+
url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/responses`,
|
|
1508
|
+
headers: (0, import_provider_utils7.combineHeaders)(this.config.headers(), options.headers),
|
|
1509
|
+
body,
|
|
1510
|
+
failedResponseHandler: xaiFailedResponseHandler,
|
|
1511
|
+
successfulResponseHandler: (0, import_provider_utils7.createJsonResponseHandler)(
|
|
1512
|
+
xaiResponsesResponseSchema
|
|
1513
|
+
),
|
|
1514
|
+
abortSignal: options.abortSignal,
|
|
1515
|
+
fetch: this.config.fetch
|
|
1516
|
+
});
|
|
1517
|
+
const content = [];
|
|
1518
|
+
const webSearchSubTools = [
|
|
1519
|
+
"web_search",
|
|
1520
|
+
"web_search_with_snippets",
|
|
1521
|
+
"browse_page"
|
|
1522
|
+
];
|
|
1523
|
+
const xSearchSubTools = [
|
|
1524
|
+
"x_user_search",
|
|
1525
|
+
"x_keyword_search",
|
|
1526
|
+
"x_semantic_search",
|
|
1527
|
+
"x_thread_fetch"
|
|
1528
|
+
];
|
|
1529
|
+
for (const part of response.output) {
|
|
1530
|
+
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") {
|
|
1531
|
+
let toolName = part.name;
|
|
1532
|
+
if (webSearchSubTools.includes(part.name)) {
|
|
1533
|
+
toolName = webSearchToolName != null ? webSearchToolName : "web_search";
|
|
1534
|
+
} else if (xSearchSubTools.includes(part.name)) {
|
|
1535
|
+
toolName = xSearchToolName != null ? xSearchToolName : "x_search";
|
|
1536
|
+
} else if (part.name === "code_execution") {
|
|
1537
|
+
toolName = codeExecutionToolName != null ? codeExecutionToolName : "code_execution";
|
|
1538
|
+
}
|
|
1539
|
+
content.push({
|
|
1540
|
+
type: "tool-call",
|
|
1541
|
+
toolCallId: part.id,
|
|
1542
|
+
toolName,
|
|
1543
|
+
input: part.arguments,
|
|
1544
|
+
providerExecuted: true
|
|
1545
|
+
});
|
|
1546
|
+
continue;
|
|
1547
|
+
}
|
|
1548
|
+
switch (part.type) {
|
|
1549
|
+
case "message": {
|
|
1550
|
+
for (const contentPart of part.content) {
|
|
1551
|
+
if (contentPart.text) {
|
|
1552
|
+
content.push({
|
|
1553
|
+
type: "text",
|
|
1554
|
+
text: contentPart.text
|
|
1555
|
+
});
|
|
1556
|
+
}
|
|
1557
|
+
if (contentPart.annotations) {
|
|
1558
|
+
for (const annotation of contentPart.annotations) {
|
|
1559
|
+
if (annotation.type === "url_citation" && "url" in annotation) {
|
|
1560
|
+
content.push({
|
|
1561
|
+
type: "source",
|
|
1562
|
+
sourceType: "url",
|
|
1563
|
+
id: this.config.generateId(),
|
|
1564
|
+
url: annotation.url,
|
|
1565
|
+
title: (_b = annotation.title) != null ? _b : annotation.url
|
|
1566
|
+
});
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
break;
|
|
1572
|
+
}
|
|
1573
|
+
case "function_call": {
|
|
1574
|
+
content.push({
|
|
1575
|
+
type: "tool-call",
|
|
1576
|
+
toolCallId: part.call_id,
|
|
1577
|
+
toolName: part.name,
|
|
1578
|
+
input: part.arguments
|
|
1579
|
+
});
|
|
1580
|
+
break;
|
|
1581
|
+
}
|
|
1582
|
+
default: {
|
|
1583
|
+
break;
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
return {
|
|
1588
|
+
content,
|
|
1589
|
+
finishReason: mapXaiResponsesFinishReason(response.status),
|
|
1590
|
+
usage: {
|
|
1591
|
+
inputTokens: response.usage.input_tokens,
|
|
1592
|
+
outputTokens: response.usage.output_tokens,
|
|
1593
|
+
totalTokens: response.usage.total_tokens,
|
|
1594
|
+
reasoningTokens: (_c = response.usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens
|
|
1595
|
+
},
|
|
1596
|
+
request: { body },
|
|
1597
|
+
response: {
|
|
1598
|
+
...getResponseMetadata(response),
|
|
1599
|
+
headers: responseHeaders,
|
|
1600
|
+
body: rawResponse
|
|
1601
|
+
},
|
|
1602
|
+
warnings
|
|
1603
|
+
};
|
|
1604
|
+
}
|
|
1605
|
+
async doStream(options) {
|
|
1606
|
+
var _a;
|
|
1607
|
+
const {
|
|
1608
|
+
args,
|
|
1609
|
+
warnings,
|
|
1610
|
+
webSearchToolName,
|
|
1611
|
+
xSearchToolName,
|
|
1612
|
+
codeExecutionToolName
|
|
1613
|
+
} = await this.getArgs(options);
|
|
1614
|
+
const body = {
|
|
1615
|
+
...args,
|
|
1616
|
+
stream: true
|
|
1617
|
+
};
|
|
1618
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils7.postJsonToApi)({
|
|
1619
|
+
url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/responses`,
|
|
1620
|
+
headers: (0, import_provider_utils7.combineHeaders)(this.config.headers(), options.headers),
|
|
1621
|
+
body,
|
|
1622
|
+
failedResponseHandler: xaiFailedResponseHandler,
|
|
1623
|
+
successfulResponseHandler: (0, import_provider_utils7.createEventSourceResponseHandler)(
|
|
1624
|
+
xaiResponsesChunkSchema
|
|
1625
|
+
),
|
|
1626
|
+
abortSignal: options.abortSignal,
|
|
1627
|
+
fetch: this.config.fetch
|
|
1628
|
+
});
|
|
1629
|
+
let finishReason = "unknown";
|
|
1630
|
+
const usage = {
|
|
1631
|
+
inputTokens: void 0,
|
|
1632
|
+
outputTokens: void 0,
|
|
1633
|
+
totalTokens: void 0
|
|
1634
|
+
};
|
|
1635
|
+
let isFirstChunk = true;
|
|
1636
|
+
const contentBlocks = {};
|
|
1637
|
+
const seenToolCalls = /* @__PURE__ */ new Set();
|
|
1638
|
+
const self = this;
|
|
1639
|
+
return {
|
|
1640
|
+
stream: response.pipeThrough(
|
|
1641
|
+
new TransformStream({
|
|
1642
|
+
start(controller) {
|
|
1643
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
1644
|
+
},
|
|
1645
|
+
transform(chunk, controller) {
|
|
1646
|
+
var _a2, _b, _c, _d;
|
|
1647
|
+
if (options.includeRawChunks) {
|
|
1648
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
1649
|
+
}
|
|
1650
|
+
if (!chunk.success) {
|
|
1651
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
|
1652
|
+
return;
|
|
1653
|
+
}
|
|
1654
|
+
const event = chunk.value;
|
|
1655
|
+
if (event.type === "response.created" || event.type === "response.in_progress") {
|
|
1656
|
+
if (isFirstChunk) {
|
|
1657
|
+
controller.enqueue({
|
|
1658
|
+
type: "response-metadata",
|
|
1659
|
+
...getResponseMetadata(event.response)
|
|
1660
|
+
});
|
|
1661
|
+
isFirstChunk = false;
|
|
1662
|
+
}
|
|
1663
|
+
return;
|
|
1664
|
+
}
|
|
1665
|
+
if (event.type === "response.reasoning_summary_part.added") {
|
|
1666
|
+
const blockId = `reasoning-${event.item_id}`;
|
|
1667
|
+
controller.enqueue({
|
|
1668
|
+
type: "reasoning-start",
|
|
1669
|
+
id: blockId
|
|
1670
|
+
});
|
|
1671
|
+
}
|
|
1672
|
+
if (event.type === "response.reasoning_summary_text.delta") {
|
|
1673
|
+
const blockId = `reasoning-${event.item_id}`;
|
|
1674
|
+
controller.enqueue({
|
|
1675
|
+
type: "reasoning-delta",
|
|
1676
|
+
id: blockId,
|
|
1677
|
+
delta: event.delta
|
|
1678
|
+
});
|
|
1679
|
+
return;
|
|
1680
|
+
}
|
|
1681
|
+
if (event.type === "response.reasoning_summary_text.done") {
|
|
1682
|
+
const blockId = `reasoning-${event.item_id}`;
|
|
1683
|
+
controller.enqueue({
|
|
1684
|
+
type: "reasoning-end",
|
|
1685
|
+
id: blockId
|
|
1686
|
+
});
|
|
1687
|
+
}
|
|
1688
|
+
if (event.type === "response.output_text.delta") {
|
|
1689
|
+
const blockId = `text-${event.item_id}`;
|
|
1690
|
+
if (contentBlocks[blockId] == null) {
|
|
1691
|
+
contentBlocks[blockId] = { type: "text" };
|
|
1692
|
+
controller.enqueue({
|
|
1693
|
+
type: "text-start",
|
|
1694
|
+
id: blockId
|
|
1695
|
+
});
|
|
1696
|
+
}
|
|
1697
|
+
controller.enqueue({
|
|
1698
|
+
type: "text-delta",
|
|
1699
|
+
id: blockId,
|
|
1700
|
+
delta: event.delta
|
|
1701
|
+
});
|
|
1702
|
+
return;
|
|
1703
|
+
}
|
|
1704
|
+
if (event.type === "response.output_text.done") {
|
|
1705
|
+
if (event.annotations) {
|
|
1706
|
+
for (const annotation of event.annotations) {
|
|
1707
|
+
if (annotation.type === "url_citation" && "url" in annotation) {
|
|
1708
|
+
controller.enqueue({
|
|
1709
|
+
type: "source",
|
|
1710
|
+
sourceType: "url",
|
|
1711
|
+
id: self.config.generateId(),
|
|
1712
|
+
url: annotation.url,
|
|
1713
|
+
title: (_a2 = annotation.title) != null ? _a2 : annotation.url
|
|
1714
|
+
});
|
|
1715
|
+
}
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
return;
|
|
1719
|
+
}
|
|
1720
|
+
if (event.type === "response.output_text.annotation.added") {
|
|
1721
|
+
const annotation = event.annotation;
|
|
1722
|
+
if (annotation.type === "url_citation" && "url" in annotation) {
|
|
1723
|
+
controller.enqueue({
|
|
1724
|
+
type: "source",
|
|
1725
|
+
sourceType: "url",
|
|
1726
|
+
id: self.config.generateId(),
|
|
1727
|
+
url: annotation.url,
|
|
1728
|
+
title: (_b = annotation.title) != null ? _b : annotation.url
|
|
1729
|
+
});
|
|
1730
|
+
}
|
|
1731
|
+
return;
|
|
1732
|
+
}
|
|
1733
|
+
if (event.type === "response.done" || event.type === "response.completed") {
|
|
1734
|
+
const response2 = event.response;
|
|
1735
|
+
if (response2.usage) {
|
|
1736
|
+
usage.inputTokens = response2.usage.input_tokens;
|
|
1737
|
+
usage.outputTokens = response2.usage.output_tokens;
|
|
1738
|
+
usage.totalTokens = response2.usage.total_tokens;
|
|
1739
|
+
usage.reasoningTokens = (_c = response2.usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens;
|
|
1740
|
+
}
|
|
1741
|
+
if (response2.status) {
|
|
1742
|
+
finishReason = mapXaiResponsesFinishReason(response2.status);
|
|
1743
|
+
}
|
|
1744
|
+
return;
|
|
1745
|
+
}
|
|
1746
|
+
if (event.type === "response.output_item.added" || event.type === "response.output_item.done") {
|
|
1747
|
+
const part = event.item;
|
|
1748
|
+
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") {
|
|
1749
|
+
if (!seenToolCalls.has(part.id)) {
|
|
1750
|
+
seenToolCalls.add(part.id);
|
|
1751
|
+
const webSearchSubTools = [
|
|
1752
|
+
"web_search",
|
|
1753
|
+
"web_search_with_snippets",
|
|
1754
|
+
"browse_page"
|
|
1755
|
+
];
|
|
1756
|
+
const xSearchSubTools = [
|
|
1757
|
+
"x_user_search",
|
|
1758
|
+
"x_keyword_search",
|
|
1759
|
+
"x_semantic_search",
|
|
1760
|
+
"x_thread_fetch"
|
|
1761
|
+
];
|
|
1762
|
+
let toolName = part.name;
|
|
1763
|
+
if (webSearchSubTools.includes(part.name)) {
|
|
1764
|
+
toolName = webSearchToolName != null ? webSearchToolName : "web_search";
|
|
1765
|
+
} else if (xSearchSubTools.includes(part.name)) {
|
|
1766
|
+
toolName = xSearchToolName != null ? xSearchToolName : "x_search";
|
|
1767
|
+
} else if (part.name === "code_execution") {
|
|
1768
|
+
toolName = codeExecutionToolName != null ? codeExecutionToolName : "code_execution";
|
|
1769
|
+
}
|
|
1770
|
+
controller.enqueue({
|
|
1771
|
+
type: "tool-input-start",
|
|
1772
|
+
id: part.id,
|
|
1773
|
+
toolName
|
|
1774
|
+
});
|
|
1775
|
+
controller.enqueue({
|
|
1776
|
+
type: "tool-input-delta",
|
|
1777
|
+
id: part.id,
|
|
1778
|
+
delta: part.arguments
|
|
1779
|
+
});
|
|
1780
|
+
controller.enqueue({
|
|
1781
|
+
type: "tool-input-end",
|
|
1782
|
+
id: part.id
|
|
1783
|
+
});
|
|
1784
|
+
controller.enqueue({
|
|
1785
|
+
type: "tool-call",
|
|
1786
|
+
toolCallId: part.id,
|
|
1787
|
+
toolName,
|
|
1788
|
+
input: part.arguments,
|
|
1789
|
+
providerExecuted: true
|
|
1790
|
+
});
|
|
1791
|
+
}
|
|
1792
|
+
return;
|
|
1793
|
+
}
|
|
1794
|
+
if (part.type === "message") {
|
|
1795
|
+
for (const contentPart of part.content) {
|
|
1796
|
+
if (contentPart.text && contentPart.text.length > 0) {
|
|
1797
|
+
const blockId = `text-${part.id}`;
|
|
1798
|
+
if (contentBlocks[blockId] == null) {
|
|
1799
|
+
contentBlocks[blockId] = { type: "text" };
|
|
1800
|
+
controller.enqueue({
|
|
1801
|
+
type: "text-start",
|
|
1802
|
+
id: blockId
|
|
1803
|
+
});
|
|
1804
|
+
}
|
|
1805
|
+
controller.enqueue({
|
|
1806
|
+
type: "text-delta",
|
|
1807
|
+
id: blockId,
|
|
1808
|
+
delta: contentPart.text
|
|
1809
|
+
});
|
|
1810
|
+
}
|
|
1811
|
+
if (contentPart.annotations) {
|
|
1812
|
+
for (const annotation of contentPart.annotations) {
|
|
1813
|
+
if (annotation.type === "url_citation" && "url" in annotation) {
|
|
1814
|
+
controller.enqueue({
|
|
1815
|
+
type: "source",
|
|
1816
|
+
sourceType: "url",
|
|
1817
|
+
id: self.config.generateId(),
|
|
1818
|
+
url: annotation.url,
|
|
1819
|
+
title: (_d = annotation.title) != null ? _d : annotation.url
|
|
1820
|
+
});
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
} else if (part.type === "function_call") {
|
|
1826
|
+
if (!seenToolCalls.has(part.call_id)) {
|
|
1827
|
+
seenToolCalls.add(part.call_id);
|
|
1828
|
+
controller.enqueue({
|
|
1829
|
+
type: "tool-input-start",
|
|
1830
|
+
id: part.call_id,
|
|
1831
|
+
toolName: part.name
|
|
1832
|
+
});
|
|
1833
|
+
controller.enqueue({
|
|
1834
|
+
type: "tool-input-delta",
|
|
1835
|
+
id: part.call_id,
|
|
1836
|
+
delta: part.arguments
|
|
1837
|
+
});
|
|
1838
|
+
controller.enqueue({
|
|
1839
|
+
type: "tool-input-end",
|
|
1840
|
+
id: part.call_id
|
|
1841
|
+
});
|
|
1842
|
+
controller.enqueue({
|
|
1843
|
+
type: "tool-call",
|
|
1844
|
+
toolCallId: part.call_id,
|
|
1845
|
+
toolName: part.name,
|
|
1846
|
+
input: part.arguments
|
|
1847
|
+
});
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
},
|
|
1852
|
+
flush(controller) {
|
|
1853
|
+
for (const [blockId, block] of Object.entries(contentBlocks)) {
|
|
1854
|
+
if (block.type === "text") {
|
|
1855
|
+
controller.enqueue({
|
|
1856
|
+
type: "text-end",
|
|
1857
|
+
id: blockId
|
|
1858
|
+
});
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
controller.enqueue({ type: "finish", finishReason, usage });
|
|
1862
|
+
}
|
|
1863
|
+
})
|
|
1864
|
+
),
|
|
1865
|
+
request: { body },
|
|
1866
|
+
response: { headers: responseHeaders }
|
|
1867
|
+
};
|
|
1868
|
+
}
|
|
1869
|
+
};
|
|
1870
|
+
|
|
1871
|
+
// src/tool/code-execution.ts
|
|
1872
|
+
var import_provider_utils8 = require("@ai-sdk/provider-utils");
|
|
1873
|
+
var import_v48 = require("zod/v4");
|
|
1874
|
+
var codeExecutionOutputSchema = import_v48.z.object({
|
|
1875
|
+
output: import_v48.z.string().describe("the output of the code execution"),
|
|
1876
|
+
error: import_v48.z.string().optional().describe("any error that occurred")
|
|
1877
|
+
});
|
|
1878
|
+
var codeExecutionToolFactory = (0, import_provider_utils8.createProviderDefinedToolFactoryWithOutputSchema)({
|
|
1879
|
+
id: "xai.code_execution",
|
|
1880
|
+
name: "code_execution",
|
|
1881
|
+
inputSchema: import_v48.z.object({}).describe("no input parameters"),
|
|
1882
|
+
outputSchema: codeExecutionOutputSchema
|
|
1883
|
+
});
|
|
1884
|
+
var codeExecution = (args = {}) => codeExecutionToolFactory(args);
|
|
1885
|
+
|
|
1886
|
+
// src/tool/view-image.ts
|
|
1887
|
+
var import_provider_utils9 = require("@ai-sdk/provider-utils");
|
|
1888
|
+
var import_v49 = require("zod/v4");
|
|
1889
|
+
var viewImageOutputSchema = import_v49.z.object({
|
|
1890
|
+
description: import_v49.z.string().describe("description of the image"),
|
|
1891
|
+
objects: import_v49.z.array(import_v49.z.string()).optional().describe("objects detected in the image")
|
|
1892
|
+
});
|
|
1893
|
+
var viewImageToolFactory = (0, import_provider_utils9.createProviderDefinedToolFactoryWithOutputSchema)({
|
|
1894
|
+
id: "xai.view_image",
|
|
1895
|
+
name: "view_image",
|
|
1896
|
+
inputSchema: import_v49.z.object({}).describe("no input parameters"),
|
|
1897
|
+
outputSchema: viewImageOutputSchema
|
|
1898
|
+
});
|
|
1899
|
+
var viewImage = (args = {}) => viewImageToolFactory(args);
|
|
1900
|
+
|
|
1901
|
+
// src/tool/view-x-video.ts
|
|
1902
|
+
var import_provider_utils10 = require("@ai-sdk/provider-utils");
|
|
1903
|
+
var import_v410 = require("zod/v4");
|
|
1904
|
+
var viewXVideoOutputSchema = import_v410.z.object({
|
|
1905
|
+
transcript: import_v410.z.string().optional().describe("transcript of the video"),
|
|
1906
|
+
description: import_v410.z.string().describe("description of the video content"),
|
|
1907
|
+
duration: import_v410.z.number().optional().describe("duration in seconds")
|
|
1908
|
+
});
|
|
1909
|
+
var viewXVideoToolFactory = (0, import_provider_utils10.createProviderDefinedToolFactoryWithOutputSchema)({
|
|
1910
|
+
id: "xai.view_x_video",
|
|
1911
|
+
name: "view_x_video",
|
|
1912
|
+
inputSchema: import_v410.z.object({}).describe("no input parameters"),
|
|
1913
|
+
outputSchema: viewXVideoOutputSchema
|
|
1914
|
+
});
|
|
1915
|
+
var viewXVideo = (args = {}) => viewXVideoToolFactory(args);
|
|
1916
|
+
|
|
1917
|
+
// src/tool/index.ts
|
|
1918
|
+
var xaiTools = {
|
|
1919
|
+
codeExecution,
|
|
1920
|
+
viewImage,
|
|
1921
|
+
viewXVideo,
|
|
1922
|
+
webSearch,
|
|
1923
|
+
xSearch
|
|
1924
|
+
};
|
|
1925
|
+
|
|
769
1926
|
// src/version.ts
|
|
770
|
-
var VERSION = true ? "2.0.
|
|
1927
|
+
var VERSION = true ? "2.0.36" : "0.0.0-test";
|
|
771
1928
|
|
|
772
1929
|
// src/xai-provider.ts
|
|
773
1930
|
var xaiErrorStructure = {
|
|
@@ -776,12 +1933,12 @@ var xaiErrorStructure = {
|
|
|
776
1933
|
};
|
|
777
1934
|
function createXai(options = {}) {
|
|
778
1935
|
var _a;
|
|
779
|
-
const baseURL = (0,
|
|
1936
|
+
const baseURL = (0, import_provider_utils11.withoutTrailingSlash)(
|
|
780
1937
|
(_a = options.baseURL) != null ? _a : "https://api.x.ai/v1"
|
|
781
1938
|
);
|
|
782
|
-
const getHeaders = () => (0,
|
|
1939
|
+
const getHeaders = () => (0, import_provider_utils11.withUserAgentSuffix)(
|
|
783
1940
|
{
|
|
784
|
-
Authorization: `Bearer ${(0,
|
|
1941
|
+
Authorization: `Bearer ${(0, import_provider_utils11.loadApiKey)({
|
|
785
1942
|
apiKey: options.apiKey,
|
|
786
1943
|
environmentVariableName: "XAI_API_KEY",
|
|
787
1944
|
description: "xAI API key"
|
|
@@ -790,12 +1947,21 @@ function createXai(options = {}) {
|
|
|
790
1947
|
},
|
|
791
1948
|
`ai-sdk/xai/${VERSION}`
|
|
792
1949
|
);
|
|
793
|
-
const
|
|
1950
|
+
const createChatLanguageModel = (modelId) => {
|
|
794
1951
|
return new XaiChatLanguageModel(modelId, {
|
|
795
1952
|
provider: "xai.chat",
|
|
796
1953
|
baseURL,
|
|
797
1954
|
headers: getHeaders,
|
|
798
|
-
generateId:
|
|
1955
|
+
generateId: import_provider_utils11.generateId,
|
|
1956
|
+
fetch: options.fetch
|
|
1957
|
+
});
|
|
1958
|
+
};
|
|
1959
|
+
const createResponsesLanguageModel = (modelId) => {
|
|
1960
|
+
return new XaiResponsesLanguageModel(modelId, {
|
|
1961
|
+
provider: "xai.responses",
|
|
1962
|
+
baseURL,
|
|
1963
|
+
headers: getHeaders,
|
|
1964
|
+
generateId: import_provider_utils11.generateId,
|
|
799
1965
|
fetch: options.fetch
|
|
800
1966
|
});
|
|
801
1967
|
};
|
|
@@ -808,21 +1974,30 @@ function createXai(options = {}) {
|
|
|
808
1974
|
errorStructure: xaiErrorStructure
|
|
809
1975
|
});
|
|
810
1976
|
};
|
|
811
|
-
const provider = (modelId) =>
|
|
812
|
-
provider.
|
|
813
|
-
provider.
|
|
1977
|
+
const provider = (modelId) => createChatLanguageModel(modelId);
|
|
1978
|
+
provider.specificationVersion = "v2";
|
|
1979
|
+
provider.languageModel = createChatLanguageModel;
|
|
1980
|
+
provider.chat = createChatLanguageModel;
|
|
1981
|
+
provider.responses = createResponsesLanguageModel;
|
|
814
1982
|
provider.textEmbeddingModel = (modelId) => {
|
|
815
|
-
throw new
|
|
1983
|
+
throw new import_provider4.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
|
816
1984
|
};
|
|
817
1985
|
provider.imageModel = createImageModel;
|
|
818
1986
|
provider.image = createImageModel;
|
|
1987
|
+
provider.tools = xaiTools;
|
|
819
1988
|
return provider;
|
|
820
1989
|
}
|
|
821
1990
|
var xai = createXai();
|
|
822
1991
|
// Annotate the CommonJS export names for ESM import in node:
|
|
823
1992
|
0 && (module.exports = {
|
|
824
1993
|
VERSION,
|
|
1994
|
+
codeExecution,
|
|
825
1995
|
createXai,
|
|
826
|
-
|
|
1996
|
+
viewImage,
|
|
1997
|
+
viewXVideo,
|
|
1998
|
+
webSearch,
|
|
1999
|
+
xSearch,
|
|
2000
|
+
xai,
|
|
2001
|
+
xaiTools
|
|
827
2002
|
});
|
|
828
2003
|
//# sourceMappingURL=index.js.map
|