@ai-sdk/google 3.0.54 → 3.0.56

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.
@@ -277,7 +277,7 @@ function appendLegacyToolResultParts(parts, toolName, outputValue) {
277
277
  }
278
278
  }
279
279
  function convertToGoogleGenerativeAIMessages(prompt, options) {
280
- var _a, _b, _c, _d;
280
+ var _a, _b, _c, _d, _e, _f, _g, _h;
281
281
  const systemInstructionParts = [];
282
282
  const contents = [];
283
283
  let systemMessagesAllowed = true;
@@ -364,6 +364,18 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
364
364
  };
365
365
  }
366
366
  case "tool-call": {
367
+ const serverToolCallId = (providerOpts == null ? void 0 : providerOpts.serverToolCallId) != null ? String(providerOpts.serverToolCallId) : void 0;
368
+ const serverToolType = (providerOpts == null ? void 0 : providerOpts.serverToolType) != null ? String(providerOpts.serverToolType) : void 0;
369
+ if (serverToolCallId && serverToolType) {
370
+ return {
371
+ toolCall: {
372
+ toolType: serverToolType,
373
+ args: typeof part.input === "string" ? JSON.parse(part.input) : part.input,
374
+ id: serverToolCallId
375
+ },
376
+ thoughtSignature
377
+ };
378
+ }
367
379
  return {
368
380
  functionCall: {
369
381
  name: part.toolName,
@@ -372,6 +384,21 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
372
384
  thoughtSignature
373
385
  };
374
386
  }
387
+ case "tool-result": {
388
+ const serverToolCallId = (providerOpts == null ? void 0 : providerOpts.serverToolCallId) != null ? String(providerOpts.serverToolCallId) : void 0;
389
+ const serverToolType = (providerOpts == null ? void 0 : providerOpts.serverToolType) != null ? String(providerOpts.serverToolType) : void 0;
390
+ if (serverToolCallId && serverToolType) {
391
+ return {
392
+ toolResponse: {
393
+ toolType: serverToolType,
394
+ response: part.output.type === "json" ? part.output.value : {},
395
+ id: serverToolCallId
396
+ },
397
+ thoughtSignature
398
+ };
399
+ }
400
+ return void 0;
401
+ }
375
402
  }
376
403
  }).filter((part) => part !== void 0)
377
404
  });
@@ -384,6 +411,26 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
384
411
  if (part.type === "tool-approval-response") {
385
412
  continue;
386
413
  }
414
+ const partProviderOpts = (_g = (_d = part.providerOptions) == null ? void 0 : _d[providerOptionsName]) != null ? _g : providerOptionsName !== "google" ? (_e = part.providerOptions) == null ? void 0 : _e.google : (_f = part.providerOptions) == null ? void 0 : _f.vertex;
415
+ const serverToolCallId = (partProviderOpts == null ? void 0 : partProviderOpts.serverToolCallId) != null ? String(partProviderOpts.serverToolCallId) : void 0;
416
+ const serverToolType = (partProviderOpts == null ? void 0 : partProviderOpts.serverToolType) != null ? String(partProviderOpts.serverToolType) : void 0;
417
+ if (serverToolCallId && serverToolType) {
418
+ const serverThoughtSignature = (partProviderOpts == null ? void 0 : partProviderOpts.thoughtSignature) != null ? String(partProviderOpts.thoughtSignature) : void 0;
419
+ if (contents.length > 0) {
420
+ const lastContent = contents[contents.length - 1];
421
+ if (lastContent.role === "model") {
422
+ lastContent.parts.push({
423
+ toolResponse: {
424
+ toolType: serverToolType,
425
+ response: part.output.type === "json" ? part.output.value : {},
426
+ id: serverToolCallId
427
+ },
428
+ thoughtSignature: serverThoughtSignature
429
+ });
430
+ continue;
431
+ }
432
+ }
433
+ }
387
434
  const output = part.output;
388
435
  if (output.type === "content") {
389
436
  if (supportsFunctionResponseParts) {
@@ -397,7 +444,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
397
444
  name: part.toolName,
398
445
  response: {
399
446
  name: part.toolName,
400
- content: output.type === "execution-denied" ? (_d = output.reason) != null ? _d : "Tool execution denied." : output.value
447
+ content: output.type === "execution-denied" ? (_h = output.reason) != null ? _h : "Tool execution denied." : output.value
401
448
  }
402
449
  }
403
450
  });
