@ai-sdk/google 3.0.122 → 3.0.124

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,17 @@
1
1
  # @ai-sdk/google
2
2
 
3
+ ## 3.0.124
4
+
5
+ ### Patch Changes
6
+
7
+ - 9548d8e: fix(google): ignore default prompt block reasons in non-streaming responses
8
+
9
+ ## 3.0.123
10
+
11
+ ### Patch Changes
12
+
13
+ - c36c87c: fix(google): preserve prompt feedback and metadata across streaming chunks
14
+
3
15
  ## 3.0.122
4
16
 
5
17
  ### 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.124" : "0.0.0-test";
239
239
 
240
240
  // src/google-generative-ai-embedding-model.ts
241
241
  var import_provider2 = require("@ai-sdk/provider");
@@ -2056,7 +2056,7 @@ var GoogleGenerativeAILanguageModel = class {
2056
2056
  };
2057
2057
  }
2058
2058
  async doGenerate(options) {
2059
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
2059
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
2060
2060
  const { args, warnings, providerOptionsName, extraHeaders } = await this.getArgs(options);
2061
2061
  const mergedHeaders = (0, import_provider_utils7.combineHeaders)(
2062
2062
  await (0, import_provider_utils7.resolve)(this.config.headers),
@@ -2080,15 +2080,18 @@ var GoogleGenerativeAILanguageModel = class {
2080
2080
  });
2081
2081
  const candidate = (_a = response.candidates) == null ? void 0 : _a[0];
2082
2082
  const promptBlockReason = (_b = response.promptFeedback) == null ? void 0 : _b.blockReason;
2083
- const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && promptBlockReason != null;
2084
- const rawFinishReason = (_d = (_c = candidate == null ? void 0 : candidate.finishReason) != null ? _c : promptBlockReason) != null ? _d : void 0;
2083
+ const confirmedPromptBlockReason = isConfirmedPromptBlockReason(
2084
+ promptBlockReason
2085
+ ) ? promptBlockReason : void 0;
2086
+ const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && confirmedPromptBlockReason != null;
2087
+ const rawFinishReason = (_c = candidate == null ? void 0 : candidate.finishReason) != null ? _c : confirmedPromptBlockReason;
2085
2088
  const content = [];
2086
- const parts = (_f = (_e = candidate == null ? void 0 : candidate.content) == null ? void 0 : _e.parts) != null ? _f : [];
2089
+ const parts = (_e = (_d = candidate == null ? void 0 : candidate.content) == null ? void 0 : _d.parts) != null ? _e : [];
2087
2090
  const usageMetadata = response.usageMetadata;
2088
2091
  let lastCodeExecutionToolCallId;
2089
2092
  let lastServerToolCallId;
2090
2093
  for (const part of parts) {
2091
- if ("executableCode" in part && ((_g = part.executableCode) == null ? void 0 : _g.code)) {
2094
+ if ("executableCode" in part && ((_f = part.executableCode) == null ? void 0 : _f.code)) {
2092
2095
  const toolCallId = this.config.generateId();
2093
2096
  lastCodeExecutionToolCallId = toolCallId;
2094
2097
  content.push({
@@ -2106,7 +2109,7 @@ var GoogleGenerativeAILanguageModel = class {
2106
2109
  toolName: "code_execution",
2107
2110
  result: {
2108
2111
  outcome: part.codeExecutionResult.outcome,
2109
- output: (_h = part.codeExecutionResult.output) != null ? _h : ""
2112
+ output: (_g = part.codeExecutionResult.output) != null ? _g : ""
2110
2113
  }
2111
2114
  });
2112
2115
  } else if ("text" in part && part.text != null) {
@@ -2130,9 +2133,9 @@ var GoogleGenerativeAILanguageModel = class {
2130
2133
  } else if ("functionCall" in part && part.functionCall.name != null) {
2131
2134
  content.push({
2132
2135
  type: "tool-call",
2133
- toolCallId: (_i = part.functionCall.id) != null ? _i : this.config.generateId(),
2136
+ toolCallId: (_h = part.functionCall.id) != null ? _h : this.config.generateId(),
2134
2137
  toolName: part.functionCall.name,
2135
- input: JSON.stringify((_j = part.functionCall.args) != null ? _j : {}),
2138
+ input: JSON.stringify((_i = part.functionCall.args) != null ? _i : {}),
2136
2139
  providerMetadata: part.thoughtSignature ? {
2137
2140
  [providerOptionsName]: {
2138
2141
  thoughtSignature: part.thoughtSignature
@@ -2154,13 +2157,13 @@ var GoogleGenerativeAILanguageModel = class {
2154
2157
  } : void 0
2155
2158
  });
2156
2159
  } else if ("toolCall" in part && part.toolCall) {
2157
- const toolCallId = (_k = part.toolCall.id) != null ? _k : this.config.generateId();
2160
+ const toolCallId = (_j = part.toolCall.id) != null ? _j : this.config.generateId();
2158
2161
  lastServerToolCallId = toolCallId;
2159
2162
  content.push({
2160
2163
  type: "tool-call",
2161
2164
  toolCallId,
2162
2165
  toolName: `server:${part.toolCall.toolType}`,
2163
- input: JSON.stringify((_l = part.toolCall.args) != null ? _l : {}),
2166
+ input: JSON.stringify((_k = part.toolCall.args) != null ? _k : {}),
2164
2167
  providerExecuted: true,
2165
2168
  dynamic: true,
2166
2169
  providerMetadata: part.thoughtSignature ? {
@@ -2177,12 +2180,12 @@ var GoogleGenerativeAILanguageModel = class {
2177
2180
  }
2178
2181
  });
2179
2182
  } else if ("toolResponse" in part && part.toolResponse) {
2180
- const responseToolCallId = (_m = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _m : this.config.generateId();
2183
+ const responseToolCallId = (_l = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _l : this.config.generateId();
2181
2184
  content.push({
2182
2185
  type: "tool-result",
2183
2186
  toolCallId: responseToolCallId,
2184
2187
  toolName: `server:${part.toolResponse.toolType}`,
2185
- result: (_n = part.toolResponse.response) != null ? _n : {},
2188
+ result: (_m = part.toolResponse.response) != null ? _m : {},
2186
2189
  providerMetadata: part.thoughtSignature ? {
2187
2190
  [providerOptionsName]: {
2188
2191
  thoughtSignature: part.thoughtSignature,
@@ -2199,10 +2202,10 @@ var GoogleGenerativeAILanguageModel = class {
2199
2202
  lastServerToolCallId = void 0;
2200
2203
  }
2201
2204
  }
2202
- const sources = (_o = extractSources({
2205
+ const sources = (_n = extractSources({
2203
2206
  groundingMetadata: candidate == null ? void 0 : candidate.groundingMetadata,
2204
2207
  generateId: this.config.generateId
2205
- })) != null ? _o : [];
2208
+ })) != null ? _n : [];
2206
2209
  for (const source of sources) {
2207
2210
  content.push(source);
2208
2211
  }
@@ -2222,19 +2225,19 @@ var GoogleGenerativeAILanguageModel = class {
2222
2225
  warnings,
2223
2226
  providerMetadata: {
2224
2227
  [providerOptionsName]: {
2225
- promptFeedback: (_p = response.promptFeedback) != null ? _p : null,
2226
- groundingMetadata: (_q = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _q : null,
2227
- urlContextMetadata: (_r = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _r : null,
2228
- safetyRatings: (_s = candidate == null ? void 0 : candidate.safetyRatings) != null ? _s : null,
2228
+ promptFeedback: (_o = response.promptFeedback) != null ? _o : null,
2229
+ groundingMetadata: (_p = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _p : null,
2230
+ urlContextMetadata: (_q = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _q : null,
2231
+ safetyRatings: (_r = candidate == null ? void 0 : candidate.safetyRatings) != null ? _r : null,
2229
2232
  usageMetadata: usageMetadata != null ? usageMetadata : null,
2230
- finishMessage: (_t = candidate == null ? void 0 : candidate.finishMessage) != null ? _t : null,
2231
- serviceTier: (_u = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _u : null
2233
+ finishMessage: (_s = candidate == null ? void 0 : candidate.finishMessage) != null ? _s : null,
2234
+ serviceTier: (_t = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _t : null
2232
2235
  }
2233
2236
  },
2234
2237
  request: { body: args },
2235
2238
  response: {
2236
2239
  // TODO timestamp, model id
2237
- id: (_v = response.responseId) != null ? _v : void 0,
2240
+ id: (_u = response.responseId) != null ? _u : void 0,
2238
2241
  headers: responseHeaders,
2239
2242
  body: rawResponse
2240
2243
  }
@@ -2263,9 +2266,12 @@ var GoogleGenerativeAILanguageModel = class {
2263
2266
  raw: void 0
2264
2267
  };
2265
2268
  let usage = void 0;
2266
- let providerMetadata = void 0;
2269
+ let promptFeedback = null;
2267
2270
  let lastGroundingMetadata = null;
2268
2271
  let lastUrlContextMetadata = null;
2272
+ let lastSafetyRatings = null;
2273
+ let lastFinishMessage = null;
2274
+ let confirmedPromptBlockReason;
2269
2275
  const generateId3 = this.config.generateId;
2270
2276
  let hasToolCalls = false;
2271
2277
  let hasEmittedResponseMetadata = false;
@@ -2311,7 +2317,7 @@ var GoogleGenerativeAILanguageModel = class {
2311
2317
  controller.enqueue({ type: "stream-start", warnings });
2312
2318
  },
2313
2319
  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;
2320
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
2315
2321
  if (options.includeRawChunks) {
2316
2322
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2317
2323
  }
@@ -2331,35 +2337,35 @@ var GoogleGenerativeAILanguageModel = class {
2331
2337
  if (usageMetadata != null) {
2332
2338
  usage = usageMetadata;
2333
2339
  }
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) {
2340
+ if (value.promptFeedback != null && confirmedPromptBlockReason == null) {
2341
+ promptFeedback = value.promptFeedback;
2342
+ if (isConfirmedPromptBlockReason(value.promptFeedback.blockReason)) {
2343
+ confirmedPromptBlockReason = value.promptFeedback.blockReason;
2338
2344
  finishReason = {
2339
2345
  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
- }
2346
+ raw: confirmedPromptBlockReason
2352
2347
  };
2353
2348
  }
2354
- return;
2355
2349
  }
2356
- const content = candidate.content;
2357
- if (candidate.groundingMetadata != null) {
2358
- lastGroundingMetadata = candidate.groundingMetadata;
2350
+ const candidate = (_a = value.candidates) == null ? void 0 : _a[0];
2351
+ if (candidate != null) {
2352
+ if (candidate.groundingMetadata != null) {
2353
+ lastGroundingMetadata = candidate.groundingMetadata;
2354
+ }
2355
+ if (candidate.urlContextMetadata != null) {
2356
+ lastUrlContextMetadata = candidate.urlContextMetadata;
2357
+ }
2358
+ if (candidate.safetyRatings != null) {
2359
+ lastSafetyRatings = candidate.safetyRatings;
2360
+ }
2361
+ if (candidate.finishMessage != null) {
2362
+ lastFinishMessage = candidate.finishMessage;
2363
+ }
2359
2364
  }
2360
- if (candidate.urlContextMetadata != null) {
2361
- lastUrlContextMetadata = candidate.urlContextMetadata;
2365
+ if (confirmedPromptBlockReason != null || candidate == null) {
2366
+ return;
2362
2367
  }
2368
+ const content = candidate.content;
2363
2369
  const sources = extractSources({
2364
2370
  groundingMetadata: candidate.groundingMetadata,
2365
2371
  generateId: generateId3
@@ -2373,9 +2379,9 @@ var GoogleGenerativeAILanguageModel = class {
2373
2379
  }
2374
2380
  }
2375
2381
  if (content != null) {
2376
- const parts = (_e = content.parts) != null ? _e : [];
2382
+ const parts = (_b = content.parts) != null ? _b : [];
2377
2383
  for (const part of parts) {
2378
- if ("executableCode" in part && ((_f = part.executableCode) == null ? void 0 : _f.code)) {
2384
+ if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
2379
2385
  const toolCallId = generateId3();
2380
2386
  lastCodeExecutionToolCallId = toolCallId;
2381
2387
  controller.enqueue({
@@ -2394,7 +2400,7 @@ var GoogleGenerativeAILanguageModel = class {
2394
2400
  toolName: "code_execution",
2395
2401
  result: {
2396
2402
  outcome: part.codeExecutionResult.outcome,
2397
- output: (_g = part.codeExecutionResult.output) != null ? _g : ""
2403
+ output: (_d = part.codeExecutionResult.output) != null ? _d : ""
2398
2404
  }
2399
2405
  });
2400
2406
  }
@@ -2488,7 +2494,7 @@ var GoogleGenerativeAILanguageModel = class {
2488
2494
  providerMetadata: fileMeta
2489
2495
  });
2490
2496
  } else if ("toolCall" in part && part.toolCall) {
2491
- const toolCallId = (_h = part.toolCall.id) != null ? _h : generateId3();
2497
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId3();
2492
2498
  lastServerToolCallId = toolCallId;
2493
2499
  const serverMeta = {
2494
2500
  [providerOptionsName]: {
@@ -2501,13 +2507,13 @@ var GoogleGenerativeAILanguageModel = class {
2501
2507
  type: "tool-call",
2502
2508
  toolCallId,
2503
2509
  toolName: `server:${part.toolCall.toolType}`,
2504
- input: JSON.stringify((_i = part.toolCall.args) != null ? _i : {}),
2510
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
2505
2511
  providerExecuted: true,
2506
2512
  dynamic: true,
2507
2513
  providerMetadata: serverMeta
2508
2514
  });
2509
2515
  } else if ("toolResponse" in part && part.toolResponse) {
2510
- const responseToolCallId = (_j = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _j : generateId3();
2516
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId3();
2511
2517
  const serverMeta = {
2512
2518
  [providerOptionsName]: {
2513
2519
  ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
@@ -2519,7 +2525,7 @@ var GoogleGenerativeAILanguageModel = class {
2519
2525
  type: "tool-result",
2520
2526
  toolCallId: responseToolCallId,
2521
2527
  toolName: `server:${part.toolResponse.toolType}`,
2522
- result: (_k = part.toolResponse.response) != null ? _k : {},
2528
+ result: (_h = part.toolResponse.response) != null ? _h : {},
2523
2529
  providerMetadata: serverMeta
2524
2530
  });
2525
2531
  lastServerToolCallId = void 0;
@@ -2538,7 +2544,7 @@ var GoogleGenerativeAILanguageModel = class {
2538
2544
  const isNoArgsCompleteCall = part.functionCall.name != null && part.functionCall.args == null && part.functionCall.partialArgs == null && part.functionCall.willContinue !== true;
2539
2545
  if (isStreamingChunk) {
2540
2546
  if (part.functionCall.name != null) {
2541
- const toolCallId = (_l = part.functionCall.id) != null ? _l : generateId3();
2547
+ const toolCallId = (_i = part.functionCall.id) != null ? _i : generateId3();
2542
2548
  const accumulator = new GoogleJSONAccumulator();
2543
2549
  activeStreamingToolCalls.push({
2544
2550
  toolCallId,
@@ -2586,9 +2592,9 @@ var GoogleGenerativeAILanguageModel = class {
2586
2592
  } else if (isTerminalChunk && activeStreamingToolCalls.length > 0) {
2587
2593
  finishActiveStreamingToolCall(controller);
2588
2594
  } else if (isCompleteCall) {
2589
- const toolCallId = (_m = part.functionCall.id) != null ? _m : generateId3();
2595
+ const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId3();
2590
2596
  const toolName = part.functionCall.name;
2591
- const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_n = part.functionCall.args) != null ? _n : {});
2597
+ const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_k = part.functionCall.args) != null ? _k : {});
2592
2598
  controller.enqueue({
2593
2599
  type: "tool-input-start",
2594
2600
  id: toolCallId,
@@ -2615,7 +2621,7 @@ var GoogleGenerativeAILanguageModel = class {
2615
2621
  });
2616
2622
  hasToolCalls = true;
2617
2623
  } else if (isNoArgsCompleteCall) {
2618
- const toolCallId = (_o = part.functionCall.id) != null ? _o : generateId3();
2624
+ const toolCallId = (_l = part.functionCall.id) != null ? _l : generateId3();
2619
2625
  const toolName = part.functionCall.name;
2620
2626
  controller.enqueue({
2621
2627
  type: "tool-input-start",
@@ -2639,31 +2645,18 @@ var GoogleGenerativeAILanguageModel = class {
2639
2645
  }
2640
2646
  }
2641
2647
  }
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) {
2648
+ if (candidate.finishReason != null) {
2646
2649
  finishReason = {
2647
- unified: isPromptBlocked ? "content-filter" : mapGoogleGenerativeAIFinishReason({
2648
- finishReason: rawFinishReason,
2650
+ unified: mapGoogleGenerativeAIFinishReason({
2651
+ finishReason: candidate.finishReason,
2649
2652
  hasToolCalls
2650
2653
  }),
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
- }
2654
+ raw: candidate.finishReason
2663
2655
  };
2664
2656
  }
2665
2657
  },
2666
2658
  flush(controller) {
2659
+ var _a;
2667
2660
  if (currentTextBlockId !== null) {
2668
2661
  controller.enqueue({
2669
2662
  type: "text-end",
@@ -2680,7 +2673,17 @@ var GoogleGenerativeAILanguageModel = class {
2680
2673
  type: "finish",
2681
2674
  finishReason,
2682
2675
  usage: convertGoogleGenerativeAIUsage(usage),
2683
- providerMetadata
2676
+ providerMetadata: {
2677
+ [providerOptionsName]: {
2678
+ promptFeedback,
2679
+ groundingMetadata: lastGroundingMetadata,
2680
+ urlContextMetadata: lastUrlContextMetadata,
2681
+ safetyRatings: lastSafetyRatings,
2682
+ usageMetadata: usage != null ? usage : null,
2683
+ finishMessage: lastFinishMessage,
2684
+ serviceTier: (_a = usage == null ? void 0 : usage.serviceTier) != null ? _a : null
2685
+ }
2686
+ }
2684
2687
  });
2685
2688
  }
2686
2689
  })
@@ -2977,6 +2980,9 @@ var chunkSchema = (0, import_provider_utils7.lazySchema)(
2977
2980
  })
2978
2981
  )
2979
2982
  );
2983
+ function isConfirmedPromptBlockReason(blockReason) {
2984
+ return blockReason != null && blockReason !== "" && blockReason !== "BLOCK_REASON_UNSPECIFIED" && blockReason !== "BLOCKED_REASON_UNSPECIFIED";
2985
+ }
2980
2986
 
2981
2987
  // src/tool/code-execution.ts
2982
2988
  var import_provider_utils8 = require("@ai-sdk/provider-utils");