@ai-sdk/google 2.0.0-beta.7 → 2.0.0-beta.9

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,8 @@ 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,
618
699
  usageMetadata: usageMetadata != null ? usageMetadata : null
619
700
  }
620
701
  },
@@ -651,7 +732,7 @@ var GoogleGenerativeAILanguageModel = class {
651
732
  totalTokens: void 0
652
733
  };
653
734
  let providerMetadata = void 0;
654
- const generateId = this.config.generateId;
735
+ const generateId2 = this.config.generateId;
655
736
  let hasToolCalls = false;
656
737
  let currentTextBlockId = null;
657
738
  let currentReasoningBlockId = null;
@@ -663,7 +744,7 @@ var GoogleGenerativeAILanguageModel = class {
663
744
  controller.enqueue({ type: "stream-start", warnings });
664
745
  },
665
746
  transform(chunk, controller) {
666
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
747
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
667
748
  if (options.includeRawChunks) {
668
749
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
669
750
  }
@@ -744,7 +825,7 @@ var GoogleGenerativeAILanguageModel = class {
744
825
  }
745
826
  const toolCallDeltas = getToolCallsFromParts({
746
827
  parts: content.parts,
747
- generateId
828
+ generateId: generateId2
748
829
  });
749
830
  if (toolCallDeltas != null) {
750
831
  for (const toolCall of toolCallDeltas) {
@@ -779,7 +860,7 @@ var GoogleGenerativeAILanguageModel = class {
779
860
  });
780
861
  const sources = (_h = extractSources({
781
862
  groundingMetadata: candidate.groundingMetadata,
782
- generateId
863
+ generateId: generateId2
783
864
  })) != null ? _h : [];
784
865
  for (const source of sources) {
785
866
  controller.enqueue(source);
@@ -787,7 +868,8 @@ var GoogleGenerativeAILanguageModel = class {
787
868
  providerMetadata = {
788
869
  google: {
789
870
  groundingMetadata: (_i = candidate.groundingMetadata) != null ? _i : null,
790
- safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null
871
+ urlContextMetadata: (_j = candidate.urlContextMetadata) != null ? _j : null,
872
+ safetyRatings: (_k = candidate.safetyRatings) != null ? _k : null
791
873
  }
792
874
  };
793
875
  if (usageMetadata != null) {
@@ -824,14 +906,14 @@ var GoogleGenerativeAILanguageModel = class {
824
906
  };
825
907
  function getToolCallsFromParts({
826
908
  parts,
827
- generateId
909
+ generateId: generateId2
828
910
  }) {
829
911
  const functionCallParts = parts == null ? void 0 : parts.filter(
830
912
  (part) => "functionCall" in part
831
913
  );
832
914
  return functionCallParts == null || functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
833
915
  type: "tool-call",
834
- toolCallId: generateId(),
916
+ toolCallId: generateId2(),
835
917
  toolName: part.functionCall.name,
836
918
  args: JSON.stringify(part.functionCall.args)
837
919
  }));
@@ -843,7 +925,7 @@ function getInlineDataParts(parts) {
843
925
  }
844
926
  function extractSources({
845
927
  groundingMetadata,
846
- generateId
928
+ generateId: generateId2
847
929
  }) {
848
930
  var _a;
849
931
  return (_a = groundingMetadata == null ? void 0 : groundingMetadata.groundingChunks) == null ? void 0 : _a.filter(
@@ -851,104 +933,90 @@ function extractSources({
851
933
  ).map((chunk) => ({
852
934
  type: "source",
853
935
  sourceType: "url",
854
- id: generateId(),
936
+ id: generateId2(),
855
937
  url: chunk.web.uri,
856
938
  title: chunk.web.title
857
939
  }));
858
940
  }
859
- var contentSchema = z3.object({
860
- parts: z3.array(
861
- z3.union([
941
+ var contentSchema = z5.object({
942
+ parts: z5.array(
943
+ z5.union([
862
944
  // note: order matters since text can be fully empty
863
- z3.object({
864
- functionCall: z3.object({
865
- name: z3.string(),
866
- args: z3.unknown()
945
+ z5.object({
946
+ functionCall: z5.object({
947
+ name: z5.string(),
948
+ args: z5.unknown()
867
949
  })
868
950
  }),
869
- z3.object({
870
- inlineData: z3.object({
871
- mimeType: z3.string(),
872
- data: z3.string()
951
+ z5.object({
952
+ inlineData: z5.object({
953
+ mimeType: z5.string(),
954
+ data: z5.string()
873
955
  })
874
956
  }),
875
- z3.object({
876
- text: z3.string().nullish(),
877
- thought: z3.boolean().nullish()
957
+ z5.object({
958
+ text: z5.string().nullish(),
959
+ thought: z5.boolean().nullish()
878
960
  })
879
961
  ])
880
962
  ).nullish()
881
963
  });
882
- var groundingChunkSchema = z3.object({
883
- web: z3.object({ uri: z3.string(), title: z3.string() }).nullish(),
884
- retrievedContext: z3.object({ uri: z3.string(), title: z3.string() }).nullish()
885
- });
886
- var groundingMetadataSchema = z3.object({
887
- webSearchQueries: z3.array(z3.string()).nullish(),
888
- retrievalQueries: z3.array(z3.string()).nullish(),
889
- searchEntryPoint: z3.object({ renderedContent: z3.string() }).nullish(),
890
- groundingChunks: z3.array(groundingChunkSchema).nullish(),
891
- groundingSupports: z3.array(
892
- z3.object({
893
- segment: z3.object({
894
- startIndex: z3.number().nullish(),
895
- endIndex: z3.number().nullish(),
896
- text: z3.string().nullish()
897
- }),
898
- segment_text: z3.string().nullish(),
899
- groundingChunkIndices: z3.array(z3.number()).nullish(),
900
- supportChunkIndices: z3.array(z3.number()).nullish(),
901
- confidenceScores: z3.array(z3.number()).nullish(),
902
- confidenceScore: z3.array(z3.number()).nullish()
903
- })
904
- ).nullish(),
905
- retrievalMetadata: z3.union([
906
- z3.object({
907
- webDynamicRetrievalScore: z3.number()
908
- }),
909
- z3.object({})
910
- ]).nullish()
911
- });
912
- var safetyRatingSchema = z3.object({
913
- category: z3.string().nullish(),
914
- probability: z3.string().nullish(),
915
- probabilityScore: z3.number().nullish(),
916
- severity: z3.string().nullish(),
917
- severityScore: z3.number().nullish(),
918
- blocked: z3.boolean().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()
919
971
  });
920
- var usageSchema = z3.object({
921
- cachedContentTokenCount: z3.number().nullish(),
922
- thoughtsTokenCount: z3.number().nullish(),
923
- promptTokenCount: z3.number().nullish(),
924
- candidatesTokenCount: z3.number().nullish(),
925
- totalTokenCount: z3.number().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()
926
978
  });
927
- var responseSchema = z3.object({
928
- candidates: z3.array(
929
- z3.object({
930
- content: contentSchema.nullish().or(z3.object({}).strict()),
931
- finishReason: z3.string().nullish(),
932
- safetyRatings: z3.array(safetyRatingSchema).nullish(),
933
- 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()
934
987
  })
935
988
  ),
936
989
  usageMetadata: usageSchema.nullish()
937
990
  });
938
- var chunkSchema = z3.object({
939
- candidates: z3.array(
940
- z3.object({
991
+ var chunkSchema = z5.object({
992
+ candidates: z5.array(
993
+ z5.object({
941
994
  content: contentSchema.nullish(),
942
- finishReason: z3.string().nullish(),
943
- safetyRatings: z3.array(safetyRatingSchema).nullish(),
944
- groundingMetadata: groundingMetadataSchema.nullish()
995
+ finishReason: z5.string().nullish(),
996
+ safetyRatings: z5.array(safetyRatingSchema).nullish(),
997
+ groundingMetadata: groundingMetadataSchema.nullish(),
998
+ urlContextMetadata: urlContextMetadataSchema.nullish()
945
999
  })
946
1000
  ).nullish(),
947
1001
  usageMetadata: usageSchema.nullish()
948
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
+ };
949
1017
  export {
950
1018
  GoogleGenerativeAILanguageModel,
951
- groundingMetadataSchema,
1019
+ googleTools,
952
1020
  safetyRatingSchema
953
1021
  };
954
1022
  //# sourceMappingURL=index.mjs.map