@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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @ai-sdk/google
2
2
 
3
+ ## 4.0.0-beta.19
4
+
5
+ ### Patch Changes
6
+
7
+ - 01fa606: feat(provider/google): support combining built-in tools with function calling on Gemini 3
8
+
3
9
  ## 4.0.0-beta.18
4
10
 
5
11
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -59,6 +59,20 @@ declare const responseSchema: _ai_sdk_provider_utils.LazySchema<{
59
59
  };
60
60
  thought?: boolean | null | undefined;
61
61
  thoughtSignature?: string | null | undefined;
62
+ } | {
63
+ toolCall: {
64
+ toolType: string;
65
+ id: string;
66
+ args?: unknown;
67
+ };
68
+ thoughtSignature?: string | null | undefined;
69
+ } | {
70
+ toolResponse: {
71
+ toolType: string;
72
+ id: string;
73
+ response?: unknown;
74
+ };
75
+ thoughtSignature?: string | null | undefined;
62
76
  } | {
63
77
  executableCode?: {
64
78
  language: string;
package/dist/index.d.ts CHANGED
@@ -59,6 +59,20 @@ declare const responseSchema: _ai_sdk_provider_utils.LazySchema<{
59
59
  };
60
60
  thought?: boolean | null | undefined;
61
61
  thoughtSignature?: string | null | undefined;
62
+ } | {
63
+ toolCall: {
64
+ toolType: string;
65
+ id: string;
66
+ args?: unknown;
67
+ };
68
+ thoughtSignature?: string | null | undefined;
69
+ } | {
70
+ toolResponse: {
71
+ toolType: string;
72
+ id: string;
73
+ response?: unknown;
74
+ };
75
+ thoughtSignature?: string | null | undefined;
62
76
  } | {
63
77
  executableCode?: {
64
78
  language: string;
package/dist/index.js CHANGED
@@ -30,7 +30,7 @@ module.exports = __toCommonJS(index_exports);
30
30
  var import_provider_utils16 = require("@ai-sdk/provider-utils");
31
31
 
32
32
  // src/version.ts
33
- var VERSION = true ? "4.0.0-beta.18" : "0.0.0-test";
33
+ var VERSION = true ? "4.0.0-beta.19" : "0.0.0-test";
34
34
 
35
35
  // src/google-generative-ai-embedding-model.ts
36
36
  var import_provider = require("@ai-sdk/provider");
@@ -505,7 +505,7 @@ function appendLegacyToolResultParts(parts, toolName, outputValue) {
505
505
  }
506
506
  }
507
507
  function convertToGoogleGenerativeAIMessages(prompt, options) {
508
- var _a, _b, _c, _d;
508
+ var _a, _b, _c, _d, _e, _f, _g, _h;
509
509
  const systemInstructionParts = [];
510
510
  const contents = [];
511
511
  let systemMessagesAllowed = true;
@@ -607,6 +607,18 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
607
607
  };
608
608
  }
609
609
  case "tool-call": {
610
+ const serverToolCallId = (providerOpts == null ? void 0 : providerOpts.serverToolCallId) != null ? String(providerOpts.serverToolCallId) : void 0;
611
+ const serverToolType = (providerOpts == null ? void 0 : providerOpts.serverToolType) != null ? String(providerOpts.serverToolType) : void 0;
612
+ if (serverToolCallId && serverToolType) {
613
+ return {
614
+ toolCall: {
615
+ toolType: serverToolType,
616
+ args: typeof part.input === "string" ? JSON.parse(part.input) : part.input,
617
+ id: serverToolCallId
618
+ },
619
+ thoughtSignature
620
+ };
621
+ }
610
622
  return {
611
623
  functionCall: {
612
624
  name: part.toolName,
@@ -615,6 +627,21 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
615
627
  thoughtSignature
616
628
  };
617
629
  }
630
+ case "tool-result": {
631
+ const serverToolCallId = (providerOpts == null ? void 0 : providerOpts.serverToolCallId) != null ? String(providerOpts.serverToolCallId) : void 0;
632
+ const serverToolType = (providerOpts == null ? void 0 : providerOpts.serverToolType) != null ? String(providerOpts.serverToolType) : void 0;
633
+ if (serverToolCallId && serverToolType) {
634
+ return {
635
+ toolResponse: {
636
+ toolType: serverToolType,
637
+ response: part.output.type === "json" ? part.output.value : {},
638
+ id: serverToolCallId
639
+ },
640
+ thoughtSignature
641
+ };
642
+ }
643
+ return void 0;
644
+ }
618
645
  }
619
646
  }).filter((part) => part !== void 0)
