@ai-sdk/google 3.0.51 → 3.0.52

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.
@@ -18,14 +18,14 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
20
  // src/internal/index.ts
21
- var internal_exports = {};
22
- __export(internal_exports, {
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
23
  GoogleGenerativeAILanguageModel: () => GoogleGenerativeAILanguageModel,
24
24
  getGroundingMetadataSchema: () => getGroundingMetadataSchema,
25
25
  getUrlContextMetadataSchema: () => getUrlContextMetadataSchema,
26
26
  googleTools: () => googleTools
27
27
  });
28
- module.exports = __toCommonJS(internal_exports);
28
+ module.exports = __toCommonJS(index_exports);
29
29
 
30
30
  // src/google-generative-ai-language-model.ts
31
31
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
@@ -189,13 +189,118 @@ function isEmptyObjectSchema(jsonSchema) {
189
189
  // src/convert-to-google-generative-ai-messages.ts
190
190
  var import_provider = require("@ai-sdk/provider");
191
191
  var import_provider_utils = require("@ai-sdk/provider-utils");
192
+ var dataUrlRegex = /^data:([^;,]+);base64,(.+)$/s;
193
+ function parseBase64DataUrl(value) {
194
+ const match = dataUrlRegex.exec(value);
195
+ if (match == null) {
196
+ return void 0;
197
+ }
198
+ return {
199
+ mediaType: match[1],
200
+ data: match[2]
201
+ };
202
+ }
203
+ function convertUrlToolResultPart(url) {
204
+ const parsedDataUrl = parseBase64DataUrl(url);
205
+ if (parsedDataUrl == null) {
206
+ return void 0;
207
+ }
208
+ return {
209
+ inlineData: {
210
+ mimeType: parsedDataUrl.mediaType,
211
+ data: parsedDataUrl.data
212
+ }
213
+ };
214
+ }
215
+ function appendToolResultParts(parts, toolName, outputValue) {
216
+ const functionResponseParts = [];
217
+ const responseTextParts = [];
218
+ for (const contentPart of outputValue) {
219
+ switch (contentPart.type) {
220
+ case "text": {
221
+ responseTextParts.push(contentPart.text);
222
+ break;
223
+ }
224
+ case "image-data":
225
+ case "file-data": {
226
+ functionResponseParts.push({
227
+ inlineData: {
228
+ mimeType: contentPart.mediaType,
229
+ data: contentPart.data
230
+ }
231
+ });
232
+ break;
233
+ }
234
+ case "image-url":
235
+ case "file-url": {
236
+ const functionResponsePart = convertUrlToolResultPart(
237
+ contentPart.url
238
+ );
239
+ if (functionResponsePart != null) {
240
+ functionResponseParts.push(functionResponsePart);
241
+ } else {
242
+ responseTextParts.push(JSON.stringify(contentPart));
243
+ }
244
+ break;
245
+ }
246
+ default: {
247
+ responseTextParts.push(JSON.stringify(contentPart));
248
+ break;
249
+ }
250
+ }
251
+ }
252
+ parts.push({
253
+ functionResponse: {
254
+ name: toolName,
255
+ response: {
256
+ name: toolName,
257
+ content: responseTextParts.length > 0 ? responseTextParts.join("\n") : "Tool executed successfully."
258
+ },
259
+ ...functionResponseParts.length > 0 ? { parts: functionResponseParts } : {}
260
+ }
261
+ });
262
+ }
263
+ function appendLegacyToolResultParts(parts, toolName, outputValue) {
264
+ for (const contentPart of outputValue) {
265
+ switch (contentPart.type) {
266
+ case "text":
267
+ parts.push({
268
+ functionResponse: {
269
+ name: toolName,
270
+ response: {
271
+ name: toolName,
272
+ content: contentPart.text
273
+ }
274
+ }
275
+ });
276
+ break;
277
+ case "image-data":
278
+ parts.push(
279
+ {
280
+ inlineData: {
281
+ mimeType: String(contentPart.mediaType),
282
+ data: String(contentPart.data)
283
+ }
284
+ },
285
+ {
286
+ text: "Tool executed successfully and returned this image as a response"
287
+ }
288
+ );
289
+ break;
290
+ default:
291
+ parts.push({ text: JSON.stringify(contentPart) });
292
+ break;
293
+ }
294
+ }
295
+ }
192
296
  function convertToGoogleGenerativeAIMessages(prompt, options) {
193
- var _a, _b, _c;
297
+ var _a, _b, _c, _d;
194
298
  const systemInstructionParts = [];
195
299
  const contents = [];
196
300
  let systemMessagesAllowed = true;
197
301
  const isGemmaModel = (_a = options == null ? void 0 : options.isGemmaModel) != null ? _a : false;
198
302
  const providerOptionsName = (_b = options == null ? void 0 : options.providerOptionsName) != null ? _b : "google";
303
+ const supportsFunctionResponseParts = (_c = options == null ? void 0 : options.supportsFunctionResponseParts) != null ? _c : true;
199
304
  for (const { role, content } of prompt) {
200
305
  switch (role) {
201
306
  case "system": {
@@ -243,8 +348,8 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
243
348
  contents.push({
244
349
  role: "model",
245
350
  parts: content.map((part) => {
246
- var _a2, _b2, _c2, _d;
247
- 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;
351
+ var _a2, _b2, _c2, _d2;
352
+ 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;
248
353
  const thoughtSignature = (providerOpts == null ? void 0 : providerOpts.thoughtSignature) != null ? String(providerOpts.thoughtSignature) : void 0;
249
354
  switch (part.type) {
250
355
  case "text": {
@@ -298,36 +403,10 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
298
403
  }
299
404
  const output = part.output;
300
405
  if (output.type === "content") {
301
- for (const contentPart of output.value) {
302
- switch (contentPart.type) {
303
- case "text":
304
- parts.push({
305
- functionResponse: {
306
- name: part.toolName,
307
- response: {
308
- name: part.toolName,
309
- content: contentPart.text
310
- }
311
- }
312
- });
313
- break;
314
- case "image-data":
315
- parts.push(
316
- {
317
- inlineData: {
318
- mimeType: contentPart.mediaType,
319
- data: contentPart.data
320
- }
321
- },
322
- {
323
- text: "Tool executed successfully and returned this image as a response"
324
- }
325
- );
326
- break;
327
- default:
328
- parts.push({ text: JSON.stringify(contentPart) });
329
- break;
330
- }
406
+ if (supportsFunctionResponseParts) {
407
+ appendToolResultParts(parts, part.toolName, output.value);
408
+ } else {
409
+ appendLegacyToolResultParts(parts, part.toolName, output.value);
331
410
  }
332
411
  } else {
333
412
  parts.push({
@@ -335,7 +414,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
335
414
  name: part.toolName,
336
415
  response: {
337
416
  name: part.toolName,
338
- content: output.type === "execution-denied" ? (_c = output.reason) != null ? _c : "Tool execution denied." : output.value
417
+ content: output.type === "execution-denied" ? (_d = output.reason) != null ? _d : "Tool execution denied." : output.value
339
418
  }
340
419
  }
341
420
  });
@@ -796,9 +875,14 @@ var GoogleGenerativeAILanguageModel = class {
796
875
  });
797
876
  }
798
877
  const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
878
+ const supportsFunctionResponseParts = this.modelId.startsWith("gemini-3");
799
879
  const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
800
880
  prompt,
801
- { isGemmaModel, providerOptionsName }
881
+ {
882
+ isGemmaModel,
883
+ providerOptionsName,
884
+ supportsFunctionResponseParts
885
+ }
802
886
  );
803
887
  const {
804
888
  tools: googleTools2,