@urun-sh/openai 0.6.0 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- import{e as r}from"./chunk-QTVTCMJU.js";import{c as e}from"./chunk-YSFSRI3D.js";e();var s=0;function a(){return s+=1,`req_${Date.now().toString(36)}_${s.toString(36)}`}var o=class{transport;constructor(n){this.transport=new r(n)}responses={create:async n=>{let t=a(),i=await this.transport.sendResponses(n,t);return Object.assign((async function*(){yield*i})(),{requestId:t})}}};export{o as a};
1
+ import{e as r}from"./chunk-7V2JKPIG.js";import{c as e}from"./chunk-YSFSRI3D.js";e();var s=0;function a(){return s+=1,`req_${Date.now().toString(36)}_${s.toString(36)}`}var o=class{transport;constructor(n){this.transport=new r(n)}responses={create:async n=>{let t=a(),i=await this.transport.sendResponses(n,t);return Object.assign((async function*(){yield*i})(),{requestId:t})}}};export{o as a};
@@ -0,0 +1 @@
1
+ import{c as a}from"./chunk-YSFSRI3D.js";a();function _(e,t,o,n){let r=e.response??{},i={request_id:o,consumer_id:n,stream:!0,kind:"chat",messages:t};return typeof r.instructions=="string"&&(i.instructions=r.instructions),Array.isArray(r.modalities)&&(i.modalities=r.modalities),typeof r.temperature=="number"&&(i.temperature=r.temperature),typeof r.max_output_tokens=="number"&&(i.max_output_tokens=r.max_output_tokens),r.tools!==void 0&&(i.tools=r.tools),i}function f(e,t,o){let n={request_id:t,consumer_id:o,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(n.model=e.model),e.tools!==void 0&&(n.tools=e.tools),e.tool_choice!==void 0&&(n.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(n.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(n.max_output_tokens=e.max_output_tokens),typeof e.top_p=="number"&&(n.top_p=e.top_p),Array.isArray(e.stop)&&e.stop.length>0&&(n.stop=e.stop),typeof e.reasoning_effort=="string"&&(n.reasoning_effort=e.reasoning_effort),typeof e.instructions=="string"&&(n.instructions=e.instructions),e.chat_template_kwargs!==void 0&&(n.chat_template_kwargs=e.chat_template_kwargs),e.response_format!==void 0&&(n.response_format=e.response_format),typeof e.voice=="string"&&(n.voice=e.voice),typeof e.language=="string"&&(n.language=e.language),n}a();function y(e){if(e instanceof Error)return{type:"error",error:{type:"urun_error",code:e.name||null,message:e.message}};if(e&&typeof e=="object"&&e.t==="error"){let t=e,o=t.body??{},n=o.error,r=n?.message??o.message??`unreadable serve error body: ${JSON.stringify(t.body??null)}`;return{type:"error",error:{type:n?.type??"urun_error",code:t.code??n?.code??null,message:r}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}a();a();var w="llm-resp";async function*m(e,t){let o=`${w}:${t}`,n=`resp_${t}`;yield{type:"response.created",response:{id:n,status:"in_progress"}};let r=new Map;try{for await(let i of e.stream(o).messages()){let s=i;if(s.t==="delta"){if(typeof s.delta=="string"&&(yield{type:"response.output_text.delta",item_id:n,delta:s.delta}),typeof s.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:n,delta:s.reasoning}),Array.isArray(s.tool_calls))for(let u of s.tool_calls){let d=typeof u.index=="number"?u.index:0,c=r.get(d)??{};u.id&&(c.id=u.id),u.function?.name&&(c.name=u.function.name),r.set(d,c),yield{type:"response.function_call_arguments.delta",item_id:`fc_${t}_${d}`,tool_index:d,call_id:c.id,name:c.name,delta:u.function?.arguments??""}}}else if(s.t==="response"){yield{type:"response.completed",response:{id:n,status:"completed",...s.body}};return}else if(s.t==="error"){yield y(s);return}}}finally{try{e.doc("llm").set({requests:{[t]:null}})}catch{}}}a();var k=class e{constructor(t,o=e.DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS){this.session=t;this.docSyncTimeoutMs=o;this.sessionTag=t.sessionId??globalThis.crypto.randomUUID()}session;docSyncTimeoutMs;sessionTag;static DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS=15e3;get consumerId(){return this.session.consumerId}async ensureDocSynced(){let t=this.session.doc("llm");if(t.synced===!0||typeof t.onSynced!="function")return;let o;await new Promise((r,i)=>{let s=t.onSynced(()=>{clearTimeout(o),r()});o=setTimeout(()=>{s(),i(new Error(`[urun] llm doc plane never synced within ${this.docSyncTimeoutMs}ms \u2014 the request envelope write is withheld rather than raced into a possibly-lost pre-sync write (resumed-attach first-write-lost, prod-usw2 2026-09-23)`))},this.docSyncTimeoutMs)})}write(t){t.session_tag=this.sessionTag,this.session.doc("llm").set({requests:{[t.request_id]:{payload:t,consumer_id:t.consumer_id,stream:t.stream}}})}sendResponses(t,o){return this.writeGated(o,()=>{let n=f(t,o,this.consumerId);return this.write(n),m(this.session,o)})}sendResponseCreate(t,o,n){return this.writeGated(n,()=>{let r=_(t,o,n,this.consumerId);return this.write(r),m(this.session,n)})}async writeGated(t,o){try{await this.ensureDocSynced()}catch(n){throw new Error(`[urun] responses dial ${t} aborted before its envelope write: ${n instanceof Error?n.message:String(n)}`)}return o()}};export{_ as a,f as b,y as c,m as d,k as e};
@@ -0,0 +1,7 @@
1
+ import{a as j,b as W}from"./chunk-X3GLH2HQ.js";import{C as re,D as oe,E as se,F as ie,H as ae,I as A,J as le,K as ce,L as ue,M as de,N as he,O as pe,P as ge,Q as fe,R as me,b as Z,d as ee,e as te,q as ne,r as O}from"./chunk-URMOBMSP.js";import{e as Q,g as z}from"./chunk-TMQJY56F.js";import{a as X}from"./chunk-3JWYIKHM.js";import{c as p}from"./chunk-YSFSRI3D.js";p();import{Counter as S,Gauge as ye,Histogram as we,Registry as Ue,collectDefaultMetrics as Pe}from"@prometheus-io/client";import{createServer as $e}from"http";var It=9464,be="/metrics",I="(other)",Le="(none)",He=64,Me=128,De=["/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",ae,ce],Fe=new Set(De);function Ke(t){return Fe.has(t)?t:I}function Ge(t){return t===null?"none":t>=500?"5xx":t>=400?"4xx":t>=300?"3xx":t>=200?"2xx":"1xx"}var T=class{constructor(e){this.max=e}max;admitted=new Set;overflows=0;of(e,n=!0){return e===null||e===""?Le:this.admitted.has(e)?e:n?this.admitted.size>=this.max?(this.overflows+=1,I):(this.admitted.add(e),e):I}get size(){return this.admitted.size}get overflowCount(){return this.overflows}},N=class{registry=new Ue;modelLabel=new T(He);orgLabel=new T(Me);requests;duration;ttft;streamErrors;undelivered;ledgerWrites;ledgerQueue;labelCardinality;labelOverflows;constructor(e={}){this.requests=new S({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 we({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 we({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 S({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 S({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 S({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 ue)this.ledgerWrites.inc({outcome:n.outcome,reason:n.reason},0);this.ledgerQueue=new ye({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"},de()),this.ledgerQueue.set({measure:"oldest_age_seconds"},he()/1e3)}}),this.labelCardinality=new ye({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 S({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&&Pe({register:this.registry,prefix:"urun_inference_proxy_"})}observeLedger(e){this.ledgerWrites.inc({outcome:e.outcome,reason:e.reason})}observe(e){let n=Ke(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 l=e.stream===!0?"true":"false";this.requests.inc({lane:n,method:e.method===""?I:e.method,status_class:Ge(e.status),stream:l,model:a,org:s}),this.duration.observe({lane:n,stream:l,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 ve(t){return $e((e,n)=>{(async()=>{let o=(e.url??"/").split("?")[0];if(e.method!=="GET"||o!==be){n.writeHead(404,{"content-type":"text/plain"}),n.end(`no route for ${e.method} ${o} \u2014 this port serves only GET ${be}
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 Ee}from"crypto";import{createServer as Be}from"http";var $t=9465,ze="x-urun-key-id",je="x-urun-key-fingerprint",We="urun-ratelimit-bucket:",Je="urun-ratelimit-tier:",_e="anonymous",qe=/^urun_sk_[A-Za-z0-9_-]{43}$/;function Ve(t){return Ee("sha256").update(`${We}${t}`).digest("hex")}function Ye(t){return Ee("sha256").update(`${Je}${t}`).digest("hex")}function Qe(t){let e=/^Bearer[ \t]+(\S.*)$/i.exec((t??"").trim());if(!e)return null;let n=e[1].trim();return qe.test(n)?n:null}function Se(){return Be((t,e)=>{let n=Qe(t.headers.authorization);e.setHeader(ze,n===null?_e:Ve(n)),e.setHeader(je,n===null?_e:Ye(n)),e.writeHead(200),e.end()})}p();import{createClientToken as Xe,ClientTokenError as Ze}from"@urun-sh/core";var m=class extends Error{constructor(n,o=401){super(n);this.status=o;this.name="ProxyAuthError"}status},_=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function R(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 et=60,tt=6e4,nt=4096;function rt(t){return t===401||t===403}var U=class{constructor(e){this.opts=e;this.mint=e.mint??Xe,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<tt)return{apiKey:e,orgId:n.orgId};let o;try{o=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:et})).orgId}catch(r){throw r instanceof Ze&&rt(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 _(`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 _("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>=nt){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 ot=8080,P="serve",Gt="0.0.0.0";function st(t,e,n){let o=(t[e]??"").trim();if(!o)throw new Error(`${e} is not set \u2014 ${n}`);return o}function Bt(t=process.env){let e=(t.PORT??"").trim(),n=e===""?ot: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=ne(O(st(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 l=s?{catalogUrl:O(s,"URUN_CATALOG_URL"),anonKey:i}:null,u=(t.URUN_SESSION_TOKEN_SECRET??"").trim(),f=(t.URUN_SHARED_ORG_API_KEY??"").trim(),x=(t.URUN_SHARED_ORG_ID??"").trim();if(!!f!=!!x)throw new Error("URUN_SHARED_ORG_API_KEY and URUN_SHARED_ORG_ID must be set together (or both left unset) \u2014 exactly one of them is set, which would silently leave the shared endpoints on the session lane");return{port:n,baseUrl:o,apiUrl:a,catalog:l,decisionSecret:u,sharedOrgKey:f,sharedOrgId:x}}p();import{createHash as it,randomUUID as at}from"crypto";var lt=512;function M(t){return`urun-inference-proxy-${it("sha256").update(t).digest("hex").slice(0,32)}`}var $=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 i=this.opts.build(e);return{router:i.router,clients:this.label(i.clients,e)}}let n=0,o=M(e.apiKey),r=()=>{n+=1},a=j({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:P,sharedOnlyListing:!0,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:()=>n===0?o:`${o}#${n}`,onPoolWedged:r,release:"end"}),s=this.opts.decisionIntake?new fe({targetFor:i=>a.targetFor(i),apiUrl:this.opts.apiUrl,apiKey:e.apiKey,orgId:e.orgId,fnName:P,secret:this.opts.decisionIntake.secret,...this.opts.decisionIntake.shared?{shared:this.opts.decisionIntake.shared}:{}}):void 0;return{router:a,clients:this.label(W(a,void 0,r,s),e)}}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>=lt;){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}async closeAll(){let e=[...this.tenants.values()].map(o=>o.router);this.tenants.clear();let n=[];if(await Promise.all(e.map(async o=>{try{await o.closeAll()}catch(r){n.push(String(r instanceof Error?r.message:r))}})),n.length>0)throw new Error(`failed to close ${n.length} tenant backhaul(s): ${n.join("; ")}`)}},Re=512,ct=1024,L=class extends Error{constructor(){super(`conversation capacity reached (${Re} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function ut(t){return{sessionStartOptions:e=>{if(e.startsWith(z))return;let n=t.get(e);return n===void 0?void 0:{sessionId:n}},onSessionOpen:(e,n)=>{e.startsWith(z)||t.set(e,n)}}}function dt(t,e,n,o){let r=j({baseUrl:t.baseUrl,orgId:e.orgId,defaultApp:null,fnName:P,sharedOnlyListing:!0,apiUrl:t.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:t.baseUrl},catalog:t.catalog,subject:n.subject,release:"detach",...ut(o)});return{clients:W(r),release:async()=>{await r.closeAll()}}}var H=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,n){let o={id:n,subject:`${M(e.apiKey)}:live:${n}`},r=new Map,a=this.opts.build?this.opts.build(e,o):dt(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>=Re;){let e=this.byCaller.keys().next();if(e.done)throw new L;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 L;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,at())),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 l=r().backhaul.clients.openAudio;if(l===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return l(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 l=r().backhaul.clients.openAudioOn;if(l===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 l(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.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>=ct;){let l=this.byHandle.keys().next();if(l.done)break;this.byHandle.delete(l.value)}this.byHandle.set(s,{apiKey:e.apiKey,conversationId:i.id})},clientsForResume:async s=>{let i=this.byHandle.get(s),l=i===void 0?void 0:this.byCaller.get(i.apiKey),u=i===void 0||l===void 0?void 0:l.get(i.conversationId);if(i===void 0||u===void 0||i.apiKey!==e.apiKey)throw new Q("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 ht="/inference-ledger",pt=1e4,D=class extends Error{ledgerFailureReason;constructor(e,n="duplicate"){super(e),this.name="LedgerDuplicateError",this.ledgerFailureReason=n}},q=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"}},V=class extends m{ledgerFailureReason="auth";constructor(e){super(e),this.name="LedgerAuthError"}},gt=[["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",xe],["cost_nano_usd",xe],["price_version",t=>t===null||typeof t=="string"]];function ft(t){for(let[e,n]of gt){if(!(e in t))return`${e} (absent)`;if(!n(t[e]))return`${e} (${JSON.stringify(t[e])})`}return null}function mt(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"}:ft(o)!==null?{status:"unavailable"}:{status:"found",row:o}}function xe(t){return t===null?!0:typeof t=="number"?Number.isFinite(t):typeof t!="string"?!1:/[^\s]/.test(t)&&Number.isFinite(Number(t))}function J(t,e){return t===null?e===null:e===null?!1:Number(e)===t}function Ce(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 yt(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&&Ce(t.started_at,n.started_at)&&Ce(t.completed_at,n.completed_at)&&J(t.prompt_tokens,n.prompt_tokens)&&J(t.completion_tokens,n.completion_tokens)&&J(t.output_units,n.output_units)}function wt(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 f=n[u];if(f===null)return null;if(typeof f!="string")throw new g(`${e} answered a non-string ${u}: ${JSON.stringify(f)}`);if(!/[^\s]/.test(f))throw new g(`${e} answered a BLANK ${u} (${JSON.stringify(f)}) \u2014 it satisfies "is a string" while naming nothing, which is exactly what inference_requests_priced_attribution_check exists to forbid`);return f},s=a("price_version"),i=a("unpriced_reason"),l=r!=null;if(l&&(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(!l&&(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 F=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async write(e,n){let o=`${this.opts.apiUrl.replace(/\/+$/,"")}${ht}`,r;try{r=await this.fetchImpl(o,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify(n),signal:AbortSignal.timeout(pt)})}catch(l){throw new g(`the uRun ledger surface at ${o} could not be reached: ${l instanceof Error?l.message:String(l)}`)}if(r.status===409){let l=await r.text().catch(()=>""),u=mt(l);throw u.status==="found"&&yt(n,u.row)?new q(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 D(`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 D(`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). ${l.slice(0,400)}`)}if(r.status===401||r.status===403)throw await r.text().catch(()=>{}),new V("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 l=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}: ${l.slice(0,400)}`)}let a;try{a=await r.json()}catch(l){throw new g(`the uRun ledger surface at ${o} answered a body that is not JSON: ${l instanceof Error?l.message:String(l)}`)}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=wt(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}};p();import{createServer as vt}from"http";p();var K="/inference-usage",ke=25e3;var bt=new Set(["Error","TypeError","SyntaxError","TimeoutError"]);function Oe(t){if(!(t instanceof Error))return`a ${typeof t}`;let e=bt.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 G=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async lookup(e,n){let o=`${this.opts.apiUrl.replace(/\/+$/,"")}${K}`,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(ke)})}catch(s){throw new A(`the uRun usage surface (POST ${K}) could not be reached within ${ke}ms: ${Oe(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 A(`the uRun usage surface (POST ${K}) answered HTTP ${r.status} (${i})`)}let a;try{a=await r.json()}catch(s){throw new A(`the uRun usage surface (POST ${K}) answered HTTP ${r.status} with a body that is not JSON: ${Oe(s)}`)}return le(a,this.opts.onRejectedRow)}};var _t=5e3,Et=3e3,St="/api/client-tokens";function Y(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function Ae(t,e,n,o){Y(t,e,{error:{message:n,type:o}})}async function Rt(t,e=fetch){let n=`${t.replace(/\/+$/,"")}${St}`;try{let o=await e(n,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(Et)});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 _n(t){let e=t.resolver??new U({gatewayUrl:t.baseUrl}),n=t.decisionSecret??"",o=t.sharedOrgKey&&t.sharedOrgId?{apiKey:t.sharedOrgKey}:void 0,r=t.registry??new $({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog,...n!==""?{decisionIntake:{secret:n,...o?{shared:o}:{}}}:{}});n!==""&&process.stderr.write(`inference-proxy: stateless direct path ENABLED \u2014 caller-org decisions dial ready runtimes directly (no session mint)
4
+ `);let a=t.probeControlPlane??(()=>Rt(t.baseUrl)),s=t.usageClient??new G({apiUrl:t.apiUrl}),i=t.ledgerClient??new F({apiUrl:t.apiUrl}),l=new N(t.defaultMetrics===void 0?{}:{defaultMetrics:t.defaultMetrics}),u=async c=>e.resolve(R(c.headers.authorization)),f=c=>c instanceof m?{status:c.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:c instanceof _?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,x=me({clients:async c=>r.clientsFor(await u(c)),usage:{authorize:c=>R(c.headers.authorization),lookup:(c,d)=>s.lookup(c,d)},statusOf:f,...t.requestLog?{requestLog:t.requestLog}:{},observe:c=>l.observe(c),ledger:{write:(c,d)=>i.write(R(c.headers.authorization),d),observe:c=>l.observeLedger(c),...t.ledgerLog?{report:t.ledgerLog}:{}}}),v=null,E=vt((c,d)=>{let h=new URL(c.url??"/","http://localhost");(async()=>{if(c.method==="GET"&&h.pathname==="/healthz"){Y(d,200,{ok:!0});return}if(c.method==="GET"&&h.pathname==="/readyz"){let k=Date.now();if(!v||k-v.at>=_t){let Ne=await a();v={at:k,...Ne}}Y(d,v.ready?200:503,{ok:v.ready,control_plane:v.detail});return}let y=h.pathname==="/v1"||h.pathname.startsWith("/v1/"),C=h.pathname.startsWith("/partners/");if(!y&&!C){Ae(d,404,`no route for ${c.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}x(c,d)})().catch(y=>{if(!d.headersSent){Ae(d,500,String(y instanceof Error?y.message:y),"proxy_error");return}d.end()})}),B=t.conversations??new H({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),Ie=[Z(E,{authenticate:async c=>{let d=await e.resolve(c),h=B.connectionFor(d);return{clients:h.clients,scope:M(d.apiKey),onBindResumeHandle:h.bindResumeHandle,clientsForResume:h.clientsForResume,release:h.release}}}),te(E,{clients:async c=>r.clientsFor(await u(c)),statusOf:f}).close,oe(E,{resolver:{resolve:async(c,d)=>{let h=await e.resolve(R(c.headers.authorization)),y=B.connectionFor(h),C;try{C=await ie(y.clients,se(c),d)}catch(k){throw y.release().catch(()=>{}),k}return{...C,release:y.release}}}})];E.on("upgrade",(c,d)=>{let h=new URL(c.url??"/","http://localhost");h.pathname===X||h.pathname===ee||h.pathname===re||d.destroyed||(d.write(`HTTP/1.1 404 Not Found\r
5
+ Connection: close\r
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 Te=async()=>{for(let c of Ie)await c();await B.detachAll()};return{server:E,metricsServer:ve(l),metrics:l,edgeIdentityServer:Se(),registry:r,closeAll:()=>r.closeAll(),closeWebSockets:Te}}async function En(t,e,n){n&&await n();let o=new Promise(r=>t.close(()=>r()));t.closeAllConnections(),await o,await pe()||ge(),await e()}export{It as a,be as b,I as c,Le as d,He as e,Me as f,Ke as g,Ge as h,T as i,N as j,ve as k,$t as l,ze as m,je as n,We as o,Je as p,_e as q,Ve as r,Ye as s,Qe as t,Se as u,m as v,_ as w,R as x,tt as y,nt as z,U as A,ot as B,P as C,Gt as D,Bt as E,lt as F,M as G,$ as H,Re as I,ct as J,L as K,H as L,ht as M,pt as N,D as O,g as P,wt as Q,F as R,_t as S,Et as T,_n as U,En as V};
@@ -0,0 +1,4 @@
1
+ import{c as y}from"./chunk-YSFSRI3D.js";y();y();var p=class extends Error{},ue=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;function ce(o){if(typeof o!="string")return!1;let t=ue.exec(o);if(!t)return!1;let[,e,n,r,i,l,d]=t.map(Number);if(!Number.isFinite(Date.parse(o)))return!1;let a=new Date(Date.UTC(e,n-1,r,i,l,d));return a.getUTCFullYear()===e&&a.getUTCMonth()===n-1&&a.getUTCDate()===r&&a.getUTCHours()===i&&a.getUTCMinutes()===l&&a.getUTCSeconds()===d}function pe(o){if(o===null||typeof o!="object")return null;let t=o.shared;if(t==null)return null;if(typeof t!="object")throw new p(`catalog "shared" block is ${typeof t}, expected an object`);let{shared_org_id:e,endpoints:n,regions:r,compliance:i}=t;if(typeof e!="string"||e==="")throw new p(`catalog "shared" block has no shared_org_id: ${JSON.stringify(t)}`);if(!Array.isArray(n))throw new p(`catalog "shared" block endpoints is not an array: ${JSON.stringify(t)}`);let l=n.map((d,a)=>{if(d===null||typeof d!="object")throw new p(`catalog shared endpoints[${a}] is not an object: ${JSON.stringify(d)}`);let s=d;for(let c of["model_id","variant","app_slug","function"])if(typeof s[c]!="string"||s[c].length===0)throw new p(`catalog shared endpoints[${a}] (${JSON.stringify(s)}) has no valid "${c}"`);let u=(c,g)=>{if(s[c]!==void 0&&s[c]!==null&&!g)throw new p(`catalog shared endpoints[${a}] (${s.model_id}:${s.variant}) carries a malformed "${c}": ${JSON.stringify(s[c])}`)};u("ready",typeof s.ready=="boolean"),u("ready_runtimes",typeof s.ready_runtimes=="number"&&Number.isInteger(s.ready_runtimes)&&s.ready_runtimes>=0),u("ready_since",ce(s.ready_since));for(let c of["model_id","variant"]){let g=s[c];if(g.includes(":"))throw new p(`catalog shared endpoints[${a}] has "${c}" containing ":" (${JSON.stringify(g)}) \u2014 the external identity <model_id>:<variant> would be unparseable (resolveSharedModel splits at the LAST colon); platform catalog defect`)}return u("output_free",typeof s.output_free=="boolean"),s});if(r!=null&&(!Array.isArray(r)||!r.every(d=>typeof d=="string"&&d.length>0)))throw new p(`catalog "shared" block regions is malformed: ${JSON.stringify(r)}`);if(i!=null){let d=i;if(typeof i!="object"||typeof d.zdr!="boolean"||typeof d.hipaa!="boolean")throw new p(`catalog "shared" block compliance is malformed (operator-declared zdr + hipaa booleans required): ${JSON.stringify(i)}`);for(let a of Object.keys(d))if(a!=="zdr"&&a!=="hipaa")throw new p(`catalog "shared" block compliance carries "${a}" beyond the declared zdr/hipaa contract: ${JSON.stringify(i)}`)}return l.length===0?null:{shared_org_id:e,endpoints:l,...r!=null?{regions:r}:{},...i!=null?{compliance:i}:{}}}function S(o){return[...o.endpoints].sort((t,e)=>t.model_id.localeCompare(e.model_id)||t.variant.localeCompare(e.variant))}function K(o,t,e){let n=(t??"").trim();if(n==="")return null;let r=n,i=e,l=n.lastIndexOf(":");l>0&&(r=n.slice(0,l),i=n.slice(l+1));let d=o.endpoints.filter(s=>s.model_id===r);if(d.length===0)return null;if(i!==void 0){let s=d.find(u=>u.variant===i);return s?{shared_org_id:o.shared_org_id,endpoint:s}:null}if(d.length===1)return{shared_org_id:o.shared_org_id,endpoint:d[0]};let a=d.filter(s=>s.compat_default===!0);if(a.length!==1)throw new p(`shared model "${r}" has ${a.length} compat_default rows (exactly one required) among variants ${d.map(s=>s.variant).join(", ")}`);return{shared_org_id:o.shared_org_id,endpoint:a[0]}}async function Ie(o){let t=o.fetchImpl??fetch,e=`${o.apiUrl.replace(/\/+$/,"")}/catalog`,n=await t(e,{headers:{Authorization:`Bearer ${o.apiKey}`,Accept:"application/json"}});if(!n.ok)throw new p(`catalog read failed: GET ${e} \u2192 ${n.status}`);return pe(await n.json())}y();function _(o){return o.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}y();var w=class extends Error{name="ModelPriceError"},E="per_million_input_tokens",N="per_million_output_tokens",z="model_id,variant,unit,price_usd,effective_at,expires_at",J={per_request:!0,per_minute:!0,per_output_unit:!0},ge=5e3;function fe(o,t){let e=typeof o=="number"?o:typeof o=="string"&&o.trim()!==""?Number(o):NaN;if(!Number.isFinite(e)||e<0)throw new w(`${t}: price_usd is not a non-negative number (${JSON.stringify(o)})`);return e}function H(o,t,e){let n=typeof o=="string"?Date.parse(o):NaN;if(!Number.isFinite(n))throw new w(`${t}: ${e} is not a parseable timestamp (${JSON.stringify(o)})`);return n}function he(o,t,e){return o===null?null:H(o,t,e)}function me(o,t){if(o===E)return"input";if(o===N)return"output";if(typeof o=="string"&&Object.hasOwn(J,o))return null;throw new w(`${t}: unit ${JSON.stringify(o)} is neither a per-token rate (${E} | ${N}) nor a known GPU-time unit (${Object.keys(J).join(" | ")}). Skipping it would publish no prices forever with no signal \u2014 if ENG-317 named the per-token units differently, teach this module the new names instead`)}function we(o,t){let e=o.reduce((r,i)=>i.effective_at>r.effective_at?i:r,o[0]),n=o.find(r=>r.effective_at===e.effective_at&&r.usd!==e.usd);return n?{reason:`two ${t} rates are effective at the same instant (${e.usd} and ${n.usd} USD/Mtok), so the winner would be read order \u2014 this model is served UNPRICED until one of them is expired; expire one of them`}:{rate:e}}function D(o,t){return`${o}\0${t}`}function ye(o){if(!Array.isArray(o))throw new w(`model_prices read is not an array: ${JSON.stringify(o)}`);let t=[];for(let e=0;e<o.length;e++){let n=o[e],r=`model_prices[${e}]`;if(n===null||typeof n!="object"||Array.isArray(n))throw new w(`${r} is not an object: ${JSON.stringify(n)}`);let i=n;for(let s of z.split(","))if(!Object.hasOwn(i,s))throw new w(`${r} has no "${s}" column: ${JSON.stringify(i)}`);let l=me(i.unit,r);if(l===null)continue;let d=i.model_id;if(typeof d!="string"||d==="")throw new w(`${r} has no model_id (${JSON.stringify(i)})`);let a=i.variant;if(!(a===null||typeof a=="string"&&a!==""))throw new w(`${r} has a non-string variant (${JSON.stringify(a)})`);t.push({model_id:d,variant:a,side:l,usd:fe(i.price_usd,r),effective_at:H(i.effective_at,r,"effective_at"),expires_at:he(i.expires_at,r,"expires_at")})}return t}function q(o,t=Date.now(),e){let n=new Map,r=new Map;for(let a of o){if(a.effective_at>t||a.expires_at!==null&&a.expires_at<=t)continue;let s=n.get(a.model_id);s||(s=new Map,n.set(a.model_id,s),r.set(a.model_id,new Map));let u=a.variant??"",c=s.get(u);c||(c={input:[],output:[]},s.set(u,c),r.get(a.model_id).set(u,a.variant)),c[a.side].push({effective_at:a.effective_at,usd:a.usd})}let i=[],l=[];for(let[a,s]of n){let u=s.get(""),c=(g,h,v)=>{let m=g&&g.length>0?g:u?.[h];if(!m||m.length===0)return null;let f=we(m,h);return"reason"in f?f:f.rate.usd===0?h==="output"&&v!==null&&e?.has(D(a,v))?f.rate:{reason:`the ${h} rate in force is zero, which publishes as "free" and would top :cheapest routing \u2014 this model is served UNPRICED; delete the row if the model is not token-priced, or set a real rate`}:f.rate};for(let[g,h]of r.get(a)){let v=s.get(g),m=c(v?.input,"input",h),f=c(v?.output,"output",h);if(m!==null&&"reason"in m){l.push({model_id:a,variant:h,reason:m.reason});continue}if(f!==null&&"reason"in f){l.push({model_id:a,variant:h,reason:f.reason});continue}m===null||f===null||i.push({model_id:a,variant:h,pricing:{input:m.usd,output:f.usd}})}}let d=(a,s)=>a.model_id.localeCompare(s.model_id)||(a.variant===null?1:0)-(s.variant===null?1:0)||(a.variant??"").localeCompare(s.variant??"");return{prices:i.sort(d),refusals:l.sort(d)}}function k(o,t,e){if(e.refusals.some(i=>i.model_id===o&&i.variant===t))return null;let n=e.prices.find(i=>i.model_id===o&&i.variant===t);if(n)return n.pricing;if(e.refusals.some(i=>i.model_id===o&&i.variant===null))return null;let r=e.prices.find(i=>i.model_id===o&&i.variant===null);return r?r.pricing:null}var C={prices:[],refusals:[]};async function Le(o){let t=o.fetchImpl??fetch,e=`${E},${N}`,n=await t(`${o.catalogUrl}/model_prices?select=${z}&unit=in.(${e})`,{headers:{apikey:o.anonKey,Authorization:`Bearer ${o.anonKey}`},signal:AbortSignal.timeout(ge)});if(!n.ok)throw new Error(`model_prices fetch failed: ${n.status}`);return ye(await n.json())}y();var _e={chat:!0,code:!0,agent:!0,vl:!0},G={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function ve(o){let t=o.toLowerCase();return Object.hasOwn(G,t)?G[t]:null}var V={"us-east-1":"US","us-east-2":"US","us-west-1":"US","us-west-2":"US","ca-central-1":"CA","ca-west-1":"CA","sa-east-1":"BR","eu-central-1":"DE","eu-central-2":"CH","eu-west-1":"IE","eu-west-2":"GB","eu-west-3":"FR","eu-north-1":"SE","eu-south-1":"IT","eu-south-2":"ES","af-south-1":"ZA","ap-east-1":"HK","ap-south-1":"IN","ap-south-2":"IN","ap-northeast-1":"JP","ap-northeast-2":"KR","ap-northeast-3":"JP","ap-southeast-1":"SG","ap-southeast-2":"AU","ap-southeast-3":"ID","ap-southeast-4":"AU","ap-southeast-5":"MY","ap-southeast-7":"TH","il-central-1":"IL","me-central-1":"AE","me-south-1":"BH"};function Re(o,t){if(!o||o.length===0)return[];let e=[],n=[];for(let r of o){let i=Object.hasOwn(V,r)?V[r]:null;if(i===null){n.push(r);continue}e.some(d=>d.region===r)||e.push({country_code:i,region:r})}return n.length>0&&t(`urun openrouter doc: shared block region(s) ${n.join(", ")} have no country mapping \u2014 omitted from datacenters, never guessed`),e}var W={qwen:"Qwen",qwen1:"Qwen",qwen2:"Qwen",qwen3:"Qwen",gemma:"Gemma",diffusiongemma:"Gemma",gpt:"GPT",glm:"GLM",deepseek:"DeepSeek",mistral:"Mistral"},Q={2:"Llama2",3:"Llama3"};function be(o){if(!o)return null;let t=o.split("@")[0],e=t.indexOf("/"),n=(e===-1?t:t.slice(e+1)).toLowerCase().split(/[^a-z0-9]+/).filter(r=>r.length>0);for(let r=0;r<n.length;r+=1){let i=n[r];if(Object.hasOwn(W,i))return W[i];let l=n[r+1];if(i==="llama"&&l!==void 0&&Object.hasOwn(Q,l))return Q[l];if(i==="phi"&&l!==void 0&&/^\d/.test(l))return"Phi"}return null}var Se=["context_length","max_model_len"];function $e(o){let t=Se.map(e=>o.engine_args?.[e]).filter(e=>typeof e=="number"&&Number.isFinite(e)&&e>0);return t.length===0?null:t.every(e=>e===t[0])?t[0]:null}function T(o){let t=o.map($e),e=t[0];return e==null?null:t.every(n=>n===e)?e:null}function ke(o){let t=o.engine_args?.reasoning_parser;return t==null||t===""?!1:typeof t=="string"?!0:null}function F(o){if(o.length===0)return null;let t=o.map(ke),e=t[0];return e==null?null:t.every(n=>n===e)?e:null}function Ce(o,t){let e=t.filter(r=>_(`${r.model_id}-${r.variant}`)===o);if(e.length>0)return M(e);let n=t.filter(r=>_(r.model_id)===o);return n.length>0?M(n):null}function M(o){let t=o[0];return o.every(n=>n.model_id===t.model_id&&n.variant===t.variant)?{row:o.length===1?t:Pe(o),matches:o}:null}function Pe(o){let t=o[0],e=n=>o.every(r=>(r[n]??void 0)===(t[n]??void 0))?t[n]:void 0;return{model_id:t.model_id,variant:t.variant,gpu_spec:e("gpu_spec"),engine:e("engine"),task:e("task"),description:e("description"),size_gb:e("size_gb"),hf_repo:e("hf_repo"),updated_at:e("updated_at")}}function Y(o){let t=o.updated_at?Date.parse(o.updated_at):NaN;return Number.isFinite(t)?Math.floor(t/1e3):0}function A(o,t,e){let n=Ce(o,t);if(!n)return{id:o,object:"model",created:0,owned_by:"urun"};let{row:r,matches:i}=n,l=T(i),d=k(r.model_id,r.variant,e),a=ee(r,i),s=F(i);return{id:o,object:"model",created:Y(r),owned_by:"urun",name:`${r.model_id}:${r.variant}`,...r.description?{description:r.description}:{},...r.task?{task:r.task}:{},...r.engine?{engine:r.engine}:{},...r.gpu_spec?{gpu_spec:r.gpu_spec}:{},...l!==null?{context_length:l}:{},...d!==null?{pricing:d}:{},...a.published.length>0?{voices:[...a.published]}:{},...s!==null?{capabilities:{reasoning:s}}:{}}}var xe=["Vivian","Serena","Uncle_Fu","Dylan","Eric","Ryan","Aiden","Ono_Anna","Sohee"];function ee(o,t){if((o.task??void 0)!=="tts")return{published:[],defect:null};let e=t.map(i=>i.engine_args?.voices),n=e[0];if(Array.isArray(n)&&n.length>0&&n.every(i=>typeof i=="string")&&e.every(i=>Array.isArray(i)&&i.length===n.length&&i.every((l,d)=>l===n[d])))return{published:[...n],defect:null};let r=e.some(i=>i!=null)?`the catalog's engine_args.voices for ${o.model_id}:${o.variant} could not be vouched (empty, malformed, or not an identical non-empty string array on every GPU placement)`:null;return o.model_id==="qwen3-tts-customvoice"?{published:[...xe],defect:r}:{published:[],defect:r}}function L(o){let t=o.length>0?M(o):null;return t===null?{published:[],defect:null}:ee(t.row,t.matches)}function X(o){let t=/^(\d+)(?:\.(\d+))?(?:e([+-]?\d+))?$/.exec(String(o));if(!t)throw new Error(`unrenderable per-token price: ${String(o)}`);let[,e="",n="",r=""]=t,i=e+n,l=e.length-6+(r===""?0:Number(r));return(l<=0?`0.${"0".repeat(-l)}${i}`:`${i.slice(0,l)}.${i.slice(l).padEnd(1,"0")}`).replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,"")}function Z(o){return{input:[{type:"prompt",unit:"token",cost_usd:X(o.input)}],output:[{type:"completion",unit:"token",cost_usd:X(o.output)}]}}function Oe(o,t,e,n){if(t.length===0)return null;let r=M(t);if(!r)return null;let{row:i,matches:l}=r;if(!i.task||_e[i.task]!==!0)return null;let d=T(l),a=k(i.model_id,i.variant,e),s=be(i.hf_repo),u={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};return d!==null&&(u.max_tokens={type:"integer",min:1,max:d,unit:"token"}),{schema_version:"2.4",id:`${i.model_id}:${i.variant}`,name:`${i.model_id}:${i.variant}`,created:Y(i),quantization:ve(i.variant),...s!==null?{tokenizer:s}:{},description:i.description??"",hugging_face_id:i.hf_repo?i.hf_repo.split("@")[0]??i.hf_repo:"",is_ready:o.ready===!0&&a!==null,is_free:!1,input_modalities:[{type:"text",...d!==null?{supported_inputs:{max_context_length:{value:d,unit:"token"}}}:{},...a?{pricing:Z(a).input}:{}},...i.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:u,...a?{pricing:Z(a).output}:{}}],...n.datacenters.length>0?{datacenters:n.datacenters}:{},...n.compliance?{compliance:n.compliance}:{}}}function te(o,t,e,n){let r=n??(s=>console.warn(s.trimEnd())),i=o?S(o):[];if(i.length>0){let s=i.filter(u=>u.ready===void 0);if(s.length===i.length)r("urun openrouter doc: readiness seam absent from shared block; declaring every endpoint not ready");else if(s.length>0){let u=s.map(c=>`${c.model_id}:${c.variant}`).join(", ");r(`urun openrouter doc: readiness seam partially deployed \u2014 endpoints ${u} carry no ready field and are declared not ready`)}}let l={datacenters:Re(o?.regions,r),compliance:o?.compliance??null},d=[],a=new Set;for(let s of i){let u=`${s.model_id}:${s.variant}`;if(a.has(u))continue;a.add(u);let c=Oe(s,t.filter(g=>g.model_id===s.model_id&&g.variant===s.variant),e,l);c&&d.push(c)}return{data:d}}var Xe="https://api.urun.sh/v1",$=class extends Error{},P=class extends Error{},B="urs1.";function ne(o,t){return B+Buffer.from(JSON.stringify([o,t]),"utf8").toString("base64url")}function Me(o){if(!o.startsWith(B))return null;try{let t=JSON.parse(Buffer.from(o.slice(B.length),"base64url").toString("utf8"));if(Array.isArray(t)&&typeof t[0]=="string"&&typeof t[1]=="string")return{app:t[0],key:t[1]}}catch{}return null}async function Ze(o){let t=o.fetchImpl??fetch,e=`${o.apiUrl.replace(/\/+$/,"")}/apps`,n=await t(e,{headers:{Authorization:`Bearer ${o.apiKey}`,Accept:"application/json"}});if(!n.ok)throw new Error(`org apps listing failed: GET ${e} \u2192 ${n.status}`);let r=await n.json();if(!Array.isArray(r.apps))throw new Error(`org apps listing returned no "apps" array (GET ${e})`);if(r.truncated===!0)throw new Error(`org apps listing was truncated (GET ${e} returned ${r.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return r.apps}function oe(o){if(o.length===0)return[];if(!o.every(e=>(e.task??void 0)==="image"))return[];let t=Ae(o);return t===null?[]:[t]}function U(o,t,e,n,r){if(!r)throw new p(`shared model "${o}" cannot be gated: no catalog oracle is configured, and a shared endpoint's modality must come from the model's OWN model_catalog row \u2014 never a default`);if(t===void 0||e===void 0)throw new p(`shared model "${o}" resolved without a catalog id + variant to join on`);let i=n.filter(l=>l.model_id===t&&l.variant===e);if(i.length===0)throw new p(`shared model "${o}" names endpoint ${t}:${e} but the catalog has no model_catalog row for it \u2014 the shared block cannot vouch a modality from nothing`);return i}function re(o,t){if(!t.every(r=>(r.task??void 0)==="image"))return[];let e=t.map(r=>r.engine_args?.expects_image),n=e[0];if(typeof n!="boolean"||!e.every(r=>r===n))throw new p(`shared image model "${o}" does not vouch an explicit image mode: its ${t.length} model_catalog row(s) carry engine_args.expects_image ${JSON.stringify(e)} \u2014 the shared lane requires a boolean (false = pure text-to-image generation, true = edit) and refuses to silently default to edit`);return[n===!1?"generation":"edit"]}var de="shared:",ie="speech:";function j(o){return o.sharedApp?de+o.appSlug:o.appSlug}function se(o){return typeof o.modelId=="string"&&typeof o.variant=="string"?{kind:"catalog",model_id:o.modelId,variant:o.variant}:{kind:"not-a-catalog-model",model:o.appSlug}}function b(o,t){let e=t.filter(i=>_(`${i.model_id}-${i.variant}`)===o);if(e.length>0)return e;let n=t.filter(i=>_(i.model_id)===o);return new Set(n.map(i=>i.variant)).size===1?n:[]}function Te(o){let t=o.engine_args?.expects_image;return t==null?"edit":t===!1?"generation":t===!0?"edit":null}function Ae(o){let t=o.map(Te),e=t[0];return e!=null&&t.every(n=>n===e)?e:null}var ae={stt:"stt",tts:"tts"};function R(o){if(o.length===0)return null;let t=o[0].task??void 0;return t===void 0||ae[t]===void 0?null:o.every(e=>(e.task??void 0)===t)?ae[t]:null}var I=6e4,le=class{constructor(t){this.opts=t}opts;pool=new Map;appsCache=null;rowsCache=null;seed(t,e){this.pool.set(t,Promise.resolve(e))}async deployedApps(){if(!this.opts.listApps)return[];let t=this.opts.appsTtlMs??I;if(this.appsCache&&Date.now()-this.appsCache.at<t)return this.appsCache.apps;let e=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:e},e}async sharedBlock(){if(!this.opts.shared)return null;let t=this.opts.appsTtlMs??I;if(this.sharedCache&&Date.now()-this.sharedCache.at<t)return this.sharedCache.block;let e=await this.opts.shared();return this.sharedCache={at:Date.now(),block:e},e}sharedCache=null;async catalogRows(){if(!this.opts.listCatalog)return[];let t=this.opts.appsTtlMs??I;if(this.rowsCache&&Date.now()-this.rowsCache.at<t)return this.rowsCache.rows;let e=await this.opts.listCatalog();return this.rowsCache={at:Date.now(),rows:e},e}pricesCache=null;async priceRows(){if(!this.opts.listPrices)return[];let t=this.opts.appsTtlMs??I;if(this.pricesCache&&Date.now()-this.pricesCache.at<t)return this.pricesCache.rows;let e=await this.opts.listPrices();return this.pricesCache={at:Date.now(),rows:e},e}async pricesForListing(t){let e;try{e=await this.priceRows()}catch(r){if(r instanceof w)throw r;return this.warn(`urun /v1/models: the per-token price surface could not be read (${String(r instanceof Error?r.message:r)}) \u2014 every model is listed UNPRICED for now; Hugging Face will show no price until the read succeeds
2
+ `),C}let n=q(e,Date.now(),t);for(let r of n.refusals)this.warn(`urun /v1/models: refusing to price ${r.model_id}:${r.variant??"*"} \u2014 ${r.reason}
3
+ `);return n}outputFreeKeys(t){let e=new Set;if(!t)return e;for(let n of t.endpoints)n.output_free===!0&&e.add(D(n.model_id,n.variant));return e}warn(t){(this.opts.warn??(n=>console.warn(n.trimEnd())))(t)}servable(t){return t.filter(e=>e.serves==="openai"&&e.deployment_status==="active").map(e=>e.app_slug)}async availableIds(){let t=this.opts.defaultApp,e=new Set(t===null?[]:[t]);if(this.opts.listApps)for(let r of this.servable(await this.deployedApps()))e.add(r);for(let r of this.pool.keys())r.startsWith(de)||r.startsWith(ie)||e.add(r);let n=[...e].filter(r=>r!==t).sort();return t===null?n:[t,...n]}async noDefaultApp(t){let e=await this.availableIds(),n=e.length>0?e.join(", "):"(none deployed)";throw new $(t===""?`no "model" was given and this endpoint has no default model \u2014 name one of your org's deployed apps that declare serves="openai"; available models: ${n}`:`model "${t}" does not name a deployed app in your org that declares serves="openai", and this endpoint has no default model to fall back to; available models: ${n}`)}async resolveTarget(t){let e=(t??"").trim(),n=this.opts.defaultApp;if(!e||e==="urun")return n===null?this.noDefaultApp(e):{appSlug:n};let r=_(e);if(r===n)return{appSlug:n};if(this.pool.has(r))return{appSlug:r};if(this.opts.listApps){let i=await this.deployedApps(),l=this.servable(i);if(l.includes(r))return{appSlug:r};let d=l.filter(s=>s.startsWith(`${r}-`));if(d.length===1)return{appSlug:d[0]};if(d.length>1)throw new $(`model "${e}" is ambiguous across deployed apps ${d.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let a=i.find(s=>s.app_slug===r||s.app_slug.startsWith(`${r}-`));if(a)throw new $(`model "${e}" maps to app "${a.app_slug}", which has no function declaring serves="openai" \u2014 add serves="openai" to its @app.function or deploy it with \`urun serve ${e}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.shared){let i=await this.sharedBlock();if(i){let l=K(i,e);if(l)return{appSlug:l.endpoint.app_slug,fnName:l.endpoint.function,sharedApp:!0,modelId:l.endpoint.model_id,variant:l.endpoint.variant,sharedOrgId:i.shared_org_id}}}if(this.opts.listCatalog){let l=(await this.opts.listCatalog()).find(d=>e===d.model_id||e===`${d.model_id}:${d.variant}`||r===_(d.model_id)||r===_(`${d.model_id}-${d.variant}`));if(l)throw new $(`model "${e}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${l.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return n===null?this.noDefaultApp(e):{appSlug:n}}async catalogIdentityFor(t){let e=se(await this.resolveTarget(t));return e.kind==="catalog"?{model_id:e.model_id,variant:e.variant}:null}async servingAppSlugFor(t){return(await this.resolveTarget(t)).appSlug}async targetFor(t){return this.resolveTarget(t)}async resolveApp(t){return j(await this.resolveTarget(t))}async sessionFor(t){let e=await this.resolveTarget(t);return this.acquire(e,j(e))}async speechSessionFor(t){let e=await this.resolveTarget(t);return this.acquire(e,ie+j(e))}async acquire(t,e){let n=this.pool.get(e);return n||(n=Promise.resolve(this.opts.openSession(t,e)),this.pool.set(e,n),n.catch(()=>this.pool.delete(e))),{app:e,entry:await n,identity:se(t)}}keyOf(t){let e=this.opts.sessionKey;if(!e)throw new Error("ModelRouter: sessionKey is not configured \u2014 the session-identity seam (handleFor/sessionForHandle) is unavailable on this router");let n=e(t);if(typeof n!="string"||n.length===0)throw new Error("ModelRouter: sessionKey returned an empty identity for a pooled session");return n}async handleFor(t){let{app:e,entry:n}=await this.sessionFor(t);return{app:e,handle:ne(e,this.keyOf(n))}}async speechHandleFor(t){let{app:e,entry:n}=await this.speechSessionFor(t);return{app:e,handle:ne(e,this.keyOf(n))}}async sessionForHandle(t){let e=Me(t);if(!e)throw new P(`malformed session handle ${JSON.stringify(t)} \u2014 not issued by this proxy`);let{app:n,key:r}=e,i=this.pool.get(n),l=i?await i.then(d=>d,()=>null):null;if(l===null)throw new P(`the session behind this handle (app "${n}") is gone \u2014 closed, evicted after its pod died, or the proxy restarted; it cannot be reattached. Reconnect without a handle to start a new session.`);if(this.keyOf(l)!==r)throw new P(`the session behind this handle (app "${n}") was replaced \u2014 the original backhaul died and a new session serves this app now; the handle's session cannot be reattached. Reconnect without a handle to start a new session.`);return{app:n,entry:l}}async evict(t,e){let n=this.pool.get(t);!n||await n.then(i=>i,()=>null)!==e||this.pool.get(t)!==n||(this.pool.delete(t),await this.opts.closeSession(e).catch(()=>{}))}async modelList(){let t=this.opts.sharedOnlyListing?[]:await this.availableIds(),e=await this.sharedBlock(),[n,r]=await Promise.all([this.catalogRows().then(s=>({ok:!0,rows:s}),()=>({ok:!1,rows:[]})),this.pricesForListing(this.outputFreeKeys(e))]),i=n.rows,l=n.ok?r:C;n.ok||this.warn(`urun /v1/models: the model catalog could not be read \u2014 entries fall back to the bare OpenAI fields AND no model is priced on either lane (pricing follows catalog identity, so a half-priced listing would flicker on Hugging Face)
4
+ `);let a=(e?S(e):[]).map(s=>{let u={id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun",name:`${s.model_id}:${s.variant}`};s.gpu_spec&&(u.gpu_spec=s.gpu_spec),typeof s.warm=="number"&&(u.warm=s.warm);let c=i.filter(f=>f.model_id===s.model_id&&f.variant===s.variant),g=T(c);g!==null&&(u.context_length=g);let h=F(c);h!==null&&(u.capabilities={reasoning:h});let v=k(s.model_id,s.variant,l);v!==null&&(u.pricing=v);let m=L(c);return m.published.length>0&&(u.voices=[...m.published]),u});return{object:"list",data:this.opts.sharedOnlyListing?a:[...t.map(s=>A(s,i,l)),...a]}}async openRouterModels(){let t=await this.sharedBlock(),[e,n]=await Promise.all([this.catalogRows(),this.pricesForListing(this.outputFreeKeys(t))]);return te(t,e,n,r=>this.warn(r))}async supportsImages(t,e){let n=await this.resolveTarget(t),r=await this.catalogRows();if(n.sharedApp){let i=U(String(t),n.modelId,n.variant,r,this.opts.listCatalog!==null);return re(`${n.modelId}:${n.variant}`,i).includes(e)}return oe(b(n.appSlug,r)).includes(e)}async supportsAudio(t){let e=await this.resolveTarget(t),n=await this.catalogRows();return e.sharedApp?R(n.filter(r=>r.model_id===e.modelId&&r.variant===e.variant))!==null:R(b(e.appSlug,n))!==null}async supportsSpeech(t){let e=await this.resolveTarget(t),n=await this.catalogRows();return e.sharedApp?R(n.filter(r=>r.model_id===e.modelId&&r.variant===e.variant))==="tts":R(b(e.appSlug,n))==="tts"}async supportsTranscription(t){let e=await this.resolveTarget(t),n=await this.catalogRows();return e.sharedApp?R(n.filter(r=>r.model_id===e.modelId&&r.variant===e.variant))==="stt":R(b(e.appSlug,n))==="stt"}async speechVoices(t){let e=await this.resolveTarget(t),n=await this.catalogRows(),r=e.sharedApp?n.filter(i=>i.model_id===e.modelId&&i.variant===e.variant):b(e.appSlug,n);return L(r)}async imageModelList(){let t=await this.catalogRows(),e=C,n=[];for(let i of await this.availableIds()){let l=oe(b(i,t));l.length!==0&&n.push({...A(i,t,e),image_modes:l})}let r=await this.sharedBlock();if(r)for(let i of S(r)){let l=U(`${i.model_id}:${i.variant}`,i.model_id,i.variant,t,this.opts.listCatalog!==null),d=re(`${i.model_id}:${i.variant}`,l);if(d.length===0)continue;let a={id:`${i.model_id}:${i.variant}`,object:"model",created:0,owned_by:"urun",name:`${i.model_id}:${i.variant}`,image_modes:d};i.gpu_spec&&(a.gpu_spec=i.gpu_spec),typeof i.warm=="number"&&(a.warm=i.warm),n.push(a)}return{object:"list",data:n}}async speechToTextModelList(){let t=await this.catalogRows(),e=C,n=[];for(let i of await this.availableIds())R(b(i,t))==="stt"&&n.push(A(i,t,e));let r=await this.sharedBlock();if(r)for(let i of S(r)){let l=U(`${i.model_id}:${i.variant}`,i.model_id,i.variant,t,this.opts.listCatalog!==null);if(R(l)!=="stt")continue;let d={id:`${i.model_id}:${i.variant}`,object:"model",created:0,owned_by:"urun",name:`${i.model_id}:${i.variant}`};i.gpu_spec&&(d.gpu_spec=i.gpu_spec),typeof i.warm=="number"&&(d.warm=i.warm),n.push(d)}return{object:"list",data:n}}async closeAll(){let t=[...this.pool.values()],e=[];if(await Promise.all(t.map(async n=>{try{await this.opts.closeSession(await n)}catch(r){e.push(String(r instanceof Error?r.message:r))}})),e.length>0)throw new Error(`failed to close ${e.length} pooled session(s): ${e.join("; ")}`)}};export{Ie as a,Le as b,Xe as c,$ as d,P as e,Ze as f,ie as g,j as h,se as i,le as j};