@ai-sdk/google 2.0.0-canary.8 → 2.0.0

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 z2 } from "zod";
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) {
@@ -103,20 +104,20 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema) {
103
104
  return result;
104
105
  }
105
106
  function isEmptyObjectSchema(jsonSchema) {
106
- return jsonSchema != null && typeof jsonSchema === "object" && jsonSchema.type === "object" && (jsonSchema.properties == null || Object.keys(jsonSchema.properties).length === 0);
107
+ return jsonSchema != null && typeof jsonSchema === "object" && jsonSchema.type === "object" && (jsonSchema.properties == null || Object.keys(jsonSchema.properties).length === 0) && !jsonSchema.additionalProperties;
107
108
  }
108
109
 
109
110
  // src/convert-to-google-generative-ai-messages.ts
110
111
  import {
111
112
  UnsupportedFunctionalityError
112
113
  } from "@ai-sdk/provider";
113
- import {
114
- convertToBase64
115
- } from "@ai-sdk/provider-utils";
116
- function convertToGoogleGenerativeAIMessages(prompt) {
114
+ import { convertToBase64 } from "@ai-sdk/provider-utils";
115
+ function convertToGoogleGenerativeAIMessages(prompt, options) {
116
+ var _a;
117
117
  const systemInstructionParts = [];
118
118
  const contents = [];
119
119
  let systemMessagesAllowed = true;
120
+ const isGemmaModel = (_a = options == null ? void 0 : options.isGemmaModel) != null ? _a : false;
120
121
  for (const { role, content } of prompt) {
121
122
  switch (role) {
122
123
  case "system": {
@@ -190,7 +191,7 @@ function convertToGoogleGenerativeAIMessages(prompt) {
190
191
  return {
191
192
  functionCall: {
192
193
  name: part.toolName,
193
- args: part.args
194
+ args: part.input
194
195
  }
195
196
  };
196
197
  }
@@ -208,7 +209,7 @@ function convertToGoogleGenerativeAIMessages(prompt) {
208
209
  name: part.toolName,
209
210
  response: {
210
211
  name: part.toolName,
211
- content: part.result
212
+ content: part.output.value
212
213
  }
213
214
  }
214
215
  }))
@@ -217,8 +218,12 @@ function convertToGoogleGenerativeAIMessages(prompt) {
217
218
  }
218
219
  }
219
220
  }
221
+ if (isGemmaModel && systemInstructionParts.length > 0 && contents.length > 0 && contents[0].role === "user") {
222
+ const systemText = systemInstructionParts.map((part) => part.text).join("\n\n");
223
+ contents[0].parts.unshift({ text: systemText + "\n\n" });
224
+ }
220
225
  return {
221
- systemInstruction: systemInstructionParts.length > 0 ? { parts: systemInstructionParts } : void 0,
226
+ systemInstruction: systemInstructionParts.length > 0 && !isGemmaModel ? { parts: systemInstructionParts } : void 0,
222
227
  contents
223
228
  };
224
229
  }
