@urun-sh/openai 0.6.0 → 0.6.1

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,71 +1,71 @@
1
1
  #!/usr/bin/env node
2
- "use strict";var Qi=Object.create;var Qr=Object.defineProperty;var Zi=Object.getOwnPropertyDescriptor;var ea=Object.getOwnPropertyNames;var ta=Object.getPrototypeOf,na=Object.prototype.hasOwnProperty;var ra=(t,e,n)=>()=>{if(n)throw n[0];try{return t&&(e=t(t=0)),e}catch(r){throw n=[r],r}};var oa=(t,e)=>()=>{try{return e||t((e={exports:{}}).exports,e),e.exports}catch(n){throw e=0,n}};var sa=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of ea(e))!na.call(t,o)&&o!==n&&Qr(t,o,{get:()=>e[o],enumerable:!(r=Zi(e,o))||r.enumerable});return t};var Ge=(t,e,n)=>(n=t!=null?Qi(ta(t)):{},sa(e||!t||!t.__esModule?Qr(n,"default",{value:t,enumerable:!0}):n,t));var S=ra(()=>{"use strict"});var eo=oa(()=>{"use strict";S();var Zr;(function(t){(function(e){var n=typeof globalThis=="object"?globalThis:typeof global=="object"?global:typeof self=="object"?self:typeof this=="object"?this:a(),r=o(t);typeof n.Reflect<"u"&&(r=o(n.Reflect,r)),e(r,n),typeof n.Reflect>"u"&&(n.Reflect=t);function o(u,d){return function(c,l){Object.defineProperty(u,c,{configurable:!0,writable:!0,value:l}),d&&d(c,l)}}function s(){try{return Function("return this;")()}catch{}}function i(){try{return(0,eval)("(function() { return this; })()")}catch{}}function a(){return s()||i()}})(function(e,n){var r=Object.prototype.hasOwnProperty,o=typeof Symbol=="function",s=o&&typeof Symbol.toPrimitive<"u"?Symbol.toPrimitive:"@@toPrimitive",i=o&&typeof Symbol.iterator<"u"?Symbol.iterator:"@@iterator",a=typeof Object.create=="function",u={__proto__:[]}instanceof Array,d=!a&&!u,c={create:a?function(){return Wn(Object.create(null))}:u?function(){return Wn({__proto__:null})}:function(){return Wn({})},has:d?function(f,h){return r.call(f,h)}:function(f,h){return h in f},get:d?function(f,h){return r.call(f,h)?f[h]:void 0}:function(f,h){return f[h]}},l=Object.getPrototypeOf(Function),p=typeof Map=="function"&&typeof Map.prototype.entries=="function"?Map:Ji(),m=typeof Set=="function"&&typeof Set.prototype.entries=="function"?Set:Ki(),b=typeof WeakMap=="function"?WeakMap:zi(),E=o?Symbol.for("@reflect-metadata:registry"):void 0,w=Gi(),g=Vi(w);function v(f,h,y,x){if(U(y)){if(!Gr(f))throw new TypeError;if(!Vr(h))throw new TypeError;return de(f,h)}else{if(!Gr(f))throw new TypeError;if(!H(h))throw new TypeError;if(!H(x)&&!U(x)&&!we(x))throw new TypeError;return we(x)&&(x=void 0),y=Ae(y),pe(f,h,y,x)}}e("decorate",v);function _(f,h){function y(x,L){if(!H(x))throw new TypeError;if(!U(L)&&!qi(L))throw new TypeError;he(f,h,x,L)}return y}e("metadata",_);function C(f,h,y,x){if(!H(y))throw new TypeError;return U(x)||(x=Ae(x)),he(f,h,y,x)}e("defineMetadata",C);function R(f,h,y){if(!H(h))throw new TypeError;return U(y)||(y=Ae(y)),oe(f,h,y)}e("hasMetadata",R);function O(f,h,y){if(!H(h))throw new TypeError;return U(y)||(y=Ae(y)),q(f,h,y)}e("hasOwnMetadata",O);function k(f,h,y){if(!H(h))throw new TypeError;return U(y)||(y=Ae(y)),Y(f,h,y)}e("getMetadata",k);function $(f,h,y){if(!H(h))throw new TypeError;return U(y)||(y=Ae(y)),Z(f,h,y)}e("getOwnMetadata",$);function j(f,h){if(!H(f))throw new TypeError;return U(h)||(h=Ae(h)),Ee(f,h)}e("getMetadataKeys",j);function W(f,h){if(!H(f))throw new TypeError;return U(h)||(h=Ae(h)),Oe(f,h)}e("getOwnMetadataKeys",W);function F(f,h,y){if(!H(h))throw new TypeError;if(U(y)||(y=Ae(y)),!H(h))throw new TypeError;U(y)||(y=Ae(y));var x=pt(h,y,!1);return U(x)?!1:x.OrdinaryDeleteMetadata(f,h,y)}e("deleteMetadata",F);function de(f,h){for(var y=f.length-1;y>=0;--y){var x=f[y],L=x(h);if(!U(L)&&!we(L)){if(!Vr(L))throw new TypeError;h=L}}return h}function pe(f,h,y,x){for(var L=f.length-1;L>=0;--L){var te=f[L],ie=te(h,y,x);if(!U(ie)&&!we(ie)){if(!H(ie))throw new TypeError;x=ie}}return x}function oe(f,h,y){var x=q(f,h,y);if(x)return!0;var L=Vn(h);return we(L)?!1:oe(f,L,y)}function q(f,h,y){var x=pt(h,y,!1);return U(x)?!1:Hr(x.OrdinaryHasOwnMetadata(f,h,y))}function Y(f,h,y){var x=q(f,h,y);if(x)return Z(f,h,y);var L=Vn(h);if(!we(L))return Y(f,L,y)}function Z(f,h,y){var x=pt(h,y,!1);if(!U(x))return x.OrdinaryGetOwnMetadata(f,h,y)}function he(f,h,y,x){var L=pt(y,x,!0);L.OrdinaryDefineOwnMetadata(f,h,y,x)}function Ee(f,h){var y=Oe(f,h),x=Vn(f);if(x===null)return y;var L=Ee(x,h);if(L.length<=0)return y;if(y.length<=0)return L;for(var te=new m,ie=[],V=0,I=y;V<I.length;V++){var T=I[V],P=te.has(T);P||(te.add(T),ie.push(T))}for(var M=0,J=L;M<J.length;M++){var T=J[M],P=te.has(T);P||(te.add(T),ie.push(T))}return ie}function Oe(f,h){var y=pt(f,h,!1);return y?y.OrdinaryOwnMetadataKeys(f,h):[]}function Se(f){if(f===null)return 1;switch(typeof f){case"undefined":return 0;case"boolean":return 2;case"string":return 3;case"symbol":return 4;case"number":return 5;case"object":return f===null?1:6;default:return 6}}function U(f){return f===void 0}function we(f){return f===null}function Be(f){return typeof f=="symbol"}function H(f){return typeof f=="object"?f!==null:typeof f=="function"}function ge(f,h){switch(Se(f)){case 0:return f;case 1:return f;case 2:return f;case 3:return f;case 4:return f;case 5:return f}var y=h===3?"string":h===5?"number":"default",x=Wr(f,s);if(x!==void 0){var L=x.call(f,y);if(H(L))throw new TypeError;return L}return Ft(f,y==="default"?"number":y)}function Ft(f,h){if(h==="string"){var y=f.toString;if(tt(y)){var x=y.call(f);if(!H(x))return x}var L=f.valueOf;if(tt(L)){var x=L.call(f);if(!H(x))return x}}else{var L=f.valueOf;if(tt(L)){var x=L.call(f);if(!H(x))return x}var te=f.toString;if(tt(te)){var x=te.call(f);if(!H(x))return x}}throw new TypeError}function Hr(f){return!!f}function ji(f){return""+f}function Ae(f){var h=ge(f,3);return Be(h)?h:ji(h)}function Gr(f){return Array.isArray?Array.isArray(f):f instanceof Object?f instanceof Array:Object.prototype.toString.call(f)==="[object Array]"}function tt(f){return typeof f=="function"}function Vr(f){return typeof f=="function"}function qi(f){switch(Se(f)){case 3:return!0;case 4:return!0;default:return!1}}function Gn(f,h){return f===h||f!==f&&h!==h}function Wr(f,h){var y=f[h];if(y!=null){if(!tt(y))throw new TypeError;return y}}function Jr(f){var h=Wr(f,i);if(!tt(h))throw new TypeError;var y=h.call(f);if(!H(y))throw new TypeError;return y}function Kr(f){return f.value}function zr(f){var h=f.next();return h.done?!1:h}function Yr(f){var h=f.return;h&&h.call(f)}function Vn(f){var h=Object.getPrototypeOf(f);if(typeof f!="function"||f===l||h!==l)return h;var y=f.prototype,x=y&&Object.getPrototypeOf(y);if(x==null||x===Object.prototype)return h;var L=x.constructor;return typeof L!="function"||L===f?h:L}function Hi(){var f;!U(E)&&typeof n.Reflect<"u"&&!(E in n.Reflect)&&typeof n.Reflect.defineMetadata=="function"&&(f=Wi(n.Reflect));var h,y,x,L=new b,te={registerProvider:ie,getProvider:I,setProvider:P};return te;function ie(M){if(!Object.isExtensible(te))throw new Error("Cannot add provider to a frozen registry.");switch(!0){case f===M:break;case U(h):h=M;break;case h===M:break;case U(y):y=M;break;case y===M:break;default:x===void 0&&(x=new m),x.add(M);break}}function V(M,J){if(!U(h)){if(h.isProviderFor(M,J))return h;if(!U(y)){if(y.isProviderFor(M,J))return h;if(!U(x))for(var X=Jr(x);;){var ne=zr(X);if(!ne)return;var Re=Kr(ne);if(Re.isProviderFor(M,J))return Yr(X),Re}}}if(!U(f)&&f.isProviderFor(M,J))return f}function I(M,J){var X=L.get(M),ne;return U(X)||(ne=X.get(J)),U(ne)&&(ne=V(M,J),U(ne)||(U(X)&&(X=new p,L.set(M,X)),X.set(J,ne))),ne}function T(M){if(U(M))throw new TypeError;return h===M||y===M||!U(x)&&x.has(M)}function P(M,J,X){if(!T(X))throw new Error("Metadata provider not registered.");var ne=I(M,J);if(ne!==X){if(!U(ne))return!1;var Re=L.get(M);U(Re)&&(Re=new p,L.set(M,Re)),Re.set(J,X)}return!0}}function Gi(){var f;return!U(E)&&H(n.Reflect)&&Object.isExtensible(n.Reflect)&&(f=n.Reflect[E]),U(f)&&(f=Hi()),!U(E)&&H(n.Reflect)&&Object.isExtensible(n.Reflect)&&Object.defineProperty(n.Reflect,E,{enumerable:!1,configurable:!1,writable:!1,value:f}),f}function Vi(f){var h=new b,y={isProviderFor:function(T,P){var M=h.get(T);return U(M)?!1:M.has(P)},OrdinaryDefineOwnMetadata:ie,OrdinaryHasOwnMetadata:L,OrdinaryGetOwnMetadata:te,OrdinaryOwnMetadataKeys:V,OrdinaryDeleteMetadata:I};return w.registerProvider(y),y;function x(T,P,M){var J=h.get(T),X=!1;if(U(J)){if(!M)return;J=new p,h.set(T,J),X=!0}var ne=J.get(P);if(U(ne)){if(!M)return;if(ne=new p,J.set(P,ne),!f.setProvider(T,P,y))throw J.delete(P),X&&h.delete(T),new Error("Wrong provider for target.")}return ne}function L(T,P,M){var J=x(P,M,!1);return U(J)?!1:Hr(J.has(T))}function te(T,P,M){var J=x(P,M,!1);if(!U(J))return J.get(T)}function ie(T,P,M,J){var X=x(M,J,!0);X.set(T,P)}function V(T,P){var M=[],J=x(T,P,!1);if(U(J))return M;for(var X=J.keys(),ne=Jr(X),Re=0;;){var Xr=zr(ne);if(!Xr)return M.length=Re,M;var Yi=Kr(Xr);try{M[Re]=Yi}catch(Xi){try{Yr(ne)}finally{throw Xi}}Re++}}function I(T,P,M){var J=x(P,M,!1);if(U(J)||!J.delete(T))return!1;if(J.size===0){var X=h.get(P);U(X)||(X.delete(M),X.size===0&&h.delete(X))}return!0}}function Wi(f){var h=f.defineMetadata,y=f.hasOwnMetadata,x=f.getOwnMetadata,L=f.getOwnMetadataKeys,te=f.deleteMetadata,ie=new b,V={isProviderFor:function(I,T){var P=ie.get(I);return!U(P)&&P.has(T)?!0:L(I,T).length?(U(P)&&(P=new m,ie.set(I,P)),P.add(T),!0):!1},OrdinaryDefineOwnMetadata:h,OrdinaryHasOwnMetadata:y,OrdinaryGetOwnMetadata:x,OrdinaryOwnMetadataKeys:L,OrdinaryDeleteMetadata:te};return V}function pt(f,h,y){var x=w.getProvider(f,h);if(!U(x))return x;if(y){if(w.setProvider(f,h,g))return g;throw new Error("Illegal state.")}}function Ji(){var f={},h=[],y=(function(){function V(I,T,P){this._index=0,this._keys=I,this._values=T,this._selector=P}return V.prototype["@@iterator"]=function(){return this},V.prototype[i]=function(){return this},V.prototype.next=function(){var I=this._index;if(I>=0&&I<this._keys.length){var T=this._selector(this._keys[I],this._values[I]);return I+1>=this._keys.length?(this._index=-1,this._keys=h,this._values=h):this._index++,{value:T,done:!1}}return{value:void 0,done:!0}},V.prototype.throw=function(I){throw this._index>=0&&(this._index=-1,this._keys=h,this._values=h),I},V.prototype.return=function(I){return this._index>=0&&(this._index=-1,this._keys=h,this._values=h),{value:I,done:!0}},V})(),x=(function(){function V(){this._keys=[],this._values=[],this._cacheKey=f,this._cacheIndex=-2}return Object.defineProperty(V.prototype,"size",{get:function(){return this._keys.length},enumerable:!0,configurable:!0}),V.prototype.has=function(I){return this._find(I,!1)>=0},V.prototype.get=function(I){var T=this._find(I,!1);return T>=0?this._values[T]:void 0},V.prototype.set=function(I,T){var P=this._find(I,!0);return this._values[P]=T,this},V.prototype.delete=function(I){var T=this._find(I,!1);if(T>=0){for(var P=this._keys.length,M=T+1;M<P;M++)this._keys[M-1]=this._keys[M],this._values[M-1]=this._values[M];return this._keys.length--,this._values.length--,Gn(I,this._cacheKey)&&(this._cacheKey=f,this._cacheIndex=-2),!0}return!1},V.prototype.clear=function(){this._keys.length=0,this._values.length=0,this._cacheKey=f,this._cacheIndex=-2},V.prototype.keys=function(){return new y(this._keys,this._values,L)},V.prototype.values=function(){return new y(this._keys,this._values,te)},V.prototype.entries=function(){return new y(this._keys,this._values,ie)},V.prototype["@@iterator"]=function(){return this.entries()},V.prototype[i]=function(){return this.entries()},V.prototype._find=function(I,T){if(!Gn(this._cacheKey,I)){this._cacheIndex=-1;for(var P=0;P<this._keys.length;P++)if(Gn(this._keys[P],I)){this._cacheIndex=P;break}}return this._cacheIndex<0&&T&&(this._cacheIndex=this._keys.length,this._keys.push(I),this._values.push(void 0)),this._cacheIndex},V})();return x;function L(V,I){return V}function te(V,I){return I}function ie(V,I){return[V,I]}}function Ki(){var f=(function(){function h(){this._map=new p}return Object.defineProperty(h.prototype,"size",{get:function(){return this._map.size},enumerable:!0,configurable:!0}),h.prototype.has=function(y){return this._map.has(y)},h.prototype.add=function(y){return this._map.set(y,y),this},h.prototype.delete=function(y){return this._map.delete(y)},h.prototype.clear=function(){this._map.clear()},h.prototype.keys=function(){return this._map.keys()},h.prototype.values=function(){return this._map.keys()},h.prototype.entries=function(){return this._map.entries()},h.prototype["@@iterator"]=function(){return this.keys()},h.prototype[i]=function(){return this.keys()},h})();return f}function zi(){var f=16,h=c.create(),y=x();return(function(){function I(){this._key=x()}return I.prototype.has=function(T){var P=L(T,!1);return P!==void 0?c.has(P,this._key):!1},I.prototype.get=function(T){var P=L(T,!1);return P!==void 0?c.get(P,this._key):void 0},I.prototype.set=function(T,P){var M=L(T,!0);return M[this._key]=P,this},I.prototype.delete=function(T){var P=L(T,!1);return P!==void 0?delete P[this._key]:!1},I.prototype.clear=function(){this._key=x()},I})();function x(){var I;do I="@@WeakMap@@"+V();while(c.has(h,I));return h[I]=!0,I}function L(I,T){if(!r.call(I,y)){if(!T)return;Object.defineProperty(I,y,{value:c.create()})}return I[y]}function te(I,T){for(var P=0;P<T;++P)I[P]=Math.random()*255|0;return I}function ie(I){if(typeof Uint8Array=="function"){var T=new Uint8Array(I);return typeof crypto<"u"?crypto.getRandomValues(T):typeof msCrypto<"u"?msCrypto.getRandomValues(T):te(T,I),T}return te(new Array(I),I)}function V(){var I=ie(f);I[6]=I[6]&79|64,I[8]=I[8]&191|128;for(var T="",P=0;P<f;++P){var M=I[P];(P===4||P===6||P===8)&&(T+="-"),M<16&&(T+="0"),T+=M.toString(16).toLowerCase()}return T}}function Wn(f){return f.__=void 0,delete f.__,f}})})(Zr||(Zr={}))});S();var Rm=Ge(eo(),1),xe=Ge(require("process"),1);S();var Aa=Ge(require("os"),1),Ia=Ge(require("path"),1),Ta=require("url"),Pa=require("util");S();S();var ae=class extends Error{},ia=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;function aa(t){if(typeof t!="string")return!1;let e=ia.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 ua(t){if(t===null||typeof t!="object")return null;let e=t.shared;if(e==null)return null;if(typeof e!="object")throw new ae(`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 ae(`catalog "shared" block has no shared_org_id: ${JSON.stringify(e)}`);if(!Array.isArray(r))throw new ae(`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 ae(`catalog shared endpoints[${u}] is not an object: ${JSON.stringify(a)}`);let d=a;for(let l of["model_id","variant","app_slug","function"])if(typeof d[l]!="string"||d[l].length===0)throw new ae(`catalog shared endpoints[${u}] (${JSON.stringify(d)}) has no valid "${l}"`);let c=(l,p)=>{if(d[l]!==void 0&&d[l]!==null&&!p)throw new ae(`catalog shared endpoints[${u}] (${d.model_id}:${d.variant}) carries a malformed "${l}": ${JSON.stringify(d[l])}`)};c("ready",typeof d.ready=="boolean"),c("ready_runtimes",typeof d.ready_runtimes=="number"&&Number.isInteger(d.ready_runtimes)&&d.ready_runtimes>=0),c("ready_since",aa(d.ready_since));for(let l of["model_id","variant"]){let p=d[l];if(p.includes(":"))throw new ae(`catalog shared endpoints[${u}] has "${l}" containing ":" (${JSON.stringify(p)}) \u2014 the external identity <model_id>:<variant> would be unparseable (resolveSharedModel splits at the LAST colon); platform catalog defect`)}return c("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 ae(`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 ae(`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 ae(`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 ft(t){return[...t.endpoints].sort((e,n)=>e.model_id.localeCompare(n.model_id)||e.variant.localeCompare(n.variant))}function to(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(c=>c.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 ae(`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 no(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 ae(`catalog read failed: GET ${n} \u2192 ${r.status}`);return ua(await r.json())}S();function Ue(t){return t.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}S();var _e=class extends Error{name="ModelPriceError"},Jn="per_million_input_tokens",Kn="per_million_output_tokens",oo="model_id,variant,unit,price_usd,effective_at,expires_at",ro={per_request:!0,per_minute:!0,per_output_unit:!0},da=5e3;function la(t,e){let n=typeof t=="number"?t:typeof t=="string"&&t.trim()!==""?Number(t):NaN;if(!Number.isFinite(n)||n<0)throw new _e(`${e}: price_usd is not a non-negative number (${JSON.stringify(t)})`);return n}function so(t,e,n){let r=typeof t=="string"?Date.parse(t):NaN;if(!Number.isFinite(r))throw new _e(`${e}: ${n} is not a parseable timestamp (${JSON.stringify(t)})`);return r}function ca(t,e,n){return t===null?null:so(t,e,n)}function pa(t,e){if(t===Jn)return"input";if(t===Kn)return"output";if(typeof t=="string"&&Object.hasOwn(ro,t))return null;throw new _e(`${e}: unit ${JSON.stringify(t)} is neither a per-token rate (${Jn} | ${Kn}) nor a known GPU-time unit (${Object.keys(ro).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 fa(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 zn(t,e){return`${t}\0${e}`}function ma(t){if(!Array.isArray(t))throw new _e(`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 _e(`${o} is not an object: ${JSON.stringify(r)}`);let s=r;for(let d of oo.split(","))if(!Object.hasOwn(s,d))throw new _e(`${o} has no "${d}" column: ${JSON.stringify(s)}`);let i=pa(s.unit,o);if(i===null)continue;let a=s.model_id;if(typeof a!="string"||a==="")throw new _e(`${o} has no model_id (${JSON.stringify(s)})`);let u=s.variant;if(!(u===null||typeof u=="string"&&u!==""))throw new _e(`${o} has a non-string variant (${JSON.stringify(u)})`);e.push({model_id:a,variant:u,side:i,usd:la(s.price_usd,o),effective_at:so(s.effective_at,o,"effective_at"),expires_at:ca(s.expires_at,o,"expires_at")})}return e}function io(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 c=u.variant??"",l=d.get(c);l||(l={input:[],output:[]},d.set(c,l),o.get(u.model_id).set(c,u.variant)),l[u.side].push({effective_at:u.effective_at,usd:u.usd})}let s=[],i=[];for(let[u,d]of r){let c=d.get(""),l=(p,m,b)=>{let E=p&&p.length>0?p:c?.[m];if(!E||E.length===0)return null;let w=fa(E,m);return"reason"in w?w:w.rate.usd===0?m==="output"&&b!==null&&n?.has(zn(u,b))?w.rate:{reason:`the ${m} 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`}:w.rate};for(let[p,m]of o.get(u)){let b=d.get(p),E=l(b?.input,"input",m),w=l(b?.output,"output",m);if(E!==null&&"reason"in E){i.push({model_id:u,variant:m,reason:E.reason});continue}if(w!==null&&"reason"in w){i.push({model_id:u,variant:m,reason:w.reason});continue}E===null||w===null||s.push({model_id:u,variant:m,pricing:{input:E.usd,output:w.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 mt(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 Bt={prices:[],refusals:[]};async function ao(t){let e=t.fetchImpl??fetch,n=`${Jn},${Kn}`,r=await e(`${t.catalogUrl}/model_prices?select=${oo}&unit=in.(${n})`,{headers:{apikey:t.anonKey,Authorization:`Bearer ${t.anonKey}`},signal:AbortSignal.timeout(da)});if(!r.ok)throw new Error(`model_prices fetch failed: ${r.status}`);return ma(await r.json())}S();var ha={chat:!0,code:!0,agent:!0,vl:!0},uo={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function ga(t){let e=t.toLowerCase();return Object.hasOwn(uo,e)?uo[e]:null}var lo={"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 ya(t,e){if(!t||t.length===0)return[];let n=[],r=[];for(let o of t){let s=Object.hasOwn(lo,o)?lo[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 co={qwen:"Qwen",qwen1:"Qwen",qwen2:"Qwen",qwen3:"Qwen",gemma:"Gemma",diffusiongemma:"Gemma",gpt:"GPT",glm:"GLM",deepseek:"DeepSeek",mistral:"Mistral"},po={2:"Llama2",3:"Llama3"};function wa(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(co,s))return co[s];let i=r[o+1];if(s==="llama"&&i!==void 0&&Object.hasOwn(po,i))return po[i];if(s==="phi"&&i!==void 0&&/^\d/.test(i))return"Phi"}return null}var _a=["context_length","max_model_len"];function va(t){let e=_a.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 qt(t){let e=t.map(va),n=e[0];return n==null?null:e.every(r=>r===n)?n:null}function ba(t,e){let n=e.filter(o=>Ue(`${o.model_id}-${o.variant}`)===t);if(n.length>0)return jt(n);let r=e.filter(o=>Ue(o.model_id)===t);return r.length>0?jt(r):null}function jt(t){let e=t[0];return t.every(r=>r.model_id===e.model_id&&r.variant===e.variant)?{row:t.length===1?e:ka(t),matches:t}:null}function ka(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 ho(t){let e=t.updated_at?Date.parse(t.updated_at):NaN;return Number.isFinite(e)?Math.floor(e/1e3):0}function Yn(t,e,n){let r=ba(t,e);if(!r)return{id:t,object:"model",created:0,owned_by:"urun"};let{row:o,matches:s}=r,i=qt(s),a=mt(o.model_id,o.variant,n),u=go(o,s);return{id:t,object:"model",created:ho(o),owned_by:"urun",name:`${o.model_id}:${o.variant}`,...o.description?{description:o.description}:{},...o.task?{task:o.task}:{},...o.engine?{engine:o.engine}:{},...o.gpu_spec?{gpu_spec:o.gpu_spec}:{},...i!==null?{context_length:i}:{},...a!==null?{pricing:a}:{},...u.published.length>0?{voices:[...u.published]}:{}}}var Sa=["Vivian","Serena","Uncle_Fu","Dylan","Eric","Ryan","Aiden","Ono_Anna","Sohee"];function go(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:[...Sa],defect:o}:{published:[],defect:o}}function Xn(t){let e=t.length>0?jt(t):null;return e===null?{published:[],defect:null}:go(e.row,e.matches)}function fo(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 mo(t){return{input:[{type:"prompt",unit:"token",cost_usd:fo(t.input)}],output:[{type:"completion",unit:"token",cost_usd:fo(t.output)}]}}function Ra(t,e,n,r){if(e.length===0)return null;let o=jt(e);if(!o)return null;let{row:s,matches:i}=o;if(!s.task||ha[s.task]!==!0)return null;let a=qt(i),u=mt(s.model_id,s.variant,n),d=wa(s.hf_repo),c={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};return a!==null&&(c.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:ho(s),quantization:ga(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:mo(u).input}:{}},...s.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:c,...u?{pricing:mo(u).output}:{}}],...r.datacenters.length>0?{datacenters:r.datacenters}:{},...r.compliance?{compliance:r.compliance}:{}}}function yo(t,e,n,r){let o=r??(d=>console.warn(d.trimEnd())),s=t?ft(t):[];if(s.length>0){let d=s.filter(c=>c.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 c=d.map(l=>`${l.model_id}:${l.variant}`).join(", ");o(`urun openrouter doc: readiness seam partially deployed \u2014 endpoints ${c} carry no ready field and are declared not ready`)}}let i={datacenters:ya(t?.regions,o),compliance:t?.compliance??null},a=[],u=new Set;for(let d of s){let c=`${d.model_id}:${d.variant}`;if(u.has(c))continue;u.add(c);let l=Ra(d,e.filter(p=>p.model_id===d.model_id&&p.variant===d.variant),n,i);l&&a.push(l)}return{data:a}}var z=class extends Error{},ce=class extends Error{},Qn="urs1.";function wo(t,e){return Qn+Buffer.from(JSON.stringify([t,e]),"utf8").toString("base64url")}function xa(t){if(!t.startsWith(Qn))return null;try{let e=JSON.parse(Buffer.from(t.slice(Qn.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 So(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 _o(t){if(t.length===0)return[];if(!t.every(n=>(n.task??void 0)==="image"))return[];let e=Oa(t);return e===null?[]:[e]}function vo(t,e,n,r,o){if(!o)throw new ae(`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 ae(`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 ae(`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 bo(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 ae(`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 Ro="shared:",Ve="speech:";function gt(t){return t.sharedApp?Ro+t.appSlug:t.appSlug}function yt(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 ht(t,e){let n=e.filter(s=>Ue(`${s.model_id}-${s.variant}`)===t);if(n.length>0)return n;let r=e.filter(s=>Ue(s.model_id)===t);return new Set(r.map(s=>s.variant)).size===1?r:[]}function Ea(t){let e=t.engine_args?.expects_image;return e==null?"edit":e===!1?"generation":e===!0?"edit":null}function Oa(t){let e=t.map(Ea),n=e[0];return n!=null&&e.every(r=>r===n)?n:null}var ko={stt:"stt",tts:"tts"};function Ht(t){if(t.length===0)return null;let e=t[0].task??void 0;return e===void 0||ko[e]===void 0?null:t.every(n=>(n.task??void 0)===e)?ko[e]:null}var Gt=6e4,Vt=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??Gt;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??Gt;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??Gt;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??Gt;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 _e)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
- `),Bt}let r=io(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(zn(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(Ro)||o.startsWith(Ve)||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 z(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=Ue(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 z(`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 z(`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=to(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===Ue(a.model_id)||o===Ue(`${a.model_id}-${a.variant}`));if(i)throw new z(`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=yt(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 gt(await this.resolveTarget(e))}async sessionFor(e){let n=await this.resolveTarget(e);return this.acquire(n,gt(n))}async speechSessionFor(e){let n=await this.resolveTarget(e);return this.acquire(n,Ve+gt(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:yt(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:wo(n,this.keyOf(r))}}async speechHandleFor(e){let{app:n,entry:r}=await this.speechSessionFor(e);return{app:n,handle:wo(n,this.keyOf(r))}}async sessionForHandle(e){let n=xa(e);if(!n)throw new ce(`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 ce(`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 ce(`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:Bt;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?ft(n):[]).map(d=>{let c={id:`${d.model_id}:${d.variant}`,object:"model",created:0,owned_by:"urun",name:`${d.model_id}:${d.variant}`};d.gpu_spec&&(c.gpu_spec=d.gpu_spec),typeof d.warm=="number"&&(c.warm=d.warm);let l=s.filter(E=>E.model_id===d.model_id&&E.variant===d.variant),p=qt(l);p!==null&&(c.context_length=p);let m=mt(d.model_id,d.variant,i);m!==null&&(c.pricing=m);let b=Xn(l);return b.published.length>0&&(c.voices=[...b.published]),c});return{object:"list",data:this.opts.sharedOnlyListing?u:[...e.map(d=>Yn(d,s,i)),...u]}}async openRouterModels(){let e=await this.sharedBlock(),[n,r]=await Promise.all([this.catalogRows(),this.pricesForListing(this.outputFreeKeys(e))]);return yo(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=vo(String(e),r.modelId,r.variant,o,this.opts.listCatalog!==null);return bo(`${r.modelId}:${r.variant}`,s).includes(n)}return _o(ht(r.appSlug,o)).includes(n)}async supportsAudio(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Ht(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))!==null:Ht(ht(n.appSlug,r))!==null}async supportsSpeech(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Ht(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))==="tts":Ht(ht(n.appSlug,r))==="tts"}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):ht(n.appSlug,r);return Xn(o)}async imageModelList(){let e=await this.catalogRows(),n=Bt,r=[];for(let s of await this.availableIds()){let i=_o(ht(s,e));i.length!==0&&r.push({...Yn(s,e,n),image_modes:i})}let o=await this.sharedBlock();if(o)for(let s of ft(o)){let i=vo(`${s.model_id}:${s.variant}`,s.model_id,s.variant,e,this.opts.listCatalog!==null),a=bo(`${s.model_id}:${s.variant}`,i);if(a.length===0)continue;let u={id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun",name:`${s.model_id}:${s.variant}`,image_modes:a};s.gpu_spec&&(u.gpu_spec=s.gpu_spec),typeof s.warm=="number"&&(u.warm=s.warm),r.push(u)}return{object:"list",data:r}}async closeAll(){let e=[...this.pool.values()],n=[];if(await Promise.all(e.map(async r=>{try{await this.opts.closeSession(await r)}catch(o){n.push(String(o instanceof Error?o.message:o))}})),n.length>0)throw new Error(`failed to close ${n.length} pooled session(s): ${n.join("; ")}`)}};function xo(t,e="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(t).pathname))throw new Error(`${e} must be the session-gateway base (e.g. https://api.urun.sh), got ${t} \u2014 a trailing /v1 is the org control-plane API form (that belongs in URUN_API_URL); session allocation 404s against it. Drop the /v1.`);return t}function Wt(t,e="URUN_GATEWAY_URL"){let n=new URL(t),r=n.hostname==="localhost"||n.hostname==="127.0.0.1"||n.hostname==="[::1]";if(n.protocol!=="https:"&&!r)throw new Error(`${e} must be https (got ${t}) \u2014 the org API key rides the request's Authorization header`);return t}var Ca=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function Eo(t){let e=t?.code;return typeof e=="string"&&Ca.has(e)}function Ie(t,e){try{return t.write(e),!0}catch(n){if(Eo(n))return!1;throw n}}function Oo(t){let e={stdout:!1,stderr:!1},n=[];for(let r of["stdout","stderr"]){let o=t.streams[r],s=i=>{Eo(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
6
- `)}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)}}}S();var fe=require("@prometheus-io/client"),Xo=require("http");S();var $a={"/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"};function Ma(t){return $a[t]??null}var To=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function Ua(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function La(t){return t==="surface"}function Na(t){let e=t?.ledgerFailureReason;return To.includes(e)?e:"unknown"}var Da=["no_response_head","no_org","model_unresolved","no_model_named","queue_full"],Ao=["priced","unpriced"],Fa={written:Ao,already_written:Ao,skipped:Da,failed:To},Po=Object.entries(Fa).flatMap(([t,e])=>e.map(n=>({outcome:t,reason:n})));function Ba(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function ja(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 qa=256,Io=4096,wt=12e4,Ha=250,Ga=8e3,Le=[],Zn=new Set,_t=0,nt=!1;function Co(){return Le.length}function $o(t=Date.now()){let e=Le[0];return e===void 0?0:Math.max(0,t-e.queuedAt)}var Jt=new Set;function Mo(){return _t===0&&Le.length===0}function Uo(){if(Mo()){nt=!1;for(let t of[...Jt])t()}}async function Lo(t=Do){return Mo()?!0:await new Promise(e=>{let n=null,r=()=>{Jt.delete(r),n&&clearTimeout(n),e(!0)};Jt.add(r),n=setTimeout(()=>{Jt.delete(r),e(!1)},t),n.unref?.()})}function No(){nt=!0;let t=[...Le.splice(0,Le.length),...Zn];for(let e of t)e.settled||We(e,{outcome:"failed",reason:"dropped_on_exit"},{event:"inference_ledger_write_failed",reason:"dropped_on_exit",attempts:e.attempts,error:`the proxy is shutting down and this row was still unplaced after the ${Do}ms drain \u2014 the request was served and answered, and its billing row was never written`});return Uo(),t.length}var Do=5e3;function Fo(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
7
- `}function Va(t){Ie(process.stdout,t)}function Bo(t,e){try{t(e)}catch{}}function jo(t){let{record:e,ledger:n}=t;if(n===null)return;let r=n.report??Va,o=Ma(e.path);if(o===null)return;let s=c=>{try{n.observe(c)}catch{}},i=(c,l)=>{s({outcome:"skipped",reason:c}),Bo(r,Fo("inference_ledger_skipped",{inference_id:e.inference_id,path:e.path,reason:c,detail:l}))};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:ja(e),completed_at:e.ts,billable_outcome:Ba(e),session_id:e.session_id,prompt_tokens:e.prompt_tokens,completion_tokens:e.completion_tokens,output_units:e.output_units,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(Le.length>=Io){i("queue_full",`${Le.length} ledger writes are already waiting (capacity ${Io}, ${_t} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}Le.push({entry:d,req:t.req,record:e,ledger:n,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),_t<qa&&(_t+=1,Wa())}async function Wa(){try{for(;;){let t=Ja();if(t===void 0)return;Zn.add(t);try{await Ka(t)}finally{Zn.delete(t)}}}finally{_t-=1,Uo()}}function Ja(){let t=Date.now();for(;;){let e=Le.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<wt)return e;We(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 ${wt}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 Ka(t){let{entry:e,record:n,ledger:r}=t;for(;;){t.attempts+=1;try{let o=await r.write(t.req,e);We(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=Ua(o);if(s!==null){We(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=Na(o),a=Date.now()-t.queuedAt;if(nt){We(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(La(i)&&a<wt){if(await Ya(za(t.attempts)),nt||Date.now()-t.queuedAt>=wt){let u=Date.now()-t.queuedAt;We(t,{outcome:"failed",reason:nt?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:nt?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:u,error:`this row's backoff outlived the ${wt}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}We(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 za(t){return Math.min(Ga,Ha*2**Math.max(0,t-1))}function Ya(t){return new Promise(e=>{setTimeout(e,t).unref?.()})}function We(t,e,n){if(t.settled)return;t.settled=!0;try{t.ledger.observe(e)}catch{}let{event:r,...o}=n;Bo(t.report,Fo(r,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...o}))}S();S();var zt="/v1/usage/requests",qo=1e4,Xa=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,je=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},ee=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}},Kt=class extends ee{constructor(e){super(e),this.name="UsageWithholdingError"}},Wo=1024*1024;function nr(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new je(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new je(`"${e}" must be an array of uuid strings`);if(n.length>qo)throw new je(`"${e}" carries ${n.length} ids; at most ${qo} 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"||!Xa.test(s))throw new je(`"${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 Jo(t){return nr(t,"inference_ids")}var Qa=["inference_id","model_id","identity_kind","variant","task","http_status","started_at","completed_at","prompt_tokens","completion_tokens","output_units","cost_nano_usd","price_version","priced_at"];function rt(t,e,n){let r=t[e];if(typeof r!="string"||r==="")throw new ee(`${n}: ${e} is not a non-empty string (got ${ot(r)})`);return r}function er(t,e,n){let r=t[e];if(r===null)return null;if(typeof r!="string"||r==="")throw new ee(`${n}: ${e} is neither null nor a non-empty string (got ${ot(r)})`);return r}function vt(t,e,n,r){let o=t[e];if(o===null)return null;let s=typeof o=="number"?o:typeof o=="string"&&/^-?\d+(\.\d+)?$/.test(o.trim())?Number(o.trim()):NaN;if(!Number.isFinite(s))throw new ee(`${n}: ${e} is not a number (got ${ot(o)})`);if(r&&!Number.isSafeInteger(s))throw new ee(`${n}: ${e} is not a safely representable integer \u2014 a billing quantity that silently loses digits must fail rather than be reported`);if(s<0)throw new ee(`${n}: ${e} is negative`);return Object.is(s,-0)?0:s}var Za=40,Ho=64,Go=600;function Ko(t){let e=Object.keys(t).sort(),r=e.slice(0,Za).map(o=>o.length>Ho?`${o.slice(0,Ho)}\u2026`:o).join(", ");return r.length>Go&&(r=`${r.slice(0,Go)}\u2026`),`${e.length} keys present: [${r}]`}function ot(t){return t===null?"null":t===void 0?"undefined":Array.isArray(t)?"an array":typeof t=="object"?"an object":`a ${typeof t}`}function tr(t){Ie(process.stdout,`${JSON.stringify({event:"usage_row_rejected",ts:new Date().toISOString(),where:t.where,reason:t.reason})}
8
- `)}function eu(t,e){let n;try{n=t(e)}catch{Vo(t,e);return}tu(n)&&Promise.resolve(n).catch(()=>{Vo(t,e)})}function tu(t){return typeof t?.then=="function"}function Vo(t,e){if(t!==tr)try{tr(e)}catch{}}function zo(t,e=tr){let n=t?.requests;if(!Array.isArray(n)){let o=t!==null&&typeof t=="object"&&!Array.isArray(t)?Ko(t):ot(t);throw new ee(`the usage surface answered without a "requests" array: got ${ot(n)} (${o})`)}let r=[];for(let o=0;o<n.length;o++){let s=`usage.requests[${o}]`;try{r.push(nu(n[o],s))}catch(i){if(i instanceof Kt||!(i instanceof ee))throw i;eu(e,{where:s,reason:i.message})}}if(n.length>0&&r.length===0)throw new ee(`the usage surface returned ${n.length} row(s) and NONE of them could be validated \u2014 answering an empty result here would be indistinguishable from "we hold nothing for these ids", which a billing caller treats as final and writes the whole batch off unbilled`);return r}function nu(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new ee(`${e} is not an object: got ${ot(t)}`);let n=t;if(Object.hasOwn(n,"gpu_seconds"))throw new Kt(`${e} carries gpu_seconds \u2014 that column was DROPPED from the ledger (ENG-417): the cost of compute is metered once, on the instance, and a per-request figure counts the same GPU-second twice. Its reappearance means a second, competing measure has been reintroduced upstream`);for(let o of Qa)if(!Object.hasOwn(n,o))throw new ee(`${e} has no "${o}" column (${Ko(n)})`);let r=vt(n,"http_status",e,!0);if(r===null)throw new ee(`${e}: http_status is null, but the ledger column is NOT NULL`);return{inference_id:rt(n,"inference_id",e),model_id:rt(n,"model_id",e),identity_kind:rt(n,"identity_kind",e),variant:er(n,"variant",e),task:rt(n,"task",e),http_status:r,started_at:rt(n,"started_at",e),completed_at:rt(n,"completed_at",e),prompt_tokens:vt(n,"prompt_tokens",e,!0),completion_tokens:vt(n,"completion_tokens",e,!0),output_units:vt(n,"output_units",e,!1),cost_nano_usd:vt(n,"cost_nano_usd",e,!0),price_version:er(n,"price_version",e),priced_at:er(n,"priced_at",e)}}function Yt(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 ee(`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 ee(`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 bt="/partners/huggingface/v1/billing/requests";function ru(t){return nr(t,"requestIds")}function ou(t){return t>=200&&t<=399}function su(t){let e=[];for(let n of t)n.cost_nano_usd!==null&&ou(n.http_status)&&e.push({requestId:n.inference_id,costNanoUsd:n.cost_nano_usd});return{requests:e.length>0?e:null}}async function Yo(t,e){let n=ru(t),r=await e(n);return Yt(r,n),su(r)}var rr=9464,Xt="/metrics",Qt="(other)",iu="(none)",au=64,uu=128,du=["/v1/responses","/v1/chat/completions","/v1/systemone","/v1/messages","/v1/messages/count_tokens","/v1/models","/v1/images/models","/v1/images/generations","/v1/images/edits","/v1/audio/speech",zt,bt],lu=new Set(du);function cu(t){return lu.has(t)?t:Qt}function pu(t){return t===null?"none":t>=500?"5xx":t>=400?"4xx":t>=300?"3xx":t>=200?"2xx":"1xx"}var Zt=class{constructor(e){this.max=e}max;admitted=new Set;overflows=0;of(e,n=!0){return e===null||e===""?iu:this.admitted.has(e)?e:n?this.admitted.size>=this.max?(this.overflows+=1,Qt):(this.admitted.add(e),e):Qt}get size(){return this.admitted.size}get overflowCount(){return this.overflows}},en=class{registry=new fe.Registry;modelLabel=new Zt(au);orgLabel=new Zt(uu);requests;duration;ttft;streamErrors;undelivered;ledgerWrites;ledgerQueue;labelCardinality;labelOverflows;constructor(e={}){this.requests=new fe.Counter({name:"urun_inference_requests_total",help:"Completed /v1 requests, by lane, method, HTTP status class, streaming, model and org.",labelNames:["lane","method","status_class","stream","model","org"],registers:[this.registry]}),this.duration=new fe.Histogram({name:"urun_inference_request_duration_seconds",help:"Wall time from request receipt to response close. For a stream this is the WHOLE generation, not the time to the head.",labelNames:["lane","stream","model","org"],buckets:[.1,.25,.5,1,2.5,5,10,30,60,120,300,600],registers:[this.registry]}),this.ttft=new fe.Histogram({name:"urun_inference_ttft_seconds",help:"Time to first output-text delta on a streamed response. The metric Hugging Face gates provider listing on (<5s for conversational models).",labelNames:["lane","model","org"],buckets:[.1,.25,.5,1,2,3,5,8,15,30,60],registers:[this.registry]}),this.streamErrors=new fe.Counter({name:"urun_inference_stream_errors_total",help:"Responses that emitted an error frame AFTER the head flushed. These are HTTP 200 with a complete body, so no status-based metric can see them.",labelNames:["lane","model"],registers:[this.registry]}),this.undelivered=new fe.Counter({name:"urun_inference_undelivered_total",help:"Responses whose socket did not finish flushing \u2014 the client vanished mid-answer.",labelNames:["lane","model"],registers:[this.registry]}),this.ledgerWrites=new fe.Counter({name:"urun_inference_ledger_writes_total",help:"Terminal fate of the per-request ledger row for each billable /v1 request. outcome=written is the row landing (reason=priced|unpriced); outcome=skipped and outcome=failed are BOTH revenue that went unrecorded. failed/duplicate is the ENG-410 fabricated-row signal.",labelNames:["outcome","reason"],registers:[this.registry]});for(let n of Po)this.ledgerWrites.inc({outcome:n.outcome,reason:n.reason},0);this.ledgerQueue=new fe.Gauge({name:"urun_inference_ledger_queue",help:'The ledger write queue right now. measure="depth" is rows waiting for a worker; measure="oldest_age_seconds" is how long the head has waited \u2014 approaching the dwell bound means rows are about to be given up on, which is revenue lost.',labelNames:["measure"],registers:[this.registry],collect:()=>{this.ledgerQueue.set({measure:"depth"},Co()),this.ledgerQueue.set({measure:"oldest_age_seconds"},$o()/1e3)}}),this.labelCardinality=new fe.Gauge({name:"urun_inference_label_cardinality",help:"Distinct values admitted for a bounded metric label. At its ceiling, further values report as (other).",labelNames:["label"],registers:[this.registry],collect:()=>{this.labelCardinality.set({label:"model"},this.modelLabel.size),this.labelCardinality.set({label:"org"},this.orgLabel.size)}}),this.labelOverflows=new fe.Counter({name:"urun_inference_label_overflows_total",help:"Observations that would have taken a new label value but found the budget spent. Non-zero means a metric dimension is now partly blind.",labelNames:["label"],registers:[this.registry]}),e.defaultMetrics!==!1&&(0,fe.collectDefaultMetrics)({register:this.registry,prefix:"urun_inference_proxy_"})}observeLedger(e){this.ledgerWrites.inc({outcome:e.outcome,reason:e.reason})}observe(e){let n=cu(e.path),r=e.status!==null&&!(e.status>=400&&e.status<500),o=this.modelLabel.overflowCount+this.orgLabel.overflowCount,s=this.modelLabel.of(e.model,r),i=this.orgLabel.of(e.org,r);this.modelLabel.overflowCount+this.orgLabel.overflowCount-o>0&&(this.modelLabel.overflowCount>o?this.labelOverflows.inc({label:"model"}):this.labelOverflows.inc({label:"org"}));let u=e.stream===!0?"true":"false";this.requests.inc({lane:n,method:e.method===""?Qt:e.method,status_class:pu(e.status),stream:u,model:s,org:i}),this.duration.observe({lane:n,stream:u,model:s,org:i},e.duration_ms/1e3),e.ttft_ms!==null&&this.ttft.observe({lane:n,model:s,org:i},e.ttft_ms/1e3),e.stream_error&&this.streamErrors.inc({lane:n,model:s}),e.delivered||this.undelivered.inc({lane:n,model:s})}scrape(){return this.registry.metrics()}get contentType(){return this.registry.contentType}};function Qo(t){return(0,Xo.createServer)((e,n)=>{(async()=>{let r=(e.url??"/").split("?")[0];if(e.method!=="GET"||r!==Xt){n.writeHead(404,{"content-type":"text/plain"}),n.end(`no route for ${e.method} ${r} \u2014 this port serves only GET ${Xt}
2
+ "use strict";var da=Object.create;var io=Object.defineProperty;var la=Object.getOwnPropertyDescriptor;var ca=Object.getOwnPropertyNames;var pa=Object.getPrototypeOf,fa=Object.prototype.hasOwnProperty;var ma=(t,e,n)=>()=>{if(n)throw n[0];try{return t&&(e=t(t=0)),e}catch(r){throw n=[r],r}};var ga=(t,e)=>()=>{try{return e||t((e={exports:{}}).exports,e),e.exports}catch(n){throw e=0,n}};var ha=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of ca(e))!fa.call(t,o)&&o!==n&&io(t,o,{get:()=>e[o],enumerable:!(r=la(e,o))||r.enumerable});return t};var ze=(t,e,n)=>(n=t!=null?da(pa(t)):{},ha(e||!t||!t.__esModule?io(n,"default",{value:t,enumerable:!0}):n,t));var k=ma(()=>{"use strict"});var uo=ga(()=>{"use strict";k();var ao;(function(t){(function(e){var n=typeof globalThis=="object"?globalThis:typeof global=="object"?global:typeof self=="object"?self:typeof this=="object"?this:a(),r=o(t);typeof n.Reflect<"u"&&(r=o(n.Reflect,r)),e(r,n),typeof n.Reflect>"u"&&(n.Reflect=t);function o(u,d){return function(l,p){Object.defineProperty(u,l,{configurable:!0,writable:!0,value:p}),d&&d(l,p)}}function s(){try{return Function("return this;")()}catch{}}function i(){try{return(0,eval)("(function() { return this; })()")}catch{}}function a(){return s()||i()}})(function(e,n){var r=Object.prototype.hasOwnProperty,o=typeof Symbol=="function",s=o&&typeof Symbol.toPrimitive<"u"?Symbol.toPrimitive:"@@toPrimitive",i=o&&typeof Symbol.iterator<"u"?Symbol.iterator:"@@iterator",a=typeof Object.create=="function",u={__proto__:[]}instanceof Array,d=!a&&!u,l={create:a?function(){return Zn(Object.create(null))}:u?function(){return Zn({__proto__:null})}:function(){return Zn({})},has:d?function(f,g){return r.call(f,g)}:function(f,g){return g in f},get:d?function(f,g){return r.call(f,g)?f[g]:void 0}:function(f,g){return f[g]}},p=Object.getPrototypeOf(Function),y=typeof Map=="function"&&typeof Map.prototype.entries=="function"?Map:oa(),c=typeof Set=="function"&&typeof Set.prototype.entries=="function"?Set:sa(),m=typeof WeakMap=="function"?WeakMap:ia(),w=o?Symbol.for("@reflect-metadata:registry"):void 0,R=ta(),h=na(R);function v(f,g,_,O){if(I(_)){if(!vt(f))throw new TypeError;if(!Zr(g))throw new TypeError;return de(f,g)}else{if(!vt(f))throw new TypeError;if(!F(g))throw new TypeError;if(!F(O)&&!I(O)&&!Y(O))throw new TypeError;return Y(O)&&(O=void 0),_=_e(_),fe(f,g,_,O)}}e("decorate",v);function b(f,g){function _(O,U){if(!F(O))throw new TypeError;if(!I(U)&&!Zi(U))throw new TypeError;Se(f,g,O,U)}return _}e("metadata",b);function $(f,g,_,O){if(!F(_))throw new TypeError;return I(O)||(O=_e(O)),Se(f,g,_,O)}e("defineMetadata",$);function S(f,g,_){if(!F(g))throw new TypeError;return I(_)||(_=_e(_)),se(f,g,_)}e("hasMetadata",S);function E(f,g,_){if(!F(g))throw new TypeError;return I(_)||(_=_e(_)),ie(f,g,_)}e("hasOwnMetadata",E);function x(f,g,_){if(!F(g))throw new TypeError;return I(_)||(_=_e(_)),ke(f,g,_)}e("getMetadata",x);function L(f,g,_){if(!F(g))throw new TypeError;return I(_)||(_=_e(_)),Z(f,g,_)}e("getOwnMetadata",L);function B(f,g){if(!F(f))throw new TypeError;return I(g)||(g=_e(g)),Ae(f,g)}e("getMetadataKeys",B);function q(f,g){if(!F(f))throw new TypeError;return I(g)||(g=_e(g)),Ke(f,g)}e("getOwnMetadataKeys",q);function V(f,g,_){if(!F(g))throw new TypeError;if(I(_)||(_=_e(_)),!F(g))throw new TypeError;I(_)||(_=_e(_));var O=bt(g,_,!1);return I(O)?!1:O.OrdinaryDeleteMetadata(f,g,_)}e("deleteMetadata",V);function de(f,g){for(var _=f.length-1;_>=0;--_){var O=f[_],U=O(g);if(!I(U)&&!Y(U)){if(!Zr(U))throw new TypeError;g=U}}return g}function fe(f,g,_,O){for(var U=f.length-1;U>=0;--U){var ne=f[U],le=ne(g,_,O);if(!I(le)&&!Y(le)){if(!F(le))throw new TypeError;O=le}}return O}function se(f,g,_){var O=ie(f,g,_);if(O)return!0;var U=Qn(g);return Y(U)?!1:se(f,U,_)}function ie(f,g,_){var O=bt(g,_,!1);return I(O)?!1:De(O.OrdinaryHasOwnMetadata(f,g,_))}function ke(f,g,_){var O=ie(f,g,_);if(O)return Z(f,g,_);var U=Qn(g);if(!Y(U))return ke(f,U,_)}function Z(f,g,_){var O=bt(g,_,!1);if(!I(O))return O.OrdinaryGetOwnMetadata(f,g,_)}function Se(f,g,_,O){var U=bt(_,O,!0);U.OrdinaryDefineOwnMetadata(f,g,_,O)}function Ae(f,g){var _=Ke(f,g),O=Qn(f);if(O===null)return _;var U=Ae(O,g);if(U.length<=0)return _;if(_.length<=0)return U;for(var ne=new c,le=[],W=0,T=_;W<T.length;W++){var P=T[W],C=ne.has(P);C||(ne.add(P),le.push(P))}for(var M=0,J=U;M<J.length;M++){var P=J[M],C=ne.has(P);C||(ne.add(P),le.push(P))}return le}function Ke(f,g){var _=bt(f,g,!1);return _?_.OrdinaryOwnMetadataKeys(f,g):[]}function H(f){if(f===null)return 1;switch(typeof f){case"undefined":return 0;case"boolean":return 2;case"string":return 3;case"symbol":return 4;case"number":return 5;case"object":return f===null?1:6;default:return 6}}function I(f){return f===void 0}function Y(f){return f===null}function Ie(f){return typeof f=="symbol"}function F(f){return typeof f=="object"?f!==null:typeof f=="function"}function me(f,g){switch(H(f)){case 0:return f;case 1:return f;case 2:return f;case 3:return f;case 4:return f;case 5:return f}var _=g===3?"string":g===5?"number":"default",O=eo(f,s);if(O!==void 0){var U=O.call(f,_);if(F(U))throw new TypeError;return U}return Ce(f,_==="default"?"number":_)}function Ce(f,g){if(g==="string"){var _=f.toString;if(Fe(_)){var O=_.call(f);if(!F(O))return O}var U=f.valueOf;if(Fe(U)){var O=U.call(f);if(!F(O))return O}}else{var U=f.valueOf;if(Fe(U)){var O=U.call(f);if(!F(O))return O}var ne=f.toString;if(Fe(ne)){var O=ne.call(f);if(!F(O))return O}}throw new TypeError}function De(f){return!!f}function at(f){return""+f}function _e(f){var g=me(f,3);return Ie(g)?g:at(g)}function vt(f){return Array.isArray?Array.isArray(f):f instanceof Object?f instanceof Array:Object.prototype.toString.call(f)==="[object Array]"}function Fe(f){return typeof f=="function"}function Zr(f){return typeof f=="function"}function Zi(f){switch(H(f)){case 3:return!0;case 4:return!0;default:return!1}}function Xn(f,g){return f===g||f!==f&&g!==g}function eo(f,g){var _=f[g];if(_!=null){if(!Fe(_))throw new TypeError;return _}}function to(f){var g=eo(f,i);if(!Fe(g))throw new TypeError;var _=g.call(f);if(!F(_))throw new TypeError;return _}function no(f){return f.value}function ro(f){var g=f.next();return g.done?!1:g}function oo(f){var g=f.return;g&&g.call(f)}function Qn(f){var g=Object.getPrototypeOf(f);if(typeof f!="function"||f===p||g!==p)return g;var _=f.prototype,O=_&&Object.getPrototypeOf(_);if(O==null||O===Object.prototype)return g;var U=O.constructor;return typeof U!="function"||U===f?g:U}function ea(){var f;!I(w)&&typeof n.Reflect<"u"&&!(w in n.Reflect)&&typeof n.Reflect.defineMetadata=="function"&&(f=ra(n.Reflect));var g,_,O,U=new m,ne={registerProvider:le,getProvider:T,setProvider:C};return ne;function le(M){if(!Object.isExtensible(ne))throw new Error("Cannot add provider to a frozen registry.");switch(!0){case f===M:break;case I(g):g=M;break;case g===M:break;case I(_):_=M;break;case _===M:break;default:O===void 0&&(O=new c),O.add(M);break}}function W(M,J){if(!I(g)){if(g.isProviderFor(M,J))return g;if(!I(_)){if(_.isProviderFor(M,J))return g;if(!I(O))for(var X=to(O);;){var re=ro(X);if(!re)return;var Te=no(re);if(Te.isProviderFor(M,J))return oo(X),Te}}}if(!I(f)&&f.isProviderFor(M,J))return f}function T(M,J){var X=U.get(M),re;return I(X)||(re=X.get(J)),I(re)&&(re=W(M,J),I(re)||(I(X)&&(X=new y,U.set(M,X)),X.set(J,re))),re}function P(M){if(I(M))throw new TypeError;return g===M||_===M||!I(O)&&O.has(M)}function C(M,J,X){if(!P(X))throw new Error("Metadata provider not registered.");var re=T(M,J);if(re!==X){if(!I(re))return!1;var Te=U.get(M);I(Te)&&(Te=new y,U.set(M,Te)),Te.set(J,X)}return!0}}function ta(){var f;return!I(w)&&F(n.Reflect)&&Object.isExtensible(n.Reflect)&&(f=n.Reflect[w]),I(f)&&(f=ea()),!I(w)&&F(n.Reflect)&&Object.isExtensible(n.Reflect)&&Object.defineProperty(n.Reflect,w,{enumerable:!1,configurable:!1,writable:!1,value:f}),f}function na(f){var g=new m,_={isProviderFor:function(P,C){var M=g.get(P);return I(M)?!1:M.has(C)},OrdinaryDefineOwnMetadata:le,OrdinaryHasOwnMetadata:U,OrdinaryGetOwnMetadata:ne,OrdinaryOwnMetadataKeys:W,OrdinaryDeleteMetadata:T};return R.registerProvider(_),_;function O(P,C,M){var J=g.get(P),X=!1;if(I(J)){if(!M)return;J=new y,g.set(P,J),X=!0}var re=J.get(C);if(I(re)){if(!M)return;if(re=new y,J.set(C,re),!f.setProvider(P,C,_))throw J.delete(C),X&&g.delete(P),new Error("Wrong provider for target.")}return re}function U(P,C,M){var J=O(C,M,!1);return I(J)?!1:De(J.has(P))}function ne(P,C,M){var J=O(C,M,!1);if(!I(J))return J.get(P)}function le(P,C,M,J){var X=O(M,J,!0);X.set(P,C)}function W(P,C){var M=[],J=O(P,C,!1);if(I(J))return M;for(var X=J.keys(),re=to(X),Te=0;;){var so=ro(re);if(!so)return M.length=Te,M;var aa=no(so);try{M[Te]=aa}catch(ua){try{oo(re)}finally{throw ua}}Te++}}function T(P,C,M){var J=O(C,M,!1);if(I(J)||!J.delete(P))return!1;if(J.size===0){var X=g.get(C);I(X)||(X.delete(M),X.size===0&&g.delete(X))}return!0}}function ra(f){var g=f.defineMetadata,_=f.hasOwnMetadata,O=f.getOwnMetadata,U=f.getOwnMetadataKeys,ne=f.deleteMetadata,le=new m,W={isProviderFor:function(T,P){var C=le.get(T);return!I(C)&&C.has(P)?!0:U(T,P).length?(I(C)&&(C=new c,le.set(T,C)),C.add(P),!0):!1},OrdinaryDefineOwnMetadata:g,OrdinaryHasOwnMetadata:_,OrdinaryGetOwnMetadata:O,OrdinaryOwnMetadataKeys:U,OrdinaryDeleteMetadata:ne};return W}function bt(f,g,_){var O=R.getProvider(f,g);if(!I(O))return O;if(_){if(R.setProvider(f,g,h))return h;throw new Error("Illegal state.")}}function oa(){var f={},g=[],_=(function(){function W(T,P,C){this._index=0,this._keys=T,this._values=P,this._selector=C}return W.prototype["@@iterator"]=function(){return this},W.prototype[i]=function(){return this},W.prototype.next=function(){var T=this._index;if(T>=0&&T<this._keys.length){var P=this._selector(this._keys[T],this._values[T]);return T+1>=this._keys.length?(this._index=-1,this._keys=g,this._values=g):this._index++,{value:P,done:!1}}return{value:void 0,done:!0}},W.prototype.throw=function(T){throw this._index>=0&&(this._index=-1,this._keys=g,this._values=g),T},W.prototype.return=function(T){return this._index>=0&&(this._index=-1,this._keys=g,this._values=g),{value:T,done:!0}},W})(),O=(function(){function W(){this._keys=[],this._values=[],this._cacheKey=f,this._cacheIndex=-2}return Object.defineProperty(W.prototype,"size",{get:function(){return this._keys.length},enumerable:!0,configurable:!0}),W.prototype.has=function(T){return this._find(T,!1)>=0},W.prototype.get=function(T){var P=this._find(T,!1);return P>=0?this._values[P]:void 0},W.prototype.set=function(T,P){var C=this._find(T,!0);return this._values[C]=P,this},W.prototype.delete=function(T){var P=this._find(T,!1);if(P>=0){for(var C=this._keys.length,M=P+1;M<C;M++)this._keys[M-1]=this._keys[M],this._values[M-1]=this._values[M];return this._keys.length--,this._values.length--,Xn(T,this._cacheKey)&&(this._cacheKey=f,this._cacheIndex=-2),!0}return!1},W.prototype.clear=function(){this._keys.length=0,this._values.length=0,this._cacheKey=f,this._cacheIndex=-2},W.prototype.keys=function(){return new _(this._keys,this._values,U)},W.prototype.values=function(){return new _(this._keys,this._values,ne)},W.prototype.entries=function(){return new _(this._keys,this._values,le)},W.prototype["@@iterator"]=function(){return this.entries()},W.prototype[i]=function(){return this.entries()},W.prototype._find=function(T,P){if(!Xn(this._cacheKey,T)){this._cacheIndex=-1;for(var C=0;C<this._keys.length;C++)if(Xn(this._keys[C],T)){this._cacheIndex=C;break}}return this._cacheIndex<0&&P&&(this._cacheIndex=this._keys.length,this._keys.push(T),this._values.push(void 0)),this._cacheIndex},W})();return O;function U(W,T){return W}function ne(W,T){return T}function le(W,T){return[W,T]}}function sa(){var f=(function(){function g(){this._map=new y}return Object.defineProperty(g.prototype,"size",{get:function(){return this._map.size},enumerable:!0,configurable:!0}),g.prototype.has=function(_){return this._map.has(_)},g.prototype.add=function(_){return this._map.set(_,_),this},g.prototype.delete=function(_){return this._map.delete(_)},g.prototype.clear=function(){this._map.clear()},g.prototype.keys=function(){return this._map.keys()},g.prototype.values=function(){return this._map.keys()},g.prototype.entries=function(){return this._map.entries()},g.prototype["@@iterator"]=function(){return this.keys()},g.prototype[i]=function(){return this.keys()},g})();return f}function ia(){var f=16,g=l.create(),_=O();return(function(){function T(){this._key=O()}return T.prototype.has=function(P){var C=U(P,!1);return C!==void 0?l.has(C,this._key):!1},T.prototype.get=function(P){var C=U(P,!1);return C!==void 0?l.get(C,this._key):void 0},T.prototype.set=function(P,C){var M=U(P,!0);return M[this._key]=C,this},T.prototype.delete=function(P){var C=U(P,!1);return C!==void 0?delete C[this._key]:!1},T.prototype.clear=function(){this._key=O()},T})();function O(){var T;do T="@@WeakMap@@"+W();while(l.has(g,T));return g[T]=!0,T}function U(T,P){if(!r.call(T,_)){if(!P)return;Object.defineProperty(T,_,{value:l.create()})}return T[_]}function ne(T,P){for(var C=0;C<P;++C)T[C]=Math.random()*255|0;return T}function le(T){if(typeof Uint8Array=="function"){var P=new Uint8Array(T);return typeof crypto<"u"?crypto.getRandomValues(P):typeof msCrypto<"u"?msCrypto.getRandomValues(P):ne(P,T),P}return ne(new Array(T),T)}function W(){var T=le(f);T[6]=T[6]&79|64,T[8]=T[8]&191|128;for(var P="",C=0;C<f;++C){var M=T[C];(C===4||C===6||C===8)&&(P+="-"),M<16&&(P+="0"),P+=M.toString(16).toLowerCase()}return P}}function Zn(f){return f.__=void 0,delete f.__,f}})})(ao||(ao={}))});k();var rg=ze(uo(),1),Pe=ze(require("process"),1);k();var Ba=ze(require("os"),1),ja=ze(require("path"),1),qa=require("url"),Ha=require("util");k();k();var ce=class extends Error{},ya=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;function wa(t){if(typeof t!="string")return!1;let e=ya.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 _a(t){if(t===null||typeof t!="object")return null;let e=t.shared;if(e==null)return null;if(typeof e!="object")throw new ce(`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 ce(`catalog "shared" block has no shared_org_id: ${JSON.stringify(e)}`);if(!Array.isArray(r))throw new ce(`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 ce(`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 ce(`catalog shared endpoints[${u}] (${JSON.stringify(d)}) has no valid "${p}"`);let l=(p,y)=>{if(d[p]!==void 0&&d[p]!==null&&!y)throw new ce(`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",wa(d.ready_since));for(let p of["model_id","variant"]){let y=d[p];if(y.includes(":"))throw new ce(`catalog shared endpoints[${u}] has "${p}" containing ":" (${JSON.stringify(y)}) \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 ce(`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 ce(`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 ce(`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 ut(t){return[...t.endpoints].sort((e,n)=>e.model_id.localeCompare(n.model_id)||e.variant.localeCompare(n.variant))}function lo(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 ce(`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 co(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 ce(`catalog read failed: GET ${n} \u2192 ${r.status}`);return _a(await r.json())}k();function Be(t){return t.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}k();var Re=class extends Error{name="ModelPriceError"},er="per_million_input_tokens",tr="per_million_output_tokens",fo="model_id,variant,unit,price_usd,effective_at,expires_at",po={per_request:!0,per_minute:!0,per_output_unit:!0},va=5e3;function ba(t,e){let n=typeof t=="number"?t:typeof t=="string"&&t.trim()!==""?Number(t):NaN;if(!Number.isFinite(n)||n<0)throw new Re(`${e}: price_usd is not a non-negative number (${JSON.stringify(t)})`);return n}function mo(t,e,n){let r=typeof t=="string"?Date.parse(t):NaN;if(!Number.isFinite(r))throw new Re(`${e}: ${n} is not a parseable timestamp (${JSON.stringify(t)})`);return r}function ka(t,e,n){return t===null?null:mo(t,e,n)}function Sa(t,e){if(t===er)return"input";if(t===tr)return"output";if(typeof t=="string"&&Object.hasOwn(po,t))return null;throw new Re(`${e}: unit ${JSON.stringify(t)} is neither a per-token rate (${er} | ${tr}) nor a known GPU-time unit (${Object.keys(po).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 Ra(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 nr(t,e){return`${t}\0${e}`}function xa(t){if(!Array.isArray(t))throw new Re(`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 Re(`${o} is not an object: ${JSON.stringify(r)}`);let s=r;for(let d of fo.split(","))if(!Object.hasOwn(s,d))throw new Re(`${o} has no "${d}" column: ${JSON.stringify(s)}`);let i=Sa(s.unit,o);if(i===null)continue;let a=s.model_id;if(typeof a!="string"||a==="")throw new Re(`${o} has no model_id (${JSON.stringify(s)})`);let u=s.variant;if(!(u===null||typeof u=="string"&&u!==""))throw new Re(`${o} has a non-string variant (${JSON.stringify(u)})`);e.push({model_id:a,variant:u,side:i,usd:ba(s.price_usd,o),effective_at:mo(s.effective_at,o,"effective_at"),expires_at:ka(s.expires_at,o,"expires_at")})}return e}function go(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=(y,c,m)=>{let w=y&&y.length>0?y:l?.[c];if(!w||w.length===0)return null;let R=Ra(w,c);return"reason"in R?R:R.rate.usd===0?c==="output"&&m!==null&&n?.has(nr(u,m))?R.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`}:R.rate};for(let[y,c]of o.get(u)){let m=d.get(y),w=p(m?.input,"input",c),R=p(m?.output,"output",c);if(w!==null&&"reason"in w){i.push({model_id:u,variant:c,reason:w.reason});continue}if(R!==null&&"reason"in R){i.push({model_id:u,variant:c,reason:R.reason});continue}w===null||R===null||s.push({model_id:u,variant:c,pricing:{input:w.usd,output:R.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 kt(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 St={prices:[],refusals:[]};async function ho(t){let e=t.fetchImpl??fetch,n=`${er},${tr}`,r=await e(`${t.catalogUrl}/model_prices?select=${fo}&unit=in.(${n})`,{headers:{apikey:t.anonKey,Authorization:`Bearer ${t.anonKey}`},signal:AbortSignal.timeout(va)});if(!r.ok)throw new Error(`model_prices fetch failed: ${r.status}`);return xa(await r.json())}k();var Ea={chat:!0,code:!0,agent:!0,vl:!0},yo={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function Oa(t){let e=t.toLowerCase();return Object.hasOwn(yo,e)?yo[e]:null}var wo={"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 Aa(t,e){if(!t||t.length===0)return[];let n=[],r=[];for(let o of t){let s=Object.hasOwn(wo,o)?wo[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 _o={qwen:"Qwen",qwen1:"Qwen",qwen2:"Qwen",qwen3:"Qwen",gemma:"Gemma",diffusiongemma:"Gemma",gpt:"GPT",glm:"GLM",deepseek:"DeepSeek",mistral:"Mistral"},vo={2:"Llama2",3:"Llama3"};function Ia(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(_o,s))return _o[s];let i=r[o+1];if(s==="llama"&&i!==void 0&&Object.hasOwn(vo,i))return vo[i];if(s==="phi"&&i!==void 0&&/^\d/.test(i))return"Phi"}return null}var Ta=["context_length","max_model_len"];function Pa(t){let e=Ta.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 Jt(t){let e=t.map(Pa),n=e[0];return n==null?null:e.every(r=>r===n)?n:null}function Ca(t){let e=t.engine_args?.reasoning_parser;return e==null||e===""?!1:typeof e=="string"?!0:null}function rr(t){if(t.length===0)return null;let e=t.map(Ca),n=e[0];return n==null?null:e.every(r=>r===n)?n:null}function $a(t,e){let n=e.filter(o=>Be(`${o.model_id}-${o.variant}`)===t);if(n.length>0)return Wt(n);let r=e.filter(o=>Be(o.model_id)===t);return r.length>0?Wt(r):null}function Wt(t){let e=t[0];return t.every(r=>r.model_id===e.model_id&&r.variant===e.variant)?{row:t.length===1?e:Ma(t),matches:t}:null}function Ma(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 So(t){let e=t.updated_at?Date.parse(t.updated_at):NaN;return Number.isFinite(e)?Math.floor(e/1e3):0}function Kt(t,e,n){let r=$a(t,e);if(!r)return{id:t,object:"model",created:0,owned_by:"urun"};let{row:o,matches:s}=r,i=Jt(s),a=kt(o.model_id,o.variant,n),u=Ro(o,s),d=rr(s);return{id:t,object:"model",created:So(o),owned_by:"urun",name:`${o.model_id}:${o.variant}`,...o.description?{description:o.description}:{},...o.task?{task:o.task}:{},...o.engine?{engine:o.engine}:{},...o.gpu_spec?{gpu_spec:o.gpu_spec}:{},...i!==null?{context_length:i}:{},...a!==null?{pricing:a}:{},...u.published.length>0?{voices:[...u.published]}:{},...d!==null?{capabilities:{reasoning:d}}:{}}}var La=["Vivian","Serena","Uncle_Fu","Dylan","Eric","Ryan","Aiden","Ono_Anna","Sohee"];function Ro(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:[...La],defect:o}:{published:[],defect:o}}function or(t){let e=t.length>0?Wt(t):null;return e===null?{published:[],defect:null}:Ro(e.row,e.matches)}function bo(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 ko(t){return{input:[{type:"prompt",unit:"token",cost_usd:bo(t.input)}],output:[{type:"completion",unit:"token",cost_usd:bo(t.output)}]}}function Ua(t,e,n,r){if(e.length===0)return null;let o=Wt(e);if(!o)return null;let{row:s,matches:i}=o;if(!s.task||Ea[s.task]!==!0)return null;let a=Jt(i),u=kt(s.model_id,s.variant,n),d=Ia(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:So(s),quantization:Oa(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:ko(u).input}:{}},...s.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:l,...u?{pricing:ko(u).output}:{}}],...r.datacenters.length>0?{datacenters:r.datacenters}:{},...r.compliance?{compliance:r.compliance}:{}}}function xo(t,e,n,r){let o=r??(d=>console.warn(d.trimEnd())),s=t?ut(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:Aa(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=Ua(d,e.filter(y=>y.model_id===d.model_id&&y.variant===d.variant),n,i);p&&a.push(p)}return{data:a}}var z=class extends Error{},pe=class extends Error{},ir="urs1.";function Eo(t,e){return ir+Buffer.from(JSON.stringify([t,e]),"utf8").toString("base64url")}function Na(t){if(!t.startsWith(ir))return null;try{let e=JSON.parse(Buffer.from(t.slice(ir.length),"base64url").toString("utf8"));if(Array.isArray(e)&&typeof e[0]=="string"&&typeof e[1]=="string")return{app:e[0],key:e[1]}}catch{}return null}async function To(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/apps`,r=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new Error(`org apps listing failed: GET ${n} \u2192 ${r.status}`);let o=await r.json();if(!Array.isArray(o.apps))throw new Error(`org apps listing returned no "apps" array (GET ${n})`);if(o.truncated===!0)throw new Error(`org apps listing was truncated (GET ${n} returned ${o.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return o.apps}function Oo(t){if(t.length===0)return[];if(!t.every(n=>(n.task??void 0)==="image"))return[];let e=Fa(t);return e===null?[]:[e]}function sr(t,e,n,r,o){if(!o)throw new ce(`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 ce(`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 ce(`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 Ao(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 ce(`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 Po="shared:",Xe="speech:";function Rt(t){return t.sharedApp?Po+t.appSlug:t.appSlug}function xt(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 Ye(t,e){let n=e.filter(s=>Be(`${s.model_id}-${s.variant}`)===t);if(n.length>0)return n;let r=e.filter(s=>Be(s.model_id)===t);return new Set(r.map(s=>s.variant)).size===1?r:[]}function Da(t){let e=t.engine_args?.expects_image;return e==null?"edit":e===!1?"generation":e===!0?"edit":null}function Fa(t){let e=t.map(Da),n=e[0];return n!=null&&e.every(r=>r===n)?n:null}var Io={stt:"stt",tts:"tts"};function Ge(t){if(t.length===0)return null;let e=t[0].task??void 0;return e===void 0||Io[e]===void 0?null:t.every(n=>(n.task??void 0)===e)?Io[e]:null}var zt=6e4,Yt=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??zt;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??zt;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??zt;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??zt;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 Re)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
+ `),St}let r=go(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(nr(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(Po)||o.startsWith(Xe)||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 z(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=Be(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 z(`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 z(`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=lo(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===Be(a.model_id)||o===Be(`${a.model_id}-${a.variant}`));if(i)throw new z(`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=xt(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,Xe+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:xt(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:Eo(n,this.keyOf(r))}}async speechHandleFor(e){let{app:n,entry:r}=await this.speechSessionFor(e);return{app:n,handle:Eo(n,this.keyOf(r))}}async sessionForHandle(e){let n=Na(e);if(!n)throw new pe(`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 pe(`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 pe(`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:St;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?ut(n):[]).map(d=>{let l={id:`${d.model_id}:${d.variant}`,object:"model",created:0,owned_by:"urun",name:`${d.model_id}:${d.variant}`};d.gpu_spec&&(l.gpu_spec=d.gpu_spec),typeof d.warm=="number"&&(l.warm=d.warm);let p=s.filter(R=>R.model_id===d.model_id&&R.variant===d.variant),y=Jt(p);y!==null&&(l.context_length=y);let c=rr(p);c!==null&&(l.capabilities={reasoning:c});let m=kt(d.model_id,d.variant,i);m!==null&&(l.pricing=m);let w=or(p);return w.published.length>0&&(l.voices=[...w.published]),l});return{object:"list",data:this.opts.sharedOnlyListing?u:[...e.map(d=>Kt(d,s,i)),...u]}}async openRouterModels(){let e=await this.sharedBlock(),[n,r]=await Promise.all([this.catalogRows(),this.pricesForListing(this.outputFreeKeys(e))]);return xo(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=sr(String(e),r.modelId,r.variant,o,this.opts.listCatalog!==null);return Ao(`${r.modelId}:${r.variant}`,s).includes(n)}return Oo(Ye(r.appSlug,o)).includes(n)}async supportsAudio(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Ge(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))!==null:Ge(Ye(n.appSlug,r))!==null}async supportsSpeech(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Ge(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))==="tts":Ge(Ye(n.appSlug,r))==="tts"}async supportsTranscription(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Ge(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))==="stt":Ge(Ye(n.appSlug,r))==="stt"}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):Ye(n.appSlug,r);return or(o)}async imageModelList(){let e=await this.catalogRows(),n=St,r=[];for(let s of await this.availableIds()){let i=Oo(Ye(s,e));i.length!==0&&r.push({...Kt(s,e,n),image_modes:i})}let o=await this.sharedBlock();if(o)for(let s of ut(o)){let i=sr(`${s.model_id}:${s.variant}`,s.model_id,s.variant,e,this.opts.listCatalog!==null),a=Ao(`${s.model_id}:${s.variant}`,i);if(a.length===0)continue;let u={id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun",name:`${s.model_id}:${s.variant}`,image_modes:a};s.gpu_spec&&(u.gpu_spec=s.gpu_spec),typeof s.warm=="number"&&(u.warm=s.warm),r.push(u)}return{object:"list",data:r}}async speechToTextModelList(){let e=await this.catalogRows(),n=St,r=[];for(let s of await this.availableIds())Ge(Ye(s,e))==="stt"&&r.push(Kt(s,e,n));let o=await this.sharedBlock();if(o)for(let s of ut(o)){let i=sr(`${s.model_id}:${s.variant}`,s.model_id,s.variant,e,this.opts.listCatalog!==null);if(Ge(i)!=="stt")continue;let a={id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun",name:`${s.model_id}:${s.variant}`};s.gpu_spec&&(a.gpu_spec=s.gpu_spec),typeof s.warm=="number"&&(a.warm=s.warm),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("; ")}`)}};function Co(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 Xt(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}var Ga=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function $o(t){let e=t?.code;return typeof e=="string"&&Ga.has(e)}function $e(t,e){try{return t.write(e),!0}catch(n){if($o(n))return!1;throw n}}function Mo(t){let e={stdout:!1,stderr:!1},n=[];for(let r of["stdout","stderr"]){let o=t.streams[r],s=i=>{$o(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
6
+ `)}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)}}}k();var ye=require("@prometheus-io/client"),os=require("http");k();var Va={"/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 Wa(t){return Va[t]??null}var No=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function Ja(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function Ka(t){return t==="surface"}function za(t){let e=t?.ledgerFailureReason;return No.includes(e)?e:"unknown"}var Ya=["no_response_head","no_org","model_unresolved","no_model_named","queue_full"],Lo=["priced","unpriced"],Xa={written:Lo,already_written:Lo,skipped:Ya,failed:No},Do=Object.entries(Xa).flatMap(([t,e])=>e.map(n=>({outcome:t,reason:n})));function Qa(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function Za(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 eu=256,Uo=4096,Et=12e4,tu=250,nu=8e3,je=[],ar=new Set,Ot=0,dt=!1;function Fo(){return je.length}function Bo(t=Date.now()){let e=je[0];return e===void 0?0:Math.max(0,t-e.queuedAt)}var Qt=new Set;function jo(){return Ot===0&&je.length===0}function qo(){if(jo()){dt=!1;for(let t of[...Qt])t()}}async function Ho(t=Vo){return jo()?!0:await new Promise(e=>{let n=null,r=()=>{Qt.delete(r),n&&clearTimeout(n),e(!0)};Qt.add(r),n=setTimeout(()=>{Qt.delete(r),e(!1)},t),n.unref?.()})}function Go(){dt=!0;let t=[...je.splice(0,je.length),...ar];for(let e of t)e.settled||Qe(e,{outcome:"failed",reason:"dropped_on_exit"},{event:"inference_ledger_write_failed",reason:"dropped_on_exit",attempts:e.attempts,error:`the proxy is shutting down and this row was still unplaced after the ${Vo}ms drain \u2014 the request was served and answered, and its billing row was never written`});return qo(),t.length}var Vo=5e3;function Wo(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
7
+ `}function ru(t){$e(process.stdout,t)}function Jo(t,e){try{t(e)}catch{}}function Ko(t){let{record:e,ledger:n}=t;if(n===null)return;let r=n.report??ru,o=Wa(e.path);if(o===null)return;let s=l=>{try{n.observe(l)}catch{}},i=(l,p)=>{s({outcome:"skipped",reason:l}),Jo(r,Wo("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:Za(e),completed_at:e.ts,billable_outcome:Qa(e),session_id:e.session_id,prompt_tokens:e.prompt_tokens,completion_tokens:e.completion_tokens,output_units:e.output_units,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(je.length>=Uo){i("queue_full",`${je.length} ledger writes are already waiting (capacity ${Uo}, ${Ot} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}je.push({entry:d,req:t.req,record:e,ledger:n,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),Ot<eu&&(Ot+=1,ou())}async function ou(){try{for(;;){let t=su();if(t===void 0)return;ar.add(t);try{await iu(t)}finally{ar.delete(t)}}}finally{Ot-=1,qo()}}function su(){let t=Date.now();for(;;){let e=je.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<Et)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 ${Et}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 iu(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=Ja(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=za(o),a=Date.now()-t.queuedAt;if(dt){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(Ka(i)&&a<Et){if(await uu(au(t.attempts)),dt||Date.now()-t.queuedAt>=Et){let u=Date.now()-t.queuedAt;Qe(t,{outcome:"failed",reason:dt?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:dt?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:u,error:`this row's backoff outlived the ${Et}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 au(t){return Math.min(nu,tu*2**Math.max(0,t-1))}function uu(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;Jo(t.report,Wo(r,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...o}))}k();k();var en="/v1/usage/requests",zo=1e4,du=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,Ve=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},te=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}},Zt=class extends te{constructor(e){super(e),this.name="UsageWithholdingError"}},Zo=1024*1024;function lr(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new Ve(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new Ve(`"${e}" must be an array of uuid strings`);if(n.length>zo)throw new Ve(`"${e}" carries ${n.length} ids; at most ${zo} 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"||!du.test(s))throw new Ve(`"${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 es(t){return lr(t,"inference_ids")}var lu=["inference_id","model_id","identity_kind","variant","task","http_status","started_at","completed_at","prompt_tokens","completion_tokens","output_units","cost_nano_usd","price_version","priced_at"];function lt(t,e,n){let r=t[e];if(typeof r!="string"||r==="")throw new te(`${n}: ${e} is not a non-empty string (got ${ct(r)})`);return r}function ur(t,e,n){let r=t[e];if(r===null)return null;if(typeof r!="string"||r==="")throw new te(`${n}: ${e} is neither null nor a non-empty string (got ${ct(r)})`);return r}function At(t,e,n,r){let o=t[e];if(o===null)return null;let s=typeof o=="number"?o:typeof o=="string"&&/^-?\d+(\.\d+)?$/.test(o.trim())?Number(o.trim()):NaN;if(!Number.isFinite(s))throw new te(`${n}: ${e} is not a number (got ${ct(o)})`);if(r&&!Number.isSafeInteger(s))throw new te(`${n}: ${e} is not a safely representable integer \u2014 a billing quantity that silently loses digits must fail rather than be reported`);if(s<0)throw new te(`${n}: ${e} is negative`);return Object.is(s,-0)?0:s}var cu=40,Yo=64,Xo=600;function ts(t){let e=Object.keys(t).sort(),r=e.slice(0,cu).map(o=>o.length>Yo?`${o.slice(0,Yo)}\u2026`:o).join(", ");return r.length>Xo&&(r=`${r.slice(0,Xo)}\u2026`),`${e.length} keys present: [${r}]`}function ct(t){return t===null?"null":t===void 0?"undefined":Array.isArray(t)?"an array":typeof t=="object"?"an object":`a ${typeof t}`}function dr(t){$e(process.stdout,`${JSON.stringify({event:"usage_row_rejected",ts:new Date().toISOString(),where:t.where,reason:t.reason})}
8
+ `)}function pu(t,e){let n;try{n=t(e)}catch{Qo(t,e);return}fu(n)&&Promise.resolve(n).catch(()=>{Qo(t,e)})}function fu(t){return typeof t?.then=="function"}function Qo(t,e){if(t!==dr)try{dr(e)}catch{}}function ns(t,e=dr){let n=t?.requests;if(!Array.isArray(n)){let o=t!==null&&typeof t=="object"&&!Array.isArray(t)?ts(t):ct(t);throw new te(`the usage surface answered without a "requests" array: got ${ct(n)} (${o})`)}let r=[];for(let o=0;o<n.length;o++){let s=`usage.requests[${o}]`;try{r.push(mu(n[o],s))}catch(i){if(i instanceof Zt||!(i instanceof te))throw i;pu(e,{where:s,reason:i.message})}}if(n.length>0&&r.length===0)throw new te(`the usage surface returned ${n.length} row(s) and NONE of them could be validated \u2014 answering an empty result here would be indistinguishable from "we hold nothing for these ids", which a billing caller treats as final and writes the whole batch off unbilled`);return r}function mu(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new te(`${e} is not an object: got ${ct(t)}`);let n=t;if(Object.hasOwn(n,"gpu_seconds"))throw new Zt(`${e} carries gpu_seconds \u2014 that column was DROPPED from the ledger (ENG-417): the cost of compute is metered once, on the instance, and a per-request figure counts the same GPU-second twice. Its reappearance means a second, competing measure has been reintroduced upstream`);for(let o of lu)if(!Object.hasOwn(n,o))throw new te(`${e} has no "${o}" column (${ts(n)})`);let r=At(n,"http_status",e,!0);if(r===null)throw new te(`${e}: http_status is null, but the ledger column is NOT NULL`);return{inference_id:lt(n,"inference_id",e),model_id:lt(n,"model_id",e),identity_kind:lt(n,"identity_kind",e),variant:ur(n,"variant",e),task:lt(n,"task",e),http_status:r,started_at:lt(n,"started_at",e),completed_at:lt(n,"completed_at",e),prompt_tokens:At(n,"prompt_tokens",e,!0),completion_tokens:At(n,"completion_tokens",e,!0),output_units:At(n,"output_units",e,!1),cost_nano_usd:At(n,"cost_nano_usd",e,!0),price_version:ur(n,"price_version",e),priced_at:ur(n,"priced_at",e)}}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 te(`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 te(`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 It="/partners/huggingface/v1/billing/requests";function gu(t){return lr(t,"requestIds")}function hu(t){return t>=200&&t<=399}function yu(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 rs(t,e){let n=gu(t),r=await e(n);return tn(r,n),yu(r)}var cr=9464,nn="/metrics",rn="(other)",wu="(none)",_u=64,vu=128,bu=["/v1/responses","/v1/chat/completions","/v1/systemone","/v1/messages","/v1/messages/count_tokens","/v1/models","/v1/images/models","/v1/images/generations","/v1/images/edits","/v1/audio/speech","/v1/audio/models","/v1/audio/transcriptions",en,It],ku=new Set(bu);function Su(t){return ku.has(t)?t:rn}function Ru(t){return t===null?"none":t>=500?"5xx":t>=400?"4xx":t>=300?"3xx":t>=200?"2xx":"1xx"}var on=class{constructor(e){this.max=e}max;admitted=new Set;overflows=0;of(e,n=!0){return e===null||e===""?wu:this.admitted.has(e)?e:n?this.admitted.size>=this.max?(this.overflows+=1,rn):(this.admitted.add(e),e):rn}get size(){return this.admitted.size}get overflowCount(){return this.overflows}},sn=class{registry=new ye.Registry;modelLabel=new on(_u);orgLabel=new on(vu);requests;duration;ttft;streamErrors;undelivered;ledgerWrites;ledgerQueue;labelCardinality;labelOverflows;constructor(e={}){this.requests=new ye.Counter({name:"urun_inference_requests_total",help:"Completed /v1 requests, by lane, method, HTTP status class, streaming, model and org.",labelNames:["lane","method","status_class","stream","model","org"],registers:[this.registry]}),this.duration=new ye.Histogram({name:"urun_inference_request_duration_seconds",help:"Wall time from request receipt to response close. For a stream this is the WHOLE generation, not the time to the head.",labelNames:["lane","stream","model","org"],buckets:[.1,.25,.5,1,2.5,5,10,30,60,120,300,600],registers:[this.registry]}),this.ttft=new ye.Histogram({name:"urun_inference_ttft_seconds",help:"Time to first output-text delta on a streamed response. The metric Hugging Face gates provider listing on (<5s for conversational models).",labelNames:["lane","model","org"],buckets:[.1,.25,.5,1,2,3,5,8,15,30,60],registers:[this.registry]}),this.streamErrors=new ye.Counter({name:"urun_inference_stream_errors_total",help:"Responses that emitted an error frame AFTER the head flushed. These are HTTP 200 with a complete body, so no status-based metric can see them.",labelNames:["lane","model"],registers:[this.registry]}),this.undelivered=new ye.Counter({name:"urun_inference_undelivered_total",help:"Responses whose socket did not finish flushing \u2014 the client vanished mid-answer.",labelNames:["lane","model"],registers:[this.registry]}),this.ledgerWrites=new ye.Counter({name:"urun_inference_ledger_writes_total",help:"Terminal fate of the per-request ledger row for each billable /v1 request. outcome=written is the row landing (reason=priced|unpriced); outcome=skipped and outcome=failed are BOTH revenue that went unrecorded. failed/duplicate is the ENG-410 fabricated-row signal.",labelNames:["outcome","reason"],registers:[this.registry]});for(let n of Do)this.ledgerWrites.inc({outcome:n.outcome,reason:n.reason},0);this.ledgerQueue=new ye.Gauge({name:"urun_inference_ledger_queue",help:'The ledger write queue right now. measure="depth" is rows waiting for a worker; measure="oldest_age_seconds" is how long the head has waited \u2014 approaching the dwell bound means rows are about to be given up on, which is revenue lost.',labelNames:["measure"],registers:[this.registry],collect:()=>{this.ledgerQueue.set({measure:"depth"},Fo()),this.ledgerQueue.set({measure:"oldest_age_seconds"},Bo()/1e3)}}),this.labelCardinality=new ye.Gauge({name:"urun_inference_label_cardinality",help:"Distinct values admitted for a bounded metric label. At its ceiling, further values report as (other).",labelNames:["label"],registers:[this.registry],collect:()=>{this.labelCardinality.set({label:"model"},this.modelLabel.size),this.labelCardinality.set({label:"org"},this.orgLabel.size)}}),this.labelOverflows=new ye.Counter({name:"urun_inference_label_overflows_total",help:"Observations that would have taken a new label value but found the budget spent. Non-zero means a metric dimension is now partly blind.",labelNames:["label"],registers:[this.registry]}),e.defaultMetrics!==!1&&(0,ye.collectDefaultMetrics)({register:this.registry,prefix:"urun_inference_proxy_"})}observeLedger(e){this.ledgerWrites.inc({outcome:e.outcome,reason:e.reason})}observe(e){let n=Su(e.path),r=e.status!==null&&!(e.status>=400&&e.status<500),o=this.modelLabel.overflowCount+this.orgLabel.overflowCount,s=this.modelLabel.of(e.model,r),i=this.orgLabel.of(e.org,r);this.modelLabel.overflowCount+this.orgLabel.overflowCount-o>0&&(this.modelLabel.overflowCount>o?this.labelOverflows.inc({label:"model"}):this.labelOverflows.inc({label:"org"}));let u=e.stream===!0?"true":"false";this.requests.inc({lane:n,method:e.method===""?rn:e.method,status_class:Ru(e.status),stream:u,model:s,org:i}),this.duration.observe({lane:n,stream:u,model:s,org:i},e.duration_ms/1e3),e.ttft_ms!==null&&this.ttft.observe({lane:n,model:s,org:i},e.ttft_ms/1e3),e.stream_error&&this.streamErrors.inc({lane:n,model:s}),e.delivered||this.undelivered.inc({lane:n,model:s})}scrape(){return this.registry.metrics()}get contentType(){return this.registry.contentType}};function ss(t){return(0,os.createServer)((e,n)=>{(async()=>{let r=(e.url??"/").split("?")[0];if(e.method!=="GET"||r!==nn){n.writeHead(404,{"content-type":"text/plain"}),n.end(`no route for ${e.method} ${r} \u2014 this port serves only GET ${nn}
9
9
  `);return}let o=await t.scrape();n.writeHead(200,{"content-type":t.contentType}),n.end(o)})().catch(r=>{if(!n.headersSent){n.writeHead(500,{"content-type":"text/plain"}),n.end(`${String(r instanceof Error?r.message:r)}
10
- `);return}n.end()})})}S();var or=require("crypto"),es=require("http"),sr=9465,fu="x-urun-key-id",mu="x-urun-key-fingerprint",hu="urun-ratelimit-bucket:",gu="urun-ratelimit-tier:",Zo="anonymous",yu=/^urun_sk_[A-Za-z0-9_-]{43}$/;function wu(t){return(0,or.createHash)("sha256").update(`${hu}${t}`).digest("hex")}function _u(t){return(0,or.createHash)("sha256").update(`${gu}${t}`).digest("hex")}function vu(t){let e=/^Bearer[ \t]+(\S.*)$/i.exec((t??"").trim());if(!e)return null;let n=e[1].trim();return yu.test(n)?n:null}function ts(){return(0,es.createServer)((t,e)=>{let n=vu(t.headers.authorization);e.setHeader(fu,n===null?Zo:wu(n)),e.setHeader(mu,n===null?Zo:_u(n)),e.writeHead(200),e.end()})}S();var bu=8080,tn="serve",Je="0.0.0.0";function ku(t,e,n){let r=(t[e]??"").trim();if(!r)throw new Error(`${e} is not set \u2014 ${n}`);return r}function ns(t=process.env){let e=(t.PORT??"").trim(),n=e===""?bu:Number(e);if(!Number.isInteger(n)||n<0||n>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let r=xo(Wt(ku(t,"URUN_BASE_URL","the hosted proxy needs the uRun session-gateway base it opens sessions and verifies API keys against (e.g. https://api.urun.sh)"),"URUN_BASE_URL")),o=(t.URUN_API_URL??"").trim(),s=o?Wt(o,"URUN_API_URL"):`${r.replace(/\/+$/,"")}/v1`,i=(t.URUN_CATALOG_URL??"").trim(),a=(t.URUN_CATALOG_ANON_KEY??"").trim();if(!!i!=!!a)throw new Error("URUN_CATALOG_URL and URUN_CATALOG_ANON_KEY must be set together (or both left unset) \u2014 exactly one of them is set, which would silently disable the model-catalog oracle");let u=i?{catalogUrl:Wt(i,"URUN_CATALOG_URL"),anonKey:a}:null,d=(t.URUN_SESSION_TOKEN_SECRET??"").trim(),c=(t.URUN_SHARED_ORG_API_KEY??"").trim(),l=(t.URUN_SHARED_ORG_ID??"").trim();if(!!c!=!!l)throw new Error("URUN_SHARED_ORG_API_KEY and URUN_SHARED_ORG_ID must be set together (or both left unset) \u2014 exactly one of them is set, which would silently leave the shared endpoints on the session lane");return{port:n,baseUrl:r,apiUrl:s,catalog:u,decisionSecret:d,sharedOrgKey:c,sharedOrgId:l}}S();var Di=require("http");S();S();var ss="audio",ir=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 is(t,e,n=1e4){let r=new ir(e,{sampleRate:24e3}),o=await r.startOutbound();await t.stream(ss).attach(o);let s=await Su(t,n);return await r.startInbound(s),r}function Su(t,e){let n=t.stream(ss);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 rs="rt-video-in",os=256e3;function as(t){let e=t.stream(rs),n=e.emit;if(typeof n!="function")throw new Error(`enableSessionVideo: session.stream('${rs}') 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>os)throw new Error(`enableSessionVideo: frame is ${r.byteLength} bytes \u2014 over the ${os}-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 Ru=24e3,kt=480,xu=111;async function Eu(){let t=await import("opusscript"),e=t.default??t;return new e(Ru,1,2048)}async function us(){let t=await import("werift"),e=await Eu();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>=kt;){let c=u.subarray(d,d+kt);d+=kt;let l=Buffer.from(c.buffer,c.byteOffset,c.byteLength),p=e.encode(l,kt);o=o+1&65535,s=s+kt>>>0;let m=new t.RtpHeader({version:2,payloadType:xu,sequenceNumber:o,timestamp:s,ssrc:r,marker:!1}),b=new t.RtpPacket(m,p);n.writeRtp(b)}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}}}S();var nn=require("@urun-sh/core"),ar="rt-video-out";var Ou="h264";function Au(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 Iu(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;Au(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 ds="urun.videoOut.v2-ws";function ls(t){let e=t.stream(ar);if(typeof e.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${ar}') 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: "${ar}" delivered a non-BINARY \xA75 payload (${typeof u}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let d;try{d=Iu(u)}catch(c){s(c instanceof Error?c:new Error(String(c)));continue}for(let c of n)c(d)}for(let u of o)u()};return{codec:Ou,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 an="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",Tu=1007,ps=1008,Ne=1011,un=1008,A=class extends Error{constructor(n,r=Tu){super(n);this.closeCode=r}closeCode},re=t=>typeof t=="object"&&t!==null&&!Array.isArray(t)?t:null,rn=["setup","clientContent","realtimeInput","toolResponse"];function fs(t){let e;try{e=JSON.parse(t)}catch{throw new A("client message is not valid JSON")}let n=re(e);if(!n)throw new A("client message must be a JSON object");let r=rn.filter(a=>n[a]!==void 0),o=Object.keys(n).filter(a=>!rn.includes(a));if(o.length>0)throw new A(`unknown client message field(s) ${JSON.stringify(o)} \u2014 expected exactly one of ${rn.join(", ")}`);if(r.length!==1)throw new A(`client message must contain exactly one of ${rn.join(", ")} (got ${r.length})`);let s=r[0],i=re(n[s]);if(!i)throw new A(`"${s}" must be a JSON object`);return{kind:s,[s]:i}}function ms(t,e){if(typeof t=="string")return t;let n=re(t),r=n?n.parts:void 0;if(!Array.isArray(r))throw new A(`${e}: expected a Content with a parts array`);let o=[];for(let s of r){let i=re(s);if(i&&typeof i.text=="string"){o.push(i.text);continue}throw i&&(i.inlineData!==void 0||i.fileData!==void 0)?new A(`${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 A(`${e}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new A(`${e}: unsupported part ${JSON.stringify(s)}`)}return o.join(`
11
- `)}var Pu={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},Ke="urun.videoOut";function hs(t){return{framing:ds,codec:t}}function gs(t){let{seq:e,ptsUs:n,codec:r,key:o}=t.header;return{[Ke]:{v:1,seq:e,ptsUs:n,codec:r,key:o,data:Buffer.from(t.au).toString("base64")}}}var Cu={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},$u=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",Ke],cs=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function Mu(t){let e=re(t);if(!e)throw new A("setup.realtimeInputConfig must be an object");for(let o of Object.keys(e))if(!(o==="automaticActivityDetection"||o==="activityHandling"))throw new A(o==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${o}"`);let n=re(e.automaticActivityDetection);if(e.automaticActivityDetection!==void 0&&!n)throw new A("realtimeInputConfig.automaticActivityDetection must be an object");for(let o of Object.keys(n??{}))if(o!=="disabled")throw new A(`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 A("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(!cs.includes(e.activityHandling))throw new A(`unsupported activityHandling ${JSON.stringify(e.activityHandling)} \u2014 expected one of ${cs.join(", ")}`);r=e.activityHandling}return{manualActivity:!0,activityHandling:r}}var Uu=["temperature","maxOutputTokens","responseModalities"];function ys(t){for(let[l,p]of Object.entries(Cu))if(t[l]!==void 0)throw new A(p);for(let l of Object.keys(t))if(!$u.includes(l))throw new A(`unsupported setup field "${l}"`);if(typeof t.model!="string"||t.model.length===0)throw new A('setup.model is required (format "models/{model}")');let e=t.model.replace(/^models\//,""),n,r,o=!1,s=re(t.generationConfig);if(t.generationConfig!==void 0&&!s)throw new A("setup.generationConfig must be an object");if(s){for(let l of Object.keys(s))if(!Uu.includes(l))throw new A(`unsupported generationConfig field "${l}"`);if(s.temperature!==void 0){if(typeof s.temperature!="number")throw new A("generationConfig.temperature must be a number");n=s.temperature}if(s.maxOutputTokens!==void 0){if(typeof s.maxOutputTokens!="number")throw new A("generationConfig.maxOutputTokens must be a number");r=s.maxOutputTokens}if(s.responseModalities!==void 0){let l=Array.isArray(s.responseModalities)?s.responseModalities:[s.responseModalities];for(let p of l){let m=String(p).toUpperCase();if(m==="AUDIO"){o=!0;continue}if(m!=="TEXT")throw new A(`responseModalities ${JSON.stringify(p)} 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:ms(t.systemInstruction,"setup.systemInstruction")});let a;if(t.tools!==void 0){if(!Array.isArray(t.tools))throw new A("setup.tools must be an array");a=[];for(let l of t.tools){let p=re(l);if(!p)throw new A("setup.tools entries must be objects");for(let m of Object.keys(p))if(m!=="functionDeclarations")throw new A(`unsupported tool "${m}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(p.functionDeclarations))throw new A("tool.functionDeclarations must be an array");for(let m of p.functionDeclarations){let b=re(m);if(!b||typeof b.name!="string")throw new A("functionDeclarations entries require a string name");a.push({type:"function",name:b.name,description:b.description,parameters:b.parameters})}}a.length===0&&(a=void 0)}let u={enabled:!1,handle:null};if(t.sessionResumption!==void 0){let l=re(t.sessionResumption);if(!l)throw new A("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let p of Object.keys(l))if(p!=="handle")throw new A(`unsupported sessionResumption field "${p}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(l.handle!==void 0&&(typeof l.handle!="string"||l.handle.length===0))throw new A("sessionResumption.handle must be a non-empty string when present");u={enabled:!0,handle:typeof l.handle=="string"?l.handle:null}}let d=t.realtimeInputConfig===void 0?Pu:Mu(t.realtimeInputConfig),c=!1;if(t[Ke]!==void 0){if(!re(t[Ke]))throw new A(`setup["${Ke}"] must be an object (the v1 opt-in is {})`);c=!0}return{model:e,systemItems:i,tools:a,temperature:n,maxOutputTokens:r,audioModality:o,activity:d,sessionResumption:u,videoOut:c}}function ws(t){return{sessionResumptionUpdate:{newHandle:t,resumable:!0}}}function _s(t){let e=Math.max(0,t??0)/1e3;return{goAway:{timeLeft:`${Math.round(e*1e3)/1e3}s`}}}function vs(t){for(let r of Object.keys(t))if(r!=="turns"&&r!=="turnComplete")throw new A(`unsupported clientContent field "${r}"`);let e=t.turns===void 0?[]:t.turns;if(!Array.isArray(e))throw new A("clientContent.turns must be an array");let n=[];for(let r of e){let o=re(r);if(!o)throw new A("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 A(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(o.role)})`);n.push({role:s==="model"?"assistant":"user",content:ms(o,"clientContent.turns")})}return{items:n,turnComplete:t.turnComplete===!0}}var on=16e3,bs=24e3,Lu=`audio/pcm;rate=${bs}`,ks=Buffer.alloc(960).toString("base64");function Nu(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 c=u-d;o[a]=Math.round(t[d]+(t[d+1]-t[d])*c)}return o}function Du(t){let e=re(t);if(!e)throw new A("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(e.mimeType))throw new A(`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 A("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 A("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(n)}function Fu(t){let e=re(t);if(!e)throw new A("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(e.mimeType))throw new A(`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])!==on)throw new A(`realtimeInput.audio.mimeType declares rate=${n[1]} \u2014 the Live spec input format is ${on} Hz (audio/pcm;rate=${on})`);if(typeof e.data!="string"||e.data.length===0)throw new A("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 A(`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=Nu(o,on,bs);return Buffer.from(s.buffer,s.byteOffset,s.byteLength).toString("base64")}function Ss(t){if(t.mediaChunks!==void 0)throw new A('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 A("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let o=t.activityStart!==void 0?"activityStart":"activityEnd",s=re(t[o]);if(!s||Object.keys(s).length!==0)throw new A(`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 A(`unsupported realtimeInput field "${r}"`);let n=e.filter(r=>t[r]!==void 0);if(n.length!==1)throw new A(`realtimeInput must carry exactly one of ${e.join(", ")} (got ${n.length})`);if(t.audio!==void 0)return{kind:"audio",base64Pcm24k:Fu(t.audio)};if(t.video!==void 0)return{kind:"video",jpegFrame:Du(t.video)};if(t.audioStreamEnd!==void 0){if(t.audioStreamEnd!==!0)throw new A("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof t.text!="string"||t.text.length===0)throw new A("realtimeInput.text must be a non-empty string");return{kind:"text",text:t.text}}function Rs(t){for(let e of Object.keys(t))if(e!=="functionResponses")throw new A(`unsupported toolResponse field "${e}"`);if(!Array.isArray(t.functionResponses)||t.functionResponses.length===0)throw new A("toolResponse.functionResponses must be a non-empty array");return t.functionResponses.map(e=>{let n=re(e);if(!n||typeof n.id!="string"||n.id.length===0)throw new A("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 xs(t){return t.type!=="response.output_text.delta"||typeof t.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:t.delta}]}}}}function Es(t){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:Lu,data:t}}]}}}}function Os(t){let e=re(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 c=re(d);if(!(c?.type!=="message"||!Array.isArray(c.content)))for(let l of c.content){let p=re(l);p?.type==="output_text"&&typeof p.text=="string"&&(s+=p.text)}}s&&o.push({role:"assistant",content:s});for(let d of n){let c=re(d);if(c?.type!=="function_call")continue;if(typeof c.call_id!="string"||typeof c.name!="string")throw new A(`upstream function_call item is missing call_id/name: ${JSON.stringify(d)}`,Ne);let l=typeof c.arguments=="string"&&c.arguments.length>0?c.arguments:"{}",p;try{p=JSON.parse(l)}catch{throw new A(`upstream function_call arguments are not valid JSON: ${l}`,Ne)}r.push({id:c.call_id,name:c.name,args:p}),o.push({type:"function_call",call_id:c.call_id,name:c.name,arguments:l})}let i=[];r.length>0&&i.push({toolCall:{functionCalls:r}}),i.push({serverContent:{generationComplete:!0}});let a=re(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}}S();var As=require("crypto"),Is=require("ws");var Bu=256,ur=class{snapshots=new Map;store(e,n,r){for(this.snapshots.set(`${e}\0${n}`,r);this.snapshots.size>Bu;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(e,n){return this.snapshots.get(`${e}\0${n}`)}},ju="local",St=t=>t.slice(0,120),dr=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 z){this.ws.close(ps,St(e.message));return}if(e instanceof ce){this.ws.close(un,St(e.message));return}if(e instanceof A){this.ws.close(e.closeCode,St(e.message));return}this.ws.close(Ne,St(String(e instanceof Error?e.message:e)))}onMessage(e){try{let n=fs(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 A(`"${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}=vs(n.clientContent);this.transcript.push(...o),s&&this.enqueue(()=>this.runTurn());return}if(n.kind==="realtimeInput"){let o=Ss(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:ks);return}this.transcript.push(...Rs(n.toolResponse)),this.enqueue(()=>this.runTurn())}catch(n){this.fail(n)}}async handleSetup(e){if(this.config)throw new A("setup may only be sent once, as the first client message");let n=ys(e),r=n.sessionResumption.handle;if(r!==null){let s=this.registry.get(this.scope,r);if(!s)throw new A("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",un);if(s.model!==n.model)throw new A(`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 A("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 A(`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`,un);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(gs(d))),u=i.onLaneError(d=>this.fail(d));this.detachVideoOut=()=>{a(),u()},o[Ke]=hs(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 A("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openAudio;if(!n)throw new A("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native AudioBridge seam over the canonical `audio` lane)",Ne);return this.audio=(async()=>{let r=await n.call(this.clients,e.model);return this.detachAudio=r.onOutputAudio(o=>this.send(Es(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 A("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openVideo;if(!n)throw new A("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",Ne);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 A(`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 A("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 A("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,As.randomUUID)();this.registry.store(this.scope,r,{model:e.model,proxyHandle:n,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(r),this.send(ws(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(_s(e.timeLeftMs)),this.ws.close(Ne,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 A(`upstream error: ${JSON.stringify(d)}`,Ne);if(d.type==="response.completed"){o=d.response,s=!0;continue}let c=xs(d);c&&this.send(c)}if(n.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!s)throw new A("upstream produced no response.completed event",Ne);let{messages:i,assistantItems:a}=Os(o);this.transcript.push(...a);for(let u of i)this.send(u);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},ze=class extends Error{constructor(n,r){super(r);this.status=n;this.name="UpgradeAuthError"}status};function qu(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 ze(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 ze(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 ze(401,"missing credentials: send the org API key in the x-goog-api-key header or as Authorization: Bearer");return a??""}function lr(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 Is.WebSocketServer({noServer:!0}),o=new ur;t.on("upgrade",(i,a,u)=>{let d=new URL(i.url??"/","http://localhost");d.pathname===an&&(a.on("error",()=>{}),(async()=>{let c=qu(i,d,n),l;if(e.authenticate)l=await e.authenticate(c,i);else{if(e.apiKey!==void 0&&c!==e.apiKey)throw new ze(401,"invalid local proxy api key");l={clients:e.clients,scope:ju}}r.handleUpgrade(i,a,u,p=>{new dr(p,l,o)})})().catch(c=>{let l=502;(c instanceof ze||typeof c=="object"&&c!==null&&"status"in c&&typeof c.status=="number")&&(l=c.status);let p=l===401?"Unauthorized":l===403?"Forbidden":l===502?"Bad Gateway":"Internal Server Error",m=c instanceof Error?c.message:String(c);a.destroyed||(a.write(`HTTP/1.1 ${l} ${p}\r
10
+ `);return}n.end()})})}k();var pr=require("crypto"),as=require("http"),fr=9465,xu="x-urun-key-id",Eu="x-urun-key-fingerprint",Ou="urun-ratelimit-bucket:",Au="urun-ratelimit-tier:",is="anonymous",Iu=/^urun_sk_[A-Za-z0-9_-]{43}$/;function Tu(t){return(0,pr.createHash)("sha256").update(`${Ou}${t}`).digest("hex")}function Pu(t){return(0,pr.createHash)("sha256").update(`${Au}${t}`).digest("hex")}function Cu(t){let e=/^Bearer[ \t]+(\S.*)$/i.exec((t??"").trim());if(!e)return null;let n=e[1].trim();return Iu.test(n)?n:null}function us(){return(0,as.createServer)((t,e)=>{let n=Cu(t.headers.authorization);e.setHeader(xu,n===null?is:Tu(n)),e.setHeader(Eu,n===null?is:Pu(n)),e.writeHead(200),e.end()})}k();var $u=8080,an="serve",Ze="0.0.0.0";function Mu(t,e,n){let r=(t[e]??"").trim();if(!r)throw new Error(`${e} is not set \u2014 ${n}`);return r}function ds(t=process.env){let e=(t.PORT??"").trim(),n=e===""?$u:Number(e);if(!Number.isInteger(n)||n<0||n>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let r=Co(Xt(Mu(t,"URUN_BASE_URL","the hosted proxy needs the uRun session-gateway base it opens sessions and verifies API keys against (e.g. https://api.urun.sh)"),"URUN_BASE_URL")),o=(t.URUN_API_URL??"").trim(),s=o?Xt(o,"URUN_API_URL"):`${r.replace(/\/+$/,"")}/v1`,i=(t.URUN_CATALOG_URL??"").trim(),a=(t.URUN_CATALOG_ANON_KEY??"").trim();if(!!i!=!!a)throw new Error("URUN_CATALOG_URL and URUN_CATALOG_ANON_KEY must be set together (or both left unset) \u2014 exactly one of them is set, which would silently disable the model-catalog oracle");let u=i?{catalogUrl:Xt(i,"URUN_CATALOG_URL"),anonKey:a}:null,d=(t.URUN_SESSION_TOKEN_SECRET??"").trim(),l=(t.URUN_SHARED_ORG_API_KEY??"").trim(),p=(t.URUN_SHARED_ORG_ID??"").trim();if(!!l!=!!p)throw new Error("URUN_SHARED_ORG_API_KEY and URUN_SHARED_ORG_ID must be set together (or both left unset) \u2014 exactly one of them is set, which would silently leave the shared endpoints on the session lane");return{port:n,baseUrl:r,apiUrl:s,catalog:u,decisionSecret:d,sharedOrgKey:l,sharedOrgId:p}}k();var Yi=require("http");k();k();var ps="audio",mr=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 fs(t,e,n=1e4){let r=new mr(e,{sampleRate:24e3}),o=await r.startOutbound();await t.stream(ps).attach(o);let s=await Lu(t,n);return await r.startInbound(s),r}function Lu(t,e){let n=t.stream(ps);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 ls="rt-video-in",cs=256e3;function ms(t){let e=t.stream(ls),n=e.emit;if(typeof n!="function")throw new Error(`enableSessionVideo: session.stream('${ls}') 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>cs)throw new Error(`enableSessionVideo: frame is ${r.byteLength} bytes \u2014 over the ${cs}-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 Uu=24e3,Tt=480,Nu=111;async function Du(){let t=await import("opusscript"),e=t.default??t;return new e(Uu,1,2048)}async function gs(){let t=await import("werift"),e=await Du();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>=Tt;){let l=u.subarray(d,d+Tt);d+=Tt;let p=Buffer.from(l.buffer,l.byteOffset,l.byteLength),y=e.encode(p,Tt);o=o+1&65535,s=s+Tt>>>0;let c=new t.RtpHeader({version:2,payloadType:Nu,sequenceNumber:o,timestamp:s,ssrc:r,marker:!1}),m=new t.RtpPacket(c,y);n.writeRtp(m)}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}}}k();var un=require("@urun-sh/core"),gr="rt-video-out";var Fu="h264";function Bu(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 ju(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;Bu(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 hs="urun.videoOut.v2-ws";function ys(t){let e=t.stream(gr);if(typeof e.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${gr}') 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: "${gr}" delivered a non-BINARY \xA75 payload (${typeof u}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let d;try{d=ju(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:Fu,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 pn="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",qu=1007,_s=1008,qe=1011,fn=1008,A=class extends Error{constructor(n,r=qu){super(n);this.closeCode=r}closeCode},oe=t=>typeof t=="object"&&t!==null&&!Array.isArray(t)?t:null,dn=["setup","clientContent","realtimeInput","toolResponse"];function vs(t){let e;try{e=JSON.parse(t)}catch{throw new A("client message is not valid JSON")}let n=oe(e);if(!n)throw new A("client message must be a JSON object");let r=dn.filter(a=>n[a]!==void 0),o=Object.keys(n).filter(a=>!dn.includes(a));if(o.length>0)throw new A(`unknown client message field(s) ${JSON.stringify(o)} \u2014 expected exactly one of ${dn.join(", ")}`);if(r.length!==1)throw new A(`client message must contain exactly one of ${dn.join(", ")} (got ${r.length})`);let s=r[0],i=oe(n[s]);if(!i)throw new A(`"${s}" must be a JSON object`);return{kind:s,[s]:i}}function bs(t,e){if(typeof t=="string")return t;let n=oe(t),r=n?n.parts:void 0;if(!Array.isArray(r))throw new A(`${e}: expected a Content with a parts array`);let o=[];for(let s of r){let i=oe(s);if(i&&typeof i.text=="string"){o.push(i.text);continue}throw i&&(i.inlineData!==void 0||i.fileData!==void 0)?new A(`${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 A(`${e}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new A(`${e}: unsupported part ${JSON.stringify(s)}`)}return o.join(`
11
+ `)}var Hu={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},et="urun.videoOut";function ks(t){return{framing:hs,codec:t}}function Ss(t){let{seq:e,ptsUs:n,codec:r,key:o}=t.header;return{[et]:{v:1,seq:e,ptsUs:n,codec:r,key:o,data:Buffer.from(t.au).toString("base64")}}}var Gu={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},Vu=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",et],ws=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function Wu(t){let e=oe(t);if(!e)throw new A("setup.realtimeInputConfig must be an object");for(let o of Object.keys(e))if(!(o==="automaticActivityDetection"||o==="activityHandling"))throw new A(o==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${o}"`);let n=oe(e.automaticActivityDetection);if(e.automaticActivityDetection!==void 0&&!n)throw new A("realtimeInputConfig.automaticActivityDetection must be an object");for(let o of Object.keys(n??{}))if(o!=="disabled")throw new A(`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 A("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(!ws.includes(e.activityHandling))throw new A(`unsupported activityHandling ${JSON.stringify(e.activityHandling)} \u2014 expected one of ${ws.join(", ")}`);r=e.activityHandling}return{manualActivity:!0,activityHandling:r}}var Ju=["temperature","maxOutputTokens","responseModalities"];function Rs(t){for(let[p,y]of Object.entries(Gu))if(t[p]!==void 0)throw new A(y);for(let p of Object.keys(t))if(!Vu.includes(p))throw new A(`unsupported setup field "${p}"`);if(typeof t.model!="string"||t.model.length===0)throw new A('setup.model is required (format "models/{model}")');let e=t.model.replace(/^models\//,""),n,r,o=!1,s=oe(t.generationConfig);if(t.generationConfig!==void 0&&!s)throw new A("setup.generationConfig must be an object");if(s){for(let p of Object.keys(s))if(!Ju.includes(p))throw new A(`unsupported generationConfig field "${p}"`);if(s.temperature!==void 0){if(typeof s.temperature!="number")throw new A("generationConfig.temperature must be a number");n=s.temperature}if(s.maxOutputTokens!==void 0){if(typeof s.maxOutputTokens!="number")throw new A("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 y of p){let c=String(y).toUpperCase();if(c==="AUDIO"){o=!0;continue}if(c!=="TEXT")throw new A(`responseModalities ${JSON.stringify(y)} 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:bs(t.systemInstruction,"setup.systemInstruction")});let a;if(t.tools!==void 0){if(!Array.isArray(t.tools))throw new A("setup.tools must be an array");a=[];for(let p of t.tools){let y=oe(p);if(!y)throw new A("setup.tools entries must be objects");for(let c of Object.keys(y))if(c!=="functionDeclarations")throw new A(`unsupported tool "${c}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(y.functionDeclarations))throw new A("tool.functionDeclarations must be an array");for(let c of y.functionDeclarations){let m=oe(c);if(!m||typeof m.name!="string")throw new A("functionDeclarations entries require a string name");a.push({type:"function",name:m.name,description:m.description,parameters:m.parameters})}}a.length===0&&(a=void 0)}let u={enabled:!1,handle:null};if(t.sessionResumption!==void 0){let p=oe(t.sessionResumption);if(!p)throw new A("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let y of Object.keys(p))if(y!=="handle")throw new A(`unsupported sessionResumption field "${y}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(p.handle!==void 0&&(typeof p.handle!="string"||p.handle.length===0))throw new A("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?Hu:Wu(t.realtimeInputConfig),l=!1;if(t[et]!==void 0){if(!oe(t[et]))throw new A(`setup["${et}"] 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 xs(t){return{sessionResumptionUpdate:{newHandle:t,resumable:!0}}}function Es(t){let e=Math.max(0,t??0)/1e3;return{goAway:{timeLeft:`${Math.round(e*1e3)/1e3}s`}}}function Os(t){for(let r of Object.keys(t))if(r!=="turns"&&r!=="turnComplete")throw new A(`unsupported clientContent field "${r}"`);let e=t.turns===void 0?[]:t.turns;if(!Array.isArray(e))throw new A("clientContent.turns must be an array");let n=[];for(let r of e){let o=oe(r);if(!o)throw new A("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 A(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(o.role)})`);n.push({role:s==="model"?"assistant":"user",content:bs(o,"clientContent.turns")})}return{items:n,turnComplete:t.turnComplete===!0}}var ln=16e3,As=24e3,Ku=`audio/pcm;rate=${As}`,Is=Buffer.alloc(960).toString("base64");function zu(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 Yu(t){let e=oe(t);if(!e)throw new A("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(e.mimeType))throw new A(`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 A("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 A("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(n)}function Xu(t){let e=oe(t);if(!e)throw new A("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(e.mimeType))throw new A(`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])!==ln)throw new A(`realtimeInput.audio.mimeType declares rate=${n[1]} \u2014 the Live spec input format is ${ln} Hz (audio/pcm;rate=${ln})`);if(typeof e.data!="string"||e.data.length===0)throw new A("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 A(`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=zu(o,ln,As);return Buffer.from(s.buffer,s.byteOffset,s.byteLength).toString("base64")}function Ts(t){if(t.mediaChunks!==void 0)throw new A('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 A("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let o=t.activityStart!==void 0?"activityStart":"activityEnd",s=oe(t[o]);if(!s||Object.keys(s).length!==0)throw new A(`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 A(`unsupported realtimeInput field "${r}"`);let n=e.filter(r=>t[r]!==void 0);if(n.length!==1)throw new A(`realtimeInput must carry exactly one of ${e.join(", ")} (got ${n.length})`);if(t.audio!==void 0)return{kind:"audio",base64Pcm24k:Xu(t.audio)};if(t.video!==void 0)return{kind:"video",jpegFrame:Yu(t.video)};if(t.audioStreamEnd!==void 0){if(t.audioStreamEnd!==!0)throw new A("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof t.text!="string"||t.text.length===0)throw new A("realtimeInput.text must be a non-empty string");return{kind:"text",text:t.text}}function Ps(t){for(let e of Object.keys(t))if(e!=="functionResponses")throw new A(`unsupported toolResponse field "${e}"`);if(!Array.isArray(t.functionResponses)||t.functionResponses.length===0)throw new A("toolResponse.functionResponses must be a non-empty array");return t.functionResponses.map(e=>{let n=oe(e);if(!n||typeof n.id!="string"||n.id.length===0)throw new A("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 Cs(t){return t.type!=="response.output_text.delta"||typeof t.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:t.delta}]}}}}function $s(t){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:Ku,data:t}}]}}}}function Ms(t){let e=oe(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=oe(d);if(!(l?.type!=="message"||!Array.isArray(l.content)))for(let p of l.content){let y=oe(p);y?.type==="output_text"&&typeof y.text=="string"&&(s+=y.text)}}s&&o.push({role:"assistant",content:s});for(let d of n){let l=oe(d);if(l?.type!=="function_call")continue;if(typeof l.call_id!="string"||typeof l.name!="string")throw new A(`upstream function_call item is missing call_id/name: ${JSON.stringify(d)}`,qe);let p=typeof l.arguments=="string"&&l.arguments.length>0?l.arguments:"{}",y;try{y=JSON.parse(p)}catch{throw new A(`upstream function_call arguments are not valid JSON: ${p}`,qe)}r.push({id:l.call_id,name:l.name,args:y}),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=oe(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}}k();var Ls=require("crypto"),Us=require("ws");var Qu=256,hr=class{snapshots=new Map;store(e,n,r){for(this.snapshots.set(`${e}\0${n}`,r);this.snapshots.size>Qu;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(e,n){return this.snapshots.get(`${e}\0${n}`)}},Zu="local",Pt=t=>t.slice(0,120),yr=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 z){this.ws.close(_s,Pt(e.message));return}if(e instanceof pe){this.ws.close(fn,Pt(e.message));return}if(e instanceof A){this.ws.close(e.closeCode,Pt(e.message));return}this.ws.close(qe,Pt(String(e instanceof Error?e.message:e)))}onMessage(e){try{let n=vs(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 A(`"${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}=Os(n.clientContent);this.transcript.push(...o),s&&this.enqueue(()=>this.runTurn());return}if(n.kind==="realtimeInput"){let o=Ts(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:Is);return}this.transcript.push(...Ps(n.toolResponse)),this.enqueue(()=>this.runTurn())}catch(n){this.fail(n)}}async handleSetup(e){if(this.config)throw new A("setup may only be sent once, as the first client message");let n=Rs(e),r=n.sessionResumption.handle;if(r!==null){let s=this.registry.get(this.scope,r);if(!s)throw new A("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",fn);if(s.model!==n.model)throw new A(`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 A("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 A(`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`,fn);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(Ss(d))),u=i.onLaneError(d=>this.fail(d));this.detachVideoOut=()=>{a(),u()},o[et]=ks(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 A("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openAudio;if(!n)throw new A("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native AudioBridge seam over the canonical `audio` lane)",qe);return this.audio=(async()=>{let r=await n.call(this.clients,e.model);return this.detachAudio=r.onOutputAudio(o=>this.send($s(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 A("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openVideo;if(!n)throw new A("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",qe);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 A(`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 A("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 A("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,Ls.randomUUID)();this.registry.store(this.scope,r,{model:e.model,proxyHandle:n,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(r),this.send(xs(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(Es(e.timeLeftMs)),this.ws.close(qe,Pt(`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 A(`upstream error: ${JSON.stringify(d)}`,qe);if(d.type==="response.completed"){o=d.response,s=!0;continue}let l=Cs(d);l&&this.send(l)}if(n.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!s)throw new A("upstream produced no response.completed event",qe);let{messages:i,assistantItems:a}=Ms(o);this.transcript.push(...a);for(let u of i)this.send(u);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},tt=class extends Error{constructor(n,r){super(r);this.status=n;this.name="UpgradeAuthError"}status};function ed(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 tt(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 tt(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 tt(401,"missing credentials: send the org API key in the x-goog-api-key header or as Authorization: Bearer");return a??""}function wr(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 Us.WebSocketServer({noServer:!0}),o=new hr;t.on("upgrade",(i,a,u)=>{let d=new URL(i.url??"/","http://localhost");d.pathname===pn&&(a.on("error",()=>{}),(async()=>{let l=ed(i,d,n),p;if(e.authenticate)p=await e.authenticate(l,i);else{if(e.apiKey!==void 0&&l!==e.apiKey)throw new tt(401,"invalid local proxy api key");p={clients:e.clients,scope:Zu}}r.handleUpgrade(i,a,u,y=>{new yr(y,p,o)})})().catch(l=>{let p=502;(l instanceof tt||typeof l=="object"&&l!==null&&"status"in l&&typeof l.status=="number")&&(p=l.status);let y=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} ${y}\r
12
12
  Connection: close\r
13
13
  \r
14
- ${m}\r
15
- `),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)}S();var He=require("ws");S();S();var Ts=require("crypto"),Ps="Inference-Id";function Rt(){return(0,Ts.randomUUID)()}function cr(t){return t==="/v1"||t.startsWith("/v1/")}function Cs(t,e){if(!cr(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 xt(t){return`resp_${t}`}function $s(t){return`chatcmpl-${t}`}function Ms(t){return`msg_${t}`}function Te(t){return e=>{t.sessionId=e.sessionId,t.catalog=e.identity}}function Us(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,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 Ls(t){return`${JSON.stringify(t)}
16
- `}var Hu=256,Gu=128;function be(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,Gu)}var Ye=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>=Hu?"(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}}}},Vu=256;function Wu(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 Xe=class extends Error{},st=class{conversations=new Map;thread(e,n){let r=Wu(n);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new Xe(`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<=Vu)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},Ns=new WeakMap;function Et(t){let e=Ns.get(t);return e||(e=new st,Ns.set(t,e)),e}function it(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 at(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 qe=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}))}},Ju=["<tool_call>","<function="];function Ku(t){return Ju.find(e=>t.includes(e))}function Qe(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 Ze(t,e,n){if(!(!t||e>0))return Ku(n)}function dn(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 zu(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 pr(t,e){let n=zu(t);return n.length>0?n:e?[{role:"assistant",content:e}]:[]}var ve=class extends Error{constructor(n,r,o=400,s){super(n);this.code=r;this.status=o;this.param=s}code;status;param};function ut(t,e){try{return t.thread(e.previous_response_id,e.input)}catch(n){throw n instanceof Xe?new ve(n.message,"previous_response_not_found",400,"previous_response_id"):new ve(String(n instanceof Error?n.message:n),"invalid_input")}}function Ds(t){let e=t.text;if(e!==null&&typeof e=="object"){let n=e.format;if(n!==void 0)return n}}async function fr(t,e,n,r,o){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new ve("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}:{},...Ds(e)!==void 0?{response_format:Ds(e)}:{}},o)}async function Ot(t){let{body:e,clients:n,store:r,stats:o}=t,s=ut(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=be(e);o.model(d);let c=Array.isArray(e.tools)&&e.tools.length>0,l=t.turn,p=await fr(n,e,s,!0,l?Te(l):void 0);return{responseId:i,events:Yu({events:p,conversation:s,responseId:i,modelKey:d,requestHadTools:c,remember:a,persist:u,store:r,stats:o,turn:t.turn})}}async function*Yu(t){let{events:e,conversation:n,responseId:r,modelKey:o,requestHadTools:s,remember:i,persist:a,store:u,stats:d}=t,c=[],l=q=>q.response&&typeof q.response=="object"?{...q,response:{...q.response,id:r}}:q,p=`item_${r.slice(5)}`,m=`rs_${r.slice(5)}`,b=!1,E=!1,w=0;function*g(){b||(b=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let v=!1,_=0,C="";function*R(){v&&(v=!1,yield{type:"response.reasoning_text.done",item_id:m,output_index:_,content_index:0,text:C},yield{type:"response.output_item.done",output_index:_,item:{id:m,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:C}],status:"completed"}})}let O=!1,k=0,$="";function*j(q){O&&(O=!1,yield{type:"response.output_text.done",item_id:p,output_index:k,content_index:0,text:$},yield{type:"response.content_part.done",item_id:p,output_index:k,content_index:0,part:{type:"output_text",text:$}},yield{type:"response.output_item.done",output_index:k,item:{id:p,type:"message",role:"assistant",status:q,content:[{type:"output_text",text:$}]}})}let W=new qe,F=null,de=-1;function*pe(q){if(!F)return;let Y=F;F=null,yield{type:"response.function_call_arguments.done",item_id:Y.fcId,output_index:Y.outputIndex,arguments:Y.args},yield{type:"response.output_item.done",output_index:Y.outputIndex,item:{id:Y.fcId,type:"function_call",call_id:Y.callId??Y.fcId,name:Y.name??"",arguments:Y.args,status:q}}}let oe=!1;for await(let q of d.track(e,o,t.turn)){c.push(q);let Y=String(q.type??"");if(Y==="response.created"||Y==="response.in_progress"){b=!0,yield l(q);continue}if(Y==="response.output_item.added"){E=!0,yield l(q);continue}if(Y==="response.reasoning_text.delta"&&!E){yield*g(),v||(v=!0,_=w++,yield{type:"response.output_item.added",output_index:_,item:{id:m,type:"reasoning",summary:[],content:[],status:"in_progress"}}),C+=String(q.delta??""),yield{type:"response.reasoning_text.delta",item_id:m,output_index:_,content_index:0,delta:q.delta};continue}if(Y==="response.output_text.delta"&&!E){yield*g(),yield*R(),O||(O=!0,k=w++,yield{type:"response.output_item.added",output_index:k,item:{id:p,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:p,output_index:k,content_index:0,part:{type:"output_text",text:""}}),$+=String(q.delta??""),yield{type:Y,item_id:p,output_index:k,content_index:0,delta:q.delta};continue}let Z=E?null:at(q);if(Z){if(yield*g(),yield*R(),yield*j("completed"),W.add(Z),Z.tool_index!==de){yield*pe("completed"),de=Z.tool_index;let he=w++;F={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:F.fcId,type:"function_call",call_id:F.callId??F.fcId,name:F.name??"",arguments:"",status:"in_progress"}}}F&&(Z.call_id&&(F.callId=Z.call_id),Z.name&&(F.name=Z.name),F.args+=Z.delta,yield{type:"response.function_call_arguments.delta",item_id:F.fcId,output_index:F.outputIndex,delta:Z.delta});continue}if(Y==="response.completed"&&!E){let he=it(q.response),Ee=Ze(s,W.size+he.length,$);if(Ee){oe=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:Qe(o,Ee)}};break}if(yield*R(),yield*j("completed"),yield*pe("completed"),W.size===0)for(let Oe of he){let Se=w++,U=Oe.id??Oe.call_id??`fc_${Se}`,we=Oe.arguments??"",Be={id:U,type:"function_call",call_id:Oe.call_id??U,name:Oe.name??""};yield{type:"response.output_item.added",output_index:Se,item:{...Be,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:U,output_index:Se,delta:we},yield{type:"response.function_call_arguments.done",item_id:U,output_index:Se,arguments:we},yield{type:"response.output_item.done",output_index:Se,item:{...Be,arguments:we,status:"completed"}}}yield l(q);continue}yield l(q)}if(yield*R(),yield*j("incomplete"),yield*pe("incomplete"),i&&!oe){let q=Pe(c);u.remember(r,n,pr(q,dn(c,q)),a)}}var gr="/v1/responses",Xu=64*1024*1024,Qu=8*1024*1024,Zu=16,Fs=32,Bs=3600*1e3,ed=/^[A-Za-z0-9._-]{1,256}$/;function ye(t,e,n){return{type:"error",status:t,...n!==void 0?{stream_id:n}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var td=ye(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 yr(){let t,e;return{promise:new Promise((r,o)=>{t=r,e=o}),resolve:t,reject:e}}async function nd(t){for(;t.bufferedAmount>Qu&&t.readyState===He.WebSocket.OPEN;){let{promise:e,resolve:n}=yr();setTimeout(n,5),await e}}var mr=class extends st{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 Xe))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)}},hr=class{constructor(e,n,r,o){this.ws=e;this.clients=n;this.stats=r;this.statusOf=o;this.store=new mr(Et(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(ye(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${Bs/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},Bs).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(ye(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(ye(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(ye(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(ye(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(ye(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(ye(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"||!ed.test(o.stream_id)){this.sendFrame(td);return}if(s=o.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=Fs){this.sendFrame(ye(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${Fs}). 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 Ot({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:xt(Rt())})}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!==He.WebSocket.OPEN)break;this.send(i,n),await nd(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=xt(Rt());try{let o=ut(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 ve)return ye(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},n);if(e instanceof z)return ye(404,{type:"invalid_request_error",code:"model_not_found",message:r},n);if(e instanceof ce)return ye(503,{type:"upstream_error",code:"session_gone",message:r},n);let o=this.statusOf?.(e)??null;return o?ye(o.status,{type:o.openaiType,message:r},n):ye(500,{type:"upstream_error",message:r},n)}async acquire(){for(;this.active>=Zu;){let{promise:e,resolve:n}=yr();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!==He.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!==He.WebSocket.OPEN||this.ws.send(JSON.stringify(e))}};function ln(t,e,n){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${n}\r
14
+ ${c}\r
15
+ `),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)}k();var Je=require("ws");k();k();var Ns=require("crypto"),Ds="Inference-Id";function Ct(){return(0,Ns.randomUUID)()}function _r(t){return t==="/v1"||t.startsWith("/v1/")}function Fs(t,e){if(!_r(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 $t(t){return`resp_${t}`}function Bs(t){return`chatcmpl-${t}`}function js(t){return`msg_${t}`}function xe(t){return e=>{t.sessionId=e.sessionId,t.catalog=e.identity}}function qs(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,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 Hs(t){return`${JSON.stringify(t)}
16
+ `}var td=256,nd=128;function ve(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,nd)}var nt=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>=td?"(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}}}},rd=256;function od(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 rt=class extends Error{},pt=class{conversations=new Map;thread(e,n){let r=od(n);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new rt(`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<=rd)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},Gs=new WeakMap;function Mt(t){let e=Gs.get(t);return e||(e=new pt,Gs.set(t,e)),e}function ft(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function Me(t){return t.find(e=>e.type==="response.completed")?.response??null}function mt(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 We=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}))}},sd=["<tool_call>","<function="];function id(t){return sd.find(e=>t.includes(e))}function ot(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 st(t,e,n){if(!(!t||e>0))return id(n)}function gt(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 ad(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 vr(t,e){let n=ad(t);return n.length>0?n:e?[{role:"assistant",content:e}]:[]}var Ee=class extends Error{constructor(n,r,o=400,s){super(n);this.code=r;this.status=o;this.param=s}code;status;param};function ht(t,e){try{return t.thread(e.previous_response_id,e.input)}catch(n){throw n instanceof rt?new Ee(n.message,"previous_response_not_found",400,"previous_response_id"):new Ee(String(n instanceof Error?n.message:n),"invalid_input")}}function Vs(t){let e=t.text;if(e!==null&&typeof e=="object"){let n=e.format;if(n!==void 0)return n}}async function br(t,e,n,r,o){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new Ee("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}:{},...Vs(e)!==void 0?{response_format:Vs(e)}:{}},o)}async function Lt(t){let{body:e,clients:n,store:r,stats:o}=t,s=ht(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=ve(e);o.model(d);let l=Array.isArray(e.tools)&&e.tools.length>0,p=t.turn,y=await br(n,e,s,!0,p?xe(p):void 0);return{responseId:i,events:cd({events:y,conversation:s,responseId:i,modelKey:d,requestHadTools:l,remember:a,persist:u,store:r,stats:o,turn:t.turn})}}function ud(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?ld(o[i++],u):u),...dd(r,n)]}function dd(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 ld(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*cd(t){let{events:e,conversation:n,responseId:r,modelKey:o,requestHadTools:s,remember:i,persist:a,store:u,stats:d}=t,l=[],p=H=>H.response&&typeof H.response=="object"?{...H,response:{...H.response,id:r}}:H,y=`item_${r.slice(5)}`,c=!1,m=!1,w=0;function*R(){c||(c=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let h=[],v=(H,I)=>{h.push({output_index:H,item:I})},b=!1,$=0,S="",E=0,x="";function*L(){if(!b)return;b=!1,yield{type:"response.reasoning_text.done",item_id:S,output_index:$,content_index:0,text:x};let H={id:S,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:x}],status:"completed"};yield{type:"response.output_item.done",output_index:$,item:H},v($,H),x=""}let B=!1,q=0,V="",de=0,fe="",se="";function*ie(H){if(!B)return;B=!1,yield{type:"response.output_text.done",item_id:V,output_index:q,content_index:0,text:fe},yield{type:"response.content_part.done",item_id:V,output_index:q,content_index:0,part:{type:"output_text",text:fe}};let I={id:V,type:"message",role:"assistant",status:H,content:[{type:"output_text",text:fe}]};yield{type:"response.output_item.done",output_index:q,item:I},v(q,I),fe=""}let ke=new We,Z=null,Se=-1;function*Ae(H){if(!Z)return;let I=Z;Z=null,yield{type:"response.function_call_arguments.done",item_id:I.fcId,output_index:I.outputIndex,arguments:I.args};let Y={id:I.fcId,type:"function_call",call_id:I.callId??I.fcId,name:I.name??"",arguments:I.args,status:H};yield{type:"response.output_item.done",output_index:I.outputIndex,item:Y},v(I.outputIndex,Y)}let Ke=!1;for await(let H of d.track(e,o,t.turn)){l.push(H);let I=String(H.type??"");if(I==="response.created"||I==="response.in_progress"){c=!0,yield p(H);continue}if(I==="response.output_item.added"){m=!0,yield p(H);continue}if(I==="response.reasoning_text.delta"&&!m){yield*R(),b||(b=!0,$=w++,E+=1,S=E===1?`rs_${r.slice(5)}`:`rs_${r.slice(5)}_${E}`,yield{type:"response.output_item.added",output_index:$,item:{id:S,type:"reasoning",summary:[],content:[],status:"in_progress"}}),x+=String(H.delta??""),yield{type:"response.reasoning_text.delta",item_id:S,output_index:$,content_index:0,delta:H.delta};continue}if(I==="response.output_text.delta"&&!m){yield*R(),yield*L(),B||(B=!0,q=w++,de+=1,V=de===1?y:`${y}_${de}`,yield{type:"response.output_item.added",output_index:q,item:{id:V,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:V,output_index:q,content_index:0,part:{type:"output_text",text:""}}),fe+=String(H.delta??""),se+=String(H.delta??""),yield{type:I,item_id:V,output_index:q,content_index:0,delta:H.delta};continue}let Y=m?null:mt(H);if(Y){if(yield*R(),yield*L(),yield*ie("completed"),ke.add(Y),Y.tool_index!==Se){yield*Ae("completed"),Se=Y.tool_index;let Ie=w++;Z={fcId:`fc_${r.slice(5)}_${Y.tool_index}`,outputIndex:Ie,callId:Y.call_id,name:Y.name,args:""},yield{type:"response.output_item.added",output_index:Ie,item:{id:Z.fcId,type:"function_call",call_id:Z.callId??Z.fcId,name:Z.name??"",arguments:"",status:"in_progress"}}}Z&&(Y.call_id&&(Z.callId=Y.call_id),Y.name&&(Z.name=Y.name),Z.args+=Y.delta,yield{type:"response.function_call_arguments.delta",item_id:Z.fcId,output_index:Z.outputIndex,delta:Y.delta});continue}if(I==="response.completed"&&!m){let Ie=ft(H.response),F=st(s,ke.size+Ie.length,se);if(F){Ke=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:ot(o,F)}};break}if(yield*L(),yield*ie("completed"),yield*Ae("completed"),ke.size===0)for(let Ce of Ie){let De=w++,at=Ce.id??Ce.call_id??`fc_${De}`,_e=Ce.arguments??"",vt={id:at,type:"function_call",call_id:Ce.call_id??at,name:Ce.name??""};yield{type:"response.output_item.added",output_index:De,item:{...vt,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:at,output_index:De,delta:_e},yield{type:"response.function_call_arguments.done",item_id:at,output_index:De,arguments:_e};let Fe={...vt,arguments:_e,status:"completed"};yield{type:"response.output_item.done",output_index:De,item:Fe},v(De,Fe)}let me=ud(h,H.response);yield me.length>0?p({...H,response:{...H.response,output:me}}):p(H);continue}yield p(H)}if(yield*L(),yield*ie("incomplete"),yield*Ae("incomplete"),i&&!Ke){let H=Me(l);u.remember(r,n,vr(H,gt(l,H)),a)}}var Rr="/v1/responses",pd=64*1024*1024,fd=8*1024*1024,md=16,Ws=32,Js=3600*1e3,gd=/^[A-Za-z0-9._-]{1,256}$/;function be(t,e,n){return{type:"error",status:t,...n!==void 0?{stream_id:n}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var hd=be(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 xr(){let t,e;return{promise:new Promise((r,o)=>{t=r,e=o}),resolve:t,reject:e}}async function yd(t){for(;t.bufferedAmount>fd&&t.readyState===Je.WebSocket.OPEN;){let{promise:e,resolve:n}=xr();setTimeout(n,5),await e}}var kr=class extends pt{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 rt))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)}},Sr=class{constructor(e,n,r,o){this.ws=e;this.clients=n;this.stats=r;this.statusOf=o;this.store=new kr(Mt(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(be(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${Js/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},Js).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(be(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(be(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(be(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(be(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(be(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(be(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"||!gd.test(o.stream_id)){this.sendFrame(hd);return}if(s=o.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=Ws){this.sendFrame(be(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${Ws}). 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 Lt({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:$t(Ct())})}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!==Je.WebSocket.OPEN)break;this.send(i,n),await yd(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=$t(Ct());try{let o=ht(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 Ee)return be(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},n);if(e instanceof z)return be(404,{type:"invalid_request_error",code:"model_not_found",message:r},n);if(e instanceof pe)return be(503,{type:"upstream_error",code:"session_gone",message:r},n);let o=this.statusOf?.(e)??null;return o?be(o.status,{type:o.openaiType,message:r},n):be(500,{type:"upstream_error",message:r},n)}async acquire(){for(;this.active>=md;){let{promise:e,resolve:n}=xr();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!==Je.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!==Je.WebSocket.OPEN||this.ws.send(JSON.stringify(e))}};function mn(t,e,n){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${n}\r
17
17
  `;t.end(`HTTP/1.1 ${e} ${r}\r
18
18
  Connection: close\r
19
19
  Content-Type: text/plain; charset=utf-8\r
20
20
  Content-Length: ${Buffer.byteLength(o)}\r
21
21
  \r
22
- ${o}`)}function wr(t,e){let n=new He.WebSocketServer({noServer:!0,maxPayload:Xu}),r=new Ye,o=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,u=!1,d=(c,l,p)=>{let m;try{m=new URL(c.url??"/","http://localhost")}catch{ln(l,400,"malformed upgrade URL");return}if(l.on("error",()=>l.destroy()),l.on("close",()=>i.delete(l)),m.pathname!==gr){i.add(l);return}s.add(l);let b=!1;(async()=>{if(e.apiKey&&(c.headers.authorization??"")!==`Bearer ${e.apiKey}`){ln(l,401,"invalid local proxy api key");return}let E;try{E=await a(c)}catch(w){let g=e.statusOf?.(w)??null;ln(l,g?.status??500,"upstream resolver rejected the connection");return}u||l.destroyed||(b=!0,n.handleUpgrade(c,l,p,w=>{s.delete(l),o.add(w),w.on("close",()=>o.delete(w));try{new hr(w,E,r,e.statusOf)}catch{o.delete(w),w.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!b&&!l.destroyed&&ln(l,500,"responses websocket upgrade failed")}).finally(()=>s.delete(l))};return t.on("upgrade",d),{stats:r,close:()=>{let{promise:c,resolve:l}=yr();if(u)return l(),c;u=!0,t.off("upgrade",d);for(let m of s)m.destroy();s.clear();for(let m of i)m.destroy();i.clear();for(let m of o)m.close(1001,"server shutting down");let p=setTimeout(()=>{for(let m of o)m.readyState!==He.WebSocket.CLOSED&&m.terminate()},2e3);return p.unref?.(),n.close(()=>{clearTimeout(p),l()}),c}}}S();S();var Ws=require("@urun-sh/core");S();var js=require("@urun-sh/core"),rd={no_capacity:429,queue_timeout:429,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502},od={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 _r(t,e){let n=od[t.code];return e?`${n} (${e})`:n}function cn(t){if(t==null)return null;let e=rd[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 pn(t){if(t.kind==="create-failed")return cn(t.status.error?.code??null);if(t.kind!=="never-live")return null;let e=(t.status.elapsedMs??0)>=js.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return cn(e?"cold_start_exceeded":"queue_timeout")}var Js=require("ws");S();S();var Hs=require("crypto");function N(t){return`${t}_${(0,Hs.randomUUID)().replace(/-/g,"").slice(0,24)}`}var fn=4*1024*1024,vr=8*1024*1024,mn=512,hn=20*1024*1024;function se(t,e,n){return{type:"invalid_request_error",code:t,message:e,param:n??null}}function It(t,e){return{type:"server_error",code:t,message:e}}function Q(t){return{type:"error",event_id:N("event"),error:t}}function br(){return{outputModalities:["text"],turnDetection:null,inputAudioTranscription:!1}}var B=class extends Error{constructor(n,r,o="invalid_value"){super(n);this.param=r;this.code=o;this.name="SessionUpdateError"}param;code},sd={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},id={format:!0,transcription:!0,turn_detection:!0,noise_reduction:!0},ad={format:!0,voice:!0,speed:!0};function qs(t,e){let n=t;if(!n||typeof n!="object"||n.type!=="audio/pcm"||n.rate!==void 0&&n.rate!==24e3)throw new B(`${e} must be {"type":"audio/pcm","rate":24000} \u2014 the session audio lane is 24 kHz mono PCM16 only`,e)}function At(t,e,n){if(!t||typeof t!="object"||Array.isArray(t))throw new B("session must be an object","session","invalid_request");let r=t,o=Object.keys(r).filter(i=>!(i in sd));if(o.length>0)throw new B(`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"model":if(a!==null&&typeof a!="string")throw new B("model must be a string or null","model");s.model=a??void 0;break;case"instructions":if(a!==null&&typeof a!="string")throw new B("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 B("modalities must be a non-empty array","modalities");let u=[];for(let d of a){if(d!=="text"&&d!=="audio")throw new B(`modality ${JSON.stringify(d)} is not "text" or "audio"`,"modalities");if(d==="audio"&&!n.voice)throw new B('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 B("voice must be a string or null","voice");if(!n.voice)throw new B("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 B(`${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 B("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 B(`${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 B("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 B("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 B("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 B("turn_detection must be an object or null","turn_detection");let d=String(u.type??"");if(d!=="server_vad"&&d!=="semantic_vad")throw new B(`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 B("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 B("input_audio_transcription must be an object or null","input_audio_transcription");for(let u of Object.keys(a))if(!["model","language","languages","prompt"].includes(u))throw new B(`input_audio_transcription field not honored by this backend: ${u} (transcription comes from the voice engine itself)`,`input_audio_transcription.${u}`,"unsupported_field");s.inputAudioTranscription=!0;break}case"input_audio_noise_reduction":if(a!==null)throw new B("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 B("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 B("audio.input must be an object","audio.input");for(let c of Object.keys(d))if(!(c in id))throw new B(`audio.input field not supported by this backend: ${c}`,`audio.input.${c}`,"unsupported_field");if(d.format!==void 0&&qs(d.format,"audio.input.format"),d.transcription!==null&&d.transcription!==void 0){let c=At({input_audio_transcription:d.transcription},s,n);s.inputAudioTranscription=c.inputAudioTranscription}if(d.turn_detection!==null&&d.turn_detection!==void 0){let c=At({turn_detection:d.turn_detection},s,n);s.turnDetection=c.turnDetection}if(d.noise_reduction!==null&&d.noise_reduction!==void 0)throw new B("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 B("audio.output must be an object","audio.output");for(let c of Object.keys(d))if(!(c in ad))throw new B(`audio.output field not supported by this backend: ${c}`,`audio.output.${c}`,"unsupported_field");if(d.format!==void 0&&qs(d.format,"audio.output.format"),d.speed!==void 0)throw new B("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 B("audio.output.voice must be a string or null","audio.output.voice");if(!n.voice)throw new B("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 gn(t,e){return{id:e,object:"realtime.session",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 kr(t,e){let n=t;if(!n||typeof n!="object")throw new B("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 B("function_call_output item requires string call_id and output","item.call_id","invalid_request");return{id:typeof n.id=="string"?n.id:N("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 B('message item role must be "user", "assistant" or "system"',"item.role","invalid_request");if(!Array.isArray(n.content))throw new B("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 B("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 B(`${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 B("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 B("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 B(`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:N("item"),type:"message",role:r,content:o}}throw new B(`item type ${String(n.type)} is not supported \u2014 message and function_call_output items only`,"item.type","invalid_request")}function Sr(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 ud=1001,dd=1011,Gs=t=>t.slice(0,120),ld=/^[A-Za-z0-9+/=]*$/,cd=2;function pd(t){let e=Buffer.from(t,"base64");for(let n=0;n+1<e.byteLength;n+=2)if(Math.abs(e.readInt16LE(n))>cd)return!1;return!0}var Tt=class{id=N("conn");ws;binding;onClosed;config=br();sessionId=N("sess");conversationId=N("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;turnTranscript={open:!1,text:""};lastCommittedTranscript=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.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:N("event"),session:gn(this.config,this.sessionId)}),this.send({type:"conversation.created",event_id:N("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;(async()=>{try{for await(let r of n()){if(this.closed||e.signal.aborted)return;this.onTurnEvent(r)}}catch(r){!this.closed&&!e.signal.aborted&&this.send(Q(It("turn_lane_fault",`the backend turn/transcript lane failed: ${r instanceof Error?r.message:String(r)}`)))}})()}onTurnEvent(e){switch(e.kind){case"delta":case"final":{let n=typeof e.text=="string"?e.text:"";if(n.length===0)return;this.turnTranscript.open=!0,this.turnTranscript.text+=n,this.config.inputAudioTranscription&&this.send({type:"conversation.item.input_audio_transcription.delta",event_id:N("event"),item_id:"input_audio_turn",content_index:0,delta:n});return}case"turn_start":{this.turnTranscript={open:!0,text:this.turnTranscript.text},this.send({type:"input_audio_buffer.speech_started",event_id:N("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.turnTranscript.text.trim(),r=n.length>0?n:null;this.turnTranscript={open:!1,text:""},this.lastCommittedTranscript=r,this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let o=N("item"),s=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem({id:o,type:"message",role:"user",content:[{type:"input_audio",...r!==null?{transcript:r}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:N("event"),previous_item_id:s,item_id:o}),this.send({type:"conversation.item.created",event_id:N("event"),previous_item_id:s,item:this.itemToProtocol(this.items[this.items.length-1])}),this.send({type:"input_audio_buffer.speech_stopped",event_id:N("event"),item_id:o,audio_end_ms:typeof e.startMs=="number"?e.startMs:0}),this.config.inputAudioTranscription&&r!==null&&this.send({type:"conversation.item.input_audio_transcription.completed",event_id:N("event"),item_id:o,content_index:0,transcript:r}),this.config.turnDetection?.createResponse&&!this.inFlight&&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(se("decode_error","frame is not decodable text"),"invalid_frame");return}if(Buffer.byteLength(n,"utf8")>fn){this.fail(se("frame_too_large",`frame exceeds the ${fn}-byte limit`),"invalid_frame");return}let r;try{r=JSON.parse(n)}catch{this.fail(se("invalid_json","frame is not valid JSON"),"invalid_frame");return}if(typeof r.type!="string"||r.type.length===0){this.fail(se("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(Q({type:"invalid_request_error",code:s.code??"invalid_value",message:s.message,param:s.param??null}));return}this.fail(It(null,o instanceof Error?o.message:String(o)),"handler_fault")}}async handle(e){switch(e.type){case"session.update":this.config=At(e.session,this.config,{voice:this.binding.voice,turns:this.binding.turns!==void 0}),this.send({type:"session.updated",event_id:N("event"),session:gn(this.config,this.sessionId)});return;case"conversation.item.create":{let n=kr(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:N("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(Q(se("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:N("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(Q(se("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.send({type:"conversation.item.retrieved",event_id:N("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(Q(se("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:N("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(Q(se("invalid_audio",'input_audio_buffer.append requires base64 "audio"',"audio")));return}if(!this.ensureVoice())return;if(!ld.test(e.audio)){this.send(Q(se("invalid_audio","input_audio_buffer.append audio must be base64","audio")));return}let n=Buffer.byteLength(e.audio,"base64");if(this.pendingAudioBytes.length+n>hn){this.send(Q(se("buffer_full",`uncommitted input audio exceeds the ${hn}-byte bound \u2014 commit or clear first`,"audio")));return}if(this.inFlight){this.binding.audioSession.audio.appendInputAudio(e.audio);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.send({type:"input_audio_buffer.cleared",event_id:N("event")});return}case"input_audio_buffer.commit":{if(!this.ensureVoice())return;if(this.pendingAudioTotalBytes===0&&this.takeTurnTranscript()===null){this.send(Q(se("empty_buffer","input_audio_buffer.commit with no buffered audio","audio")));return}this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let n=this.takeTurnTranscript();n===null&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let r=N("item"),o=this.items.length>0?this.items[this.items.length-1].id:null;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:N("event"),previous_item_id:o,item_id:r}),this.send({type:"conversation.item.created",event_id:N("event"),previous_item_id:o,item:this.itemToProtocol(this.items[this.items.length-1])});return}case"output_audio_buffer.clear":{this.send(Q(se("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":this.startResponse(e.response??{});return;case"response.cancel":this.cancelResponse();return;default:this.send(Q(se("unknown_event",`event type ${e.type} is not supported by this adapter`)))}}ensureVoice(){return!this.binding.voice||!this.binding.audioSession?(this.send(Q(se("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(Q({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(Q(se("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(E=>E!=="text"&&E!=="audio")){this.send(Q(se("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(Q(se("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=N("req"),o=`resp_${r}`;this.activeResponseId=o;let s=this.items.length,{input:i,sawAudioOnlyItem:a}=Sr(this.items),u=typeof n.instructions=="string"?n.instructions:this.config.instructions,d=typeof n.temperature=="number"?n.temperature:this.config.temperature,c=n.max_output_tokens??this.config.maxOutputTokens,l=c==="inf"||c===void 0?void 0:c,p=n.tools!==void 0?n.tools:this.config.tools,m=n.tool_choice!==void 0?n.tool_choice:this.config.toolChoice,b={input:i,stream:!0,...this.config.model?{model:this.config.model}:{},...u!==void 0?{instructions:u}:{},...d!==void 0?{temperature:d}:{},...l!==void 0?{max_output_tokens:l}:{},...p!==void 0?{tools:p}:{},...m!==void 0?{tool_choice:m}:{}};for(let E of this.frozenAudio.splice(0))this.binding.audioSession?.audio.appendInputAudio(E);this.inFlight=(async()=>{let E=[],w=C=>{this.responseFlushed?this.send(C):E.push(C)},g=()=>{if(!this.responseFlushed){this.send({type:"response.created",event_id:N("event"),response:this.responseObject(o,"in_progress")}),this.send({type:"response.in_progress",event_id:N("event"),response:this.responseObject(o,"in_progress")});for(let C of E)this.send(C);E.length=0,this.responseFlushed=!0}},v={messageOpen:!1,messageText:"",tools:new Map,nextOutputIndex:0},_=null;try{a&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let C;try{C=await this.upstreamStream(b,r)}catch(R){this.emitUpstreamFault(R,o);return}try{for await(let R of C){if(this.cancelled)break;let O=R;if(O.type==="error"){let k=this.upstreamErrorBody(O);this.responseFlushed?this.send(Q(k)):this.send({type:"response.failed",event_id:N("event"),response:this.failedResponse(o,k)});return}if(O.type==="response.output_text.delta"&&typeof O.delta=="string"){g(),v.messageOpen||(this.beginMessageItem(o,v.nextOutputIndex++),v.messageOpen=!0),w({type:"response.output_text.delta",event_id:N("event"),item_id:`${o}_msg_0`,response_id:o,output_index:0,content_index:0,delta:O.delta}),v.messageText+=O.delta;continue}if(O.type==="response.function_call_arguments.delta"){g();let k=typeof O.delta=="string"?O.delta:"",$=typeof O.call_id=="string"&&O.call_id.length>0?O.call_id:`${o}_fc_${v.nextOutputIndex}`,j=v.tools.get($);if(!j){let W=typeof O.name=="string"&&O.name.length>0?O.name:void 0;j={outputIndex:v.nextOutputIndex++,name:W,args:""},v.tools.set($,j),w({type:"response.output_item.added",event_id:N("event"),response_id:o,output_index:j.outputIndex,item:{id:$,object:"realtime.item",type:"function_call",call_id:$,name:W??"",arguments:"",status:"in_progress"}})}typeof O.name=="string"&&O.name.length>0&&(j.name=O.name),j.args+=k,w({type:"response.function_call_arguments.delta",event_id:N("event"),item_id:$,response_id:o,output_index:j.outputIndex,call_id:$,name:j.name??"",delta:k});continue}if(O.type==="response.completed"){_=O.response??null;continue}}}catch(R){this.emitUpstreamFault(R,o);return}if(this.cancelled)return;this.closeOpenItems(o,v,s),g(),this.send({type:"response.done",event_id:N("event"),response:this.doneResponse(o,s,_)})}finally{this.inFlight=null,this.activeResponseId=null}})()}onOutputAudioFrame(e,n){if(this.cancelled)return;if(this.activeResponseId!==e){if(pd(n))return;!this.outOfTurnAudioWarned&&!this.closed&&(this.outOfTurnAudioWarned=!0,this.send(Q({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:N("event"),response:this.responseObject(e,"in_progress")}),this.send({type:"response.in_progress",event_id:N("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:N("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:N("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:N("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:N("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:N("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:N("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:N("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:N("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:N("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:N("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:N("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}}emitUpstreamFault(e,n){if(this.cancelled)return;let r=It(null,e instanceof Error?e.message:String(e));this.responseFlushed?this.send(Q(r)):this.send({type:"response.failed",event_id:N("event"),response:this.failedResponse(n,r)})}cancelResponse(){let e=this.inFlight;if(!e){this.send(Q(se("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:N("event"),response:this.responseObject(n,"cancelled")})})}pushItem(e){this.items.push(e),this.items.length>mn&&(this.items.shift(),this.send(Q({type:"invalid_request_error",code:"transcript_evicted",message:`the conversation transcript exceeded ${mn} 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>vr){this.ws.close(ud,Gs("outbound buffer exceeded")),this.teardown("outbound buffer exceeded");return}this.ws.send(JSON.stringify(e))}}fail(e,n){this.send(Q(e)),this.ws.close(dd,Gs(n)),this.teardown(n)}teardown(e){if(!this.closed){this.closed=!0,this.turnLaneAbort?.abort(),this.turnLaneAbort=null,this.releaseVoiceBinding(),this.audioUnsubscribe?.(),this.audioUnsubscribe=null,this.binding.release&&Promise.resolve().then(()=>this.binding.release?.()).catch(n=>Vs(this.id,`binding release failed: ${n instanceof Error?n.message:String(n)}`));try{this.ws.removeAllListeners("message")}catch{}this.onClosed?.(this,{reason:e}),Vs(this.id,e)}}},Rr=[];function Vs(t,e){Rr.push({id:t,reason:e}),Rr.length>256&&Rr.shift()}var Pt="/v1/realtime",fd=t=>t.slice(0,120);function md(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
+ ${o}`)}function Er(t,e){let n=new Je.WebSocketServer({noServer:!0,maxPayload:pd}),r=new nt,o=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,u=!1,d=(l,p,y)=>{let c;try{c=new URL(l.url??"/","http://localhost")}catch{mn(p,400,"malformed upgrade URL");return}if(p.on("error",()=>p.destroy()),p.on("close",()=>i.delete(p)),c.pathname!==Rr){i.add(p);return}s.add(p);let m=!1;(async()=>{if(e.apiKey&&(l.headers.authorization??"")!==`Bearer ${e.apiKey}`){mn(p,401,"invalid local proxy api key");return}let w;try{w=await a(l)}catch(R){let h=e.statusOf?.(R)??null;mn(p,h?.status??500,"upstream resolver rejected the connection");return}u||p.destroyed||(m=!0,n.handleUpgrade(l,p,y,R=>{s.delete(p),o.add(R),R.on("close",()=>o.delete(R));try{new Sr(R,w,r,e.statusOf)}catch{o.delete(R),R.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!m&&!p.destroyed&&mn(p,500,"responses websocket upgrade failed")}).finally(()=>s.delete(p))};return t.on("upgrade",d),{stats:r,close:()=>{let{promise:l,resolve:p}=xr();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 y=setTimeout(()=>{for(let c of o)c.readyState!==Je.WebSocket.CLOSED&&c.terminate()},2e3);return y.unref?.(),n.close(()=>{clearTimeout(y),p()}),l}}}k();k();var ei=require("@urun-sh/core");k();var Ks=require("@urun-sh/core"),wd={no_capacity:429,queue_timeout:429,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502},_d={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 Or(t,e){let n=_d[t.code];return e?`${n} (${e})`:n}function gn(t){if(t==null)return null;let e=wd[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 hn(t){if(t.kind==="create-failed")return gn(t.status.error?.code??null);if(t.kind!=="never-live")return null;let e=(t.status.elapsedMs??0)>=Ks.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return gn(e?"cold_start_exceeded":"queue_timeout")}var ti=require("ws");k();var Zs=require("@urun-sh/core");k();var Ys=require("crypto");function N(t){return`${t}_${(0,Ys.randomUUID)().replace(/-/g,"").slice(0,24)}`}var yn=4*1024*1024,Ar=8*1024*1024,wn=512,_n=20*1024*1024;function ae(t,e,n){return{type:"invalid_request_error",code:t,message:e,param:n??null}}function yt(t,e){return{type:"server_error",code:t,message:e}}function Q(t){return{type:"error",event_id:N("event"),error:t}}function Ir(){return{outputModalities:["text"],turnDetection:null,inputAudioTranscription:!1}}var j=class extends Error{constructor(n,r,o="invalid_value"){super(n);this.param=r;this.code=o;this.name="SessionUpdateError"}param;code},vd={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},bd={format:!0,transcription:!0,turn_detection:!0,noise_reduction:!0},kd={format:!0,voice:!0,speed:!0};function zs(t,e){let n=t;if(!n||typeof n!="object"||n.type!=="audio/pcm"||n.rate!==void 0&&n.rate!==24e3)throw new j(`${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 j("session must be an object","session","invalid_request");let r=t,o=Object.keys(r).filter(i=>!(i in vd));if(o.length>0)throw new j(`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"model":if(a!==null&&typeof a!="string")throw new j("model must be a string or null","model");s.model=a??void 0;break;case"instructions":if(a!==null&&typeof a!="string")throw new j("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 j("modalities must be a non-empty array","modalities");let u=[];for(let d of a){if(d!=="text"&&d!=="audio")throw new j(`modality ${JSON.stringify(d)} is not "text" or "audio"`,"modalities");if(d==="audio"&&!n.voice)throw new j('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 j("voice must be a string or null","voice");if(!n.voice)throw new j("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 j(`${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 j("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 j(`${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 j("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 j("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 j("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 j("turn_detection must be an object or null","turn_detection");let d=String(u.type??"");if(d!=="server_vad"&&d!=="semantic_vad")throw new j(`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 j("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 j("input_audio_transcription must be an object or null","input_audio_transcription");for(let u of Object.keys(a))if(!["model","language","languages","prompt"].includes(u))throw new j(`input_audio_transcription field not honored by this backend: ${u} (transcription comes from the voice engine itself)`,`input_audio_transcription.${u}`,"unsupported_field");s.inputAudioTranscription=!0;break}case"input_audio_noise_reduction":if(a!==null)throw new j("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 j("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 j("audio.input must be an object","audio.input");for(let l of Object.keys(d))if(!(l in bd))throw new j(`audio.input field not supported by this backend: ${l}`,`audio.input.${l}`,"unsupported_field");if(d.format!==void 0&&zs(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 j("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 j("audio.output must be an object","audio.output");for(let l of Object.keys(d))if(!(l in kd))throw new j(`audio.output field not supported by this backend: ${l}`,`audio.output.${l}`,"unsupported_field");if(d.format!==void 0&&zs(d.format,"audio.output.format"),d.speed!==void 0)throw new j("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 j("audio.output.voice must be a string or null","audio.output.voice");if(!n.voice)throw new j("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 vn(t,e){return{id:e,object:"realtime.session",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 Tr(t,e){let n=t;if(!n||typeof n!="object")throw new j("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 j("function_call_output item requires string call_id and output","item.call_id","invalid_request");return{id:typeof n.id=="string"?n.id:N("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 j('message item role must be "user", "assistant" or "system"',"item.role","invalid_request");if(!Array.isArray(n.content))throw new j("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 j("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 j(`${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 j("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 j("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 j(`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:N("item"),type:"message",role:r,content:o}}throw new j(`item type ${String(n.type)} is not supported \u2014 message and function_call_output items only`,"item.type","invalid_request")}function Pr(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 Sd=1001,Rd=1011,Xs=t=>t.slice(0,120),xd=/^[A-Za-z0-9+/=]*$/,Ed=2;function Od(t){let e=Buffer.from(t,"base64");for(let n=0;n+1<e.byteLength;n+=2)if(Math.abs(e.readInt16LE(n))>Ed)return!1;return!0}var Nt=class{id=N("conn");ws;binding;onClosed;config=Ir();sessionId=N("sess");conversationId=N("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;turnTranscript={open:!1,text:""};lastCommittedTranscript=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.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:N("event"),session:vn(this.config,this.sessionId)}),this.send({type:"conversation.created",event_id:N("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;(async()=>{try{for await(let r of n()){if(this.closed||e.signal.aborted)return;this.onTurnEvent(r)}}catch(r){!this.closed&&!e.signal.aborted&&this.send(Q(yt("turn_lane_fault",`the backend turn/transcript lane failed: ${r instanceof Error?r.message:String(r)}`)))}})()}onTurnEvent(e){switch(e.kind){case"delta":case"final":{let n=typeof e.text=="string"?e.text:"";if(n.length===0)return;this.turnTranscript.open=!0,this.turnTranscript.text+=n,this.config.inputAudioTranscription&&this.send({type:"conversation.item.input_audio_transcription.delta",event_id:N("event"),item_id:"input_audio_turn",content_index:0,delta:n});return}case"turn_start":{this.turnTranscript={open:!0,text:this.turnTranscript.text},this.send({type:"input_audio_buffer.speech_started",event_id:N("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.turnTranscript.text.trim(),r=n.length>0?n:null;this.turnTranscript={open:!1,text:""},this.lastCommittedTranscript=r,this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let o=N("item"),s=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem({id:o,type:"message",role:"user",content:[{type:"input_audio",...r!==null?{transcript:r}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:N("event"),previous_item_id:s,item_id:o}),this.send({type:"conversation.item.created",event_id:N("event"),previous_item_id:s,item:this.itemToProtocol(this.items[this.items.length-1])}),this.send({type:"input_audio_buffer.speech_stopped",event_id:N("event"),item_id:o,audio_end_ms:typeof e.startMs=="number"?e.startMs:0}),this.config.inputAudioTranscription&&r!==null&&this.send({type:"conversation.item.input_audio_transcription.completed",event_id:N("event"),item_id:o,content_index:0,transcript:r}),this.config.turnDetection?.createResponse&&!this.inFlight&&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(ae("decode_error","frame is not decodable text"),"invalid_frame");return}if(Buffer.byteLength(n,"utf8")>yn){this.fail(ae("frame_too_large",`frame exceeds the ${yn}-byte limit`),"invalid_frame");return}let r;try{r=JSON.parse(n)}catch{this.fail(ae("invalid_json","frame is not valid JSON"),"invalid_frame");return}if(typeof r.type!="string"||r.type.length===0){this.fail(ae("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(Q({type:"invalid_request_error",code:s.code??"invalid_value",message:s.message,param:s.param??null}));return}this.fail(yt(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}),this.send({type:"session.updated",event_id:N("event"),session:vn(this.config,this.sessionId)});return;case"conversation.item.create":{let n=Tr(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:N("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(Q(ae("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:N("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(Q(ae("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.send({type:"conversation.item.retrieved",event_id:N("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(Q(ae("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:N("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(Q(ae("invalid_audio",'input_audio_buffer.append requires base64 "audio"',"audio")));return}if(!this.ensureVoice())return;if(!xd.test(e.audio)){this.send(Q(ae("invalid_audio","input_audio_buffer.append audio must be base64","audio")));return}let n=Buffer.byteLength(e.audio,"base64");if(this.pendingAudioBytes.length+n>_n){this.send(Q(ae("buffer_full",`uncommitted input audio exceeds the ${_n}-byte bound \u2014 commit or clear first`,"audio")));return}if(this.inFlight){this.binding.audioSession.audio.appendInputAudio(e.audio);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.send({type:"input_audio_buffer.cleared",event_id:N("event")});return}case"input_audio_buffer.commit":{if(!this.ensureVoice())return;if(this.pendingAudioTotalBytes===0&&this.takeTurnTranscript()===null){this.send(Q(ae("empty_buffer","input_audio_buffer.commit with no buffered audio","audio")));return}this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let n=this.takeTurnTranscript();n===null&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let r=N("item"),o=this.items.length>0?this.items[this.items.length-1].id:null;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:N("event"),previous_item_id:o,item_id:r}),this.send({type:"conversation.item.created",event_id:N("event"),previous_item_id:o,item:this.itemToProtocol(this.items[this.items.length-1])});return}case"output_audio_buffer.clear":{this.send(Q(ae("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":this.startResponse(e.response??{});return;case"response.cancel":this.cancelResponse();return;default:this.send(Q(ae("unknown_event",`event type ${e.type} is not supported by this adapter`)))}}ensureVoice(){return!this.binding.voice||!this.binding.audioSession?(this.send(Q(ae("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(Q({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(Q(ae("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(w=>w!=="text"&&w!=="audio")){this.send(Q(ae("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(Q(ae("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=N("req"),o=`resp_${r}`;this.activeResponseId=o;let s=this.items.length,{input:i,sawAudioOnlyItem:a}=Pr(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,y=n.tools!==void 0?n.tools:this.config.tools,c=n.tool_choice!==void 0?n.tool_choice:this.config.toolChoice,m={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}:{},...y!==void 0?{tools:y}:{},...c!==void 0?{tool_choice:c}:{}};for(let w of this.frozenAudio.splice(0))this.binding.audioSession?.audio.appendInputAudio(w);this.inFlight=(async()=>{let w=[],R=$=>{this.responseFlushed?this.send($):w.push($)},h=()=>{if(!this.responseFlushed){this.send({type:"response.created",event_id:N("event"),response:this.responseObject(o,"in_progress")}),this.send({type:"response.in_progress",event_id:N("event"),response:this.responseObject(o,"in_progress")});for(let $ of w)this.send($);w.length=0,this.responseFlushed=!0}},v={messageOpen:!1,messageText:"",tools:new Map,nextOutputIndex:0},b=null;try{a&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let $;try{$=await this.upstreamStream(m,r)}catch(S){this.upstreamTurnFault(S,o);return}try{for await(let S of $){if(this.cancelled)break;let E=S;if(E.type==="error"){let x=this.upstreamErrorBody(E);this.responseFlushed?this.send(Q(x)):this.send({type:"response.failed",event_id:N("event"),response:this.failedResponse(o,x)});return}if(E.type==="response.output_text.delta"&&typeof E.delta=="string"){h(),v.messageOpen||(this.beginMessageItem(o,v.nextOutputIndex++),v.messageOpen=!0),R({type:"response.output_text.delta",event_id:N("event"),item_id:`${o}_msg_0`,response_id:o,output_index:0,content_index:0,delta:E.delta}),v.messageText+=E.delta;continue}if(E.type==="response.function_call_arguments.delta"){h();let x=typeof E.delta=="string"?E.delta:"",L=typeof E.call_id=="string"&&E.call_id.length>0?E.call_id:`${o}_fc_${v.nextOutputIndex}`,B=v.tools.get(L);if(!B){let q=typeof E.name=="string"&&E.name.length>0?E.name:void 0;B={outputIndex:v.nextOutputIndex++,name:q,args:""},v.tools.set(L,B),R({type:"response.output_item.added",event_id:N("event"),response_id:o,output_index:B.outputIndex,item:{id:L,object:"realtime.item",type:"function_call",call_id:L,name:q??"",arguments:"",status:"in_progress"}})}typeof E.name=="string"&&E.name.length>0&&(B.name=E.name),B.args+=x,R({type:"response.function_call_arguments.delta",event_id:N("event"),item_id:L,response_id:o,output_index:B.outputIndex,call_id:L,name:B.name??"",delta:x});continue}if(E.type==="response.completed"){b=E.response??null;continue}}}catch(S){this.upstreamTurnFault(S,o);return}if(this.cancelled)return;this.closeOpenItems(o,v,s),h(),this.send({type:"response.done",event_id:N("event"),response:this.doneResponse(o,s,b)})}finally{this.inFlight=null,this.activeResponseId=null}})()}onOutputAudioFrame(e,n){if(this.cancelled)return;if(this.activeResponseId!==e){if(Od(n))return;!this.outOfTurnAudioWarned&&!this.closed&&(this.outOfTurnAudioWarned=!0,this.send(Q({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:N("event"),response:this.responseObject(e,"in_progress")}),this.send({type:"response.in_progress",event_id:N("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:N("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:N("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:N("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:N("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:N("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:N("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:N("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:N("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:N("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:N("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:N("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(yt("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 pe){this.failSessionGone(e);return}if(!this.cancelled){if(!this.responseFlushed&&!(e instanceof Zs.SessionFailedError&&(e.kind==="never-live"||e.kind==="create-failed"))){this.failSessionGone(e);return}this.emitUpstreamFault(e,n)}}emitUpstreamFault(e,n){let r=yt(null,e instanceof Error?e.message:String(e));this.responseFlushed?this.send(Q(r)):this.send({type:"response.failed",event_id:N("event"),response:this.failedResponse(n,r)})}cancelResponse(){let e=this.inFlight;if(!e){this.send(Q(ae("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:N("event"),response:this.responseObject(n,"cancelled")})})}pushItem(e){this.items.push(e),this.items.length>wn&&(this.items.shift(),this.send(Q({type:"invalid_request_error",code:"transcript_evicted",message:`the conversation transcript exceeded ${wn} 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>Ar){this.ws.close(Sd,Xs("outbound buffer exceeded")),this.teardown("outbound buffer exceeded");return}this.ws.send(JSON.stringify(e))}}fail(e,n){this.send(Q(e)),this.ws.close(Rd,Xs(n)),this.teardown(n)}teardown(e){if(!this.closed){this.closed=!0,this.turnLaneAbort?.abort(),this.turnLaneAbort=null,this.releaseVoiceBinding(),this.audioUnsubscribe?.(),this.audioUnsubscribe=null,this.binding.release&&Promise.resolve().then(()=>this.binding.release?.()).catch(n=>Qs(this.id,`binding release failed: ${n instanceof Error?n.message:String(n)}`));try{this.ws.removeAllListeners("message")}catch{}this.onClosed?.(this,{reason:e}),Qs(this.id,e)}}},Cr=[];function Qs(t,e){Cr.push({id:t,reason:e}),Cr.length>256&&Cr.shift()}var Dt="/v1/realtime",Ad=t=>t.slice(0,120);function Id(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
- ${fd(n)}\r
27
- `),t.destroy()}function hd(t){return`${JSON.stringify({event:"realtime_upgrade",ts:new Date().toISOString(),...t})}
28
- `}function gd(t){Ie(process.stdout,t)}function yd(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function xr(t){return t.audioSession?.audio.sessionId??void 0}function yn(t,e){let n=new Js.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,s,i)=>{if(new URL(o.url??"/","http://localhost").pathname!==Pt)return;r.add(s),s.on("close",()=>{r.delete(s)});let u=N("rtup"),d=yd(o),c=R=>{(e.log??gd)(hd(R))};c({stage:"accept",conn:u,model:d}),s.resume();let l=new AbortController,p=null,m=R=>{p??=R,l.abort(new Error(p))},b=()=>m("client socket closed"),E=()=>m("client socket closed (half-close)"),w=R=>m(`client socket error: ${R.message}`);s.once("close",b),s.once("end",E),s.once("error",w);let g=()=>{s.removeListener("close",b),s.removeListener("end",E),s.removeListener("error",w)},v=R=>{c({stage:"close",conn:u,model:d,...R})},_=R=>{let O=R,k=O.session!=null&&typeof O.session.sendResponseCreate=="function",$=O.clients!=null&&typeof O.clients.createResponse=="function";if(!k&&!$)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((j,W)=>{n.handleUpgrade(o,s,i,F=>{try{new Tt({ws:F,binding:R,onClosed:(de,pe)=>{v({while:"live",reason:pe.reason})}}),c({stage:"admit",conn:u,model:d,...xr(R)===void 0?{}:{session:xr(R)}}),j()}catch(de){W(de)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o,l.signal).then(async R=>{if(l.signal.aborted){g();let O=xr(R);try{await R.release?.()}catch(k){v({while:"queued",...O===void 0?{}:{session:O},reason:p??"client socket closed",release_failed:k instanceof Error?k.message:String(k)});return}v({while:"queued",...O===void 0?{}:{session:O},reason:p??"client socket closed",released:!0}),s.destroy();return}g(),await _(R)}).catch(R=>{let O=R instanceof Ws.SessionFailedError?pn(R):null,k=null;if(R!==null&&typeof R=="object"&&"status"in R){let W=R.status;typeof W=="number"&&(k=W)}let $=k??O?.status??502,j=R instanceof Error?R.message:String(R);v({while:"queued",...l.signal.aborted?{reason:p??"client socket closed"}:{reason:j,status:$}}),s.destroyed||md(s,$,j)}).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()))}}S();S();var zd=require("http");S();var Ks="/v1/images/generations",wn="/v1/images/edits";function zs(t){return t===Ks||t===wn}var Ys="/v1/images/models";async function Xs(t,e,n){if(t){if(t.model=be({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 K=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},wd=16*1024*1024,Qs=64*1024*1024;function Zs(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 Ct(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}:{}}}))}var De=class extends Error{};async function _d(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new De(`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 De(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function ei(t,e,n,r){let o=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},r),s=null;for await(let l of o){if(l.type==="error"){let p=l.error??{};throw new K(502,`image upstream error: ${String(p.message??"upstream reported an error")}`)}if(l.type==="response.completed"){s=l.response??null;break}}if(s===null)throw new K(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 K(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(Zs(d)===null)throw new K(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 ti(t){return t instanceof K?t:t instanceof z?new K(404,t.message):new K(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function ni(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 Ct(r,new K(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 ri(t,e){t&&(t.outputUnits=e.images.length)}function oi(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 vd={model:!0,prompt:!0,response_format:!0,n:!0};async function si(t,e,n){let r=n.maxBodyBytes??Qs;try{if(t.method!=="POST")throw new K(405,`${String(t.method)} is not supported on ${Ks}`);let o;try{o=JSON.parse((await _d(t,r)).toString("utf8")||"{}")}catch(l){throw l instanceof De?new K(413,l.message):new K(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new K(400,"request body must be a JSON object");let s=o;for(let l of Object.keys(s))if(!vd[l])throw new K(400,`request field "${l}" 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.`,l);let i=s.prompt;if(typeof i!="string"||i.trim().length===0)throw new K(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 K(400,"model must be a string","model");await Xs(n.turn,n.clients,a);let u=s.n??1;if(u!==1)throw new K(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 K(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 ni(n.imageModels,a,"generation",e))return;let c=[{type:"input_text",text:i}];try{let l=await ei(n.clients,a,c,n.turn?Te(n.turn):void 0);ri(n.turn,l),oi(e,l)}catch(l){throw ti(l)}}catch(o){if(o instanceof De){Ct(e,new K(413,o.message));return}if(o instanceof K){Ct(e,o);return}throw o}}var bd={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function ii(t,e,n){let r=n.maxBodyBytes??Qs,o=n.maxImagePartBytes??wd;try{if(t.method!=="POST")throw new K(405,`${String(t.method)} is not supported on ${wn}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new K(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i=t[Symbol.asyncIterator](),a=0,u=new ReadableStream({async pull(w){try{let g=await i.next();if(g.done){w.close();return}if(a+=g.value.length,a>r){w.error(new De(`request body exceeds the ${r}-byte limit`));return}w.enqueue(g.value)}catch(g){w.error(g)}},cancel(){i.return?.()}}),d;try{let w=new Headers;for(let[_,C]of Object.entries(t.headers))if(typeof C=="string")w.set(_,C);else if(Array.isArray(C))for(let R of C)w.append(_,R);let g={method:"POST",headers:w,body:u,duplex:"half"};d=await new Request(`https://proxy.internal${t.url??"/"}`,g).formData()}catch(w){throw w instanceof De?new K(413,w.message):new K(400,`malformed multipart body: ${String(w instanceof Error?w.message:w)}`)}let c=d.get("prompt");if(typeof c!="string"||c.trim().length===0)throw new K(400,"prompt is required and must be a non-empty string field","prompt");let l=d.get("model");if(l!==null&&typeof l!="string")throw new K(400,"model must be a string field","model");let p=l===null?void 0:l;await Xs(n.turn,n.clients,p);let m=d.get("response_format")??"b64_json";if(m!=="b64_json")throw new K(400,`response_format ${JSON.stringify(m)} 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 w of d.keys())if(!bd[w])throw new K(400,`form field "${w}" 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[]).`,w);let b=[];for(let w of["image","image[]"])for(let g of d.getAll(w)){if(!(g instanceof File))throw new K(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",w);if(g.size>o)throw new K(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,w);let v=new Uint8Array(await g.arrayBuffer()),_=Zs(v);if(_===null)throw new K(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",w);b.push({bytes:v,mime:_})}if(b.length===0)throw new K(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",c.length,c.length),!await ni(n.imageModels,p,"edit",e))return;let E=[{type:"input_text",text:c}];for(let{bytes:w,mime:g}of b)E.push({type:"input_image",image_url:`data:${g};base64,${Buffer.from(w).toString("base64")}`});try{let w=await ei(n.clients,p,E,n.turn?Te(n.turn):void 0);ri(n.turn,w),oi(e,w)}catch(w){throw ti(w)}}catch(s){if(s instanceof De){Ct(e,new K(413,s.message));return}if(s instanceof K){Ct(e,s);return}throw s}}S();var Mt="/v1/audio/speech";function ci(t){return t===Mt}var ai=4096,kd=18e4,Sd=5e3,Rd=64*1024*1024,ui=8,di=5,vn="voice_applied",xd="X-Urun-Voice-Applied",G=class extends Error{status;param;retryAfterSeconds;constructor(e,n,r,o){super(n),this.status=e,this.param=r,this.retryAfterSeconds=o}};function Er(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 dt=class extends Error{},bn=class extends G{constructor(e){super(502,e)}};async function Ed(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new dt(`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 dt(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function Od(t,e,n){if(t){if(t.model=be({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 Ad(t,e){t&&(t.outputUnits=e/(24e3*2))}var Id=2;function Td(t){for(let e=0;e+1<t.byteLength;e+=2)if(Math.abs(t.readInt16LE(e))>Id)return!1;return!0}function Pd(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 li=new WeakMap,_n=new WeakMap;function Cd(t,e){let n=_n.get(t)??0;if(n>=ui)throw new G(503,`this model's speech queue is full \u2014 ${ui} requests are already admitted (one in-flight turn plus the queued depth) and each holds a pooled session turn; retry after ${di}s`,void 0,di);_n.set(t,n+1);let r=li.get(t)??Promise.resolve(),o,s=new Promise(i=>{o=i});return li.set(t,s),(async()=>{await r;try{return await e()}finally{_n.set(t,(_n.get(t)??1)-1),o()}})()}async function $d(t,e,n,r){if(typeof t.evictSpeechSession!="function")throw new G(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 ce))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 $t(t){return t instanceof G?t:t instanceof z?new G(404,t.message):new G(502,`speech upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function Md(t,e,n,r,o,s,i){return Cd(e,async()=>{if(i?.()===!0)return null;let a=[],u=!1,d=0,c=!1,l=!1,p,m=new Promise(C=>{p=C}),b=()=>{c&&!l&&d>=o.quietRunFrames&&(l=!0,p())},E=e.onOutputAudio(C=>{let R=Buffer.from(C,"base64");if(R.byteLength===0)return;let O=Td(R);if(!u){if(O)return;u=!0}a.push(R),d=O?d+1:0,b()}),w=null,g,v,_;try{_=(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 R=new Promise(j=>{v=setTimeout(j,s.capMs)}),O=!1;for(;;){let j=_.next(),W=await Promise.race([j,R]);if(W===void 0){O=!0,j.catch(()=>{});break}if(W.done)break;let F=W.value;if(F.type==="error"){let de=F.error??{};throw await _.return?.(),new G(502,`speech upstream error: ${String(de.message??"upstream reported an error")}`)}if(F.type==="response.completed"){w=F.response??null,c=!0,b(),await _.return?.();break}}if(clearTimeout(v),O)throw new G(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(w===null)throw new G(502,"speech upstream produced no terminal response \u2014 refusing to fabricate audio");let k=!1;if(await Promise.race([m,new Promise(j=>{g=setTimeout(()=>{k=!0,j()},o.capMs)})]),g!==void 0&&clearTimeout(g),r.voice!==void 0){let j=w[vn];if(typeof j!="string"||j!==r.voice){let W=`the runtime did not acknowledge the requested voice ${JSON.stringify(r.voice)} \u2014 the terminal response carried ${typeof j=="string"?`${vn} ${JSON.stringify(j)}`:`no ${vn} field`}; the speak-bridge companion is not deployed, and this lane never serves default-voice audio as the requested voice`;throw l?new bn(W):new G(502,W)}}if(!u)throw new G(502,"the upstream produced no voice for this utterance \u2014 refusing to serve silence as success");if(k&&!l&&d<1)throw new G(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 $=d>0?a.slice(0,a.length-d):a;return{pcm:Buffer.concat($),completed:w}}catch(C){throw C instanceof bn||await $d(t,n,_,s.returnBoundMs),C}finally{g!==void 0&&clearTimeout(g),v!==void 0&&clearTimeout(v),E()}})}async function Ud(t,e,n){let r;try{r=await t(e)}catch(o){throw $t(o)}return r?!0:(Er(n,new G(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 Ld={model:!0,input:!0,voice:!0,response_format:!0,stream_format:!0,speed:!0,instructions:!0},Nd="wav, pcm";async function pi(t,e,n){let r=n.maxBodyBytes??Rd,o=!1;e.once("close",()=>{o=!0});let s=()=>o||e.destroyed;try{if(t.method!=="POST")throw new G(405,`${String(t.method)} is not supported on ${Mt}`);let i;try{i=JSON.parse((await Ed(t,r)).toString("utf8")||"{}")}catch(_){throw _ instanceof dt?new G(413,_.message):new G(400,"request body is not valid JSON")}if(i===null||typeof i!="object"||Array.isArray(i))throw new G(400,"request body must be a JSON object");let a=i;for(let _ of Object.keys(a))if(!Ld[_])throw new G(400,`request field "${_}" 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.`,_);let u=a.input;if(typeof u!="string"||u.trim().length===0)throw new G(400,"input is required and must be a non-empty string","input");if(u.length>ai)throw new G(400,`input is ${u.length} chars \u2014 over the ${ai}-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 G(400,"model must be a string","model");let c=a.voice;if(c!==void 0&&(typeof c!="string"||c.trim().length===0))throw new G(400,"voice must be a non-empty string","voice");if(typeof n.clients.speechVoices!="function")throw new G(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 ${Mt} against a stale copy of the list`);let l;try{l=await n.clients.speechVoices(d)}catch(_){throw $t(_)}if(l.defect!==null)throw new G(500,`the voice set for this model cannot be resolved: ${l.defect} \u2014 fix the catalog row; the lane refuses to serve rather than validate against a set nobody vouched`);let p=typeof c=="string"?c:void 0;if(p!==void 0&&l.published.length>0&&!l.published.includes(p))throw new G(400,`voice ${JSON.stringify(p)} is not one of the voices this model publishes \u2014 accepted voices: ${l.published.join(", ")}`,"voice");let m=a.response_format??"wav";if(m!=="wav"&&m!=="pcm")throw new G(400,`response_format ${JSON.stringify(m)} is not supported: this lane serves the engine's raw 24 kHz mono PCM16, so the supported formats are ${Nd} \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 G(400,"stream_format, when present, must be a string","stream_format");let b=a.speed;if(b!==void 0){if(typeof b!="number"||!Number.isFinite(b))throw new G(400,"speed, when present, must be a number","speed");if(b!==1)throw new G(400,`speed=${JSON.stringify(b)} 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 E=a.instructions;if(E!==void 0){if(typeof E!="string")throw new G(400,"instructions, when present, must be a string","instructions");if(E.trim().length>0)throw new G(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 Od(n.turn,n.clients,d);let w=Buffer.byteLength(JSON.stringify(a));if(n.stats?.request("audio_speech",w,w),!await Ud(n.speechModels,d,e))return;if(typeof n.clients.openAudioOn!="function")throw new G(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 G(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 G(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 g;try{g=await n.clients.speechSessionHandle(d)}catch(_){throw $t(_)}let v;try{v=await n.clients.openAudioOn(g)}catch(_){throw $t(_)}n.turn&&(n.turn.sessionId=v.sessionId);try{let _=await Md(n.clients,v,g,{model:d,text:u,...p!==void 0?{voice:p}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??kd,returnBoundMs:n.turnReturnBoundMs??Sd},s);if(_===null||s())return;if(Ad(n.turn,_.pcm.byteLength),e.statusCode=200,p===void 0){let C=_.completed[vn];typeof C=="string"&&e.setHeader(xd,C)}m==="wav"?(e.setHeader("content-type","audio/wav"),e.end(Buffer.concat([Pd(_.pcm.byteLength),_.pcm]))):(e.setHeader("content-type","audio/pcm"),e.end(_.pcm))}catch(_){throw $t(_)}}catch(i){if(i instanceof dt){Er(e,new G(413,i.message));return}if(i instanceof G){Er(e,i);return}throw i}}var On=require("@urun-sh/core");S();var Or=Ge(require("crypto"),1),Dd="urun-runtime-key-v1";function Fd(t,e){if(!t)throw new Error("deriveOrgRuntimeKey requires a master secret");if(!e)throw new Error("deriveOrgRuntimeKey requires an orgId");return Or.createHmac("sha256",t).update(`${Dd}|${e}`,"ascii").digest("base64url")}var Bd="decision-intake",kn=6e4,jd=kn/1e3+5,fi=3,Ar=15;function qd(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:Bd,iat:r,exp:r+Math.max(1,n)},a=o(JSON.stringify(i)),u=Or.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 Gd(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(kn)})}catch(u){throw(u instanceof Error?u.name:void 0)==="TimeoutError"?new Ce(`runtime presence read for (app=${t.app} fn=${t.fn}) timed out after ${kn}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<=Ar)}var Vd=Ar*1e3,Wd=Ar*500,Jd=256,Sn=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 Ce("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 Ce(`no ready runtime for (org app=${o} fn=${s}) \u2014 nothing is serving decisions right now`);let c=null;for(let l=0;l<fi;l++){let p=this.pick(d);if(p===null)throw new Ce(`every discovered runtime for app=${o} failed at the transport layer \u2014 no replica took the decision`);let m=i&&r.sharedOrgId?r.sharedOrgId:this.opts.orgId,b=qd(Fd(this.opts.secret,m),{tenant:this.opts.orgId,app:o,fn:s,...i&&r.sharedOrgId?{provider:r.sharedOrgId}:{}},jd);try{return{...await this.post(p,e,b),target:{...r,fnName:s}}}catch(E){if(E instanceof et)throw E;c=E;let w=this.replicas.get(p)??{outstanding:0,cooldownUntil:0};w.cooldownUntil=this.nowMs()+5e3,this.replicas.set(p,w);let g=this.presenceCache.get(u);g&&(g.rows=g.rows.filter(v=>v.address!==p),g.rows.length===0&&this.presenceCache.delete(u))}}throw new Ce(`no runtime for app=${o} answered within ${fi} attempts: ${String(c instanceof Error?c.message:c)}`)}async ensureRows(e,n,r,o){let s=this.presenceCache.get(e);if(s)if(this.nowMs()-s.fetchedAtMs>Vd)this.presenceCache.delete(e);else return this.nowMs()-s.fetchedAtMs>Wd&&!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>=Jd;){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 Gd({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??kn)}),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 et(s.status,i);return{status:s.status,body:i,completed:Kd(i)}}finally{o.outstanding-=1}}};function Kd(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 et=class extends Error{constructor(n,r){super(`runtime answered ${n}`);this.status=n;this.body=r}status;body},Ce=class extends Error{};function Pr(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function ue(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function le(t,e,n,r="invalid_request_error",o){ue(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function Cr(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);ue(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function $e(t,e,n,r){ue(t,e,{type:"error",error:{type:n,message:r}})}function Me(t,e,n){if(e==="anthropic"){ue(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}ue(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var Yd=64*1024*1024,Ut=class extends Error{};async function mi(t,e=Yd){let n=[],r=0;for await(let s of t){if(r+=s.length,r>e)throw new Ut(`request body exceeds ${e} bytes`);n.push(s)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function lt(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 gi(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 Rn(t){let e=gi(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function hi(t){let e=Rn(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 Xd(t,e){let n=gi(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function Qd(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 Zd(t,e){let n=Qd(e);n&&(t.timing=n)}function Fe(t,e){Xd(t,e),Zd(t,e)}async function An(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 yi(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 el(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 xn=class extends Error{};function tl(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 xn(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function nl(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:tl(n.role,n.content)})}return e}function Ir(t){return it(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 rl(t,e,n,r,o,s){let i=xt(s.id),a=t.stream===!0;if(s.model=be(t),s.stream=a,await An(s,e,typeof t.model=="string"?t.model:void 0),a){let g;try{g=await Ot({body:t,clients:e,store:n,stats:o,responseId:i,turn:s})}catch(v){if(v instanceof z){Me(r,"openai",v);return}if(v instanceof ve){le(r,v.status,v.message);return}throw v}Pr(r);for await(let v of g.events)v.type==="error"&&(s.streamError=!0),v.type==="response.completed"&&Fe(s,v.response),r.write(`event: ${String(v.type??"message")}
26
+ ${Ad(n)}\r
27
+ `),t.destroy()}function Td(t){return`${JSON.stringify({event:"realtime_upgrade",ts:new Date().toISOString(),...t})}
28
+ `}function Pd(t){$e(process.stdout,t)}function Cd(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function $r(t){return t.audioSession?.audio.sessionId??void 0}function bn(t,e){let n=new ti.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,s,i)=>{if(new URL(o.url??"/","http://localhost").pathname!==Dt)return;r.add(s),s.on("close",()=>{r.delete(s)});let u=N("rtup"),d=Cd(o),l=S=>{(e.log??Pd)(Td(S))};l({stage:"accept",conn:u,model:d}),s.resume();let p=new AbortController,y=null,c=S=>{y??=S,p.abort(new Error(y))},m=()=>c("client socket closed"),w=()=>c("client socket closed (half-close)"),R=S=>c(`client socket error: ${S.message}`);s.once("close",m),s.once("end",w),s.once("error",R);let h=()=>{s.removeListener("close",m),s.removeListener("end",w),s.removeListener("error",R)},v=S=>{l({stage:"close",conn:u,model:d,...S})},b=S=>{let E=S,x=E.session!=null&&typeof E.session.sendResponseCreate=="function",L=E.clients!=null&&typeof E.clients.createResponse=="function";if(!x&&!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((B,q)=>{n.handleUpgrade(o,s,i,V=>{try{new Nt({ws:V,binding:S,onClosed:(de,fe)=>{v({while:"live",reason:fe.reason})}}),l({stage:"admit",conn:u,model:d,...$r(S)===void 0?{}:{session:$r(S)}}),B()}catch(de){q(de)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o,p.signal).then(async S=>{if(p.signal.aborted){h();let E=$r(S);try{await S.release?.()}catch(x){v({while:"queued",...E===void 0?{}:{session:E},reason:y??"client socket closed",release_failed:x instanceof Error?x.message:String(x)});return}v({while:"queued",...E===void 0?{}:{session:E},reason:y??"client socket closed",released:!0}),s.destroy();return}h(),await b(S)}).catch(S=>{let E=S instanceof ei.SessionFailedError?hn(S):null,x=null;if(S!==null&&typeof S=="object"&&"status"in S){let q=S.status;typeof q=="number"&&(x=q)}let L=x??E?.status??502,B=S instanceof Error?S.message:String(S);v({while:"queued",...p.signal.aborted?{reason:y??"client socket closed"}:{reason:B,status:L}}),s.destroyed||Id(s,L,B)}).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()))}}k();k();var hl=require("http");k();k();var he=class extends Error{};async function kn(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new he(`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 he(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function Sn(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 he(`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 he?i:new Error(`malformed multipart body: ${String(i instanceof Error?i.message:i)}`)}return s}var ni="/v1/images/generations",Rn="/v1/images/edits";function ri(t){return t===ni||t===Rn}var oi="/v1/images/models";async function si(t,e,n){if(t){if(t.model=ve({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 K=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},$d=16*1024*1024,ii=64*1024*1024;function ai(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 Ft(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 ui(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 y=p.error??{};throw new K(502,`image upstream error: ${String(y.message??"upstream reported an error")}`)}if(p.type==="response.completed"){s=p.response??null;break}}if(s===null)throw new K(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 K(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(ai(d)===null)throw new K(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 di(t){return t instanceof K?t:t instanceof z?new K(404,t.message):new K(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function li(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 Ft(r,new K(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 ci(t,e){t&&(t.outputUnits=e.images.length)}function pi(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 Md={model:!0,prompt:!0,response_format:!0,n:!0};async function fi(t,e,n){let r=n.maxBodyBytes??ii;try{if(t.method!=="POST")throw new K(405,`${String(t.method)} is not supported on ${ni}`);let o;try{o=JSON.parse((await kn(t,r)).toString("utf8")||"{}")}catch(p){throw p instanceof he?new K(413,p.message):new K(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new K(400,"request body must be a JSON object");let s=o;for(let p of Object.keys(s))if(!Md[p])throw new K(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 K(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 K(400,"model must be a string","model");await si(n.turn,n.clients,a);let u=s.n??1;if(u!==1)throw new K(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 K(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 li(n.imageModels,a,"generation",e))return;let l=[{type:"input_text",text:i}];try{let p=await ui(n.clients,a,l,n.turn?xe(n.turn):void 0);ci(n.turn,p),pi(e,p)}catch(p){throw di(p)}}catch(o){if(o instanceof he){Ft(e,new K(413,o.message));return}if(o instanceof K){Ft(e,o);return}throw o}}var Ld={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function mi(t,e,n){let r=n.maxBodyBytes??ii,o=n.maxImagePartBytes??$d;try{if(t.method!=="POST")throw new K(405,`${String(t.method)} is not supported on ${Rn}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new K(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await Sn(t,r)}catch(c){throw c instanceof he?new K(413,c.message):new K(400,String(c instanceof Error?c.message:c))}let a=i.get("prompt");if(typeof a!="string"||a.trim().length===0)throw new K(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 K(400,"model must be a string field","model");let d=u===null?void 0:u;await si(n.turn,n.clients,d);let l=i.get("response_format")??"b64_json";if(l!=="b64_json")throw new K(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(!Ld[c])throw new K(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 m of i.getAll(c)){if(!(m instanceof File))throw new K(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",c);if(m.size>o)throw new K(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,c);let w=new Uint8Array(await m.arrayBuffer()),R=ai(w);if(R===null)throw new K(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:w,mime:R})}if(p.length===0)throw new K(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",a.length,a.length),!await li(n.imageModels,d,"edit",e))return;let y=[{type:"input_text",text:a}];for(let{bytes:c,mime:m}of p)y.push({type:"input_image",image_url:`data:${m};base64,${Buffer.from(c).toString("base64")}`});try{let c=await ui(n.clients,d,y,n.turn?xe(n.turn):void 0);ci(n.turn,c),pi(e,c)}catch(c){throw di(c)}}catch(s){if(s instanceof he){Ft(e,new K(413,s.message));return}if(s instanceof K){Ft(e,s);return}throw s}}k();var jt="/v1/audio/speech";function _i(t){return t===jt}var gi=4096,Ud=18e4,Nd=5e3,Dd=64*1024*1024,hi=8,yi=5,En="voice_applied",Fd="X-Urun-Voice-Applied",G=class extends Error{status;param;retryAfterSeconds;constructor(e,n,r,o){super(n),this.status=e,this.param=r,this.retryAfterSeconds=o}};function Mr(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 On=class extends G{constructor(e){super(502,e)}};async function Bd(t,e,n){if(t){if(t.model=ve({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 jd(t,e){t&&(t.outputUnits=e/(24e3*2))}var qd=2;function Hd(t){for(let e=0;e+1<t.byteLength;e+=2)if(Math.abs(t.readInt16LE(e))>qd)return!1;return!0}function Gd(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 wi=new WeakMap,xn=new WeakMap;function Vd(t,e){let n=xn.get(t)??0;if(n>=hi)throw new G(503,`this model's speech queue is full \u2014 ${hi} requests are already admitted (one in-flight turn plus the queued depth) and each holds a pooled session turn; retry after ${yi}s`,void 0,yi);xn.set(t,n+1);let r=wi.get(t)??Promise.resolve(),o,s=new Promise(i=>{o=i});return wi.set(t,s),(async()=>{await r;try{return await e()}finally{xn.set(t,(xn.get(t)??1)-1),o()}})()}async function Wd(t,e,n,r){if(typeof t.evictSpeechSession!="function")throw new G(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 pe))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 Bt(t){return t instanceof G?t:t instanceof z?new G(404,t.message):new G(502,`speech upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function Jd(t,e,n,r,o,s,i){return Vd(e,async()=>{if(i?.()===!0)return null;let a=[],u=!1,d=0,l=!1,p=!1,y,c=new Promise($=>{y=$}),m=()=>{l&&!p&&d>=o.quietRunFrames&&(p=!0,y())},w=e.onOutputAudio($=>{let S=Buffer.from($,"base64");if(S.byteLength===0)return;let E=Hd(S);if(!u){if(E)return;u=!0}a.push(S),d=E?d+1:0,m()}),R=null,h,v,b;try{b=(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 S=new Promise(B=>{v=setTimeout(B,s.capMs)}),E=!1;for(;;){let B=b.next(),q=await Promise.race([B,S]);if(q===void 0){E=!0,B.catch(()=>{});break}if(q.done)break;let V=q.value;if(V.type==="error"){let de=V.error??{};throw await b.return?.(),new G(502,`speech upstream error: ${String(de.message??"upstream reported an error")}`)}if(V.type==="response.completed"){R=V.response??null,l=!0,m(),await b.return?.();break}}if(clearTimeout(v),E)throw new G(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(R===null)throw new G(502,"speech upstream produced no terminal response \u2014 refusing to fabricate audio");let x=!1;if(await Promise.race([c,new Promise(B=>{h=setTimeout(()=>{x=!0,B()},o.capMs)})]),h!==void 0&&clearTimeout(h),r.voice!==void 0){let B=R[En];if(typeof B!="string"||B!==r.voice){let q=`the runtime did not acknowledge the requested voice ${JSON.stringify(r.voice)} \u2014 the terminal response carried ${typeof B=="string"?`${En} ${JSON.stringify(B)}`:`no ${En} field`}; the speak-bridge companion is not deployed, and this lane never serves default-voice audio as the requested voice`;throw p?new On(q):new G(502,q)}}if(!u)throw new G(502,"the upstream produced no voice for this utterance \u2014 refusing to serve silence as success");if(x&&!p&&d<1)throw new G(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:R}}catch($){throw $ instanceof On||await Wd(t,n,b,s.returnBoundMs),$}finally{h!==void 0&&clearTimeout(h),v!==void 0&&clearTimeout(v),w()}})}async function Kd(t,e,n){let r;try{r=await t(e)}catch(o){throw Bt(o)}return r?!0:(Mr(n,new G(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 zd={model:!0,input:!0,voice:!0,response_format:!0,stream_format:!0,speed:!0,instructions:!0},Yd="wav, pcm";async function vi(t,e,n){let r=n.maxBodyBytes??Dd,o=!1;e.once("close",()=>{o=!0});let s=()=>o||e.destroyed;try{if(t.method!=="POST")throw new G(405,`${String(t.method)} is not supported on ${jt}`);let i;try{i=JSON.parse((await kn(t,r)).toString("utf8")||"{}")}catch(b){throw b instanceof he?new G(413,b.message):new G(400,"request body is not valid JSON")}if(i===null||typeof i!="object"||Array.isArray(i))throw new G(400,"request body must be a JSON object");let a=i;for(let b of Object.keys(a))if(!zd[b])throw new G(400,`request field "${b}" 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.`,b);let u=a.input;if(typeof u!="string"||u.trim().length===0)throw new G(400,"input is required and must be a non-empty string","input");if(u.length>gi)throw new G(400,`input is ${u.length} chars \u2014 over the ${gi}-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 G(400,"model must be a string","model");let l=a.voice;if(l!==void 0&&(typeof l!="string"||l.trim().length===0))throw new G(400,"voice must be a non-empty string","voice");if(typeof n.clients.speechVoices!="function")throw new G(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 ${jt} against a stale copy of the list`);let p;try{p=await n.clients.speechVoices(d)}catch(b){throw Bt(b)}if(p.defect!==null)throw new G(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 y=typeof l=="string"?l:void 0;if(y!==void 0&&p.published.length>0&&!p.published.includes(y))throw new G(400,`voice ${JSON.stringify(y)} 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 G(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 ${Yd} \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 G(400,"stream_format, when present, must be a string","stream_format");let m=a.speed;if(m!==void 0){if(typeof m!="number"||!Number.isFinite(m))throw new G(400,"speed, when present, must be a number","speed");if(m!==1)throw new G(400,`speed=${JSON.stringify(m)} 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 w=a.instructions;if(w!==void 0){if(typeof w!="string")throw new G(400,"instructions, when present, must be a string","instructions");if(w.trim().length>0)throw new G(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 Bd(n.turn,n.clients,d);let R=Buffer.byteLength(JSON.stringify(a));if(n.stats?.request("audio_speech",R,R),!await Kd(n.speechModels,d,e))return;if(typeof n.clients.openAudioOn!="function")throw new G(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 G(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 G(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 h;try{h=await n.clients.speechSessionHandle(d)}catch(b){throw Bt(b)}let v;try{v=await n.clients.openAudioOn(h)}catch(b){throw Bt(b)}n.turn&&(n.turn.sessionId=v.sessionId);try{let b=await Jd(n.clients,v,h,{model:d,text:u,...y!==void 0?{voice:y}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??Ud,returnBoundMs:n.turnReturnBoundMs??Nd},s);if(b===null||s())return;if(jd(n.turn,b.pcm.byteLength),e.statusCode=200,y===void 0){let $=b.completed[En];typeof $=="string"&&e.setHeader(Fd,$)}c==="wav"?(e.setHeader("content-type","audio/wav"),e.end(Buffer.concat([Gd(b.pcm.byteLength),b.pcm]))):(e.setHeader("content-type","audio/pcm"),e.end(b.pcm))}catch(b){throw Bt(b)}}catch(i){if(i instanceof he){Mr(e,new G(413,i.message));return}if(i instanceof G){Mr(e,i);return}throw i}}k();var An="/v1/audio/transcriptions";function bi(t){return t===An}var ki="/v1/audio/models",Xd=16*1024*1024,Qd=64*1024*1024,ee=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}};function Lr(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 Si(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 Zd(t){if(!Si(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 el(t,e,n){if(t){if(t.model=ve({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 tl(t,e){t&&(t.outputUnits=e.seconds)}async function nl(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 ee(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 ee(502,"transcription upstream produced no terminal response \u2014 refusing to fabricate a transcript");return gt(i,a)}function Ri(t){return t instanceof ee?t:t instanceof z?new ee(404,t.message):new ee(502,`transcription upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function rl(t,e,n){let r;try{r=await t(e)}catch(o){throw Ri(o)}return r?!0:(Lr(n,new ee(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 ol=new Set(["file","model","language","response_format"]);async function xi(t,e,n){let r=n.maxBodyBytes??Qd,o=n.maxAudioPartBytes??Xd;try{if(t.method!=="POST")throw new ee(405,`${String(t.method)} is not supported on ${An}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new ee(400,`the transcriptions endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await Sn(t,r)}catch(h){throw h instanceof he?new ee(413,h.message):new ee(400,String(h instanceof Error?h.message:h))}let a=new Map;for(let h of i.keys()){if(!ol.has(h))throw new ee(400,`form field "${h}" 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.`,h);a.set(h,(a.get(h)??0)+1)}for(let[h,v]of a)if(v>1)throw new ee(400,`form field "${h}" appears ${v} 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`,h);let u=i.get("model");if(u!==null&&typeof u!="string")throw new ee(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 ee(400,"language, when present, must be a non-empty string","language");let p=l===null?void 0:l,y=i.get("response_format")??"json";if(y!=="json")throw new ee(400,`response_format ${JSON.stringify(y)} 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 ee(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 ee(413,`file part exceeds the ${o}-byte limit (the serve protocol's MAX_AUDIO_BYTES)`,"file");let m=new Uint8Array(await c.arrayBuffer());if(!Si(m))throw new ee(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 w=Zd(m);if(w===null)throw new ee(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 el(n.turn,n.clients,d),n.stats?.request("audio_transcriptions",m.length,m.length),!await rl(n.transcriptionModels,d,e))return;let R=[{type:"input_audio",input_audio:{data:Buffer.from(m).toString("base64"),format:"wav"}}];try{let h=await nl(n.clients,d,R,p,n.turn?xe(n.turn):void 0);tl(n.turn,w),e.statusCode=200,e.setHeader("content-type","application/json"),e.end(JSON.stringify({text:h}))}catch(h){throw Ri(h)}}catch(s){if(s instanceof he){Lr(e,new ee(413,s.message));return}if(s instanceof ee){Lr(e,s);return}throw s}}var Mn=require("@urun-sh/core");k();var Ur=ze(require("crypto"),1),sl="urun-runtime-key-v1";function il(t,e){if(!t)throw new Error("deriveOrgRuntimeKey requires a master secret");if(!e)throw new Error("deriveOrgRuntimeKey requires an orgId");return Ur.createHmac("sha256",t).update(`${sl}|${e}`,"ascii").digest("base64url")}var al="decision-intake",In=6e4,ul=In/1e3+5,Ei=3,Nr=15;function dl(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:al,iat:r,exp:r+Math.max(1,n)},a=o(JSON.stringify(i)),u=Ur.createHmac("sha256",t).update(`${s}.${a}`,"ascii").digest("base64url");return`${s}.${a}.${u}`}function ll(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 cl(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(In)})}catch(u){throw(u instanceof Error?u.name:void 0)==="TimeoutError"?new Le(`runtime presence read for (app=${t.app} fn=${t.fn}) timed out after ${In}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(!ll(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 pl=Nr*1e3,fl=Nr*500,ml=256,Tn=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 Le("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 Le(`no ready runtime for (org app=${o} fn=${s}) \u2014 nothing is serving decisions right now`);let l=null;for(let p=0;p<Ei;p++){let y=this.pick(d);if(y===null)throw new Le(`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,m=dl(il(this.opts.secret,c),{tenant:this.opts.orgId,app:o,fn:s,...i&&r.sharedOrgId?{provider:r.sharedOrgId}:{}},ul);try{return{...await this.post(y,e,m),target:{...r,fnName:s}}}catch(w){if(w instanceof it)throw w;l=w;let R=this.replicas.get(y)??{outstanding:0,cooldownUntil:0};R.cooldownUntil=this.nowMs()+5e3,this.replicas.set(y,R);let h=this.presenceCache.get(u);h&&(h.rows=h.rows.filter(v=>v.address!==y),h.rows.length===0&&this.presenceCache.delete(u))}}throw new Le(`no runtime for app=${o} answered within ${Ei} 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>pl)this.presenceCache.delete(e);else return this.nowMs()-s.fetchedAtMs>fl&&!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>=ml;){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 cl({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??In)}),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 it(s.status,i);return{status:s.status,body:i,completed:gl(i)}}finally{o.outstanding-=1}}};function gl(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 it=class extends Error{constructor(n,r){super(`runtime answered ${n}`);this.status=n;this.body=r}status;body},Le=class extends Error{};function Br(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function ue(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function ge(t,e,n,r="invalid_request_error",o){ue(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function jr(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);ue(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function Ue(t,e,n,r){ue(t,e,{type:"error",error:{type:n,message:r}})}function Ne(t,e,n){if(e==="anthropic"){ue(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}ue(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var yl=64*1024*1024,qt=class extends Error{};async function Oi(t,e=yl){let n=[],r=0;for await(let s of t){if(r+=s.length,r>e)throw new qt(`request body exceeds ${e} bytes`);n.push(s)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function wt(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 Ii(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 Pn(t){let e=Ii(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function Ai(t){let e=Pn(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 wl(t,e){let n=Ii(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function _l(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 vl(t,e){let n=_l(e);n&&(t.timing=n)}function He(t,e){wl(t,e),vl(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 Ti(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 bl(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 Cn=class extends Error{};function kl(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 Cn(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function Sl(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:kl(n.role,n.content)})}return e}function Dr(t){return ft(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 Rl(t,e,n,r,o,s){let i=$t(s.id),a=t.stream===!0;if(s.model=ve(t),s.stream=a,await Ln(s,e,typeof t.model=="string"?t.model:void 0),a){let h;try{h=await Lt({body:t,clients:e,store:n,stats:o,responseId:i,turn:s})}catch(v){if(v instanceof z){Ne(r,"openai",v);return}if(v instanceof Ee){ge(r,v.status,v.message);return}throw v}Br(r);for await(let v of h.events)v.type==="error"&&(s.streamError=!0),v.type==="response.completed"&&He(s,v.response),r.write(`event: ${String(v.type??"message")}
29
29
  data: ${JSON.stringify(v)}
30
30
 
31
31
  `);r.write(`data: [DONE]
32
32
 
33
- `),r.end();return}let u;try{u=ut(n,t)}catch(g){if(g instanceof ve){le(r,g.status,g.message);return}throw g}t.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(u))-Buffer.byteLength(JSON.stringify(t.input)));let d=t.store!==!1,c=be(t);o.model(c);let l=Array.isArray(t.tools)&&t.tools.length>0,p;try{p=await fr(e,t,u,!1,Te(s))}catch(g){if(g instanceof z){Me(r,"openai",g);return}if(g instanceof ve){le(r,g.status,g.message);return}throw g}let m=[],b=null;for await(let g of o.track(p,c,s)){if(m.push(g),g.type==="error"){Cr(r,g);return}g.type==="response.completed"&&(b=g.response)}if(b==null){le(r,502,"upstream produced no response.completed event","upstream_error");return}Fe(s,b);let E=dn(m,b),w=Ze(l,it(b).length,E);if(w){le(r,502,Qe(c,w),"upstream_error");return}d&&n.remember(i,u,pr(b,E),d),ue(r,200,{...b,id:i})}async function ol(t,e,n,r,o){let s=t.messages;if(!Array.isArray(s)){le(n,400,"chat/completions requires a messages array");return}let i=String(t.model??"urun"),a=$s(o.id),u;try{u=nl(s)}catch(k){if(k instanceof xn){le(n,400,k.message);return}throw k}let d=be(t);r.model(d),o.model=d,o.stream=t.stream===!0,await An(o,e,typeof t.model=="string"?t.model:void 0);let c=Array.isArray(t.tools)&&t.tools.length>0;if(t.stream!==!0&&e.stateless?.enabled){let k;try{k=await e.stateless.targetFor(typeof t.model=="string"?t.model:void 0)}catch($){if($ instanceof z){Me(n,"openai",$);return}throw $}if(k!==null){try{let $=await e.stateless.decide(t,typeof t.model=="string"?t.model:void 0);o.catalog=yt($.target),Fe(o,$.completed),ue(n,200,{...$.body,id:a})}catch($){if($ instanceof z){Me(n,"openai",$);return}if($ instanceof et){ue(n,$.status,$.body);return}if($ instanceof Ce){le(n,503,$.message,"upstream_error");return}throw $}return}}let l;try{l=await e.createResponse({model:t.model,input:u,stream:!0,tools:el(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}:{}},Te(o))}catch(k){if(k instanceof z){Me(n,"openai",k);return}throw k}if(t.stream===!0){Pr(n);let k=[],$=new qe,j="";n.write(`data: ${JSON.stringify(lt(a,i,{role:"assistant"},null))}
33
+ `),r.end();return}let u;try{u=ht(n,t)}catch(h){if(h instanceof Ee){ge(r,h.status,h.message);return}throw h}t.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(u))-Buffer.byteLength(JSON.stringify(t.input)));let d=t.store!==!1,l=ve(t);o.model(l);let p=Array.isArray(t.tools)&&t.tools.length>0,y;try{y=await br(e,t,u,!1,xe(s))}catch(h){if(h instanceof z){Ne(r,"openai",h);return}if(h instanceof Ee){ge(r,h.status,h.message);return}throw h}let c=[],m=null;for await(let h of o.track(y,l,s)){if(c.push(h),h.type==="error"){jr(r,h);return}h.type==="response.completed"&&(m=h.response)}if(m==null){ge(r,502,"upstream produced no response.completed event","upstream_error");return}He(s,m);let w=gt(c,m),R=st(p,ft(m).length,w);if(R){ge(r,502,ot(l,R),"upstream_error");return}d&&n.remember(i,u,vr(m,w),d),ue(r,200,{...m,id:i})}async function xl(t,e,n,r,o){let s=t.messages;if(!Array.isArray(s)){ge(n,400,"chat/completions requires a messages array");return}let i=String(t.model??"urun"),a=Bs(o.id),u;try{u=Sl(s)}catch(x){if(x instanceof Cn){ge(n,400,x.message);return}throw x}let d=ve(t);r.model(d),o.model=d,o.stream=t.stream===!0,await Ln(o,e,typeof t.model=="string"?t.model:void 0);let l=Array.isArray(t.tools)&&t.tools.length>0;if(t.stream!==!0&&e.stateless?.enabled){let x;try{x=await e.stateless.targetFor(typeof t.model=="string"?t.model:void 0)}catch(L){if(L instanceof z){Ne(n,"openai",L);return}throw L}if(x!==null){try{let L=await e.stateless.decide(t,typeof t.model=="string"?t.model:void 0);o.catalog=xt(L.target),He(o,L.completed),ue(n,200,{...L.body,id:a})}catch(L){if(L instanceof z){Ne(n,"openai",L);return}if(L instanceof it){ue(n,L.status,L.body);return}if(L instanceof Le){ge(n,503,L.message,"upstream_error");return}throw L}return}}let p;try{p=await e.createResponse({model:t.model,input:u,stream:!0,tools:bl(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}:{}},xe(o))}catch(x){if(x instanceof z){Ne(n,"openai",x);return}throw x}if(t.stream===!0){Br(n);let x=[],L=new We,B="";n.write(`data: ${JSON.stringify(wt(a,i,{role:"assistant"},null))}
34
34
 
35
- `);for await(let oe of r.track(l,d,o)){k.push(oe);let q=at(oe);if(oe.type==="response.output_text.delta"&&typeof oe.delta=="string")j+=oe.delta,n.write(`data: ${JSON.stringify(lt(a,i,{content:oe.delta},null))}
35
+ `);for await(let se of r.track(p,d,o)){x.push(se);let ie=mt(se);if(se.type==="response.output_text.delta"&&typeof se.delta=="string")B+=se.delta,n.write(`data: ${JSON.stringify(wt(a,i,{content:se.delta},null))}
36
36
 
37
- `);else if(oe.type==="response.reasoning_text.delta"&&typeof oe.delta=="string")n.write(`data: ${JSON.stringify(lt(a,i,{reasoning_content:oe.delta},null))}
37
+ `);else if(se.type==="response.reasoning_text.delta"&&typeof se.delta=="string")n.write(`data: ${JSON.stringify(wt(a,i,{reasoning_content:se.delta},null))}
38
38
 
39
- `);else if(q){let{call:Y,isNew:Z}=$.add(q),he=Z?{index:q.tool_index,id:Y.id??`call_${q.tool_index}`,type:"function",function:{name:Y.name??"",arguments:q.delta}}:{index:q.tool_index,function:{arguments:q.delta}};n.write(`data: ${JSON.stringify(lt(a,i,{tool_calls:[he]},null))}
39
+ `);else if(ie){let{call:ke,isNew:Z}=L.add(ie),Se=Z?{index:ie.tool_index,id:ke.id??`call_${ie.tool_index}`,type:"function",function:{name:ke.name??"",arguments:ie.delta}}:{index:ie.tool_index,function:{arguments:ie.delta}};n.write(`data: ${JSON.stringify(wt(a,i,{tool_calls:[Se]},null))}
40
40
 
41
- `)}else if(oe.type==="error"){o.streamError=!0,n.write(`data: ${JSON.stringify({error:oe})}
41
+ `)}else if(se.type==="error"){o.streamError=!0,n.write(`data: ${JSON.stringify({error:se})}
42
42
 
43
- `),n.end();return}}let W=Ir(Pe(k)),F=Ze(c,$.size+W.length,j);if(F){o.streamError=!0,n.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:Qe(d,F)}})}
43
+ `),n.end();return}}let q=Dr(Me(x)),V=st(l,L.size+q.length,B);if(V){o.streamError=!0,n.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:ot(d,V)}})}
44
44
 
45
- `),n.end();return}$.size===0&&W.length>0&&n.write(`data: ${JSON.stringify(lt(a,i,{tool_calls:W},null))}
45
+ `),n.end();return}L.size===0&&q.length>0&&n.write(`data: ${JSON.stringify(wt(a,i,{tool_calls:q},null))}
46
46
 
47
- `);let de=$.size>0||W.length>0;n.write(`data: ${JSON.stringify(lt(a,i,{},de?"tool_calls":"stop"))}
47
+ `);let de=L.size>0||q.length>0;n.write(`data: ${JSON.stringify(wt(a,i,{},de?"tool_calls":"stop"))}
48
48
 
49
- `);let pe=Rn(Pe(k));Fe(o,Pe(k)),pe&&n.write(`data: ${JSON.stringify({id:a,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:i,choices:[],usage:hi(Pe(k))})}
49
+ `);let fe=Pn(Me(x));He(o,Me(x)),fe&&n.write(`data: ${JSON.stringify({id:a,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:i,choices:[],usage:Ai(Me(x))})}
50
50
 
51
51
  `),n.write(`data: [DONE]
52
52
 
53
- `),n.end();return}let p="",m="",b=[],E=new qe;for await(let k of r.track(l,d,o)){b.push(k),k.type==="response.output_text.delta"&&typeof k.delta=="string"&&(p+=k.delta),k.type==="response.reasoning_text.delta"&&typeof k.delta=="string"&&(m+=k.delta);let $=at(k);if($&&E.add($),k.type==="error"){Cr(n,k);return}}let w=Pe(b);Fe(o,w);let g=Ir(w),v=g.length>0?g:Ir({output:E.items()}),_=Ze(c,v.length,p);if(_){le(n,502,Qe(d,_),"upstream_error");return}let C=v.map(({index:k,...$})=>$),R={role:"assistant",content:p||null},O=m||yi(w);O&&(R.reasoning_content=O),C.length>0&&(R.tool_calls=C),ue(n,200,{id:a,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:i,choices:[{index:0,message:R,finish_reason:C.length>0?"tool_calls":"stop"}],usage:hi(w)})}var sl="reflex-latest";async function il(t,e,n,r,o){if(t===null||typeof t!="object"||Array.isArray(t)){le(n,400,"systemone requires a JSON object body");return}delete t.key;let s=typeof t.model=="string"&&t.model?t.model:sl,i=be({model:s});if(o.model=i,o.stream=!1,t.state===void 0||t.state===null){le(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){le(n,400,"systemone requires a questions object (question id -> {kind, options, ...})");return}if(t.media!==void 0&&!Array.isArray(t.media)){le(n,400,"systemone media, when present, must be an array");return}if(r.model(i),await An(o,e,s),e.stateless?.enabled){let m;try{m=await e.stateless.targetFor(s)}catch(b){if(b instanceof z){Me(n,"openai",b);return}throw b}if(m!==null){try{let b=await e.stateless.decide(t,s);o.catalog=yt(b.target),Fe(o,b.completed),ue(n,200,b.body)}catch(b){if(b instanceof z){Me(n,"openai",b);return}if(b instanceof et){ue(n,b.status,b.body);return}if(b instanceof Ce){le(n,503,b.message,"upstream_error");return}throw b}return}}let u;try{u=await e.createResponse({model:s,input:[{role:"user",content:JSON.stringify(t)}],stream:!0},Te(o))}catch(m){if(m instanceof z){Me(n,"openai",m);return}throw m}let d=[];try{for await(let m of r.track(u,i,o))if(d.push(m),m.type==="error"){Cr(n,m);return}}catch(m){if(m instanceof z){Me(n,"openai",m);return}throw m}let c=Pe(d);if(c==null){le(n,502,"upstream produced no response.completed event","upstream_error");return}Fe(o,c);let l=dn(d,c),p;try{p=JSON.parse(l)}catch{le(n,502,"systemone answer was not valid JSON","upstream_error");return}if(p===null||typeof p!="object"||Array.isArray(p)){le(n,502,"systemone answer was not a JSON object","upstream_error");return}ue(n,200,p)}function al(t){return typeof t=="string"?t:Array.isArray(t)?t.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var Lt=class extends Error{};function En(t){return new Lt(`unsupported anthropic content block type "${t}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function ul(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 dl(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 Lt(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function ll(t){if(typeof t=="string")return t;if(t==null)return"";if(!Array.isArray(t))throw En(typeof t);let e="";for(let n of t){let r=String(n.type??"");if(r!=="text")throw En(`tool_result > ${r}`);e+=String(n.text??"")}return e}function cl(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 En(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:ll(i.content)});else{if(a==="thinking"||a==="redacted_thinking")continue;throw En(a)}}return s(),r}function Tr(t){return it(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 pl(t,e,n,r,o){let s=t.messages;if(!Array.isArray(s)){$e(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){$e(n,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(t.top_k!==void 0){$e(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(k=>typeof k!="string"))){$e(n,400,"invalid_request_error","stop_sequences must be an array of strings");return}let i=String(t.model??"urun"),a=Ms(o.id),u=[],d;try{t.system&&u.push({role:"system",content:al(t.system)});for(let k of s)u.push(...cl(k));d=dl(t.tool_choice)}catch(k){if(k instanceof Lt){$e(n,400,"invalid_request_error",k.message);return}throw k}let c=be(t);r.model(c),o.model=c,o.stream=t.stream===!0,await An(o,e,typeof t.model=="string"?t.model:void 0);let l=Array.isArray(t.tools)&&t.tools.length>0,p;try{p=await e.createResponse({model:t.model,input:u,stream:!0,tools:ul(t.tools),tool_choice:d,temperature:t.temperature,max_output_tokens:t.max_tokens,top_p:t.top_p,stop:t.stop_sequences},Te(o))}catch(k){if(k instanceof z){Me(n,"anthropic",k);return}throw k}let m=(k,$)=>{n.destroyed||n.write(`event: ${k}
54
- data: ${JSON.stringify({type:k,...$})}
53
+ `),n.end();return}let y="",c="",m=[],w=new We;for await(let x of r.track(p,d,o)){m.push(x),x.type==="response.output_text.delta"&&typeof x.delta=="string"&&(y+=x.delta),x.type==="response.reasoning_text.delta"&&typeof x.delta=="string"&&(c+=x.delta);let L=mt(x);if(L&&w.add(L),x.type==="error"){jr(n,x);return}}let R=Me(m);He(o,R);let h=Dr(R),v=h.length>0?h:Dr({output:w.items()}),b=st(l,v.length,y);if(b){ge(n,502,ot(d,b),"upstream_error");return}let $=v.map(({index:x,...L})=>L),S={role:"assistant",content:y||null},E=c||Ti(R);E&&(S.reasoning_content=E),$.length>0&&(S.tool_calls=$),ue(n,200,{id:a,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:i,choices:[{index:0,message:S,finish_reason:$.length>0?"tool_calls":"stop"}],usage:Ai(R)})}var El="reflex-latest";async function Ol(t,e,n,r,o){if(t===null||typeof t!="object"||Array.isArray(t)){ge(n,400,"systemone requires a JSON object body");return}delete t.key;let s=typeof t.model=="string"&&t.model?t.model:El,i=ve({model:s});if(o.model=i,o.stream=!1,t.state===void 0||t.state===null){ge(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){ge(n,400,"systemone requires a questions object (question id -> {kind, options, ...})");return}if(t.media!==void 0&&!Array.isArray(t.media)){ge(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(m){if(m instanceof z){Ne(n,"openai",m);return}throw m}if(c!==null){try{let m=await e.stateless.decide(t,s);o.catalog=xt(m.target),He(o,m.completed),ue(n,200,m.body)}catch(m){if(m instanceof z){Ne(n,"openai",m);return}if(m instanceof it){ue(n,m.status,m.body);return}if(m instanceof Le){ge(n,503,m.message,"upstream_error");return}throw m}return}}let u;try{u=await e.createResponse({model:s,input:[{role:"user",content:JSON.stringify(t)}],stream:!0},xe(o))}catch(c){if(c instanceof z){Ne(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"){jr(n,c);return}}catch(c){if(c instanceof z){Ne(n,"openai",c);return}throw c}let l=Me(d);if(l==null){ge(n,502,"upstream produced no response.completed event","upstream_error");return}He(o,l);let p=gt(d,l),y;try{y=JSON.parse(p)}catch{ge(n,502,"systemone answer was not valid JSON","upstream_error");return}if(y===null||typeof y!="object"||Array.isArray(y)){ge(n,502,"systemone answer was not a JSON object","upstream_error");return}ue(n,200,y)}function Al(t){return typeof t=="string"?t:Array.isArray(t)?t.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var Ht=class extends Error{};function $n(t){return new Ht(`unsupported anthropic content block type "${t}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function Il(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 Tl(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 Ht(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function Pl(t){if(typeof t=="string")return t;if(t==null)return"";if(!Array.isArray(t))throw $n(typeof t);let e="";for(let n of t){let r=String(n.type??"");if(r!=="text")throw $n(`tool_result > ${r}`);e+=String(n.text??"")}return e}function Cl(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 $n(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:Pl(i.content)});else{if(a==="thinking"||a==="redacted_thinking")continue;throw $n(a)}}return s(),r}function Fr(t){return ft(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 $l(t,e,n,r,o){let s=t.messages;if(!Array.isArray(s)){Ue(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){Ue(n,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(t.top_k!==void 0){Ue(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"))){Ue(n,400,"invalid_request_error","stop_sequences must be an array of strings");return}let i=String(t.model??"urun"),a=js(o.id),u=[],d;try{t.system&&u.push({role:"system",content:Al(t.system)});for(let x of s)u.push(...Cl(x));d=Tl(t.tool_choice)}catch(x){if(x instanceof Ht){Ue(n,400,"invalid_request_error",x.message);return}throw x}let l=ve(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,y;try{y=await e.createResponse({model:t.model,input:u,stream:!0,tools:Il(t.tools),tool_choice:d,temperature:t.temperature,max_output_tokens:t.max_tokens,top_p:t.top_p,stop:t.stop_sequences},xe(o))}catch(x){if(x instanceof z){Ne(n,"anthropic",x);return}throw x}let c=(x,L)=>{n.destroyed||n.write(`event: ${x}
54
+ data: ${JSON.stringify({type:x,...L})}
55
55
 
56
- `)};if(t.stream===!0){let k=r.track(p,c,o);Pr(n),m("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:i,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let $=0,j="",W=0,F="none",de=()=>F,pe=-1,oe=-1,q=new qe,Y=[],Z=()=>{F!=="none"&&(F="none",m("content_block_stop",{index:pe}))},he=(H,ge)=>{Z(),F=H,pe=W++,m("content_block_start",{index:pe,content_block:ge})};for await(let H of k){if(n.destroyed)break;Y.push(H);let ge=at(H);if(H.type==="response.reasoning_text.delta"&&typeof H.delta=="string")de()!=="thinking"&&he("thinking",{type:"thinking",thinking:""}),m("content_block_delta",{index:pe,delta:{type:"thinking_delta",thinking:H.delta}});else if(H.type==="response.output_text.delta"&&typeof H.delta=="string")de()!=="text"&&he("text",{type:"text",text:""}),$+=1,j+=H.delta,m("content_block_delta",{index:pe,delta:{type:"text_delta",text:H.delta}});else if(ge){let{call:Ft}=q.add(ge);(de()!=="tool"||ge.tool_index!==oe)&&(oe=ge.tool_index,he("tool",{type:"tool_use",id:Ft.id??`call_${ge.tool_index}`,name:Ft.name??"",input:{}})),ge.delta&&m("content_block_delta",{index:pe,delta:{type:"input_json_delta",partial_json:ge.delta}})}else if(H.type==="error"){o.streamError=!0,m("error",{error:{type:"api_error",message:JSON.stringify(H)}}),n.end();return}}if(n.destroyed)return;let Ee=Pe(Y),Oe=it(Ee),Se=Ze(l,q.size+Oe.length,j);if(Se){o.streamError=!0,m("error",{error:{type:"api_error",message:Qe(c,Se)}}),n.end();return}Z();let U=[];if(q.size===0)try{U=Tr(Ee)}catch(H){o.streamError=!0,m("error",{error:{type:"api_error",message:String(H instanceof Error?H.message:H)}}),n.end();return}W===0&&U.length===0&&(he("text",{type:"text",text:""}),Z()),U.forEach(H=>{let ge=W++;m("content_block_start",{index:ge,content_block:{...H,input:{}}}),m("content_block_delta",{index:ge,delta:{type:"input_json_delta",partial_json:JSON.stringify(H.input??{})}}),m("content_block_stop",{index:ge})});let we=q.size>0||U.length>0,Be=Rn(Ee);Fe(o,Ee),m("message_delta",{delta:{stop_reason:we?"tool_use":"end_turn"},usage:Be?{input_tokens:Be.input_tokens,output_tokens:Be.output_tokens}:{output_tokens:$}}),m("message_stop",{}),n.end();return}let b="",E="",w=[],g=new qe;for await(let k of r.track(p,c,o)){w.push(k),k.type==="response.output_text.delta"&&typeof k.delta=="string"&&(b+=k.delta),k.type==="response.reasoning_text.delta"&&typeof k.delta=="string"&&(E+=k.delta);let $=at(k);if($&&g.add($),k.type==="error"){let j=k.error,W=j?.type==="invalid_request_error";$e(n,W?400:502,W?"invalid_request_error":"api_error",j?.message??JSON.stringify(k));return}if(n.destroyed)break}if(n.destroyed)return;let v=Pe(w);Fe(o,v);let _;try{_=Tr(v),_.length===0&&g.size>0&&(_=Tr({output:g.items()}))}catch(k){$e(n,502,"api_error",String(k instanceof Error?k.message:k));return}let C=Ze(l,_.length,b);if(C){$e(n,502,"api_error",Qe(c,C));return}let R=[],O=E||yi(v);O&&R.push({type:"thinking",thinking:O}),(b||_.length===0)&&R.push({type:"text",text:b}),R.push(..._),ue(n,200,{id:a,type:"message",role:"assistant",content:R,model:i,stop_reason:_.length>0?"tool_use":"end_turn",usage:Rn(v)??{input_tokens:0,output_tokens:0}})}function fl(t){Ie(process.stdout,t)}function wi(t){let{clients:e,apiKey:n,identity:r,statusOf:o}=t,s=typeof e=="function"?e:()=>e,i=t.requestLog??fl,a=t.observe,u=new Ye;return(d,c)=>{let l=new URL(d.url??"/","http://localhost"),p=l.pathname==="/v1/messages"||l.pathname.startsWith("/v1/messages/"),m=(v,_,C,R,O)=>p?$e(c,v,R,_):le(c,v,_,C,O),b=cr(l.pathname),E=l.pathname.startsWith("/partners/"),w=b||E?{id:Rt(),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,sessionId:null,timing:null}:null;w&&(c.setHeader(Ps,w.id),c.once("close",()=>{let v=Us({turn:w,method:d.method??"",path:l.pathname,res:c});i(Ls(v)),a?.(v),jo({record:v,ledger:t.ledger??null,req:d})}));let g=()=>Cs(l.pathname,w);(async()=>{if(d.method==="GET"&&l.pathname==="/healthz"){ue(c,200,{ok:!0});return}if(d.method==="GET"&&l.pathname==="/stats"){ue(c,200,r?{identity:r,...u.snapshot()}:u.snapshot());return}if(n&&(d.headers.authorization??"")!==`Bearer ${n}`){m(401,"invalid local proxy api key","authentication_error","authentication_error");return}let v=l.pathname===zt?"canonical":l.pathname===bt?"huggingface":null;if(v){if(d.method!=="POST"){m(405,`${l.pathname} is a POST batch lookup`,"invalid_request_error","invalid_request_error");return}let O=t.usage;if(!O){m(501,`${l.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 k=O.authorize(d),$;try{$=await mi(d,Wo)}catch(F){if(F instanceof Ut){m(413,F.message,"request_too_large","request_too_large");return}m(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let j=F=>O.lookup(k,F),W;try{if(v==="canonical"){let F=Jo($),de=await j(F);Yt(de,F),W={requests:de}}else W=await Yo($,j)}catch(F){if(F instanceof je){m(F.status,F.message,"invalid_request_error","invalid_request_error");return}throw F}ue(c,200,W);return}if(l.pathname.startsWith("/partners/")){m(404,`no partner adapter for ${d.method} ${l.pathname} \u2014 the partner surface currently carries ${bt}`,"invalid_request_error","not_found_error");return}let _=await s(d);if(w&&(w.org=_.tenant??null),d.method==="GET"&&l.pathname==="/v1/models"){if(l.searchParams.get("format")==="openrouter"){if(!_.openRouterModels){m(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}ue(c,200,await _.openRouterModels());return}ue(c,200,await _.listModels());return}if(d.method==="GET"&&l.pathname===Ys){if(!_.listImageModels){m(501,"GET /v1/images/models needs a router-backed client (listImageModels)","not_implemented","not_implemented_error");return}ue(c,200,await _.listImageModels());return}if(zs(l.pathname)){if(!_.supportsImages){m(501,`the image lanes need a router-backed client (supportsImages) to gate model capability for ${l.pathname}`,"not_implemented","not_implemented_error");return}let O={clients:_,imageModels:_.supportsImages,stats:u,turn:g()};l.pathname===wn?await ii(d,c,O):await si(d,c,O);return}if(ci(l.pathname)){if(!_.supportsSpeech){m(501,`the speech lane needs a router-backed client (supportsSpeech) to gate model capability for ${Mt}`,"not_implemented","not_implemented_error");return}let O={clients:_,speechModels:_.supportsSpeech,stats:u,turn:g()};await pi(d,c,O);return}if(d.method!=="POST"){m(404,`no route for ${d.method} ${l.pathname}`,"invalid_request_error","not_found_error");return}let C;try{C=await mi(d)}catch(O){if(O instanceof Ut){m(413,O.message,"request_too_large","request_too_large");return}m(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let R=Buffer.byteLength(JSON.stringify(C));if(l.pathname==="/v1/responses"){u.request("responses",R,R),await rl(C,_,Et(_),c,u,g());return}if(l.pathname==="/v1/chat/completions"){u.request("chat_completions",R,R),await ol(C,_,c,u,g());return}if(l.pathname==="/v1/systemone"){u.request("systemone",R,R),await il(C,_,c,u,g());return}if(l.pathname==="/v1/messages"){u.request("messages",R,R),await pl(C,_,c,u,g());return}if(l.pathname==="/v1/messages/count_tokens"){u.request("count_tokens",R,R),$e(c,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}m(404,`no route for POST ${l.pathname}`,"invalid_request_error","not_found_error")})().catch(v=>{let _=String(v instanceof Error?v.message:v);if(!c.headersSent){let C=o?.(v)??null;if(C){m(C.status,_,C.openaiType,C.anthropicType);return}if(v instanceof On.SessionAllocationError){let R=cn(v.code);if(R){let O=v.retryAfterSeconds??(R.status===429?30:void 0);O!==void 0&&c.setHeader("Retry-After",String(O)),m(R.status,_r(R,v.code??void 0),R.openaiType,R.anthropicType,R.code);return}}if(v instanceof On.SessionFailedError){let R=pn(v);if(R){let O=R.status===429?30:void 0;O!==void 0&&c.setHeader("Retry-After",String(O)),m(R.status,_r(R,v.status.sessionId?`session ${v.status.sessionId}`:void 0),R.openaiType,R.anthropicType,R.code);return}}if(v instanceof ee){m(502,_,"upstream_error","api_error");return}m(500,_,"proxy_error","api_error","proxy_error");return}w&&(w.streamError=!0),c.destroyed||(p?c.write(`event: error
57
- data: ${JSON.stringify({type:"error",error:{type:"api_error",message:_}})}
56
+ `)};if(t.stream===!0){let x=r.track(y,l,o);Br(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,B="",q=0,V="none",de=()=>V,fe=-1,se=-1,ie=new We,ke=[],Z=()=>{V!=="none"&&(V="none",c("content_block_stop",{index:fe}))},Se=(F,me)=>{Z(),V=F,fe=q++,c("content_block_start",{index:fe,content_block:me})};for await(let F of x){if(n.destroyed)break;ke.push(F);let me=mt(F);if(F.type==="response.reasoning_text.delta"&&typeof F.delta=="string")de()!=="thinking"&&Se("thinking",{type:"thinking",thinking:""}),c("content_block_delta",{index:fe,delta:{type:"thinking_delta",thinking:F.delta}});else if(F.type==="response.output_text.delta"&&typeof F.delta=="string")de()!=="text"&&Se("text",{type:"text",text:""}),L+=1,B+=F.delta,c("content_block_delta",{index:fe,delta:{type:"text_delta",text:F.delta}});else if(me){let{call:Ce}=ie.add(me);(de()!=="tool"||me.tool_index!==se)&&(se=me.tool_index,Se("tool",{type:"tool_use",id:Ce.id??`call_${me.tool_index}`,name:Ce.name??"",input:{}})),me.delta&&c("content_block_delta",{index:fe,delta:{type:"input_json_delta",partial_json:me.delta}})}else if(F.type==="error"){o.streamError=!0,c("error",{error:{type:"api_error",message:JSON.stringify(F)}}),n.end();return}}if(n.destroyed)return;let Ae=Me(ke),Ke=ft(Ae),H=st(p,ie.size+Ke.length,B);if(H){o.streamError=!0,c("error",{error:{type:"api_error",message:ot(l,H)}}),n.end();return}Z();let I=[];if(ie.size===0)try{I=Fr(Ae)}catch(F){o.streamError=!0,c("error",{error:{type:"api_error",message:String(F instanceof Error?F.message:F)}}),n.end();return}q===0&&I.length===0&&(Se("text",{type:"text",text:""}),Z()),I.forEach(F=>{let me=q++;c("content_block_start",{index:me,content_block:{...F,input:{}}}),c("content_block_delta",{index:me,delta:{type:"input_json_delta",partial_json:JSON.stringify(F.input??{})}}),c("content_block_stop",{index:me})});let Y=ie.size>0||I.length>0,Ie=Pn(Ae);He(o,Ae),c("message_delta",{delta:{stop_reason:Y?"tool_use":"end_turn"},usage:Ie?{input_tokens:Ie.input_tokens,output_tokens:Ie.output_tokens}:{output_tokens:L}}),c("message_stop",{}),n.end();return}let m="",w="",R=[],h=new We;for await(let x of r.track(y,l,o)){R.push(x),x.type==="response.output_text.delta"&&typeof x.delta=="string"&&(m+=x.delta),x.type==="response.reasoning_text.delta"&&typeof x.delta=="string"&&(w+=x.delta);let L=mt(x);if(L&&h.add(L),x.type==="error"){let B=x.error,q=B?.type==="invalid_request_error";Ue(n,q?400:502,q?"invalid_request_error":"api_error",B?.message??JSON.stringify(x));return}if(n.destroyed)break}if(n.destroyed)return;let v=Me(R);He(o,v);let b;try{b=Fr(v),b.length===0&&h.size>0&&(b=Fr({output:h.items()}))}catch(x){Ue(n,502,"api_error",String(x instanceof Error?x.message:x));return}let $=st(p,b.length,m);if($){Ue(n,502,"api_error",ot(l,$));return}let S=[],E=w||Ti(v);E&&S.push({type:"thinking",thinking:E}),(m||b.length===0)&&S.push({type:"text",text:m}),S.push(...b),ue(n,200,{id:a,type:"message",role:"assistant",content:S,model:i,stop_reason:b.length>0?"tool_use":"end_turn",usage:Pn(v)??{input_tokens:0,output_tokens:0}})}function Ml(t){$e(process.stdout,t)}function Pi(t){let{clients:e,apiKey:n,identity:r,statusOf:o}=t,s=typeof e=="function"?e:()=>e,i=t.requestLog??Ml,a=t.observe,u=new nt;return(d,l)=>{let p=new URL(d.url??"/","http://localhost"),y=p.pathname==="/v1/messages"||p.pathname.startsWith("/v1/messages/"),c=(v,b,$,S,E)=>y?Ue(l,v,S,b):ge(l,v,b,$,E),m=_r(p.pathname),w=p.pathname.startsWith("/partners/"),R=m||w?{id:Ct(),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,sessionId:null,timing:null}:null;R&&(l.setHeader(Ds,R.id),l.once("close",()=>{let v=qs({turn:R,method:d.method??"",path:p.pathname,res:l});i(Hs(v)),a?.(v),Ko({record:v,ledger:t.ledger??null,req:d})}));let h=()=>Fs(p.pathname,R);(async()=>{if(d.method==="GET"&&p.pathname==="/healthz"){ue(l,200,{ok:!0});return}if(d.method==="GET"&&p.pathname==="/stats"){ue(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 v=p.pathname===en?"canonical":p.pathname===It?"huggingface":null;if(v){if(d.method!=="POST"){c(405,`${p.pathname} is a POST batch lookup`,"invalid_request_error","invalid_request_error");return}let E=t.usage;if(!E){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=E.authorize(d),L;try{L=await Oi(d,Zo)}catch(V){if(V instanceof qt){c(413,V.message,"request_too_large","request_too_large");return}c(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let B=V=>E.lookup(x,V),q;try{if(v==="canonical"){let V=es(L),de=await B(V);tn(de,V),q={requests:de}}else q=await rs(L,B)}catch(V){if(V instanceof Ve){c(V.status,V.message,"invalid_request_error","invalid_request_error");return}throw V}ue(l,200,q);return}if(p.pathname.startsWith("/partners/")){c(404,`no partner adapter for ${d.method} ${p.pathname} \u2014 the partner surface currently carries ${It}`,"invalid_request_error","not_found_error");return}let b=await s(d);if(R&&(R.org=b.tenant??null),d.method==="GET"&&p.pathname==="/v1/models"){if(p.searchParams.get("format")==="openrouter"){if(!b.openRouterModels){c(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}ue(l,200,await b.openRouterModels());return}ue(l,200,await b.listModels());return}if(d.method==="GET"&&p.pathname===oi){if(!b.listImageModels){c(501,"GET /v1/images/models needs a router-backed client (listImageModels)","not_implemented","not_implemented_error");return}ue(l,200,await b.listImageModels());return}if(ri(p.pathname)){if(!b.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 E={clients:b,imageModels:b.supportsImages,stats:u,turn:h()};p.pathname===Rn?await mi(d,l,E):await fi(d,l,E);return}if(d.method==="GET"&&p.pathname===ki){if(!b.listSpeechToTextModels){c(501,"GET /v1/audio/models needs a router-backed client (listSpeechToTextModels)","not_implemented","not_implemented_error");return}ue(l,200,await b.listSpeechToTextModels());return}if(bi(p.pathname)){if(!b.supportsTranscription){c(501,`the transcription lane needs a router-backed client (supportsTranscription) to gate model capability for ${An}`,"not_implemented","not_implemented_error");return}let E={clients:b,transcriptionModels:b.supportsTranscription,stats:u,turn:h()};await xi(d,l,E);return}if(_i(p.pathname)){if(!b.supportsSpeech){c(501,`the speech lane needs a router-backed client (supportsSpeech) to gate model capability for ${jt}`,"not_implemented","not_implemented_error");return}let E={clients:b,speechModels:b.supportsSpeech,stats:u,turn:h()};await vi(d,l,E);return}if(d.method!=="POST"){c(404,`no route for ${d.method} ${p.pathname}`,"invalid_request_error","not_found_error");return}let $;try{$=await Oi(d)}catch(E){if(E instanceof qt){c(413,E.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=Buffer.byteLength(JSON.stringify($));if(p.pathname==="/v1/responses"){u.request("responses",S,S),await Rl($,b,Mt(b),l,u,h());return}if(p.pathname==="/v1/chat/completions"){u.request("chat_completions",S,S),await xl($,b,l,u,h());return}if(p.pathname==="/v1/systemone"){u.request("systemone",S,S),await Ol($,b,l,u,h());return}if(p.pathname==="/v1/messages"){u.request("messages",S,S),await $l($,b,l,u,h());return}if(p.pathname==="/v1/messages/count_tokens"){u.request("count_tokens",S,S),Ue(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(v=>{let b=String(v instanceof Error?v.message:v);if(!l.headersSent){let $=o?.(v)??null;if($){c($.status,b,$.openaiType,$.anthropicType);return}if(v instanceof Mn.SessionAllocationError){let S=gn(v.code);if(S){let E=v.retryAfterSeconds??(S.status===429?30:void 0);E!==void 0&&l.setHeader("Retry-After",String(E)),c(S.status,Or(S,v.code??void 0),S.openaiType,S.anthropicType,S.code);return}}if(v instanceof Mn.SessionFailedError){let S=hn(v);if(S){let E=S.status===429?30:void 0;E!==void 0&&l.setHeader("Retry-After",String(E)),c(S.status,Or(S,v.status.sessionId?`session ${v.status.sessionId}`:void 0),S.openaiType,S.anthropicType,S.code);return}}if(v instanceof te){c(502,b,"upstream_error","api_error");return}c(500,b,"proxy_error","api_error","proxy_error");return}R&&(R.streamError=!0),l.destroyed||(y?l.write(`event: error
57
+ data: ${JSON.stringify({type:"error",error:{type:"api_error",message:b}})}
58
58
 
59
- `):c.write(`data: ${JSON.stringify({error:{message:_,type:"upstream_error"}})}
59
+ `):l.write(`data: ${JSON.stringify({error:{message:b,type:"upstream_error"}})}
60
60
 
61
- `)),c.end()})}}var In=(t,e)=>Object.assign(new Error(e),{status:t});function _i(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}async function vi(t,e,n){if(!t.supportsAudio)throw In(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 r;try{r=await t.supportsAudio(e)}catch(i){throw i instanceof z?In(404,i.message):i}if(!r)throw In(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.openAudio)throw In(501,"an audio-capable model needs a router-backed client (openAudio) to open its native session audio bridge \u2014 refusing loudly rather than binding a voiceless session");let o={audio:await t.openAudio(e,n)},s={clients:t,voice:r,audioSession:o};return e!==void 0&&(s.model=e),s}S();var Pn=require("@urun-sh/core"),ke=class extends Error{constructor(n,r=401){super(n);this.status=r;this.name="ProxyAuthError"}status},ct=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function Nt(t){let e=(t??"").trim();if(e==="")throw new ke("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let n=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!n)throw new ke("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return n[1].trim()}var ml=60,hl=6e4,gl=4096;function yl(t){return t===401||t===403}var Tn=class{constructor(e){this.opts=e;this.mint=e.mint??Pn.createClientToken,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new ke("empty Authorization bearer token");let n=this.cache.get(e);if(n&&this.now()-n.at<hl)return{apiKey:e,orgId:n.orgId};let r;try{r=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:ml})).orgId}catch(o){throw o instanceof Pn.ClientTokenError&&yl(o.status)?(this.cache.delete(e),new ke("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new ct(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${o instanceof Error?o.message:String(o)}`)}if(!r)throw new ct("the control plane verified the API key but returned no org_id \u2014 refusing to route a request with no tenancy (control-plane contract violation)");if(this.cache.size>=gl){let o=this.cache.keys().next();o.done||this.cache.delete(o.value)}return this.cache.set(e,{orgId:r,at:this.now()}),{apiKey:e,orgId:r}}clear(){this.cache.clear()}};S();var Dn=require("crypto");S();var Dt=require("@urun-sh/core");S();S();S();S();function bi(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 ki(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),r}S();S();function Si(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 wl="llm-resp";async function*$r(t,e){let n=`${wl}:${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 Si(i);return}}}finally{try{t.doc("llm").set({requests:{[e]:null}})}catch{}}}var $n=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=ki(e,n,this.consumerId);return this.write(r),$r(this.session,n)})}sendResponseCreate(e,n,r){return this.writeGated(r,()=>{let o=bi(e,n,r,this.consumerId);return this.write(o),$r(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 Ri=0;function _l(){return Ri+=1,`req_${Date.now().toString(36)}_${Ri.toString(36)}`}var Mn=class{transport;constructor(e){this.transport=new $n(e)}responses={create:async e=>{let n=_l(),r=await this.transport.sendResponses(e,n);return Object.assign((async function*(){yield*r})(),{requestId:n})}}};S();async function xi(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()}S();var Ei=require("@urun-sh/core"),Mr=2;function vl(t){let e=t?.type;return e==="response.created"||e==="response.in_progress"}function bl(t){let e=t?.type;return e==="response.completed"||e==="error"}function Oi(t){return async function(n,r){return(async function*(){for(let s=1;s<=Mr;s++){let{app:i,entry:a,identity:u}=await t.sessionFor(n.model);r?.({sessionId:t.sessionIdOf?.(a)??null,identity:u});let d=!1,c=!1;try{let l=await t.createStream(a,n);for await(let p of l){if(bl(p)&&(d=!0),vl(p)){if(s>1)continue}else c=!0;yield p}}catch(l){if(l instanceof Ei.SessionFailedError&&(l.kind==="never-live"||l.kind==="create-failed")){try{t.onAdmissionVerdict?.(i,a,l)}catch{}throw l}if(s<Mr&&!c){await t.evict(i,a);continue}throw l}if(d)return;if(s<Mr&&!c){await t.evict(i,a);continue}return}})()}}S();var kl=3,Sl="llm",Rl=new Set(["paused","error"]);function Ai(t,e){let n=t.doc(Sl),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=c=>{if(!s){s=c;for(let l of[...a])l(c);a.clear()}};return r&&u.push(n.onConnectionState(c=>{i&&(c.consecutiveFailures<kl||d(new ce(`the pooled session for app "${e}" is gone: its doc backhaul failed ${c.consecutiveFailures} consecutive times (last close ${c.lastCloseCode===null?"by provider liveness reap":`code ${c.lastCloseCode}`}${c.lastCloseReason?`, reason ${JSON.stringify(c.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`)))})),o&&u.push(t.onPhase(c=>{if(c.name==="live"){i=!0;return}i&&Rl.has(c.name)&&d(new ce(`the pooled session for app "${e}" lost its compute binding (phase "${c.name}"). The entry is evicted; retry to get a fresh session. The named session itself is not over \u2014 it stays attachable by name.`))})),{gone:()=>s,onGone(c){return s?(c(s),()=>{}):(a.add(c),()=>a.delete(c))},dispose(){a.clear();for(let c of u.splice(0))c()}}}async function*Ur(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 Ii(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 xl(t,e,n){let{baseUrl:r,orgId:o,auth:s}=t,i=typeof t.subject=="function"?t.subject():t.subject,a=n.startsWith(Ve)&&i!==void 0?`${i}:speech`:i,u=e.fnName??t.fnName,d=t.sessionStartOptions?.(n),c=e.sharedApp?{sharedApp:!0,...d}:d,l=E=>({session:E,responses:new Mn(E),gone:Ai(E,n)});if(s.lane==="jwt"){let E=(0,Dt.App)(e.appSlug,{baseUrl:r,orgId:o,jwt:s.jwt});return l(Ii(E,e.appSlug,u,c))}let{apiKey:p,gatewayUrl:m}=s,b=(0,Dt.App)(e.appSlug,{baseUrl:r,orgId:o,getAccessToken:async()=>(await(0,Dt.createClientToken)(p,{baseUrl:m,expiresIn:300,allowedFunctions:[`${e.appSlug}/${u}`],...a===void 0?{}:{subject:a},...e.sharedApp?{sharedApp:!0}:{}})).token});return l(Ii(b,e.appSlug,u,c))}function El(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 Ol(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 Al=new Set(["error","paused"]);function Nr(t){let{auth:e,catalog:n}=t,r=null,o=(i,a)=>{let u=xl(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 Vt({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"?()=>So({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,listCatalog:n?()=>xi(n):null,listPrices:n?()=>ao(n):null,shared:e.lane==="api-key"?()=>no({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,sessionKey:El});return r=s,t.defaultApp!==null&&t.statelessDirect!==!0&&s.seed(t.defaultApp,o({appSlug:t.defaultApp},gt({appSlug:t.defaultApp}))),s}async function Lr(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,e===void 0?void 0:{signal:e})}async function Ti(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 Pi(t){return t.reason instanceof Error?t.reason:new Error("the client socket closed")}function Dr(t,e=us,n,r){let o=new WeakMap,s=new WeakMap,i=new WeakMap,a=new WeakMap,u=l=>{let p=l.session.id;return typeof p=="string"&&p.length>0?p:null},d=(l,p)=>{let m=o.get(l);return m||(m=(async()=>{await Lr(l.session,p);let b=await is(l.session,await e());return Object.assign(b,{sessionId:l.session.id})})(),o.set(l,m),m.catch(()=>o.delete(l))),m},c=l=>{let p=a.get(l);return p||(p=(async()=>{let m=await d(l);return{appendInputAudio:b=>m.appendInputAudio(b),onOutputAudio:b=>m.onOutputAudio(b),sessionId:u(l)}})(),a.set(l,p),p.catch(()=>a.delete(l))),p};return{createResponse:Oi({sessionFor:l=>t.sessionFor(l),sessionIdOf:l=>u(l),evict:(l,p)=>t.evict(l,p),onAdmissionVerdict:(l,p,m)=>{Ol(m)&&(t.evict(l,p),n?.(l,m.kind==="never-live"?"never-live":"create-failed"))},createStream:async(l,p)=>(await Lr(l.session),Ur(await l.responses.responses.create(p),l.gone))}),listModels:()=>t.modelList(),openRouterModels:()=>t.openRouterModels(),supportsImages:(l,p)=>t.supportsImages(l,p),listImageModels:()=>t.imageModelList(),catalogIdentity:l=>t.catalogIdentityFor(l),servingAppSlug:async l=>await t.servingAppSlugFor(l),supportsAudio:l=>t.supportsAudio(l),supportsSpeech:l=>t.supportsSpeech(l),speechVoices:l=>t.speechVoices(l),openAudio:async(l,p)=>{let{entry:m}=await t.sessionFor(l);if(p?.aborted)throw await Ti(m.session),Pi(p);let b=d(m,p);return p===void 0?b:new Promise((E,w)=>{let g=()=>{Ti(m.session).catch(()=>{}),w(Pi(p))};p.addEventListener("abort",g,{once:!0});let v=()=>p.removeEventListener("abort",g);b.then(_=>{v(),E(_)},_=>{v(),w(_)})})},openAudioOn:async l=>c((await t.sessionForHandle(l)).entry),openVideo:async l=>{let{entry:p}=await t.sessionFor(l),m=s.get(p);return m||(m=as(p.session),s.set(p,m)),m},openVideoOut:async l=>{let{entry:p}=await t.sessionFor(l),m=i.get(p);return m||(m=ls(p.session),i.set(p,m)),m},sessionHandle:async l=>(await t.handleFor(l)).handle,speechSessionHandle:async l=>(await t.speechHandleFor(l)).handle,evictSpeechSession:async l=>{let{app:p,entry:m}=await t.sessionForHandle(l);await t.evict(p,m)},createResponseOn:async(l,p)=>{let{entry:m}=await t.sessionForHandle(l);return await Lr(m.session),Ur(await m.responses.responses.create(p),m.gone)},onSessionEnd:async(l,p)=>{let{entry:m}=await t.sessionForHandle(l),b=m.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 E=!1;return b.onPhase(w=>{if(E||!Al.has(w.name))return;E=!0;let g=b.endsAt??null;p({timeLeftMs:g?Math.max(0,g.getTime()-Date.now()):null,reason:w.reason??`session ${w.name}`})})},...r?{stateless:r}:{}}}var Il=512;function Fn(t){return`urun-inference-proxy-${(0,Dn.createHash)("sha256").update(t).digest("hex").slice(0,32)}`}var Un=class{constructor(e){this.opts=e}opts;tenants=new Map;label(e,n){return Object.assign(Object.create(e),{tenant:n.orgId})}build(e){if(this.opts.build){let a=this.opts.build(e);return{router:a.router,clients:this.label(a.clients,e)}}let n=0,r=Fn(e.apiKey),o=()=>{n+=1},s=Nr({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:tn,sharedOnlyListing:!0,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:()=>n===0?r:`${r}#${n}`,onPoolWedged:o,release:"end"}),i=this.opts.decisionIntake?new Sn({targetFor:a=>s.targetFor(a),apiUrl:this.opts.apiUrl,apiKey:e.apiKey,orgId:e.orgId,fnName:tn,secret:this.opts.decisionIntake.secret,...this.opts.decisionIntake.shared?{shared:this.opts.decisionIntake.shared}:{}}):void 0;return{router:s,clients:this.label(Dr(s,void 0,o,i),e)}}clientsFor(e){let n=this.tenants.get(e.apiKey);if(n)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,n),n.clients;for(;this.tenants.size>=Il;){let o=this.tenants.entries().next();if(o.done)break;let[s,i]=o.value;this.tenants.delete(s),i.router.closeAll().catch(()=>{})}let r=this.build(e);return this.tenants.set(e.apiKey,r),r.clients}get size(){return this.tenants.size}async closeAll(){let e=[...this.tenants.values()].map(r=>r.router);this.tenants.clear();let n=[];if(await Promise.all(e.map(async r=>{try{await r.closeAll()}catch(o){n.push(String(o instanceof Error?o.message:o))}})),n.length>0)throw new Error(`failed to close ${n.length} tenant backhaul(s): ${n.join("; ")}`)}},Ci=512,Tl=1024,Ln=class extends Error{constructor(){super(`conversation capacity reached (${Ci} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function Pl(t){return{sessionStartOptions:e=>{if(e.startsWith(Ve))return;let n=t.get(e);return n===void 0?void 0:{sessionId:n}},onSessionOpen:(e,n)=>{e.startsWith(Ve)||t.set(e,n)}}}function Cl(t,e,n,r){let o=Nr({baseUrl:t.baseUrl,orgId:e.orgId,defaultApp:null,fnName:tn,sharedOnlyListing:!0,apiUrl:t.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:t.baseUrl},catalog:t.catalog,subject:n.subject,release:"detach",...Pl(r)});return{clients:Dr(o),release:async()=>{await o.closeAll()}}}var Nn=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,n){let r={id:n,subject:`${Fn(e.apiKey)}:live:${n}`},o=new Map,s=this.opts.build?this.opts.build(e,r):Cl(this.opts,e,r,o);return{id:n,backhaul:s,attachments:0,nativeRefs:o}}attachEntry(e,n){let r=this.byCaller.get(e.apiKey);r===void 0&&(r=new Map,this.byCaller.set(e.apiKey,r));let o=r.get(n);if(o!==void 0)return r.delete(n),r.set(n,o),o.attachments+=1,o;this.evictOverflow();let s=this.build(e,n);return s.attachments=1,r.set(n,s),s}get conversationCount(){let e=0;for(let n of this.byCaller.values())e+=n.size;return e}evictOverflow(){for(;this.conversationCount>=Ci;){let e=this.byCaller.keys().next();if(e.done)throw new Ln;let n=e.value,r=this.byCaller.get(n),o=!1;for(let[s,i]of r)if(!(i.attachments>0)){r.delete(s),this.dropHandleIndexFor(n,s),i.backhaul.release().catch(()=>{}),o=!0;break}if(!o)throw r.size===0&&this.byCaller.delete(n),new Ln;r.size===0&&this.byCaller.delete(n)}}dropHandleIndexFor(e,n){for(let[r,o]of this.byHandle)o.apiKey===e&&o.conversationId===n&&this.byHandle.delete(r)}connectionFor(e){let n=null,r=!1,o=()=>(n===null&&(n=this.attachEntry(e,(0,Dn.randomUUID)())),n),s={createResponse:(i,a)=>o().backhaul.clients.createResponse(i,a),catalogIdentity:i=>o().backhaul.clients.catalogIdentity(i),servingAppSlug:i=>{let a=o().backhaul.clients.servingAppSlug;if(a===void 0)throw new Error("the conversation backhaul wires no servingAppSlug seam \u2014 the caller-org app a request resolves to cannot be known");return a(i)},listModels:()=>o().backhaul.clients.listModels(),sessionHandle:i=>o().backhaul.clients.sessionHandle(i),createResponseOn:(i,a)=>o().backhaul.clients.createResponseOn(i,a),onSessionEnd:(i,a)=>o().backhaul.clients.onSessionEnd(i,a)};return s.openAudio=async(i,a)=>{let u=o().backhaul.clients.openAudio;if(u===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return u(i,a)},s.openVideo=async i=>{let a=o().backhaul.clients.openVideo;if(a===void 0)throw new Error("the conversation backhaul wires no openVideo seam \u2014 video-in is unavailable on this deployment");return a(i)},s.openVideoOut=async i=>{let a=o().backhaul.clients.openVideoOut;if(a===void 0)throw new Error("the conversation backhaul wires no openVideoOut seam \u2014 video-out is unavailable on this deployment");return a(i)},s.supportsAudio=async i=>{let a=o().backhaul.clients.supportsAudio;if(a===void 0)throw new Error("the conversation backhaul wires no supportsAudio seam \u2014 the realtime lane cannot gate audio modality on this deployment");return a(i)},s.supportsSpeech=async i=>{let a=o().backhaul.clients.supportsSpeech;if(a===void 0)throw new Error("the conversation backhaul wires no supportsSpeech seam \u2014 the speech lane cannot gate model capability on this deployment");return a(i)},s.speechVoices=async i=>{let a=o().backhaul.clients.speechVoices;if(a===void 0)throw new Error("the conversation backhaul wires no speechVoices seam \u2014 the speech lane cannot resolve the voice set /v1/models publishes on this deployment");return a(i)},s.speechSessionHandle=async i=>{let a=o().backhaul.clients.speechSessionHandle;if(a===void 0)throw new Error("the conversation backhaul wires no speechSessionHandle seam \u2014 the speech lane cannot resolve its own pooled session on this deployment");return a(i)},s.openAudioOn=async i=>{let a=o().backhaul.clients.openAudioOn;if(a===void 0)throw new Error("the conversation backhaul wires no openAudioOn seam \u2014 the speech lane cannot open its pinned native session audio on this deployment");return a(i)},s.evictSpeechSession=async i=>{let a=o().backhaul.clients.evictSpeechSession;if(a===void 0)throw new Error("the conversation backhaul wires no evictSpeechSession seam \u2014 the speech lane cannot retire its pinned speech session after a failed turn on this deployment");return a(i)},{clients:s,bindResumeHandle:i=>{let a=n;if(a===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=Tl;){let u=this.byHandle.keys().next();if(u.done)break;this.byHandle.delete(u.value)}this.byHandle.set(i,{apiKey:e.apiKey,conversationId:a.id})},clientsForResume:async i=>{let a=this.byHandle.get(i),u=a===void 0?void 0:this.byCaller.get(a.apiKey),d=a===void 0||u===void 0?void 0:u.get(a.conversationId);if(a===void 0||d===void 0||a.apiKey!==e.apiKey)throw new ce("the conversation behind this resume handle is gone (evicted at capacity, or the replica restarted) or the handle is not yours \u2014 reconnect without a handle to start a new conversation");return n=d,d.attachments+=1,d.backhaul.clients},release:async()=>{if(r)return;r=!0;let i=n;n=null,i!==null&&(i.attachments-=1,!(i.attachments>0)&&await i.backhaul.release().catch(()=>{}))}}}async detachAll(){let e=[];for(let r of this.byCaller.values())for(let o of r.values())e.push(o.backhaul.release());this.byCaller.clear(),this.byHandle.clear();let n=[];if(await Promise.all(e.map(async r=>{try{await r}catch(o){n.push(String(o instanceof Error?o.message:o))}})),n.length>0)throw new Error(`failed to detach ${n.length} conversation backhaul(s): ${n.join("; ")}`)}};S();var Bn="/inference-usage",$i=25e3;var $l=new Set(["Error","TypeError","SyntaxError","TimeoutError"]);function Mi(t){if(!(t instanceof Error))return`a ${typeof t}`;let e=$l.has(t.name)?t.name:"Error",n=t.cause?.code;return typeof n=="string"&&/^[A-Z][A-Z0-9_]{0,31}$/.test(n)?`${e} (${n})`:e}var jn=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async lookup(e,n){let r=`${this.opts.apiUrl.replace(/\/+$/,"")}${Bn}`,o;try{o=await this.fetchImpl(r,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify({inference_ids:n}),signal:AbortSignal.timeout($i)})}catch(i){throw new ee(`the uRun usage surface (POST ${Bn}) could not be reached within ${$i}ms: ${Mi(i)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new ke("the uRun org API key was rejected by the uRun control plane \u2014 check the key and that it is not revoked");if(!o.ok){let i=await o.text().catch(()=>null),a=i===null?"body unreadable":`${i.length}-character body`;throw new ee(`the uRun usage surface (POST ${Bn}) answered HTTP ${o.status} (${a})`)}let s;try{s=await o.json()}catch(i){throw new ee(`the uRun usage surface (POST ${Bn}) answered HTTP ${o.status} with a body that is not JSON: ${Mi(i)}`)}return zo(s,this.opts.onRejectedRow)}};S();var Ml="/inference-ledger",Ul=1e4,qn=class extends Error{ledgerFailureReason;constructor(e,n="duplicate"){super(e),this.name="LedgerDuplicateError",this.ledgerFailureReason=n}},Br=class extends Error{constructor(n,r){super(r);this.ledgerAlreadyWritten=n;this.name="LedgerAlreadyWrittenError"}ledgerAlreadyWritten},me=class extends Error{ledgerFailureReason="surface";constructor(e){super(e),this.name="LedgerSurfaceError"}},jr=class extends ke{ledgerFailureReason="auth";constructor(e){super(e),this.name="LedgerAuthError"}},Ll=[["inference_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["model_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["variant",t=>t===null||typeof t=="string"],["task",t=>typeof t=="string"&&/[^\s]/.test(t)],["http_status",t=>typeof t=="number"&&Number.isInteger(t)],["started_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["completed_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["prompt_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["completion_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["output_units",Ui],["cost_nano_usd",Ui],["price_version",t=>t===null||typeof t=="string"]];function Nl(t){for(let[e,n]of Ll){if(!(e in t))return`${e} (absent)`;if(!n(t[e]))return`${e} (${JSON.stringify(t[e])})`}return null}function Dl(t){let e;try{e=JSON.parse(t)}catch{return{status:"unavailable"}}if(e===null||typeof e!="object"||Array.isArray(e))return{status:"unavailable"};let n=e.existing_status;if(n==="not_in_this_org")return{status:"not_in_this_org"};if(n!=="found")return{status:"unavailable"};let r=e.existing;return r===null||typeof r!="object"||Array.isArray(r)?{status:"unavailable"}:Nl(r)!==null?{status:"unavailable"}:{status:"found",row:r}}function Ui(t){return t===null?!0:typeof t=="number"?Number.isFinite(t):typeof t!="string"?!1:/[^\s]/.test(t)&&Number.isFinite(Number(t))}function Fr(t,e){return t===null?e===null:e===null?!1:Number(e)===t}function Li(t,e){if(typeof e!="string")return!1;let n=Date.parse(t),r=Date.parse(e);return Number.isFinite(n)&&Number.isFinite(r)&&n===r}function Fl(t,e){let n=e;return n.inference_id===t.inference_id&&n.model_id===t.model_id&&(n.variant??null)===t.variant&&n.task===t.task&&n.http_status===t.http_status&&Li(t.started_at,n.started_at)&&Li(t.completed_at,n.completed_at)&&Fr(t.prompt_tokens,n.prompt_tokens)&&Fr(t.completion_tokens,n.completion_tokens)&&Fr(t.output_units,n.output_units)}function Bl(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new me(`${e} answered a "recorded" that is not an object: ${JSON.stringify(t)}`);let n=t,r=n.inference_id;if(typeof r!="string"||r==="")throw new me(`${e} answered a row with no inference_id: ${JSON.stringify(t)}`);if(!("cost_nano_usd"in n))throw new me(`${e} answered a row with no cost_nano_usd field: ${JSON.stringify(t)}`);let o=n.cost_nano_usd;if(o!=null&&!(typeof o=="number"&&Number.isInteger(o)&&o>=0))throw new me(`${e} answered cost_nano_usd ${JSON.stringify(o)}, which is neither a non-negative integer nor null`);let s=d=>{if(!(d in n))throw new me(`${e} answered a row with no ${d} field: ${JSON.stringify(t)}`);let c=n[d];if(c===null)return null;if(typeof c!="string")throw new me(`${e} answered a non-string ${d}: ${JSON.stringify(c)}`);if(!/[^\s]/.test(c))throw new me(`${e} answered a BLANK ${d} (${JSON.stringify(c)}) \u2014 it satisfies "is a string" while naming nothing, which is exactly what inference_requests_priced_attribution_check exists to forbid`);return c},i=s("price_version"),a=s("unpriced_reason"),u=o!=null;if(u&&(i===null||a!==null))throw new me(`${e} answered a PRICED row (cost_nano_usd ${String(o)}) that is not cleanly attributed \u2014 price_version ${JSON.stringify(i)}, unpriced_reason ${JSON.stringify(a)}`);if(!u&&(i!==null||a===null))throw new me(`${e} answered an UNPRICED row that does not say why \u2014 price_version ${JSON.stringify(i)}, unpriced_reason ${JSON.stringify(a)}. NULL means NOT YET PRICED, and the reason is how an operator learns that traffic is being served which nobody can bill.`);return{inference_id:r,cost_nano_usd:o===void 0?null:o,price_version:i,unpriced_reason:a}}var Hn=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async write(e,n){let r=`${this.opts.apiUrl.replace(/\/+$/,"")}${Ml}`,o;try{o=await this.fetchImpl(r,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify(n),signal:AbortSignal.timeout(Ul)})}catch(u){throw new me(`the uRun ledger surface at ${r} could not be reached: ${u instanceof Error?u.message:String(u)}`)}if(o.status===409){let u=await o.text().catch(()=>""),d=Dl(u);throw d.status==="found"&&Fl(n,d.row)?new Br(d.row,`the uRun control plane already holds THIS PROXY'S OWN row for inference_id ${n.inference_id} \u2014 every billable field matches the row being re-sent, so an earlier attempt landed and this retry is a no-op. Nothing was written and nothing was overwritten.`):d.status==="not_in_this_org"?new qn(`inference_id ${n.inference_id} is ALREADY TAKEN by a ledger row this org cannot read. The id space is a global primary key, so this is not a retry and cannot be this proxy's own write \u2014 either two orgs collided on a uuid, or a row was written for this id by somebody else.`,"duplicate_foreign"):new qn(`the uRun control plane already holds a ledger row for inference_id ${n.inference_id} that is NOT the one being written${d.status==="unavailable"?" (and it could not be read back to compare)":""} \u2014 nothing was written and nothing was overwritten. Either this proxy wrote twice, or a row was fabricated ahead of it (this write is authenticated by the CALLER'S OWN org key). ${u.slice(0,400)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new jr("the uRun org API key was rejected by the uRun control plane when writing the inference ledger \u2014 the request was served but produced no billable row");if(!o.ok){let u=await o.text().catch(d=>`<body unreadable: ${d instanceof Error?d.message:String(d)}>`);throw new me(`the uRun ledger surface at ${r} answered HTTP ${o.status}: ${u.slice(0,400)}`)}let s;try{s=await o.json()}catch(u){throw new me(`the uRun ledger surface at ${r} answered a body that is not JSON: ${u instanceof Error?u.message:String(u)}`)}let i=s?.recorded;if(i===void 0)throw new me(`the uRun ledger surface at ${r} answered a body with no "recorded" field`);let a=Bl(i,`the uRun ledger surface at ${r}`);if(a.inference_id!==n.inference_id)throw new me(`the uRun ledger surface at ${r} answered for inference_id ${a.inference_id} when asked about ${n.inference_id}`);return a}};var jl=5e3,ql=3e3,Hl="/api/client-tokens";function qr(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function Ni(t,e,n,r){qr(t,e,{error:{message:n,type:r}})}async function Gl(t,e=fetch){let n=`${t.replace(/\/+$/,"")}${Hl}`;try{let r=await e(n,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(ql)});return r.status===404?{ready:!1,detail:`${n} answered 404 \u2014 this is not a uRun control plane`}:r.status>=500?{ready:!1,detail:`${n} answered HTTP ${r.status}`}:{ready:!0,detail:`${n} answered HTTP ${r.status}`}}catch(r){return{ready:!1,detail:`${n} unreachable: ${r instanceof Error?r.message:String(r)}`}}}function Fi(t){let e=t.resolver??new Tn({gatewayUrl:t.baseUrl}),n=t.decisionSecret??"",r=t.sharedOrgKey&&t.sharedOrgId?{apiKey:t.sharedOrgKey}:void 0,o=t.registry??new Un({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog,...n!==""?{decisionIntake:{secret:n,...r?{shared:r}:{}}}:{}});n!==""&&process.stderr.write(`inference-proxy: stateless direct path ENABLED \u2014 caller-org decisions dial ready runtimes directly (no session mint)
62
- `);let s=t.probeControlPlane??(()=>Gl(t.baseUrl)),i=t.usageClient??new jn({apiUrl:t.apiUrl}),a=t.ledgerClient??new Hn({apiUrl:t.apiUrl}),u=new en(t.defaultMetrics===void 0?{}:{defaultMetrics:t.defaultMetrics}),d=async g=>e.resolve(Nt(g.headers.authorization)),c=g=>g instanceof ke?{status:g.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:g instanceof ct?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,l=wi({clients:async g=>o.clientsFor(await d(g)),usage:{authorize:g=>Nt(g.headers.authorization),lookup:(g,v)=>i.lookup(g,v)},statusOf:c,...t.requestLog?{requestLog:t.requestLog}:{},observe:g=>u.observe(g),ledger:{write:(g,v)=>a.write(Nt(g.headers.authorization),v),observe:g=>u.observeLedger(g),...t.ledgerLog?{report:t.ledgerLog}:{}}}),p=null,m=(0,Di.createServer)((g,v)=>{let _=new URL(g.url??"/","http://localhost");(async()=>{if(g.method==="GET"&&_.pathname==="/healthz"){qr(v,200,{ok:!0});return}if(g.method==="GET"&&_.pathname==="/readyz"){let O=Date.now();if(!p||O-p.at>=jl){let k=await s();p={at:O,...k}}qr(v,p.ready?200:503,{ok:p.ready,control_plane:p.detail});return}let C=_.pathname==="/v1"||_.pathname.startsWith("/v1/"),R=_.pathname.startsWith("/partners/");if(!C&&!R){Ni(v,404,`no route for ${g.method} ${_.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface, the partner adapter surface (/partners/...), the WS upgrade paths (/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent and /v1/realtime), plus /healthz and /readyz`,"invalid_request_error");return}l(g,v)})().catch(C=>{if(!v.headersSent){Ni(v,500,String(C instanceof Error?C.message:C),"proxy_error");return}v.end()})}),b=t.conversations??new Nn({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),E=[lr(m,{authenticate:async g=>{let v=await e.resolve(g),_=b.connectionFor(v);return{clients:_.clients,scope:Fn(v.apiKey),onBindResumeHandle:_.bindResumeHandle,clientsForResume:_.clientsForResume,release:_.release}}}),wr(m,{clients:async g=>o.clientsFor(await d(g)),statusOf:c}).close,yn(m,{resolver:{resolve:async(g,v)=>{let _=await e.resolve(Nt(g.headers.authorization)),C=b.connectionFor(_),R;try{R=await vi(C.clients,_i(g),v)}catch(O){throw C.release().catch(()=>{}),O}return{...R,release:C.release}}}})];m.on("upgrade",(g,v)=>{let _=new URL(g.url??"/","http://localhost");_.pathname===an||_.pathname===gr||_.pathname===Pt||v.destroyed||(v.write(`HTTP/1.1 404 Not Found\r
61
+ `)),l.end()})}}var Un=(t,e)=>Object.assign(new Error(e),{status:t});function Ci(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}async function $i(t,e,n){if(!t.supportsAudio)throw Un(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 r;try{r=await t.supportsAudio(e)}catch(a){throw a instanceof z?Un(404,a.message):a}if(!r)throw Un(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 Un(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 o=await t.sessionHandle(e),s={audio:await t.openAudioOn(o,n)},i={clients:{createResponse:async a=>await t.createResponseOn(o,a)},voice:r,audioSession:s};return e!==void 0&&(i.model=e),i}k();var Dn=require("@urun-sh/core"),Oe=class extends Error{constructor(n,r=401){super(n);this.status=r;this.name="ProxyAuthError"}status},_t=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function Gt(t){let e=(t??"").trim();if(e==="")throw new Oe("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let n=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!n)throw new Oe("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return n[1].trim()}var Ll=60,Ul=6e4,Nl=4096;function Dl(t){return t===401||t===403}var Nn=class{constructor(e){this.opts=e;this.mint=e.mint??Dn.createClientToken,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new Oe("empty Authorization bearer token");let n=this.cache.get(e);if(n&&this.now()-n.at<Ul)return{apiKey:e,orgId:n.orgId};let r;try{r=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:Ll})).orgId}catch(o){throw o instanceof Dn.ClientTokenError&&Dl(o.status)?(this.cache.delete(e),new Oe("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new _t(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${o instanceof Error?o.message:String(o)}`)}if(!r)throw new _t("the control plane verified the API key but returned no org_id \u2014 refusing to route a request with no tenancy (control-plane contract violation)");if(this.cache.size>=Nl){let o=this.cache.keys().next();o.done||this.cache.delete(o.value)}return this.cache.set(e,{orgId:r,at:this.now()}),{apiKey:e,orgId:r}}clear(){this.cache.clear()}};k();var Vn=require("crypto");k();var Vt=require("@urun-sh/core");k();k();k();k();function Mi(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 Li(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}k();k();function Ui(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 Fl="llm-resp";async function*qr(t,e){let n=`${Fl}:${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 Ui(i);return}}}finally{try{t.doc("llm").set({requests:{[e]:null}})}catch{}}}var Bn=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=Li(e,n,this.consumerId);return this.write(r),qr(this.session,n)})}sendResponseCreate(e,n,r){return this.writeGated(r,()=>{let o=Mi(e,n,r,this.consumerId);return this.write(o),qr(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 Ni=0;function Bl(){return Ni+=1,`req_${Date.now().toString(36)}_${Ni.toString(36)}`}var jn=class{transport;constructor(e){this.transport=new Bn(e)}responses={create:async e=>{let n=Bl(),r=await this.transport.sendResponses(e,n);return Object.assign((async function*(){yield*r})(),{requestId:n})}}};k();async function Di(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()}k();var Fi=require("@urun-sh/core"),Hr=2;function jl(t){let e=t?.type;return e==="response.created"||e==="response.in_progress"}function ql(t){let e=t?.type;return e==="response.completed"||e==="error"}function Bi(t){return async function(n,r){return(async function*(){for(let s=1;s<=Hr;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 y of p){if(ql(y)&&(d=!0),jl(y)){if(s>1)continue}else l=!0;yield y}}catch(p){if(p instanceof Fi.SessionFailedError&&(p.kind==="never-live"||p.kind==="create-failed")){try{t.onAdmissionVerdict?.(i,a,p)}catch{}throw p}if(s<Hr&&!l){await t.evict(i,a);continue}throw p}if(d)return;if(s<Hr&&!l){await t.evict(i,a);continue}return}})()}}k();var Hl=3,Gl="llm",Vl=new Set(["paused","error"]);function ji(t,e){let n=t.doc(Gl),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<Hl||d(new pe(`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&&Vl.has(l.name)&&d(new pe(`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*Gr(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 qi(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 Wl(t,e,n){let{baseUrl:r,orgId:o,auth:s}=t,i=typeof t.subject=="function"?t.subject():t.subject,a=n.startsWith(Xe)&&i!==void 0?`${i}:speech`:i,u=e.fnName??t.fnName,d=t.sessionStartOptions?.(n),l=e.sharedApp?{sharedApp:!0,...d}:d,p=w=>({session:w,responses:new jn(w),gone:ji(w,n)});if(s.lane==="jwt"){let w=(0,Vt.App)(e.appSlug,{baseUrl:r,orgId:o,jwt:s.jwt});return p(qi(w,e.appSlug,u,l))}let{apiKey:y,gatewayUrl:c}=s,m=(0,Vt.App)(e.appSlug,{baseUrl:r,orgId:o,getAccessToken:async()=>(await(0,Vt.createClientToken)(y,{baseUrl:c,expiresIn:300,allowedFunctions:[`${e.appSlug}/${u}`],...a===void 0?{}:{subject:a},...e.sharedApp?{sharedApp:!0}:{}})).token});return p(qi(m,e.appSlug,u,l))}function Jl(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 Kl(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 zl=new Set(["error","paused"]);function Jr(t){let{auth:e,catalog:n}=t,r=null,o=(i,a)=>{let u=Wl(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 Yt({sharedOnlyListing:t.sharedOnlyListing===!0,defaultApp:t.defaultApp,fnName:t.fnName,openSession:o,closeSession:async i=>{if(i.gone.dispose(),t.release==="detach"){if(typeof i.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');i.session.detach();return}await i.session.end()},listApps:e.lane==="api-key"?()=>To({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,listCatalog:n?()=>Di(n):null,listPrices:n?()=>ho(n):null,shared:e.lane==="api-key"?()=>co({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,sessionKey:Jl});return r=s,t.defaultApp!==null&&t.statelessDirect!==!0&&s.seed(t.defaultApp,o({appSlug:t.defaultApp},Rt({appSlug:t.defaultApp}))),s}async function Vr(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,e===void 0?void 0:{signal:e})}async function Hi(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 Wr(t){return t.reason instanceof Error?t.reason:new Error("the client socket closed")}function Kr(t,e=gs,n,r){let o=new WeakMap,s=new WeakMap,i=new WeakMap,a=new WeakMap,u=new WeakMap,d=c=>{let m=c.session.id;return typeof m=="string"&&m.length>0?m:null},l=c=>{let m=o.get(c);if(!m){let w={waiting:0,gone:new AbortController};u.set(c,w),m=(async()=>{if(await Vr(c.session,w.gone.signal),w.gone.signal.aborted)throw Wr(w.gone.signal);u.get(c)===w&&u.delete(c);let R=await fs(c.session,await e());return Object.assign(R,{sessionId:c.session.id})})(),o.set(c,m),m.catch(()=>{o.get(c)===m&&o.delete(c)}),m.then(()=>{u.get(c)===w&&u.delete(c)},()=>{u.get(c)===w&&u.delete(c)})}return m},p=c=>{let m=a.get(c);return m||(m=(async()=>{let w=await l(c);return{appendInputAudio:R=>w.appendInputAudio(R),onOutputAudio:R=>w.onOutputAudio(R),sessionId:d(c)}})(),a.set(c,m),m.catch(()=>{a.get(c)===m&&a.delete(c)})),m},y=async(c,m,w)=>{if(w?.aborted){let v=u.get(c);throw(o.get(c)===void 0||v!==void 0&&v.waiting===0)&&await Hi(c.session),Wr(w)}let R=m(c),h=u.get(c);return h!==void 0&&h.waiting++,w===void 0?R:new Promise((v,b)=>{let $=()=>{h!==void 0&&--h.waiting===0&&u.get(c)===h&&(h.gone.abort(),u.delete(c),o.delete(c),a.delete(c),Hi(c.session).catch(()=>{})),b(Wr(w))};w.addEventListener("abort",$,{once:!0});let S=()=>w.removeEventListener("abort",$);R.then(E=>{S(),v(E)},E=>{S(),b(E)})})};return{createResponse:Bi({sessionFor:c=>t.sessionFor(c),sessionIdOf:c=>d(c),evict:(c,m)=>t.evict(c,m),onAdmissionVerdict:(c,m,w)=>{Kl(w)&&(t.evict(c,m),n?.(c,w.kind==="never-live"?"never-live":"create-failed"))},createStream:async(c,m)=>(await Vr(c.session),Gr(await c.responses.responses.create(m),c.gone))}),listModels:()=>t.modelList(),openRouterModels:()=>t.openRouterModels(),supportsImages:(c,m)=>t.supportsImages(c,m),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),listSpeechToTextModels:()=>t.speechToTextModelList(),openAudio:async(c,m)=>{let{entry:w}=await t.sessionFor(c);return await y(w,l,m)},openAudioOn:async(c,m)=>{let{entry:w}=await t.sessionForHandle(c);return await y(w,p,m)},openVideo:async c=>{let{entry:m}=await t.sessionFor(c),w=s.get(m);return w||(w=ms(m.session),s.set(m,w)),w},openVideoOut:async c=>{let{entry:m}=await t.sessionFor(c),w=i.get(m);return w||(w=ys(m.session),i.set(m,w)),w},sessionHandle:async c=>(await t.handleFor(c)).handle,speechSessionHandle:async c=>(await t.speechHandleFor(c)).handle,evictSpeechSession:async c=>{let{app:m,entry:w}=await t.sessionForHandle(c);await t.evict(m,w)},createResponseOn:async(c,m)=>{let{entry:w}=await t.sessionForHandle(c);return await Vr(w.session),Gr(await w.responses.responses.create(m),w.gone)},onSessionEnd:async(c,m)=>{let{entry:w}=await t.sessionForHandle(c),R=w.session;if(typeof R.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let h=!1;return R.onPhase(v=>{if(h||!zl.has(v.name))return;h=!0;let b=R.endsAt??null;m({timeLeftMs:b?Math.max(0,b.getTime()-Date.now()):null,reason:v.reason??`session ${v.name}`})})},...r?{stateless:r}:{}}}var Yl=512;function Wn(t){return`urun-inference-proxy-${(0,Vn.createHash)("sha256").update(t).digest("hex").slice(0,32)}`}var qn=class{constructor(e){this.opts=e}opts;tenants=new Map;label(e,n){return Object.assign(Object.create(e),{tenant:n.orgId})}build(e){if(this.opts.build){let a=this.opts.build(e);return{router:a.router,clients:this.label(a.clients,e)}}let n=0,r=Wn(e.apiKey),o=()=>{n+=1},s=Jr({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:an,sharedOnlyListing:!0,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:()=>n===0?r:`${r}#${n}`,onPoolWedged:o,release:"end"}),i=this.opts.decisionIntake?new Tn({targetFor:a=>s.targetFor(a),apiUrl:this.opts.apiUrl,apiKey:e.apiKey,orgId:e.orgId,fnName:an,secret:this.opts.decisionIntake.secret,...this.opts.decisionIntake.shared?{shared:this.opts.decisionIntake.shared}:{}}):void 0;return{router:s,clients:this.label(Kr(s,void 0,o,i),e)}}clientsFor(e){let n=this.tenants.get(e.apiKey);if(n)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,n),n.clients;for(;this.tenants.size>=Yl;){let o=this.tenants.entries().next();if(o.done)break;let[s,i]=o.value;this.tenants.delete(s),i.router.closeAll().catch(()=>{})}let r=this.build(e);return this.tenants.set(e.apiKey,r),r.clients}get size(){return this.tenants.size}async closeAll(){let e=[...this.tenants.values()].map(r=>r.router);this.tenants.clear();let n=[];if(await Promise.all(e.map(async r=>{try{await r.closeAll()}catch(o){n.push(String(o instanceof Error?o.message:o))}})),n.length>0)throw new Error(`failed to close ${n.length} tenant backhaul(s): ${n.join("; ")}`)}},Gi=512,Xl=1024,Hn=class extends Error{constructor(){super(`conversation capacity reached (${Gi} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function Ql(t){return{sessionStartOptions:e=>{if(e.startsWith(Xe))return;let n=t.get(e);return n===void 0?void 0:{sessionId:n}},onSessionOpen:(e,n)=>{e.startsWith(Xe)||t.set(e,n)}}}function Zl(t,e,n,r){let o=Jr({baseUrl:t.baseUrl,orgId:e.orgId,defaultApp:null,fnName:an,sharedOnlyListing:!0,apiUrl:t.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:t.baseUrl},catalog:t.catalog,subject:n.subject,release:"detach",...Ql(r)});return{clients:Kr(o),release:async()=>{await o.closeAll()}}}var Gn=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,n){let r={id:n,subject:`${Wn(e.apiKey)}:live:${n}`},o=new Map,s=this.opts.build?this.opts.build(e,r):Zl(this.opts,e,r,o);return{id:n,backhaul:s,attachments:0,nativeRefs:o}}attachEntry(e,n){let r=this.byCaller.get(e.apiKey);r===void 0&&(r=new Map,this.byCaller.set(e.apiKey,r));let o=r.get(n);if(o!==void 0)return r.delete(n),r.set(n,o),o.attachments+=1,o;this.evictOverflow();let s=this.build(e,n);return s.attachments=1,r.set(n,s),s}get conversationCount(){let e=0;for(let n of this.byCaller.values())e+=n.size;return e}evictOverflow(){for(;this.conversationCount>=Gi;){let e=this.byCaller.keys().next();if(e.done)throw new Hn;let n=e.value,r=this.byCaller.get(n),o=!1;for(let[s,i]of r)if(!(i.attachments>0)){r.delete(s),this.dropHandleIndexFor(n,s),i.backhaul.release().catch(()=>{}),o=!0;break}if(!o)throw r.size===0&&this.byCaller.delete(n),new Hn;r.size===0&&this.byCaller.delete(n)}}dropHandleIndexFor(e,n){for(let[r,o]of this.byHandle)o.apiKey===e&&o.conversationId===n&&this.byHandle.delete(r)}connectionFor(e){let n=null,r=!1,o=()=>(n===null&&(n=this.attachEntry(e,(0,Vn.randomUUID)())),n),s={createResponse:(i,a)=>o().backhaul.clients.createResponse(i,a),catalogIdentity:i=>o().backhaul.clients.catalogIdentity(i),servingAppSlug:i=>{let a=o().backhaul.clients.servingAppSlug;if(a===void 0)throw new Error("the conversation backhaul wires no servingAppSlug seam \u2014 the caller-org app a request resolves to cannot be known");return a(i)},listModels:()=>o().backhaul.clients.listModels(),sessionHandle:i=>o().backhaul.clients.sessionHandle(i),createResponseOn:(i,a)=>o().backhaul.clients.createResponseOn(i,a),onSessionEnd:(i,a)=>o().backhaul.clients.onSessionEnd(i,a)};return s.openAudio=async(i,a)=>{let u=o().backhaul.clients.openAudio;if(u===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return u(i,a)},s.openVideo=async i=>{let a=o().backhaul.clients.openVideo;if(a===void 0)throw new Error("the conversation backhaul wires no openVideo seam \u2014 video-in is unavailable on this deployment");return a(i)},s.openVideoOut=async i=>{let a=o().backhaul.clients.openVideoOut;if(a===void 0)throw new Error("the conversation backhaul wires no openVideoOut seam \u2014 video-out is unavailable on this deployment");return a(i)},s.supportsAudio=async i=>{let a=o().backhaul.clients.supportsAudio;if(a===void 0)throw new Error("the conversation backhaul wires no supportsAudio seam \u2014 the realtime lane cannot gate audio modality on this deployment");return a(i)},s.supportsSpeech=async i=>{let a=o().backhaul.clients.supportsSpeech;if(a===void 0)throw new Error("the conversation backhaul wires no supportsSpeech seam \u2014 the speech lane cannot gate model capability on this deployment");return a(i)},s.speechVoices=async i=>{let a=o().backhaul.clients.speechVoices;if(a===void 0)throw new Error("the conversation backhaul wires no speechVoices seam \u2014 the speech lane cannot resolve the voice set /v1/models publishes on this deployment");return a(i)},s.speechSessionHandle=async i=>{let a=o().backhaul.clients.speechSessionHandle;if(a===void 0)throw new Error("the conversation backhaul wires no speechSessionHandle seam \u2014 the speech lane cannot resolve its own pooled session on this deployment");return a(i)},s.openAudioOn=async(i,a)=>{let u=o().backhaul.clients.openAudioOn;if(u===void 0)throw new Error("the conversation backhaul wires no openAudioOn seam \u2014 the speech and realtime lanes cannot open their pinned native session audio on this deployment");return u(i,a)},s.evictSpeechSession=async i=>{let a=o().backhaul.clients.evictSpeechSession;if(a===void 0)throw new Error("the conversation backhaul wires no evictSpeechSession seam \u2014 the speech lane cannot retire its pinned speech session after a failed turn on this deployment");return a(i)},s.supportsTranscription=async i=>{let a=o().backhaul.clients.supportsTranscription;if(a===void 0)throw new Error("the conversation backhaul wires no supportsTranscription seam \u2014 the transcription lane cannot gate model capability on this deployment");return a(i)},s.listSpeechToTextModels=async()=>{let i=o().backhaul.clients.listSpeechToTextModels;if(i===void 0)throw new Error("the conversation backhaul wires no listSpeechToTextModels seam \u2014 the transcription lane cannot list the stt models on this deployment");return i()},{clients:s,bindResumeHandle:i=>{let a=n;if(a===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=Xl;){let u=this.byHandle.keys().next();if(u.done)break;this.byHandle.delete(u.value)}this.byHandle.set(i,{apiKey:e.apiKey,conversationId:a.id})},clientsForResume:async i=>{let a=this.byHandle.get(i),u=a===void 0?void 0:this.byCaller.get(a.apiKey),d=a===void 0||u===void 0?void 0:u.get(a.conversationId);if(a===void 0||d===void 0||a.apiKey!==e.apiKey)throw new pe("the conversation behind this resume handle is gone (evicted at capacity, or the replica restarted) or the handle is not yours \u2014 reconnect without a handle to start a new conversation");return n=d,d.attachments+=1,d.backhaul.clients},release:async()=>{if(r)return;r=!0;let i=n;n=null,i!==null&&(i.attachments-=1,!(i.attachments>0)&&await i.backhaul.release().catch(()=>{}))}}}async detachAll(){let e=[];for(let r of this.byCaller.values())for(let o of r.values())e.push(o.backhaul.release());this.byCaller.clear(),this.byHandle.clear();let n=[];if(await Promise.all(e.map(async r=>{try{await r}catch(o){n.push(String(o instanceof Error?o.message:o))}})),n.length>0)throw new Error(`failed to detach ${n.length} conversation backhaul(s): ${n.join("; ")}`)}};k();var Jn="/inference-usage",Vi=25e3;var ec=new Set(["Error","TypeError","SyntaxError","TimeoutError"]);function Wi(t){if(!(t instanceof Error))return`a ${typeof t}`;let e=ec.has(t.name)?t.name:"Error",n=t.cause?.code;return typeof n=="string"&&/^[A-Z][A-Z0-9_]{0,31}$/.test(n)?`${e} (${n})`:e}var Kn=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async lookup(e,n){let r=`${this.opts.apiUrl.replace(/\/+$/,"")}${Jn}`,o;try{o=await this.fetchImpl(r,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify({inference_ids:n}),signal:AbortSignal.timeout(Vi)})}catch(i){throw new te(`the uRun usage surface (POST ${Jn}) could not be reached within ${Vi}ms: ${Wi(i)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new Oe("the uRun org API key was rejected by the uRun control plane \u2014 check the key and that it is not revoked");if(!o.ok){let i=await o.text().catch(()=>null),a=i===null?"body unreadable":`${i.length}-character body`;throw new te(`the uRun usage surface (POST ${Jn}) answered HTTP ${o.status} (${a})`)}let s;try{s=await o.json()}catch(i){throw new te(`the uRun usage surface (POST ${Jn}) answered HTTP ${o.status} with a body that is not JSON: ${Wi(i)}`)}return ns(s,this.opts.onRejectedRow)}};k();var tc="/inference-ledger",nc=1e4,zn=class extends Error{ledgerFailureReason;constructor(e,n="duplicate"){super(e),this.name="LedgerDuplicateError",this.ledgerFailureReason=n}},Yr=class extends Error{constructor(n,r){super(r);this.ledgerAlreadyWritten=n;this.name="LedgerAlreadyWrittenError"}ledgerAlreadyWritten},we=class extends Error{ledgerFailureReason="surface";constructor(e){super(e),this.name="LedgerSurfaceError"}},Xr=class extends Oe{ledgerFailureReason="auth";constructor(e){super(e),this.name="LedgerAuthError"}},rc=[["inference_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["model_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["variant",t=>t===null||typeof t=="string"],["task",t=>typeof t=="string"&&/[^\s]/.test(t)],["http_status",t=>typeof t=="number"&&Number.isInteger(t)],["started_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["completed_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["prompt_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["completion_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["output_units",Ji],["cost_nano_usd",Ji],["price_version",t=>t===null||typeof t=="string"]];function oc(t){for(let[e,n]of rc){if(!(e in t))return`${e} (absent)`;if(!n(t[e]))return`${e} (${JSON.stringify(t[e])})`}return null}function sc(t){let e;try{e=JSON.parse(t)}catch{return{status:"unavailable"}}if(e===null||typeof e!="object"||Array.isArray(e))return{status:"unavailable"};let n=e.existing_status;if(n==="not_in_this_org")return{status:"not_in_this_org"};if(n!=="found")return{status:"unavailable"};let r=e.existing;return r===null||typeof r!="object"||Array.isArray(r)?{status:"unavailable"}:oc(r)!==null?{status:"unavailable"}:{status:"found",row:r}}function Ji(t){return t===null?!0:typeof t=="number"?Number.isFinite(t):typeof t!="string"?!1:/[^\s]/.test(t)&&Number.isFinite(Number(t))}function zr(t,e){return t===null?e===null:e===null?!1:Number(e)===t}function Ki(t,e){if(typeof e!="string")return!1;let n=Date.parse(t),r=Date.parse(e);return Number.isFinite(n)&&Number.isFinite(r)&&n===r}function ic(t,e){let n=e;return n.inference_id===t.inference_id&&n.model_id===t.model_id&&(n.variant??null)===t.variant&&n.task===t.task&&n.http_status===t.http_status&&Ki(t.started_at,n.started_at)&&Ki(t.completed_at,n.completed_at)&&zr(t.prompt_tokens,n.prompt_tokens)&&zr(t.completion_tokens,n.completion_tokens)&&zr(t.output_units,n.output_units)}function ac(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new we(`${e} answered a "recorded" that is not an object: ${JSON.stringify(t)}`);let n=t,r=n.inference_id;if(typeof r!="string"||r==="")throw new we(`${e} answered a row with no inference_id: ${JSON.stringify(t)}`);if(!("cost_nano_usd"in n))throw new we(`${e} answered a row with no cost_nano_usd field: ${JSON.stringify(t)}`);let o=n.cost_nano_usd;if(o!=null&&!(typeof o=="number"&&Number.isInteger(o)&&o>=0))throw new we(`${e} answered cost_nano_usd ${JSON.stringify(o)}, which is neither a non-negative integer nor null`);let s=d=>{if(!(d in n))throw new we(`${e} answered a row with no ${d} field: ${JSON.stringify(t)}`);let l=n[d];if(l===null)return null;if(typeof l!="string")throw new we(`${e} answered a non-string ${d}: ${JSON.stringify(l)}`);if(!/[^\s]/.test(l))throw new we(`${e} answered a BLANK ${d} (${JSON.stringify(l)}) \u2014 it satisfies "is a string" while naming nothing, which is exactly what inference_requests_priced_attribution_check exists to forbid`);return l},i=s("price_version"),a=s("unpriced_reason"),u=o!=null;if(u&&(i===null||a!==null))throw new we(`${e} answered a PRICED row (cost_nano_usd ${String(o)}) that is not cleanly attributed \u2014 price_version ${JSON.stringify(i)}, unpriced_reason ${JSON.stringify(a)}`);if(!u&&(i!==null||a===null))throw new we(`${e} answered an UNPRICED row that does not say why \u2014 price_version ${JSON.stringify(i)}, unpriced_reason ${JSON.stringify(a)}. NULL means NOT YET PRICED, and the reason is how an operator learns that traffic is being served which nobody can bill.`);return{inference_id:r,cost_nano_usd:o===void 0?null:o,price_version:i,unpriced_reason:a}}var Yn=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async write(e,n){let r=`${this.opts.apiUrl.replace(/\/+$/,"")}${tc}`,o;try{o=await this.fetchImpl(r,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify(n),signal:AbortSignal.timeout(nc)})}catch(u){throw new we(`the uRun ledger surface at ${r} could not be reached: ${u instanceof Error?u.message:String(u)}`)}if(o.status===409){let u=await o.text().catch(()=>""),d=sc(u);throw d.status==="found"&&ic(n,d.row)?new Yr(d.row,`the uRun control plane already holds THIS PROXY'S OWN row for inference_id ${n.inference_id} \u2014 every billable field matches the row being re-sent, so an earlier attempt landed and this retry is a no-op. Nothing was written and nothing was overwritten.`):d.status==="not_in_this_org"?new zn(`inference_id ${n.inference_id} is ALREADY TAKEN by a ledger row this org cannot read. The id space is a global primary key, so this is not a retry and cannot be this proxy's own write \u2014 either two orgs collided on a uuid, or a row was written for this id by somebody else.`,"duplicate_foreign"):new zn(`the uRun control plane already holds a ledger row for inference_id ${n.inference_id} that is NOT the one being written${d.status==="unavailable"?" (and it could not be read back to compare)":""} \u2014 nothing was written and nothing was overwritten. Either this proxy wrote twice, or a row was fabricated ahead of it (this write is authenticated by the CALLER'S OWN org key). ${u.slice(0,400)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new Xr("the uRun org API key was rejected by the uRun control plane when writing the inference ledger \u2014 the request was served but produced no billable row");if(!o.ok){let u=await o.text().catch(d=>`<body unreadable: ${d instanceof Error?d.message:String(d)}>`);throw new we(`the uRun ledger surface at ${r} answered HTTP ${o.status}: ${u.slice(0,400)}`)}let s;try{s=await o.json()}catch(u){throw new we(`the uRun ledger surface at ${r} answered a body that is not JSON: ${u instanceof Error?u.message:String(u)}`)}let i=s?.recorded;if(i===void 0)throw new we(`the uRun ledger surface at ${r} answered a body with no "recorded" field`);let a=ac(i,`the uRun ledger surface at ${r}`);if(a.inference_id!==n.inference_id)throw new we(`the uRun ledger surface at ${r} answered for inference_id ${a.inference_id} when asked about ${n.inference_id}`);return a}};var uc=5e3,dc=3e3,lc="/api/client-tokens";function Qr(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function zi(t,e,n,r){Qr(t,e,{error:{message:n,type:r}})}async function cc(t,e=fetch){let n=`${t.replace(/\/+$/,"")}${lc}`;try{let r=await e(n,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(dc)});return r.status===404?{ready:!1,detail:`${n} answered 404 \u2014 this is not a uRun control plane`}:r.status>=500?{ready:!1,detail:`${n} answered HTTP ${r.status}`}:{ready:!0,detail:`${n} answered HTTP ${r.status}`}}catch(r){return{ready:!1,detail:`${n} unreachable: ${r instanceof Error?r.message:String(r)}`}}}function Xi(t){let e=t.resolver??new Nn({gatewayUrl:t.baseUrl}),n=t.decisionSecret??"",r=t.sharedOrgKey&&t.sharedOrgId?{apiKey:t.sharedOrgKey}:void 0,o=t.registry??new qn({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog,...n!==""?{decisionIntake:{secret:n,...r?{shared:r}:{}}}:{}});n!==""&&process.stderr.write(`inference-proxy: stateless direct path ENABLED \u2014 caller-org decisions dial ready runtimes directly (no session mint)
62
+ `);let s=t.probeControlPlane??(()=>cc(t.baseUrl)),i=t.usageClient??new Kn({apiUrl:t.apiUrl}),a=t.ledgerClient??new Yn({apiUrl:t.apiUrl}),u=new sn(t.defaultMetrics===void 0?{}:{defaultMetrics:t.defaultMetrics}),d=async h=>e.resolve(Gt(h.headers.authorization)),l=h=>h instanceof Oe?{status:h.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:h instanceof _t?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,p=Pi({clients:async h=>o.clientsFor(await d(h)),usage:{authorize:h=>Gt(h.headers.authorization),lookup:(h,v)=>i.lookup(h,v)},statusOf:l,...t.requestLog?{requestLog:t.requestLog}:{},observe:h=>u.observe(h),ledger:{write:(h,v)=>a.write(Gt(h.headers.authorization),v),observe:h=>u.observeLedger(h),...t.ledgerLog?{report:t.ledgerLog}:{}}}),y=null,c=(0,Yi.createServer)((h,v)=>{let b=new URL(h.url??"/","http://localhost");(async()=>{if(h.method==="GET"&&b.pathname==="/healthz"){Qr(v,200,{ok:!0});return}if(h.method==="GET"&&b.pathname==="/readyz"){let E=Date.now();if(!y||E-y.at>=uc){let x=await s();y={at:E,...x}}Qr(v,y.ready?200:503,{ok:y.ready,control_plane:y.detail});return}let $=b.pathname==="/v1"||b.pathname.startsWith("/v1/"),S=b.pathname.startsWith("/partners/");if(!$&&!S){zi(v,404,`no route for ${h.method} ${b.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface, the partner adapter surface (/partners/...), the WS upgrade paths (/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent and /v1/realtime), plus /healthz and /readyz`,"invalid_request_error");return}p(h,v)})().catch($=>{if(!v.headersSent){zi(v,500,String($ instanceof Error?$.message:$),"proxy_error");return}v.end()})}),m=t.conversations??new Gn({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),w=[wr(c,{authenticate:async h=>{let v=await e.resolve(h),b=m.connectionFor(v);return{clients:b.clients,scope:Wn(v.apiKey),onBindResumeHandle:b.bindResumeHandle,clientsForResume:b.clientsForResume,release:b.release}}}),Er(c,{clients:async h=>o.clientsFor(await d(h)),statusOf:l}).close,bn(c,{resolver:{resolve:async(h,v)=>{let b=await e.resolve(Gt(h.headers.authorization)),$=m.connectionFor(b),S;try{S=await $i($.clients,Ci(h),v)}catch(E){throw $.release().catch(()=>{}),E}return{...S,release:$.release}}}})];c.on("upgrade",(h,v)=>{let b=new URL(h.url??"/","http://localhost");b.pathname===pn||b.pathname===Rr||b.pathname===Dt||v.destroyed||(v.write(`HTTP/1.1 404 Not Found\r
63
63
  Connection: close\r
64
64
  \r
65
- no WS route for ${_.pathname} \u2014 this endpoint serves only the Gemini Live, /v1/responses, and /v1/realtime upgrade paths`),v.destroy())});let w=async()=>{for(let g of E)await g();await b.detachAll()};return{server:m,metricsServer:Qo(u),metrics:u,edgeIdentityServer:ts(),registry:o,closeAll:()=>o.closeAll(),closeWebSockets:w}}async function Bi(t,e,n){n&&await n();let r=new Promise(o=>t.close(()=>o()));t.closeAllConnections(),await r,await Lo()||No(),await e()}function Vl(){let t=null;return t=Oo({streams:xe.default,note:e=>{t?.isDead("stderr")!==!0&&Ie(xe.default.stderr,e)}}),t}async function Wl(){Vl();let t=ns(xe.default.env),{server:e,metricsServer:n,edgeIdentityServer:r,closeAll:o,closeWebSockets:s}=Fi(t);await new Promise((a,u)=>{e.once("error",u),e.listen(t.port,Je,a)}),await new Promise((a,u)=>{n.once("error",u),n.listen(rr,Je,a)}),await new Promise((a,u)=>{r.once("error",u),r.listen(sr,Je,a)}),xe.default.stdout.write(`inference-proxy listening on http://${Je}:${t.port}/v1 (metrics http://${Je}:${rr}${Xt}, edge identity http://${Je}:${sr}, control plane ${t.baseUrl}, org API ${t.apiUrl}, catalog oracle ${t.catalog?"on":"off"})
66
- `);let i=async a=>{xe.default.stdout.write(`inference-proxy: ${a} \u2014 draining
67
- `),await Bi(e,o,s),await new Promise(u=>n.close(()=>u())),await new Promise(u=>r.close(()=>u()))};await new Promise(a=>{xe.default.once("SIGINT",()=>{i("SIGINT").finally(a)}),xe.default.once("SIGTERM",()=>{i("SIGTERM").finally(a)})})}Wl().then(()=>xe.default.exit(0),t=>{xe.default.stderr.write(`inference-proxy: ${String(t instanceof Error?t.stack??t.message:t)}
68
- `),xe.default.exit(1)});
65
+ no WS route for ${b.pathname} \u2014 this endpoint serves only the Gemini Live, /v1/responses, and /v1/realtime upgrade paths`),v.destroy())});let R=async()=>{for(let h of w)await h();await m.detachAll()};return{server:c,metricsServer:ss(u),metrics:u,edgeIdentityServer:us(),registry:o,closeAll:()=>o.closeAll(),closeWebSockets:R}}async function Qi(t,e,n){n&&await n();let r=new Promise(o=>t.close(()=>o()));t.closeAllConnections(),await r,await Ho()||Go(),await e()}function pc(){let t=null;return t=Mo({streams:Pe.default,note:e=>{t?.isDead("stderr")!==!0&&$e(Pe.default.stderr,e)}}),t}async function fc(){pc();let t=ds(Pe.default.env),{server:e,metricsServer:n,edgeIdentityServer:r,closeAll:o,closeWebSockets:s}=Xi(t);await new Promise((a,u)=>{e.once("error",u),e.listen(t.port,Ze,a)}),await new Promise((a,u)=>{n.once("error",u),n.listen(cr,Ze,a)}),await new Promise((a,u)=>{r.once("error",u),r.listen(fr,Ze,a)}),Pe.default.stdout.write(`inference-proxy listening on http://${Ze}:${t.port}/v1 (metrics http://${Ze}:${cr}${nn}, edge identity http://${Ze}:${fr}, control plane ${t.baseUrl}, org API ${t.apiUrl}, catalog oracle ${t.catalog?"on":"off"})
66
+ `);let i=async a=>{Pe.default.stdout.write(`inference-proxy: ${a} \u2014 draining
67
+ `),await Qi(e,o,s),await new Promise(u=>n.close(()=>u())),await new Promise(u=>r.close(()=>u()))};await new Promise(a=>{Pe.default.once("SIGINT",()=>{i("SIGINT").finally(a)}),Pe.default.once("SIGTERM",()=>{i("SIGTERM").finally(a)})})}fc().then(()=>Pe.default.exit(0),t=>{Pe.default.stderr.write(`inference-proxy: ${String(t instanceof Error?t.stack??t.message:t)}
68
+ `),Pe.default.exit(1)});
69
69
  /*! Bundled license information:
70
70
 
71
71
  reflect-metadata/Reflect.js: