@ai-sdk/google 4.0.0-beta.18 → 4.0.0-beta.19

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;
@@ -396,6 +396,18 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
396
396
  };
397
397
  }
398
398
  case "tool-call": {
399
+ const serverToolCallId = (providerOpts == null ? void 0 : providerOpts.serverToolCallId) != null ? String(providerOpts.serverToolCallId) : void 0;
400
+ const serverToolType = (providerOpts == null ? void 0 : providerOpts.serverToolType) != null ? String(providerOpts.serverToolType) : void 0;
401
+ if (serverToolCallId && serverToolType) {
402
+ return {
403
+ toolCall: {
404
+ toolType: serverToolType,
405
+ args: typeof part.input === "string" ? JSON.parse(part.input) : part.input,
406
+ id: serverToolCallId
407
+ },
408
+ thoughtSignature
409
+ };
410
+ }
399
411
  return {
400
412
  functionCall: {
401
413
  name: part.toolName,
@@ -404,6 +416,21 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
404
416
  thoughtSignature
405
417
  };
406
418
  }
419
+ case "tool-result": {
420
+ const serverToolCallId = (providerOpts == null ? void 0 : providerOpts.serverToolCallId) != null ? String(providerOpts.serverToolCallId) : void 0;
421
+ const serverToolType = (providerOpts == null ? void 0 : providerOpts.serverToolType) != null ? String(providerOpts.serverToolType) : void 0;
422
+ if (serverToolCallId && serverToolType) {
423
+ return {
424
+ toolResponse: {
425
+ toolType: serverToolType,
426
+ response: part.output.type === "json" ? part.output.value : {},
427
+ id: serverToolCallId
428
+ },
429
+ thoughtSignature
430
+ };
431
+ }
432
+ return void 0;
433
+ }
407
434
  }
408
435
  }).filter((part) => part !== void 0)
409
436
  });
@@ -416,6 +443,26 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
416
443
  if (part.type === "tool-approval-response") {
417
444
  continue;
418
445
  }
446
+ 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;
447
+ const serverToolCallId = (partProviderOpts == null ? void 0 : partProviderOpts.serverToolCallId) != null ? String(partProviderOpts.serverToolCallId) : void 0;
448
+ const serverToolType = (partProviderOpts == null ? void 0 : partProviderOpts.serverToolType) != null ? String(partProviderOpts.serverToolType) : void 0;
449
+ if (serverToolCallId && serverToolType) {
450
+ const serverThoughtSignature = (partProviderOpts == null ? void 0 : partProviderOpts.thoughtSignature) != null ? String(partProviderOpts.thoughtSignature) : void 0;
451
+ if (contents.length > 0) {
452
+ const lastContent = contents[contents.length - 1];
453
+ if (lastContent.role === "model") {
454
+ lastContent.parts.push({
455
+ toolResponse: {
456
+ toolType: serverToolType,
457
+ response: part.output.type === "json" ? part.output.value : {},
458
+ id: serverToolCallId
459
+ },
460
+ thoughtSignature: serverThoughtSignature
461
+ });
462
+ continue;
463
+ }
464
+ }
465
+ }
419
466
  const output = part.output;
420
467
  if (output.type === "content") {
421
468
  if (supportsFunctionResponseParts) {
@@ -429,7 +476,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
429
476
  name: part.toolName,
430
477
  response: {
431
478
  name: part.toolName,
432
- content: output.type === "execution-denied" ? (_d = output.reason) != null ? _d : "Tool execution denied." : output.value
479
+ content: output.type === "execution-denied" ? (_h = output.reason) != null ? _h : "Tool execution denied." : output.value
433
480
  }
434
481
  }
435
482
  });
