@warlock.js/ai-google 5.53.1 → 5.54.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 +10 -0
- package/cjs/index.cjs +30 -8
- package/cjs/index.cjs.map +1 -1
- package/esm/model.mjs +6 -6
- package/esm/model.mjs.map +1 -1
- package/esm/utils/wrap-google-error.mjs +25 -3
- package/esm/utils/wrap-google-error.mjs.map +1 -1
- package/llms-full.txt +4 -2
- package/package.json +3 -3
- package/skills/SKILL.md +4 -2
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,16 @@ 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.54.0 - 2026-11-16
|
|
8
|
+
|
|
9
|
+
### Upgrading
|
|
10
|
+
|
|
11
|
+
- A call cancelled through its own `signal` now fails with a `ProviderAbortError` (category `cancelled`, never retried) instead of a `ProviderTimeoutError`. Code that caught `ProviderTimeoutError` to notice a user's cancel should catch `ProviderAbortError` (or check `category === "cancelled"`). An abort the caller did not ask for, such as the SDK's own `httpOptions.timeout`, stays a `ProviderTimeoutError`, as do `504`, `DEADLINE_EXCEEDED` and `ETIMEDOUT`; a `TimeoutError` (from `AbortSignal.timeout()`) is now one too instead of a plain `ProviderError`.
|
|
12
|
+
|
|
13
|
+
### Fixed
|
|
14
|
+
|
|
15
|
+
- A spent quota or billing allowance ("check your plan and billing details") is raised as a `QuotaExceededError`, which is not retried, instead of a rate limit. Per-minute and per-day request limits stay `ProviderRateLimitError`.
|
|
16
|
+
|
|
7
17
|
## 5.47.0 - 2026-11-06
|
|
8
18
|
|
|
9
19
|
### Added
|
package/cjs/index.cjs
CHANGED
|
@@ -284,12 +284,17 @@ function toJsonSchema(input) {
|
|
|
284
284
|
* throw wrapGoogleError(thrown);
|
|
285
285
|
* }
|
|
286
286
|
*/
|
|
287
|
-
function wrapGoogleError(thrown) {
|
|
287
|
+
function wrapGoogleError(thrown, options = {}) {
|
|
288
288
|
if (thrown instanceof _warlock_js_ai.AIError) return thrown;
|
|
289
289
|
const shape = toShape(thrown);
|
|
290
290
|
const context = buildContext(shape);
|
|
291
291
|
const message = shape.message ?? (thrown instanceof Error ? thrown.message : String(thrown));
|
|
292
292
|
const billed = shape.status === 400 || shape.status === 500 ? false : void 0;
|
|
293
|
+
if (shape.name === "AbortError" && options.signal?.aborted === true) return new _warlock_js_ai.ProviderAbortError(message, {
|
|
294
|
+
cause: thrown,
|
|
295
|
+
context,
|
|
296
|
+
billed
|
|
297
|
+
});
|
|
293
298
|
if (isTimeout(shape)) return new _warlock_js_ai.ProviderTimeoutError(message, {
|
|
294
299
|
cause: thrown,
|
|
295
300
|
context,
|
|
@@ -300,6 +305,11 @@ function wrapGoogleError(thrown) {
|
|
|
300
305
|
context,
|
|
301
306
|
billed
|
|
302
307
|
});
|
|
308
|
+
if (isSpentQuota(message)) return new _warlock_js_ai.QuotaExceededError(message, {
|
|
309
|
+
cause: thrown,
|
|
310
|
+
context,
|
|
311
|
+
billed
|
|
312
|
+
});
|
|
303
313
|
if (shape.status === 429 || /resource_exhausted|quota/i.test(message)) return new _warlock_js_ai.ProviderRateLimitError(message, {
|
|
304
314
|
cause: thrown,
|
|
305
315
|
context,
|
|
@@ -334,6 +344,17 @@ function wrapGoogleError(thrown) {
|
|
|
334
344
|
});
|
|
335
345
|
}
|
|
336
346
|
/**
|
|
347
|
+
* Whether the message says the project's billing or free-tier allowance is
|
|
348
|
+
* used up, as opposed to a per-minute / per-day request rate limit. Google
|
|
349
|
+
* words rate limits as "quota exceeded for metric ... per minute" and spent
|
|
350
|
+
* billing as "exceeded your current quota, please check your plan and
|
|
351
|
+
* billing details" or "billing ... not enabled".
|
|
352
|
+
*/
|
|
353
|
+
function isSpentQuota(message) {
|
|
354
|
+
if (/per[ -]?(minute|second|day)|requests? per|retry in|retryDelay/i.test(message)) return false;
|
|
355
|
+
return /check your plan and billing|billing (account|details|is not|not )|prepayment credits? (are|is)|exceeded your current quota|free tier.*(exhausted|exceeded)/i.test(message);
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
337
358
|
* Read the raw error shape. The Gemini SDK's `ApiError` carries a
|
|
338
359
|
* numeric `status`; flattened/proxied errors may carry it (or `code`)
|
|
339
360
|
* loosely.
|
|
@@ -361,8 +382,9 @@ function toShape(thrown) {
|
|
|
361
382
|
* `AbortError` / `ETIMEDOUT` / `ECONNABORTED`.
|
|
362
383
|
*/
|
|
363
384
|
function isTimeout(shape) {
|
|
385
|
+
if (shape.name === "AbortError" || shape.name === "TimeoutError") return true;
|
|
364
386
|
if (shape.status === 504) return true;
|
|
365
|
-
if (
|
|
387
|
+
if (/deadline_exceeded/i.test(shape.message ?? "")) return true;
|
|
366
388
|
return shape.code === "ETIMEDOUT" || shape.code === "ECONNABORTED";
|
|
367
389
|
}
|
|
368
390
|
/** True for HTTP 4xx — a client-side request problem, not a server fault. */
|
|
@@ -1157,7 +1179,7 @@ var GoogleModel = class {
|
|
|
1157
1179
|
config: this.buildConfig(systemInstruction, options)
|
|
1158
1180
|
});
|
|
1159
1181
|
} catch (thrown) {
|
|
1160
|
-
throw this.logAndWrap(thrown);
|
|
1182
|
+
throw this.logAndWrap(thrown, options?.signal);
|
|
1161
1183
|
}
|
|
1162
1184
|
const toolCalls = this.extractToolCalls(response);
|
|
1163
1185
|
const finishReason = toolCalls ? "tool_calls" : mapFinishReason(response.candidates?.[0]?.finishReason);
|
|
@@ -1198,7 +1220,7 @@ var GoogleModel = class {
|
|
|
1198
1220
|
if (response.totalTokens === void 0) throw new _warlock_js_ai.InvalidRequestError("Google countTokens returned no totalTokens.");
|
|
1199
1221
|
return response.totalTokens;
|
|
1200
1222
|
} catch (thrown) {
|
|
1201
|
-
throw this.logAndWrap(thrown);
|
|
1223
|
+
throw this.logAndWrap(thrown, options?.signal);
|
|
1202
1224
|
}
|
|
1203
1225
|
}
|
|
1204
1226
|
/**
|
|
@@ -1264,7 +1286,7 @@ var GoogleModel = class {
|
|
|
1264
1286
|
config: this.buildConfig(systemInstruction, options)
|
|
1265
1287
|
});
|
|
1266
1288
|
} catch (thrown) {
|
|
1267
|
-
throw this.logAndWrap(thrown);
|
|
1289
|
+
throw this.logAndWrap(thrown, options?.signal);
|
|
1268
1290
|
}
|
|
1269
1291
|
let rawFinishReason;
|
|
1270
1292
|
let sawToolCall = false;
|
|
@@ -1312,7 +1334,7 @@ var GoogleModel = class {
|
|
|
1312
1334
|
}
|
|
1313
1335
|
}
|
|
1314
1336
|
} catch (thrown) {
|
|
1315
|
-
throw this.logAndWrap(thrown);
|
|
1337
|
+
throw this.logAndWrap(thrown, options?.signal);
|
|
1316
1338
|
}
|
|
1317
1339
|
const finishReason = sawToolCall ? "tool_calls" : mapFinishReason(rawFinishReason);
|
|
1318
1340
|
(0, _warlock_js_ai.attachCost)(usage, this.pricing);
|
|
@@ -1461,8 +1483,8 @@ var GoogleModel = class {
|
|
|
1461
1483
|
* Wrap a thrown provider error into the typed `AIError` hierarchy
|
|
1462
1484
|
* and emit the standard error log line before it propagates.
|
|
1463
1485
|
*/
|
|
1464
|
-
logAndWrap(thrown) {
|
|
1465
|
-
const wrapped = wrapGoogleError(thrown);
|
|
1486
|
+
logAndWrap(thrown, signal) {
|
|
1487
|
+
const wrapped = wrapGoogleError(thrown, { signal });
|
|
1466
1488
|
this.logger.error(LOG_MODULE, "error", wrapped.message, {
|
|
1467
1489
|
code: wrapped.code,
|
|
1468
1490
|
context: wrapped.context
|
package/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["toolResultText","safeJsonParse","InvalidRequestError","extractJsonSchema","AIError","ProviderTimeoutError","ProviderAuthError","ProviderRateLimitError","ContextLengthExceededError","InvalidRequestError","ProviderOverloadedError","ProviderError","ApiError","LOG_MODULE","log","LOG_MODULE","log","ContentFilterError","ProviderError","LOG_MODULE","log","ContentFilterError","ProviderError","InvalidRequestError","InvalidRequestError","log","InvalidRequestError","resolveStreamChunks","selectProviderTools","GoogleGenAI","approximateTokenCount","inferModelKind"],"sources":["../../../../../../../packages/ai-google/src/utils/apply-google-usage.ts","../../../../../../../packages/ai-google/src/utils/map-finish-reason.ts","../../../../../../../packages/ai-google/src/utils/to-google-contents.ts","../../../../../../../packages/ai-google/src/utils/to-google-tools.ts","../../../../../../../packages/ai-google/src/utils/wrap-google-error.ts","../../../../../../../packages/ai-google/src/embedder.ts","../../../../../../../packages/ai-google/src/gemini-image.ts","../../../../../../../packages/ai-google/src/image.ts","../../../../../../../packages/ai-google/src/known-pricing.ts","../../../../../../../packages/ai-google/src/search-budget.ts","../../../../../../../packages/ai-google/src/utils/grounding.ts","../../../../../../../packages/ai-google/src/provider-options.ts","../../../../../../../packages/ai-google/src/known-context-windows.ts","../../../../../../../packages/ai-google/src/known-vision-models.ts","../../../../../../../packages/ai-google/src/model.ts","../../../../../../../packages/ai-google/src/sdk.ts","../../../../../../../packages/ai-google/src/known-lifecycle.ts"],"sourcesContent":["import type { Usage } from \"@warlock.js/ai\";\nimport type { GenerateContentResponse } from \"@google/genai\";\n\n/** Gemini's per-response token accounting block, as the SDK types it. */\nexport type GoogleUsageMetadata = NonNullable<GenerateContentResponse[\"usageMetadata\"]>;\n\n/**\n * Fold a Gemini `usageMetadata` block into a running neutral `Usage`\n * accumulator. Shared by every `generateContent`-backed surface — the\n * chat model's `complete()`, its streaming loop (where the final chunk\n * carries cumulative totals), and the Gemini image model — so one\n * mapping decides what a Gemini token report means package-wide.\n *\n * Cache-read hits (`cachedContentTokenCount`, implicit or explicit\n * context caching) surface as `cachedTokens`; the thinking-phase tokens\n * of a reasoning model (`thoughtsTokenCount`) surface as\n * `reasoningTokens`. Both are emitted only when reported `> 0` so an\n * absent channel leaves the field undefined rather than a false zero.\n *\n * `total` falls back to `input + output` when Google omits\n * `totalTokenCount`.\n *\n * `promptTokenCount` \"includes the number of tokens in the cached\n * content\" (https://ai.google.dev/api/generate-content#UsageMetadata), so\n * the usage is marked `inputIncludesCache: true`.\n */\nexport function applyGoogleUsage(usage: Usage, raw: GoogleUsageMetadata): void {\n usage.inputIncludesCache = true;\n usage.input = raw.promptTokenCount ?? usage.input;\n usage.output = raw.candidatesTokenCount ?? usage.output;\n usage.total = raw.totalTokenCount ?? usage.input + usage.output;\n\n const cached = raw.cachedContentTokenCount;\n\n if (cached && cached > 0) {\n usage.cachedTokens = cached;\n }\n\n const reasoning = raw.thoughtsTokenCount;\n\n if (reasoning && reasoning > 0) {\n usage.reasoningTokens = reasoning;\n }\n}\n","import type { FinishReason } from \"@warlock.js/ai\";\n\nconst finishReasonMap: Record<string, FinishReason> = {\n STOP: \"stop\",\n MAX_TOKENS: \"length\",\n};\n\n/**\n * Map Gemini's `FinishReason` enum value to the normalized\n * `FinishReason` union.\n *\n * `STOP` is the natural terminal. `MAX_TOKENS` maps to `length`.\n * Everything else — `SAFETY`, `RECITATION`, `BLOCKLIST`,\n * `PROHIBITED_CONTENT`, `SPII`, `MALFORMED_FUNCTION_CALL`,\n * `UNEXPECTED_TOOL_CALL`, `LANGUAGE`, `OTHER`,\n * `FINISH_REASON_UNSPECIFIED`, `null`, or any future value — falls\n * through to `\"error\"`.\n *\n * Note: Gemini reports `STOP` even when the turn ended in a function\n * call (it has no `tool_use` reason). `GoogleModel` overrides the\n * mapped reason to `\"tool_calls\"` when the response carries function\n * calls — this map intentionally stays purely about the raw signal.\n *\n * @example\n * mapFinishReason(\"STOP\"); // \"stop\"\n * mapFinishReason(\"MAX_TOKENS\"); // \"length\"\n * mapFinishReason(\"SAFETY\"); // \"error\"\n * mapFinishReason(undefined); // \"error\"\n */\nexport function mapFinishReason(raw: string | null | undefined): FinishReason {\n return finishReasonMap[raw ?? \"\"] ?? \"error\";\n}\n","import {\n InvalidRequestError,\n safeJsonParse,\n toolResultText,\n type ContentPart,\n type Message,\n} from \"@warlock.js/ai\";\nimport type { Content, Part } from \"@google/genai\";\n\n/**\n * Result of splitting a vendor-neutral `Message[]` for Gemini's\n * `generateContent`: the system prompt is hoisted to a separate\n * `systemInstruction` string (Gemini has no `\"system\"` role — content\n * roles must be `\"user\"` or `\"model\"`), and the remaining turns map to\n * `Content[]`.\n */\nexport type GoogleContents = {\n systemInstruction: string | undefined;\n contents: Content[];\n};\n\n/**\n * Convert vendor-neutral `Message[]` into Gemini's request shape.\n *\n * Gemini specifics this function absorbs:\n *\n * 1. **No `system` role.** System messages concatenate into the\n * separate `systemInstruction` config field.\n * 2. **Role names differ.** Neutral `assistant` → Gemini `\"model\"`;\n * `user` stays `\"user\"`.\n * 3. **Tool results are `user` turns.** A neutral `tool` message\n * becomes a `\"user\"` content with a single `functionResponse` part.\n * 4. **Tool calls are `functionCall` parts.** An assistant message\n * with `toolCalls` becomes a `\"model\"` content: an optional leading\n * `text` part followed by one `functionCall` part per call.\n * 5. **Adjacent same-role turns merge.** Consecutive contents with the\n * same role become one content with their parts in order, so the\n * results of parallel tool calls travel as one `user` turn (as the\n * `@google/genai` SDK's own automatic function calling sends them)\n * and a compaction summary next to a same-role turn does not break\n * the user/model alternation.\n *\n * @example\n * const { systemInstruction, contents } = toGoogleContents([\n * { role: \"system\", content: \"Be concise.\" },\n * { role: \"user\", content: \"Hi\" },\n * ]);\n */\nexport function toGoogleContents(messages: Message[]): GoogleContents {\n const systemParts: string[] = [];\n const contents: Content[] = [];\n\n for (const message of messages) {\n if (message.role === \"system\") {\n systemParts.push(stringifyContent(message.content));\n\n continue;\n }\n\n if (message.role === \"tool\") {\n contents.push({\n role: \"user\",\n parts: [\n {\n // Gemini matches a `functionResponse` to its `functionCall`\n // by `name` (the Developer API has no call ids). `name` is\n // the neutral `toolCallId`, which `GoogleModel` set to the\n // function name. The wire `id` is intentionally omitted —\n // an empty/synthetic id is rejected as an invalid argument.\n functionResponse: {\n name: message.toolCallId ?? \"\",\n response: toResponseObject(toolResultText(message.content)),\n },\n },\n ],\n });\n\n continue;\n }\n\n if (message.role === \"assistant\" && message.toolCalls && message.toolCalls.length > 0) {\n const parts: Part[] = [];\n const text = stringifyContent(message.content);\n\n if (text) {\n parts.push({ text });\n }\n\n for (const toolCall of message.toolCalls) {\n // Replay the opaque `thoughtSignature` Gemini attached to this\n // function call on the original turn. Thinking models reject\n // the follow-up request with a 400 if the signature is missing\n // from the echoed `functionCall` part. Captured by\n // `GoogleModel.partToToolCall` into `providerMetadata`.\n const thoughtSignature = toolCall.providerMetadata?.thoughtSignature;\n\n parts.push({\n ...(typeof thoughtSignature === \"string\" ? { thoughtSignature } : {}),\n // `id` omitted deliberately — Gemini Developer API function\n // calls have no ids; echoing an empty/synthetic one is\n // rejected as an invalid argument. Matched by `name`.\n functionCall: {\n name: toolCall.name,\n args: (toolCall.input ?? {}) as Record<string, unknown>,\n },\n });\n }\n\n contents.push({ role: \"model\", parts });\n\n continue;\n }\n\n if (message.role === \"user\" && Array.isArray(message.content)) {\n contents.push({ role: \"user\", parts: message.content.map(toGooglePart) });\n\n continue;\n }\n\n contents.push({\n role: message.role === \"assistant\" ? \"model\" : \"user\",\n parts: [{ text: stringifyContent(message.content) }],\n });\n }\n\n return {\n systemInstruction: systemParts.length > 0 ? systemParts.join(\"\\n\\n\") : undefined,\n contents: mergeAdjacentContents(contents),\n };\n}\n\n/**\n * Join consecutive contents that share a role into one, keeping part\n * order: parallel `functionResponse` parts stay in their `functionCall`\n * order, and each `functionCall` keeps its own `thoughtSignature`.\n * Returns new content objects; the input contents are not modified.\n *\n * Source for the single-turn shape: `@google/genai` 2.19.0\n * `Models.generateContent` (automatic function calling) sends every\n * function response of one model turn as a single `{ role: \"user\",\n * parts }` content, in call order.\n */\nfunction mergeAdjacentContents(contents: Content[]): Content[] {\n const merged: Content[] = [];\n\n for (const content of contents) {\n const previous = merged.at(-1);\n\n if (previous && previous.role === content.role) {\n merged[merged.length - 1] = {\n ...previous,\n parts: [...(previous.parts ?? []), ...(content.parts ?? [])],\n };\n\n continue;\n }\n\n merged.push(content);\n }\n\n return merged;\n}\n\n/**\n * Multipart content is only meaningful on user messages — for any\n * other role collapse a `ContentPart[]` to concatenated text. Plain\n * strings pass through unchanged.\n */\nfunction stringifyContent(content: string | ContentPart[]): string {\n if (typeof content === \"string\") {\n return content;\n }\n\n return content\n .filter((part): part is { type: \"text\"; text: string } => part.type === \"text\")\n .map((part) => part.text)\n .join(\"\");\n}\n\n/**\n * Gemini's `functionResponse.response` must be a JSON object. Tool\n * results arrive as a string (usually stringified JSON) — parse it\n * when it is a JSON object, otherwise wrap the raw string under a\n * `result` key so the model always receives a well-formed object.\n */\nfunction toResponseObject(raw: string): Record<string, unknown> {\n const parsed = safeJsonParse<unknown>(raw, undefined);\n\n if (parsed !== null && typeof parsed === \"object\" && !Array.isArray(parsed)) {\n return parsed as Record<string, unknown>;\n }\n\n return { result: raw };\n}\n\n/**\n * Map a resolved `ContentPart` to a Gemini `Part`. All binary\n * modalities — **image, PDF, and audio** — go to a single\n * `inlineData: { mimeType, data }` block; Gemini's multimodal input is\n * media-agnostic and keys off the IANA `mimeType` (`image/png`,\n * `application/pdf`, `audio/mpeg`, …), so one mapping covers every part\n * type the model's capabilities admit. PDF and audio reach this point\n * only when the model declares the matching capability (`google.model`\n * infers `pdf` / `audio` from the multimodal Gemini families); the\n * agent's modality gate throws upfront otherwise, so capability and\n * behavior stay in lockstep.\n *\n * Gemini's `generateContent` does not fetch arbitrary remote URLs (only\n * Files API / GCS URIs via `fileData`), so a neutral `{ url }` source\n * surfaces a typed `InvalidRequestError` upfront — for any modality —\n * rather than a downstream Gemini fault. The agent resolves attachments\n * before this point, so nothing is read or fetched here.\n */\nfunction toGooglePart(part: ContentPart): Part {\n if (part.type === \"text\") {\n return { text: part.text };\n }\n\n if (\"url\" in part.source) {\n throw new InvalidRequestError(\n `Gemini generateContent cannot fetch remote-URL ${part.type} media; supply base64 bytes instead.`,\n );\n }\n\n return {\n inlineData: { mimeType: part.source.mediaType, data: part.source.base64 },\n };\n}\n","import { extractJsonSchema, type ToolConfig } from \"@warlock.js/ai\";\nimport type { Tool } from \"@google/genai\";\n\n/**\n * Convert vendor-neutral `ToolConfig[]` into Gemini's `tools` array —\n * a single `Tool` carrying one `functionDeclarations` entry per tool.\n *\n * The input schema is forwarded via `parametersJsonSchema` (raw JSON\n * Schema, mutually exclusive with Gemini's typed `parameters`).\n * Non-object extractions degrade to a parameterless object so\n * registration never fails.\n *\n * Returns `undefined` when there are no tools so the caller can omit\n * `config.tools` entirely.\n *\n * @example\n * const tools = toGoogleTools([weatherTool]);\n * await ai.models.generateContent({ model, contents, config: { tools } });\n */\nexport function toGoogleTools(\n tools: ToolConfig<unknown, unknown>[] | undefined,\n): Tool[] | undefined {\n if (!tools || tools.length === 0) {\n return undefined;\n }\n\n return [\n {\n functionDeclarations: tools.map((tool) => ({\n name: tool.name,\n description: tool.description,\n parametersJsonSchema: toJsonSchema(tool.input),\n })),\n },\n ];\n}\n\n/**\n * Resolve a tool's input schema to a JSON-Schema object. Gemini wants\n * an object root for function parameters; anything else (or a failed\n * extraction) degrades to a parameterless object.\n */\nfunction toJsonSchema(input: ToolConfig<unknown, unknown>[\"input\"]): Record<string, unknown> {\n const schema = extractJsonSchema(input);\n\n if (schema && schema.type === \"object\") {\n return schema;\n }\n\n return { type: \"object\" };\n}\n","import {\n AIError,\n ContextLengthExceededError,\n InvalidRequestError,\n ProviderAuthError,\n ProviderError,\n ProviderOverloadedError,\n ProviderRateLimitError,\n ProviderTimeoutError,\n} from \"@warlock.js/ai\";\nimport { ApiError } from \"@google/genai\";\n\n/**\n * Raw-error fields the wrapper reads off a Gemini SDK error.\n * `@google/genai`'s `ApiError` exposes `status` (HTTP code) +\n * `message`; transport aborts surface as `AbortError` / `ETIMEDOUT`.\n * We duck-type so proxied / re-thrown errors still classify.\n */\ntype GoogleErrorShape = {\n status?: number;\n message?: string;\n name?: string;\n code?: string;\n};\n\n/**\n * Wrap any thrown value caught inside the Gemini adapter into the\n * appropriate `@warlock.js/ai` `AIError` subclass.\n *\n * **Dispatch strategy.** Gemini has no machine error `code`; the\n * signals are the HTTP `status` and the canonical status phrase Google\n * embeds in `message` (`PERMISSION_DENIED`, `RESOURCE_EXHAUSTED`,\n * `INVALID_ARGUMENT`, …). Dispatch keys on `status`, using the message\n * phrase as the tie-breaker for the two 400 sub-cases\n * (context-length vs generic) and for status-less auth/quota errors.\n *\n * `AIError` instances pass through unchanged so `catch/throw wrap(e)`\n * pipelines never double-wrap.\n *\n * @example\n * try {\n * return await this.ai.models.generateContent(...);\n * } catch (thrown) {\n * throw wrapGoogleError(thrown);\n * }\n */\nexport function wrapGoogleError(thrown: unknown): AIError {\n if (thrown instanceof AIError) {\n return thrown;\n }\n\n const shape = toShape(thrown);\n const context = buildContext(shape);\n const message = shape.message ?? (thrown instanceof Error ? thrown.message : String(thrown));\n // \"If your request fails with a 400 or 500 error, you won't be charged\n // for the tokens used.\" — https://ai.google.dev/gemini-api/docs/billing\n // (fetched 2026-10-17). Every other failure keeps the default.\n const billed = shape.status === 400 || shape.status === 500 ? false : undefined;\n\n if (isTimeout(shape)) {\n return new ProviderTimeoutError(message, { cause: thrown, context, billed });\n }\n\n if (\n shape.status === 401 ||\n shape.status === 403 ||\n /permission_denied|api key not valid|unauthenticated/i.test(message)\n ) {\n return new ProviderAuthError(message, { cause: thrown, context, billed });\n }\n\n if (shape.status === 429 || /resource_exhausted|quota/i.test(message)) {\n return new ProviderRateLimitError(message, { cause: thrown, context, billed });\n }\n\n if (shape.status === 400) {\n if (/token count|context length|exceeds the maximum|input is too long/i.test(message)) {\n return new ContextLengthExceededError(message, { cause: thrown, context, billed });\n }\n\n return new InvalidRequestError(message, { cause: thrown, context, billed });\n }\n\n if (shape.status === 404 || isClientStatus(shape.status)) {\n return new InvalidRequestError(message, { cause: thrown, context, billed });\n }\n\n // 503 UNAVAILABLE — \"temporarily overloaded or down\"\n // (https://ai.google.dev/gemini-api/docs/api-errors).\n if (shape.status === 503 || /\\bUNAVAILABLE\\b/.test(message) || /overloaded/i.test(message)) {\n return new ProviderOverloadedError(message, { cause: thrown, context, billed });\n }\n\n return new ProviderError(message, { cause: thrown, context, billed });\n}\n\n/**\n * Read the raw error shape. The Gemini SDK's `ApiError` carries a\n * numeric `status`; flattened/proxied errors may carry it (or `code`)\n * loosely.\n */\nfunction toShape(thrown: unknown): GoogleErrorShape {\n if (thrown instanceof ApiError) {\n return { status: thrown.status, message: thrown.message, name: thrown.name };\n }\n\n if (typeof thrown === \"object\" && thrown !== null) {\n const raw = thrown as Record<string, unknown>;\n\n return {\n status: typeof raw.status === \"number\" ? raw.status : undefined,\n message: typeof raw.message === \"string\" ? raw.message : undefined,\n name: typeof raw.name === \"string\" ? raw.name : undefined,\n code: typeof raw.code === \"string\" ? raw.code : undefined,\n };\n }\n\n return {};\n}\n\n/**\n * Decide whether the error is a timeout. Gemini maps gateway timeouts\n * to HTTP 504 (`DEADLINE_EXCEEDED`); transport aborts surface as\n * `AbortError` / `ETIMEDOUT` / `ECONNABORTED`.\n */\nfunction isTimeout(shape: GoogleErrorShape): boolean {\n if (shape.status === 504) {\n return true;\n }\n\n if (shape.name === \"AbortError\" || /deadline_exceeded/i.test(shape.message ?? \"\")) {\n return true;\n }\n\n return shape.code === \"ETIMEDOUT\" || shape.code === \"ECONNABORTED\";\n}\n\n/** True for HTTP 4xx — a client-side request problem, not a server fault. */\nfunction isClientStatus(status: number | undefined): boolean {\n return typeof status === \"number\" && status >= 400 && status < 500;\n}\n\n/** Attach the diagnostic fields to `error.context`. */\nfunction buildContext(shape: GoogleErrorShape): Record<string, unknown> {\n const context: Record<string, unknown> = {};\n\n if (shape.status !== undefined) {\n context.status = shape.status;\n }\n\n if (shape.name) {\n context.code = shape.name;\n }\n\n return context;\n}\n","import {\n type EmbeddingBatchResult,\n type EmbeddingResult,\n type EmbeddingUsage,\n type EmbedderContract,\n} from \"@warlock.js/ai\";\nimport { log, type Logger } from \"@warlock.js/logger\";\nimport type { EmbedContentResponse, GoogleGenAI } from \"@google/genai\";\nimport type { GoogleEmbedderConfig } from \"./config.type\";\nimport { wrapGoogleError } from \"./utils\";\n\nconst LOG_MODULE = \"ai.google\";\n\n/**\n * Token usage is not returned by Gemini's `embedContent`, so every\n * embedding result reports a zeroed `EmbeddingUsage` (honest absence,\n * not a fabricated estimate).\n */\nconst NO_USAGE: EmbeddingUsage = { promptTokens: 0, totalTokens: 0 };\n\n/**\n * Google Gemini-backed implementation of `EmbedderContract`\n * (`gemini-embedding-001`, `text-embedding-004`, …) via\n * `models.embedContent`.\n *\n * **Role.** Converts text into floating-point vectors. Standalone\n * primitive — unrelated to generateContent / tools / the agent loop.\n *\n * **Batch is native.** Gemini's `embedContent` accepts an array of\n * inputs and returns embeddings in the same order, so `embedMany` is\n * a single request (unlike the Bedrock/Titan adapter, which has to\n * loop).\n *\n * **No usage.** Gemini's embed endpoint returns no token counts;\n * `usage` is always `{ promptTokens: 0, totalTokens: 0 }`.\n *\n * **Dimensions.** When no `dimensions` override is given,\n * `this.dimensions` starts at `0` and is populated from the first\n * response's vector length, then cached. Passing `dimensions`\n * forwards Gemini's `outputDimensionality` truncation hint and sets\n * the initial value immediately.\n *\n * @example\n * const embedder = new GoogleEmbedder(ai, { name: \"gemini-embedding-001\" });\n * const { vector } = await embedder.embed(\"Hello world\");\n * const { vectors } = await embedder.embedMany([\"doc 1\", \"doc 2\"]);\n */\nexport class GoogleEmbedder implements EmbedderContract {\n public readonly name: string;\n public readonly provider: string;\n public dimensions: number;\n\n private readonly ai: GoogleGenAI;\n private readonly configuredDimensions: number | undefined;\n private readonly logger: Logger = log;\n\n public constructor(ai: GoogleGenAI, config: GoogleEmbedderConfig, provider: string = \"google\") {\n this.ai = ai;\n this.name = config.name;\n this.provider = provider;\n this.configuredDimensions = config.dimensions;\n this.dimensions = config.dimensions ?? 0;\n }\n\n public async embed(input: string): Promise<EmbeddingResult> {\n const vectors = await this.request([input]);\n\n return { vector: vectors[0], dimensions: this.dimensions, usage: NO_USAGE };\n }\n\n public async embedMany(inputs: string[]): Promise<EmbeddingBatchResult> {\n const vectors = await this.request(inputs);\n\n return { vectors, dimensions: this.dimensions, usage: NO_USAGE };\n }\n\n /**\n * Shared transport: one `embedContent` call for the whole batch,\n * wrap provider errors, cache `dimensions` from the first vector,\n * and return the raw vectors in input order.\n */\n private async request(inputs: string[]): Promise<number[][]> {\n this.logger.debug(LOG_MODULE, \"embedder.request\", \"embedContent\", {\n model: this.name,\n count: inputs.length,\n });\n\n let response: EmbedContentResponse;\n\n try {\n response = await this.ai.models.embedContent({\n model: this.name,\n contents: inputs,\n ...(this.configuredDimensions !== undefined\n ? { config: { outputDimensionality: this.configuredDimensions } }\n : {}),\n });\n } catch (thrown) {\n const wrapped = wrapGoogleError(thrown);\n\n this.logger.error(LOG_MODULE, \"embedder.error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n throw wrapped;\n }\n\n const vectors = (response.embeddings ?? []).map((embedding) => embedding.values ?? []);\n\n if (this.dimensions === 0 && vectors[0]) {\n this.dimensions = vectors[0].length;\n }\n\n this.logger.debug(LOG_MODULE, \"embedder.response\", \"embedContent returned\", {\n count: vectors.length,\n dimensions: this.dimensions,\n });\n\n return vectors;\n }\n}\n","import {\n ContentFilterError,\n ProviderError,\n type AIError,\n type GeneratedImage,\n type ImageGenerationOptions,\n type ImageGenerationResponse,\n type ImageModelContract,\n type ImageModelPricing,\n type Usage,\n} from \"@warlock.js/ai\";\nimport { log, type Logger } from \"@warlock.js/logger\";\nimport type {\n GenerateContentConfig,\n GenerateContentResponse,\n GoogleGenAI,\n ImageConfig,\n Part,\n} from \"@google/genai\";\nimport type { GoogleImageConfig } from \"./config.type\";\nimport { applyGoogleUsage, wrapGoogleError } from \"./utils\";\n\nconst LOG_MODULE = \"ai.google\";\n\n/**\n * Response modalities requested when the caller names none.\n *\n * `IMAGE` is the modality this adapter extracts; `TEXT` rides along so\n * a model that narrates what it drew is not answering outside the set\n * it was granted (the narration is then dropped — only inline image\n * parts become `GeneratedImage`s).\n *\n * *Unverified:* which pairing any individual Gemini image model\n * requires is not established here — no spec or run in this package\n * touches the live API. `options.responseModalities` replaces this list\n * verbatim for a model that wants something else.\n */\nconst DEFAULT_RESPONSE_MODALITIES = [\"TEXT\", \"IMAGE\"];\n\n/**\n * Gemini `finishReason` values that mean generation was stopped by a\n * safety / policy rule rather than by the model simply not drawing.\n * Taken from the `FinishReason` enum in `@google/genai`'s own type\n * declarations, whose doc comments describe each of these as content\n * or image generation being \"stopped\" for safety, prohibited content,\n * recitation, blocklist, or SPII.\n */\nconst FILTERED_FINISH_REASONS = new Set([\n \"SAFETY\",\n \"IMAGE_SAFETY\",\n \"PROHIBITED_CONTENT\",\n \"IMAGE_PROHIBITED_CONTENT\",\n \"RECITATION\",\n \"IMAGE_RECITATION\",\n \"BLOCKLIST\",\n \"SPII\",\n]);\n\n/** How much of a text-only answer to quote back inside the error message. */\nconst TEXT_EXCERPT_LIMIT = 200;\n\n/**\n * Gemini-native implementation of `ImageModelContract`, via\n * `ai.models.generateContent` with `config.responseModalities`\n * including `\"IMAGE\"`.\n *\n * **Why a second image adapter.** `GoogleImageModel` calls\n * `ai.models.generateImages`, which the `@google/genai` bundle routes\n * to `{model}:predict` (`generateImages` → `generateImagesInternal` →\n * `formatMap('{model}:predict', …)`). A Gemini image model is not\n * served there: asking for one returns Google's\n * `404 … is not found for API version v1beta, or is not supported for\n * predict`. `generateContent` is the SDK's own named replacement — its\n * runtime deprecation notice for `generateImages` reads \"Please use the\n * generateContent method with image models instead\" — so that is the\n * transport this class speaks, hence a separate class rather than a\n * branch inside `image.ts`.\n *\n * **Same envelope.** Inline image parts are mapped to the identical\n * `GeneratedImage[]` shape `GoogleImageModel` produces, so `ai.image()`\n * callers see no difference between the two paths.\n *\n * **Token usage is passed through, not zeroed.** The Imagen path\n * returns a hard `{ 0, 0, 0 }` because Imagen reports no tokens at all;\n * here, whatever `usageMetadata` Google attaches is mapped by the same\n * {@link applyGoogleUsage} the chat model uses, and only an absent\n * block collapses to zeros. Price accordingly.\n *\n * **No model-id validation.** `config.name` is forwarded to\n * `generateContent` exactly as given; nothing here inspects it. An id\n * Google does not serve fails at Google, wrapped into the typed\n * `AIError` hierarchy — never with a local throw.\n *\n * **Evidence, in two tiers.** No spec in this package calls Google.\n * *Measured here:* a `gemini-*` image id, which 404s on the `predict`\n * transport, reached the model on this one and came back with a quota\n * error (HTTP 429) — the endpoint accepts the id. *Reported by the\n * maintainer:* once billing was enabled on the project, an image came\n * back end-to-end from an application running a locally linked build.\n * *Still unestablished:* whether these models report token usage — no\n * `usageMetadata` from a successful image call has been observed, so\n * the pass-through above is untested against a real response.\n *\n * @example\n * const model = new GeminiImageModel(ai, { name: \"gemini-3.1-flash-lite-image\" });\n * const { images, usage } = await model.generate(\"a red bicycle on a white background\");\n */\nexport class GeminiImageModel implements ImageModelContract {\n public readonly name: string;\n public readonly provider: string;\n public readonly pricing?: ImageModelPricing;\n\n private readonly ai: GoogleGenAI;\n private readonly logger: Logger = log;\n\n public constructor(ai: GoogleGenAI, config: GoogleImageConfig, provider: string = \"google\") {\n this.ai = ai;\n this.name = config.name;\n this.provider = provider;\n this.pricing = config.pricing;\n }\n\n public async generate(\n prompt: string,\n options?: ImageGenerationOptions,\n ): Promise<ImageGenerationResponse> {\n const config = this.buildConfig(options);\n\n this.logger.debug(LOG_MODULE, \"image.request\", \"models.generateContent\", {\n model: this.name,\n responseModalities: config.responseModalities,\n });\n\n let response: GenerateContentResponse;\n\n try {\n // `contents` takes a bare string: the SDK's own `generateContent`\n // example passes one (`contents: 'Why is the sky blue?'`).\n response = await this.ai.models.generateContent({\n model: this.name,\n contents: prompt,\n config,\n });\n } catch (thrown) {\n const wrapped = wrapGoogleError(thrown);\n\n this.logger.error(LOG_MODULE, \"image.error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n throw wrapped;\n }\n\n const parts = collectParts(response);\n const images = toGeneratedImages(parts);\n\n if (images.length === 0) {\n throw this.noImageError(response, parts);\n }\n\n const usage: Usage = { input: 0, output: 0, total: 0 };\n\n if (response.usageMetadata) {\n applyGoogleUsage(usage, response.usageMetadata);\n }\n\n this.logger.debug(LOG_MODULE, \"image.response\", \"models.generateContent succeeded\", {\n images: images.length,\n usage,\n });\n\n return { images, usage };\n }\n\n /**\n * Assemble the `GenerateContentConfig` for an image turn: the\n * requested modalities, the image-specific knobs Gemini exposes under\n * `imageConfig`, and the cancellation handle.\n *\n * Three neutral options are deliberately NOT forwarded, because\n * `GenerateContentConfig` / `ImageConfig` in `@google/genai` expose\n * no equivalent for them on this path: `count` (no per-request image\n * count — every inline image part the model does return is mapped),\n * `negativePrompt` (an Imagen-only field), and `format`\n * (`ImageConfig.outputMimeType` is documented \"not supported in\n * Gemini API\"). Fold those intentions into the prompt instead.\n */\n private buildConfig(options: ImageGenerationOptions | undefined): GenerateContentConfig {\n const imageConfig: ImageConfig = {};\n\n if (options?.aspectRatio !== undefined) {\n imageConfig.aspectRatio = options.aspectRatio;\n }\n\n // Provider passthroughs off the neutral options' index signature —\n // forwarded verbatim, never re-spelled, so the value the caller\n // wrote is the value Google rules on. `ImageConfig` documents\n // `imageSize` as `1K`/`2K`/`4K` and `personGeneration` as\n // `ALLOW_ALL`/`ALLOW_ADULT`/`ALLOW_NONE`.\n if (typeof options?.imageSize === \"string\") {\n imageConfig.imageSize = options.imageSize;\n }\n\n if (typeof options?.personGeneration === \"string\") {\n imageConfig.personGeneration = options.personGeneration;\n }\n\n const requested = options?.responseModalities;\n const responseModalities = Array.isArray(requested)\n ? (requested as string[])\n : DEFAULT_RESPONSE_MODALITIES;\n\n return {\n responseModalities,\n ...(Object.keys(imageConfig).length > 0 ? { imageConfig } : {}),\n ...(options?.signal ? { abortSignal: options.signal } : {}),\n };\n }\n\n /**\n * Build the typed error for a response that carried no inline image\n * part. Never a silent empty success: the caller asked for an image\n * and got something else, so the error names what actually came back.\n *\n * - A blocked prompt (`promptFeedback.blockReason`) or a\n * safety/policy `finishReason` → `ContentFilterError` carrying the\n * reason, matching how the Imagen path reports `raiFilteredReason`.\n * - A text-only answer → `ProviderError` quoting the text, so the\n * log says what the model replied instead of guessing.\n * - Anything else → `ProviderError` naming the finish reason and how\n * many parts arrived.\n */\n private noImageError(response: GenerateContentResponse, parts: Part[]): AIError {\n const blockReason = response.promptFeedback?.blockReason;\n\n if (blockReason) {\n return new ContentFilterError(`Gemini blocked the prompt for ${this.name}: ${blockReason}`, {\n reason: blockReason,\n });\n }\n\n const finishReason = response.candidates?.[0]?.finishReason;\n\n if (finishReason && FILTERED_FINISH_REASONS.has(finishReason)) {\n return new ContentFilterError(`Gemini filtered the image for ${this.name}: ${finishReason}`, {\n reason: finishReason,\n });\n }\n\n const text = collectText(parts);\n\n if (text) {\n return new ProviderError(\n `Gemini returned no image for ${this.name} — the response was text only: \"${excerpt(text)}\"`,\n { context: { model: this.name, ...(finishReason ? { finishReason } : {}) } },\n );\n }\n\n return new ProviderError(\n `Gemini returned no image part for ${this.name} (parts: ${parts.length}${\n finishReason ? `, finishReason: ${finishReason}` : \"\"\n }).`,\n { context: { model: this.name, parts: parts.length } },\n );\n }\n}\n\n/**\n * Flatten every candidate's content parts into one list. Read off\n * `candidates[].content.parts` rather than the response's convenience\n * getters: `response.text` covers only the first candidate's text and\n * there is no getter for inline image data at all.\n */\nfunction collectParts(response: GenerateContentResponse): Part[] {\n const parts: Part[] = [];\n\n for (const candidate of response.candidates ?? []) {\n parts.push(...(candidate.content?.parts ?? []));\n }\n\n return parts;\n}\n\n/**\n * Map the inline image parts to the neutral `GeneratedImage[]` — the\n * same `{ type: \"base64\", base64, mediaType }` shape the Imagen path\n * emits, including its `image/png` fallback for a part that arrives\n * without a declared mime type.\n */\nfunction toGeneratedImages(parts: Part[]): GeneratedImage[] {\n const images: GeneratedImage[] = [];\n\n for (const part of parts) {\n const data = part.inlineData?.data;\n\n if (!data) {\n continue;\n }\n\n images.push({\n type: \"base64\",\n base64: data,\n mediaType: part.inlineData?.mimeType ?? \"image/png\",\n });\n }\n\n return images;\n}\n\n/** Join the text parts of a response — what the model said instead of drawing. */\nfunction collectText(parts: Part[]): string {\n return parts\n .map((part) => part.text)\n .filter((text): text is string => typeof text === \"string\" && text.length > 0)\n .join(\" \")\n .trim();\n}\n\n/** Trim a quoted model answer so an error message stays readable. */\nfunction excerpt(text: string): string {\n return text.length > TEXT_EXCERPT_LIMIT ? `${text.slice(0, TEXT_EXCERPT_LIMIT)}…` : text;\n}\n","import {\n ContentFilterError,\n ProviderError,\n type GeneratedImage,\n type ImageGenerationOptions,\n type ImageGenerationResponse,\n type ImageModelContract,\n type ImageModelPricing,\n} from \"@warlock.js/ai\";\nimport { log, type Logger } from \"@warlock.js/logger\";\nimport type { GenerateImagesConfig, GoogleGenAI } from \"@google/genai\";\nimport type { GoogleImageConfig } from \"./config.type\";\nimport { wrapGoogleError } from \"./utils\";\n\nconst LOG_MODULE = \"ai.google\";\n\n/** Map a neutral output container hint to an IANA media type. */\nfunction mediaTypeFor(format: string | undefined): string | undefined {\n switch (format) {\n case \"png\":\n return \"image/png\";\n case \"jpeg\":\n case \"jpg\":\n return \"image/jpeg\";\n case \"webp\":\n return \"image/webp\";\n default:\n return undefined;\n }\n}\n\n/**\n * Google Imagen-backed implementation of `ImageModelContract`, via\n * `ai.models.generateImages`. Imagen is per-image-metered and returns\n * base64 image bytes (no hosted URL, no token usage).\n *\n * **No model-id validation.** `config.name` is passed through to\n * `ai.models.generateImages` exactly as given — the constructor never\n * inspects it. Google adds and retires image model ids on its own\n * schedule, so an id this adapter does not recognize is not the\n * adapter's call to refuse; an unsupported id surfaces as a provider\n * error from Google (wrapped into the typed `AIError` hierarchy by\n * `generate()`), not as a local one.\n *\n * **Safety filtering.** When Imagen filters every candidate for safety\n * (`raiFilteredReason`), this surfaces a typed `ContentFilterError`\n * carrying the reason, rather than returning an empty success.\n *\n * @example\n * const model = new GoogleImageModel(ai, { name: \"imagen-4.0-generate-001\" }, \"google\");\n * const { images } = await model.generate(\"a watercolor lighthouse at dawn\");\n */\nexport class GoogleImageModel implements ImageModelContract {\n public readonly name: string;\n public readonly provider: string;\n public readonly pricing?: ImageModelPricing;\n\n private readonly ai: GoogleGenAI;\n private readonly logger: Logger = log;\n\n public constructor(ai: GoogleGenAI, config: GoogleImageConfig, provider: string = \"google\") {\n this.ai = ai;\n this.name = config.name;\n this.provider = provider;\n this.pricing = config.pricing;\n }\n\n public async generate(\n prompt: string,\n options?: ImageGenerationOptions,\n ): Promise<ImageGenerationResponse> {\n const config: GenerateImagesConfig = {};\n\n if (options?.count !== undefined) config.numberOfImages = options.count;\n if (options?.aspectRatio !== undefined) config.aspectRatio = options.aspectRatio;\n if (options?.negativePrompt !== undefined) config.negativePrompt = options.negativePrompt;\n if (options?.signal !== undefined) config.abortSignal = options.signal;\n\n const outputMimeType = mediaTypeFor(options?.format);\n if (outputMimeType !== undefined) config.outputMimeType = outputMimeType;\n\n // Imagen sizing is `imageSize` (\"1K\"/\"2K\") — a distinct concept from\n // OpenAI's WxH `size`, so we honor only an explicit passthrough.\n if (typeof options?.imageSize === \"string\") config.imageSize = options.imageSize;\n if (typeof options?.personGeneration === \"string\") {\n config.personGeneration =\n options.personGeneration as GenerateImagesConfig[\"personGeneration\"];\n }\n\n this.logger.debug(LOG_MODULE, \"image.request\", \"models.generateImages\", {\n model: this.name,\n count: options?.count ?? 1,\n });\n\n let response: Awaited<ReturnType<GoogleGenAI[\"models\"][\"generateImages\"]>>;\n\n try {\n response = await this.ai.models.generateImages({ model: this.name, prompt, config });\n } catch (thrown) {\n const wrapped = wrapGoogleError(thrown);\n\n this.logger.error(LOG_MODULE, \"image.error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n throw wrapped;\n }\n\n const generated = response.generatedImages ?? [];\n const images: GeneratedImage[] = [];\n\n for (const candidate of generated) {\n const bytes = candidate.image?.imageBytes;\n if (!bytes) continue;\n\n images.push({\n type: \"base64\",\n base64: bytes,\n mediaType: candidate.image?.mimeType ?? outputMimeType ?? \"image/png\",\n ...(candidate.enhancedPrompt ? { revisedPrompt: candidate.enhancedPrompt } : {}),\n });\n }\n\n if (images.length === 0) {\n const filtered = generated.find((candidate) => candidate.raiFilteredReason);\n\n if (filtered?.raiFilteredReason) {\n throw new ContentFilterError(\n `Imagen filtered all candidates: ${filtered.raiFilteredReason}`,\n { reason: filtered.raiFilteredReason },\n );\n }\n\n throw new ProviderError(\"Imagen returned no images.\");\n }\n\n this.logger.debug(LOG_MODULE, \"image.response\", \"models.generateImages succeeded\", {\n images: images.length,\n });\n\n // Imagen returns no token usage — honest zero (priced per image).\n return { images, usage: { input: 0, output: 0, total: 0 } };\n }\n}\n","import type { ModelPricing } from \"@warlock.js/ai\";\n\n/**\n * Built-in Gemini API list prices (paid tier, Standard), USD per\n * 1,000,000 tokens, keyed by exact model id. `cachedInput` is the\n * context-cache read rate; the hourly cache-storage charge is not a token\n * rate and is never included.\n *\n * Source: https://ai.google.dev/gemini-api/docs/pricing, as of\n * {@link GOOGLE_MODEL_PRICING_AS_OF}. Prices Google has already scheduled\n * are in `schedule`, so a model costs the right amount on each day without\n * a package update; models priced by prompt length carry `tiers`.\n * Left out on purpose — pass `pricing` yourself for them:\n *\n * - Models with a higher audio-input rate (`gemini-3.1-flash-lite`,\n * `gemini-3-flash-preview`): `Usage` does not split input by modality.\n * - Models without a listed price.\n *\n * Models Google retired or reroutes stay out of the table; see\n * {@link GOOGLE_MODEL_LIFECYCLE}.\n */\nexport const GOOGLE_MODEL_PRICING: Readonly<Record<string, ModelPricing>> = {\n \"gemini-4-argon\": {\n input: 2,\n output: 10,\n cachedInput: 0.1,\n schedule: [{ from: \"2026-12-01\", input: 4, output: 20, cachedInput: 0.2 }],\n },\n \"gemini-3.8-flash\": {\n input: 0.75,\n output: 3.75,\n cachedInput: 0.075,\n schedule: [{ from: \"2027-01-01\", input: 1.5, output: 7.5, cachedInput: 0.15 }],\n },\n \"gemini-3.6-flash\": {\n input: 0.75,\n output: 3.75,\n cachedInput: 0.075,\n schedule: [{ from: \"2027-01-01\", input: 1.5, output: 7.5, cachedInput: 0.15 }],\n },\n \"gemini-3.5-flash-lite\": { input: 0.3, output: 2.5, cachedInput: 0.03 },\n \"gemini-3.1-pro-preview\": {\n input: 2,\n output: 12,\n cachedInput: 0.2,\n tiers: [{ abovePromptTokens: 200_000, input: 4, output: 18, cachedInput: 0.4 }],\n },\n};\n\n/** The date the {@link GOOGLE_MODEL_PRICING} rates were last checked against Google's pricing page. */\nexport const GOOGLE_MODEL_PRICING_AS_OF = \"2026-11-06\";\n\n/**\n * Look up the built-in list price for a Gemini model id (exact match),\n * or `undefined` so cost stays unknown rather than wrong.\n *\n * @example\n * googlePricingFor(\"gemini-3.5-flash-lite\"); // { input: 0.3, output: 2.5, cachedInput: 0.03 }\n * googlePricingFor(\"gemini-2.5-pro\"); // undefined — not in the table\n */\nexport function googlePricingFor(modelName: string): ModelPricing | undefined {\n return GOOGLE_MODEL_PRICING[modelName];\n}\n\n/** USD per nonempty unique query for Gemini 3+, before free monthly allowances.\n * Gemini 2.5 and older bill per grounded prompt; do not use this rate for them.\n * https://ai.google.dev/gemini-api/docs/google-search#pricing — verified 2026-10-18. */\nexport const GOOGLE_SERVER_TOOL_PRICING = { webSearch: 0.014 };\n","import {\n InvalidRequestError,\n type ModelCallOptions,\n type ProviderToolDefinition,\n} from \"@warlock.js/ai\";\n\n/**\n * A running allowance of Google Search queries, shared by the model calls of\n * one run. Create it with {@link googleSearchBudget} and pass it as\n * `providerOptions.google.search.budget` on every call of that run.\n */\nexport type GoogleSearchBudget = {\n /** The allowance the budget was created with. */\n readonly max: number;\n /** Unique search queries the provider reported so far. */\n readonly used: number;\n /** `max - used`, never below zero. */\n readonly remaining: number;\n /** Add queries the provider reported for one finished call. */\n record(queries: number): void;\n};\n\n/**\n * Bound Google Search grounding across the model calls of one run.\n *\n * The Gemini API has no per-request search limit: `{ googleSearch: {} }` takes\n * no `max_uses`, and the model decides how many queries one `generateContent`\n * call runs. What the adapter can enforce is the number of queries across\n * calls: after each call it adds the queries Google reported\n * (`groundingMetadata.webSearchQueries`, counted the way Gemini 3 bills them,\n * unique and non-empty) to the budget, and once the budget is spent it stops\n * sending the `googleSearch` tool, so later calls run without search. One\n * call that is already running can still overshoot the budget by the queries\n * it makes itself.\n *\n * @example\n * const budget = googleSearchBudget(3);\n * const search = ai.providerTool({ provider: \"google\", definition: { googleSearch: {} } });\n * await model.complete(messages, {\n * tools: [search],\n * providerOptions: { google: { search: { budget } } },\n * });\n * budget.used; // queries spent so far\n */\nexport function googleSearchBudget(maxQueries: number): GoogleSearchBudget {\n if (!Number.isInteger(maxQueries) || maxQueries < 0) {\n throw new InvalidRequestError(\n `googleSearchBudget(maxQueries) needs a non-negative integer, got ${String(maxQueries)}.`,\n );\n }\n\n let used = 0;\n\n return {\n max: maxQueries,\n get used() {\n return used;\n },\n get remaining() {\n return Math.max(0, maxQueries - used);\n },\n record(queries) {\n if (Number.isFinite(queries) && queries > 0) {\n used += queries;\n }\n },\n };\n}\n\n/** The budget on a call, validated; `undefined` when the call sets none. */\nexport function searchBudgetOf(\n options: ModelCallOptions | undefined,\n): GoogleSearchBudget | undefined {\n const search = (options?.providerOptions?.google as { search?: { budget?: unknown } } | undefined)\n ?.search;\n const budget = search?.budget;\n\n if (budget === undefined) {\n return undefined;\n }\n\n if (\n typeof budget !== \"object\" ||\n budget === null ||\n typeof (budget as GoogleSearchBudget).record !== \"function\" ||\n typeof (budget as GoogleSearchBudget).remaining !== \"number\"\n ) {\n throw new InvalidRequestError(\n \"providerOptions.google.search.budget must come from googleSearchBudget(maxQueries).\",\n );\n }\n\n return budget as GoogleSearchBudget;\n}\n\n/** `true` for a Gemini tool entry that runs Google Search grounding. */\nexport function isSearchDefinition(definition: ProviderToolDefinition[\"definition\"]): boolean {\n return \"googleSearch\" in definition || \"googleSearchRetrieval\" in definition;\n}\n","import type { GenerateContentResponse, GroundingMetadata } from \"@google/genai\";\nimport type { Usage } from \"@warlock.js/ai\";\n\n/** Candidate snapshots are replaced, not added: streamed metadata may repeat.\n * Installed @google/genai 2.19.0 GroundingMetadata.webSearchQueries; REST schema:\n * https://ai.google.dev/api/generate-content#GroundingMetadata\n * Billing ignores empty queries and counts unique queries for Gemini 3+:\n * https://ai.google.dev/gemini-api/docs/google-search#pricing\n * Gemini 2.5 and older bill per grounded prompt; this counter is NOT that unit. */\nexport function applyGrounding(\n usage: Usage,\n snapshots: Map<number, GroundingMetadata>,\n response: GenerateContentResponse,\n): void {\n for (const [position, candidate] of (response.candidates ?? []).entries()) {\n if (candidate.groundingMetadata)\n snapshots.set(candidate.index ?? position, candidate.groundingMetadata);\n }\n const reported = [...snapshots.values()].filter(\n (metadata) => metadata.webSearchQueries !== undefined,\n );\n if (reported.length > 0) {\n const queries = reported.flatMap((metadata) => metadata.webSearchQueries ?? []);\n usage.serverToolUse = {\n ...usage.serverToolUse,\n webSearchRequests: new Set(queries.filter((query) => query !== \"\")).size,\n googleWebSearchQueries: queries.length,\n };\n }\n}\n\n/** Retain chunks, supports, search entry point and queries without flattening them. */\nexport function groundingMetadata(\n snapshots: Map<number, GroundingMetadata>,\n): Record<string, unknown> | undefined {\n return snapshots.size\n ? {\n google: {\n groundingMetadata: [...snapshots].map(([index, metadata]) => ({ index, ...metadata })),\n },\n }\n : undefined;\n}\n","import type { GoogleSearchBudget } from \"./search-budget\";\nimport { InvalidRequestError, type ModelCallOptions } from \"@warlock.js/ai\";\n\n/** Adapter-only fields merged after the neutral request parameters. */\nexport type GoogleCallOptions = {\n body?: Record<string, unknown>;\n /**\n * Bound Google Search across the calls of one run. See\n * `googleSearchBudget()`: the Gemini API has no per-request limit, so this\n * stops offering the search tool once the allowance is spent.\n */\n search?: { budget?: GoogleSearchBudget };\n};\n\nconst RESERVED_FIELDS = new Set([\"systemInstruction\", \"tools\", \"abortSignal\", \"httpOptions\"]);\n\nexport function mergeProviderBody<T extends object>(\n built: T,\n options: ModelCallOptions | undefined,\n): T {\n const provider = options?.providerOptions?.google as GoogleCallOptions | undefined;\n const body = provider?.body ?? {};\n const reserved = Object.keys(body).filter((key) => RESERVED_FIELDS.has(key));\n if (reserved.length > 0) {\n throw new InvalidRequestError(\n `providerOptions.google.body cannot set ${reserved.join(\", \")}: use the call's messages, tools and model options.`,\n { context: { fields: reserved } },\n );\n }\n const defaults = built as Record<string, unknown>;\n const overrides = Object.fromEntries(\n Object.entries(body).map(([key, value]) => {\n const previous = defaults[key];\n return [\n key,\n isPlainObject(previous) && isPlainObject(value) ? { ...previous, ...value } : value,\n ];\n }),\n );\n return { ...built, ...overrides };\n}\n\n/** Merge only plain objects; arrays, null and provider scalar values replace the field. */\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n if (value === null || typeof value !== \"object\") return false;\n const prototype = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n}\n","/*\n * Input context windows of Gemini families documented at 1,048,576 tokens (and 2,097,152 for 1.5 Pro);\n * Gemini 4 documents \"1M tokens\" and uses the same figure.\n * Other ids resolve to `undefined`; set them with\n * `google.model({ name, contextWindow })`.\n */\nconst WINDOW_1M_PREFIXES = [\n \"gemini-4\",\n \"gemini-3\",\n \"gemini-2.5-\",\n \"gemini-2.0-flash\",\n \"gemini-1.5-flash\",\n];\n\n/** Gemini 1.5 Pro documents a 2,097,152-token input window. */\nconst WINDOW_2M_PREFIXES = [\"gemini-1.5-pro\"];\n\n/**\n * The known context window of a Gemini model id, or `undefined` when it is\n * not in the table. A leading `models/` is accepted.\n *\n * @example\n * inferContextWindow(\"gemini-2.5-flash\"); // → 1048576\n */\nexport function inferContextWindow(modelName: string): number | undefined {\n const id = modelName.toLowerCase().replace(/^models\\//, \"\");\n\n if (WINDOW_2M_PREFIXES.some((prefix) => id.startsWith(prefix))) {\n return 2_097_152;\n }\n\n return WINDOW_1M_PREFIXES.some((prefix) => id.startsWith(prefix)) ? 1_048_576 : undefined;\n}\n","/**\n * Substrings identifying Gemini model ids whose family accepts image\n * input (vision).\n *\n * Every Gemini 1.5, 2.x, and 2.5 model is natively multimodal, as is\n * the legacy `gemini-pro-vision`. Only the original text-only\n * `gemini-pro` / `gemini-1.0-pro` is excluded. A substring match\n * tolerates the date/preview suffixes Google appends\n * (`gemini-2.5-flash-preview-05-20`). Override per-model via\n * `google.model({ name, vision: true | false })`.\n */\nconst VISION_CAPABLE_SUBSTRINGS = [\n \"gemini-1.5\",\n \"gemini-2\",\n \"gemini-exp\",\n \"gemini-pro-vision\",\n \"gemini-flash\",\n];\n\n/**\n * Infer whether a Gemini model id supports vision based on the known\n * multimodal-family substrings. Unknown ids default to `false` so\n * passing an image attachment to an unsupported model surfaces a\n * clear, agent-side capability error instead of an opaque Gemini 400.\n *\n * @example\n * inferVisionCapability(\"gemini-2.5-flash\"); // → true\n * inferVisionCapability(\"gemini-1.5-pro-002\"); // → true\n * inferVisionCapability(\"gemini-1.0-pro\"); // → false\n * inferVisionCapability(\"text-embedding-004\"); // → false\n */\nexport function inferVisionCapability(modelId: string): boolean {\n const normalized = modelId.toLowerCase();\n\n return VISION_CAPABLE_SUBSTRINGS.some((fragment) => normalized.includes(fragment));\n}\n","import {\n attachCost,\n InvalidRequestError,\n resolveStreamChunks,\n selectProviderTools,\n type Message,\n type ModelCallOptions,\n type ModelCapabilities,\n type ModelContract,\n type ModelPricing,\n type ModelResponse,\n type ModelStreamChunk,\n type ModelToolCallRequest,\n type ReasoningEffort,\n type Usage,\n} from \"@warlock.js/ai\";\nimport { log, type Logger } from \"@warlock.js/logger\";\nimport type {\n GenerateContentConfig,\n GenerateContentResponse,\n GoogleGenAI,\n GroundingMetadata,\n Part,\n Tool,\n} from \"@google/genai\";\nimport { isSearchDefinition, searchBudgetOf } from \"./search-budget\";\nimport { applyGrounding, groundingMetadata } from \"./utils/grounding\";\nimport { mergeProviderBody } from \"./provider-options\";\nimport type { GoogleModelConfig } from \"./config.type\";\nimport { inferContextWindow } from \"./known-context-windows\";\nimport { inferVisionCapability } from \"./known-vision-models\";\nimport {\n applyGoogleUsage,\n mapFinishReason,\n toGoogleContents,\n toGoogleTools,\n wrapGoogleError,\n} from \"./utils\";\n\nconst LOG_MODULE = \"ai.google\";\n\n/** `provider` value of `ai.providerTool()` entries this adapter sends verbatim. */\nconst GOOGLE_WIRE_FORMAT = \"google\";\n\n/**\n * Bucketed `thinkingBudget` (token caps) for the neutral\n * `reasoning.effort` levels when the caller gives no explicit\n * `reasoning.maxTokens`. Gemini 2.5 accepts a positive budget as a cap\n * on the thinking phase; these mirror the spread the OpenAI\n * `reasoning_effort` low/medium/high tiers imply. The installed\n * `@google/genai` `ThinkingConfig` declaration documents that allowed\n * ranges are model-dependent, so retain the existing conservative 24,576\n * cap for the new tiers rather than sending a budget unsupported by Flash.\n */\nconst EFFORT_THINKING_BUDGET: Record<Exclude<ReasoningEffort, \"none\">, number> = {\n minimal: 1024,\n low: 1024,\n medium: 8192,\n high: 24576,\n xhigh: 24576,\n max: 24576,\n};\n\n/**\n * Google Gemini-backed implementation of `ModelContract`.\n *\n * **Role.** The provider-facing bridge between the vendor-neutral\n * `@warlock.js/ai` agent runtime and the `@google/genai` SDK\n * (`models.generateContent` / `generateContentStream`).\n *\n * **Responsibility.**\n * - Owns: a long-lived `GoogleGenAI` client + frozen `ModelConfig`\n * (name, temperature, maxTokens) used as per-call defaults.\n * - Owns: translating vendor-neutral `Message[]` / `ToolConfig[]` into\n * Gemini shapes (systemInstruction hoisting, `model` role,\n * `functionCall` / `functionResponse` parts, inline image bytes) on\n * the way out, and Gemini's candidate/parts response (text, function\n * calls, finish reason, token usage) back into neutral shapes on the\n * way in.\n * - Does NOT own: dispatching tools, looping, history, retries — those\n * are agent concerns. The model is a per-call protocol adapter.\n *\n * Modeled as a class (see §4.2 of code-style.md — \"long-lived state\n * across calls\"): the `GoogleGenAI` client is reused for the SDK's\n * lifetime.\n *\n * @example\n * import { GoogleGenAI } from \"@google/genai\";\n * const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });\n * const model = new GoogleModel(ai, { name: \"gemini-2.5-flash\" });\n *\n * const myAgent = agent({ model, tools: [searchTool] });\n * const result = await myAgent.execute(\"Summarize today's news.\");\n */\nexport class GoogleModel implements ModelContract {\n public readonly name: string;\n public readonly provider: string;\n public readonly capabilities: ModelCapabilities;\n public readonly pricing?: ModelPricing;\n\n private readonly ai: GoogleGenAI;\n private readonly config: GoogleModelConfig;\n private readonly logger: Logger = log;\n\n public constructor(ai: GoogleGenAI, config: GoogleModelConfig, provider: string = \"google\") {\n this.ai = ai;\n this.config = config;\n this.name = config.name;\n this.provider = provider;\n this.pricing = config.pricing;\n const multimodal = config.vision ?? inferVisionCapability(config.name);\n\n const window =\n config.contextWindow ?? (provider === \"google\" ? inferContextWindow(config.name) : undefined);\n\n this.capabilities = {\n ...(window !== undefined ? { contextWindow: window } : {}),\n structuredOutput: config.structuredOutput ?? true,\n vision: multimodal,\n // Function responses are JSON objects here; images fall back to text.\n toolResultImages: false,\n // Every Gemini 2.5 model thinks; older families harmlessly ignore\n // an empty thinking budget. Defaulting `true` lets the agent\n // forward reasoning options; an explicit `false` opts a model out.\n reasoning: config.reasoning ?? true,\n // Gemini reports cache-read hits (`cachedContentTokenCount`) on\n // every call via implicit caching, and accepts explicit context\n // caching. Read-side accounting is always honored.\n promptCaching: true,\n // The multimodal Gemini families that accept images also accept\n // audio and PDF/document parts. Mirror the vision inference unless\n // explicitly overridden.\n audio: config.audio ?? multimodal,\n pdf: config.pdf ?? multimodal,\n };\n }\n\n /**\n * Single-shot completion. Sends the full message list to\n * `generateContent`, waits for the terminal response, and reshapes\n * it into a vendor-neutral `ModelResponse`. Per-call `options`\n * override the instance defaults for this call only.\n */\n public async complete(messages: Message[], options?: ModelCallOptions): Promise<ModelResponse> {\n this.logger.debug(LOG_MODULE, \"request\", \"Starting generateContent call\", {\n model: this.name,\n messageCount: messages.length,\n streaming: false,\n toolCount: options?.tools?.length ?? 0,\n });\n\n const { systemInstruction, contents } = toGoogleContents(messages);\n\n let response: GenerateContentResponse;\n\n try {\n response = await this.ai.models.generateContent({\n model: this.name,\n contents,\n config: this.buildConfig(systemInstruction, options),\n });\n } catch (thrown) {\n throw this.logAndWrap(thrown);\n }\n\n const toolCalls = this.extractToolCalls(response);\n const finishReason = toolCalls\n ? \"tool_calls\"\n : mapFinishReason(response.candidates?.[0]?.finishReason);\n const usage = this.extractUsage(response);\n const grounding = new Map<number, GroundingMetadata>();\n applyGrounding(usage, grounding, response);\n searchBudgetOf(options)?.record(usage.serverToolUse?.webSearchRequests ?? 0);\n const providerMetadata = groundingMetadata(grounding);\n attachCost(usage, this.pricing);\n\n this.logger.debug(LOG_MODULE, \"response\", \"generateContent call succeeded\", {\n finishReason,\n usage,\n });\n\n return {\n ...(providerMetadata ? { providerMetadata } : {}),\n content: response.text ?? \"\",\n finishReason,\n usage,\n toolCalls,\n };\n }\n\n /** Count native input tokens with the same converted content, system and tools. */\n public async countTokens(messages: Message[], options?: ModelCallOptions): Promise<number> {\n const { systemInstruction, contents } = toGoogleContents(messages);\n const config = this.buildConfig(systemInstruction, options);\n if (config.cachedContent !== undefined) {\n throw new InvalidRequestError(\n \"The Google SDK countTokens endpoint cannot count a cachedContent reference.\",\n { reason: \"count-unsupported\" },\n );\n }\n try {\n const response = this.ai.vertexai\n ? await this.ai.models.countTokens({\n model: this.name,\n contents,\n config: {\n ...(config.systemInstruction !== undefined\n ? { systemInstruction: config.systemInstruction }\n : {}),\n ...(config.tools !== undefined ? { tools: config.tools as Tool[] } : {}),\n ...(config.abortSignal ? { abortSignal: config.abortSignal } : {}),\n ...(config.httpOptions ? { httpOptions: config.httpOptions } : {}),\n },\n })\n : await this.countDeveloperTokens(contents, config);\n if (response.totalTokens === undefined)\n throw new InvalidRequestError(\"Google countTokens returned no totalTokens.\");\n return response.totalTokens;\n } catch (thrown) {\n throw this.logAndWrap(thrown);\n }\n }\n\n /**\n * REST counts the full generation request, unlike the typed Developer API\n * converter in @google/genai 2.19.0. See https://ai.google.dev/api/tokens.\n * extraBody keeps the SDK's auth, fetch, base URL, retries and cancellation.\n */\n private countDeveloperTokens(\n contents: ReturnType<typeof toGoogleContents>[\"contents\"],\n config: GenerateContentConfig,\n ) {\n config.abortSignal?.throwIfAborted();\n\n const {\n systemInstruction,\n serviceTier,\n tools,\n toolConfig,\n safetySettings,\n cachedContent,\n abortSignal,\n httpOptions,\n ...generationConfig\n } = config;\n // Automatic function calling is SDK-only, never a REST generation field.\n delete generationConfig.automaticFunctionCalling;\n const model = this.name.startsWith(\"models/\") ? this.name : `models/${this.name}`;\n\n return this.ai.models.countTokens({\n model: this.name,\n contents,\n config: {\n abortSignal,\n httpOptions: {\n ...httpOptions,\n extraBody: {\n ...httpOptions?.extraBody,\n // The SDK merges extraBody before JSON serialization. undefined\n // removes its contents field: REST accepts only one input form.\n contents: undefined,\n generateContentRequest: {\n model,\n contents,\n ...(serviceTier !== undefined ? { serviceTier } : {}),\n ...(systemInstruction !== undefined\n ? { systemInstruction: { parts: [{ text: systemInstruction }] } }\n : {}),\n ...(tools !== undefined ? { tools } : {}),\n ...(toolConfig !== undefined ? { toolConfig } : {}),\n ...(safetySettings !== undefined ? { safetySettings } : {}),\n ...(cachedContent !== undefined ? { cachedContent } : {}),\n ...(Object.keys(generationConfig).length > 0 ? { generationConfig } : {}),\n },\n },\n },\n },\n });\n }\n\n /**\n * Incremental streaming completion via `generateContentStream`.\n * Yields neutral `ModelStreamChunk`s — `delta` for text, `tool-call`\n * per function call (Gemini emits a fully-formed call, not partial\n * JSON), and a terminal `done` with the final finish reason + usage.\n *\n * With `options.streamChunks` it also yields `reasoning` for thought\n * parts (only sent when `thinkingConfig.includeThoughts` is on — set it\n * through `providerOptions.google.body`) and `usage` whenever a chunk\n * carries `usageMetadata`. No `tool-args`: calls arrive whole.\n */\n public async *stream(\n messages: Message[],\n options?: ModelCallOptions,\n ): AsyncIterable<ModelStreamChunk> {\n this.logger.debug(LOG_MODULE, \"request\", \"Starting generateContentStream call\", {\n model: this.name,\n messageCount: messages.length,\n streaming: true,\n toolCount: options?.tools?.length ?? 0,\n });\n\n const { systemInstruction, contents } = toGoogleContents(messages);\n\n let iterable: AsyncGenerator<GenerateContentResponse>;\n\n try {\n iterable = await this.ai.models.generateContentStream({\n model: this.name,\n contents,\n config: this.buildConfig(systemInstruction, options),\n });\n } catch (thrown) {\n throw this.logAndWrap(thrown);\n }\n\n let rawFinishReason: string | undefined;\n let sawToolCall = false;\n const usage: Usage = { input: 0, output: 0, total: 0 };\n const want = resolveStreamChunks(options);\n const grounding = new Map<number, GroundingMetadata>();\n\n try {\n for await (const chunk of iterable) {\n applyGrounding(usage, grounding, chunk);\n // Thought parts (`thought: true`, sent when `thinkingConfig.includeThoughts`\n // is on) carry the thought summary in `text`; the SDK's `chunk.text`\n // getter already leaves them out of the answer.\n if (want.reasoning) {\n for (const part of chunk.candidates?.[0]?.content?.parts ?? []) {\n if (part.thought === true && typeof part.text === \"string\" && part.text !== \"\") {\n yield { type: \"reasoning\", delta: part.text };\n }\n }\n }\n\n const text = chunk.text;\n\n if (text) {\n yield { type: \"delta\", content: text };\n }\n\n for (const part of chunk.candidates?.[0]?.content?.parts ?? []) {\n const toolCall = this.partToToolCall(part);\n\n if (!toolCall) {\n continue;\n }\n\n sawToolCall = true;\n\n yield {\n type: \"tool-call\",\n id: toolCall.id,\n name: toolCall.name,\n input: toolCall.input,\n ...(toolCall.providerMetadata ? { providerMetadata: toolCall.providerMetadata } : {}),\n };\n }\n\n const candidateFinish = chunk.candidates?.[0]?.finishReason;\n\n if (candidateFinish) {\n rawFinishReason = candidateFinish;\n }\n\n if (chunk.usageMetadata) {\n applyGoogleUsage(usage, chunk.usageMetadata);\n\n if (want.usage) {\n yield { type: \"usage\", usage: { ...usage } };\n }\n }\n }\n } catch (thrown) {\n throw this.logAndWrap(thrown);\n }\n\n const finishReason = sawToolCall ? \"tool_calls\" : mapFinishReason(rawFinishReason);\n\n attachCost(usage, this.pricing);\n\n this.logger.debug(LOG_MODULE, \"response\", \"generateContentStream call succeeded\", {\n finishReason,\n usage,\n });\n\n searchBudgetOf(options)?.record(usage.serverToolUse?.webSearchRequests ?? 0);\n const providerMetadata = groundingMetadata(grounding);\n yield { type: \"done\", finishReason, usage, ...(providerMetadata ? { providerMetadata } : {}) };\n }\n\n /**\n * Assemble the `GenerateContentConfig` shared by `complete()` and\n * `stream()`: inference params, hoisted system instruction,\n * cancellation signal, and conditional tools + native structured\n * output.\n */\n private buildConfig(\n systemInstruction: string | undefined,\n options: ModelCallOptions | undefined,\n ): GenerateContentConfig {\n const temperature = options?.temperature ?? this.config.temperature;\n const maxOutputTokens = options?.maxTokens ?? this.config.maxTokens;\n\n return mergeProviderBody<GenerateContentConfig>(\n {\n ...(systemInstruction ? { systemInstruction } : {}),\n ...(temperature !== undefined ? { temperature } : {}),\n ...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}),\n ...(options?.signal ? { abortSignal: options.signal } : {}),\n ...this.buildTools(options),\n ...this.buildStructuredOutput(options?.responseSchema),\n ...this.buildThinking(options?.reasoning),\n },\n options,\n );\n }\n\n /**\n * Translate the neutral `reasoning` option into Gemini's\n * `thinkingConfig`. `reasoning.maxTokens` maps directly to\n * `thinkingBudget` (token cap on the thinking phase); when only\n * `reasoning.effort` is given it is bucketed into a budget. Emitted\n * only when the model is `reasoning`-capable — a `false` capability\n * (config override) drops it so a non-thinking model never receives\n * an unsupported `thinkingConfig`.\n *\n * Gemini's `thinkingBudget` semantics: `0` disables thinking, `-1`\n * lets the model decide automatically. A positive value caps the\n * thinking tokens. The neutral `effort: \"none\"` (\"run without\n * reasoning\") maps to `thinkingBudget: 0`.\n */\n private buildThinking(\n reasoning: ModelCallOptions[\"reasoning\"],\n ): Pick<GenerateContentConfig, \"thinkingConfig\"> {\n if (!reasoning || !this.capabilities.reasoning) {\n return {};\n }\n\n // `effort: \"none\"` = explicit \"run without reasoning\". Gemini disables\n // thinking with `thinkingBudget: 0` (its native off switch), so emit\n // that rather than omitting the config — an omitted config lets a\n // thinking model reason at its default budget.\n if (reasoning.effort === \"none\") {\n return { thinkingConfig: { thinkingBudget: 0 } };\n }\n\n const thinkingBudget =\n reasoning.maxTokens ??\n (reasoning.effort ? EFFORT_THINKING_BUDGET[reasoning.effort] : undefined);\n\n if (thinkingBudget === undefined) {\n return {};\n }\n\n return { thinkingConfig: { thinkingBudget } };\n }\n\n /**\n * Spread-friendly tools fragment. Empty object when no tools were\n * supplied so the caller can unconditionally spread it. Function tools\n * come first; `ai.providerTool({ provider: \"google\" })` definitions\n * (`{ googleSearch: {} }`, `{ codeExecution: {} }`, `{ urlContext: {} }`,\n * …) follow verbatim as their own `Tool` entries. Other providers'\n * definitions are skipped with a warning.\n */\n private buildTools(options: ModelCallOptions | undefined): Pick<GenerateContentConfig, \"tools\"> {\n const { tools, definitions } = selectProviderTools(options, {\n accepts: [GOOGLE_WIRE_FORMAT, this.provider],\n module: LOG_MODULE,\n model: this.name,\n logger: this.logger,\n });\n const budget = searchBudgetOf(options);\n // The Gemini API cannot bound searches inside one call, so a spent budget\n // is enforced here by not offering Google Search again.\n const offered =\n budget && budget.remaining <= 0\n ? definitions.filter((definition) => !isSearchDefinition(definition))\n : definitions;\n\n if (offered.length < definitions.length) {\n this.logger.warn(\n LOG_MODULE,\n \"search-budget\",\n \"Google Search budget is spent; calling without the search tool\",\n { model: this.name, max: budget?.max, used: budget?.used },\n );\n }\n\n const mapped: Tool[] = [...(toGoogleTools(tools) ?? []), ...(offered as unknown as Tool[])];\n\n return mapped.length > 0 ? { tools: mapped } : {};\n }\n\n /**\n * Translate the neutral `responseSchema` into Gemini's native JSON\n * structured output (`responseMimeType: \"application/json\"` +\n * `responseJsonSchema`, which takes a raw JSON Schema directly).\n * Emitted only when the model is `structuredOutput`-capable and the\n * schema is an object root — otherwise the agent's soft prompt hint\n * + client-side `validate()` carry shape.\n */\n private buildStructuredOutput(\n responseSchema: Record<string, unknown> | undefined,\n ): Pick<GenerateContentConfig, \"responseMimeType\" | \"responseJsonSchema\"> {\n if (!responseSchema || !this.capabilities.structuredOutput) {\n return {};\n }\n\n if (responseSchema.type !== \"object\" || typeof responseSchema.properties !== \"object\") {\n return {};\n }\n\n return {\n responseMimeType: \"application/json\",\n responseJsonSchema: responseSchema,\n };\n }\n\n /**\n * Reshape Gemini's function-call content parts into the neutral\n * `ModelToolCallRequest[]`. Returns `undefined` when the model\n * requested no functions so callers can branch on presence.\n *\n * Reads `candidates[0].content.parts` directly rather than the\n * `response.functionCalls` getter: the getter discards the\n * part-level `thoughtSignature`, and Gemini \"thinking\" models 400\n * the follow-up turn if that signature is not echoed back. See\n * `partToToolCall`.\n */\n private extractToolCalls(response: GenerateContentResponse): ModelToolCallRequest[] | undefined {\n const parts = response.candidates?.[0]?.content?.parts ?? [];\n const toolCalls = parts\n .map((part) => this.partToToolCall(part))\n .filter((call): call is ModelToolCallRequest => call !== undefined);\n\n return toolCalls.length > 0 ? toolCalls : undefined;\n }\n\n /**\n * Map a single Gemini `Part` to a neutral `ModelToolCallRequest`,\n * or `undefined` when the part is not a function call. The part's\n * `thoughtSignature` (opaque, set by thinking models) is carried on\n * `providerMetadata` so `toGoogleContents` can replay it on the\n * assistant turn — Gemini rejects the next request without it.\n */\n private partToToolCall(part: Part): ModelToolCallRequest | undefined {\n if (!part.functionCall) {\n return undefined;\n }\n\n const call = part.functionCall;\n\n return {\n // The Gemini Developer API does not assign function-call ids\n // (only Vertex parallel-calling does). Fall back to the function\n // name so the neutral `toolCallId` is non-empty and the echoed\n // `functionResponse.name` resolves — Gemini matches a result to\n // its call by name. See decisions §49.\n id: call.id ?? call.name ?? \"\",\n name: call.name ?? \"\",\n input: (call.args ?? {}) as Record<string, unknown>,\n ...(part.thoughtSignature\n ? { providerMetadata: { thoughtSignature: part.thoughtSignature } }\n : {}),\n };\n }\n\n /**\n * Normalize Gemini's `usageMetadata` into the neutral `Usage` shape\n * via the shared {@link applyGoogleUsage} mapper (the same one the\n * streaming loop and the Gemini image model use). Absent usage\n * collapses to zeros.\n */\n private extractUsage(response: GenerateContentResponse): Usage {\n const usage: Usage = { input: 0, output: 0, total: 0 };\n\n if (response.usageMetadata) {\n applyGoogleUsage(usage, response.usageMetadata);\n }\n\n return usage;\n }\n\n /**\n * Wrap a thrown provider error into the typed `AIError` hierarchy\n * and emit the standard error log line before it propagates.\n */\n private logAndWrap(thrown: unknown) {\n const wrapped = wrapGoogleError(thrown);\n\n this.logger.error(LOG_MODULE, \"error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n return wrapped;\n }\n}\n","import { GoogleGenAI } from \"@google/genai\";\nimport type {\n EmbedderContract,\n ListModelsOptions,\n ModelInfo,\n ModelKind,\n ImageModelContract,\n ModelContract,\n ModelPricing,\n SDKAdapterContract,\n} from \"@warlock.js/ai\";\nimport { approximateTokenCount, inferModelKind } from \"@warlock.js/ai\";\nimport type {\n GoogleEmbedderConfig,\n GoogleImageConfig,\n GoogleModelConfig,\n GoogleSDKConfig,\n} from \"./config.type\";\nimport { GoogleEmbedder } from \"./embedder\";\nimport { GeminiImageModel } from \"./gemini-image\";\nimport { GoogleImageModel } from \"./image\";\nimport { googlePricingFor } from \"./known-pricing\";\nimport { GoogleModel } from \"./model\";\nimport { wrapGoogleError } from \"./utils\";\n\n/**\n * Pick the transport for an image model id.\n *\n * `ai.models.generateImages` calls `{model}:predict`, and a `gemini-`\n * id sent there comes back `404 … is not supported for predict`\n * (observed verbatim from Google). `generateContent` is what the SDK\n * itself points `generateImages` users at — its deprecation notice\n * reads \"Please use the generateContent method with image models\n * instead\" — so the id has to choose the transport.\n *\n * Runs in this package establish where a `gemini-` id is ACCEPTED, not\n * what it returns: on this transport such an id got as far as a quota\n * error (HTTP 429) instead of the 404. That an image\n * comes back end-to-end once billing is enabled is reported by the\n * maintainer from a locally linked build, not measured here. Whether\n * these models report token usage is still unknown.\n *\n * This is ROUTING, not validation — no id is refused here. An id this\n * function does not recognize takes the `generateImages` route, the\n * only route that existed before Gemini image support landed, so every\n * id that reached Google before still reaches Google the same way and\n * still fails (or succeeds) at the provider.\n *\n * A leading `models/` resource prefix is tolerated, matching the id\n * shapes `inferVisionCapability` already accepts\n * (`models/gemini-1.5-flash-001`).\n */\nfunction usesGeminiImageTransport(name: string): boolean {\n return name\n .toLowerCase()\n .replace(/^models\\//, \"\")\n .startsWith(\"gemini-\");\n}\n\n/**\n * Google Gemini-backed implementation of `SDKAdapterContract`.\n *\n * **Role.** The package entry point for Gemini models via the\n * `@google/genai` SDK. A single `GoogleSDK` holds one live\n * `GoogleGenAI` client, shared by every `ModelContract` /\n * `EmbedderContract` it produces. Construct one SDK per\n * account/project and reuse it everywhere.\n *\n * **Responsibility.**\n * - Owns: a long-lived `GoogleGenAI` client (auth, Vertex vs Gemini\n * API) and its lifetime. Factory for `GoogleModel` /\n * `GoogleEmbedder` instances sharing that client.\n * - Does NOT own: anything per-call — those live in `GoogleModel` /\n * `GoogleEmbedder` and the agent runtime.\n *\n * Modeled as a class (see §4.2 of code-style.md — \"long-lived state\n * across many calls\"), fronted by FP usage like the other adapters.\n *\n * @example\n * const google = new GoogleSDK({ apiKey: process.env.GEMINI_API_KEY! });\n * const model = google.model({ name: \"gemini-2.5-flash\", temperature: 0.7 });\n * const embedder = google.embedder({ name: \"gemini-embedding-001\" });\n */\nexport class GoogleSDK implements SDKAdapterContract {\n private readonly ai: GoogleGenAI;\n private readonly provider: string;\n private readonly pricing?: Record<string, ModelPricing>;\n\n public constructor(config: GoogleSDKConfig) {\n const { provider, pricing, ...clientOptions } = config;\n\n this.ai = new GoogleGenAI(clientOptions);\n this.provider = provider ?? \"google\";\n this.pricing = pricing;\n }\n\n /**\n * Build a `GoogleModel` bound to this SDK's client. Each call\n * returns a fresh instance; all instances share the underlying\n * `GoogleGenAI` client. The SDK's `provider` label is forwarded.\n *\n * Pricing resolution: per-model `config.pricing` wins; otherwise the\n * SDK-level registry entry keyed by `config.name`; otherwise the\n * built-in list price (`GOOGLE_MODEL_PRICING`); otherwise `undefined`\n * (no cost computed).\n */\n public model(config: GoogleModelConfig): ModelContract {\n const resolvedPricing =\n config.pricing ?? this.pricing?.[config.name] ?? googlePricingFor(config.name);\n const resolvedConfig: GoogleModelConfig =\n resolvedPricing === config.pricing ? config : { ...config, pricing: resolvedPricing };\n\n return new GoogleModel(this.ai, resolvedConfig, this.provider);\n }\n\n /**\n * Rough token-count estimate. Uses the character-heuristic\n * (`approximateTokenCount`) from the core package — Gemini's\n * `countTokens` is a network round-trip; `count()` is intentionally\n * offline. Good for budgeting/quota guards, not billing.\n */\n public async count(text: string, _model?: string): Promise<number> {\n return approximateTokenCount(text);\n }\n\n /**\n * Build a `GoogleEmbedder` bound to this SDK's client.\n *\n * @example\n * const embedder = google.embedder({ name: \"gemini-embedding-001\" });\n * const { vector } = await embedder.embed(\"Hello world\");\n */\n public embedder(config: GoogleEmbedderConfig): EmbedderContract {\n return new GoogleEmbedder(this.ai, config, this.provider);\n }\n\n /**\n * Build an image model bound to this SDK's client for use with\n * `ai.image({ model, prompt })`. `config.name` decides the transport\n * (see {@link usesGeminiImageTransport}) — a `gemini-` id gets the\n * `generateContent` implementation, everything else the Imagen\n * `generateImages` one. No id is rejected locally either way, so an\n * unsupported model fails at Google, not here.\n *\n * The two differ in what usage they can report, which is what the\n * caller must price for: the Imagen path always returns a zero token\n * `Usage` (Imagen reports none — price with `{ perImage }`), while the\n * Gemini path passes through whatever `usageMetadata` Google attaches\n * (price with `{ input, output }` when tokens come back).\n *\n * Pricing resolution mirrors `model()`: per-model `config.pricing`\n * wins, otherwise the SDK-level registry entry keyed by `config.name`,\n * otherwise `undefined`.\n *\n * @example\n * const imagen = google.image({ name: \"imagen-4.0-generate-001\" });\n * const gemini = google.image({ name: \"gemini-3.1-flash-lite-image\" });\n * const { data } = await ai.image({ model: gemini, prompt: \"a red bicycle\" });\n */\n public image(config: GoogleImageConfig): ImageModelContract {\n const resolvedPricing = config.pricing ?? this.pricing?.[config.name];\n const resolvedConfig: GoogleImageConfig =\n resolvedPricing === config.pricing ? config : { ...config, pricing: resolvedPricing };\n\n if (usesGeminiImageTransport(config.name)) {\n return new GeminiImageModel(this.ai, resolvedConfig, this.provider);\n }\n\n return new GoogleImageModel(this.ai, resolvedConfig, this.provider);\n }\n\n /**\n * List the models this key or project can use, live from the Gemini API\n * `models.list` endpoint, mapped to {@link ModelInfo}. Follows pagination to\n * the end. `kind` comes from the model's `supportedActions`:\n * `generateContent` is `\"chat\"` (unless the id is an image or audio\n * model), `embedContent` is `\"embedding\"`, `predict` / `predictLongRunning`\n * is `\"image\"`. Google reports no creation date, so `created` is left out.\n * Failures are thrown as the adapter's usual provider errors.\n *\n * @example\n * const models = await google.models();\n * const chat = models.filter((model) => model.kind === \"chat\");\n */\n public async models(options?: ListModelsOptions): Promise<ModelInfo[]> {\n const found = new Map<string, ModelInfo>();\n\n try {\n const pager = await this.ai.models.list({\n config: {\n pageSize: 1000,\n ...(options?.signal ? { abortSignal: options.signal } : {}),\n },\n });\n\n for await (const entry of pager) {\n const rawName = entry.name;\n\n if (typeof rawName !== \"string\" || rawName === \"\") {\n continue;\n }\n\n const id = rawName.replace(/^(?:publishers\\/[^/]+\\/)?models\\//u, \"\");\n const actions = entry.supportedActions ?? [];\n\n found.set(id, {\n id,\n label: entry.displayName || id,\n provider: this.provider,\n kind: kindOfActions(actions, id),\n ...(entry.inputTokenLimit ? { contextWindow: entry.inputTokenLimit } : {}),\n ...(entry.outputTokenLimit ? { maxOutputTokens: entry.outputTokenLimit } : {}),\n ...(entry.description ? { description: entry.description } : {}),\n });\n }\n } catch (thrown) {\n throw wrapGoogleError(thrown);\n }\n\n return [...found.values()];\n }\n}\n\n/** Map Gemini `supportedActions` to a {@link ModelKind}; fall back to the id when none are listed. */\nfunction kindOfActions(actions: string[], id: string): ModelKind {\n const inferred = inferModelKind(id);\n\n if (actions.includes(\"embedContent\") || actions.includes(\"batchEmbedContents\")) {\n return \"embedding\";\n }\n\n if (actions.includes(\"predict\") || actions.includes(\"predictLongRunning\")) {\n return inferred === \"audio\" ? \"audio\" : \"image\";\n }\n\n if (actions.includes(\"generateContent\")) {\n return inferred === \"embedding\" || inferred === \"moderation\" ? \"other\" : inferred;\n }\n\n return actions.length === 0 ? inferred : \"other\";\n}\n","import type { ModelLifecycle } from \"@warlock.js/ai\";\n\n/**\n * Where Google's deprecations page puts the Gemini text models the\n * adapter knows, as of {@link GOOGLE_MODEL_LIFECYCLE_AS_OF}. A `retiring`\n * model is served until `retiresOn`; Google reroutes `gemini-2.5-pro` and\n * `gemini-2.5-flash` requests to the replacement on that date. `retired`\n * ids were removed or are already rerouted.\n *\n * Source: https://ai.google.dev/gemini-api/docs/deprecations (earliest\n * possible shutdown dates, as Google words them). `gemini-2.5-flash-lite`\n * is left out: the pricing page calls it deprecated while the\n * deprecations page lists no date.\n */\nexport const GOOGLE_MODEL_LIFECYCLE: Readonly<Record<string, ModelLifecycle>> = {\n \"gemini-4-argon\": { status: \"current\" },\n \"gemini-3.8-flash\": { status: \"current\" },\n \"gemini-3.6-flash\": { status: \"current\" },\n \"gemini-3.5-flash-lite\": { status: \"current\" },\n \"gemini-3.1-pro-preview\": { status: \"current\" },\n \"gemini-3-flash-preview\": { status: \"current\", replacement: \"gemini-3.6-flash\" },\n \"gemini-3.1-flash-lite\": {\n status: \"retiring\",\n retiresOn: \"2027-05-07\",\n replacement: \"gemini-3.5-flash-lite\",\n },\n \"gemini-2.5-pro\": {\n status: \"retiring\",\n retiresOn: \"2026-12-01\",\n replacement: \"gemini-3.8-flash\",\n },\n \"gemini-2.5-flash\": {\n status: \"retiring\",\n retiresOn: \"2026-12-01\",\n replacement: \"gemini-3.5-flash-lite\",\n },\n \"gemini-3.7-flash\": { status: \"retired\", replacement: \"gemini-3.8-flash\" },\n \"gemini-3.5-flash\": { status: \"retired\", replacement: \"gemini-3.6-flash\" },\n \"gemini-3-pro-preview\": {\n status: \"retired\",\n retiresOn: \"2026-03-09\",\n replacement: \"gemini-3.1-pro-preview\",\n },\n \"gemini-2.0-flash\": {\n status: \"retired\",\n retiresOn: \"2026-06-01\",\n replacement: \"gemini-3.6-flash\",\n },\n \"gemini-2.0-flash-lite\": {\n status: \"retired\",\n retiresOn: \"2026-06-01\",\n replacement: \"gemini-3.1-flash-lite\",\n },\n};\n\n/** The date {@link GOOGLE_MODEL_LIFECYCLE} was last checked against Google's deprecations page. */\nexport const GOOGLE_MODEL_LIFECYCLE_AS_OF = \"2026-11-06\";\n\n/**\n * Look up the lifecycle of a Gemini model id (exact match; a leading\n * `models/` is accepted), or `undefined` when the table does not cover it.\n *\n * @example\n * googleLifecycleFor(\"gemini-2.5-pro\"); // { status: \"retiring\", retiresOn: \"2026-12-01\", replacement: \"gemini-3.8-flash\" }\n */\nexport function googleLifecycleFor(modelName: string): ModelLifecycle | undefined {\n const id = modelName.replace(/^models\\//, \"\");\n\n return Object.hasOwn(GOOGLE_MODEL_LIFECYCLE, id) ? GOOGLE_MODEL_LIFECYCLE[id] : undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,SAAgB,iBAAiB,OAAc,KAAgC;CAC7E,MAAM,qBAAqB;CAC3B,MAAM,QAAQ,IAAI,oBAAoB,MAAM;CAC5C,MAAM,SAAS,IAAI,wBAAwB,MAAM;CACjD,MAAM,QAAQ,IAAI,mBAAmB,MAAM,QAAQ,MAAM;CAEzD,MAAM,SAAS,IAAI;CAEnB,IAAI,UAAU,SAAS,GACrB,MAAM,eAAe;CAGvB,MAAM,YAAY,IAAI;CAEtB,IAAI,aAAa,YAAY,GAC3B,MAAM,kBAAkB;AAE5B;;;;ACzCA,MAAM,kBAAgD;CACpD,MAAM;CACN,YAAY;AACd;;;;;;;;;;;;;;;;;;;;;;;AAwBA,SAAgB,gBAAgB,KAA8C;CAC5E,OAAO,gBAAgB,OAAO,OAAO;AACvC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACiBA,SAAgB,iBAAiB,UAAqC;CACpE,MAAM,cAAwB,CAAC;CAC/B,MAAM,WAAsB,CAAC;CAE7B,KAAK,MAAM,WAAW,UAAU;EA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AW,IAAI,WAAW;AAC3C;;;;;;;;;;;;;;;;AClOA,MAAa,yBAAmE;CAC9E,kBAAkB,EAAE,QAAQ,UAAU;CACtC,oBAAoB,EAAE,QAAQ,UAAU;CACxC,oBAAoB,EAAE,QAAQ,UAAU;CACxC,yBAAyB,EAAE,QAAQ,UAAU;CAC7C,0BAA0B,EAAE,QAAQ,UAAU;CAC9C,0BAA0B;EAAE,QAAQ;EAAW,aAAa;CAAmB;CAC/E,yBAAyB;EACvB,QAAQ;EACR,WAAW;EACX,aAAa;CACf;CACA,kBAAkB;EAChB,QAAQ;EACR,WAAW;EACX,aAAa;CACf;CACA,oBAAoB;EAClB,QAAQ;EACR,WAAW;EACX,aAAa;CACf;CACA,oBAAoB;EAAE,QAAQ;EAAW,aAAa;CAAmB;CACzE,oBAAoB;EAAE,QAAQ;EAAW,aAAa;CAAmB;CACzE,wBAAwB;EACtB,QAAQ;EACR,WAAW;EACX,aAAa;CACf;CACA,oBAAoB;EAClB,QAAQ;EACR,WAAW;EACX,aAAa;CACf;CACA,yBAAyB;EACvB,QAAQ;EACR,WAAW;EACX,aAAa;CACf;AACF;;AAGA,MAAa,+BAA+B;;;;;;;;AAS5C,SAAgB,mBAAmB,WAA+C;CAChF,MAAM,KAAK,UAAU,QAAQ,aAAa,EAAE;CAE5C,OAAO,OAAO,OAAO,wBAAwB,EAAE,IAAI,uBAAuB,MAAM;AAClF"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["toolResultText","safeJsonParse","InvalidRequestError","extractJsonSchema","AIError","ProviderAbortError","ProviderTimeoutError","ProviderAuthError","QuotaExceededError","ProviderRateLimitError","ContextLengthExceededError","InvalidRequestError","ProviderOverloadedError","ProviderError","ApiError","LOG_MODULE","log","LOG_MODULE","log","ContentFilterError","ProviderError","LOG_MODULE","log","ContentFilterError","ProviderError","InvalidRequestError","InvalidRequestError","log","InvalidRequestError","resolveStreamChunks","selectProviderTools","GoogleGenAI","approximateTokenCount","inferModelKind"],"sources":["../../../../../../../packages/ai-google/src/utils/apply-google-usage.ts","../../../../../../../packages/ai-google/src/utils/map-finish-reason.ts","../../../../../../../packages/ai-google/src/utils/to-google-contents.ts","../../../../../../../packages/ai-google/src/utils/to-google-tools.ts","../../../../../../../packages/ai-google/src/utils/wrap-google-error.ts","../../../../../../../packages/ai-google/src/embedder.ts","../../../../../../../packages/ai-google/src/gemini-image.ts","../../../../../../../packages/ai-google/src/image.ts","../../../../../../../packages/ai-google/src/known-pricing.ts","../../../../../../../packages/ai-google/src/search-budget.ts","../../../../../../../packages/ai-google/src/utils/grounding.ts","../../../../../../../packages/ai-google/src/provider-options.ts","../../../../../../../packages/ai-google/src/known-context-windows.ts","../../../../../../../packages/ai-google/src/known-vision-models.ts","../../../../../../../packages/ai-google/src/model.ts","../../../../../../../packages/ai-google/src/sdk.ts","../../../../../../../packages/ai-google/src/known-lifecycle.ts"],"sourcesContent":["import type { Usage } from \"@warlock.js/ai\";\nimport type { GenerateContentResponse } from \"@google/genai\";\n\n/** Gemini's per-response token accounting block, as the SDK types it. */\nexport type GoogleUsageMetadata = NonNullable<GenerateContentResponse[\"usageMetadata\"]>;\n\n/**\n * Fold a Gemini `usageMetadata` block into a running neutral `Usage`\n * accumulator. Shared by every `generateContent`-backed surface — the\n * chat model's `complete()`, its streaming loop (where the final chunk\n * carries cumulative totals), and the Gemini image model — so one\n * mapping decides what a Gemini token report means package-wide.\n *\n * Cache-read hits (`cachedContentTokenCount`, implicit or explicit\n * context caching) surface as `cachedTokens`; the thinking-phase tokens\n * of a reasoning model (`thoughtsTokenCount`) surface as\n * `reasoningTokens`. Both are emitted only when reported `> 0` so an\n * absent channel leaves the field undefined rather than a false zero.\n *\n * `total` falls back to `input + output` when Google omits\n * `totalTokenCount`.\n *\n * `promptTokenCount` \"includes the number of tokens in the cached\n * content\" (https://ai.google.dev/api/generate-content#UsageMetadata), so\n * the usage is marked `inputIncludesCache: true`.\n */\nexport function applyGoogleUsage(usage: Usage, raw: GoogleUsageMetadata): void {\n usage.inputIncludesCache = true;\n usage.input = raw.promptTokenCount ?? usage.input;\n usage.output = raw.candidatesTokenCount ?? usage.output;\n usage.total = raw.totalTokenCount ?? usage.input + usage.output;\n\n const cached = raw.cachedContentTokenCount;\n\n if (cached && cached > 0) {\n usage.cachedTokens = cached;\n }\n\n const reasoning = raw.thoughtsTokenCount;\n\n if (reasoning && reasoning > 0) {\n usage.reasoningTokens = reasoning;\n }\n}\n","import type { FinishReason } from \"@warlock.js/ai\";\n\nconst finishReasonMap: Record<string, FinishReason> = {\n STOP: \"stop\",\n MAX_TOKENS: \"length\",\n};\n\n/**\n * Map Gemini's `FinishReason` enum value to the normalized\n * `FinishReason` union.\n *\n * `STOP` is the natural terminal. `MAX_TOKENS` maps to `length`.\n * Everything else — `SAFETY`, `RECITATION`, `BLOCKLIST`,\n * `PROHIBITED_CONTENT`, `SPII`, `MALFORMED_FUNCTION_CALL`,\n * `UNEXPECTED_TOOL_CALL`, `LANGUAGE`, `OTHER`,\n * `FINISH_REASON_UNSPECIFIED`, `null`, or any future value — falls\n * through to `\"error\"`.\n *\n * Note: Gemini reports `STOP` even when the turn ended in a function\n * call (it has no `tool_use` reason). `GoogleModel` overrides the\n * mapped reason to `\"tool_calls\"` when the response carries function\n * calls — this map intentionally stays purely about the raw signal.\n *\n * @example\n * mapFinishReason(\"STOP\"); // \"stop\"\n * mapFinishReason(\"MAX_TOKENS\"); // \"length\"\n * mapFinishReason(\"SAFETY\"); // \"error\"\n * mapFinishReason(undefined); // \"error\"\n */\nexport function mapFinishReason(raw: string | null | undefined): FinishReason {\n return finishReasonMap[raw ?? \"\"] ?? \"error\";\n}\n","import {\n InvalidRequestError,\n safeJsonParse,\n toolResultText,\n type ContentPart,\n type Message,\n} from \"@warlock.js/ai\";\nimport type { Content, Part } from \"@google/genai\";\n\n/**\n * Result of splitting a vendor-neutral `Message[]` for Gemini's\n * `generateContent`: the system prompt is hoisted to a separate\n * `systemInstruction` string (Gemini has no `\"system\"` role — content\n * roles must be `\"user\"` or `\"model\"`), and the remaining turns map to\n * `Content[]`.\n */\nexport type GoogleContents = {\n systemInstruction: string | undefined;\n contents: Content[];\n};\n\n/**\n * Convert vendor-neutral `Message[]` into Gemini's request shape.\n *\n * Gemini specifics this function absorbs:\n *\n * 1. **No `system` role.** System messages concatenate into the\n * separate `systemInstruction` config field.\n * 2. **Role names differ.** Neutral `assistant` → Gemini `\"model\"`;\n * `user` stays `\"user\"`.\n * 3. **Tool results are `user` turns.** A neutral `tool` message\n * becomes a `\"user\"` content with a single `functionResponse` part.\n * 4. **Tool calls are `functionCall` parts.** An assistant message\n * with `toolCalls` becomes a `\"model\"` content: an optional leading\n * `text` part followed by one `functionCall` part per call.\n * 5. **Adjacent same-role turns merge.** Consecutive contents with the\n * same role become one content with their parts in order, so the\n * results of parallel tool calls travel as one `user` turn (as the\n * `@google/genai` SDK's own automatic function calling sends them)\n * and a compaction summary next to a same-role turn does not break\n * the user/model alternation.\n *\n * @example\n * const { systemInstruction, contents } = toGoogleContents([\n * { role: \"system\", content: \"Be concise.\" },\n * { role: \"user\", content: \"Hi\" },\n * ]);\n */\nexport function toGoogleContents(messages: Message[]): GoogleContents {\n const systemParts: string[] = [];\n const contents: Content[] = [];\n\n for (const message of messages) {\n if (message.role === \"system\") {\n systemParts.push(stringifyContent(message.content));\n\n continue;\n }\n\n if (message.role === \"tool\") {\n contents.push({\n role: \"user\",\n parts: [\n {\n // Gemini matches a `functionResponse` to its `functionCall`\n // by `name` (the Developer API has no call ids). `name` is\n // the neutral `toolCallId`, which `GoogleModel` set to the\n // function name. The wire `id` is intentionally omitted —\n // an empty/synthetic id is rejected as an invalid argument.\n functionResponse: {\n name: message.toolCallId ?? \"\",\n response: toResponseObject(toolResultText(message.content)),\n },\n },\n ],\n });\n\n continue;\n }\n\n if (message.role === \"assistant\" && message.toolCalls && message.toolCalls.length > 0) {\n const parts: Part[] = [];\n const text = stringifyContent(message.content);\n\n if (text) {\n parts.push({ text });\n }\n\n for (const toolCall of message.toolCalls) {\n // Replay the opaque `thoughtSignature` Gemini attached to this\n // function call on the original turn. Thinking models reject\n // the follow-up request with a 400 if the signature is missing\n // from the echoed `functionCall` part. Captured by\n // `GoogleModel.partToToolCall` into `providerMetadata`.\n const thoughtSignature = toolCall.providerMetadata?.thoughtSignature;\n\n parts.push({\n ...(typeof thoughtSignature === \"string\" ? { thoughtSignature } : {}),\n // `id` omitted deliberately — Gemini Developer API function\n // calls have no ids; echoing an empty/synthetic one is\n // rejected as an invalid argument. Matched by `name`.\n functionCall: {\n name: toolCall.name,\n args: (toolCall.input ?? {}) as Record<string, unknown>,\n },\n });\n }\n\n contents.push({ role: \"model\", parts });\n\n continue;\n }\n\n if (message.role === \"user\" && Array.isArray(message.content)) {\n contents.push({ role: \"user\", parts: message.content.map(toGooglePart) });\n\n continue;\n }\n\n contents.push({\n role: message.role === \"assistant\" ? \"model\" : \"user\",\n parts: [{ text: stringifyContent(message.content) }],\n });\n }\n\n return {\n systemInstruction: systemParts.length > 0 ? systemParts.join(\"\\n\\n\") : undefined,\n contents: mergeAdjacentContents(contents),\n };\n}\n\n/**\n * Join consecutive contents that share a role into one, keeping part\n * order: parallel `functionResponse` parts stay in their `functionCall`\n * order, and each `functionCall` keeps its own `thoughtSignature`.\n * Returns new content objects; the input contents are not modified.\n *\n * Source for the single-turn shape: `@google/genai` 2.19.0\n * `Models.generateContent` (automatic function calling) sends every\n * function response of one model turn as a single `{ role: \"user\",\n * parts }` content, in call order.\n */\nfunction mergeAdjacentContents(contents: Content[]): Content[] {\n const merged: Content[] = [];\n\n for (const content of contents) {\n const previous = merged.at(-1);\n\n if (previous && previous.role === content.role) {\n merged[merged.length - 1] = {\n ...previous,\n parts: [...(previous.parts ?? []), ...(content.parts ?? [])],\n };\n\n continue;\n }\n\n merged.push(content);\n }\n\n return merged;\n}\n\n/**\n * Multipart content is only meaningful on user messages — for any\n * other role collapse a `ContentPart[]` to concatenated text. Plain\n * strings pass through unchanged.\n */\nfunction stringifyContent(content: string | ContentPart[]): string {\n if (typeof content === \"string\") {\n return content;\n }\n\n return content\n .filter((part): part is { type: \"text\"; text: string } => part.type === \"text\")\n .map((part) => part.text)\n .join(\"\");\n}\n\n/**\n * Gemini's `functionResponse.response` must be a JSON object. Tool\n * results arrive as a string (usually stringified JSON) — parse it\n * when it is a JSON object, otherwise wrap the raw string under a\n * `result` key so the model always receives a well-formed object.\n */\nfunction toResponseObject(raw: string): Record<string, unknown> {\n const parsed = safeJsonParse<unknown>(raw, undefined);\n\n if (parsed !== null && typeof parsed === \"object\" && !Array.isArray(parsed)) {\n return parsed as Record<string, unknown>;\n }\n\n return { result: raw };\n}\n\n/**\n * Map a resolved `ContentPart` to a Gemini `Part`. All binary\n * modalities — **image, PDF, and audio** — go to a single\n * `inlineData: { mimeType, data }` block; Gemini's multimodal input is\n * media-agnostic and keys off the IANA `mimeType` (`image/png`,\n * `application/pdf`, `audio/mpeg`, …), so one mapping covers every part\n * type the model's capabilities admit. PDF and audio reach this point\n * only when the model declares the matching capability (`google.model`\n * infers `pdf` / `audio` from the multimodal Gemini families); the\n * agent's modality gate throws upfront otherwise, so capability and\n * behavior stay in lockstep.\n *\n * Gemini's `generateContent` does not fetch arbitrary remote URLs (only\n * Files API / GCS URIs via `fileData`), so a neutral `{ url }` source\n * surfaces a typed `InvalidRequestError` upfront — for any modality —\n * rather than a downstream Gemini fault. The agent resolves attachments\n * before this point, so nothing is read or fetched here.\n */\nfunction toGooglePart(part: ContentPart): Part {\n if (part.type === \"text\") {\n return { text: part.text };\n }\n\n if (\"url\" in part.source) {\n throw new InvalidRequestError(\n `Gemini generateContent cannot fetch remote-URL ${part.type} media; supply base64 bytes instead.`,\n );\n }\n\n return {\n inlineData: { mimeType: part.source.mediaType, data: part.source.base64 },\n };\n}\n","import { extractJsonSchema, type ToolConfig } from \"@warlock.js/ai\";\nimport type { Tool } from \"@google/genai\";\n\n/**\n * Convert vendor-neutral `ToolConfig[]` into Gemini's `tools` array —\n * a single `Tool` carrying one `functionDeclarations` entry per tool.\n *\n * The input schema is forwarded via `parametersJsonSchema` (raw JSON\n * Schema, mutually exclusive with Gemini's typed `parameters`).\n * Non-object extractions degrade to a parameterless object so\n * registration never fails.\n *\n * Returns `undefined` when there are no tools so the caller can omit\n * `config.tools` entirely.\n *\n * @example\n * const tools = toGoogleTools([weatherTool]);\n * await ai.models.generateContent({ model, contents, config: { tools } });\n */\nexport function toGoogleTools(\n tools: ToolConfig<unknown, unknown>[] | undefined,\n): Tool[] | undefined {\n if (!tools || tools.length === 0) {\n return undefined;\n }\n\n return [\n {\n functionDeclarations: tools.map((tool) => ({\n name: tool.name,\n description: tool.description,\n parametersJsonSchema: toJsonSchema(tool.input),\n })),\n },\n ];\n}\n\n/**\n * Resolve a tool's input schema to a JSON-Schema object. Gemini wants\n * an object root for function parameters; anything else (or a failed\n * extraction) degrades to a parameterless object.\n */\nfunction toJsonSchema(input: ToolConfig<unknown, unknown>[\"input\"]): Record<string, unknown> {\n const schema = extractJsonSchema(input);\n\n if (schema && schema.type === \"object\") {\n return schema;\n }\n\n return { type: \"object\" };\n}\n","import {\n AIError,\n ContextLengthExceededError,\n InvalidRequestError,\n ProviderAbortError,\n ProviderAuthError,\n ProviderError,\n ProviderOverloadedError,\n ProviderRateLimitError,\n ProviderTimeoutError,\n QuotaExceededError,\n} from \"@warlock.js/ai\";\nimport { ApiError } from \"@google/genai\";\n\n/**\n * Raw-error fields the wrapper reads off a Gemini SDK error.\n * `@google/genai`'s `ApiError` exposes `status` (HTTP code) +\n * `message`; transport aborts surface as `AbortError` / `ETIMEDOUT`.\n * We duck-type so proxied / re-thrown errors still classify.\n */\ntype GoogleErrorShape = {\n status?: number;\n message?: string;\n name?: string;\n code?: string;\n};\n\n/**\n * Wrap any thrown value caught inside the Gemini adapter into the\n * appropriate `@warlock.js/ai` `AIError` subclass.\n *\n * **Dispatch strategy.** Gemini has no machine error `code`; the\n * signals are the HTTP `status` and the canonical status phrase Google\n * embeds in `message` (`PERMISSION_DENIED`, `RESOURCE_EXHAUSTED`,\n * `INVALID_ARGUMENT`, …). Dispatch keys on `status`, using the message\n * phrase as the tie-breaker for the two 400 sub-cases\n * (context-length vs generic) and for status-less auth/quota errors.\n *\n * `AIError` instances pass through unchanged so `catch/throw wrap(e)`\n * pipelines never double-wrap.\n *\n * @example\n * try {\n * return await this.ai.models.generateContent(...);\n * } catch (thrown) {\n * throw wrapGoogleError(thrown);\n * }\n */\nexport function wrapGoogleError(thrown: unknown, options: { signal?: AbortSignal } = {}): AIError {\n if (thrown instanceof AIError) {\n return thrown;\n }\n\n const shape = toShape(thrown);\n const context = buildContext(shape);\n const message = shape.message ?? (thrown instanceof Error ? thrown.message : String(thrown));\n // \"If your request fails with a 400 or 500 error, you won't be charged\n // for the tokens used.\" — https://ai.google.dev/gemini-api/docs/billing\n // (fetched 2026-10-17). Every other failure keeps the default.\n const billed = shape.status === 400 || shape.status === 500 ? false : undefined;\n\n // The SDK aborts the request the same way for the caller's signal and for\n // its own `httpOptions.timeout`, so only an aborted caller signal makes an\n // `AbortError` a cancellation; any other one is the SDK's timeout.\n if (shape.name === \"AbortError\" && options.signal?.aborted === true) {\n return new ProviderAbortError(message, { cause: thrown, context, billed });\n }\n\n if (isTimeout(shape)) {\n return new ProviderTimeoutError(message, { cause: thrown, context, billed });\n }\n\n if (\n shape.status === 401 ||\n shape.status === 403 ||\n /permission_denied|api key not valid|unauthenticated/i.test(message)\n ) {\n return new ProviderAuthError(message, { cause: thrown, context, billed });\n }\n\n // Spent quota or billing needs a human (top up, raise the cap): a retry\n // cannot fix it. A per-minute / per-day rate limit stays a rate limit.\n if (isSpentQuota(message)) {\n return new QuotaExceededError(message, { cause: thrown, context, billed });\n }\n\n if (shape.status === 429 || /resource_exhausted|quota/i.test(message)) {\n return new ProviderRateLimitError(message, { cause: thrown, context, billed });\n }\n\n if (shape.status === 400) {\n if (/token count|context length|exceeds the maximum|input is too long/i.test(message)) {\n return new ContextLengthExceededError(message, { cause: thrown, context, billed });\n }\n\n return new InvalidRequestError(message, { cause: thrown, context, billed });\n }\n\n if (shape.status === 404 || isClientStatus(shape.status)) {\n return new InvalidRequestError(message, { cause: thrown, context, billed });\n }\n\n // 503 UNAVAILABLE — \"temporarily overloaded or down\"\n // (https://ai.google.dev/gemini-api/docs/api-errors).\n if (shape.status === 503 || /\\bUNAVAILABLE\\b/.test(message) || /overloaded/i.test(message)) {\n return new ProviderOverloadedError(message, { cause: thrown, context, billed });\n }\n\n return new ProviderError(message, { cause: thrown, context, billed });\n}\n\n/**\n * Whether the message says the project's billing or free-tier allowance is\n * used up, as opposed to a per-minute / per-day request rate limit. Google\n * words rate limits as \"quota exceeded for metric ... per minute\" and spent\n * billing as \"exceeded your current quota, please check your plan and\n * billing details\" or \"billing ... not enabled\".\n */\nfunction isSpentQuota(message: string): boolean {\n if (/per[ -]?(minute|second|day)|requests? per|retry in|retryDelay/i.test(message)) {\n return false;\n }\n\n return /check your plan and billing|billing (account|details|is not|not )|prepayment credits? (are|is)|exceeded your current quota|free tier.*(exhausted|exceeded)/i.test(\n message,\n );\n}\n\n/**\n * Read the raw error shape. The Gemini SDK's `ApiError` carries a\n * numeric `status`; flattened/proxied errors may carry it (or `code`)\n * loosely.\n */\nfunction toShape(thrown: unknown): GoogleErrorShape {\n if (thrown instanceof ApiError) {\n return { status: thrown.status, message: thrown.message, name: thrown.name };\n }\n\n if (typeof thrown === \"object\" && thrown !== null) {\n const raw = thrown as Record<string, unknown>;\n\n return {\n status: typeof raw.status === \"number\" ? raw.status : undefined,\n message: typeof raw.message === \"string\" ? raw.message : undefined,\n name: typeof raw.name === \"string\" ? raw.name : undefined,\n code: typeof raw.code === \"string\" ? raw.code : undefined,\n };\n }\n\n return {};\n}\n\n/**\n * Decide whether the error is a timeout. Gemini maps gateway timeouts\n * to HTTP 504 (`DEADLINE_EXCEEDED`); transport aborts surface as\n * `AbortError` / `ETIMEDOUT` / `ECONNABORTED`.\n */\nfunction isTimeout(shape: GoogleErrorShape): boolean {\n // An abort the caller did not ask for: the SDK's `httpOptions.timeout`.\n if (shape.name === \"AbortError\" || shape.name === \"TimeoutError\") {\n return true;\n }\n\n if (shape.status === 504) {\n return true;\n }\n\n if (/deadline_exceeded/i.test(shape.message ?? \"\")) {\n return true;\n }\n\n return shape.code === \"ETIMEDOUT\" || shape.code === \"ECONNABORTED\";\n}\n\n/** True for HTTP 4xx — a client-side request problem, not a server fault. */\nfunction isClientStatus(status: number | undefined): boolean {\n return typeof status === \"number\" && status >= 400 && status < 500;\n}\n\n/** Attach the diagnostic fields to `error.context`. */\nfunction buildContext(shape: GoogleErrorShape): Record<string, unknown> {\n const context: Record<string, unknown> = {};\n\n if (shape.status !== undefined) {\n context.status = shape.status;\n }\n\n if (shape.name) {\n context.code = shape.name;\n }\n\n return context;\n}\n","import {\n type EmbeddingBatchResult,\n type EmbeddingResult,\n type EmbeddingUsage,\n type EmbedderContract,\n} from \"@warlock.js/ai\";\nimport { log, type Logger } from \"@warlock.js/logger\";\nimport type { EmbedContentResponse, GoogleGenAI } from \"@google/genai\";\nimport type { GoogleEmbedderConfig } from \"./config.type\";\nimport { wrapGoogleError } from \"./utils\";\n\nconst LOG_MODULE = \"ai.google\";\n\n/**\n * Token usage is not returned by Gemini's `embedContent`, so every\n * embedding result reports a zeroed `EmbeddingUsage` (honest absence,\n * not a fabricated estimate).\n */\nconst NO_USAGE: EmbeddingUsage = { promptTokens: 0, totalTokens: 0 };\n\n/**\n * Google Gemini-backed implementation of `EmbedderContract`\n * (`gemini-embedding-001`, `text-embedding-004`, …) via\n * `models.embedContent`.\n *\n * **Role.** Converts text into floating-point vectors. Standalone\n * primitive — unrelated to generateContent / tools / the agent loop.\n *\n * **Batch is native.** Gemini's `embedContent` accepts an array of\n * inputs and returns embeddings in the same order, so `embedMany` is\n * a single request (unlike the Bedrock/Titan adapter, which has to\n * loop).\n *\n * **No usage.** Gemini's embed endpoint returns no token counts;\n * `usage` is always `{ promptTokens: 0, totalTokens: 0 }`.\n *\n * **Dimensions.** When no `dimensions` override is given,\n * `this.dimensions` starts at `0` and is populated from the first\n * response's vector length, then cached. Passing `dimensions`\n * forwards Gemini's `outputDimensionality` truncation hint and sets\n * the initial value immediately.\n *\n * @example\n * const embedder = new GoogleEmbedder(ai, { name: \"gemini-embedding-001\" });\n * const { vector } = await embedder.embed(\"Hello world\");\n * const { vectors } = await embedder.embedMany([\"doc 1\", \"doc 2\"]);\n */\nexport class GoogleEmbedder implements EmbedderContract {\n public readonly name: string;\n public readonly provider: string;\n public dimensions: number;\n\n private readonly ai: GoogleGenAI;\n private readonly configuredDimensions: number | undefined;\n private readonly logger: Logger = log;\n\n public constructor(ai: GoogleGenAI, config: GoogleEmbedderConfig, provider: string = \"google\") {\n this.ai = ai;\n this.name = config.name;\n this.provider = provider;\n this.configuredDimensions = config.dimensions;\n this.dimensions = config.dimensions ?? 0;\n }\n\n public async embed(input: string): Promise<EmbeddingResult> {\n const vectors = await this.request([input]);\n\n return { vector: vectors[0], dimensions: this.dimensions, usage: NO_USAGE };\n }\n\n public async embedMany(inputs: string[]): Promise<EmbeddingBatchResult> {\n const vectors = await this.request(inputs);\n\n return { vectors, dimensions: this.dimensions, usage: NO_USAGE };\n }\n\n /**\n * Shared transport: one `embedContent` call for the whole batch,\n * wrap provider errors, cache `dimensions` from the first vector,\n * and return the raw vectors in input order.\n */\n private async request(inputs: string[]): Promise<number[][]> {\n this.logger.debug(LOG_MODULE, \"embedder.request\", \"embedContent\", {\n model: this.name,\n count: inputs.length,\n });\n\n let response: EmbedContentResponse;\n\n try {\n response = await this.ai.models.embedContent({\n model: this.name,\n contents: inputs,\n ...(this.configuredDimensions !== undefined\n ? { config: { outputDimensionality: this.configuredDimensions } }\n : {}),\n });\n } catch (thrown) {\n const wrapped = wrapGoogleError(thrown);\n\n this.logger.error(LOG_MODULE, \"embedder.error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n throw wrapped;\n }\n\n const vectors = (response.embeddings ?? []).map((embedding) => embedding.values ?? []);\n\n if (this.dimensions === 0 && vectors[0]) {\n this.dimensions = vectors[0].length;\n }\n\n this.logger.debug(LOG_MODULE, \"embedder.response\", \"embedContent returned\", {\n count: vectors.length,\n dimensions: this.dimensions,\n });\n\n return vectors;\n }\n}\n","import {\n ContentFilterError,\n ProviderError,\n type AIError,\n type GeneratedImage,\n type ImageGenerationOptions,\n type ImageGenerationResponse,\n type ImageModelContract,\n type ImageModelPricing,\n type Usage,\n} from \"@warlock.js/ai\";\nimport { log, type Logger } from \"@warlock.js/logger\";\nimport type {\n GenerateContentConfig,\n GenerateContentResponse,\n GoogleGenAI,\n ImageConfig,\n Part,\n} from \"@google/genai\";\nimport type { GoogleImageConfig } from \"./config.type\";\nimport { applyGoogleUsage, wrapGoogleError } from \"./utils\";\n\nconst LOG_MODULE = \"ai.google\";\n\n/**\n * Response modalities requested when the caller names none.\n *\n * `IMAGE` is the modality this adapter extracts; `TEXT` rides along so\n * a model that narrates what it drew is not answering outside the set\n * it was granted (the narration is then dropped — only inline image\n * parts become `GeneratedImage`s).\n *\n * *Unverified:* which pairing any individual Gemini image model\n * requires is not established here — no spec or run in this package\n * touches the live API. `options.responseModalities` replaces this list\n * verbatim for a model that wants something else.\n */\nconst DEFAULT_RESPONSE_MODALITIES = [\"TEXT\", \"IMAGE\"];\n\n/**\n * Gemini `finishReason` values that mean generation was stopped by a\n * safety / policy rule rather than by the model simply not drawing.\n * Taken from the `FinishReason` enum in `@google/genai`'s own type\n * declarations, whose doc comments describe each of these as content\n * or image generation being \"stopped\" for safety, prohibited content,\n * recitation, blocklist, or SPII.\n */\nconst FILTERED_FINISH_REASONS = new Set([\n \"SAFETY\",\n \"IMAGE_SAFETY\",\n \"PROHIBITED_CONTENT\",\n \"IMAGE_PROHIBITED_CONTENT\",\n \"RECITATION\",\n \"IMAGE_RECITATION\",\n \"BLOCKLIST\",\n \"SPII\",\n]);\n\n/** How much of a text-only answer to quote back inside the error message. */\nconst TEXT_EXCERPT_LIMIT = 200;\n\n/**\n * Gemini-native implementation of `ImageModelContract`, via\n * `ai.models.generateContent` with `config.responseModalities`\n * including `\"IMAGE\"`.\n *\n * **Why a second image adapter.** `GoogleImageModel` calls\n * `ai.models.generateImages`, which the `@google/genai` bundle routes\n * to `{model}:predict` (`generateImages` → `generateImagesInternal` →\n * `formatMap('{model}:predict', …)`). A Gemini image model is not\n * served there: asking for one returns Google's\n * `404 … is not found for API version v1beta, or is not supported for\n * predict`. `generateContent` is the SDK's own named replacement — its\n * runtime deprecation notice for `generateImages` reads \"Please use the\n * generateContent method with image models instead\" — so that is the\n * transport this class speaks, hence a separate class rather than a\n * branch inside `image.ts`.\n *\n * **Same envelope.** Inline image parts are mapped to the identical\n * `GeneratedImage[]` shape `GoogleImageModel` produces, so `ai.image()`\n * callers see no difference between the two paths.\n *\n * **Token usage is passed through, not zeroed.** The Imagen path\n * returns a hard `{ 0, 0, 0 }` because Imagen reports no tokens at all;\n * here, whatever `usageMetadata` Google attaches is mapped by the same\n * {@link applyGoogleUsage} the chat model uses, and only an absent\n * block collapses to zeros. Price accordingly.\n *\n * **No model-id validation.** `config.name` is forwarded to\n * `generateContent` exactly as given; nothing here inspects it. An id\n * Google does not serve fails at Google, wrapped into the typed\n * `AIError` hierarchy — never with a local throw.\n *\n * **Evidence, in two tiers.** No spec in this package calls Google.\n * *Measured here:* a `gemini-*` image id, which 404s on the `predict`\n * transport, reached the model on this one and came back with a quota\n * error (HTTP 429) — the endpoint accepts the id. *Reported by the\n * maintainer:* once billing was enabled on the project, an image came\n * back end-to-end from an application running a locally linked build.\n * *Still unestablished:* whether these models report token usage — no\n * `usageMetadata` from a successful image call has been observed, so\n * the pass-through above is untested against a real response.\n *\n * @example\n * const model = new GeminiImageModel(ai, { name: \"gemini-3.1-flash-lite-image\" });\n * const { images, usage } = await model.generate(\"a red bicycle on a white background\");\n */\nexport class GeminiImageModel implements ImageModelContract {\n public readonly name: string;\n public readonly provider: string;\n public readonly pricing?: ImageModelPricing;\n\n private readonly ai: GoogleGenAI;\n private readonly logger: Logger = log;\n\n public constructor(ai: GoogleGenAI, config: GoogleImageConfig, provider: string = \"google\") {\n this.ai = ai;\n this.name = config.name;\n this.provider = provider;\n this.pricing = config.pricing;\n }\n\n public async generate(\n prompt: string,\n options?: ImageGenerationOptions,\n ): Promise<ImageGenerationResponse> {\n const config = this.buildConfig(options);\n\n this.logger.debug(LOG_MODULE, \"image.request\", \"models.generateContent\", {\n model: this.name,\n responseModalities: config.responseModalities,\n });\n\n let response: GenerateContentResponse;\n\n try {\n // `contents` takes a bare string: the SDK's own `generateContent`\n // example passes one (`contents: 'Why is the sky blue?'`).\n response = await this.ai.models.generateContent({\n model: this.name,\n contents: prompt,\n config,\n });\n } catch (thrown) {\n const wrapped = wrapGoogleError(thrown);\n\n this.logger.error(LOG_MODULE, \"image.error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n throw wrapped;\n }\n\n const parts = collectParts(response);\n const images = toGeneratedImages(parts);\n\n if (images.length === 0) {\n throw this.noImageError(response, parts);\n }\n\n const usage: Usage = { input: 0, output: 0, total: 0 };\n\n if (response.usageMetadata) {\n applyGoogleUsage(usage, response.usageMetadata);\n }\n\n this.logger.debug(LOG_MODULE, \"image.response\", \"models.generateContent succeeded\", {\n images: images.length,\n usage,\n });\n\n return { images, usage };\n }\n\n /**\n * Assemble the `GenerateContentConfig` for an image turn: the\n * requested modalities, the image-specific knobs Gemini exposes under\n * `imageConfig`, and the cancellation handle.\n *\n * Three neutral options are deliberately NOT forwarded, because\n * `GenerateContentConfig` / `ImageConfig` in `@google/genai` expose\n * no equivalent for them on this path: `count` (no per-request image\n * count — every inline image part the model does return is mapped),\n * `negativePrompt` (an Imagen-only field), and `format`\n * (`ImageConfig.outputMimeType` is documented \"not supported in\n * Gemini API\"). Fold those intentions into the prompt instead.\n */\n private buildConfig(options: ImageGenerationOptions | undefined): GenerateContentConfig {\n const imageConfig: ImageConfig = {};\n\n if (options?.aspectRatio !== undefined) {\n imageConfig.aspectRatio = options.aspectRatio;\n }\n\n // Provider passthroughs off the neutral options' index signature —\n // forwarded verbatim, never re-spelled, so the value the caller\n // wrote is the value Google rules on. `ImageConfig` documents\n // `imageSize` as `1K`/`2K`/`4K` and `personGeneration` as\n // `ALLOW_ALL`/`ALLOW_ADULT`/`ALLOW_NONE`.\n if (typeof options?.imageSize === \"string\") {\n imageConfig.imageSize = options.imageSize;\n }\n\n if (typeof options?.personGeneration === \"string\") {\n imageConfig.personGeneration = options.personGeneration;\n }\n\n const requested = options?.responseModalities;\n const responseModalities = Array.isArray(requested)\n ? (requested as string[])\n : DEFAULT_RESPONSE_MODALITIES;\n\n return {\n responseModalities,\n ...(Object.keys(imageConfig).length > 0 ? { imageConfig } : {}),\n ...(options?.signal ? { abortSignal: options.signal } : {}),\n };\n }\n\n /**\n * Build the typed error for a response that carried no inline image\n * part. Never a silent empty success: the caller asked for an image\n * and got something else, so the error names what actually came back.\n *\n * - A blocked prompt (`promptFeedback.blockReason`) or a\n * safety/policy `finishReason` → `ContentFilterError` carrying the\n * reason, matching how the Imagen path reports `raiFilteredReason`.\n * - A text-only answer → `ProviderError` quoting the text, so the\n * log says what the model replied instead of guessing.\n * - Anything else → `ProviderError` naming the finish reason and how\n * many parts arrived.\n */\n private noImageError(response: GenerateContentResponse, parts: Part[]): AIError {\n const blockReason = response.promptFeedback?.blockReason;\n\n if (blockReason) {\n return new ContentFilterError(`Gemini blocked the prompt for ${this.name}: ${blockReason}`, {\n reason: blockReason,\n });\n }\n\n const finishReason = response.candidates?.[0]?.finishReason;\n\n if (finishReason && FILTERED_FINISH_REASONS.has(finishReason)) {\n return new ContentFilterError(`Gemini filtered the image for ${this.name}: ${finishReason}`, {\n reason: finishReason,\n });\n }\n\n const text = collectText(parts);\n\n if (text) {\n return new ProviderError(\n `Gemini returned no image for ${this.name} — the response was text only: \"${excerpt(text)}\"`,\n { context: { model: this.name, ...(finishReason ? { finishReason } : {}) } },\n );\n }\n\n return new ProviderError(\n `Gemini returned no image part for ${this.name} (parts: ${parts.length}${\n finishReason ? `, finishReason: ${finishReason}` : \"\"\n }).`,\n { context: { model: this.name, parts: parts.length } },\n );\n }\n}\n\n/**\n * Flatten every candidate's content parts into one list. Read off\n * `candidates[].content.parts` rather than the response's convenience\n * getters: `response.text` covers only the first candidate's text and\n * there is no getter for inline image data at all.\n */\nfunction collectParts(response: GenerateContentResponse): Part[] {\n const parts: Part[] = [];\n\n for (const candidate of response.candidates ?? []) {\n parts.push(...(candidate.content?.parts ?? []));\n }\n\n return parts;\n}\n\n/**\n * Map the inline image parts to the neutral `GeneratedImage[]` — the\n * same `{ type: \"base64\", base64, mediaType }` shape the Imagen path\n * emits, including its `image/png` fallback for a part that arrives\n * without a declared mime type.\n */\nfunction toGeneratedImages(parts: Part[]): GeneratedImage[] {\n const images: GeneratedImage[] = [];\n\n for (const part of parts) {\n const data = part.inlineData?.data;\n\n if (!data) {\n continue;\n }\n\n images.push({\n type: \"base64\",\n base64: data,\n mediaType: part.inlineData?.mimeType ?? \"image/png\",\n });\n }\n\n return images;\n}\n\n/** Join the text parts of a response — what the model said instead of drawing. */\nfunction collectText(parts: Part[]): string {\n return parts\n .map((part) => part.text)\n .filter((text): text is string => typeof text === \"string\" && text.length > 0)\n .join(\" \")\n .trim();\n}\n\n/** Trim a quoted model answer so an error message stays readable. */\nfunction excerpt(text: string): string {\n return text.length > TEXT_EXCERPT_LIMIT ? `${text.slice(0, TEXT_EXCERPT_LIMIT)}…` : text;\n}\n","import {\n ContentFilterError,\n ProviderError,\n type GeneratedImage,\n type ImageGenerationOptions,\n type ImageGenerationResponse,\n type ImageModelContract,\n type ImageModelPricing,\n} from \"@warlock.js/ai\";\nimport { log, type Logger } from \"@warlock.js/logger\";\nimport type { GenerateImagesConfig, GoogleGenAI } from \"@google/genai\";\nimport type { GoogleImageConfig } from \"./config.type\";\nimport { wrapGoogleError } from \"./utils\";\n\nconst LOG_MODULE = \"ai.google\";\n\n/** Map a neutral output container hint to an IANA media type. */\nfunction mediaTypeFor(format: string | undefined): string | undefined {\n switch (format) {\n case \"png\":\n return \"image/png\";\n case \"jpeg\":\n case \"jpg\":\n return \"image/jpeg\";\n case \"webp\":\n return \"image/webp\";\n default:\n return undefined;\n }\n}\n\n/**\n * Google Imagen-backed implementation of `ImageModelContract`, via\n * `ai.models.generateImages`. Imagen is per-image-metered and returns\n * base64 image bytes (no hosted URL, no token usage).\n *\n * **No model-id validation.** `config.name` is passed through to\n * `ai.models.generateImages` exactly as given — the constructor never\n * inspects it. Google adds and retires image model ids on its own\n * schedule, so an id this adapter does not recognize is not the\n * adapter's call to refuse; an unsupported id surfaces as a provider\n * error from Google (wrapped into the typed `AIError` hierarchy by\n * `generate()`), not as a local one.\n *\n * **Safety filtering.** When Imagen filters every candidate for safety\n * (`raiFilteredReason`), this surfaces a typed `ContentFilterError`\n * carrying the reason, rather than returning an empty success.\n *\n * @example\n * const model = new GoogleImageModel(ai, { name: \"imagen-4.0-generate-001\" }, \"google\");\n * const { images } = await model.generate(\"a watercolor lighthouse at dawn\");\n */\nexport class GoogleImageModel implements ImageModelContract {\n public readonly name: string;\n public readonly provider: string;\n public readonly pricing?: ImageModelPricing;\n\n private readonly ai: GoogleGenAI;\n private readonly logger: Logger = log;\n\n public constructor(ai: GoogleGenAI, config: GoogleImageConfig, provider: string = \"google\") {\n this.ai = ai;\n this.name = config.name;\n this.provider = provider;\n this.pricing = config.pricing;\n }\n\n public async generate(\n prompt: string,\n options?: ImageGenerationOptions,\n ): Promise<ImageGenerationResponse> {\n const config: GenerateImagesConfig = {};\n\n if (options?.count !== undefined) config.numberOfImages = options.count;\n if (options?.aspectRatio !== undefined) config.aspectRatio = options.aspectRatio;\n if (options?.negativePrompt !== undefined) config.negativePrompt = options.negativePrompt;\n if (options?.signal !== undefined) config.abortSignal = options.signal;\n\n const outputMimeType = mediaTypeFor(options?.format);\n if (outputMimeType !== undefined) config.outputMimeType = outputMimeType;\n\n // Imagen sizing is `imageSize` (\"1K\"/\"2K\") — a distinct concept from\n // OpenAI's WxH `size`, so we honor only an explicit passthrough.\n if (typeof options?.imageSize === \"string\") config.imageSize = options.imageSize;\n if (typeof options?.personGeneration === \"string\") {\n config.personGeneration =\n options.personGeneration as GenerateImagesConfig[\"personGeneration\"];\n }\n\n this.logger.debug(LOG_MODULE, \"image.request\", \"models.generateImages\", {\n model: this.name,\n count: options?.count ?? 1,\n });\n\n let response: Awaited<ReturnType<GoogleGenAI[\"models\"][\"generateImages\"]>>;\n\n try {\n response = await this.ai.models.generateImages({ model: this.name, prompt, config });\n } catch (thrown) {\n const wrapped = wrapGoogleError(thrown);\n\n this.logger.error(LOG_MODULE, \"image.error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n throw wrapped;\n }\n\n const generated = response.generatedImages ?? [];\n const images: GeneratedImage[] = [];\n\n for (const candidate of generated) {\n const bytes = candidate.image?.imageBytes;\n if (!bytes) continue;\n\n images.push({\n type: \"base64\",\n base64: bytes,\n mediaType: candidate.image?.mimeType ?? outputMimeType ?? \"image/png\",\n ...(candidate.enhancedPrompt ? { revisedPrompt: candidate.enhancedPrompt } : {}),\n });\n }\n\n if (images.length === 0) {\n const filtered = generated.find((candidate) => candidate.raiFilteredReason);\n\n if (filtered?.raiFilteredReason) {\n throw new ContentFilterError(\n `Imagen filtered all candidates: ${filtered.raiFilteredReason}`,\n { reason: filtered.raiFilteredReason },\n );\n }\n\n throw new ProviderError(\"Imagen returned no images.\");\n }\n\n this.logger.debug(LOG_MODULE, \"image.response\", \"models.generateImages succeeded\", {\n images: images.length,\n });\n\n // Imagen returns no token usage — honest zero (priced per image).\n return { images, usage: { input: 0, output: 0, total: 0 } };\n }\n}\n","import type { ModelPricing } from \"@warlock.js/ai\";\n\n/**\n * Built-in Gemini API list prices (paid tier, Standard), USD per\n * 1,000,000 tokens, keyed by exact model id. `cachedInput` is the\n * context-cache read rate; the hourly cache-storage charge is not a token\n * rate and is never included.\n *\n * Source: https://ai.google.dev/gemini-api/docs/pricing, as of\n * {@link GOOGLE_MODEL_PRICING_AS_OF}. Prices Google has already scheduled\n * are in `schedule`, so a model costs the right amount on each day without\n * a package update; models priced by prompt length carry `tiers`.\n * Left out on purpose — pass `pricing` yourself for them:\n *\n * - Models with a higher audio-input rate (`gemini-3.1-flash-lite`,\n * `gemini-3-flash-preview`): `Usage` does not split input by modality.\n * - Models without a listed price.\n *\n * Models Google retired or reroutes stay out of the table; see\n * {@link GOOGLE_MODEL_LIFECYCLE}.\n */\nexport const GOOGLE_MODEL_PRICING: Readonly<Record<string, ModelPricing>> = {\n \"gemini-4-argon\": {\n input: 2,\n output: 10,\n cachedInput: 0.1,\n schedule: [{ from: \"2026-12-01\", input: 4, output: 20, cachedInput: 0.2 }],\n },\n \"gemini-3.8-flash\": {\n input: 0.75,\n output: 3.75,\n cachedInput: 0.075,\n schedule: [{ from: \"2027-01-01\", input: 1.5, output: 7.5, cachedInput: 0.15 }],\n },\n \"gemini-3.6-flash\": {\n input: 0.75,\n output: 3.75,\n cachedInput: 0.075,\n schedule: [{ from: \"2027-01-01\", input: 1.5, output: 7.5, cachedInput: 0.15 }],\n },\n \"gemini-3.5-flash-lite\": { input: 0.3, output: 2.5, cachedInput: 0.03 },\n \"gemini-3.1-pro-preview\": {\n input: 2,\n output: 12,\n cachedInput: 0.2,\n tiers: [{ abovePromptTokens: 200_000, input: 4, output: 18, cachedInput: 0.4 }],\n },\n};\n\n/** The date the {@link GOOGLE_MODEL_PRICING} rates were last checked against Google's pricing page. */\nexport const GOOGLE_MODEL_PRICING_AS_OF = \"2026-11-06\";\n\n/**\n * Look up the built-in list price for a Gemini model id (exact match),\n * or `undefined` so cost stays unknown rather than wrong.\n *\n * @example\n * googlePricingFor(\"gemini-3.5-flash-lite\"); // { input: 0.3, output: 2.5, cachedInput: 0.03 }\n * googlePricingFor(\"gemini-2.5-pro\"); // undefined — not in the table\n */\nexport function googlePricingFor(modelName: string): ModelPricing | undefined {\n return GOOGLE_MODEL_PRICING[modelName];\n}\n\n/** USD per nonempty unique query for Gemini 3+, before free monthly allowances.\n * Gemini 2.5 and older bill per grounded prompt; do not use this rate for them.\n * https://ai.google.dev/gemini-api/docs/google-search#pricing — verified 2026-10-18. */\nexport const GOOGLE_SERVER_TOOL_PRICING = { webSearch: 0.014 };\n","import {\n InvalidRequestError,\n type ModelCallOptions,\n type ProviderToolDefinition,\n} from \"@warlock.js/ai\";\n\n/**\n * A running allowance of Google Search queries, shared by the model calls of\n * one run. Create it with {@link googleSearchBudget} and pass it as\n * `providerOptions.google.search.budget` on every call of that run.\n */\nexport type GoogleSearchBudget = {\n /** The allowance the budget was created with. */\n readonly max: number;\n /** Unique search queries the provider reported so far. */\n readonly used: number;\n /** `max - used`, never below zero. */\n readonly remaining: number;\n /** Add queries the provider reported for one finished call. */\n record(queries: number): void;\n};\n\n/**\n * Bound Google Search grounding across the model calls of one run.\n *\n * The Gemini API has no per-request search limit: `{ googleSearch: {} }` takes\n * no `max_uses`, and the model decides how many queries one `generateContent`\n * call runs. What the adapter can enforce is the number of queries across\n * calls: after each call it adds the queries Google reported\n * (`groundingMetadata.webSearchQueries`, counted the way Gemini 3 bills them,\n * unique and non-empty) to the budget, and once the budget is spent it stops\n * sending the `googleSearch` tool, so later calls run without search. One\n * call that is already running can still overshoot the budget by the queries\n * it makes itself.\n *\n * @example\n * const budget = googleSearchBudget(3);\n * const search = ai.providerTool({ provider: \"google\", definition: { googleSearch: {} } });\n * await model.complete(messages, {\n * tools: [search],\n * providerOptions: { google: { search: { budget } } },\n * });\n * budget.used; // queries spent so far\n */\nexport function googleSearchBudget(maxQueries: number): GoogleSearchBudget {\n if (!Number.isInteger(maxQueries) || maxQueries < 0) {\n throw new InvalidRequestError(\n `googleSearchBudget(maxQueries) needs a non-negative integer, got ${String(maxQueries)}.`,\n );\n }\n\n let used = 0;\n\n return {\n max: maxQueries,\n get used() {\n return used;\n },\n get remaining() {\n return Math.max(0, maxQueries - used);\n },\n record(queries) {\n if (Number.isFinite(queries) && queries > 0) {\n used += queries;\n }\n },\n };\n}\n\n/** The budget on a call, validated; `undefined` when the call sets none. */\nexport function searchBudgetOf(\n options: ModelCallOptions | undefined,\n): GoogleSearchBudget | undefined {\n const search = (options?.providerOptions?.google as { search?: { budget?: unknown } } | undefined)\n ?.search;\n const budget = search?.budget;\n\n if (budget === undefined) {\n return undefined;\n }\n\n if (\n typeof budget !== \"object\" ||\n budget === null ||\n typeof (budget as GoogleSearchBudget).record !== \"function\" ||\n typeof (budget as GoogleSearchBudget).remaining !== \"number\"\n ) {\n throw new InvalidRequestError(\n \"providerOptions.google.search.budget must come from googleSearchBudget(maxQueries).\",\n );\n }\n\n return budget as GoogleSearchBudget;\n}\n\n/** `true` for a Gemini tool entry that runs Google Search grounding. */\nexport function isSearchDefinition(definition: ProviderToolDefinition[\"definition\"]): boolean {\n return \"googleSearch\" in definition || \"googleSearchRetrieval\" in definition;\n}\n","import type { GenerateContentResponse, GroundingMetadata } from \"@google/genai\";\nimport type { Usage } from \"@warlock.js/ai\";\n\n/** Candidate snapshots are replaced, not added: streamed metadata may repeat.\n * Installed @google/genai 2.19.0 GroundingMetadata.webSearchQueries; REST schema:\n * https://ai.google.dev/api/generate-content#GroundingMetadata\n * Billing ignores empty queries and counts unique queries for Gemini 3+:\n * https://ai.google.dev/gemini-api/docs/google-search#pricing\n * Gemini 2.5 and older bill per grounded prompt; this counter is NOT that unit. */\nexport function applyGrounding(\n usage: Usage,\n snapshots: Map<number, GroundingMetadata>,\n response: GenerateContentResponse,\n): void {\n for (const [position, candidate] of (response.candidates ?? []).entries()) {\n if (candidate.groundingMetadata)\n snapshots.set(candidate.index ?? position, candidate.groundingMetadata);\n }\n const reported = [...snapshots.values()].filter(\n (metadata) => metadata.webSearchQueries !== undefined,\n );\n if (reported.length > 0) {\n const queries = reported.flatMap((metadata) => metadata.webSearchQueries ?? []);\n usage.serverToolUse = {\n ...usage.serverToolUse,\n webSearchRequests: new Set(queries.filter((query) => query !== \"\")).size,\n googleWebSearchQueries: queries.length,\n };\n }\n}\n\n/** Retain chunks, supports, search entry point and queries without flattening them. */\nexport function groundingMetadata(\n snapshots: Map<number, GroundingMetadata>,\n): Record<string, unknown> | undefined {\n return snapshots.size\n ? {\n google: {\n groundingMetadata: [...snapshots].map(([index, metadata]) => ({ index, ...metadata })),\n },\n }\n : undefined;\n}\n","import type { GoogleSearchBudget } from \"./search-budget\";\nimport { InvalidRequestError, type ModelCallOptions } from \"@warlock.js/ai\";\n\n/** Adapter-only fields merged after the neutral request parameters. */\nexport type GoogleCallOptions = {\n body?: Record<string, unknown>;\n /**\n * Bound Google Search across the calls of one run. See\n * `googleSearchBudget()`: the Gemini API has no per-request limit, so this\n * stops offering the search tool once the allowance is spent.\n */\n search?: { budget?: GoogleSearchBudget };\n};\n\nconst RESERVED_FIELDS = new Set([\"systemInstruction\", \"tools\", \"abortSignal\", \"httpOptions\"]);\n\nexport function mergeProviderBody<T extends object>(\n built: T,\n options: ModelCallOptions | undefined,\n): T {\n const provider = options?.providerOptions?.google as GoogleCallOptions | undefined;\n const body = provider?.body ?? {};\n const reserved = Object.keys(body).filter((key) => RESERVED_FIELDS.has(key));\n if (reserved.length > 0) {\n throw new InvalidRequestError(\n `providerOptions.google.body cannot set ${reserved.join(\", \")}: use the call's messages, tools and model options.`,\n { context: { fields: reserved } },\n );\n }\n const defaults = built as Record<string, unknown>;\n const overrides = Object.fromEntries(\n Object.entries(body).map(([key, value]) => {\n const previous = defaults[key];\n return [\n key,\n isPlainObject(previous) && isPlainObject(value) ? { ...previous, ...value } : value,\n ];\n }),\n );\n return { ...built, ...overrides };\n}\n\n/** Merge only plain objects; arrays, null and provider scalar values replace the field. */\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n if (value === null || typeof value !== \"object\") return false;\n const prototype = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n}\n","/*\n * Input context windows of Gemini families documented at 1,048,576 tokens (and 2,097,152 for 1.5 Pro);\n * Gemini 4 documents \"1M tokens\" and uses the same figure.\n * Other ids resolve to `undefined`; set them with\n * `google.model({ name, contextWindow })`.\n */\nconst WINDOW_1M_PREFIXES = [\n \"gemini-4\",\n \"gemini-3\",\n \"gemini-2.5-\",\n \"gemini-2.0-flash\",\n \"gemini-1.5-flash\",\n];\n\n/** Gemini 1.5 Pro documents a 2,097,152-token input window. */\nconst WINDOW_2M_PREFIXES = [\"gemini-1.5-pro\"];\n\n/**\n * The known context window of a Gemini model id, or `undefined` when it is\n * not in the table. A leading `models/` is accepted.\n *\n * @example\n * inferContextWindow(\"gemini-2.5-flash\"); // → 1048576\n */\nexport function inferContextWindow(modelName: string): number | undefined {\n const id = modelName.toLowerCase().replace(/^models\\//, \"\");\n\n if (WINDOW_2M_PREFIXES.some((prefix) => id.startsWith(prefix))) {\n return 2_097_152;\n }\n\n return WINDOW_1M_PREFIXES.some((prefix) => id.startsWith(prefix)) ? 1_048_576 : undefined;\n}\n","/**\n * Substrings identifying Gemini model ids whose family accepts image\n * input (vision).\n *\n * Every Gemini 1.5, 2.x, and 2.5 model is natively multimodal, as is\n * the legacy `gemini-pro-vision`. Only the original text-only\n * `gemini-pro` / `gemini-1.0-pro` is excluded. A substring match\n * tolerates the date/preview suffixes Google appends\n * (`gemini-2.5-flash-preview-05-20`). Override per-model via\n * `google.model({ name, vision: true | false })`.\n */\nconst VISION_CAPABLE_SUBSTRINGS = [\n \"gemini-1.5\",\n \"gemini-2\",\n \"gemini-exp\",\n \"gemini-pro-vision\",\n \"gemini-flash\",\n];\n\n/**\n * Infer whether a Gemini model id supports vision based on the known\n * multimodal-family substrings. Unknown ids default to `false` so\n * passing an image attachment to an unsupported model surfaces a\n * clear, agent-side capability error instead of an opaque Gemini 400.\n *\n * @example\n * inferVisionCapability(\"gemini-2.5-flash\"); // → true\n * inferVisionCapability(\"gemini-1.5-pro-002\"); // → true\n * inferVisionCapability(\"gemini-1.0-pro\"); // → false\n * inferVisionCapability(\"text-embedding-004\"); // → false\n */\nexport function inferVisionCapability(modelId: string): boolean {\n const normalized = modelId.toLowerCase();\n\n return VISION_CAPABLE_SUBSTRINGS.some((fragment) => normalized.includes(fragment));\n}\n","import {\n attachCost,\n InvalidRequestError,\n resolveStreamChunks,\n selectProviderTools,\n type Message,\n type ModelCallOptions,\n type ModelCapabilities,\n type ModelContract,\n type ModelPricing,\n type ModelResponse,\n type ModelStreamChunk,\n type ModelToolCallRequest,\n type ReasoningEffort,\n type Usage,\n} from \"@warlock.js/ai\";\nimport { log, type Logger } from \"@warlock.js/logger\";\nimport type {\n GenerateContentConfig,\n GenerateContentResponse,\n GoogleGenAI,\n GroundingMetadata,\n Part,\n Tool,\n} from \"@google/genai\";\nimport { isSearchDefinition, searchBudgetOf } from \"./search-budget\";\nimport { applyGrounding, groundingMetadata } from \"./utils/grounding\";\nimport { mergeProviderBody } from \"./provider-options\";\nimport type { GoogleModelConfig } from \"./config.type\";\nimport { inferContextWindow } from \"./known-context-windows\";\nimport { inferVisionCapability } from \"./known-vision-models\";\nimport {\n applyGoogleUsage,\n mapFinishReason,\n toGoogleContents,\n toGoogleTools,\n wrapGoogleError,\n} from \"./utils\";\n\nconst LOG_MODULE = \"ai.google\";\n\n/** `provider` value of `ai.providerTool()` entries this adapter sends verbatim. */\nconst GOOGLE_WIRE_FORMAT = \"google\";\n\n/**\n * Bucketed `thinkingBudget` (token caps) for the neutral\n * `reasoning.effort` levels when the caller gives no explicit\n * `reasoning.maxTokens`. Gemini 2.5 accepts a positive budget as a cap\n * on the thinking phase; these mirror the spread the OpenAI\n * `reasoning_effort` low/medium/high tiers imply. The installed\n * `@google/genai` `ThinkingConfig` declaration documents that allowed\n * ranges are model-dependent, so retain the existing conservative 24,576\n * cap for the new tiers rather than sending a budget unsupported by Flash.\n */\nconst EFFORT_THINKING_BUDGET: Record<Exclude<ReasoningEffort, \"none\">, number> = {\n minimal: 1024,\n low: 1024,\n medium: 8192,\n high: 24576,\n xhigh: 24576,\n max: 24576,\n};\n\n/**\n * Google Gemini-backed implementation of `ModelContract`.\n *\n * **Role.** The provider-facing bridge between the vendor-neutral\n * `@warlock.js/ai` agent runtime and the `@google/genai` SDK\n * (`models.generateContent` / `generateContentStream`).\n *\n * **Responsibility.**\n * - Owns: a long-lived `GoogleGenAI` client + frozen `ModelConfig`\n * (name, temperature, maxTokens) used as per-call defaults.\n * - Owns: translating vendor-neutral `Message[]` / `ToolConfig[]` into\n * Gemini shapes (systemInstruction hoisting, `model` role,\n * `functionCall` / `functionResponse` parts, inline image bytes) on\n * the way out, and Gemini's candidate/parts response (text, function\n * calls, finish reason, token usage) back into neutral shapes on the\n * way in.\n * - Does NOT own: dispatching tools, looping, history, retries — those\n * are agent concerns. The model is a per-call protocol adapter.\n *\n * Modeled as a class (see §4.2 of code-style.md — \"long-lived state\n * across calls\"): the `GoogleGenAI` client is reused for the SDK's\n * lifetime.\n *\n * @example\n * import { GoogleGenAI } from \"@google/genai\";\n * const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });\n * const model = new GoogleModel(ai, { name: \"gemini-2.5-flash\" });\n *\n * const myAgent = agent({ model, tools: [searchTool] });\n * const result = await myAgent.execute(\"Summarize today's news.\");\n */\nexport class GoogleModel implements ModelContract {\n public readonly name: string;\n public readonly provider: string;\n public readonly capabilities: ModelCapabilities;\n public readonly pricing?: ModelPricing;\n\n private readonly ai: GoogleGenAI;\n private readonly config: GoogleModelConfig;\n private readonly logger: Logger = log;\n\n public constructor(ai: GoogleGenAI, config: GoogleModelConfig, provider: string = \"google\") {\n this.ai = ai;\n this.config = config;\n this.name = config.name;\n this.provider = provider;\n this.pricing = config.pricing;\n const multimodal = config.vision ?? inferVisionCapability(config.name);\n\n const window =\n config.contextWindow ?? (provider === \"google\" ? inferContextWindow(config.name) : undefined);\n\n this.capabilities = {\n ...(window !== undefined ? { contextWindow: window } : {}),\n structuredOutput: config.structuredOutput ?? true,\n vision: multimodal,\n // Function responses are JSON objects here; images fall back to text.\n toolResultImages: false,\n // Every Gemini 2.5 model thinks; older families harmlessly ignore\n // an empty thinking budget. Defaulting `true` lets the agent\n // forward reasoning options; an explicit `false` opts a model out.\n reasoning: config.reasoning ?? true,\n // Gemini reports cache-read hits (`cachedContentTokenCount`) on\n // every call via implicit caching, and accepts explicit context\n // caching. Read-side accounting is always honored.\n promptCaching: true,\n // The multimodal Gemini families that accept images also accept\n // audio and PDF/document parts. Mirror the vision inference unless\n // explicitly overridden.\n audio: config.audio ?? multimodal,\n pdf: config.pdf ?? multimodal,\n };\n }\n\n /**\n * Single-shot completion. Sends the full message list to\n * `generateContent`, waits for the terminal response, and reshapes\n * it into a vendor-neutral `ModelResponse`. Per-call `options`\n * override the instance defaults for this call only.\n */\n public async complete(messages: Message[], options?: ModelCallOptions): Promise<ModelResponse> {\n this.logger.debug(LOG_MODULE, \"request\", \"Starting generateContent call\", {\n model: this.name,\n messageCount: messages.length,\n streaming: false,\n toolCount: options?.tools?.length ?? 0,\n });\n\n const { systemInstruction, contents } = toGoogleContents(messages);\n\n let response: GenerateContentResponse;\n\n try {\n response = await this.ai.models.generateContent({\n model: this.name,\n contents,\n config: this.buildConfig(systemInstruction, options),\n });\n } catch (thrown) {\n throw this.logAndWrap(thrown, options?.signal);\n }\n\n const toolCalls = this.extractToolCalls(response);\n const finishReason = toolCalls\n ? \"tool_calls\"\n : mapFinishReason(response.candidates?.[0]?.finishReason);\n const usage = this.extractUsage(response);\n const grounding = new Map<number, GroundingMetadata>();\n applyGrounding(usage, grounding, response);\n searchBudgetOf(options)?.record(usage.serverToolUse?.webSearchRequests ?? 0);\n const providerMetadata = groundingMetadata(grounding);\n attachCost(usage, this.pricing);\n\n this.logger.debug(LOG_MODULE, \"response\", \"generateContent call succeeded\", {\n finishReason,\n usage,\n });\n\n return {\n ...(providerMetadata ? { providerMetadata } : {}),\n content: response.text ?? \"\",\n finishReason,\n usage,\n toolCalls,\n };\n }\n\n /** Count native input tokens with the same converted content, system and tools. */\n public async countTokens(messages: Message[], options?: ModelCallOptions): Promise<number> {\n const { systemInstruction, contents } = toGoogleContents(messages);\n const config = this.buildConfig(systemInstruction, options);\n if (config.cachedContent !== undefined) {\n throw new InvalidRequestError(\n \"The Google SDK countTokens endpoint cannot count a cachedContent reference.\",\n { reason: \"count-unsupported\" },\n );\n }\n try {\n const response = this.ai.vertexai\n ? await this.ai.models.countTokens({\n model: this.name,\n contents,\n config: {\n ...(config.systemInstruction !== undefined\n ? { systemInstruction: config.systemInstruction }\n : {}),\n ...(config.tools !== undefined ? { tools: config.tools as Tool[] } : {}),\n ...(config.abortSignal ? { abortSignal: config.abortSignal } : {}),\n ...(config.httpOptions ? { httpOptions: config.httpOptions } : {}),\n },\n })\n : await this.countDeveloperTokens(contents, config);\n if (response.totalTokens === undefined)\n throw new InvalidRequestError(\"Google countTokens returned no totalTokens.\");\n return response.totalTokens;\n } catch (thrown) {\n throw this.logAndWrap(thrown, options?.signal);\n }\n }\n\n /**\n * REST counts the full generation request, unlike the typed Developer API\n * converter in @google/genai 2.19.0. See https://ai.google.dev/api/tokens.\n * extraBody keeps the SDK's auth, fetch, base URL, retries and cancellation.\n */\n private countDeveloperTokens(\n contents: ReturnType<typeof toGoogleContents>[\"contents\"],\n config: GenerateContentConfig,\n ) {\n config.abortSignal?.throwIfAborted();\n\n const {\n systemInstruction,\n serviceTier,\n tools,\n toolConfig,\n safetySettings,\n cachedContent,\n abortSignal,\n httpOptions,\n ...generationConfig\n } = config;\n // Automatic function calling is SDK-only, never a REST generation field.\n delete generationConfig.automaticFunctionCalling;\n const model = this.name.startsWith(\"models/\") ? this.name : `models/${this.name}`;\n\n return this.ai.models.countTokens({\n model: this.name,\n contents,\n config: {\n abortSignal,\n httpOptions: {\n ...httpOptions,\n extraBody: {\n ...httpOptions?.extraBody,\n // The SDK merges extraBody before JSON serialization. undefined\n // removes its contents field: REST accepts only one input form.\n contents: undefined,\n generateContentRequest: {\n model,\n contents,\n ...(serviceTier !== undefined ? { serviceTier } : {}),\n ...(systemInstruction !== undefined\n ? { systemInstruction: { parts: [{ text: systemInstruction }] } }\n : {}),\n ...(tools !== undefined ? { tools } : {}),\n ...(toolConfig !== undefined ? { toolConfig } : {}),\n ...(safetySettings !== undefined ? { safetySettings } : {}),\n ...(cachedContent !== undefined ? { cachedContent } : {}),\n ...(Object.keys(generationConfig).length > 0 ? { generationConfig } : {}),\n },\n },\n },\n },\n });\n }\n\n /**\n * Incremental streaming completion via `generateContentStream`.\n * Yields neutral `ModelStreamChunk`s — `delta` for text, `tool-call`\n * per function call (Gemini emits a fully-formed call, not partial\n * JSON), and a terminal `done` with the final finish reason + usage.\n *\n * With `options.streamChunks` it also yields `reasoning` for thought\n * parts (only sent when `thinkingConfig.includeThoughts` is on — set it\n * through `providerOptions.google.body`) and `usage` whenever a chunk\n * carries `usageMetadata`. No `tool-args`: calls arrive whole.\n */\n public async *stream(\n messages: Message[],\n options?: ModelCallOptions,\n ): AsyncIterable<ModelStreamChunk> {\n this.logger.debug(LOG_MODULE, \"request\", \"Starting generateContentStream call\", {\n model: this.name,\n messageCount: messages.length,\n streaming: true,\n toolCount: options?.tools?.length ?? 0,\n });\n\n const { systemInstruction, contents } = toGoogleContents(messages);\n\n let iterable: AsyncGenerator<GenerateContentResponse>;\n\n try {\n iterable = await this.ai.models.generateContentStream({\n model: this.name,\n contents,\n config: this.buildConfig(systemInstruction, options),\n });\n } catch (thrown) {\n throw this.logAndWrap(thrown, options?.signal);\n }\n\n let rawFinishReason: string | undefined;\n let sawToolCall = false;\n const usage: Usage = { input: 0, output: 0, total: 0 };\n const want = resolveStreamChunks(options);\n const grounding = new Map<number, GroundingMetadata>();\n\n try {\n for await (const chunk of iterable) {\n applyGrounding(usage, grounding, chunk);\n // Thought parts (`thought: true`, sent when `thinkingConfig.includeThoughts`\n // is on) carry the thought summary in `text`; the SDK's `chunk.text`\n // getter already leaves them out of the answer.\n if (want.reasoning) {\n for (const part of chunk.candidates?.[0]?.content?.parts ?? []) {\n if (part.thought === true && typeof part.text === \"string\" && part.text !== \"\") {\n yield { type: \"reasoning\", delta: part.text };\n }\n }\n }\n\n const text = chunk.text;\n\n if (text) {\n yield { type: \"delta\", content: text };\n }\n\n for (const part of chunk.candidates?.[0]?.content?.parts ?? []) {\n const toolCall = this.partToToolCall(part);\n\n if (!toolCall) {\n continue;\n }\n\n sawToolCall = true;\n\n yield {\n type: \"tool-call\",\n id: toolCall.id,\n name: toolCall.name,\n input: toolCall.input,\n ...(toolCall.providerMetadata ? { providerMetadata: toolCall.providerMetadata } : {}),\n };\n }\n\n const candidateFinish = chunk.candidates?.[0]?.finishReason;\n\n if (candidateFinish) {\n rawFinishReason = candidateFinish;\n }\n\n if (chunk.usageMetadata) {\n applyGoogleUsage(usage, chunk.usageMetadata);\n\n if (want.usage) {\n yield { type: \"usage\", usage: { ...usage } };\n }\n }\n }\n } catch (thrown) {\n throw this.logAndWrap(thrown, options?.signal);\n }\n\n const finishReason = sawToolCall ? \"tool_calls\" : mapFinishReason(rawFinishReason);\n\n attachCost(usage, this.pricing);\n\n this.logger.debug(LOG_MODULE, \"response\", \"generateContentStream call succeeded\", {\n finishReason,\n usage,\n });\n\n searchBudgetOf(options)?.record(usage.serverToolUse?.webSearchRequests ?? 0);\n const providerMetadata = groundingMetadata(grounding);\n yield { type: \"done\", finishReason, usage, ...(providerMetadata ? { providerMetadata } : {}) };\n }\n\n /**\n * Assemble the `GenerateContentConfig` shared by `complete()` and\n * `stream()`: inference params, hoisted system instruction,\n * cancellation signal, and conditional tools + native structured\n * output.\n */\n private buildConfig(\n systemInstruction: string | undefined,\n options: ModelCallOptions | undefined,\n ): GenerateContentConfig {\n const temperature = options?.temperature ?? this.config.temperature;\n const maxOutputTokens = options?.maxTokens ?? this.config.maxTokens;\n\n return mergeProviderBody<GenerateContentConfig>(\n {\n ...(systemInstruction ? { systemInstruction } : {}),\n ...(temperature !== undefined ? { temperature } : {}),\n ...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}),\n ...(options?.signal ? { abortSignal: options.signal } : {}),\n ...this.buildTools(options),\n ...this.buildStructuredOutput(options?.responseSchema),\n ...this.buildThinking(options?.reasoning),\n },\n options,\n );\n }\n\n /**\n * Translate the neutral `reasoning` option into Gemini's\n * `thinkingConfig`. `reasoning.maxTokens` maps directly to\n * `thinkingBudget` (token cap on the thinking phase); when only\n * `reasoning.effort` is given it is bucketed into a budget. Emitted\n * only when the model is `reasoning`-capable — a `false` capability\n * (config override) drops it so a non-thinking model never receives\n * an unsupported `thinkingConfig`.\n *\n * Gemini's `thinkingBudget` semantics: `0` disables thinking, `-1`\n * lets the model decide automatically. A positive value caps the\n * thinking tokens. The neutral `effort: \"none\"` (\"run without\n * reasoning\") maps to `thinkingBudget: 0`.\n */\n private buildThinking(\n reasoning: ModelCallOptions[\"reasoning\"],\n ): Pick<GenerateContentConfig, \"thinkingConfig\"> {\n if (!reasoning || !this.capabilities.reasoning) {\n return {};\n }\n\n // `effort: \"none\"` = explicit \"run without reasoning\". Gemini disables\n // thinking with `thinkingBudget: 0` (its native off switch), so emit\n // that rather than omitting the config — an omitted config lets a\n // thinking model reason at its default budget.\n if (reasoning.effort === \"none\") {\n return { thinkingConfig: { thinkingBudget: 0 } };\n }\n\n const thinkingBudget =\n reasoning.maxTokens ??\n (reasoning.effort ? EFFORT_THINKING_BUDGET[reasoning.effort] : undefined);\n\n if (thinkingBudget === undefined) {\n return {};\n }\n\n return { thinkingConfig: { thinkingBudget } };\n }\n\n /**\n * Spread-friendly tools fragment. Empty object when no tools were\n * supplied so the caller can unconditionally spread it. Function tools\n * come first; `ai.providerTool({ provider: \"google\" })` definitions\n * (`{ googleSearch: {} }`, `{ codeExecution: {} }`, `{ urlContext: {} }`,\n * …) follow verbatim as their own `Tool` entries. Other providers'\n * definitions are skipped with a warning.\n */\n private buildTools(options: ModelCallOptions | undefined): Pick<GenerateContentConfig, \"tools\"> {\n const { tools, definitions } = selectProviderTools(options, {\n accepts: [GOOGLE_WIRE_FORMAT, this.provider],\n module: LOG_MODULE,\n model: this.name,\n logger: this.logger,\n });\n const budget = searchBudgetOf(options);\n // The Gemini API cannot bound searches inside one call, so a spent budget\n // is enforced here by not offering Google Search again.\n const offered =\n budget && budget.remaining <= 0\n ? definitions.filter((definition) => !isSearchDefinition(definition))\n : definitions;\n\n if (offered.length < definitions.length) {\n this.logger.warn(\n LOG_MODULE,\n \"search-budget\",\n \"Google Search budget is spent; calling without the search tool\",\n { model: this.name, max: budget?.max, used: budget?.used },\n );\n }\n\n const mapped: Tool[] = [...(toGoogleTools(tools) ?? []), ...(offered as unknown as Tool[])];\n\n return mapped.length > 0 ? { tools: mapped } : {};\n }\n\n /**\n * Translate the neutral `responseSchema` into Gemini's native JSON\n * structured output (`responseMimeType: \"application/json\"` +\n * `responseJsonSchema`, which takes a raw JSON Schema directly).\n * Emitted only when the model is `structuredOutput`-capable and the\n * schema is an object root — otherwise the agent's soft prompt hint\n * + client-side `validate()` carry shape.\n */\n private buildStructuredOutput(\n responseSchema: Record<string, unknown> | undefined,\n ): Pick<GenerateContentConfig, \"responseMimeType\" | \"responseJsonSchema\"> {\n if (!responseSchema || !this.capabilities.structuredOutput) {\n return {};\n }\n\n if (responseSchema.type !== \"object\" || typeof responseSchema.properties !== \"object\") {\n return {};\n }\n\n return {\n responseMimeType: \"application/json\",\n responseJsonSchema: responseSchema,\n };\n }\n\n /**\n * Reshape Gemini's function-call content parts into the neutral\n * `ModelToolCallRequest[]`. Returns `undefined` when the model\n * requested no functions so callers can branch on presence.\n *\n * Reads `candidates[0].content.parts` directly rather than the\n * `response.functionCalls` getter: the getter discards the\n * part-level `thoughtSignature`, and Gemini \"thinking\" models 400\n * the follow-up turn if that signature is not echoed back. See\n * `partToToolCall`.\n */\n private extractToolCalls(response: GenerateContentResponse): ModelToolCallRequest[] | undefined {\n const parts = response.candidates?.[0]?.content?.parts ?? [];\n const toolCalls = parts\n .map((part) => this.partToToolCall(part))\n .filter((call): call is ModelToolCallRequest => call !== undefined);\n\n return toolCalls.length > 0 ? toolCalls : undefined;\n }\n\n /**\n * Map a single Gemini `Part` to a neutral `ModelToolCallRequest`,\n * or `undefined` when the part is not a function call. The part's\n * `thoughtSignature` (opaque, set by thinking models) is carried on\n * `providerMetadata` so `toGoogleContents` can replay it on the\n * assistant turn — Gemini rejects the next request without it.\n */\n private partToToolCall(part: Part): ModelToolCallRequest | undefined {\n if (!part.functionCall) {\n return undefined;\n }\n\n const call = part.functionCall;\n\n return {\n // The Gemini Developer API does not assign function-call ids\n // (only Vertex parallel-calling does). Fall back to the function\n // name so the neutral `toolCallId` is non-empty and the echoed\n // `functionResponse.name` resolves — Gemini matches a result to\n // its call by name. See decisions §49.\n id: call.id ?? call.name ?? \"\",\n name: call.name ?? \"\",\n input: (call.args ?? {}) as Record<string, unknown>,\n ...(part.thoughtSignature\n ? { providerMetadata: { thoughtSignature: part.thoughtSignature } }\n : {}),\n };\n }\n\n /**\n * Normalize Gemini's `usageMetadata` into the neutral `Usage` shape\n * via the shared {@link applyGoogleUsage} mapper (the same one the\n * streaming loop and the Gemini image model use). Absent usage\n * collapses to zeros.\n */\n private extractUsage(response: GenerateContentResponse): Usage {\n const usage: Usage = { input: 0, output: 0, total: 0 };\n\n if (response.usageMetadata) {\n applyGoogleUsage(usage, response.usageMetadata);\n }\n\n return usage;\n }\n\n /**\n * Wrap a thrown provider error into the typed `AIError` hierarchy\n * and emit the standard error log line before it propagates.\n */\n private logAndWrap(thrown: unknown, signal?: AbortSignal) {\n const wrapped = wrapGoogleError(thrown, { signal });\n\n this.logger.error(LOG_MODULE, \"error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n return wrapped;\n }\n}\n","import { GoogleGenAI } from \"@google/genai\";\nimport type {\n EmbedderContract,\n ListModelsOptions,\n ModelInfo,\n ModelKind,\n ImageModelContract,\n ModelContract,\n ModelPricing,\n SDKAdapterContract,\n} from \"@warlock.js/ai\";\nimport { approximateTokenCount, inferModelKind } from \"@warlock.js/ai\";\nimport type {\n GoogleEmbedderConfig,\n GoogleImageConfig,\n GoogleModelConfig,\n GoogleSDKConfig,\n} from \"./config.type\";\nimport { GoogleEmbedder } from \"./embedder\";\nimport { GeminiImageModel } from \"./gemini-image\";\nimport { GoogleImageModel } from \"./image\";\nimport { googlePricingFor } from \"./known-pricing\";\nimport { GoogleModel } from \"./model\";\nimport { wrapGoogleError } from \"./utils\";\n\n/**\n * Pick the transport for an image model id.\n *\n * `ai.models.generateImages` calls `{model}:predict`, and a `gemini-`\n * id sent there comes back `404 … is not supported for predict`\n * (observed verbatim from Google). `generateContent` is what the SDK\n * itself points `generateImages` users at — its deprecation notice\n * reads \"Please use the generateContent method with image models\n * instead\" — so the id has to choose the transport.\n *\n * Runs in this package establish where a `gemini-` id is ACCEPTED, not\n * what it returns: on this transport such an id got as far as a quota\n * error (HTTP 429) instead of the 404. That an image\n * comes back end-to-end once billing is enabled is reported by the\n * maintainer from a locally linked build, not measured here. Whether\n * these models report token usage is still unknown.\n *\n * This is ROUTING, not validation — no id is refused here. An id this\n * function does not recognize takes the `generateImages` route, the\n * only route that existed before Gemini image support landed, so every\n * id that reached Google before still reaches Google the same way and\n * still fails (or succeeds) at the provider.\n *\n * A leading `models/` resource prefix is tolerated, matching the id\n * shapes `inferVisionCapability` already accepts\n * (`models/gemini-1.5-flash-001`).\n */\nfunction usesGeminiImageTransport(name: string): boolean {\n return name\n .toLowerCase()\n .replace(/^models\\//, \"\")\n .startsWith(\"gemini-\");\n}\n\n/**\n * Google Gemini-backed implementation of `SDKAdapterContract`.\n *\n * **Role.** The package entry point for Gemini models via the\n * `@google/genai` SDK. A single `GoogleSDK` holds one live\n * `GoogleGenAI` client, shared by every `ModelContract` /\n * `EmbedderContract` it produces. Construct one SDK per\n * account/project and reuse it everywhere.\n *\n * **Responsibility.**\n * - Owns: a long-lived `GoogleGenAI` client (auth, Vertex vs Gemini\n * API) and its lifetime. Factory for `GoogleModel` /\n * `GoogleEmbedder` instances sharing that client.\n * - Does NOT own: anything per-call — those live in `GoogleModel` /\n * `GoogleEmbedder` and the agent runtime.\n *\n * Modeled as a class (see §4.2 of code-style.md — \"long-lived state\n * across many calls\"), fronted by FP usage like the other adapters.\n *\n * @example\n * const google = new GoogleSDK({ apiKey: process.env.GEMINI_API_KEY! });\n * const model = google.model({ name: \"gemini-2.5-flash\", temperature: 0.7 });\n * const embedder = google.embedder({ name: \"gemini-embedding-001\" });\n */\nexport class GoogleSDK implements SDKAdapterContract {\n private readonly ai: GoogleGenAI;\n private readonly provider: string;\n private readonly pricing?: Record<string, ModelPricing>;\n\n public constructor(config: GoogleSDKConfig) {\n const { provider, pricing, ...clientOptions } = config;\n\n this.ai = new GoogleGenAI(clientOptions);\n this.provider = provider ?? \"google\";\n this.pricing = pricing;\n }\n\n /**\n * Build a `GoogleModel` bound to this SDK's client. Each call\n * returns a fresh instance; all instances share the underlying\n * `GoogleGenAI` client. The SDK's `provider` label is forwarded.\n *\n * Pricing resolution: per-model `config.pricing` wins; otherwise the\n * SDK-level registry entry keyed by `config.name`; otherwise the\n * built-in list price (`GOOGLE_MODEL_PRICING`); otherwise `undefined`\n * (no cost computed).\n */\n public model(config: GoogleModelConfig): ModelContract {\n const resolvedPricing =\n config.pricing ?? this.pricing?.[config.name] ?? googlePricingFor(config.name);\n const resolvedConfig: GoogleModelConfig =\n resolvedPricing === config.pricing ? config : { ...config, pricing: resolvedPricing };\n\n return new GoogleModel(this.ai, resolvedConfig, this.provider);\n }\n\n /**\n * Rough token-count estimate. Uses the character-heuristic\n * (`approximateTokenCount`) from the core package — Gemini's\n * `countTokens` is a network round-trip; `count()` is intentionally\n * offline. Good for budgeting/quota guards, not billing.\n */\n public async count(text: string, _model?: string): Promise<number> {\n return approximateTokenCount(text);\n }\n\n /**\n * Build a `GoogleEmbedder` bound to this SDK's client.\n *\n * @example\n * const embedder = google.embedder({ name: \"gemini-embedding-001\" });\n * const { vector } = await embedder.embed(\"Hello world\");\n */\n public embedder(config: GoogleEmbedderConfig): EmbedderContract {\n return new GoogleEmbedder(this.ai, config, this.provider);\n }\n\n /**\n * Build an image model bound to this SDK's client for use with\n * `ai.image({ model, prompt })`. `config.name` decides the transport\n * (see {@link usesGeminiImageTransport}) — a `gemini-` id gets the\n * `generateContent` implementation, everything else the Imagen\n * `generateImages` one. No id is rejected locally either way, so an\n * unsupported model fails at Google, not here.\n *\n * The two differ in what usage they can report, which is what the\n * caller must price for: the Imagen path always returns a zero token\n * `Usage` (Imagen reports none — price with `{ perImage }`), while the\n * Gemini path passes through whatever `usageMetadata` Google attaches\n * (price with `{ input, output }` when tokens come back).\n *\n * Pricing resolution mirrors `model()`: per-model `config.pricing`\n * wins, otherwise the SDK-level registry entry keyed by `config.name`,\n * otherwise `undefined`.\n *\n * @example\n * const imagen = google.image({ name: \"imagen-4.0-generate-001\" });\n * const gemini = google.image({ name: \"gemini-3.1-flash-lite-image\" });\n * const { data } = await ai.image({ model: gemini, prompt: \"a red bicycle\" });\n */\n public image(config: GoogleImageConfig): ImageModelContract {\n const resolvedPricing = config.pricing ?? this.pricing?.[config.name];\n const resolvedConfig: GoogleImageConfig =\n resolvedPricing === config.pricing ? config : { ...config, pricing: resolvedPricing };\n\n if (usesGeminiImageTransport(config.name)) {\n return new GeminiImageModel(this.ai, resolvedConfig, this.provider);\n }\n\n return new GoogleImageModel(this.ai, resolvedConfig, this.provider);\n }\n\n /**\n * List the models this key or project can use, live from the Gemini API\n * `models.list` endpoint, mapped to {@link ModelInfo}. Follows pagination to\n * the end. `kind` comes from the model's `supportedActions`:\n * `generateContent` is `\"chat\"` (unless the id is an image or audio\n * model), `embedContent` is `\"embedding\"`, `predict` / `predictLongRunning`\n * is `\"image\"`. Google reports no creation date, so `created` is left out.\n * Failures are thrown as the adapter's usual provider errors.\n *\n * @example\n * const models = await google.models();\n * const chat = models.filter((model) => model.kind === \"chat\");\n */\n public async models(options?: ListModelsOptions): Promise<ModelInfo[]> {\n const found = new Map<string, ModelInfo>();\n\n try {\n const pager = await this.ai.models.list({\n config: {\n pageSize: 1000,\n ...(options?.signal ? { abortSignal: options.signal } : {}),\n },\n });\n\n for await (const entry of pager) {\n const rawName = entry.name;\n\n if (typeof rawName !== \"string\" || rawName === \"\") {\n continue;\n }\n\n const id = rawName.replace(/^(?:publishers\\/[^/]+\\/)?models\\//u, \"\");\n const actions = entry.supportedActions ?? [];\n\n found.set(id, {\n id,\n label: entry.displayName || id,\n provider: this.provider,\n kind: kindOfActions(actions, id),\n ...(entry.inputTokenLimit ? { contextWindow: entry.inputTokenLimit } : {}),\n ...(entry.outputTokenLimit ? { maxOutputTokens: entry.outputTokenLimit } : {}),\n ...(entry.description ? { description: entry.description } : {}),\n });\n }\n } catch (thrown) {\n throw wrapGoogleError(thrown);\n }\n\n return [...found.values()];\n }\n}\n\n/** Map Gemini `supportedActions` to a {@link ModelKind}; fall back to the id when none are listed. */\nfunction kindOfActions(actions: string[], id: string): ModelKind {\n const inferred = inferModelKind(id);\n\n if (actions.includes(\"embedContent\") || actions.includes(\"batchEmbedContents\")) {\n return \"embedding\";\n }\n\n if (actions.includes(\"predict\") || actions.includes(\"predictLongRunning\")) {\n return inferred === \"audio\" ? \"audio\" : \"image\";\n }\n\n if (actions.includes(\"generateContent\")) {\n return inferred === \"embedding\" || inferred === \"moderation\" ? \"other\" : inferred;\n }\n\n return actions.length === 0 ? inferred : \"other\";\n}\n","import type { ModelLifecycle } from \"@warlock.js/ai\";\n\n/**\n * Where Google's deprecations page puts the Gemini text models the\n * adapter knows, as of {@link GOOGLE_MODEL_LIFECYCLE_AS_OF}. A `retiring`\n * model is served until `retiresOn`; Google reroutes `gemini-2.5-pro` and\n * `gemini-2.5-flash` requests to the replacement on that date. `retired`\n * ids were removed or are already rerouted.\n *\n * Source: https://ai.google.dev/gemini-api/docs/deprecations (earliest\n * possible shutdown dates, as Google words them). `gemini-2.5-flash-lite`\n * is left out: the pricing page calls it deprecated while the\n * deprecations page lists no date.\n */\nexport const GOOGLE_MODEL_LIFECYCLE: Readonly<Record<string, ModelLifecycle>> = {\n \"gemini-4-argon\": { status: \"current\" },\n \"gemini-3.8-flash\": { status: \"current\" },\n \"gemini-3.6-flash\": { status: \"current\" },\n \"gemini-3.5-flash-lite\": { status: \"current\" },\n \"gemini-3.1-pro-preview\": { status: \"current\" },\n \"gemini-3-flash-preview\": { status: \"current\", replacement: \"gemini-3.6-flash\" },\n \"gemini-3.1-flash-lite\": {\n status: \"retiring\",\n retiresOn: \"2027-05-07\",\n replacement: \"gemini-3.5-flash-lite\",\n },\n \"gemini-2.5-pro\": {\n status: \"retiring\",\n retiresOn: \"2026-12-01\",\n replacement: \"gemini-3.8-flash\",\n },\n \"gemini-2.5-flash\": {\n status: \"retiring\",\n retiresOn: \"2026-12-01\",\n replacement: \"gemini-3.5-flash-lite\",\n },\n \"gemini-3.7-flash\": { status: \"retired\", replacement: \"gemini-3.8-flash\" },\n \"gemini-3.5-flash\": { status: \"retired\", replacement: \"gemini-3.6-flash\" },\n \"gemini-3-pro-preview\": {\n status: \"retired\",\n retiresOn: \"2026-03-09\",\n replacement: \"gemini-3.1-pro-preview\",\n },\n \"gemini-2.0-flash\": {\n status: \"retired\",\n retiresOn: \"2026-06-01\",\n replacement: \"gemini-3.6-flash\",\n },\n \"gemini-2.0-flash-lite\": {\n status: \"retired\",\n retiresOn: \"2026-06-01\",\n replacement: \"gemini-3.1-flash-lite\",\n },\n};\n\n/** The date {@link GOOGLE_MODEL_LIFECYCLE} was last checked against Google's deprecations page. */\nexport const GOOGLE_MODEL_LIFECYCLE_AS_OF = \"2026-11-06\";\n\n/**\n * Look up the lifecycle of a Gemini model id (exact match; a leading\n * `models/` is accepted), or `undefined` when the table does not cover it.\n *\n * @example\n * googleLifecycleFor(\"gemini-2.5-pro\"); // { status: \"retiring\", retiresOn: \"2026-12-01\", replacement: \"gemini-3.8-flash\" }\n */\nexport function googleLifecycleFor(modelName: string): ModelLifecycle | undefined {\n const id = modelName.replace(/^models\\//, \"\");\n\n return Object.hasOwn(GOOGLE_MODEL_LIFECYCLE, id) ? GOOGLE_MODEL_LIFECYCLE[id] : undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA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AQ,UAAU;CACxC,oBAAoB,EAAE,QAAQ,UAAU;CACxC,yBAAyB,EAAE,QAAQ,UAAU;CAC7C,0BAA0B,EAAE,QAAQ,UAAU;CAC9C,0BAA0B;EAAE,QAAQ;EAAW,aAAa;CAAmB;CAC/E,yBAAyB;EACvB,QAAQ;EACR,WAAW;EACX,aAAa;CACf;CACA,kBAAkB;EAChB,QAAQ;EACR,WAAW;EACX,aAAa;CACf;CACA,oBAAoB;EAClB,QAAQ;EACR,WAAW;EACX,aAAa;CACf;CACA,oBAAoB;EAAE,QAAQ;EAAW,aAAa;CAAmB;CACzE,oBAAoB;EAAE,QAAQ;EAAW,aAAa;CAAmB;CACzE,wBAAwB;EACtB,QAAQ;EACR,WAAW;EACX,aAAa;CACf;CACA,oBAAoB;EAClB,QAAQ;EACR,WAAW;EACX,aAAa;CACf;CACA,yBAAyB;EACvB,QAAQ;EACR,WAAW;EACX,aAAa;CACf;AACF;;AAGA,MAAa,+BAA+B;;;;;;;;AAS5C,SAAgB,mBAAmB,WAA+C;CAChF,MAAM,KAAK,UAAU,QAAQ,aAAa,EAAE;CAE5C,OAAO,OAAO,OAAO,wBAAwB,EAAE,IAAI,uBAAuB,MAAM;AAClF"}
|
package/esm/model.mjs
CHANGED
|
@@ -108,7 +108,7 @@ var GoogleModel = class {
|
|
|
108
108
|
config: this.buildConfig(systemInstruction, options)
|
|
109
109
|
});
|
|
110
110
|
} catch (thrown) {
|
|
111
|
-
throw this.logAndWrap(thrown);
|
|
111
|
+
throw this.logAndWrap(thrown, options?.signal);
|
|
112
112
|
}
|
|
113
113
|
const toolCalls = this.extractToolCalls(response);
|
|
114
114
|
const finishReason = toolCalls ? "tool_calls" : mapFinishReason(response.candidates?.[0]?.finishReason);
|
|
@@ -149,7 +149,7 @@ var GoogleModel = class {
|
|
|
149
149
|
if (response.totalTokens === void 0) throw new InvalidRequestError("Google countTokens returned no totalTokens.");
|
|
150
150
|
return response.totalTokens;
|
|
151
151
|
} catch (thrown) {
|
|
152
|
-
throw this.logAndWrap(thrown);
|
|
152
|
+
throw this.logAndWrap(thrown, options?.signal);
|
|
153
153
|
}
|
|
154
154
|
}
|
|
155
155
|
/**
|
|
@@ -215,7 +215,7 @@ var GoogleModel = class {
|
|
|
215
215
|
config: this.buildConfig(systemInstruction, options)
|
|
216
216
|
});
|
|
217
217
|
} catch (thrown) {
|
|
218
|
-
throw this.logAndWrap(thrown);
|
|
218
|
+
throw this.logAndWrap(thrown, options?.signal);
|
|
219
219
|
}
|
|
220
220
|
let rawFinishReason;
|
|
221
221
|
let sawToolCall = false;
|
|
@@ -263,7 +263,7 @@ var GoogleModel = class {
|
|
|
263
263
|
}
|
|
264
264
|
}
|
|
265
265
|
} catch (thrown) {
|
|
266
|
-
throw this.logAndWrap(thrown);
|
|
266
|
+
throw this.logAndWrap(thrown, options?.signal);
|
|
267
267
|
}
|
|
268
268
|
const finishReason = sawToolCall ? "tool_calls" : mapFinishReason(rawFinishReason);
|
|
269
269
|
attachCost(usage, this.pricing);
|
|
@@ -412,8 +412,8 @@ var GoogleModel = class {
|
|
|
412
412
|
* Wrap a thrown provider error into the typed `AIError` hierarchy
|
|
413
413
|
* and emit the standard error log line before it propagates.
|
|
414
414
|
*/
|
|
415
|
-
logAndWrap(thrown) {
|
|
416
|
-
const wrapped = wrapGoogleError(thrown);
|
|
415
|
+
logAndWrap(thrown, signal) {
|
|
416
|
+
const wrapped = wrapGoogleError(thrown, { signal });
|
|
417
417
|
this.logger.error(LOG_MODULE, "error", wrapped.message, {
|
|
418
418
|
code: wrapped.code,
|
|
419
419
|
context: wrapped.context
|
package/esm/model.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.mjs","names":[],"sources":["../../../../../../../packages/ai-google/src/model.ts"],"sourcesContent":["import {\n attachCost,\n InvalidRequestError,\n resolveStreamChunks,\n selectProviderTools,\n type Message,\n type ModelCallOptions,\n type ModelCapabilities,\n type ModelContract,\n type ModelPricing,\n type ModelResponse,\n type ModelStreamChunk,\n type ModelToolCallRequest,\n type ReasoningEffort,\n type Usage,\n} from \"@warlock.js/ai\";\nimport { log, type Logger } from \"@warlock.js/logger\";\nimport type {\n GenerateContentConfig,\n GenerateContentResponse,\n GoogleGenAI,\n GroundingMetadata,\n Part,\n Tool,\n} from \"@google/genai\";\nimport { isSearchDefinition, searchBudgetOf } from \"./search-budget\";\nimport { applyGrounding, groundingMetadata } from \"./utils/grounding\";\nimport { mergeProviderBody } from \"./provider-options\";\nimport type { GoogleModelConfig } from \"./config.type\";\nimport { inferContextWindow } from \"./known-context-windows\";\nimport { inferVisionCapability } from \"./known-vision-models\";\nimport {\n applyGoogleUsage,\n mapFinishReason,\n toGoogleContents,\n toGoogleTools,\n wrapGoogleError,\n} from \"./utils\";\n\nconst LOG_MODULE = \"ai.google\";\n\n/** `provider` value of `ai.providerTool()` entries this adapter sends verbatim. */\nconst GOOGLE_WIRE_FORMAT = \"google\";\n\n/**\n * Bucketed `thinkingBudget` (token caps) for the neutral\n * `reasoning.effort` levels when the caller gives no explicit\n * `reasoning.maxTokens`. Gemini 2.5 accepts a positive budget as a cap\n * on the thinking phase; these mirror the spread the OpenAI\n * `reasoning_effort` low/medium/high tiers imply. The installed\n * `@google/genai` `ThinkingConfig` declaration documents that allowed\n * ranges are model-dependent, so retain the existing conservative 24,576\n * cap for the new tiers rather than sending a budget unsupported by Flash.\n */\nconst EFFORT_THINKING_BUDGET: Record<Exclude<ReasoningEffort, \"none\">, number> = {\n minimal: 1024,\n low: 1024,\n medium: 8192,\n high: 24576,\n xhigh: 24576,\n max: 24576,\n};\n\n/**\n * Google Gemini-backed implementation of `ModelContract`.\n *\n * **Role.** The provider-facing bridge between the vendor-neutral\n * `@warlock.js/ai` agent runtime and the `@google/genai` SDK\n * (`models.generateContent` / `generateContentStream`).\n *\n * **Responsibility.**\n * - Owns: a long-lived `GoogleGenAI` client + frozen `ModelConfig`\n * (name, temperature, maxTokens) used as per-call defaults.\n * - Owns: translating vendor-neutral `Message[]` / `ToolConfig[]` into\n * Gemini shapes (systemInstruction hoisting, `model` role,\n * `functionCall` / `functionResponse` parts, inline image bytes) on\n * the way out, and Gemini's candidate/parts response (text, function\n * calls, finish reason, token usage) back into neutral shapes on the\n * way in.\n * - Does NOT own: dispatching tools, looping, history, retries — those\n * are agent concerns. The model is a per-call protocol adapter.\n *\n * Modeled as a class (see §4.2 of code-style.md — \"long-lived state\n * across calls\"): the `GoogleGenAI` client is reused for the SDK's\n * lifetime.\n *\n * @example\n * import { GoogleGenAI } from \"@google/genai\";\n * const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });\n * const model = new GoogleModel(ai, { name: \"gemini-2.5-flash\" });\n *\n * const myAgent = agent({ model, tools: [searchTool] });\n * const result = await myAgent.execute(\"Summarize today's news.\");\n */\nexport class GoogleModel implements ModelContract {\n public readonly name: string;\n public readonly provider: string;\n public readonly capabilities: ModelCapabilities;\n public readonly pricing?: ModelPricing;\n\n private readonly ai: GoogleGenAI;\n private readonly config: GoogleModelConfig;\n private readonly logger: Logger = log;\n\n public constructor(ai: GoogleGenAI, config: GoogleModelConfig, provider: string = \"google\") {\n this.ai = ai;\n this.config = config;\n this.name = config.name;\n this.provider = provider;\n this.pricing = config.pricing;\n const multimodal = config.vision ?? inferVisionCapability(config.name);\n\n const window =\n config.contextWindow ?? (provider === \"google\" ? inferContextWindow(config.name) : undefined);\n\n this.capabilities = {\n ...(window !== undefined ? { contextWindow: window } : {}),\n structuredOutput: config.structuredOutput ?? true,\n vision: multimodal,\n // Function responses are JSON objects here; images fall back to text.\n toolResultImages: false,\n // Every Gemini 2.5 model thinks; older families harmlessly ignore\n // an empty thinking budget. Defaulting `true` lets the agent\n // forward reasoning options; an explicit `false` opts a model out.\n reasoning: config.reasoning ?? true,\n // Gemini reports cache-read hits (`cachedContentTokenCount`) on\n // every call via implicit caching, and accepts explicit context\n // caching. Read-side accounting is always honored.\n promptCaching: true,\n // The multimodal Gemini families that accept images also accept\n // audio and PDF/document parts. Mirror the vision inference unless\n // explicitly overridden.\n audio: config.audio ?? multimodal,\n pdf: config.pdf ?? multimodal,\n };\n }\n\n /**\n * Single-shot completion. Sends the full message list to\n * `generateContent`, waits for the terminal response, and reshapes\n * it into a vendor-neutral `ModelResponse`. Per-call `options`\n * override the instance defaults for this call only.\n */\n public async complete(messages: Message[], options?: ModelCallOptions): Promise<ModelResponse> {\n this.logger.debug(LOG_MODULE, \"request\", \"Starting generateContent call\", {\n model: this.name,\n messageCount: messages.length,\n streaming: false,\n toolCount: options?.tools?.length ?? 0,\n });\n\n const { systemInstruction, contents } = toGoogleContents(messages);\n\n let response: GenerateContentResponse;\n\n try {\n response = await this.ai.models.generateContent({\n model: this.name,\n contents,\n config: this.buildConfig(systemInstruction, options),\n });\n } catch (thrown) {\n throw this.logAndWrap(thrown);\n }\n\n const toolCalls = this.extractToolCalls(response);\n const finishReason = toolCalls\n ? \"tool_calls\"\n : mapFinishReason(response.candidates?.[0]?.finishReason);\n const usage = this.extractUsage(response);\n const grounding = new Map<number, GroundingMetadata>();\n applyGrounding(usage, grounding, response);\n searchBudgetOf(options)?.record(usage.serverToolUse?.webSearchRequests ?? 0);\n const providerMetadata = groundingMetadata(grounding);\n attachCost(usage, this.pricing);\n\n this.logger.debug(LOG_MODULE, \"response\", \"generateContent call succeeded\", {\n finishReason,\n usage,\n });\n\n return {\n ...(providerMetadata ? { providerMetadata } : {}),\n content: response.text ?? \"\",\n finishReason,\n usage,\n toolCalls,\n };\n }\n\n /** Count native input tokens with the same converted content, system and tools. */\n public async countTokens(messages: Message[], options?: ModelCallOptions): Promise<number> {\n const { systemInstruction, contents } = toGoogleContents(messages);\n const config = this.buildConfig(systemInstruction, options);\n if (config.cachedContent !== undefined) {\n throw new InvalidRequestError(\n \"The Google SDK countTokens endpoint cannot count a cachedContent reference.\",\n { reason: \"count-unsupported\" },\n );\n }\n try {\n const response = this.ai.vertexai\n ? await this.ai.models.countTokens({\n model: this.name,\n contents,\n config: {\n ...(config.systemInstruction !== undefined\n ? { systemInstruction: config.systemInstruction }\n : {}),\n ...(config.tools !== undefined ? { tools: config.tools as Tool[] } : {}),\n ...(config.abortSignal ? { abortSignal: config.abortSignal } : {}),\n ...(config.httpOptions ? { httpOptions: config.httpOptions } : {}),\n },\n })\n : await this.countDeveloperTokens(contents, config);\n if (response.totalTokens === undefined)\n throw new InvalidRequestError(\"Google countTokens returned no totalTokens.\");\n return response.totalTokens;\n } catch (thrown) {\n throw this.logAndWrap(thrown);\n }\n }\n\n /**\n * REST counts the full generation request, unlike the typed Developer API\n * converter in @google/genai 2.19.0. See https://ai.google.dev/api/tokens.\n * extraBody keeps the SDK's auth, fetch, base URL, retries and cancellation.\n */\n private countDeveloperTokens(\n contents: ReturnType<typeof toGoogleContents>[\"contents\"],\n config: GenerateContentConfig,\n ) {\n config.abortSignal?.throwIfAborted();\n\n const {\n systemInstruction,\n serviceTier,\n tools,\n toolConfig,\n safetySettings,\n cachedContent,\n abortSignal,\n httpOptions,\n ...generationConfig\n } = config;\n // Automatic function calling is SDK-only, never a REST generation field.\n delete generationConfig.automaticFunctionCalling;\n const model = this.name.startsWith(\"models/\") ? this.name : `models/${this.name}`;\n\n return this.ai.models.countTokens({\n model: this.name,\n contents,\n config: {\n abortSignal,\n httpOptions: {\n ...httpOptions,\n extraBody: {\n ...httpOptions?.extraBody,\n // The SDK merges extraBody before JSON serialization. undefined\n // removes its contents field: REST accepts only one input form.\n contents: undefined,\n generateContentRequest: {\n model,\n contents,\n ...(serviceTier !== undefined ? { serviceTier } : {}),\n ...(systemInstruction !== undefined\n ? { systemInstruction: { parts: [{ text: systemInstruction }] } }\n : {}),\n ...(tools !== undefined ? { tools } : {}),\n ...(toolConfig !== undefined ? { toolConfig } : {}),\n ...(safetySettings !== undefined ? { safetySettings } : {}),\n ...(cachedContent !== undefined ? { cachedContent } : {}),\n ...(Object.keys(generationConfig).length > 0 ? { generationConfig } : {}),\n },\n },\n },\n },\n });\n }\n\n /**\n * Incremental streaming completion via `generateContentStream`.\n * Yields neutral `ModelStreamChunk`s — `delta` for text, `tool-call`\n * per function call (Gemini emits a fully-formed call, not partial\n * JSON), and a terminal `done` with the final finish reason + usage.\n *\n * With `options.streamChunks` it also yields `reasoning` for thought\n * parts (only sent when `thinkingConfig.includeThoughts` is on — set it\n * through `providerOptions.google.body`) and `usage` whenever a chunk\n * carries `usageMetadata`. No `tool-args`: calls arrive whole.\n */\n public async *stream(\n messages: Message[],\n options?: ModelCallOptions,\n ): AsyncIterable<ModelStreamChunk> {\n this.logger.debug(LOG_MODULE, \"request\", \"Starting generateContentStream call\", {\n model: this.name,\n messageCount: messages.length,\n streaming: true,\n toolCount: options?.tools?.length ?? 0,\n });\n\n const { systemInstruction, contents } = toGoogleContents(messages);\n\n let iterable: AsyncGenerator<GenerateContentResponse>;\n\n try {\n iterable = await this.ai.models.generateContentStream({\n model: this.name,\n contents,\n config: this.buildConfig(systemInstruction, options),\n });\n } catch (thrown) {\n throw this.logAndWrap(thrown);\n }\n\n let rawFinishReason: string | undefined;\n let sawToolCall = false;\n const usage: Usage = { input: 0, output: 0, total: 0 };\n const want = resolveStreamChunks(options);\n const grounding = new Map<number, GroundingMetadata>();\n\n try {\n for await (const chunk of iterable) {\n applyGrounding(usage, grounding, chunk);\n // Thought parts (`thought: true`, sent when `thinkingConfig.includeThoughts`\n // is on) carry the thought summary in `text`; the SDK's `chunk.text`\n // getter already leaves them out of the answer.\n if (want.reasoning) {\n for (const part of chunk.candidates?.[0]?.content?.parts ?? []) {\n if (part.thought === true && typeof part.text === \"string\" && part.text !== \"\") {\n yield { type: \"reasoning\", delta: part.text };\n }\n }\n }\n\n const text = chunk.text;\n\n if (text) {\n yield { type: \"delta\", content: text };\n }\n\n for (const part of chunk.candidates?.[0]?.content?.parts ?? []) {\n const toolCall = this.partToToolCall(part);\n\n if (!toolCall) {\n continue;\n }\n\n sawToolCall = true;\n\n yield {\n type: \"tool-call\",\n id: toolCall.id,\n name: toolCall.name,\n input: toolCall.input,\n ...(toolCall.providerMetadata ? { providerMetadata: toolCall.providerMetadata } : {}),\n };\n }\n\n const candidateFinish = chunk.candidates?.[0]?.finishReason;\n\n if (candidateFinish) {\n rawFinishReason = candidateFinish;\n }\n\n if (chunk.usageMetadata) {\n applyGoogleUsage(usage, chunk.usageMetadata);\n\n if (want.usage) {\n yield { type: \"usage\", usage: { ...usage } };\n }\n }\n }\n } catch (thrown) {\n throw this.logAndWrap(thrown);\n }\n\n const finishReason = sawToolCall ? \"tool_calls\" : mapFinishReason(rawFinishReason);\n\n attachCost(usage, this.pricing);\n\n this.logger.debug(LOG_MODULE, \"response\", \"generateContentStream call succeeded\", {\n finishReason,\n usage,\n });\n\n searchBudgetOf(options)?.record(usage.serverToolUse?.webSearchRequests ?? 0);\n const providerMetadata = groundingMetadata(grounding);\n yield { type: \"done\", finishReason, usage, ...(providerMetadata ? { providerMetadata } : {}) };\n }\n\n /**\n * Assemble the `GenerateContentConfig` shared by `complete()` and\n * `stream()`: inference params, hoisted system instruction,\n * cancellation signal, and conditional tools + native structured\n * output.\n */\n private buildConfig(\n systemInstruction: string | undefined,\n options: ModelCallOptions | undefined,\n ): GenerateContentConfig {\n const temperature = options?.temperature ?? this.config.temperature;\n const maxOutputTokens = options?.maxTokens ?? this.config.maxTokens;\n\n return mergeProviderBody<GenerateContentConfig>(\n {\n ...(systemInstruction ? { systemInstruction } : {}),\n ...(temperature !== undefined ? { temperature } : {}),\n ...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}),\n ...(options?.signal ? { abortSignal: options.signal } : {}),\n ...this.buildTools(options),\n ...this.buildStructuredOutput(options?.responseSchema),\n ...this.buildThinking(options?.reasoning),\n },\n options,\n );\n }\n\n /**\n * Translate the neutral `reasoning` option into Gemini's\n * `thinkingConfig`. `reasoning.maxTokens` maps directly to\n * `thinkingBudget` (token cap on the thinking phase); when only\n * `reasoning.effort` is given it is bucketed into a budget. Emitted\n * only when the model is `reasoning`-capable — a `false` capability\n * (config override) drops it so a non-thinking model never receives\n * an unsupported `thinkingConfig`.\n *\n * Gemini's `thinkingBudget` semantics: `0` disables thinking, `-1`\n * lets the model decide automatically. A positive value caps the\n * thinking tokens. The neutral `effort: \"none\"` (\"run without\n * reasoning\") maps to `thinkingBudget: 0`.\n */\n private buildThinking(\n reasoning: ModelCallOptions[\"reasoning\"],\n ): Pick<GenerateContentConfig, \"thinkingConfig\"> {\n if (!reasoning || !this.capabilities.reasoning) {\n return {};\n }\n\n // `effort: \"none\"` = explicit \"run without reasoning\". Gemini disables\n // thinking with `thinkingBudget: 0` (its native off switch), so emit\n // that rather than omitting the config — an omitted config lets a\n // thinking model reason at its default budget.\n if (reasoning.effort === \"none\") {\n return { thinkingConfig: { thinkingBudget: 0 } };\n }\n\n const thinkingBudget =\n reasoning.maxTokens ??\n (reasoning.effort ? EFFORT_THINKING_BUDGET[reasoning.effort] : undefined);\n\n if (thinkingBudget === undefined) {\n return {};\n }\n\n return { thinkingConfig: { thinkingBudget } };\n }\n\n /**\n * Spread-friendly tools fragment. Empty object when no tools were\n * supplied so the caller can unconditionally spread it. Function tools\n * come first; `ai.providerTool({ provider: \"google\" })` definitions\n * (`{ googleSearch: {} }`, `{ codeExecution: {} }`, `{ urlContext: {} }`,\n * …) follow verbatim as their own `Tool` entries. Other providers'\n * definitions are skipped with a warning.\n */\n private buildTools(options: ModelCallOptions | undefined): Pick<GenerateContentConfig, \"tools\"> {\n const { tools, definitions } = selectProviderTools(options, {\n accepts: [GOOGLE_WIRE_FORMAT, this.provider],\n module: LOG_MODULE,\n model: this.name,\n logger: this.logger,\n });\n const budget = searchBudgetOf(options);\n // The Gemini API cannot bound searches inside one call, so a spent budget\n // is enforced here by not offering Google Search again.\n const offered =\n budget && budget.remaining <= 0\n ? definitions.filter((definition) => !isSearchDefinition(definition))\n : definitions;\n\n if (offered.length < definitions.length) {\n this.logger.warn(\n LOG_MODULE,\n \"search-budget\",\n \"Google Search budget is spent; calling without the search tool\",\n { model: this.name, max: budget?.max, used: budget?.used },\n );\n }\n\n const mapped: Tool[] = [...(toGoogleTools(tools) ?? []), ...(offered as unknown as Tool[])];\n\n return mapped.length > 0 ? { tools: mapped } : {};\n }\n\n /**\n * Translate the neutral `responseSchema` into Gemini's native JSON\n * structured output (`responseMimeType: \"application/json\"` +\n * `responseJsonSchema`, which takes a raw JSON Schema directly).\n * Emitted only when the model is `structuredOutput`-capable and the\n * schema is an object root — otherwise the agent's soft prompt hint\n * + client-side `validate()` carry shape.\n */\n private buildStructuredOutput(\n responseSchema: Record<string, unknown> | undefined,\n ): Pick<GenerateContentConfig, \"responseMimeType\" | \"responseJsonSchema\"> {\n if (!responseSchema || !this.capabilities.structuredOutput) {\n return {};\n }\n\n if (responseSchema.type !== \"object\" || typeof responseSchema.properties !== \"object\") {\n return {};\n }\n\n return {\n responseMimeType: \"application/json\",\n responseJsonSchema: responseSchema,\n };\n }\n\n /**\n * Reshape Gemini's function-call content parts into the neutral\n * `ModelToolCallRequest[]`. Returns `undefined` when the model\n * requested no functions so callers can branch on presence.\n *\n * Reads `candidates[0].content.parts` directly rather than the\n * `response.functionCalls` getter: the getter discards the\n * part-level `thoughtSignature`, and Gemini \"thinking\" models 400\n * the follow-up turn if that signature is not echoed back. See\n * `partToToolCall`.\n */\n private extractToolCalls(response: GenerateContentResponse): ModelToolCallRequest[] | undefined {\n const parts = response.candidates?.[0]?.content?.parts ?? [];\n const toolCalls = parts\n .map((part) => this.partToToolCall(part))\n .filter((call): call is ModelToolCallRequest => call !== undefined);\n\n return toolCalls.length > 0 ? toolCalls : undefined;\n }\n\n /**\n * Map a single Gemini `Part` to a neutral `ModelToolCallRequest`,\n * or `undefined` when the part is not a function call. The part's\n * `thoughtSignature` (opaque, set by thinking models) is carried on\n * `providerMetadata` so `toGoogleContents` can replay it on the\n * assistant turn — Gemini rejects the next request without it.\n */\n private partToToolCall(part: Part): ModelToolCallRequest | undefined {\n if (!part.functionCall) {\n return undefined;\n }\n\n const call = part.functionCall;\n\n return {\n // The Gemini Developer API does not assign function-call ids\n // (only Vertex parallel-calling does). Fall back to the function\n // name so the neutral `toolCallId` is non-empty and the echoed\n // `functionResponse.name` resolves — Gemini matches a result to\n // its call by name. See decisions §49.\n id: call.id ?? call.name ?? \"\",\n name: call.name ?? \"\",\n input: (call.args ?? {}) as Record<string, unknown>,\n ...(part.thoughtSignature\n ? { providerMetadata: { thoughtSignature: part.thoughtSignature } }\n : {}),\n };\n }\n\n /**\n * Normalize Gemini's `usageMetadata` into the neutral `Usage` shape\n * via the shared {@link applyGoogleUsage} mapper (the same one the\n * streaming loop and the Gemini image model use). Absent usage\n * collapses to zeros.\n */\n private extractUsage(response: GenerateContentResponse): Usage {\n const usage: Usage = { input: 0, output: 0, total: 0 };\n\n if (response.usageMetadata) {\n applyGoogleUsage(usage, response.usageMetadata);\n }\n\n return usage;\n }\n\n /**\n * Wrap a thrown provider error into the typed `AIError` hierarchy\n * and emit the standard error log line before it propagates.\n */\n private logAndWrap(thrown: unknown) {\n const wrapped = wrapGoogleError(thrown);\n\n this.logger.error(LOG_MODULE, \"error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n return wrapped;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAuCA,MAAM,aAAa;;AAGnB,MAAM,qBAAqB;;;;;;;;;;;AAY3B,MAAM,yBAA2E;CAC/E,SAAS;CACT,KAAK;CACL,QAAQ;CACR,MAAM;CACN,OAAO;CACP,KAAK;AACP;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA,IAAa,cAAb,MAAkD;CAUhD,AAAO,YAAY,IAAiB,QAA2B,WAAmB,UAAU;gBAF1D;EAGhC,KAAK,KAAK;EACV,KAAK,SAAS;EACd,KAAK,OAAO,OAAO;EACnB,KAAK,WAAW;EAChB,KAAK,UAAU,OAAO;EACtB,MAAM,aAAa,OAAO,UAAU,sBAAsB,OAAO,IAAI;EAErE,MAAM,SACJ,OAAO,kBAAkB,aAAa,WAAW,mBAAmB,OAAO,IAAI,IAAI;EA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ACxC,OAAO,CAAC;EAGV,IAAI,eAAe,SAAS,YAAY,OAAO,eAAe,eAAe,UAC3E,OAAO,CAAC;EAGV,OAAO;GACL,kBAAkB;GAClB,oBAAoB;EACtB;CACF;;;;;;;;;;;;CAaA,AAAQ,iBAAiB,UAAuE;EAE9F,MAAM,aADQ,SAAS,aAAa,EAAE,EAAE,SAAS,SAAS,CAAC,EACpC,CACpB,KAAK,SAAS,KAAK,eAAe,IAAI,CAAC,CAAC,CACxC,QAAQ,SAAuC,SAAS,MAAS;EAEpE,OAAO,UAAU,SAAS,IAAI,YAAY;CAC5C;;;;;;;;CASA,AAAQ,eAAe,MAA8C;EACnE,IAAI,CAAC,KAAK,cACR;EAGF,MAAM,OAAO,KAAK;EAElB,OAAO;GAML,IAAI,KAAK,MAAM,KAAK,QAAQ;GAC5B,MAAM,KAAK,QAAQ;GACnB,OAAQ,KAAK,QAAQ,CAAC;GACtB,GAAI,KAAK,mBACL,EAAE,kBAAkB,EAAE,kBAAkB,KAAK,iBAAiB,EAAE,IAChE,CAAC;EACP;CACF;;;;;;;CAQA,AAAQ,aAAa,UAA0C;EAC7D,MAAM,QAAe;GAAE,OAAO;GAAG,QAAQ;GAAG,OAAO;EAAE;EAErD,IAAI,SAAS,eACX,iBAAiB,OAAO,SAAS,aAAa;EAGhD,OAAO;CACT;;;;;CAMA,AAAQ,WAAW,QAAiB;EAClC,MAAM,UAAU,gBAAgB,MAAM;EAEtC,KAAK,OAAO,MAAM,YAAY,SAAS,QAAQ,SAAS;GACtD,MAAM,QAAQ;GACd,SAAS,QAAQ;EACnB,CAAC;EAED,OAAO;CACT;AACF"}
|
|
1
|
+
{"version":3,"file":"model.mjs","names":[],"sources":["../../../../../../../packages/ai-google/src/model.ts"],"sourcesContent":["import {\n attachCost,\n InvalidRequestError,\n resolveStreamChunks,\n selectProviderTools,\n type Message,\n type ModelCallOptions,\n type ModelCapabilities,\n type ModelContract,\n type ModelPricing,\n type ModelResponse,\n type ModelStreamChunk,\n type ModelToolCallRequest,\n type ReasoningEffort,\n type Usage,\n} from \"@warlock.js/ai\";\nimport { log, type Logger } from \"@warlock.js/logger\";\nimport type {\n GenerateContentConfig,\n GenerateContentResponse,\n GoogleGenAI,\n GroundingMetadata,\n Part,\n Tool,\n} from \"@google/genai\";\nimport { isSearchDefinition, searchBudgetOf } from \"./search-budget\";\nimport { applyGrounding, groundingMetadata } from \"./utils/grounding\";\nimport { mergeProviderBody } from \"./provider-options\";\nimport type { GoogleModelConfig } from \"./config.type\";\nimport { inferContextWindow } from \"./known-context-windows\";\nimport { inferVisionCapability } from \"./known-vision-models\";\nimport {\n applyGoogleUsage,\n mapFinishReason,\n toGoogleContents,\n toGoogleTools,\n wrapGoogleError,\n} from \"./utils\";\n\nconst LOG_MODULE = \"ai.google\";\n\n/** `provider` value of `ai.providerTool()` entries this adapter sends verbatim. */\nconst GOOGLE_WIRE_FORMAT = \"google\";\n\n/**\n * Bucketed `thinkingBudget` (token caps) for the neutral\n * `reasoning.effort` levels when the caller gives no explicit\n * `reasoning.maxTokens`. Gemini 2.5 accepts a positive budget as a cap\n * on the thinking phase; these mirror the spread the OpenAI\n * `reasoning_effort` low/medium/high tiers imply. The installed\n * `@google/genai` `ThinkingConfig` declaration documents that allowed\n * ranges are model-dependent, so retain the existing conservative 24,576\n * cap for the new tiers rather than sending a budget unsupported by Flash.\n */\nconst EFFORT_THINKING_BUDGET: Record<Exclude<ReasoningEffort, \"none\">, number> = {\n minimal: 1024,\n low: 1024,\n medium: 8192,\n high: 24576,\n xhigh: 24576,\n max: 24576,\n};\n\n/**\n * Google Gemini-backed implementation of `ModelContract`.\n *\n * **Role.** The provider-facing bridge between the vendor-neutral\n * `@warlock.js/ai` agent runtime and the `@google/genai` SDK\n * (`models.generateContent` / `generateContentStream`).\n *\n * **Responsibility.**\n * - Owns: a long-lived `GoogleGenAI` client + frozen `ModelConfig`\n * (name, temperature, maxTokens) used as per-call defaults.\n * - Owns: translating vendor-neutral `Message[]` / `ToolConfig[]` into\n * Gemini shapes (systemInstruction hoisting, `model` role,\n * `functionCall` / `functionResponse` parts, inline image bytes) on\n * the way out, and Gemini's candidate/parts response (text, function\n * calls, finish reason, token usage) back into neutral shapes on the\n * way in.\n * - Does NOT own: dispatching tools, looping, history, retries — those\n * are agent concerns. The model is a per-call protocol adapter.\n *\n * Modeled as a class (see §4.2 of code-style.md — \"long-lived state\n * across calls\"): the `GoogleGenAI` client is reused for the SDK's\n * lifetime.\n *\n * @example\n * import { GoogleGenAI } from \"@google/genai\";\n * const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });\n * const model = new GoogleModel(ai, { name: \"gemini-2.5-flash\" });\n *\n * const myAgent = agent({ model, tools: [searchTool] });\n * const result = await myAgent.execute(\"Summarize today's news.\");\n */\nexport class GoogleModel implements ModelContract {\n public readonly name: string;\n public readonly provider: string;\n public readonly capabilities: ModelCapabilities;\n public readonly pricing?: ModelPricing;\n\n private readonly ai: GoogleGenAI;\n private readonly config: GoogleModelConfig;\n private readonly logger: Logger = log;\n\n public constructor(ai: GoogleGenAI, config: GoogleModelConfig, provider: string = \"google\") {\n this.ai = ai;\n this.config = config;\n this.name = config.name;\n this.provider = provider;\n this.pricing = config.pricing;\n const multimodal = config.vision ?? inferVisionCapability(config.name);\n\n const window =\n config.contextWindow ?? (provider === \"google\" ? inferContextWindow(config.name) : undefined);\n\n this.capabilities = {\n ...(window !== undefined ? { contextWindow: window } : {}),\n structuredOutput: config.structuredOutput ?? true,\n vision: multimodal,\n // Function responses are JSON objects here; images fall back to text.\n toolResultImages: false,\n // Every Gemini 2.5 model thinks; older families harmlessly ignore\n // an empty thinking budget. Defaulting `true` lets the agent\n // forward reasoning options; an explicit `false` opts a model out.\n reasoning: config.reasoning ?? true,\n // Gemini reports cache-read hits (`cachedContentTokenCount`) on\n // every call via implicit caching, and accepts explicit context\n // caching. Read-side accounting is always honored.\n promptCaching: true,\n // The multimodal Gemini families that accept images also accept\n // audio and PDF/document parts. Mirror the vision inference unless\n // explicitly overridden.\n audio: config.audio ?? multimodal,\n pdf: config.pdf ?? multimodal,\n };\n }\n\n /**\n * Single-shot completion. Sends the full message list to\n * `generateContent`, waits for the terminal response, and reshapes\n * it into a vendor-neutral `ModelResponse`. Per-call `options`\n * override the instance defaults for this call only.\n */\n public async complete(messages: Message[], options?: ModelCallOptions): Promise<ModelResponse> {\n this.logger.debug(LOG_MODULE, \"request\", \"Starting generateContent call\", {\n model: this.name,\n messageCount: messages.length,\n streaming: false,\n toolCount: options?.tools?.length ?? 0,\n });\n\n const { systemInstruction, contents } = toGoogleContents(messages);\n\n let response: GenerateContentResponse;\n\n try {\n response = await this.ai.models.generateContent({\n model: this.name,\n contents,\n config: this.buildConfig(systemInstruction, options),\n });\n } catch (thrown) {\n throw this.logAndWrap(thrown, options?.signal);\n }\n\n const toolCalls = this.extractToolCalls(response);\n const finishReason = toolCalls\n ? \"tool_calls\"\n : mapFinishReason(response.candidates?.[0]?.finishReason);\n const usage = this.extractUsage(response);\n const grounding = new Map<number, GroundingMetadata>();\n applyGrounding(usage, grounding, response);\n searchBudgetOf(options)?.record(usage.serverToolUse?.webSearchRequests ?? 0);\n const providerMetadata = groundingMetadata(grounding);\n attachCost(usage, this.pricing);\n\n this.logger.debug(LOG_MODULE, \"response\", \"generateContent call succeeded\", {\n finishReason,\n usage,\n });\n\n return {\n ...(providerMetadata ? { providerMetadata } : {}),\n content: response.text ?? \"\",\n finishReason,\n usage,\n toolCalls,\n };\n }\n\n /** Count native input tokens with the same converted content, system and tools. */\n public async countTokens(messages: Message[], options?: ModelCallOptions): Promise<number> {\n const { systemInstruction, contents } = toGoogleContents(messages);\n const config = this.buildConfig(systemInstruction, options);\n if (config.cachedContent !== undefined) {\n throw new InvalidRequestError(\n \"The Google SDK countTokens endpoint cannot count a cachedContent reference.\",\n { reason: \"count-unsupported\" },\n );\n }\n try {\n const response = this.ai.vertexai\n ? await this.ai.models.countTokens({\n model: this.name,\n contents,\n config: {\n ...(config.systemInstruction !== undefined\n ? { systemInstruction: config.systemInstruction }\n : {}),\n ...(config.tools !== undefined ? { tools: config.tools as Tool[] } : {}),\n ...(config.abortSignal ? { abortSignal: config.abortSignal } : {}),\n ...(config.httpOptions ? { httpOptions: config.httpOptions } : {}),\n },\n })\n : await this.countDeveloperTokens(contents, config);\n if (response.totalTokens === undefined)\n throw new InvalidRequestError(\"Google countTokens returned no totalTokens.\");\n return response.totalTokens;\n } catch (thrown) {\n throw this.logAndWrap(thrown, options?.signal);\n }\n }\n\n /**\n * REST counts the full generation request, unlike the typed Developer API\n * converter in @google/genai 2.19.0. See https://ai.google.dev/api/tokens.\n * extraBody keeps the SDK's auth, fetch, base URL, retries and cancellation.\n */\n private countDeveloperTokens(\n contents: ReturnType<typeof toGoogleContents>[\"contents\"],\n config: GenerateContentConfig,\n ) {\n config.abortSignal?.throwIfAborted();\n\n const {\n systemInstruction,\n serviceTier,\n tools,\n toolConfig,\n safetySettings,\n cachedContent,\n abortSignal,\n httpOptions,\n ...generationConfig\n } = config;\n // Automatic function calling is SDK-only, never a REST generation field.\n delete generationConfig.automaticFunctionCalling;\n const model = this.name.startsWith(\"models/\") ? this.name : `models/${this.name}`;\n\n return this.ai.models.countTokens({\n model: this.name,\n contents,\n config: {\n abortSignal,\n httpOptions: {\n ...httpOptions,\n extraBody: {\n ...httpOptions?.extraBody,\n // The SDK merges extraBody before JSON serialization. undefined\n // removes its contents field: REST accepts only one input form.\n contents: undefined,\n generateContentRequest: {\n model,\n contents,\n ...(serviceTier !== undefined ? { serviceTier } : {}),\n ...(systemInstruction !== undefined\n ? { systemInstruction: { parts: [{ text: systemInstruction }] } }\n : {}),\n ...(tools !== undefined ? { tools } : {}),\n ...(toolConfig !== undefined ? { toolConfig } : {}),\n ...(safetySettings !== undefined ? { safetySettings } : {}),\n ...(cachedContent !== undefined ? { cachedContent } : {}),\n ...(Object.keys(generationConfig).length > 0 ? { generationConfig } : {}),\n },\n },\n },\n },\n });\n }\n\n /**\n * Incremental streaming completion via `generateContentStream`.\n * Yields neutral `ModelStreamChunk`s — `delta` for text, `tool-call`\n * per function call (Gemini emits a fully-formed call, not partial\n * JSON), and a terminal `done` with the final finish reason + usage.\n *\n * With `options.streamChunks` it also yields `reasoning` for thought\n * parts (only sent when `thinkingConfig.includeThoughts` is on — set it\n * through `providerOptions.google.body`) and `usage` whenever a chunk\n * carries `usageMetadata`. No `tool-args`: calls arrive whole.\n */\n public async *stream(\n messages: Message[],\n options?: ModelCallOptions,\n ): AsyncIterable<ModelStreamChunk> {\n this.logger.debug(LOG_MODULE, \"request\", \"Starting generateContentStream call\", {\n model: this.name,\n messageCount: messages.length,\n streaming: true,\n toolCount: options?.tools?.length ?? 0,\n });\n\n const { systemInstruction, contents } = toGoogleContents(messages);\n\n let iterable: AsyncGenerator<GenerateContentResponse>;\n\n try {\n iterable = await this.ai.models.generateContentStream({\n model: this.name,\n contents,\n config: this.buildConfig(systemInstruction, options),\n });\n } catch (thrown) {\n throw this.logAndWrap(thrown, options?.signal);\n }\n\n let rawFinishReason: string | undefined;\n let sawToolCall = false;\n const usage: Usage = { input: 0, output: 0, total: 0 };\n const want = resolveStreamChunks(options);\n const grounding = new Map<number, GroundingMetadata>();\n\n try {\n for await (const chunk of iterable) {\n applyGrounding(usage, grounding, chunk);\n // Thought parts (`thought: true`, sent when `thinkingConfig.includeThoughts`\n // is on) carry the thought summary in `text`; the SDK's `chunk.text`\n // getter already leaves them out of the answer.\n if (want.reasoning) {\n for (const part of chunk.candidates?.[0]?.content?.parts ?? []) {\n if (part.thought === true && typeof part.text === \"string\" && part.text !== \"\") {\n yield { type: \"reasoning\", delta: part.text };\n }\n }\n }\n\n const text = chunk.text;\n\n if (text) {\n yield { type: \"delta\", content: text };\n }\n\n for (const part of chunk.candidates?.[0]?.content?.parts ?? []) {\n const toolCall = this.partToToolCall(part);\n\n if (!toolCall) {\n continue;\n }\n\n sawToolCall = true;\n\n yield {\n type: \"tool-call\",\n id: toolCall.id,\n name: toolCall.name,\n input: toolCall.input,\n ...(toolCall.providerMetadata ? { providerMetadata: toolCall.providerMetadata } : {}),\n };\n }\n\n const candidateFinish = chunk.candidates?.[0]?.finishReason;\n\n if (candidateFinish) {\n rawFinishReason = candidateFinish;\n }\n\n if (chunk.usageMetadata) {\n applyGoogleUsage(usage, chunk.usageMetadata);\n\n if (want.usage) {\n yield { type: \"usage\", usage: { ...usage } };\n }\n }\n }\n } catch (thrown) {\n throw this.logAndWrap(thrown, options?.signal);\n }\n\n const finishReason = sawToolCall ? \"tool_calls\" : mapFinishReason(rawFinishReason);\n\n attachCost(usage, this.pricing);\n\n this.logger.debug(LOG_MODULE, \"response\", \"generateContentStream call succeeded\", {\n finishReason,\n usage,\n });\n\n searchBudgetOf(options)?.record(usage.serverToolUse?.webSearchRequests ?? 0);\n const providerMetadata = groundingMetadata(grounding);\n yield { type: \"done\", finishReason, usage, ...(providerMetadata ? { providerMetadata } : {}) };\n }\n\n /**\n * Assemble the `GenerateContentConfig` shared by `complete()` and\n * `stream()`: inference params, hoisted system instruction,\n * cancellation signal, and conditional tools + native structured\n * output.\n */\n private buildConfig(\n systemInstruction: string | undefined,\n options: ModelCallOptions | undefined,\n ): GenerateContentConfig {\n const temperature = options?.temperature ?? this.config.temperature;\n const maxOutputTokens = options?.maxTokens ?? this.config.maxTokens;\n\n return mergeProviderBody<GenerateContentConfig>(\n {\n ...(systemInstruction ? { systemInstruction } : {}),\n ...(temperature !== undefined ? { temperature } : {}),\n ...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}),\n ...(options?.signal ? { abortSignal: options.signal } : {}),\n ...this.buildTools(options),\n ...this.buildStructuredOutput(options?.responseSchema),\n ...this.buildThinking(options?.reasoning),\n },\n options,\n );\n }\n\n /**\n * Translate the neutral `reasoning` option into Gemini's\n * `thinkingConfig`. `reasoning.maxTokens` maps directly to\n * `thinkingBudget` (token cap on the thinking phase); when only\n * `reasoning.effort` is given it is bucketed into a budget. Emitted\n * only when the model is `reasoning`-capable — a `false` capability\n * (config override) drops it so a non-thinking model never receives\n * an unsupported `thinkingConfig`.\n *\n * Gemini's `thinkingBudget` semantics: `0` disables thinking, `-1`\n * lets the model decide automatically. A positive value caps the\n * thinking tokens. The neutral `effort: \"none\"` (\"run without\n * reasoning\") maps to `thinkingBudget: 0`.\n */\n private buildThinking(\n reasoning: ModelCallOptions[\"reasoning\"],\n ): Pick<GenerateContentConfig, \"thinkingConfig\"> {\n if (!reasoning || !this.capabilities.reasoning) {\n return {};\n }\n\n // `effort: \"none\"` = explicit \"run without reasoning\". Gemini disables\n // thinking with `thinkingBudget: 0` (its native off switch), so emit\n // that rather than omitting the config — an omitted config lets a\n // thinking model reason at its default budget.\n if (reasoning.effort === \"none\") {\n return { thinkingConfig: { thinkingBudget: 0 } };\n }\n\n const thinkingBudget =\n reasoning.maxTokens ??\n (reasoning.effort ? EFFORT_THINKING_BUDGET[reasoning.effort] : undefined);\n\n if (thinkingBudget === undefined) {\n return {};\n }\n\n return { thinkingConfig: { thinkingBudget } };\n }\n\n /**\n * Spread-friendly tools fragment. Empty object when no tools were\n * supplied so the caller can unconditionally spread it. Function tools\n * come first; `ai.providerTool({ provider: \"google\" })` definitions\n * (`{ googleSearch: {} }`, `{ codeExecution: {} }`, `{ urlContext: {} }`,\n * …) follow verbatim as their own `Tool` entries. Other providers'\n * definitions are skipped with a warning.\n */\n private buildTools(options: ModelCallOptions | undefined): Pick<GenerateContentConfig, \"tools\"> {\n const { tools, definitions } = selectProviderTools(options, {\n accepts: [GOOGLE_WIRE_FORMAT, this.provider],\n module: LOG_MODULE,\n model: this.name,\n logger: this.logger,\n });\n const budget = searchBudgetOf(options);\n // The Gemini API cannot bound searches inside one call, so a spent budget\n // is enforced here by not offering Google Search again.\n const offered =\n budget && budget.remaining <= 0\n ? definitions.filter((definition) => !isSearchDefinition(definition))\n : definitions;\n\n if (offered.length < definitions.length) {\n this.logger.warn(\n LOG_MODULE,\n \"search-budget\",\n \"Google Search budget is spent; calling without the search tool\",\n { model: this.name, max: budget?.max, used: budget?.used },\n );\n }\n\n const mapped: Tool[] = [...(toGoogleTools(tools) ?? []), ...(offered as unknown as Tool[])];\n\n return mapped.length > 0 ? { tools: mapped } : {};\n }\n\n /**\n * Translate the neutral `responseSchema` into Gemini's native JSON\n * structured output (`responseMimeType: \"application/json\"` +\n * `responseJsonSchema`, which takes a raw JSON Schema directly).\n * Emitted only when the model is `structuredOutput`-capable and the\n * schema is an object root — otherwise the agent's soft prompt hint\n * + client-side `validate()` carry shape.\n */\n private buildStructuredOutput(\n responseSchema: Record<string, unknown> | undefined,\n ): Pick<GenerateContentConfig, \"responseMimeType\" | \"responseJsonSchema\"> {\n if (!responseSchema || !this.capabilities.structuredOutput) {\n return {};\n }\n\n if (responseSchema.type !== \"object\" || typeof responseSchema.properties !== \"object\") {\n return {};\n }\n\n return {\n responseMimeType: \"application/json\",\n responseJsonSchema: responseSchema,\n };\n }\n\n /**\n * Reshape Gemini's function-call content parts into the neutral\n * `ModelToolCallRequest[]`. Returns `undefined` when the model\n * requested no functions so callers can branch on presence.\n *\n * Reads `candidates[0].content.parts` directly rather than the\n * `response.functionCalls` getter: the getter discards the\n * part-level `thoughtSignature`, and Gemini \"thinking\" models 400\n * the follow-up turn if that signature is not echoed back. See\n * `partToToolCall`.\n */\n private extractToolCalls(response: GenerateContentResponse): ModelToolCallRequest[] | undefined {\n const parts = response.candidates?.[0]?.content?.parts ?? [];\n const toolCalls = parts\n .map((part) => this.partToToolCall(part))\n .filter((call): call is ModelToolCallRequest => call !== undefined);\n\n return toolCalls.length > 0 ? toolCalls : undefined;\n }\n\n /**\n * Map a single Gemini `Part` to a neutral `ModelToolCallRequest`,\n * or `undefined` when the part is not a function call. The part's\n * `thoughtSignature` (opaque, set by thinking models) is carried on\n * `providerMetadata` so `toGoogleContents` can replay it on the\n * assistant turn — Gemini rejects the next request without it.\n */\n private partToToolCall(part: Part): ModelToolCallRequest | undefined {\n if (!part.functionCall) {\n return undefined;\n }\n\n const call = part.functionCall;\n\n return {\n // The Gemini Developer API does not assign function-call ids\n // (only Vertex parallel-calling does). Fall back to the function\n // name so the neutral `toolCallId` is non-empty and the echoed\n // `functionResponse.name` resolves — Gemini matches a result to\n // its call by name. See decisions §49.\n id: call.id ?? call.name ?? \"\",\n name: call.name ?? \"\",\n input: (call.args ?? {}) as Record<string, unknown>,\n ...(part.thoughtSignature\n ? { providerMetadata: { thoughtSignature: part.thoughtSignature } }\n : {}),\n };\n }\n\n /**\n * Normalize Gemini's `usageMetadata` into the neutral `Usage` shape\n * via the shared {@link applyGoogleUsage} mapper (the same one the\n * streaming loop and the Gemini image model use). Absent usage\n * collapses to zeros.\n */\n private extractUsage(response: GenerateContentResponse): Usage {\n const usage: Usage = { input: 0, output: 0, total: 0 };\n\n if (response.usageMetadata) {\n applyGoogleUsage(usage, response.usageMetadata);\n }\n\n return usage;\n }\n\n /**\n * Wrap a thrown provider error into the typed `AIError` hierarchy\n * and emit the standard error log line before it propagates.\n */\n private logAndWrap(thrown: unknown, signal?: AbortSignal) {\n const wrapped = wrapGoogleError(thrown, { signal });\n\n this.logger.error(LOG_MODULE, \"error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n return wrapped;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAuCA,MAAM,aAAa;;AAGnB,MAAM,qBAAqB;;;;;;;;;;;AAY3B,MAAM,yBAA2E;CAC/E,SAAS;CACT,KAAK;CACL,QAAQ;CACR,MAAM;CACN,OAAO;CACP,KAAK;AACP;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA,IAAa,cAAb,MAAkD;CAUhD,AAAO,YAAY,IAAiB,QAA2B,WAAmB,UAAU;gBAF1D;EAGhC,KAAK,KAAK;EACV,KAAK,SAAS;EACd,KAAK,OAAO,OAAO;EACnB,KAAK,WAAW;EAChB,KAAK,UAAU,OAAO;EACtB,MAAM,aAAa,OAAO,UAAU,sBAAsB,OAAO,IAAI;EAErE,MAAM,SACJ,OAAO,kBAAkB,aAAa,WAAW,mB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;;;;;CAUA,AAAQ,sBACN,gBACwE;EACxE,IAAI,CAAC,kBAAkB,CAAC,KAAK,aAAa,kBACxC,OAAO,CAAC;EAGV,IAAI,eAAe,SAAS,YAAY,OAAO,eAAe,eAAe,UAC3E,OAAO,CAAC;EAGV,OAAO;GACL,kBAAkB;GAClB,oBAAoB;EACtB;CACF;;;;;;;;;;;;CAaA,AAAQ,iBAAiB,UAAuE;EAE9F,MAAM,aADQ,SAAS,aAAa,EAAE,EAAE,SAAS,SAAS,CAAC,EACpC,CACpB,KAAK,SAAS,KAAK,eAAe,IAAI,CAAC,CAAC,CACxC,QAAQ,SAAuC,SAAS,MAAS;EAEpE,OAAO,UAAU,SAAS,IAAI,YAAY;CAC5C;;;;;;;;CASA,AAAQ,eAAe,MAA8C;EACnE,IAAI,CAAC,KAAK,cACR;EAGF,MAAM,OAAO,KAAK;EAElB,OAAO;GAML,IAAI,KAAK,MAAM,KAAK,QAAQ;GAC5B,MAAM,KAAK,QAAQ;GACnB,OAAQ,KAAK,QAAQ,CAAC;GACtB,GAAI,KAAK,mBACL,EAAE,kBAAkB,EAAE,kBAAkB,KAAK,iBAAiB,EAAE,IAChE,CAAC;EACP;CACF;;;;;;;CAQA,AAAQ,aAAa,UAA0C;EAC7D,MAAM,QAAe;GAAE,OAAO;GAAG,QAAQ;GAAG,OAAO;EAAE;EAErD,IAAI,SAAS,eACX,iBAAiB,OAAO,SAAS,aAAa;EAGhD,OAAO;CACT;;;;;CAMA,AAAQ,WAAW,QAAiB,QAAsB;EACxD,MAAM,UAAU,gBAAgB,QAAQ,EAAE,OAAO,CAAC;EAElD,KAAK,OAAO,MAAM,YAAY,SAAS,QAAQ,SAAS;GACtD,MAAM,QAAQ;GACd,SAAS,QAAQ;EACnB,CAAC;EAED,OAAO;CACT;AACF"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ApiError } from "@google/genai";
|
|
2
|
-
import { AIError, ContextLengthExceededError, InvalidRequestError, ProviderAuthError, ProviderError, ProviderOverloadedError, ProviderRateLimitError, ProviderTimeoutError } from "@warlock.js/ai";
|
|
2
|
+
import { AIError, ContextLengthExceededError, InvalidRequestError, ProviderAbortError, ProviderAuthError, ProviderError, ProviderOverloadedError, ProviderRateLimitError, ProviderTimeoutError, QuotaExceededError } from "@warlock.js/ai";
|
|
3
3
|
|
|
4
4
|
//#region ../../packages/ai-google/src/utils/wrap-google-error.ts
|
|
5
5
|
/**
|
|
@@ -23,12 +23,17 @@ import { AIError, ContextLengthExceededError, InvalidRequestError, ProviderAuthE
|
|
|
23
23
|
* throw wrapGoogleError(thrown);
|
|
24
24
|
* }
|
|
25
25
|
*/
|
|
26
|
-
function wrapGoogleError(thrown) {
|
|
26
|
+
function wrapGoogleError(thrown, options = {}) {
|
|
27
27
|
if (thrown instanceof AIError) return thrown;
|
|
28
28
|
const shape = toShape(thrown);
|
|
29
29
|
const context = buildContext(shape);
|
|
30
30
|
const message = shape.message ?? (thrown instanceof Error ? thrown.message : String(thrown));
|
|
31
31
|
const billed = shape.status === 400 || shape.status === 500 ? false : void 0;
|
|
32
|
+
if (shape.name === "AbortError" && options.signal?.aborted === true) return new ProviderAbortError(message, {
|
|
33
|
+
cause: thrown,
|
|
34
|
+
context,
|
|
35
|
+
billed
|
|
36
|
+
});
|
|
32
37
|
if (isTimeout(shape)) return new ProviderTimeoutError(message, {
|
|
33
38
|
cause: thrown,
|
|
34
39
|
context,
|
|
@@ -39,6 +44,11 @@ function wrapGoogleError(thrown) {
|
|
|
39
44
|
context,
|
|
40
45
|
billed
|
|
41
46
|
});
|
|
47
|
+
if (isSpentQuota(message)) return new QuotaExceededError(message, {
|
|
48
|
+
cause: thrown,
|
|
49
|
+
context,
|
|
50
|
+
billed
|
|
51
|
+
});
|
|
42
52
|
if (shape.status === 429 || /resource_exhausted|quota/i.test(message)) return new ProviderRateLimitError(message, {
|
|
43
53
|
cause: thrown,
|
|
44
54
|
context,
|
|
@@ -73,6 +83,17 @@ function wrapGoogleError(thrown) {
|
|
|
73
83
|
});
|
|
74
84
|
}
|
|
75
85
|
/**
|
|
86
|
+
* Whether the message says the project's billing or free-tier allowance is
|
|
87
|
+
* used up, as opposed to a per-minute / per-day request rate limit. Google
|
|
88
|
+
* words rate limits as "quota exceeded for metric ... per minute" and spent
|
|
89
|
+
* billing as "exceeded your current quota, please check your plan and
|
|
90
|
+
* billing details" or "billing ... not enabled".
|
|
91
|
+
*/
|
|
92
|
+
function isSpentQuota(message) {
|
|
93
|
+
if (/per[ -]?(minute|second|day)|requests? per|retry in|retryDelay/i.test(message)) return false;
|
|
94
|
+
return /check your plan and billing|billing (account|details|is not|not )|prepayment credits? (are|is)|exceeded your current quota|free tier.*(exhausted|exceeded)/i.test(message);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
76
97
|
* Read the raw error shape. The Gemini SDK's `ApiError` carries a
|
|
77
98
|
* numeric `status`; flattened/proxied errors may carry it (or `code`)
|
|
78
99
|
* loosely.
|
|
@@ -100,8 +121,9 @@ function toShape(thrown) {
|
|
|
100
121
|
* `AbortError` / `ETIMEDOUT` / `ECONNABORTED`.
|
|
101
122
|
*/
|
|
102
123
|
function isTimeout(shape) {
|
|
124
|
+
if (shape.name === "AbortError" || shape.name === "TimeoutError") return true;
|
|
103
125
|
if (shape.status === 504) return true;
|
|
104
|
-
if (
|
|
126
|
+
if (/deadline_exceeded/i.test(shape.message ?? "")) return true;
|
|
105
127
|
return shape.code === "ETIMEDOUT" || shape.code === "ECONNABORTED";
|
|
106
128
|
}
|
|
107
129
|
/** True for HTTP 4xx — a client-side request problem, not a server fault. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wrap-google-error.mjs","names":[],"sources":["../../../../../../../../packages/ai-google/src/utils/wrap-google-error.ts"],"sourcesContent":["import {\n AIError,\n ContextLengthExceededError,\n InvalidRequestError,\n ProviderAuthError,\n ProviderError,\n ProviderOverloadedError,\n ProviderRateLimitError,\n ProviderTimeoutError,\n} from \"@warlock.js/ai\";\nimport { ApiError } from \"@google/genai\";\n\n/**\n * Raw-error fields the wrapper reads off a Gemini SDK error.\n * `@google/genai`'s `ApiError` exposes `status` (HTTP code) +\n * `message`; transport aborts surface as `AbortError` / `ETIMEDOUT`.\n * We duck-type so proxied / re-thrown errors still classify.\n */\ntype GoogleErrorShape = {\n status?: number;\n message?: string;\n name?: string;\n code?: string;\n};\n\n/**\n * Wrap any thrown value caught inside the Gemini adapter into the\n * appropriate `@warlock.js/ai` `AIError` subclass.\n *\n * **Dispatch strategy.** Gemini has no machine error `code`; the\n * signals are the HTTP `status` and the canonical status phrase Google\n * embeds in `message` (`PERMISSION_DENIED`, `RESOURCE_EXHAUSTED`,\n * `INVALID_ARGUMENT`, …). Dispatch keys on `status`, using the message\n * phrase as the tie-breaker for the two 400 sub-cases\n * (context-length vs generic) and for status-less auth/quota errors.\n *\n * `AIError` instances pass through unchanged so `catch/throw wrap(e)`\n * pipelines never double-wrap.\n *\n * @example\n * try {\n * return await this.ai.models.generateContent(...);\n * } catch (thrown) {\n * throw wrapGoogleError(thrown);\n * }\n */\nexport function wrapGoogleError(thrown: unknown): AIError {\n if (thrown instanceof AIError) {\n return thrown;\n }\n\n const shape = toShape(thrown);\n const context = buildContext(shape);\n const message = shape.message ?? (thrown instanceof Error ? thrown.message : String(thrown));\n // \"If your request fails with a 400 or 500 error, you won't be charged\n // for the tokens used.\" — https://ai.google.dev/gemini-api/docs/billing\n // (fetched 2026-10-17). Every other failure keeps the default.\n const billed = shape.status === 400 || shape.status === 500 ? false : undefined;\n\n if (isTimeout(shape)) {\n return new ProviderTimeoutError(message, { cause: thrown, context, billed });\n }\n\n if (\n shape.status === 401 ||\n shape.status === 403 ||\n /permission_denied|api key not valid|unauthenticated/i.test(message)\n ) {\n return new ProviderAuthError(message, { cause: thrown, context, billed });\n }\n\n if (shape.status === 429 || /resource_exhausted|quota/i.test(message)) {\n return new ProviderRateLimitError(message, { cause: thrown, context, billed });\n }\n\n if (shape.status === 400) {\n if (/token count|context length|exceeds the maximum|input is too long/i.test(message)) {\n return new ContextLengthExceededError(message, { cause: thrown, context, billed });\n }\n\n return new InvalidRequestError(message, { cause: thrown, context, billed });\n }\n\n if (shape.status === 404 || isClientStatus(shape.status)) {\n return new InvalidRequestError(message, { cause: thrown, context, billed });\n }\n\n // 503 UNAVAILABLE — \"temporarily overloaded or down\"\n // (https://ai.google.dev/gemini-api/docs/api-errors).\n if (shape.status === 503 || /\\bUNAVAILABLE\\b/.test(message) || /overloaded/i.test(message)) {\n return new ProviderOverloadedError(message, { cause: thrown, context, billed });\n }\n\n return new ProviderError(message, { cause: thrown, context, billed });\n}\n\n/**\n * Read the raw error shape. The Gemini SDK's `ApiError` carries a\n * numeric `status`; flattened/proxied errors may carry it (or `code`)\n * loosely.\n */\nfunction toShape(thrown: unknown): GoogleErrorShape {\n if (thrown instanceof ApiError) {\n return { status: thrown.status, message: thrown.message, name: thrown.name };\n }\n\n if (typeof thrown === \"object\" && thrown !== null) {\n const raw = thrown as Record<string, unknown>;\n\n return {\n status: typeof raw.status === \"number\" ? raw.status : undefined,\n message: typeof raw.message === \"string\" ? raw.message : undefined,\n name: typeof raw.name === \"string\" ? raw.name : undefined,\n code: typeof raw.code === \"string\" ? raw.code : undefined,\n };\n }\n\n return {};\n}\n\n/**\n * Decide whether the error is a timeout. Gemini maps gateway timeouts\n * to HTTP 504 (`DEADLINE_EXCEEDED`); transport aborts surface as\n * `AbortError` / `ETIMEDOUT` / `ECONNABORTED`.\n */\nfunction isTimeout(shape: GoogleErrorShape): boolean {\n if (shape.
|
|
1
|
+
{"version":3,"file":"wrap-google-error.mjs","names":[],"sources":["../../../../../../../../packages/ai-google/src/utils/wrap-google-error.ts"],"sourcesContent":["import {\n AIError,\n ContextLengthExceededError,\n InvalidRequestError,\n ProviderAbortError,\n ProviderAuthError,\n ProviderError,\n ProviderOverloadedError,\n ProviderRateLimitError,\n ProviderTimeoutError,\n QuotaExceededError,\n} from \"@warlock.js/ai\";\nimport { ApiError } from \"@google/genai\";\n\n/**\n * Raw-error fields the wrapper reads off a Gemini SDK error.\n * `@google/genai`'s `ApiError` exposes `status` (HTTP code) +\n * `message`; transport aborts surface as `AbortError` / `ETIMEDOUT`.\n * We duck-type so proxied / re-thrown errors still classify.\n */\ntype GoogleErrorShape = {\n status?: number;\n message?: string;\n name?: string;\n code?: string;\n};\n\n/**\n * Wrap any thrown value caught inside the Gemini adapter into the\n * appropriate `@warlock.js/ai` `AIError` subclass.\n *\n * **Dispatch strategy.** Gemini has no machine error `code`; the\n * signals are the HTTP `status` and the canonical status phrase Google\n * embeds in `message` (`PERMISSION_DENIED`, `RESOURCE_EXHAUSTED`,\n * `INVALID_ARGUMENT`, …). Dispatch keys on `status`, using the message\n * phrase as the tie-breaker for the two 400 sub-cases\n * (context-length vs generic) and for status-less auth/quota errors.\n *\n * `AIError` instances pass through unchanged so `catch/throw wrap(e)`\n * pipelines never double-wrap.\n *\n * @example\n * try {\n * return await this.ai.models.generateContent(...);\n * } catch (thrown) {\n * throw wrapGoogleError(thrown);\n * }\n */\nexport function wrapGoogleError(thrown: unknown, options: { signal?: AbortSignal } = {}): AIError {\n if (thrown instanceof AIError) {\n return thrown;\n }\n\n const shape = toShape(thrown);\n const context = buildContext(shape);\n const message = shape.message ?? (thrown instanceof Error ? thrown.message : String(thrown));\n // \"If your request fails with a 400 or 500 error, you won't be charged\n // for the tokens used.\" — https://ai.google.dev/gemini-api/docs/billing\n // (fetched 2026-10-17). Every other failure keeps the default.\n const billed = shape.status === 400 || shape.status === 500 ? false : undefined;\n\n // The SDK aborts the request the same way for the caller's signal and for\n // its own `httpOptions.timeout`, so only an aborted caller signal makes an\n // `AbortError` a cancellation; any other one is the SDK's timeout.\n if (shape.name === \"AbortError\" && options.signal?.aborted === true) {\n return new ProviderAbortError(message, { cause: thrown, context, billed });\n }\n\n if (isTimeout(shape)) {\n return new ProviderTimeoutError(message, { cause: thrown, context, billed });\n }\n\n if (\n shape.status === 401 ||\n shape.status === 403 ||\n /permission_denied|api key not valid|unauthenticated/i.test(message)\n ) {\n return new ProviderAuthError(message, { cause: thrown, context, billed });\n }\n\n // Spent quota or billing needs a human (top up, raise the cap): a retry\n // cannot fix it. A per-minute / per-day rate limit stays a rate limit.\n if (isSpentQuota(message)) {\n return new QuotaExceededError(message, { cause: thrown, context, billed });\n }\n\n if (shape.status === 429 || /resource_exhausted|quota/i.test(message)) {\n return new ProviderRateLimitError(message, { cause: thrown, context, billed });\n }\n\n if (shape.status === 400) {\n if (/token count|context length|exceeds the maximum|input is too long/i.test(message)) {\n return new ContextLengthExceededError(message, { cause: thrown, context, billed });\n }\n\n return new InvalidRequestError(message, { cause: thrown, context, billed });\n }\n\n if (shape.status === 404 || isClientStatus(shape.status)) {\n return new InvalidRequestError(message, { cause: thrown, context, billed });\n }\n\n // 503 UNAVAILABLE — \"temporarily overloaded or down\"\n // (https://ai.google.dev/gemini-api/docs/api-errors).\n if (shape.status === 503 || /\\bUNAVAILABLE\\b/.test(message) || /overloaded/i.test(message)) {\n return new ProviderOverloadedError(message, { cause: thrown, context, billed });\n }\n\n return new ProviderError(message, { cause: thrown, context, billed });\n}\n\n/**\n * Whether the message says the project's billing or free-tier allowance is\n * used up, as opposed to a per-minute / per-day request rate limit. Google\n * words rate limits as \"quota exceeded for metric ... per minute\" and spent\n * billing as \"exceeded your current quota, please check your plan and\n * billing details\" or \"billing ... not enabled\".\n */\nfunction isSpentQuota(message: string): boolean {\n if (/per[ -]?(minute|second|day)|requests? per|retry in|retryDelay/i.test(message)) {\n return false;\n }\n\n return /check your plan and billing|billing (account|details|is not|not )|prepayment credits? (are|is)|exceeded your current quota|free tier.*(exhausted|exceeded)/i.test(\n message,\n );\n}\n\n/**\n * Read the raw error shape. The Gemini SDK's `ApiError` carries a\n * numeric `status`; flattened/proxied errors may carry it (or `code`)\n * loosely.\n */\nfunction toShape(thrown: unknown): GoogleErrorShape {\n if (thrown instanceof ApiError) {\n return { status: thrown.status, message: thrown.message, name: thrown.name };\n }\n\n if (typeof thrown === \"object\" && thrown !== null) {\n const raw = thrown as Record<string, unknown>;\n\n return {\n status: typeof raw.status === \"number\" ? raw.status : undefined,\n message: typeof raw.message === \"string\" ? raw.message : undefined,\n name: typeof raw.name === \"string\" ? raw.name : undefined,\n code: typeof raw.code === \"string\" ? raw.code : undefined,\n };\n }\n\n return {};\n}\n\n/**\n * Decide whether the error is a timeout. Gemini maps gateway timeouts\n * to HTTP 504 (`DEADLINE_EXCEEDED`); transport aborts surface as\n * `AbortError` / `ETIMEDOUT` / `ECONNABORTED`.\n */\nfunction isTimeout(shape: GoogleErrorShape): boolean {\n // An abort the caller did not ask for: the SDK's `httpOptions.timeout`.\n if (shape.name === \"AbortError\" || shape.name === \"TimeoutError\") {\n return true;\n }\n\n if (shape.status === 504) {\n return true;\n }\n\n if (/deadline_exceeded/i.test(shape.message ?? \"\")) {\n return true;\n }\n\n return shape.code === \"ETIMEDOUT\" || shape.code === \"ECONNABORTED\";\n}\n\n/** True for HTTP 4xx — a client-side request problem, not a server fault. */\nfunction isClientStatus(status: number | undefined): boolean {\n return typeof status === \"number\" && status >= 400 && status < 500;\n}\n\n/** Attach the diagnostic fields to `error.context`. */\nfunction buildContext(shape: GoogleErrorShape): Record<string, unknown> {\n const context: Record<string, unknown> = {};\n\n if (shape.status !== undefined) {\n context.status = shape.status;\n }\n\n if (shape.name) {\n context.code = shape.name;\n }\n\n return context;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,SAAgB,gBAAgB,QAAiB,UAAoC,CAAC,GAAY;CAChG,IAAI,kBAAkB,SACpB,OAAO;CAGT,MAAM,QAAQ,QAAQ,MAAM;CAC5B,MAAM,UAAU,aAAa,KAAK;CAClC,MAAM,UAAU,MAAM,YAAY,kBAAkB,QAAQ,OAAO,UAAU,OAAO,MAAM;CAI1F,MAAM,SAAS,MAAM,WAAW,OAAO,MAAM,WAAW,MAAM,QAAQ;CAKtE,IAAI,MAAM,SAAS,gBAAgB,QAAQ,QAAQ,YAAY,M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BAAkB,UACpB,OAAO;EAAE,QAAQ,OAAO;EAAQ,SAAS,OAAO;EAAS,MAAM,OAAO;CAAK;CAG7E,IAAI,OAAO,WAAW,YAAY,WAAW,MAAM;EACjD,MAAM,MAAM;EAEZ,OAAO;GACL,QAAQ,OAAO,IAAI,WAAW,WAAW,IAAI,SAAS;GACtD,SAAS,OAAO,IAAI,YAAY,WAAW,IAAI,UAAU;GACzD,MAAM,OAAO,IAAI,SAAS,WAAW,IAAI,OAAO;GAChD,MAAM,OAAO,IAAI,SAAS,WAAW,IAAI,OAAO;EAClD;CACF;CAEA,OAAO,CAAC;AACV;;;;;;AAOA,SAAS,UAAU,OAAkC;CAEnD,IAAI,MAAM,SAAS,gBAAgB,MAAM,SAAS,gBAChD,OAAO;CAGT,IAAI,MAAM,WAAW,KACnB,OAAO;CAGT,IAAI,qBAAqB,KAAK,MAAM,WAAW,EAAE,GAC/C,OAAO;CAGT,OAAO,MAAM,SAAS,eAAe,MAAM,SAAS;AACtD;;AAGA,SAAS,eAAe,QAAqC;CAC3D,OAAO,OAAO,WAAW,YAAY,UAAU,OAAO,SAAS;AACjE;;AAGA,SAAS,aAAa,OAAkD;CACtE,MAAM,UAAmC,CAAC;CAE1C,IAAI,MAAM,WAAW,QACnB,QAAQ,SAAS,MAAM;CAGzB,IAAI,MAAM,MACR,QAAQ,OAAO,MAAM;CAGvB,OAAO;AACT"}
|
package/llms-full.txt
CHANGED
|
@@ -196,10 +196,12 @@ const { vectors } = await embedder.embedMany(["a", "b"]); // single batched call
|
|
|
196
196
|
Wrapped into the typed `@warlock.js/ai` `AIError` hierarchy. Gemini has no machine error code — dispatch keys on HTTP `status` + canonical status phrase in the message:
|
|
197
197
|
|
|
198
198
|
- 401/403 / `PERMISSION_DENIED` / "API key not valid" → `ProviderAuthError`
|
|
199
|
+
- spent billing or free-tier allowance ("check your plan and billing details", with no per-minute / per-day / retry hint) → `QuotaExceededError` (not retried)
|
|
199
200
|
- 429 / `RESOURCE_EXHAUSTED` → `ProviderRateLimitError`
|
|
200
201
|
- 400 with token/context phrasing → `ContextLengthExceededError`, else `InvalidRequestError`
|
|
201
202
|
- 404 / any other 4xx → `InvalidRequestError`
|
|
202
|
-
-
|
|
203
|
+
- `AbortError` while the call's own `signal` is aborted → `ProviderAbortError` (category `cancelled`, never retried)
|
|
204
|
+
- 504 / `DEADLINE_EXCEEDED` / `ETIMEDOUT` / `ECONNABORTED` / any other `AbortError` (the SDK's own `httpOptions.timeout`) → `ProviderTimeoutError`
|
|
203
205
|
- 503 / `UNAVAILABLE` / "overloaded" → `ProviderOverloadedError` (`code: "PROVIDER_OVERLOADED"`, `retryable: true`) — Gemini documents 503 as "temporarily overloaded or down" (https://ai.google.dev/gemini-api/docs/api-errors)
|
|
204
206
|
- anything else (5xx and status-less) → `ProviderError`
|
|
205
207
|
|
|
@@ -219,7 +221,7 @@ if (result.error instanceof ProviderOverloadedError) {
|
|
|
219
221
|
}
|
|
220
222
|
```
|
|
221
223
|
|
|
222
|
-
|
|
224
|
+
The abort and timeout checks run first, so an `AbortError` classifies as `ProviderAbortError` (caller cancelled) or `ProviderTimeoutError` regardless of status. `AIError` instances pass through unwrapped (no double-wrap).
|
|
223
225
|
|
|
224
226
|
## Token counting
|
|
225
227
|
|
package/package.json
CHANGED
|
@@ -15,13 +15,13 @@
|
|
|
15
15
|
"directory": "packages/ai-google"
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|
|
18
|
-
"@warlock.js/ai": "5.
|
|
18
|
+
"@warlock.js/ai": "5.54.0"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@google/genai": "^2.17.1",
|
|
22
|
-
"@warlock.js/logger": "5.
|
|
22
|
+
"@warlock.js/logger": "5.54.0"
|
|
23
23
|
},
|
|
24
|
-
"version": "5.
|
|
24
|
+
"version": "5.54.0",
|
|
25
25
|
"main": "./cjs/index.cjs",
|
|
26
26
|
"module": "./esm/index.mjs",
|
|
27
27
|
"types": "./esm/index.d.mts",
|
package/skills/SKILL.md
CHANGED
|
@@ -188,10 +188,12 @@ const { vectors } = await embedder.embedMany(["a", "b"]); // single batched call
|
|
|
188
188
|
Wrapped into the typed `@warlock.js/ai` `AIError` hierarchy. Gemini has no machine error code — dispatch keys on HTTP `status` + canonical status phrase in the message:
|
|
189
189
|
|
|
190
190
|
- 401/403 / `PERMISSION_DENIED` / "API key not valid" → `ProviderAuthError`
|
|
191
|
+
- spent billing or free-tier allowance ("check your plan and billing details", with no per-minute / per-day / retry hint) → `QuotaExceededError` (not retried)
|
|
191
192
|
- 429 / `RESOURCE_EXHAUSTED` → `ProviderRateLimitError`
|
|
192
193
|
- 400 with token/context phrasing → `ContextLengthExceededError`, else `InvalidRequestError`
|
|
193
194
|
- 404 / any other 4xx → `InvalidRequestError`
|
|
194
|
-
-
|
|
195
|
+
- `AbortError` while the call's own `signal` is aborted → `ProviderAbortError` (category `cancelled`, never retried)
|
|
196
|
+
- 504 / `DEADLINE_EXCEEDED` / `ETIMEDOUT` / `ECONNABORTED` / any other `AbortError` (the SDK's own `httpOptions.timeout`) → `ProviderTimeoutError`
|
|
195
197
|
- 503 / `UNAVAILABLE` / "overloaded" → `ProviderOverloadedError` (`code: "PROVIDER_OVERLOADED"`, `retryable: true`) — Gemini documents 503 as "temporarily overloaded or down" (https://ai.google.dev/gemini-api/docs/api-errors)
|
|
196
198
|
- anything else (5xx and status-less) → `ProviderError`
|
|
197
199
|
|
|
@@ -211,7 +213,7 @@ if (result.error instanceof ProviderOverloadedError) {
|
|
|
211
213
|
}
|
|
212
214
|
```
|
|
213
215
|
|
|
214
|
-
|
|
216
|
+
The abort and timeout checks run first, so an `AbortError` classifies as `ProviderAbortError` (caller cancelled) or `ProviderTimeoutError` regardless of status. `AIError` instances pass through unwrapped (no double-wrap).
|
|
215
217
|
|
|
216
218
|
## Token counting
|
|
217
219
|
|