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