@urun-sh/openai 0.6.22 → 0.6.25

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.
@@ -1,7 +1,7 @@
1
- import{a as Se,c as z,d as Re,e as W}from"./chunk-KSC5Q6PZ.js";import{C as le,D as ce,E as ue,F as de,G as he,K as pe,L as T,M as fe,Q as ge,R as me,S as ye,T as we,U as be,V as ve,W as _e,X as Ee,b as oe,d as se,e as ie,q as ae,r as O}from"./chunk-4W3ZPIR7.js";import{c as te,g as ne,i as K}from"./chunk-CUDFHFNX.js";import{a as re}from"./chunk-3JWYIKHM.js";import{c as p}from"./chunk-YSFSRI3D.js";p();import{Counter as x,Gauge as xe,Histogram as ke,Registry as Ge,collectDefaultMetrics as Be}from"@prometheus-io/client";import{createServer as Ke}from"http";var Bt=9464,Ce="/metrics",A="(other)",ze="(none)",We=64,je=128,Je=["/v1/responses","/v1/chat/completions","/v1/systemone","/v1/messages","/v1/messages/count_tokens","/v1/models","/v1/images/models","/v1/images/generations","/v1/images/edits","/v1/audio/speech","/v1/audio/models","/v1/audio/transcriptions",pe,ge,te],Ve=new Set(Je);function qe(t){return Ve.has(t)?t:A}function Ye(t){return t===null?"none":t>=500?"5xx":t>=400?"4xx":t>=300?"3xx":t>=200?"2xx":"1xx"}var I=class{constructor(e){this.max=e}max;admitted=new Set;overflows=0;of(e,n=!0){return e===null||e===""?ze:this.admitted.has(e)?e:n?this.admitted.size>=this.max?(this.overflows+=1,A):(this.admitted.add(e),e):A}get size(){return this.admitted.size}get overflowCount(){return this.overflows}},P=class{registry=new Ge;modelLabel=new I(We);orgLabel=new I(je);requests;duration;ttft;streamErrors;undelivered;ledgerWrites;ledgerQueue;labelCardinality;labelOverflows;constructor(e={}){this.requests=new x({name:"urun_inference_requests_total",help:"Completed /v1 requests, by lane, method, HTTP status class, streaming, model and org.",labelNames:["lane","method","status_class","stream","model","org"],registers:[this.registry]}),this.duration=new ke({name:"urun_inference_request_duration_seconds",help:"Wall time from request receipt to response close. For a stream this is the WHOLE generation, not the time to the head.",labelNames:["lane","stream","model","org"],buckets:[.1,.25,.5,1,2.5,5,10,30,60,120,300,600],registers:[this.registry]}),this.ttft=new ke({name:"urun_inference_ttft_seconds",help:"Time to first output-text delta on a streamed response. The metric Hugging Face gates provider listing on (<5s for conversational models).",labelNames:["lane","model","org"],buckets:[.1,.25,.5,1,2,3,5,8,15,30,60],registers:[this.registry]}),this.streamErrors=new x({name:"urun_inference_stream_errors_total",help:"Responses that emitted an error frame AFTER the head flushed. These are HTTP 200 with a complete body, so no status-based metric can see them.",labelNames:["lane","model"],registers:[this.registry]}),this.undelivered=new x({name:"urun_inference_undelivered_total",help:"Responses whose socket did not finish flushing \u2014 the client vanished mid-answer.",labelNames:["lane","model"],registers:[this.registry]}),this.ledgerWrites=new x({name:"urun_inference_ledger_writes_total",help:"Terminal fate of the per-request ledger row for each billable /v1 request. outcome=written is the row landing (reason=priced|unpriced); outcome=skipped and outcome=failed are BOTH revenue that went unrecorded. failed/duplicate is the ENG-410 fabricated-row signal.",labelNames:["outcome","reason"],registers:[this.registry]});for(let n of me)this.ledgerWrites.inc({outcome:n.outcome,reason:n.reason},0);this.ledgerQueue=new xe({name:"urun_inference_ledger_queue",help:'The ledger write queue right now. measure="depth" is rows waiting for a worker; measure="oldest_age_seconds" is how long the head has waited \u2014 approaching the dwell bound means rows are about to be given up on, which is revenue lost.',labelNames:["measure"],registers:[this.registry],collect:()=>{this.ledgerQueue.set({measure:"depth"},ye()),this.ledgerQueue.set({measure:"oldest_age_seconds"},we()/1e3)}}),this.labelCardinality=new xe({name:"urun_inference_label_cardinality",help:"Distinct values admitted for a bounded metric label. At its ceiling, further values report as (other).",labelNames:["label"],registers:[this.registry],collect:()=>{this.labelCardinality.set({label:"model"},this.modelLabel.size),this.labelCardinality.set({label:"org"},this.orgLabel.size)}}),this.labelOverflows=new x({name:"urun_inference_label_overflows_total",help:"Observations that would have taken a new label value but found the budget spent. Non-zero means a metric dimension is now partly blind.",labelNames:["label"],registers:[this.registry]}),e.defaultMetrics!==!1&&Be({register:this.registry,prefix:"urun_inference_proxy_"})}observeLedger(e){this.ledgerWrites.inc({outcome:e.outcome,reason:e.reason})}observe(e){let n=qe(e.path),o=e.status!==null&&!(e.status>=400&&e.status<500),r=this.modelLabel.overflowCount+this.orgLabel.overflowCount,a=this.modelLabel.of(e.model,o),s=this.orgLabel.of(e.org,o);this.modelLabel.overflowCount+this.orgLabel.overflowCount-r>0&&(this.modelLabel.overflowCount>r?this.labelOverflows.inc({label:"model"}):this.labelOverflows.inc({label:"org"}));let c=e.stream===!0?"true":"false";this.requests.inc({lane:n,method:e.method===""?A:e.method,status_class:Ye(e.status),stream:c,model:a,org:s}),this.duration.observe({lane:n,stream:c,model:a,org:s},e.duration_ms/1e3),e.ttft_ms!==null&&this.ttft.observe({lane:n,model:a,org:s},e.ttft_ms/1e3),e.stream_error&&this.streamErrors.inc({lane:n,model:a}),e.delivered||this.undelivered.inc({lane:n,model:a})}scrape(){return this.registry.metrics()}get contentType(){return this.registry.contentType}};function Oe(t){return Ke((e,n)=>{(async()=>{let o=(e.url??"/").split("?")[0];if(e.method!=="GET"||o!==Ce){n.writeHead(404,{"content-type":"text/plain"}),n.end(`no route for ${e.method} ${o} \u2014 this port serves only GET ${Ce}
1
+ import{a as Se,c as z,d as Re,e as W}from"./chunk-PPPJ42Z7.js";import{C as le,D as ce,E as ue,F as de,G as he,K as pe,L as T,M as fe,Q as ge,R as me,S as ye,T as we,U as be,V as ve,W as _e,X as Ee,b as oe,d as se,e as ie,q as ae,r as O}from"./chunk-SOAZAPCO.js";import{c as te,g as ne,i as K}from"./chunk-CUDFHFNX.js";import{a as re}from"./chunk-3JWYIKHM.js";import{c as f}from"./chunk-YSFSRI3D.js";f();import{Counter as x,Gauge as xe,Histogram as ke,Registry as Ge,collectDefaultMetrics as Be}from"@prometheus-io/client";import{createServer as Ke}from"http";var Bt=9464,Ce="/metrics",A="(other)",ze="(none)",We=64,je=128,Je=["/v1/responses","/v1/chat/completions","/v1/systemone","/v1/messages","/v1/messages/count_tokens","/v1/models","/v1/images/models","/v1/images/generations","/v1/images/edits","/v1/audio/speech","/v1/audio/models","/v1/audio/transcriptions",pe,ge,te],Ve=new Set(Je);function qe(t){return Ve.has(t)?t:A}function Ye(t){return t===null?"none":t>=500?"5xx":t>=400?"4xx":t>=300?"3xx":t>=200?"2xx":"1xx"}var I=class{constructor(e){this.max=e}max;admitted=new Set;overflows=0;of(e,n=!0){return e===null||e===""?ze:this.admitted.has(e)?e:n?this.admitted.size>=this.max?(this.overflows+=1,A):(this.admitted.add(e),e):A}get size(){return this.admitted.size}get overflowCount(){return this.overflows}},P=class{registry=new Ge;modelLabel=new I(We);orgLabel=new I(je);requests;duration;ttft;streamErrors;undelivered;ledgerWrites;ledgerQueue;labelCardinality;labelOverflows;constructor(e={}){this.requests=new x({name:"urun_inference_requests_total",help:"Completed /v1 requests, by lane, method, HTTP status class, streaming, model and org.",labelNames:["lane","method","status_class","stream","model","org"],registers:[this.registry]}),this.duration=new ke({name:"urun_inference_request_duration_seconds",help:"Wall time from request receipt to response close. For a stream this is the WHOLE generation, not the time to the head.",labelNames:["lane","stream","model","org"],buckets:[.1,.25,.5,1,2.5,5,10,30,60,120,300,600],registers:[this.registry]}),this.ttft=new ke({name:"urun_inference_ttft_seconds",help:"Time to first output-text delta on a streamed response. The metric Hugging Face gates provider listing on (<5s for conversational models).",labelNames:["lane","model","org"],buckets:[.1,.25,.5,1,2,3,5,8,15,30,60],registers:[this.registry]}),this.streamErrors=new x({name:"urun_inference_stream_errors_total",help:"Responses that emitted an error frame AFTER the head flushed. These are HTTP 200 with a complete body, so no status-based metric can see them.",labelNames:["lane","model"],registers:[this.registry]}),this.undelivered=new x({name:"urun_inference_undelivered_total",help:"Responses whose socket did not finish flushing \u2014 the client vanished mid-answer.",labelNames:["lane","model"],registers:[this.registry]}),this.ledgerWrites=new x({name:"urun_inference_ledger_writes_total",help:"Terminal fate of the per-request ledger row for each billable /v1 request. outcome=written is the row landing (reason=priced|unpriced); outcome=skipped and outcome=failed are BOTH revenue that went unrecorded. failed/duplicate is the ENG-410 fabricated-row signal.",labelNames:["outcome","reason"],registers:[this.registry]});for(let n of me)this.ledgerWrites.inc({outcome:n.outcome,reason:n.reason},0);this.ledgerQueue=new xe({name:"urun_inference_ledger_queue",help:'The ledger write queue right now. measure="depth" is rows waiting for a worker; measure="oldest_age_seconds" is how long the head has waited \u2014 approaching the dwell bound means rows are about to be given up on, which is revenue lost.',labelNames:["measure"],registers:[this.registry],collect:()=>{this.ledgerQueue.set({measure:"depth"},ye()),this.ledgerQueue.set({measure:"oldest_age_seconds"},we()/1e3)}}),this.labelCardinality=new xe({name:"urun_inference_label_cardinality",help:"Distinct values admitted for a bounded metric label. At its ceiling, further values report as (other).",labelNames:["label"],registers:[this.registry],collect:()=>{this.labelCardinality.set({label:"model"},this.modelLabel.size),this.labelCardinality.set({label:"org"},this.orgLabel.size)}}),this.labelOverflows=new x({name:"urun_inference_label_overflows_total",help:"Observations that would have taken a new label value but found the budget spent. Non-zero means a metric dimension is now partly blind.",labelNames:["label"],registers:[this.registry]}),e.defaultMetrics!==!1&&Be({register:this.registry,prefix:"urun_inference_proxy_"})}observeLedger(e){this.ledgerWrites.inc({outcome:e.outcome,reason:e.reason})}observe(e){let n=qe(e.path),o=e.status!==null&&!(e.status>=400&&e.status<500),r=this.modelLabel.overflowCount+this.orgLabel.overflowCount,a=this.modelLabel.of(e.model,o),s=this.orgLabel.of(e.org,o);this.modelLabel.overflowCount+this.orgLabel.overflowCount-r>0&&(this.modelLabel.overflowCount>r?this.labelOverflows.inc({label:"model"}):this.labelOverflows.inc({label:"org"}));let c=e.stream===!0?"true":"false";this.requests.inc({lane:n,method:e.method===""?A:e.method,status_class:Ye(e.status),stream:c,model:a,org:s}),this.duration.observe({lane:n,stream:c,model:a,org:s},e.duration_ms/1e3),e.ttft_ms!==null&&this.ttft.observe({lane:n,model:a,org:s},e.ttft_ms/1e3),e.stream_error&&this.streamErrors.inc({lane:n,model:a}),e.delivered||this.undelivered.inc({lane:n,model:a})}scrape(){return this.registry.metrics()}get contentType(){return this.registry.contentType}};function Oe(t){return Ke((e,n)=>{(async()=>{let o=(e.url??"/").split("?")[0];if(e.method!=="GET"||o!==Ce){n.writeHead(404,{"content-type":"text/plain"}),n.end(`no route for ${e.method} ${o} \u2014 this port serves only GET ${Ce}
2
2
  `);return}let r=await t.scrape();n.writeHead(200,{"content-type":t.contentType}),n.end(r)})().catch(o=>{if(!n.headersSent){n.writeHead(500,{"content-type":"text/plain"}),n.end(`${String(o instanceof Error?o.message:o)}
3
- `);return}n.end()})})}p();import{createHash as Ae}from"crypto";import{createServer as Qe}from"http";var Jt=9465,Xe="x-urun-key-id",Ze="x-urun-key-fingerprint",et="urun-ratelimit-bucket:",tt="urun-ratelimit-tier:",Te="anonymous",nt=/^urun_sk_[A-Za-z0-9_-]{43}$/;function rt(t){return Ae("sha256").update(`${et}${t}`).digest("hex")}function ot(t){return Ae("sha256").update(`${tt}${t}`).digest("hex")}function st(t){let e=/^Bearer[ \t]+(\S.*)$/i.exec((t??"").trim());if(!e)return null;let n=e[1].trim();return nt.test(n)?n:null}function Ie(){return Qe((t,e)=>{let n=st(t.headers.authorization);e.setHeader(Xe,n===null?Te:rt(n)),e.setHeader(Ze,n===null?Te:ot(n)),e.writeHead(200),e.end()})}p();import{createClientToken as it,ClientTokenError as at}from"@urun-sh/core";var g=class extends Error{constructor(n,o=401){super(n);this.status=o;this.name="ProxyAuthError"}status},S=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function k(t){let e=(t??"").trim();if(e==="")throw new g("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let n=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!n)throw new g("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return n[1].trim()}var lt=60,ct=6e4,ut=4096;function dt(t){return t===401||t===403}var N=class{constructor(e){this.opts=e;this.mint=e.mint??it,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new g("empty Authorization bearer token");let n=this.cache.get(e);if(n&&this.now()-n.at<ct)return{apiKey:e,orgId:n.orgId};let o;try{o=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:lt})).orgId}catch(r){throw r instanceof at&&dt(r.status)?(this.cache.delete(e),new g("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new S(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${r instanceof Error?r.message:String(r)}`)}if(!o)throw new S("the control plane verified the API key but returned no org_id \u2014 refusing to route a request with no tenancy (control-plane contract violation)");if(this.cache.size>=ut){let r=this.cache.keys().next();r.done||this.cache.delete(r.value)}return this.cache.set(e,{orgId:o,at:this.now()}),{apiKey:e,orgId:o}}clear(){this.cache.clear()}};p();var ht=8080,j="serve",en="0.0.0.0";function pt(t,e,n){let o=(t[e]??"").trim();if(!o)throw new Error(`${e} is not set \u2014 ${n}`);return o}function tn(t=process.env){let e=(t.PORT??"").trim(),n=e===""?ht:Number(e);if(!Number.isInteger(n)||n<0||n>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let o=ae(O(pt(t,"URUN_BASE_URL","the hosted proxy needs the uRun session-gateway base it opens sessions and verifies API keys against (e.g. https://api.urun.sh)"),"URUN_BASE_URL")),r=(t.URUN_API_URL??"").trim(),a=r?O(r,"URUN_API_URL"):`${o.replace(/\/+$/,"")}/v1`,s=(t.URUN_CATALOG_URL??"").trim(),i=(t.URUN_CATALOG_ANON_KEY??"").trim();if(!!s!=!!i)throw new Error("URUN_CATALOG_URL and URUN_CATALOG_ANON_KEY must be set together (or both left unset) \u2014 exactly one of them is set, which would silently disable the model-catalog oracle");let c=s?{catalogUrl:O(s,"URUN_CATALOG_URL"),anonKey:i}:null;return{port:n,baseUrl:o,apiUrl:a,catalog:c}}p();import{createHash as ft,randomUUID as gt}from"crypto";var mt=512;function J(t){return`urun-inference-proxy-${ft("sha256").update(t).digest("hex").slice(0,32)}`}var U=class{constructor(e){this.opts=e}opts;tenants=new Map;label(e,n){return Object.assign(Object.create(e),{tenant:n.orgId})}build(e){if(this.opts.build){let a=this.opts.build(e);return a.decision&&this.ensureDecisionReaper(),{router:a.router,clients:this.label(a.clients,e),decision:a.decision}}let n={baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:j,sharedOnlyListing:!0,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,release:"end",decisionPool:Se(process.env,process.env.HOSTNAME)??void 0},o=z(n),r=n.decisionPool?Re(n,o):null;return r&&this.ensureDecisionReaper(),{router:o,clients:this.label(W(o,void 0,void 0,r??void 0),e),decision:r??void 0}}ensureDecisionReaper(){if(this.decisionReaper!==null)return;let e=setInterval(()=>{for(let n of this.tenants.values())n.decision?.reap()},5e3);e.unref(),this.decisionReaper=e}decisionReaper=null;clientsFor(e){let n=this.tenants.get(e.apiKey);if(n)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,n),n.clients;for(;this.tenants.size>=mt;){let r=this.tenants.entries().next();if(r.done)break;let[a,s]=r.value;this.tenants.delete(a),s.router.closeAll().catch(()=>{})}let o=this.build(e);return this.tenants.set(e.apiKey,o),o.clients}get size(){return this.tenants.size}*decisionPools(){for(let e of this.tenants.values())e.decision&&(yield e.decision)}decisionWarmerFor(e){return this.clientsFor(e),this.tenants.get(e.apiKey)?.decision??null}async closeAll(){this.decisionReaper!==null&&(clearInterval(this.decisionReaper),this.decisionReaper=null);let e=[...this.tenants.values()];this.tenants.clear();let n=[];if(await Promise.all(e.map(async o=>{let r=[o.router.closeAll().catch(a=>{n.push(String(a instanceof Error?a.message:a))})];o.decision&&r.push(o.decision.close().catch(a=>{n.push(String(a instanceof Error?a.message:a))})),await Promise.all(r)})),n.length>0)throw new Error(`failed to close ${n.length} tenant backhaul(s): ${n.join("; ")}`)}},Pe=512,yt=1024,$=class extends Error{constructor(){super(`conversation capacity reached (${Pe} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function wt(t){return{sessionStartOptions:e=>{if(e.startsWith(K))return;let n=t.get(e);return n===void 0?void 0:{sessionId:n}},onSessionOpen:(e,n)=>{e.startsWith(K)||t.set(e,n)}}}function bt(t,e,n,o){let r=z({baseUrl:t.baseUrl,orgId:e.orgId,defaultApp:null,fnName:j,sharedOnlyListing:!0,apiUrl:t.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:t.baseUrl},catalog:t.catalog,subject:n.subject,release:"detach",...wt(o)});return{clients:W(r),release:async()=>{await r.closeAll()}}}var L=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,n){let o={id:n,subject:`${J(e.apiKey)}:live:${n}`},r=new Map,a=this.opts.build?this.opts.build(e,o):bt(this.opts,e,o,r);return{id:n,backhaul:a,attachments:0,nativeRefs:r}}attachEntry(e,n){let o=this.byCaller.get(e.apiKey);o===void 0&&(o=new Map,this.byCaller.set(e.apiKey,o));let r=o.get(n);if(r!==void 0)return o.delete(n),o.set(n,r),r.attachments+=1,r;this.evictOverflow();let a=this.build(e,n);return a.attachments=1,o.set(n,a),a}get conversationCount(){let e=0;for(let n of this.byCaller.values())e+=n.size;return e}evictOverflow(){for(;this.conversationCount>=Pe;){let e=this.byCaller.keys().next();if(e.done)throw new $;let n=e.value,o=this.byCaller.get(n),r=!1;for(let[a,s]of o)if(!(s.attachments>0)){o.delete(a),this.dropHandleIndexFor(n,a),s.backhaul.release().catch(()=>{}),r=!0;break}if(!r)throw o.size===0&&this.byCaller.delete(n),new $;o.size===0&&this.byCaller.delete(n)}}dropHandleIndexFor(e,n){for(let[o,r]of this.byHandle)r.apiKey===e&&r.conversationId===n&&this.byHandle.delete(o)}connectionFor(e){let n=null,o=!1,r=()=>(n===null&&(n=this.attachEntry(e,gt())),n),a={createResponse:(s,i)=>r().backhaul.clients.createResponse(s,i),catalogIdentity:s=>r().backhaul.clients.catalogIdentity(s),servingAppSlug:s=>{let i=r().backhaul.clients.servingAppSlug;if(i===void 0)throw new Error("the conversation backhaul wires no servingAppSlug seam \u2014 the caller-org app a request resolves to cannot be known");return i(s)},listModels:()=>r().backhaul.clients.listModels(),sessionHandle:s=>r().backhaul.clients.sessionHandle(s),createResponseOn:(s,i)=>r().backhaul.clients.createResponseOn(s,i),onSessionEnd:(s,i)=>r().backhaul.clients.onSessionEnd(s,i)};return a.openAudio=async(s,i)=>{let c=r().backhaul.clients.openAudio;if(c===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return c(s,i)},a.openVideo=async s=>{let i=r().backhaul.clients.openVideo;if(i===void 0)throw new Error("the conversation backhaul wires no openVideo seam \u2014 video-in is unavailable on this deployment");return i(s)},a.openVideoOut=async s=>{let i=r().backhaul.clients.openVideoOut;if(i===void 0)throw new Error("the conversation backhaul wires no openVideoOut seam \u2014 video-out is unavailable on this deployment");return i(s)},a.supportsAudio=async s=>{let i=r().backhaul.clients.supportsAudio;if(i===void 0)throw new Error("the conversation backhaul wires no supportsAudio seam \u2014 the realtime lane cannot gate audio modality on this deployment");return i(s)},a.supportsSpeech=async s=>{let i=r().backhaul.clients.supportsSpeech;if(i===void 0)throw new Error("the conversation backhaul wires no supportsSpeech seam \u2014 the speech lane cannot gate model capability on this deployment");return i(s)},a.speechVoices=async s=>{let i=r().backhaul.clients.speechVoices;if(i===void 0)throw new Error("the conversation backhaul wires no speechVoices seam \u2014 the speech lane cannot resolve the voice set /v1/models publishes on this deployment");return i(s)},a.speechSessionHandle=async s=>{let i=r().backhaul.clients.speechSessionHandle;if(i===void 0)throw new Error("the conversation backhaul wires no speechSessionHandle seam \u2014 the speech lane cannot resolve its own pooled session on this deployment");return i(s)},a.openAudioOn=async(s,i)=>{let c=r().backhaul.clients.openAudioOn;if(c===void 0)throw new Error("the conversation backhaul wires no openAudioOn seam \u2014 the speech and realtime lanes cannot open their pinned native session audio on this deployment");return c(s,i)},a.openTurnsOn=(s,i)=>{let c=r().backhaul.clients.openTurnsOn;if(c===void 0)throw new Error("the conversation backhaul wires no openTurnsOn seam \u2014 the realtime transcription lane cannot open the backend turn/transcript lane on this deployment");return c(s,i)},a.evictSpeechSession=async s=>{let i=r().backhaul.clients.evictSpeechSession;if(i===void 0)throw new Error("the conversation backhaul wires no evictSpeechSession seam \u2014 the speech lane cannot retire its pinned speech session after a failed turn on this deployment");return i(s)},a.supportsTranscription=async s=>{let i=r().backhaul.clients.supportsTranscription;if(i===void 0)throw new Error("the conversation backhaul wires no supportsTranscription seam \u2014 the transcription lane cannot gate model capability on this deployment");return i(s)},a.transcriptStreamFor=async s=>{let i=r().backhaul.clients.transcriptStreamFor;if(i===void 0)throw new Error("the conversation backhaul wires no transcriptStreamFor seam \u2014 the realtime transcription lane cannot verify the model's engine_args.transcript_stream on this deployment");return i(s)},a.listSpeechToTextModels=async()=>{let s=r().backhaul.clients.listSpeechToTextModels;if(s===void 0)throw new Error("the conversation backhaul wires no listSpeechToTextModels seam \u2014 the transcription lane cannot list the stt models on this deployment");return s()},{clients:a,bindResumeHandle:s=>{let i=n;if(i===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=yt;){let c=this.byHandle.keys().next();if(c.done)break;this.byHandle.delete(c.value)}this.byHandle.set(s,{apiKey:e.apiKey,conversationId:i.id})},clientsForResume:async s=>{let i=this.byHandle.get(s),c=i===void 0?void 0:this.byCaller.get(i.apiKey),u=i===void 0||c===void 0?void 0:c.get(i.conversationId);if(i===void 0||u===void 0||i.apiKey!==e.apiKey)throw new ne("the conversation behind this resume handle is gone (evicted at capacity, or the replica restarted) or the handle is not yours \u2014 reconnect without a handle to start a new conversation");return n=u,u.attachments+=1,u.backhaul.clients},release:async()=>{if(o)return;o=!0;let s=n;n=null,s!==null&&(s.attachments-=1,!(s.attachments>0)&&await s.backhaul.release().catch(()=>{}))}}}async detachAll(){let e=[];for(let o of this.byCaller.values())for(let r of o.values())e.push(r.backhaul.release());this.byCaller.clear(),this.byHandle.clear();let n=[];if(await Promise.all(e.map(async o=>{try{await o}catch(r){n.push(String(r instanceof Error?r.message:r))}})),n.length>0)throw new Error(`failed to detach ${n.length} conversation backhaul(s): ${n.join("; ")}`)}};p();var vt="/inference-ledger",_t=1e4,H=class extends Error{ledgerFailureReason;constructor(e,n="duplicate"){super(e),this.name="LedgerDuplicateError",this.ledgerFailureReason=n}},Y=class extends Error{constructor(n,o){super(o);this.ledgerAlreadyWritten=n;this.name="LedgerAlreadyWrittenError"}ledgerAlreadyWritten},f=class extends Error{ledgerFailureReason="surface";constructor(e){super(e),this.name="LedgerSurfaceError"}},Q=class extends g{ledgerFailureReason="auth";constructor(e){super(e),this.name="LedgerAuthError"}},Et=[["inference_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["model_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["variant",t=>t===null||typeof t=="string"],["task",t=>typeof t=="string"&&/[^\s]/.test(t)],["http_status",t=>typeof t=="number"&&Number.isInteger(t)],["started_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["completed_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["prompt_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["completion_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["output_units",V],["output_minutes",V],["cost_nano_usd",V],["price_version",t=>t===null||typeof t=="string"]];function St(t){for(let[e,n]of Et){if(!(e in t))return`${e} (absent)`;if(!n(t[e]))return`${e} (${JSON.stringify(t[e])})`}return null}function Rt(t){let e;try{e=JSON.parse(t)}catch{return{status:"unavailable"}}if(e===null||typeof e!="object"||Array.isArray(e))return{status:"unavailable"};let n=e.existing_status;if(n==="not_in_this_org")return{status:"not_in_this_org"};if(n!=="found")return{status:"unavailable"};let o=e.existing;return o===null||typeof o!="object"||Array.isArray(o)?{status:"unavailable"}:St(o)!==null?{status:"unavailable"}:{status:"found",row:o}}function V(t){return t===null?!0:typeof t=="number"?Number.isFinite(t):typeof t!="string"?!1:/[^\s]/.test(t)&&Number.isFinite(Number(t))}function q(t,e){return t===null?e===null:e===null?!1:Number(e)===t}function xt(t,e){return t===null?e===null:e===null?!1:Number(t.toFixed(9))===Number(e)}function Ne(t,e){if(typeof e!="string")return!1;let n=Date.parse(t),o=Date.parse(e);return Number.isFinite(n)&&Number.isFinite(o)&&n===o}function kt(t,e){let n=e;return n.inference_id===t.inference_id&&n.model_id===t.model_id&&(n.variant??null)===t.variant&&n.task===t.task&&n.http_status===t.http_status&&Ne(t.started_at,n.started_at)&&Ne(t.completed_at,n.completed_at)&&q(t.prompt_tokens,n.prompt_tokens)&&q(t.completion_tokens,n.completion_tokens)&&q(t.output_units,n.output_units)&&xt(t.output_minutes,n.output_minutes)}function Ct(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new f(`${e} answered a "recorded" that is not an object: ${JSON.stringify(t)}`);let n=t,o=n.inference_id;if(typeof o!="string"||o==="")throw new f(`${e} answered a row with no inference_id: ${JSON.stringify(t)}`);if(!("cost_nano_usd"in n))throw new f(`${e} answered a row with no cost_nano_usd field: ${JSON.stringify(t)}`);let r=n.cost_nano_usd;if(r!=null&&!(typeof r=="number"&&Number.isInteger(r)&&r>=0))throw new f(`${e} answered cost_nano_usd ${JSON.stringify(r)}, which is neither a non-negative integer nor null`);let a=u=>{if(!(u in n))throw new f(`${e} answered a row with no ${u} field: ${JSON.stringify(t)}`);let m=n[u];if(m===null)return null;if(typeof m!="string")throw new f(`${e} answered a non-string ${u}: ${JSON.stringify(m)}`);if(!/[^\s]/.test(m))throw new f(`${e} answered a BLANK ${u} (${JSON.stringify(m)}) \u2014 it satisfies "is a string" while naming nothing, which is exactly what inference_requests_priced_attribution_check exists to forbid`);return m},s=a("price_version"),i=a("unpriced_reason"),c=r!=null;if(c&&(s===null||i!==null))throw new f(`${e} answered a PRICED row (cost_nano_usd ${String(r)}) that is not cleanly attributed \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(i)}`);if(!c&&(s!==null||i===null))throw new f(`${e} answered an UNPRICED row that does not say why \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(i)}. NULL means NOT YET PRICED, and the reason is how an operator learns that traffic is being served which nobody can bill.`);return{inference_id:o,cost_nano_usd:r===void 0?null:r,price_version:s,unpriced_reason:i}}var M=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async write(e,n){let o=`${this.opts.apiUrl.replace(/\/+$/,"")}${vt}`,r;try{r=await this.fetchImpl(o,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify(n),signal:AbortSignal.timeout(_t)})}catch(c){throw new f(`the uRun ledger surface at ${o} could not be reached: ${c instanceof Error?c.message:String(c)}`)}if(r.status===409){let c=await r.text().catch(()=>""),u=Rt(c);throw u.status==="found"&&kt(n,u.row)?new Y(u.row,`the uRun control plane already holds THIS PROXY'S OWN row for inference_id ${n.inference_id} \u2014 every billable field matches the row being re-sent, so an earlier attempt landed and this retry is a no-op. Nothing was written and nothing was overwritten.`):u.status==="not_in_this_org"?new H(`inference_id ${n.inference_id} is ALREADY TAKEN by a ledger row this org cannot read. The id space is a global primary key, so this is not a retry and cannot be this proxy's own write \u2014 either two orgs collided on a uuid, or a row was written for this id by somebody else.`,"duplicate_foreign"):new H(`the uRun control plane already holds a ledger row for inference_id ${n.inference_id} that is NOT the one being written${u.status==="unavailable"?" (and it could not be read back to compare)":""} \u2014 nothing was written and nothing was overwritten. Either this proxy wrote twice, or a row was fabricated ahead of it (this write is authenticated by the CALLER'S OWN org key). ${c.slice(0,400)}`)}if(r.status===401||r.status===403)throw await r.text().catch(()=>{}),new Q("the uRun org API key was rejected by the uRun control plane when writing the inference ledger \u2014 the request was served but produced no billable row");if(!r.ok){let c=await r.text().catch(u=>`<body unreadable: ${u instanceof Error?u.message:String(u)}>`);throw new f(`the uRun ledger surface at ${o} answered HTTP ${r.status}: ${c.slice(0,400)}`)}let a;try{a=await r.json()}catch(c){throw new f(`the uRun ledger surface at ${o} answered a body that is not JSON: ${c instanceof Error?c.message:String(c)}`)}let s=a?.recorded;if(s===void 0)throw new f(`the uRun ledger surface at ${o} answered a body with no "recorded" field`);let i=Ct(s,`the uRun ledger surface at ${o}`);if(i.inference_id!==n.inference_id)throw new f(`the uRun ledger surface at ${o} answered for inference_id ${i.inference_id} when asked about ${n.inference_id}`);return i}};p();import{createServer as Tt}from"http";p();var D="/inference-usage",Ue=25e3;var Ot=new Set(["Error","TypeError","SyntaxError","TimeoutError"]);function $e(t){if(!(t instanceof Error))return`a ${typeof t}`;let e=Ot.has(t.name)?t.name:"Error",n=t.cause?.code;return typeof n=="string"&&/^[A-Z][A-Z0-9_]{0,31}$/.test(n)?`${e} (${n})`:e}var F=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async lookup(e,n){let o=`${this.opts.apiUrl.replace(/\/+$/,"")}${D}`,r;try{r=await this.fetchImpl(o,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify({inference_ids:n}),signal:AbortSignal.timeout(Ue)})}catch(s){throw new T(`the uRun usage surface (POST ${D}) could not be reached within ${Ue}ms: ${$e(s)}`)}if(r.status===401||r.status===403)throw await r.text().catch(()=>{}),new g("the uRun org API key was rejected by the uRun control plane \u2014 check the key and that it is not revoked");if(!r.ok){let s=await r.text().catch(()=>null),i=s===null?"body unreadable":`${s.length}-character body`;throw new T(`the uRun usage surface (POST ${D}) answered HTTP ${r.status} (${i})`)}let a;try{a=await r.json()}catch(s){throw new T(`the uRun usage surface (POST ${D}) answered HTTP ${r.status} with a body that is not JSON: ${$e(s)}`)}return fe(a,this.opts.onRejectedRow)}};var At=5e3,It=1e3,Pt=1500,Nt=3e3,Ut="/api/client-tokens";function X(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function Le(t,e,n,o){X(t,e,{error:{message:n,type:o}})}async function $t(t,e=fetch){let n=`${t.replace(/\/+$/,"")}${Ut}`;try{let o=await e(n,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(Nt)});return o.status===404?{ready:!1,detail:`${n} answered 404 \u2014 this is not a uRun control plane`}:o.status>=500?{ready:!1,detail:`${n} answered HTTP ${o.status}`}:{ready:!0,detail:`${n} answered HTTP ${o.status}`}}catch(o){return{ready:!1,detail:`${n} unreachable: ${o instanceof Error?o.message:String(o)}`}}}function Pn(t){let e=t.resolver??new N({gatewayUrl:t.baseUrl}),n=t.registry??new U({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),o=t.probeControlPlane??(()=>$t(t.baseUrl)),r=async()=>{let l=null;for(let h of n.decisionPools()){l=h;break}if(!l)return null;let d=await l.pingReady(Pt);return{ok:d,detail:d?"a decision slot answered its session ping":"no decision slot answered its session ping"}},a=t.usageClient??new F({apiUrl:t.apiUrl}),s=t.ledgerClient??new M({apiUrl:t.apiUrl}),i=process.env.URUN_SHARED_ORG_ID?.trim(),c=process.env.URUN_SHARED_ORG_API_KEY?.trim();i&&c&&(async()=>{try{await n.decisionWarmerFor({orgId:i,apiKey:c})?.warmFor(_e)}catch(l){process.stderr.write(`[decision-warm] out-of-band warm failed (best-effort, retrying on next demand): ${String(l instanceof Error?l.message:l)}
4
- `)}})();let u=new P(t.defaultMetrics===void 0?{}:{defaultMetrics:t.defaultMetrics}),m=async l=>e.resolve(k(l.headers.authorization)),Z=l=>l instanceof g?{status:l.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:l instanceof S?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,He=Ee({clients:async l=>n.clientsFor(await m(l)),usage:{authorize:l=>k(l.headers.authorization),lookup:(l,d)=>a.lookup(l,d)},statusOf:Z,...t.requestLog?{requestLog:t.requestLog}:{},observe:l=>u.observe(l),ledger:{write:(l,d)=>s.write(k(l.headers.authorization),d),observe:l=>u.observeLedger(l),...t.ledgerLog?{report:t.ledgerLog}:{}}}),_=null,E=null,R=Tt((l,d)=>{let h=new URL(l.url??"/","http://localhost");(async()=>{if(l.method==="GET"&&h.pathname==="/healthz"){X(d,200,{ok:!0});return}if(l.method==="GET"&&h.pathname==="/readyz"){let w=Date.now();if((!_||w-_.at>=At)&&(_={at:w,...await o()}),!E||w-E.at>=It){let B=await r();E=B===null?{at:w,ok:!0,detail:null}:{at:w,ok:B.ok,detail:B.detail}}let ee=(_?.ready??!1)&&E.ok,Fe=E.detail===null?_?.detail??"control plane not yet probed":`${_?.detail??"control plane not yet probed"}; ${E.detail}`;X(d,ee?200:503,{ok:ee,control_plane:Fe});return}let y=h.pathname==="/v1"||h.pathname.startsWith("/v1/"),C=h.pathname.startsWith("/partners/");if(!y&&!C){Le(d,404,`no route for ${l.method} ${h.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface, the partner adapter surface (/partners/...), the WS upgrade paths (/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent and /v1/realtime), plus /healthz and /readyz`,"invalid_request_error");return}He(l,d)})().catch(y=>{if(!d.headersSent){Le(d,500,String(y instanceof Error?y.message:y),"proxy_error");return}d.end()})}),G=t.conversations??new L({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),Me=[oe(R,{authenticate:async l=>{let d=await e.resolve(l),h=G.connectionFor(d);return{clients:h.clients,scope:J(d.apiKey),onBindResumeHandle:h.bindResumeHandle,clientsForResume:h.clientsForResume,release:h.release}}}),ie(R,{clients:async l=>n.clientsFor(await m(l)),statusOf:Z}).close,ce(R,{resolver:{resolve:async(l,d)=>{let h=await e.resolve(k(l.headers.authorization)),y=G.connectionFor(h),C;try{C=await he(y.clients,ue(l),d,de(l))}catch(w){throw y.release().catch(()=>{}),w}return{...C,release:y.release}}}})];R.on("upgrade",(l,d)=>{let h=new URL(l.url??"/","http://localhost");h.pathname===re||h.pathname===se||h.pathname===le||d.destroyed||(d.write(`HTTP/1.1 404 Not Found\r
3
+ `);return}n.end()})})}f();import{createHash as Ae}from"crypto";import{createServer as Qe}from"http";var Jt=9465,Xe="x-urun-key-id",Ze="x-urun-key-fingerprint",et="urun-ratelimit-bucket:",tt="urun-ratelimit-tier:",Te="anonymous",nt=/^urun_sk_[A-Za-z0-9_-]{43}$/;function rt(t){return Ae("sha256").update(`${et}${t}`).digest("hex")}function ot(t){return Ae("sha256").update(`${tt}${t}`).digest("hex")}function st(t){let e=/^Bearer[ \t]+(\S.*)$/i.exec((t??"").trim());if(!e)return null;let n=e[1].trim();return nt.test(n)?n:null}function Ie(){return Qe((t,e)=>{let n=st(t.headers.authorization);e.setHeader(Xe,n===null?Te:rt(n)),e.setHeader(Ze,n===null?Te:ot(n)),e.writeHead(200),e.end()})}f();import{createClientToken as it,ClientTokenError as at}from"@urun-sh/core";var m=class extends Error{constructor(n,o=401){super(n);this.status=o;this.name="ProxyAuthError"}status},S=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function k(t){let e=(t??"").trim();if(e==="")throw new m("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let n=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!n)throw new m("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return n[1].trim()}var lt=60,ct=6e4,ut=4096;function dt(t){return t===401||t===403}var N=class{constructor(e){this.opts=e;this.mint=e.mint??it,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new m("empty Authorization bearer token");let n=this.cache.get(e);if(n&&this.now()-n.at<ct)return{apiKey:e,orgId:n.orgId};let o;try{o=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:lt})).orgId}catch(r){throw r instanceof at&&dt(r.status)?(this.cache.delete(e),new m("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new S(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${r instanceof Error?r.message:String(r)}`)}if(!o)throw new S("the control plane verified the API key but returned no org_id \u2014 refusing to route a request with no tenancy (control-plane contract violation)");if(this.cache.size>=ut){let r=this.cache.keys().next();r.done||this.cache.delete(r.value)}return this.cache.set(e,{orgId:o,at:this.now()}),{apiKey:e,orgId:o}}clear(){this.cache.clear()}};f();var ht=8080,j="serve",en="0.0.0.0";function pt(t,e,n){let o=(t[e]??"").trim();if(!o)throw new Error(`${e} is not set \u2014 ${n}`);return o}function tn(t=process.env){let e=(t.PORT??"").trim(),n=e===""?ht:Number(e);if(!Number.isInteger(n)||n<0||n>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let o=ae(O(pt(t,"URUN_BASE_URL","the hosted proxy needs the uRun session-gateway base it opens sessions and verifies API keys against (e.g. https://api.urun.sh)"),"URUN_BASE_URL")),r=(t.URUN_API_URL??"").trim(),a=r?O(r,"URUN_API_URL"):`${o.replace(/\/+$/,"")}/v1`,s=(t.URUN_CATALOG_URL??"").trim(),i=(t.URUN_CATALOG_ANON_KEY??"").trim();if(!!s!=!!i)throw new Error("URUN_CATALOG_URL and URUN_CATALOG_ANON_KEY must be set together (or both left unset) \u2014 exactly one of them is set, which would silently disable the model-catalog oracle");let c=s?{catalogUrl:O(s,"URUN_CATALOG_URL"),anonKey:i}:null;return{port:n,baseUrl:o,apiUrl:a,catalog:c}}f();import{createHash as ft,randomUUID as gt}from"crypto";var mt=512;function J(t){return`urun-inference-proxy-${ft("sha256").update(t).digest("hex").slice(0,32)}`}var U=class{constructor(e){this.opts=e}opts;tenants=new Map;label(e,n){return Object.assign(Object.create(e),{tenant:n.orgId})}build(e){if(this.opts.build){let a=this.opts.build(e);return a.decision&&this.ensureDecisionReaper(),{router:a.router,clients:this.label(a.clients,e),decision:a.decision}}let n={baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:j,sharedOnlyListing:!0,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,release:"end",decisionPool:Se(process.env,process.env.HOSTNAME)??void 0},o=z(n),r=n.decisionPool?Re(n,o):null;return r&&this.ensureDecisionReaper(),{router:o,clients:this.label(W(o,void 0,void 0,r??void 0),e),decision:r??void 0}}ensureDecisionReaper(){if(this.decisionReaper!==null)return;let e=setInterval(()=>{for(let n of this.tenants.values())n.decision?.reap()},5e3);e.unref(),this.decisionReaper=e}decisionReaper=null;clientsFor(e){let n=this.tenants.get(e.apiKey);if(n)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,n),n.clients;for(;this.tenants.size>=mt;){let r=this.tenants.entries().next();if(r.done)break;let[a,s]=r.value;this.tenants.delete(a),s.router.closeAll().catch(()=>{})}let o=this.build(e);return this.tenants.set(e.apiKey,o),o.clients}get size(){return this.tenants.size}*decisionPools(){for(let e of this.tenants.values())e.decision&&(yield e.decision)}decisionWarmerFor(e){return this.clientsFor(e),this.tenants.get(e.apiKey)?.decision??null}async closeAll(){this.decisionReaper!==null&&(clearInterval(this.decisionReaper),this.decisionReaper=null);let e=[...this.tenants.values()];this.tenants.clear();let n=[];if(await Promise.all(e.map(async o=>{let r=[o.router.closeAll().catch(a=>{n.push(String(a instanceof Error?a.message:a))})];o.decision&&r.push(o.decision.close().catch(a=>{n.push(String(a instanceof Error?a.message:a))})),await Promise.all(r)})),n.length>0)throw new Error(`failed to close ${n.length} tenant backhaul(s): ${n.join("; ")}`)}},Pe=512,yt=1024,$=class extends Error{constructor(){super(`conversation capacity reached (${Pe} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function wt(t){return{sessionStartOptions:e=>{if(e.startsWith(K))return;let n=t.get(e);return n===void 0?void 0:{sessionId:n}},onSessionOpen:(e,n)=>{e.startsWith(K)||t.set(e,n)}}}function bt(t,e,n,o){let r=z({baseUrl:t.baseUrl,orgId:e.orgId,defaultApp:null,fnName:j,sharedOnlyListing:!0,apiUrl:t.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:t.baseUrl},catalog:t.catalog,subject:n.subject,release:"detach",...wt(o)});return{clients:W(r),release:async()=>{await r.closeAll()}}}var L=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,n){let o={id:n,subject:`${J(e.apiKey)}:live:${n}`},r=new Map,a=this.opts.build?this.opts.build(e,o):bt(this.opts,e,o,r);return{id:n,backhaul:a,attachments:0,nativeRefs:r}}attachEntry(e,n){let o=this.byCaller.get(e.apiKey);o===void 0&&(o=new Map,this.byCaller.set(e.apiKey,o));let r=o.get(n);if(r!==void 0)return o.delete(n),o.set(n,r),r.attachments+=1,r;this.evictOverflow();let a=this.build(e,n);return a.attachments=1,o.set(n,a),a}get conversationCount(){let e=0;for(let n of this.byCaller.values())e+=n.size;return e}evictOverflow(){for(;this.conversationCount>=Pe;){let e=this.byCaller.keys().next();if(e.done)throw new $;let n=e.value,o=this.byCaller.get(n),r=!1;for(let[a,s]of o)if(!(s.attachments>0)){o.delete(a),this.dropHandleIndexFor(n,a),s.backhaul.release().catch(()=>{}),r=!0;break}if(!r)throw o.size===0&&this.byCaller.delete(n),new $;o.size===0&&this.byCaller.delete(n)}}dropHandleIndexFor(e,n){for(let[o,r]of this.byHandle)r.apiKey===e&&r.conversationId===n&&this.byHandle.delete(o)}connectionFor(e){let n=null,o=!1,r=()=>(n===null&&(n=this.attachEntry(e,gt())),n),a={createResponse:(s,i)=>r().backhaul.clients.createResponse(s,i),catalogIdentity:s=>r().backhaul.clients.catalogIdentity(s),servingAppSlug:s=>{let i=r().backhaul.clients.servingAppSlug;if(i===void 0)throw new Error("the conversation backhaul wires no servingAppSlug seam \u2014 the caller-org app a request resolves to cannot be known");return i(s)},listModels:()=>r().backhaul.clients.listModels(),sessionHandle:s=>r().backhaul.clients.sessionHandle(s),createResponseOn:(s,i)=>r().backhaul.clients.createResponseOn(s,i),onSessionEnd:(s,i)=>r().backhaul.clients.onSessionEnd(s,i)};return a.openAudio=async(s,i)=>{let c=r().backhaul.clients.openAudio;if(c===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return c(s,i)},a.openVideo=async s=>{let i=r().backhaul.clients.openVideo;if(i===void 0)throw new Error("the conversation backhaul wires no openVideo seam \u2014 video-in is unavailable on this deployment");return i(s)},a.openVideoOut=async s=>{let i=r().backhaul.clients.openVideoOut;if(i===void 0)throw new Error("the conversation backhaul wires no openVideoOut seam \u2014 video-out is unavailable on this deployment");return i(s)},a.supportsAudio=async s=>{let i=r().backhaul.clients.supportsAudio;if(i===void 0)throw new Error("the conversation backhaul wires no supportsAudio seam \u2014 the realtime lane cannot gate audio modality on this deployment");return i(s)},a.supportsSpeech=async s=>{let i=r().backhaul.clients.supportsSpeech;if(i===void 0)throw new Error("the conversation backhaul wires no supportsSpeech seam \u2014 the speech lane cannot gate model capability on this deployment");return i(s)},a.speechVoices=async s=>{let i=r().backhaul.clients.speechVoices;if(i===void 0)throw new Error("the conversation backhaul wires no speechVoices seam \u2014 the speech lane cannot resolve the voice set /v1/models publishes on this deployment");return i(s)},a.speechSessionHandle=async s=>{let i=r().backhaul.clients.speechSessionHandle;if(i===void 0)throw new Error("the conversation backhaul wires no speechSessionHandle seam \u2014 the speech lane cannot resolve its own pooled session on this deployment");return i(s)},a.openAudioOn=async(s,i)=>{let c=r().backhaul.clients.openAudioOn;if(c===void 0)throw new Error("the conversation backhaul wires no openAudioOn seam \u2014 the speech and realtime lanes cannot open their pinned native session audio on this deployment");return c(s,i)},a.openTurnsOn=(s,i)=>{let c=r().backhaul.clients.openTurnsOn;if(c===void 0)throw new Error("the conversation backhaul wires no openTurnsOn seam \u2014 the realtime transcription lane cannot open the backend turn/transcript lane on this deployment");return c(s,i)},a.evictSpeechSession=async s=>{let i=r().backhaul.clients.evictSpeechSession;if(i===void 0)throw new Error("the conversation backhaul wires no evictSpeechSession seam \u2014 the speech lane cannot retire its pinned speech session after a failed turn on this deployment");return i(s)},a.supportsTranscription=async s=>{let i=r().backhaul.clients.supportsTranscription;if(i===void 0)throw new Error("the conversation backhaul wires no supportsTranscription seam \u2014 the transcription lane cannot gate model capability on this deployment");return i(s)},a.transcriptStreamFor=async s=>{let i=r().backhaul.clients.transcriptStreamFor;if(i===void 0)throw new Error("the conversation backhaul wires no transcriptStreamFor seam \u2014 the realtime transcription lane cannot verify the model's engine_args.transcript_stream on this deployment");return i(s)},a.listSpeechToTextModels=async()=>{let s=r().backhaul.clients.listSpeechToTextModels;if(s===void 0)throw new Error("the conversation backhaul wires no listSpeechToTextModels seam \u2014 the transcription lane cannot list the stt models on this deployment");return s()},{clients:a,bindResumeHandle:s=>{let i=n;if(i===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=yt;){let c=this.byHandle.keys().next();if(c.done)break;this.byHandle.delete(c.value)}this.byHandle.set(s,{apiKey:e.apiKey,conversationId:i.id})},clientsForResume:async s=>{let i=this.byHandle.get(s),c=i===void 0?void 0:this.byCaller.get(i.apiKey),u=i===void 0||c===void 0?void 0:c.get(i.conversationId);if(i===void 0||u===void 0||i.apiKey!==e.apiKey)throw new ne("the conversation behind this resume handle is gone (evicted at capacity, or the replica restarted) or the handle is not yours \u2014 reconnect without a handle to start a new conversation");return n=u,u.attachments+=1,u.backhaul.clients},release:async()=>{if(o)return;o=!0;let s=n;n=null,s!==null&&(s.attachments-=1,!(s.attachments>0)&&await s.backhaul.release().catch(()=>{}))}}}async detachAll(){let e=[];for(let o of this.byCaller.values())for(let r of o.values())e.push(r.backhaul.release());this.byCaller.clear(),this.byHandle.clear();let n=[];if(await Promise.all(e.map(async o=>{try{await o}catch(r){n.push(String(r instanceof Error?r.message:r))}})),n.length>0)throw new Error(`failed to detach ${n.length} conversation backhaul(s): ${n.join("; ")}`)}};f();var vt="/inference-ledger",_t=1e4,H=class extends Error{ledgerFailureReason;constructor(e,n="duplicate"){super(e),this.name="LedgerDuplicateError",this.ledgerFailureReason=n}},Y=class extends Error{constructor(n,o){super(o);this.ledgerAlreadyWritten=n;this.name="LedgerAlreadyWrittenError"}ledgerAlreadyWritten},g=class extends Error{ledgerFailureReason="surface";constructor(e){super(e),this.name="LedgerSurfaceError"}},Q=class extends m{ledgerFailureReason="auth";constructor(e){super(e),this.name="LedgerAuthError"}},Et=[["inference_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["model_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["variant",t=>t===null||typeof t=="string"],["task",t=>typeof t=="string"&&/[^\s]/.test(t)],["http_status",t=>typeof t=="number"&&Number.isInteger(t)],["started_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["completed_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["prompt_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["completion_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["output_units",V],["output_minutes",V],["cost_nano_usd",V],["price_version",t=>t===null||typeof t=="string"]];function St(t){for(let[e,n]of Et){if(!(e in t))return`${e} (absent)`;if(!n(t[e]))return`${e} (${JSON.stringify(t[e])})`}return null}function Rt(t){let e;try{e=JSON.parse(t)}catch{return{status:"unavailable"}}if(e===null||typeof e!="object"||Array.isArray(e))return{status:"unavailable"};let n=e.existing_status;if(n==="not_in_this_org")return{status:"not_in_this_org"};if(n!=="found")return{status:"unavailable"};let o=e.existing;return o===null||typeof o!="object"||Array.isArray(o)?{status:"unavailable"}:St(o)!==null?{status:"unavailable"}:{status:"found",row:o}}function V(t){return t===null?!0:typeof t=="number"?Number.isFinite(t):typeof t!="string"?!1:/[^\s]/.test(t)&&Number.isFinite(Number(t))}function q(t,e){return t===null?e===null:e===null?!1:Number(e)===t}function xt(t,e){return t===null?e===null:e===null?!1:Number(t.toFixed(9))===Number(e)}function Ne(t,e){if(typeof e!="string")return!1;let n=Date.parse(t),o=Date.parse(e);return Number.isFinite(n)&&Number.isFinite(o)&&n===o}function kt(t,e){let n=e;return n.inference_id===t.inference_id&&n.model_id===t.model_id&&(n.variant??null)===t.variant&&n.task===t.task&&n.http_status===t.http_status&&Ne(t.started_at,n.started_at)&&Ne(t.completed_at,n.completed_at)&&q(t.prompt_tokens,n.prompt_tokens)&&q(t.completion_tokens,n.completion_tokens)&&q(t.output_units,n.output_units)&&xt(t.output_minutes,n.output_minutes)}function Ct(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new g(`${e} answered a "recorded" that is not an object: ${JSON.stringify(t)}`);let n=t,o=n.inference_id;if(typeof o!="string"||o==="")throw new g(`${e} answered a row with no inference_id: ${JSON.stringify(t)}`);if(!("cost_nano_usd"in n))throw new g(`${e} answered a row with no cost_nano_usd field: ${JSON.stringify(t)}`);let r=n.cost_nano_usd;if(r!=null&&!(typeof r=="number"&&Number.isInteger(r)&&r>=0))throw new g(`${e} answered cost_nano_usd ${JSON.stringify(r)}, which is neither a non-negative integer nor null`);let a=u=>{if(!(u in n))throw new g(`${e} answered a row with no ${u} field: ${JSON.stringify(t)}`);let y=n[u];if(y===null)return null;if(typeof y!="string")throw new g(`${e} answered a non-string ${u}: ${JSON.stringify(y)}`);if(!/[^\s]/.test(y))throw new g(`${e} answered a BLANK ${u} (${JSON.stringify(y)}) \u2014 it satisfies "is a string" while naming nothing, which is exactly what inference_requests_priced_attribution_check exists to forbid`);return y},s=a("price_version"),i=a("unpriced_reason"),c=r!=null;if(c&&(s===null||i!==null))throw new g(`${e} answered a PRICED row (cost_nano_usd ${String(r)}) that is not cleanly attributed \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(i)}`);if(!c&&(s!==null||i===null))throw new g(`${e} answered an UNPRICED row that does not say why \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(i)}. NULL means NOT YET PRICED, and the reason is how an operator learns that traffic is being served which nobody can bill.`);return{inference_id:o,cost_nano_usd:r===void 0?null:r,price_version:s,unpriced_reason:i}}var M=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async write(e,n){let o=`${this.opts.apiUrl.replace(/\/+$/,"")}${vt}`,r;try{r=await this.fetchImpl(o,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify(n),signal:AbortSignal.timeout(_t)})}catch(c){throw new g(`the uRun ledger surface at ${o} could not be reached: ${c instanceof Error?c.message:String(c)}`)}if(r.status===409){let c=await r.text().catch(()=>""),u=Rt(c);throw u.status==="found"&&kt(n,u.row)?new Y(u.row,`the uRun control plane already holds THIS PROXY'S OWN row for inference_id ${n.inference_id} \u2014 every billable field matches the row being re-sent, so an earlier attempt landed and this retry is a no-op. Nothing was written and nothing was overwritten.`):u.status==="not_in_this_org"?new H(`inference_id ${n.inference_id} is ALREADY TAKEN by a ledger row this org cannot read. The id space is a global primary key, so this is not a retry and cannot be this proxy's own write \u2014 either two orgs collided on a uuid, or a row was written for this id by somebody else.`,"duplicate_foreign"):new H(`the uRun control plane already holds a ledger row for inference_id ${n.inference_id} that is NOT the one being written${u.status==="unavailable"?" (and it could not be read back to compare)":""} \u2014 nothing was written and nothing was overwritten. Either this proxy wrote twice, or a row was fabricated ahead of it (this write is authenticated by the CALLER'S OWN org key). ${c.slice(0,400)}`)}if(r.status===401||r.status===403)throw await r.text().catch(()=>{}),new Q("the uRun org API key was rejected by the uRun control plane when writing the inference ledger \u2014 the request was served but produced no billable row");if(!r.ok){let c=await r.text().catch(u=>`<body unreadable: ${u instanceof Error?u.message:String(u)}>`);throw new g(`the uRun ledger surface at ${o} answered HTTP ${r.status}: ${c.slice(0,400)}`)}let a;try{a=await r.json()}catch(c){throw new g(`the uRun ledger surface at ${o} answered a body that is not JSON: ${c instanceof Error?c.message:String(c)}`)}let s=a?.recorded;if(s===void 0)throw new g(`the uRun ledger surface at ${o} answered a body with no "recorded" field`);let i=Ct(s,`the uRun ledger surface at ${o}`);if(i.inference_id!==n.inference_id)throw new g(`the uRun ledger surface at ${o} answered for inference_id ${i.inference_id} when asked about ${n.inference_id}`);return i}};f();import{createServer as Tt}from"http";f();var D="/inference-usage",Ue=25e3;var Ot=new Set(["Error","TypeError","SyntaxError","TimeoutError"]);function $e(t){if(!(t instanceof Error))return`a ${typeof t}`;let e=Ot.has(t.name)?t.name:"Error",n=t.cause?.code;return typeof n=="string"&&/^[A-Z][A-Z0-9_]{0,31}$/.test(n)?`${e} (${n})`:e}var F=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async lookup(e,n){let o=`${this.opts.apiUrl.replace(/\/+$/,"")}${D}`,r;try{r=await this.fetchImpl(o,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify({inference_ids:n}),signal:AbortSignal.timeout(Ue)})}catch(s){throw new T(`the uRun usage surface (POST ${D}) could not be reached within ${Ue}ms: ${$e(s)}`)}if(r.status===401||r.status===403)throw await r.text().catch(()=>{}),new m("the uRun org API key was rejected by the uRun control plane \u2014 check the key and that it is not revoked");if(!r.ok){let s=await r.text().catch(()=>null),i=s===null?"body unreadable":`${s.length}-character body`;throw new T(`the uRun usage surface (POST ${D}) answered HTTP ${r.status} (${i})`)}let a;try{a=await r.json()}catch(s){throw new T(`the uRun usage surface (POST ${D}) answered HTTP ${r.status} with a body that is not JSON: ${$e(s)}`)}return fe(a,this.opts.onRejectedRow)}};var At=5e3,It=1e3,Pt=1500,Nt=3e3,Ut="/api/client-tokens";function X(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function Le(t,e,n,o){X(t,e,{error:{message:n,type:o}})}async function $t(t,e=fetch){let n=`${t.replace(/\/+$/,"")}${Ut}`;try{let o=await e(n,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(Nt)});return o.status===404?{ready:!1,detail:`${n} answered 404 \u2014 this is not a uRun control plane`}:o.status>=500?{ready:!1,detail:`${n} answered HTTP ${o.status}`}:{ready:!0,detail:`${n} answered HTTP ${o.status}`}}catch(o){return{ready:!1,detail:`${n} unreachable: ${o instanceof Error?o.message:String(o)}`}}}function Pn(t){let e=t.resolver??new N({gatewayUrl:t.baseUrl}),n=t.registry??new U({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),o=t.probeControlPlane??(()=>$t(t.baseUrl)),r=async()=>{let l=null;for(let p of n.decisionPools()){l=p;break}if(!l)return null;let d=l.stats();if(d.some(p=>p.draining))return{ok:!1,detail:"decision pool draining"};if(d.every(p=>p.attached===0&&p.inFlight===0))return{ok:!0,detail:"no active decision slot; gateway accepts on-demand resume"};let h=await l.pingReady(Pt);return{ok:h,detail:h?"a decision slot answered its session ping":"no decision slot answered its session ping"}},a=t.usageClient??new F({apiUrl:t.apiUrl}),s=t.ledgerClient??new M({apiUrl:t.apiUrl}),i=process.env.URUN_SHARED_ORG_ID?.trim(),c=process.env.URUN_SHARED_ORG_API_KEY?.trim();i&&c&&(async()=>{try{await n.decisionWarmerFor({orgId:i,apiKey:c})?.warmFor(_e)}catch(l){process.stderr.write(`[decision-warm] out-of-band warm failed (best-effort, retrying on next demand): ${String(l instanceof Error?l.message:l)}
4
+ `)}})();let u=new P(t.defaultMetrics===void 0?{}:{defaultMetrics:t.defaultMetrics}),y=async l=>e.resolve(k(l.headers.authorization)),Z=l=>l instanceof m?{status:l.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:l instanceof S?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,He=Ee({clients:async l=>n.clientsFor(await y(l)),usage:{authorize:l=>k(l.headers.authorization),lookup:(l,d)=>a.lookup(l,d)},statusOf:Z,...t.requestLog?{requestLog:t.requestLog}:{},observe:l=>u.observe(l),ledger:{write:(l,d)=>s.write(k(l.headers.authorization),d),observe:l=>u.observeLedger(l),...t.ledgerLog?{report:t.ledgerLog}:{}}}),_=null,E=null,R=Tt((l,d)=>{let h=new URL(l.url??"/","http://localhost");(async()=>{if(l.method==="GET"&&h.pathname==="/healthz"){X(d,200,{ok:!0});return}if(l.method==="GET"&&h.pathname==="/readyz"){let w=Date.now();if((!_||w-_.at>=At)&&(_={at:w,...await o()}),!E||w-E.at>=It){let B=await r();E=B===null?{at:w,ok:!0,detail:null}:{at:w,ok:B.ok,detail:B.detail}}let ee=(_?.ready??!1)&&E.ok,Fe=E.detail===null?_?.detail??"control plane not yet probed":`${_?.detail??"control plane not yet probed"}; ${E.detail}`;X(d,ee?200:503,{ok:ee,control_plane:Fe});return}let p=h.pathname==="/v1"||h.pathname.startsWith("/v1/"),C=h.pathname.startsWith("/partners/");if(!p&&!C){Le(d,404,`no route for ${l.method} ${h.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface, the partner adapter surface (/partners/...), the WS upgrade paths (/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent and /v1/realtime), plus /healthz and /readyz`,"invalid_request_error");return}He(l,d)})().catch(p=>{if(!d.headersSent){Le(d,500,String(p instanceof Error?p.message:p),"proxy_error");return}d.end()})}),G=t.conversations??new L({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),Me=[oe(R,{authenticate:async l=>{let d=await e.resolve(l),h=G.connectionFor(d);return{clients:h.clients,scope:J(d.apiKey),onBindResumeHandle:h.bindResumeHandle,clientsForResume:h.clientsForResume,release:h.release}}}),ie(R,{clients:async l=>n.clientsFor(await y(l)),statusOf:Z}).close,ce(R,{resolver:{resolve:async(l,d)=>{let h=await e.resolve(k(l.headers.authorization)),p=G.connectionFor(h),C;try{C=await he(p.clients,ue(l),d,de(l))}catch(w){throw p.release().catch(()=>{}),w}return{...C,release:p.release}}}})];R.on("upgrade",(l,d)=>{let h=new URL(l.url??"/","http://localhost");h.pathname===re||h.pathname===se||h.pathname===le||d.destroyed||(d.write(`HTTP/1.1 404 Not Found\r
5
5
  Connection: close\r
6
6
  \r
7
- no WS route for ${h.pathname} \u2014 this endpoint serves only the Gemini Live, /v1/responses, and /v1/realtime upgrade paths`),d.destroy())});let De=async()=>{for(let l of Me)await l();await G.detachAll()};return{server:R,metricsServer:Oe(u),metrics:u,edgeIdentityServer:Ie(),registry:n,closeAll:()=>n.closeAll(),closeWebSockets:De}}async function Nn(t,e,n){n&&await n();let o=new Promise(r=>t.close(()=>r()));t.closeAllConnections(),await o,await be()||ve(),await e()}export{Bt as a,Ce as b,A as c,ze as d,We as e,je as f,qe as g,Ye as h,I as i,P as j,Oe as k,Jt as l,Xe as m,Ze as n,et as o,tt as p,Te as q,rt as r,ot as s,st as t,Ie as u,g as v,S as w,k as x,ct as y,ut as z,N as A,ht as B,j as C,en as D,tn as E,mt as F,J as G,U as H,Pe as I,yt as J,$ as K,L,vt as M,_t as N,H as O,f as P,Ct as Q,M as R,At as S,Nt as T,Pn as U,Nn as V};
7
+ no WS route for ${h.pathname} \u2014 this endpoint serves only the Gemini Live, /v1/responses, and /v1/realtime upgrade paths`),d.destroy())});let De=async()=>{for(let l of Me)await l();await G.detachAll()};return{server:R,metricsServer:Oe(u),metrics:u,edgeIdentityServer:Ie(),registry:n,closeAll:()=>n.closeAll(),closeWebSockets:De}}async function Nn(t,e,n){n&&await n();let o=new Promise(r=>t.close(()=>r()));t.closeAllConnections(),await o,await be()||ve(),await e()}export{Bt as a,Ce as b,A as c,ze as d,We as e,je as f,qe as g,Ye as h,I as i,P as j,Oe as k,Jt as l,Xe as m,Ze as n,et as o,tt as p,Te as q,rt as r,ot as s,st as t,Ie as u,m as v,S as w,k as x,ct as y,ut as z,N as A,ht as B,j as C,en as D,tn as E,mt as F,J as G,U as H,Pe as I,yt as J,$ as K,L,vt as M,_t as N,H as O,g as P,Ct as Q,M as R,At as S,Nt as T,Pn as U,Nn as V};