@@ -588,7 +635,7 @@ function prepareTools({
588
635
  toolChoice,
589
636
  modelId
590
637
  }) {
591
- var _a;
638
+ var _a, _b;
592
639
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
593
640
  const toolWarnings = [];
594
641
  const isLatest = [
@@ -597,13 +644,14 @@ function prepareTools({
597
644
  "gemini-pro-latest"
598
645
  ].some((id) => id === modelId);
599
646
  const isGemini2orNewer = modelId.includes("gemini-2") || modelId.includes("gemini-3") || modelId.includes("nano-banana") || isLatest;
647
+ const isGemini3orNewer = modelId.includes("gemini-3");
600
648
  const supportsFileSearch = modelId.includes("gemini-2.5") || modelId.includes("gemini-3");
601
649
  if (tools == null) {
602
650
  return { tools: void 0, toolConfig: void 0, toolWarnings };
603
651
  }
604
652
  const hasFunctionTools = tools.some((tool) => tool.type === "function");
605
653
  const hasProviderTools = tools.some((tool) => tool.type === "provider");
606
- if (hasFunctionTools && hasProviderTools) {
654
+ if (hasFunctionTools && hasProviderTools && !isGemini3orNewer) {
607
655
  toolWarnings.push({
608
656
  type: "unsupported",
609
657
  feature: `combination of function and provider-defined tools`
@@ -708,6 +756,45 @@ function prepareTools({
708
756
  break;
709
757
  }
710
758
  });
759
+ if (hasFunctionTools && isGemini3orNewer && googleTools2.length > 0) {
760
+ const functionDeclarations2 = [];
761
+ for (const tool of tools) {
762
+ if (tool.type === "function") {
763
+ functionDeclarations2.push({
764
+ name: tool.name,
765
+ description: (_a = tool.description) != null ? _a : "",
766
+ parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
767
+ });
768
+ }
769
+ }
770
+ const combinedToolConfig = {
771
+ functionCallingConfig: { mode: "VALIDATED" },
772
+ includeServerSideToolInvocations: true
773
+ };
774
+ if (toolChoice != null) {
775
+ switch (toolChoice.type) {
776
+ case "auto":
777
+ break;
778
+ case "none":
779
+ combinedToolConfig.functionCallingConfig = { mode: "NONE" };
780
+ break;
781
+ case "required":
782
+ combinedToolConfig.functionCallingConfig = { mode: "ANY" };
783
+ break;
784
+ case "tool":
785
+ combinedToolConfig.functionCallingConfig = {
786
+ mode: "ANY",
787
+ allowedFunctionNames: [toolChoice.toolName]
788
+ };
789
+ break;
790
+ }
791
+ }
792
+ return {
793
+ tools: [...googleTools2, { functionDeclarations: functionDeclarations2 }],
794
+ toolConfig: combinedToolConfig,
795
+ toolWarnings
796
+ };
797
+ }
711
798
  return {
712
799
  tools: googleTools2.length > 0 ? googleTools2 : void 0,
713
800
  toolConfig: void 0,
@@ -721,7 +808,7 @@ function prepareTools({
721
808
  case "function":
722
809
  functionDeclarations.push({
723
810
  name: tool.name,
724
- description: (_a = tool.description) != null ? _a : "",
811
+ description: (_b = tool.description) != null ? _b : "",
725
812
  parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
726
813
  });
727
814
  if (tool.strict === true) {
@@ -938,7 +1025,7 @@ var GoogleGenerativeAILanguageModel = class {
938
1025
  };
939
1026
  }
940
1027
  async doGenerate(options) {
941
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
1028
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
942
1029
  const { args, warnings, providerOptionsName } = await this.getArgs(options);
943
1030
  const mergedHeaders = combineHeaders(
944
1031
  await resolve(this.config.headers),
@@ -964,6 +1051,7 @@ var GoogleGenerativeAILanguageModel = class {
964
1051
  const parts = (_b = (_a = candidate.content) == null ? void 0 : _a.parts) != null ? _b : [];
965
1052
  const usageMetadata = response.usageMetadata;
966
1053
  let lastCodeExecutionToolCallId;
1054
+ let lastServerToolCallId;
967
1055
  for (const part of parts) {
968
1056
  if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
969
1057
  const toolCallId = this.config.generateId();
@@ -1031,12 +1119,56 @@ var GoogleGenerativeAILanguageModel = class {
1031
1119
  }
1032
1120
  } : void 0
1033
1121
  });
1122
+ } else if ("toolCall" in part && part.toolCall) {
1123
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : this.config.generateId();
1124
+ lastServerToolCallId = toolCallId;
1125
+ content.push({
1126
+ type: "tool-call",
1127
+ toolCallId,
1128
+ toolName: `server:${part.toolCall.toolType}`,
1129
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
1130
+ providerExecuted: true,
1131
+ dynamic: true,
1132
+ providerMetadata: part.thoughtSignature ? {
1133
+ [providerOptionsName]: {
1134
+ thoughtSignature: part.thoughtSignature,
1135
+ serverToolCallId: toolCallId,
1136
+ serverToolType: part.toolCall.toolType
1137
+ }
1138
+ } : {
1139
+ [providerOptionsName]: {
1140
+ serverToolCallId: toolCallId,
1141
+ serverToolType: part.toolCall.toolType
1142
+ }
1143
+ }
1144
+ });
1145
+ } else if ("toolResponse" in part && part.toolResponse) {
1146
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : this.config.generateId();
1147
+ content.push({
1148
+ type: "tool-result",
1149
+ toolCallId: responseToolCallId,
1150
+ toolName: `server:${part.toolResponse.toolType}`,
1151
+ result: (_h = part.toolResponse.response) != null ? _h : {},
1152
+ providerMetadata: part.thoughtSignature ? {
1153
+ [providerOptionsName]: {
1154
+ thoughtSignature: part.thoughtSignature,
1155
+ serverToolCallId: responseToolCallId,
1156
+ serverToolType: part.toolResponse.toolType
1157
+ }
1158
+ } : {
1159
+ [providerOptionsName]: {
1160
+ serverToolCallId: responseToolCallId,
1161
+ serverToolType: part.toolResponse.toolType
1162
+ }
1163
+ }
1164
+ });
1165
+ lastServerToolCallId = void 0;
1034
1166
  }
1035
1167
  }
1036
- const sources = (_e = extractSources({
1168
+ const sources = (_i = extractSources({
1037
1169
  groundingMetadata: candidate.groundingMetadata,
1038
1170
  generateId: this.config.generateId
1039
- })) != null ? _e : [];
1171
+ })) != null ? _i : [];
1040
1172
  for (const source of sources) {
1041
1173
  content.push(source);
1042
1174
  }
@@ -1050,19 +1182,19 @@ var GoogleGenerativeAILanguageModel = class {
1050
1182
  (part) => part.type === "tool-call" && !part.providerExecuted
1051
1183
  )
1052
1184
  }),
1053
- raw: (_f = candidate.finishReason) != null ? _f : void 0
1185
+ raw: (_j = candidate.finishReason) != null ? _j : void 0
1054
1186
  },
1055
1187
  usage: convertGoogleGenerativeAIUsage(usageMetadata),
1056
1188
  warnings,
1057
1189
  providerMetadata: {
1058
1190
  [providerOptionsName]: {
1059
- promptFeedback: (_g = response.promptFeedback) != null ? _g : null,
1060
- groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
1061
- urlContextMetadata: (_i = candidate.urlContextMetadata) != null ? _i : null,
1062
- safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
1191
+ promptFeedback: (_k = response.promptFeedback) != null ? _k : null,
1192
+ groundingMetadata: (_l = candidate.groundingMetadata) != null ? _l : null,
1193
+ urlContextMetadata: (_m = candidate.urlContextMetadata) != null ? _m : null,
1194
+ safetyRatings: (_n = candidate.safetyRatings) != null ? _n : null,
1063
1195
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1064
- finishMessage: (_k = candidate.finishMessage) != null ? _k : null,
1065
- serviceTier: (_l = response.serviceTier) != null ? _l : null
1196
+ finishMessage: (_o = candidate.finishMessage) != null ? _o : null,
1197
+ serviceTier: (_p = response.serviceTier) != null ? _p : null
1066
1198
  }
1067
1199
  },
1068
1200
  request: { body: args },
@@ -1106,6 +1238,7 @@ var GoogleGenerativeAILanguageModel = class {
1106
1238
  let blockCounter = 0;
1107
1239
  const emittedSourceUrls = /* @__PURE__ */ new Set();
1108
1240
  let lastCodeExecutionToolCallId;
1241
+ let lastServerToolCallId;
1109
1242
  return {
1110
1243
  stream: response.pipeThrough(
1111
1244
  new TransformStream({
@@ -1113,7 +1246,7 @@ var GoogleGenerativeAILanguageModel = class {
1113
1246
  controller.enqueue({ type: "stream-start", warnings });
1114
1247
  },
1115
1248
  transform(chunk, controller) {
1116
- var _a, _b, _c, _d, _e, _f, _g;
1249
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
1117
1250
  if (options.includeRawChunks) {
1118
1251
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1119
1252
  }
@@ -1268,6 +1401,42 @@ var GoogleGenerativeAILanguageModel = class {
1268
1401
  data: part.inlineData.data,
1269
1402
  providerMetadata: fileMeta
1270
1403
  });
1404
+ } else if ("toolCall" in part && part.toolCall) {
1405
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId2();
1406
+ lastServerToolCallId = toolCallId;
1407
+ const serverMeta = {
1408
+ [providerOptionsName]: {
1409
+ ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
1410
+ serverToolCallId: toolCallId,
1411
+ serverToolType: part.toolCall.toolType
1412
+ }
1413
+ };
1414
+ controller.enqueue({
1415
+ type: "tool-call",
1416
+ toolCallId,
1417
+ toolName: `server:${part.toolCall.toolType}`,
1418
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
1419
+ providerExecuted: true,
1420
+ dynamic: true,
1421
+ providerMetadata: serverMeta
1422
+ });
1423
+ } else if ("toolResponse" in part && part.toolResponse) {
1424
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId2();
1425
+ const serverMeta = {
1426
+ [providerOptionsName]: {
1427
+ ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
1428
+ serverToolCallId: responseToolCallId,
1429
+ serverToolType: part.toolResponse.toolType
1430
+ }
1431
+ };
1432
+ controller.enqueue({
1433
+ type: "tool-result",
1434
+ toolCallId: responseToolCallId,
1435
+ toolName: `server:${part.toolResponse.toolType}`,
1436
+ result: (_h = part.toolResponse.response) != null ? _h : {},
1437
+ providerMetadata: serverMeta
1438
+ });
1439
+ lastServerToolCallId = void 0;
1271
1440
  }
1272
1441
  }
1273
1442
  const toolCallDeltas = getToolCallsFromParts({
@@ -1315,12 +1484,12 @@ var GoogleGenerativeAILanguageModel = class {
1315
1484
  };
1316
1485
  providerMetadata = {
1317
1486
  [providerOptionsName]: {
1318
- promptFeedback: (_e = value.promptFeedback) != null ? _e : null,
1487
+ promptFeedback: (_i = value.promptFeedback) != null ? _i : null,
1319
1488
  groundingMetadata: lastGroundingMetadata,
1320
1489
  urlContextMetadata: lastUrlContextMetadata,
1321
- safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null,
1490
+ safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
1322
1491
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1323
- finishMessage: (_g = candidate.finishMessage) != null ? _g : null,
1492
+ finishMessage: (_k = candidate.finishMessage) != null ? _k : null,
1324
1493
  serviceTier
1325
1494
  }
1326
1495
  };
@@ -1535,6 +1704,22 @@ var getContentSchema = () => z3.object({
1535
1704
  thought: z3.boolean().nullish(),
1536
1705
  thoughtSignature: z3.string().nullish()
1537
1706
  }),
1707
+ z3.object({
1708
+ toolCall: z3.object({
1709
+ toolType: z3.string(),
1710
+ args: z3.unknown().nullish(),
1711
+ id: z3.string()
1712
+ }),
1713
+ thoughtSignature: z3.string().nullish()
1714
+ }),
1715
+ z3.object({
1716
+ toolResponse: z3.object({
1717
+ toolType: z3.string(),
1718
+ response: z3.unknown().nullish(),
1719
+ id: z3.string()
1720
+ }),
1721
+ thoughtSignature: z3.string().nullish()
1722
+ }),
1538
1723
  z3.object({
1539
1724
  executableCode: z3.object({
1540
1725
  language: z3.string(),