vern-llm 2.4.1 → 2.4.2

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/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));const c=s(require(`crypto`)),l=new Set([`unknown_tool`,`duplicate_tool_call_id`,`tool_choice_none_violated`,`unexpected_tool_calls`]),u=new Set([`rate_limit_queue_full`,`rate_limit_queue_timeout`,`rate_limit_capacity_exceeded`]),d=new Set([`parse`,`validation`,`invalid_params`,`aborted`]);function f(e,t){return!(d.has(e)||t&&l.has(t)||t&&u.has(t))}function p(e){if(e!==void 0)try{return JSON.stringify(e),e}catch{return`[Unserializable: issues contained a circular reference]`}}function m(e){if(e!==void 0)try{return JSON.parse(JSON.stringify(e))}catch{return`[Unserializable: request body contained a circular reference]`}}const h=new Set([`authorization`,`x-api-key`,`x-goog-api-key`,`api-key`]);function g(e){if(e===void 0)return;let t={};for(let[n,r]of Object.entries(e))h.has(n.toLowerCase())||(t[n]=r);return t}const _=20;function v(e,t=0){if(e!==void 0)return t>=_?[]:e.map(e=>({...e,error:{...e.error,issues:p(e.error.issues),attempts:v(e.error.attempts,t+1)},request:e.request&&{...e.request,body:m(e.request.body),headers:g(e.request.headers)}}))}function y(e,t,n,r,i=Date.now()){return{provider:e,model:t,body:m(n),headers:g(r),startedAt:i}}var b=class extends Error{status;issues;cause;retryAfterMs;code;attempts;constructor(e,t,n={}){super(e),this.type=t,this.name=`LLMError`,this.status=n.status,this.issues=n.issues,this.cause=n.cause,this.retryAfterMs=n.retryAfterMs,this.code=n.code,this.attempts=n.attempts}get retryable(){return f(this.type,this.code)}toSnapshot(){return{message:this.message,type:this.type,status:this.status,issues:p(this.issues),retryAfterMs:this.retryAfterMs,code:this.code,retryable:this.retryable,attempts:v(this.attempts)}}toJSON(){return{name:this.name,message:this.message,type:this.type,status:this.status,issues:p(this.issues),retryAfterMs:this.retryAfterMs,code:this.code,retryable:this.retryable,attempts:v(this.attempts)}}};function x(e){return e instanceof b}function ee(e,t){return e.code===t&&e.issues!==void 0}var S=class{store=new Map;constructor(e=1e3){this.maxSize=e}async get(e){let t=this.store.get(e);return t?Date.now()>=t.expiresAt?(this.store.delete(e),{hit:!1,value:null}):{hit:!0,value:t.value}:{hit:!1,value:null}}async set(e,t,n){this.cleanupExpiredEntries(),this.store.set(e,{value:t,expiresAt:Date.now()+n*1e3}),this.enforceSizeLimit()}async delete(e){this.store.delete(e)}cleanupExpiredEntries(){let e=Date.now();for(let[t,n]of this.store)e>=n.expiresAt&&this.store.delete(t)}enforceSizeLimit(){for(;this.store.size>this.maxSize;){let e=this.store.keys().next().value;if(e===void 0)break;this.store.delete(e)}}},C=class{constructor(e=new S){this.inner=e}normalize(e){return e.toLowerCase().trim().replace(/[^\p{L}\p{N}\s]/gu,` `).replace(/\s+/g,` `).trim()}async resolveKey(e){return this.normalize(e)}async get(e){return this.inner.get(this.normalize(e))}async set(e,t,n){await this.inner.set(this.normalize(e),t,n)}async delete(e){await this.inner.delete?.(this.normalize(e))}},w=class{constructor(e,t,n){this.l1=e,this.l2=t,this.l1Ttl=n}async resolveKey(e){return this.l1.resolveKey?this.l1.resolveKey(e):this.l2.resolveKey?this.l2.resolveKey(e):e}async get(e){let t=await this.l1.get(e);if(t.hit)return t;let n=await this.l2.get(e);return n.hit&&await this.l1.set(e,n.value,this.l1Ttl??60),n}async set(e,t,n){await Promise.all([this.l1.set(e,t,this.l1Ttl??n),this.l2.set(e,t,n)])}async delete(e){await Promise.all([this.l1.delete?.(e),this.l2.delete?.(e)])}};function T(e){return typeof e==`object`&&!!e&&`type`in e&&e.type===`tool_calls`&&Array.isArray(e.toolCalls)}const te=new Set([`unknown_tool`,`duplicate_tool_call_id`,`tool_choice_none_violated`,`unexpected_tool_calls`]),ne=e=>e.type===`parse`||e.type===`validation`||e.type===`aborted`||e.type===`quota_exceeded`||e.code&&te.has(e.code)?`stop`:`next`;var E=class extends b{constructor(e){let t=e[e.length-1]?.error;super(`${e.length} provider${e.length===1?``:`s`} attempted and failed: ${e.map(e=>`${e.provider}(${e.error.type})`).join(` then `)}`,`fallback_exhausted`,{status:t?.status,cause:t,retryAfterMs:t?.retryAfterMs,code:`fallback_exhausted`,attempts:e}),this.attempts=e}get retryable(){let e=this.attempts[this.attempts.length-1]?.error;return e?e.retryable:super.retryable}};function re(e){return e instanceof E}async function ie(e,t,n){if(!e.reserveUsage)return!1;try{return await e.reserveUsage({coalesced:t,signal:n}),!0}catch(e){throw n?.aborted?new b(`LLM request aborted`,`aborted`):new b(e instanceof Error?e.message:`Usage reservation failed`,`quota_exceeded`,{cause:e})}}function ae(e,t,n,r){return async i=>{try{await e.refundUsage?.({coalesced:t,signal:n})}catch(e){r(i,e)}}}async function D(e,t,n,r,i){if(r?.aborted)throw new b(`LLM request aborted`,`aborted`);let a=await ie(e,t,r),o=ae(e,t,r,i);if(r?.aborted)throw a&&await o(`[VernLLM] refundUsage failed after abort`),new b(`LLM request aborted`,`aborted`);let s;try{s=await n()}catch(e){throw a&&await o(`[VernLLM] refundUsage failed`),e}if(r?.aborted)throw a&&await o(`[VernLLM] refundUsage failed after abort`),new b(`LLM request aborted`,`aborted`);return s}async function oe(e,t,n,r){if(n?.aborted)throw new b(`LLM request aborted`,`aborted`);let i=await ie(e,!1,n),a=ae(e,!1,n,r);if(n?.aborted)throw i&&await a(`[VernLLM] refundUsage failed after abort`),new b(`LLM request aborted`,`aborted`);let o;try{o=await t()}catch(e){throw i&&await a(`[VernLLM] refundUsage failed after stream-open failure`),e}let s=o.finalResult.then(e=>e,async e=>{throw i&&await a(`[VernLLM] refundUsage failed after stream error`),e});return s.catch(()=>{}),{chunks:o.chunks,finalResult:s}}function se(e){return typeof e==`string`?e:JSON.stringify(e)??``}function ce(e,t){let n=[];if(t){let t=e;t.type===`tool_calls`?(t.toolCalls.forEach((e,t)=>{n.push({type:`tool_call_delta`,index:t,id:e.id,name:e.name,argsDelta:JSON.stringify(e.arguments??{}),complete:!0})}),t.content&&n.push({type:`text-delta`,delta:t.content})):n.push({type:`text-delta`,delta:se(t.content)})}else n.push({type:`text-delta`,delta:se(e)});return{async*[Symbol.asyncIterator](){for(let e of n)yield e}}}function le(e,t){return{async*[Symbol.asyncIterator](){let n=await e;yield*ce(n,t)}}}var ue=class{inFlight=new Map;constructor(e,t){this.cache=e,this.logger=t}async resolveCacheKey(e){return this.cache.resolveKey?await this.cache.resolveKey(e):e}async deleteCache(e){if(this.cache.delete)try{await this.cache.delete(await this.resolveCacheKey(e))}catch(e){this.logger.warn(`[VernLLM] cache delete failed: ${e instanceof Error?e.message:`unknown`}`)}}logRefundError(e,t){this.logger.error(e,{message:t instanceof Error?t.message:`unknown`})}async getCached(e){try{return await this.cache.get(e)}catch(e){return this.logger.warn(`[VernLLM] cache read failed: ${e instanceof Error?e.message:`unknown`}`),{hit:!1}}}async runCached(e){let t=await this.resolveCacheKey(e.cacheKey),n=t===e.cacheKey?e:{...e,cacheKey:t},r=await this.getCached(t);if(r.hit)return r.value;let i=this.inFlight.get(t);return i?D(n,!0,()=>i,e.signal,(e,t)=>this.logRefundError(e,t)):this.registerTrigger(n)}registerTrigger(e){let t=D(e,!1,()=>this.runAndCache(e),e.signal,(e,t)=>this.logRefundError(e,t));return this.inFlight.set(e.cacheKey,t),t.catch(()=>{}).finally(()=>{this.inFlight.delete(e.cacheKey)}),t}async runAndCache(e){let t=await e.fn();try{await this.cache.set(e.cacheKey,t,e.ttl)}catch(e){this.logger.warn(`[VernLLM] cache write failed: ${e instanceof Error?e.message:`unknown`}`)}return t}async runCachedStream(e,t){let n=await this.resolveCacheKey(e.cacheKey),r=n===e.cacheKey?e:{...e,cacheKey:n},i=await this.getCached(n);if(i.hit){let e=i.value;return{chunks:ce(e,t),finalResult:Promise.resolve(e)}}let a=this.inFlight.get(n);if(a){let n=D(r,!0,()=>a,e.signal,(e,t)=>this.logRefundError(e,t));return n.catch(()=>{}),{chunks:le(n,t),finalResult:n}}return this.registerStreamTrigger(r)}registerStreamTrigger(e){let t,n,r=new Promise((e,r)=>{t=e,n=r});this.inFlight.set(e.cacheKey,r),r.catch(()=>{}).finally(()=>{this.inFlight.delete(e.cacheKey)});let i=oe(e,async()=>{let t=await e.openStream(),n=t.finalResult.then(async t=>{try{await this.cache.set(e.cacheKey,t,e.ttl)}catch(e){this.logger.warn(`[VernLLM] cache write failed: ${e instanceof Error?e.message:`unknown`}`)}return t},e=>{throw e});return{chunks:t.chunks,finalResult:n}},e.signal,(e,t)=>this.logRefundError(e,t));return i.then(e=>{e.finalResult.then(t,n)},e=>{n(e)}),i}};function de(){return{state:`closed`,consecutiveFailures:0,openedAt:0,trialInFlight:!1}}const O=``;var fe=class{threshold;cooldownMs;onStateChange;isolateByModel;sharedBucket=de();bucketsByModel=new Map;constructor(e={}){this.threshold=e.threshold??5,this.cooldownMs=e.cooldownMs??3e4,this.onStateChange=e.onStateChange,this.isolateByModel=e.isolateByModel??!1}lookupBucket(e){if(!this.isolateByModel)return this.sharedBucket;let t=e??O;return this.bucketsByModel.get(t)}ensureBucketFor(e){if(!this.isolateByModel)return this.sharedBucket;let t=e??O,n=this.bucketsByModel.get(t);return n||(n=de(),this.bucketsByModel.set(t,n)),n}transition(e,t,n){if(t===e.state)return;let r=e.state;e.state=t,this.onStateChange?.(r,t,e.consecutiveFailures,n)}assertClosed(e){let t=this.ensureBucketFor(e);if(t.state!==`closed`){if(t.state===`open`){let n=Date.now()-t.openedAt;if(n<this.cooldownMs)throw new b(`Circuit open, provider has failed ${t.consecutiveFailures} times in a row. Retry in ${Math.ceil((this.cooldownMs-n)/1e3)}s.`,`circuit_open`,{code:`circuit_cooling_down`});t.trialInFlight=!0,this.transition(t,`half-open`,e);return}if(t.trialInFlight)throw new b(`Circuit half-open. A trial request is already in flight. Try again shortly.`,`circuit_open`,{code:`circuit_trial_in_flight`});t.trialInFlight=!0}}recordSuccess(e){let t=this.lookupBucket(e);t&&(t.consecutiveFailures=0,t.trialInFlight=!1,this.transition(t,`closed`,e),this.isolateByModel&&t.state===`closed`&&t.consecutiveFailures===0&&this.bucketsByModel.delete(e??O))}recordFailure(e){let t=this.ensureBucketFor(e);if(t.consecutiveFailures+=1,t.trialInFlight=!1,t.state===`half-open`){t.openedAt=Date.now(),this.transition(t,`open`,e);return}t.consecutiveFailures>=this.threshold&&(t.openedAt=Date.now(),this.transition(t,`open`,e))}getState(e){return this.lookupBucket(e)?.state??`closed`}open(e){let t=this.ensureBucketFor(e);t.openedAt=Date.now(),t.trialInFlight=!1,this.transition(t,`open`,e)}close(e){let t=this.ensureBucketFor(e);t.consecutiveFailures=0,t.trialInFlight=!1,this.transition(t,`closed`,e),this.isolateByModel&&t.state===`closed`&&t.consecutiveFailures===0&&this.bucketsByModel.delete(e??O)}};function k(e,t){return n=>{if(e)try{e(n)}catch(e){t.error(`[VernLLM] onEvent failed`,{message:e instanceof Error?e.message:`unknown`})}}}function pe(e,t,n,r,i){if(!e)return;let a=typeof e==`object`?e:void 0,o=a?.onStateChange,s=k(r,i);return new fe({...a,onStateChange:(e,r,a,c)=>{if(s({kind:`circuit_state`,provider:t,model:c??n,from:e,to:r,consecutiveFailures:a}),o)try{o(e,r,a,c)}catch(e){i.error(`[VernLLM] circuitBreaker.onStateChange failed`,{message:e instanceof Error?e.message:`unknown`})}}})}const me=1e4,he=2147483647;function A(e){return!e||e<=0||e===1/0?void 0:e}function j(e){return Math.min(e,he)}async function M(e,t,n){let r=new AbortController,i=A(t),a=i===void 0?void 0:setTimeout(()=>{r.abort()},j(i)),o=n?AbortSignal.any([n,r.signal]):r.signal;try{return await e(o)}catch(e){throw r.signal.aborted&&!n?.aborted&&e instanceof DOMException&&e.name===`AbortError`?new b(`Request timed out`,`timeout`,{code:`request_timeout`}):e}finally{clearTimeout(a)}}function ge(e,t,n,r){let i=A(t);if(i===void 0)return e();let a=!1;return new Promise((t,o)=>{let s=setTimeout(()=>{a=!0,n?.(),o(new b(`No stream chunk received for ${i}ms (idle timeout)`,`timeout`,{code:`idle_timeout`}))},j(i));e().then(e=>{if(clearTimeout(s),a){r?.debug(`[VernLLM] chunk resolved after idle timeout already fired; discarding`);return}a=!0,t(e)},e=>{if(clearTimeout(s),a){r?.debug(`[VernLLM] chunk rejection arrived after idle timeout already fired; discarding`);return}a=!0,o(e)})})}function N(e,t=me){if(!e||typeof e!=`object`)return;let n=e,r=n.headers??n.response?.headers;if(!r||typeof r!=`object`)return;let i=r,a=typeof i.get==`function`?i.get(`Retry-After`):Object.entries(r).find(([e])=>e.toLowerCase()===`retry-after`)?.at(1);if(typeof a!=`string`||a.trim()===``)return;let o=a.trim();if(/^\d+$/.test(o))return Math.max(0,Math.min(Number(o)*1e3,t));let s=Date.parse(o);if(!Number.isNaN(s))return Math.max(0,Math.min(s-Date.now(),t))}function _e(e,t,n=me){let r=Math.min(e*2**t,n);return r/2+Math.random()*(r/2)}async function ve(e,t){if(t?.aborted)throw new b(`Operation aborted`,`aborted`);await new Promise((n,r)=>{let i=()=>{clearTimeout(a),r(new b(`Operation aborted`,`aborted`))},a=setTimeout(()=>{t?.removeEventListener(`abort`,i),n()},e);t?.addEventListener(`abort`,i,{once:!0})})}function P(e){if(!e||typeof e!=`object`)return;let t=e;if(typeof t.status==`number`)return t.status;if(typeof t.statusCode==`number`)return t.statusCode;if(typeof t.$metadata?.httpStatusCode==`number`)return t.$metadata.httpStatusCode}const F=new Set([`ECONNREFUSED`,`ENOTFOUND`,`ECONNRESET`,`ETIMEDOUT`,`EAI_AGAIN`,`EPIPE`,`ECONNABORTED`,`EHOSTUNREACH`,`ENETUNREACH`]),ye=new Set([`fetch failed`,`failed to fetch`,`load failed`,`networkerror when attempting to fetch resource.`]);function be(e){if(!e||typeof e!=`object`)return!1;let t=e;if(typeof t.code==`string`&&F.has(t.code)||typeof t.message==`string`&&ye.has(t.message.toLowerCase()))return!0;if(t.cause&&typeof t.cause==`object`){let e=t.cause;if(typeof e.code==`string`&&F.has(e.code))return!0}return!1}function I(e){try{return JSON.stringify(e,null,2)??String(e)}catch{try{return String(e)}catch{return`[unprintable error]`}}}function L(e){if(e&&typeof e==`object`)try{let t=e;if(t.error!==void 0)return I(t.error);if(typeof t.message==`string`)return t.message}catch{}return I(e)}function R(e){switch(e){case 429:return`provider_rate_limited`;case 401:return`authentication`;case 403:return`authorization`;case 404:return`not_found`;case 413:return`payload_too_large`;default:return e>=500?`server_error`:void 0}}const xe=/\(no body\)/i;function Se(e){return Object.keys(e).length===0}function Ce(e){if(e&&typeof e==`object`){let{error:t,message:n}=e;if(t!=null){let e=typeof t==`string`&&t.trim().length===0,n=typeof t==`object`&&Se(t);if(!e&&!n)return!1}if(typeof n==`string`){let e=n.trim();return e.length===0||xe.test(e)}return!0}return!0}function z(e,t,n){if(t?.aborted)return new b(`LLM request aborted`,`aborted`,{attempts:n});if(e instanceof b)return e.code===void 0&&e.status!==void 0&&(e.code=R(e.status)),e.attempts===void 0&&n!==void 0&&(e.attempts=n),e;let r=P(e),i=N(e);if(r!==void 0){let t=L(e),a=R(r),o=a===void 0,s=Ce(e)?o?`LLM request failed with status ${r} and no error detail from the provider. This usually means a field or value in the request isn't supported by the specific model (for example, a reasoning/thinking parameter the model doesn't accept), rather than a transport or auth problem.`:`LLM request failed with status ${r} and no error detail from the provider.`:`LLM request failed: ${t}`;return new b(s,`api`,{status:r,cause:e,retryAfterMs:i,code:a,attempts:n})}return be(e)?new b(`LLM request failed`,`network`,{cause:e,retryAfterMs:i,code:`connection_failed`,attempts:n}):new b(`LLM request failed`,`unknown`,{cause:e,retryAfterMs:i,attempts:n})}function we(e){try{return JSON.parse(e)}catch{return}}function Te(e){return e.map(e=>({type:`function`,function:{name:e.name,description:e.description,parameters:e.parameters}}))}function Ee(e){return e.map(e=>({id:e.id,type:`function`,function:{name:e.name,arguments:JSON.stringify(e.arguments??{})}}))}function De(e){return e.map(e=>{let t;try{t=e.function.arguments.trim()?JSON.parse(e.function.arguments):{}}catch{throw new b(`Invalid JSON arguments for tool call "${e.function.name}"`,`parse`,{code:`tool_arguments_parse_failed`})}return{id:e.id,name:e.function.name,arguments:t}})}function B(e){return typeof e==`string`?e:JSON.stringify(e)}var Oe=class{model;defaultMaxTokens;defaultTemperature;defaultReasoningEffort;defaultBudgetTokens;supportsJsonObjectMode;constructor(e){this.model=e.model,this.defaultMaxTokens=e.defaultMaxTokens,this.defaultTemperature=e.defaultTemperature,this.defaultReasoningEffort=e.defaultReasoningEffort,this.defaultBudgetTokens=e.defaultBudgetTokens,this.supportsJsonObjectMode=e.supportsJsonObjectMode}build(e){let{systemPrompt:t,userContent:n,history:r=[],maxTokens:i=this.defaultMaxTokens,model:a=this.model,jsonSchema:o,tools:s,toolChoice:c}=e,l=e.temperature===void 0?this.defaultTemperature:e.temperature,u=e.reasoningEffort===void 0?this.defaultReasoningEffort:e.reasoningEffort,d=e.budgetTokens===void 0?this.defaultBudgetTokens:e.budgetTokens;if(s&&s.length===0)throw new b("`tools` was an empty array. This is almost always a bug (e.g. a filtered tool list that ended up empty). An empty `tools` array still switches on tool-call mode (response shape, jsonMode default, wire format) with nothing for the model to call. Omit `tools` entirely for a normal call, or make sure the array is non-empty.",`invalid_params`);if(s){let e=new Set,t=new Set;for(let n of s)e.has(n.name)&&t.add(n.name),e.add(n.name);if(t.size)throw new b(`\`tools\` has duplicate name(s): [${[...t].join(`, `)}]. Tool names must be unique.`,`invalid_params`,{code:`duplicate_tool_names`,issues:{names:[...t]}})}if(c&&!s)throw new b("`toolChoice` was set without `tools`. There is nothing for it to choose between. Set `tools`, or remove `toolChoice`.",`invalid_params`);if(s&&typeof c==`object`&&!s.some(e=>e.name===c.name))throw new b(`toolChoice names "${c.name}", which is not in \`tools\` ([${s.map(e=>e.name).join(`, `)}]).`,`invalid_params`,{code:`unknown_tool_choice`,issues:{requested:c.name,available:s.map(e=>e.name)}});let f=e.jsonMode,p=f??!s;if(!this.supportsJsonObjectMode&&!o&&f===!0)throw new b('jsonMode: true was set explicitly, but this client does not support `response_format: "json_object"` (see LLMClient.supportsJsonObjectMode). Neither Anthropic nor Bedrock has a field that mechanically guarantees JSON output for this mode. Use `jsonSchema` instead, which maps to a real constraint on both.',`invalid_params`);if(!this.supportsJsonObjectMode&&!o&&f===void 0&&e.schema)throw new b('`schema` was provided, which requires JSON output to validate against, but this client does not support `response_format: "json_object"` (see LLMClient.supportsJsonObjectMode) and no `jsonSchema` was set. Neither Anthropic nor Bedrock has a field that mechanically guarantees JSON output without one. Use `jsonSchema` instead, which maps to a real constraint on both and still runs `schema` against its parsed result.',`invalid_params`);let m=!this.supportsJsonObjectMode&&!o&&f===void 0?!1:p,h=m||!!o;if(e.schema&&!h)throw new b(`schema was provided but jsonMode: false disables JSON parsing, so nothing would validate it. Remove jsonMode: false, set jsonSchema, or remove schema.`,`invalid_params`);let g=this.buildResponseFormat(o,h);this.validateHistory(r);let _={model:a,...l===null?{}:{temperature:l},max_tokens:i,...g?{response_format:g}:{},...u?{reasoning_effort:u}:{},...d==null?{}:{budget_tokens:d},...s?{tools:Te(s)}:{},...s?{tool_choice:this.buildWireToolChoice(c)}:{},messages:[...t?[{role:`system`,content:t}]:[],...r.flatMap(e=>this.turnToWireMessages(e)),{role:`user`,content:n}]};return{useJson:h,model:a,request:_}}validateHistory(e){let t;for(let[n,r]of e.entries()){if(r.role===`tool`){if(t?.role!==`assistant`||!t.toolCalls?.length)throw new b(`history[${n}] is a "tool" turn, but must immediately follow an "assistant" turn that requested tools`,`invalid_params`);if(!r.toolResults?.length)throw new b(`history[${n}] is a "tool" turn but has no toolResults`,`invalid_params`);let e=new Set(t.toolCalls.map(e=>e.id)),i=r.toolResults.map(e=>e.toolCallId),a=i.filter(t=>!e.has(t));if(a.length)throw new b(`history[${n}].toolResults references unknown toolCallId(s) [${a.join(`, `)}]`,`invalid_params`,{code:`unknown_tool_result_ids`,issues:{historyIndex:n,ids:a}});let o=new Set,s=new Set;for(let e of i)o.has(e)&&s.add(e),o.add(e);if(s.size)throw new b(`history[${n}].toolResults has duplicate toolCallId(s) [${[...s].join(`, `)}]`,`invalid_params`,{code:`duplicate_tool_result_ids`,issues:{historyIndex:n,ids:[...s]}});let c=[...e].filter(e=>!i.includes(e));if(c.length)throw new b(`history[${n}] is missing toolResults for toolCallId(s) [${c.join(`, `)}]`,`invalid_params`,{code:`missing_tool_results`,issues:{historyIndex:n,ids:c}})}else{if(r.role===t?.role)throw new b(`history must alternate user/assistant turns: consecutive "${r.role}" turns at history[${n-1}] and history[${n}]`,`invalid_params`);if(t?.role===`assistant`&&t.toolCalls?.length)throw new b(`history[${n}] follows an assistant tool request without tool results`,`invalid_params`)}t=r}if(t?.role===`assistant`&&t.toolCalls?.length)throw new b(`The last entry in history is an assistant tool request without tool results`,`invalid_params`);if(t?.role===`user`)throw new b(`The last entry in history is a "user" turn, which would collide with the current userContent turn.`,`invalid_params`)}buildWireToolChoice(e){return!e||e===`auto`?`auto`:e===`none`||e===`required`?e:{type:`function`,function:{name:e.name}}}turnToWireMessages(e){return e.role===`tool`?(e.toolResults??[]).map(e=>({role:`tool`,tool_call_id:e.toolCallId,content:typeof e.content==`string`?e.content:JSON.stringify(e.content??null),...e.isError?{is_error:!0}:{}})):e.role===`assistant`&&e.toolCalls?.length?[{role:`assistant`,...e.content===void 0?{}:{content:B(e.content)},tool_calls:Ee(e.toolCalls)}]:e.role===`assistant`?[{role:`assistant`,content:B(e.content===void 0?``:e.content)}]:[{role:e.role,content:e.content??``}]}buildResponseFormat(e,t){return e?{type:`json_schema`,json_schema:{name:e.name,schema:e.schema,strict:e.strict??!0,description:e.description}}:t?{type:`json_object`}:void 0}};function ke(e,t,n){let{requestId:r,model:i,providerName:a,isFallback:o,chunkIdleTimeoutMs:s,streamController:c,logger:l,signal:u}=n,d,f,p=new Promise((e,t)=>{d=e,f=t});p.catch(()=>{});let m=1e4,h=[],g=[],_=!1,v,y=!1,b=e=>{let t=g.shift();if(t){t.resolve({done:!1,value:e});return}h.push(e),h.length>m*2&&(y||(y=!0,l.warn(`[VernLLM] stream chunk buffer exceeded cap (${m}), evicting ${h.length-m} oldest chunk(s); buffered=${h.length}. The chunks iterable was never read (or fell far behind) for this stream.`)),h.splice(0,h.length-m))},x=()=>{_=!0;for(let e of g.splice(0))e.resolve({done:!0,value:void 0})},ee=e=>{_=!0,v=e;for(let t of g.splice(0))t.reject(e)},S={[Symbol.asyncIterator](){return{next(){return h.length?Promise.resolve({done:!1,value:h.shift()}):_?v?Promise.reject(v):Promise.resolve({done:!0,value:void 0}):new Promise((e,t)=>{g.push({resolve:e,reject:t})})}}}},C=new Map,w=``,T;return(async()=>{try{let n=t;for(;!n.done;){let t=n.value;if(t.type!==`ping`){if(t.type===`text-delta`)w+=t.delta,b({type:`text-delta`,delta:t.delta});else if(t.type===`tool_call_delta`){let e=C.get(t.index)??{args:``};e.id??=t.id,e.name??=t.name,e.args+=t.argumentsDelta??``,C.set(t.index,e),b({type:`tool_call_delta`,index:t.index,id:t.id,name:t.name,argsDelta:t.argumentsDelta,complete:t.complete})}else if(t.type===`usage`){let e=t.usage.completion_tokens_details?.reasoning_tokens;T={promptTokens:t.usage.prompt_tokens??0,completionTokens:t.usage.completion_tokens??0,totalTokens:t.usage.total_tokens??0,...e===void 0?{}:{reasoningTokens:e},requestId:r,model:i,provider:a,usedFallback:o},b({type:`usage`,usage:T})}}n=await ge(()=>e.next(),s,()=>c.abort(),l)}}catch(t){try{await e.return?.()}catch{}c.abort();let r=z(t,u);try{n.onStreamFailure(r,T)}catch{}ee(r),f(r);return}x();try{n.onStreamSuccess(T)}catch{}try{let e=C.size?[...C.entries()].sort(([e],[t])=>e-t).map(([,e])=>({id:e.id??``,type:`function`,function:{name:e.name??``,arguments:e.args}})):void 0,t=n.finalize(w,e,T);d(t)}catch(e){f(e)}})(),{chunks:S,finalResult:p}}function Ae(e){return e}var je=class{maxRetries;timeoutMs;chunkIdleTimeoutMs;baseDelayMs;nonRetryableStatus;parseJson;logger;redact;onUsage;onUsageFailure;reportEvent;breaker;limiter;isFallback;requestBuilder;constructor(e,t,n,r){this.providerName=e,this.client=t,this.model=n,this.maxRetries=r.maxRetries,this.timeoutMs=r.timeoutMs,this.chunkIdleTimeoutMs=r.chunkIdleTimeoutMs,this.baseDelayMs=r.baseDelayMs,this.nonRetryableStatus=r.nonRetryableStatus,this.parseJson=r.parseJson??we,this.logger=r.logger,this.redact=r.redact,this.onUsage=r.onUsage,this.onUsageFailure=r.onUsageFailure,this.reportEvent=k(r.onEvent,this.logger),this.breaker=r.breaker,this.limiter=r.limiter,this.isFallback=r.isFallback??!1,this.requestBuilder=new Oe({model:n,defaultMaxTokens:r.defaultMaxTokens,defaultTemperature:r.defaultTemperature,defaultReasoningEffort:r.defaultReasoningEffort,defaultBudgetTokens:r.defaultBudgetTokens,supportsJsonObjectMode:t.supportsJsonObjectMode??!0})}getCircuitState(e){return this.breaker?.getState(e)}get isolateByModel(){return this.breaker?.isolateByModel??!1}openCircuit(e){this.breaker?.open(e)}closeCircuit(e){this.breaker?.close(e)}assertBreakerClosed(e){this.breaker?.assertClosed(e??this.model)}async run(e,t,n){let r=e.model??this.model,i=[];try{return await this.retryWithBackoff((n,r)=>this.executeCall(e,t,n,r),t,r,e.signal,n,i)}catch(n){let a=z(n,e.signal,i.length>0?i:void 0);throw this.countsTowardBreaker(a)&&this.breaker?.recordFailure(r),this.logger.debug(`[VernLLM:${t}] error:\n${this.redactText(L(n))}`),a}}async runStream(e,t,n){let r=e.model??this.model,i=[];try{return await this.retryWithBackoff((n,r)=>this.executeStreamCall(e,t,n,r),t,r,e.signal,n,i)}catch(n){let a=z(n,e.signal,i.length>0?i:void 0);throw this.countsTowardBreaker(a)&&this.breaker?.recordFailure(r),this.logger.debug(`[VernLLM:${t}] stream-open error:\n${this.redactText(L(n))}`),a}}async executeCall(e,t,n,r){let{useJson:i,model:a,request:o}=this.requestBuilder.build(e);r?.(y(this.providerName,a,o,void 0,Date.now()));let s;if(this.limiter){let n=await this.limiter.acquire(this.limiter.estimate(o),e.signal);s=n.release,n.waitedMs>0&&this.reportEvent({kind:`rate_limited`,requestId:t,provider:this.providerName,model:a,waitedMs:n.waitedMs,reason:n.reason??`rpm`})}try{let r=await M(e=>this.client.chat.completions.create(o,{signal:e}),this.timeoutMs,e.signal),c=this.extractUsage(r,t,a);s?.(this.actualTokensFor(c)),s=void 0;let l=r.choices?.[0]?.message?.content,u=r.choices?.[0]?.message?.tool_calls;return this.finalizeResponse(l,u,e,i,a,c,t,n)}finally{s?.()}}redactText(e){return this.redact?this.redact(e):e}redactedOutput(e,t){return this.redactText(e??`[${t?.length??0} tool call(s)]`)}finalizeResponse(e,t,n,r,i,a,o,s){try{let s=e?.trim();if(!s&&!t?.length)throw new b(`Empty LLM response`,`api`,{code:`empty_response`});if(this.logger.debug(`[VernLLM:${o}] output:\n${this.redactedOutput(s,t).slice(0,800)}`),t?.length){if(!n.tools)throw new b("Provider returned tool_calls but no `tools` were sent with this call.",`validation`,{code:`unexpected_tool_calls`});if(n.toolChoice===`none`)throw new b(`Provider returned tool_calls despite toolChoice: 'none'.`,`validation`,{code:`tool_choice_none_violated`});let e=De(t);return this.validateToolCallArguments(e,n.tools),this.breaker?.recordSuccess(i),this.reportUsage(a),{type:`tool_calls`,toolCalls:e,...s?{content:s}:{}}}let c=s??``;if(!r)return this.breaker?.recordSuccess(i),this.reportUsage(a),n.tools?{type:`content`,content:c}:c;let l=this.parseAndValidate(c,n.schema);return this.breaker?.recordSuccess(i),this.reportUsage(a),n.tools?{type:`content`,content:l}:l}catch(e){let t=z(e,n.signal);throw a&&t.type!==`aborted`&&this.reportUsageFailure(a,t,s),t}}async executeStreamCall(e,t,n,r){let{useJson:i,model:a,request:o}=this.requestBuilder.build(e);r?.(y(this.providerName,a,o,void 0,Date.now()));let s=this.client.chat.completions;if(!s.createStream)throw new b(`stream: true requires a client/adapter with createStream`,`invalid_params`,{code:`unsupported_capability`,issues:{capability:`createStream`}});let c=s.createStream.bind(s),l;if(this.limiter){let n=await this.limiter.acquire(this.limiter.estimate(o),e.signal);l=n.release,n.waitedMs>0&&this.reportEvent({kind:`rate_limited`,requestId:t,provider:this.providerName,model:a,waitedMs:n.waitedMs,reason:n.reason??`rpm`})}let u=new AbortController,d=e.signal?AbortSignal.any([e.signal,u.signal]):u.signal;try{let{iterator:r,first:s}=await M(async e=>{let t=c(o,{signal:e})[Symbol.asyncIterator](),n=await t.next();return{iterator:t,first:n}},this.timeoutMs,d);if(s.done)throw new b(`Empty LLM response`,`api`);let f=l,p=ke(r,s,{requestId:t,model:a,providerName:this.providerName,isFallback:this.isFallback,chunkIdleTimeoutMs:e.chunkIdleTimeoutMs??this.chunkIdleTimeoutMs,streamController:u,logger:this.logger,signal:e.signal,onStreamSuccess:e=>{this.breaker?.recordSuccess(a),f?.(this.actualTokensFor(e))},onStreamFailure:(e,t)=>{e.type===`timeout`&&this.breaker?.recordFailure(a),t&&e.type!==`aborted`&&this.reportUsageFailure(t,e,n,!0),f?.(this.actualTokensFor(t))},finalize:(r,o,s)=>this.finalizeResponse(r,o,e,i,a,s,t,n)});return l=void 0,p}finally{l?.()}}validateToolCallArguments(e,t){let n=new Map(t.map(e=>[e.name,e])),r=new Set,i=[];for(let t of e)r.has(t.id)&&i.push({name:t.name,toolCallId:t.id,code:`duplicate_tool_call_id`}),r.add(t.id),n.has(t.name)||i.push({name:t.name,toolCallId:t.id,code:`unknown_tool`});if(i.length>0){let e=i.find(e=>e.code===`unknown_tool`),t=e?`Model requested tool "${e.name}", which was not in the tools offered ([${[...n.keys()].join(`, `)}]).`:`Duplicate tool call id "${i[0].toolCallId}" in the model's response.`,r=i.length>1?`${t} (${i.length} tool call issues total, see error.issues.)`:t;throw new b(r,`validation`,{code:e?`unknown_tool`:`duplicate_tool_call_id`,issues:i})}for(let t of e){let e=n.get(t.name);if(!e?.argumentsSchema)continue;let r=e.argumentsSchema.safeParse(t.arguments);if(!r.success)throw new b(`Arguments for tool call "${t.name}" failed validation`,`validation`,{issues:r.error})}}async retryWithBackoff(e,t,n,r,i,a){let o,s;for(let c=0;c<=this.maxRetries;c++){s=void 0;try{return c>0&&await this.recoverDelay(t,n,c,o,r),i?.(),await e(c,e=>{s=e})}catch(e){o=e;let t=c<this.maxRetries&&this.shouldRetry(e,r);if(!t)break;a?.push({index:c,error:z(e,r).toSnapshot(),request:Ae(s)})}}throw o}extractUsage(e,t,n){if(!e.usage)return;let r=e.usage.completion_tokens_details?.reasoning_tokens;return{promptTokens:e.usage.prompt_tokens??0,completionTokens:e.usage.completion_tokens??0,totalTokens:e.usage.total_tokens??0,...r===void 0?{}:{reasoningTokens:r},requestId:t,model:n,provider:this.providerName,usedFallback:this.isFallback}}actualTokensFor(e){if(e)return e.totalTokens||e.promptTokens+e.completionTokens}reportUsage(e){if(!(!e||!this.onUsage))try{this.onUsage(e)}catch(e){this.logger.error(`[VernLLM] onUsage failed`,{message:e instanceof Error?e.message:`unknown`})}}reportUsageFailure(e,t,n,r=!1){let i=e.totalTokens||e.promptTokens+e.completionTokens,a=r?`mid-stream failure (terminal, no further attempts)`:`attempt ${n+1}/${this.maxRetries+1}`;if(this.logger.warn(`[VernLLM:${e.requestId}] usage failure, ${a}: type=${t.type} tokens=${i}`),this.onUsageFailure)try{this.onUsageFailure(e,t)}catch(e){this.logger.error(`[VernLLM] onUsageFailure failed`,{message:e instanceof Error?e.message:`unknown`})}}parseAndValidate(e,t){let n;try{n=this.parseJson(e)}catch{throw new b(`Invalid JSON response`,`parse`)}if(n==null)throw new b(`Invalid JSON response`,`parse`);if(!t)return n;let r=t.safeParse(n);if(!r.success)throw new b(`Schema validation failed`,`validation`,{issues:r.error});return r.data}async recoverDelay(e,t,n,r,i){let a=N(r),o=a??_e(this.baseDelayMs,n),s=a!==void 0;this.logger.warn(`[VernLLM:${e}] recovery attempt ${n}/${this.maxRetries}, waiting ${Math.ceil(o)}ms`+(s?` (honoring Retry-After)`:``)),this.reportEvent({kind:`retry`,requestId:e,provider:this.providerName,model:t,attempt:n,maxRetries:this.maxRetries,delayMs:o,retryAfterHonored:s,error:z(r,i)}),await ve(o,i)}shouldRetry(e,t){if(t?.aborted||e instanceof b&&!e.retryable)return!1;let n=P(e);return!(n!==void 0&&this.nonRetryableStatus.includes(n))}countsTowardBreaker(e){return e.retryable}};function Me(e){return{debug:V(e,`debug`),warn:V(e,`warn`),error:V(e,`error`)}}function V(e,t){let n=e[t].bind(e);return(...e)=>{try{Pe(n(...e))}catch{}}}function Ne(e){return typeof e?.then==`function`}function Pe(e){Ne(e)&&Promise.resolve(e).catch(()=>{})}var Fe=class{constructor(e){this.debugEnabled=e}debug(e){this.debugEnabled&&console.debug(e)}warn(e){console.warn(e)}error(e,t){console.error(e,t??``)}};function Ie(e){let t=e.messages.reduce((e,t)=>{let n=t.content;if(typeof n==`string`)return e+n.length;if(n==null)return e;try{return e+JSON.stringify(n).length}catch{return e}},0);return Math.ceil(t/4)+(e.max_tokens??0)}var H=class{available;lastRefill=Date.now();constructor(e,t){this.capacity=e,this.refillPerMs=t,this.available=e}refill(){if(this.refillPerMs===0)return;let e=Date.now(),t=e-this.lastRefill;this.available=Math.min(this.capacity,this.available+Math.max(0,t)*this.refillPerMs),this.lastRefill=e}tryTake(e){return this.refill(),this.available<e?!1:(this.available-=e,!0)}msUntilAvailable(e){return this.refill(),this.available>=e?0:this.refillPerMs===0?1/0:(e-this.available)/this.refillPerMs}give(e){this.available=Math.min(this.capacity,this.available+e)}getCapacity(){return this.capacity}};const Le=2147483647;var Re=class{requests;tokens;concurrency;maxQueueMs;maxQueueSize;estimateTokensFn;queue=[];wakeTimer;constructor(e){e.requestsPerMinute&&(this.requests=new H(e.requestsPerMinute,e.requestsPerMinute/6e4)),e.tokensPerMinute&&(this.tokens=new H(e.tokensPerMinute,e.tokensPerMinute/6e4)),e.maxConcurrent&&(this.concurrency=new H(e.maxConcurrent,0)),this.maxQueueMs=e.maxQueueMs??3e4,this.maxQueueSize=e.maxQueueSize??0,this.estimateTokensFn=e.estimateTokens??Ie}estimate(e){return this.estimateTokensFn(e)}async acquire(e,t){if(t?.aborted)throw new b(`LLM request aborted`,`aborted`);if(!Number.isFinite(e)||e<0)throw new b(`estimatedTokens must be a finite, non-negative number, got ${String(e)}`,`invalid_params`);if(this.tokens&&e>this.tokens.getCapacity())throw new b(`estimatedTokens (${e}) exceeds the configured tokensPerMinute capacity (${this.tokens.getCapacity()}); this call could never acquire capacity.`,`rate_limited`,{code:`rate_limit_capacity_exceeded`});if(this.queue.length===0){let n=this.tryAcquireBuckets(e);return n.ok?{release:this.makeRelease(e),waitedMs:0}:this.enqueue(e,n.reason,t)}if(this.maxQueueSize>0&&this.queue.length>=this.maxQueueSize)throw this.queueFullError();return this.enqueue(e,void 0,t)}queueFullError(){return new b(`Rate limit queue is full`,`rate_limited`,{code:`rate_limit_queue_full`})}enqueue(e,t,n){return new Promise((r,i)=>{let a={estimatedTokens:e,enqueuedAt:Date.now(),lastReason:t,resolve:e=>{c(),r(e)},reject:e=>{c(),this.wakeTimer&&(clearTimeout(this.wakeTimer),this.wakeTimer=void 0),this.drain(),i(e)}},o,s=()=>{a.reject(new b(`LLM request aborted`,`aborted`))},c=()=>{o&&clearTimeout(o),n?.removeEventListener(`abort`,s);let e=this.queue.indexOf(a);e!==-1&&this.queue.splice(e,1)};this.maxQueueMs>0&&(o=setTimeout(()=>{a.reject(new b(`Rate limit queue timed out before capacity was available`,`rate_limited`,{code:`rate_limit_queue_timeout`}))},this.maxQueueMs)),n?.addEventListener(`abort`,s,{once:!0}),this.queue.push(a),this.drain()})}tryAcquireBuckets(e){let t=[],n=(e,n)=>e?e.tryTake(n)?(t.push({bucket:e,amount:n}),!0):!1:!0;if(!n(this.concurrency,1))return{ok:!1,reason:`concurrency`};if(!n(this.requests,1)){for(let e of t)e.bucket.give(e.amount);return{ok:!1,reason:`rpm`}}if(!n(this.tokens,e)){for(let e of t)e.bucket.give(e.amount);return{ok:!1,reason:`tpm`}}return{ok:!0}}drain(){for(;this.queue.length>0;){let e=this.queue[0],t=this.tryAcquireBuckets(e.estimatedTokens);if(!t.ok){e.lastReason=t.reason,this.scheduleWake(t.reason,e.estimatedTokens);return}let n=Date.now()-e.enqueuedAt;e.resolve({release:this.makeRelease(e.estimatedTokens),waitedMs:n,reason:e.lastReason})}}scheduleWake(e,t){if(this.wakeTimer)return;let n=e===`rpm`?this.requests?.msUntilAvailable(1):e===`tpm`?this.tokens?.msUntilAvailable(t):void 0;if(n===void 0||!Number.isFinite(n))return;let r=Math.min(Math.max(1,Math.ceil(n)),Le);this.wakeTimer=setTimeout(()=>{this.wakeTimer=void 0,this.drain()},r)}makeRelease(e){let t=!1;return n=>{t||(t=!0,this.concurrency?.give(1),this.tokens&&n!==void 0&&Number.isFinite(n)&&this.tokens.give(e-n),this.drain())}}},ze=class{logger;executors;fallbackOn;reportEvent;cacheOrchestrator;constructor(e){this.logger=Me(e.logger??new Fe(e.debug??!1));let t=e.name??`primary`;this.cacheOrchestrator=new ue(e.cache??new S,this.logger),this.fallbackOn=e.fallbackOn??ne,this.reportEvent=k(e.onEvent,this.logger);let n=e.defaultTemperature===void 0?.2:e.defaultTemperature,r=e.defaultReasoningEffort,i=e.defaultBudgetTokens,a={client:e.client,model:e.model,name:t,maxRetries:e.maxRetries,timeoutMs:e.timeoutMs,chunkIdleTimeoutMs:e.chunkIdleTimeoutMs,baseDelayMs:e.baseDelayMs,defaultMaxTokens:e.defaultMaxTokens,defaultTemperature:n,defaultReasoningEffort:r,defaultBudgetTokens:i,nonRetryableStatus:e.nonRetryableStatus,circuitBreaker:e.circuitBreaker,rateLimit:e.rateLimit},o=Array.isArray(e.fallback)?e.fallback:e.fallback?[e.fallback]:[],s=[a,...o];this.executors=s.map((a,o)=>{let s=o>0,c=a.name??(s?`fallback[${o-1}]`:t),l=pe(a.circuitBreaker,c,a.model,e.onEvent,this.logger);return new je(c,a.client,a.model,{maxRetries:a.maxRetries??e.maxRetries??1,timeoutMs:a.timeoutMs??e.timeoutMs??25e3,chunkIdleTimeoutMs:a.chunkIdleTimeoutMs??e.chunkIdleTimeoutMs??3e4,baseDelayMs:a.baseDelayMs??e.baseDelayMs??500,defaultMaxTokens:a.defaultMaxTokens??e.defaultMaxTokens??1e3,defaultTemperature:a.defaultTemperature===void 0?n:a.defaultTemperature,defaultReasoningEffort:a.defaultReasoningEffort===void 0?r:a.defaultReasoningEffort,defaultBudgetTokens:a.defaultBudgetTokens===void 0?i:a.defaultBudgetTokens,nonRetryableStatus:a.nonRetryableStatus??e.nonRetryableStatus??[400,401,403,404,422],parseJson:e.parseJson,logger:this.logger,redact:e.redact,onUsage:e.onUsage,onUsageFailure:e.onUsageFailure,onEvent:e.onEvent,breaker:l,limiter:a.rateLimit?new Re(a.rateLimit):void 0,isFallback:s})})}logRefundError(e,t){this.logger.error(e,{message:t instanceof Error?t.message:`unknown`})}async runFallbackChain(e,t,n,r=!1){let i=[];for(let a=0;a<this.executors.length;a++){let o=this.executors[a],s=Date.now(),c=0;try{a===0&&r||o.assertBreakerClosed(e.model);let t=await n(o,()=>{c+=1});return{result:t,executor:o,index:a,attemptCount:c}}catch(n){let r=z(n,e.signal);i.push({index:a-1,provider:o.providerName,model:e.model??o.model,error:r.toSnapshot()});let c=a===this.executors.length-1,l=this.fallbackOn(r,{isLastTarget:c}),u=c?`stop`:l;if(u===`stop`)throw i.length>1?new E(i):r;let d=this.executors[a+1];this.reportEvent({kind:`fallback`,requestId:t,from:o.providerName,to:d.providerName,fromIndex:a-1,toIndex:a,error:r,elapsedMs:Date.now()-s})}}throw new b(`No provider targets configured`,`invalid_params`)}async call(e){if(e.signal?.aborted)throw new b(`LLM request aborted`,`aborted`);let t=e.requestId??(0,c.randomUUID)(),n=this.executors.length===1;return n&&this.executors[0].assertBreakerClosed(e.model),e.stream?oe(e,async()=>{let{result:r}=await this.runFallbackChain(e,t,(n,r)=>n.runStream(e,t,r),n);return r},e.signal,(e,t)=>this.logRefundError(e,t)):D(e,!1,async()=>{let{result:r,executor:i,index:a,attemptCount:o}=await this.runFallbackChain(e,t,(n,r)=>n.run(e,t,r),n);return e.meta&&(e.meta.current={provider:i.providerName,model:e.model??i.model,fallbackIndex:a-1,usedFallback:a>0,attempts:o}),r},e.signal,(e,t)=>this.logRefundError(e,t))}runCached(e){return this.cacheOrchestrator.runCached(e)}async deleteCache(e){await this.cacheOrchestrator.deleteCache(e)}async cachedCall(e){let{call:t,...n}=e,r=t;if(r.reserveUsage||r.refundUsage)throw new b("`reserveUsage`/`refundUsage` were set inside `call`, where cachedCall ignores them. Move them to the top level of the cachedCall() params, alongside cacheKey/ttl, instead.",`invalid_params`);if(r.stream){let e=r;return this.cacheOrchestrator.runCachedStream({...n,openStream:()=>this.call(e)},!!r.tools)}return this.runCached({...n,fn:()=>this.call(r)})}getCircuitState(e){let t=this.resolveExecutor(e?.index??0,`getCircuitState`);return this.warnIfModelUnsupported(t.isolateByModel,e?.model,`getCircuitState`),t.getCircuitState(e?.model??t.model)}getCircuitStates(e){return this.executors.map((t,n)=>({provider:t.providerName,index:n,isFallback:n>0,isolateByModel:t.isolateByModel,state:t.getCircuitState(e??t.model)}))}openCircuit(e){let t=this.resolveExecutor(e?.index??0,`openCircuit`);this.warnIfModelUnsupported(t.isolateByModel,e?.model,`openCircuit`),t.openCircuit(e?.model??t.model)}closeCircuit(e){let t=this.resolveExecutor(e?.index??0,`closeCircuit`);this.warnIfModelUnsupported(t.isolateByModel,e?.model,`closeCircuit`),t.closeCircuit(e?.model??t.model)}resolveExecutor(e,t){let n=this.executors[e];if(!n)throw RangeError(`${t}: no target at index ${e} (chain has ${this.executors.length} target${this.executors.length===1?``:`s`})`);return n}warnIfModelUnsupported(e,t,n){t!==void 0&&!e&&this.logger.warn(`[VernLLM] ${n}: \`model: '${t}'\` has no effect here. This target's circuitBreaker doesn't have isolateByModel on, so it only tracks one shared circuit regardless of \`model\`. Omit \`model\`, or set \`circuitBreaker.isolateByModel: true\` on this target if per-model tracking is what you want.`)}};function Be(e){return e}function Ve(e){return e}async function*He(e){let t=new TextDecoder(`utf-8`,{fatal:!0}),n=``;for await(let r of e){let e;try{e=typeof r==`string`?r:t.decode(r,{stream:!0})}catch(e){throw new b(`Invalid UTF-8 in SSE stream`,`parse`,{cause:e})}n=(n+e).replace(/\r\n/g,`
1
+ "use strict";var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));const c=s(require(`crypto`)),l=new Set([`unknown_tool`,`duplicate_tool_call_id`,`tool_choice_none_violated`,`unexpected_tool_calls`]),u=new Set([`rate_limit_queue_full`,`rate_limit_queue_timeout`,`rate_limit_capacity_exceeded`]),d=new Set([`parse`,`validation`,`invalid_params`,`aborted`]);function f(e,t){return!(d.has(e)||t&&l.has(t)||t&&u.has(t))}function p(e){if(e!==void 0)try{return JSON.stringify(e),e}catch{return`[Unserializable: issues contained a circular reference]`}}function m(e){if(e!==void 0)try{return JSON.parse(JSON.stringify(e))}catch{return`[Unserializable: request body contained a circular reference]`}}const h=new Set([`authorization`,`x-api-key`,`x-goog-api-key`,`api-key`]);function g(e){if(e===void 0)return;let t={};for(let[n,r]of Object.entries(e))h.has(n.toLowerCase())||(t[n]=r);return t}const _=20;function v(e,t=0){if(e!==void 0)return t>=_?[]:e.map(e=>({...e,error:{...e.error,issues:p(e.error.issues),attempts:v(e.error.attempts,t+1)},request:e.request&&{...e.request,body:m(e.request.body),headers:g(e.request.headers)}}))}function y(e,t,n,r,i=Date.now()){return{provider:e,model:t,body:m(n),headers:g(r),startedAt:i}}var b=class extends Error{status;issues;cause;retryAfterMs;code;attempts;constructor(e,t,n={}){super(e),this.type=t,this.name=`LLMError`,this.status=n.status,this.issues=n.issues,this.cause=n.cause,this.retryAfterMs=n.retryAfterMs,this.code=n.code,this.attempts=n.attempts}get retryable(){return f(this.type,this.code)}toSnapshot(){return{message:this.message,type:this.type,status:this.status,issues:p(this.issues),retryAfterMs:this.retryAfterMs,code:this.code,retryable:this.retryable,attempts:v(this.attempts)}}toJSON(){return{name:this.name,message:this.message,type:this.type,status:this.status,issues:p(this.issues),retryAfterMs:this.retryAfterMs,code:this.code,retryable:this.retryable,attempts:v(this.attempts)}}};function x(e){return e instanceof b}function ee(e,t){return e.code===t&&e.issues!==void 0}var S=class{store=new Map;constructor(e=1e3){this.maxSize=e}async get(e){let t=this.store.get(e);return t?Date.now()>=t.expiresAt?(this.store.delete(e),{hit:!1,value:null}):{hit:!0,value:t.value}:{hit:!1,value:null}}async set(e,t,n){this.cleanupExpiredEntries(),this.store.set(e,{value:t,expiresAt:Date.now()+n*1e3}),this.enforceSizeLimit()}async delete(e){this.store.delete(e)}cleanupExpiredEntries(){let e=Date.now();for(let[t,n]of this.store)e>=n.expiresAt&&this.store.delete(t)}enforceSizeLimit(){for(;this.store.size>this.maxSize;){let e=this.store.keys().next().value;if(e===void 0)break;this.store.delete(e)}}},C=class{constructor(e=new S){this.inner=e}normalize(e){return e.toLowerCase().trim().replace(/[^\p{L}\p{N}\s]/gu,` `).replace(/\s+/g,` `).trim()}async resolveKey(e){return this.normalize(e)}async get(e){return this.inner.get(this.normalize(e))}async set(e,t,n){await this.inner.set(this.normalize(e),t,n)}async delete(e){await this.inner.delete?.(this.normalize(e))}},w=class{constructor(e,t,n){this.l1=e,this.l2=t,this.l1Ttl=n}async resolveKey(e){return this.l1.resolveKey?this.l1.resolveKey(e):this.l2.resolveKey?this.l2.resolveKey(e):e}async get(e){let t=await this.l1.get(e);if(t.hit)return t;let n=await this.l2.get(e);return n.hit&&await this.l1.set(e,n.value,this.l1Ttl??60),n}async set(e,t,n){await Promise.all([this.l1.set(e,t,this.l1Ttl??n),this.l2.set(e,t,n)])}async delete(e){await Promise.all([this.l1.delete?.(e),this.l2.delete?.(e)])}};function T(e){return e}function E(e){return typeof e==`object`&&!!e&&`type`in e&&e.type===`tool_calls`&&Array.isArray(e.toolCalls)}const te=new Set([`unknown_tool`,`duplicate_tool_call_id`,`tool_choice_none_violated`,`unexpected_tool_calls`]),ne=e=>e.type===`parse`||e.type===`validation`||e.type===`aborted`||e.type===`quota_exceeded`||e.code&&te.has(e.code)?`stop`:`next`;var D=class extends b{constructor(e){let t=e[e.length-1]?.error;super(`${e.length} provider${e.length===1?``:`s`} attempted and failed: ${e.map(e=>`${e.provider}(${e.error.type})`).join(` then `)}`,`fallback_exhausted`,{status:t?.status,cause:t,retryAfterMs:t?.retryAfterMs,code:`fallback_exhausted`,attempts:e}),this.attempts=e}get retryable(){let e=this.attempts[this.attempts.length-1]?.error;return e?e.retryable:super.retryable}};function re(e){return e instanceof D}async function ie(e,t,n){if(!e.reserveUsage)return!1;try{return await e.reserveUsage({coalesced:t,signal:n}),!0}catch(e){throw n?.aborted?new b(`LLM request aborted`,`aborted`):new b(e instanceof Error?e.message:`Usage reservation failed`,`quota_exceeded`,{cause:e})}}function ae(e,t,n,r){return async i=>{try{await e.refundUsage?.({coalesced:t,signal:n})}catch(e){r(i,e)}}}async function O(e,t,n,r,i){if(r?.aborted)throw new b(`LLM request aborted`,`aborted`);let a=await ie(e,t,r),o=ae(e,t,r,i);if(r?.aborted)throw a&&await o(`[VernLLM] refundUsage failed after abort`),new b(`LLM request aborted`,`aborted`);let s;try{s=await n()}catch(e){throw a&&await o(`[VernLLM] refundUsage failed`),e}if(r?.aborted)throw a&&await o(`[VernLLM] refundUsage failed after abort`),new b(`LLM request aborted`,`aborted`);return s}async function oe(e,t,n,r){if(n?.aborted)throw new b(`LLM request aborted`,`aborted`);let i=await ie(e,!1,n),a=ae(e,!1,n,r);if(n?.aborted)throw i&&await a(`[VernLLM] refundUsage failed after abort`),new b(`LLM request aborted`,`aborted`);let o;try{o=await t()}catch(e){throw i&&await a(`[VernLLM] refundUsage failed after stream-open failure`),e}let s=o.finalResult.then(e=>e,async e=>{throw i&&await a(`[VernLLM] refundUsage failed after stream error`),e});return s.catch(()=>{}),{chunks:o.chunks,finalResult:s}}function se(e){return typeof e==`string`?e:JSON.stringify(e)??``}function ce(e,t){let n=[];if(t){let t=e;t.type===`tool_calls`?(t.toolCalls.forEach((e,t)=>{n.push({type:`tool_call_delta`,index:t,id:e.id,name:e.name,argsDelta:JSON.stringify(e.arguments??{}),complete:!0})}),t.content&&n.push({type:`text-delta`,delta:t.content})):n.push({type:`text-delta`,delta:se(t.content)})}else n.push({type:`text-delta`,delta:se(e)});return{async*[Symbol.asyncIterator](){for(let e of n)yield e}}}function le(e,t){return{async*[Symbol.asyncIterator](){let n=await e;yield*ce(n,t)}}}var ue=class{inFlight=new Map;constructor(e,t){this.cache=e,this.logger=t}async resolveCacheKey(e){return this.cache.resolveKey?await this.cache.resolveKey(e):e}async deleteCache(e){if(this.cache.delete)try{await this.cache.delete(await this.resolveCacheKey(e))}catch(e){this.logger.warn(`[VernLLM] cache delete failed: ${e instanceof Error?e.message:`unknown`}`)}}logRefundError(e,t){this.logger.error(e,{message:t instanceof Error?t.message:`unknown`})}async getCached(e){try{return await this.cache.get(e)}catch(e){return this.logger.warn(`[VernLLM] cache read failed: ${e instanceof Error?e.message:`unknown`}`),{hit:!1}}}async runCached(e){let t=await this.resolveCacheKey(e.cacheKey),n=t===e.cacheKey?e:{...e,cacheKey:t},r=await this.getCached(t);if(r.hit)return r.value;let i=this.inFlight.get(t);return i?O(n,!0,()=>i,e.signal,(e,t)=>this.logRefundError(e,t)):this.registerTrigger(n)}registerTrigger(e){let t=O(e,!1,()=>this.runAndCache(e),e.signal,(e,t)=>this.logRefundError(e,t));return this.inFlight.set(e.cacheKey,t),t.catch(()=>{}).finally(()=>{this.inFlight.delete(e.cacheKey)}),t}async runAndCache(e){let t=await e.fn();try{await this.cache.set(e.cacheKey,t,e.ttl)}catch(e){this.logger.warn(`[VernLLM] cache write failed: ${e instanceof Error?e.message:`unknown`}`)}return t}async runCachedStream(e,t){let n=await this.resolveCacheKey(e.cacheKey),r=n===e.cacheKey?e:{...e,cacheKey:n},i=await this.getCached(n);if(i.hit){let e=i.value;return{chunks:ce(e,t),finalResult:Promise.resolve(e)}}let a=this.inFlight.get(n);if(a){let n=O(r,!0,()=>a,e.signal,(e,t)=>this.logRefundError(e,t));return n.catch(()=>{}),{chunks:le(n,t),finalResult:n}}return this.registerStreamTrigger(r)}registerStreamTrigger(e){let t,n,r=new Promise((e,r)=>{t=e,n=r});this.inFlight.set(e.cacheKey,r),r.catch(()=>{}).finally(()=>{this.inFlight.delete(e.cacheKey)});let i=oe(e,async()=>{let t=await e.openStream(),n=t.finalResult.then(async t=>{try{await this.cache.set(e.cacheKey,t,e.ttl)}catch(e){this.logger.warn(`[VernLLM] cache write failed: ${e instanceof Error?e.message:`unknown`}`)}return t},e=>{throw e});return{chunks:t.chunks,finalResult:n}},e.signal,(e,t)=>this.logRefundError(e,t));return i.then(e=>{e.finalResult.then(t,n)},e=>{n(e)}),i}};function de(){return{state:`closed`,consecutiveFailures:0,openedAt:0,trialInFlight:!1}}const k=``;var fe=class{threshold;cooldownMs;onStateChange;isolateByModel;sharedBucket=de();bucketsByModel=new Map;constructor(e={}){this.threshold=e.threshold??5,this.cooldownMs=e.cooldownMs??3e4,this.onStateChange=e.onStateChange,this.isolateByModel=e.isolateByModel??!1}lookupBucket(e){if(!this.isolateByModel)return this.sharedBucket;let t=e??k;return this.bucketsByModel.get(t)}ensureBucketFor(e){if(!this.isolateByModel)return this.sharedBucket;let t=e??k,n=this.bucketsByModel.get(t);return n||(n=de(),this.bucketsByModel.set(t,n)),n}transition(e,t,n){if(t===e.state)return;let r=e.state;e.state=t,this.onStateChange?.(r,t,e.consecutiveFailures,n)}assertClosed(e){let t=this.ensureBucketFor(e);if(t.state!==`closed`){if(t.state===`open`){let n=Date.now()-t.openedAt;if(n<this.cooldownMs)throw new b(`Circuit open, provider has failed ${t.consecutiveFailures} times in a row. Retry in ${Math.ceil((this.cooldownMs-n)/1e3)}s.`,`circuit_open`,{code:`circuit_cooling_down`});t.trialInFlight=!0,this.transition(t,`half-open`,e);return}if(t.trialInFlight)throw new b(`Circuit half-open. A trial request is already in flight. Try again shortly.`,`circuit_open`,{code:`circuit_trial_in_flight`});t.trialInFlight=!0}}recordSuccess(e){let t=this.lookupBucket(e);t&&(t.consecutiveFailures=0,t.trialInFlight=!1,this.transition(t,`closed`,e),this.isolateByModel&&t.state===`closed`&&t.consecutiveFailures===0&&this.bucketsByModel.delete(e??k))}recordFailure(e){let t=this.ensureBucketFor(e);if(t.consecutiveFailures+=1,t.trialInFlight=!1,t.state===`half-open`){t.openedAt=Date.now(),this.transition(t,`open`,e);return}t.consecutiveFailures>=this.threshold&&(t.openedAt=Date.now(),this.transition(t,`open`,e))}getState(e){return this.lookupBucket(e)?.state??`closed`}open(e){let t=this.ensureBucketFor(e);t.openedAt=Date.now(),t.trialInFlight=!1,this.transition(t,`open`,e)}close(e){let t=this.ensureBucketFor(e);t.consecutiveFailures=0,t.trialInFlight=!1,this.transition(t,`closed`,e),this.isolateByModel&&t.state===`closed`&&t.consecutiveFailures===0&&this.bucketsByModel.delete(e??k)}};function A(e,t){return n=>{if(e)try{e(n)}catch(e){t.error(`[VernLLM] onEvent failed`,{message:e instanceof Error?e.message:`unknown`})}}}function pe(e,t,n,r,i){if(!e)return;let a=typeof e==`object`?e:void 0,o=a?.onStateChange,s=A(r,i);return new fe({...a,onStateChange:(e,r,a,c)=>{if(s({kind:`circuit_state`,provider:t,model:c??n,from:e,to:r,consecutiveFailures:a}),o)try{o(e,r,a,c)}catch(e){i.error(`[VernLLM] circuitBreaker.onStateChange failed`,{message:e instanceof Error?e.message:`unknown`})}}})}const me=1e4,he=2147483647;function j(e){return!e||e<=0||e===1/0?void 0:e}function M(e){return Math.min(e,he)}async function N(e,t,n){let r=new AbortController,i=j(t),a=i===void 0?void 0:setTimeout(()=>{r.abort()},M(i)),o=n?AbortSignal.any([n,r.signal]):r.signal;try{return await e(o)}catch(e){throw r.signal.aborted&&!n?.aborted&&e instanceof DOMException&&e.name===`AbortError`?new b(`Request timed out`,`timeout`,{code:`request_timeout`}):e}finally{clearTimeout(a)}}function ge(e,t,n,r){let i=j(t);if(i===void 0)return e();let a=!1;return new Promise((t,o)=>{let s=setTimeout(()=>{a=!0,n?.(),o(new b(`No stream chunk received for ${i}ms (idle timeout)`,`timeout`,{code:`idle_timeout`}))},M(i));e().then(e=>{if(clearTimeout(s),a){r?.debug(`[VernLLM] chunk resolved after idle timeout already fired; discarding`);return}a=!0,t(e)},e=>{if(clearTimeout(s),a){r?.debug(`[VernLLM] chunk rejection arrived after idle timeout already fired; discarding`);return}a=!0,o(e)})})}function P(e,t=me){if(!e||typeof e!=`object`)return;let n=e,r=n.headers??n.response?.headers;if(!r||typeof r!=`object`)return;let i=r,a=typeof i.get==`function`?i.get(`Retry-After`):Object.entries(r).find(([e])=>e.toLowerCase()===`retry-after`)?.at(1);if(typeof a!=`string`||a.trim()===``)return;let o=a.trim();if(/^\d+$/.test(o))return Math.max(0,Math.min(Number(o)*1e3,t));let s=Date.parse(o);if(!Number.isNaN(s))return Math.max(0,Math.min(s-Date.now(),t))}function _e(e,t,n=me){let r=Math.min(e*2**t,n);return r/2+Math.random()*(r/2)}async function ve(e,t){if(t?.aborted)throw new b(`Operation aborted`,`aborted`);await new Promise((n,r)=>{let i=()=>{clearTimeout(a),r(new b(`Operation aborted`,`aborted`))},a=setTimeout(()=>{t?.removeEventListener(`abort`,i),n()},e);t?.addEventListener(`abort`,i,{once:!0})})}function F(e){if(!e||typeof e!=`object`)return;let t=e;if(typeof t.status==`number`)return t.status;if(typeof t.statusCode==`number`)return t.statusCode;if(typeof t.$metadata?.httpStatusCode==`number`)return t.$metadata.httpStatusCode}const I=new Set([`ECONNREFUSED`,`ENOTFOUND`,`ECONNRESET`,`ETIMEDOUT`,`EAI_AGAIN`,`EPIPE`,`ECONNABORTED`,`EHOSTUNREACH`,`ENETUNREACH`]),ye=new Set([`fetch failed`,`failed to fetch`,`load failed`,`networkerror when attempting to fetch resource.`]);function be(e){if(!e||typeof e!=`object`)return!1;let t=e;if(typeof t.code==`string`&&I.has(t.code)||typeof t.message==`string`&&ye.has(t.message.toLowerCase()))return!0;if(t.cause&&typeof t.cause==`object`){let e=t.cause;if(typeof e.code==`string`&&I.has(e.code))return!0}return!1}function L(e){try{return JSON.stringify(e,null,2)??String(e)}catch{try{return String(e)}catch{return`[unprintable error]`}}}function R(e){if(e&&typeof e==`object`)try{let t=e;if(t.error!==void 0)return L(t.error);if(typeof t.message==`string`)return t.message}catch{}return L(e)}function z(e){switch(e){case 429:return`provider_rate_limited`;case 401:return`authentication`;case 403:return`authorization`;case 404:return`not_found`;case 413:return`payload_too_large`;default:return e>=500?`server_error`:void 0}}const xe=/\(no body\)/i;function Se(e){return Object.keys(e).length===0}function Ce(e){if(e&&typeof e==`object`){let{error:t,message:n}=e;if(t!=null){let e=typeof t==`string`&&t.trim().length===0,n=typeof t==`object`&&Se(t);if(!e&&!n)return!1}if(typeof n==`string`){let e=n.trim();return e.length===0||xe.test(e)}return!0}return!0}function B(e,t,n){if(t?.aborted)return new b(`LLM request aborted`,`aborted`,{attempts:n});if(e instanceof b)return e.code===void 0&&e.status!==void 0&&(e.code=z(e.status)),e.attempts===void 0&&n!==void 0&&(e.attempts=n),e;let r=F(e),i=P(e);if(r!==void 0){let t=R(e),a=z(r),o=a===void 0,s=Ce(e)?o?`LLM request failed with status ${r} and no error detail from the provider. This usually means a field or value in the request isn't supported by the specific model (for example, a reasoning/thinking parameter the model doesn't accept), rather than a transport or auth problem.`:`LLM request failed with status ${r} and no error detail from the provider.`:`LLM request failed: ${t}`;return new b(s,`api`,{status:r,cause:e,retryAfterMs:i,code:a,attempts:n})}return be(e)?new b(`LLM request failed`,`network`,{cause:e,retryAfterMs:i,code:`connection_failed`,attempts:n}):new b(`LLM request failed`,`unknown`,{cause:e,retryAfterMs:i,attempts:n})}function we(e){try{return JSON.parse(e)}catch{return}}function Te(e){return e.map(e=>({type:`function`,function:{name:e.name,description:e.description,parameters:e.parameters}}))}function Ee(e){return e.map(e=>({id:e.id,type:`function`,function:{name:e.name,arguments:JSON.stringify(e.arguments??{})}}))}function De(e){return e.map(e=>{let t;try{t=e.function.arguments.trim()?JSON.parse(e.function.arguments):{}}catch{throw new b(`Invalid JSON arguments for tool call "${e.function.name}"`,`parse`,{code:`tool_arguments_parse_failed`})}return{id:e.id,name:e.function.name,arguments:t}})}function Oe(e){return typeof e==`string`?e:JSON.stringify(e)}var ke=class{model;defaultMaxTokens;defaultTemperature;defaultReasoningEffort;defaultBudgetTokens;supportsJsonObjectMode;constructor(e){this.model=e.model,this.defaultMaxTokens=e.defaultMaxTokens,this.defaultTemperature=e.defaultTemperature,this.defaultReasoningEffort=e.defaultReasoningEffort,this.defaultBudgetTokens=e.defaultBudgetTokens,this.supportsJsonObjectMode=e.supportsJsonObjectMode}build(e){let{systemPrompt:t,userContent:n,history:r=[],maxTokens:i=this.defaultMaxTokens,model:a=this.model,jsonSchema:o,tools:s,toolChoice:c}=e,l=e.temperature===void 0?this.defaultTemperature:e.temperature,u=e.reasoningEffort===void 0?this.defaultReasoningEffort:e.reasoningEffort,d=e.budgetTokens===void 0?this.defaultBudgetTokens:e.budgetTokens;if(s&&s.length===0)throw new b("`tools` was an empty array. This is almost always a bug (e.g. a filtered tool list that ended up empty). An empty `tools` array still switches on tool-call mode (response shape, jsonMode default, wire format) with nothing for the model to call. Omit `tools` entirely for a normal call, or make sure the array is non-empty.",`invalid_params`);if(s){let e=new Set,t=new Set;for(let n of s)e.has(n.name)&&t.add(n.name),e.add(n.name);if(t.size)throw new b(`\`tools\` has duplicate name(s): [${[...t].join(`, `)}]. Tool names must be unique.`,`invalid_params`,{code:`duplicate_tool_names`,issues:{names:[...t]}})}if(c&&!s)throw new b("`toolChoice` was set without `tools`. There is nothing for it to choose between. Set `tools`, or remove `toolChoice`.",`invalid_params`);if(s&&typeof c==`object`&&!s.some(e=>e.name===c.name))throw new b(`toolChoice names "${c.name}", which is not in \`tools\` ([${s.map(e=>e.name).join(`, `)}]).`,`invalid_params`,{code:`unknown_tool_choice`,issues:{requested:c.name,available:s.map(e=>e.name)}});let f=e.jsonMode,p=f??!s;if(!this.supportsJsonObjectMode&&!o&&f===!0)throw new b('jsonMode: true was set explicitly, but this client does not support `response_format: "json_object"` (see LLMClient.supportsJsonObjectMode). Neither Anthropic nor Bedrock has a field that mechanically guarantees JSON output for this mode. Use `jsonSchema` instead, which maps to a real constraint on both.',`invalid_params`);if(!this.supportsJsonObjectMode&&!o&&f===void 0&&e.schema)throw new b('`schema` was provided, which requires JSON output to validate against, but this client does not support `response_format: "json_object"` (see LLMClient.supportsJsonObjectMode) and no `jsonSchema` was set. Neither Anthropic nor Bedrock has a field that mechanically guarantees JSON output without one. Use `jsonSchema` instead, which maps to a real constraint on both and still runs `schema` against its parsed result.',`invalid_params`);let m=!this.supportsJsonObjectMode&&!o&&f===void 0?!1:p,h=m||!!o;if(e.schema&&!h)throw new b(`schema was provided but jsonMode: false disables JSON parsing, so nothing would validate it. Remove jsonMode: false, set jsonSchema, or remove schema.`,`invalid_params`);let g=this.buildResponseFormat(o,h);this.validateHistory(r);let _={model:a,...l===null?{}:{temperature:l},max_tokens:i,...g?{response_format:g}:{},...u?{reasoning_effort:u}:{},...d==null?{}:{budget_tokens:d},...s?{tools:Te(s)}:{},...s?{tool_choice:this.buildWireToolChoice(c)}:{},messages:[...t?[{role:`system`,content:t}]:[],...r.flatMap(e=>this.turnToWireMessages(e)),{role:`user`,content:n}]};return{useJson:h,model:a,request:_}}validateHistory(e){let t;for(let[n,r]of e.entries()){if(r.role===`tool`){if(t?.role!==`assistant`||!t.toolCalls?.length)throw new b(`history[${n}] is a "tool" turn, but must immediately follow an "assistant" turn that requested tools`,`invalid_params`);if(!r.toolResults?.length)throw new b(`history[${n}] is a "tool" turn but has no toolResults`,`invalid_params`);let e=new Set(t.toolCalls.map(e=>e.id)),i=r.toolResults.map(e=>e.toolCallId),a=i.filter(t=>!e.has(t));if(a.length)throw new b(`history[${n}].toolResults references unknown toolCallId(s) [${a.join(`, `)}]`,`invalid_params`,{code:`unknown_tool_result_ids`,issues:{historyIndex:n,ids:a}});let o=new Set,s=new Set;for(let e of i)o.has(e)&&s.add(e),o.add(e);if(s.size)throw new b(`history[${n}].toolResults has duplicate toolCallId(s) [${[...s].join(`, `)}]`,`invalid_params`,{code:`duplicate_tool_result_ids`,issues:{historyIndex:n,ids:[...s]}});let c=[...e].filter(e=>!i.includes(e));if(c.length)throw new b(`history[${n}] is missing toolResults for toolCallId(s) [${c.join(`, `)}]`,`invalid_params`,{code:`missing_tool_results`,issues:{historyIndex:n,ids:c}})}else{if(r.role===t?.role)throw new b(`history must alternate user/assistant turns: consecutive "${r.role}" turns at history[${n-1}] and history[${n}]`,`invalid_params`);if(t?.role===`assistant`&&t.toolCalls?.length)throw new b(`history[${n}] follows an assistant tool request without tool results`,`invalid_params`)}t=r}if(t?.role===`assistant`&&t.toolCalls?.length)throw new b(`The last entry in history is an assistant tool request without tool results`,`invalid_params`);if(t?.role===`user`)throw new b(`The last entry in history is a "user" turn, which would collide with the current userContent turn.`,`invalid_params`)}buildWireToolChoice(e){return!e||e===`auto`?`auto`:e===`none`||e===`required`?e:{type:`function`,function:{name:e.name}}}turnToWireMessages(e){return e.role===`tool`?(e.toolResults??[]).map(e=>({role:`tool`,tool_call_id:e.toolCallId,content:typeof e.content==`string`?e.content:JSON.stringify(e.content??null),...e.isError?{is_error:!0}:{}})):e.role===`assistant`&&e.toolCalls?.length?[{role:`assistant`,...e.content===void 0?{}:{content:Oe(e.content)},tool_calls:Ee(e.toolCalls)}]:e.role===`assistant`?[{role:`assistant`,content:Oe(e.content===void 0?``:e.content)}]:[{role:e.role,content:e.content??``}]}buildResponseFormat(e,t){return e?{type:`json_schema`,json_schema:{name:e.name,schema:e.schema,strict:e.strict??!0,description:e.description}}:t?{type:`json_object`}:void 0}};const Ae=1e4,je=1e6;function Me(e,t,n){let{requestId:r,model:i,providerName:a,isFallback:o,chunkIdleTimeoutMs:s,streamController:c,logger:l,signal:u}=n,d,f,p=new Promise((e,t)=>{d=e,f=t});p.catch(()=>{});let m=1e4,h=[],g=[],_=!1,v,y=!1,x=e=>{let t=g.shift();if(t){t.resolve({done:!1,value:e});return}h.push(e),h.length>m*2&&(y||(y=!0,l.warn(`[VernLLM] stream chunk buffer exceeded cap (${m}), evicting ${h.length-m} oldest chunk(s); buffered=${h.length}. The chunks iterable was never read (or fell far behind) for this stream.`)),h.splice(0,h.length-m))},ee=()=>{_=!0;for(let e of g.splice(0))e.resolve({done:!0,value:void 0})},S=e=>{_=!0,v=e;for(let t of g.splice(0))t.reject(e)},C={[Symbol.asyncIterator](){return{next(){return h.length?Promise.resolve({done:!1,value:h.shift()}):_?v?Promise.reject(v):Promise.resolve({done:!0,value:void 0}):new Promise((e,t)=>{g.push({resolve:e,reject:t})})}}}},w=new Map,T=``,E;return(async()=>{try{let n=t;for(;!n.done;){let t=n.value;if(t.type!==`ping`){if(t.type===`text-delta`)T+=t.delta,x({type:`text-delta`,delta:t.delta});else if(t.type===`tool_call_delta`){let e=w.get(t.index);if(!e){if(w.size>=Ae)throw new b(`Stream contained more than ${Ae} distinct tool calls`,`validation`);e={args:``}}let n=t.argumentsDelta??``;if(e.args.length+n.length>je)throw new b(`Tool call arguments exceeded the ${je}-character stream limit`,`validation`);e.id??=t.id,e.name??=t.name,e.args+=n,w.set(t.index,e),x({type:`tool_call_delta`,index:t.index,id:t.id,name:t.name,argsDelta:t.argumentsDelta,complete:t.complete})}else if(t.type===`usage`){let e=t.usage.completion_tokens_details?.reasoning_tokens;E={promptTokens:t.usage.prompt_tokens??0,completionTokens:t.usage.completion_tokens??0,totalTokens:t.usage.total_tokens??0,...e===void 0?{}:{reasoningTokens:e},requestId:r,model:i,provider:a,usedFallback:o},x({type:`usage`,usage:E})}}n=await ge(()=>e.next(),s,()=>c.abort(),l)}}catch(t){try{await e.return?.()}catch{}c.abort();let r=B(t,u);try{n.onStreamFailure(r,E)}catch{}S(r),f(r);return}ee();try{n.onStreamSuccess(E)}catch{}try{let e=w.size?[...w.entries()].sort(([e],[t])=>e-t).map(([,e])=>({id:e.id??``,type:`function`,function:{name:e.name??``,arguments:e.args}})):void 0,t=n.finalize(T,e,E);d(t)}catch(e){f(e)}})(),{chunks:C,finalResult:p}}function Ne(e){return e}var Pe=class{maxRetries;timeoutMs;chunkIdleTimeoutMs;baseDelayMs;nonRetryableStatus;parseJson;logger;redact;onUsage;onUsageFailure;reportEvent;breaker;limiter;isFallback;requestBuilder;constructor(e,t,n,r){this.providerName=e,this.client=t,this.model=n,this.maxRetries=r.maxRetries,this.timeoutMs=r.timeoutMs,this.chunkIdleTimeoutMs=r.chunkIdleTimeoutMs,this.baseDelayMs=r.baseDelayMs,this.nonRetryableStatus=r.nonRetryableStatus,this.parseJson=r.parseJson??we,this.logger=r.logger,this.redact=r.redact,this.onUsage=r.onUsage,this.onUsageFailure=r.onUsageFailure,this.reportEvent=A(r.onEvent,this.logger),this.breaker=r.breaker,this.limiter=r.limiter,this.isFallback=r.isFallback??!1,this.requestBuilder=new ke({model:n,defaultMaxTokens:r.defaultMaxTokens,defaultTemperature:r.defaultTemperature,defaultReasoningEffort:r.defaultReasoningEffort,defaultBudgetTokens:r.defaultBudgetTokens,supportsJsonObjectMode:t.supportsJsonObjectMode??!0})}getCircuitState(e){return this.breaker?.getState(e)}get isolateByModel(){return this.breaker?.isolateByModel??!1}openCircuit(e){this.breaker?.open(e)}closeCircuit(e){this.breaker?.close(e)}assertBreakerClosed(e){this.breaker?.assertClosed(e??this.model)}async run(e,t,n){let r=e.model??this.model,i=[];try{return await this.retryWithBackoff((n,r)=>this.executeCall(e,t,n,r),t,r,e.signal,n,i)}catch(n){let a=B(n,e.signal,i.length>0?i:void 0);throw this.countsTowardBreaker(a)&&this.breaker?.recordFailure(r),this.logger.debug(`[VernLLM:${t}] error:\n${this.redactText(R(n))}`),a}}async runStream(e,t,n){let r=e.model??this.model,i=[];try{return await this.retryWithBackoff((n,r)=>this.executeStreamCall(e,t,n,r),t,r,e.signal,n,i)}catch(n){let a=B(n,e.signal,i.length>0?i:void 0);throw this.countsTowardBreaker(a)&&this.breaker?.recordFailure(r),this.logger.debug(`[VernLLM:${t}] stream-open error:\n${this.redactText(R(n))}`),a}}async executeCall(e,t,n,r){let{useJson:i,model:a,request:o}=this.requestBuilder.build(e);r?.(y(this.providerName,a,o,void 0,Date.now()));let s;if(this.limiter){let n=await this.limiter.acquire(this.limiter.estimate(o),e.signal);s=n.release,n.waitedMs>0&&this.reportEvent({kind:`rate_limited`,requestId:t,provider:this.providerName,model:a,waitedMs:n.waitedMs,reason:n.reason??`rpm`})}try{let r=await N(e=>this.client.chat.completions.create(o,{signal:e}),this.timeoutMs,e.signal),c=this.extractUsage(r,t,a);s?.(this.actualTokensFor(c)),s=void 0;let l=r.choices?.[0]?.message?.content,u=r.choices?.[0]?.message?.tool_calls;return this.finalizeResponse(l,u,e,i,a,c,t,n)}finally{s?.()}}redactText(e){return this.redact?this.redact(e):e}redactedOutput(e,t){return this.redactText(e??`[${t?.length??0} tool call(s)]`)}finalizeResponse(e,t,n,r,i,a,o,s){try{let s=e?.trim();if(!s&&!t?.length)throw new b(`Empty LLM response`,`api`,{code:`empty_response`});if(this.logger.debug(`[VernLLM:${o}] output:\n${this.redactedOutput(s,t).slice(0,800)}`),t?.length){if(!n.tools)throw new b("Provider returned tool_calls but no `tools` were sent with this call.",`validation`,{code:`unexpected_tool_calls`});if(n.toolChoice===`none`)throw new b(`Provider returned tool_calls despite toolChoice: 'none'.`,`validation`,{code:`tool_choice_none_violated`});let e=De(t);return this.validateToolCallArguments(e,n.tools),this.breaker?.recordSuccess(i),this.reportUsage(a),{type:`tool_calls`,toolCalls:e,...s?{content:s}:{}}}let c=s??``;if(!r)return this.breaker?.recordSuccess(i),this.reportUsage(a),n.tools?{type:`content`,content:c}:c;let l=this.parseAndValidate(c,n.schema);return this.breaker?.recordSuccess(i),this.reportUsage(a),n.tools?{type:`content`,content:l}:l}catch(e){let t=B(e,n.signal);throw a&&t.type!==`aborted`&&this.reportUsageFailure(a,t,s),t}}async executeStreamCall(e,t,n,r){let{useJson:i,model:a,request:o}=this.requestBuilder.build(e);r?.(y(this.providerName,a,o,void 0,Date.now()));let s=this.client.chat.completions;if(!s.createStream)throw new b(`stream: true requires a client/adapter with createStream`,`invalid_params`,{code:`unsupported_capability`,issues:{capability:`createStream`}});let c=s.createStream.bind(s),l;if(this.limiter){let n=await this.limiter.acquire(this.limiter.estimate(o),e.signal);l=n.release,n.waitedMs>0&&this.reportEvent({kind:`rate_limited`,requestId:t,provider:this.providerName,model:a,waitedMs:n.waitedMs,reason:n.reason??`rpm`})}let u=new AbortController,d=e.signal?AbortSignal.any([e.signal,u.signal]):u.signal;try{let{iterator:r,first:s}=await N(async e=>{let t=c(o,{signal:e})[Symbol.asyncIterator](),n=await t.next();return{iterator:t,first:n}},this.timeoutMs,d);if(s.done)throw new b(`Empty LLM response`,`api`);let f=l,p=Me(r,s,{requestId:t,model:a,providerName:this.providerName,isFallback:this.isFallback,chunkIdleTimeoutMs:e.chunkIdleTimeoutMs??this.chunkIdleTimeoutMs,streamController:u,logger:this.logger,signal:e.signal,onStreamSuccess:e=>{this.breaker?.recordSuccess(a),f?.(this.actualTokensFor(e))},onStreamFailure:(e,t)=>{e.type===`timeout`&&this.breaker?.recordFailure(a),t&&e.type!==`aborted`&&this.reportUsageFailure(t,e,n,!0),f?.(this.actualTokensFor(t))},finalize:(r,o,s)=>this.finalizeResponse(r,o,e,i,a,s,t,n)});return l=void 0,p}finally{l?.()}}validateToolCallArguments(e,t){let n=new Map(t.map(e=>[e.name,e])),r=new Set,i=[];for(let t of e)r.has(t.id)&&i.push({name:t.name,toolCallId:t.id,code:`duplicate_tool_call_id`}),r.add(t.id),n.has(t.name)||i.push({name:t.name,toolCallId:t.id,code:`unknown_tool`});if(i.length>0){let e=i.find(e=>e.code===`unknown_tool`),t=e?`Model requested tool "${e.name}", which was not in the tools offered ([${[...n.keys()].join(`, `)}]).`:`Duplicate tool call id "${i[0].toolCallId}" in the model's response.`,r=i.length>1?`${t} (${i.length} tool call issues total, see error.issues.)`:t;throw new b(r,`validation`,{code:e?`unknown_tool`:`duplicate_tool_call_id`,issues:i})}for(let t of e){let e=n.get(t.name);if(!e?.argumentsSchema)continue;let r=e.argumentsSchema.safeParse(t.arguments);if(!r.success)throw new b(`Arguments for tool call "${t.name}" failed validation`,`validation`,{issues:r.error})}}async retryWithBackoff(e,t,n,r,i,a){let o,s;for(let c=0;c<=this.maxRetries;c++){s=void 0;try{return c>0&&await this.recoverDelay(t,n,c,o,r),i?.(),await e(c,e=>{s=e})}catch(e){o=e;let t=c<this.maxRetries&&this.shouldRetry(e,r);if(!t)break;a?.push({index:c,error:B(e,r).toSnapshot(),request:Ne(s)})}}throw o}extractUsage(e,t,n){if(!e.usage)return;let r=e.usage.completion_tokens_details?.reasoning_tokens;return{promptTokens:e.usage.prompt_tokens??0,completionTokens:e.usage.completion_tokens??0,totalTokens:e.usage.total_tokens??0,...r===void 0?{}:{reasoningTokens:r},requestId:t,model:n,provider:this.providerName,usedFallback:this.isFallback}}actualTokensFor(e){if(e)return e.totalTokens||e.promptTokens+e.completionTokens}reportUsage(e){if(!(!e||!this.onUsage))try{this.onUsage(e)}catch(e){this.logger.error(`[VernLLM] onUsage failed`,{message:e instanceof Error?e.message:`unknown`})}}reportUsageFailure(e,t,n,r=!1){let i=e.totalTokens||e.promptTokens+e.completionTokens,a=r?`mid-stream failure (terminal, no further attempts)`:`attempt ${n+1}/${this.maxRetries+1}`;if(this.logger.warn(`[VernLLM:${e.requestId}] usage failure, ${a}: type=${t.type} tokens=${i}`),this.onUsageFailure)try{this.onUsageFailure(e,t)}catch(e){this.logger.error(`[VernLLM] onUsageFailure failed`,{message:e instanceof Error?e.message:`unknown`})}}parseAndValidate(e,t){let n;try{n=this.parseJson(e)}catch{throw new b(`Invalid JSON response`,`parse`)}if(n==null)throw new b(`Invalid JSON response`,`parse`);if(!t)return n;let r=t.safeParse(n);if(!r.success)throw new b(`Schema validation failed`,`validation`,{issues:r.error});return r.data}async recoverDelay(e,t,n,r,i){let a=P(r),o=a??_e(this.baseDelayMs,n),s=a!==void 0;this.logger.warn(`[VernLLM:${e}] recovery attempt ${n}/${this.maxRetries}, waiting ${Math.ceil(o)}ms`+(s?` (honoring Retry-After)`:``)),this.reportEvent({kind:`retry`,requestId:e,provider:this.providerName,model:t,attempt:n,maxRetries:this.maxRetries,delayMs:o,retryAfterHonored:s,error:B(r,i)}),await ve(o,i)}shouldRetry(e,t){if(t?.aborted||e instanceof b&&!e.retryable)return!1;let n=F(e);return!(n!==void 0&&this.nonRetryableStatus.includes(n))}countsTowardBreaker(e){return e.retryable}};function Fe(e){return{debug:V(e,`debug`),warn:V(e,`warn`),error:V(e,`error`)}}function V(e,t){let n=e[t].bind(e);return(...e)=>{try{Le(n(...e))}catch{}}}function Ie(e){return typeof e?.then==`function`}function Le(e){Ie(e)&&Promise.resolve(e).catch(()=>{})}var Re=class{constructor(e){this.debugEnabled=e}debug(e){this.debugEnabled&&console.debug(e)}warn(e){console.warn(e)}error(e,t){console.error(e,t??``)}};function ze(e){let t=e.messages.reduce((e,t)=>{let n=t.content;if(typeof n==`string`)return e+n.length;if(n==null)return e;try{return e+JSON.stringify(n).length}catch{return e}},0);return Math.ceil(t/4)+(e.max_tokens??0)}var H=class{available;lastRefill=Date.now();constructor(e,t){this.capacity=e,this.refillPerMs=t,this.available=e}refill(){if(this.refillPerMs===0)return;let e=Date.now(),t=e-this.lastRefill;this.available=Math.min(this.capacity,this.available+Math.max(0,t)*this.refillPerMs),this.lastRefill=e}tryTake(e){return this.refill(),this.available<e?!1:(this.available-=e,!0)}msUntilAvailable(e){return this.refill(),this.available>=e?0:this.refillPerMs===0?1/0:(e-this.available)/this.refillPerMs}give(e){this.available=Math.min(this.capacity,this.available+e)}getCapacity(){return this.capacity}};const Be=2147483647;var Ve=class{requests;tokens;concurrency;maxQueueMs;maxQueueSize;estimateTokensFn;queue=[];wakeTimer;constructor(e){e.requestsPerMinute&&(this.requests=new H(e.requestsPerMinute,e.requestsPerMinute/6e4)),e.tokensPerMinute&&(this.tokens=new H(e.tokensPerMinute,e.tokensPerMinute/6e4)),e.maxConcurrent&&(this.concurrency=new H(e.maxConcurrent,0)),this.maxQueueMs=e.maxQueueMs??3e4,this.maxQueueSize=e.maxQueueSize??0,this.estimateTokensFn=e.estimateTokens??ze}estimate(e){return this.estimateTokensFn(e)}async acquire(e,t){if(t?.aborted)throw new b(`LLM request aborted`,`aborted`);if(!Number.isFinite(e)||e<0)throw new b(`estimatedTokens must be a finite, non-negative number, got ${String(e)}`,`invalid_params`);if(this.tokens&&e>this.tokens.getCapacity())throw new b(`estimatedTokens (${e}) exceeds the configured tokensPerMinute capacity (${this.tokens.getCapacity()}); this call could never acquire capacity.`,`rate_limited`,{code:`rate_limit_capacity_exceeded`});if(this.queue.length===0){let n=this.tryAcquireBuckets(e);return n.ok?{release:this.makeRelease(e),waitedMs:0}:this.enqueue(e,n.reason,t)}if(this.maxQueueSize>0&&this.queue.length>=this.maxQueueSize)throw this.queueFullError();return this.enqueue(e,void 0,t)}queueFullError(){return new b(`Rate limit queue is full`,`rate_limited`,{code:`rate_limit_queue_full`})}enqueue(e,t,n){return new Promise((r,i)=>{let a={estimatedTokens:e,enqueuedAt:Date.now(),lastReason:t,resolve:e=>{c(),r(e)},reject:e=>{c(),this.wakeTimer&&(clearTimeout(this.wakeTimer),this.wakeTimer=void 0),this.drain(),i(e)}},o,s=()=>{a.reject(new b(`LLM request aborted`,`aborted`))},c=()=>{o&&clearTimeout(o),n?.removeEventListener(`abort`,s);let e=this.queue.indexOf(a);e!==-1&&this.queue.splice(e,1)};this.maxQueueMs>0&&(o=setTimeout(()=>{a.reject(new b(`Rate limit queue timed out before capacity was available`,`rate_limited`,{code:`rate_limit_queue_timeout`}))},this.maxQueueMs)),n?.addEventListener(`abort`,s,{once:!0}),this.queue.push(a),this.drain()})}tryAcquireBuckets(e){let t=[],n=(e,n)=>e?e.tryTake(n)?(t.push({bucket:e,amount:n}),!0):!1:!0;if(!n(this.concurrency,1))return{ok:!1,reason:`concurrency`};if(!n(this.requests,1)){for(let e of t)e.bucket.give(e.amount);return{ok:!1,reason:`rpm`}}if(!n(this.tokens,e)){for(let e of t)e.bucket.give(e.amount);return{ok:!1,reason:`tpm`}}return{ok:!0}}drain(){for(;this.queue.length>0;){let e=this.queue[0],t=this.tryAcquireBuckets(e.estimatedTokens);if(!t.ok){e.lastReason=t.reason,this.scheduleWake(t.reason,e.estimatedTokens);return}let n=Date.now()-e.enqueuedAt;e.resolve({release:this.makeRelease(e.estimatedTokens),waitedMs:n,reason:e.lastReason})}}scheduleWake(e,t){if(this.wakeTimer)return;let n=e===`rpm`?this.requests?.msUntilAvailable(1):e===`tpm`?this.tokens?.msUntilAvailable(t):void 0;if(n===void 0||!Number.isFinite(n))return;let r=Math.min(Math.max(1,Math.ceil(n)),Be);this.wakeTimer=setTimeout(()=>{this.wakeTimer=void 0,this.drain()},r)}makeRelease(e){let t=!1;return n=>{t||(t=!0,this.concurrency?.give(1),this.tokens&&n!==void 0&&Number.isFinite(n)&&this.tokens.give(e-n),this.drain())}}},He=class{logger;executors;fallbackOn;reportEvent;cacheOrchestrator;constructor(e){this.logger=Fe(e.logger??new Re(e.debug??!1));let t=e.name??`primary`;this.cacheOrchestrator=new ue(e.cache??new S,this.logger),this.fallbackOn=e.fallbackOn??ne,this.reportEvent=A(e.onEvent,this.logger);let n=e.defaultTemperature===void 0?.2:e.defaultTemperature,r=e.defaultReasoningEffort,i=e.defaultBudgetTokens,a={client:e.client,model:e.model,name:t,maxRetries:e.maxRetries,timeoutMs:e.timeoutMs,chunkIdleTimeoutMs:e.chunkIdleTimeoutMs,baseDelayMs:e.baseDelayMs,defaultMaxTokens:e.defaultMaxTokens,defaultTemperature:n,defaultReasoningEffort:r,defaultBudgetTokens:i,nonRetryableStatus:e.nonRetryableStatus,circuitBreaker:e.circuitBreaker,rateLimit:e.rateLimit},o=Array.isArray(e.fallback)?e.fallback:e.fallback?[e.fallback]:[],s=[a,...o];this.executors=s.map((a,o)=>{let s=o>0,c=a.name??(s?`fallback[${o-1}]`:t),l=pe(a.circuitBreaker,c,a.model,e.onEvent,this.logger);return new Pe(c,a.client,a.model,{maxRetries:a.maxRetries??e.maxRetries??1,timeoutMs:a.timeoutMs??e.timeoutMs??25e3,chunkIdleTimeoutMs:a.chunkIdleTimeoutMs??e.chunkIdleTimeoutMs??3e4,baseDelayMs:a.baseDelayMs??e.baseDelayMs??500,defaultMaxTokens:a.defaultMaxTokens??e.defaultMaxTokens??1e3,defaultTemperature:a.defaultTemperature===void 0?n:a.defaultTemperature,defaultReasoningEffort:a.defaultReasoningEffort===void 0?r:a.defaultReasoningEffort,defaultBudgetTokens:a.defaultBudgetTokens===void 0?i:a.defaultBudgetTokens,nonRetryableStatus:a.nonRetryableStatus??e.nonRetryableStatus??[400,401,403,404,422],parseJson:e.parseJson,logger:this.logger,redact:e.redact,onUsage:e.onUsage,onUsageFailure:e.onUsageFailure,onEvent:e.onEvent,breaker:l,limiter:a.rateLimit?new Ve(a.rateLimit):void 0,isFallback:s})})}logRefundError(e,t){this.logger.error(e,{message:t instanceof Error?t.message:`unknown`})}async runFallbackChain(e,t,n,r=!1){let i=[];for(let a=0;a<this.executors.length;a++){let o=this.executors[a],s=Date.now(),c=0;try{a===0&&r||o.assertBreakerClosed(e.model);let t=await n(o,()=>{c+=1});return{result:t,executor:o,index:a,attemptCount:c}}catch(n){let r=B(n,e.signal);i.push({index:a-1,provider:o.providerName,model:e.model??o.model,error:r.toSnapshot()});let c=a===this.executors.length-1,l=this.fallbackOn(r,{isLastTarget:c}),u=c?`stop`:l;if(u===`stop`)throw i.length>1?new D(i):r;let d=this.executors[a+1];this.reportEvent({kind:`fallback`,requestId:t,from:o.providerName,to:d.providerName,fromIndex:a-1,toIndex:a,error:r,elapsedMs:Date.now()-s})}}throw new b(`No provider targets configured`,`invalid_params`)}async call(e){if(e.signal?.aborted)throw new b(`LLM request aborted`,`aborted`);let t=e.requestId??(0,c.randomUUID)(),n=this.executors.length===1;return n&&this.executors[0].assertBreakerClosed(e.model),e.stream?oe(e,async()=>{let{result:r}=await this.runFallbackChain(e,t,(n,r)=>n.runStream(e,t,r),n);return r},e.signal,(e,t)=>this.logRefundError(e,t)):O(e,!1,async()=>{let{result:r,executor:i,index:a,attemptCount:o}=await this.runFallbackChain(e,t,(n,r)=>n.run(e,t,r),n);return e.meta&&(e.meta.current={provider:i.providerName,model:e.model??i.model,fallbackIndex:a-1,usedFallback:a>0,attempts:o}),r},e.signal,(e,t)=>this.logRefundError(e,t))}runCached(e){return this.cacheOrchestrator.runCached(e)}async deleteCache(e){await this.cacheOrchestrator.deleteCache(e)}async cachedCall(e){let{call:t,...n}=e,r=t;if(r.reserveUsage||r.refundUsage)throw new b("`reserveUsage`/`refundUsage` were set inside `call`, where cachedCall ignores them. Move them to the top level of the cachedCall() params, alongside cacheKey/ttl, instead.",`invalid_params`);if(r.stream){let e=r;return this.cacheOrchestrator.runCachedStream({...n,openStream:()=>this.call(e)},!!r.tools)}return this.runCached({...n,fn:()=>this.call(r)})}getCircuitState(e){let t=this.resolveExecutor(e?.index??0,`getCircuitState`);return this.warnIfModelUnsupported(t.isolateByModel,e?.model,`getCircuitState`),t.getCircuitState(e?.model??t.model)}getCircuitStates(e){return this.executors.map((t,n)=>({provider:t.providerName,index:n,isFallback:n>0,isolateByModel:t.isolateByModel,state:t.getCircuitState(e??t.model)}))}openCircuit(e){let t=this.resolveExecutor(e?.index??0,`openCircuit`);this.warnIfModelUnsupported(t.isolateByModel,e?.model,`openCircuit`),t.openCircuit(e?.model??t.model)}closeCircuit(e){let t=this.resolveExecutor(e?.index??0,`closeCircuit`);this.warnIfModelUnsupported(t.isolateByModel,e?.model,`closeCircuit`),t.closeCircuit(e?.model??t.model)}resolveExecutor(e,t){let n=this.executors[e];if(!n)throw RangeError(`${t}: no target at index ${e} (chain has ${this.executors.length} target${this.executors.length===1?``:`s`})`);return n}warnIfModelUnsupported(e,t,n){t!==void 0&&!e&&this.logger.warn(`[VernLLM] ${n}: \`model: '${t}'\` has no effect here. This target's circuitBreaker doesn't have isolateByModel on, so it only tracks one shared circuit regardless of \`model\`. Omit \`model\`, or set \`circuitBreaker.isolateByModel: true\` on this target if per-model tracking is what you want.`)}};function Ue(e){return e}function We(e){return e}async function*Ge(e){let t=new TextDecoder(`utf-8`,{fatal:!0}),n=``;for await(let r of e){let e;try{e=typeof r==`string`?r:t.decode(r,{stream:!0})}catch(e){throw new b(`Invalid UTF-8 in SSE stream`,`parse`,{cause:e})}n=(n+e).replace(/\r\n/g,`
2
2
  `).replace(/\r(?!$)/g,`
3
3
  `);let i=n.indexOf(`
4
4
 
@@ -11,5 +11,5 @@
11
11
 
12
12
  `)}let i=n.trim();if(i){let e=K(i);e!==U&&e!==W&&(yield e)}}const U=Symbol(`sse-stream-done`),W=Symbol(`sse-frame-no-data`),G=Symbol(`sse-frame-ping`);function K(e){let t=[],n=!1;for(let r of e.split(`
13
13
  `)){if(r.startsWith(`:`)){n=!0;continue}if(!r.startsWith(`data:`))continue;t.push(r.startsWith(`data: `)?r.slice(6):r.slice(5))}if(!t.length)return n?G:W;let r=t.join(`
14
- `);if(r===`[DONE]`)return U;try{return JSON.parse(r)}catch(e){throw new b(`Invalid JSON in SSE frame: ${r.slice(0,200)}`,`parse`,{cause:e,code:`stream_frame_invalid`})}}const Ue=[`image/png`,`image/jpeg`,`image/gif`,`image/webp`];function q(e){if(Ue.includes(e))return e;throw new b(`Unsupported image mimeType "${e}": expected one of ${Ue.join(`, `)}`,`invalid_params`)}function J(e,t){return t?Array.isArray(t)?t.includes(e):t(e):!1}const Y={minimal:1024,low:4096,medium:16e3,high:32e3};function X(e){if(!e)return Y;let t={...Y,...e};if(!(t.minimal<t.low&&t.low<t.medium&&t.medium<t.high))throw new b(`reasoningEffortTokens must keep tiers in strictly ascending order (minimal < low < medium < high), got ${JSON.stringify(t)}. An out-of-order override doesn't just misrank tiers, it can make some of them unreachable.`,`invalid_params`);return t}function Z(e,t=Y){return t[e]}function Q(e,t=Y){return e<=t.minimal?`minimal`:e<=t.low?`low`:e<=t.medium?`medium`:`high`}function We(e){let t=/opus-(\d+)(?:-(\d+))?/.exec(e);if(!t)return null;let n=t[2],r=n===void 0||n.length>=8?0:Number(n);return[Number(t[1]),r]}function Ge(e){let t=We(e);if(t){let[e,n]=t;return e>4||e===4&&n>=7}return[`sonnet-5`,`fable-5`,`mythos`].some(t=>e.includes(t))}function Ke(e,t){return Ge(e)?!0:t?Array.isArray(t)?t.includes(e):t(e):!1}function qe(e,t){return!Ke(e,t)}function Je(e,t){if(e<1024)throw new b(`budgetTokens (${e}) is below Anthropic's minimum of 1024. Raise budgetTokens, or use a reasoningEffort tier of 'low' or above with the default conversion table.`,`invalid_params`);if(e>=t)throw new b(`budgetTokens (${e}) must be less than maxTokens (${t}); the thinking budget and the reply share the same max_tokens ceiling on Anthropic. Raise maxTokens, or lower budgetTokens/reasoningEffort.`,`invalid_params`)}function Ye(e){if(e)throw new b(`budgetTokens/reasoningEffort was set alongside ${e}. Anthropic rejects thinking combined with a tool_choice that forces tool use, the model has to be able to reply with plain text for thinking to run. Use toolChoice: 'auto' (or omit toolChoice) for this call, or drop budgetTokens/reasoningEffort for it.`,`invalid_params`)}function Xe(e){return e===`minimal`?`low`:e}function Ze(e,t){return $e(t,e.toUpperCase())}function Qe(e){let t=/gemini-\d+\.(\d+)/.exec(e);return t?Number(t[1]):0}function $e(e,t){if(!e.includes(`pro`))return t;let n=et(e);if(n===null||n<3)return t;let r=Qe(e);return r===0?t===`HIGH`?`HIGH`:`LOW`:t===`MINIMAL`?`LOW`:t}function et(e){let t=/gemini-(\d+)/.exec(e);return t?Number(t[1]):null}function tt(e){let t=et(e);return t!==null&&t>=3}function nt(e,t){return tt(e)?!0:t?Array.isArray(t)?t.includes(e):t(e):!1}function rt(e){return e.map(e=>e.type===`image`?{type:`image`,source:{type:`base64`,media_type:q(e.mimeType),data:e.data}}:{type:`text`,text:e.text})}function it(e,t){if(e.type!==`object`)throw new b(`Tool "${t}"'s schema must have "type": "object" (Anthropic requires object-shaped tool parameters).`,`validation`);return e}function at(e){return!e||e===`auto`?{type:`auto`}:e===`none`?{type:`none`}:e===`required`?{type:`any`}:{type:`tool`,name:e.function.name}}function ot(e,t){return{tools:e.map(e=>({name:e.function.name,description:e.function.description,input_schema:it(e.function.parameters,e.function.name)})),toolChoice:at(t)}}function st(e,t,n,r){let i=e.messages.find(e=>e.role===`system`),a=e.messages.filter(e=>e.role===`user`||e.role===`assistant`||e.role===`tool`),o=e.response_format?.type===`json_schema`?e.response_format.json_schema:void 0,s=o?.name.trim();if(o&&!s)throw new b(`json_schema.name must not be empty.`,`validation`);let c=!!o&&J(e.model,t);if(o&&e.tools?.length&&!c)throw new b(`Anthropic model "${e.model}" is not covered by nativeStructuredOutputModels, so \`jsonSchema\` is emulated as a forced single tool call there, which collides with the \`tools\` you also provided. Either drop \`tools\` or \`jsonSchema\` for this call, or pass this model in fromAnthropic's \`nativeStructuredOutputModels\` option once you've confirmed it supports Anthropic's \`output_config.format\`.`,`validation`);if(e.response_format?.type===`json_object`)throw new b('response_format: "json_object" is not supported on Anthropic. Unlike OpenAI, Anthropic has no API-level field that mechanically guarantees valid JSON output for this mode, so it used to be emulated by injecting a "respond with JSON only" instruction into the system prompt, a guarantee this adapter can no longer make. Use `jsonSchema` instead, which maps to a real API-level constraint (Anthropic\'s native output_config.format on covered models, or a forced single tool call otherwise).',`validation`);let l,u,d,f;if(o&&c)u={type:`json_schema`,schema:o.schema},e.tools?.length&&({tools:d,toolChoice:f}=ot(e.tools,e.tool_choice));else if(o&&s){let{schema:e,description:t,strict:n}=o;l=s,d=[{name:l,description:t,input_schema:it(e,l),strict:n}],f={type:`tool`,name:l}}!o&&e.tools?.length&&({tools:d,toolChoice:f}=ot(e.tools,e.tool_choice));let p,m;if(e.budget_tokens!==void 0||e.reasoning_effort!==void 0)if(Ye(f?.type===`tool`?`toolChoice forcing the "${f.name}" tool`:f?.type===`any`?`toolChoice: 'required' (Anthropic's "any" tool_choice)`:void 0),qe(e.model,r)){let t=e.budget_tokens??Z(e.reasoning_effort,n);Je(t,e.max_tokens),p={type:`enabled`,budget_tokens:t}}else{let t=e.reasoning_effort??Q(e.budget_tokens,n);p={type:`adaptive`},m=Xe(t)}let h=i?.content,g=p?void 0:e.temperature,_={model:e.model,max_tokens:e.max_tokens,...g===void 0?{}:{temperature:g},system:h||void 0,messages:lt(a.map(e=>ut(e))),...d?{tools:d,tool_choice:f}:{},...u||m?{output_config:{...u?{format:u}:{},...m?{effort:m}:{}}}:{},...p?{thinking:p}:{}};return{body:_,toolName:l}}function ct(e,t){let n=t?.nativeStructuredOutputModels,r=X(t?.reasoningEffortTokens),i=t?.adaptiveOnlyModels,a=e.messages.create.bind(e.messages);return{supportsJsonObjectMode:!1,chat:{completions:{async create(t,a){let{body:o,toolName:s}=st(t,n,r,i),c=await e.messages.create(o,a),l,u;if(s){let e=c.content.find(e=>e.type===`tool_use`&&e.name===s);if(!e)throw new b(`Anthropic did not return the required structured output tool "${s}".`,`validation`);if(!e.input||typeof e.input!=`object`||Array.isArray(e.input))throw new b(`Anthropic returned invalid structured output for tool "${s}". Expected an object.`,`validation`);l=JSON.stringify(e.input)}else{l=c.content.filter(e=>e.type===`text`).map(e=>e.text??``).join(``);let e=c.content.filter(e=>e.type===`tool_use`);e.length&&(u=e.map(e=>({id:e.id,type:`function`,function:{name:e.name,arguments:JSON.stringify(e.input??{})}})))}return{choices:[{message:{content:l,...u?{tool_calls:u}:{}}}],usage:{prompt_tokens:c.usage?.input_tokens,completion_tokens:c.usage?.output_tokens,total_tokens:(c.usage?.input_tokens??0)+(c.usage?.output_tokens??0),...c.usage?.output_tokens_details?.thinking_tokens===void 0?{}:{completion_tokens_details:{reasoning_tokens:c.usage.output_tokens_details.thinking_tokens}}}}},async*createStream(e,t){let{body:o,toolName:s}=st(e,n,r,i),c=await a({...o,stream:!0},t),l=new Map,u=0,d=!1;for await(let e of c)if(e.type===`message_start`)u=e.message.usage?.input_tokens??0;else if(e.type===`content_block_start`)if(e.content_block.type===`tool_use`){let t=e.content_block.name===s?`json-tool`:`tool_use`;l.set(e.index,t),t===`json-tool`?d=!0:s||(yield{type:`tool_call_delta`,index:e.index,id:e.content_block.id,name:e.content_block.name})}else l.set(e.index,`text`);else if(e.type===`content_block_delta`){if(e.delta.type===`text_delta`)s||(yield{type:`text-delta`,delta:e.delta.text});else if(e.delta.type===`input_json_delta`){let t=l.get(e.index);t===`json-tool`?yield{type:`text-delta`,delta:e.delta.partial_json}:s||(yield{type:`tool_call_delta`,index:e.index,argumentsDelta:e.delta.partial_json})}}else if(e.type===`message_delta`){let t=e.usage?.output_tokens??0,n=e.usage?.output_tokens_details?.thinking_tokens;yield{type:`usage`,usage:{prompt_tokens:u,completion_tokens:t,total_tokens:u+t,...n===void 0?{}:{completion_tokens_details:{reasoning_tokens:n}}}}}else e.type===`ping`&&(yield{type:`ping`});if(s&&!d)throw new b(`Anthropic did not return the required structured output tool "${s}".`,`validation`)}}}}}function lt(e){let t=e=>e.role===`user`&&Array.isArray(e.content)&&e.content.length>0&&e.content.every(e=>e.type===`tool_result`),n=[];for(let r of e){let e=n.at(-1);t(r)&&e&&t(e)?e.content.push(...r.content):n.push(r)}return n}function ut(e){if(e.role===`tool`)return{role:`user`,content:[{type:`tool_result`,tool_use_id:e.tool_call_id,content:e.content,...e.is_error?{is_error:!0}:{}}]};if(e.role===`assistant`&&e.tool_calls?.length){let t=[];e.content&&t.push({type:`text`,text:e.content});for(let n of e.tool_calls){let e;try{e=n.function.arguments.trim()?JSON.parse(n.function.arguments):{}}catch(e){throw new b(`Assistant tool call "${n.function.name}" (${n.id}) has arguments that are not valid JSON.`,`validation`,{cause:e})}if(e===null||Array.isArray(e)||typeof e!=`object`)throw new b(`Assistant tool call "${n.function.name}" (${n.id}) arguments must be a JSON object.`,`validation`);t.push({type:`tool_use`,id:n.id,name:n.function.name,input:e})}return{role:`assistant`,content:t}}return{role:e.role,content:Array.isArray(e.content)?rt(e.content):e.content??``}}function dt(e){return e.map(e=>e.type===`image`?{inlineData:{mimeType:q(e.mimeType),data:e.data}}:{text:e.text})}function ft(e){return!e||e===`auto`?{functionCallingConfig:{mode:`AUTO`}}:e===`none`?{functionCallingConfig:{mode:`NONE`}}:e===`required`?{functionCallingConfig:{mode:`ANY`}}:{functionCallingConfig:{mode:`ANY`,allowedFunctionNames:[e.function.name]}}}function pt(e){if(e.role===`tool`)return{role:`user`,parts:[{functionResponse:{name:e.tool_call_id,response:ht(e.content)}}]};if(e.role===`assistant`&&e.tool_calls?.length){let t=[];return typeof e.content==`string`&&e.content&&t.push({text:e.content}),t.push(...e.tool_calls.map(e=>({functionCall:{name:e.function.name,args:mt(e.function.arguments,e.function.name)}}))),{role:`model`,parts:t}}return{role:e.role===`assistant`?`model`:`user`,parts:Array.isArray(e.content)?dt(e.content):[{text:e.content??``}]}}function mt(e,t){let n;try{n=e.trim()?JSON.parse(e):{}}catch(e){throw new b(`Tool call "${t}" arguments are not valid JSON.`,`parse`,{cause:e,code:`tool_arguments_parse_failed`})}if(!n||Array.isArray(n)||typeof n!=`object`)throw new b(`Tool call "${t}" arguments must be a JSON object.`,`validation`);return n}function ht(e){let t;try{t=e.trim()?JSON.parse(e):``}catch{t=e}return t&&!Array.isArray(t)&&typeof t==`object`?t:{output:t}}function gt(e){let t=e=>e.role===`user`&&e.parts.length>0&&e.parts.every(e=>`functionResponse`in e),n=[];for(let r of e){let e=n.at(-1);t(r)&&e&&t(e)?e.parts.push(...r.parts):n.push(r)}return n}function _t(e,t,n){let r=e.messages.find(e=>e.role===`system`),i=e.messages.filter(e=>e.role===`user`||e.role===`assistant`||e.role===`tool`),a=!!e.response_format,o={...e.temperature===void 0?{}:{temperature:e.temperature},maxOutputTokens:e.max_tokens,...r?{systemInstruction:{parts:[{text:r.content}]}}:{}};if(a&&(o.responseMimeType=`application/json`),e.response_format?.type===`json_schema`){let{schema:t,description:n}=e.response_format.json_schema;o.responseSchema={...t,...n?{description:n}:{}}}if(e.tools?.length&&(o.tools=[{functionDeclarations:e.tools.map(e=>({name:e.function.name,description:e.function.description,parameters:e.function.parameters}))}],o.toolConfig=ft(e.tool_choice)),nt(e.model,n)){let n=e.reasoning_effort??(e.budget_tokens===void 0?void 0:Q(e.budget_tokens,t));n!==void 0&&(o.thinkingConfig={thinkingLevel:Ze(n,e.model)})}else{let n=e.budget_tokens??(e.reasoning_effort?Z(e.reasoning_effort,t):void 0);n!==void 0&&(o.thinkingConfig={thinkingBudget:n})}return{model:e.model,contents:gt(i.map(e=>pt(e))),config:o}}function vt(e,t){let n=X(t?.reasoningEffortTokens),r=t?.thinkingLevelModels,i=e.models??e;if(typeof i.generateContent!=`function`)throw new b(`fromGemini requires a client with generateContent: pass ai.models, or the whole ai client (fromGemini(ai)).`,`invalid_params`,{code:`unsupported_capability`,issues:{capability:`generateContent`}});let a=i.generateContent.bind(i),o=typeof i.generateContentStream==`function`?i.generateContentStream.bind(i):void 0;return{chat:{completions:{async create(e,t){let i=_t(e,n,r);i.config={...i.config,abortSignal:t.signal};let o=await a(i),s=o.candidates?.[0]?.content?.parts??[],c=s.map(e=>e.text??``).join(``),l=s.filter(e=>e.functionCall),u;return l.length&&(u=l.map(e=>({id:e.functionCall.name,type:`function`,function:{name:e.functionCall.name,arguments:JSON.stringify(e.functionCall.args??{})}}))),{choices:[{message:{content:c,...u?{tool_calls:u}:{}}}],usage:{prompt_tokens:o.usageMetadata?.promptTokenCount,completion_tokens:o.usageMetadata?.candidatesTokenCount,total_tokens:o.usageMetadata?.totalTokenCount,...o.usageMetadata?.thoughtsTokenCount===void 0?{}:{completion_tokens_details:{reasoning_tokens:o.usageMetadata.thoughtsTokenCount}}}}},async*createStream(e,t){if(!o)throw new b(`stream: true requires a Gemini client with generateContentStream`,`invalid_params`,{code:`unsupported_capability`,issues:{capability:`generateContentStream`}});let i=_t(e,n,r);i.config={...i.config,abortSignal:t.signal};let a=await o(i),s=0,c;for await(let e of a){let t=e.candidates?.[0]?.content?.parts??[];for(let e of t)e.text&&(yield{type:`text-delta`,delta:e.text}),e.functionCall&&(yield{type:`tool_call_delta`,index:s,id:e.functionCall.name,name:e.functionCall.name,argumentsDelta:JSON.stringify(e.functionCall.args??{}),complete:!0},s++);e.usageMetadata&&(c=e.usageMetadata)}c&&(yield{type:`usage`,usage:{prompt_tokens:c.promptTokenCount,completion_tokens:c.candidatesTokenCount,total_tokens:c.totalTokenCount,...c.thoughtsTokenCount===void 0?{}:{completion_tokens_details:{reasoning_tokens:c.thoughtsTokenCount}}}})}}}}}function yt(e){return e.includes(`claude`)}function bt(e){switch(q(e)){case`image/png`:return`png`;case`image/jpeg`:return`jpeg`;case`image/gif`:return`gif`;case`image/webp`:return`webp`}}function xt(e){return new Uint8Array(Buffer.from(e,`base64`))}function St(e){return e.map(e=>e.type===`image`?{image:{format:bt(e.mimeType),source:{bytes:xt(e.data)}}}:{text:e.text})}function Ct(e,t){return{tools:e.map(e=>({toolSpec:{name:e.function.name,description:e.function.description,inputSchema:{json:e.function.parameters}}})),toolChoice:At(t)}}function wt(e,t,n,r,i){let a=e.messages.find(e=>e.role===`system`),o=e.messages.filter(e=>e.role===`user`||e.role===`assistant`||e.role===`tool`),s=e.response_format?.type===`json_schema`?e.response_format.json_schema:void 0,c=s?.name.trim();if(s&&!c)throw new b(`json_schema.name must not be empty.`,`validation`);let l=!!s&&J(e.model,n);if(s&&e.tools?.length&&!l)throw new b(`Bedrock model "${e.model}" is not covered by nativeStructuredOutputModels, so \`jsonSchema\` is emulated as a forced single tool call there (via \`toolConfig\`), which collides with the \`tools\` you also provided. Either drop \`tools\` or \`jsonSchema\` for this call, or pass this model in fromBedrock's \`nativeStructuredOutputModels\` option once you've confirmed it supports Converse's \`outputConfig.textFormat\`.`,`validation`);if(e.response_format?.type===`json_object`)throw new b('response_format: "json_object" is not supported on Bedrock. Converse has no field that mechanically guarantees valid JSON output for this mode, so it used to be emulated by injecting a "respond with JSON only" instruction into the system prompt, a guarantee this adapter can no longer make. Use `jsonSchema` instead, which maps to a real constraint (Converse\'s native outputConfig.textFormat on covered models, or a forced tool call otherwise).',`validation`);let u,d,f;if(s&&l){let{schema:e,description:t}=s;f={textFormat:{type:`json_schema`,structure:{jsonSchema:{schema:JSON.stringify(e),name:c,description:t}}}}}else if(s&&c){let{schema:e,description:t,strict:n}=s;u=c,d={tools:[{toolSpec:{name:u,description:t,inputSchema:{json:e},strict:n}}],toolChoice:{tool:{name:u}}}}if(e.tools?.length&&!u&&(d=Ct(e.tools,e.tool_choice)),s&&d&&t){let n=Array.isArray(t)?t.includes(e.model):t(e.model);if(!n)throw new b(`Bedrock model "${e.model}" is not listed in toolUseSupportedModels, but this call requires Converse tool use (either jsonSchema emulated as a forced tool call, or real \`tools\` sent alongside native structured output).`,`invalid_params`,{code:`unsupported_capability`,issues:{capability:`toolUseSupportedModels`}})}let p,m;if(yt(e.model)&&(e.budget_tokens!==void 0||e.reasoning_effort!==void 0)){let t=d?.toolChoice;if(Ye(t&&`tool`in t?`toolChoice forcing the "${t.tool?.name}" tool`:t&&`any`in t?`toolChoice: 'required' (Converse's "any" tool_choice)`:void 0),qe(e.model,i)){let t=e.budget_tokens??Z(e.reasoning_effort,r);Je(t,e.max_tokens),p={thinking:{type:`enabled`,budget_tokens:t}}}else{let t=e.reasoning_effort??Q(e.budget_tokens,r);p={thinking:{type:`adaptive`}},m=Xe(t)}}let h=p?void 0:e.temperature,g={modelId:e.model,messages:Mt(o.map(e=>jt(e))),system:a?.content?[{text:a.content}]:void 0,inferenceConfig:{...h===void 0?{}:{temperature:h},maxTokens:e.max_tokens},...d?{toolConfig:d}:{},...f||m?{outputConfig:{...f??{},...m?{effort:m}:{}}}:{},...p?{additionalModelRequestFields:p}:{}};return{request:g,toolName:u}}function Tt(e){return typeof e.send==`function`}function Et(e){if(`messageStart`in e)return{messageStart:e.messageStart};if(`contentBlockStart`in e)return{contentBlockStart:e.contentBlockStart};if(`contentBlockDelta`in e)return{contentBlockDelta:e.contentBlockDelta};if(`contentBlockStop`in e)return{contentBlockStop:e.contentBlockStop};if(`messageStop`in e)return{messageStop:e.messageStop};if(`metadata`in e)return{metadata:e.metadata};if(`internalServerException`in e)return{internalServerException:e.internalServerException};if(`modelStreamErrorException`in e)return{modelStreamErrorException:e.modelStreamErrorException};if(`validationException`in e)return{validationException:e.validationException};if(`throttlingException`in e)return{throttlingException:e.throttlingException};if(`serviceUnavailableException`in e)return{serviceUnavailableException:e.serviceUnavailableException}}async function*Dt(e){for await(let t of e){let e=Et(t);e&&(yield e)}}function Ot(e){let t;function n(){return t??=import(`@aws-sdk/client-bedrock-runtime`).then(e=>e,e=>{throw t=void 0,new b(`fromBedrock requires "@aws-sdk/client-bedrock-runtime" to be installed to use a raw AWS SDK client (it is not a dependency of vern-llm itself). Install it, or pass your own object with .converse()/.converseStream() methods instead.`,`validation`,{cause:e})}),t}return{converse:async(t,r)=>{let{ConverseCommand:i}=await n();return e.send(new i(t),{abortSignal:r.signal})},converseStream:async(t,r)=>{let{ConverseStreamCommand:i}=await n(),a=await e.send(new i(t),{abortSignal:r.signal});if(!a.stream)throw new b(`Bedrock ConverseStreamCommand response did not include a stream. This can happen if the request or the model doesn't actually support Converse streaming.`,`api`,{code:`server_error`});return{stream:Dt(a.stream)}}}}function kt(e,t){let n=Tt(e)?Ot(e):e,r=t?.toolUseSupportedModels,i=t?.nativeStructuredOutputModels,a=X(t?.reasoningEffortTokens),o=t?.adaptiveOnlyModels;return{supportsJsonObjectMode:!1,chat:{completions:{async create(e,t){let{request:s,toolName:c}=wt(e,r,i,a,o),l=await n.converse(s,t),u,d;if(c){let e=l.output?.message?.content?.find(e=>e.toolUse?.name===c);u=e?.toolUse?JSON.stringify(e.toolUse.input):``}else{let e=l.output?.message?.content??[];u=e.map(e=>e.text??``).join(``);let t=e.filter(e=>!!e.toolUse);t.length&&(d=t.map((e,t)=>{let n=e.toolUse;if(!n.name)throw new b(`Bedrock returned a toolUse block without a name at index ${t}.`,`validation`);return{id:n.toolUseId??`${n.name}_${t}`,type:`function`,function:{name:n.name,arguments:JSON.stringify(n.input??{})}}}))}return{choices:[{message:{content:u,...d?{tool_calls:d}:{}}}],usage:{prompt_tokens:l.usage?.inputTokens,completion_tokens:l.usage?.outputTokens,total_tokens:l.usage?.totalTokens}}},async*createStream(e,t){if(!n.converseStream)throw new b(`stream: true requires a Bedrock client with converseStream`,`invalid_params`,{code:`unsupported_capability`,issues:{capability:`converseStream`}});let{request:s,toolName:c}=wt(e,r,i,a,o),{stream:l}=await n.converseStream(s,t),u=new Map;for await(let e of l)if(`contentBlockStart`in e){let{contentBlockIndex:t,start:n}=e.contentBlockStart;if(n?.toolUse){let e=n.toolUse.name===c?`json-tool`:`tool_use`;u.set(t,e),e===`tool_use`&&!c&&(yield{type:`tool_call_delta`,index:t,id:n.toolUse.toolUseId,name:n.toolUse.name})}else u.set(t,`text`)}else if(`contentBlockDelta`in e){let{contentBlockIndex:t,delta:n}=e.contentBlockDelta;if(n&&`text`in n&&n.text!==void 0&&!c)yield{type:`text-delta`,delta:n.text};else if(n&&`toolUse`in n&&n.toolUse?.input!==void 0){let e=u.get(t);e===`json-tool`?yield{type:`text-delta`,delta:n.toolUse.input}:c||(yield{type:`tool_call_delta`,index:t,argumentsDelta:n.toolUse.input})}}else if(`metadata`in e&&e.metadata.usage)yield{type:`usage`,usage:{prompt_tokens:e.metadata.usage.inputTokens,completion_tokens:e.metadata.usage.outputTokens,total_tokens:e.metadata.usage.totalTokens}};else if(`throttlingException`in e)throw new b(e.throttlingException.message??`Bedrock throttled the request mid-stream`,`api`,{status:429,code:`provider_rate_limited`});else if(`validationException`in e)throw new b(e.validationException.message??`Bedrock rejected the request mid-stream`,`validation`);else if(`internalServerException`in e||`serviceUnavailableException`in e||`modelStreamErrorException`in e){let t=`internalServerException`in e&&e.internalServerException.message||`serviceUnavailableException`in e&&e.serviceUnavailableException.message||`modelStreamErrorException`in e&&e.modelStreamErrorException.message||`Bedrock reported a mid-stream error`,n=`modelStreamErrorException`in e&&e.modelStreamErrorException.originalStatusCode||`serviceUnavailableException`in e&&503||500;throw new b(t,`api`,{status:n,code:n>=500?`server_error`:void 0})}}}}}}function At(e){if(!e||e===`auto`)return{auto:{}};if(e===`required`)return{any:{}};if(e===`none`)throw new b("'none' is not supported by fromBedrock: Bedrock Converse has no `tool_choice` equivalent to forbidding tool use while tools are still offered. Omit `tools` entirely for this call instead.",`invalid_params`,{code:`unsupported_capability`,issues:{capability:`toolChoice: 'none'`}});return{tool:{name:e.function.name}}}function jt(e){if(e.role===`tool`)return{role:`user`,content:[{toolResult:{toolUseId:e.tool_call_id,content:[{text:e.content}],status:e.is_error?`error`:`success`}}]};if(e.role===`assistant`&&e.tool_calls?.length){let t=[];e.content&&t.push({text:e.content});for(let n of e.tool_calls){let e;if(!n.function.arguments.trim())e={};else try{e=JSON.parse(n.function.arguments)}catch(e){throw new b(`Assistant tool call "${n.function.name}" (${n.id}) has arguments that are not valid JSON.`,`validation`,{cause:e})}t.push({toolUse:{toolUseId:n.id,name:n.function.name,input:e}})}return{role:`assistant`,content:t}}return{role:e.role,content:Array.isArray(e.content)?St(e.content):[{text:e.content??``}]}}function Mt(e){let t=e=>e.role===`user`&&e.content.length>0&&e.content.every(e=>`toolResult`in e),n=[];for(let r of e){let e=n.at(-1);t(r)&&e&&t(e)?e.content.push(...r.content):n.push(r)}return n}async function*Nt(e){let t=e.getReader();try{for(;;){let{done:e,value:n}=await t.read();if(e)return;n&&(yield n)}}finally{try{await t.cancel()}catch{}t.releaseLock()}}async function Pt(e,t){let n=await fetch(e,t);if(!n.ok){let e=await n.text().catch(()=>``),t=Error(`Fetch adapter stream request failed (${n.status}): ${e.slice(0,500)}`);throw t.status=n.status,t.headers=n.headers,t}if(!n.body)throw Error(`Fetch adapter stream request received a response with no body.`);return Nt(n.body)}async function Ft(e,t,n){let r=typeof e.url==`function`?e.url(t):e.url,i=typeof e.headers==`function`?await e.headers():e.headers,a=e.method??`POST`,o=![`GET`,`HEAD`].includes(a.toUpperCase());return{url:r,method:a,headers:o?{"Content-Type":`application/json`,...i}:{...i},...o?{body:JSON.stringify(n)}:{}}}function It(e){return{chat:{completions:{async create(t,n){let{url:r,method:i,headers:a,body:o}=await Ft(e,t,e.mapRequest(t)),s=e.request??fetch,c=await s(r,{method:i,headers:a,body:o,signal:n.signal});if(!c.ok){let e=await c.text().catch(()=>``),t=Error(`Fetch adapter request failed (${c.status}): ${e.slice(0,500)}`);throw t.status=c.status,t.headers=c.headers,t}let l=await c.json(),{content:u,usage:d,toolCalls:f}=e.mapResponse(l),p=f?.length?f.map(e=>({id:e.id,type:`function`,function:{name:e.name,arguments:e.arguments}})):void 0;return{choices:[{message:{content:u,...p?{tool_calls:p}:{}}}],usage:d?{prompt_tokens:d.promptTokens,completion_tokens:d.completionTokens,total_tokens:d.totalTokens}:void 0}},async*createStream(t,n){if(!e.mapStreamEvent)throw new b(`stream: true requires mapStreamEvent to be configured on fromFetch`,`invalid_params`,{code:`unsupported_capability`,issues:{capability:`mapStreamEvent`}});if(e.request&&!e.requestStream)throw new b("`stream: true` requires `requestStream` to be configured on fromFetch when a custom `request` transport is set. `requestStream` does not fall back to `request` (it needs an async-iterable byte stream, which `RequestLike`'s buffered `ResponseLike` has no way to provide), without it, `stream: true` would silently use plain native `fetch` instead of your configured transport. Add a `requestStream` that opens the same connection your `request` does, or omit `request` if native `fetch` is fine for both.",`invalid_params`,{code:`unsupported_capability`,issues:{capability:`requestStream`}});let{url:r,method:i,headers:a,body:o}=await Ft(e,t,e.mapRequest(t)),s=e.requestStream??Pt,c=e.parseStreamFrames??He,l=await s(r,{method:i,headers:a,body:o,signal:n.signal});for await(let t of c(l)){if(t===G){yield{type:`ping`};continue}let n=e.mapStreamEvent(t);if(!n)continue;Array.isArray(n)?yield*n:yield n}}}}}}function Lt(e,t){if(e.budget_tokens===void 0)return e;let{budget_tokens:n,...r}=e;return r.reasoning_effort===void 0?{...r,reasoning_effort:Q(n,t)}:r}function Rt(e){return e.map(e=>e.type===`image`?{type:`image_url`,image_url:{url:`data:${q(e.mimeType)};base64,${e.data}`}}:{type:`text`,text:e.text})}function zt(e){return e.messages.map(e=>{if(e.role===`user`&&Array.isArray(e.content))return{...e,content:Rt(e.content)};if(e.role===`tool`){let{is_error:t,...n}=e;return n}return e})}function*Bt(e){let t=e.choices?.[0]?.delta;if(t?.content&&(yield{type:`text-delta`,delta:t.content}),t?.tool_calls?.length)for(let e of t.tool_calls)yield{type:`tool_call_delta`,index:e.index,id:e.id,name:e.function?.name,argumentsDelta:e.function?.arguments};e.usage&&(yield{type:`usage`,usage:e.usage})}function $(e,t={}){let n=e,{supportsStreamUsage:r=!0}=t,i=X(t.reasoningEffortTokens),a=n.chat.completions.create.bind(n.chat.completions);return{chat:{completions:{async create(e,t){let r=zt(e);return n.chat.completions.create(Lt({...e,messages:r},i),t)},async*createStream(e,t){let n=zt(e),o=await a(Lt({...e,messages:n,stream:!0,...r?{stream_options:{include_usage:!0}}:{}},i),t);for await(let e of o)yield*Bt(e)}}}}}const Vt=$,Ht=$,Ut=$,Wt=$,Gt=$,Kt=$,qt=$,Jt=$,Yt=$,Xt=$,Zt=$,Qt=$,$t=$,en=$,tn=$,nn=$,rn=$,an=$,on=$,sn=$,cn=$,ln=$,un=$,dn=$,fn=$,pn=$,mn=$,hn=$,gn=$,_n=$,vn=$,yn=$,bn=$,xn=$,Sn=$,Cn=$,wn=$,Tn=$;exports.CircuitBreaker=fe,exports.ConsoleLogger=Fe,exports.FallbackExhaustedError=E,exports.InMemoryCacheAdapter=S,exports.LLMError=b,exports.NormalizedCacheAdapter=C,exports.RateLimiter=Re,exports.SSE_PING=G,exports.TieredCacheAdapter=w,exports.VernLLM=ze,exports.defaultEstimateTokens=Ie,exports.defaultFallbackOn=ne,exports.defineCachedCallParams=Ve,exports.defineCallParams=Be,exports.from01AI=Tn,exports.fromAnthropic=ct,exports.fromAnyscale=bn,exports.fromAtlasCloud=wn,exports.fromBaseten=dn,exports.fromBedrock=kt,exports.fromCerebras=Gt,exports.fromCloudflareWorkersAI=cn,exports.fromDeepInfra=Zt,exports.fromDeepSeek=Wt,exports.fromFeatherless=fn,exports.fromFetch=It,exports.fromFireworks=qt,exports.fromFriendli=pn,exports.fromGemini=vt,exports.fromGroq=Ht,exports.fromHyperbolic=$t,exports.fromInferenceNet=Sn,exports.fromInfermatic=Cn,exports.fromLMStudio=nn,exports.fromLambdaLabs=vn,exports.fromLepton=xn,exports.fromMiniMax=_n,exports.fromMistral=Ut,exports.fromMoonshot=en,exports.fromNebius=ln,exports.fromNovita=Qt,exports.fromNvidiaNIM=on,exports.fromOllama=Jt,exports.fromOpenAI=Vt,exports.fromOpenAICompatible=$,exports.fromOpenRouter=Yt,exports.fromParasail=hn,exports.fromPerplexity=Xt,exports.fromSambaNova=un,exports.fromSiliconFlow=mn,exports.fromSnowflakeCortex=yn,exports.fromStepFun=gn,exports.fromTogether=Kt,exports.fromVLLM=rn,exports.fromVercelAIGateway=sn,exports.fromXAI=an,exports.fromZhipu=tn,exports.hasIssues=ee,exports.isFallbackExhaustedError=re,exports.isLLMError=x,exports.isToolCallResult=T,exports.parseSseStream=He;
14
+ `);if(r===`[DONE]`)return U;try{return JSON.parse(r)}catch(e){throw new b(`Invalid JSON in SSE frame: ${r.slice(0,200)}`,`parse`,{cause:e,code:`stream_frame_invalid`})}}const Ke=[`image/png`,`image/jpeg`,`image/gif`,`image/webp`];function q(e){if(Ke.includes(e))return e;throw new b(`Unsupported image mimeType "${e}": expected one of ${Ke.join(`, `)}`,`invalid_params`)}function qe(e,t){return t?Array.isArray(t)?t.includes(e):t(e):!1}const J={minimal:1024,low:4096,medium:16e3,high:32e3};function Y(e){if(!e)return J;let t={...J,...e};if(!(t.minimal<t.low&&t.low<t.medium&&t.medium<t.high))throw new b(`reasoningEffortTokens must keep tiers in strictly ascending order (minimal < low < medium < high), got ${JSON.stringify(t)}. An out-of-order override doesn't just misrank tiers, it can make some of them unreachable.`,`invalid_params`);return t}function X(e,t=J){return t[e]}function Z(e,t=J){return e<=t.minimal?`minimal`:e<=t.low?`low`:e<=t.medium?`medium`:`high`}function Je(e){let t=/opus-(\d+)(?:-(\d+))?/.exec(e);if(!t)return null;let n=t[2],r=n===void 0||n.length>=8?0:Number(n);return[Number(t[1]),r]}function Ye(e){let t=Je(e);if(t){let[e,n]=t;return e>4||e===4&&n>=7}return[`sonnet-5`,`fable-5`,`mythos`].some(t=>e.includes(t))}function Xe(e,t){return Ye(e)?!0:t?Array.isArray(t)?t.includes(e):t(e):!1}function Ze(e,t){return!Xe(e,t)}function Qe(e,t){if(e<1024)throw new b(`budgetTokens (${e}) is below Anthropic's minimum of 1024. Raise budgetTokens, or use a reasoningEffort tier of 'low' or above with the default conversion table.`,`invalid_params`);if(e>=t)throw new b(`budgetTokens (${e}) must be less than maxTokens (${t}); the thinking budget and the reply share the same max_tokens ceiling on Anthropic. Raise maxTokens, or lower budgetTokens/reasoningEffort.`,`invalid_params`)}function $e(e){if(e)throw new b(`budgetTokens/reasoningEffort was set alongside ${e}. Anthropic rejects thinking combined with a tool_choice that forces tool use, the model has to be able to reply with plain text for thinking to run. Use toolChoice: 'auto' (or omit toolChoice) for this call, or drop budgetTokens/reasoningEffort for it.`,`invalid_params`)}function et(e){return e===`minimal`?`low`:e}function tt(e,t){return rt(t,e.toUpperCase())}function nt(e){let t=/gemini-\d+\.(\d+)/.exec(e);return t?Number(t[1]):0}function rt(e,t){if(!e.includes(`pro`))return t;let n=it(e);if(n===null||n<3)return t;let r=nt(e);return r===0?t===`HIGH`?`HIGH`:`LOW`:t===`MINIMAL`?`LOW`:t}function it(e){let t=/gemini-(\d+)/.exec(e);return t?Number(t[1]):null}function at(e){let t=it(e);return t!==null&&t>=3}function ot(e,t){return at(e)?!0:t?Array.isArray(t)?t.includes(e):t(e):!1}function st(e){return e.map(e=>e.type===`image`?{type:`image`,source:{type:`base64`,media_type:q(e.mimeType),data:e.data}}:{type:`text`,text:e.text})}function ct(e,t){if(e.type!==`object`)throw new b(`Tool "${t}"'s schema must have "type": "object" (Anthropic requires object-shaped tool parameters).`,`validation`);return e}function lt(e){return!e||e===`auto`?{type:`auto`}:e===`none`?{type:`none`}:e===`required`?{type:`any`}:{type:`tool`,name:e.function.name}}function ut(e,t){return{tools:e.map(e=>({name:e.function.name,description:e.function.description,input_schema:ct(e.function.parameters,e.function.name)})),toolChoice:lt(t)}}function Q(e,t,n,r){let i=e.messages.find(e=>e.role===`system`),a=e.messages.filter(e=>e.role===`user`||e.role===`assistant`||e.role===`tool`),o=e.response_format?.type===`json_schema`?e.response_format.json_schema:void 0,s=o?.name.trim();if(o&&!s)throw new b(`json_schema.name must not be empty.`,`validation`);let c=!!o&&qe(e.model,t);if(o&&e.tools?.length&&!c)throw new b(`Anthropic model "${e.model}" is not covered by nativeStructuredOutputModels, so \`jsonSchema\` is emulated as a forced single tool call there, which collides with the \`tools\` you also provided. Either drop \`tools\` or \`jsonSchema\` for this call, or pass this model in fromAnthropic's \`nativeStructuredOutputModels\` option once you've confirmed it supports Anthropic's \`output_config.format\`.`,`invalid_params`,{code:`unsupported_capability`,issues:{capability:`tools_with_json_schema`}});if(e.response_format?.type===`json_object`)throw new b('response_format: "json_object" is not supported on Anthropic. Unlike OpenAI, Anthropic has no API-level field that mechanically guarantees valid JSON output for this mode, so it used to be emulated by injecting a "respond with JSON only" instruction into the system prompt, a guarantee this adapter can no longer make. Use `jsonSchema` instead, which maps to a real API-level constraint (Anthropic\'s native output_config.format on covered models, or a forced single tool call otherwise).',`validation`);let l,u,d,f;if(o&&c)u={type:`json_schema`,schema:o.schema},e.tools?.length&&({tools:d,toolChoice:f}=ut(e.tools,e.tool_choice));else if(o&&s){let{schema:e,description:t,strict:n}=o;l=s,d=[{name:l,description:t,input_schema:ct(e,l),strict:n}],f={type:`tool`,name:l}}!o&&e.tools?.length&&({tools:d,toolChoice:f}=ut(e.tools,e.tool_choice));let p,m;if(e.budget_tokens!==void 0||e.reasoning_effort!==void 0)if($e(f?.type===`tool`?`toolChoice forcing the "${f.name}" tool`:f?.type===`any`?`toolChoice: 'required' (Anthropic's "any" tool_choice)`:void 0),Ze(e.model,r)){let t=e.budget_tokens??X(e.reasoning_effort,n);Qe(t,e.max_tokens),p={type:`enabled`,budget_tokens:t}}else{let t=e.reasoning_effort??Z(e.budget_tokens,n);p={type:`adaptive`},m=et(t)}let h=i?.content,g=p?void 0:e.temperature,_={model:e.model,max_tokens:e.max_tokens,...g===void 0?{}:{temperature:g},system:h||void 0,messages:ft(a.map(e=>pt(e))),...d?{tools:d,tool_choice:f}:{},...u||m?{output_config:{...u?{format:u}:{},...m?{effort:m}:{}}}:{},...p?{thinking:p}:{}};return{body:_,toolName:l}}function dt(e,t){let n=t?.nativeStructuredOutputModels,r=Y(t?.reasoningEffortTokens),i=t?.adaptiveOnlyModels,a=e.messages.create.bind(e.messages);return{supportsJsonObjectMode:!1,chat:{completions:{async create(t,a){let{body:o,toolName:s}=Q(t,n,r,i),c=await e.messages.create(o,a),l,u;if(s){let e=c.content.find(e=>e.type===`tool_use`&&e.name===s);if(!e)throw new b(`Anthropic did not return the required structured output tool "${s}".`,`validation`);if(!e.input||typeof e.input!=`object`||Array.isArray(e.input))throw new b(`Anthropic returned invalid structured output for tool "${s}". Expected an object.`,`validation`);l=JSON.stringify(e.input)}else{l=c.content.filter(e=>e.type===`text`).map(e=>e.text??``).join(``);let e=c.content.filter(e=>e.type===`tool_use`);e.length&&(u=e.map(e=>({id:e.id,type:`function`,function:{name:e.name,arguments:JSON.stringify(e.input??{})}})))}return{choices:[{message:{content:l,...u?{tool_calls:u}:{}}}],usage:{prompt_tokens:c.usage?.input_tokens,completion_tokens:c.usage?.output_tokens,total_tokens:(c.usage?.input_tokens??0)+(c.usage?.output_tokens??0),...c.usage?.output_tokens_details?.thinking_tokens===void 0?{}:{completion_tokens_details:{reasoning_tokens:c.usage.output_tokens_details.thinking_tokens}}}}},async*createStream(e,t){let{body:o,toolName:s}=Q(e,n,r,i),c=await a({...o,stream:!0},t),l=new Map,u=0,d=!1;for await(let e of c)if(e.type===`message_start`)u=e.message.usage?.input_tokens??0;else if(e.type===`content_block_start`)if(e.content_block.type===`tool_use`){let t=e.content_block.name===s?`json-tool`:`tool_use`;l.set(e.index,t),t===`json-tool`?d=!0:s||(yield{type:`tool_call_delta`,index:e.index,id:e.content_block.id,name:e.content_block.name})}else l.set(e.index,`text`);else if(e.type===`content_block_delta`){if(e.delta.type===`text_delta`)s||(yield{type:`text-delta`,delta:e.delta.text});else if(e.delta.type===`input_json_delta`){let t=l.get(e.index);t===`json-tool`?yield{type:`text-delta`,delta:e.delta.partial_json}:s||(yield{type:`tool_call_delta`,index:e.index,argumentsDelta:e.delta.partial_json})}}else if(e.type===`message_delta`){let t=e.usage?.output_tokens??0,n=e.usage?.output_tokens_details?.thinking_tokens;yield{type:`usage`,usage:{prompt_tokens:u,completion_tokens:t,total_tokens:u+t,...n===void 0?{}:{completion_tokens_details:{reasoning_tokens:n}}}}}else e.type===`ping`&&(yield{type:`ping`});if(s&&!d)throw new b(`Anthropic did not return the required structured output tool "${s}".`,`validation`)}}}}}function ft(e){let t=e=>e.role===`user`&&Array.isArray(e.content)&&e.content.length>0&&e.content.every(e=>e.type===`tool_result`),n=[];for(let r of e){let e=n.at(-1);t(r)&&e&&t(e)?e.content.push(...r.content):n.push(r)}return n}function pt(e){if(e.role===`tool`)return{role:`user`,content:[{type:`tool_result`,tool_use_id:e.tool_call_id,content:e.content,...e.is_error?{is_error:!0}:{}}]};if(e.role===`assistant`&&e.tool_calls?.length){let t=[];e.content&&t.push({type:`text`,text:e.content});for(let n of e.tool_calls){let e;try{e=n.function.arguments.trim()?JSON.parse(n.function.arguments):{}}catch(e){throw new b(`Assistant tool call "${n.function.name}" (${n.id}) has arguments that are not valid JSON.`,`validation`,{cause:e})}if(e===null||Array.isArray(e)||typeof e!=`object`)throw new b(`Assistant tool call "${n.function.name}" (${n.id}) arguments must be a JSON object.`,`validation`);t.push({type:`tool_use`,id:n.id,name:n.function.name,input:e})}return{role:`assistant`,content:t}}return{role:e.role,content:Array.isArray(e.content)?st(e.content):e.content??``}}function mt(e){return e.map(e=>e.type===`image`?{inlineData:{mimeType:q(e.mimeType),data:e.data}}:{text:e.text})}function ht(e,t){return`${e}#${t}`}function gt(e){return!e||e===`auto`?{functionCallingConfig:{mode:`AUTO`}}:e===`none`?{functionCallingConfig:{mode:`NONE`}}:e===`required`?{functionCallingConfig:{mode:`ANY`}}:{functionCallingConfig:{mode:`ANY`,allowedFunctionNames:[e.function.name]}}}function _t(e){let t=new Map;for(let n of e)if(n.role===`assistant`)for(let e of n.tool_calls??[])t.set(e.id,e.function.name);return t}function vt(e,t){if(e.role===`tool`)return{role:`user`,parts:[{functionResponse:{id:e.tool_call_id,name:t.get(e.tool_call_id)??e.tool_call_id,response:bt(e.content)}}]};if(e.role===`assistant`&&e.tool_calls?.length){let t=[];return typeof e.content==`string`&&e.content&&t.push({text:e.content}),t.push(...e.tool_calls.map(e=>({functionCall:{id:e.id,name:e.function.name,args:yt(e.function.arguments,e.function.name)}}))),{role:`model`,parts:t}}return{role:e.role===`assistant`?`model`:`user`,parts:Array.isArray(e.content)?mt(e.content):[{text:e.content??``}]}}function yt(e,t){let n;try{n=e.trim()?JSON.parse(e):{}}catch(e){throw new b(`Tool call "${t}" arguments are not valid JSON.`,`parse`,{cause:e,code:`tool_arguments_parse_failed`})}if(!n||Array.isArray(n)||typeof n!=`object`)throw new b(`Tool call "${t}" arguments must be a JSON object.`,`validation`);return n}function bt(e){let t;try{t=e.trim()?JSON.parse(e):``}catch{t=e}return t&&!Array.isArray(t)&&typeof t==`object`?t:{output:t}}function xt(e){let t=e=>e.role===`user`&&e.parts.length>0&&e.parts.every(e=>`functionResponse`in e),n=[];for(let r of e){let e=n.at(-1);t(r)&&e&&t(e)?e.parts.push(...r.parts):n.push(r)}return n}function St(e,t,n){let r=e.messages.find(e=>e.role===`system`),i=e.messages.filter(e=>e.role===`user`||e.role===`assistant`||e.role===`tool`),a=!!e.response_format,o={...e.temperature===void 0?{}:{temperature:e.temperature},maxOutputTokens:e.max_tokens,...r?{systemInstruction:{parts:[{text:r.content}]}}:{}};if(a&&(o.responseMimeType=`application/json`),e.response_format?.type===`json_schema`){let{schema:t,description:n}=e.response_format.json_schema;o.responseSchema={...t,...n?{description:n}:{}}}if(e.tools?.length&&(o.tools=[{functionDeclarations:e.tools.map(e=>({name:e.function.name,description:e.function.description,parameters:e.function.parameters}))}],o.toolConfig=gt(e.tool_choice)),ot(e.model,n)){let n=e.reasoning_effort??(e.budget_tokens===void 0?void 0:Z(e.budget_tokens,t));n!==void 0&&(o.thinkingConfig={thinkingLevel:tt(n,e.model)})}else{let n=e.budget_tokens??(e.reasoning_effort?X(e.reasoning_effort,t):void 0);n!==void 0&&(o.thinkingConfig={thinkingBudget:n})}let s=_t(i);return{model:e.model,contents:xt(i.map(e=>vt(e,s))),config:o}}function Ct(e,t){let n=Y(t?.reasoningEffortTokens),r=t?.thinkingLevelModels,i=e.models??e;if(typeof i.generateContent!=`function`)throw new b(`fromGemini requires a client with generateContent: pass ai.models, or the whole ai client (fromGemini(ai)).`,`invalid_params`,{code:`unsupported_capability`,issues:{capability:`generateContent`}});let a=i.generateContent.bind(i),o=typeof i.generateContentStream==`function`?i.generateContentStream.bind(i):void 0;return{chat:{completions:{async create(e,t){let i=St(e,n,r);i.config={...i.config,abortSignal:t.signal};let o=await a(i),s=o.candidates?.[0]?.content?.parts??[],c=s.map(e=>e.text??``).join(``),l=s.filter(e=>e.functionCall),u;if(l.length){let e=new Map;u=l.map(t=>{let n=t.functionCall.name,r=t.functionCall.id,i=e.get(n)??0;return e.set(n,i+1),{id:r??ht(n,i),type:`function`,function:{name:n,arguments:JSON.stringify(t.functionCall.args??{})}}})}return{choices:[{message:{content:c,...u?{tool_calls:u}:{}}}],usage:{prompt_tokens:o.usageMetadata?.promptTokenCount,completion_tokens:o.usageMetadata?.candidatesTokenCount,total_tokens:o.usageMetadata?.totalTokenCount,...o.usageMetadata?.thoughtsTokenCount===void 0?{}:{completion_tokens_details:{reasoning_tokens:o.usageMetadata.thoughtsTokenCount}}}}},async*createStream(e,t){if(!o)throw new b(`stream: true requires a Gemini client with generateContentStream`,`invalid_params`,{code:`unsupported_capability`,issues:{capability:`generateContentStream`}});let i=St(e,n,r);i.config={...i.config,abortSignal:t.signal};let a=await o(i),s=0,c=new Map,l;for await(let e of a){let t=e.candidates?.[0]?.content?.parts??[];for(let e of t)if(e.text&&(yield{type:`text-delta`,delta:e.text}),e.functionCall){let t=e.functionCall.name,n=e.functionCall.id,r=t?c.get(t)??0:0;t&&c.set(t,r+1),yield{type:`tool_call_delta`,index:s,id:n??(t?ht(t,r):void 0),name:e.functionCall.name,argumentsDelta:JSON.stringify(e.functionCall.args??{}),complete:!0},s++}e.usageMetadata&&(l=e.usageMetadata)}l&&(yield{type:`usage`,usage:{prompt_tokens:l.promptTokenCount,completion_tokens:l.candidatesTokenCount,total_tokens:l.totalTokenCount,...l.thoughtsTokenCount===void 0?{}:{completion_tokens_details:{reasoning_tokens:l.thoughtsTokenCount}}}})}}}}}function wt(e){return e.includes(`claude`)}function Tt(e){switch(q(e)){case`image/png`:return`png`;case`image/jpeg`:return`jpeg`;case`image/gif`:return`gif`;case`image/webp`:return`webp`}}function Et(e){return new Uint8Array(Buffer.from(e,`base64`))}function Dt(e){return e.map(e=>e.type===`image`?{image:{format:Tt(e.mimeType),source:{bytes:Et(e.data)}}}:{text:e.text})}function Ot(e,t){return{tools:e.map(e=>({toolSpec:{name:e.function.name,description:e.function.description,inputSchema:{json:e.function.parameters}}})),toolChoice:Ft(t)}}function kt(e,t,n,r,i){let a=e.messages.find(e=>e.role===`system`),o=e.messages.filter(e=>e.role===`user`||e.role===`assistant`||e.role===`tool`),s=e.response_format?.type===`json_schema`?e.response_format.json_schema:void 0,c=s?.name.trim();if(s&&!c)throw new b(`json_schema.name must not be empty.`,`validation`);let l=!!s&&qe(e.model,n);if(s&&e.tools?.length&&!l)throw new b(`Bedrock model "${e.model}" is not covered by nativeStructuredOutputModels, so \`jsonSchema\` is emulated as a forced single tool call there (via \`toolConfig\`), which collides with the \`tools\` you also provided. Either drop \`tools\` or \`jsonSchema\` for this call, or pass this model in fromBedrock's \`nativeStructuredOutputModels\` option once you've confirmed it supports Converse's \`outputConfig.textFormat\`.`,`invalid_params`,{code:`unsupported_capability`,issues:{capability:`tools_with_json_schema`}});if(e.response_format?.type===`json_object`)throw new b('response_format: "json_object" is not supported on Bedrock. Converse has no field that mechanically guarantees valid JSON output for this mode, so it used to be emulated by injecting a "respond with JSON only" instruction into the system prompt, a guarantee this adapter can no longer make. Use `jsonSchema` instead, which maps to a real constraint (Converse\'s native outputConfig.textFormat on covered models, or a forced tool call otherwise).',`validation`);let u,d,f;if(s&&l){let{schema:e,description:t}=s;f={textFormat:{type:`json_schema`,structure:{jsonSchema:{schema:JSON.stringify(e),name:c,description:t}}}}}else if(s&&c){let{schema:e,description:t,strict:n}=s;u=c,d={tools:[{toolSpec:{name:u,description:t,inputSchema:{json:e},strict:n}}],toolChoice:{tool:{name:u}}}}if(e.tools?.length&&!u&&(d=Ot(e.tools,e.tool_choice)),s&&d&&t){let n=Array.isArray(t)?t.includes(e.model):t(e.model);if(!n)throw new b(`Bedrock model "${e.model}" is not listed in toolUseSupportedModels, but this call requires Converse tool use (either jsonSchema emulated as a forced tool call, or real \`tools\` sent alongside native structured output).`,`invalid_params`,{code:`unsupported_capability`,issues:{capability:`toolUseSupportedModels`}})}let p,m;if(wt(e.model)&&(e.budget_tokens!==void 0||e.reasoning_effort!==void 0)){let t=d?.toolChoice;if($e(t&&`tool`in t?`toolChoice forcing the "${t.tool?.name}" tool`:t&&`any`in t?`toolChoice: 'required' (Converse's "any" tool_choice)`:void 0),Ze(e.model,i)){let t=e.budget_tokens??X(e.reasoning_effort,r);Qe(t,e.max_tokens),p={thinking:{type:`enabled`,budget_tokens:t}}}else{let t=e.reasoning_effort??Z(e.budget_tokens,r);p={thinking:{type:`adaptive`}},m=et(t)}}let h=p?void 0:e.temperature,g={modelId:e.model,messages:Lt(o.map(e=>It(e))),system:a?.content?[{text:a.content}]:void 0,inferenceConfig:{...h===void 0?{}:{temperature:h},maxTokens:e.max_tokens},...d?{toolConfig:d}:{},...f||m?{outputConfig:{...f??{},...m?{effort:m}:{}}}:{},...p?{additionalModelRequestFields:p}:{}};return{request:g,toolName:u}}function At(e){return typeof e.send==`function`}function jt(e){if(`messageStart`in e)return{messageStart:e.messageStart};if(`contentBlockStart`in e)return{contentBlockStart:e.contentBlockStart};if(`contentBlockDelta`in e)return{contentBlockDelta:e.contentBlockDelta};if(`contentBlockStop`in e)return{contentBlockStop:e.contentBlockStop};if(`messageStop`in e)return{messageStop:e.messageStop};if(`metadata`in e)return{metadata:e.metadata};if(`internalServerException`in e)return{internalServerException:e.internalServerException};if(`modelStreamErrorException`in e)return{modelStreamErrorException:e.modelStreamErrorException};if(`validationException`in e)return{validationException:e.validationException};if(`throttlingException`in e)return{throttlingException:e.throttlingException};if(`serviceUnavailableException`in e)return{serviceUnavailableException:e.serviceUnavailableException}}async function*Mt(e){for await(let t of e){let e=jt(t);e&&(yield e)}}function Nt(e){let t;function n(){return t??=import(`@aws-sdk/client-bedrock-runtime`).then(e=>e,e=>{throw t=void 0,new b(`fromBedrock requires "@aws-sdk/client-bedrock-runtime" to be installed to use a raw AWS SDK client (it is not a dependency of vern-llm itself). Install it, or pass your own object with .converse()/.converseStream() methods instead.`,`validation`,{cause:e})}),t}return{converse:async(t,r)=>{let{ConverseCommand:i}=await n();return e.send(new i(t),{abortSignal:r.signal})},converseStream:async(t,r)=>{let{ConverseStreamCommand:i}=await n(),a=await e.send(new i(t),{abortSignal:r.signal});if(!a.stream)throw new b(`Bedrock ConverseStreamCommand response did not include a stream. This can happen if the request or the model doesn't actually support Converse streaming.`,`api`,{code:`server_error`});return{stream:Mt(a.stream)}}}}function Pt(e,t){let n=At(e)?Nt(e):e,r=t?.toolUseSupportedModels,i=t?.nativeStructuredOutputModels,a=Y(t?.reasoningEffortTokens),o=t?.adaptiveOnlyModels;return{supportsJsonObjectMode:!1,chat:{completions:{async create(e,t){let{request:s,toolName:c}=kt(e,r,i,a,o),l=await n.converse(s,t),u,d;if(c){let e=l.output?.message?.content?.find(e=>e.toolUse?.name===c);u=e?.toolUse?JSON.stringify(e.toolUse.input):``}else{let e=l.output?.message?.content??[];u=e.map(e=>e.text??``).join(``);let t=e.filter(e=>!!e.toolUse);t.length&&(d=t.map((e,t)=>{let n=e.toolUse;if(!n.name)throw new b(`Bedrock returned a toolUse block without a name at index ${t}.`,`validation`);return{id:n.toolUseId??`${n.name}_${t}`,type:`function`,function:{name:n.name,arguments:JSON.stringify(n.input??{})}}}))}return{choices:[{message:{content:u,...d?{tool_calls:d}:{}}}],usage:{prompt_tokens:l.usage?.inputTokens,completion_tokens:l.usage?.outputTokens,total_tokens:l.usage?.totalTokens}}},async*createStream(e,t){if(!n.converseStream)throw new b(`stream: true requires a Bedrock client with converseStream`,`invalid_params`,{code:`unsupported_capability`,issues:{capability:`converseStream`}});let{request:s,toolName:c}=kt(e,r,i,a,o),{stream:l}=await n.converseStream(s,t),u=new Map;for await(let e of l)if(`contentBlockStart`in e){let{contentBlockIndex:t,start:n}=e.contentBlockStart;if(n?.toolUse){let e=n.toolUse.name===c?`json-tool`:`tool_use`;u.set(t,e),e===`tool_use`&&!c&&(yield{type:`tool_call_delta`,index:t,id:n.toolUse.toolUseId,name:n.toolUse.name})}else u.set(t,`text`)}else if(`contentBlockDelta`in e){let{contentBlockIndex:t,delta:n}=e.contentBlockDelta;if(n&&`text`in n&&n.text!==void 0&&!c)yield{type:`text-delta`,delta:n.text};else if(n&&`toolUse`in n&&n.toolUse?.input!==void 0){let e=u.get(t);e===`json-tool`?yield{type:`text-delta`,delta:n.toolUse.input}:c||(yield{type:`tool_call_delta`,index:t,argumentsDelta:n.toolUse.input})}}else if(`metadata`in e&&e.metadata.usage)yield{type:`usage`,usage:{prompt_tokens:e.metadata.usage.inputTokens,completion_tokens:e.metadata.usage.outputTokens,total_tokens:e.metadata.usage.totalTokens}};else if(`throttlingException`in e)throw new b(e.throttlingException.message??`Bedrock throttled the request mid-stream`,`api`,{status:429,code:`provider_rate_limited`});else if(`validationException`in e)throw new b(e.validationException.message??`Bedrock rejected the request mid-stream`,`validation`);else if(`internalServerException`in e||`serviceUnavailableException`in e||`modelStreamErrorException`in e){let t=`internalServerException`in e&&e.internalServerException.message||`serviceUnavailableException`in e&&e.serviceUnavailableException.message||`modelStreamErrorException`in e&&e.modelStreamErrorException.message||`Bedrock reported a mid-stream error`,n=`modelStreamErrorException`in e&&e.modelStreamErrorException.originalStatusCode||`serviceUnavailableException`in e&&503||500;throw new b(t,`api`,{status:n,code:n>=500?`server_error`:void 0})}}}}}}function Ft(e){if(!e||e===`auto`)return{auto:{}};if(e===`required`)return{any:{}};if(e===`none`)throw new b("'none' is not supported by fromBedrock: Bedrock Converse has no `tool_choice` equivalent to forbidding tool use while tools are still offered. Omit `tools` entirely for this call instead.",`invalid_params`,{code:`unsupported_capability`,issues:{capability:`toolChoice: 'none'`}});return{tool:{name:e.function.name}}}function It(e){if(e.role===`tool`)return{role:`user`,content:[{toolResult:{toolUseId:e.tool_call_id,content:[{text:e.content}],status:e.is_error?`error`:`success`}}]};if(e.role===`assistant`&&e.tool_calls?.length){let t=[];e.content&&t.push({text:e.content});for(let n of e.tool_calls){let e;if(!n.function.arguments.trim())e={};else try{e=JSON.parse(n.function.arguments)}catch(e){throw new b(`Assistant tool call "${n.function.name}" (${n.id}) has arguments that are not valid JSON.`,`validation`,{cause:e})}t.push({toolUse:{toolUseId:n.id,name:n.function.name,input:e}})}return{role:`assistant`,content:t}}return{role:e.role,content:Array.isArray(e.content)?Dt(e.content):[{text:e.content??``}]}}function Lt(e){let t=e=>e.role===`user`&&e.content.length>0&&e.content.every(e=>`toolResult`in e),n=[];for(let r of e){let e=n.at(-1);t(r)&&e&&t(e)?e.content.push(...r.content):n.push(r)}return n}async function*Rt(e){let t=e.getReader();try{for(;;){let{done:e,value:n}=await t.read();if(e)return;n&&(yield n)}}finally{try{await t.cancel()}catch{}t.releaseLock()}}async function zt(e,t){let n=await fetch(e,t);if(!n.ok){let e=await n.text().catch(()=>``),t=Error(`Fetch adapter stream request failed (${n.status}): ${e.slice(0,500)}`);throw t.status=n.status,t.headers=n.headers,t}if(!n.body)throw Error(`Fetch adapter stream request received a response with no body.`);return Rt(n.body)}async function Bt(e,t,n){let r=typeof e.url==`function`?e.url(t):e.url,i=typeof e.headers==`function`?await e.headers():e.headers,a=e.method??`POST`,o=![`GET`,`HEAD`].includes(a.toUpperCase());return{url:r,method:a,headers:o?{"Content-Type":`application/json`,...i}:{...i},...o?{body:JSON.stringify(n)}:{}}}function Vt(e){return{chat:{completions:{async create(t,n){let{url:r,method:i,headers:a,body:o}=await Bt(e,t,e.mapRequest(t)),s=e.request??fetch,c=await s(r,{method:i,headers:a,body:o,signal:n.signal});if(!c.ok){let e=await c.text().catch(()=>``),t=Error(`Fetch adapter request failed (${c.status}): ${e.slice(0,500)}`);throw t.status=c.status,t.headers=c.headers,t}let l=await c.json(),{content:u,usage:d,toolCalls:f}=e.mapResponse(l),p=f?.length?f.map(e=>({id:e.id,type:`function`,function:{name:e.name,arguments:e.arguments}})):void 0;return{choices:[{message:{content:u,...p?{tool_calls:p}:{}}}],usage:d?{prompt_tokens:d.promptTokens,completion_tokens:d.completionTokens,total_tokens:d.totalTokens}:void 0}},async*createStream(t,n){if(!e.mapStreamEvent)throw new b(`stream: true requires mapStreamEvent to be configured on fromFetch`,`invalid_params`,{code:`unsupported_capability`,issues:{capability:`mapStreamEvent`}});if(e.request&&!e.requestStream)throw new b("`stream: true` requires `requestStream` to be configured on fromFetch when a custom `request` transport is set. `requestStream` does not fall back to `request` (it needs an async-iterable byte stream, which `RequestLike`'s buffered `ResponseLike` has no way to provide), without it, `stream: true` would silently use plain native `fetch` instead of your configured transport. Add a `requestStream` that opens the same connection your `request` does, or omit `request` if native `fetch` is fine for both.",`invalid_params`,{code:`unsupported_capability`,issues:{capability:`requestStream`}});let{url:r,method:i,headers:a,body:o}=await Bt(e,t,e.mapRequest(t)),s=e.requestStream??zt,c=e.parseStreamFrames??Ge,l=await s(r,{method:i,headers:a,body:o,signal:n.signal});for await(let t of c(l)){if(t===G){yield{type:`ping`};continue}let n=e.mapStreamEvent(t);if(!n)continue;Array.isArray(n)?yield*n:yield n}}}}}}function Ht(e,t){if(e.budget_tokens===void 0)return e;let{budget_tokens:n,...r}=e;return r.reasoning_effort===void 0?{...r,reasoning_effort:Z(n,t)}:r}function Ut(e){return e.map(e=>e.type===`image`?{type:`image_url`,image_url:{url:`data:${q(e.mimeType)};base64,${e.data}`}}:{type:`text`,text:e.text})}function Wt(e){return e.messages.map(e=>{if(e.role===`user`&&Array.isArray(e.content))return{...e,content:Ut(e.content)};if(e.role===`tool`){let{is_error:t,...n}=e;return n}return e})}function*Gt(e){let t=e.choices?.[0]?.delta;if(t?.content&&(yield{type:`text-delta`,delta:t.content}),t?.tool_calls?.length)for(let e of t.tool_calls)yield{type:`tool_call_delta`,index:e.index,id:e.id,name:e.function?.name,argumentsDelta:e.function?.arguments};e.usage&&(yield{type:`usage`,usage:e.usage})}function $(e,t={}){let n=e,{supportsStreamUsage:r=!0}=t,i=Y(t.reasoningEffortTokens),a=n.chat.completions.create.bind(n.chat.completions);return{chat:{completions:{async create(e,t){let r=Wt(e);return n.chat.completions.create(Ht({...e,messages:r},i),t)},async*createStream(e,t){let n=Wt(e),o=await a(Ht({...e,messages:n,stream:!0,...r?{stream_options:{include_usage:!0}}:{}},i),t);for await(let e of o)yield*Gt(e)}}}}}const Kt=$,qt=$,Jt=$,Yt=$,Xt=$,Zt=$,Qt=$,$t=$,en=$,tn=$,nn=$,rn=$,an=$,on=$,sn=$,cn=$,ln=$,un=$,dn=$,fn=$,pn=$,mn=$,hn=$,gn=$,_n=$,vn=$,yn=$,bn=$,xn=$,Sn=$,Cn=$,wn=$,Tn=$,En=$,Dn=$,On=$,kn=$,An=$;exports.CircuitBreaker=fe,exports.ConsoleLogger=Re,exports.FallbackExhaustedError=D,exports.InMemoryCacheAdapter=S,exports.LLMError=b,exports.NormalizedCacheAdapter=C,exports.RateLimiter=Ve,exports.SSE_PING=G,exports.TieredCacheAdapter=w,exports.VernLLM=He,exports.defaultEstimateTokens=ze,exports.defaultFallbackOn=ne,exports.defineCachedCallParams=We,exports.defineCallParams=Ue,exports.defineTool=T,exports.from01AI=An,exports.fromAnthropic=dt,exports.fromAnyscale=Tn,exports.fromAtlasCloud=kn,exports.fromBaseten=gn,exports.fromBedrock=Pt,exports.fromCerebras=Xt,exports.fromCloudflareWorkersAI=pn,exports.fromDeepInfra=nn,exports.fromDeepSeek=Yt,exports.fromFeatherless=_n,exports.fromFetch=Vt,exports.fromFireworks=Qt,exports.fromFriendli=vn,exports.fromGemini=Ct,exports.fromGroq=qt,exports.fromHyperbolic=an,exports.fromInferenceNet=Dn,exports.fromInfermatic=On,exports.fromLMStudio=cn,exports.fromLambdaLabs=Cn,exports.fromLepton=En,exports.fromMiniMax=Sn,exports.fromMistral=Jt,exports.fromMoonshot=on,exports.fromNebius=mn,exports.fromNovita=rn,exports.fromNvidiaNIM=dn,exports.fromOllama=$t,exports.fromOpenAI=Kt,exports.fromOpenAICompatible=$,exports.fromOpenRouter=en,exports.fromParasail=bn,exports.fromPerplexity=tn,exports.fromSambaNova=hn,exports.fromSiliconFlow=yn,exports.fromSnowflakeCortex=wn,exports.fromStepFun=xn,exports.fromTogether=Zt,exports.fromVLLM=ln,exports.fromVercelAIGateway=fn,exports.fromXAI=un,exports.fromZhipu=sn,exports.hasIssues=ee,exports.isFallbackExhaustedError=re,exports.isLLMError=x,exports.isToolCallResult=E,exports.parseSseStream=Ge;
15
15
  //# sourceMappingURL=index.cjs.map