inference-relay 1.6.8 → 1.6.11

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,5 +1,5 @@
1
1
  'use strict';var child_process=require('child_process'),fs=require('fs'),path=require('path'),os=require('os'),crypto$1=require('crypto'),vm=require('vm'),module$1=require('module'),events=require('events');/*! inference-relay (c) 2026 L2B II LLC. Patent pending. Proprietary. See LICENSE. */
2
- var rt=Object.defineProperty;var nt=(n=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(n,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):n)(function(n){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+n+'" is not supported')});var st=(n,e)=>()=>(n&&(e=n(n=0)),e);var it=(n,e)=>{for(var t in e)rt(n,t,{get:e[t],enumerable:true});};var Fe={};it(Fe,{detectCLI:()=>V});function Et(n){let e=process.env.PATH;if(!e)return null;let t=process.platform==="win32",r=t?";":":",s=t?(process.env.PATHEXT??".COM;.EXE;.BAT;.CMD").split(";").map(i=>i.toLowerCase()):[""];for(let i of e.split(r))if(i)for(let a of s){let o=path.join(i,n+a);if(fs.existsSync(o))return o}return null}async function V(){if(!Et("claude"))return {available:false,authenticated:false};let e=null;try{let r=child_process.execSync("claude --version",{encoding:"utf-8",timeout:5e3}).trim(),s=r.match(/(\d+\.\d+\.\d+)/);e=s?s[1]:r;}catch{}let t;return e&&!bt.some(r=>e.startsWith(r.split(".").slice(0,2).join(".")))&&(t=`CLI version ${e} is not tested with this version of inference-relay. Streaming may be unstable.`),{available:true,authenticated:true,cliVersion:e||void 0,versionWarning:t}}var bt,ne=st(()=>{bt=["2.1.87"];});var K=class n extends Error{code="RELAY_CAP_EXCEEDED";tier;baseCap;topUps;effectiveCap;usedThisCycle;resolutionUrl;constructor(e){let t=e.resolutionUrl??"https://inference-relay.com/dashboard/billing?topup=1";super(`Cap reached (${e.usedThisCycle} / ${e.effectiveCap??"\u221E"} calls this cycle). Buy a top-up or upgrade at ${t}`),this.name="RelayCapExceededError",this.tier=e.tier,this.baseCap=e.baseCap,this.topUps=e.topUps,this.effectiveCap=e.effectiveCap,this.usedThisCycle=e.usedThisCycle,this.resolutionUrl=t,Object.setPrototypeOf(this,n.prototype);}};function M(n,e){let t=new Map,r=null,s=null,i=new Promise((d,f)=>{r=d,s=f;});i.catch(()=>{});let a=[],o=null,l="",p="";function c(d,...f){let u=t.get(d);u&&u.forEach(h=>h(...f));}async function*g(){try{for await(let d of n)if(yield d,d.type==="content_block_start")a[d.index]=d.content_block;else if(d.type==="content_block_delta"){let f=a[d.index];f&&d.delta?.type==="text_delta"&&(f.text=(f.text||"")+d.delta.text,c("text",d.delta.text));}else if(d.type==="message_start")l=d.message?.model||"",o=d.message?.usage;else if(d.type==="message_delta")p=d.delta?.stop_reason||"",d.usage&&(o={...o,...d.usage});else if(d.type==="message_stop"){let f={role:"assistant",model:l,content:a.filter(Boolean),usage:o,stop_reason:p};c("message",f),r&&(r(f),r=null),e&&e(f);}if(r){let d={role:"assistant",model:l,content:a.filter(Boolean),usage:o,stop_reason:p||"end_turn"};c("message",d),r(d),r=null,e&&e(d);}}catch(d){throw c("error",d),s&&(s(d),s=null),d}}let m=g(),y=false,_={[Symbol.asyncIterator](){if(y)throw new Error("[inference-relay] RelayStream can only be iterated once");return y=true,m},on(d,f){return t.has(d)||t.set(d,[]),t.get(d).push(f),_},finalMessage(){let d,f=new Promise((u,h)=>{d=setTimeout(()=>h(new Error("[inference-relay] finalMessage() timed out \u2014 no message_stop received after 5 minutes")),3e5);});return Promise.race([i,f]).finally(()=>clearTimeout(d))}};return _}var C=class{priority=100;async streamRaw(e,t){throw new Error(`[inference-relay] Provider '${this.type}' does not implement streamRaw(). Override either stream() or streamRaw() in the provider class.`)}async stream(e,t){return M(await this.streamRaw(e,t))}};var be=`-----BEGIN PUBLIC KEY-----
2
+ var it=Object.defineProperty;var at=(n=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(n,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):n)(function(n){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+n+'" is not supported')});var ot=(n,e)=>()=>(n&&(e=n(n=0)),e);var lt=(n,e)=>{for(var t in e)it(n,t,{get:e[t],enumerable:true});};var Ke={};lt(Ke,{detectCLI:()=>V});function At(n){let e=process.env.PATH;if(!e)return null;let t=process.platform==="win32",r=t?";":":",s=t?(process.env.PATHEXT??".COM;.EXE;.BAT;.CMD").split(";").map(i=>i.toLowerCase()):[""];for(let i of e.split(r))if(i)for(let a of s){let o=path.join(i,n+a);if(fs.existsSync(o))return o}return null}async function V(){if(!At("claude"))return {available:false,authenticated:false};let e=null;try{let r=child_process.execSync("claude --version",{encoding:"utf-8",timeout:5e3}).trim(),s=r.match(/(\d+\.\d+\.\d+)/);e=s?s[1]:r;}catch{}let t;return e&&!Rt.some(r=>e.startsWith(r.split(".").slice(0,2).join(".")))&&(t=`CLI version ${e} is not tested with this version of inference-relay. Streaming may be unstable.`),{available:true,authenticated:true,cliVersion:e||void 0,versionWarning:t}}var Rt,se=ot(()=>{Rt=["2.1.87"];});var K=class n extends Error{code="RELAY_CAP_EXCEEDED";tier;baseCap;topUps;effectiveCap;usedThisCycle;resolutionUrl;constructor(e){let t=e.resolutionUrl??"https://inference-relay.com/dashboard/billing?topup=1";super(`Cap reached (${e.usedThisCycle} / ${e.effectiveCap??"\u221E"} calls this cycle). Buy a top-up or upgrade at ${t}`),this.name="RelayCapExceededError",this.tier=e.tier,this.baseCap=e.baseCap,this.topUps=e.topUps,this.effectiveCap=e.effectiveCap,this.usedThisCycle=e.usedThisCycle,this.resolutionUrl=t,Object.setPrototypeOf(this,n.prototype);}};function M(n,e){let t=new Map,r=null,s=null,i=new Promise((d,f)=>{r=d,s=f;});i.catch(()=>{});let a=[],o=null,l="",p="";function c(d,...f){let u=t.get(d);u&&u.forEach(h=>h(...f));}async function*g(){try{for await(let d of n)if(yield d,d.type==="content_block_start")a[d.index]=d.content_block;else if(d.type==="content_block_delta"){let f=a[d.index];f&&d.delta?.type==="text_delta"&&(f.text=(f.text||"")+d.delta.text,c("text",d.delta.text));}else if(d.type==="message_start")l=d.message?.model||"",o=d.message?.usage;else if(d.type==="message_delta")p=d.delta?.stop_reason||"",d.usage&&(o={...o,...d.usage});else if(d.type==="message_stop"){let f={role:"assistant",model:l,content:a.filter(Boolean),usage:o,stop_reason:p};c("message",f),r&&(r(f),r=null),e&&e(f);}if(r){let d={role:"assistant",model:l,content:a.filter(Boolean),usage:o,stop_reason:p||"end_turn"};c("message",d),r(d),r=null,e&&e(d);}}catch(d){throw c("error",d),s&&(s(d),s=null),d}}let m=g(),y=false,_={[Symbol.asyncIterator](){if(y)throw new Error("[inference-relay] RelayStream can only be iterated once");return y=true,m},on(d,f){return t.has(d)||t.set(d,[]),t.get(d).push(f),_},finalMessage(){let d,f=new Promise((u,h)=>{d=setTimeout(()=>h(new Error("[inference-relay] finalMessage() timed out \u2014 no message_stop received after 5 minutes")),3e5);});return Promise.race([i,f]).finally(()=>clearTimeout(d))}};return _}var S=class{priority=100;async streamRaw(e,t){throw new Error(`[inference-relay] Provider '${this.type}' does not implement streamRaw(). Override either stream() or streamRaw() in the provider class.`)}async stream(e,t){return M(await this.streamRaw(e,t))}};var ve=`-----BEGIN PUBLIC KEY-----
3
3
  MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzk88Bt9LZSmuSd8ulKqy
4
4
  EGNiPB+r8u1ZinBvMM4qX8dp4kzJEC/47L1RYrrxCphD05HToREjljbj+upH6rZH
5
5
  t6D/5funYPi2TO1IOSC6BRVRwMqqTmJGVJcLcG/WF3gTE+2UQ3q5EppWJCEKcz6x
@@ -7,12 +7,12 @@ kfynZEzOcLikoQALIxEjxRfi6g83wNJS+WBwAJQ68s7N9FHE3rkLzEqv3FJRP+ZR
7
7
  7lqwqIyuCLr9j9yc4XqF6VY9yooD2hyw/n1EVFejzjDgntWTqIkaCIXwh8lDzRrK
8
8
  Rxn07gcI6ea2Eym9sXh4xZLFudXfAT2DFsH/XboDUuMKAV1YHc1NsIGApesWe2M+
9
9
  UQIDAQAB