620
647
  });
@@ -627,6 +654,26 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
627
654
  if (part.type === "tool-approval-response") {
628
655
  continue;
629
656
  }
657
+ 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;
658
+ const serverToolCallId = (partProviderOpts == null ? void 0 : partProviderOpts.serverToolCallId) != null ? String(partProviderOpts.serverToolCallId) : void 0;
659
+ const serverToolType = (partProviderOpts == null ? void 0 : partProviderOpts.serverToolType) != null ? String(partProviderOpts.serverToolType) : void 0;
660
+ if (serverToolCallId && serverToolType) {
661
+ const serverThoughtSignature = (partProviderOpts == null ? void 0 : partProviderOpts.thoughtSignature) != null ? String(partProviderOpts.thoughtSignature) : void 0;
662
+ if (contents.length > 0) {
663
+ const lastContent = contents[contents.length - 1];
664
+ if (lastContent.role === "model") {
665
+ lastContent.parts.push({
666
+ toolResponse: {
667
+ toolType: serverToolType,
668
+ response: part.output.type === "json" ? part.output.value : {},
669
+ id: serverToolCallId
670
+ },
671
+ thoughtSignature: serverThoughtSignature
672
+ });
673
+ continue;
674
+ }
675
+ }
676
+ }
630
677
  const output = part.output;
631
678
  if (output.type === "content") {
632
679
  if (supportsFunctionResponseParts) {
@@ -640,7 +687,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
640
687
  name: part.toolName,
641
688
  response: {
642
689
  name: part.toolName,
643
- content: output.type === "execution-denied" ? (_d = output.reason) != null ? _d : "Tool execution denied." : output.value
690
+ content: output.type === "execution-denied" ? (_h = output.reason) != null ? _h : "Tool execution denied." : output.value
644
691
  }
645
692
  }
646
693
  });
