@ai-sdk/xai 3.0.0-beta.33 → 3.0.0-beta.34

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