@latimer-woods-tech/llm 0.6.0 → 0.6.1
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 +39 -0
- package/README.md +17 -5
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +131 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,44 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.6.1 — 2026-08-18
|
|
4
|
+
|
|
5
|
+
### Fixed — ships the routing and sampling fixes from #3946, which never reached npm
|
|
6
|
+
|
|
7
|
+
Released as a **patch** deliberately. Under 0.x caret semantics `^0.6.0` accepts
|
|
8
|
+
`0.6.1` but rejects `0.7.0`, so a minor bump would have left the one consumer
|
|
9
|
+
pinned at `^0.6.0` (`apps/daily-brief`) on the broken chain indefinitely. From a
|
|
10
|
+
caller's perspective this is a bug fix anyway: they asked for the `balanced`
|
|
11
|
+
tier and should get the current balanced model.
|
|
12
|
+
|
|
13
|
+
`0.6.0` was published 2026-08-03. [#3946](https://github.com/Latimer-Woods-Tech/Factory/pull/3946)
|
|
14
|
+
landed on `main` 2026-08-14 and was never published, so for four days every
|
|
15
|
+
consumer installed a chain a generation behind the one in source. This release
|
|
16
|
+
carries that fix and nothing else — no new API surface.
|
|
17
|
+
|
|
18
|
+
- **Anthropic tier routing is current-gen again.** `balanced` resolves to
|
|
19
|
+
`claude-sonnet-5` (was `claude-sonnet-4-6`) and `smart` to `claude-opus-5`
|
|
20
|
+
(was `claude-opus-4-7`).
|
|
21
|
+
- **`temperature` is gated by model family.** Claude 4.7+ rejects sampling
|
|
22
|
+
params with `400 "temperature is deprecated for this model"`. `0.6.0` sent
|
|
23
|
+
`temperature` unconditionally, so the `smart` tier's Anthropic leg 400'd on
|
|
24
|
+
every call and silently fell through to Gemini — a degradation that never
|
|
25
|
+
surfaced as an error to callers. The request builder now omits the field for
|
|
26
|
+
model families that removed it.
|
|
27
|
+
- **Canonical prices corrected.** `claude-opus-4-7` was listed at $15/$75 per
|
|
28
|
+
MTok against a real $5/$25; ledger rows written by `llm-meter` for smart-tier
|
|
29
|
+
calls before this release are overstated ~3×.
|
|
30
|
+
|
|
31
|
+
Why it matters beyond correctness: on 2026-08-17 the Anthropic org hit its
|
|
32
|
+
monthly spend cap, and `claude-sonnet-4-6` — reachable *only* through the stale
|
|
33
|
+
published routing — accounted for 83% of the month's usage. See
|
|
34
|
+
[Factory#4116](https://github.com/Latimer-Woods-Tech/Factory/issues/4116).
|
|
35
|
+
|
|
36
|
+
> Verification note: the corrected chain is covered by request-shape regression
|
|
37
|
+
> tests (132 passing), and the built `dist/` was read directly to confirm both
|
|
38
|
+
> the routing table and the sampling gate. It could **not** be live-tested
|
|
39
|
+
> against `api.anthropic.com` at release time — the org spend cap was in force,
|
|
40
|
+
> so the primary rung returns 400 until the cap is raised or resets 2026-09-01.
|
|
41
|
+
|
|
3
42
|
## 0.6.0 — 2026-08-03
|
|
4
43
|
|
|
5
44
|
### Added — local Qwen workbench and tool protocol
|
package/README.md
CHANGED
|
@@ -1,21 +1,33 @@
|
|
|
1
1
|
# @latimer-woods-tech/llm
|
|
2
2
|
|
|
3
|
-
Tier-routed LLM orchestration for the Factory platform, with Cloudflare AI Gateway,
|
|
4
|
-
|
|
3
|
+
Tier-routed LLM orchestration for the Factory platform, with Cloudflare AI Gateway, local Qwen,
|
|
4
|
+
Anthropic, Gemini, Grok, Groq, and DeepSeek routing.
|
|
5
5
|
|
|
6
6
|
## Routing (0.3.0)
|
|
7
7
|
|
|
8
8
|
| Tier | Primary | Fallback | Notes |
|
|
9
9
|
|---|---|---|---|
|
|
10
|
-
| `fast` | Claude Haiku 4 |
|
|
11
|
-
| `balanced` *(default)* | Claude Sonnet 4 | Gemini 2.5
|
|
12
|
-
| `smart` | Claude Opus 4 | Gemini 2.5
|
|
10
|
+
| `fast` | Grok 4.3 | Claude Haiku 4 | local Qwen 8B can become the opt-in primary |
|
|
11
|
+
| `balanced` *(default)* | Claude Sonnet 4 | Gemini 2.5 Flash | swaps to Gemini when est. tokens ≥ 150k |
|
|
12
|
+
| `smart` | Claude Opus 4 | Gemini 2.5 Flash | ditto; tools, long reasoning |
|
|
13
13
|
| `verifier` | Groq Llama 3.3 70B | — | cheap second opinion; no fallback |
|
|
14
14
|
| `workbench` | DeepSeek Chat | Groq Llama | boring, reviewable, non-sensitive batch work |
|
|
15
15
|
|
|
16
16
|
All traffic flows through `AI_GATEWAY_BASE_URL`. The gateway handles caching, rate-limit shedding,
|
|
17
17
|
and per-project cost telemetry.
|
|
18
18
|
|
|
19
|
+
## Local Qwen routes
|
|
20
|
+
|
|
21
|
+
Set `LLM_LOCAL_FIRST=true` with the GPU bearer to make `qwen3:8b` the `fast`-tier primary.
|
|
22
|
+
The package calls `custom-local-fast-gpu/api/chat` using Ollama's native contract with
|
|
23
|
+
`stream:false` and `think:false`; `maxTokens` maps to `options.num_predict`. A local failure
|
|
24
|
+
falls through to Grok, never directly to Anthropic.
|
|
25
|
+
|
|
26
|
+
Set `LLM_LOCAL_WORKBENCH=true` to make `qwen3.6:27b` the `workbench` primary. The 27B model
|
|
27
|
+
retains the OpenAI-compatible `custom-local-gpu/v1/chat/completions` route and normalized tool
|
|
28
|
+
protocol, with DeepSeek as its independent fallback. Both routes require `GPU_LLM_API_TOKEN`;
|
|
29
|
+
the Cloudflare Access client id and secret are forwarded when supplied.
|
|
30
|
+
|
|
19
31
|
## Usage
|
|
20
32
|
|
|
21
33
|
```ts
|
package/dist/index.d.mts
CHANGED
|
@@ -273,7 +273,7 @@ interface LLMEnv {
|
|
|
273
273
|
GROK_API_KEY?: string;
|
|
274
274
|
/**
|
|
275
275
|
* Cost optimization: when true, `fast`-tier calls route to the self-hosted GPU
|
|
276
|
-
* (qwen3 via the `custom-local-gpu` AI Gateway provider) FIRST, with the normal
|
|
276
|
+
* (qwen3:8b via the `custom-local-fast-gpu` AI Gateway provider) FIRST, with the normal
|
|
277
277
|
* fast route (Grok→Haiku) as automatic fallback on any error. Off by default →
|
|
278
278
|
* fully dormant. Requires {@link LLMEnv.GPU_LLM_API_TOKEN}.
|
|
279
279
|
*/
|
|
@@ -376,8 +376,8 @@ interface LLMDeps {
|
|
|
376
376
|
declare const MODELS: {
|
|
377
377
|
readonly anthropic: {
|
|
378
378
|
readonly fast: "claude-haiku-4-5";
|
|
379
|
-
readonly balanced: "claude-sonnet-
|
|
380
|
-
readonly smart: "claude-opus-
|
|
379
|
+
readonly balanced: "claude-sonnet-5";
|
|
380
|
+
readonly smart: "claude-opus-5";
|
|
381
381
|
};
|
|
382
382
|
readonly gemini: {
|
|
383
383
|
readonly smart: "gemini-2.5-flash";
|
package/dist/index.mjs
CHANGED
|
@@ -207,8 +207,17 @@ var MODELS = {
|
|
|
207
207
|
// `claude-haiku-4-5-20251001`. This is the `fast` tier's FALLBACK, so the
|
|
208
208
|
// 404 stayed invisible for as long as the Grok primary held.
|
|
209
209
|
fast: "claude-haiku-4-5",
|
|
210
|
-
|
|
211
|
-
|
|
210
|
+
// Current-generation Sonnet/Opus (2026-08 refresh). The previous pins —
|
|
211
|
+
// `claude-sonnet-4-6` / `claude-opus-4-7` — were a generation behind, and
|
|
212
|
+
// the opus-4-7 leg was HARD-BROKEN: Claude 4.7+ rejects `temperature`
|
|
213
|
+
// (400 "`temperature` is deprecated for this model", verified live
|
|
214
|
+
// 2026-08-14) and buildAnthropicRequest always sent one, so every `smart`
|
|
215
|
+
// call 400'd on its Anthropic primary and silently served Gemini Flash.
|
|
216
|
+
// Sonnet 5 / Opus 5 cost the same list price or less than the models they
|
|
217
|
+
// replace ($3/$15, $5/$25 per 1M). The sampling/thinking request-shape
|
|
218
|
+
// differences these models introduce are handled in buildAnthropicRequest.
|
|
219
|
+
balanced: "claude-sonnet-5",
|
|
220
|
+
smart: "claude-opus-5"
|
|
212
221
|
},
|
|
213
222
|
gemini: {
|
|
214
223
|
// `gemini-2.5-flash`, not `-pro`: the leg's job here is a fast, reliable,
|
|
@@ -237,8 +246,8 @@ var MODELS = {
|
|
|
237
246
|
workbench: "deepseek-chat"
|
|
238
247
|
},
|
|
239
248
|
local: {
|
|
240
|
-
// Self-hosted qwen3:8b on the GPU
|
|
241
|
-
// AI Gateway provider
|
|
249
|
+
// Self-hosted qwen3:8b on the dedicated fast GPU, reached through the
|
|
250
|
+
// `custom-local-fast-gpu` AI Gateway provider's native Ollama chat route.
|
|
242
251
|
fast: "qwen3:8b",
|
|
243
252
|
workbench: "qwen3.6:27b"
|
|
244
253
|
}
|
|
@@ -282,14 +291,24 @@ async function recordOrgCostUsage(kv, todayKey, monthKey, costUsd, opts) {
|
|
|
282
291
|
var MODEL_PRICE_PER_1M = {
|
|
283
292
|
// Anthropic Haiku 4.5 — `claude-haiku-4-5` is the routed alias; the dated id
|
|
284
293
|
// is what the API echoes back in `response.model`, so both must price.
|
|
285
|
-
|
|
286
|
-
|
|
294
|
+
// (Rates corrected 2026-08: the previous $0.80/$4.00 rows UNDERSTATED the
|
|
295
|
+
// actual $1/$5 list price.)
|
|
296
|
+
"claude-haiku-4-5": { input: 1, output: 5, cacheRead: 0.1, cacheWrite: 1.25 },
|
|
297
|
+
"claude-haiku-4-5-20251001": { input: 1, output: 5, cacheRead: 0.1, cacheWrite: 1.25 },
|
|
287
298
|
// Anthropic Sonnet 4
|
|
288
299
|
"claude-sonnet-4-20250514": { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
|
|
289
300
|
"claude-sonnet-4-6": { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
|
|
290
|
-
// Anthropic
|
|
301
|
+
// Anthropic Sonnet 5 — the routed `balanced` primary. List $3/$15; intro
|
|
302
|
+
// pricing ($2/$10) runs through 2026-08-31 — priced at list here, the
|
|
303
|
+
// conservative upper bound (table convention).
|
|
304
|
+
"claude-sonnet-5": { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
|
|
305
|
+
// Anthropic Opus 4 (dated id) — genuinely the $15/$75 era.
|
|
291
306
|
"claude-opus-4-20250514": { input: 15, output: 75, cacheRead: 1.5, cacheWrite: 18.75 },
|
|
292
|
-
|
|
307
|
+
// Opus 4.7 was NEVER $15/$75 — it launched at $5/$25. The old row copied the
|
|
308
|
+
// Opus 4 rate and overstated every smart-tier ledger entry 3x.
|
|
309
|
+
"claude-opus-4-7": { input: 5, output: 25, cacheRead: 0.5, cacheWrite: 6.25 },
|
|
310
|
+
// Anthropic Opus 5 — the routed `smart` primary. Same $5/$25 as Opus 4.8/4.7.
|
|
311
|
+
"claude-opus-5": { input: 5, output: 25, cacheRead: 0.5, cacheWrite: 6.25 },
|
|
293
312
|
// Gemini 2.5 Flash — the routed `smart`/long-context model (JSON fallback leg).
|
|
294
313
|
"gemini-2.5-flash": { input: 0.3, output: 2.5, cacheRead: 0.075, cacheWrite: 0.3 },
|
|
295
314
|
// Gemini 2.5 Pro — retained for historical ledger rows (was the routed model).
|
|
@@ -349,15 +368,22 @@ function computeBackoffMs(attempt) {
|
|
|
349
368
|
const jitter = Math.floor(Math.random() * BACKOFF_JITTER_MAX_MS);
|
|
350
369
|
return Math.min(BACKOFF_BASE_MS * Math.pow(2, attempt) + jitter, BACKOFF_CAP_MS);
|
|
351
370
|
}
|
|
371
|
+
var ANTHROPIC_SAMPLING_REMOVED = /^claude-(opus-4-[78]|opus-5|sonnet-5|fable-5|mythos-5)/;
|
|
372
|
+
var ANTHROPIC_THINKING_DEFAULT_ON = /^claude-(opus-5|sonnet-5)$/;
|
|
352
373
|
function buildAnthropicRequest(model, messages, opts, env, streaming = false) {
|
|
353
374
|
const sys = systemText(opts, messages);
|
|
354
375
|
const filtered = messages.filter((m) => m.role !== "system");
|
|
355
376
|
const body = {
|
|
356
377
|
model,
|
|
357
378
|
max_tokens: opts.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
358
|
-
temperature: opts.temperature ?? DEFAULT_TEMPERATURE,
|
|
359
379
|
messages: filtered.map((m) => ({ role: m.role, content: m.content }))
|
|
360
380
|
};
|
|
381
|
+
if (!ANTHROPIC_SAMPLING_REMOVED.test(model)) {
|
|
382
|
+
body.temperature = opts.temperature ?? DEFAULT_TEMPERATURE;
|
|
383
|
+
}
|
|
384
|
+
if (ANTHROPIC_THINKING_DEFAULT_ON.test(model)) {
|
|
385
|
+
body.thinking = { type: "disabled" };
|
|
386
|
+
}
|
|
361
387
|
if (streaming) {
|
|
362
388
|
body.stream = true;
|
|
363
389
|
}
|
|
@@ -459,6 +485,53 @@ function toOpenAiMessages(messages, sys) {
|
|
|
459
485
|
}
|
|
460
486
|
return out;
|
|
461
487
|
}
|
|
488
|
+
function toOllamaMessages(messages, sys) {
|
|
489
|
+
const toolNames = /* @__PURE__ */ new Map();
|
|
490
|
+
for (const message of messages) {
|
|
491
|
+
if (typeof message.content === "string") continue;
|
|
492
|
+
for (const block of message.content) {
|
|
493
|
+
if (block.type === "tool_use") toolNames.set(block.id, block.name);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
const out = [];
|
|
497
|
+
if (sys) out.push({ role: "system", content: sys });
|
|
498
|
+
for (const message of messages) {
|
|
499
|
+
if (message.role === "system") continue;
|
|
500
|
+
if (typeof message.content === "string") {
|
|
501
|
+
out.push({ role: message.role, content: message.content });
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
let text = "";
|
|
505
|
+
const toolCalls = [];
|
|
506
|
+
const results = [];
|
|
507
|
+
for (const block of message.content) {
|
|
508
|
+
if (block.type === "text") text += block.text;
|
|
509
|
+
else if (block.type === "tool_use") {
|
|
510
|
+
toolCalls.push({
|
|
511
|
+
type: "function",
|
|
512
|
+
function: { index: toolCalls.length, name: block.name, arguments: block.input }
|
|
513
|
+
});
|
|
514
|
+
} else if (block.type === "tool_result") {
|
|
515
|
+
results.push({ toolUseId: block.tool_use_id, content: block.content });
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
if (results.length > 0) {
|
|
519
|
+
for (const result of results) {
|
|
520
|
+
out.push({
|
|
521
|
+
role: "tool",
|
|
522
|
+
tool_name: toolNames.get(result.toolUseId) ?? result.toolUseId,
|
|
523
|
+
content: result.content
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
if (text) out.push({ role: "user", content: text });
|
|
527
|
+
} else if (toolCalls.length > 0) {
|
|
528
|
+
out.push({ role: "assistant", content: text, tool_calls: toolCalls });
|
|
529
|
+
} else {
|
|
530
|
+
out.push({ role: message.role, content: text });
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return out;
|
|
534
|
+
}
|
|
462
535
|
function openAiTools(opts) {
|
|
463
536
|
if (!opts.tools || opts.tools.length === 0) return void 0;
|
|
464
537
|
return opts.tools.map((t) => ({
|
|
@@ -551,7 +624,6 @@ function buildLocalRequest(model, messages, opts, env) {
|
|
|
551
624
|
}
|
|
552
625
|
const sys = systemText(opts, messages);
|
|
553
626
|
const fastMode = model === MODELS.local.fast;
|
|
554
|
-
const augmented = fastMode ? sys ? `${sys} /no_think` : "/no_think" : sys;
|
|
555
627
|
const headers = {
|
|
556
628
|
"content-type": "application/json",
|
|
557
629
|
authorization: `Bearer ${env.GPU_LLM_API_TOKEN}`
|
|
@@ -559,6 +631,26 @@ function buildLocalRequest(model, messages, opts, env) {
|
|
|
559
631
|
if (env.GPU_LLM_ACCESS_CLIENT_ID) headers["CF-Access-Client-Id"] = env.GPU_LLM_ACCESS_CLIENT_ID;
|
|
560
632
|
if (env.GPU_LLM_ACCESS_CLIENT_SECRET) headers["CF-Access-Client-Secret"] = env.GPU_LLM_ACCESS_CLIENT_SECRET;
|
|
561
633
|
const tools = openAiTools(opts);
|
|
634
|
+
if (fastMode) {
|
|
635
|
+
if (typeof opts.toolChoice === "object") {
|
|
636
|
+
throw new ValidationError2("named tool choice is not supported by the native Qwen 8B route");
|
|
637
|
+
}
|
|
638
|
+
return {
|
|
639
|
+
url: `${env.AI_GATEWAY_BASE_URL}/custom-local-fast-gpu/api/chat`,
|
|
640
|
+
headers,
|
|
641
|
+
body: JSON.stringify({
|
|
642
|
+
model,
|
|
643
|
+
stream: false,
|
|
644
|
+
think: false,
|
|
645
|
+
messages: toOllamaMessages(messages, sys),
|
|
646
|
+
options: {
|
|
647
|
+
num_predict: opts.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
648
|
+
temperature: opts.temperature ?? DEFAULT_TEMPERATURE
|
|
649
|
+
},
|
|
650
|
+
...tools && opts.toolChoice !== "none" ? { tools } : {}
|
|
651
|
+
})
|
|
652
|
+
};
|
|
653
|
+
}
|
|
562
654
|
return {
|
|
563
655
|
url: `${env.AI_GATEWAY_BASE_URL}/custom-local-gpu/v1/chat/completions`,
|
|
564
656
|
headers,
|
|
@@ -566,7 +658,7 @@ function buildLocalRequest(model, messages, opts, env) {
|
|
|
566
658
|
model,
|
|
567
659
|
max_tokens: opts.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
568
660
|
temperature: opts.temperature ?? DEFAULT_TEMPERATURE,
|
|
569
|
-
messages: toOpenAiMessages(messages,
|
|
661
|
+
messages: toOpenAiMessages(messages, sys),
|
|
570
662
|
...tools ? { tools, tool_choice: openAiToolChoice(opts.toolChoice ?? "auto") } : {}
|
|
571
663
|
})
|
|
572
664
|
};
|
|
@@ -655,6 +747,29 @@ function parseOpenAi(json) {
|
|
|
655
747
|
stopReason: normalizeOpenAiStop(choice?.finish_reason)
|
|
656
748
|
};
|
|
657
749
|
}
|
|
750
|
+
function parseOllamaChat(json) {
|
|
751
|
+
const response = json;
|
|
752
|
+
const toolCalls = (response.message?.tool_calls ?? []).filter((call) => typeof call.function?.name === "string").map((call, index) => ({
|
|
753
|
+
id: `call_${index}`,
|
|
754
|
+
name: call.function.name,
|
|
755
|
+
arguments: typeof call.function?.arguments === "string" ? parseToolArgs(call.function.arguments) : call.function?.arguments ?? {}
|
|
756
|
+
}));
|
|
757
|
+
let stopReason;
|
|
758
|
+
if (toolCalls.length > 0) stopReason = "tool_use";
|
|
759
|
+
else if (response.done_reason === "length") stopReason = "max_tokens";
|
|
760
|
+
else if (response.done) stopReason = "end";
|
|
761
|
+
else if (response.done_reason) stopReason = "other";
|
|
762
|
+
return {
|
|
763
|
+
// Deliberately exclude message.thinking: callers receive only the bounded
|
|
764
|
+
// classification answer even if a non-conforming origin returns a trace.
|
|
765
|
+
content: response.message?.content ?? "",
|
|
766
|
+
input: response.prompt_eval_count ?? 0,
|
|
767
|
+
output: response.eval_count ?? 0,
|
|
768
|
+
model: response.model,
|
|
769
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
770
|
+
stopReason
|
|
771
|
+
};
|
|
772
|
+
}
|
|
658
773
|
async function callWithBackoff(provider, request, fetchImpl, signal, logger, nowFn) {
|
|
659
774
|
function exhaustAndThrow(err) {
|
|
660
775
|
markProviderCoolingDown(provider, nowFn ?? Date.now);
|
|
@@ -816,7 +931,11 @@ async function callOne(leg, messages, opts, env, fetchImpl, logger, nowFn) {
|
|
|
816
931
|
case "deepseek":
|
|
817
932
|
return { parsed: parseOpenAi(json), gatewayRequestId, attempts };
|
|
818
933
|
case "local":
|
|
819
|
-
return {
|
|
934
|
+
return {
|
|
935
|
+
parsed: leg.model === MODELS.local.fast ? parseOllamaChat(json) : parseOpenAi(json),
|
|
936
|
+
gatewayRequestId,
|
|
937
|
+
attempts
|
|
938
|
+
};
|
|
820
939
|
}
|
|
821
940
|
}
|
|
822
941
|
async function complete(messages, env, opts = {}, deps = {}) {
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/gcp-token.ts","../src/embed.ts"],"sourcesContent":["import {\n InternalError,\n RateLimitError,\n ValidationError,\n toErrorResponse,\n type FactoryResponse,\n} from '@latimer-woods-tech/errors';\nimport type { Logger } from '@latimer-woods-tech/logger';\nimport { fetchAdcAccessToken, mintGcpAccessToken, serviceAccountProjectId } from './gcp-token.js';\n\n/**\n * A tool the model may call. `parameters` is a JSON Schema object describing\n * the tool's input. Provider-agnostic; normalized per provider at request time.\n */\nexport interface LLMTool {\n name: string;\n description?: string;\n /** JSON Schema for the tool's input arguments. */\n parameters: Record<string, unknown>;\n}\n\n/**\n * A tool invocation requested by the model, normalized across providers.\n */\nexport interface LLMToolCall {\n /** Provider-assigned call id; echo it back in the matching tool_result. */\n id: string;\n name: string;\n /** Parsed argument object the model passed to the tool. */\n arguments: Record<string, unknown>;\n}\n\n/**\n * Structured content block for tool-calling conversations. The field shapes\n * mirror the Anthropic Messages wire format so they pass through unchanged.\n */\nexport type LLMContentBlock =\n | { type: 'text'; text: string }\n | { type: 'tool_use'; id: string; name: string; input: Record<string, unknown> }\n | { type: 'tool_result'; tool_use_id: string; content: string; is_error?: boolean };\n\n/**\n * Single chat message exchanged with an LLM provider.\n *\n * `content` is a plain string in the common case. For tool-calling\n * conversations it may be an array of {@link LLMContentBlock}s (e.g. an\n * assistant turn carrying `tool_use` blocks, or a user turn carrying\n * `tool_result` blocks). Providers that don't support tool-calling receive\n * the text projection of the content (see `contentToText`).\n */\nexport interface LLMMessage {\n role: 'user' | 'assistant' | 'system';\n content: string | LLMContentBlock[];\n}\n\n/**\n * Flattens message content to plain text for providers/paths that only handle\n * strings. `tool_use` blocks contribute nothing; `tool_result` blocks\n * contribute their textual content.\n */\nfunction contentToText(content: string | LLMContentBlock[]): string {\n if (typeof content === 'string') return content;\n return content\n .map((b) => (b.type === 'text' ? b.text : b.type === 'tool_result' ? b.content : ''))\n .join('');\n}\n\n/**\n * Resolves the system prompt: explicit `opts.system` wins, else the first\n * `system` message, flattened to text. Returns `undefined` when neither is set.\n */\nfunction systemText(opts: LLMOptions, messages: LLMMessage[]): string | undefined {\n if (opts.system !== undefined) return opts.system;\n const c = messages.find((m) => m.role === 'system')?.content;\n return c === undefined ? undefined : contentToText(c);\n}\n\n/**\n * Quality tier selected by the caller. Routing is workload-split:\n * - `fast` → Grok 4.3 with Anthropic Haiku fallback (routine drafts/small jobs)\n * - `balanced` → Anthropic Sonnet (default)\n * - `smart` → Anthropic Opus OR Gemini 2.5 Flash if input is long-context (>150k tokens estimated)\n * - `verifier` → Groq Llama (cheap second opinion; only used from verifier code path)\n * - `workbench` → DeepSeek Chat with Groq fallback (boring, reviewable, non-sensitive batch work)\n */\nexport type LLMTier = 'fast' | 'balanced' | 'smart' | 'verifier' | 'workbench';\n\n/**\n * Options that influence LLM completion behaviour.\n */\nexport interface LLMOptions {\n /** Quality tier; see {@link LLMTier}. Defaults to `balanced`. */\n tier?: LLMTier;\n /** Explicit model override. Takes precedence over tier. */\n model?: string;\n maxTokens?: number;\n temperature?: number;\n system?: string;\n /** Token budget above which we force long-context routing (Gemini). */\n longContextThreshold?: number;\n /** Per-call cancellation signal. Aborts the in-flight provider request. */\n signal?: AbortSignal;\n /** Optional run identifier stamped on ledger rows + logs. */\n runId?: string;\n /** Optional project identifier stamped on ledger rows + logs. */\n project?: string;\n /** Optional actor identifier (supervisor / worker / human). */\n actor?: string;\n /** Optional workload label used in logs and cost-policy call sites. */\n workload?: string;\n /** Grok reasoning effort. Defaults to `none` for cost-controlled fast/draft calls. */\n reasoningEffort?: 'none' | 'low' | 'medium' | 'high';\n /** Anthropic prompt-cache control. Defaults to `true` for `system` prompts ≥ 1024 tokens. */\n promptCache?: boolean;\n /**\n * Maximum estimated cost in USD for this completion.\n * This cap is enforced after the provider returns because it uses actual\n * response token counts to compute the final cost.\n * If the post-call estimated cost exceeds this cap, `complete` returns a\n * {@link RateLimitError} with code `LLM_COST_CAP_EXCEEDED` and\n * `completionStream` throws the same error.\n * Pricing is based on {@link MODEL_PRICE_PER_1M}; unknown models default to\n * Opus rates (conservative upper bound).\n */\n maxCostUsd?: number;\n /**\n * Org-level daily cost cap in USD. Requires `env.LLM_COST_KV` to be set.\n * When today's cumulative spend read from KV is >= this value, `complete`\n * returns a {@link RateLimitError} with code `LLM_DAILY_CAP_EXCEEDED`\n * without making any provider call. After a successful call the daily\n * accumulator is updated in KV (TTL: 48 h).\n */\n dailyCapUsd?: number;\n /**\n * Org-level monthly cost cap in USD. Requires `env.LLM_COST_KV` to be set.\n * Same enforcement pattern as {@link dailyCapUsd} but keyed by YYYY-MM.\n * KV TTL: 40 days.\n */\n monthlyCapUsd?: number;\n /**\n * Metering context. When supplied and `deps.onRecord` is set, a {@link LLMRecordRow}\n * is emitted after every successful completion. Errors are swallowed.\n */\n ledger?: LLMRecordContext;\n /**\n * Tools the model may call. When present, routing **fails closed** to\n * tool-capable providers — failover never falls back to a provider that\n * can't honour the tool schema. See {@link LLMResult.toolCalls}.\n */\n tools?: LLMTool[];\n /**\n * Tool-selection policy. `'auto'` (default when `tools` is set) lets the\n * model decide; `'none'` forbids tool use; `{ name }` forces a specific tool.\n */\n toolChoice?: 'auto' | 'none' | { name: string };\n}\n\n/**\n * Provider that produced an LLM response.\n */\nexport type LLMProvider = 'anthropic' | 'gemini' | 'groq' | 'grok' | 'deepseek' | 'local';\n\n/**\n * Result returned by a successful completion.\n */\nexport interface LLMResult {\n content: string;\n provider: LLMProvider;\n model: string;\n tier: LLMTier;\n tokens: { input: number; output: number; cacheRead?: number; cacheWrite?: number };\n latency: number;\n /** Number of attempts before success (1 = primary succeeded). */\n attempts: number;\n /** Monotonic request id from AI Gateway, if present in headers. */\n gatewayRequestId?: string;\n /**\n * Why generation stopped, normalized across providers. `'tool_use'` means\n * the model is requesting one or more tool calls (see {@link toolCalls}).\n */\n stopReason?: 'end' | 'tool_use' | 'max_tokens' | 'other';\n /**\n * Tool calls the model requested, normalized across providers. Present\n * (non-empty) when `stopReason === 'tool_use'`.\n */\n toolCalls?: LLMToolCall[];\n}\n\n/**\n * Environment bindings required by {@link complete}.\n *\n * `AI_GATEWAY_BASE_URL` is REQUIRED in 0.3.0. All provider calls flow through the\n * Cloudflare AI Gateway for unified logging, rate limiting, and cost telemetry.\n * In test/dev the caller may pass a custom fetch impl that short-circuits this.\n */\nexport interface LLMEnv {\n AI_GATEWAY_BASE_URL: string;\n ANTHROPIC_API_KEY: string;\n GROQ_API_KEY: string;\n /** Optional — only required for `{ tier: 'workbench' }` or `deepseek-*` model overrides. */\n DEEPSEEK_API_KEY?: string;\n /** Optional — only required when caller passes `{ model: 'grok-*' }` override. */\n GROK_API_KEY?: string;\n /**\n * Cost optimization: when true, `fast`-tier calls route to the self-hosted GPU\n * (qwen3 via the `custom-local-gpu` AI Gateway provider) FIRST, with the normal\n * fast route (Grok→Haiku) as automatic fallback on any error. Off by default →\n * fully dormant. Requires {@link LLMEnv.GPU_LLM_API_TOKEN}.\n */\n LLM_LOCAL_FIRST?: boolean;\n /**\n * Agentic-work opt-in: route the `workbench` tier to local Qwen 27B first,\n * with DeepSeek as the independent cloud fallback.\n */\n LLM_LOCAL_WORKBENCH?: boolean;\n /** Bearer for the local GPU proxy (`GPU_LLM_API_TOKEN` in Secret Manager). Required to enable local. */\n GPU_LLM_API_TOKEN?: string;\n /** Cloudflare Access service-token id for the local GPU app; forwarded by the gateway to the origin. */\n GPU_LLM_ACCESS_CLIENT_ID?: string;\n /** Cloudflare Access service-token secret for the local GPU app. */\n GPU_LLM_ACCESS_CLIENT_SECRET?: string;\n /**\n * GCP service-account JSON key (base64-encoded or raw) with `roles/aiplatform.user`.\n * The credential for the `gemini` leg in environments WITHOUT a metadata server\n * (Cloudflare Workers): the package exchanges it for a fresh access token per\n * isolate and caches it (see `./gcp-token.ts`).\n *\n * Optional. When neither this nor {@link LLMEnv.VERTEX_ACCESS_TOKEN} is set, the\n * leg falls back to Application Default Credentials — the GCP metadata server's\n * ambient service-account token (keyless; the default on Cloud Run / GCE). Off\n * GCP with no static credential, the metadata probe fails and the chain falls\n * through to its next leg.\n */\n GCP_SA_KEY?: string;\n /**\n * Pre-minted Google Cloud access token with `aiplatform.endpoints.predict`.\n * Explicit override, used only when {@link LLMEnv.GCP_SA_KEY} is absent and\n * before ADC is attempted. Valid for ~1 hour from minting, so it cannot be a\n * durable Worker secret.\n */\n VERTEX_ACCESS_TOKEN?: string;\n /** Vertex project. Defaults to the `GCP_SA_KEY`'s own `project_id`. */\n VERTEX_PROJECT?: string;\n /** Vertex location. Defaults to `us-central1`. */\n VERTEX_LOCATION?: string;\n /**\n * Optional KV store for org-level daily/monthly cost tracking and enforcement.\n * When provided alongside {@link LLMOptions.dailyCapUsd} or {@link LLMOptions.monthlyCapUsd},\n * `complete` will block calls that would exceed the declared cap.\n * Any KV-like store satisfying `get`/`put` works (e.g. Cloudflare KV, in-memory stub).\n */\n LLM_COST_KV?: CostKvStore;\n}\n\n/**\n * Minimal KV store interface for org-level LLM cost tracking.\n * Cloudflare KV satisfies this. An in-memory stub is sufficient for tests.\n */\nexport interface CostKvStore {\n get(key: string): Promise<string | null>;\n put(key: string, value: string, options?: { expirationTtl?: number }): Promise<void>;\n}\n\n/**\n * Caller-supplied context stamped on every metering row.\n * Mirrors the `LLMRecordContext` in `@latimer-woods-tech/llm-meter`; kept inline\n * to avoid a circular dependency (llm-meter imports llm).\n */\nexport interface LLMRecordContext {\n project: string;\n actor: string;\n runId?: string;\n workload?: string;\n tenantId?: string;\n}\n\n/**\n * Row shape passed to the optional {@link LLMDeps.onRecord} callback.\n * Callers can wire this directly to `recordCall` from `@latimer-woods-tech/llm-meter`.\n */\nexport interface LLMRecordRow extends LLMRecordContext {\n model: string;\n provider: LLMProvider;\n tier: LLMTier;\n inputTokens: number;\n outputTokens: number;\n cacheReadTokens: number;\n cacheWriteTokens: number;\n latencyMs: number;\n costUsd: number;\n yyyyMm: string;\n}\n\n/**\n * Optional dependencies for {@link complete}.\n */\nexport interface LLMDeps {\n fetch?: typeof fetch;\n logger?: Logger;\n now?: () => number;\n /**\n * Optional metering callback. Called after every successful completion.\n * Errors are swallowed so metering never blocks the caller.\n * Wire to `recordCall` from `@latimer-woods-tech/llm-meter`.\n */\n onRecord?: (row: LLMRecordRow) => Promise<void>;\n}\n\n// Model catalogue — keep in sync with docs/architecture/FACTORY_V1.md § LLM substrate.\nconst MODELS = {\n anthropic: {\n // `claude-haiku-4-20250514` was never a model Anthropic served — it 404s\n // (verified live against /v1/messages). Date suffixes are never appended to\n // an alias; the alias is `claude-haiku-4-5`, which resolves server-side to\n // `claude-haiku-4-5-20251001`. This is the `fast` tier's FALLBACK, so the\n // 404 stayed invisible for as long as the Grok primary held.\n fast: 'claude-haiku-4-5',\n balanced: 'claude-sonnet-4-6',\n smart: 'claude-opus-4-7',\n },\n gemini: {\n // `gemini-2.5-flash`, not `-pro`: the leg's job here is a fast, reliable,\n // JSON-returning fallback. Gemini 2.5 *Pro* mandates a thinking budget of\n // ≥128 tokens that is drawn from `maxOutputTokens` and CANNOT be disabled\n // (thinkingBudget=0 is rejected). On the render-runner's large judge /\n // generation prompts — and any low-`maxTokens` call (headline uses 40) —\n // the thinking phase exhausts the whole budget and Vertex returns 200 with\n // an empty candidate (finishReason MAX_TOKENS, no text), which the router\n // treats as a failed leg. Flash supports `thinkingBudget: 0` (set in\n // buildGeminiRequest), so text is always emitted. Both are Vertex-served.\n smart: 'gemini-2.5-flash',\n },\n groq: {\n // `llama-4-maverick` was NOT a model Groq serves — every `verifier` call\n // 404'd (the tier has no fallback, so it was hard-broken), and it was also\n // the `workbench` fallback. Groq's llama-4 offering is `scout`, which is\n // blocked at our org level; `llama-3.3-70b-versatile` is served and\n // unblocked (verified against /v1/models and a live completion).\n verifier: 'llama-3.3-70b-versatile',\n },\n grok: {\n fast: 'grok-4.3',\n },\n deepseek: {\n workbench: 'deepseek-chat',\n },\n local: {\n // Self-hosted qwen3:8b on the GPU box, reached via the `custom-local-gpu`\n // AI Gateway provider. The `fast` tier's optional zero-cost primary.\n fast: 'qwen3:8b',\n workbench: 'qwen3.6:27b',\n },\n} as const;\n\nconst DEFAULT_MAX_TOKENS = 1024;\nconst DEFAULT_TEMPERATURE = 0.7;\n/** Vertex region used when the caller does not pin one. */\nconst DEFAULT_VERTEX_LOCATION = 'us-central1';\nconst DEFAULT_LONG_CONTEXT_THRESHOLD = 150_000; // tokens\n\n// ─── Per-provider exponential backoff constants ────────────────────────────\n/** Base delay in ms for the first retry. */\nconst BACKOFF_BASE_MS = 500;\n/** Maximum backoff cap in ms. */\nconst BACKOFF_CAP_MS = 8_000;\n/** Max random jitter added to each backoff delay, in ms. */\nconst BACKOFF_JITTER_MAX_MS = 250;\n/** Maximum number of attempts per provider (1 initial + 2 retries). */\nconst PER_PROVIDER_MAX_ATTEMPTS = 3;\n\n// ─── Per-provider cooldown state (module-level) ────────────────────────────\n/**\n * Tracks when a provider's cooldown period expires.\n * Keyed by {@link LLMProvider}; value is the `Date.now()` epoch ms at which\n * the cooldown expires. Absent key means \"not cooling down\".\n */\nconst providerCooldownUntil: Map<LLMProvider, number> = new Map();\n\n/** Cooldown duration in ms after a provider exhausts all retries. */\nconst PROVIDER_COOLDOWN_MS = 30_000;\n\n/**\n * Returns `true` if the provider is currently in its cooldown window.\n * Uses the injected `now` function (or `Date.now`) for testability.\n */\nfunction isProviderCoolingDown(provider: LLMProvider, now: () => number = Date.now): boolean {\n const until = providerCooldownUntil.get(provider);\n if (until === undefined) return false;\n return now() < until;\n}\n\n/** Returns `YYYY-MM-DD` from a Unix timestamp (ms). Used for daily KV cost keys. */\nfunction isoDate(nowMs: number): string {\n return new Date(nowMs).toISOString().slice(0, 10);\n}\n\n/**\n * Record actual call spend in org-level daily/monthly KV buckets.\n * This is intentionally best-effort: Cloudflare KV does not provide an atomic\n * compare-and-swap, so concurrent requests can race and undercount spend.\n */\nasync function recordOrgCostUsage(\n kv: CostKvStore,\n todayKey: string,\n monthKey: string,\n costUsd: number,\n opts: LLMOptions,\n): Promise<void> {\n if (opts.dailyCapUsd !== undefined) {\n const raw = await kv.get(todayKey).catch(() => null);\n const spent = parseFloat(raw ?? '0');\n await kv.put(todayKey, String(spent + costUsd), { expirationTtl: 172_800 /* 48 h */ }).catch(() => undefined);\n }\n if (opts.monthlyCapUsd !== undefined) {\n const raw = await kv.get(monthKey).catch(() => null);\n const spent = parseFloat(raw ?? '0');\n await kv.put(monthKey, String(spent + costUsd), { expirationTtl: 3_456_000 /* 40 d */ }).catch(() => undefined);\n }\n}\n\n/**\n * USD cost per 1 million tokens for each model.\n * Source: Anthropic / Google / xAI pricing pages as of 2026-05.\n * Keep these model names in sync with the default routing constants in\n * {@link MODELS}; unknown models fall back to Opus rates (conservative upper bound).\n *\n * CANONICAL pricing source for the platform. `@latimer-woods-tech/llm-meter`\n * derives its cents-denominated rates from this table and a drift-guard test\n * there fails CI if they diverge — make all rate changes here.\n */\nexport const MODEL_PRICE_PER_1M: Record<string, { input: number; output: number; cacheRead: number; cacheWrite: number }> = {\n // Anthropic Haiku 4.5 — `claude-haiku-4-5` is the routed alias; the dated id\n // is what the API echoes back in `response.model`, so both must price.\n 'claude-haiku-4-5': { input: 0.80, output: 4.00, cacheRead: 0.08, cacheWrite: 1.00 },\n 'claude-haiku-4-5-20251001': { input: 0.80, output: 4.00, cacheRead: 0.08, cacheWrite: 1.00 },\n // Anthropic Sonnet 4\n 'claude-sonnet-4-20250514': { input: 3.00, output: 15.00, cacheRead: 0.30, cacheWrite: 3.75 },\n 'claude-sonnet-4-6': { input: 3.00, output: 15.00, cacheRead: 0.30, cacheWrite: 3.75 },\n // Anthropic Opus 4\n 'claude-opus-4-20250514': { input: 15.00, output: 75.00, cacheRead: 1.50, cacheWrite: 18.75 },\n 'claude-opus-4-7': { input: 15.00, output: 75.00, cacheRead: 1.50, cacheWrite: 18.75 },\n // Gemini 2.5 Flash — the routed `smart`/long-context model (JSON fallback leg).\n 'gemini-2.5-flash': { input: 0.30, output: 2.50, cacheRead: 0.075, cacheWrite: 0.30 },\n // Gemini 2.5 Pro — retained for historical ledger rows (was the routed model).\n 'gemini-2.5-pro': { input: 1.25, output: 10.00, cacheRead: 0.31, cacheWrite: 4.50 },\n // Groq Llama 3.3 70B Versatile (`verifier` tier). Groq has no prompt caching,\n // so cache rates are 0.00 — same convention as grok-4.3 below.\n 'llama-3.3-70b-versatile': { input: 0.59, output: 0.79, cacheRead: 0.00, cacheWrite: 0.00 },\n // Grok 4.3\n 'grok-4.3': { input: 1.25, output: 2.50, cacheRead: 0.00, cacheWrite: 0.00 },\n // DeepSeek API pricing as of 2026-05: cache-write conservatively uses cache-miss input pricing.\n 'deepseek-chat': { input: 0.27, output: 1.10, cacheRead: 0.07, cacheWrite: 0.27 },\n 'deepseek-reasoner': { input: 0.55, output: 2.19, cacheRead: 0.14, cacheWrite: 0.55 },\n // Deprecated aliases retained for historical ledger rows.\n 'grok-4-fast': { input: 1.25, output: 2.50, cacheRead: 0.00, cacheWrite: 0.00 },\n 'grok-3-mini-latest': { input: 1.25, output: 2.50, cacheRead: 0.00, cacheWrite: 0.00 },\n // Self-hosted qwen3 on the GPU box — zero marginal cost (electricity aside).\n 'qwen3:8b': { input: 0.0, output: 0.0, cacheRead: 0.0, cacheWrite: 0.0 },\n 'qwen3.6:27b': { input: 0.0, output: 0.0, cacheRead: 0.0, cacheWrite: 0.0 },\n};\n\n/** Fallback pricing used for unrecognised models (Opus rates — conservative upper bound). */\nconst PRICE_FALLBACK = MODEL_PRICE_PER_1M['claude-opus-4-7']!;\n\n/**\n * Estimates the USD cost of a single LLM completion from token counts.\n * Returns 0 for zero-token results. Uses {@link MODEL_PRICE_PER_1M} with\n * {@link PRICE_FALLBACK} for unknown models.\n */\nfunction estimateCostUsd(\n tokens: { input: number; output: number; cacheRead?: number; cacheWrite?: number },\n model: string,\n): number {\n const price = MODEL_PRICE_PER_1M[model] ?? PRICE_FALLBACK;\n return (\n (tokens.input * price.input +\n tokens.output * price.output +\n (tokens.cacheRead ?? 0) * price.cacheRead +\n (tokens.cacheWrite ?? 0) * price.cacheWrite) /\n 1_000_000\n );\n}\n\n/** Returns `YYYY-MM` from a Unix timestamp (ms). Used for monthly KV cost keys. */\nfunction isoMonth(nowMs: number): string {\n return new Date(nowMs).toISOString().slice(0, 7);\n}\n\n/**\n * Marks a provider as cooling down for {@link PROVIDER_COOLDOWN_MS} milliseconds.\n */\nfunction markProviderCoolingDown(provider: LLMProvider, now: () => number = Date.now): void {\n providerCooldownUntil.set(provider, now() + PROVIDER_COOLDOWN_MS);\n}\n\n/**\n * Clears the cooldown state for a provider after a successful call.\n */\nfunction clearProviderCooldown(provider: LLMProvider): void {\n providerCooldownUntil.delete(provider);\n}\n\n// ─── Legacy backoff constant (kept for the existing callWithBackoff signature) ─\nconst BASE_BACKOFF_MS = 250;\n\ninterface ProviderError {\n provider: LLMProvider;\n status: number;\n retryable: boolean;\n message: string;\n}\n\n/**\n * Returns `true` for status codes that should trigger a retry.\n * Only 429 and 5xx (transient server errors) qualify; other 4xx are terminal.\n */\nfunction isRetryableForBackoff(status: number): boolean {\n return status === 429 || (status >= 500 && status < 600);\n}\n\nfunction estimateTokens(messages: LLMMessage[], system?: string): number {\n // Cheap estimator: ~4 chars/token. Good enough for threshold routing.\n let chars = system?.length ?? 0;\n for (const m of messages) chars += contentToText(m.content).length;\n return Math.ceil(chars / 4);\n}\n\nfunction sleep(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n const t = setTimeout(resolve, ms);\n if (signal) {\n const onAbort = () => {\n clearTimeout(t);\n reject(new DOMException('Aborted', 'AbortError'));\n };\n if (signal.aborted) onAbort();\n else signal.addEventListener('abort', onAbort, { once: true });\n }\n });\n}\n\n/**\n * Computes the exponential backoff delay for a given attempt with jitter.\n *\n * Formula: `Math.min(base * 2^attempt + jitter, cap)`\n * where `jitter` is a random value in `[0, BACKOFF_JITTER_MAX_MS)`.\n *\n * @param attempt - Zero-based attempt index (0 = first retry after initial failure).\n */\nfunction computeBackoffMs(attempt: number): number {\n const jitter = Math.floor(Math.random() * BACKOFF_JITTER_MAX_MS);\n return Math.min(BACKOFF_BASE_MS * Math.pow(2, attempt) + jitter, BACKOFF_CAP_MS);\n}\n\n// ─── Provider request builders ─────────────────────────────────────────────\n\nfunction buildAnthropicRequest(\n model: string,\n messages: LLMMessage[],\n opts: LLMOptions,\n env: LLMEnv,\n streaming = false,\n): { url: string; headers: Record<string, string>; body: string } {\n const sys = systemText(opts, messages);\n const filtered = messages.filter((m) => m.role !== 'system');\n const body: Record<string, unknown> = {\n model,\n max_tokens: opts.maxTokens ?? DEFAULT_MAX_TOKENS,\n temperature: opts.temperature ?? DEFAULT_TEMPERATURE,\n messages: filtered.map((m) => ({ role: m.role, content: m.content })),\n };\n if (streaming) {\n body.stream = true;\n }\n if (sys) {\n const cache = opts.promptCache ?? sys.length >= 4096;\n body.system = cache\n ? [{ type: 'text', text: sys, cache_control: { type: 'ephemeral' } }]\n : sys;\n }\n if (opts.tools && opts.tools.length > 0) {\n body.tools = opts.tools.map((t) => ({\n name: t.name,\n description: t.description ?? '',\n input_schema: t.parameters,\n }));\n const tc = opts.toolChoice ?? 'auto';\n body.tool_choice =\n tc === 'auto'\n ? { type: 'auto' }\n : tc === 'none'\n ? { type: 'none' }\n : { type: 'tool', name: tc.name };\n }\n return {\n url: `${env.AI_GATEWAY_BASE_URL}/anthropic/v1/messages`,\n headers: {\n 'content-type': 'application/json',\n 'x-api-key': env.ANTHROPIC_API_KEY,\n 'anthropic-version': '2023-06-01',\n 'anthropic-beta': 'prompt-caching-2024-07-31',\n },\n body: JSON.stringify(body),\n };\n}\n\n/** A resolved Vertex credential: a bearer token plus (for ADC) the ambient project id. */\ninterface VertexAuth {\n token: string;\n /** Project id discovered from the metadata server (ADC path only). */\n project?: string;\n}\n\n/**\n * Resolve a Vertex credential, in priority order:\n * 1. `GCP_SA_KEY` — mint a fresh token from the service-account key (the\n * credential Cloudflare Workers supply, where there is no metadata server).\n * 2. `VERTEX_ACCESS_TOKEN` — a pre-minted token (legacy / CI override).\n * 3. Application Default Credentials — the GCP metadata server's ambient\n * service-account token. This is the KEYLESS default on Cloud Run / GCE:\n * neither env var need be set, mirroring the post-WIF-migration pattern\n * the render-runner already uses for every other GCP API call.\n *\n * @throws ValidationError when no static credential is set AND the metadata\n * server is unreachable (i.e. off-GCP) — the leg fails before the request so\n * the router falls through to the next provider, never calling Vertex\n * unauthenticated.\n */\nasync function resolveVertexAuth(env: LLMEnv, fetchImpl: typeof fetch): Promise<VertexAuth> {\n if (env.GCP_SA_KEY) {\n try {\n return { token: await mintGcpAccessToken(env.GCP_SA_KEY, fetchImpl) };\n } catch (error) {\n // A pre-minted token is worth trying if minting broke. With nothing to fall\n // back to, surface the minting failure rather than swallowing it.\n if (!env.VERTEX_ACCESS_TOKEN) throw error;\n }\n }\n if (env.VERTEX_ACCESS_TOKEN) return { token: env.VERTEX_ACCESS_TOKEN };\n // Keyless default: Application Default Credentials from the GCP metadata\n // server. Throws (→ chain falls through) when off-GCP.\n return fetchAdcAccessToken(fetchImpl);\n}\n\nfunction buildGeminiRequest(\n model: string,\n messages: LLMMessage[],\n opts: LLMOptions,\n env: LLMEnv,\n accessToken: string,\n adcProject?: string,\n): { url: string; headers: Record<string, string>; body: string } {\n const sys = systemText(opts, messages);\n const contents = messages\n .filter((m) => m.role !== 'system')\n .map((m) => ({\n role: m.role === 'assistant' ? 'model' : 'user',\n parts: [{ text: contentToText(m.content) }],\n }));\n const body: Record<string, unknown> = {\n contents,\n generationConfig: {\n maxOutputTokens: opts.maxTokens ?? DEFAULT_MAX_TOKENS,\n temperature: opts.temperature ?? DEFAULT_TEMPERATURE,\n // Disable \"thinking\": Gemini 2.5 draws thinking tokens from\n // maxOutputTokens, so on a large prompt (or a small token budget) the\n // model can spend the entire budget thinking and return an empty\n // candidate (finishReason MAX_TOKENS). This leg wants deterministic text\n // out, so thinking is turned off (supported by gemini-2.5-flash).\n thinkingConfig: { thinkingBudget: 0 },\n },\n };\n if (sys) {\n body.systemInstruction = { parts: [{ text: sys }] };\n }\n // Project resolution: an explicit VERTEX_PROJECT wins; else the service\n // account's own project (GCP_SA_KEY path); else the ambient project id the\n // metadata server reported (ADC path). So a Cloud Run job needs no Vertex\n // config at all — the runner's own project is used.\n const project =\n env.VERTEX_PROJECT ||\n (env.GCP_SA_KEY ? serviceAccountProjectId(env.GCP_SA_KEY) : (adcProject ?? ''));\n const location = env.VERTEX_LOCATION || DEFAULT_VERTEX_LOCATION;\n const path = `v1/projects/${project}/locations/${location}/publishers/google/models/${model}:generateContent`;\n return {\n url: `${env.AI_GATEWAY_BASE_URL}/google-vertex-ai/${path}`,\n headers: {\n 'content-type': 'application/json',\n authorization: `Bearer ${accessToken}`,\n },\n body: JSON.stringify(body),\n };\n}\n\n// ─── OpenAI-style (Grok / DeepSeek / Groq) tool-calling helpers ──────────────\n\n/**\n * Converts provider-agnostic messages to OpenAI chat-completions format,\n * translating the Anthropic-shaped tool blocks: `tool_use` → an assistant\n * message with `tool_calls`; `tool_result` → a standalone `tool` message keyed\n * by `tool_call_id`. Plain-string content passes through unchanged.\n */\nfunction toOpenAiMessages(messages: LLMMessage[], sys: string | undefined): Array<Record<string, unknown>> {\n const out: Array<Record<string, unknown>> = [];\n if (sys) out.push({ role: 'system', content: sys });\n for (const m of messages) {\n if (m.role === 'system') continue;\n if (typeof m.content === 'string') {\n out.push({ role: m.role, content: m.content });\n continue;\n }\n let text = '';\n const toolCalls: Array<Record<string, unknown>> = [];\n const results: Array<{ tool_use_id: string; content: string }> = [];\n for (const b of m.content) {\n if (b.type === 'text') text += b.text;\n else if (b.type === 'tool_use')\n toolCalls.push({ id: b.id, type: 'function', function: { name: b.name, arguments: JSON.stringify(b.input) } });\n else if (b.type === 'tool_result') results.push({ tool_use_id: b.tool_use_id, content: b.content });\n }\n if (results.length > 0) {\n for (const r of results) out.push({ role: 'tool', tool_call_id: r.tool_use_id, content: r.content });\n if (text) out.push({ role: 'user', content: text });\n } else if (toolCalls.length > 0) {\n out.push({ role: 'assistant', content: text || null, tool_calls: toolCalls });\n } else {\n out.push({ role: m.role, content: text });\n }\n }\n return out;\n}\n\n/** Builds the OpenAI `tools` array from {@link LLMOptions.tools}, or undefined. */\nfunction openAiTools(opts: LLMOptions): Array<Record<string, unknown>> | undefined {\n if (!opts.tools || opts.tools.length === 0) return undefined;\n return opts.tools.map((t) => ({\n type: 'function',\n function: { name: t.name, description: t.description ?? '', parameters: t.parameters },\n }));\n}\n\n/** Maps {@link LLMOptions.toolChoice} to the OpenAI `tool_choice` value. */\nfunction openAiToolChoice(tc: LLMOptions['toolChoice']): unknown {\n if (tc === undefined) return undefined;\n if (tc === 'auto' || tc === 'none') return tc;\n return { type: 'function', function: { name: tc.name } };\n}\n\nfunction buildGroqRequest(\n model: string,\n messages: LLMMessage[],\n opts: LLMOptions,\n env: LLMEnv,\n): { url: string; headers: Record<string, string>; body: string } {\n const sys = systemText(opts, messages);\n const merged: LLMMessage[] = [];\n if (sys) merged.push({ role: 'system', content: sys });\n for (const m of messages) if (m.role !== 'system') merged.push({ role: m.role, content: contentToText(m.content) });\n return {\n url: `${env.AI_GATEWAY_BASE_URL}/groq/openai/v1/chat/completions`,\n headers: {\n 'content-type': 'application/json',\n authorization: `Bearer ${env.GROQ_API_KEY}`,\n },\n body: JSON.stringify({\n model,\n max_tokens: opts.maxTokens ?? DEFAULT_MAX_TOKENS,\n temperature: opts.temperature ?? DEFAULT_TEMPERATURE,\n messages: merged,\n }),\n };\n}\n\nfunction buildGrokRequest(\n model: string,\n messages: LLMMessage[],\n opts: LLMOptions,\n env: LLMEnv,\n): { url: string; headers: Record<string, string>; body: string } {\n if (!env.GROK_API_KEY) {\n throw new ValidationError('GROK_API_KEY required for grok-* model override');\n }\n const sys = systemText(opts, messages);\n const body: Record<string, unknown> = {\n model,\n max_tokens: opts.maxTokens ?? DEFAULT_MAX_TOKENS,\n temperature: opts.temperature ?? DEFAULT_TEMPERATURE,\n messages: toOpenAiMessages(messages, sys),\n };\n const tools = openAiTools(opts);\n if (tools) {\n body.tools = tools;\n const tc = openAiToolChoice(opts.toolChoice ?? 'auto');\n if (tc !== undefined) body.tool_choice = tc;\n }\n if (model === MODELS.grok.fast) {\n body.reasoning_effort = opts.reasoningEffort ?? 'none';\n }\n return {\n url: `${env.AI_GATEWAY_BASE_URL}/grok/v1/chat/completions`,\n headers: {\n 'content-type': 'application/json',\n authorization: `Bearer ${env.GROK_API_KEY}`,\n },\n body: JSON.stringify(body),\n };\n}\n\nfunction buildDeepSeekRequest(\n model: string,\n messages: LLMMessage[],\n opts: LLMOptions,\n env: LLMEnv,\n): { url: string; headers: Record<string, string>; body: string } {\n if (!env.DEEPSEEK_API_KEY) {\n throw new ValidationError('DEEPSEEK_API_KEY required for workbench tier or deepseek-* model override');\n }\n const sys = systemText(opts, messages);\n const body: Record<string, unknown> = {\n model,\n max_tokens: opts.maxTokens ?? DEFAULT_MAX_TOKENS,\n temperature: opts.temperature ?? DEFAULT_TEMPERATURE,\n messages: toOpenAiMessages(messages, sys),\n };\n const tools = openAiTools(opts);\n if (tools) {\n body.tools = tools;\n const tc = openAiToolChoice(opts.toolChoice ?? 'auto');\n if (tc !== undefined) body.tool_choice = tc;\n }\n return {\n url: `${env.AI_GATEWAY_BASE_URL}/deepseek/chat/completions`,\n headers: {\n 'content-type': 'application/json',\n authorization: `Bearer ${env.DEEPSEEK_API_KEY}`,\n },\n body: JSON.stringify(body),\n };\n}\n\n/**\n * Builds an OpenAI-compatible request to the self-hosted GPU (qwen3) via the\n * `custom-local-gpu` Cloudflare AI Gateway provider. Auth is per-request and\n * forwarded by the gateway to the origin: a bearer for the proxy plus the CF\n * Access service-token headers (defense-in-depth). `/no_think` is appended only\n * for the 8B fast model. Qwen 27B keeps full reasoning for workbench tasks. Both\n * local models use the normalized OpenAI tool-call wire format.\n */\nfunction buildLocalRequest(\n model: string,\n messages: LLMMessage[],\n opts: LLMOptions,\n env: LLMEnv,\n): { url: string; headers: Record<string, string>; body: string } {\n if (!env.GPU_LLM_API_TOKEN) {\n throw new ValidationError('GPU_LLM_API_TOKEN required for the local provider');\n }\n const sys = systemText(opts, messages);\n const fastMode = model === MODELS.local.fast;\n const augmented = fastMode ? (sys ? `${sys} /no_think` : '/no_think') : sys;\n const headers: Record<string, string> = {\n 'content-type': 'application/json',\n authorization: `Bearer ${env.GPU_LLM_API_TOKEN}`,\n };\n if (env.GPU_LLM_ACCESS_CLIENT_ID) headers['CF-Access-Client-Id'] = env.GPU_LLM_ACCESS_CLIENT_ID;\n if (env.GPU_LLM_ACCESS_CLIENT_SECRET) headers['CF-Access-Client-Secret'] = env.GPU_LLM_ACCESS_CLIENT_SECRET;\n const tools = openAiTools(opts);\n return {\n url: `${env.AI_GATEWAY_BASE_URL}/custom-local-gpu/v1/chat/completions`,\n headers,\n body: JSON.stringify({\n model,\n max_tokens: opts.maxTokens ?? DEFAULT_MAX_TOKENS,\n temperature: opts.temperature ?? DEFAULT_TEMPERATURE,\n messages: toOpenAiMessages(messages, augmented),\n ...(tools\n ? { tools, tool_choice: openAiToolChoice(opts.toolChoice ?? 'auto') }\n : {}),\n }),\n };\n}\n\n// ─── Response parsers ──────────────────────────────────────────────────────\n\ninterface AnthropicResponse {\n content?: Array<{\n type: string;\n text?: string;\n id?: string;\n name?: string;\n input?: Record<string, unknown>;\n }>;\n stop_reason?: string;\n usage?: {\n input_tokens?: number;\n output_tokens?: number;\n cache_read_input_tokens?: number;\n cache_creation_input_tokens?: number;\n };\n model?: string;\n}\n\n/** Maps a provider stop reason to the normalized {@link LLMResult.stopReason}. */\nfunction normalizeAnthropicStop(reason: string | undefined): LLMResult['stopReason'] {\n switch (reason) {\n case 'end_turn':\n case 'stop_sequence':\n return 'end';\n case 'tool_use':\n return 'tool_use';\n case 'max_tokens':\n return 'max_tokens';\n default:\n return reason ? 'other' : undefined;\n }\n}\n\ninterface GeminiResponse {\n candidates?: Array<{ content?: { parts?: Array<{ text?: string }> } }>;\n usageMetadata?: {\n promptTokenCount?: number;\n candidatesTokenCount?: number;\n };\n}\n\ninterface GroqResponse {\n choices?: Array<{ message?: { content?: string } }>;\n usage?: { prompt_tokens?: number; completion_tokens?: number };\n model?: string;\n}\n\nfunction parseAnthropic(\n json: unknown,\n): {\n content: string;\n input: number;\n output: number;\n cacheRead: number;\n cacheWrite: number;\n model?: string;\n toolCalls?: LLMToolCall[];\n stopReason?: LLMResult['stopReason'];\n} {\n const r = json as AnthropicResponse;\n const toolCalls: LLMToolCall[] = (r.content ?? [])\n .filter((c) => c.type === 'tool_use' && typeof c.id === 'string' && typeof c.name === 'string')\n .map((c) => ({ id: c.id!, name: c.name!, arguments: c.input ?? {} }));\n return {\n content: r.content?.find((c) => c.type === 'text')?.text ?? '',\n input: r.usage?.input_tokens ?? 0,\n output: r.usage?.output_tokens ?? 0,\n cacheRead: r.usage?.cache_read_input_tokens ?? 0,\n cacheWrite: r.usage?.cache_creation_input_tokens ?? 0,\n model: r.model,\n toolCalls: toolCalls.length > 0 ? toolCalls : undefined,\n stopReason: normalizeAnthropicStop(r.stop_reason),\n };\n}\n\nfunction parseGemini(json: unknown): { content: string; input: number; output: number } {\n const r = json as GeminiResponse;\n const text =\n r.candidates?.[0]?.content?.parts?.map((p) => p.text ?? '').join('') ?? '';\n return {\n content: text,\n input: r.usageMetadata?.promptTokenCount ?? 0,\n output: r.usageMetadata?.candidatesTokenCount ?? 0,\n };\n}\n\nfunction parseGroq(json: unknown): { content: string; input: number; output: number; model?: string } {\n const r = json as GroqResponse;\n return {\n content: r.choices?.[0]?.message?.content ?? '',\n input: r.usage?.prompt_tokens ?? 0,\n output: r.usage?.completion_tokens ?? 0,\n model: r.model,\n };\n}\n\ninterface OpenAiResponse {\n choices?: Array<{\n message?: {\n content?: string | null;\n tool_calls?: Array<{ id?: string; function?: { name?: string; arguments?: string } }>;\n };\n finish_reason?: string;\n }>;\n usage?: { prompt_tokens?: number; completion_tokens?: number };\n model?: string;\n}\n\n/** Maps an OpenAI `finish_reason` to the normalized {@link LLMResult.stopReason}. */\nfunction normalizeOpenAiStop(reason: string | undefined): LLMResult['stopReason'] {\n switch (reason) {\n case 'stop':\n return 'end';\n case 'tool_calls':\n case 'function_call':\n return 'tool_use';\n case 'length':\n return 'max_tokens';\n default:\n return reason ? 'other' : undefined;\n }\n}\n\n/** Best-effort parse of an OpenAI tool-call arguments string; `{}` on failure. */\nfunction parseToolArgs(raw: string | undefined): Record<string, unknown> {\n if (!raw) return {};\n try {\n const v = JSON.parse(raw) as unknown;\n return typeof v === 'object' && v !== null ? (v as Record<string, unknown>) : {};\n } catch {\n return {};\n }\n}\n\n/**\n * Parses an OpenAI chat-completions response (Grok, DeepSeek), extracting\n * normalized tool calls and a stop reason in addition to text + tokens.\n */\nfunction parseOpenAi(json: unknown): {\n content: string;\n input: number;\n output: number;\n model?: string;\n toolCalls?: LLMToolCall[];\n stopReason?: LLMResult['stopReason'];\n} {\n const r = json as OpenAiResponse;\n const choice = r.choices?.[0];\n const toolCalls: LLMToolCall[] = (choice?.message?.tool_calls ?? [])\n .filter((c) => typeof c.function?.name === 'string')\n .map((c, i) => ({\n id: c.id ?? `call_${i}`,\n name: c.function!.name!,\n arguments: parseToolArgs(c.function?.arguments),\n }));\n return {\n content: choice?.message?.content ?? '',\n input: r.usage?.prompt_tokens ?? 0,\n output: r.usage?.completion_tokens ?? 0,\n model: r.model,\n toolCalls: toolCalls.length > 0 ? toolCalls : undefined,\n stopReason: normalizeOpenAiStop(choice?.finish_reason),\n };\n}\n\n// ─── Core call with backoff ────────────────────────────────────────────────\n\n/**\n * Calls a provider with per-provider exponential backoff.\n *\n * Retries up to {@link PER_PROVIDER_MAX_ATTEMPTS} times on 429 or transient 5xx.\n * Other 4xx codes are treated as terminal and not retried.\n * AbortError is never retried — it bubbles immediately.\n *\n * @param provider - Provider name, used for error tagging.\n * @param request - Pre-built HTTP request descriptor.\n * @param fetchImpl - Fetch implementation (injectable for tests).\n * @param signal - Optional AbortSignal for cancellation.\n * @param logger - Optional logger for per-attempt warnings.\n * @param nowFn - Optional clock injection for testability.\n * @returns Parsed JSON body, optional AI Gateway request ID, and attempt count.\n */\nasync function callWithBackoff(\n provider: LLMProvider,\n request: { url: string; headers: Record<string, string>; body: string },\n fetchImpl: typeof fetch,\n signal: AbortSignal | undefined,\n logger: Logger | undefined,\n nowFn?: () => number,\n): Promise<{ json: unknown; gatewayRequestId?: string; attempts: number }> {\n /**\n * Helper: mark provider cooling down and then throw the error.\n * Called whenever we determine we've exhausted all retries for the provider.\n * AbortError is never counted as a provider exhaustion — it bypasses this.\n */\n function exhaustAndThrow(err: ProviderError): never {\n markProviderCoolingDown(provider, nowFn ?? Date.now);\n throw err;\n }\n\n let lastErr: ProviderError | undefined;\n for (let attempt = 1; attempt <= PER_PROVIDER_MAX_ATTEMPTS; attempt++) {\n try {\n const response = await fetchImpl(request.url, {\n method: 'POST',\n headers: request.headers,\n body: request.body,\n signal,\n });\n if (!response.ok) {\n const text = await response.text().catch(() => '');\n const retryable = isRetryableForBackoff(response.status);\n const err: ProviderError = {\n provider,\n status: response.status,\n retryable,\n message: `${provider} ${String(response.status)}: ${text.slice(0, 300)}`,\n };\n logger?.warn?.('llm.provider.error', { provider, status: response.status, attempt });\n if (!err.retryable || attempt === PER_PROVIDER_MAX_ATTEMPTS) {\n if (err.retryable) exhaustAndThrow(err); // retryable but exhausted\n throw err; // terminal non-retryable error — no cooldown\n }\n lastErr = err;\n } else {\n const gatewayRequestId = response.headers.get('cf-aig-request-id') ?? undefined;\n clearProviderCooldown(provider);\n return { json: await response.json(), gatewayRequestId, attempts: attempt };\n }\n } catch (e) {\n if (e instanceof DOMException && e.name === 'AbortError') throw e;\n if (typeof e === 'object' && e !== null && 'retryable' in e) {\n const err = e as ProviderError;\n if (!err.retryable || attempt === PER_PROVIDER_MAX_ATTEMPTS) {\n if (err.retryable) exhaustAndThrow(err); // retryable but exhausted\n throw err; // terminal — no cooldown\n }\n lastErr = err;\n } else {\n const err: ProviderError = {\n provider,\n status: 0,\n retryable: true,\n message: e instanceof Error ? e.message : String(e),\n };\n if (attempt === PER_PROVIDER_MAX_ATTEMPTS) exhaustAndThrow(err);\n lastErr = err;\n }\n }\n // Exponential backoff with jitter: base=500ms, cap=8000ms, jitter up to 250ms\n const backoffMs = computeBackoffMs(attempt - 1);\n await sleep(backoffMs, signal);\n }\n // Fallthrough — should not be reached, but mark cooling down defensively.\n markProviderCoolingDown(provider, nowFn ?? Date.now);\n throw lastErr ?? ({ provider, status: 0, retryable: false, message: 'exhausted' } as ProviderError);\n}\n\nfunction isProviderError(err: unknown): err is ProviderError {\n return (\n typeof err === 'object' &&\n err !== null &&\n typeof (err as { status?: unknown }).status === 'number' &&\n typeof (err as { message?: unknown }).message === 'string' &&\n typeof (err as { provider?: unknown }).provider === 'string'\n );\n}\n\n// ─── Routing ───────────────────────────────────────────────────────────────\n\ninterface RoutePlan {\n primary: { provider: LLMProvider; model: string };\n fallback?: { provider: LLMProvider; model: string };\n}\n\n/**\n * Providers whose request builder + response parser support tool-calling.\n * When `opts.tools` is set, routing is restricted to this set and **fails\n * closed** rather than silently calling a provider that would ignore the\n * tools. Expanded in 1b as the other providers' tool formats are normalized.\n */\nconst TOOL_CAPABLE_PROVIDERS = new Set<LLMProvider>(['anthropic', 'grok', 'deepseek', 'local']);\n\nfunction plan(tier: LLMTier, opts: LLMOptions, tokenEstimate: number): RoutePlan {\n if (opts.model) {\n // Explicit override — best-effort provider detection.\n const m = opts.model;\n if (m.startsWith('claude')) return { primary: { provider: 'anthropic', model: m } };\n if (m.startsWith('gemini')) return { primary: { provider: 'gemini', model: m } };\n if (m.startsWith('grok')) return { primary: { provider: 'grok', model: m } };\n if (m.startsWith('deepseek')) return { primary: { provider: 'deepseek', model: m } };\n if (m.startsWith('qwen')) return { primary: { provider: 'local', model: m } };\n return { primary: { provider: 'groq', model: m } };\n }\n const longContext = tokenEstimate >= (opts.longContextThreshold ?? DEFAULT_LONG_CONTEXT_THRESHOLD);\n switch (tier) {\n case 'workbench':\n return {\n primary: { provider: 'deepseek', model: MODELS.deepseek.workbench },\n fallback: { provider: 'groq', model: MODELS.groq.verifier },\n };\n case 'verifier':\n return { primary: { provider: 'groq', model: MODELS.groq.verifier } };\n case 'smart':\n return longContext\n ? {\n primary: { provider: 'gemini', model: MODELS.gemini.smart },\n fallback: { provider: 'anthropic', model: MODELS.anthropic.smart },\n }\n : {\n primary: { provider: 'anthropic', model: MODELS.anthropic.smart },\n fallback: { provider: 'gemini', model: MODELS.gemini.smart },\n };\n case 'fast':\n return {\n primary: { provider: 'grok', model: MODELS.grok.fast },\n fallback: { provider: 'anthropic', model: MODELS.anthropic.fast },\n };\n case 'balanced':\n default:\n return longContext\n ? {\n primary: { provider: 'gemini', model: MODELS.gemini.smart },\n fallback: { provider: 'anthropic', model: MODELS.anthropic.balanced },\n }\n : {\n primary: { provider: 'anthropic', model: MODELS.anthropic.balanced },\n fallback: { provider: 'gemini', model: MODELS.gemini.smart },\n };\n }\n}\n\n/**\n * Build the `cf-aig-metadata` header value for the Cloudflare AI Gateway.\n *\n * Carries caller attribution (project / workload / actor / runId) so a single\n * shared gateway can be sliced per-app and per-feature in the AI Gateway\n * dashboard and logs. This replaces the per-app-gateway convention: rather than\n * one gateway per app (which has to be provisioned and silently 401s when it\n * isn't), one gateway tags every request with who made it.\n *\n * Returns `undefined` when no attribution fields are set (header omitted).\n * The CF AI Gateway accepts a JSON object of string/number/boolean values.\n */\nfunction buildAigMetadata(opts: LLMOptions): string | undefined {\n const meta: Record<string, string> = {};\n if (opts.project) meta.project = opts.project;\n if (opts.workload) meta.workload = opts.workload;\n if (opts.actor) meta.actor = opts.actor;\n if (opts.runId) meta.runId = opts.runId;\n return Object.keys(meta).length > 0 ? JSON.stringify(meta) : undefined;\n}\n\nasync function callOne(\n leg: { provider: LLMProvider; model: string },\n messages: LLMMessage[],\n opts: LLMOptions,\n env: LLMEnv,\n fetchImpl: typeof fetch,\n logger: Logger | undefined,\n nowFn?: () => number,\n): Promise<{ parsed: { content: string; input: number; output: number; cacheRead?: number; cacheWrite?: number; model?: string; toolCalls?: LLMToolCall[]; stopReason?: LLMResult['stopReason'] }; gatewayRequestId?: string; attempts: number }> {\n let req: { url: string; headers: Record<string, string>; body: string };\n switch (leg.provider) {\n case 'anthropic':\n req = buildAnthropicRequest(leg.model, messages, opts, env);\n break;\n case 'gemini': {\n // Credential resolution is a network call (mint from key, or the metadata\n // server for ADC), so it must happen here rather than inside the\n // (synchronous) request builders. Cached per isolate, so it is almost\n // always a no-op after the first gemini call.\n const vertexAuth = await resolveVertexAuth(env, fetchImpl);\n req = buildGeminiRequest(leg.model, messages, opts, env, vertexAuth.token, vertexAuth.project);\n break;\n }\n case 'groq':\n req = buildGroqRequest(leg.model, messages, opts, env);\n break;\n case 'grok':\n req = buildGrokRequest(leg.model, messages, opts, env);\n break;\n case 'deepseek':\n req = buildDeepSeekRequest(leg.model, messages, opts, env);\n break;\n case 'local':\n req = buildLocalRequest(leg.model, messages, opts, env);\n break;\n }\n // Attribution for the shared AI Gateway — one gateway, sliced per-app/feature.\n const aigMetadata = buildAigMetadata(opts);\n if (aigMetadata) req.headers['cf-aig-metadata'] = aigMetadata;\n const { json, gatewayRequestId, attempts } = await callWithBackoff(\n leg.provider,\n req,\n fetchImpl,\n opts.signal,\n logger,\n nowFn,\n );\n switch (leg.provider) {\n case 'anthropic':\n return { parsed: parseAnthropic(json), gatewayRequestId, attempts };\n case 'gemini':\n return { parsed: parseGemini(json), gatewayRequestId, attempts };\n case 'groq':\n return { parsed: parseGroq(json), gatewayRequestId, attempts };\n case 'grok':\n return { parsed: parseOpenAi(json), gatewayRequestId, attempts };\n case 'deepseek':\n return { parsed: parseOpenAi(json), gatewayRequestId, attempts };\n case 'local':\n return { parsed: parseOpenAi(json), gatewayRequestId, attempts };\n }\n}\n\n/**\n * Run a completion through the routing plan for the requested tier.\n *\n * Routing summary (0.3.0):\n * - `fast` → Grok 4.3; Anthropic Haiku fallback when Grok is unavailable\n * - `balanced` → Anthropic Sonnet; Gemini 2.5 Flash if `longContextThreshold` exceeded\n * - `smart` → Anthropic Opus; Gemini 2.5 Flash if long-context\n * - `verifier` → Groq Llama 3.3 70B (no fallback — verifier is inherently cheap/best-effort)\n * - `workbench` → DeepSeek Chat; Groq fallback for boring/reviewable internal batch jobs\n *\n * All provider traffic flows through Cloudflare AI Gateway at `AI_GATEWAY_BASE_URL`.\n *\n * Per-provider reliability guarantees (0.4.0):\n * - Exponential backoff with jitter on 429 / 5xx (base 500ms, cap 8s, up to 2 retries).\n * - Provider cooldown: after exhausting retries the provider is marked cooling down\n * for 30 seconds; subsequent calls skip it and go straight to the fallback leg.\n *\n * @param messages - Ordered chat history.\n * @param env - API key + gateway bindings.\n * @param opts - Optional tier/model/parameters override.\n * @param deps - Optional fetch/logger/clock injection (for testing).\n * @returns A {@link FactoryResponse} carrying either an {@link LLMResult} or\n * an error (`LLM_ALL_PROVIDERS_FAILED`, `LLM_RATE_LIMITED`, or `INTERNAL_ERROR`).\n */\nexport async function complete(\n messages: LLMMessage[],\n env: LLMEnv,\n opts: LLMOptions = {},\n deps: LLMDeps = {},\n): Promise<FactoryResponse<LLMResult>> {\n if (messages.length === 0) {\n throw new ValidationError('messages must not be empty');\n }\n if (!env.AI_GATEWAY_BASE_URL) {\n throw new ValidationError('AI_GATEWAY_BASE_URL is required in 0.3.0');\n }\n const fetchImpl = deps.fetch ?? fetch;\n const now = deps.now ?? (() => Date.now());\n const logger = deps.logger;\n const startedAt = now();\n\n const tier: LLMTier = opts.tier ?? 'balanced';\n const system = systemText(opts, messages);\n const tokenEstimate = estimateTokens(messages, system);\n let route = plan(tier, opts, tokenEstimate);\n // Cost optimization: prefer the self-hosted GPU (qwen3, $0) for cheap `fast`-tier\n // work, with the original fast route (Grok→Haiku) as automatic fallback on any\n // error. Dormant unless LLM_LOCAL_FIRST + GPU_LLM_API_TOKEN are set.\n if (\n env.LLM_LOCAL_FIRST &&\n tier === 'fast' &&\n !opts.model &&\n env.GPU_LLM_API_TOKEN\n ) {\n route = { primary: { provider: 'local', model: MODELS.local.fast }, fallback: route.primary };\n }\n // Qwen 27B owns local agentic work. DeepSeek remains the independent fallback;\n // explicit model overrides always win.\n if (\n env.LLM_LOCAL_WORKBENCH &&\n tier === 'workbench' &&\n !opts.model &&\n env.GPU_LLM_API_TOKEN\n ) {\n route = { primary: { provider: 'local', model: MODELS.local.workbench }, fallback: route.primary };\n }\n\n // ── Org-level daily / monthly cap pre-check ──────────────────────────────\n const kv = env.LLM_COST_KV;\n const todayKey = `llm:daily-cost:${isoDate(now())}`;\n const monthKey = `llm:monthly-cost:${isoMonth(now())}`;\n if (kv) {\n if (opts.dailyCapUsd !== undefined) {\n const raw = await kv.get(todayKey).catch(() => null);\n const spent = parseFloat(raw ?? '0');\n if (spent >= opts.dailyCapUsd) {\n return toErrorResponse(\n new RateLimitError('LLM_DAILY_CAP_EXCEEDED', {\n spentUsd: spent,\n dailyCapUsd: opts.dailyCapUsd,\n }),\n );\n }\n }\n if (opts.monthlyCapUsd !== undefined) {\n const raw = await kv.get(monthKey).catch(() => null);\n const spent = parseFloat(raw ?? '0');\n if (spent >= opts.monthlyCapUsd) {\n return toErrorResponse(\n new RateLimitError('LLM_MONTHLY_CAP_EXCEEDED', {\n spentUsd: spent,\n monthlyCapUsd: opts.monthlyCapUsd,\n }),\n );\n }\n }\n }\n\n const attemptLog: Array<{ provider: LLMProvider; status?: number; message: string }> = [];\n\n let routeLegs = [route.primary, route.fallback].filter(Boolean) as Array<{ provider: LLMProvider; model: string }>;\n // Tool-calling fails closed: never fall back to a provider that can't honour\n // the tool schema. Narrow the route to tool-capable providers when tools are set.\n if (opts.tools && opts.tools.length > 0) {\n routeLegs = routeLegs.filter((l) => TOOL_CAPABLE_PROVIDERS.has(l.provider));\n if (routeLegs.length === 0) {\n throw new ValidationError(\n `tool-calling requires a tool-capable provider (${[...TOOL_CAPABLE_PROVIDERS].join(', ')}); tier '${tier}' has none — use tier fast/balanced/smart or a claude-* model override`,\n );\n }\n }\n for (const [legIndex, leg] of routeLegs.entries()) {\n // Skip providers that are currently in their cooldown window.\n if (isProviderCoolingDown(leg.provider, now)) {\n logger?.warn?.('llm.provider.coolingDown', { provider: leg.provider });\n attemptLog.push({ provider: leg.provider, message: 'skipped: cooling down' });\n continue;\n }\n if (opts.signal?.aborted) {\n return toErrorResponse(\n new InternalError('llm call aborted', { provider: leg.provider, model: leg.model }),\n );\n }\n try {\n const result = await callOne(leg, messages, opts, env, fetchImpl, logger, now);\n // A tool_use turn legitimately has no text content — only treat a\n // genuinely empty response (no text AND no tool calls) as a failure.\n if (!result.parsed.content && !(result.parsed.toolCalls && result.parsed.toolCalls.length > 0)) {\n throw { provider: leg.provider, status: 200, retryable: false, message: 'empty content' } satisfies ProviderError;\n }\n logger?.info?.('llm.complete', {\n provider: leg.provider,\n model: leg.model,\n tier,\n tokenEstimate,\n attempts: result.attempts,\n runId: opts.runId,\n project: opts.project,\n actor: opts.actor,\n workload: opts.workload,\n });\n const llmResult: LLMResult = {\n content: result.parsed.content,\n provider: leg.provider,\n model: result.parsed.model ?? leg.model,\n tier,\n tokens: {\n input: result.parsed.input,\n output: result.parsed.output,\n cacheRead: result.parsed.cacheRead,\n cacheWrite: result.parsed.cacheWrite,\n },\n latency: now() - startedAt,\n attempts: result.attempts,\n gatewayRequestId: result.gatewayRequestId,\n stopReason: result.parsed.stopReason,\n toolCalls: result.parsed.toolCalls,\n };\n const costUsd = estimateCostUsd(llmResult.tokens, llmResult.model);\n if (opts.maxCostUsd !== undefined && costUsd > opts.maxCostUsd) {\n if (kv && (opts.dailyCapUsd !== undefined || opts.monthlyCapUsd !== undefined)) {\n await recordOrgCostUsage(kv, todayKey, monthKey, costUsd, opts);\n }\n return toErrorResponse(\n new RateLimitError('LLM_COST_CAP_EXCEEDED', {\n costUsd,\n maxCostUsd: opts.maxCostUsd,\n model: llmResult.model,\n tokens: llmResult.tokens,\n }),\n );\n }\n // ── Update org-level cost accumulators in KV ─────────────────────────\n // The KV writes are best-effort. Cloudflare KV does not support atomic\n // compare-and-swap, so concurrent increments may undercount spend.\n if (kv && (opts.dailyCapUsd !== undefined || opts.monthlyCapUsd !== undefined)) {\n await recordOrgCostUsage(kv, todayKey, monthKey, costUsd, opts);\n }\n // ── Metering callback ────────────────────────────────────────────────\n if (deps.onRecord && opts.ledger) {\n const row: LLMRecordRow = {\n ...opts.ledger,\n model: llmResult.model,\n provider: llmResult.provider,\n tier: llmResult.tier,\n inputTokens: llmResult.tokens.input,\n outputTokens: llmResult.tokens.output,\n cacheReadTokens: llmResult.tokens.cacheRead ?? 0,\n cacheWriteTokens: llmResult.tokens.cacheWrite ?? 0,\n latencyMs: llmResult.latency,\n costUsd,\n yyyyMm: isoMonth(now()),\n };\n deps.onRecord(row).catch((e: unknown) => {\n logger?.warn?.('llm.onRecord.error', { message: e instanceof Error ? e.message : String(e) });\n });\n }\n return { data: llmResult, error: null };\n } catch (e) {\n if (e instanceof DOMException && e.name === 'AbortError') {\n return toErrorResponse(\n new InternalError('llm call aborted', { provider: leg.provider, model: leg.model }),\n );\n }\n if (isProviderError(e)) {\n attemptLog.push({ provider: e.provider, status: e.status, message: e.message });\n if (e.status === 429 && legIndex === routeLegs.length - 1) {\n return toErrorResponse(\n new RateLimitError(`llm rate limited on ${e.provider}`, { attempts: attemptLog }),\n );\n }\n logger?.warn?.('llm.leg.failed', { provider: leg.provider, status: e.status });\n continue;\n }\n attemptLog.push({ provider: leg.provider, message: e instanceof Error ? e.message : String(e) });\n }\n }\n\n return toErrorResponse(\n new InternalError('LLM_ALL_PROVIDERS_FAILED', { attempts: attemptLog, tier, tokenEstimate }),\n );\n}\n\n// ─── Streaming ────────────────────────────────────────────────────────────\n\n/**\n * Anthropic server-sent event shapes used by the streaming parser.\n * Only the fields we consume are typed; the rest are ignored.\n */\ninterface AnthropicStreamEvent {\n type: string;\n index?: number;\n delta?: { type?: string; text?: string; partial_json?: string; stop_reason?: string };\n content_block?: { type?: string; id?: string; name?: string };\n message?: {\n usage?: {\n input_tokens?: number;\n output_tokens?: number;\n cache_read_input_tokens?: number;\n cache_creation_input_tokens?: number;\n };\n model?: string;\n };\n usage?: {\n input_tokens?: number;\n output_tokens?: number;\n };\n}\n\n/**\n * Streams a completion from the primary Anthropic provider, yielding text chunks\n * as they arrive. Falls back to the non-streaming {@link complete} function when\n * the provider does not support streaming (i.e. a non-Anthropic primary is selected).\n *\n * The generator's **return value** (accessible via `gen.return()` or by consuming\n * the full iteration) is an {@link LLMResult} with the same shape as {@link complete}.\n *\n * Usage pattern:\n * ```ts\n * const gen = completionStream(messages, env, opts);\n * for await (const chunk of gen) {\n * // stream chunk to client\n * }\n * const result = (await gen.return(undefined)).value; // LLMResult\n * ```\n *\n * @param messages - Ordered chat history.\n * @param env - API key + gateway bindings.\n * @param opts - Optional tier/model/parameters override. Accepts `deps` as nested field.\n * @returns An async generator that yields `string` chunks and returns an {@link LLMResult}.\n */\nexport async function* completionStream(\n messages: LLMMessage[],\n env: LLMEnv,\n opts: LLMOptions & { deps?: LLMDeps } = {},\n): AsyncGenerator<string, LLMResult, unknown> {\n if (messages.length === 0) {\n throw new ValidationError('messages must not be empty');\n }\n if (!env.AI_GATEWAY_BASE_URL) {\n throw new ValidationError('AI_GATEWAY_BASE_URL is required in 0.3.0');\n }\n\n const deps: LLMDeps = opts.deps ?? {};\n const fetchImpl = deps.fetch ?? fetch;\n const now = deps.now ?? (() => Date.now());\n const logger = deps.logger;\n const startedAt = now();\n\n const tier: LLMTier = opts.tier ?? 'balanced';\n const system = systemText(opts, messages);\n const tokenEstimate = estimateTokens(messages, system);\n const route = plan(tier, opts, tokenEstimate);\n const streamLeg =\n route.primary.provider === 'grok' && !env.GROK_API_KEY && route.fallback?.provider === 'anthropic'\n ? route.fallback\n : route.primary;\n\n // Only Anthropic supports streaming in the current implementation.\n // For all other primaries, fall back to non-streaming complete().\n if (streamLeg.provider !== 'anthropic') {\n const result = await complete(messages, env, opts, deps);\n if (result.error !== null || result.data === null) {\n throw new InternalError('LLM_ALL_PROVIDERS_FAILED', { error: result.error });\n }\n yield result.data.content;\n return result.data;\n }\n\n // Check cooldown before attempting the streaming call.\n if (isProviderCoolingDown(streamLeg.provider, now)) {\n logger?.warn?.('llm.provider.coolingDown', { provider: streamLeg.provider });\n // Fall back to non-streaming complete() which will handle the fallback leg.\n const result = await complete(messages, env, opts, deps);\n if (result.error !== null || result.data === null) {\n throw new InternalError('LLM_ALL_PROVIDERS_FAILED', { error: result.error });\n }\n yield result.data.content;\n return result.data;\n }\n\n const req = buildAnthropicRequest(streamLeg.model, messages, opts, env, true);\n // Attribution for the shared AI Gateway (matches the non-streaming path).\n const streamAigMetadata = buildAigMetadata(opts);\n if (streamAigMetadata) req.headers['cf-aig-metadata'] = streamAigMetadata;\n\n let response: Response;\n try {\n response = await fetchImpl(req.url, {\n method: 'POST',\n headers: req.headers,\n body: req.body,\n // Fall back to a 60 s default when the caller provides no signal — prevents\n // a hung provider connection from consuming the Worker's wall-clock budget.\n signal: opts.signal ?? AbortSignal.timeout(60_000),\n });\n } catch (e) {\n if (e instanceof DOMException && e.name === 'AbortError') {\n throw new InternalError('llm call aborted', {\n provider: streamLeg.provider,\n model: streamLeg.model,\n });\n }\n throw new InternalError('llm stream fetch failed', {\n message: e instanceof Error ? e.message : String(e),\n });\n }\n\n if (!response.ok) {\n const text = await response.text().catch(() => '');\n const retryable = isRetryableForBackoff(response.status);\n if (retryable && response.status === 429) {\n markProviderCoolingDown(streamLeg.provider, now);\n }\n // Fall back to non-streaming complete() which will try the fallback leg.\n const result = await complete(messages, env, opts, deps);\n if (result.error !== null || result.data === null) {\n throw new InternalError('LLM_ALL_PROVIDERS_FAILED', {\n streamError: `${streamLeg.provider} ${String(response.status)}: ${text.slice(0, 300)}`,\n error: result.error,\n });\n }\n yield result.data.content;\n return result.data;\n }\n\n if (!response.body) {\n throw new InternalError('llm stream response body is null', {\n provider: streamLeg.provider,\n });\n }\n\n // Stream SSE events from Anthropic.\n const decoder = new TextDecoder();\n let accumulatedText = '';\n let inputTokens = 0;\n let outputTokens = 0;\n let cacheRead = 0;\n let cacheWrite = 0;\n let modelName: string | undefined;\n // Tool-use blocks arrive as content_block_start (id/name) then a sequence of\n // input_json_delta fragments that concatenate into the arguments JSON string.\n const toolBlocks = new Map<number, { id: string; name: string; json: string }>();\n let streamStopReason: LLMResult['stopReason'];\n const gatewayRequestId: string | undefined = response.headers.get('cf-aig-request-id') ?? undefined;\n\n const reader = response.body.getReader();\n let buffer = '';\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n buffer += decoder.decode(value, { stream: true });\n\n // SSE lines are delimited by '\\n'. Events are separated by '\\n\\n'.\n const lines = buffer.split('\\n');\n // Keep the last (potentially incomplete) line in the buffer.\n buffer = lines.pop() ?? '';\n\n for (const line of lines) {\n if (!line.startsWith('data: ')) continue;\n const data = line.slice(6).trim();\n if (data === '[DONE]') break;\n let event: AnthropicStreamEvent;\n try {\n event = JSON.parse(data) as AnthropicStreamEvent;\n } catch {\n continue; // Skip malformed SSE lines.\n }\n\n switch (event.type) {\n case 'message_start':\n inputTokens = event.message?.usage?.input_tokens ?? 0;\n cacheRead = event.message?.usage?.cache_read_input_tokens ?? 0;\n cacheWrite = event.message?.usage?.cache_creation_input_tokens ?? 0;\n modelName = event.message?.model;\n break;\n case 'content_block_start':\n if (\n event.content_block?.type === 'tool_use' &&\n typeof event.index === 'number' &&\n typeof event.content_block.id === 'string' &&\n typeof event.content_block.name === 'string'\n ) {\n toolBlocks.set(event.index, { id: event.content_block.id, name: event.content_block.name, json: '' });\n }\n break;\n case 'content_block_delta':\n if (event.delta?.type === 'text_delta' && typeof event.delta.text === 'string') {\n accumulatedText += event.delta.text;\n yield event.delta.text;\n } else if (\n event.delta?.type === 'input_json_delta' &&\n typeof event.delta.partial_json === 'string' &&\n typeof event.index === 'number'\n ) {\n const block = toolBlocks.get(event.index);\n if (block) block.json += event.delta.partial_json;\n }\n break;\n case 'message_delta':\n outputTokens = event.usage?.output_tokens ?? outputTokens;\n if (event.delta?.stop_reason) streamStopReason = normalizeAnthropicStop(event.delta.stop_reason);\n break;\n default:\n break;\n }\n }\n }\n } finally {\n reader.releaseLock();\n }\n\n clearProviderCooldown(streamLeg.provider);\n logger?.info?.('llm.completionStream', {\n provider: streamLeg.provider,\n model: streamLeg.model,\n tier,\n tokenEstimate,\n runId: opts.runId,\n project: opts.project,\n actor: opts.actor,\n workload: opts.workload,\n });\n\n const toolCalls: LLMToolCall[] = [...toolBlocks.values()].map((b) => ({\n id: b.id,\n name: b.name,\n arguments: parseToolArgs(b.json),\n }));\n\n return {\n content: accumulatedText,\n provider: streamLeg.provider,\n model: modelName ?? streamLeg.model,\n tier,\n tokens: { input: inputTokens, output: outputTokens, cacheRead, cacheWrite },\n latency: now() - startedAt,\n attempts: 1,\n gatewayRequestId,\n stopReason: streamStopReason,\n toolCalls: toolCalls.length > 0 ? toolCalls : undefined,\n };\n}\n\n// ─── Grounding assertion ───────────────────────────────────────────────────\n\n/**\n * Returns `true` if `response` contains at least one verbatim phrase of at\n * least 5 consecutive whitespace-delimited tokens that also appears in one of\n * the `sources` strings.\n *\n * Returns `true` unconditionally when `sources` is empty (no grounding\n * documents means grounding cannot be violated).\n *\n * This is a lightweight guard for RAG pipelines — it detects obvious\n * hallucinations where the model generates content not present in any\n * retrieved source. It is NOT a semantic similarity check.\n *\n * @param response - The LLM-generated text to inspect.\n * @param sources - Retrieved source documents to check against.\n * @returns `true` if the response is grounded, `false` if hallucination detected.\n *\n * @example\n * ```ts\n * const grounded = assertGrounding(llmAnswer, retrievedDocs);\n * if (!grounded) {\n * // flag or re-rank the response\n * }\n * ```\n */\nexport function assertGrounding(response: string, sources: string[]): boolean {\n if (sources.length === 0) return true;\n\n const WINDOW = 5;\n const responseTokens = response.split(/\\s+/).filter((t) => t.length > 0);\n\n if (responseTokens.length < WINDOW) return false;\n\n // Build a set of all 5-token ngrams from each source for O(n) lookup.\n const sourceNgrams = new Set<string>();\n for (const source of sources) {\n const tokens = source.split(/\\s+/).filter((t) => t.length > 0);\n for (let i = 0; i <= tokens.length - WINDOW; i++) {\n const ngram = tokens.slice(i, i + WINDOW).join(' ');\n sourceNgrams.add(ngram);\n }\n }\n\n if (sourceNgrams.size === 0) return false;\n\n // Slide a window of WINDOW tokens over the response and check for a match.\n for (let i = 0; i <= responseTokens.length - WINDOW; i++) {\n const ngram = responseTokens.slice(i, i + WINDOW).join(' ');\n if (sourceNgrams.has(ngram)) return true;\n }\n\n return false;\n}\n\n// ─── Exported helpers (kept for existing consumers) ───────────────────────\n\nexport { MODELS, isProviderCoolingDown, markProviderCoolingDown, clearProviderCooldown, PROVIDER_COOLDOWN_MS };\nexport { BASE_BACKOFF_MS };\n\n// ─── Embeddings ────────────────────────────────────────────────────────────\n\nexport { embed, embedLocal, DEFAULT_EMBEDDING_MODEL, LOCAL_EMBEDDING_MODEL } from './embed.js';\nexport type { AiBinding, EmbedResult, EmbeddingModel, LocalEmbedEnv } from './embed.js';\n\nexport { mintGcpAccessToken, clearGcpTokenCache, serviceAccountProjectId } from './gcp-token.js';\n","/**\n * Mint short-lived Google Cloud OAuth2 access tokens from a service-account key,\n * inside a Cloudflare Worker.\n *\n * The `gemini` leg authenticates to Vertex AI with a GCP access token, which\n * expires after one hour. A token therefore cannot be a durable Worker secret —\n * any stored value is stale almost immediately. Callers supply the long-lived\n * service-account key instead, and this module exchanges it for a fresh token\n * via the JWT-bearer flow, caching the result per isolate.\n *\n * Web Crypto only — no Node.js built-ins, no `Buffer`. Lifted from\n * `apps/admin-studio/src/lib/gcp-secrets.ts`, where this exact flow has been\n * running in production; hoisted here so every consumer of the `gemini` leg\n * gets a working credential rather than just the one app that hand-rolled it.\n */\nimport { InternalError, ValidationError } from '@latimer-woods-tech/errors';\n\n/** The subset of a GCP service-account JSON key needed for the JWT-bearer flow. */\ninterface ServiceAccountKey {\n project_id: string;\n private_key: string;\n client_email: string;\n token_uri: string;\n}\n\ninterface OAuth2Response {\n access_token: string;\n expires_in: number;\n}\n\n/** Refresh this long before expiry so an in-flight request never races the clock. */\nconst EXPIRY_SKEW_MS = 300_000;\n\n/**\n * Per-isolate token cache, keyed by service-account email.\n *\n * Caching a *string* across requests is safe. Caching an I/O object (a socket, a\n * database client, a `Response`) is not — Workers rejects reuse of I/O created\n * on behalf of a different request. This holds only the token text and its\n * expiry, so it is reused freely.\n */\nconst tokenCache = new Map<string, { token: string; expiresAt: number }>();\n\n/** Decode a base64url-encoded service-account key, tolerating raw JSON too. */\nfunction parseServiceAccountKey(gcpSaKey: string): ServiceAccountKey {\n const text = gcpSaKey.trimStart().startsWith('{') ? gcpSaKey : atob(gcpSaKey);\n const key = JSON.parse(text) as ServiceAccountKey;\n if (!key.client_email || !key.private_key || !key.token_uri) {\n throw new ValidationError('GCP_SA_KEY is missing client_email, private_key, or token_uri');\n }\n return key;\n}\n\n/** Encode bytes as base64url (RFC 4648 §5): `-`/`_` for `+`/`/`, no padding. */\nfunction base64UrlEncode(bytes: Uint8Array): string {\n let binary = '';\n for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]!);\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/g, '');\n}\n\n/** Import a PEM-encoded PKCS#8 private key as a Web Crypto signing key. */\nasync function importPrivateKey(pem: string): Promise<CryptoKey> {\n const body = pem\n .split('\\n')\n .filter((line) => !line.startsWith('-----'))\n .join('');\n const binary = atob(body);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);\n return crypto.subtle.importKey(\n 'pkcs8',\n bytes.buffer,\n { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' },\n false,\n ['sign'],\n );\n}\n\n/** Build a service-account JWT assertion signed with RS256, per GCP's spec. */\nasync function createAssertion(key: ServiceAccountKey, nowSeconds: number): Promise<string> {\n const encoder = new TextEncoder();\n const header = base64UrlEncode(encoder.encode(JSON.stringify({ alg: 'RS256', typ: 'JWT' })));\n const payload = base64UrlEncode(\n encoder.encode(\n JSON.stringify({\n iss: key.client_email,\n scope: 'https://www.googleapis.com/auth/cloud-platform',\n aud: key.token_uri,\n exp: nowSeconds + 3600,\n iat: nowSeconds,\n }),\n ),\n );\n const signingInput = `${header}.${payload}`;\n const cryptoKey = await importPrivateKey(key.private_key);\n const signature = await crypto.subtle.sign(\n 'RSASSA-PKCS1-v1_5',\n cryptoKey,\n encoder.encode(signingInput).buffer,\n );\n return `${signingInput}.${base64UrlEncode(new Uint8Array(signature))}`;\n}\n\n/**\n * Exchange a service-account key for a cloud-platform access token, reusing a\n * cached token until it is within {@link EXPIRY_SKEW_MS} of expiring.\n *\n * @param gcpSaKey - Service-account JSON key, base64-encoded or raw.\n * @param fetchImpl - Fetch implementation (injectable for tests).\n * @returns A bearer token valid for Vertex AI.\n * @throws ValidationError when the key is malformed; InternalError when the exchange fails.\n */\nexport async function mintGcpAccessToken(gcpSaKey: string, fetchImpl: typeof fetch): Promise<string> {\n const key = parseServiceAccountKey(gcpSaKey);\n\n const now = Date.now();\n const cached = tokenCache.get(key.client_email);\n if (cached && cached.expiresAt - EXPIRY_SKEW_MS > now) return cached.token;\n\n const assertion = await createAssertion(key, Math.floor(now / 1000));\n const response = await fetchImpl(key.token_uri, {\n method: 'POST',\n headers: { 'content-type': 'application/x-www-form-urlencoded' },\n body: new URLSearchParams({\n grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',\n assertion,\n }).toString(),\n }).catch((cause: unknown) => {\n throw new InternalError(`GCP token exchange failed: ${String(cause)}`);\n });\n\n if (!response.ok) {\n throw new InternalError(`GCP token exchange returned ${response.status}`);\n }\n\n const data = (await response.json()) as OAuth2Response;\n if (!data.access_token) {\n throw new InternalError('GCP token exchange returned no access_token');\n }\n\n // Trust the server's TTL rather than assuming the 3600s default.\n const ttlMs = (data.expires_in > 0 ? data.expires_in : 3600) * 1000;\n tokenCache.set(key.client_email, { token: data.access_token, expiresAt: now + ttlMs });\n return data.access_token;\n}\n\n// ─── Application Default Credentials (GCP metadata server) ───────────────────\n/**\n * GCE / Cloud Run metadata server base. Reachable only from GCP compute; the\n * hostname does not resolve elsewhere, so an off-GCP probe fails fast.\n */\nconst METADATA_BASE = 'http://metadata.google.internal/computeMetadata/v1';\n/** Required header for every metadata-server request (anti-SSRF guard). */\nconst METADATA_HEADER = { 'Metadata-Flavor': 'Google' } as const;\n/**\n * Bound the metadata probe so a non-GCP environment (where the host may hang\n * rather than refuse) falls through to the next provider quickly.\n */\nconst METADATA_TIMEOUT_MS = 2_000;\n\n/** Per-isolate cache for the ADC (metadata-server) credential. */\nlet adcCache: { token: string; project?: string; expiresAt: number } | undefined;\n\ninterface AdcCredential {\n token: string;\n /** The instance's own GCP project id, as reported by the metadata server. */\n project?: string;\n}\n\n/**\n * Obtain an access token (and project id) from the GCP metadata server using\n * Application Default Credentials — the keyless credential every Cloud Run /\n * GCE workload already has via its attached service account. No key, no env\n * var: the instance SA's cloud-platform token is served by the metadata\n * server. This mirrors the render-runner's own `_meta_token` shell helper.\n *\n * Fails (throws {@link InternalError}) when the metadata server is unreachable\n * or returns no token — i.e. off-GCP — so the caller falls through to the next\n * provider leg. Never throws a bare `AbortError` (which the router treats as a\n * hard cancellation): an internal timeout is surfaced as an `InternalError`.\n *\n * @param fetchImpl - Fetch implementation (injectable for tests).\n * @returns A bearer token valid for Vertex AI plus the ambient project id.\n */\nexport async function fetchAdcAccessToken(fetchImpl: typeof fetch): Promise<AdcCredential> {\n const now = Date.now();\n if (adcCache && adcCache.expiresAt - EXPIRY_SKEW_MS > now) {\n return { token: adcCache.token, project: adcCache.project };\n }\n\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), METADATA_TIMEOUT_MS);\n try {\n const tokenRes = await fetchImpl(\n `${METADATA_BASE}/instance/service-accounts/default/token`,\n { headers: METADATA_HEADER, signal: controller.signal },\n );\n if (!tokenRes.ok) {\n throw new InternalError(`GCP metadata token endpoint returned ${tokenRes.status}`);\n }\n const data = (await tokenRes.json()) as OAuth2Response;\n if (!data.access_token) {\n throw new InternalError('GCP metadata token endpoint returned no access_token');\n }\n\n // Project id is best-effort: a caller may pin VERTEX_PROJECT instead.\n let project: string | undefined;\n try {\n const projRes = await fetchImpl(`${METADATA_BASE}/project/project-id`, {\n headers: METADATA_HEADER,\n signal: controller.signal,\n });\n if (projRes.ok) project = (await projRes.text()).trim() || undefined;\n } catch {\n /* project id is optional — the token is still usable */\n }\n\n const ttlMs = (data.expires_in > 0 ? data.expires_in : 3600) * 1000;\n adcCache = { token: data.access_token, project, expiresAt: now + ttlMs };\n return { token: data.access_token, project };\n } catch (cause) {\n // Normalize an internal-timeout AbortError into an InternalError so the\n // router falls through to the next leg instead of hard-cancelling the call.\n if (cause instanceof InternalError) throw cause;\n throw new InternalError(`GCP metadata credential unavailable: ${String(cause)}`);\n } finally {\n clearTimeout(timer);\n }\n}\n\n/** Reset the per-isolate token cache. Tests only. */\nexport function clearGcpTokenCache(): void {\n tokenCache.clear();\n adcCache = undefined;\n}\n\n/** The project the service account belongs to — the Vertex project by default. */\nexport function serviceAccountProjectId(gcpSaKey: string): string {\n return parseServiceAccountKey(gcpSaKey).project_id;\n}\n","/**\n * Workers AI embedding API.\n *\n * Pins to bge-base-en-v1.5 (768-dim cosine) — the platform standard.\n * @remarks Local-rail errors use @latimer-woods-tech/errors (observability ratchet).\n * model_version is returned with every result so callers can record it\n * for provenance-aware re-embed when the model is swapped.\n *\n * Contract: inject the Workers AI binding (env.AI) — never import a vendor SDK.\n */\n\nimport { InternalError } from '@latimer-woods-tech/errors';\n\n/** The platform-standard embedding model (768-dim cosine) used when no override is given. */\nexport const DEFAULT_EMBEDDING_MODEL = '@cf/baai/bge-base-en-v1.5';\n\n/** Supported embedding model identifiers — widen deliberately, since dims are part of the contract. */\nexport type EmbeddingModel = '@cf/baai/bge-base-en-v1.5';\n\n/** One embedding call's output: the vectors plus the provenance needed to re-embed later. */\nexport interface EmbedResult {\n vectors: number[][];\n model: string;\n dims: number;\n}\n\n/** Minimal Workers AI binding shape needed for embeddings. */\nexport interface AiBinding {\n run(\n model: string,\n inputs: { text: string | string[] },\n ): Promise<{ data: number[][] }>;\n}\n\n/**\n * Embeds one or more text strings using the Workers AI binding.\n *\n * @param ai - Workers AI binding (`env.AI`) — injected, not imported.\n * @param input - Single string or array of strings to embed.\n * @param opts - Optional model override (must be a supported EmbeddingModel).\n * @returns Embedding vectors, model name, and dimension count.\n *\n * @throws When the AI binding call fails (let the caller decide whether to catch).\n */\nexport async function embed(\n ai: AiBinding,\n input: string | string[],\n opts?: { model?: EmbeddingModel },\n): Promise<EmbedResult> {\n const model = opts?.model ?? DEFAULT_EMBEDDING_MODEL;\n const texts = Array.isArray(input) ? input : [input];\n const result = await ai.run(model, { text: texts });\n const vectors = result.data;\n if (!vectors || vectors.length === 0) {\n throw new Error(`embed(): Workers AI returned no vectors for model ${model}`);\n }\n return {\n vectors,\n model,\n dims: vectors[0]!.length,\n };\n}\n\n/**\n * Self-hosted GPU embedding model (nomic-embed-text, 768-dim), reached through\n * the same CF AI Gateway custom provider as the local chat provider.\n *\n * ⚠️ VECTOR-SPACE WARNING: nomic-embed-text and bge-base-en-v1.5 are BOTH 768-dim\n * but occupy DIFFERENT embedding spaces. Their vectors are NOT comparable. A store\n * built with one model must be QUERIED — and, when migrating, RE-EMBEDDED — with\n * the SAME model. Never write vectors from both models into one store.\n */\nexport const LOCAL_EMBEDDING_MODEL = 'nomic-embed-text';\n\n/** Env subset needed to reach the local embedding rail (shares GPU_LLM_* with the chat provider). */\nexport interface LocalEmbedEnv {\n AI_GATEWAY_BASE_URL: string;\n GPU_LLM_API_TOKEN?: string;\n GPU_LLM_ACCESS_CLIENT_ID?: string;\n GPU_LLM_ACCESS_CLIENT_SECRET?: string;\n}\n\n/**\n * Embeds text on the self-hosted GPU rail (nomic-embed-text) via the AI Gateway\n * custom provider — the $0 alternative to Workers AI {@link embed}.\n *\n * Unlike the local CHAT provider, this has NO cross-provider fallback ON PURPOSE:\n * falling back to Workers AI (bge) would silently write an incompatible-space\n * vector into a nomic store and corrupt retrieval. If the rail is unavailable\n * this THROWS and the caller decides (retry local, or defer the write) — it must\n * NOT substitute a different model.\n *\n * @param env - Gateway base URL + GPU_LLM_* auth (same secrets as the chat provider).\n * @param input - Single string or array of strings to embed.\n * @returns Embedding vectors, model `nomic-embed-text`, and dimension count.\n * @throws On any transport/HTTP/empty-response error (no silent fallback).\n */\nexport async function embedLocal(\n env: LocalEmbedEnv,\n input: string | string[],\n): Promise<EmbedResult> {\n const texts = Array.isArray(input) ? input : [input];\n const res = await fetch(\n `${env.AI_GATEWAY_BASE_URL}/custom-local-gpu/v1/embeddings`,\n {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n // CF Access service-token headers only sent when present (bearer-only rails still work).\n ...(env.GPU_LLM_API_TOKEN ? { Authorization: `Bearer ${env.GPU_LLM_API_TOKEN}` } : {}),\n ...(env.GPU_LLM_ACCESS_CLIENT_ID ? { 'CF-Access-Client-Id': env.GPU_LLM_ACCESS_CLIENT_ID } : {}),\n ...(env.GPU_LLM_ACCESS_CLIENT_SECRET ? { 'CF-Access-Client-Secret': env.GPU_LLM_ACCESS_CLIENT_SECRET } : {}),\n },\n body: JSON.stringify({ model: LOCAL_EMBEDDING_MODEL, input: texts }),\n },\n );\n if (!res.ok) {\n throw new InternalError(\n `embedLocal(): rail ${res.status}: ${(await res.text()).slice(0, 160)}`,\n );\n }\n const json = (await res.json()) as { data?: Array<{ embedding: number[] }> };\n const vectors = (json.data ?? []).map((d) => d.embedding);\n if (vectors.length === 0) {\n throw new InternalError(\n `embedLocal(): rail returned no vectors for model ${LOCAL_EMBEDDING_MODEL}`,\n );\n }\n return {\n vectors,\n model: LOCAL_EMBEDDING_MODEL,\n dims: vectors[0]!.length,\n };\n}\n"],"mappings":";AAAA;AAAA,EACE,iBAAAA;AAAA,EACA;AAAA,EACA,mBAAAC;AAAA,EACA;AAAA,OAEK;;;ACSP,SAAS,eAAe,uBAAuB;AAgB/C,IAAM,iBAAiB;AAUvB,IAAM,aAAa,oBAAI,IAAkD;AAGzE,SAAS,uBAAuB,UAAqC;AACnE,QAAM,OAAO,SAAS,UAAU,EAAE,WAAW,GAAG,IAAI,WAAW,KAAK,QAAQ;AAC5E,QAAM,MAAM,KAAK,MAAM,IAAI;AAC3B,MAAI,CAAC,IAAI,gBAAgB,CAAC,IAAI,eAAe,CAAC,IAAI,WAAW;AAC3D,UAAM,IAAI,gBAAgB,+DAA+D;AAAA,EAC3F;AACA,SAAO;AACT;AAGA,SAAS,gBA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AS;AAC5B,UAAM,SAAS,OAAO,MAAM,KAAK,EAAE,OAAO,CAAC,MAAM,EAAE,SAAS,CAAC;AAC7D,aAAS,IAAI,GAAG,KAAK,OAAO,SAAS,QAAQ,KAAK;AAChD,YAAM,QAAQ,OAAO,MAAM,GAAG,IAAI,MAAM,EAAE,KAAK,GAAG;AAClD,mBAAa,IAAI,KAAK;AAAA,IACxB;AAAA,EACF;AAEA,MAAI,aAAa,SAAS,EAAG,QAAO;AAGpC,WAAS,IAAI,GAAG,KAAK,eAAe,SAAS,QAAQ,KAAK;AACxD,UAAM,QAAQ,eAAe,MAAM,GAAG,IAAI,MAAM,EAAE,KAAK,GAAG;AAC1D,QAAI,aAAa,IAAI,KAAK,EAAG,QAAO;AAAA,EACtC;AAEA,SAAO;AACT;","names":["InternalError","ValidationError","InternalError","ValidationError","InternalError"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/gcp-token.ts","../src/embed.ts"],"sourcesContent":["import {\n InternalError,\n RateLimitError,\n ValidationError,\n toErrorResponse,\n type FactoryResponse,\n} from '@latimer-woods-tech/errors';\nimport type { Logger } from '@latimer-woods-tech/logger';\nimport { fetchAdcAccessToken, mintGcpAccessToken, serviceAccountProjectId } from './gcp-token.js';\n\n/**\n * A tool the model may call. `parameters` is a JSON Schema object describing\n * the tool's input. Provider-agnostic; normalized per provider at request time.\n */\nexport interface LLMTool {\n name: string;\n description?: string;\n /** JSON Schema for the tool's input arguments. */\n parameters: Record<string, unknown>;\n}\n\n/**\n * A tool invocation requested by the model, normalized across providers.\n */\nexport interface LLMToolCall {\n /** Provider-assigned call id; echo it back in the matching tool_result. */\n id: string;\n name: string;\n /** Parsed argument object the model passed to the tool. */\n arguments: Record<string, unknown>;\n}\n\n/**\n * Structured content block for tool-calling conversations. The field shapes\n * mirror the Anthropic Messages wire format so they pass through unchanged.\n */\nexport type LLMContentBlock =\n | { type: 'text'; text: string }\n | { type: 'tool_use'; id: string; name: string; input: Record<string, unknown> }\n | { type: 'tool_result'; tool_use_id: string; content: string; is_error?: boolean };\n\n/**\n * Single chat message exchanged with an LLM provider.\n *\n * `content` is a plain string in the common case. For tool-calling\n * conversations it may be an array of {@link LLMContentBlock}s (e.g. an\n * assistant turn carrying `tool_use` blocks, or a user turn carrying\n * `tool_result` blocks). Providers that don't support tool-calling receive\n * the text projection of the content (see `contentToText`).\n */\nexport interface LLMMessage {\n role: 'user' | 'assistant' | 'system';\n content: string | LLMContentBlock[];\n}\n\n/**\n * Flattens message content to plain text for providers/paths that only handle\n * strings. `tool_use` blocks contribute nothing; `tool_result` blocks\n * contribute their textual content.\n */\nfunction contentToText(content: string | LLMContentBlock[]): string {\n if (typeof content === 'string') return content;\n return content\n .map((b) => (b.type === 'text' ? b.text : b.type === 'tool_result' ? b.content : ''))\n .join('');\n}\n\n/**\n * Resolves the system prompt: explicit `opts.system` wins, else the first\n * `system` message, flattened to text. Returns `undefined` when neither is set.\n */\nfunction systemText(opts: LLMOptions, messages: LLMMessage[]): string | undefined {\n if (opts.system !== undefined) return opts.system;\n const c = messages.find((m) => m.role === 'system')?.content;\n return c === undefined ? undefined : contentToText(c);\n}\n\n/**\n * Quality tier selected by the caller. Routing is workload-split:\n * - `fast` → Grok 4.3 with Anthropic Haiku fallback (routine drafts/small jobs)\n * - `balanced` → Anthropic Sonnet (default)\n * - `smart` → Anthropic Opus OR Gemini 2.5 Flash if input is long-context (>150k tokens estimated)\n * - `verifier` → Groq Llama (cheap second opinion; only used from verifier code path)\n * - `workbench` → DeepSeek Chat with Groq fallback (boring, reviewable, non-sensitive batch work)\n */\nexport type LLMTier = 'fast' | 'balanced' | 'smart' | 'verifier' | 'workbench';\n\n/**\n * Options that influence LLM completion behaviour.\n */\nexport interface LLMOptions {\n /** Quality tier; see {@link LLMTier}. Defaults to `balanced`. */\n tier?: LLMTier;\n /** Explicit model override. Takes precedence over tier. */\n model?: string;\n maxTokens?: number;\n temperature?: number;\n system?: string;\n /** Token budget above which we force long-context routing (Gemini). */\n longContextThreshold?: number;\n /** Per-call cancellation signal. Aborts the in-flight provider request. */\n signal?: AbortSignal;\n /** Optional run identifier stamped on ledger rows + logs. */\n runId?: string;\n /** Optional project identifier stamped on ledger rows + logs. */\n project?: string;\n /** Optional actor identifier (supervisor / worker / human). */\n actor?: string;\n /** Optional workload label used in logs and cost-policy call sites. */\n workload?: string;\n /** Grok reasoning effort. Defaults to `none` for cost-controlled fast/draft calls. */\n reasoningEffort?: 'none' | 'low' | 'medium' | 'high';\n /** Anthropic prompt-cache control. Defaults to `true` for `system` prompts ≥ 1024 tokens. */\n promptCache?: boolean;\n /**\n * Maximum estimated cost in USD for this completion.\n * This cap is enforced after the provider returns because it uses actual\n * response token counts to compute the final cost.\n * If the post-call estimated cost exceeds this cap, `complete` returns a\n * {@link RateLimitError} with code `LLM_COST_CAP_EXCEEDED` and\n * `completionStream` throws the same error.\n * Pricing is based on {@link MODEL_PRICE_PER_1M}; unknown models default to\n * Opus rates (conservative upper bound).\n */\n maxCostUsd?: number;\n /**\n * Org-level daily cost cap in USD. Requires `env.LLM_COST_KV` to be set.\n * When today's cumulative spend read from KV is >= this value, `complete`\n * returns a {@link RateLimitError} with code `LLM_DAILY_CAP_EXCEEDED`\n * without making any provider call. After a successful call the daily\n * accumulator is updated in KV (TTL: 48 h).\n */\n dailyCapUsd?: number;\n /**\n * Org-level monthly cost cap in USD. Requires `env.LLM_COST_KV` to be set.\n * Same enforcement pattern as {@link dailyCapUsd} but keyed by YYYY-MM.\n * KV TTL: 40 days.\n */\n monthlyCapUsd?: number;\n /**\n * Metering context. When supplied and `deps.onRecord` is set, a {@link LLMRecordRow}\n * is emitted after every successful completion. Errors are swallowed.\n */\n ledger?: LLMRecordContext;\n /**\n * Tools the model may call. When present, routing **fails closed** to\n * tool-capable providers — failover never falls back to a provider that\n * can't honour the tool schema. See {@link LLMResult.toolCalls}.\n */\n tools?: LLMTool[];\n /**\n * Tool-selection policy. `'auto'` (default when `tools` is set) lets the\n * model decide; `'none'` forbids tool use; `{ name }` forces a specific tool.\n */\n toolChoice?: 'auto' | 'none' | { name: string };\n}\n\n/**\n * Provider that produced an LLM response.\n */\nexport type LLMProvider = 'anthropic' | 'gemini' | 'groq' | 'grok' | 'deepseek' | 'local';\n\n/**\n * Result returned by a successful completion.\n */\nexport interface LLMResult {\n content: string;\n provider: LLMProvider;\n model: string;\n tier: LLMTier;\n tokens: { input: number; output: number; cacheRead?: number; cacheWrite?: number };\n latency: number;\n /** Number of attempts before success (1 = primary succeeded). */\n attempts: number;\n /** Monotonic request id from AI Gateway, if present in headers. */\n gatewayRequestId?: string;\n /**\n * Why generation stopped, normalized across providers. `'tool_use'` means\n * the model is requesting one or more tool calls (see {@link toolCalls}).\n */\n stopReason?: 'end' | 'tool_use' | 'max_tokens' | 'other';\n /**\n * Tool calls the model requested, normalized across providers. Present\n * (non-empty) when `stopReason === 'tool_use'`.\n */\n toolCalls?: LLMToolCall[];\n}\n\n/**\n * Environment bindings required by {@link complete}.\n *\n * `AI_GATEWAY_BASE_URL` is REQUIRED in 0.3.0. All provider calls flow through the\n * Cloudflare AI Gateway for unified logging, rate limiting, and cost telemetry.\n * In test/dev the caller may pass a custom fetch impl that short-circuits this.\n */\nexport interface LLMEnv {\n AI_GATEWAY_BASE_URL: string;\n ANTHROPIC_API_KEY: string;\n GROQ_API_KEY: string;\n /** Optional — only required for `{ tier: 'workbench' }` or `deepseek-*` model overrides. */\n DEEPSEEK_API_KEY?: string;\n /** Optional — only required when caller passes `{ model: 'grok-*' }` override. */\n GROK_API_KEY?: string;\n /**\n * Cost optimization: when true, `fast`-tier calls route to the self-hosted GPU\n * (qwen3:8b via the `custom-local-fast-gpu` AI Gateway provider) FIRST, with the normal\n * fast route (Grok→Haiku) as automatic fallback on any error. Off by default →\n * fully dormant. Requires {@link LLMEnv.GPU_LLM_API_TOKEN}.\n */\n LLM_LOCAL_FIRST?: boolean;\n /**\n * Agentic-work opt-in: route the `workbench` tier to local Qwen 27B first,\n * with DeepSeek as the independent cloud fallback.\n */\n LLM_LOCAL_WORKBENCH?: boolean;\n /** Bearer for the local GPU proxy (`GPU_LLM_API_TOKEN` in Secret Manager). Required to enable local. */\n GPU_LLM_API_TOKEN?: string;\n /** Cloudflare Access service-token id for the local GPU app; forwarded by the gateway to the origin. */\n GPU_LLM_ACCESS_CLIENT_ID?: string;\n /** Cloudflare Access service-token secret for the local GPU app. */\n GPU_LLM_ACCESS_CLIENT_SECRET?: string;\n /**\n * GCP service-account JSON key (base64-encoded or raw) with `roles/aiplatform.user`.\n * The credential for the `gemini` leg in environments WITHOUT a metadata server\n * (Cloudflare Workers): the package exchanges it for a fresh access token per\n * isolate and caches it (see `./gcp-token.ts`).\n *\n * Optional. When neither this nor {@link LLMEnv.VERTEX_ACCESS_TOKEN} is set, the\n * leg falls back to Application Default Credentials — the GCP metadata server's\n * ambient service-account token (keyless; the default on Cloud Run / GCE). Off\n * GCP with no static credential, the metadata probe fails and the chain falls\n * through to its next leg.\n */\n GCP_SA_KEY?: string;\n /**\n * Pre-minted Google Cloud access token with `aiplatform.endpoints.predict`.\n * Explicit override, used only when {@link LLMEnv.GCP_SA_KEY} is absent and\n * before ADC is attempted. Valid for ~1 hour from minting, so it cannot be a\n * durable Worker secret.\n */\n VERTEX_ACCESS_TOKEN?: string;\n /** Vertex project. Defaults to the `GCP_SA_KEY`'s own `project_id`. */\n VERTEX_PROJECT?: string;\n /** Vertex location. Defaults to `us-central1`. */\n VERTEX_LOCATION?: string;\n /**\n * Optional KV store for org-level daily/monthly cost tracking and enforcement.\n * When provided alongside {@link LLMOptions.dailyCapUsd} or {@link LLMOptions.monthlyCapUsd},\n * `complete` will block calls that would exceed the declared cap.\n * Any KV-like store satisfying `get`/`put` works (e.g. Cloudflare KV, in-memory stub).\n */\n LLM_COST_KV?: CostKvStore;\n}\n\n/**\n * Minimal KV store interface for org-level LLM cost tracking.\n * Cloudflare KV satisfies this. An in-memory stub is sufficient for tests.\n */\nexport interface CostKvStore {\n get(key: string): Promise<string | null>;\n put(key: string, value: string, options?: { expirationTtl?: number }): Promise<void>;\n}\n\n/**\n * Caller-supplied context stamped on every metering row.\n * Mirrors the `LLMRecordContext` in `@latimer-woods-tech/llm-meter`; kept inline\n * to avoid a circular dependency (llm-meter imports llm).\n */\nexport interface LLMRecordContext {\n project: string;\n actor: string;\n runId?: string;\n workload?: string;\n tenantId?: string;\n}\n\n/**\n * Row shape passed to the optional {@link LLMDeps.onRecord} callback.\n * Callers can wire this directly to `recordCall` from `@latimer-woods-tech/llm-meter`.\n */\nexport interface LLMRecordRow extends LLMRecordContext {\n model: string;\n provider: LLMProvider;\n tier: LLMTier;\n inputTokens: number;\n outputTokens: number;\n cacheReadTokens: number;\n cacheWriteTokens: number;\n latencyMs: number;\n costUsd: number;\n yyyyMm: string;\n}\n\n/**\n * Optional dependencies for {@link complete}.\n */\nexport interface LLMDeps {\n fetch?: typeof fetch;\n logger?: Logger;\n now?: () => number;\n /**\n * Optional metering callback. Called after every successful completion.\n * Errors are swallowed so metering never blocks the caller.\n * Wire to `recordCall` from `@latimer-woods-tech/llm-meter`.\n */\n onRecord?: (row: LLMRecordRow) => Promise<void>;\n}\n\n// Model catalogue — keep in sync with docs/architecture/FACTORY_V1.md § LLM substrate.\nconst MODELS = {\n anthropic: {\n // `claude-haiku-4-20250514` was never a model Anthropic served — it 404s\n // (verified live against /v1/messages). Date suffixes are never appended to\n // an alias; the alias is `claude-haiku-4-5`, which resolves server-side to\n // `claude-haiku-4-5-20251001`. This is the `fast` tier's FALLBACK, so the\n // 404 stayed invisible for as long as the Grok primary held.\n fast: 'claude-haiku-4-5',\n // Current-generation Sonnet/Opus (2026-08 refresh). The previous pins —\n // `claude-sonnet-4-6` / `claude-opus-4-7` — were a generation behind, and\n // the opus-4-7 leg was HARD-BROKEN: Claude 4.7+ rejects `temperature`\n // (400 \"`temperature` is deprecated for this model\", verified live\n // 2026-08-14) and buildAnthropicRequest always sent one, so every `smart`\n // call 400'd on its Anthropic primary and silently served Gemini Flash.\n // Sonnet 5 / Opus 5 cost the same list price or less than the models they\n // replace ($3/$15, $5/$25 per 1M). The sampling/thinking request-shape\n // differences these models introduce are handled in buildAnthropicRequest.\n balanced: 'claude-sonnet-5',\n smart: 'claude-opus-5',\n },\n gemini: {\n // `gemini-2.5-flash`, not `-pro`: the leg's job here is a fast, reliable,\n // JSON-returning fallback. Gemini 2.5 *Pro* mandates a thinking budget of\n // ≥128 tokens that is drawn from `maxOutputTokens` and CANNOT be disabled\n // (thinkingBudget=0 is rejected). On the render-runner's large judge /\n // generation prompts — and any low-`maxTokens` call (headline uses 40) —\n // the thinking phase exhausts the whole budget and Vertex returns 200 with\n // an empty candidate (finishReason MAX_TOKENS, no text), which the router\n // treats as a failed leg. Flash supports `thinkingBudget: 0` (set in\n // buildGeminiRequest), so text is always emitted. Both are Vertex-served.\n smart: 'gemini-2.5-flash',\n },\n groq: {\n // `llama-4-maverick` was NOT a model Groq serves — every `verifier` call\n // 404'd (the tier has no fallback, so it was hard-broken), and it was also\n // the `workbench` fallback. Groq's llama-4 offering is `scout`, which is\n // blocked at our org level; `llama-3.3-70b-versatile` is served and\n // unblocked (verified against /v1/models and a live completion).\n verifier: 'llama-3.3-70b-versatile',\n },\n grok: {\n fast: 'grok-4.3',\n },\n deepseek: {\n workbench: 'deepseek-chat',\n },\n local: {\n // Self-hosted qwen3:8b on the dedicated fast GPU, reached through the\n // `custom-local-fast-gpu` AI Gateway provider's native Ollama chat route.\n fast: 'qwen3:8b',\n workbench: 'qwen3.6:27b',\n },\n} as const;\n\nconst DEFAULT_MAX_TOKENS = 1024;\nconst DEFAULT_TEMPERATURE = 0.7;\n/** Vertex region used when the caller does not pin one. */\nconst DEFAULT_VERTEX_LOCATION = 'us-central1';\nconst DEFAULT_LONG_CONTEXT_THRESHOLD = 150_000; // tokens\n\n// ─── Per-provider exponential backoff constants ────────────────────────────\n/** Base delay in ms for the first retry. */\nconst BACKOFF_BASE_MS = 500;\n/** Maximum backoff cap in ms. */\nconst BACKOFF_CAP_MS = 8_000;\n/** Max random jitter added to each backoff delay, in ms. */\nconst BACKOFF_JITTER_MAX_MS = 250;\n/** Maximum number of attempts per provider (1 initial + 2 retries). */\nconst PER_PROVIDER_MAX_ATTEMPTS = 3;\n\n// ─── Per-provider cooldown state (module-level) ────────────────────────────\n/**\n * Tracks when a provider's cooldown period expires.\n * Keyed by {@link LLMProvider}; value is the `Date.now()` epoch ms at which\n * the cooldown expires. Absent key means \"not cooling down\".\n */\nconst providerCooldownUntil: Map<LLMProvider, number> = new Map();\n\n/** Cooldown duration in ms after a provider exhausts all retries. */\nconst PROVIDER_COOLDOWN_MS = 30_000;\n\n/**\n * Returns `true` if the provider is currently in its cooldown window.\n * Uses the injected `now` function (or `Date.now`) for testability.\n */\nfunction isProviderCoolingDown(provider: LLMProvider, now: () => number = Date.now): boolean {\n const until = providerCooldownUntil.get(provider);\n if (until === undefined) return false;\n return now() < until;\n}\n\n/** Returns `YYYY-MM-DD` from a Unix timestamp (ms). Used for daily KV cost keys. */\nfunction isoDate(nowMs: number): string {\n return new Date(nowMs).toISOString().slice(0, 10);\n}\n\n/**\n * Record actual call spend in org-level daily/monthly KV buckets.\n * This is intentionally best-effort: Cloudflare KV does not provide an atomic\n * compare-and-swap, so concurrent requests can race and undercount spend.\n */\nasync function recordOrgCostUsage(\n kv: CostKvStore,\n todayKey: string,\n monthKey: string,\n costUsd: number,\n opts: LLMOptions,\n): Promise<void> {\n if (opts.dailyCapUsd !== undefined) {\n const raw = await kv.get(todayKey).catch(() => null);\n const spent = parseFloat(raw ?? '0');\n await kv.put(todayKey, String(spent + costUsd), { expirationTtl: 172_800 /* 48 h */ }).catch(() => undefined);\n }\n if (opts.monthlyCapUsd !== undefined) {\n const raw = await kv.get(monthKey).catch(() => null);\n const spent = parseFloat(raw ?? '0');\n await kv.put(monthKey, String(spent + costUsd), { expirationTtl: 3_456_000 /* 40 d */ }).catch(() => undefined);\n }\n}\n\n/**\n * USD cost per 1 million tokens for each model.\n * Source: Anthropic / Google / xAI pricing pages as of 2026-05.\n * Keep these model names in sync with the default routing constants in\n * {@link MODELS}; unknown models fall back to Opus rates (conservative upper bound).\n *\n * CANONICAL pricing source for the platform. `@latimer-woods-tech/llm-meter`\n * derives its cents-denominated rates from this table and a drift-guard test\n * there fails CI if they diverge — make all rate changes here.\n */\nexport const MODEL_PRICE_PER_1M: Record<string, { input: number; output: number; cacheRead: number; cacheWrite: number }> = {\n // Anthropic Haiku 4.5 — `claude-haiku-4-5` is the routed alias; the dated id\n // is what the API echoes back in `response.model`, so both must price.\n // (Rates corrected 2026-08: the previous $0.80/$4.00 rows UNDERSTATED the\n // actual $1/$5 list price.)\n 'claude-haiku-4-5': { input: 1.00, output: 5.00, cacheRead: 0.10, cacheWrite: 1.25 },\n 'claude-haiku-4-5-20251001': { input: 1.00, output: 5.00, cacheRead: 0.10, cacheWrite: 1.25 },\n // Anthropic Sonnet 4\n 'claude-sonnet-4-20250514': { input: 3.00, output: 15.00, cacheRead: 0.30, cacheWrite: 3.75 },\n 'claude-sonnet-4-6': { input: 3.00, output: 15.00, cacheRead: 0.30, cacheWrite: 3.75 },\n // Anthropic Sonnet 5 — the routed `balanced` primary. List $3/$15; intro\n // pricing ($2/$10) runs through 2026-08-31 — priced at list here, the\n // conservative upper bound (table convention).\n 'claude-sonnet-5': { input: 3.00, output: 15.00, cacheRead: 0.30, cacheWrite: 3.75 },\n // Anthropic Opus 4 (dated id) — genuinely the $15/$75 era.\n 'claude-opus-4-20250514': { input: 15.00, output: 75.00, cacheRead: 1.50, cacheWrite: 18.75 },\n // Opus 4.7 was NEVER $15/$75 — it launched at $5/$25. The old row copied the\n // Opus 4 rate and overstated every smart-tier ledger entry 3x.\n 'claude-opus-4-7': { input: 5.00, output: 25.00, cacheRead: 0.50, cacheWrite: 6.25 },\n // Anthropic Opus 5 — the routed `smart` primary. Same $5/$25 as Opus 4.8/4.7.\n 'claude-opus-5': { input: 5.00, output: 25.00, cacheRead: 0.50, cacheWrite: 6.25 },\n // Gemini 2.5 Flash — the routed `smart`/long-context model (JSON fallback leg).\n 'gemini-2.5-flash': { input: 0.30, output: 2.50, cacheRead: 0.075, cacheWrite: 0.30 },\n // Gemini 2.5 Pro — retained for historical ledger rows (was the routed model).\n 'gemini-2.5-pro': { input: 1.25, output: 10.00, cacheRead: 0.31, cacheWrite: 4.50 },\n // Groq Llama 3.3 70B Versatile (`verifier` tier). Groq has no prompt caching,\n // so cache rates are 0.00 — same convention as grok-4.3 below.\n 'llama-3.3-70b-versatile': { input: 0.59, output: 0.79, cacheRead: 0.00, cacheWrite: 0.00 },\n // Grok 4.3\n 'grok-4.3': { input: 1.25, output: 2.50, cacheRead: 0.00, cacheWrite: 0.00 },\n // DeepSeek API pricing as of 2026-05: cache-write conservatively uses cache-miss input pricing.\n 'deepseek-chat': { input: 0.27, output: 1.10, cacheRead: 0.07, cacheWrite: 0.27 },\n 'deepseek-reasoner': { input: 0.55, output: 2.19, cacheRead: 0.14, cacheWrite: 0.55 },\n // Deprecated aliases retained for historical ledger rows.\n 'grok-4-fast': { input: 1.25, output: 2.50, cacheRead: 0.00, cacheWrite: 0.00 },\n 'grok-3-mini-latest': { input: 1.25, output: 2.50, cacheRead: 0.00, cacheWrite: 0.00 },\n // Self-hosted qwen3 on the GPU box — zero marginal cost (electricity aside).\n 'qwen3:8b': { input: 0.0, output: 0.0, cacheRead: 0.0, cacheWrite: 0.0 },\n 'qwen3.6:27b': { input: 0.0, output: 0.0, cacheRead: 0.0, cacheWrite: 0.0 },\n};\n\n/** Fallback pricing used for unrecognised models (Opus rates — conservative upper bound). */\nconst PRICE_FALLBACK = MODEL_PRICE_PER_1M['claude-opus-4-7']!;\n\n/**\n * Estimates the USD cost of a single LLM completion from token counts.\n * Returns 0 for zero-token results. Uses {@link MODEL_PRICE_PER_1M} with\n * {@link PRICE_FALLBACK} for unknown models.\n */\nfunction estimateCostUsd(\n tokens: { input: number; output: number; cacheRead?: number; cacheWrite?: number },\n model: string,\n): number {\n const price = MODEL_PRICE_PER_1M[model] ?? PRICE_FALLBACK;\n return (\n (tokens.input * price.input +\n tokens.output * price.output +\n (tokens.cacheRead ?? 0) * price.cacheRead +\n (tokens.cacheWrite ?? 0) * price.cacheWrite) /\n 1_000_000\n );\n}\n\n/** Returns `YYYY-MM` from a Unix timestamp (ms). Used for monthly KV cost keys. */\nfunction isoMonth(nowMs: number): string {\n return new Date(nowMs).toISOString().slice(0, 7);\n}\n\n/**\n * Marks a provider as cooling down for {@link PROVIDER_COOLDOWN_MS} milliseconds.\n */\nfunction markProviderCoolingDown(provider: LLMProvider, now: () => number = Date.now): void {\n providerCooldownUntil.set(provider, now() + PROVIDER_COOLDOWN_MS);\n}\n\n/**\n * Clears the cooldown state for a provider after a successful call.\n */\nfunction clearProviderCooldown(provider: LLMProvider): void {\n providerCooldownUntil.delete(provider);\n}\n\n// ─── Legacy backoff constant (kept for the existing callWithBackoff signature) ─\nconst BASE_BACKOFF_MS = 250;\n\ninterface ProviderError {\n provider: LLMProvider;\n status: number;\n retryable: boolean;\n message: string;\n}\n\n/**\n * Returns `true` for status codes that should trigger a retry.\n * Only 429 and 5xx (transient server errors) qualify; other 4xx are terminal.\n */\nfunction isRetryableForBackoff(status: number): boolean {\n return status === 429 || (status >= 500 && status < 600);\n}\n\nfunction estimateTokens(messages: LLMMessage[], system?: string): number {\n // Cheap estimator: ~4 chars/token. Good enough for threshold routing.\n let chars = system?.length ?? 0;\n for (const m of messages) chars += contentToText(m.content).length;\n return Math.ceil(chars / 4);\n}\n\nfunction sleep(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n const t = setTimeout(resolve, ms);\n if (signal) {\n const onAbort = () => {\n clearTimeout(t);\n reject(new DOMException('Aborted', 'AbortError'));\n };\n if (signal.aborted) onAbort();\n else signal.addEventListener('abort', onAbort, { once: true });\n }\n });\n}\n\n/**\n * Computes the exponential backoff delay for a given attempt with jitter.\n *\n * Formula: `Math.min(base * 2^attempt + jitter, cap)`\n * where `jitter` is a random value in `[0, BACKOFF_JITTER_MAX_MS)`.\n *\n * @param attempt - Zero-based attempt index (0 = first retry after initial failure).\n */\nfunction computeBackoffMs(attempt: number): number {\n const jitter = Math.floor(Math.random() * BACKOFF_JITTER_MAX_MS);\n return Math.min(BACKOFF_BASE_MS * Math.pow(2, attempt) + jitter, BACKOFF_CAP_MS);\n}\n\n// ─── Provider request builders ─────────────────────────────────────────────\n\n/**\n * Claude 4.7-and-later models reject the classic sampling params: sending\n * `temperature` (or `top_p`/`top_k`) returns 400 \"`temperature` is deprecated\n * for this model\" (verified live against /v1/messages, 2026-08-14). Models\n * matching this pattern must not receive a `temperature` field at all — this\n * is exactly what hard-broke the `smart` tier while it pinned opus-4-7.\n */\nconst ANTHROPIC_SAMPLING_REMOVED = /^claude-(opus-4-[78]|opus-5|sonnet-5|fable-5|mythos-5)/;\n\n/**\n * Models that run ADAPTIVE THINKING by default when the `thinking` field is\n * omitted. Thinking tokens draw from `max_tokens`, and this package's budgets\n * are small (DEFAULT_MAX_TOKENS 1024; the headline call passes 40) — left on,\n * the model can spend the whole budget thinking and truncate the visible\n * text. Same failure mode as Gemini 2.5 Pro's mandatory thinking budget (see\n * the MODELS.gemini comment), same fix: turn it off explicitly.\n * `{type:'disabled'}` is accepted on both models at the default effort\n * (verified live 2026-08-14). Do NOT add `claude-fable-5`/`claude-mythos-5`\n * here — those models reject an explicit `disabled` (thinking is always on).\n */\nconst ANTHROPIC_THINKING_DEFAULT_ON = /^claude-(opus-5|sonnet-5)$/;\n\nfunction buildAnthropicRequest(\n model: string,\n messages: LLMMessage[],\n opts: LLMOptions,\n env: LLMEnv,\n streaming = false,\n): { url: string; headers: Record<string, string>; body: string } {\n const sys = systemText(opts, messages);\n const filtered = messages.filter((m) => m.role !== 'system');\n const body: Record<string, unknown> = {\n model,\n max_tokens: opts.maxTokens ?? DEFAULT_MAX_TOKENS,\n messages: filtered.map((m) => ({ role: m.role, content: m.content })),\n };\n if (!ANTHROPIC_SAMPLING_REMOVED.test(model)) {\n body.temperature = opts.temperature ?? DEFAULT_TEMPERATURE;\n }\n if (ANTHROPIC_THINKING_DEFAULT_ON.test(model)) {\n body.thinking = { type: 'disabled' };\n }\n if (streaming) {\n body.stream = true;\n }\n if (sys) {\n const cache = opts.promptCache ?? sys.length >= 4096;\n body.system = cache\n ? [{ type: 'text', text: sys, cache_control: { type: 'ephemeral' } }]\n : sys;\n }\n if (opts.tools && opts.tools.length > 0) {\n body.tools = opts.tools.map((t) => ({\n name: t.name,\n description: t.description ?? '',\n input_schema: t.parameters,\n }));\n const tc = opts.toolChoice ?? 'auto';\n body.tool_choice =\n tc === 'auto'\n ? { type: 'auto' }\n : tc === 'none'\n ? { type: 'none' }\n : { type: 'tool', name: tc.name };\n }\n return {\n url: `${env.AI_GATEWAY_BASE_URL}/anthropic/v1/messages`,\n headers: {\n 'content-type': 'application/json',\n 'x-api-key': env.ANTHROPIC_API_KEY,\n 'anthropic-version': '2023-06-01',\n 'anthropic-beta': 'prompt-caching-2024-07-31',\n },\n body: JSON.stringify(body),\n };\n}\n\n/** A resolved Vertex credential: a bearer token plus (for ADC) the ambient project id. */\ninterface VertexAuth {\n token: string;\n /** Project id discovered from the metadata server (ADC path only). */\n project?: string;\n}\n\n/**\n * Resolve a Vertex credential, in priority order:\n * 1. `GCP_SA_KEY` — mint a fresh token from the service-account key (the\n * credential Cloudflare Workers supply, where there is no metadata server).\n * 2. `VERTEX_ACCESS_TOKEN` — a pre-minted token (legacy / CI override).\n * 3. Application Default Credentials — the GCP metadata server's ambient\n * service-account token. This is the KEYLESS default on Cloud Run / GCE:\n * neither env var need be set, mirroring the post-WIF-migration pattern\n * the render-runner already uses for every other GCP API call.\n *\n * @throws ValidationError when no static credential is set AND the metadata\n * server is unreachable (i.e. off-GCP) — the leg fails before the request so\n * the router falls through to the next provider, never calling Vertex\n * unauthenticated.\n */\nasync function resolveVertexAuth(env: LLMEnv, fetchImpl: typeof fetch): Promise<VertexAuth> {\n if (env.GCP_SA_KEY) {\n try {\n return { token: await mintGcpAccessToken(env.GCP_SA_KEY, fetchImpl) };\n } catch (error) {\n // A pre-minted token is worth trying if minting broke. With nothing to fall\n // back to, surface the minting failure rather than swallowing it.\n if (!env.VERTEX_ACCESS_TOKEN) throw error;\n }\n }\n if (env.VERTEX_ACCESS_TOKEN) return { token: env.VERTEX_ACCESS_TOKEN };\n // Keyless default: Application Default Credentials from the GCP metadata\n // server. Throws (→ chain falls through) when off-GCP.\n return fetchAdcAccessToken(fetchImpl);\n}\n\nfunction buildGeminiRequest(\n model: string,\n messages: LLMMessage[],\n opts: LLMOptions,\n env: LLMEnv,\n accessToken: string,\n adcProject?: string,\n): { url: string; headers: Record<string, string>; body: string } {\n const sys = systemText(opts, messages);\n const contents = messages\n .filter((m) => m.role !== 'system')\n .map((m) => ({\n role: m.role === 'assistant' ? 'model' : 'user',\n parts: [{ text: contentToText(m.content) }],\n }));\n const body: Record<string, unknown> = {\n contents,\n generationConfig: {\n maxOutputTokens: opts.maxTokens ?? DEFAULT_MAX_TOKENS,\n temperature: opts.temperature ?? DEFAULT_TEMPERATURE,\n // Disable \"thinking\": Gemini 2.5 draws thinking tokens from\n // maxOutputTokens, so on a large prompt (or a small token budget) the\n // model can spend the entire budget thinking and return an empty\n // candidate (finishReason MAX_TOKENS). This leg wants deterministic text\n // out, so thinking is turned off (supported by gemini-2.5-flash).\n thinkingConfig: { thinkingBudget: 0 },\n },\n };\n if (sys) {\n body.systemInstruction = { parts: [{ text: sys }] };\n }\n // Project resolution: an explicit VERTEX_PROJECT wins; else the service\n // account's own project (GCP_SA_KEY path); else the ambient project id the\n // metadata server reported (ADC path). So a Cloud Run job needs no Vertex\n // config at all — the runner's own project is used.\n const project =\n env.VERTEX_PROJECT ||\n (env.GCP_SA_KEY ? serviceAccountProjectId(env.GCP_SA_KEY) : (adcProject ?? ''));\n const location = env.VERTEX_LOCATION || DEFAULT_VERTEX_LOCATION;\n const path = `v1/projects/${project}/locations/${location}/publishers/google/models/${model}:generateContent`;\n return {\n url: `${env.AI_GATEWAY_BASE_URL}/google-vertex-ai/${path}`,\n headers: {\n 'content-type': 'application/json',\n authorization: `Bearer ${accessToken}`,\n },\n body: JSON.stringify(body),\n };\n}\n\n// ─── OpenAI-style (Grok / DeepSeek / Groq) tool-calling helpers ──────────────\n\n/**\n * Converts provider-agnostic messages to OpenAI chat-completions format,\n * translating the Anthropic-shaped tool blocks: `tool_use` → an assistant\n * message with `tool_calls`; `tool_result` → a standalone `tool` message keyed\n * by `tool_call_id`. Plain-string content passes through unchanged.\n */\nfunction toOpenAiMessages(messages: LLMMessage[], sys: string | undefined): Array<Record<string, unknown>> {\n const out: Array<Record<string, unknown>> = [];\n if (sys) out.push({ role: 'system', content: sys });\n for (const m of messages) {\n if (m.role === 'system') continue;\n if (typeof m.content === 'string') {\n out.push({ role: m.role, content: m.content });\n continue;\n }\n let text = '';\n const toolCalls: Array<Record<string, unknown>> = [];\n const results: Array<{ tool_use_id: string; content: string }> = [];\n for (const b of m.content) {\n if (b.type === 'text') text += b.text;\n else if (b.type === 'tool_use')\n toolCalls.push({ id: b.id, type: 'function', function: { name: b.name, arguments: JSON.stringify(b.input) } });\n else if (b.type === 'tool_result') results.push({ tool_use_id: b.tool_use_id, content: b.content });\n }\n if (results.length > 0) {\n for (const r of results) out.push({ role: 'tool', tool_call_id: r.tool_use_id, content: r.content });\n if (text) out.push({ role: 'user', content: text });\n } else if (toolCalls.length > 0) {\n out.push({ role: 'assistant', content: text || null, tool_calls: toolCalls });\n } else {\n out.push({ role: m.role, content: text });\n }\n }\n return out;\n}\n\n/**\n * Converts normalized messages to Ollama's native chat shape. Native tool calls\n * carry object arguments (rather than OpenAI's JSON string), and tool results\n * identify the function by name rather than by call id.\n */\nfunction toOllamaMessages(messages: LLMMessage[], sys: string | undefined): Array<Record<string, unknown>> {\n const toolNames = new Map<string, string>();\n for (const message of messages) {\n if (typeof message.content === 'string') continue;\n for (const block of message.content) {\n if (block.type === 'tool_use') toolNames.set(block.id, block.name);\n }\n }\n\n const out: Array<Record<string, unknown>> = [];\n if (sys) out.push({ role: 'system', content: sys });\n for (const message of messages) {\n if (message.role === 'system') continue;\n if (typeof message.content === 'string') {\n out.push({ role: message.role, content: message.content });\n continue;\n }\n\n let text = '';\n const toolCalls: Array<Record<string, unknown>> = [];\n const results: Array<{ toolUseId: string; content: string }> = [];\n for (const block of message.content) {\n if (block.type === 'text') text += block.text;\n else if (block.type === 'tool_use') {\n toolCalls.push({\n type: 'function',\n function: { index: toolCalls.length, name: block.name, arguments: block.input },\n });\n } else if (block.type === 'tool_result') {\n results.push({ toolUseId: block.tool_use_id, content: block.content });\n }\n }\n if (results.length > 0) {\n for (const result of results) {\n out.push({\n role: 'tool',\n tool_name: toolNames.get(result.toolUseId) ?? result.toolUseId,\n content: result.content,\n });\n }\n if (text) out.push({ role: 'user', content: text });\n } else if (toolCalls.length > 0) {\n out.push({ role: 'assistant', content: text, tool_calls: toolCalls });\n } else {\n out.push({ role: message.role, content: text });\n }\n }\n return out;\n}\n\n/** Builds the OpenAI `tools` array from {@link LLMOptions.tools}, or undefined. */\nfunction openAiTools(opts: LLMOptions): Array<Record<string, unknown>> | undefined {\n if (!opts.tools || opts.tools.length === 0) return undefined;\n return opts.tools.map((t) => ({\n type: 'function',\n function: { name: t.name, description: t.description ?? '', parameters: t.parameters },\n }));\n}\n\n/** Maps {@link LLMOptions.toolChoice} to the OpenAI `tool_choice` value. */\nfunction openAiToolChoice(tc: LLMOptions['toolChoice']): unknown {\n if (tc === undefined) return undefined;\n if (tc === 'auto' || tc === 'none') return tc;\n return { type: 'function', function: { name: tc.name } };\n}\n\nfunction buildGroqRequest(\n model: string,\n messages: LLMMessage[],\n opts: LLMOptions,\n env: LLMEnv,\n): { url: string; headers: Record<string, string>; body: string } {\n const sys = systemText(opts, messages);\n const merged: LLMMessage[] = [];\n if (sys) merged.push({ role: 'system', content: sys });\n for (const m of messages) if (m.role !== 'system') merged.push({ role: m.role, content: contentToText(m.content) });\n return {\n url: `${env.AI_GATEWAY_BASE_URL}/groq/openai/v1/chat/completions`,\n headers: {\n 'content-type': 'application/json',\n authorization: `Bearer ${env.GROQ_API_KEY}`,\n },\n body: JSON.stringify({\n model,\n max_tokens: opts.maxTokens ?? DEFAULT_MAX_TOKENS,\n temperature: opts.temperature ?? DEFAULT_TEMPERATURE,\n messages: merged,\n }),\n };\n}\n\nfunction buildGrokRequest(\n model: string,\n messages: LLMMessage[],\n opts: LLMOptions,\n env: LLMEnv,\n): { url: string; headers: Record<string, string>; body: string } {\n if (!env.GROK_API_KEY) {\n throw new ValidationError('GROK_API_KEY required for grok-* model override');\n }\n const sys = systemText(opts, messages);\n const body: Record<string, unknown> = {\n model,\n max_tokens: opts.maxTokens ?? DEFAULT_MAX_TOKENS,\n temperature: opts.temperature ?? DEFAULT_TEMPERATURE,\n messages: toOpenAiMessages(messages, sys),\n };\n const tools = openAiTools(opts);\n if (tools) {\n body.tools = tools;\n const tc = openAiToolChoice(opts.toolChoice ?? 'auto');\n if (tc !== undefined) body.tool_choice = tc;\n }\n if (model === MODELS.grok.fast) {\n body.reasoning_effort = opts.reasoningEffort ?? 'none';\n }\n return {\n url: `${env.AI_GATEWAY_BASE_URL}/grok/v1/chat/completions`,\n headers: {\n 'content-type': 'application/json',\n authorization: `Bearer ${env.GROK_API_KEY}`,\n },\n body: JSON.stringify(body),\n };\n}\n\nfunction buildDeepSeekRequest(\n model: string,\n messages: LLMMessage[],\n opts: LLMOptions,\n env: LLMEnv,\n): { url: string; headers: Record<string, string>; body: string } {\n if (!env.DEEPSEEK_API_KEY) {\n throw new ValidationError('DEEPSEEK_API_KEY required for workbench tier or deepseek-* model override');\n }\n const sys = systemText(opts, messages);\n const body: Record<string, unknown> = {\n model,\n max_tokens: opts.maxTokens ?? DEFAULT_MAX_TOKENS,\n temperature: opts.temperature ?? DEFAULT_TEMPERATURE,\n messages: toOpenAiMessages(messages, sys),\n };\n const tools = openAiTools(opts);\n if (tools) {\n body.tools = tools;\n const tc = openAiToolChoice(opts.toolChoice ?? 'auto');\n if (tc !== undefined) body.tool_choice = tc;\n }\n return {\n url: `${env.AI_GATEWAY_BASE_URL}/deepseek/chat/completions`,\n headers: {\n 'content-type': 'application/json',\n authorization: `Bearer ${env.DEEPSEEK_API_KEY}`,\n },\n body: JSON.stringify(body),\n };\n}\n\n/**\n * Builds a local Qwen request behind Cloudflare AI Gateway. Qwen 8B uses the\n * dedicated fast origin's native Ollama contract so `think:false` is enforced;\n * Qwen 27B retains the OpenAI-compatible agentic/tool-call route.\n */\nfunction buildLocalRequest(\n model: string,\n messages: LLMMessage[],\n opts: LLMOptions,\n env: LLMEnv,\n): { url: string; headers: Record<string, string>; body: string } {\n if (!env.GPU_LLM_API_TOKEN) {\n throw new ValidationError('GPU_LLM_API_TOKEN required for the local provider');\n }\n const sys = systemText(opts, messages);\n const fastMode = model === MODELS.local.fast;\n const headers: Record<string, string> = {\n 'content-type': 'application/json',\n authorization: `Bearer ${env.GPU_LLM_API_TOKEN}`,\n };\n if (env.GPU_LLM_ACCESS_CLIENT_ID) headers['CF-Access-Client-Id'] = env.GPU_LLM_ACCESS_CLIENT_ID;\n if (env.GPU_LLM_ACCESS_CLIENT_SECRET) headers['CF-Access-Client-Secret'] = env.GPU_LLM_ACCESS_CLIENT_SECRET;\n const tools = openAiTools(opts);\n if (fastMode) {\n if (typeof opts.toolChoice === 'object') {\n throw new ValidationError('named tool choice is not supported by the native Qwen 8B route');\n }\n return {\n url: `${env.AI_GATEWAY_BASE_URL}/custom-local-fast-gpu/api/chat`,\n headers,\n body: JSON.stringify({\n model,\n stream: false,\n think: false,\n messages: toOllamaMessages(messages, sys),\n options: {\n num_predict: opts.maxTokens ?? DEFAULT_MAX_TOKENS,\n temperature: opts.temperature ?? DEFAULT_TEMPERATURE,\n },\n ...(tools && opts.toolChoice !== 'none' ? { tools } : {}),\n }),\n };\n }\n return {\n url: `${env.AI_GATEWAY_BASE_URL}/custom-local-gpu/v1/chat/completions`,\n headers,\n body: JSON.stringify({\n model,\n max_tokens: opts.maxTokens ?? DEFAULT_MAX_TOKENS,\n temperature: opts.temperature ?? DEFAULT_TEMPERATURE,\n messages: toOpenAiMessages(messages, sys),\n ...(tools\n ? { tools, tool_choice: openAiToolChoice(opts.toolChoice ?? 'auto') }\n : {}),\n }),\n };\n}\n\n// ─── Response parsers ──────────────────────────────────────────────────────\n\ninterface AnthropicResponse {\n content?: Array<{\n type: string;\n text?: string;\n id?: string;\n name?: string;\n input?: Record<string, unknown>;\n }>;\n stop_reason?: string;\n usage?: {\n input_tokens?: number;\n output_tokens?: number;\n cache_read_input_tokens?: number;\n cache_creation_input_tokens?: number;\n };\n model?: string;\n}\n\n/** Maps a provider stop reason to the normalized {@link LLMResult.stopReason}. */\nfunction normalizeAnthropicStop(reason: string | undefined): LLMResult['stopReason'] {\n switch (reason) {\n case 'end_turn':\n case 'stop_sequence':\n return 'end';\n case 'tool_use':\n return 'tool_use';\n case 'max_tokens':\n return 'max_tokens';\n default:\n return reason ? 'other' : undefined;\n }\n}\n\ninterface GeminiResponse {\n candidates?: Array<{ content?: { parts?: Array<{ text?: string }> } }>;\n usageMetadata?: {\n promptTokenCount?: number;\n candidatesTokenCount?: number;\n };\n}\n\ninterface GroqResponse {\n choices?: Array<{ message?: { content?: string } }>;\n usage?: { prompt_tokens?: number; completion_tokens?: number };\n model?: string;\n}\n\nfunction parseAnthropic(\n json: unknown,\n): {\n content: string;\n input: number;\n output: number;\n cacheRead: number;\n cacheWrite: number;\n model?: string;\n toolCalls?: LLMToolCall[];\n stopReason?: LLMResult['stopReason'];\n} {\n const r = json as AnthropicResponse;\n const toolCalls: LLMToolCall[] = (r.content ?? [])\n .filter((c) => c.type === 'tool_use' && typeof c.id === 'string' && typeof c.name === 'string')\n .map((c) => ({ id: c.id!, name: c.name!, arguments: c.input ?? {} }));\n return {\n content: r.content?.find((c) => c.type === 'text')?.text ?? '',\n input: r.usage?.input_tokens ?? 0,\n output: r.usage?.output_tokens ?? 0,\n cacheRead: r.usage?.cache_read_input_tokens ?? 0,\n cacheWrite: r.usage?.cache_creation_input_tokens ?? 0,\n model: r.model,\n toolCalls: toolCalls.length > 0 ? toolCalls : undefined,\n stopReason: normalizeAnthropicStop(r.stop_reason),\n };\n}\n\nfunction parseGemini(json: unknown): { content: string; input: number; output: number } {\n const r = json as GeminiResponse;\n const text =\n r.candidates?.[0]?.content?.parts?.map((p) => p.text ?? '').join('') ?? '';\n return {\n content: text,\n input: r.usageMetadata?.promptTokenCount ?? 0,\n output: r.usageMetadata?.candidatesTokenCount ?? 0,\n };\n}\n\nfunction parseGroq(json: unknown): { content: string; input: number; output: number; model?: string } {\n const r = json as GroqResponse;\n return {\n content: r.choices?.[0]?.message?.content ?? '',\n input: r.usage?.prompt_tokens ?? 0,\n output: r.usage?.completion_tokens ?? 0,\n model: r.model,\n };\n}\n\ninterface OpenAiResponse {\n choices?: Array<{\n message?: {\n content?: string | null;\n tool_calls?: Array<{ id?: string; function?: { name?: string; arguments?: string } }>;\n };\n finish_reason?: string;\n }>;\n usage?: { prompt_tokens?: number; completion_tokens?: number };\n model?: string;\n}\n\ninterface OllamaChatResponse {\n model?: string;\n message?: {\n content?: string;\n thinking?: string;\n tool_calls?: Array<{\n function?: { name?: string; arguments?: Record<string, unknown> | string };\n }>;\n };\n done?: boolean;\n done_reason?: string;\n prompt_eval_count?: number;\n eval_count?: number;\n}\n\n/** Maps an OpenAI `finish_reason` to the normalized {@link LLMResult.stopReason}. */\nfunction normalizeOpenAiStop(reason: string | undefined): LLMResult['stopReason'] {\n switch (reason) {\n case 'stop':\n return 'end';\n case 'tool_calls':\n case 'function_call':\n return 'tool_use';\n case 'length':\n return 'max_tokens';\n default:\n return reason ? 'other' : undefined;\n }\n}\n\n/** Best-effort parse of an OpenAI tool-call arguments string; `{}` on failure. */\nfunction parseToolArgs(raw: string | undefined): Record<string, unknown> {\n if (!raw) return {};\n try {\n const v = JSON.parse(raw) as unknown;\n return typeof v === 'object' && v !== null ? (v as Record<string, unknown>) : {};\n } catch {\n return {};\n }\n}\n\n/**\n * Parses an OpenAI chat-completions response (Grok, DeepSeek), extracting\n * normalized tool calls and a stop reason in addition to text + tokens.\n */\nfunction parseOpenAi(json: unknown): {\n content: string;\n input: number;\n output: number;\n model?: string;\n toolCalls?: LLMToolCall[];\n stopReason?: LLMResult['stopReason'];\n} {\n const r = json as OpenAiResponse;\n const choice = r.choices?.[0];\n const toolCalls: LLMToolCall[] = (choice?.message?.tool_calls ?? [])\n .filter((c) => typeof c.function?.name === 'string')\n .map((c, i) => ({\n id: c.id ?? `call_${i}`,\n name: c.function!.name!,\n arguments: parseToolArgs(c.function?.arguments),\n }));\n return {\n content: choice?.message?.content ?? '',\n input: r.usage?.prompt_tokens ?? 0,\n output: r.usage?.completion_tokens ?? 0,\n model: r.model,\n toolCalls: toolCalls.length > 0 ? toolCalls : undefined,\n stopReason: normalizeOpenAiStop(choice?.finish_reason),\n };\n}\n\n/** Parses Ollama's native non-streaming `/api/chat` response for Qwen 8B. */\nfunction parseOllamaChat(json: unknown): {\n content: string;\n input: number;\n output: number;\n model?: string;\n toolCalls?: LLMToolCall[];\n stopReason?: LLMResult['stopReason'];\n} {\n const response = json as OllamaChatResponse;\n const toolCalls: LLMToolCall[] = (response.message?.tool_calls ?? [])\n .filter((call) => typeof call.function?.name === 'string')\n .map((call, index) => ({\n id: `call_${index}`,\n name: call.function!.name!,\n arguments:\n typeof call.function?.arguments === 'string'\n ? parseToolArgs(call.function.arguments)\n : (call.function?.arguments ?? {}),\n }));\n let stopReason: LLMResult['stopReason'];\n if (toolCalls.length > 0) stopReason = 'tool_use';\n else if (response.done_reason === 'length') stopReason = 'max_tokens';\n else if (response.done) stopReason = 'end';\n else if (response.done_reason) stopReason = 'other';\n return {\n // Deliberately exclude message.thinking: callers receive only the bounded\n // classification answer even if a non-conforming origin returns a trace.\n content: response.message?.content ?? '',\n input: response.prompt_eval_count ?? 0,\n output: response.eval_count ?? 0,\n model: response.model,\n toolCalls: toolCalls.length > 0 ? toolCalls : undefined,\n stopReason,\n };\n}\n\n// ─── Core call with backoff ────────────────────────────────────────────────\n\n/**\n * Calls a provider with per-provider exponential backoff.\n *\n * Retries up to {@link PER_PROVIDER_MAX_ATTEMPTS} times on 429 or transient 5xx.\n * Other 4xx codes are treated as terminal and not retried.\n * AbortError is never retried — it bubbles immediately.\n *\n * @param provider - Provider name, used for error tagging.\n * @param request - Pre-built HTTP request descriptor.\n * @param fetchImpl - Fetch implementation (injectable for tests).\n * @param signal - Optional AbortSignal for cancellation.\n * @param logger - Optional logger for per-attempt warnings.\n * @param nowFn - Optional clock injection for testability.\n * @returns Parsed JSON body, optional AI Gateway request ID, and attempt count.\n */\nasync function callWithBackoff(\n provider: LLMProvider,\n request: { url: string; headers: Record<string, string>; body: string },\n fetchImpl: typeof fetch,\n signal: AbortSignal | undefined,\n logger: Logger | undefined,\n nowFn?: () => number,\n): Promise<{ json: unknown; gatewayRequestId?: string; attempts: number }> {\n /**\n * Helper: mark provider cooling down and then throw the error.\n * Called whenever we determine we've exhausted all retries for the provider.\n * AbortError is never counted as a provider exhaustion — it bypasses this.\n */\n function exhaustAndThrow(err: ProviderError): never {\n markProviderCoolingDown(provider, nowFn ?? Date.now);\n throw err;\n }\n\n let lastErr: ProviderError | undefined;\n for (let attempt = 1; attempt <= PER_PROVIDER_MAX_ATTEMPTS; attempt++) {\n try {\n const response = await fetchImpl(request.url, {\n method: 'POST',\n headers: request.headers,\n body: request.body,\n signal,\n });\n if (!response.ok) {\n const text = await response.text().catch(() => '');\n const retryable = isRetryableForBackoff(response.status);\n const err: ProviderError = {\n provider,\n status: response.status,\n retryable,\n message: `${provider} ${String(response.status)}: ${text.slice(0, 300)}`,\n };\n logger?.warn?.('llm.provider.error', { provider, status: response.status, attempt });\n if (!err.retryable || attempt === PER_PROVIDER_MAX_ATTEMPTS) {\n if (err.retryable) exhaustAndThrow(err); // retryable but exhausted\n throw err; // terminal non-retryable error — no cooldown\n }\n lastErr = err;\n } else {\n const gatewayRequestId = response.headers.get('cf-aig-request-id') ?? undefined;\n clearProviderCooldown(provider);\n return { json: await response.json(), gatewayRequestId, attempts: attempt };\n }\n } catch (e) {\n if (e instanceof DOMException && e.name === 'AbortError') throw e;\n if (typeof e === 'object' && e !== null && 'retryable' in e) {\n const err = e as ProviderError;\n if (!err.retryable || attempt === PER_PROVIDER_MAX_ATTEMPTS) {\n if (err.retryable) exhaustAndThrow(err); // retryable but exhausted\n throw err; // terminal — no cooldown\n }\n lastErr = err;\n } else {\n const err: ProviderError = {\n provider,\n status: 0,\n retryable: true,\n message: e instanceof Error ? e.message : String(e),\n };\n if (attempt === PER_PROVIDER_MAX_ATTEMPTS) exhaustAndThrow(err);\n lastErr = err;\n }\n }\n // Exponential backoff with jitter: base=500ms, cap=8000ms, jitter up to 250ms\n const backoffMs = computeBackoffMs(attempt - 1);\n await sleep(backoffMs, signal);\n }\n // Fallthrough — should not be reached, but mark cooling down defensively.\n markProviderCoolingDown(provider, nowFn ?? Date.now);\n throw lastErr ?? ({ provider, status: 0, retryable: false, message: 'exhausted' } as ProviderError);\n}\n\nfunction isProviderError(err: unknown): err is ProviderError {\n return (\n typeof err === 'object' &&\n err !== null &&\n typeof (err as { status?: unknown }).status === 'number' &&\n typeof (err as { message?: unknown }).message === 'string' &&\n typeof (err as { provider?: unknown }).provider === 'string'\n );\n}\n\n// ─── Routing ───────────────────────────────────────────────────────────────\n\ninterface RoutePlan {\n primary: { provider: LLMProvider; model: string };\n fallback?: { provider: LLMProvider; model: string };\n}\n\n/**\n * Providers whose request builder + response parser support tool-calling.\n * When `opts.tools` is set, routing is restricted to this set and **fails\n * closed** rather than silently calling a provider that would ignore the\n * tools. Expanded in 1b as the other providers' tool formats are normalized.\n */\nconst TOOL_CAPABLE_PROVIDERS = new Set<LLMProvider>(['anthropic', 'grok', 'deepseek', 'local']);\n\nfunction plan(tier: LLMTier, opts: LLMOptions, tokenEstimate: number): RoutePlan {\n if (opts.model) {\n // Explicit override — best-effort provider detection.\n const m = opts.model;\n if (m.startsWith('claude')) return { primary: { provider: 'anthropic', model: m } };\n if (m.startsWith('gemini')) return { primary: { provider: 'gemini', model: m } };\n if (m.startsWith('grok')) return { primary: { provider: 'grok', model: m } };\n if (m.startsWith('deepseek')) return { primary: { provider: 'deepseek', model: m } };\n if (m.startsWith('qwen')) return { primary: { provider: 'local', model: m } };\n return { primary: { provider: 'groq', model: m } };\n }\n const longContext = tokenEstimate >= (opts.longContextThreshold ?? DEFAULT_LONG_CONTEXT_THRESHOLD);\n switch (tier) {\n case 'workbench':\n return {\n primary: { provider: 'deepseek', model: MODELS.deepseek.workbench },\n fallback: { provider: 'groq', model: MODELS.groq.verifier },\n };\n case 'verifier':\n return { primary: { provider: 'groq', model: MODELS.groq.verifier } };\n case 'smart':\n return longContext\n ? {\n primary: { provider: 'gemini', model: MODELS.gemini.smart },\n fallback: { provider: 'anthropic', model: MODELS.anthropic.smart },\n }\n : {\n primary: { provider: 'anthropic', model: MODELS.anthropic.smart },\n fallback: { provider: 'gemini', model: MODELS.gemini.smart },\n };\n case 'fast':\n return {\n primary: { provider: 'grok', model: MODELS.grok.fast },\n fallback: { provider: 'anthropic', model: MODELS.anthropic.fast },\n };\n case 'balanced':\n default:\n return longContext\n ? {\n primary: { provider: 'gemini', model: MODELS.gemini.smart },\n fallback: { provider: 'anthropic', model: MODELS.anthropic.balanced },\n }\n : {\n primary: { provider: 'anthropic', model: MODELS.anthropic.balanced },\n fallback: { provider: 'gemini', model: MODELS.gemini.smart },\n };\n }\n}\n\n/**\n * Build the `cf-aig-metadata` header value for the Cloudflare AI Gateway.\n *\n * Carries caller attribution (project / workload / actor / runId) so a single\n * shared gateway can be sliced per-app and per-feature in the AI Gateway\n * dashboard and logs. This replaces the per-app-gateway convention: rather than\n * one gateway per app (which has to be provisioned and silently 401s when it\n * isn't), one gateway tags every request with who made it.\n *\n * Returns `undefined` when no attribution fields are set (header omitted).\n * The CF AI Gateway accepts a JSON object of string/number/boolean values.\n */\nfunction buildAigMetadata(opts: LLMOptions): string | undefined {\n const meta: Record<string, string> = {};\n if (opts.project) meta.project = opts.project;\n if (opts.workload) meta.workload = opts.workload;\n if (opts.actor) meta.actor = opts.actor;\n if (opts.runId) meta.runId = opts.runId;\n return Object.keys(meta).length > 0 ? JSON.stringify(meta) : undefined;\n}\n\nasync function callOne(\n leg: { provider: LLMProvider; model: string },\n messages: LLMMessage[],\n opts: LLMOptions,\n env: LLMEnv,\n fetchImpl: typeof fetch,\n logger: Logger | undefined,\n nowFn?: () => number,\n): Promise<{ parsed: { content: string; input: number; output: number; cacheRead?: number; cacheWrite?: number; model?: string; toolCalls?: LLMToolCall[]; stopReason?: LLMResult['stopReason'] }; gatewayRequestId?: string; attempts: number }> {\n let req: { url: string; headers: Record<string, string>; body: string };\n switch (leg.provider) {\n case 'anthropic':\n req = buildAnthropicRequest(leg.model, messages, opts, env);\n break;\n case 'gemini': {\n // Credential resolution is a network call (mint from key, or the metadata\n // server for ADC), so it must happen here rather than inside the\n // (synchronous) request builders. Cached per isolate, so it is almost\n // always a no-op after the first gemini call.\n const vertexAuth = await resolveVertexAuth(env, fetchImpl);\n req = buildGeminiRequest(leg.model, messages, opts, env, vertexAuth.token, vertexAuth.project);\n break;\n }\n case 'groq':\n req = buildGroqRequest(leg.model, messages, opts, env);\n break;\n case 'grok':\n req = buildGrokRequest(leg.model, messages, opts, env);\n break;\n case 'deepseek':\n req = buildDeepSeekRequest(leg.model, messages, opts, env);\n break;\n case 'local':\n req = buildLocalRequest(leg.model, messages, opts, env);\n break;\n }\n // Attribution for the shared AI Gateway — one gateway, sliced per-app/feature.\n const aigMetadata = buildAigMetadata(opts);\n if (aigMetadata) req.headers['cf-aig-metadata'] = aigMetadata;\n const { json, gatewayRequestId, attempts } = await callWithBackoff(\n leg.provider,\n req,\n fetchImpl,\n opts.signal,\n logger,\n nowFn,\n );\n switch (leg.provider) {\n case 'anthropic':\n return { parsed: parseAnthropic(json), gatewayRequestId, attempts };\n case 'gemini':\n return { parsed: parseGemini(json), gatewayRequestId, attempts };\n case 'groq':\n return { parsed: parseGroq(json), gatewayRequestId, attempts };\n case 'grok':\n return { parsed: parseOpenAi(json), gatewayRequestId, attempts };\n case 'deepseek':\n return { parsed: parseOpenAi(json), gatewayRequestId, attempts };\n case 'local':\n return {\n parsed: leg.model === MODELS.local.fast ? parseOllamaChat(json) : parseOpenAi(json),\n gatewayRequestId,\n attempts,\n };\n }\n}\n\n/**\n * Run a completion through the routing plan for the requested tier.\n *\n * Routing summary (0.3.0):\n * - `fast` → Grok 4.3; Anthropic Haiku fallback when Grok is unavailable\n * - `balanced` → Anthropic Sonnet; Gemini 2.5 Flash if `longContextThreshold` exceeded\n * - `smart` → Anthropic Opus; Gemini 2.5 Flash if long-context\n * - `verifier` → Groq Llama 3.3 70B (no fallback — verifier is inherently cheap/best-effort)\n * - `workbench` → DeepSeek Chat; Groq fallback for boring/reviewable internal batch jobs\n *\n * All provider traffic flows through Cloudflare AI Gateway at `AI_GATEWAY_BASE_URL`.\n *\n * Per-provider reliability guarantees (0.4.0):\n * - Exponential backoff with jitter on 429 / 5xx (base 500ms, cap 8s, up to 2 retries).\n * - Provider cooldown: after exhausting retries the provider is marked cooling down\n * for 30 seconds; subsequent calls skip it and go straight to the fallback leg.\n *\n * @param messages - Ordered chat history.\n * @param env - API key + gateway bindings.\n * @param opts - Optional tier/model/parameters override.\n * @param deps - Optional fetch/logger/clock injection (for testing).\n * @returns A {@link FactoryResponse} carrying either an {@link LLMResult} or\n * an error (`LLM_ALL_PROVIDERS_FAILED`, `LLM_RATE_LIMITED`, or `INTERNAL_ERROR`).\n */\nexport async function complete(\n messages: LLMMessage[],\n env: LLMEnv,\n opts: LLMOptions = {},\n deps: LLMDeps = {},\n): Promise<FactoryResponse<LLMResult>> {\n if (messages.length === 0) {\n throw new ValidationError('messages must not be empty');\n }\n if (!env.AI_GATEWAY_BASE_URL) {\n throw new ValidationError('AI_GATEWAY_BASE_URL is required in 0.3.0');\n }\n const fetchImpl = deps.fetch ?? fetch;\n const now = deps.now ?? (() => Date.now());\n const logger = deps.logger;\n const startedAt = now();\n\n const tier: LLMTier = opts.tier ?? 'balanced';\n const system = systemText(opts, messages);\n const tokenEstimate = estimateTokens(messages, system);\n let route = plan(tier, opts, tokenEstimate);\n // Cost optimization: prefer the dedicated Qwen 8B GPU ($0) for cheap `fast`-tier\n // work, with the original fast route (Grok→Haiku) as automatic fallback on any\n // error. Dormant unless LLM_LOCAL_FIRST + GPU_LLM_API_TOKEN are set.\n if (\n env.LLM_LOCAL_FIRST &&\n tier === 'fast' &&\n !opts.model &&\n env.GPU_LLM_API_TOKEN\n ) {\n route = { primary: { provider: 'local', model: MODELS.local.fast }, fallback: route.primary };\n }\n // Qwen 27B owns local agentic work. DeepSeek remains the independent fallback;\n // explicit model overrides always win.\n if (\n env.LLM_LOCAL_WORKBENCH &&\n tier === 'workbench' &&\n !opts.model &&\n env.GPU_LLM_API_TOKEN\n ) {\n route = { primary: { provider: 'local', model: MODELS.local.workbench }, fallback: route.primary };\n }\n\n // ── Org-level daily / monthly cap pre-check ──────────────────────────────\n const kv = env.LLM_COST_KV;\n const todayKey = `llm:daily-cost:${isoDate(now())}`;\n const monthKey = `llm:monthly-cost:${isoMonth(now())}`;\n if (kv) {\n if (opts.dailyCapUsd !== undefined) {\n const raw = await kv.get(todayKey).catch(() => null);\n const spent = parseFloat(raw ?? '0');\n if (spent >= opts.dailyCapUsd) {\n return toErrorResponse(\n new RateLimitError('LLM_DAILY_CAP_EXCEEDED', {\n spentUsd: spent,\n dailyCapUsd: opts.dailyCapUsd,\n }),\n );\n }\n }\n if (opts.monthlyCapUsd !== undefined) {\n const raw = await kv.get(monthKey).catch(() => null);\n const spent = parseFloat(raw ?? '0');\n if (spent >= opts.monthlyCapUsd) {\n return toErrorResponse(\n new RateLimitError('LLM_MONTHLY_CAP_EXCEEDED', {\n spentUsd: spent,\n monthlyCapUsd: opts.monthlyCapUsd,\n }),\n );\n }\n }\n }\n\n const attemptLog: Array<{ provider: LLMProvider; status?: number; message: string }> = [];\n\n let routeLegs = [route.primary, route.fallback].filter(Boolean) as Array<{ provider: LLMProvider; model: string }>;\n // Tool-calling fails closed: never fall back to a provider that can't honour\n // the tool schema. Narrow the route to tool-capable providers when tools are set.\n if (opts.tools && opts.tools.length > 0) {\n routeLegs = routeLegs.filter((l) => TOOL_CAPABLE_PROVIDERS.has(l.provider));\n if (routeLegs.length === 0) {\n throw new ValidationError(\n `tool-calling requires a tool-capable provider (${[...TOOL_CAPABLE_PROVIDERS].join(', ')}); tier '${tier}' has none — use tier fast/balanced/smart or a claude-* model override`,\n );\n }\n }\n for (const [legIndex, leg] of routeLegs.entries()) {\n // Skip providers that are currently in their cooldown window.\n if (isProviderCoolingDown(leg.provider, now)) {\n logger?.warn?.('llm.provider.coolingDown', { provider: leg.provider });\n attemptLog.push({ provider: leg.provider, message: 'skipped: cooling down' });\n continue;\n }\n if (opts.signal?.aborted) {\n return toErrorResponse(\n new InternalError('llm call aborted', { provider: leg.provider, model: leg.model }),\n );\n }\n try {\n const result = await callOne(leg, messages, opts, env, fetchImpl, logger, now);\n // A tool_use turn legitimately has no text content — only treat a\n // genuinely empty response (no text AND no tool calls) as a failure.\n if (!result.parsed.content && !(result.parsed.toolCalls && result.parsed.toolCalls.length > 0)) {\n throw { provider: leg.provider, status: 200, retryable: false, message: 'empty content' } satisfies ProviderError;\n }\n logger?.info?.('llm.complete', {\n provider: leg.provider,\n model: leg.model,\n tier,\n tokenEstimate,\n attempts: result.attempts,\n runId: opts.runId,\n project: opts.project,\n actor: opts.actor,\n workload: opts.workload,\n });\n const llmResult: LLMResult = {\n content: result.parsed.content,\n provider: leg.provider,\n model: result.parsed.model ?? leg.model,\n tier,\n tokens: {\n input: result.parsed.input,\n output: result.parsed.output,\n cacheRead: result.parsed.cacheRead,\n cacheWrite: result.parsed.cacheWrite,\n },\n latency: now() - startedAt,\n attempts: result.attempts,\n gatewayRequestId: result.gatewayRequestId,\n stopReason: result.parsed.stopReason,\n toolCalls: result.parsed.toolCalls,\n };\n const costUsd = estimateCostUsd(llmResult.tokens, llmResult.model);\n if (opts.maxCostUsd !== undefined && costUsd > opts.maxCostUsd) {\n if (kv && (opts.dailyCapUsd !== undefined || opts.monthlyCapUsd !== undefined)) {\n await recordOrgCostUsage(kv, todayKey, monthKey, costUsd, opts);\n }\n return toErrorResponse(\n new RateLimitError('LLM_COST_CAP_EXCEEDED', {\n costUsd,\n maxCostUsd: opts.maxCostUsd,\n model: llmResult.model,\n tokens: llmResult.tokens,\n }),\n );\n }\n // ── Update org-level cost accumulators in KV ─────────────────────────\n // The KV writes are best-effort. Cloudflare KV does not support atomic\n // compare-and-swap, so concurrent increments may undercount spend.\n if (kv && (opts.dailyCapUsd !== undefined || opts.monthlyCapUsd !== undefined)) {\n await recordOrgCostUsage(kv, todayKey, monthKey, costUsd, opts);\n }\n // ── Metering callback ────────────────────────────────────────────────\n if (deps.onRecord && opts.ledger) {\n const row: LLMRecordRow = {\n ...opts.ledger,\n model: llmResult.model,\n provider: llmResult.provider,\n tier: llmResult.tier,\n inputTokens: llmResult.tokens.input,\n outputTokens: llmResult.tokens.output,\n cacheReadTokens: llmResult.tokens.cacheRead ?? 0,\n cacheWriteTokens: llmResult.tokens.cacheWrite ?? 0,\n latencyMs: llmResult.latency,\n costUsd,\n yyyyMm: isoMonth(now()),\n };\n deps.onRecord(row).catch((e: unknown) => {\n logger?.warn?.('llm.onRecord.error', { message: e instanceof Error ? e.message : String(e) });\n });\n }\n return { data: llmResult, error: null };\n } catch (e) {\n if (e instanceof DOMException && e.name === 'AbortError') {\n return toErrorResponse(\n new InternalError('llm call aborted', { provider: leg.provider, model: leg.model }),\n );\n }\n if (isProviderError(e)) {\n attemptLog.push({ provider: e.provider, status: e.status, message: e.message });\n if (e.status === 429 && legIndex === routeLegs.length - 1) {\n return toErrorResponse(\n new RateLimitError(`llm rate limited on ${e.provider}`, { attempts: attemptLog }),\n );\n }\n logger?.warn?.('llm.leg.failed', { provider: leg.provider, status: e.status });\n continue;\n }\n attemptLog.push({ provider: leg.provider, message: e instanceof Error ? e.message : String(e) });\n }\n }\n\n return toErrorResponse(\n new InternalError('LLM_ALL_PROVIDERS_FAILED', { attempts: attemptLog, tier, tokenEstimate }),\n );\n}\n\n// ─── Streaming ────────────────────────────────────────────────────────────\n\n/**\n * Anthropic server-sent event shapes used by the streaming parser.\n * Only the fields we consume are typed; the rest are ignored.\n */\ninterface AnthropicStreamEvent {\n type: string;\n index?: number;\n delta?: { type?: string; text?: string; partial_json?: string; stop_reason?: string };\n content_block?: { type?: string; id?: string; name?: string };\n message?: {\n usage?: {\n input_tokens?: number;\n output_tokens?: number;\n cache_read_input_tokens?: number;\n cache_creation_input_tokens?: number;\n };\n model?: string;\n };\n usage?: {\n input_tokens?: number;\n output_tokens?: number;\n };\n}\n\n/**\n * Streams a completion from the primary Anthropic provider, yielding text chunks\n * as they arrive. Falls back to the non-streaming {@link complete} function when\n * the provider does not support streaming (i.e. a non-Anthropic primary is selected).\n *\n * The generator's **return value** (accessible via `gen.return()` or by consuming\n * the full iteration) is an {@link LLMResult} with the same shape as {@link complete}.\n *\n * Usage pattern:\n * ```ts\n * const gen = completionStream(messages, env, opts);\n * for await (const chunk of gen) {\n * // stream chunk to client\n * }\n * const result = (await gen.return(undefined)).value; // LLMResult\n * ```\n *\n * @param messages - Ordered chat history.\n * @param env - API key + gateway bindings.\n * @param opts - Optional tier/model/parameters override. Accepts `deps` as nested field.\n * @returns An async generator that yields `string` chunks and returns an {@link LLMResult}.\n */\nexport async function* completionStream(\n messages: LLMMessage[],\n env: LLMEnv,\n opts: LLMOptions & { deps?: LLMDeps } = {},\n): AsyncGenerator<string, LLMResult, unknown> {\n if (messages.length === 0) {\n throw new ValidationError('messages must not be empty');\n }\n if (!env.AI_GATEWAY_BASE_URL) {\n throw new ValidationError('AI_GATEWAY_BASE_URL is required in 0.3.0');\n }\n\n const deps: LLMDeps = opts.deps ?? {};\n const fetchImpl = deps.fetch ?? fetch;\n const now = deps.now ?? (() => Date.now());\n const logger = deps.logger;\n const startedAt = now();\n\n const tier: LLMTier = opts.tier ?? 'balanced';\n const system = systemText(opts, messages);\n const tokenEstimate = estimateTokens(messages, system);\n const route = plan(tier, opts, tokenEstimate);\n const streamLeg =\n route.primary.provider === 'grok' && !env.GROK_API_KEY && route.fallback?.provider === 'anthropic'\n ? route.fallback\n : route.primary;\n\n // Only Anthropic supports streaming in the current implementation.\n // For all other primaries, fall back to non-streaming complete().\n if (streamLeg.provider !== 'anthropic') {\n const result = await complete(messages, env, opts, deps);\n if (result.error !== null || result.data === null) {\n throw new InternalError('LLM_ALL_PROVIDERS_FAILED', { error: result.error });\n }\n yield result.data.content;\n return result.data;\n }\n\n // Check cooldown before attempting the streaming call.\n if (isProviderCoolingDown(streamLeg.provider, now)) {\n logger?.warn?.('llm.provider.coolingDown', { provider: streamLeg.provider });\n // Fall back to non-streaming complete() which will handle the fallback leg.\n const result = await complete(messages, env, opts, deps);\n if (result.error !== null || result.data === null) {\n throw new InternalError('LLM_ALL_PROVIDERS_FAILED', { error: result.error });\n }\n yield result.data.content;\n return result.data;\n }\n\n const req = buildAnthropicRequest(streamLeg.model, messages, opts, env, true);\n // Attribution for the shared AI Gateway (matches the non-streaming path).\n const streamAigMetadata = buildAigMetadata(opts);\n if (streamAigMetadata) req.headers['cf-aig-metadata'] = streamAigMetadata;\n\n let response: Response;\n try {\n response = await fetchImpl(req.url, {\n method: 'POST',\n headers: req.headers,\n body: req.body,\n // Fall back to a 60 s default when the caller provides no signal — prevents\n // a hung provider connection from consuming the Worker's wall-clock budget.\n signal: opts.signal ?? AbortSignal.timeout(60_000),\n });\n } catch (e) {\n if (e instanceof DOMException && e.name === 'AbortError') {\n throw new InternalError('llm call aborted', {\n provider: streamLeg.provider,\n model: streamLeg.model,\n });\n }\n throw new InternalError('llm stream fetch failed', {\n message: e instanceof Error ? e.message : String(e),\n });\n }\n\n if (!response.ok) {\n const text = await response.text().catch(() => '');\n const retryable = isRetryableForBackoff(response.status);\n if (retryable && response.status === 429) {\n markProviderCoolingDown(streamLeg.provider, now);\n }\n // Fall back to non-streaming complete() which will try the fallback leg.\n const result = await complete(messages, env, opts, deps);\n if (result.error !== null || result.data === null) {\n throw new InternalError('LLM_ALL_PROVIDERS_FAILED', {\n streamError: `${streamLeg.provider} ${String(response.status)}: ${text.slice(0, 300)}`,\n error: result.error,\n });\n }\n yield result.data.content;\n return result.data;\n }\n\n if (!response.body) {\n throw new InternalError('llm stream response body is null', {\n provider: streamLeg.provider,\n });\n }\n\n // Stream SSE events from Anthropic.\n const decoder = new TextDecoder();\n let accumulatedText = '';\n let inputTokens = 0;\n let outputTokens = 0;\n let cacheRead = 0;\n let cacheWrite = 0;\n let modelName: string | undefined;\n // Tool-use blocks arrive as content_block_start (id/name) then a sequence of\n // input_json_delta fragments that concatenate into the arguments JSON string.\n const toolBlocks = new Map<number, { id: string; name: string; json: string }>();\n let streamStopReason: LLMResult['stopReason'];\n const gatewayRequestId: string | undefined = response.headers.get('cf-aig-request-id') ?? undefined;\n\n const reader = response.body.getReader();\n let buffer = '';\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n buffer += decoder.decode(value, { stream: true });\n\n // SSE lines are delimited by '\\n'. Events are separated by '\\n\\n'.\n const lines = buffer.split('\\n');\n // Keep the last (potentially incomplete) line in the buffer.\n buffer = lines.pop() ?? '';\n\n for (const line of lines) {\n if (!line.startsWith('data: ')) continue;\n const data = line.slice(6).trim();\n if (data === '[DONE]') break;\n let event: AnthropicStreamEvent;\n try {\n event = JSON.parse(data) as AnthropicStreamEvent;\n } catch {\n continue; // Skip malformed SSE lines.\n }\n\n switch (event.type) {\n case 'message_start':\n inputTokens = event.message?.usage?.input_tokens ?? 0;\n cacheRead = event.message?.usage?.cache_read_input_tokens ?? 0;\n cacheWrite = event.message?.usage?.cache_creation_input_tokens ?? 0;\n modelName = event.message?.model;\n break;\n case 'content_block_start':\n if (\n event.content_block?.type === 'tool_use' &&\n typeof event.index === 'number' &&\n typeof event.content_block.id === 'string' &&\n typeof event.content_block.name === 'string'\n ) {\n toolBlocks.set(event.index, { id: event.content_block.id, name: event.content_block.name, json: '' });\n }\n break;\n case 'content_block_delta':\n if (event.delta?.type === 'text_delta' && typeof event.delta.text === 'string') {\n accumulatedText += event.delta.text;\n yield event.delta.text;\n } else if (\n event.delta?.type === 'input_json_delta' &&\n typeof event.delta.partial_json === 'string' &&\n typeof event.index === 'number'\n ) {\n const block = toolBlocks.get(event.index);\n if (block) block.json += event.delta.partial_json;\n }\n break;\n case 'message_delta':\n outputTokens = event.usage?.output_tokens ?? outputTokens;\n if (event.delta?.stop_reason) streamStopReason = normalizeAnthropicStop(event.delta.stop_reason);\n break;\n default:\n break;\n }\n }\n }\n } finally {\n reader.releaseLock();\n }\n\n clearProviderCooldown(streamLeg.provider);\n logger?.info?.('llm.completionStream', {\n provider: streamLeg.provider,\n model: streamLeg.model,\n tier,\n tokenEstimate,\n runId: opts.runId,\n project: opts.project,\n actor: opts.actor,\n workload: opts.workload,\n });\n\n const toolCalls: LLMToolCall[] = [...toolBlocks.values()].map((b) => ({\n id: b.id,\n name: b.name,\n arguments: parseToolArgs(b.json),\n }));\n\n return {\n content: accumulatedText,\n provider: streamLeg.provider,\n model: modelName ?? streamLeg.model,\n tier,\n tokens: { input: inputTokens, output: outputTokens, cacheRead, cacheWrite },\n latency: now() - startedAt,\n attempts: 1,\n gatewayRequestId,\n stopReason: streamStopReason,\n toolCalls: toolCalls.length > 0 ? toolCalls : undefined,\n };\n}\n\n// ─── Grounding assertion ───────────────────────────────────────────────────\n\n/**\n * Returns `true` if `response` contains at least one verbatim phrase of at\n * least 5 consecutive whitespace-delimited tokens that also appears in one of\n * the `sources` strings.\n *\n * Returns `true` unconditionally when `sources` is empty (no grounding\n * documents means grounding cannot be violated).\n *\n * This is a lightweight guard for RAG pipelines — it detects obvious\n * hallucinations where the model generates content not present in any\n * retrieved source. It is NOT a semantic similarity check.\n *\n * @param response - The LLM-generated text to inspect.\n * @param sources - Retrieved source documents to check against.\n * @returns `true` if the response is grounded, `false` if hallucination detected.\n *\n * @example\n * ```ts\n * const grounded = assertGrounding(llmAnswer, retrievedDocs);\n * if (!grounded) {\n * // flag or re-rank the response\n * }\n * ```\n */\nexport function assertGrounding(response: string, sources: string[]): boolean {\n if (sources.length === 0) return true;\n\n const WINDOW = 5;\n const responseTokens = response.split(/\\s+/).filter((t) => t.length > 0);\n\n if (responseTokens.length < WINDOW) return false;\n\n // Build a set of all 5-token ngrams from each source for O(n) lookup.\n const sourceNgrams = new Set<string>();\n for (const source of sources) {\n const tokens = source.split(/\\s+/).filter((t) => t.length > 0);\n for (let i = 0; i <= tokens.length - WINDOW; i++) {\n const ngram = tokens.slice(i, i + WINDOW).join(' ');\n sourceNgrams.add(ngram);\n }\n }\n\n if (sourceNgrams.size === 0) return false;\n\n // Slide a window of WINDOW tokens over the response and check for a match.\n for (let i = 0; i <= responseTokens.length - WINDOW; i++) {\n const ngram = responseTokens.slice(i, i + WINDOW).join(' ');\n if (sourceNgrams.has(ngram)) return true;\n }\n\n return false;\n}\n\n// ─── Exported helpers (kept for existing consumers) ───────────────────────\n\nexport { MODELS, isProviderCoolingDown, markProviderCoolingDown, clearProviderCooldown, PROVIDER_COOLDOWN_MS };\nexport { BASE_BACKOFF_MS };\n\n// ─── Embeddings ────────────────────────────────────────────────────────────\n\nexport { embed, embedLocal, DEFAULT_EMBEDDING_MODEL, LOCAL_EMBEDDING_MODEL } from './embed.js';\nexport type { AiBinding, EmbedResult, EmbeddingModel, LocalEmbedEnv } from './embed.js';\n\nexport { mintGcpAccessToken, clearGcpTokenCache, serviceAccountProjectId } from './gcp-token.js';\n","/**\n * Mint short-lived Google Cloud OAuth2 access tokens from a service-account key,\n * inside a Cloudflare Worker.\n *\n * The `gemini` leg authenticates to Vertex AI with a GCP access token, which\n * expires after one hour. A token therefore cannot be a durable Worker secret —\n * any stored value is stale almost immediately. Callers supply the long-lived\n * service-account key instead, and this module exchanges it for a fresh token\n * via the JWT-bearer flow, caching the result per isolate.\n *\n * Web Crypto only — no Node.js built-ins, no `Buffer`. Lifted from\n * `apps/admin-studio/src/lib/gcp-secrets.ts`, where this exact flow has been\n * running in production; hoisted here so every consumer of the `gemini` leg\n * gets a working credential rather than just the one app that hand-rolled it.\n */\nimport { InternalError, ValidationError } from '@latimer-woods-tech/errors';\n\n/** The subset of a GCP service-account JSON key needed for the JWT-bearer flow. */\ninterface ServiceAccountKey {\n project_id: string;\n private_key: string;\n client_email: string;\n token_uri: string;\n}\n\ninterface OAuth2Response {\n access_token: string;\n expires_in: number;\n}\n\n/** Refresh this long before expiry so an in-flight request never races the clock. */\nconst EXPIRY_SKEW_MS = 300_000;\n\n/**\n * Per-isolate token cache, keyed by service-account email.\n *\n * Caching a *string* across requests is safe. Caching an I/O object (a socket, a\n * database client, a `Response`) is not — Workers rejects reuse of I/O created\n * on behalf of a different request. This holds only the token text and its\n * expiry, so it is reused freely.\n */\nconst tokenCache = new Map<string, { token: string; expiresAt: number }>();\n\n/** Decode a base64url-encoded service-account key, tolerating raw JSON too. */\nfunction parseServiceAccountKey(gcpSaKey: string): ServiceAccountKey {\n const text = gcpSaKey.trimStart().startsWith('{') ? gcpSaKey : atob(gcpSaKey);\n const key = JSON.parse(text) as ServiceAccountKey;\n if (!key.client_email || !key.private_key || !key.token_uri) {\n throw new ValidationError('GCP_SA_KEY is missing client_email, private_key, or token_uri');\n }\n return key;\n}\n\n/** Encode bytes as base64url (RFC 4648 §5): `-`/`_` for `+`/`/`, no padding. */\nfunction base64UrlEncode(bytes: Uint8Array): string {\n let binary = '';\n for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]!);\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/g, '');\n}\n\n/** Import a PEM-encoded PKCS#8 private key as a Web Crypto signing key. */\nasync function importPrivateKey(pem: string): Promise<CryptoKey> {\n const body = pem\n .split('\\n')\n .filter((line) => !line.startsWith('-----'))\n .join('');\n const binary = atob(body);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);\n return crypto.subtle.importKey(\n 'pkcs8',\n bytes.buffer,\n { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' },\n false,\n ['sign'],\n );\n}\n\n/** Build a service-account JWT assertion signed with RS256, per GCP's spec. */\nasync function createAssertion(key: ServiceAccountKey, nowSeconds: number): Promise<string> {\n const encoder = new TextEncoder();\n const header = base64UrlEncode(encoder.encode(JSON.stringify({ alg: 'RS256', typ: 'JWT' })));\n const payload = base64UrlEncode(\n encoder.encode(\n JSON.stringify({\n iss: key.client_email,\n scope: 'https://www.googleapis.com/auth/cloud-platform',\n aud: key.token_uri,\n exp: nowSeconds + 3600,\n iat: nowSeconds,\n }),\n ),\n );\n const signingInput = `${header}.${payload}`;\n const cryptoKey = await importPrivateKey(key.private_key);\n const signature = await crypto.subtle.sign(\n 'RSASSA-PKCS1-v1_5',\n cryptoKey,\n encoder.encode(signingInput).buffer,\n );\n return `${signingInput}.${base64UrlEncode(new Uint8Array(signature))}`;\n}\n\n/**\n * Exchange a service-account key for a cloud-platform access token, reusing a\n * cached token until it is within {@link EXPIRY_SKEW_MS} of expiring.\n *\n * @param gcpSaKey - Service-account JSON key, base64-encoded or raw.\n * @param fetchImpl - Fetch implementation (injectable for tests).\n * @returns A bearer token valid for Vertex AI.\n * @throws ValidationError when the key is malformed; InternalError when the exchange fails.\n */\nexport async function mintGcpAccessToken(gcpSaKey: string, fetchImpl: typeof fetch): Promise<string> {\n const key = parseServiceAccountKey(gcpSaKey);\n\n const now = Date.now();\n const cached = tokenCache.get(key.client_email);\n if (cached && cached.expiresAt - EXPIRY_SKEW_MS > now) return cached.token;\n\n const assertion = await createAssertion(key, Math.floor(now / 1000));\n const response = await fetchImpl(key.token_uri, {\n method: 'POST',\n headers: { 'content-type': 'application/x-www-form-urlencoded' },\n body: new URLSearchParams({\n grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',\n assertion,\n }).toString(),\n }).catch((cause: unknown) => {\n throw new InternalError(`GCP token exchange failed: ${String(cause)}`);\n });\n\n if (!response.ok) {\n throw new InternalError(`GCP token exchange returned ${response.status}`);\n }\n\n const data = (await response.json()) as OAuth2Response;\n if (!data.access_token) {\n throw new InternalError('GCP token exchange returned no access_token');\n }\n\n // Trust the server's TTL rather than assuming the 3600s default.\n const ttlMs = (data.expires_in > 0 ? data.expires_in : 3600) * 1000;\n tokenCache.set(key.client_email, { token: data.access_token, expiresAt: now + ttlMs });\n return data.access_token;\n}\n\n// ─── Application Default Credentials (GCP metadata server) ───────────────────\n/**\n * GCE / Cloud Run metadata server base. Reachable only from GCP compute; the\n * hostname does not resolve elsewhere, so an off-GCP probe fails fast.\n */\nconst METADATA_BASE = 'http://metadata.google.internal/computeMetadata/v1';\n/** Required header for every metadata-server request (anti-SSRF guard). */\nconst METADATA_HEADER = { 'Metadata-Flavor': 'Google' } as const;\n/**\n * Bound the metadata probe so a non-GCP environment (where the host may hang\n * rather than refuse) falls through to the next provider quickly.\n */\nconst METADATA_TIMEOUT_MS = 2_000;\n\n/** Per-isolate cache for the ADC (metadata-server) credential. */\nlet adcCache: { token: string; project?: string; expiresAt: number } | undefined;\n\ninterface AdcCredential {\n token: string;\n /** The instance's own GCP project id, as reported by the metadata server. */\n project?: string;\n}\n\n/**\n * Obtain an access token (and project id) from the GCP metadata server using\n * Application Default Credentials — the keyless credential every Cloud Run /\n * GCE workload already has via its attached service account. No key, no env\n * var: the instance SA's cloud-platform token is served by the metadata\n * server. This mirrors the render-runner's own `_meta_token` shell helper.\n *\n * Fails (throws {@link InternalError}) when the metadata server is unreachable\n * or returns no token — i.e. off-GCP — so the caller falls through to the next\n * provider leg. Never throws a bare `AbortError` (which the router treats as a\n * hard cancellation): an internal timeout is surfaced as an `InternalError`.\n *\n * @param fetchImpl - Fetch implementation (injectable for tests).\n * @returns A bearer token valid for Vertex AI plus the ambient project id.\n */\nexport async function fetchAdcAccessToken(fetchImpl: typeof fetch): Promise<AdcCredential> {\n const now = Date.now();\n if (adcCache && adcCache.expiresAt - EXPIRY_SKEW_MS > now) {\n return { token: adcCache.token, project: adcCache.project };\n }\n\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), METADATA_TIMEOUT_MS);\n try {\n const tokenRes = await fetchImpl(\n `${METADATA_BASE}/instance/service-accounts/default/token`,\n { headers: METADATA_HEADER, signal: controller.signal },\n );\n if (!tokenRes.ok) {\n throw new InternalError(`GCP metadata token endpoint returned ${tokenRes.status}`);\n }\n const data = (await tokenRes.json()) as OAuth2Response;\n if (!data.access_token) {\n throw new InternalError('GCP metadata token endpoint returned no access_token');\n }\n\n // Project id is best-effort: a caller may pin VERTEX_PROJECT instead.\n let project: string | undefined;\n try {\n const projRes = await fetchImpl(`${METADATA_BASE}/project/project-id`, {\n headers: METADATA_HEADER,\n signal: controller.signal,\n });\n if (projRes.ok) project = (await projRes.text()).trim() || undefined;\n } catch {\n /* project id is optional — the token is still usable */\n }\n\n const ttlMs = (data.expires_in > 0 ? data.expires_in : 3600) * 1000;\n adcCache = { token: data.access_token, project, expiresAt: now + ttlMs };\n return { token: data.access_token, project };\n } catch (cause) {\n // Normalize an internal-timeout AbortError into an InternalError so the\n // router falls through to the next leg instead of hard-cancelling the call.\n if (cause instanceof InternalError) throw cause;\n throw new InternalError(`GCP metadata credential unavailable: ${String(cause)}`);\n } finally {\n clearTimeout(timer);\n }\n}\n\n/** Reset the per-isolate token cache. Tests only. */\nexport function clearGcpTokenCache(): void {\n tokenCache.clear();\n adcCache = undefined;\n}\n\n/** The project the service account belongs to — the Vertex project by default. */\nexport function serviceAccountProjectId(gcpSaKey: string): string {\n return parseServiceAccountKey(gcpSaKey).project_id;\n}\n","/**\n * Workers AI embedding API.\n *\n * Pins to bge-base-en-v1.5 (768-dim cosine) — the platform standard.\n * @remarks Local-rail errors use @latimer-woods-tech/errors (observability ratchet).\n * model_version is returned with every result so callers can record it\n * for provenance-aware re-embed when the model is swapped.\n *\n * Contract: inject the Workers AI binding (env.AI) — never import a vendor SDK.\n */\n\nimport { InternalError } from '@latimer-woods-tech/errors';\n\n/** The platform-standard embedding model (768-dim cosine) used when no override is given. */\nexport const DEFAULT_EMBEDDING_MODEL = '@cf/baai/bge-base-en-v1.5';\n\n/** Supported embedding model identifiers — widen deliberately, since dims are part of the contract. */\nexport type EmbeddingModel = '@cf/baai/bge-base-en-v1.5';\n\n/** One embedding call's output: the vectors plus the provenance needed to re-embed later. */\nexport interface EmbedResult {\n vectors: number[][];\n model: string;\n dims: number;\n}\n\n/** Minimal Workers AI binding shape needed for embeddings. */\nexport interface AiBinding {\n run(\n model: string,\n inputs: { text: string | string[] },\n ): Promise<{ data: number[][] }>;\n}\n\n/**\n * Embeds one or more text strings using the Workers AI binding.\n *\n * @param ai - Workers AI binding (`env.AI`) — injected, not imported.\n * @param input - Single string or array of strings to embed.\n * @param opts - Optional model override (must be a supported EmbeddingModel).\n * @returns Embedding vectors, model name, and dimension count.\n *\n * @throws When the AI binding call fails (let the caller decide whether to catch).\n */\nexport async function embed(\n ai: AiBinding,\n input: string | string[],\n opts?: { model?: EmbeddingModel },\n): Promise<EmbedResult> {\n const model = opts?.model ?? DEFAULT_EMBEDDING_MODEL;\n const texts = Array.isArray(input) ? input : [input];\n const result = await ai.run(model, { text: texts });\n const vectors = result.data;\n if (!vectors || vectors.length === 0) {\n throw new Error(`embed(): Workers AI returned no vectors for model ${model}`);\n }\n return {\n vectors,\n model,\n dims: vectors[0]!.length,\n };\n}\n\n/**\n * Self-hosted GPU embedding model (nomic-embed-text, 768-dim), reached through\n * the same CF AI Gateway custom provider as the local chat provider.\n *\n * ⚠️ VECTOR-SPACE WARNING: nomic-embed-text and bge-base-en-v1.5 are BOTH 768-dim\n * but occupy DIFFERENT embedding spaces. Their vectors are NOT comparable. A store\n * built with one model must be QUERIED — and, when migrating, RE-EMBEDDED — with\n * the SAME model. Never write vectors from both models into one store.\n */\nexport const LOCAL_EMBEDDING_MODEL = 'nomic-embed-text';\n\n/** Env subset needed to reach the local embedding rail (shares GPU_LLM_* with the chat provider). */\nexport interface LocalEmbedEnv {\n AI_GATEWAY_BASE_URL: string;\n GPU_LLM_API_TOKEN?: string;\n GPU_LLM_ACCESS_CLIENT_ID?: string;\n GPU_LLM_ACCESS_CLIENT_SECRET?: string;\n}\n\n/**\n * Embeds text on the self-hosted GPU rail (nomic-embed-text) via the AI Gateway\n * custom provider — the $0 alternative to Workers AI {@link embed}.\n *\n * Unlike the local CHAT provider, this has NO cross-provider fallback ON PURPOSE:\n * falling back to Workers AI (bge) would silently write an incompatible-space\n * vector into a nomic store and corrupt retrieval. If the rail is unavailable\n * this THROWS and the caller decides (retry local, or defer the write) — it must\n * NOT substitute a different model.\n *\n * @param env - Gateway base URL + GPU_LLM_* auth (same secrets as the chat provider).\n * @param input - Single string or array of strings to embed.\n * @returns Embedding vectors, model `nomic-embed-text`, and dimension count.\n * @throws On any transport/HTTP/empty-response error (no silent fallback).\n */\nexport async function embedLocal(\n env: LocalEmbedEnv,\n input: string | string[],\n): Promise<EmbedResult> {\n const texts = Array.isArray(input) ? input : [input];\n const res = await fetch(\n `${env.AI_GATEWAY_BASE_URL}/custom-local-gpu/v1/embeddings`,\n {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n // CF Access service-token headers only sent when present (bearer-only rails still work).\n ...(env.GPU_LLM_API_TOKEN ? { Authorization: `Bearer ${env.GPU_LLM_API_TOKEN}` } : {}),\n ...(env.GPU_LLM_ACCESS_CLIENT_ID ? { 'CF-Access-Client-Id': env.GPU_LLM_ACCESS_CLIENT_ID } : {}),\n ...(env.GPU_LLM_ACCESS_CLIENT_SECRET ? { 'CF-Access-Client-Secret': env.GPU_LLM_ACCESS_CLIENT_SECRET } : {}),\n },\n body: JSON.stringify({ model: LOCAL_EMBEDDING_MODEL, input: texts }),\n },\n );\n if (!res.ok) {\n throw new InternalError(\n `embedLocal(): rail ${res.status}: ${(await res.text()).slice(0, 160)}`,\n );\n }\n const json = (await res.json()) as { data?: Array<{ embedding: number[] }> };\n const vectors = (json.data ?? []).map((d) => d.embedding);\n if (vectors.length === 0) {\n throw new InternalError(\n `embedLocal(): rail returned no vectors for model ${LOCAL_EMBEDDING_MODEL}`,\n );\n }\n return {\n vectors,\n model: LOCAL_EMBEDDING_MODEL,\n dims: vectors[0]!.length,\n };\n}\n"],"mappings":";AAAA;AAAA,EACE,iBAAAA;AAAA,EACA;AAAA,EACA,mBAAAC;AAAA,EACA;AAAA,OAEK;;;ACSP,SAAS,eAAe,uBAAuB;AAgB/C,IAAM,iBAAiB;AAUvB,IAAM,aAAa,oBAAI,IAAkD;AAGzE,SAAS,uBAAuB,UAAqC;AACnE,QAAM,OAAO,SAAS,UAAU,EAAE,WAAW,GAAG,IAAI,WAAW,KAAK,QAAQ;AAC5E,QAAM,MAAM,KAAK,MAAM,IAAI;AAC3B,MAAI,CAAC,IAAI,gBAAgB,CAAC,IAAI,eAAe,CAAC,IAAI,WAAW;AAC3D,UAAM,IAAI,gBAAgB,+DAA+D;AAAA,EAC3F;AACA,SAAO;AACT;AAGA,SAAS,gBAAgB,OAA2B;AAClD,MAAI,SAAS;AACb,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,IAAK,WAAU,OAAO,aAAa,MAAM,CAAC,CAAE;AAC9E,SAAO,KAAK,MAAM,EAAE,QAAQ,OAAO,GAAG,EAAE,QAAQ,OAAO,GAAG,EAAE,QAAQ,QAAQ,EAAE;AAChF;AAGA,eAAe,iBAAiB,KAAiC;AAC/D,QAAM,OAAO,IACV,MAAM,IAAI,EACV,OAAO,CAAC,SAAS,CAAC,KAAK,WAAW,OAAO,CAAC,EAC1C,KAAK,EAAE;AACV,QAAM,SAAS,KAAK,IAAI;AACxB,QAAM,QAAQ,IAAI,WAAW,OAAO,MAAM;AAC1C,WAAS,IAAI,GAAG,IAAI,OAAO,QAAQ,IAAK,OAAM,CAAC,IAAI,OAAO,WAAW,CAAC;AACtE,SAAO,OAAO,OAAO;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