@ai-sdk/google 2.0.0-canary.9 → 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, _f;
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(
@@ -469,17 +657,45 @@ var GoogleGenerativeAILanguageModel = class {
469
657
  });
470
658
  const candidate = response.candidates[0];
471
659
  const content = [];
472
- const parts = candidate.content == null || typeof candidate.content !== "object" || !("parts" in candidate.content) ? [] : (_a = candidate.content.parts) != null ? _a : [];
660
+ const parts = (_b = (_a = candidate.content) == null ? void 0 : _a.parts) != null ? _b : [];
661
+ const usageMetadata = response.usageMetadata;
662
+ let lastCodeExecutionToolCallId;
473
663
  for (const part of parts) {
474
- if ("text" in part && part.text.length > 0) {
475
- content.push({ type: "text", text: part.text });
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
+ }
476
693
  } else if ("functionCall" in part) {
477
694
  content.push({
478
695
  type: "tool-call",
479
- toolCallType: "function",
480
696
  toolCallId: this.config.generateId(),
481
697
  toolName: part.functionCall.name,
482
- args: JSON.stringify(part.functionCall.args)
698
+ input: JSON.stringify(part.functionCall.args)
483
699
  });
484
700
  } else if ("inlineData" in part) {
485
701
  content.push({
@@ -489,14 +705,13 @@ var GoogleGenerativeAILanguageModel = class {
489
705
  });
490
706
  }
491
707
  }
492
- const sources = (_b = extractSources({
708
+ const sources = (_d = extractSources({
493
709
  groundingMetadata: candidate.groundingMetadata,
494
710
  generateId: this.config.generateId
495
- })) != null ? _b : [];
711
+ })) != null ? _d : [];
496
712
  for (const source of sources) {
497
713
  content.push(source);
498
714
  }
