@ai-sdk/google 2.0.0-beta.17 → 2.0.0-beta.18
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 +10 -0
- package/dist/index.d.mts +33 -2
- package/dist/index.d.ts +33 -2
- package/dist/index.js +131 -34
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +122 -25
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +17 -0
- package/dist/internal/index.d.ts +17 -0
- package/dist/internal/index.js +116 -19
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +116 -19
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
@@ -522,6 +522,17 @@ function prepareTools({
|
|
522
522
|
});
|
523
523
|
}
|
524
524
|
break;
|
525
|
+
case "google.code_execution":
|
526
|
+
if (isGemini2) {
|
527
|
+
googleTools2.codeExecution = {};
|
528
|
+
} else {
|
529
|
+
toolWarnings.push({
|
530
|
+
type: "unsupported-tool",
|
531
|
+
tool,
|
532
|
+
details: "The code execution tools is not supported with other Gemini models than Gemini 2."
|
533
|
+
});
|
534
|
+
}
|
535
|
+
break;
|
525
536
|
default:
|
526
537
|
toolWarnings.push({ type: "unsupported-tool", tool });
|
527
538
|
break;
|
@@ -774,7 +785,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
774
785
|
};
|
775
786
|
}
|
776
787
|
async doGenerate(options) {
|
777
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
788
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
778
789
|
const { args, warnings } = await this.getArgs(options);
|
779
790
|
const body = JSON.stringify(args);
|
780
791
|
const mergedHeaders = combineHeaders2(
|
@@ -798,10 +809,34 @@ var GoogleGenerativeAILanguageModel = class {
|
|
798
809
|
});
|
799
810
|
const candidate = response.candidates[0];
|
800
811
|
const content = [];
|
801
|
-
const parts =
|
812
|
+
const parts = (_b = (_a = candidate.content) == null ? void 0 : _a.parts) != null ? _b : [];
|
802
813
|
const usageMetadata = response.usageMetadata;
|
814
|
+
let lastCodeExecutionToolCallId;
|
803
815
|
for (const part of parts) {
|
804
|
-
if ("
|
816
|
+
if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
|
817
|
+
const toolCallId = this.config.generateId();
|
818
|
+
lastCodeExecutionToolCallId = toolCallId;
|
819
|
+
content.push({
|
820
|
+
type: "tool-call",
|
821
|
+
toolCallId,
|
822
|
+
toolName: "code_execution",
|
823
|
+
input: JSON.stringify(part.executableCode),
|
824
|
+
providerExecuted: true
|
825
|
+
});
|
826
|
+
} else if ("codeExecutionResult" in part && part.codeExecutionResult) {
|
827
|
+
content.push({
|
828
|
+
type: "tool-result",
|
829
|
+
// Assumes a result directly follows its corresponding call part.
|
830
|
+
toolCallId: lastCodeExecutionToolCallId,
|
831
|
+
toolName: "code_execution",
|
832
|
+
result: {
|
833
|
+
outcome: part.codeExecutionResult.outcome,
|
834
|
+
output: part.codeExecutionResult.output
|
835
|
+
},
|
836
|
+
providerExecuted: true
|
837
|
+
});
|
838
|
+
lastCodeExecutionToolCallId = void 0;
|
839
|
+
} else if ("text" in part && part.text != null && part.text.length > 0) {
|
805
840
|
if (part.thought === true) {
|
806
841
|
content.push({ type: "reasoning", text: part.text });
|
807
842
|
} else {
|
@@ -822,10 +857,10 @@ var GoogleGenerativeAILanguageModel = class {
|
|
822
857
|
});
|
823
858
|
}
|
824
859
|
}
|
825
|
-
const sources = (
|
860
|
+
const sources = (_d = extractSources({
|
826
861
|
groundingMetadata: candidate.groundingMetadata,
|
827
862
|
generateId: this.config.generateId
|
828
|
-
})) != null ?
|
863
|
+
})) != null ? _d : [];
|
829
864
|
for (const source of sources) {
|
830
865
|
content.push(source);
|
831
866
|
}
|
@@ -836,18 +871,18 @@ var GoogleGenerativeAILanguageModel = class {
|
|
836
871
|
hasToolCalls: content.some((part) => part.type === "tool-call")
|
837
872
|
}),
|
838
873
|
usage: {
|
839
|
-
inputTokens: (
|
840
|
-
outputTokens: (
|
841
|
-
totalTokens: (
|
842
|
-
reasoningTokens: (
|
843
|
-
cachedInputTokens: (
|
874
|
+
inputTokens: (_e = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _e : void 0,
|
875
|
+
outputTokens: (_f = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _f : void 0,
|
876
|
+
totalTokens: (_g = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _g : void 0,
|
877
|
+
reasoningTokens: (_h = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _h : void 0,
|
878
|
+
cachedInputTokens: (_i = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _i : void 0
|
844
879
|
},
|
845
880
|
warnings,
|
846
881
|
providerMetadata: {
|
847
882
|
google: {
|
848
|
-
groundingMetadata: (
|
849
|
-
urlContextMetadata: (
|
850
|
-
safetyRatings: (
|
883
|
+
groundingMetadata: (_j = candidate.groundingMetadata) != null ? _j : null,
|
884
|
+
urlContextMetadata: (_k = candidate.urlContextMetadata) != null ? _k : null,
|
885
|
+
safetyRatings: (_l = candidate.safetyRatings) != null ? _l : null,
|
851
886
|
usageMetadata: usageMetadata != null ? usageMetadata : null
|
852
887
|
}
|
853
888
|
},
|
@@ -890,6 +925,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
890
925
|
let currentReasoningBlockId = null;
|
891
926
|
let blockCounter = 0;
|
892
927
|
const emittedSourceUrls = /* @__PURE__ */ new Set();
|
928
|
+
let lastCodeExecutionToolCallId;
|
893
929
|
return {
|
894
930
|
stream: response.pipeThrough(
|
895
931
|
new TransformStream({
|
@@ -897,7 +933,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
897
933
|
controller.enqueue({ type: "stream-start", warnings });
|
898
934
|
},
|
899
935
|
transform(chunk, controller) {
|
900
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
936
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
901
937
|
if (options.includeRawChunks) {
|
902
938
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
903
939
|
}
|
@@ -934,7 +970,33 @@ var GoogleGenerativeAILanguageModel = class {
|
|
934
970
|
if (content != null) {
|
935
971
|
const parts = (_g = content.parts) != null ? _g : [];
|
936
972
|
for (const part of parts) {
|
937
|
-
if ("
|
973
|
+
if ("executableCode" in part && ((_h = part.executableCode) == null ? void 0 : _h.code)) {
|
974
|
+
const toolCallId = generateId3();
|
975
|
+
lastCodeExecutionToolCallId = toolCallId;
|
976
|
+
controller.enqueue({
|
977
|
+
type: "tool-call",
|
978
|
+
toolCallId,
|
979
|
+
toolName: "code_execution",
|
980
|
+
input: JSON.stringify(part.executableCode),
|
981
|
+
providerExecuted: true
|
982
|
+
});
|
983
|
+
hasToolCalls = true;
|
984
|
+
} else if ("codeExecutionResult" in part && part.codeExecutionResult) {
|
985
|
+
const toolCallId = lastCodeExecutionToolCallId;
|
986
|
+
if (toolCallId) {
|
987
|
+
controller.enqueue({
|
988
|
+
type: "tool-result",
|
989
|
+
toolCallId,
|
990
|
+
toolName: "code_execution",
|
991
|
+
result: {
|
992
|
+
outcome: part.codeExecutionResult.outcome,
|
993
|
+
output: part.codeExecutionResult.output
|
994
|
+
},
|
995
|
+
providerExecuted: true
|
996
|
+
});
|
997
|
+
lastCodeExecutionToolCallId = void 0;
|
998
|
+
}
|
999
|
+
} else if ("text" in part && part.text != null && part.text.length > 0) {
|
938
1000
|
if (part.thought === true) {
|
939
1001
|
if (currentTextBlockId !== null) {
|
940
1002
|
controller.enqueue({
|
@@ -1025,9 +1087,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
1025
1087
|
});
|
1026
1088
|
providerMetadata = {
|
1027
1089
|
google: {
|
1028
|
-
groundingMetadata: (
|
1029
|
-
urlContextMetadata: (
|
1030
|
-
safetyRatings: (
|
1090
|
+
groundingMetadata: (_i = candidate.groundingMetadata) != null ? _i : null,
|
1091
|
+
urlContextMetadata: (_j = candidate.urlContextMetadata) != null ? _j : null,
|
1092
|
+
safetyRatings: (_k = candidate.safetyRatings) != null ? _k : null
|
1031
1093
|
}
|
1032
1094
|
};
|
1033
1095
|
if (usageMetadata != null) {
|
@@ -1113,6 +1175,14 @@ var contentSchema = z7.object({
|
|
1113
1175
|
})
|
1114
1176
|
}),
|
1115
1177
|
z7.object({
|
1178
|
+
executableCode: z7.object({
|
1179
|
+
language: z7.string(),
|
1180
|
+
code: z7.string()
|
1181
|
+
}).nullish(),
|
1182
|
+
codeExecutionResult: z7.object({
|
1183
|
+
outcome: z7.string(),
|
1184
|
+
output: z7.string()
|
1185
|
+
}).nullish(),
|
1116
1186
|
text: z7.string().nullish(),
|
1117
1187
|
thought: z7.boolean().nullish()
|
1118
1188
|
})
|
@@ -1159,6 +1229,22 @@ var chunkSchema = z7.object({
|
|
1159
1229
|
usageMetadata: usageSchema.nullish()
|
1160
1230
|
});
|
1161
1231
|
|
1232
|
+
// src/tool/code-execution.ts
|
1233
|
+
import { createProviderDefinedToolFactoryWithOutputSchema } from "@ai-sdk/provider-utils";
|
1234
|
+
import { z as z8 } from "zod/v4";
|
1235
|
+
var codeExecution = createProviderDefinedToolFactoryWithOutputSchema({
|
1236
|
+
id: "google.code_execution",
|
1237
|
+
name: "code_execution",
|
1238
|
+
inputSchema: z8.object({
|
1239
|
+
language: z8.string().describe("The programming language of the code."),
|
1240
|
+
code: z8.string().describe("The code to be executed.")
|
1241
|
+
}),
|
1242
|
+
outputSchema: z8.object({
|
1243
|
+
outcome: z8.string().describe('The outcome of the execution (e.g., "OUTCOME_OK").'),
|
1244
|
+
output: z8.string().describe("The output from the code execution.")
|
1245
|
+
})
|
1246
|
+
});
|
1247
|
+
|
1162
1248
|
// src/google-tools.ts
|
1163
1249
|
var googleTools = {
|
1164
1250
|
/**
|
@@ -1170,7 +1256,18 @@ var googleTools = {
|
|
1170
1256
|
* Creates a URL context tool that gives Google direct access to real-time web content.
|
1171
1257
|
* Must have name "url_context".
|
1172
1258
|
*/
|
1173
|
-
urlContext
|
1259
|
+
urlContext,
|
1260
|
+
/**
|
1261
|
+
* A tool that enables the model to generate and run Python code.
|
1262
|
+
* Must have name "code_execution".
|
1263
|
+
*
|
1264
|
+
* @note Ensure the selected model supports Code Execution.
|
1265
|
+
* Multi-tool usage with the code execution tool is typically compatible with Gemini >=2 models.
|
1266
|
+
*
|
1267
|
+
* @see https://ai.google.dev/gemini-api/docs/code-execution (Google AI)
|
1268
|
+
* @see https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/code-execution-api (Vertex AI)
|
1269
|
+
*/
|
1270
|
+
codeExecution
|
1174
1271
|
};
|
1175
1272
|
|
1176
1273
|
// src/google-generative-ai-image-model.ts
|
@@ -1181,7 +1278,7 @@ import {
|
|
1181
1278
|
postJsonToApi as postJsonToApi3,
|
1182
1279
|
resolve as resolve3
|
1183
1280
|
} from "@ai-sdk/provider-utils";
|
1184
|
-
import { z as
|
1281
|
+
import { z as z9 } from "zod/v4";
|
1185
1282
|
var GoogleGenerativeAIImageModel = class {
|
1186
1283
|
constructor(modelId, settings, config) {
|
1187
1284
|
this.modelId = modelId;
|
@@ -1273,12 +1370,12 @@ var GoogleGenerativeAIImageModel = class {
|
|
1273
1370
|
};
|
1274
1371
|
}
|
1275
1372
|
};
|
1276
|
-
var googleImageResponseSchema =
|
1277
|
-
predictions:
|
1373
|
+
var googleImageResponseSchema = z9.object({
|
1374
|
+
predictions: z9.array(z9.object({ bytesBase64Encoded: z9.string() })).default([])
|
1278
1375
|
});
|
1279
|
-
var googleImageProviderOptionsSchema =
|
1280
|
-
personGeneration:
|
1281
|
-
aspectRatio:
|
1376
|
+
var googleImageProviderOptionsSchema = z9.object({
|
1377
|
+
personGeneration: z9.enum(["dont_allow", "allow_adult", "allow_all"]).nullish(),
|
1378
|
+
aspectRatio: z9.enum(["1:1", "3:4", "4:3", "9:16", "16:9"]).nullish()
|
1282
1379
|
});
|
1283
1380
|
|
1284
1381
|
// src/google-provider.ts
|