@ai-sdk/google 2.0.107 → 2.0.108

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.
@@ -28,6 +28,7 @@ __export(index_exports, {
28
28
  module.exports = __toCommonJS(index_exports);
29
29
 
30
30
  // src/google-generative-ai-language-model.ts
31
+ var import_provider4 = require("@ai-sdk/provider");
31
32
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
32
33
  var import_v43 = require("zod/v4");
33
34
 
@@ -503,7 +504,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
503
504
  functionCall: {
504
505
  ...part.toolCallId != null ? { id: part.toolCallId } : {},
505
506
  name: part.toolName,
506
- args: part.input
507
+ args: parseToolCallInput(part.input)
507
508
  },
508
509
  thoughtSignature: effectiveThoughtSignature
509
510
  };
@@ -531,7 +532,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
531
532
  name: part.toolName,
532
533
  response: {
533
534
  name: part.toolName,
534
- content: output.value
535
+ content: serializeFunctionResponseContent(output.value)
535
536
  }
536
537
  }
537
538
  });
@@ -622,11 +623,46 @@ function appendLegacyToolResultParts({
622
623
  );
623
624
  break;
624
625
  default:
625
- parts.push({ text: JSON.stringify(contentPart) });
626
+ parts.push({ text: (0, import_provider_utils.stringifyWithBigIntAndCycles)(contentPart) });
626
627
  break;
627
628
  }
628
629
  }
629
630
  }
631
+ function serializeFunctionResponseContent(value) {
632
+ return containsNonJsonValue(value) ? (0, import_provider_utils.secureJsonParse)((0, import_provider_utils.stringifyWithBigIntAndCycles)(value)) : value;
633
+ }
634
+ function containsNonJsonValue(value, ancestors = /* @__PURE__ */ new Set()) {
635
+ if (typeof value === "bigint") {
636
+ return true;
637
+ }
638
+ if (typeof value !== "object" || value === null) {
639
+ return false;
640
+ }
641
+ if (typeof value.toJSON === "function") {
642
+ return true;
643
+ }
644
+ if (ancestors.has(value)) {
645
+ return true;
646
+ }
647
+ ancestors.add(value);
648
+ try {
649
+ return Object.values(value).some(
650
+ (item) => containsNonJsonValue(item, ancestors)
651
+ );
652
+ } finally {
653
+ ancestors.delete(value);
654
+ }
655
+ }
656
+ function parseToolCallInput(input) {
657
+ if (typeof input !== "string") {
658
+ return input;
659
+ }
660
+ try {
661
+ return (0, import_provider_utils.secureJsonParse)(input);
662
+ } catch (e) {
663
+ return input;
664
+ }
665
+ }
630
666
 
631
667
  // src/get-model-path.ts
