vern-llm 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/index.cjs +104 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +81 -16
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +81 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +104 -36
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
<p align="center">
|
|
8
8
|
<a href="https://github.com/LakBud/vernLLM">GitHub</a> ·
|
|
9
|
-
<a href="https://
|
|
9
|
+
<a href="https://vernllm.vercel.app">Documentation</a> ·
|
|
10
10
|
<a href="https://www.npmjs.com/package/vern-llm">npm</a>
|
|
11
11
|
</p>
|
|
12
12
|
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
|
|
23
23
|
Production-ready resilience for LLM calls; Retries, timeouts, caching, and circuit breaking for any OpenAI-compatible chat completion API (OpenAI, Groq, Anthropic, Gemini, Bedrock, and more via adapters).
|
|
24
24
|
|
|
25
|
-
**Full documentation: [
|
|
25
|
+
**Full documentation: [vernllm.vercel.app](https://vernllm.vercel.app)** — installation, structured output, caching, circuit breaker, every adapter, and the complete API reference all live there and are kept up to date. This README is a quick pitch, not the manual.
|
|
26
26
|
|
|
27
27
|
## Install
|
|
28
28
|
|
|
@@ -60,7 +60,7 @@ const result = await llm.call({
|
|
|
60
60
|
- **One interface, every provider**: OpenAI, Groq, Mistral, DeepSeek, Cerebras, Together, Fireworks, Ollama, Anthropic, Gemini, Bedrock, or raw HTTP via `fromFetch`
|
|
61
61
|
- **Zero bundled deps**: `zod` and provider SDKs are peer dependencies; this package only relies on their shapes structurally
|
|
62
62
|
|
|
63
|
-
See the [docs](https://
|
|
63
|
+
See the [docs](https://vernllm.vercel.app) for adapter setup, caching, the circuit breaker, and structured output in depth.
|
|
64
64
|
|
|
65
65
|
## Development
|
|
66
66
|
|
package/dist/index.cjs
CHANGED
|
@@ -357,28 +357,53 @@ var VernLLM = class {
|
|
|
357
357
|
return this.parseAndValidate(content, params.schema);
|
|
358
358
|
}
|
|
359
359
|
/**
|
|
360
|
+
* Anthropic and Gemini both require strict user/assistant alternation
|
|
361
|
+
* (and reject or silently mishandle two consecutive same-role turns), so
|
|
362
|
+
* this validates `history` up front rather than letting a malformed
|
|
363
|
+
* request surface as a confusing provider-side error. Thrown as a
|
|
364
|
+
* validation LLMError, which `shouldRetry` never retries, since retrying
|
|
365
|
+
* the same malformed input can't succeed
|
|
366
|
+
*/
|
|
367
|
+
validateHistory(history) {
|
|
368
|
+
let previousRole;
|
|
369
|
+
for (const [index, turn] of history.entries()) {
|
|
370
|
+
if (turn.role !== "user" && turn.role !== "assistant") throw new LLMError(`Invalid history[${index}].role "${turn.role}": must be "user" or "assistant"`, "validation");
|
|
371
|
+
if (turn.role === previousRole) throw new LLMError(`history must alternate user/assistant turns: consecutive "${turn.role}" turns at history[${index - 1}] and history[${index}]`, "validation");
|
|
372
|
+
previousRole = turn.role;
|
|
373
|
+
}
|
|
374
|
+
if (previousRole === "user") throw new LLMError("The last entry in history is a \"user\" turn, which would collide with the current userContent turn. history must end with an \"assistant\" turn (or be empty).", "validation");
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
360
377
|
* Applies per call defaults and shapes the params into the request
|
|
361
378
|
* object expected by the underlying client, including the resolved
|
|
362
379
|
* response format. Also returns whether JSON parsing should be
|
|
363
380
|
* applied to the response and which model was ultimately used
|
|
364
381
|
*/
|
|
365
382
|
buildRequestPayload(params) {
|
|
366
|
-
const { systemPrompt, userContent, temperature = .2, jsonMode = true, maxTokens = this.defaultMaxTokens, model = this.model, reasoningEffort, jsonSchema } = params;
|
|
383
|
+
const { systemPrompt, userContent, history = [], temperature = .2, jsonMode = true, maxTokens = this.defaultMaxTokens, model = this.model, reasoningEffort, jsonSchema } = params;
|
|
367
384
|
const useJson = jsonMode || Boolean(jsonSchema);
|
|
368
385
|
const responseFormat = this.buildResponseFormat(jsonSchema, useJson);
|
|
386
|
+
this.validateHistory(history);
|
|
369
387
|
const request = {
|
|
370
388
|
model,
|
|
371
389
|
temperature,
|
|
372
390
|
max_tokens: maxTokens,
|
|
373
391
|
...responseFormat ? { response_format: responseFormat } : {},
|
|
374
392
|
...reasoningEffort ? { reasoning_effort: reasoningEffort } : {},
|
|
375
|
-
messages: [
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
393
|
+
messages: [
|
|
394
|
+
{
|
|
395
|
+
role: "system",
|
|
396
|
+
content: systemPrompt
|
|
397
|
+
},
|
|
398
|
+
...history.map((turn) => ({
|
|
399
|
+
role: turn.role,
|
|
400
|
+
content: turn.content
|
|
401
|
+
})),
|
|
402
|
+
{
|
|
403
|
+
role: "user",
|
|
404
|
+
content: userContent
|
|
405
|
+
}
|
|
406
|
+
]
|
|
382
407
|
};
|
|
383
408
|
return {
|
|
384
409
|
useJson,
|
|
@@ -515,19 +540,33 @@ var VernLLM = class {
|
|
|
515
540
|
//#region src/adapters/anthropic.ts
|
|
516
541
|
/**
|
|
517
542
|
* Wraps an Anthropic SDK client so it satisfies the same `LLMClient`
|
|
518
|
-
* interface VernLLM uses for OpenAI/Groq.
|
|
519
|
-
*
|
|
520
|
-
*
|
|
521
|
-
*
|
|
543
|
+
* interface VernLLM uses for OpenAI/Groq.
|
|
544
|
+
*
|
|
545
|
+
* `response_format: json_schema` is mapped to Anthropics forced tool-use:
|
|
546
|
+
* a single tool is defined with `input_schema` set to the caller's schema,
|
|
547
|
+
* and `tool_choice` forces the model to call it, so the output is
|
|
548
|
+
* provider-constrained to match the schema rather than merely instructed
|
|
549
|
+
* to via prompt text (the same guarantee OpenAIs native `json_schema` mode
|
|
550
|
+
* gives, built on Anthropics tool-calling primitive instead).
|
|
551
|
+
*
|
|
552
|
+
* `response_format: json_object` (no schema to build a tool from) falls
|
|
553
|
+
* back to a system-prompt instruction, since theres nothing to constrain
|
|
554
|
+
* generation against.
|
|
522
555
|
*/
|
|
523
556
|
function fromAnthropic(anthropicClient) {
|
|
524
557
|
return { chat: { completions: { async create(params, options) {
|
|
525
558
|
const systemMessage = params.messages.find((m) => m.role === "system");
|
|
526
|
-
const
|
|
559
|
+
const conversationMessages = params.messages.filter((m) => m.role === "user" || m.role === "assistant");
|
|
560
|
+
const toolName = params.response_format?.type === "json_schema" ? params.response_format.json_schema.name : void 0;
|
|
527
561
|
let jsonInstruction;
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
562
|
+
let tools;
|
|
563
|
+
if (params.response_format?.type === "json_schema" && toolName) {
|
|
564
|
+
const { schema, description } = params.response_format.json_schema;
|
|
565
|
+
tools = [{
|
|
566
|
+
name: toolName,
|
|
567
|
+
description,
|
|
568
|
+
input_schema: schema
|
|
569
|
+
}];
|
|
531
570
|
} else if (params.response_format?.type === "json_object") jsonInstruction = "Respond with valid JSON only, no prose or markdown fences.";
|
|
532
571
|
const system = [systemMessage?.content, jsonInstruction].filter(Boolean).join("\n\n");
|
|
533
572
|
const response = await anthropicClient.messages.create({
|
|
@@ -535,12 +574,23 @@ function fromAnthropic(anthropicClient) {
|
|
|
535
574
|
max_tokens: params.max_tokens,
|
|
536
575
|
temperature: params.temperature,
|
|
537
576
|
system: system || void 0,
|
|
538
|
-
messages:
|
|
539
|
-
role:
|
|
577
|
+
messages: conversationMessages.map((m) => ({
|
|
578
|
+
role: m.role,
|
|
540
579
|
content: m.content
|
|
541
|
-
}))
|
|
580
|
+
})),
|
|
581
|
+
...tools ? {
|
|
582
|
+
tools,
|
|
583
|
+
tool_choice: {
|
|
584
|
+
type: "tool",
|
|
585
|
+
name: toolName
|
|
586
|
+
}
|
|
587
|
+
} : {}
|
|
542
588
|
}, options);
|
|
543
|
-
|
|
589
|
+
let text;
|
|
590
|
+
if (toolName) {
|
|
591
|
+
const toolUse = response.content.find((block) => block.type === "tool_use" && block.name === toolName);
|
|
592
|
+
text = toolUse ? JSON.stringify(toolUse.input) : "";
|
|
593
|
+
} else text = response.content.find((block) => block.type === "text")?.text ?? "";
|
|
544
594
|
return {
|
|
545
595
|
choices: [{ message: { content: text } }],
|
|
546
596
|
usage: {
|
|
@@ -568,7 +618,7 @@ function fromAnthropic(anthropicClient) {
|
|
|
568
618
|
function fromGemini(geminiClient) {
|
|
569
619
|
return { chat: { completions: { async create(params, options) {
|
|
570
620
|
const systemMessage = params.messages.find((m) => m.role === "system");
|
|
571
|
-
const
|
|
621
|
+
const conversationMessages = params.messages.filter((m) => m.role === "user" || m.role === "assistant");
|
|
572
622
|
const wantsJson = Boolean(params.response_format);
|
|
573
623
|
const generationConfig = {
|
|
574
624
|
temperature: params.temperature,
|
|
@@ -578,8 +628,8 @@ function fromGemini(geminiClient) {
|
|
|
578
628
|
if (params.response_format?.type === "json_schema") generationConfig.responseSchema = params.response_format.json_schema.schema;
|
|
579
629
|
const response = await geminiClient.generateContent({
|
|
580
630
|
model: params.model,
|
|
581
|
-
contents:
|
|
582
|
-
role: "user",
|
|
631
|
+
contents: conversationMessages.map((m) => ({
|
|
632
|
+
role: m.role === "assistant" ? "model" : "user",
|
|
583
633
|
parts: [{ text: m.content }]
|
|
584
634
|
})),
|
|
585
635
|
systemInstruction: systemMessage ? { parts: [{ text: systemMessage.content }] } : void 0,
|
|
@@ -607,35 +657,53 @@ function fromGemini(geminiClient) {
|
|
|
607
657
|
* regardless of which underlying model `modelId` points at, as long as
|
|
608
658
|
* that model supports Converse (most current-generation ones do)
|
|
609
659
|
*
|
|
610
|
-
*
|
|
611
|
-
*
|
|
612
|
-
*
|
|
613
|
-
*
|
|
614
|
-
*
|
|
660
|
+
* `response_format: json_schema` is mapped to Converses `toolConfig`: a
|
|
661
|
+
* single tool is defined from the schema and `toolChoice` forces the model
|
|
662
|
+
* to call it, constraining output at generation time rather than merely
|
|
663
|
+
* instructing for it via prompt text. Native tool support varies by model
|
|
664
|
+
* family (most current-generation ones support it via Converse; check your
|
|
665
|
+
* specific `modelId` if a call fails with an unsupported-parameter error).
|
|
666
|
+
* `response_format: json_object` (no schema to build a tool from) and
|
|
667
|
+
* `reasoning_effort` (no Converse equivalent) fall back to a system-prompt
|
|
668
|
+
* instruction and are dropped respectively.
|
|
615
669
|
*/
|
|
616
670
|
function fromBedrock(bedrockClient) {
|
|
617
671
|
return { chat: { completions: { async create(params, options) {
|
|
618
672
|
const systemMessage = params.messages.find((m) => m.role === "system");
|
|
619
|
-
const
|
|
673
|
+
const conversationMessages = params.messages.filter((m) => m.role === "user" || m.role === "assistant");
|
|
674
|
+
const toolName = params.response_format?.type === "json_schema" ? params.response_format.json_schema.name : void 0;
|
|
620
675
|
let jsonInstruction;
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
676
|
+
let toolConfig;
|
|
677
|
+
if (params.response_format?.type === "json_schema" && toolName) {
|
|
678
|
+
const { schema, description } = params.response_format.json_schema;
|
|
679
|
+
toolConfig = {
|
|
680
|
+
tools: [{ toolSpec: {
|
|
681
|
+
name: toolName,
|
|
682
|
+
description,
|
|
683
|
+
inputSchema: { json: schema }
|
|
684
|
+
} }],
|
|
685
|
+
toolChoice: { tool: { name: toolName } }
|
|
686
|
+
};
|
|
624
687
|
} else if (params.response_format?.type === "json_object") jsonInstruction = "Respond with valid JSON only, no prose or markdown fences.";
|
|
625
688
|
const systemParts = [systemMessage?.content, jsonInstruction].filter((s) => Boolean(s));
|
|
626
689
|
const response = await bedrockClient.converse({
|
|
627
690
|
modelId: params.model,
|
|
628
|
-
messages:
|
|
629
|
-
role:
|
|
691
|
+
messages: conversationMessages.map((m) => ({
|
|
692
|
+
role: m.role,
|
|
630
693
|
content: [{ text: m.content }]
|
|
631
694
|
})),
|
|
632
695
|
system: systemParts.length ? systemParts.map((text$1) => ({ text: text$1 })) : void 0,
|
|
633
696
|
inferenceConfig: {
|
|
634
697
|
temperature: params.temperature,
|
|
635
698
|
maxTokens: params.max_tokens
|
|
636
|
-
}
|
|
699
|
+
},
|
|
700
|
+
...toolConfig ? { toolConfig } : {}
|
|
637
701
|
}, options);
|
|
638
|
-
|
|
702
|
+
let text;
|
|
703
|
+
if (toolName) {
|
|
704
|
+
const toolUseBlock = response.output?.message?.content?.find((block) => block.toolUse?.name === toolName);
|
|
705
|
+
text = toolUseBlock?.toolUse ? JSON.stringify(toolUseBlock.toolUse.input) : "";
|
|
706
|
+
} else text = response.output?.message?.content?.map((c) => c.text ?? "").join("") ?? "";
|
|
639
707
|
return {
|
|
640
708
|
choices: [{ message: { content: text } }],
|
|
641
709
|
usage: {
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["message: string","type: LLMErrorType","status?: number","issues?: unknown","err: unknown","key: string","value: T","ttl: number","options: CircuitBreakerOptions","content: string","err: unknown","fn: (signal: AbortSignal) => Promise<T>","timeoutMs: number","externalSignal?: AbortSignal","baseDelayMs: number","attempt: number","delay: number","signal?: AbortSignal","debugEnabled: boolean","message: string","meta?: Record<string, unknown>","options: VernLLMOptions","params: CallParams<T>","fn: () => Promise<T>","requestId: string","signal?: AbortSignal","lastError: unknown","error: unknown","jsonSchema: CallParams<unknown>['jsonSchema']","useJson: boolean","response: Awaited<ReturnType<LLMClient['chat']['completions']['create']>>","model: string","content: string","schema?: CallParams<T>['schema']","parsed: unknown","attempt: number","params: CachedCallParams<T>","params: Omit<CachedCallParams<T>, 'fn'> & { call: CallParams<T> }","anthropicClient: AnthropicClient","jsonInstruction: string | undefined","geminiClient: GeminiClient","generationConfig: NonNullable<\n Parameters<GeminiClient['generateContent']>[0]['generationConfig']\n >","bedrockClient: BedrockConverseClient","jsonInstruction: string | undefined","text","config: FetchAdapterConfig","client: unknown"],"sources":["../src/types.ts","../src/circuitBreaker.ts","../src/internal/vernLLM.utilts.ts","../src/logger.ts","../src/vernLLM.ts","../src/adapters/anthropic.ts","../src/adapters/gemini.ts","../src/adapters/bedrock.ts","../src/adapters/fetch.ts","../src/adapters/openaiCompatible.ts"],"sourcesContent":["export type LLMErrorType =\n | 'timeout'\n | 'api'\n | 'parse'\n | 'validation'\n | 'circuit_open'\n | 'unknown'\n | 'aborted';\n\nexport class LLMError extends Error {\n constructor(\n message: string,\n public type: LLMErrorType,\n public status?: number,\n public issues?: unknown,\n ) {\n super(message);\n this.name = 'LLMError';\n }\n}\n\nexport function isLLMError(err: unknown): err is LLMError {\n return err instanceof LLMError;\n}\n\nexport interface CacheAdapter<T = unknown> {\n get(key: string): Promise<T | null>;\n set(key: string, value: T, ttl: number): Promise<void>;\n}\n\n/**\n * Trivial default so the package works out of the box with no external deps\n * Not shared across processes, swap in Redis/Upstash/etc for production\n */\nexport class InMemoryCacheAdapter<T = unknown> implements CacheAdapter<T> {\n private store = new Map<string, { value: T; expiresAt: number }>();\n\n async get(key: string): Promise<T | null> {\n const entry = this.store.get(key);\n if (!entry) return null;\n if (Date.now() > entry.expiresAt) {\n this.store.delete(key);\n return null;\n }\n return entry.value;\n }\n\n async set(key: string, value: T, ttl: number): Promise<void> {\n this.store.set(key, { value, expiresAt: Date.now() + ttl * 1000 });\n }\n}\n\n/**\n * Minimal shape compatible with the OpenAI SDKs chat.completions.create,\n * so consumers can pass an OpenAI client directly\n * `response_format.json_schema` and `reasoning_effort` are optional on the wire\n * providers that don't support them will just ignore fields they don't recognize,\n * but not every SDKs TS types accept them, hence this being a structural type\n * rather than importing the SDKs own params type\n */\nexport interface LLMClient {\n chat: {\n completions: {\n create(\n params: {\n model: string;\n temperature: number;\n max_tokens: number;\n response_format?:\n | { type: 'json_object' }\n | {\n type: 'json_schema';\n json_schema: {\n name: string;\n schema: Record<string, unknown>;\n strict?: boolean;\n description?: string;\n };\n };\n /** OpenAI reasoning-model param (o-series, gpt-5), ignored by providers that don't support it */\n reasoning_effort?: 'minimal' | 'low' | 'medium' | 'high';\n messages: Array<{ role: 'system' | 'user'; content: string }>;\n },\n options: { signal: AbortSignal },\n ): Promise<{\n choices?: Array<{ message?: { content?: string | null } }>;\n usage?: {\n prompt_tokens?: number;\n completion_tokens?: number;\n total_tokens?: number;\n };\n }>;\n };\n };\n}\n\nexport type ReserveUsage = () => Promise<void>;\nexport type RefundUsage = () => Promise<void>;\n\nexport interface TokenUsage {\n promptTokens: number;\n completionTokens: number;\n totalTokens: number;\n requestId: string;\n model: string;\n}\n\nexport type OnUsage = (usage: TokenUsage) => void;\n\n/**\n * Minimal structural type for a Zod-like schema, so this package doesnt need\n * a hard dependency on a specific Zod major version. Any object exposing\n * `safeParse` (Zod v3/v4, and most Zod-compatible validators) should satisfy this\n */\nexport interface SchemaLike<T> {\n safeParse(data: unknown): { success: true; data: T } | { success: false; error: unknown };\n}\n\nexport interface VernLLMOptions {\n client: LLMClient;\n model: string;\n /** Max retries after the first attempt. Default 1 (2 attempts total) */\n maxRetries?: number;\n /** Per-attempt timeout in ms. Default 25000 */\n timeoutMs?: number;\n /** Base delay for exponential backoff in ms. Default 500 */\n baseDelayMs?: number;\n /** Default max_tokens for calls that don't override it. Default 1000 */\n defaultMaxTokens?: number;\n /** Enables debug logging of raw model output. Default: NODE_ENV !== 'production' */\n debug?: boolean;\n /** Cache adapter for cachedCall. Defaults to an in-memory adapter */\n cache?: CacheAdapter;\n /** HTTP status codes that should fail fast without retrying. Default [400, 401, 403] */\n nonRetryableStatus?: number[];\n /** Custom JSON parser. Must return undefined/null on failure. Default: JSON.parse wrapped in try/catch */\n parseJson?: (content: string) => unknown;\n /** Called after every successful call with token usage, if the provider reports it */\n onUsage?: OnUsage;\n /** Injectable logger. Defaults to a console-based logger gated by `debug` */\n logger?: import('./logger.js').Logger;\n /**\n * Enables a circuit breaker that short-circuits calls after repeated\n * consecutive failures, instead of continuing to hammer a down provider\n * Pass `true` for defaults, or an options object to tune threshold/cooldown\n */\n circuitBreaker?: boolean | import('./circuitBreaker.js').CircuitBreakerOptions;\n}\n\n/**\n * A provider-native JSON Schema for structured outputs (OpenAI/Groq\n * `response_format: { type: 'json_schema' }`) This is the wire-format\n * schema the model is constrained to generate against, distinct from\n * `schema`, which is a client-side Zod validator run on the parsed result\n * You can use one, both, or neither; using both gets you provider-level\n * constraint plus client-side type inference/validation as a safety net\n */\nexport interface JsonSchemaSpec {\n name: string;\n schema: Record<string, unknown>;\n /** Enforces the schema strictly (OpenAI-specific), default true when supported */\n strict?: boolean;\n description?: string;\n}\n\nexport interface CallParams<T = unknown> {\n systemPrompt: string;\n userContent: string;\n temperature?: number;\n jsonMode?: boolean;\n maxTokens?: number;\n requestId?: string;\n signal?: AbortSignal;\n /** Overrides the model set on the VernLLM instance for this call only */\n model?: string;\n /**\n * OpenAI-style reasoning effort for reasoning models (o-series, gpt-5, etc).\n * Passed through as-is, providers/models that don't support it ignore it\n */\n reasoningEffort?: 'minimal' | 'low' | 'medium' | 'high';\n /**\n * Provider-native JSON Schema structured-output mode. When set, this is sent\n * as `response_format: { type: 'json_schema', json_schema: ... }` instead of\n * the looser `json_object` mode, constraining the models output shape at\n * generation time (not just validating it after the fact). Implies jsonMode: true.\n */\n jsonSchema?: JsonSchemaSpec;\n /**\n * Optional Zod (or Zod-compatible) schema. When provided, the parsed JSON\n * is validated against it; on failure an LLMError('validation') is thrown\n * with `.issues` set to the schema's error object. Implies jsonMode: true.\n * Can be combined with `jsonSchema` for provider-level constraint + client-side typing.\n */\n schema?: SchemaLike<T>;\n}\n\nexport interface CachedCallParams<T> {\n cacheKey: string;\n ttl: number;\n fn: () => Promise<T>;\n reserveUsage?: ReserveUsage;\n refundUsage?: RefundUsage;\n}\n","import { LLMError } from './types.js';\n\nexport interface CircuitBreakerOptions {\n /** Consecutive failures before the circuit opens, default 5 */\n threshold?: number;\n /** How long the circuit stays open before allowing a trial request, in ms. Default 30000 */\n cooldownMs?: number;\n}\n\ntype CircuitState = 'closed' | 'open' | 'half-open';\n\n/**\n * Per retry VernLLM-instance circuit breaker. Tracks consecutive failures across\n * calls. Once the threshold is hit, short-circuits new calls with an\n * LLMError('circuit_open') instead of hitting the provider, until the\n * cooldown elapses and a single trial call is allowed through\n */\nexport class CircuitBreaker {\n private state: CircuitState = 'closed';\n private consecutiveFailures = 0;\n private openedAt = 0;\n private threshold: number;\n private cooldownMs: number;\n\n constructor(options: CircuitBreakerOptions = {}) {\n this.threshold = options.threshold ?? 5;\n this.cooldownMs = options.cooldownMs ?? 30_000;\n }\n\n /** Throws if the circuit is open and the cooldown hasnt elapsed */\n assertClosed(): void {\n if (this.state !== 'open') return;\n\n const elapsed = Date.now() - this.openedAt;\n if (elapsed >= this.cooldownMs) {\n this.state = 'half-open';\n return;\n }\n\n throw new LLMError(\n `Circuit open — provider has failed ${this.consecutiveFailures} times in a row. Retry in ${Math.ceil((this.cooldownMs - elapsed) / 1000)}s.`,\n 'circuit_open',\n );\n }\n\n recordSuccess(): void {\n this.consecutiveFailures = 0;\n this.state = 'closed';\n }\n\n recordFailure(): void {\n this.consecutiveFailures += 1;\n\n if (this.state === 'half-open') {\n // Trial call failed: reopen and reset the cooldown window.\n this.state = 'open';\n this.openedAt = Date.now();\n return;\n }\n\n if (this.consecutiveFailures >= this.threshold) {\n this.state = 'open';\n this.openedAt = Date.now();\n }\n }\n\n getState(): CircuitState {\n return this.state;\n }\n}\n","import { LLMError } from '../types.js';\n\nexport function defaultParseJson(content: string): unknown {\n try {\n return JSON.parse(content);\n } catch {\n return undefined;\n }\n}\n\n/**\n * Looks inside an unknown error value and pulls out an http status code\n * if one is present. Checks the status field first then the status code\n * field since different client libraries use different names for this.\n * Returns undefined when the error is not an object or carries no status\n */\nexport function extractStatus(err: unknown): number | undefined {\n if (!err || typeof err !== 'object') return undefined;\n\n const error = err as {\n status?: unknown;\n statusCode?: unknown;\n };\n\n if (typeof error.status === 'number') return error.status;\n if (typeof error.statusCode === 'number') return error.statusCode;\n\n return undefined;\n}\n\n/**\n * Runs an async function and cancels it if it takes longer than the given\n * timeout. Creates an internal abort controller that fires after the\n * timeout elapses, and combines it with any external signal the caller\n * passed in so either one can cancel the underlying call. If the internal\n * timeout triggers and the underlying operation aborts, the error is\n * converted into an LLMError with type \"timeout\". External cancellations\n * continue to propagate as aborted errors. The internal timer is always\n * cleared afterward, whether the function succeeds, fails, or is aborted,\n * so nothing is left running in the background.\n */\nexport async function withTimeout<T>(\n fn: (signal: AbortSignal) => Promise<T>,\n timeoutMs: number,\n externalSignal?: AbortSignal,\n): Promise<T> {\n const controller = new AbortController();\n\n const timer = setTimeout(() => {\n controller.abort();\n }, timeoutMs);\n\n const signal = externalSignal\n ? AbortSignal.any([externalSignal, controller.signal])\n : controller.signal;\n\n try {\n return await fn(signal);\n } catch (err) {\n if (\n controller.signal.aborted &&\n !externalSignal?.aborted &&\n err instanceof DOMException &&\n err.name === 'AbortError'\n ) {\n throw new LLMError('Request timed out', 'timeout');\n }\n\n throw err;\n } finally {\n clearTimeout(timer);\n }\n}\n\n/**\n * Exponential backoff with jitter, capped at maxDelayMs.\n * Jitter avoids thundering-herd retries when many callers back off in lockstep,\n * the cap prevents unbounded delays when maxRetries is high\n */\nexport function getBackoffDelay(baseDelayMs: number, attempt: number, maxDelayMs = 10_000): number {\n const exp = Math.min(baseDelayMs * 2 ** attempt, maxDelayMs);\n return exp / 2 + Math.random() * (exp / 2);\n}\n\n/**\n * Pauses execution for the given delay before a retry attempt. If an\n * abort signal is provided and it fires while waiting, the pending\n * timer is cancelled immediately and the wait rejects right away with\n * an aborted error instead of continuing to sit idle until the delay\n * would have finished on its own\n */\nexport async function waitForRetry(delay: number, signal?: AbortSignal): Promise<void> {\n await new Promise<void>((resolve, reject) => {\n const onAbort = () => {\n clearTimeout(timer);\n reject(new LLMError('Operation aborted', 'aborted'));\n };\n\n const timer = setTimeout(() => {\n signal?.removeEventListener('abort', onAbort);\n resolve();\n }, delay);\n\n signal?.addEventListener('abort', onAbort, { once: true });\n });\n}\n","export interface Logger {\n debug(message: string): void;\n warn(message: string): void;\n error(message: string, meta?: Record<string, unknown>): void;\n}\n\n/**\n * Default logger. `debug` is gated by the `debug` option on VernLLM\n * warn/error always fire since they indicate real problems (retries, cache failures)\n */\nexport class ConsoleLogger implements Logger {\n constructor(private debugEnabled: boolean) {}\n\n debug(message: string): void {\n if (this.debugEnabled) console.debug(message);\n }\n\n warn(message: string): void {\n console.warn(message);\n }\n\n error(message: string, meta?: Record<string, unknown>): void {\n console.error(message, meta ?? '');\n }\n}\n","import { randomUUID } from 'crypto';\n\nimport { CircuitBreaker } from './circuitBreaker.js';\nimport {\n defaultParseJson,\n extractStatus,\n withTimeout,\n getBackoffDelay,\n waitForRetry,\n} from './internal/vernLLM.utilts.js';\nimport { ConsoleLogger, type Logger } from './logger.js';\nimport {\n LLMError,\n InMemoryCacheAdapter,\n type VernLLMOptions,\n type CallParams,\n type CachedCallParams,\n type CacheAdapter,\n type LLMClient,\n} from './types.js';\n\n/**\n * A resilient wrapper around an LLM chat completions client, this is VernLLM!\n *\n * Adds retry with exponential backoff and jitter, per-attempt timeouts,\n * an optional circuit breaker, JSON parsing with optional schema\n * validation, usage tracking, and an optional response cache, all\n * configurable, all opt-in beyond sensible defaults\n */\nexport class VernLLM {\n private readonly client: LLMClient;\n private readonly model: string;\n\n private readonly maxRetries: number;\n private readonly timeoutMs: number;\n private readonly baseDelayMs: number;\n private readonly defaultMaxTokens: number;\n\n private readonly cache: CacheAdapter<unknown>;\n private readonly nonRetryableStatus: number[];\n\n private readonly parseJson: (content: string) => unknown;\n private readonly onUsage?: VernLLMOptions['onUsage'];\n\n private readonly logger: Logger;\n private readonly breaker?: CircuitBreaker;\n\n /**\n * @param options: Client, model, and all tunables (retries, timeout,\n * backoff, cache, circuit breaker, logger, etc). See VernLLMOptions in `types.ts`\n * for individual defaults\n */\n constructor(options: VernLLMOptions) {\n this.client = options.client;\n this.model = options.model;\n\n const retryConfig = this.resolveRetryConfig(options);\n this.maxRetries = retryConfig.maxRetries;\n this.timeoutMs = retryConfig.timeoutMs;\n this.baseDelayMs = retryConfig.baseDelayMs;\n this.defaultMaxTokens = retryConfig.defaultMaxTokens;\n\n this.cache = options.cache ?? new InMemoryCacheAdapter();\n this.nonRetryableStatus = options.nonRetryableStatus ?? [400, 401, 403];\n\n this.parseJson = options.parseJson ?? defaultParseJson;\n this.onUsage = options.onUsage;\n\n this.logger = this.resolveLogger(options);\n this.breaker = this.resolveCircuitBreaker(options);\n }\n\n /**\n * Resolves retry/timeout/token defaults from the given options,\n * falling back to the librarys built-in defaults for anything unset\n */\n private resolveRetryConfig(options: VernLLMOptions) {\n return {\n maxRetries: options.maxRetries ?? 1,\n timeoutMs: options.timeoutMs ?? 25_000,\n baseDelayMs: options.baseDelayMs ?? 500,\n defaultMaxTokens: options.defaultMaxTokens ?? 1000,\n };\n }\n\n /**\n * Returns the caller supplied logger, or a console-based logger whose\n * debug output is gated by the `debug` option (defaulting to on\n * outside production)\n */\n private resolveLogger(options: VernLLMOptions): Logger {\n return (\n options.logger ?? new ConsoleLogger(options.debug ?? process.env.NODE_ENV !== 'production')\n );\n }\n\n /**\n * Builds a circuit breaker if `circuitBreaker` is truthy on the\n * options. Passing `true` uses default thresholds, passing an options\n * object tunes them. Returns undefined when the breaker is disabled\n */\n private resolveCircuitBreaker(options: VernLLMOptions): CircuitBreaker | undefined {\n if (!options.circuitBreaker) return undefined;\n\n return new CircuitBreaker(options.circuitBreaker === true ? undefined : options.circuitBreaker);\n }\n\n /**\n * Makes a single logical LLM call, transparently retrying on failure\n * according to the configured retry policy\n *\n * Fails fast if the circuit breaker is open or the signal is already\n * aborted, before any request is dispatched. On exhausting all\n * retries, records a circuit breaker failure and rejects with a\n * normalized LLMError\n *\n * @param params : System/user content plus per call overrides\n * (model, temperature, jsonMode, schema, signal, etc)\n * @returns The parsed and optionally schema-validated response, or\n * the raw string content when jsonMode is disabled\n */\n async call<T = unknown>(params: CallParams<T>): Promise<T> {\n this.breaker?.assertClosed();\n\n if (params.signal?.aborted) {\n throw new LLMError('LLM request aborted', 'aborted');\n }\n\n const requestId = params.requestId ?? randomUUID();\n\n try {\n const result = await this.retryWithBackoff(\n () => this.executeCall(params, requestId),\n requestId,\n params.signal,\n );\n\n return result;\n } catch (error) {\n this.breaker?.recordFailure();\n throw this.normalizeError(error, params.signal);\n }\n }\n\n /**\n * Runs `fn`, retrying with backoff according to `shouldRetry` policy\n * Purely mechanical: knows nothing about LLM specifics beyond the retry\n * predicate, so its testable independent of request/response shaping\n */\n private async retryWithBackoff<T>(\n fn: () => Promise<T>,\n requestId: string,\n signal?: AbortSignal,\n ): Promise<T> {\n let lastError: unknown;\n\n for (let attempt = 0; attempt <= this.maxRetries; attempt++) {\n try {\n if (attempt > 0) {\n await this.recoverDelay(requestId, attempt, signal);\n }\n\n return await fn();\n } catch (error) {\n lastError = error;\n\n if (!this.shouldRetry(error, signal)) {\n break;\n }\n }\n }\n\n throw lastError;\n }\n\n /**\n * Converts any thrown value into a well-typed LLMError for the public\n * API surface. Preserves an existing LLMError as is, reports aborted\n * signals as such, classifies errors carrying an http status as\n * type api, and otherwise falls back to a generic unknown error.\n */\n private normalizeError(error: unknown, signal?: AbortSignal): LLMError {\n if (signal?.aborted) {\n return new LLMError('LLM request aborted', 'aborted');\n }\n\n if (error instanceof LLMError) {\n return error;\n }\n\n const status = extractStatus(error);\n\n if (status !== undefined) {\n return new LLMError('LLM request failed', 'api', status);\n }\n\n return new LLMError('LLM request failed', 'unknown');\n }\n\n /**\n * Performs a single attempt: builds the request, dispatches it with a\n * timeout, and shapes the response. Throws on an empty response so\n * the retry loop treats it like any other transient failure. Records\n * usage and a circuit breaker success before returning\n */\n private async executeCall<T>(params: CallParams<T>, requestId: string): Promise<T> {\n const { useJson, model, request } = this.buildRequestPayload(params);\n\n const response = await withTimeout(\n (attemptSignal) => this.client.chat.completions.create(request, { signal: attemptSignal }),\n this.timeoutMs,\n params.signal,\n );\n\n const content = response.choices?.[0]?.message?.content?.trim();\n\n if (!content) {\n throw new LLMError('Empty LLM response', 'api');\n }\n\n this.logger.debug(`[vern:${requestId}] output:\\n${content.slice(0, 800)}`);\n\n this.recordUsage(response, requestId, model);\n\n this.breaker?.recordSuccess();\n\n if (!useJson) {\n return content as T;\n }\n\n return this.parseAndValidate(content, params.schema);\n }\n\n /**\n * Applies per call defaults and shapes the params into the request\n * object expected by the underlying client, including the resolved\n * response format. Also returns whether JSON parsing should be\n * applied to the response and which model was ultimately used\n */\n private buildRequestPayload<T>(params: CallParams<T>) {\n const {\n systemPrompt,\n userContent,\n temperature = 0.2,\n jsonMode = true,\n maxTokens = this.defaultMaxTokens,\n model = this.model,\n reasoningEffort,\n jsonSchema,\n } = params;\n\n const useJson = jsonMode || Boolean(jsonSchema);\n const responseFormat = this.buildResponseFormat(jsonSchema, useJson);\n\n const request = {\n model,\n temperature,\n max_tokens: maxTokens,\n ...(responseFormat ? { response_format: responseFormat } : {}),\n ...(reasoningEffort ? { reasoning_effort: reasoningEffort } : {}),\n messages: [\n { role: 'system' as const, content: systemPrompt },\n { role: 'user' as const, content: userContent },\n ],\n };\n\n return { useJson, model, request };\n }\n\n /**\n * Chooses the response format to send to the provider. A provider\n * native json schema takes priority when supplied, constraining\n * generation directly, otherwise falls back to the looser json\n * object mode when JSON output is requested, or no format at all\n * for plain text responses\n */\n private buildResponseFormat(jsonSchema: CallParams<unknown>['jsonSchema'], useJson: boolean) {\n if (jsonSchema) {\n return {\n type: 'json_schema' as const,\n json_schema: {\n name: jsonSchema.name,\n schema: jsonSchema.schema,\n strict: jsonSchema.strict ?? true,\n description: jsonSchema.description,\n },\n };\n }\n\n return useJson ? { type: 'json_object' as const } : undefined;\n }\n\n /**\n * Reports token usage to the caller supplied onUsage callback, when\n * both a callback was configured and the provider actually returned\n * usage data on this response. A no op otherwise.\n */\n private recordUsage(\n response: Awaited<ReturnType<LLMClient['chat']['completions']['create']>>,\n requestId: string,\n model: string,\n ): void {\n if (!response.usage || !this.onUsage) return;\n\n this.onUsage({\n promptTokens: response.usage.prompt_tokens ?? 0,\n completionTokens: response.usage.completion_tokens ?? 0,\n totalTokens: response.usage.total_tokens ?? 0,\n requestId,\n model,\n });\n }\n\n /**\n * Parses the raw response content as JSON and, when a schema is\n * supplied, validates the parsed value against it. Throws a parse\n * type LLMError on malformed JSON and a validation type LLMError,\n * carrying the schemas issues, on a failed validation\n */\n private parseAndValidate<T>(content: string, schema?: CallParams<T>['schema']): T {\n let parsed: unknown;\n\n try {\n parsed = this.parseJson(content);\n } catch {\n throw new LLMError('Invalid JSON response', 'parse');\n }\n\n if (parsed === null || parsed === undefined) {\n throw new LLMError('Invalid JSON response', 'parse');\n }\n\n if (!schema) {\n return parsed as T;\n }\n\n const result = schema.safeParse(parsed);\n\n if (!result.success) {\n throw new LLMError('Schema validation failed', 'validation', undefined, result.error);\n }\n\n return result.data;\n }\n\n /**\n * Waits out the backoff delay for a given retry attempt, logging the\n * attempt for observability before the wait begins. Rejects early if\n * the signal aborts during the wait\n */\n private async recoverDelay(requestId: string, attempt: number, signal?: AbortSignal) {\n const delay = getBackoffDelay(this.baseDelayMs, attempt);\n\n this.logger.warn(\n `[vern:${requestId}] recovery attempt ${attempt}/${this.maxRetries}, waiting ${delay}ms`,\n );\n\n await waitForRetry(delay, signal);\n }\n\n /**\n * Decides whether a failed attempt is worth retrying. Never retries\n * once the signal has aborted, never retries a parse or validation\n * failure since those stem from the response content rather than a\n * transient fault, and never retries a status code the caller has\n * marked as non retryable. Retries everything else\n */\n private shouldRetry(error: unknown, signal?: AbortSignal): boolean {\n if (signal?.aborted) {\n return false;\n }\n\n if (error instanceof LLMError && (error.type === 'parse' || error.type === 'validation')) {\n return false;\n }\n\n const status = extractStatus(error);\n\n if (status !== undefined && this.nonRetryableStatus.includes(status)) {\n return false;\n }\n\n return true;\n }\n\n /**\n * Thin cache wrapper around caller supplied logic. `params.fn` is expected\n * to be a call that itself invokes `this.call(...)` (see `cachedLLMCall`\n * below for a convenience wrapper that wires this up automatically),\n * `cachedCall` does not itself apply retry/timeout policy.\n */\n async cachedCall<T>(params: CachedCallParams<T>): Promise<T> {\n const cached = await this.cache.get(params.cacheKey);\n\n if (cached !== null) {\n return cached as T;\n }\n\n try {\n await params.reserveUsage?.();\n\n const result = await params.fn();\n\n try {\n await this.cache.set(params.cacheKey, result, params.ttl);\n } catch (error) {\n this.logger.error('[VernLLM] cache write failed', {\n message: error instanceof Error ? error.message : 'unknown',\n });\n }\n\n return result;\n } catch (error) {\n try {\n await params.refundUsage?.();\n } catch (refundError) {\n this.logger.error('[VernLLM] refundUsage failed', {\n message: refundError instanceof Error ? refundError.message : 'unknown',\n });\n }\n\n throw error;\n }\n }\n\n /**\n * Convenience wrapper composing `call` + `cachedCall`, so cached LLM calls\n * automatically get retry/timeout/circuit-breaker behavior without callers\n * having to remember to wire `fn: () => this.call(...)` themselves\n */\n async cachedLLMCall<T>(\n params: Omit<CachedCallParams<T>, 'fn'> & { call: CallParams<T> },\n ): Promise<T> {\n const { call: callParams, ...cacheParams } = params;\n\n return this.cachedCall({\n ...cacheParams,\n fn: () => this.call(callParams),\n });\n }\n\n /**\n * Returns the current circuit breaker state, or undefined when no\n * circuit breaker was configured on this instance\n */\n getCircuitState() {\n return this.breaker?.getState();\n }\n}\n","import type { LLMClient } from '../types.js';\n\n/** Minimal structural type for the Anthropic SDKs `messages.create` */\nexport interface AnthropicClient {\n messages: {\n create(\n params: {\n model: string;\n max_tokens: number;\n temperature?: number;\n system?: string;\n messages: Array<{ role: 'user' | 'assistant'; content: string }>;\n },\n options: { signal: AbortSignal },\n ): Promise<{\n content: Array<{ type: string; text?: string }>;\n usage?: { input_tokens?: number; output_tokens?: number };\n }>;\n };\n}\n\n/**\n * Wraps an Anthropic SDK client so it satisfies the same `LLMClient`\n * interface VernLLM uses for OpenAI/Groq. Anthropics Messages API has no\n * `response_format: json_object` equivalent, so when the caller requests\n * JSON mode, this adapter appends an instruction to the system prompt\n * asking the model to respond with JSON only\n */\nexport function fromAnthropic(anthropicClient: AnthropicClient): LLMClient {\n return {\n chat: {\n completions: {\n async create(params, options) {\n const systemMessage = params.messages.find((m) => m.role === 'system');\n const userMessages = params.messages.filter((m) => m.role === 'user');\n\n // Anthropics Messages API has no `response_format: json_object` or\n // `json_schema` equivalent, so both are emulated via system prompt\n // instructions. For json_schema, the schema itself is embedded so the\n // model has something concrete to conform to (not provider-enforced,\n // unlike OpenAIs native structured outputs)\n let jsonInstruction: string | undefined;\n if (params.response_format?.type === 'json_schema') {\n const { name, schema } = params.response_format.json_schema;\n jsonInstruction = `Respond with valid JSON only, no prose or markdown fences. The JSON must conform to this schema (name: \"${name}\"):\\n${JSON.stringify(schema)}`;\n } else if (params.response_format?.type === 'json_object') {\n jsonInstruction = 'Respond with valid JSON only, no prose or markdown fences.';\n }\n\n // `reasoning_effort` (OpenAI o-series/gpt-5 style) has no direct Anthropic\n // equivalent, Claudes extended thinking uses a token budget, not a tier\n // string, so its intentionally dropped here rather than guessed at.\n\n const system = [systemMessage?.content, jsonInstruction].filter(Boolean).join('\\n\\n');\n\n const response = await anthropicClient.messages.create(\n {\n model: params.model,\n max_tokens: params.max_tokens,\n temperature: params.temperature,\n system: system || undefined,\n messages: userMessages.map((m) => ({ role: 'user' as const, content: m.content })),\n },\n options,\n );\n\n const text = response.content.find((block) => block.type === 'text')?.text ?? '';\n\n return {\n choices: [{ message: { content: text } }],\n usage: {\n prompt_tokens: response.usage?.input_tokens,\n completion_tokens: response.usage?.output_tokens,\n total_tokens:\n (response.usage?.input_tokens ?? 0) + (response.usage?.output_tokens ?? 0),\n },\n };\n },\n },\n },\n };\n}\n","import type { LLMClient } from '../types.js';\n\n/**\n * Minimal structural type for Geminis `generateContent`, matching both the\n * legacy `@google/generative-ai` SDKs `model.generateContent(...)` and the\n * newer `@google/genai` SDKs `ai.models.generateContent({ model, ... })`\n * closely enough to adapt either — pass whichever `.generateContent` you have.\n */\nexport interface GeminiClient {\n generateContent(\n params: {\n model?: string;\n contents: Array<{ role: 'user'; parts: Array<{ text: string }> }>;\n systemInstruction?: { parts: Array<{ text: string }> };\n generationConfig?: {\n temperature?: number;\n maxOutputTokens?: number;\n responseMimeType?: string;\n responseSchema?: Record<string, unknown>;\n };\n },\n options: { signal: AbortSignal },\n ): Promise<{\n candidates?: Array<{ content?: { parts?: Array<{ text?: string }> } }>;\n usageMetadata?: {\n promptTokenCount?: number;\n candidatesTokenCount?: number;\n totalTokenCount?: number;\n };\n }>;\n}\n\n/**\n * Wraps a Gemini client so it satisfies the `LLMClient` interface VernLLM\n * uses for OpenAI/Groq. Geminis shape differs on nearly every axis: a\n * `contents` array instead of `messages`, a separate `systemInstruction`\n * field instead of a `system` role message, `generationConfig` instead of\n * top-level `temperature`/`max_tokens`, and native JSON Schema support via\n * `responseMimeType: 'application/json'` + `responseSchema` (so `jsonSchema`\n * is provider-enforced here, unlike the Anthropic adapters prompt-embedding\n * fallback). `reasoning_effort` has no equivalent. Geminis thinking models\n * use a token budget, not an effort tier, so its dropped, same as Anthropic.\n */\nexport function fromGemini(geminiClient: GeminiClient): LLMClient {\n return {\n chat: {\n completions: {\n async create(params, options) {\n const systemMessage = params.messages.find((m) => m.role === 'system');\n const userMessages = params.messages.filter((m) => m.role === 'user');\n\n const wantsJson = Boolean(params.response_format);\n const generationConfig: NonNullable<\n Parameters<GeminiClient['generateContent']>[0]['generationConfig']\n > = {\n temperature: params.temperature,\n maxOutputTokens: params.max_tokens,\n };\n\n if (wantsJson) {\n generationConfig.responseMimeType = 'application/json';\n }\n if (params.response_format?.type === 'json_schema') {\n generationConfig.responseSchema = params.response_format.json_schema.schema;\n }\n\n const response = await geminiClient.generateContent(\n {\n model: params.model,\n contents: userMessages.map((m) => ({\n role: 'user' as const,\n parts: [{ text: m.content }],\n })),\n systemInstruction: systemMessage\n ? { parts: [{ text: systemMessage.content }] }\n : undefined,\n generationConfig,\n },\n options,\n );\n\n const text =\n response.candidates?.[0]?.content?.parts?.map((p) => p.text ?? '').join('') ?? '';\n\n return {\n choices: [{ message: { content: text } }],\n usage: {\n prompt_tokens: response.usageMetadata?.promptTokenCount,\n completion_tokens: response.usageMetadata?.candidatesTokenCount,\n total_tokens: response.usageMetadata?.totalTokenCount,\n },\n };\n },\n },\n },\n };\n}\n","import type { LLMClient } from '../types.js';\n\n/**\n * Minimal structural type matching AWS Bedrocks Converse API. This is\n * intentionally NOT `BedrockRuntimeClient` itself, the AWS SDK v3 client\n * exposes `.send(command)`, not a direct `.converse()` method, and pulling\n * in `@aws-sdk/client-bedrock-runtime` as a dependency just for its types\n * isn't worth it for a structural adapter. Wrap your client, e.g:\n *\n * ```ts\n * import { BedrockRuntimeClient, ConverseCommand } from '@aws-sdk/client-bedrock-runtime';\n * const client = new BedrockRuntimeClient({ region: 'us-east-1' });\n * const converseClient = {\n * converse: (params, options) =>\n * client.send(new ConverseCommand(params), { abortSignal: options.signal }),\n * };\n * ```\n */\nexport interface BedrockConverseClient {\n converse(\n params: {\n modelId: string;\n messages: Array<{ role: 'user'; content: Array<{ text: string }> }>;\n system?: Array<{ text: string }>;\n inferenceConfig?: { temperature?: number; maxTokens?: number };\n },\n options: { signal: AbortSignal },\n ): Promise<{\n output?: { message?: { content?: Array<{ text?: string }> } };\n usage?: { inputTokens?: number; outputTokens?: number; totalTokens?: number };\n }>;\n}\n\n/**\n * Wraps a Bedrock Converse-API client so it satisfies the `LLMClient`\n * interface VernLLM uses for OpenAI/Groq. The Converse API is unified\n * across Bedrocks model families (Anthropic, Titan, Llama, Mistral, etc.),\n * so unlike raw per-model Bedrock invocation, this one adapter works\n * regardless of which underlying model `modelId` points at, as long as\n * that model supports Converse (most current-generation ones do)\n *\n * Theres no uniform native JSON Schema enforcement across families here\n * (some support it via forced tool-use, which varies per model), so\n * `jsonSchema`/`jsonMode` are emulated via a system-prompt instruction, same\n * approach as the Anthropic adapter. `reasoning_effort` has no Converse\n * equivalent and is dropped\n */\nexport function fromBedrock(bedrockClient: BedrockConverseClient): LLMClient {\n return {\n chat: {\n completions: {\n async create(params, options) {\n const systemMessage = params.messages.find((m) => m.role === 'system');\n const userMessages = params.messages.filter((m) => m.role === 'user');\n\n let jsonInstruction: string | undefined;\n if (params.response_format?.type === 'json_schema') {\n const { name, schema } = params.response_format.json_schema;\n jsonInstruction = `Respond with valid JSON only, no prose or markdown fences. The JSON must conform to this schema (name: \"${name}\"):\\n${JSON.stringify(schema)}`;\n } else if (params.response_format?.type === 'json_object') {\n jsonInstruction = 'Respond with valid JSON only, no prose or markdown fences.';\n }\n\n const systemParts = [systemMessage?.content, jsonInstruction].filter((s): s is string =>\n Boolean(s),\n );\n\n const response = await bedrockClient.converse(\n {\n modelId: params.model,\n messages: userMessages.map((m) => ({\n role: 'user' as const,\n content: [{ text: m.content }],\n })),\n system: systemParts.length ? systemParts.map((text) => ({ text })) : undefined,\n inferenceConfig: {\n temperature: params.temperature,\n maxTokens: params.max_tokens,\n },\n },\n options,\n );\n\n const text = response.output?.message?.content?.map((c) => c.text ?? '').join('') ?? '';\n\n return {\n choices: [{ message: { content: text } }],\n usage: {\n prompt_tokens: response.usage?.inputTokens,\n completion_tokens: response.usage?.outputTokens,\n total_tokens: response.usage?.totalTokens,\n },\n };\n },\n },\n },\n };\n}\n","import type { LLMClient } from '../types.js';\n\n/** The chat-completion-shaped request VernLLM builds internally */\ntype ChatRequest = Parameters<LLMClient['chat']['completions']['create']>[0];\n\nexport interface FetchAdapterConfig {\n /** Endpoint URL, or a function of the request in case it depends on model/params */\n url: string | ((params: ChatRequest) => string);\n /** Static headers, or a function (sync or async) for things like refreshed auth tokens */\n headers?:\n | Record<string, string>\n | (() => Record<string, string> | Promise<Record<string, string>>);\n /** HTTP method. Default 'POST' */\n method?: string;\n /** Maps VernLLMs internal chat-completion request into the providers raw request body */\n mapRequest: (params: ChatRequest) => unknown;\n /**\n * Maps the providers raw JSON response into `{ content, usage? }`\n * `content` is the assistants text (JSON string when JSON mode was requested)\n */\n mapResponse: (json: unknown) => {\n content: string;\n usage?: { promptTokens?: number; completionTokens?: number; totalTokens?: number };\n };\n}\n\n/**\n * A fetch-based escape hatch for providers with no SDK, or where pulling one\n * in isnt worth it. You supply the URL, headers, and two small mapping\n * functions; this handles the HTTP call and slots the result into the same\n * `LLMClient` shape every other adapter produces, so retries, timeouts,\n * the circuit breaker, and JSON/schema handling all still work unmodified\n *\n * Non-2xx responses throw an error with `.status` set to the HTTP status\n * code, so VernLLMs `nonRetryableStatus` handling (e.g. failing fast on\n * 401/403) applies here too\n */\nexport function fromFetch(config: FetchAdapterConfig): LLMClient {\n return {\n chat: {\n completions: {\n async create(params, options) {\n const url = typeof config.url === 'function' ? config.url(params) : config.url;\n const headers =\n typeof config.headers === 'function' ? await config.headers() : config.headers;\n\n const res = await fetch(url, {\n method: config.method ?? 'POST',\n headers: { 'Content-Type': 'application/json', ...headers },\n body: JSON.stringify(config.mapRequest(params)),\n signal: options.signal,\n });\n\n if (!res.ok) {\n const body = await res.text().catch(() => '');\n const err = new Error(\n `Fetch adapter request failed (${res.status}): ${body.slice(0, 500)}`,\n ) as Error & { status?: number };\n err.status = res.status;\n throw err;\n }\n\n const json = await res.json();\n const { content, usage } = config.mapResponse(json);\n\n return {\n choices: [{ message: { content } }],\n usage: usage\n ? {\n prompt_tokens: usage.promptTokens,\n completion_tokens: usage.completionTokens,\n total_tokens: usage.totalTokens,\n }\n : undefined,\n };\n },\n },\n },\n };\n}\n","import type { LLMClient } from '../types.js';\n\n/**\n * Passthrough adapter for any SDK/client whose `chat.completions.create`\n * already matches the OpenAI wire format 1:1 : this covers most hosted\n * inference providers, since \"OpenAI-compatible\" is a de facto standard for\n * chat completion APIs. No transformation happens here, this exists purely\n * so call sites read clearly (`fromMistral(client)` vs handing a Mistral\n * client to something typed for OpenAI) and so a real transformation could\n * be added later, per-provider, without a breaking change.\n *\n * Not every SDKs own TypeScript types line up exactly with `LLMClient`\n * (extra fields, stricter unions, etc.), so this takes `unknown` and casts:\n * the actual compatibility contract is the JSON each provider sends and\n * receives over the wire, not the SDKs TS types.\n */\nexport function fromOpenAICompatible(client: unknown): LLMClient {\n return client as LLMClient;\n}\n\n/** Groqs SDK matches the OpenAI wire format */\nexport const fromGroq = fromOpenAICompatible;\n\n/** Mistrals `chat.completions`-shaped client (or their OpenAI-compat endpoint) */\nexport const fromMistral = fromOpenAICompatible;\n\n/** DeepSeeks API is OpenAI-compatible */\nexport const fromDeepSeek = fromOpenAICompatible;\n\n/** Cerebras inference API is OpenAI-compatible */\nexport const fromCerebras = fromOpenAICompatible;\n\n/** Together AIs API is OpenAI-compatible */\nexport const fromTogether = fromOpenAICompatible;\n\n/** Fireworks AIs API is OpenAI-compatible */\nexport const fromFireworks = fromOpenAICompatible;\n\n/**\n * Ollama exposes an OpenAI-compatible endpoint at `/v1/chat/completions`\n * (as opposed to its native `/api/chat` format, which differs). Point an\n * OpenAI SDK instances `baseURL` at your Ollama server and pass it here:\n * this does not talk to Ollamas native API directly.\n */\nexport const fromOllama = fromOpenAICompatible;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AASA,IAAa,WAAb,cAA8B,MAAM;CAClC,YACEA,SACOC,MACAC,QACAC,QACP;AACA,QAAM,QAAQ;EA2LjB,KA/LU;EA+LT,KA9LS;EA8LR,KA7LQ;AAGP,OAAK,OAAO;CACb;AACF;AAED,SAAgB,WAAWC,KAA+B;AACxD,QAAO,eAAe;AACvB;;;;;AAWD,IAAa,uBAAb,MAA0E;CACxE,AAAQ,QAAQ,IAAI;CAEpB,MAAM,IAAIC,KAAgC;EACxC,MAAM,QAAQ,KAAK,MAAM,IAAI,IAAI;AACjC,OAAK,MAAO,QAAO;AACnB,MAAI,KAAK,KAAK,GAAG,MAAM,WAAW;AAChC,QAAK,MAAM,OAAO,IAAI;AACtB,UAAO;EACR;AACD,SAAO,MAAM;CACd;CAED,MAAM,IAAIA,KAAaC,OAAUC,KAA4B;AAC3D,OAAK,MAAM,IAAI,KAAK;GAAE;GAAO,WAAW,KAAK,KAAK,GAAG,MAAM;EAAM,EAAC;CACnE;AACF;;;;;;;;;;ACjCD,IAAa,iBAAb,MAA4B;CAC1B,AAAQ,QAAsB;CAC9B,AAAQ,sBAAsB;CAC9B,AAAQ,WAAW;CAC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AC7C,MAAM,MAAM,IAAI,OACb,gCAAgC,IAAI,OAAO,KAAK,KAAK,MAAM,GAAG,IAAI,CAAC;AAEtE,OAAI,SAAS,IAAI;AACjB,SAAM;EACP;EAED,MAAM,OAAO,MAAM,IAAI,MAAM;EAC7B,MAAM,EAAE,SAAS,OAAO,GAAG,OAAO,YAAY,KAAK;AAEnD,SAAO;GACL,SAAS,CAAC,EAAE,SAAS,EAAE,QAAS,EAAE,CAAC;GACnC,OAAO,QACH;IACE,eAAe,MAAM;IACrB,mBAAmB,MAAM;IACzB,cAAc,MAAM;GACrB;EAEN;CACF,EACF,EACF,EACF;AACF;;;;;;;;;;;;;;;;;;AC/DD,SAAgB,qBAAqBC,QAA4B;AAC/D,QAAO;AACR;;AAGD,MAAa,WAAW;;AAGxB,MAAa,cAAc;;AAG3B,MAAa,eAAe;;AAG5B,MAAa,eAAe;;AAG5B,MAAa,eAAe;;AAG5B,MAAa,gBAAgB;;;;;;;AAQ7B,MAAa,aAAa"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["message: string","type: LLMErrorType","status?: number","issues?: unknown","err: unknown","key: string","value: T","ttl: number","options: CircuitBreakerOptions","content: string","err: unknown","fn: (signal: AbortSignal) => Promise<T>","timeoutMs: number","externalSignal?: AbortSignal","baseDelayMs: number","attempt: number","delay: number","signal?: AbortSignal","debugEnabled: boolean","message: string","meta?: Record<string, unknown>","options: VernLLMOptions","params: CallParams<T>","fn: () => Promise<T>","requestId: string","signal?: AbortSignal","lastError: unknown","error: unknown","history: ConversationTurn[]","previousRole: 'user' | 'assistant' | undefined","jsonSchema: CallParams<unknown>['jsonSchema']","useJson: boolean","response: Awaited<ReturnType<LLMClient['chat']['completions']['create']>>","model: string","content: string","schema?: CallParams<T>['schema']","parsed: unknown","attempt: number","params: CachedCallParams<T>","params: Omit<CachedCallParams<T>, 'fn'> & { call: CallParams<T> }","anthropicClient: AnthropicClient","jsonInstruction: string | undefined","tools:\n | NonNullable<Parameters<AnthropicClient['messages']['create']>[0]['tools']>\n | undefined","text: string","geminiClient: GeminiClient","generationConfig: NonNullable<\n Parameters<GeminiClient['generateContent']>[0]['generationConfig']\n >","bedrockClient: BedrockConverseClient","jsonInstruction: string | undefined","toolConfig:\n | NonNullable<Parameters<BedrockConverseClient['converse']>[0]['toolConfig']>\n | undefined","text","text: string","config: FetchAdapterConfig","client: unknown"],"sources":["../src/types.ts","../src/circuitBreaker.ts","../src/internal/vernLLM.utilts.ts","../src/logger.ts","../src/vernLLM.ts","../src/adapters/anthropic.ts","../src/adapters/gemini.ts","../src/adapters/bedrock.ts","../src/adapters/fetch.ts","../src/adapters/openaiCompatible.ts"],"sourcesContent":["export type LLMErrorType =\n | 'timeout'\n | 'api'\n | 'parse'\n | 'validation'\n | 'circuit_open'\n | 'unknown'\n | 'aborted';\n\nexport class LLMError extends Error {\n constructor(\n message: string,\n public type: LLMErrorType,\n public status?: number,\n public issues?: unknown,\n ) {\n super(message);\n this.name = 'LLMError';\n }\n}\n\nexport function isLLMError(err: unknown): err is LLMError {\n return err instanceof LLMError;\n}\n\nexport interface CacheAdapter<T = unknown> {\n get(key: string): Promise<T | null>;\n set(key: string, value: T, ttl: number): Promise<void>;\n}\n\n/**\n * Trivial default so the package works out of the box with no external deps\n * Not shared across processes, swap in Redis/Upstash/etc for production\n */\nexport class InMemoryCacheAdapter<T = unknown> implements CacheAdapter<T> {\n private store = new Map<string, { value: T; expiresAt: number }>();\n\n async get(key: string): Promise<T | null> {\n const entry = this.store.get(key);\n if (!entry) return null;\n if (Date.now() > entry.expiresAt) {\n this.store.delete(key);\n return null;\n }\n return entry.value;\n }\n\n async set(key: string, value: T, ttl: number): Promise<void> {\n this.store.set(key, { value, expiresAt: Date.now() + ttl * 1000 });\n }\n}\n\n/**\n * Minimal shape compatible with the OpenAI SDKs chat.completions.create,\n * so consumers can pass an OpenAI client directly\n * `response_format.json_schema` and `reasoning_effort` are optional on the wire\n * providers that don't support them will just ignore fields they don't recognize,\n * but not every SDKs TS types accept them, hence this being a structural type\n * rather than importing the SDKs own params type\n */\nexport interface LLMClient {\n chat: {\n completions: {\n create(\n params: {\n model: string;\n temperature: number;\n max_tokens: number;\n response_format?:\n | { type: 'json_object' }\n | {\n type: 'json_schema';\n json_schema: {\n name: string;\n schema: Record<string, unknown>;\n strict?: boolean;\n description?: string;\n };\n };\n /** OpenAI reasoning-model param (o-series, gpt-5), ignored by providers that don't support it */\n reasoning_effort?: 'minimal' | 'low' | 'medium' | 'high';\n messages: Array<{ role: 'system' | 'user' | 'assistant'; content: string }>;\n },\n options: { signal: AbortSignal },\n ): Promise<{\n choices?: Array<{ message?: { content?: string | null } }>;\n usage?: {\n prompt_tokens?: number;\n completion_tokens?: number;\n total_tokens?: number;\n };\n }>;\n };\n };\n}\n\nexport type ReserveUsage = () => Promise<void>;\nexport type RefundUsage = () => Promise<void>;\n\nexport interface TokenUsage {\n promptTokens: number;\n completionTokens: number;\n totalTokens: number;\n requestId: string;\n model: string;\n}\n\nexport type OnUsage = (usage: TokenUsage) => void;\n\n/**\n * Minimal structural type for a Zod-like schema, so this package doesnt need\n * a hard dependency on a specific Zod major version. Any object exposing\n * `safeParse` (Zod v3/v4, and most Zod-compatible validators) should satisfy this\n */\nexport interface SchemaLike<T> {\n safeParse(data: unknown): { success: true; data: T } | { success: false; error: unknown };\n}\n\nexport interface VernLLMOptions {\n client: LLMClient;\n model: string;\n /** Max retries after the first attempt. Default 1 (2 attempts total) */\n maxRetries?: number;\n /** Per-attempt timeout in ms. Default 25000 */\n timeoutMs?: number;\n /** Base delay for exponential backoff in ms. Default 500 */\n baseDelayMs?: number;\n /** Default max_tokens for calls that don't override it. Default 1000 */\n defaultMaxTokens?: number;\n /** Enables debug logging of raw model output. Default: NODE_ENV !== 'production' */\n debug?: boolean;\n /** Cache adapter for cachedCall. Defaults to an in-memory adapter */\n cache?: CacheAdapter;\n /** HTTP status codes that should fail fast without retrying. Default [400, 401, 403] */\n nonRetryableStatus?: number[];\n /** Custom JSON parser. Must return undefined/null on failure. Default: JSON.parse wrapped in try/catch */\n parseJson?: (content: string) => unknown;\n /** Called after every successful call with token usage, if the provider reports it */\n onUsage?: OnUsage;\n /** Injectable logger. Defaults to a console-based logger gated by `debug` */\n logger?: import('./logger.js').Logger;\n /**\n * Enables a circuit breaker that short-circuits calls after repeated\n * consecutive failures, instead of continuing to hammer a down provider\n * Pass `true` for defaults, or an options object to tune threshold/cooldown\n */\n circuitBreaker?: boolean | import('./circuitBreaker.js').CircuitBreakerOptions;\n}\n\n/**\n * A provider-native JSON Schema for structured outputs (OpenAI/Groq\n * `response_format: { type: 'json_schema' }`) This is the wire-format\n * schema the model is constrained to generate against, distinct from\n * `schema`, which is a client-side Zod validator run on the parsed result\n * You can use one, both, or neither; using both gets you provider-level\n * constraint plus client-side type inference/validation as a safety net\n */\nexport interface JsonSchemaSpec {\n name: string;\n schema: Record<string, unknown>;\n /** Enforces the schema strictly (OpenAI-specific), default true when supported */\n strict?: boolean;\n description?: string;\n}\n\n/** A single prior turn in a multi-turn conversation, passed via `history`. */\nexport interface ConversationTurn {\n role: 'user' | 'assistant';\n content: string;\n}\n\nexport interface CallParams<T = unknown> {\n systemPrompt: string;\n userContent: string;\n /**\n * Prior turns in the conversation, oldest first, NOT including the current\n * `userContent` (that's appended automatically as the final user turn).\n * Passed straight through to the provider so follow-up questions have\n * access to earlier context. Must strictly alternate user/assistant and\n * end on an assistant turn; validated up front and rejected with an\n * LLMError('validation') before any request is made if malformed.\n */\n history?: ConversationTurn[];\n temperature?: number;\n jsonMode?: boolean;\n maxTokens?: number;\n requestId?: string;\n signal?: AbortSignal;\n /** Overrides the model set on the VernLLM instance for this call only */\n model?: string;\n /**\n * OpenAI-style reasoning effort for reasoning models (o-series, gpt-5, etc).\n * Passed through as-is, providers/models that don't support it ignore it\n */\n reasoningEffort?: 'minimal' | 'low' | 'medium' | 'high';\n /**\n * Provider-native JSON Schema structured-output mode. When set, this is sent\n * as `response_format: { type: 'json_schema', json_schema: ... }` instead of\n * the looser `json_object` mode, constraining the models output shape at\n * generation time (not just validating it after the fact). Implies jsonMode: true.\n */\n jsonSchema?: JsonSchemaSpec;\n /**\n * Optional Zod (or Zod-compatible) schema. When provided, the parsed JSON\n * is validated against it; on failure an LLMError('validation') is thrown\n * with `.issues` set to the schema's error object. Implies jsonMode: true.\n * Can be combined with `jsonSchema` for provider-level constraint + client-side typing.\n */\n schema?: SchemaLike<T>;\n}\n\nexport interface CachedCallParams<T> {\n cacheKey: string;\n ttl: number;\n fn: () => Promise<T>;\n reserveUsage?: ReserveUsage;\n refundUsage?: RefundUsage;\n}\n","import { LLMError } from './types.js';\n\nexport interface CircuitBreakerOptions {\n /** Consecutive failures before the circuit opens, default 5 */\n threshold?: number;\n /** How long the circuit stays open before allowing a trial request, in ms. Default 30000 */\n cooldownMs?: number;\n}\n\ntype CircuitState = 'closed' | 'open' | 'half-open';\n\n/**\n * Per retry VernLLM-instance circuit breaker. Tracks consecutive failures across\n * calls. Once the threshold is hit, short-circuits new calls with an\n * LLMError('circuit_open') instead of hitting the provider, until the\n * cooldown elapses and a single trial call is allowed through\n */\nexport class CircuitBreaker {\n private state: CircuitState = 'closed';\n private consecutiveFailures = 0;\n private openedAt = 0;\n private threshold: number;\n private cooldownMs: number;\n\n constructor(options: CircuitBreakerOptions = {}) {\n this.threshold = options.threshold ?? 5;\n this.cooldownMs = options.cooldownMs ?? 30_000;\n }\n\n /** Throws if the circuit is open and the cooldown hasnt elapsed */\n assertClosed(): void {\n if (this.state !== 'open') return;\n\n const elapsed = Date.now() - this.openedAt;\n if (elapsed >= this.cooldownMs) {\n this.state = 'half-open';\n return;\n }\n\n throw new LLMError(\n `Circuit open — provider has failed ${this.consecutiveFailures} times in a row. Retry in ${Math.ceil((this.cooldownMs - elapsed) / 1000)}s.`,\n 'circuit_open',\n );\n }\n\n recordSuccess(): void {\n this.consecutiveFailures = 0;\n this.state = 'closed';\n }\n\n recordFailure(): void {\n this.consecutiveFailures += 1;\n\n if (this.state === 'half-open') {\n // Trial call failed: reopen and reset the cooldown window.\n this.state = 'open';\n this.openedAt = Date.now();\n return;\n }\n\n if (this.consecutiveFailures >= this.threshold) {\n this.state = 'open';\n this.openedAt = Date.now();\n }\n }\n\n getState(): CircuitState {\n return this.state;\n }\n}\n","import { LLMError } from '../types.js';\n\nexport function defaultParseJson(content: string): unknown {\n try {\n return JSON.parse(content);\n } catch {\n return undefined;\n }\n}\n\n/**\n * Looks inside an unknown error value and pulls out an http status code\n * if one is present. Checks the status field first then the status code\n * field since different client libraries use different names for this.\n * Returns undefined when the error is not an object or carries no status\n */\nexport function extractStatus(err: unknown): number | undefined {\n if (!err || typeof err !== 'object') return undefined;\n\n const error = err as {\n status?: unknown;\n statusCode?: unknown;\n };\n\n if (typeof error.status === 'number') return error.status;\n if (typeof error.statusCode === 'number') return error.statusCode;\n\n return undefined;\n}\n\n/**\n * Runs an async function and cancels it if it takes longer than the given\n * timeout. Creates an internal abort controller that fires after the\n * timeout elapses, and combines it with any external signal the caller\n * passed in so either one can cancel the underlying call. If the internal\n * timeout triggers and the underlying operation aborts, the error is\n * converted into an LLMError with type \"timeout\". External cancellations\n * continue to propagate as aborted errors. The internal timer is always\n * cleared afterward, whether the function succeeds, fails, or is aborted,\n * so nothing is left running in the background.\n */\nexport async function withTimeout<T>(\n fn: (signal: AbortSignal) => Promise<T>,\n timeoutMs: number,\n externalSignal?: AbortSignal,\n): Promise<T> {\n const controller = new AbortController();\n\n const timer = setTimeout(() => {\n controller.abort();\n }, timeoutMs);\n\n const signal = externalSignal\n ? AbortSignal.any([externalSignal, controller.signal])\n : controller.signal;\n\n try {\n return await fn(signal);\n } catch (err) {\n if (\n controller.signal.aborted &&\n !externalSignal?.aborted &&\n err instanceof DOMException &&\n err.name === 'AbortError'\n ) {\n throw new LLMError('Request timed out', 'timeout');\n }\n\n throw err;\n } finally {\n clearTimeout(timer);\n }\n}\n\n/**\n * Exponential backoff with jitter, capped at maxDelayMs.\n * Jitter avoids thundering-herd retries when many callers back off in lockstep,\n * the cap prevents unbounded delays when maxRetries is high\n */\nexport function getBackoffDelay(baseDelayMs: number, attempt: number, maxDelayMs = 10_000): number {\n const exp = Math.min(baseDelayMs * 2 ** attempt, maxDelayMs);\n return exp / 2 + Math.random() * (exp / 2);\n}\n\n/**\n * Pauses execution for the given delay before a retry attempt. If an\n * abort signal is provided and it fires while waiting, the pending\n * timer is cancelled immediately and the wait rejects right away with\n * an aborted error instead of continuing to sit idle until the delay\n * would have finished on its own\n */\nexport async function waitForRetry(delay: number, signal?: AbortSignal): Promise<void> {\n await new Promise<void>((resolve, reject) => {\n const onAbort = () => {\n clearTimeout(timer);\n reject(new LLMError('Operation aborted', 'aborted'));\n };\n\n const timer = setTimeout(() => {\n signal?.removeEventListener('abort', onAbort);\n resolve();\n }, delay);\n\n signal?.addEventListener('abort', onAbort, { once: true });\n });\n}\n","export interface Logger {\n debug(message: string): void;\n warn(message: string): void;\n error(message: string, meta?: Record<string, unknown>): void;\n}\n\n/**\n * Default logger. `debug` is gated by the `debug` option on VernLLM\n * warn/error always fire since they indicate real problems (retries, cache failures)\n */\nexport class ConsoleLogger implements Logger {\n constructor(private debugEnabled: boolean) {}\n\n debug(message: string): void {\n if (this.debugEnabled) console.debug(message);\n }\n\n warn(message: string): void {\n console.warn(message);\n }\n\n error(message: string, meta?: Record<string, unknown>): void {\n console.error(message, meta ?? '');\n }\n}\n","import { randomUUID } from 'crypto';\n\nimport { CircuitBreaker } from './circuitBreaker.js';\nimport {\n defaultParseJson,\n extractStatus,\n withTimeout,\n getBackoffDelay,\n waitForRetry,\n} from './internal/vernLLM.utilts.js';\nimport { ConsoleLogger, type Logger } from './logger.js';\nimport {\n LLMError,\n InMemoryCacheAdapter,\n type VernLLMOptions,\n type CallParams,\n type CachedCallParams,\n type ConversationTurn,\n type CacheAdapter,\n type LLMClient,\n} from './types.js';\n\n/**\n * A resilient wrapper around an LLM chat completions client, this is VernLLM!\n *\n * Adds retry with exponential backoff and jitter, per-attempt timeouts,\n * an optional circuit breaker, JSON parsing with optional schema\n * validation, usage tracking, and an optional response cache, all\n * configurable, all opt-in beyond sensible defaults\n */\nexport class VernLLM {\n private readonly client: LLMClient;\n private readonly model: string;\n\n private readonly maxRetries: number;\n private readonly timeoutMs: number;\n private readonly baseDelayMs: number;\n private readonly defaultMaxTokens: number;\n\n private readonly cache: CacheAdapter<unknown>;\n private readonly nonRetryableStatus: number[];\n\n private readonly parseJson: (content: string) => unknown;\n private readonly onUsage?: VernLLMOptions['onUsage'];\n\n private readonly logger: Logger;\n private readonly breaker?: CircuitBreaker;\n\n /**\n * @param options: Client, model, and all tunables (retries, timeout,\n * backoff, cache, circuit breaker, logger, etc). See VernLLMOptions in `types.ts`\n * for individual defaults\n */\n constructor(options: VernLLMOptions) {\n this.client = options.client;\n this.model = options.model;\n\n const retryConfig = this.resolveRetryConfig(options);\n this.maxRetries = retryConfig.maxRetries;\n this.timeoutMs = retryConfig.timeoutMs;\n this.baseDelayMs = retryConfig.baseDelayMs;\n this.defaultMaxTokens = retryConfig.defaultMaxTokens;\n\n this.cache = options.cache ?? new InMemoryCacheAdapter();\n this.nonRetryableStatus = options.nonRetryableStatus ?? [400, 401, 403];\n\n this.parseJson = options.parseJson ?? defaultParseJson;\n this.onUsage = options.onUsage;\n\n this.logger = this.resolveLogger(options);\n this.breaker = this.resolveCircuitBreaker(options);\n }\n\n /**\n * Resolves retry/timeout/token defaults from the given options,\n * falling back to the librarys built-in defaults for anything unset\n */\n private resolveRetryConfig(options: VernLLMOptions) {\n return {\n maxRetries: options.maxRetries ?? 1,\n timeoutMs: options.timeoutMs ?? 25_000,\n baseDelayMs: options.baseDelayMs ?? 500,\n defaultMaxTokens: options.defaultMaxTokens ?? 1000,\n };\n }\n\n /**\n * Returns the caller supplied logger, or a console-based logger whose\n * debug output is gated by the `debug` option (defaulting to on\n * outside production)\n */\n private resolveLogger(options: VernLLMOptions): Logger {\n return (\n options.logger ?? new ConsoleLogger(options.debug ?? process.env.NODE_ENV !== 'production')\n );\n }\n\n /**\n * Builds a circuit breaker if `circuitBreaker` is truthy on the\n * options. Passing `true` uses default thresholds, passing an options\n * object tunes them. Returns undefined when the breaker is disabled\n */\n private resolveCircuitBreaker(options: VernLLMOptions): CircuitBreaker | undefined {\n if (!options.circuitBreaker) return undefined;\n\n return new CircuitBreaker(options.circuitBreaker === true ? undefined : options.circuitBreaker);\n }\n\n /**\n * Makes a single logical LLM call, transparently retrying on failure\n * according to the configured retry policy\n *\n * Fails fast if the circuit breaker is open or the signal is already\n * aborted, before any request is dispatched. On exhausting all\n * retries, records a circuit breaker failure and rejects with a\n * normalized LLMError\n *\n * @param params : System/user content plus per call overrides\n * (model, temperature, jsonMode, schema, signal, etc)\n * @returns The parsed and optionally schema-validated response, or\n * the raw string content when jsonMode is disabled\n */\n async call<T = unknown>(params: CallParams<T>): Promise<T> {\n this.breaker?.assertClosed();\n\n if (params.signal?.aborted) {\n throw new LLMError('LLM request aborted', 'aborted');\n }\n\n const requestId = params.requestId ?? randomUUID();\n\n try {\n const result = await this.retryWithBackoff(\n () => this.executeCall(params, requestId),\n requestId,\n params.signal,\n );\n\n return result;\n } catch (error) {\n this.breaker?.recordFailure();\n throw this.normalizeError(error, params.signal);\n }\n }\n\n /**\n * Runs `fn`, retrying with backoff according to `shouldRetry` policy\n * Purely mechanical: knows nothing about LLM specifics beyond the retry\n * predicate, so its testable independent of request/response shaping\n */\n private async retryWithBackoff<T>(\n fn: () => Promise<T>,\n requestId: string,\n signal?: AbortSignal,\n ): Promise<T> {\n let lastError: unknown;\n\n for (let attempt = 0; attempt <= this.maxRetries; attempt++) {\n try {\n if (attempt > 0) {\n await this.recoverDelay(requestId, attempt, signal);\n }\n\n return await fn();\n } catch (error) {\n lastError = error;\n\n if (!this.shouldRetry(error, signal)) {\n break;\n }\n }\n }\n\n throw lastError;\n }\n\n /**\n * Converts any thrown value into a well-typed LLMError for the public\n * API surface. Preserves an existing LLMError as is, reports aborted\n * signals as such, classifies errors carrying an http status as\n * type api, and otherwise falls back to a generic unknown error.\n */\n private normalizeError(error: unknown, signal?: AbortSignal): LLMError {\n if (signal?.aborted) {\n return new LLMError('LLM request aborted', 'aborted');\n }\n\n if (error instanceof LLMError) {\n return error;\n }\n\n const status = extractStatus(error);\n\n if (status !== undefined) {\n return new LLMError('LLM request failed', 'api', status);\n }\n\n return new LLMError('LLM request failed', 'unknown');\n }\n\n /**\n * Performs a single attempt: builds the request, dispatches it with a\n * timeout, and shapes the response. Throws on an empty response so\n * the retry loop treats it like any other transient failure. Records\n * usage and a circuit breaker success before returning\n */\n private async executeCall<T>(params: CallParams<T>, requestId: string): Promise<T> {\n const { useJson, model, request } = this.buildRequestPayload(params);\n\n const response = await withTimeout(\n (attemptSignal) => this.client.chat.completions.create(request, { signal: attemptSignal }),\n this.timeoutMs,\n params.signal,\n );\n\n const content = response.choices?.[0]?.message?.content?.trim();\n\n if (!content) {\n throw new LLMError('Empty LLM response', 'api');\n }\n\n this.logger.debug(`[vern:${requestId}] output:\\n${content.slice(0, 800)}`);\n\n this.recordUsage(response, requestId, model);\n\n this.breaker?.recordSuccess();\n\n if (!useJson) {\n return content as T;\n }\n\n return this.parseAndValidate(content, params.schema);\n }\n\n /**\n * Anthropic and Gemini both require strict user/assistant alternation\n * (and reject or silently mishandle two consecutive same-role turns), so\n * this validates `history` up front rather than letting a malformed\n * request surface as a confusing provider-side error. Thrown as a\n * validation LLMError, which `shouldRetry` never retries, since retrying\n * the same malformed input can't succeed\n */\n private validateHistory(history: ConversationTurn[]): void {\n let previousRole: 'user' | 'assistant' | undefined;\n\n for (const [index, turn] of history.entries()) {\n if (turn.role !== 'user' && turn.role !== 'assistant') {\n throw new LLMError(\n `Invalid history[${index}].role \"${turn.role}\": must be \"user\" or \"assistant\"`,\n 'validation',\n );\n }\n\n if (turn.role === previousRole) {\n throw new LLMError(\n `history must alternate user/assistant turns: consecutive \"${turn.role}\" turns at history[${index - 1}] and history[${index}]`,\n 'validation',\n );\n }\n\n previousRole = turn.role;\n }\n\n if (previousRole === 'user') {\n throw new LLMError(\n 'The last entry in history is a \"user\" turn, which would collide with the current userContent turn. history must end with an \"assistant\" turn (or be empty).',\n 'validation',\n );\n }\n }\n\n /**\n * Applies per call defaults and shapes the params into the request\n * object expected by the underlying client, including the resolved\n * response format. Also returns whether JSON parsing should be\n * applied to the response and which model was ultimately used\n */\n private buildRequestPayload<T>(params: CallParams<T>) {\n const {\n systemPrompt,\n userContent,\n history = [],\n temperature = 0.2,\n jsonMode = true,\n maxTokens = this.defaultMaxTokens,\n model = this.model,\n reasoningEffort,\n jsonSchema,\n } = params;\n\n const useJson = jsonMode || Boolean(jsonSchema);\n const responseFormat = this.buildResponseFormat(jsonSchema, useJson);\n\n this.validateHistory(history);\n\n const request = {\n model,\n temperature,\n max_tokens: maxTokens,\n ...(responseFormat ? { response_format: responseFormat } : {}),\n ...(reasoningEffort ? { reasoning_effort: reasoningEffort } : {}),\n messages: [\n { role: 'system' as const, content: systemPrompt },\n ...history.map((turn) => ({ role: turn.role, content: turn.content })),\n { role: 'user' as const, content: userContent },\n ],\n };\n\n return { useJson, model, request };\n }\n\n /**\n * Chooses the response format to send to the provider. A provider\n * native json schema takes priority when supplied, constraining\n * generation directly, otherwise falls back to the looser json\n * object mode when JSON output is requested, or no format at all\n * for plain text responses\n */\n private buildResponseFormat(jsonSchema: CallParams<unknown>['jsonSchema'], useJson: boolean) {\n if (jsonSchema) {\n return {\n type: 'json_schema' as const,\n json_schema: {\n name: jsonSchema.name,\n schema: jsonSchema.schema,\n strict: jsonSchema.strict ?? true,\n description: jsonSchema.description,\n },\n };\n }\n\n return useJson ? { type: 'json_object' as const } : undefined;\n }\n\n /**\n * Reports token usage to the caller supplied onUsage callback, when\n * both a callback was configured and the provider actually returned\n * usage data on this response. A no op otherwise.\n */\n private recordUsage(\n response: Awaited<ReturnType<LLMClient['chat']['completions']['create']>>,\n requestId: string,\n model: string,\n ): void {\n if (!response.usage || !this.onUsage) return;\n\n this.onUsage({\n promptTokens: response.usage.prompt_tokens ?? 0,\n completionTokens: response.usage.completion_tokens ?? 0,\n totalTokens: response.usage.total_tokens ?? 0,\n requestId,\n model,\n });\n }\n\n /**\n * Parses the raw response content as JSON and, when a schema is\n * supplied, validates the parsed value against it. Throws a parse\n * type LLMError on malformed JSON and a validation type LLMError,\n * carrying the schemas issues, on a failed validation\n */\n private parseAndValidate<T>(content: string, schema?: CallParams<T>['schema']): T {\n let parsed: unknown;\n\n try {\n parsed = this.parseJson(content);\n } catch {\n throw new LLMError('Invalid JSON response', 'parse');\n }\n\n if (parsed === null || parsed === undefined) {\n throw new LLMError('Invalid JSON response', 'parse');\n }\n\n if (!schema) {\n return parsed as T;\n }\n\n const result = schema.safeParse(parsed);\n\n if (!result.success) {\n throw new LLMError('Schema validation failed', 'validation', undefined, result.error);\n }\n\n return result.data;\n }\n\n /**\n * Waits out the backoff delay for a given retry attempt, logging the\n * attempt for observability before the wait begins. Rejects early if\n * the signal aborts during the wait\n */\n private async recoverDelay(requestId: string, attempt: number, signal?: AbortSignal) {\n const delay = getBackoffDelay(this.baseDelayMs, attempt);\n\n this.logger.warn(\n `[vern:${requestId}] recovery attempt ${attempt}/${this.maxRetries}, waiting ${delay}ms`,\n );\n\n await waitForRetry(delay, signal);\n }\n\n /**\n * Decides whether a failed attempt is worth retrying. Never retries\n * once the signal has aborted, never retries a parse or validation\n * failure since those stem from the response content rather than a\n * transient fault, and never retries a status code the caller has\n * marked as non retryable. Retries everything else\n */\n private shouldRetry(error: unknown, signal?: AbortSignal): boolean {\n if (signal?.aborted) {\n return false;\n }\n\n if (error instanceof LLMError && (error.type === 'parse' || error.type === 'validation')) {\n return false;\n }\n\n const status = extractStatus(error);\n\n if (status !== undefined && this.nonRetryableStatus.includes(status)) {\n return false;\n }\n\n return true;\n }\n\n /**\n * Thin cache wrapper around caller supplied logic. `params.fn` is expected\n * to be a call that itself invokes `this.call(...)` (see `cachedLLMCall`\n * below for a convenience wrapper that wires this up automatically),\n * `cachedCall` does not itself apply retry/timeout policy.\n */\n async cachedCall<T>(params: CachedCallParams<T>): Promise<T> {\n const cached = await this.cache.get(params.cacheKey);\n\n if (cached !== null) {\n return cached as T;\n }\n\n try {\n await params.reserveUsage?.();\n\n const result = await params.fn();\n\n try {\n await this.cache.set(params.cacheKey, result, params.ttl);\n } catch (error) {\n this.logger.error('[VernLLM] cache write failed', {\n message: error instanceof Error ? error.message : 'unknown',\n });\n }\n\n return result;\n } catch (error) {\n try {\n await params.refundUsage?.();\n } catch (refundError) {\n this.logger.error('[VernLLM] refundUsage failed', {\n message: refundError instanceof Error ? refundError.message : 'unknown',\n });\n }\n\n throw error;\n }\n }\n\n /**\n * Convenience wrapper composing `call` + `cachedCall`, so cached LLM calls\n * automatically get retry/timeout/circuit-breaker behavior without callers\n * having to remember to wire `fn: () => this.call(...)` themselves\n */\n async cachedLLMCall<T>(\n params: Omit<CachedCallParams<T>, 'fn'> & { call: CallParams<T> },\n ): Promise<T> {\n const { call: callParams, ...cacheParams } = params;\n\n return this.cachedCall({\n ...cacheParams,\n fn: () => this.call(callParams),\n });\n }\n\n /**\n * Returns the current circuit breaker state, or undefined when no\n * circuit breaker was configured on this instance\n */\n getCircuitState() {\n return this.breaker?.getState();\n }\n}\n","import type { LLMClient } from '../types.js';\n\n/** Minimal structural type for the Anthropic SDKs `messages.create` */\nexport interface AnthropicClient {\n messages: {\n create(\n params: {\n model: string;\n max_tokens: number;\n temperature?: number;\n system?: string;\n messages: Array<{ role: 'user' | 'assistant'; content: string }>;\n tools?: Array<{\n name: string;\n description?: string;\n input_schema: Record<string, unknown>;\n }>;\n tool_choice?: { type: 'tool'; name: string };\n },\n options: { signal: AbortSignal },\n ): Promise<{\n content: Array<{ type: string; text?: string; name?: string; input?: unknown }>;\n usage?: { input_tokens?: number; output_tokens?: number };\n }>;\n };\n}\n\n/**\n * Wraps an Anthropic SDK client so it satisfies the same `LLMClient`\n * interface VernLLM uses for OpenAI/Groq.\n *\n * `response_format: json_schema` is mapped to Anthropics forced tool-use:\n * a single tool is defined with `input_schema` set to the caller's schema,\n * and `tool_choice` forces the model to call it, so the output is\n * provider-constrained to match the schema rather than merely instructed\n * to via prompt text (the same guarantee OpenAIs native `json_schema` mode\n * gives, built on Anthropics tool-calling primitive instead).\n *\n * `response_format: json_object` (no schema to build a tool from) falls\n * back to a system-prompt instruction, since theres nothing to constrain\n * generation against.\n */\nexport function fromAnthropic(anthropicClient: AnthropicClient): LLMClient {\n return {\n chat: {\n completions: {\n async create(params, options) {\n const systemMessage = params.messages.find((m) => m.role === 'system');\n // Keep both user and assistant turns, in order, so multi-turn history\n // survives instead of collapsing to consecutive user messages.\n const conversationMessages = params.messages.filter(\n (m): m is typeof m & { role: 'user' | 'assistant' } =>\n m.role === 'user' || m.role === 'assistant',\n );\n\n const toolName =\n params.response_format?.type === 'json_schema'\n ? params.response_format.json_schema.name\n : undefined;\n\n let jsonInstruction: string | undefined;\n let tools:\n | NonNullable<Parameters<AnthropicClient['messages']['create']>[0]['tools']>\n | undefined;\n\n if (params.response_format?.type === 'json_schema' && toolName) {\n const { schema, description } = params.response_format.json_schema;\n tools = [{ name: toolName, description, input_schema: schema }];\n } else if (params.response_format?.type === 'json_object') {\n // No schema to build a tool from, fall back to a prompt instruction\n jsonInstruction = 'Respond with valid JSON only, no prose or markdown fences.';\n }\n\n // `reasoning_effort` (OpenAI o-series/gpt-5 style) has no direct Anthropic\n // equivalent, Claudes extended thinking uses a token budget, not a tier\n // string, so its intentionally dropped here rather than guessed at.\n\n const system = [systemMessage?.content, jsonInstruction].filter(Boolean).join('\\n\\n');\n\n const response = await anthropicClient.messages.create(\n {\n model: params.model,\n max_tokens: params.max_tokens,\n temperature: params.temperature,\n system: system || undefined,\n messages: conversationMessages.map((m) => ({ role: m.role, content: m.content })),\n ...(tools ? { tools, tool_choice: { type: 'tool' as const, name: toolName! } } : {}),\n },\n options,\n );\n\n let text: string;\n if (toolName) {\n // Forced tool-use: the schema-conforming payload arrives as the\n // tool_use block's already-parsed `input`, not as text. Re-serialize\n // it to JSON so it flows through the same string-content contract\n // every other adapter uses (VernLLM JSON.parses the content itself).\n const toolUse = response.content.find(\n (block) => block.type === 'tool_use' && block.name === toolName,\n );\n text = toolUse ? JSON.stringify(toolUse.input) : '';\n } else {\n text = response.content.find((block) => block.type === 'text')?.text ?? '';\n }\n\n return {\n choices: [{ message: { content: text } }],\n usage: {\n prompt_tokens: response.usage?.input_tokens,\n completion_tokens: response.usage?.output_tokens,\n total_tokens:\n (response.usage?.input_tokens ?? 0) + (response.usage?.output_tokens ?? 0),\n },\n };\n },\n },\n },\n };\n}\n","import type { LLMClient } from '../types.js';\n\n/**\n * Minimal structural type for Geminis `generateContent`, matching both the\n * legacy `@google/generative-ai` SDKs `model.generateContent(...)` and the\n * newer `@google/genai` SDKs `ai.models.generateContent({ model, ... })`\n * closely enough to adapt either — pass whichever `.generateContent` you have.\n */\nexport interface GeminiClient {\n generateContent(\n params: {\n model?: string;\n contents: Array<{ role: 'user' | 'model'; parts: Array<{ text: string }> }>;\n systemInstruction?: { parts: Array<{ text: string }> };\n generationConfig?: {\n temperature?: number;\n maxOutputTokens?: number;\n responseMimeType?: string;\n responseSchema?: Record<string, unknown>;\n };\n },\n options: { signal: AbortSignal },\n ): Promise<{\n candidates?: Array<{ content?: { parts?: Array<{ text?: string }> } }>;\n usageMetadata?: {\n promptTokenCount?: number;\n candidatesTokenCount?: number;\n totalTokenCount?: number;\n };\n }>;\n}\n\n/**\n * Wraps a Gemini client so it satisfies the `LLMClient` interface VernLLM\n * uses for OpenAI/Groq. Geminis shape differs on nearly every axis: a\n * `contents` array instead of `messages`, a separate `systemInstruction`\n * field instead of a `system` role message, `generationConfig` instead of\n * top-level `temperature`/`max_tokens`, and native JSON Schema support via\n * `responseMimeType: 'application/json'` + `responseSchema` (so `jsonSchema`\n * is provider-enforced here, unlike the Anthropic adapters prompt-embedding\n * fallback). `reasoning_effort` has no equivalent. Geminis thinking models\n * use a token budget, not an effort tier, so its dropped, same as Anthropic.\n */\nexport function fromGemini(geminiClient: GeminiClient): LLMClient {\n return {\n chat: {\n completions: {\n async create(params, options) {\n const systemMessage = params.messages.find((m) => m.role === 'system');\n // Keep both user and assistant turns, in order. Gemini calls the\n // assistant role 'model' rather than 'assistant'.\n const conversationMessages = params.messages.filter(\n (m) => m.role === 'user' || m.role === 'assistant',\n );\n\n const wantsJson = Boolean(params.response_format);\n const generationConfig: NonNullable<\n Parameters<GeminiClient['generateContent']>[0]['generationConfig']\n > = {\n temperature: params.temperature,\n maxOutputTokens: params.max_tokens,\n };\n\n if (wantsJson) {\n generationConfig.responseMimeType = 'application/json';\n }\n if (params.response_format?.type === 'json_schema') {\n generationConfig.responseSchema = params.response_format.json_schema.schema;\n }\n\n const response = await geminiClient.generateContent(\n {\n model: params.model,\n contents: conversationMessages.map((m) => ({\n role: m.role === 'assistant' ? ('model' as const) : ('user' as const),\n parts: [{ text: m.content }],\n })),\n systemInstruction: systemMessage\n ? { parts: [{ text: systemMessage.content }] }\n : undefined,\n generationConfig,\n },\n options,\n );\n\n const text =\n response.candidates?.[0]?.content?.parts?.map((p) => p.text ?? '').join('') ?? '';\n\n return {\n choices: [{ message: { content: text } }],\n usage: {\n prompt_tokens: response.usageMetadata?.promptTokenCount,\n completion_tokens: response.usageMetadata?.candidatesTokenCount,\n total_tokens: response.usageMetadata?.totalTokenCount,\n },\n };\n },\n },\n },\n };\n}\n","import type { LLMClient } from '../types.js';\n\n/**\n * Minimal structural type matching AWS Bedrocks Converse API. This is\n * intentionally NOT `BedrockRuntimeClient` itself, the AWS SDK v3 client\n * exposes `.send(command)`, not a direct `.converse()` method, and pulling\n * in `@aws-sdk/client-bedrock-runtime` as a dependency just for its types\n * isn't worth it for a structural adapter. Wrap your client, e.g:\n *\n * ```ts\n * import { BedrockRuntimeClient, ConverseCommand } from '@aws-sdk/client-bedrock-runtime';\n * const client = new BedrockRuntimeClient({ region: 'us-east-1' });\n * const converseClient = {\n * converse: (params, options) =>\n * client.send(new ConverseCommand(params), { abortSignal: options.signal }),\n * };\n * ```\n */\nexport interface BedrockConverseClient {\n converse(\n params: {\n modelId: string;\n messages: Array<{ role: 'user' | 'assistant'; content: Array<{ text: string }> }>;\n system?: Array<{ text: string }>;\n inferenceConfig?: { temperature?: number; maxTokens?: number };\n toolConfig?: {\n tools: Array<{\n toolSpec: {\n name: string;\n description?: string;\n inputSchema: { json: Record<string, unknown> };\n };\n }>;\n toolChoice?: { tool: { name: string } };\n };\n },\n options: { signal: AbortSignal },\n ): Promise<{\n output?: {\n message?: {\n content?: Array<{ text?: string; toolUse?: { name?: string; input?: unknown } }>;\n };\n };\n usage?: { inputTokens?: number; outputTokens?: number; totalTokens?: number };\n }>;\n}\n\n/**\n * Wraps a Bedrock Converse-API client so it satisfies the `LLMClient`\n * interface VernLLM uses for OpenAI/Groq. The Converse API is unified\n * across Bedrocks model families (Anthropic, Titan, Llama, Mistral, etc.),\n * so unlike raw per-model Bedrock invocation, this one adapter works\n * regardless of which underlying model `modelId` points at, as long as\n * that model supports Converse (most current-generation ones do)\n *\n * `response_format: json_schema` is mapped to Converses `toolConfig`: a\n * single tool is defined from the schema and `toolChoice` forces the model\n * to call it, constraining output at generation time rather than merely\n * instructing for it via prompt text. Native tool support varies by model\n * family (most current-generation ones support it via Converse; check your\n * specific `modelId` if a call fails with an unsupported-parameter error).\n * `response_format: json_object` (no schema to build a tool from) and\n * `reasoning_effort` (no Converse equivalent) fall back to a system-prompt\n * instruction and are dropped respectively.\n */\nexport function fromBedrock(bedrockClient: BedrockConverseClient): LLMClient {\n return {\n chat: {\n completions: {\n async create(params, options) {\n const systemMessage = params.messages.find((m) => m.role === 'system');\n // Keep both user and assistant turns, in order, so conversation\n // history survives instead of collapsing to consecutive user turns.\n const conversationMessages = params.messages.filter(\n (m): m is typeof m & { role: 'user' | 'assistant' } =>\n m.role === 'user' || m.role === 'assistant',\n );\n\n const toolName =\n params.response_format?.type === 'json_schema'\n ? params.response_format.json_schema.name\n : undefined;\n\n let jsonInstruction: string | undefined;\n let toolConfig:\n | NonNullable<Parameters<BedrockConverseClient['converse']>[0]['toolConfig']>\n | undefined;\n\n if (params.response_format?.type === 'json_schema' && toolName) {\n const { schema, description } = params.response_format.json_schema;\n toolConfig = {\n tools: [{ toolSpec: { name: toolName, description, inputSchema: { json: schema } } }],\n toolChoice: { tool: { name: toolName } },\n };\n } else if (params.response_format?.type === 'json_object') {\n // No schema to build a tool from, fall back to a prompt instruction\n jsonInstruction = 'Respond with valid JSON only, no prose or markdown fences.';\n }\n\n const systemParts = [systemMessage?.content, jsonInstruction].filter((s): s is string =>\n Boolean(s),\n );\n\n const response = await bedrockClient.converse(\n {\n modelId: params.model,\n messages: conversationMessages.map((m) => ({\n role: m.role,\n content: [{ text: m.content }],\n })),\n system: systemParts.length ? systemParts.map((text) => ({ text })) : undefined,\n inferenceConfig: {\n temperature: params.temperature,\n maxTokens: params.max_tokens,\n },\n ...(toolConfig ? { toolConfig } : {}),\n },\n options,\n );\n\n let text: string;\n if (toolName) {\n // Forced tool-use: the schema-conforming payload arrives as the\n // toolUse content block's already-parsed `input`, not as text.\n // Re-serialize it to JSON so it flows through the same\n // string-content contract every other adapter uses.\n const toolUseBlock = response.output?.message?.content?.find(\n (block) => block.toolUse?.name === toolName,\n );\n text = toolUseBlock?.toolUse ? JSON.stringify(toolUseBlock.toolUse.input) : '';\n } else {\n text = response.output?.message?.content?.map((c) => c.text ?? '').join('') ?? '';\n }\n\n return {\n choices: [{ message: { content: text } }],\n usage: {\n prompt_tokens: response.usage?.inputTokens,\n completion_tokens: response.usage?.outputTokens,\n total_tokens: response.usage?.totalTokens,\n },\n };\n },\n },\n },\n };\n}\n","import type { LLMClient } from '../types.js';\n\n/** The chat-completion-shaped request VernLLM builds internally */\ntype ChatRequest = Parameters<LLMClient['chat']['completions']['create']>[0];\n\nexport interface FetchAdapterConfig {\n /** Endpoint URL, or a function of the request in case it depends on model/params */\n url: string | ((params: ChatRequest) => string);\n /** Static headers, or a function (sync or async) for things like refreshed auth tokens */\n headers?:\n | Record<string, string>\n | (() => Record<string, string> | Promise<Record<string, string>>);\n /** HTTP method. Default 'POST' */\n method?: string;\n /** Maps VernLLMs internal chat-completion request into the providers raw request body */\n mapRequest: (params: ChatRequest) => unknown;\n /**\n * Maps the providers raw JSON response into `{ content, usage? }`\n * `content` is the assistants text (JSON string when JSON mode was requested)\n */\n mapResponse: (json: unknown) => {\n content: string;\n usage?: { promptTokens?: number; completionTokens?: number; totalTokens?: number };\n };\n}\n\n/**\n * A fetch-based escape hatch for providers with no SDK, or where pulling one\n * in isnt worth it. You supply the URL, headers, and two small mapping\n * functions; this handles the HTTP call and slots the result into the same\n * `LLMClient` shape every other adapter produces, so retries, timeouts,\n * the circuit breaker, and JSON/schema handling all still work unmodified\n *\n * Non-2xx responses throw an error with `.status` set to the HTTP status\n * code, so VernLLMs `nonRetryableStatus` handling (e.g. failing fast on\n * 401/403) applies here too\n */\nexport function fromFetch(config: FetchAdapterConfig): LLMClient {\n return {\n chat: {\n completions: {\n async create(params, options) {\n const url = typeof config.url === 'function' ? config.url(params) : config.url;\n const headers =\n typeof config.headers === 'function' ? await config.headers() : config.headers;\n\n const res = await fetch(url, {\n method: config.method ?? 'POST',\n headers: { 'Content-Type': 'application/json', ...headers },\n body: JSON.stringify(config.mapRequest(params)),\n signal: options.signal,\n });\n\n if (!res.ok) {\n const body = await res.text().catch(() => '');\n const err = new Error(\n `Fetch adapter request failed (${res.status}): ${body.slice(0, 500)}`,\n ) as Error & { status?: number };\n err.status = res.status;\n throw err;\n }\n\n const json = await res.json();\n const { content, usage } = config.mapResponse(json);\n\n return {\n choices: [{ message: { content } }],\n usage: usage\n ? {\n prompt_tokens: usage.promptTokens,\n completion_tokens: usage.completionTokens,\n total_tokens: usage.totalTokens,\n }\n : undefined,\n };\n },\n },\n },\n };\n}\n","import type { LLMClient } from '../types.js';\n\n/**\n * Passthrough adapter for any SDK/client whose `chat.completions.create`\n * already matches the OpenAI wire format 1:1 : this covers most hosted\n * inference providers, since \"OpenAI-compatible\" is a de facto standard for\n * chat completion APIs. No transformation happens here, this exists purely\n * so call sites read clearly (`fromMistral(client)` vs handing a Mistral\n * client to something typed for OpenAI) and so a real transformation could\n * be added later, per-provider, without a breaking change.\n *\n * Not every SDKs own TypeScript types line up exactly with `LLMClient`\n * (extra fields, stricter unions, etc.), so this takes `unknown` and casts:\n * the actual compatibility contract is the JSON each provider sends and\n * receives over the wire, not the SDKs TS types.\n */\nexport function fromOpenAICompatible(client: unknown): LLMClient {\n return client as LLMClient;\n}\n\n/** Groqs SDK matches the OpenAI wire format */\nexport const fromGroq = fromOpenAICompatible;\n\n/** Mistrals `chat.completions`-shaped client (or their OpenAI-compat endpoint) */\nexport const fromMistral = fromOpenAICompatible;\n\n/** DeepSeeks API is OpenAI-compatible */\nexport const fromDeepSeek = fromOpenAICompatible;\n\n/** Cerebras inference API is OpenAI-compatible */\nexport const fromCerebras = fromOpenAICompatible;\n\n/** Together AIs API is OpenAI-compatible */\nexport const fromTogether = fromOpenAICompatible;\n\n/** Fireworks AIs API is OpenAI-compatible */\nexport const fromFireworks = fromOpenAICompatible;\n\n/**\n * Ollama exposes an OpenAI-compatible endpoint at `/v1/chat/completions`\n * (as opposed to its native `/api/chat` format, which differs). Point an\n * OpenAI SDK instances `baseURL` at your Ollama server and pass it here:\n * this does not talk to Ollamas native API directly.\n */\nexport const fromOllama = fromOpenAICompatible;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AASA,IAAa,WAAb,cAA8B,MAAM;CAClC,YACEA,SACOC,MACAC,QACAC,QACP;AACA,QAAM,QAAQ;EA0MjB,KA9MU;EA8MT,KA7MS;EA6MR,KA5MQ;AAGP,OAAK,OAAO;CACb;AACF;AAED,SAAgB,WAAWC,KAA+B;AACxD,QAAO,eAAe;AACvB;;;;;AAWD,IAAa,uBAAb,MAA0E;CACxE,AAAQ,QAAQ,IAAI;CAEpB,MAAM,IAAIC,KAAgC;EACxC,MAAM,QAAQ,KAAK,MAAM,IAAI,IAAI;A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