@urun-sh/openai 0.6.22 → 0.6.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,8 +2,8 @@
2
2
  "use strict";var Ai=Object.create;var mr=Object.defineProperty;var Ii=Object.getOwnPropertyDescriptor;var Oi=Object.getOwnPropertyNames;var Ti=Object.getPrototypeOf,Pi=Object.prototype.hasOwnProperty;var Ci=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of Oi(e))!Pi.call(t,o)&&o!==n&&mr(t,o,{get:()=>e[o],enumerable:!(r=Ii(e,o))||r.enumerable});return t};var Be=(t,e,n)=>(n=t!=null?Ai(Ti(t)):{},Ci(e||!t||!t.__esModule?mr(n,"default",{value:t,enumerable:!0}):n,t));var Mi=()=>typeof document>"u"?new URL(`file:${__filename}`).href:document.currentScript&&document.currentScript.tagName.toUpperCase()==="SCRIPT"?document.currentScript.src:new URL("main.js",document.baseURI).href,O=Mi();var lr=require("child_process"),pe=require("fs"),cr=Be(require("path"),1),C=Be(require("process"),1);var ie=require("@urun-sh/core");function gr(t,e,n,r){let o=t.response??{},s={request_id:n,consumer_id:r,stream:!0,kind:"chat",messages:e};return typeof o.instructions=="string"&&(s.instructions=o.instructions),Array.isArray(o.modalities)&&(s.modalities=o.modalities),typeof o.temperature=="number"&&(s.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(s.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(s.tools=o.tools),s}function hr(t,e,n){let r={request_id:e,consumer_id:n,stream:!!t.stream,kind:"responses",input:t.input};return t.model&&(r.model=t.model),t.tools!==void 0&&(r.tools=t.tools),t.tool_choice!==void 0&&(r.tool_choice=t.tool_choice),typeof t.temperature=="number"&&(r.temperature=t.temperature),typeof t.max_output_tokens=="number"&&(r.max_output_tokens=t.max_output_tokens),typeof t.top_p=="number"&&(r.top_p=t.top_p),Array.isArray(t.stop)&&t.stop.length>0&&(r.stop=t.stop),typeof t.reasoning_effort=="string"&&(r.reasoning_effort=t.reasoning_effort),typeof t.instructions=="string"&&(r.instructions=t.instructions),t.chat_template_kwargs!==void 0&&(r.chat_template_kwargs=t.chat_template_kwargs),t.response_format!==void 0&&(r.response_format=t.response_format),typeof t.voice=="string"&&(r.voice=t.voice),typeof t.language=="string"&&(r.language=t.language),r}function yr(t){if(t instanceof Error)return{type:"error",error:{type:"urun_error",code:t.name||null,message:t.message}};if(t&&typeof t=="object"&&t.t==="error"){let e=t,n=e.body??{},r=n.error,o=r?.message??n.message??`unreadable serve error body: ${JSON.stringify(e.body??null)}`;return{type:"error",error:{type:r?.type??"urun_error",code:e.code??r?.code??null,message:o}}}return{type:"error",error:{type:"urun_error",code:null,message:String(t)}}}var Li="llm-resp";async function*hn(t,e){let n=`${Li}:${e}`,r=`resp_${e}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};let o=new Map;try{for await(let s of t.stream(n).messages()){let i=s;if(i.t==="delta"){if(typeof i.delta=="string"&&(yield{type:"response.output_text.delta",item_id:r,delta:i.delta}),typeof i.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:r,delta:i.reasoning}),Array.isArray(i.tool_calls))for(let u of i.tool_calls){let a=typeof u.index=="number"?u.index:0,d=o.get(a)??{};u.id&&(d.id=u.id),u.function?.name&&(d.name=u.function.name),o.set(a,d),yield{type:"response.function_call_arguments.delta",item_id:`fc_${e}_${a}`,tool_index:a,call_id:d.id,name:d.name,delta:u.function?.arguments??""}}}else if(i.t==="response"){yield{type:"response.completed",response:{id:r,status:"completed",...i.body}};return}else if(i.t==="error"){yield yr(i);return}}}finally{try{t.doc("llm").set({requests:{[e]:null}})}catch{}}}var xt=class t{constructor(e,n=t.DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS){this.session=e;this.docSyncTimeoutMs=n;this.sessionTag=e.sessionId??globalThis.crypto.randomUUID()}session;docSyncTimeoutMs;sessionTag;static DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS=15e3;get consumerId(){return this.session.consumerId}async ensureDocSynced(){let e=this.session.doc("llm");if(e.synced===!0||typeof e.onSynced!="function")return;let n;await new Promise((o,s)=>{let i=e.onSynced(()=>{clearTimeout(n),o()});n=setTimeout(()=>{i(),s(new Error(`[urun] llm doc plane never synced within ${this.docSyncTimeoutMs}ms \u2014 the request envelope write is withheld rather than raced into a possibly-lost pre-sync write (resumed-attach first-write-lost, prod-usw2 2026-09-23)`))},this.docSyncTimeoutMs)})}write(e){e.session_tag=this.sessionTag,this.session.doc("llm").set({requests:{[e.request_id]:{payload:e,consumer_id:e.consumer_id,stream:e.stream}}})}sendResponses(e,n){return this.writeGated(n,()=>{let r=hr(e,n,this.consumerId);return this.write(r),hn(this.session,n)})}sendResponseCreate(e,n,r){return this.writeGated(r,()=>{let o=gr(e,n,r,this.consumerId);return this.write(o),hn(this.session,r)})}async writeGated(e,n){try{await this.ensureDocSynced()}catch(r){throw new Error(`[urun] responses dial ${e} aborted before its envelope write: ${r instanceof Error?r.message:String(r)}`)}return n()}};var _r=0;function $i(){return _r+=1,`req_${Date.now().toString(36)}_${_r.toString(36)}`}var Rt=class{transport;constructor(e){this.transport=new xt(e)}responses={create:async e=>{let n=$i(),r=await this.transport.sendResponses(e,n);return Object.assign((async function*(){yield*r})(),{requestId:n})}}};var br="audio",yn=class{constructor(e,n){this.backend=e;this.opts=n}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(e){if(!this.source)throw new Error("startOutbound() not called");let n=Buffer.from(e,"base64"),r=Math.floor(n.byteLength/2),o=new Int16Array(r);for(let s=0;s<r;s++)o[s]=n.readInt16LE(s*2);this.source.onData(o)}async startInbound(e){if(e==null)throw new Error('startInbound: no downstream audio track \u2014 the runtime audio producer has not been consumed yet (session.stream("audio").track is null)');this.sink=this.backend.createSink(e),this.sink.onframe=n=>{let r=Buffer.from(n.samples.buffer,n.samples.byteOffset,n.samples.byteLength).toString("base64");for(let o of this.outHandlers)o(r)}}onOutputAudio(e){return this.outHandlers.add(e),()=>this.outHandlers.delete(e)}};async function Sr(t,e,n=1e4){let r=new yn(e,{sampleRate:24e3}),o=await r.startOutbound();await t.stream(br).attach(o);let s=await Ui(t,n);return await r.startInbound(s),r}function Ui(t,e){let n=t.stream(br);return n.track!=null?Promise.resolve(n.track):new Promise((r,o)=>{let s,i=setTimeout(()=>{s?.(),o(new Error(`enableSessionAudio: no downstream audio track within ${e}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},e),u=a=>{a!=null&&(clearTimeout(i),s?.(),r(a))};s=n.on?.("track",u),n.track!=null&&u(n.track)})}var wr="rt-video-in",vr=256e3;function kr(t){let e=t.stream(wr),n=e.emit;if(typeof n!="function")throw new Error(`enableSessionVideo: session.stream('${wr}') has no emit() \u2014 the named-DATA stream produce seam (@urun-sh/core SessionStream.emit, contract \xA75) is required for the video frame lane`);return{sendInputFrame:async r=>{if(r.byteLength===0)throw new Error("enableSessionVideo: refusing to send an empty video frame");if(r.byteLength>vr)throw new Error(`enableSessionVideo: frame is ${r.byteLength} bytes \u2014 over the ${vr}-byte data-channel budget (the SFU's 262144-byte SCTP ceiling drops oversize messages SILENTLY; send smaller/lower-quality frames)`);await n.call(e,r)}}}var Ni=24e3,tt=480,Di=111;async function Fi(){let t=await import("opusscript"),e=t.default??t;return new e(Ni,1,2048)}async function Et(){let t=await import("werift"),e=await Fi();return{createSource:()=>{let n=new t.MediaStreamTrack({kind:"audio"}),r=Math.random()*4294967295>>>0,o=Math.random()*65535&65535,s=Math.random()*4294967295>>>0,i=new Int16Array(0);return{track:n,onData:u=>{let a=new Int16Array(i.length+u.length);a.set(i,0),a.set(u,i.length);let d=0;for(;a.length-d>=tt;){let l=a.subarray(d,d+tt);d+=tt;let c=Buffer.from(l.buffer,l.byteOffset,l.byteLength),h=e.encode(c,tt);o=o+1&65535,s=s+tt>>>0;let f=new t.RtpHeader({version:2,payloadType:Di,sequenceNumber:o,timestamp:s,ssrc:r,marker:!1}),p=new t.RtpPacket(f,h);n.writeRtp(p)}i=a.slice(d)}}},createSink:n=>{let r={onframe:null};return n.onReceiveRtp.subscribe(s=>{let i;try{i=e.decode(s.payload)}catch{return}let u=new Int16Array(i.length/2);for(let a=0;a<u.length;a++)u[a]=i.readInt16LE(a*2);r.onframe?.({samples:u})}),r}}}async function xr(t){let n=await(t.fetchImpl??fetch)(`${t.catalogUrl}/model_catalog?select=model_id,variant,gpu_spec,engine,task,description,size_gb,hf_repo,engine_args,updated_at`,{headers:{apikey:t.anonKey,Authorization:`Bearer ${t.anonKey}`}});if(!n.ok)throw new Error(`model_catalog fetch failed: ${n.status}`);return await n.json()}var At=require("@urun-sh/core"),_n="rt-video-out";var Bi="h264";function qi(t,e){if(!Number.isInteger(t.seq)||t.seq<0)throw new Error(`${e}: header seq must be a non-negative integer (got ${t.seq})`);if(!Number.isInteger(t.ptsUs))throw new Error(`${e}: header ptsUs must be an integer microsecond timestamp (got ${t.ptsUs})`);if(typeof t.codec!="string"||t.codec.length===0)throw new Error(`${e}: header codec must be a non-empty string`);if(typeof t.key!="boolean")throw new Error(`${e}: header key must be a boolean`)}function ji(t){if(t.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${t.byteLength} byte(s), no header length)`);let e=t[0]|t[1]<<8;if(t.byteLength<2+e)throw new Error(`decodeVideoOutRecord: truncated record (header says ${e} bytes, ${t.byteLength-2} present)`);let n;try{n=JSON.parse(new TextDecoder().decode(t.subarray(2,2+e)))}catch(i){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(i)})`)}let r=n;if(r===null||typeof r!="object"||r.v!==1)throw new Error(`decodeVideoOutRecord: header is not spec v1 (v=${JSON.stringify(r?.v)}) \u2014 a newer producer must be met with a newer consumer, never silently misread`);let o=r;qi(o,"decodeVideoOutRecord");let s=t.subarray(2+e);if(s.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:o,au:s.slice()}}var Rr="urun.videoOut.v2-ws";function Er(t){let e=t.stream(_n);if(typeof e.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${_n}') has no messages() \u2014 the named-DATA stream consume seam (@urun-sh/core SessionStream.messages, contract \xA75) is required for the video OUT lane`);let n=new Set,r=new Set,o=new Set,s=a=>{for(let d of r)d(a)},i=!1,u=async()=>{for await(let a of e.messages()){if(!(a instanceof Uint8Array)){s(new Error(`openSessionVideoOutLane: "${_n}" delivered a non-BINARY \xA75 payload (${typeof a}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let d;try{d=ji(a)}catch(l){s(l instanceof Error?l:new Error(String(l)));continue}for(let l of n)l(d)}for(let a of o)a()};return{codec:Bi,onOutputFrame:a=>(n.add(a),i||(i=!0,u().catch(d=>s(d instanceof Error?d:new Error(String(d))))),()=>n.delete(a)),onLaneError:a=>(r.add(a),()=>r.delete(a)),onEnd:a=>(o.add(a),()=>o.delete(a))}}var Ar={delta:!0,final:!0,turn_start:!0,turn_end:!0,turn_eager_end:!0,turn_resumed:!0},qe=class extends Error{constructor(e){super(e),this.name="TurnLaneDecodeError"}};function Ir(t){let e=t.t;if(typeof e!="string"||!(e in Ar))throw new qe(`the "stt" transcript lane delivered an unknown turn kind ${JSON.stringify(e??null)} \u2014 known kinds: ${Object.keys(Ar).join(", ")}`);let n={kind:e};return typeof t.delta=="string"&&(n.text=t.delta),typeof t.start_ms=="number"&&Number.isFinite(t.start_ms)&&(n.startMs=t.start_ms),n}function Gi(t){if(typeof t=="string"){let e;try{e=JSON.parse(t)}catch(n){throw new qe(`the "stt" transcript lane delivered a string payload that is not valid JSON (${n instanceof Error?n.message:String(n)})`)}if(e===null||typeof e!="object"||Array.isArray(e))throw new qe(`the "stt" transcript lane delivered a JSON string that is not an object (${JSON.stringify(e)})`);return Ir(e)}if(t===null||typeof t!="object"||Array.isArray(t))throw new qe(`the "stt" transcript lane delivered a non-object payload (${Array.isArray(t)?"array":typeof t}) \u2014 transcript events are {"t":\u2026,"delta":\u2026,"start_ms":\u2026} objects (or their JSON serialization)`);return Ir(t)}function Or(t){return t.reason instanceof Error?t.reason:new Error("the turn-lane consumer aborted")}async function Hi(t,e){return e.aborted?"aborted":await new Promise(n=>{let r=!1,o=i=>{r||(r=!0,e.removeEventListener("abort",s),n(i))},s=()=>o("aborted");e.addEventListener("abort",s,{once:!0}),t.then(()=>o("pulled"),()=>o("pulled"))})}function Tr(t,e){let n=t.stream("stt");if(typeof n.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 o=n.messages()[Symbol.asyncIterator]();try{for(;;){if(e!==void 0&&e.aborted)throw Or(e);let s=o.next();if(e!==void 0&&await Hi(s,e)==="aborted")throw s.catch(()=>{}),Or(e);let i=await s;if(i.done)return;yield Gi(i.value)}}finally{let s=o.return?.();s&&s.catch(()=>{})}}return r()}var Pr=require("@urun-sh/core"),wn=2;function Vi(t){let e=t?.type;return e==="response.created"||e==="response.in_progress"}function Ji(t){let e=t?.type;return e==="response.completed"||e==="error"}function Cr(t){return async function(n,r){return(async function*(){for(let s=1;s<=wn;s++){let{app:i,entry:u,identity:a}=await t.sessionFor(n.model);r?.({sessionId:t.sessionIdOf?.(u)??null,identity:a});let d=!1,l=!1;try{let c=await t.createStream(u,n);for await(let h of c){if(Ji(h)&&(d=!0),Vi(h)){if(s>1)continue}else l=!0;yield h}}catch(c){if(c instanceof Pr.SessionFailedError&&(c.kind==="never-live"||c.kind==="create-failed")){try{t.onAdmissionVerdict?.(i,u,c)}catch{}throw c}if(s<wn&&!l){await t.evict(i,u);continue}throw c}if(d)return;if(s<wn&&!l){await t.evict(i,u);continue}return}})()}}var uo=require("crypto");var se=class extends Error{name="ModelPriceError"},vn="per_million_input_tokens",bn="per_million_output_tokens",Lr="model_id,variant,unit,price_usd,effective_at,expires_at",Mr={per_request:!0,per_minute:!0,per_output_unit:!0},Wi=5e3;function Ki(t,e){let n=typeof t=="number"?t:typeof t=="string"&&t.trim()!==""?Number(t):NaN;if(!Number.isFinite(n)||n<0)throw new se(`${e}: price_usd is not a non-negative number (${JSON.stringify(t)})`);return n}function $r(t,e,n){let r=typeof t=="string"?Date.parse(t):NaN;if(!Number.isFinite(r))throw new se(`${e}: ${n} is not a parseable timestamp (${JSON.stringify(t)})`);return r}function zi(t,e,n){return t===null?null:$r(t,e,n)}function Yi(t,e){if(t===vn)return"input";if(t===bn)return"output";if(typeof t=="string"&&Object.hasOwn(Mr,t))return null;throw new se(`${e}: unit ${JSON.stringify(t)} is neither a per-token rate (${vn} | ${bn}) nor a known GPU-time unit (${Object.keys(Mr).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 Xi(t,e){let n=t.reduce((o,s)=>s.effective_at>o.effective_at?s:o,t[0]),r=t.find(o=>o.effective_at===n.effective_at&&o.usd!==n.usd);return r?{reason:`two ${e} rates are effective at the same instant (${n.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:n}}function Sn(t,e){return`${t}\0${e}`}function Qi(t){if(!Array.isArray(t))throw new se(`model_prices read is not an array: ${JSON.stringify(t)}`);let e=[];for(let n=0;n<t.length;n++){let r=t[n],o=`model_prices[${n}]`;if(r===null||typeof r!="object"||Array.isArray(r))throw new se(`${o} is not an object: ${JSON.stringify(r)}`);let s=r;for(let d of Lr.split(","))if(!Object.hasOwn(s,d))throw new se(`${o} has no "${d}" column: ${JSON.stringify(s)}`);let i=Yi(s.unit,o);if(i===null)continue;let u=s.model_id;if(typeof u!="string"||u==="")throw new se(`${o} has no model_id (${JSON.stringify(s)})`);let a=s.variant;if(!(a===null||typeof a=="string"&&a!==""))throw new se(`${o} has a non-string variant (${JSON.stringify(a)})`);e.push({model_id:u,variant:a,side:i,usd:Ki(s.price_usd,o),effective_at:$r(s.effective_at,o,"effective_at"),expires_at:zi(s.expires_at,o,"expires_at")})}return e}function Ur(t,e=Date.now(),n){let r=new Map,o=new Map;for(let a of t){if(a.effective_at>e||a.expires_at!==null&&a.expires_at<=e)continue;let d=r.get(a.model_id);d||(d=new Map,r.set(a.model_id,d),o.set(a.model_id,new Map));let l=a.variant??"",c=d.get(l);c||(c={input:[],output:[]},d.set(l,c),o.get(a.model_id).set(l,a.variant)),c[a.side].push({effective_at:a.effective_at,usd:a.usd})}let s=[],i=[];for(let[a,d]of r){let l=d.get(""),c=(h,f,p)=>{let _=h&&h.length>0?h:l?.[f];if(!_||_.length===0)return null;let m=Xi(_,f);return"reason"in m?m:m.rate.usd===0?f==="output"&&p!==null&&n?.has(Sn(a,p))?m.rate:{reason:`the ${f} 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`}:m.rate};for(let[h,f]of o.get(a)){let p=d.get(h),_=c(p?.input,"input",f),m=c(p?.output,"output",f);if(_!==null&&"reason"in _){i.push({model_id:a,variant:f,reason:_.reason});continue}if(m!==null&&"reason"in m){i.push({model_id:a,variant:f,reason:m.reason});continue}_===null||m===null||s.push({model_id:a,variant:f,pricing:{input:_.usd,output:m.usd}})}}let u=(a,d)=>a.model_id.localeCompare(d.model_id)||(a.variant===null?1:0)-(d.variant===null?1:0)||(a.variant??"").localeCompare(d.variant??"");return{prices:s.sort(u),refusals:i.sort(u)}}function nt(t,e,n){if(n.refusals.some(s=>s.model_id===t&&s.variant===e))return null;let r=n.prices.find(s=>s.model_id===t&&s.variant===e);if(r)return r.pricing;if(n.refusals.some(s=>s.model_id===t&&s.variant===null))return null;let o=n.prices.find(s=>s.model_id===t&&s.variant===null);return o?o.pricing:null}var rt={prices:[],refusals:[]};async function Nr(t){let e=t.fetchImpl??fetch,n=`${vn},${bn}`,r=await e(`${t.catalogUrl}/model_prices?select=${Lr}&unit=in.(${n})`,{headers:{apikey:t.anonKey,Authorization:`Bearer ${t.anonKey}`},signal:AbortSignal.timeout(Wi)});if(!r.ok)throw new Error(`model_prices fetch failed: ${r.status}`);return Qi(await r.json())}var W=class extends Error{},Zi=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;function ea(t){if(typeof t!="string")return!1;let e=Zi.exec(t);if(!e)return!1;let[,n,r,o,s,i,u]=e.map(Number);if(!Number.isFinite(Date.parse(t)))return!1;let a=new Date(Date.UTC(n,r-1,o,s,i,u));return a.getUTCFullYear()===n&&a.getUTCMonth()===r-1&&a.getUTCDate()===o&&a.getUTCHours()===s&&a.getUTCMinutes()===i&&a.getUTCSeconds()===u}function ta(t){if(t===null||typeof t!="object")return null;let e=t.shared;if(e==null)return null;if(typeof e!="object")throw new W(`catalog "shared" block is ${typeof e}, expected an object`);let{shared_org_id:n,endpoints:r,regions:o,compliance:s}=e;if(typeof n!="string"||n==="")throw new W(`catalog "shared" block has no shared_org_id: ${JSON.stringify(e)}`);if(!Array.isArray(r))throw new W(`catalog "shared" block endpoints is not an array: ${JSON.stringify(e)}`);let i=r.map((u,a)=>{if(u===null||typeof u!="object")throw new W(`catalog shared endpoints[${a}] is not an object: ${JSON.stringify(u)}`);let d=u;for(let c of["model_id","variant","app_slug","function"])if(typeof d[c]!="string"||d[c].length===0)throw new W(`catalog shared endpoints[${a}] (${JSON.stringify(d)}) has no valid "${c}"`);let l=(c,h)=>{if(d[c]!==void 0&&d[c]!==null&&!h)throw new W(`catalog shared endpoints[${a}] (${d.model_id}:${d.variant}) carries a malformed "${c}": ${JSON.stringify(d[c])}`)};l("ready",typeof d.ready=="boolean"),l("ready_runtimes",typeof d.ready_runtimes=="number"&&Number.isInteger(d.ready_runtimes)&&d.ready_runtimes>=0),l("ready_since",ea(d.ready_since));for(let c of["model_id","variant"]){let h=d[c];if(h.includes(":"))throw new W(`catalog shared endpoints[${a}] has "${c}" containing ":" (${JSON.stringify(h)}) \u2014 the external identity <model_id>:<variant> would be unparseable (resolveSharedModel splits at the LAST colon); platform catalog defect`)}return l("output_free",typeof d.output_free=="boolean"),d});if(o!=null&&(!Array.isArray(o)||!o.every(u=>typeof u=="string"&&u.length>0)))throw new W(`catalog "shared" block regions is malformed: ${JSON.stringify(o)}`);if(s!=null){let u=s;if(typeof s!="object"||typeof u.zdr!="boolean"||typeof u.hipaa!="boolean")throw new W(`catalog "shared" block compliance is malformed (operator-declared zdr + hipaa booleans required): ${JSON.stringify(s)}`);for(let a of Object.keys(u))if(a!=="zdr"&&a!=="hipaa")throw new W(`catalog "shared" block compliance carries "${a}" beyond the declared zdr/hipaa contract: ${JSON.stringify(s)}`)}return i.length===0?null:{shared_org_id:n,endpoints:i,...o!=null?{regions:o}:{},...s!=null?{compliance:s}:{}}}function je(t){return[...t.endpoints].sort((e,n)=>e.model_id.localeCompare(n.model_id)||e.variant.localeCompare(n.variant))}function Dr(t,e,n){let r=(e??"").trim();if(r==="")return null;let o=r,s=n,i=r.lastIndexOf(":");i>0&&(o=r.slice(0,i),s=r.slice(i+1));let u=t.endpoints.filter(d=>d.model_id===o);if(u.length===0)return null;if(s!==void 0){let d=u.find(l=>l.variant===s);return d?{shared_org_id:t.shared_org_id,endpoint:d}:null}if(u.length===1)return{shared_org_id:t.shared_org_id,endpoint:u[0]};let a=u.filter(d=>d.compat_default===!0);if(a.length!==1)throw new W(`shared model "${o}" has ${a.length} compat_default rows (exactly one required) among variants ${u.map(d=>d.variant).join(", ")}`);return{shared_org_id:t.shared_org_id,endpoint:a[0]}}async function Fr(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/catalog`,r=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new W(`catalog read failed: GET ${n} \u2192 ${r.status}`);return ta(await r.json())}function he(t){return t.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}var ot="/partners/openrouter/models",na={chat:!0,code:!0,agent:!0,vl:!0},Br={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function ra(t){let e=t.toLowerCase();return Object.hasOwn(Br,e)?Br[e]:null}var qr={"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 oa(t,e){if(!t||t.length===0)return[];let n=[],r=[];for(let o of t){let s=Object.hasOwn(qr,o)?qr[o]:null;if(s===null){r.push(o);continue}n.some(u=>u.region===o)||n.push({country_code:s,region:o})}return r.length>0&&e(`urun openrouter doc: shared block region(s) ${r.join(", ")} have no country mapping \u2014 omitted from datacenters, never guessed`),n}var jr={qwen:"Qwen",qwen1:"Qwen",qwen2:"Qwen",qwen3:"Qwen",gemma:"Gemma",diffusiongemma:"Gemma",gpt:"GPT",glm:"GLM",deepseek:"DeepSeek",mistral:"Mistral"},Gr={2:"Llama2",3:"Llama3"};function sa(t){if(!t)return null;let e=t.split("@")[0],n=e.indexOf("/"),r=(n===-1?e:e.slice(n+1)).toLowerCase().split(/[^a-z0-9]+/).filter(o=>o.length>0);for(let o=0;o<r.length;o+=1){let s=r[o];if(Object.hasOwn(jr,s))return jr[s];let i=r[o+1];if(s==="llama"&&i!==void 0&&Object.hasOwn(Gr,i))return Gr[i];if(s==="phi"&&i!==void 0&&/^\d/.test(i))return"Phi"}return null}var ia=["context_length","max_model_len"];function aa(t){let e=ia.map(n=>t.engine_args?.[n]).filter(n=>typeof n=="number"&&Number.isFinite(n)&&n>0);return e.length===0?null:e.every(n=>n===e[0])?e[0]:null}function Ot(t){let e=t.map(aa),n=e[0];return n==null?null:e.every(r=>r===n)?n:null}function ua(t){let e=t.engine_args?.reasoning_parser;return e==null||e===""?!1:typeof e=="string"?!0:null}function kn(t){if(t.length===0)return null;let e=t.map(ua),n=e[0];return n==null?null:e.every(r=>r===n)?n:null}function da(t,e){let n=e.filter(o=>he(`${o.model_id}-${o.variant}`)===t);if(n.length>0)return It(n);let r=e.filter(o=>he(o.model_id)===t);return r.length>0?It(r):null}function It(t){let e=t[0];return t.every(r=>r.model_id===e.model_id&&r.variant===e.variant)?{row:t.length===1?e:la(t),matches:t}:null}function la(t){let e=t[0],n=r=>t.every(o=>(o[r]??void 0)===(e[r]??void 0))?e[r]:void 0;return{model_id:e.model_id,variant:e.variant,gpu_spec:n("gpu_spec"),engine:n("engine"),task:n("task"),description:n("description"),size_gb:n("size_gb"),hf_repo:n("hf_repo"),updated_at:n("updated_at")}}function Jr(t){return 0}function Ie(t){let e={id:t.id,object:t.object,created:t.created,owned_by:t.owned_by,...t.context_length!==void 0?{context_length:t.context_length}:{},...t.pricing!==void 0?{pricing:t.pricing}:{},...t.voices!==void 0?{voices:t.voices}:{},...t.capabilities!==void 0?{capabilities:t.capabilities}:{}};return"image_modes"in t?{...e,image_modes:t.image_modes}:e}function Tt(t,e,n){let r=da(t,e);if(!r)return Ie({id:t,object:"model",created:0,owned_by:"urun"});let{row:o,matches:s}=r,i=Ot(s),u=nt(o.model_id,o.variant,n),a=Wr(o,s),d=kn(s);return Ie({id:t,object:"model",created:Jr(o),owned_by:"urun",...i!==null?{context_length:i}:{},...u!==null?{pricing:u}:{},...a.published.length>0?{voices:[...a.published]}:{},...d!==null?{capabilities:{reasoning:d}}:{}})}var ca=["Vivian","Serena","Uncle_Fu","Dylan","Eric","Ryan","Aiden","Ono_Anna","Sohee"];function Wr(t,e){if((t.task??void 0)!=="tts")return{published:[],defect:null};let n=e.map(s=>s.engine_args?.voices),r=n[0];if(Array.isArray(r)&&r.length>0&&r.every(s=>typeof s=="string")&&n.every(s=>Array.isArray(s)&&s.length===r.length&&s.every((i,u)=>i===r[u])))return{published:[...r],defect:null};let o=n.some(s=>s!=null)?`the catalog's engine_args.voices for ${t.model_id}:${t.variant} could not be vouched (empty, malformed, or not an identical non-empty string array on every GPU placement)`:null;return t.model_id==="qwen3-tts-customvoice"?{published:[...ca],defect:o}:{published:[],defect:o}}function xn(t){let e=t.length>0?It(t):null;return e===null?{published:[],defect:null}:Wr(e.row,e.matches)}function Hr(t){let e=/^(\d+)(?:\.(\d+))?(?:e([+-]?\d+))?$/.exec(String(t));if(!e)throw new Error(`unrenderable per-token price: ${String(t)}`);let[,n="",r="",o=""]=e,s=n+r,i=n.length-6+(o===""?0:Number(o));return(i<=0?`0.${"0".repeat(-i)}${s}`:`${s.slice(0,i)}.${s.slice(i).padEnd(1,"0")}`).replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,"")}function Vr(t){return{input:[{type:"prompt",unit:"token",cost_usd:Hr(t.input)}],output:[{type:"completion",unit:"token",cost_usd:Hr(t.output)}]}}function pa(t,e,n,r){if(e.length===0)return null;let o=It(e);if(!o)return null;let{row:s,matches:i}=o;if(!s.task||na[s.task]!==!0)return null;let u=Ot(i),a=nt(s.model_id,s.variant,n),d=sa(s.hf_repo),l={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};return u!==null&&(l.max_tokens={type:"integer",min:1,max:u,unit:"token"}),{schema_version:"2.4",id:`${s.model_id}:${s.variant}`,name:`${s.model_id}:${s.variant}`,created:Jr(s),quantization:ra(s.variant),...d!==null?{tokenizer:d}:{},description:s.description??"",hugging_face_id:s.hf_repo?s.hf_repo.split("@")[0]??s.hf_repo:"",is_ready:t.ready===!0&&a!==null,is_free:!1,input_modalities:[{type:"text",...u!==null?{supported_inputs:{max_context_length:{value:u,unit:"token"}}}:{},...a?{pricing:Vr(a).input}:{}},...s.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:l,...a?{pricing:Vr(a).output}:{}}],...r.datacenters.length>0?{datacenters:r.datacenters}:{},...r.compliance?{compliance:r.compliance}:{}}}function Kr(t,e,n,r){let o=r??(d=>console.warn(d.trimEnd())),s=t?je(t):[];if(s.length>0){let d=s.filter(l=>l.ready===void 0);if(d.length===s.length)o("urun openrouter doc: readiness seam absent from shared block; declaring every endpoint not ready");else if(d.length>0){let l=d.map(c=>`${c.model_id}:${c.variant}`).join(", ");o(`urun openrouter doc: readiness seam partially deployed \u2014 endpoints ${l} carry no ready field and are declared not ready`)}}let i={datacenters:oa(t?.regions,o),compliance:t?.compliance??null},u=[],a=new Set;for(let d of s){let l=`${d.model_id}:${d.variant}`;if(a.has(l))continue;a.add(l);let c=pa(d,e.filter(h=>h.model_id===d.model_id&&h.variant===d.variant),n,i);c&&u.push(c)}return{data:u}}var eo="https://api.urun.sh/v1",N=class extends Error{},J=class extends Error{},En="urs1.";function zr(t,e){return En+Buffer.from(JSON.stringify([t,e]),"utf8").toString("base64url")}function fa(t){if(!t.startsWith(En))return null;try{let e=JSON.parse(Buffer.from(t.slice(En.length),"base64url").toString("utf8"));if(Array.isArray(e)&&typeof e[0]=="string"&&typeof e[1]=="string")return{app:e[0],key:e[1]}}catch{}return null}async function to(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/apps`,r=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new Error(`org apps listing failed: GET ${n} \u2192 ${r.status}`);let o=await r.json();if(!Array.isArray(o.apps))throw new Error(`org apps listing returned no "apps" array (GET ${n})`);if(o.truncated===!0)throw new Error(`org apps listing was truncated (GET ${n} returned ${o.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return o.apps}function Yr(t){if(t.length===0)return[];if(!t.every(n=>(n.task??void 0)==="image"))return[];let e=ga(t);return e===null?[]:[e]}function Rn(t,e,n,r,o){if(!o)throw new W(`shared model "${t}" 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(e===void 0||n===void 0)throw new W(`shared model "${t}" resolved without a catalog id + variant to join on`);let s=r.filter(i=>i.model_id===e&&i.variant===n);if(s.length===0)throw new W(`shared model "${t}" names endpoint ${e}:${n} but the catalog has no model_catalog row for it \u2014 the shared block cannot vouch a modality from nothing`);return s}function Xr(t,e){if(!e.every(o=>(o.task??void 0)==="image"))return[];let n=e.map(o=>o.engine_args?.expects_image),r=n[0];if(typeof r!="boolean"||!n.every(o=>o===r))throw new W(`shared image model "${t}" does not vouch an explicit image mode: its ${e.length} model_catalog row(s) carry engine_args.expects_image ${JSON.stringify(n)} \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 no="shared:",Qr="speech:";function ke(t){return t.sharedApp?no+t.appSlug:t.appSlug}function Ct(t){return typeof t.modelId=="string"&&typeof t.variant=="string"?{kind:"catalog",model_id:t.modelId,variant:t.variant}:{kind:"not-a-catalog-model",model:t.appSlug}}function be(t,e){let n=e.filter(s=>he(`${s.model_id}-${s.variant}`)===t);if(n.length>0)return n;let r=e.filter(s=>he(s.model_id)===t);return new Set(r.map(s=>s.variant)).size===1?r:[]}function ma(t){let e=t.engine_args?.expects_image;return e==null?"edit":e===!1?"generation":e===!0?"edit":null}function ga(t){let e=t.map(ma),n=e[0];return n!=null&&e.every(r=>r===n)?n:null}var Zr={stt:"stt",tts:"tts"};function Se(t){if(t.length===0)return null;let e=t[0].task??void 0;return e===void 0||Zr[e]===void 0?null:t.every(n=>(n.task??void 0)===e)?Zr[e]:null}function ha(t){let e=t.map(r=>r.engine_args?.transcript_stream).filter(r=>r!=null);if(e.length===0)return"stt";let n=e[0];if(typeof n!="string"||n.length===0||!e.every(r=>r===n))throw new Error(`the model's model_catalog rows cannot vow one engine_args.transcript_stream (found ${JSON.stringify(e)}) \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 n}var Pt=6e4,Mt=class{constructor(e){this.opts=e}opts;pool=new Map;appsCache=null;rowsCache=null;seed(e,n){this.pool.set(e,Promise.resolve(n))}async deployedApps(){if(!this.opts.listApps)return[];let e=this.opts.appsTtlMs??Pt;if(this.appsCache&&Date.now()-this.appsCache.at<e)return this.appsCache.apps;let n=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:n},n}async sharedBlock(){if(!this.opts.shared)return null;let e=this.opts.appsTtlMs??Pt;if(this.sharedCache&&Date.now()-this.sharedCache.at<e)return this.sharedCache.block;let n=await this.opts.shared();return this.sharedCache={at:Date.now(),block:n},n}sharedCache=null;async catalogRows(){if(!this.opts.listCatalog)return[];let e=this.opts.appsTtlMs??Pt;if(this.rowsCache&&Date.now()-this.rowsCache.at<e)return this.rowsCache.rows;let n=await this.opts.listCatalog();return this.rowsCache={at:Date.now(),rows:n},n}pricesCache=null;async priceRows(){if(!this.opts.listPrices)return[];let e=this.opts.appsTtlMs??Pt;if(this.pricesCache&&Date.now()-this.pricesCache.at<e)return this.pricesCache.rows;let n=await this.opts.listPrices();return this.pricesCache={at:Date.now(),rows:n},n}async pricesForListing(e){let n;try{n=await this.priceRows()}catch(o){if(o instanceof se)throw o;return this.warn(`urun /v1/models: the per-token price surface could not be read (${String(o instanceof Error?o.message:o)}) \u2014 every model is listed UNPRICED for now; Hugging Face will show no price until the read succeeds
3
3
  `),rt}let r=Ur(n,Date.now(),e);for(let o of r.refusals)this.warn(`urun /v1/models: refusing to price ${o.model_id}:${o.variant??"*"} \u2014 ${o.reason}
4
4
  `);return r}outputFreeKeys(e){let n=new Set;if(!e)return n;for(let r of e.endpoints)r.output_free===!0&&n.add(Sn(r.model_id,r.variant));return n}warn(e){(this.opts.warn??(r=>console.warn(r.trimEnd())))(e)}servable(e){return e.filter(n=>n.serves==="openai"&&n.deployment_status==="active").map(n=>n.app_slug)}async availableIds(){let e=this.opts.defaultApp,n=new Set(e===null?[]:[e]);if(this.opts.listApps)for(let o of this.servable(await this.deployedApps()))n.add(o);for(let o of this.pool.keys())o.startsWith(no)||o.startsWith(Qr)||n.add(o);let r=[...n].filter(o=>o!==e).sort();return e===null?r:[e,...r]}async noDefaultApp(e){let n=await this.availableIds(),r=n.length>0?n.join(", "):"(none deployed)";throw new N(e===""?`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 "${e}" 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(e){let n=(e??"").trim(),r=this.opts.defaultApp;if(!n||n==="urun")return r===null?this.noDefaultApp(n):{appSlug:r};let o=he(n);if(o===r)return{appSlug:r};if(this.pool.has(o))return{appSlug:o};if(this.opts.listApps){let s=await this.deployedApps(),i=this.servable(s);if(i.includes(o))return{appSlug:o};let u=i.filter(d=>d.startsWith(`${o}-`));if(u.length===1)return{appSlug:u[0]};if(u.length>1)throw new N(`model "${n}" is ambiguous across deployed apps ${u.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let a=s.find(d=>d.app_slug===o||d.app_slug.startsWith(`${o}-`));if(a)throw new N(`model "${n}" 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 ${n}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.shared){let s=await this.sharedBlock();if(s){let i=Dr(s,n);if(i)return{appSlug:i.endpoint.app_slug,fnName:i.endpoint.function,sharedApp:!0,modelId:i.endpoint.model_id,variant:i.endpoint.variant,sharedOrgId:s.shared_org_id}}}if(this.opts.listCatalog){let i=(await this.opts.listCatalog()).find(u=>n===u.model_id||n===`${u.model_id}:${u.variant}`||o===he(u.model_id)||o===he(`${u.model_id}-${u.variant}`));if(i)throw new N(`model "${n}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${i.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return r===null?this.noDefaultApp(n):{appSlug:r}}async catalogIdentityFor(e){let n=Ct(await this.resolveTarget(e));return n.kind==="catalog"?{model_id:n.model_id,variant:n.variant}:null}async servingAppSlugFor(e){return(await this.resolveTarget(e)).appSlug}async resolveApp(e){return ke(await this.resolveTarget(e))}async sessionFor(e){let n=await this.resolveTarget(e);return this.acquire(n,ke(n))}async resolveTargetFor(e){return this.resolveTarget(e)}async speechSessionFor(e){let n=await this.resolveTarget(e);return this.acquire(n,Qr+ke(n))}async acquire(e,n){let r=this.pool.get(n);return r||(r=Promise.resolve(this.opts.openSession(e,n)),this.pool.set(n,r),r.catch(()=>this.pool.delete(n))),{app:n,entry:await r,identity:Ct(e)}}keyOf(e){let n=this.opts.sessionKey;if(!n)throw new Error("ModelRouter: sessionKey is not configured \u2014 the session-identity seam (handleFor/sessionForHandle) is unavailable on this router");let r=n(e);if(typeof r!="string"||r.length===0)throw new Error("ModelRouter: sessionKey returned an empty identity for a pooled session");return r}async handleFor(e){let{app:n,entry:r}=await this.sessionFor(e);return{app:n,handle:zr(n,this.keyOf(r))}}async speechHandleFor(e){let{app:n,entry:r}=await this.speechSessionFor(e);return{app:n,handle:zr(n,this.keyOf(r))}}async sessionForHandle(e){let n=fa(e);if(!n)throw new J(`malformed session handle ${JSON.stringify(e)} \u2014 not issued by this proxy`);let{app:r,key:o}=n,s=this.pool.get(r),i=s?await s.then(u=>u,()=>null):null;if(i===null)throw new J(`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(i)!==o)throw new J(`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:i}}async evict(e,n){let r=this.pool.get(e);!r||await r.then(s=>s,()=>null)!==n||this.pool.get(e)!==r||(this.pool.delete(e),await this.opts.closeSession(n).catch(()=>{}))}async modelList(){let e=this.opts.sharedOnlyListing?[]:await this.availableIds(),n=await this.sharedBlock(),[r,o]=await Promise.all([this.catalogRows().then(d=>({ok:!0,rows:d}),()=>({ok:!1,rows:[]})),this.pricesForListing(this.outputFreeKeys(n))]),s=r.rows,i=r.ok?o:rt;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)
5
- `);let a=(n?je(n):[]).map(d=>{let l=s.filter(m=>m.model_id===d.model_id&&m.variant===d.variant),c=Ot(l),h=kn(l),f=nt(d.model_id,d.variant,i),p=xn(l);return Ie({id:`${d.model_id}:${d.variant}`,object:"model",created:0,owned_by:"urun",...c!==null?{context_length:c}:{},...f!==null?{pricing:f}:{},...p.published.length>0?{voices:[...p.published]}:{},...h!==null?{capabilities:{reasoning:h}}:{}})});return{object:"list",data:this.opts.sharedOnlyListing?a:[...e.map(d=>Tt(d,s,i)),...a]}}async openRouterModels(){let e=await this.sharedBlock(),[n,r]=await Promise.all([this.catalogRows(),this.pricesForListing(this.outputFreeKeys(e))]);return Kr(e,n,r,o=>this.warn(o))}async supportsImages(e,n){let r=await this.resolveTarget(e),o=await this.catalogRows();if(r.sharedApp){let s=Rn(String(e),r.modelId,r.variant,o,this.opts.listCatalog!==null);return Xr(`${r.modelId}:${r.variant}`,s).includes(n)}return Yr(be(r.appSlug,o)).includes(n)}async supportsAudio(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Se(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))!==null:Se(be(n.appSlug,r))!==null}async supportsSpeech(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Se(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))==="tts":Se(be(n.appSlug,r))==="tts"}async supportsTranscription(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Se(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))==="stt":Se(be(n.appSlug,r))==="stt"}async transcriptStreamFor(e){let n=await this.resolveTarget(e),r=await this.catalogRows(),o=n.sharedApp?r.filter(s=>s.model_id===n.modelId&&s.variant===n.variant):be(n.appSlug,r);return ha(o)}async speechVoices(e){let n=await this.resolveTarget(e),r=await this.catalogRows(),o=n.sharedApp?r.filter(s=>s.model_id===n.modelId&&s.variant===n.variant):be(n.appSlug,r);return xn(o)}async imageModelList(){let e=await this.catalogRows(),n=rt,r=[];for(let s of await this.availableIds()){let i=Yr(be(s,e));i.length!==0&&r.push(Ie({...Tt(s,e,n),image_modes:i}))}let o=await this.sharedBlock();if(o)for(let s of je(o)){let i=Rn(`${s.model_id}:${s.variant}`,s.model_id,s.variant,e,this.opts.listCatalog!==null),u=Xr(`${s.model_id}:${s.variant}`,i);if(u.length===0)continue;let a=Ie({id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun",image_modes:u});r.push(a)}return{object:"list",data:r}}async speechToTextModelList(){let e=await this.catalogRows(),n=rt,r=[];for(let s of await this.availableIds())Se(be(s,e))==="stt"&&r.push(Tt(s,e,n));let o=await this.sharedBlock();if(o)for(let s of je(o)){let i=Rn(`${s.model_id}:${s.variant}`,s.model_id,s.variant,e,this.opts.listCatalog!==null);if(Se(i)!=="stt")continue;let u=Ie({id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun"});r.push(u)}return{object:"list",data:r}}async closeAll(){let e=[...this.pool.values()],n=[];if(await Promise.all(e.map(async r=>{try{await this.opts.closeSession(await r)}catch(o){n.push(String(o instanceof Error?o.message:o))}})),n.length>0)throw new Error(`failed to close ${n.length} pooled session(s): ${n.join("; ")}`)}};var Ge=class extends Error{code;status;retryAfterSeconds;constructor(e,n,r,o){super(o),this.name="DecisionStreamFailure",this.code=e,this.status=n,this.retryAfterSeconds=r,Object.setPrototypeOf(this,new.target.prototype)}},st=class extends Ge{constructor(){super("draining",503,5,"decision pool is draining (GOAWAY): no new decisions may start; retry on the next replica")}},An=class extends Ge{constructor(e){super("acquire_timeout",429,30,`no decision slot capacity within ${e}ms (all slots at their in-flight cap)`)}},Oe=class extends Error{constructor(){super("no ready decision slot on this proxy \u2014 the request was refused fast, not fenced; retry shortly (an out-of-band slot add was submitted)"),this.name="DecisionCapacityError"}},$t=class{constructor(e){this.opts=e;if(!Number.isInteger(e.min)||e.min<0)throw new Error("decision pool min must be a non-negative integer");if(!Number.isInteger(e.max)||e.max<e.min||e.max<1)throw new Error("decision pool max must be an integer >= min and >= 1");if(e.idleGraceMs!==void 0&&e.idleGraceMs<0)throw new Error("decision pool idleGraceMs must be >= 0");if(!Number.isInteger(e.inFlightCap)||e.inFlightCap<1)throw new Error("decision pool inFlightCap must be an integer >= 1");if(!e.replicaId)throw new Error("decision pool replicaId must be non-empty");this.now=e.now??Date.now,this.idleGraceMs=e.idleGraceMs??3e5}opts;slots=[];nextIndex=0;idleGraceMs;waiters=[];now;closed=!1;draining=!1;drainComplete=null;slotKey(e){return`decision:${this.opts.replicaId}:${e}`}async boot(){if(!(this.closed||this.draining)){for(;this.slots.length<this.opts.min;)this.ensureSlot();await Promise.all(this.slots.map(e=>e.opening??Promise.resolve()))}}ensureSlot(){let e=this.nextIndex++,r={slot:{index:e,concurrencyKey:this.slotKey(e)},state:"queued",entry:null,inFlight:0,idleSince:null,opening:null};return this.slots.push(r),this.openRuntime(r),r}openRuntime(e){e.opening||(e.state="queued",e.opening=(async()=>{try{let n=await this.opts.openSlot(e.slot);if(this.closed||this.draining){await this.opts.closeSlot(n).catch(()=>{}),this.signalDrain();return}e.entry=n,e.state="attached",e.idleSince=this.now(),this.pump()}catch(n){e.entry=null,e.state="paused",e.opening=null,this.opts.onDetach?.(e.slot,n instanceof Error?n.message:String(n));return}e.opening=null})())}async acquire(){if(this.closed)throw new Error("decision pool is closed");if(this.draining)throw new st;let e=this.opts.acquireTimeoutMs??6e5,n=Date.now();for(;;){let r=this.tryLease();if(r)return r;if(this.draining)throw new st;if(!this.slots.some(a=>a.state==="queued")&&this.slots.length<this.opts.max){this.ensureSlot();continue}let s=n+e-Date.now();if(s<=0)throw new An(e);let i,u={resolve:()=>{},settled:!1};try{await Promise.race([new Promise(a=>{u.resolve=a,this.waiters.push(u)}),new Promise(a=>{i=setTimeout(a,s)})])}finally{clearTimeout(i);let a=this.waiters.indexOf(u);a!==-1&&this.waiters.splice(a,1),u.settled=!0}if(this.closed)throw new Error("decision pool is closed");if(this.draining)throw new st}}tryAcquireReady(){if(this.closed)return null;this.restoreMin();let e=this.tryLease();return e||(!this.slots.some(n=>n.state==="queued")&&this.slots.length<this.opts.max&&this.ensureSlot(),null)}async pingReady(e){if(this.closed)return!1;for(let n=0;n<this.slots.length;n++){let r=this.slots[n];if(r.state!=="attached"||r.entry===null)continue;let o=r.entry;if(o===null||typeof o!="object"||!("session"in o))return!1;let s=o.session;if(typeof s!="object"||s===null||!("whenLive"in s))return!1;let i=s.whenLive;if(typeof i!="function")return!1;let u=r.entry;try{return await i.call(s,{timeout:e}),!0}catch{this.evict(u),r.entry!==null&&r.entry!==u&&n--;continue}}return!1}tryLease(){this.restoreMin();let e=null;for(let s of this.slots)s.state!=="attached"||s.inFlight>=this.opts.inFlightCap||(!e||s.inFlight<e.inFlight)&&(e=s);if(!e)return null;let n=e.entry;if(n===null)return null;let r=e;r.inFlight+=1;let o=!1;return{slot:r.slot,entry:n,release:()=>{o||(o=!0,r.inFlight=Math.max(0,r.inFlight-1),r.inFlight===0&&(r.idleSince=this.now()),this.pump(),this.reap(),this.signalDrain())}}}pump(){for(;this.waiters.length>0;){let e=this.waiters.shift();if(!e.settled){e.settled=!0,e.resolve();return}}}evict(e){let n=this.slots.find(r=>r.entry===e);n&&(n.entry=null,n.state="paused",n.opening=null,this.opts.onDetach?.(n.slot,"runtime detached"),this.signalDrain())}restoreMin(){if(this.slots.filter(r=>r.state!=="paused").length>=this.opts.min)return;let n=this.slots.filter(r=>r.state==="paused"&&!r.opening);n.sort((r,o)=>r.slot.index-o.slot.index);for(let r of n){if(this.slots.filter(o=>o.state!=="paused").length>=this.opts.min)break;this.openRuntime(r)}}reap(){if(this.closed||this.draining)return;this.restoreMin();let e=this.now(),n=this.slots.filter(r=>r.state==="attached"&&r.inFlight===0&&r.idleSince!==null&&e-r.idleSince>=this.idleGraceMs).sort((r,o)=>(r.idleSince??0)-(o.idleSince??0)||o.slot.index-r.slot.index);for(let r of n){if(this.slots.filter(o=>o.state!=="paused").length<=this.opts.min)break;this.endSlot(r)}}endSlot(e){e.state="paused";let n=e.entry;e.entry=null,this.slots.splice(this.slots.indexOf(e),1),n&&this.opts.closeSlot(n).catch(()=>{})}stats(){let e=this.slots.filter(o=>o.state==="attached").length,n=this.slots.filter(o=>o.state==="queued").length,r=this.slots.filter(o=>o.state==="paused").length;return{requested:this.slots.length,attached:e,queued:n,paused:r,inFlight:this.slots.reduce((o,s)=>o+s.inFlight,0),draining:this.draining}}signalDrain(){if(!(!this.draining||this.drainComplete===null)&&this.slots.reduce((e,n)=>e+n.inFlight,0)===0){let e=this.drainComplete;this.drainComplete=null,e.resolve()}}async drain(e){if(!this.closed){this.draining=!0;for(let n of this.waiters.splice(0))n.settled||(n.settled=!0,n.resolve());if(this.slots.reduce((n,r)=>n+r.inFlight,0)>0){if(!this.drainComplete){let r,o=new Promise(s=>{r=s});this.drainComplete={promise:o,resolve:r}}let n=this.drainComplete;await Promise.race([n.promise,new Promise(r=>setTimeout(r,e?.timeoutMs??5e3))])}await this.close()}}async close(){this.closed=!0;for(let o of this.waiters.splice(0))o.settled||(o.settled=!0,o.resolve());let e=this.slots.map(o=>o.entry).filter(o=>o!==null);this.slots.length=0;let r=(await Promise.allSettled(e.map(o=>this.opts.closeSlot(o)))).filter(o=>o.status==="rejected");if(r.length>0)throw new Error(`failed to end ${r.length} decision pool session(s)`)}};var In=require("@urun-sh/core"),ye=class extends Error{kind;code;retryable;constructor(e,n,r,o){super(n),this.name="DecisionStreamError",this.kind=e,this.code=r,this.retryable=o,Object.setPrototypeOf(this,new.target.prototype)}},Nt=33e4;function ro(t,e=Nt){return(async function*(){let r=t[Symbol.asyncIterator]();for(;;){let o,s=new Promise((d,l)=>{o=l}),i=setTimeout(()=>o(new Ut),e),u;try{u=await Promise.race([r.next(),s])}catch(d){throw d instanceof J||d instanceof In.SessionFailedError&&d.kind==="transport-failed"?d:d instanceof Ut?(await r.return?.(void 0),new ye("upstream_idle_timeout",`decision lane produced no event for ${e}ms (upstream wedged) \u2014 the session is untouched; retry this decision`,"upstream_idle_timeout",!0)):new ye("upstream_threw",`decision stream failed: ${String(d instanceof Error?d.message:d)}`,"upstream_threw",!1)}finally{clearTimeout(i)}if(u.done)return;let a=u.value;if(typeof a=="object"&&a!==null&&"type"in a&&a.type==="error")throw new ye("upstream_error","decision stream ended with an upstream error event","upstream_error",!1);yield a}})()}var Ut=class extends Error{constructor(){super("stream idle timeout")}};function oo(t){return t instanceof ye?!1:t instanceof J?!0:t instanceof In.SessionFailedError?t.kind==="transport-failed":!1}var ya=3,_a="llm",wa=new Set(["paused","error"]);function so(t,e){let n=t.doc(_a),r=typeof n.onConnectionState=="function",o=typeof t.onPhase=="function";if(!r&&!o)throw new Error(`session for app "${e}" 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 s=null,i=!1,u=new Set,a=[],d=l=>{if(!s){s=l;for(let c of[...u])c(l);u.clear()}};return r&&a.push(n.onConnectionState(l=>{i&&(l.consecutiveFailures<ya||d(new J(`the pooled session for app "${e}" is gone: its doc backhaul failed ${l.consecutiveFailures} consecutive times (last close ${l.lastCloseCode===null?"by provider liveness reap":`code ${l.lastCloseCode}`}${l.lastCloseReason?`, reason ${JSON.stringify(l.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`)))})),o&&a.push(t.onPhase(l=>{if(l.name==="live"){i=!0;return}i&&wa.has(l.name)&&d(new J(`the pooled session for app "${e}" lost its compute binding (phase "${l.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:()=>s,onGone(l){return s?(l(s),()=>{}):(u.add(l),()=>u.delete(l))},dispose(){u.clear();for(let l of a.splice(0))l()}}}async function*On(t,e){let n=e.gone();if(n)throw n;let r=t[Symbol.asyncIterator](),o,s=new Promise((i,u)=>{o=e.onGone(a=>u(a))});s.catch(()=>{});try{for(;;){let i=await Promise.race([r.next(),s]);if(i.done)return;yield i.value}}finally{o?.(),r.return?.(void 0)}}var ba=1,Sa=4,ka=4;function lo(t,e){let n=(i,u)=>{let a=(t[i]??"").trim();if(!a)return u;let d=Number.parseInt(a,10);if(!Number.isInteger(d)||d<1)throw new Error(`${i} must be a positive integer (got ${JSON.stringify(t[i])})`);return d},r=(t.URUN_DECISION_POOL_MIN??"").trim();if(r==="0")return null;let o=r?Number.parseInt(r,10):ba;if(!Number.isInteger(o)||o<0)throw new Error(`URUN_DECISION_POOL_MIN must be a non-negative integer (got ${JSON.stringify(t.URUN_DECISION_POOL_MIN)})`);let s=n("URUN_DECISION_POOL_MAX",Math.max(o,Sa));return{min:o,max:s,idleGraceMs:3e5,inFlightCap:n("URUN_DECISION_POOL_INFLIGHT_CAP",ka),replicaId:(t.URUN_REPLICA_ID??"").trim()||e||"proxy"}}function co(t){let e=(t.URUN_DECISION_STREAM_IDLE_TIMEOUT_S??"").trim();if(!e)return Nt;let n=Number.parseInt(e,10);if(!Number.isInteger(n)||n<=0)throw new Error(`URUN_DECISION_STREAM_IDLE_TIMEOUT_S must be a positive integer (got ${JSON.stringify(t.URUN_DECISION_STREAM_IDLE_TIMEOUT_S)})`);return n*1e3}function io(t,e,n,r){let o=t[n];if(typeof o!="function")throw new Error(`app "${e}" has no function "${n}" (set URUN_FUNCTION)`);let s=r?o(void 0,r):o();if(typeof s.end!="function")throw new Error(`app "${e}" function "${n}" returned a session without end()`);return s}function po(t,e,n,r){let{baseUrl:o,orgId:s,auth:i}=t,u=e.fnName??t.fnName,a=t.sessionStartOptions?.(n),d=r||a||e.sharedApp?{...e.sharedApp?{sharedApp:!0}:{},...a,...r}:void 0,l=_=>({session:_,responses:new Rt(_),gone:so(_,n)});if(i.lane==="jwt"){let _=(0,ie.App)(e.appSlug,{baseUrl:o,orgId:s,jwt:i.jwt});return l(io(_,e.appSlug,u,d))}let{apiKey:c,gatewayUrl:h}=i,f=(typeof t.subject=="function"?t.subject():t.subject)??(0,uo.randomUUID)(),p=(0,ie.App)(e.appSlug,{baseUrl:o,orgId:s,getAccessToken:async()=>(await(0,ie.createClientToken)(c,{baseUrl:h,expiresIn:300,allowedFunctions:[`${e.appSlug}/${u}`],subject:f,...e.sharedApp?{sharedApp:!0}:{}})).token});return l(io(p,e.appSlug,u,d))}function xa(t){let e=t.session.id;if(typeof e!="string"||e.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 e}function Ra(t){if(t.kind==="never-live")return!0;if(t.kind!=="create-failed")return!1;let e=t.status.error?.httpStatus,n=t.status.error?.code;return e===410||e===409&&(n==="cancelled"||n==="expired")?!0:n==="client_ended"}var Ea=new Set(["error","paused"]);function fo(t){let{auth:e,catalog:n}=t,r=null,o=(i,u)=>{let a=po(t,i,u);return t.onSessionOpen&&typeof a.session.id=="string"&&a.session.id!==""&&t.onSessionOpen(u,a.session.id),a.gone.onGone(()=>{r?.evict(u,a)}),a},s=new Mt({sharedOnlyListing:t.sharedOnlyListing===!0,defaultApp:t.defaultApp,fnName:t.fnName,openSession:o,closeSession:async i=>{if(i.gone.dispose(),t.release==="detach"){if(typeof i.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');i.session.detach();return}await i.session.end()},listApps:e.lane==="api-key"?()=>to({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,listCatalog:n?()=>xr(n):null,listPrices:n?()=>Nr(n):null,shared:e.lane==="api-key"?()=>Fr({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,sessionKey:xa});return r=s,t.defaultApp!==null&&s.seed(t.defaultApp,o({appSlug:t.defaultApp},ke({appSlug:t.defaultApp}))),s}function mo(t,e){let n=t.decisionPool;if(!n)throw new Error("wireDecisionPool called without BackhaulConfig.decisionPool");let r=n.replicaId??"proxy",o=new Map,s=new Map,i=a=>{process.stderr.write(`[decision-pool] ${a}
6
- `)},u=(a,d)=>{let l=o.get(d);if(l)return l;let c=new $t({min:n.min,max:n.max,idleGraceMs:n.idleGraceMs,inFlightCap:n.inFlightCap,replicaId:r,openSlot:async h=>{let f=po(t,a,d,{concurrencyKey:h.concurrencyKey});return f.gone.onGone(()=>c.evict(f)),await Ft(f.session),i(`slot attached pool=${d} slot=${h.concurrencyKey} session=${f.session.id??"(no id)"}`),f},closeSlot:async h=>{h.gone.dispose(),await h.session.end()},onDetach:(h,f)=>i(`slot detached pool=${d} slot=${h.concurrencyKey} reason=${f}`)});return o.set(d,c),c};return{async boot(){if(t.defaultApp===null)return;let a={appSlug:t.defaultApp};await u(a,ke(a)).boot()},async sessionFor(a){let d=await e.resolveTargetFor(a),l=ke(d),c=u(d,l).tryAcquireReady();if(!c)throw new Oe;let h=s.get(c.entry)??[];return h.push(c),s.set(c.entry,h),{app:l,entry:c.entry,identity:Ct(d)}},async warmFor(a){let d=await e.resolveTargetFor(a);await u(d,ke(d)).boot()},end(a){let d=s.get(a)?.shift();d&&(s.get(a)?.length===0&&s.delete(a),d.release())},evict(a){for(let d of o.values())d.evict(a)},reap(){for(let a of o.values())a.reap()},async pingReady(a){for(let d of o.values())if(await d.pingReady(a))return!0;return!1},stats(){return[...o.entries()].map(([a,d])=>({poolKey:a,...d.stats()}))},async drain(a){await Promise.all([...o.values()].map(d=>d.drain({timeoutMs:a})))},async close(){await Promise.all([...o.values()].map(a=>a.close()))}}}function Aa(t){return(0,ie.isWakingPhase)(t?.name)?t?.runtime?.wake?.expectedWakePath==="portable-golden-restore"?ie.DEFAULT_WHEN_LIVE_TIMEOUT_MS:ie.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS:ie.DEFAULT_WHEN_LIVE_TIMEOUT_MS}async function Ft(t,e){let n=t.whenLive;if(typeof n!="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 n.call(t,{timeout:Aa(t.phase),signal:e})}async function ao(t){if(typeof t.cancel=="function")try{await t.cancel();return}catch{}if(typeof t.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");t.detach()}function Dt(t){return t.reason instanceof Error?t.reason:new Error("the client socket closed")}function go(t,e=Et,n,r,o=Nt){let s=new WeakMap,i=new WeakMap,u=new WeakMap,a=new WeakMap,d=new WeakMap,l=p=>{let _=p.session.id;return typeof _=="string"&&_.length>0?_:null},c=p=>{let _=s.get(p);if(!_){let m={waiting:0,gone:new AbortController};d.set(p,m),_=(async()=>{if(await Ft(p.session,m.gone.signal),m.gone.signal.aborted)throw Dt(m.gone.signal);d.get(p)===m&&d.delete(p);let y=await Sr(p.session,await e());return Object.assign(y,{sessionId:p.session.id})})(),s.set(p,_),_.catch(()=>{s.get(p)===_&&s.delete(p)}),_.then(()=>{d.get(p)===m&&d.delete(p)},()=>{d.get(p)===m&&d.delete(p)})}return _},h=p=>{let _=a.get(p);return _||(_=(async()=>{let m=await c(p);return{appendInputAudio:y=>m.appendInputAudio(y),onOutputAudio:y=>m.onOutputAudio(y),sessionId:l(p)}})(),a.set(p,_),_.catch(()=>{a.get(p)===_&&a.delete(p)})),_},f=async(p,_,m)=>{if(m?.aborted){let v=d.get(p);throw(s.get(p)===void 0||v!==void 0&&v.waiting===0)&&await ao(p.session),Dt(m)}let y=_(p),g=d.get(p);return g!==void 0&&g.waiting++,m===void 0?y:new Promise((v,E)=>{let b=()=>{g!==void 0&&--g.waiting===0&&d.get(p)===g&&(g.gone.abort(),d.delete(p),s.delete(p),a.delete(p),ao(p.session).catch(()=>{})),E(Dt(m))};m.addEventListener("abort",b,{once:!0});let w=()=>m.removeEventListener("abort",b);y.then(k=>{w(),v(k)},k=>{w(),E(k)})})};return{createResponse:Cr({sessionFor:r?p=>r.sessionFor(p):p=>t.sessionFor(p),sessionIdOf:p=>l(p),evict:(p,_)=>{r?r.evict(_):t.evict(p,_)},onAdmissionVerdict:(p,_,m)=>{Ra(m)&&(r?r.evict(_):t.evict(p,_),n?.(p,m.kind==="never-live"?"never-live":"create-failed"))},createStream:async(p,_)=>{try{await Ft(p.session);let m=ro(On(await p.responses.responses.create(_),p.gone),o);if(!r)return m;let y=p;return(async function*(){try{yield*m}catch(v){throw oo(v)&&r.evict(y),v}finally{r.end(y)}})()}catch(m){throw r&&r.end(p),m}}}),listModels:()=>t.modelList(),openRouterModels:()=>t.openRouterModels(),supportsImages:(p,_)=>t.supportsImages(p,_),listImageModels:()=>t.imageModelList(),catalogIdentity:p=>t.catalogIdentityFor(p),servingAppSlug:async p=>await t.servingAppSlugFor(p),supportsAudio:p=>t.supportsAudio(p),supportsSpeech:p=>t.supportsSpeech(p),speechVoices:p=>t.speechVoices(p),supportsTranscription:p=>t.supportsTranscription(p),transcriptStreamFor:p=>t.transcriptStreamFor(p),listSpeechToTextModels:()=>t.speechToTextModelList(),openAudio:async(p,_)=>{let{entry:m}=await t.sessionFor(p);return await f(m,c,_)},openAudioOn:async(p,_)=>{let{entry:m}=await t.sessionForHandle(p);return await f(m,h,_)},openTurnsOn:async function*(p,_){if(_?.aborted)throw Dt(_);let{entry:m}=await t.sessionForHandle(p);yield*Tr(m.session,_)},openVideo:async p=>{let{entry:_}=await t.sessionFor(p),m=i.get(_);return m||(m=kr(_.session),i.set(_,m)),m},openVideoOut:async p=>{let{entry:_}=await t.sessionFor(p),m=u.get(_);return m||(m=Er(_.session),u.set(_,m)),m},sessionHandle:async p=>(await t.handleFor(p)).handle,speechSessionHandle:async p=>(await t.speechHandleFor(p)).handle,evictSpeechSession:async p=>{let{app:_,entry:m}=await t.sessionForHandle(p);await t.evict(_,m)},createResponseOn:async(p,_)=>{let{entry:m}=await t.sessionForHandle(p);return await Ft(m.session),On(await m.responses.responses.create(_),m.gone)},onSessionEnd:async(p,_)=>{let{entry:m}=await t.sessionForHandle(p),y=m.session;if(typeof y.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let g=!1;return y.onPhase(v=>{if(g||!Ea.has(v.name))return;g=!0;let E=y.endsAt??null;_({timeLeftMs:E?Math.max(0,E.getTime()-Date.now()):null,reason:v.reason??`session ${v.name}`})})}}}var _i=require("http");var Lo=require("crypto"),$o=require("ws");var yo="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",Ia=1007,_o=1008,_e=1011,Gt=1008,S=class extends Error{constructor(n,r=Ia){super(n);this.closeCode=r}closeCode},j=t=>typeof t=="object"&&t!==null&&!Array.isArray(t)?t:null,Bt=["setup","clientContent","realtimeInput","toolResponse"];function wo(t){let e;try{e=JSON.parse(t)}catch{throw new S("client message is not valid JSON")}let n=j(e);if(!n)throw new S("client message must be a JSON object");let r=Bt.filter(u=>n[u]!==void 0),o=Object.keys(n).filter(u=>!Bt.includes(u));if(o.length>0)throw new S(`unknown client message field(s) ${JSON.stringify(o)} \u2014 expected exactly one of ${Bt.join(", ")}`);if(r.length!==1)throw new S(`client message must contain exactly one of ${Bt.join(", ")} (got ${r.length})`);let s=r[0],i=j(n[s]);if(!i)throw new S(`"${s}" must be a JSON object`);return{kind:s,[s]:i}}function vo(t,e){if(typeof t=="string")return t;let n=j(t),r=n?n.parts:void 0;if(!Array.isArray(r))throw new S(`${e}: expected a Content with a parts array`);let o=[];for(let s of r){let i=j(s);if(i&&typeof i.text=="string"){o.push(i.text);continue}throw i&&(i.inlineData!==void 0||i.fileData!==void 0)?new S(`${e}: media parts (inlineData/fileData) are a declared follow-up slice (audio/video lanes over the proxy transport AudioBridge) \u2014 only text parts are supported in this slice`):i&&(i.functionCall!==void 0||i.functionResponse!==void 0)?new S(`${e}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new S(`${e}: unsupported part ${JSON.stringify(s)}`)}return o.join(`
5
+ `);let a=(n?je(n):[]).map(d=>{let l=s.filter(m=>m.model_id===d.model_id&&m.variant===d.variant),c=Ot(l),h=kn(l),f=nt(d.model_id,d.variant,i),p=xn(l);return Ie({id:`${d.model_id}:${d.variant}`,object:"model",created:0,owned_by:"urun",...c!==null?{context_length:c}:{},...f!==null?{pricing:f}:{},...p.published.length>0?{voices:[...p.published]}:{},...h!==null?{capabilities:{reasoning:h}}:{}})});return{object:"list",data:this.opts.sharedOnlyListing?a:[...e.map(d=>Tt(d,s,i)),...a]}}async openRouterModels(){let e=await this.sharedBlock(),[n,r]=await Promise.all([this.catalogRows(),this.pricesForListing(this.outputFreeKeys(e))]);return Kr(e,n,r,o=>this.warn(o))}async supportsImages(e,n){let r=await this.resolveTarget(e),o=await this.catalogRows();if(r.sharedApp){let s=Rn(String(e),r.modelId,r.variant,o,this.opts.listCatalog!==null);return Xr(`${r.modelId}:${r.variant}`,s).includes(n)}return Yr(be(r.appSlug,o)).includes(n)}async supportsAudio(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Se(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))!==null:Se(be(n.appSlug,r))!==null}async supportsSpeech(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Se(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))==="tts":Se(be(n.appSlug,r))==="tts"}async supportsTranscription(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Se(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))==="stt":Se(be(n.appSlug,r))==="stt"}async transcriptStreamFor(e){let n=await this.resolveTarget(e),r=await this.catalogRows(),o=n.sharedApp?r.filter(s=>s.model_id===n.modelId&&s.variant===n.variant):be(n.appSlug,r);return ha(o)}async speechVoices(e){let n=await this.resolveTarget(e),r=await this.catalogRows(),o=n.sharedApp?r.filter(s=>s.model_id===n.modelId&&s.variant===n.variant):be(n.appSlug,r);return xn(o)}async imageModelList(){let e=await this.catalogRows(),n=rt,r=[];for(let s of await this.availableIds()){let i=Yr(be(s,e));i.length!==0&&r.push(Ie({...Tt(s,e,n),image_modes:i}))}let o=await this.sharedBlock();if(o)for(let s of je(o)){let i=Rn(`${s.model_id}:${s.variant}`,s.model_id,s.variant,e,this.opts.listCatalog!==null),u=Xr(`${s.model_id}:${s.variant}`,i);if(u.length===0)continue;let a=Ie({id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun",image_modes:u});r.push(a)}return{object:"list",data:r}}async speechToTextModelList(){let e=await this.catalogRows(),n=rt,r=[];for(let s of await this.availableIds())Se(be(s,e))==="stt"&&r.push(Tt(s,e,n));let o=await this.sharedBlock();if(o)for(let s of je(o)){let i=Rn(`${s.model_id}:${s.variant}`,s.model_id,s.variant,e,this.opts.listCatalog!==null);if(Se(i)!=="stt")continue;let u=Ie({id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun"});r.push(u)}return{object:"list",data:r}}async closeAll(){let e=[...this.pool.values()],n=[];if(await Promise.all(e.map(async r=>{try{await this.opts.closeSession(await r)}catch(o){n.push(String(o instanceof Error?o.message:o))}})),n.length>0)throw new Error(`failed to close ${n.length} pooled session(s): ${n.join("; ")}`)}};var Ge=class extends Error{code;status;retryAfterSeconds;constructor(e,n,r,o){super(o),this.name="DecisionStreamFailure",this.code=e,this.status=n,this.retryAfterSeconds=r,Object.setPrototypeOf(this,new.target.prototype)}},st=class extends Ge{constructor(){super("draining",503,5,"decision pool is draining (GOAWAY): no new decisions may start; retry on the next replica")}},An=class extends Ge{constructor(e){super("acquire_timeout",429,30,`no decision slot capacity within ${e}ms (all slots at their in-flight cap)`)}},Oe=class extends Error{lastDemandAgeMs;constructor(e){super("no ready decision slot on this proxy \u2014 the request was refused fast, not fenced; retry shortly (an out-of-band slot add was submitted)"),this.name="DecisionCapacityError",this.lastDemandAgeMs=e}},$t=class{constructor(e){this.opts=e;if(!Number.isInteger(e.min)||e.min<0)throw new Error("decision pool min must be a non-negative integer");if(!Number.isInteger(e.max)||e.max<e.min||e.max<1)throw new Error("decision pool max must be an integer >= min and >= 1");if(e.idleGraceMs!==void 0&&e.idleGraceMs<0)throw new Error("decision pool idleGraceMs must be >= 0");if(!Number.isInteger(e.inFlightCap)||e.inFlightCap<1)throw new Error("decision pool inFlightCap must be an integer >= 1");if(!e.replicaId)throw new Error("decision pool replicaId must be non-empty");this.now=e.now??Date.now,this.idleGraceMs=e.idleGraceMs??3e4}opts;slots=[];nextIndex=0;lastDemandAt=null;missCount=0;missCountPastWindow=0;lastMissAgeMs=null;idleGraceMs;waiters=[];now;closed=!1;draining=!1;drainComplete=null;slotKey(e){return`decision:${this.opts.replicaId}:${e}`}async boot(){this.closed||this.draining||await Promise.all(this.slots.map(e=>e.opening??Promise.resolve()))}openForDemand(){let e=this.slots.filter(n=>n.state==="paused"&&!n.opening).sort((n,r)=>n.slot.index-r.slot.index);return e.length>0?(this.openRuntime(e[0]),e[0]):this.ensureSlot()}ensureSlot(){let e=this.nextIndex++,r={slot:{index:e,concurrencyKey:this.slotKey(e)},state:"queued",entry:null,inFlight:0,idleSince:null,opening:null};return this.slots.push(r),this.openRuntime(r),r}openRuntime(e){e.opening||(e.state="queued",e.opening=(async()=>{try{let n=await this.opts.openSlot(e.slot);if(this.closed||this.draining){await this.opts.closeSlot(n).catch(()=>{}),this.signalDrain();return}e.entry=n,e.state="attached",e.idleSince=this.now(),this.pump()}catch(n){e.entry=null,e.state="paused",e.opening=null,this.opts.onDetach?.(e.slot,n instanceof Error?n.message:String(n)),this.pump();return}e.opening=null})())}async acquire(){if(this.closed)throw new Error("decision pool is closed");if(this.draining)throw new st;this.lastDemandAt=this.now();let e=this.opts.acquireTimeoutMs??6e5,n=Date.now();for(;;){let r=this.tryLease();if(r)return r;if(this.draining)throw new st;if(!this.slots.some(a=>a.state==="queued")&&this.slots.length<this.opts.max){this.openForDemand();continue}let s=n+e-Date.now();if(s<=0)throw new An(e);let i,u={resolve:()=>{},settled:!1};try{await Promise.race([new Promise(a=>{u.resolve=a,this.waiters.push(u)}),new Promise(a=>{i=setTimeout(a,s)})])}finally{clearTimeout(i);let a=this.waiters.indexOf(u);a!==-1&&this.waiters.splice(a,1),u.settled=!0}if(this.closed)throw new Error("decision pool is closed");if(this.draining)throw new st}}tryAcquireReady(){if(this.closed)return null;let e=this.lastDemandAt===null?null:this.now()-this.lastDemandAt;this.lastDemandAt=this.now();let n=this.tryLease();return n||(!this.slots.some(r=>r.state==="queued")&&this.slots.length<this.opts.max&&this.openForDemand(),this.missCount+=1,e!==null&&e>this.idleGraceMs&&(this.missCountPastWindow+=1),this.lastMissAgeMs=e,null)}async pingReady(e){if(this.closed)return!1;for(let n=0;n<this.slots.length;n++){let r=this.slots[n];if(r.state!=="attached"||r.entry===null)continue;let o=r.entry;if(o===null||typeof o!="object"||!("session"in o))return!1;let s=o.session;if(typeof s!="object"||s===null||!("whenLive"in s))return!1;let i=s.whenLive;if(typeof i!="function")return!1;let u=r.entry;try{return await i.call(s,{timeout:e}),!0}catch{this.evict(u),r.entry!==null&&r.entry!==u&&n--;continue}}return!1}tryLease(){let e=null;for(let s of this.slots)s.state!=="attached"||s.inFlight>=this.opts.inFlightCap||(!e||s.inFlight<e.inFlight)&&(e=s);if(!e)return null;let n=e.entry;if(n===null)return null;let r=e;r.inFlight+=1;let o=!1;return{slot:r.slot,entry:n,release:()=>{o||(o=!0,r.inFlight=Math.max(0,r.inFlight-1),r.inFlight===0&&(r.idleSince=this.now()),this.pump(),this.reap(),this.signalDrain())}}}pump(){for(;this.waiters.length>0;){let e=this.waiters.shift();if(!e.settled){e.settled=!0,e.resolve();return}}}evict(e){let n=this.slots.find(r=>r.entry===e);n&&(n.entry=null,n.state="paused",n.opening=null,this.opts.onDetach?.(n.slot,"runtime detached"),this.signalDrain())}reap(){if(this.closed||this.draining)return;let e=this.now(),n=this.slots.filter(r=>r.state==="attached"&&r.inFlight===0&&r.idleSince!==null&&e-r.idleSince>=this.idleGraceMs).sort((r,o)=>(r.idleSince??0)-(o.idleSince??0)||o.slot.index-r.slot.index);for(let r of n)this.endSlot(r)}endSlot(e){e.state="paused";let n=e.entry;e.entry=null,this.slots.splice(this.slots.indexOf(e),1),n&&this.opts.closeSlot(n).catch(()=>{})}stats(){let e=this.slots.filter(o=>o.state==="attached").length,n=this.slots.filter(o=>o.state==="queued").length,r=this.slots.filter(o=>o.state==="paused").length;return{requested:this.slots.length,attached:e,queued:n,paused:r,inFlight:this.slots.reduce((o,s)=>o+s.inFlight,0),draining:this.draining,misses:this.missCount,missesPastWindow:this.missCountPastWindow,lastMissAgeMs:this.lastMissAgeMs}}signalDrain(){if(!(!this.draining||this.drainComplete===null)&&this.slots.reduce((e,n)=>e+n.inFlight,0)===0){let e=this.drainComplete;this.drainComplete=null,e.resolve()}}async drain(e){if(!this.closed){this.draining=!0;for(let n of this.waiters.splice(0))n.settled||(n.settled=!0,n.resolve());if(this.slots.reduce((n,r)=>n+r.inFlight,0)>0){if(!this.drainComplete){let r,o=new Promise(s=>{r=s});this.drainComplete={promise:o,resolve:r}}let n=this.drainComplete;await Promise.race([n.promise,new Promise(r=>setTimeout(r,e?.timeoutMs??5e3))])}await this.close()}}async close(){this.closed=!0;for(let o of this.waiters.splice(0))o.settled||(o.settled=!0,o.resolve());let e=this.slots.map(o=>o.entry).filter(o=>o!==null);this.slots.length=0;let r=(await Promise.allSettled(e.map(o=>this.opts.closeSlot(o)))).filter(o=>o.status==="rejected");if(r.length>0)throw new Error(`failed to end ${r.length} decision pool session(s)`)}};var In=require("@urun-sh/core"),ye=class extends Error{kind;code;retryable;constructor(e,n,r,o){super(n),this.name="DecisionStreamError",this.kind=e,this.code=r,this.retryable=o,Object.setPrototypeOf(this,new.target.prototype)}},Nt=33e4;function ro(t,e=Nt){return(async function*(){let r=t[Symbol.asyncIterator]();for(;;){let o,s=new Promise((d,l)=>{o=l}),i=setTimeout(()=>o(new Ut),e),u;try{u=await Promise.race([r.next(),s])}catch(d){throw d instanceof J||d instanceof In.SessionFailedError&&d.kind==="transport-failed"?d:d instanceof Ut?(await r.return?.(void 0),new ye("upstream_idle_timeout",`decision lane produced no event for ${e}ms (upstream wedged) \u2014 the session is untouched; retry this decision`,"upstream_idle_timeout",!0)):new ye("upstream_threw",`decision stream failed: ${String(d instanceof Error?d.message:d)}`,"upstream_threw",!1)}finally{clearTimeout(i)}if(u.done)return;let a=u.value;if(typeof a=="object"&&a!==null&&"type"in a&&a.type==="error")throw new ye("upstream_error","decision stream ended with an upstream error event","upstream_error",!1);yield a}})()}var Ut=class extends Error{constructor(){super("stream idle timeout")}};function oo(t){return t instanceof ye?!1:t instanceof J?!0:t instanceof In.SessionFailedError?t.kind==="transport-failed":!1}var ya=3,_a="llm",wa=new Set(["paused","error"]);function so(t,e){let n=t.doc(_a),r=typeof n.onConnectionState=="function",o=typeof t.onPhase=="function";if(!r&&!o)throw new Error(`session for app "${e}" 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 s=null,i=!1,u=new Set,a=[],d=l=>{if(!s){s=l;for(let c of[...u])c(l);u.clear()}};return r&&a.push(n.onConnectionState(l=>{i&&(l.consecutiveFailures<ya||d(new J(`the pooled session for app "${e}" is gone: its doc backhaul failed ${l.consecutiveFailures} consecutive times (last close ${l.lastCloseCode===null?"by provider liveness reap":`code ${l.lastCloseCode}`}${l.lastCloseReason?`, reason ${JSON.stringify(l.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`)))})),o&&a.push(t.onPhase(l=>{if(l.name==="live"){i=!0;return}i&&wa.has(l.name)&&d(new J(`the pooled session for app "${e}" lost its compute binding (phase "${l.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:()=>s,onGone(l){return s?(l(s),()=>{}):(u.add(l),()=>u.delete(l))},dispose(){u.clear();for(let l of a.splice(0))l()}}}async function*On(t,e){let n=e.gone();if(n)throw n;let r=t[Symbol.asyncIterator](),o,s=new Promise((i,u)=>{o=e.onGone(a=>u(a))});s.catch(()=>{});try{for(;;){let i=await Promise.race([r.next(),s]);if(i.done)return;yield i.value}}finally{o?.(),r.return?.(void 0)}}var ba=1,Sa=4,ka=4;function lo(t,e){let n=(i,u)=>{let a=(t[i]??"").trim();if(!a)return u;let d=Number.parseInt(a,10);if(!Number.isInteger(d)||d<1)throw new Error(`${i} must be a positive integer (got ${JSON.stringify(t[i])})`);return d},r=(t.URUN_DECISION_POOL_MIN??"").trim();if(r==="0")return null;let o=r?Number.parseInt(r,10):ba;if(!Number.isInteger(o)||o<0)throw new Error(`URUN_DECISION_POOL_MIN must be a non-negative integer (got ${JSON.stringify(t.URUN_DECISION_POOL_MIN)})`);let s=n("URUN_DECISION_POOL_MAX",Math.max(o,Sa));return{min:o,max:s,idleGraceMs:3e4,inFlightCap:n("URUN_DECISION_POOL_INFLIGHT_CAP",ka),replicaId:(t.URUN_REPLICA_ID??"").trim()||e||"proxy"}}function co(t){let e=(t.URUN_DECISION_STREAM_IDLE_TIMEOUT_S??"").trim();if(!e)return Nt;let n=Number.parseInt(e,10);if(!Number.isInteger(n)||n<=0)throw new Error(`URUN_DECISION_STREAM_IDLE_TIMEOUT_S must be a positive integer (got ${JSON.stringify(t.URUN_DECISION_STREAM_IDLE_TIMEOUT_S)})`);return n*1e3}function io(t,e,n,r){let o=t[n];if(typeof o!="function")throw new Error(`app "${e}" has no function "${n}" (set URUN_FUNCTION)`);let s=r?o(void 0,r):o();if(typeof s.end!="function")throw new Error(`app "${e}" function "${n}" returned a session without end()`);return s}function po(t,e,n,r){let{baseUrl:o,orgId:s,auth:i}=t,u=e.fnName??t.fnName,a=t.sessionStartOptions?.(n),d=r||a||e.sharedApp?{...e.sharedApp?{sharedApp:!0}:{},...a,...r}:void 0,l=_=>({session:_,responses:new Rt(_),gone:so(_,n)});if(i.lane==="jwt"){let _=(0,ie.App)(e.appSlug,{baseUrl:o,orgId:s,jwt:i.jwt});return l(io(_,e.appSlug,u,d))}let{apiKey:c,gatewayUrl:h}=i,f=(typeof t.subject=="function"?t.subject():t.subject)??(0,uo.randomUUID)(),p=(0,ie.App)(e.appSlug,{baseUrl:o,orgId:s,getAccessToken:async()=>(await(0,ie.createClientToken)(c,{baseUrl:h,expiresIn:300,allowedFunctions:[`${e.appSlug}/${u}`],subject:f,...e.sharedApp?{sharedApp:!0}:{}})).token});return l(io(p,e.appSlug,u,d))}function xa(t){let e=t.session.id;if(typeof e!="string"||e.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 e}function Ra(t){if(t.kind==="never-live")return!0;if(t.kind!=="create-failed")return!1;let e=t.status.error?.httpStatus,n=t.status.error?.code;return e===410||e===409&&(n==="cancelled"||n==="expired")?!0:n==="client_ended"}var Ea=new Set(["error","paused"]);function fo(t){let{auth:e,catalog:n}=t,r=null,o=(i,u)=>{let a=po(t,i,u);return t.onSessionOpen&&typeof a.session.id=="string"&&a.session.id!==""&&t.onSessionOpen(u,a.session.id),a.gone.onGone(()=>{r?.evict(u,a)}),a},s=new Mt({sharedOnlyListing:t.sharedOnlyListing===!0,defaultApp:t.defaultApp,fnName:t.fnName,openSession:o,closeSession:async i=>{if(i.gone.dispose(),t.release==="detach"){if(typeof i.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');i.session.detach();return}await i.session.end()},listApps:e.lane==="api-key"?()=>to({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,listCatalog:n?()=>xr(n):null,listPrices:n?()=>Nr(n):null,shared:e.lane==="api-key"?()=>Fr({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,sessionKey:xa});return r=s,t.defaultApp!==null&&s.seed(t.defaultApp,o({appSlug:t.defaultApp},ke({appSlug:t.defaultApp}))),s}function mo(t,e){let n=t.decisionPool;if(!n)throw new Error("wireDecisionPool called without BackhaulConfig.decisionPool");let r=n.replicaId??"proxy",o=new Map,s=new Map,i=a=>{process.stderr.write(`[decision-pool] ${a}
6
+ `)},u=(a,d)=>{let l=o.get(d);if(l)return l;let c=new $t({min:n.min,max:n.max,idleGraceMs:n.idleGraceMs,inFlightCap:n.inFlightCap,replicaId:r,openSlot:async h=>{let f=po(t,a,d,{concurrencyKey:h.concurrencyKey});return f.gone.onGone(()=>c.evict(f)),await Ft(f.session),i(`slot attached pool=${d} slot=${h.concurrencyKey} session=${f.session.id??"(no id)"}`),f},closeSlot:async h=>{h.gone.dispose(),await h.session.end()},onDetach:(h,f)=>i(`slot detached pool=${d} slot=${h.concurrencyKey} reason=${f}`)});return o.set(d,c),c};return{async boot(){if(t.defaultApp===null)return;let a={appSlug:t.defaultApp};await u(a,ke(a)).boot()},async sessionFor(a){let d=await e.resolveTargetFor(a),l=ke(d),c=u(d,l).tryAcquireReady();if(!c)throw new Oe(u(d,l).stats().lastMissAgeMs);let h=s.get(c.entry)??[];return h.push(c),s.set(c.entry,h),{app:l,entry:c.entry,identity:Ct(d)}},async warmFor(a){let d=await e.resolveTargetFor(a);await u(d,ke(d)).boot()},end(a){let d=s.get(a)?.shift();d&&(s.get(a)?.length===0&&s.delete(a),d.release())},evict(a){for(let d of o.values())d.evict(a)},reap(){for(let a of o.values())a.reap()},async pingReady(a){for(let d of o.values())if(await d.pingReady(a))return!0;return!1},stats(){return[...o.entries()].map(([a,d])=>({poolKey:a,...d.stats()}))},async drain(a){await Promise.all([...o.values()].map(d=>d.drain({timeoutMs:a})))},async close(){await Promise.all([...o.values()].map(a=>a.close()))}}}function Aa(t){return(0,ie.isWakingPhase)(t?.name)?t?.runtime?.wake?.expectedWakePath==="portable-golden-restore"?ie.DEFAULT_WHEN_LIVE_TIMEOUT_MS:ie.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS:ie.DEFAULT_WHEN_LIVE_TIMEOUT_MS}async function Ft(t,e){let n=t.whenLive;if(typeof n!="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 n.call(t,{timeout:Aa(t.phase),signal:e})}async function ao(t){if(typeof t.cancel=="function")try{await t.cancel();return}catch{}if(typeof t.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");t.detach()}function Dt(t){return t.reason instanceof Error?t.reason:new Error("the client socket closed")}function go(t,e=Et,n,r,o=Nt){let s=new WeakMap,i=new WeakMap,u=new WeakMap,a=new WeakMap,d=new WeakMap,l=p=>{let _=p.session.id;return typeof _=="string"&&_.length>0?_:null},c=p=>{let _=s.get(p);if(!_){let m={waiting:0,gone:new AbortController};d.set(p,m),_=(async()=>{if(await Ft(p.session,m.gone.signal),m.gone.signal.aborted)throw Dt(m.gone.signal);d.get(p)===m&&d.delete(p);let y=await Sr(p.session,await e());return Object.assign(y,{sessionId:p.session.id})})(),s.set(p,_),_.catch(()=>{s.get(p)===_&&s.delete(p)}),_.then(()=>{d.get(p)===m&&d.delete(p)},()=>{d.get(p)===m&&d.delete(p)})}return _},h=p=>{let _=a.get(p);return _||(_=(async()=>{let m=await c(p);return{appendInputAudio:y=>m.appendInputAudio(y),onOutputAudio:y=>m.onOutputAudio(y),sessionId:l(p)}})(),a.set(p,_),_.catch(()=>{a.get(p)===_&&a.delete(p)})),_},f=async(p,_,m)=>{if(m?.aborted){let v=d.get(p);throw(s.get(p)===void 0||v!==void 0&&v.waiting===0)&&await ao(p.session),Dt(m)}let y=_(p),g=d.get(p);return g!==void 0&&g.waiting++,m===void 0?y:new Promise((v,E)=>{let b=()=>{g!==void 0&&--g.waiting===0&&d.get(p)===g&&(g.gone.abort(),d.delete(p),s.delete(p),a.delete(p),ao(p.session).catch(()=>{})),E(Dt(m))};m.addEventListener("abort",b,{once:!0});let w=()=>m.removeEventListener("abort",b);y.then(k=>{w(),v(k)},k=>{w(),E(k)})})};return{createResponse:Cr({sessionFor:r?p=>r.sessionFor(p):p=>t.sessionFor(p),sessionIdOf:p=>l(p),evict:(p,_)=>{r?r.evict(_):t.evict(p,_)},onAdmissionVerdict:(p,_,m)=>{Ra(m)&&(r?r.evict(_):t.evict(p,_),n?.(p,m.kind==="never-live"?"never-live":"create-failed"))},createStream:async(p,_)=>{try{await Ft(p.session);let m=ro(On(await p.responses.responses.create(_),p.gone),o);if(!r)return m;let y=p;return(async function*(){try{yield*m}catch(v){throw oo(v)&&r.evict(y),v}finally{r.end(y)}})()}catch(m){throw r&&r.end(p),m}}}),listModels:()=>t.modelList(),openRouterModels:()=>t.openRouterModels(),supportsImages:(p,_)=>t.supportsImages(p,_),listImageModels:()=>t.imageModelList(),catalogIdentity:p=>t.catalogIdentityFor(p),servingAppSlug:async p=>await t.servingAppSlugFor(p),supportsAudio:p=>t.supportsAudio(p),supportsSpeech:p=>t.supportsSpeech(p),speechVoices:p=>t.speechVoices(p),supportsTranscription:p=>t.supportsTranscription(p),transcriptStreamFor:p=>t.transcriptStreamFor(p),listSpeechToTextModels:()=>t.speechToTextModelList(),openAudio:async(p,_)=>{let{entry:m}=await t.sessionFor(p);return await f(m,c,_)},openAudioOn:async(p,_)=>{let{entry:m}=await t.sessionForHandle(p);return await f(m,h,_)},openTurnsOn:async function*(p,_){if(_?.aborted)throw Dt(_);let{entry:m}=await t.sessionForHandle(p);yield*Tr(m.session,_)},openVideo:async p=>{let{entry:_}=await t.sessionFor(p),m=i.get(_);return m||(m=kr(_.session),i.set(_,m)),m},openVideoOut:async p=>{let{entry:_}=await t.sessionFor(p),m=u.get(_);return m||(m=Er(_.session),u.set(_,m)),m},sessionHandle:async p=>(await t.handleFor(p)).handle,speechSessionHandle:async p=>(await t.speechHandleFor(p)).handle,evictSpeechSession:async p=>{let{app:_,entry:m}=await t.sessionForHandle(p);await t.evict(_,m)},createResponseOn:async(p,_)=>{let{entry:m}=await t.sessionForHandle(p);return await Ft(m.session),On(await m.responses.responses.create(_),m.gone)},onSessionEnd:async(p,_)=>{let{entry:m}=await t.sessionForHandle(p),y=m.session;if(typeof y.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let g=!1;return y.onPhase(v=>{if(g||!Ea.has(v.name))return;g=!0;let E=y.endsAt??null;_({timeLeftMs:E?Math.max(0,E.getTime()-Date.now()):null,reason:v.reason??`session ${v.name}`})})}}}var _i=require("http");var Lo=require("crypto"),$o=require("ws");var yo="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",Ia=1007,_o=1008,_e=1011,Gt=1008,S=class extends Error{constructor(n,r=Ia){super(n);this.closeCode=r}closeCode},j=t=>typeof t=="object"&&t!==null&&!Array.isArray(t)?t:null,Bt=["setup","clientContent","realtimeInput","toolResponse"];function wo(t){let e;try{e=JSON.parse(t)}catch{throw new S("client message is not valid JSON")}let n=j(e);if(!n)throw new S("client message must be a JSON object");let r=Bt.filter(u=>n[u]!==void 0),o=Object.keys(n).filter(u=>!Bt.includes(u));if(o.length>0)throw new S(`unknown client message field(s) ${JSON.stringify(o)} \u2014 expected exactly one of ${Bt.join(", ")}`);if(r.length!==1)throw new S(`client message must contain exactly one of ${Bt.join(", ")} (got ${r.length})`);let s=r[0],i=j(n[s]);if(!i)throw new S(`"${s}" must be a JSON object`);return{kind:s,[s]:i}}function vo(t,e){if(typeof t=="string")return t;let n=j(t),r=n?n.parts:void 0;if(!Array.isArray(r))throw new S(`${e}: expected a Content with a parts array`);let o=[];for(let s of r){let i=j(s);if(i&&typeof i.text=="string"){o.push(i.text);continue}throw i&&(i.inlineData!==void 0||i.fileData!==void 0)?new S(`${e}: media parts (inlineData/fileData) are a declared follow-up slice (audio/video lanes over the proxy transport AudioBridge) \u2014 only text parts are supported in this slice`):i&&(i.functionCall!==void 0||i.functionResponse!==void 0)?new S(`${e}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new S(`${e}: unsupported part ${JSON.stringify(s)}`)}return o.join(`
7
7
  `)}var Oa={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},Te="urun.videoOut";function bo(t){return{framing:Rr,codec:t}}function So(t){let{seq:e,ptsUs:n,codec:r,key:o}=t.header;return{[Te]:{v:1,seq:e,ptsUs:n,codec:r,key:o,data:Buffer.from(t.au).toString("base64")}}}var Ta={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},Pa=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",Te],ho=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function Ca(t){let e=j(t);if(!e)throw new S("setup.realtimeInputConfig must be an object");for(let o of Object.keys(e))if(!(o==="automaticActivityDetection"||o==="activityHandling"))throw new S(o==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${o}"`);let n=j(e.automaticActivityDetection);if(e.automaticActivityDetection!==void 0&&!n)throw new S("realtimeInputConfig.automaticActivityDetection must be an object");for(let o of Object.keys(n??{}))if(o!=="disabled")throw new S(`automaticActivityDetection.${o} tunes AUTOMATIC (server-side) activity detection \u2014 the declared platform follow-up (serve turn_detection events over the session stream lane); only "disabled: true" (manual activity mode) is supported in this slice`);if(n?.disabled!==!0)throw new S("automatic (server-side) activity detection is the declared platform follow-up (serve turn_detection turn_start/turn_end events over the session stream lane, not proxy DSP) \u2014 set realtimeInputConfig.automaticActivityDetection.disabled: true for manual activity mode");let r="START_OF_ACTIVITY_INTERRUPTS";if(e.activityHandling!==void 0){if(!ho.includes(e.activityHandling))throw new S(`unsupported activityHandling ${JSON.stringify(e.activityHandling)} \u2014 expected one of ${ho.join(", ")}`);r=e.activityHandling}return{manualActivity:!0,activityHandling:r}}var Ma=["temperature","maxOutputTokens","responseModalities"];function ko(t){for(let[c,h]of Object.entries(Ta))if(t[c]!==void 0)throw new S(h);for(let c of Object.keys(t))if(!Pa.includes(c))throw new S(`unsupported setup field "${c}"`);if(typeof t.model!="string"||t.model.length===0)throw new S('setup.model is required (format "models/{model}")');let e=t.model.replace(/^models\//,""),n,r,o=!1,s=j(t.generationConfig);if(t.generationConfig!==void 0&&!s)throw new S("setup.generationConfig must be an object");if(s){for(let c of Object.keys(s))if(!Ma.includes(c))throw new S(`unsupported generationConfig field "${c}"`);if(s.temperature!==void 0){if(typeof s.temperature!="number")throw new S("generationConfig.temperature must be a number");n=s.temperature}if(s.maxOutputTokens!==void 0){if(typeof s.maxOutputTokens!="number")throw new S("generationConfig.maxOutputTokens must be a number");r=s.maxOutputTokens}if(s.responseModalities!==void 0){let c=Array.isArray(s.responseModalities)?s.responseModalities:[s.responseModalities];for(let h of c){let f=String(h).toUpperCase();if(f==="AUDIO"){o=!0;continue}if(f!=="TEXT")throw new S(`responseModalities ${JSON.stringify(h)} is not supported \u2014 this surface speaks TEXT and AUDIO (audio-out rides the native AudioBridge 'audio' lane)`)}}}let i=[];t.systemInstruction!==void 0&&i.push({role:"system",content:vo(t.systemInstruction,"setup.systemInstruction")});let u;if(t.tools!==void 0){if(!Array.isArray(t.tools))throw new S("setup.tools must be an array");u=[];for(let c of t.tools){let h=j(c);if(!h)throw new S("setup.tools entries must be objects");for(let f of Object.keys(h))if(f!=="functionDeclarations")throw new S(`unsupported tool "${f}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(h.functionDeclarations))throw new S("tool.functionDeclarations must be an array");for(let f of h.functionDeclarations){let p=j(f);if(!p||typeof p.name!="string")throw new S("functionDeclarations entries require a string name");u.push({type:"function",name:p.name,description:p.description,parameters:p.parameters})}}u.length===0&&(u=void 0)}let a={enabled:!1,handle:null};if(t.sessionResumption!==void 0){let c=j(t.sessionResumption);if(!c)throw new S("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let h of Object.keys(c))if(h!=="handle")throw new S(`unsupported sessionResumption field "${h}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(c.handle!==void 0&&(typeof c.handle!="string"||c.handle.length===0))throw new S("sessionResumption.handle must be a non-empty string when present");a={enabled:!0,handle:typeof c.handle=="string"?c.handle:null}}let d=t.realtimeInputConfig===void 0?Oa:Ca(t.realtimeInputConfig),l=!1;if(t[Te]!==void 0){if(!j(t[Te]))throw new S(`setup["${Te}"] must be an object (the v1 opt-in is {})`);l=!0}return{model:e,systemItems:i,tools:u,temperature:n,maxOutputTokens:r,audioModality:o,activity:d,sessionResumption:a,videoOut:l}}function xo(t){return{sessionResumptionUpdate:{newHandle:t,resumable:!0}}}function Ro(t){let e=Math.max(0,t??0)/1e3;return{goAway:{timeLeft:`${Math.round(e*1e3)/1e3}s`}}}function Eo(t){for(let r of Object.keys(t))if(r!=="turns"&&r!=="turnComplete")throw new S(`unsupported clientContent field "${r}"`);let e=t.turns===void 0?[]:t.turns;if(!Array.isArray(e))throw new S("clientContent.turns must be an array");let n=[];for(let r of e){let o=j(r);if(!o)throw new S("clientContent.turns entries must be Content objects");let s=o.role===void 0?"user":String(o.role).toLowerCase();if(s!=="user"&&s!=="model")throw new S(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(o.role)})`);n.push({role:s==="model"?"assistant":"user",content:vo(o,"clientContent.turns")})}return{items:n,turnComplete:t.turnComplete===!0}}var qt=16e3,Ao=24e3,La=`audio/pcm;rate=${Ao}`,Io=Buffer.alloc(960).toString("base64");function $a(t,e,n){if(e===n)return t;let r=Math.floor(t.length*n/e),o=new Int16Array(r),s=e/n,i=t.length-1;for(let u=0;u<r;u++){let a=u*s,d=Math.floor(a);if(d>=i){o[u]=t[i];continue}let l=a-d;o[u]=Math.round(t[d]+(t[d+1]-t[d])*l)}return o}function Ua(t){let e=j(t);if(!e)throw new S("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(e.mimeType))throw new S(`realtimeInput.video.mimeType must be image/jpeg (the Live video input stream is individual JPEG frames), got ${JSON.stringify(e.mimeType)}`);if(typeof e.data!="string"||e.data.length===0)throw new S("realtimeInput.video.data must be non-empty base64");let n=Buffer.from(e.data,"base64");if(n.length<3||n[0]!==255||n[1]!==216)throw new S("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(n)}function Na(t){let e=j(t);if(!e)throw new S("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(e.mimeType))throw new S(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(e.mimeType)}`);let n=/;\s*rate=(\d+)/.exec(e.mimeType);if(n&&Number(n[1])!==qt)throw new S(`realtimeInput.audio.mimeType declares rate=${n[1]} \u2014 the Live spec input format is ${qt} Hz (audio/pcm;rate=${qt})`);if(typeof e.data!="string"||e.data.length===0)throw new S("realtimeInput.audio.data must be non-empty base64");let r=Buffer.from(e.data,"base64");if(r.length===0||r.length%2!==0)throw new S(`realtimeInput.audio.data must decode to whole 16-bit samples (got ${r.length} bytes \u2014 not even)`);let o=new Int16Array(r.length/2);for(let i=0;i<o.length;i++)o[i]=r.readInt16LE(i*2);let s=$a(o,qt,Ao);return Buffer.from(s.buffer,s.byteOffset,s.byteLength).toString("base64")}function Oo(t){if(t.mediaChunks!==void 0)throw new S('realtimeInput.mediaChunks is deprecated by the Live API \u2014 send the audio stream via realtimeInput.audio ({mimeType:"audio/pcm;rate=16000", data}) instead');if(t.activityStart!==void 0||t.activityEnd!==void 0){if(Object.keys(t).length!==1||t.activityStart!==void 0&&t.activityEnd!==void 0)throw new S("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let o=t.activityStart!==void 0?"activityStart":"activityEnd",s=j(t[o]);if(!s||Object.keys(s).length!==0)throw new S(`realtimeInput.${o} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:o}}let e=["text","audio","audioStreamEnd","video"];for(let r of Object.keys(t))if(!e.includes(r))throw new S(`unsupported realtimeInput field "${r}"`);let n=e.filter(r=>t[r]!==void 0);if(n.length!==1)throw new S(`realtimeInput must carry exactly one of ${e.join(", ")} (got ${n.length})`);if(t.audio!==void 0)return{kind:"audio",base64Pcm24k:Na(t.audio)};if(t.video!==void 0)return{kind:"video",jpegFrame:Ua(t.video)};if(t.audioStreamEnd!==void 0){if(t.audioStreamEnd!==!0)throw new S("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof t.text!="string"||t.text.length===0)throw new S("realtimeInput.text must be a non-empty string");return{kind:"text",text:t.text}}function To(t){for(let e of Object.keys(t))if(e!=="functionResponses")throw new S(`unsupported toolResponse field "${e}"`);if(!Array.isArray(t.functionResponses)||t.functionResponses.length===0)throw new S("toolResponse.functionResponses must be a non-empty array");return t.functionResponses.map(e=>{let n=j(e);if(!n||typeof n.id!="string"||n.id.length===0)throw new S("functionResponses entries require the string id matched from the toolCall");return{type:"function_call_output",call_id:n.id,output:JSON.stringify(n.response??null)}})}function Po(t){return t.type!=="response.output_text.delta"||typeof t.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:t.delta}]}}}}function Co(t){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:La,data:t}}]}}}}function Mo(t){let e=j(t)??{},n=Array.isArray(e.output)?e.output:[],r=[],o=[],s=typeof e.output_text=="string"?e.output_text:"";if(!s)for(let d of n){let l=j(d);if(!(l?.type!=="message"||!Array.isArray(l.content)))for(let c of l.content){let h=j(c);h?.type==="output_text"&&typeof h.text=="string"&&(s+=h.text)}}s&&o.push({role:"assistant",content:s});for(let d of n){let l=j(d);if(l?.type!=="function_call")continue;if(typeof l.call_id!="string"||typeof l.name!="string")throw new S(`upstream function_call item is missing call_id/name: ${JSON.stringify(d)}`,_e);let c=typeof l.arguments=="string"&&l.arguments.length>0?l.arguments:"{}",h;try{h=JSON.parse(c)}catch{throw new S(`upstream function_call arguments are not valid JSON: ${c}`,_e)}r.push({id:l.call_id,name:l.name,args:h}),o.push({type:"function_call",call_id:l.call_id,name:l.name,arguments:c})}let i=[];r.length>0&&i.push({toolCall:{functionCalls:r}}),i.push({serverContent:{generationComplete:!0}});let u=j(e.usage),a={serverContent:{turnComplete:!0}};if(u){let d={};typeof u.input_tokens=="number"&&(d.promptTokenCount=u.input_tokens),typeof u.output_tokens=="number"&&(d.responseTokenCount=u.output_tokens),typeof u.total_tokens=="number"&&(d.totalTokenCount=u.total_tokens),Object.keys(d).length>0&&(a.usageMetadata=d)}return i.push(a),{messages:i,assistantItems:o}}var Da=256,Tn=class{snapshots=new Map;store(e,n,r){for(this.snapshots.set(`${e}\0${n}`,r);this.snapshots.size>Da;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(e,n){return this.snapshots.get(`${e}\0${n}`)}},Fa="local",it=t=>t.slice(0,120),Pn=class{constructor(e,n,r){this.ws=e;this.call=n;this.registry=r;this.clients=n.clients,this.scope=n.scope,e.on("message",o=>this.onMessage(o)),e.on("close",()=>{this.unsubscribeEnd?.(),this.unsubscribeEnd=null,this.detachAudio?.(),this.detachVideoOut?.(),this.call.release?.().catch(()=>{})})}ws;call;registry;config=null;transcript=[];queue=Promise.resolve();proxyHandle=null;unsubscribeEnd=null;audio=null;detachAudio=null;detachVideoOut=null;video=null;activityOpen=!1;activeTurn=null;scope;clients;send(e){this.ws.readyState===this.ws.OPEN&&this.ws.send(JSON.stringify(e))}fail(e){if(e instanceof N){this.ws.close(_o,it(e.message));return}if(e instanceof J){this.ws.close(Gt,it(e.message));return}if(e instanceof S){this.ws.close(e.closeCode,it(e.message));return}this.ws.close(_e,it(String(e instanceof Error?e.message:e)))}onMessage(e){try{let n=wo(typeof e=="string"?e:Buffer.concat(Array.isArray(e)?e:[e]).toString("utf8"));if(n.kind==="setup"){this.handleSetup(n.setup).catch(o=>this.fail(o));return}if(!this.config)throw new S(`"${n.kind}" before setup \u2014 the first client message must be BidiGenerateContentSetup`);let r=this.config;if(n.kind==="clientContent"){let{items:o,turnComplete:s}=Eo(n.clientContent);this.transcript.push(...o),s&&this.enqueue(()=>this.runTurn());return}if(n.kind==="realtimeInput"){let o=Oo(n.realtimeInput);if(o.kind==="activityStart"||o.kind==="activityEnd"){this.onActivity(o.kind,r);return}if(o.kind==="text"){this.transcript.push({role:"user",content:o.text}),this.enqueue(()=>this.runTurn());return}if(o.kind==="video"){this.appendVideoFrame(o.jpegFrame);return}this.appendAudio(o.kind==="audio"?o.base64Pcm24k:Io);return}this.transcript.push(...To(n.toolResponse)),this.enqueue(()=>this.runTurn())}catch(n){this.fail(n)}}async handleSetup(e){if(this.config)throw new S("setup may only be sent once, as the first client message");let n=ko(e),r=n.sessionResumption.handle;if(r!==null){let s=this.registry.get(this.scope,r);if(!s)throw new S("unknown sessionResumption.handle \u2014 the Live session it names is gone (proxy restarted, handle evicted, or never issued) and cannot be resumed; reconnect without a handle to start a new session",Gt);if(s.model!==n.model)throw new S(`sessionResumption.handle was issued for model "${s.model}" \u2014 the model cannot change on resume (got "${n.model}", per the Live API session-resumption contract)`);if(n.systemItems.length>0)throw new S("setup.systemInstruction cannot be changed on resume \u2014 it is part of the resumed conversation state");if(this.call.clientsForResume!==void 0&&(this.clients=await this.call.clientsForResume(r)),this.proxyHandle=await this.clients.sessionHandle(n.model),this.proxyHandle!==s.proxyHandle)throw new S(`sessionResumption.handle named pooled session "${s.proxyHandle}" but the re-attach landed on "${this.proxyHandle}" \u2014 the conversation's native session changed and the snapshot cannot be adopted; reconnect without a handle to start a new session`,Gt);this.unsubscribeEnd=await this.clients.onSessionEnd(s.proxyHandle,i=>this.enqueue(()=>this.handleUpstreamEnd(i))),this.transcript=[...s.transcript]}else this.proxyHandle=await this.clients.sessionHandle(n.model),this.unsubscribeEnd=await this.clients.onSessionEnd(this.proxyHandle,s=>this.enqueue(()=>this.handleUpstreamEnd(s))),this.transcript.push(...n.systemItems);this.config=n;let o={};if(n.videoOut){let s=this.clients.openVideoOut;if(s){let i=await s.call(this.clients,n.model),u=i.onOutputFrame(d=>this.send(So(d))),a=i.onLaneError(d=>this.fail(d));this.detachVideoOut=()=>{u(),a()},o[Te]=bo(i.codec)}}this.send({setupComplete:o}),n.audioModality&&this.ensureAudioLane(),n.sessionResumption.enabled&&await this.mintResumptionUpdate()}ensureAudioLane(){if(this.audio)return this.audio;let e=this.config;if(!e)throw new S("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openAudio;if(!n)throw new S("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native AudioBridge seam over the canonical `audio` lane)",_e);return this.audio=(async()=>{let r=await n.call(this.clients,e.model);return this.detachAudio=r.onOutputAudio(o=>this.send(Co(o))),r})(),this.audio.catch(r=>this.fail(r)),this.audio}appendAudio(e){this.ensureAudioLane().then(n=>n.appendInputAudio(e)).catch(n=>this.fail(n))}ensureVideoLane(){if(this.video)return this.video;let e=this.config;if(!e)throw new S("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openVideo;if(!n)throw new S("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",_e);return this.video=Promise.resolve(n.call(this.clients,e.model)),this.video.catch(r=>this.fail(r)),this.video}appendVideoFrame(e){this.ensureVideoLane().then(n=>n.sendInputFrame(e)).catch(n=>this.fail(n))}onActivity(e,n){if(!n.activity.manualActivity)throw new S(`realtimeInput.${e} requires manual activity mode \u2014 set setup.realtimeInputConfig.automaticActivityDetection.disabled: true (automatic server-side activity detection is the declared platform follow-up)`);if(e==="activityStart"){if(this.activityOpen)throw new S("activityStart while an activity window is already open (send activityEnd first)");this.activityOpen=!0,n.activity.activityHandling==="START_OF_ACTIVITY_INTERRUPTS"&&this.activeTurn&&(this.activeTurn.interrupted=!0);return}if(!this.activityOpen)throw new S("activityEnd without an open activity window (send activityStart first)");this.activityOpen=!1}enqueue(e){this.queue=this.queue.then(e,()=>{}),this.queue=this.queue.catch(n=>this.fail(n))}async mintResumptionUpdate(){let e=this.config;if(!e?.sessionResumption.enabled||this.ws.readyState!==this.ws.OPEN)return;let n=await this.clients.sessionHandle(e.model);this.proxyHandle=n;let r=(0,Lo.randomUUID)();this.registry.store(this.scope,r,{model:e.model,proxyHandle:n,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(r),this.send(xo(r))}async handleUpstreamEnd(e){if(this.ws.readyState!==this.ws.OPEN||!this.config)return;this.unsubscribeEnd?.(),this.unsubscribeEnd=null;let n=null;try{n=await this.clients.sessionHandle(this.config.model)}catch{}if(n!==null&&n!==this.proxyHandle){this.proxyHandle=n,this.unsubscribeEnd=await this.clients.onSessionEnd(n,r=>this.enqueue(()=>this.handleUpstreamEnd(r))),this.config.sessionResumption.enabled&&await this.mintResumptionUpdate();return}this.send(Ro(e.timeLeftMs)),this.ws.close(_e,it(`upstream session ended: ${e.reason}`))}async runTurn(){let e=this.config;if(!e||this.ws.readyState!==this.ws.OPEN)return;let n={interrupted:!1};this.activeTurn=n;try{let r=await this.clients.createResponse({model:e.model,input:[...this.transcript],stream:!0,tools:e.tools,temperature:e.temperature,max_output_tokens:e.maxOutputTokens}),o,s=!1;for await(let a of r){if(n.interrupted)break;let d=a;if(d.type==="error")throw new S(`upstream error: ${JSON.stringify(d)}`,_e);if(d.type==="response.completed"){o=d.response,s=!0;continue}let l=Po(d);l&&this.send(l)}if(n.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!s)throw new S("upstream produced no response.completed event",_e);let{messages:i,assistantItems:u}=Mo(o);this.transcript.push(...u);for(let a of i)this.send(a);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},Pe=class extends Error{constructor(n,r){super(r);this.status=n;this.name="UpgradeAuthError"}status};function Ba(t,e,n){let r=new Set;(a=>{if(typeof a=="string")a!==""&&r.add(a);else if(Array.isArray(a))for(let d of a)d!==""&&r.add(d)})(t.headers["x-goog-api-key"]);let s=t.headers.authorization;typeof s=="string"&&s.startsWith("Bearer ")&&s.length>7&&r.add(s.slice(7));let i=e.searchParams.get("key")??void 0;if(i!==void 0&&i!==""&&r.add(i),r.size>1)throw new Pe(401,`contradictory credentials: ${r.size} distinct keys presented across the x-goog-api-key/Authorization headers and the ?key= query parameter \u2014 present exactly one`);if(n&&i!==void 0&&i!=="")throw new Pe(401,"the ?key= query parameter is not accepted on the hosted endpoint \u2014 send the org API key in the x-goog-api-key header or as Authorization: Bearer");let u;for(let a of r)u=a;if(u===void 0&&n)throw new Pe(401,"missing credentials: send the org API key in the x-goog-api-key header or as Authorization: Bearer");return u??""}function Uo(t,e){let n=e.authenticate!==void 0;if(n&&(e.clients!==void 0||e.apiKey!==void 0))throw new TypeError("attachGeminiLive: authenticate (hosted per-request resolution) is mutually exclusive with a fixed clients/apiKey lane");if(!n&&e.clients===void 0)throw new TypeError("attachGeminiLive: pass fixed `clients` (local lane) or `authenticate` (hosted lane)");let r=new $o.WebSocketServer({noServer:!0}),o=new Tn;t.on("upgrade",(i,u,a)=>{let d=new URL(i.url??"/","http://localhost");d.pathname===yo&&(u.on("error",()=>{}),(async()=>{let l=Ba(i,d,n),c;if(e.authenticate)c=await e.authenticate(l,i);else{if(e.apiKey!==void 0&&l!==e.apiKey)throw new Pe(401,"invalid local proxy api key");c={clients:e.clients,scope:Fa}}r.handleUpgrade(i,u,a,h=>{new Pn(h,c,o)})})().catch(l=>{let c=502;(l instanceof Pe||typeof l=="object"&&l!==null&&"status"in l&&typeof l.status=="number")&&(c=l.status);let h=c===401?"Unauthorized":c===403?"Forbidden":c===502?"Bad Gateway":"Internal Server Error",f=l instanceof Error?l.message:String(l);u.destroyed||(u.write(`HTTP/1.1 ${c} ${h}\r
8
8
  Connection: close\r
9
9
  \r
@@ -15,7 +15,7 @@ Connection: close\r
15
15
  Content-Type: text/plain; charset=utf-8\r
16
16
  Content-Length: ${Buffer.byteLength(o)}\r
17
17
  \r
18
- ${o}`)}function Ko(t,e){let n=new Re.WebSocketServer({noServer:!0,maxPayload:Za}),r=new Ce,o=new Set,s=new Set,i=new Set,u=typeof e.clients=="function"?e.clients:()=>e.clients,a=!1,d=(l,c,h)=>{let f;try{f=new URL(l.url??"/","http://localhost")}catch{Ht(c,400,"malformed upgrade URL");return}if(c.on("error",()=>c.destroy()),c.on("close",()=>i.delete(c)),f.pathname!==Qa){i.add(c);return}s.add(c);let p=!1;(async()=>{if(e.apiKey&&(l.headers.authorization??"")!==`Bearer ${e.apiKey}`){Ht(c,401,"invalid local proxy api key");return}let _;try{_=await u(l)}catch(m){let y=e.statusOf?.(m)??null;Ht(c,y?.status??500,"upstream resolver rejected the connection");return}a||c.destroyed||(p=!0,n.handleUpgrade(l,c,h,m=>{s.delete(c),o.add(m),m.on("close",()=>o.delete(m));try{new Un(m,_,r,e.statusOf)}catch{o.delete(m),m.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!p&&!c.destroyed&&Ht(c,500,"responses websocket upgrade failed")}).finally(()=>s.delete(c))};return t.on("upgrade",d),{stats:r,close:()=>{let{promise:l,resolve:c}=Nn();if(a)return c(),l;a=!0,t.off("upgrade",d);for(let f of s)f.destroy();s.clear();for(let f of i)f.destroy();i.clear();for(let f of o)f.close(1001,"server shutting down");let h=setTimeout(()=>{for(let f of o)f.readyState!==Re.WebSocket.CLOSED&&f.terminate()},2e3);return h.unref?.(),n.close(()=>{clearTimeout(h),c()}),l}}}var Ss=require("@urun-sh/core");var zo=require("@urun-sh/core"),su={no_capacity:429,queue_timeout:429,no_attached_consumer:503,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502},iu={no_capacity:"no ready GPU capacity for this model right now \u2014 the platform will cold-wake it on request; retry",queue_timeout:"the session was admitted but never became live within the cold-wake fence \u2014 the backend is busy or wedged; retry",cold_start_exceeded:"the cold boot exceeded its budget without serving \u2014 the platform keeps pursuing the wake; retry with a longer client timeout",queue_full:"the admission queue for this model is full \u2014 retry after the Retry-After period",admission_busy:"admission is momentarily contended (lock convoy) \u2014 retry shortly",admission_unavailable:"session admission is temporarily unavailable \u2014 retry",backend_error:"the control plane failed while admitting the session",no_attached_consumer:"no consumer is attached to serve this session right now \u2014 the platform is provisioning for the waiting demand; retry shortly"};function Dn(t,e){let n=iu[t.code];return e?`${n} (${e})`:n}function Vt(t){if(t==null)return null;let e=su[t];return e===void 0?null:{status:e,code:t,retryable:e!==502,openaiType:t==="no_capacity"||t==="queue_full"?"capacity_error":t==="queue_timeout"||t==="cold_start_exceeded"||t==="no_attached_consumer"?"scheduling_error":"upstream_error",anthropicType:e===429?"rate_limit_error":e===503?"overloaded_error":"api_error"}}function Jt(t){if(t.kind==="create-failed")return Vt(t.status.error?.code??null);if(t.kind!=="never-live")return null;let e=(t.status.elapsedMs??0)>=zo.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return Vt(e?"cold_start_exceeded":"queue_timeout")}var Xo=Be(require("os"),1),Qo=Be(require("path"),1),Zo=require("url"),es=require("util");var Yo="0.6.22";var ts=4141,uu=Yo,Kt={claude:{bin:"claude",env:"anthropic"},codex:{bin:"codex",env:"openai"},pi:{bin:"pi",env:"openai"},omp:{bin:"omp",env:"openai"},aider:{bin:"aider",env:"openai"},opencode:{bin:"opencode",env:"openai"},crush:{bin:"crush",env:"openai"}};function qn(t,e="openai"){if(e==="anthropic")return{ANTHROPIC_BASE_URL:`http://127.0.0.1:${t}`,ANTHROPIC_API_KEY:"urun-local"};let n=`http://127.0.0.1:${t}/v1`;return{OPENAI_BASE_URL:n,OPENAI_API_BASE:n,OPENAI_API_KEY:"urun-local"}}function ns(t,e,n){let r={...t,...qn(e,n)};return delete r.URUN_API_KEY,delete r.URUN_JWT,r}function rs(t,e,n){let r=["-c","model_providers.urun.name=urun","-c",`model_providers.urun.base_url="http://127.0.0.1:${t}/v1"`,"-c",'model_providers.urun.wire_api="responses"',"-c",'model_provider="urun"'],o=n.indexOf("--"),s=o===-1?n:n.slice(0,o),i=s.some(l=>l==="-m"||l==="--model"||l.startsWith("--model=")),u=n[0]==="exec",a=s.includes("--skip-git-repo-check"),d=u&&!a?["exec","--skip-git-repo-check",...n.slice(1)]:n;return[...r,...i?[]:["-m",e],...d]}function os(t,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--provider","urun","--model",t,...e]}function ss(t,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--model",`urun/${t}`,...e]}function is(t,e,n){let r=(t.URUN_PI_EXTENSION_ENTRY??"").trim();if(r){if(!n(r))throw new Error(`URUN_PI_EXTENSION_ENTRY points at ${r}, which does not exist \u2014 the launcher that injected it (the vendored urun CLI shim) must materialize the pi extension bundle before spawning pi`);return r}let o=(0,Zo.fileURLToPath)(new URL("../pi-extension/standalone.cjs",e));if(!n(o))throw new Error(`pi extension entry not found at ${o} \u2014 the pi lane loads the self-contained @urun-sh/openai dist/pi-extension/standalone.cjs from the installed package (run \`urun compat pi\` via the npm-installed CLI, e.g. \`npx @urun-sh/openai\`), or pass an entry to pi yourself: \`pi -e <path-to-@urun-sh/openai>/dist/pi-extension/standalone.cjs\``);return o}function as(t){let e=Number(t);if(!Number.isInteger(e)||e<0)throw new Error(`--port needs a non-negative integer (0 = OS-assigned ephemeral), got ${t}`);return e}function us(t){if(t[0]!=="--port")return null;let e=as(t[1]);return t.splice(0,2),e}function jn(t){let e=t.indexOf("--port");if(e===-1)return ts;let n=as(t[e+1]);return t.splice(e,2),n}function ds(t){if(t===0)throw new Error("`env` cannot use --port 0: it starts no proxy, so the OS never assigns the ephemeral port \u2014 pass the actual port of the running proxy");return t}function Fn(t,e){let n=(t[e]??"").trim();if(!n)throw new Error(`${e} is not set. The proxy needs URUN_BASE_URL, URUN_ORG_ID, URUN_APP and either URUN_API_KEY (the org key; the proxy vends short-lived function-scoped client tokens from it via the SDK) or URUN_JWT (an explicit pre-vended token override) \u2014 plus optional URUN_FUNCTION, default "serve" \u2014 to open the backhaul session.`);return n}function ze(t){let e=du(Fn(t,"URUN_BASE_URL")),n=Fn(t,"URUN_ORG_ID"),r=Fn(t,"URUN_APP"),o=(t.URUN_FUNCTION??"serve").trim()||"serve",s=(t.URUN_API_URL??"").trim(),i=s?Wt(s,"URUN_API_URL"):eo,u=(t.URUN_JWT??"").trim();if(u)return{baseUrl:e,orgId:n,appId:r,fnName:o,apiUrl:i,auth:{lane:"jwt",jwt:u}};let a=(t.URUN_API_KEY??"").trim();if(a){let d=(t.URUN_GATEWAY_URL??"").trim(),l=d?Wt(d):void 0;return{baseUrl:e,orgId:n,appId:r,fnName:o,apiUrl:i,auth:{lane:"api-key",apiKey:a,gatewayUrl:l}}}throw new Error("neither URUN_API_KEY nor URUN_JWT is set. Set URUN_API_KEY (the org API key \u2014 the proxy vends short-lived, function-scoped client tokens from it via the SDK, so the key never reaches the agent process) or URUN_JWT (an explicit pre-vended token; when set it always wins). URUN_BASE_URL, URUN_ORG_ID and URUN_APP are also required.")}function du(t,e="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(t).pathname))throw new Error(`${e} must be the session-gateway base (e.g. https://api.urun.sh), got ${t} \u2014 a trailing /v1 is the org control-plane API form (that belongs in URUN_API_URL); session allocation 404s against it. Drop the /v1.`);return t}function Wt(t,e="URUN_GATEWAY_URL"){let n=new URL(t),r=n.hostname==="localhost"||n.hostname==="127.0.0.1"||n.hostname==="[::1]";if(n.protocol!=="https:"&&!r)throw new Error(`${e} must be https (got ${t}) \u2014 the org API key rides the request's Authorization header`);return t}function Gn(t){return{app:t.appId,org:t.orgId,fn:t.fnName,base_url:t.baseUrl,proxy_version:uu}}function lu(t,e){return t.app===e.app&&t.org===e.org&&t.fn===e.fn&&t.base_url===e.base_url&&t.proxy_version===e.proxy_version}var cu=250;async function Hn(t,e=cu){try{let n=await fetch(`http://127.0.0.1:${t}/stats`,{signal:AbortSignal.timeout(e)});if(!n.ok)return null;let r=(await n.json()).identity;if(r==null||typeof r!="object")return null;let{app:o,org:s,fn:i,base_url:u,proxy_version:a}=r;return typeof o!="string"||typeof s!="string"||typeof i!="string"||typeof u!="string"||typeof a!="string"?null:{app:o,org:s,fn:i,base_url:u,proxy_version:a}}catch{return null}}var ls=2e3;async function pu(t,e=ls){try{let n=await fetch(`http://127.0.0.1:${t}/v1/models`,{signal:AbortSignal.timeout(e)});return n.ok?null:`GET /v1/models answered HTTP ${n.status}`}catch(n){return`GET /v1/models failed (${String(n instanceof Error?n.message:n)})`}}async function cs(t,e,n=ts,r=ls){if(t!=null)return{mode:"spawn",port:t};let o=await Hn(n);if(o!==null&&lu(o,e)){let s=await pu(n,r);return s===null?{mode:"reuse",port:n,identity:o}:{mode:"spawn",port:0,reason:`a urun-openai proxy on port ${n} matches this invocation's identity but failed the serving-surface liveness check (${s}) \u2014 starting a fresh proxy on an ephemeral port instead of pointing the harness at it`}}return{mode:"spawn",port:0}}function Vn(t,e=Xo.default.homedir()){return Qo.default.join(e,".urun","logs",`compat-proxy-${t}.log`)}var Bn=["log","info","warn","error","debug","trace"];function ps(t,e=process,n=console){let r=e.stdout.write,o=e.stderr.write,s=(u,...a)=>t.write(u,...a);e.stdout.write=s,e.stderr.write=s;let i=Object.fromEntries(Bn.map(u=>[u,n[u]]));for(let u of Bn)n[u]=(...a)=>{t.write(`${(0,es.format)(...a)}
18
+ ${o}`)}function Ko(t,e){let n=new Re.WebSocketServer({noServer:!0,maxPayload:Za}),r=new Ce,o=new Set,s=new Set,i=new Set,u=typeof e.clients=="function"?e.clients:()=>e.clients,a=!1,d=(l,c,h)=>{let f;try{f=new URL(l.url??"/","http://localhost")}catch{Ht(c,400,"malformed upgrade URL");return}if(c.on("error",()=>c.destroy()),c.on("close",()=>i.delete(c)),f.pathname!==Qa){i.add(c);return}s.add(c);let p=!1;(async()=>{if(e.apiKey&&(l.headers.authorization??"")!==`Bearer ${e.apiKey}`){Ht(c,401,"invalid local proxy api key");return}let _;try{_=await u(l)}catch(m){let y=e.statusOf?.(m)??null;Ht(c,y?.status??500,"upstream resolver rejected the connection");return}a||c.destroyed||(p=!0,n.handleUpgrade(l,c,h,m=>{s.delete(c),o.add(m),m.on("close",()=>o.delete(m));try{new Un(m,_,r,e.statusOf)}catch{o.delete(m),m.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!p&&!c.destroyed&&Ht(c,500,"responses websocket upgrade failed")}).finally(()=>s.delete(c))};return t.on("upgrade",d),{stats:r,close:()=>{let{promise:l,resolve:c}=Nn();if(a)return c(),l;a=!0,t.off("upgrade",d);for(let f of s)f.destroy();s.clear();for(let f of i)f.destroy();i.clear();for(let f of o)f.close(1001,"server shutting down");let h=setTimeout(()=>{for(let f of o)f.readyState!==Re.WebSocket.CLOSED&&f.terminate()},2e3);return h.unref?.(),n.close(()=>{clearTimeout(h),c()}),l}}}var Ss=require("@urun-sh/core");var zo=require("@urun-sh/core"),su={no_capacity:429,queue_timeout:429,no_attached_consumer:503,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502},iu={no_capacity:"no ready GPU capacity for this model right now \u2014 the platform will cold-wake it on request; retry",queue_timeout:"the session was admitted but never became live within the cold-wake fence \u2014 the backend is busy or wedged; retry",cold_start_exceeded:"the cold boot exceeded its budget without serving \u2014 the platform keeps pursuing the wake; retry with a longer client timeout",queue_full:"the admission queue for this model is full \u2014 retry after the Retry-After period",admission_busy:"admission is momentarily contended (lock convoy) \u2014 retry shortly",admission_unavailable:"session admission is temporarily unavailable \u2014 retry",backend_error:"the control plane failed while admitting the session",no_attached_consumer:"no consumer is attached to serve this session right now \u2014 the platform is provisioning for the waiting demand; retry shortly"};function Dn(t,e){let n=iu[t.code];return e?`${n} (${e})`:n}function Vt(t){if(t==null)return null;let e=su[t];return e===void 0?null:{status:e,code:t,retryable:e!==502,openaiType:t==="no_capacity"||t==="queue_full"?"capacity_error":t==="queue_timeout"||t==="cold_start_exceeded"||t==="no_attached_consumer"?"scheduling_error":"upstream_error",anthropicType:e===429?"rate_limit_error":e===503?"overloaded_error":"api_error"}}function Jt(t){if(t.kind==="create-failed")return Vt(t.status.error?.code??null);if(t.kind!=="never-live")return null;let e=(t.status.elapsedMs??0)>=zo.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return Vt(e?"cold_start_exceeded":"queue_timeout")}var Xo=Be(require("os"),1),Qo=Be(require("path"),1),Zo=require("url"),es=require("util");var Yo="0.6.25";var ts=4141,uu=Yo,Kt={claude:{bin:"claude",env:"anthropic"},codex:{bin:"codex",env:"openai"},pi:{bin:"pi",env:"openai"},omp:{bin:"omp",env:"openai"},aider:{bin:"aider",env:"openai"},opencode:{bin:"opencode",env:"openai"},crush:{bin:"crush",env:"openai"}};function qn(t,e="openai"){if(e==="anthropic")return{ANTHROPIC_BASE_URL:`http://127.0.0.1:${t}`,ANTHROPIC_API_KEY:"urun-local"};let n=`http://127.0.0.1:${t}/v1`;return{OPENAI_BASE_URL:n,OPENAI_API_BASE:n,OPENAI_API_KEY:"urun-local"}}function ns(t,e,n){let r={...t,...qn(e,n)};return delete r.URUN_API_KEY,delete r.URUN_JWT,r}function rs(t,e,n){let r=["-c","model_providers.urun.name=urun","-c",`model_providers.urun.base_url="http://127.0.0.1:${t}/v1"`,"-c",'model_providers.urun.wire_api="responses"',"-c",'model_provider="urun"'],o=n.indexOf("--"),s=o===-1?n:n.slice(0,o),i=s.some(l=>l==="-m"||l==="--model"||l.startsWith("--model=")),u=n[0]==="exec",a=s.includes("--skip-git-repo-check"),d=u&&!a?["exec","--skip-git-repo-check",...n.slice(1)]:n;return[...r,...i?[]:["-m",e],...d]}function os(t,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--provider","urun","--model",t,...e]}function ss(t,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--model",`urun/${t}`,...e]}function is(t,e,n){let r=(t.URUN_PI_EXTENSION_ENTRY??"").trim();if(r){if(!n(r))throw new Error(`URUN_PI_EXTENSION_ENTRY points at ${r}, which does not exist \u2014 the launcher that injected it (the vendored urun CLI shim) must materialize the pi extension bundle before spawning pi`);return r}let o=(0,Zo.fileURLToPath)(new URL("../pi-extension/standalone.cjs",e));if(!n(o))throw new Error(`pi extension entry not found at ${o} \u2014 the pi lane loads the self-contained @urun-sh/openai dist/pi-extension/standalone.cjs from the installed package (run \`urun compat pi\` via the npm-installed CLI, e.g. \`npx @urun-sh/openai\`), or pass an entry to pi yourself: \`pi -e <path-to-@urun-sh/openai>/dist/pi-extension/standalone.cjs\``);return o}function as(t){let e=Number(t);if(!Number.isInteger(e)||e<0)throw new Error(`--port needs a non-negative integer (0 = OS-assigned ephemeral), got ${t}`);return e}function us(t){if(t[0]!=="--port")return null;let e=as(t[1]);return t.splice(0,2),e}function jn(t){let e=t.indexOf("--port");if(e===-1)return ts;let n=as(t[e+1]);return t.splice(e,2),n}function ds(t){if(t===0)throw new Error("`env` cannot use --port 0: it starts no proxy, so the OS never assigns the ephemeral port \u2014 pass the actual port of the running proxy");return t}function Fn(t,e){let n=(t[e]??"").trim();if(!n)throw new Error(`${e} is not set. The proxy needs URUN_BASE_URL, URUN_ORG_ID, URUN_APP and either URUN_API_KEY (the org key; the proxy vends short-lived function-scoped client tokens from it via the SDK) or URUN_JWT (an explicit pre-vended token override) \u2014 plus optional URUN_FUNCTION, default "serve" \u2014 to open the backhaul session.`);return n}function ze(t){let e=du(Fn(t,"URUN_BASE_URL")),n=Fn(t,"URUN_ORG_ID"),r=Fn(t,"URUN_APP"),o=(t.URUN_FUNCTION??"serve").trim()||"serve",s=(t.URUN_API_URL??"").trim(),i=s?Wt(s,"URUN_API_URL"):eo,u=(t.URUN_JWT??"").trim();if(u)return{baseUrl:e,orgId:n,appId:r,fnName:o,apiUrl:i,auth:{lane:"jwt",jwt:u}};let a=(t.URUN_API_KEY??"").trim();if(a){let d=(t.URUN_GATEWAY_URL??"").trim(),l=d?Wt(d):void 0;return{baseUrl:e,orgId:n,appId:r,fnName:o,apiUrl:i,auth:{lane:"api-key",apiKey:a,gatewayUrl:l}}}throw new Error("neither URUN_API_KEY nor URUN_JWT is set. Set URUN_API_KEY (the org API key \u2014 the proxy vends short-lived, function-scoped client tokens from it via the SDK, so the key never reaches the agent process) or URUN_JWT (an explicit pre-vended token; when set it always wins). URUN_BASE_URL, URUN_ORG_ID and URUN_APP are also required.")}function du(t,e="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(t).pathname))throw new Error(`${e} must be the session-gateway base (e.g. https://api.urun.sh), got ${t} \u2014 a trailing /v1 is the org control-plane API form (that belongs in URUN_API_URL); session allocation 404s against it. Drop the /v1.`);return t}function Wt(t,e="URUN_GATEWAY_URL"){let n=new URL(t),r=n.hostname==="localhost"||n.hostname==="127.0.0.1"||n.hostname==="[::1]";if(n.protocol!=="https:"&&!r)throw new Error(`${e} must be https (got ${t}) \u2014 the org API key rides the request's Authorization header`);return t}function Gn(t){return{app:t.appId,org:t.orgId,fn:t.fnName,base_url:t.baseUrl,proxy_version:uu}}function lu(t,e){return t.app===e.app&&t.org===e.org&&t.fn===e.fn&&t.base_url===e.base_url&&t.proxy_version===e.proxy_version}var cu=250;async function Hn(t,e=cu){try{let n=await fetch(`http://127.0.0.1:${t}/stats`,{signal:AbortSignal.timeout(e)});if(!n.ok)return null;let r=(await n.json()).identity;if(r==null||typeof r!="object")return null;let{app:o,org:s,fn:i,base_url:u,proxy_version:a}=r;return typeof o!="string"||typeof s!="string"||typeof i!="string"||typeof u!="string"||typeof a!="string"?null:{app:o,org:s,fn:i,base_url:u,proxy_version:a}}catch{return null}}var ls=2e3;async function pu(t,e=ls){try{let n=await fetch(`http://127.0.0.1:${t}/v1/models`,{signal:AbortSignal.timeout(e)});return n.ok?null:`GET /v1/models answered HTTP ${n.status}`}catch(n){return`GET /v1/models failed (${String(n instanceof Error?n.message:n)})`}}async function cs(t,e,n=ts,r=ls){if(t!=null)return{mode:"spawn",port:t};let o=await Hn(n);if(o!==null&&lu(o,e)){let s=await pu(n,r);return s===null?{mode:"reuse",port:n,identity:o}:{mode:"spawn",port:0,reason:`a urun-openai proxy on port ${n} matches this invocation's identity but failed the serving-surface liveness check (${s}) \u2014 starting a fresh proxy on an ephemeral port instead of pointing the harness at it`}}return{mode:"spawn",port:0}}function Vn(t,e=Xo.default.homedir()){return Qo.default.join(e,".urun","logs",`compat-proxy-${t}.log`)}var Bn=["log","info","warn","error","debug","trace"];function ps(t,e=process,n=console){let r=e.stdout.write,o=e.stderr.write,s=(u,...a)=>t.write(u,...a);e.stdout.write=s,e.stderr.write=s;let i=Object.fromEntries(Bn.map(u=>[u,n[u]]));for(let u of Bn)n[u]=(...a)=>{t.write(`${(0,es.format)(...a)}
19
19
  `)};return()=>{e.stdout.write=r,e.stderr.write=o;for(let u of Bn)n[u]=i[u]}}var fu=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function fs(t){let e=t?.code;return typeof e=="string"&&fu.has(e)}function le(t,e){try{return t.write(e),!0}catch(n){if(fs(n))return!1;throw n}}function ms(t){let e={stdout:!1,stderr:!1},n=[];for(let r of["stdout","stderr"]){let o=t.streams[r],s=i=>{fs(i)&&(e[r]=!0);let u=i?.code;try{t.note(`${new Date().toISOString()} urun compat: process.${r} died (${String(u??(i instanceof Error?i.message:i))}) \u2014 the terminal is gone; launcher output continues in this log only
20
20
  `)}catch{}};o.on("error",s),n.push([o,s])}return{isDead:r=>e[r],uninstall:()=>{for(let[r,o]of n)r.off("error",o)}}}function gs(t){let e=n=>{let r=n instanceof Error?`${n.stack??`${n.name}: ${n.message}`}`:String(n);try{t.appendSync(t.logPath,`${new Date().toISOString()} launcher crashed (in-process proxy): ${r}
21
21
  `)}catch{}t.restore();try{t.writeStderr(`urun compat: the in-process proxy crashed (${n instanceof Error?n.message:String(n)}) \u2014 terminating the harness (its backend is gone); details: ${t.logPath}
package/dist/proxy/cli.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import{a as J,b as q,c as H,d as Y,e as V}from"../chunk-KSC5Q6PZ.js";import"../chunk-K23AZPI4.js";import{A as F,Y as K,f as v,g as $,h as C,i as U,j as I,k as L,l as N,m as O,n as x,o as _,p as g,r as G,s as P,t as D,u as M,v as A,w as W,x as b,y as B,z as j}from"../chunk-4W3ZPIR7.js";import"../chunk-76BZLCGT.js";import"../chunk-CUDFHFNX.js";import"../chunk-3JWYIKHM.js";import{g as R}from"../chunk-6M5JK4YC.js";import"../chunk-7V2JKPIG.js";import{c as h}from"../chunk-YSFSRI3D.js";h();import{spawn as ee}from"child_process";import{appendFileSync as T,createWriteStream as de,existsSync as pe,mkdirSync as ne}from"fs";import re from"path";import n from"process";h();import{ensureNodeWebRTC as ae,nodeWebRTCUnavailableReason as le}from"@urun-sh/core/internal";var ue={ensureWebRTC:ae,webrtcUnavailableReason:le,audioBackend:R};async function z(e=ue){try{await e.ensureWebRTC()}catch(t){return{ok:!1,reason:`the WebRTC bootstrap threw: ${String(t)}`}}let i=e.webrtcUnavailableReason();if(i)return{ok:!1,reason:`${i} \u2014 this artifact would join every session doc/KV-only and every token stream would error. Either "werift" is missing from the deployment directory (npm lane), or it was marked --external in a single-file build, where nothing can ever resolve it (bunfs: "Cannot find package 'werift' from '/$bunfs/root/\u2026'").`};try{(await e.audioBackend()).createSource()}catch(t){return{ok:!1,reason:`the werift/opus audio backend could not be constructed: ${String(t)}`}}return{ok:!0,reason:null}}function fe(e,i){let t=(e.URUN_CATALOG_URL??"").trim(),l=t?G(t,"URUN_CATALOG_URL"):"",s=(e.URUN_CATALOG_ANON_KEY??"").trim();return!l||!s?(i(`urun compat: catalog oracle off (URUN_CATALOG_URL/URUN_CATALOG_ANON_KEY unset) \u2014 an undeployed uRun CATALOG model name falls through to the default app instead of a loud 404; deployed apps still resolve exactly, and /stats.models records every mapping
2
+ import{a as J,b as q,c as H,d as Y,e as V}from"../chunk-PPPJ42Z7.js";import"../chunk-K23AZPI4.js";import{A as F,Y as K,f as v,g as $,h as C,i as U,j as I,k as L,l as N,m as O,n as x,o as _,p as g,r as G,s as P,t as D,u as M,v as A,w as W,x as b,y as B,z as j}from"../chunk-SOAZAPCO.js";import"../chunk-76BZLCGT.js";import"../chunk-CUDFHFNX.js";import"../chunk-3JWYIKHM.js";import{g as R}from"../chunk-6M5JK4YC.js";import"../chunk-7V2JKPIG.js";import{c as h}from"../chunk-YSFSRI3D.js";h();import{spawn as ee}from"child_process";import{appendFileSync as T,createWriteStream as de,existsSync as pe,mkdirSync as ne}from"fs";import re from"path";import n from"process";h();import{ensureNodeWebRTC as ae,nodeWebRTCUnavailableReason as le}from"@urun-sh/core/internal";var ue={ensureWebRTC:ae,webrtcUnavailableReason:le,audioBackend:R};async function z(e=ue){try{await e.ensureWebRTC()}catch(t){return{ok:!1,reason:`the WebRTC bootstrap threw: ${String(t)}`}}let i=e.webrtcUnavailableReason();if(i)return{ok:!1,reason:`${i} \u2014 this artifact would join every session doc/KV-only and every token stream would error. Either "werift" is missing from the deployment directory (npm lane), or it was marked --external in a single-file build, where nothing can ever resolve it (bunfs: "Cannot find package 'werift' from '/$bunfs/root/\u2026'").`};try{(await e.audioBackend()).createSource()}catch(t){return{ok:!1,reason:`the werift/opus audio backend could not be constructed: ${String(t)}`}}return{ok:!0,reason:null}}function fe(e,i){let t=(e.URUN_CATALOG_URL??"").trim(),l=t?G(t,"URUN_CATALOG_URL"):"",s=(e.URUN_CATALOG_ANON_KEY??"").trim();return!l||!s?(i(`urun compat: catalog oracle off (URUN_CATALOG_URL/URUN_CATALOG_ANON_KEY unset) \u2014 an undeployed uRun CATALOG model name falls through to the default app instead of a loud 404; deployed apps still resolve exactly, and /stats.models records every mapping
3
3
  `),null):{catalogUrl:l,anonKey:s}}function me(e){let i={baseUrl:e.baseUrl,orgId:e.orgId,defaultApp:e.appId,fnName:e.fnName,apiUrl:e.apiUrl,auth:e.auth,catalog:fe(n.env,l=>n.stderr.write(l)),decisionPool:J(n.env,n.env.HOSTNAME)??void 0},t=H(i);return{router:t,decision:i.decisionPool?Y(i,t):null}}async function Q(e,i,t){let l=ee(e,i,{stdio:"inherit",env:t});return new Promise(s=>{l.once("exit",r=>s(r??1)),l.once("error",r=>{n.stderr.write(`failed to launch ${e}: ${String(r)}
4
4
  `),s(127)})})}function X(){return N(n.env,import.meta.url,pe)}function te(e){for(let[i,t]of Object.entries($(e)))n.stdout.write(`export ${i}=${t}
5
5
  `)}async function Z(e,i){let t=g(n.env),l=i?P(t):void 0,{router:s,decision:r}=me(t);r&&(r.boot().then(()=>r.reap()).catch(u=>n.stderr.write(`[decision-pool] boot FAILED: ${String(u instanceof Error?u.message:u)}