@ai-sdk/google 3.0.60 → 3.0.62
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 +12 -0
- package/dist/index.d.mts +12 -2
- package/dist/index.d.ts +12 -2
- package/dist/index.js +375 -56
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +375 -56
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +11 -2
- package/dist/internal/index.d.ts +11 -2
- package/dist/internal/index.js +374 -55
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +374 -55
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/google-generative-ai-language-model.ts +216 -45
- package/src/google-generative-ai-options.ts +12 -0
- package/src/google-json-accumulator.ts +336 -0
- package/src/google-prepare-tools.ts +1 -0
|
@@ -86,8 +86,17 @@ declare const responseSchema: _ai_sdk_provider_utils.LazySchema<{
|
|
|
86
86
|
content?: Record<string, never> | {
|
|
87
87
|
parts?: ({
|
|
88
88
|
functionCall: {
|
|
89
|
-
name
|
|
90
|
-
args
|
|
89
|
+
name?: string | null | undefined;
|
|
90
|
+
args?: unknown;
|
|
91
|
+
partialArgs?: {
|
|
92
|
+
jsonPath: string;
|
|
93
|
+
stringValue?: string | null | undefined;
|
|
94
|
+
numberValue?: number | null | undefined;
|
|
95
|
+
boolValue?: boolean | null | undefined;
|
|
96
|
+
nullValue?: unknown;
|
|
97
|
+
willContinue?: boolean | null | undefined;
|
|
98
|
+
}[] | null | undefined;
|
|
99
|
+
willContinue?: boolean | null | undefined;
|
|
91
100
|
};
|
|
92
101
|
thoughtSignature?: string | null | undefined;
|
|
93
102
|
} | {
|
package/dist/internal/index.d.ts
CHANGED
|
@@ -86,8 +86,17 @@ declare const responseSchema: _ai_sdk_provider_utils.LazySchema<{
|
|
|
86
86
|
content?: Record<string, never> | {
|
|
87
87
|
parts?: ({
|
|
88
88
|
functionCall: {
|
|
89
|
-
name
|
|
90
|
-
args
|
|
89
|
+
name?: string | null | undefined;
|
|
90
|
+
args?: unknown;
|
|
91
|
+
partialArgs?: {
|
|
92
|
+
jsonPath: string;
|
|
93
|
+
stringValue?: string | null | undefined;
|
|
94
|
+
numberValue?: number | null | undefined;
|
|
95
|
+
boolValue?: boolean | null | undefined;
|
|
96
|
+
nullValue?: unknown;
|
|
97
|
+
willContinue?: boolean | null | undefined;
|
|
98
|
+
}[] | null | undefined;
|
|
99
|
+
willContinue?: boolean | null | undefined;
|
|
91
100
|
};
|
|
92
101
|
thoughtSignature?: string | null | undefined;
|
|
93
102
|
} | {
|
package/dist/internal/index.js
CHANGED
|
@@ -627,6 +627,17 @@ var googleLanguageModelOptions = (0, import_provider_utils3.lazySchema)(
|
|
|
627
627
|
longitude: import_v42.z.number()
|
|
628
628
|
}).optional()
|
|
629
629
|
}).optional(),
|
|
630
|
+
/**
|
|
631
|
+
* Optional. When set to true, function call arguments will be streamed
|
|
632
|
+
* incrementally via partialArgs in streaming responses. Only supported
|
|
633
|
+
* on the Vertex AI API (not the Gemini API) and only for Gemini 3+
|
|
634
|
+
* models.
|
|
635
|
+
*
|
|
636
|
+
* @default false
|
|
637
|
+
*
|
|
638
|
+
* https://docs.cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling#streaming-fc
|
|
639
|
+
*/
|
|
640
|
+
streamFunctionCallArguments: import_v42.z.boolean().optional(),
|
|
630
641
|
/**
|
|
631
642
|
* Optional. The service tier to use for the request.
|
|
632
643
|
*/
|
|
@@ -885,6 +896,229 @@ function prepareTools({
|
|
|
885
896
|
}
|
|
886
897
|
}
|
|
887
898
|
|
|
899
|
+
// src/google-json-accumulator.ts
|
|
900
|
+
var GoogleJSONAccumulator = class {
|
|
901
|
+
constructor() {
|
|
902
|
+
this.accumulatedArgs = {};
|
|
903
|
+
this.jsonText = "";
|
|
904
|
+
/**
|
|
905
|
+
* Stack representing the currently "open" containers in the JSON output.
|
|
906
|
+
* Entry 0 is always the root `{` object once the first value is written.
|
|
907
|
+
*/
|
|
908
|
+
this.pathStack = [];
|
|
909
|
+
/**
|
|
910
|
+
* Whether a string value is currently "open" (willContinue was true),
|
|
911
|
+
* meaning the closing quote has not yet been emitted.
|
|
912
|
+
*/
|
|
913
|
+
this.stringOpen = false;
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* Input: [{jsonPath:"$.brightness",numberValue:50}]
|
|
917
|
+
* Output: { currentJSON:{brightness:50}, textDelta:'{"brightness":50' }
|
|
918
|
+
*/
|
|
919
|
+
processPartialArgs(partialArgs) {
|
|
920
|
+
let delta = "";
|
|
921
|
+
for (const arg of partialArgs) {
|
|
922
|
+
const rawPath = arg.jsonPath.replace(/^\$\./, "");
|
|
923
|
+
if (!rawPath) continue;
|
|
924
|
+
const segments = parsePath(rawPath);
|
|
925
|
+
const existingValue = getNestedValue(this.accumulatedArgs, segments);
|
|
926
|
+
const isStringContinuation = arg.stringValue != null && existingValue !== void 0;
|
|
927
|
+
if (isStringContinuation) {
|
|
928
|
+
const escaped = JSON.stringify(arg.stringValue).slice(1, -1);
|
|
929
|
+
setNestedValue(
|
|
930
|
+
this.accumulatedArgs,
|
|
931
|
+
segments,
|
|
932
|
+
existingValue + arg.stringValue
|
|
933
|
+
);
|
|
934
|
+
delta += escaped;
|
|
935
|
+
continue;
|
|
936
|
+
}
|
|
937
|
+
const resolved = resolvePartialArgValue(arg);
|
|
938
|
+
if (resolved == null) continue;
|
|
939
|
+
setNestedValue(this.accumulatedArgs, segments, resolved.value);
|
|
940
|
+
delta += this.emitNavigationTo(segments, arg, resolved.json);
|
|
941
|
+
}
|
|
942
|
+
this.jsonText += delta;
|
|
943
|
+
return {
|
|
944
|
+
currentJSON: this.accumulatedArgs,
|
|
945
|
+
textDelta: delta
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
/**
|
|
949
|
+
* Input: jsonText='{"brightness":50', accumulatedArgs={brightness:50}
|
|
950
|
+
* Output: { finalJSON:'{"brightness":50}', closingDelta:'}' }
|
|
951
|
+
*/
|
|
952
|
+
finalize() {
|
|
953
|
+
const finalArgs = JSON.stringify(this.accumulatedArgs);
|
|
954
|
+
const closingDelta = finalArgs.slice(this.jsonText.length);
|
|
955
|
+
return { finalJSON: finalArgs, closingDelta };
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* Input: pathStack=[] (first call) or pathStack=[root,...] (subsequent calls)
|
|
959
|
+
* Output: '{' (first call) or '' (subsequent calls)
|
|
960
|
+
*/
|
|
961
|
+
ensureRoot() {
|
|
962
|
+
if (this.pathStack.length === 0) {
|
|
963
|
+
this.pathStack.push({ segment: "", isArray: false, childCount: 0 });
|
|
964
|
+
return "{";
|
|
965
|
+
}
|
|
966
|
+
return "";
|
|
967
|
+
}
|
|
968
|
+
/**
|
|
969
|
+
* Emits the JSON text fragment needed to navigate from the current open
|
|
970
|
+
* path to the new leaf at `targetSegments`, then writes the value.
|
|
971
|
+
*
|
|
972
|
+
* Input: targetSegments=["recipe","name"], arg={jsonPath:"$.recipe.name",stringValue:"Lasagna"}, valueJson='"Lasagna"'
|
|
973
|
+
* Output: '{"recipe":{"name":"Lasagna"'
|
|
974
|
+
*/
|
|
975
|
+
emitNavigationTo(targetSegments, arg, valueJson) {
|
|
976
|
+
let fragment = "";
|
|
977
|
+
if (this.stringOpen) {
|
|
978
|
+
fragment += '"';
|
|
979
|
+
this.stringOpen = false;
|
|
980
|
+
}
|
|
981
|
+
fragment += this.ensureRoot();
|
|
982
|
+
const targetContainerSegments = targetSegments.slice(0, -1);
|
|
983
|
+
const leafSegment = targetSegments[targetSegments.length - 1];
|
|
984
|
+
const commonDepth = this.findCommonStackDepth(targetContainerSegments);
|
|
985
|
+
fragment += this.closeDownTo(commonDepth);
|
|
986
|
+
fragment += this.openDownTo(targetContainerSegments, leafSegment);
|
|
987
|
+
fragment += this.emitLeaf(leafSegment, arg, valueJson);
|
|
988
|
+
return fragment;
|
|
989
|
+
}
|
|
990
|
+
/**
|
|
991
|
+
* Returns the stack depth to preserve when navigating to a new target
|
|
992
|
+
* container path. Always >= 1 (the root is never popped).
|
|
993
|
+
*
|
|
994
|
+
* Input: stack=[root,"recipe","ingredients",0], target=["recipe","ingredients",1]
|
|
995
|
+
* Output: 3 (keep root+"recipe"+"ingredients")
|
|
996
|
+
*/
|
|
997
|
+
findCommonStackDepth(targetContainer) {
|
|
998
|
+
const maxDepth = Math.min(
|
|
999
|
+
this.pathStack.length - 1,
|
|
1000
|
+
targetContainer.length
|
|
1001
|
+
);
|
|
1002
|
+
let common = 0;
|
|
1003
|
+
for (let i = 0; i < maxDepth; i++) {
|
|
1004
|
+
if (this.pathStack[i + 1].segment === targetContainer[i]) {
|
|
1005
|
+
common++;
|
|
1006
|
+
} else {
|
|
1007
|
+
break;
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
return common + 1;
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* Closes containers from the current stack depth back down to `targetDepth`.
|
|
1014
|
+
*
|
|
1015
|
+
* Input: this.pathStack=[root,"recipe","ingredients",0], targetDepth=3
|
|
1016
|
+
* Output: '}'
|
|
1017
|
+
*/
|
|
1018
|
+
closeDownTo(targetDepth) {
|
|
1019
|
+
let fragment = "";
|
|
1020
|
+
while (this.pathStack.length > targetDepth) {
|
|
1021
|
+
const entry = this.pathStack.pop();
|
|
1022
|
+
fragment += entry.isArray ? "]" : "}";
|
|
1023
|
+
}
|
|
1024
|
+
return fragment;
|
|
1025
|
+
}
|
|
1026
|
+
/**
|
|
1027
|
+
* Opens containers from the current stack depth down to the full target
|
|
1028
|
+
* container path, emitting opening `{`, `[`, keys, and commas as needed.
|
|
1029
|
+
* `leafSegment` is used to determine if the innermost container is an array.
|
|
1030
|
+
*
|
|
1031
|
+
* Input: this.pathStack=[root], targetContainer=["recipe","ingredients"], leafSegment=0
|
|
1032
|
+
* Output: '"recipe":{"ingredients":['
|
|
1033
|
+
*/
|
|
1034
|
+
openDownTo(targetContainer, leafSegment) {
|
|
1035
|
+
let fragment = "";
|
|
1036
|
+
const startIdx = this.pathStack.length - 1;
|
|
1037
|
+
for (let i = startIdx; i < targetContainer.length; i++) {
|
|
1038
|
+
const seg = targetContainer[i];
|
|
1039
|
+
const parentEntry = this.pathStack[this.pathStack.length - 1];
|
|
1040
|
+
if (parentEntry.childCount > 0) {
|
|
1041
|
+
fragment += ",";
|
|
1042
|
+
}
|
|
1043
|
+
parentEntry.childCount++;
|
|
1044
|
+
if (typeof seg === "string") {
|
|
1045
|
+
fragment += `${JSON.stringify(seg)}:`;
|
|
1046
|
+
}
|
|
1047
|
+
const childSeg = i + 1 < targetContainer.length ? targetContainer[i + 1] : leafSegment;
|
|
1048
|
+
const isArray = typeof childSeg === "number";
|
|
1049
|
+
fragment += isArray ? "[" : "{";
|
|
1050
|
+
this.pathStack.push({ segment: seg, isArray, childCount: 0 });
|
|
1051
|
+
}
|
|
1052
|
+
return fragment;
|
|
1053
|
+
}
|
|
1054
|
+
/**
|
|
1055
|
+
* Emits the comma, key, and value for a leaf entry in the current container.
|
|
1056
|
+
*
|
|
1057
|
+
* Input: leafSegment="name", arg={stringValue:"Lasagna"}, valueJson='"Lasagna"'
|
|
1058
|
+
* Output: '"name":"Lasagna"' (or ',"name":"Lasagna"' if container.childCount > 0)
|
|
1059
|
+
*/
|
|
1060
|
+
emitLeaf(leafSegment, arg, valueJson) {
|
|
1061
|
+
let fragment = "";
|
|
1062
|
+
const container = this.pathStack[this.pathStack.length - 1];
|
|
1063
|
+
if (container.childCount > 0) {
|
|
1064
|
+
fragment += ",";
|
|
1065
|
+
}
|
|
1066
|
+
container.childCount++;
|
|
1067
|
+
if (typeof leafSegment === "string") {
|
|
1068
|
+
fragment += `${JSON.stringify(leafSegment)}:`;
|
|
1069
|
+
}
|
|
1070
|
+
if (arg.stringValue != null && arg.willContinue) {
|
|
1071
|
+
fragment += valueJson.slice(0, -1);
|
|
1072
|
+
this.stringOpen = true;
|
|
1073
|
+
} else {
|
|
1074
|
+
fragment += valueJson;
|
|
1075
|
+
}
|
|
1076
|
+
return fragment;
|
|
1077
|
+
}
|
|
1078
|
+
};
|
|
1079
|
+
function parsePath(rawPath) {
|
|
1080
|
+
const segments = [];
|
|
1081
|
+
for (const part of rawPath.split(".")) {
|
|
1082
|
+
const bracketIdx = part.indexOf("[");
|
|
1083
|
+
if (bracketIdx === -1) {
|
|
1084
|
+
segments.push(part);
|
|
1085
|
+
} else {
|
|
1086
|
+
if (bracketIdx > 0) segments.push(part.slice(0, bracketIdx));
|
|
1087
|
+
for (const m of part.matchAll(/\[(\d+)\]/g)) {
|
|
1088
|
+
segments.push(parseInt(m[1], 10));
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
return segments;
|
|
1093
|
+
}
|
|
1094
|
+
function getNestedValue(obj, segments) {
|
|
1095
|
+
let current = obj;
|
|
1096
|
+
for (const seg of segments) {
|
|
1097
|
+
if (current == null || typeof current !== "object") return void 0;
|
|
1098
|
+
current = current[seg];
|
|
1099
|
+
}
|
|
1100
|
+
return current;
|
|
1101
|
+
}
|
|
1102
|
+
function setNestedValue(obj, segments, value) {
|
|
1103
|
+
let current = obj;
|
|
1104
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
1105
|
+
const seg = segments[i];
|
|
1106
|
+
const nextSeg = segments[i + 1];
|
|
1107
|
+
if (current[seg] == null) {
|
|
1108
|
+
current[seg] = typeof nextSeg === "number" ? [] : {};
|
|
1109
|
+
}
|
|
1110
|
+
current = current[seg];
|
|
1111
|
+
}
|
|
1112
|
+
current[segments[segments.length - 1]] = value;
|
|
1113
|
+
}
|
|
1114
|
+
function resolvePartialArgValue(arg) {
|
|
1115
|
+
var _a, _b;
|
|
1116
|
+
const value = (_b = (_a = arg.stringValue) != null ? _a : arg.numberValue) != null ? _b : arg.boolValue;
|
|
1117
|
+
if (value != null) return { value, json: JSON.stringify(value) };
|
|
1118
|
+
if ("nullValue" in arg) return { value: null, json: "null" };
|
|
1119
|
+
return void 0;
|
|
1120
|
+
}
|
|
1121
|
+
|
|
888
1122
|
// src/map-google-generative-ai-finish-reason.ts
|
|
889
1123
|
function mapGoogleGenerativeAIFinishReason({
|
|
890
1124
|
finishReason,
|
|
@@ -941,8 +1175,8 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
941
1175
|
tools,
|
|
942
1176
|
toolChoice,
|
|
943
1177
|
providerOptions
|
|
944
|
-
}) {
|
|
945
|
-
var _a;
|
|
1178
|
+
}, { isStreaming = false } = {}) {
|
|
1179
|
+
var _a, _b;
|
|
946
1180
|
const warnings = [];
|
|
947
1181
|
const providerOptionsName = this.config.provider.includes("vertex") ? "vertex" : "google";
|
|
948
1182
|
let googleOptions = await (0, import_provider_utils4.parseProviderOptions)({
|
|
@@ -957,14 +1191,21 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
957
1191
|
schema: googleLanguageModelOptions
|
|
958
1192
|
});
|
|
959
1193
|
}
|
|
1194
|
+
const isVertexProvider = this.config.provider.startsWith("google.vertex.");
|
|
960
1195
|
if ((tools == null ? void 0 : tools.some(
|
|
961
1196
|
(tool) => tool.type === "provider" && tool.id === "google.vertex_rag_store"
|
|
962
|
-
)) && !
|
|
1197
|
+
)) && !isVertexProvider) {
|
|
963
1198
|
warnings.push({
|
|
964
1199
|
type: "other",
|
|
965
1200
|
message: `The 'vertex_rag_store' tool is only supported with the Google Vertex provider and might not be supported or could behave unexpectedly with the current Google provider (${this.config.provider}).`
|
|
966
1201
|
});
|
|
967
1202
|
}
|
|
1203
|
+
if ((googleOptions == null ? void 0 : googleOptions.streamFunctionCallArguments) && !isVertexProvider) {
|
|
1204
|
+
warnings.push({
|
|
1205
|
+
type: "other",
|
|
1206
|
+
message: `'streamFunctionCallArguments' is only supported on the Vertex AI API and will be ignored with the current Google provider (${this.config.provider}). See https://docs.cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling#streaming-fc`
|
|
1207
|
+
});
|
|
1208
|
+
}
|
|
968
1209
|
const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
|
|
969
1210
|
const supportsFunctionResponseParts = this.modelId.startsWith("gemini-3");
|
|
970
1211
|
const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
|
|
@@ -984,6 +1225,19 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
984
1225
|
toolChoice,
|
|
985
1226
|
modelId: this.modelId
|
|
986
1227
|
});
|
|
1228
|
+
const streamFunctionCallArguments = isStreaming && isVertexProvider ? (_a = googleOptions == null ? void 0 : googleOptions.streamFunctionCallArguments) != null ? _a : false : void 0;
|
|
1229
|
+
const toolConfig = googleToolConfig || streamFunctionCallArguments || (googleOptions == null ? void 0 : googleOptions.retrievalConfig) ? {
|
|
1230
|
+
...googleToolConfig,
|
|
1231
|
+
...streamFunctionCallArguments && {
|
|
1232
|
+
functionCallingConfig: {
|
|
1233
|
+
...googleToolConfig == null ? void 0 : googleToolConfig.functionCallingConfig,
|
|
1234
|
+
streamFunctionCallArguments: true
|
|
1235
|
+
}
|
|
1236
|
+
},
|
|
1237
|
+
...(googleOptions == null ? void 0 : googleOptions.retrievalConfig) && {
|
|
1238
|
+
retrievalConfig: googleOptions.retrievalConfig
|
|
1239
|
+
}
|
|
1240
|
+
} : void 0;
|
|
987
1241
|
return {
|
|
988
1242
|
args: {
|
|
989
1243
|
generationConfig: {
|
|
@@ -1001,7 +1255,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1001
1255
|
responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
|
|
1002
1256
|
// so this is needed as an escape hatch:
|
|
1003
1257
|
// TODO convert into provider option
|
|
1004
|
-
((
|
|
1258
|
+
((_b = googleOptions == null ? void 0 : googleOptions.structuredOutputs) != null ? _b : true) ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
|
|
1005
1259
|
...(googleOptions == null ? void 0 : googleOptions.audioTimestamp) && {
|
|
1006
1260
|
audioTimestamp: googleOptions.audioTimestamp
|
|
1007
1261
|
},
|
|
@@ -1019,10 +1273,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1019
1273
|
systemInstruction: isGemmaModel ? void 0 : systemInstruction,
|
|
1020
1274
|
safetySettings: googleOptions == null ? void 0 : googleOptions.safetySettings,
|
|
1021
1275
|
tools: googleTools2,
|
|
1022
|
-
toolConfig
|
|
1023
|
-
...googleToolConfig,
|
|
1024
|
-
retrievalConfig: googleOptions.retrievalConfig
|
|
1025
|
-
} : googleToolConfig,
|
|
1276
|
+
toolConfig,
|
|
1026
1277
|
cachedContent: googleOptions == null ? void 0 : googleOptions.cachedContent,
|
|
1027
1278
|
labels: googleOptions == null ? void 0 : googleOptions.labels,
|
|
1028
1279
|
serviceTier: googleOptions == null ? void 0 : googleOptions.serviceTier
|
|
@@ -1100,7 +1351,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1100
1351
|
providerMetadata: thoughtSignatureMetadata
|
|
1101
1352
|
});
|
|
1102
1353
|
}
|
|
1103
|
-
} else if ("functionCall" in part) {
|
|
1354
|
+
} else if ("functionCall" in part && part.functionCall.name != null && part.functionCall.args != null) {
|
|
1104
1355
|
content.push({
|
|
1105
1356
|
type: "tool-call",
|
|
1106
1357
|
toolCallId: this.config.generateId(),
|
|
@@ -1213,7 +1464,10 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1213
1464
|
};
|
|
1214
1465
|
}
|
|
1215
1466
|
async doStream(options) {
|
|
1216
|
-
const { args, warnings, providerOptionsName } = await this.getArgs(
|
|
1467
|
+
const { args, warnings, providerOptionsName } = await this.getArgs(
|
|
1468
|
+
options,
|
|
1469
|
+
{ isStreaming: true }
|
|
1470
|
+
);
|
|
1217
1471
|
const headers = (0, import_provider_utils4.combineHeaders)(
|
|
1218
1472
|
await (0, import_provider_utils4.resolve)(this.config.headers),
|
|
1219
1473
|
options.headers
|
|
@@ -1246,6 +1500,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1246
1500
|
const emittedSourceUrls = /* @__PURE__ */ new Set();
|
|
1247
1501
|
let lastCodeExecutionToolCallId;
|
|
1248
1502
|
let lastServerToolCallId;
|
|
1503
|
+
const activeStreamingToolCalls = [];
|
|
1249
1504
|
return {
|
|
1250
1505
|
stream: response.pipeThrough(
|
|
1251
1506
|
new TransformStream({
|
|
@@ -1253,7 +1508,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1253
1508
|
controller.enqueue({ type: "stream-start", warnings });
|
|
1254
1509
|
},
|
|
1255
1510
|
transform(chunk, controller) {
|
|
1256
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
1511
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
1257
1512
|
if (options.includeRawChunks) {
|
|
1258
1513
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
1259
1514
|
}
|
|
@@ -1446,36 +1701,110 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1446
1701
|
lastServerToolCallId = void 0;
|
|
1447
1702
|
}
|
|
1448
1703
|
}
|
|
1449
|
-
const
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1704
|
+
for (const part of parts) {
|
|
1705
|
+
if (!("functionCall" in part)) continue;
|
|
1706
|
+
const providerMeta = part.thoughtSignature ? {
|
|
1707
|
+
[providerOptionsName]: {
|
|
1708
|
+
thoughtSignature: part.thoughtSignature
|
|
1709
|
+
}
|
|
1710
|
+
} : void 0;
|
|
1711
|
+
const isStreamingChunk = part.functionCall.partialArgs != null || part.functionCall.name != null && part.functionCall.willContinue === true;
|
|
1712
|
+
const isTerminalChunk = part.functionCall.name == null && part.functionCall.args == null && part.functionCall.partialArgs == null && part.functionCall.willContinue == null;
|
|
1713
|
+
const isCompleteCall = part.functionCall.name != null && part.functionCall.args != null && part.functionCall.partialArgs == null;
|
|
1714
|
+
if (isStreamingChunk) {
|
|
1715
|
+
if (part.functionCall.name != null && part.functionCall.willContinue === true) {
|
|
1716
|
+
const toolCallId = generateId2();
|
|
1717
|
+
const accumulator = new GoogleJSONAccumulator();
|
|
1718
|
+
activeStreamingToolCalls.push({
|
|
1719
|
+
toolCallId,
|
|
1720
|
+
toolName: part.functionCall.name,
|
|
1721
|
+
accumulator,
|
|
1722
|
+
providerMetadata: providerMeta
|
|
1723
|
+
});
|
|
1724
|
+
controller.enqueue({
|
|
1725
|
+
type: "tool-input-start",
|
|
1726
|
+
id: toolCallId,
|
|
1727
|
+
toolName: part.functionCall.name,
|
|
1728
|
+
providerMetadata: providerMeta
|
|
1729
|
+
});
|
|
1730
|
+
if (part.functionCall.partialArgs != null) {
|
|
1731
|
+
const { textDelta } = accumulator.processPartialArgs(
|
|
1732
|
+
part.functionCall.partialArgs
|
|
1733
|
+
);
|
|
1734
|
+
if (textDelta.length > 0) {
|
|
1735
|
+
controller.enqueue({
|
|
1736
|
+
type: "tool-input-delta",
|
|
1737
|
+
id: toolCallId,
|
|
1738
|
+
delta: textDelta,
|
|
1739
|
+
providerMetadata: providerMeta
|
|
1740
|
+
});
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
} else if (part.functionCall.partialArgs != null && activeStreamingToolCalls.length > 0) {
|
|
1744
|
+
const active = activeStreamingToolCalls[activeStreamingToolCalls.length - 1];
|
|
1745
|
+
const { textDelta } = active.accumulator.processPartialArgs(
|
|
1746
|
+
part.functionCall.partialArgs
|
|
1747
|
+
);
|
|
1748
|
+
if (textDelta.length > 0) {
|
|
1749
|
+
controller.enqueue({
|
|
1750
|
+
type: "tool-input-delta",
|
|
1751
|
+
id: active.toolCallId,
|
|
1752
|
+
delta: textDelta,
|
|
1753
|
+
providerMetadata: providerMeta
|
|
1754
|
+
});
|
|
1755
|
+
}
|
|
1756
|
+
}
|
|
1757
|
+
} else if (isTerminalChunk && activeStreamingToolCalls.length > 0) {
|
|
1758
|
+
const active = activeStreamingToolCalls.pop();
|
|
1759
|
+
const { finalJSON, closingDelta } = active.accumulator.finalize();
|
|
1760
|
+
if (closingDelta.length > 0) {
|
|
1761
|
+
controller.enqueue({
|
|
1762
|
+
type: "tool-input-delta",
|
|
1763
|
+
id: active.toolCallId,
|
|
1764
|
+
delta: closingDelta,
|
|
1765
|
+
providerMetadata: active.providerMetadata
|
|
1766
|
+
});
|
|
1767
|
+
}
|
|
1768
|
+
controller.enqueue({
|
|
1769
|
+
type: "tool-input-end",
|
|
1770
|
+
id: active.toolCallId,
|
|
1771
|
+
providerMetadata: active.providerMetadata
|
|
1772
|
+
});
|
|
1773
|
+
controller.enqueue({
|
|
1774
|
+
type: "tool-call",
|
|
1775
|
+
toolCallId: active.toolCallId,
|
|
1776
|
+
toolName: active.toolName,
|
|
1777
|
+
input: finalJSON,
|
|
1778
|
+
providerMetadata: active.providerMetadata
|
|
1779
|
+
});
|
|
1780
|
+
hasToolCalls = true;
|
|
1781
|
+
} else if (isCompleteCall) {
|
|
1782
|
+
const toolCallId = generateId2();
|
|
1783
|
+
const toolName = part.functionCall.name;
|
|
1784
|
+
const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_i = part.functionCall.args) != null ? _i : {});
|
|
1456
1785
|
controller.enqueue({
|
|
1457
1786
|
type: "tool-input-start",
|
|
1458
|
-
id:
|
|
1459
|
-
toolName
|
|
1460
|
-
providerMetadata:
|
|
1787
|
+
id: toolCallId,
|
|
1788
|
+
toolName,
|
|
1789
|
+
providerMetadata: providerMeta
|
|
1461
1790
|
});
|
|
1462
1791
|
controller.enqueue({
|
|
1463
1792
|
type: "tool-input-delta",
|
|
1464
|
-
id:
|
|
1465
|
-
delta:
|
|
1466
|
-
providerMetadata:
|
|
1793
|
+
id: toolCallId,
|
|
1794
|
+
delta: args2,
|
|
1795
|
+
providerMetadata: providerMeta
|
|
1467
1796
|
});
|
|
1468
1797
|
controller.enqueue({
|
|
1469
1798
|
type: "tool-input-end",
|
|
1470
|
-
id:
|
|
1471
|
-
providerMetadata:
|
|
1799
|
+
id: toolCallId,
|
|
1800
|
+
providerMetadata: providerMeta
|
|
1472
1801
|
});
|
|
1473
1802
|
controller.enqueue({
|
|
1474
1803
|
type: "tool-call",
|
|
1475
|
-
toolCallId
|
|
1476
|
-
toolName
|
|
1477
|
-
input:
|
|
1478
|
-
providerMetadata:
|
|
1804
|
+
toolCallId,
|
|
1805
|
+
toolName,
|
|
1806
|
+
input: args2,
|
|
1807
|
+
providerMetadata: providerMeta
|
|
1479
1808
|
});
|
|
1480
1809
|
hasToolCalls = true;
|
|
1481
1810
|
}
|
|
@@ -1491,12 +1820,12 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1491
1820
|
};
|
|
1492
1821
|
providerMetadata = {
|
|
1493
1822
|
[providerOptionsName]: {
|
|
1494
|
-
promptFeedback: (
|
|
1823
|
+
promptFeedback: (_j = value.promptFeedback) != null ? _j : null,
|
|
1495
1824
|
groundingMetadata: lastGroundingMetadata,
|
|
1496
1825
|
urlContextMetadata: lastUrlContextMetadata,
|
|
1497
|
-
safetyRatings: (
|
|
1826
|
+
safetyRatings: (_k = candidate.safetyRatings) != null ? _k : null,
|
|
1498
1827
|
usageMetadata: usageMetadata != null ? usageMetadata : null,
|
|
1499
|
-
finishMessage: (
|
|
1828
|
+
finishMessage: (_l = candidate.finishMessage) != null ? _l : null,
|
|
1500
1829
|
serviceTier
|
|
1501
1830
|
}
|
|
1502
1831
|
};
|
|
@@ -1529,26 +1858,6 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1529
1858
|
};
|
|
1530
1859
|
}
|
|
1531
1860
|
};
|
|
1532
|
-
function getToolCallsFromParts({
|
|
1533
|
-
parts,
|
|
1534
|
-
generateId: generateId2,
|
|
1535
|
-
providerOptionsName
|
|
1536
|
-
}) {
|
|
1537
|
-
const functionCallParts = parts == null ? void 0 : parts.filter(
|
|
1538
|
-
(part) => "functionCall" in part
|
|
1539
|
-
);
|
|
1540
|
-
return functionCallParts == null || functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
|
|
1541
|
-
type: "tool-call",
|
|
1542
|
-
toolCallId: generateId2(),
|
|
1543
|
-
toolName: part.functionCall.name,
|
|
1544
|
-
args: JSON.stringify(part.functionCall.args),
|
|
1545
|
-
providerMetadata: part.thoughtSignature ? {
|
|
1546
|
-
[providerOptionsName]: {
|
|
1547
|
-
thoughtSignature: part.thoughtSignature
|
|
1548
|
-
}
|
|
1549
|
-
} : void 0
|
|
1550
|
-
}));
|
|
1551
|
-
}
|
|
1552
1861
|
function extractSources({
|
|
1553
1862
|
groundingMetadata,
|
|
1554
1863
|
generateId: generateId2
|
|
@@ -1692,14 +2001,24 @@ var getGroundingMetadataSchema = () => import_v43.z.object({
|
|
|
1692
2001
|
import_v43.z.object({})
|
|
1693
2002
|
]).nullish()
|
|
1694
2003
|
});
|
|
2004
|
+
var partialArgSchema = import_v43.z.object({
|
|
2005
|
+
jsonPath: import_v43.z.string(),
|
|
2006
|
+
stringValue: import_v43.z.string().nullish(),
|
|
2007
|
+
numberValue: import_v43.z.number().nullish(),
|
|
2008
|
+
boolValue: import_v43.z.boolean().nullish(),
|
|
2009
|
+
nullValue: import_v43.z.unknown().nullish(),
|
|
2010
|
+
willContinue: import_v43.z.boolean().nullish()
|
|
2011
|
+
});
|
|
1695
2012
|
var getContentSchema = () => import_v43.z.object({
|
|
1696
2013
|
parts: import_v43.z.array(
|
|
1697
2014
|
import_v43.z.union([
|
|
1698
2015
|
// note: order matters since text can be fully empty
|
|
1699
2016
|
import_v43.z.object({
|
|
1700
2017
|
functionCall: import_v43.z.object({
|
|
1701
|
-
name: import_v43.z.string(),
|
|
1702
|
-
args: import_v43.z.unknown()
|
|
2018
|
+
name: import_v43.z.string().nullish(),
|
|
2019
|
+
args: import_v43.z.unknown().nullish(),
|
|
2020
|
+
partialArgs: import_v43.z.array(partialArgSchema).nullish(),
|
|
2021
|
+
willContinue: import_v43.z.boolean().nullish()
|
|
1703
2022
|
}),
|
|
1704
2023
|
thoughtSignature: import_v43.z.string().nullish()
|
|
1705
2024
|
}),
|