@ai-sdk/google 3.0.54 → 3.0.55

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.
@@ -97,6 +97,20 @@ declare const responseSchema: _ai_sdk_provider_utils.LazySchema<{
97
97
  };
98
98
  thought?: boolean | null | undefined;
99
99
  thoughtSignature?: string | null | undefined;
100
+ } | {
101
+ toolCall: {
102
+ toolType: string;
103
+ id: string;
104
+ args?: unknown;
105
+ };
106
+ thoughtSignature?: string | null | undefined;
107
+ } | {
108
+ toolResponse: {
109
+ toolType: string;
110
+ id: string;
111
+ response?: unknown;
112
+ };
113
+ thoughtSignature?: string | null | undefined;
100
114
  } | {
101
115
  executableCode?: {
102
116
  language: string;
@@ -97,6 +97,20 @@ declare const responseSchema: _ai_sdk_provider_utils.LazySchema<{
97
97
  };
98
98
  thought?: boolean | null | undefined;
99
99
  thoughtSignature?: string | null | undefined;
100
+ } | {
101
+ toolCall: {
102
+ toolType: string;
103
+ id: string;
104
+ args?: unknown;
105
+ };
106
+ thoughtSignature?: string | null | undefined;
107
+ } | {
108
+ toolResponse: {
109
+ toolType: string;
110
+ id: string;
111
+ response?: unknown;
112
+ };
113
+ thoughtSignature?: string | null | undefined;
100
114
  } | {
101
115
  executableCode?: {
102
116
  language: string;
@@ -294,7 +294,7 @@ function appendLegacyToolResultParts(parts, toolName, outputValue) {
294
294
  }
295
295
  }
296
296
  function convertToGoogleGenerativeAIMessages(prompt, options) {
297
- var _a, _b, _c, _d;
297
+ var _a, _b, _c, _d, _e, _f, _g, _h;
298
298
  const systemInstructionParts = [];
299
299
  const contents = [];
300
300
  let systemMessagesAllowed = true;
@@ -381,6 +381,18 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
381
381
  };
382
382
  }
383
383
  case "tool-call": {
384
+ const serverToolCallId = (providerOpts == null ? void 0 : providerOpts.serverToolCallId) != null ? String(providerOpts.serverToolCallId) : void 0;
385
+ const serverToolType = (providerOpts == null ? void 0 : providerOpts.serverToolType) != null ? String(providerOpts.serverToolType) : void 0;
386
+ if (serverToolCallId && serverToolType) {
387
+ return {
388
+ toolCall: {
389
+ toolType: serverToolType,
390
+ args: typeof part.input === "string" ? JSON.parse(part.input) : part.input,
391
+ id: serverToolCallId
392
+ },
393
+ thoughtSignature
394
+ };
395
+ }
384
396
  return {
385
397
  functionCall: {
386
398
  name: part.toolName,
@@ -389,6 +401,21 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
389
401
  thoughtSignature
390
402
  };
391
403
  }
404
+ case "tool-result": {
405
+ const serverToolCallId = (providerOpts == null ? void 0 : providerOpts.serverToolCallId) != null ? String(providerOpts.serverToolCallId) : void 0;
406
+ const serverToolType = (providerOpts == null ? void 0 : providerOpts.serverToolType) != null ? String(providerOpts.serverToolType) : void 0;
407
+ if (serverToolCallId && serverToolType) {
408
+ return {
409
+ toolResponse: {
410
+ toolType: serverToolType,
411
+ response: part.output.type === "json" ? part.output.value : {},
412
+ id: serverToolCallId
413
+ },
414
+ thoughtSignature
415
+ };
416
+ }
417
+ return void 0;
418
+ }
392
419
  }
393
420
  }).filter((part) => part !== void 0)
394
421
  });
@@ -401,6 +428,26 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
401
428
  if (part.type === "tool-approval-response") {
402
429
  continue;
403
430
  }
431
+ 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;
432
+ const serverToolCallId = (partProviderOpts == null ? void 0 : partProviderOpts.serverToolCallId) != null ? String(partProviderOpts.serverToolCallId) : void 0;
433
+ const serverToolType = (partProviderOpts == null ? void 0 : partProviderOpts.serverToolType) != null ? String(partProviderOpts.serverToolType) : void 0;
434
+ if (serverToolCallId && serverToolType) {
435
+ const serverThoughtSignature = (partProviderOpts == null ? void 0 : partProviderOpts.thoughtSignature) != null ? String(partProviderOpts.thoughtSignature) : void 0;
436
+ if (contents.length > 0) {
437
+ const lastContent = contents[contents.length - 1];
438
+ if (lastContent.role === "model") {
439
+ lastContent.parts.push({
440
+ toolResponse: {
441
+ toolType: serverToolType,
442
+ response: part.output.type === "json" ? part.output.value : {},
443
+ id: serverToolCallId
444
+ },
445
+ thoughtSignature: serverThoughtSignature
446
+ });
447
+ continue;
448
+ }
449
+ }
450
+ }
404
451
  const output = part.output;