@@ -230,7 +235,7 @@ function getModelPath(modelId) {
230
235
 
231
236
  // src/google-error.ts
232
237
  import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
233
- import { z } from "zod";
238
+ import { z } from "zod/v4";
234
239
  var googleErrorDataSchema = z.object({
235
240
  error: z.object({
236
241
  code: z.number().nullable(),
@@ -243,6 +248,68 @@ var googleFailedResponseHandler = createJsonErrorResponseHandler({
243
248
  errorToMessage: (data) => data.error.message
244
249
  });
245
250
 
251
+ // src/google-generative-ai-options.ts
252
+ import { z as z2 } from "zod/v4";
253
+ var googleGenerativeAIProviderOptions = z2.object({
254
+ responseModalities: z2.array(z2.enum(["TEXT", "IMAGE"])).optional(),
255
+ thinkingConfig: z2.object({
256
+ thinkingBudget: z2.number().optional(),
257
+ includeThoughts: z2.boolean().optional()
258
+ }).optional(),
259
+ /**
260
+ Optional.
261
+ The name of the cached content used as context to serve the prediction.
262
+ Format: cachedContents/{cachedContent}
263
+ */
264
+ cachedContent: z2.string().optional(),
265
+ /**
266
+ * Optional. Enable structured output. Default is true.
267
+ *
268
+ * This is useful when the JSON Schema contains elements that are
269
+ * not supported by the OpenAPI schema version that
270
+ * Google Generative AI uses. You can use this to disable
271
+ * structured outputs if you need to.
272
+ */
273
+ structuredOutputs: z2.boolean().optional(),
274
+ /**
275
+ Optional. A list of unique safety settings for blocking unsafe content.
276
+ */
277
+ safetySettings: z2.array(
278
+ z2.object({
279
+ category: z2.enum([
280
+ "HARM_CATEGORY_UNSPECIFIED",
281
+ "HARM_CATEGORY_HATE_SPEECH",
282
+ "HARM_CATEGORY_DANGEROUS_CONTENT",
283
+ "HARM_CATEGORY_HARASSMENT",
284
+ "HARM_CATEGORY_SEXUALLY_EXPLICIT",
285
+ "HARM_CATEGORY_CIVIC_INTEGRITY"
286
+ ]),
287
+ threshold: z2.enum([
288
+ "HARM_BLOCK_THRESHOLD_UNSPECIFIED",
289
+ "BLOCK_LOW_AND_ABOVE",
290
+ "BLOCK_MEDIUM_AND_ABOVE",
291
+ "BLOCK_ONLY_HIGH",
292
+ "BLOCK_NONE",
293
+ "OFF"
294
+ ])
295
+ })
296
+ ).optional(),
297
+ threshold: z2.enum([
298
+ "HARM_BLOCK_THRESHOLD_UNSPECIFIED",
299
+ "BLOCK_LOW_AND_ABOVE",
300
+ "BLOCK_MEDIUM_AND_ABOVE",
301
+ "BLOCK_ONLY_HIGH",
302
+ "BLOCK_NONE",
303
+ "OFF"
304
+ ]).optional(),
305
+ /**
306
+ * Optional. Enables timestamp understanding for audio-only files.
307
+ *
308
+ * https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/audio-understanding
309
+ */
310
+ audioTimestamp: z2.boolean().optional()
311
+ });
312
+
246
313
  // src/google-prepare-tools.ts
247
314
  import {
248
315
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
@@ -250,8 +317,6 @@ import {
250
317
  function prepareTools({
251
318
  tools,
252
319
  toolChoice,
253
- useSearchGrounding,
254
- dynamicRetrievalConfig,
255
320
  modelId
256
321
  }) {
257
322
  var _a;
@@ -259,28 +324,87 @@ function prepareTools({
259
324
  const toolWarnings = [];
260
325
  const isGemini2 = modelId.includes("gemini-2");
261
326
  const supportsDynamicRetrieval = modelId.includes("gemini-1.5-flash") && !modelId.includes("-8b");
262
- 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
+ case "google.code_execution":
374
+ if (isGemini2) {
375
+ googleTools2.codeExecution = {};
376
+ } else {
377
+ toolWarnings.push({
378
+ type: "unsupported-tool",
379
+ tool,
380
+ details: "The code execution tools is not supported with other Gemini models than Gemini 2."
381
+ });
382
+ }
383
+ break;
384
+ default:
385
+ toolWarnings.push({ type: "unsupported-tool", tool });
386
+ break;
387
+ }
388
+ });
263
389
  return {
264
- tools: isGemini2 ? { googleSearch: {} } : {
265
- googleSearchRetrieval: !supportsDynamicRetrieval || !dynamicRetrievalConfig ? {} : { dynamicRetrievalConfig }
266
- },
390
+ tools: Object.keys(googleTools2).length > 0 ? googleTools2 : void 0,
267
391
  toolConfig: void 0,
268
392
  toolWarnings
269
393
  };
270
394
  }
271
- if (tools == null) {
272
- return { tools: void 0, toolConfig: void 0, toolWarnings };
273
- }
274
395
  const functionDeclarations = [];
275
396
  for (const tool of tools) {
276
- if (tool.type === "provider-defined") {
277
- toolWarnings.push({ type: "unsupported-tool", tool });
278
- } else {
279
- functionDeclarations.push({
280
- name: tool.name,
281
- description: (_a = tool.description) != null ? _a : "",
282
- parameters: convertJSONSchemaToOpenAPISchema(tool.parameters)
283
- });
397
+ switch (tool.type) {
398
+ case "function":
399
+ functionDeclarations.push({
400
+ name: tool.name,
401
+ description: (_a = tool.description) != null ? _a : "",
402
+ parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
403
+ });
404
+ break;
405
+ default:
406
+ toolWarnings.push({ type: "unsupported-tool", tool });
407
+ break;
284
408
  }
285
409
  }
286
410
  if (toolChoice == null) {
@@ -357,23 +481,80 @@ function mapGoogleGenerativeAIFinishReason({
357
481
  }
358
482
  }
359
483
 
484
+ // src/tool/google-search.ts
485
+ import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
486
+ import { z as z3 } from "zod/v4";
487
+ var groundingChunkSchema = z3.object({
488
+ web: z3.object({ uri: z3.string(), title: z3.string() }).nullish(),
489
+ retrievedContext: z3.object({ uri: z3.string(), title: z3.string() }).nullish()
490
+ });
491
+ var groundingMetadataSchema = z3.object({
492
+ webSearchQueries: z3.array(z3.string()).nullish(),
493
+ retrievalQueries: z3.array(z3.string()).nullish(),
494
+ searchEntryPoint: z3.object({ renderedContent: z3.string() }).nullish(),
495
+ groundingChunks: z3.array(groundingChunkSchema).nullish(),
496
+ groundingSupports: z3.array(
497
+ z3.object({
498
+ segment: z3.object({
499
+ startIndex: z3.number().nullish(),
500
+ endIndex: z3.number().nullish(),
501
+ text: z3.string().nullish()
502
+ }),
503
+ segment_text: z3.string().nullish(),
504
+ groundingChunkIndices: z3.array(z3.number()).nullish(),
505
+ supportChunkIndices: z3.array(z3.number()).nullish(),
506
+ confidenceScores: z3.array(z3.number()).nullish(),
507
+ confidenceScore: z3.array(z3.number()).nullish()
508
+ })
509
+ ).nullish(),
510
+ retrievalMetadata: z3.union([
511
+ z3.object({
512
+ webDynamicRetrievalScore: z3.number()
513
+ }),
514
+ z3.object({})
515
+ ]).nullish()
516
+ });
517
+ var googleSearch = createProviderDefinedToolFactory({
518
+ id: "google.google_search",
519
+ name: "google_search",
520
+ inputSchema: z3.object({
521
+ mode: z3.enum(["MODE_DYNAMIC", "MODE_UNSPECIFIED"]).default("MODE_UNSPECIFIED"),
522
+ dynamicThreshold: z3.number().default(1)
523
+ })
524
+ });
525
+
526
+ // src/tool/url-context.ts
527
+ import { createProviderDefinedToolFactory as createProviderDefinedToolFactory2 } from "@ai-sdk/provider-utils";
528
+ import { z as z4 } from "zod/v4";
529
+ var urlMetadataSchema = z4.object({
530
+ retrievedUrl: z4.string(),
531
+ urlRetrievalStatus: z4.string()
532
+ });
533
+ var urlContextMetadataSchema = z4.object({
534
+ urlMetadata: z4.array(urlMetadataSchema)
535
+ });
536
+ var urlContext = createProviderDefinedToolFactory2({
537
+ id: "google.url_context",
538
+ name: "url_context",
539
+ inputSchema: z4.object({})
540
+ });
541
+
360
542
  // src/google-generative-ai-language-model.ts
361
543
  var GoogleGenerativeAILanguageModel = class {
362
- constructor(modelId, settings, config) {
544
+ constructor(modelId, config) {
363
545
  this.specificationVersion = "v2";
364
- this.defaultObjectGenerationMode = "json";
365
- this.supportsImageUrls = false;
546
+ var _a;
366
547
  this.modelId = modelId;
367
- this.settings = settings;
368
548
  this.config = config;
369
- }
370
- get supportsStructuredOutputs() {
371
- var _a;
372
- return (_a = this.settings.structuredOutputs) != null ? _a : true;
549
+ this.generateId = (_a = config.generateId) != null ? _a : generateId;
373
550
  }
374
551
  get provider() {
375
552
  return this.config.provider;
376
553
  }
554
+ get supportedUrls() {
555
+ var _a, _b, _c;
556
+ return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
557
+ }
377
558
  async getArgs({
378
559
  prompt,
379
560
  maxOutputTokens,
@@ -389,23 +570,31 @@ var GoogleGenerativeAILanguageModel = class {
389
570
  toolChoice,
390
571
  providerOptions
391
572
  }) {
392
- var _a;
573
+ var _a, _b;
393
574
  const warnings = [];
394
- const googleOptions = parseProviderOptions({
575
+ const googleOptions = await parseProviderOptions({
395
576
  provider: "google",
396
577
  providerOptions,
397
- schema: googleGenerativeAIProviderOptionsSchema
578
+ schema: googleGenerativeAIProviderOptions
398
579
  });
399
- const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(prompt);
580
+ if (((_a = googleOptions == null ? void 0 : googleOptions.thinkingConfig) == null ? void 0 : _a.includeThoughts) === true && !this.config.provider.startsWith("google.vertex.")) {
581
+ warnings.push({
582
+ type: "other",
583
+ message: `The 'includeThoughts' option is only supported with the Google Vertex provider and might not be supported or could behave unexpectedly with the current Google provider (${this.config.provider}).`
584
+ });
585
+ }
586
+ const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
587
+ const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
588
+ prompt,
589
+ { isGemmaModel }
590
+ );
400
591
  const {
401
- tools: googleTools,
592
+ tools: googleTools2,
402
593
  toolConfig: googleToolConfig,
403
594
  toolWarnings
404
595
  } = prepareTools({
405
596
  tools,
406
597
  toolChoice,
407
- useSearchGrounding: (_a = this.settings.useSearchGrounding) != null ? _a : false,
408
- dynamicRetrievalConfig: this.settings.dynamicRetrievalConfig,
409
598
  modelId: this.modelId
410
599
  });
411
600
  return {
@@ -424,28 +613,27 @@ var GoogleGenerativeAILanguageModel = class {
424
613
  responseMimeType: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? "application/json" : void 0,
425
614
  responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
426
615
  // so this is needed as an escape hatch:
427
- this.supportsStructuredOutputs ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
428
- ...this.settings.audioTimestamp && {
429
- audioTimestamp: this.settings.audioTimestamp
616
+ // TODO convert into provider option
617
+ ((_b = googleOptions == null ? void 0 : googleOptions.structuredOutputs) != null ? _b : true) ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
618
+ ...(googleOptions == null ? void 0 : googleOptions.audioTimestamp) && {
619
+ audioTimestamp: googleOptions.audioTimestamp
430
620
  },
431
621
  // provider options:
432
- responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities
622
+ responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities,
623
+ thinkingConfig: googleOptions == null ? void 0 : googleOptions.thinkingConfig
433
624
  },
434
625
  contents,
435
- systemInstruction,
436
- safetySettings: this.settings.safetySettings,
437
- tools: googleTools,
626
+ systemInstruction: isGemmaModel ? void 0 : systemInstruction,
627
+ safetySettings: googleOptions == null ? void 0 : googleOptions.safetySettings,
628
+ tools: googleTools2,
438
629
  toolConfig: googleToolConfig,
439
- cachedContent: this.settings.cachedContent
630
+ cachedContent: googleOptions == null ? void 0 : googleOptions.cachedContent
440
631
  },
441
632
  warnings: [...warnings, ...toolWarnings]
442
633
  };
443
634
  }
444
- supportsUrl(url) {
445
- return this.config.isSupportedUrl(url);
446
- }
447
635
  async doGenerate(options) {
448
- var _a, _b, _c, _d, _e;
636
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
449
637
  const { args, warnings } = await this.getArgs(options);
450
638
  const body = JSON.stringify(args);
451
639
  const mergedHeaders = combineHeaders(
@@ -468,39 +656,84 @@ var GoogleGenerativeAILanguageModel = class {
468
656
  fetch: this.config.fetch
469
657
  });
470
658
  const candidate = response.candidates[0];
471
- const parts = candidate.content == null || typeof candidate.content !== "object" || !("parts" in candidate.content) ? [] : candidate.content.parts;
472
- const toolCalls = getToolCallsFromParts({
473
- parts,
474
- generateId: this.config.generateId
475
- });
659
+ const content = [];
660
+ const parts = (_b = (_a = candidate.content) == null ? void 0 : _a.parts) != null ? _b : [];
476
661
  const usageMetadata = response.usageMetadata;
662
+ let lastCodeExecutionToolCallId;
663
+ for (const part of parts) {
664
+ if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
665
+ const toolCallId = this.config.generateId();
666
+ lastCodeExecutionToolCallId = toolCallId;
667
+ content.push({
668
+ type: "tool-call",
669
+ toolCallId,
670
+ toolName: "code_execution",
671
+ input: JSON.stringify(part.executableCode),
672
+ providerExecuted: true
673
+ });
674
+ } else if ("codeExecutionResult" in part && part.codeExecutionResult) {
675
+ content.push({
676
+ type: "tool-result",
677
+ // Assumes a result directly follows its corresponding call part.
678
+ toolCallId: lastCodeExecutionToolCallId,
679
+ toolName: "code_execution",
680
+ result: {
681
+ outcome: part.codeExecutionResult.outcome,
682
+ output: part.codeExecutionResult.output
683
+ },
684
+ providerExecuted: true
685
+ });
686
+ lastCodeExecutionToolCallId = void 0;
687
+ } else if ("text" in part && part.text != null && part.text.length > 0) {
688
+ if (part.thought === true) {
689
+ content.push({ type: "reasoning", text: part.text });
690
+ } else {
691
+ content.push({ type: "text", text: part.text });
692
+ }
693
+ } else if ("functionCall" in part) {
694
+ content.push({
695
+ type: "tool-call",
696
+ toolCallId: this.config.generateId(),
697
+ toolName: part.functionCall.name,
698
+ input: JSON.stringify(part.functionCall.args)
699
+ });
700
+ } else if ("inlineData" in part) {
701
+ content.push({
702
+ type: "file",
703
+ data: part.inlineData.data,
704
+ mediaType: part.inlineData.mimeType
705
+ });
706
+ }
707
+ }
708
+ const sources = (_d = extractSources({
709
+ groundingMetadata: candidate.groundingMetadata,
710
+ generateId: this.config.generateId
711
+ })) != null ? _d : [];
712
+ for (const source of sources) {
713
+ content.push(source);
714
+ }
477
715
  return {
478
- text: getTextFromParts(parts),
479
- files: (_a = getInlineDataParts(parts)) == null ? void 0 : _a.map((part) => ({
480
- type: "file",
481
- data: part.inlineData.data,
482
- mediaType: part.inlineData.mimeType
483
- })),
484
- toolCalls,
716
+ content,
485
717
  finishReason: mapGoogleGenerativeAIFinishReason({
486
718
  finishReason: candidate.finishReason,
487
- hasToolCalls: toolCalls != null && toolCalls.length > 0
719
+ hasToolCalls: content.some((part) => part.type === "tool-call")
488
720
  }),
489
721
  usage: {
490
- inputTokens: (_b = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _b : void 0,
491
- outputTokens: (_c = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _c : void 0
722
+ inputTokens: (_e = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _e : void 0,
723
+ outputTokens: (_f = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _f : void 0,
724
+ totalTokens: (_g = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _g : void 0,
725
+ reasoningTokens: (_h = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _h : void 0,
726
+ cachedInputTokens: (_i = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _i : void 0
492
727
  },
493
728
  warnings,
494
729
  providerMetadata: {
495
730
  google: {
496
- groundingMetadata: (_d = candidate.groundingMetadata) != null ? _d : null,
497
- safetyRatings: (_e = candidate.safetyRatings) != null ? _e : null
731
+ groundingMetadata: (_j = candidate.groundingMetadata) != null ? _j : null,
732
+ urlContextMetadata: (_k = candidate.urlContextMetadata) != null ? _k : null,
733
+ safetyRatings: (_l = candidate.safetyRatings) != null ? _l : null,
734
+ usageMetadata: usageMetadata != null ? usageMetadata : null
498
735
  }
499
736
  },
500
- sources: extractSources({
501
- groundingMetadata: candidate.groundingMetadata,
502
- generateId: this.config.generateId
503
- }),
504
737
  request: { body },
505
738
  response: {
506
739
  // TODO timestamp, model id, id
@@ -530,16 +763,28 @@ var GoogleGenerativeAILanguageModel = class {
530
763
  let finishReason = "unknown";
531
764
  const usage = {
532
765
  inputTokens: void 0,
533
- outputTokens: void 0
766
+ outputTokens: void 0,
767
+ totalTokens: void 0
534
768
  };
535
769
  let providerMetadata = void 0;
536
- const generateId = this.config.generateId;
770
+ const generateId2 = this.config.generateId;
537
771
  let hasToolCalls = false;
772
+ let currentTextBlockId = null;
773
+ let currentReasoningBlockId = null;
774
+ let blockCounter = 0;
775
+ const emittedSourceUrls = /* @__PURE__ */ new Set();
776
+ let lastCodeExecutionToolCallId;
538
777
  return {
539
778
  stream: response.pipeThrough(
540
779
  new TransformStream({
780
+ start(controller) {
781
+ controller.enqueue({ type: "stream-start", warnings });
782
+ },
541
783
  transform(chunk, controller) {
542
- var _a, _b, _c, _d, _e, _f;
784
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
785
+ if (options.includeRawChunks) {
786
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
787
+ }
543
788
  if (!chunk.success) {
544
789
  controller.enqueue({ type: "error", error: chunk.error });
545
790
  return;
@@ -549,16 +794,99 @@ var GoogleGenerativeAILanguageModel = class {
549
794
  if (usageMetadata != null) {
550
795
  usage.inputTokens = (_a = usageMetadata.promptTokenCount) != null ? _a : void 0;
551
796
  usage.outputTokens = (_b = usageMetadata.candidatesTokenCount) != null ? _b : void 0;
797
+ usage.totalTokens = (_c = usageMetadata.totalTokenCount) != null ? _c : void 0;
798
+ usage.reasoningTokens = (_d = usageMetadata.thoughtsTokenCount) != null ? _d : void 0;
799
+ usage.cachedInputTokens = (_e = usageMetadata.cachedContentTokenCount) != null ? _e : void 0;
552
800
  }
553
- const candidate = (_c = value.candidates) == null ? void 0 : _c[0];
801
+ const candidate = (_f = value.candidates) == null ? void 0 : _f[0];
554
802
  if (candidate == null) {
555
803
  return;
556
804
  }
557
805
  const content = candidate.content;
806
+ const sources = extractSources({
807
+ groundingMetadata: candidate.groundingMetadata,
808
+ generateId: generateId2
809
+ });
810
+ if (sources != null) {
811
+ for (const source of sources) {
812
+ if (source.sourceType === "url" && !emittedSourceUrls.has(source.url)) {
813
+ emittedSourceUrls.add(source.url);
814
+ controller.enqueue(source);
815
+ }
816
+ }
817
+ }
558
818
  if (content != null) {
559
- const deltaText = getTextFromParts(content.parts);
560
- if (deltaText != null) {
561
- controller.enqueue(deltaText);
819
+ const parts = (_g = content.parts) != null ? _g : [];
820
+ for (const part of parts) {
821
+ if ("executableCode" in part && ((_h = part.executableCode) == null ? void 0 : _h.code)) {
822
+ const toolCallId = generateId2();
823
+ lastCodeExecutionToolCallId = toolCallId;
824
+ controller.enqueue({
825
+ type: "tool-call",
826
+ toolCallId,
827
+ toolName: "code_execution",
828
+ input: JSON.stringify(part.executableCode),
829
+ providerExecuted: true
830
+ });
831
+ hasToolCalls = true;
832
+ } else if ("codeExecutionResult" in part && part.codeExecutionResult) {
833
+ const toolCallId = lastCodeExecutionToolCallId;
834
+ if (toolCallId) {
835
+ controller.enqueue({
836
+ type: "tool-result",
837
+ toolCallId,
838
+ toolName: "code_execution",
839
+ result: {
840
+ outcome: part.codeExecutionResult.outcome,
841
+ output: part.codeExecutionResult.output
842
+ },
843
+ providerExecuted: true
844
+ });
845
+ lastCodeExecutionToolCallId = void 0;
846
+ }
847
+ } else if ("text" in part && part.text != null && part.text.length > 0) {
848
+ if (part.thought === true) {
849
+ if (currentTextBlockId !== null) {
850
+ controller.enqueue({
851
+ type: "text-end",
852
+ id: currentTextBlockId
853
+ });
854
+ currentTextBlockId = null;
855
+ }
856
+ if (currentReasoningBlockId === null) {
857
+ currentReasoningBlockId = String(blockCounter++);
858
+ controller.enqueue({
859
+ type: "reasoning-start",
860
+ id: currentReasoningBlockId
861
+ });
862
+ }
863
+ controller.enqueue({
864
+ type: "reasoning-delta",
865
+ id: currentReasoningBlockId,
866
+ delta: part.text
867
+ });
868
+ } else {
869
+ if (currentReasoningBlockId !== null) {
870
+ controller.enqueue({
871
+ type: "reasoning-end",
872
+ id: currentReasoningBlockId
873
+ });
874
+ currentReasoningBlockId = null;
875
+ }
876
+ if (currentTextBlockId === null) {
877
+ currentTextBlockId = String(blockCounter++);
878
+ controller.enqueue({
879
+ type: "text-start",
880
+ id: currentTextBlockId
881
+ });
882
+ }
883
+ controller.enqueue({
884
+ type: "text-delta",
885
+ id: currentTextBlockId,
886
+ delta: part.text
887
+ });
888
+ }
889
+ }
562
890
  }
563
891
  const inlineDataParts = getInlineDataParts(content.parts);
564
892
  if (inlineDataParts != null) {
@@ -572,23 +900,29 @@ var GoogleGenerativeAILanguageModel = class {
572
900
  }
573
901
  const toolCallDeltas = getToolCallsFromParts({
574
902
  parts: content.parts,
575
- generateId
903
+ generateId: generateId2
576
904
  });
577
905
  if (toolCallDeltas != null) {
578
906
  for (const toolCall of toolCallDeltas) {
579
907
  controller.enqueue({
580
- type: "tool-call-delta",
581
- toolCallType: "function",
582
- toolCallId: toolCall.toolCallId,
583
- toolName: toolCall.toolName,
584
- argsTextDelta: toolCall.args
908
+ type: "tool-input-start",
909
+ id: toolCall.toolCallId,
910
+ toolName: toolCall.toolName
911
+ });
912
+ controller.enqueue({
913
+ type: "tool-input-delta",
914
+ id: toolCall.toolCallId,
915
+ delta: toolCall.args
916
+ });
917
+ controller.enqueue({
918
+ type: "tool-input-end",
919
+ id: toolCall.toolCallId
585
920
  });
586
921
  controller.enqueue({
587
922
  type: "tool-call",
588
- toolCallType: "function",
589
923
  toolCallId: toolCall.toolCallId,
590
924
  toolName: toolCall.toolName,
591
- args: toolCall.args
925
+ input: toolCall.args
592
926
  });
593
927
  hasToolCalls = true;
594
928
  }
@@ -599,22 +933,31 @@ var GoogleGenerativeAILanguageModel = class {
599
933
  finishReason: candidate.finishReason,
600
934
  hasToolCalls
601
935
  });
602
- const sources = (_d = extractSources({
603
- groundingMetadata: candidate.groundingMetadata,
604
- generateId
605
- })) != null ? _d : [];
606
- for (const source of sources) {
607
- controller.enqueue(source);
608
- }
609
936
  providerMetadata = {
610
937
  google: {
611
- groundingMetadata: (_e = candidate.groundingMetadata) != null ? _e : null,
612
- safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null
938
+ groundingMetadata: (_i = candidate.groundingMetadata) != null ? _i : null,
939
+ urlContextMetadata: (_j = candidate.urlContextMetadata) != null ? _j : null,
940
+ safetyRatings: (_k = candidate.safetyRatings) != null ? _k : null
613
941
  }
614
942
  };
943
+ if (usageMetadata != null) {
944
+ providerMetadata.google.usageMetadata = usageMetadata;
945
+ }
615
946
  }
616
947
  },
617
948
  flush(controller) {
949
+ if (currentTextBlockId !== null) {
950
+ controller.enqueue({
951
+ type: "text-end",
952
+ id: currentTextBlockId
953
+ });
954
+ }
955
+ if (currentReasoningBlockId !== null) {
956
+ controller.enqueue({
957
+ type: "reasoning-end",
958
+ id: currentReasoningBlockId
959
+ });
960
+ }
618
961
  controller.enqueue({
619
962
  type: "finish",
620
963
  finishReason,
@@ -625,33 +968,24 @@ var GoogleGenerativeAILanguageModel = class {
625
968
  })
626
969
  ),
627
970
  response: { headers: responseHeaders },
628
- warnings,
629
971
  request: { body }
630
972
  };
631
973
  }
632
974
  };
633
975
  function getToolCallsFromParts({
634
976
  parts,
635
- generateId
977
+ generateId: generateId2
636
978
  }) {
637
979
  const functionCallParts = parts == null ? void 0 : parts.filter(
638
980
  (part) => "functionCall" in part
639
981
  );
640
982
  return functionCallParts == null || functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
641
983
  type: "tool-call",
642
- toolCallType: "function",
643
- toolCallId: generateId(),
984
+ toolCallId: generateId2(),
644
985
  toolName: part.functionCall.name,
645
986
  args: JSON.stringify(part.functionCall.args)
646
987
  }));
647
988
  }
648
- function getTextFromParts(parts) {
649
- const textParts = parts == null ? void 0 : parts.filter((part) => "text" in part);
650
- return textParts == null || textParts.length === 0 ? void 0 : {
651
- type: "text",
652
- text: textParts.map((part) => part.text).join("")
653
- };
654
- }
655
989
  function getInlineDataParts(parts) {
656
990
  return parts == null ? void 0 : parts.filter(
657
991
  (part) => "inlineData" in part
@@ -659,7 +993,7 @@ function getInlineDataParts(parts) {
659
993
  }
660
994
  function extractSources({
661
995
  groundingMetadata,
662
- generateId
996
+ generateId: generateId2
663
997
  }) {
664
998
  var _a;
665
999
  return (_a = groundingMetadata == null ? void 0 : groundingMetadata.groundingChunks) == null ? void 0 : _a.filter(
@@ -667,107 +1001,125 @@ function extractSources({
667
1001
  ).map((chunk) => ({
668
1002
  type: "source",
669
1003
  sourceType: "url",
670
- id: generateId(),
1004
+ id: generateId2(),
671
1005
  url: chunk.web.uri,
672
1006
  title: chunk.web.title
673
1007
  }));
674
1008
  }
675
- var contentSchema = z2.object({
676
- role: z2.string(),
677
- parts: z2.array(
678
- z2.union([
679
- z2.object({
680
- text: z2.string()
681
- }),
682
- z2.object({
683
- functionCall: z2.object({
684
- name: z2.string(),
685
- args: z2.unknown()
1009
+ var contentSchema = z5.object({
1010
+ parts: z5.array(
1011
+ z5.union([
1012
+ // note: order matters since text can be fully empty
1013
+ z5.object({
1014
+ functionCall: z5.object({
1015
+ name: z5.string(),
1016
+ args: z5.unknown()
686
1017
  })
687
1018
  }),
688
- z2.object({
689
- inlineData: z2.object({
690
- mimeType: z2.string(),
691
- data: z2.string()
1019
+ z5.object({
1020
+ inlineData: z5.object({
1021
+ mimeType: z5.string(),
1022
+ data: z5.string()
692
1023
  })
1024
+ }),
1025
+ z5.object({
1026
+ executableCode: z5.object({
1027
+ language: z5.string(),
1028
+ code: z5.string()
1029
+ }).nullish(),
1030
+ codeExecutionResult: z5.object({
1031
+ outcome: z5.string(),
1032
+ output: z5.string()
1033
+ }).nullish(),
1034
+ text: z5.string().nullish(),
1035
+ thought: z5.boolean().nullish()
693
1036
  })
694
1037
  ])
695
1038
  ).nullish()
696
1039
  });
697
- var groundingChunkSchema = z2.object({
698
- web: z2.object({ uri: z2.string(), title: z2.string() }).nullish(),
699
- retrievedContext: z2.object({ uri: z2.string(), title: z2.string() }).nullish()
1040
+ var safetyRatingSchema = z5.object({
1041
+ category: z5.string().nullish(),
1042
+ probability: z5.string().nullish(),
1043
+ probabilityScore: z5.number().nullish(),
1044
+ severity: z5.string().nullish(),
1045
+ severityScore: z5.number().nullish(),
1046
+ blocked: z5.boolean().nullish()
700
1047
  });
701
- var groundingMetadataSchema = z2.object({
702
- webSearchQueries: z2.array(z2.string()).nullish(),
703
- retrievalQueries: z2.array(z2.string()).nullish(),
704
- searchEntryPoint: z2.object({ renderedContent: z2.string() }).nullish(),
705
- groundingChunks: z2.array(groundingChunkSchema).nullish(),
706
- groundingSupports: z2.array(
707
- z2.object({
708
- segment: z2.object({
709
- startIndex: z2.number().nullish(),
710
- endIndex: z2.number().nullish(),
711
- text: z2.string().nullish()
712
- }),
713
- segment_text: z2.string().nullish(),
714
- groundingChunkIndices: z2.array(z2.number()).nullish(),
715
- supportChunkIndices: z2.array(z2.number()).nullish(),
716
- confidenceScores: z2.array(z2.number()).nullish(),
717
- confidenceScore: z2.array(z2.number()).nullish()
718
- })
719
- ).nullish(),
720
- retrievalMetadata: z2.union([
721
- z2.object({
722
- webDynamicRetrievalScore: z2.number()
723
- }),
724
- z2.object({})
725
- ]).nullish()
1048
+ var usageSchema = z5.object({
1049
+ cachedContentTokenCount: z5.number().nullish(),
1050
+ thoughtsTokenCount: z5.number().nullish(),
1051
+ promptTokenCount: z5.number().nullish(),
1052
+ candidatesTokenCount: z5.number().nullish(),
1053
+ totalTokenCount: z5.number().nullish()
726
1054
  });
727
- var safetyRatingSchema = z2.object({
728
- category: z2.string(),
729
- probability: z2.string(),
730
- probabilityScore: z2.number().nullish(),
731
- severity: z2.string().nullish(),
732
- severityScore: z2.number().nullish(),
733
- blocked: z2.boolean().nullish()
734
- });
735
- var responseSchema = z2.object({
736
- candidates: z2.array(
737
- z2.object({
738
- content: contentSchema.nullish().or(z2.object({}).strict()),
739
- finishReason: z2.string().nullish(),
740
- safetyRatings: z2.array(safetyRatingSchema).nullish(),
741
- groundingMetadata: groundingMetadataSchema.nullish()
1055
+ var responseSchema = z5.object({
1056
+ candidates: z5.array(
1057
+ z5.object({
1058
+ content: contentSchema.nullish().or(z5.object({}).strict()),
1059
+ finishReason: z5.string().nullish(),
1060
+ safetyRatings: z5.array(safetyRatingSchema).nullish(),
1061
+ groundingMetadata: groundingMetadataSchema.nullish(),
1062
+ urlContextMetadata: urlContextMetadataSchema.nullish()
742
1063
  })
743
1064
  ),
744
- usageMetadata: z2.object({
745
- promptTokenCount: z2.number().nullish(),
746
- candidatesTokenCount: z2.number().nullish(),
747
- totalTokenCount: z2.number().nullish()
748
- }).nullish()
1065
+ usageMetadata: usageSchema.nullish()
749
1066
  });
750
- var chunkSchema = z2.object({
751
- candidates: z2.array(
752
- z2.object({
1067
+ var chunkSchema = z5.object({
1068
+ candidates: z5.array(
1069
+ z5.object({
753
1070
  content: contentSchema.nullish(),
754
- finishReason: z2.string().nullish(),
755
- safetyRatings: z2.array(safetyRatingSchema).nullish(),
756
- groundingMetadata: groundingMetadataSchema.nullish()
1071
+ finishReason: z5.string().nullish(),
1072
+ safetyRatings: z5.array(safetyRatingSchema).nullish(),
1073
+ groundingMetadata: groundingMetadataSchema.nullish(),
1074
+ urlContextMetadata: urlContextMetadataSchema.nullish()
757
1075
  })
758
1076
  ).nullish(),
759
- usageMetadata: z2.object({
760
- promptTokenCount: z2.number().nullish(),
761
- candidatesTokenCount: z2.number().nullish(),
762
- totalTokenCount: z2.number().nullish()
763
- }).nullish()
1077
+ usageMetadata: usageSchema.nullish()
764
1078
  });
765
- var googleGenerativeAIProviderOptionsSchema = z2.object({
766
- responseModalities: z2.array(z2.enum(["TEXT", "IMAGE"])).nullish()
1079
+
1080
+ // src/tool/code-execution.ts
1081
+ import { createProviderDefinedToolFactoryWithOutputSchema } from "@ai-sdk/provider-utils";
1082
+ import { z as z6 } from "zod/v4";
1083
+ var codeExecution = createProviderDefinedToolFactoryWithOutputSchema({
1084
+ id: "google.code_execution",
1085
+ name: "code_execution",
1086
+ inputSchema: z6.object({
1087
+ language: z6.string().describe("The programming language of the code."),
1088
+ code: z6.string().describe("The code to be executed.")
1089
+ }),
1090
+ outputSchema: z6.object({
1091
+ outcome: z6.string().describe('The outcome of the execution (e.g., "OUTCOME_OK").'),
1092
+ output: z6.string().describe("The output from the code execution.")
1093
+ })
767
1094
  });
1095
+
1096
+ // src/google-tools.ts
1097
+ var googleTools = {
1098
+ /**
1099
+ * Creates a Google search tool that gives Google direct access to real-time web content.
1100
+ * Must have name "google_search".
1101
+ */
1102
+ googleSearch,
1103
+ /**
1104
+ * Creates a URL context tool that gives Google direct access to real-time web content.
1105
+ * Must have name "url_context".
1106
+ */
1107
+ urlContext,
1108
+ /**
1109
+ * A tool that enables the model to generate and run Python code.
1110
+ * Must have name "code_execution".
1111
+ *
1112
+ * @note Ensure the selected model supports Code Execution.
1113
+ * Multi-tool usage with the code execution tool is typically compatible with Gemini >=2 models.
1114
+ *
1115
+ * @see https://ai.google.dev/gemini-api/docs/code-execution (Google AI)
1116
+ * @see https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/code-execution-api (Vertex AI)
1117
+ */
1118
+ codeExecution
1119
+ };
768
1120
  export {
769
1121
  GoogleGenerativeAILanguageModel,
770
- groundingMetadataSchema,
1122
+ googleTools,
771
1123
  safetyRatingSchema
772
1124
  };
773
1125
  //# sourceMappingURL=index.mjs.map