@ai-sdk/google 2.0.0-beta.6 → 2.0.0-beta.8

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.
@@ -3,11 +3,12 @@ import {
3
3
  combineHeaders,
4
4
  createEventSourceResponseHandler,
5
5
  createJsonResponseHandler,
6
+ generateId,
6
7
  parseProviderOptions,
7
8
  postJsonToApi,
8
9
  resolve
9
10
  } from "@ai-sdk/provider-utils";
10
- import { z as z3 } from "zod/v4";
11
+ import { z as z5 } from "zod/v4";
11
12
 
12
13
  // src/convert-json-schema-to-openapi-schema.ts
13
14
  function convertJSONSchemaToOpenAPISchema(jsonSchema) {
@@ -249,17 +250,6 @@ var googleFailedResponseHandler = createJsonErrorResponseHandler({
249
250
 
250
251
  // src/google-generative-ai-options.ts
251
252
  import { z as z2 } from "zod/v4";
252
- var dynamicRetrievalConfig = z2.object({
253
- /**
254
- * The mode of the predictor to be used in dynamic retrieval.
255
- */
256
- mode: z2.enum(["MODE_UNSPECIFIED", "MODE_DYNAMIC"]).optional(),
257
- /**
258
- * The threshold to be used in dynamic retrieval. If not set, a system default
259
- * value is used.
260
- */
261
- dynamicThreshold: z2.number().optional()
262
- });
263
253
  var googleGenerativeAIProviderOptions = z2.object({
264
254
  responseModalities: z2.array(z2.enum(["TEXT", "IMAGE"])).optional(),
265
255
  thinkingConfig: z2.object({
@@ -317,21 +307,7 @@ var googleGenerativeAIProviderOptions = z2.object({
317
307
  *
318
308
  * https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/audio-understanding
319
309
  */
320
- audioTimestamp: z2.boolean().optional(),
321
- /**
322
- Optional. When enabled, the model will use Google search to ground the response.
323
-
324
- @see https://cloud.google.com/vertex-ai/generative-ai/docs/grounding/overview
325
- */
326
- useSearchGrounding: z2.boolean().optional(),
327
- /**
328
- Optional. Specifies the dynamic retrieval configuration.
329
-
330
- @note Dynamic retrieval is only compatible with Gemini 1.5 Flash.
331
-
332
- @see https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/ground-with-google-search#dynamic-retrieval
333
- */
334
- dynamicRetrievalConfig: dynamicRetrievalConfig.optional()
310
+ audioTimestamp: z2.boolean().optional()
335
311
  });
336
312
 
337
313
  // src/google-prepare-tools.ts
@@ -341,8 +317,6 @@ import {
341
317
  function prepareTools({
342
318
  tools,
343
319
  toolChoice,
344
- useSearchGrounding,
345
- dynamicRetrievalConfig: dynamicRetrievalConfig2,
346
320
  modelId
347
321
  }) {
348
322
  var _a;
@@ -350,28 +324,76 @@ function prepareTools({
350
324
  const toolWarnings = [];
351
325
  const isGemini2 = modelId.includes("gemini-2");
352
326
  const supportsDynamicRetrieval = modelId.includes("gemini-1.5-flash") && !modelId.includes("-8b");
353
- if (useSearchGrounding) {
327
+ if (tools == null) {
328
+ return { tools: void 0, toolConfig: void 0, toolWarnings };
329
+ }
330
+ const hasFunctionTools = tools.some((tool) => tool.type === "function");
331
+ const hasProviderDefinedTools = tools.some(
332
+ (tool) => tool.type === "provider-defined"
333
+ );
334
+ if (hasFunctionTools && hasProviderDefinedTools) {
335
+ toolWarnings.push({
336
+ type: "unsupported-tool",
337
+ tool: tools.find((tool) => tool.type === "function"),
338
+ details: "Cannot mix function tools with provider-defined tools in the same request. Please use either function tools or provider-defined tools, but not both."
339
+ });
340
+ }
341
+ if (hasProviderDefinedTools) {
342
+ const googleTools2 = {};
343
+ const providerDefinedTools = tools.filter(
344
+ (tool) => tool.type === "provider-defined"
345
+ );
346
+ providerDefinedTools.forEach((tool) => {
347
+ switch (tool.id) {
348
+ case "google.google_search":
349
+ if (isGemini2) {
350
+ googleTools2.googleSearch = {};
351
+ } else if (supportsDynamicRetrieval) {
352
+ googleTools2.googleSearchRetrieval = {
353
+ dynamicRetrievalConfig: {
354
+ mode: tool.args.mode,
355
+ dynamicThreshold: tool.args.dynamicThreshold
356
+ }
357
+ };
358
+ } else {
359
+ googleTools2.googleSearchRetrieval = {};
360
+ }
361
+ break;
362
+ case "google.url_context":
363
+ if (isGemini2) {
364
+ googleTools2.urlContext = {};
365
+ } else {
366
+ toolWarnings.push({
367
+ type: "unsupported-tool",
368
+ tool,
369
+ details: "The URL context tool is not supported with other Gemini models than Gemini 2."
370
+ });
371
+ }
372
+ break;
373
+ default:
374
+ toolWarnings.push({ type: "unsupported-tool", tool });
375
+ break;
376
+ }
377
+ });
354
378
  return {
355
- tools: isGemini2 ? { googleSearch: {} } : {
356
- googleSearchRetrieval: !supportsDynamicRetrieval || !dynamicRetrievalConfig2 ? {} : { dynamicRetrievalConfig: dynamicRetrievalConfig2 }
357
- },
379
+ tools: Object.keys(googleTools2).length > 0 ? googleTools2 : void 0,
358
380
  toolConfig: void 0,
359
381
  toolWarnings
360
382
  };
361
383
  }
362
- if (tools == null) {
363
- return { tools: void 0, toolConfig: void 0, toolWarnings };
364
- }
365
384
  const functionDeclarations = [];
366
385
  for (const tool of tools) {
367
- if (tool.type === "provider-defined") {
368
- toolWarnings.push({ type: "unsupported-tool", tool });
369
- } else {
370
- functionDeclarations.push({
371
- name: tool.name,
372
- description: (_a = tool.description) != null ? _a : "",
373
- parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
374
- });
386
+ switch (tool.type) {
387
+ case "function":
388
+ functionDeclarations.push({
389
+ name: tool.name,
390
+ description: (_a = tool.description) != null ? _a : "",
391
+ parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
392
+ });
393
+ break;
394
+ default:
395
+ toolWarnings.push({ type: "unsupported-tool", tool });
396
+ break;
375
397
  }
376
398
  }
377
399
  if (toolChoice == null) {
@@ -448,12 +470,72 @@ function mapGoogleGenerativeAIFinishReason({
448
470
  }
449
471
  }
450
472
 
473
+ // src/tool/google-search.ts
474
+ import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
475
+ import { z as z3 } from "zod/v4";
476
+ var groundingChunkSchema = z3.object({
477
+ web: z3.object({ uri: z3.string(), title: z3.string() }).nullish(),
478
+ retrievedContext: z3.object({ uri: z3.string(), title: z3.string() }).nullish()
479
+ });
480
+ var groundingMetadataSchema = z3.object({
481
+ webSearchQueries: z3.array(z3.string()).nullish(),
482
+ retrievalQueries: z3.array(z3.string()).nullish(),
483
+ searchEntryPoint: z3.object({ renderedContent: z3.string() }).nullish(),
484
+ groundingChunks: z3.array(groundingChunkSchema).nullish(),
485
+ groundingSupports: z3.array(
486
+ z3.object({
487
+ segment: z3.object({
488
+ startIndex: z3.number().nullish(),
489
+ endIndex: z3.number().nullish(),
490
+ text: z3.string().nullish()
491
+ }),
492
+ segment_text: z3.string().nullish(),
493
+ groundingChunkIndices: z3.array(z3.number()).nullish(),
494
+ supportChunkIndices: z3.array(z3.number()).nullish(),
495
+ confidenceScores: z3.array(z3.number()).nullish(),
496
+ confidenceScore: z3.array(z3.number()).nullish()
497
+ })
498
+ ).nullish(),
499
+ retrievalMetadata: z3.union([
500
+ z3.object({
501
+ webDynamicRetrievalScore: z3.number()
502
+ }),
503
+ z3.object({})
504
+ ]).nullish()
505
+ });
506
+ var googleSearch = createProviderDefinedToolFactory({
507
+ id: "google.google_search",
508
+ name: "google_search",
509
+ inputSchema: z3.object({
510
+ mode: z3.enum(["MODE_DYNAMIC", "MODE_UNSPECIFIED"]).default("MODE_UNSPECIFIED"),
511
+ dynamicThreshold: z3.number().default(1)
512
+ })
513
+ });
514
+
515
+ // src/tool/url-context.ts
516
+ import { createProviderDefinedToolFactory as createProviderDefinedToolFactory2 } from "@ai-sdk/provider-utils";
517
+ import { z as z4 } from "zod/v4";
518
+ var urlMetadataSchema = z4.object({
519
+ retrievedUrl: z4.string(),
520
+ urlRetrievalStatus: z4.string()
521
+ });
522
+ var urlContextMetadataSchema = z4.object({
523
+ urlMetadata: z4.array(urlMetadataSchema)
524
+ });
525
+ var urlContext = createProviderDefinedToolFactory2({
526
+ id: "google.url_context",
527
+ name: "url_context",
528
+ inputSchema: z4.object({})
529
+ });
530
+
451
531
  // src/google-generative-ai-language-model.ts
452
532
  var GoogleGenerativeAILanguageModel = class {
453
533
  constructor(modelId, config) {
454
534
  this.specificationVersion = "v2";
535
+ var _a;
455
536
  this.modelId = modelId;
456
537
  this.config = config;
538
+ this.generateId = (_a = config.generateId) != null ? _a : generateId;
457
539
  }
458
540
  get provider() {
459
541
  return this.config.provider;
@@ -477,7 +559,7 @@ var GoogleGenerativeAILanguageModel = class {
477
559
  toolChoice,
478
560
  providerOptions
479
561
  }) {
480
- var _a, _b, _c;
562
+ var _a, _b;
481
563
  const warnings = [];
482
564
  const googleOptions = await parseProviderOptions({
483
565
  provider: "google",
@@ -496,14 +578,12 @@ var GoogleGenerativeAILanguageModel = class {
496
578
  { isGemmaModel }
497
579
  );
498
580
  const {
499
- tools: googleTools,
581
+ tools: googleTools2,
500
582
  toolConfig: googleToolConfig,
501
583
  toolWarnings
502
584
  } = prepareTools({
503
585
  tools,
504
586
  toolChoice,
505
- useSearchGrounding: (_b = googleOptions == null ? void 0 : googleOptions.useSearchGrounding) != null ? _b : false,
506
- dynamicRetrievalConfig: googleOptions == null ? void 0 : googleOptions.dynamicRetrievalConfig,
507
587
  modelId: this.modelId
508
588
  });
509
589
  return {
@@ -523,7 +603,7 @@ var GoogleGenerativeAILanguageModel = class {
523
603
  responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
524
604
  // so this is needed as an escape hatch:
525
605
  // TODO convert into provider option
526
- ((_c = googleOptions == null ? void 0 : googleOptions.structuredOutputs) != null ? _c : true) ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
606
+ ((_b = googleOptions == null ? void 0 : googleOptions.structuredOutputs) != null ? _b : true) ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
527
607
  ...(googleOptions == null ? void 0 : googleOptions.audioTimestamp) && {
528
608
  audioTimestamp: googleOptions.audioTimestamp
529
609
  },
@@ -534,7 +614,7 @@ var GoogleGenerativeAILanguageModel = class {
534
614
  contents,
535
615
  systemInstruction: isGemmaModel ? void 0 : systemInstruction,
536
616
  safetySettings: googleOptions == null ? void 0 : googleOptions.safetySettings,
537
- tools: googleTools,
617
+ tools: googleTools2,
538
618
  toolConfig: googleToolConfig,
539
619
  cachedContent: googleOptions == null ? void 0 : googleOptions.cachedContent
540
620
  },
@@ -542,7 +622,7 @@ var GoogleGenerativeAILanguageModel = class {
542
622
  };
543
623
  }
544
624
  async doGenerate(options) {
545
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
625
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
546
626
  const { args, warnings } = await this.getArgs(options);
547
627
  const body = JSON.stringify(args);
548
628
  const mergedHeaders = combineHeaders(
@@ -614,7 +694,9 @@ var GoogleGenerativeAILanguageModel = class {
614
694
  providerMetadata: {
615
695
  google: {
616
696
  groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
617
- safetyRatings: (_i = candidate.safetyRatings) != null ? _i : null
697
+ urlContextMetadata: (_i = candidate.urlContextMetadata) != null ? _i : null,
698
+ safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
699
+ usageMetadata: usageMetadata != null ? usageMetadata : null
618
700
  }
619
701
  },
620
702
  request: { body },
@@ -650,7 +732,7 @@ var GoogleGenerativeAILanguageModel = class {
650
732
  totalTokens: void 0
651
733
  };
652
734
  let providerMetadata = void 0;
653
- const generateId = this.config.generateId;
735
+ const generateId2 = this.config.generateId;
654
736
  let hasToolCalls = false;
655
737
  let currentTextBlockId = null;
656
738
  let currentReasoningBlockId = null;
@@ -662,7 +744,7 @@ var GoogleGenerativeAILanguageModel = class {
662
744
  controller.enqueue({ type: "stream-start", warnings });
663
745
  },
664
746
  transform(chunk, controller) {
665
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
747
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
666
748
  if (options.includeRawChunks) {
667
749
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
668
750
  }
@@ -743,7 +825,7 @@ var GoogleGenerativeAILanguageModel = class {
743
825
  }
744
826
  const toolCallDeltas = getToolCallsFromParts({
745
827
  parts: content.parts,
746
- generateId
828
+ generateId: generateId2
747
829
  });
748
830
  if (toolCallDeltas != null) {
749
831
  for (const toolCall of toolCallDeltas) {
@@ -778,7 +860,7 @@ var GoogleGenerativeAILanguageModel = class {
778
860
  });
779
861
  const sources = (_h = extractSources({
780
862
  groundingMetadata: candidate.groundingMetadata,
781
- generateId
863
+ generateId: generateId2
782
864
  })) != null ? _h : [];
783
865
  for (const source of sources) {
784
866
  controller.enqueue(source);
@@ -786,9 +868,13 @@ var GoogleGenerativeAILanguageModel = class {
786
868
  providerMetadata = {
787
869
  google: {
788
870
  groundingMetadata: (_i = candidate.groundingMetadata) != null ? _i : null,
789
- safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null
871
+ urlContextMetadata: (_j = candidate.urlContextMetadata) != null ? _j : null,
872
+ safetyRatings: (_k = candidate.safetyRatings) != null ? _k : null
790
873
  }
791
874
  };
875
+ if (usageMetadata != null) {
876
+ providerMetadata.google.usageMetadata = usageMetadata;
877
+ }
792
878
  }
793
879
  },
794
880
  flush(controller) {
@@ -820,14 +906,14 @@ var GoogleGenerativeAILanguageModel = class {
820
906
  };
821
907
  function getToolCallsFromParts({
822
908
  parts,
823
- generateId
909
+ generateId: generateId2
824
910
  }) {
825
911
  const functionCallParts = parts == null ? void 0 : parts.filter(
826
912
  (part) => "functionCall" in part
827
913
  );
828
914
  return functionCallParts == null || functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
829
915
  type: "tool-call",
830
- toolCallId: generateId(),
916
+ toolCallId: generateId2(),
831
917
  toolName: part.functionCall.name,
832
918
  args: JSON.stringify(part.functionCall.args)
833
919
  }));
@@ -839,7 +925,7 @@ function getInlineDataParts(parts) {
839
925
  }
840
926
  function extractSources({
841
927
  groundingMetadata,
842
- generateId
928
+ generateId: generateId2
843
929
  }) {
844
930
  var _a;
845
931
  return (_a = groundingMetadata == null ? void 0 : groundingMetadata.groundingChunks) == null ? void 0 : _a.filter(
@@ -847,104 +933,90 @@ function extractSources({
847
933
  ).map((chunk) => ({
848
934
  type: "source",
849
935
  sourceType: "url",
850
- id: generateId(),
936
+ id: generateId2(),
851
937
  url: chunk.web.uri,
852
938
  title: chunk.web.title
853
939
  }));
854
940
  }
855
- var contentSchema = z3.object({
856
- parts: z3.array(
857
- z3.union([
941
+ var contentSchema = z5.object({
942
+ parts: z5.array(
943
+ z5.union([
858
944
  // note: order matters since text can be fully empty
859
- z3.object({
860
- functionCall: z3.object({
861
- name: z3.string(),
862
- args: z3.unknown()
945
+ z5.object({
946
+ functionCall: z5.object({
947
+ name: z5.string(),
948
+ args: z5.unknown()
863
949
  })
864
950
  }),
865
- z3.object({
866
- inlineData: z3.object({
867
- mimeType: z3.string(),
868
- data: z3.string()
951
+ z5.object({
952
+ inlineData: z5.object({
953
+ mimeType: z5.string(),
954
+ data: z5.string()
869
955
  })
870
956
  }),
871
- z3.object({
872
- text: z3.string().nullish(),
873
- thought: z3.boolean().nullish()
957
+ z5.object({
958
+ text: z5.string().nullish(),
959
+ thought: z5.boolean().nullish()
874
960
  })
875
961
  ])
876
962
  ).nullish()
877
963
  });
878
- var groundingChunkSchema = z3.object({
879
- web: z3.object({ uri: z3.string(), title: z3.string() }).nullish(),
880
- retrievedContext: z3.object({ uri: z3.string(), title: z3.string() }).nullish()
964
+ var safetyRatingSchema = z5.object({
965
+ category: z5.string().nullish(),
966
+ probability: z5.string().nullish(),
967
+ probabilityScore: z5.number().nullish(),
968
+ severity: z5.string().nullish(),
969
+ severityScore: z5.number().nullish(),
970
+ blocked: z5.boolean().nullish()
881
971
  });
882
- var groundingMetadataSchema = z3.object({
883
- webSearchQueries: z3.array(z3.string()).nullish(),
884
- retrievalQueries: z3.array(z3.string()).nullish(),
885
- searchEntryPoint: z3.object({ renderedContent: z3.string() }).nullish(),
886
- groundingChunks: z3.array(groundingChunkSchema).nullish(),
887
- groundingSupports: z3.array(
888
- z3.object({
889
- segment: z3.object({
890
- startIndex: z3.number().nullish(),
891
- endIndex: z3.number().nullish(),
892
- text: z3.string().nullish()
893
- }),
894
- segment_text: z3.string().nullish(),
895
- groundingChunkIndices: z3.array(z3.number()).nullish(),
896
- supportChunkIndices: z3.array(z3.number()).nullish(),
897
- confidenceScores: z3.array(z3.number()).nullish(),
898
- confidenceScore: z3.array(z3.number()).nullish()
899
- })
900
- ).nullish(),
901
- retrievalMetadata: z3.union([
902
- z3.object({
903
- webDynamicRetrievalScore: z3.number()
904
- }),
905
- z3.object({})
906
- ]).nullish()
907
- });
908
- var safetyRatingSchema = z3.object({
909
- category: z3.string().nullish(),
910
- probability: z3.string().nullish(),
911
- probabilityScore: z3.number().nullish(),
912
- severity: z3.string().nullish(),
913
- severityScore: z3.number().nullish(),
914
- blocked: z3.boolean().nullish()
972
+ var usageSchema = z5.object({
973
+ cachedContentTokenCount: z5.number().nullish(),
974
+ thoughtsTokenCount: z5.number().nullish(),
975
+ promptTokenCount: z5.number().nullish(),
976
+ candidatesTokenCount: z5.number().nullish(),
977
+ totalTokenCount: z5.number().nullish()
915
978
  });
916
- var usageSchema = z3.object({
917
- cachedContentTokenCount: z3.number().nullish(),
918
- thoughtsTokenCount: z3.number().nullish(),
919
- promptTokenCount: z3.number().nullish(),
920
- candidatesTokenCount: z3.number().nullish(),
921
- totalTokenCount: z3.number().nullish()
922
- });
923
- var responseSchema = z3.object({
924
- candidates: z3.array(
925
- z3.object({
926
- content: contentSchema.nullish().or(z3.object({}).strict()),
927
- finishReason: z3.string().nullish(),
928
- safetyRatings: z3.array(safetyRatingSchema).nullish(),
929
- groundingMetadata: groundingMetadataSchema.nullish()
979
+ var responseSchema = z5.object({
980
+ candidates: z5.array(
981
+ z5.object({
982
+ content: contentSchema.nullish().or(z5.object({}).strict()),
983
+ finishReason: z5.string().nullish(),
984
+ safetyRatings: z5.array(safetyRatingSchema).nullish(),
985
+ groundingMetadata: groundingMetadataSchema.nullish(),
986
+ urlContextMetadata: urlContextMetadataSchema.nullish()
930
987
  })
931
988
  ),
932
989
  usageMetadata: usageSchema.nullish()
933
990
  });
934
- var chunkSchema = z3.object({
935
- candidates: z3.array(
936
- z3.object({
991
+ var chunkSchema = z5.object({
992
+ candidates: z5.array(
993
+ z5.object({
937
994
  content: contentSchema.nullish(),
938
- finishReason: z3.string().nullish(),
939
- safetyRatings: z3.array(safetyRatingSchema).nullish(),
940
- groundingMetadata: groundingMetadataSchema.nullish()
995
+ finishReason: z5.string().nullish(),
996
+ safetyRatings: z5.array(safetyRatingSchema).nullish(),
997
+ groundingMetadata: groundingMetadataSchema.nullish(),
998
+ urlContextMetadata: urlContextMetadataSchema.nullish()
941
999
  })
942
1000
  ).nullish(),
943
1001
  usageMetadata: usageSchema.nullish()
944
1002
  });
1003
+
1004
+ // src/google-tools.ts
1005
+ var googleTools = {
1006
+ /**
1007
+ * Creates a Google search tool that gives Google direct access to real-time web content.
1008
+ * Must have name "google_search".
1009
+ */
1010
+ googleSearch,
1011
+ /**
1012
+ * Creates a URL context tool that gives Google direct access to real-time web content.
1013
+ * Must have name "url_context".
1014
+ */
1015
+ urlContext
1016
+ };
945
1017
  export {
946
1018
  GoogleGenerativeAILanguageModel,
947
- groundingMetadataSchema,
1019
+ googleTools,
948
1020
  safetyRatingSchema
949
1021
  };
950
1022
  //# sourceMappingURL=index.mjs.map