405
452
  if (output.type === "content") {
406
453
  if (supportsFunctionResponseParts) {
@@ -414,7 +461,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
414
461
  name: part.toolName,
415
462
  response: {
416
463
  name: part.toolName,
417
- content: output.type === "execution-denied" ? (_d = output.reason) != null ? _d : "Tool execution denied." : output.value
464
+ content: output.type === "execution-denied" ? (_h = output.reason) != null ? _h : "Tool execution denied." : output.value
418
465
  }
419
466
  }
420
467
  });
@@ -599,7 +646,7 @@ function prepareTools({
599
646
  toolChoice,
600
647
  modelId
601
648
  }) {
602
- var _a;
649
+ var _a, _b;
603
650
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
604
651
  const toolWarnings = [];
605
652
  const isLatest = [
@@ -608,13 +655,14 @@ function prepareTools({
608
655
  "gemini-pro-latest"
609
656
  ].some((id) => id === modelId);
610
657
  const isGemini2orNewer = modelId.includes("gemini-2") || modelId.includes("gemini-3") || modelId.includes("nano-banana") || isLatest;
658
+ const isGemini3orNewer = modelId.includes("gemini-3");
611
659
  const supportsFileSearch = modelId.includes("gemini-2.5") || modelId.includes("gemini-3");
612
660
  if (tools == null) {
613
661
  return { tools: void 0, toolConfig: void 0, toolWarnings };
614
662
  }
615
663
  const hasFunctionTools = tools.some((tool) => tool.type === "function");
616
664
  const hasProviderTools = tools.some((tool) => tool.type === "provider");
617
- if (hasFunctionTools && hasProviderTools) {
665
+ if (hasFunctionTools && hasProviderTools && !isGemini3orNewer) {
618
666
  toolWarnings.push({
619
667
  type: "unsupported",
620
668
  feature: `combination of function and provider-defined tools`
@@ -719,6 +767,45 @@ function prepareTools({
719
767
  break;
720
768
  }
721
769
  });
770
+ if (hasFunctionTools && isGemini3orNewer && googleTools2.length > 0) {
771
+ const functionDeclarations2 = [];
772
+ for (const tool of tools) {
773
+ if (tool.type === "function") {
774
+ functionDeclarations2.push({
775
+ name: tool.name,
776
+ description: (_a = tool.description) != null ? _a : "",
777
+ parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
778
+ });
779
+ }
780
+ }
781
+ const combinedToolConfig = {
782
+ functionCallingConfig: { mode: "VALIDATED" },
783
+ includeServerSideToolInvocations: true
784
+ };
785
+ if (toolChoice != null) {
786
+ switch (toolChoice.type) {
787
+ case "auto":
788
+ break;
789
+ case "none":
790
+ combinedToolConfig.functionCallingConfig = { mode: "NONE" };
791
+ break;
792
+ case "required":
793
+ combinedToolConfig.functionCallingConfig = { mode: "ANY" };
794
+ break;
795
+ case "tool":
796
+ combinedToolConfig.functionCallingConfig = {
797
+ mode: "ANY",
798
+ allowedFunctionNames: [toolChoice.toolName]
799
+ };
800
+ break;
801
+ }
802
+ }
803
+ return {
804
+ tools: [...googleTools2, { functionDeclarations: functionDeclarations2 }],
805
+ toolConfig: combinedToolConfig,
806
+ toolWarnings
807
+ };
808
+ }
722
809
  return {
723
810
  tools: googleTools2.length > 0 ? googleTools2 : void 0,
724
811
  toolConfig: void 0,
@@ -732,7 +819,7 @@ function prepareTools({
732
819
  case "function":
733
820
  functionDeclarations.push({
734
821
  name: tool.name,
735
- description: (_a = tool.description) != null ? _a : "",
822
+ description: (_b = tool.description) != null ? _b : "",
736
823
  parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
737
824
  });
738
825
  if (tool.strict === true) {
@@ -949,7 +1036,7 @@ var GoogleGenerativeAILanguageModel = class {
949
1036
  };
950
1037
  }
951
1038
  async doGenerate(options) {
952
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
1039
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
953
1040
  const { args, warnings, providerOptionsName } = await this.getArgs(options);
954
1041
  const mergedHeaders = (0, import_provider_utils4.combineHeaders)(
955
1042
  await (0, import_provider_utils4.resolve)(this.config.headers),
@@ -975,6 +1062,7 @@ var GoogleGenerativeAILanguageModel = class {
975
1062
  const parts = (_b = (_a = candidate.content) == null ? void 0 : _a.parts) != null ? _b : [];
976
1063
  const usageMetadata = response.usageMetadata;
977
1064
  let lastCodeExecutionToolCallId;
1065
+ let lastServerToolCallId;
978
1066
  for (const part of parts) {
979
1067
  if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
980
1068
  const toolCallId = this.config.generateId();
@@ -1042,12 +1130,56 @@ var GoogleGenerativeAILanguageModel = class {
1042
1130
  }
1043
1131
  } : void 0
1044
1132
  });
1133
+ } else if ("toolCall" in part && part.toolCall) {
1134
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : this.config.generateId();
1135
+ lastServerToolCallId = toolCallId;
1136
+ content.push({
1137
+ type: "tool-call",
1138
+ toolCallId,
1139
+ toolName: `server:${part.toolCall.toolType}`,
1140
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
1141
+ providerExecuted: true,
1142
+ dynamic: true,
1143
+ providerMetadata: part.thoughtSignature ? {
1144
+ [providerOptionsName]: {
1145
+ thoughtSignature: part.thoughtSignature,
1146
+ serverToolCallId: toolCallId,
1147
+ serverToolType: part.toolCall.toolType
1148
+ }
1149
+ } : {
1150
+ [providerOptionsName]: {
1151
+ serverToolCallId: toolCallId,
1152
+ serverToolType: part.toolCall.toolType
1153
+ }
1154
+ }
1155
+ });
1156
+ } else if ("toolResponse" in part && part.toolResponse) {
1157
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : this.config.generateId();
1158
+ content.push({
1159
+ type: "tool-result",
1160
+ toolCallId: responseToolCallId,
1161
+ toolName: `server:${part.toolResponse.toolType}`,
1162
+ result: (_h = part.toolResponse.response) != null ? _h : {},
1163
+ providerMetadata: part.thoughtSignature ? {
1164
+ [providerOptionsName]: {
1165
+ thoughtSignature: part.thoughtSignature,
1166
+ serverToolCallId: responseToolCallId,
1167
+ serverToolType: part.toolResponse.toolType
1168
+ }
1169
+ } : {
1170
+ [providerOptionsName]: {
1171
+ serverToolCallId: responseToolCallId,
1172
+ serverToolType: part.toolResponse.toolType
1173
+ }
1174
+ }
1175
+ });
1176
+ lastServerToolCallId = void 0;
1045
1177
  }
1046
1178
  }
1047
- const sources = (_e = extractSources({
1179
+ const sources = (_i = extractSources({
1048
1180
  groundingMetadata: candidate.groundingMetadata,
1049
1181
  generateId: this.config.generateId
1050
- })) != null ? _e : [];
1182
+ })) != null ? _i : [];
1051
1183
  for (const source of sources) {
1052
1184
  content.push(source);
1053
1185
  }
@@ -1061,19 +1193,19 @@ var GoogleGenerativeAILanguageModel = class {
1061
1193
  (part) => part.type === "tool-call" && !part.providerExecuted
1062
1194
  )
1063
1195
  }),
1064
- raw: (_f = candidate.finishReason) != null ? _f : void 0
1196
+ raw: (_j = candidate.finishReason) != null ? _j : void 0
1065
1197
  },
1066
1198
  usage: convertGoogleGenerativeAIUsage(usageMetadata),
1067
1199
  warnings,
1068
1200
  providerMetadata: {
1069
1201
  [providerOptionsName]: {
1070
- promptFeedback: (_g = response.promptFeedback) != null ? _g : null,
1071
- groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
1072
- urlContextMetadata: (_i = candidate.urlContextMetadata) != null ? _i : null,
1073
- safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
1202
+ promptFeedback: (_k = response.promptFeedback) != null ? _k : null,
1203
+ groundingMetadata: (_l = candidate.groundingMetadata) != null ? _l : null,
1204
+ urlContextMetadata: (_m = candidate.urlContextMetadata) != null ? _m : null,
1205
+ safetyRatings: (_n = candidate.safetyRatings) != null ? _n : null,
1074
1206
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1075
- finishMessage: (_k = candidate.finishMessage) != null ? _k : null,
1076
- serviceTier: (_l = response.serviceTier) != null ? _l : null
1207
+ finishMessage: (_o = candidate.finishMessage) != null ? _o : null,
1208
+ serviceTier: (_p = response.serviceTier) != null ? _p : null
1077
1209
  }
1078
1210
  },
1079
1211
  request: { body: args },
@@ -1117,6 +1249,7 @@ var GoogleGenerativeAILanguageModel = class {
1117
1249
  let blockCounter = 0;
1118
1250
  const emittedSourceUrls = /* @__PURE__ */ new Set();
1119
1251
  let lastCodeExecutionToolCallId;
1252
+ let lastServerToolCallId;
1120
1253
  return {
1121
1254
  stream: response.pipeThrough(
1122
1255
  new TransformStream({
@@ -1124,7 +1257,7 @@ var GoogleGenerativeAILanguageModel = class {
1124
1257
  controller.enqueue({ type: "stream-start", warnings });
1125
1258
  },
1126
1259
  transform(chunk, controller) {
1127
- var _a, _b, _c, _d, _e, _f, _g;
1260
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
1128
1261
  if (options.includeRawChunks) {
1129
1262
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1130
1263
  }
@@ -1279,6 +1412,42 @@ var GoogleGenerativeAILanguageModel = class {
1279
1412
  data: part.inlineData.data,
1280
1413
  providerMetadata: fileMeta
1281
1414
  });
1415
+ } else if ("toolCall" in part && part.toolCall) {
1416
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId2();
1417
+ lastServerToolCallId = toolCallId;
1418
+ const serverMeta = {
1419
+ [providerOptionsName]: {
1420
+ ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
1421
+ serverToolCallId: toolCallId,
1422
+ serverToolType: part.toolCall.toolType
1423
+ }
1424
+ };
1425
+ controller.enqueue({
1426
+ type: "tool-call",
1427
+ toolCallId,
1428
+ toolName: `server:${part.toolCall.toolType}`,
1429
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
1430
+ providerExecuted: true,
1431
+ dynamic: true,
1432
+ providerMetadata: serverMeta
1433
+ });
1434
+ } else if ("toolResponse" in part && part.toolResponse) {
1435
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId2();
1436
+ const serverMeta = {
1437
+ [providerOptionsName]: {
1438
+ ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
1439
+ serverToolCallId: responseToolCallId,
1440
+ serverToolType: part.toolResponse.toolType
1441
+ }
1442
+ };
1443
+ controller.enqueue({
1444
+ type: "tool-result",
1445
+ toolCallId: responseToolCallId,
1446
+ toolName: `server:${part.toolResponse.toolType}`,
1447
+ result: (_h = part.toolResponse.response) != null ? _h : {},
1448
+ providerMetadata: serverMeta
1449
+ });
1450
+ lastServerToolCallId = void 0;
1282
1451
  }
1283
1452
  }
1284
1453
  const toolCallDeltas = getToolCallsFromParts({
@@ -1326,12 +1495,12 @@ var GoogleGenerativeAILanguageModel = class {
1326
1495
  };
1327
1496
  providerMetadata = {
1328
1497
  [providerOptionsName]: {
1329
- promptFeedback: (_e = value.promptFeedback) != null ? _e : null,
1498
+ promptFeedback: (_i = value.promptFeedback) != null ? _i : null,
1330
1499
  groundingMetadata: lastGroundingMetadata,
1331
1500
  urlContextMetadata: lastUrlContextMetadata,
1332
- safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null,
1501
+ safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
1333
1502
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1334
- finishMessage: (_g = candidate.finishMessage) != null ? _g : null,
1503
+ finishMessage: (_k = candidate.finishMessage) != null ? _k : null,
1335
1504
  serviceTier
1336
1505
  }
1337
1506
  };
@@ -1546,6 +1715,22 @@ var getContentSchema = () => import_v43.z.object({
1546
1715
  thought: import_v43.z.boolean().nullish(),
1547
1716
  thoughtSignature: import_v43.z.string().nullish()
1548
1717
  }),
1718
+ import_v43.z.object({
1719
+ toolCall: import_v43.z.object({
1720
+ toolType: import_v43.z.string(),
1721
+ args: import_v43.z.unknown().nullish(),
1722
+ id: import_v43.z.string()
1723
+ }),
1724
+ thoughtSignature: import_v43.z.string().nullish()
1725
+ }),
1726
+ import_v43.z.object({
1727
+ toolResponse: import_v43.z.object({
1728
+ toolType: import_v43.z.string(),
1729
+ response: import_v43.z.unknown().nullish(),
1730
+ id: import_v43.z.string()
1731
+ }),
1732
+ thoughtSignature: import_v43.z.string().nullish()
1733
+ }),
1549
1734
  import_v43.z.object({
1550
1735
  executableCode: import_v43.z.object({
1551
1736
  language: import_v43.z.string(),