499
- const usageMetadata = response.usageMetadata;
500
715
  return {
501
716
  content,
502
717
  finishReason: mapGoogleGenerativeAIFinishReason({
@@ -504,14 +719,19 @@ var GoogleGenerativeAILanguageModel = class {
504
719
  hasToolCalls: content.some((part) => part.type === "tool-call")
505
720
  }),
506
721
  usage: {
507
- inputTokens: (_c = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _c : void 0,
508
- outputTokens: (_d = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _d : 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
509
727
  },
510
728
  warnings,
511
729
  providerMetadata: {
512
730
  google: {
513
- groundingMetadata: (_e = candidate.groundingMetadata) != null ? _e : null,
514
- safetyRatings: (_f = candidate.safetyRatings) != null ? _f : 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
515
735
  }
516
736
  },
517
737
  request: { body },
@@ -543,11 +763,17 @@ var GoogleGenerativeAILanguageModel = class {
543
763
  let finishReason = "unknown";
544
764
  const usage = {
545
765
  inputTokens: void 0,
546
- outputTokens: void 0
766
+ outputTokens: void 0,
767
+ totalTokens: void 0
547
768
  };
548
769
  let providerMetadata = void 0;
549
- const generateId = this.config.generateId;
770
+ const generateId2 = this.config.generateId;
550
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;
551
777
  return {
552
778
  stream: response.pipeThrough(
553
779
  new TransformStream({
@@ -555,7 +781,10 @@ var GoogleGenerativeAILanguageModel = class {
555
781
  controller.enqueue({ type: "stream-start", warnings });
556
782
  },
557
783
  transform(chunk, controller) {
558
- 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
+ }
559
788
  if (!chunk.success) {
560
789
  controller.enqueue({ type: "error", error: chunk.error });
561
790
  return;
@@ -565,16 +794,99 @@ var GoogleGenerativeAILanguageModel = class {
565
794
  if (usageMetadata != null) {
566
795
  usage.inputTokens = (_a = usageMetadata.promptTokenCount) != null ? _a : void 0;
567
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;
568
800
  }
569
- const candidate = (_c = value.candidates) == null ? void 0 : _c[0];
801
+ const candidate = (_f = value.candidates) == null ? void 0 : _f[0];
570
802
  if (candidate == null) {
571
803
  return;
572
804
  }
573
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
+ }
574
818
  if (content != null) {
575
- const deltaText = getTextFromParts(content.parts);
576
- if (deltaText != null) {
577
- 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
+ }
578
890
  }
579
891
  const inlineDataParts = getInlineDataParts(content.parts);
580
892
  if (inlineDataParts != null) {
@@ -588,23 +900,29 @@ var GoogleGenerativeAILanguageModel = class {
588
900
  }
589
901
  const toolCallDeltas = getToolCallsFromParts({
590
902
  parts: content.parts,
591
- generateId
903
+ generateId: generateId2
592
904
  });
593
905
  if (toolCallDeltas != null) {
594
906
  for (const toolCall of toolCallDeltas) {
595
907
  controller.enqueue({
596
- type: "tool-call-delta",
597
- toolCallType: "function",
598
- toolCallId: toolCall.toolCallId,
599
- toolName: toolCall.toolName,
600
- 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
601
920
  });
602
921
  controller.enqueue({
603
922
  type: "tool-call",
604
- toolCallType: "function",
605
923
  toolCallId: toolCall.toolCallId,
606
924
  toolName: toolCall.toolName,
607
- args: toolCall.args
925
+ input: toolCall.args
608
926
  });
609
927
  hasToolCalls = true;
610
928
  }
@@ -615,22 +933,31 @@ var GoogleGenerativeAILanguageModel = class {
615
933
  finishReason: candidate.finishReason,
616
934
  hasToolCalls
617
935
  });
618
- const sources = (_d = extractSources({
619
- groundingMetadata: candidate.groundingMetadata,
620
- generateId
621
- })) != null ? _d : [];
622
- for (const source of sources) {
623
- controller.enqueue(source);
624
- }
625
936
  providerMetadata = {
626
937
  google: {
627
- groundingMetadata: (_e = candidate.groundingMetadata) != null ? _e : null,
628
- 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
629
941
  }
630
942
  };
943
+ if (usageMetadata != null) {
944
+ providerMetadata.google.usageMetadata = usageMetadata;
945
+ }
631
946
  }
632
947
  },
633
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
+ }
634
961
  controller.enqueue({
635
962
  type: "finish",
636
963
  finishReason,
@@ -647,26 +974,18 @@ var GoogleGenerativeAILanguageModel = class {
647
974
  };
648
975
  function getToolCallsFromParts({
649
976
  parts,
650
- generateId
977
+ generateId: generateId2
651
978
  }) {
652
979
  const functionCallParts = parts == null ? void 0 : parts.filter(
653
980
  (part) => "functionCall" in part
654
981
  );
655
982
  return functionCallParts == null || functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
656
983
  type: "tool-call",
657
- toolCallType: "function",
658
- toolCallId: generateId(),
984
+ toolCallId: generateId2(),
659
985
  toolName: part.functionCall.name,
660
986
  args: JSON.stringify(part.functionCall.args)
661
987
  }));
662
988
  }
663
- function getTextFromParts(parts) {
664
- const textParts = parts == null ? void 0 : parts.filter((part) => "text" in part);
665
- return textParts == null || textParts.length === 0 ? void 0 : {
666
- type: "text",
667
- text: textParts.map((part) => part.text).join("")
668
- };
669
- }
670
989
  function getInlineDataParts(parts) {
671
990
  return parts == null ? void 0 : parts.filter(
672
991
  (part) => "inlineData" in part
@@ -674,7 +993,7 @@ function getInlineDataParts(parts) {
674
993
  }
675
994
  function extractSources({
676
995
  groundingMetadata,
677
- generateId
996
+ generateId: generateId2
678
997
  }) {
679
998
  var _a;
680
999
  return (_a = groundingMetadata == null ? void 0 : groundingMetadata.groundingChunks) == null ? void 0 : _a.filter(
@@ -682,107 +1001,125 @@ function extractSources({
682
1001
  ).map((chunk) => ({
683
1002
  type: "source",
684
1003
  sourceType: "url",
685
- id: generateId(),
1004
+ id: generateId2(),
686
1005
  url: chunk.web.uri,
687
1006
  title: chunk.web.title
688
1007
  }));
689
1008
  }
690
- var contentSchema = z2.object({
691
- role: z2.string(),
692
- parts: z2.array(
693
- z2.union([
694
- z2.object({
695
- text: z2.string()
696
- }),
697
- z2.object({
698
- functionCall: z2.object({
699
- name: z2.string(),
700
- 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()
701
1017
  })
702
1018
  }),
703
- z2.object({
704
- inlineData: z2.object({
705
- mimeType: z2.string(),
706
- data: z2.string()
1019
+ z5.object({
1020
+ inlineData: z5.object({
1021
+ mimeType: z5.string(),
1022
+ data: z5.string()
707
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()
708
1036
  })
709
1037
  ])
710
1038
  ).nullish()
711
1039
  });
712
- var groundingChunkSchema = z2.object({
713
- web: z2.object({ uri: z2.string(), title: z2.string() }).nullish(),
714
- 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()
715
1047
  });
716
- var groundingMetadataSchema = z2.object({
717
- webSearchQueries: z2.array(z2.string()).nullish(),
718
- retrievalQueries: z2.array(z2.string()).nullish(),
719
- searchEntryPoint: z2.object({ renderedContent: z2.string() }).nullish(),
720
- groundingChunks: z2.array(groundingChunkSchema).nullish(),
721
- groundingSupports: z2.array(
722
- z2.object({
723
- segment: z2.object({
724
- startIndex: z2.number().nullish(),
725
- endIndex: z2.number().nullish(),
726
- text: z2.string().nullish()
727
- }),
728
- segment_text: z2.string().nullish(),
729
- groundingChunkIndices: z2.array(z2.number()).nullish(),
730
- supportChunkIndices: z2.array(z2.number()).nullish(),
731
- confidenceScores: z2.array(z2.number()).nullish(),
732
- confidenceScore: z2.array(z2.number()).nullish()
733
- })
734
- ).nullish(),
735
- retrievalMetadata: z2.union([
736
- z2.object({
737
- webDynamicRetrievalScore: z2.number()
738
- }),
739
- z2.object({})
740
- ]).nullish()
741
- });
742
- var safetyRatingSchema = z2.object({
743
- category: z2.string(),
744
- probability: z2.string(),
745
- probabilityScore: z2.number().nullish(),
746
- severity: z2.string().nullish(),
747
- severityScore: z2.number().nullish(),
748
- blocked: z2.boolean().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()
749
1054
  });
750
- var responseSchema = z2.object({
751
- candidates: z2.array(
752
- z2.object({
753
- content: contentSchema.nullish().or(z2.object({}).strict()),
754
- finishReason: z2.string().nullish(),
755
- safetyRatings: z2.array(safetyRatingSchema).nullish(),
756
- 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()
757
1063
  })
758
1064
  ),
759
- usageMetadata: z2.object({
760
- promptTokenCount: z2.number().nullish(),
761
- candidatesTokenCount: z2.number().nullish(),
762
- totalTokenCount: z2.number().nullish()
763
- }).nullish()
1065
+ usageMetadata: usageSchema.nullish()
764
1066
  });
765
- var chunkSchema = z2.object({
766
- candidates: z2.array(
767
- z2.object({
1067
+ var chunkSchema = z5.object({
1068
+ candidates: z5.array(
1069
+ z5.object({
768
1070
  content: contentSchema.nullish(),
769
- finishReason: z2.string().nullish(),
770
- safetyRatings: z2.array(safetyRatingSchema).nullish(),
771
- groundingMetadata: groundingMetadataSchema.nullish()
1071
+ finishReason: z5.string().nullish(),
1072
+ safetyRatings: z5.array(safetyRatingSchema).nullish(),
1073
+ groundingMetadata: groundingMetadataSchema.nullish(),
1074
+ urlContextMetadata: urlContextMetadataSchema.nullish()
772
1075
  })
773
1076
  ).nullish(),
774
- usageMetadata: z2.object({
775
- promptTokenCount: z2.number().nullish(),
776
- candidatesTokenCount: z2.number().nullish(),
777
- totalTokenCount: z2.number().nullish()
778
- }).nullish()
1077
+ usageMetadata: usageSchema.nullish()
779
1078
  });
780
- var googleGenerativeAIProviderOptionsSchema = z2.object({
781
- 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
+ })
782
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
+ };
783
1120
  export {
784
1121
  GoogleGenerativeAILanguageModel,
785
- groundingMetadataSchema,
1122
+ googleTools,
786
1123
  safetyRatingSchema
787
1124
  };
788
1125
  //# sourceMappingURL=index.mjs.map