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