@ai-sdk/google 2.0.0-canary.2 → 2.0.0-canary.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -15,10 +15,11 @@ import {
15
15
  import {
16
16
  combineHeaders,
17
17
  createJsonResponseHandler,
18
+ parseProviderOptions,
18
19
  postJsonToApi,
19
20
  resolve
20
21
  } from "@ai-sdk/provider-utils";
21
- import { z as z2 } from "zod";
22
+ import { z as z3 } from "zod";
22
23
 
23
24
  // src/google-error.ts
24
25
  import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
@@ -35,28 +36,61 @@ var googleFailedResponseHandler = createJsonErrorResponseHandler({
35
36
  errorToMessage: (data) => data.error.message
36
37
  });
37
38
 
39
+ // src/google-generative-ai-embedding-options.ts
40
+ import { z as z2 } from "zod";
41
+ var googleGenerativeAIEmbeddingProviderOptions = z2.object({
42
+ /**
43
+ * Optional. Optional reduced dimension for the output embedding.
44
+ * If set, excessive values in the output embedding are truncated from the end.
45
+ */
46
+ outputDimensionality: z2.number().optional(),
47
+ /**
48
+ * Optional. Specifies the task type for generating embeddings.
49
+ * Supported task types:
50
+ * - SEMANTIC_SIMILARITY: Optimized for text similarity.
51
+ * - CLASSIFICATION: Optimized for text classification.
52
+ * - CLUSTERING: Optimized for clustering texts based on similarity.
53
+ * - RETRIEVAL_DOCUMENT: Optimized for document retrieval.
54
+ * - RETRIEVAL_QUERY: Optimized for query-based retrieval.
55
+ * - QUESTION_ANSWERING: Optimized for answering questions.
56
+ * - FACT_VERIFICATION: Optimized for verifying factual information.
57
+ * - CODE_RETRIEVAL_QUERY: Optimized for retrieving code blocks based on natural language queries.
58
+ */
59
+ taskType: z2.enum([
60
+ "SEMANTIC_SIMILARITY",
61
+ "CLASSIFICATION",
62
+ "CLUSTERING",
63
+ "RETRIEVAL_DOCUMENT",
64
+ "RETRIEVAL_QUERY",
65
+ "QUESTION_ANSWERING",
66
+ "FACT_VERIFICATION",
67
+ "CODE_RETRIEVAL_QUERY"
68
+ ]).optional()
69
+ });
70
+
38
71
  // src/google-generative-ai-embedding-model.ts
39
72
  var GoogleGenerativeAIEmbeddingModel = class {
40
- constructor(modelId, settings, config) {
41
- this.specificationVersion = "v1";
73
+ constructor(modelId, config) {
74
+ this.specificationVersion = "v2";
75
+ this.maxEmbeddingsPerCall = 2048;
76
+ this.supportsParallelCalls = true;
42
77
  this.modelId = modelId;
43
- this.settings = settings;
44
78
  this.config = config;
45
79
  }
46
80
  get provider() {
47
81
  return this.config.provider;
48
82
  }
49
- get maxEmbeddingsPerCall() {
50
- return 2048;
51
- }
52
- get supportsParallelCalls() {
53
- return true;
54
- }
55
83
  async doEmbed({
56
84
  values,
57
85
  headers,
58
- abortSignal
86
+ abortSignal,
87
+ providerOptions
59
88
  }) {
89
+ const googleOptions = await parseProviderOptions({
90
+ provider: "google",
91
+ providerOptions,
92
+ schema: googleGenerativeAIEmbeddingProviderOptions
93
+ });
60
94
  if (values.length > this.maxEmbeddingsPerCall) {
61
95
  throw new TooManyEmbeddingValuesForCallError({
62
96
  provider: this.provider,
@@ -69,14 +103,19 @@ var GoogleGenerativeAIEmbeddingModel = class {
69
103
  await resolve(this.config.headers),
70
104
  headers
71
105
  );
72
- const { responseHeaders, value: response } = await postJsonToApi({
106
+ const {
107
+ responseHeaders,
108
+ value: response,
109
+ rawValue
110
+ } = await postJsonToApi({
73
111
  url: `${this.config.baseURL}/models/${this.modelId}:batchEmbedContents`,
74
112
  headers: mergedHeaders,
75
113
  body: {
76
114
  requests: values.map((value) => ({
77
115
  model: `models/${this.modelId}`,
78
116
  content: { role: "user", parts: [{ text: value }] },
79
- outputDimensionality: this.settings.outputDimensionality
117
+ outputDimensionality: googleOptions == null ? void 0 : googleOptions.outputDimensionality,
118
+ taskType: googleOptions == null ? void 0 : googleOptions.taskType
80
119
  }))
81
120
  },
82
121
  failedResponseHandler: googleFailedResponseHandler,
@@ -89,12 +128,12 @@ var GoogleGenerativeAIEmbeddingModel = class {
89
128
  return {
90
129
  embeddings: response.embeddings.map((item) => item.values),
91
130
  usage: void 0,
92
- rawResponse: { headers: responseHeaders }
131
+ response: { headers: responseHeaders, body: rawValue }
93
132
  };
94
133
  }
95
134
  };
96
- var googleGenerativeAITextEmbeddingResponseSchema = z2.object({
97
- embeddings: z2.array(z2.object({ values: z2.array(z2.number()) }))
135
+ var googleGenerativeAITextEmbeddingResponseSchema = z3.object({
136
+ embeddings: z3.array(z3.object({ values: z3.array(z3.number()) }))
98
137
  });
99
138
 
100
139
  // src/google-generative-ai-language-model.ts
@@ -102,15 +141,15 @@ import {
102
141
  combineHeaders as combineHeaders2,
103
142
  createEventSourceResponseHandler,
104
143
  createJsonResponseHandler as createJsonResponseHandler2,
105
- parseProviderOptions,
144
+ parseProviderOptions as parseProviderOptions2,
106
145
  postJsonToApi as postJsonToApi2,
107
146
  resolve as resolve2
108
147
  } from "@ai-sdk/provider-utils";
109
- import { z as z3 } from "zod";
148
+ import { z as z5 } from "zod";
110
149
 
111
150
  // src/convert-json-schema-to-openapi-schema.ts
112
151
  function convertJSONSchemaToOpenAPISchema(jsonSchema) {
113
- if (isEmptyObjectSchema(jsonSchema)) {
152
+ if (jsonSchema == null || isEmptyObjectSchema(jsonSchema)) {
114
153
  return void 0;
115
154
  }
116
155
  if (typeof jsonSchema === "boolean") {
@@ -209,9 +248,10 @@ function isEmptyObjectSchema(jsonSchema) {
209
248
  import {
210
249
  UnsupportedFunctionalityError
211
250
  } from "@ai-sdk/provider";
212
- import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
251
+ import {
252
+ convertToBase64
253
+ } from "@ai-sdk/provider-utils";
213
254
  function convertToGoogleGenerativeAIMessages(prompt) {
214
- var _a, _b;
215
255
  const systemInstructionParts = [];
216
256
  const contents = [];
217
257
  let systemMessagesAllowed = true;
@@ -235,33 +275,18 @@ function convertToGoogleGenerativeAIMessages(prompt) {
235
275
  parts.push({ text: part.text });
236
276
  break;
237
277
  }
238
- case "image": {
239
- parts.push(
240
- part.image instanceof URL ? {
241
- fileData: {
242
- mimeType: (_a = part.mimeType) != null ? _a : "image/jpeg",
243
- fileUri: part.image.toString()
244
- }
245
- } : {
246
- inlineData: {
247
- mimeType: (_b = part.mimeType) != null ? _b : "image/jpeg",
248
- data: convertUint8ArrayToBase64(part.image)
249
- }
250
- }
251
- );
252
- break;
253
- }
254
278
  case "file": {
279
+ const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
255
280
  parts.push(
256
281
  part.data instanceof URL ? {
257
282
  fileData: {
258
- mimeType: part.mimeType,
283
+ mimeType: mediaType,
259
284
  fileUri: part.data.toString()
260
285
  }
261
286
  } : {
262
287
  inlineData: {
263
- mimeType: part.mimeType,
264
- data: part.data
288
+ mimeType: mediaType,
289
+ data: convertToBase64(part.data)
265
290
  }
266
291
  }
267
292
  );
@@ -282,7 +307,7 @@ function convertToGoogleGenerativeAIMessages(prompt) {
282
307
  return part.text.length === 0 ? void 0 : { text: part.text };
283
308
  }
284
309
  case "file": {
285
- if (part.mimeType !== "image/png") {
310
+ if (part.mediaType !== "image/png") {
286
311
  throw new UnsupportedFunctionalityError({
287
312
  functionality: "Only PNG images are supported in assistant messages"
288
313
  });
@@ -294,8 +319,8 @@ function convertToGoogleGenerativeAIMessages(prompt) {
294
319
  }
295
320
  return {
296
321
  inlineData: {
297
- mimeType: part.mimeType,
298
- data: part.data
322
+ mimeType: part.mediaType,
323
+ data: convertToBase64(part.data)
299
324
  }
300
325
  };
301
326
  }
@@ -341,20 +366,112 @@ function getModelPath(modelId) {
341
366
  return modelId.includes("/") ? modelId : `models/${modelId}`;
342
367
  }
343
368
 
369
+ // src/google-generative-ai-options.ts
370
+ import { z as z4 } from "zod";
371
+ var dynamicRetrievalConfig = z4.object({
372
+ /**
373
+ * The mode of the predictor to be used in dynamic retrieval.
374
+ */
375
+ mode: z4.enum(["MODE_UNSPECIFIED", "MODE_DYNAMIC"]).optional(),
376
+ /**
377
+ * The threshold to be used in dynamic retrieval. If not set, a system default
378
+ * value is used.
379
+ */
380
+ dynamicThreshold: z4.number().optional()
381
+ });
382
+ var googleGenerativeAIProviderOptions = z4.object({
383
+ responseModalities: z4.array(z4.enum(["TEXT", "IMAGE"])).optional(),
384
+ thinkingConfig: z4.object({
385
+ thinkingBudget: z4.number().optional()
386
+ }).optional(),
387
+ /**
388
+ Optional.
389
+ The name of the cached content used as context to serve the prediction.
390
+ Format: cachedContents/{cachedContent}
391
+ */
392
+ cachedContent: z4.string().optional(),
393
+ /**
394
+ * Optional. Enable structured output. Default is true.
395
+ *
396
+ * This is useful when the JSON Schema contains elements that are
397
+ * not supported by the OpenAPI schema version that
398
+ * Google Generative AI uses. You can use this to disable
399
+ * structured outputs if you need to.
400
+ */
401
+ structuredOutputs: z4.boolean().optional(),
402
+ /**
403
+ Optional. A list of unique safety settings for blocking unsafe content.
404
+ */
405
+ safetySettings: z4.array(
406
+ z4.object({
407
+ category: z4.enum([
408
+ "HARM_CATEGORY_UNSPECIFIED",
409
+ "HARM_CATEGORY_HATE_SPEECH",
410
+ "HARM_CATEGORY_DANGEROUS_CONTENT",
411
+ "HARM_CATEGORY_HARASSMENT",
412
+ "HARM_CATEGORY_SEXUALLY_EXPLICIT",
413
+ "HARM_CATEGORY_CIVIC_INTEGRITY"
414
+ ]),
415
+ threshold: z4.enum([
416
+ "HARM_BLOCK_THRESHOLD_UNSPECIFIED",
417
+ "BLOCK_LOW_AND_ABOVE",
418
+ "BLOCK_MEDIUM_AND_ABOVE",
419
+ "BLOCK_ONLY_HIGH",
420
+ "BLOCK_NONE",
421
+ "OFF"
422
+ ])
423
+ })
424
+ ).optional(),
425
+ threshold: z4.enum([
426
+ "HARM_BLOCK_THRESHOLD_UNSPECIFIED",
427
+ "BLOCK_LOW_AND_ABOVE",
428
+ "BLOCK_MEDIUM_AND_ABOVE",
429
+ "BLOCK_ONLY_HIGH",
430
+ "BLOCK_NONE",
431
+ "OFF"
432
+ ]).optional(),
433
+ /**
434
+ * Optional. Enables timestamp understanding for audio-only files.
435
+ *
436
+ * https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/audio-understanding
437
+ */
438
+ audioTimestamp: z4.boolean().optional(),
439
+ /**
440
+ Optional. When enabled, the model will use Google search to ground the response.
441
+
442
+ @see https://cloud.google.com/vertex-ai/generative-ai/docs/grounding/overview
443
+ */
444
+ useSearchGrounding: z4.boolean().optional(),
445
+ /**
446
+ Optional. Specifies the dynamic retrieval configuration.
447
+
448
+ @note Dynamic retrieval is only compatible with Gemini 1.5 Flash.
449
+
450
+ @see https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/ground-with-google-search#dynamic-retrieval
451
+ */
452
+ dynamicRetrievalConfig: dynamicRetrievalConfig.optional()
453
+ });
454
+
344
455
  // src/google-prepare-tools.ts
345
456
  import {
346
457
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
347
458
  } from "@ai-sdk/provider";
348
- function prepareTools(mode, useSearchGrounding, dynamicRetrievalConfig, modelId) {
349
- var _a, _b;
350
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
459
+ function prepareTools({
460
+ tools,
461
+ toolChoice,
462
+ useSearchGrounding,
463
+ dynamicRetrievalConfig: dynamicRetrievalConfig2,
464
+ modelId
465
+ }) {
466
+ var _a;
467
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
351
468
  const toolWarnings = [];
352
469
  const isGemini2 = modelId.includes("gemini-2");
353
470
  const supportsDynamicRetrieval = modelId.includes("gemini-1.5-flash") && !modelId.includes("-8b");
354
471
  if (useSearchGrounding) {
355
472
  return {
356
473
  tools: isGemini2 ? { googleSearch: {} } : {
357
- googleSearchRetrieval: !supportsDynamicRetrieval || !dynamicRetrievalConfig ? {} : { dynamicRetrievalConfig }
474
+ googleSearchRetrieval: !supportsDynamicRetrieval || !dynamicRetrievalConfig2 ? {} : { dynamicRetrievalConfig: dynamicRetrievalConfig2 }
358
475
  },
359
476
  toolConfig: void 0,
360
477
  toolWarnings
@@ -370,12 +487,11 @@ function prepareTools(mode, useSearchGrounding, dynamicRetrievalConfig, modelId)
370
487
  } else {
371
488
  functionDeclarations.push({
372
489
  name: tool.name,
373
- description: (_b = tool.description) != null ? _b : "",
490
+ description: (_a = tool.description) != null ? _a : "",
374
491
  parameters: convertJSONSchemaToOpenAPISchema(tool.parameters)
375
492
  });
376
493
  }
377
494
  }
378
- const toolChoice = mode.toolChoice;
379
495
  if (toolChoice == null) {
380
496
  return {
381
497
  tools: { functionDeclarations },
@@ -417,7 +533,7 @@ function prepareTools(mode, useSearchGrounding, dynamicRetrievalConfig, modelId)
417
533
  default: {
418
534
  const _exhaustiveCheck = type;
419
535
  throw new UnsupportedFunctionalityError2({
420
- functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
536
+ functionality: `tool choice type: ${_exhaustiveCheck}`
421
537
  });
422
538
  }
423
539
  }
@@ -452,25 +568,21 @@ function mapGoogleGenerativeAIFinishReason({
452
568
 
453
569
  // src/google-generative-ai-language-model.ts
454
570
  var GoogleGenerativeAILanguageModel = class {
455
- constructor(modelId, settings, config) {
571
+ constructor(modelId, config) {
456
572
  this.specificationVersion = "v2";
457
- this.defaultObjectGenerationMode = "json";
458
- this.supportsImageUrls = false;
459
573
  this.modelId = modelId;
460
- this.settings = settings;
461
574
  this.config = config;
462
575
  }
463
- get supportsStructuredOutputs() {
464
- var _a;
465
- return (_a = this.settings.structuredOutputs) != null ? _a : true;
466
- }
467
576
  get provider() {
468
577
  return this.config.provider;
469
578
  }
579
+ get supportedUrls() {
580
+ var _a, _b, _c;
581
+ return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
582
+ }
470
583
  async getArgs({
471
- mode,
472
584
  prompt,
473
- maxTokens,
585
+ maxOutputTokens,
474
586
  temperature,
475
587
  topP,
476
588
  topK,
@@ -479,111 +591,66 @@ var GoogleGenerativeAILanguageModel = class {
479
591
  stopSequences,
480
592
  responseFormat,
481
593
  seed,
482
- providerMetadata
594
+ tools,
595
+ toolChoice,
596
+ providerOptions
483
597
  }) {
484
598
  var _a, _b;
485
- const type = mode.type;
486
599
  const warnings = [];
487
- const googleOptions = parseProviderOptions({
600
+ const googleOptions = await parseProviderOptions2({
488
601
  provider: "google",
489
- providerOptions: providerMetadata,
490
- schema: googleGenerativeAIProviderOptionsSchema
602
+ providerOptions,
603
+ schema: googleGenerativeAIProviderOptions
491
604
  });
492
- const generationConfig = {
493
- // standardized settings:
494
- maxOutputTokens: maxTokens,
495
- temperature,
496
- topK,
497
- topP,
498
- frequencyPenalty,
499
- presencePenalty,
500
- stopSequences,
501
- seed,
502
- // response format:
503
- responseMimeType: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? "application/json" : void 0,
504
- responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
505
- // so this is needed as an escape hatch:
506
- this.supportsStructuredOutputs ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
507
- ...this.settings.audioTimestamp && {
508
- audioTimestamp: this.settings.audioTimestamp
509
- },
510
- // provider options:
511
- responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities
512
- };
513
605
  const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(prompt);
514
- switch (type) {
515
- case "regular": {
516
- const { tools, toolConfig, toolWarnings } = prepareTools(
517
- mode,
518
- (_a = this.settings.useSearchGrounding) != null ? _a : false,
519
- this.settings.dynamicRetrievalConfig,
520
- this.modelId
521
- );
522
- return {
523
- args: {
524
- generationConfig,
525
- contents,
526
- systemInstruction,
527
- safetySettings: this.settings.safetySettings,
528
- tools,
529
- toolConfig,
530
- cachedContent: this.settings.cachedContent
531
- },
532
- warnings: [...warnings, ...toolWarnings]
533
- };
534
- }
535
- case "object-json": {
536
- return {
537
- args: {
538
- generationConfig: {
539
- ...generationConfig,
540
- responseMimeType: "application/json",
541
- responseSchema: mode.schema != null && // Google GenAI does not support all OpenAPI Schema features,
542
- // so this is needed as an escape hatch:
543
- this.supportsStructuredOutputs ? convertJSONSchemaToOpenAPISchema(mode.schema) : void 0
544
- },
545
- contents,
546
- systemInstruction,
547
- safetySettings: this.settings.safetySettings,
548
- cachedContent: this.settings.cachedContent
549
- },
550
- warnings
551
- };
552
- }
553
- case "object-tool": {
554
- return {
555
- args: {
556
- generationConfig,
557
- contents,
558
- tools: {
559
- functionDeclarations: [
560
- {
561
- name: mode.tool.name,
562
- description: (_b = mode.tool.description) != null ? _b : "",
563
- parameters: convertJSONSchemaToOpenAPISchema(
564
- mode.tool.parameters
565
- )
566
- }
567
- ]
568
- },
569
- toolConfig: { functionCallingConfig: { mode: "ANY" } },
570
- safetySettings: this.settings.safetySettings,
571
- cachedContent: this.settings.cachedContent
606
+ const {
607
+ tools: googleTools,
608
+ toolConfig: googleToolConfig,
609
+ toolWarnings
610
+ } = prepareTools({
611
+ tools,
612
+ toolChoice,
613
+ useSearchGrounding: (_a = googleOptions == null ? void 0 : googleOptions.useSearchGrounding) != null ? _a : false,
614
+ dynamicRetrievalConfig: googleOptions == null ? void 0 : googleOptions.dynamicRetrievalConfig,
615
+ modelId: this.modelId
616
+ });
617
+ return {
618
+ args: {
619
+ generationConfig: {
620
+ // standardized settings:
621
+ maxOutputTokens,
622
+ temperature,
623
+ topK,
624
+ topP,
625
+ frequencyPenalty,
626
+ presencePenalty,
627
+ stopSequences,
628
+ seed,
629
+ // response format:
630
+ responseMimeType: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? "application/json" : void 0,
631
+ responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
632
+ // so this is needed as an escape hatch:
633
+ // TODO convert into provider option
634
+ ((_b = googleOptions == null ? void 0 : googleOptions.structuredOutputs) != null ? _b : true) ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
635
+ ...(googleOptions == null ? void 0 : googleOptions.audioTimestamp) && {
636
+ audioTimestamp: googleOptions.audioTimestamp
572
637
  },
573
- warnings
574
- };
575
- }
576
- default: {
577
- const _exhaustiveCheck = type;
578
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
579
- }
580
- }
581
- }
582
- supportsUrl(url) {
583
- return this.config.isSupportedUrl(url);
638
+ // provider options:
639
+ responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities,
640
+ thinkingConfig: googleOptions == null ? void 0 : googleOptions.thinkingConfig
641
+ },
642
+ contents,
643
+ systemInstruction,
644
+ safetySettings: googleOptions == null ? void 0 : googleOptions.safetySettings,
645
+ tools: googleTools,
646
+ toolConfig: googleToolConfig,
647
+ cachedContent: googleOptions == null ? void 0 : googleOptions.cachedContent
648
+ },
649
+ warnings: [...warnings, ...toolWarnings]
650
+ };
584
651
  }
585
652
  async doGenerate(options) {
586
- var _a, _b, _c, _d, _e;
653
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
587
654
  const { args, warnings } = await this.getArgs(options);
588
655
  const body = JSON.stringify(args);
589
656
  const mergedHeaders = combineHeaders2(
@@ -605,43 +672,62 @@ var GoogleGenerativeAILanguageModel = class {
605
672
  abortSignal: options.abortSignal,
606
673
  fetch: this.config.fetch
607
674
  });
608
- const { contents: rawPrompt, ...rawSettings } = args;
609
675
  const candidate = response.candidates[0];
610
- const parts = candidate.content == null || typeof candidate.content !== "object" || !("parts" in candidate.content) ? [] : candidate.content.parts;
611
- const toolCalls = getToolCallsFromParts({
612
- parts,
676
+ const content = [];
677
+ const parts = candidate.content == null || typeof candidate.content !== "object" || !("parts" in candidate.content) ? [] : (_a = candidate.content.parts) != null ? _a : [];
678
+ for (const part of parts) {
679
+ if ("text" in part && part.text.length > 0) {
680
+ content.push({ type: "text", text: part.text });
681
+ } else if ("functionCall" in part) {
682
+ content.push({
683
+ type: "tool-call",
684
+ toolCallType: "function",
685
+ toolCallId: this.config.generateId(),
686
+ toolName: part.functionCall.name,
687
+ args: JSON.stringify(part.functionCall.args)
688
+ });
689
+ } else if ("inlineData" in part) {
690
+ content.push({
691
+ type: "file",
692
+ data: part.inlineData.data,
693
+ mediaType: part.inlineData.mimeType
694
+ });
695
+ }
696
+ }
697
+ const sources = (_b = extractSources({
698
+ groundingMetadata: candidate.groundingMetadata,
613
699
  generateId: this.config.generateId
614
- });
700
+ })) != null ? _b : [];
701
+ for (const source of sources) {
702
+ content.push(source);
703
+ }
615
704
  const usageMetadata = response.usageMetadata;
616
705
  return {
617
- text: getTextFromParts(parts),
618
- files: (_a = getInlineDataParts(parts)) == null ? void 0 : _a.map((part) => ({
619
- data: part.inlineData.data,
620
- mimeType: part.inlineData.mimeType
621
- })),
622
- toolCalls,
706
+ content,
623
707
  finishReason: mapGoogleGenerativeAIFinishReason({
624
708
  finishReason: candidate.finishReason,
625
- hasToolCalls: toolCalls != null && toolCalls.length > 0
709
+ hasToolCalls: content.some((part) => part.type === "tool-call")
626
710
  }),
627
711
  usage: {
628
- promptTokens: (_b = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _b : NaN,
629
- completionTokens: (_c = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _c : NaN
712
+ inputTokens: (_c = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _c : void 0,
713
+ outputTokens: (_d = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _d : void 0,
714
+ totalTokens: (_e = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _e : void 0,
715
+ reasoningTokens: (_f = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _f : void 0,
716
+ cachedInputTokens: (_g = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _g : void 0
630
717
  },
631
- rawCall: { rawPrompt, rawSettings },
632
- rawResponse: { headers: responseHeaders, body: rawResponse },
633
718
  warnings,
634
719
  providerMetadata: {
635
720
  google: {
636
- groundingMetadata: (_d = candidate.groundingMetadata) != null ? _d : null,
637
- safetyRatings: (_e = candidate.safetyRatings) != null ? _e : null
721
+ groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
722
+ safetyRatings: (_i = candidate.safetyRatings) != null ? _i : null
638
723
  }
639
724
  },
640
- sources: extractSources({
641
- groundingMetadata: candidate.groundingMetadata,
642
- generateId: this.config.generateId
643
- }),
644
- request: { body }
725
+ request: { body },
726
+ response: {
727
+ // TODO timestamp, model id, id
728
+ headers: responseHeaders,
729
+ body: rawResponse
730
+ }
645
731
  };
646
732
  }
647
733
  async doStream(options) {
@@ -662,11 +748,11 @@ var GoogleGenerativeAILanguageModel = class {
662
748
  abortSignal: options.abortSignal,
663
749
  fetch: this.config.fetch
664
750
  });
665
- const { contents: rawPrompt, ...rawSettings } = args;
666
751
  let finishReason = "unknown";
667
- let usage = {
668
- promptTokens: Number.NaN,
669
- completionTokens: Number.NaN
752
+ const usage = {
753
+ inputTokens: void 0,
754
+ outputTokens: void 0,
755
+ totalTokens: void 0
670
756
  };
671
757
  let providerMetadata = void 0;
672
758
  const generateId2 = this.config.generateId;
@@ -674,8 +760,11 @@ var GoogleGenerativeAILanguageModel = class {
674
760
  return {
675
761
  stream: response.pipeThrough(
676
762
  new TransformStream({
763
+ start(controller) {
764
+ controller.enqueue({ type: "stream-start", warnings });
765
+ },
677
766
  transform(chunk, controller) {
678
- var _a, _b, _c, _d, _e, _f;
767
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
679
768
  if (!chunk.success) {
680
769
  controller.enqueue({ type: "error", error: chunk.error });
681
770
  return;
@@ -683,12 +772,13 @@ var GoogleGenerativeAILanguageModel = class {
683
772
  const value = chunk.value;
684
773
  const usageMetadata = value.usageMetadata;
685
774
  if (usageMetadata != null) {
686
- usage = {
687
- promptTokens: (_a = usageMetadata.promptTokenCount) != null ? _a : NaN,
688
- completionTokens: (_b = usageMetadata.candidatesTokenCount) != null ? _b : NaN
689
- };
775
+ usage.inputTokens = (_a = usageMetadata.promptTokenCount) != null ? _a : void 0;
776
+ usage.outputTokens = (_b = usageMetadata.candidatesTokenCount) != null ? _b : void 0;
777
+ usage.totalTokens = (_c = usageMetadata.totalTokenCount) != null ? _c : void 0;
778
+ usage.reasoningTokens = (_d = usageMetadata.thoughtsTokenCount) != null ? _d : void 0;
779
+ usage.cachedInputTokens = (_e = usageMetadata.cachedContentTokenCount) != null ? _e : void 0;
690
780
  }
691
- const candidate = (_c = value.candidates) == null ? void 0 : _c[0];
781
+ const candidate = (_f = value.candidates) == null ? void 0 : _f[0];
692
782
  if (candidate == null) {
693
783
  return;
694
784
  }
@@ -696,17 +786,14 @@ var GoogleGenerativeAILanguageModel = class {
696
786
  if (content != null) {
697
787
  const deltaText = getTextFromParts(content.parts);
698
788
  if (deltaText != null) {
699
- controller.enqueue({
700
- type: "text-delta",
701
- textDelta: deltaText
702
- });
789
+ controller.enqueue(deltaText);
703
790
  }
704
791
  const inlineDataParts = getInlineDataParts(content.parts);
705
792
  if (inlineDataParts != null) {
706
793
  for (const part of inlineDataParts) {
707
794
  controller.enqueue({
708
795
  type: "file",
709
- mimeType: part.inlineData.mimeType,
796
+ mediaType: part.inlineData.mimeType,
710
797
  data: part.inlineData.data
711
798
  });
712
799
  }
@@ -740,17 +827,17 @@ var GoogleGenerativeAILanguageModel = class {
740
827
  finishReason: candidate.finishReason,
741
828
  hasToolCalls
742
829
  });
743
- const sources = (_d = extractSources({
830
+ const sources = (_g = extractSources({
744
831
  groundingMetadata: candidate.groundingMetadata,
745
832
  generateId: generateId2
746
- })) != null ? _d : [];
833
+ })) != null ? _g : [];
747
834
  for (const source of sources) {
748
- controller.enqueue({ type: "source", source });
835
+ controller.enqueue(source);
749
836
  }
750
837
  providerMetadata = {
751
838
  google: {
752
- groundingMetadata: (_e = candidate.groundingMetadata) != null ? _e : null,
753
- safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null
839
+ groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
840
+ safetyRatings: (_i = candidate.safetyRatings) != null ? _i : null
754
841
  }
755
842
  };
756
843
  }
@@ -765,9 +852,7 @@ var GoogleGenerativeAILanguageModel = class {
765
852
  }
766
853
  })
767
854
  ),
768
- rawCall: { rawPrompt, rawSettings },
769
- rawResponse: { headers: responseHeaders },
770
- warnings,
855
+ response: { headers: responseHeaders },
771
856
  request: { body }
772
857
  };
773
858
  }
@@ -780,6 +865,7 @@ function getToolCallsFromParts({
780
865
  (part) => "functionCall" in part
781
866
  );
782
867
  return functionCallParts == null || functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
868
+ type: "tool-call",
783
869
  toolCallType: "function",
784
870
  toolCallId: generateId2(),
785
871
  toolName: part.functionCall.name,
@@ -788,7 +874,10 @@ function getToolCallsFromParts({
788
874
  }
789
875
  function getTextFromParts(parts) {
790
876
  const textParts = parts == null ? void 0 : parts.filter((part) => "text" in part);
791
- return textParts == null || textParts.length === 0 ? void 0 : textParts.map((part) => part.text).join("");
877
+ return textParts == null || textParts.length === 0 ? void 0 : {
878
+ type: "text",
879
+ text: textParts.map((part) => part.text).join("")
880
+ };
792
881
  }
793
882
  function getInlineDataParts(parts) {
794
883
  return parts == null ? void 0 : parts.filter(
@@ -803,110 +892,102 @@ function extractSources({
803
892
  return (_a = groundingMetadata == null ? void 0 : groundingMetadata.groundingChunks) == null ? void 0 : _a.filter(
804
893
  (chunk) => chunk.web != null
805
894
  ).map((chunk) => ({
895
+ type: "source",
806
896
  sourceType: "url",
807
897
  id: generateId2(),
808
898
  url: chunk.web.uri,
809
899
  title: chunk.web.title
810
900
  }));
811
901
  }
812
- var contentSchema = z3.object({
813
- role: z3.string(),
814
- parts: z3.array(
815
- z3.union([
816
- z3.object({
817
- text: z3.string()
902
+ var contentSchema = z5.object({
903
+ role: z5.string(),
904
+ parts: z5.array(
905
+ z5.union([
906
+ z5.object({
907
+ text: z5.string()
818
908
  }),
819
- z3.object({
820
- functionCall: z3.object({
821
- name: z3.string(),
822
- args: z3.unknown()
909
+ z5.object({
910
+ functionCall: z5.object({
911
+ name: z5.string(),
912
+ args: z5.unknown()
823
913
  })
824
914
  }),
825
- z3.object({
826
- inlineData: z3.object({
827
- mimeType: z3.string(),
828
- data: z3.string()
915
+ z5.object({
916
+ inlineData: z5.object({
917
+ mimeType: z5.string(),
918
+ data: z5.string()
829
919
  })
830
920
  })
831
921
  ])
832
922
  ).nullish()
833
923
  });
834
- var groundingChunkSchema = z3.object({
835
- web: z3.object({ uri: z3.string(), title: z3.string() }).nullish(),
836
- retrievedContext: z3.object({ uri: z3.string(), title: z3.string() }).nullish()
924
+ var groundingChunkSchema = z5.object({
925
+ web: z5.object({ uri: z5.string(), title: z5.string() }).nullish(),
926
+ retrievedContext: z5.object({ uri: z5.string(), title: z5.string() }).nullish()
837
927
  });
838
- var groundingMetadataSchema = z3.object({
839
- webSearchQueries: z3.array(z3.string()).nullish(),
840
- retrievalQueries: z3.array(z3.string()).nullish(),
841
- searchEntryPoint: z3.object({ renderedContent: z3.string() }).nullish(),
842
- groundingChunks: z3.array(groundingChunkSchema).nullish(),
843
- groundingSupports: z3.array(
844
- z3.object({
845
- segment: z3.object({
846
- startIndex: z3.number().nullish(),
847
- endIndex: z3.number().nullish(),
848
- text: z3.string().nullish()
928
+ var groundingMetadataSchema = z5.object({
929
+ webSearchQueries: z5.array(z5.string()).nullish(),
930
+ retrievalQueries: z5.array(z5.string()).nullish(),
931
+ searchEntryPoint: z5.object({ renderedContent: z5.string() }).nullish(),
932
+ groundingChunks: z5.array(groundingChunkSchema).nullish(),
933
+ groundingSupports: z5.array(
934
+ z5.object({
935
+ segment: z5.object({
936
+ startIndex: z5.number().nullish(),
937
+ endIndex: z5.number().nullish(),
938
+ text: z5.string().nullish()
849
939
  }),
850
- segment_text: z3.string().nullish(),
851
- groundingChunkIndices: z3.array(z3.number()).nullish(),
852
- supportChunkIndices: z3.array(z3.number()).nullish(),
853
- confidenceScores: z3.array(z3.number()).nullish(),
854
- confidenceScore: z3.array(z3.number()).nullish()
940
+ segment_text: z5.string().nullish(),
941
+ groundingChunkIndices: z5.array(z5.number()).nullish(),
942
+ supportChunkIndices: z5.array(z5.number()).nullish(),
943
+ confidenceScores: z5.array(z5.number()).nullish(),
944
+ confidenceScore: z5.array(z5.number()).nullish()
855
945
  })
856
946
  ).nullish(),
857
- retrievalMetadata: z3.union([
858
- z3.object({
859
- webDynamicRetrievalScore: z3.number()
947
+ retrievalMetadata: z5.union([
948
+ z5.object({
949
+ webDynamicRetrievalScore: z5.number()
860
950
  }),
861
- z3.object({})
951
+ z5.object({})
862
952
  ]).nullish()
863
953
  });
864
- var safetyRatingSchema = z3.object({
865
- category: z3.string(),
866
- probability: z3.string(),
867
- probabilityScore: z3.number().nullish(),
868
- severity: z3.string().nullish(),
869
- severityScore: z3.number().nullish(),
870
- blocked: z3.boolean().nullish()
954
+ var safetyRatingSchema = z5.object({
955
+ category: z5.string().nullish(),
956
+ probability: z5.string().nullish(),
957
+ probabilityScore: z5.number().nullish(),
958
+ severity: z5.string().nullish(),
959
+ severityScore: z5.number().nullish(),
960
+ blocked: z5.boolean().nullish()
961
+ });
962
+ var usageSchema = z5.object({
963
+ cachedContentTokenCount: z5.number().nullish(),
964
+ thoughtsTokenCount: z5.number().nullish(),
965
+ promptTokenCount: z5.number().nullish(),
966
+ candidatesTokenCount: z5.number().nullish(),
967
+ totalTokenCount: z5.number().nullish()
871
968
  });
872
- var responseSchema = z3.object({
873
- candidates: z3.array(
874
- z3.object({
875
- content: contentSchema.nullish().or(z3.object({}).strict()),
876
- finishReason: z3.string().nullish(),
877
- safetyRatings: z3.array(safetyRatingSchema).nullish(),
969
+ var responseSchema = z5.object({
970
+ candidates: z5.array(
971
+ z5.object({
972
+ content: contentSchema.nullish().or(z5.object({}).strict()),
973
+ finishReason: z5.string().nullish(),
974
+ safetyRatings: z5.array(safetyRatingSchema).nullish(),
878
975
  groundingMetadata: groundingMetadataSchema.nullish()
879
976
  })
880
977
  ),
881
- usageMetadata: z3.object({
882
- promptTokenCount: z3.number().nullish(),
883
- candidatesTokenCount: z3.number().nullish(),
884
- totalTokenCount: z3.number().nullish()
885
- }).nullish()
978
+ usageMetadata: usageSchema.nullish()
886
979
  });
887
- var chunkSchema = z3.object({
888
- candidates: z3.array(
889
- z3.object({
980
+ var chunkSchema = z5.object({
981
+ candidates: z5.array(
982
+ z5.object({
890
983
  content: contentSchema.nullish(),
891
- finishReason: z3.string().nullish(),
892
- safetyRatings: z3.array(safetyRatingSchema).nullish(),
984
+ finishReason: z5.string().nullish(),
985
+ safetyRatings: z5.array(safetyRatingSchema).nullish(),
893
986
  groundingMetadata: groundingMetadataSchema.nullish()
894
987
  })
895
988
  ).nullish(),
896
- usageMetadata: z3.object({
897
- promptTokenCount: z3.number().nullish(),
898
- candidatesTokenCount: z3.number().nullish(),
899
- totalTokenCount: z3.number().nullish()
900
- }).nullish()
989
+ usageMetadata: usageSchema.nullish()
901
990
  });
902
- var googleGenerativeAIProviderOptionsSchema = z3.object({
903
- responseModalities: z3.array(z3.enum(["TEXT", "IMAGE"])).nullish()
904
- });
905
-
906
- // src/google-supported-file-url.ts
907
- function isSupportedFileUrl(url) {
908
- return url.toString().startsWith("https://generativelanguage.googleapis.com/v1beta/files/");
909
- }
910
991
 
911
992
  // src/google-provider.ts
912
993
  function createGoogleGenerativeAI(options = {}) {
@@ -920,30 +1001,35 @@ function createGoogleGenerativeAI(options = {}) {
920
1001
  }),
921
1002
  ...options.headers
922
1003
  });
923
- const createChatModel = (modelId, settings = {}) => {
1004
+ const createChatModel = (modelId) => {
924
1005
  var _a2;
925
- return new GoogleGenerativeAILanguageModel(modelId, settings, {
1006
+ return new GoogleGenerativeAILanguageModel(modelId, {
926
1007
  provider: "google.generative-ai",
927
1008
  baseURL,
928
1009
  headers: getHeaders,
929
1010
  generateId: (_a2 = options.generateId) != null ? _a2 : generateId,
930
- isSupportedUrl: isSupportedFileUrl,
1011
+ supportedUrls: () => ({
1012
+ "*": [
1013
+ // HTTP URLs:
1014
+ /^https?:\/\/.*$/
1015
+ ]
1016
+ }),
931
1017
  fetch: options.fetch
932
1018
  });
933
1019
  };
934
- const createEmbeddingModel = (modelId, settings = {}) => new GoogleGenerativeAIEmbeddingModel(modelId, settings, {
1020
+ const createEmbeddingModel = (modelId) => new GoogleGenerativeAIEmbeddingModel(modelId, {
935
1021
  provider: "google.generative-ai",
936
1022
  baseURL,
937
1023
  headers: getHeaders,
938
1024
  fetch: options.fetch
939
1025
  });
940
- const provider = function(modelId, settings) {
1026
+ const provider = function(modelId) {
941
1027
  if (new.target) {
942
1028
  throw new Error(
943
1029
  "The Google Generative AI model function cannot be called with the new keyword."
944
1030
  );
945
1031
  }
946
- return createChatModel(modelId, settings);
1032
+ return createChatModel(modelId);
947
1033
  };
948
1034
  provider.languageModel = createChatModel;
949
1035
  provider.chat = createChatModel;