@ai-sdk/google 3.0.75 → 3.0.78

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.
@@ -643,17 +643,32 @@ var googleLanguageModelOptions = lazySchema2(
643
643
  */
644
644
  streamFunctionCallArguments: z2.boolean().optional(),
645
645
  /**
646
- * Optional. The service tier to use for the request.
646
+ * Optional. The service tier to use for the request. Sent as the
647
+ * `serviceTier` body field. Gemini API only.
647
648
  */
648
- serviceTier: z2.enum(["standard", "flex", "priority"]).optional()
649
+ serviceTier: z2.enum(["standard", "flex", "priority"]).optional(),
650
+ /**
651
+ * Optional. Vertex AI only. Sent as the
652
+ * `X-Vertex-AI-LLM-Shared-Request-Type` request header to select a
653
+ * shared (PayGo) tier. With Provisioned Throughput allocated and
654
+ * `requestType` unset, the request falls back to this tier only if
655
+ * PT capacity is exhausted.
656
+ *
657
+ * https://docs.cloud.google.com/vertex-ai/generative-ai/docs/priority-paygo
658
+ * https://docs.cloud.google.com/vertex-ai/generative-ai/docs/flex-paygo
659
+ */
660
+ sharedRequestType: z2.enum(["priority", "flex", "standard"]).optional(),
661
+ /**
662
+ * Optional. Vertex AI only. Sent as the `X-Vertex-AI-LLM-Request-Type`
663
+ * request header. Set to `'shared'` together with `sharedRequestType`
664
+ * to bypass Provisioned Throughput entirely.
665
+ *
666
+ * https://docs.cloud.google.com/vertex-ai/generative-ai/docs/priority-paygo
667
+ */
668
+ requestType: z2.enum(["shared"]).optional()
649
669
  })
650
670
  )
651
671
  );
652
- var VertexServiceTierMap = {
653
- standard: "SERVICE_TIER_STANDARD",
654
- flex: "SERVICE_TIER_FLEX",
655
- priority: "SERVICE_TIER_PRIORITY"
656
- };
657
672
 
658
673
  // src/google-prepare-tools.ts
659
674
  import {
@@ -1220,10 +1235,27 @@ var GoogleGenerativeAILanguageModel = class {
1220
1235
  message: `'streamFunctionCallArguments' is only supported on the Vertex AI API and will be ignored with the current Google provider (${this.config.provider}). See https://docs.cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling#streaming-fc`
1221
1236
  });
1222
1237
  }
1223
- let sanitizedServiceTier = googleOptions == null ? void 0 : googleOptions.serviceTier;
1224
1238
  if ((googleOptions == null ? void 0 : googleOptions.serviceTier) && isVertexProvider) {
1225
- sanitizedServiceTier = VertexServiceTierMap[googleOptions.serviceTier];
1239
+ warnings.push({
1240
+ type: "other",
1241
+ message: "'serviceTier' is a Gemini API option and is not supported on Vertex AI. Use 'sharedRequestType' (and optionally 'requestType') instead. See https://docs.cloud.google.com/vertex-ai/generative-ai/docs/priority-paygo"
1242
+ });
1226
1243
  }
1244
+ if (((googleOptions == null ? void 0 : googleOptions.sharedRequestType) || (googleOptions == null ? void 0 : googleOptions.requestType)) && !isVertexProvider) {
1245
+ warnings.push({
1246
+ type: "other",
1247
+ message: `'sharedRequestType' and 'requestType' are Vertex AI options and are ignored with the current Google provider (${this.config.provider}).`
1248
+ });
1249
+ }
1250
+ const vertexPaygoHeaders = isVertexProvider && ((googleOptions == null ? void 0 : googleOptions.sharedRequestType) || (googleOptions == null ? void 0 : googleOptions.requestType)) ? {
1251
+ ...googleOptions.sharedRequestType && {
1252
+ "X-Vertex-AI-LLM-Shared-Request-Type": googleOptions.sharedRequestType
1253
+ },
1254
+ ...googleOptions.requestType && {
1255
+ "X-Vertex-AI-LLM-Request-Type": googleOptions.requestType
1256
+ }
1257
+ } : void 0;
1258
+ const bodyServiceTier = isVertexProvider ? void 0 : googleOptions == null ? void 0 : googleOptions.serviceTier;
1227
1259
  const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
