@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/dist/index.mjs CHANGED
@@ -224,7 +224,7 @@ import {
224
224
  } from "@ai-sdk/provider-utils";
225
225
 
226
226
  // src/version.ts
227
- var VERSION = true ? "3.0.135" : "0.0.0-test";
227
+ var VERSION = true ? "3.0.136" : "0.0.0-test";
228
228
 
229
229
  // src/google-generative-ai-embedding-model.ts
230
230
  import {
@@ -2325,7 +2325,7 @@ var GoogleGenerativeAILanguageModel = class {
2325
2325
  };
2326
2326
  }
2327
2327
  async doGenerate(options) {
2328
- 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;
2328
+ 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;
2329
2329
  const { args, warnings, providerOptionsName, extraHeaders } = await this.getArgs(options);
2330
2330
  const mergedHeaders = combineHeaders3(
2331
2331
  await resolve3(this.config.headers),
@@ -2349,18 +2349,20 @@ var GoogleGenerativeAILanguageModel = class {
2349
2349
  rawValue: rawResponse
2350
2350
  } = await generateContent(args);
2351
2351
  const continuedParts = [];
2352
+ const groundingMetadataPerRequest = [];
2352
2353
  let continuedUsageMetadata;
2353
2354
  const firstPromptTokenCount = (_a2 = response.usageMetadata) == null ? void 0 : _a2.promptTokenCount;
2354
2355
  while (true) {
2355
2356
  const candidate2 = (_b = response.candidates) == null ? void 0 : _b[0];
2357
+ groundingMetadataPerRequest.push((_c = candidate2 == null ? void 0 : candidate2.groundingMetadata) != null ? _c : null);
2356
2358
  const continuationToken = (candidate2 == null ? void 0 : candidate2.finishReason) === "CONTINUATION" ? candidate2.continuationToken : void 0;
2357
2359
  if (continuationToken == null) {
2358
2360
  break;
2359
2361
  }
2360
- continuedParts.push(...(_d = (_c = candidate2 == null ? void 0 : candidate2.content) == null ? void 0 : _c.parts) != null ? _d : []);
2362
+ continuedParts.push(...(_e = (_d = candidate2 == null ? void 0 : candidate2.content) == null ? void 0 : _d.parts) != null ? _e : []);
2361
2363
  continuedUsageMetadata = addGoogleGenerativeAIUsageMetadata(
2362
2364
  continuedUsageMetadata,
2363
- (_e = response.usageMetadata) != null ? _e : void 0
2365
+ (_f = response.usageMetadata) != null ? _f : void 0
2364
2366
  );
2365
2367
  ({
2366
2368
  responseHeaders,
@@ -2368,23 +2370,23 @@ var GoogleGenerativeAILanguageModel = class {
2368
2370
  rawValue: rawResponse
2369
2371
  } = await generateContent({ ...args, continuationToken }));
2370
2372
  }
2371
- const candidate = (_f = response.candidates) == null ? void 0 : _f[0];
2372
- const promptBlockReason = (_g = response.promptFeedback) == null ? void 0 : _g.blockReason;
2373
+ const candidate = (_g = response.candidates) == null ? void 0 : _g[0];
2374
+ const promptBlockReason = (_h = response.promptFeedback) == null ? void 0 : _h.blockReason;
2373
2375
  const confirmedPromptBlockReason = isConfirmedPromptBlockReason(
2374
2376
  promptBlockReason
2375
2377
  ) ? promptBlockReason : void 0;
2376
2378
  const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && confirmedPromptBlockReason != null;
2377
- const rawFinishReason = (_h = candidate == null ? void 0 : candidate.finishReason) != null ? _h : confirmedPromptBlockReason;
2379
+ const rawFinishReason = (_i = candidate == null ? void 0 : candidate.finishReason) != null ? _i : confirmedPromptBlockReason;
2378
2380
  const content = [];
2379
- const parts = [...continuedParts, ...(_j = (_i = candidate == null ? void 0 : candidate.content) == null ? void 0 : _i.parts) != null ? _j : []];
2381
+ const parts = [...continuedParts, ...(_k = (_j = candidate == null ? void 0 : candidate.content) == null ? void 0 : _j.parts) != null ? _k : []];
2380
2382
  const usageMetadata = addGoogleGenerativeAIUsageMetadata(
2381
2383
  continuedUsageMetadata,
2382
- (_k = response.usageMetadata) != null ? _k : void 0
2384
+ (_l = response.usageMetadata) != null ? _l : void 0
2383
2385
  );
2384
2386
  let lastCodeExecutionToolCallId;
2385
2387
  let lastServerToolCallId;
2386
2388
  for (const part of parts) {
2387
- if ("executableCode" in part && ((_l = part.executableCode) == null ? void 0 : _l.code)) {
2389
+ if ("executableCode" in part && ((_m = part.executableCode) == null ? void 0 : _m.code)) {
2388
2390
  const toolCallId = this.config.generateId();
2389
2391
  lastCodeExecutionToolCallId = toolCallId;
2390
2392
  content.push({
@@ -2402,7 +2404,7 @@ var GoogleGenerativeAILanguageModel = class {
2402
2404
  toolName: "code_execution",
2403
2405
  result: {
2404
2406
  outcome: part.codeExecutionResult.outcome,
2405
- output: (_m = part.codeExecutionResult.output) != null ? _m : ""
2407
+ output: (_n = part.codeExecutionResult.output) != null ? _n : ""
2406
2408
  }
2407
2409
  });
2408
2410
  } else if ("text" in part && part.text != null) {
@@ -2426,9 +2428,9 @@ var GoogleGenerativeAILanguageModel = class {
2426
2428
  } else if ("functionCall" in part && part.functionCall.name != null) {
2427
2429
  content.push({
2428
2430
  type: "tool-call",
2429
- toolCallId: (_n = part.functionCall.id) != null ? _n : this.config.generateId(),
2431
+ toolCallId: (_o = part.functionCall.id) != null ? _o : this.config.generateId(),
2430
2432
  toolName: part.functionCall.name,
2431
- input: JSON.stringify((_o = part.functionCall.args) != null ? _o : {}),
2433
+ input: JSON.stringify((_p = part.functionCall.args) != null ? _p : {}),
2432
2434
  providerMetadata: part.thoughtSignature ? {
2433
2435
  [providerOptionsName]: {
2434
2436
  thoughtSignature: part.thoughtSignature
@@ -2450,13 +2452,13 @@ var GoogleGenerativeAILanguageModel = class {
2450
2452
  } : void 0
2451
2453
  });
2452
2454
  } else if ("toolCall" in part && part.toolCall) {
2453
- const toolCallId = (_p = part.toolCall.id) != null ? _p : this.config.generateId();
2455
+ const toolCallId = (_q = part.toolCall.id) != null ? _q : this.config.generateId();
2454
2456
  lastServerToolCallId = toolCallId;
2455
2457
  content.push({
2456
2458
  type: "tool-call",
2457
2459
  toolCallId,
2458
2460
  toolName: `server:${part.toolCall.toolType}`,
2459
- input: JSON.stringify((_q = part.toolCall.args) != null ? _q : {}),
2461
+ input: JSON.stringify((_r = part.toolCall.args) != null ? _r : {}),
2460
2462
  providerExecuted: true,
2461
2463
  dynamic: true,
2462
2464
  providerMetadata: part.thoughtSignature ? {
@@ -2473,12 +2475,12 @@ var GoogleGenerativeAILanguageModel = class {
2473
2475
  }
2474
2476
  });
2475
2477
  } else if ("toolResponse" in part && part.toolResponse) {
2476
- const responseToolCallId = (_r = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _r : this.config.generateId();
2478
+ const responseToolCallId = (_s = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _s : this.config.generateId();
2477
2479
  content.push({
2478
2480
  type: "tool-result",
2479
2481
  toolCallId: responseToolCallId,
2480
2482
  toolName: `server:${part.toolResponse.toolType}`,
2481
- result: (_s = part.toolResponse.response) != null ? _s : {},
2483
+ result: (_t = part.toolResponse.response) != null ? _t : {},
2482
2484
  providerMetadata: part.thoughtSignature ? {
2483
2485
  [providerOptionsName]: {
2484
2486
  thoughtSignature: part.thoughtSignature,
@@ -2495,10 +2497,10 @@ var GoogleGenerativeAILanguageModel = class {
2495
2497
  lastServerToolCallId = void 0;
2496
2498
  }
2497
2499
  }
2498
- const sources = (_t = extractSources({
2500
+ const sources = (_u = extractSources({
2499
2501
  groundingMetadata: candidate == null ? void 0 : candidate.groundingMetadata,
2500
2502
  generateId: this.config.generateId
2501
- })) != null ? _t : [];
2503
+ })) != null ? _u : [];
2502
2504
  for (const source of sources) {
2503
2505
  content.push(source);
2504
2506
  }
@@ -2518,13 +2520,14 @@ var GoogleGenerativeAILanguageModel = class {
2518
2520
  warnings,
2519
2521
  providerMetadata: {
2520
2522
  [providerOptionsName]: {
2521
- promptFeedback: (_u = response.promptFeedback) != null ? _u : null,
2522
- groundingMetadata: (_v = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _v : null,
2523
- urlContextMetadata: (_w = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _w : null,
2524
- safetyRatings: (_x = candidate == null ? void 0 : candidate.safetyRatings) != null ? _x : null,
2523
+ promptFeedback: (_v = response.promptFeedback) != null ? _v : null,
2524
+ groundingMetadata: (_w = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _w : null,
2525
+ ...groundingMetadataPerRequest.length > 1 ? { groundingMetadataPerRequest } : {},
2526
+ urlContextMetadata: (_x = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _x : null,
2527
+ safetyRatings: (_y = candidate == null ? void 0 : candidate.safetyRatings) != null ? _y : null,
2525
2528
  usageMetadata: usageMetadata != null ? usageMetadata : null,
2526
- finishMessage: (_y = candidate == null ? void 0 : candidate.finishMessage) != null ? _y : null,
2527
- serviceTier: (_z = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _z : null,
2529
+ finishMessage: (_z = candidate == null ? void 0 : candidate.finishMessage) != null ? _z : null,
2530
+ serviceTier: (_A = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _A : null,
2528
2531
  ...(candidate == null ? void 0 : candidate.continuationToken) != null && rawFinishReason === "MAX_TOKENS" ? { continuationToken: candidate.continuationToken } : {},
2529
2532
  ...continuedUsageMetadata != null && firstPromptTokenCount != null ? { initialPromptTokenCount: firstPromptTokenCount } : {}
2530
2533
  }
@@ -2532,7 +2535,7 @@ var GoogleGenerativeAILanguageModel = class {
2532
2535
  request: { body: args },
2533
2536
  response: {
2534
2537
  // TODO timestamp, model id
2535
- id: (_A = response.responseId) != null ? _A : void 0,
2538
+ id: (_B = response.responseId) != null ? _B : void 0,
2536
2539
  headers: responseHeaders,
2537
2540
  body: rawResponse
2538
2541
  }
@@ -2557,14 +2560,27 @@ var GoogleGenerativeAILanguageModel = class {
2557
2560
  fetch: this.config.fetch
2558
2561
  });
2559
2562
  const { responseHeaders, value: firstResponse } = await streamGenerateContent(args);
2563
+ const groundingMetadataPerRequest = [];
2564
+ let requestGroundingMetadata = null;
2560
2565
  const response = continueGoogleStream({
2561
2566
  stream: firstResponse,
2562
2567
  getContinuationToken: (chunk) => {
2563
2568
  var _a2, _b;
2564
2569
  const candidate = (_a2 = chunk.candidates) == null ? void 0 : _a2[0];
2570
+ if ((candidate == null ? void 0 : candidate.groundingMetadata) != null) {
2571
+ requestGroundingMetadata = candidate.groundingMetadata;
2572
+ }
2565
2573
  return (candidate == null ? void 0 : candidate.finishReason) === "CONTINUATION" ? (_b = candidate.continuationToken) != null ? _b : void 0 : void 0;
2566
2574
  },
2567
- continueStream: async (continuationToken) => (await streamGenerateContent({ ...args, continuationToken })).value
2575
+ continueStream: async (continuationToken) => {
2576
+ const { value } = await streamGenerateContent({
2577
+ ...args,
2578
+ continuationToken
2579
+ });
2580
+ groundingMetadataPerRequest.push(requestGroundingMetadata);
2581
+ requestGroundingMetadata = null;
2582
+ return value;
2583
+ }
2568
2584
  });
2569
2585
  let finishReason = {
2570
2586
  unified: "other",
@@ -2999,6 +3015,12 @@ var GoogleGenerativeAILanguageModel = class {
2999
3015
  [providerOptionsName]: {
3000
3016
  promptFeedback,
3001
3017
  groundingMetadata: lastGroundingMetadata,
3018
+ ...groundingMetadataPerRequest.length > 0 ? {
3019
+ groundingMetadataPerRequest: [
3020
+ ...groundingMetadataPerRequest,
3021
+ requestGroundingMetadata
3022
+ ]
3023
+ } : {},
3002
3024
  urlContextMetadata: lastUrlContextMetadata,
3003
3025
  safetyRatings: lastSafetyRatings,
3004
3026
  usageMetadata: usage != null ? usage : null,