@ai-sdk/google 4.0.0-beta.14 → 4.0.0-beta.15

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 ? "4.0.0-beta.14" : "0.0.0-test";
10
+ var VERSION = true ? "4.0.0-beta.15" : "0.0.0-test";
11
11
 
12
12
  // src/google-generative-ai-embedding-model.ts
13
13
  import {
@@ -409,13 +409,118 @@ import {
409
409
  UnsupportedFunctionalityError
410
410
  } from "@ai-sdk/provider";
411
411
  import { convertToBase64 } from "@ai-sdk/provider-utils";
412
+ var dataUrlRegex = /^data:([^;,]+);base64,(.+)$/s;
413
+ function parseBase64DataUrl(value) {
414
+ const match = dataUrlRegex.exec(value);
415
+ if (match == null) {
416
+ return void 0;
417
+ }
418
+ return {
419
+ mediaType: match[1],
420
+ data: match[2]
421
+ };
422
+ }
423
+ function convertUrlToolResultPart(url) {
424
+ const parsedDataUrl = parseBase64DataUrl(url);
425
+ if (parsedDataUrl == null) {
426
+ return void 0;
427
+ }
428
+ return {
429
+ inlineData: {
430
+ mimeType: parsedDataUrl.mediaType,
431
+ data: parsedDataUrl.data
432
+ }
433
+ };
434
+ }
435
+ function appendToolResultParts(parts, toolName, outputValue) {
436
+ const functionResponseParts = [];
437
+ const responseTextParts = [];
438
+ for (const contentPart of outputValue) {
439
+ switch (contentPart.type) {
440
+ case "text": {
441
+ responseTextParts.push(contentPart.text);
442
+ break;
443
+ }
444
+ case "image-data":
445
+ case "file-data": {
446
+ functionResponseParts.push({
447
+ inlineData: {
448
+ mimeType: contentPart.mediaType,
449
+ data: contentPart.data
450
+ }
451
+ });
452
+ break;
453
+ }
454
+ case "image-url":
455
+ case "file-url": {
456
+ const functionResponsePart = convertUrlToolResultPart(
457
+ contentPart.url
458
+ );
459
+ if (functionResponsePart != null) {
460
+ functionResponseParts.push(functionResponsePart);
461
+ } else {
462
+ responseTextParts.push(JSON.stringify(contentPart));
463
+ }
464
+ break;
465
+ }
466
+ default: {
467
+ responseTextParts.push(JSON.stringify(contentPart));
468
+ break;
469
+ }
470
+ }
471
+ }
472
+ parts.push({
473
+ functionResponse: {
474
+ name: toolName,
475
+ response: {
476
+ name: toolName,
477
+ content: responseTextParts.length > 0 ? responseTextParts.join("\n") : "Tool executed successfully."
478
+ },
479
+ ...functionResponseParts.length > 0 ? { parts: functionResponseParts } : {}
480
+ }
481
+ });
482
+ }
483
+ function appendLegacyToolResultParts(parts, toolName, outputValue) {
484
+ for (const contentPart of outputValue) {
485
+ switch (contentPart.type) {
486
+ case "text":
487
+ parts.push({
488
+ functionResponse: {
489
+ name: toolName,
490
+ response: {
491
+ name: toolName,
492
+ content: contentPart.text
493
+ }
494
+ }
495
+ });
496
+ break;
497
+ case "image-data":
498
+ parts.push(
499
+ {
500
+ inlineData: {
501
+ mimeType: String(contentPart.mediaType),
502
+ data: String(contentPart.data)
503
+ }
504
+ },
505
+ {
506
+ text: "Tool executed successfully and returned this image as a response"
507
+ }
508
+ );
509
+ break;
510
+ default:
511
+ parts.push({ text: JSON.stringify(contentPart) });
512
+ break;
513
+ }
514
+ }
515
+ }
412
516
  function convertToGoogleGenerativeAIMessages(prompt, options) {
413
- var _a, _b, _c;
517
+ var _a, _b, _c, _d;
414
518
  const systemInstructionParts = [];
415
519
  const contents = [];
416
520
  let systemMessagesAllowed = true;
417
521
  const isGemmaModel = (_a = options == null ? void 0 : options.isGemmaModel) != null ? _a : false;
418
522
  const providerOptionsName = (_b = options == null ? void 0 : options.providerOptionsName) != null ? _b : "google";
523
+ const supportsFunctionResponseParts = (_c = options == null ? void 0 : options.supportsFunctionResponseParts) != null ? _c : true;
419
524
  for (const { role, content } of prompt) {
420
525
  switch (role) {
421
526
  case "system": {
@@ -463,8 +568,8 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
463
568
  contents.push({
464
569
  role: "model",
465
570
  parts: content.map((part) => {
466
- var _a2, _b2, _c2, _d;
467
- 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;
571
+ var _a2, _b2, _c2, _d2;
572
+ 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;
468
573
  const thoughtSignature = (providerOpts == null ? void 0 : providerOpts.thoughtSignature) != null ? String(providerOpts.thoughtSignature) : void 0;
469
574
  switch (part.type) {
470
575
  case "text": {
@@ -533,36 +638,10 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
533
638
  }
534
639
  const output = part.output;
535
640
  if (output.type === "content") {
536
- for (const contentPart of output.value) {
537
- switch (contentPart.type) {
538
- case "text":
539
- parts.push({
540
- functionResponse: {
541
- name: part.toolName,
542
- response: {
543
- name: part.toolName,
544
- content: contentPart.text
545
- }
546
- }
547
- });
548
- break;
549
- case "image-data":
550
- parts.push(
551
- {
552
- inlineData: {
553
- mimeType: contentPart.mediaType,
554
- data: contentPart.data
555
- }
556
- },
557
- {
558
- text: "Tool executed successfully and returned this image as a response"
559
- }
560
- );
561
- break;
562
- default:
563
- parts.push({ text: JSON.stringify(contentPart) });
564
- break;
565
- }
641
+ if (supportsFunctionResponseParts) {
642
+ appendToolResultParts(parts, part.toolName, output.value);
643
+ } else {
644
+ appendLegacyToolResultParts(parts, part.toolName, output.value);
566
645
  }
567
646
  } else {
568
647
  parts.push({
@@ -570,7 +649,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
570
649
  name: part.toolName,
571
650
  response: {
572
651
  name: part.toolName,
573
- content: output.type === "execution-denied" ? (_c = output.reason) != null ? _c : "Tool execution denied." : output.value
652
+ content: output.type === "execution-denied" ? (_d = output.reason) != null ? _d : "Tool execution denied." : output.value
574
653
  }
575
654
  }
576
655
  });
@@ -1015,9 +1094,14 @@ var GoogleGenerativeAILanguageModel = class {
1015
1094
  });
1016
1095
  }
1017
1096
  const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
1097
+ const supportsFunctionResponseParts = this.modelId.startsWith("gemini-3");
1018
1098
  const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
1019
1099
  prompt,
1020
- { isGemmaModel, providerOptionsName }
1100
+ {
1101
+ isGemmaModel,
1102
+ providerOptionsName,
1103
+ supportsFunctionResponseParts
1104
+ }
1021
1105
  );
1022
1106
  const {
1023
1107
  tools: googleTools2,