@ai-sdk/google 4.0.0-beta.14 → 4.0.0-beta.16

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.
@@ -175,13 +175,118 @@ import {
175
175
  UnsupportedFunctionalityError
176
176
  } from "@ai-sdk/provider";
177
177
  import { convertToBase64 } from "@ai-sdk/provider-utils";
178
+ var dataUrlRegex = /^data:([^;,]+);base64,(.+)$/s;
179
+ function parseBase64DataUrl(value) {
180
+ const match = dataUrlRegex.exec(value);
181
+ if (match == null) {
182
+ return void 0;
183
+ }
184
+ return {
185
+ mediaType: match[1],
186
+ data: match[2]
187
+ };
188
+ }
189
+ function convertUrlToolResultPart(url) {
190
+ const parsedDataUrl = parseBase64DataUrl(url);
191
+ if (parsedDataUrl == null) {
192
+ return void 0;
193
+ }
194
+ return {
195
+ inlineData: {
196
+ mimeType: parsedDataUrl.mediaType,
197
+ data: parsedDataUrl.data
198
+ }
199
+ };
200
+ }
201
+ function appendToolResultParts(parts, toolName, outputValue) {
202
+ const functionResponseParts = [];
203
+ const responseTextParts = [];
204
+ for (const contentPart of outputValue) {
205
+ switch (contentPart.type) {
206
+ case "text": {
207
+ responseTextParts.push(contentPart.text);
208
+ break;
209
+ }
210
+ case "image-data":
211
+ case "file-data": {
212
+ functionResponseParts.push({
213
+ inlineData: {
214
+ mimeType: contentPart.mediaType,
215
+ data: contentPart.data
216
+ }
217
+ });
218
+ break;
219
+ }
220
+ case "image-url":
221
+ case "file-url": {
222
+ const functionResponsePart = convertUrlToolResultPart(
223
+ contentPart.url
224
+ );
225
+ if (functionResponsePart != null) {
226
+ functionResponseParts.push(functionResponsePart);
227
+ } else {
228
+ responseTextParts.push(JSON.stringify(contentPart));
229
+ }
230
+ break;
231
+ }
232
+ default: {
233
+ responseTextParts.push(JSON.stringify(contentPart));
234
+ break;
235
+ }
236
+ }
237
+ }
238
+ parts.push({
239
+ functionResponse: {
240
+ name: toolName,
241
+ response: {
242
+ name: toolName,
243
+ content: responseTextParts.length > 0 ? responseTextParts.join("\n") : "Tool executed successfully."
244
+ },
245
+ ...functionResponseParts.length > 0 ? { parts: functionResponseParts } : {}
246
+ }
247
+ });
248
+ }
249
+ function appendLegacyToolResultParts(parts, toolName, outputValue) {
250
+ for (const contentPart of outputValue) {
251
+ switch (contentPart.type) {
252
+ case "text":
253
+ parts.push({
254
+ functionResponse: {
255
+ name: toolName,
256
+ response: {
257
+ name: toolName,
258
+ content: contentPart.text
259
+ }
260
+ }
261
+ });
262
+ break;
263
+ case "image-data":
264
+ parts.push(
265
+ {
266
+ inlineData: {
267
+ mimeType: String(contentPart.mediaType),
268
+ data: String(contentPart.data)
269
+ }
270
+ },
271
+ {
272
+ text: "Tool executed successfully and returned this image as a response"
273
+ }
274
+ );
275
+ break;
276
+ default:
277
+ parts.push({ text: JSON.stringify(contentPart) });
278
+ break;
279
+ }
280
+ }
281
+ }
178
282
  function convertToGoogleGenerativeAIMessages(prompt, options) {
179
- var _a, _b, _c;
283
+ var _a, _b, _c, _d;
180
284
  const systemInstructionParts = [];
181
285
  const contents = [];
182
286
  let systemMessagesAllowed = true;
183
287
  const isGemmaModel = (_a = options == null ? void 0 : options.isGemmaModel) != null ? _a : false;
184
288
  const providerOptionsName = (_b = options == null ? void 0 : options.providerOptionsName) != null ? _b : "google";
289
+ const supportsFunctionResponseParts = (_c = options == null ? void 0 : options.supportsFunctionResponseParts) != null ? _c : true;
185
290
  for (const { role, content } of prompt) {
186
291
  switch (role) {
187
292
  case "system": {
@@ -229,8 +334,8 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
229
334
  contents.push({
230
335
  role: "model",
231
336
  parts: content.map((part) => {
232
- var _a2, _b2, _c2, _d;
233
- 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;
337
+ var _a2, _b2, _c2, _d2;
338
+ 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;
234
339
  const thoughtSignature = (providerOpts == null ? void 0 : providerOpts.thoughtSignature) != null ? String(providerOpts.thoughtSignature) : void 0;
235
340
  switch (part.type) {
236
341
  case "text": {
@@ -299,36 +404,10 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
299
404
  }
300
405
  const output = part.output;
301
406
  if (output.type === "content") {
302
- for (const contentPart of output.value) {
303
- switch (contentPart.type) {
304
- case "text":
305
- parts.push({
306
- functionResponse: {
307
- name: part.toolName,
308
- response: {
309
- name: part.toolName,
310
- content: contentPart.text
311
- }
312
- }
313
- });
314
- break;
315
- case "image-data":
316
- parts.push(
317
- {
318
- inlineData: {
319
- mimeType: contentPart.mediaType,
320
- data: contentPart.data
321
- }
322
- },
323
- {
324
- text: "Tool executed successfully and returned this image as a response"
325
- }
326
- );
327
- break;
328
- default:
329
- parts.push({ text: JSON.stringify(contentPart) });
330
- break;
331
- }
407
+ if (supportsFunctionResponseParts) {
408
+ appendToolResultParts(parts, part.toolName, output.value);
409
+ } else {
410
+ appendLegacyToolResultParts(parts, part.toolName, output.value);
332
411
  }
333
412
  } else {
334
413
  parts.push({
@@ -336,7 +415,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
336
415
  name: part.toolName,
337
416
  response: {
338
417
  name: part.toolName,
339
- content: output.type === "execution-denied" ? (_c = output.reason) != null ? _c : "Tool execution denied." : output.value
418
+ content: output.type === "execution-denied" ? (_d = output.reason) != null ? _d : "Tool execution denied." : output.value
340
419
  }
341
420
  }
342
421
  });
@@ -804,9 +883,14 @@ var GoogleGenerativeAILanguageModel = class {
804
883
  });
805
884
  }
806
885
  const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
886
+ const supportsFunctionResponseParts = this.modelId.startsWith("gemini-3");
807
887
  const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
808
888
  prompt,
809
- { isGemmaModel, providerOptionsName }
889
+ {
890
+ isGemmaModel,
891
+ providerOptionsName,
892
+ supportsFunctionResponseParts
893
+ }
810
894
  );
811
895
  const {
812
896
  tools: googleTools2,