@warlock.js/ai-google 5.39.1 → 5.40.0

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
@@ -4,6 +4,13 @@ All notable changes to `@warlock.js/ai-google` are documented in this file.
4
4
 
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). `@warlock.js/*` packages are released in lockstep — every package shares the same version number, so a version below may list only the changes that affected this package.
6
6
 
7
+ ## 5.40.0 - 2026-10-29
8
+
9
+ ### Added
10
+
11
+ - `googleSearchBudget(maxQueries)` and `providerOptions.google.search.budget` bound Google Search across the calls of one run. The Gemini API has no per-request search limit, so after each call the adapter adds the unique queries Google reported to the budget and, once it is spent, stops sending the `googleSearch` tool on later calls (other tools are kept). One call that is already running can still overshoot the budget.
12
+ - `google.models()` lists the models the key can use as `ModelInfo[]`, with `kind` from each model's `supportedActions`, token limits and description.
13
+
7
14
  ## 5.34.3 - 2026-10-20
8
15
 
9
16
  ### Changed
package/cjs/index.cjs CHANGED
@@ -837,6 +837,58 @@ function googlePricingFor(modelName) {
837
837
  * https://ai.google.dev/gemini-api/docs/google-search#pricing — verified 2026-10-18. */
838
838
  const GOOGLE_SERVER_TOOL_PRICING = { webSearch: .014 };
839
839
 
840
+ //#endregion
841
+ //#region ../../packages/ai-google/src/search-budget.ts
842
+ /**
843
+ * Bound Google Search grounding across the model calls of one run.
844
+ *
845
+ * The Gemini API has no per-request search limit: `{ googleSearch: {} }` takes
846
+ * no `max_uses`, and the model decides how many queries one `generateContent`
847
+ * call runs. What the adapter can enforce is the number of queries across
848
+ * calls: after each call it adds the queries Google reported
849
+ * (`groundingMetadata.webSearchQueries`, counted the way Gemini 3 bills them,
850
+ * unique and non-empty) to the budget, and once the budget is spent it stops
851
+ * sending the `googleSearch` tool, so later calls run without search. One
852
+ * call that is already running can still overshoot the budget by the queries
853
+ * it makes itself.
854
+ *
855
+ * @example
856
+ * const budget = googleSearchBudget(3);
857
+ * const search = ai.providerTool({ provider: "google", definition: { googleSearch: {} } });
858
+ * await model.complete(messages, {
859
+ * tools: [search],
860
+ * providerOptions: { google: { search: { budget } } },
861
+ * });
862
+ * budget.used; // queries spent so far
863
+ */
864
+ function googleSearchBudget(maxQueries) {
865
+ if (!Number.isInteger(maxQueries) || maxQueries < 0) throw new _warlock_js_ai.InvalidRequestError(`googleSearchBudget(maxQueries) needs a non-negative integer, got ${String(maxQueries)}.`);
866
+ let used = 0;
867
+ return {
868
+ max: maxQueries,
869
+ get used() {
870
+ return used;
871
+ },
872
+ get remaining() {
873
+ return Math.max(0, maxQueries - used);
874
+ },
875
+ record(queries) {
876
+ if (Number.isFinite(queries) && queries > 0) used += queries;
877
+ }
878
+ };
879
+ }
880
+ /** The budget on a call, validated; `undefined` when the call sets none. */
881
+ function searchBudgetOf(options) {
882
+ const budget = ((options?.providerOptions?.google)?.search)?.budget;
883
+ if (budget === void 0) return;
884
+ if (typeof budget !== "object" || budget === null || typeof budget.record !== "function" || typeof budget.remaining !== "number") throw new _warlock_js_ai.InvalidRequestError("providerOptions.google.search.budget must come from googleSearchBudget(maxQueries).");
885
+ return budget;
886
+ }
887
+ /** `true` for a Gemini tool entry that runs Google Search grounding. */
888
+ function isSearchDefinition(definition) {
889
+ return "googleSearch" in definition || "googleSearchRetrieval" in definition;
890
+ }
891
+
840
892
  //#endregion
841
893
  //#region ../../packages/ai-google/src/utils/grounding.ts
842
894
  /** Candidate snapshots are replaced, not added: streamed metadata may repeat.
@@ -1035,6 +1087,7 @@ var GoogleModel = class {
1035
1087
  const usage = this.extractUsage(response);
1036
1088
  const grounding = /* @__PURE__ */ new Map();
1037
1089
  applyGrounding(usage, grounding, response);
1090
+ searchBudgetOf(options)?.record(usage.serverToolUse?.webSearchRequests ?? 0);
1038
1091
  const providerMetadata = groundingMetadata(grounding);
1039
1092
  (0, _warlock_js_ai.attachCost)(usage, this.pricing);
1040
1093
  this.logger.debug(LOG_MODULE, "response", "generateContent call succeeded", {
@@ -1190,6 +1243,7 @@ var GoogleModel = class {
1190
1243
  finishReason,
1191
1244
  usage
1192
1245
  });
1246
+ searchBudgetOf(options)?.record(usage.serverToolUse?.webSearchRequests ?? 0);
1193
1247
  const providerMetadata = groundingMetadata(grounding);
1194
1248
  yield {
1195
1249
  type: "done",
@@ -1253,7 +1307,14 @@ var GoogleModel = class {
1253
1307
  model: this.name,
1254
1308
  logger: this.logger
1255
1309
  });
1256
- const mapped = [...toGoogleTools(tools) ?? [], ...definitions];
1310
+ const budget = searchBudgetOf(options);
1311
+ const offered = budget && budget.remaining <= 0 ? definitions.filter((definition) => !isSearchDefinition(definition)) : definitions;
1312
+ if (offered.length < definitions.length) this.logger.warn(LOG_MODULE, "search-budget", "Google Search budget is spent; calling without the search tool", {
1313
+ model: this.name,
1314
+ max: budget?.max,
1315
+ used: budget?.used
1316
+ });
1317
+ const mapped = [...toGoogleTools(tools) ?? [], ...offered];
1257
1318
  return mapped.length > 0 ? { tools: mapped } : {};
1258
1319
  }
1259
1320
  /**
@@ -1465,7 +1526,55 @@ var GoogleSDK = class {
1465
1526
  if (usesGeminiImageTransport(config.name)) return new GeminiImageModel(this.ai, resolvedConfig, this.provider);
1466
1527
  return new GoogleImageModel(this.ai, resolvedConfig, this.provider);
1467
1528
  }
1529
+ /**
1530
+ * List the models this key or project can use, live from the Gemini API
1531
+ * `models.list` endpoint, mapped to {@link ModelInfo}. Follows pagination to
1532
+ * the end. `kind` comes from the model's `supportedActions`:
1533
+ * `generateContent` is `"chat"` (unless the id is an image or audio
1534
+ * model), `embedContent` is `"embedding"`, `predict` / `predictLongRunning`
1535
+ * is `"image"`. Google reports no creation date, so `created` is left out.
1536
+ * Failures are thrown as the adapter's usual provider errors.
1537
+ *
1538
+ * @example
1539
+ * const models = await google.models();
1540
+ * const chat = models.filter((model) => model.kind === "chat");
1541
+ */
1542
+ async models(options) {
1543
+ const found = /* @__PURE__ */ new Map();
1544
+ try {
1545
+ const pager = await this.ai.models.list({ config: {
1546
+ pageSize: 1e3,
1547
+ ...options?.signal ? { abortSignal: options.signal } : {}
1548
+ } });
1549
+ for await (const entry of pager) {
1550
+ const rawName = entry.name;
1551
+ if (typeof rawName !== "string" || rawName === "") continue;
1552
+ const id = rawName.replace(/^(?:publishers\/[^/]+\/)?models\//u, "");
1553
+ const actions = entry.supportedActions ?? [];
1554
+ found.set(id, {
1555
+ id,
1556
+ label: entry.displayName || id,
1557
+ provider: this.provider,
1558
+ kind: kindOfActions(actions, id),
1559
+ ...entry.inputTokenLimit ? { contextWindow: entry.inputTokenLimit } : {},
1560
+ ...entry.outputTokenLimit ? { maxOutputTokens: entry.outputTokenLimit } : {},
1561
+ ...entry.description ? { description: entry.description } : {}
1562
+ });
1563
+ }
1564
+ } catch (thrown) {
1565
+ throw wrapGoogleError(thrown);
1566
+ }
1567
+ return [...found.values()];
1568
+ }
1468
1569
  };
1570
+ /** Map Gemini `supportedActions` to a {@link ModelKind}; fall back to the id when none are listed. */
1571
+ function kindOfActions(actions, id) {
1572
+ const inferred = (0, _warlock_js_ai.inferModelKind)(id);
1573
+ if (actions.includes("embedContent") || actions.includes("batchEmbedContents")) return "embedding";
1574
+ if (actions.includes("predict") || actions.includes("predictLongRunning")) return inferred === "audio" ? "audio" : "image";
1575
+ if (actions.includes("generateContent")) return inferred === "embedding" || inferred === "moderation" ? "other" : inferred;
1576
+ return actions.length === 0 ? inferred : "other";
1577
+ }
1469
1578
 
1470
1579
  //#endregion
1471
1580
  exports.GOOGLE_MODEL_PRICING = GOOGLE_MODEL_PRICING;
@@ -1474,4 +1583,5 @@ exports.GeminiImageModel = GeminiImageModel;
1474
1583
  exports.GoogleImageModel = GoogleImageModel;
1475
1584
  exports.GoogleSDK = GoogleSDK;
1476
1585
  exports.googlePricingFor = googlePricingFor;
1586
+ exports.googleSearchBudget = googleSearchBudget;
1477
1587
  //# sourceMappingURL=index.cjs.map