@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/dist/index.mjs CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  } from "@ai-sdk/provider-utils";
8
8
 
9
9
  // src/version.ts
10
- var VERSION = true ? "4.0.0-beta.18" : "0.0.0-test";
10
+ var VERSION = true ? "4.0.0-beta.19" : "0.0.0-test";
11
11
 
12
12
  // src/google-generative-ai-embedding-model.ts
13
13
  import {
@@ -514,7 +514,7 @@ function appendLegacyToolResultParts(parts, toolName, outputValue) {
514
514
  }
515
515
  }
516
516
  function convertToGoogleGenerativeAIMessages(prompt, options) {
517
- var _a, _b, _c, _d;
517
+ var _a, _b, _c, _d, _e, _f, _g, _h;
518
518
  const systemInstructionParts = [];
519
519
  const contents = [];
520
520
  let systemMessagesAllowed = true;
@@ -616,6 +616,18 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
616
616
  };
617
617
  }
618
618
  case "tool-call": {
619
+ const serverToolCallId = (providerOpts == null ? void 0 : providerOpts.serverToolCallId) != null ? String(providerOpts.serverToolCallId) : void 0;
620
+ const serverToolType = (providerOpts == null ? void 0 : providerOpts.serverToolType) != null ? String(providerOpts.serverToolType) : void 0;
621
+ if (serverToolCallId && serverToolType) {
622
+ return {
623
+ toolCall: {
624
+ toolType: serverToolType,
625
+ args: typeof part.input === "string" ? JSON.parse(part.input) : part.input,
626
+ id: serverToolCallId
627
+ },
628
+ thoughtSignature
629
+ };
630
+ }
619
631
  return {
620
632
  functionCall: {
621
633
  name: part.toolName,
@@ -624,6 +636,21 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
624
636
  thoughtSignature
625
637
  };
626
638
  }
639
+ case "tool-result": {
640
+ const serverToolCallId = (providerOpts == null ? void 0 : providerOpts.serverToolCallId) != null ? String(providerOpts.serverToolCallId) : void 0;
641
+ const serverToolType = (providerOpts == null ? void 0 : providerOpts.serverToolType) != null ? String(providerOpts.serverToolType) : void 0;
642
+ if (serverToolCallId && serverToolType) {
643
+ return {
644
+ toolResponse: {
645
+ toolType: serverToolType,
646
+ response: part.output.type === "json" ? part.output.value : {},
647
+ id: serverToolCallId
648
+ },
649
+ thoughtSignature
650
+ };
651
+ }
652
+ return void 0;
653
+ }
627
654
  }
628
655
  }).filter((part) => part !== void 0)
629
656
  });
@@ -636,6 +663,26 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
636
663
  if (part.type === "tool-approval-response") {
637
664
  continue;
638
665
  }
666
+ 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;
667
+ const serverToolCallId = (partProviderOpts == null ? void 0 : partProviderOpts.serverToolCallId) != null ? String(partProviderOpts.serverToolCallId) : void 0;
668
+ const serverToolType = (partProviderOpts == null ? void 0 : partProviderOpts.serverToolType) != null ? String(partProviderOpts.serverToolType) : void 0;
669
+ if (serverToolCallId && serverToolType) {
670
+ const serverThoughtSignature = (partProviderOpts == null ? void 0 : partProviderOpts.thoughtSignature) != null ? String(partProviderOpts.thoughtSignature) : void 0;
671
+ if (contents.length > 0) {
672
+ const lastContent = contents[contents.length - 1];
673
+ if (lastContent.role === "model") {
674
+ lastContent.parts.push({
675
+ toolResponse: {
676
+ toolType: serverToolType,
677
+ response: part.output.type === "json" ? part.output.value : {},
678
+ id: serverToolCallId
679
+ },
680
+ thoughtSignature: serverThoughtSignature
681
+ });
682
+ continue;
683
+ }
684
+ }
685
+ }
639
686
  const output = part.output;
640
687
  if (output.type === "content") {
641
688
  if (supportsFunctionResponseParts) {
@@ -649,7 +696,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
649
696
  name: part.toolName,
650
697
  response: {
651
698
  name: part.toolName,
652
- content: output.type === "execution-denied" ? (_d = output.reason) != null ? _d : "Tool execution denied." : output.value
699
+ content: output.type === "execution-denied" ? (_h = output.reason) != null ? _h : "Tool execution denied." : output.value
653
700
  }
654
701
  }
655
702
  });
