@urun-sh/openai 0.6.18 → 0.6.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,80 +1,80 @@
1
1
  #!/usr/bin/env node
2
- "use strict";var ci=Object.create;var cr=Object.defineProperty;var pi=Object.getOwnPropertyDescriptor;var fi=Object.getOwnPropertyNames;var mi=Object.getPrototypeOf,gi=Object.prototype.hasOwnProperty;var hi=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of fi(e))!gi.call(t,o)&&o!==n&&cr(t,o,{get:()=>e[o],enumerable:!(r=pi(e,o))||r.enumerable});return t};var Ie=(t,e,n)=>(n=t!=null?ci(mi(t)):{},hi(e||!t||!t.__esModule?cr(n,"default",{value:t,enumerable:!0}):n,t));var yi=()=>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=yi();var ar=require("child_process"),ce=require("fs"),ur=Ie(require("path"),1),M=Ie(require("process"),1);var Te=require("@urun-sh/core");function pr(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 fr(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 mr(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 wi="llm-resp";async function*pn(t,e){let n=`${wi}:${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 a of i.tool_calls){let u=typeof a.index=="number"?a.index:0,d=o.get(u)??{};a.id&&(d.id=a.id),a.function?.name&&(d.name=a.function.name),o.set(u,d),yield{type:"response.function_call_arguments.delta",item_id:`fc_${e}_${u}`,tool_index:u,call_id:d.id,name:d.name,delta:a.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 mr(i);return}}}finally{try{t.doc("llm").set({requests:{[e]:null}})}catch{}}}var kt=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=fr(e,n,this.consumerId);return this.write(r),pn(this.session,n)})}sendResponseCreate(e,n,r){return this.writeGated(r,()=>{let o=pr(e,n,r,this.consumerId);return this.write(o),pn(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 gr=0;function _i(){return gr+=1,`req_${Date.now().toString(36)}_${gr.toString(36)}`}var St=class{transport;constructor(e){this.transport=new kt(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 wr="audio",fn=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 _r(t,e,n=1e4){let r=new fn(e,{sampleRate:24e3}),o=await r.startOutbound();await t.stream(wr).attach(o);let s=await vi(t,n);return await r.startInbound(s),r}function vi(t,e){let n=t.stream(wr);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),a=u=>{u!=null&&(clearTimeout(i),s?.(),r(u))};s=n.on?.("track",a),n.track!=null&&a(n.track)})}var hr="rt-video-in",yr=256e3;function vr(t){let e=t.stream(hr),n=e.emit;if(typeof n!="function")throw new Error(`enableSessionVideo: session.stream('${hr}') 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>yr)throw new Error(`enableSessionVideo: frame is ${r.byteLength} bytes \u2014 over the ${yr}-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 bi=24e3,Xe=480,ki=111;async function Si(){let t=await import("opusscript"),e=t.default??t;return new e(bi,1,2048)}async function Rt(){let t=await import("werift"),e=await Si();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:a=>{let u=new Int16Array(i.length+a.length);u.set(i,0),u.set(a,i.length);let d=0;for(;u.length-d>=Xe;){let l=u.subarray(d,d+Xe);d+=Xe;let p=Buffer.from(l.buffer,l.byteOffset,l.byteLength),m=e.encode(p,Xe);o=o+1&65535,s=s+Xe>>>0;let c=new t.RtpHeader({version:2,payloadType:ki,sequenceNumber:o,timestamp:s,ssrc:r,marker:!1}),f=new t.RtpPacket(c,m);n.writeRtp(f)}i=u.slice(d)}}},createSink:n=>{let r={onframe:null};return n.onReceiveRtp.subscribe(s=>{let i;try{i=e.decode(s.payload)}catch{return}let a=new Int16Array(i.length/2);for(let u=0;u<a.length;u++)a[u]=i.readInt16LE(u*2);r.onframe?.({samples:a})}),r}}}async function br(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 xt=require("@urun-sh/core"),mn="rt-video-out";var Ri="h264";function xi(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 Ei(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;xi(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 kr="urun.videoOut.v2-ws";function Sr(t){let e=t.stream(mn);if(typeof e.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${mn}') 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=u=>{for(let d of r)d(u)},i=!1,a=async()=>{for await(let u of e.messages()){if(!(u instanceof Uint8Array)){s(new Error(`openSessionVideoOutLane: "${mn}" delivered a non-BINARY \xA75 payload (${typeof u}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let d;try{d=Ei(u)}catch(l){s(l instanceof Error?l:new Error(String(l)));continue}for(let l of n)l(d)}for(let u of o)u()};return{codec:Ri,onOutputFrame:u=>(n.add(u),i||(i=!0,a().catch(d=>s(d instanceof Error?d:new Error(String(d))))),()=>n.delete(u)),onLaneError:u=>(r.add(u),()=>r.delete(u)),onEnd:u=>(o.add(u),()=>o.delete(u))}}var Rr={delta:!0,final:!0,turn_start:!0,turn_end:!0,turn_eager_end:!0,turn_resumed:!0},Be=class extends Error{constructor(e){super(e),this.name="TurnLaneDecodeError"}};function xr(t){let e=t.t;if(typeof e!="string"||!(e in Rr))throw new Be(`the "stt" transcript lane delivered an unknown turn kind ${JSON.stringify(e??null)} \u2014 known kinds: ${Object.keys(Rr).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 Ai(t){if(typeof t=="string"){let e;try{e=JSON.parse(t)}catch(n){throw new Be(`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 Be(`the "stt" transcript lane delivered a JSON string that is not an object (${JSON.stringify(e)})`);return xr(e)}if(t===null||typeof t!="object"||Array.isArray(t))throw new Be(`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 xr(t)}function Er(t){return t.reason instanceof Error?t.reason:new Error("the turn-lane consumer aborted")}async function Ii(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 Ar(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 Er(e);let s=o.next();if(e!==void 0&&await Ii(s,e)==="aborted")throw s.catch(()=>{}),Er(e);let i=await s;if(i.done)return;yield Ai(i.value)}}finally{let s=o.return?.();s&&s.catch(()=>{})}}return r()}var Ir=require("@urun-sh/core"),gn=2;function Oi(t){let e=t?.type;return e==="response.created"||e==="response.in_progress"}function Ti(t){let e=t?.type;return e==="response.completed"||e==="error"}function Or(t){return async function(n,r){return(async function*(){for(let s=1;s<=gn;s++){let{app:i,entry:a,identity:u}=await t.sessionFor(n.model);r?.({sessionId:t.sessionIdOf?.(a)??null,identity:u});let d=!1,l=!1;try{let p=await t.createStream(a,n);for await(let m of p){if(Ti(m)&&(d=!0),Oi(m)){if(s>1)continue}else l=!0;yield m}}catch(p){if(p instanceof Ir.SessionFailedError&&(p.kind==="never-live"||p.kind==="create-failed")){try{t.onAdmissionVerdict?.(i,a,p)}catch{}throw p}if(s<gn&&!l){await t.evict(i,a);continue}throw p}if(d)return;if(s<gn&&!l){await t.evict(i,a);continue}return}})()}}var se=class extends Error{name="ModelPriceError"},hn="per_million_input_tokens",yn="per_million_output_tokens",Pr="model_id,variant,unit,price_usd,effective_at,expires_at",Tr={per_request:!0,per_minute:!0,per_output_unit:!0},Pi=5e3;function Ci(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 Cr(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 $i(t,e,n){return t===null?null:Cr(t,e,n)}function Mi(t,e){if(t===hn)return"input";if(t===yn)return"output";if(typeof t=="string"&&Object.hasOwn(Tr,t))return null;throw new se(`${e}: unit ${JSON.stringify(t)} is neither a per-token rate (${hn} | ${yn}) nor a known GPU-time unit (${Object.keys(Tr).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 Li(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 wn(t,e){return`${t}\0${e}`}function Ui(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 Pr.split(","))if(!Object.hasOwn(s,d))throw new se(`${o} has no "${d}" column: ${JSON.stringify(s)}`);let i=Mi(s.unit,o);if(i===null)continue;let a=s.model_id;if(typeof a!="string"||a==="")throw new se(`${o} has no model_id (${JSON.stringify(s)})`);let u=s.variant;if(!(u===null||typeof u=="string"&&u!==""))throw new se(`${o} has a non-string variant (${JSON.stringify(u)})`);e.push({model_id:a,variant:u,side:i,usd:Ci(s.price_usd,o),effective_at:Cr(s.effective_at,o,"effective_at"),expires_at:$i(s.expires_at,o,"expires_at")})}return e}function $r(t,e=Date.now(),n){let r=new Map,o=new Map;for(let u of t){if(u.effective_at>e||u.expires_at!==null&&u.expires_at<=e)continue;let d=r.get(u.model_id);d||(d=new Map,r.set(u.model_id,d),o.set(u.model_id,new Map));let l=u.variant??"",p=d.get(l);p||(p={input:[],output:[]},d.set(l,p),o.get(u.model_id).set(l,u.variant)),p[u.side].push({effective_at:u.effective_at,usd:u.usd})}let s=[],i=[];for(let[u,d]of r){let l=d.get(""),p=(m,c,f)=>{let g=m&&m.length>0?m:l?.[c];if(!g||g.length===0)return null;let _=Li(g,c);return"reason"in _?_:_.rate.usd===0?c==="output"&&f!==null&&n?.has(wn(u,f))?_.rate:{reason:`the ${c} 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`}:_.rate};for(let[m,c]of o.get(u)){let f=d.get(m),g=p(f?.input,"input",c),_=p(f?.output,"output",c);if(g!==null&&"reason"in g){i.push({model_id:u,variant:c,reason:g.reason});continue}if(_!==null&&"reason"in _){i.push({model_id:u,variant:c,reason:_.reason});continue}g===null||_===null||s.push({model_id:u,variant:c,pricing:{input:g.usd,output:_.usd}})}}let a=(u,d)=>u.model_id.localeCompare(d.model_id)||(u.variant===null?1:0)-(d.variant===null?1:0)||(u.variant??"").localeCompare(d.variant??"");return{prices:s.sort(a),refusals:i.sort(a)}}function Qe(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 Ze={prices:[],refusals:[]};async function Mr(t){let e=t.fetchImpl??fetch,n=`${hn},${yn}`,r=await e(`${t.catalogUrl}/model_prices?select=${Pr}&unit=in.(${n})`,{headers:{apikey:t.anonKey,Authorization:`Bearer ${t.anonKey}`},signal:AbortSignal.timeout(Pi)});if(!r.ok)throw new Error(`model_prices fetch failed: ${r.status}`);return Ui(await r.json())}var W=class extends Error{},Ni=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;function Di(t){if(typeof t!="string")return!1;let e=Ni.exec(t);if(!e)return!1;let[,n,r,o,s,i,a]=e.map(Number);if(!Number.isFinite(Date.parse(t)))return!1;let u=new Date(Date.UTC(n,r-1,o,s,i,a));return u.getUTCFullYear()===n&&u.getUTCMonth()===r-1&&u.getUTCDate()===o&&u.getUTCHours()===s&&u.getUTCMinutes()===i&&u.getUTCSeconds()===a}function Fi(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((a,u)=>{if(a===null||typeof a!="object")throw new W(`catalog shared endpoints[${u}] is not an object: ${JSON.stringify(a)}`);let d=a;for(let p of["model_id","variant","app_slug","function"])if(typeof d[p]!="string"||d[p].length===0)throw new W(`catalog shared endpoints[${u}] (${JSON.stringify(d)}) has no valid "${p}"`);let l=(p,m)=>{if(d[p]!==void 0&&d[p]!==null&&!m)throw new W(`catalog shared endpoints[${u}] (${d.model_id}:${d.variant}) carries a malformed "${p}": ${JSON.stringify(d[p])}`)};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",Di(d.ready_since));for(let p of["model_id","variant"]){let m=d[p];if(m.includes(":"))throw new W(`catalog shared endpoints[${u}] has "${p}" containing ":" (${JSON.stringify(m)}) \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(a=>typeof a=="string"&&a.length>0)))throw new W(`catalog "shared" block regions is malformed: ${JSON.stringify(o)}`);if(s!=null){let a=s;if(typeof s!="object"||typeof a.zdr!="boolean"||typeof a.hipaa!="boolean")throw new W(`catalog "shared" block compliance is malformed (operator-declared zdr + hipaa booleans required): ${JSON.stringify(s)}`);for(let u of Object.keys(a))if(u!=="zdr"&&u!=="hipaa")throw new W(`catalog "shared" block compliance carries "${u}" 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 Lr(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 a=t.endpoints.filter(d=>d.model_id===o);if(a.length===0)return null;if(s!==void 0){let d=a.find(l=>l.variant===s);return d?{shared_org_id:t.shared_org_id,endpoint:d}:null}if(a.length===1)return{shared_org_id:t.shared_org_id,endpoint:a[0]};let u=a.filter(d=>d.compat_default===!0);if(u.length!==1)throw new W(`shared model "${o}" has ${u.length} compat_default rows (exactly one required) among variants ${a.map(d=>d.variant).join(", ")}`);return{shared_org_id:t.shared_org_id,endpoint:u[0]}}async function Ur(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 Fi(await r.json())}function ye(t){return t.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}var et="/partners/openrouter/models",Bi={chat:!0,code:!0,agent:!0,vl:!0},Nr={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function ji(t){let e=t.toLowerCase();return Object.hasOwn(Nr,e)?Nr[e]:null}var Dr={"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 qi(t,e){if(!t||t.length===0)return[];let n=[],r=[];for(let o of t){let s=Object.hasOwn(Dr,o)?Dr[o]:null;if(s===null){r.push(o);continue}n.some(a=>a.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 Fr={qwen:"Qwen",qwen1:"Qwen",qwen2:"Qwen",qwen3:"Qwen",gemma:"Gemma",diffusiongemma:"Gemma",gpt:"GPT",glm:"GLM",deepseek:"DeepSeek",mistral:"Mistral"},Br={2:"Llama2",3:"Llama3"};function Hi(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(Fr,s))return Fr[s];let i=r[o+1];if(s==="llama"&&i!==void 0&&Object.hasOwn(Br,i))return Br[i];if(s==="phi"&&i!==void 0&&/^\d/.test(i))return"Phi"}return null}var Gi=["context_length","max_model_len"];function Vi(t){let e=Gi.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 At(t){let e=t.map(Vi),n=e[0];return n==null?null:e.every(r=>r===n)?n:null}function Ji(t){let e=t.engine_args?.reasoning_parser;return e==null||e===""?!1:typeof e=="string"?!0:null}function _n(t){if(t.length===0)return null;let e=t.map(Ji),n=e[0];return n==null?null:e.every(r=>r===n)?n:null}function Wi(t,e){let n=e.filter(o=>ye(`${o.model_id}-${o.variant}`)===t);if(n.length>0)return Et(n);let r=e.filter(o=>ye(o.model_id)===t);return r.length>0?Et(r):null}function Et(t){let e=t[0];return t.every(r=>r.model_id===e.model_id&&r.variant===e.variant)?{row:t.length===1?e:Ki(t),matches:t}:null}function Ki(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 Hr(t){return 0}function Oe(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 It(t,e,n){let r=Wi(t,e);if(!r)return Oe({id:t,object:"model",created:0,owned_by:"urun"});let{row:o,matches:s}=r,i=At(s),a=Qe(o.model_id,o.variant,n),u=Gr(o,s),d=_n(s);return Oe({id:t,object:"model",created:Hr(o),owned_by:"urun",...i!==null?{context_length:i}:{},...a!==null?{pricing:a}:{},...u.published.length>0?{voices:[...u.published]}:{},...d!==null?{capabilities:{reasoning:d}}:{}})}var zi=["Vivian","Serena","Uncle_Fu","Dylan","Eric","Ryan","Aiden","Ono_Anna","Sohee"];function Gr(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,a)=>i===r[a])))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:[...zi],defect:o}:{published:[],defect:o}}function vn(t){let e=t.length>0?Et(t):null;return e===null?{published:[],defect:null}:Gr(e.row,e.matches)}function jr(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 qr(t){return{input:[{type:"prompt",unit:"token",cost_usd:jr(t.input)}],output:[{type:"completion",unit:"token",cost_usd:jr(t.output)}]}}function Yi(t,e,n,r){if(e.length===0)return null;let o=Et(e);if(!o)return null;let{row:s,matches:i}=o;if(!s.task||Bi[s.task]!==!0)return null;let a=At(i),u=Qe(s.model_id,s.variant,n),d=Hi(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 a!==null&&(l.max_tokens={type:"integer",min:1,max:a,unit:"token"}),{schema_version:"2.4",id:`${s.model_id}:${s.variant}`,name:`${s.model_id}:${s.variant}`,created:Hr(s),quantization:ji(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&&u!==null,is_free:!1,input_modalities:[{type:"text",...a!==null?{supported_inputs:{max_context_length:{value:a,unit:"token"}}}:{},...u?{pricing:qr(u).input}:{}},...s.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:l,...u?{pricing:qr(u).output}:{}}],...r.datacenters.length>0?{datacenters:r.datacenters}:{},...r.compliance?{compliance:r.compliance}:{}}}function Vr(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(p=>`${p.model_id}:${p.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:qi(t?.regions,o),compliance:t?.compliance??null},a=[],u=new Set;for(let d of s){let l=`${d.model_id}:${d.variant}`;if(u.has(l))continue;u.add(l);let p=Yi(d,e.filter(m=>m.model_id===d.model_id&&m.variant===d.variant),n,i);p&&a.push(p)}return{data:a}}var Yr="https://api.urun.sh/v1",U=class extends Error{},X=class extends Error{},kn="urs1.";function Jr(t,e){return kn+Buffer.from(JSON.stringify([t,e]),"utf8").toString("base64url")}function Xi(t){if(!t.startsWith(kn))return null;try{let e=JSON.parse(Buffer.from(t.slice(kn.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 Xr(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 Wr(t){if(t.length===0)return[];if(!t.every(n=>(n.task??void 0)==="image"))return[];let e=Zi(t);return e===null?[]:[e]}function bn(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 Kr(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 Qr="shared:",Tt="speech:";function tt(t){return t.sharedApp?Qr+t.appSlug:t.appSlug}function nt(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 ke(t,e){let n=e.filter(s=>ye(`${s.model_id}-${s.variant}`)===t);if(n.length>0)return n;let r=e.filter(s=>ye(s.model_id)===t);return new Set(r.map(s=>s.variant)).size===1?r:[]}function Qi(t){let e=t.engine_args?.expects_image;return e==null?"edit":e===!1?"generation":e===!0?"edit":null}function Zi(t){let e=t.map(Qi),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 ea(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 Ot=6e4,Pt=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??Ot;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??Ot;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??Ot;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??Ot;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
- `),Ze}let r=$r(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
- `);return r}outputFreeKeys(e){let n=new Set;if(!e)return n;for(let r of e.endpoints)r.output_free===!0&&n.add(wn(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(Qr)||o.startsWith(Tt)||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 U(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=ye(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 a=i.filter(d=>d.startsWith(`${o}-`));if(a.length===1)return{appSlug:a[0]};if(a.length>1)throw new U(`model "${n}" is ambiguous across deployed apps ${a.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let u=s.find(d=>d.app_slug===o||d.app_slug.startsWith(`${o}-`));if(u)throw new U(`model "${n}" maps to app "${u.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=Lr(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(a=>n===a.model_id||n===`${a.model_id}:${a.variant}`||o===ye(a.model_id)||o===ye(`${a.model_id}-${a.variant}`));if(i)throw new U(`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=nt(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 targetFor(e){return this.resolveTarget(e)}async resolveApp(e){return tt(await this.resolveTarget(e))}async sessionFor(e){let n=await this.resolveTarget(e);return this.acquire(n,tt(n))}async speechSessionFor(e){let n=await this.resolveTarget(e);return this.acquire(n,Tt+tt(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:nt(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:Jr(n,this.keyOf(r))}}async speechHandleFor(e){let{app:n,entry:r}=await this.speechSessionFor(e);return{app:n,handle:Jr(n,this.keyOf(r))}}async sessionForHandle(e){let n=Xi(e);if(!n)throw new X(`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(a=>a,()=>null):null;if(i===null)throw new X(`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 X(`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:Ze;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 u=(n?je(n):[]).map(d=>{let l=s.filter(_=>_.model_id===d.model_id&&_.variant===d.variant),p=At(l),m=_n(l),c=Qe(d.model_id,d.variant,i),f=vn(l);return Oe({id:`${d.model_id}:${d.variant}`,object:"model",created:0,owned_by:"urun",...p!==null?{context_length:p}:{},...c!==null?{pricing:c}:{},...f.published.length>0?{voices:[...f.published]}:{},...m!==null?{capabilities:{reasoning:m}}:{}})});return{object:"list",data:this.opts.sharedOnlyListing?u:[...e.map(d=>It(d,s,i)),...u]}}async openRouterModels(){let e=await this.sharedBlock(),[n,r]=await Promise.all([this.catalogRows(),this.pricesForListing(this.outputFreeKeys(e))]);return Vr(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=bn(String(e),r.modelId,r.variant,o,this.opts.listCatalog!==null);return Kr(`${r.modelId}:${r.variant}`,s).includes(n)}return Wr(ke(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(ke(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(ke(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(ke(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):ke(n.appSlug,r);return ea(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):ke(n.appSlug,r);return vn(o)}async imageModelList(){let e=await this.catalogRows(),n=Ze,r=[];for(let s of await this.availableIds()){let i=Wr(ke(s,e));i.length!==0&&r.push(Oe({...It(s,e,n),image_modes:i}))}let o=await this.sharedBlock();if(o)for(let s of je(o)){let i=bn(`${s.model_id}:${s.variant}`,s.model_id,s.variant,e,this.opts.listCatalog!==null),a=Kr(`${s.model_id}:${s.variant}`,i);if(a.length===0)continue;let u=Oe({id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun",image_modes:a});r.push(u)}return{object:"list",data:r}}async speechToTextModelList(){let e=await this.catalogRows(),n=Ze,r=[];for(let s of await this.availableIds())Se(ke(s,e))==="stt"&&r.push(It(s,e,n));let o=await this.sharedBlock();if(o)for(let s of je(o)){let i=bn(`${s.model_id}:${s.variant}`,s.model_id,s.variant,e,this.opts.listCatalog!==null);if(Se(i)!=="stt")continue;let a=Oe({id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun"});r.push(a)}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 ta=3,na="llm",ra=new Set(["paused","error"]);function Zr(t,e){let n=t.doc(na),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,a=new Set,u=[],d=l=>{if(!s){s=l;for(let p of[...a])p(l);a.clear()}};return r&&u.push(n.onConnectionState(l=>{i&&(l.consecutiveFailures<ta||d(new X(`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&&u.push(t.onPhase(l=>{if(l.name==="live"){i=!0;return}i&&ra.has(l.name)&&d(new X(`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),()=>{}):(a.add(l),()=>a.delete(l))},dispose(){a.clear();for(let l of u.splice(0))l()}}}async function*Sn(t,e){let n=e.gone();if(n)throw n;let r=t[Symbol.asyncIterator](),o,s=new Promise((i,a)=>{o=e.onGone(u=>a(u))});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)}}function eo(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 oa(t,e,n){let{baseUrl:r,orgId:o,auth:s}=t,i=typeof t.subject=="function"?t.subject():t.subject,a=n.startsWith(Tt)&&i!==void 0?`${i}:speech`:i,u=e.fnName??t.fnName,d=t.sessionStartOptions?.(n),l=e.sharedApp?{sharedApp:!0,...d}:d,p=g=>({session:g,responses:new St(g),gone:Zr(g,n)});if(s.lane==="jwt"){let g=(0,Te.App)(e.appSlug,{baseUrl:r,orgId:o,jwt:s.jwt});return p(eo(g,e.appSlug,u,l))}let{apiKey:m,gatewayUrl:c}=s,f=(0,Te.App)(e.appSlug,{baseUrl:r,orgId:o,getAccessToken:async()=>(await(0,Te.createClientToken)(m,{baseUrl:c,expiresIn:300,allowedFunctions:[`${e.appSlug}/${u}`],...a===void 0?{}:{subject:a},...e.sharedApp?{sharedApp:!0}:{}})).token});return p(eo(f,e.appSlug,u,l))}function sa(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 ia(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 aa=new Set(["error","paused"]);function no(t){let{auth:e,catalog:n}=t,r=null,o=(i,a)=>{let u=oa(t,i,a);return t.onSessionOpen&&typeof u.session.id=="string"&&u.session.id!==""&&t.onSessionOpen(a,u.session.id),u.gone.onGone(()=>{r?.evict(a,u)}),u},s=new Pt({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"?()=>Xr({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,listCatalog:n?()=>br(n):null,listPrices:n?()=>Mr(n):null,shared:e.lane==="api-key"?()=>Ur({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,sessionKey:sa});return r=s,t.defaultApp!==null&&t.statelessDirect!==!0&&s.seed(t.defaultApp,o({appSlug:t.defaultApp},tt({appSlug:t.defaultApp}))),s}async function Rn(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:Te.DEFAULT_WHEN_LIVE_TIMEOUT_MS,signal:e})}async function to(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 $t(t){return t.reason instanceof Error?t.reason:new Error("the client socket closed")}function ro(t,e=Rt,n,r){let o=new WeakMap,s=new WeakMap,i=new WeakMap,a=new WeakMap,u=new WeakMap,d=c=>{let f=c.session.id;return typeof f=="string"&&f.length>0?f:null},l=c=>{let f=o.get(c);if(!f){let g={waiting:0,gone:new AbortController};u.set(c,g),f=(async()=>{if(await Rn(c.session,g.gone.signal),g.gone.signal.aborted)throw $t(g.gone.signal);u.get(c)===g&&u.delete(c);let _=await _r(c.session,await e());return Object.assign(_,{sessionId:c.session.id})})(),o.set(c,f),f.catch(()=>{o.get(c)===f&&o.delete(c)}),f.then(()=>{u.get(c)===g&&u.delete(c)},()=>{u.get(c)===g&&u.delete(c)})}return f},p=c=>{let f=a.get(c);return f||(f=(async()=>{let g=await l(c);return{appendInputAudio:_=>g.appendInputAudio(_),onOutputAudio:_=>g.onOutputAudio(_),sessionId:d(c)}})(),a.set(c,f),f.catch(()=>{a.get(c)===f&&a.delete(c)})),f},m=async(c,f,g)=>{if(g?.aborted){let h=u.get(c);throw(o.get(c)===void 0||h!==void 0&&h.waiting===0)&&await to(c.session),$t(g)}let _=f(c),y=u.get(c);return y!==void 0&&y.waiting++,g===void 0?_:new Promise((h,w)=>{let A=()=>{y!==void 0&&--y.waiting===0&&u.get(c)===y&&(y.gone.abort(),u.delete(c),o.delete(c),a.delete(c),to(c.session).catch(()=>{})),w($t(g))};g.addEventListener("abort",A,{once:!0});let v=()=>g.removeEventListener("abort",A);_.then(b=>{v(),h(b)},b=>{v(),w(b)})})};return{createResponse:Or({sessionFor:c=>t.sessionFor(c),sessionIdOf:c=>d(c),evict:(c,f)=>t.evict(c,f),onAdmissionVerdict:(c,f,g)=>{ia(g)&&(t.evict(c,f),n?.(c,g.kind==="never-live"?"never-live":"create-failed"))},createStream:async(c,f)=>(await Rn(c.session),Sn(await c.responses.responses.create(f),c.gone))}),listModels:()=>t.modelList(),openRouterModels:()=>t.openRouterModels(),supportsImages:(c,f)=>t.supportsImages(c,f),listImageModels:()=>t.imageModelList(),catalogIdentity:c=>t.catalogIdentityFor(c),servingAppSlug:async c=>await t.servingAppSlugFor(c),supportsAudio:c=>t.supportsAudio(c),supportsSpeech:c=>t.supportsSpeech(c),speechVoices:c=>t.speechVoices(c),supportsTranscription:c=>t.supportsTranscription(c),transcriptStreamFor:c=>t.transcriptStreamFor(c),listSpeechToTextModels:()=>t.speechToTextModelList(),openAudio:async(c,f)=>{let{entry:g}=await t.sessionFor(c);return await m(g,l,f)},openAudioOn:async(c,f)=>{let{entry:g}=await t.sessionForHandle(c);return await m(g,p,f)},openTurnsOn:async function*(c,f){if(f?.aborted)throw $t(f);let{entry:g}=await t.sessionForHandle(c);yield*Ar(g.session,f)},openVideo:async c=>{let{entry:f}=await t.sessionFor(c),g=s.get(f);return g||(g=vr(f.session),s.set(f,g)),g},openVideoOut:async c=>{let{entry:f}=await t.sessionFor(c),g=i.get(f);return g||(g=Sr(f.session),i.set(f,g)),g},sessionHandle:async c=>(await t.handleFor(c)).handle,speechSessionHandle:async c=>(await t.speechHandleFor(c)).handle,evictSpeechSession:async c=>{let{app:f,entry:g}=await t.sessionForHandle(c);await t.evict(f,g)},createResponseOn:async(c,f)=>{let{entry:g}=await t.sessionForHandle(c);return await Rn(g.session),Sn(await g.responses.responses.create(f),g.gone)},onSessionEnd:async(c,f)=>{let{entry:g}=await t.sessionForHandle(c),_=g.session;if(typeof _.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let y=!1;return _.onPhase(h=>{if(y||!aa.has(h.name))return;y=!0;let w=_.endsAt??null;f({timeLeftMs:w?Math.max(0,w.getTime()-Date.now()):null,reason:h.reason??`session ${h.name}`})})},...r?{stateless:r}:{}}}var ti=require("http");var So=require("crypto"),Ro=require("ws");var so="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",ua=1007,io=1008,we=1011,Nt=1008,k=class extends Error{constructor(n,r=ua){super(n);this.closeCode=r}closeCode},q=t=>typeof t=="object"&&t!==null&&!Array.isArray(t)?t:null,Mt=["setup","clientContent","realtimeInput","toolResponse"];function ao(t){let e;try{e=JSON.parse(t)}catch{throw new k("client message is not valid JSON")}let n=q(e);if(!n)throw new k("client message must be a JSON object");let r=Mt.filter(a=>n[a]!==void 0),o=Object.keys(n).filter(a=>!Mt.includes(a));if(o.length>0)throw new k(`unknown client message field(s) ${JSON.stringify(o)} \u2014 expected exactly one of ${Mt.join(", ")}`);if(r.length!==1)throw new k(`client message must contain exactly one of ${Mt.join(", ")} (got ${r.length})`);let s=r[0],i=q(n[s]);if(!i)throw new k(`"${s}" must be a JSON object`);return{kind:s,[s]:i}}function uo(t,e){if(typeof t=="string")return t;let n=q(t),r=n?n.parts:void 0;if(!Array.isArray(r))throw new k(`${e}: expected a Content with a parts array`);let o=[];for(let s of r){let i=q(s);if(i&&typeof i.text=="string"){o.push(i.text);continue}throw i&&(i.inlineData!==void 0||i.fileData!==void 0)?new k(`${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 k(`${e}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new k(`${e}: unsupported part ${JSON.stringify(s)}`)}return o.join(`
6
- `)}var da={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},Pe="urun.videoOut";function lo(t){return{framing:kr,codec:t}}function co(t){let{seq:e,ptsUs:n,codec:r,key:o}=t.header;return{[Pe]:{v:1,seq:e,ptsUs:n,codec:r,key:o,data:Buffer.from(t.au).toString("base64")}}}var la={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},ca=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",Pe],oo=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function pa(t){let e=q(t);if(!e)throw new k("setup.realtimeInputConfig must be an object");for(let o of Object.keys(e))if(!(o==="automaticActivityDetection"||o==="activityHandling"))throw new k(o==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${o}"`);let n=q(e.automaticActivityDetection);if(e.automaticActivityDetection!==void 0&&!n)throw new k("realtimeInputConfig.automaticActivityDetection must be an object");for(let o of Object.keys(n??{}))if(o!=="disabled")throw new k(`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 k("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(!oo.includes(e.activityHandling))throw new k(`unsupported activityHandling ${JSON.stringify(e.activityHandling)} \u2014 expected one of ${oo.join(", ")}`);r=e.activityHandling}return{manualActivity:!0,activityHandling:r}}var fa=["temperature","maxOutputTokens","responseModalities"];function po(t){for(let[p,m]of Object.entries(la))if(t[p]!==void 0)throw new k(m);for(let p of Object.keys(t))if(!ca.includes(p))throw new k(`unsupported setup field "${p}"`);if(typeof t.model!="string"||t.model.length===0)throw new k('setup.model is required (format "models/{model}")');let e=t.model.replace(/^models\//,""),n,r,o=!1,s=q(t.generationConfig);if(t.generationConfig!==void 0&&!s)throw new k("setup.generationConfig must be an object");if(s){for(let p of Object.keys(s))if(!fa.includes(p))throw new k(`unsupported generationConfig field "${p}"`);if(s.temperature!==void 0){if(typeof s.temperature!="number")throw new k("generationConfig.temperature must be a number");n=s.temperature}if(s.maxOutputTokens!==void 0){if(typeof s.maxOutputTokens!="number")throw new k("generationConfig.maxOutputTokens must be a number");r=s.maxOutputTokens}if(s.responseModalities!==void 0){let p=Array.isArray(s.responseModalities)?s.responseModalities:[s.responseModalities];for(let m of p){let c=String(m).toUpperCase();if(c==="AUDIO"){o=!0;continue}if(c!=="TEXT")throw new k(`responseModalities ${JSON.stringify(m)} 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:uo(t.systemInstruction,"setup.systemInstruction")});let a;if(t.tools!==void 0){if(!Array.isArray(t.tools))throw new k("setup.tools must be an array");a=[];for(let p of t.tools){let m=q(p);if(!m)throw new k("setup.tools entries must be objects");for(let c of Object.keys(m))if(c!=="functionDeclarations")throw new k(`unsupported tool "${c}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(m.functionDeclarations))throw new k("tool.functionDeclarations must be an array");for(let c of m.functionDeclarations){let f=q(c);if(!f||typeof f.name!="string")throw new k("functionDeclarations entries require a string name");a.push({type:"function",name:f.name,description:f.description,parameters:f.parameters})}}a.length===0&&(a=void 0)}let u={enabled:!1,handle:null};if(t.sessionResumption!==void 0){let p=q(t.sessionResumption);if(!p)throw new k("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let m of Object.keys(p))if(m!=="handle")throw new k(`unsupported sessionResumption field "${m}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(p.handle!==void 0&&(typeof p.handle!="string"||p.handle.length===0))throw new k("sessionResumption.handle must be a non-empty string when present");u={enabled:!0,handle:typeof p.handle=="string"?p.handle:null}}let d=t.realtimeInputConfig===void 0?da:pa(t.realtimeInputConfig),l=!1;if(t[Pe]!==void 0){if(!q(t[Pe]))throw new k(`setup["${Pe}"] must be an object (the v1 opt-in is {})`);l=!0}return{model:e,systemItems:i,tools:a,temperature:n,maxOutputTokens:r,audioModality:o,activity:d,sessionResumption:u,videoOut:l}}function fo(t){return{sessionResumptionUpdate:{newHandle:t,resumable:!0}}}function mo(t){let e=Math.max(0,t??0)/1e3;return{goAway:{timeLeft:`${Math.round(e*1e3)/1e3}s`}}}function go(t){for(let r of Object.keys(t))if(r!=="turns"&&r!=="turnComplete")throw new k(`unsupported clientContent field "${r}"`);let e=t.turns===void 0?[]:t.turns;if(!Array.isArray(e))throw new k("clientContent.turns must be an array");let n=[];for(let r of e){let o=q(r);if(!o)throw new k("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 k(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(o.role)})`);n.push({role:s==="model"?"assistant":"user",content:uo(o,"clientContent.turns")})}return{items:n,turnComplete:t.turnComplete===!0}}var Lt=16e3,ho=24e3,ma=`audio/pcm;rate=${ho}`,yo=Buffer.alloc(960).toString("base64");function ga(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 a=0;a<r;a++){let u=a*s,d=Math.floor(u);if(d>=i){o[a]=t[i];continue}let l=u-d;o[a]=Math.round(t[d]+(t[d+1]-t[d])*l)}return o}function ha(t){let e=q(t);if(!e)throw new k("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(e.mimeType))throw new k(`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 k("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 k("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(n)}function ya(t){let e=q(t);if(!e)throw new k("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(e.mimeType))throw new k(`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])!==Lt)throw new k(`realtimeInput.audio.mimeType declares rate=${n[1]} \u2014 the Live spec input format is ${Lt} Hz (audio/pcm;rate=${Lt})`);if(typeof e.data!="string"||e.data.length===0)throw new k("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 k(`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=ga(o,Lt,ho);return Buffer.from(s.buffer,s.byteOffset,s.byteLength).toString("base64")}function wo(t){if(t.mediaChunks!==void 0)throw new k('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 k("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let o=t.activityStart!==void 0?"activityStart":"activityEnd",s=q(t[o]);if(!s||Object.keys(s).length!==0)throw new k(`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 k(`unsupported realtimeInput field "${r}"`);let n=e.filter(r=>t[r]!==void 0);if(n.length!==1)throw new k(`realtimeInput must carry exactly one of ${e.join(", ")} (got ${n.length})`);if(t.audio!==void 0)return{kind:"audio",base64Pcm24k:ya(t.audio)};if(t.video!==void 0)return{kind:"video",jpegFrame:ha(t.video)};if(t.audioStreamEnd!==void 0){if(t.audioStreamEnd!==!0)throw new k("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof t.text!="string"||t.text.length===0)throw new k("realtimeInput.text must be a non-empty string");return{kind:"text",text:t.text}}function _o(t){for(let e of Object.keys(t))if(e!=="functionResponses")throw new k(`unsupported toolResponse field "${e}"`);if(!Array.isArray(t.functionResponses)||t.functionResponses.length===0)throw new k("toolResponse.functionResponses must be a non-empty array");return t.functionResponses.map(e=>{let n=q(e);if(!n||typeof n.id!="string"||n.id.length===0)throw new k("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 vo(t){return t.type!=="response.output_text.delta"||typeof t.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:t.delta}]}}}}function bo(t){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:ma,data:t}}]}}}}function ko(t){let e=q(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=q(d);if(!(l?.type!=="message"||!Array.isArray(l.content)))for(let p of l.content){let m=q(p);m?.type==="output_text"&&typeof m.text=="string"&&(s+=m.text)}}s&&o.push({role:"assistant",content:s});for(let d of n){let l=q(d);if(l?.type!=="function_call")continue;if(typeof l.call_id!="string"||typeof l.name!="string")throw new k(`upstream function_call item is missing call_id/name: ${JSON.stringify(d)}`,we);let p=typeof l.arguments=="string"&&l.arguments.length>0?l.arguments:"{}",m;try{m=JSON.parse(p)}catch{throw new k(`upstream function_call arguments are not valid JSON: ${p}`,we)}r.push({id:l.call_id,name:l.name,args:m}),o.push({type:"function_call",call_id:l.call_id,name:l.name,arguments:p})}let i=[];r.length>0&&i.push({toolCall:{functionCalls:r}}),i.push({serverContent:{generationComplete:!0}});let a=q(e.usage),u={serverContent:{turnComplete:!0}};if(a){let d={};typeof a.input_tokens=="number"&&(d.promptTokenCount=a.input_tokens),typeof a.output_tokens=="number"&&(d.responseTokenCount=a.output_tokens),typeof a.total_tokens=="number"&&(d.totalTokenCount=a.total_tokens),Object.keys(d).length>0&&(u.usageMetadata=d)}return i.push(u),{messages:i,assistantItems:o}}var wa=256,xn=class{snapshots=new Map;store(e,n,r){for(this.snapshots.set(`${e}\0${n}`,r);this.snapshots.size>wa;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(e,n){return this.snapshots.get(`${e}\0${n}`)}},_a="local",rt=t=>t.slice(0,120),En=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 U){this.ws.close(io,rt(e.message));return}if(e instanceof X){this.ws.close(Nt,rt(e.message));return}if(e instanceof k){this.ws.close(e.closeCode,rt(e.message));return}this.ws.close(we,rt(String(e instanceof Error?e.message:e)))}onMessage(e){try{let n=ao(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 k(`"${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}=go(n.clientContent);this.transcript.push(...o),s&&this.enqueue(()=>this.runTurn());return}if(n.kind==="realtimeInput"){let o=wo(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:yo);return}this.transcript.push(..._o(n.toolResponse)),this.enqueue(()=>this.runTurn())}catch(n){this.fail(n)}}async handleSetup(e){if(this.config)throw new k("setup may only be sent once, as the first client message");let n=po(e),r=n.sessionResumption.handle;if(r!==null){let s=this.registry.get(this.scope,r);if(!s)throw new k("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",Nt);if(s.model!==n.model)throw new k(`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 k("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 k(`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`,Nt);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),a=i.onOutputFrame(d=>this.send(co(d))),u=i.onLaneError(d=>this.fail(d));this.detachVideoOut=()=>{a(),u()},o[Pe]=lo(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 k("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openAudio;if(!n)throw new k("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native AudioBridge seam over the canonical `audio` lane)",we);return this.audio=(async()=>{let r=await n.call(this.clients,e.model);return this.detachAudio=r.onOutputAudio(o=>this.send(bo(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 k("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openVideo;if(!n)throw new k("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",we);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 k(`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 k("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 k("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,So.randomUUID)();this.registry.store(this.scope,r,{model:e.model,proxyHandle:n,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(r),this.send(fo(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(mo(e.timeLeftMs)),this.ws.close(we,rt(`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 u of r){if(n.interrupted)break;let d=u;if(d.type==="error")throw new k(`upstream error: ${JSON.stringify(d)}`,we);if(d.type==="response.completed"){o=d.response,s=!0;continue}let l=vo(d);l&&this.send(l)}if(n.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!s)throw new k("upstream produced no response.completed event",we);let{messages:i,assistantItems:a}=ko(o);this.transcript.push(...a);for(let u of i)this.send(u);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},Ce=class extends Error{constructor(n,r){super(r);this.status=n;this.name="UpgradeAuthError"}status};function va(t,e,n){let r=new Set;(u=>{if(typeof u=="string")u!==""&&r.add(u);else if(Array.isArray(u))for(let d of u)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 Ce(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 Ce(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 a;for(let u of r)a=u;if(a===void 0&&n)throw new Ce(401,"missing credentials: send the org API key in the x-goog-api-key header or as Authorization: Bearer");return a??""}function xo(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 Ro.WebSocketServer({noServer:!0}),o=new xn;t.on("upgrade",(i,a,u)=>{let d=new URL(i.url??"/","http://localhost");d.pathname===so&&(a.on("error",()=>{}),(async()=>{let l=va(i,d,n),p;if(e.authenticate)p=await e.authenticate(l,i);else{if(e.apiKey!==void 0&&l!==e.apiKey)throw new Ce(401,"invalid local proxy api key");p={clients:e.clients,scope:_a}}r.handleUpgrade(i,a,u,m=>{new En(m,p,o)})})().catch(l=>{let p=502;(l instanceof Ce||typeof l=="object"&&l!==null&&"status"in l&&typeof l.status=="number")&&(p=l.status);let m=p===401?"Unauthorized":p===403?"Forbidden":p===502?"Bad Gateway":"Internal Server Error",c=l instanceof Error?l.message:String(l);a.destroyed||(a.write(`HTTP/1.1 ${p} ${m}\r
2
+ "use strict";var _i=Object.create;var pr=Object.defineProperty;var vi=Object.getOwnPropertyDescriptor;var bi=Object.getOwnPropertyNames;var ki=Object.getPrototypeOf,Si=Object.prototype.hasOwnProperty;var xi=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of bi(e))!Si.call(t,o)&&o!==n&&pr(t,o,{get:()=>e[o],enumerable:!(r=vi(e,o))||r.enumerable});return t};var Ie=(t,e,n)=>(n=t!=null?_i(ki(t)):{},xi(e||!t||!t.__esModule?pr(n,"default",{value:t,enumerable:!0}):n,t));var Ri=()=>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=Ri();var ur=require("child_process"),ce=require("fs"),dr=Ie(require("path"),1),L=Ie(require("process"),1);var Te=require("@urun-sh/core");function fr(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 mr(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 gr(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 Ei="llm-resp";async function*fn(t,e){let n=`${Ei}:${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 a of i.tool_calls){let u=typeof a.index=="number"?a.index:0,d=o.get(u)??{};a.id&&(d.id=a.id),a.function?.name&&(d.name=a.function.name),o.set(u,d),yield{type:"response.function_call_arguments.delta",item_id:`fc_${e}_${u}`,tool_index:u,call_id:d.id,name:d.name,delta:a.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 gr(i);return}}}finally{try{t.doc("llm").set({requests:{[e]:null}})}catch{}}}var St=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=mr(e,n,this.consumerId);return this.write(r),fn(this.session,n)})}sendResponseCreate(e,n,r){return this.writeGated(r,()=>{let o=fr(e,n,r,this.consumerId);return this.write(o),fn(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 hr=0;function Ai(){return hr+=1,`req_${Date.now().toString(36)}_${hr.toString(36)}`}var xt=class{transport;constructor(e){this.transport=new St(e)}responses={create:async e=>{let n=Ai(),r=await this.transport.sendResponses(e,n);return Object.assign((async function*(){yield*r})(),{requestId:n})}}};var _r="audio",mn=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 vr(t,e,n=1e4){let r=new mn(e,{sampleRate:24e3}),o=await r.startOutbound();await t.stream(_r).attach(o);let s=await Ii(t,n);return await r.startInbound(s),r}function Ii(t,e){let n=t.stream(_r);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),a=u=>{u!=null&&(clearTimeout(i),s?.(),r(u))};s=n.on?.("track",a),n.track!=null&&a(n.track)})}var yr="rt-video-in",wr=256e3;function br(t){let e=t.stream(yr),n=e.emit;if(typeof n!="function")throw new Error(`enableSessionVideo: session.stream('${yr}') 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>wr)throw new Error(`enableSessionVideo: frame is ${r.byteLength} bytes \u2014 over the ${wr}-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 Oi=24e3,Ze=480,Ti=111;async function Pi(){let t=await import("opusscript"),e=t.default??t;return new e(Oi,1,2048)}async function Rt(){let t=await import("werift"),e=await Pi();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:a=>{let u=new Int16Array(i.length+a.length);u.set(i,0),u.set(a,i.length);let d=0;for(;u.length-d>=Ze;){let l=u.subarray(d,d+Ze);d+=Ze;let p=Buffer.from(l.buffer,l.byteOffset,l.byteLength),m=e.encode(p,Ze);o=o+1&65535,s=s+Ze>>>0;let c=new t.RtpHeader({version:2,payloadType:Ti,sequenceNumber:o,timestamp:s,ssrc:r,marker:!1}),f=new t.RtpPacket(c,m);n.writeRtp(f)}i=u.slice(d)}}},createSink:n=>{let r={onframe:null};return n.onReceiveRtp.subscribe(s=>{let i;try{i=e.decode(s.payload)}catch{return}let a=new Int16Array(i.length/2);for(let u=0;u<a.length;u++)a[u]=i.readInt16LE(u*2);r.onframe?.({samples:a})}),r}}}async function kr(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 Et=require("@urun-sh/core"),gn="rt-video-out";var Ci="h264";function $i(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 Mi(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;$i(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 Sr="urun.videoOut.v2-ws";function xr(t){let e=t.stream(gn);if(typeof e.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${gn}') 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=u=>{for(let d of r)d(u)},i=!1,a=async()=>{for await(let u of e.messages()){if(!(u instanceof Uint8Array)){s(new Error(`openSessionVideoOutLane: "${gn}" delivered a non-BINARY \xA75 payload (${typeof u}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let d;try{d=Mi(u)}catch(l){s(l instanceof Error?l:new Error(String(l)));continue}for(let l of n)l(d)}for(let u of o)u()};return{codec:Ci,onOutputFrame:u=>(n.add(u),i||(i=!0,a().catch(d=>s(d instanceof Error?d:new Error(String(d))))),()=>n.delete(u)),onLaneError:u=>(r.add(u),()=>r.delete(u)),onEnd:u=>(o.add(u),()=>o.delete(u))}}var Rr={delta:!0,final:!0,turn_start:!0,turn_end:!0,turn_eager_end:!0,turn_resumed:!0},je=class extends Error{constructor(e){super(e),this.name="TurnLaneDecodeError"}};function Er(t){let e=t.t;if(typeof e!="string"||!(e in Rr))throw new je(`the "stt" transcript lane delivered an unknown turn kind ${JSON.stringify(e??null)} \u2014 known kinds: ${Object.keys(Rr).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 Li(t){if(typeof t=="string"){let e;try{e=JSON.parse(t)}catch(n){throw new je(`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 je(`the "stt" transcript lane delivered a JSON string that is not an object (${JSON.stringify(e)})`);return Er(e)}if(t===null||typeof t!="object"||Array.isArray(t))throw new je(`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 Er(t)}function Ar(t){return t.reason instanceof Error?t.reason:new Error("the turn-lane consumer aborted")}async function Ui(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 Ir(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 Ar(e);let s=o.next();if(e!==void 0&&await Ui(s,e)==="aborted")throw s.catch(()=>{}),Ar(e);let i=await s;if(i.done)return;yield Li(i.value)}}finally{let s=o.return?.();s&&s.catch(()=>{})}}return r()}var Or=require("@urun-sh/core"),hn=2;function Ni(t){let e=t?.type;return e==="response.created"||e==="response.in_progress"}function Di(t){let e=t?.type;return e==="response.completed"||e==="error"}function Tr(t){return async function(n,r){return(async function*(){for(let s=1;s<=hn;s++){let{app:i,entry:a,identity:u}=await t.sessionFor(n.model);r?.({sessionId:t.sessionIdOf?.(a)??null,identity:u});let d=!1,l=!1;try{let p=await t.createStream(a,n);for await(let m of p){if(Di(m)&&(d=!0),Ni(m)){if(s>1)continue}else l=!0;yield m}}catch(p){if(p instanceof Or.SessionFailedError&&(p.kind==="never-live"||p.kind==="create-failed")){try{t.onAdmissionVerdict?.(i,a,p)}catch{}throw p}if(s<hn&&!l){await t.evict(i,a);continue}throw p}if(d)return;if(s<hn&&!l){await t.evict(i,a);continue}return}})()}}var se=class extends Error{name="ModelPriceError"},yn="per_million_input_tokens",wn="per_million_output_tokens",Cr="model_id,variant,unit,price_usd,effective_at,expires_at",Pr={per_request:!0,per_minute:!0,per_output_unit:!0},Fi=5e3;function Bi(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 ji(t,e,n){return t===null?null:$r(t,e,n)}function qi(t,e){if(t===yn)return"input";if(t===wn)return"output";if(typeof t=="string"&&Object.hasOwn(Pr,t))return null;throw new se(`${e}: unit ${JSON.stringify(t)} is neither a per-token rate (${yn} | ${wn}) nor a known GPU-time unit (${Object.keys(Pr).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 Hi(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 _n(t,e){return`${t}\0${e}`}function Gi(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 Cr.split(","))if(!Object.hasOwn(s,d))throw new se(`${o} has no "${d}" column: ${JSON.stringify(s)}`);let i=qi(s.unit,o);if(i===null)continue;let a=s.model_id;if(typeof a!="string"||a==="")throw new se(`${o} has no model_id (${JSON.stringify(s)})`);let u=s.variant;if(!(u===null||typeof u=="string"&&u!==""))throw new se(`${o} has a non-string variant (${JSON.stringify(u)})`);e.push({model_id:a,variant:u,side:i,usd:Bi(s.price_usd,o),effective_at:$r(s.effective_at,o,"effective_at"),expires_at:ji(s.expires_at,o,"expires_at")})}return e}function Mr(t,e=Date.now(),n){let r=new Map,o=new Map;for(let u of t){if(u.effective_at>e||u.expires_at!==null&&u.expires_at<=e)continue;let d=r.get(u.model_id);d||(d=new Map,r.set(u.model_id,d),o.set(u.model_id,new Map));let l=u.variant??"",p=d.get(l);p||(p={input:[],output:[]},d.set(l,p),o.get(u.model_id).set(l,u.variant)),p[u.side].push({effective_at:u.effective_at,usd:u.usd})}let s=[],i=[];for(let[u,d]of r){let l=d.get(""),p=(m,c,f)=>{let g=m&&m.length>0?m:l?.[c];if(!g||g.length===0)return null;let b=Hi(g,c);return"reason"in b?b:b.rate.usd===0?c==="output"&&f!==null&&n?.has(_n(u,f))?b.rate:{reason:`the ${c} 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`}:b.rate};for(let[m,c]of o.get(u)){let f=d.get(m),g=p(f?.input,"input",c),b=p(f?.output,"output",c);if(g!==null&&"reason"in g){i.push({model_id:u,variant:c,reason:g.reason});continue}if(b!==null&&"reason"in b){i.push({model_id:u,variant:c,reason:b.reason});continue}g===null||b===null||s.push({model_id:u,variant:c,pricing:{input:g.usd,output:b.usd}})}}let a=(u,d)=>u.model_id.localeCompare(d.model_id)||(u.variant===null?1:0)-(d.variant===null?1:0)||(u.variant??"").localeCompare(d.variant??"");return{prices:s.sort(a),refusals:i.sort(a)}}function et(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 tt={prices:[],refusals:[]};async function Lr(t){let e=t.fetchImpl??fetch,n=`${yn},${wn}`,r=await e(`${t.catalogUrl}/model_prices?select=${Cr}&unit=in.(${n})`,{headers:{apikey:t.anonKey,Authorization:`Bearer ${t.anonKey}`},signal:AbortSignal.timeout(Fi)});if(!r.ok)throw new Error(`model_prices fetch failed: ${r.status}`);return Gi(await r.json())}var W=class extends Error{},Vi=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;function Ji(t){if(typeof t!="string")return!1;let e=Vi.exec(t);if(!e)return!1;let[,n,r,o,s,i,a]=e.map(Number);if(!Number.isFinite(Date.parse(t)))return!1;let u=new Date(Date.UTC(n,r-1,o,s,i,a));return u.getUTCFullYear()===n&&u.getUTCMonth()===r-1&&u.getUTCDate()===o&&u.getUTCHours()===s&&u.getUTCMinutes()===i&&u.getUTCSeconds()===a}function Wi(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((a,u)=>{if(a===null||typeof a!="object")throw new W(`catalog shared endpoints[${u}] is not an object: ${JSON.stringify(a)}`);let d=a;for(let p of["model_id","variant","app_slug","function"])if(typeof d[p]!="string"||d[p].length===0)throw new W(`catalog shared endpoints[${u}] (${JSON.stringify(d)}) has no valid "${p}"`);let l=(p,m)=>{if(d[p]!==void 0&&d[p]!==null&&!m)throw new W(`catalog shared endpoints[${u}] (${d.model_id}:${d.variant}) carries a malformed "${p}": ${JSON.stringify(d[p])}`)};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",Ji(d.ready_since));for(let p of["model_id","variant"]){let m=d[p];if(m.includes(":"))throw new W(`catalog shared endpoints[${u}] has "${p}" containing ":" (${JSON.stringify(m)}) \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(a=>typeof a=="string"&&a.length>0)))throw new W(`catalog "shared" block regions is malformed: ${JSON.stringify(o)}`);if(s!=null){let a=s;if(typeof s!="object"||typeof a.zdr!="boolean"||typeof a.hipaa!="boolean")throw new W(`catalog "shared" block compliance is malformed (operator-declared zdr + hipaa booleans required): ${JSON.stringify(s)}`);for(let u of Object.keys(a))if(u!=="zdr"&&u!=="hipaa")throw new W(`catalog "shared" block compliance carries "${u}" 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 qe(t){return[...t.endpoints].sort((e,n)=>e.model_id.localeCompare(n.model_id)||e.variant.localeCompare(n.variant))}function Ur(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 a=t.endpoints.filter(d=>d.model_id===o);if(a.length===0)return null;if(s!==void 0){let d=a.find(l=>l.variant===s);return d?{shared_org_id:t.shared_org_id,endpoint:d}:null}if(a.length===1)return{shared_org_id:t.shared_org_id,endpoint:a[0]};let u=a.filter(d=>d.compat_default===!0);if(u.length!==1)throw new W(`shared model "${o}" has ${u.length} compat_default rows (exactly one required) among variants ${a.map(d=>d.variant).join(", ")}`);return{shared_org_id:t.shared_org_id,endpoint:u[0]}}async function Nr(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 Wi(await r.json())}function ye(t){return t.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}var nt="/partners/openrouter/models",Ki={chat:!0,code:!0,agent:!0,vl:!0},Dr={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function zi(t){let e=t.toLowerCase();return Object.hasOwn(Dr,e)?Dr[e]:null}var Fr={"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 Yi(t,e){if(!t||t.length===0)return[];let n=[],r=[];for(let o of t){let s=Object.hasOwn(Fr,o)?Fr[o]:null;if(s===null){r.push(o);continue}n.some(a=>a.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 Br={qwen:"Qwen",qwen1:"Qwen",qwen2:"Qwen",qwen3:"Qwen",gemma:"Gemma",diffusiongemma:"Gemma",gpt:"GPT",glm:"GLM",deepseek:"DeepSeek",mistral:"Mistral"},jr={2:"Llama2",3:"Llama3"};function Xi(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(Br,s))return Br[s];let i=r[o+1];if(s==="llama"&&i!==void 0&&Object.hasOwn(jr,i))return jr[i];if(s==="phi"&&i!==void 0&&/^\d/.test(i))return"Phi"}return null}var Qi=["context_length","max_model_len"];function Zi(t){let e=Qi.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 It(t){let e=t.map(Zi),n=e[0];return n==null?null:e.every(r=>r===n)?n:null}function ea(t){let e=t.engine_args?.reasoning_parser;return e==null||e===""?!1:typeof e=="string"?!0:null}function vn(t){if(t.length===0)return null;let e=t.map(ea),n=e[0];return n==null?null:e.every(r=>r===n)?n:null}function ta(t,e){let n=e.filter(o=>ye(`${o.model_id}-${o.variant}`)===t);if(n.length>0)return At(n);let r=e.filter(o=>ye(o.model_id)===t);return r.length>0?At(r):null}function At(t){let e=t[0];return t.every(r=>r.model_id===e.model_id&&r.variant===e.variant)?{row:t.length===1?e:na(t),matches:t}:null}function na(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 Gr(t){return 0}function Oe(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 Ot(t,e,n){let r=ta(t,e);if(!r)return Oe({id:t,object:"model",created:0,owned_by:"urun"});let{row:o,matches:s}=r,i=It(s),a=et(o.model_id,o.variant,n),u=Vr(o,s),d=vn(s);return Oe({id:t,object:"model",created:Gr(o),owned_by:"urun",...i!==null?{context_length:i}:{},...a!==null?{pricing:a}:{},...u.published.length>0?{voices:[...u.published]}:{},...d!==null?{capabilities:{reasoning:d}}:{}})}var ra=["Vivian","Serena","Uncle_Fu","Dylan","Eric","Ryan","Aiden","Ono_Anna","Sohee"];function Vr(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,a)=>i===r[a])))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:[...ra],defect:o}:{published:[],defect:o}}function bn(t){let e=t.length>0?At(t):null;return e===null?{published:[],defect:null}:Vr(e.row,e.matches)}function qr(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 Hr(t){return{input:[{type:"prompt",unit:"token",cost_usd:qr(t.input)}],output:[{type:"completion",unit:"token",cost_usd:qr(t.output)}]}}function oa(t,e,n,r){if(e.length===0)return null;let o=At(e);if(!o)return null;let{row:s,matches:i}=o;if(!s.task||Ki[s.task]!==!0)return null;let a=It(i),u=et(s.model_id,s.variant,n),d=Xi(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 a!==null&&(l.max_tokens={type:"integer",min:1,max:a,unit:"token"}),{schema_version:"2.4",id:`${s.model_id}:${s.variant}`,name:`${s.model_id}:${s.variant}`,created:Gr(s),quantization:zi(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&&u!==null,is_free:!1,input_modalities:[{type:"text",...a!==null?{supported_inputs:{max_context_length:{value:a,unit:"token"}}}:{},...u?{pricing:Hr(u).input}:{}},...s.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:l,...u?{pricing:Hr(u).output}:{}}],...r.datacenters.length>0?{datacenters:r.datacenters}:{},...r.compliance?{compliance:r.compliance}:{}}}function Jr(t,e,n,r){let o=r??(d=>console.warn(d.trimEnd())),s=t?qe(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(p=>`${p.model_id}:${p.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:Yi(t?.regions,o),compliance:t?.compliance??null},a=[],u=new Set;for(let d of s){let l=`${d.model_id}:${d.variant}`;if(u.has(l))continue;u.add(l);let p=oa(d,e.filter(m=>m.model_id===d.model_id&&m.variant===d.variant),n,i);p&&a.push(p)}return{data:a}}var Xr="https://api.urun.sh/v1",U=class extends Error{},X=class extends Error{},Sn="urs1.";function Wr(t,e){return Sn+Buffer.from(JSON.stringify([t,e]),"utf8").toString("base64url")}function sa(t){if(!t.startsWith(Sn))return null;try{let e=JSON.parse(Buffer.from(t.slice(Sn.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 Qr(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 Kr(t){if(t.length===0)return[];if(!t.every(n=>(n.task??void 0)==="image"))return[];let e=aa(t);return e===null?[]:[e]}function kn(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 zr(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 Zr="shared:",Pt="speech:";function rt(t){return t.sharedApp?Zr+t.appSlug:t.appSlug}function ot(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 ke(t,e){let n=e.filter(s=>ye(`${s.model_id}-${s.variant}`)===t);if(n.length>0)return n;let r=e.filter(s=>ye(s.model_id)===t);return new Set(r.map(s=>s.variant)).size===1?r:[]}function ia(t){let e=t.engine_args?.expects_image;return e==null?"edit":e===!1?"generation":e===!0?"edit":null}function aa(t){let e=t.map(ia),n=e[0];return n!=null&&e.every(r=>r===n)?n:null}var Yr={stt:"stt",tts:"tts"};function Se(t){if(t.length===0)return null;let e=t[0].task??void 0;return e===void 0||Yr[e]===void 0?null:t.every(n=>(n.task??void 0)===e)?Yr[e]:null}function ua(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 Tt=6e4,Ct=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??Tt;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??Tt;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??Tt;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??Tt;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
+ `),tt}let r=Mr(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
+ `);return r}outputFreeKeys(e){let n=new Set;if(!e)return n;for(let r of e.endpoints)r.output_free===!0&&n.add(_n(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(Zr)||o.startsWith(Pt)||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 U(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=ye(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 a=i.filter(d=>d.startsWith(`${o}-`));if(a.length===1)return{appSlug:a[0]};if(a.length>1)throw new U(`model "${n}" is ambiguous across deployed apps ${a.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let u=s.find(d=>d.app_slug===o||d.app_slug.startsWith(`${o}-`));if(u)throw new U(`model "${n}" maps to app "${u.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=Ur(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(a=>n===a.model_id||n===`${a.model_id}:${a.variant}`||o===ye(a.model_id)||o===ye(`${a.model_id}-${a.variant}`));if(i)throw new U(`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=ot(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 targetFor(e){return this.resolveTarget(e)}async resolveApp(e){return rt(await this.resolveTarget(e))}async sessionFor(e){let n=await this.resolveTarget(e);return this.acquire(n,rt(n))}async speechSessionFor(e){let n=await this.resolveTarget(e);return this.acquire(n,Pt+rt(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:ot(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:Wr(n,this.keyOf(r))}}async speechHandleFor(e){let{app:n,entry:r}=await this.speechSessionFor(e);return{app:n,handle:Wr(n,this.keyOf(r))}}async sessionForHandle(e){let n=sa(e);if(!n)throw new X(`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(a=>a,()=>null):null;if(i===null)throw new X(`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 X(`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:tt;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 u=(n?qe(n):[]).map(d=>{let l=s.filter(b=>b.model_id===d.model_id&&b.variant===d.variant),p=It(l),m=vn(l),c=et(d.model_id,d.variant,i),f=bn(l);return Oe({id:`${d.model_id}:${d.variant}`,object:"model",created:0,owned_by:"urun",...p!==null?{context_length:p}:{},...c!==null?{pricing:c}:{},...f.published.length>0?{voices:[...f.published]}:{},...m!==null?{capabilities:{reasoning:m}}:{}})});return{object:"list",data:this.opts.sharedOnlyListing?u:[...e.map(d=>Ot(d,s,i)),...u]}}async openRouterModels(){let e=await this.sharedBlock(),[n,r]=await Promise.all([this.catalogRows(),this.pricesForListing(this.outputFreeKeys(e))]);return Jr(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=kn(String(e),r.modelId,r.variant,o,this.opts.listCatalog!==null);return zr(`${r.modelId}:${r.variant}`,s).includes(n)}return Kr(ke(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(ke(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(ke(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(ke(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):ke(n.appSlug,r);return ua(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):ke(n.appSlug,r);return bn(o)}async imageModelList(){let e=await this.catalogRows(),n=tt,r=[];for(let s of await this.availableIds()){let i=Kr(ke(s,e));i.length!==0&&r.push(Oe({...Ot(s,e,n),image_modes:i}))}let o=await this.sharedBlock();if(o)for(let s of qe(o)){let i=kn(`${s.model_id}:${s.variant}`,s.model_id,s.variant,e,this.opts.listCatalog!==null),a=zr(`${s.model_id}:${s.variant}`,i);if(a.length===0)continue;let u=Oe({id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun",image_modes:a});r.push(u)}return{object:"list",data:r}}async speechToTextModelList(){let e=await this.catalogRows(),n=tt,r=[];for(let s of await this.availableIds())Se(ke(s,e))==="stt"&&r.push(Ot(s,e,n));let o=await this.sharedBlock();if(o)for(let s of qe(o)){let i=kn(`${s.model_id}:${s.variant}`,s.model_id,s.variant,e,this.opts.listCatalog!==null);if(Se(i)!=="stt")continue;let a=Oe({id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun"});r.push(a)}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 da=3,la="llm",ca=new Set(["paused","error"]);function eo(t,e){let n=t.doc(la),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,a=new Set,u=[],d=l=>{if(!s){s=l;for(let p of[...a])p(l);a.clear()}};return r&&u.push(n.onConnectionState(l=>{i&&(l.consecutiveFailures<da||d(new X(`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&&u.push(t.onPhase(l=>{if(l.name==="live"){i=!0;return}i&&ca.has(l.name)&&d(new X(`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),()=>{}):(a.add(l),()=>a.delete(l))},dispose(){a.clear();for(let l of u.splice(0))l()}}}async function*xn(t,e){let n=e.gone();if(n)throw n;let r=t[Symbol.asyncIterator](),o,s=new Promise((i,a)=>{o=e.onGone(u=>a(u))});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)}}function to(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 pa(t,e,n){let{baseUrl:r,orgId:o,auth:s}=t,i=typeof t.subject=="function"?t.subject():t.subject,a=n.startsWith(Pt)&&i!==void 0?`${i}:speech`:i,u=e.fnName??t.fnName,d=t.sessionStartOptions?.(n),l=e.sharedApp?{sharedApp:!0,...d}:d,p=g=>({session:g,responses:new xt(g),gone:eo(g,n)});if(s.lane==="jwt"){let g=(0,Te.App)(e.appSlug,{baseUrl:r,orgId:o,jwt:s.jwt});return p(to(g,e.appSlug,u,l))}let{apiKey:m,gatewayUrl:c}=s,f=(0,Te.App)(e.appSlug,{baseUrl:r,orgId:o,getAccessToken:async()=>(await(0,Te.createClientToken)(m,{baseUrl:c,expiresIn:300,allowedFunctions:[`${e.appSlug}/${u}`],...a===void 0?{}:{subject:a},...e.sharedApp?{sharedApp:!0}:{}})).token});return p(to(f,e.appSlug,u,l))}function fa(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 ma(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 ga=new Set(["error","paused"]);function ro(t){let{auth:e,catalog:n}=t,r=null,o=(i,a)=>{let u=pa(t,i,a);return t.onSessionOpen&&typeof u.session.id=="string"&&u.session.id!==""&&t.onSessionOpen(a,u.session.id),u.gone.onGone(()=>{r?.evict(a,u)}),u},s=new Ct({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"?()=>Qr({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,listCatalog:n?()=>kr(n):null,listPrices:n?()=>Lr(n):null,shared:e.lane==="api-key"?()=>Nr({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,sessionKey:fa});return r=s,t.defaultApp!==null&&t.statelessDirect!==!0&&s.seed(t.defaultApp,o({appSlug:t.defaultApp},rt({appSlug:t.defaultApp}))),s}async function Rn(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:Te.DEFAULT_WHEN_LIVE_TIMEOUT_MS,signal:e})}async function no(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 Mt(t){return t.reason instanceof Error?t.reason:new Error("the client socket closed")}function oo(t,e=Rt,n,r){let o=new WeakMap,s=new WeakMap,i=new WeakMap,a=new WeakMap,u=new WeakMap,d=c=>{let f=c.session.id;return typeof f=="string"&&f.length>0?f:null},l=c=>{let f=o.get(c);if(!f){let g={waiting:0,gone:new AbortController};u.set(c,g),f=(async()=>{if(await Rn(c.session,g.gone.signal),g.gone.signal.aborted)throw Mt(g.gone.signal);u.get(c)===g&&u.delete(c);let b=await vr(c.session,await e());return Object.assign(b,{sessionId:c.session.id})})(),o.set(c,f),f.catch(()=>{o.get(c)===f&&o.delete(c)}),f.then(()=>{u.get(c)===g&&u.delete(c)},()=>{u.get(c)===g&&u.delete(c)})}return f},p=c=>{let f=a.get(c);return f||(f=(async()=>{let g=await l(c);return{appendInputAudio:b=>g.appendInputAudio(b),onOutputAudio:b=>g.onOutputAudio(b),sessionId:d(c)}})(),a.set(c,f),f.catch(()=>{a.get(c)===f&&a.delete(c)})),f},m=async(c,f,g)=>{if(g?.aborted){let h=u.get(c);throw(o.get(c)===void 0||h!==void 0&&h.waiting===0)&&await no(c.session),Mt(g)}let b=f(c),y=u.get(c);return y!==void 0&&y.waiting++,g===void 0?b:new Promise((h,v)=>{let A=()=>{y!==void 0&&--y.waiting===0&&u.get(c)===y&&(y.gone.abort(),u.delete(c),o.delete(c),a.delete(c),no(c.session).catch(()=>{})),v(Mt(g))};g.addEventListener("abort",A,{once:!0});let w=()=>g.removeEventListener("abort",A);b.then(_=>{w(),h(_)},_=>{w(),v(_)})})};return{createResponse:Tr({sessionFor:c=>t.sessionFor(c),sessionIdOf:c=>d(c),evict:(c,f)=>t.evict(c,f),onAdmissionVerdict:(c,f,g)=>{ma(g)&&(t.evict(c,f),n?.(c,g.kind==="never-live"?"never-live":"create-failed"))},createStream:async(c,f)=>(await Rn(c.session),xn(await c.responses.responses.create(f),c.gone))}),listModels:()=>t.modelList(),openRouterModels:()=>t.openRouterModels(),supportsImages:(c,f)=>t.supportsImages(c,f),listImageModels:()=>t.imageModelList(),catalogIdentity:c=>t.catalogIdentityFor(c),servingAppSlug:async c=>await t.servingAppSlugFor(c),supportsAudio:c=>t.supportsAudio(c),supportsSpeech:c=>t.supportsSpeech(c),speechVoices:c=>t.speechVoices(c),supportsTranscription:c=>t.supportsTranscription(c),transcriptStreamFor:c=>t.transcriptStreamFor(c),listSpeechToTextModels:()=>t.speechToTextModelList(),openAudio:async(c,f)=>{let{entry:g}=await t.sessionFor(c);return await m(g,l,f)},openAudioOn:async(c,f)=>{let{entry:g}=await t.sessionForHandle(c);return await m(g,p,f)},openTurnsOn:async function*(c,f){if(f?.aborted)throw Mt(f);let{entry:g}=await t.sessionForHandle(c);yield*Ir(g.session,f)},openVideo:async c=>{let{entry:f}=await t.sessionFor(c),g=s.get(f);return g||(g=br(f.session),s.set(f,g)),g},openVideoOut:async c=>{let{entry:f}=await t.sessionFor(c),g=i.get(f);return g||(g=xr(f.session),i.set(f,g)),g},sessionHandle:async c=>(await t.handleFor(c)).handle,speechSessionHandle:async c=>(await t.speechHandleFor(c)).handle,evictSpeechSession:async c=>{let{app:f,entry:g}=await t.sessionForHandle(c);await t.evict(f,g)},createResponseOn:async(c,f)=>{let{entry:g}=await t.sessionForHandle(c);return await Rn(g.session),xn(await g.responses.responses.create(f),g.gone)},onSessionEnd:async(c,f)=>{let{entry:g}=await t.sessionForHandle(c),b=g.session;if(typeof b.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let y=!1;return b.onPhase(h=>{if(y||!ga.has(h.name))return;y=!0;let v=b.endsAt??null;f({timeLeftMs:v?Math.max(0,v.getTime()-Date.now()):null,reason:h.reason??`session ${h.name}`})})},...r?{stateless:r}:{}}}var di=require("http");var xo=require("crypto"),Ro=require("ws");var io="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",ha=1007,ao=1008,we=1011,Dt=1008,k=class extends Error{constructor(n,r=ha){super(n);this.closeCode=r}closeCode},q=t=>typeof t=="object"&&t!==null&&!Array.isArray(t)?t:null,Lt=["setup","clientContent","realtimeInput","toolResponse"];function uo(t){let e;try{e=JSON.parse(t)}catch{throw new k("client message is not valid JSON")}let n=q(e);if(!n)throw new k("client message must be a JSON object");let r=Lt.filter(a=>n[a]!==void 0),o=Object.keys(n).filter(a=>!Lt.includes(a));if(o.length>0)throw new k(`unknown client message field(s) ${JSON.stringify(o)} \u2014 expected exactly one of ${Lt.join(", ")}`);if(r.length!==1)throw new k(`client message must contain exactly one of ${Lt.join(", ")} (got ${r.length})`);let s=r[0],i=q(n[s]);if(!i)throw new k(`"${s}" must be a JSON object`);return{kind:s,[s]:i}}function lo(t,e){if(typeof t=="string")return t;let n=q(t),r=n?n.parts:void 0;if(!Array.isArray(r))throw new k(`${e}: expected a Content with a parts array`);let o=[];for(let s of r){let i=q(s);if(i&&typeof i.text=="string"){o.push(i.text);continue}throw i&&(i.inlineData!==void 0||i.fileData!==void 0)?new k(`${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 k(`${e}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new k(`${e}: unsupported part ${JSON.stringify(s)}`)}return o.join(`
6
+ `)}var ya={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},Pe="urun.videoOut";function co(t){return{framing:Sr,codec:t}}function po(t){let{seq:e,ptsUs:n,codec:r,key:o}=t.header;return{[Pe]:{v:1,seq:e,ptsUs:n,codec:r,key:o,data:Buffer.from(t.au).toString("base64")}}}var wa={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},_a=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",Pe],so=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function va(t){let e=q(t);if(!e)throw new k("setup.realtimeInputConfig must be an object");for(let o of Object.keys(e))if(!(o==="automaticActivityDetection"||o==="activityHandling"))throw new k(o==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${o}"`);let n=q(e.automaticActivityDetection);if(e.automaticActivityDetection!==void 0&&!n)throw new k("realtimeInputConfig.automaticActivityDetection must be an object");for(let o of Object.keys(n??{}))if(o!=="disabled")throw new k(`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 k("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(!so.includes(e.activityHandling))throw new k(`unsupported activityHandling ${JSON.stringify(e.activityHandling)} \u2014 expected one of ${so.join(", ")}`);r=e.activityHandling}return{manualActivity:!0,activityHandling:r}}var ba=["temperature","maxOutputTokens","responseModalities"];function fo(t){for(let[p,m]of Object.entries(wa))if(t[p]!==void 0)throw new k(m);for(let p of Object.keys(t))if(!_a.includes(p))throw new k(`unsupported setup field "${p}"`);if(typeof t.model!="string"||t.model.length===0)throw new k('setup.model is required (format "models/{model}")');let e=t.model.replace(/^models\//,""),n,r,o=!1,s=q(t.generationConfig);if(t.generationConfig!==void 0&&!s)throw new k("setup.generationConfig must be an object");if(s){for(let p of Object.keys(s))if(!ba.includes(p))throw new k(`unsupported generationConfig field "${p}"`);if(s.temperature!==void 0){if(typeof s.temperature!="number")throw new k("generationConfig.temperature must be a number");n=s.temperature}if(s.maxOutputTokens!==void 0){if(typeof s.maxOutputTokens!="number")throw new k("generationConfig.maxOutputTokens must be a number");r=s.maxOutputTokens}if(s.responseModalities!==void 0){let p=Array.isArray(s.responseModalities)?s.responseModalities:[s.responseModalities];for(let m of p){let c=String(m).toUpperCase();if(c==="AUDIO"){o=!0;continue}if(c!=="TEXT")throw new k(`responseModalities ${JSON.stringify(m)} 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:lo(t.systemInstruction,"setup.systemInstruction")});let a;if(t.tools!==void 0){if(!Array.isArray(t.tools))throw new k("setup.tools must be an array");a=[];for(let p of t.tools){let m=q(p);if(!m)throw new k("setup.tools entries must be objects");for(let c of Object.keys(m))if(c!=="functionDeclarations")throw new k(`unsupported tool "${c}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(m.functionDeclarations))throw new k("tool.functionDeclarations must be an array");for(let c of m.functionDeclarations){let f=q(c);if(!f||typeof f.name!="string")throw new k("functionDeclarations entries require a string name");a.push({type:"function",name:f.name,description:f.description,parameters:f.parameters})}}a.length===0&&(a=void 0)}let u={enabled:!1,handle:null};if(t.sessionResumption!==void 0){let p=q(t.sessionResumption);if(!p)throw new k("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let m of Object.keys(p))if(m!=="handle")throw new k(`unsupported sessionResumption field "${m}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(p.handle!==void 0&&(typeof p.handle!="string"||p.handle.length===0))throw new k("sessionResumption.handle must be a non-empty string when present");u={enabled:!0,handle:typeof p.handle=="string"?p.handle:null}}let d=t.realtimeInputConfig===void 0?ya:va(t.realtimeInputConfig),l=!1;if(t[Pe]!==void 0){if(!q(t[Pe]))throw new k(`setup["${Pe}"] must be an object (the v1 opt-in is {})`);l=!0}return{model:e,systemItems:i,tools:a,temperature:n,maxOutputTokens:r,audioModality:o,activity:d,sessionResumption:u,videoOut:l}}function mo(t){return{sessionResumptionUpdate:{newHandle:t,resumable:!0}}}function go(t){let e=Math.max(0,t??0)/1e3;return{goAway:{timeLeft:`${Math.round(e*1e3)/1e3}s`}}}function ho(t){for(let r of Object.keys(t))if(r!=="turns"&&r!=="turnComplete")throw new k(`unsupported clientContent field "${r}"`);let e=t.turns===void 0?[]:t.turns;if(!Array.isArray(e))throw new k("clientContent.turns must be an array");let n=[];for(let r of e){let o=q(r);if(!o)throw new k("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 k(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(o.role)})`);n.push({role:s==="model"?"assistant":"user",content:lo(o,"clientContent.turns")})}return{items:n,turnComplete:t.turnComplete===!0}}var Ut=16e3,yo=24e3,ka=`audio/pcm;rate=${yo}`,wo=Buffer.alloc(960).toString("base64");function Sa(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 a=0;a<r;a++){let u=a*s,d=Math.floor(u);if(d>=i){o[a]=t[i];continue}let l=u-d;o[a]=Math.round(t[d]+(t[d+1]-t[d])*l)}return o}function xa(t){let e=q(t);if(!e)throw new k("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(e.mimeType))throw new k(`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 k("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 k("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(n)}function Ra(t){let e=q(t);if(!e)throw new k("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(e.mimeType))throw new k(`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])!==Ut)throw new k(`realtimeInput.audio.mimeType declares rate=${n[1]} \u2014 the Live spec input format is ${Ut} Hz (audio/pcm;rate=${Ut})`);if(typeof e.data!="string"||e.data.length===0)throw new k("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 k(`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=Sa(o,Ut,yo);return Buffer.from(s.buffer,s.byteOffset,s.byteLength).toString("base64")}function _o(t){if(t.mediaChunks!==void 0)throw new k('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 k("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let o=t.activityStart!==void 0?"activityStart":"activityEnd",s=q(t[o]);if(!s||Object.keys(s).length!==0)throw new k(`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 k(`unsupported realtimeInput field "${r}"`);let n=e.filter(r=>t[r]!==void 0);if(n.length!==1)throw new k(`realtimeInput must carry exactly one of ${e.join(", ")} (got ${n.length})`);if(t.audio!==void 0)return{kind:"audio",base64Pcm24k:Ra(t.audio)};if(t.video!==void 0)return{kind:"video",jpegFrame:xa(t.video)};if(t.audioStreamEnd!==void 0){if(t.audioStreamEnd!==!0)throw new k("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof t.text!="string"||t.text.length===0)throw new k("realtimeInput.text must be a non-empty string");return{kind:"text",text:t.text}}function vo(t){for(let e of Object.keys(t))if(e!=="functionResponses")throw new k(`unsupported toolResponse field "${e}"`);if(!Array.isArray(t.functionResponses)||t.functionResponses.length===0)throw new k("toolResponse.functionResponses must be a non-empty array");return t.functionResponses.map(e=>{let n=q(e);if(!n||typeof n.id!="string"||n.id.length===0)throw new k("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 bo(t){return t.type!=="response.output_text.delta"||typeof t.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:t.delta}]}}}}function ko(t){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:ka,data:t}}]}}}}function So(t){let e=q(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=q(d);if(!(l?.type!=="message"||!Array.isArray(l.content)))for(let p of l.content){let m=q(p);m?.type==="output_text"&&typeof m.text=="string"&&(s+=m.text)}}s&&o.push({role:"assistant",content:s});for(let d of n){let l=q(d);if(l?.type!=="function_call")continue;if(typeof l.call_id!="string"||typeof l.name!="string")throw new k(`upstream function_call item is missing call_id/name: ${JSON.stringify(d)}`,we);let p=typeof l.arguments=="string"&&l.arguments.length>0?l.arguments:"{}",m;try{m=JSON.parse(p)}catch{throw new k(`upstream function_call arguments are not valid JSON: ${p}`,we)}r.push({id:l.call_id,name:l.name,args:m}),o.push({type:"function_call",call_id:l.call_id,name:l.name,arguments:p})}let i=[];r.length>0&&i.push({toolCall:{functionCalls:r}}),i.push({serverContent:{generationComplete:!0}});let a=q(e.usage),u={serverContent:{turnComplete:!0}};if(a){let d={};typeof a.input_tokens=="number"&&(d.promptTokenCount=a.input_tokens),typeof a.output_tokens=="number"&&(d.responseTokenCount=a.output_tokens),typeof a.total_tokens=="number"&&(d.totalTokenCount=a.total_tokens),Object.keys(d).length>0&&(u.usageMetadata=d)}return i.push(u),{messages:i,assistantItems:o}}var Ea=256,En=class{snapshots=new Map;store(e,n,r){for(this.snapshots.set(`${e}\0${n}`,r);this.snapshots.size>Ea;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(e,n){return this.snapshots.get(`${e}\0${n}`)}},Aa="local",st=t=>t.slice(0,120),An=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 U){this.ws.close(ao,st(e.message));return}if(e instanceof X){this.ws.close(Dt,st(e.message));return}if(e instanceof k){this.ws.close(e.closeCode,st(e.message));return}this.ws.close(we,st(String(e instanceof Error?e.message:e)))}onMessage(e){try{let n=uo(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 k(`"${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}=ho(n.clientContent);this.transcript.push(...o),s&&this.enqueue(()=>this.runTurn());return}if(n.kind==="realtimeInput"){let o=_o(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:wo);return}this.transcript.push(...vo(n.toolResponse)),this.enqueue(()=>this.runTurn())}catch(n){this.fail(n)}}async handleSetup(e){if(this.config)throw new k("setup may only be sent once, as the first client message");let n=fo(e),r=n.sessionResumption.handle;if(r!==null){let s=this.registry.get(this.scope,r);if(!s)throw new k("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",Dt);if(s.model!==n.model)throw new k(`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 k("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 k(`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`,Dt);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),a=i.onOutputFrame(d=>this.send(po(d))),u=i.onLaneError(d=>this.fail(d));this.detachVideoOut=()=>{a(),u()},o[Pe]=co(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 k("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openAudio;if(!n)throw new k("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native AudioBridge seam over the canonical `audio` lane)",we);return this.audio=(async()=>{let r=await n.call(this.clients,e.model);return this.detachAudio=r.onOutputAudio(o=>this.send(ko(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 k("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openVideo;if(!n)throw new k("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",we);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 k(`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 k("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 k("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,xo.randomUUID)();this.registry.store(this.scope,r,{model:e.model,proxyHandle:n,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(r),this.send(mo(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(go(e.timeLeftMs)),this.ws.close(we,st(`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 u of r){if(n.interrupted)break;let d=u;if(d.type==="error")throw new k(`upstream error: ${JSON.stringify(d)}`,we);if(d.type==="response.completed"){o=d.response,s=!0;continue}let l=bo(d);l&&this.send(l)}if(n.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!s)throw new k("upstream produced no response.completed event",we);let{messages:i,assistantItems:a}=So(o);this.transcript.push(...a);for(let u of i)this.send(u);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},Ce=class extends Error{constructor(n,r){super(r);this.status=n;this.name="UpgradeAuthError"}status};function Ia(t,e,n){let r=new Set;(u=>{if(typeof u=="string")u!==""&&r.add(u);else if(Array.isArray(u))for(let d of u)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 Ce(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 Ce(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 a;for(let u of r)a=u;if(a===void 0&&n)throw new Ce(401,"missing credentials: send the org API key in the x-goog-api-key header or as Authorization: Bearer");return a??""}function Eo(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 Ro.WebSocketServer({noServer:!0}),o=new En;t.on("upgrade",(i,a,u)=>{let d=new URL(i.url??"/","http://localhost");d.pathname===io&&(a.on("error",()=>{}),(async()=>{let l=Ia(i,d,n),p;if(e.authenticate)p=await e.authenticate(l,i);else{if(e.apiKey!==void 0&&l!==e.apiKey)throw new Ce(401,"invalid local proxy api key");p={clients:e.clients,scope:Aa}}r.handleUpgrade(i,a,u,m=>{new An(m,p,o)})})().catch(l=>{let p=502;(l instanceof Ce||typeof l=="object"&&l!==null&&"status"in l&&typeof l.status=="number")&&(p=l.status);let m=p===401?"Unauthorized":p===403?"Forbidden":p===502?"Bad Gateway":"Internal Server Error",c=l instanceof Error?l.message:String(l);a.destroyed||(a.write(`HTTP/1.1 ${p} ${m}\r
7
7
  Connection: close\r
8
8
  \r
9
9
  ${c}\r
10
- `),a.destroy())}))});let s=null;return()=>(s??=(async()=>{for(let i of r.clients)i.close(1001,"server closing");await new Promise((i,a)=>r.close(u=>u===void 0?i():a(u)))})(),s)}var xe=require("ws");var Eo=require("crypto"),Ao="Inference-Id";function ot(){return(0,Eo.randomUUID)()}function An(t){return t==="/v1"||t.startsWith("/v1/")}function Io(t,e){if(!An(t)||e===null)throw new Error(`no inference turn for ${t}: an inference lane was reached off the /v1 surface, so its response would carry no Inference-Id and could never be billed`);return e}function st(t){return`resp_${t}`}function Oo(t){return`chatcmpl-${t}`}function To(t){return`msg_${t}`}function ie(t){return e=>{t.sessionId=e.sessionId,t.catalog=e.identity}}function Po(t){let e=t.now??(()=>performance.now());return{event:"inference_request",ts:new Date().toISOString(),inference_id:t.turn.id,method:t.method,path:t.path,status:t.res.headersSent?t.res.statusCode:null,delivered:t.res.writableFinished,stream_error:t.turn.streamError,model:t.turn.model,stream:t.turn.stream,org:t.turn.org,duration_ms:e()-t.turn.startedAt,ttft_ms:t.turn.stream===!0?t.turn.ttftMs:null,prompt_tokens:t.turn.promptTokens,completion_tokens:t.turn.completionTokens,output_units:t.turn.outputUnits,output_minutes:t.turn.outputMinutes,session_id:t.turn.sessionId,queue_ms:t.turn.timing?.queue_ms??null,prefill_ms:t.turn.timing?.prefill_ms??null,decode_ms:t.turn.timing?.decode_ms??null,total_ms:t.turn.timing?.total_ms??null,catalog:t.turn.catalog}}function Co(t){return`${JSON.stringify(t)}
11
- `}var ba=256,ka=128;function ne(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,ka)}var $e=class t{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(e,n,r){this.requests[e]=(this.requests[e]??0)+1,this.localInBytes+=n,this.naiveInBytes+=r}modelEntry(e){let n=this.models[e];if(n)return n;let r=Object.keys(this.models).length>=ba?"(other)":e;return this.models[r]??={requests:0,tokens_out:0}}model(e){this.modelEntry(e).requests+=1}track(e,n,r){let o=this,s=performance.now(),i=null,a=0;return(async function*(){for await(let d of e)d.type==="response.output_text.delta"&&typeof d.delta=="string"&&(i===null&&(i=performance.now(),o.ttftMs.push(i-s),o.ttftMs.length>512&&o.ttftMs.shift(),r!==void 0&&(r.ttftMs=i-r.startedAt)),a+=1,o.tokensOut+=1,o.charsOut+=d.delta.length,n!==void 0&&(o.modelEntry(n).tokens_out+=1)),yield d;let u=(performance.now()-(i??s))/1e3;a>0&&u>0&&(o.tokRates.push(a/u),o.tokRates.length>512&&o.tokRates.shift())})()}static p50(e){if(!e.length)return null;let n=[...e].sort((r,o)=>r-o);return n[Math.floor(n.length/2)]}snapshot(){let e=Object.values(this.requests).reduce((n,r)=>n+r,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:t.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:t.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:e},persistent_connections:1,history_bytes_saved:Math.max(0,this.naiveInBytes-this.localInBytes),history_bytes_saved_pct:this.naiveInBytes>0?Math.round((1-this.localInBytes/this.naiveInBytes)*1e3)/10:null}}}},Sa=256;function Ra(t){if(typeof t=="string")return[{role:"user",content:t}];if(Array.isArray(t))return t;throw new Error("responses input must be a string or an array of messages/items")}var Me=class extends Error{},qe=class{conversations=new Map;thread(e,n){let r=Ra(n);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new Me(`unknown previous_response_id ${String(e)} (proxy restarts drop stored responses)`);return[...o,...r]}remember(e,n,r,o=!0){this.conversations.set(e,[...n,...r]);for(let s of this.conversations.keys()){if(this.conversations.size<=Sa)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},$o=new WeakMap;function it(t){let e=$o.get(t);return e||(e=new qe,$o.set(t,e)),e}function He(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function pe(t){return t.find(e=>e.type==="response.completed")?.response??null}function Ge(t){return t.type!=="response.function_call_arguments.delta"||typeof t.tool_index!="number"?null:{tool_index:t.tool_index,call_id:typeof t.call_id=="string"?t.call_id:void 0,name:typeof t.name=="string"?t.name:void 0,delta:typeof t.delta=="string"?t.delta:""}}var Re=class{calls=new Map;add(e){let n=this.calls.get(e.tool_index),r=!n;return n||(n={args:""},this.calls.set(e.tool_index,n)),e.call_id&&(n.id=e.call_id),e.name&&(n.name=e.name),n.args+=e.delta,{call:n,isNew:r}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([e,n])=>({type:"function_call",call_id:n.id??`call_${e}`,name:n.name??"",arguments:n.args}))}},xa=["<tool_call>","<function="];function Ea(t){return xa.find(e=>t.includes(e))}function Le(t,e){return`serving row emitted a prose tool call (literal ${JSON.stringify(e)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${t}" (see catalog parser_defaults)`}function Ue(t,e,n){if(!(!t||e>0))return Ea(n)}function Ve(t,e){let n=e?.output_text;return typeof n=="string"?n:t.filter(r=>r.type==="response.output_text.delta"&&typeof r.delta=="string").map(r=>r.delta).join("")}function Aa(t){let e=t?.output;if(!Array.isArray(e))return[];let n=[];for(let r of e){if(r===null||typeof r!="object")continue;let o=r;o.type==="message"?n.push({role:"assistant",content:o.content??""}):o.type==="function_call"&&n.push({type:"function_call",call_id:o.call_id,name:o.name,arguments:o.arguments})}return n}function In(t,e){let n=Aa(t);return n.length>0?n:e?[{role:"assistant",content:e}]:[]}var ae=class extends Error{constructor(n,r,o=400,s){super(n);this.code=r;this.status=o;this.param=s}code;status;param};function Je(t,e){try{return t.thread(e.previous_response_id,e.input)}catch(n){throw n instanceof Me?new ae(n.message,"previous_response_not_found",400,"previous_response_id"):new ae(String(n instanceof Error?n.message:n),"invalid_input")}}function Mo(t){let e=t.text;if(e!==null&&typeof e=="object"){let n=e.format;if(n!==void 0)return n}}async function On(t,e,n,r,o){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new ae("instructions must be a string","invalid_instructions");let s=typeof e.instructions=="string"?e.instructions:void 0;return t.createResponse({model:e.model,input:n,stream:r,tools:e.tools,tool_choice:e.tool_choice,temperature:e.temperature,top_p:e.top_p,stop:e.stop,...s!==void 0?{instructions:s}:{},max_output_tokens:e.max_output_tokens,...e.reasoning_effort!==void 0?{reasoning_effort:e.reasoning_effort}:{},...e.chat_template_kwargs!==void 0?{chat_template_kwargs:e.chat_template_kwargs}:{},...Mo(e)!==void 0?{response_format:Mo(e)}:{}},o)}async function at(t){let{body:e,clients:n,store:r,stats:o}=t,s=Je(r,e);e.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(s))-Buffer.byteLength(JSON.stringify(e.input)));let i=t.responseId,a=t.remember??e.store!==!1,u=e.store!==!1,d=ne(e);o.model(d);let l=Array.isArray(e.tools)&&e.tools.length>0,p=t.turn,m=await On(n,e,s,!0,p?ie(p):void 0);return{responseId:i,events:Pa({events:m,conversation:s,responseId:i,modelKey:d,requestHadTools:l,remember:a,persist:u,store:r,stats:o,turn:t.turn})}}function Ia(t,e){let n=[...t].sort((u,d)=>u.output_index-d.output_index).map(({item:u})=>u),r=Array.isArray(e?.output)?e.output:[],o=r.filter(u=>u!==null&&typeof u=="object"&&u.type==="message"),s=n.filter(u=>u.type==="message").length===o.length,i=0;return[...n.map(u=>u.type==="message"&&s?Ta(o[i++],u):u),...Oa(r,n)]}function Oa(t,e){let n=e.some(d=>d.type==="message"),r=e.some(d=>d.type==="reasoning"),o=d=>String(d.call_id??d.id??""),s=e.filter(d=>d.type==="function_call"),i=t.filter(d=>d!==null&&typeof d=="object"&&d.type==="function_call").length,a=s.length===i,u=new Set(s.map(o));return t.filter(d=>{if(d===null||typeof d!="object")return!0;let l=d;return l.type==="message"?!n:l.type==="reasoning"?!r:l.type==="function_call"?!a&&!u.has(o(l)):!0})}function Ta(t,e){let n=a=>a!==null&&typeof a=="object"&&a.type==="output_text",r=Array.isArray(t.content)?t.content:[];if(r.filter(n).length!==1)return e;let s=(Array.isArray(e.content)?e.content:[]).find(n);if(s===void 0)return e;let i={...t};return i.id=e.id,t.role===void 0&&(i.role=e.role),t.status===void 0&&(i.status=e.status),i.content=r.map(a=>n(a)?{...a,text:s.text}:a),i}async function*Pa(t){let{events:e,conversation:n,responseId:r,modelKey:o,requestHadTools:s,remember:i,persist:a,store:u,stats:d}=t,l=[],p=T=>T.response&&typeof T.response=="object"?{...T,response:{...T.response,id:r}}:T,m=`item_${r.slice(5)}`,c=!1,f=!1,g=0;function*_(){c||(c=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let y=[],h=(T,N)=>{y.push({output_index:T,item:N})},w=!1,A=0,v="",b=0,R="";function*L(){if(!w)return;w=!1,yield{type:"response.reasoning_text.done",item_id:v,output_index:A,content_index:0,text:R};let T={id:v,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:R}],status:"completed"};yield{type:"response.output_item.done",output_index:A,item:T},h(A,T),R=""}let S=!1,x=0,C="",F=0,Y="",te="";function*ue(T){if(!S)return;S=!1,yield{type:"response.output_text.done",item_id:C,output_index:x,content_index:0,text:Y},yield{type:"response.content_part.done",item_id:C,output_index:x,content_index:0,part:{type:"output_text",text:Y}};let N={id:C,type:"message",role:"assistant",status:T,content:[{type:"output_text",text:Y}]};yield{type:"response.output_item.done",output_index:x,item:N},h(x,N),Y=""}let ve=new Re,G=null,V=-1;function*Z(T){if(!G)return;let N=G;G=null,yield{type:"response.function_call_arguments.done",item_id:N.fcId,output_index:N.outputIndex,arguments:N.args};let z={id:N.fcId,type:"function_call",call_id:N.callId??N.fcId,name:N.name??"",arguments:N.args,status:T};yield{type:"response.output_item.done",output_index:N.outputIndex,item:z},h(N.outputIndex,z)}let Ae=!1;for await(let T of d.track(e,o,t.turn)){l.push(T);let N=String(T.type??"");if(N==="response.created"||N==="response.in_progress"){c=!0,yield p(T);continue}if(N==="response.output_item.added"){f=!0,yield p(T);continue}if(N==="response.reasoning_text.delta"&&!f){yield*_(),w||(w=!0,A=g++,b+=1,v=b===1?`rs_${r.slice(5)}`:`rs_${r.slice(5)}_${b}`,yield{type:"response.output_item.added",output_index:A,item:{id:v,type:"reasoning",summary:[],content:[],status:"in_progress"}}),R+=String(T.delta??""),yield{type:"response.reasoning_text.delta",item_id:v,output_index:A,content_index:0,delta:T.delta};continue}if(N==="response.output_text.delta"&&!f){yield*_(),yield*L(),S||(S=!0,x=g++,F+=1,C=F===1?m:`${m}_${F}`,yield{type:"response.output_item.added",output_index:x,item:{id:C,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:C,output_index:x,content_index:0,part:{type:"output_text",text:""}}),Y+=String(T.delta??""),te+=String(T.delta??""),yield{type:N,item_id:C,output_index:x,content_index:0,delta:T.delta};continue}let z=f?null:Ge(T);if(z){if(yield*_(),yield*L(),yield*ue("completed"),ve.add(z),z.tool_index!==V){yield*Z("completed"),V=z.tool_index;let he=g++;G={fcId:`fc_${r.slice(5)}_${z.tool_index}`,outputIndex:he,callId:z.call_id,name:z.name,args:""},yield{type:"response.output_item.added",output_index:he,item:{id:G.fcId,type:"function_call",call_id:G.callId??G.fcId,name:G.name??"",arguments:"",status:"in_progress"}}}G&&(z.call_id&&(G.callId=z.call_id),z.name&&(G.name=z.name),G.args+=z.delta,yield{type:"response.function_call_arguments.delta",item_id:G.fcId,output_index:G.outputIndex,delta:z.delta});continue}if(N==="response.completed"&&!f){let he=He(T.response),B=Ue(s,ve.size+he.length,te);if(B){Ae=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:Le(o,B)}};break}if(yield*L(),yield*ue("completed"),yield*Z("completed"),ve.size===0)for(let be of he){let Fe=g++,vt=be.id??be.call_id??`fc_${Fe}`,cn=be.arguments??"",dr={id:vt,type:"function_call",call_id:be.call_id??vt,name:be.name??""};yield{type:"response.output_item.added",output_index:Fe,item:{...dr,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:vt,output_index:Fe,delta:cn},yield{type:"response.function_call_arguments.done",item_id:vt,output_index:Fe,arguments:cn};let lr={...dr,arguments:cn,status:"completed"};yield{type:"response.output_item.done",output_index:Fe,item:lr},h(Fe,lr)}let ee=Ia(y,T.response);yield ee.length>0?p({...T,response:{...T.response,output:ee}}):p(T);continue}yield p(T)}if(yield*L(),yield*ue("incomplete"),yield*Z("incomplete"),i&&!Ae){let T=pe(l);u.remember(r,n,In(T,Ve(l,T)),a)}}var Ca="/v1/responses",$a=64*1024*1024,Ma=8*1024*1024,La=16,Lo=32,Uo=3600*1e3,Ua=/^[A-Za-z0-9._-]{1,256}$/;function re(t,e,n){return{type:"error",status:t,...n!==void 0?{stream_id:n}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var Na=re(400,{type:"invalid_request_error",code:"invalid_stream_id",message:"The 'stream_id' field must be a non-empty string with at most 256 characters and may only contain letters, numbers, underscores, hyphens, and periods.",param:"stream_id"});function Cn(){let t,e;return{promise:new Promise((r,o)=>{t=r,e=o}),resolve:t,reject:e}}async function Da(t){for(;t.bufferedAmount>Ma&&t.readyState===xe.WebSocket.OPEN;){let{promise:e,resolve:n}=Cn();setTimeout(n,5),await e}}var Tn=class extends qe{constructor(n){super();this.shared=n}shared;thread(n,r){if(n==null)return super.thread(n,r);try{return super.thread(n,r)}catch(o){if(!(o instanceof Me))throw o;return this.shared.thread(n,r)}}remember(n,r,o,s=!0){s?this.shared.remember(n,r,o):super.remember(n,r,o)}forget(n){super.forget(n),this.shared.forget(n)}},Pn=class{constructor(e,n,r,o){this.ws=e;this.clients=n;this.stats=r;this.statusOf=o;this.store=new Tn(it(n)),e.on("message",(i,a)=>this.onMessage(i,a)),e.on("close",()=>this.dispose()),e.on("error",()=>this.dispose()),setTimeout(()=>{this.closed||(this.sendFrame(re(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${Uo/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},Uo).unref?.()}ws;clients;stats;statusOf;store;lanes=new Map;laneOfResponse=new Map;activeTurns=new Set;active=0;waiters=[];closed=!1;onMessage(e,n){if(this.closed)return;if(n){this.sendFrame(re(400,{type:"invalid_request_error",code:"binary_frame",message:"Responses WebSocket frames must be JSON text"})),this.ws.close(1003,"binary frames are not supported");return}let r;try{r=JSON.parse(e.toString())}catch{this.sendFrame(re(400,{type:"invalid_request_error",code:"invalid_json",message:"frame is not valid JSON"}));return}if(r===null||typeof r!="object"||Array.isArray(r)){this.sendFrame(re(400,{type:"invalid_request_error",code:"invalid_event",message:"frame must be a JSON object"}));return}let o=r;if(o.type!=="response.create"){this.sendFrame(re(400,{type:"invalid_request_error",code:"unknown_event",message:`unknown client event type ${JSON.stringify(o.type??null)}; only "response.create" is supported`}));return}if("stream"in o){this.sendFrame(re(400,{type:"invalid_request_error",code:"stream_is_implicit",message:"'stream' is implicit over the Responses WebSocket and must not be sent",param:"stream"}));return}if("background"in o){this.sendFrame(re(400,{type:"invalid_request_error",code:"background_not_supported",message:"'background' is not supported over the Responses WebSocket",param:"background"}));return}let s;if(o.stream_id!==void 0){if(typeof o.stream_id!="string"||!Ua.test(o.stream_id)){this.sendFrame(Na);return}if(s=o.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=Lo){this.sendFrame(re(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${Lo}). Reuse an existing stream_id or open a new WebSocket connection.`,param:"stream_id"},s));return}}let i=o.generate===!1?()=>this.runWarmup(o,s):()=>this.runTurn(o,s),a=s??"",u=this.lanes.get(a)??Promise.resolve();this.lanes.set(a,u.then(i).catch(()=>{}))}namedLaneCount(){return this.lanes.has("")?this.lanes.size-1:this.lanes.size}async runTurn(e,n){if(!this.closed){await this.acquire();try{let r=Buffer.byteLength(JSON.stringify(e));this.stats.request("responses_ws",r,r);let o;try{o=await at({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:st(ot())})}catch(i){this.failTurn(i,n,e);return}this.laneOfResponse.set(o.responseId,n??null);let s=o.events;this.activeTurns.add(s);try{for await(let i of s){if(this.closed||this.ws.readyState!==xe.WebSocket.OPEN)break;this.send(i,n),await Da(this.ws)}}finally{this.activeTurns.delete(s)}}catch(r){this.failTurn(r,n,e)}finally{this.release()}}}runWarmup(e,n){if(this.closed)return;let r=st(ot());try{let o=Je(this.store,{...e,input:e.input??(e.previous_response_id!=null?[]:void 0)});this.store.remember(r,o,[],e.store!==!1),this.laneOfResponse.set(r,n??null),this.send({type:"response.created",response:{id:r,object:"response",status:"in_progress"}},n)}catch(o){this.failTurn(o,n,e)}}failTurn(e,n,r){this.sendFrame(this.errorFrameOf(e,n));let o=r.previous_response_id;typeof o=="string"&&this.laneOfResponse.get(o)===(n??null)&&(this.store.forget(o),this.laneOfResponse.delete(o))}errorFrameOf(e,n){let r=String(e instanceof Error?e.message:e);if(e instanceof ae)return re(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},n);if(e instanceof U)return re(404,{type:"invalid_request_error",code:"model_not_found",message:r},n);if(e instanceof X)return re(503,{type:"upstream_error",code:"session_gone",message:r},n);let o=this.statusOf?.(e)??null;return o?re(o.status,{type:o.openaiType,message:r},n):re(500,{type:"upstream_error",message:r},n)}async acquire(){for(;this.active>=La;){let{promise:e,resolve:n}=Cn();this.waiters.push(n),await e}this.active++}release(){this.active--,this.waiters.shift()?.()}dispose(){if(!this.closed){this.closed=!0;for(let e of[...this.activeTurns])e.return(void 0);this.activeTurns.clear(),this.lanes.clear(),this.laneOfResponse.clear();for(let e of this.waiters.splice(0))e()}}send(e,n){if(this.closed||this.ws.readyState!==xe.WebSocket.OPEN)return;let r=n===void 0?e:{...e,stream_id:n};this.ws.send(JSON.stringify(r))}sendFrame(e){this.closed||this.ws.readyState!==xe.WebSocket.OPEN||this.ws.send(JSON.stringify(e))}};function Dt(t,e,n){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${n}\r
10
+ `),a.destroy())}))});let s=null;return()=>(s??=(async()=>{for(let i of r.clients)i.close(1001,"server closing");await new Promise((i,a)=>r.close(u=>u===void 0?i():a(u)))})(),s)}var Re=require("ws");var Ao=require("crypto"),Io="Inference-Id";function it(){return(0,Ao.randomUUID)()}function In(t){return t==="/v1"||t.startsWith("/v1/")}function Oo(t,e){if(!In(t)||e===null)throw new Error(`no inference turn for ${t}: an inference lane was reached off the /v1 surface, so its response would carry no Inference-Id and could never be billed`);return e}function at(t){return`resp_${t}`}function To(t){return`chatcmpl-${t}`}function Po(t){return`msg_${t}`}function ie(t){return e=>{t.sessionId=e.sessionId,t.catalog=e.identity}}function Co(t){let e=t.now??(()=>performance.now());return{event:"inference_request",ts:new Date().toISOString(),inference_id:t.turn.id,method:t.method,path:t.path,status:t.res.headersSent?t.res.statusCode:null,delivered:t.res.writableFinished,stream_error:t.turn.streamError,model:t.turn.model,stream:t.turn.stream,org:t.turn.org,duration_ms:e()-t.turn.startedAt,ttft_ms:t.turn.stream===!0?t.turn.ttftMs:null,prompt_tokens:t.turn.promptTokens,completion_tokens:t.turn.completionTokens,output_units:t.turn.outputUnits,output_minutes:t.turn.outputMinutes,session_id:t.turn.sessionId,queue_ms:t.turn.timing?.queue_ms??null,prefill_ms:t.turn.timing?.prefill_ms??null,decode_ms:t.turn.timing?.decode_ms??null,total_ms:t.turn.timing?.total_ms??null,catalog:t.turn.catalog}}function $o(t){return`${JSON.stringify(t)}
11
+ `}var Oa=256,Ta=128;function ne(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,Ta)}var $e=class t{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(e,n,r){this.requests[e]=(this.requests[e]??0)+1,this.localInBytes+=n,this.naiveInBytes+=r}modelEntry(e){let n=this.models[e];if(n)return n;let r=Object.keys(this.models).length>=Oa?"(other)":e;return this.models[r]??={requests:0,tokens_out:0}}model(e){this.modelEntry(e).requests+=1}track(e,n,r){let o=this,s=performance.now(),i=null,a=0;return(async function*(){for await(let d of e)d.type==="response.output_text.delta"&&typeof d.delta=="string"&&(i===null&&(i=performance.now(),o.ttftMs.push(i-s),o.ttftMs.length>512&&o.ttftMs.shift(),r!==void 0&&(r.ttftMs=i-r.startedAt)),a+=1,o.tokensOut+=1,o.charsOut+=d.delta.length,n!==void 0&&(o.modelEntry(n).tokens_out+=1)),yield d;let u=(performance.now()-(i??s))/1e3;a>0&&u>0&&(o.tokRates.push(a/u),o.tokRates.length>512&&o.tokRates.shift())})()}static p50(e){if(!e.length)return null;let n=[...e].sort((r,o)=>r-o);return n[Math.floor(n.length/2)]}snapshot(){let e=Object.values(this.requests).reduce((n,r)=>n+r,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:t.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:t.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:e},persistent_connections:1,history_bytes_saved:Math.max(0,this.naiveInBytes-this.localInBytes),history_bytes_saved_pct:this.naiveInBytes>0?Math.round((1-this.localInBytes/this.naiveInBytes)*1e3)/10:null}}}},Pa=256;function Ca(t){if(typeof t=="string")return[{role:"user",content:t}];if(Array.isArray(t))return t;throw new Error("responses input must be a string or an array of messages/items")}var Me=class extends Error{},He=class{conversations=new Map;thread(e,n){let r=Ca(n);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new Me(`unknown previous_response_id ${String(e)} (proxy restarts drop stored responses)`);return[...o,...r]}remember(e,n,r,o=!0){this.conversations.set(e,[...n,...r]);for(let s of this.conversations.keys()){if(this.conversations.size<=Pa)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},Mo=new WeakMap;function ut(t){let e=Mo.get(t);return e||(e=new He,Mo.set(t,e)),e}function Ge(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function pe(t){return t.find(e=>e.type==="response.completed")?.response??null}function Ve(t){return t.type!=="response.function_call_arguments.delta"||typeof t.tool_index!="number"?null:{tool_index:t.tool_index,call_id:typeof t.call_id=="string"?t.call_id:void 0,name:typeof t.name=="string"?t.name:void 0,delta:typeof t.delta=="string"?t.delta:""}}var xe=class{calls=new Map;add(e){let n=this.calls.get(e.tool_index),r=!n;return n||(n={args:""},this.calls.set(e.tool_index,n)),e.call_id&&(n.id=e.call_id),e.name&&(n.name=e.name),n.args+=e.delta,{call:n,isNew:r}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([e,n])=>({type:"function_call",call_id:n.id??`call_${e}`,name:n.name??"",arguments:n.args}))}},$a=["<tool_call>","<function="];function Ma(t){return $a.find(e=>t.includes(e))}function Le(t,e){return`serving row emitted a prose tool call (literal ${JSON.stringify(e)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${t}" (see catalog parser_defaults)`}function Ue(t,e,n){if(!(!t||e>0))return Ma(n)}function Je(t,e){let n=e?.output_text;return typeof n=="string"?n:t.filter(r=>r.type==="response.output_text.delta"&&typeof r.delta=="string").map(r=>r.delta).join("")}function La(t){let e=t?.output;if(!Array.isArray(e))return[];let n=[];for(let r of e){if(r===null||typeof r!="object")continue;let o=r;o.type==="message"?n.push({role:"assistant",content:o.content??""}):o.type==="function_call"&&n.push({type:"function_call",call_id:o.call_id,name:o.name,arguments:o.arguments})}return n}function On(t,e){let n=La(t);return n.length>0?n:e?[{role:"assistant",content:e}]:[]}var ae=class extends Error{constructor(n,r,o=400,s){super(n);this.code=r;this.status=o;this.param=s}code;status;param};function We(t,e){try{return t.thread(e.previous_response_id,e.input)}catch(n){throw n instanceof Me?new ae(n.message,"previous_response_not_found",400,"previous_response_id"):new ae(String(n instanceof Error?n.message:n),"invalid_input")}}function Lo(t){let e=t.text;if(e!==null&&typeof e=="object"){let n=e.format;if(n!==void 0)return n}}async function Tn(t,e,n,r,o){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new ae("instructions must be a string","invalid_instructions");let s=typeof e.instructions=="string"?e.instructions:void 0;return t.createResponse({model:e.model,input:n,stream:r,tools:e.tools,tool_choice:e.tool_choice,temperature:e.temperature,top_p:e.top_p,stop:e.stop,...s!==void 0?{instructions:s}:{},max_output_tokens:e.max_output_tokens,...e.reasoning_effort!==void 0?{reasoning_effort:e.reasoning_effort}:{},...e.chat_template_kwargs!==void 0?{chat_template_kwargs:e.chat_template_kwargs}:{},...Lo(e)!==void 0?{response_format:Lo(e)}:{}},o)}async function dt(t){let{body:e,clients:n,store:r,stats:o}=t,s=We(r,e);e.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(s))-Buffer.byteLength(JSON.stringify(e.input)));let i=t.responseId,a=t.remember??e.store!==!1,u=e.store!==!1,d=ne(e);o.model(d);let l=Array.isArray(e.tools)&&e.tools.length>0,p=t.turn,m=await Tn(n,e,s,!0,p?ie(p):void 0);return{responseId:i,events:Fa({events:m,conversation:s,responseId:i,modelKey:d,requestHadTools:l,remember:a,persist:u,store:r,stats:o,turn:t.turn})}}function Ua(t,e){let n=[...t].sort((u,d)=>u.output_index-d.output_index).map(({item:u})=>u),r=Array.isArray(e?.output)?e.output:[],o=r.filter(u=>u!==null&&typeof u=="object"&&u.type==="message"),s=n.filter(u=>u.type==="message").length===o.length,i=0;return[...n.map(u=>u.type==="message"&&s?Da(o[i++],u):u),...Na(r,n)]}function Na(t,e){let n=e.some(d=>d.type==="message"),r=e.some(d=>d.type==="reasoning"),o=d=>String(d.call_id??d.id??""),s=e.filter(d=>d.type==="function_call"),i=t.filter(d=>d!==null&&typeof d=="object"&&d.type==="function_call").length,a=s.length===i,u=new Set(s.map(o));return t.filter(d=>{if(d===null||typeof d!="object")return!0;let l=d;return l.type==="message"?!n:l.type==="reasoning"?!r:l.type==="function_call"?!a&&!u.has(o(l)):!0})}function Da(t,e){let n=a=>a!==null&&typeof a=="object"&&a.type==="output_text",r=Array.isArray(t.content)?t.content:[];if(r.filter(n).length!==1)return e;let s=(Array.isArray(e.content)?e.content:[]).find(n);if(s===void 0)return e;let i={...t};return i.id=e.id,t.role===void 0&&(i.role=e.role),t.status===void 0&&(i.status=e.status),i.content=r.map(a=>n(a)?{...a,text:s.text}:a),i}async function*Fa(t){let{events:e,conversation:n,responseId:r,modelKey:o,requestHadTools:s,remember:i,persist:a,store:u,stats:d}=t,l=[],p=T=>T.response&&typeof T.response=="object"?{...T,response:{...T.response,id:r}}:T,m=`item_${r.slice(5)}`,c=!1,f=!1,g=0;function*b(){c||(c=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let y=[],h=(T,N)=>{y.push({output_index:T,item:N})},v=!1,A=0,w="",_=0,x="";function*C(){if(!v)return;v=!1,yield{type:"response.reasoning_text.done",item_id:w,output_index:A,content_index:0,text:x};let T={id:w,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:x}],status:"completed"};yield{type:"response.output_item.done",output_index:A,item:T},h(A,T),x=""}let S=!1,R=0,$="",F=0,Y="",te="";function*ue(T){if(!S)return;S=!1,yield{type:"response.output_text.done",item_id:$,output_index:R,content_index:0,text:Y},yield{type:"response.content_part.done",item_id:$,output_index:R,content_index:0,part:{type:"output_text",text:Y}};let N={id:$,type:"message",role:"assistant",status:T,content:[{type:"output_text",text:Y}]};yield{type:"response.output_item.done",output_index:R,item:N},h(R,N),Y=""}let ve=new xe,G=null,V=-1;function*Z(T){if(!G)return;let N=G;G=null,yield{type:"response.function_call_arguments.done",item_id:N.fcId,output_index:N.outputIndex,arguments:N.args};let z={id:N.fcId,type:"function_call",call_id:N.callId??N.fcId,name:N.name??"",arguments:N.args,status:T};yield{type:"response.output_item.done",output_index:N.outputIndex,item:z},h(N.outputIndex,z)}let Ae=!1;for await(let T of d.track(e,o,t.turn)){l.push(T);let N=String(T.type??"");if(N==="response.created"||N==="response.in_progress"){c=!0,yield p(T);continue}if(N==="response.output_item.added"){f=!0,yield p(T);continue}if(N==="response.reasoning_text.delta"&&!f){yield*b(),v||(v=!0,A=g++,_+=1,w=_===1?`rs_${r.slice(5)}`:`rs_${r.slice(5)}_${_}`,yield{type:"response.output_item.added",output_index:A,item:{id:w,type:"reasoning",summary:[],content:[],status:"in_progress"}}),x+=String(T.delta??""),yield{type:"response.reasoning_text.delta",item_id:w,output_index:A,content_index:0,delta:T.delta};continue}if(N==="response.output_text.delta"&&!f){yield*b(),yield*C(),S||(S=!0,R=g++,F+=1,$=F===1?m:`${m}_${F}`,yield{type:"response.output_item.added",output_index:R,item:{id:$,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:$,output_index:R,content_index:0,part:{type:"output_text",text:""}}),Y+=String(T.delta??""),te+=String(T.delta??""),yield{type:N,item_id:$,output_index:R,content_index:0,delta:T.delta};continue}let z=f?null:Ve(T);if(z){if(yield*b(),yield*C(),yield*ue("completed"),ve.add(z),z.tool_index!==V){yield*Z("completed"),V=z.tool_index;let he=g++;G={fcId:`fc_${r.slice(5)}_${z.tool_index}`,outputIndex:he,callId:z.call_id,name:z.name,args:""},yield{type:"response.output_item.added",output_index:he,item:{id:G.fcId,type:"function_call",call_id:G.callId??G.fcId,name:G.name??"",arguments:"",status:"in_progress"}}}G&&(z.call_id&&(G.callId=z.call_id),z.name&&(G.name=z.name),G.args+=z.delta,yield{type:"response.function_call_arguments.delta",item_id:G.fcId,output_index:G.outputIndex,delta:z.delta});continue}if(N==="response.completed"&&!f){let he=Ge(T.response),B=Ue(s,ve.size+he.length,te);if(B){Ae=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:Le(o,B)}};break}if(yield*C(),yield*ue("completed"),yield*Z("completed"),ve.size===0)for(let be of he){let Be=g++,bt=be.id??be.call_id??`fc_${Be}`,pn=be.arguments??"",lr={id:bt,type:"function_call",call_id:be.call_id??bt,name:be.name??""};yield{type:"response.output_item.added",output_index:Be,item:{...lr,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:bt,output_index:Be,delta:pn},yield{type:"response.function_call_arguments.done",item_id:bt,output_index:Be,arguments:pn};let cr={...lr,arguments:pn,status:"completed"};yield{type:"response.output_item.done",output_index:Be,item:cr},h(Be,cr)}let ee=Ua(y,T.response);yield ee.length>0?p({...T,response:{...T.response,output:ee}}):p(T);continue}yield p(T)}if(yield*C(),yield*ue("incomplete"),yield*Z("incomplete"),i&&!Ae){let T=pe(l);u.remember(r,n,On(T,Je(l,T)),a)}}var Ba="/v1/responses",ja=64*1024*1024,qa=8*1024*1024,Ha=16,Uo=32,No=3600*1e3,Ga=/^[A-Za-z0-9._-]{1,256}$/;function re(t,e,n){return{type:"error",status:t,...n!==void 0?{stream_id:n}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var Va=re(400,{type:"invalid_request_error",code:"invalid_stream_id",message:"The 'stream_id' field must be a non-empty string with at most 256 characters and may only contain letters, numbers, underscores, hyphens, and periods.",param:"stream_id"});function $n(){let t,e;return{promise:new Promise((r,o)=>{t=r,e=o}),resolve:t,reject:e}}async function Ja(t){for(;t.bufferedAmount>qa&&t.readyState===Re.WebSocket.OPEN;){let{promise:e,resolve:n}=$n();setTimeout(n,5),await e}}var Pn=class extends He{constructor(n){super();this.shared=n}shared;thread(n,r){if(n==null)return super.thread(n,r);try{return super.thread(n,r)}catch(o){if(!(o instanceof Me))throw o;return this.shared.thread(n,r)}}remember(n,r,o,s=!0){s?this.shared.remember(n,r,o):super.remember(n,r,o)}forget(n){super.forget(n),this.shared.forget(n)}},Cn=class{constructor(e,n,r,o){this.ws=e;this.clients=n;this.stats=r;this.statusOf=o;this.store=new Pn(ut(n)),e.on("message",(i,a)=>this.onMessage(i,a)),e.on("close",()=>this.dispose()),e.on("error",()=>this.dispose()),setTimeout(()=>{this.closed||(this.sendFrame(re(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${No/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},No).unref?.()}ws;clients;stats;statusOf;store;lanes=new Map;laneOfResponse=new Map;activeTurns=new Set;active=0;waiters=[];closed=!1;onMessage(e,n){if(this.closed)return;if(n){this.sendFrame(re(400,{type:"invalid_request_error",code:"binary_frame",message:"Responses WebSocket frames must be JSON text"})),this.ws.close(1003,"binary frames are not supported");return}let r;try{r=JSON.parse(e.toString())}catch{this.sendFrame(re(400,{type:"invalid_request_error",code:"invalid_json",message:"frame is not valid JSON"}));return}if(r===null||typeof r!="object"||Array.isArray(r)){this.sendFrame(re(400,{type:"invalid_request_error",code:"invalid_event",message:"frame must be a JSON object"}));return}let o=r;if(o.type!=="response.create"){this.sendFrame(re(400,{type:"invalid_request_error",code:"unknown_event",message:`unknown client event type ${JSON.stringify(o.type??null)}; only "response.create" is supported`}));return}if("stream"in o){this.sendFrame(re(400,{type:"invalid_request_error",code:"stream_is_implicit",message:"'stream' is implicit over the Responses WebSocket and must not be sent",param:"stream"}));return}if("background"in o){this.sendFrame(re(400,{type:"invalid_request_error",code:"background_not_supported",message:"'background' is not supported over the Responses WebSocket",param:"background"}));return}let s;if(o.stream_id!==void 0){if(typeof o.stream_id!="string"||!Ga.test(o.stream_id)){this.sendFrame(Va);return}if(s=o.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=Uo){this.sendFrame(re(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${Uo}). Reuse an existing stream_id or open a new WebSocket connection.`,param:"stream_id"},s));return}}let i=o.generate===!1?()=>this.runWarmup(o,s):()=>this.runTurn(o,s),a=s??"",u=this.lanes.get(a)??Promise.resolve();this.lanes.set(a,u.then(i).catch(()=>{}))}namedLaneCount(){return this.lanes.has("")?this.lanes.size-1:this.lanes.size}async runTurn(e,n){if(!this.closed){await this.acquire();try{let r=Buffer.byteLength(JSON.stringify(e));this.stats.request("responses_ws",r,r);let o;try{o=await dt({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:at(it())})}catch(i){this.failTurn(i,n,e);return}this.laneOfResponse.set(o.responseId,n??null);let s=o.events;this.activeTurns.add(s);try{for await(let i of s){if(this.closed||this.ws.readyState!==Re.WebSocket.OPEN)break;this.send(i,n),await Ja(this.ws)}}finally{this.activeTurns.delete(s)}}catch(r){this.failTurn(r,n,e)}finally{this.release()}}}runWarmup(e,n){if(this.closed)return;let r=at(it());try{let o=We(this.store,{...e,input:e.input??(e.previous_response_id!=null?[]:void 0)});this.store.remember(r,o,[],e.store!==!1),this.laneOfResponse.set(r,n??null),this.send({type:"response.created",response:{id:r,object:"response",status:"in_progress"}},n)}catch(o){this.failTurn(o,n,e)}}failTurn(e,n,r){this.sendFrame(this.errorFrameOf(e,n));let o=r.previous_response_id;typeof o=="string"&&this.laneOfResponse.get(o)===(n??null)&&(this.store.forget(o),this.laneOfResponse.delete(o))}errorFrameOf(e,n){let r=String(e instanceof Error?e.message:e);if(e instanceof ae)return re(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},n);if(e instanceof U)return re(404,{type:"invalid_request_error",code:"model_not_found",message:r},n);if(e instanceof X)return re(503,{type:"upstream_error",code:"session_gone",message:r},n);let o=this.statusOf?.(e)??null;return o?re(o.status,{type:o.openaiType,message:r},n):re(500,{type:"upstream_error",message:r},n)}async acquire(){for(;this.active>=Ha;){let{promise:e,resolve:n}=$n();this.waiters.push(n),await e}this.active++}release(){this.active--,this.waiters.shift()?.()}dispose(){if(!this.closed){this.closed=!0;for(let e of[...this.activeTurns])e.return(void 0);this.activeTurns.clear(),this.lanes.clear(),this.laneOfResponse.clear();for(let e of this.waiters.splice(0))e()}}send(e,n){if(this.closed||this.ws.readyState!==Re.WebSocket.OPEN)return;let r=n===void 0?e:{...e,stream_id:n};this.ws.send(JSON.stringify(r))}sendFrame(e){this.closed||this.ws.readyState!==Re.WebSocket.OPEN||this.ws.send(JSON.stringify(e))}};function Ft(t,e,n){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${n}\r
12
12
  `;t.end(`HTTP/1.1 ${e} ${r}\r
13
13
  Connection: close\r
14
14
  Content-Type: text/plain; charset=utf-8\r
15
15
  Content-Length: ${Buffer.byteLength(o)}\r
16
16
  \r
17
- ${o}`)}function No(t,e){let n=new xe.WebSocketServer({noServer:!0,maxPayload:$a}),r=new $e,o=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,u=!1,d=(l,p,m)=>{let c;try{c=new URL(l.url??"/","http://localhost")}catch{Dt(p,400,"malformed upgrade URL");return}if(p.on("error",()=>p.destroy()),p.on("close",()=>i.delete(p)),c.pathname!==Ca){i.add(p);return}s.add(p);let f=!1;(async()=>{if(e.apiKey&&(l.headers.authorization??"")!==`Bearer ${e.apiKey}`){Dt(p,401,"invalid local proxy api key");return}let g;try{g=await a(l)}catch(_){let y=e.statusOf?.(_)??null;Dt(p,y?.status??500,"upstream resolver rejected the connection");return}u||p.destroyed||(f=!0,n.handleUpgrade(l,p,m,_=>{s.delete(p),o.add(_),_.on("close",()=>o.delete(_));try{new Pn(_,g,r,e.statusOf)}catch{o.delete(_),_.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!f&&!p.destroyed&&Dt(p,500,"responses websocket upgrade failed")}).finally(()=>s.delete(p))};return t.on("upgrade",d),{stats:r,close:()=>{let{promise:l,resolve:p}=Cn();if(u)return p(),l;u=!0,t.off("upgrade",d);for(let c of s)c.destroy();s.clear();for(let c of i)c.destroy();i.clear();for(let c of o)c.close(1001,"server shutting down");let m=setTimeout(()=>{for(let c of o)c.readyState!==xe.WebSocket.CLOSED&&c.terminate()},2e3);return m.unref?.(),n.close(()=>{clearTimeout(m),p()}),l}}}var cs=require("@urun-sh/core");var Do=require("@urun-sh/core"),Fa={no_capacity:429,queue_timeout:429,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502},Ba={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"};function $n(t,e){let n=Ba[t.code];return e?`${n} (${e})`:n}function Ft(t){if(t==null)return null;let e=Fa[t];return e===void 0?null:{status:e,code:t,retryable:e!==502,openaiType:e===429?"capacity_error":"upstream_error",anthropicType:e===429?"rate_limit_error":e===503?"overloaded_error":"api_error"}}function Bt(t){if(t.kind==="create-failed")return Ft(t.status.error?.code??null);if(t.kind!=="never-live")return null;let e=(t.status.elapsedMs??0)>=Do.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return Ft(e?"cold_start_exceeded":"queue_timeout")}var Bo=Ie(require("os"),1),jo=Ie(require("path"),1),qo=require("url"),Ho=require("util");var Fo="0.6.18";var Go=4141,qa=Fo,qt={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 Un(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 Vo(t,e,n){let r={...t,...Un(e,n)};return delete r.URUN_API_KEY,delete r.URUN_JWT,delete r.URUN_SESSION_TOKEN_SECRET,r}function Jo(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=")),a=n[0]==="exec",u=s.includes("--skip-git-repo-check"),d=a&&!u?["exec","--skip-git-repo-check",...n.slice(1)]:n;return[...r,...i?[]:["-m",e],...d]}function Wo(t,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--provider","urun","--model",t,...e]}function Ko(t,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--model",`urun/${t}`,...e]}function zo(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,qo.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 Yo(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 Xo(t){if(t[0]!=="--port")return null;let e=Yo(t[1]);return t.splice(0,2),e}function Nn(t){let e=t.indexOf("--port");if(e===-1)return Go;let n=Yo(t[e+1]);return t.splice(e,2),n}function Qo(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 Mn(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 We(t){let e=Ha(Mn(t,"URUN_BASE_URL")),n=Mn(t,"URUN_ORG_ID"),r=Mn(t,"URUN_APP"),o=(t.URUN_FUNCTION??"serve").trim()||"serve",s=(t.URUN_API_URL??"").trim(),i=s?jt(s,"URUN_API_URL"):Yr,a=(t.URUN_SESSION_TOKEN_SECRET??"").trim(),u=(t.URUN_JWT??"").trim();if(u)return{baseUrl:e,orgId:n,appId:r,fnName:o,apiUrl:i,decisionSecret:a,auth:{lane:"jwt",jwt:u}};let d=(t.URUN_API_KEY??"").trim();if(d){let l=(t.URUN_GATEWAY_URL??"").trim(),p=l?jt(l):void 0;return{baseUrl:e,orgId:n,appId:r,fnName:o,apiUrl:i,decisionSecret:a,auth:{lane:"api-key",apiKey:d,gatewayUrl:p}}}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 Ha(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 jt(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 Dn(t){return{app:t.appId,org:t.orgId,fn:t.fnName,base_url:t.baseUrl,proxy_version:qa}}function Ga(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 Va=250;async function Fn(t,e=Va){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:a,proxy_version:u}=r;return typeof o!="string"||typeof s!="string"||typeof i!="string"||typeof a!="string"||typeof u!="string"?null:{app:o,org:s,fn:i,base_url:a,proxy_version:u}}catch{return null}}var Zo=2e3;async function Ja(t,e=Zo){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 es(t,e,n=Go,r=Zo){if(t!=null)return{mode:"spawn",port:t};let o=await Fn(n);if(o!==null&&Ga(o,e)){let s=await Ja(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 Bn(t,e=Bo.default.homedir()){return jo.default.join(e,".urun","logs",`compat-proxy-${t}.log`)}var Ln=["log","info","warn","error","debug","trace"];function ts(t,e=process,n=console){let r=e.stdout.write,o=e.stderr.write,s=(a,...u)=>t.write(a,...u);e.stdout.write=s,e.stderr.write=s;let i=Object.fromEntries(Ln.map(a=>[a,n[a]]));for(let a of Ln)n[a]=(...u)=>{t.write(`${(0,Ho.format)(...u)}
18
- `)};return()=>{e.stdout.write=r,e.stderr.write=o;for(let a of Ln)n[a]=i[a]}}var Wa=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function ns(t){let e=t?.code;return typeof e=="string"&&Wa.has(e)}function de(t,e){try{return t.write(e),!0}catch(n){if(ns(n))return!1;throw n}}function rs(t){let e={stdout:!1,stderr:!1},n=[];for(let r of["stdout","stderr"]){let o=t.streams[r],s=i=>{ns(i)&&(e[r]=!0);let a=i?.code;try{t.note(`${new Date().toISOString()} urun compat: process.${r} died (${String(a??(i instanceof Error?i.message:i))}) \u2014 the terminal is gone; launcher output continues in this log only
19
- `)}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 os(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}
17
+ ${o}`)}function Do(t,e){let n=new Re.WebSocketServer({noServer:!0,maxPayload:ja}),r=new $e,o=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,u=!1,d=(l,p,m)=>{let c;try{c=new URL(l.url??"/","http://localhost")}catch{Ft(p,400,"malformed upgrade URL");return}if(p.on("error",()=>p.destroy()),p.on("close",()=>i.delete(p)),c.pathname!==Ba){i.add(p);return}s.add(p);let f=!1;(async()=>{if(e.apiKey&&(l.headers.authorization??"")!==`Bearer ${e.apiKey}`){Ft(p,401,"invalid local proxy api key");return}let g;try{g=await a(l)}catch(b){let y=e.statusOf?.(b)??null;Ft(p,y?.status??500,"upstream resolver rejected the connection");return}u||p.destroyed||(f=!0,n.handleUpgrade(l,p,m,b=>{s.delete(p),o.add(b),b.on("close",()=>o.delete(b));try{new Cn(b,g,r,e.statusOf)}catch{o.delete(b),b.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!f&&!p.destroyed&&Ft(p,500,"responses websocket upgrade failed")}).finally(()=>s.delete(p))};return t.on("upgrade",d),{stats:r,close:()=>{let{promise:l,resolve:p}=$n();if(u)return p(),l;u=!0,t.off("upgrade",d);for(let c of s)c.destroy();s.clear();for(let c of i)c.destroy();i.clear();for(let c of o)c.close(1001,"server shutting down");let m=setTimeout(()=>{for(let c of o)c.readyState!==Re.WebSocket.CLOSED&&c.terminate()},2e3);return m.unref?.(),n.close(()=>{clearTimeout(m),p()}),l}}}var ps=require("@urun-sh/core");var Fo=require("@urun-sh/core"),Wa={no_capacity:429,queue_timeout:429,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502},Ka={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"};function Mn(t,e){let n=Ka[t.code];return e?`${n} (${e})`:n}function Bt(t){if(t==null)return null;let e=Wa[t];return e===void 0?null:{status:e,code:t,retryable:e!==502,openaiType:e===429?"capacity_error":"upstream_error",anthropicType:e===429?"rate_limit_error":e===503?"overloaded_error":"api_error"}}function jt(t){if(t.kind==="create-failed")return Bt(t.status.error?.code??null);if(t.kind!=="never-live")return null;let e=(t.status.elapsedMs??0)>=Fo.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return Bt(e?"cold_start_exceeded":"queue_timeout")}var jo=Ie(require("os"),1),qo=Ie(require("path"),1),Ho=require("url"),Go=require("util");var Bo="0.6.19";var Vo=4141,Ya=Bo,Ht={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 Nn(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 Jo(t,e,n){let r={...t,...Nn(e,n)};return delete r.URUN_API_KEY,delete r.URUN_JWT,delete r.URUN_SESSION_TOKEN_SECRET,r}function Wo(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=")),a=n[0]==="exec",u=s.includes("--skip-git-repo-check"),d=a&&!u?["exec","--skip-git-repo-check",...n.slice(1)]:n;return[...r,...i?[]:["-m",e],...d]}function Ko(t,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--provider","urun","--model",t,...e]}function zo(t,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--model",`urun/${t}`,...e]}function Yo(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,Ho.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 Xo(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 Qo(t){if(t[0]!=="--port")return null;let e=Xo(t[1]);return t.splice(0,2),e}function Dn(t){let e=t.indexOf("--port");if(e===-1)return Vo;let n=Xo(t[e+1]);return t.splice(e,2),n}function Zo(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 Ln(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 Ke(t){let e=Xa(Ln(t,"URUN_BASE_URL")),n=Ln(t,"URUN_ORG_ID"),r=Ln(t,"URUN_APP"),o=(t.URUN_FUNCTION??"serve").trim()||"serve",s=(t.URUN_API_URL??"").trim(),i=s?qt(s,"URUN_API_URL"):Xr,a=(t.URUN_SESSION_TOKEN_SECRET??"").trim(),u=(t.URUN_JWT??"").trim();if(u)return{baseUrl:e,orgId:n,appId:r,fnName:o,apiUrl:i,decisionSecret:a,auth:{lane:"jwt",jwt:u}};let d=(t.URUN_API_KEY??"").trim();if(d){let l=(t.URUN_GATEWAY_URL??"").trim(),p=l?qt(l):void 0;return{baseUrl:e,orgId:n,appId:r,fnName:o,apiUrl:i,decisionSecret:a,auth:{lane:"api-key",apiKey:d,gatewayUrl:p}}}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 Xa(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 qt(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 Fn(t){return{app:t.appId,org:t.orgId,fn:t.fnName,base_url:t.baseUrl,proxy_version:Ya}}function Qa(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 Za=250;async function Bn(t,e=Za){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:a,proxy_version:u}=r;return typeof o!="string"||typeof s!="string"||typeof i!="string"||typeof a!="string"||typeof u!="string"?null:{app:o,org:s,fn:i,base_url:a,proxy_version:u}}catch{return null}}var es=2e3;async function eu(t,e=es){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 ts(t,e,n=Vo,r=es){if(t!=null)return{mode:"spawn",port:t};let o=await Bn(n);if(o!==null&&Qa(o,e)){let s=await eu(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 jn(t,e=jo.default.homedir()){return qo.default.join(e,".urun","logs",`compat-proxy-${t}.log`)}var Un=["log","info","warn","error","debug","trace"];function ns(t,e=process,n=console){let r=e.stdout.write,o=e.stderr.write,s=(a,...u)=>t.write(a,...u);e.stdout.write=s,e.stderr.write=s;let i=Object.fromEntries(Un.map(a=>[a,n[a]]));for(let a of Un)n[a]=(...u)=>{t.write(`${(0,Go.format)(...u)}
18
+ `)};return()=>{e.stdout.write=r,e.stderr.write=o;for(let a of Un)n[a]=i[a]}}var tu=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function rs(t){let e=t?.code;return typeof e=="string"&&tu.has(e)}function de(t,e){try{return t.write(e),!0}catch(n){if(rs(n))return!1;throw n}}function os(t){let e={stdout:!1,stderr:!1},n=[];for(let r of["stdout","stderr"]){let o=t.streams[r],s=i=>{rs(i)&&(e[r]=!0);let a=i?.code;try{t.note(`${new Date().toISOString()} urun compat: process.${r} died (${String(a??(i instanceof Error?i.message:i))}) \u2014 the terminal is gone; launcher output continues in this log only
19
+ `)}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 ss(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}
20
20
  `)}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}
21
21
  `)}catch(o){try{t.appendSync(t.logPath,`${new Date().toISOString()} urun compat: the crash line could not reach stderr (${String(o?.code??o)}) \u2014 the terminal is gone
22
- `)}catch{}}t.killChild(),t.exit(1)};return t.proc.on("uncaughtException",e),t.proc.on("unhandledRejection",e),()=>{t.proc.off("uncaughtException",e),t.proc.off("unhandledRejection",e)}}function ss(t,e){let n="pass --port 0 for an OS-assigned ephemeral port";return e?`port ${t} is held by another urun-openai proxy serving app ${e.app} (org ${e.org}, fn ${e.fn}, version ${e.proxy_version}) \u2014 reuse it by pointing your agent at http://127.0.0.1:${t}, or ${n}`:`port ${t} is already in use and does not answer the urun-openai /stats identity probe \u2014 ${n}`}var ps=require("ws");var ls=require("@urun-sh/core");var as=require("crypto");function E(t){return`${t}_${(0,as.randomUUID)().replace(/-/g,"").slice(0,24)}`}var Ht=4*1024*1024,jn=8*1024*1024,Gt=512,Vt=20*1024*1024;function H(t,e,n){return{type:"invalid_request_error",code:t,message:e,param:n??null}}function Ke(t,e){return{type:"server_error",code:t,message:e}}function D(t){return{type:"error",event_id:E("event"),error:t}}function qn(){return{outputModalities:["text"],turnDetection:null,inputAudioTranscription:!1}}var I=class extends Error{constructor(n,r,o="invalid_value"){super(n);this.param=r;this.code=o;this.name="SessionUpdateError"}param;code},Ka={type:!0,model:!0,instructions:!0,modalities:!0,voice:!0,input_audio_format:!0,output_audio_format:!0,temperature:!0,max_output_tokens:!0,max_response_output_tokens:!0,tool_choice:!0,tools:!0,metadata:!0,turn_detection:!0,input_audio_transcription:!0,input_audio_noise_reduction:!0,audio:!0},za={format:!0,transcription:!0,turn_detection:!0,noise_reduction:!0},Ya={format:!0,voice:!0,speed:!0};function is(t,e){let n=t;if(!n||typeof n!="object"||n.type!=="audio/pcm"||n.rate!==void 0&&n.rate!==24e3)throw new I(`${e} must be {"type":"audio/pcm","rate":24000} \u2014 the session audio lane is 24 kHz mono PCM16 only`,e)}function ut(t,e,n){if(!t||typeof t!="object"||Array.isArray(t))throw new I("session must be an object","session","invalid_request");let r=t,o=Object.keys(r).filter(i=>!(i in Ka));if(o.length>0)throw new I(`session field(s) not supported by this backend: ${o.join(", ")}`,o[0],"unsupported_field");let s={...e,outputModalities:[...e.outputModalities]};for(let[i,a]of Object.entries(r))switch(i){case"type":{if(a!=="realtime"&&a!=="transcription")throw new I(`session.type ${JSON.stringify(a)} is not "realtime" or "transcription"`,"type");if(e.type!==void 0&&e.type!==a)throw new I(`session.type cannot change once established \u2014 this session is ${JSON.stringify(e.type)} and the update names ${JSON.stringify(a)}: the type is fixed at establishment (the connection's URL intent, the first session.update that names it, or the first input_audio_buffer.append, which establishes realtime)`,"type","invalid_request");if(a==="transcription"&&!n.turns)throw new I('session.type "transcription" is refused: the resolved backend wires no turn/transcript lane (the stt stream behind the engine\u2019s own transcripts) \u2014 there is no transcription source on this session',"type","unsupported_field");s.type=a;break}case"model":if(a!==null&&typeof a!="string")throw new I("model must be a string or null","model");s.model=a??void 0;break;case"instructions":if(a!==null&&typeof a!="string")throw new I("instructions must be a string or null","instructions");s.instructions=a??void 0;break;case"modalities":{if(!Array.isArray(a)||a.length===0)throw new I("modalities must be a non-empty array","modalities");let u=[];for(let d of a){if(d!=="text"&&d!=="audio")throw new I(`modality ${JSON.stringify(d)} is not "text" or "audio"`,"modalities");if(d==="audio"&&!n.voice)throw new I('modality "audio" refused: the resolver did not provide an authoritative voice binding for the resolved backend \u2014 this model cannot speak on this session',"modalities","audio_not_supported");u.includes(d)||u.push(d)}s.outputModalities=u;break}case"voice":if(a===null){delete s.voice;break}if(typeof a!="string")throw new I("voice must be a string or null","voice");if(!n.voice)throw new I("voice refused: no authoritative voice binding for the resolved backend","voice","audio_not_supported");s.voice=a;break;case"input_audio_format":case"output_audio_format":if(a!=="pcm16")throw new I(`${i} must be "pcm16" \u2014 the session audio lane is 24 kHz mono PCM16 only`,i);break;case"temperature":if(a!==null&&(typeof a!="number"||!Number.isFinite(a)))throw new I("temperature must be a finite number or null","temperature");s.temperature=a??void 0;break;case"max_output_tokens":case"max_response_output_tokens":if(a!==null&&a!=="inf"&&(typeof a!="number"||!Number.isInteger(a)||a<1))throw new I(`${i} must be a positive integer, "inf" or null`,i);s.maxOutputTokens=a===null?void 0:a;break;case"tools":if(!Array.isArray(a))throw new I("tools must be an array","tools");s.tools=a;break;case"tool_choice":s.toolChoice=a;break;case"metadata":if(a!==null&&(!a||typeof a!="object"||Array.isArray(a)))throw new I("metadata must be an object or null","metadata");s.metadata=a??null;break;case"turn_detection":{if(a===null){s.turnDetection=null;break}if(!n.turns)throw new I("turn_detection is refused: the resolved backend does not wire its own turn boundary lane (stt stream with turn_start/turn_end) \u2014 there is no native VAD source to drive responses from. Drive turns manually: input_audio_buffer.commit + response.create.","turn_detection","unsupported_field");let u=a;if(!u||typeof u!="object"||Array.isArray(u))throw new I("turn_detection must be an object or null","turn_detection");let d=String(u.type??"");if(d!=="server_vad"&&d!=="semantic_vad")throw new I(`turn_detection.type ${JSON.stringify(u.type)} is not "server_vad"/"semantic_vad"`,"turn_detection.type");s.turnDetection={createResponse:u.create_response!==!1,interruptResponse:u.interrupt_response!==!1};break}case"input_audio_transcription":{if(a===null){s.inputAudioTranscription=!1;break}if(!n.turns)throw new I("input_audio_transcription is refused: the resolved backend does not wire a transcript lane \u2014 no native transcription source exists behind this session","input_audio_transcription","unsupported_field");if(!a||typeof a!="object"||Array.isArray(a))throw new I("input_audio_transcription must be an object or null","input_audio_transcription");for(let d of Object.keys(a))if(!["model","language","languages","prompt"].includes(d))throw new I(`input_audio_transcription field not honored by this backend: ${d} (transcription comes from the voice engine itself)`,`input_audio_transcription.${d}`,"unsupported_field");let u=a.model;if(u!=null){let d=typeof u=="string"?u:JSON.stringify(u);if(typeof u!="string"||u!==n.model)throw new I(`input_audio_transcription.model ${JSON.stringify(d)} does not match the model this connection is bound to (${n.model===void 0?"the unnamed default app":JSON.stringify(n.model)}) \u2014 a transcription session runs the engine the connection bound and cannot be retuned to another model mid-connection`,"input_audio_transcription.model","unsupported_field")}s.inputAudioTranscription=!0;break}case"input_audio_noise_reduction":if(a!==null)throw new I("input_audio_noise_reduction must be null on this backend: no noise-reduction primitive exists behind the session audio bridge","input_audio_noise_reduction","unsupported_field");break;case"audio":{if(a===null)break;let u=a;if(!u||typeof u!="object"||Array.isArray(u))throw new I("audio must be an object or null","audio");if(u.input!==void 0&&u.input!==null){let d=u.input;if(!d||typeof d!="object"||Array.isArray(d))throw new I("audio.input must be an object","audio.input");for(let l of Object.keys(d))if(!(l in za))throw new I(`audio.input field not supported by this backend: ${l}`,`audio.input.${l}`,"unsupported_field");if(d.format!==void 0&&is(d.format,"audio.input.format"),d.transcription!==null&&d.transcription!==void 0){let l=ut({input_audio_transcription:d.transcription},s,n);s.inputAudioTranscription=l.inputAudioTranscription}if(d.turn_detection!==null&&d.turn_detection!==void 0){let l=ut({turn_detection:d.turn_detection},s,n);s.turnDetection=l.turnDetection}if(d.noise_reduction!==null&&d.noise_reduction!==void 0)throw new I("audio.input.noise_reduction must be null on this backend: no noise-reduction primitive","audio.input.noise_reduction","unsupported_field")}if(u.output!==void 0&&u.output!==null){let d=u.output;if(!d||typeof d!="object"||Array.isArray(d))throw new I("audio.output must be an object","audio.output");for(let l of Object.keys(d))if(!(l in Ya))throw new I(`audio.output field not supported by this backend: ${l}`,`audio.output.${l}`,"unsupported_field");if(d.format!==void 0&&is(d.format,"audio.output.format"),d.speed!==void 0)throw new I("audio.output.speed is not supported on this backend: playback rate is fixed","audio.output.speed","unsupported_field");if(d.voice!==void 0)if(d.voice===null)delete s.voice;else{if(typeof d.voice!="string")throw new I("audio.output.voice must be a string or null","audio.output.voice");if(!n.voice)throw new I("audio.output.voice refused: no authoritative voice binding for the resolved backend","audio.output.voice","audio_not_supported");s.voice=d.voice}d.format!==void 0&&!s.outputModalities.includes("audio")&&(s.outputModalities=["text","audio"])}break}default:break}return s}function Jt(t,e){return{id:e,object:"realtime.session",type:t.type??"realtime",model:t.model??null,instructions:t.instructions??"",modalities:[...t.outputModalities],voice:t.voice??null,input_audio_format:"pcm16",output_audio_format:"pcm16",input_audio_transcription:t.inputAudioTranscription?{}:null,input_audio_noise_reduction:null,turn_detection:t.turnDetection?{type:"server_vad",create_response:t.turnDetection.createResponse,interrupt_response:t.turnDetection.interruptResponse}:null,temperature:t.temperature??null,max_response_output_tokens:t.maxOutputTokens??"inf",tool_choice:t.toolChoice??"auto",tools:t.tools??[],metadata:t.metadata??null}}function Hn(t,e){let n=t;if(!n||typeof n!="object")throw new I("item must be an object","item","invalid_request");if(n.type==="function_call_output"){if(typeof n.call_id!="string"||typeof n.output!="string")throw new I("function_call_output item requires string call_id and output","item.call_id","invalid_request");return{id:typeof n.id=="string"?n.id:E("item"),type:"function_call_output",callId:n.call_id,output:n.output}}if(n.type==="message"){let r=n.role;if(r!=="user"&&r!=="assistant"&&r!=="system")throw new I('message item role must be "user", "assistant" or "system"',"item.role","invalid_request");if(!Array.isArray(n.content))throw new I("message item content must be an array","item.content","invalid_request");let o=[];for(let s of n.content){let i=s;if(!i||typeof i!="object")throw new I("content part must be an object","item.content","invalid_request");if(i.type==="input_text"||r==="assistant"&&i.type==="output_text"){if(typeof i.text!="string")throw new I(`${String(i.type)} part requires string text`,"item.content.text","invalid_request");o.push({type:i.type,text:i.text});continue}if(i.type==="input_audio"){if(!e)throw new I("input_audio content refused: no authoritative voice binding for the resolved backend","item.content.type","audio_not_supported");if(typeof i.audio!="string")throw new I("input_audio part requires base64 audio","item.content.audio","invalid_request");o.push({type:"input_audio",...typeof i.transcript=="string"?{transcript:i.transcript}:{}});continue}throw new I(`content part type ${String(i.type)} is not supported on message items here (input_text / output_text / input_audio)`,"item.content.type","invalid_request")}return{id:typeof n.id=="string"?n.id:E("item"),type:"message",role:r,content:o}}throw new I(`item type ${String(n.type)} is not supported \u2014 message and function_call_output items only`,"item.type","invalid_request")}function Gn(t){let e=[],n=!1;for(let r of t){if(r.type==="function_call"){e.push({type:"function_call",call_id:r.callId,name:r.name,arguments:r.arguments??"{}"});continue}if(r.type==="function_call_output"){e.push({type:"function_call_output",call_id:r.callId,output:r.output});continue}let o=[];for(let s of r.content??[]){if(s.type==="input_text"||s.type==="output_text"){o.push({type:s.type,text:s.text});continue}if(s.type==="input_audio"){let i=s.transcript;if(typeof i=="string"&&i.trim().length>0){o.push({type:"input_text",text:i});continue}n=!0;continue}}e.push({role:r.role,content:o})}return{input:e,sawAudioOnlyItem:n}}var Xa=1001,Qa=1011,us=t=>t.slice(0,120),Za=/^[A-Za-z0-9+/=]*$/,eu=2;function tu(t){let e=Buffer.from(t,"base64");for(let n=0;n+1<e.byteLength;n+=2)if(Math.abs(e.readInt16LE(n))>eu)return!1;return!0}var dt=class{id=E("conn");ws;binding;onClosed;config=qn();sessionId=E("sess");conversationId=E("conv");items=[];pendingAudioBytes=[];pendingAudioTotalBytes=0;frozenAudio=[];inFlight=null;activeResponseId=null;responseFlushed=!1;cancelled=!1;voiceClaimed=!1;audioLaneOwner=!1;audioUnsubscribe=null;outOfTurnAudioWarned=!1;audioOnlyTranscriptGapWarned=!1;closed=!1;turnLaneAbort=null;turnLaneIterator=null;turnTranscript={open:!1,text:"",final:!1};lastCommittedTranscript=null;pendingTranscriptItemId=null;manuallyDischargedTurnItem=null;constructor(e){this.ws=e.ws,this.binding=e.binding,this.onClosed=e.onClosed,this.binding.model&&(this.config.model=this.binding.model),this.binding.intent==="transcription"&&(this.config.type="transcription"),this.ws.on("message",n=>{this.onMessage(n)}),this.ws.on("close",()=>this.teardown("client disconnected")),this.ws.on("error",()=>this.teardown("client socket error")),this.send({type:"session.created",event_id:E("event"),session:Jt(this.config,this.sessionId)}),this.send({type:"conversation.created",event_id:E("event"),conversation:{id:this.conversationId,object:"realtime.conversation"}}),this.binding.turns&&this.startTurnLane()}startTurnLane(){let e=new AbortController;this.turnLaneAbort=e;let n=this.binding.turns(e.signal)[Symbol.asyncIterator]();this.turnLaneIterator=n,(async()=>{try{for(;;){let r=await n.next();if(r.done||this.closed||e.signal.aborted)return;this.onTurnEvent(r.value)}}catch(r){!this.closed&&!e.signal.aborted&&this.send(D(Ke("turn_lane_fault",`the backend turn/transcript lane failed: ${r instanceof Error?r.message:String(r)}`)))}finally{this.turnLaneIterator===n&&(this.turnLaneIterator=null)}})()}isTranscriptionSession(){return this.binding.intent==="transcription"||this.config.type==="transcription"}transcriptionEventsOn(){return this.config.inputAudioTranscription||this.isTranscriptionSession()}completePendingTranscription(e){this.pendingTranscriptItemId=null;let n=this.turnTranscript.text.trim();this.turnTranscript={open:!1,text:"",final:!1},this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:e,content_index:0,transcript:n})}onTurnEvent(e){switch(e.kind){case"delta":case"final":{let n=typeof e.text=="string"?e.text:"",r=this.pendingTranscriptItemId;this.turnTranscript.open=!0,n.length>0&&(this.turnTranscript.text+=n),this.turnTranscript.final=e.kind==="final",this.transcriptionEventsOn()&&n.length>0&&this.send({type:"conversation.item.input_audio_transcription.delta",event_id:E("event"),item_id:r??"input_audio_turn",content_index:0,delta:n}),e.kind==="final"&&r!==null&&(this.completePendingTranscription(r),this.manuallyDischargedTurnItem=r);return}case"turn_start":{this.manuallyDischargedTurnItem=null,this.turnTranscript={open:!0,text:this.turnTranscript.text,final:!1},this.send({type:"input_audio_buffer.speech_started",event_id:E("event"),item_id:"input_audio_turn",audio_start_ms:typeof e.startMs=="number"?e.startMs:0}),this.config.turnDetection?.interruptResponse&&this.inFlight&&this.cancelResponse();return}case"turn_end":{let n=this.pendingTranscriptItemId;if(n!==null){this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:n,audio_end_ms:typeof e.startMs=="number"?e.startMs:0}),this.completePendingTranscription(n);return}let r=this.manuallyDischargedTurnItem;if(r!==null){this.manuallyDischargedTurnItem=null,this.turnTranscript={open:!1,text:"",final:!1},this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:r,audio_end_ms:typeof e.startMs=="number"?e.startMs:0});return}let o=this.turnTranscript.text.trim(),s=o.length>0?o:null;if(this.turnTranscript={open:!1,text:"",final:!1},this.lastCommittedTranscript=s,this.isTranscriptionSession()&&this.config.turnDetection===null){this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:"input_audio_turn",audio_end_ms:typeof e.startMs=="number"?e.startMs:0});return}this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let i=E("item"),a=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem({id:i,type:"message",role:"user",content:[{type:"input_audio",...s!==null?{transcript:s}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:E("event"),previous_item_id:a,item_id:i}),this.send({type:"conversation.item.created",event_id:E("event"),previous_item_id:a,item:this.itemToProtocol(this.items[this.items.length-1])}),this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:i,audio_end_ms:typeof e.startMs=="number"?e.startMs:0}),this.transcriptionEventsOn()&&s!==null&&this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:i,content_index:0,transcript:s}),this.config.turnDetection?.createResponse&&!this.inFlight&&!this.isTranscriptionSession()&&this.startResponse({});return}case"turn_eager_end":case"turn_resumed":return;default:return}}takeTurnTranscript(){let e=this.lastCommittedTranscript;return this.lastCommittedTranscript=null,e}async onMessage(e){if(this.closed)return;let n;try{n=e.toString()}catch{this.fail(H("decode_error","frame is not decodable text"),"invalid_frame");return}if(Buffer.byteLength(n,"utf8")>Ht){this.fail(H("frame_too_large",`frame exceeds the ${Ht}-byte limit`),"invalid_frame");return}let r;try{r=JSON.parse(n)}catch{this.fail(H("invalid_json","frame is not valid JSON"),"invalid_frame");return}if(typeof r.type!="string"||r.type.length===0){this.fail(H("missing_type",'event is missing a string "type"'),"invalid_frame");return}try{await this.handle(r)}catch(o){let s=o;if(s&&typeof s=="object"&&s.name==="SessionUpdateError"){this.send(D({type:"invalid_request_error",code:s.code??"invalid_value",message:s.message,param:s.param??null}));return}this.fail(Ke(null,o instanceof Error?o.message:String(o)),"handler_fault")}}async handle(e){switch(e.type){case"session.update":this.config=ut(e.session,this.config,{voice:this.binding.voice,turns:this.binding.turns!==void 0,model:this.binding.model}),this.send({type:"session.updated",event_id:E("event"),session:Jt(this.config,this.sessionId)});return;case"conversation.item.create":{let n=Hn(e.item,this.binding.voice),r=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem(n),this.send({type:"conversation.item.created",event_id:E("event"),previous_item_id:r,item:this.itemToProtocol(n)});return}case"conversation.item.delete":{let n=this.items.findIndex(r=>r.id===e.item_id);if(n<0){this.send(D(H("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.items.splice(n,1),this.send({type:"conversation.item.deleted",event_id:E("event"),item_id:e.item_id});return}case"conversation.item.retrieve":{let n=this.items.find(r=>r.id===e.item_id);if(!n){this.send(D(H("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.send({type:"conversation.item.retrieved",event_id:E("event"),item:this.itemToProtocol(n)});return}case"conversation.item.truncate":{let n=this.items.find(r=>r.id===e.item_id);if(!n||n.type!=="message"||n.role!=="assistant"){this.send(D(H("item_not_found",`no assistant message item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}n.truncated=!0,this.send({type:"conversation.item.truncated",event_id:E("event"),item_id:n.id,content_index:typeof e.content_index=="number"?e.content_index:0,audio_end_ms:typeof e.audio_end_ms=="number"?e.audio_end_ms:0});return}case"input_audio_buffer.append":{if(typeof e.audio!="string"||e.audio.length===0){this.send(D(H("invalid_audio",'input_audio_buffer.append requires base64 "audio"',"audio")));return}if(!this.ensureVoice())return;if(!Za.test(e.audio)){this.send(D(H("invalid_audio","input_audio_buffer.append audio must be base64","audio")));return}let n=Buffer.byteLength(e.audio,"base64");if(this.pendingAudioTotalBytes+n>Vt){this.send(D(H("buffer_full",`uncommitted input audio exceeds the ${Vt}-byte bound \u2014 commit or clear first`,"audio")));return}if(this.config.type===void 0&&(this.config.type="realtime"),this.inFlight){this.binding.audioSession.audio.appendInputAudio(e.audio);return}if(this.isTranscriptionSession()){this.binding.audioSession.audio.appendInputAudio(e.audio),this.pendingAudioTotalBytes+=n;return}this.pendingAudioBytes.push(e.audio),this.pendingAudioTotalBytes+=n;return}case"input_audio_buffer.clear":{if(!this.ensureVoice())return;this.pendingAudioBytes.length=0,this.pendingAudioTotalBytes=0,this.lastCommittedTranscript=null,this.pendingTranscriptItemId===null&&(this.turnTranscript={open:!1,text:"",final:!1}),this.send({type:"input_audio_buffer.cleared",event_id:E("event")});return}case"input_audio_buffer.commit":{if(!this.ensureVoice())return;if(this.pendingAudioTotalBytes===0&&this.takeTurnTranscript()===null){this.send(D(H("empty_buffer","input_audio_buffer.commit with no buffered audio","audio")));return}this.isTranscriptionSession()&&this.pendingTranscriptItemId!==null&&this.completePendingTranscription(this.pendingTranscriptItemId),this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let n=this.takeTurnTranscript();n===null&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let r=E("item"),o=this.items.length>0?this.items[this.items.length-1].id:null;if(this.pushItem({id:r,type:"message",role:"user",content:[{type:"input_audio",...n!==null?{transcript:n}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:E("event"),previous_item_id:o,item_id:r}),this.send({type:"conversation.item.created",event_id:E("event"),previous_item_id:o,item:this.itemToProtocol(this.items[this.items.length-1])}),this.isTranscriptionSession())if(n!==null)this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:r,content_index:0,transcript:n});else if(this.turnTranscript.final){let s=this.turnTranscript.text.trim();this.turnTranscript={open:!1,text:"",final:!1},this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:r,content_index:0,transcript:s}),this.manuallyDischargedTurnItem=r}else this.pendingTranscriptItemId=r;return}case"output_audio_buffer.clear":{this.send(D(H("output_clear_unavailable","output_audio_buffer.clear has no server-side playback queue to flush on this adapter: output audio is delivered as response.output_audio.delta events and interrupted turns are stopped with response.cancel")));return}case"response.create":if(this.isTranscriptionSession()){this.send(D(H("unsupported","response.create is not supported on a transcription session \u2014 this session transcribes the streamed audio with the engine's own transcript lane and emits conversation.item.input_audio_transcription events; there are no model responses to create","response.create")));return}this.startResponse(e.response??{});return;case"response.cancel":this.cancelResponse();return;default:this.send(D(H("unknown_event",`event type ${e.type} is not supported by this adapter`)))}}ensureVoice(){return!this.binding.voice||!this.binding.audioSession?(this.send(D(H("audio_not_supported","audio events require a voice binding from the resolver \u2014 this session was resolved text-only","audio"))),!1):(this.voiceClaimed||(this.claimVoiceBinding(),this.audioUnsubscribe=this.binding.audioSession.audio.onOutputAudio(e=>{this.onOutputAudioFrame(this.activeResponseId??"",e)}),this.voiceClaimed=!0),!0)}claimVoiceBinding(){let e=this.binding.audioSession,n=e.openaiRealtimeClaims??(e.openaiRealtimeClaims=new Set);if(n.has(e))throw new Error("voice binding already owned by another connection \u2014 the resolver handed one audio bridge to two realtime conversations, which must never happen (independent callers require private bindings)");n.add(e),this.audioLaneOwner=!0}releaseVoiceBinding(){let e=this.binding.audioSession;!e||!this.audioLaneOwner||(e.openaiRealtimeClaims?.delete(e),this.audioLaneOwner=!1)}warnAudioOnlyTranscriptGap(){this.audioOnlyTranscriptGapWarned||(this.audioOnlyTranscriptGapWarned=!0,this.send(D({type:"invalid_request_error",code:"audio_input_transcript_unavailable",message:"committed input audio is forwarded to the runtime audio lane but is NOT model-visible in the text turn lane: the resolved backend wires no turn/transcript lane (the voice engine's own stt stream with turn_start/turn_end + transcripts). Wire the transcribe/turn-detection leg upstream (the resolver supplies binding.turns) and committed audio gains its REAL transcript as the model-visible turn.",param:"input_audio_buffer.commit"})))}async upstreamStream(e,n){let r=this.binding.session;if(r&&typeof r.sendResponseCreate=="function"){let s=e.response??e;return r.sendResponseCreate({type:"response.create",response:s},e.input??[],n)}let o=this.binding.clients;if(o&&typeof o.createResponse=="function"){let{response:s,input:i,...a}=e;return await o.createResponse({...a,input:i})}throw new Error("realtime binding provides neither session.sendResponseCreate nor clients.createResponse \u2014 no upstream turn seam")}startResponse(e){if(this.inFlight){this.send(D(H("response_in_progress","a response is already in progress \u2014 cancel it with response.cancel first","response.create")));return}let n=e??{};if(n.modalities!==void 0){if(!Array.isArray(n.modalities)||n.modalities.some(g=>g!=="text"&&g!=="audio")){this.send(D(H("invalid_modalities",'response.modalities must be "text"/"audio"',"response.modalities")));return}if(n.modalities.includes("audio")&&!this.ensureVoice())return}if(n.input!==void 0){this.send(D(H("response_input_unsupported","response.input is not supported by this adapter: build the context with conversation.item.create, then response.create","response.input")));return}this.cancelled=!1,this.responseFlushed=!1,this.messageItemAdded=!1;let r=E("req"),o=`resp_${r}`;this.activeResponseId=o;let s=this.items.length,{input:i,sawAudioOnlyItem:a}=Gn(this.items),u=typeof n.instructions=="string"?n.instructions:this.config.instructions,d=typeof n.temperature=="number"?n.temperature:this.config.temperature,l=n.max_output_tokens??this.config.maxOutputTokens,p=l==="inf"||l===void 0?void 0:l,m=n.tools!==void 0?n.tools:this.config.tools,c=n.tool_choice!==void 0?n.tool_choice:this.config.toolChoice,f={input:i,stream:!0,...this.config.model?{model:this.config.model}:{},...u!==void 0?{instructions:u}:{},...d!==void 0?{temperature:d}:{},...p!==void 0?{max_output_tokens:p}:{},...m!==void 0?{tools:m}:{},...c!==void 0?{tool_choice:c}:{}};for(let g of this.frozenAudio.splice(0))this.binding.audioSession?.audio.appendInputAudio(g);this.inFlight=(async()=>{let g=[],_=A=>{this.responseFlushed?this.send(A):g.push(A)},y=()=>{if(!this.responseFlushed){this.send({type:"response.created",event_id:E("event"),response:this.responseObject(o,"in_progress")}),this.send({type:"response.in_progress",event_id:E("event"),response:this.responseObject(o,"in_progress")});for(let A of g)this.send(A);g.length=0,this.responseFlushed=!0}},h={messageOpen:!1,messageText:"",tools:new Map,nextOutputIndex:0},w=null;try{a&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let A;try{A=await this.upstreamStream(f,r)}catch(v){this.upstreamTurnFault(v,o);return}try{for await(let v of A){if(this.cancelled)break;let b=v;if(b.type==="error"){let R=this.upstreamErrorBody(b);this.responseFlushed?this.send(D(R)):this.send({type:"response.failed",event_id:E("event"),response:this.failedResponse(o,R)});return}if(b.type==="response.output_text.delta"&&typeof b.delta=="string"){y(),h.messageOpen||(this.beginMessageItem(o,h.nextOutputIndex++),h.messageOpen=!0),_({type:"response.output_text.delta",event_id:E("event"),item_id:`${o}_msg_0`,response_id:o,output_index:0,content_index:0,delta:b.delta}),h.messageText+=b.delta;continue}if(b.type==="response.function_call_arguments.delta"){y();let R=typeof b.delta=="string"?b.delta:"",L=typeof b.call_id=="string"&&b.call_id.length>0?b.call_id:`${o}_fc_${h.nextOutputIndex}`,S=h.tools.get(L);if(!S){let x=typeof b.name=="string"&&b.name.length>0?b.name:void 0;S={outputIndex:h.nextOutputIndex++,name:x,args:""},h.tools.set(L,S),_({type:"response.output_item.added",event_id:E("event"),response_id:o,output_index:S.outputIndex,item:{id:L,object:"realtime.item",type:"function_call",call_id:L,name:x??"",arguments:"",status:"in_progress"}})}typeof b.name=="string"&&b.name.length>0&&(S.name=b.name),S.args+=R,_({type:"response.function_call_arguments.delta",event_id:E("event"),item_id:L,response_id:o,output_index:S.outputIndex,call_id:L,name:S.name??"",delta:R});continue}if(b.type==="response.completed"){w=b.response??null;continue}}}catch(v){this.upstreamTurnFault(v,o);return}if(this.cancelled)return;this.closeOpenItems(o,h,s),y(),this.send({type:"response.done",event_id:E("event"),response:this.doneResponse(o,s,w)})}finally{this.inFlight=null,this.activeResponseId=null}})()}onOutputAudioFrame(e,n){if(this.cancelled)return;if(this.activeResponseId!==e){if(tu(n))return;!this.outOfTurnAudioWarned&&!this.closed&&(this.outOfTurnAudioWarned=!0,this.send(D({type:"invalid_request_error",code:"out_of_turn_audio",message:"the runtime produced voice OUT outside a response turn; this adapter delivers output audio only within response.output_audio.delta of an open turn \u2014 wire server-driven turns (VAD) upstream or keep speech inside response turns"})));return}this.responseFlushed||(this.send({type:"response.created",event_id:E("event"),response:this.responseObject(e,"in_progress")}),this.send({type:"response.in_progress",event_id:E("event"),response:this.responseObject(e,"in_progress")}),this.responseFlushed=!0);let r=`${e}_msg_0`;this.messageItemOpenForAudio||(this.beginAudioMessageItem(e),this.messageItemOpenForAudio=!0),this.send({type:"response.output_audio.delta",event_id:E("event"),item_id:r,response_id:e,output_index:0,content_index:0,delta:n})}messageItemAdded=!1;messageItemOpenForAudio=!1;beginMessageItem(e,n){let r=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:E("event"),response_id:e,output_index:n,item:{id:r,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:E("event"),item_id:r,response_id:e,output_index:n,content_index:0,part:{type:"output_text",text:""}})}beginAudioMessageItem(e){let n=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:E("event"),response_id:e,output_index:0,item:{id:n,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:E("event"),item_id:n,response_id:e,output_index:0,content_index:0,part:{type:"output_audio",transcript:""}})}closeOpenItems(e,n,r){if(n.messageOpen||this.messageItemOpenForAudio){let o=`${e}_msg_0`,s=this.messageItemOpenForAudio;s&&this.send({type:"response.output_audio.done",event_id:E("event"),item_id:o,response_id:e,output_index:0,content_index:0}),n.messageText.length>0&&this.send({type:"response.output_text.done",event_id:E("event"),item_id:o,response_id:e,output_index:0,content_index:0,text:n.messageText}),this.send({type:"response.content_part.done",event_id:E("event"),item_id:o,response_id:e,output_index:0,content_index:0,part:s?{type:"output_audio",transcript:""}:{type:"output_text",text:n.messageText}});let i=[];s&&i.push({type:"output_audio"}),n.messageText.length>0&&i.push({type:"output_text",text:n.messageText});let a={id:o,type:"message",role:"assistant",content:i};this.pushItem(a),this.send({type:"response.output_item.done",event_id:E("event"),response_id:e,output_index:0,item:this.itemToProtocol(a)})}for(let[o,s]of n.tools){let i=s.args.length>0?s.args:"{}";this.send({type:"response.function_call_arguments.done",event_id:E("event"),item_id:o,response_id:e,output_index:s.outputIndex,call_id:o,name:s.name??"",arguments:i});let a={id:o,type:"function_call",callId:o,name:s.name,arguments:i};this.pushItem(a),this.send({type:"response.output_item.done",event_id:E("event"),response_id:e,output_index:s.outputIndex,item:this.itemToProtocol(a)})}this.messageItemOpenForAudio=!1,this.messageItemAdded=!1}responseObject(e,n,r){return{id:e,object:"realtime.response",status:n,conversation_id:this.conversationId,usage:null,...r??{}}}failedResponse(e,n){return this.responseObject(e,"failed",{status_details:{error:{code:n.code,message:n.message}}})}doneResponse(e,n,r){let o=r&&typeof r.usage=="object"&&r.usage!==null?r.usage:null,s=[];for(let i=n;i<this.items.length;i++)s.push(this.itemToProtocol(this.items[i]));return this.responseObject(e,"completed",{output:s,usage:o?{input_tokens:typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0,output_tokens:typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0,total_tokens:typeof o.total_tokens=="number"&&Number.isFinite(o.total_tokens)?o.total_tokens:(typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0)+(typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0),input_token_details:{cached_tokens:0,audio_tokens:0,text_tokens:0},output_token_details:{audio_tokens:0,text_tokens:0}}:null})}upstreamErrorBody(e){let n=e.error,r=n&&typeof n.message=="string"?n.message:JSON.stringify(e);return{type:"server_error",code:n&&typeof n.code=="string"?n.code:null,message:r}}failSessionGone(e){let n=e instanceof Error?e.message:String(e);this.fail(Ke("session_gone",`this realtime connection's pooled session \u2014 the one its audio bridge and every turn ride \u2014 is gone: ${n}`),"session_gone")}upstreamTurnFault(e,n){if(e instanceof X){this.failSessionGone(e);return}if(!this.cancelled){if(!this.responseFlushed&&!(e instanceof ls.SessionFailedError&&(e.kind==="never-live"||e.kind==="create-failed"))){this.failSessionGone(e);return}this.emitUpstreamFault(e,n)}}emitUpstreamFault(e,n){let r=Ke(null,e instanceof Error?e.message:String(e));this.responseFlushed?this.send(D(r)):this.send({type:"response.failed",event_id:E("event"),response:this.failedResponse(n,r)})}cancelResponse(){let e=this.inFlight;if(!e){this.send(D(H("no_response_to_cancel","no response is in progress","response.cancel")));return}this.cancelled=!0;let n=this.activeResponseId??"resp_";e.finally(()=>{this.send({type:"response.cancelled",event_id:E("event"),response:this.responseObject(n,"cancelled")})})}pushItem(e){this.items.push(e),this.items.length>Gt&&(this.items.shift(),this.send(D({type:"invalid_request_error",code:"transcript_evicted",message:`the conversation transcript exceeded ${Gt} items \u2014 the oldest item was evicted to keep the per-connection bound`})))}itemToProtocol(e){let n={id:e.id,object:"realtime.item"};if(e.type==="function_call")return{...n,type:"function_call",call_id:e.callId,name:e.name,arguments:e.arguments??"{}",status:"completed"};if(e.type==="function_call_output")return{...n,type:"function_call_output",call_id:e.callId,output:e.output};let r=(e.content??[]).map(o=>o.type==="input_text"||o.type==="output_text"?{type:o.type,text:o.text}:o.type==="input_audio"?{type:"input_audio",...o.transcript?{transcript:o.transcript}:{}}:{type:"output_audio",transcript:o.transcript??""});return{...n,type:"message",role:e.role,status:e.truncated?"incomplete":"completed",content:r}}send(e){if(!(this.closed||this.ws.readyState!==this.ws.OPEN)){if(this.ws.bufferedAmount>jn){this.ws.close(Xa,us("outbound buffer exceeded")),this.teardown("outbound buffer exceeded");return}this.ws.send(JSON.stringify(e))}}fail(e,n){this.send(D(e)),this.ws.close(Qa,us(n)),this.teardown(n)}teardown(e){if(this.closed)return;this.closed=!0,this.turnLaneAbort?.abort(new Error("the realtime connection closed \u2014 the turn lane consumer is gone")),this.turnLaneAbort=null;let n=this.turnLaneIterator;if(this.turnLaneIterator=null,n!==null){let r=n.return?.();r&&r.catch(()=>{})}this.releaseVoiceBinding(),this.audioUnsubscribe?.(),this.audioUnsubscribe=null,this.binding.release&&Promise.resolve().then(()=>this.binding.release?.()).catch(r=>ds(this.id,`binding release failed: ${r instanceof Error?r.message:String(r)}`));try{this.ws.removeAllListeners("message")}catch{}this.onClosed?.(this,{reason:e}),ds(this.id,e)}},Vn=[];function ds(t,e){Vn.push({id:t,reason:e}),Vn.length>256&&Vn.shift()}var Wn="/v1/realtime",nu=t=>t.slice(0,120);function ru(t,e,n){let r=e===401||e===403?"Unauthorized":e===502?"Upstream Fault":"Upgrade Refused";t.write(`HTTP/1.1 ${e} ${r}\r
22
+ `)}catch{}}t.killChild(),t.exit(1)};return t.proc.on("uncaughtException",e),t.proc.on("unhandledRejection",e),()=>{t.proc.off("uncaughtException",e),t.proc.off("unhandledRejection",e)}}function is(t,e){let n="pass --port 0 for an OS-assigned ephemeral port";return e?`port ${t} is held by another urun-openai proxy serving app ${e.app} (org ${e.org}, fn ${e.fn}, version ${e.proxy_version}) \u2014 reuse it by pointing your agent at http://127.0.0.1:${t}, or ${n}`:`port ${t} is already in use and does not answer the urun-openai /stats identity probe \u2014 ${n}`}var fs=require("ws");var cs=require("@urun-sh/core");var us=require("crypto");function E(t){return`${t}_${(0,us.randomUUID)().replace(/-/g,"").slice(0,24)}`}var Gt=4*1024*1024,qn=8*1024*1024,Vt=512,Jt=20*1024*1024;function H(t,e,n){return{type:"invalid_request_error",code:t,message:e,param:n??null}}function ze(t,e){return{type:"server_error",code:t,message:e}}function D(t){return{type:"error",event_id:E("event"),error:t}}function Hn(){return{outputModalities:["text"],turnDetection:null,inputAudioTranscription:!1}}var I=class extends Error{constructor(n,r,o="invalid_value"){super(n);this.param=r;this.code=o;this.name="SessionUpdateError"}param;code},nu={type:!0,model:!0,instructions:!0,modalities:!0,voice:!0,input_audio_format:!0,output_audio_format:!0,temperature:!0,max_output_tokens:!0,max_response_output_tokens:!0,tool_choice:!0,tools:!0,metadata:!0,turn_detection:!0,input_audio_transcription:!0,input_audio_noise_reduction:!0,audio:!0},ru={format:!0,transcription:!0,turn_detection:!0,noise_reduction:!0},ou={format:!0,voice:!0,speed:!0};function as(t,e){let n=t;if(!n||typeof n!="object"||n.type!=="audio/pcm"||n.rate!==void 0&&n.rate!==24e3)throw new I(`${e} must be {"type":"audio/pcm","rate":24000} \u2014 the session audio lane is 24 kHz mono PCM16 only`,e)}function lt(t,e,n){if(!t||typeof t!="object"||Array.isArray(t))throw new I("session must be an object","session","invalid_request");let r=t,o=Object.keys(r).filter(i=>!(i in nu));if(o.length>0)throw new I(`session field(s) not supported by this backend: ${o.join(", ")}`,o[0],"unsupported_field");let s={...e,outputModalities:[...e.outputModalities]};for(let[i,a]of Object.entries(r))switch(i){case"type":{if(a!=="realtime"&&a!=="transcription")throw new I(`session.type ${JSON.stringify(a)} is not "realtime" or "transcription"`,"type");if(e.type!==void 0&&e.type!==a)throw new I(`session.type cannot change once established \u2014 this session is ${JSON.stringify(e.type)} and the update names ${JSON.stringify(a)}: the type is fixed at establishment (the connection's URL intent, the first session.update that names it, or the first input_audio_buffer.append, which establishes realtime)`,"type","invalid_request");if(a==="transcription"&&!n.turns)throw new I('session.type "transcription" is refused: the resolved backend wires no turn/transcript lane (the stt stream behind the engine\u2019s own transcripts) \u2014 there is no transcription source on this session',"type","unsupported_field");s.type=a;break}case"model":if(a!==null&&typeof a!="string")throw new I("model must be a string or null","model");s.model=a??void 0;break;case"instructions":if(a!==null&&typeof a!="string")throw new I("instructions must be a string or null","instructions");s.instructions=a??void 0;break;case"modalities":{if(!Array.isArray(a)||a.length===0)throw new I("modalities must be a non-empty array","modalities");let u=[];for(let d of a){if(d!=="text"&&d!=="audio")throw new I(`modality ${JSON.stringify(d)} is not "text" or "audio"`,"modalities");if(d==="audio"&&!n.voice)throw new I('modality "audio" refused: the resolver did not provide an authoritative voice binding for the resolved backend \u2014 this model cannot speak on this session',"modalities","audio_not_supported");u.includes(d)||u.push(d)}s.outputModalities=u;break}case"voice":if(a===null){delete s.voice;break}if(typeof a!="string")throw new I("voice must be a string or null","voice");if(!n.voice)throw new I("voice refused: no authoritative voice binding for the resolved backend","voice","audio_not_supported");s.voice=a;break;case"input_audio_format":case"output_audio_format":if(a!=="pcm16")throw new I(`${i} must be "pcm16" \u2014 the session audio lane is 24 kHz mono PCM16 only`,i);break;case"temperature":if(a!==null&&(typeof a!="number"||!Number.isFinite(a)))throw new I("temperature must be a finite number or null","temperature");s.temperature=a??void 0;break;case"max_output_tokens":case"max_response_output_tokens":if(a!==null&&a!=="inf"&&(typeof a!="number"||!Number.isInteger(a)||a<1))throw new I(`${i} must be a positive integer, "inf" or null`,i);s.maxOutputTokens=a===null?void 0:a;break;case"tools":if(!Array.isArray(a))throw new I("tools must be an array","tools");s.tools=a;break;case"tool_choice":s.toolChoice=a;break;case"metadata":if(a!==null&&(!a||typeof a!="object"||Array.isArray(a)))throw new I("metadata must be an object or null","metadata");s.metadata=a??null;break;case"turn_detection":{if(a===null){s.turnDetection=null;break}if(!n.turns)throw new I("turn_detection is refused: the resolved backend does not wire its own turn boundary lane (stt stream with turn_start/turn_end) \u2014 there is no native VAD source to drive responses from. Drive turns manually: input_audio_buffer.commit + response.create.","turn_detection","unsupported_field");let u=a;if(!u||typeof u!="object"||Array.isArray(u))throw new I("turn_detection must be an object or null","turn_detection");let d=String(u.type??"");if(d!=="server_vad"&&d!=="semantic_vad")throw new I(`turn_detection.type ${JSON.stringify(u.type)} is not "server_vad"/"semantic_vad"`,"turn_detection.type");s.turnDetection={createResponse:u.create_response!==!1,interruptResponse:u.interrupt_response!==!1};break}case"input_audio_transcription":{if(a===null){s.inputAudioTranscription=!1;break}if(!n.turns)throw new I("input_audio_transcription is refused: the resolved backend does not wire a transcript lane \u2014 no native transcription source exists behind this session","input_audio_transcription","unsupported_field");if(!a||typeof a!="object"||Array.isArray(a))throw new I("input_audio_transcription must be an object or null","input_audio_transcription");for(let d of Object.keys(a))if(!["model","language","languages","prompt"].includes(d))throw new I(`input_audio_transcription field not honored by this backend: ${d} (transcription comes from the voice engine itself)`,`input_audio_transcription.${d}`,"unsupported_field");let u=a.model;if(u!=null){let d=typeof u=="string"?u:JSON.stringify(u);if(typeof u!="string"||u!==n.model)throw new I(`input_audio_transcription.model ${JSON.stringify(d)} does not match the model this connection is bound to (${n.model===void 0?"the unnamed default app":JSON.stringify(n.model)}) \u2014 a transcription session runs the engine the connection bound and cannot be retuned to another model mid-connection`,"input_audio_transcription.model","unsupported_field")}s.inputAudioTranscription=!0;break}case"input_audio_noise_reduction":if(a!==null)throw new I("input_audio_noise_reduction must be null on this backend: no noise-reduction primitive exists behind the session audio bridge","input_audio_noise_reduction","unsupported_field");break;case"audio":{if(a===null)break;let u=a;if(!u||typeof u!="object"||Array.isArray(u))throw new I("audio must be an object or null","audio");if(u.input!==void 0&&u.input!==null){let d=u.input;if(!d||typeof d!="object"||Array.isArray(d))throw new I("audio.input must be an object","audio.input");for(let l of Object.keys(d))if(!(l in ru))throw new I(`audio.input field not supported by this backend: ${l}`,`audio.input.${l}`,"unsupported_field");if(d.format!==void 0&&as(d.format,"audio.input.format"),d.transcription!==null&&d.transcription!==void 0){let l=lt({input_audio_transcription:d.transcription},s,n);s.inputAudioTranscription=l.inputAudioTranscription}if(d.turn_detection!==null&&d.turn_detection!==void 0){let l=lt({turn_detection:d.turn_detection},s,n);s.turnDetection=l.turnDetection}if(d.noise_reduction!==null&&d.noise_reduction!==void 0)throw new I("audio.input.noise_reduction must be null on this backend: no noise-reduction primitive","audio.input.noise_reduction","unsupported_field")}if(u.output!==void 0&&u.output!==null){let d=u.output;if(!d||typeof d!="object"||Array.isArray(d))throw new I("audio.output must be an object","audio.output");for(let l of Object.keys(d))if(!(l in ou))throw new I(`audio.output field not supported by this backend: ${l}`,`audio.output.${l}`,"unsupported_field");if(d.format!==void 0&&as(d.format,"audio.output.format"),d.speed!==void 0)throw new I("audio.output.speed is not supported on this backend: playback rate is fixed","audio.output.speed","unsupported_field");if(d.voice!==void 0)if(d.voice===null)delete s.voice;else{if(typeof d.voice!="string")throw new I("audio.output.voice must be a string or null","audio.output.voice");if(!n.voice)throw new I("audio.output.voice refused: no authoritative voice binding for the resolved backend","audio.output.voice","audio_not_supported");s.voice=d.voice}d.format!==void 0&&!s.outputModalities.includes("audio")&&(s.outputModalities=["text","audio"])}break}default:break}return s}function Wt(t,e){return{id:e,object:"realtime.session",type:t.type??"realtime",model:t.model??null,instructions:t.instructions??"",modalities:[...t.outputModalities],voice:t.voice??null,input_audio_format:"pcm16",output_audio_format:"pcm16",input_audio_transcription:t.inputAudioTranscription?{}:null,input_audio_noise_reduction:null,turn_detection:t.turnDetection?{type:"server_vad",create_response:t.turnDetection.createResponse,interrupt_response:t.turnDetection.interruptResponse}:null,temperature:t.temperature??null,max_response_output_tokens:t.maxOutputTokens??"inf",tool_choice:t.toolChoice??"auto",tools:t.tools??[],metadata:t.metadata??null}}function Gn(t,e){let n=t;if(!n||typeof n!="object")throw new I("item must be an object","item","invalid_request");if(n.type==="function_call_output"){if(typeof n.call_id!="string"||typeof n.output!="string")throw new I("function_call_output item requires string call_id and output","item.call_id","invalid_request");return{id:typeof n.id=="string"?n.id:E("item"),type:"function_call_output",callId:n.call_id,output:n.output}}if(n.type==="message"){let r=n.role;if(r!=="user"&&r!=="assistant"&&r!=="system")throw new I('message item role must be "user", "assistant" or "system"',"item.role","invalid_request");if(!Array.isArray(n.content))throw new I("message item content must be an array","item.content","invalid_request");let o=[];for(let s of n.content){let i=s;if(!i||typeof i!="object")throw new I("content part must be an object","item.content","invalid_request");if(i.type==="input_text"||r==="assistant"&&i.type==="output_text"){if(typeof i.text!="string")throw new I(`${String(i.type)} part requires string text`,"item.content.text","invalid_request");o.push({type:i.type,text:i.text});continue}if(i.type==="input_audio"){if(!e)throw new I("input_audio content refused: no authoritative voice binding for the resolved backend","item.content.type","audio_not_supported");if(typeof i.audio!="string")throw new I("input_audio part requires base64 audio","item.content.audio","invalid_request");o.push({type:"input_audio",...typeof i.transcript=="string"?{transcript:i.transcript}:{}});continue}throw new I(`content part type ${String(i.type)} is not supported on message items here (input_text / output_text / input_audio)`,"item.content.type","invalid_request")}return{id:typeof n.id=="string"?n.id:E("item"),type:"message",role:r,content:o}}throw new I(`item type ${String(n.type)} is not supported \u2014 message and function_call_output items only`,"item.type","invalid_request")}function Vn(t){let e=[],n=!1;for(let r of t){if(r.type==="function_call"){e.push({type:"function_call",call_id:r.callId,name:r.name,arguments:r.arguments??"{}"});continue}if(r.type==="function_call_output"){e.push({type:"function_call_output",call_id:r.callId,output:r.output});continue}let o=[];for(let s of r.content??[]){if(s.type==="input_text"||s.type==="output_text"){o.push({type:s.type,text:s.text});continue}if(s.type==="input_audio"){let i=s.transcript;if(typeof i=="string"&&i.trim().length>0){o.push({type:"input_text",text:i});continue}n=!0;continue}}e.push({role:r.role,content:o})}return{input:e,sawAudioOnlyItem:n}}var su=1001,iu=1011,ds=t=>t.slice(0,120),au=/^[A-Za-z0-9+/=]*$/,uu=2;function du(t){let e=Buffer.from(t,"base64");for(let n=0;n+1<e.byteLength;n+=2)if(Math.abs(e.readInt16LE(n))>uu)return!1;return!0}var ct=class{id=E("conn");ws;binding;onClosed;config=Hn();sessionId=E("sess");conversationId=E("conv");items=[];pendingAudioBytes=[];pendingAudioTotalBytes=0;frozenAudio=[];inFlight=null;activeResponseId=null;responseFlushed=!1;cancelled=!1;voiceClaimed=!1;audioLaneOwner=!1;audioUnsubscribe=null;outOfTurnAudioWarned=!1;audioOnlyTranscriptGapWarned=!1;closed=!1;turnLaneAbort=null;turnLaneIterator=null;turnTranscript={open:!1,text:"",final:!1};lastCommittedTranscript=null;pendingTranscriptItemId=null;manuallyDischargedTurnItem=null;constructor(e){this.ws=e.ws,this.binding=e.binding,this.onClosed=e.onClosed,this.binding.model&&(this.config.model=this.binding.model),this.binding.intent==="transcription"&&(this.config.type="transcription"),this.ws.on("message",n=>{this.onMessage(n)}),this.ws.on("close",()=>this.teardown("client disconnected")),this.ws.on("error",()=>this.teardown("client socket error")),this.send({type:"session.created",event_id:E("event"),session:Wt(this.config,this.sessionId)}),this.send({type:"conversation.created",event_id:E("event"),conversation:{id:this.conversationId,object:"realtime.conversation"}}),this.binding.turns&&this.startTurnLane()}startTurnLane(){let e=new AbortController;this.turnLaneAbort=e;let n=this.binding.turns(e.signal)[Symbol.asyncIterator]();this.turnLaneIterator=n,(async()=>{try{for(;;){let r=await n.next();if(r.done||this.closed||e.signal.aborted)return;this.onTurnEvent(r.value)}}catch(r){!this.closed&&!e.signal.aborted&&this.send(D(ze("turn_lane_fault",`the backend turn/transcript lane failed: ${r instanceof Error?r.message:String(r)}`)))}finally{this.turnLaneIterator===n&&(this.turnLaneIterator=null)}})()}isTranscriptionSession(){return this.binding.intent==="transcription"||this.config.type==="transcription"}transcriptionEventsOn(){return this.config.inputAudioTranscription||this.isTranscriptionSession()}completePendingTranscription(e){this.pendingTranscriptItemId=null;let n=this.turnTranscript.text.trim();this.turnTranscript={open:!1,text:"",final:!1},this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:e,content_index:0,transcript:n})}onTurnEvent(e){switch(e.kind){case"delta":case"final":{let n=typeof e.text=="string"?e.text:"",r=this.pendingTranscriptItemId;this.turnTranscript.open=!0,n.length>0&&(this.turnTranscript.text+=n),this.turnTranscript.final=e.kind==="final",this.transcriptionEventsOn()&&n.length>0&&this.send({type:"conversation.item.input_audio_transcription.delta",event_id:E("event"),item_id:r??"input_audio_turn",content_index:0,delta:n}),e.kind==="final"&&r!==null&&(this.completePendingTranscription(r),this.manuallyDischargedTurnItem=r);return}case"turn_start":{this.manuallyDischargedTurnItem=null,this.turnTranscript={open:!0,text:this.turnTranscript.text,final:!1},this.send({type:"input_audio_buffer.speech_started",event_id:E("event"),item_id:"input_audio_turn",audio_start_ms:typeof e.startMs=="number"?e.startMs:0}),this.config.turnDetection?.interruptResponse&&this.inFlight&&this.cancelResponse();return}case"turn_end":{let n=this.pendingTranscriptItemId;if(n!==null){this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:n,audio_end_ms:typeof e.startMs=="number"?e.startMs:0}),this.completePendingTranscription(n);return}let r=this.manuallyDischargedTurnItem;if(r!==null){this.manuallyDischargedTurnItem=null,this.turnTranscript={open:!1,text:"",final:!1},this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:r,audio_end_ms:typeof e.startMs=="number"?e.startMs:0});return}let o=this.turnTranscript.text.trim(),s=o.length>0?o:null;if(this.turnTranscript={open:!1,text:"",final:!1},this.lastCommittedTranscript=s,this.isTranscriptionSession()&&this.config.turnDetection===null){this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:"input_audio_turn",audio_end_ms:typeof e.startMs=="number"?e.startMs:0});return}this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let i=E("item"),a=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem({id:i,type:"message",role:"user",content:[{type:"input_audio",...s!==null?{transcript:s}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:E("event"),previous_item_id:a,item_id:i}),this.send({type:"conversation.item.created",event_id:E("event"),previous_item_id:a,item:this.itemToProtocol(this.items[this.items.length-1])}),this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:i,audio_end_ms:typeof e.startMs=="number"?e.startMs:0}),this.transcriptionEventsOn()&&s!==null&&this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:i,content_index:0,transcript:s}),this.config.turnDetection?.createResponse&&!this.inFlight&&!this.isTranscriptionSession()&&this.startResponse({});return}case"turn_eager_end":case"turn_resumed":return;default:return}}takeTurnTranscript(){let e=this.lastCommittedTranscript;return this.lastCommittedTranscript=null,e}async onMessage(e){if(this.closed)return;let n;try{n=e.toString()}catch{this.fail(H("decode_error","frame is not decodable text"),"invalid_frame");return}if(Buffer.byteLength(n,"utf8")>Gt){this.fail(H("frame_too_large",`frame exceeds the ${Gt}-byte limit`),"invalid_frame");return}let r;try{r=JSON.parse(n)}catch{this.fail(H("invalid_json","frame is not valid JSON"),"invalid_frame");return}if(typeof r.type!="string"||r.type.length===0){this.fail(H("missing_type",'event is missing a string "type"'),"invalid_frame");return}try{await this.handle(r)}catch(o){let s=o;if(s&&typeof s=="object"&&s.name==="SessionUpdateError"){this.send(D({type:"invalid_request_error",code:s.code??"invalid_value",message:s.message,param:s.param??null}));return}this.fail(ze(null,o instanceof Error?o.message:String(o)),"handler_fault")}}async handle(e){switch(e.type){case"session.update":this.config=lt(e.session,this.config,{voice:this.binding.voice,turns:this.binding.turns!==void 0,model:this.binding.model}),this.send({type:"session.updated",event_id:E("event"),session:Wt(this.config,this.sessionId)});return;case"conversation.item.create":{let n=Gn(e.item,this.binding.voice),r=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem(n),this.send({type:"conversation.item.created",event_id:E("event"),previous_item_id:r,item:this.itemToProtocol(n)});return}case"conversation.item.delete":{let n=this.items.findIndex(r=>r.id===e.item_id);if(n<0){this.send(D(H("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.items.splice(n,1),this.send({type:"conversation.item.deleted",event_id:E("event"),item_id:e.item_id});return}case"conversation.item.retrieve":{let n=this.items.find(r=>r.id===e.item_id);if(!n){this.send(D(H("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.send({type:"conversation.item.retrieved",event_id:E("event"),item:this.itemToProtocol(n)});return}case"conversation.item.truncate":{let n=this.items.find(r=>r.id===e.item_id);if(!n||n.type!=="message"||n.role!=="assistant"){this.send(D(H("item_not_found",`no assistant message item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}n.truncated=!0,this.send({type:"conversation.item.truncated",event_id:E("event"),item_id:n.id,content_index:typeof e.content_index=="number"?e.content_index:0,audio_end_ms:typeof e.audio_end_ms=="number"?e.audio_end_ms:0});return}case"input_audio_buffer.append":{if(typeof e.audio!="string"||e.audio.length===0){this.send(D(H("invalid_audio",'input_audio_buffer.append requires base64 "audio"',"audio")));return}if(!this.ensureVoice())return;if(!au.test(e.audio)){this.send(D(H("invalid_audio","input_audio_buffer.append audio must be base64","audio")));return}let n=Buffer.byteLength(e.audio,"base64");if(this.pendingAudioTotalBytes+n>Jt){this.send(D(H("buffer_full",`uncommitted input audio exceeds the ${Jt}-byte bound \u2014 commit or clear first`,"audio")));return}if(this.config.type===void 0&&(this.config.type="realtime"),this.inFlight){this.binding.audioSession.audio.appendInputAudio(e.audio);return}if(this.isTranscriptionSession()){this.binding.audioSession.audio.appendInputAudio(e.audio),this.pendingAudioTotalBytes+=n;return}this.pendingAudioBytes.push(e.audio),this.pendingAudioTotalBytes+=n;return}case"input_audio_buffer.clear":{if(!this.ensureVoice())return;this.pendingAudioBytes.length=0,this.pendingAudioTotalBytes=0,this.lastCommittedTranscript=null,this.pendingTranscriptItemId===null&&(this.turnTranscript={open:!1,text:"",final:!1}),this.send({type:"input_audio_buffer.cleared",event_id:E("event")});return}case"input_audio_buffer.commit":{if(!this.ensureVoice())return;if(this.pendingAudioTotalBytes===0&&this.takeTurnTranscript()===null){this.send(D(H("empty_buffer","input_audio_buffer.commit with no buffered audio","audio")));return}this.isTranscriptionSession()&&this.pendingTranscriptItemId!==null&&this.completePendingTranscription(this.pendingTranscriptItemId),this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let n=this.takeTurnTranscript();n===null&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let r=E("item"),o=this.items.length>0?this.items[this.items.length-1].id:null;if(this.pushItem({id:r,type:"message",role:"user",content:[{type:"input_audio",...n!==null?{transcript:n}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:E("event"),previous_item_id:o,item_id:r}),this.send({type:"conversation.item.created",event_id:E("event"),previous_item_id:o,item:this.itemToProtocol(this.items[this.items.length-1])}),this.isTranscriptionSession())if(n!==null)this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:r,content_index:0,transcript:n});else if(this.turnTranscript.final){let s=this.turnTranscript.text.trim();this.turnTranscript={open:!1,text:"",final:!1},this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:r,content_index:0,transcript:s}),this.manuallyDischargedTurnItem=r}else this.pendingTranscriptItemId=r;return}case"output_audio_buffer.clear":{this.send(D(H("output_clear_unavailable","output_audio_buffer.clear has no server-side playback queue to flush on this adapter: output audio is delivered as response.output_audio.delta events and interrupted turns are stopped with response.cancel")));return}case"response.create":if(this.isTranscriptionSession()){this.send(D(H("unsupported","response.create is not supported on a transcription session \u2014 this session transcribes the streamed audio with the engine's own transcript lane and emits conversation.item.input_audio_transcription events; there are no model responses to create","response.create")));return}this.startResponse(e.response??{});return;case"response.cancel":this.cancelResponse();return;default:this.send(D(H("unknown_event",`event type ${e.type} is not supported by this adapter`)))}}ensureVoice(){return!this.binding.voice||!this.binding.audioSession?(this.send(D(H("audio_not_supported","audio events require a voice binding from the resolver \u2014 this session was resolved text-only","audio"))),!1):(this.voiceClaimed||(this.claimVoiceBinding(),this.audioUnsubscribe=this.binding.audioSession.audio.onOutputAudio(e=>{this.onOutputAudioFrame(this.activeResponseId??"",e)}),this.voiceClaimed=!0),!0)}claimVoiceBinding(){let e=this.binding.audioSession,n=e.openaiRealtimeClaims??(e.openaiRealtimeClaims=new Set);if(n.has(e))throw new Error("voice binding already owned by another connection \u2014 the resolver handed one audio bridge to two realtime conversations, which must never happen (independent callers require private bindings)");n.add(e),this.audioLaneOwner=!0}releaseVoiceBinding(){let e=this.binding.audioSession;!e||!this.audioLaneOwner||(e.openaiRealtimeClaims?.delete(e),this.audioLaneOwner=!1)}warnAudioOnlyTranscriptGap(){this.audioOnlyTranscriptGapWarned||(this.audioOnlyTranscriptGapWarned=!0,this.send(D({type:"invalid_request_error",code:"audio_input_transcript_unavailable",message:"committed input audio is forwarded to the runtime audio lane but is NOT model-visible in the text turn lane: the resolved backend wires no turn/transcript lane (the voice engine's own stt stream with turn_start/turn_end + transcripts). Wire the transcribe/turn-detection leg upstream (the resolver supplies binding.turns) and committed audio gains its REAL transcript as the model-visible turn.",param:"input_audio_buffer.commit"})))}async upstreamStream(e,n){let r=this.binding.session;if(r&&typeof r.sendResponseCreate=="function"){let s=e.response??e;return r.sendResponseCreate({type:"response.create",response:s},e.input??[],n)}let o=this.binding.clients;if(o&&typeof o.createResponse=="function"){let{response:s,input:i,...a}=e;return await o.createResponse({...a,input:i})}throw new Error("realtime binding provides neither session.sendResponseCreate nor clients.createResponse \u2014 no upstream turn seam")}startResponse(e){if(this.inFlight){this.send(D(H("response_in_progress","a response is already in progress \u2014 cancel it with response.cancel first","response.create")));return}let n=e??{};if(n.modalities!==void 0){if(!Array.isArray(n.modalities)||n.modalities.some(g=>g!=="text"&&g!=="audio")){this.send(D(H("invalid_modalities",'response.modalities must be "text"/"audio"',"response.modalities")));return}if(n.modalities.includes("audio")&&!this.ensureVoice())return}if(n.input!==void 0){this.send(D(H("response_input_unsupported","response.input is not supported by this adapter: build the context with conversation.item.create, then response.create","response.input")));return}this.cancelled=!1,this.responseFlushed=!1,this.messageItemAdded=!1;let r=E("req"),o=`resp_${r}`;this.activeResponseId=o;let s=this.items.length,{input:i,sawAudioOnlyItem:a}=Vn(this.items),u=typeof n.instructions=="string"?n.instructions:this.config.instructions,d=typeof n.temperature=="number"?n.temperature:this.config.temperature,l=n.max_output_tokens??this.config.maxOutputTokens,p=l==="inf"||l===void 0?void 0:l,m=n.tools!==void 0?n.tools:this.config.tools,c=n.tool_choice!==void 0?n.tool_choice:this.config.toolChoice,f={input:i,stream:!0,...this.config.model?{model:this.config.model}:{},...u!==void 0?{instructions:u}:{},...d!==void 0?{temperature:d}:{},...p!==void 0?{max_output_tokens:p}:{},...m!==void 0?{tools:m}:{},...c!==void 0?{tool_choice:c}:{}};for(let g of this.frozenAudio.splice(0))this.binding.audioSession?.audio.appendInputAudio(g);this.inFlight=(async()=>{let g=[],b=A=>{this.responseFlushed?this.send(A):g.push(A)},y=()=>{if(!this.responseFlushed){this.send({type:"response.created",event_id:E("event"),response:this.responseObject(o,"in_progress")}),this.send({type:"response.in_progress",event_id:E("event"),response:this.responseObject(o,"in_progress")});for(let A of g)this.send(A);g.length=0,this.responseFlushed=!0}},h={messageOpen:!1,messageText:"",tools:new Map,nextOutputIndex:0},v=null;try{a&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let A;try{A=await this.upstreamStream(f,r)}catch(w){this.upstreamTurnFault(w,o);return}try{for await(let w of A){if(this.cancelled)break;let _=w;if(_.type==="error"){let x=this.upstreamErrorBody(_);this.responseFlushed?this.send(D(x)):this.send({type:"response.failed",event_id:E("event"),response:this.failedResponse(o,x)});return}if(_.type==="response.output_text.delta"&&typeof _.delta=="string"){y(),h.messageOpen||(this.beginMessageItem(o,h.nextOutputIndex++),h.messageOpen=!0),b({type:"response.output_text.delta",event_id:E("event"),item_id:`${o}_msg_0`,response_id:o,output_index:0,content_index:0,delta:_.delta}),h.messageText+=_.delta;continue}if(_.type==="response.function_call_arguments.delta"){y();let x=typeof _.delta=="string"?_.delta:"",C=typeof _.call_id=="string"&&_.call_id.length>0?_.call_id:`${o}_fc_${h.nextOutputIndex}`,S=h.tools.get(C);if(!S){let R=typeof _.name=="string"&&_.name.length>0?_.name:void 0;S={outputIndex:h.nextOutputIndex++,name:R,args:""},h.tools.set(C,S),b({type:"response.output_item.added",event_id:E("event"),response_id:o,output_index:S.outputIndex,item:{id:C,object:"realtime.item",type:"function_call",call_id:C,name:R??"",arguments:"",status:"in_progress"}})}typeof _.name=="string"&&_.name.length>0&&(S.name=_.name),S.args+=x,b({type:"response.function_call_arguments.delta",event_id:E("event"),item_id:C,response_id:o,output_index:S.outputIndex,call_id:C,name:S.name??"",delta:x});continue}if(_.type==="response.completed"){v=_.response??null;continue}}}catch(w){this.upstreamTurnFault(w,o);return}if(this.cancelled)return;this.closeOpenItems(o,h,s),y(),this.send({type:"response.done",event_id:E("event"),response:this.doneResponse(o,s,v)})}finally{this.inFlight=null,this.activeResponseId=null}})()}onOutputAudioFrame(e,n){if(this.cancelled)return;if(this.activeResponseId!==e){if(du(n))return;!this.outOfTurnAudioWarned&&!this.closed&&(this.outOfTurnAudioWarned=!0,this.send(D({type:"invalid_request_error",code:"out_of_turn_audio",message:"the runtime produced voice OUT outside a response turn; this adapter delivers output audio only within response.output_audio.delta of an open turn \u2014 wire server-driven turns (VAD) upstream or keep speech inside response turns"})));return}this.responseFlushed||(this.send({type:"response.created",event_id:E("event"),response:this.responseObject(e,"in_progress")}),this.send({type:"response.in_progress",event_id:E("event"),response:this.responseObject(e,"in_progress")}),this.responseFlushed=!0);let r=`${e}_msg_0`;this.messageItemOpenForAudio||(this.beginAudioMessageItem(e),this.messageItemOpenForAudio=!0),this.send({type:"response.output_audio.delta",event_id:E("event"),item_id:r,response_id:e,output_index:0,content_index:0,delta:n})}messageItemAdded=!1;messageItemOpenForAudio=!1;beginMessageItem(e,n){let r=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:E("event"),response_id:e,output_index:n,item:{id:r,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:E("event"),item_id:r,response_id:e,output_index:n,content_index:0,part:{type:"output_text",text:""}})}beginAudioMessageItem(e){let n=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:E("event"),response_id:e,output_index:0,item:{id:n,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:E("event"),item_id:n,response_id:e,output_index:0,content_index:0,part:{type:"output_audio",transcript:""}})}closeOpenItems(e,n,r){if(n.messageOpen||this.messageItemOpenForAudio){let o=`${e}_msg_0`,s=this.messageItemOpenForAudio;s&&this.send({type:"response.output_audio.done",event_id:E("event"),item_id:o,response_id:e,output_index:0,content_index:0}),n.messageText.length>0&&this.send({type:"response.output_text.done",event_id:E("event"),item_id:o,response_id:e,output_index:0,content_index:0,text:n.messageText}),this.send({type:"response.content_part.done",event_id:E("event"),item_id:o,response_id:e,output_index:0,content_index:0,part:s?{type:"output_audio",transcript:""}:{type:"output_text",text:n.messageText}});let i=[];s&&i.push({type:"output_audio"}),n.messageText.length>0&&i.push({type:"output_text",text:n.messageText});let a={id:o,type:"message",role:"assistant",content:i};this.pushItem(a),this.send({type:"response.output_item.done",event_id:E("event"),response_id:e,output_index:0,item:this.itemToProtocol(a)})}for(let[o,s]of n.tools){let i=s.args.length>0?s.args:"{}";this.send({type:"response.function_call_arguments.done",event_id:E("event"),item_id:o,response_id:e,output_index:s.outputIndex,call_id:o,name:s.name??"",arguments:i});let a={id:o,type:"function_call",callId:o,name:s.name,arguments:i};this.pushItem(a),this.send({type:"response.output_item.done",event_id:E("event"),response_id:e,output_index:s.outputIndex,item:this.itemToProtocol(a)})}this.messageItemOpenForAudio=!1,this.messageItemAdded=!1}responseObject(e,n,r){return{id:e,object:"realtime.response",status:n,conversation_id:this.conversationId,usage:null,...r??{}}}failedResponse(e,n){return this.responseObject(e,"failed",{status_details:{error:{code:n.code,message:n.message}}})}doneResponse(e,n,r){let o=r&&typeof r.usage=="object"&&r.usage!==null?r.usage:null,s=[];for(let i=n;i<this.items.length;i++)s.push(this.itemToProtocol(this.items[i]));return this.responseObject(e,"completed",{output:s,usage:o?{input_tokens:typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0,output_tokens:typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0,total_tokens:typeof o.total_tokens=="number"&&Number.isFinite(o.total_tokens)?o.total_tokens:(typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0)+(typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0),input_token_details:{cached_tokens:0,audio_tokens:0,text_tokens:0},output_token_details:{audio_tokens:0,text_tokens:0}}:null})}upstreamErrorBody(e){let n=e.error,r=n&&typeof n.message=="string"?n.message:JSON.stringify(e);return{type:"server_error",code:n&&typeof n.code=="string"?n.code:null,message:r}}failSessionGone(e){let n=e instanceof Error?e.message:String(e);this.fail(ze("session_gone",`this realtime connection's pooled session \u2014 the one its audio bridge and every turn ride \u2014 is gone: ${n}`),"session_gone")}upstreamTurnFault(e,n){if(e instanceof X){this.failSessionGone(e);return}if(!this.cancelled){if(!this.responseFlushed&&!(e instanceof cs.SessionFailedError&&(e.kind==="never-live"||e.kind==="create-failed"))){this.failSessionGone(e);return}this.emitUpstreamFault(e,n)}}emitUpstreamFault(e,n){let r=ze(null,e instanceof Error?e.message:String(e));this.responseFlushed?this.send(D(r)):this.send({type:"response.failed",event_id:E("event"),response:this.failedResponse(n,r)})}cancelResponse(){let e=this.inFlight;if(!e){this.send(D(H("no_response_to_cancel","no response is in progress","response.cancel")));return}this.cancelled=!0;let n=this.activeResponseId??"resp_";e.finally(()=>{this.send({type:"response.cancelled",event_id:E("event"),response:this.responseObject(n,"cancelled")})})}pushItem(e){this.items.push(e),this.items.length>Vt&&(this.items.shift(),this.send(D({type:"invalid_request_error",code:"transcript_evicted",message:`the conversation transcript exceeded ${Vt} items \u2014 the oldest item was evicted to keep the per-connection bound`})))}itemToProtocol(e){let n={id:e.id,object:"realtime.item"};if(e.type==="function_call")return{...n,type:"function_call",call_id:e.callId,name:e.name,arguments:e.arguments??"{}",status:"completed"};if(e.type==="function_call_output")return{...n,type:"function_call_output",call_id:e.callId,output:e.output};let r=(e.content??[]).map(o=>o.type==="input_text"||o.type==="output_text"?{type:o.type,text:o.text}:o.type==="input_audio"?{type:"input_audio",...o.transcript?{transcript:o.transcript}:{}}:{type:"output_audio",transcript:o.transcript??""});return{...n,type:"message",role:e.role,status:e.truncated?"incomplete":"completed",content:r}}send(e){if(!(this.closed||this.ws.readyState!==this.ws.OPEN)){if(this.ws.bufferedAmount>qn){this.ws.close(su,ds("outbound buffer exceeded")),this.teardown("outbound buffer exceeded");return}this.ws.send(JSON.stringify(e))}}fail(e,n){this.send(D(e)),this.ws.close(iu,ds(n)),this.teardown(n)}teardown(e){if(this.closed)return;this.closed=!0,this.turnLaneAbort?.abort(new Error("the realtime connection closed \u2014 the turn lane consumer is gone")),this.turnLaneAbort=null;let n=this.turnLaneIterator;if(this.turnLaneIterator=null,n!==null){let r=n.return?.();r&&r.catch(()=>{})}this.releaseVoiceBinding(),this.audioUnsubscribe?.(),this.audioUnsubscribe=null,this.binding.release&&Promise.resolve().then(()=>this.binding.release?.()).catch(r=>ls(this.id,`binding release failed: ${r instanceof Error?r.message:String(r)}`));try{this.ws.removeAllListeners("message")}catch{}this.onClosed?.(this,{reason:e}),ls(this.id,e)}},Jn=[];function ls(t,e){Jn.push({id:t,reason:e}),Jn.length>256&&Jn.shift()}var Kn="/v1/realtime",lu=t=>t.slice(0,120);function cu(t,e,n){let r=e===401||e===403?"Unauthorized":e===502?"Upstream Fault":"Upgrade Refused";t.write(`HTTP/1.1 ${e} ${r}\r
23
23
  Connection: close\r
24
24
  Content-Type: text/plain\r
25
25
  \r
26
- ${nu(n)}\r
27
- `),t.destroy()}function ou(t){return`${JSON.stringify({event:"realtime_upgrade",ts:new Date().toISOString(),...t})}
28
- `}function su(t){de(process.stdout,t)}function iu(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function Jn(t){return t.audioSession?.audio.sessionId??void 0}function Kn(t,e){let n=new ps.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,s,i)=>{if(new URL(o.url??"/","http://localhost").pathname!==Wn)return;r.add(s),s.on("close",()=>{r.delete(s)});let u=E("rtup"),d=iu(o),l=v=>{(e.log??su)(ou(v))};l({stage:"accept",conn:u,model:d}),s.resume();let p=new AbortController,m=null,c=v=>{m??=v,p.abort(new Error(m))},f=()=>c("client socket closed"),g=()=>c("client socket closed (half-close)"),_=v=>c(`client socket error: ${v.message}`);s.once("close",f),s.once("end",g),s.once("error",_);let y=()=>{s.removeListener("close",f),s.removeListener("end",g),s.removeListener("error",_)},h=v=>{l({stage:"close",conn:u,model:d,...v})},w=v=>{let b=v,R=b.session!=null&&typeof b.session.sendResponseCreate=="function",L=b.clients!=null&&typeof b.clients.createResponse=="function";if(!R&&!L)throw new Error("resolver returned a binding without an upstream turn seam (session.sendResponseCreate or clients.createResponse required) \u2014 refusing the upgrade LOUD");return new Promise((S,x)=>{n.handleUpgrade(o,s,i,C=>{try{new dt({ws:C,binding:v,onClosed:(F,Y)=>{h({while:"live",reason:Y.reason})}}),l({stage:"admit",conn:u,model:d,...Jn(v)===void 0?{}:{session:Jn(v)}}),S()}catch(F){x(F)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o,p.signal).then(async v=>{if(p.signal.aborted){y();let b=Jn(v);try{await v.release?.()}catch(R){h({while:"queued",...b===void 0?{}:{session:b},reason:m??"client socket closed",release_failed:R instanceof Error?R.message:String(R)});return}h({while:"queued",...b===void 0?{}:{session:b},reason:m??"client socket closed",released:!0}),s.destroy();return}y(),await w(v)}).catch(v=>{let b=v instanceof cs.SessionFailedError?Bt(v):null,R=null;if(v!==null&&typeof v=="object"&&"status"in v){let x=v.status;typeof x=="number"&&(R=x)}let L=R??b?.status??502,S=v instanceof Error?v.message:String(v);h({while:"queued",...p.signal.aborted?{reason:m??"client socket closed"}:{reason:S,status:L}}),s.destroyed||ru(s,L,S)}).finally(()=>{r.delete(s)})}),async()=>{let o=Date.now()+5e3;for(;r.size>0&&Date.now()<o;)await new Promise(s=>setTimeout(s,25));await new Promise(s=>n.close(()=>s()))}}var le=(t,e)=>Object.assign(new Error(e),{status:t});function au(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function uu(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("intent")?.trim();return n===""?void 0:n}var du='"transcription" (a transcription session), or no intent at all (the realtime voice session)';async function lu(t,e,n,r){if(!t.supportsAudio)throw le(501,"the realtime lane needs a router-backed client (supportsAudio) to gate audio modality \u2014 hand-built clients without the catalog oracle are refused, never allowed silently");let o;try{o=await t.supportsAudio(e)}catch(d){throw d instanceof U?le(404,d.message):d}if(!o)throw le(404,`model ${JSON.stringify(e??"(default)")} is not audio-capable (catalog task is neither stt nor tts) \u2014 this realtime lane serves only audio-task models; chat models ride /v1/chat/completions`);if(!t.openAudioOn)throw le(501,"an audio-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the connection\u2019s pooled session \u2014 refusing loudly rather than binding a voiceless session");let s=!1;if(t.supportsTranscription)try{s=await t.supportsTranscription(e)}catch(d){throw d instanceof U?le(404,d.message):d}if(s&&!t.openTurnsOn)throw le(501,"a transcription-capable (stt) model needs a router-backed client (openTurnsOn) to open the engine\u2019s own turn/transcript lane (the stt named stream) pinned to the connection\u2019s pooled session \u2014 refusing loudly rather than binding a transcript-less transcription session");if(s&&t.transcriptStreamFor){let d;try{d=await t.transcriptStreamFor(e)}catch(l){throw l instanceof U?le(404,l.message):l}if(d!=="stt")throw le(501,`model ${JSON.stringify(e??"(default)")} overrides engine_args.transcript_stream with ${JSON.stringify(d)} \u2014 this lane consumes the engine\u2019s own transcripts from the platform\u2019s default "${"stt"}" stream and refuses loudly rather than listen on a stream the engine does not write`)}if(r!==void 0&&r!=="transcription")throw le(400,`intent ${JSON.stringify(r)} is not supported on this realtime lane \u2014 accepted values: ${du}`);if(r==="transcription"&&!s)throw le(400,`model ${JSON.stringify(e??"(default)")} cannot serve the transcription intent \u2014 the transcription intent needs an stt model (the catalog task is not stt)`);let i=await t.sessionHandle(e),a={audio:await t.openAudioOn(i,n)},u={clients:{createResponse:async d=>await t.createResponseOn(i,d)},voice:o,audioSession:a};return s&&(u.turns=d=>t.openTurnsOn(i,d)),r==="transcription"&&(u.intent="transcription"),e!==void 0&&(u.model=e),u}function fs(t,e){return{resolve:async(n,r)=>{if(e!==void 0){let o=n.headers.authorization,s=typeof o=="string"?o.trim():"",i=s.startsWith("Bearer ")?s.slice(7).trim():"";if(i===""||i!==e)throw le(401,"invalid local proxy api key")}return lu(t,au(n),r,uu(n))}}}var Q=class extends Error{};async function Wt(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new Q(`request body exceeds the ${e}-byte limit`);let r=[],o=0;for await(let s of t){let i=s;if(o+=i.length,o>e)throw new Q(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function Kt(t,e){let n=t[Symbol.asyncIterator](),r=0,o=new ReadableStream({async pull(i){try{let a=await n.next();if(a.done){i.close();return}if(r+=a.value.length,r>e){i.error(new Q(`request body exceeds the ${e}-byte limit`));return}i.enqueue(a.value)}catch(a){i.error(a)}},cancel(){n.return?.()}}),s;try{let i=new Headers;for(let[d,l]of Object.entries(t.headers))if(typeof l=="string")i.set(d,l);else if(Array.isArray(l))for(let p of l)i.append(d,p);let a={method:"POST",headers:i,body:o,duplex:"half"};s=await new Request(`https://proxy.internal${t.url??"/"}`,a).formData()}catch(i){throw i instanceof Q?i:new Error(`malformed multipart body: ${String(i instanceof Error?i.message:i)}`)}return s}var ms="/v1/images/generations",zt="/v1/images/edits";function gs(t){return t===ms||t===zt}var hs="/v1/images/models";async function ys(t,e,n){if(t){if(t.model=ne({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}var $=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},cu=16*1024*1024,ws=64*1024*1024;function _s(t){return t.length>=8&&t[0]===137&&t[1]===80&&t[2]===78&&t[3]===71&&t[4]===13&&t[5]===10&&t[6]===26&&t[7]===10?"image/png":t.length>=3&&t[0]===255&&t[1]===216&&t[2]===255?"image/jpeg":t.length>=12&&t[0]===82&&t[1]===73&&t[2]===70&&t[3]===70&&t[8]===87&&t[9]===69&&t[10]===66&&t[11]===80?"image/webp":null}function lt(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}async function vs(t,e,n,r){let o=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},r),s=null;for await(let p of o){if(p.type==="error"){let m=p.error??{};throw new $(502,`image upstream error: ${String(m.message??"upstream reported an error")}`)}if(p.type==="response.completed"){s=p.response??null;break}}if(s===null)throw new $(502,"image upstream produced no terminal response \u2014 refusing to fabricate an image");let i=s.images;if(!Array.isArray(i)||i.length!==1||typeof i[0]!="string"||i[0].length===0)throw new $(502,`image upstream returned ${Array.isArray(i)?i.length:"a non-array images field"} images for the single-shot (n=1) contract, which produces exactly one \u2014 refusing to truncate or fabricate`);let a=[],u=i[0].startsWith("data:")?i[0].slice(i[0].indexOf(";base64,")+8):i[0],d=Buffer.from(u,"base64");if(_s(d)===null)throw new $(502,`image upstream returned a payload that is not a PNG/JPEG/WebP image (${d.length} bytes) \u2014 refusing to pass it through`);return a.push(u),{images:a}}function bs(t){return t instanceof $?t:t instanceof U?new $(404,t.message):new $(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function ks(t,e,n,r){if(await t(e,n))return!0;let o=n==="edit"?"no image-input (expects_image) edit destination":"no pure text-to-image (expects_image=false) generation destination";return lt(r,new $(404,e===void 0?`no image-capable default model is configured for this caller (${o}) \u2014 name an image-capable \`model\``:`model "${e}" is not ${n==="edit"?"edit-capable":"generation-capable"} for this caller (${o}; capability is read from the catalog's modality metadata, not the serve function name)`)),!1}function Ss(t,e){t&&(t.outputUnits=e.images.length)}function Rs(t,e){t.statusCode=200,t.setHeader("content-type","application/json"),t.end(JSON.stringify({created:Math.floor(Date.now()/1e3),data:e.images.map(n=>({b64_json:n}))}))}var pu={model:!0,prompt:!0,response_format:!0,n:!0};async function xs(t,e,n){let r=n.maxBodyBytes??ws;try{if(t.method!=="POST")throw new $(405,`${String(t.method)} is not supported on ${ms}`);let o;try{o=JSON.parse((await Wt(t,r)).toString("utf8")||"{}")}catch(p){throw p instanceof Q?new $(413,p.message):new $(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new $(400,"request body must be a JSON object");let s=o;for(let p of Object.keys(s))if(!pu[p])throw new $(400,`request field "${p}" is not implemented: the native image request schema carries only the prompt (size, quality, style, seed, mask and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, n.`,p);let i=s.prompt;if(typeof i!="string"||i.trim().length===0)throw new $(400,"prompt is required and must be a non-empty string","prompt");let a=s.model===void 0?void 0:s.model;if(a!==void 0&&typeof a!="string")throw new $(400,"model must be a string","model");await ys(n.turn,n.clients,a);let u=s.n??1;if(u!==1)throw new $(400,`n=${JSON.stringify(u)} is not supported: the native image request is single-shot and produces exactly one image per request (no batch support yet)`,"n");let d=s.response_format??"b64_json";if(d!=="b64_json")throw new $(400,`response_format ${JSON.stringify(d)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");if(n.stats?.request("images_generations",i.length,i.length),!await ks(n.imageModels,a,"generation",e))return;let l=[{type:"input_text",text:i}];try{let p=await vs(n.clients,a,l,n.turn?ie(n.turn):void 0);Ss(n.turn,p),Rs(e,p)}catch(p){throw bs(p)}}catch(o){if(o instanceof Q){lt(e,new $(413,o.message));return}if(o instanceof $){lt(e,o);return}throw o}}var fu={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function Es(t,e,n){let r=n.maxBodyBytes??ws,o=n.maxImagePartBytes??cu;try{if(t.method!=="POST")throw new $(405,`${String(t.method)} is not supported on ${zt}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new $(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await Kt(t,r)}catch(c){throw c instanceof Q?new $(413,c.message):new $(400,String(c instanceof Error?c.message:c))}let a=i.get("prompt");if(typeof a!="string"||a.trim().length===0)throw new $(400,"prompt is required and must be a non-empty string field","prompt");let u=i.get("model");if(u!==null&&typeof u!="string")throw new $(400,"model must be a string field","model");let d=u===null?void 0:u;await ys(n.turn,n.clients,d);let l=i.get("response_format")??"b64_json";if(l!=="b64_json")throw new $(400,`response_format ${JSON.stringify(l)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");for(let c of i.keys())if(!fu[c])throw new $(400,`form field "${c}" is not implemented: the native image request schema carries the prompt and the image inputs only (mask, size, seed and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, image (or image[]).`,c);let p=[];for(let c of["image","image[]"])for(let f of i.getAll(c)){if(!(f instanceof File))throw new $(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",c);if(f.size>o)throw new $(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,c);let g=new Uint8Array(await f.arrayBuffer()),_=_s(g);if(_===null)throw new $(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",c);p.push({bytes:g,mime:_})}if(p.length===0)throw new $(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",a.length,a.length),!await ks(n.imageModels,d,"edit",e))return;let m=[{type:"input_text",text:a}];for(let{bytes:c,mime:f}of p)m.push({type:"input_image",image_url:`data:${f};base64,${Buffer.from(c).toString("base64")}`});try{let c=await vs(n.clients,d,m,n.turn?ie(n.turn):void 0);Ss(n.turn,c),Rs(e,c)}catch(c){throw bs(c)}}catch(s){if(s instanceof Q){lt(e,new $(413,s.message));return}if(s instanceof $){lt(e,s);return}throw s}}var pt="/v1/audio/speech";function Ps(t){return t===pt}var As=4096,mu=18e4,gu=5e3,hu=64*1024*1024,Is=8,Os=5,Xt="voice_applied",yu="X-Urun-Voice-Applied",P=class extends Error{status;param;retryAfterSeconds;constructor(e,n,r,o){super(n),this.status=e,this.param=r,this.retryAfterSeconds=o}};function zn(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),e.retryAfterSeconds!==void 0&&t.setHeader("retry-after",String(e.retryAfterSeconds)),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}var Qt=class extends P{constructor(e){super(502,e)}};async function wu(t,e,n){if(t){if(t.model=ne({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}function _u(t,e){if(!t)return;let n=e/(24e3*2);t.outputUnits=n,t.outputMinutes=n/60}var vu=2;function bu(t){for(let e=0;e+1<t.byteLength;e+=2)if(Math.abs(t.readInt16LE(e))>vu)return!1;return!0}function ku(t){let e=24e3,n=2,r=1,o=Buffer.alloc(44);return o.write("RIFF",0,"ascii"),o.writeUInt32LE(36+t,4),o.write("WAVE",8,"ascii"),o.write("fmt ",12,"ascii"),o.writeUInt32LE(16,16),o.writeUInt16LE(1,20),o.writeUInt16LE(r,22),o.writeUInt32LE(e,24),o.writeUInt32LE(e*r*n,28),o.writeUInt16LE(r*n,32),o.writeUInt16LE(16,34),o.write("data",36,"ascii"),o.writeUInt32LE(t,40),o}var Ts=new WeakMap,Yt=new WeakMap;function Su(t,e){let n=Yt.get(t)??0;if(n>=Is)throw new P(503,`this model's speech queue is full \u2014 ${Is} requests are already admitted (one in-flight turn plus the queued depth) and each holds a pooled session turn; retry after ${Os}s`,void 0,Os);Yt.set(t,n+1);let r=Ts.get(t)??Promise.resolve(),o,s=new Promise(i=>{o=i});return Ts.set(t,s),(async()=>{await r;try{return await e()}finally{Yt.set(t,(Yt.get(t)??1)-1),o()}})()}async function Ru(t,e,n,r){if(typeof t.evictSpeechSession!="function")throw new P(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned session after a failed turn \u2014 refusing loudly rather than handing the next request the failed turn\u2019s audio");try{await t.evictSpeechSession(e)}catch(i){if(!(i instanceof X))throw i}if(n?.return===void 0)return;let o=Promise.resolve(n.return()).then(()=>{},()=>{}),s;await Promise.race([o,new Promise(i=>{s=setTimeout(i,r)})]).finally(()=>{s!==void 0&&clearTimeout(s)})}function ct(t){return t instanceof P?t:t instanceof U?new P(404,t.message):new P(502,`speech upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function xu(t,e,n,r,o,s,i){return Su(e,async()=>{if(i?.()===!0)return null;let a=[],u=!1,d=0,l=!1,p=!1,m,c=new Promise(A=>{m=A}),f=()=>{l&&!p&&d>=o.quietRunFrames&&(p=!0,m())},g=e.onOutputAudio(A=>{let v=Buffer.from(A,"base64");if(v.byteLength===0)return;let b=bu(v);if(!u){if(b)return;u=!0}a.push(v),d=b?d+1:0,f()}),_=null,y,h,w;try{w=(await t.createResponseOn(n,{model:r.model,input:[{role:"user",content:[{type:"input_text",text:r.text}]}],stream:!1,...r.voice!==void 0?{voice:r.voice}:{}}))[Symbol.asyncIterator]();let v=new Promise(S=>{h=setTimeout(S,s.capMs)}),b=!1;for(;;){let S=w.next(),x=await Promise.race([S,v]);if(x===void 0){b=!0,S.catch(()=>{});break}if(x.done)break;let C=x.value;if(C.type==="error"){let F=C.error??{};throw await w.return?.(),new P(502,`speech upstream error: ${String(F.message??"upstream reported an error")}`)}if(C.type==="response.completed"){_=C.response??null,l=!0,f(),await w.return?.();break}}if(clearTimeout(h),b)throw new P(504,`the speech turn produced no terminal event within ${s.capMs}ms \u2014 the upstream stream never completed, so the pinned speech session is evicted (the next request opens a fresh one), the serialized lane is released and the caller can retry; a stuck turn must not hold every later request on this lane`);if(_===null)throw new P(502,"speech upstream produced no terminal response \u2014 refusing to fabricate audio");let R=!1;if(await Promise.race([c,new Promise(S=>{y=setTimeout(()=>{R=!0,S()},o.capMs)})]),y!==void 0&&clearTimeout(y),r.voice!==void 0){let S=_[Xt];if(typeof S!="string"||S!==r.voice){let x=`the runtime did not acknowledge the requested voice ${JSON.stringify(r.voice)} \u2014 the terminal response carried ${typeof S=="string"?`${Xt} ${JSON.stringify(S)}`:`no ${Xt} field`}; the speak-bridge companion is not deployed, and this lane never serves default-voice audio as the requested voice`;throw p?new Qt(x):new P(502,x)}}if(!u)throw new P(502,"the upstream produced no voice for this utterance \u2014 refusing to serve silence as success");if(R&&!p&&d<1)throw new P(502,`the upstream audio stream did not quiesce within ${o.capMs}ms of the turn's end (voiced frames still arriving) \u2014 refusing to serve audio whose tail cannot be vouched for`);let L=d>0?a.slice(0,a.length-d):a;return{pcm:Buffer.concat(L),completed:_}}catch(A){throw A instanceof Qt||await Ru(t,n,w,s.returnBoundMs),A}finally{y!==void 0&&clearTimeout(y),h!==void 0&&clearTimeout(h),g()}})}async function Eu(t,e,n){let r;try{r=await t(e)}catch(o){throw ct(o)}return r?!0:(zn(n,new P(404,e===void 0?"no speech-capable default model is configured for this caller (catalog task tts) \u2014 name a tts model":`model "${e}" is not speech-capable (catalog task is not tts) \u2014 this lane serves only text-to-speech models; chat models ride /v1/chat/completions`)),!1)}var Au={model:!0,input:!0,voice:!0,response_format:!0,stream_format:!0,speed:!0,instructions:!0},Iu="wav, pcm";async function Cs(t,e,n){let r=n.maxBodyBytes??hu,o=!1;e.once("close",()=>{o=!0});let s=()=>o||e.destroyed;try{if(t.method!=="POST")throw new P(405,`${String(t.method)} is not supported on ${pt}`);let i;try{i=JSON.parse((await Wt(t,r)).toString("utf8")||"{}")}catch(w){throw w instanceof Q?new P(413,w.message):new P(400,"request body is not valid JSON")}if(i===null||typeof i!="object"||Array.isArray(i))throw new P(400,"request body must be a JSON object");let a=i;for(let w of Object.keys(a))if(!Au[w])throw new P(400,`request field "${w}" is not implemented: the speech request schema carries the utterance and its voice (the qwen3 backend fixes voice style at deploy time, and sample rate is pinned to the engine's native 24 kHz mono). Supported fields: model, input, voice, response_format, stream_format, speed, instructions.`,w);let u=a.input;if(typeof u!="string"||u.trim().length===0)throw new P(400,"input is required and must be a non-empty string","input");if(u.length>As)throw new P(400,`input is ${u.length} chars \u2014 over the ${As}-char limit (one utterance; the backend bounds generation text-shaped, and an unbounded text is an unbounded synthesis)`,"input");let d=a.model===void 0?void 0:a.model;if(d!==void 0&&typeof d!="string")throw new P(400,"model must be a string","model");let l=a.voice;if(l!==void 0&&(typeof l!="string"||l.trim().length===0))throw new P(400,"voice must be a non-empty string","voice");if(typeof n.clients.speechVoices!="function")throw new P(501,`the speech lane needs a router-backed client (speechVoices) to resolve the voice set GET /v1/models publishes \u2014 refusing loudly rather than validating voices of ${pt} against a stale copy of the list`);let p;try{p=await n.clients.speechVoices(d)}catch(w){throw ct(w)}if(p.defect!==null)throw new P(500,`the voice set for this model cannot be resolved: ${p.defect} \u2014 fix the catalog row; the lane refuses to serve rather than validate against a set nobody vouched`);let m=typeof l=="string"?l:void 0;if(m!==void 0&&p.published.length>0&&!p.published.includes(m))throw new P(400,`voice ${JSON.stringify(m)} is not one of the voices this model publishes \u2014 accepted voices: ${p.published.join(", ")}`,"voice");let c=a.response_format??"wav";if(c!=="wav"&&c!=="pcm")throw new P(400,`response_format ${JSON.stringify(c)} is not supported: this lane serves the engine's raw 24 kHz mono PCM16, so the supported formats are ${Iu} \u2014 and the lane will not label bytes with a format it did not encode`,"response_format");if(a.stream_format!==void 0&&typeof a.stream_format!="string")throw new P(400,"stream_format, when present, must be a string","stream_format");let f=a.speed;if(f!==void 0){if(typeof f!="number"||!Number.isFinite(f))throw new P(400,"speed, when present, must be a number","speed");if(f!==1)throw new P(400,`speed=${JSON.stringify(f)} is not supported: the backend exposes no speed knob (the qwen3 engine's speaking rate is fixed), so a non-default speed would be silently ignored \u2014 1 (the OpenAI default) is the only accepted value`,"speed")}let g=a.instructions;if(g!==void 0){if(typeof g!="string")throw new P(400,"instructions, when present, must be a string","instructions");if(g.trim().length>0)throw new P(400,"instructions are not supported: the backend fixes the voice style (instruct) at deploy time, so a per-request instruction would be silently ignored \u2014 the deployment speaks with its configured style","instructions")}await wu(n.turn,n.clients,d);let _=Buffer.byteLength(JSON.stringify(a));if(n.stats?.request("audio_speech",_,_),!await Eu(n.speechModels,d,e))return;if(typeof n.clients.openAudioOn!="function")throw new P(501,"a speech-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the same session identity as the response turn \u2014 refusing loudly rather than answering an utterance whose audio cannot be collected");if(typeof n.clients.speechSessionHandle!="function")throw new P(501,"a speech-capable model needs a router-backed client (speechSessionHandle) to resolve the speech lane's own pooled session \u2014 one no interactive binding shares, because the per-session audio bridge broadcasts every output frame to every subscriber \u2014 refusing loudly rather than collecting speech audio on a session a realtime/gemini lane can be speaking on");if(typeof n.clients.evictSpeechSession!="function")throw new P(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned speech session after a failed turn (a turn-cap timeout above all) \u2014 without it, the next request on the same model would silently collect the failed turn\u2019s still-emitting audio; refusing loudly rather than serving on an un-evictable speech slot");if(s())return;let y;try{y=await n.clients.speechSessionHandle(d)}catch(w){throw ct(w)}let h;try{h=await n.clients.openAudioOn(y)}catch(w){throw ct(w)}n.turn&&(n.turn.sessionId=h.sessionId);try{let w=await xu(n.clients,h,y,{model:d,text:u,...m!==void 0?{voice:m}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??mu,returnBoundMs:n.turnReturnBoundMs??gu},s);if(w===null||s())return;if(_u(n.turn,w.pcm.byteLength),e.statusCode=200,m===void 0){let A=w.completed[Xt];typeof A=="string"&&e.setHeader(yu,A)}c==="wav"?(e.setHeader("content-type","audio/wav"),e.end(Buffer.concat([ku(w.pcm.byteLength),w.pcm]))):(e.setHeader("content-type","audio/pcm"),e.end(w.pcm))}catch(w){throw ct(w)}}catch(i){if(i instanceof Q){zn(e,new P(413,i.message));return}if(i instanceof P){zn(e,i);return}throw i}}var Zt="/v1/audio/transcriptions";function $s(t){return t===Zt}var Ms="/v1/audio/models",Ou=16*1024*1024,Tu=64*1024*1024,j=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}};function Yn(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}function Ls(t){return t.length>=12&&t[0]===82&&t[1]===73&&t[2]===70&&t[3]===70&&t[8]===87&&t[9]===65&&t[10]===86&&t[11]===69}function Pu(t){if(!Ls(t))return null;let e=12,n=null,r=null;for(;e+8<=t.length;){let s=String.fromCharCode(t[e],t[e+1],t[e+2],t[e+3]),i=(t[e+4]|t[e+5]<<8|t[e+6]<<16|t[e+7]<<24)>>>0,a=e+8;if(s==="fmt "&&a+16<=t.length){let u=t[a+2]|t[a+3]<<8,d=(t[a+4]|t[a+5]<<8|t[a+6]<<16|t[a+7]<<24)>>>0,l=t[a+14]|t[a+15]<<8;n={sampleRate:d,channels:u,bitsPerSample:l}}else if(s==="data"){let u=t.length-a;r=Math.min(i===0||i===4294967295?u:i,u)}e=a+i+i%2}if(n===null||r===null||r===0||n.sampleRate===0||n.channels===0||n.bitsPerSample===0)return null;let o=r/(n.sampleRate*n.channels*(n.bitsPerSample/8));return!Number.isFinite(o)||o<0?null:{seconds:o,sampleRate:n.sampleRate,channels:n.channels,bitsPerSample:n.bitsPerSample}}async function Cu(t,e,n){if(t){if(t.model=ne({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}function $u(t,e){t&&(t.outputUnits=e.seconds)}async function Mu(t,e,n,r,o){let s=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1,...r!==void 0?{language:r}:{}},o),i=[],a=null;for await(let u of s){if(i.push(u),u.type==="error"){let d=u.error??{},l=typeof d.type=="string"?d.type:"upstream_error",p=String(d.message??"upstream reported an error");throw new j(l==="invalid_request_error"?400:502,`transcription upstream error: ${p}`)}if(u.type==="response.completed"){a=u.response??null;break}}if(a===null)throw new j(502,"transcription upstream produced no terminal response \u2014 refusing to fabricate a transcript");return Ve(i,a)}function Us(t){return t instanceof j?t:t instanceof U?new j(404,t.message):new j(502,`transcription upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function Lu(t,e,n){let r;try{r=await t(e)}catch(o){throw Us(o)}return r?!0:(Yn(n,new j(404,e===void 0?"no transcription-capable default model is configured for this caller (catalog task stt) \u2014 name an stt model":`model "${e}" is not transcription-capable (catalog task is not stt) \u2014 this lane serves only speech-to-text models; tts models ride /v1/audio/speech and chat models ride /v1/chat/completions`)),!1)}var Uu=new Set(["file","model","language","response_format"]);async function Ns(t,e,n){let r=n.maxBodyBytes??Tu,o=n.maxAudioPartBytes??Ou;try{if(t.method!=="POST")throw new j(405,`${String(t.method)} is not supported on ${Zt}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new j(400,`the transcriptions endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await Kt(t,r)}catch(y){throw y instanceof Q?new j(413,y.message):new j(400,String(y instanceof Error?y.message:y))}let a=new Map;for(let y of i.keys()){if(!Uu.has(y))throw new j(400,`form field "${y}" is not implemented: the native transcription request carries the audio clip and a language hint only (prompt, temperature, timestamps and similar knobs have no native surface on this lane). Supported fields: file, model, language, response_format.`,y);a.set(y,(a.get(y)??0)+1)}for(let[y,h]of a)if(h>1)throw new j(400,`form field "${y}" appears ${h} times: the lane reads exactly one value per field, and a repeat is refused rather than silently discarded \u2014 resend the request with each field once`,y);let u=i.get("model");if(u!==null&&typeof u!="string")throw new j(400,"model must be a string field","model");let d=u===null?void 0:u,l=i.get("language");if(l!==null&&(typeof l!="string"||l.trim().length===0))throw new j(400,"language, when present, must be a non-empty string","language");let p=l===null?void 0:l,m=i.get("response_format")??"json";if(m!=="json")throw new j(400,`response_format ${JSON.stringify(m)} is not supported: timestamps/segments are not produced on this lane yet \u2014 the runtime answers terminal transcript text only, so json is the only output format`,"response_format");let c=i.get("file");if(!(c instanceof File))throw new j(400,"file must be an uploaded WAV: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths","file");if(c.size>o)throw new j(413,`file part exceeds the ${o}-byte limit (the serve protocol's MAX_AUDIO_BYTES)`,"file");let f=new Uint8Array(await c.arrayBuffer());if(!Ls(f))throw new j(400,"the audio payload is not a RIFF/WAVE container \u2014 this lane accepts WAV uploads only (detected by magic bytes, not the declared Content-Type); the runtime decodes PCM16 WAV and nothing else","file");let g=Pu(f);if(g===null)throw new j(400,"the WAV header cannot be read (no fmt /data chunk pair, or a zero rate/channel/depth) \u2014 the runtime decodes RIFF/WAVE PCM16 and this container cannot be honestly decoded or billed","file");if(await Cu(n.turn,n.clients,d),n.stats?.request("audio_transcriptions",f.length,f.length),!await Lu(n.transcriptionModels,d,e))return;let _=[{type:"input_audio",input_audio:{data:Buffer.from(f).toString("base64"),format:"wav"}}];try{let y=await Mu(n.clients,d,_,p,n.turn?ie(n.turn):void 0);$u(n.turn,g),e.statusCode=200,e.setHeader("content-type","application/json"),e.end(JSON.stringify({text:y}))}catch(y){throw Us(y)}}catch(s){if(s instanceof Q){Yn(e,new j(413,s.message));return}if(s instanceof j){Yn(e,s);return}throw s}}var Fs="/v1/usage/requests",Ds=1e4,Nu=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,Ee=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},ze=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}};var Bs=1024*1024;function Xn(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new Ee(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new Ee(`"${e}" must be an array of uuid strings`);if(n.length>Ds)throw new Ee(`"${e}" carries ${n.length} ids; at most ${Ds} may be looked up in one request. Split the batch \u2014 the ids past the limit are NOT silently dropped`);let r=[],o=new Set;for(let s of n){if(typeof s!="string"||!Nu.test(s))throw new Ee(`"${e}" contains ${JSON.stringify(s)}, which is not a LOWERCASE uuid \u2014 every uRun inference id is the lowercase uuid returned in the \`Inference-Id\` response header, and answering a differently-cased spelling would echo back an id the caller cannot match`);o.has(s)||(o.add(s),r.push(s))}return r}function js(t){return Xn(t,"inference_ids")}function en(t,e){let n=new Set(e),r=new Set;for(let o=0;o<t.length;o++){let s=t[o];if(!n.has(s.inference_id))throw new ze(`the usage surface returned an id at validated answer position ${o} that was not in the request (${e.length} asked, ${t.length} answered) \u2014 an answer carrying ids nobody asked about cannot be attributed to this caller`);if(r.has(s.inference_id))throw new ze(`the usage surface returned ${s.inference_id} more than once \u2014 inference_id is the ledger primary key, so a duplicate means the upstream query is not the one we asked, and a caller that sums the entries would bill the request twice`);r.add(s.inference_id)}}var un=require("@urun-sh/core");var Qn="/partners/huggingface/v1/billing/requests";function Du(t){return Xn(t,"requestIds")}function Fu(t){return t>=200&&t<=399}function Bu(t){let e=[];for(let n of t)n.cost_nano_usd!==null&&Fu(n.http_status)&&e.push({requestId:n.inference_id,costNanoUsd:n.cost_nano_usd});return{requests:e.length>0?e:null}}async function qs(t,e){let n=Du(t),r=await e(n);return en(r,n),Bu(r)}var ju={"/v1/chat/completions":"conversational","/v1/responses":"conversational","/v1/messages":"conversational","/v1/systemone":"conversational","/v1/images/generations":"text-to-image","/v1/images/edits":"image-to-image","/v1/audio/speech":"text-to-speech","/v1/audio/transcriptions":"speech-to-text"};function qu(t){return ju[t]??null}var Js=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function Hu(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function Gu(t){return t==="surface"}function Vu(t){let e=t?.ledgerFailureReason;return Js.includes(e)?e:"unknown"}var Ju=["no_response_head","no_org","model_unresolved","no_model_named","queue_full"],Hs=["priced","unpriced"],Wu={written:Hs,already_written:Hs,skipped:Ju,failed:Js},Ep=Object.entries(Wu).flatMap(([t,e])=>e.map(n=>({outcome:t,reason:n})));function Ku(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function zu(t){let e=Date.parse(t.ts),n=Number.isFinite(t.duration_ms)?Math.max(0,t.duration_ms):0;return new Date(e-Math.round(n)).toISOString()}var Yu=256,Gs=4096,mt=12e4,Xu=250,Qu=8e3,gt=[],Vs=new Set,ht=0,ft=!1;var Zu=new Set;function ed(){return ht===0&&gt.length===0}function td(){if(ed()){ft=!1;for(let t of[...Zu])t()}}function Ws(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
29
- `}function nd(t){de(process.stdout,t)}function Ks(t,e){try{t(e)}catch{}}function zs(t){let{record:e,ledger:n}=t;if(n===null)return;let r=n.report??nd,o=qu(e.path);if(o===null)return;let s=l=>{try{n.observe(l)}catch{}},i=(l,p)=>{s({outcome:"skipped",reason:l}),Ks(r,Ws("inference_ledger_skipped",{inference_id:e.inference_id,path:e.path,reason:l,detail:p}))};if(e.status===null){i("no_response_head","no head flushed, so the caller never received an Inference-Id and no status can be recorded honestly");return}if(e.org===null){i("no_org","the request never resolved a tenant, so there is no org to attribute the row to");return}let a=e.catalog;if(a.kind==="unresolved"){i("model_unresolved",a.reason);return}let u;if(a.kind==="catalog")u={model_id:a.model_id,identity_kind:"catalog_model",variant:a.variant};else{if(a.model===null){i("no_model_named","the request resolved to a caller-org app but named no model, so there is no app slug to record and `model_id` cannot be written honestly");return}u={model_id:a.model,identity_kind:"caller_org_app",variant:null}}let d={inference_id:e.inference_id,...u,task:o,http_status:e.status,started_at:zu(e),completed_at:e.ts,billable_outcome:Ku(e),session_id:e.session_id,prompt_tokens:e.prompt_tokens,completion_tokens:e.completion_tokens,output_units:e.output_units,output_minutes:e.output_minutes,queue_ms:e.queue_ms,prefill_ms:e.prefill_ms,decode_ms:e.decode_ms,total_ms:e.total_ms,ttft_ms:e.ttft_ms===null?null:Math.round(e.ttft_ms),e2e_ms:Math.round(e.duration_ms)};if(gt.length>=Gs){i("queue_full",`${gt.length} ledger writes are already waiting (capacity ${Gs}, ${ht} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}gt.push({entry:d,req:t.req,record:e,ledger:n,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),ht<Yu&&(ht+=1,rd())}async function rd(){try{for(;;){let t=od();if(t===void 0)return;Vs.add(t);try{await sd(t)}finally{Vs.delete(t)}}}finally{ht-=1,td()}}function od(){let t=Date.now();for(;;){let e=gt.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<mt)return e;Ye(e,{outcome:"failed",reason:"dwell_expired"},{event:"inference_ledger_write_failed",reason:"dwell_expired",error:`this row waited ${n}ms for the control plane (bound ${mt}ms) after ${e.attempts} attempt(s) and was given up on \u2014 the request was served and answered, and it will never be billed. Hugging Face writes a request off ~30 minutes after serving, so a row placed later than this is not worth the memory to keep trying`})}}async function sd(t){let{entry:e,record:n,ledger:r}=t;for(;;){t.attempts+=1;try{let o=await r.write(t.req,e);Ye(t,{outcome:"written",reason:o.cost_nano_usd===null?"unpriced":"priced"},{event:"inference_ledger_written",inference_id:o.inference_id,path:n.path,model_id:e.model_id,identity_kind:e.identity_kind,variant:e.variant,org:n.org,session_id:e.session_id,cost_nano_usd:o.cost_nano_usd,price_version:o.price_version,unpriced_reason:o.unpriced_reason,attempts:t.attempts});return}catch(o){let s=Hu(o);if(s!==null){Ye(t,{outcome:"already_written",reason:s.cost_nano_usd===null?"unpriced":"priced"},{event:"inference_ledger_already_written",inference_id:e.inference_id,model_id:e.model_id,identity_kind:e.identity_kind,variant:e.variant,cost_nano_usd:s.cost_nano_usd,price_version:s.price_version,attempts:t.attempts});return}let i=Vu(o),a=Date.now()-t.queuedAt;if(ft){Ye(t,{outcome:"failed",reason:"dropped_on_exit"},{event:"inference_ledger_write_failed",reason:"dropped_on_exit",attempts:t.attempts,waited_ms:a,error:`the proxy is shutting down and this row was still being retried when the drain ran out \u2014 the request was served and answered, and its billing row was never written. Last failure: ${o instanceof Error?o.message:String(o)}`});return}if(Gu(i)&&a<mt){if(await ad(id(t.attempts)),ft||Date.now()-t.queuedAt>=mt){let u=Date.now()-t.queuedAt;Ye(t,{outcome:"failed",reason:ft?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:ft?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:u,error:`this row's backoff outlived the ${mt}ms dwell bound (waited ${u}ms over ${t.attempts} attempt(s)) and was given up on rather than starting an attempt that could only finish past it. Last failure: ${o instanceof Error?o.message:String(o)}`});return}continue}Ye(t,{outcome:"failed",reason:i},{event:"inference_ledger_write_failed",reason:i,attempts:t.attempts,waited_ms:a,error:o instanceof Error?o.message:String(o)});return}}}function id(t){return Math.min(Qu,Xu*2**Math.max(0,t-1))}function ad(t){return new Promise(e=>{setTimeout(e,t).unref?.()})}function Ye(t,e,n){if(t.settled)return;t.settled=!0;try{t.ledger.observe(e)}catch{}let{event:r,...o}=n;Ks(t.report,Ws(r,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...o}))}var er="x-urun-profile",yt="voice";var oe=class extends Error{status=400},ud=/(?:[.!?:;]|\n)[)\]"'\u2019\u201d\u00bb\u300d\u300b\u3011\uff09\uff3d]?\s*$/,dd=/\s/;function ld(t){return ud.test(t)}function Zn(t,e,n){let r=t[e];if(r===void 0)return n;if(typeof r!="boolean")throw new oe(`stream_options.voice.${e} must be a boolean when present (got ${typeof r})`);return r}function Ys(t,e,n,r={}){let o=t[e];if(o===void 0)return n;if(typeof o!="number"||!Number.isFinite(o)||o<=0)throw new oe(`stream_options.voice.${e} must be a positive finite number when present (got ${JSON.stringify(o)})`);if(r.min!==void 0&&o<r.min)throw new oe(`stream_options.voice.${e} must be at least ${r.min} when present (got ${JSON.stringify(o)})${r.minReason?` \u2014 ${r.minReason}`:""}`);if(r.max!==void 0&&o>r.max)throw new oe(`stream_options.voice.${e} must be at most ${r.max} when present (got ${JSON.stringify(o)}) \u2014 larger values wrap Node's setTimeout to a 1ms delay`);return o}function Xs(t,e){let n=Array.isArray(t)?t[0]:t,r=e.stream_options??void 0;if(r!==void 0&&(typeof r!="object"||Array.isArray(r)))throw new oe(`stream_options must be a JSON object when present, e.g. {"profile":"voice","voice":{...}} (got ${JSON.stringify(r)})`);let o=r,s=o?.profile,i=!1;if(n!=null){let d=n.trim();if(d==="")throw new oe(`the ${er} header is empty \u2014 the only profile this lane accepts is "${yt}" (omit the header for the ordinary lane)`);if(d!==yt)throw new oe(`unknown profile "${d}" \u2014 the only profile this lane accepts is "${yt}"`);i=!0}if(s!==void 0){if(typeof s!="string"||s!==yt)throw new oe(`unknown profile ${JSON.stringify(s)} \u2014 the only profile this lane accepts is "${yt}"`);i=!0}if(!i)return null;if(e.stream!==!0)throw new oe("the voice profile is streaming-only \u2014 send stream: true");let a=o?.voice;if(a!==void 0&&(typeof a!="object"||a===null||Array.isArray(a)))throw new oe("stream_options.voice must be an object when present (flush_max_chars, flush_max_ms, sentence_flush, include_reasoning, metadata)");let u=a??{};return{flushMaxChars:Ys(u,"flush_max_chars",120,{min:2,minReason:"a hard split at a bound below 2 cannot keep UTF-16 surrogate pairs (astral characters) intact"}),flushMaxMs:Ys(u,"flush_max_ms",250,{max:2147483647}),sentenceFlush:Zn(u,"sentence_flush",!0),includeReasoning:Zn(u,"include_reasoning",!1),metadata:Zn(u,"metadata",!1)}}var tn=class{constructor(e,n){this.config=e;this.sink=n}config;sink;buffer="";timer=null;push(e){e!==""&&(this.buffer===""&&(this.timer=setTimeout(()=>{this.timer=null,this.flush()},this.config.flushMaxMs)),this.buffer+=e,this.drain())}flush(){if(this.clearTimer(),this.buffer==="")return;let e=this.buffer;this.buffer="",this.sink(e)}dispose(){this.clearTimer()}drain(){let e=this.config.flushMaxChars;for(;this.buffer.length>e;){let n=this.boundedCut();this.sink(this.buffer.slice(0,n)),this.buffer=this.buffer.slice(n)}(this.buffer.length>=e||this.config.sentenceFlush&&ld(this.buffer))&&this.flush(),this.buffer===""&&this.clearTimer()}boundedCut(){let e=Math.min(Math.floor(this.config.flushMaxChars),this.buffer.length);for(let o=e-1;o>0;o--)if(dd.test(this.buffer[o]))return o+1;let n=Math.max(1,e),r=this.buffer.charCodeAt(n-1);return r>=55296&&r<=56319?n-1:n}clearTimer(){this.timer!==null&&(clearTimeout(this.timer),this.timer=null)}};var tr=Ie(require("crypto"),1),cd="urun-runtime-key-v1";function pd(t,e){if(!t)throw new Error("deriveOrgRuntimeKey requires a master secret");if(!e)throw new Error("deriveOrgRuntimeKey requires an orgId");return tr.createHmac("sha256",t).update(`${cd}|${e}`,"ascii").digest("base64url")}var fd="decision-intake",nn=6e4,md=nn/1e3+5,Qs=3,nr=15;function gd(t,e,n=60,r=Math.floor(Date.now()/1e3)){if(!t)throw new Error("cannot mint a decision token without a secret");let o=d=>Buffer.from(d).toString("base64url"),s=o(JSON.stringify({alg:"HS256",typ:"JWT"})),i={tenant:e.tenant,app:e.app,fn:e.fn,...e.provider?{provider:e.provider}:{},session_id:"decision-intake",sub:`machine:${e.tenant}/${e.app}/${e.fn}`,aud:fd,iat:r,exp:r+Math.max(1,n)},a=o(JSON.stringify(i)),u=tr.createHmac("sha256",t).update(`${s}.${a}`,"ascii").digest("base64url");return`${s}.${a}.${u}`}function hd(t){if(t===null||typeof t!="object")return!1;let e=t;return typeof e.runtime_id=="string"&&e.runtime_id!==""&&typeof e.ready=="boolean"&&typeof e.address=="string"&&e.address!==""&&typeof e.last_seen=="string"&&!Number.isNaN(Date.parse(e.last_seen))}async function yd(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/runtimes?app=${encodeURIComponent(t.app)}&fn=${encodeURIComponent(t.fn)}`,r;try{r=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"},signal:AbortSignal.timeout(nn)})}catch(u){throw(u instanceof Error?u.name:void 0)==="TimeoutError"?new fe(`runtime presence read for (app=${t.app} fn=${t.fn}) timed out after ${nn}ms \u2014 the control plane is not answering; no replica can be discovered`):u}if(!r.ok)throw new Error(`runtime presence read failed: GET ${n} \u2192 ${r.status}`);let o=await r.json();if(!Array.isArray(o.runtimes))throw new Error(`runtime presence read returned no "runtimes" array (GET ${n})`);let s=[],i=[];for(let u of o.runtimes){if(!hd(u)){s.push(JSON.stringify(u));continue}i.push(u)}if(s.length>0)throw new Error(`runtime presence read for (app=${t.app} fn=${t.fn}) returned ${s.length} malformed presence row(s) \u2014 the control plane's runtimes schema drifted (runtime_id/address/ready/last_seen are all required): ${s.slice(0,3).join("; ")}`);let a=(t.nowMs??Date.now())/1e3;return i.filter(u=>u.ready===!0&&a-Date.parse(u.last_seen)/1e3<=nr)}var wd=nr*1e3,_d=nr*500,vd=256,rn=class{constructor(e){this.opts=e;this.nowMs=e.nowMs??Date.now}opts;replicas=new Map;nowMs;presenceCache=new Map;get enabled(){return this.opts.secret!==""}async targetFor(e){let n=await this.opts.targetFor(e);return n.sharedApp===!0&&this.opts.shared===void 0?null:n}async decide(e,n){let r=await this.targetFor(n);if(r===null)throw new fe("the shared-endpoint lane keeps the session dial \u2014 the stateless direct path serves caller-org functions");let o=r.appSlug,s=r.fnName??this.opts.fnName,i=r.sharedApp===!0,a=i?this.opts.shared.apiKey:this.opts.apiKey,u=`${a}\0${o}\0${s}`,d=await this.ensureRows(u,a,o,s);if(d.length===0)throw new fe(`no ready runtime for (org app=${o} fn=${s}) \u2014 nothing is serving decisions right now`);let l=null;for(let p=0;p<Qs;p++){let m=this.pick(d);if(m===null)throw new fe(`every discovered runtime for app=${o} failed at the transport layer \u2014 no replica took the decision`);let c=i&&r.sharedOrgId?r.sharedOrgId:this.opts.orgId,f=gd(pd(this.opts.secret,c),{tenant:this.opts.orgId,app:o,fn:s,...i&&r.sharedOrgId?{provider:r.sharedOrgId}:{}},md);try{return{...await this.post(m,e,f),target:{...r,fnName:s}}}catch(g){if(g instanceof Ne)throw g;l=g;let _=this.replicas.get(m)??{outstanding:0,cooldownUntil:0};_.cooldownUntil=this.nowMs()+5e3,this.replicas.set(m,_);let y=this.presenceCache.get(u);y&&(y.rows=y.rows.filter(h=>h.address!==m),y.rows.length===0&&this.presenceCache.delete(u))}}throw new fe(`no runtime for app=${o} answered within ${Qs} attempts: ${String(l instanceof Error?l.message:l)}`)}async ensureRows(e,n,r,o){let s=this.presenceCache.get(e);if(s)if(this.nowMs()-s.fetchedAtMs>wd)this.presenceCache.delete(e);else return this.nowMs()-s.fetchedAtMs>_d&&!s.refreshing&&(s.refreshing=!0,this.readPresence(n,r,o).then(a=>{this.presenceCache.set(e,{rows:a,fetchedAtMs:this.nowMs(),refreshing:!1})}).catch(()=>{s.refreshing=!1})),s.rows;let i=await this.readPresence(n,r,o);for(;this.presenceCache.size>=vd;){let a=this.presenceCache.keys().next();if(a.done)break;this.presenceCache.delete(a.value)}return this.presenceCache.set(e,{rows:i,fetchedAtMs:this.nowMs(),refreshing:!1}),i}readPresence(e,n,r){return yd({apiUrl:this.opts.apiUrl,apiKey:e,app:n,fn:r,fetchImpl:this.opts.fetchImpl,nowMs:this.nowMs()})}pick(e){let n=this.nowMs(),r=null,o=1/0;for(let s of e){let i=this.replicas.get(s.address);if(i!==void 0&&i.cooldownUntil>n)continue;let a=i?.outstanding??0;a<o&&(r=s.address,o=a)}return r}async post(e,n,r){let o=this.replicas.get(e)??{outstanding:0,cooldownUntil:0};o.outstanding+=1,this.replicas.set(e,o);try{let s=await fetch(`${e.replace(/\/+$/,"")}/v1/decision`,{method:"POST",headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},body:JSON.stringify(n),signal:AbortSignal.timeout(this.opts.timeoutMs??nn)}),i=null;try{i=await s.json()}catch{i={error:{message:`runtime answered ${s.status} with a non-JSON body`,type:"upstream_error"}}}if(s.status>=500)throw new Error(`runtime ${e} answered ${s.status}`);if(s.status>=400)throw new Ne(s.status,i);return{status:s.status,body:i,completed:bd(i)}}finally{o.outstanding-=1}}};function bd(t){if(t===null||typeof t!="object")return null;let e=t,n=e.usage;if(n===null||typeof n!="object")return e;let r=n;return{...e,usage:{input_tokens:r.prompt_tokens??r.input_tokens,output_tokens:r.completion_tokens??r.output_tokens}}}var Ne=class extends Error{constructor(n,r){super(`runtime answered ${n}`);this.status=n;this.body=r}status;body},fe=class extends Error{};function sr(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function kd(t){t.writeHead(200,{"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache",connection:"keep-alive","x-accel-buffering":"no"}),t.flushHeaders(),t.socket?.setNoDelay(!0)}function J(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function K(t,e,n,r="invalid_request_error",o){J(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function ir(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);J(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function me(t,e,n,r){J(t,e,{type:"error",error:{type:n,message:r}})}function ge(t,e,n){if(e==="anthropic"){J(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}J(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var Sd=64*1024*1024,wt=class extends Error{};async function Zs(t,e=Sd){let n=[],r=0;for await(let s of t){if(r+=s.length,r>e)throw new wt(`request body exceeds ${e} bytes`);n.push(s)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function De(t,e,n,r){return{id:t,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:e,choices:[{index:0,delta:n,finish_reason:r}]}}function ni(t){let e=t?.usage;if(!e||typeof e!="object")return{kind:"absent"};let n=e,r=u=>typeof u=="number"&&Number.isFinite(u)&&u>=0?u:null,o=r(n.input_tokens),s=r(n.output_tokens),i=u=>u??0,a=r(n.total_tokens)??i(o)+i(s);if(i(o)>0||i(s)>0){let u={input_tokens:i(o),output_tokens:i(s),total_tokens:a};return o===null||s===null?{kind:"partial",usage:u}:{kind:"counts",usage:u}}return o===null||s===null?{kind:"absent"}:{kind:"zero",usage:{input_tokens:0,output_tokens:0,total_tokens:a}}}function on(t){let e=ni(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function ei(t){let e=on(t);return e?{prompt_tokens:e.input_tokens,completion_tokens:e.output_tokens,total_tokens:e.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function Rd(t,e){let n=ni(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function xd(t){let e=t?.urun_timing;if(!e||typeof e!="object")return null;let n=e,r=u=>{let d=n[u];return typeof d=="number"&&Number.isSafeInteger(d)&&d>=0?d:null},o=r("queue_ms"),s=r("prefill_ms"),i=r("decode_ms"),a=r("total_ms");return o===null||s===null||i===null||a===null?null:{queue_ms:o,prefill_ms:s,decode_ms:i,total_ms:a}}function Ed(t,e){let n=xd(e);n&&(t.timing=n)}function _e(t,e){Rd(t,e),Ed(t,e)}async function dn(t,e,n){if(typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}function ri(t){let e=t?.output;if(!Array.isArray(e))return"";let n="";for(let r of e)if(!(r.type!=="reasoning"||!Array.isArray(r.content)))for(let o of r.content)o.type==="reasoning_text"&&(n+=String(o.text??""));return n}function Ad(t){if(Array.isArray(t))return t.map(e=>{let n=e.function;return!n||typeof n!="object"?e:{type:"function",name:n.name,description:n.description,parameters:n.parameters,strict:n.strict}})}var sn=class extends Error{};function Id(t,e){return Array.isArray(e)?e.map(n=>{let r=n;if(r.type==="text")return{type:t==="assistant"?"output_text":"input_text",text:r.text};if(r.type==="image_url"){let o=r.image_url??{};return{type:"input_image",image_url:o.url,detail:o.detail}}throw new sn(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function Od(t){let e=[];for(let n of t){if(n.role==="tool"){e.push({type:"function_call_output",call_id:n.tool_call_id,output:typeof n.content=="string"?n.content:JSON.stringify(n.content??"")});continue}if(n.role==="assistant"&&Array.isArray(n.tool_calls)){typeof n.content=="string"&&n.content&&e.push({role:"assistant",content:n.content});for(let r of n.tool_calls){let o=r.function??{};e.push({type:"function_call",call_id:r.id,name:o.name,arguments:o.arguments})}continue}e.push({...n,content:Id(n.role,n.content)})}return e}function rr(t){return He(t).map((e,n)=>({index:n,id:e.call_id??e.id??`call_${n}`,type:"function",function:{name:e.name??"",arguments:e.arguments??"{}"}}))}async function Td(t,e,n,r,o,s){let i=st(s.id),a=t.stream===!0;if(s.model=ne(t),s.stream=a,await dn(s,e,typeof t.model=="string"?t.model:void 0),a){let y;try{y=await at({body:t,clients:e,store:n,stats:o,responseId:i,turn:s})}catch(h){if(h instanceof U){ge(r,"openai",h);return}if(h instanceof ae){K(r,h.status,h.message);return}throw h}sr(r);for await(let h of y.events)h.type==="error"&&(s.streamError=!0),h.type==="response.completed"&&_e(s,h.response),r.write(`event: ${String(h.type??"message")}
26
+ ${lu(n)}\r
27
+ `),t.destroy()}function pu(t){return`${JSON.stringify({event:"realtime_upgrade",ts:new Date().toISOString(),...t})}
28
+ `}function fu(t){de(process.stdout,t)}function mu(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function Wn(t){return t.audioSession?.audio.sessionId??void 0}function zn(t,e){let n=new fs.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,s,i)=>{if(new URL(o.url??"/","http://localhost").pathname!==Kn)return;r.add(s),s.on("close",()=>{r.delete(s)});let u=E("rtup"),d=mu(o),l=w=>{(e.log??fu)(pu(w))};l({stage:"accept",conn:u,model:d}),s.resume();let p=new AbortController,m=null,c=w=>{m??=w,p.abort(new Error(m))},f=()=>c("client socket closed"),g=()=>c("client socket closed (half-close)"),b=w=>c(`client socket error: ${w.message}`);s.once("close",f),s.once("end",g),s.once("error",b);let y=()=>{s.removeListener("close",f),s.removeListener("end",g),s.removeListener("error",b)},h=w=>{l({stage:"close",conn:u,model:d,...w})},v=w=>{let _=w,x=_.session!=null&&typeof _.session.sendResponseCreate=="function",C=_.clients!=null&&typeof _.clients.createResponse=="function";if(!x&&!C)throw new Error("resolver returned a binding without an upstream turn seam (session.sendResponseCreate or clients.createResponse required) \u2014 refusing the upgrade LOUD");return new Promise((S,R)=>{n.handleUpgrade(o,s,i,$=>{try{new ct({ws:$,binding:w,onClosed:(F,Y)=>{h({while:"live",reason:Y.reason})}}),l({stage:"admit",conn:u,model:d,...Wn(w)===void 0?{}:{session:Wn(w)}}),S()}catch(F){R(F)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o,p.signal).then(async w=>{if(p.signal.aborted){y();let _=Wn(w);try{await w.release?.()}catch(x){h({while:"queued",..._===void 0?{}:{session:_},reason:m??"client socket closed",release_failed:x instanceof Error?x.message:String(x)});return}h({while:"queued",..._===void 0?{}:{session:_},reason:m??"client socket closed",released:!0}),s.destroy();return}y(),await v(w)}).catch(w=>{let _=w instanceof ps.SessionFailedError?jt(w):null,x=null;if(w!==null&&typeof w=="object"&&"status"in w){let R=w.status;typeof R=="number"&&(x=R)}let C=x??_?.status??502,S=w instanceof Error?w.message:String(w);h({while:"queued",...p.signal.aborted?{reason:m??"client socket closed"}:{reason:S,status:C}}),s.destroyed||cu(s,C,S)}).finally(()=>{r.delete(s)})}),async()=>{let o=Date.now()+5e3;for(;r.size>0&&Date.now()<o;)await new Promise(s=>setTimeout(s,25));await new Promise(s=>n.close(()=>s()))}}var le=(t,e)=>Object.assign(new Error(e),{status:t});function gu(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function hu(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("intent")?.trim();return n===""?void 0:n}var yu='"transcription" (a transcription session), or no intent at all (the realtime voice session)';async function wu(t,e,n,r){if(!t.supportsAudio)throw le(501,"the realtime lane needs a router-backed client (supportsAudio) to gate audio modality \u2014 hand-built clients without the catalog oracle are refused, never allowed silently");let o;try{o=await t.supportsAudio(e)}catch(d){throw d instanceof U?le(404,d.message):d}if(!o)throw le(404,`model ${JSON.stringify(e??"(default)")} is not audio-capable (catalog task is neither stt nor tts) \u2014 this realtime lane serves only audio-task models; chat models ride /v1/chat/completions`);if(!t.openAudioOn)throw le(501,"an audio-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the connection\u2019s pooled session \u2014 refusing loudly rather than binding a voiceless session");let s=!1;if(t.supportsTranscription)try{s=await t.supportsTranscription(e)}catch(d){throw d instanceof U?le(404,d.message):d}if(s&&!t.openTurnsOn)throw le(501,"a transcription-capable (stt) model needs a router-backed client (openTurnsOn) to open the engine\u2019s own turn/transcript lane (the stt named stream) pinned to the connection\u2019s pooled session \u2014 refusing loudly rather than binding a transcript-less transcription session");if(s&&t.transcriptStreamFor){let d;try{d=await t.transcriptStreamFor(e)}catch(l){throw l instanceof U?le(404,l.message):l}if(d!=="stt")throw le(501,`model ${JSON.stringify(e??"(default)")} overrides engine_args.transcript_stream with ${JSON.stringify(d)} \u2014 this lane consumes the engine\u2019s own transcripts from the platform\u2019s default "${"stt"}" stream and refuses loudly rather than listen on a stream the engine does not write`)}if(r!==void 0&&r!=="transcription")throw le(400,`intent ${JSON.stringify(r)} is not supported on this realtime lane \u2014 accepted values: ${yu}`);if(r==="transcription"&&!s)throw le(400,`model ${JSON.stringify(e??"(default)")} cannot serve the transcription intent \u2014 the transcription intent needs an stt model (the catalog task is not stt)`);let i=await t.sessionHandle(e),a={audio:await t.openAudioOn(i,n)},u={clients:{createResponse:async d=>await t.createResponseOn(i,d)},voice:o,audioSession:a};return s&&(u.turns=d=>t.openTurnsOn(i,d)),r==="transcription"&&(u.intent="transcription"),e!==void 0&&(u.model=e),u}function ms(t,e){return{resolve:async(n,r)=>{if(e!==void 0){let o=n.headers.authorization,s=typeof o=="string"?o.trim():"",i=s.startsWith("Bearer ")?s.slice(7).trim():"";if(i===""||i!==e)throw le(401,"invalid local proxy api key")}return wu(t,gu(n),r,hu(n))}}}var Q=class extends Error{};async function Kt(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new Q(`request body exceeds the ${e}-byte limit`);let r=[],o=0;for await(let s of t){let i=s;if(o+=i.length,o>e)throw new Q(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function zt(t,e){let n=t[Symbol.asyncIterator](),r=0,o=new ReadableStream({async pull(i){try{let a=await n.next();if(a.done){i.close();return}if(r+=a.value.length,r>e){i.error(new Q(`request body exceeds the ${e}-byte limit`));return}i.enqueue(a.value)}catch(a){i.error(a)}},cancel(){n.return?.()}}),s;try{let i=new Headers;for(let[d,l]of Object.entries(t.headers))if(typeof l=="string")i.set(d,l);else if(Array.isArray(l))for(let p of l)i.append(d,p);let a={method:"POST",headers:i,body:o,duplex:"half"};s=await new Request(`https://proxy.internal${t.url??"/"}`,a).formData()}catch(i){throw i instanceof Q?i:new Error(`malformed multipart body: ${String(i instanceof Error?i.message:i)}`)}return s}var gs="/v1/images/generations",Yt="/v1/images/edits";function hs(t){return t===gs||t===Yt}var ys="/v1/images/models";async function ws(t,e,n){if(t){if(t.model=ne({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}var M=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},_u=16*1024*1024,_s=64*1024*1024;function vs(t){return t.length>=8&&t[0]===137&&t[1]===80&&t[2]===78&&t[3]===71&&t[4]===13&&t[5]===10&&t[6]===26&&t[7]===10?"image/png":t.length>=3&&t[0]===255&&t[1]===216&&t[2]===255?"image/jpeg":t.length>=12&&t[0]===82&&t[1]===73&&t[2]===70&&t[3]===70&&t[8]===87&&t[9]===69&&t[10]===66&&t[11]===80?"image/webp":null}function pt(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}async function bs(t,e,n,r){let o=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},r),s=null;for await(let p of o){if(p.type==="error"){let m=p.error??{};throw new M(502,`image upstream error: ${String(m.message??"upstream reported an error")}`)}if(p.type==="response.completed"){s=p.response??null;break}}if(s===null)throw new M(502,"image upstream produced no terminal response \u2014 refusing to fabricate an image");let i=s.images;if(!Array.isArray(i)||i.length!==1||typeof i[0]!="string"||i[0].length===0)throw new M(502,`image upstream returned ${Array.isArray(i)?i.length:"a non-array images field"} images for the single-shot (n=1) contract, which produces exactly one \u2014 refusing to truncate or fabricate`);let a=[],u=i[0].startsWith("data:")?i[0].slice(i[0].indexOf(";base64,")+8):i[0],d=Buffer.from(u,"base64");if(vs(d)===null)throw new M(502,`image upstream returned a payload that is not a PNG/JPEG/WebP image (${d.length} bytes) \u2014 refusing to pass it through`);return a.push(u),{images:a}}function ks(t){return t instanceof M?t:t instanceof U?new M(404,t.message):new M(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function Ss(t,e,n,r){if(await t(e,n))return!0;let o=n==="edit"?"no image-input (expects_image) edit destination":"no pure text-to-image (expects_image=false) generation destination";return pt(r,new M(404,e===void 0?`no image-capable default model is configured for this caller (${o}) \u2014 name an image-capable \`model\``:`model "${e}" is not ${n==="edit"?"edit-capable":"generation-capable"} for this caller (${o}; capability is read from the catalog's modality metadata, not the serve function name)`)),!1}function xs(t,e){t&&(t.outputUnits=e.images.length)}function Rs(t,e){t.statusCode=200,t.setHeader("content-type","application/json"),t.end(JSON.stringify({created:Math.floor(Date.now()/1e3),data:e.images.map(n=>({b64_json:n}))}))}var vu={model:!0,prompt:!0,response_format:!0,n:!0};async function Es(t,e,n){let r=n.maxBodyBytes??_s;try{if(t.method!=="POST")throw new M(405,`${String(t.method)} is not supported on ${gs}`);let o;try{o=JSON.parse((await Kt(t,r)).toString("utf8")||"{}")}catch(p){throw p instanceof Q?new M(413,p.message):new M(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new M(400,"request body must be a JSON object");let s=o;for(let p of Object.keys(s))if(!vu[p])throw new M(400,`request field "${p}" is not implemented: the native image request schema carries only the prompt (size, quality, style, seed, mask and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, n.`,p);let i=s.prompt;if(typeof i!="string"||i.trim().length===0)throw new M(400,"prompt is required and must be a non-empty string","prompt");let a=s.model===void 0?void 0:s.model;if(a!==void 0&&typeof a!="string")throw new M(400,"model must be a string","model");await ws(n.turn,n.clients,a);let u=s.n??1;if(u!==1)throw new M(400,`n=${JSON.stringify(u)} is not supported: the native image request is single-shot and produces exactly one image per request (no batch support yet)`,"n");let d=s.response_format??"b64_json";if(d!=="b64_json")throw new M(400,`response_format ${JSON.stringify(d)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");if(n.stats?.request("images_generations",i.length,i.length),!await Ss(n.imageModels,a,"generation",e))return;let l=[{type:"input_text",text:i}];try{let p=await bs(n.clients,a,l,n.turn?ie(n.turn):void 0);xs(n.turn,p),Rs(e,p)}catch(p){throw ks(p)}}catch(o){if(o instanceof Q){pt(e,new M(413,o.message));return}if(o instanceof M){pt(e,o);return}throw o}}var bu={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function As(t,e,n){let r=n.maxBodyBytes??_s,o=n.maxImagePartBytes??_u;try{if(t.method!=="POST")throw new M(405,`${String(t.method)} is not supported on ${Yt}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new M(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await zt(t,r)}catch(c){throw c instanceof Q?new M(413,c.message):new M(400,String(c instanceof Error?c.message:c))}let a=i.get("prompt");if(typeof a!="string"||a.trim().length===0)throw new M(400,"prompt is required and must be a non-empty string field","prompt");let u=i.get("model");if(u!==null&&typeof u!="string")throw new M(400,"model must be a string field","model");let d=u===null?void 0:u;await ws(n.turn,n.clients,d);let l=i.get("response_format")??"b64_json";if(l!=="b64_json")throw new M(400,`response_format ${JSON.stringify(l)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");for(let c of i.keys())if(!bu[c])throw new M(400,`form field "${c}" is not implemented: the native image request schema carries the prompt and the image inputs only (mask, size, seed and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, image (or image[]).`,c);let p=[];for(let c of["image","image[]"])for(let f of i.getAll(c)){if(!(f instanceof File))throw new M(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",c);if(f.size>o)throw new M(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,c);let g=new Uint8Array(await f.arrayBuffer()),b=vs(g);if(b===null)throw new M(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",c);p.push({bytes:g,mime:b})}if(p.length===0)throw new M(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",a.length,a.length),!await Ss(n.imageModels,d,"edit",e))return;let m=[{type:"input_text",text:a}];for(let{bytes:c,mime:f}of p)m.push({type:"input_image",image_url:`data:${f};base64,${Buffer.from(c).toString("base64")}`});try{let c=await bs(n.clients,d,m,n.turn?ie(n.turn):void 0);xs(n.turn,c),Rs(e,c)}catch(c){throw ks(c)}}catch(s){if(s instanceof Q){pt(e,new M(413,s.message));return}if(s instanceof M){pt(e,s);return}throw s}}var ft="/v1/audio/speech";function Us(t){return t===ft}var Is=4096,Os=18e4,Ts=5e3,ku=64*1024*1024,Ps=8,Cs=5,$s=1024*1024,Ye="voice_applied",Qt="X-Urun-Voice-Applied",P=class extends Error{status;param;retryAfterSeconds;constructor(e,n,r,o){super(n),this.status=e,this.param=r,this.retryAfterSeconds=o}};function Yn(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),e.retryAfterSeconds!==void 0&&t.setHeader("retry-after",String(e.retryAfterSeconds)),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}var Zt=class extends P{constructor(e){super(502,e)}};function Ns(t){for(let e=0;e<t.length;e+=1){let n=t.charCodeAt(e);if(n!==9&&!(n>=32&&n<=126)&&!(n>=128&&n<=255))return!1}return!0}function Ds(t){return new P(502,`the runtime's ${Ye} receipt ${JSON.stringify(t)} cannot be relayed as the ${Qt} header \u2014 it carries a character no HTTP header or trailer value can carry (a newline, another control character, or a non-Latin-1 character); the lane never serves an answer whose receipt it cannot deliver nor silently drops the runtime's own acknowledgment`)}async function Su(t,e,n){if(t){if(t.model=ne({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}function Fs(t,e){if(!t)return;let n=e/(24e3*2);t.outputUnits=n,t.outputMinutes=n/60}var xu=2;function Ru(t){for(let e=0;e+1<t.byteLength;e+=2)if(Math.abs(t.readInt16LE(e))>xu)return!1;return!0}function Eu(t){let e=24e3,n=2,r=1,o=Buffer.alloc(44);return o.write("RIFF",0,"ascii"),o.writeUInt32LE(36+t,4),o.write("WAVE",8,"ascii"),o.write("fmt ",12,"ascii"),o.writeUInt32LE(16,16),o.writeUInt16LE(1,20),o.writeUInt16LE(r,22),o.writeUInt32LE(e,24),o.writeUInt32LE(e*r*n,28),o.writeUInt16LE(r*n,32),o.writeUInt16LE(16,34),o.write("data",36,"ascii"),o.writeUInt32LE(t,40),o}var Ms=new WeakMap,Xt=new WeakMap;function Au(t,e){let n=Xt.get(t)??0;if(n>=Ps)throw new P(503,`this model's speech queue is full \u2014 ${Ps} requests are already admitted (one in-flight turn plus the queued depth) and each holds a pooled session turn; retry after ${Cs}s`,void 0,Cs);Xt.set(t,n+1);let r=Ms.get(t)??Promise.resolve(),o,s=new Promise(i=>{o=i});return Ms.set(t,s),(async()=>{await r;try{return await e()}finally{Xt.set(t,(Xt.get(t)??1)-1),o()}})()}async function Iu(t,e,n,r){if(typeof t.evictSpeechSession!="function")throw new P(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned session after a failed turn \u2014 refusing loudly rather than handing the next request the failed turn\u2019s audio");try{await t.evictSpeechSession(e)}catch(i){if(!(i instanceof X))throw i}if(n?.return===void 0)return;let o=Promise.resolve(n.return()).then(()=>{},()=>{}),s;await Promise.race([o,new Promise(i=>{s=setTimeout(i,r)})]).finally(()=>{s!==void 0&&clearTimeout(s)})}function Ne(t){return t instanceof P?t:t instanceof U?new P(404,t.message):new P(502,`speech upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function Ls(t,e,n,r,o,s,i,a){let u=a??(()=>{});return Au(e,async()=>{if(i?.()===!0)return null;let d=!1,l=!1,p=!1,m,c=new Promise(w=>{m=w}),f=[],g=()=>{l&&!p&&f.length>=o.quietRunFrames&&(p=!0,m())},b=e.onOutputAudio(w=>{let _=Buffer.from(w,"base64");if(_.byteLength===0)return;let x=Ru(_);if(!d){if(x)return;d=!0}if(x){f.push(_),g();return}for(let C of f)u(C);f.length=0,u(_),g()}),y=null,h,v,A;try{A=(await t.createResponseOn(n,{model:r.model,input:[{role:"user",content:[{type:"input_text",text:r.text}]}],stream:!1,...r.voice!==void 0?{voice:r.voice}:{}}))[Symbol.asyncIterator]();let _=new Promise(S=>{v=setTimeout(S,s.capMs)}),x=!1;for(;;){let S=A.next(),R=await Promise.race([S,_]);if(R===void 0){x=!0,S.catch(()=>{});break}if(R.done)break;let $=R.value;if($.type==="error"){let F=$.error??{};throw await A.return?.(),new P(502,`speech upstream error: ${String(F.message??"upstream reported an error")}`)}if($.type==="response.completed"){y=$.response??null,l=!0,g(),await A.return?.();break}}if(clearTimeout(v),x)throw new P(504,`the speech turn produced no terminal event within ${s.capMs}ms \u2014 the upstream stream never completed, so the pinned speech session is evicted (the next request opens a fresh one), the serialized lane is released and the caller can retry; a stuck turn must not hold every later request on this lane`);if(y===null)throw new P(502,"speech upstream produced no terminal response \u2014 refusing to fabricate audio");let C=!1;if(await Promise.race([c,new Promise(S=>{h=setTimeout(()=>{C=!0,S()},o.capMs)})]),h!==void 0&&clearTimeout(h),r.voice!==void 0){let S=y[Ye];if(typeof S!="string"||S!==r.voice){let R=`the runtime did not acknowledge the requested voice ${JSON.stringify(r.voice)} \u2014 the terminal response carried ${typeof S=="string"?`${Ye} ${JSON.stringify(S)}`:`no ${Ye} field`}; the speak-bridge companion is not deployed, and this lane never serves default-voice audio as the requested voice`;throw p?new Zt(R):new P(502,R)}}if(!d)throw new P(502,"the upstream produced no voice for this utterance \u2014 refusing to serve silence as success");if(C&&!p&&f.length<1)throw new P(502,`the upstream audio stream did not quiesce within ${o.capMs}ms of the turn's end (voiced frames still arriving) \u2014 refusing to serve audio whose tail cannot be vouched for`);return y}catch(w){throw w instanceof Zt||await Iu(t,n,A,s.returnBoundMs),w}finally{h!==void 0&&clearTimeout(h),v!==void 0&&clearTimeout(v),b()}})}function Ou(t,e,n,r){let o=!1,s=0,i=a=>{t.destroy(a instanceof Error?a:new Error(String(a)))};return{sink(a){n()||(o||(o=!0,t.statusCode=200,t.setHeader("content-type","audio/pcm"),r&&t.setHeader("trailer",Qt)),t.write(a,u=>{u||(s+=a.byteLength,Fs(e,s))}),t.writableLength>$s&&(e&&(e.streamError=!0),i(new Error(`the streamed pcm answer's undrained buffer crossed ${$s} bytes (1 MiB \u2014 about 21 s of 24 kHz mono PCM16): the caller stopped reading the stream, and the lane bounds what it will buffer for one answer rather than let a stalled reader hold the process's memory; the transfer is aborted (an after-first-byte failure \u2014 the bytes whose writes the socket confirmed are billed) and the caller should retry when it can drain the audio as it arrives`))))},started:()=>o,async finish(a){if(!n()){if(r){let u=a[Ye];if(typeof u=="string"){if(!Ns(u))throw Ds(u);t.addTrailers({[Qt]:u})}}await new Promise(u=>{t.end(()=>u())})}},fail:i}}async function Tu(t,e,n){let r;try{r=await t(e)}catch(o){throw Ne(o)}return r?!0:(Yn(n,new P(404,e===void 0?"no speech-capable default model is configured for this caller (catalog task tts) \u2014 name a tts model":`model "${e}" is not speech-capable (catalog task is not tts) \u2014 this lane serves only text-to-speech models; chat models ride /v1/chat/completions`)),!1)}var Pu={model:!0,input:!0,voice:!0,response_format:!0,stream_format:!0,speed:!0,instructions:!0},Cu="wav, pcm";async function Bs(t,e,n){let r=n.maxBodyBytes??ku,o=!1;e.once("close",()=>{o=!0});let s=()=>o||e.destroyed;try{if(t.method!=="POST")throw new P(405,`${String(t.method)} is not supported on ${ft}`);let i;try{i=JSON.parse((await Kt(t,r)).toString("utf8")||"{}")}catch(v){throw v instanceof Q?new P(413,v.message):new P(400,"request body is not valid JSON")}if(i===null||typeof i!="object"||Array.isArray(i))throw new P(400,"request body must be a JSON object");let a=i;for(let v of Object.keys(a))if(!Pu[v])throw new P(400,`request field "${v}" is not implemented: the speech request schema carries the utterance and its voice (the qwen3 backend fixes voice style at deploy time, and sample rate is pinned to the engine's native 24 kHz mono). Supported fields: model, input, voice, response_format, stream_format, speed, instructions.`,v);let u=a.input;if(typeof u!="string"||u.trim().length===0)throw new P(400,"input is required and must be a non-empty string","input");if(u.length>Is)throw new P(400,`input is ${u.length} chars \u2014 over the ${Is}-char limit (one utterance; the backend bounds generation text-shaped, and an unbounded text is an unbounded synthesis)`,"input");let d=a.model===void 0?void 0:a.model;if(d!==void 0&&typeof d!="string")throw new P(400,"model must be a string","model");let l=a.voice;if(l!==void 0&&(typeof l!="string"||l.trim().length===0))throw new P(400,"voice must be a non-empty string","voice");if(typeof n.clients.speechVoices!="function")throw new P(501,`the speech lane needs a router-backed client (speechVoices) to resolve the voice set GET /v1/models publishes \u2014 refusing loudly rather than validating voices of ${ft} against a stale copy of the list`);let p;try{p=await n.clients.speechVoices(d)}catch(v){throw Ne(v)}if(p.defect!==null)throw new P(500,`the voice set for this model cannot be resolved: ${p.defect} \u2014 fix the catalog row; the lane refuses to serve rather than validate against a set nobody vouched`);let m=typeof l=="string"?l:void 0;if(m!==void 0&&p.published.length>0&&!p.published.includes(m))throw new P(400,`voice ${JSON.stringify(m)} is not one of the voices this model publishes \u2014 accepted voices: ${p.published.join(", ")}`,"voice");let c=a.response_format??"wav";if(c!=="wav"&&c!=="pcm")throw new P(400,`response_format ${JSON.stringify(c)} is not supported: this lane serves the engine's raw 24 kHz mono PCM16, so the supported formats are ${Cu} \u2014 and the lane will not label bytes with a format it did not encode`,"response_format");if(a.stream_format!==void 0&&typeof a.stream_format!="string")throw new P(400,"stream_format, when present, must be a string","stream_format");let f=a.speed;if(f!==void 0){if(typeof f!="number"||!Number.isFinite(f))throw new P(400,"speed, when present, must be a number","speed");if(f!==1)throw new P(400,`speed=${JSON.stringify(f)} is not supported: the backend exposes no speed knob (the qwen3 engine's speaking rate is fixed), so a non-default speed would be silently ignored \u2014 1 (the OpenAI default) is the only accepted value`,"speed")}let g=a.instructions;if(g!==void 0){if(typeof g!="string")throw new P(400,"instructions, when present, must be a string","instructions");if(g.trim().length>0)throw new P(400,"instructions are not supported: the backend fixes the voice style (instruct) at deploy time, so a per-request instruction would be silently ignored \u2014 the deployment speaks with its configured style","instructions")}await Su(n.turn,n.clients,d);let b=Buffer.byteLength(JSON.stringify(a));if(n.stats?.request("audio_speech",b,b),!await Tu(n.speechModels,d,e))return;if(typeof n.clients.openAudioOn!="function")throw new P(501,"a speech-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the same session identity as the response turn \u2014 refusing loudly rather than answering an utterance whose audio cannot be collected");if(typeof n.clients.speechSessionHandle!="function")throw new P(501,"a speech-capable model needs a router-backed client (speechSessionHandle) to resolve the speech lane's own pooled session \u2014 one no interactive binding shares, because the per-session audio bridge broadcasts every output frame to every subscriber \u2014 refusing loudly rather than collecting speech audio on a session a realtime/gemini lane can be speaking on");if(typeof n.clients.evictSpeechSession!="function")throw new P(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned speech session after a failed turn (a turn-cap timeout above all) \u2014 without it, the next request on the same model would silently collect the failed turn\u2019s still-emitting audio; refusing loudly rather than serving on an un-evictable speech slot");if(s())return;let y;try{y=await n.clients.speechSessionHandle(d)}catch(v){throw Ne(v)}let h;try{h=await n.clients.openAudioOn(y)}catch(v){throw Ne(v)}n.turn&&(n.turn.sessionId=h.sessionId);try{if(c==="wav"){let v=[],A=await Ls(n.clients,h,y,{model:d,text:u,...m!==void 0?{voice:m}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??Os,returnBoundMs:n.turnReturnBoundMs??Ts},s,_=>v.push(_));if(A===null||s())return;let w=Buffer.concat(v);if(m===void 0){let _=A[Ye];if(typeof _=="string"){if(!Ns(_))throw Ds(_);e.setHeader(Qt,_)}}Fs(n.turn,w.byteLength),e.statusCode=200,e.setHeader("content-type","audio/wav"),e.end(Buffer.concat([Eu(w.byteLength),w]))}else{let v=Ou(e,n.turn,s,m===void 0),A;try{A=await Ls(n.clients,h,y,{model:d,text:u,...m!==void 0?{voice:m}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??Os,returnBoundMs:n.turnReturnBoundMs??Ts},s,v.sink)}catch(w){let _=Ne(w);if(!v.started())throw _;n.turn&&(n.turn.streamError=!0),v.fail(_);return}if(A===null)return;try{await v.finish(A)}catch(w){let _=Ne(w);n.turn&&(n.turn.streamError=!0),v.fail(_);return}}}catch(v){throw Ne(v)}}catch(i){if(i instanceof Q){Yn(e,new P(413,i.message));return}if(i instanceof P){Yn(e,i);return}throw i}}var en="/v1/audio/transcriptions";function js(t){return t===en}var qs="/v1/audio/models",$u=16*1024*1024,Mu=64*1024*1024,j=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}};function Xn(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}function Hs(t){return t.length>=12&&t[0]===82&&t[1]===73&&t[2]===70&&t[3]===70&&t[8]===87&&t[9]===65&&t[10]===86&&t[11]===69}function Lu(t){if(!Hs(t))return null;let e=12,n=null,r=null;for(;e+8<=t.length;){let s=String.fromCharCode(t[e],t[e+1],t[e+2],t[e+3]),i=(t[e+4]|t[e+5]<<8|t[e+6]<<16|t[e+7]<<24)>>>0,a=e+8;if(s==="fmt "&&a+16<=t.length){let u=t[a+2]|t[a+3]<<8,d=(t[a+4]|t[a+5]<<8|t[a+6]<<16|t[a+7]<<24)>>>0,l=t[a+14]|t[a+15]<<8;n={sampleRate:d,channels:u,bitsPerSample:l}}else if(s==="data"){let u=t.length-a;r=Math.min(i===0||i===4294967295?u:i,u)}e=a+i+i%2}if(n===null||r===null||r===0||n.sampleRate===0||n.channels===0||n.bitsPerSample===0)return null;let o=r/(n.sampleRate*n.channels*(n.bitsPerSample/8));return!Number.isFinite(o)||o<0?null:{seconds:o,sampleRate:n.sampleRate,channels:n.channels,bitsPerSample:n.bitsPerSample}}async function Uu(t,e,n){if(t){if(t.model=ne({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}function Nu(t,e){t&&(t.outputUnits=e.seconds)}async function Du(t,e,n,r,o){let s=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1,...r!==void 0?{language:r}:{}},o),i=[],a=null;for await(let u of s){if(i.push(u),u.type==="error"){let d=u.error??{},l=typeof d.type=="string"?d.type:"upstream_error",p=String(d.message??"upstream reported an error");throw new j(l==="invalid_request_error"?400:502,`transcription upstream error: ${p}`)}if(u.type==="response.completed"){a=u.response??null;break}}if(a===null)throw new j(502,"transcription upstream produced no terminal response \u2014 refusing to fabricate a transcript");return Je(i,a)}function Gs(t){return t instanceof j?t:t instanceof U?new j(404,t.message):new j(502,`transcription upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function Fu(t,e,n){let r;try{r=await t(e)}catch(o){throw Gs(o)}return r?!0:(Xn(n,new j(404,e===void 0?"no transcription-capable default model is configured for this caller (catalog task stt) \u2014 name an stt model":`model "${e}" is not transcription-capable (catalog task is not stt) \u2014 this lane serves only speech-to-text models; tts models ride /v1/audio/speech and chat models ride /v1/chat/completions`)),!1)}var Bu=new Set(["file","model","language","response_format"]);async function Vs(t,e,n){let r=n.maxBodyBytes??Mu,o=n.maxAudioPartBytes??$u;try{if(t.method!=="POST")throw new j(405,`${String(t.method)} is not supported on ${en}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new j(400,`the transcriptions endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await zt(t,r)}catch(y){throw y instanceof Q?new j(413,y.message):new j(400,String(y instanceof Error?y.message:y))}let a=new Map;for(let y of i.keys()){if(!Bu.has(y))throw new j(400,`form field "${y}" is not implemented: the native transcription request carries the audio clip and a language hint only (prompt, temperature, timestamps and similar knobs have no native surface on this lane). Supported fields: file, model, language, response_format.`,y);a.set(y,(a.get(y)??0)+1)}for(let[y,h]of a)if(h>1)throw new j(400,`form field "${y}" appears ${h} times: the lane reads exactly one value per field, and a repeat is refused rather than silently discarded \u2014 resend the request with each field once`,y);let u=i.get("model");if(u!==null&&typeof u!="string")throw new j(400,"model must be a string field","model");let d=u===null?void 0:u,l=i.get("language");if(l!==null&&(typeof l!="string"||l.trim().length===0))throw new j(400,"language, when present, must be a non-empty string","language");let p=l===null?void 0:l,m=i.get("response_format")??"json";if(m!=="json")throw new j(400,`response_format ${JSON.stringify(m)} is not supported: timestamps/segments are not produced on this lane yet \u2014 the runtime answers terminal transcript text only, so json is the only output format`,"response_format");let c=i.get("file");if(!(c instanceof File))throw new j(400,"file must be an uploaded WAV: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths","file");if(c.size>o)throw new j(413,`file part exceeds the ${o}-byte limit (the serve protocol's MAX_AUDIO_BYTES)`,"file");let f=new Uint8Array(await c.arrayBuffer());if(!Hs(f))throw new j(400,"the audio payload is not a RIFF/WAVE container \u2014 this lane accepts WAV uploads only (detected by magic bytes, not the declared Content-Type); the runtime decodes PCM16 WAV and nothing else","file");let g=Lu(f);if(g===null)throw new j(400,"the WAV header cannot be read (no fmt /data chunk pair, or a zero rate/channel/depth) \u2014 the runtime decodes RIFF/WAVE PCM16 and this container cannot be honestly decoded or billed","file");if(await Uu(n.turn,n.clients,d),n.stats?.request("audio_transcriptions",f.length,f.length),!await Fu(n.transcriptionModels,d,e))return;let b=[{type:"input_audio",input_audio:{data:Buffer.from(f).toString("base64"),format:"wav"}}];try{let y=await Du(n.clients,d,b,p,n.turn?ie(n.turn):void 0);Nu(n.turn,g),e.statusCode=200,e.setHeader("content-type","application/json"),e.end(JSON.stringify({text:y}))}catch(y){throw Gs(y)}}catch(s){if(s instanceof Q){Xn(e,new j(413,s.message));return}if(s instanceof j){Xn(e,s);return}throw s}}var Ws="/v1/usage/requests",Js=1e4,ju=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,Ee=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},Xe=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}};var Ks=1024*1024;function Qn(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new Ee(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new Ee(`"${e}" must be an array of uuid strings`);if(n.length>Js)throw new Ee(`"${e}" carries ${n.length} ids; at most ${Js} may be looked up in one request. Split the batch \u2014 the ids past the limit are NOT silently dropped`);let r=[],o=new Set;for(let s of n){if(typeof s!="string"||!ju.test(s))throw new Ee(`"${e}" contains ${JSON.stringify(s)}, which is not a LOWERCASE uuid \u2014 every uRun inference id is the lowercase uuid returned in the \`Inference-Id\` response header, and answering a differently-cased spelling would echo back an id the caller cannot match`);o.has(s)||(o.add(s),r.push(s))}return r}function zs(t){return Qn(t,"inference_ids")}function tn(t,e){let n=new Set(e),r=new Set;for(let o=0;o<t.length;o++){let s=t[o];if(!n.has(s.inference_id))throw new Xe(`the usage surface returned an id at validated answer position ${o} that was not in the request (${e.length} asked, ${t.length} answered) \u2014 an answer carrying ids nobody asked about cannot be attributed to this caller`);if(r.has(s.inference_id))throw new Xe(`the usage surface returned ${s.inference_id} more than once \u2014 inference_id is the ledger primary key, so a duplicate means the upstream query is not the one we asked, and a caller that sums the entries would bill the request twice`);r.add(s.inference_id)}}var dn=require("@urun-sh/core");var Zn="/partners/huggingface/v1/billing/requests";function qu(t){return Qn(t,"requestIds")}function Hu(t){return t>=200&&t<=399}function Gu(t){let e=[];for(let n of t)n.cost_nano_usd!==null&&Hu(n.http_status)&&e.push({requestId:n.inference_id,costNanoUsd:n.cost_nano_usd});return{requests:e.length>0?e:null}}async function Ys(t,e){let n=qu(t),r=await e(n);return tn(r,n),Gu(r)}var Vu={"/v1/chat/completions":"conversational","/v1/responses":"conversational","/v1/messages":"conversational","/v1/systemone":"conversational","/v1/images/generations":"text-to-image","/v1/images/edits":"image-to-image","/v1/audio/speech":"text-to-speech","/v1/audio/transcriptions":"speech-to-text"};function Ju(t){return Vu[t]??null}var ei=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function Wu(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function Ku(t){return t==="surface"}function zu(t){let e=t?.ledgerFailureReason;return ei.includes(e)?e:"unknown"}var Yu=["no_response_head","no_org","model_unresolved","no_model_named","queue_full"],Xs=["priced","unpriced"],Xu={written:Xs,already_written:Xs,skipped:Yu,failed:ei},Tp=Object.entries(Xu).flatMap(([t,e])=>e.map(n=>({outcome:t,reason:n})));function Qu(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function Zu(t){let e=Date.parse(t.ts),n=Number.isFinite(t.duration_ms)?Math.max(0,t.duration_ms):0;return new Date(e-Math.round(n)).toISOString()}var ed=256,Qs=4096,gt=12e4,td=250,nd=8e3,ht=[],Zs=new Set,yt=0,mt=!1;var rd=new Set;function od(){return yt===0&&ht.length===0}function sd(){if(od()){mt=!1;for(let t of[...rd])t()}}function ti(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
29
+ `}function id(t){de(process.stdout,t)}function ni(t,e){try{t(e)}catch{}}function ri(t){let{record:e,ledger:n}=t;if(n===null)return;let r=n.report??id,o=Ju(e.path);if(o===null)return;let s=l=>{try{n.observe(l)}catch{}},i=(l,p)=>{s({outcome:"skipped",reason:l}),ni(r,ti("inference_ledger_skipped",{inference_id:e.inference_id,path:e.path,reason:l,detail:p}))};if(e.status===null){i("no_response_head","no head flushed, so the caller never received an Inference-Id and no status can be recorded honestly");return}if(e.org===null){i("no_org","the request never resolved a tenant, so there is no org to attribute the row to");return}let a=e.catalog;if(a.kind==="unresolved"){i("model_unresolved",a.reason);return}let u;if(a.kind==="catalog")u={model_id:a.model_id,identity_kind:"catalog_model",variant:a.variant};else{if(a.model===null){i("no_model_named","the request resolved to a caller-org app but named no model, so there is no app slug to record and `model_id` cannot be written honestly");return}u={model_id:a.model,identity_kind:"caller_org_app",variant:null}}let d={inference_id:e.inference_id,...u,task:o,http_status:e.status,started_at:Zu(e),completed_at:e.ts,billable_outcome:Qu(e),session_id:e.session_id,prompt_tokens:e.prompt_tokens,completion_tokens:e.completion_tokens,output_units:e.output_units,output_minutes:e.output_minutes,queue_ms:e.queue_ms,prefill_ms:e.prefill_ms,decode_ms:e.decode_ms,total_ms:e.total_ms,ttft_ms:e.ttft_ms===null?null:Math.round(e.ttft_ms),e2e_ms:Math.round(e.duration_ms)};if(ht.length>=Qs){i("queue_full",`${ht.length} ledger writes are already waiting (capacity ${Qs}, ${yt} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}ht.push({entry:d,req:t.req,record:e,ledger:n,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),yt<ed&&(yt+=1,ad())}async function ad(){try{for(;;){let t=ud();if(t===void 0)return;Zs.add(t);try{await dd(t)}finally{Zs.delete(t)}}}finally{yt-=1,sd()}}function ud(){let t=Date.now();for(;;){let e=ht.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<gt)return e;Qe(e,{outcome:"failed",reason:"dwell_expired"},{event:"inference_ledger_write_failed",reason:"dwell_expired",error:`this row waited ${n}ms for the control plane (bound ${gt}ms) after ${e.attempts} attempt(s) and was given up on \u2014 the request was served and answered, and it will never be billed. Hugging Face writes a request off ~30 minutes after serving, so a row placed later than this is not worth the memory to keep trying`})}}async function dd(t){let{entry:e,record:n,ledger:r}=t;for(;;){t.attempts+=1;try{let o=await r.write(t.req,e);Qe(t,{outcome:"written",reason:o.cost_nano_usd===null?"unpriced":"priced"},{event:"inference_ledger_written",inference_id:o.inference_id,path:n.path,model_id:e.model_id,identity_kind:e.identity_kind,variant:e.variant,org:n.org,session_id:e.session_id,cost_nano_usd:o.cost_nano_usd,price_version:o.price_version,unpriced_reason:o.unpriced_reason,attempts:t.attempts});return}catch(o){let s=Wu(o);if(s!==null){Qe(t,{outcome:"already_written",reason:s.cost_nano_usd===null?"unpriced":"priced"},{event:"inference_ledger_already_written",inference_id:e.inference_id,model_id:e.model_id,identity_kind:e.identity_kind,variant:e.variant,cost_nano_usd:s.cost_nano_usd,price_version:s.price_version,attempts:t.attempts});return}let i=zu(o),a=Date.now()-t.queuedAt;if(mt){Qe(t,{outcome:"failed",reason:"dropped_on_exit"},{event:"inference_ledger_write_failed",reason:"dropped_on_exit",attempts:t.attempts,waited_ms:a,error:`the proxy is shutting down and this row was still being retried when the drain ran out \u2014 the request was served and answered, and its billing row was never written. Last failure: ${o instanceof Error?o.message:String(o)}`});return}if(Ku(i)&&a<gt){if(await cd(ld(t.attempts)),mt||Date.now()-t.queuedAt>=gt){let u=Date.now()-t.queuedAt;Qe(t,{outcome:"failed",reason:mt?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:mt?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:u,error:`this row's backoff outlived the ${gt}ms dwell bound (waited ${u}ms over ${t.attempts} attempt(s)) and was given up on rather than starting an attempt that could only finish past it. Last failure: ${o instanceof Error?o.message:String(o)}`});return}continue}Qe(t,{outcome:"failed",reason:i},{event:"inference_ledger_write_failed",reason:i,attempts:t.attempts,waited_ms:a,error:o instanceof Error?o.message:String(o)});return}}}function ld(t){return Math.min(nd,td*2**Math.max(0,t-1))}function cd(t){return new Promise(e=>{setTimeout(e,t).unref?.()})}function Qe(t,e,n){if(t.settled)return;t.settled=!0;try{t.ledger.observe(e)}catch{}let{event:r,...o}=n;ni(t.report,ti(r,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...o}))}var tr="x-urun-profile",wt="voice";var oe=class extends Error{status=400},pd=/(?:[.!?:;]|\n)[)\]"'\u2019\u201d\u00bb\u300d\u300b\u3011\uff09\uff3d]?\s*$/,fd=/\s/;function md(t){return pd.test(t)}function er(t,e,n){let r=t[e];if(r===void 0)return n;if(typeof r!="boolean")throw new oe(`stream_options.voice.${e} must be a boolean when present (got ${typeof r})`);return r}function oi(t,e,n,r={}){let o=t[e];if(o===void 0)return n;if(typeof o!="number"||!Number.isFinite(o)||o<=0)throw new oe(`stream_options.voice.${e} must be a positive finite number when present (got ${JSON.stringify(o)})`);if(r.min!==void 0&&o<r.min)throw new oe(`stream_options.voice.${e} must be at least ${r.min} when present (got ${JSON.stringify(o)})${r.minReason?` \u2014 ${r.minReason}`:""}`);if(r.max!==void 0&&o>r.max)throw new oe(`stream_options.voice.${e} must be at most ${r.max} when present (got ${JSON.stringify(o)}) \u2014 larger values wrap Node's setTimeout to a 1ms delay`);return o}function si(t,e){let n=Array.isArray(t)?t[0]:t,r=e.stream_options??void 0;if(r!==void 0&&(typeof r!="object"||Array.isArray(r)))throw new oe(`stream_options must be a JSON object when present, e.g. {"profile":"voice","voice":{...}} (got ${JSON.stringify(r)})`);let o=r,s=o?.profile,i=!1;if(n!=null){let d=n.trim();if(d==="")throw new oe(`the ${tr} header is empty \u2014 the only profile this lane accepts is "${wt}" (omit the header for the ordinary lane)`);if(d!==wt)throw new oe(`unknown profile "${d}" \u2014 the only profile this lane accepts is "${wt}"`);i=!0}if(s!==void 0){if(typeof s!="string"||s!==wt)throw new oe(`unknown profile ${JSON.stringify(s)} \u2014 the only profile this lane accepts is "${wt}"`);i=!0}if(!i)return null;if(e.stream!==!0)throw new oe("the voice profile is streaming-only \u2014 send stream: true");let a=o?.voice;if(a!==void 0&&(typeof a!="object"||a===null||Array.isArray(a)))throw new oe("stream_options.voice must be an object when present (flush_max_chars, flush_max_ms, sentence_flush, include_reasoning, metadata)");let u=a??{};return{flushMaxChars:oi(u,"flush_max_chars",120,{min:2,minReason:"a hard split at a bound below 2 cannot keep UTF-16 surrogate pairs (astral characters) intact"}),flushMaxMs:oi(u,"flush_max_ms",250,{max:2147483647}),sentenceFlush:er(u,"sentence_flush",!0),includeReasoning:er(u,"include_reasoning",!1),metadata:er(u,"metadata",!1)}}var nn=class{constructor(e,n){this.config=e;this.sink=n}config;sink;buffer="";timer=null;push(e){e!==""&&(this.buffer===""&&(this.timer=setTimeout(()=>{this.timer=null,this.flush()},this.config.flushMaxMs)),this.buffer+=e,this.drain())}flush(){if(this.clearTimer(),this.buffer==="")return;let e=this.buffer;this.buffer="",this.sink(e)}dispose(){this.clearTimer()}drain(){let e=this.config.flushMaxChars;for(;this.buffer.length>e;){let n=this.boundedCut();this.sink(this.buffer.slice(0,n)),this.buffer=this.buffer.slice(n)}(this.buffer.length>=e||this.config.sentenceFlush&&md(this.buffer))&&this.flush(),this.buffer===""&&this.clearTimer()}boundedCut(){let e=Math.min(Math.floor(this.config.flushMaxChars),this.buffer.length);for(let o=e-1;o>0;o--)if(fd.test(this.buffer[o]))return o+1;let n=Math.max(1,e),r=this.buffer.charCodeAt(n-1);return r>=55296&&r<=56319?n-1:n}clearTimer(){this.timer!==null&&(clearTimeout(this.timer),this.timer=null)}};var nr=Ie(require("crypto"),1),gd="urun-runtime-key-v1";function hd(t,e){if(!t)throw new Error("deriveOrgRuntimeKey requires a master secret");if(!e)throw new Error("deriveOrgRuntimeKey requires an orgId");return nr.createHmac("sha256",t).update(`${gd}|${e}`,"ascii").digest("base64url")}var yd="decision-intake",rn=6e4,wd=rn/1e3+5,ii=3,rr=15;function _d(t,e,n=60,r=Math.floor(Date.now()/1e3)){if(!t)throw new Error("cannot mint a decision token without a secret");let o=d=>Buffer.from(d).toString("base64url"),s=o(JSON.stringify({alg:"HS256",typ:"JWT"})),i={tenant:e.tenant,app:e.app,fn:e.fn,...e.provider?{provider:e.provider}:{},session_id:"decision-intake",sub:`machine:${e.tenant}/${e.app}/${e.fn}`,aud:yd,iat:r,exp:r+Math.max(1,n)},a=o(JSON.stringify(i)),u=nr.createHmac("sha256",t).update(`${s}.${a}`,"ascii").digest("base64url");return`${s}.${a}.${u}`}function vd(t){if(t===null||typeof t!="object")return!1;let e=t;return typeof e.runtime_id=="string"&&e.runtime_id!==""&&typeof e.ready=="boolean"&&typeof e.address=="string"&&e.address!==""&&typeof e.last_seen=="string"&&!Number.isNaN(Date.parse(e.last_seen))}async function bd(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/runtimes?app=${encodeURIComponent(t.app)}&fn=${encodeURIComponent(t.fn)}`,r;try{r=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"},signal:AbortSignal.timeout(rn)})}catch(u){throw(u instanceof Error?u.name:void 0)==="TimeoutError"?new fe(`runtime presence read for (app=${t.app} fn=${t.fn}) timed out after ${rn}ms \u2014 the control plane is not answering; no replica can be discovered`):u}if(!r.ok)throw new Error(`runtime presence read failed: GET ${n} \u2192 ${r.status}`);let o=await r.json();if(!Array.isArray(o.runtimes))throw new Error(`runtime presence read returned no "runtimes" array (GET ${n})`);let s=[],i=[];for(let u of o.runtimes){if(!vd(u)){s.push(JSON.stringify(u));continue}i.push(u)}if(s.length>0)throw new Error(`runtime presence read for (app=${t.app} fn=${t.fn}) returned ${s.length} malformed presence row(s) \u2014 the control plane's runtimes schema drifted (runtime_id/address/ready/last_seen are all required): ${s.slice(0,3).join("; ")}`);let a=(t.nowMs??Date.now())/1e3;return i.filter(u=>u.ready===!0&&a-Date.parse(u.last_seen)/1e3<=rr)}var kd=rr*1e3,Sd=rr*500,xd=256,on=class{constructor(e){this.opts=e;this.nowMs=e.nowMs??Date.now}opts;replicas=new Map;nowMs;presenceCache=new Map;get enabled(){return this.opts.secret!==""}async targetFor(e){let n=await this.opts.targetFor(e);return n.sharedApp===!0&&this.opts.shared===void 0?null:n}async decide(e,n){let r=await this.targetFor(n);if(r===null)throw new fe("the shared-endpoint lane keeps the session dial \u2014 the stateless direct path serves caller-org functions");let o=r.appSlug,s=r.fnName??this.opts.fnName,i=r.sharedApp===!0,a=i?this.opts.shared.apiKey:this.opts.apiKey,u=`${a}\0${o}\0${s}`,d=await this.ensureRows(u,a,o,s);if(d.length===0)throw new fe(`no ready runtime for (org app=${o} fn=${s}) \u2014 nothing is serving decisions right now`);let l=null;for(let p=0;p<ii;p++){let m=this.pick(d);if(m===null)throw new fe(`every discovered runtime for app=${o} failed at the transport layer \u2014 no replica took the decision`);let c=i&&r.sharedOrgId?r.sharedOrgId:this.opts.orgId,f=_d(hd(this.opts.secret,c),{tenant:this.opts.orgId,app:o,fn:s,...i&&r.sharedOrgId?{provider:r.sharedOrgId}:{}},wd);try{return{...await this.post(m,e,f),target:{...r,fnName:s}}}catch(g){if(g instanceof De)throw g;l=g;let b=this.replicas.get(m)??{outstanding:0,cooldownUntil:0};b.cooldownUntil=this.nowMs()+5e3,this.replicas.set(m,b);let y=this.presenceCache.get(u);y&&(y.rows=y.rows.filter(h=>h.address!==m),y.rows.length===0&&this.presenceCache.delete(u))}}throw new fe(`no runtime for app=${o} answered within ${ii} attempts: ${String(l instanceof Error?l.message:l)}`)}async ensureRows(e,n,r,o){let s=this.presenceCache.get(e);if(s)if(this.nowMs()-s.fetchedAtMs>kd)this.presenceCache.delete(e);else return this.nowMs()-s.fetchedAtMs>Sd&&!s.refreshing&&(s.refreshing=!0,this.readPresence(n,r,o).then(a=>{this.presenceCache.set(e,{rows:a,fetchedAtMs:this.nowMs(),refreshing:!1})}).catch(()=>{s.refreshing=!1})),s.rows;let i=await this.readPresence(n,r,o);for(;this.presenceCache.size>=xd;){let a=this.presenceCache.keys().next();if(a.done)break;this.presenceCache.delete(a.value)}return this.presenceCache.set(e,{rows:i,fetchedAtMs:this.nowMs(),refreshing:!1}),i}readPresence(e,n,r){return bd({apiUrl:this.opts.apiUrl,apiKey:e,app:n,fn:r,fetchImpl:this.opts.fetchImpl,nowMs:this.nowMs()})}pick(e){let n=this.nowMs(),r=null,o=1/0;for(let s of e){let i=this.replicas.get(s.address);if(i!==void 0&&i.cooldownUntil>n)continue;let a=i?.outstanding??0;a<o&&(r=s.address,o=a)}return r}async post(e,n,r){let o=this.replicas.get(e)??{outstanding:0,cooldownUntil:0};o.outstanding+=1,this.replicas.set(e,o);try{let s=await fetch(`${e.replace(/\/+$/,"")}/v1/decision`,{method:"POST",headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},body:JSON.stringify(n),signal:AbortSignal.timeout(this.opts.timeoutMs??rn)}),i=null;try{i=await s.json()}catch{i={error:{message:`runtime answered ${s.status} with a non-JSON body`,type:"upstream_error"}}}if(s.status>=500)throw new Error(`runtime ${e} answered ${s.status}`);if(s.status>=400)throw new De(s.status,i);return{status:s.status,body:i,completed:Rd(i)}}finally{o.outstanding-=1}}};function Rd(t){if(t===null||typeof t!="object")return null;let e=t,n=e.usage;if(n===null||typeof n!="object")return e;let r=n;return{...e,usage:{input_tokens:r.prompt_tokens??r.input_tokens,output_tokens:r.completion_tokens??r.output_tokens}}}var De=class extends Error{constructor(n,r){super(`runtime answered ${n}`);this.status=n;this.body=r}status;body},fe=class extends Error{};function ir(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function Ed(t){t.writeHead(200,{"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache",connection:"keep-alive","x-accel-buffering":"no"}),t.flushHeaders(),t.socket?.setNoDelay(!0)}function J(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function K(t,e,n,r="invalid_request_error",o){J(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function ar(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);J(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function me(t,e,n,r){J(t,e,{type:"error",error:{type:n,message:r}})}function ge(t,e,n){if(e==="anthropic"){J(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}J(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var Ad=64*1024*1024,_t=class extends Error{};async function ai(t,e=Ad){let n=[],r=0;for await(let s of t){if(r+=s.length,r>e)throw new _t(`request body exceeds ${e} bytes`);n.push(s)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function Fe(t,e,n,r){return{id:t,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:e,choices:[{index:0,delta:n,finish_reason:r}]}}function li(t){let e=t?.usage;if(!e||typeof e!="object")return{kind:"absent"};let n=e,r=u=>typeof u=="number"&&Number.isFinite(u)&&u>=0?u:null,o=r(n.input_tokens),s=r(n.output_tokens),i=u=>u??0,a=r(n.total_tokens)??i(o)+i(s);if(i(o)>0||i(s)>0){let u={input_tokens:i(o),output_tokens:i(s),total_tokens:a};return o===null||s===null?{kind:"partial",usage:u}:{kind:"counts",usage:u}}return o===null||s===null?{kind:"absent"}:{kind:"zero",usage:{input_tokens:0,output_tokens:0,total_tokens:a}}}function sn(t){let e=li(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function ui(t){let e=sn(t);return e?{prompt_tokens:e.input_tokens,completion_tokens:e.output_tokens,total_tokens:e.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function Id(t,e){let n=li(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function Od(t){let e=t?.urun_timing;if(!e||typeof e!="object")return null;let n=e,r=u=>{let d=n[u];return typeof d=="number"&&Number.isSafeInteger(d)&&d>=0?d:null},o=r("queue_ms"),s=r("prefill_ms"),i=r("decode_ms"),a=r("total_ms");return o===null||s===null||i===null||a===null?null:{queue_ms:o,prefill_ms:s,decode_ms:i,total_ms:a}}function Td(t,e){let n=Od(e);n&&(t.timing=n)}function _e(t,e){Id(t,e),Td(t,e)}async function ln(t,e,n){if(typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}function ci(t){let e=t?.output;if(!Array.isArray(e))return"";let n="";for(let r of e)if(!(r.type!=="reasoning"||!Array.isArray(r.content)))for(let o of r.content)o.type==="reasoning_text"&&(n+=String(o.text??""));return n}function Pd(t){if(Array.isArray(t))return t.map(e=>{let n=e.function;return!n||typeof n!="object"?e:{type:"function",name:n.name,description:n.description,parameters:n.parameters,strict:n.strict}})}var an=class extends Error{};function Cd(t,e){return Array.isArray(e)?e.map(n=>{let r=n;if(r.type==="text")return{type:t==="assistant"?"output_text":"input_text",text:r.text};if(r.type==="image_url"){let o=r.image_url??{};return{type:"input_image",image_url:o.url,detail:o.detail}}throw new an(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function $d(t){let e=[];for(let n of t){if(n.role==="tool"){e.push({type:"function_call_output",call_id:n.tool_call_id,output:typeof n.content=="string"?n.content:JSON.stringify(n.content??"")});continue}if(n.role==="assistant"&&Array.isArray(n.tool_calls)){typeof n.content=="string"&&n.content&&e.push({role:"assistant",content:n.content});for(let r of n.tool_calls){let o=r.function??{};e.push({type:"function_call",call_id:r.id,name:o.name,arguments:o.arguments})}continue}e.push({...n,content:Cd(n.role,n.content)})}return e}function or(t){return Ge(t).map((e,n)=>({index:n,id:e.call_id??e.id??`call_${n}`,type:"function",function:{name:e.name??"",arguments:e.arguments??"{}"}}))}async function Md(t,e,n,r,o,s){let i=at(s.id),a=t.stream===!0;if(s.model=ne(t),s.stream=a,await ln(s,e,typeof t.model=="string"?t.model:void 0),a){let y;try{y=await dt({body:t,clients:e,store:n,stats:o,responseId:i,turn:s})}catch(h){if(h instanceof U){ge(r,"openai",h);return}if(h instanceof ae){K(r,h.status,h.message);return}throw h}ir(r);for await(let h of y.events)h.type==="error"&&(s.streamError=!0),h.type==="response.completed"&&_e(s,h.response),r.write(`event: ${String(h.type??"message")}
30
30
  data: ${JSON.stringify(h)}
31
31
 
32
32
  `);r.write(`data: [DONE]
33
33
 
34
- `),r.end();return}let u;try{u=Je(n,t)}catch(y){if(y instanceof ae){K(r,y.status,y.message);return}throw y}t.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(u))-Buffer.byteLength(JSON.stringify(t.input)));let d=t.store!==!1,l=ne(t);o.model(l);let p=Array.isArray(t.tools)&&t.tools.length>0,m;try{m=await On(e,t,u,!1,ie(s))}catch(y){if(y instanceof U){ge(r,"openai",y);return}if(y instanceof ae){K(r,y.status,y.message);return}throw y}let c=[],f=null;for await(let y of o.track(m,l,s)){if(c.push(y),y.type==="error"){ir(r,y);return}y.type==="response.completed"&&(f=y.response)}if(f==null){K(r,502,"upstream produced no response.completed event","upstream_error");return}_e(s,f);let g=Ve(c,f),_=Ue(p,He(f).length,g);if(_){K(r,502,Le(l,_),"upstream_error");return}d&&n.remember(i,u,In(f,g),d),J(r,200,{...f,id:i})}async function Pd(t,e,n,r,o,s){let i=t.messages;if(!Array.isArray(i)){K(n,400,"chat/completions requires a messages array");return}let a;try{a=Xs(s,t)}catch(S){if(S instanceof oe){K(n,S.status,S.message);return}throw S}let u=String(t.model??"urun"),d=Oo(o.id),l;try{l=Od(i)}catch(S){if(S instanceof sn){K(n,400,S.message);return}throw S}let p=ne(t);r.model(p),o.model=p,o.stream=t.stream===!0,await dn(o,e,typeof t.model=="string"?t.model:void 0);let m=Array.isArray(t.tools)&&t.tools.length>0;if(t.stream!==!0&&e.stateless?.enabled){let S;try{S=await e.stateless.targetFor(typeof t.model=="string"?t.model:void 0)}catch(x){if(x instanceof U){ge(n,"openai",x);return}throw x}if(S!==null){try{let x=await e.stateless.decide(t,typeof t.model=="string"?t.model:void 0);o.catalog=nt(x.target),_e(o,x.completed),J(n,200,{...x.body,id:d})}catch(x){if(x instanceof U){ge(n,"openai",x);return}if(x instanceof Ne){J(n,x.status,x.body);return}if(x instanceof fe){K(n,503,x.message,"upstream_error");return}throw x}return}}let c;try{c=await e.createResponse({model:t.model,input:l,stream:!0,tools:Ad(t.tools),tool_choice:t.tool_choice,temperature:t.temperature,top_p:t.top_p,stop:t.stop,max_output_tokens:t.max_completion_tokens??t.max_tokens,...t.reasoning_effort!==void 0?{reasoning_effort:t.reasoning_effort}:{},...t.chat_template_kwargs!==void 0?{chat_template_kwargs:t.chat_template_kwargs}:{},...t.response_format!==void 0?{response_format:t.response_format}:{}},ie(o))}catch(S){if(S instanceof U){ge(n,"openai",S);return}throw S}if(t.stream===!0){a?kd(n):sr(n);let S=[],x=new Re,C="",F=a?new tn(a,te=>{n.write(`data: ${JSON.stringify(De(d,u,{content:te},null))}
34
+ `),r.end();return}let u;try{u=We(n,t)}catch(y){if(y instanceof ae){K(r,y.status,y.message);return}throw y}t.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(u))-Buffer.byteLength(JSON.stringify(t.input)));let d=t.store!==!1,l=ne(t);o.model(l);let p=Array.isArray(t.tools)&&t.tools.length>0,m;try{m=await Tn(e,t,u,!1,ie(s))}catch(y){if(y instanceof U){ge(r,"openai",y);return}if(y instanceof ae){K(r,y.status,y.message);return}throw y}let c=[],f=null;for await(let y of o.track(m,l,s)){if(c.push(y),y.type==="error"){ar(r,y);return}y.type==="response.completed"&&(f=y.response)}if(f==null){K(r,502,"upstream produced no response.completed event","upstream_error");return}_e(s,f);let g=Je(c,f),b=Ue(p,Ge(f).length,g);if(b){K(r,502,Le(l,b),"upstream_error");return}d&&n.remember(i,u,On(f,g),d),J(r,200,{...f,id:i})}async function Ld(t,e,n,r,o,s){let i=t.messages;if(!Array.isArray(i)){K(n,400,"chat/completions requires a messages array");return}let a;try{a=si(s,t)}catch(S){if(S instanceof oe){K(n,S.status,S.message);return}throw S}let u=String(t.model??"urun"),d=To(o.id),l;try{l=$d(i)}catch(S){if(S instanceof an){K(n,400,S.message);return}throw S}let p=ne(t);r.model(p),o.model=p,o.stream=t.stream===!0,await ln(o,e,typeof t.model=="string"?t.model:void 0);let m=Array.isArray(t.tools)&&t.tools.length>0;if(t.stream!==!0&&e.stateless?.enabled){let S;try{S=await e.stateless.targetFor(typeof t.model=="string"?t.model:void 0)}catch(R){if(R instanceof U){ge(n,"openai",R);return}throw R}if(S!==null){try{let R=await e.stateless.decide(t,typeof t.model=="string"?t.model:void 0);o.catalog=ot(R.target),_e(o,R.completed),J(n,200,{...R.body,id:d})}catch(R){if(R instanceof U){ge(n,"openai",R);return}if(R instanceof De){J(n,R.status,R.body);return}if(R instanceof fe){K(n,503,R.message,"upstream_error");return}throw R}return}}let c;try{c=await e.createResponse({model:t.model,input:l,stream:!0,tools:Pd(t.tools),tool_choice:t.tool_choice,temperature:t.temperature,top_p:t.top_p,stop:t.stop,max_output_tokens:t.max_completion_tokens??t.max_tokens,...t.reasoning_effort!==void 0?{reasoning_effort:t.reasoning_effort}:{},...t.chat_template_kwargs!==void 0?{chat_template_kwargs:t.chat_template_kwargs}:{},...t.response_format!==void 0?{response_format:t.response_format}:{}},ie(o))}catch(S){if(S instanceof U){ge(n,"openai",S);return}throw S}if(t.stream===!0){a?Ed(n):ir(n);let S=[],R=new xe,$="",F=a?new nn(a,te=>{n.write(`data: ${JSON.stringify(Fe(d,u,{content:te},null))}
35
35
 
36
36
  `)}):null,Y=te=>{F?.flush(),n.write(te),n.end()};try{a?.metadata&&n.write(`data: ${JSON.stringify({object:"chat.completion.metadata",id:d,model:u,interruptible:!0,tts:{sample_rate_hz:24e3,voice_hint:null}})}
37
37
 
38
- `),n.write(`data: ${JSON.stringify(De(d,u,{role:"assistant"},null))}
38
+ `),n.write(`data: ${JSON.stringify(Fe(d,u,{role:"assistant"},null))}
39
39
 
40
- `);for await(let V of r.track(c,p,o)){S.push(V);let Z=Ge(V);if(V.type==="response.output_text.delta"&&typeof V.delta=="string")C+=V.delta,F?F.push(V.delta):n.write(`data: ${JSON.stringify(De(d,u,{content:V.delta},null))}
40
+ `);for await(let V of r.track(c,p,o)){S.push(V);let Z=Ve(V);if(V.type==="response.output_text.delta"&&typeof V.delta=="string")$+=V.delta,F?F.push(V.delta):n.write(`data: ${JSON.stringify(Fe(d,u,{content:V.delta},null))}
41
41
 
42
- `);else if(V.type==="response.reasoning_text.delta"&&typeof V.delta=="string")(!a||a.includeReasoning)&&n.write(`data: ${JSON.stringify(De(d,u,{reasoning_content:V.delta},null))}
42
+ `);else if(V.type==="response.reasoning_text.delta"&&typeof V.delta=="string")(!a||a.includeReasoning)&&n.write(`data: ${JSON.stringify(Fe(d,u,{reasoning_content:V.delta},null))}
43
43
 
44
- `);else if(Z){F?.flush();let{call:Ae,isNew:T}=x.add(Z),N=T?{index:Z.tool_index,id:Ae.id??`call_${Z.tool_index}`,type:"function",function:{name:Ae.name??"",arguments:Z.delta}}:{index:Z.tool_index,function:{arguments:Z.delta}};n.write(`data: ${JSON.stringify(De(d,u,{tool_calls:[N]},null))}
44
+ `);else if(Z){F?.flush();let{call:Ae,isNew:T}=R.add(Z),N=T?{index:Z.tool_index,id:Ae.id??`call_${Z.tool_index}`,type:"function",function:{name:Ae.name??"",arguments:Z.delta}}:{index:Z.tool_index,function:{arguments:Z.delta}};n.write(`data: ${JSON.stringify(Fe(d,u,{tool_calls:[N]},null))}
45
45
 
46
46
  `)}else if(V.type==="error"){o.streamError=!0,Y(`data: ${JSON.stringify({error:V})}
47
47
 
48
- `);return}}let te=rr(pe(S)),ue=Ue(m,x.size+te.length,C);if(ue){o.streamError=!0,Y(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:Le(p,ue)}})}
48
+ `);return}}let te=or(pe(S)),ue=Ue(m,R.size+te.length,$);if(ue){o.streamError=!0,Y(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:Le(p,ue)}})}
49
49
 
50
- `);return}F?.flush(),x.size===0&&te.length>0&&n.write(`data: ${JSON.stringify(De(d,u,{tool_calls:te},null))}
50
+ `);return}F?.flush(),R.size===0&&te.length>0&&n.write(`data: ${JSON.stringify(Fe(d,u,{tool_calls:te},null))}
51
51
 
52
- `);let ve=x.size>0||te.length>0;n.write(`data: ${JSON.stringify(De(d,u,{},ve?"tool_calls":"stop"))}
52
+ `);let ve=R.size>0||te.length>0;n.write(`data: ${JSON.stringify(Fe(d,u,{},ve?"tool_calls":"stop"))}
53
53
 
54
- `);let G=on(pe(S));_e(o,pe(S)),G&&n.write(`data: ${JSON.stringify({id:d,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:u,choices:[],usage:ei(pe(S))})}
54
+ `);let G=sn(pe(S));_e(o,pe(S)),G&&n.write(`data: ${JSON.stringify({id:d,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:u,choices:[],usage:ui(pe(S))})}
55
55
 
56
56
  `),n.write(`data: [DONE]
57
57
 
58
- `),n.end();return}finally{F?.flush(),F?.dispose()}}let f="",g="",_=[],y=new Re;for await(let S of r.track(c,p,o)){_.push(S),S.type==="response.output_text.delta"&&typeof S.delta=="string"&&(f+=S.delta),S.type==="response.reasoning_text.delta"&&typeof S.delta=="string"&&(g+=S.delta);let x=Ge(S);if(x&&y.add(x),S.type==="error"){ir(n,S);return}}let h=pe(_);_e(o,h);let w=rr(h),A=w.length>0?w:rr({output:y.items()}),v=Ue(m,A.length,f);if(v){K(n,502,Le(p,v),"upstream_error");return}let b=A.map(({index:S,...x})=>x),R={role:"assistant",content:f||null},L=g||ri(h);L&&(R.reasoning_content=L),b.length>0&&(R.tool_calls=b),J(n,200,{id:d,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:u,choices:[{index:0,message:R,finish_reason:b.length>0?"tool_calls":"stop"}],usage:ei(h)})}var Cd="reflex-latest";async function $d(t,e,n,r,o){if(t===null||typeof t!="object"||Array.isArray(t)){K(n,400,"systemone requires a JSON object body");return}delete t.key;let s=typeof t.model=="string"&&t.model?t.model:Cd,i=ne({model:s});if(o.model=i,o.stream=!1,t.state===void 0||t.state===null){K(n,400,"systemone requires a state (string or JSON structure)");return}let a=t.questions;if(a===null||typeof a!="object"||Array.isArray(a)||Object.keys(a).length===0){K(n,400,"systemone requires a questions object (question id -> {kind, options, ...})");return}if(t.media!==void 0&&!Array.isArray(t.media)){K(n,400,"systemone media, when present, must be an array");return}if(r.model(i),await dn(o,e,s),e.stateless?.enabled){let c;try{c=await e.stateless.targetFor(s)}catch(f){if(f instanceof U){ge(n,"openai",f);return}throw f}if(c!==null){try{let f=await e.stateless.decide(t,s);o.catalog=nt(f.target),_e(o,f.completed),J(n,200,f.body)}catch(f){if(f instanceof U){ge(n,"openai",f);return}if(f instanceof Ne){J(n,f.status,f.body);return}if(f instanceof fe){K(n,503,f.message,"upstream_error");return}throw f}return}}let u;try{u=await e.createResponse({model:s,input:[{role:"user",content:JSON.stringify(t)}],stream:!0},ie(o))}catch(c){if(c instanceof U){ge(n,"openai",c);return}throw c}let d=[];try{for await(let c of r.track(u,i,o))if(d.push(c),c.type==="error"){ir(n,c);return}}catch(c){if(c instanceof U){ge(n,"openai",c);return}throw c}let l=pe(d);if(l==null){K(n,502,"upstream produced no response.completed event","upstream_error");return}_e(o,l);let p=Ve(d,l),m;try{m=JSON.parse(p)}catch{K(n,502,"systemone answer was not valid JSON","upstream_error");return}if(m===null||typeof m!="object"||Array.isArray(m)){K(n,502,"systemone answer was not a JSON object","upstream_error");return}J(n,200,m)}function Md(t){return typeof t=="string"?t:Array.isArray(t)?t.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var _t=class extends Error{};function an(t){return new _t(`unsupported anthropic content block type "${t}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function Ld(t){if(Array.isArray(t))return t.map(e=>{let n=e;return{type:"function",name:n.name,description:n.description,parameters:n.input_schema}})}function Ud(t){if(t==null)return;let e=t.type;if(e==="auto")return"auto";if(e==="none")return"none";if(e==="any")return"required";if(e==="tool")return{type:"function",name:t.name};throw new _t(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function Nd(t){if(typeof t=="string")return t;if(t==null)return"";if(!Array.isArray(t))throw an(typeof t);let e="";for(let n of t){let r=String(n.type??"");if(r!=="text")throw an(`tool_result > ${r}`);e+=String(n.text??"")}return e}function Dd(t){let{role:e,content:n}=t;if(typeof n=="string")return[{role:e,content:n}];if(n==null)return[];if(!Array.isArray(n))throw an(typeof n);let r=[],o="",s=()=>{o&&(r.push({role:e,content:o}),o="")};for(let i of n){let a=String(i.type??"");if(a==="text")o+=String(i.text??"");else if(a==="tool_use")s(),r.push({type:"function_call",call_id:i.id,name:i.name,arguments:JSON.stringify(i.input??{})});else if(a==="tool_result")s(),r.push({type:"function_call_output",call_id:i.tool_use_id,output:Nd(i.content)});else{if(a==="thinking"||a==="redacted_thinking")continue;throw an(a)}}return s(),r}function or(t){return He(t).map((e,n)=>{let r;try{r=JSON.parse(e.arguments||"{}")}catch{throw new Error(`upstream function_call "${e.name??""}" arguments are not valid JSON`)}return{type:"tool_use",id:e.call_id??e.id??`toolu_${n}`,name:e.name??"",input:r}})}async function Fd(t,e,n,r,o){let s=t.messages;if(!Array.isArray(s)){me(n,400,"invalid_request_error","messages requires a messages array");return}if(typeof t.max_tokens!="number"||!Number.isInteger(t.max_tokens)||t.max_tokens<1){me(n,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(t.top_k!==void 0){me(n,400,"invalid_request_error","top_k is not supported: the uRun serve lane exposes temperature/top_p/stop_sequences only");return}if(t.stop_sequences!==void 0&&(!Array.isArray(t.stop_sequences)||t.stop_sequences.some(R=>typeof R!="string"))){me(n,400,"invalid_request_error","stop_sequences must be an array of strings");return}let i=String(t.model??"urun"),a=To(o.id),u=[],d;try{t.system&&u.push({role:"system",content:Md(t.system)});for(let R of s)u.push(...Dd(R));d=Ud(t.tool_choice)}catch(R){if(R instanceof _t){me(n,400,"invalid_request_error",R.message);return}throw R}let l=ne(t);r.model(l),o.model=l,o.stream=t.stream===!0,await dn(o,e,typeof t.model=="string"?t.model:void 0);let p=Array.isArray(t.tools)&&t.tools.length>0,m;try{m=await e.createResponse({model:t.model,input:u,stream:!0,tools:Ld(t.tools),tool_choice:d,temperature:t.temperature,max_output_tokens:t.max_tokens,top_p:t.top_p,stop:t.stop_sequences},ie(o))}catch(R){if(R instanceof U){ge(n,"anthropic",R);return}throw R}let c=(R,L)=>{n.destroyed||n.write(`event: ${R}
59
- data: ${JSON.stringify({type:R,...L})}
60
-
61
- `)};if(t.stream===!0){let R=r.track(m,l,o);sr(n),c("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:i,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let L=0,S="",x=0,C="none",F=()=>C,Y=-1,te=-1,ue=new Re,ve=[],G=()=>{C!=="none"&&(C="none",c("content_block_stop",{index:Y}))},V=(B,ee)=>{G(),C=B,Y=x++,c("content_block_start",{index:Y,content_block:ee})};for await(let B of R){if(n.destroyed)break;ve.push(B);let ee=Ge(B);if(B.type==="response.reasoning_text.delta"&&typeof B.delta=="string")F()!=="thinking"&&V("thinking",{type:"thinking",thinking:""}),c("content_block_delta",{index:Y,delta:{type:"thinking_delta",thinking:B.delta}});else if(B.type==="response.output_text.delta"&&typeof B.delta=="string")F()!=="text"&&V("text",{type:"text",text:""}),L+=1,S+=B.delta,c("content_block_delta",{index:Y,delta:{type:"text_delta",text:B.delta}});else if(ee){let{call:be}=ue.add(ee);(F()!=="tool"||ee.tool_index!==te)&&(te=ee.tool_index,V("tool",{type:"tool_use",id:be.id??`call_${ee.tool_index}`,name:be.name??"",input:{}})),ee.delta&&c("content_block_delta",{index:Y,delta:{type:"input_json_delta",partial_json:ee.delta}})}else if(B.type==="error"){o.streamError=!0,c("error",{error:{type:"api_error",message:JSON.stringify(B)}}),n.end();return}}if(n.destroyed)return;let Z=pe(ve),Ae=He(Z),T=Ue(p,ue.size+Ae.length,S);if(T){o.streamError=!0,c("error",{error:{type:"api_error",message:Le(l,T)}}),n.end();return}G();let N=[];if(ue.size===0)try{N=or(Z)}catch(B){o.streamError=!0,c("error",{error:{type:"api_error",message:String(B instanceof Error?B.message:B)}}),n.end();return}x===0&&N.length===0&&(V("text",{type:"text",text:""}),G()),N.forEach(B=>{let ee=x++;c("content_block_start",{index:ee,content_block:{...B,input:{}}}),c("content_block_delta",{index:ee,delta:{type:"input_json_delta",partial_json:JSON.stringify(B.input??{})}}),c("content_block_stop",{index:ee})});let z=ue.size>0||N.length>0,he=on(Z);_e(o,Z),c("message_delta",{delta:{stop_reason:z?"tool_use":"end_turn"},usage:he?{input_tokens:he.input_tokens,output_tokens:he.output_tokens}:{output_tokens:L}}),c("message_stop",{}),n.end();return}let f="",g="",_=[],y=new Re;for await(let R of r.track(m,l,o)){_.push(R),R.type==="response.output_text.delta"&&typeof R.delta=="string"&&(f+=R.delta),R.type==="response.reasoning_text.delta"&&typeof R.delta=="string"&&(g+=R.delta);let L=Ge(R);if(L&&y.add(L),R.type==="error"){let S=R.error,x=S?.type==="invalid_request_error";me(n,x?400:502,x?"invalid_request_error":"api_error",S?.message??JSON.stringify(R));return}if(n.destroyed)break}if(n.destroyed)return;let h=pe(_);_e(o,h);let w;try{w=or(h),w.length===0&&y.size>0&&(w=or({output:y.items()}))}catch(R){me(n,502,"api_error",String(R instanceof Error?R.message:R));return}let A=Ue(p,w.length,f);if(A){me(n,502,"api_error",Le(l,A));return}let v=[],b=g||ri(h);b&&v.push({type:"thinking",thinking:b}),(f||w.length===0)&&v.push({type:"text",text:f}),v.push(...w),J(n,200,{id:a,type:"message",role:"assistant",content:v,model:i,stop_reason:w.length>0?"tool_use":"end_turn",usage:on(h)??{input_tokens:0,output_tokens:0}})}function Bd(t){de(process.stdout,t)}function jd(t){let{clients:e,apiKey:n,identity:r,statusOf:o}=t,s=typeof e=="function"?e:()=>e,i=t.requestLog??Bd,a=t.observe,u=new $e;return(d,l)=>{let p=new URL(d.url??"/","http://localhost"),m=p.pathname==="/v1/messages"||p.pathname.startsWith("/v1/messages/"),c=(h,w,A,v,b)=>m?me(l,h,v,w):K(l,h,w,A,b),f=An(p.pathname),g=p.pathname.startsWith("/partners/"),_=f||g?{id:ot(),startedAt:performance.now(),streamError:!1,model:null,stream:null,org:null,ttftMs:null,catalog:{kind:"unresolved",reason:"no lane resolved a model for this request"},promptTokens:null,completionTokens:null,outputUnits:null,outputMinutes:null,sessionId:null,timing:null}:null;_&&(l.setHeader(Ao,_.id),l.once("close",()=>{let h=Po({turn:_,method:d.method??"",path:p.pathname,res:l});i(Co(h)),a?.(h),zs({record:h,ledger:t.ledger??null,req:d})}));let y=()=>Io(p.pathname,_);(async()=>{if(d.method==="GET"&&p.pathname==="/healthz"){J(l,200,{ok:!0});return}if(d.method==="GET"&&p.pathname==="/stats"){J(l,200,r?{identity:r,...u.snapshot()}:u.snapshot());return}if(n&&(d.headers.authorization??"")!==`Bearer ${n}`){c(401,"invalid local proxy api key","authentication_error","authentication_error");return}let h=p.pathname===Fs?"canonical":p.pathname===Qn?"huggingface":null;if(h){if(d.method!=="POST"){c(405,`${p.pathname} is a POST batch lookup`,"invalid_request_error","invalid_request_error");return}let b=t.usage;if(!b){c(501,`${p.pathname} needs a usage seam (ProxyHandlerOptions.usage) to read the per-request inference ledger; this proxy has none, and answering an empty result would be indistinguishable from "nothing is priced yet"`,"not_implemented","not_implemented_error");return}let R=b.authorize(d),L;try{L=await Zs(d,Bs)}catch(C){if(C instanceof wt){c(413,C.message,"request_too_large","request_too_large");return}c(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let S=C=>b.lookup(R,C),x;try{if(h==="canonical"){let C=js(L),F=await S(C);en(F,C),x={requests:F}}else x=await qs(L,S)}catch(C){if(C instanceof Ee){c(C.status,C.message,"invalid_request_error","invalid_request_error");return}throw C}J(l,200,x);return}if(d.method==="GET"&&p.pathname===et){let b=await s(d);if(_&&(_.org=b.tenant??null),!b.openRouterModels){c(501,`GET ${et} needs a router-backed client (openRouterModels)`,"not_implemented","not_implemented_error");return}J(l,200,await b.openRouterModels());return}if(p.pathname.startsWith("/partners/")){c(404,`no partner adapter for ${d.method} ${p.pathname} \u2014 the partner surface currently carries ${Qn} and GET ${et}`,"invalid_request_error","not_found_error");return}let w=await s(d);if(_&&(_.org=w.tenant??null),d.method==="GET"&&p.pathname==="/v1/models"){if(p.searchParams.get("format")==="openrouter"){l.writeHead(308,{location:et}),l.end();return}J(l,200,await w.listModels());return}if(d.method==="GET"&&p.pathname===hs){if(!w.listImageModels){c(501,"GET /v1/images/models needs a router-backed client (listImageModels)","not_implemented","not_implemented_error");return}J(l,200,await w.listImageModels());return}if(gs(p.pathname)){if(!w.supportsImages){c(501,`the image lanes need a router-backed client (supportsImages) to gate model capability for ${p.pathname}`,"not_implemented","not_implemented_error");return}let b={clients:w,imageModels:w.supportsImages,stats:u,turn:y()};p.pathname===zt?await Es(d,l,b):await xs(d,l,b);return}if(d.method==="GET"&&p.pathname===Ms){if(!w.listSpeechToTextModels){c(501,"GET /v1/audio/models needs a router-backed client (listSpeechToTextModels)","not_implemented","not_implemented_error");return}J(l,200,await w.listSpeechToTextModels());return}if($s(p.pathname)){if(!w.supportsTranscription){c(501,`the transcription lane needs a router-backed client (supportsTranscription) to gate model capability for ${Zt}`,"not_implemented","not_implemented_error");return}let b={clients:w,transcriptionModels:w.supportsTranscription,stats:u,turn:y()};await Ns(d,l,b);return}if(Ps(p.pathname)){if(!w.supportsSpeech){c(501,`the speech lane needs a router-backed client (supportsSpeech) to gate model capability for ${pt}`,"not_implemented","not_implemented_error");return}let b={clients:w,speechModels:w.supportsSpeech,stats:u,turn:y()};await Cs(d,l,b);return}if(d.method!=="POST"){c(404,`no route for ${d.method} ${p.pathname}`,"invalid_request_error","not_found_error");return}let A;try{A=await Zs(d)}catch(b){if(b instanceof wt){c(413,b.message,"request_too_large","request_too_large");return}c(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let v=Buffer.byteLength(JSON.stringify(A));if(p.pathname==="/v1/responses"){u.request("responses",v,v),await Td(A,w,it(w),l,u,y());return}if(p.pathname==="/v1/chat/completions"){u.request("chat_completions",v,v),await Pd(A,w,l,u,y(),d.headers[er]);return}if(p.pathname==="/v1/systemone"){u.request("systemone",v,v),await $d(A,w,l,u,y());return}if(p.pathname==="/v1/messages"){u.request("messages",v,v),await Fd(A,w,l,u,y());return}if(p.pathname==="/v1/messages/count_tokens"){u.request("count_tokens",v,v),me(l,404,"not_found_error","count_tokens is not implemented by the uRun proxy: the serve transport exposes no token-count lane, and the proxy will not fabricate counts");return}c(404,`no route for POST ${p.pathname}`,"invalid_request_error","not_found_error")})().catch(h=>{let w=String(h instanceof Error?h.message:h);if(!l.headersSent){let A=o?.(h)??null;if(A){c(A.status,w,A.openaiType,A.anthropicType);return}if(h instanceof un.SessionAllocationError){let v=Ft(h.code);if(v){let b=h.retryAfterSeconds??(v.status===429?30:void 0);b!==void 0&&l.setHeader("Retry-After",String(b)),c(v.status,$n(v,h.code??void 0),v.openaiType,v.anthropicType,v.code);return}}if(h instanceof un.SessionFailedError){let v=Bt(h);if(v){let b=v.status===429?30:void 0;b!==void 0&&l.setHeader("Retry-After",String(b)),c(v.status,$n(v,h.status.sessionId?`session ${h.status.sessionId}`:void 0),v.openaiType,v.anthropicType,v.code);return}}if(h instanceof ze){c(502,w,"upstream_error","api_error");return}c(500,w,"proxy_error","api_error","proxy_error");return}_&&(_.streamError=!0),l.destroyed||(m?l.write(`event: error
62
- data: ${JSON.stringify({type:"error",error:{type:"api_error",message:w}})}
63
-
64
- `):l.write(`data: ${JSON.stringify({error:{message:w,type:"upstream_error"}})}
65
-
66
- `)),l.end()})}}function oi(t){let{clients:e,apiKey:n}=t,r=(0,ti.createServer)(jd(t)),o=No(r,{clients:e,apiKey:n,statusOf:t.statusOf}),s=Kn(r,{resolver:fs(e,n)}),i=r.close.bind(r);return r.close=(a=>(o.close(),s(),i(a))),xo(r,{clients:e,apiKey:n}),r}var ln=require("@urun-sh/core/internal");var qd={ensureWebRTC:ln.ensureNodeWebRTC,webrtcUnavailableReason:ln.nodeWebRTCUnavailableReason,audioBackend:Rt};async function si(t=qd){try{await t.ensureWebRTC()}catch(n){return{ok:!1,reason:`the WebRTC bootstrap threw: ${String(n)}`}}let e=t.webrtcUnavailableReason();if(e)return{ok:!1,reason:`${e} \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 t.audioBackend()).createSource()}catch(n){return{ok:!1,reason:`the werift/opus audio backend could not be constructed: ${String(n)}`}}return{ok:!0,reason:null}}function Hd(t,e){let n=(t.URUN_CATALOG_URL??"").trim(),r=n?jt(n,"URUN_CATALOG_URL"):"",o=(t.URUN_CATALOG_ANON_KEY??"").trim();return!r||!o?(e(`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
67
- `),null):{catalogUrl:r,anonKey:o}}function Gd(t){return no({baseUrl:t.baseUrl,orgId:t.orgId,defaultApp:t.appId,fnName:t.fnName,apiUrl:t.apiUrl,auth:t.auth,catalog:Hd(M.default.env,e=>M.default.stderr.write(e)),statelessDirect:di(t)})}function di(t){return t.decisionSecret!==""&&t.auth.lane==="api-key"}function Vd(t,e){if(di(t))return M.default.stderr.write(`urun compat: stateless direct path ENABLED (POST /v1/decision answered by the serving pods)
68
- `),new rn({targetFor:n=>e.targetFor(n),apiUrl:t.apiUrl,fnName:t.fnName,apiKey:t.auth.apiKey,orgId:t.orgId,secret:t.decisionSecret})}async function ii(t,e,n){let r=(0,ar.spawn)(t,e,{stdio:"inherit",env:n});return new Promise(o=>{r.once("exit",s=>o(s??1)),r.once("error",s=>{M.default.stderr.write(`failed to launch ${t}: ${String(s)}
69
- `),o(127)})})}function ai(){return zo(M.default.env,O,ce.existsSync)}function li(t){for(let[e,n]of Object.entries(Un(t)))M.default.stdout.write(`export ${e}=${n}
70
- `)}async function ui(t,e){let n=We(M.default.env),r=e?Dn(n):void 0,o=Gd(n),s=null,i=d=>{if(s===null){let l=a.address();if(l===null)throw new Error("urun compat: a /v1 request was recorded before the proxy socket was listening");s=Bn(l.port),(0,ce.mkdirSync)(ur.default.dirname(s),{recursive:!0})}(0,ce.appendFileSync)(s,d)},a=oi({clients:ro(o,void 0,void 0,Vd(n,o)),identity:r,...e?{requestLog:i}:{}});try{await new Promise((d,l)=>{a.once("error",l),a.listen(t,"127.0.0.1",d)})}catch(d){throw await o.closeAll().catch(()=>{}),d.code==="EADDRINUSE"?new Error(ss(t,await Fn(t))):d}let u=a.address().port;return e&&(M.default.stdout.write(`urun openai proxy listening on http://127.0.0.1:${u}/v1
71
- `),li(u)),{port:u,close:async()=>{let d=new Promise(l=>a.close(()=>l()));a.closeAllConnections(),await d,await o.closeAll()}}}async function Jd(){let t=M.default.argv.slice(2),e=Xo(t),n=t.shift()??"proxy";if(n==="env")return li(Qo(e??Nn(t))),0;if(n==="selftest"){let{ok:o,reason:s}=await si();return o?(M.default.stdout.write(`urun-openai selftest: OK \u2014 werift WebRTC peer globals installed and the werift/opus audio backend constructed from this artifact
72
- `),0):(M.default.stderr.write(`urun-openai selftest: FAIL \u2014 ${s}
73
- `),1)}if(n==="proxy"){let o=e??Nn(t),s=await ui(o,!0);return await new Promise(i=>{M.default.once("SIGINT",i),M.default.once("SIGTERM",i)}),await s.close(),0}let r=n==="run"?t.shift():n;{if(!r)throw new Error(`launch needs an agent (${Object.keys(qt).join("|")}) or \`-- <cmd>\``);if(r==="pi"){let h=We(M.default.env);return ii("pi",["-e",ai(),...Wo(h.appId,t)],M.default.env)}if(r==="omp"){let h=We(M.default.env);return ii("omp",["-e",ai(),...Ko(h.appId,t)],M.default.env)}let o,s,i="openai";if(r==="--"){if(o=t.shift()??"",s=t,!o)throw new Error("`-- <cmd>` needs a command to launch")}else{let h=qt[r];if(!h)throw new Error(`unknown command/agent ${r}; commands: proxy | env; agents: ${Object.keys(qt).join(" | ")} \u2014 or \`-- <cmd>\``);o=h.bin,s=[...h.args??[],...t],i=h.env}let a=await es(e,Dn(We(M.default.env))),u=null,d=a.port;a.mode==="reuse"?M.default.stdout.write(`reusing the running urun openai proxy on http://127.0.0.1:${d} (app ${a.identity.app}, fn ${a.identity.fn}, v${a.identity.proxy_version})
74
- `):a.reason&&M.default.stderr.write(`urun compat: ${a.reason}
75
- `);let l=null,p=null,m=null,c=null;a.mode!=="reuse"&&(u=await ui(a.port,!1),d=u.port,c=Bn(d),(0,ce.mkdirSync)(ur.default.dirname(c),{recursive:!0}),m=(0,ce.createWriteStream)(c,{flags:"a"}),p=rs({streams:M.default,note:h=>(0,ce.appendFileSync)(c,h)}).uninstall,m.on("error",h=>{l?.(),l=null,de(M.default.stderr,`urun compat: proxy log ${c} failed: ${String(h)} \u2014 proxy output continues on the terminal
76
- `)}),M.default.stdout.write(`urun openai proxy on http://127.0.0.1:${d}/v1 \u2014 proxy logs: ${c}
77
- `),l=ts(m)),r==="codex"&&(s=Jo(d,We(M.default.env).appId,s));let f=(0,ar.spawn)(o,s,{stdio:"inherit",env:Vo(M.default.env,d,i)}),g=c!==null?os({logPath:c,appendSync:ce.appendFileSync,restore:()=>{l?.(),l=null},writeStderr:h=>M.default.stderr.write(h),killChild:()=>f.kill("SIGTERM"),exit:h=>M.default.exit(h),proc:M.default}):null,_=await new Promise(h=>{f.once("exit",w=>h(w??1)),f.once("error",w=>{l?.(),l=null,de(M.default.stderr,`failed to launch ${o}: ${String(w)}
78
- `),h(127)})});g?.();let y=null;try{u&&await u.close()}catch(h){y=h}finally{l?.(),l=null,p?.(),p=null,m?.end()}return y!==null&&de(M.default.stderr,`urun compat: proxy teardown failed after the harness exited (harness exit code ${_} preserved): ${String(y instanceof Error?y.message:y)}
79
- `),_}}Jd().then(t=>M.default.exit(t),t=>{M.default.stderr.write(`${String(t instanceof Error?t.message:t)}
80
- `),M.default.exit(1)});
58
+ `),n.end();return}finally{F?.flush(),F?.dispose()}}let f="",g="",b=[],y=new xe;for await(let S of r.track(c,p,o)){b.push(S),S.type==="response.output_text.delta"&&typeof S.delta=="string"&&(f+=S.delta),S.type==="response.reasoning_text.delta"&&typeof S.delta=="string"&&(g+=S.delta);let R=Ve(S);if(R&&y.add(R),S.type==="error"){ar(n,S);return}}let h=pe(b);_e(o,h);let v=or(h),A=v.length>0?v:or({output:y.items()}),w=Ue(m,A.length,f);if(w){K(n,502,Le(p,w),"upstream_error");return}let _=A.map(({index:S,...R})=>R),x={role:"assistant",content:f||null},C=g||ci(h);C&&(x.reasoning_content=C),_.length>0&&(x.tool_calls=_),J(n,200,{id:d,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:u,choices:[{index:0,message:x,finish_reason:_.length>0?"tool_calls":"stop"}],usage:ui(h)})}var Ud="reflex-latest";async function Nd(t,e,n,r,o){if(t===null||typeof t!="object"||Array.isArray(t)){K(n,400,"systemone requires a JSON object body");return}delete t.key;let s=typeof t.model=="string"&&t.model?t.model:Ud,i=ne({model:s});if(o.model=i,o.stream=!1,t.state===void 0||t.state===null){K(n,400,"systemone requires a state (string or JSON structure)");return}let a=t.questions;if(a===null||typeof a!="object"||Array.isArray(a)||Object.keys(a).length===0){K(n,400,"systemone requires a questions object (question id -> {kind, options, ...})");return}if(t.media!==void 0&&!Array.isArray(t.media)){K(n,400,"systemone media, when present, must be an array");return}if(r.model(i),await ln(o,e,s),e.stateless?.enabled){let c;try{c=await e.stateless.targetFor(s)}catch(f){if(f instanceof U){ge(n,"openai",f);return}throw f}if(c!==null){try{let f=await e.stateless.decide(t,s);o.catalog=ot(f.target),_e(o,f.completed),J(n,200,f.body)}catch(f){if(f instanceof U){ge(n,"openai",f);return}if(f instanceof De){J(n,f.status,f.body);return}if(f instanceof fe){K(n,503,f.message,"upstream_error");return}throw f}return}}let u;try{u=await e.createResponse({model:s,input:[{role:"user",content:JSON.stringify(t)}],stream:!0},ie(o))}catch(c){if(c instanceof U){ge(n,"openai",c);return}throw c}let d=[];try{for await(let c of r.track(u,i,o))if(d.push(c),c.type==="error"){ar(n,c);return}}catch(c){if(c instanceof U){ge(n,"openai",c);return}throw c}let l=pe(d);if(l==null){K(n,502,"upstream produced no response.completed event","upstream_error");return}_e(o,l);let p=Je(d,l),m;try{m=JSON.parse(p)}catch{K(n,502,"systemone answer was not valid JSON","upstream_error");return}if(m===null||typeof m!="object"||Array.isArray(m)){K(n,502,"systemone answer was not a JSON object","upstream_error");return}J(n,200,m)}function Dd(t){return typeof t=="string"?t:Array.isArray(t)?t.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var vt=class extends Error{};function un(t){return new vt(`unsupported anthropic content block type "${t}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function Fd(t){if(Array.isArray(t))return t.map(e=>{let n=e;return{type:"function",name:n.name,description:n.description,parameters:n.input_schema}})}function Bd(t){if(t==null)return;let e=t.type;if(e==="auto")return"auto";if(e==="none")return"none";if(e==="any")return"required";if(e==="tool")return{type:"function",name:t.name};throw new vt(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function jd(t){if(typeof t=="string")return t;if(t==null)return"";if(!Array.isArray(t))throw un(typeof t);let e="";for(let n of t){let r=String(n.type??"");if(r!=="text")throw un(`tool_result > ${r}`);e+=String(n.text??"")}return e}function qd(t){let{role:e,content:n}=t;if(typeof n=="string")return[{role:e,content:n}];if(n==null)return[];if(!Array.isArray(n))throw un(typeof n);let r=[],o="",s=()=>{o&&(r.push({role:e,content:o}),o="")};for(let i of n){let a=String(i.type??"");if(a==="text")o+=String(i.text??"");else if(a==="tool_use")s(),r.push({type:"function_call",call_id:i.id,name:i.name,arguments:JSON.stringify(i.input??{})});else if(a==="tool_result")s(),r.push({type:"function_call_output",call_id:i.tool_use_id,output:jd(i.content)});else{if(a==="thinking"||a==="redacted_thinking")continue;throw un(a)}}return s(),r}function sr(t){return Ge(t).map((e,n)=>{let r;try{r=JSON.parse(e.arguments||"{}")}catch{throw new Error(`upstream function_call "${e.name??""}" arguments are not valid JSON`)}return{type:"tool_use",id:e.call_id??e.id??`toolu_${n}`,name:e.name??"",input:r}})}async function Hd(t,e,n,r,o){let s=t.messages;if(!Array.isArray(s)){me(n,400,"invalid_request_error","messages requires a messages array");return}if(typeof t.max_tokens!="number"||!Number.isInteger(t.max_tokens)||t.max_tokens<1){me(n,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(t.top_k!==void 0){me(n,400,"invalid_request_error","top_k is not supported: the uRun serve lane exposes temperature/top_p/stop_sequences only");return}if(t.stop_sequences!==void 0&&(!Array.isArray(t.stop_sequences)||t.stop_sequences.some(x=>typeof x!="string"))){me(n,400,"invalid_request_error","stop_sequences must be an array of strings");return}let i=String(t.model??"urun"),a=Po(o.id),u=[],d;try{t.system&&u.push({role:"system",content:Dd(t.system)});for(let x of s)u.push(...qd(x));d=Bd(t.tool_choice)}catch(x){if(x instanceof vt){me(n,400,"invalid_request_error",x.message);return}throw x}let l=ne(t);r.model(l),o.model=l,o.stream=t.stream===!0,await ln(o,e,typeof t.model=="string"?t.model:void 0);let p=Array.isArray(t.tools)&&t.tools.length>0,m;try{m=await e.createResponse({model:t.model,input:u,stream:!0,tools:Fd(t.tools),tool_choice:d,temperature:t.temperature,max_output_tokens:t.max_tokens,top_p:t.top_p,stop:t.stop_sequences},ie(o))}catch(x){if(x instanceof U){ge(n,"anthropic",x);return}throw x}let c=(x,C)=>{n.destroyed||n.write(`event: ${x}
59
+ data: ${JSON.stringify({type:x,...C})}
60
+
61
+ `)};if(t.stream===!0){let x=r.track(m,l,o);ir(n),c("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:i,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let C=0,S="",R=0,$="none",F=()=>$,Y=-1,te=-1,ue=new xe,ve=[],G=()=>{$!=="none"&&($="none",c("content_block_stop",{index:Y}))},V=(B,ee)=>{G(),$=B,Y=R++,c("content_block_start",{index:Y,content_block:ee})};for await(let B of x){if(n.destroyed)break;ve.push(B);let ee=Ve(B);if(B.type==="response.reasoning_text.delta"&&typeof B.delta=="string")F()!=="thinking"&&V("thinking",{type:"thinking",thinking:""}),c("content_block_delta",{index:Y,delta:{type:"thinking_delta",thinking:B.delta}});else if(B.type==="response.output_text.delta"&&typeof B.delta=="string")F()!=="text"&&V("text",{type:"text",text:""}),C+=1,S+=B.delta,c("content_block_delta",{index:Y,delta:{type:"text_delta",text:B.delta}});else if(ee){let{call:be}=ue.add(ee);(F()!=="tool"||ee.tool_index!==te)&&(te=ee.tool_index,V("tool",{type:"tool_use",id:be.id??`call_${ee.tool_index}`,name:be.name??"",input:{}})),ee.delta&&c("content_block_delta",{index:Y,delta:{type:"input_json_delta",partial_json:ee.delta}})}else if(B.type==="error"){o.streamError=!0,c("error",{error:{type:"api_error",message:JSON.stringify(B)}}),n.end();return}}if(n.destroyed)return;let Z=pe(ve),Ae=Ge(Z),T=Ue(p,ue.size+Ae.length,S);if(T){o.streamError=!0,c("error",{error:{type:"api_error",message:Le(l,T)}}),n.end();return}G();let N=[];if(ue.size===0)try{N=sr(Z)}catch(B){o.streamError=!0,c("error",{error:{type:"api_error",message:String(B instanceof Error?B.message:B)}}),n.end();return}R===0&&N.length===0&&(V("text",{type:"text",text:""}),G()),N.forEach(B=>{let ee=R++;c("content_block_start",{index:ee,content_block:{...B,input:{}}}),c("content_block_delta",{index:ee,delta:{type:"input_json_delta",partial_json:JSON.stringify(B.input??{})}}),c("content_block_stop",{index:ee})});let z=ue.size>0||N.length>0,he=sn(Z);_e(o,Z),c("message_delta",{delta:{stop_reason:z?"tool_use":"end_turn"},usage:he?{input_tokens:he.input_tokens,output_tokens:he.output_tokens}:{output_tokens:C}}),c("message_stop",{}),n.end();return}let f="",g="",b=[],y=new xe;for await(let x of r.track(m,l,o)){b.push(x),x.type==="response.output_text.delta"&&typeof x.delta=="string"&&(f+=x.delta),x.type==="response.reasoning_text.delta"&&typeof x.delta=="string"&&(g+=x.delta);let C=Ve(x);if(C&&y.add(C),x.type==="error"){let S=x.error,R=S?.type==="invalid_request_error";me(n,R?400:502,R?"invalid_request_error":"api_error",S?.message??JSON.stringify(x));return}if(n.destroyed)break}if(n.destroyed)return;let h=pe(b);_e(o,h);let v;try{v=sr(h),v.length===0&&y.size>0&&(v=sr({output:y.items()}))}catch(x){me(n,502,"api_error",String(x instanceof Error?x.message:x));return}let A=Ue(p,v.length,f);if(A){me(n,502,"api_error",Le(l,A));return}let w=[],_=g||ci(h);_&&w.push({type:"thinking",thinking:_}),(f||v.length===0)&&w.push({type:"text",text:f}),w.push(...v),J(n,200,{id:a,type:"message",role:"assistant",content:w,model:i,stop_reason:v.length>0?"tool_use":"end_turn",usage:sn(h)??{input_tokens:0,output_tokens:0}})}function Gd(t){de(process.stdout,t)}function Vd(t){let{clients:e,apiKey:n,identity:r,statusOf:o}=t,s=typeof e=="function"?e:()=>e,i=t.requestLog??Gd,a=t.observe,u=new $e;return(d,l)=>{let p=new URL(d.url??"/","http://localhost"),m=p.pathname==="/v1/messages"||p.pathname.startsWith("/v1/messages/"),c=(h,v,A,w,_)=>m?me(l,h,w,v):K(l,h,v,A,_),f=In(p.pathname),g=p.pathname.startsWith("/partners/"),b=f||g?{id:it(),startedAt:performance.now(),streamError:!1,model:null,stream:null,org:null,ttftMs:null,catalog:{kind:"unresolved",reason:"no lane resolved a model for this request"},promptTokens:null,completionTokens:null,outputUnits:null,outputMinutes:null,sessionId:null,timing:null}:null;b&&(l.setHeader(Io,b.id),l.once("close",()=>{let h=Co({turn:b,method:d.method??"",path:p.pathname,res:l});i($o(h)),a?.(h),ri({record:h,ledger:t.ledger??null,req:d})}));let y=()=>Oo(p.pathname,b);(async()=>{if(d.method==="GET"&&p.pathname==="/healthz"){J(l,200,{ok:!0});return}if(d.method==="GET"&&p.pathname==="/stats"){J(l,200,r?{identity:r,...u.snapshot()}:u.snapshot());return}if(n&&(d.headers.authorization??"")!==`Bearer ${n}`){c(401,"invalid local proxy api key","authentication_error","authentication_error");return}let h=p.pathname===Ws?"canonical":p.pathname===Zn?"huggingface":null;if(h){if(d.method!=="POST"){c(405,`${p.pathname} is a POST batch lookup`,"invalid_request_error","invalid_request_error");return}let _=t.usage;if(!_){c(501,`${p.pathname} needs a usage seam (ProxyHandlerOptions.usage) to read the per-request inference ledger; this proxy has none, and answering an empty result would be indistinguishable from "nothing is priced yet"`,"not_implemented","not_implemented_error");return}let x=_.authorize(d),C;try{C=await ai(d,Ks)}catch($){if($ instanceof _t){c(413,$.message,"request_too_large","request_too_large");return}c(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let S=$=>_.lookup(x,$),R;try{if(h==="canonical"){let $=zs(C),F=await S($);tn(F,$),R={requests:F}}else R=await Ys(C,S)}catch($){if($ instanceof Ee){c($.status,$.message,"invalid_request_error","invalid_request_error");return}throw $}J(l,200,R);return}if(d.method==="GET"&&p.pathname===nt){let _=await s(d);if(b&&(b.org=_.tenant??null),!_.openRouterModels){c(501,`GET ${nt} needs a router-backed client (openRouterModels)`,"not_implemented","not_implemented_error");return}J(l,200,await _.openRouterModels());return}if(p.pathname.startsWith("/partners/")){c(404,`no partner adapter for ${d.method} ${p.pathname} \u2014 the partner surface currently carries ${Zn} and GET ${nt}`,"invalid_request_error","not_found_error");return}let v=await s(d);if(b&&(b.org=v.tenant??null),d.method==="GET"&&p.pathname==="/v1/models"){if(p.searchParams.get("format")==="openrouter"){l.writeHead(308,{location:nt}),l.end();return}J(l,200,await v.listModels());return}if(d.method==="GET"&&p.pathname===ys){if(!v.listImageModels){c(501,"GET /v1/images/models needs a router-backed client (listImageModels)","not_implemented","not_implemented_error");return}J(l,200,await v.listImageModels());return}if(hs(p.pathname)){if(!v.supportsImages){c(501,`the image lanes need a router-backed client (supportsImages) to gate model capability for ${p.pathname}`,"not_implemented","not_implemented_error");return}let _={clients:v,imageModels:v.supportsImages,stats:u,turn:y()};p.pathname===Yt?await As(d,l,_):await Es(d,l,_);return}if(d.method==="GET"&&p.pathname===qs){if(!v.listSpeechToTextModels){c(501,"GET /v1/audio/models needs a router-backed client (listSpeechToTextModels)","not_implemented","not_implemented_error");return}J(l,200,await v.listSpeechToTextModels());return}if(js(p.pathname)){if(!v.supportsTranscription){c(501,`the transcription lane needs a router-backed client (supportsTranscription) to gate model capability for ${en}`,"not_implemented","not_implemented_error");return}let _={clients:v,transcriptionModels:v.supportsTranscription,stats:u,turn:y()};await Vs(d,l,_);return}if(Us(p.pathname)){if(!v.supportsSpeech){c(501,`the speech lane needs a router-backed client (supportsSpeech) to gate model capability for ${ft}`,"not_implemented","not_implemented_error");return}let _={clients:v,speechModels:v.supportsSpeech,stats:u,turn:y()};await Bs(d,l,_);return}if(d.method!=="POST"){c(404,`no route for ${d.method} ${p.pathname}`,"invalid_request_error","not_found_error");return}let A;try{A=await ai(d)}catch(_){if(_ instanceof _t){c(413,_.message,"request_too_large","request_too_large");return}c(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let w=Buffer.byteLength(JSON.stringify(A));if(p.pathname==="/v1/responses"){u.request("responses",w,w),await Md(A,v,ut(v),l,u,y());return}if(p.pathname==="/v1/chat/completions"){u.request("chat_completions",w,w),await Ld(A,v,l,u,y(),d.headers[tr]);return}if(p.pathname==="/v1/systemone"){u.request("systemone",w,w),await Nd(A,v,l,u,y());return}if(p.pathname==="/v1/messages"){u.request("messages",w,w),await Hd(A,v,l,u,y());return}if(p.pathname==="/v1/messages/count_tokens"){u.request("count_tokens",w,w),me(l,404,"not_found_error","count_tokens is not implemented by the uRun proxy: the serve transport exposes no token-count lane, and the proxy will not fabricate counts");return}c(404,`no route for POST ${p.pathname}`,"invalid_request_error","not_found_error")})().catch(h=>{let v=String(h instanceof Error?h.message:h);if(!l.headersSent){let A=o?.(h)??null;if(A){c(A.status,v,A.openaiType,A.anthropicType);return}if(h instanceof dn.SessionAllocationError){let w=Bt(h.code);if(w){let _=h.retryAfterSeconds??(w.status===429?30:void 0);_!==void 0&&l.setHeader("Retry-After",String(_)),c(w.status,Mn(w,h.code??void 0),w.openaiType,w.anthropicType,w.code);return}}if(h instanceof dn.SessionFailedError){let w=jt(h);if(w){let _=w.status===429?30:void 0;_!==void 0&&l.setHeader("Retry-After",String(_)),c(w.status,Mn(w,h.status.sessionId?`session ${h.status.sessionId}`:void 0),w.openaiType,w.anthropicType,w.code);return}}if(h instanceof Xe){c(502,v,"upstream_error","api_error");return}c(500,v,"proxy_error","api_error","proxy_error");return}b&&(b.streamError=!0),l.destroyed||(m?l.write(`event: error
62
+ data: ${JSON.stringify({type:"error",error:{type:"api_error",message:v}})}
63
+
64
+ `):l.write(`data: ${JSON.stringify({error:{message:v,type:"upstream_error"}})}
65
+
66
+ `)),l.end()})}}function pi(t){let{clients:e,apiKey:n}=t,r=(0,di.createServer)(Vd(t)),o=Do(r,{clients:e,apiKey:n,statusOf:t.statusOf}),s=zn(r,{resolver:ms(e,n)}),i=r.close.bind(r);return r.close=(a=>(o.close(),s(),i(a))),Eo(r,{clients:e,apiKey:n}),r}var cn=require("@urun-sh/core/internal");var Jd={ensureWebRTC:cn.ensureNodeWebRTC,webrtcUnavailableReason:cn.nodeWebRTCUnavailableReason,audioBackend:Rt};async function fi(t=Jd){try{await t.ensureWebRTC()}catch(n){return{ok:!1,reason:`the WebRTC bootstrap threw: ${String(n)}`}}let e=t.webrtcUnavailableReason();if(e)return{ok:!1,reason:`${e} \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 t.audioBackend()).createSource()}catch(n){return{ok:!1,reason:`the werift/opus audio backend could not be constructed: ${String(n)}`}}return{ok:!0,reason:null}}function Wd(t,e){let n=(t.URUN_CATALOG_URL??"").trim(),r=n?qt(n,"URUN_CATALOG_URL"):"",o=(t.URUN_CATALOG_ANON_KEY??"").trim();return!r||!o?(e(`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
67
+ `),null):{catalogUrl:r,anonKey:o}}function Kd(t){return ro({baseUrl:t.baseUrl,orgId:t.orgId,defaultApp:t.appId,fnName:t.fnName,apiUrl:t.apiUrl,auth:t.auth,catalog:Wd(L.default.env,e=>L.default.stderr.write(e)),statelessDirect:yi(t)})}function yi(t){return t.decisionSecret!==""&&t.auth.lane==="api-key"}function zd(t,e){if(yi(t))return L.default.stderr.write(`urun compat: stateless direct path ENABLED (POST /v1/decision answered by the serving pods)
68
+ `),new on({targetFor:n=>e.targetFor(n),apiUrl:t.apiUrl,fnName:t.fnName,apiKey:t.auth.apiKey,orgId:t.orgId,secret:t.decisionSecret})}async function mi(t,e,n){let r=(0,ur.spawn)(t,e,{stdio:"inherit",env:n});return new Promise(o=>{r.once("exit",s=>o(s??1)),r.once("error",s=>{L.default.stderr.write(`failed to launch ${t}: ${String(s)}
69
+ `),o(127)})})}function gi(){return Yo(L.default.env,O,ce.existsSync)}function wi(t){for(let[e,n]of Object.entries(Nn(t)))L.default.stdout.write(`export ${e}=${n}
70
+ `)}async function hi(t,e){let n=Ke(L.default.env),r=e?Fn(n):void 0,o=Kd(n),s=null,i=d=>{if(s===null){let l=a.address();if(l===null)throw new Error("urun compat: a /v1 request was recorded before the proxy socket was listening");s=jn(l.port),(0,ce.mkdirSync)(dr.default.dirname(s),{recursive:!0})}(0,ce.appendFileSync)(s,d)},a=pi({clients:oo(o,void 0,void 0,zd(n,o)),identity:r,...e?{requestLog:i}:{}});try{await new Promise((d,l)=>{a.once("error",l),a.listen(t,"127.0.0.1",d)})}catch(d){throw await o.closeAll().catch(()=>{}),d.code==="EADDRINUSE"?new Error(is(t,await Bn(t))):d}let u=a.address().port;return e&&(L.default.stdout.write(`urun openai proxy listening on http://127.0.0.1:${u}/v1
71
+ `),wi(u)),{port:u,close:async()=>{let d=new Promise(l=>a.close(()=>l()));a.closeAllConnections(),await d,await o.closeAll()}}}async function Yd(){let t=L.default.argv.slice(2),e=Qo(t),n=t.shift()??"proxy";if(n==="env")return wi(Zo(e??Dn(t))),0;if(n==="selftest"){let{ok:o,reason:s}=await fi();return o?(L.default.stdout.write(`urun-openai selftest: OK \u2014 werift WebRTC peer globals installed and the werift/opus audio backend constructed from this artifact
72
+ `),0):(L.default.stderr.write(`urun-openai selftest: FAIL \u2014 ${s}
73
+ `),1)}if(n==="proxy"){let o=e??Dn(t),s=await hi(o,!0);return await new Promise(i=>{L.default.once("SIGINT",i),L.default.once("SIGTERM",i)}),await s.close(),0}let r=n==="run"?t.shift():n;{if(!r)throw new Error(`launch needs an agent (${Object.keys(Ht).join("|")}) or \`-- <cmd>\``);if(r==="pi"){let h=Ke(L.default.env);return mi("pi",["-e",gi(),...Ko(h.appId,t)],L.default.env)}if(r==="omp"){let h=Ke(L.default.env);return mi("omp",["-e",gi(),...zo(h.appId,t)],L.default.env)}let o,s,i="openai";if(r==="--"){if(o=t.shift()??"",s=t,!o)throw new Error("`-- <cmd>` needs a command to launch")}else{let h=Ht[r];if(!h)throw new Error(`unknown command/agent ${r}; commands: proxy | env; agents: ${Object.keys(Ht).join(" | ")} \u2014 or \`-- <cmd>\``);o=h.bin,s=[...h.args??[],...t],i=h.env}let a=await ts(e,Fn(Ke(L.default.env))),u=null,d=a.port;a.mode==="reuse"?L.default.stdout.write(`reusing the running urun openai proxy on http://127.0.0.1:${d} (app ${a.identity.app}, fn ${a.identity.fn}, v${a.identity.proxy_version})
74
+ `):a.reason&&L.default.stderr.write(`urun compat: ${a.reason}
75
+ `);let l=null,p=null,m=null,c=null;a.mode!=="reuse"&&(u=await hi(a.port,!1),d=u.port,c=jn(d),(0,ce.mkdirSync)(dr.default.dirname(c),{recursive:!0}),m=(0,ce.createWriteStream)(c,{flags:"a"}),p=os({streams:L.default,note:h=>(0,ce.appendFileSync)(c,h)}).uninstall,m.on("error",h=>{l?.(),l=null,de(L.default.stderr,`urun compat: proxy log ${c} failed: ${String(h)} \u2014 proxy output continues on the terminal
76
+ `)}),L.default.stdout.write(`urun openai proxy on http://127.0.0.1:${d}/v1 \u2014 proxy logs: ${c}
77
+ `),l=ns(m)),r==="codex"&&(s=Wo(d,Ke(L.default.env).appId,s));let f=(0,ur.spawn)(o,s,{stdio:"inherit",env:Jo(L.default.env,d,i)}),g=c!==null?ss({logPath:c,appendSync:ce.appendFileSync,restore:()=>{l?.(),l=null},writeStderr:h=>L.default.stderr.write(h),killChild:()=>f.kill("SIGTERM"),exit:h=>L.default.exit(h),proc:L.default}):null,b=await new Promise(h=>{f.once("exit",v=>h(v??1)),f.once("error",v=>{l?.(),l=null,de(L.default.stderr,`failed to launch ${o}: ${String(v)}
78
+ `),h(127)})});g?.();let y=null;try{u&&await u.close()}catch(h){y=h}finally{l?.(),l=null,p?.(),p=null,m?.end()}return y!==null&&de(L.default.stderr,`urun compat: proxy teardown failed after the harness exited (harness exit code ${b} preserved): ${String(y instanceof Error?y.message:y)}
79
+ `),b}}Yd().then(t=>L.default.exit(t),t=>{L.default.stderr.write(`${String(t instanceof Error?t.message:t)}
80
+ `),L.default.exit(1)});