@urun-sh/openai 0.6.16 → 0.6.18

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 +0,0 @@
1
- import{c as y}from"./chunk-YSFSRI3D.js";y();y();var p=class extends Error{},ue=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;function ce(o){if(typeof o!="string")return!1;let t=ue.exec(o);if(!t)return!1;let[,e,n,r,i,l,d]=t.map(Number);if(!Number.isFinite(Date.parse(o)))return!1;let a=new Date(Date.UTC(e,n-1,r,i,l,d));return a.getUTCFullYear()===e&&a.getUTCMonth()===n-1&&a.getUTCDate()===r&&a.getUTCHours()===i&&a.getUTCMinutes()===l&&a.getUTCSeconds()===d}function pe(o){if(o===null||typeof o!="object")return null;let t=o.shared;if(t==null)return null;if(typeof t!="object")throw new p(`catalog "shared" block is ${typeof t}, expected an object`);let{shared_org_id:e,endpoints:n,regions:r,compliance:i}=t;if(typeof e!="string"||e==="")throw new p(`catalog "shared" block has no shared_org_id: ${JSON.stringify(t)}`);if(!Array.isArray(n))throw new p(`catalog "shared" block endpoints is not an array: ${JSON.stringify(t)}`);let l=n.map((d,a)=>{if(d===null||typeof d!="object")throw new p(`catalog shared endpoints[${a}] is not an object: ${JSON.stringify(d)}`);let s=d;for(let c of["model_id","variant","app_slug","function"])if(typeof s[c]!="string"||s[c].length===0)throw new p(`catalog shared endpoints[${a}] (${JSON.stringify(s)}) has no valid "${c}"`);let u=(c,g)=>{if(s[c]!==void 0&&s[c]!==null&&!g)throw new p(`catalog shared endpoints[${a}] (${s.model_id}:${s.variant}) carries a malformed "${c}": ${JSON.stringify(s[c])}`)};u("ready",typeof s.ready=="boolean"),u("ready_runtimes",typeof s.ready_runtimes=="number"&&Number.isInteger(s.ready_runtimes)&&s.ready_runtimes>=0),u("ready_since",ce(s.ready_since));for(let c of["model_id","variant"]){let g=s[c];if(g.includes(":"))throw new p(`catalog shared endpoints[${a}] has "${c}" containing ":" (${JSON.stringify(g)}) \u2014 the external identity <model_id>:<variant> would be unparseable (resolveSharedModel splits at the LAST colon); platform catalog defect`)}return u("output_free",typeof s.output_free=="boolean"),s});if(r!=null&&(!Array.isArray(r)||!r.every(d=>typeof d=="string"&&d.length>0)))throw new p(`catalog "shared" block regions is malformed: ${JSON.stringify(r)}`);if(i!=null){let d=i;if(typeof i!="object"||typeof d.zdr!="boolean"||typeof d.hipaa!="boolean")throw new p(`catalog "shared" block compliance is malformed (operator-declared zdr + hipaa booleans required): ${JSON.stringify(i)}`);for(let a of Object.keys(d))if(a!=="zdr"&&a!=="hipaa")throw new p(`catalog "shared" block compliance carries "${a}" beyond the declared zdr/hipaa contract: ${JSON.stringify(i)}`)}return l.length===0?null:{shared_org_id:e,endpoints:l,...r!=null?{regions:r}:{},...i!=null?{compliance:i}:{}}}function S(o){return[...o.endpoints].sort((t,e)=>t.model_id.localeCompare(e.model_id)||t.variant.localeCompare(e.variant))}function K(o,t,e){let n=(t??"").trim();if(n==="")return null;let r=n,i=e,l=n.lastIndexOf(":");l>0&&(r=n.slice(0,l),i=n.slice(l+1));let d=o.endpoints.filter(s=>s.model_id===r);if(d.length===0)return null;if(i!==void 0){let s=d.find(u=>u.variant===i);return s?{shared_org_id:o.shared_org_id,endpoint:s}:null}if(d.length===1)return{shared_org_id:o.shared_org_id,endpoint:d[0]};let a=d.filter(s=>s.compat_default===!0);if(a.length!==1)throw new p(`shared model "${r}" has ${a.length} compat_default rows (exactly one required) among variants ${d.map(s=>s.variant).join(", ")}`);return{shared_org_id:o.shared_org_id,endpoint:a[0]}}async function Ie(o){let t=o.fetchImpl??fetch,e=`${o.apiUrl.replace(/\/+$/,"")}/catalog`,n=await t(e,{headers:{Authorization:`Bearer ${o.apiKey}`,Accept:"application/json"}});if(!n.ok)throw new p(`catalog read failed: GET ${e} \u2192 ${n.status}`);return pe(await n.json())}y();function _(o){return o.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}y();var w=class extends Error{name="ModelPriceError"},E="per_million_input_tokens",N="per_million_output_tokens",z="model_id,variant,unit,price_usd,effective_at,expires_at",J={per_request:!0,per_minute:!0,per_output_unit:!0},ge=5e3;function fe(o,t){let e=typeof o=="number"?o:typeof o=="string"&&o.trim()!==""?Number(o):NaN;if(!Number.isFinite(e)||e<0)throw new w(`${t}: price_usd is not a non-negative number (${JSON.stringify(o)})`);return e}function H(o,t,e){let n=typeof o=="string"?Date.parse(o):NaN;if(!Number.isFinite(n))throw new w(`${t}: ${e} is not a parseable timestamp (${JSON.stringify(o)})`);return n}function he(o,t,e){return o===null?null:H(o,t,e)}function me(o,t){if(o===E)return"input";if(o===N)return"output";if(typeof o=="string"&&Object.hasOwn(J,o))return null;throw new w(`${t}: unit ${JSON.stringify(o)} is neither a per-token rate (${E} | ${N}) nor a known GPU-time unit (${Object.keys(J).join(" | ")}). Skipping it would publish no prices forever with no signal \u2014 if ENG-317 named the per-token units differently, teach this module the new names instead`)}function we(o,t){let e=o.reduce((r,i)=>i.effective_at>r.effective_at?i:r,o[0]),n=o.find(r=>r.effective_at===e.effective_at&&r.usd!==e.usd);return n?{reason:`two ${t} rates are effective at the same instant (${e.usd} and ${n.usd} USD/Mtok), so the winner would be read order \u2014 this model is served UNPRICED until one of them is expired; expire one of them`}:{rate:e}}function D(o,t){return`${o}\0${t}`}function ye(o){if(!Array.isArray(o))throw new w(`model_prices read is not an array: ${JSON.stringify(o)}`);let t=[];for(let e=0;e<o.length;e++){let n=o[e],r=`model_prices[${e}]`;if(n===null||typeof n!="object"||Array.isArray(n))throw new w(`${r} is not an object: ${JSON.stringify(n)}`);let i=n;for(let s of z.split(","))if(!Object.hasOwn(i,s))throw new w(`${r} has no "${s}" column: ${JSON.stringify(i)}`);let l=me(i.unit,r);if(l===null)continue;let d=i.model_id;if(typeof d!="string"||d==="")throw new w(`${r} has no model_id (${JSON.stringify(i)})`);let a=i.variant;if(!(a===null||typeof a=="string"&&a!==""))throw new w(`${r} has a non-string variant (${JSON.stringify(a)})`);t.push({model_id:d,variant:a,side:l,usd:fe(i.price_usd,r),effective_at:H(i.effective_at,r,"effective_at"),expires_at:he(i.expires_at,r,"expires_at")})}return t}function q(o,t=Date.now(),e){let n=new Map,r=new Map;for(let a of o){if(a.effective_at>t||a.expires_at!==null&&a.expires_at<=t)continue;let s=n.get(a.model_id);s||(s=new Map,n.set(a.model_id,s),r.set(a.model_id,new Map));let u=a.variant??"",c=s.get(u);c||(c={input:[],output:[]},s.set(u,c),r.get(a.model_id).set(u,a.variant)),c[a.side].push({effective_at:a.effective_at,usd:a.usd})}let i=[],l=[];for(let[a,s]of n){let u=s.get(""),c=(g,h,v)=>{let m=g&&g.length>0?g:u?.[h];if(!m||m.length===0)return null;let f=we(m,h);return"reason"in f?f:f.rate.usd===0?h==="output"&&v!==null&&e?.has(D(a,v))?f.rate:{reason:`the ${h} rate in force is zero, which publishes as "free" and would top :cheapest routing \u2014 this model is served UNPRICED; delete the row if the model is not token-priced, or set a real rate`}:f.rate};for(let[g,h]of r.get(a)){let v=s.get(g),m=c(v?.input,"input",h),f=c(v?.output,"output",h);if(m!==null&&"reason"in m){l.push({model_id:a,variant:h,reason:m.reason});continue}if(f!==null&&"reason"in f){l.push({model_id:a,variant:h,reason:f.reason});continue}m===null||f===null||i.push({model_id:a,variant:h,pricing:{input:m.usd,output:f.usd}})}}let d=(a,s)=>a.model_id.localeCompare(s.model_id)||(a.variant===null?1:0)-(s.variant===null?1:0)||(a.variant??"").localeCompare(s.variant??"");return{prices:i.sort(d),refusals:l.sort(d)}}function k(o,t,e){if(e.refusals.some(i=>i.model_id===o&&i.variant===t))return null;let n=e.prices.find(i=>i.model_id===o&&i.variant===t);if(n)return n.pricing;if(e.refusals.some(i=>i.model_id===o&&i.variant===null))return null;let r=e.prices.find(i=>i.model_id===o&&i.variant===null);return r?r.pricing:null}var C={prices:[],refusals:[]};async function Le(o){let t=o.fetchImpl??fetch,e=`${E},${N}`,n=await t(`${o.catalogUrl}/model_prices?select=${z}&unit=in.(${e})`,{headers:{apikey:o.anonKey,Authorization:`Bearer ${o.anonKey}`},signal:AbortSignal.timeout(ge)});if(!n.ok)throw new Error(`model_prices fetch failed: ${n.status}`);return ye(await n.json())}y();var _e={chat:!0,code:!0,agent:!0,vl:!0},G={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function ve(o){let t=o.toLowerCase();return Object.hasOwn(G,t)?G[t]:null}var V={"us-east-1":"US","us-east-2":"US","us-west-1":"US","us-west-2":"US","ca-central-1":"CA","ca-west-1":"CA","sa-east-1":"BR","eu-central-1":"DE","eu-central-2":"CH","eu-west-1":"IE","eu-west-2":"GB","eu-west-3":"FR","eu-north-1":"SE","eu-south-1":"IT","eu-south-2":"ES","af-south-1":"ZA","ap-east-1":"HK","ap-south-1":"IN","ap-south-2":"IN","ap-northeast-1":"JP","ap-northeast-2":"KR","ap-northeast-3":"JP","ap-southeast-1":"SG","ap-southeast-2":"AU","ap-southeast-3":"ID","ap-southeast-4":"AU","ap-southeast-5":"MY","ap-southeast-7":"TH","il-central-1":"IL","me-central-1":"AE","me-south-1":"BH"};function Re(o,t){if(!o||o.length===0)return[];let e=[],n=[];for(let r of o){let i=Object.hasOwn(V,r)?V[r]:null;if(i===null){n.push(r);continue}e.some(d=>d.region===r)||e.push({country_code:i,region:r})}return n.length>0&&t(`urun openrouter doc: shared block region(s) ${n.join(", ")} have no country mapping \u2014 omitted from datacenters, never guessed`),e}var W={qwen:"Qwen",qwen1:"Qwen",qwen2:"Qwen",qwen3:"Qwen",gemma:"Gemma",diffusiongemma:"Gemma",gpt:"GPT",glm:"GLM",deepseek:"DeepSeek",mistral:"Mistral"},Q={2:"Llama2",3:"Llama3"};function be(o){if(!o)return null;let t=o.split("@")[0],e=t.indexOf("/"),n=(e===-1?t:t.slice(e+1)).toLowerCase().split(/[^a-z0-9]+/).filter(r=>r.length>0);for(let r=0;r<n.length;r+=1){let i=n[r];if(Object.hasOwn(W,i))return W[i];let l=n[r+1];if(i==="llama"&&l!==void 0&&Object.hasOwn(Q,l))return Q[l];if(i==="phi"&&l!==void 0&&/^\d/.test(l))return"Phi"}return null}var Se=["context_length","max_model_len"];function $e(o){let t=Se.map(e=>o.engine_args?.[e]).filter(e=>typeof e=="number"&&Number.isFinite(e)&&e>0);return t.length===0?null:t.every(e=>e===t[0])?t[0]:null}function T(o){let t=o.map($e),e=t[0];return e==null?null:t.every(n=>n===e)?e:null}function ke(o){let t=o.engine_args?.reasoning_parser;return t==null||t===""?!1:typeof t=="string"?!0:null}function F(o){if(o.length===0)return null;let t=o.map(ke),e=t[0];return e==null?null:t.every(n=>n===e)?e:null}function Ce(o,t){let e=t.filter(r=>_(`${r.model_id}-${r.variant}`)===o);if(e.length>0)return M(e);let n=t.filter(r=>_(r.model_id)===o);return n.length>0?M(n):null}function M(o){let t=o[0];return o.every(n=>n.model_id===t.model_id&&n.variant===t.variant)?{row:o.length===1?t:Pe(o),matches:o}:null}function Pe(o){let t=o[0],e=n=>o.every(r=>(r[n]??void 0)===(t[n]??void 0))?t[n]:void 0;return{model_id:t.model_id,variant:t.variant,gpu_spec:e("gpu_spec"),engine:e("engine"),task:e("task"),description:e("description"),size_gb:e("size_gb"),hf_repo:e("hf_repo"),updated_at:e("updated_at")}}function Y(o){let t=o.updated_at?Date.parse(o.updated_at):NaN;return Number.isFinite(t)?Math.floor(t/1e3):0}function A(o,t,e){let n=Ce(o,t);if(!n)return{id:o,object:"model",created:0,owned_by:"urun"};let{row:r,matches:i}=n,l=T(i),d=k(r.model_id,r.variant,e),a=ee(r,i),s=F(i);return{id:o,object:"model",created:Y(r),owned_by:"urun",name:`${r.model_id}:${r.variant}`,...r.description?{description:r.description}:{},...r.task?{task:r.task}:{},...r.engine?{engine:r.engine}:{},...r.gpu_spec?{gpu_spec:r.gpu_spec}:{},...l!==null?{context_length:l}:{},...d!==null?{pricing:d}:{},...a.published.length>0?{voices:[...a.published]}:{},...s!==null?{capabilities:{reasoning:s}}:{}}}var xe=["Vivian","Serena","Uncle_Fu","Dylan","Eric","Ryan","Aiden","Ono_Anna","Sohee"];function ee(o,t){if((o.task??void 0)!=="tts")return{published:[],defect:null};let e=t.map(i=>i.engine_args?.voices),n=e[0];if(Array.isArray(n)&&n.length>0&&n.every(i=>typeof i=="string")&&e.every(i=>Array.isArray(i)&&i.length===n.length&&i.every((l,d)=>l===n[d])))return{published:[...n],defect:null};let r=e.some(i=>i!=null)?`the catalog's engine_args.voices for ${o.model_id}:${o.variant} could not be vouched (empty, malformed, or not an identical non-empty string array on every GPU placement)`:null;return o.model_id==="qwen3-tts-customvoice"?{published:[...xe],defect:r}:{published:[],defect:r}}function L(o){let t=o.length>0?M(o):null;return t===null?{published:[],defect:null}:ee(t.row,t.matches)}function X(o){let t=/^(\d+)(?:\.(\d+))?(?:e([+-]?\d+))?$/.exec(String(o));if(!t)throw new Error(`unrenderable per-token price: ${String(o)}`);let[,e="",n="",r=""]=t,i=e+n,l=e.length-6+(r===""?0:Number(r));return(l<=0?`0.${"0".repeat(-l)}${i}`:`${i.slice(0,l)}.${i.slice(l).padEnd(1,"0")}`).replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,"")}function Z(o){return{input:[{type:"prompt",unit:"token",cost_usd:X(o.input)}],output:[{type:"completion",unit:"token",cost_usd:X(o.output)}]}}function Oe(o,t,e,n){if(t.length===0)return null;let r=M(t);if(!r)return null;let{row:i,matches:l}=r;if(!i.task||_e[i.task]!==!0)return null;let d=T(l),a=k(i.model_id,i.variant,e),s=be(i.hf_repo),u={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};return d!==null&&(u.max_tokens={type:"integer",min:1,max:d,unit:"token"}),{schema_version:"2.4",id:`${i.model_id}:${i.variant}`,name:`${i.model_id}:${i.variant}`,created:Y(i),quantization:ve(i.variant),...s!==null?{tokenizer:s}:{},description:i.description??"",hugging_face_id:i.hf_repo?i.hf_repo.split("@")[0]??i.hf_repo:"",is_ready:o.ready===!0&&a!==null,is_free:!1,input_modalities:[{type:"text",...d!==null?{supported_inputs:{max_context_length:{value:d,unit:"token"}}}:{},...a?{pricing:Z(a).input}:{}},...i.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:u,...a?{pricing:Z(a).output}:{}}],...n.datacenters.length>0?{datacenters:n.datacenters}:{},...n.compliance?{compliance:n.compliance}:{}}}function te(o,t,e,n){let r=n??(s=>console.warn(s.trimEnd())),i=o?S(o):[];if(i.length>0){let s=i.filter(u=>u.ready===void 0);if(s.length===i.length)r("urun openrouter doc: readiness seam absent from shared block; declaring every endpoint not ready");else if(s.length>0){let u=s.map(c=>`${c.model_id}:${c.variant}`).join(", ");r(`urun openrouter doc: readiness seam partially deployed \u2014 endpoints ${u} carry no ready field and are declared not ready`)}}let l={datacenters:Re(o?.regions,r),compliance:o?.compliance??null},d=[],a=new Set;for(let s of i){let u=`${s.model_id}:${s.variant}`;if(a.has(u))continue;a.add(u);let c=Oe(s,t.filter(g=>g.model_id===s.model_id&&g.variant===s.variant),e,l);c&&d.push(c)}return{data:d}}var Xe="https://api.urun.sh/v1",$=class extends Error{},P=class extends Error{},B="urs1.";function ne(o,t){return B+Buffer.from(JSON.stringify([o,t]),"utf8").toString("base64url")}function Me(o){if(!o.startsWith(B))return null;try{let t=JSON.parse(Buffer.from(o.slice(B.length),"base64url").toString("utf8"));if(Array.isArray(t)&&typeof t[0]=="string"&&typeof t[1]=="string")return{app:t[0],key:t[1]}}catch{}return null}async function Ze(o){let t=o.fetchImpl??fetch,e=`${o.apiUrl.replace(/\/+$/,"")}/apps`,n=await t(e,{headers:{Authorization:`Bearer ${o.apiKey}`,Accept:"application/json"}});if(!n.ok)throw new Error(`org apps listing failed: GET ${e} \u2192 ${n.status}`);let r=await n.json();if(!Array.isArray(r.apps))throw new Error(`org apps listing returned no "apps" array (GET ${e})`);if(r.truncated===!0)throw new Error(`org apps listing was truncated (GET ${e} returned ${r.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return r.apps}function oe(o){if(o.length===0)return[];if(!o.every(e=>(e.task??void 0)==="image"))return[];let t=Ae(o);return t===null?[]:[t]}function U(o,t,e,n,r){if(!r)throw new p(`shared model "${o}" cannot be gated: no catalog oracle is configured, and a shared endpoint's modality must come from the model's OWN model_catalog row \u2014 never a default`);if(t===void 0||e===void 0)throw new p(`shared model "${o}" resolved without a catalog id + variant to join on`);let i=n.filter(l=>l.model_id===t&&l.variant===e);if(i.length===0)throw new p(`shared model "${o}" names endpoint ${t}:${e} but the catalog has no model_catalog row for it \u2014 the shared block cannot vouch a modality from nothing`);return i}function re(o,t){if(!t.every(r=>(r.task??void 0)==="image"))return[];let e=t.map(r=>r.engine_args?.expects_image),n=e[0];if(typeof n!="boolean"||!e.every(r=>r===n))throw new p(`shared image model "${o}" does not vouch an explicit image mode: its ${t.length} model_catalog row(s) carry engine_args.expects_image ${JSON.stringify(e)} \u2014 the shared lane requires a boolean (false = pure text-to-image generation, true = edit) and refuses to silently default to edit`);return[n===!1?"generation":"edit"]}var de="shared:",ie="speech:";function j(o){return o.sharedApp?de+o.appSlug:o.appSlug}function se(o){return typeof o.modelId=="string"&&typeof o.variant=="string"?{kind:"catalog",model_id:o.modelId,variant:o.variant}:{kind:"not-a-catalog-model",model:o.appSlug}}function b(o,t){let e=t.filter(i=>_(`${i.model_id}-${i.variant}`)===o);if(e.length>0)return e;let n=t.filter(i=>_(i.model_id)===o);return new Set(n.map(i=>i.variant)).size===1?n:[]}function Te(o){let t=o.engine_args?.expects_image;return t==null?"edit":t===!1?"generation":t===!0?"edit":null}function Ae(o){let t=o.map(Te),e=t[0];return e!=null&&t.every(n=>n===e)?e:null}var ae={stt:"stt",tts:"tts"};function R(o){if(o.length===0)return null;let t=o[0].task??void 0;return t===void 0||ae[t]===void 0?null:o.every(e=>(e.task??void 0)===t)?ae[t]:null}var I=6e4,le=class{constructor(t){this.opts=t}opts;pool=new Map;appsCache=null;rowsCache=null;seed(t,e){this.pool.set(t,Promise.resolve(e))}async deployedApps(){if(!this.opts.listApps)return[];let t=this.opts.appsTtlMs??I;if(this.appsCache&&Date.now()-this.appsCache.at<t)return this.appsCache.apps;let e=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:e},e}async sharedBlock(){if(!this.opts.shared)return null;let t=this.opts.appsTtlMs??I;if(this.sharedCache&&Date.now()-this.sharedCache.at<t)return this.sharedCache.block;let e=await this.opts.shared();return this.sharedCache={at:Date.now(),block:e},e}sharedCache=null;async catalogRows(){if(!this.opts.listCatalog)return[];let t=this.opts.appsTtlMs??I;if(this.rowsCache&&Date.now()-this.rowsCache.at<t)return this.rowsCache.rows;let e=await this.opts.listCatalog();return this.rowsCache={at:Date.now(),rows:e},e}pricesCache=null;async priceRows(){if(!this.opts.listPrices)return[];let t=this.opts.appsTtlMs??I;if(this.pricesCache&&Date.now()-this.pricesCache.at<t)return this.pricesCache.rows;let e=await this.opts.listPrices();return this.pricesCache={at:Date.now(),rows:e},e}async pricesForListing(t){let e;try{e=await this.priceRows()}catch(r){if(r instanceof w)throw r;return this.warn(`urun /v1/models: the per-token price surface could not be read (${String(r instanceof Error?r.message:r)}) \u2014 every model is listed UNPRICED for now; Hugging Face will show no price until the read succeeds
2
- `),C}let n=q(e,Date.now(),t);for(let r of n.refusals)this.warn(`urun /v1/models: refusing to price ${r.model_id}:${r.variant??"*"} \u2014 ${r.reason}
3
- `);return n}outputFreeKeys(t){let e=new Set;if(!t)return e;for(let n of t.endpoints)n.output_free===!0&&e.add(D(n.model_id,n.variant));return e}warn(t){(this.opts.warn??(n=>console.warn(n.trimEnd())))(t)}servable(t){return t.filter(e=>e.serves==="openai"&&e.deployment_status==="active").map(e=>e.app_slug)}async availableIds(){let t=this.opts.defaultApp,e=new Set(t===null?[]:[t]);if(this.opts.listApps)for(let r of this.servable(await this.deployedApps()))e.add(r);for(let r of this.pool.keys())r.startsWith(de)||r.startsWith(ie)||e.add(r);let n=[...e].filter(r=>r!==t).sort();return t===null?n:[t,...n]}async noDefaultApp(t){let e=await this.availableIds(),n=e.length>0?e.join(", "):"(none deployed)";throw new $(t===""?`no "model" was given and this endpoint has no default model \u2014 name one of your org's deployed apps that declare serves="openai"; available models: ${n}`:`model "${t}" does not name a deployed app in your org that declares serves="openai", and this endpoint has no default model to fall back to; available models: ${n}`)}async resolveTarget(t){let e=(t??"").trim(),n=this.opts.defaultApp;if(!e||e==="urun")return n===null?this.noDefaultApp(e):{appSlug:n};let r=_(e);if(r===n)return{appSlug:n};if(this.pool.has(r))return{appSlug:r};if(this.opts.listApps){let i=await this.deployedApps(),l=this.servable(i);if(l.includes(r))return{appSlug:r};let d=l.filter(s=>s.startsWith(`${r}-`));if(d.length===1)return{appSlug:d[0]};if(d.length>1)throw new $(`model "${e}" is ambiguous across deployed apps ${d.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let a=i.find(s=>s.app_slug===r||s.app_slug.startsWith(`${r}-`));if(a)throw new $(`model "${e}" maps to app "${a.app_slug}", which has no function declaring serves="openai" \u2014 add serves="openai" to its @app.function or deploy it with \`urun serve ${e}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.shared){let i=await this.sharedBlock();if(i){let l=K(i,e);if(l)return{appSlug:l.endpoint.app_slug,fnName:l.endpoint.function,sharedApp:!0,modelId:l.endpoint.model_id,variant:l.endpoint.variant,sharedOrgId:i.shared_org_id}}}if(this.opts.listCatalog){let l=(await this.opts.listCatalog()).find(d=>e===d.model_id||e===`${d.model_id}:${d.variant}`||r===_(d.model_id)||r===_(`${d.model_id}-${d.variant}`));if(l)throw new $(`model "${e}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${l.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return n===null?this.noDefaultApp(e):{appSlug:n}}async catalogIdentityFor(t){let e=se(await this.resolveTarget(t));return e.kind==="catalog"?{model_id:e.model_id,variant:e.variant}:null}async servingAppSlugFor(t){return(await this.resolveTarget(t)).appSlug}async targetFor(t){return this.resolveTarget(t)}async resolveApp(t){return j(await this.resolveTarget(t))}async sessionFor(t){let e=await this.resolveTarget(t);return this.acquire(e,j(e))}async speechSessionFor(t){let e=await this.resolveTarget(t);return this.acquire(e,ie+j(e))}async acquire(t,e){let n=this.pool.get(e);return n||(n=Promise.resolve(this.opts.openSession(t,e)),this.pool.set(e,n),n.catch(()=>this.pool.delete(e))),{app:e,entry:await n,identity:se(t)}}keyOf(t){let e=this.opts.sessionKey;if(!e)throw new Error("ModelRouter: sessionKey is not configured \u2014 the session-identity seam (handleFor/sessionForHandle) is unavailable on this router");let n=e(t);if(typeof n!="string"||n.length===0)throw new Error("ModelRouter: sessionKey returned an empty identity for a pooled session");return n}async handleFor(t){let{app:e,entry:n}=await this.sessionFor(t);return{app:e,handle:ne(e,this.keyOf(n))}}async speechHandleFor(t){let{app:e,entry:n}=await this.speechSessionFor(t);return{app:e,handle:ne(e,this.keyOf(n))}}async sessionForHandle(t){let e=Me(t);if(!e)throw new P(`malformed session handle ${JSON.stringify(t)} \u2014 not issued by this proxy`);let{app:n,key:r}=e,i=this.pool.get(n),l=i?await i.then(d=>d,()=>null):null;if(l===null)throw new P(`the session behind this handle (app "${n}") is gone \u2014 closed, evicted after its pod died, or the proxy restarted; it cannot be reattached. Reconnect without a handle to start a new session.`);if(this.keyOf(l)!==r)throw new P(`the session behind this handle (app "${n}") was replaced \u2014 the original backhaul died and a new session serves this app now; the handle's session cannot be reattached. Reconnect without a handle to start a new session.`);return{app:n,entry:l}}async evict(t,e){let n=this.pool.get(t);!n||await n.then(i=>i,()=>null)!==e||this.pool.get(t)!==n||(this.pool.delete(t),await this.opts.closeSession(e).catch(()=>{}))}async modelList(){let t=this.opts.sharedOnlyListing?[]:await this.availableIds(),e=await this.sharedBlock(),[n,r]=await Promise.all([this.catalogRows().then(s=>({ok:!0,rows:s}),()=>({ok:!1,rows:[]})),this.pricesForListing(this.outputFreeKeys(e))]),i=n.rows,l=n.ok?r:C;n.ok||this.warn(`urun /v1/models: the model catalog could not be read \u2014 entries fall back to the bare OpenAI fields AND no model is priced on either lane (pricing follows catalog identity, so a half-priced listing would flicker on Hugging Face)
4
- `);let a=(e?S(e):[]).map(s=>{let u={id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun",name:`${s.model_id}:${s.variant}`};s.gpu_spec&&(u.gpu_spec=s.gpu_spec),typeof s.warm=="number"&&(u.warm=s.warm);let c=i.filter(f=>f.model_id===s.model_id&&f.variant===s.variant),g=T(c);g!==null&&(u.context_length=g);let h=F(c);h!==null&&(u.capabilities={reasoning:h});let v=k(s.model_id,s.variant,l);v!==null&&(u.pricing=v);let m=L(c);return m.published.length>0&&(u.voices=[...m.published]),u});return{object:"list",data:this.opts.sharedOnlyListing?a:[...t.map(s=>A(s,i,l)),...a]}}async openRouterModels(){let t=await this.sharedBlock(),[e,n]=await Promise.all([this.catalogRows(),this.pricesForListing(this.outputFreeKeys(t))]);return te(t,e,n,r=>this.warn(r))}async supportsImages(t,e){let n=await this.resolveTarget(t),r=await this.catalogRows();if(n.sharedApp){let i=U(String(t),n.modelId,n.variant,r,this.opts.listCatalog!==null);return re(`${n.modelId}:${n.variant}`,i).includes(e)}return oe(b(n.appSlug,r)).includes(e)}async supportsAudio(t){let e=await this.resolveTarget(t),n=await this.catalogRows();return e.sharedApp?R(n.filter(r=>r.model_id===e.modelId&&r.variant===e.variant))!==null:R(b(e.appSlug,n))!==null}async supportsSpeech(t){let e=await this.resolveTarget(t),n=await this.catalogRows();return e.sharedApp?R(n.filter(r=>r.model_id===e.modelId&&r.variant===e.variant))==="tts":R(b(e.appSlug,n))==="tts"}async supportsTranscription(t){let e=await this.resolveTarget(t),n=await this.catalogRows();return e.sharedApp?R(n.filter(r=>r.model_id===e.modelId&&r.variant===e.variant))==="stt":R(b(e.appSlug,n))==="stt"}async speechVoices(t){let e=await this.resolveTarget(t),n=await this.catalogRows(),r=e.sharedApp?n.filter(i=>i.model_id===e.modelId&&i.variant===e.variant):b(e.appSlug,n);return L(r)}async imageModelList(){let t=await this.catalogRows(),e=C,n=[];for(let i of await this.availableIds()){let l=oe(b(i,t));l.length!==0&&n.push({...A(i,t,e),image_modes:l})}let r=await this.sharedBlock();if(r)for(let i of S(r)){let l=U(`${i.model_id}:${i.variant}`,i.model_id,i.variant,t,this.opts.listCatalog!==null),d=re(`${i.model_id}:${i.variant}`,l);if(d.length===0)continue;let a={id:`${i.model_id}:${i.variant}`,object:"model",created:0,owned_by:"urun",name:`${i.model_id}:${i.variant}`,image_modes:d};i.gpu_spec&&(a.gpu_spec=i.gpu_spec),typeof i.warm=="number"&&(a.warm=i.warm),n.push(a)}return{object:"list",data:n}}async speechToTextModelList(){let t=await this.catalogRows(),e=C,n=[];for(let i of await this.availableIds())R(b(i,t))==="stt"&&n.push(A(i,t,e));let r=await this.sharedBlock();if(r)for(let i of S(r)){let l=U(`${i.model_id}:${i.variant}`,i.model_id,i.variant,t,this.opts.listCatalog!==null);if(R(l)!=="stt")continue;let d={id:`${i.model_id}:${i.variant}`,object:"model",created:0,owned_by:"urun",name:`${i.model_id}:${i.variant}`};i.gpu_spec&&(d.gpu_spec=i.gpu_spec),typeof i.warm=="number"&&(d.warm=i.warm),n.push(d)}return{object:"list",data:n}}async closeAll(){let t=[...this.pool.values()],e=[];if(await Promise.all(t.map(async n=>{try{await this.opts.closeSession(await n)}catch(r){e.push(String(r instanceof Error?r.message:r))}})),e.length>0)throw new Error(`failed to close ${e.length} pooled session(s): ${e.join("; ")}`)}};export{Ie as a,Le as b,Xe as c,$ as d,P as e,Ze as f,ie as g,j as h,se as i,le as j};
@@ -1 +0,0 @@
1
- import{a as T}from"./chunk-K23AZPI4.js";import{a as I}from"./chunk-76BZLCGT.js";import{a as M,b as U,e as A,f as D,g as _,h as $,j}from"./chunk-TMQJY56F.js";import{c as C,f as x,g as F,p as G}from"./chunk-6M5JK4YC.js";import{c as y}from"./chunk-YSFSRI3D.js";y();import{App as B,createClientToken as ee,DEFAULT_WHEN_LIVE_TIMEOUT_MS as ne}from"@urun-sh/core";y();import{SessionFailedError as J}from"@urun-sh/core";var P=2;function Q(e){let t=e?.type;return t==="response.created"||t==="response.in_progress"}function X(e){let t=e?.type;return t==="response.completed"||t==="error"}function W(e){return async function(i,p){return(async function*(){for(let d=1;d<=P;d++){let{app:c,entry:l,identity:r}=await e.sessionFor(i.model);p?.({sessionId:e.sessionIdOf?.(l)??null,identity:r});let m=!1,a=!1;try{let f=await e.createStream(l,i);for await(let g of f){if(X(g)&&(m=!0),Q(g)){if(d>1)continue}else a=!0;yield g}}catch(f){if(f instanceof J&&(f.kind==="never-live"||f.kind==="create-failed")){try{e.onAdmissionVerdict?.(c,l,f)}catch{}throw f}if(d<P&&!a){await e.evict(c,l);continue}throw f}if(m)return;if(d<P&&!a){await e.evict(c,l);continue}return}})()}}y();var Y=3,z="llm",Z=new Set(["paused","error"]);function N(e,t){let i=e.doc(z),p=typeof i.onConnectionState=="function",u=typeof e.onPhase=="function";if(!p&&!u)throw new Error(`session for app "${t}" 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 d=null,c=!1,l=new Set,r=[],m=a=>{if(!d){d=a;for(let f of[...l])f(a);l.clear()}};return p&&r.push(i.onConnectionState(a=>{c&&(a.consecutiveFailures<Y||m(new A(`the pooled session for app "${t}" is gone: its doc backhaul failed ${a.consecutiveFailures} consecutive times (last close ${a.lastCloseCode===null?"by provider liveness reap":`code ${a.lastCloseCode}`}${a.lastCloseReason?`, reason ${JSON.stringify(a.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&&r.push(e.onPhase(a=>{if(a.name==="live"){c=!0;return}c&&Z.has(a.name)&&m(new A(`the pooled session for app "${t}" lost its compute binding (phase "${a.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:()=>d,onGone(a){return d?(a(d),()=>{}):(l.add(a),()=>l.delete(a))},dispose(){l.clear();for(let a of r.splice(0))a()}}}async function*E(e,t){let i=t.gone();if(i)throw i;let p=e[Symbol.asyncIterator](),u,d=new Promise((c,l)=>{u=t.onGone(r=>l(r))});d.catch(()=>{});try{for(;;){let c=await Promise.race([p.next(),d]);if(c.done)return;yield c.value}}finally{u?.(),p.return?.(void 0)}}function q(e,t,i,p){let u=e[i];if(typeof u!="function")throw new Error(`app "${t}" has no function "${i}" (set URUN_FUNCTION)`);let d=p?u(void 0,p):u();if(typeof d.end!="function")throw new Error(`app "${t}" function "${i}" returned a session without end()`);return d}function oe(e,t,i){let{baseUrl:p,orgId:u,auth:d}=e,c=typeof e.subject=="function"?e.subject():e.subject,l=i.startsWith(_)&&c!==void 0?`${c}:speech`:c,r=t.fnName??e.fnName,m=e.sessionStartOptions?.(i),a=t.sharedApp?{sharedApp:!0,...m}:m,f=o=>({session:o,responses:new I(o),gone:N(o,i)});if(d.lane==="jwt"){let o=B(t.appSlug,{baseUrl:p,orgId:u,jwt:d.jwt});return f(q(o,t.appSlug,r,a))}let{apiKey:g,gatewayUrl:n}=d,s=B(t.appSlug,{baseUrl:p,orgId:u,getAccessToken:async()=>(await ee(g,{baseUrl:n,expiresIn:300,allowedFunctions:[`${t.appSlug}/${r}`],...l===void 0?{}:{subject:l},...t.sharedApp?{sharedApp:!0}:{}})).token});return f(q(s,t.appSlug,r,a))}function se(e){let t=e.session.id;if(typeof t!="string"||t.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 t}function te(e){if(e.kind==="never-live")return!0;if(e.kind!=="create-failed")return!1;let t=e.status.error?.httpStatus,i=e.status.error?.code;return t===410||t===409&&(i==="cancelled"||i==="expired")?!0:i==="client_ended"}var ie=new Set(["error","paused"]);function Pe(e){let{auth:t,catalog:i}=e,p=null,u=(c,l)=>{let r=oe(e,c,l);return e.onSessionOpen&&typeof r.session.id=="string"&&r.session.id!==""&&e.onSessionOpen(l,r.session.id),r.gone.onGone(()=>{p?.evict(l,r)}),r},d=new j({sharedOnlyListing:e.sharedOnlyListing===!0,defaultApp:e.defaultApp,fnName:e.fnName,openSession:u,closeSession:async c=>{if(c.gone.dispose(),e.release==="detach"){if(typeof c.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');c.session.detach();return}await c.session.end()},listApps:t.lane==="api-key"?()=>D({apiUrl:e.apiUrl,apiKey:t.apiKey}):null,listCatalog:i?()=>T(i):null,listPrices:i?()=>U(i):null,shared:t.lane==="api-key"?()=>M({apiUrl:e.apiUrl,apiKey:t.apiKey}):null,sessionKey:se});return p=d,e.defaultApp!==null&&e.statelessDirect!==!0&&d.seed(e.defaultApp,u({appSlug:e.defaultApp},$({appSlug:e.defaultApp}))),d}async function O(e,t){let i=e.whenLive;if(typeof i!="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 i.call(e,{timeout:ne,signal:t})}async function K(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 k(e){return e.reason instanceof Error?e.reason:new Error("the client socket closed")}function Ee(e,t=F,i,p){let u=new WeakMap,d=new WeakMap,c=new WeakMap,l=new WeakMap,r=new WeakMap,m=n=>{let s=n.session.id;return typeof s=="string"&&s.length>0?s:null},a=n=>{let s=u.get(n);if(!s){let o={waiting:0,gone:new AbortController};r.set(n,o),s=(async()=>{if(await O(n.session,o.gone.signal),o.gone.signal.aborted)throw k(o.gone.signal);r.get(n)===o&&r.delete(n);let h=await C(n.session,await t());return Object.assign(h,{sessionId:n.session.id})})(),u.set(n,s),s.catch(()=>{u.get(n)===s&&u.delete(n)}),s.then(()=>{r.get(n)===o&&r.delete(n)},()=>{r.get(n)===o&&r.delete(n)})}return s},f=n=>{let s=l.get(n);return s||(s=(async()=>{let o=await a(n);return{appendInputAudio:h=>o.appendInputAudio(h),onOutputAudio:h=>o.onOutputAudio(h),sessionId:m(n)}})(),l.set(n,s),s.catch(()=>{l.get(n)===s&&l.delete(n)})),s},g=async(n,s,o)=>{if(o?.aborted){let S=r.get(n);throw(u.get(n)===void 0||S!==void 0&&S.waiting===0)&&await K(n.session),k(o)}let h=s(n),w=r.get(n);return w!==void 0&&w.waiting++,o===void 0?h:new Promise((S,v)=>{let L=()=>{w!==void 0&&--w.waiting===0&&r.get(n)===w&&(w.gone.abort(),r.delete(n),u.delete(n),l.delete(n),K(n.session).catch(()=>{})),v(k(o))};o.addEventListener("abort",L,{once:!0});let R=()=>o.removeEventListener("abort",L);h.then(b=>{R(),S(b)},b=>{R(),v(b)})})};return{createResponse:W({sessionFor:n=>e.sessionFor(n),sessionIdOf:n=>m(n),evict:(n,s)=>e.evict(n,s),onAdmissionVerdict:(n,s,o)=>{te(o)&&(e.evict(n,s),i?.(n,o.kind==="never-live"?"never-live":"create-failed"))},createStream:async(n,s)=>(await O(n.session),E(await n.responses.responses.create(s),n.gone))}),listModels:()=>e.modelList(),openRouterModels:()=>e.openRouterModels(),supportsImages:(n,s)=>e.supportsImages(n,s),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),listSpeechToTextModels:()=>e.speechToTextModelList(),openAudio:async(n,s)=>{let{entry:o}=await e.sessionFor(n);return await g(o,a,s)},openAudioOn:async(n,s)=>{let{entry:o}=await e.sessionForHandle(n);return await g(o,f,s)},openVideo:async n=>{let{entry:s}=await e.sessionFor(n),o=d.get(s);return o||(o=x(s.session),d.set(s,o)),o},openVideoOut:async n=>{let{entry:s}=await e.sessionFor(n),o=c.get(s);return o||(o=G(s.session),c.set(s,o)),o},sessionHandle:async n=>(await e.handleFor(n)).handle,speechSessionHandle:async n=>(await e.speechHandleFor(n)).handle,evictSpeechSession:async n=>{let{app:s,entry:o}=await e.sessionForHandle(n);await e.evict(s,o)},createResponseOn:async(n,s)=>{let{entry:o}=await e.sessionForHandle(n);return await O(o.session),E(await o.responses.responses.create(s),o.gone)},onSessionEnd:async(n,s)=>{let{entry:o}=await e.sessionForHandle(n),h=o.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 w=!1;return h.onPhase(S=>{if(w||!ie.has(S.name))return;w=!0;let v=h.endsAt??null;s({timeLeftMs:v?Math.max(0,v.getTime()-Date.now()):null,reason:S.reason??`session ${S.name}`})})},...p?{stateless:p}:{}}}export{Pe as a,Ee as b};