@@ -817,7 +864,7 @@ function prepareTools({
817
864
  toolChoice,
818
865
  modelId
819
866
  }) {
820
- var _a;
867
+ var _a, _b;
821
868
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
822
869
  const toolWarnings = [];
823
870
  const isLatest = [
@@ -826,13 +873,14 @@ function prepareTools({
826
873
  "gemini-pro-latest"
827
874
  ].some((id) => id === modelId);
828
875
  const isGemini2orNewer = modelId.includes("gemini-2") || modelId.includes("gemini-3") || modelId.includes("nano-banana") || isLatest;
876
+ const isGemini3orNewer = modelId.includes("gemini-3");
829
877
  const supportsFileSearch = modelId.includes("gemini-2.5") || modelId.includes("gemini-3");
830
878
  if (tools == null) {
831
879
  return { tools: void 0, toolConfig: void 0, toolWarnings };
832
880
  }
833
881
  const hasFunctionTools = tools.some((tool) => tool.type === "function");
834
882
  const hasProviderTools = tools.some((tool) => tool.type === "provider");
835
- if (hasFunctionTools && hasProviderTools) {
883
+ if (hasFunctionTools && hasProviderTools && !isGemini3orNewer) {
836
884
  toolWarnings.push({
837
885
  type: "unsupported",
838
886
  feature: `combination of function and provider-defined tools`
@@ -937,6 +985,45 @@ function prepareTools({
937
985
  break;
938
986
  }
939
987
  });
988
+ if (hasFunctionTools && isGemini3orNewer && googleTools2.length > 0) {
989
+ const functionDeclarations2 = [];
990
+ for (const tool of tools) {
991
+ if (tool.type === "function") {
992
+ functionDeclarations2.push({
993
+ name: tool.name,
994
+ description: (_a = tool.description) != null ? _a : "",
995
+ parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
996
+ });
997
+ }
998
+ }
999
+ const combinedToolConfig = {
1000
+ functionCallingConfig: { mode: "VALIDATED" },
1001
+ includeServerSideToolInvocations: true
1002
+ };
1003
+ if (toolChoice != null) {
1004
+ switch (toolChoice.type) {
1005
+ case "auto":
1006
+ break;
1007
+ case "none":
1008
+ combinedToolConfig.functionCallingConfig = { mode: "NONE" };
1009
+ break;
1010
+ case "required":
1011
+ combinedToolConfig.functionCallingConfig = { mode: "ANY" };
1012
+ break;
1013
+ case "tool":
1014
+ combinedToolConfig.functionCallingConfig = {
1015
+ mode: "ANY",
1016
+ allowedFunctionNames: [toolChoice.toolName]
1017
+ };
1018
+ break;
1019
+ }
1020
+ }
1021
+ return {
1022
+ tools: [...googleTools2, { functionDeclarations: functionDeclarations2 }],
1023
+ toolConfig: combinedToolConfig,
1024
+ toolWarnings
1025
+ };
1026
+ }
940
1027
  return {
941
1028
  tools: googleTools2.length > 0 ? googleTools2 : void 0,
942
1029
  toolConfig: void 0,
@@ -950,7 +1037,7 @@ function prepareTools({
950
1037
  case "function":
951
1038
  functionDeclarations.push({
952
1039
  name: tool.name,
953
- description: (_a = tool.description) != null ? _a : "",
1040
+ description: (_b = tool.description) != null ? _b : "",
954
1041
  parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
955
1042
  });
956
1043
  if (tool.strict === true) {
@@ -1174,7 +1261,7 @@ var GoogleGenerativeAILanguageModel = class {
1174
1261
  };
1175
1262
  }
1176
1263
  async doGenerate(options) {
1177
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
1264
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
1178
1265
  const { args, warnings, providerOptionsName } = await this.getArgs(options);
1179
1266
  const mergedHeaders = combineHeaders2(
1180
1267
  await resolve2(this.config.headers),
@@ -1200,6 +1287,7 @@ var GoogleGenerativeAILanguageModel = class {
1200
1287
  const parts = (_b = (_a = candidate.content) == null ? void 0 : _a.parts) != null ? _b : [];
1201
1288
  const usageMetadata = response.usageMetadata;
1202
1289
  let lastCodeExecutionToolCallId;
1290
+ let lastServerToolCallId;
1203
1291
  for (const part of parts) {
1204
1292
  if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
1205
1293
  const toolCallId = this.config.generateId();
@@ -1266,12 +1354,56 @@ var GoogleGenerativeAILanguageModel = class {
1266
1354
  }
1267
1355
  } : void 0
1268
1356
  });
1357
+ } else if ("toolCall" in part && part.toolCall) {
1358
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : this.config.generateId();
1359
+ lastServerToolCallId = toolCallId;
1360
+ content.push({
1361
+ type: "tool-call",
1362
+ toolCallId,
1363
+ toolName: `server:${part.toolCall.toolType}`,
1364
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
1365
+ providerExecuted: true,
1366
+ dynamic: true,
1367
+ providerMetadata: part.thoughtSignature ? {
1368
+ [providerOptionsName]: {
1369
+ thoughtSignature: part.thoughtSignature,
1370
+ serverToolCallId: toolCallId,
1371
+ serverToolType: part.toolCall.toolType
1372
+ }
1373
+ } : {
1374
+ [providerOptionsName]: {
1375
+ serverToolCallId: toolCallId,
1376
+ serverToolType: part.toolCall.toolType
1377
+ }
1378
+ }
1379
+ });
1380
+ } else if ("toolResponse" in part && part.toolResponse) {
1381
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : this.config.generateId();
1382
+ content.push({
1383
+ type: "tool-result",
1384
+ toolCallId: responseToolCallId,
1385
+ toolName: `server:${part.toolResponse.toolType}`,
1386
+ result: (_h = part.toolResponse.response) != null ? _h : {},
1387
+ providerMetadata: part.thoughtSignature ? {
1388
+ [providerOptionsName]: {
1389
+ thoughtSignature: part.thoughtSignature,
1390
+ serverToolCallId: responseToolCallId,
1391
+ serverToolType: part.toolResponse.toolType
1392
+ }
1393
+ } : {
1394
+ [providerOptionsName]: {
1395
+ serverToolCallId: responseToolCallId,
1396
+ serverToolType: part.toolResponse.toolType
1397
+ }
1398
+ }
1399
+ });
1400
+ lastServerToolCallId = void 0;
1269
1401
  }
1270
1402
  }
1271
- const sources = (_e = extractSources({
1403
+ const sources = (_i = extractSources({
1272
1404
  groundingMetadata: candidate.groundingMetadata,
1273
1405
  generateId: this.config.generateId
1274
- })) != null ? _e : [];
1406
+ })) != null ? _i : [];
1275
1407
  for (const source of sources) {
1276
1408
  content.push(source);
1277
1409
  }
@@ -1285,19 +1417,19 @@ var GoogleGenerativeAILanguageModel = class {
1285
1417
  (part) => part.type === "tool-call" && !part.providerExecuted
1286
1418
  )
1287
1419
  }),
1288
- raw: (_f = candidate.finishReason) != null ? _f : void 0
1420
+ raw: (_j = candidate.finishReason) != null ? _j : void 0
1289
1421
  },
1290
1422
  usage: convertGoogleGenerativeAIUsage(usageMetadata),
1291
1423
  warnings,
1292
1424
  providerMetadata: {
1293
1425
  [providerOptionsName]: {
1294
- promptFeedback: (_g = response.promptFeedback) != null ? _g : null,
1295
- groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
1296
- urlContextMetadata: (_i = candidate.urlContextMetadata) != null ? _i : null,
1297
- safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
1426
+ promptFeedback: (_k = response.promptFeedback) != null ? _k : null,
1427
+ groundingMetadata: (_l = candidate.groundingMetadata) != null ? _l : null,
1428
+ urlContextMetadata: (_m = candidate.urlContextMetadata) != null ? _m : null,
1429
+ safetyRatings: (_n = candidate.safetyRatings) != null ? _n : null,
1298
1430
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1299
- finishMessage: (_k = candidate.finishMessage) != null ? _k : null,
1300
- serviceTier: (_l = response.serviceTier) != null ? _l : null
1431
+ finishMessage: (_o = candidate.finishMessage) != null ? _o : null,
1432
+ serviceTier: (_p = response.serviceTier) != null ? _p : null
1301
1433
  }
1302
1434
  },
1303
1435
  request: { body: args },
@@ -1341,6 +1473,7 @@ var GoogleGenerativeAILanguageModel = class {
1341
1473
  let blockCounter = 0;
1342
1474
  const emittedSourceUrls = /* @__PURE__ */ new Set();
1343
1475
  let lastCodeExecutionToolCallId;
1476
+ let lastServerToolCallId;
1344
1477
  return {
1345
1478
  stream: response.pipeThrough(
1346
1479
  new TransformStream({
@@ -1348,7 +1481,7 @@ var GoogleGenerativeAILanguageModel = class {
1348
1481
  controller.enqueue({ type: "stream-start", warnings });
1349
1482
  },
1350
1483
  transform(chunk, controller) {
1351
- var _a, _b, _c, _d, _e, _f, _g;
1484
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
1352
1485
  if (options.includeRawChunks) {
1353
1486
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1354
1487
  }
@@ -1502,6 +1635,42 @@ var GoogleGenerativeAILanguageModel = class {
1502
1635
  data: part.inlineData.data,
1503
1636
  providerMetadata: fileMeta
1504
1637
  });
1638
+ } else if ("toolCall" in part && part.toolCall) {
1639
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId3();
1640
+ lastServerToolCallId = toolCallId;
1641
+ const serverMeta = {
1642
+ [providerOptionsName]: {
1643
+ ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
1644
+ serverToolCallId: toolCallId,
1645
+ serverToolType: part.toolCall.toolType
1646
+ }
1647
+ };
1648
+ controller.enqueue({
1649
+ type: "tool-call",
1650
+ toolCallId,
1651
+ toolName: `server:${part.toolCall.toolType}`,
1652
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
1653
+ providerExecuted: true,
1654
+ dynamic: true,
1655
+ providerMetadata: serverMeta
1656
+ });
1657
+ } else if ("toolResponse" in part && part.toolResponse) {
1658
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId3();
1659
+ const serverMeta = {
1660
+ [providerOptionsName]: {
1661
+ ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
1662
+ serverToolCallId: responseToolCallId,
1663
+ serverToolType: part.toolResponse.toolType
1664
+ }
1665
+ };
1666
+ controller.enqueue({
1667
+ type: "tool-result",
1668
+ toolCallId: responseToolCallId,
1669
+ toolName: `server:${part.toolResponse.toolType}`,
1670
+ result: (_h = part.toolResponse.response) != null ? _h : {},
1671
+ providerMetadata: serverMeta
1672
+ });
1673
+ lastServerToolCallId = void 0;
1505
1674
  }
1506
1675
  }
1507
1676
  const toolCallDeltas = getToolCallsFromParts({
@@ -1549,12 +1718,12 @@ var GoogleGenerativeAILanguageModel = class {
1549
1718
  };
1550
1719
  providerMetadata = {
1551
1720
  [providerOptionsName]: {
1552
- promptFeedback: (_e = value.promptFeedback) != null ? _e : null,
1721
+ promptFeedback: (_i = value.promptFeedback) != null ? _i : null,
1553
1722
  groundingMetadata: lastGroundingMetadata,
1554
1723
  urlContextMetadata: lastUrlContextMetadata,
1555
- safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null,
1724
+ safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
1556
1725
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1557
- finishMessage: (_g = candidate.finishMessage) != null ? _g : null,
1726
+ finishMessage: (_k = candidate.finishMessage) != null ? _k : null,
1558
1727
  serviceTier
1559
1728
  }
1560
1729
  };
@@ -1838,6 +2007,22 @@ var getContentSchema = () => z5.object({
1838
2007
  thought: z5.boolean().nullish(),
1839
2008
  thoughtSignature: z5.string().nullish()
1840
2009
  }),
2010
+ z5.object({
2011
+ toolCall: z5.object({
2012
+ toolType: z5.string(),
2013
+ args: z5.unknown().nullish(),
2014
+ id: z5.string()
2015
+ }),
2016
+ thoughtSignature: z5.string().nullish()
2017
+ }),
2018
+ z5.object({
2019
+ toolResponse: z5.object({
2020
+ toolType: z5.string(),
2021
+ response: z5.unknown().nullish(),
2022
+ id: z5.string()
2023
+ }),
2024
+ thoughtSignature: z5.string().nullish()
2025
+ }),
1841
2026
  z5.object({
1842
2027
  executableCode: z5.object({
1843
2028
  language: z5.string(),