@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.
@@ -172,13 +172,118 @@ import {
172
172
  UnsupportedFunctionalityError
173
173
  } from "@ai-sdk/provider";
174
174
  import { convertToBase64 } from "@ai-sdk/provider-utils";
175
+ var dataUrlRegex = /^data:([^;,]+);base64,(.+)$/s;
176
+ function parseBase64DataUrl(value) {
177
+ const match = dataUrlRegex.exec(value);
178
+ if (match == null) {
179
+ return void 0;
180
+ }
181
+ return {
182
+ mediaType: match[1],
183
+ data: match[2]
184
+ };
185
+ }
186
+ function convertUrlToolResultPart(url) {
187
+ const parsedDataUrl = parseBase64DataUrl(url);
188
+ if (parsedDataUrl == null) {
189
+ return void 0;
190
+ }
191
+ return {
192
+ inlineData: {
193
+ mimeType: parsedDataUrl.mediaType,
194
+ data: parsedDataUrl.data
195
+ }
196
+ };
197
+ }
198
+ function appendToolResultParts(parts, toolName, outputValue) {
199
+ const functionResponseParts = [];
200
+ const responseTextParts = [];
201
+ for (const contentPart of outputValue) {
202
+ switch (contentPart.type) {
203
+ case "text": {
204
+ responseTextParts.push(contentPart.text);
205
+ break;
206
+ }
207
+ case "image-data":
208
+ case "file-data": {
209
+ functionResponseParts.push({
210
+ inlineData: {
211
+ mimeType: contentPart.mediaType,
212
+ data: contentPart.data
213
+ }
214
+ });
215
+ break;
216
+ }
217
+ case "image-url":
218
+ case "file-url": {
219
+ const functionResponsePart = convertUrlToolResultPart(
220
+ contentPart.url
221
+ );
222
+ if (functionResponsePart != null) {
223
+ functionResponseParts.push(functionResponsePart);
224
+ } else {
225
+ responseTextParts.push(JSON.stringify(contentPart));
226
+ }
227
+ break;
228
+ }
229
+ default: {
230
+ responseTextParts.push(JSON.stringify(contentPart));
231
+ break;
232
+ }
233
+ }
234
+ }
235
+ parts.push({
236
+ functionResponse: {
237
+ name: toolName,
238
+ response: {
239
+ name: toolName,
240
+ content: responseTextParts.length > 0 ? responseTextParts.join("\n") : "Tool executed successfully."
241
+ },
242
+ ...functionResponseParts.length > 0 ? { parts: functionResponseParts } : {}
243
+ }
244
+ });
245
+ }
246
+ function appendLegacyToolResultParts(parts, toolName, outputValue) {
247
+ for (const contentPart of outputValue) {
248
+ switch (contentPart.type) {
249
+ case "text":
250
+ parts.push({
251
+ functionResponse: {
252
+ name: toolName,
253
+ response: {
254
+ name: toolName,
255
+ content: contentPart.text
256
+ }
257
+ }
258
+ });
259
+ break;
260
+ case "image-data":
261
+ parts.push(
262
+ {
263
+ inlineData: {
264
+ mimeType: String(contentPart.mediaType),
265
+ data: String(contentPart.data)
266
+ }
267
+ },
268
+ {
269
+ text: "Tool executed successfully and returned this image as a response"
270
+ }
271
+ );
272
+ break;
273
+ default:
274
+ parts.push({ text: JSON.stringify(contentPart) });
275
+ break;
276
+ }
277
+ }
278
+ }
175
279
  function convertToGoogleGenerativeAIMessages(prompt, options) {
176
- var _a, _b, _c;
280
+ var _a, _b, _c, _d;
177
281
  const systemInstructionParts = [];
178
282
  const contents = [];
179
283
  let systemMessagesAllowed = true;
180
284
  const isGemmaModel = (_a = options == null ? void 0 : options.isGemmaModel) != null ? _a : false;
181
285
  const providerOptionsName = (_b = options == null ? void 0 : options.providerOptionsName) != null ? _b : "google";
286
+ const supportsFunctionResponseParts = (_c = options == null ? void 0 : options.supportsFunctionResponseParts) != null ? _c : true;
182
287
  for (const { role, content } of prompt) {
183
288
  switch (role) {
184
289
  case "system": {
@@ -226,8 +331,8 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
226
331
  contents.push({
227
332
  role: "model",
228
333
  parts: content.map((part) => {
229
- var _a2, _b2, _c2, _d;
230
- 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;
334
+ var _a2, _b2, _c2, _d2;
335
+ 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;
231
336
  const thoughtSignature = (providerOpts == null ? void 0 : providerOpts.thoughtSignature) != null ? String(providerOpts.thoughtSignature) : void 0;
232
337
  switch (part.type) {
233
338
  case "text": {
@@ -281,36 +386,10 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
281
386
  }
282
387
  const output = part.output;
283
388
  if (output.type === "content") {
284
- for (const contentPart of output.value) {
285
- switch (contentPart.type) {
286
- case "text":
287
- parts.push({
288
- functionResponse: {
289
- name: part.toolName,
290
- response: {
291
- name: part.toolName,
292
- content: contentPart.text
293
- }
294
- }
295
- });
296
- break;
297
- case "image-data":
298
- parts.push(
299
- {
300
- inlineData: {
301
- mimeType: contentPart.mediaType,
302
- data: contentPart.data
303
- }
304
- },
305
- {
306
- text: "Tool executed successfully and returned this image as a response"
307
- }
308
- );
309
- break;
310
- default:
311
- parts.push({ text: JSON.stringify(contentPart) });
312
- break;
313
- }
389
+ if (supportsFunctionResponseParts) {
390
+ appendToolResultParts(parts, part.toolName, output.value);
391
+ } else {
392
+ appendLegacyToolResultParts(parts, part.toolName, output.value);
314
393
  }
315
394
  } else {
316
395
  parts.push({
@@ -318,7 +397,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
318
397
  name: part.toolName,
319
398
  response: {
320
399
  name: part.toolName,
321
- content: output.type === "execution-denied" ? (_c = output.reason) != null ? _c : "Tool execution denied." : output.value
400
+ content: output.type === "execution-denied" ? (_d = output.reason) != null ? _d : "Tool execution denied." : output.value
322
401
  }
323
402
  }
324
403
  });
@@ -785,9 +864,14 @@ var GoogleGenerativeAILanguageModel = class {
785
864
  });
786
865
  }
787
866
  const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
867
+ const supportsFunctionResponseParts = this.modelId.startsWith("gemini-3");
788
868
  const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
789
869
  prompt,
790
- { isGemmaModel, providerOptionsName }
870
+ {
871
+ isGemmaModel,
872
+ providerOptionsName,
873
+ supportsFunctionResponseParts
874
+ }
791
875
  );
792
876
  const {
793
877
  tools: googleTools2,