@ai-sdk/google 3.0.135 → 3.0.136

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @ai-sdk/google
2
2
 
3
+ ## 3.0.136
4
+
5
+ ### Patch Changes
6
+
7
+ - 8fccc63: Preserve grounding metadata from each automatically continued Google response in `groundingMetadataPerRequest`. Previously, a later response without grounding could leave earlier Search queries absent from the final provider metadata even though their sources were returned. The new array preserves request order and includes `null` for requests without grounding.
8
+
3
9
  ## 3.0.135
4
10
 
5
11
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -241,6 +241,8 @@ type GoogleGenerativeAIUsageMetadata = UsageMetadataSchema;
241
241
  interface GoogleGenerativeAIProviderMetadata {
242
242
  promptFeedback: GoogleGenerativeAIPromptFeedback | null;
243
243
  groundingMetadata: GoogleGenerativeAIGroundingMetadata | null;
244
+ /** Grounding from each automatically continued request, in request order. */
245
+ groundingMetadataPerRequest?: Array<GoogleGenerativeAIGroundingMetadata | null>;
244
246
  urlContextMetadata: GoogleGenerativeAIUrlContextMetadata | null;
245
247
  safetyRatings: GoogleGenerativeAISafetyRating[] | null;
246
248
  usageMetadata: GoogleGenerativeAIUsageMetadata | null;
package/dist/index.d.ts CHANGED
@@ -241,6 +241,8 @@ type GoogleGenerativeAIUsageMetadata = UsageMetadataSchema;
241
241
  interface GoogleGenerativeAIProviderMetadata {
242
242
  promptFeedback: GoogleGenerativeAIPromptFeedback | null;
243
243
  groundingMetadata: GoogleGenerativeAIGroundingMetadata | null;
244
+ /** Grounding from each automatically continued request, in request order. */
245
+ groundingMetadataPerRequest?: Array<GoogleGenerativeAIGroundingMetadata | null>;
244
246
  urlContextMetadata: GoogleGenerativeAIUrlContextMetadata | null;
245
247
  safetyRatings: GoogleGenerativeAISafetyRating[] | null;
246
248
  usageMetadata: GoogleGenerativeAIUsageMetadata | null;
package/dist/index.js CHANGED
@@ -235,7 +235,7 @@ var googleInteractionsTranscriptionResponseSchema = import_v43.z.object({
235
235
  var import_provider_utils25 = require("@ai-sdk/provider-utils");
236
236
 
237
237
  // src/version.ts
238
- var VERSION = true ? "3.0.135" : "0.0.0-test";
238
+ var VERSION = true ? "3.0.136" : "0.0.0-test";
239
239
 
240
240
  // src/google-generative-ai-embedding-model.ts
241
241
  var import_provider2 = require("@ai-sdk/provider");
@@ -2295,7 +2295,7 @@ var GoogleGenerativeAILanguageModel = class {
2295
2295
  };
2296
2296
  }
2297
2297
  async doGenerate(options) {
2298
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A;
2298
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
2299
2299
  const { args, warnings, providerOptionsName, extraHeaders } = await this.getArgs(options);
2300
2300
  const mergedHeaders = (0, import_provider_utils8.combineHeaders)(
2301
2301
  await (0, import_provider_utils8.resolve)(this.config.headers),
@@ -2319,18 +2319,20 @@ var GoogleGenerativeAILanguageModel = class {
2319
2319
  rawValue: rawResponse
2320
2320
  } = await generateContent(args);
2321
2321
  const continuedParts = [];
2322
+ const groundingMetadataPerRequest = [];
2322
2323
  let continuedUsageMetadata;
2323
2324
  const firstPromptTokenCount = (_a2 = response.usageMetadata) == null ? void 0 : _a2.promptTokenCount;
2324
2325
  while (true) {
2325
2326
  const candidate2 = (_b = response.candidates) == null ? void 0 : _b[0];
2327
+ groundingMetadataPerRequest.push((_c = candidate2 == null ? void 0 : candidate2.groundingMetadata) != null ? _c : null);
2326
2328
  const continuationToken = (candidate2 == null ? void 0 : candidate2.finishReason) === "CONTINUATION" ? candidate2.continuationToken : void 0;
2327
2329
  if (continuationToken == null) {
2328
2330
  break;
2329
2331
  }
2330
- continuedParts.push(...(_d = (_c = candidate2 == null ? void 0 : candidate2.content) == null ? void 0 : _c.parts) != null ? _d : []);
2332
+ continuedParts.push(...(_e = (_d = candidate2 == null ? void 0 : candidate2.content) == null ? void 0 : _d.parts) != null ? _e : []);
2331
2333
  continuedUsageMetadata = addGoogleGenerativeAIUsageMetadata(
2332
2334
  continuedUsageMetadata,
2333
- (_e = response.usageMetadata) != null ? _e : void 0
2335
+ (_f = response.usageMetadata) != null ? _f : void 0
2334
2336
  );
2335
2337
  ({
2336
2338
  responseHeaders,
@@ -2338,23 +2340,23 @@ var GoogleGenerativeAILanguageModel = class {
2338
2340
  rawValue: rawResponse
2339
2341
  } = await generateContent({ ...args, continuationToken }));
2340
2342
  }
2341
- const candidate = (_f = response.candidates) == null ? void 0 : _f[0];
2342
- const promptBlockReason = (_g = response.promptFeedback) == null ? void 0 : _g.blockReason;
2343
+ const candidate = (_g = response.candidates) == null ? void 0 : _g[0];
2344
+ const promptBlockReason = (_h = response.promptFeedback) == null ? void 0 : _h.blockReason;
2343
2345
  const confirmedPromptBlockReason = isConfirmedPromptBlockReason(
2344
2346
  promptBlockReason
2345
2347
  ) ? promptBlockReason : void 0;
2346
2348
  const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && confirmedPromptBlockReason != null;
2347
- const rawFinishReason = (_h = candidate == null ? void 0 : candidate.finishReason) != null ? _h : confirmedPromptBlockReason;
2349
+ const rawFinishReason = (_i = candidate == null ? void 0 : candidate.finishReason) != null ? _i : confirmedPromptBlockReason;
2348
2350
  const content = [];
2349
- const parts = [...continuedParts, ...(_j = (_i = candidate == null ? void 0 : candidate.content) == null ? void 0 : _i.parts) != null ? _j : []];
2351
+ const parts = [...continuedParts, ...(_k = (_j = candidate == null ? void 0 : candidate.content) == null ? void 0 : _j.parts) != null ? _k : []];
2350
2352
  const usageMetadata = addGoogleGenerativeAIUsageMetadata(
2351
2353
  continuedUsageMetadata,
2352
- (_k = response.usageMetadata) != null ? _k : void 0
2354
+ (_l = response.usageMetadata) != null ? _l : void 0
2353
2355
  );
2354
2356
  let lastCodeExecutionToolCallId;
2355
2357
  let lastServerToolCallId;
2356
2358
  for (const part of parts) {
2357
- if ("executableCode" in part && ((_l = part.executableCode) == null ? void 0 : _l.code)) {
2359
+ if ("executableCode" in part && ((_m = part.executableCode) == null ? void 0 : _m.code)) {
2358
2360
  const toolCallId = this.config.generateId();
2359
2361
  lastCodeExecutionToolCallId = toolCallId;
2360
2362
  content.push({
@@ -2372,7 +2374,7 @@ var GoogleGenerativeAILanguageModel = class {
2372
2374
  toolName: "code_execution",
2373
2375
  result: {
2374
2376
  outcome: part.codeExecutionResult.outcome,
2375
- output: (_m = part.codeExecutionResult.output) != null ? _m : ""
2377
+ output: (_n = part.codeExecutionResult.output) != null ? _n : ""
2376
2378
  }
2377
2379
  });
2378
2380
  } else if ("text" in part && part.text != null) {
@@ -2396,9 +2398,9 @@ var GoogleGenerativeAILanguageModel = class {
2396
2398
  } else if ("functionCall" in part && part.functionCall.name != null) {
2397
2399
  content.push({
2398
2400
  type: "tool-call",
2399
- toolCallId: (_n = part.functionCall.id) != null ? _n : this.config.generateId(),
2401
+ toolCallId: (_o = part.functionCall.id) != null ? _o : this.config.generateId(),
2400
2402
  toolName: part.functionCall.name,
2401
- input: JSON.stringify((_o = part.functionCall.args) != null ? _o : {}),
2403
+ input: JSON.stringify((_p = part.functionCall.args) != null ? _p : {}),
2402
2404
  providerMetadata: part.thoughtSignature ? {
2403
2405
  [providerOptionsName]: {
2404
2406
  thoughtSignature: part.thoughtSignature
@@ -2420,13 +2422,13 @@ var GoogleGenerativeAILanguageModel = class {
2420
2422
  } : void 0
2421
2423
  });
2422
2424
  } else if ("toolCall" in part && part.toolCall) {
2423
- const toolCallId = (_p = part.toolCall.id) != null ? _p : this.config.generateId();
2425
+ const toolCallId = (_q = part.toolCall.id) != null ? _q : this.config.generateId();
2424
2426
  lastServerToolCallId = toolCallId;
2425
2427
  content.push({
2426
2428
  type: "tool-call",
2427
2429
  toolCallId,
2428
2430
  toolName: `server:${part.toolCall.toolType}`,
2429
- input: JSON.stringify((_q = part.toolCall.args) != null ? _q : {}),
2431
+ input: JSON.stringify((_r = part.toolCall.args) != null ? _r : {}),
2430
2432
  providerExecuted: true,
2431
2433
  dynamic: true,
2432
2434
  providerMetadata: part.thoughtSignature ? {
@@ -2443,12 +2445,12 @@ var GoogleGenerativeAILanguageModel = class {
2443
2445
  }
2444
2446
  });
2445
2447
  } else if ("toolResponse" in part && part.toolResponse) {
2446
- const responseToolCallId = (_r = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _r : this.config.generateId();
2448
+ const responseToolCallId = (_s = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _s : this.config.generateId();
2447
2449
  content.push({
2448
2450
  type: "tool-result",
2449
2451
  toolCallId: responseToolCallId,
2450
2452
  toolName: `server:${part.toolResponse.toolType}`,
2451
- result: (_s = part.toolResponse.response) != null ? _s : {},
2453
+ result: (_t = part.toolResponse.response) != null ? _t : {},
2452
2454
  providerMetadata: part.thoughtSignature ? {
2453
2455
  [providerOptionsName]: {
2454
2456
  thoughtSignature: part.thoughtSignature,
@@ -2465,10 +2467,10 @@ var GoogleGenerativeAILanguageModel = class {
2465
2467
  lastServerToolCallId = void 0;
2466
2468
  }
2467
2469
  }
2468
- const sources = (_t = extractSources({
2470
+ const sources = (_u = extractSources({
2469
2471
  groundingMetadata: candidate == null ? void 0 : candidate.groundingMetadata,
2470
2472
  generateId: this.config.generateId
2471
- })) != null ? _t : [];
2473
+ })) != null ? _u : [];
2472
2474
  for (const source of sources) {
2473
2475
  content.push(source);
2474
2476
  }
@@ -2488,13 +2490,14 @@ var GoogleGenerativeAILanguageModel = class {
2488
2490
  warnings,
2489
2491
  providerMetadata: {
2490
2492
  [providerOptionsName]: {
2491
- promptFeedback: (_u = response.promptFeedback) != null ? _u : null,
2492
- groundingMetadata: (_v = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _v : null,
2493
- urlContextMetadata: (_w = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _w : null,
2494
- safetyRatings: (_x = candidate == null ? void 0 : candidate.safetyRatings) != null ? _x : null,
2493
+ promptFeedback: (_v = response.promptFeedback) != null ? _v : null,
2494
+ groundingMetadata: (_w = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _w : null,
2495
+ ...groundingMetadataPerRequest.length > 1 ? { groundingMetadataPerRequest } : {},
2496
+ urlContextMetadata: (_x = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _x : null,
2497
+ safetyRatings: (_y = candidate == null ? void 0 : candidate.safetyRatings) != null ? _y : null,
2495
2498
  usageMetadata: usageMetadata != null ? usageMetadata : null,
2496
- finishMessage: (_y = candidate == null ? void 0 : candidate.finishMessage) != null ? _y : null,
2497
- serviceTier: (_z = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _z : null,
2499
+ finishMessage: (_z = candidate == null ? void 0 : candidate.finishMessage) != null ? _z : null,
2500
+ serviceTier: (_A = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _A : null,
2498
2501
  ...(candidate == null ? void 0 : candidate.continuationToken) != null && rawFinishReason === "MAX_TOKENS" ? { continuationToken: candidate.continuationToken } : {},
2499
2502
  ...continuedUsageMetadata != null && firstPromptTokenCount != null ? { initialPromptTokenCount: firstPromptTokenCount } : {}
2500
2503
  }
@@ -2502,7 +2505,7 @@ var GoogleGenerativeAILanguageModel = class {
2502
2505
  request: { body: args },
2503
2506
  response: {
2504
2507
  // TODO timestamp, model id
2505
- id: (_A = response.responseId) != null ? _A : void 0,
2508
+ id: (_B = response.responseId) != null ? _B : void 0,
2506
2509
  headers: responseHeaders,
2507
2510
  body: rawResponse
2508
2511
  }
@@ -2527,14 +2530,27 @@ var GoogleGenerativeAILanguageModel = class {
2527
2530
  fetch: this.config.fetch
2528
2531
  });
2529
2532
  const { responseHeaders, value: firstResponse } = await streamGenerateContent(args);
2533
+ const groundingMetadataPerRequest = [];
2534
+ let requestGroundingMetadata = null;
2530
2535
  const response = continueGoogleStream({
2531
2536
  stream: firstResponse,
2532
2537
  getContinuationToken: (chunk) => {
2533
2538
  var _a2, _b;
2534
2539
  const candidate = (_a2 = chunk.candidates) == null ? void 0 : _a2[0];
2540
+ if ((candidate == null ? void 0 : candidate.groundingMetadata) != null) {
2541
+ requestGroundingMetadata = candidate.groundingMetadata;
2542
+ }
2535
2543
  return (candidate == null ? void 0 : candidate.finishReason) === "CONTINUATION" ? (_b = candidate.continuationToken) != null ? _b : void 0 : void 0;
2536
2544
  },
2537
- continueStream: async (continuationToken) => (await streamGenerateContent({ ...args, continuationToken })).value
2545
+ continueStream: async (continuationToken) => {
2546
+ const { value } = await streamGenerateContent({
2547
+ ...args,
2548
+ continuationToken
2549
+ });
2550
+ groundingMetadataPerRequest.push(requestGroundingMetadata);
2551
+ requestGroundingMetadata = null;
2552
+ return value;
2553
+ }
2538
2554
  });
2539
2555
  let finishReason = {
2540
2556
  unified: "other",
@@ -2969,6 +2985,12 @@ var GoogleGenerativeAILanguageModel = class {
2969
2985
  [providerOptionsName]: {
2970
2986
  promptFeedback,
2971
2987
  groundingMetadata: lastGroundingMetadata,
2988
+ ...groundingMetadataPerRequest.length > 0 ? {
2989
+ groundingMetadataPerRequest: [
2990
+ ...groundingMetadataPerRequest,
2991
+ requestGroundingMetadata
2992
+ ]
2993
+ } : {},
2972
2994
  urlContextMetadata: lastUrlContextMetadata,
2973
2995
  safetyRatings: lastSafetyRatings,
2974
2996
  usageMetadata: usage != null ? usage : null,