@@ -806,7 +853,7 @@ function prepareTools({
806
853
  toolChoice,
807
854
  modelId
808
855
  }) {
809
- var _a;
856
+ var _a, _b;
810
857
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
811
858
  const toolWarnings = [];
812
859
  const isLatest = [
@@ -815,13 +862,14 @@ function prepareTools({
815
862
  "gemini-pro-latest"
816
863
  ].some((id) => id === modelId);
817
864
  const isGemini2orNewer = modelId.includes("gemini-2") || modelId.includes("gemini-3") || modelId.includes("nano-banana") || isLatest;
865
+ const isGemini3orNewer = modelId.includes("gemini-3");
818
866
  const supportsFileSearch = modelId.includes("gemini-2.5") || modelId.includes("gemini-3");
819
867
  if (tools == null) {
820
868
  return { tools: void 0, toolConfig: void 0, toolWarnings };
821
869
  }
822
870
  const hasFunctionTools = tools.some((tool) => tool.type === "function");
823
871
  const hasProviderTools = tools.some((tool) => tool.type === "provider");
824
- if (hasFunctionTools && hasProviderTools) {
872
+ if (hasFunctionTools && hasProviderTools && !isGemini3orNewer) {
825
873
  toolWarnings.push({
826
874
  type: "unsupported",
827
875
  feature: `combination of function and provider-defined tools`
@@ -926,6 +974,45 @@ function prepareTools({
926
974
  break;
927
975
  }
928
976
  });
977
+ if (hasFunctionTools && isGemini3orNewer && googleTools2.length > 0) {
978
+ const functionDeclarations2 = [];
979
+ for (const tool of tools) {
980
+ if (tool.type === "function") {
981
+ functionDeclarations2.push({
982
+ name: tool.name,
983
+ description: (_a = tool.description) != null ? _a : "",
984
+ parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
985
+ });
986
+ }
987
+ }
988
+ const combinedToolConfig = {
989
+ functionCallingConfig: { mode: "VALIDATED" },
990
+ includeServerSideToolInvocations: true
991
+ };
992
+ if (toolChoice != null) {
993
+ switch (toolChoice.type) {
994
+ case "auto":
995
+ break;
996
+ case "none":
997
+ combinedToolConfig.functionCallingConfig = { mode: "NONE" };
998
+ break;
999
+ case "required":
1000
+ combinedToolConfig.functionCallingConfig = { mode: "ANY" };
1001
+ break;
1002
+ case "tool":
1003
+ combinedToolConfig.functionCallingConfig = {
1004
+ mode: "ANY",
1005
+ allowedFunctionNames: [toolChoice.toolName]
1006
+ };
1007
+ break;
1008
+ }
1009
+ }
1010
+ return {
1011
+ tools: [...googleTools2, { functionDeclarations: functionDeclarations2 }],
1012
+ toolConfig: combinedToolConfig,
1013
+ toolWarnings
1014
+ };
1015
+ }
929
1016
  return {
930
1017
  tools: googleTools2.length > 0 ? googleTools2 : void 0,
931
1018
  toolConfig: void 0,
@@ -939,7 +1026,7 @@ function prepareTools({
939
1026
  case "function":
940
1027
  functionDeclarations.push({
941
1028
  name: tool.name,
942
- description: (_a = tool.description) != null ? _a : "",
1029
+ description: (_b = tool.description) != null ? _b : "",
943
1030
  parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
944
1031
  });
945
1032
  if (tool.strict === true) {
@@ -1163,7 +1250,7 @@ var GoogleGenerativeAILanguageModel = class {
1163
1250
  };
1164
1251
  }
1165
1252
  async doGenerate(options) {
1166
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
1253
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
1167
1254
  const { args, warnings, providerOptionsName } = await this.getArgs(options);
1168
1255
  const mergedHeaders = (0, import_provider_utils6.combineHeaders)(
1169
1256
  await (0, import_provider_utils6.resolve)(this.config.headers),
@@ -1189,6 +1276,7 @@ var GoogleGenerativeAILanguageModel = class {
1189
1276
  const parts = (_b = (_a = candidate.content) == null ? void 0 : _a.parts) != null ? _b : [];
1190
1277
  const usageMetadata = response.usageMetadata;
1191
1278
  let lastCodeExecutionToolCallId;
1279
+ let lastServerToolCallId;
1192
1280
  for (const part of parts) {
1193
1281
  if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
1194
1282
  const toolCallId = this.config.generateId();
@@ -1255,12 +1343,56 @@ var GoogleGenerativeAILanguageModel = class {
1255
1343
  }
1256
1344
  } : void 0
1257
1345
  });
1346
+ } else if ("toolCall" in part && part.toolCall) {
1347
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : this.config.generateId();
1348
+ lastServerToolCallId = toolCallId;
1349
+ content.push({
1350
+ type: "tool-call",
1351
+ toolCallId,
1352
+ toolName: `server:${part.toolCall.toolType}`,
1353
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
1354
+ providerExecuted: true,
1355
+ dynamic: true,
1356
+ providerMetadata: part.thoughtSignature ? {
1357
+ [providerOptionsName]: {
1358
+ thoughtSignature: part.thoughtSignature,
1359
+ serverToolCallId: toolCallId,
1360
+ serverToolType: part.toolCall.toolType
1361
+ }
1362
+ } : {
1363
+ [providerOptionsName]: {
1364
+ serverToolCallId: toolCallId,
1365
+ serverToolType: part.toolCall.toolType
1366
+ }
1367
+ }
1368
+ });
1369
+ } else if ("toolResponse" in part && part.toolResponse) {
1370
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : this.config.generateId();
1371
+ content.push({
1372
+ type: "tool-result",
1373
+ toolCallId: responseToolCallId,
1374
+ toolName: `server:${part.toolResponse.toolType}`,
1375
+ result: (_h = part.toolResponse.response) != null ? _h : {},
1376
+ providerMetadata: part.thoughtSignature ? {
1377
+ [providerOptionsName]: {
1378
+ thoughtSignature: part.thoughtSignature,
1379
+ serverToolCallId: responseToolCallId,
1380
+ serverToolType: part.toolResponse.toolType
1381
+ }
1382
+ } : {
1383
+ [providerOptionsName]: {
1384
+ serverToolCallId: responseToolCallId,
1385
+ serverToolType: part.toolResponse.toolType
1386
+ }
1387
+ }
1388
+ });
1389
+ lastServerToolCallId = void 0;
1258
1390
  }
1259
1391
  }
1260
- const sources = (_e = extractSources({
1392
+ const sources = (_i = extractSources({
1261
1393
  groundingMetadata: candidate.groundingMetadata,
1262
1394
  generateId: this.config.generateId
1263
- })) != null ? _e : [];
1395
+ })) != null ? _i : [];
1264
1396
  for (const source of sources) {
1265
1397
  content.push(source);
1266
1398
  }
@@ -1274,19 +1406,19 @@ var GoogleGenerativeAILanguageModel = class {
1274
1406
  (part) => part.type === "tool-call" && !part.providerExecuted
1275
1407
  )
1276
1408
  }),
1277
- raw: (_f = candidate.finishReason) != null ? _f : void 0
1409
+ raw: (_j = candidate.finishReason) != null ? _j : void 0
1278
1410
  },
1279
1411
  usage: convertGoogleGenerativeAIUsage(usageMetadata),
1280
1412
  warnings,
1281
1413
  providerMetadata: {
1282
1414
  [providerOptionsName]: {
1283
- promptFeedback: (_g = response.promptFeedback) != null ? _g : null,
1284
- groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
1285
- urlContextMetadata: (_i = candidate.urlContextMetadata) != null ? _i : null,
1286
- safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
1415
+ promptFeedback: (_k = response.promptFeedback) != null ? _k : null,
1416
+ groundingMetadata: (_l = candidate.groundingMetadata) != null ? _l : null,
1417
+ urlContextMetadata: (_m = candidate.urlContextMetadata) != null ? _m : null,
1418
+ safetyRatings: (_n = candidate.safetyRatings) != null ? _n : null,
1287
1419
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1288
- finishMessage: (_k = candidate.finishMessage) != null ? _k : null,
1289
- serviceTier: (_l = response.serviceTier) != null ? _l : null
1420
+ finishMessage: (_o = candidate.finishMessage) != null ? _o : null,
1421
+ serviceTier: (_p = response.serviceTier) != null ? _p : null
1290
1422
  }
1291
1423
  },
1292
1424
  request: { body: args },
@@ -1330,6 +1462,7 @@ var GoogleGenerativeAILanguageModel = class {
1330
1462
  let blockCounter = 0;
1331
1463
  const emittedSourceUrls = /* @__PURE__ */ new Set();
1332
1464
  let lastCodeExecutionToolCallId;
1465
+ let lastServerToolCallId;
1333
1466
  return {
1334
1467
  stream: response.pipeThrough(
1335
1468
  new TransformStream({
@@ -1337,7 +1470,7 @@ var GoogleGenerativeAILanguageModel = class {
1337
1470
  controller.enqueue({ type: "stream-start", warnings });
1338
1471
  },
1339
1472
  transform(chunk, controller) {
1340
- var _a, _b, _c, _d, _e, _f, _g;
1473
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
1341
1474
  if (options.includeRawChunks) {
1342
1475
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1343
1476
  }
@@ -1491,6 +1624,42 @@ var GoogleGenerativeAILanguageModel = class {
1491
1624
  data: part.inlineData.data,
1492
1625
  providerMetadata: fileMeta
1493
1626
  });
1627
+ } else if ("toolCall" in part && part.toolCall) {
1628
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId3();
1629
+ lastServerToolCallId = toolCallId;
1630
+ const serverMeta = {
1631
+ [providerOptionsName]: {
1632
+ ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
1633
+ serverToolCallId: toolCallId,
1634
+ serverToolType: part.toolCall.toolType
1635
+ }
1636
+ };
1637
+ controller.enqueue({
1638
+ type: "tool-call",
1639
+ toolCallId,
1640
+ toolName: `server:${part.toolCall.toolType}`,
1641
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
1642
+ providerExecuted: true,
1643
+ dynamic: true,
1644
+ providerMetadata: serverMeta
1645
+ });
1646
+ } else if ("toolResponse" in part && part.toolResponse) {
1647
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId3();
1648
+ const serverMeta = {
1649
+ [providerOptionsName]: {
1650
+ ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
1651
+ serverToolCallId: responseToolCallId,
1652
+ serverToolType: part.toolResponse.toolType
1653
+ }
1654
+ };
1655
+ controller.enqueue({
1656
+ type: "tool-result",
1657
+ toolCallId: responseToolCallId,
1658
+ toolName: `server:${part.toolResponse.toolType}`,
1659
+ result: (_h = part.toolResponse.response) != null ? _h : {},
1660
+ providerMetadata: serverMeta
1661
+ });
1662
+ lastServerToolCallId = void 0;
1494
1663
  }
1495
1664
  }
1496
1665
  const toolCallDeltas = getToolCallsFromParts({
@@ -1538,12 +1707,12 @@ var GoogleGenerativeAILanguageModel = class {
1538
1707
  };
1539
1708
  providerMetadata = {
1540
1709
  [providerOptionsName]: {
1541
- promptFeedback: (_e = value.promptFeedback) != null ? _e : null,
1710
+ promptFeedback: (_i = value.promptFeedback) != null ? _i : null,
1542
1711
  groundingMetadata: lastGroundingMetadata,
1543
1712
  urlContextMetadata: lastUrlContextMetadata,
1544
- safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null,
1713
+ safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
1545
1714
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1546
- finishMessage: (_g = candidate.finishMessage) != null ? _g : null,
1715
+ finishMessage: (_k = candidate.finishMessage) != null ? _k : null,
1547
1716
  serviceTier
1548
1717
  }
1549
1718
  };
@@ -1827,6 +1996,22 @@ var getContentSchema = () => import_v45.z.object({
1827
1996
  thought: import_v45.z.boolean().nullish(),
1828
1997
  thoughtSignature: import_v45.z.string().nullish()
1829
1998
  }),
1999
+ import_v45.z.object({
2000
+ toolCall: import_v45.z.object({
2001
+ toolType: import_v45.z.string(),
2002
+ args: import_v45.z.unknown().nullish(),
2003
+ id: import_v45.z.string()
2004
+ }),
2005
+ thoughtSignature: import_v45.z.string().nullish()
2006
+ }),
2007
+ import_v45.z.object({
2008
+ toolResponse: import_v45.z.object({
2009
+ toolType: import_v45.z.string(),
2010
+ response: import_v45.z.unknown().nullish(),
2011
+ id: import_v45.z.string()
2012
+ }),
2013
+ thoughtSignature: import_v45.z.string().nullish()
2014
+ }),
1830
2015
  import_v45.z.object({
1831
2016
  executableCode: import_v45.z.object({
1832
2017
  language: import_v45.z.string(),