@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.
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 ? "3.0.54" : "0.0.0-test";
10
+ var VERSION = true ? "3.0.55" : "0.0.0-test";
11
11
 
12
12
  // src/google-generative-ai-embedding-model.ts
13
13
  import {
@@ -511,7 +511,7 @@ function appendLegacyToolResultParts(parts, toolName, outputValue) {
511
511
  }
512
512
  }
513
513
  function convertToGoogleGenerativeAIMessages(prompt, options) {
514
- var _a, _b, _c, _d;
514
+ var _a, _b, _c, _d, _e, _f, _g, _h;
515
515
  const systemInstructionParts = [];
516
516
  const contents = [];
517
517
  let systemMessagesAllowed = true;
@@ -598,6 +598,18 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
598
598
  };
599
599
  }
600
600
  case "tool-call": {
601
+ const serverToolCallId = (providerOpts == null ? void 0 : providerOpts.serverToolCallId) != null ? String(providerOpts.serverToolCallId) : void 0;
602
+ const serverToolType = (providerOpts == null ? void 0 : providerOpts.serverToolType) != null ? String(providerOpts.serverToolType) : void 0;
603
+ if (serverToolCallId && serverToolType) {
604
+ return {
605
+ toolCall: {
606
+ toolType: serverToolType,
607
+ args: typeof part.input === "string" ? JSON.parse(part.input) : part.input,
608
+ id: serverToolCallId
609
+ },
610
+ thoughtSignature
611
+ };
612
+ }
601
613
  return {
602
614
  functionCall: {
603
615
  name: part.toolName,
@@ -606,6 +618,21 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
606
618
  thoughtSignature
607
619
  };
608
620
  }
621
+ case "tool-result": {
622
+ const serverToolCallId = (providerOpts == null ? void 0 : providerOpts.serverToolCallId) != null ? String(providerOpts.serverToolCallId) : void 0;
623
+ const serverToolType = (providerOpts == null ? void 0 : providerOpts.serverToolType) != null ? String(providerOpts.serverToolType) : void 0;
624
+ if (serverToolCallId && serverToolType) {
625
+ return {
626
+ toolResponse: {
627
+ toolType: serverToolType,
628
+ response: part.output.type === "json" ? part.output.value : {},
629
+ id: serverToolCallId
630
+ },
631
+ thoughtSignature
632
+ };
633
+ }
634
+ return void 0;
635
+ }
609
636
  }
610
637
  }).filter((part) => part !== void 0)
611
638
  });
@@ -618,6 +645,26 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
618
645
  if (part.type === "tool-approval-response") {
619
646
  continue;
620
647
  }
648
+ 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;
649
+ const serverToolCallId = (partProviderOpts == null ? void 0 : partProviderOpts.serverToolCallId) != null ? String(partProviderOpts.serverToolCallId) : void 0;
650
+ const serverToolType = (partProviderOpts == null ? void 0 : partProviderOpts.serverToolType) != null ? String(partProviderOpts.serverToolType) : void 0;
651
+ if (serverToolCallId && serverToolType) {
652
+ const serverThoughtSignature = (partProviderOpts == null ? void 0 : partProviderOpts.thoughtSignature) != null ? String(partProviderOpts.thoughtSignature) : void 0;
653
+ if (contents.length > 0) {
654
+ const lastContent = contents[contents.length - 1];
655
+ if (lastContent.role === "model") {
656
+ lastContent.parts.push({
657
+ toolResponse: {
658
+ toolType: serverToolType,
659
+ response: part.output.type === "json" ? part.output.value : {},
660
+ id: serverToolCallId
661
+ },
662
+ thoughtSignature: serverThoughtSignature
663
+ });
664
+ continue;
665
+ }
666
+ }
667
+ }
621
668
  const output = part.output;
622
669
  if (output.type === "content") {
623
670
  if (supportsFunctionResponseParts) {
@@ -631,7 +678,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
631
678
  name: part.toolName,
632
679
  response: {
633
680
  name: part.toolName,
634
- content: output.type === "execution-denied" ? (_d = output.reason) != null ? _d : "Tool execution denied." : output.value
681
+ content: output.type === "execution-denied" ? (_h = output.reason) != null ? _h : "Tool execution denied." : output.value
635
682
  }
636
683
  }
637
684
  });
