@ai-sdk/google 3.0.122 → 3.0.123

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.123
4
+
5
+ ### Patch Changes
6
+
7
+ - c36c87c: fix(google): preserve prompt feedback and metadata across streaming chunks
8
+
3
9
  ## 3.0.122
4
10
 
5
11
  ### Patch Changes
package/dist/index.js CHANGED
@@ -235,7 +235,7 @@ var googleInteractionsTranscriptionResponseSchema = import_v43.z.object({
235
235
  var import_provider_utils24 = require("@ai-sdk/provider-utils");
236
236
 
237
237
  // src/version.ts
238
- var VERSION = true ? "3.0.122" : "0.0.0-test";
238
+ var VERSION = true ? "3.0.123" : "0.0.0-test";
239
239
 
240
240
  // src/google-generative-ai-embedding-model.ts
241
241
  var import_provider2 = require("@ai-sdk/provider");
@@ -2263,9 +2263,12 @@ var GoogleGenerativeAILanguageModel = class {
2263
2263
  raw: void 0
2264
2264
  };
2265
2265
  let usage = void 0;
2266
- let providerMetadata = void 0;
2266
+ let promptFeedback = null;
2267
2267
  let lastGroundingMetadata = null;
2268
2268
  let lastUrlContextMetadata = null;
2269
+ let lastSafetyRatings = null;
2270
+ let lastFinishMessage = null;
2271
+ let confirmedPromptBlockReason;
2269
2272
  const generateId3 = this.config.generateId;
2270
2273
  let hasToolCalls = false;
2271
2274
  let hasEmittedResponseMetadata = false;
@@ -2311,7 +2314,7 @@ var GoogleGenerativeAILanguageModel = class {
2311
2314
  controller.enqueue({ type: "stream-start", warnings });
2312
2315
  },
2313
2316
  transform(chunk, controller) {
2314
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
2317
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
2315
2318
  if (options.includeRawChunks) {
2316
2319
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2317
2320
  }
@@ -2331,35 +2334,35 @@ var GoogleGenerativeAILanguageModel = class {
2331
2334
  if (usageMetadata != null) {
2332
2335
  usage = usageMetadata;
2333
2336
  }
2334
- const candidate = (_a = value.candidates) == null ? void 0 : _a[0];
2335
- if (candidate == null) {
2336
- const promptBlockReason2 = (_b = value.promptFeedback) == null ? void 0 : _b.blockReason;
2337
- if (promptBlockReason2 != null) {
2337
+ if (value.promptFeedback != null && confirmedPromptBlockReason == null) {
2338
+ promptFeedback = value.promptFeedback;
2339
+ if (isConfirmedPromptBlockReason(value.promptFeedback.blockReason)) {
2340
+ confirmedPromptBlockReason = value.promptFeedback.blockReason;
2338
2341
  finishReason = {
2339
2342
  unified: "content-filter",
2340
- raw: promptBlockReason2
2341
- };
2342
- providerMetadata = {
2343
- [providerOptionsName]: {
2344
- promptFeedback: (_c = value.promptFeedback) != null ? _c : null,
2345
- groundingMetadata: lastGroundingMetadata,
2346
- urlContextMetadata: lastUrlContextMetadata,
2347
- safetyRatings: null,
2348
- usageMetadata: usageMetadata != null ? usageMetadata : null,
2349
- finishMessage: null,
2350
- serviceTier: (_d = usage == null ? void 0 : usage.serviceTier) != null ? _d : null
2351
- }
2343
+ raw: confirmedPromptBlockReason
2352
2344
  };
2353
2345
  }
2354
- return;
2355
2346
  }
2356
- const content = candidate.content;
2357
- if (candidate.groundingMetadata != null) {
2358
- lastGroundingMetadata = candidate.groundingMetadata;
2347
+ const candidate = (_a = value.candidates) == null ? void 0 : _a[0];
2348
+ if (candidate != null) {
2349
+ if (candidate.groundingMetadata != null) {
2350
+ lastGroundingMetadata = candidate.groundingMetadata;
2351
+ }
2352
+ if (candidate.urlContextMetadata != null) {
2353
+ lastUrlContextMetadata = candidate.urlContextMetadata;
2354
+ }
2355
+ if (candidate.safetyRatings != null) {
2356
+ lastSafetyRatings = candidate.safetyRatings;
2357
+ }
2358
+ if (candidate.finishMessage != null) {
2359
+ lastFinishMessage = candidate.finishMessage;
2360
+ }
2359
2361
  }
2360
- if (candidate.urlContextMetadata != null) {
2361
- lastUrlContextMetadata = candidate.urlContextMetadata;
2362
+ if (confirmedPromptBlockReason != null || candidate == null) {
2363
+ return;
2362
2364
  }
2365
+ const content = candidate.content;
2363
2366
  const sources = extractSources({
2364
2367
  groundingMetadata: candidate.groundingMetadata,
2365
2368
  generateId: generateId3
@@ -2373,9 +2376,9 @@ var GoogleGenerativeAILanguageModel = class {
2373
2376
  }
2374
2377
  }
2375
2378
  if (content != null) {
2376
- const parts = (_e = content.parts) != null ? _e : [];
2379
+ const parts = (_b = content.parts) != null ? _b : [];
2377
2380
  for (const part of parts) {
2378
- if ("executableCode" in part && ((_f = part.executableCode) == null ? void 0 : _f.code)) {
2381
+ if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
2379
2382
  const toolCallId = generateId3();
2380
2383
  lastCodeExecutionToolCallId = toolCallId;
2381
2384
  controller.enqueue({
@@ -2394,7 +2397,7 @@ var GoogleGenerativeAILanguageModel = class {
2394
2397
  toolName: "code_execution",
2395
2398
  result: {
2396
2399
  outcome: part.codeExecutionResult.outcome,
2397
- output: (_g = part.codeExecutionResult.output) != null ? _g : ""
2400
+ output: (_d = part.codeExecutionResult.output) != null ? _d : ""
2398
2401
  }
2399
2402
  });
2400
2403
  }
@@ -2488,7 +2491,7 @@ var GoogleGenerativeAILanguageModel = class {
2488
2491
  providerMetadata: fileMeta
2489
2492
  });
2490
2493
  } else if ("toolCall" in part && part.toolCall) {
2491
- const toolCallId = (_h = part.toolCall.id) != null ? _h : generateId3();
2494
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId3();
2492
2495
  lastServerToolCallId = toolCallId;
2493
2496
  const serverMeta = {
2494
2497
  [providerOptionsName]: {
@@ -2501,13 +2504,13 @@ var GoogleGenerativeAILanguageModel = class {
2501
2504
  type: "tool-call",
2502
2505
  toolCallId,
2503
2506
  toolName: `server:${part.toolCall.toolType}`,
2504
- input: JSON.stringify((_i = part.toolCall.args) != null ? _i : {}),
2507
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
2505
2508
  providerExecuted: true,
2506
2509
  dynamic: true,
2507
2510
  providerMetadata: serverMeta
2508
2511
  });
2509
2512
  } else if ("toolResponse" in part && part.toolResponse) {
2510
- const responseToolCallId = (_j = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _j : generateId3();
2513
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId3();
2511
2514
  const serverMeta = {
2512
2515
  [providerOptionsName]: {
2513
2516
  ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
@@ -2519,7 +2522,7 @@ var GoogleGenerativeAILanguageModel = class {
2519
2522
  type: "tool-result",
2520
2523
  toolCallId: responseToolCallId,
2521
2524
  toolName: `server:${part.toolResponse.toolType}`,
2522
- result: (_k = part.toolResponse.response) != null ? _k : {},
2525
+ result: (_h = part.toolResponse.response) != null ? _h : {},
2523
2526
  providerMetadata: serverMeta
2524
2527
  });
2525
2528
  lastServerToolCallId = void 0;
@@ -2538,7 +2541,7 @@ var GoogleGenerativeAILanguageModel = class {
2538
2541
  const isNoArgsCompleteCall = part.functionCall.name != null && part.functionCall.args == null && part.functionCall.partialArgs == null && part.functionCall.willContinue !== true;
2539
2542
  if (isStreamingChunk) {
2540
2543
  if (part.functionCall.name != null) {
2541
- const toolCallId = (_l = part.functionCall.id) != null ? _l : generateId3();
2544
+ const toolCallId = (_i = part.functionCall.id) != null ? _i : generateId3();
2542
2545
  const accumulator = new GoogleJSONAccumulator();
2543
2546
  activeStreamingToolCalls.push({
2544
2547
  toolCallId,
@@ -2586,9 +2589,9 @@ var GoogleGenerativeAILanguageModel = class {
2586
2589
  } else if (isTerminalChunk && activeStreamingToolCalls.length > 0) {
2587
2590
  finishActiveStreamingToolCall(controller);
2588
2591
  } else if (isCompleteCall) {
2589
- const toolCallId = (_m = part.functionCall.id) != null ? _m : generateId3();
2592
+ const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId3();
2590
2593
  const toolName = part.functionCall.name;
2591
- const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_n = part.functionCall.args) != null ? _n : {});
2594
+ const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_k = part.functionCall.args) != null ? _k : {});
2592
2595
  controller.enqueue({
2593
2596
  type: "tool-input-start",
2594
2597
  id: toolCallId,
@@ -2615,7 +2618,7 @@ var GoogleGenerativeAILanguageModel = class {
2615
2618
  });
2616
2619
  hasToolCalls = true;
2617
2620
  } else if (isNoArgsCompleteCall) {
2618
- const toolCallId = (_o = part.functionCall.id) != null ? _o : generateId3();
2621
+ const toolCallId = (_l = part.functionCall.id) != null ? _l : generateId3();
2619
2622
  const toolName = part.functionCall.name;
2620
2623
  controller.enqueue({
2621
2624
  type: "tool-input-start",
@@ -2639,31 +2642,18 @@ var GoogleGenerativeAILanguageModel = class {
2639
2642
  }
2640
2643
  }
2641
2644
  }
2642
- const promptBlockReason = (_p = value.promptFeedback) == null ? void 0 : _p.blockReason;
2643
- const isPromptBlocked = candidate.finishReason == null && promptBlockReason != null;
2644
- const rawFinishReason = (_r = (_q = candidate.finishReason) != null ? _q : promptBlockReason) != null ? _r : void 0;
2645
- if (rawFinishReason != null) {
2645
+ if (candidate.finishReason != null) {
2646
2646
  finishReason = {
2647
- unified: isPromptBlocked ? "content-filter" : mapGoogleGenerativeAIFinishReason({
2648
- finishReason: rawFinishReason,
2647
+ unified: mapGoogleGenerativeAIFinishReason({
2648
+ finishReason: candidate.finishReason,
2649
2649
  hasToolCalls
2650
2650
  }),
2651
- raw: rawFinishReason
2652
- };
2653
- providerMetadata = {
2654
- [providerOptionsName]: {
2655
- promptFeedback: (_s = value.promptFeedback) != null ? _s : null,
2656
- groundingMetadata: lastGroundingMetadata,
2657
- urlContextMetadata: lastUrlContextMetadata,
2658
- safetyRatings: (_t = candidate.safetyRatings) != null ? _t : null,
2659
- usageMetadata: usageMetadata != null ? usageMetadata : null,
2660
- finishMessage: (_u = candidate.finishMessage) != null ? _u : null,
2661
- serviceTier: (_v = usage == null ? void 0 : usage.serviceTier) != null ? _v : null
2662
- }
2651
+ raw: candidate.finishReason
2663
2652
  };
2664
2653
  }
2665
2654
  },
2666
2655
  flush(controller) {
2656
+ var _a;
2667
2657
  if (currentTextBlockId !== null) {
2668
2658
  controller.enqueue({
2669
2659
  type: "text-end",
@@ -2680,7 +2670,17 @@ var GoogleGenerativeAILanguageModel = class {
2680
2670
  type: "finish",
2681
2671
  finishReason,
2682
2672
  usage: convertGoogleGenerativeAIUsage(usage),
2683
- providerMetadata
2673
+ providerMetadata: {
2674
+ [providerOptionsName]: {
2675
+ promptFeedback,
2676
+ groundingMetadata: lastGroundingMetadata,
2677
+ urlContextMetadata: lastUrlContextMetadata,
2678
+ safetyRatings: lastSafetyRatings,
2679
+ usageMetadata: usage != null ? usage : null,
2680
+ finishMessage: lastFinishMessage,
2681
+ serviceTier: (_a = usage == null ? void 0 : usage.serviceTier) != null ? _a : null
2682
+ }
2683
+ }
2684
2684
  });
2685
2685
  }
2686
2686
  })
@@ -2977,6 +2977,9 @@ var chunkSchema = (0, import_provider_utils7.lazySchema)(
2977
2977
  })
2978
2978
  )
2979
2979
  );
2980
+ function isConfirmedPromptBlockReason(blockReason) {
2981
+ return blockReason != null && blockReason !== "" && blockReason !== "BLOCK_REASON_UNSPECIFIED" && blockReason !== "BLOCKED_REASON_UNSPECIFIED";
2982
+ }
2980
2983
 
2981
2984
  // src/tool/code-execution.ts
2982
2985
  var import_provider_utils8 = require("@ai-sdk/provider-utils");