@ai-sdk/google 3.0.50 → 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,
@@ -845,7 +929,7 @@ var GoogleGenerativeAILanguageModel = class {
845
929
  };
846
930
  }
847
931
  async doGenerate(options) {
848
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
932
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
849
933
  const { args, warnings, providerOptionsName } = await this.getArgs(options);
850
934
  const mergedHeaders = combineHeaders(
851
935
  await resolve(this.config.headers),
@@ -967,7 +1051,8 @@ var GoogleGenerativeAILanguageModel = class {
967
1051
  groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
968
1052
  urlContextMetadata: (_i = candidate.urlContextMetadata) != null ? _i : null,
969
1053
  safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
970
- usageMetadata: usageMetadata != null ? usageMetadata : null
1054
+ usageMetadata: usageMetadata != null ? usageMetadata : null,
1055
+ finishMessage: (_k = candidate.finishMessage) != null ? _k : null
971
1056
  }
972
1057
  },
973
1058
  request: { body: args },
@@ -1017,7 +1102,7 @@ var GoogleGenerativeAILanguageModel = class {
1017
1102
  controller.enqueue({ type: "stream-start", warnings });
1018
1103
  },
1019
1104
  transform(chunk, controller) {
1020
- var _a, _b, _c, _d, _e, _f;
1105
+ var _a, _b, _c, _d, _e, _f, _g;
1021
1106
  if (options.includeRawChunks) {
1022
1107
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1023
1108
  }
@@ -1219,12 +1304,11 @@ var GoogleGenerativeAILanguageModel = class {
1219
1304
  promptFeedback: (_e = value.promptFeedback) != null ? _e : null,
1220
1305
  groundingMetadata: lastGroundingMetadata,
1221
1306
  urlContextMetadata: lastUrlContextMetadata,
1222
- safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null
1307
+ safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null,
1308
+ usageMetadata: usageMetadata != null ? usageMetadata : null,
1309
+ finishMessage: (_g = candidate.finishMessage) != null ? _g : null
1223
1310
  }
1224
1311
  };
1225
- if (usageMetadata != null) {
1226
- providerMetadata[providerOptionsName].usageMetadata = usageMetadata;
1227
- }
1228
1312
  }
1229
1313
  },
1230
1314
  flush(controller) {
@@ -1484,6 +1568,7 @@ var responseSchema = lazySchema3(
1484
1568
  z3.object({
1485
1569
  content: getContentSchema().nullish().or(z3.object({}).strict()),
1486
1570
  finishReason: z3.string().nullish(),
1571
+ finishMessage: z3.string().nullish(),
1487
1572
  safetyRatings: z3.array(getSafetyRatingSchema()).nullish(),
1488
1573
  groundingMetadata: getGroundingMetadataSchema().nullish(),
1489
1574
  urlContextMetadata: getUrlContextMetadataSchema().nullish()
@@ -1504,6 +1589,7 @@ var chunkSchema = lazySchema3(
1504
1589
  z3.object({
1505
1590
  content: getContentSchema().nullish(),
1506
1591
  finishReason: z3.string().nullish(),
1592
+ finishMessage: z3.string().nullish(),
1507
1593
  safetyRatings: z3.array(getSafetyRatingSchema()).nullish(),
1508
1594
  groundingMetadata: getGroundingMetadataSchema().nullish(),
1509
1595
  urlContextMetadata: getUrlContextMetadataSchema().nullish()