@@ -799,7 +846,7 @@ function prepareTools({
799
846
  toolChoice,
800
847
  modelId
801
848
  }) {
802
- var _a;
849
+ var _a, _b;
803
850
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
804
851
  const toolWarnings = [];
805
852
  const isLatest = [
@@ -808,13 +855,14 @@ function prepareTools({
808
855
  "gemini-pro-latest"
809
856
  ].some((id) => id === modelId);
810
857
  const isGemini2orNewer = modelId.includes("gemini-2") || modelId.includes("gemini-3") || modelId.includes("nano-banana") || isLatest;
858
+ const isGemini3orNewer = modelId.includes("gemini-3");
811
859
  const supportsFileSearch = modelId.includes("gemini-2.5") || modelId.includes("gemini-3");
812
860
  if (tools == null) {
813
861
  return { tools: void 0, toolConfig: void 0, toolWarnings };
814
862
  }
815
863
  const hasFunctionTools = tools.some((tool) => tool.type === "function");
816
864
  const hasProviderTools = tools.some((tool) => tool.type === "provider");
817
- if (hasFunctionTools && hasProviderTools) {
865
+ if (hasFunctionTools && hasProviderTools && !isGemini3orNewer) {
818
866
  toolWarnings.push({
819
867
  type: "unsupported",
820
868
  feature: `combination of function and provider-defined tools`
@@ -919,6 +967,45 @@ function prepareTools({
919
967
  break;
920
968
  }
921
969
  });
970
+ if (hasFunctionTools && isGemini3orNewer && googleTools2.length > 0) {
971
+ const functionDeclarations2 = [];
972
+ for (const tool of tools) {
973
+ if (tool.type === "function") {
974
+ functionDeclarations2.push({
975
+ name: tool.name,
976
+ description: (_a = tool.description) != null ? _a : "",
977
+ parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
978
+ });
979
+ }
980
+ }
981
+ const combinedToolConfig = {
982
+ functionCallingConfig: { mode: "VALIDATED" },
983
+ includeServerSideToolInvocations: true
984
+ };
985
+ if (toolChoice != null) {
986
+ switch (toolChoice.type) {
987
+ case "auto":
988
+ break;
989
+ case "none":
990
+ combinedToolConfig.functionCallingConfig = { mode: "NONE" };
991
+ break;
992
+ case "required":
993
+ combinedToolConfig.functionCallingConfig = { mode: "ANY" };
994
+ break;
995
+ case "tool":
996
+ combinedToolConfig.functionCallingConfig = {
997
+ mode: "ANY",
998
+ allowedFunctionNames: [toolChoice.toolName]
999
+ };
1000
+ break;
1001
+ }
1002
+ }
1003
+ return {
1004
+ tools: [...googleTools2, { functionDeclarations: functionDeclarations2 }],
1005
+ toolConfig: combinedToolConfig,
1006
+ toolWarnings
1007
+ };
1008
+ }
922
1009
  return {
923
1010
  tools: googleTools2.length > 0 ? googleTools2 : void 0,
924
1011
  toolConfig: void 0,
@@ -932,7 +1019,7 @@ function prepareTools({
932
1019
  case "function":
933
1020
  functionDeclarations.push({
934
1021
  name: tool.name,
935
- description: (_a = tool.description) != null ? _a : "",
1022
+ description: (_b = tool.description) != null ? _b : "",
936
1023
  parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
937
1024
  });
938
1025
  if (tool.strict === true) {
@@ -1149,7 +1236,7 @@ var GoogleGenerativeAILanguageModel = class {
1149
1236
  };
1150
1237
  }
1151
1238
  async doGenerate(options) {
1152
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
1239
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
1153
1240
  const { args, warnings, providerOptionsName } = await this.getArgs(options);
1154
1241
  const mergedHeaders = combineHeaders2(
1155
1242
  await resolve2(this.config.headers),
@@ -1175,6 +1262,7 @@ var GoogleGenerativeAILanguageModel = class {
1175
1262
  const parts = (_b = (_a = candidate.content) == null ? void 0 : _a.parts) != null ? _b : [];
1176
1263
  const usageMetadata = response.usageMetadata;
1177
1264
  let lastCodeExecutionToolCallId;
1265
+ let lastServerToolCallId;
1178
1266
  for (const part of parts) {
1179
1267
  if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
1180
1268
  const toolCallId = this.config.generateId();
@@ -1242,12 +1330,56 @@ var GoogleGenerativeAILanguageModel = class {
1242
1330
  }
1243
1331
  } : void 0
1244
1332
  });
1333
+ } else if ("toolCall" in part && part.toolCall) {
1334
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : this.config.generateId();
1335
+ lastServerToolCallId = toolCallId;
1336
+ content.push({
1337
+ type: "tool-call",
1338
+ toolCallId,
1339
+ toolName: `server:${part.toolCall.toolType}`,
1340
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
1341
+ providerExecuted: true,
1342
+ dynamic: true,
1343
+ providerMetadata: part.thoughtSignature ? {
1344
+ [providerOptionsName]: {
1345
+ thoughtSignature: part.thoughtSignature,
1346
+ serverToolCallId: toolCallId,
1347
+ serverToolType: part.toolCall.toolType
1348
+ }
1349
+ } : {
1350
+ [providerOptionsName]: {
1351
+ serverToolCallId: toolCallId,
1352
+ serverToolType: part.toolCall.toolType
1353
+ }
1354
+ }
1355
+ });
1356
+ } else if ("toolResponse" in part && part.toolResponse) {
1357
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : this.config.generateId();
1358
+ content.push({
1359
+ type: "tool-result",
1360
+ toolCallId: responseToolCallId,
1361
+ toolName: `server:${part.toolResponse.toolType}`,
1362
+ result: (_h = part.toolResponse.response) != null ? _h : {},
1363
+ providerMetadata: part.thoughtSignature ? {
1364
+ [providerOptionsName]: {
1365
+ thoughtSignature: part.thoughtSignature,
1366
+ serverToolCallId: responseToolCallId,
1367
+ serverToolType: part.toolResponse.toolType
1368
+ }
1369
+ } : {
1370
+ [providerOptionsName]: {
1371
+ serverToolCallId: responseToolCallId,
1372
+ serverToolType: part.toolResponse.toolType
1373
+ }
1374
+ }
1375
+ });
1376
+ lastServerToolCallId = void 0;
1245
1377
  }
1246
1378
  }
1247
- const sources = (_e = extractSources({
1379
+ const sources = (_i = extractSources({
1248
1380
  groundingMetadata: candidate.groundingMetadata,
1249
1381
  generateId: this.config.generateId
1250
- })) != null ? _e : [];
1382
+ })) != null ? _i : [];
1251
1383
  for (const source of sources) {
1252
1384
  content.push(source);
1253
1385
  }
@@ -1261,19 +1393,19 @@ var GoogleGenerativeAILanguageModel = class {
1261
1393
  (part) => part.type === "tool-call" && !part.providerExecuted
1262
1394
  )
1263
1395
  }),
1264
- raw: (_f = candidate.finishReason) != null ? _f : void 0
1396
+ raw: (_j = candidate.finishReason) != null ? _j : void 0
1265
1397
  },
1266
1398
  usage: convertGoogleGenerativeAIUsage(usageMetadata),
1267
1399
  warnings,
1268
1400
  providerMetadata: {
1269
1401
  [providerOptionsName]: {
1270
- promptFeedback: (_g = response.promptFeedback) != null ? _g : null,
1271
- groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
1272
- urlContextMetadata: (_i = candidate.urlContextMetadata) != null ? _i : null,
1273
- safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
1402
+ promptFeedback: (_k = response.promptFeedback) != null ? _k : null,
1403
+ groundingMetadata: (_l = candidate.groundingMetadata) != null ? _l : null,
1404
+ urlContextMetadata: (_m = candidate.urlContextMetadata) != null ? _m : null,
1405
+ safetyRatings: (_n = candidate.safetyRatings) != null ? _n : null,
1274
1406
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1275
- finishMessage: (_k = candidate.finishMessage) != null ? _k : null,
1276
- serviceTier: (_l = response.serviceTier) != null ? _l : null
1407
+ finishMessage: (_o = candidate.finishMessage) != null ? _o : null,
1408
+ serviceTier: (_p = response.serviceTier) != null ? _p : null
1277
1409
  }
1278
1410
  },
1279
1411
  request: { body: args },
@@ -1317,6 +1449,7 @@ var GoogleGenerativeAILanguageModel = class {
1317
1449
  let blockCounter = 0;
1318
1450
  const emittedSourceUrls = /* @__PURE__ */ new Set();
1319
1451
  let lastCodeExecutionToolCallId;
1452
+ let lastServerToolCallId;
1320
1453
  return {
1321
1454
  stream: response.pipeThrough(
1322
1455
  new TransformStream({
@@ -1324,7 +1457,7 @@ var GoogleGenerativeAILanguageModel = class {
1324
1457
  controller.enqueue({ type: "stream-start", warnings });
1325
1458
  },
1326
1459
  transform(chunk, controller) {
1327
- var _a, _b, _c, _d, _e, _f, _g;
1460
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
1328
1461
  if (options.includeRawChunks) {
1329
1462
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1330
1463
  }
@@ -1479,6 +1612,42 @@ var GoogleGenerativeAILanguageModel = class {
1479
1612
  data: part.inlineData.data,
1480
1613
  providerMetadata: fileMeta
1481
1614
  });
1615
+ } else if ("toolCall" in part && part.toolCall) {
1616
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId3();
1617
+ lastServerToolCallId = toolCallId;
1618
+ const serverMeta = {
1619
+ [providerOptionsName]: {
1620
+ ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
1621
+ serverToolCallId: toolCallId,
1622
+ serverToolType: part.toolCall.toolType
1623
+ }
1624
+ };
1625
+ controller.enqueue({
1626
+ type: "tool-call",
1627
+ toolCallId,
1628
+ toolName: `server:${part.toolCall.toolType}`,
1629
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
1630
+ providerExecuted: true,
1631
+ dynamic: true,
1632
+ providerMetadata: serverMeta
1633
+ });
1634
+ } else if ("toolResponse" in part && part.toolResponse) {
1635
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId3();
1636
+ const serverMeta = {
1637
+ [providerOptionsName]: {
1638
+ ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
1639
+ serverToolCallId: responseToolCallId,
1640
+ serverToolType: part.toolResponse.toolType
1641
+ }
1642
+ };
1643
+ controller.enqueue({
1644
+ type: "tool-result",
1645
+ toolCallId: responseToolCallId,
1646
+ toolName: `server:${part.toolResponse.toolType}`,
1647
+ result: (_h = part.toolResponse.response) != null ? _h : {},
1648
+ providerMetadata: serverMeta
1649
+ });
1650
+ lastServerToolCallId = void 0;
1482
1651
  }
1483
1652
  }
1484
1653
  const toolCallDeltas = getToolCallsFromParts({
@@ -1526,12 +1695,12 @@ var GoogleGenerativeAILanguageModel = class {
1526
1695
  };
1527
1696
  providerMetadata = {
1528
1697
  [providerOptionsName]: {
1529
- promptFeedback: (_e = value.promptFeedback) != null ? _e : null,
1698
+ promptFeedback: (_i = value.promptFeedback) != null ? _i : null,
1530
1699
  groundingMetadata: lastGroundingMetadata,
1531
1700
  urlContextMetadata: lastUrlContextMetadata,
1532
- safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null,
1701
+ safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
1533
1702
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1534
- finishMessage: (_g = candidate.finishMessage) != null ? _g : null,
1703
+ finishMessage: (_k = candidate.finishMessage) != null ? _k : null,
1535
1704
  serviceTier
1536
1705
  }
1537
1706
  };
@@ -1746,6 +1915,22 @@ var getContentSchema = () => z5.object({
1746
1915
  thought: z5.boolean().nullish(),
1747
1916
  thoughtSignature: z5.string().nullish()
1748
1917
  }),
1918
+ z5.object({
1919
+ toolCall: z5.object({
1920
+ toolType: z5.string(),
1921
+ args: z5.unknown().nullish(),
1922
+ id: z5.string()
1923
+ }),
1924
+ thoughtSignature: z5.string().nullish()
1925
+ }),
1926
+ z5.object({
1927
+ toolResponse: z5.object({
1928
+ toolType: z5.string(),
1929
+ response: z5.unknown().nullish(),
1930
+ id: z5.string()
1931
+ }),
1932
+ thoughtSignature: z5.string().nullish()
1933
+ }),
1749
1934
  z5.object({
1750
1935
  executableCode: z5.object({
1751
1936
  language: z5.string(),