@@ -614,7 +661,7 @@ function prepareTools({
614
661
  toolChoice,
615
662
  modelId
616
663
  }) {
617
- var _a;
664
+ var _a, _b;
618
665
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
619
666
  const toolWarnings = [];
620
667
  const isLatest = [
@@ -623,13 +670,14 @@ function prepareTools({
623
670
  "gemini-pro-latest"
624
671
  ].some((id) => id === modelId);
625
672
  const isGemini2orNewer = modelId.includes("gemini-2") || modelId.includes("gemini-3") || modelId.includes("nano-banana") || isLatest;
673
+ const isGemini3orNewer = modelId.includes("gemini-3");
626
674
  const supportsFileSearch = modelId.includes("gemini-2.5") || modelId.includes("gemini-3");
627
675
  if (tools == null) {
628
676
  return { tools: void 0, toolConfig: void 0, toolWarnings };
629
677
  }
630
678
  const hasFunctionTools = tools.some((tool) => tool.type === "function");
631
679
  const hasProviderTools = tools.some((tool) => tool.type === "provider");
632
- if (hasFunctionTools && hasProviderTools) {
680
+ if (hasFunctionTools && hasProviderTools && !isGemini3orNewer) {
633
681
  toolWarnings.push({
634
682
  type: "unsupported",
635
683
  feature: `combination of function and provider-defined tools`
@@ -734,6 +782,45 @@ function prepareTools({
734
782
  break;
735
783
  }
736
784
  });
785
+ if (hasFunctionTools && isGemini3orNewer && googleTools2.length > 0) {
786
+ const functionDeclarations2 = [];
787
+ for (const tool of tools) {
788
+ if (tool.type === "function") {
789
+ functionDeclarations2.push({
790
+ name: tool.name,
791
+ description: (_a = tool.description) != null ? _a : "",
792
+ parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
793
+ });
794
+ }
795
+ }
796
+ const combinedToolConfig = {
797
+ functionCallingConfig: { mode: "VALIDATED" },
798
+ includeServerSideToolInvocations: true
799
+ };
800
+ if (toolChoice != null) {
801
+ switch (toolChoice.type) {
802
+ case "auto":
803
+ break;
804
+ case "none":
805
+ combinedToolConfig.functionCallingConfig = { mode: "NONE" };
806
+ break;
807
+ case "required":
808
+ combinedToolConfig.functionCallingConfig = { mode: "ANY" };
809
+ break;
810
+ case "tool":
811
+ combinedToolConfig.functionCallingConfig = {
812
+ mode: "ANY",
813
+ allowedFunctionNames: [toolChoice.toolName]
814
+ };
815
+ break;
816
+ }
817
+ }
818
+ return {
819
+ tools: [...googleTools2, { functionDeclarations: functionDeclarations2 }],
820
+ toolConfig: combinedToolConfig,
821
+ toolWarnings
822
+ };
823
+ }
737
824
  return {
738
825
  tools: googleTools2.length > 0 ? googleTools2 : void 0,
739
826
  toolConfig: void 0,
@@ -747,7 +834,7 @@ function prepareTools({
747
834
  case "function":
748
835
  functionDeclarations.push({
749
836
  name: tool.name,
750
- description: (_a = tool.description) != null ? _a : "",
837
+ description: (_b = tool.description) != null ? _b : "",
751
838
  parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
752
839
  });
753
840
  if (tool.strict === true) {
@@ -971,7 +1058,7 @@ var GoogleGenerativeAILanguageModel = class {
971
1058
  };
972
1059
  }
973
1060
  async doGenerate(options) {
974
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
1061
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
975
1062
  const { args, warnings, providerOptionsName } = await this.getArgs(options);
976
1063
  const mergedHeaders = (0, import_provider_utils4.combineHeaders)(
977
1064
  await (0, import_provider_utils4.resolve)(this.config.headers),
@@ -997,6 +1084,7 @@ var GoogleGenerativeAILanguageModel = class {
997
1084
  const parts = (_b = (_a = candidate.content) == null ? void 0 : _a.parts) != null ? _b : [];
998
1085
  const usageMetadata = response.usageMetadata;
999
1086
  let lastCodeExecutionToolCallId;
1087
+ let lastServerToolCallId;
1000
1088
  for (const part of parts) {
1001
1089
  if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
1002
1090
  const toolCallId = this.config.generateId();
@@ -1063,12 +1151,56 @@ var GoogleGenerativeAILanguageModel = class {
1063
1151
  }
1064
1152
  } : void 0
1065
1153
  });
1154
+ } else if ("toolCall" in part && part.toolCall) {
1155
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : this.config.generateId();
1156
+ lastServerToolCallId = toolCallId;
1157
+ content.push({
1158
+ type: "tool-call",
1159
+ toolCallId,
1160
+ toolName: `server:${part.toolCall.toolType}`,
1161
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
1162
+ providerExecuted: true,
1163
+ dynamic: true,
1164
+ providerMetadata: part.thoughtSignature ? {
1165
+ [providerOptionsName]: {
1166
+ thoughtSignature: part.thoughtSignature,
1167
+ serverToolCallId: toolCallId,
1168
+ serverToolType: part.toolCall.toolType
1169
+ }
1170
+ } : {
1171
+ [providerOptionsName]: {
1172
+ serverToolCallId: toolCallId,
1173
+ serverToolType: part.toolCall.toolType
1174
+ }
1175
+ }
1176
+ });
1177
+ } else if ("toolResponse" in part && part.toolResponse) {
1178
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : this.config.generateId();
1179
+ content.push({
1180
+ type: "tool-result",
1181
+ toolCallId: responseToolCallId,
1182
+ toolName: `server:${part.toolResponse.toolType}`,
1183
+ result: (_h = part.toolResponse.response) != null ? _h : {},
1184
+ providerMetadata: part.thoughtSignature ? {
1185
+ [providerOptionsName]: {
1186
+ thoughtSignature: part.thoughtSignature,
1187
+ serverToolCallId: responseToolCallId,
1188
+ serverToolType: part.toolResponse.toolType
1189
+ }
1190
+ } : {
1191
+ [providerOptionsName]: {
1192
+ serverToolCallId: responseToolCallId,
1193
+ serverToolType: part.toolResponse.toolType
1194
+ }
1195
+ }
1196
+ });
1197
+ lastServerToolCallId = void 0;
1066
1198
  }
1067
1199
  }
1068
- const sources = (_e = extractSources({
1200
+ const sources = (_i = extractSources({
1069
1201
  groundingMetadata: candidate.groundingMetadata,
1070
1202
  generateId: this.config.generateId
1071
- })) != null ? _e : [];
1203
+ })) != null ? _i : [];
1072
1204
  for (const source of sources) {
1073
1205
  content.push(source);
1074
1206
  }
@@ -1082,19 +1214,19 @@ var GoogleGenerativeAILanguageModel = class {
1082
1214
  (part) => part.type === "tool-call" && !part.providerExecuted
1083
1215
  )
1084
1216
  }),
1085
- raw: (_f = candidate.finishReason) != null ? _f : void 0
1217
+ raw: (_j = candidate.finishReason) != null ? _j : void 0
1086
1218
  },
1087
1219
  usage: convertGoogleGenerativeAIUsage(usageMetadata),
1088
1220
  warnings,
1089
1221
  providerMetadata: {
1090
1222
  [providerOptionsName]: {
1091
- promptFeedback: (_g = response.promptFeedback) != null ? _g : null,
1092
- groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
1093
- urlContextMetadata: (_i = candidate.urlContextMetadata) != null ? _i : null,
1094
- safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
1223
+ promptFeedback: (_k = response.promptFeedback) != null ? _k : null,
1224
+ groundingMetadata: (_l = candidate.groundingMetadata) != null ? _l : null,
1225
+ urlContextMetadata: (_m = candidate.urlContextMetadata) != null ? _m : null,
1226
+ safetyRatings: (_n = candidate.safetyRatings) != null ? _n : null,
1095
1227
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1096
- finishMessage: (_k = candidate.finishMessage) != null ? _k : null,
1097
- serviceTier: (_l = response.serviceTier) != null ? _l : null
1228
+ finishMessage: (_o = candidate.finishMessage) != null ? _o : null,
1229
+ serviceTier: (_p = response.serviceTier) != null ? _p : null
1098
1230
  }
1099
1231
  },
1100
1232
  request: { body: args },
@@ -1138,6 +1270,7 @@ var GoogleGenerativeAILanguageModel = class {
1138
1270
  let blockCounter = 0;
1139
1271
  const emittedSourceUrls = /* @__PURE__ */ new Set();
1140
1272
  let lastCodeExecutionToolCallId;
1273
+ let lastServerToolCallId;
1141
1274
  return {
1142
1275
  stream: response.pipeThrough(
1143
1276
  new TransformStream({
@@ -1145,7 +1278,7 @@ var GoogleGenerativeAILanguageModel = class {
1145
1278
  controller.enqueue({ type: "stream-start", warnings });
1146
1279
  },
1147
1280
  transform(chunk, controller) {
1148
- var _a, _b, _c, _d, _e, _f, _g;
1281
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
1149
1282
  if (options.includeRawChunks) {
1150
1283
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1151
1284
  }
@@ -1299,6 +1432,42 @@ var GoogleGenerativeAILanguageModel = class {
1299
1432
  data: part.inlineData.data,
1300
1433
  providerMetadata: fileMeta
1301
1434
  });
1435
+ } else if ("toolCall" in part && part.toolCall) {
1436
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId2();
1437
+ lastServerToolCallId = toolCallId;
1438
+ const serverMeta = {
1439
+ [providerOptionsName]: {
1440
+ ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
1441
+ serverToolCallId: toolCallId,
1442
+ serverToolType: part.toolCall.toolType
1443
+ }
1444
+ };
1445
+ controller.enqueue({
1446
+ type: "tool-call",
1447
+ toolCallId,
1448
+ toolName: `server:${part.toolCall.toolType}`,
1449
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
1450
+ providerExecuted: true,
1451
+ dynamic: true,
1452
+ providerMetadata: serverMeta
1453
+ });
1454
+ } else if ("toolResponse" in part && part.toolResponse) {
1455
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId2();
1456
+ const serverMeta = {
1457
+ [providerOptionsName]: {
1458
+ ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
1459
+ serverToolCallId: responseToolCallId,
1460
+ serverToolType: part.toolResponse.toolType
1461
+ }
1462
+ };
1463
+ controller.enqueue({
1464
+ type: "tool-result",
1465
+ toolCallId: responseToolCallId,
1466
+ toolName: `server:${part.toolResponse.toolType}`,
1467
+ result: (_h = part.toolResponse.response) != null ? _h : {},
1468
+ providerMetadata: serverMeta
1469
+ });
1470
+ lastServerToolCallId = void 0;
1302
1471
  }
1303
1472
  }
1304
1473
  const toolCallDeltas = getToolCallsFromParts({
@@ -1346,12 +1515,12 @@ var GoogleGenerativeAILanguageModel = class {
1346
1515
  };
1347
1516
  providerMetadata = {
1348
1517
  [providerOptionsName]: {
1349
- promptFeedback: (_e = value.promptFeedback) != null ? _e : null,
1518
+ promptFeedback: (_i = value.promptFeedback) != null ? _i : null,
1350
1519
  groundingMetadata: lastGroundingMetadata,
1351
1520
  urlContextMetadata: lastUrlContextMetadata,
1352
- safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null,
1521
+ safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
1353
1522
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1354
- finishMessage: (_g = candidate.finishMessage) != null ? _g : null,
1523
+ finishMessage: (_k = candidate.finishMessage) != null ? _k : null,
1355
1524
  serviceTier
1356
1525
  }
1357
1526
  };
@@ -1635,6 +1804,22 @@ var getContentSchema = () => import_v43.z.object({
1635
1804
  thought: import_v43.z.boolean().nullish(),
1636
1805
  thoughtSignature: import_v43.z.string().nullish()
1637
1806
  }),
1807
+ import_v43.z.object({
1808
+ toolCall: import_v43.z.object({
1809
+ toolType: import_v43.z.string(),
1810
+ args: import_v43.z.unknown().nullish(),
1811
+ id: import_v43.z.string()
1812
+ }),
1813
+ thoughtSignature: import_v43.z.string().nullish()
1814
+ }),
1815
+ import_v43.z.object({
1816
+ toolResponse: import_v43.z.object({
1817
+ toolType: import_v43.z.string(),
1818
+ response: import_v43.z.unknown().nullish(),
1819
+ id: import_v43.z.string()
1820
+ }),
1821
+ thoughtSignature: import_v43.z.string().nullish()
1822
+ }),
1638
1823
  import_v43.z.object({
1639
1824
  executableCode: import_v43.z.object({
1640
1825
  language: import_v43.z.string(),