@ai-sdk/google 3.0.50 → 3.0.52

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
@@ -7,7 +7,7 @@ import {
7
7
  } from "@ai-sdk/provider-utils";
8
8
 
9
9
  // src/version.ts
10
- var VERSION = true ? "3.0.50" : "0.0.0-test";
10
+ var VERSION = true ? "3.0.52" : "0.0.0-test";
11
11
 
12
12
  // src/google-generative-ai-embedding-model.ts
13
13
  import {
@@ -406,13 +406,118 @@ import {
406
406
  UnsupportedFunctionalityError
407
407
  } from "@ai-sdk/provider";
408
408
  import { convertToBase64 } from "@ai-sdk/provider-utils";
409
+ var dataUrlRegex = /^data:([^;,]+);base64,(.+)$/s;
410
+ function parseBase64DataUrl(value) {
411
+ const match = dataUrlRegex.exec(value);
412
+ if (match == null) {
413
+ return void 0;
414
+ }
415
+ return {
416
+ mediaType: match[1],
417
+ data: match[2]
418
+ };
419
+ }
420
+ function convertUrlToolResultPart(url) {
421
+ const parsedDataUrl = parseBase64DataUrl(url);
422
+ if (parsedDataUrl == null) {
423
+ return void 0;
424
+ }
425
+ return {
426
+ inlineData: {
427
+ mimeType: parsedDataUrl.mediaType,
428
+ data: parsedDataUrl.data
429
+ }
430
+ };
431
+ }
432
+ function appendToolResultParts(parts, toolName, outputValue) {
433
+ const functionResponseParts = [];
434
+ const responseTextParts = [];
435
+ for (const contentPart of outputValue) {
436
+ switch (contentPart.type) {
437
+ case "text": {
438
+ responseTextParts.push(contentPart.text);
439
+ break;
440
+ }
441
+ case "image-data":
442
+ case "file-data": {
443
+ functionResponseParts.push({
444
+ inlineData: {
445
+ mimeType: contentPart.mediaType,
446
+ data: contentPart.data
447
+ }
448
+ });
449
+ break;
450
+ }
451
+ case "image-url":
452
+ case "file-url": {
453
+ const functionResponsePart = convertUrlToolResultPart(
454
+ contentPart.url
455
+ );
456
+ if (functionResponsePart != null) {
457
+ functionResponseParts.push(functionResponsePart);
458
+ } else {
459
+ responseTextParts.push(JSON.stringify(contentPart));
460
+ }
461
+ break;
462
+ }
463
+ default: {
464
+ responseTextParts.push(JSON.stringify(contentPart));
465
+ break;
466
+ }
467
+ }
468
+ }
469
+ parts.push({
470
+ functionResponse: {
471
+ name: toolName,
472
+ response: {
473
+ name: toolName,
474
+ content: responseTextParts.length > 0 ? responseTextParts.join("\n") : "Tool executed successfully."
475
+ },
476
+ ...functionResponseParts.length > 0 ? { parts: functionResponseParts } : {}
477
+ }
478
+ });
479
+ }
480
+ function appendLegacyToolResultParts(parts, toolName, outputValue) {
481
+ for (const contentPart of outputValue) {
482
+ switch (contentPart.type) {
483
+ case "text":
484
+ parts.push({
485
+ functionResponse: {
486
+ name: toolName,
487
+ response: {
488
+ name: toolName,
489
+ content: contentPart.text
490
+ }
491
+ }
492
+ });
493
+ break;
494
+ case "image-data":
495
+ parts.push(
496
+ {
497
+ inlineData: {
498
+ mimeType: String(contentPart.mediaType),
499
+ data: String(contentPart.data)
500
+ }
501
+ },
502
+ {
503
+ text: "Tool executed successfully and returned this image as a response"
504
+ }
505
+ );
506
+ break;
507
+ default:
508
+ parts.push({ text: JSON.stringify(contentPart) });
509
+ break;
510
+ }
511
+ }
512
+ }
409
513
  function convertToGoogleGenerativeAIMessages(prompt, options) {
410
- var _a, _b, _c;
514
+ var _a, _b, _c, _d;
411
515
  const systemInstructionParts = [];
412
516
  const contents = [];
413
517
  let systemMessagesAllowed = true;
414
518
  const isGemmaModel = (_a = options == null ? void 0 : options.isGemmaModel) != null ? _a : false;
415
519
  const providerOptionsName = (_b = options == null ? void 0 : options.providerOptionsName) != null ? _b : "google";
520
+ const supportsFunctionResponseParts = (_c = options == null ? void 0 : options.supportsFunctionResponseParts) != null ? _c : true;
416
521
  for (const { role, content } of prompt) {
417
522
  switch (role) {
418
523
  case "system": {
@@ -460,8 +565,8 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
460
565
  contents.push({
461
566
  role: "model",
462
567
  parts: content.map((part) => {
463
- var _a2, _b2, _c2, _d;
464
- const providerOpts = (_d = (_a2 = part.providerOptions) == null ? void 0 : _a2[providerOptionsName]) != null ? _d : providerOptionsName !== "google" ? (_b2 = part.providerOptions) == null ? void 0 : _b2.google : (_c2 = part.providerOptions) == null ? void 0 : _c2.vertex;
568
+ var _a2, _b2, _c2, _d2;
569
+ const providerOpts = (_d2 = (_a2 = part.providerOptions) == null ? void 0 : _a2[providerOptionsName]) != null ? _d2 : providerOptionsName !== "google" ? (_b2 = part.providerOptions) == null ? void 0 : _b2.google : (_c2 = part.providerOptions) == null ? void 0 : _c2.vertex;
465
570
  const thoughtSignature = (providerOpts == null ? void 0 : providerOpts.thoughtSignature) != null ? String(providerOpts.thoughtSignature) : void 0;
466
571
  switch (part.type) {
467
572
  case "text": {
@@ -515,36 +620,10 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
515
620
  }
516
621
  const output = part.output;
517
622
  if (output.type === "content") {
518
- for (const contentPart of output.value) {
519
- switch (contentPart.type) {
520
- case "text":
521
- parts.push({
522
- functionResponse: {
523
- name: part.toolName,
524
- response: {
525
- name: part.toolName,
526
- content: contentPart.text
527
- }
528
- }
529
- });
530
- break;
531
- case "image-data":
532
- parts.push(
533
- {
534
- inlineData: {
535
- mimeType: contentPart.mediaType,
536
- data: contentPart.data
537
- }
538
- },
539
- {
540
- text: "Tool executed successfully and returned this image as a response"
541
- }
542
- );
543
- break;
544
- default:
545
- parts.push({ text: JSON.stringify(contentPart) });
546
- break;
547
- }
623
+ if (supportsFunctionResponseParts) {
624
+ appendToolResultParts(parts, part.toolName, output.value);
625
+ } else {
626
+ appendLegacyToolResultParts(parts, part.toolName, output.value);
548
627
  }
549
628
  } else {
550
629
  parts.push({
@@ -552,7 +631,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
552
631
  name: part.toolName,
553
632
  response: {
554
633
  name: part.toolName,
555
- content: output.type === "execution-denied" ? (_c = output.reason) != null ? _c : "Tool execution denied." : output.value
634
+ content: output.type === "execution-denied" ? (_d = output.reason) != null ? _d : "Tool execution denied." : output.value
556
635
  }
557
636
  }
558
637
  });
@@ -996,9 +1075,14 @@ var GoogleGenerativeAILanguageModel = class {
996
1075
  });
997
1076
  }
998
1077
  const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
1078
+ const supportsFunctionResponseParts = this.modelId.startsWith("gemini-3");
999
1079
  const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
1000
1080
  prompt,
1001
- { isGemmaModel, providerOptionsName }
1081
+ {
1082
+ isGemmaModel,
1083
+ providerOptionsName,
1084
+ supportsFunctionResponseParts
1085
+ }
1002
1086
  );
1003
1087
  const {
1004
1088
  tools: googleTools2,
@@ -1056,7 +1140,7 @@ var GoogleGenerativeAILanguageModel = class {
1056
1140
  };
1057
1141
  }
1058
1142
  async doGenerate(options) {
1059
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1143
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
1060
1144
  const { args, warnings, providerOptionsName } = await this.getArgs(options);
1061
1145
  const mergedHeaders = combineHeaders2(
1062
1146
  await resolve2(this.config.headers),
@@ -1178,7 +1262,8 @@ var GoogleGenerativeAILanguageModel = class {
1178
1262
  groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
1179
1263
  urlContextMetadata: (_i = candidate.urlContextMetadata) != null ? _i : null,
1180
1264
  safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
1181
- usageMetadata: usageMetadata != null ? usageMetadata : null
1265
+ usageMetadata: usageMetadata != null ? usageMetadata : null,
1266
+ finishMessage: (_k = candidate.finishMessage) != null ? _k : null
1182
1267
  }
1183
1268
  },
1184
1269
  request: { body: args },
@@ -1228,7 +1313,7 @@ var GoogleGenerativeAILanguageModel = class {
1228
1313
  controller.enqueue({ type: "stream-start", warnings });
1229
1314
  },
1230
1315
  transform(chunk, controller) {
1231
- var _a, _b, _c, _d, _e, _f;
1316
+ var _a, _b, _c, _d, _e, _f, _g;
1232
1317
  if (options.includeRawChunks) {
1233
1318
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1234
1319
  }
@@ -1430,12 +1515,11 @@ var GoogleGenerativeAILanguageModel = class {
1430
1515
  promptFeedback: (_e = value.promptFeedback) != null ? _e : null,
1431
1516
  groundingMetadata: lastGroundingMetadata,
1432
1517
  urlContextMetadata: lastUrlContextMetadata,
1433
- safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null
1518
+ safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null,
1519
+ usageMetadata: usageMetadata != null ? usageMetadata : null,
1520
+ finishMessage: (_g = candidate.finishMessage) != null ? _g : null
1434
1521
  }
1435
1522
  };
1436
- if (usageMetadata != null) {
1437
- providerMetadata[providerOptionsName].usageMetadata = usageMetadata;
1438
- }
1439
1523
  }
1440
1524
  },
1441
1525
  flush(controller) {
@@ -1695,6 +1779,7 @@ var responseSchema = lazySchema5(
1695
1779
  z5.object({
1696
1780
  content: getContentSchema().nullish().or(z5.object({}).strict()),
1697
1781
  finishReason: z5.string().nullish(),
1782
+ finishMessage: z5.string().nullish(),
1698
1783
  safetyRatings: z5.array(getSafetyRatingSchema()).nullish(),
1699
1784
  groundingMetadata: getGroundingMetadataSchema().nullish(),
1700
1785
  urlContextMetadata: getUrlContextMetadataSchema().nullish()
@@ -1715,6 +1800,7 @@ var chunkSchema = lazySchema5(
1715
1800
  z5.object({
1716
1801
  content: getContentSchema().nullish(),
1717
1802
  finishReason: z5.string().nullish(),
1803
+ finishMessage: z5.string().nullish(),
1718
1804
  safetyRatings: z5.array(getSafetyRatingSchema()).nullish(),
1719
1805
  groundingMetadata: getGroundingMetadataSchema().nullish(),
1720
1806
  urlContextMetadata: getUrlContextMetadataSchema().nullish()