@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/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.122" : "0.0.0-test";
227
+ var VERSION = true ? "3.0.124" : "0.0.0-test";
228
228
 
229
229
  // src/google-generative-ai-embedding-model.ts
230
230
  import {
@@ -2077,7 +2077,7 @@ var GoogleGenerativeAILanguageModel = class {
2077
2077
  };
2078
2078
  }
2079
2079
  async doGenerate(options) {
2080
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
2080
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
2081
2081
  const { args, warnings, providerOptionsName, extraHeaders } = await this.getArgs(options);
2082
2082
  const mergedHeaders = combineHeaders3(
2083
2083
  await resolve3(this.config.headers),
@@ -2101,15 +2101,18 @@ var GoogleGenerativeAILanguageModel = class {
2101
2101
  });
2102
2102
  const candidate = (_a = response.candidates) == null ? void 0 : _a[0];
2103
2103
  const promptBlockReason = (_b = response.promptFeedback) == null ? void 0 : _b.blockReason;
2104
- const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && promptBlockReason != null;
2105
- const rawFinishReason = (_d = (_c = candidate == null ? void 0 : candidate.finishReason) != null ? _c : promptBlockReason) != null ? _d : void 0;
2104
+ const confirmedPromptBlockReason = isConfirmedPromptBlockReason(
2105
+ promptBlockReason
2106
+ ) ? promptBlockReason : void 0;
2107
+ const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && confirmedPromptBlockReason != null;
2108
+ const rawFinishReason = (_c = candidate == null ? void 0 : candidate.finishReason) != null ? _c : confirmedPromptBlockReason;
2106
2109
  const content = [];
2107
- const parts = (_f = (_e = candidate == null ? void 0 : candidate.content) == null ? void 0 : _e.parts) != null ? _f : [];
2110
+ const parts = (_e = (_d = candidate == null ? void 0 : candidate.content) == null ? void 0 : _d.parts) != null ? _e : [];
2108
2111
  const usageMetadata = response.usageMetadata;
2109
2112
  let lastCodeExecutionToolCallId;
2110
2113
  let lastServerToolCallId;
2111
2114
  for (const part of parts) {
2112
- if ("executableCode" in part && ((_g = part.executableCode) == null ? void 0 : _g.code)) {
2115
+ if ("executableCode" in part && ((_f = part.executableCode) == null ? void 0 : _f.code)) {
2113
2116
  const toolCallId = this.config.generateId();
2114
2117
  lastCodeExecutionToolCallId = toolCallId;
2115
2118
  content.push({
@@ -2127,7 +2130,7 @@ var GoogleGenerativeAILanguageModel = class {
2127
2130
  toolName: "code_execution",
2128
2131
  result: {
2129
2132
  outcome: part.codeExecutionResult.outcome,
2130
- output: (_h = part.codeExecutionResult.output) != null ? _h : ""
2133
+ output: (_g = part.codeExecutionResult.output) != null ? _g : ""
2131
2134
  }
2132
2135
  });
2133
2136
  } else if ("text" in part && part.text != null) {
@@ -2151,9 +2154,9 @@ var GoogleGenerativeAILanguageModel = class {
2151
2154
  } else if ("functionCall" in part && part.functionCall.name != null) {
2152
2155
  content.push({
2153
2156
  type: "tool-call",
2154
- toolCallId: (_i = part.functionCall.id) != null ? _i : this.config.generateId(),
2157
+ toolCallId: (_h = part.functionCall.id) != null ? _h : this.config.generateId(),
2155
2158
  toolName: part.functionCall.name,
2156
- input: JSON.stringify((_j = part.functionCall.args) != null ? _j : {}),
2159
+ input: JSON.stringify((_i = part.functionCall.args) != null ? _i : {}),
2157
2160
  providerMetadata: part.thoughtSignature ? {
2158
2161
  [providerOptionsName]: {
2159
2162
  thoughtSignature: part.thoughtSignature
@@ -2175,13 +2178,13 @@ var GoogleGenerativeAILanguageModel = class {
2175
2178
  } : void 0
2176
2179
  });
2177
2180
  } else if ("toolCall" in part && part.toolCall) {
2178
- const toolCallId = (_k = part.toolCall.id) != null ? _k : this.config.generateId();
2181
+ const toolCallId = (_j = part.toolCall.id) != null ? _j : this.config.generateId();
2179
2182
  lastServerToolCallId = toolCallId;
2180
2183
  content.push({
2181
2184
  type: "tool-call",
2182
2185
  toolCallId,
2183
2186
  toolName: `server:${part.toolCall.toolType}`,
2184
- input: JSON.stringify((_l = part.toolCall.args) != null ? _l : {}),
2187
+ input: JSON.stringify((_k = part.toolCall.args) != null ? _k : {}),
2185
2188
  providerExecuted: true,
2186
2189
  dynamic: true,
2187
2190
  providerMetadata: part.thoughtSignature ? {
@@ -2198,12 +2201,12 @@ var GoogleGenerativeAILanguageModel = class {
2198
2201
  }
2199
2202
  });
2200
2203
  } else if ("toolResponse" in part && part.toolResponse) {
2201
- const responseToolCallId = (_m = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _m : this.config.generateId();
2204
+ const responseToolCallId = (_l = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _l : this.config.generateId();
2202
2205
  content.push({
2203
2206
  type: "tool-result",
2204
2207
  toolCallId: responseToolCallId,
2205
2208
  toolName: `server:${part.toolResponse.toolType}`,
2206
- result: (_n = part.toolResponse.response) != null ? _n : {},
2209
+ result: (_m = part.toolResponse.response) != null ? _m : {},
2207
2210
  providerMetadata: part.thoughtSignature ? {
2208
2211
  [providerOptionsName]: {
2209
2212
  thoughtSignature: part.thoughtSignature,
@@ -2220,10 +2223,10 @@ var GoogleGenerativeAILanguageModel = class {
2220
2223
  lastServerToolCallId = void 0;
2221
2224
  }
2222
2225
  }
2223
- const sources = (_o = extractSources({
2226
+ const sources = (_n = extractSources({
2224
2227
  groundingMetadata: candidate == null ? void 0 : candidate.groundingMetadata,
2225
2228
  generateId: this.config.generateId
2226
- })) != null ? _o : [];
2229
+ })) != null ? _n : [];
2227
2230
  for (const source of sources) {
2228
2231
  content.push(source);
2229
2232
  }
@@ -2243,19 +2246,19 @@ var GoogleGenerativeAILanguageModel = class {
2243
2246
  warnings,
2244
2247
  providerMetadata: {
2245
2248
  [providerOptionsName]: {
2246
- promptFeedback: (_p = response.promptFeedback) != null ? _p : null,
2247
- groundingMetadata: (_q = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _q : null,
2248
- urlContextMetadata: (_r = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _r : null,
2249
- safetyRatings: (_s = candidate == null ? void 0 : candidate.safetyRatings) != null ? _s : null,
2249
+ promptFeedback: (_o = response.promptFeedback) != null ? _o : null,
2250
+ groundingMetadata: (_p = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _p : null,
2251
+ urlContextMetadata: (_q = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _q : null,
2252
+ safetyRatings: (_r = candidate == null ? void 0 : candidate.safetyRatings) != null ? _r : null,
2250
2253
  usageMetadata: usageMetadata != null ? usageMetadata : null,
2251
- finishMessage: (_t = candidate == null ? void 0 : candidate.finishMessage) != null ? _t : null,
2252
- serviceTier: (_u = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _u : null
2254
+ finishMessage: (_s = candidate == null ? void 0 : candidate.finishMessage) != null ? _s : null,
2255
+ serviceTier: (_t = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _t : null
2253
2256
  }
2254
2257
  },
2255
2258
  request: { body: args },
2256
2259
  response: {
2257
2260
  // TODO timestamp, model id
2258
- id: (_v = response.responseId) != null ? _v : void 0,
2261
+ id: (_u = response.responseId) != null ? _u : void 0,
2259
2262
  headers: responseHeaders,
2260
2263
  body: rawResponse
2261
2264
  }
@@ -2284,9 +2287,12 @@ var GoogleGenerativeAILanguageModel = class {
2284
2287
  raw: void 0
2285
2288
  };
2286
2289
  let usage = void 0;
2287
- let providerMetadata = void 0;
2290
+ let promptFeedback = null;
2288
2291
  let lastGroundingMetadata = null;
2289
2292
  let lastUrlContextMetadata = null;
2293
+ let lastSafetyRatings = null;
2294
+ let lastFinishMessage = null;
2295
+ let confirmedPromptBlockReason;
2290
2296
  const generateId3 = this.config.generateId;
2291
2297
  let hasToolCalls = false;
2292
2298
  let hasEmittedResponseMetadata = false;
@@ -2332,7 +2338,7 @@ var GoogleGenerativeAILanguageModel = class {
2332
2338
  controller.enqueue({ type: "stream-start", warnings });
2333
2339
  },
2334
2340
  transform(chunk, controller) {
2335
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
2341
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
2336
2342
  if (options.includeRawChunks) {
2337
2343
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2338
2344
  }
@@ -2352,35 +2358,35 @@ var GoogleGenerativeAILanguageModel = class {
2352
2358
  if (usageMetadata != null) {
2353
2359
  usage = usageMetadata;
2354
2360
  }
2355
- const candidate = (_a = value.candidates) == null ? void 0 : _a[0];
2356
- if (candidate == null) {
2357
- const promptBlockReason2 = (_b = value.promptFeedback) == null ? void 0 : _b.blockReason;
2358
- if (promptBlockReason2 != null) {
2361
+ if (value.promptFeedback != null && confirmedPromptBlockReason == null) {
2362
+ promptFeedback = value.promptFeedback;
2363
+ if (isConfirmedPromptBlockReason(value.promptFeedback.blockReason)) {
2364
+ confirmedPromptBlockReason = value.promptFeedback.blockReason;
2359
2365
  finishReason = {
2360
2366
  unified: "content-filter",
2361
- raw: promptBlockReason2
2362
- };
2363
- providerMetadata = {
2364
- [providerOptionsName]: {
2365
- promptFeedback: (_c = value.promptFeedback) != null ? _c : null,
2366
- groundingMetadata: lastGroundingMetadata,
2367
- urlContextMetadata: lastUrlContextMetadata,
2368
- safetyRatings: null,
2369
- usageMetadata: usageMetadata != null ? usageMetadata : null,
2370
- finishMessage: null,
2371
- serviceTier: (_d = usage == null ? void 0 : usage.serviceTier) != null ? _d : null
2372
- }
2367
+ raw: confirmedPromptBlockReason
2373
2368
  };
2374
2369
  }
2375
- return;
2376
2370
  }
2377
- const content = candidate.content;
2378
- if (candidate.groundingMetadata != null) {
2379
- lastGroundingMetadata = candidate.groundingMetadata;
2371
+ const candidate = (_a = value.candidates) == null ? void 0 : _a[0];
2372
+ if (candidate != null) {
2373
+ if (candidate.groundingMetadata != null) {
2374
+ lastGroundingMetadata = candidate.groundingMetadata;
2375
+ }
2376
+ if (candidate.urlContextMetadata != null) {
2377
+ lastUrlContextMetadata = candidate.urlContextMetadata;
2378
+ }
2379
+ if (candidate.safetyRatings != null) {
2380
+ lastSafetyRatings = candidate.safetyRatings;
2381
+ }
2382
+ if (candidate.finishMessage != null) {
2383
+ lastFinishMessage = candidate.finishMessage;
2384
+ }
2380
2385
  }
2381
- if (candidate.urlContextMetadata != null) {
2382
- lastUrlContextMetadata = candidate.urlContextMetadata;
2386
+ if (confirmedPromptBlockReason != null || candidate == null) {
2387
+ return;
2383
2388
  }
2389
+ const content = candidate.content;
2384
2390
  const sources = extractSources({
2385
2391
  groundingMetadata: candidate.groundingMetadata,
2386
2392
  generateId: generateId3
@@ -2394,9 +2400,9 @@ var GoogleGenerativeAILanguageModel = class {
2394
2400
  }
2395
2401
  }
2396
2402
  if (content != null) {
2397
- const parts = (_e = content.parts) != null ? _e : [];
2403
+ const parts = (_b = content.parts) != null ? _b : [];
2398
2404
  for (const part of parts) {
2399
- if ("executableCode" in part && ((_f = part.executableCode) == null ? void 0 : _f.code)) {
2405
+ if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
2400
2406
  const toolCallId = generateId3();
2401
2407
  lastCodeExecutionToolCallId = toolCallId;
2402
2408
  controller.enqueue({
@@ -2415,7 +2421,7 @@ var GoogleGenerativeAILanguageModel = class {
2415
2421
  toolName: "code_execution",
2416
2422
  result: {
2417
2423
  outcome: part.codeExecutionResult.outcome,
2418
- output: (_g = part.codeExecutionResult.output) != null ? _g : ""
2424
+ output: (_d = part.codeExecutionResult.output) != null ? _d : ""
2419
2425
  }
2420
2426
  });
2421
2427
  }
@@ -2509,7 +2515,7 @@ var GoogleGenerativeAILanguageModel = class {
2509
2515
  providerMetadata: fileMeta
2510
2516
  });
2511
2517
  } else if ("toolCall" in part && part.toolCall) {
2512
- const toolCallId = (_h = part.toolCall.id) != null ? _h : generateId3();
2518
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId3();
2513
2519
  lastServerToolCallId = toolCallId;
2514
2520
  const serverMeta = {
2515
2521
  [providerOptionsName]: {
@@ -2522,13 +2528,13 @@ var GoogleGenerativeAILanguageModel = class {
2522
2528
  type: "tool-call",
2523
2529
  toolCallId,
2524
2530
  toolName: `server:${part.toolCall.toolType}`,
2525
- input: JSON.stringify((_i = part.toolCall.args) != null ? _i : {}),
2531
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
2526
2532
  providerExecuted: true,
2527
2533
  dynamic: true,
2528
2534
  providerMetadata: serverMeta
2529
2535
  });
2530
2536
  } else if ("toolResponse" in part && part.toolResponse) {
2531
- const responseToolCallId = (_j = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _j : generateId3();
2537
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId3();
2532
2538
  const serverMeta = {
2533
2539
  [providerOptionsName]: {
2534
2540
  ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
@@ -2540,7 +2546,7 @@ var GoogleGenerativeAILanguageModel = class {
2540
2546
  type: "tool-result",
2541
2547
  toolCallId: responseToolCallId,
2542
2548
  toolName: `server:${part.toolResponse.toolType}`,
2543
- result: (_k = part.toolResponse.response) != null ? _k : {},
2549
+ result: (_h = part.toolResponse.response) != null ? _h : {},
2544
2550
  providerMetadata: serverMeta
2545
2551
  });
2546
2552
  lastServerToolCallId = void 0;
@@ -2559,7 +2565,7 @@ var GoogleGenerativeAILanguageModel = class {
2559
2565
  const isNoArgsCompleteCall = part.functionCall.name != null && part.functionCall.args == null && part.functionCall.partialArgs == null && part.functionCall.willContinue !== true;
2560
2566
  if (isStreamingChunk) {
2561
2567
  if (part.functionCall.name != null) {
2562
- const toolCallId = (_l = part.functionCall.id) != null ? _l : generateId3();
2568
+ const toolCallId = (_i = part.functionCall.id) != null ? _i : generateId3();
2563
2569
  const accumulator = new GoogleJSONAccumulator();
2564
2570
  activeStreamingToolCalls.push({
2565
2571
  toolCallId,
@@ -2607,9 +2613,9 @@ var GoogleGenerativeAILanguageModel = class {
2607
2613
  } else if (isTerminalChunk && activeStreamingToolCalls.length > 0) {
2608
2614
  finishActiveStreamingToolCall(controller);
2609
2615
  } else if (isCompleteCall) {
2610
- const toolCallId = (_m = part.functionCall.id) != null ? _m : generateId3();
2616
+ const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId3();
2611
2617
  const toolName = part.functionCall.name;
2612
- const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_n = part.functionCall.args) != null ? _n : {});
2618
+ const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_k = part.functionCall.args) != null ? _k : {});
2613
2619
  controller.enqueue({
2614
2620
  type: "tool-input-start",
2615
2621
  id: toolCallId,
@@ -2636,7 +2642,7 @@ var GoogleGenerativeAILanguageModel = class {
2636
2642
  });
2637
2643
  hasToolCalls = true;
2638
2644
  } else if (isNoArgsCompleteCall) {
2639
- const toolCallId = (_o = part.functionCall.id) != null ? _o : generateId3();
2645
+ const toolCallId = (_l = part.functionCall.id) != null ? _l : generateId3();
2640
2646
  const toolName = part.functionCall.name;
2641
2647
  controller.enqueue({
2642
2648
  type: "tool-input-start",
@@ -2660,31 +2666,18 @@ var GoogleGenerativeAILanguageModel = class {
2660
2666
  }
2661
2667
  }
2662
2668
  }
2663
- const promptBlockReason = (_p = value.promptFeedback) == null ? void 0 : _p.blockReason;
2664
- const isPromptBlocked = candidate.finishReason == null && promptBlockReason != null;
2665
- const rawFinishReason = (_r = (_q = candidate.finishReason) != null ? _q : promptBlockReason) != null ? _r : void 0;
2666
- if (rawFinishReason != null) {
2669
+ if (candidate.finishReason != null) {
2667
2670
  finishReason = {
2668
- unified: isPromptBlocked ? "content-filter" : mapGoogleGenerativeAIFinishReason({
2669
- finishReason: rawFinishReason,
2671
+ unified: mapGoogleGenerativeAIFinishReason({
2672
+ finishReason: candidate.finishReason,
2670
2673
  hasToolCalls
2671
2674
  }),
2672
- raw: rawFinishReason
2673
- };
2674
- providerMetadata = {
2675
- [providerOptionsName]: {
2676
- promptFeedback: (_s = value.promptFeedback) != null ? _s : null,
2677
- groundingMetadata: lastGroundingMetadata,
2678
- urlContextMetadata: lastUrlContextMetadata,
2679
- safetyRatings: (_t = candidate.safetyRatings) != null ? _t : null,
2680
- usageMetadata: usageMetadata != null ? usageMetadata : null,
2681
- finishMessage: (_u = candidate.finishMessage) != null ? _u : null,
2682
- serviceTier: (_v = usage == null ? void 0 : usage.serviceTier) != null ? _v : null
2683
- }
2675
+ raw: candidate.finishReason
2684
2676
  };
2685
2677
  }
2686
2678
  },
2687
2679
  flush(controller) {
2680
+ var _a;
2688
2681
  if (currentTextBlockId !== null) {
2689
2682
  controller.enqueue({
2690
2683
  type: "text-end",
@@ -2701,7 +2694,17 @@ var GoogleGenerativeAILanguageModel = class {
2701
2694
  type: "finish",
2702
2695
  finishReason,
2703
2696
  usage: convertGoogleGenerativeAIUsage(usage),
2704
- providerMetadata
2697
+ providerMetadata: {
2698
+ [providerOptionsName]: {
2699
+ promptFeedback,
2700
+ groundingMetadata: lastGroundingMetadata,
2701
+ urlContextMetadata: lastUrlContextMetadata,
2702
+ safetyRatings: lastSafetyRatings,
2703
+ usageMetadata: usage != null ? usage : null,
2704
+ finishMessage: lastFinishMessage,
2705
+ serviceTier: (_a = usage == null ? void 0 : usage.serviceTier) != null ? _a : null
2706
+ }
2707
+ }
2705
2708
  });
2706
2709
  }
2707
2710
  })
@@ -2998,6 +3001,9 @@ var chunkSchema = lazySchema5(
2998
3001
  })
2999
3002
  )
3000
3003
  );
3004
+ function isConfirmedPromptBlockReason(blockReason) {
3005
+ return blockReason != null && blockReason !== "" && blockReason !== "BLOCK_REASON_UNSPECIFIED" && blockReason !== "BLOCKED_REASON_UNSPECIFIED";
3006
+ }
3001
3007
 
3002
3008
  // src/tool/code-execution.ts
3003
3009
  import { createProviderToolFactoryWithOutputSchema } from "@ai-sdk/provider-utils";