1228
1260
  const supportsFunctionResponseParts = this.modelId.startsWith("gemini-3");
1229
1261
  const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
@@ -1295,18 +1327,20 @@ var GoogleGenerativeAILanguageModel = class {
1295
1327
  toolConfig,
1296
1328
  cachedContent: googleOptions == null ? void 0 : googleOptions.cachedContent,
1297
1329
  labels: googleOptions == null ? void 0 : googleOptions.labels,
1298
- serviceTier: sanitizedServiceTier
1330
+ serviceTier: bodyServiceTier
1299
1331
  },
1300
1332
  warnings: [...warnings, ...toolWarnings],
1301
- providerOptionsName
1333
+ providerOptionsName,
1334
+ extraHeaders: vertexPaygoHeaders
1302
1335
  };
1303
1336
  }
1304
1337
  async doGenerate(options) {
1305
1338
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
1306
- const { args, warnings, providerOptionsName } = await this.getArgs(options);
1339
+ const { args, warnings, providerOptionsName, extraHeaders } = await this.getArgs(options);
1307
1340
  const mergedHeaders = combineHeaders(
1308
1341
  await resolve(this.config.headers),
1309
- options.headers
1342
+ options.headers,
1343
+ extraHeaders
1310
1344
  );
1311
1345
  const {
1312
1346
  responseHeaders,
@@ -1471,7 +1505,7 @@ var GoogleGenerativeAILanguageModel = class {
1471
1505
  safetyRatings: (_p = candidate.safetyRatings) != null ? _p : null,
1472
1506
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1473
1507
  finishMessage: (_q = candidate.finishMessage) != null ? _q : null,
1474
- serviceTier: (_r = response.serviceTier) != null ? _r : null
1508
+ serviceTier: (_r = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _r : null
1475
1509
  }
1476
1510
  },
1477
1511
  request: { body: args },
@@ -1483,13 +1517,11 @@ var GoogleGenerativeAILanguageModel = class {
1483
1517
  };
1484
1518
  }
1485
1519
  async doStream(options) {
1486
- const { args, warnings, providerOptionsName } = await this.getArgs(
1487
- options,
1488
- { isStreaming: true }
1489
- );
1520
+ const { args, warnings, providerOptionsName, extraHeaders } = await this.getArgs(options, { isStreaming: true });
1490
1521
  const headers = combineHeaders(
1491
1522
  await resolve(this.config.headers),
1492
- options.headers
1523
+ options.headers,
1524
+ extraHeaders
1493
1525
  );
1494
1526
  const { responseHeaders, value: response } = await postJsonToApi({
1495
1527
  url: `${this.config.baseURL}/${getModelPath(
@@ -1510,7 +1542,6 @@ var GoogleGenerativeAILanguageModel = class {
1510
1542
  let providerMetadata = void 0;
1511
1543
  let lastGroundingMetadata = null;
1512
1544
  let lastUrlContextMetadata = null;
1513
- let serviceTier = null;
1514
1545
  const generateId2 = this.config.generateId;
1515
1546
  let hasToolCalls = false;
1516
1547
  let currentTextBlockId = null;
@@ -1520,6 +1551,34 @@ var GoogleGenerativeAILanguageModel = class {
1520
1551
  let lastCodeExecutionToolCallId;
1521
1552
  let lastServerToolCallId;
1522
1553
  const activeStreamingToolCalls = [];
1554
+ const finishActiveStreamingToolCall = (controller) => {
1555
+ const active = activeStreamingToolCalls.pop();
1556
+ if (active == null) {
1557
+ return;
1558
+ }
1559
+ const { finalJSON, closingDelta } = active.accumulator.finalize();
1560
+ if (closingDelta.length > 0) {
1561
+ controller.enqueue({
1562
+ type: "tool-input-delta",
1563
+ id: active.toolCallId,
1564
+ delta: closingDelta,
1565
+ providerMetadata: active.providerMetadata
1566
+ });
1567
+ }
1568
+ controller.enqueue({
1569
+ type: "tool-input-end",
1570
+ id: active.toolCallId,
1571
+ providerMetadata: active.providerMetadata
1572
+ });
1573
+ controller.enqueue({
1574
+ type: "tool-call",
1575
+ toolCallId: active.toolCallId,
1576
+ toolName: active.toolName,
1577
+ input: finalJSON,
1578
+ providerMetadata: active.providerMetadata
1579
+ });
1580
+ hasToolCalls = true;
1581
+ };
1523
1582
  return {
1524
1583
  stream: response.pipeThrough(
1525
1584
  new TransformStream({
@@ -1527,7 +1586,7 @@ var GoogleGenerativeAILanguageModel = class {
1527
1586
  controller.enqueue({ type: "stream-start", warnings });
1528
1587
  },
1529
1588
  transform(chunk, controller) {
1530
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1589
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
1531
1590
  if (options.includeRawChunks) {
1532
1591
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1533
1592
  }
@@ -1540,9 +1599,6 @@ var GoogleGenerativeAILanguageModel = class {
1540
1599
  if (usageMetadata != null) {
1541
1600
  usage = usageMetadata;
1542
1601
  }
1543
- if (value.serviceTier != null) {
1544
- serviceTier = value.serviceTier;
1545
- }
1546
1602
  const candidate = (_a = value.candidates) == null ? void 0 : _a[0];
1547
1603
  if (candidate == null) {
1548
1604
  return;
@@ -1732,7 +1788,7 @@ var GoogleGenerativeAILanguageModel = class {
1732
1788
  const isCompleteCall = part.functionCall.name != null && part.functionCall.args != null && part.functionCall.partialArgs == null;
1733
1789
  const isNoArgsCompleteCall = part.functionCall.name != null && part.functionCall.args == null && part.functionCall.partialArgs == null && part.functionCall.willContinue !== true;
1734
1790
  if (isStreamingChunk) {
1735
- if (part.functionCall.name != null && part.functionCall.willContinue === true) {
1791
+ if (part.functionCall.name != null) {
1736
1792
  const toolCallId = (_i = part.functionCall.id) != null ? _i : generateId2();
1737
1793
  const accumulator = new GoogleJSONAccumulator();
1738
1794
  activeStreamingToolCalls.push({
@@ -1748,9 +1804,8 @@ var GoogleGenerativeAILanguageModel = class {
1748
1804
  providerMetadata: providerMeta
1749
1805
  });
1750
1806
  if (part.functionCall.partialArgs != null) {
1751
- const { textDelta } = accumulator.processPartialArgs(
1752
- part.functionCall.partialArgs
1753
- );
1807
+ const partialArgs = part.functionCall.partialArgs;
1808
+ const { textDelta } = accumulator.processPartialArgs(partialArgs);
1754
1809
  if (textDelta.length > 0) {
1755
1810
  controller.enqueue({
1756
1811
  type: "tool-input-delta",
@@ -1759,12 +1814,14 @@ var GoogleGenerativeAILanguageModel = class {
1759
1814
  providerMetadata: providerMeta
1760
1815
  });
1761
1816
  }
1817
+ if (part.functionCall.willContinue !== true && partialArgs.every((arg) => arg.willContinue !== true)) {
1818
+ finishActiveStreamingToolCall(controller);
1819
+ }
1762
1820
  }
1763
1821
  } else if (part.functionCall.partialArgs != null && activeStreamingToolCalls.length > 0) {
1764
1822
  const active = activeStreamingToolCalls[activeStreamingToolCalls.length - 1];
1765
- const { textDelta } = active.accumulator.processPartialArgs(
1766
- part.functionCall.partialArgs
1767
- );
1823
+ const partialArgs = part.functionCall.partialArgs;
1824
+ const { textDelta } = active.accumulator.processPartialArgs(partialArgs);
1768
1825
  if (textDelta.length > 0) {
1769
1826
  controller.enqueue({
1770
1827
  type: "tool-input-delta",
@@ -1773,31 +1830,12 @@ var GoogleGenerativeAILanguageModel = class {
1773
1830
  providerMetadata: providerMeta
1774
1831
  });
1775
1832
  }
1833
+ if (part.functionCall.willContinue !== true && partialArgs.every((arg) => arg.willContinue !== true)) {
1834
+ finishActiveStreamingToolCall(controller);
1835
+ }
1776
1836
  }
1777
1837
  } else if (isTerminalChunk && activeStreamingToolCalls.length > 0) {
1778
- const active = activeStreamingToolCalls.pop();
1779
- const { finalJSON, closingDelta } = active.accumulator.finalize();
1780
- if (closingDelta.length > 0) {
1781
- controller.enqueue({
1782
- type: "tool-input-delta",
1783
- id: active.toolCallId,
1784
- delta: closingDelta,
1785
- providerMetadata: active.providerMetadata
1786
- });
1787
- }
1788
- controller.enqueue({
1789
- type: "tool-input-end",
1790
- id: active.toolCallId,
1791
- providerMetadata: active.providerMetadata
1792
- });
1793
- controller.enqueue({
1794
- type: "tool-call",
1795
- toolCallId: active.toolCallId,
1796
- toolName: active.toolName,
1797
- input: finalJSON,
1798
- providerMetadata: active.providerMetadata
1799
- });
1800
- hasToolCalls = true;
1838
+ finishActiveStreamingToolCall(controller);
1801
1839
  } else if (isCompleteCall) {
1802
1840
  const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId2();
1803
1841
  const toolName = part.functionCall.name;
@@ -1868,7 +1906,7 @@ var GoogleGenerativeAILanguageModel = class {
1868
1906
  safetyRatings: (_n = candidate.safetyRatings) != null ? _n : null,
1869
1907
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1870
1908
  finishMessage: (_o = candidate.finishMessage) != null ? _o : null,
1871
- serviceTier
1909
+ serviceTier: (_p = usage == null ? void 0 : usage.serviceTier) != null ? _p : null
1872
1910
  }
1873
1911
  };
1874
1912
  }
@@ -2127,6 +2165,7 @@ var usageSchema = z3.object({
2127
2165
  totalTokenCount: z3.number().nullish(),
2128
2166
  // https://cloud.google.com/vertex-ai/generative-ai/docs/reference/rest/v1/GenerateContentResponse#TrafficType
2129
2167
  trafficType: z3.string().nullish(),
2168
+ serviceTier: z3.string().nullish(),
2130
2169
  // https://ai.google.dev/api/generate-content#Modality
2131
2170
  promptTokensDetails: tokenDetailsSchema,
2132
2171
  candidatesTokensDetails: tokenDetailsSchema
@@ -2156,8 +2195,7 @@ var responseSchema = lazySchema3(
2156
2195
  promptFeedback: z3.object({
2157
2196
  blockReason: z3.string().nullish(),
2158
2197
  safetyRatings: z3.array(getSafetyRatingSchema()).nullish()
2159
- }).nullish(),
2160
- serviceTier: z3.string().nullish()
2198
+ }).nullish()
2161
2199
  })
2162
2200
  )
2163
2201
  );
@@ -2178,8 +2216,7 @@ var chunkSchema = lazySchema3(
2178
2216
  promptFeedback: z3.object({
2179
2217
  blockReason: z3.string().nullish(),
2180
2218
  safetyRatings: z3.array(getSafetyRatingSchema()).nullish()
2181
- }).nullish(),
2182
- serviceTier: z3.string().nullish()
2219
+ }).nullish()
2183
2220
  })
2184
2221
  )
2185
2222
  );