@ai-sdk/xai 2.0.34 → 2.0.35

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