@ai-sdk/google 2.0.107 → 2.0.109
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/CHANGELOG.md +20 -0
- package/dist/index.js +123 -29
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +130 -30
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +122 -28
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +129 -29
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/internal/index.mjs
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
// src/google-generative-ai-language-model.ts
|
|
2
|
+
import {
|
|
3
|
+
APICallError
|
|
4
|
+
} from "@ai-sdk/provider";
|
|
2
5
|
import {
|
|
3
6
|
combineHeaders,
|
|
4
7
|
createEventSourceResponseHandler,
|
|
@@ -387,7 +390,11 @@ function isEmptyObjectSchema(jsonSchema) {
|
|
|
387
390
|
import {
|
|
388
391
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
389
392
|
} from "@ai-sdk/provider";
|
|
390
|
-
import {
|
|
393
|
+
import {
|
|
394
|
+
stringifyWithBigIntAndCycles,
|
|
395
|
+
secureJsonParse,
|
|
396
|
+
convertToBase64
|
|
397
|
+
} from "@ai-sdk/provider-utils";
|
|
391
398
|
var SKIP_THOUGHT_SIGNATURE_VALIDATOR = "skip_thought_signature_validator";
|
|
392
399
|
function convertToGoogleGenerativeAIMessages(prompt, options) {
|
|
393
400
|
var _a, _b, _c;
|
|
@@ -488,7 +495,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
|
|
|
488
495
|
functionCall: {
|
|
489
496
|
...part.toolCallId != null ? { id: part.toolCallId } : {},
|
|
490
497
|
name: part.toolName,
|
|
491
|
-
args: part.input
|
|
498
|
+
args: parseToolCallInput(part.input)
|
|
492
499
|
},
|
|
493
500
|
thoughtSignature: effectiveThoughtSignature
|
|
494
501
|
};
|
|
@@ -516,7 +523,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
|
|
|
516
523
|
name: part.toolName,
|
|
517
524
|
response: {
|
|
518
525
|
name: part.toolName,
|
|
519
|
-
content: output.value
|
|
526
|
+
content: serializeFunctionResponseContent(output.value)
|
|
520
527
|
}
|
|
521
528
|
}
|
|
522
529
|
});
|
|
@@ -607,11 +614,46 @@ function appendLegacyToolResultParts({
|
|
|
607
614
|
);
|
|
608
615
|
break;
|
|
609
616
|
default:
|
|
610
|
-
parts.push({ text:
|
|
617
|
+
parts.push({ text: stringifyWithBigIntAndCycles(contentPart) });
|
|
611
618
|
break;
|
|
612
619
|
}
|
|
613
620
|
}
|
|
614
621
|
}
|
|
622
|
+
function serializeFunctionResponseContent(value) {
|
|
623
|
+
return containsNonJsonValue(value) ? secureJsonParse(stringifyWithBigIntAndCycles(value)) : value;
|
|
624
|
+
}
|
|
625
|
+
function containsNonJsonValue(value, ancestors = /* @__PURE__ */ new Set()) {
|
|
626
|
+
if (typeof value === "bigint") {
|
|
627
|
+
return true;
|
|
628
|
+
}
|
|
629
|
+
if (typeof value !== "object" || value === null) {
|
|
630
|
+
return false;
|
|
631
|
+
}
|
|
632
|
+
if (typeof value.toJSON === "function") {
|
|
633
|
+
return true;
|
|
634
|
+
}
|
|
635
|
+
if (ancestors.has(value)) {
|
|
636
|
+
return true;
|
|
637
|
+
}
|
|
638
|
+
ancestors.add(value);
|
|
639
|
+
try {
|
|
640
|
+
return Object.values(value).some(
|
|
641
|
+
(item) => containsNonJsonValue(item, ancestors)
|
|
642
|
+
);
|
|
643
|
+
} finally {
|
|
644
|
+
ancestors.delete(value);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
function parseToolCallInput(input) {
|
|
648
|
+
if (typeof input !== "string") {
|
|
649
|
+
return input;
|
|
650
|
+
}
|
|
651
|
+
try {
|
|
652
|
+
return secureJsonParse(input);
|
|
653
|
+
} catch (e) {
|
|
654
|
+
return input;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
615
657
|
|
|
616
658
|
// src/get-model-path.ts
|
|
617
659
|
function getModelPath(modelId) {
|
|
@@ -625,15 +667,16 @@ import {
|
|
|
625
667
|
zodSchema
|
|
626
668
|
} from "@ai-sdk/provider-utils";
|
|
627
669
|
import { z } from "zod/v4";
|
|
670
|
+
var getGoogleErrorSchema = () => z.object({
|
|
671
|
+
code: z.number().nullable(),
|
|
672
|
+
message: z.string(),
|
|
673
|
+
status: z.string(),
|
|
674
|
+
details: z.array(z.unknown()).nullish()
|
|
675
|
+
});
|
|
628
676
|
var googleErrorDataSchema = lazySchema(
|
|
629
677
|
() => zodSchema(
|
|
630
678
|
z.object({
|
|
631
|
-
error:
|
|
632
|
-
code: z.number().nullable(),
|
|
633
|
-
message: z.string(),
|
|
634
|
-
status: z.string(),
|
|
635
|
-
details: z.array(z.unknown()).nullish()
|
|
636
|
-
})
|
|
679
|
+
error: getGoogleErrorSchema()
|
|
637
680
|
})
|
|
638
681
|
)
|
|
639
682
|
);
|
|
@@ -641,6 +684,39 @@ var googleFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
641
684
|
errorSchema: googleErrorDataSchema,
|
|
642
685
|
errorToMessage: (data) => data.error.message
|
|
643
686
|
});
|
|
687
|
+
function mapGoogleErrorStatusToHttpStatusCode(status) {
|
|
688
|
+
switch (status) {
|
|
689
|
+
case "CANCELLED":
|
|
690
|
+
return 499;
|
|
691
|
+
case "INVALID_ARGUMENT":
|
|
692
|
+
case "FAILED_PRECONDITION":
|
|
693
|
+
case "OUT_OF_RANGE":
|
|
694
|
+
return 400;
|
|
695
|
+
case "DEADLINE_EXCEEDED":
|
|
696
|
+
return 504;
|
|
697
|
+
case "NOT_FOUND":
|
|
698
|
+
return 404;
|
|
699
|
+
case "ALREADY_EXISTS":
|
|
700
|
+
case "ABORTED":
|
|
701
|
+
return 409;
|
|
702
|
+
case "PERMISSION_DENIED":
|
|
703
|
+
return 403;
|
|
704
|
+
case "UNAUTHENTICATED":
|
|
705
|
+
return 401;
|
|
706
|
+
case "RESOURCE_EXHAUSTED":
|
|
707
|
+
return 429;
|
|
708
|
+
case "UNIMPLEMENTED":
|
|
709
|
+
return 501;
|
|
710
|
+
case "UNKNOWN":
|
|
711
|
+
case "INTERNAL":
|
|
712
|
+
case "DATA_LOSS":
|
|
713
|
+
return 500;
|
|
714
|
+
case "UNAVAILABLE":
|
|
715
|
+
return 503;
|
|
716
|
+
default:
|
|
717
|
+
return void 0;
|
|
718
|
+
}
|
|
719
|
+
}
|
|
644
720
|
|
|
645
721
|
// src/google-generative-ai-options.ts
|
|
646
722
|
import {
|
|
@@ -848,6 +924,9 @@ function prepareTools({
|
|
|
848
924
|
modelId,
|
|
849
925
|
isVertexProvider = false
|
|
850
926
|
}) {
|
|
927
|
+
if ((toolChoice == null ? void 0 : toolChoice.type) === "none") {
|
|
928
|
+
tools = tools == null ? void 0 : tools.filter((tool) => tool.type === "function");
|
|
929
|
+
}
|
|
851
930
|
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
852
931
|
const toolWarnings = [];
|
|
853
932
|
const { supportsGemini2Tools, supportsFileSearch, usesGemini3Features } = getGoogleModelCapabilities(modelId);
|
|
@@ -863,7 +942,9 @@ function prepareTools({
|
|
|
863
942
|
toolWarnings.push({
|
|
864
943
|
type: "unsupported-tool",
|
|
865
944
|
tool: tools.find((tool) => tool.type === "function"),
|
|
866
|
-
details: `Cannot mix function tools with provider-defined tools in the same request. Falling back to provider-defined tools only. The following function tools will be ignored: ${functionTools.map((t) => t.name).join(
|
|
945
|
+
details: `Cannot mix function tools with provider-defined tools in the same request. Falling back to provider-defined tools only. The following function tools will be ignored: ${functionTools.map((t) => t.name).join(
|
|
946
|
+
", "
|
|
947
|
+
)}. Please use either function tools or provider-defined tools, but not both.`
|
|
867
948
|
});
|
|
868
949
|
}
|
|
869
950
|
if (hasProviderDefinedTools) {
|
|
@@ -1452,10 +1533,11 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1452
1533
|
await resolve(this.config.headers),
|
|
1453
1534
|
options.headers
|
|
1454
1535
|
);
|
|
1536
|
+
const url = `${this.config.baseURL}/${getModelPath(
|
|
1537
|
+
this.modelId
|
|
1538
|
+
)}:streamGenerateContent?alt=sse`;
|
|
1455
1539
|
const streamGenerateContent = (body2) => postJsonToApi({
|
|
1456
|
-
url
|
|
1457
|
-
this.modelId
|
|
1458
|
-
)}:streamGenerateContent?alt=sse`,
|
|
1540
|
+
url,
|
|
1459
1541
|
headers,
|
|
1460
1542
|
body: body2,
|
|
1461
1543
|
failedResponseHandler: googleFailedResponseHandler,
|
|
@@ -1514,7 +1596,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1514
1596
|
controller.enqueue({ type: "stream-start", warnings });
|
|
1515
1597
|
},
|
|
1516
1598
|
transform(chunk, controller) {
|
|
1517
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
|
1599
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
|
|
1518
1600
|
if (options.includeRawChunks) {
|
|
1519
1601
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
1520
1602
|
}
|
|
@@ -1523,6 +1605,23 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1523
1605
|
return;
|
|
1524
1606
|
}
|
|
1525
1607
|
const value = chunk.value;
|
|
1608
|
+
if (value.error != null) {
|
|
1609
|
+
finishReason = "error";
|
|
1610
|
+
const error = value.error;
|
|
1611
|
+
controller.enqueue({
|
|
1612
|
+
type: "error",
|
|
1613
|
+
error: new APICallError({
|
|
1614
|
+
message: error.message,
|
|
1615
|
+
url,
|
|
1616
|
+
requestBodyValues: args,
|
|
1617
|
+
statusCode: (_a = error.code) != null ? _a : mapGoogleErrorStatusToHttpStatusCode(error.status),
|
|
1618
|
+
responseHeaders,
|
|
1619
|
+
responseBody: JSON.stringify({ error }),
|
|
1620
|
+
data: error
|
|
1621
|
+
})
|
|
1622
|
+
});
|
|
1623
|
+
return;
|
|
1624
|
+
}
|
|
1526
1625
|
if (value.usageMetadata != null) {
|
|
1527
1626
|
requestUsageMetadata = value.usageMetadata;
|
|
1528
1627
|
}
|
|
@@ -1531,16 +1630,16 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1531
1630
|
requestUsageMetadata
|
|
1532
1631
|
);
|
|
1533
1632
|
if (usageMetadata != null) {
|
|
1534
|
-
usage.inputTokens = (
|
|
1535
|
-
usage.outputTokens = (
|
|
1536
|
-
usage.totalTokens = (
|
|
1537
|
-
usage.reasoningTokens = (
|
|
1538
|
-
usage.cachedInputTokens = (
|
|
1633
|
+
usage.inputTokens = (_b = usageMetadata.promptTokenCount) != null ? _b : void 0;
|
|
1634
|
+
usage.outputTokens = (_c = usageMetadata.candidatesTokenCount) != null ? _c : void 0;
|
|
1635
|
+
usage.totalTokens = (_d = usageMetadata.totalTokenCount) != null ? _d : void 0;
|
|
1636
|
+
usage.reasoningTokens = (_e = usageMetadata.thoughtsTokenCount) != null ? _e : void 0;
|
|
1637
|
+
usage.cachedInputTokens = (_f = usageMetadata.cachedContentTokenCount) != null ? _f : void 0;
|
|
1539
1638
|
}
|
|
1540
1639
|
if (value.serviceTier != null) {
|
|
1541
1640
|
serviceTier = value.serviceTier;
|
|
1542
1641
|
}
|
|
1543
|
-
const candidate = (
|
|
1642
|
+
const candidate = (_g = value.candidates) == null ? void 0 : _g[0];
|
|
1544
1643
|
if ((candidate == null ? void 0 : candidate.continuationToken) != null) {
|
|
1545
1644
|
lastContinuationToken = candidate.continuationToken;
|
|
1546
1645
|
}
|
|
@@ -1555,12 +1654,12 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1555
1654
|
requestUsageMetadata = void 0;
|
|
1556
1655
|
}
|
|
1557
1656
|
if (candidate == null) {
|
|
1558
|
-
const promptBlockReason2 = (
|
|
1657
|
+
const promptBlockReason2 = (_h = value.promptFeedback) == null ? void 0 : _h.blockReason;
|
|
1559
1658
|
if (promptBlockReason2 != null) {
|
|
1560
1659
|
finishReason = "content-filter";
|
|
1561
1660
|
providerMetadata = {
|
|
1562
1661
|
google: {
|
|
1563
|
-
promptFeedback: (
|
|
1662
|
+
promptFeedback: (_i = value.promptFeedback) != null ? _i : null,
|
|
1564
1663
|
groundingMetadata: lastGroundingMetadata,
|
|
1565
1664
|
urlContextMetadata: lastUrlContextMetadata,
|
|
1566
1665
|
safetyRatings: null,
|
|
@@ -1593,9 +1692,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1593
1692
|
}
|
|
1594
1693
|
}
|
|
1595
1694
|
if (content != null) {
|
|
1596
|
-
const parts = (
|
|
1695
|
+
const parts = (_j = content.parts) != null ? _j : [];
|
|
1597
1696
|
for (const part of parts) {
|
|
1598
|
-
if ("executableCode" in part && ((
|
|
1697
|
+
if ("executableCode" in part && ((_k = part.executableCode) == null ? void 0 : _k.code)) {
|
|
1599
1698
|
const toolCallId = generateId2();
|
|
1600
1699
|
lastCodeExecutionToolCallId = toolCallId;
|
|
1601
1700
|
controller.enqueue({
|
|
@@ -1722,9 +1821,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1722
1821
|
}
|
|
1723
1822
|
}
|
|
1724
1823
|
}
|
|
1725
|
-
const promptBlockReason = (
|
|
1824
|
+
const promptBlockReason = (_l = value.promptFeedback) == null ? void 0 : _l.blockReason;
|
|
1726
1825
|
const isPromptBlocked = candidate.finishReason == null && promptBlockReason != null;
|
|
1727
|
-
const rawFinishReason = (
|
|
1826
|
+
const rawFinishReason = (_n = (_m = candidate.finishReason) != null ? _m : promptBlockReason) != null ? _n : void 0;
|
|
1728
1827
|
if (rawFinishReason != null) {
|
|
1729
1828
|
finishReason = isPromptBlocked ? "content-filter" : mapGoogleGenerativeAIFinishReason({
|
|
1730
1829
|
finishReason: rawFinishReason,
|
|
@@ -1732,10 +1831,10 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1732
1831
|
});
|
|
1733
1832
|
providerMetadata = {
|
|
1734
1833
|
google: {
|
|
1735
|
-
promptFeedback: (
|
|
1834
|
+
promptFeedback: (_o = value.promptFeedback) != null ? _o : null,
|
|
1736
1835
|
groundingMetadata: lastGroundingMetadata,
|
|
1737
1836
|
urlContextMetadata: lastUrlContextMetadata,
|
|
1738
|
-
safetyRatings: (
|
|
1837
|
+
safetyRatings: (_p = candidate.safetyRatings) != null ? _p : null,
|
|
1739
1838
|
serviceTier,
|
|
1740
1839
|
// Only surface the token when the response stopped early
|
|
1741
1840
|
// (budget used up, or continuing failed), not for the
|
|
@@ -2042,6 +2141,7 @@ var responseSchema = lazySchema3(
|
|
|
2042
2141
|
var chunkSchema = lazySchema3(
|
|
2043
2142
|
() => zodSchema3(
|
|
2044
2143
|
z3.object({
|
|
2144
|
+
error: getGoogleErrorSchema().nullish(),
|
|
2045
2145
|
candidates: z3.array(
|
|
2046
2146
|
z3.object({
|
|
2047
2147
|
content: getContentSchema().nullish(),
|