@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.mjs CHANGED
@@ -268,12 +268,12 @@ function prepareTools({
268
268
  if (tools == null) {
269
269
  return { tools: void 0, toolChoice: void 0, toolWarnings };
270
270
  }
271
- const xaiTools = [];
271
+ const xaiTools2 = [];
272
272
  for (const tool of tools) {
273
273
  if (tool.type === "provider-defined") {
274
274
  toolWarnings.push({ type: "unsupported-tool", tool });
275
275
  } else {
276
- xaiTools.push({
276
+ xaiTools2.push({
277
277
  type: "function",
278
278
  function: {
279
279
  name: tool.name,
@@ -284,18 +284,18 @@ function prepareTools({
284
284
  }
285
285
  }
286
286
  if (toolChoice == null) {
287
- return { tools: xaiTools, toolChoice: void 0, toolWarnings };
287
+ return { tools: xaiTools2, toolChoice: void 0, toolWarnings };
288
288
  }
289
289
  const type = toolChoice.type;
290
290
  switch (type) {
291
291
  case "auto":
292
292
  case "none":
293
- return { tools: xaiTools, toolChoice: type, toolWarnings };
293
+ return { tools: xaiTools2, toolChoice: type, toolWarnings };
294
294
  case "required":
295
- return { tools: xaiTools, toolChoice: "required", toolWarnings };
295
+ return { tools: xaiTools2, toolChoice: "required", toolWarnings };
296
296
  case "tool":
297
297
  return {
298
- tools: xaiTools,
298
+ tools: xaiTools2,
299
299
  toolChoice: {
300
300
  type: "function",
301
301
  function: { name: toolChoice.toolName }
@@ -373,7 +373,7 @@ var XaiChatLanguageModel = class {
373
373
  const { messages, warnings: messageWarnings } = convertToXaiChatMessages(prompt);
374
374
  warnings.push(...messageWarnings);
375
375
  const {
376
- tools: xaiTools,
376
+ tools: xaiTools2,
377
377
  toolChoice: xaiToolChoice,
378
378
  toolWarnings
379
379
  } = prepareTools({
@@ -438,7 +438,7 @@ var XaiChatLanguageModel = class {
438
438
  // messages in xai format
439
439
  messages,
440
440
  // tools in xai format
441
- tools: xaiTools,
441
+ tools: xaiTools2,
442
442
  tool_choice: xaiToolChoice
443
443
  };
444
444
  return {
@@ -757,8 +757,1090 @@ var xaiChatChunkSchema = z3.object({
757
757
  citations: z3.array(z3.string().url()).nullish()
758
758
  });
759
759
 
760
+ // src/responses/xai-responses-language-model.ts
761
+ import {
762
+ combineHeaders as combineHeaders2,
763
+ createEventSourceResponseHandler as createEventSourceResponseHandler2,
764
+ createJsonResponseHandler as createJsonResponseHandler2,
765
+ parseProviderOptions as parseProviderOptions2,
766
+ postJsonToApi as postJsonToApi2
767
+ } from "@ai-sdk/provider-utils";
768
+
769
+ // src/responses/xai-responses-api.ts
770
+ import { z as z4 } from "zod/v4";
771
+ var annotationSchema = z4.union([
772
+ z4.object({
773
+ type: z4.literal("url_citation"),
774
+ url: z4.string(),
775
+ title: z4.string().optional()
776
+ }),
777
+ z4.object({
778
+ type: z4.string()
779
+ })
780
+ ]);
781
+ var messageContentPartSchema = z4.object({
782
+ type: z4.string(),
783
+ text: z4.string().optional(),
784
+ logprobs: z4.array(z4.any()).optional(),
785
+ annotations: z4.array(annotationSchema).optional()
786
+ });
787
+ var toolCallSchema = z4.object({
788
+ name: z4.string(),
789
+ arguments: z4.string(),
790
+ call_id: z4.string(),
791
+ id: z4.string(),
792
+ status: z4.string()
793
+ });
794
+ var outputItemSchema = z4.discriminatedUnion("type", [
795
+ z4.object({
796
+ type: z4.literal("web_search_call"),
797
+ ...toolCallSchema.shape
798
+ }),
799
+ z4.object({
800
+ type: z4.literal("x_search_call"),
801
+ ...toolCallSchema.shape
802
+ }),
803
+ z4.object({
804
+ type: z4.literal("code_interpreter_call"),
805
+ ...toolCallSchema.shape
806
+ }),
807
+ z4.object({
808
+ type: z4.literal("code_execution_call"),
809
+ ...toolCallSchema.shape
810
+ }),
811
+ z4.object({
812
+ type: z4.literal("view_image_call"),
813
+ ...toolCallSchema.shape
814
+ }),
815
+ z4.object({
816
+ type: z4.literal("view_x_video_call"),
817
+ ...toolCallSchema.shape
818
+ }),
819
+ z4.object({
820
+ type: z4.literal("message"),
821
+ role: z4.string(),
822
+ content: z4.array(messageContentPartSchema),
823
+ id: z4.string(),
824
+ status: z4.string()
825
+ }),
826
+ z4.object({
827
+ type: z4.literal("function_call"),
828
+ name: z4.string(),
829
+ arguments: z4.string(),
830
+ call_id: z4.string(),
831
+ id: z4.string()
832
+ })
833
+ ]);
834
+ var xaiResponsesUsageSchema = z4.object({
835
+ input_tokens: z4.number(),
836
+ output_tokens: z4.number(),
837
+ total_tokens: z4.number().optional(),
838
+ input_tokens_details: z4.object({
839
+ cached_tokens: z4.number().optional()
840
+ }).optional(),
841
+ output_tokens_details: z4.object({
842
+ reasoning_tokens: z4.number().optional()
843
+ }).optional(),
844
+ num_sources_used: z4.number().optional(),
845
+ num_server_side_tools_used: z4.number().optional()
846
+ });
847
+ var xaiResponsesResponseSchema = z4.object({
848
+ id: z4.string().nullish(),
849
+ created_at: z4.number().nullish(),
850
+ model: z4.string().nullish(),
851
+ object: z4.literal("response"),
852
+ output: z4.array(outputItemSchema),
853
+ usage: xaiResponsesUsageSchema,
854
+ status: z4.string()
855
+ });
856
+ var xaiResponsesChunkSchema = z4.union([
857
+ z4.object({
858
+ type: z4.literal("response.created"),
859
+ response: xaiResponsesResponseSchema.partial({ usage: true, status: true })
860
+ }),
861
+ z4.object({
862
+ type: z4.literal("response.in_progress"),
863
+ response: xaiResponsesResponseSchema.partial({ usage: true, status: true })
864
+ }),
865
+ z4.object({
866
+ type: z4.literal("response.output_item.added"),
867
+ item: outputItemSchema,
868
+ output_index: z4.number()
869
+ }),
870
+ z4.object({
871
+ type: z4.literal("response.output_item.done"),
872
+ item: outputItemSchema,
873
+ output_index: z4.number()
874
+ }),
875
+ z4.object({
876
+ type: z4.literal("response.content_part.added"),
877
+ item_id: z4.string(),
878
+ output_index: z4.number(),
879
+ content_index: z4.number(),
880
+ part: messageContentPartSchema
881
+ }),
882
+ z4.object({
883
+ type: z4.literal("response.content_part.done"),
884
+ item_id: z4.string(),
885
+ output_index: z4.number(),
886
+ content_index: z4.number(),
887
+ part: messageContentPartSchema
888
+ }),
889
+ z4.object({
890
+ type: z4.literal("response.output_text.delta"),
891
+ item_id: z4.string(),
892
+ output_index: z4.number(),
893
+ content_index: z4.number(),
894
+ delta: z4.string(),
895
+ logprobs: z4.array(z4.any()).optional()
896
+ }),
897
+ z4.object({
898
+ type: z4.literal("response.output_text.done"),
899
+ item_id: z4.string(),
900
+ output_index: z4.number(),
901
+ content_index: z4.number(),
902
+ text: z4.string(),
903
+ logprobs: z4.array(z4.any()).optional(),
904
+ annotations: z4.array(annotationSchema).optional()
905
+ }),
906
+ z4.object({
907
+ type: z4.literal("response.output_text.annotation.added"),
908
+ item_id: z4.string(),
909
+ output_index: z4.number(),
910
+ content_index: z4.number(),
911
+ annotation_index: z4.number(),
912
+ annotation: annotationSchema
913
+ }),
914
+ z4.object({
915
+ type: z4.literal("response.done"),
916
+ response: xaiResponsesResponseSchema
917
+ }),
918
+ z4.object({
919
+ type: z4.literal("response.completed"),
920
+ response: xaiResponsesResponseSchema
921
+ })
922
+ ]);
923
+
924
+ // src/responses/map-xai-responses-finish-reason.ts
925
+ function mapXaiResponsesFinishReason(finishReason) {
926
+ switch (finishReason) {
927
+ case "stop":
928
+ case "completed":
929
+ return "stop";
930
+ case "length":
931
+ return "length";
932
+ case "tool_calls":
933
+ case "function_call":
934
+ return "tool-calls";
935
+ case "content_filter":
936
+ return "content-filter";
937
+ default:
938
+ return "unknown";
939
+ }
940
+ }
941
+
942
+ // src/responses/xai-responses-options.ts
943
+ import { z as z5 } from "zod/v4";
944
+ var xaiResponsesProviderOptions = z5.object({
945
+ reasoningEffort: z5.enum(["low", "high"]).optional()
946
+ });
947
+
948
+ // src/responses/convert-to-xai-responses-input.ts
949
+ async function convertToXaiResponsesInput({
950
+ prompt
951
+ }) {
952
+ var _a, _b, _c, _d;
953
+ const input = [];
954
+ const inputWarnings = [];
955
+ for (const message of prompt) {
956
+ switch (message.role) {
957
+ case "system": {
958
+ input.push({
959
+ role: "system",
960
+ content: message.content
961
+ });
962
+ break;
963
+ }
964
+ case "user": {
965
+ let userContent = "";
966
+ for (const block of message.content) {
967
+ switch (block.type) {
968
+ case "text": {
969
+ userContent += block.text;
970
+ break;
971
+ }
972
+ case "file": {
973
+ inputWarnings.push({
974
+ type: "other",
975
+ message: `xAI Responses API does not support ${block.type} in user messages`
976
+ });
977
+ break;
978
+ }
979
+ default: {
980
+ const _exhaustiveCheck = block;
981
+ inputWarnings.push({
982
+ type: "other",
983
+ message: "xAI Responses API does not support this content type in user messages"
984
+ });
985
+ }
986
+ }
987
+ }
988
+ input.push({
989
+ role: "user",
990
+ content: userContent
991
+ });
992
+ break;
993
+ }
994
+ case "assistant": {
995
+ for (const part of message.content) {
996
+ switch (part.type) {
997
+ case "text": {
998
+ const id = typeof ((_b = (_a = part.providerOptions) == null ? void 0 : _a.xai) == null ? void 0 : _b.itemId) === "string" ? part.providerOptions.xai.itemId : void 0;
999
+ input.push({
1000
+ role: "assistant",
1001
+ content: part.text,
1002
+ id
1003
+ });
1004
+ break;
1005
+ }
1006
+ case "tool-call": {
1007
+ if (part.providerExecuted) {
1008
+ break;
1009
+ }
1010
+ const id = typeof ((_d = (_c = part.providerOptions) == null ? void 0 : _c.xai) == null ? void 0 : _d.itemId) === "string" ? part.providerOptions.xai.itemId : void 0;
1011
+ input.push({
1012
+ type: "function_call",
1013
+ id: id != null ? id : part.toolCallId,
1014
+ call_id: part.toolCallId,
1015
+ name: part.toolName,
1016
+ arguments: JSON.stringify(part.input),
1017
+ status: "completed"
1018
+ });
1019
+ break;
1020
+ }
1021
+ case "tool-result": {
1022
+ break;
1023
+ }
1024
+ case "reasoning":
1025
+ case "file": {
1026
+ inputWarnings.push({
1027
+ type: "other",
1028
+ message: `xAI Responses API does not support ${part.type} in assistant messages`
1029
+ });
1030
+ break;
1031
+ }
1032
+ default: {
1033
+ const _exhaustiveCheck = part;
1034
+ inputWarnings.push({
1035
+ type: "other",
1036
+ message: "xAI Responses API does not support this content type in assistant messages"
1037
+ });
1038
+ }
1039
+ }
1040
+ }
1041
+ break;
1042
+ }
1043
+ case "tool": {
1044
+ for (const part of message.content) {
1045
+ const output = part.output;
1046
+ let outputValue;
1047
+ switch (output.type) {
1048
+ case "text":
1049
+ case "error-text":
1050
+ outputValue = output.value;
1051
+ break;
1052
+ case "json":
1053
+ case "error-json":
1054
+ outputValue = JSON.stringify(output.value);
1055
+ break;
1056
+ case "content":
1057
+ outputValue = output.value.map((item) => {
1058
+ if (item.type === "text") {
1059
+ return item.text;
1060
+ }
1061
+ return "";
1062
+ }).join("");
1063
+ break;
1064
+ default: {
1065
+ const _exhaustiveCheck = output;
1066
+ outputValue = "";
1067
+ }
1068
+ }
1069
+ input.push({
1070
+ type: "function_call_output",
1071
+ call_id: part.toolCallId,
1072
+ output: outputValue
1073
+ });
1074
+ }
1075
+ break;
1076
+ }
1077
+ default: {
1078
+ const _exhaustiveCheck = message;
1079
+ inputWarnings.push({
1080
+ type: "other",
1081
+ message: "unsupported message role"
1082
+ });
1083
+ }
1084
+ }
1085
+ }
1086
+ return { input, inputWarnings };
1087
+ }
1088
+
1089
+ // src/responses/xai-responses-prepare-tools.ts
1090
+ import {
1091
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError3
1092
+ } from "@ai-sdk/provider";
1093
+ import { validateTypes } from "@ai-sdk/provider-utils";
1094
+
1095
+ // src/tool/web-search.ts
1096
+ import {
1097
+ createProviderDefinedToolFactoryWithOutputSchema,
1098
+ lazySchema,
1099
+ zodSchema
1100
+ } from "@ai-sdk/provider-utils";
1101
+ import { z as z6 } from "zod/v4";
1102
+ var webSearchArgsSchema = lazySchema(
1103
+ () => zodSchema(
1104
+ z6.object({
1105
+ allowedDomains: z6.array(z6.string()).max(5).optional(),
1106
+ excludedDomains: z6.array(z6.string()).max(5).optional(),
1107
+ enableImageUnderstanding: z6.boolean().optional()
1108
+ })
1109
+ )
1110
+ );
1111
+ var webSearchOutputSchema = lazySchema(
1112
+ () => zodSchema(
1113
+ z6.object({
1114
+ query: z6.string(),
1115
+ sources: z6.array(
1116
+ z6.object({
1117
+ title: z6.string(),
1118
+ url: z6.string(),
1119
+ snippet: z6.string()
1120
+ })
1121
+ )
1122
+ })
1123
+ )
1124
+ );
1125
+ var webSearchToolFactory = createProviderDefinedToolFactoryWithOutputSchema({
1126
+ id: "xai.web_search",
1127
+ name: "web_search",
1128
+ inputSchema: lazySchema(() => zodSchema(z6.object({}))),
1129
+ outputSchema: webSearchOutputSchema
1130
+ });
1131
+ var webSearch = (args = {}) => webSearchToolFactory(args);
1132
+
1133
+ // src/tool/x-search.ts
1134
+ import {
1135
+ createProviderDefinedToolFactoryWithOutputSchema as createProviderDefinedToolFactoryWithOutputSchema2,
1136
+ lazySchema as lazySchema2,
1137
+ zodSchema as zodSchema2
1138
+ } from "@ai-sdk/provider-utils";
1139
+ import { z as z7 } from "zod/v4";
1140
+ var xSearchArgsSchema = lazySchema2(
1141
+ () => zodSchema2(
1142
+ z7.object({
1143
+ allowedXHandles: z7.array(z7.string()).max(10).optional(),
1144
+ excludedXHandles: z7.array(z7.string()).max(10).optional(),
1145
+ fromDate: z7.string().optional(),
1146
+ toDate: z7.string().optional(),
1147
+ enableImageUnderstanding: z7.boolean().optional(),
1148
+ enableVideoUnderstanding: z7.boolean().optional()
1149
+ })
1150
+ )
1151
+ );
1152
+ var xSearchOutputSchema = lazySchema2(
1153
+ () => zodSchema2(
1154
+ z7.object({
1155
+ query: z7.string(),
1156
+ posts: z7.array(
1157
+ z7.object({
1158
+ author: z7.string(),
1159
+ text: z7.string(),
1160
+ url: z7.string(),
1161
+ likes: z7.number()
1162
+ })
1163
+ )
1164
+ })
1165
+ )
1166
+ );
1167
+ var xSearchToolFactory = createProviderDefinedToolFactoryWithOutputSchema2({
1168
+ id: "xai.x_search",
1169
+ name: "x_search",
1170
+ inputSchema: lazySchema2(() => zodSchema2(z7.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 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 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 UnsupportedFunctionalityError3({
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 = "v2";
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 parseProviderOptions2({
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 postJsonToApi2({
1446
+ url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/responses`,
1447
+ headers: combineHeaders2(this.config.headers(), options.headers),
1448
+ body,
1449
+ failedResponseHandler: xaiFailedResponseHandler,
1450
+ successfulResponseHandler: createJsonResponseHandler2(
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 postJsonToApi2({
1558
+ url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/responses`,
1559
+ headers: combineHeaders2(this.config.headers(), options.headers),
1560
+ body,
1561
+ failedResponseHandler: xaiFailedResponseHandler,
1562
+ successfulResponseHandler: createEventSourceResponseHandler2(
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
+ import { createProviderDefinedToolFactoryWithOutputSchema as createProviderDefinedToolFactoryWithOutputSchema3 } from "@ai-sdk/provider-utils";
1789
+ import { z as z8 } from "zod/v4";
1790
+ var codeExecutionOutputSchema = z8.object({
1791
+ output: z8.string().describe("the output of the code execution"),
1792
+ error: z8.string().optional().describe("any error that occurred")
1793
+ });
1794
+ var codeExecutionToolFactory = createProviderDefinedToolFactoryWithOutputSchema3({
1795
+ id: "xai.code_execution",
1796
+ name: "code_execution",
1797
+ inputSchema: z8.object({}).describe("no input parameters"),
1798
+ outputSchema: codeExecutionOutputSchema
1799
+ });
1800
+ var codeExecution = (args = {}) => codeExecutionToolFactory(args);
1801
+
1802
+ // src/tool/view-image.ts
1803
+ import { createProviderDefinedToolFactoryWithOutputSchema as createProviderDefinedToolFactoryWithOutputSchema4 } from "@ai-sdk/provider-utils";
1804
+ import { z as z9 } from "zod/v4";
1805
+ var viewImageOutputSchema = z9.object({
1806
+ description: z9.string().describe("description of the image"),
1807
+ objects: z9.array(z9.string()).optional().describe("objects detected in the image")
1808
+ });
1809
+ var viewImageToolFactory = createProviderDefinedToolFactoryWithOutputSchema4({
1810
+ id: "xai.view_image",
1811
+ name: "view_image",
1812
+ inputSchema: z9.object({}).describe("no input parameters"),
1813
+ outputSchema: viewImageOutputSchema
1814
+ });
1815
+ var viewImage = (args = {}) => viewImageToolFactory(args);
1816
+
1817
+ // src/tool/view-x-video.ts
1818
+ import { createProviderDefinedToolFactoryWithOutputSchema as createProviderDefinedToolFactoryWithOutputSchema5 } from "@ai-sdk/provider-utils";
1819
+ import { z as z10 } from "zod/v4";
1820
+ var viewXVideoOutputSchema = z10.object({
1821
+ transcript: z10.string().optional().describe("transcript of the video"),
1822
+ description: z10.string().describe("description of the video content"),
1823
+ duration: z10.number().optional().describe("duration in seconds")
1824
+ });
1825
+ var viewXVideoToolFactory = createProviderDefinedToolFactoryWithOutputSchema5({
1826
+ id: "xai.view_x_video",
1827
+ name: "view_x_video",
1828
+ inputSchema: z10.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
+
760
1842
  // src/version.ts
761
- var VERSION = true ? "2.0.34" : "0.0.0-test";
1843
+ var VERSION = true ? "2.0.35" : "0.0.0-test";
762
1844
 
763
1845
  // src/xai-provider.ts
764
1846
  var xaiErrorStructure = {
@@ -781,7 +1863,7 @@ function createXai(options = {}) {
781
1863
  },
782
1864
  `ai-sdk/xai/${VERSION}`
783
1865
  );
784
- const createLanguageModel = (modelId) => {
1866
+ const createChatLanguageModel = (modelId) => {
785
1867
  return new XaiChatLanguageModel(modelId, {
786
1868
  provider: "xai.chat",
787
1869
  baseURL,
@@ -790,6 +1872,15 @@ function createXai(options = {}) {
790
1872
  fetch: options.fetch
791
1873
  });
792
1874
  };
1875
+ const createResponsesLanguageModel = (modelId) => {
1876
+ return new XaiResponsesLanguageModel(modelId, {
1877
+ provider: "xai.responses",
1878
+ baseURL,
1879
+ headers: getHeaders,
1880
+ generateId,
1881
+ fetch: options.fetch
1882
+ });
1883
+ };
793
1884
  const createImageModel = (modelId) => {
794
1885
  return new OpenAICompatibleImageModel(modelId, {
795
1886
  provider: "xai.image",
@@ -799,20 +1890,29 @@ function createXai(options = {}) {
799
1890
  errorStructure: xaiErrorStructure
800
1891
  });
801
1892
  };
802
- const provider = (modelId) => createLanguageModel(modelId);
803
- provider.languageModel = createLanguageModel;
804
- provider.chat = createLanguageModel;
1893
+ const provider = (modelId) => createChatLanguageModel(modelId);
1894
+ provider.specificationVersion = "v2";
1895
+ provider.languageModel = createChatLanguageModel;
1896
+ provider.chat = createChatLanguageModel;
1897
+ provider.responses = createResponsesLanguageModel;
805
1898
  provider.textEmbeddingModel = (modelId) => {
806
1899
  throw new NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
807
1900
  };
808
1901
  provider.imageModel = createImageModel;
809
1902
  provider.image = createImageModel;
1903
+ provider.tools = xaiTools;
810
1904
  return provider;
811
1905
  }
812
1906
  var xai = createXai();
813
1907
  export {
814
1908
  VERSION,
1909
+ codeExecution,
815
1910
  createXai,
816
- xai
1911
+ viewImage,
1912
+ viewXVideo,
1913
+ webSearch,
1914
+ xSearch,
1915
+ xai,
1916
+ xaiTools
817
1917
  };
818
1918
  //# sourceMappingURL=index.mjs.map