632
668
  function getModelPath(modelId) {
@@ -636,15 +672,16 @@ function getModelPath(modelId) {
636
672
  // src/google-error.ts
637
673
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
638
674
  var import_v4 = require("zod/v4");
675
+ var getGoogleErrorSchema = () => import_v4.z.object({
676
+ code: import_v4.z.number().nullable(),
677
+ message: import_v4.z.string(),
678
+ status: import_v4.z.string(),
679
+ details: import_v4.z.array(import_v4.z.unknown()).nullish()
680
+ });
639
681
  var googleErrorDataSchema = (0, import_provider_utils2.lazySchema)(
640
682
  () => (0, import_provider_utils2.zodSchema)(
641
683
  import_v4.z.object({
642
- error: import_v4.z.object({
643
- code: import_v4.z.number().nullable(),
644
- message: import_v4.z.string(),
645
- status: import_v4.z.string(),
646
- details: import_v4.z.array(import_v4.z.unknown()).nullish()
647
- })
684
+ error: getGoogleErrorSchema()
648
685
  })
649
686
  )
650
687
  );
@@ -652,6 +689,39 @@ var googleFailedResponseHandler = (0, import_provider_utils2.createJsonErrorResp
652
689
  errorSchema: googleErrorDataSchema,
653
690
  errorToMessage: (data) => data.error.message
654
691
  });
692
+ function mapGoogleErrorStatusToHttpStatusCode(status) {
693
+ switch (status) {
694
+ case "CANCELLED":
695
+ return 499;
696
+ case "INVALID_ARGUMENT":
697
+ case "FAILED_PRECONDITION":
698
+ case "OUT_OF_RANGE":
699
+ return 400;
700
+ case "DEADLINE_EXCEEDED":
701
+ return 504;
702
+ case "NOT_FOUND":
703
+ return 404;
704
+ case "ALREADY_EXISTS":
705
+ case "ABORTED":
706
+ return 409;
707
+ case "PERMISSION_DENIED":
708
+ return 403;
709
+ case "UNAUTHENTICATED":
710
+ return 401;
711
+ case "RESOURCE_EXHAUSTED":
712
+ return 429;
713
+ case "UNIMPLEMENTED":
714
+ return 501;
715
+ case "UNKNOWN":
716
+ case "INTERNAL":
717
+ case "DATA_LOSS":
718
+ return 500;
719
+ case "UNAVAILABLE":
720
+ return 503;
721
+ default:
722
+ return void 0;
723
+ }
724
+ }
655
725
 
656
726
  // src/google-generative-ai-options.ts
657
727
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
@@ -1458,10 +1528,11 @@ var GoogleGenerativeAILanguageModel = class {
1458
1528
  await (0, import_provider_utils4.resolve)(this.config.headers),
1459
1529
  options.headers
1460
1530
  );
1531
+ const url = `${this.config.baseURL}/${getModelPath(
1532
+ this.modelId
1533
+ )}:streamGenerateContent?alt=sse`;
1461
1534
  const streamGenerateContent = (body2) => (0, import_provider_utils4.postJsonToApi)({
1462
- url: `${this.config.baseURL}/${getModelPath(
1463
- this.modelId
1464
- )}:streamGenerateContent?alt=sse`,
1535
+ url,
1465
1536
  headers,
1466
1537
  body: body2,
1467
1538
  failedResponseHandler: googleFailedResponseHandler,
@@ -1520,7 +1591,7 @@ var GoogleGenerativeAILanguageModel = class {
1520
1591
  controller.enqueue({ type: "stream-start", warnings });
1521
1592
  },
1522
1593
  transform(chunk, controller) {
1523
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1594
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
1524
1595
  if (options.includeRawChunks) {
1525
1596
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1526
1597
  }
@@ -1529,6 +1600,23 @@ var GoogleGenerativeAILanguageModel = class {
1529
1600
  return;
1530
1601
  }
1531
1602
  const value = chunk.value;
1603
+ if (value.error != null) {
1604
+ finishReason = "error";
1605
+ const error = value.error;
1606
+ controller.enqueue({
1607
+ type: "error",
1608
+ error: new import_provider4.APICallError({
1609
+ message: error.message,
1610
+ url,
1611
+ requestBodyValues: args,
1612
+ statusCode: (_a = error.code) != null ? _a : mapGoogleErrorStatusToHttpStatusCode(error.status),
1613
+ responseHeaders,
1614
+ responseBody: JSON.stringify({ error }),
1615
+ data: error
1616
+ })
1617
+ });
1618
+ return;
1619
+ }
1532
1620
  if (value.usageMetadata != null) {
1533
1621
  requestUsageMetadata = value.usageMetadata;
1534
1622
  }
@@ -1537,16 +1625,16 @@ var GoogleGenerativeAILanguageModel = class {
1537
1625
  requestUsageMetadata
1538
1626
  );
1539
1627
  if (usageMetadata != null) {
1540
- usage.inputTokens = (_a = usageMetadata.promptTokenCount) != null ? _a : void 0;
1541
- usage.outputTokens = (_b = usageMetadata.candidatesTokenCount) != null ? _b : void 0;
1542
- usage.totalTokens = (_c = usageMetadata.totalTokenCount) != null ? _c : void 0;
1543
- usage.reasoningTokens = (_d = usageMetadata.thoughtsTokenCount) != null ? _d : void 0;
1544
- usage.cachedInputTokens = (_e = usageMetadata.cachedContentTokenCount) != null ? _e : void 0;
1628
+ usage.inputTokens = (_b = usageMetadata.promptTokenCount) != null ? _b : void 0;
1629
+ usage.outputTokens = (_c = usageMetadata.candidatesTokenCount) != null ? _c : void 0;
1630
+ usage.totalTokens = (_d = usageMetadata.totalTokenCount) != null ? _d : void 0;
1631
+ usage.reasoningTokens = (_e = usageMetadata.thoughtsTokenCount) != null ? _e : void 0;
1632
+ usage.cachedInputTokens = (_f = usageMetadata.cachedContentTokenCount) != null ? _f : void 0;
1545
1633
  }
1546
1634
  if (value.serviceTier != null) {
1547
1635
  serviceTier = value.serviceTier;
1548
1636
  }
1549
- const candidate = (_f = value.candidates) == null ? void 0 : _f[0];
1637
+ const candidate = (_g = value.candidates) == null ? void 0 : _g[0];
1550
1638
  if ((candidate == null ? void 0 : candidate.continuationToken) != null) {
1551
1639
  lastContinuationToken = candidate.continuationToken;
1552
1640
  }
@@ -1561,12 +1649,12 @@ var GoogleGenerativeAILanguageModel = class {
1561
1649
  requestUsageMetadata = void 0;
1562
1650
  }
1563
1651
  if (candidate == null) {
1564
- const promptBlockReason2 = (_g = value.promptFeedback) == null ? void 0 : _g.blockReason;
1652
+ const promptBlockReason2 = (_h = value.promptFeedback) == null ? void 0 : _h.blockReason;
1565
1653
  if (promptBlockReason2 != null) {
1566
1654
  finishReason = "content-filter";
1567
1655
  providerMetadata = {
1568
1656
  google: {
1569
- promptFeedback: (_h = value.promptFeedback) != null ? _h : null,
1657
+ promptFeedback: (_i = value.promptFeedback) != null ? _i : null,
1570
1658
  groundingMetadata: lastGroundingMetadata,
1571
1659
  urlContextMetadata: lastUrlContextMetadata,
1572
1660
  safetyRatings: null,
@@ -1599,9 +1687,9 @@ var GoogleGenerativeAILanguageModel = class {
1599
1687
  }
1600
1688
  }
1601
1689
  if (content != null) {
1602
- const parts = (_i = content.parts) != null ? _i : [];
1690
+ const parts = (_j = content.parts) != null ? _j : [];
1603
1691
  for (const part of parts) {
1604
- if ("executableCode" in part && ((_j = part.executableCode) == null ? void 0 : _j.code)) {
1692
+ if ("executableCode" in part && ((_k = part.executableCode) == null ? void 0 : _k.code)) {
1605
1693
  const toolCallId = generateId2();
1606
1694
  lastCodeExecutionToolCallId = toolCallId;
1607
1695
  controller.enqueue({
@@ -1728,9 +1816,9 @@ var GoogleGenerativeAILanguageModel = class {
1728
1816
  }
1729
1817
  }
1730
1818
  }
1731
- const promptBlockReason = (_k = value.promptFeedback) == null ? void 0 : _k.blockReason;
1819
+ const promptBlockReason = (_l = value.promptFeedback) == null ? void 0 : _l.blockReason;
1732
1820
  const isPromptBlocked = candidate.finishReason == null && promptBlockReason != null;
1733
- const rawFinishReason = (_m = (_l = candidate.finishReason) != null ? _l : promptBlockReason) != null ? _m : void 0;
1821
+ const rawFinishReason = (_n = (_m = candidate.finishReason) != null ? _m : promptBlockReason) != null ? _n : void 0;
1734
1822
  if (rawFinishReason != null) {
1735
1823
  finishReason = isPromptBlocked ? "content-filter" : mapGoogleGenerativeAIFinishReason({
1736
1824
  finishReason: rawFinishReason,
@@ -1738,10 +1826,10 @@ var GoogleGenerativeAILanguageModel = class {
1738
1826
  });
1739
1827
  providerMetadata = {
1740
1828
  google: {
1741
- promptFeedback: (_n = value.promptFeedback) != null ? _n : null,
1829
+ promptFeedback: (_o = value.promptFeedback) != null ? _o : null,
1742
1830
  groundingMetadata: lastGroundingMetadata,
1743
1831
  urlContextMetadata: lastUrlContextMetadata,
1744
- safetyRatings: (_o = candidate.safetyRatings) != null ? _o : null,
1832
+ safetyRatings: (_p = candidate.safetyRatings) != null ? _p : null,
1745
1833
  serviceTier,
1746
1834
  // Only surface the token when the response stopped early
1747
1835
  // (budget used up, or continuing failed), not for the
@@ -2048,6 +2136,7 @@ var responseSchema = (0, import_provider_utils4.lazySchema)(
2048
2136
  var chunkSchema = (0, import_provider_utils4.lazySchema)(
2049
2137
  () => (0, import_provider_utils4.zodSchema)(
2050
2138
  import_v43.z.object({
2139
+ error: getGoogleErrorSchema().nullish(),
2051
2140
  candidates: import_v43.z.array(
2052
2141
  import_v43.z.object({
2053
2142
  content: getContentSchema().nullish(),