@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.
@@ -112,6 +112,7 @@ declare const responseSchema: _ai_sdk_provider_utils.LazySchema<{
112
112
  })[] | null | undefined;
113
113
  } | null | undefined;
114
114
  finishReason?: string | null | undefined;
115
+ finishMessage?: string | null | undefined;
115
116
  safetyRatings?: {
116
117
  category?: string | null | undefined;
117
118
  probability?: string | null | undefined;
@@ -197,6 +198,8 @@ declare const responseSchema: _ai_sdk_provider_utils.LazySchema<{
197
198
  type GroundingMetadataSchema = NonNullable<InferSchema<typeof responseSchema>['candidates'][number]['groundingMetadata']>;
198
199
  type UrlContextMetadataSchema = NonNullable<InferSchema<typeof responseSchema>['candidates'][number]['urlContextMetadata']>;
199
200
  type SafetyRatingSchema = NonNullable<InferSchema<typeof responseSchema>['candidates'][number]['safetyRatings']>[number];
201
+ type PromptFeedbackSchema = NonNullable<InferSchema<typeof responseSchema>['promptFeedback']>;
202
+ type UsageMetadataSchema = NonNullable<InferSchema<typeof responseSchema>['usageMetadata']>;
200
203
 
201
204
  declare const googleTools: {
202
205
  /**
@@ -281,4 +284,4 @@ declare const googleTools: {
281
284
  }>;
282
285
  };
283
286
 
284
- export { GoogleGenerativeAILanguageModel, type GoogleGenerativeAIModelId, type GroundingMetadataSchema, type SafetyRatingSchema, type UrlContextMetadataSchema, getGroundingMetadataSchema, getUrlContextMetadataSchema, googleTools };
287
+ export { GoogleGenerativeAILanguageModel, type GoogleGenerativeAIModelId, type GroundingMetadataSchema, type PromptFeedbackSchema, type SafetyRatingSchema, type UrlContextMetadataSchema, type UsageMetadataSchema, getGroundingMetadataSchema, getUrlContextMetadataSchema, googleTools };
@@ -112,6 +112,7 @@ declare const responseSchema: _ai_sdk_provider_utils.LazySchema<{
112
112
  })[] | null | undefined;
113
113
  } | null | undefined;
114
114
  finishReason?: string | null | undefined;
115
+ finishMessage?: string | null | undefined;
115
116
  safetyRatings?: {
116
117
  category?: string | null | undefined;
117
118
  probability?: string | null | undefined;
@@ -197,6 +198,8 @@ declare const responseSchema: _ai_sdk_provider_utils.LazySchema<{
197
198
  type GroundingMetadataSchema = NonNullable<InferSchema<typeof responseSchema>['candidates'][number]['groundingMetadata']>;
198
199
  type UrlContextMetadataSchema = NonNullable<InferSchema<typeof responseSchema>['candidates'][number]['urlContextMetadata']>;
199
200
  type SafetyRatingSchema = NonNullable<InferSchema<typeof responseSchema>['candidates'][number]['safetyRatings']>[number];
201
+ type PromptFeedbackSchema = NonNullable<InferSchema<typeof responseSchema>['promptFeedback']>;
202
+ type UsageMetadataSchema = NonNullable<InferSchema<typeof responseSchema>['usageMetadata']>;
200
203
 
201
204
  declare const googleTools: {
202
205
  /**
@@ -281,4 +284,4 @@ declare const googleTools: {
281
284
  }>;
282
285
  };
283
286
 
284
- export { GoogleGenerativeAILanguageModel, type GoogleGenerativeAIModelId, type GroundingMetadataSchema, type SafetyRatingSchema, type UrlContextMetadataSchema, getGroundingMetadataSchema, getUrlContextMetadataSchema, googleTools };
287
+ export { GoogleGenerativeAILanguageModel, type GoogleGenerativeAIModelId, type GroundingMetadataSchema, type PromptFeedbackSchema, type SafetyRatingSchema, type UrlContextMetadataSchema, type UsageMetadataSchema, getGroundingMetadataSchema, getUrlContextMetadataSchema, googleTools };
@@ -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,
@@ -856,7 +940,7 @@ var GoogleGenerativeAILanguageModel = class {
856
940
  };
857
941
  }
858
942
  async doGenerate(options) {
859
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
943
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
860
944
  const { args, warnings, providerOptionsName } = await this.getArgs(options);
861
945
  const mergedHeaders = (0, import_provider_utils4.combineHeaders)(
862
946
  await (0, import_provider_utils4.resolve)(this.config.headers),
@@ -978,7 +1062,8 @@ var GoogleGenerativeAILanguageModel = class {
978
1062
  groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
979
1063
  urlContextMetadata: (_i = candidate.urlContextMetadata) != null ? _i : null,
980
1064
  safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
981
- usageMetadata: usageMetadata != null ? usageMetadata : null
1065
+ usageMetadata: usageMetadata != null ? usageMetadata : null,
1066
+ finishMessage: (_k = candidate.finishMessage) != null ? _k : null
982
1067
  }
983
1068
  },
984
1069
  request: { body: args },
@@ -1028,7 +1113,7 @@ var GoogleGenerativeAILanguageModel = class {
1028
1113
  controller.enqueue({ type: "stream-start", warnings });
1029
1114
  },
1030
1115
  transform(chunk, controller) {
1031
- var _a, _b, _c, _d, _e, _f;
1116
+ var _a, _b, _c, _d, _e, _f, _g;
1032
1117
  if (options.includeRawChunks) {
1033
1118
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1034
1119
  }
@@ -1230,12 +1315,11 @@ var GoogleGenerativeAILanguageModel = class {
1230
1315
  promptFeedback: (_e = value.promptFeedback) != null ? _e : null,
1231
1316
  groundingMetadata: lastGroundingMetadata,
1232
1317
  urlContextMetadata: lastUrlContextMetadata,
1233
- safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null
1318
+ safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null,
1319
+ usageMetadata: usageMetadata != null ? usageMetadata : null,
1320
+ finishMessage: (_g = candidate.finishMessage) != null ? _g : null
1234
1321
  }
1235
1322
  };
1236
- if (usageMetadata != null) {
1237
- providerMetadata[providerOptionsName].usageMetadata = usageMetadata;
1238
- }
1239
1323
  }
1240
1324
  },
1241
1325
  flush(controller) {
@@ -1495,6 +1579,7 @@ var responseSchema = (0, import_provider_utils4.lazySchema)(
1495
1579
  import_v43.z.object({
1496
1580
  content: getContentSchema().nullish().or(import_v43.z.object({}).strict()),
1497
1581
  finishReason: import_v43.z.string().nullish(),
1582
+ finishMessage: import_v43.z.string().nullish(),
1498
1583
  safetyRatings: import_v43.z.array(getSafetyRatingSchema()).nullish(),
1499
1584
  groundingMetadata: getGroundingMetadataSchema().nullish(),
1500
1585
  urlContextMetadata: getUrlContextMetadataSchema().nullish()
@@ -1515,6 +1600,7 @@ var chunkSchema = (0, import_provider_utils4.lazySchema)(
1515
1600
  import_v43.z.object({
1516
1601
  content: getContentSchema().nullish(),
1517
1602
  finishReason: import_v43.z.string().nullish(),
1603
+ finishMessage: import_v43.z.string().nullish(),
1518
1604
  safetyRatings: import_v43.z.array(getSafetyRatingSchema()).nullish(),
1519
1605
  groundingMetadata: getGroundingMetadataSchema().nullish(),
1520
1606
  urlContextMetadata: getUrlContextMetadataSchema().nullish()