10
- -----END PUBLIC KEY-----`,_e={version:"1.0.0",cliVersion:"2.1.87",eventTypes:{system:"system",streamEvent:"stream_event",assistant:"assistant",result:"result"},fieldMappings:{sessionId:"session_id",model:"model",cliVersion:"claude_code_version",content:"content",usage:"usage",inputTokens:"input_tokens",outputTokens:"output_tokens",stopReason:"stop_reason",isError:"is_error",totalCostUsd:"total_cost_usd",durationMs:"duration_ms",error:"error",event:"event"},syntheticModel:"<synthetic>"},D=class{verificationFailures=0;cachedManifest=null;currentSecurityState="SEC_NOMINAL";async verifyManifest(e){try{let{compactVerify:t,importSPKI:r}=await import('jose'),s=await r(be,"RS256"),{payload:i}=await t(e,s),a=JSON.parse(new TextDecoder().decode(i));return this.verificationFailures=0,this.cachedManifest=a,this.currentSecurityState="SEC_NOMINAL",{manifest:a,securityState:"SEC_NOMINAL"}}catch{if(this.verificationFailures++,this.verificationFailures>=3){this.currentSecurityState="SEC_DEGRADED";let t=2e3+Math.random()*3e3;return await new Promise(r=>setTimeout(r,t)),console.warn("[inference-relay] Logic synchronization mismatch. Routing via local recovery."),{manifest:_e,securityState:"SEC_DEGRADED"}}return this.cachedManifest?(this.currentSecurityState="SEC_CACHE",{manifest:this.cachedManifest,securityState:"SEC_CACHE"}):(this.currentSecurityState="SEC_RECOVERY",{manifest:_e,securityState:"SEC_RECOVERY"})}}getSecurityState(){return this.currentSecurityState}clearCache(){this.cachedManifest=null,this.verificationFailures=0,this.currentSecurityState="SEC_NOMINAL";}},at=new D;function Le(){at.clearCache();}function ot(){return _e}function lt(n,e){return e.split(".").reduce((t,r)=>{if(!(r==="__proto__"||r==="constructor"||r==="prototype"))return t&&t[r]},n)}function S(n,...e){process.env.IR_DEBUG==="1"&&console.error(`[ir-debug:${n}]`,...e);}var mt=process.env.IR_GATEWAY_URL||"https://api.inference-relay.com";function Re(n){return path.join(os.homedir(),".inference-relay","gateway",n)}function De(n){return path.join(Re(n),"bundle.js")}function Ne(n){return path.join(Re(n),"meta.json")}var E=class extends Error{constructor(t,r,s){super(t);this.code=r;this.cause=s;this.name="GatewayLoaderError";}code;cause},x=class extends E{constructor(e,t){super(e,"GATEWAY_FETCH_FAILED",t);}},G=new Map,ht=[0,1e3,4e3];function yt(n){let e=(r,s)=>{if(!Array.isArray(r)||r.length===0)return null;let i=[];for(let a of r){let o=typeof a=="string"?Number(a):a;if(typeof o!="number"||!Number.isFinite(o)||o<0)return S("loader.retry-config-invalid",{source:s,badValue:a}),null;i.push(o);}return i};if(n!==void 0){let r=e(n,"option");if(r)return r}let t=process.env.IR_GATEWAY_RETRY_DELAYS_MS;if(t){let r=e(t.split(",").map(s=>s.trim()),"env");if(r)return r}return ht}var N=class{static async load(e,t,r=mt,s){let i=`${t}:${e.slice(-8)}`;if(G.has(i))return G.get(i);try{let m=await this.tryLoadFromDisk(t);if(m)return S("loader.cache-hit",{libraryVersion:t}),G.set(i,m),m}catch{}S("loader.cache-miss",{libraryVersion:t});let{bytes:a,sha256:o,signature:l,libraryVersion:p}=await this.fetchFromAuthority(e,t,r,s);if(crypto$1.createHash("sha256").update(a).digest("hex")!==o)throw new E("[ir-system]: GATEWAY_VERIFICATION_FAILED (REASON: HASH_MISMATCH)","GATEWAY_VERIFICATION_FAILED");await this.verifySignature(l,p,o);try{let m=Re(t);fs.mkdirSync(m,{recursive:!0}),fs.writeFileSync(De(t),a),fs.writeFileSync(Ne(t),JSON.stringify({sha256:o,signature:l,libraryVersion:p}));}catch{}let g=this.loadIntoVm(a.toString("utf-8"));return G.set(i,g),g}static async loadFromDisk(e){if(!fs.existsSync(e))throw new E(`[ir-system]: GATEWAY_LOAD_FAILED (REASON: BUNDLE_NOT_FOUND at ${e})`,"GATEWAY_LOAD_FAILED");let t=fs.readFileSync(e,"utf-8");return this.loadIntoVm(t)}static async tryLoadFromDisk(e){let t=De(e),r=Ne(e);if(!fs.existsSync(t)||!fs.existsSync(r))return null;let s=fs.readFileSync(t),i=JSON.parse(fs.readFileSync(r,"utf-8"));return crypto$1.createHash("sha256").update(s).digest("hex")!==i.sha256?null:(await this.verifySignature(i.signature,i.libraryVersion,i.sha256),this.loadIntoVm(s.toString("utf-8")))}static async fetchFromAuthority(e,t,r,s){let i,a=yt(s?.retryDelaysMs),o;for(let g=0;g<a.length;g++){let m=a[g];m>0&&await new Promise(y=>setTimeout(y,m)),S("loader.fetch",{url:`${r}/v1/gateway/fetch`,libraryVersion:t,attempt:g+1,delayMs:m});try{let y=await fetch(`${r}/v1/gateway/fetch`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify({libraryVersion:t})});if(S("loader.fetch-result",{status:y.status,attempt:g+1}),y.status===401||y.status===403)throw new E("[ir-system]: LICENSE_REJECTED (REASON: GATEWAY_FETCH_UNAUTHORIZED)","LICENSE_REJECTED");if(y.status>=500&&y.status<600){o=new x(`[ir-system]: GATEWAY_FETCH_FAILED (REASON: HTTP_${y.status})`);continue}if(!y.ok)throw new x(`[ir-system]: GATEWAY_FETCH_FAILED (REASON: HTTP_${y.status})`);i=await y.json(),o=null;break}catch(y){if(y instanceof E&&y.code==="LICENSE_REJECTED")throw y;o=y;}}if(o||!i)throw o instanceof E?o:new x("[ir-system]: GATEWAY_FETCH_FAILED (REASON: NETWORK_ERROR)",o);let l;try{l=await fetch(i.bundleUrl);}catch(g){throw new x("[ir-system]: GATEWAY_FETCH_FAILED (REASON: BUNDLE_DOWNLOAD_FAILED)",g)}if(!l.ok)throw new x(`[ir-system]: GATEWAY_FETCH_FAILED (REASON: BUNDLE_HTTP_${l.status})`);let p=await l.arrayBuffer();return {bytes:Buffer.from(p),sha256:i.sha256,signature:i.signature,libraryVersion:i.libraryVersion,bundleVersion:i.bundleVersion}}static async verifySignature(e,t,r){try{let{compactVerify:s,importSPKI:i}=await import('jose'),a=await i(be,"RS256"),{payload:o}=await s(e,a),l=JSON.parse(new TextDecoder().decode(o));if(l.purpose!=="gateway-bundle")throw new E("[ir-system]: GATEWAY_VERIFICATION_FAILED (REASON: PURPOSE_MISMATCH)","GATEWAY_VERIFICATION_FAILED");if(l.libraryVersion!==t)throw new E("[ir-system]: GATEWAY_VERIFICATION_FAILED (REASON: LIBRARY_VERSION_MISMATCH)","GATEWAY_VERIFICATION_FAILED");if(l.sha256!==r)throw new E("[ir-system]: GATEWAY_VERIFICATION_FAILED (REASON: SHA256_MISMATCH)","GATEWAY_VERIFICATION_FAILED")}catch(s){throw s instanceof E?s:new E("[ir-system]: GATEWAY_VERIFICATION_FAILED (REASON: SIGNATURE_INVALID)","GATEWAY_VERIFICATION_FAILED",s)}}static loadIntoVm(e){try{let t=path.join(os.homedir(),".inference-relay","gateway","runtime.cjs"),r=path.dirname(t),s=module$1.createRequire(t),i={exports:{}};vm.compileFunction(e,["exports","require","module","__filename","__dirname"],{filename:"<gateway-bundle>"})(i.exports,s,i,t,r);let o=i.exports;if(!o.ClaudeCLIProvider)throw new E("[ir-system]: GATEWAY_LOAD_FAILED (REASON: MISSING_CLAUDE_CLI_EXPORT)","GATEWAY_LOAD_FAILED");return o}catch(t){throw t instanceof E?t:new E("[ir-system]: GATEWAY_LOAD_FAILED (REASON: VM_EXECUTION_ERROR)","GATEWAY_LOAD_FAILED",t)}}static clearCache(){G.clear();}};var He="1.6.8",se=class extends C{type="claude-cli";supportsStreaming=true;supportsMultimodal=true;platformRequirement="desktop";licenseKey;loadOptions;loadedProvider=null;loadingPromise=null;health={status:"healthy",lastCheckAt:Date.now(),errorCount:0};constructor(e,t){super(),this.licenseKey=e,this.loadOptions=t;}async ensureLoaded(){return this.loadedProvider?this.loadedProvider:this.loadingPromise?this.loadingPromise:(this.loadingPromise=(async()=>{try{let t=(await N.load(this.licenseKey,He,void 0,this.loadOptions)).ClaudeCLIProvider,r=new t;return this.loadedProvider=r,r}catch(e){throw this.loadingPromise=null,this.health.status="unhealthy",this.health.errorCount++,e instanceof E?this.health.lastError=e.message:e instanceof Error&&(this.health.lastError=e.message),e}})(),this.loadingPromise)}async warmup(){await this.ensureLoaded();}async probe(){let{detectCLI:e}=await Promise.resolve().then(()=>(ne(),Fe));return e()}async execute(e,t){return (await this.ensureLoaded()).execute(e,t)}async streamRaw(e,t){return (await this.ensureLoaded()).streamRaw(e,t)}estimateCost(e){if(this.loadedProvider)return this.loadedProvider.estimateCost(e);let r=JSON.stringify(e.messages).length/4+5e3,s=(r*3+1e3*15)/1e6;return {estimatedInputTokens:Math.round(r),estimatedCost:Math.round(s*1e4)/1e4,model:e.model,provider:"claude-cli"}}getHealth(){return this.loadedProvider?this.loadedProvider.getHealth():{...this.health}}reportRateLimit(e){this.loadedProvider?this.loadedProvider.reportRateLimit(e):(this.health.status="degraded",this.health.lastError=`Rate limited: ${e.utilization*100}% utilization`,this.health.lastCheckAt=Date.now());}},ie=class extends C{type="kimi-cli";supportsStreaming=true;supportsMultimodal=false;platformRequirement="desktop";licenseKey;loadOptions;loadedProvider=null;loadingPromise=null;health={status:"healthy",lastCheckAt:Date.now(),errorCount:0};constructor(e,t){super(),this.licenseKey=e,this.loadOptions=t;}async ensureLoaded(){return this.loadedProvider?this.loadedProvider:this.loadingPromise?this.loadingPromise:(this.loadingPromise=(async()=>{try{let t=(await N.load(this.licenseKey,He,void 0,this.loadOptions)).KimiCLIProvider;if(!t)throw new E("[ir-system]: GATEWAY_LOAD_FAILED (REASON: STALE_BUNDLE_NO_KIMI_CLI)","GATEWAY_LOAD_FAILED");let r=new t;return this.loadedProvider=r,r}catch(e){throw this.loadingPromise=null,this.health.status="unhealthy",this.health.errorCount++,this.health.lastError=e instanceof Error?e.message:String(e),e}})(),this.loadingPromise)}async warmup(){await this.ensureLoaded();}async probe(){let{existsSync:e}=await import('fs'),{homedir:t}=await import('os'),{join:r}=await import('path'),s=process.env.KIMI_CODE_HOME||r(t(),".kimi-code"),i=process.env.IR_KIMI_BIN||r(s,"bin","kimi"),a=e(i),o=e(r(s,"credentials"))||e(r(s,"oauth"));return {available:a,authenticated:o,subscriptionType:o?"kimi-code":void 0}}async execute(e,t){return (await this.ensureLoaded()).execute(e,t)}async streamRaw(e,t){return (await this.ensureLoaded()).streamRaw(e,t)}estimateCost(e){if(this.loadedProvider)return this.loadedProvider.estimateCost(e);let t=JSON.stringify(e.messages).length/4;return {estimatedInputTokens:Math.round(t+2e3),estimatedCost:0,model:e.model,provider:"kimi-cli"}}getHealth(){return this.loadedProvider?this.loadedProvider.getHealth():{...this.health}}reportRateLimit(e){this.loadedProvider?this.loadedProvider.reportRateLimit(e):(this.health.status="degraded",this.health.lastError=`Rate limited: ${e.utilization*100}% utilization`,this.health.lastCheckAt=Date.now());}};var $=[{canonical:"claude-haiku-4-5",apiId:"claude-haiku-4-5-20251001",provider:"anthropic-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:2e5},pricing:[.8,4]},{canonical:"claude-sonnet-4-6",apiId:"claude-sonnet-4-20250514",provider:"anthropic-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:2e5},pricing:[3,15]},{canonical:"claude-opus-4-6",apiId:"claude-opus-4-20250514",provider:"anthropic-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:2e5},pricing:[15,75]},{canonical:"gpt-4.1",apiId:"gpt-4.1",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:1e6},pricing:[2,8]},{canonical:"gpt-4.1-mini",apiId:"gpt-4.1-mini",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:1e6},pricing:[.4,1.6]},{canonical:"gpt-4.1-nano",apiId:"gpt-4.1-nano",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:1e6},pricing:[.1,.4]},{canonical:"gpt-4o",apiId:"gpt-4o",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:128e3},pricing:[2.5,10]},{canonical:"gpt-4o-mini",apiId:"gpt-4o-mini",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:128e3},pricing:[.15,.6]},{canonical:"o4-mini",apiId:"o4-mini",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:false,contextWindow:2e5},pricing:[1.1,4.4]},{canonical:"o3",apiId:"o3",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:false,contextWindow:2e5},pricing:[.4,1.6]},{canonical:"o3-mini",apiId:"o3-mini",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:false,contextWindow:2e5},pricing:[1.1,4.4]},{canonical:"o1",apiId:"o1",provider:"openai-api",capabilities:{streaming:false,tools:false,multimodal:false,contextWindow:2e5},pricing:[15,60]},{canonical:"llama3.1",apiId:"llama3.1",provider:"ollama",capabilities:{streaming:true,tools:true,multimodal:false,contextWindow:128e3}},{canonical:"mistral",apiId:"mistral",provider:"ollama",capabilities:{streaming:true,tools:false,multimodal:false,contextWindow:32e3}},{canonical:"mistral-nemo",apiId:"mistral-nemo",provider:"ollama",capabilities:{streaming:true,tools:true,multimodal:false,contextWindow:128e3}},{canonical:"llava",apiId:"llava",provider:"ollama",capabilities:{streaming:true,tools:false,multimodal:true,contextWindow:32e3}},{canonical:"qwen2:0.5b",apiId:"qwen2:0.5b",provider:"ollama",capabilities:{streaming:true,tools:false,multimodal:false,contextWindow:32e3}}],W=Object.fromEntries($.filter(n=>n.canonical!==n.apiId).map(n=>[n.canonical,n.apiId]));W["claude-sonnet-4"]="claude-sonnet-4-20250514";W["claude-opus-4"]="claude-opus-4-20250514";W["claude-haiku-4"]="claude-haiku-4-5-20251001";function Ae(n){return W[n]||n}function vt(n){return $.find(e=>e.canonical===n)}function ae(n){return $.find(e=>e.canonical===n)?.provider}function Rt(n){return $.filter(e=>e.provider===n)}var At="https://api.anthropic.com/v1/messages",Ct="2023-06-01",Ce={"claude-sonnet-4-5-20250514":[3,15],"claude-sonnet-4-20250514":[3,15],"claude-haiku-4-5-20250414":[.8,4],"claude-opus-4-20250514":[15,75]};function Be(n){if(Ce[n])return Ce[n];for(let[e,t]of Object.entries(Ce))if(n.startsWith(e.split("-20")[0]))return t;return [3,15]}var Y=class extends C{type="anthropic-api";supportsStreaming=true;supportsMultimodal=true;platformRequirement="any";apiKey;baseUrl;dangerousDirectBrowserAccess;health={status:"healthy",lastCheckAt:Date.now(),errorCount:0};constructor(e){super(),this.apiKey=e.apiKey,this.baseUrl=e.baseUrl?.replace(/\/$/,"")||At,this.dangerousDirectBrowserAccess=e.dangerousDirectBrowserAccess??false;}async probe(){try{let e=await fetch(this.baseUrl,{method:"POST",headers:this.buildHeaders(),body:JSON.stringify({model:"claude-haiku-4-5-20250414",max_tokens:1,messages:[{role:"user",content:"hi"}]})});return e.status===401?{available:!0,authenticated:!1}:e.ok||e.status===429?{available:!0,authenticated:!0,rateLimitTier:e.headers.get("anthropic-ratelimit-tier")??void 0}:e.status===529?{available:!0,authenticated:!0}:{available:!1,authenticated:!1}}catch{return {available:false,authenticated:false}}}async execute(e,t){let r=Date.now(),s=this.buildRequestBody(e,false),i=await fetch(this.baseUrl,{method:"POST",headers:this.buildHeaders(),body:JSON.stringify(s),signal:t?.signal});if(!i.ok)throw this.handleErrorResponse(i),new Error("unreachable");let a=await i.json(),o=Date.now()-r;this.recordSuccess();let[l,p]=Be(a.model||e.model),c=((a.usage?.input_tokens||0)*l+(a.usage?.output_tokens||0)*p)/1e6,g={},m=["request-id","x-request-id","anthropic-ratelimit-requests-limit","anthropic-ratelimit-requests-remaining","anthropic-ratelimit-requests-reset","anthropic-ratelimit-tokens-limit","anthropic-ratelimit-tokens-remaining","anthropic-ratelimit-tokens-reset","anthropic-ratelimit-tier"];for(let y of m){let _=i.headers.get(y);_&&(g[y]=_);}return {content:a.content||[],model:a.model||e.model,usage:{input_tokens:a.usage?.input_tokens||0,output_tokens:a.usage?.output_tokens||0},stop_reason:a.stop_reason||"end_turn",provider:"anthropic-api",costUsd:Math.round(c*1e6)/1e6,durationMs:o,fallbackChain:["anthropic-api:success"],headers:Object.keys(g).length>0?g:void 0}}async streamRaw(e,t){let r=this.buildRequestBody(e,true),s=await fetch(this.baseUrl,{method:"POST",headers:this.buildHeaders(),body:JSON.stringify(r),signal:t?.signal});if(s.ok||this.handleErrorResponse(s),!s.body)throw new Error("ANTHROPIC_API_ERROR: Response body is null \u2014 streaming unavailable");let i=s.body.getReader(),a=new TextDecoder;return (async function*(){let o="",l=false;try{for(;;){let{done:p,value:c}=await i.read();if(p)break;o+=a.decode(c,{stream:!0});let g=o.split(`
11
- `);o=g.pop()||"";let m=null;for(let y of g)if(y.startsWith("event: "))m=y.slice(7).trim();else if(y.startsWith("data: ")){let _=y.slice(6);if(m&&_)try{let d=JSON.parse(_);d.type==="message_stop"&&(l=!0),yield d;}catch{console.warn("[inference-relay] Skipped malformed SSE data:",_.substring(0,100));}m=null;}else y.trim()===""&&(m=null);}l||(yield {type:"message_stop"});}finally{try{i.releaseLock();}catch{}}})()}estimateCost(e){let t=Math.round(JSON.stringify(e.messages).length/4),r=e.system?Math.round(e.system.length/4):0,s=t+r,[i,a]=Be(e.model),o=(s*i+1e3*a)/1e6;return {estimatedInputTokens:s,estimatedCost:Math.round(o*1e4)/1e4,model:e.model,provider:"anthropic-api"}}getHealth(){return {...this.health}}reportRateLimit(e){this.health.status="degraded",this.health.lastError=`Rate limited: ${e.utilization*100}% utilization`,this.health.lastCheckAt=Date.now();}buildHeaders(){let e={"content-type":"application/json","x-api-key":this.apiKey,"anthropic-version":Ct};return this.dangerousDirectBrowserAccess&&(e["anthropic-dangerous-direct-browser-access"]="true"),e}buildRequestBody(e,t){let r={model:Ae(e.model),max_tokens:e.max_tokens,messages:e.messages};return t&&(r.stream=true),e.system&&(r.system=e.system),e.temperature!==void 0&&(r.temperature=e.temperature),e.tools&&e.tools.length>0&&(r.tools=e.tools),e.metadata&&(r.metadata=e.metadata),r}handleErrorResponse(e){let t=e.status;if(t===401)throw this.health.status="unhealthy",this.health.lastError="Authentication failed",this.health.errorCount++,this.health.lastCheckAt=Date.now(),new Error("ANTHROPIC_AUTH_ERROR: Invalid API key (401)");if(t===429){let r=e.headers.get("retry-after"),s=r?parseInt(r,10)*1e3:6e4;this.health.status="degraded",this.health.lastError=`Rate limited \u2014 retry after ${s}ms`,this.health.lastCheckAt=Date.now();let i=new Error(`ANTHROPIC_RATE_LIMIT: Too many requests (429). Retry after ${s}ms`);throw i.retryAfterMs=s,i}throw t===529?(this.health.status="degraded",this.health.lastError="API overloaded",this.health.lastCheckAt=Date.now(),new Error("ANTHROPIC_OVERLOADED: API is temporarily overloaded (529)")):(this.health.errorCount++,this.health.lastCheckAt=Date.now(),new Error(`ANTHROPIC_API_ERROR: Request failed with status ${t}`))}recordSuccess(){this.health.status="healthy",this.health.lastCheckAt=Date.now(),this.health.errorCount=0,this.health.lastError=void 0;}};var St="https://api.openai.com/v1/chat/completions",Ke=["gpt-","o1","o3","o4","chatgpt-","text-","davinci","babbage","ft:"],Se={"gpt-4.1":[2,8],"gpt-4.1-mini":[.4,1.6],"gpt-4.1-nano":[.1,.4],"gpt-4o":[2.5,10],"gpt-4o-mini":[.15,.6],"o4-mini":[1.1,4.4],o3:[.4,1.6],"o3-mini":[1.1,4.4],o1:[15,60],"o1-mini":[3,12],"gpt-4-turbo":[10,30],"gpt-4":[30,60],"gpt-3.5-turbo":[.5,1.5]};function Ge(n){if(Se[n])return Se[n];for(let[e,t]of Object.entries(Se))if(n.startsWith(e))return t;return [2.5,10]}function wt(n){return typeof n=="string"?n:n.filter(e=>e.type==="text"&&e.text).map(e=>e.text).join("")}function Pt(n,e){let t=[];e&&t.push({role:"system",content:e});for(let r of n){let s=wt(r.content);if(r.role==="assistant"&&Array.isArray(r.content)){let i=r.content.filter(a=>a.type==="tool_use");if(i.length>0){t.push({role:"assistant",content:s||"",tool_calls:i.map(a=>({id:a.id||`call_${Math.random().toString(36).slice(2)}`,type:"function",function:{name:a.name,arguments:typeof a.input=="string"?a.input:JSON.stringify(a.input)}}))});continue}}t.push({role:r.role,content:s});}return t}function Ve(n){switch(n){case "stop":return "end_turn";case "length":return "max_tokens";case "tool_calls":case "function_call":return "tool_use";default:return "end_turn"}}var q=class extends C{type="openai-api";supportsStreaming=true;supportsMultimodal=true;platformRequirement="any";apiKey;baseUrl;health={status:"healthy",lastCheckAt:Date.now(),errorCount:0};constructor(e){super(),this.apiKey=e.apiKey,this.baseUrl=e.baseUrl?.replace(/\/$/,"")||St;}async probe(){try{let e=this.baseUrl.replace(/\/chat\/completions$/,"/models"),t=await fetch(e,{method:"GET",headers:this.buildHeaders()});return t.status===401?{available:!0,authenticated:!1}:t.ok||t.status===429?{available:!0,authenticated:!0}:{available:!1,authenticated:!1}}catch{return {available:false,authenticated:false}}}async execute(e,t){let r=Date.now(),s=this.buildRequestBody(e,false),i=await fetch(this.baseUrl,{method:"POST",headers:this.buildHeaders(),body:JSON.stringify(s),signal:t?.signal});if(!i.ok)throw this.handleErrorResponse(i),new Error("unreachable");let a=await i.json(),o=Date.now()-r;this.recordSuccess();let l=a.choices?.[0],p=[];if(l?.message?.content&&p.push({type:"text",text:l.message.content}),l?.message?.tool_calls)for(let u of l.message.tool_calls)p.push({type:"tool_use",id:u.id,name:u.function?.name,input:u.function?.arguments?JSON.parse(u.function.arguments):{}});let c=a.usage?.prompt_tokens||0,g=a.usage?.completion_tokens||0,[m,y]=Ge(a.model||e.model),_=(c*m+g*y)/1e6,d={},f=["x-request-id","x-ratelimit-limit-requests","x-ratelimit-remaining-requests","x-ratelimit-limit-tokens","x-ratelimit-remaining-tokens","x-ratelimit-reset-requests","x-ratelimit-reset-tokens"];for(let u of f){let h=i.headers.get(u);h&&(d[u]=h);}return {content:p,model:a.model||e.model,usage:{input_tokens:c,output_tokens:g},stop_reason:Ve(l?.finish_reason),provider:"openai-api",costUsd:Math.round(_*1e6)/1e6,durationMs:o,fallbackChain:["openai-api:success"],headers:Object.keys(d).length>0?d:void 0}}async streamRaw(e,t){let r=this.buildRequestBody(e,true),s=await fetch(this.baseUrl,{method:"POST",headers:this.buildHeaders(),body:JSON.stringify(r),signal:t?.signal});if(s.ok||this.handleErrorResponse(s),!s.body)throw new Error("OPENAI_API_ERROR: Response body is null \u2014 streaming unavailable");let i=s.body.getReader(),a=new TextDecoder,o=e.model;return (async function*(){let l="",p=false,c=false,g,m;yield {type:"message_start",message:{model:o,usage:{input_tokens:0,output_tokens:0}}};try{for(;;){let{done:y,value:_}=await i.read();if(y)break;l+=a.decode(_,{stream:!0});let d=l.split(`
12
- `);l=d.pop()||"";for(let f of d){if(!f.startsWith("data: "))continue;let u=f.slice(6).trim();if(u==="[DONE]"){yield {type:"message_delta",delta:{stop_reason:m||"end_turn"},usage:g||{}},yield {type:"message_stop"},c=!0;return}try{let h=JSON.parse(u);h.usage&&(g={input_tokens:h.usage.prompt_tokens||0,output_tokens:h.usage.completion_tokens||0});let R=h.choices?.[0];if(!R)continue;let b=R.delta;b?.content&&!p&&(p=!0,yield {type:"content_block_start",index:0,content_block:{type:"text",text:""}}),b?.content&&(yield {type:"content_block_delta",index:0,delta:{type:"text_delta",text:b.content}}),R.finish_reason&&(p&&(yield {type:"content_block_stop",index:0}),m=Ve(R.finish_reason));}catch{}}}c||(yield {type:"message_delta",delta:{stop_reason:m||"end_turn"},usage:g||{}},yield {type:"message_stop"});}finally{try{i.releaseLock();}catch{}}})()}estimateCost(e){let t=Math.round(JSON.stringify(e.messages).length/4),r=e.system?Math.round(e.system.length/4):0,s=t+r,[i,a]=Ge(e.model),o=(s*i+1e3*a)/1e6;return {estimatedInputTokens:s,estimatedCost:Math.round(o*1e4)/1e4,model:e.model,provider:"openai-api"}}getHealth(){return {...this.health}}reportRateLimit(e){this.health.status="degraded",this.health.lastError=`Rate limited: ${e.utilization*100}% utilization`,this.health.lastCheckAt=Date.now();}buildHeaders(){return {"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`}}buildRequestBody(e,t){let r=e.model.toLowerCase();if(!Ke.some(o=>r.startsWith(o)))throw new Error(`OPENAI_MODEL_TRANSLATION_REQUIRED: The OpenAI provider received the model name '${e.model}', which does not match any known OpenAI model prefix (${Ke.join(", ")}). The relay does NOT auto-translate model names across providers in v1.3.x. Either (a) pass an OpenAI model name like 'gpt-4o-mini' when routing to this provider, or (b) route non-OpenAI models through a provider that understands them (e.g. 'anthropic-api' for Claude models).`);let i=Pt(e.messages,e.system),a={model:e.model,max_tokens:e.max_tokens,messages:i};return t&&(a.stream=true,a.stream_options={include_usage:true}),e.temperature!==void 0&&(a.temperature=e.temperature),e.tools&&e.tools.length>0&&(a.tools=e.tools),a}handleErrorResponse(e){let t=e.status;if(t===401)throw this.health.status="unhealthy",this.health.lastError="Authentication failed",this.health.errorCount++,this.health.lastCheckAt=Date.now(),new Error("OPENAI_AUTH_ERROR: Invalid API key (401)");if(t===429){let r=e.headers.get("retry-after"),s=r?parseInt(r,10)*1e3:6e4;this.health.status="degraded",this.health.lastError=`Rate limited \u2014 retry after ${s}ms`,this.health.lastCheckAt=Date.now();let i=new Error(`OPENAI_RATE_LIMIT: Too many requests (429). Retry after ${s}ms`);throw i.retryAfterMs=s,i}throw this.health.errorCount++,this.health.lastCheckAt=Date.now(),new Error(`OPENAI_API_ERROR: Request failed with status ${t}`)}recordSuccess(){this.health.status="healthy",this.health.lastCheckAt=Date.now(),this.health.errorCount=0,this.health.lastError=void 0;}};var kt="http://localhost:11434";function Tt(n){return typeof n=="string"?n:n.filter(e=>e.type==="text"&&e.text).map(e=>e.text).join("")}function It(n,e){let t=[];e&&t.push({role:"system",content:e});for(let r of n)t.push({role:r.role,content:Tt(r.content)});return t}var j=class extends C{type="ollama";supportsStreaming=true;supportsMultimodal=false;platformRequirement="desktop";baseUrl;health={status:"healthy",lastCheckAt:Date.now(),errorCount:0};constructor(e){super(),this.baseUrl=e?.baseUrl?.replace(/\/$/,"")||kt;}async probe(){try{return (await fetch(`${this.baseUrl}/api/tags`,{method:"GET"})).ok?{available:!0,authenticated:!0}:{available:!1,authenticated:!1}}catch{return {available:false,authenticated:false}}}async execute(e,t){let r=Date.now(),s=this.buildRequestBody(e,false),i=await fetch(`${this.baseUrl}/api/chat`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(s),signal:t?.signal});if(!i.ok)throw this.handleErrorResponse(i),new Error("unreachable");let a=await i.json(),o=Date.now()-r;this.recordSuccess();let l=[];a.message?.content&&l.push({type:"text",text:a.message.content});let p=a.prompt_eval_count||0,c=a.eval_count||0;return {content:l,model:a.model||e.model,usage:{input_tokens:p,output_tokens:c},stop_reason:(a.done,"end_turn"),provider:"ollama",costUsd:0,durationMs:o,fallbackChain:["ollama:success"]}}async streamRaw(e,t){let r=this.buildRequestBody(e,true),s=await fetch(`${this.baseUrl}/api/chat`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r),signal:t?.signal});if(s.ok||this.handleErrorResponse(s),!s.body)throw new Error("OLLAMA_ERROR: Response body is null \u2014 streaming unavailable");let i=s.body.getReader(),a=new TextDecoder,o=e.model;return (async function*(){let l="",p=false,c=false;yield {type:"message_start",message:{model:o,usage:{input_tokens:0,output_tokens:0}}};try{for(;;){let{done:g,value:m}=await i.read();if(g)break;l+=a.decode(m,{stream:!0});let y=l.split(`
13
- `);l=y.pop()||"";for(let _ of y){let d=_.trim();if(d)try{let f=JSON.parse(d);if(f.message?.content&&(p||(p=!0,yield {type:"content_block_start",index:0,content_block:{type:"text",text:""}}),yield {type:"content_block_delta",index:0,delta:{type:"text_delta",text:f.message.content}}),f.done){p&&(yield {type:"content_block_stop",index:0}),yield {type:"message_delta",delta:{stop_reason:"end_turn"},usage:{input_tokens:f.prompt_eval_count||0,output_tokens:f.eval_count||0}},yield {type:"message_stop"},c=!0;return}}catch{}}}c||(yield {type:"message_stop"});}finally{try{i.releaseLock();}catch{}}})()}estimateCost(e){let t=Math.round(JSON.stringify(e.messages).length/4),r=e.system?Math.round(e.system.length/4):0;return {estimatedInputTokens:t+r,estimatedCost:0,model:e.model,provider:"ollama"}}getHealth(){return {...this.health}}reportRateLimit(e){this.health.status="degraded",this.health.lastError=`Rate limited: ${e.utilization*100}% utilization`,this.health.lastCheckAt=Date.now();}buildRequestBody(e,t){if(e.tools&&e.tools.length>0)throw new Error(`OLLAMA_TOOLS_NOT_SUPPORTED: The Ollama provider received ${e.tools.length} tool definition(s) in params.tools, but this provider does not implement function calling in v1.3.x. For tool-use workflows route to 'anthropic-api' or 'openai-api' instead. Tracking issue: 1.4.0 ships an Ollama tool-use shim for models that support native function calling (llama3.1, mistral-nemo, etc.).`);let r=It(e.messages,e.system),s={model:e.model,messages:r,stream:t};return e.temperature!==void 0&&(s.options={temperature:e.temperature}),s}handleErrorResponse(e){let t=e.status;throw this.health.errorCount++,this.health.lastCheckAt=Date.now(),t===404?(this.health.status="unhealthy",this.health.lastError="Model not found",new Error("OLLAMA_MODEL_NOT_FOUND: Requested model is not available locally (404)")):(this.health.status="unhealthy",this.health.lastError=`Request failed with status ${t}`,new Error(`OLLAMA_ERROR: Request failed with status ${t}`))}recordSuccess(){this.health.status="healthy",this.health.lastCheckAt=Date.now(),this.health.errorCount=0,this.health.lastError=void 0;}};var J=class extends events.EventEmitter{constructor(){super(),this.setMaxListeners(50);}emitTyped(e){return this.emit(e.kind,e)}onTyped(e,t){return this.on(e,t)}offTyped(e,t){return this.off(e,t)}};var Xe=200;function oe(n,e){if(e instanceof E)return `${n}:failed:${e.code}:${e.message}`;let t=e?.message;return `${n}:failed:${t?.slice(0,Xe)||"unknown"}`}function $e(n){let e=n.filter(t=>t.includes(":failed:"));return e.length===0?false:e.every(t=>t.endsWith(":failed:request_too_large"))}var We=new Set(["anthropic-api","openai-api","ollama"]),U=class extends Error{code="ALL_PROVIDERS_FAILED";fallbackChain;constructor(e){super(`All providers failed. Chain: ${e.join(" \u2192 ")}`),this.name="RelayError",this.fallbackChain=e;}},z=class extends U{constructor(e){super(e);}};function Ye(n){return n.messages.some(e=>typeof e.content=="string"?false:Array.isArray(e.content)&&e.content.some(t=>t.type==="image"||t.type==="image_url"))}function le(n){if(!(n instanceof Error))return "unknown";let e=n.code;if(typeof e=="string"){if(e==="LICENSE_REJECTED")return "auth_failed";if(e==="GATEWAY_FETCH_FAILED"||e==="GATEWAY_VERIFICATION_FAILED"||e==="GATEWAY_LOAD_FAILED")return "gateway_unavailable"}let t=n.message.toLowerCase(),r=n.name.toLowerCase();return t.includes("prompt is too long")||t.includes("prompt too long")||t.includes("prompt_too_large")||t.includes("input length")||t.includes("input is too long")||t.includes("context_length_exceeded")||t.includes("context length exceeded")||t.includes("request too large")||t.includes("tokens exceed")||t.includes("exceeds the maximum")||t.includes("too many tokens")?"request_too_large":t.includes("code 3221226505")||t.includes("cli exited with code 3 ")||t.includes("cli stream exited with code 3 ")||t.includes("bun has crashed")||t.includes("illegal instruction")||t.includes("stream ended without producing any content")||t.includes("cli returned empty response")||t.includes("cli closed stdout without sending any response")||t.includes("kimi_stall_silent")?"transient":t.includes("credit balance is too low")||t.includes("credit balance too low")?"auth_failed":t.startsWith("openai_model_translation_required:")||t.startsWith("ollama_tools_not_supported:")||t.startsWith("multimodal_cli_too_old:")||t.startsWith("stream_json_cli_too_old:")?"provider_incompatible":t.includes("rate limit")||t.includes("429")||r.includes("ratelimit")?"rate_limited":t.includes("auth")||t.includes("401")||t.includes("403")||t.includes("unauthorized")||t.includes("forbidden")?"auth_failed":t.includes("timeout")||t.includes("timed out")||t.includes("aborted")||r==="aborterror"||r==="timeouterror"?"timeout":"unknown"}function qe(n){return [...n].sort((e,t)=>e.priority-t.priority)}var X=class n{healthMap=new Map;engineEvents=new J;static UNHEALTHY_TTL_DEFAULT_MS=300*1e3;static UNHEALTHY_TTL_AUTH_MS=30*1e3;getHealthStatus(e){let t=this.healthMap.get(e);if(!t)return "healthy";let r=t.ttlMs??n.UNHEALTHY_TTL_DEFAULT_MS;if(t.status==="unhealthy"&&Date.now()-t.updatedAt>=r){let s=Date.now()-t.updatedAt;return this.healthMap.delete(e),S("engine.recovered",{providerType:e,ageMs:s,ttlMs:r}),"healthy"}return t.status}markUnhealthy(e,t,r){this.healthMap.set(e,{status:"unhealthy",lastError:t,updatedAt:Date.now(),ttlMs:r}),S("engine.unhealthy",{providerType:e,reason:t,ttlMs:r});}markDegraded(e,t){this.healthMap.set(e,{status:"degraded",lastError:t,updatedAt:Date.now()});}markHealthy(e){this.healthMap.set(e,{status:"healthy",updatedAt:Date.now()});}resetHealth(){this.healthMap.clear();}async execute(e,t,r){let s=qe(t),i=[],a=Ye(e),o=crypto$1.randomUUID(),l=Date.now();for(let p of s){let c=p.type;if(this.getHealthStatus(c)==="unhealthy"){i.push(`${c}:skipped:unhealthy`);continue}if(a&&!p.supportsMultimodal){i.push(`${c}:skipped:no_multimodal`);continue}let m=ae(e.model);if(m&&m!==c&&We.has(c)){i.push(`${c}:skipped:wrong_provider`);continue}let y=r?.maxSameProviderRetries??2,_=0,d=null,f=false;for(;!f;)try{let u=await p.execute(e,{signal:r?.signal});return f=!0,this.markHealthy(c),_>0&&i.push(`${c}:retry_success:${_}`),i.push(`${c}:success`),this.emitEngineEvent({kind:"stream_complete",callId:o,id:crypto$1.randomUUID(),ts:Date.now(),stitched:!1,segments:[{provider:c,inputTokens:u.usage.input_tokens,outputTokens:u.usage.output_tokens,costUsd:u.costUsd,userCostUsd:u.userCostUsd??0}],provider:c,model:u.model,inputTokens:u.usage.input_tokens,outputTokens:u.usage.output_tokens,costUsd:u.costUsd,userCostUsd:u.userCostUsd,durationMs:u.durationMs??Date.now()-l,stopReason:u.stop_reason,fallbackChain:i,rateLimitHit:!1,securityState:r?.licenseState}),{...u,fallbackChain:i}}catch(u){if(u?.name==="AbortError"||r?.signal?.aborted)throw u;if(le(u)==="transient"&&_<y){_++;let R=250*Math.pow(2,_-1);S("engine.transient-retry",{id:c,attempt:_,backoffMs:R,msg:String(u?.message||"").slice(0,120)}),i.push(`${c}:retry:${_}`),await new Promise(b=>setTimeout(b,R));continue}d=u;break}if(!f){let u=d;switch(le(u)){case "rate_limited":p.reportRateLimit({provider:c,utilization:1,resetsAt:Date.now()+6e4}),this.markDegraded(c,"rate_limited"),i.push(`${c}:failed:rate_limited`);break;case "auth_failed":this.markUnhealthy(c,"auth_failed",n.UNHEALTHY_TTL_AUTH_MS),i.push(`${c}:failed:auth_failed`);break;case "timeout":i.push(`${c}:failed:timeout`);break;case "provider_incompatible":i.push(`${c}:incompatible`);break;case "gateway_unavailable":this.markUnhealthy(c,"gateway_unavailable"),i.push(oe(c,u));break;case "request_too_large":i.push(`${c}:failed:request_too_large`);break;case "transient":this.markDegraded(c,"transient_exhausted"),i.push(`${c}:failed:transient_exhausted`);break;default:i.push(oe(c,u));break}if(r?.subscriptionOnly&&c==="claude-cli"){i.push("subscription_only:cascade_halted");break}}}throw S("engine.cascade-exhausted",{chain:i,mode:"execute"}),$e(i)?new z(i):new U(i)}async stream(e,t,r){let s=crypto$1.randomUUID(),i=qe(t),a=Ye(e),o=Date.now(),l=[],p=null,c=null;for(let u of i){let h=u.type;if(this.getHealthStatus(h)==="unhealthy"){l.push(`${h}:skipped:unhealthy`);continue}if(a&&!u.supportsMultimodal){l.push(`${h}:skipped:no_multimodal`);continue}if(!u.supportsStreaming){l.push(`${h}:skipped:no_streaming`);continue}let R=ae(e.model);if(R&&R!==h&&We.has(h)){l.push(`${h}:skipped:wrong_provider`);continue}try{c=await u.streamRaw(e,{signal:r?.signal}),p=u,l.push(`${h}:success`);break}catch(b){if(b?.name==="AbortError"||r?.signal?.aborted)throw b;switch(le(b)){case "rate_limited":u.reportRateLimit({provider:h,utilization:1,resetsAt:Date.now()+6e4}),this.markDegraded(h,"rate_limited"),l.push(`${h}:failed:rate_limited`);break;case "auth_failed":this.markUnhealthy(h,"auth_failed",n.UNHEALTHY_TTL_AUTH_MS),l.push(`${h}:failed:auth_failed`);break;case "timeout":l.push(`${h}:failed:timeout`);break;case "provider_incompatible":l.push(`${h}:incompatible`);break;case "gateway_unavailable":this.markUnhealthy(h,"gateway_unavailable"),l.push(oe(h,b));break;case "request_too_large":l.push(`${h}:failed:request_too_large`);break;case "transient":this.markDegraded(h,"transient_exhausted"),l.push(`${h}:failed:transient_exhausted`);break;default:l.push(oe(h,b));break}if(r?.subscriptionOnly&&h==="claude-cli"){l.push("subscription_only:cascade_halted");break}}}if(!p||!c)throw S("engine.cascade-exhausted",{chain:l,mode:"stream"}),$e(l)?new z(l):new U(l);let m=this,y=r?.maxMidStreamFailovers??2,_=new Set([p]);async function*d(){let u=p,h=c[Symbol.asyncIterator](),R={role:"assistant",content:[]},b={segments:[]},I=je(u.type);b.segments.push(I);let Te=0,Z=null,ee=false,fe=y,H="",B="";for(m.emitEngineEvent({kind:"segment_start",callId:s,id:crypto$1.randomUUID(),ts:Date.now(),provider:u.type});;)try{for(;;){let A=await h.next();if(A.done)break;let L=A.value,k=Mt(L,Te,R,ee,Z);if(k.action==="suppress"){"coalesceMode"in k&&(Z=k.coalesceMode),k.clearedJustFailedOver&&(ee=!1);continue}k.coalesceModeCleared&&(Z=null),k.clearedJustFailedOver&&(ee=!1);let v=k.event;if(Ot(R,v),v.type==="message_start"){if(b.segments.length>1)continue;H=v.message?.model||H,v.message?.usage?.input_tokens!=null&&(I.inputTokens=v.message.usage.input_tokens);}if(v.type==="message_delta"&&(v.delta?.stop_reason&&(B=v.delta.stop_reason),v.usage)){Lt(I,u.type,v.usage),yield {...v,usage:void 0};continue}if(v.type==="message_stop")break;yield v;}if(R.content.length===0&&I.outputTokens===0)throw new Error("stream ended without producing any content");m.markHealthy(u.type),m.emitEngineEvent({kind:"segment_complete",callId:s,id:crypto$1.randomUUID(),ts:Date.now(),provider:u.type,usage:{...I}});let w=Je(b.segments);yield {type:"message_delta",delta:{stop_reason:B||"end_turn"},usage:w},yield {type:"message_stop"},m.emitEngineEvent({kind:"stream_complete",callId:s,id:crypto$1.randomUUID(),ts:Date.now(),stitched:b.segments.length>1,segments:b.segments.map(A=>({...A})),provider:b.segments.map(A=>A.provider).join("+"),model:H,inputTokens:w.input_tokens,outputTokens:w.output_tokens,costUsd:b.segments.reduce((A,L)=>A+L.costUsd,0),userCostUsd:b.segments.reduce((A,L)=>A+L.userCostUsd,0),durationMs:Date.now()-o,stopReason:B||"end_turn",fallbackChain:l,rateLimitHit:!1,securityState:r?.licenseState});return}catch(w){if(w?.name==="AbortError"||r?.signal?.aborted)throw w;let A=le(w);try{await h.return?.();}catch{}if(m.markUnhealthy(u.type,String(w)),m.emitEngineEvent({kind:"mid_stream_failover_attempt",callId:s,id:crypto$1.randomUUID(),ts:Date.now(),from:u.type,bytesBuffered:ze(R),errorClass:A,retriesLeft:fe}),l.push(`${u.type}:partial(${ze(R)}b)`),fe<=0)throw m.emitTerminalExhaustion(s,b,H,B,l,o,r?.licenseState),w;let L=Dt(R.content),k=[...e.messages,{role:"assistant",content:L}],v=null,te=null,Ie=u;for(;;){let T=m.nextHealthyProvider(i,Ie,a,_);if(!T)break;Ie=T,_.add(T);let tt={...e,messages:k,...T.type!=="anthropic-api"&&T.type!=="claude-cli"?{system:Nt(e.system,"[SYSTEM]: CONTINUE_LAST_RESPONSE_WITHOUT_REPEATING")}:{}};try{te=await T.streamRaw(tt,{signal:r?.signal}),v=T;break}catch(xe){m.markUnhealthy(T.type,String(xe)),l.push(`${T.type}:failed_connect:${String(xe).slice(0,Xe)}`);}}if(!v||!te)throw m.emitTerminalExhaustion(s,b,H,B,l,o,r?.licenseState),w;l.push(`${v.type}:stitched`),m.emitEngineEvent({kind:"mid_stream_failover_success",callId:s,id:crypto$1.randomUUID(),ts:Date.now(),from:u.type,to:v.type}),Te=Ut(R)+1,fe--,u=v,h=te[Symbol.asyncIterator](),ee=true,Z=null,I=je(u.type),b.segments.push(I),m.emitEngineEvent({kind:"segment_start",callId:s,id:crypto$1.randomUUID(),ts:Date.now(),provider:u.type});}}let f=d();return r?.licenseState==="SEC_DEGRADED"&&(f=Ft(f)),M(f)}nextHealthyProvider(e,t,r,s){let i=e.indexOf(t);if(i===-1)return null;for(let a=i+1;a<e.length;a++){let o=e[a];if(!s.has(o)&&!(r&&!o.supportsMultimodal)&&o.supportsStreaming)return o}return null}emitEngineEvent(e){this.engineEvents.emitTyped(e);}emitTerminalExhaustion(e,t,r,s,i,a,o){let l=Je(t.segments);this.emitEngineEvent({kind:"stream_complete",callId:e,id:crypto$1.randomUUID(),ts:Date.now(),stitched:t.segments.length>1,segments:t.segments.map(p=>({...p})),provider:t.segments.map(p=>p.provider).join("+"),model:r,inputTokens:l.input_tokens,outputTokens:l.output_tokens,costUsd:t.segments.reduce((p,c)=>p+c.costUsd,0),userCostUsd:t.segments.reduce((p,c)=>p+c.userCostUsd,0),durationMs:Date.now()-a,stopReason:s||"error",fallbackChain:i,rateLimitHit:false,securityState:o});}};function je(n){return {provider:n,inputTokens:0,outputTokens:0,costUsd:0,userCostUsd:0}}function Lt(n,e,t){typeof t.input_tokens=="number"&&(n.inputTokens=t.input_tokens),typeof t.output_tokens=="number"&&(e==="anthropic-api"||e==="claude-cli"?n.outputTokens+=t.output_tokens:n.outputTokens=t.output_tokens);}function Je(n){return {input_tokens:n[n.length-1]?.inputTokens??0,output_tokens:n.reduce((e,t)=>e+t.outputTokens,0)}}function Ot(n,e){if(e.type==="content_block_start"){let t=e.content_block;t?.type==="text"?n.content.push({type:"text",text:t.text||"",open:true}):t?.type==="tool_use"&&n.content.push({type:"tool_use",id:t.id,name:t.name,input:{},_partialJson:"",open:true});}else if(e.type==="content_block_delta"){let t=n.content[n.content.length-1];if(!t)return;t.type==="text"&&e.delta?.type==="text_delta"?t.text+=e.delta.text||"":t.type==="tool_use"&&e.delta?.type==="input_json_delta"&&(t._partialJson=(t._partialJson||"")+(e.delta.partial_json||""));}else if(e.type==="content_block_stop"){for(let t=n.content.length-1;t>=0;t--)if(n.content[t].open){if(n.content[t].open=false,n.content[t].type==="tool_use"){let r=n.content[t];try{r.input=JSON.parse(r._partialJson||"{}");}catch{}delete r._partialJson;}break}}}function Mt(n,e,t,r,s){if(s&&n.type==="content_block_delta"&&n.index===s.fromIndex)return {action:"yield",event:{...n,index:s.toIndex}};if(s&&n.type==="content_block_stop"&&n.index===s.fromIndex)return {action:"suppress",coalesceMode:null,coalesceModeCleared:true};if(r&&n.type==="content_block_start"&&n.content_block?.type==="text"){let a=t.content[t.content.length-1];if(a?.type==="text"&&a.open===true)return {action:"suppress",coalesceMode:{fromIndex:n.index+e,toIndex:t.content.length-1},clearedJustFailedOver:true}}let i=r&&n.type==="content_block_start";return typeof n.index=="number"&&e>0?{action:"yield",event:{...n,index:n.index+e},clearedJustFailedOver:i}:{action:"yield",event:n,clearedJustFailedOver:i}}function Dt(n){let e=[];for(let t of n)t.type==="text"?t.text&&e.push({type:"text",text:t.text}):t.type==="tool_use"&&!t.open&&t.input&&Object.keys(t.input).length>0&&e.push({type:"tool_use",id:t.id,name:t.name,input:t.input});return e}function Nt(n,e){return n?`${n}
10
+ -----END PUBLIC KEY-----`,Ee={version:"1.0.0",cliVersion:"2.1.87",eventTypes:{system:"system",streamEvent:"stream_event",assistant:"assistant",result:"result"},fieldMappings:{sessionId:"session_id",model:"model",cliVersion:"claude_code_version",content:"content",usage:"usage",inputTokens:"input_tokens",outputTokens:"output_tokens",stopReason:"stop_reason",isError:"is_error",totalCostUsd:"total_cost_usd",durationMs:"duration_ms",error:"error",event:"event"},syntheticModel:"<synthetic>"},D=class{verificationFailures=0;cachedManifest=null;currentSecurityState="SEC_NOMINAL";async verifyManifest(e){try{let{compactVerify:t,importSPKI:r}=await import('jose'),s=await r(ve,"RS256"),{payload:i}=await t(e,s),a=JSON.parse(new TextDecoder().decode(i));return this.verificationFailures=0,this.cachedManifest=a,this.currentSecurityState="SEC_NOMINAL",{manifest:a,securityState:"SEC_NOMINAL"}}catch{if(this.verificationFailures++,this.verificationFailures>=3){this.currentSecurityState="SEC_DEGRADED";let t=2e3+Math.random()*3e3;return await new Promise(r=>setTimeout(r,t)),console.warn("[inference-relay] Logic synchronization mismatch. Routing via local recovery."),{manifest:Ee,securityState:"SEC_DEGRADED"}}return this.cachedManifest?(this.currentSecurityState="SEC_CACHE",{manifest:this.cachedManifest,securityState:"SEC_CACHE"}):(this.currentSecurityState="SEC_RECOVERY",{manifest:Ee,securityState:"SEC_RECOVERY"})}}getSecurityState(){return this.currentSecurityState}clearCache(){this.cachedManifest=null,this.verificationFailures=0,this.currentSecurityState="SEC_NOMINAL";}},ct=new D;function De(){ct.clearCache();}function ut(){return Ee}function dt(n,e){return e.split(".").reduce((t,r)=>{if(!(r==="__proto__"||r==="constructor"||r==="prototype"))return t&&t[r]},n)}function C(n,...e){process.env.IR_DEBUG==="1"&&console.error(`[ir-debug:${n}]`,...e);}var gt=process.env.IR_GATEWAY_URL||"https://api.inference-relay.com";function Se(n){return path.join(os.homedir(),".inference-relay","gateway",n)}function Fe(n){return path.join(Se(n),"bundle.js")}function He(n){return path.join(Se(n),"meta.json")}var E=class extends Error{constructor(t,r,s){super(t);this.code=r;this.cause=s;this.name="GatewayLoaderError";}code;cause},x=class extends E{constructor(e,t){super(e,"GATEWAY_FETCH_FAILED",t);}},G=new Map,ft=[0,1e3,4e3];function _t(n){let e=(r,s)=>{if(!Array.isArray(r)||r.length===0)return null;let i=[];for(let a of r){let o=typeof a=="string"?Number(a):a;if(typeof o!="number"||!Number.isFinite(o)||o<0)return C("loader.retry-config-invalid",{source:s,badValue:a}),null;i.push(o);}return i};if(n!==void 0){let r=e(n,"option");if(r)return r}let t=process.env.IR_GATEWAY_RETRY_DELAYS_MS;if(t){let r=e(t.split(",").map(s=>s.trim()),"env");if(r)return r}return ft}var N=class{static async load(e,t,r=gt,s){let i=`${t}:${e.slice(-8)}`;if(G.has(i))return G.get(i);try{let m=await this.tryLoadFromDisk(t);if(m)return C("loader.cache-hit",{libraryVersion:t}),G.set(i,m),m}catch{}C("loader.cache-miss",{libraryVersion:t});let{bytes:a,sha256:o,signature:l,libraryVersion:p}=await this.fetchFromAuthority(e,t,r,s);if(crypto$1.createHash("sha256").update(a).digest("hex")!==o)throw new E("[ir-system]: GATEWAY_VERIFICATION_FAILED (REASON: HASH_MISMATCH)","GATEWAY_VERIFICATION_FAILED");await this.verifySignature(l,p,o);try{let m=Se(t);fs.mkdirSync(m,{recursive:!0}),fs.writeFileSync(Fe(t),a),fs.writeFileSync(He(t),JSON.stringify({sha256:o,signature:l,libraryVersion:p}));}catch{}let g=this.loadIntoVm(a.toString("utf-8"));return G.set(i,g),g}static async loadFromDisk(e){if(!fs.existsSync(e))throw new E(`[ir-system]: GATEWAY_LOAD_FAILED (REASON: BUNDLE_NOT_FOUND at ${e})`,"GATEWAY_LOAD_FAILED");let t=fs.readFileSync(e,"utf-8");return this.loadIntoVm(t)}static async tryLoadFromDisk(e){let t=Fe(e),r=He(e);if(!fs.existsSync(t)||!fs.existsSync(r))return null;let s=fs.readFileSync(t),i=JSON.parse(fs.readFileSync(r,"utf-8"));return crypto$1.createHash("sha256").update(s).digest("hex")!==i.sha256?null:(await this.verifySignature(i.signature,i.libraryVersion,i.sha256),this.loadIntoVm(s.toString("utf-8")))}static async fetchFromAuthority(e,t,r,s){let i,a=_t(s?.retryDelaysMs),o;for(let g=0;g<a.length;g++){let m=a[g];m>0&&await new Promise(y=>setTimeout(y,m)),C("loader.fetch",{url:`${r}/v1/gateway/fetch`,libraryVersion:t,attempt:g+1,delayMs:m});try{let y=await fetch(`${r}/v1/gateway/fetch`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify({libraryVersion:t})});if(C("loader.fetch-result",{status:y.status,attempt:g+1}),y.status===401||y.status===403)throw new E("[ir-system]: LICENSE_REJECTED (REASON: GATEWAY_FETCH_UNAUTHORIZED)","LICENSE_REJECTED");if(y.status>=500&&y.status<600){o=new x(`[ir-system]: GATEWAY_FETCH_FAILED (REASON: HTTP_${y.status})`);continue}if(!y.ok)throw new x(`[ir-system]: GATEWAY_FETCH_FAILED (REASON: HTTP_${y.status})`);i=await y.json(),o=null;break}catch(y){if(y instanceof E&&y.code==="LICENSE_REJECTED")throw y;o=y;}}if(o||!i)throw o instanceof E?o:new x("[ir-system]: GATEWAY_FETCH_FAILED (REASON: NETWORK_ERROR)",o);let l;try{l=await fetch(i.bundleUrl);}catch(g){throw new x("[ir-system]: GATEWAY_FETCH_FAILED (REASON: BUNDLE_DOWNLOAD_FAILED)",g)}if(!l.ok)throw new x(`[ir-system]: GATEWAY_FETCH_FAILED (REASON: BUNDLE_HTTP_${l.status})`);let p=await l.arrayBuffer();return {bytes:Buffer.from(p),sha256:i.sha256,signature:i.signature,libraryVersion:i.libraryVersion,bundleVersion:i.bundleVersion}}static async verifySignature(e,t,r){try{let{compactVerify:s,importSPKI:i}=await import('jose'),a=await i(ve,"RS256"),{payload:o}=await s(e,a),l=JSON.parse(new TextDecoder().decode(o));if(l.purpose!=="gateway-bundle")throw new E("[ir-system]: GATEWAY_VERIFICATION_FAILED (REASON: PURPOSE_MISMATCH)","GATEWAY_VERIFICATION_FAILED");if(l.libraryVersion!==t)throw new E("[ir-system]: GATEWAY_VERIFICATION_FAILED (REASON: LIBRARY_VERSION_MISMATCH)","GATEWAY_VERIFICATION_FAILED");if(l.sha256!==r)throw new E("[ir-system]: GATEWAY_VERIFICATION_FAILED (REASON: SHA256_MISMATCH)","GATEWAY_VERIFICATION_FAILED")}catch(s){throw s instanceof E?s:new E("[ir-system]: GATEWAY_VERIFICATION_FAILED (REASON: SIGNATURE_INVALID)","GATEWAY_VERIFICATION_FAILED",s)}}static loadIntoVm(e){try{let t=path.join(os.homedir(),".inference-relay","gateway","runtime.cjs"),r=path.dirname(t),s=module$1.createRequire(t),i={exports:{}};vm.compileFunction(e,["exports","require","module","__filename","__dirname"],{filename:"<gateway-bundle>"})(i.exports,s,i,t,r);let o=i.exports;if(!o.ClaudeCLIProvider)throw new E("[ir-system]: GATEWAY_LOAD_FAILED (REASON: MISSING_CLAUDE_CLI_EXPORT)","GATEWAY_LOAD_FAILED");return o}catch(t){throw t instanceof E?t:new E("[ir-system]: GATEWAY_LOAD_FAILED (REASON: VM_EXECUTION_ERROR)","GATEWAY_LOAD_FAILED",t)}}static clearCache(){G.clear();}};var Ge="1.6.11",ie=class extends S{type="claude-cli";supportsStreaming=true;supportsMultimodal=true;platformRequirement="desktop";licenseKey;loadOptions;loadedProvider=null;loadingPromise=null;health={status:"healthy",lastCheckAt:Date.now(),errorCount:0};constructor(e,t){super(),this.licenseKey=e,this.loadOptions=t;}async ensureLoaded(){return this.loadedProvider?this.loadedProvider:this.loadingPromise?this.loadingPromise:(this.loadingPromise=(async()=>{try{let t=(await N.load(this.licenseKey,Ge,void 0,this.loadOptions)).ClaudeCLIProvider,r=new t;return this.loadedProvider=r,r}catch(e){throw this.loadingPromise=null,this.health.status="unhealthy",this.health.errorCount++,e instanceof E?this.health.lastError=e.message:e instanceof Error&&(this.health.lastError=e.message),e}})(),this.loadingPromise)}async warmup(){await this.ensureLoaded();}async probe(){let{detectCLI:e}=await Promise.resolve().then(()=>(se(),Ke));return e()}async execute(e,t){return (await this.ensureLoaded()).execute(e,t)}async streamRaw(e,t){return (await this.ensureLoaded()).streamRaw(e,t)}estimateCost(e){if(this.loadedProvider)return this.loadedProvider.estimateCost(e);let r=JSON.stringify(e.messages).length/4+5e3,s=(r*3+1e3*15)/1e6;return {estimatedInputTokens:Math.round(r),estimatedCost:Math.round(s*1e4)/1e4,model:e.model,provider:"claude-cli"}}getHealth(){return this.loadedProvider?this.loadedProvider.getHealth():{...this.health}}reportRateLimit(e){this.loadedProvider?this.loadedProvider.reportRateLimit(e):(this.health.status="degraded",this.health.lastError=`Rate limited: ${e.utilization*100}% utilization`,this.health.lastCheckAt=Date.now());}},ae=class extends S{type="kimi-cli";supportsStreaming=true;supportsMultimodal=false;platformRequirement="desktop";licenseKey;loadOptions;loadedProvider=null;loadingPromise=null;health={status:"healthy",lastCheckAt:Date.now(),errorCount:0};constructor(e,t){super(),this.licenseKey=e,this.loadOptions=t;}async ensureLoaded(){return this.loadedProvider?this.loadedProvider:this.loadingPromise?this.loadingPromise:(this.loadingPromise=(async()=>{try{let t=(await N.load(this.licenseKey,Ge,void 0,this.loadOptions)).KimiCLIProvider;if(!t)throw new E("[ir-system]: GATEWAY_LOAD_FAILED (REASON: STALE_BUNDLE_NO_KIMI_CLI)","GATEWAY_LOAD_FAILED");let r=new t;return this.loadedProvider=r,r}catch(e){throw this.loadingPromise=null,this.health.status="unhealthy",this.health.errorCount++,this.health.lastError=e instanceof Error?e.message:String(e),e}})(),this.loadingPromise)}async warmup(){await this.ensureLoaded();}async probe(){let{existsSync:e}=await import('fs'),{homedir:t}=await import('os'),{join:r}=await import('path'),s=process.env.KIMI_CODE_HOME||r(t(),".kimi-code"),i=process.env.IR_KIMI_BIN||r(s,"bin","kimi"),a=e(i),o=e(r(s,"credentials"))||e(r(s,"oauth"));return {available:a,authenticated:o,subscriptionType:o?"kimi-code":void 0}}async execute(e,t){return (await this.ensureLoaded()).execute(e,t)}async streamRaw(e,t){return (await this.ensureLoaded()).streamRaw(e,t)}estimateCost(e){if(this.loadedProvider)return this.loadedProvider.estimateCost(e);let t=JSON.stringify(e.messages).length/4;return {estimatedInputTokens:Math.round(t+2e3),estimatedCost:0,model:e.model,provider:"kimi-cli"}}getHealth(){return this.loadedProvider?this.loadedProvider.getHealth():{...this.health}}reportRateLimit(e){this.loadedProvider?this.loadedProvider.reportRateLimit(e):(this.health.status="degraded",this.health.lastError=`Rate limited: ${e.utilization*100}% utilization`,this.health.lastCheckAt=Date.now());}};var Y=[{canonical:"claude-haiku-4-5",apiId:"claude-haiku-4-5-20251001",provider:"anthropic-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:2e5},pricing:[.8,4]},{canonical:"claude-sonnet-4-6",apiId:"claude-sonnet-4-20250514",provider:"anthropic-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:2e5},pricing:[3,15]},{canonical:"claude-opus-4-6",apiId:"claude-opus-4-20250514",provider:"anthropic-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:2e5},pricing:[15,75]},{canonical:"gpt-4.1",apiId:"gpt-4.1",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:1e6},pricing:[2,8]},{canonical:"gpt-4.1-mini",apiId:"gpt-4.1-mini",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:1e6},pricing:[.4,1.6]},{canonical:"gpt-4.1-nano",apiId:"gpt-4.1-nano",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:1e6},pricing:[.1,.4]},{canonical:"gpt-4o",apiId:"gpt-4o",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:128e3},pricing:[2.5,10]},{canonical:"gpt-4o-mini",apiId:"gpt-4o-mini",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:true,contextWindow:128e3},pricing:[.15,.6]},{canonical:"o4-mini",apiId:"o4-mini",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:false,contextWindow:2e5},pricing:[1.1,4.4]},{canonical:"o3",apiId:"o3",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:false,contextWindow:2e5},pricing:[.4,1.6]},{canonical:"o3-mini",apiId:"o3-mini",provider:"openai-api",capabilities:{streaming:true,tools:true,multimodal:false,contextWindow:2e5},pricing:[1.1,4.4]},{canonical:"o1",apiId:"o1",provider:"openai-api",capabilities:{streaming:false,tools:false,multimodal:false,contextWindow:2e5},pricing:[15,60]},{canonical:"llama3.1",apiId:"llama3.1",provider:"ollama",capabilities:{streaming:true,tools:true,multimodal:false,contextWindow:128e3}},{canonical:"mistral",apiId:"mistral",provider:"ollama",capabilities:{streaming:true,tools:false,multimodal:false,contextWindow:32e3}},{canonical:"mistral-nemo",apiId:"mistral-nemo",provider:"ollama",capabilities:{streaming:true,tools:true,multimodal:false,contextWindow:128e3}},{canonical:"llava",apiId:"llava",provider:"ollama",capabilities:{streaming:true,tools:false,multimodal:true,contextWindow:32e3}},{canonical:"qwen2:0.5b",apiId:"qwen2:0.5b",provider:"ollama",capabilities:{streaming:true,tools:false,multimodal:false,contextWindow:32e3}}],$=Object.fromEntries(Y.filter(n=>n.canonical!==n.apiId).map(n=>[n.canonical,n.apiId]));$["claude-sonnet-4"]="claude-sonnet-4-20250514";$["claude-opus-4"]="claude-opus-4-20250514";$["claude-haiku-4"]="claude-haiku-4-5-20251001";function Ce(n){return $[n]||n}function St(n){return Y.find(e=>e.canonical===n)}function oe(n){return Y.find(e=>e.canonical===n)?.provider}function Ct(n){return Y.filter(e=>e.provider===n)}var wt="https://api.anthropic.com/v1/messages",Pt="2023-06-01",we={"claude-sonnet-4-5-20250514":[3,15],"claude-sonnet-4-20250514":[3,15],"claude-haiku-4-5-20250414":[.8,4],"claude-opus-4-20250514":[15,75]};function Ve(n){if(we[n])return we[n];for(let[e,t]of Object.entries(we))if(n.startsWith(e.split("-20")[0]))return t;return [3,15]}var W=class extends S{type="anthropic-api";supportsStreaming=true;supportsMultimodal=true;platformRequirement="any";apiKey;baseUrl;dangerousDirectBrowserAccess;health={status:"healthy",lastCheckAt:Date.now(),errorCount:0};constructor(e){super(),this.apiKey=e.apiKey,this.baseUrl=e.baseUrl?.replace(/\/$/,"")||wt,this.dangerousDirectBrowserAccess=e.dangerousDirectBrowserAccess??false;}async probe(){try{let e=await fetch(this.baseUrl,{method:"POST",headers:this.buildHeaders(),body:JSON.stringify({model:"claude-haiku-4-5-20250414",max_tokens:1,messages:[{role:"user",content:"hi"}]})});return e.status===401?{available:!0,authenticated:!1}:e.ok||e.status===429?{available:!0,authenticated:!0,rateLimitTier:e.headers.get("anthropic-ratelimit-tier")??void 0}:e.status===529?{available:!0,authenticated:!0}:{available:!1,authenticated:!1}}catch{return {available:false,authenticated:false}}}async execute(e,t){let r=Date.now(),s=this.buildRequestBody(e,false),i=await fetch(this.baseUrl,{method:"POST",headers:this.buildHeaders(),body:JSON.stringify(s),signal:t?.signal});if(!i.ok)throw this.handleErrorResponse(i),new Error("unreachable");let a=await i.json(),o=Date.now()-r;this.recordSuccess();let[l,p]=Ve(a.model||e.model),c=((a.usage?.input_tokens||0)*l+(a.usage?.output_tokens||0)*p)/1e6,g={},m=["request-id","x-request-id","anthropic-ratelimit-requests-limit","anthropic-ratelimit-requests-remaining","anthropic-ratelimit-requests-reset","anthropic-ratelimit-tokens-limit","anthropic-ratelimit-tokens-remaining","anthropic-ratelimit-tokens-reset","anthropic-ratelimit-tier"];for(let y of m){let _=i.headers.get(y);_&&(g[y]=_);}return {content:a.content||[],model:a.model||e.model,usage:{input_tokens:a.usage?.input_tokens||0,output_tokens:a.usage?.output_tokens||0},stop_reason:a.stop_reason||"end_turn",provider:"anthropic-api",costUsd:Math.round(c*1e6)/1e6,durationMs:o,fallbackChain:["anthropic-api:success"],headers:Object.keys(g).length>0?g:void 0}}async streamRaw(e,t){let r=this.buildRequestBody(e,true),s=await fetch(this.baseUrl,{method:"POST",headers:this.buildHeaders(),body:JSON.stringify(r),signal:t?.signal});if(s.ok||this.handleErrorResponse(s),!s.body)throw new Error("ANTHROPIC_API_ERROR: Response body is null \u2014 streaming unavailable");let i=s.body.getReader(),a=new TextDecoder;return (async function*(){let o="",l=false;try{for(;;){let{done:p,value:c}=await i.read();if(p)break;o+=a.decode(c,{stream:!0});let g=o.split(`
11
+ `);o=g.pop()||"";let m=null;for(let y of g)if(y.startsWith("event: "))m=y.slice(7).trim();else if(y.startsWith("data: ")){let _=y.slice(6);if(m&&_)try{let d=JSON.parse(_);d.type==="message_stop"&&(l=!0),yield d;}catch{console.warn("[inference-relay] Skipped malformed SSE data:",_.substring(0,100));}m=null;}else y.trim()===""&&(m=null);}l||(yield {type:"message_stop"});}finally{try{i.releaseLock();}catch{}}})()}estimateCost(e){let t=Math.round(JSON.stringify(e.messages).length/4),r=e.system?Math.round(e.system.length/4):0,s=t+r,[i,a]=Ve(e.model),o=(s*i+1e3*a)/1e6;return {estimatedInputTokens:s,estimatedCost:Math.round(o*1e4)/1e4,model:e.model,provider:"anthropic-api"}}getHealth(){return {...this.health}}reportRateLimit(e){this.health.status="degraded",this.health.lastError=`Rate limited: ${e.utilization*100}% utilization`,this.health.lastCheckAt=Date.now();}buildHeaders(){let e={"content-type":"application/json","x-api-key":this.apiKey,"anthropic-version":Pt};return this.dangerousDirectBrowserAccess&&(e["anthropic-dangerous-direct-browser-access"]="true"),e}buildRequestBody(e,t){let r={model:Ce(e.model),max_tokens:e.max_tokens,messages:e.messages};return t&&(r.stream=true),e.system&&(r.system=e.system),e.temperature!==void 0&&(r.temperature=e.temperature),e.tools&&e.tools.length>0&&(r.tools=e.tools),e.metadata&&(r.metadata=e.metadata),r}handleErrorResponse(e){let t=e.status;if(t===401)throw this.health.status="unhealthy",this.health.lastError="Authentication failed",this.health.errorCount++,this.health.lastCheckAt=Date.now(),new Error("ANTHROPIC_AUTH_ERROR: Invalid API key (401)");if(t===429){let r=e.headers.get("retry-after"),s=r?parseInt(r,10)*1e3:6e4;this.health.status="degraded",this.health.lastError=`Rate limited \u2014 retry after ${s}ms`,this.health.lastCheckAt=Date.now();let i=new Error(`ANTHROPIC_RATE_LIMIT: Too many requests (429). Retry after ${s}ms`);throw i.retryAfterMs=s,i}throw t===529?(this.health.status="degraded",this.health.lastError="API overloaded",this.health.lastCheckAt=Date.now(),new Error("ANTHROPIC_OVERLOADED: API is temporarily overloaded (529)")):(this.health.errorCount++,this.health.lastCheckAt=Date.now(),new Error(`ANTHROPIC_API_ERROR: Request failed with status ${t}`))}recordSuccess(){this.health.status="healthy",this.health.lastCheckAt=Date.now(),this.health.errorCount=0,this.health.lastError=void 0;}};var kt="https://api.openai.com/v1/chat/completions",Ye=["gpt-","o1","o3","o4","chatgpt-","text-","davinci","babbage","ft:"],Pe={"gpt-4.1":[2,8],"gpt-4.1-mini":[.4,1.6],"gpt-4.1-nano":[.1,.4],"gpt-4o":[2.5,10],"gpt-4o-mini":[.15,.6],"o4-mini":[1.1,4.4],o3:[.4,1.6],"o3-mini":[1.1,4.4],o1:[15,60],"o1-mini":[3,12],"gpt-4-turbo":[10,30],"gpt-4":[30,60],"gpt-3.5-turbo":[.5,1.5]};function $e(n){if(Pe[n])return Pe[n];for(let[e,t]of Object.entries(Pe))if(n.startsWith(e))return t;return [2.5,10]}function It(n){return typeof n=="string"?n:n.filter(e=>e.type==="text"&&e.text).map(e=>e.text).join("")}function Tt(n,e){let t=[];e&&t.push({role:"system",content:e});for(let r of n){let s=It(r.content);if(r.role==="assistant"&&Array.isArray(r.content)){let i=r.content.filter(a=>a.type==="tool_use");if(i.length>0){t.push({role:"assistant",content:s||"",tool_calls:i.map(a=>({id:a.id||`call_${Math.random().toString(36).slice(2)}`,type:"function",function:{name:a.name,arguments:typeof a.input=="string"?a.input:JSON.stringify(a.input)}}))});continue}}t.push({role:r.role,content:s});}return t}function We(n){switch(n){case "stop":return "end_turn";case "length":return "max_tokens";case "tool_calls":case "function_call":return "tool_use";default:return "end_turn"}}var q=class extends S{type="openai-api";supportsStreaming=true;supportsMultimodal=true;platformRequirement="any";apiKey;baseUrl;health={status:"healthy",lastCheckAt:Date.now(),errorCount:0};constructor(e){super(),this.apiKey=e.apiKey,this.baseUrl=e.baseUrl?.replace(/\/$/,"")||kt;}async probe(){try{let e=this.baseUrl.replace(/\/chat\/completions$/,"/models"),t=await fetch(e,{method:"GET",headers:this.buildHeaders()});return t.status===401?{available:!0,authenticated:!1}:t.ok||t.status===429?{available:!0,authenticated:!0}:{available:!1,authenticated:!1}}catch{return {available:false,authenticated:false}}}async execute(e,t){let r=Date.now(),s=this.buildRequestBody(e,false),i=await fetch(this.baseUrl,{method:"POST",headers:this.buildHeaders(),body:JSON.stringify(s),signal:t?.signal});if(!i.ok)throw this.handleErrorResponse(i),new Error("unreachable");let a=await i.json(),o=Date.now()-r;this.recordSuccess();let l=a.choices?.[0],p=[];if(l?.message?.content&&p.push({type:"text",text:l.message.content}),l?.message?.tool_calls)for(let u of l.message.tool_calls)p.push({type:"tool_use",id:u.id,name:u.function?.name,input:u.function?.arguments?JSON.parse(u.function.arguments):{}});let c=a.usage?.prompt_tokens||0,g=a.usage?.completion_tokens||0,[m,y]=$e(a.model||e.model),_=(c*m+g*y)/1e6,d={},f=["x-request-id","x-ratelimit-limit-requests","x-ratelimit-remaining-requests","x-ratelimit-limit-tokens","x-ratelimit-remaining-tokens","x-ratelimit-reset-requests","x-ratelimit-reset-tokens"];for(let u of f){let h=i.headers.get(u);h&&(d[u]=h);}return {content:p,model:a.model||e.model,usage:{input_tokens:c,output_tokens:g},stop_reason:We(l?.finish_reason),provider:"openai-api",costUsd:Math.round(_*1e6)/1e6,durationMs:o,fallbackChain:["openai-api:success"],headers:Object.keys(d).length>0?d:void 0}}async streamRaw(e,t){let r=this.buildRequestBody(e,true),s=await fetch(this.baseUrl,{method:"POST",headers:this.buildHeaders(),body:JSON.stringify(r),signal:t?.signal});if(s.ok||this.handleErrorResponse(s),!s.body)throw new Error("OPENAI_API_ERROR: Response body is null \u2014 streaming unavailable");let i=s.body.getReader(),a=new TextDecoder,o=e.model;return (async function*(){let l="",p=false,c=false,g,m;yield {type:"message_start",message:{model:o,usage:{input_tokens:0,output_tokens:0}}};try{for(;;){let{done:y,value:_}=await i.read();if(y)break;l+=a.decode(_,{stream:!0});let d=l.split(`
12
+ `);l=d.pop()||"";for(let f of d){if(!f.startsWith("data: "))continue;let u=f.slice(6).trim();if(u==="[DONE]"){yield {type:"message_delta",delta:{stop_reason:m||"end_turn"},usage:g||{}},yield {type:"message_stop"},c=!0;return}try{let h=JSON.parse(u);h.usage&&(g={input_tokens:h.usage.prompt_tokens||0,output_tokens:h.usage.completion_tokens||0});let R=h.choices?.[0];if(!R)continue;let b=R.delta;b?.content&&!p&&(p=!0,yield {type:"content_block_start",index:0,content_block:{type:"text",text:""}}),b?.content&&(yield {type:"content_block_delta",index:0,delta:{type:"text_delta",text:b.content}}),R.finish_reason&&(p&&(yield {type:"content_block_stop",index:0}),m=We(R.finish_reason));}catch{}}}c||(yield {type:"message_delta",delta:{stop_reason:m||"end_turn"},usage:g||{}},yield {type:"message_stop"});}finally{try{i.releaseLock();}catch{}}})()}estimateCost(e){let t=Math.round(JSON.stringify(e.messages).length/4),r=e.system?Math.round(e.system.length/4):0,s=t+r,[i,a]=$e(e.model),o=(s*i+1e3*a)/1e6;return {estimatedInputTokens:s,estimatedCost:Math.round(o*1e4)/1e4,model:e.model,provider:"openai-api"}}getHealth(){return {...this.health}}reportRateLimit(e){this.health.status="degraded",this.health.lastError=`Rate limited: ${e.utilization*100}% utilization`,this.health.lastCheckAt=Date.now();}buildHeaders(){return {"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`}}buildRequestBody(e,t){let r=e.model.toLowerCase();if(!Ye.some(o=>r.startsWith(o)))throw new Error(`OPENAI_MODEL_TRANSLATION_REQUIRED: The OpenAI provider received the model name '${e.model}', which does not match any known OpenAI model prefix (${Ye.join(", ")}). The relay does NOT auto-translate model names across providers in v1.3.x. Either (a) pass an OpenAI model name like 'gpt-4o-mini' when routing to this provider, or (b) route non-OpenAI models through a provider that understands them (e.g. 'anthropic-api' for Claude models).`);let i=Tt(e.messages,e.system),a={model:e.model,max_tokens:e.max_tokens,messages:i};return t&&(a.stream=true,a.stream_options={include_usage:true}),e.temperature!==void 0&&(a.temperature=e.temperature),e.tools&&e.tools.length>0&&(a.tools=e.tools),a}handleErrorResponse(e){let t=e.status;if(t===401)throw this.health.status="unhealthy",this.health.lastError="Authentication failed",this.health.errorCount++,this.health.lastCheckAt=Date.now(),new Error("OPENAI_AUTH_ERROR: Invalid API key (401)");if(t===429){let r=e.headers.get("retry-after"),s=r?parseInt(r,10)*1e3:6e4;this.health.status="degraded",this.health.lastError=`Rate limited \u2014 retry after ${s}ms`,this.health.lastCheckAt=Date.now();let i=new Error(`OPENAI_RATE_LIMIT: Too many requests (429). Retry after ${s}ms`);throw i.retryAfterMs=s,i}throw this.health.errorCount++,this.health.lastCheckAt=Date.now(),new Error(`OPENAI_API_ERROR: Request failed with status ${t}`)}recordSuccess(){this.health.status="healthy",this.health.lastCheckAt=Date.now(),this.health.errorCount=0,this.health.lastError=void 0;}};var xt="http://localhost:11434";function Lt(n){return typeof n=="string"?n:n.filter(e=>e.type==="text"&&e.text).map(e=>e.text).join("")}function Ot(n,e){let t=[];e&&t.push({role:"system",content:e});for(let r of n)t.push({role:r.role,content:Lt(r.content)});return t}var j=class extends S{type="ollama";supportsStreaming=true;supportsMultimodal=false;platformRequirement="desktop";baseUrl;health={status:"healthy",lastCheckAt:Date.now(),errorCount:0};constructor(e){super(),this.baseUrl=e?.baseUrl?.replace(/\/$/,"")||xt;}async probe(){try{return (await fetch(`${this.baseUrl}/api/tags`,{method:"GET"})).ok?{available:!0,authenticated:!0}:{available:!1,authenticated:!1}}catch{return {available:false,authenticated:false}}}async execute(e,t){let r=Date.now(),s=this.buildRequestBody(e,false),i=await fetch(`${this.baseUrl}/api/chat`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(s),signal:t?.signal});if(!i.ok)throw this.handleErrorResponse(i),new Error("unreachable");let a=await i.json(),o=Date.now()-r;this.recordSuccess();let l=[];a.message?.content&&l.push({type:"text",text:a.message.content});let p=a.prompt_eval_count||0,c=a.eval_count||0;return {content:l,model:a.model||e.model,usage:{input_tokens:p,output_tokens:c},stop_reason:(a.done,"end_turn"),provider:"ollama",costUsd:0,durationMs:o,fallbackChain:["ollama:success"]}}async streamRaw(e,t){let r=this.buildRequestBody(e,true),s=await fetch(`${this.baseUrl}/api/chat`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r),signal:t?.signal});if(s.ok||this.handleErrorResponse(s),!s.body)throw new Error("OLLAMA_ERROR: Response body is null \u2014 streaming unavailable");let i=s.body.getReader(),a=new TextDecoder,o=e.model;return (async function*(){let l="",p=false,c=false;yield {type:"message_start",message:{model:o,usage:{input_tokens:0,output_tokens:0}}};try{for(;;){let{done:g,value:m}=await i.read();if(g)break;l+=a.decode(m,{stream:!0});let y=l.split(`
13
+ `);l=y.pop()||"";for(let _ of y){let d=_.trim();if(d)try{let f=JSON.parse(d);if(f.message?.content&&(p||(p=!0,yield {type:"content_block_start",index:0,content_block:{type:"text",text:""}}),yield {type:"content_block_delta",index:0,delta:{type:"text_delta",text:f.message.content}}),f.done){p&&(yield {type:"content_block_stop",index:0}),yield {type:"message_delta",delta:{stop_reason:"end_turn"},usage:{input_tokens:f.prompt_eval_count||0,output_tokens:f.eval_count||0}},yield {type:"message_stop"},c=!0;return}}catch{}}}c||(yield {type:"message_stop"});}finally{try{i.releaseLock();}catch{}}})()}estimateCost(e){let t=Math.round(JSON.stringify(e.messages).length/4),r=e.system?Math.round(e.system.length/4):0;return {estimatedInputTokens:t+r,estimatedCost:0,model:e.model,provider:"ollama"}}getHealth(){return {...this.health}}reportRateLimit(e){this.health.status="degraded",this.health.lastError=`Rate limited: ${e.utilization*100}% utilization`,this.health.lastCheckAt=Date.now();}buildRequestBody(e,t){if(e.tools&&e.tools.length>0)throw new Error(`OLLAMA_TOOLS_NOT_SUPPORTED: The Ollama provider received ${e.tools.length} tool definition(s) in params.tools, but this provider does not implement function calling in v1.3.x. For tool-use workflows route to 'anthropic-api' or 'openai-api' instead. Tracking issue: 1.4.0 ships an Ollama tool-use shim for models that support native function calling (llama3.1, mistral-nemo, etc.).`);let r=Ot(e.messages,e.system),s={model:e.model,messages:r,stream:t};return e.temperature!==void 0&&(s.options={temperature:e.temperature}),s}handleErrorResponse(e){let t=e.status;throw this.health.errorCount++,this.health.lastCheckAt=Date.now(),t===404?(this.health.status="unhealthy",this.health.lastError="Model not found",new Error("OLLAMA_MODEL_NOT_FOUND: Requested model is not available locally (404)")):(this.health.status="unhealthy",this.health.lastError=`Request failed with status ${t}`,new Error(`OLLAMA_ERROR: Request failed with status ${t}`))}recordSuccess(){this.health.status="healthy",this.health.lastCheckAt=Date.now(),this.health.errorCount=0,this.health.lastError=void 0;}};var J=class extends events.EventEmitter{constructor(){super(),this.setMaxListeners(50);}emitTyped(e){return this.emit(e.kind,e)}onTyped(e,t){return this.on(e,t)}offTyped(e,t){return this.off(e,t)}};var et=200;function le(n,e){if(e instanceof E)return `${n}:failed:${e.code}:${e.message}`;let t=e?.message;return `${n}:failed:${t?.slice(0,et)||"unknown"}`}function qe(n){let e=n.filter(t=>t.includes(":failed:"));return e.length===0?false:e.every(t=>t.endsWith(":failed:request_too_large"))}var je=new Set(["anthropic-api","openai-api","ollama"]),U=class extends Error{code="ALL_PROVIDERS_FAILED";fallbackChain;constructor(e){super(`All providers failed. Chain: ${e.join(" \u2192 ")}`),this.name="RelayError",this.fallbackChain=e;}},z=class extends U{constructor(e){super(e);}};function Je(n){return n.messages.some(e=>typeof e.content=="string"?false:Array.isArray(e.content)&&e.content.some(t=>t.type==="image"||t.type==="image_url"))}function ce(n){if(!(n instanceof Error))return "unknown";let e=n.code;if(typeof e=="string"){if(e==="LICENSE_REJECTED")return "auth_failed";if(e==="GATEWAY_FETCH_FAILED"||e==="GATEWAY_VERIFICATION_FAILED"||e==="GATEWAY_LOAD_FAILED")return "gateway_unavailable"}let t=n.message.toLowerCase(),r=n.name.toLowerCase();return t.includes("prompt is too long")||t.includes("prompt too long")||t.includes("prompt_too_large")||t.includes("input length")||t.includes("input is too long")||t.includes("context_length_exceeded")||t.includes("context length exceeded")||t.includes("request too large")||t.includes("tokens exceed")||t.includes("exceeds the maximum")||t.includes("too many tokens")?"request_too_large":t.includes("code 3221226505")||t.includes("cli exited with code 3 ")||t.includes("cli stream exited with code 3 ")||t.includes("bun has crashed")||t.includes("illegal instruction")||t.includes("stream ended without producing any content")||t.includes("cli returned empty response")||t.includes("cli closed stdout without sending any response")||t.includes("kimi_stall_silent")?"transient":t.includes("credit balance is too low")||t.includes("credit balance too low")?"auth_failed":t.startsWith("openai_model_translation_required:")||t.startsWith("ollama_tools_not_supported:")||t.startsWith("multimodal_cli_too_old:")||t.startsWith("stream_json_cli_too_old:")?"provider_incompatible":t.includes("rate limit")||t.includes("429")||r.includes("ratelimit")?"rate_limited":t.includes("auth")||t.includes("401")||t.includes("403")||t.includes("unauthorized")||t.includes("forbidden")?"auth_failed":t.includes("timeout")||t.includes("timed out")||t.includes("aborted")||r==="aborterror"||r==="timeouterror"?"timeout":"unknown"}function ze(n){return [...n].sort((e,t)=>e.priority-t.priority)}var X=class n{healthMap=new Map;engineEvents=new J;static UNHEALTHY_TTL_DEFAULT_MS=300*1e3;static UNHEALTHY_TTL_AUTH_MS=30*1e3;getHealthStatus(e){let t=this.healthMap.get(e);if(!t)return "healthy";let r=t.ttlMs??n.UNHEALTHY_TTL_DEFAULT_MS;if(t.status==="unhealthy"&&Date.now()-t.updatedAt>=r){let s=Date.now()-t.updatedAt;return this.healthMap.delete(e),C("engine.recovered",{providerType:e,ageMs:s,ttlMs:r}),"healthy"}return t.status}markUnhealthy(e,t,r){this.healthMap.set(e,{status:"unhealthy",lastError:t,updatedAt:Date.now(),ttlMs:r}),C("engine.unhealthy",{providerType:e,reason:t,ttlMs:r});}markDegraded(e,t){this.healthMap.set(e,{status:"degraded",lastError:t,updatedAt:Date.now()});}markHealthy(e){this.healthMap.set(e,{status:"healthy",updatedAt:Date.now()});}resetHealth(){this.healthMap.clear();}async execute(e,t,r){let s=ze(t),i=[],a=Je(e),o=crypto$1.randomUUID(),l=Date.now();for(let p of s){let c=p.type;if(this.getHealthStatus(c)==="unhealthy"){i.push(`${c}:skipped:unhealthy`);continue}if(a&&!p.supportsMultimodal){i.push(`${c}:skipped:no_multimodal`);continue}let m=oe(e.model);if(m&&m!==c&&je.has(c)){i.push(`${c}:skipped:wrong_provider`);continue}let y=r?.maxSameProviderRetries??2,_=0,d=null,f=false;for(;!f;)try{let u=await p.execute(e,{signal:r?.signal});return f=!0,this.markHealthy(c),_>0&&i.push(`${c}:retry_success:${_}`),i.push(`${c}:success`),this.emitEngineEvent({kind:"stream_complete",callId:o,id:crypto$1.randomUUID(),ts:Date.now(),stitched:!1,segments:[{provider:c,inputTokens:u.usage.input_tokens,outputTokens:u.usage.output_tokens,costUsd:u.costUsd,userCostUsd:u.userCostUsd??0}],provider:c,model:u.model,inputTokens:u.usage.input_tokens,outputTokens:u.usage.output_tokens,costUsd:u.costUsd,userCostUsd:u.userCostUsd,durationMs:u.durationMs??Date.now()-l,stopReason:u.stop_reason,fallbackChain:i,rateLimitHit:!1,securityState:r?.licenseState}),{...u,fallbackChain:i}}catch(u){if(u?.name==="AbortError"||r?.signal?.aborted)throw u;if(ce(u)==="transient"&&_<y){_++;let R=250*Math.pow(2,_-1);C("engine.transient-retry",{id:c,attempt:_,backoffMs:R,msg:String(u?.message||"").slice(0,120)}),i.push(`${c}:retry:${_}`),await new Promise(b=>setTimeout(b,R));continue}d=u;break}if(!f){let u=d;switch(ce(u)){case "rate_limited":p.reportRateLimit({provider:c,utilization:1,resetsAt:Date.now()+6e4}),this.markDegraded(c,"rate_limited"),i.push(`${c}:failed:rate_limited`);break;case "auth_failed":this.markUnhealthy(c,"auth_failed",n.UNHEALTHY_TTL_AUTH_MS),i.push(`${c}:failed:auth_failed`);break;case "timeout":i.push(`${c}:failed:timeout`);break;case "provider_incompatible":i.push(`${c}:incompatible`);break;case "gateway_unavailable":this.markUnhealthy(c,"gateway_unavailable"),i.push(le(c,u));break;case "request_too_large":i.push(`${c}:failed:request_too_large`);break;case "transient":this.markDegraded(c,"transient_exhausted"),i.push(`${c}:failed:transient_exhausted`);break;default:i.push(le(c,u));break}if(r?.subscriptionOnly&&c==="claude-cli"){i.push("subscription_only:cascade_halted");break}}}throw C("engine.cascade-exhausted",{chain:i,mode:"execute"}),qe(i)?new z(i):new U(i)}async stream(e,t,r){let s=crypto$1.randomUUID(),i=ze(t),a=Je(e),o=Date.now(),l=[],p=null,c=null;for(let u of i){let h=u.type;if(this.getHealthStatus(h)==="unhealthy"){l.push(`${h}:skipped:unhealthy`);continue}if(a&&!u.supportsMultimodal){l.push(`${h}:skipped:no_multimodal`);continue}if(!u.supportsStreaming){l.push(`${h}:skipped:no_streaming`);continue}let R=oe(e.model);if(R&&R!==h&&je.has(h)){l.push(`${h}:skipped:wrong_provider`);continue}try{c=await u.streamRaw(e,{signal:r?.signal}),p=u,l.push(`${h}:success`);break}catch(b){if(b?.name==="AbortError"||r?.signal?.aborted)throw b;switch(ce(b)){case "rate_limited":u.reportRateLimit({provider:h,utilization:1,resetsAt:Date.now()+6e4}),this.markDegraded(h,"rate_limited"),l.push(`${h}:failed:rate_limited`);break;case "auth_failed":this.markUnhealthy(h,"auth_failed",n.UNHEALTHY_TTL_AUTH_MS),l.push(`${h}:failed:auth_failed`);break;case "timeout":l.push(`${h}:failed:timeout`);break;case "provider_incompatible":l.push(`${h}:incompatible`);break;case "gateway_unavailable":this.markUnhealthy(h,"gateway_unavailable"),l.push(le(h,b));break;case "request_too_large":l.push(`${h}:failed:request_too_large`);break;case "transient":this.markDegraded(h,"transient_exhausted"),l.push(`${h}:failed:transient_exhausted`);break;default:l.push(le(h,b));break}if(r?.subscriptionOnly&&h==="claude-cli"){l.push("subscription_only:cascade_halted");break}}}if(!p||!c)throw C("engine.cascade-exhausted",{chain:l,mode:"stream"}),qe(l)?new z(l):new U(l);let m=this,y=r?.maxMidStreamFailovers??2,_=new Set([p]);async function*d(){let u=p,h=c[Symbol.asyncIterator](),R={role:"assistant",content:[]},b={segments:[]},T=Xe(u.type);b.segments.push(T);let Le=0,ee=null,te=false,be=y,H="",B="";for(m.emitEngineEvent({kind:"segment_start",callId:s,id:crypto$1.randomUUID(),ts:Date.now(),provider:u.type});;)try{for(;;){let A=await h.next();if(A.done)break;let L=A.value,k=Ut(L,Le,R,te,ee);if(k.action==="suppress"){"coalesceMode"in k&&(ee=k.coalesceMode),k.clearedJustFailedOver&&(te=!1);continue}k.coalesceModeCleared&&(ee=null),k.clearedJustFailedOver&&(te=!1);let v=k.event;if(Nt(R,v),v.type==="message_start"){if(b.segments.length>1)continue;H=v.message?.model||H,v.message?.usage?.input_tokens!=null&&(T.inputTokens=v.message.usage.input_tokens);}if(v.type==="message_delta"&&(v.delta?.stop_reason&&(B=v.delta.stop_reason),v.usage)){Dt(T,u.type,v.usage),yield {...v,usage:void 0};continue}if(v.type==="message_stop")break;yield v;}if(R.content.length===0&&T.outputTokens===0)throw new Error("stream ended without producing any content");m.markHealthy(u.type),m.emitEngineEvent({kind:"segment_complete",callId:s,id:crypto$1.randomUUID(),ts:Date.now(),provider:u.type,usage:{...T}});let w=Qe(b.segments);yield {type:"message_delta",delta:{stop_reason:B||"end_turn"},usage:w},yield {type:"message_stop"},m.emitEngineEvent({kind:"stream_complete",callId:s,id:crypto$1.randomUUID(),ts:Date.now(),stitched:b.segments.length>1,segments:b.segments.map(A=>({...A})),provider:b.segments.map(A=>A.provider).join("+"),model:H,inputTokens:w.input_tokens,outputTokens:w.output_tokens,costUsd:b.segments.reduce((A,L)=>A+L.costUsd,0),userCostUsd:b.segments.reduce((A,L)=>A+L.userCostUsd,0),durationMs:Date.now()-o,stopReason:B||"end_turn",fallbackChain:l,rateLimitHit:!1,securityState:r?.licenseState});return}catch(w){if(w?.name==="AbortError"||r?.signal?.aborted)throw w;let A=ce(w);try{await h.return?.();}catch{}if(m.markUnhealthy(u.type,String(w)),m.emitEngineEvent({kind:"mid_stream_failover_attempt",callId:s,id:crypto$1.randomUUID(),ts:Date.now(),from:u.type,bytesBuffered:Ze(R),errorClass:A,retriesLeft:be}),l.push(`${u.type}:partial(${Ze(R)}b)`),be<=0)throw m.emitTerminalExhaustion(s,b,H,B,l,o,r?.licenseState),w;let L=Ft(R.content),k=[...e.messages,{role:"assistant",content:L}],v=null,re=null,Oe=u;for(;;){let I=m.nextHealthyProvider(i,Oe,a,_);if(!I)break;Oe=I,_.add(I);let st={...e,messages:k,...I.type!=="anthropic-api"&&I.type!=="claude-cli"?{system:Ht(e.system,"[SYSTEM]: CONTINUE_LAST_RESPONSE_WITHOUT_REPEATING")}:{}};try{re=await I.streamRaw(st,{signal:r?.signal}),v=I;break}catch(Me){m.markUnhealthy(I.type,String(Me)),l.push(`${I.type}:failed_connect:${String(Me).slice(0,et)}`);}}if(!v||!re)throw m.emitTerminalExhaustion(s,b,H,B,l,o,r?.licenseState),w;l.push(`${v.type}:stitched`),m.emitEngineEvent({kind:"mid_stream_failover_success",callId:s,id:crypto$1.randomUUID(),ts:Date.now(),from:u.type,to:v.type}),Le=Bt(R)+1,be--,u=v,h=re[Symbol.asyncIterator](),te=true,ee=null,T=Xe(u.type),b.segments.push(T),m.emitEngineEvent({kind:"segment_start",callId:s,id:crypto$1.randomUUID(),ts:Date.now(),provider:u.type});}}let f=d();return r?.licenseState==="SEC_DEGRADED"&&(f=Kt(f)),M(f)}nextHealthyProvider(e,t,r,s){let i=e.indexOf(t);if(i===-1)return null;for(let a=i+1;a<e.length;a++){let o=e[a];if(!s.has(o)&&!(r&&!o.supportsMultimodal)&&o.supportsStreaming)return o}return null}emitEngineEvent(e){this.engineEvents.emitTyped(e);}emitTerminalExhaustion(e,t,r,s,i,a,o){let l=Qe(t.segments);this.emitEngineEvent({kind:"stream_complete",callId:e,id:crypto$1.randomUUID(),ts:Date.now(),stitched:t.segments.length>1,segments:t.segments.map(p=>({...p})),provider:t.segments.map(p=>p.provider).join("+"),model:r,inputTokens:l.input_tokens,outputTokens:l.output_tokens,costUsd:t.segments.reduce((p,c)=>p+c.costUsd,0),userCostUsd:t.segments.reduce((p,c)=>p+c.userCostUsd,0),durationMs:Date.now()-a,stopReason:s||"error",fallbackChain:i,rateLimitHit:false,securityState:o});}};function Xe(n){return {provider:n,inputTokens:0,outputTokens:0,costUsd:0,userCostUsd:0}}function Dt(n,e,t){typeof t.input_tokens=="number"&&(n.inputTokens=t.input_tokens),typeof t.output_tokens=="number"&&(e==="anthropic-api"||e==="claude-cli"?n.outputTokens+=t.output_tokens:n.outputTokens=t.output_tokens);}function Qe(n){return {input_tokens:n[n.length-1]?.inputTokens??0,output_tokens:n.reduce((e,t)=>e+t.outputTokens,0)}}function Nt(n,e){if(e.type==="content_block_start"){let t=e.content_block;t?.type==="text"?n.content.push({type:"text",text:t.text||"",open:true}):t?.type==="tool_use"&&n.content.push({type:"tool_use",id:t.id,name:t.name,input:{},_partialJson:"",open:true});}else if(e.type==="content_block_delta"){let t=n.content[n.content.length-1];if(!t)return;t.type==="text"&&e.delta?.type==="text_delta"?t.text+=e.delta.text||"":t.type==="tool_use"&&e.delta?.type==="input_json_delta"&&(t._partialJson=(t._partialJson||"")+(e.delta.partial_json||""));}else if(e.type==="content_block_stop"){for(let t=n.content.length-1;t>=0;t--)if(n.content[t].open){if(n.content[t].open=false,n.content[t].type==="tool_use"){let r=n.content[t];try{r.input=JSON.parse(r._partialJson||"{}");}catch{}delete r._partialJson;}break}}}function Ut(n,e,t,r,s){if(s&&n.type==="content_block_delta"&&n.index===s.fromIndex)return {action:"yield",event:{...n,index:s.toIndex}};if(s&&n.type==="content_block_stop"&&n.index===s.fromIndex)return {action:"suppress",coalesceMode:null,coalesceModeCleared:true};if(r&&n.type==="content_block_start"&&n.content_block?.type==="text"){let a=t.content[t.content.length-1];if(a?.type==="text"&&a.open===true)return {action:"suppress",coalesceMode:{fromIndex:n.index+e,toIndex:t.content.length-1},clearedJustFailedOver:true}}let i=r&&n.type==="content_block_start";return typeof n.index=="number"&&e>0?{action:"yield",event:{...n,index:n.index+e},clearedJustFailedOver:i}:{action:"yield",event:n,clearedJustFailedOver:i}}function Ft(n){let e=[];for(let t of n)t.type==="text"?t.text&&e.push({type:"text",text:t.text}):t.type==="tool_use"&&!t.open&&t.input&&Object.keys(t.input).length>0&&e.push({type:"tool_use",id:t.id,name:t.name,input:t.input});return e}function Ht(n,e){return n?`${n}
14
14
 
15
- ${e}`:e}function ze(n){let e=0;for(let t of n.content)t.type==="text"?e+=t.text.length:t.type==="tool_use"&&(e+=(t._partialJson||"").length);return e}function Ut(n){return Math.max(0,n.content.length-1)}async function*Ft(n){for await(let e of n)await new Promise(t=>setTimeout(t,50+Math.random()*50)),yield e;}ne();function ce(n,e){if(e==="prefer-user"){let s=n.filter(a=>a.type==="claude-cli"),i=n.filter(a=>a.type!=="claude-cli");return [...s,...i]}if(e==="prefer-app"){let s=n.filter(a=>a.type==="claude-cli");return [...n.filter(a=>a.type!=="claude-cli"),...s]}let t=n.filter(s=>s.type===e),r=n.filter(s=>s.type!==e);return [...t,...r]}function ue(n){return (e,t)=>n.maxCostPerCall!==void 0&&t.estimatedCost>n.maxCostPerCall?"prefer-user":n.preferLocal?"ollama":"prefer-user"}var de={"claude-fable-5":[10,50],"claude-mythos-5":[10,50],"claude-opus-5-5":[4,20],"claude-opus-5":[5,25],"claude-opus-4-8":[5,25],"claude-opus-4-7":[5,25],"claude-opus-4-6":[5,25],"claude-opus-4-5":[5,25],"claude-opus-4":[15,75],"claude-sonnet-5":[2,10],"claude-sonnet-4":[3,15],"claude-haiku-5-5":[.1,.5],"claude-haiku-4-5":[1,5],"claude-3-5-haiku":[.8,4],"claude-3-haiku":[.25,1.25],"kimi-k3":[3,15],"kimi-k2.7-code":[.95,4],"kimi-for-coding":[.95,4],"gpt-4o":[2.5,10],"gpt-4o-mini":[.15,.6],"gpt-4-turbo":[10,30],"gpt-3.5-turbo":[.5,1.5],llama:[0,0],mistral:[0,0],codellama:[0,0]};function pe(n){if(de[n])return de[n];let e=null;for(let t of Object.keys(de))n.startsWith(t)&&(e===null||t.length>e.length)&&(e=t);return e!==null?de[e]:[3,15]}function me(n){let e=0;for(let o of n.messages)if(typeof o.content=="string")e+=o.content.length;else if(Array.isArray(o.content))for(let l of o.content)l.type==="text"&&l.text&&(e+=l.text.length);n.system&&(e+=n.system.length);let t=Math.ceil(e/4),r=Math.min(n.max_tokens||1024,4096),[s,i]=pe(n.model),a=(t*s+r*i)/1e6;return {estimatedInputTokens:t,estimatedCost:Math.round(a*1e4)/1e4,model:n.model,provider:""}}var F=class{endpoint;enabled;keyId;lineageBuffer=new Map;unsubscribers=[];constructor(e){this.endpoint=e.endpoint||"https://api.inference-relay.com/v1/events",this.enabled=e.enabled??true,this.keyId=e.keyId||"";}subscribeToEngine(e){let t=s=>this.bufferLineageEvent(s),r=s=>{s.kind==="stream_complete"&&this.flushLineageForCall(s);};e.engineEvents.onTyped("segment_start",t),e.engineEvents.onTyped("segment_complete",t),e.engineEvents.onTyped("mid_stream_failover_attempt",t),e.engineEvents.onTyped("mid_stream_failover_success",t),e.engineEvents.onTyped("stream_complete",r),this.unsubscribers.push(()=>{e.engineEvents.offTyped("segment_start",t),e.engineEvents.offTyped("segment_complete",t),e.engineEvents.offTyped("mid_stream_failover_attempt",t),e.engineEvents.offTyped("mid_stream_failover_success",t),e.engineEvents.offTyped("stream_complete",r);});}unsubscribe(){for(let e of this.unsubscribers)e();this.unsubscribers=[],this.lineageBuffer.clear();}bufferLineageEvent(e){let t=this.lineageBuffer.get(e.callId)??[];t.push(e),this.lineageBuffer.set(e.callId,t);}flushLineageForCall(e){let t=this.lineageBuffer.get(e.callId)??[];this.lineageBuffer.delete(e.callId);let r={timestamp:e.ts,provider:e.provider,model:e.model,inputTokens:e.inputTokens,outputTokens:e.outputTokens,costUsd:e.costUsd,userCostUsd:e.userCostUsd,durationMs:e.durationMs,stopReason:e.stopReason,fallbackChain:e.fallbackChain,rateLimitHit:e.rateLimitHit,securityState:e.securityState,stitched:e.stitched,segments:e.segments,lineage:t.map(s=>s.id),promptContent:false,completionContent:false};this.report(r);}report(e){!this.enabled||!this.keyId||fetch(this.endpoint,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({keyId:this.keyId,provider:e.provider,model:e.model,inputTokens:e.inputTokens,outputTokens:e.outputTokens,costUsd:e.costUsd,...e.userCostUsd!==void 0?{userCostUsd:e.userCostUsd}:{},durationMs:e.durationMs,fallback:e.fallbackChain.length>1,timestamp:e.timestamp,...e.stitched?{stitched:true,segmentCount:e.segments?.length??0}:{},...e.lineage?{lineageIds:e.lineage}:{}})}).catch(()=>{});}};var Ht="https://api.inference-relay.com",Bt="/v1/validate",Kt=1440*60*1e3,Gt=10080*60*1e3,Vt=`-----BEGIN PUBLIC KEY-----
15
+ ${e}`:e}function Ze(n){let e=0;for(let t of n.content)t.type==="text"?e+=t.text.length:t.type==="tool_use"&&(e+=(t._partialJson||"").length);return e}function Bt(n){return Math.max(0,n.content.length-1)}async function*Kt(n){for await(let e of n)await new Promise(t=>setTimeout(t,50+Math.random()*50)),yield e;}se();function ue(n,e){if(e==="prefer-user"){let s=n.filter(a=>a.type==="claude-cli"),i=n.filter(a=>a.type!=="claude-cli");return [...s,...i]}if(e==="prefer-app"){let s=n.filter(a=>a.type==="claude-cli");return [...n.filter(a=>a.type!=="claude-cli"),...s]}let t=n.filter(s=>s.type===e),r=n.filter(s=>s.type!==e);return [...t,...r]}function de(n){return (e,t)=>n.maxCostPerCall!==void 0&&t.estimatedCost>n.maxCostPerCall?"prefer-user":n.preferLocal?"ollama":"prefer-user"}var pe={"claude-fable-5":[10,50],"claude-mythos-5":[10,50],"claude-opus-5-5":[4,20],"claude-opus-5":[5,25],"claude-opus-4-8":[5,25],"claude-opus-4-7":[5,25],"claude-opus-4-6":[5,25],"claude-opus-4-5":[5,25],"claude-opus-4":[15,75],"claude-sonnet-5":[2,10],"claude-sonnet-4":[3,15],"claude-haiku-5-5":[.1,.5],"claude-haiku-4-5":[1,5],"claude-3-5-haiku":[.8,4],"claude-3-haiku":[.25,1.25],"kimi-k3":[3,15],"kimi-k2.7-code":[.95,4],"kimi-for-coding":[.95,4],"gpt-4o":[2.5,10],"gpt-4o-mini":[.15,.6],"gpt-4-turbo":[10,30],"gpt-3.5-turbo":[.5,1.5],llama:[0,0],mistral:[0,0],codellama:[0,0]};function me(n){if(pe[n])return pe[n];let e=null;for(let t of Object.keys(pe))n.startsWith(t)&&(e===null||t.length>e.length)&&(e=t);return e!==null?pe[e]:[3,15]}function he(n){let e=0;for(let o of n.messages)if(typeof o.content=="string")e+=o.content.length;else if(Array.isArray(o.content))for(let l of o.content)l.type==="text"&&l.text&&(e+=l.text.length);n.system&&(e+=n.system.length);let t=Math.ceil(e/4),r=Math.min(n.max_tokens||1024,4096),[s,i]=me(n.model),a=(t*s+r*i)/1e6;return {estimatedInputTokens:t,estimatedCost:Math.round(a*1e4)/1e4,model:n.model,provider:""}}var F=class{endpoint;enabled;keyId;lineageBuffer=new Map;unsubscribers=[];constructor(e){this.endpoint=e.endpoint||"https://api.inference-relay.com/v1/events",this.enabled=e.enabled??true,this.keyId=e.keyId||"";}subscribeToEngine(e){let t=s=>this.bufferLineageEvent(s),r=s=>{s.kind==="stream_complete"&&this.flushLineageForCall(s);};e.engineEvents.onTyped("segment_start",t),e.engineEvents.onTyped("segment_complete",t),e.engineEvents.onTyped("mid_stream_failover_attempt",t),e.engineEvents.onTyped("mid_stream_failover_success",t),e.engineEvents.onTyped("stream_complete",r),this.unsubscribers.push(()=>{e.engineEvents.offTyped("segment_start",t),e.engineEvents.offTyped("segment_complete",t),e.engineEvents.offTyped("mid_stream_failover_attempt",t),e.engineEvents.offTyped("mid_stream_failover_success",t),e.engineEvents.offTyped("stream_complete",r);});}unsubscribe(){for(let e of this.unsubscribers)e();this.unsubscribers=[],this.lineageBuffer.clear();}bufferLineageEvent(e){let t=this.lineageBuffer.get(e.callId)??[];t.push(e),this.lineageBuffer.set(e.callId,t);}flushLineageForCall(e){let t=this.lineageBuffer.get(e.callId)??[];this.lineageBuffer.delete(e.callId);let r={timestamp:e.ts,provider:e.provider,model:e.model,inputTokens:e.inputTokens,outputTokens:e.outputTokens,costUsd:e.costUsd,userCostUsd:e.userCostUsd,durationMs:e.durationMs,stopReason:e.stopReason,fallbackChain:e.fallbackChain,rateLimitHit:e.rateLimitHit,securityState:e.securityState,stitched:e.stitched,segments:e.segments,lineage:t.map(s=>s.id),promptContent:false,completionContent:false};this.report(r);}report(e){!this.enabled||!this.keyId||fetch(this.endpoint,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({keyId:this.keyId,provider:e.provider,model:e.model,inputTokens:e.inputTokens,outputTokens:e.outputTokens,costUsd:e.costUsd,...e.userCostUsd!==void 0?{userCostUsd:e.userCostUsd}:{},durationMs:e.durationMs,fallback:e.fallbackChain.length>1,timestamp:e.timestamp,...e.stitched?{stitched:true,segmentCount:e.segments?.length??0}:{},...e.lineage?{lineageIds:e.lineage}:{}})}).catch(()=>{});}};var Q="1.6.11",ye=Object.freeze({streamKeepsHistory:true,streamToolBlocks:true,streamBareToolNames:true});function ke(){return {name:"inference-relay",version:Q,features:ye}}var Gt="https://api.inference-relay.com",Vt="/v1/validate",Yt=1440*60*1e3,$t=10080*60*1e3,Wt=`-----BEGIN PUBLIC KEY-----
16
16
  MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzk88Bt9LZSmuSd8ulKqy
17
17
  EGNiPB+r8u1ZinBvMM4qX8dp4kzJEC/47L1RYrrxCphD05HToREjljbj+upH6rZH
18
18
  t6D/5funYPi2TO1IOSC6BRVRwMqqTmJGVJcLcG/WF3gTE+2UQ3q5EppWJCEKcz6x
@@ -20,5 +20,5 @@ kfynZEzOcLikoQALIxEjxRfi6g83wNJS+WBwAJQ68s7N9FHE3rkLzEqv3FJRP+ZR
20
20
  7lqwqIyuCLr9j9yc4XqF6VY9yooD2hyw/n1EVFejzjDgntWTqIkaCIXwh8lDzRrK
21
21
  Rxn07gcI6ea2Eym9sXh4xZLFudXfAT2DFsH/XboDUuMKAV1YHc1NsIGApesWe2M+
22
22
  UQIDAQAB
23
- -----END PUBLIC KEY-----`,O=new Map,we=new Set;async function Pe(n,e){if(!n)return {valid:false,error:"License key required. Get one at inference-relay.com"};let t=O.get(n);if(t&&Date.now()-t.cachedAt<t.ttl)return t.result;let s=!O.has(n)?1e4:3e3,i=(e||Ht)+Bt,a=async()=>{let p=new AbortController,c=setTimeout(()=>p.abort(),s);try{return await(await fetch(i,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({key:n}),signal:p.signal})).json()}finally{clearTimeout(c);}},o;try{o=await a();}catch{try{if(t)throw new Error("cached answer stands in");o=await a();}catch{return t&&Date.now()-t.cachedAt<Gt?t.result:{valid:false,error:"License validation failed (network error). Visit inference-relay.com"}}}let l;if(o.jws)try{let{compactVerify:p,importSPKI:c}=await import('jose'),g=await c(Vt,"RS256"),{payload:m}=await p(o.jws,g);l=JSON.parse(new TextDecoder().decode(m));}catch{return O.delete(n),{valid:false,error:"License validation signature invalid. Possible MITM \u2014 refusing to start."}}else if(l=o,l.valid)return O.delete(n),{valid:false,error:"License validation response not signed. Possible MITM."};return O.set(n,{result:l,cachedAt:Date.now(),ttl:l.valid?Kt:6e4}),l}var ke=class n{providers=[];config;fallbackEngine;telemetryReporter;routingFn=null;license=null;licensePromise=null;licenseValidatedAt=0;callsSinceValidation=0;manifestDecryptor=new D;capProbeTimer=null;capProbeTicksRemaining=0;messages;constructor(e){if(this.config=e,this.fallbackEngine=new X,this.telemetryReporter=new F({endpoint:e.endpoint?`${e.endpoint}/v1/events`:void 0,enabled:e.telemetry!==false,keyId:e.licenseKey}),this.telemetryReporter.subscribeToEngine(this.fallbackEngine),e.routing&&(this.routingFn=ue(e.routing)),e.providers)for(let s of e.providers){let i=typeof s=="string"?{type:s}:s,a=this.createProvider(i);a&&(i.priority!==void 0&&(a.priority=i.priority),this.providers.push(a));}let t=100,r=3600*1e3;this.messages={create:async(s,i)=>{let a=this.license&&(this.callsSinceValidation>=t||Date.now()-this.licenseValidatedAt>=r);if(a&&(this.license=null),!this.license){this.licensePromise||(this.licensePromise=Pe(this.config.licenseKey,this.config.endpoint));try{this.license=await this.licensePromise;}finally{this.licensePromise=null;}if(this.licenseValidatedAt=Date.now(),this.callsSinceValidation=0,this.license.valid){if(this.license.capExceeded)throw this.startCapProbe(),new K({tier:this.license.tier||"solo",baseCap:this.license.baseCap??this.license.usageCap??null,topUps:this.license.topUpGrants??0,effectiveCap:this.license.usageCap??null,usedThisCycle:this.license.usageThisMonth??0});!a&&!we.has(this.config.licenseKey)&&(we.add(this.config.licenseKey),console.log(`[inference-relay] LICENSE AUTHORIZED [${(this.license.tier||"unknown").toUpperCase()} TIER] \u2014 READY.`));}else throw new Error(`[inference-relay] ${this.license.error||"License key invalid or expired. Visit inference-relay.com"}`)}return this.callsSinceValidation++,s.stream?this.streamMessage(s,i):this.executeMessage(s,i)}};}startCapProbe(){if(this.capProbeTimer!==null)return;let e=15e3,t=20,r=4;this.capProbeTicksRemaining=t;let s=async()=>{this.capProbeTicksRemaining-=1;let i=Math.ceil(this.capProbeTicksRemaining*e/6e4);this.capProbeTicksRemaining>0&&this.capProbeTicksRemaining%r===0&&console.log(`[inference-relay] cap exceeded \u2014 checking for top-up every 15s (${i}m remaining)`),this.config.licenseKey&&O.delete(this.config.licenseKey);try{let a=await this.probeValidate();if(a.valid&&a.capExceeded===!1){console.log("[inference-relay] cap cleared \u2014 probe exiting, inference resuming"),this.license=a,this.licenseValidatedAt=Date.now(),this.callsSinceValidation=0,this.stopCapProbe();return}}catch{}this.capProbeTicksRemaining<=0&&(console.log("[inference-relay] cap probe exhausted (5 min) \u2014 stopping"),this.stopCapProbe());};this.capProbeTimer=setInterval(s,e),typeof this.capProbeTimer.unref=="function"&&this.capProbeTimer.unref();}async probeValidate(){return Pe(this.config.licenseKey,this.config.endpoint)}stopCapProbe(){this.capProbeTimer!==null&&(clearInterval(this.capProbeTimer),this.capProbeTimer=null),this.capProbeTicksRemaining=0;}static async autoDetect(e){let t=[],r=await V();r.available&&r.authenticated!==false&&t.push({type:"claude-cli",priority:1});let s=e?.subscriptionOnly===true||process.env.IR_SUBSCRIPTION_ONLY==="1";return process.env.ANTHROPIC_API_KEY&&!s&&t.push({type:"anthropic-api",priority:2,apiKey:process.env.ANTHROPIC_API_KEY}),new n({licenseKey:e?.licenseKey||process.env.IR_LICENSE_KEY||"",providers:t,fallback:e?.fallback??true,subscriptionOnly:s,...e})}async warmup(){let e=this.providers.find(t=>t.type==="claude-cli");e&&"warmup"in e&&typeof e.warmup=="function"&&await e.warmup();}resetHealth(){this.fallbackEngine.resetHealth();}markHealthy(e){this.fallbackEngine.markHealthy(e);}dispose(){this.providers=[],this.config.licenseKey&&(O.delete(this.config.licenseKey),we.delete(this.config.licenseKey)),this.license=null,this.licensePromise=null,this.callsSinceValidation=0,this.stopCapProbe(),this.telemetryReporter.unsubscribe(),this.manifestDecryptor.clearCache(),Le();}resolveProviders(e){if(!this.routingFn)return this.providers;let t=me(e),r=this.routingFn(e,t);return ce(this.providers,r)}async executeMessage(e,t){let r=this.resolveProviders(e);return this.fallbackEngine.execute(e,r,{signal:t?.signal,licenseState:this.manifestDecryptor.getSecurityState(),subscriptionOnly:this.config.subscriptionOnly,maxSameProviderRetries:this.config.maxSameProviderRetries})}async streamMessage(e,t){let r=this.resolveProviders(e);return this.fallbackEngine.stream(e,r,{signal:t?.signal,licenseState:this.manifestDecryptor.getSecurityState(),subscriptionOnly:this.config.subscriptionOnly,maxSameProviderRetries:this.config.maxSameProviderRetries})}createProvider(e){switch(e.type){case "claude-cli":return new se(this.config.licenseKey,{retryDelaysMs:this.config.gatewayRetryDelaysMs});case "kimi-cli":return new ie(this.config.licenseKey,{retryDelaysMs:this.config.gatewayRetryDelaysMs});case "anthropic-api":return e.apiKey?new Y({apiKey:e.apiKey,baseUrl:e.baseUrl}):null;case "openai-api":return e.apiKey?new q({apiKey:e.apiKey,baseUrl:e.baseUrl}):null;case "ollama":return new j({baseUrl:e.baseUrl});default:return null}}};function $t(n){if(!n)return 0;if(typeof n=="string")return Math.ceil(n.length/4);if(!Array.isArray(n))return 0;let e=0;for(let t of n){if(!t||typeof t!="object")continue;let r=t;if(r.type==="image"||r.type==="image_url"){e+=1500;continue}if(typeof r.text=="string"){e+=Math.ceil(r.text.length/4);continue}try{e+=Math.ceil(JSON.stringify(t).length/4);}catch{e+=50;}}return e}function Wt(n){return 8+$t(n.content)}function Yt(n){let e=50;typeof n.system=="string"&&(e+=Math.ceil(n.system.length/4));for(let t of n.messages)e+=Wt(t);return e}ne();var qt="https://api.anthropic.com/v1/messages",jt="2023-06-01";async function Qe(n){try{let e=await fetch(qt,{method:"POST",headers:{"content-type":"application/json","x-api-key":n,"anthropic-version":jt},body:JSON.stringify({model:"claude-haiku-4-5-20250414",max_tokens:1,messages:[{role:"user",content:"hi"}]})});return e.status===401?{valid:!1,error:"Invalid API key (401)"}:e.ok?{valid:!0}:e.status===429?{valid:!0,error:"Rate limited (429)"}:e.status===529?{valid:!0,error:"API overloaded (529)"}:{valid:!1,error:`Unexpected status ${e.status}`}}catch(e){return {valid:false,error:`Network error: ${e.message}`}}}var he=class{filePath;safeStorage=null;memoryFallback=new Map;constructor(e){let t=e?.storagePath||path.join(os.homedir(),".inference-relay");fs.existsSync(t)||fs.mkdirSync(t,{recursive:true}),this.filePath=path.join(t,"credentials.enc");try{let r=nt("electron");r?.safeStorage?.isEncryptionAvailable?.()&&(this.safeStorage=r.safeStorage);}catch{}}readDisk(){if(!fs.existsSync(this.filePath))return {};try{let e=fs.readFileSync(this.filePath,"utf-8");return JSON.parse(e)}catch{return {}}}writeDisk(e){fs.writeFileSync(this.filePath,JSON.stringify(e,null,2),{mode:384});}async get(e){if(!this.safeStorage)return this.memoryFallback.get(e)??null;let r=this.readDisk()[e];if(!r)return null;try{let s=Buffer.from(r.data,"base64");return this.safeStorage.decryptString(s)}catch{return null}}async set(e,t){if(!this.safeStorage){this.memoryFallback.set(e,t);return}let r=this.readDisk(),s=this.safeStorage.encryptString(t);r[e]={data:s.toString("base64")},this.writeDisk(r);}async delete(e){if(!this.safeStorage){this.memoryFallback.delete(e);return}let t=this.readDisk();e in t&&(delete t[e],this.writeDisk(t));}async has(e){if(!this.safeStorage)return this.memoryFallback.has(e);let t=this.readDisk();return e in t}};var Q="AES-GCM";var ye=class{dbName="inference-relay-credentials";storeName="keys";cryptoKey=null;passphrase;initPromise=null;constructor(e){this.passphrase=e?.passphrase,e?.dbName&&(this.dbName=e.dbName);}async ensureReady(){if(!this.cryptoKey)return this.initPromise?this.initPromise:(this.initPromise=this.init(),this.initPromise)}async init(){if(await this.openDB(),this.passphrase)this.cryptoKey=await this.deriveKey(this.passphrase,this.getOrCreateSessionSalt());else {let e=typeof sessionStorage<"u"?sessionStorage.getItem("ir-cred-key"):null;if(e){let t=Uint8Array.from(atob(e),r=>r.charCodeAt(0));this.cryptoKey=await crypto.subtle.importKey("raw",t,Q,false,["encrypt","decrypt"]);}else {this.cryptoKey=await crypto.subtle.generateKey({name:Q,length:256},true,["encrypt","decrypt"]);let t=await crypto.subtle.exportKey("raw",this.cryptoKey),r=btoa(String.fromCharCode(...new Uint8Array(t)));typeof sessionStorage<"u"&&sessionStorage.setItem("ir-cred-key",r);}}}openDB(){return new Promise((e,t)=>{let r=indexedDB.open(this.dbName,1);r.onupgradeneeded=()=>{let s=r.result;s.objectStoreNames.contains(this.storeName)||s.createObjectStore(this.storeName);},r.onsuccess=()=>e(r.result),r.onerror=()=>t(r.error);})}async idbGet(e){let t=await this.openDB();return new Promise((r,s)=>{let o=t.transaction(this.storeName,"readonly").objectStore(this.storeName).get(e);o.onsuccess=()=>r(o.result),o.onerror=()=>s(o.error);})}async idbPut(e,t){let r=await this.openDB();return new Promise((s,i)=>{let l=r.transaction(this.storeName,"readwrite").objectStore(this.storeName).put(t,e);l.onsuccess=()=>s(),l.onerror=()=>i(l.error);})}async idbDelete(e){let t=await this.openDB();return new Promise((r,s)=>{let o=t.transaction(this.storeName,"readwrite").objectStore(this.storeName).delete(e);o.onsuccess=()=>r(),o.onerror=()=>s(o.error);})}async idbHas(e){let t=await this.openDB();return new Promise((r,s)=>{let o=t.transaction(this.storeName,"readonly").objectStore(this.storeName).count(e);o.onsuccess=()=>r(o.result>0),o.onerror=()=>s(o.error);})}getOrCreateSessionSalt(){let e="ir-cred-salt";if(typeof sessionStorage<"u"){let r=sessionStorage.getItem(e);if(r)return Uint8Array.from(atob(r),s=>s.charCodeAt(0))}let t=crypto.getRandomValues(new Uint8Array(16));return typeof sessionStorage<"u"&&sessionStorage.setItem(e,btoa(String.fromCharCode(...t))),t}async deriveKey(e,t){let r=new TextEncoder,s=await crypto.subtle.importKey("raw",r.encode(e),"PBKDF2",false,["deriveKey"]);return crypto.subtle.deriveKey({name:"PBKDF2",salt:t.buffer,iterations:1e5,hash:"SHA-256"},s,{name:Q,length:256},false,["encrypt","decrypt"])}async encrypt(e){let t=crypto.getRandomValues(new Uint8Array(12)),r=this.getOrCreateSessionSalt(),s=new TextEncoder().encode(e),i=await crypto.subtle.encrypt({name:Q,iv:t},this.cryptoKey,s);return {iv:btoa(String.fromCharCode(...t)),ciphertext:btoa(String.fromCharCode(...new Uint8Array(i))),salt:btoa(String.fromCharCode(...r))}}async decrypt(e){let t=Uint8Array.from(atob(e.iv),i=>i.charCodeAt(0)),r=Uint8Array.from(atob(e.ciphertext),i=>i.charCodeAt(0)),s=await crypto.subtle.decrypt({name:Q,iv:t},this.cryptoKey,r);return new TextDecoder().decode(s)}async get(e){await this.ensureReady();let t=await this.idbGet(e);if(!t)return null;try{return await this.decrypt(t)}catch{return null}}async set(e,t){await this.ensureReady();let r=await this.encrypt(t);await this.idbPut(e,r);}async delete(e){await this.ensureReady(),await this.idbDelete(e);}async has(e){return await this.ensureReady(),this.idbHas(e)}};var ge=class{store=new Map;async get(e){return this.store.get(e)??null}async set(e,t){this.store.set(e,t);}async delete(e){this.store.delete(e);}async has(e){return this.store.has(e)}};
24
- exports.AnthropicAPIProvider=Y;exports.BaseProvider=C;exports.ElectronCredentialStore=he;exports.EngineEventEmitter=J;exports.FallbackEngine=X;exports.GatewayLoader=N;exports.GatewayLoaderError=E;exports.GatewayUnavailableError=x;exports.IndexedDBCredentialStore=ye;exports.InferenceRelay=ke;exports.MODEL_ALIASES=W;exports.MODEL_REGISTRY=$;exports.ManifestDecryptor=D;exports.MemoryCredentialStore=ge;exports.OllamaProvider=j;exports.OpenAIAPIProvider=q;exports.RelayCapExceededError=K;exports.RelayError=U;exports.RequestTooLargeError=z;exports.TelemetryReporter=F;exports.applyRouting=ce;exports.createRelayStream=M;exports.createRoutingFunction=ue;exports.detectCLI=V;exports.estimateCost=me;exports.estimateInputTokens=Yt;exports.findModel=vt;exports.getLastKnownGoodManifest=ot;exports.getModelPricing=pe;exports.getValue=lt;exports.modelsForProvider=Rt;exports.normalizeModel=Ae;exports.providerForModel=ae;exports.validateAnthropicKey=Qe;exports.validateLicense=Pe;
23
+ -----END PUBLIC KEY-----`,O=new Map,Ie=new Set;async function Te(n,e){if(!n)return {valid:false,error:"License key required. Get one at inference-relay.com"};let t=O.get(n);if(t&&Date.now()-t.cachedAt<t.ttl)return t.result;let s=!O.has(n)?1e4:3e3,i=(e||Gt)+Vt,a=async()=>{let p=new AbortController,c=setTimeout(()=>p.abort(),s);try{return await(await fetch(i,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({key:n}),signal:p.signal})).json()}finally{clearTimeout(c);}},o;try{o=await a();}catch{try{if(t)throw new Error("cached answer stands in");o=await a();}catch{return t&&Date.now()-t.cachedAt<$t?t.result:{valid:false,error:"License validation failed (network error). Visit inference-relay.com"}}}let l;if(o.jws)try{let{compactVerify:p,importSPKI:c}=await import('jose'),g=await c(Wt,"RS256"),{payload:m}=await p(o.jws,g);l=JSON.parse(new TextDecoder().decode(m));}catch{return O.delete(n),{valid:false,error:"License validation signature invalid. Possible MITM \u2014 refusing to start."}}else if(l=o,l.valid)return O.delete(n),{valid:false,error:"License validation response not signed. Possible MITM."};return O.set(n,{result:l,cachedAt:Date.now(),ttl:l.valid?Yt:6e4}),l}var xe=class n{static version=Q;get version(){return Q}get features(){return ye}hello(){return ke()}providers=[];config;fallbackEngine;telemetryReporter;routingFn=null;license=null;licensePromise=null;licenseValidatedAt=0;callsSinceValidation=0;manifestDecryptor=new D;capProbeTimer=null;capProbeTicksRemaining=0;messages;constructor(e){if(this.config=e,this.fallbackEngine=new X,this.telemetryReporter=new F({endpoint:e.endpoint?`${e.endpoint}/v1/events`:void 0,enabled:e.telemetry!==false,keyId:e.licenseKey}),this.telemetryReporter.subscribeToEngine(this.fallbackEngine),e.routing&&(this.routingFn=de(e.routing)),e.providers)for(let s of e.providers){let i=typeof s=="string"?{type:s}:s,a=this.createProvider(i);a&&(i.priority!==void 0&&(a.priority=i.priority),this.providers.push(a));}let t=100,r=3600*1e3;this.messages={create:async(s,i)=>{let a=this.license&&(this.callsSinceValidation>=t||Date.now()-this.licenseValidatedAt>=r);if(a&&(this.license=null),!this.license){this.licensePromise||(this.licensePromise=Te(this.config.licenseKey,this.config.endpoint));try{this.license=await this.licensePromise;}finally{this.licensePromise=null;}if(this.licenseValidatedAt=Date.now(),this.callsSinceValidation=0,this.license.valid){if(this.license.capExceeded)throw this.startCapProbe(),new K({tier:this.license.tier||"solo",baseCap:this.license.baseCap??this.license.usageCap??null,topUps:this.license.topUpGrants??0,effectiveCap:this.license.usageCap??null,usedThisCycle:this.license.usageThisMonth??0});!a&&!Ie.has(this.config.licenseKey)&&(Ie.add(this.config.licenseKey),console.log(`[inference-relay] LICENSE AUTHORIZED [${(this.license.tier||"unknown").toUpperCase()} TIER] \u2014 READY.`));}else throw new Error(`[inference-relay] ${this.license.error||"License key invalid or expired. Visit inference-relay.com"}`)}return this.callsSinceValidation++,s.stream?this.streamMessage(s,i):this.executeMessage(s,i)}};}startCapProbe(){if(this.capProbeTimer!==null)return;let e=15e3,t=20,r=4;this.capProbeTicksRemaining=t;let s=async()=>{this.capProbeTicksRemaining-=1;let i=Math.ceil(this.capProbeTicksRemaining*e/6e4);this.capProbeTicksRemaining>0&&this.capProbeTicksRemaining%r===0&&console.log(`[inference-relay] cap exceeded \u2014 checking for top-up every 15s (${i}m remaining)`),this.config.licenseKey&&O.delete(this.config.licenseKey);try{let a=await this.probeValidate();if(a.valid&&a.capExceeded===!1){console.log("[inference-relay] cap cleared \u2014 probe exiting, inference resuming"),this.license=a,this.licenseValidatedAt=Date.now(),this.callsSinceValidation=0,this.stopCapProbe();return}}catch{}this.capProbeTicksRemaining<=0&&(console.log("[inference-relay] cap probe exhausted (5 min) \u2014 stopping"),this.stopCapProbe());};this.capProbeTimer=setInterval(s,e),typeof this.capProbeTimer.unref=="function"&&this.capProbeTimer.unref();}async probeValidate(){return Te(this.config.licenseKey,this.config.endpoint)}stopCapProbe(){this.capProbeTimer!==null&&(clearInterval(this.capProbeTimer),this.capProbeTimer=null),this.capProbeTicksRemaining=0;}static async autoDetect(e){let t=[],r=await V();r.available&&r.authenticated!==false&&t.push({type:"claude-cli",priority:1});let s=e?.subscriptionOnly===true||process.env.IR_SUBSCRIPTION_ONLY==="1";return process.env.ANTHROPIC_API_KEY&&!s&&t.push({type:"anthropic-api",priority:2,apiKey:process.env.ANTHROPIC_API_KEY}),new n({licenseKey:e?.licenseKey||process.env.IR_LICENSE_KEY||"",providers:t,fallback:e?.fallback??true,subscriptionOnly:s,...e})}async warmup(){let e=this.providers.find(t=>t.type==="claude-cli");e&&"warmup"in e&&typeof e.warmup=="function"&&await e.warmup();}resetHealth(){this.fallbackEngine.resetHealth();}markHealthy(e){this.fallbackEngine.markHealthy(e);}dispose(){this.providers=[],this.config.licenseKey&&(O.delete(this.config.licenseKey),Ie.delete(this.config.licenseKey)),this.license=null,this.licensePromise=null,this.callsSinceValidation=0,this.stopCapProbe(),this.telemetryReporter.unsubscribe(),this.manifestDecryptor.clearCache(),De();}resolveProviders(e){if(!this.routingFn)return this.providers;let t=he(e),r=this.routingFn(e,t);return ue(this.providers,r)}async executeMessage(e,t){let r=this.resolveProviders(e);return this.fallbackEngine.execute(e,r,{signal:t?.signal,licenseState:this.manifestDecryptor.getSecurityState(),subscriptionOnly:this.config.subscriptionOnly,maxSameProviderRetries:this.config.maxSameProviderRetries})}async streamMessage(e,t){let r=this.resolveProviders(e);return this.fallbackEngine.stream(e,r,{signal:t?.signal,licenseState:this.manifestDecryptor.getSecurityState(),subscriptionOnly:this.config.subscriptionOnly,maxSameProviderRetries:this.config.maxSameProviderRetries})}createProvider(e){switch(e.type){case "claude-cli":return new ie(this.config.licenseKey,{retryDelaysMs:this.config.gatewayRetryDelaysMs});case "kimi-cli":return new ae(this.config.licenseKey,{retryDelaysMs:this.config.gatewayRetryDelaysMs});case "anthropic-api":return e.apiKey?new W({apiKey:e.apiKey,baseUrl:e.baseUrl}):null;case "openai-api":return e.apiKey?new q({apiKey:e.apiKey,baseUrl:e.baseUrl}):null;case "ollama":return new j({baseUrl:e.baseUrl});default:return null}}};function qt(n){if(!n)return 0;if(typeof n=="string")return Math.ceil(n.length/4);if(!Array.isArray(n))return 0;let e=0;for(let t of n){if(!t||typeof t!="object")continue;let r=t;if(r.type==="image"||r.type==="image_url"){e+=1500;continue}if(typeof r.text=="string"){e+=Math.ceil(r.text.length/4);continue}try{e+=Math.ceil(JSON.stringify(t).length/4);}catch{e+=50;}}return e}function jt(n){return 8+qt(n.content)}function Jt(n){let e=50;typeof n.system=="string"&&(e+=Math.ceil(n.system.length/4));for(let t of n.messages)e+=jt(t);return e}se();var zt="https://api.anthropic.com/v1/messages",Xt="2023-06-01";async function tt(n){try{let e=await fetch(zt,{method:"POST",headers:{"content-type":"application/json","x-api-key":n,"anthropic-version":Xt},body:JSON.stringify({model:"claude-haiku-4-5-20250414",max_tokens:1,messages:[{role:"user",content:"hi"}]})});return e.status===401?{valid:!1,error:"Invalid API key (401)"}:e.ok?{valid:!0}:e.status===429?{valid:!0,error:"Rate limited (429)"}:e.status===529?{valid:!0,error:"API overloaded (529)"}:{valid:!1,error:`Unexpected status ${e.status}`}}catch(e){return {valid:false,error:`Network error: ${e.message}`}}}var ge=class{filePath;safeStorage=null;memoryFallback=new Map;constructor(e){let t=e?.storagePath||path.join(os.homedir(),".inference-relay");fs.existsSync(t)||fs.mkdirSync(t,{recursive:true}),this.filePath=path.join(t,"credentials.enc");try{let r=at("electron");r?.safeStorage?.isEncryptionAvailable?.()&&(this.safeStorage=r.safeStorage);}catch{}}readDisk(){if(!fs.existsSync(this.filePath))return {};try{let e=fs.readFileSync(this.filePath,"utf-8");return JSON.parse(e)}catch{return {}}}writeDisk(e){fs.writeFileSync(this.filePath,JSON.stringify(e,null,2),{mode:384});}async get(e){if(!this.safeStorage)return this.memoryFallback.get(e)??null;let r=this.readDisk()[e];if(!r)return null;try{let s=Buffer.from(r.data,"base64");return this.safeStorage.decryptString(s)}catch{return null}}async set(e,t){if(!this.safeStorage){this.memoryFallback.set(e,t);return}let r=this.readDisk(),s=this.safeStorage.encryptString(t);r[e]={data:s.toString("base64")},this.writeDisk(r);}async delete(e){if(!this.safeStorage){this.memoryFallback.delete(e);return}let t=this.readDisk();e in t&&(delete t[e],this.writeDisk(t));}async has(e){if(!this.safeStorage)return this.memoryFallback.has(e);let t=this.readDisk();return e in t}};var Z="AES-GCM";var fe=class{dbName="inference-relay-credentials";storeName="keys";cryptoKey=null;passphrase;initPromise=null;constructor(e){this.passphrase=e?.passphrase,e?.dbName&&(this.dbName=e.dbName);}async ensureReady(){if(!this.cryptoKey)return this.initPromise?this.initPromise:(this.initPromise=this.init(),this.initPromise)}async init(){if(await this.openDB(),this.passphrase)this.cryptoKey=await this.deriveKey(this.passphrase,this.getOrCreateSessionSalt());else {let e=typeof sessionStorage<"u"?sessionStorage.getItem("ir-cred-key"):null;if(e){let t=Uint8Array.from(atob(e),r=>r.charCodeAt(0));this.cryptoKey=await crypto.subtle.importKey("raw",t,Z,false,["encrypt","decrypt"]);}else {this.cryptoKey=await crypto.subtle.generateKey({name:Z,length:256},true,["encrypt","decrypt"]);let t=await crypto.subtle.exportKey("raw",this.cryptoKey),r=btoa(String.fromCharCode(...new Uint8Array(t)));typeof sessionStorage<"u"&&sessionStorage.setItem("ir-cred-key",r);}}}openDB(){return new Promise((e,t)=>{let r=indexedDB.open(this.dbName,1);r.onupgradeneeded=()=>{let s=r.result;s.objectStoreNames.contains(this.storeName)||s.createObjectStore(this.storeName);},r.onsuccess=()=>e(r.result),r.onerror=()=>t(r.error);})}async idbGet(e){let t=await this.openDB();return new Promise((r,s)=>{let o=t.transaction(this.storeName,"readonly").objectStore(this.storeName).get(e);o.onsuccess=()=>r(o.result),o.onerror=()=>s(o.error);})}async idbPut(e,t){let r=await this.openDB();return new Promise((s,i)=>{let l=r.transaction(this.storeName,"readwrite").objectStore(this.storeName).put(t,e);l.onsuccess=()=>s(),l.onerror=()=>i(l.error);})}async idbDelete(e){let t=await this.openDB();return new Promise((r,s)=>{let o=t.transaction(this.storeName,"readwrite").objectStore(this.storeName).delete(e);o.onsuccess=()=>r(),o.onerror=()=>s(o.error);})}async idbHas(e){let t=await this.openDB();return new Promise((r,s)=>{let o=t.transaction(this.storeName,"readonly").objectStore(this.storeName).count(e);o.onsuccess=()=>r(o.result>0),o.onerror=()=>s(o.error);})}getOrCreateSessionSalt(){let e="ir-cred-salt";if(typeof sessionStorage<"u"){let r=sessionStorage.getItem(e);if(r)return Uint8Array.from(atob(r),s=>s.charCodeAt(0))}let t=crypto.getRandomValues(new Uint8Array(16));return typeof sessionStorage<"u"&&sessionStorage.setItem(e,btoa(String.fromCharCode(...t))),t}async deriveKey(e,t){let r=new TextEncoder,s=await crypto.subtle.importKey("raw",r.encode(e),"PBKDF2",false,["deriveKey"]);return crypto.subtle.deriveKey({name:"PBKDF2",salt:t.buffer,iterations:1e5,hash:"SHA-256"},s,{name:Z,length:256},false,["encrypt","decrypt"])}async encrypt(e){let t=crypto.getRandomValues(new Uint8Array(12)),r=this.getOrCreateSessionSalt(),s=new TextEncoder().encode(e),i=await crypto.subtle.encrypt({name:Z,iv:t},this.cryptoKey,s);return {iv:btoa(String.fromCharCode(...t)),ciphertext:btoa(String.fromCharCode(...new Uint8Array(i))),salt:btoa(String.fromCharCode(...r))}}async decrypt(e){let t=Uint8Array.from(atob(e.iv),i=>i.charCodeAt(0)),r=Uint8Array.from(atob(e.ciphertext),i=>i.charCodeAt(0)),s=await crypto.subtle.decrypt({name:Z,iv:t},this.cryptoKey,r);return new TextDecoder().decode(s)}async get(e){await this.ensureReady();let t=await this.idbGet(e);if(!t)return null;try{return await this.decrypt(t)}catch{return null}}async set(e,t){await this.ensureReady();let r=await this.encrypt(t);await this.idbPut(e,r);}async delete(e){await this.ensureReady(),await this.idbDelete(e);}async has(e){return await this.ensureReady(),this.idbHas(e)}};var _e=class{store=new Map;async get(e){return this.store.get(e)??null}async set(e,t){this.store.set(e,t);}async delete(e){this.store.delete(e);}async has(e){return this.store.has(e)}};
24
+ exports.AnthropicAPIProvider=W;exports.BaseProvider=S;exports.ElectronCredentialStore=ge;exports.EngineEventEmitter=J;exports.FallbackEngine=X;exports.GatewayLoader=N;exports.GatewayLoaderError=E;exports.GatewayUnavailableError=x;exports.IndexedDBCredentialStore=fe;exports.InferenceRelay=xe;exports.MODEL_ALIASES=$;exports.MODEL_REGISTRY=Y;exports.ManifestDecryptor=D;exports.MemoryCredentialStore=_e;exports.OllamaProvider=j;exports.OpenAIAPIProvider=q;exports.RELAY_FEATURES=ye;exports.RELAY_VERSION=Q;exports.RelayCapExceededError=K;exports.RelayError=U;exports.RequestTooLargeError=z;exports.TelemetryReporter=F;exports.applyRouting=ue;exports.createRelayStream=M;exports.createRoutingFunction=de;exports.detectCLI=V;exports.estimateCost=he;exports.estimateInputTokens=Jt;exports.findModel=St;exports.getLastKnownGoodManifest=ut;exports.getModelPricing=me;exports.getValue=dt;exports.modelsForProvider=Ct;exports.normalizeModel=Ce;exports.providerForModel=oe;exports.relayHello=ke;exports.validateAnthropicKey=tt;exports.validateLicense=Te;
package/dist/index.d.cts CHANGED
@@ -1,9 +1,50 @@
1
- import { C as CreateParams, R as RelayResult, I as InferenceRelayConfig, P as ProbeResult, a as CostEstimate, b as ProviderHealth, c as RateLimitEvent, E as EngineEvent, S as SecurityState$1, A as AuditEvent, d as RoutingConfig } from './index--VZSRE8-.cjs';
2
- export { e as ConcurrencyConfig, f as ContentBlock, M as Message, g as ProviderConfig, h as RelayCapExceededError, i as SecureEnclaveConfig, j as SecureEnclaveCredentials, k as SegmentUsage } from './index--VZSRE8-.cjs';
1
+ import { C as CreateParams, R as RelayResult, I as InferenceRelayConfig, P as ProbeResult, a as CostEstimate, b as ProviderHealth, c as RateLimitEvent, E as EngineEvent, S as SecurityState$1, A as AuditEvent, d as RoutingConfig } from './index-yGhLy31i.cjs';
2
+ export { e as ConcurrencyConfig, f as ContentBlock, M as Message, g as ProviderConfig, h as RelayCapExceededError, i as SecureEnclaveConfig, j as SecureEnclaveCredentials, k as SegmentUsage } from './index-yGhLy31i.cjs';
3
3
  import { RelayStream } from './stream/index.cjs';
4
4
  export { createRelayStream } from './stream/index.cjs';
5
5
  import { EventEmitter } from 'node:events';
6
6
 
7
+ /**
8
+ * The relay's version and what its roads keep, for its caller (1.6.10, 9 Nov 2026).
9
+ *
10
+ * A caller that streams (a desktop app, for one) has to know, BEFORE it chooses the streamed road, whether the relay it
11
+ * runs keeps the whole request there. Published 1.6.8's streamed road sent the model only the system prompt and the last message, and
12
+ * returned tool names prefixed mcp__r__; 1.6.10's streamed road runs the queue route's own round (claude-cli streamRaw over executeImpl),
13
+ * so every message, every tool_use and tool_result block, and the bare tool names survive.
14
+ *
15
+ * Read it with no call and no network:
16
+ * import { RELAY_VERSION, RELAY_FEATURES, relayHello } from 'inference-relay';
17
+ * RELAY_FEATURES.streamKeepsHistory === true // the one test a caller needs; undefined on 1.6.8 and earlier (no such export)
18
+ * relayHello() // { name: 'inference-relay', version: '1.6.10', features: { streamKeepsHistory: true, ... } }
19
+ * or on an instance: relay.version, relay.features, relay.hello() (and InferenceRelay.version).
20
+ *
21
+ * A caller that may run an OLDER relay (1.6.8 and earlier have none of these exports) must not import them by name: a static
22
+ * `import { RELAY_FEATURES }` of a missing export is a hard error in ESM and in strict bundlers. Read them off the module instead, with
23
+ * optional access, so an old relay reads as "does not keep history":
24
+ * const ir = await import('inference-relay'); // or: import * as ir from 'inference-relay'
25
+ * const keepsHistory = ir.RELAY_FEATURES?.streamKeepsHistory === true;
26
+ *
27
+ * The flags describe the claude-cli streamed road of the gateway bundle published with this library version: the library fetches the
28
+ * bundle by its own version (gateway/lazy-provider.ts, LIBRARY_VERSION), so a 1.6.10 library always runs the 1.6.10 bundle.
29
+ */
30
+ declare const RELAY_VERSION: string;
31
+ interface RelayFeatures {
32
+ /** The streamed road sends the model every message of the request (earlier turns, a tool round's call and result), as the queue road does. */
33
+ readonly streamKeepsHistory: boolean;
34
+ /** tool_use and tool_result content blocks in the request survive on the streamed road. */
35
+ readonly streamToolBlocks: boolean;
36
+ /** Tool names on the streamed road come back as the caller declared them (no mcp__r__ prefix, no CLI caller field). */
37
+ readonly streamBareToolNames: boolean;
38
+ }
39
+ declare const RELAY_FEATURES: RelayFeatures;
40
+ interface RelayHello {
41
+ readonly name: 'inference-relay';
42
+ readonly version: string;
43
+ readonly features: RelayFeatures;
44
+ }
45
+ /** One object a caller can log, send to its server, or put on a response header (JSON). */
46
+ declare function relayHello(): RelayHello;
47
+
7
48
  interface LicenseValidation {
8
49
  valid: boolean;
9
50
  tier?: string;
@@ -45,6 +86,14 @@ interface LicenseValidation {
45
86
  */
46
87
  declare function validateLicense(key: string, endpoint?: string): Promise<LicenseValidation>;
47
88
  declare class InferenceRelay {
89
+ /** This library's version (package.json at build time), e.g. '1.6.10'. Also exported as RELAY_VERSION. */
90
+ static readonly version: string;
91
+ /** This library's version, for a caller holding an instance. */
92
+ get version(): string;
93
+ /** What this relay's roads keep (src/version.ts). A caller lifts a one-message streaming guard only when streamKeepsHistory is true. */
94
+ get features(): RelayFeatures;
95
+ /** { name, version, features }: one object a caller can log or send on (no call, no network). */
96
+ hello(): RelayHello;
48
97
  private providers;
49
98
  private config;
50
99
  private fallbackEngine;
@@ -893,4 +942,4 @@ declare class GatewayLoader {
893
942
  static clearCache(): void;
894
943
  }
895
944
 
896
- export { AnthropicAPIProvider, AuditEvent, BaseProvider, type CLIManifest, CostEstimate, CreateParams, type CredentialStore, ElectronCredentialStore, EngineEvent, EngineEventEmitter, FallbackEngine, GatewayLoader, GatewayLoaderError, type GatewayModule, GatewayUnavailableError, IndexedDBCredentialStore, InferenceRelay, InferenceRelayConfig, type LicenseValidation, MODEL_ALIASES, MODEL_REGISTRY, ManifestDecryptor, MemoryCredentialStore, type ModelEntry, OllamaProvider, OpenAIAPIProvider, ProbeResult, ProviderHealth, type ProviderType, RateLimitEvent, RelayError, RelayResult, RelayStream, RequestTooLargeError, RoutingConfig, type RoutingDecision, type RoutingFunction, SecurityState$1 as SecurityState, TelemetryReporter, applyRouting, createRoutingFunction, detectCLI, estimateCost, estimateInputTokens, findModel, getLastKnownGoodManifest, getModelPricing, getValue, modelsForProvider, normalizeModel, providerForModel, validateAnthropicKey, validateLicense };
945
+ export { AnthropicAPIProvider, AuditEvent, BaseProvider, type CLIManifest, CostEstimate, CreateParams, type CredentialStore, ElectronCredentialStore, EngineEvent, EngineEventEmitter, FallbackEngine, GatewayLoader, GatewayLoaderError, type GatewayModule, GatewayUnavailableError, IndexedDBCredentialStore, InferenceRelay, InferenceRelayConfig, type LicenseValidation, MODEL_ALIASES, MODEL_REGISTRY, ManifestDecryptor, MemoryCredentialStore, type ModelEntry, OllamaProvider, OpenAIAPIProvider, ProbeResult, ProviderHealth, type ProviderType, RELAY_FEATURES, RELAY_VERSION, RateLimitEvent, RelayError, type RelayFeatures, type RelayHello, RelayResult, RelayStream, RequestTooLargeError, RoutingConfig, type RoutingDecision, type RoutingFunction, SecurityState$1 as SecurityState, TelemetryReporter, applyRouting, createRoutingFunction, detectCLI, estimateCost, estimateInputTokens, findModel, getLastKnownGoodManifest, getModelPricing, getValue, modelsForProvider, normalizeModel, providerForModel, relayHello, validateAnthropicKey, validateLicense };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,50 @@
1
- import { C as CreateParams, R as RelayResult, I as InferenceRelayConfig, P as ProbeResult, a as CostEstimate, b as ProviderHealth, c as RateLimitEvent, E as EngineEvent, S as SecurityState$1, A as AuditEvent, d as RoutingConfig } from './index--VZSRE8-.js';
2
- export { e as ConcurrencyConfig, f as ContentBlock, M as Message, g as ProviderConfig, h as RelayCapExceededError, i as SecureEnclaveConfig, j as SecureEnclaveCredentials, k as SegmentUsage } from './index--VZSRE8-.js';
1
+ import { C as CreateParams, R as RelayResult, I as InferenceRelayConfig, P as ProbeResult, a as CostEstimate, b as ProviderHealth, c as RateLimitEvent, E as EngineEvent, S as SecurityState$1, A as AuditEvent, d as RoutingConfig } from './index-yGhLy31i.js';
2
+ export { e as ConcurrencyConfig, f as ContentBlock, M as Message, g as ProviderConfig, h as RelayCapExceededError, i as SecureEnclaveConfig, j as SecureEnclaveCredentials, k as SegmentUsage } from './index-yGhLy31i.js';
3
3
  import { RelayStream } from './stream/index.js';
4
4
  export { createRelayStream } from './stream/index.js';
5
5
  import { EventEmitter } from 'node:events';
6
6
 
7
+ /**
8
+ * The relay's version and what its roads keep, for its caller (1.6.10, 9 Nov 2026).
9
+ *
10
+ * A caller that streams (a desktop app, for one) has to know, BEFORE it chooses the streamed road, whether the relay it
11
+ * runs keeps the whole request there. Published 1.6.8's streamed road sent the model only the system prompt and the last message, and
12
+ * returned tool names prefixed mcp__r__; 1.6.10's streamed road runs the queue route's own round (claude-cli streamRaw over executeImpl),
13
+ * so every message, every tool_use and tool_result block, and the bare tool names survive.
14
+ *
15
+ * Read it with no call and no network:
16
+ * import { RELAY_VERSION, RELAY_FEATURES, relayHello } from 'inference-relay';
17
+ * RELAY_FEATURES.streamKeepsHistory === true // the one test a caller needs; undefined on 1.6.8 and earlier (no such export)
18
+ * relayHello() // { name: 'inference-relay', version: '1.6.10', features: { streamKeepsHistory: true, ... } }
19
+ * or on an instance: relay.version, relay.features, relay.hello() (and InferenceRelay.version).
20
+ *
21
+ * A caller that may run an OLDER relay (1.6.8 and earlier have none of these exports) must not import them by name: a static
22
+ * `import { RELAY_FEATURES }` of a missing export is a hard error in ESM and in strict bundlers. Read them off the module instead, with
23
+ * optional access, so an old relay reads as "does not keep history":
24
+ * const ir = await import('inference-relay'); // or: import * as ir from 'inference-relay'
25
+ * const keepsHistory = ir.RELAY_FEATURES?.streamKeepsHistory === true;
26
+ *
27
+ * The flags describe the claude-cli streamed road of the gateway bundle published with this library version: the library fetches the
28
+ * bundle by its own version (gateway/lazy-provider.ts, LIBRARY_VERSION), so a 1.6.10 library always runs the 1.6.10 bundle.
29
+ */
30
+ declare const RELAY_VERSION: string;
31
+ interface RelayFeatures {
32
+ /** The streamed road sends the model every message of the request (earlier turns, a tool round's call and result), as the queue road does. */
33
+ readonly streamKeepsHistory: boolean;
34
+ /** tool_use and tool_result content blocks in the request survive on the streamed road. */
35
+ readonly streamToolBlocks: boolean;
36
+ /** Tool names on the streamed road come back as the caller declared them (no mcp__r__ prefix, no CLI caller field). */
37
+ readonly streamBareToolNames: boolean;
38
+ }
39
+ declare const RELAY_FEATURES: RelayFeatures;
40
+ interface RelayHello {
41
+ readonly name: 'inference-relay';
42
+ readonly version: string;
43
+ readonly features: RelayFeatures;
44
+ }
45
+ /** One object a caller can log, send to its server, or put on a response header (JSON). */
46
+ declare function relayHello(): RelayHello;
47
+
7
48
  interface LicenseValidation {
8
49
  valid: boolean;
9
50
  tier?: string;
@@ -45,6 +86,14 @@ interface LicenseValidation {
45
86
  */
46
87
  declare function validateLicense(key: string, endpoint?: string): Promise<LicenseValidation>;
47
88
  declare class InferenceRelay {
89
+ /** This library's version (package.json at build time), e.g. '1.6.10'. Also exported as RELAY_VERSION. */
90
+ static readonly version: string;
91
+ /** This library's version, for a caller holding an instance. */
92
+ get version(): string;
93
+ /** What this relay's roads keep (src/version.ts). A caller lifts a one-message streaming guard only when streamKeepsHistory is true. */
94
+ get features(): RelayFeatures;
95
+ /** { name, version, features }: one object a caller can log or send on (no call, no network). */
96
+ hello(): RelayHello;
48
97
  private providers;
49
98
  private config;
50
99
  private fallbackEngine;
@@ -893,4 +942,4 @@ declare class GatewayLoader {
893
942
  static clearCache(): void;
894
943
  }
895
944
 
896
- export { AnthropicAPIProvider, AuditEvent, BaseProvider, type CLIManifest, CostEstimate, CreateParams, type CredentialStore, ElectronCredentialStore, EngineEvent, EngineEventEmitter, FallbackEngine, GatewayLoader, GatewayLoaderError, type GatewayModule, GatewayUnavailableError, IndexedDBCredentialStore, InferenceRelay, InferenceRelayConfig, type LicenseValidation, MODEL_ALIASES, MODEL_REGISTRY, ManifestDecryptor, MemoryCredentialStore, type ModelEntry, OllamaProvider, OpenAIAPIProvider, ProbeResult, ProviderHealth, type ProviderType, RateLimitEvent, RelayError, RelayResult, RelayStream, RequestTooLargeError, RoutingConfig, type RoutingDecision, type RoutingFunction, SecurityState$1 as SecurityState, TelemetryReporter, applyRouting, createRoutingFunction, detectCLI, estimateCost, estimateInputTokens, findModel, getLastKnownGoodManifest, getModelPricing, getValue, modelsForProvider, normalizeModel, providerForModel, validateAnthropicKey, validateLicense };
945
+ export { AnthropicAPIProvider, AuditEvent, BaseProvider, type CLIManifest, CostEstimate, CreateParams, type CredentialStore, ElectronCredentialStore, EngineEvent, EngineEventEmitter, FallbackEngine, GatewayLoader, GatewayLoaderError, type GatewayModule, GatewayUnavailableError, IndexedDBCredentialStore, InferenceRelay, InferenceRelayConfig, type LicenseValidation, MODEL_ALIASES, MODEL_REGISTRY, ManifestDecryptor, MemoryCredentialStore, type ModelEntry, OllamaProvider, OpenAIAPIProvider, ProbeResult, ProviderHealth, type ProviderType, RELAY_FEATURES, RELAY_VERSION, RateLimitEvent, RelayError, type RelayFeatures, type RelayHello, RelayResult, RelayStream, RequestTooLargeError, RoutingConfig, type RoutingDecision, type RoutingFunction, SecurityState$1 as SecurityState, TelemetryReporter, applyRouting, createRoutingFunction, detectCLI, estimateCost, estimateInputTokens, findModel, getLastKnownGoodManifest, getModelPricing, getValue, modelsForProvider, normalizeModel, providerForModel, relayHello, validateAnthropicKey, validateLicense };