@urun-sh/openai 0.6.20 → 0.6.22

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,4 +1,4 @@
1
- import{c as m}from"./chunk-YSFSRI3D.js";m();m();var p=class extends Error{},he=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;function me(n){if(typeof n!="string")return!1;let t=he.exec(n);if(!t)return!1;let[,e,r,i,o,a,d]=t.map(Number);if(!Number.isFinite(Date.parse(n)))return!1;let l=new Date(Date.UTC(e,r-1,i,o,a,d));return l.getUTCFullYear()===e&&l.getUTCMonth()===r-1&&l.getUTCDate()===i&&l.getUTCHours()===o&&l.getUTCMinutes()===a&&l.getUTCSeconds()===d}function we(n){if(n===null||typeof n!="object")return null;let t=n.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:r,regions:i,compliance:o}=t;if(typeof e!="string"||e==="")throw new p(`catalog "shared" block has no shared_org_id: ${JSON.stringify(t)}`);if(!Array.isArray(r))throw new p(`catalog "shared" block endpoints is not an array: ${JSON.stringify(t)}`);let a=r.map((d,l)=>{if(d===null||typeof d!="object")throw new p(`catalog shared endpoints[${l}] is not an object: ${JSON.stringify(d)}`);let s=d;for(let u of["model_id","variant","app_slug","function"])if(typeof s[u]!="string"||s[u].length===0)throw new p(`catalog shared endpoints[${l}] (${JSON.stringify(s)}) has no valid "${u}"`);let c=(u,f)=>{if(s[u]!==void 0&&s[u]!==null&&!f)throw new p(`catalog shared endpoints[${l}] (${s.model_id}:${s.variant}) carries a malformed "${u}": ${JSON.stringify(s[u])}`)};c("ready",typeof s.ready=="boolean"),c("ready_runtimes",typeof s.ready_runtimes=="number"&&Number.isInteger(s.ready_runtimes)&&s.ready_runtimes>=0),c("ready_since",me(s.ready_since));for(let u of["model_id","variant"]){let f=s[u];if(f.includes(":"))throw new p(`catalog shared endpoints[${l}] has "${u}" containing ":" (${JSON.stringify(f)}) \u2014 the external identity <model_id>:<variant> would be unparseable (resolveSharedModel splits at the LAST colon); platform catalog defect`)}return c("output_free",typeof s.output_free=="boolean"),s});if(i!=null&&(!Array.isArray(i)||!i.every(d=>typeof d=="string"&&d.length>0)))throw new p(`catalog "shared" block regions is malformed: ${JSON.stringify(i)}`);if(o!=null){let d=o;if(typeof o!="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(o)}`);for(let l of Object.keys(d))if(l!=="zdr"&&l!=="hipaa")throw new p(`catalog "shared" block compliance carries "${l}" beyond the declared zdr/hipaa contract: ${JSON.stringify(o)}`)}return a.length===0?null:{shared_org_id:e,endpoints:a,...i!=null?{regions:i}:{},...o!=null?{compliance:o}:{}}}function $(n){return[...n.endpoints].sort((t,e)=>t.model_id.localeCompare(e.model_id)||t.variant.localeCompare(e.variant))}function z(n,t,e){let r=(t??"").trim();if(r==="")return null;let i=r,o=e,a=r.lastIndexOf(":");a>0&&(i=r.slice(0,a),o=r.slice(a+1));let d=n.endpoints.filter(s=>s.model_id===i);if(d.length===0)return null;if(o!==void 0){let s=d.find(c=>c.variant===o);return s?{shared_org_id:n.shared_org_id,endpoint:s}:null}if(d.length===1)return{shared_org_id:n.shared_org_id,endpoint:d[0]};let l=d.filter(s=>s.compat_default===!0);if(l.length!==1)throw new p(`shared model "${i}" has ${l.length} compat_default rows (exactly one required) among variants ${d.map(s=>s.variant).join(", ")}`);return{shared_org_id:n.shared_org_id,endpoint:l[0]}}async function Je(n){let t=n.fetchImpl??fetch,e=`${n.apiUrl.replace(/\/+$/,"")}/catalog`,r=await t(e,{headers:{Authorization:`Bearer ${n.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new p(`catalog read failed: GET ${e} \u2192 ${r.status}`);return we(await r.json())}m();function _(n){return n.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}m();var w=class extends Error{name="ModelPriceError"},L="per_million_input_tokens",D="per_million_output_tokens",q="model_id,variant,unit,price_usd,effective_at,expires_at",H={per_request:!0,per_minute:!0,per_output_unit:!0},ye=5e3;function _e(n,t){let e=typeof n=="number"?n:typeof n=="string"&&n.trim()!==""?Number(n):NaN;if(!Number.isFinite(e)||e<0)throw new w(`${t}: price_usd is not a non-negative number (${JSON.stringify(n)})`);return e}function G(n,t,e){let r=typeof n=="string"?Date.parse(n):NaN;if(!Number.isFinite(r))throw new w(`${t}: ${e} is not a parseable timestamp (${JSON.stringify(n)})`);return r}function ve(n,t,e){return n===null?null:G(n,t,e)}function Re(n,t){if(n===L)return"input";if(n===D)return"output";if(typeof n=="string"&&Object.hasOwn(H,n))return null;throw new w(`${t}: unit ${JSON.stringify(n)} is neither a per-token rate (${L} | ${D}) nor a known GPU-time unit (${Object.keys(H).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 be(n,t){let e=n.reduce((i,o)=>o.effective_at>i.effective_at?o:i,n[0]),r=n.find(i=>i.effective_at===e.effective_at&&i.usd!==e.usd);return r?{reason:`two ${t} rates are effective at the same instant (${e.usd} and ${r.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 F(n,t){return`${n}\0${t}`}function Se(n){if(!Array.isArray(n))throw new w(`model_prices read is not an array: ${JSON.stringify(n)}`);let t=[];for(let e=0;e<n.length;e++){let r=n[e],i=`model_prices[${e}]`;if(r===null||typeof r!="object"||Array.isArray(r))throw new w(`${i} is not an object: ${JSON.stringify(r)}`);let o=r;for(let s of q.split(","))if(!Object.hasOwn(o,s))throw new w(`${i} has no "${s}" column: ${JSON.stringify(o)}`);let a=Re(o.unit,i);if(a===null)continue;let d=o.model_id;if(typeof d!="string"||d==="")throw new w(`${i} has no model_id (${JSON.stringify(o)})`);let l=o.variant;if(!(l===null||typeof l=="string"&&l!==""))throw new w(`${i} has a non-string variant (${JSON.stringify(l)})`);t.push({model_id:d,variant:l,side:a,usd:_e(o.price_usd,i),effective_at:G(o.effective_at,i,"effective_at"),expires_at:ve(o.expires_at,i,"expires_at")})}return t}function V(n,t=Date.now(),e){let r=new Map,i=new Map;for(let l of n){if(l.effective_at>t||l.expires_at!==null&&l.expires_at<=t)continue;let s=r.get(l.model_id);s||(s=new Map,r.set(l.model_id,s),i.set(l.model_id,new Map));let c=l.variant??"",u=s.get(c);u||(u={input:[],output:[]},s.set(c,u),i.get(l.model_id).set(c,l.variant)),u[l.side].push({effective_at:l.effective_at,usd:l.usd})}let o=[],a=[];for(let[l,s]of r){let c=s.get(""),u=(f,h,v)=>{let y=f&&f.length>0?f:c?.[h];if(!y||y.length===0)return null;let g=be(y,h);return"reason"in g?g:g.rate.usd===0?h==="output"&&v!==null&&e?.has(F(l,v))?g.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`}:g.rate};for(let[f,h]of i.get(l)){let v=s.get(f),y=u(v?.input,"input",h),g=u(v?.output,"output",h);if(y!==null&&"reason"in y){a.push({model_id:l,variant:h,reason:y.reason});continue}if(g!==null&&"reason"in g){a.push({model_id:l,variant:h,reason:g.reason});continue}y===null||g===null||o.push({model_id:l,variant:h,pricing:{input:y.usd,output:g.usd}})}}let d=(l,s)=>l.model_id.localeCompare(s.model_id)||(l.variant===null?1:0)-(s.variant===null?1:0)||(l.variant??"").localeCompare(s.variant??"");return{prices:o.sort(d),refusals:a.sort(d)}}function x(n,t,e){if(e.refusals.some(o=>o.model_id===n&&o.variant===t))return null;let r=e.prices.find(o=>o.model_id===n&&o.variant===t);if(r)return r.pricing;if(e.refusals.some(o=>o.model_id===n&&o.variant===null))return null;let i=e.prices.find(o=>o.model_id===n&&o.variant===null);return i?i.pricing:null}var A={prices:[],refusals:[]};async function Ge(n){let t=n.fetchImpl??fetch,e=`${L},${D}`,r=await t(`${n.catalogUrl}/model_prices?select=${q}&unit=in.(${e})`,{headers:{apikey:n.anonKey,Authorization:`Bearer ${n.anonKey}`},signal:AbortSignal.timeout(ye)});if(!r.ok)throw new Error(`model_prices fetch failed: ${r.status}`);return Se(await r.json())}m();var Ye="/partners/openrouter/models",$e={chat:!0,code:!0,agent:!0,vl:!0},W={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function ke(n){let t=n.toLowerCase();return Object.hasOwn(W,t)?W[t]:null}var Q={"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 Te(n,t){if(!n||n.length===0)return[];let e=[],r=[];for(let i of n){let o=Object.hasOwn(Q,i)?Q[i]:null;if(o===null){r.push(i);continue}e.some(d=>d.region===i)||e.push({country_code:o,region:i})}return r.length>0&&t(`urun openrouter doc: shared block region(s) ${r.join(", ")} have no country mapping \u2014 omitted from datacenters, never guessed`),e}var X={qwen:"Qwen",qwen1:"Qwen",qwen2:"Qwen",qwen3:"Qwen",gemma:"Gemma",diffusiongemma:"Gemma",gpt:"GPT",glm:"GLM",deepseek:"DeepSeek",mistral:"Mistral"},Z={2:"Llama2",3:"Llama3"};function Ce(n){if(!n)return null;let t=n.split("@")[0],e=t.indexOf("/"),r=(e===-1?t:t.slice(e+1)).toLowerCase().split(/[^a-z0-9]+/).filter(i=>i.length>0);for(let i=0;i<r.length;i+=1){let o=r[i];if(Object.hasOwn(X,o))return X[o];let a=r[i+1];if(o==="llama"&&a!==void 0&&Object.hasOwn(Z,a))return Z[a];if(o==="phi"&&a!==void 0&&/^\d/.test(a))return"Phi"}return null}var Pe=["context_length","max_model_len"];function xe(n){let t=Pe.map(e=>n.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 O(n){let t=n.map(xe),e=t[0];return e==null?null:t.every(r=>r===e)?e:null}function Ae(n){let t=n.engine_args?.reasoning_parser;return t==null||t===""?!1:typeof t=="string"?!0:null}function j(n){if(n.length===0)return null;let t=n.map(Ae),e=t[0];return e==null?null:t.every(r=>r===e)?e:null}function Ee(n,t){let e=t.filter(i=>_(`${i.model_id}-${i.variant}`)===n);if(e.length>0)return M(e);let r=t.filter(i=>_(i.model_id)===n);return r.length>0?M(r):null}function M(n){let t=n[0];return n.every(r=>r.model_id===t.model_id&&r.variant===t.variant)?{row:n.length===1?t:Me(n),matches:n}:null}function Me(n){let t=n[0],e=r=>n.every(i=>(i[r]??void 0)===(t[r]??void 0))?t[r]: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 te(n){return 0}function S(n){let t={id:n.id,object:n.object,created:n.created,owned_by:n.owned_by,...n.context_length!==void 0?{context_length:n.context_length}:{},...n.pricing!==void 0?{pricing:n.pricing}:{},...n.voices!==void 0?{voices:n.voices}:{},...n.capabilities!==void 0?{capabilities:n.capabilities}:{}};return"image_modes"in n?{...t,image_modes:n.image_modes}:t}function I(n,t,e){let r=Ee(n,t);if(!r)return S({id:n,object:"model",created:0,owned_by:"urun"});let{row:i,matches:o}=r,a=O(o),d=x(i.model_id,i.variant,e),l=ne(i,o),s=j(o);return S({id:n,object:"model",created:te(i),owned_by:"urun",...a!==null?{context_length:a}:{},...d!==null?{pricing:d}:{},...l.published.length>0?{voices:[...l.published]}:{},...s!==null?{capabilities:{reasoning:s}}:{}})}var Oe=["Vivian","Serena","Uncle_Fu","Dylan","Eric","Ryan","Aiden","Ono_Anna","Sohee"];function ne(n,t){if((n.task??void 0)!=="tts")return{published:[],defect:null};let e=t.map(o=>o.engine_args?.voices),r=e[0];if(Array.isArray(r)&&r.length>0&&r.every(o=>typeof o=="string")&&e.every(o=>Array.isArray(o)&&o.length===r.length&&o.every((a,d)=>a===r[d])))return{published:[...r],defect:null};let i=e.some(o=>o!=null)?`the catalog's engine_args.voices for ${n.model_id}:${n.variant} could not be vouched (empty, malformed, or not an identical non-empty string array on every GPU placement)`:null;return n.model_id==="qwen3-tts-customvoice"?{published:[...Oe],defect:i}:{published:[],defect:i}}function U(n){let t=n.length>0?M(n):null;return t===null?{published:[],defect:null}:ne(t.row,t.matches)}function Y(n){let t=/^(\d+)(?:\.(\d+))?(?:e([+-]?\d+))?$/.exec(String(n));if(!t)throw new Error(`unrenderable per-token price: ${String(n)}`);let[,e="",r="",i=""]=t,o=e+r,a=e.length-6+(i===""?0:Number(i));return(a<=0?`0.${"0".repeat(-a)}${o}`:`${o.slice(0,a)}.${o.slice(a).padEnd(1,"0")}`).replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,"")}function ee(n){return{input:[{type:"prompt",unit:"token",cost_usd:Y(n.input)}],output:[{type:"completion",unit:"token",cost_usd:Y(n.output)}]}}function Ie(n,t,e,r){if(t.length===0)return null;let i=M(t);if(!i)return null;let{row:o,matches:a}=i;if(!o.task||$e[o.task]!==!0)return null;let d=O(a),l=x(o.model_id,o.variant,e),s=Ce(o.hf_repo),c={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};return d!==null&&(c.max_tokens={type:"integer",min:1,max:d,unit:"token"}),{schema_version:"2.4",id:`${o.model_id}:${o.variant}`,name:`${o.model_id}:${o.variant}`,created:te(o),quantization:ke(o.variant),...s!==null?{tokenizer:s}:{},description:o.description??"",hugging_face_id:o.hf_repo?o.hf_repo.split("@")[0]??o.hf_repo:"",is_ready:n.ready===!0&&l!==null,is_free:!1,input_modalities:[{type:"text",...d!==null?{supported_inputs:{max_context_length:{value:d,unit:"token"}}}:{},...l?{pricing:ee(l).input}:{}},...o.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:c,...l?{pricing:ee(l).output}:{}}],...r.datacenters.length>0?{datacenters:r.datacenters}:{},...r.compliance?{compliance:r.compliance}:{}}}function re(n,t,e,r){let i=r??(s=>console.warn(s.trimEnd())),o=n?$(n):[];if(o.length>0){let s=o.filter(c=>c.ready===void 0);if(s.length===o.length)i("urun openrouter doc: readiness seam absent from shared block; declaring every endpoint not ready");else if(s.length>0){let c=s.map(u=>`${u.model_id}:${u.variant}`).join(", ");i(`urun openrouter doc: readiness seam partially deployed \u2014 endpoints ${c} carry no ready field and are declared not ready`)}}let a={datacenters:Te(n?.regions,i),compliance:n?.compliance??null},d=[],l=new Set;for(let s of o){let c=`${s.model_id}:${s.variant}`;if(l.has(c))continue;l.add(c);let u=Ie(s,t.filter(f=>f.model_id===s.model_id&&f.variant===s.variant),e,a);u&&d.push(u)}return{data:d}}m();var oe={delta:!0,final:!0,turn_start:!0,turn_end:!0,turn_eager_end:!0,turn_resumed:!0},k=class extends Error{constructor(t){super(t),this.name="TurnLaneDecodeError"}};function ie(n){let t=n.t;if(typeof t!="string"||!(t in oe))throw new k(`the "stt" transcript lane delivered an unknown turn kind ${JSON.stringify(t??null)} \u2014 known kinds: ${Object.keys(oe).join(", ")}`);let e={kind:t};return typeof n.delta=="string"&&(e.text=n.delta),typeof n.start_ms=="number"&&Number.isFinite(n.start_ms)&&(e.startMs=n.start_ms),e}function Ne(n){if(typeof n=="string"){let t;try{t=JSON.parse(n)}catch(e){throw new k(`the "stt" transcript lane delivered a string payload that is not valid JSON (${e instanceof Error?e.message:String(e)})`)}if(t===null||typeof t!="object"||Array.isArray(t))throw new k(`the "stt" transcript lane delivered a JSON string that is not an object (${JSON.stringify(t)})`);return ie(t)}if(n===null||typeof n!="object"||Array.isArray(n))throw new k(`the "stt" transcript lane delivered a non-object payload (${Array.isArray(n)?"array":typeof n}) \u2014 transcript events are {"t":\u2026,"delta":\u2026,"start_ms":\u2026} objects (or their JSON serialization)`);return ie(n)}function se(n){return n.reason instanceof Error?n.reason:new Error("the turn-lane consumer aborted")}async function Le(n,t){return t.aborted?"aborted":await new Promise(e=>{let r=!1,i=a=>{r||(r=!0,t.removeEventListener("abort",o),e(a))},o=()=>i("aborted");t.addEventListener("abort",o,{once:!0}),n.then(()=>i("pulled"),()=>i("pulled"))})}function nt(n,t){let e=n.stream("stt");if(typeof e.messages!="function")throw new Error("openSessionTurnLane: session.stream('stt') has no messages() \u2014 the named-DATA stream consume seam (@urun-sh/core SessionStream.messages, contract \xA75) is required for the engine's own turn/transcript lane");async function*r(){let i=e.messages()[Symbol.asyncIterator]();try{for(;;){if(t!==void 0&&t.aborted)throw se(t);let o=i.next();if(t!==void 0&&await Le(o,t)==="aborted")throw o.catch(()=>{}),se(t);let a=await o;if(a.done)return;yield Ne(a.value)}}finally{let o=i.return?.();o&&o.catch(()=>{})}}return r()}var ct="https://api.urun.sh/v1",T=class extends Error{},E=class extends Error{},B="urs1.";function ae(n,t){return B+Buffer.from(JSON.stringify([n,t]),"utf8").toString("base64url")}function Fe(n){if(!n.startsWith(B))return null;try{let t=JSON.parse(Buffer.from(n.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 pt(n){let t=n.fetchImpl??fetch,e=`${n.apiUrl.replace(/\/+$/,"")}/apps`,r=await t(e,{headers:{Authorization:`Bearer ${n.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new Error(`org apps listing failed: GET ${e} \u2192 ${r.status}`);let i=await r.json();if(!Array.isArray(i.apps))throw new Error(`org apps listing returned no "apps" array (GET ${e})`);if(i.truncated===!0)throw new Error(`org apps listing was truncated (GET ${e} returned ${i.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return i.apps}function le(n){if(n.length===0)return[];if(!n.every(e=>(e.task??void 0)==="image"))return[];let t=Ue(n);return t===null?[]:[t]}function K(n,t,e,r,i){if(!i)throw new p(`shared model "${n}" 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 "${n}" resolved without a catalog id + variant to join on`);let o=r.filter(a=>a.model_id===t&&a.variant===e);if(o.length===0)throw new p(`shared model "${n}" 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 o}function de(n,t){if(!t.every(i=>(i.task??void 0)==="image"))return[];let e=t.map(i=>i.engine_args?.expects_image),r=e[0];if(typeof r!="boolean"||!e.every(i=>i===r))throw new p(`shared image model "${n}" 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[r===!1?"generation":"edit"]}var ge="shared:",ue="speech:";function J(n){return n.sharedApp?ge+n.appSlug:n.appSlug}function ce(n){return typeof n.modelId=="string"&&typeof n.variant=="string"?{kind:"catalog",model_id:n.modelId,variant:n.variant}:{kind:"not-a-catalog-model",model:n.appSlug}}function R(n,t){let e=t.filter(o=>_(`${o.model_id}-${o.variant}`)===n);if(e.length>0)return e;let r=t.filter(o=>_(o.model_id)===n);return new Set(r.map(o=>o.variant)).size===1?r:[]}function je(n){let t=n.engine_args?.expects_image;return t==null?"edit":t===!1?"generation":t===!0?"edit":null}function Ue(n){let t=n.map(je),e=t[0];return e!=null&&t.every(r=>r===e)?e:null}var pe={stt:"stt",tts:"tts"};function b(n){if(n.length===0)return null;let t=n[0].task??void 0;return t===void 0||pe[t]===void 0?null:n.every(e=>(e.task??void 0)===t)?pe[t]:null}function Ke(n){let t=n.map(r=>r.engine_args?.transcript_stream).filter(r=>r!=null);if(t.length===0)return"stt";let e=t[0];if(typeof e!="string"||e.length===0||!t.every(r=>r===e))throw new Error(`the model's model_catalog rows cannot vow one engine_args.transcript_stream (found ${JSON.stringify(t)}) \u2014 the field must be a non-empty string agreeing across placements; the realtime transcription lane refuses to guess the stream the engine writes`);return e}var N=6e4,fe=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??N;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??N;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??N;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??N;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(i){if(i instanceof w)throw i;return this.warn(`urun /v1/models: the per-token price surface could not be read (${String(i instanceof Error?i.message:i)}) \u2014 every model is listed UNPRICED for now; Hugging Face will show no price until the read succeeds
1
+ import{c as m}from"./chunk-YSFSRI3D.js";m();m();var p=class extends Error{},he=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;function me(n){if(typeof n!="string")return!1;let t=he.exec(n);if(!t)return!1;let[,e,r,i,o,a,d]=t.map(Number);if(!Number.isFinite(Date.parse(n)))return!1;let l=new Date(Date.UTC(e,r-1,i,o,a,d));return l.getUTCFullYear()===e&&l.getUTCMonth()===r-1&&l.getUTCDate()===i&&l.getUTCHours()===o&&l.getUTCMinutes()===a&&l.getUTCSeconds()===d}function we(n){if(n===null||typeof n!="object")return null;let t=n.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:r,regions:i,compliance:o}=t;if(typeof e!="string"||e==="")throw new p(`catalog "shared" block has no shared_org_id: ${JSON.stringify(t)}`);if(!Array.isArray(r))throw new p(`catalog "shared" block endpoints is not an array: ${JSON.stringify(t)}`);let a=r.map((d,l)=>{if(d===null||typeof d!="object")throw new p(`catalog shared endpoints[${l}] is not an object: ${JSON.stringify(d)}`);let s=d;for(let u of["model_id","variant","app_slug","function"])if(typeof s[u]!="string"||s[u].length===0)throw new p(`catalog shared endpoints[${l}] (${JSON.stringify(s)}) has no valid "${u}"`);let c=(u,g)=>{if(s[u]!==void 0&&s[u]!==null&&!g)throw new p(`catalog shared endpoints[${l}] (${s.model_id}:${s.variant}) carries a malformed "${u}": ${JSON.stringify(s[u])}`)};c("ready",typeof s.ready=="boolean"),c("ready_runtimes",typeof s.ready_runtimes=="number"&&Number.isInteger(s.ready_runtimes)&&s.ready_runtimes>=0),c("ready_since",me(s.ready_since));for(let u of["model_id","variant"]){let g=s[u];if(g.includes(":"))throw new p(`catalog shared endpoints[${l}] has "${u}" containing ":" (${JSON.stringify(g)}) \u2014 the external identity <model_id>:<variant> would be unparseable (resolveSharedModel splits at the LAST colon); platform catalog defect`)}return c("output_free",typeof s.output_free=="boolean"),s});if(i!=null&&(!Array.isArray(i)||!i.every(d=>typeof d=="string"&&d.length>0)))throw new p(`catalog "shared" block regions is malformed: ${JSON.stringify(i)}`);if(o!=null){let d=o;if(typeof o!="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(o)}`);for(let l of Object.keys(d))if(l!=="zdr"&&l!=="hipaa")throw new p(`catalog "shared" block compliance carries "${l}" beyond the declared zdr/hipaa contract: ${JSON.stringify(o)}`)}return a.length===0?null:{shared_org_id:e,endpoints:a,...i!=null?{regions:i}:{},...o!=null?{compliance:o}:{}}}function $(n){return[...n.endpoints].sort((t,e)=>t.model_id.localeCompare(e.model_id)||t.variant.localeCompare(e.variant))}function z(n,t,e){let r=(t??"").trim();if(r==="")return null;let i=r,o=e,a=r.lastIndexOf(":");a>0&&(i=r.slice(0,a),o=r.slice(a+1));let d=n.endpoints.filter(s=>s.model_id===i);if(d.length===0)return null;if(o!==void 0){let s=d.find(c=>c.variant===o);return s?{shared_org_id:n.shared_org_id,endpoint:s}:null}if(d.length===1)return{shared_org_id:n.shared_org_id,endpoint:d[0]};let l=d.filter(s=>s.compat_default===!0);if(l.length!==1)throw new p(`shared model "${i}" has ${l.length} compat_default rows (exactly one required) among variants ${d.map(s=>s.variant).join(", ")}`);return{shared_org_id:n.shared_org_id,endpoint:l[0]}}async function Je(n){let t=n.fetchImpl??fetch,e=`${n.apiUrl.replace(/\/+$/,"")}/catalog`,r=await t(e,{headers:{Authorization:`Bearer ${n.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new p(`catalog read failed: GET ${e} \u2192 ${r.status}`);return we(await r.json())}m();function _(n){return n.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}m();var w=class extends Error{name="ModelPriceError"},L="per_million_input_tokens",D="per_million_output_tokens",q="model_id,variant,unit,price_usd,effective_at,expires_at",H={per_request:!0,per_minute:!0,per_output_unit:!0},ye=5e3;function _e(n,t){let e=typeof n=="number"?n:typeof n=="string"&&n.trim()!==""?Number(n):NaN;if(!Number.isFinite(e)||e<0)throw new w(`${t}: price_usd is not a non-negative number (${JSON.stringify(n)})`);return e}function G(n,t,e){let r=typeof n=="string"?Date.parse(n):NaN;if(!Number.isFinite(r))throw new w(`${t}: ${e} is not a parseable timestamp (${JSON.stringify(n)})`);return r}function ve(n,t,e){return n===null?null:G(n,t,e)}function Re(n,t){if(n===L)return"input";if(n===D)return"output";if(typeof n=="string"&&Object.hasOwn(H,n))return null;throw new w(`${t}: unit ${JSON.stringify(n)} is neither a per-token rate (${L} | ${D}) nor a known GPU-time unit (${Object.keys(H).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 be(n,t){let e=n.reduce((i,o)=>o.effective_at>i.effective_at?o:i,n[0]),r=n.find(i=>i.effective_at===e.effective_at&&i.usd!==e.usd);return r?{reason:`two ${t} rates are effective at the same instant (${e.usd} and ${r.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 F(n,t){return`${n}\0${t}`}function Se(n){if(!Array.isArray(n))throw new w(`model_prices read is not an array: ${JSON.stringify(n)}`);let t=[];for(let e=0;e<n.length;e++){let r=n[e],i=`model_prices[${e}]`;if(r===null||typeof r!="object"||Array.isArray(r))throw new w(`${i} is not an object: ${JSON.stringify(r)}`);let o=r;for(let s of q.split(","))if(!Object.hasOwn(o,s))throw new w(`${i} has no "${s}" column: ${JSON.stringify(o)}`);let a=Re(o.unit,i);if(a===null)continue;let d=o.model_id;if(typeof d!="string"||d==="")throw new w(`${i} has no model_id (${JSON.stringify(o)})`);let l=o.variant;if(!(l===null||typeof l=="string"&&l!==""))throw new w(`${i} has a non-string variant (${JSON.stringify(l)})`);t.push({model_id:d,variant:l,side:a,usd:_e(o.price_usd,i),effective_at:G(o.effective_at,i,"effective_at"),expires_at:ve(o.expires_at,i,"expires_at")})}return t}function V(n,t=Date.now(),e){let r=new Map,i=new Map;for(let l of n){if(l.effective_at>t||l.expires_at!==null&&l.expires_at<=t)continue;let s=r.get(l.model_id);s||(s=new Map,r.set(l.model_id,s),i.set(l.model_id,new Map));let c=l.variant??"",u=s.get(c);u||(u={input:[],output:[]},s.set(c,u),i.get(l.model_id).set(c,l.variant)),u[l.side].push({effective_at:l.effective_at,usd:l.usd})}let o=[],a=[];for(let[l,s]of r){let c=s.get(""),u=(g,h,v)=>{let y=g&&g.length>0?g:c?.[h];if(!y||y.length===0)return null;let f=be(y,h);return"reason"in f?f:f.rate.usd===0?h==="output"&&v!==null&&e?.has(F(l,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 i.get(l)){let v=s.get(g),y=u(v?.input,"input",h),f=u(v?.output,"output",h);if(y!==null&&"reason"in y){a.push({model_id:l,variant:h,reason:y.reason});continue}if(f!==null&&"reason"in f){a.push({model_id:l,variant:h,reason:f.reason});continue}y===null||f===null||o.push({model_id:l,variant:h,pricing:{input:y.usd,output:f.usd}})}}let d=(l,s)=>l.model_id.localeCompare(s.model_id)||(l.variant===null?1:0)-(s.variant===null?1:0)||(l.variant??"").localeCompare(s.variant??"");return{prices:o.sort(d),refusals:a.sort(d)}}function x(n,t,e){if(e.refusals.some(o=>o.model_id===n&&o.variant===t))return null;let r=e.prices.find(o=>o.model_id===n&&o.variant===t);if(r)return r.pricing;if(e.refusals.some(o=>o.model_id===n&&o.variant===null))return null;let i=e.prices.find(o=>o.model_id===n&&o.variant===null);return i?i.pricing:null}var A={prices:[],refusals:[]};async function Ge(n){let t=n.fetchImpl??fetch,e=`${L},${D}`,r=await t(`${n.catalogUrl}/model_prices?select=${q}&unit=in.(${e})`,{headers:{apikey:n.anonKey,Authorization:`Bearer ${n.anonKey}`},signal:AbortSignal.timeout(ye)});if(!r.ok)throw new Error(`model_prices fetch failed: ${r.status}`);return Se(await r.json())}m();var Ye="/partners/openrouter/models",$e={chat:!0,code:!0,agent:!0,vl:!0},W={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function Te(n){let t=n.toLowerCase();return Object.hasOwn(W,t)?W[t]:null}var Q={"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 ke(n,t){if(!n||n.length===0)return[];let e=[],r=[];for(let i of n){let o=Object.hasOwn(Q,i)?Q[i]:null;if(o===null){r.push(i);continue}e.some(d=>d.region===i)||e.push({country_code:o,region:i})}return r.length>0&&t(`urun openrouter doc: shared block region(s) ${r.join(", ")} have no country mapping \u2014 omitted from datacenters, never guessed`),e}var X={qwen:"Qwen",qwen1:"Qwen",qwen2:"Qwen",qwen3:"Qwen",gemma:"Gemma",diffusiongemma:"Gemma",gpt:"GPT",glm:"GLM",deepseek:"DeepSeek",mistral:"Mistral"},Z={2:"Llama2",3:"Llama3"};function Ce(n){if(!n)return null;let t=n.split("@")[0],e=t.indexOf("/"),r=(e===-1?t:t.slice(e+1)).toLowerCase().split(/[^a-z0-9]+/).filter(i=>i.length>0);for(let i=0;i<r.length;i+=1){let o=r[i];if(Object.hasOwn(X,o))return X[o];let a=r[i+1];if(o==="llama"&&a!==void 0&&Object.hasOwn(Z,a))return Z[a];if(o==="phi"&&a!==void 0&&/^\d/.test(a))return"Phi"}return null}var Pe=["context_length","max_model_len"];function xe(n){let t=Pe.map(e=>n.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 O(n){let t=n.map(xe),e=t[0];return e==null?null:t.every(r=>r===e)?e:null}function Ae(n){let t=n.engine_args?.reasoning_parser;return t==null||t===""?!1:typeof t=="string"?!0:null}function j(n){if(n.length===0)return null;let t=n.map(Ae),e=t[0];return e==null?null:t.every(r=>r===e)?e:null}function Ee(n,t){let e=t.filter(i=>_(`${i.model_id}-${i.variant}`)===n);if(e.length>0)return M(e);let r=t.filter(i=>_(i.model_id)===n);return r.length>0?M(r):null}function M(n){let t=n[0];return n.every(r=>r.model_id===t.model_id&&r.variant===t.variant)?{row:n.length===1?t:Me(n),matches:n}:null}function Me(n){let t=n[0],e=r=>n.every(i=>(i[r]??void 0)===(t[r]??void 0))?t[r]: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 te(n){return 0}function S(n){let t={id:n.id,object:n.object,created:n.created,owned_by:n.owned_by,...n.context_length!==void 0?{context_length:n.context_length}:{},...n.pricing!==void 0?{pricing:n.pricing}:{},...n.voices!==void 0?{voices:n.voices}:{},...n.capabilities!==void 0?{capabilities:n.capabilities}:{}};return"image_modes"in n?{...t,image_modes:n.image_modes}:t}function I(n,t,e){let r=Ee(n,t);if(!r)return S({id:n,object:"model",created:0,owned_by:"urun"});let{row:i,matches:o}=r,a=O(o),d=x(i.model_id,i.variant,e),l=ne(i,o),s=j(o);return S({id:n,object:"model",created:te(i),owned_by:"urun",...a!==null?{context_length:a}:{},...d!==null?{pricing:d}:{},...l.published.length>0?{voices:[...l.published]}:{},...s!==null?{capabilities:{reasoning:s}}:{}})}var Oe=["Vivian","Serena","Uncle_Fu","Dylan","Eric","Ryan","Aiden","Ono_Anna","Sohee"];function ne(n,t){if((n.task??void 0)!=="tts")return{published:[],defect:null};let e=t.map(o=>o.engine_args?.voices),r=e[0];if(Array.isArray(r)&&r.length>0&&r.every(o=>typeof o=="string")&&e.every(o=>Array.isArray(o)&&o.length===r.length&&o.every((a,d)=>a===r[d])))return{published:[...r],defect:null};let i=e.some(o=>o!=null)?`the catalog's engine_args.voices for ${n.model_id}:${n.variant} could not be vouched (empty, malformed, or not an identical non-empty string array on every GPU placement)`:null;return n.model_id==="qwen3-tts-customvoice"?{published:[...Oe],defect:i}:{published:[],defect:i}}function U(n){let t=n.length>0?M(n):null;return t===null?{published:[],defect:null}:ne(t.row,t.matches)}function Y(n){let t=/^(\d+)(?:\.(\d+))?(?:e([+-]?\d+))?$/.exec(String(n));if(!t)throw new Error(`unrenderable per-token price: ${String(n)}`);let[,e="",r="",i=""]=t,o=e+r,a=e.length-6+(i===""?0:Number(i));return(a<=0?`0.${"0".repeat(-a)}${o}`:`${o.slice(0,a)}.${o.slice(a).padEnd(1,"0")}`).replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,"")}function ee(n){return{input:[{type:"prompt",unit:"token",cost_usd:Y(n.input)}],output:[{type:"completion",unit:"token",cost_usd:Y(n.output)}]}}function Ie(n,t,e,r){if(t.length===0)return null;let i=M(t);if(!i)return null;let{row:o,matches:a}=i;if(!o.task||$e[o.task]!==!0)return null;let d=O(a),l=x(o.model_id,o.variant,e),s=Ce(o.hf_repo),c={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};return d!==null&&(c.max_tokens={type:"integer",min:1,max:d,unit:"token"}),{schema_version:"2.4",id:`${o.model_id}:${o.variant}`,name:`${o.model_id}:${o.variant}`,created:te(o),quantization:Te(o.variant),...s!==null?{tokenizer:s}:{},description:o.description??"",hugging_face_id:o.hf_repo?o.hf_repo.split("@")[0]??o.hf_repo:"",is_ready:n.ready===!0&&l!==null,is_free:!1,input_modalities:[{type:"text",...d!==null?{supported_inputs:{max_context_length:{value:d,unit:"token"}}}:{},...l?{pricing:ee(l).input}:{}},...o.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:c,...l?{pricing:ee(l).output}:{}}],...r.datacenters.length>0?{datacenters:r.datacenters}:{},...r.compliance?{compliance:r.compliance}:{}}}function re(n,t,e,r){let i=r??(s=>console.warn(s.trimEnd())),o=n?$(n):[];if(o.length>0){let s=o.filter(c=>c.ready===void 0);if(s.length===o.length)i("urun openrouter doc: readiness seam absent from shared block; declaring every endpoint not ready");else if(s.length>0){let c=s.map(u=>`${u.model_id}:${u.variant}`).join(", ");i(`urun openrouter doc: readiness seam partially deployed \u2014 endpoints ${c} carry no ready field and are declared not ready`)}}let a={datacenters:ke(n?.regions,i),compliance:n?.compliance??null},d=[],l=new Set;for(let s of o){let c=`${s.model_id}:${s.variant}`;if(l.has(c))continue;l.add(c);let u=Ie(s,t.filter(g=>g.model_id===s.model_id&&g.variant===s.variant),e,a);u&&d.push(u)}return{data:d}}m();var oe={delta:!0,final:!0,turn_start:!0,turn_end:!0,turn_eager_end:!0,turn_resumed:!0},T=class extends Error{constructor(t){super(t),this.name="TurnLaneDecodeError"}};function ie(n){let t=n.t;if(typeof t!="string"||!(t in oe))throw new T(`the "stt" transcript lane delivered an unknown turn kind ${JSON.stringify(t??null)} \u2014 known kinds: ${Object.keys(oe).join(", ")}`);let e={kind:t};return typeof n.delta=="string"&&(e.text=n.delta),typeof n.start_ms=="number"&&Number.isFinite(n.start_ms)&&(e.startMs=n.start_ms),e}function Ne(n){if(typeof n=="string"){let t;try{t=JSON.parse(n)}catch(e){throw new T(`the "stt" transcript lane delivered a string payload that is not valid JSON (${e instanceof Error?e.message:String(e)})`)}if(t===null||typeof t!="object"||Array.isArray(t))throw new T(`the "stt" transcript lane delivered a JSON string that is not an object (${JSON.stringify(t)})`);return ie(t)}if(n===null||typeof n!="object"||Array.isArray(n))throw new T(`the "stt" transcript lane delivered a non-object payload (${Array.isArray(n)?"array":typeof n}) \u2014 transcript events are {"t":\u2026,"delta":\u2026,"start_ms":\u2026} objects (or their JSON serialization)`);return ie(n)}function se(n){return n.reason instanceof Error?n.reason:new Error("the turn-lane consumer aborted")}async function Le(n,t){return t.aborted?"aborted":await new Promise(e=>{let r=!1,i=a=>{r||(r=!0,t.removeEventListener("abort",o),e(a))},o=()=>i("aborted");t.addEventListener("abort",o,{once:!0}),n.then(()=>i("pulled"),()=>i("pulled"))})}function nt(n,t){let e=n.stream("stt");if(typeof e.messages!="function")throw new Error("openSessionTurnLane: session.stream('stt') has no messages() \u2014 the named-DATA stream consume seam (@urun-sh/core SessionStream.messages, contract \xA75) is required for the engine's own turn/transcript lane");async function*r(){let i=e.messages()[Symbol.asyncIterator]();try{for(;;){if(t!==void 0&&t.aborted)throw se(t);let o=i.next();if(t!==void 0&&await Le(o,t)==="aborted")throw o.catch(()=>{}),se(t);let a=await o;if(a.done)return;yield Ne(a.value)}}finally{let o=i.return?.();o&&o.catch(()=>{})}}return r()}var ct="https://api.urun.sh/v1",k=class extends Error{},E=class extends Error{},B="urs1.";function ae(n,t){return B+Buffer.from(JSON.stringify([n,t]),"utf8").toString("base64url")}function Fe(n){if(!n.startsWith(B))return null;try{let t=JSON.parse(Buffer.from(n.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 pt(n){let t=n.fetchImpl??fetch,e=`${n.apiUrl.replace(/\/+$/,"")}/apps`,r=await t(e,{headers:{Authorization:`Bearer ${n.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new Error(`org apps listing failed: GET ${e} \u2192 ${r.status}`);let i=await r.json();if(!Array.isArray(i.apps))throw new Error(`org apps listing returned no "apps" array (GET ${e})`);if(i.truncated===!0)throw new Error(`org apps listing was truncated (GET ${e} returned ${i.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return i.apps}function le(n){if(n.length===0)return[];if(!n.every(e=>(e.task??void 0)==="image"))return[];let t=Ue(n);return t===null?[]:[t]}function K(n,t,e,r,i){if(!i)throw new p(`shared model "${n}" 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 "${n}" resolved without a catalog id + variant to join on`);let o=r.filter(a=>a.model_id===t&&a.variant===e);if(o.length===0)throw new p(`shared model "${n}" 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 o}function de(n,t){if(!t.every(i=>(i.task??void 0)==="image"))return[];let e=t.map(i=>i.engine_args?.expects_image),r=e[0];if(typeof r!="boolean"||!e.every(i=>i===r))throw new p(`shared image model "${n}" 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[r===!1?"generation":"edit"]}var fe="shared:",ue="speech:";function J(n){return n.sharedApp?fe+n.appSlug:n.appSlug}function ce(n){return typeof n.modelId=="string"&&typeof n.variant=="string"?{kind:"catalog",model_id:n.modelId,variant:n.variant}:{kind:"not-a-catalog-model",model:n.appSlug}}function R(n,t){let e=t.filter(o=>_(`${o.model_id}-${o.variant}`)===n);if(e.length>0)return e;let r=t.filter(o=>_(o.model_id)===n);return new Set(r.map(o=>o.variant)).size===1?r:[]}function je(n){let t=n.engine_args?.expects_image;return t==null?"edit":t===!1?"generation":t===!0?"edit":null}function Ue(n){let t=n.map(je),e=t[0];return e!=null&&t.every(r=>r===e)?e:null}var pe={stt:"stt",tts:"tts"};function b(n){if(n.length===0)return null;let t=n[0].task??void 0;return t===void 0||pe[t]===void 0?null:n.every(e=>(e.task??void 0)===t)?pe[t]:null}function Ke(n){let t=n.map(r=>r.engine_args?.transcript_stream).filter(r=>r!=null);if(t.length===0)return"stt";let e=t[0];if(typeof e!="string"||e.length===0||!t.every(r=>r===e))throw new Error(`the model's model_catalog rows cannot vow one engine_args.transcript_stream (found ${JSON.stringify(t)}) \u2014 the field must be a non-empty string agreeing across placements; the realtime transcription lane refuses to guess the stream the engine writes`);return e}var N=6e4,ge=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??N;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??N;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??N;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??N;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(i){if(i instanceof w)throw i;return this.warn(`urun /v1/models: the per-token price surface could not be read (${String(i instanceof Error?i.message:i)}) \u2014 every model is listed UNPRICED for now; Hugging Face will show no price until the read succeeds
2
2
  `),A}let r=V(e,Date.now(),t);for(let i of r.refusals)this.warn(`urun /v1/models: refusing to price ${i.model_id}:${i.variant??"*"} \u2014 ${i.reason}
3
- `);return r}outputFreeKeys(t){let e=new Set;if(!t)return e;for(let r of t.endpoints)r.output_free===!0&&e.add(F(r.model_id,r.variant));return e}warn(t){(this.opts.warn??(r=>console.warn(r.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 i of this.servable(await this.deployedApps()))e.add(i);for(let i of this.pool.keys())i.startsWith(ge)||i.startsWith(ue)||e.add(i);let r=[...e].filter(i=>i!==t).sort();return t===null?r:[t,...r]}async noDefaultApp(t){let e=await this.availableIds(),r=e.length>0?e.join(", "):"(none deployed)";throw new T(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: ${r}`:`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: ${r}`)}async resolveTarget(t){let e=(t??"").trim(),r=this.opts.defaultApp;if(!e||e==="urun")return r===null?this.noDefaultApp(e):{appSlug:r};let i=_(e);if(i===r)return{appSlug:r};if(this.pool.has(i))return{appSlug:i};if(this.opts.listApps){let o=await this.deployedApps(),a=this.servable(o);if(a.includes(i))return{appSlug:i};let d=a.filter(s=>s.startsWith(`${i}-`));if(d.length===1)return{appSlug:d[0]};if(d.length>1)throw new T(`model "${e}" is ambiguous across deployed apps ${d.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let l=o.find(s=>s.app_slug===i||s.app_slug.startsWith(`${i}-`));if(l)throw new T(`model "${e}" maps to app "${l.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 o=await this.sharedBlock();if(o){let a=z(o,e);if(a)return{appSlug:a.endpoint.app_slug,fnName:a.endpoint.function,sharedApp:!0,modelId:a.endpoint.model_id,variant:a.endpoint.variant,sharedOrgId:o.shared_org_id}}}if(this.opts.listCatalog){let a=(await this.opts.listCatalog()).find(d=>e===d.model_id||e===`${d.model_id}:${d.variant}`||i===_(d.model_id)||i===_(`${d.model_id}-${d.variant}`));if(a)throw new T(`model "${e}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${a.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return r===null?this.noDefaultApp(e):{appSlug:r}}async catalogIdentityFor(t){let e=ce(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 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,ue+J(e))}async acquire(t,e){let r=this.pool.get(e);return r||(r=Promise.resolve(this.opts.openSession(t,e)),this.pool.set(e,r),r.catch(()=>this.pool.delete(e))),{app:e,entry:await r,identity:ce(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 r=e(t);if(typeof r!="string"||r.length===0)throw new Error("ModelRouter: sessionKey returned an empty identity for a pooled session");return r}async handleFor(t){let{app:e,entry:r}=await this.sessionFor(t);return{app:e,handle:ae(e,this.keyOf(r))}}async speechHandleFor(t){let{app:e,entry:r}=await this.speechSessionFor(t);return{app:e,handle:ae(e,this.keyOf(r))}}async sessionForHandle(t){let e=Fe(t);if(!e)throw new E(`malformed session handle ${JSON.stringify(t)} \u2014 not issued by this proxy`);let{app:r,key:i}=e,o=this.pool.get(r),a=o?await o.then(d=>d,()=>null):null;if(a===null)throw new E(`the session behind this handle (app "${r}") 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(a)!==i)throw new E(`the session behind this handle (app "${r}") 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:r,entry:a}}async evict(t,e){let r=this.pool.get(t);!r||await r.then(o=>o,()=>null)!==e||this.pool.get(t)!==r||(this.pool.delete(t),await this.opts.closeSession(e).catch(()=>{}))}async modelList(){let t=this.opts.sharedOnlyListing?[]:await this.availableIds(),e=await this.sharedBlock(),[r,i]=await Promise.all([this.catalogRows().then(s=>({ok:!0,rows:s}),()=>({ok:!1,rows:[]})),this.pricesForListing(this.outputFreeKeys(e))]),o=r.rows,a=r.ok?i:A;r.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 l=(e?$(e):[]).map(s=>{let c=o.filter(g=>g.model_id===s.model_id&&g.variant===s.variant),u=O(c),f=j(c),h=x(s.model_id,s.variant,a),v=U(c);return S({id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun",...u!==null?{context_length:u}:{},...h!==null?{pricing:h}:{},...v.published.length>0?{voices:[...v.published]}:{},...f!==null?{capabilities:{reasoning:f}}:{}})});return{object:"list",data:this.opts.sharedOnlyListing?l:[...t.map(s=>I(s,o,a)),...l]}}async openRouterModels(){let t=await this.sharedBlock(),[e,r]=await Promise.all([this.catalogRows(),this.pricesForListing(this.outputFreeKeys(t))]);return re(t,e,r,i=>this.warn(i))}async supportsImages(t,e){let r=await this.resolveTarget(t),i=await this.catalogRows();if(r.sharedApp){let o=K(String(t),r.modelId,r.variant,i,this.opts.listCatalog!==null);return de(`${r.modelId}:${r.variant}`,o).includes(e)}return le(R(r.appSlug,i)).includes(e)}async supportsAudio(t){let e=await this.resolveTarget(t),r=await this.catalogRows();return e.sharedApp?b(r.filter(i=>i.model_id===e.modelId&&i.variant===e.variant))!==null:b(R(e.appSlug,r))!==null}async supportsSpeech(t){let e=await this.resolveTarget(t),r=await this.catalogRows();return e.sharedApp?b(r.filter(i=>i.model_id===e.modelId&&i.variant===e.variant))==="tts":b(R(e.appSlug,r))==="tts"}async supportsTranscription(t){let e=await this.resolveTarget(t),r=await this.catalogRows();return e.sharedApp?b(r.filter(i=>i.model_id===e.modelId&&i.variant===e.variant))==="stt":b(R(e.appSlug,r))==="stt"}async transcriptStreamFor(t){let e=await this.resolveTarget(t),r=await this.catalogRows(),i=e.sharedApp?r.filter(o=>o.model_id===e.modelId&&o.variant===e.variant):R(e.appSlug,r);return Ke(i)}async speechVoices(t){let e=await this.resolveTarget(t),r=await this.catalogRows(),i=e.sharedApp?r.filter(o=>o.model_id===e.modelId&&o.variant===e.variant):R(e.appSlug,r);return U(i)}async imageModelList(){let t=await this.catalogRows(),e=A,r=[];for(let o of await this.availableIds()){let a=le(R(o,t));a.length!==0&&r.push(S({...I(o,t,e),image_modes:a}))}let i=await this.sharedBlock();if(i)for(let o of $(i)){let a=K(`${o.model_id}:${o.variant}`,o.model_id,o.variant,t,this.opts.listCatalog!==null),d=de(`${o.model_id}:${o.variant}`,a);if(d.length===0)continue;let l=S({id:`${o.model_id}:${o.variant}`,object:"model",created:0,owned_by:"urun",image_modes:d});r.push(l)}return{object:"list",data:r}}async speechToTextModelList(){let t=await this.catalogRows(),e=A,r=[];for(let o of await this.availableIds())b(R(o,t))==="stt"&&r.push(I(o,t,e));let i=await this.sharedBlock();if(i)for(let o of $(i)){let a=K(`${o.model_id}:${o.variant}`,o.model_id,o.variant,t,this.opts.listCatalog!==null);if(b(a)!=="stt")continue;let d=S({id:`${o.model_id}:${o.variant}`,object:"model",created:0,owned_by:"urun"});r.push(d)}return{object:"list",data:r}}async closeAll(){let t=[...this.pool.values()],e=[];if(await Promise.all(t.map(async r=>{try{await this.opts.closeSession(await r)}catch(i){e.push(String(i instanceof Error?i.message:i))}})),e.length>0)throw new Error(`failed to close ${e.length} pooled session(s): ${e.join("; ")}`)}};export{Je as a,Ge as b,Ye as c,nt as d,ct as e,T as f,E as g,pt as h,ue as i,J as j,fe as k};
3
+ `);return r}outputFreeKeys(t){let e=new Set;if(!t)return e;for(let r of t.endpoints)r.output_free===!0&&e.add(F(r.model_id,r.variant));return e}warn(t){(this.opts.warn??(r=>console.warn(r.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 i of this.servable(await this.deployedApps()))e.add(i);for(let i of this.pool.keys())i.startsWith(fe)||i.startsWith(ue)||e.add(i);let r=[...e].filter(i=>i!==t).sort();return t===null?r:[t,...r]}async noDefaultApp(t){let e=await this.availableIds(),r=e.length>0?e.join(", "):"(none deployed)";throw new k(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: ${r}`:`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: ${r}`)}async resolveTarget(t){let e=(t??"").trim(),r=this.opts.defaultApp;if(!e||e==="urun")return r===null?this.noDefaultApp(e):{appSlug:r};let i=_(e);if(i===r)return{appSlug:r};if(this.pool.has(i))return{appSlug:i};if(this.opts.listApps){let o=await this.deployedApps(),a=this.servable(o);if(a.includes(i))return{appSlug:i};let d=a.filter(s=>s.startsWith(`${i}-`));if(d.length===1)return{appSlug:d[0]};if(d.length>1)throw new k(`model "${e}" is ambiguous across deployed apps ${d.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let l=o.find(s=>s.app_slug===i||s.app_slug.startsWith(`${i}-`));if(l)throw new k(`model "${e}" maps to app "${l.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 o=await this.sharedBlock();if(o){let a=z(o,e);if(a)return{appSlug:a.endpoint.app_slug,fnName:a.endpoint.function,sharedApp:!0,modelId:a.endpoint.model_id,variant:a.endpoint.variant,sharedOrgId:o.shared_org_id}}}if(this.opts.listCatalog){let a=(await this.opts.listCatalog()).find(d=>e===d.model_id||e===`${d.model_id}:${d.variant}`||i===_(d.model_id)||i===_(`${d.model_id}-${d.variant}`));if(a)throw new k(`model "${e}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${a.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return r===null?this.noDefaultApp(e):{appSlug:r}}async catalogIdentityFor(t){let e=ce(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 resolveApp(t){return J(await this.resolveTarget(t))}async sessionFor(t){let e=await this.resolveTarget(t);return this.acquire(e,J(e))}async resolveTargetFor(t){return this.resolveTarget(t)}async speechSessionFor(t){let e=await this.resolveTarget(t);return this.acquire(e,ue+J(e))}async acquire(t,e){let r=this.pool.get(e);return r||(r=Promise.resolve(this.opts.openSession(t,e)),this.pool.set(e,r),r.catch(()=>this.pool.delete(e))),{app:e,entry:await r,identity:ce(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 r=e(t);if(typeof r!="string"||r.length===0)throw new Error("ModelRouter: sessionKey returned an empty identity for a pooled session");return r}async handleFor(t){let{app:e,entry:r}=await this.sessionFor(t);return{app:e,handle:ae(e,this.keyOf(r))}}async speechHandleFor(t){let{app:e,entry:r}=await this.speechSessionFor(t);return{app:e,handle:ae(e,this.keyOf(r))}}async sessionForHandle(t){let e=Fe(t);if(!e)throw new E(`malformed session handle ${JSON.stringify(t)} \u2014 not issued by this proxy`);let{app:r,key:i}=e,o=this.pool.get(r),a=o?await o.then(d=>d,()=>null):null;if(a===null)throw new E(`the session behind this handle (app "${r}") 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(a)!==i)throw new E(`the session behind this handle (app "${r}") 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:r,entry:a}}async evict(t,e){let r=this.pool.get(t);!r||await r.then(o=>o,()=>null)!==e||this.pool.get(t)!==r||(this.pool.delete(t),await this.opts.closeSession(e).catch(()=>{}))}async modelList(){let t=this.opts.sharedOnlyListing?[]:await this.availableIds(),e=await this.sharedBlock(),[r,i]=await Promise.all([this.catalogRows().then(s=>({ok:!0,rows:s}),()=>({ok:!1,rows:[]})),this.pricesForListing(this.outputFreeKeys(e))]),o=r.rows,a=r.ok?i:A;r.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 l=(e?$(e):[]).map(s=>{let c=o.filter(f=>f.model_id===s.model_id&&f.variant===s.variant),u=O(c),g=j(c),h=x(s.model_id,s.variant,a),v=U(c);return S({id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun",...u!==null?{context_length:u}:{},...h!==null?{pricing:h}:{},...v.published.length>0?{voices:[...v.published]}:{},...g!==null?{capabilities:{reasoning:g}}:{}})});return{object:"list",data:this.opts.sharedOnlyListing?l:[...t.map(s=>I(s,o,a)),...l]}}async openRouterModels(){let t=await this.sharedBlock(),[e,r]=await Promise.all([this.catalogRows(),this.pricesForListing(this.outputFreeKeys(t))]);return re(t,e,r,i=>this.warn(i))}async supportsImages(t,e){let r=await this.resolveTarget(t),i=await this.catalogRows();if(r.sharedApp){let o=K(String(t),r.modelId,r.variant,i,this.opts.listCatalog!==null);return de(`${r.modelId}:${r.variant}`,o).includes(e)}return le(R(r.appSlug,i)).includes(e)}async supportsAudio(t){let e=await this.resolveTarget(t),r=await this.catalogRows();return e.sharedApp?b(r.filter(i=>i.model_id===e.modelId&&i.variant===e.variant))!==null:b(R(e.appSlug,r))!==null}async supportsSpeech(t){let e=await this.resolveTarget(t),r=await this.catalogRows();return e.sharedApp?b(r.filter(i=>i.model_id===e.modelId&&i.variant===e.variant))==="tts":b(R(e.appSlug,r))==="tts"}async supportsTranscription(t){let e=await this.resolveTarget(t),r=await this.catalogRows();return e.sharedApp?b(r.filter(i=>i.model_id===e.modelId&&i.variant===e.variant))==="stt":b(R(e.appSlug,r))==="stt"}async transcriptStreamFor(t){let e=await this.resolveTarget(t),r=await this.catalogRows(),i=e.sharedApp?r.filter(o=>o.model_id===e.modelId&&o.variant===e.variant):R(e.appSlug,r);return Ke(i)}async speechVoices(t){let e=await this.resolveTarget(t),r=await this.catalogRows(),i=e.sharedApp?r.filter(o=>o.model_id===e.modelId&&o.variant===e.variant):R(e.appSlug,r);return U(i)}async imageModelList(){let t=await this.catalogRows(),e=A,r=[];for(let o of await this.availableIds()){let a=le(R(o,t));a.length!==0&&r.push(S({...I(o,t,e),image_modes:a}))}let i=await this.sharedBlock();if(i)for(let o of $(i)){let a=K(`${o.model_id}:${o.variant}`,o.model_id,o.variant,t,this.opts.listCatalog!==null),d=de(`${o.model_id}:${o.variant}`,a);if(d.length===0)continue;let l=S({id:`${o.model_id}:${o.variant}`,object:"model",created:0,owned_by:"urun",image_modes:d});r.push(l)}return{object:"list",data:r}}async speechToTextModelList(){let t=await this.catalogRows(),e=A,r=[];for(let o of await this.availableIds())b(R(o,t))==="stt"&&r.push(I(o,t,e));let i=await this.sharedBlock();if(i)for(let o of $(i)){let a=K(`${o.model_id}:${o.variant}`,o.model_id,o.variant,t,this.opts.listCatalog!==null);if(b(a)!=="stt")continue;let d=S({id:`${o.model_id}:${o.variant}`,object:"model",created:0,owned_by:"urun"});r.push(d)}return{object:"list",data:r}}async closeAll(){let t=[...this.pool.values()],e=[];if(await Promise.all(t.map(async r=>{try{await this.opts.closeSession(await r)}catch(i){e.push(String(i instanceof Error?i.message:i))}})),e.length>0)throw new Error(`failed to close ${e.length} pooled session(s): ${e.join("; ")}`)}};export{Je as a,Ge as b,Ye as c,nt as d,ct as e,k as f,E as g,pt as h,ue as i,J as j,ce as k,ge as l};
@@ -0,0 +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}
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
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 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};
@@ -0,0 +1,2 @@
1
+ import{a as M}from"./chunk-K23AZPI4.js";import{I as j,J as B,N as _,O as K,P as q}from"./chunk-4W3ZPIR7.js";import{a as F}from"./chunk-76BZLCGT.js";import{a as x,b as G,d as $,g as A,h as W,j as E,k as H,l as V}from"./chunk-CUDFHFNX.js";import{c as R,f as D,g as N,p as U}from"./chunk-6M5JK4YC.js";import{c as v}from"./chunk-YSFSRI3D.js";v();import{App as z,createClientToken as de,COLD_BOOT_WHEN_LIVE_TIMEOUT_MS as ue,DEFAULT_WHEN_LIVE_TIMEOUT_MS as Z,isWakingPhase as pe}from"@urun-sh/core";v();import{SessionFailedError as te}from"@urun-sh/core";var L=2;function se(e){let s=e?.type;return s==="response.created"||s==="response.in_progress"}function ie(e){let s=e?.type;return s==="response.completed"||s==="error"}function Q(e){return async function(a,d){return(async function*(){for(let l=1;l<=L;l++){let{app:p,entry:f,identity:t}=await e.sessionFor(a.model);d?.({sessionId:e.sessionIdOf?.(f)??null,identity:t});let r=!1,c=!1;try{let m=await e.createStream(f,a);for await(let g of m){if(ie(g)&&(r=!0),se(g)){if(l>1)continue}else c=!0;yield g}}catch(m){if(m instanceof te&&(m.kind==="never-live"||m.kind==="create-failed")){try{e.onAdmissionVerdict?.(p,f,m)}catch{}throw m}if(l<L&&!c){await e.evict(p,f);continue}throw m}if(r)return;if(l<L&&!c){await e.evict(p,f);continue}return}})()}}import{randomUUID as fe}from"crypto";v();var re=3,ae="llm",le=new Set(["paused","error"]);function Y(e,s){let a=e.doc(ae),d=typeof a.onConnectionState=="function",u=typeof e.onPhase=="function";if(!d&&!u)throw new Error(`session for app "${s}" exposes neither a doc connection-state surface nor onPhase \u2014 session-gone detection requires @urun-sh/core Session (SessionDocument.onConnectionState / Session.onPhase); pooling a session with no native lifecycle signal would silently swallow requests dispatched after the platform closes it`);let l=null,p=!1,f=new Set,t=[],r=c=>{if(!l){l=c;for(let m of[...f])m(c);f.clear()}};return d&&t.push(a.onConnectionState(c=>{p&&(c.consecutiveFailures<re||r(new A(`the pooled session for app "${s}" is gone: its doc backhaul failed ${c.consecutiveFailures} consecutive times (last close ${c.lastCloseCode===null?"by provider liveness reap":`code ${c.lastCloseCode}`}${c.lastCloseReason?`, reason ${JSON.stringify(c.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`)))})),u&&t.push(e.onPhase(c=>{if(c.name==="live"){p=!0;return}p&&le.has(c.name)&&r(new A(`the pooled session for app "${s}" lost its compute binding (phase "${c.name}"). The entry is evicted; retry to get a fresh session. The named session itself is not over \u2014 it stays attachable by name.`))})),{gone:()=>l,onGone(c){return l?(c(l),()=>{}):(f.add(c),()=>f.delete(c))},dispose(){f.clear();for(let c of t.splice(0))c()}}}async function*C(e,s){let a=s.gone();if(a)throw a;let d=e[Symbol.asyncIterator](),u,l=new Promise((p,f)=>{u=s.onGone(t=>f(t))});l.catch(()=>{});try{for(;;){let p=await Promise.race([d.next(),l]);if(p.done)return;yield p.value}}finally{u?.(),d.return?.(void 0)}}var me=1,ge=4,he=4;function He(e,s){let a=(p,f)=>{let t=(e[p]??"").trim();if(!t)return f;let r=Number.parseInt(t,10);if(!Number.isInteger(r)||r<1)throw new Error(`${p} must be a positive integer (got ${JSON.stringify(e[p])})`);return r},d=(e.URUN_DECISION_POOL_MIN??"").trim();if(d==="0")return null;let u=d?Number.parseInt(d,10):me;if(!Number.isInteger(u)||u<0)throw new Error(`URUN_DECISION_POOL_MIN must be a non-negative integer (got ${JSON.stringify(e.URUN_DECISION_POOL_MIN)})`);let l=a("URUN_DECISION_POOL_MAX",Math.max(u,ge));return{min:u,max:l,idleGraceMs:3e5,inFlightCap:a("URUN_DECISION_POOL_INFLIGHT_CAP",he),replicaId:(e.URUN_REPLICA_ID??"").trim()||s||"proxy"}}function Ve(e){let s=(e.URUN_DECISION_STREAM_IDLE_TIMEOUT_S??"").trim();if(!s)return _;let a=Number.parseInt(s,10);if(!Number.isInteger(a)||a<=0)throw new Error(`URUN_DECISION_STREAM_IDLE_TIMEOUT_S must be a positive integer (got ${JSON.stringify(e.URUN_DECISION_STREAM_IDLE_TIMEOUT_S)})`);return a*1e3}function ee(e,s,a,d){let u=e[a];if(typeof u!="function")throw new Error(`app "${s}" has no function "${a}" (set URUN_FUNCTION)`);let l=d?u(void 0,d):u();if(typeof l.end!="function")throw new Error(`app "${s}" function "${a}" returned a session without end()`);return l}function oe(e,s,a,d){let{baseUrl:u,orgId:l,auth:p}=e,f=s.fnName??e.fnName,t=e.sessionStartOptions?.(a),r=d||t||s.sharedApp?{...s.sharedApp?{sharedApp:!0}:{},...t,...d}:void 0,c=o=>({session:o,responses:new F(o),gone:Y(o,a)});if(p.lane==="jwt"){let o=z(s.appSlug,{baseUrl:u,orgId:l,jwt:p.jwt});return c(ee(o,s.appSlug,f,r))}let{apiKey:m,gatewayUrl:g}=p,w=(typeof e.subject=="function"?e.subject():e.subject)??fe(),n=z(s.appSlug,{baseUrl:u,orgId:l,getAccessToken:async()=>(await de(m,{baseUrl:g,expiresIn:300,allowedFunctions:[`${s.appSlug}/${f}`],subject:w,...s.sharedApp?{sharedApp:!0}:{}})).token});return c(ee(n,s.appSlug,f,r))}function we(e){let s=e.session.id;if(typeof s!="string"||s.length===0)throw new Error("pooled session exposes no stable session id \u2014 the session-identity seam requires @urun-sh/core Session (session.id)");return s}function Se(e){if(e.kind==="never-live")return!0;if(e.kind!=="create-failed")return!1;let s=e.status.error?.httpStatus,a=e.status.error?.code;return s===410||s===409&&(a==="cancelled"||a==="expired")?!0:a==="client_ended"}var ye=new Set(["error","paused"]);function je(e){let{auth:s,catalog:a}=e,d=null,u=(p,f)=>{let t=oe(e,p,f);return e.onSessionOpen&&typeof t.session.id=="string"&&t.session.id!==""&&e.onSessionOpen(f,t.session.id),t.gone.onGone(()=>{d?.evict(f,t)}),t},l=new V({sharedOnlyListing:e.sharedOnlyListing===!0,defaultApp:e.defaultApp,fnName:e.fnName,openSession:u,closeSession:async p=>{if(p.gone.dispose(),e.release==="detach"){if(typeof p.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');p.session.detach();return}await p.session.end()},listApps:s.lane==="api-key"?()=>W({apiUrl:e.apiUrl,apiKey:s.apiKey}):null,listCatalog:a?()=>M(a):null,listPrices:a?()=>G(a):null,shared:s.lane==="api-key"?()=>x({apiUrl:e.apiUrl,apiKey:s.apiKey}):null,sessionKey:we});return d=l,e.defaultApp!==null&&l.seed(e.defaultApp,u({appSlug:e.defaultApp},E({appSlug:e.defaultApp}))),l}function Be(e,s){let a=e.decisionPool;if(!a)throw new Error("wireDecisionPool called without BackhaulConfig.decisionPool");let d=a.replicaId??"proxy",u=new Map,l=new Map,p=t=>{process.stderr.write(`[decision-pool] ${t}
2
+ `)},f=(t,r)=>{let c=u.get(r);if(c)return c;let m=new B({min:a.min,max:a.max,idleGraceMs:a.idleGraceMs,inFlightCap:a.inFlightCap,replicaId:d,openSlot:async g=>{let w=oe(e,t,r,{concurrencyKey:g.concurrencyKey});return w.gone.onGone(()=>m.evict(w)),await b(w.session),p(`slot attached pool=${r} slot=${g.concurrencyKey} session=${w.session.id??"(no id)"}`),w},closeSlot:async g=>{g.gone.dispose(),await g.session.end()},onDetach:(g,w)=>p(`slot detached pool=${r} slot=${g.concurrencyKey} reason=${w}`)});return u.set(r,m),m};return{async boot(){if(e.defaultApp===null)return;let t={appSlug:e.defaultApp};await f(t,E(t)).boot()},async sessionFor(t){let r=await s.resolveTargetFor(t),c=E(r),m=f(r,c).tryAcquireReady();if(!m)throw new j;let g=l.get(m.entry)??[];return g.push(m),l.set(m.entry,g),{app:c,entry:m.entry,identity:H(r)}},async warmFor(t){let r=await s.resolveTargetFor(t);await f(r,E(r)).boot()},end(t){let r=l.get(t)?.shift();r&&(l.get(t)?.length===0&&l.delete(t),r.release())},evict(t){for(let r of u.values())r.evict(t)},reap(){for(let t of u.values())t.reap()},async pingReady(t){for(let r of u.values())if(await r.pingReady(t))return!0;return!1},stats(){return[...u.entries()].map(([t,r])=>({poolKey:t,...r.stats()}))},async drain(t){await Promise.all([...u.values()].map(r=>r.drain({timeoutMs:t})))},async close(){await Promise.all([...u.values()].map(t=>t.close()))}}}function ve(e){return pe(e?.name)?e?.runtime?.wake?.expectedWakePath==="portable-golden-restore"?Z:ue:Z}async function b(e,s){let a=e.whenLive;if(typeof a!="function")throw new Error("pooled session exposes no native whenLive() \u2014 the audio lane must wait for the session's own admission (cold wake: queued \u2192 demand \u2192 promote) before wiring media; a session without the core Session.whenLive surface cannot be served audio");await a.call(e,{timeout:ve(e.phase),signal:s})}async function ne(e){if(typeof e.cancel=="function")try{await e.cancel();return}catch{}if(typeof e.detach!="function")throw new Error("pooled session exposes neither cancel() nor detach() \u2014 the client-gone abandon path requires the @urun-sh/core Session native lifecycle surface");e.detach()}function O(e){return e.reason instanceof Error?e.reason:new Error("the client socket closed")}function Ke(e,s=N,a,d,u=_){let l=new WeakMap,p=new WeakMap,f=new WeakMap,t=new WeakMap,r=new WeakMap,c=n=>{let o=n.session.id;return typeof o=="string"&&o.length>0?o:null},m=n=>{let o=l.get(n);if(!o){let i={waiting:0,gone:new AbortController};r.set(n,i),o=(async()=>{if(await b(n.session,i.gone.signal),i.gone.signal.aborted)throw O(i.gone.signal);r.get(n)===i&&r.delete(n);let h=await R(n.session,await s());return Object.assign(h,{sessionId:n.session.id})})(),l.set(n,o),o.catch(()=>{l.get(n)===o&&l.delete(n)}),o.then(()=>{r.get(n)===i&&r.delete(n)},()=>{r.get(n)===i&&r.delete(n)})}return o},g=n=>{let o=t.get(n);return o||(o=(async()=>{let i=await m(n);return{appendInputAudio:h=>i.appendInputAudio(h),onOutputAudio:h=>i.onOutputAudio(h),sessionId:c(n)}})(),t.set(n,o),o.catch(()=>{t.get(n)===o&&t.delete(n)})),o},w=async(n,o,i)=>{if(i?.aborted){let S=r.get(n);throw(l.get(n)===void 0||S!==void 0&&S.waiting===0)&&await ne(n.session),O(i)}let h=o(n),y=r.get(n);return y!==void 0&&y.waiting++,i===void 0?h:new Promise((S,P)=>{let T=()=>{y!==void 0&&--y.waiting===0&&r.get(n)===y&&(y.gone.abort(),r.delete(n),l.delete(n),t.delete(n),ne(n.session).catch(()=>{})),P(O(i))};i.addEventListener("abort",T,{once:!0});let k=()=>i.removeEventListener("abort",T);h.then(I=>{k(),S(I)},I=>{k(),P(I)})})};return{createResponse:Q({sessionFor:d?n=>d.sessionFor(n):n=>e.sessionFor(n),sessionIdOf:n=>c(n),evict:(n,o)=>{d?d.evict(o):e.evict(n,o)},onAdmissionVerdict:(n,o,i)=>{Se(i)&&(d?d.evict(o):e.evict(n,o),a?.(n,i.kind==="never-live"?"never-live":"create-failed"))},createStream:async(n,o)=>{try{await b(n.session);let i=K(C(await n.responses.responses.create(o),n.gone),u);if(!d)return i;let h=n;return(async function*(){try{yield*i}catch(S){throw q(S)&&d.evict(h),S}finally{d.end(h)}})()}catch(i){throw d&&d.end(n),i}}}),listModels:()=>e.modelList(),openRouterModels:()=>e.openRouterModels(),supportsImages:(n,o)=>e.supportsImages(n,o),listImageModels:()=>e.imageModelList(),catalogIdentity:n=>e.catalogIdentityFor(n),servingAppSlug:async n=>await e.servingAppSlugFor(n),supportsAudio:n=>e.supportsAudio(n),supportsSpeech:n=>e.supportsSpeech(n),speechVoices:n=>e.speechVoices(n),supportsTranscription:n=>e.supportsTranscription(n),transcriptStreamFor:n=>e.transcriptStreamFor(n),listSpeechToTextModels:()=>e.speechToTextModelList(),openAudio:async(n,o)=>{let{entry:i}=await e.sessionFor(n);return await w(i,m,o)},openAudioOn:async(n,o)=>{let{entry:i}=await e.sessionForHandle(n);return await w(i,g,o)},openTurnsOn:async function*(n,o){if(o?.aborted)throw O(o);let{entry:i}=await e.sessionForHandle(n);yield*$(i.session,o)},openVideo:async n=>{let{entry:o}=await e.sessionFor(n),i=p.get(o);return i||(i=D(o.session),p.set(o,i)),i},openVideoOut:async n=>{let{entry:o}=await e.sessionFor(n),i=f.get(o);return i||(i=U(o.session),f.set(o,i)),i},sessionHandle:async n=>(await e.handleFor(n)).handle,speechSessionHandle:async n=>(await e.speechHandleFor(n)).handle,evictSpeechSession:async n=>{let{app:o,entry:i}=await e.sessionForHandle(n);await e.evict(o,i)},createResponseOn:async(n,o)=>{let{entry:i}=await e.sessionForHandle(n);return await b(i.session),C(await i.responses.responses.create(o),i.gone)},onSessionEnd:async(n,o)=>{let{entry:i}=await e.sessionForHandle(n),h=i.session;if(typeof h.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let y=!1;return h.onPhase(S=>{if(y||!ye.has(S.name))return;y=!0;let P=h.endsAt??null;o({timeLeftMs:P?Math.max(0,P.getTime()-Date.now()):null,reason:S.reason??`session ${S.name}`})})}}}export{He as a,Ve as b,je as c,Be as d,Ke as e};