@urun-sh/openai 0.5.5 → 0.5.6

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.
Files changed (55) hide show
  1. package/dist/{ResponsesClient-BYx3YLGo.d.ts → ResponsesClient-BE3-hx3m.d.ts} +2 -0
  2. package/dist/{ResponsesClient-Dft3bg3b.d.cts → ResponsesClient-DjZWjlFH.d.cts} +2 -0
  3. package/dist/chunk-2T2YYBVX.js +1 -0
  4. package/dist/chunk-3WAJD62J.js +4 -0
  5. package/dist/{chunk-K23AZPI4.js → chunk-5HKWNK3O.js} +1 -1
  6. package/dist/chunk-7M6LY6DR.js +2 -0
  7. package/dist/chunk-BSHT6RZZ.js +1 -0
  8. package/dist/{chunk-GBBY3PCZ.js → chunk-FP4RSAIE.js} +1 -1
  9. package/dist/chunk-HR5H6S7L.js +1 -0
  10. package/dist/chunk-NY23USZF.js +57 -0
  11. package/dist/chunk-VLRMJRLS.js +6 -0
  12. package/dist/gemini-live.cjs +2 -2
  13. package/dist/gemini-live.d.cts +17 -2
  14. package/dist/gemini-live.d.ts +7 -2
  15. package/dist/gemini-live.js +1 -1
  16. package/dist/hosted/bin.cjs +40 -26
  17. package/dist/hosted/bin.js +4 -4
  18. package/dist/hosted/index.cjs +39 -26
  19. package/dist/hosted/index.d.cts +643 -16
  20. package/dist/hosted/index.d.ts +153 -5
  21. package/dist/hosted/index.js +1 -1
  22. package/dist/index.cjs +1 -1
  23. package/dist/index.d.cts +4 -4
  24. package/dist/index.d.ts +4 -4
  25. package/dist/index.js +1 -1
  26. package/dist/{models-NYMZrklp.d.cts → models-DUdx_Y6X.d.cts} +1 -1
  27. package/dist/pi-extension/index.cjs +7 -7
  28. package/dist/pi-extension/index.d.cts +1 -1
  29. package/dist/pi-extension/index.d.ts +1 -1
  30. package/dist/pi-extension/index.js +1 -1
  31. package/dist/pi-extension/standalone.cjs +48 -48
  32. package/dist/proxy/cli.cjs +53 -43
  33. package/dist/proxy/cli.js +14 -14
  34. package/dist/proxy/index.cjs +32 -30
  35. package/dist/proxy/index.d.cts +84 -8
  36. package/dist/proxy/index.d.ts +16 -6
  37. package/dist/proxy/index.js +1 -6
  38. package/dist/responses-turn-Dr37N3kH.d.ts +486 -0
  39. package/dist/responses-turn-tNUHq5b0.d.cts +1807 -0
  40. package/dist/{translator-C9uPKypK.d.ts → translator-BCoRFaTs.d.ts} +1 -1
  41. package/dist/{translator-CcDBEfvm.d.cts → translator-Bh0Bp_Ie.d.cts} +3 -3
  42. package/dist/{video-out-D20UuJ8G.d.cts → video-out-CCksIcj8.d.cts} +5 -5
  43. package/package.json +12 -11
  44. package/dist/chunk-5BZCM3RS.js +0 -4
  45. package/dist/chunk-5NXM4IO3.js +0 -2
  46. package/dist/chunk-CWJRDBDC.js +0 -1
  47. package/dist/chunk-DMD5UENK.js +0 -1
  48. package/dist/chunk-I2Q3B3OG.js +0 -6
  49. package/dist/chunk-OI2OY32M.js +0 -1
  50. package/dist/chunk-QVF7NF7G.js +0 -44
  51. package/dist/responses-turn-KOAoIqZ-.d.ts +0 -200
  52. package/dist/responses-turn-OrO4euEN.d.cts +0 -513
  53. /package/dist/{chunk-YSFSRI3D.js → chunk-5CCJUNH7.js} +0 -0
  54. /package/dist/{models-NYMZrklp.d.ts → models-DUdx_Y6X.d.ts} +0 -0
  55. /package/dist/{video-out-CWesbk12.d.ts → video-out-IGQ4YQ-c.d.ts} +0 -0
@@ -1,55 +1,69 @@
1
1
  #!/usr/bin/env node
2
- "use strict";var yr=Object.create;var Xt=Object.defineProperty;var gr=Object.getOwnPropertyDescriptor;var wr=Object.getOwnPropertyNames;var vr=Object.getPrototypeOf,_r=Object.prototype.hasOwnProperty;var kr=(n,e,t)=>()=>{if(t)throw t[0];try{return n&&(e=n(n=0)),e}catch(r){throw t=[r],r}};var br=(n,e)=>()=>{try{return e||n((e={exports:{}}).exports,e),e.exports}catch(t){throw e=0,t}};var xr=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of wr(e))!_r.call(n,o)&&o!==t&&Xt(n,o,{get:()=>e[o],enumerable:!(r=gr(e,o))||r.enumerable});return n};var Pe=(n,e,t)=>(t=n!=null?yr(vr(n)):{},xr(e||!n||!n.__esModule?Xt(t,"default",{value:n,enumerable:!0}):t,n));var O=kr(()=>{"use strict"});var Zt=br(()=>{"use strict";O();var Qt;(function(n){(function(e){var t=typeof globalThis=="object"?globalThis:typeof global=="object"?global:typeof self=="object"?self:typeof this=="object"?this:a(),r=o(n);typeof t.Reflect<"u"&&(r=o(t.Reflect,r)),e(r,t),typeof t.Reflect>"u"&&(t.Reflect=n);function o(c,d){return function(h,m){Object.defineProperty(c,h,{configurable:!0,writable:!0,value:m}),d&&d(h,m)}}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,t){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",c={__proto__:[]}instanceof Array,d=!a&&!c,h={create:a?function(){return yt(Object.create(null))}:c?function(){return yt({__proto__:null})}:function(){return yt({})},has:d?function(u,l){return r.call(u,l)}:function(u,l){return l in u},get:d?function(u,l){return r.call(u,l)?u[l]:void 0}:function(u,l){return u[l]}},m=Object.getPrototypeOf(Function),f=typeof Map=="function"&&typeof Map.prototype.entries=="function"?Map:dr(),g=typeof Set=="function"&&typeof Set.prototype.entries=="function"?Set:pr(),S=typeof WeakMap=="function"?WeakMap:fr(),I=o?Symbol.for("@reflect-metadata:registry"):void 0,A=ur(),$=cr(A);function K(u,l,p,y){if(E(p)){if(!Gt(u))throw new TypeError;if(!Vt(l))throw new TypeError;return ie(u,l)}else{if(!Gt(u))throw new TypeError;if(!U(l))throw new TypeError;if(!U(y)&&!E(y)&&!ee(y))throw new TypeError;return ee(y)&&(y=void 0),p=fe(p),q(u,l,p,y)}}e("decorate",K);function X(u,l){function p(y,R){if(!U(y))throw new TypeError;if(!E(R)&&!ir(R))throw new TypeError;ae(u,l,y,R)}return p}e("metadata",X);function Q(u,l,p,y){if(!U(p))throw new TypeError;return E(y)||(y=fe(y)),ae(u,l,p,y)}e("defineMetadata",Q);function se(u,l,p){if(!U(l))throw new TypeError;return E(p)||(p=fe(p)),J(u,l,p)}e("hasMetadata",se);function k(u,l,p){if(!U(l))throw new TypeError;return E(p)||(p=fe(p)),T(u,l,p)}e("hasOwnMetadata",k);function M(u,l,p){if(!U(l))throw new TypeError;return E(p)||(p=fe(p)),N(u,l,p)}e("getMetadata",M);function ne(u,l,p){if(!U(l))throw new TypeError;return E(p)||(p=fe(p)),H(u,l,p)}e("getOwnMetadata",ne);function Z(u,l){if(!U(u))throw new TypeError;return E(l)||(l=fe(l)),be(u,l)}e("getMetadataKeys",Z);function re(u,l){if(!U(u))throw new TypeError;return E(l)||(l=fe(l)),ce(u,l)}e("getOwnMetadataKeys",re);function B(u,l,p){if(!U(l))throw new TypeError;if(E(p)||(p=fe(p)),!U(l))throw new TypeError;E(p)||(p=fe(p));var y=De(l,p,!1);return E(y)?!1:y.OrdinaryDeleteMetadata(u,l,p)}e("deleteMetadata",B);function ie(u,l){for(var p=u.length-1;p>=0;--p){var y=u[p],R=y(l);if(!E(R)&&!ee(R)){if(!Vt(R))throw new TypeError;l=R}}return l}function q(u,l,p,y){for(var R=u.length-1;R>=0;--R){var j=u[R],W=j(l,p,y);if(!E(W)&&!ee(W)){if(!U(W))throw new TypeError;y=W}}return y}function J(u,l,p){var y=T(u,l,p);if(y)return!0;var R=ht(l);return ee(R)?!1:J(u,R,p)}function T(u,l,p){var y=De(l,p,!1);return E(y)?!1:jt(y.OrdinaryHasOwnMetadata(u,l,p))}function N(u,l,p){var y=T(u,l,p);if(y)return H(u,l,p);var R=ht(l);if(!ee(R))return N(u,R,p)}function H(u,l,p){var y=De(l,p,!1);if(!E(y))return y.OrdinaryGetOwnMetadata(u,l,p)}function ae(u,l,p,y){var R=De(p,y,!0);R.OrdinaryDefineOwnMetadata(u,l,p,y)}function be(u,l){var p=ce(u,l),y=ht(u);if(y===null)return p;var R=be(y,l);if(R.length<=0)return p;if(p.length<=0)return R;for(var j=new g,W=[],P=0,v=p;P<v.length;P++){var _=v[P],b=j.has(_);b||(j.add(_),W.push(_))}for(var x=0,C=R;x<C.length;x++){var _=C[x],b=j.has(_);b||(j.add(_),W.push(_))}return W}function ce(u,l){var p=De(u,l,!1);return p?p.OrdinaryOwnMetadataKeys(u,l):[]}function oe(u){if(u===null)return 1;switch(typeof u){case"undefined":return 0;case"boolean":return 2;case"string":return 3;case"symbol":return 4;case"number":return 5;case"object":return u===null?1:6;default:return 6}}function E(u){return u===void 0}function ee(u){return u===null}function L(u){return typeof u=="symbol"}function U(u){return typeof u=="object"?u!==null:typeof u=="function"}function qe(u,l){switch(oe(u)){case 0:return u;case 1:return u;case 2:return u;case 3:return u;case 4:return u;case 5:return u}var p=l===3?"string":l===5?"number":"default",y=qt(u,s);if(y!==void 0){var R=y.call(u,p);if(U(R))throw new TypeError;return R}return or(u,p==="default"?"number":p)}function or(u,l){if(l==="string"){var p=u.toString;if(Ie(p)){var y=p.call(u);if(!U(y))return y}var R=u.valueOf;if(Ie(R)){var y=R.call(u);if(!U(y))return y}}else{var R=u.valueOf;if(Ie(R)){var y=R.call(u);if(!U(y))return y}var j=u.toString;if(Ie(j)){var y=j.call(u);if(!U(y))return y}}throw new TypeError}function jt(u){return!!u}function sr(u){return""+u}function fe(u){var l=qe(u,3);return L(l)?l:sr(l)}function Gt(u){return Array.isArray?Array.isArray(u):u instanceof Object?u instanceof Array:Object.prototype.toString.call(u)==="[object Array]"}function Ie(u){return typeof u=="function"}function Vt(u){return typeof u=="function"}function ir(u){switch(oe(u)){case 3:return!0;case 4:return!0;default:return!1}}function mt(u,l){return u===l||u!==u&&l!==l}function qt(u,l){var p=u[l];if(p!=null){if(!Ie(p))throw new TypeError;return p}}function Wt(u){var l=qt(u,i);if(!Ie(l))throw new TypeError;var p=l.call(u);if(!U(p))throw new TypeError;return p}function Jt(u){return u.value}function Kt(u){var l=u.next();return l.done?!1:l}function zt(u){var l=u.return;l&&l.call(u)}function ht(u){var l=Object.getPrototypeOf(u);if(typeof u!="function"||u===m||l!==m)return l;var p=u.prototype,y=p&&Object.getPrototypeOf(p);if(y==null||y===Object.prototype)return l;var R=y.constructor;return typeof R!="function"||R===u?l:R}function ar(){var u;!E(I)&&typeof t.Reflect<"u"&&!(I in t.Reflect)&&typeof t.Reflect.defineMetadata=="function"&&(u=lr(t.Reflect));var l,p,y,R=new S,j={registerProvider:W,getProvider:v,setProvider:b};return j;function W(x){if(!Object.isExtensible(j))throw new Error("Cannot add provider to a frozen registry.");switch(!0){case u===x:break;case E(l):l=x;break;case l===x:break;case E(p):p=x;break;case p===x:break;default:y===void 0&&(y=new g),y.add(x);break}}function P(x,C){if(!E(l)){if(l.isProviderFor(x,C))return l;if(!E(p)){if(p.isProviderFor(x,C))return l;if(!E(y))for(var D=Wt(y);;){var G=Kt(D);if(!G)return;var le=Jt(G);if(le.isProviderFor(x,C))return zt(D),le}}}if(!E(u)&&u.isProviderFor(x,C))return u}function v(x,C){var D=R.get(x),G;return E(D)||(G=D.get(C)),E(G)&&(G=P(x,C),E(G)||(E(D)&&(D=new f,R.set(x,D)),D.set(C,G))),G}function _(x){if(E(x))throw new TypeError;return l===x||p===x||!E(y)&&y.has(x)}function b(x,C,D){if(!_(D))throw new Error("Metadata provider not registered.");var G=v(x,C);if(G!==D){if(!E(G))return!1;var le=R.get(x);E(le)&&(le=new f,R.set(x,le)),le.set(C,D)}return!0}}function ur(){var u;return!E(I)&&U(t.Reflect)&&Object.isExtensible(t.Reflect)&&(u=t.Reflect[I]),E(u)&&(u=ar()),!E(I)&&U(t.Reflect)&&Object.isExtensible(t.Reflect)&&Object.defineProperty(t.Reflect,I,{enumerable:!1,configurable:!1,writable:!1,value:u}),u}function cr(u){var l=new S,p={isProviderFor:function(_,b){var x=l.get(_);return E(x)?!1:x.has(b)},OrdinaryDefineOwnMetadata:W,OrdinaryHasOwnMetadata:R,OrdinaryGetOwnMetadata:j,OrdinaryOwnMetadataKeys:P,OrdinaryDeleteMetadata:v};return A.registerProvider(p),p;function y(_,b,x){var C=l.get(_),D=!1;if(E(C)){if(!x)return;C=new f,l.set(_,C),D=!0}var G=C.get(b);if(E(G)){if(!x)return;if(G=new f,C.set(b,G),!u.setProvider(_,b,p))throw C.delete(b),D&&l.delete(_),new Error("Wrong provider for target.")}return G}function R(_,b,x){var C=y(b,x,!1);return E(C)?!1:jt(C.has(_))}function j(_,b,x){var C=y(b,x,!1);if(!E(C))return C.get(_)}function W(_,b,x,C){var D=y(x,C,!0);D.set(_,b)}function P(_,b){var x=[],C=y(_,b,!1);if(E(C))return x;for(var D=C.keys(),G=Wt(D),le=0;;){var Yt=Kt(G);if(!Yt)return x.length=le,x;var mr=Jt(Yt);try{x[le]=mr}catch(hr){try{zt(G)}finally{throw hr}}le++}}function v(_,b,x){var C=y(b,x,!1);if(E(C)||!C.delete(_))return!1;if(C.size===0){var D=l.get(b);E(D)||(D.delete(x),D.size===0&&l.delete(D))}return!0}}function lr(u){var l=u.defineMetadata,p=u.hasOwnMetadata,y=u.getOwnMetadata,R=u.getOwnMetadataKeys,j=u.deleteMetadata,W=new S,P={isProviderFor:function(v,_){var b=W.get(v);return!E(b)&&b.has(_)?!0:R(v,_).length?(E(b)&&(b=new g,W.set(v,b)),b.add(_),!0):!1},OrdinaryDefineOwnMetadata:l,OrdinaryHasOwnMetadata:p,OrdinaryGetOwnMetadata:y,OrdinaryOwnMetadataKeys:R,OrdinaryDeleteMetadata:j};return P}function De(u,l,p){var y=A.getProvider(u,l);if(!E(y))return y;if(p){if(A.setProvider(u,l,$))return $;throw new Error("Illegal state.")}}function dr(){var u={},l=[],p=(function(){function P(v,_,b){this._index=0,this._keys=v,this._values=_,this._selector=b}return P.prototype["@@iterator"]=function(){return this},P.prototype[i]=function(){return this},P.prototype.next=function(){var v=this._index;if(v>=0&&v<this._keys.length){var _=this._selector(this._keys[v],this._values[v]);return v+1>=this._keys.length?(this._index=-1,this._keys=l,this._values=l):this._index++,{value:_,done:!1}}return{value:void 0,done:!0}},P.prototype.throw=function(v){throw this._index>=0&&(this._index=-1,this._keys=l,this._values=l),v},P.prototype.return=function(v){return this._index>=0&&(this._index=-1,this._keys=l,this._values=l),{value:v,done:!0}},P})(),y=(function(){function P(){this._keys=[],this._values=[],this._cacheKey=u,this._cacheIndex=-2}return Object.defineProperty(P.prototype,"size",{get:function(){return this._keys.length},enumerable:!0,configurable:!0}),P.prototype.has=function(v){return this._find(v,!1)>=0},P.prototype.get=function(v){var _=this._find(v,!1);return _>=0?this._values[_]:void 0},P.prototype.set=function(v,_){var b=this._find(v,!0);return this._values[b]=_,this},P.prototype.delete=function(v){var _=this._find(v,!1);if(_>=0){for(var b=this._keys.length,x=_+1;x<b;x++)this._keys[x-1]=this._keys[x],this._values[x-1]=this._values[x];return this._keys.length--,this._values.length--,mt(v,this._cacheKey)&&(this._cacheKey=u,this._cacheIndex=-2),!0}return!1},P.prototype.clear=function(){this._keys.length=0,this._values.length=0,this._cacheKey=u,this._cacheIndex=-2},P.prototype.keys=function(){return new p(this._keys,this._values,R)},P.prototype.values=function(){return new p(this._keys,this._values,j)},P.prototype.entries=function(){return new p(this._keys,this._values,W)},P.prototype["@@iterator"]=function(){return this.entries()},P.prototype[i]=function(){return this.entries()},P.prototype._find=function(v,_){if(!mt(this._cacheKey,v)){this._cacheIndex=-1;for(var b=0;b<this._keys.length;b++)if(mt(this._keys[b],v)){this._cacheIndex=b;break}}return this._cacheIndex<0&&_&&(this._cacheIndex=this._keys.length,this._keys.push(v),this._values.push(void 0)),this._cacheIndex},P})();return y;function R(P,v){return P}function j(P,v){return v}function W(P,v){return[P,v]}}function pr(){var u=(function(){function l(){this._map=new f}return Object.defineProperty(l.prototype,"size",{get:function(){return this._map.size},enumerable:!0,configurable:!0}),l.prototype.has=function(p){return this._map.has(p)},l.prototype.add=function(p){return this._map.set(p,p),this},l.prototype.delete=function(p){return this._map.delete(p)},l.prototype.clear=function(){this._map.clear()},l.prototype.keys=function(){return this._map.keys()},l.prototype.values=function(){return this._map.keys()},l.prototype.entries=function(){return this._map.entries()},l.prototype["@@iterator"]=function(){return this.keys()},l.prototype[i]=function(){return this.keys()},l})();return u}function fr(){var u=16,l=h.create(),p=y();return(function(){function v(){this._key=y()}return v.prototype.has=function(_){var b=R(_,!1);return b!==void 0?h.has(b,this._key):!1},v.prototype.get=function(_){var b=R(_,!1);return b!==void 0?h.get(b,this._key):void 0},v.prototype.set=function(_,b){var x=R(_,!0);return x[this._key]=b,this},v.prototype.delete=function(_){var b=R(_,!1);return b!==void 0?delete b[this._key]:!1},v.prototype.clear=function(){this._key=y()},v})();function y(){var v;do v="@@WeakMap@@"+P();while(h.has(l,v));return l[v]=!0,v}function R(v,_){if(!r.call(v,p)){if(!_)return;Object.defineProperty(v,p,{value:h.create()})}return v[p]}function j(v,_){for(var b=0;b<_;++b)v[b]=Math.random()*255|0;return v}function W(v){if(typeof Uint8Array=="function"){var _=new Uint8Array(v);return typeof crypto<"u"?crypto.getRandomValues(_):typeof msCrypto<"u"?msCrypto.getRandomValues(_):j(_,v),_}return j(new Array(v),v)}function P(){var v=W(u);v[6]=v[6]&79|64,v[8]=v[8]&191|128;for(var _="",b=0;b<u;++b){var x=v[b];(b===4||b===6||b===8)&&(_+="-"),x<16&&(_+="0"),_+=x.toString(16).toLowerCase()}return _}}function yt(u){return u.__=void 0,delete u.__,u}})})(Qt||(Qt={}))});O();var Di=Pe(Zt(),1),ge=Pe(require("process"),1);O();O();var Cr=Pe(require("os"),1),Tr=Pe(require("path"),1),Ur=require("url"),Mr=require("util");O();O();function he(n){return n.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}O();var Sr={chat:!0,code:!0,agent:!0,vl:!0},en={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function Rr(n){let e=n.toLowerCase();return Object.hasOwn(en,e)?en[e]:null}function gt(n){let e=n.engine_args?.context_length;return typeof e=="number"&&Number.isFinite(e)&&e>0?e:null}function nn(n,e){let t=e.filter(s=>he(`${s.model_id}-${s.variant}`)===n);if(t.length===1)return t[0];if(t.length>1)return tn(t);let r=e.filter(s=>he(s.model_id)===n),o=new Set(r.map(s=>s.variant));return r.length>0&&o.size===1?tn(r):null}function tn(n){let e=n[0],t=i=>n.every(a=>(a[i]??void 0)===(e[i]??void 0))?e[i]:void 0,r=n.map(gt),o=r[0],s=o!=null&&r.every(i=>i===o)?o:null;return{model_id:e.model_id,variant:e.variant,gpu_spec:t("gpu_spec"),engine:t("engine"),task:t("task"),description:t("description"),size_gb:t("size_gb"),hf_repo:t("hf_repo"),engine_args:s!==null?{context_length:s}:{},updated_at:t("updated_at")}}function rn(n){let e=n.updated_at?Date.parse(n.updated_at):NaN;return Number.isFinite(e)?Math.floor(e/1e3):0}function on(n,e){let t=nn(n,e);if(!t)return{id:n,object:"model",created:0,owned_by:"urun"};let r=gt(t);return{id:n,object:"model",created:rn(t),owned_by:"urun",name:`${t.model_id}:${t.variant}`,...t.description?{description:t.description}:{},...t.task?{task:t.task}:{},...t.engine?{engine:t.engine}:{},...t.gpu_spec?{gpu_spec:t.gpu_spec}:{},...r!==null?{context_length:r}:{}}}function sn(n,e){let t=[];for(let r of n){let o=nn(r,e);if(!o||!o.task||Sr[o.task]!==!0)continue;let s=gt(o),i={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};s!==null&&(i.max_tokens={type:"integer",min:1,max:s,unit:"token"}),t.push({schema_version:"2.4",id:r,name:`${o.model_id}:${o.variant}`,created:rn(o),quantization:Rr(o.variant),description:o.description??"",hugging_face_id:o.hf_repo?o.hf_repo.split("@")[0]??o.hf_repo:"",input_modalities:[{type:"text",...s!==null?{supported_inputs:{max_context_length:{value:s,unit:"token"}}}:{}},...o.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:i}]})}return{data:t}}var z=class extends Error{},Y=class extends Error{},wt="urs1.";function Er(n,e){return wt+Buffer.from(JSON.stringify([n,e]),"utf8").toString("base64url")}function Or(n){if(!n.startsWith(wt))return null;try{let e=JSON.parse(Buffer.from(n.slice(wt.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 un(n){let e=n.fetchImpl??fetch,t=`${n.apiUrl.replace(/\/+$/,"")}/apps`,r=await e(t,{headers:{Authorization:`Bearer ${n.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new Error(`org apps listing failed: GET ${t} \u2192 ${r.status}`);let o=await r.json();if(!Array.isArray(o.apps))throw new Error(`org apps listing returned no "apps" array (GET ${t})`);if(o.truncated===!0)throw new Error(`org apps listing was truncated (GET ${t} returned ${o.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return o.apps}function Ar(n,e){let t=e.filter(s=>he(`${s.model_id}-${s.variant}`)===n);if(t.length>0)return t;let r=e.filter(s=>he(s.model_id)===n);return new Set(r.map(s=>s.variant)).size===1?r:[]}function Ir(n){let e=n.engine_args?.expects_image;return e==null?"edit":e===!1?"generation":e===!0?"edit":null}function Pr(n){let e=n.map(Ir),t=e[0];return t!=null&&e.every(r=>r===t)?t:null}var an=6e4,We=class{constructor(e){this.opts=e}opts;pool=new Map;appsCache=null;rowsCache=null;seed(e,t){this.pool.set(e,Promise.resolve(t))}async deployedApps(){if(!this.opts.listApps)return[];let e=this.opts.appsTtlMs??an;if(this.appsCache&&Date.now()-this.appsCache.at<e)return this.appsCache.apps;let t=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:t},t}async catalogRows(){if(!this.opts.listCatalog)return[];let e=this.opts.appsTtlMs??an;if(this.rowsCache&&Date.now()-this.rowsCache.at<e)return this.rowsCache.rows;let t=await this.opts.listCatalog();return this.rowsCache={at:Date.now(),rows:t},t}servable(e){return e.filter(t=>t.function_name===this.opts.fnName&&t.deployment_status==="active").map(t=>t.app_slug)}async availableIds(){let e=this.opts.defaultApp,t=new Set(e===null?[]:[e]);if(this.opts.listApps)for(let o of this.servable(await this.deployedApps()))t.add(o);for(let o of this.pool.keys())t.add(o);let r=[...t].filter(o=>o!==e).sort();return e===null?r:[e,...r]}async noDefaultApp(e){let t=await this.availableIds(),r=t.length>0?t.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 "${this.opts.fnName}" apps; available models: ${r}`:`model "${e}" does not name a deployed "${this.opts.fnName}" app in your org, and this endpoint has no default model to fall back to; available models: ${r}`)}async resolveApp(e){let t=(e??"").trim(),r=this.opts.defaultApp;if(!t||t==="urun")return r===null?this.noDefaultApp(t):r;let o=he(t);if(o===r)return r;if(this.pool.has(o))return o;if(this.opts.listApps){let s=await this.deployedApps(),i=this.servable(s);if(i.includes(o))return o;let a=i.filter(d=>d.startsWith(`${o}-`));if(a.length===1)return a[0];if(a.length>1)throw new z(`model "${t}" is ambiguous across deployed apps ${a.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let c=s.find(d=>d.app_slug===o||d.app_slug.startsWith(`${o}-`));if(c)throw new z(`model "${t}" maps to app "${c.app_slug}", which is not an active "${this.opts.fnName}" app \u2014 deploy it with \`urun serve ${t}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.listCatalog){let i=(await this.opts.listCatalog()).find(a=>t===a.model_id||t===`${a.model_id}:${a.variant}`||o===he(a.model_id)||o===he(`${a.model_id}-${a.variant}`));if(i)throw new z(`model "${t}" 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(t):r}async sessionFor(e){let t=await this.resolveApp(e),r=this.pool.get(t);return r||(r=Promise.resolve(this.opts.openSession(t)),this.pool.set(t,r),r.catch(()=>this.pool.delete(t))),{app:t,entry:await r}}keyOf(e){let t=this.opts.sessionKey;if(!t)throw new Error("ModelRouter: sessionKey is not configured \u2014 the session-identity seam (handleFor/sessionForHandle) is unavailable on this router");let r=t(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:t,entry:r}=await this.sessionFor(e);return{app:t,handle:Er(t,this.keyOf(r))}}async sessionForHandle(e){let t=Or(e);if(!t)throw new Y(`malformed session handle ${JSON.stringify(e)} \u2014 not issued by this proxy`);let{app:r,key:o}=t,s=this.pool.get(r),i=s?await s.then(a=>a,()=>null):null;if(i===null)throw new Y(`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 Y(`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,t){let r=this.pool.get(e);!r||await r.then(s=>s,()=>null)!==t||this.pool.get(e)!==r||(this.pool.delete(e),await this.opts.closeSession(t).catch(()=>{}))}async modelList(){let e=await this.availableIds(),t=await this.catalogRows().catch(()=>[]);return{object:"list",data:e.map(r=>on(r,t))}}async openRouterModels(){let e=await this.availableIds(),t=await this.catalogRows();return sn(e,t)}async supportsImages(e,t){let r=await this.resolveApp(e),o=await this.catalogRows(),s=Ar(r,o);return s.length===0||!s.every(i=>(i.task??void 0)==="image")?!1:Pr(s)===t}async closeAll(){let e=[...this.pool.values()];this.pool.clear();let t=[];if(await Promise.all(e.map(async r=>{try{await this.opts.closeSession(await r)}catch(o){t.push(String(o instanceof Error?o.message:o))}})),t.length>0)throw new Error(`failed to close ${t.length} pooled session(s): ${t.join("; ")}`)}};function cn(n,e="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(n).pathname))throw new Error(`${e} must be the session-gateway base (e.g. https://api.urun.sh), got ${n} \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 n}function Je(n,e="URUN_GATEWAY_URL"){let t=new URL(n),r=t.hostname==="localhost"||t.hostname==="127.0.0.1"||t.hostname==="[::1]";if(t.protocol!=="https:"&&!r)throw new Error(`${e} must be https (got ${n}) \u2014 the org API key rides the request's Authorization header`);return n}var Nr=8080,vt="serve",_t="0.0.0.0";function $r(n,e,t){let r=(n[e]??"").trim();if(!r)throw new Error(`${e} is not set \u2014 ${t}`);return r}function ln(n=process.env){let e=(n.PORT??"").trim(),t=e===""?Nr:Number(e);if(!Number.isInteger(t)||t<0||t>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let r=cn(Je($r(n,"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=(n.URUN_API_URL??"").trim(),s=o?Je(o,"URUN_API_URL"):`${r.replace(/\/+$/,"")}/v1`,i=(n.URUN_CATALOG_URL??"").trim(),a=(n.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 c=i?{catalogUrl:Je(i,"URUN_CATALOG_URL"),anonKey:a}:null;return{port:t,baseUrl:r,apiUrl:s,catalog:c}}O();var tr=require("http");O();O();var kt=class{constructor(e,t){this.backend=e;this.opts=t}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 t=Buffer.from(e,"base64"),r=Math.floor(t.byteLength/2),o=new Int16Array(r);for(let s=0;s<r;s++)o[s]=t.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("rt-audio-out").track is null)');this.sink=this.backend.createSink(e),this.sink.onframe=t=>{let r=Buffer.from(t.samples.buffer,t.samples.byteOffset,t.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 fn(n,e,t=1e4){let r=new kt(e,{sampleRate:24e3}),o=await r.startOutbound();await n.stream("rt-audio-in").attach(o);let s=await Lr(n,t);return await r.startInbound(s),r}function Lr(n,e){let t=n.stream("rt-audio-out");return t.track!=null?Promise.resolve(t.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=c=>{c!=null&&(clearTimeout(i),s?.(),r(c))};s=t.on?.("track",a),t.track!=null&&a(t.track)})}var dn="rt-video-in",pn=256e3;function mn(n){let e=n.stream(dn),t=e.emit;if(typeof t!="function")throw new Error(`enableSessionVideo: session.stream('${dn}') 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>pn)throw new Error(`enableSessionVideo: frame is ${r.byteLength} bytes \u2014 over the ${pn}-byte data-channel budget (the SFU's 262144-byte SCTP ceiling drops oversize messages SILENTLY; send smaller/lower-quality frames)`);await t.call(e,r)}}}var Dr=24e3,Fe=480,Fr=111;async function Hr(){let n=await import("opusscript"),e=n.default??n;return new e(Dr,1,2048)}async function hn(){let n=await import("werift"),e=await Hr();return{createSource:()=>{let t=new n.MediaStreamTrack({kind:"audio"}),r=Math.random()*4294967295>>>0,o=Math.random()*65535&65535,s=Math.random()*4294967295>>>0,i=new Int16Array(0);return{track:t,onData:a=>{let c=new Int16Array(i.length+a.length);c.set(i,0),c.set(a,i.length);let d=0;for(;c.length-d>=Fe;){let h=c.subarray(d,d+Fe);d+=Fe;let m=Buffer.from(h.buffer,h.byteOffset,h.byteLength),f=e.encode(m,Fe);o=o+1&65535,s=s+Fe>>>0;let g=new n.RtpHeader({version:2,payloadType:Fr,sequenceNumber:o,timestamp:s,ssrc:r,marker:!1}),S=new n.RtpPacket(g,f);t.writeRtp(S)}i=c.slice(d)}}},createSink:t=>{let r={onframe:null};return t.onReceiveRtp.subscribe(s=>{let i;try{i=e.decode(s.payload)}catch{return}let a=new Int16Array(i.length/2);for(let c=0;c<a.length;c++)a[c]=i.readInt16LE(c*2);r.onframe?.({samples:a})}),r}}}O();var Ke=require("@urun-sh/core"),bt="rt-video-out";var Br="h264";function jr(n,e){if(!Number.isInteger(n.seq)||n.seq<0)throw new Error(`${e}: header seq must be a non-negative integer (got ${n.seq})`);if(!Number.isInteger(n.ptsUs))throw new Error(`${e}: header ptsUs must be an integer microsecond timestamp (got ${n.ptsUs})`);if(typeof n.codec!="string"||n.codec.length===0)throw new Error(`${e}: header codec must be a non-empty string`);if(typeof n.key!="boolean")throw new Error(`${e}: header key must be a boolean`)}function Gr(n){if(n.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${n.byteLength} byte(s), no header length)`);let e=n[0]|n[1]<<8;if(n.byteLength<2+e)throw new Error(`decodeVideoOutRecord: truncated record (header says ${e} bytes, ${n.byteLength-2} present)`);let t;try{t=JSON.parse(new TextDecoder().decode(n.subarray(2,2+e)))}catch(i){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(i)})`)}let r=t;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;jr(o,"decodeVideoOutRecord");let s=n.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 yn="urun.videoOut.v2-ws";function gn(n){let e=n.stream(bt);if(typeof e.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${bt}') 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 t=new Set,r=new Set,o=new Set,s=c=>{for(let d of r)d(c)},i=!1,a=async()=>{for await(let c of e.messages()){if(!(c instanceof Uint8Array)){s(new Error(`openSessionVideoOutLane: "${bt}" delivered a non-BINARY \xA75 payload (${typeof c}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let d;try{d=Gr(c)}catch(h){s(h instanceof Error?h:new Error(String(h)));continue}for(let h of t)h(d)}for(let c of o)c()};return{codec:Br,onOutputFrame:c=>(t.add(c),i||(i=!0,a().catch(d=>s(d instanceof Error?d:new Error(String(d))))),()=>t.delete(c)),onLaneError:c=>(r.add(c),()=>r.delete(c)),onEnd:c=>(o.add(c),()=>o.delete(c))}}var Xe="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",qr=1007,vn=1008,ye=1011,Qe=1008,w=class extends Error{constructor(t,r=qr){super(t);this.closeCode=r}closeCode},V=n=>typeof n=="object"&&n!==null&&!Array.isArray(n)?n:null,ze=["setup","clientContent","realtimeInput","toolResponse"];function _n(n){let e;try{e=JSON.parse(n)}catch{throw new w("client message is not valid JSON")}let t=V(e);if(!t)throw new w("client message must be a JSON object");let r=ze.filter(a=>t[a]!==void 0),o=Object.keys(t).filter(a=>!ze.includes(a));if(o.length>0)throw new w(`unknown client message field(s) ${JSON.stringify(o)} \u2014 expected exactly one of ${ze.join(", ")}`);if(r.length!==1)throw new w(`client message must contain exactly one of ${ze.join(", ")} (got ${r.length})`);let s=r[0],i=V(t[s]);if(!i)throw new w(`"${s}" must be a JSON object`);return{kind:s,[s]:i}}function kn(n,e){if(typeof n=="string")return n;let t=V(n),r=t?t.parts:void 0;if(!Array.isArray(r))throw new w(`${e}: expected a Content with a parts array`);let o=[];for(let s of r){let i=V(s);if(i&&typeof i.text=="string"){o.push(i.text);continue}throw i&&(i.inlineData!==void 0||i.fileData!==void 0)?new w(`${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 w(`${e}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new w(`${e}: unsupported part ${JSON.stringify(s)}`)}return o.join(`
3
- `)}var Wr={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},xe="urun.videoOut";function bn(n){return{framing:yn,codec:n}}function xn(n){let{seq:e,ptsUs:t,codec:r,key:o}=n.header;return{[xe]:{v:1,seq:e,ptsUs:t,codec:r,key:o,data:Buffer.from(n.au).toString("base64")}}}var Jr={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},Kr=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",xe],wn=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function zr(n){let e=V(n);if(!e)throw new w("setup.realtimeInputConfig must be an object");for(let o of Object.keys(e))if(!(o==="automaticActivityDetection"||o==="activityHandling"))throw new w(o==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${o}"`);let t=V(e.automaticActivityDetection);if(e.automaticActivityDetection!==void 0&&!t)throw new w("realtimeInputConfig.automaticActivityDetection must be an object");for(let o of Object.keys(t??{}))if(o!=="disabled")throw new w(`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(t?.disabled!==!0)throw new w("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(!wn.includes(e.activityHandling))throw new w(`unsupported activityHandling ${JSON.stringify(e.activityHandling)} \u2014 expected one of ${wn.join(", ")}`);r=e.activityHandling}return{manualActivity:!0,activityHandling:r}}var Yr=["temperature","maxOutputTokens","responseModalities"];function Sn(n){for(let[m,f]of Object.entries(Jr))if(n[m]!==void 0)throw new w(f);for(let m of Object.keys(n))if(!Kr.includes(m))throw new w(`unsupported setup field "${m}"`);if(typeof n.model!="string"||n.model.length===0)throw new w('setup.model is required (format "models/{model}")');let e=n.model.replace(/^models\//,""),t,r,o=!1,s=V(n.generationConfig);if(n.generationConfig!==void 0&&!s)throw new w("setup.generationConfig must be an object");if(s){for(let m of Object.keys(s))if(!Yr.includes(m))throw new w(`unsupported generationConfig field "${m}"`);if(s.temperature!==void 0){if(typeof s.temperature!="number")throw new w("generationConfig.temperature must be a number");t=s.temperature}if(s.maxOutputTokens!==void 0){if(typeof s.maxOutputTokens!="number")throw new w("generationConfig.maxOutputTokens must be a number");r=s.maxOutputTokens}if(s.responseModalities!==void 0){let m=Array.isArray(s.responseModalities)?s.responseModalities:[s.responseModalities];for(let f of m){let g=String(f).toUpperCase();if(g==="AUDIO"){o=!0;continue}if(g!=="TEXT")throw new w(`responseModalities ${JSON.stringify(f)} is not supported \u2014 this surface speaks TEXT and AUDIO (audio-out rides the native AudioBridge rt-audio-out lane)`)}}}let i=[];n.systemInstruction!==void 0&&i.push({role:"system",content:kn(n.systemInstruction,"setup.systemInstruction")});let a;if(n.tools!==void 0){if(!Array.isArray(n.tools))throw new w("setup.tools must be an array");a=[];for(let m of n.tools){let f=V(m);if(!f)throw new w("setup.tools entries must be objects");for(let g of Object.keys(f))if(g!=="functionDeclarations")throw new w(`unsupported tool "${g}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(f.functionDeclarations))throw new w("tool.functionDeclarations must be an array");for(let g of f.functionDeclarations){let S=V(g);if(!S||typeof S.name!="string")throw new w("functionDeclarations entries require a string name");a.push({type:"function",name:S.name,description:S.description,parameters:S.parameters})}}a.length===0&&(a=void 0)}let c={enabled:!1,handle:null};if(n.sessionResumption!==void 0){let m=V(n.sessionResumption);if(!m)throw new w("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let f of Object.keys(m))if(f!=="handle")throw new w(`unsupported sessionResumption field "${f}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(m.handle!==void 0&&(typeof m.handle!="string"||m.handle.length===0))throw new w("sessionResumption.handle must be a non-empty string when present");c={enabled:!0,handle:typeof m.handle=="string"?m.handle:null}}let d=n.realtimeInputConfig===void 0?Wr:zr(n.realtimeInputConfig),h=!1;if(n[xe]!==void 0){if(!V(n[xe]))throw new w(`setup["${xe}"] must be an object (the v1 opt-in is {})`);h=!0}return{model:e,systemItems:i,tools:a,temperature:t,maxOutputTokens:r,audioModality:o,activity:d,sessionResumption:c,videoOut:h}}function Rn(n){return{sessionResumptionUpdate:{newHandle:n,resumable:!0}}}function En(n){let e=Math.max(0,n??0)/1e3;return{goAway:{timeLeft:`${Math.round(e*1e3)/1e3}s`}}}function On(n){for(let r of Object.keys(n))if(r!=="turns"&&r!=="turnComplete")throw new w(`unsupported clientContent field "${r}"`);let e=n.turns===void 0?[]:n.turns;if(!Array.isArray(e))throw new w("clientContent.turns must be an array");let t=[];for(let r of e){let o=V(r);if(!o)throw new w("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 w(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(o.role)})`);t.push({role:s==="model"?"assistant":"user",content:kn(o,"clientContent.turns")})}return{items:t,turnComplete:n.turnComplete===!0}}var Ye=16e3,An=24e3,Xr=`audio/pcm;rate=${An}`,In=Buffer.alloc(960).toString("base64");function Qr(n,e,t){if(e===t)return n;let r=Math.floor(n.length*t/e),o=new Int16Array(r),s=e/t,i=n.length-1;for(let a=0;a<r;a++){let c=a*s,d=Math.floor(c);if(d>=i){o[a]=n[i];continue}let h=c-d;o[a]=Math.round(n[d]+(n[d+1]-n[d])*h)}return o}function Zr(n){let e=V(n);if(!e)throw new w("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(e.mimeType))throw new w(`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 w("realtimeInput.video.data must be non-empty base64");let t=Buffer.from(e.data,"base64");if(t.length<3||t[0]!==255||t[1]!==216)throw new w("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(t)}function eo(n){let e=V(n);if(!e)throw new w("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(e.mimeType))throw new w(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(e.mimeType)}`);let t=/;\s*rate=(\d+)/.exec(e.mimeType);if(t&&Number(t[1])!==Ye)throw new w(`realtimeInput.audio.mimeType declares rate=${t[1]} \u2014 the Live spec input format is ${Ye} Hz (audio/pcm;rate=${Ye})`);if(typeof e.data!="string"||e.data.length===0)throw new w("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 w(`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=Qr(o,Ye,An);return Buffer.from(s.buffer,s.byteOffset,s.byteLength).toString("base64")}function Pn(n){if(n.mediaChunks!==void 0)throw new w('realtimeInput.mediaChunks is deprecated by the Live API \u2014 send the audio stream via realtimeInput.audio ({mimeType:"audio/pcm;rate=16000", data}) instead');if(n.activityStart!==void 0||n.activityEnd!==void 0){if(Object.keys(n).length!==1||n.activityStart!==void 0&&n.activityEnd!==void 0)throw new w("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let o=n.activityStart!==void 0?"activityStart":"activityEnd",s=V(n[o]);if(!s||Object.keys(s).length!==0)throw new w(`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(n))if(!e.includes(r))throw new w(`unsupported realtimeInput field "${r}"`);let t=e.filter(r=>n[r]!==void 0);if(t.length!==1)throw new w(`realtimeInput must carry exactly one of ${e.join(", ")} (got ${t.length})`);if(n.audio!==void 0)return{kind:"audio",base64Pcm24k:eo(n.audio)};if(n.video!==void 0)return{kind:"video",jpegFrame:Zr(n.video)};if(n.audioStreamEnd!==void 0){if(n.audioStreamEnd!==!0)throw new w("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof n.text!="string"||n.text.length===0)throw new w("realtimeInput.text must be a non-empty string");return{kind:"text",text:n.text}}function Cn(n){for(let e of Object.keys(n))if(e!=="functionResponses")throw new w(`unsupported toolResponse field "${e}"`);if(!Array.isArray(n.functionResponses)||n.functionResponses.length===0)throw new w("toolResponse.functionResponses must be a non-empty array");return n.functionResponses.map(e=>{let t=V(e);if(!t||typeof t.id!="string"||t.id.length===0)throw new w("functionResponses entries require the string id matched from the toolCall");return{type:"function_call_output",call_id:t.id,output:JSON.stringify(t.response??null)}})}function Tn(n){return n.type!=="response.output_text.delta"||typeof n.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:n.delta}]}}}}function Un(n){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:Xr,data:n}}]}}}}function Mn(n){let e=V(n)??{},t=Array.isArray(e.output)?e.output:[],r=[],o=[],s=typeof e.output_text=="string"?e.output_text:"";if(!s)for(let d of t){let h=V(d);if(!(h?.type!=="message"||!Array.isArray(h.content)))for(let m of h.content){let f=V(m);f?.type==="output_text"&&typeof f.text=="string"&&(s+=f.text)}}s&&o.push({role:"assistant",content:s});for(let d of t){let h=V(d);if(h?.type!=="function_call")continue;if(typeof h.call_id!="string"||typeof h.name!="string")throw new w(`upstream function_call item is missing call_id/name: ${JSON.stringify(d)}`,ye);let m=typeof h.arguments=="string"&&h.arguments.length>0?h.arguments:"{}",f;try{f=JSON.parse(m)}catch{throw new w(`upstream function_call arguments are not valid JSON: ${m}`,ye)}r.push({id:h.call_id,name:h.name,args:f}),o.push({type:"function_call",call_id:h.call_id,name:h.name,arguments:m})}let i=[];r.length>0&&i.push({toolCall:{functionCalls:r}}),i.push({serverContent:{generationComplete:!0}});let a=V(e.usage),c={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&&(c.usageMetadata=d)}return i.push(c),{messages:i,assistantItems:o}}O();var Nn=require("crypto"),$n=require("ws");var to=256,xt=class{snapshots=new Map;store(e,t,r){for(this.snapshots.set(`${e}\0${t}`,r);this.snapshots.size>to;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(e,t){return this.snapshots.get(`${e}\0${t}`)}},no="local",He=n=>n.slice(0,120),St=class{constructor(e,t,r){this.ws=e;this.call=t;this.registry=r;this.clients=t.clients,this.scope=t.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(vn,He(e.message));return}if(e instanceof Y){this.ws.close(Qe,He(e.message));return}if(e instanceof w){this.ws.close(e.closeCode,He(e.message));return}this.ws.close(ye,He(String(e instanceof Error?e.message:e)))}onMessage(e){try{let t=_n(typeof e=="string"?e:Buffer.concat(Array.isArray(e)?e:[e]).toString("utf8"));if(t.kind==="setup"){this.handleSetup(t.setup).catch(o=>this.fail(o));return}if(!this.config)throw new w(`"${t.kind}" before setup \u2014 the first client message must be BidiGenerateContentSetup`);let r=this.config;if(t.kind==="clientContent"){let{items:o,turnComplete:s}=On(t.clientContent);this.transcript.push(...o),s&&this.enqueue(()=>this.runTurn());return}if(t.kind==="realtimeInput"){let o=Pn(t.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:In);return}this.transcript.push(...Cn(t.toolResponse)),this.enqueue(()=>this.runTurn())}catch(t){this.fail(t)}}async handleSetup(e){if(this.config)throw new w("setup may only be sent once, as the first client message");let t=Sn(e),r=t.sessionResumption.handle;if(r!==null){let s=this.registry.get(this.scope,r);if(!s)throw new w("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",Qe);if(s.model!==t.model)throw new w(`sessionResumption.handle was issued for model "${s.model}" \u2014 the model cannot change on resume (got "${t.model}", per the Live API session-resumption contract)`);if(t.systemItems.length>0)throw new w("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(t.model),this.proxyHandle!==s.proxyHandle)throw new w(`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`,Qe);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(t.model),this.unsubscribeEnd=await this.clients.onSessionEnd(this.proxyHandle,s=>this.enqueue(()=>this.handleUpstreamEnd(s))),this.transcript.push(...t.systemItems);this.config=t;let o={};if(t.videoOut){let s=this.clients.openVideoOut;if(s){let i=await s.call(this.clients,t.model),a=i.onOutputFrame(d=>this.send(xn(d))),c=i.onLaneError(d=>this.fail(d));this.detachVideoOut=()=>{a(),c()},o[xe]=bn(i.codec)}}this.send({setupComplete:o}),t.audioModality&&this.ensureAudioLane(),t.sessionResumption.enabled&&await this.mintResumptionUpdate()}ensureAudioLane(){if(this.audio)return this.audio;let e=this.config;if(!e)throw new w("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let t=this.clients.openAudio;if(!t)throw new w("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native rt-audio-in/rt-audio-out AudioBridge seam)",ye);return this.audio=(async()=>{let r=await t.call(this.clients,e.model);return this.detachAudio=r.onOutputAudio(o=>this.send(Un(o))),r})(),this.audio.catch(r=>this.fail(r)),this.audio}appendAudio(e){this.ensureAudioLane().then(t=>t.appendInputAudio(e)).catch(t=>this.fail(t))}ensureVideoLane(){if(this.video)return this.video;let e=this.config;if(!e)throw new w("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let t=this.clients.openVideo;if(!t)throw new w("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",ye);return this.video=Promise.resolve(t.call(this.clients,e.model)),this.video.catch(r=>this.fail(r)),this.video}appendVideoFrame(e){this.ensureVideoLane().then(t=>t.sendInputFrame(e)).catch(t=>this.fail(t))}onActivity(e,t){if(!t.activity.manualActivity)throw new w(`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 w("activityStart while an activity window is already open (send activityEnd first)");this.activityOpen=!0,t.activity.activityHandling==="START_OF_ACTIVITY_INTERRUPTS"&&this.activeTurn&&(this.activeTurn.interrupted=!0);return}if(!this.activityOpen)throw new w("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(t=>this.fail(t))}async mintResumptionUpdate(){let e=this.config;if(!e?.sessionResumption.enabled||this.ws.readyState!==this.ws.OPEN)return;let t=await this.clients.sessionHandle(e.model);this.proxyHandle=t;let r=(0,Nn.randomUUID)();this.registry.store(this.scope,r,{model:e.model,proxyHandle:t,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(r),this.send(Rn(r))}async handleUpstreamEnd(e){if(this.ws.readyState!==this.ws.OPEN||!this.config)return;this.unsubscribeEnd?.(),this.unsubscribeEnd=null;let t=null;try{t=await this.clients.sessionHandle(this.config.model)}catch{}if(t!==null&&t!==this.proxyHandle){this.proxyHandle=t,this.unsubscribeEnd=await this.clients.onSessionEnd(t,r=>this.enqueue(()=>this.handleUpstreamEnd(r))),this.config.sessionResumption.enabled&&await this.mintResumptionUpdate();return}this.send(En(e.timeLeftMs)),this.ws.close(ye,He(`upstream session ended: ${e.reason}`))}async runTurn(){let e=this.config;if(!e||this.ws.readyState!==this.ws.OPEN)return;let t={interrupted:!1};this.activeTurn=t;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 c of r){if(t.interrupted)break;let d=c;if(d.type==="error")throw new w(`upstream error: ${JSON.stringify(d)}`,ye);if(d.type==="response.completed"){o=d.response,s=!0;continue}let h=Tn(d);h&&this.send(h)}if(t.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!s)throw new w("upstream produced no response.completed event",ye);let{messages:i,assistantItems:a}=Mn(o);this.transcript.push(...a);for(let c of i)this.send(c);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},Se=class extends Error{constructor(t,r){super(r);this.status=t;this.name="UpgradeAuthError"}status};function ro(n,e,t){let r=new Set;(c=>{if(typeof c=="string")c!==""&&r.add(c);else if(Array.isArray(c))for(let d of c)d!==""&&r.add(d)})(n.headers["x-goog-api-key"]);let s=n.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 Se(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(t&&i!==void 0&&i!=="")throw new Se(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 c of r)a=c;if(a===void 0&&t)throw new Se(401,"missing credentials: send the org API key in the x-goog-api-key header or as Authorization: Bearer");return a??""}function Rt(n,e){let t=e.authenticate!==void 0;if(t&&(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(!t&&e.clients===void 0)throw new TypeError("attachGeminiLive: pass fixed `clients` (local lane) or `authenticate` (hosted lane)");let r=new $n.WebSocketServer({noServer:!0}),o=new xt;n.on("upgrade",(i,a,c)=>{let d=new URL(i.url??"/","http://localhost");d.pathname===Xe&&(a.on("error",()=>{}),(async()=>{let h=ro(i,d,t),m;if(e.authenticate)m=await e.authenticate(h,i);else{if(e.apiKey!==void 0&&h!==e.apiKey)throw new Se(401,"invalid local proxy api key");m={clients:e.clients,scope:no}}r.handleUpgrade(i,a,c,f=>{new St(f,m,o)})})().catch(h=>{let m=502;(h instanceof Se||typeof h=="object"&&h!==null&&"status"in h&&typeof h.status=="number")&&(m=h.status);let f=m===401?"Unauthorized":m===403?"Forbidden":m===502?"Bad Gateway":"Internal Server Error",g=h instanceof Error?h.message:String(h);a.destroyed||(a.write(`HTTP/1.1 ${m} ${f}\r
2
+ "use strict";var ms=Object.create;var Or=Object.defineProperty;var gs=Object.getOwnPropertyDescriptor;var hs=Object.getOwnPropertyNames;var ys=Object.getPrototypeOf,_s=Object.prototype.hasOwnProperty;var ws=(t,e,n)=>()=>{if(n)throw n[0];try{return t&&(e=t(t=0)),e}catch(r){throw n=[r],r}};var vs=(t,e)=>()=>{try{return e||t((e={exports:{}}).exports,e),e.exports}catch(n){throw e=0,n}};var bs=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of hs(e))!_s.call(t,o)&&o!==n&&Or(t,o,{get:()=>e[o],enumerable:!(r=gs(e,o))||r.enumerable});return t};var ze=(t,e,n)=>(n=t!=null?ms(ys(t)):{},bs(e||!t||!t.__esModule?Or(n,"default",{value:t,enumerable:!0}):n,t));var _=ws(()=>{"use strict"});var Tr=vs(()=>{"use strict";_();var Ir;(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,c){Object.defineProperty(u,l,{configurable:!0,writable:!0,value:c}),d&&d(l,c)}}function i(){try{return Function("return this;")()}catch{}}function s(){try{return(0,eval)("(function() { return this; })()")}catch{}}function a(){return i()||s()}})(function(e,n){var r=Object.prototype.hasOwnProperty,o=typeof Symbol=="function",i=o&&typeof Symbol.toPrimitive<"u"?Symbol.toPrimitive:"@@toPrimitive",s=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 Sn(Object.create(null))}:u?function(){return Sn({__proto__:null})}:function(){return Sn({})},has:d?function(p,f){return r.call(p,f)}:function(p,f){return f in p},get:d?function(p,f){return r.call(p,f)?p[f]:void 0}:function(p,f){return p[f]}},c=Object.getPrototypeOf(Function),y=typeof Map=="function"&&typeof Map.prototype.entries=="function"?Map:ds(),g=typeof Set=="function"&&typeof Set.prototype.entries=="function"?Set:ls(),E=typeof WeakMap=="function"?WeakMap:cs(),w=o?Symbol.for("@reflect-metadata:registry"):void 0,h=ss(),b=as(h);function R(p,f,m,v){if(P(m)){if(!vr(p))throw new TypeError;if(!br(f))throw new TypeError;return ge(p,f)}else{if(!vr(p))throw new TypeError;if(!D(f))throw new TypeError;if(!D(v)&&!P(v)&&!he(v))throw new TypeError;return he(v)&&(v=void 0),m=Se(m),ce(p,f,m,v)}}e("decorate",R);function T(p,f){function m(v,C){if(!D(v))throw new TypeError;if(!P(C)&&!os(C))throw new TypeError;ue(p,f,v,C)}return m}e("metadata",T);function B(p,f,m,v){if(!D(m))throw new TypeError;return P(v)||(v=Se(v)),ue(p,f,m,v)}e("defineMetadata",B);function M(p,f,m){if(!D(f))throw new TypeError;return P(m)||(m=Se(m)),ne(p,f,m)}e("hasMetadata",M);function $(p,f,m){if(!D(f))throw new TypeError;return P(m)||(m=Se(m)),N(p,f,m)}e("hasOwnMetadata",$);function k(p,f,m){if(!D(f))throw new TypeError;return P(m)||(m=Se(m)),J(p,f,m)}e("getMetadata",k);function H(p,f,m){if(!D(f))throw new TypeError;return P(m)||(m=Se(m)),X(p,f,m)}e("getOwnMetadata",H);function W(p,f){if(!D(p))throw new TypeError;return P(f)||(f=Se(f)),xe(p,f)}e("getMetadataKeys",W);function z(p,f){if(!D(p))throw new TypeError;return P(f)||(f=Se(f)),Re(p,f)}e("getOwnMetadataKeys",z);function j(p,f,m){if(!D(f))throw new TypeError;if(P(m)||(m=Se(m)),!D(f))throw new TypeError;P(m)||(m=Se(m));var v=st(f,m,!1);return P(v)?!1:v.OrdinaryDeleteMetadata(p,f,m)}e("deleteMetadata",j);function ge(p,f){for(var m=p.length-1;m>=0;--m){var v=p[m],C=v(f);if(!P(C)&&!he(C)){if(!br(C))throw new TypeError;f=C}}return f}function ce(p,f,m,v){for(var C=p.length-1;C>=0;--C){var Z=p[C],ie=Z(f,m,v);if(!P(ie)&&!he(ie)){if(!D(ie))throw new TypeError;v=ie}}return v}function ne(p,f,m){var v=N(p,f,m);if(v)return!0;var C=Rn(f);return he(C)?!1:ne(p,C,m)}function N(p,f,m){var v=st(f,m,!1);return P(v)?!1:wr(v.OrdinaryHasOwnMetadata(p,f,m))}function J(p,f,m){var v=N(p,f,m);if(v)return X(p,f,m);var C=Rn(f);if(!he(C))return J(p,C,m)}function X(p,f,m){var v=st(f,m,!1);if(!P(v))return v.OrdinaryGetOwnMetadata(p,f,m)}function ue(p,f,m,v){var C=st(m,v,!0);C.OrdinaryDefineOwnMetadata(p,f,m,v)}function xe(p,f){var m=Re(p,f),v=Rn(p);if(v===null)return m;var C=xe(v,f);if(C.length<=0)return m;if(m.length<=0)return C;for(var Z=new g,ie=[],q=0,S=m;q<S.length;q++){var A=S[q],O=Z.has(A);O||(Z.add(A),ie.push(A))}for(var I=0,G=C;I<G.length;I++){var A=G[I],O=Z.has(A);O||(Z.add(A),ie.push(A))}return ie}function Re(p,f){var m=st(p,f,!1);return m?m.OrdinaryOwnMetadataKeys(p,f):[]}function ve(p){if(p===null)return 1;switch(typeof p){case"undefined":return 0;case"boolean":return 2;case"string":return 3;case"symbol":return 4;case"number":return 5;case"object":return p===null?1:6;default:return 6}}function P(p){return p===void 0}function he(p){return p===null}function Ue(p){return typeof p=="symbol"}function D(p){return typeof p=="object"?p!==null:typeof p=="function"}function de(p,f){switch(ve(p)){case 0:return p;case 1:return p;case 2:return p;case 3:return p;case 4:return p;case 5:return p}var m=f===3?"string":f===5?"number":"default",v=kr(p,i);if(v!==void 0){var C=v.call(p,m);if(D(C))throw new TypeError;return C}return Ot(p,m==="default"?"number":m)}function Ot(p,f){if(f==="string"){var m=p.toString;if(Ke(m)){var v=m.call(p);if(!D(v))return v}var C=p.valueOf;if(Ke(C)){var v=C.call(p);if(!D(v))return v}}else{var C=p.valueOf;if(Ke(C)){var v=C.call(p);if(!D(v))return v}var Z=p.toString;if(Ke(Z)){var v=Z.call(p);if(!D(v))return v}}throw new TypeError}function wr(p){return!!p}function rs(p){return""+p}function Se(p){var f=de(p,3);return Ue(f)?f:rs(f)}function vr(p){return Array.isArray?Array.isArray(p):p instanceof Object?p instanceof Array:Object.prototype.toString.call(p)==="[object Array]"}function Ke(p){return typeof p=="function"}function br(p){return typeof p=="function"}function os(p){switch(ve(p)){case 3:return!0;case 4:return!0;default:return!1}}function xn(p,f){return p===f||p!==p&&f!==f}function kr(p,f){var m=p[f];if(m!=null){if(!Ke(m))throw new TypeError;return m}}function xr(p){var f=kr(p,s);if(!Ke(f))throw new TypeError;var m=f.call(p);if(!D(m))throw new TypeError;return m}function Rr(p){return p.value}function Sr(p){var f=p.next();return f.done?!1:f}function Er(p){var f=p.return;f&&f.call(p)}function Rn(p){var f=Object.getPrototypeOf(p);if(typeof p!="function"||p===c||f!==c)return f;var m=p.prototype,v=m&&Object.getPrototypeOf(m);if(v==null||v===Object.prototype)return f;var C=v.constructor;return typeof C!="function"||C===p?f:C}function is(){var p;!P(w)&&typeof n.Reflect<"u"&&!(w in n.Reflect)&&typeof n.Reflect.defineMetadata=="function"&&(p=us(n.Reflect));var f,m,v,C=new E,Z={registerProvider:ie,getProvider:S,setProvider:O};return Z;function ie(I){if(!Object.isExtensible(Z))throw new Error("Cannot add provider to a frozen registry.");switch(!0){case p===I:break;case P(f):f=I;break;case f===I:break;case P(m):m=I;break;case m===I:break;default:v===void 0&&(v=new g),v.add(I);break}}function q(I,G){if(!P(f)){if(f.isProviderFor(I,G))return f;if(!P(m)){if(m.isProviderFor(I,G))return f;if(!P(v))for(var K=xr(v);;){var ee=Sr(K);if(!ee)return;var be=Rr(ee);if(be.isProviderFor(I,G))return Er(K),be}}}if(!P(p)&&p.isProviderFor(I,G))return p}function S(I,G){var K=C.get(I),ee;return P(K)||(ee=K.get(G)),P(ee)&&(ee=q(I,G),P(ee)||(P(K)&&(K=new y,C.set(I,K)),K.set(G,ee))),ee}function A(I){if(P(I))throw new TypeError;return f===I||m===I||!P(v)&&v.has(I)}function O(I,G,K){if(!A(K))throw new Error("Metadata provider not registered.");var ee=S(I,G);if(ee!==K){if(!P(ee))return!1;var be=C.get(I);P(be)&&(be=new y,C.set(I,be)),be.set(G,K)}return!0}}function ss(){var p;return!P(w)&&D(n.Reflect)&&Object.isExtensible(n.Reflect)&&(p=n.Reflect[w]),P(p)&&(p=is()),!P(w)&&D(n.Reflect)&&Object.isExtensible(n.Reflect)&&Object.defineProperty(n.Reflect,w,{enumerable:!1,configurable:!1,writable:!1,value:p}),p}function as(p){var f=new E,m={isProviderFor:function(A,O){var I=f.get(A);return P(I)?!1:I.has(O)},OrdinaryDefineOwnMetadata:ie,OrdinaryHasOwnMetadata:C,OrdinaryGetOwnMetadata:Z,OrdinaryOwnMetadataKeys:q,OrdinaryDeleteMetadata:S};return h.registerProvider(m),m;function v(A,O,I){var G=f.get(A),K=!1;if(P(G)){if(!I)return;G=new y,f.set(A,G),K=!0}var ee=G.get(O);if(P(ee)){if(!I)return;if(ee=new y,G.set(O,ee),!p.setProvider(A,O,m))throw G.delete(O),K&&f.delete(A),new Error("Wrong provider for target.")}return ee}function C(A,O,I){var G=v(O,I,!1);return P(G)?!1:wr(G.has(A))}function Z(A,O,I){var G=v(O,I,!1);if(!P(G))return G.get(A)}function ie(A,O,I,G){var K=v(I,G,!0);K.set(A,O)}function q(A,O){var I=[],G=v(A,O,!1);if(P(G))return I;for(var K=G.keys(),ee=xr(K),be=0;;){var Ar=Sr(ee);if(!Ar)return I.length=be,I;var ps=Rr(Ar);try{I[be]=ps}catch(fs){try{Er(ee)}finally{throw fs}}be++}}function S(A,O,I){var G=v(O,I,!1);if(P(G)||!G.delete(A))return!1;if(G.size===0){var K=f.get(O);P(K)||(K.delete(I),K.size===0&&f.delete(K))}return!0}}function us(p){var f=p.defineMetadata,m=p.hasOwnMetadata,v=p.getOwnMetadata,C=p.getOwnMetadataKeys,Z=p.deleteMetadata,ie=new E,q={isProviderFor:function(S,A){var O=ie.get(S);return!P(O)&&O.has(A)?!0:C(S,A).length?(P(O)&&(O=new g,ie.set(S,O)),O.add(A),!0):!1},OrdinaryDefineOwnMetadata:f,OrdinaryHasOwnMetadata:m,OrdinaryGetOwnMetadata:v,OrdinaryOwnMetadataKeys:C,OrdinaryDeleteMetadata:Z};return q}function st(p,f,m){var v=h.getProvider(p,f);if(!P(v))return v;if(m){if(h.setProvider(p,f,b))return b;throw new Error("Illegal state.")}}function ds(){var p={},f=[],m=(function(){function q(S,A,O){this._index=0,this._keys=S,this._values=A,this._selector=O}return q.prototype["@@iterator"]=function(){return this},q.prototype[s]=function(){return this},q.prototype.next=function(){var S=this._index;if(S>=0&&S<this._keys.length){var A=this._selector(this._keys[S],this._values[S]);return S+1>=this._keys.length?(this._index=-1,this._keys=f,this._values=f):this._index++,{value:A,done:!1}}return{value:void 0,done:!0}},q.prototype.throw=function(S){throw this._index>=0&&(this._index=-1,this._keys=f,this._values=f),S},q.prototype.return=function(S){return this._index>=0&&(this._index=-1,this._keys=f,this._values=f),{value:S,done:!0}},q})(),v=(function(){function q(){this._keys=[],this._values=[],this._cacheKey=p,this._cacheIndex=-2}return Object.defineProperty(q.prototype,"size",{get:function(){return this._keys.length},enumerable:!0,configurable:!0}),q.prototype.has=function(S){return this._find(S,!1)>=0},q.prototype.get=function(S){var A=this._find(S,!1);return A>=0?this._values[A]:void 0},q.prototype.set=function(S,A){var O=this._find(S,!0);return this._values[O]=A,this},q.prototype.delete=function(S){var A=this._find(S,!1);if(A>=0){for(var O=this._keys.length,I=A+1;I<O;I++)this._keys[I-1]=this._keys[I],this._values[I-1]=this._values[I];return this._keys.length--,this._values.length--,xn(S,this._cacheKey)&&(this._cacheKey=p,this._cacheIndex=-2),!0}return!1},q.prototype.clear=function(){this._keys.length=0,this._values.length=0,this._cacheKey=p,this._cacheIndex=-2},q.prototype.keys=function(){return new m(this._keys,this._values,C)},q.prototype.values=function(){return new m(this._keys,this._values,Z)},q.prototype.entries=function(){return new m(this._keys,this._values,ie)},q.prototype["@@iterator"]=function(){return this.entries()},q.prototype[s]=function(){return this.entries()},q.prototype._find=function(S,A){if(!xn(this._cacheKey,S)){this._cacheIndex=-1;for(var O=0;O<this._keys.length;O++)if(xn(this._keys[O],S)){this._cacheIndex=O;break}}return this._cacheIndex<0&&A&&(this._cacheIndex=this._keys.length,this._keys.push(S),this._values.push(void 0)),this._cacheIndex},q})();return v;function C(q,S){return q}function Z(q,S){return S}function ie(q,S){return[q,S]}}function ls(){var p=(function(){function f(){this._map=new y}return Object.defineProperty(f.prototype,"size",{get:function(){return this._map.size},enumerable:!0,configurable:!0}),f.prototype.has=function(m){return this._map.has(m)},f.prototype.add=function(m){return this._map.set(m,m),this},f.prototype.delete=function(m){return this._map.delete(m)},f.prototype.clear=function(){this._map.clear()},f.prototype.keys=function(){return this._map.keys()},f.prototype.values=function(){return this._map.keys()},f.prototype.entries=function(){return this._map.entries()},f.prototype["@@iterator"]=function(){return this.keys()},f.prototype[s]=function(){return this.keys()},f})();return p}function cs(){var p=16,f=l.create(),m=v();return(function(){function S(){this._key=v()}return S.prototype.has=function(A){var O=C(A,!1);return O!==void 0?l.has(O,this._key):!1},S.prototype.get=function(A){var O=C(A,!1);return O!==void 0?l.get(O,this._key):void 0},S.prototype.set=function(A,O){var I=C(A,!0);return I[this._key]=O,this},S.prototype.delete=function(A){var O=C(A,!1);return O!==void 0?delete O[this._key]:!1},S.prototype.clear=function(){this._key=v()},S})();function v(){var S;do S="@@WeakMap@@"+q();while(l.has(f,S));return f[S]=!0,S}function C(S,A){if(!r.call(S,m)){if(!A)return;Object.defineProperty(S,m,{value:l.create()})}return S[m]}function Z(S,A){for(var O=0;O<A;++O)S[O]=Math.random()*255|0;return S}function ie(S){if(typeof Uint8Array=="function"){var A=new Uint8Array(S);return typeof crypto<"u"?crypto.getRandomValues(A):typeof msCrypto<"u"?msCrypto.getRandomValues(A):Z(A,S),A}return Z(new Array(S),S)}function q(){var S=ie(p);S[6]=S[6]&79|64,S[8]=S[8]&191|128;for(var A="",O=0;O<p;++O){var I=S[O];(O===4||O===6||O===8)&&(A+="-"),I<16&&(A+="0"),A+=I.toString(16).toLowerCase()}return A}}function Sn(p){return p.__=void 0,delete p.__,p}})})(Ir||(Ir={}))});_();var Np=ze(Tr(),1),ke=ze(require("process"),1);_();var Bs=ze(require("os"),1),js=ze(require("path"),1),qs=require("url"),Hs=require("util");_();_();var pe=class extends Error{};function ks(t){if(t===null||typeof t!="object")return null;let e=t.shared;if(e==null)return null;if(typeof e!="object")throw new pe(`catalog "shared" block is ${typeof e}, expected an object`);let{shared_org_id:n,endpoints:r}=e;if(typeof n!="string"||n==="")throw new pe(`catalog "shared" block has no shared_org_id: ${JSON.stringify(e)}`);if(!Array.isArray(r))throw new pe(`catalog "shared" block endpoints is not an array: ${JSON.stringify(e)}`);let o=r.map((i,s)=>{if(i===null||typeof i!="object")throw new pe(`catalog shared endpoints[${s}] is not an object: ${JSON.stringify(i)}`);let a=i;for(let u of["model_id","variant","app_slug","function"])if(typeof a[u]!="string"||a[u].length===0)throw new pe(`catalog shared endpoints[${s}] (${JSON.stringify(a)}) has no valid "${u}"`);return a});return o.length===0?null:{shared_org_id:n,endpoints:o}}function It(t){return[...t.endpoints].sort((e,n)=>e.model_id.localeCompare(n.model_id)||e.variant.localeCompare(n.variant))}function Pr(t,e,n){let r=(e??"").trim();if(r==="")return null;let o=r,i=n,s=r.lastIndexOf(":");s>0&&(o=r.slice(0,s),i=r.slice(s+1));let a=t.endpoints.filter(d=>d.model_id===o);if(a.length===0)return null;if(i!==void 0){let d=a.find(l=>l.variant===i);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 pe(`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 Cr(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 pe(`catalog read failed: GET ${n} \u2192 ${r.status}`);return ks(await r.json())}_();function Ae(t){return t.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}_();var ye=class extends Error{name="ModelPriceError"},En="per_million_input_tokens",An="per_million_output_tokens",Mr="model_id,variant,unit,price_usd,effective_at,expires_at",$r={per_request:!0,per_minute:!0,per_output_unit:!0},xs=5e3;function Rs(t,e){let n=typeof t=="number"?t:typeof t=="string"&&t.trim()!==""?Number(t):NaN;if(!Number.isFinite(n)||n<0)throw new ye(`${e}: price_usd is not a non-negative number (${JSON.stringify(t)})`);return n}function Ur(t,e,n){let r=typeof t=="string"?Date.parse(t):NaN;if(!Number.isFinite(r))throw new ye(`${e}: ${n} is not a parseable timestamp (${JSON.stringify(t)})`);return r}function Ss(t,e,n){return t===null?null:Ur(t,e,n)}function Es(t,e){if(t===En)return"input";if(t===An)return"output";if(typeof t=="string"&&Object.hasOwn($r,t))return null;throw new ye(`${e}: unit ${JSON.stringify(t)} is neither a per-token rate (${En} | ${An}) nor a known GPU-time unit (${Object.keys($r).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 As(t,e){let n=t.reduce((o,i)=>i.effective_at>o.effective_at?i: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 Os(t){if(!Array.isArray(t))throw new ye(`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 ye(`${o} is not an object: ${JSON.stringify(r)}`);let i=r;for(let d of Mr.split(","))if(!Object.hasOwn(i,d))throw new ye(`${o} has no "${d}" column: ${JSON.stringify(i)}`);let s=Es(i.unit,o);if(s===null)continue;let a=i.model_id;if(typeof a!="string"||a==="")throw new ye(`${o} has no model_id (${JSON.stringify(i)})`);let u=i.variant;if(!(u===null||typeof u=="string"&&u!==""))throw new ye(`${o} has a non-string variant (${JSON.stringify(u)})`);e.push({model_id:a,variant:u,side:s,usd:Rs(i.price_usd,o),effective_at:Ur(i.effective_at,o,"effective_at"),expires_at:Ss(i.expires_at,o,"expires_at")})}return e}function Lr(t,e=Date.now()){let n=new Map,r=new Map;for(let a of t){if(a.effective_at>e||a.expires_at!==null&&a.expires_at<=e)continue;let u=n.get(a.model_id);u||(u=new Map,n.set(a.model_id,u),r.set(a.model_id,new Map));let d=a.variant??"",l=u.get(d);l||(l={input:[],output:[]},u.set(d,l),r.get(a.model_id).set(d,a.variant)),l[a.side].push({effective_at:a.effective_at,usd:a.usd})}let o=[],i=[];for(let[a,u]of n){let d=u.get(""),l=(c,y)=>{let g=c&&c.length>0?c:d?.[y];if(!g||g.length===0)return null;let E=As(g,y);return"reason"in E?E:E.rate.usd===0?{reason:`the ${y} 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`}:E.rate};for(let[c,y]of r.get(a)){let g=u.get(c),E=l(g?.input,"input"),w=l(g?.output,"output");if(E!==null&&"reason"in E){i.push({model_id:a,variant:y,reason:E.reason});continue}if(w!==null&&"reason"in w){i.push({model_id:a,variant:y,reason:w.reason});continue}E===null||w===null||o.push({model_id:a,variant:y,pricing:{input:E.usd,output:w.usd}})}}let s=(a,u)=>a.model_id.localeCompare(u.model_id)||(a.variant===null?1:0)-(u.variant===null?1:0)||(a.variant??"").localeCompare(u.variant??"");return{prices:o.sort(s),refusals:i.sort(s)}}function at(t,e,n){if(n.refusals.some(i=>i.model_id===t&&i.variant===e))return null;let r=n.prices.find(i=>i.model_id===t&&i.variant===e);if(r)return r.pricing;if(n.refusals.some(i=>i.model_id===t&&i.variant===null))return null;let o=n.prices.find(i=>i.model_id===t&&i.variant===null);return o?o.pricing:null}var Tt={prices:[],refusals:[]};async function Nr(t){let e=t.fetchImpl??fetch,n=`${En},${An}`,r=await e(`${t.catalogUrl}/model_prices?select=${Mr}&unit=in.(${n})`,{headers:{apikey:t.anonKey,Authorization:`Bearer ${t.anonKey}`},signal:AbortSignal.timeout(xs)});if(!r.ok)throw new Error(`model_prices fetch failed: ${r.status}`);return Os(await r.json())}_();var Is={chat:!0,code:!0,agent:!0,vl:!0},Dr={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function Ts(t){let e=t.toLowerCase();return Object.hasOwn(Dr,e)?Dr[e]:null}var Ps=["context_length","max_model_len"];function Cs(t){let e=Ps.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 Pt(t){let e=t.map(Cs),n=e[0];return n==null?null:e.every(r=>r===n)?n:null}function $s(t,e){let n=e.filter(o=>Ae(`${o.model_id}-${o.variant}`)===t);if(n.length>0)return On(n);let r=e.filter(o=>Ae(o.model_id)===t);return r.length>0?On(r):null}function On(t){let e=t[0];return t.every(r=>r.model_id===e.model_id&&r.variant===e.variant)?{row:t.length===1?e:Ms(t),matches:t}:null}function Ms(t){let e=t[0],n=r=>t.every(o=>(o[r]??void 0)===(e[r]??void 0))?e[r]:void 0;return{model_id:e.model_id,variant:e.variant,gpu_spec:n("gpu_spec"),engine:n("engine"),task:n("task"),description:n("description"),size_gb:n("size_gb"),hf_repo:n("hf_repo"),updated_at:n("updated_at")}}function jr(t){let e=t.updated_at?Date.parse(t.updated_at):NaN;return Number.isFinite(e)?Math.floor(e/1e3):0}function In(t,e,n){let r=$s(t,e);if(!r)return{id:t,object:"model",created:0,owned_by:"urun"};let{row:o,matches:i}=r,s=Pt(i),a=at(o.model_id,o.variant,n);return{id:t,object:"model",created:jr(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}:{},...s!==null?{context_length:s}:{},...a!==null?{pricing:a}:{}}}function Fr(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,i=n+r,s=n.length-6+(o===""?0:Number(o));return(s<=0?`0.${"0".repeat(-s)}${i}`:`${i.slice(0,s)}.${i.slice(s).padEnd(1,"0")}`).replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,"")}function Br(t){return{input:[{type:"prompt",unit:"token",cost_usd:Fr(t.input)}],output:[{type:"completion",unit:"token",cost_usd:Fr(t.output)}]}}function Us(t,e){if(t.length===0)return null;let n=On(t);if(!n)return null;let{row:r,matches:o}=n;if(!r.task||Is[r.task]!==!0)return null;let i=Pt(o),s=at(r.model_id,r.variant,e),a={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};return i!==null&&(a.max_tokens={type:"integer",min:1,max:i,unit:"token"}),{schema_version:"2.4",id:`${r.model_id}:${r.variant}`,name:`${r.model_id}:${r.variant}`,created:jr(r),quantization:Ts(r.variant),description:r.description??"",hugging_face_id:r.hf_repo?r.hf_repo.split("@")[0]??r.hf_repo:"",input_modalities:[{type:"text",...i!==null?{supported_inputs:{max_context_length:{value:i,unit:"token"}}}:{},...s?{pricing:Br(s).input}:{}},...r.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:a,...s?{pricing:Br(s).output}:{}}]}}function qr(t,e,n){let r=[],o=new Set;for(let i of t){let s=`${i.model_id}:${i.variant}`;if(o.has(s))continue;o.add(s);let a=Us(e.filter(u=>u.model_id===i.model_id&&u.variant===i.variant),n);a&&r.push(a)}return{data:r}}var re=class extends Error{},le=class extends Error{},Pn="urs1.";function Ls(t,e){return Pn+Buffer.from(JSON.stringify([t,e]),"utf8").toString("base64url")}function Ns(t){if(!t.startsWith(Pn))return null;try{let e=JSON.parse(Buffer.from(t.slice(Pn.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 zr(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 Hr(t){if(t.length===0)return[];if(!t.every(n=>(n.task??void 0)==="image"))return[];let e=Fs(t);return e===null?[]:[e]}function Gr(t,e,n,r,o){if(!o)throw new pe(`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 pe(`shared model "${t}" resolved without a catalog id + variant to join on`);let i=r.filter(s=>s.model_id===e&&s.variant===n);if(i.length===0)throw new pe(`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 i}function Vr(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 pe(`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 Yr="shared:";function ut(t){return t.sharedApp?Yr+t.appSlug:t.appSlug}function Wr(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 Tn(t,e){let n=e.filter(i=>Ae(`${i.model_id}-${i.variant}`)===t);if(n.length>0)return n;let r=e.filter(i=>Ae(i.model_id)===t);return new Set(r.map(i=>i.variant)).size===1?r:[]}function Ds(t){let e=t.engine_args?.expects_image;return e==null?"edit":e===!1?"generation":e===!0?"edit":null}function Fs(t){let e=t.map(Ds),n=e[0];return n!=null&&e.every(r=>r===n)?n:null}var Jr={stt:"stt",tts:"tts"};function Kr(t){if(t.length===0)return null;let e=t[0].task??void 0;return e===void 0||Jr[e]===void 0?null:t.every(n=>(n.task??void 0)===e)?Jr[e]:null}var Ct=6e4,$t=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??Ct;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??Ct;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??Ct;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??Ct;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(){let e;try{e=await this.priceRows()}catch(r){if(r instanceof ye)throw r;return this.warn(`urun /v1/models: the per-token price surface could not be read (${String(r instanceof Error?r.message:r)}) \u2014 every model is listed UNPRICED for now; Hugging Face will show no price until the read succeeds
3
+ `),Tt}let n=Lr(e,Date.now());for(let r of n.refusals)this.warn(`urun /v1/models: refusing to price ${r.model_id}:${r.variant??"*"} \u2014 ${r.reason}
4
+ `);return n}warn(e){(this.opts.warn??(r=>console.warn(r.trimEnd())))(e)}servable(e){return e.filter(n=>n.function_name===this.opts.fnName&&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(Yr)||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 re(e===""?`no "model" was given and this endpoint has no default model \u2014 name one of your org's deployed "${this.opts.fnName}" apps; available models: ${r}`:`model "${e}" does not name a deployed "${this.opts.fnName}" app in your org, 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=Ae(n);if(o===r)return{appSlug:r};if(this.pool.has(o))return{appSlug:o};if(this.opts.listApps){let i=await this.deployedApps(),s=this.servable(i);if(s.includes(o))return{appSlug:o};let a=s.filter(d=>d.startsWith(`${o}-`));if(a.length===1)return{appSlug:a[0]};if(a.length>1)throw new re(`model "${n}" is ambiguous across deployed apps ${a.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let u=i.find(d=>d.app_slug===o||d.app_slug.startsWith(`${o}-`));if(u)throw new re(`model "${n}" maps to app "${u.app_slug}", which is not an active "${this.opts.fnName}" app \u2014 deploy it with \`urun serve ${n}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.shared){let i=await this.sharedBlock();if(i){let s=Pr(i,n);if(s)return{appSlug:s.endpoint.app_slug,fnName:s.endpoint.function,sharedApp:!0,modelId:s.endpoint.model_id,variant:s.endpoint.variant}}}if(this.opts.listCatalog){let s=(await this.opts.listCatalog()).find(a=>n===a.model_id||n===`${a.model_id}:${a.variant}`||o===Ae(a.model_id)||o===Ae(`${a.model_id}-${a.variant}`));if(s)throw new re(`model "${n}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${s.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return r===null?this.noDefaultApp(n):{appSlug:r}}async catalogIdentityFor(e){let n=Wr(await this.resolveTarget(e));return n.kind==="catalog"?{model_id:n.model_id,variant:n.variant}:null}async servingAppSlugFor(e){return(await this.resolveTarget(e)).appSlug}async resolveApp(e){return ut(await this.resolveTarget(e))}async sessionFor(e){let n=await this.resolveTarget(e),r=ut(n),o=this.pool.get(r);return o||(o=Promise.resolve(this.opts.openSession(n)),this.pool.set(r,o),o.catch(()=>this.pool.delete(r))),{app:r,entry:await o,identity:Wr(n)}}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:Ls(n,this.keyOf(r))}}async sessionForHandle(e){let n=Ns(e);if(!n)throw new le(`malformed session handle ${JSON.stringify(e)} \u2014 not issued by this proxy`);let{app:r,key:o}=n,i=this.pool.get(r),s=i?await i.then(a=>a,()=>null):null;if(s===null)throw new le(`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(s)!==o)throw new le(`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:s}}async evict(e,n){let r=this.pool.get(e);!r||await r.then(i=>i,()=>null)!==n||this.pool.get(e)!==r||(this.pool.delete(e),await this.opts.closeSession(n).catch(()=>{}))}async modelList(){let e=await this.availableIds(),[n,r]=await Promise.all([this.catalogRows().then(d=>({ok:!0,rows:d}),()=>({ok:!1,rows:[]})),this.pricesForListing()]),o=n.rows,i=n.ok?r:Tt;n.ok||this.warn(`urun /v1/models: the model catalog could not be read \u2014 entries fall back to the bare OpenAI fields AND no model is priced on either lane (pricing follows catalog identity, so a half-priced listing would flicker on Hugging Face)
5
+ `);let s=await this.sharedBlock(),u=(s?It(s):[]).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 c=o.filter(E=>E.model_id===d.model_id&&E.variant===d.variant),y=Pt(c);y!==null&&(l.context_length=y);let g=at(d.model_id,d.variant,i);return g!==null&&(l.pricing=g),l});return{object:"list",data:[...e.map(d=>In(d,o,i)),...u]}}async openRouterModels(){let[e,n]=await Promise.all([this.catalogRows(),this.pricesForListing()]),r=await this.sharedBlock();return qr(r?It(r):[],e,n)}async supportsImages(e,n){let r=await this.resolveTarget(e),o=await this.catalogRows();if(r.sharedApp){let i=Gr(String(e),r.modelId,r.variant,o,this.opts.listCatalog!==null);return Vr(`${r.modelId}:${r.variant}`,i).includes(n)}return Hr(Tn(r.appSlug,o)).includes(n)}async supportsAudio(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Kr(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))!==null:Kr(Tn(n.appSlug,r))!==null}async imageModelList(){let e=await this.catalogRows(),n=Tt,r=[];for(let i of await this.availableIds()){let s=Hr(Tn(i,e));s.length!==0&&r.push({...In(i,e,n),image_modes:s})}let o=await this.sharedBlock();if(o)for(let i of It(o)){let s=Gr(`${i.model_id}:${i.variant}`,i.model_id,i.variant,e,this.opts.listCatalog!==null),a=Vr(`${i.model_id}:${i.variant}`,s);if(a.length===0)continue;let u={id:`${i.model_id}:${i.variant}`,object:"model",created:0,owned_by:"urun",name:`${i.model_id}:${i.variant}`,image_modes:a};i.gpu_spec&&(u.gpu_spec=i.gpu_spec),typeof i.warm=="number"&&(u.warm=i.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 Xr(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 Mt(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 Gs=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function Qr(t){let e=t?.code;return typeof e=="string"&&Gs.has(e)}function Le(t,e){try{return t.write(e),!0}catch(n){if(Qr(n))return!1;throw n}}function Zr(t){let e={stdout:!1,stderr:!1},n=[];for(let r of["stdout","stderr"]){let o=t.streams[r],i=s=>{Qr(s)&&(e[r]=!0);let a=s?.code;try{t.note(`${new Date().toISOString()} urun compat: process.${r} died (${String(a??(s instanceof Error?s.message:s))}) \u2014 the terminal is gone; launcher output continues in this log only
6
+ `)}catch{}};o.on("error",i),n.push([o,i])}return{isDead:r=>e[r],uninstall:()=>{for(let[r,o]of n)r.off("error",o)}}}_();var se=require("@prometheus-io/client"),bo=require("http");_();var Vs=new Map([["/v1/chat/completions","conversational"],["/v1/responses","conversational"],["/v1/messages","conversational"],["/v1/images/generations","text-to-image"],["/v1/images/edits","image-to-image"]]);function Ws(t){return Vs.get(t)??null}var Js=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function Ks(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function zs(t){return t==="surface"}function Ys(t){let e=t?.ledgerFailureReason;return Js.includes(e)?e:"unknown"}function Xs(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function Qs(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 Zs=256,eo=4096,dt=12e4,ea=250,ta=8e3,Oe=[],Cn=new Set,lt=0,Ye=!1;function to(){return Oe.length}function no(t=Date.now()){let e=Oe[0];return e===void 0?0:Math.max(0,t-e.queuedAt)}var Ut=new Set;function ro(){return lt===0&&Oe.length===0}function oo(){if(ro()){Ye=!1;for(let t of[...Ut])t()}}async function io(t=ao){return ro()?!0:await new Promise(e=>{let n=null,r=()=>{Ut.delete(r),n&&clearTimeout(n),e(!0)};Ut.add(r),n=setTimeout(()=>{Ut.delete(r),e(!1)},t),n.unref?.()})}function so(){Ye=!0;let t=[...Oe.splice(0,Oe.length),...Cn];for(let e of t)e.settled||Be(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 ${ao}ms drain \u2014 the request was served and answered, and its billing row was never written`});return oo(),t.length}var ao=5e3;function uo(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
7
+ `}function na(t){Le(process.stdout,t)}function lo(t,e){try{t(e)}catch{}}function co(t){let{record:e,ledger:n}=t;if(n===null)return;let r=n.report??na,o=Ws(e.path);if(o===null)return;let i=l=>{try{n.observe(l)}catch{}},s=(l,c)=>{i({outcome:"skipped",reason:l}),lo(r,uo("inference_ledger_skipped",{inference_id:e.inference_id,path:e.path,reason:l,detail:c}))};if(e.status===null){s("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){s("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"){s("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){s("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:Qs(e),completed_at:e.ts,billable_outcome:Xs(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};if(Oe.length>=eo){s("queue_full",`${Oe.length} ledger writes are already waiting (capacity ${eo}, ${lt} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}Oe.push({entry:d,req:t.req,record:e,ledger:n,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),lt<Zs&&(lt+=1,ra())}async function ra(){try{for(;;){let t=oa();if(t===void 0)return;Cn.add(t);try{await ia(t)}finally{Cn.delete(t)}}}finally{lt-=1,oo()}}function oa(){let t=Date.now();for(;;){let e=Oe.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<dt)return e;Be(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 ${dt}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 ia(t){let{entry:e,record:n,ledger:r}=t;for(;;){t.attempts+=1;try{let o=await r.write(t.req,e);Be(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 i=Ks(o);if(i!==null){Be(t,{outcome:"already_written",reason:i.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:i.cost_nano_usd,price_version:i.price_version,attempts:t.attempts});return}let s=Ys(o),a=Date.now()-t.queuedAt;if(Ye){Be(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(zs(s)&&a<dt){if(await aa(sa(t.attempts)),Ye||Date.now()-t.queuedAt>=dt){let u=Date.now()-t.queuedAt;Be(t,{outcome:"failed",reason:Ye?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:Ye?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:u,error:`this row's backoff outlived the ${dt}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}Be(t,{outcome:"failed",reason:s},{event:"inference_ledger_write_failed",reason:s,attempts:t.attempts,waited_ms:a,error:o instanceof Error?o.message:String(o)});return}}}function sa(t){return Math.min(ta,ea*2**Math.max(0,t-1))}function aa(t){return new Promise(e=>{setTimeout(e,t).unref?.()})}function Be(t,e,n){if(t.settled)return;t.settled=!0;try{t.ledger.observe(e)}catch{}let{event:r,...o}=n;lo(t.report,uo(r,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...o}))}_();_();var Nt="/v1/usage/requests",po=1e4,ua=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,Ne=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},Q=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}},Lt=class extends Q{constructor(e){super(e),this.name="UsageWithholdingError"}},ho=1024*1024;function Un(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new Ne(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new Ne(`"${e}" must be an array of uuid strings`);if(n.length>po)throw new Ne(`"${e}" carries ${n.length} ids; at most ${po} 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 i of n){if(typeof i!="string"||!ua.test(i))throw new Ne(`"${e}" contains ${JSON.stringify(i)}, 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(i)||(o.add(i),r.push(i))}return r}function yo(t){return Un(t,"inference_ids")}var da=["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 Xe(t,e,n){let r=t[e];if(typeof r!="string"||r==="")throw new Q(`${n}: ${e} is not a non-empty string (got ${Qe(r)})`);return r}function $n(t,e,n){let r=t[e];if(r===null)return null;if(typeof r!="string"||r==="")throw new Q(`${n}: ${e} is neither null nor a non-empty string (got ${Qe(r)})`);return r}function ct(t,e,n,r){let o=t[e];if(o===null)return null;let i=typeof o=="number"?o:typeof o=="string"&&/^-?\d+(\.\d+)?$/.test(o.trim())?Number(o.trim()):NaN;if(!Number.isFinite(i))throw new Q(`${n}: ${e} is not a number (got ${Qe(o)})`);if(r&&!Number.isSafeInteger(i))throw new Q(`${n}: ${e} is not a safely representable integer \u2014 a billing quantity that silently loses digits must fail rather than be reported`);if(i<0)throw new Q(`${n}: ${e} is negative`);return Object.is(i,-0)?0:i}var la=40,fo=64,mo=600;function _o(t){let e=Object.keys(t).sort(),r=e.slice(0,la).map(o=>o.length>fo?`${o.slice(0,fo)}\u2026`:o).join(", ");return r.length>mo&&(r=`${r.slice(0,mo)}\u2026`),`${e.length} keys present: [${r}]`}function Qe(t){return t===null?"null":t===void 0?"undefined":Array.isArray(t)?"an array":typeof t=="object"?"an object":`a ${typeof t}`}function Mn(t){Le(process.stdout,`${JSON.stringify({event:"usage_row_rejected",ts:new Date().toISOString(),where:t.where,reason:t.reason})}
8
+ `)}function ca(t,e){let n;try{n=t(e)}catch{go(t,e);return}pa(n)&&Promise.resolve(n).catch(()=>{go(t,e)})}function pa(t){return typeof t?.then=="function"}function go(t,e){if(t!==Mn)try{Mn(e)}catch{}}function wo(t,e=Mn){let n=t?.requests;if(!Array.isArray(n)){let o=t!==null&&typeof t=="object"&&!Array.isArray(t)?_o(t):Qe(t);throw new Q(`the usage surface answered without a "requests" array: got ${Qe(n)} (${o})`)}let r=[];for(let o=0;o<n.length;o++){let i=`usage.requests[${o}]`;try{r.push(fa(n[o],i))}catch(s){if(s instanceof Lt||!(s instanceof Q))throw s;ca(e,{where:i,reason:s.message})}}if(n.length>0&&r.length===0)throw new Q(`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 fa(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new Q(`${e} is not an object: got ${Qe(t)}`);let n=t;if(Object.hasOwn(n,"gpu_seconds"))throw new Lt(`${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 da)if(!Object.hasOwn(n,o))throw new Q(`${e} has no "${o}" column (${_o(n)})`);let r=ct(n,"http_status",e,!0);if(r===null)throw new Q(`${e}: http_status is null, but the ledger column is NOT NULL`);return{inference_id:Xe(n,"inference_id",e),model_id:Xe(n,"model_id",e),identity_kind:Xe(n,"identity_kind",e),variant:$n(n,"variant",e),task:Xe(n,"task",e),http_status:r,started_at:Xe(n,"started_at",e),completed_at:Xe(n,"completed_at",e),prompt_tokens:ct(n,"prompt_tokens",e,!0),completion_tokens:ct(n,"completion_tokens",e,!0),output_units:ct(n,"output_units",e,!1),cost_nano_usd:ct(n,"cost_nano_usd",e,!0),price_version:$n(n,"price_version",e),priced_at:$n(n,"priced_at",e)}}function Dt(t,e){let n=new Set(e),r=new Set;for(let o=0;o<t.length;o++){let i=t[o];if(!n.has(i.inference_id))throw new Q(`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(i.inference_id))throw new Q(`the usage surface returned ${i.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(i.inference_id)}}var pt="/partners/huggingface/v1/billing/requests";function ma(t){return Un(t,"requestIds")}function ga(t){return t>=200&&t<=399}function ha(t){let e=[];for(let n of t)n.cost_nano_usd!==null&&ga(n.http_status)&&e.push({requestId:n.inference_id,costNanoUsd:n.cost_nano_usd});return{requests:e.length>0?e:null}}async function vo(t,e){let n=ma(t),r=await e(n);return Dt(r,n),ha(r)}var Ln=9464,Ft="/metrics",Bt="(other)",ya="(none)",_a=64,wa=128,va=["/v1/responses","/v1/chat/completions","/v1/messages","/v1/messages/count_tokens","/v1/models","/v1/images/models","/v1/images/generations","/v1/images/edits",Nt,pt],ba=new Set(va);function ka(t){return ba.has(t)?t:Bt}function xa(t){return t===null?"none":t>=500?"5xx":t>=400?"4xx":t>=300?"3xx":t>=200?"2xx":"1xx"}var jt=class{constructor(e){this.max=e}max;admitted=new Set;overflows=0;of(e,n=!0){return e===null||e===""?ya:this.admitted.has(e)?e:n?this.admitted.size>=this.max?(this.overflows+=1,Bt):(this.admitted.add(e),e):Bt}get size(){return this.admitted.size}get overflowCount(){return this.overflows}},qt=class{registry=new se.Registry;modelLabel=new jt(_a);orgLabel=new jt(wa);requests;duration;ttft;streamErrors;undelivered;ledgerWrites;ledgerQueue;labelCardinality;labelOverflows;constructor(e={}){this.requests=new se.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 se.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 se.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 se.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 se.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 se.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]}),this.ledgerQueue=new se.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"},to()),this.ledgerQueue.set({measure:"oldest_age_seconds"},no()/1e3)}}),this.labelCardinality=new se.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 se.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,se.collectDefaultMetrics)({register:this.registry,prefix:"urun_inference_proxy_"})}observeLedger(e){this.ledgerWrites.inc({outcome:e.outcome,reason:e.reason})}observe(e){let n=ka(e.path),r=e.status!==null&&!(e.status>=400&&e.status<500),o=this.modelLabel.overflowCount+this.orgLabel.overflowCount,i=this.modelLabel.of(e.model,r),s=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===""?Bt:e.method,status_class:xa(e.status),stream:u,model:i,org:s}),this.duration.observe({lane:n,stream:u,model:i,org:s},e.duration_ms/1e3),e.ttft_ms!==null&&this.ttft.observe({lane:n,model:i,org:s},e.ttft_ms/1e3),e.stream_error&&this.streamErrors.inc({lane:n,model:i}),e.delivered||this.undelivered.inc({lane:n,model:i})}scrape(){return this.registry.metrics()}get contentType(){return this.registry.contentType}};function ko(t){return(0,bo.createServer)((e,n)=>{(async()=>{let r=(e.url??"/").split("?")[0];if(e.method!=="GET"||r!==Ft){n.writeHead(404,{"content-type":"text/plain"}),n.end(`no route for ${e.method} ${r} \u2014 this port serves only GET ${Ft}
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()})})}_();var Nn=require("crypto"),Ro=require("http"),Dn=9465,Ra="x-urun-key-id",Sa="x-urun-key-fingerprint",Ea="urun-ratelimit-bucket:",Aa="urun-ratelimit-tier:",xo="anonymous",Oa=/^urun_sk_[A-Za-z0-9_-]{43}$/;function Ia(t){return(0,Nn.createHash)("sha256").update(`${Ea}${t}`).digest("hex")}function Ta(t){return(0,Nn.createHash)("sha256").update(`${Aa}${t}`).digest("hex")}function Pa(t){let e=/^Bearer[ \t]+(\S.*)$/i.exec((t??"").trim());if(!e)return null;let n=e[1].trim();return Oa.test(n)?n:null}function So(){return(0,Ro.createServer)((t,e)=>{let n=Pa(t.headers.authorization);e.setHeader(Ra,n===null?xo:Ia(n)),e.setHeader(Sa,n===null?xo:Ta(n)),e.writeHead(200),e.end()})}_();var Ca=8080,Fn="serve",je="0.0.0.0";function $a(t,e,n){let r=(t[e]??"").trim();if(!r)throw new Error(`${e} is not set \u2014 ${n}`);return r}function Eo(t=process.env){let e=(t.PORT??"").trim(),n=e===""?Ca: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=Xr(Mt($a(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(),i=o?Mt(o,"URUN_API_URL"):`${r.replace(/\/+$/,"")}/v1`,s=(t.URUN_CATALOG_URL??"").trim(),a=(t.URUN_CATALOG_ANON_KEY??"").trim();if(!!s!=!!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=s?{catalogUrl:Mt(s,"URUN_CATALOG_URL"),anonKey:a}:null;return{port:n,baseUrl:r,apiUrl:i,catalog:u}}_();var es=require("http");_();_();var Io="audio",Bn=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 i=0;i<r;i++)o[i]=n.readInt16LE(i*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 To(t,e,n=1e4){let r=new Bn(e,{sampleRate:24e3}),o=await r.startOutbound();await t.stream(Io).attach(o);let i=await Ma(t,n);return await r.startInbound(i),r}function Ma(t,e){let n=t.stream(Io);return n.track!=null?Promise.resolve(n.track):new Promise((r,o)=>{let i,s=setTimeout(()=>{i?.(),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(s),i?.(),r(u))};i=n.on?.("track",a),n.track!=null&&a(n.track)})}var Ao="rt-video-in",Oo=256e3;function Po(t){let e=t.stream(Ao),n=e.emit;if(typeof n!="function")throw new Error(`enableSessionVideo: session.stream('${Ao}') 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>Oo)throw new Error(`enableSessionVideo: frame is ${r.byteLength} bytes \u2014 over the ${Oo}-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 Ua=24e3,ft=480,La=111;async function Na(){let t=await import("opusscript"),e=t.default??t;return new e(Ua,1,2048)}async function Co(){let t=await import("werift"),e=await Na();return{createSource:()=>{let n=new t.MediaStreamTrack({kind:"audio"}),r=Math.random()*4294967295>>>0,o=Math.random()*65535&65535,i=Math.random()*4294967295>>>0,s=new Int16Array(0);return{track:n,onData:a=>{let u=new Int16Array(s.length+a.length);u.set(s,0),u.set(a,s.length);let d=0;for(;u.length-d>=ft;){let l=u.subarray(d,d+ft);d+=ft;let c=Buffer.from(l.buffer,l.byteOffset,l.byteLength),y=e.encode(c,ft);o=o+1&65535,i=i+ft>>>0;let g=new t.RtpHeader({version:2,payloadType:La,sequenceNumber:o,timestamp:i,ssrc:r,marker:!1}),E=new t.RtpPacket(g,y);n.writeRtp(E)}s=u.slice(d)}}},createSink:n=>{let r={onframe:null};return n.onReceiveRtp.subscribe(i=>{let s;try{s=e.decode(i.payload)}catch{return}let a=new Int16Array(s.length/2);for(let u=0;u<a.length;u++)a[u]=s.readInt16LE(u*2);r.onframe?.({samples:a})}),r}}}_();var Ht=require("@urun-sh/core"),jn="rt-video-out";var Da="h264";function Fa(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 Ba(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(s){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(s)})`)}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;Fa(o,"decodeVideoOutRecord");let i=t.subarray(2+e);if(i.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:o,au:i.slice()}}var $o="urun.videoOut.v2-ws";function Mo(t){let e=t.stream(jn);if(typeof e.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${jn}') 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,i=u=>{for(let d of r)d(u)},s=!1,a=async()=>{for await(let u of e.messages()){if(!(u instanceof Uint8Array)){i(new Error(`openSessionVideoOutLane: "${jn}" delivered a non-BINARY \xA75 payload (${typeof u}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let d;try{d=Ba(u)}catch(l){i(l instanceof Error?l:new Error(String(l)));continue}for(let l of n)l(d)}for(let u of o)u()};return{codec:Da,onOutputFrame:u=>(n.add(u),s||(s=!0,a().catch(d=>i(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 Wt="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",qa=1007,Lo=1008,Ie=1011,Jt=1008,x=class extends Error{constructor(n,r=qa){super(n);this.closeCode=r}closeCode},te=t=>typeof t=="object"&&t!==null&&!Array.isArray(t)?t:null,Gt=["setup","clientContent","realtimeInput","toolResponse"];function No(t){let e;try{e=JSON.parse(t)}catch{throw new x("client message is not valid JSON")}let n=te(e);if(!n)throw new x("client message must be a JSON object");let r=Gt.filter(a=>n[a]!==void 0),o=Object.keys(n).filter(a=>!Gt.includes(a));if(o.length>0)throw new x(`unknown client message field(s) ${JSON.stringify(o)} \u2014 expected exactly one of ${Gt.join(", ")}`);if(r.length!==1)throw new x(`client message must contain exactly one of ${Gt.join(", ")} (got ${r.length})`);let i=r[0],s=te(n[i]);if(!s)throw new x(`"${i}" must be a JSON object`);return{kind:i,[i]:s}}function Do(t,e){if(typeof t=="string")return t;let n=te(t),r=n?n.parts:void 0;if(!Array.isArray(r))throw new x(`${e}: expected a Content with a parts array`);let o=[];for(let i of r){let s=te(i);if(s&&typeof s.text=="string"){o.push(s.text);continue}throw s&&(s.inlineData!==void 0||s.fileData!==void 0)?new x(`${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`):s&&(s.functionCall!==void 0||s.functionResponse!==void 0)?new x(`${e}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new x(`${e}: unsupported part ${JSON.stringify(i)}`)}return o.join(`
11
+ `)}var Ha={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},qe="urun.videoOut";function Fo(t){return{framing:$o,codec:t}}function Bo(t){let{seq:e,ptsUs:n,codec:r,key:o}=t.header;return{[qe]:{v:1,seq:e,ptsUs:n,codec:r,key:o,data:Buffer.from(t.au).toString("base64")}}}var Ga={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},Va=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",qe],Uo=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function Wa(t){let e=te(t);if(!e)throw new x("setup.realtimeInputConfig must be an object");for(let o of Object.keys(e))if(!(o==="automaticActivityDetection"||o==="activityHandling"))throw new x(o==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${o}"`);let n=te(e.automaticActivityDetection);if(e.automaticActivityDetection!==void 0&&!n)throw new x("realtimeInputConfig.automaticActivityDetection must be an object");for(let o of Object.keys(n??{}))if(o!=="disabled")throw new x(`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 x("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(!Uo.includes(e.activityHandling))throw new x(`unsupported activityHandling ${JSON.stringify(e.activityHandling)} \u2014 expected one of ${Uo.join(", ")}`);r=e.activityHandling}return{manualActivity:!0,activityHandling:r}}var Ja=["temperature","maxOutputTokens","responseModalities"];function jo(t){for(let[c,y]of Object.entries(Ga))if(t[c]!==void 0)throw new x(y);for(let c of Object.keys(t))if(!Va.includes(c))throw new x(`unsupported setup field "${c}"`);if(typeof t.model!="string"||t.model.length===0)throw new x('setup.model is required (format "models/{model}")');let e=t.model.replace(/^models\//,""),n,r,o=!1,i=te(t.generationConfig);if(t.generationConfig!==void 0&&!i)throw new x("setup.generationConfig must be an object");if(i){for(let c of Object.keys(i))if(!Ja.includes(c))throw new x(`unsupported generationConfig field "${c}"`);if(i.temperature!==void 0){if(typeof i.temperature!="number")throw new x("generationConfig.temperature must be a number");n=i.temperature}if(i.maxOutputTokens!==void 0){if(typeof i.maxOutputTokens!="number")throw new x("generationConfig.maxOutputTokens must be a number");r=i.maxOutputTokens}if(i.responseModalities!==void 0){let c=Array.isArray(i.responseModalities)?i.responseModalities:[i.responseModalities];for(let y of c){let g=String(y).toUpperCase();if(g==="AUDIO"){o=!0;continue}if(g!=="TEXT")throw new x(`responseModalities ${JSON.stringify(y)} is not supported \u2014 this surface speaks TEXT and AUDIO (audio-out rides the native AudioBridge 'audio' lane)`)}}}let s=[];t.systemInstruction!==void 0&&s.push({role:"system",content:Do(t.systemInstruction,"setup.systemInstruction")});let a;if(t.tools!==void 0){if(!Array.isArray(t.tools))throw new x("setup.tools must be an array");a=[];for(let c of t.tools){let y=te(c);if(!y)throw new x("setup.tools entries must be objects");for(let g of Object.keys(y))if(g!=="functionDeclarations")throw new x(`unsupported tool "${g}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(y.functionDeclarations))throw new x("tool.functionDeclarations must be an array");for(let g of y.functionDeclarations){let E=te(g);if(!E||typeof E.name!="string")throw new x("functionDeclarations entries require a string name");a.push({type:"function",name:E.name,description:E.description,parameters:E.parameters})}}a.length===0&&(a=void 0)}let u={enabled:!1,handle:null};if(t.sessionResumption!==void 0){let c=te(t.sessionResumption);if(!c)throw new x("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let y of Object.keys(c))if(y!=="handle")throw new x(`unsupported sessionResumption field "${y}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(c.handle!==void 0&&(typeof c.handle!="string"||c.handle.length===0))throw new x("sessionResumption.handle must be a non-empty string when present");u={enabled:!0,handle:typeof c.handle=="string"?c.handle:null}}let d=t.realtimeInputConfig===void 0?Ha:Wa(t.realtimeInputConfig),l=!1;if(t[qe]!==void 0){if(!te(t[qe]))throw new x(`setup["${qe}"] must be an object (the v1 opt-in is {})`);l=!0}return{model:e,systemItems:s,tools:a,temperature:n,maxOutputTokens:r,audioModality:o,activity:d,sessionResumption:u,videoOut:l}}function qo(t){return{sessionResumptionUpdate:{newHandle:t,resumable:!0}}}function Ho(t){let e=Math.max(0,t??0)/1e3;return{goAway:{timeLeft:`${Math.round(e*1e3)/1e3}s`}}}function Go(t){for(let r of Object.keys(t))if(r!=="turns"&&r!=="turnComplete")throw new x(`unsupported clientContent field "${r}"`);let e=t.turns===void 0?[]:t.turns;if(!Array.isArray(e))throw new x("clientContent.turns must be an array");let n=[];for(let r of e){let o=te(r);if(!o)throw new x("clientContent.turns entries must be Content objects");let i=o.role===void 0?"user":String(o.role).toLowerCase();if(i!=="user"&&i!=="model")throw new x(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(o.role)})`);n.push({role:i==="model"?"assistant":"user",content:Do(o,"clientContent.turns")})}return{items:n,turnComplete:t.turnComplete===!0}}var Vt=16e3,Vo=24e3,Ka=`audio/pcm;rate=${Vo}`,Wo=Buffer.alloc(960).toString("base64");function za(t,e,n){if(e===n)return t;let r=Math.floor(t.length*n/e),o=new Int16Array(r),i=e/n,s=t.length-1;for(let a=0;a<r;a++){let u=a*i,d=Math.floor(u);if(d>=s){o[a]=t[s];continue}let l=u-d;o[a]=Math.round(t[d]+(t[d+1]-t[d])*l)}return o}function Ya(t){let e=te(t);if(!e)throw new x("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(e.mimeType))throw new x(`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 x("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 x("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(n)}function Xa(t){let e=te(t);if(!e)throw new x("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(e.mimeType))throw new x(`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])!==Vt)throw new x(`realtimeInput.audio.mimeType declares rate=${n[1]} \u2014 the Live spec input format is ${Vt} Hz (audio/pcm;rate=${Vt})`);if(typeof e.data!="string"||e.data.length===0)throw new x("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 x(`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 s=0;s<o.length;s++)o[s]=r.readInt16LE(s*2);let i=za(o,Vt,Vo);return Buffer.from(i.buffer,i.byteOffset,i.byteLength).toString("base64")}function Jo(t){if(t.mediaChunks!==void 0)throw new x('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 x("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let o=t.activityStart!==void 0?"activityStart":"activityEnd",i=te(t[o]);if(!i||Object.keys(i).length!==0)throw new x(`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 x(`unsupported realtimeInput field "${r}"`);let n=e.filter(r=>t[r]!==void 0);if(n.length!==1)throw new x(`realtimeInput must carry exactly one of ${e.join(", ")} (got ${n.length})`);if(t.audio!==void 0)return{kind:"audio",base64Pcm24k:Xa(t.audio)};if(t.video!==void 0)return{kind:"video",jpegFrame:Ya(t.video)};if(t.audioStreamEnd!==void 0){if(t.audioStreamEnd!==!0)throw new x("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof t.text!="string"||t.text.length===0)throw new x("realtimeInput.text must be a non-empty string");return{kind:"text",text:t.text}}function Ko(t){for(let e of Object.keys(t))if(e!=="functionResponses")throw new x(`unsupported toolResponse field "${e}"`);if(!Array.isArray(t.functionResponses)||t.functionResponses.length===0)throw new x("toolResponse.functionResponses must be a non-empty array");return t.functionResponses.map(e=>{let n=te(e);if(!n||typeof n.id!="string"||n.id.length===0)throw new x("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 zo(t){return t.type!=="response.output_text.delta"||typeof t.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:t.delta}]}}}}function Yo(t){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:Ka,data:t}}]}}}}function Xo(t){let e=te(t)??{},n=Array.isArray(e.output)?e.output:[],r=[],o=[],i=typeof e.output_text=="string"?e.output_text:"";if(!i)for(let d of n){let l=te(d);if(!(l?.type!=="message"||!Array.isArray(l.content)))for(let c of l.content){let y=te(c);y?.type==="output_text"&&typeof y.text=="string"&&(i+=y.text)}}i&&o.push({role:"assistant",content:i});for(let d of n){let l=te(d);if(l?.type!=="function_call")continue;if(typeof l.call_id!="string"||typeof l.name!="string")throw new x(`upstream function_call item is missing call_id/name: ${JSON.stringify(d)}`,Ie);let c=typeof l.arguments=="string"&&l.arguments.length>0?l.arguments:"{}",y;try{y=JSON.parse(c)}catch{throw new x(`upstream function_call arguments are not valid JSON: ${c}`,Ie)}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:c})}let s=[];r.length>0&&s.push({toolCall:{functionCalls:r}}),s.push({serverContent:{generationComplete:!0}});let a=te(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 s.push(u),{messages:s,assistantItems:o}}_();var Qo=require("crypto"),Zo=require("ws");var Qa=256,qn=class{snapshots=new Map;store(e,n,r){for(this.snapshots.set(`${e}\0${n}`,r);this.snapshots.size>Qa;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(e,n){return this.snapshots.get(`${e}\0${n}`)}},Za="local",mt=t=>t.slice(0,120),Hn=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 re){this.ws.close(Lo,mt(e.message));return}if(e instanceof le){this.ws.close(Jt,mt(e.message));return}if(e instanceof x){this.ws.close(e.closeCode,mt(e.message));return}this.ws.close(Ie,mt(String(e instanceof Error?e.message:e)))}onMessage(e){try{let n=No(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 x(`"${n.kind}" before setup \u2014 the first client message must be BidiGenerateContentSetup`);let r=this.config;if(n.kind==="clientContent"){let{items:o,turnComplete:i}=Go(n.clientContent);this.transcript.push(...o),i&&this.enqueue(()=>this.runTurn());return}if(n.kind==="realtimeInput"){let o=Jo(n.realtimeInput);if(o.kind==="activityStart"||o.kind==="activityEnd"){this.onActivity(o.kind,r);return}if(o.kind==="text"){this.transcript.push({role:"user",content:o.text}),this.enqueue(()=>this.runTurn());return}if(o.kind==="video"){this.appendVideoFrame(o.jpegFrame);return}this.appendAudio(o.kind==="audio"?o.base64Pcm24k:Wo);return}this.transcript.push(...Ko(n.toolResponse)),this.enqueue(()=>this.runTurn())}catch(n){this.fail(n)}}async handleSetup(e){if(this.config)throw new x("setup may only be sent once, as the first client message");let n=jo(e),r=n.sessionResumption.handle;if(r!==null){let i=this.registry.get(this.scope,r);if(!i)throw new x("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",Jt);if(i.model!==n.model)throw new x(`sessionResumption.handle was issued for model "${i.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 x("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!==i.proxyHandle)throw new x(`sessionResumption.handle named pooled session "${i.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`,Jt);this.unsubscribeEnd=await this.clients.onSessionEnd(i.proxyHandle,s=>this.enqueue(()=>this.handleUpstreamEnd(s))),this.transcript=[...i.transcript]}else this.proxyHandle=await this.clients.sessionHandle(n.model),this.unsubscribeEnd=await this.clients.onSessionEnd(this.proxyHandle,i=>this.enqueue(()=>this.handleUpstreamEnd(i))),this.transcript.push(...n.systemItems);this.config=n;let o={};if(n.videoOut){let i=this.clients.openVideoOut;if(i){let s=await i.call(this.clients,n.model),a=s.onOutputFrame(d=>this.send(Bo(d))),u=s.onLaneError(d=>this.fail(d));this.detachVideoOut=()=>{a(),u()},o[qe]=Fo(s.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 x("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openAudio;if(!n)throw new x("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native AudioBridge seam over the canonical `audio` lane)",Ie);return this.audio=(async()=>{let r=await n.call(this.clients,e.model);return this.detachAudio=r.onOutputAudio(o=>this.send(Yo(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 x("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openVideo;if(!n)throw new x("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",Ie);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 x(`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 x("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 x("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,Qo.randomUUID)();this.registry.store(this.scope,r,{model:e.model,proxyHandle:n,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(r),this.send(qo(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(Ho(e.timeLeftMs)),this.ws.close(Ie,mt(`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,i=!1;for await(let u of r){if(n.interrupted)break;let d=u;if(d.type==="error")throw new x(`upstream error: ${JSON.stringify(d)}`,Ie);if(d.type==="response.completed"){o=d.response,i=!0;continue}let l=zo(d);l&&this.send(l)}if(n.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!i)throw new x("upstream produced no response.completed event",Ie);let{messages:s,assistantItems:a}=Xo(o);this.transcript.push(...a);for(let u of s)this.send(u);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},He=class extends Error{constructor(n,r){super(r);this.status=n;this.name="UpgradeAuthError"}status};function eu(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 i=t.headers.authorization;typeof i=="string"&&i.startsWith("Bearer ")&&i.length>7&&r.add(i.slice(7));let s=e.searchParams.get("key")??void 0;if(s!==void 0&&s!==""&&r.add(s),r.size>1)throw new He(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&&s!==void 0&&s!=="")throw new He(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 He(401,"missing credentials: send the org API key in the x-goog-api-key header or as Authorization: Bearer");return a??""}function Gn(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 Zo.WebSocketServer({noServer:!0}),o=new qn;t.on("upgrade",(s,a,u)=>{let d=new URL(s.url??"/","http://localhost");d.pathname===Wt&&(a.on("error",()=>{}),(async()=>{let l=eu(s,d,n),c;if(e.authenticate)c=await e.authenticate(l,s);else{if(e.apiKey!==void 0&&l!==e.apiKey)throw new He(401,"invalid local proxy api key");c={clients:e.clients,scope:Za}}r.handleUpgrade(s,a,u,y=>{new Hn(y,c,o)})})().catch(l=>{let c=502;(l instanceof He||typeof l=="object"&&l!==null&&"status"in l&&typeof l.status=="number")&&(c=l.status);let y=c===401?"Unauthorized":c===403?"Forbidden":c===502?"Bad Gateway":"Internal Server Error",g=l instanceof Error?l.message:String(l);a.destroyed||(a.write(`HTTP/1.1 ${c} ${y}\r
4
12
  Connection: close\r
5
13
  \r
6
14
  ${g}\r
7
- `),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(c=>c===void 0?i():a(c)))})(),s)}O();var _e=require("ws");O();function Te(n){return typeof n.model!="string"||!n.model.trim()?"(default)":n.model.trim().slice(0,128)}var Re=class n{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(e,t,r){this.requests[e]=(this.requests[e]??0)+1,this.localInBytes+=t,this.naiveInBytes+=r}modelEntry(e){let t=this.models[e];if(t)return t;let r=Object.keys(this.models).length>=256?"(other)":e;return this.models[r]??={requests:0,tokens_out:0}}model(e){this.modelEntry(e).requests+=1}track(e,t){let r=this,o=performance.now(),s=null,i=0;return(async function*(){for await(let c of e)c.type==="response.output_text.delta"&&typeof c.delta=="string"&&(s===null&&(s=performance.now(),r.ttftMs.push(s-o),r.ttftMs.length>512&&r.ttftMs.shift()),i+=1,r.tokensOut+=1,r.charsOut+=c.delta.length,t!==void 0&&(r.modelEntry(t).tokens_out+=1)),yield c;let a=(performance.now()-(s??o))/1e3;i>0&&a>0&&(r.tokRates.push(i/a),r.tokRates.length>512&&r.tokRates.shift())})()}static p50(e){if(!e.length)return null;let t=[...e].sort((r,o)=>r-o);return t[Math.floor(t.length/2)]}snapshot(){let e=Object.values(this.requests).reduce((t,r)=>t+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:n.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:n.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}}}},oo=256;function so(n){if(typeof n=="string")return[{role:"user",content:n}];if(Array.isArray(n))return n;throw new Error("responses input must be a string or an array of messages/items")}var Ee=class extends Error{},Ce=class{conversations=new Map;thread(e,t){let r=so(t);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new Ee(`unknown previous_response_id ${String(e)} (proxy restarts drop stored responses)`);return[...o,...r]}remember(e,t,r,o=!0){this.conversations.set(e,[...t,...r]);for(let s of this.conversations.keys()){if(this.conversations.size<=oo)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},Ln=new WeakMap;function Be(n){let e=Ln.get(n);return e||(e=new Ce,Ln.set(n,e)),e}function Ue(n){let e=n?.output;return Array.isArray(e)?e.filter(t=>t.type==="function_call"):[]}function ve(n){return n.find(e=>e.type==="response.completed")?.response??null}function Me(n){return n.type!=="response.function_call_arguments.delta"||typeof n.tool_index!="number"?null:{tool_index:n.tool_index,call_id:typeof n.call_id=="string"?n.call_id:void 0,name:typeof n.name=="string"?n.name:void 0,delta:typeof n.delta=="string"?n.delta:""}}var we=class{calls=new Map;add(e){let t=this.calls.get(e.tool_index),r=!t;return t||(t={args:""},this.calls.set(e.tool_index,t)),e.call_id&&(t.id=e.call_id),e.name&&(t.name=e.name),t.args+=e.delta,{call:t,isNew:r}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([e,t])=>({type:"function_call",call_id:t.id??`call_${e}`,name:t.name??"",arguments:t.args}))}},io=["<tool_call>","<function="];function ao(n){return io.find(e=>n.includes(e))}function Oe(n,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 "${n}" (see catalog parser_defaults)`}function Ae(n,e,t){if(!(!n||e>0))return ao(t)}function Et(n,e){let t=e?.output_text;return typeof t=="string"?t:n.filter(r=>r.type==="response.output_text.delta"&&typeof r.delta=="string").map(r=>r.delta).join("")}function uo(n){let e=n?.output;if(!Array.isArray(e))return[];let t=[];for(let r of e){if(r===null||typeof r!="object")continue;let o=r;o.type==="message"?t.push({role:"assistant",content:o.content??""}):o.type==="function_call"&&t.push({type:"function_call",call_id:o.call_id,name:o.name,arguments:o.arguments})}return t}function Ot(n,e){let t=uo(n);return t.length>0?t:e?[{role:"assistant",content:e}]:[]}var ue=class extends Error{constructor(t,r,o=400,s){super(t);this.code=r;this.status=o;this.param=s}code;status;param};function Ne(n,e){try{return n.thread(e.previous_response_id,e.input)}catch(t){throw t instanceof Ee?new ue(t.message,"previous_response_not_found",400,"previous_response_id"):new ue(String(t instanceof Error?t.message:t),"invalid_input")}}async function At(n,e,t,r){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new ue("instructions must be a string","invalid_instructions");let o=typeof e.instructions=="string"?e.instructions:void 0;return n.createResponse({model:e.model,input:t,stream:r,tools:e.tools,tool_choice:e.tool_choice,temperature:e.temperature,top_p:e.top_p,stop:e.stop,...o!==void 0?{instructions:o}:{},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}:{}})}async function je(n){let{body:e,clients:t,store:r,stats:o}=n,s=Ne(r,e);e.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(s))-Buffer.byteLength(JSON.stringify(e.input)));let i=`resp_${Math.random().toString(36).slice(2,14)}`,a=n.remember??e.store!==!1,c=e.store!==!1,d=Te(e);o.model(d);let h=Array.isArray(e.tools)&&e.tools.length>0,m=await At(t,e,s,!0);return{responseId:i,events:co({events:m,conversation:s,responseId:i,modelKey:d,requestHadTools:h,remember:a,persist:c,store:r,stats:o})}}async function*co(n){let{events:e,conversation:t,responseId:r,modelKey:o,requestHadTools:s,remember:i,persist:a,store:c,stats:d}=n,h=[],m=T=>T.response&&typeof T.response=="object"?{...T,response:{...T.response,id:r}}:T,f=`item_${r.slice(5)}`,g=`rs_${r.slice(5)}`,S=!1,I=!1,A=0;function*$(){S||(S=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let K=!1,X=0,Q="";function*se(){K&&(K=!1,yield{type:"response.reasoning_text.done",item_id:g,output_index:X,content_index:0,text:Q},yield{type:"response.output_item.done",output_index:X,item:{id:g,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:Q}],status:"completed"}})}let k=!1,M=0,ne="";function*Z(T){k&&(k=!1,yield{type:"response.output_text.done",item_id:f,output_index:M,content_index:0,text:ne},yield{type:"response.content_part.done",item_id:f,output_index:M,content_index:0,part:{type:"output_text",text:ne}},yield{type:"response.output_item.done",output_index:M,item:{id:f,type:"message",role:"assistant",status:T,content:[{type:"output_text",text:ne}]}})}let re=new we,B=null,ie=-1;function*q(T){if(!B)return;let N=B;B=null,yield{type:"response.function_call_arguments.done",item_id:N.fcId,output_index:N.outputIndex,arguments:N.args},yield{type:"response.output_item.done",output_index:N.outputIndex,item:{id:N.fcId,type:"function_call",call_id:N.callId??N.fcId,name:N.name??"",arguments:N.args,status:T}}}let J=!1;for await(let T of d.track(e,o)){h.push(T);let N=String(T.type??"");if(N==="response.created"||N==="response.in_progress"){S=!0,yield m(T);continue}if(N==="response.output_item.added"){I=!0,yield m(T);continue}if(N==="response.reasoning_text.delta"&&!I){yield*$(),K||(K=!0,X=A++,yield{type:"response.output_item.added",output_index:X,item:{id:g,type:"reasoning",summary:[],content:[],status:"in_progress"}}),Q+=String(T.delta??""),yield{type:"response.reasoning_text.delta",item_id:g,output_index:X,content_index:0,delta:T.delta};continue}if(N==="response.output_text.delta"&&!I){yield*$(),yield*se(),k||(k=!0,M=A++,yield{type:"response.output_item.added",output_index:M,item:{id:f,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:f,output_index:M,content_index:0,part:{type:"output_text",text:""}}),ne+=String(T.delta??""),yield{type:N,item_id:f,output_index:M,content_index:0,delta:T.delta};continue}let H=I?null:Me(T);if(H){if(yield*$(),yield*se(),yield*Z("completed"),re.add(H),H.tool_index!==ie){yield*q("completed"),ie=H.tool_index;let ae=A++;B={fcId:`fc_${r.slice(5)}_${H.tool_index}`,outputIndex:ae,callId:H.call_id,name:H.name,args:""},yield{type:"response.output_item.added",output_index:ae,item:{id:B.fcId,type:"function_call",call_id:B.callId??B.fcId,name:B.name??"",arguments:"",status:"in_progress"}}}B&&(H.call_id&&(B.callId=H.call_id),H.name&&(B.name=H.name),B.args+=H.delta,yield{type:"response.function_call_arguments.delta",item_id:B.fcId,output_index:B.outputIndex,delta:H.delta});continue}if(N==="response.completed"&&!I){let ae=Ue(T.response),be=Ae(s,re.size+ae.length,ne);if(be){J=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:Oe(o,be)}};break}if(yield*se(),yield*Z("completed"),yield*q("completed"),re.size===0)for(let ce of ae){let oe=A++,E=ce.id??ce.call_id??`fc_${oe}`,ee=ce.arguments??"",L={id:E,type:"function_call",call_id:ce.call_id??E,name:ce.name??""};yield{type:"response.output_item.added",output_index:oe,item:{...L,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:E,output_index:oe,delta:ee},yield{type:"response.function_call_arguments.done",item_id:E,output_index:oe,arguments:ee},yield{type:"response.output_item.done",output_index:oe,item:{...L,arguments:ee,status:"completed"}}}yield m(T);continue}yield m(T)}if(yield*se(),yield*Z("incomplete"),yield*q("incomplete"),i&&!J){let T=ve(h);c.remember(r,t,Ot(T,Et(h,T)),a)}}var Ct="/v1/responses",lo=64*1024*1024,po=8*1024*1024,fo=16,Dn=32,Fn=3600*1e3,mo=/^[A-Za-z0-9._-]{1,256}$/;function te(n,e,t){return{type:"error",status:n,...t!==void 0?{stream_id:t}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var ho=te(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 Tt(){let n,e;return{promise:new Promise((r,o)=>{n=r,e=o}),resolve:n,reject:e}}async function yo(n){for(;n.bufferedAmount>po&&n.readyState===_e.WebSocket.OPEN;){let{promise:e,resolve:t}=Tt();setTimeout(t,5),await e}}var It=class extends Ce{constructor(t){super();this.shared=t}shared;thread(t,r){if(t==null)return super.thread(t,r);try{return super.thread(t,r)}catch(o){if(!(o instanceof Ee))throw o;return this.shared.thread(t,r)}}remember(t,r,o,s=!0){s?this.shared.remember(t,r,o):super.remember(t,r,o)}forget(t){super.forget(t),this.shared.forget(t)}},Pt=class{constructor(e,t,r,o){this.ws=e;this.clients=t;this.stats=r;this.statusOf=o;this.store=new It(Be(t)),e.on("message",(i,a)=>this.onMessage(i,a)),e.on("close",()=>this.dispose()),e.on("error",()=>this.dispose()),setTimeout(()=>{this.closed||(this.sendFrame(te(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${Fn/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},Fn).unref?.()}ws;clients;stats;statusOf;store;lanes=new Map;laneOfResponse=new Map;activeTurns=new Set;active=0;waiters=[];closed=!1;onMessage(e,t){if(this.closed)return;if(t){this.sendFrame(te(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(te(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(te(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(te(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(te(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(te(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"||!mo.test(o.stream_id)){this.sendFrame(ho);return}if(s=o.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=Dn){this.sendFrame(te(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${Dn}). 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??"",c=this.lanes.get(a)??Promise.resolve();this.lanes.set(a,c.then(i).catch(()=>{}))}namedLaneCount(){return this.lanes.has("")?this.lanes.size-1:this.lanes.size}async runTurn(e,t){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 je({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0})}catch(i){this.failTurn(i,t,e);return}this.laneOfResponse.set(o.responseId,t??null);let s=o.events;this.activeTurns.add(s);try{for await(let i of s){if(this.closed||this.ws.readyState!==_e.WebSocket.OPEN)break;this.send(i,t),await yo(this.ws)}}finally{this.activeTurns.delete(s)}}catch(r){this.failTurn(r,t,e)}finally{this.release()}}}runWarmup(e,t){if(this.closed)return;let r=`resp_${Math.random().toString(36).slice(2,14)}`;try{let o=Ne(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,t??null),this.send({type:"response.created",response:{id:r,object:"response",status:"in_progress"}},t)}catch(o){this.failTurn(o,t,e)}}failTurn(e,t,r){this.sendFrame(this.errorFrameOf(e,t));let o=r.previous_response_id;typeof o=="string"&&this.laneOfResponse.get(o)===(t??null)&&(this.store.forget(o),this.laneOfResponse.delete(o))}errorFrameOf(e,t){let r=String(e instanceof Error?e.message:e);if(e instanceof ue)return te(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},t);if(e instanceof z)return te(404,{type:"invalid_request_error",code:"model_not_found",message:r},t);if(e instanceof Y)return te(503,{type:"upstream_error",code:"session_gone",message:r},t);let o=this.statusOf?.(e)??null;return o?te(o.status,{type:o.openaiType,message:r},t):te(500,{type:"upstream_error",message:r},t)}async acquire(){for(;this.active>=fo;){let{promise:e,resolve:t}=Tt();this.waiters.push(t),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,t){if(this.closed||this.ws.readyState!==_e.WebSocket.OPEN)return;let r=t===void 0?e:{...e,stream_id:t};this.ws.send(JSON.stringify(r))}sendFrame(e){this.closed||this.ws.readyState!==_e.WebSocket.OPEN||this.ws.send(JSON.stringify(e))}};function Ze(n,e,t){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${t}\r
8
- `;n.end(`HTTP/1.1 ${e} ${r}\r
15
+ `),a.destroy())}))});let i=null;return()=>(i??=(async()=>{for(let s of r.clients)s.close(1001,"server closing");await new Promise((s,a)=>r.close(u=>u===void 0?s():a(u)))})(),i)}_();var Fe=require("ws");_();_();var ei=require("crypto"),ti="Inference-Id";function gt(){return(0,ei.randomUUID)()}function Vn(t){return t==="/v1"||t.startsWith("/v1/")}function ni(t,e){if(!Vn(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 ht(t){return`resp_${t}`}function ri(t){return`chatcmpl-${t}`}function oi(t){return`msg_${t}`}function Te(t){return e=>{t.sessionId=e.sessionId,t.catalog=e.identity}}function ii(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 si(t){return`${JSON.stringify(t)}
16
+ `}var tu=256,nu=128;function Pe(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,nu)}var Ge=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>=tu?"(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,i=performance.now(),s=null,a=0;return(async function*(){for await(let d of e)d.type==="response.output_text.delta"&&typeof d.delta=="string"&&(s===null&&(s=performance.now(),o.ttftMs.push(s-i),o.ttftMs.length>512&&o.ttftMs.shift(),r!==void 0&&(r.ttftMs=s-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()-(s??i))/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}}}},ru=256;function ou(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 Ve=class extends Error{},Ze=class{conversations=new Map;thread(e,n){let r=ou(n);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new Ve(`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 i of this.conversations.keys()){if(this.conversations.size<=ru)break;this.conversations.delete(i)}}forget(e){this.conversations.delete(e)}},ai=new WeakMap;function yt(t){let e=ai.get(t);return e||(e=new Ze,ai.set(t,e)),e}function et(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function Ce(t){return t.find(e=>e.type==="response.completed")?.response??null}function tt(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 De=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}))}},iu=["<tool_call>","<function="];function su(t){return iu.find(e=>t.includes(e))}function We(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 Je(t,e,n){if(!(!t||e>0))return su(n)}function Wn(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 au(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 Jn(t,e){let n=au(t);return n.length>0?n:e?[{role:"assistant",content:e}]:[]}var _e=class extends Error{constructor(n,r,o=400,i){super(n);this.code=r;this.status=o;this.param=i}code;status;param};function nt(t,e){try{return t.thread(e.previous_response_id,e.input)}catch(n){throw n instanceof Ve?new _e(n.message,"previous_response_not_found",400,"previous_response_id"):new _e(String(n instanceof Error?n.message:n),"invalid_input")}}function ui(t){let e=t.text;if(e!==null&&typeof e=="object"){let n=e.format;if(n!==void 0)return n}}async function Kn(t,e,n,r,o){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new _e("instructions must be a string","invalid_instructions");let i=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,...i!==void 0?{instructions:i}:{},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}:{},...ui(e)!==void 0?{response_format:ui(e)}:{}},o)}async function _t(t){let{body:e,clients:n,store:r,stats:o}=t,i=nt(r,e);e.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(i))-Buffer.byteLength(JSON.stringify(e.input)));let s=t.responseId,a=t.remember??e.store!==!1,u=e.store!==!1,d=Pe(e);o.model(d);let l=Array.isArray(e.tools)&&e.tools.length>0,c=t.turn,y=await Kn(n,e,i,!0,c?Te(c):void 0);return{responseId:s,events:uu({events:y,conversation:i,responseId:s,modelKey:d,requestHadTools:l,remember:a,persist:u,store:r,stats:o,turn:t.turn})}}async function*uu(t){let{events:e,conversation:n,responseId:r,modelKey:o,requestHadTools:i,remember:s,persist:a,store:u,stats:d}=t,l=[],c=N=>N.response&&typeof N.response=="object"?{...N,response:{...N.response,id:r}}:N,y=`item_${r.slice(5)}`,g=`rs_${r.slice(5)}`,E=!1,w=!1,h=0;function*b(){E||(E=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let R=!1,T=0,B="";function*M(){R&&(R=!1,yield{type:"response.reasoning_text.done",item_id:g,output_index:T,content_index:0,text:B},yield{type:"response.output_item.done",output_index:T,item:{id:g,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:B}],status:"completed"}})}let $=!1,k=0,H="";function*W(N){$&&($=!1,yield{type:"response.output_text.done",item_id:y,output_index:k,content_index:0,text:H},yield{type:"response.content_part.done",item_id:y,output_index:k,content_index:0,part:{type:"output_text",text:H}},yield{type:"response.output_item.done",output_index:k,item:{id:y,type:"message",role:"assistant",status:N,content:[{type:"output_text",text:H}]}})}let z=new De,j=null,ge=-1;function*ce(N){if(!j)return;let J=j;j=null,yield{type:"response.function_call_arguments.done",item_id:J.fcId,output_index:J.outputIndex,arguments:J.args},yield{type:"response.output_item.done",output_index:J.outputIndex,item:{id:J.fcId,type:"function_call",call_id:J.callId??J.fcId,name:J.name??"",arguments:J.args,status:N}}}let ne=!1;for await(let N of d.track(e,o,t.turn)){l.push(N);let J=String(N.type??"");if(J==="response.created"||J==="response.in_progress"){E=!0,yield c(N);continue}if(J==="response.output_item.added"){w=!0,yield c(N);continue}if(J==="response.reasoning_text.delta"&&!w){yield*b(),R||(R=!0,T=h++,yield{type:"response.output_item.added",output_index:T,item:{id:g,type:"reasoning",summary:[],content:[],status:"in_progress"}}),B+=String(N.delta??""),yield{type:"response.reasoning_text.delta",item_id:g,output_index:T,content_index:0,delta:N.delta};continue}if(J==="response.output_text.delta"&&!w){yield*b(),yield*M(),$||($=!0,k=h++,yield{type:"response.output_item.added",output_index:k,item:{id:y,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:y,output_index:k,content_index:0,part:{type:"output_text",text:""}}),H+=String(N.delta??""),yield{type:J,item_id:y,output_index:k,content_index:0,delta:N.delta};continue}let X=w?null:tt(N);if(X){if(yield*b(),yield*M(),yield*W("completed"),z.add(X),X.tool_index!==ge){yield*ce("completed"),ge=X.tool_index;let ue=h++;j={fcId:`fc_${r.slice(5)}_${X.tool_index}`,outputIndex:ue,callId:X.call_id,name:X.name,args:""},yield{type:"response.output_item.added",output_index:ue,item:{id:j.fcId,type:"function_call",call_id:j.callId??j.fcId,name:j.name??"",arguments:"",status:"in_progress"}}}j&&(X.call_id&&(j.callId=X.call_id),X.name&&(j.name=X.name),j.args+=X.delta,yield{type:"response.function_call_arguments.delta",item_id:j.fcId,output_index:j.outputIndex,delta:X.delta});continue}if(J==="response.completed"&&!w){let ue=et(N.response),xe=Je(i,z.size+ue.length,H);if(xe){ne=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:We(o,xe)}};break}if(yield*M(),yield*W("completed"),yield*ce("completed"),z.size===0)for(let Re of ue){let ve=h++,P=Re.id??Re.call_id??`fc_${ve}`,he=Re.arguments??"",Ue={id:P,type:"function_call",call_id:Re.call_id??P,name:Re.name??""};yield{type:"response.output_item.added",output_index:ve,item:{...Ue,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:P,output_index:ve,delta:he},yield{type:"response.function_call_arguments.done",item_id:P,output_index:ve,arguments:he},yield{type:"response.output_item.done",output_index:ve,item:{...Ue,arguments:he,status:"completed"}}}yield c(N);continue}yield c(N)}if(yield*M(),yield*W("incomplete"),yield*ce("incomplete"),s&&!ne){let N=Ce(l);u.remember(r,n,Jn(N,Wn(l,N)),a)}}var Xn="/v1/responses",du=64*1024*1024,lu=8*1024*1024,cu=16,di=32,li=3600*1e3,pu=/^[A-Za-z0-9._-]{1,256}$/;function fe(t,e,n){return{type:"error",status:t,...n!==void 0?{stream_id:n}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var fu=fe(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 Qn(){let t,e;return{promise:new Promise((r,o)=>{t=r,e=o}),resolve:t,reject:e}}async function mu(t){for(;t.bufferedAmount>lu&&t.readyState===Fe.WebSocket.OPEN;){let{promise:e,resolve:n}=Qn();setTimeout(n,5),await e}}var zn=class extends Ze{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 Ve))throw o;return this.shared.thread(n,r)}}remember(n,r,o,i=!0){i?this.shared.remember(n,r,o):super.remember(n,r,o)}forget(n){super.forget(n),this.shared.forget(n)}},Yn=class{constructor(e,n,r,o){this.ws=e;this.clients=n;this.stats=r;this.statusOf=o;this.store=new zn(yt(n)),e.on("message",(s,a)=>this.onMessage(s,a)),e.on("close",()=>this.dispose()),e.on("error",()=>this.dispose()),setTimeout(()=>{this.closed||(this.sendFrame(fe(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${li/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},li).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(fe(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(fe(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(fe(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(fe(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(fe(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(fe(400,{type:"invalid_request_error",code:"background_not_supported",message:"'background' is not supported over the Responses WebSocket",param:"background"}));return}let i;if(o.stream_id!==void 0){if(typeof o.stream_id!="string"||!pu.test(o.stream_id)){this.sendFrame(fu);return}if(i=o.stream_id,!this.lanes.has(i)&&this.namedLaneCount()>=di){this.sendFrame(fe(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${di}). Reuse an existing stream_id or open a new WebSocket connection.`,param:"stream_id"},i));return}}let s=o.generate===!1?()=>this.runWarmup(o,i):()=>this.runTurn(o,i),a=i??"",u=this.lanes.get(a)??Promise.resolve();this.lanes.set(a,u.then(s).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 _t({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:ht(gt())})}catch(s){this.failTurn(s,n,e);return}this.laneOfResponse.set(o.responseId,n??null);let i=o.events;this.activeTurns.add(i);try{for await(let s of i){if(this.closed||this.ws.readyState!==Fe.WebSocket.OPEN)break;this.send(s,n),await mu(this.ws)}}finally{this.activeTurns.delete(i)}}catch(r){this.failTurn(r,n,e)}finally{this.release()}}}runWarmup(e,n){if(this.closed)return;let r=ht(gt());try{let o=nt(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 _e)return fe(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},n);if(e instanceof re)return fe(404,{type:"invalid_request_error",code:"model_not_found",message:r},n);if(e instanceof le)return fe(503,{type:"upstream_error",code:"session_gone",message:r},n);let o=this.statusOf?.(e)??null;return o?fe(o.status,{type:o.openaiType,message:r},n):fe(500,{type:"upstream_error",message:r},n)}async acquire(){for(;this.active>=cu;){let{promise:e,resolve:n}=Qn();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!==Fe.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!==Fe.WebSocket.OPEN||this.ws.send(JSON.stringify(e))}};function Kt(t,e,n){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${n}\r
17
+ `;t.end(`HTTP/1.1 ${e} ${r}\r
9
18
  Connection: close\r
10
19
  Content-Type: text/plain; charset=utf-8\r
11
20
  Content-Length: ${Buffer.byteLength(o)}\r
12
21
  \r
13
- ${o}`)}function Ut(n,e){let t=new _e.WebSocketServer({noServer:!0,maxPayload:lo}),r=new Re,o=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,c=!1,d=(h,m,f)=>{let g;try{g=new URL(h.url??"/","http://localhost")}catch{Ze(m,400,"malformed upgrade URL");return}if(m.on("error",()=>m.destroy()),m.on("close",()=>i.delete(m)),g.pathname!==Ct){i.add(m);return}s.add(m);let S=!1;(async()=>{if(e.apiKey&&(h.headers.authorization??"")!==`Bearer ${e.apiKey}`){Ze(m,401,"invalid local proxy api key");return}let I;try{I=await a(h)}catch(A){let $=e.statusOf?.(A)??null;Ze(m,$?.status??500,"upstream resolver rejected the connection");return}c||m.destroyed||(S=!0,t.handleUpgrade(h,m,f,A=>{s.delete(m),o.add(A),A.on("close",()=>o.delete(A));try{new Pt(A,I,r,e.statusOf)}catch{o.delete(A),A.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!S&&!m.destroyed&&Ze(m,500,"responses websocket upgrade failed")}).finally(()=>s.delete(m))};return n.on("upgrade",d),{stats:r,close:()=>{let{promise:h,resolve:m}=Tt();if(c)return m(),h;c=!0,n.off("upgrade",d);for(let g of s)g.destroy();s.clear();for(let g of i)g.destroy();i.clear();for(let g of o)g.close(1001,"server shutting down");let f=setTimeout(()=>{for(let g of o)g.readyState!==_e.WebSocket.CLOSED&&g.terminate()},2e3);return f.unref?.(),t.close(()=>{clearTimeout(f),m()}),h}}}O();var go=require("http");function $t(n){n.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function pe(n,e,t){n.writeHead(e,{"content-type":"application/json"}),n.end(JSON.stringify(t))}function de(n,e,t,r="invalid_request_error"){pe(n,e,{error:{message:t,type:r}})}function me(n,e,t,r){pe(n,e,{type:"error",error:{type:t,message:r}})}function et(n,e,t){if(e==="anthropic"){pe(n,404,{type:"error",error:{type:"not_found_error",message:t.message}});return}pe(n,404,{error:{message:t.message,type:"invalid_request_error",code:"model_not_found"}})}var Hn=64*1024*1024,tt=class extends Error{};async function wo(n){let e=[],t=0;for await(let o of n){if(t+=o.length,t>Hn)throw new tt(`request body exceeds ${Hn} bytes`);e.push(o)}let r=Buffer.concat(e).toString("utf8");return r?JSON.parse(r):{}}function $e(n,e,t,r){return{id:n,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:e,choices:[{index:0,delta:t,finish_reason:r}]}}function nt(n){let e=n?.usage;if(!e||typeof e!="object")return null;let t=i=>typeof i=="number"&&Number.isFinite(i)&&i>=0?i:0,r=t(e.input_tokens),o=t(e.output_tokens);if(r===0&&o===0)return null;let s=t(e.total_tokens)||r+o;return{input_tokens:r,output_tokens:o,total_tokens:s}}function Bn(n){let e=nt(n);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 jn(n){let e=n?.output;if(!Array.isArray(e))return"";let t="";for(let r of e)if(!(r.type!=="reasoning"||!Array.isArray(r.content)))for(let o of r.content)o.type==="reasoning_text"&&(t+=String(o.text??""));return t}function vo(n){if(Array.isArray(n))return n.map(e=>{let t=e.function;return!t||typeof t!="object"?e:{type:"function",name:t.name,description:t.description,parameters:t.parameters,strict:t.strict}})}var rt=class extends Error{};function _o(n,e){return Array.isArray(e)?e.map(t=>{let r=t;if(r.type==="text")return{type:n==="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 rt(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function ko(n){let e=[];for(let t of n){if(t.role==="tool"){e.push({type:"function_call_output",call_id:t.tool_call_id,output:typeof t.content=="string"?t.content:JSON.stringify(t.content??"")});continue}if(t.role==="assistant"&&Array.isArray(t.tool_calls)){typeof t.content=="string"&&t.content&&e.push({role:"assistant",content:t.content});for(let r of t.tool_calls){let o=r.function??{};e.push({type:"function_call",call_id:r.id,name:o.name,arguments:o.arguments})}continue}e.push({...t,content:_o(t.role,t.content)})}return e}function Mt(n){return Ue(n).map((e,t)=>({index:t,id:e.call_id??e.id??`call_${t}`,type:"function",function:{name:e.name??"",arguments:e.arguments??"{}"}}))}async function bo(n,e,t,r,o){if(n.stream===!0){let A;try{A=await je({body:n,clients:e,store:t,stats:o})}catch($){if($ instanceof z){et(r,"openai",$);return}if($ instanceof ue){de(r,$.status,$.message);return}throw $}$t(r);for await(let $ of A.events)r.write(`event: ${String($.type??"message")}
14
- data: ${JSON.stringify($)}
22
+ ${o}`)}function Zn(t,e){let n=new Fe.WebSocketServer({noServer:!0,maxPayload:du}),r=new Ge,o=new Set,i=new Set,s=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,u=!1,d=(l,c,y)=>{let g;try{g=new URL(l.url??"/","http://localhost")}catch{Kt(c,400,"malformed upgrade URL");return}if(c.on("error",()=>c.destroy()),c.on("close",()=>s.delete(c)),g.pathname!==Xn){s.add(c);return}i.add(c);let E=!1;(async()=>{if(e.apiKey&&(l.headers.authorization??"")!==`Bearer ${e.apiKey}`){Kt(c,401,"invalid local proxy api key");return}let w;try{w=await a(l)}catch(h){let b=e.statusOf?.(h)??null;Kt(c,b?.status??500,"upstream resolver rejected the connection");return}u||c.destroyed||(E=!0,n.handleUpgrade(l,c,y,h=>{i.delete(c),o.add(h),h.on("close",()=>o.delete(h));try{new Yn(h,w,r,e.statusOf)}catch{o.delete(h),h.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!E&&!c.destroyed&&Kt(c,500,"responses websocket upgrade failed")}).finally(()=>i.delete(c))};return t.on("upgrade",d),{stats:r,close:()=>{let{promise:l,resolve:c}=Qn();if(u)return c(),l;u=!0,t.off("upgrade",d);for(let g of i)g.destroy();i.clear();for(let g of s)g.destroy();s.clear();for(let g of o)g.close(1001,"server shutting down");let y=setTimeout(()=>{for(let g of o)g.readyState!==Fe.WebSocket.CLOSED&&g.terminate()},2e3);return y.unref?.(),n.close(()=>{clearTimeout(y),c()}),l}}}_();_();var hi=require("@urun-sh/core");_();var ci=require("@urun-sh/core"),gu={no_capacity:429,queue_timeout:429,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502};function zt(t){if(t==null)return null;let e=gu[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 Yt(t){if(t.kind==="create-failed")return zt(t.status.error?.code??null);if(t.kind!=="never-live")return null;let e=(t.status.elapsedMs??0)>=ci.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return zt(e?"cold_start_exceeded":"queue_timeout")}var yi=require("ws");_();_();var fi=require("crypto");function U(t){return`${t}_${(0,fi.randomUUID)().replace(/-/g,"").slice(0,24)}`}var Xt=4*1024*1024,er=8*1024*1024,Qt=512,Zt=20*1024*1024;function oe(t,e,n){return{type:"invalid_request_error",code:t,message:e,param:n??null}}function vt(t,e){return{type:"server_error",code:t,message:e}}function Y(t){return{type:"error",event_id:U("event"),error:t}}function tr(){return{outputModalities:["text"],turnDetection:null,inputAudioTranscription:!1}}var L=class extends Error{constructor(n,r,o="invalid_value"){super(n);this.param=r;this.code=o;this.name="SessionUpdateError"}param;code},hu={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},yu={format:!0,transcription:!0,turn_detection:!0,noise_reduction:!0},_u={format:!0,voice:!0,speed:!0};function pi(t,e){let n=t;if(!n||typeof n!="object"||n.type!=="audio/pcm"||n.rate!==void 0&&n.rate!==24e3)throw new L(`${e} must be {"type":"audio/pcm","rate":24000} \u2014 the session audio lane is 24 kHz mono PCM16 only`,e)}function wt(t,e,n){if(!t||typeof t!="object"||Array.isArray(t))throw new L("session must be an object","session","invalid_request");let r=t,o=Object.keys(r).filter(s=>!(s in hu));if(o.length>0)throw new L(`session field(s) not supported by this backend: ${o.join(", ")}`,o[0],"unsupported_field");let i={...e,outputModalities:[...e.outputModalities]};for(let[s,a]of Object.entries(r))switch(s){case"model":if(a!==null&&typeof a!="string")throw new L("model must be a string or null","model");i.model=a??void 0;break;case"instructions":if(a!==null&&typeof a!="string")throw new L("instructions must be a string or null","instructions");i.instructions=a??void 0;break;case"modalities":{if(!Array.isArray(a)||a.length===0)throw new L("modalities must be a non-empty array","modalities");let u=[];for(let d of a){if(d!=="text"&&d!=="audio")throw new L(`modality ${JSON.stringify(d)} is not "text" or "audio"`,"modalities");if(d==="audio"&&!n.voice)throw new L('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)}i.outputModalities=u;break}case"voice":if(a===null){delete i.voice;break}if(typeof a!="string")throw new L("voice must be a string or null","voice");if(!n.voice)throw new L("voice refused: no authoritative voice binding for the resolved backend","voice","audio_not_supported");i.voice=a;break;case"input_audio_format":case"output_audio_format":if(a!=="pcm16")throw new L(`${s} must be "pcm16" \u2014 the session audio lane is 24 kHz mono PCM16 only`,s);break;case"temperature":if(a!==null&&(typeof a!="number"||!Number.isFinite(a)))throw new L("temperature must be a finite number or null","temperature");i.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 L(`${s} must be a positive integer, "inf" or null`,s);i.maxOutputTokens=a===null?void 0:a;break;case"tools":if(!Array.isArray(a))throw new L("tools must be an array","tools");i.tools=a;break;case"tool_choice":i.toolChoice=a;break;case"metadata":if(a!==null&&(!a||typeof a!="object"||Array.isArray(a)))throw new L("metadata must be an object or null","metadata");i.metadata=a??null;break;case"turn_detection":{if(a===null){i.turnDetection=null;break}if(!n.turns)throw new L("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 L("turn_detection must be an object or null","turn_detection");let d=String(u.type??"");if(d!=="server_vad"&&d!=="semantic_vad")throw new L(`turn_detection.type ${JSON.stringify(u.type)} is not "server_vad"/"semantic_vad"`,"turn_detection.type");i.turnDetection={createResponse:u.create_response!==!1,interruptResponse:u.interrupt_response!==!1};break}case"input_audio_transcription":{if(a===null){i.inputAudioTranscription=!1;break}if(!n.turns)throw new L("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 L("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 L(`input_audio_transcription field not honored by this backend: ${u} (transcription comes from the voice engine itself)`,`input_audio_transcription.${u}`,"unsupported_field");i.inputAudioTranscription=!0;break}case"input_audio_noise_reduction":if(a!==null)throw new L("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 L("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 L("audio.input must be an object","audio.input");for(let l of Object.keys(d))if(!(l in yu))throw new L(`audio.input field not supported by this backend: ${l}`,`audio.input.${l}`,"unsupported_field");if(d.format!==void 0&&pi(d.format,"audio.input.format"),d.transcription!==null&&d.transcription!==void 0){let l=wt({input_audio_transcription:d.transcription},i,n);i.inputAudioTranscription=l.inputAudioTranscription}if(d.turn_detection!==null&&d.turn_detection!==void 0){let l=wt({turn_detection:d.turn_detection},i,n);i.turnDetection=l.turnDetection}if(d.noise_reduction!==null&&d.noise_reduction!==void 0)throw new L("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 L("audio.output must be an object","audio.output");for(let l of Object.keys(d))if(!(l in _u))throw new L(`audio.output field not supported by this backend: ${l}`,`audio.output.${l}`,"unsupported_field");if(d.format!==void 0&&pi(d.format,"audio.output.format"),d.speed!==void 0)throw new L("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 i.voice;else{if(typeof d.voice!="string")throw new L("audio.output.voice must be a string or null","audio.output.voice");if(!n.voice)throw new L("audio.output.voice refused: no authoritative voice binding for the resolved backend","audio.output.voice","audio_not_supported");i.voice=d.voice}d.format!==void 0&&!i.outputModalities.includes("audio")&&(i.outputModalities=["text","audio"])}break}default:break}return i}function en(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 nr(t,e){let n=t;if(!n||typeof n!="object")throw new L("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 L("function_call_output item requires string call_id and output","item.call_id","invalid_request");return{id:typeof n.id=="string"?n.id:U("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 L('message item role must be "user", "assistant" or "system"',"item.role","invalid_request");if(!Array.isArray(n.content))throw new L("message item content must be an array","item.content","invalid_request");let o=[];for(let i of n.content){let s=i;if(!s||typeof s!="object")throw new L("content part must be an object","item.content","invalid_request");if(s.type==="input_text"||r==="assistant"&&s.type==="output_text"){if(typeof s.text!="string")throw new L(`${String(s.type)} part requires string text`,"item.content.text","invalid_request");o.push({type:s.type,text:s.text});continue}if(s.type==="input_audio"){if(!e)throw new L("input_audio content refused: no authoritative voice binding for the resolved backend","item.content.type","audio_not_supported");if(typeof s.audio!="string")throw new L("input_audio part requires base64 audio","item.content.audio","invalid_request");o.push({type:"input_audio",...typeof s.transcript=="string"?{transcript:s.transcript}:{}});continue}throw new L(`content part type ${String(s.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:U("item"),type:"message",role:r,content:o}}throw new L(`item type ${String(n.type)} is not supported \u2014 message and function_call_output items only`,"item.type","invalid_request")}function rr(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 i of r.content??[]){if(i.type==="input_text"||i.type==="output_text"){o.push({type:i.type,text:i.text});continue}if(i.type==="input_audio"){let s=i.transcript;if(typeof s=="string"&&s.trim().length>0){o.push({type:"input_text",text:s});continue}n=!0;continue}}e.push({role:r.role,content:o})}return{input:e,sawAudioOnlyItem:n}}var wu=1001,vu=1011,mi=t=>t.slice(0,120),bu=/^[A-Za-z0-9+/=]*$/,ku=2;function xu(t){let e=Buffer.from(t,"base64");for(let n=0;n+1<e.byteLength;n+=2)if(Math.abs(e.readInt16LE(n))>ku)return!1;return!0}var bt=class{id=U("conn");ws;binding;onClosed;config=tr();sessionId=U("sess");conversationId=U("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:U("event"),session:en(this.config,this.sessionId)}),this.send({type:"conversation.created",event_id:U("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(Y(vt("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:U("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:U("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=U("item"),i=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:U("event"),previous_item_id:i,item_id:o}),this.send({type:"conversation.item.created",event_id:U("event"),previous_item_id:i,item:this.itemToProtocol(this.items[this.items.length-1])}),this.send({type:"input_audio_buffer.speech_stopped",event_id:U("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:U("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(oe("decode_error","frame is not decodable text"),"invalid_frame");return}if(Buffer.byteLength(n,"utf8")>Xt){this.fail(oe("frame_too_large",`frame exceeds the ${Xt}-byte limit`),"invalid_frame");return}let r;try{r=JSON.parse(n)}catch{this.fail(oe("invalid_json","frame is not valid JSON"),"invalid_frame");return}if(typeof r.type!="string"||r.type.length===0){this.fail(oe("missing_type",'event is missing a string "type"'),"invalid_frame");return}try{await this.handle(r)}catch(o){let i=o;if(i&&typeof i=="object"&&i.name==="SessionUpdateError"){this.send(Y({type:"invalid_request_error",code:i.code??"invalid_value",message:i.message,param:i.param??null}));return}this.fail(vt(null,o instanceof Error?o.message:String(o)),"handler_fault")}}async handle(e){switch(e.type){case"session.update":this.config=wt(e.session,this.config,{voice:this.binding.voice,turns:this.binding.turns!==void 0}),this.send({type:"session.updated",event_id:U("event"),session:en(this.config,this.sessionId)});return;case"conversation.item.create":{let n=nr(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:U("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(Y(oe("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:U("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(Y(oe("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.send({type:"conversation.item.retrieved",event_id:U("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(Y(oe("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:U("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(Y(oe("invalid_audio",'input_audio_buffer.append requires base64 "audio"',"audio")));return}if(!this.ensureVoice())return;if(!bu.test(e.audio)){this.send(Y(oe("invalid_audio","input_audio_buffer.append audio must be base64","audio")));return}let n=Buffer.byteLength(e.audio,"base64");if(this.pendingAudioBytes.length+n>Zt){this.send(Y(oe("buffer_full",`uncommitted input audio exceeds the ${Zt}-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:U("event")});return}case"input_audio_buffer.commit":{if(!this.ensureVoice())return;if(this.pendingAudioTotalBytes===0&&this.takeTurnTranscript()===null){this.send(Y(oe("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=U("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:U("event"),previous_item_id:o,item_id:r}),this.send({type:"conversation.item.created",event_id:U("event"),previous_item_id:o,item:this.itemToProtocol(this.items[this.items.length-1])});return}case"output_audio_buffer.clear":{this.send(Y(oe("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(Y(oe("unknown_event",`event type ${e.type} is not supported by this adapter`)))}}ensureVoice(){return!this.binding.voice||!this.binding.audioSession?(this.send(Y(oe("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(Y({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")return r.sendResponseCreate(e,e.input??[],n);let o=this.binding.clients;if(o&&typeof o.createResponse=="function")return await o.createResponse(e);throw new Error("realtime binding provides neither session.sendResponseCreate nor clients.createResponse \u2014 no upstream turn seam")}startResponse(e){if(this.inFlight){this.send(Y(oe("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(Y(oe("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(Y(oe("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=U("req"),o=`resp_${r}`;this.activeResponseId=o;let i=this.items.length,{input:s,sawAudioOnlyItem:a}=rr(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,c=l==="inf"||l===void 0?void 0:l,y=n.tools!==void 0?n.tools:this.config.tools,g=n.tool_choice!==void 0?n.tool_choice:this.config.toolChoice,E={input:s,stream:!0,...this.config.model?{model:this.config.model}:{},...u!==void 0?{instructions:u}:{},...d!==void 0?{temperature:d}:{},...c!==void 0?{max_output_tokens:c}:{},...y!==void 0?{tools:y}:{},...g!==void 0?{tool_choice:g}:{}};for(let w of this.frozenAudio.splice(0))this.binding.audioSession?.audio.appendInputAudio(w);this.inFlight=(async()=>{let w=[],h=B=>{this.responseFlushed?this.send(B):w.push(B)},b=()=>{if(!this.responseFlushed){this.send({type:"response.created",event_id:U("event"),response:this.responseObject(o,"in_progress")}),this.send({type:"response.in_progress",event_id:U("event"),response:this.responseObject(o,"in_progress")});for(let B of w)this.send(B);w.length=0,this.responseFlushed=!0}},R={messageOpen:!1,messageText:"",tools:new Map,nextOutputIndex:0},T=null;try{a&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let B;try{B=await this.upstreamStream(E,r)}catch(M){this.emitUpstreamFault(M,o);return}try{for await(let M of B){if(this.cancelled)break;let $=M;if($.type==="error"){let k=this.upstreamErrorBody($);this.responseFlushed?this.send(Y(k)):this.send({type:"response.failed",event_id:U("event"),response:this.failedResponse(o,k)});return}if($.type==="response.output_text.delta"&&typeof $.delta=="string"){b(),R.messageOpen||(this.beginMessageItem(o,R.nextOutputIndex++),R.messageOpen=!0),h({type:"response.output_text.delta",event_id:U("event"),item_id:`${o}_msg_0`,response_id:o,output_index:0,content_index:0,delta:$.delta}),R.messageText+=$.delta;continue}if($.type==="response.function_call_arguments.delta"){b();let k=typeof $.delta=="string"?$.delta:"",H=typeof $.call_id=="string"&&$.call_id.length>0?$.call_id:`${o}_fc_${R.nextOutputIndex}`,W=R.tools.get(H);if(!W){let z=typeof $.name=="string"&&$.name.length>0?$.name:void 0;W={outputIndex:R.nextOutputIndex++,name:z,args:""},R.tools.set(H,W),h({type:"response.output_item.added",event_id:U("event"),response_id:o,output_index:W.outputIndex,item:{id:H,object:"realtime.item",type:"function_call",call_id:H,name:z??"",arguments:"",status:"in_progress"}})}typeof $.name=="string"&&$.name.length>0&&(W.name=$.name),W.args+=k,h({type:"response.function_call_arguments.delta",event_id:U("event"),item_id:H,response_id:o,output_index:W.outputIndex,call_id:H,name:W.name??"",delta:k});continue}if($.type==="response.completed"){T=$.response??null;continue}}}catch(M){this.emitUpstreamFault(M,o);return}if(this.cancelled)return;this.closeOpenItems(o,R,i),b(),this.send({type:"response.done",event_id:U("event"),response:this.doneResponse(o,i,T)})}finally{this.inFlight=null,this.activeResponseId=null}})()}onOutputAudioFrame(e,n){if(this.cancelled)return;if(this.activeResponseId!==e){if(xu(n))return;!this.outOfTurnAudioWarned&&!this.closed&&(this.outOfTurnAudioWarned=!0,this.send(Y({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:U("event"),response:this.responseObject(e,"in_progress")}),this.send({type:"response.in_progress",event_id:U("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:U("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:U("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:U("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:U("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:U("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`,i=this.messageItemOpenForAudio;i&&this.send({type:"response.output_audio.done",event_id:U("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:U("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:U("event"),item_id:o,response_id:e,output_index:0,content_index:0,part:i?{type:"output_audio",transcript:""}:{type:"output_text",text:n.messageText}});let s=[];i&&s.push({type:"output_audio"}),n.messageText.length>0&&s.push({type:"output_text",text:n.messageText});let a={id:o,type:"message",role:"assistant",content:s};this.pushItem(a),this.send({type:"response.output_item.done",event_id:U("event"),response_id:e,output_index:0,item:this.itemToProtocol(a)})}for(let[o,i]of n.tools){let s=i.args.length>0?i.args:"{}";this.send({type:"response.function_call_arguments.done",event_id:U("event"),item_id:o,response_id:e,output_index:i.outputIndex,call_id:o,name:i.name??"",arguments:s});let a={id:o,type:"function_call",callId:o,name:i.name,arguments:s};this.pushItem(a),this.send({type:"response.output_item.done",event_id:U("event"),response_id:e,output_index:i.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,i=[];for(let s=n;s<this.items.length;s++)i.push(this.itemToProtocol(this.items[s]));return this.responseObject(e,"completed",{output:i,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=vt(null,e instanceof Error?e.message:String(e));this.responseFlushed?this.send(Y(r)):this.send({type:"response.failed",event_id:U("event"),response:this.failedResponse(n,r)})}cancelResponse(){let e=this.inFlight;if(!e){this.send(Y(oe("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:U("event"),response:this.responseObject(n,"cancelled")})})}pushItem(e){this.items.push(e),this.items.length>Qt&&(this.items.shift(),this.send(Y({type:"invalid_request_error",code:"transcript_evicted",message:`the conversation transcript exceeded ${Qt} 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>er){this.ws.close(wu,mi("outbound buffer exceeded")),this.teardown("outbound buffer exceeded");return}this.ws.send(JSON.stringify(e))}}fail(e,n){this.send(Y(e)),this.ws.close(vu,mi(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=>gi(this.id,`binding release failed: ${n instanceof Error?n.message:String(n)}`));try{this.ws.removeAllListeners("message")}catch{}this.onClosed?.(this),gi(this.id,e)}}},or=[];function gi(t,e){or.push({id:t,reason:e}),or.length>256&&or.shift()}var kt="/v1/realtime",Ru=t=>t.slice(0,120);function Su(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
+ Connection: close\r
24
+ Content-Type: text/plain\r
25
+ \r
26
+ ${Ru(n)}\r
27
+ `),t.destroy()}function tn(t,e){let n=new yi.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,i,s)=>{if(new URL(o.url??"/","http://localhost").pathname!==kt)return;r.add(i),i.on("close",()=>{r.delete(i)});let u=l=>{let c=l,y=c.session!=null&&typeof c.session.sendResponseCreate=="function",g=c.clients!=null&&typeof c.clients.createResponse=="function";if(!y&&!g)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((E,w)=>{n.handleUpgrade(o,i,s,h=>{try{new bt({ws:h,binding:l}),E()}catch(b){w(b)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o).then(u).catch(l=>{let c=l instanceof hi.SessionFailedError?Yt(l):null,y=null;if(l!==null&&typeof l=="object"&&"status"in l){let w=l.status;typeof w=="number"&&(y=w)}let g=y??c?.status??502,E=l instanceof Error?l.message:String(l);i.destroyed||Su(i,g,E)}).finally(()=>{r.delete(i)})}),async()=>{let o=Date.now()+5e3;for(;r.size>0&&Date.now()<o;)await new Promise(i=>setTimeout(i,25));await new Promise(i=>n.close(()=>i()))}}_();_();var Tu=require("http");_();var _i="/v1/images/generations",nn="/v1/images/edits";function wi(t){return t===_i||t===nn}var vi="/v1/images/models";async function bi(t,e,n){if(t){if(t.model=Pe({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 V=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},Eu=16*1024*1024,ki=64*1024*1024;function xi(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 xt(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 $e=class extends Error{};async function Au(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new $e(`request body exceeds the ${e}-byte limit`);let r=[],o=0;for await(let i of t){let s=i;if(o+=s.length,o>e)throw new $e(`request body exceeds the ${e}-byte limit`);r.push(s)}return Buffer.concat(r)}async function Ri(t,e,n,r){let o=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},r),i=null;for await(let c of o){if(c.type==="error"){let y=c.error??{};throw new V(502,`image upstream error: ${String(y.message??"upstream reported an error")}`)}if(c.type==="response.completed"){i=c.response??null;break}}if(i===null)throw new V(502,"image upstream produced no terminal response \u2014 refusing to fabricate an image");let s=i.images;if(!Array.isArray(s)||s.length!==1||typeof s[0]!="string"||s[0].length===0)throw new V(502,`image upstream returned ${Array.isArray(s)?s.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=s[0].startsWith("data:")?s[0].slice(s[0].indexOf(";base64,")+8):s[0],d=Buffer.from(u,"base64");if(xi(d)===null)throw new V(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 Si(t){return t instanceof V?t:t instanceof re?new V(404,t.message):new V(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function Ei(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 xt(r,new V(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 Ai(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 Ou={model:!0,prompt:!0,response_format:!0,n:!0};async function Ii(t,e,n){let r=n.maxBodyBytes??ki;try{if(t.method!=="POST")throw new V(405,`${String(t.method)} is not supported on ${_i}`);let o;try{o=JSON.parse((await Au(t,r)).toString("utf8")||"{}")}catch(c){throw c instanceof $e?new V(413,c.message):new V(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new V(400,"request body must be a JSON object");let i=o;for(let c of Object.keys(i))if(!Ou[c])throw new V(400,`request field "${c}" 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.`,c);let s=i.prompt;if(typeof s!="string"||s.trim().length===0)throw new V(400,"prompt is required and must be a non-empty string","prompt");let a=i.model===void 0?void 0:i.model;if(a!==void 0&&typeof a!="string")throw new V(400,"model must be a string","model");await bi(n.turn,n.clients,a);let u=i.n??1;if(u!==1)throw new V(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=i.response_format??"b64_json";if(d!=="b64_json")throw new V(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",s.length,s.length),!await Ei(n.imageModels,a,"generation",e))return;let l=[{type:"input_text",text:s}];try{let c=await Ri(n.clients,a,l,n.turn?Te(n.turn):void 0);Ai(n.turn,c),Oi(e,c)}catch(c){throw Si(c)}}catch(o){if(o instanceof $e){xt(e,new V(413,o.message));return}if(o instanceof V){xt(e,o);return}throw o}}var Iu={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function Ti(t,e,n){let r=n.maxBodyBytes??ki,o=n.maxImagePartBytes??Eu;try{if(t.method!=="POST")throw new V(405,`${String(t.method)} is not supported on ${nn}`);let i=String(t.headers["content-type"]??"");if(!i.toLowerCase().startsWith("multipart/form-data"))throw new V(400,`the edit endpoint consumes multipart/form-data, got ${i||"none"}`);let s=t[Symbol.asyncIterator](),a=0,u=new ReadableStream({async pull(h){try{let b=await s.next();if(b.done){h.close();return}if(a+=b.value.length,a>r){h.error(new $e(`request body exceeds the ${r}-byte limit`));return}h.enqueue(b.value)}catch(b){h.error(b)}},cancel(){s.return?.()}}),d;try{let h=new Headers;for(let[T,B]of Object.entries(t.headers))if(typeof B=="string")h.set(T,B);else if(Array.isArray(B))for(let M of B)h.append(T,M);let b={method:"POST",headers:h,body:u,duplex:"half"};d=await new Request(`https://proxy.internal${t.url??"/"}`,b).formData()}catch(h){throw h instanceof $e?new V(413,h.message):new V(400,`malformed multipart body: ${String(h instanceof Error?h.message:h)}`)}let l=d.get("prompt");if(typeof l!="string"||l.trim().length===0)throw new V(400,"prompt is required and must be a non-empty string field","prompt");let c=d.get("model");if(c!==null&&typeof c!="string")throw new V(400,"model must be a string field","model");let y=c===null?void 0:c;await bi(n.turn,n.clients,y);let g=d.get("response_format")??"b64_json";if(g!=="b64_json")throw new V(400,`response_format ${JSON.stringify(g)} 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 h of d.keys())if(!Iu[h])throw new V(400,`form field "${h}" 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[]).`,h);let E=[];for(let h of["image","image[]"])for(let b of d.getAll(h)){if(!(b instanceof File))throw new V(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",h);if(b.size>o)throw new V(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,h);let R=new Uint8Array(await b.arrayBuffer()),T=xi(R);if(T===null)throw new V(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",h);E.push({bytes:R,mime:T})}if(E.length===0)throw new V(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",l.length,l.length),!await Ei(n.imageModels,y,"edit",e))return;let w=[{type:"input_text",text:l}];for(let{bytes:h,mime:b}of E)w.push({type:"input_image",image_url:`data:${b};base64,${Buffer.from(h).toString("base64")}`});try{let h=await Ri(n.clients,y,w,n.turn?Te(n.turn):void 0);Ai(n.turn,h),Oi(e,h)}catch(h){throw Si(h)}}catch(i){if(i instanceof $e){xt(e,new V(413,i.message));return}if(i instanceof V){xt(e,i);return}throw i}}var un=require("@urun-sh/core");function ar(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function me(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function Me(t,e,n,r="invalid_request_error",o){me(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function $i(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);me(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function Ee(t,e,n,r){me(t,e,{type:"error",error:{type:n,message:r}})}function rn(t,e,n){if(e==="anthropic"){me(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}me(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var Pu=64*1024*1024,Rt=class extends Error{};async function Pi(t,e=Pu){let n=[],r=0;for await(let i of t){if(r+=i.length,r>e)throw new Rt(`request body exceeds ${e} bytes`);n.push(i)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function rt(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 Mi(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),i=r(n.output_tokens),s=u=>u??0,a=r(n.total_tokens)??s(o)+s(i);if(s(o)>0||s(i)>0){let u={input_tokens:s(o),output_tokens:s(i),total_tokens:a};return o===null||i===null?{kind:"partial",usage:u}:{kind:"counts",usage:u}}return o===null||i===null?{kind:"absent"}:{kind:"zero",usage:{input_tokens:0,output_tokens:0,total_tokens:a}}}function on(t){let e=Mi(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function Ci(t){let e=on(t);return e?{prompt_tokens:e.input_tokens,completion_tokens:e.output_tokens,total_tokens:e.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function Cu(t,e){let n=Mi(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function $u(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"),i=r("prefill_ms"),s=r("decode_ms"),a=r("total_ms");return o===null||i===null||s===null||a===null?null:{queue_ms:o,prefill_ms:i,decode_ms:s,total_ms:a}}function Mu(t,e){let n=$u(e);n&&(t.timing=n)}function ot(t,e){Cu(t,e),Mu(t,e)}async function ur(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 Ui(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 Uu(t){if(Array.isArray(t))return t.map(e=>{let n=e.function;return!n||typeof n!="object"?e:{type:"function",name:n.name,description:n.description,parameters:n.parameters,strict:n.strict}})}var sn=class extends Error{};function Lu(t,e){return Array.isArray(e)?e.map(n=>{let r=n;if(r.type==="text")return{type:t==="assistant"?"output_text":"input_text",text:r.text};if(r.type==="image_url"){let o=r.image_url??{};return{type:"input_image",image_url:o.url,detail:o.detail}}throw new sn(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function Nu(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:Lu(n.role,n.content)})}return e}function ir(t){return et(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 Du(t,e,n,r,o,i){let s=ht(i.id),a=t.stream===!0;if(i.model=Pe(t),i.stream=a,await ur(i,e,typeof t.model=="string"?t.model:void 0),a){let b;try{b=await _t({body:t,clients:e,store:n,stats:o,responseId:s,turn:i})}catch(R){if(R instanceof re){rn(r,"openai",R);return}if(R instanceof _e){Me(r,R.status,R.message);return}throw R}ar(r);for await(let R of b.events)R.type==="error"&&(i.streamError=!0),R.type==="response.completed"&&ot(i,R.response),r.write(`event: ${String(R.type??"message")}
28
+ data: ${JSON.stringify(R)}
15
29
 
16
30
  `);r.write(`data: [DONE]
17
31
 
18
- `),r.end();return}let i;try{i=Ne(t,n)}catch(A){if(A instanceof ue){de(r,A.status,A.message);return}throw A}n.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(i))-Buffer.byteLength(JSON.stringify(n.input)));let a=`resp_${Math.random().toString(36).slice(2,14)}`,c=n.store!==!1,d=Te(n);o.model(d);let h=Array.isArray(n.tools)&&n.tools.length>0,m;try{m=await At(e,n,i,!1)}catch(A){if(A instanceof z){et(r,"openai",A);return}if(A instanceof ue){de(r,A.status,A.message);return}throw A}let f=[],g=null;for await(let A of o.track(m,d)){if(f.push(A),A.type==="error"){de(r,502,JSON.stringify(A),"upstream_error");return}A.type==="response.completed"&&(g=A.response)}if(g==null){de(r,502,"upstream produced no response.completed event","upstream_error");return}let S=Et(f,g),I=Ae(h,Ue(g).length,S);if(I){de(r,502,Oe(d,I),"upstream_error");return}c&&t.remember(a,i,Ot(g,S),c),pe(r,200,{...g,id:a})}async function xo(n,e,t,r){let o=n.messages;if(!Array.isArray(o)){de(t,400,"chat/completions requires a messages array");return}let s=String(n.model??"urun"),i=`chatcmpl-${Math.random().toString(36).slice(2,14)}`,a;try{a=ko(o)}catch(k){if(k instanceof rt){de(t,400,k.message);return}throw k}let c=Te(n);r.model(c);let d=Array.isArray(n.tools)&&n.tools.length>0,h;try{h=await e.createResponse({model:n.model,input:a,stream:!0,tools:vo(n.tools),tool_choice:n.tool_choice,temperature:n.temperature,top_p:n.top_p,stop:n.stop,max_output_tokens:n.max_completion_tokens??n.max_tokens,...n.reasoning_effort!==void 0?{reasoning_effort:n.reasoning_effort}:{},...n.chat_template_kwargs!==void 0?{chat_template_kwargs:n.chat_template_kwargs}:{}})}catch(k){if(k instanceof z){et(t,"openai",k);return}throw k}if(n.stream===!0){$t(t);let k=[],M=new we,ne="";t.write(`data: ${JSON.stringify($e(i,s,{role:"assistant"},null))}
32
+ `),r.end();return}let u;try{u=nt(n,t)}catch(b){if(b instanceof _e){Me(r,b.status,b.message);return}throw b}t.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(u))-Buffer.byteLength(JSON.stringify(t.input)));let d=t.store!==!1,l=Pe(t);o.model(l);let c=Array.isArray(t.tools)&&t.tools.length>0,y;try{y=await Kn(e,t,u,!1,Te(i))}catch(b){if(b instanceof re){rn(r,"openai",b);return}if(b instanceof _e){Me(r,b.status,b.message);return}throw b}let g=[],E=null;for await(let b of o.track(y,l,i)){if(g.push(b),b.type==="error"){$i(r,b);return}b.type==="response.completed"&&(E=b.response)}if(E==null){Me(r,502,"upstream produced no response.completed event","upstream_error");return}ot(i,E);let w=Wn(g,E),h=Je(c,et(E).length,w);if(h){Me(r,502,We(l,h),"upstream_error");return}d&&n.remember(s,u,Jn(E,w),d),me(r,200,{...E,id:s})}async function Fu(t,e,n,r,o){let i=t.messages;if(!Array.isArray(i)){Me(n,400,"chat/completions requires a messages array");return}let s=String(t.model??"urun"),a=ri(o.id),u;try{u=Nu(i)}catch(k){if(k instanceof sn){Me(n,400,k.message);return}throw k}let d=Pe(t);r.model(d),o.model=d,o.stream=t.stream===!0,await ur(o,e,typeof t.model=="string"?t.model:void 0);let l=Array.isArray(t.tools)&&t.tools.length>0,c;try{c=await e.createResponse({model:t.model,input:u,stream:!0,tools:Uu(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 re){rn(n,"openai",k);return}throw k}if(t.stream===!0){ar(n);let k=[],H=new De,W="";n.write(`data: ${JSON.stringify(rt(a,s,{role:"assistant"},null))}
19
33
 
20
- `);for await(let q of r.track(h,c)){k.push(q);let J=Me(q);if(q.type==="response.output_text.delta"&&typeof q.delta=="string")ne+=q.delta,t.write(`data: ${JSON.stringify($e(i,s,{content:q.delta},null))}
34
+ `);for await(let ne of r.track(c,d,o)){k.push(ne);let N=tt(ne);if(ne.type==="response.output_text.delta"&&typeof ne.delta=="string")W+=ne.delta,n.write(`data: ${JSON.stringify(rt(a,s,{content:ne.delta},null))}
21
35
 
22
- `);else if(q.type==="response.reasoning_text.delta"&&typeof q.delta=="string")t.write(`data: ${JSON.stringify($e(i,s,{reasoning_content:q.delta},null))}
36
+ `);else if(ne.type==="response.reasoning_text.delta"&&typeof ne.delta=="string")n.write(`data: ${JSON.stringify(rt(a,s,{reasoning_content:ne.delta},null))}
23
37
 
24
- `);else if(J){let{call:T,isNew:N}=M.add(J),H=N?{index:J.tool_index,id:T.id??`call_${J.tool_index}`,type:"function",function:{name:T.name??"",arguments:J.delta}}:{index:J.tool_index,function:{arguments:J.delta}};t.write(`data: ${JSON.stringify($e(i,s,{tool_calls:[H]},null))}
38
+ `);else if(N){let{call:J,isNew:X}=H.add(N),ue=X?{index:N.tool_index,id:J.id??`call_${N.tool_index}`,type:"function",function:{name:J.name??"",arguments:N.delta}}:{index:N.tool_index,function:{arguments:N.delta}};n.write(`data: ${JSON.stringify(rt(a,s,{tool_calls:[ue]},null))}
25
39
 
26
- `)}else if(q.type==="error"){t.write(`data: ${JSON.stringify({error:q})}
40
+ `)}else if(ne.type==="error"){o.streamError=!0,n.write(`data: ${JSON.stringify({error:ne})}
27
41
 
28
- `),t.end();return}}let Z=Mt(ve(k)),re=Ae(d,M.size+Z.length,ne);if(re){t.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:Oe(c,re)}})}
42
+ `),n.end();return}}let z=ir(Ce(k)),j=Je(l,H.size+z.length,W);if(j){o.streamError=!0,n.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:We(d,j)}})}
29
43
 
30
- `),t.end();return}M.size===0&&Z.length>0&&t.write(`data: ${JSON.stringify($e(i,s,{tool_calls:Z},null))}
44
+ `),n.end();return}H.size===0&&z.length>0&&n.write(`data: ${JSON.stringify(rt(a,s,{tool_calls:z},null))}
31
45
 
32
- `);let B=M.size>0||Z.length>0;t.write(`data: ${JSON.stringify($e(i,s,{},B?"tool_calls":"stop"))}
46
+ `);let ge=H.size>0||z.length>0;n.write(`data: ${JSON.stringify(rt(a,s,{},ge?"tool_calls":"stop"))}
33
47
 
34
- `),nt(ve(k))&&t.write(`data: ${JSON.stringify({id:i,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:s,choices:[],usage:Bn(ve(k))})}
48
+ `);let ce=on(Ce(k));ot(o,Ce(k)),ce&&n.write(`data: ${JSON.stringify({id:a,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:s,choices:[],usage:Ci(Ce(k))})}
35
49
 
36
- `),t.write(`data: [DONE]
50
+ `),n.write(`data: [DONE]
37
51
 
38
- `),t.end();return}let m="",f="",g=[],S=new we;for await(let k of r.track(h,c)){g.push(k),k.type==="response.output_text.delta"&&typeof k.delta=="string"&&(m+=k.delta),k.type==="response.reasoning_text.delta"&&typeof k.delta=="string"&&(f+=k.delta);let M=Me(k);if(M&&S.add(M),k.type==="error"){de(t,502,JSON.stringify(k),"upstream_error");return}}let I=ve(g),A=Mt(I),$=A.length>0?A:Mt({output:S.items()}),K=Ae(d,$.length,m);if(K){de(t,502,Oe(c,K),"upstream_error");return}let X=$.map(({index:k,...M})=>M),Q={role:"assistant",content:m||null},se=f||jn(I);se&&(Q.reasoning_content=se),X.length>0&&(Q.tool_calls=X),pe(t,200,{id:i,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:s,choices:[{index:0,message:Q,finish_reason:X.length>0?"tool_calls":"stop"}],usage:Bn(I)})}function So(n){return typeof n=="string"?n:Array.isArray(n)?n.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var Ge=class extends Error{};function ot(n){return new Ge(`unsupported anthropic content block type "${n}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function Ro(n){if(Array.isArray(n))return n.map(e=>{let t=e;return{type:"function",name:t.name,description:t.description,parameters:t.input_schema}})}function Eo(n){if(n==null)return;let e=n.type;if(e==="auto")return"auto";if(e==="none")return"none";if(e==="any")return"required";if(e==="tool")return{type:"function",name:n.name};throw new Ge(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function Oo(n){if(typeof n=="string")return n;if(n==null)return"";if(!Array.isArray(n))throw ot(typeof n);let e="";for(let t of n){let r=String(t.type??"");if(r!=="text")throw ot(`tool_result > ${r}`);e+=String(t.text??"")}return e}function Ao(n){let{role:e,content:t}=n;if(typeof t=="string")return[{role:e,content:t}];if(t==null)return[];if(!Array.isArray(t))throw ot(typeof t);let r=[],o="",s=()=>{o&&(r.push({role:e,content:o}),o="")};for(let i of t){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:Oo(i.content)});else{if(a==="thinking"||a==="redacted_thinking")continue;throw ot(a)}}return s(),r}function Nt(n){return Ue(n).map((e,t)=>{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_${t}`,name:e.name??"",input:r}})}async function Io(n,e,t,r){let o=n.messages;if(!Array.isArray(o)){me(t,400,"invalid_request_error","messages requires a messages array");return}if(typeof n.max_tokens!="number"||!Number.isInteger(n.max_tokens)||n.max_tokens<1){me(t,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(n.top_k!==void 0){me(t,400,"invalid_request_error","top_k is not supported: the uRun serve lane exposes temperature/top_p/stop_sequences only");return}if(n.stop_sequences!==void 0&&(!Array.isArray(n.stop_sequences)||n.stop_sequences.some(k=>typeof k!="string"))){me(t,400,"invalid_request_error","stop_sequences must be an array of strings");return}let s=String(n.model??"urun"),i=`msg_${Math.random().toString(36).slice(2,14)}`,a=[],c;try{n.system&&a.push({role:"system",content:So(n.system)});for(let k of o)a.push(...Ao(k));c=Eo(n.tool_choice)}catch(k){if(k instanceof Ge){me(t,400,"invalid_request_error",k.message);return}throw k}let d=Te(n);r.model(d);let h=Array.isArray(n.tools)&&n.tools.length>0,m;try{m=await e.createResponse({model:n.model,input:a,stream:!0,tools:Ro(n.tools),tool_choice:c,temperature:n.temperature,max_output_tokens:n.max_tokens,top_p:n.top_p,stop:n.stop_sequences})}catch(k){if(k instanceof z){et(t,"anthropic",k);return}throw k}let f=(k,M)=>{t.destroyed||t.write(`event: ${k}
39
- data: ${JSON.stringify({type:k,...M})}
52
+ `),n.end();return}let y="",g="",E=[],w=new De;for await(let k of r.track(c,d,o)){E.push(k),k.type==="response.output_text.delta"&&typeof k.delta=="string"&&(y+=k.delta),k.type==="response.reasoning_text.delta"&&typeof k.delta=="string"&&(g+=k.delta);let H=tt(k);if(H&&w.add(H),k.type==="error"){$i(n,k);return}}let h=Ce(E);ot(o,h);let b=ir(h),R=b.length>0?b:ir({output:w.items()}),T=Je(l,R.length,y);if(T){Me(n,502,We(d,T),"upstream_error");return}let B=R.map(({index:k,...H})=>H),M={role:"assistant",content:y||null},$=g||Ui(h);$&&(M.reasoning_content=$),B.length>0&&(M.tool_calls=B),me(n,200,{id:a,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:s,choices:[{index:0,message:M,finish_reason:B.length>0?"tool_calls":"stop"}],usage:Ci(h)})}function Bu(t){return typeof t=="string"?t:Array.isArray(t)?t.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var St=class extends Error{};function an(t){return new St(`unsupported anthropic content block type "${t}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function ju(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 qu(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 St(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function Hu(t){if(typeof t=="string")return t;if(t==null)return"";if(!Array.isArray(t))throw an(typeof t);let e="";for(let n of t){let r=String(n.type??"");if(r!=="text")throw an(`tool_result > ${r}`);e+=String(n.text??"")}return e}function Gu(t){let{role:e,content:n}=t;if(typeof n=="string")return[{role:e,content:n}];if(n==null)return[];if(!Array.isArray(n))throw an(typeof n);let r=[],o="",i=()=>{o&&(r.push({role:e,content:o}),o="")};for(let s of n){let a=String(s.type??"");if(a==="text")o+=String(s.text??"");else if(a==="tool_use")i(),r.push({type:"function_call",call_id:s.id,name:s.name,arguments:JSON.stringify(s.input??{})});else if(a==="tool_result")i(),r.push({type:"function_call_output",call_id:s.tool_use_id,output:Hu(s.content)});else{if(a==="thinking"||a==="redacted_thinking")continue;throw an(a)}}return i(),r}function sr(t){return et(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 Vu(t,e,n,r,o){let i=t.messages;if(!Array.isArray(i)){Ee(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){Ee(n,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(t.top_k!==void 0){Ee(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"))){Ee(n,400,"invalid_request_error","stop_sequences must be an array of strings");return}let s=String(t.model??"urun"),a=oi(o.id),u=[],d;try{t.system&&u.push({role:"system",content:Bu(t.system)});for(let k of i)u.push(...Gu(k));d=qu(t.tool_choice)}catch(k){if(k instanceof St){Ee(n,400,"invalid_request_error",k.message);return}throw k}let l=Pe(t);r.model(l),o.model=l,o.stream=t.stream===!0,await ur(o,e,typeof t.model=="string"?t.model:void 0);let c=Array.isArray(t.tools)&&t.tools.length>0,y;try{y=await e.createResponse({model:t.model,input:u,stream:!0,tools:ju(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 re){rn(n,"anthropic",k);return}throw k}let g=(k,H)=>{n.destroyed||n.write(`event: ${k}
53
+ data: ${JSON.stringify({type:k,...H})}
40
54
 
41
- `)};if(n.stream===!0){let k=r.track(m,d);$t(t),f("message_start",{message:{id:i,type:"message",role:"assistant",content:[],model:s,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let M=0,ne="",Z=0,re="none",B=()=>re,ie=-1,q=-1,J=new we,T=[],N=()=>{re!=="none"&&(re="none",f("content_block_stop",{index:ie}))},H=(L,U)=>{N(),re=L,ie=Z++,f("content_block_start",{index:ie,content_block:U})};for await(let L of k){if(t.destroyed)break;T.push(L);let U=Me(L);if(L.type==="response.reasoning_text.delta"&&typeof L.delta=="string")B()!=="thinking"&&H("thinking",{type:"thinking",thinking:""}),f("content_block_delta",{index:ie,delta:{type:"thinking_delta",thinking:L.delta}});else if(L.type==="response.output_text.delta"&&typeof L.delta=="string")B()!=="text"&&H("text",{type:"text",text:""}),M+=1,ne+=L.delta,f("content_block_delta",{index:ie,delta:{type:"text_delta",text:L.delta}});else if(U){let{call:qe}=J.add(U);(B()!=="tool"||U.tool_index!==q)&&(q=U.tool_index,H("tool",{type:"tool_use",id:qe.id??`call_${U.tool_index}`,name:qe.name??"",input:{}})),U.delta&&f("content_block_delta",{index:ie,delta:{type:"input_json_delta",partial_json:U.delta}})}else if(L.type==="error"){f("error",{error:{type:"api_error",message:JSON.stringify(L)}}),t.end();return}}if(t.destroyed)return;let ae=ve(T),be=Ue(ae),ce=Ae(h,J.size+be.length,ne);if(ce){f("error",{error:{type:"api_error",message:Oe(d,ce)}}),t.end();return}N();let oe=[];if(J.size===0)try{oe=Nt(ae)}catch(L){f("error",{error:{type:"api_error",message:String(L instanceof Error?L.message:L)}}),t.end();return}Z===0&&oe.length===0&&(H("text",{type:"text",text:""}),N()),oe.forEach(L=>{let U=Z++;f("content_block_start",{index:U,content_block:{...L,input:{}}}),f("content_block_delta",{index:U,delta:{type:"input_json_delta",partial_json:JSON.stringify(L.input??{})}}),f("content_block_stop",{index:U})});let E=J.size>0||oe.length>0,ee=nt(ae);f("message_delta",{delta:{stop_reason:E?"tool_use":"end_turn"},usage:ee?{input_tokens:ee.input_tokens,output_tokens:ee.output_tokens}:{output_tokens:M}}),f("message_stop",{}),t.end();return}let g="",S="",I=[],A=new we;for await(let k of r.track(m,d)){I.push(k),k.type==="response.output_text.delta"&&typeof k.delta=="string"&&(g+=k.delta),k.type==="response.reasoning_text.delta"&&typeof k.delta=="string"&&(S+=k.delta);let M=Me(k);if(M&&A.add(M),k.type==="error"){me(t,502,"api_error",JSON.stringify(k));return}if(t.destroyed)break}if(t.destroyed)return;let $=ve(I),K;try{K=Nt($),K.length===0&&A.size>0&&(K=Nt({output:A.items()}))}catch(k){me(t,502,"api_error",String(k instanceof Error?k.message:k));return}let X=Ae(h,K.length,g);if(X){me(t,502,"api_error",Oe(d,X));return}let Q=[],se=S||jn($);se&&Q.push({type:"thinking",thinking:se}),(g||K.length===0)&&Q.push({type:"text",text:g}),Q.push(...K),pe(t,200,{id:i,type:"message",role:"assistant",content:Q,model:s,stop_reason:K.length>0?"tool_use":"end_turn",usage:nt($)??{input_tokens:0,output_tokens:0}})}function Gn(n){let{clients:e,apiKey:t,identity:r,statusOf:o}=n,s=typeof e=="function"?e:()=>e,i=new Re;return(a,c)=>{let d=new URL(a.url??"/","http://localhost"),h=d.pathname==="/v1/messages"||d.pathname.startsWith("/v1/messages/"),m=(f,g,S,I)=>h?me(c,f,I,g):de(c,f,g,S);(async()=>{if(a.method==="GET"&&d.pathname==="/healthz"){pe(c,200,{ok:!0});return}if(a.method==="GET"&&d.pathname==="/stats"){pe(c,200,r?{identity:r,...i.snapshot()}:i.snapshot());return}if(t&&(a.headers.authorization??"")!==`Bearer ${t}`){m(401,"invalid local proxy api key","authentication_error","authentication_error");return}let f=await s(a);if(a.method==="GET"&&d.pathname==="/v1/models"){if(d.searchParams.get("format")==="openrouter"){if(!f.openRouterModels){m(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}pe(c,200,await f.openRouterModels());return}pe(c,200,await f.listModels());return}if(a.method!=="POST"){m(404,`no route for ${a.method} ${d.pathname}`,"invalid_request_error","not_found_error");return}let g;try{g=await wo(a)}catch(I){if(I instanceof tt){m(413,I.message,"request_too_large","request_too_large");return}m(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let S=Buffer.byteLength(JSON.stringify(g));if(d.pathname==="/v1/responses"){i.request("responses",S,S),await bo(g,f,Be(f),c,i);return}if(d.pathname==="/v1/chat/completions"){i.request("chat_completions",S,S),await xo(g,f,c,i);return}if(d.pathname==="/v1/messages"){i.request("messages",S,S),await Io(g,f,c,i);return}if(d.pathname==="/v1/messages/count_tokens"){i.request("count_tokens",S,S),me(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 ${d.pathname}`,"invalid_request_error","not_found_error")})().catch(f=>{let g=String(f instanceof Error?f.message:f);if(!c.headersSent){let S=o?.(f)??null;if(S){m(S.status,g,S.openaiType,S.anthropicType);return}m(500,g,"proxy_error","api_error");return}c.destroyed||(h?c.write(`event: error
42
- data: ${JSON.stringify({type:"error",error:{type:"api_error",message:g}})}
55
+ `)};if(t.stream===!0){let k=r.track(y,l,o);ar(n),g("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:s,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let H=0,W="",z=0,j="none",ge=()=>j,ce=-1,ne=-1,N=new De,J=[],X=()=>{j!=="none"&&(j="none",g("content_block_stop",{index:ce}))},ue=(D,de)=>{X(),j=D,ce=z++,g("content_block_start",{index:ce,content_block:de})};for await(let D of k){if(n.destroyed)break;J.push(D);let de=tt(D);if(D.type==="response.reasoning_text.delta"&&typeof D.delta=="string")ge()!=="thinking"&&ue("thinking",{type:"thinking",thinking:""}),g("content_block_delta",{index:ce,delta:{type:"thinking_delta",thinking:D.delta}});else if(D.type==="response.output_text.delta"&&typeof D.delta=="string")ge()!=="text"&&ue("text",{type:"text",text:""}),H+=1,W+=D.delta,g("content_block_delta",{index:ce,delta:{type:"text_delta",text:D.delta}});else if(de){let{call:Ot}=N.add(de);(ge()!=="tool"||de.tool_index!==ne)&&(ne=de.tool_index,ue("tool",{type:"tool_use",id:Ot.id??`call_${de.tool_index}`,name:Ot.name??"",input:{}})),de.delta&&g("content_block_delta",{index:ce,delta:{type:"input_json_delta",partial_json:de.delta}})}else if(D.type==="error"){o.streamError=!0,g("error",{error:{type:"api_error",message:JSON.stringify(D)}}),n.end();return}}if(n.destroyed)return;let xe=Ce(J),Re=et(xe),ve=Je(c,N.size+Re.length,W);if(ve){o.streamError=!0,g("error",{error:{type:"api_error",message:We(l,ve)}}),n.end();return}X();let P=[];if(N.size===0)try{P=sr(xe)}catch(D){o.streamError=!0,g("error",{error:{type:"api_error",message:String(D instanceof Error?D.message:D)}}),n.end();return}z===0&&P.length===0&&(ue("text",{type:"text",text:""}),X()),P.forEach(D=>{let de=z++;g("content_block_start",{index:de,content_block:{...D,input:{}}}),g("content_block_delta",{index:de,delta:{type:"input_json_delta",partial_json:JSON.stringify(D.input??{})}}),g("content_block_stop",{index:de})});let he=N.size>0||P.length>0,Ue=on(xe);ot(o,xe),g("message_delta",{delta:{stop_reason:he?"tool_use":"end_turn"},usage:Ue?{input_tokens:Ue.input_tokens,output_tokens:Ue.output_tokens}:{output_tokens:H}}),g("message_stop",{}),n.end();return}let E="",w="",h=[],b=new De;for await(let k of r.track(y,l,o)){h.push(k),k.type==="response.output_text.delta"&&typeof k.delta=="string"&&(E+=k.delta),k.type==="response.reasoning_text.delta"&&typeof k.delta=="string"&&(w+=k.delta);let H=tt(k);if(H&&b.add(H),k.type==="error"){let W=k.error,z=W?.type==="invalid_request_error";Ee(n,z?400:502,z?"invalid_request_error":"api_error",W?.message??JSON.stringify(k));return}if(n.destroyed)break}if(n.destroyed)return;let R=Ce(h);ot(o,R);let T;try{T=sr(R),T.length===0&&b.size>0&&(T=sr({output:b.items()}))}catch(k){Ee(n,502,"api_error",String(k instanceof Error?k.message:k));return}let B=Je(c,T.length,E);if(B){Ee(n,502,"api_error",We(l,B));return}let M=[],$=w||Ui(R);$&&M.push({type:"thinking",thinking:$}),(E||T.length===0)&&M.push({type:"text",text:E}),M.push(...T),me(n,200,{id:a,type:"message",role:"assistant",content:M,model:s,stop_reason:T.length>0?"tool_use":"end_turn",usage:on(R)??{input_tokens:0,output_tokens:0}})}function Wu(t){Le(process.stdout,t)}function Li(t){let{clients:e,apiKey:n,identity:r,statusOf:o}=t,i=typeof e=="function"?e:()=>e,s=t.requestLog??Wu,a=t.observe,u=new Ge;return(d,l)=>{let c=new URL(d.url??"/","http://localhost"),y=c.pathname==="/v1/messages"||c.pathname.startsWith("/v1/messages/"),g=(R,T,B,M,$)=>y?Ee(l,R,M,T):Me(l,R,T,B,$),E=Vn(c.pathname),w=c.pathname.startsWith("/partners/"),h=E||w?{id:gt(),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;h&&(l.setHeader(ti,h.id),l.once("close",()=>{let R=ii({turn:h,method:d.method??"",path:c.pathname,res:l});s(si(R)),a?.(R),co({record:R,ledger:t.ledger??null,req:d})}));let b=()=>ni(c.pathname,h);(async()=>{if(d.method==="GET"&&c.pathname==="/healthz"){me(l,200,{ok:!0});return}if(d.method==="GET"&&c.pathname==="/stats"){me(l,200,r?{identity:r,...u.snapshot()}:u.snapshot());return}if(n&&(d.headers.authorization??"")!==`Bearer ${n}`){g(401,"invalid local proxy api key","authentication_error","authentication_error");return}let R=c.pathname===Nt?"canonical":c.pathname===pt?"huggingface":null;if(R){if(d.method!=="POST"){g(405,`${c.pathname} is a POST batch lookup`,"invalid_request_error","invalid_request_error");return}let $=t.usage;if(!$){g(501,`${c.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=$.authorize(d),H;try{H=await Pi(d,ho)}catch(j){if(j instanceof Rt){g(413,j.message,"request_too_large","request_too_large");return}g(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let W=j=>$.lookup(k,j),z;try{if(R==="canonical"){let j=yo(H),ge=await W(j);Dt(ge,j),z={requests:ge}}else z=await vo(H,W)}catch(j){if(j instanceof Ne){g(j.status,j.message,"invalid_request_error","invalid_request_error");return}throw j}me(l,200,z);return}if(c.pathname.startsWith("/partners/")){g(404,`no partner adapter for ${d.method} ${c.pathname} \u2014 the partner surface currently carries ${pt}`,"invalid_request_error","not_found_error");return}let T=await i(d);if(h&&(h.org=T.tenant??null),d.method==="GET"&&c.pathname==="/v1/models"){if(c.searchParams.get("format")==="openrouter"){if(!T.openRouterModels){g(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}me(l,200,await T.openRouterModels());return}me(l,200,await T.listModels());return}if(d.method==="GET"&&c.pathname===vi){if(!T.listImageModels){g(501,"GET /v1/images/models needs a router-backed client (listImageModels)","not_implemented","not_implemented_error");return}me(l,200,await T.listImageModels());return}if(wi(c.pathname)){if(!T.supportsImages){g(501,`the image lanes need a router-backed client (supportsImages) to gate model capability for ${c.pathname}`,"not_implemented","not_implemented_error");return}let $={clients:T,imageModels:T.supportsImages,stats:u,turn:b()};c.pathname===nn?await Ti(d,l,$):await Ii(d,l,$);return}if(d.method!=="POST"){g(404,`no route for ${d.method} ${c.pathname}`,"invalid_request_error","not_found_error");return}let B;try{B=await Pi(d)}catch($){if($ instanceof Rt){g(413,$.message,"request_too_large","request_too_large");return}g(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let M=Buffer.byteLength(JSON.stringify(B));if(c.pathname==="/v1/responses"){u.request("responses",M,M),await Du(B,T,yt(T),l,u,b());return}if(c.pathname==="/v1/chat/completions"){u.request("chat_completions",M,M),await Fu(B,T,l,u,b());return}if(c.pathname==="/v1/messages"){u.request("messages",M,M),await Vu(B,T,l,u,b());return}if(c.pathname==="/v1/messages/count_tokens"){u.request("count_tokens",M,M),Ee(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}g(404,`no route for POST ${c.pathname}`,"invalid_request_error","not_found_error")})().catch(R=>{let T=String(R instanceof Error?R.message:R);if(!l.headersSent){let B=o?.(R)??null;if(B){g(B.status,T,B.openaiType,B.anthropicType);return}if(R instanceof un.SessionAllocationError){let M=zt(R.code);if(M){let $=R.retryAfterSeconds??(M.status===429?30:void 0);$!==void 0&&l.setHeader("Retry-After",String($)),g(M.status,T,M.openaiType,M.anthropicType,M.code);return}}if(R instanceof un.SessionFailedError){let M=Yt(R);if(M){let $=M.status===429?30:void 0;$!==void 0&&l.setHeader("Retry-After",String($)),g(M.status,T,M.openaiType,M.anthropicType,M.code);return}}if(R instanceof Q){g(502,T,"upstream_error","api_error");return}g(500,T,"proxy_error","api_error","proxy_error");return}h&&(h.streamError=!0),l.destroyed||(y?l.write(`event: error
56
+ data: ${JSON.stringify({type:"error",error:{type:"api_error",message:T}})}
43
57
 
44
- `):c.write(`data: ${JSON.stringify({error:{message:g,type:"upstream_error"}})}
58
+ `):l.write(`data: ${JSON.stringify({error:{message:T,type:"upstream_error"}})}
45
59
 
46
- `)),c.end()})}}O();var it=require("@urun-sh/core"),ke=class extends Error{constructor(t,r=401){super(t);this.status=r;this.name="ProxyAuthError"}status},Le=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function Vn(n){let e=(n??"").trim();if(e==="")throw new ke("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let t=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!t)throw new ke("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return t[1].trim()}var Po=60,Co=6e4,To=4096;function Uo(n){return n===401||n===403}var st=class{constructor(e){this.opts=e;this.mint=e.mint??it.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 t=this.cache.get(e);if(t&&this.now()-t.at<Co)return{apiKey:e,orgId:t.orgId};let r;try{r=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:Po})).orgId}catch(o){throw o instanceof it.ClientTokenError&&Uo(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 Le(`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 Le("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>=To){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()}};O();var pt=require("crypto");O();var Ve=require("@urun-sh/core");O();async function qn(n){let t=await(n.fetchImpl??fetch)(`${n.catalogUrl}/model_catalog?select=model_id,variant,gpu_spec,engine,task,description,size_gb,hf_repo,engine_args,updated_at`,{headers:{apikey:n.anonKey,Authorization:`Bearer ${n.anonKey}`}});if(!t.ok)throw new Error(`model_catalog fetch failed: ${t.status}`);return await t.json()}O();O();O();function Wn(n,e,t,r){let o=n.response??{},s={request_id:t,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 Jn(n,e,t){let r={request_id:e,consumer_id:t,stream:!!n.stream,kind:"responses",input:n.input};return n.model&&(r.model=n.model),n.tools!==void 0&&(r.tools=n.tools),n.tool_choice!==void 0&&(r.tool_choice=n.tool_choice),typeof n.temperature=="number"&&(r.temperature=n.temperature),typeof n.max_output_tokens=="number"&&(r.max_output_tokens=n.max_output_tokens),typeof n.top_p=="number"&&(r.top_p=n.top_p),Array.isArray(n.stop)&&n.stop.length>0&&(r.stop=n.stop),typeof n.reasoning_effort=="string"&&(r.reasoning_effort=n.reasoning_effort),n.chat_template_kwargs!==void 0&&(r.chat_template_kwargs=n.chat_template_kwargs),r}O();O();function Kn(n){if(n instanceof Error)return{type:"error",error:{type:"urun_error",code:n.name||null,message:n.message}};if(n&&typeof n=="object"&&n.t==="error"){let e=n,t=e.body??{};return{type:"error",error:{type:"urun_error",code:e.code??null,message:t.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(n)}}}var Mo="llm-resp";async function*Lt(n,e){let t=`${Mo}:${e}`,r=`resp_${e}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};let o=new Map;for await(let s of n.stream(t).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 c=typeof a.index=="number"?a.index:0,d=o.get(c)??{};a.id&&(d.id=a.id),a.function?.name&&(d.name=a.function.name),o.set(c,d),yield{type:"response.function_call_arguments.delta",item_id:`fc_${e}_${c}`,tool_index:c,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 Kn(i);return}}}var No="llm",at=class{constructor(e){this.session=e;this.sessionTag=e.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(e){e.session_tag=this.sessionTag,this.session.doc(No).set({requests:{[e.request_id]:{payload:e,consumer_id:e.consumer_id,stream:e.stream}}})}sendResponses(e,t){let r=Jn(e,t,this.consumerId);return this.write(r),Lt(this.session,t)}sendResponseCreate(e,t,r){let o=Wn(e,t,r,this.consumerId);return this.write(o),Lt(this.session,r)}};var zn=0;function $o(){return zn+=1,`req_${Date.now().toString(36)}_${zn.toString(36)}`}var ut=class{transport;constructor(e){this.transport=new at(e)}responses={create:async e=>{let t=$o(),r=this.transport.sendResponses(e,t);return Object.assign((async function*(){yield*r})(),{requestId:t})}}};O();function Lo(n){let e=n?.type;return e==="response.created"||e==="response.in_progress"}function Do(n){let e=n?.type;return e==="response.completed"||e==="error"}function Yn(n){return async function(t){return(async function*(){for(let o=1;o<=2;o++){let{app:s,entry:i}=await n.sessionFor(t.model),a=!1,c=!1;try{let d=await n.createStream(i,t);for await(let h of d){if(Do(h)&&(a=!0),Lo(h)){if(o>1)continue}else c=!0;yield h}}catch(d){if(o<2&&!c){await n.evict(s,i);continue}throw d}if(a)return;if(o<2&&!c){await n.evict(s,i);continue}return}})()}}O();var Fo=3,Ho="llm",Bo=new Set(["paused","error"]);function Xn(n,e){let t=n.doc(Ho),r=typeof t.onConnectionState=="function",o=typeof n.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=new Set,a=[],c=d=>{if(!s){s=d;for(let h of[...i])h(d);i.clear()}};return r&&a.push(t.onConnectionState(d=>{d.consecutiveFailures<Fo||c(new Y(`the pooled session for app "${e}" is gone: its doc backhaul failed ${d.consecutiveFailures} consecutive times (last close ${d.lastCloseCode===null?"by provider liveness reap":`code ${d.lastCloseCode}`}${d.lastCloseReason?`, reason ${JSON.stringify(d.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`))})),o&&a.push(n.onPhase(d=>{Bo.has(d.name)&&c(new Y(`the pooled session for app "${e}" lost its compute binding (phase "${d.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(d){return s?(d(s),()=>{}):(i.add(d),()=>i.delete(d))},dispose(){i.clear();for(let d of a.splice(0))d()}}}async function*Dt(n,e){let t=e.gone();if(t)throw t;let r=n[Symbol.asyncIterator](),o,s=new Promise((i,a)=>{o=e.onGone(c=>a(c))});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 Qn(n,e,t,r){let o=n[t];if(typeof o!="function")throw new Error(`app "${e}" has no function "${t}" (set URUN_FUNCTION)`);let s=r?o(void 0,r):o();if(typeof s.end!="function")throw new Error(`app "${e}" function "${t}" returned a session without end()`);return s}function jo(n,e){let{baseUrl:t,orgId:r,fnName:o,auth:s,subject:i}=n,a=n.sessionStartOptions?.(e),c=f=>({session:f,responses:new ut(f),gone:Xn(f,e)});if(s.lane==="jwt"){let f=(0,Ve.App)(e,{baseUrl:t,orgId:r,jwt:s.jwt});return c(Qn(f,e,o,a))}let{apiKey:d,gatewayUrl:h}=s,m=(0,Ve.App)(e,{baseUrl:t,orgId:r,getAccessToken:async()=>(await(0,Ve.createClientToken)(d,{baseUrl:h,expiresIn:300,allowedFunctions:[`${e}/${o}`],...i===void 0?{}:{subject:i}})).token});return c(Qn(m,e,o,a))}function Go(n){let e=n.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}var Vo=new Set(["error","paused"]);function Ft(n){let{auth:e,catalog:t}=n,r=null,o=i=>{let a=jo(n,i);return n.onSessionOpen&&typeof a.session.id=="string"&&a.session.id!==""&&n.onSessionOpen(i,a.session.id),a.gone.onGone(()=>{r?.evict(i,a)}),a},s=new We({defaultApp:n.defaultApp,fnName:n.fnName,openSession:o,closeSession:async i=>{if(i.gone.dispose(),n.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"?()=>un({apiUrl:n.apiUrl,apiKey:e.apiKey}):null,listCatalog:t?()=>qn(t):null,sessionKey:Go});return r=s,n.defaultApp!==null&&s.seed(n.defaultApp,o(n.defaultApp)),s}function Ht(n,e=hn){let t=new WeakMap,r=new WeakMap,o=new WeakMap;return{createResponse:Yn({sessionFor:s=>n.sessionFor(s),evict:(s,i)=>n.evict(s,i),createStream:async(s,i)=>Dt(await s.responses.responses.create(i),s.gone)}),listModels:()=>n.modelList(),openRouterModels:()=>n.openRouterModels(),openAudio:async s=>{let{entry:i}=await n.sessionFor(s),a=t.get(i);return a||(a=(async()=>fn(i.session,await e()))(),t.set(i,a),a.catch(()=>t.delete(i))),a},openVideo:async s=>{let{entry:i}=await n.sessionFor(s),a=r.get(i);return a||(a=mn(i.session),r.set(i,a)),a},openVideoOut:async s=>{let{entry:i}=await n.sessionFor(s),a=o.get(i);return a||(a=gn(i.session),o.set(i,a)),a},sessionHandle:async s=>(await n.handleFor(s)).handle,createResponseOn:async(s,i)=>{let{entry:a}=await n.sessionForHandle(s);return Dt(await a.responses.responses.create(i),a.gone)},onSessionEnd:async(s,i)=>{let{entry:a}=await n.sessionForHandle(s),c=a.session;if(typeof c.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let d=!1;return c.onPhase(h=>{if(d||!Vo.has(h.name))return;d=!0;let m=c.endsAt??null;i({timeLeftMs:m?Math.max(0,m.getTime()-Date.now()):null,reason:h.reason??`session ${h.name}`})})}}}var qo=512;function ft(n){return`urun-inference-proxy-${(0,pt.createHash)("sha256").update(n).digest("hex").slice(0,32)}`}var ct=class{constructor(e){this.opts=e}opts;tenants=new Map;build(e){if(this.opts.build)return this.opts.build(e);let t=Ft({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:vt,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:ft(e.apiKey),release:"detach"});return{router:t,clients:Ht(t)}}clientsFor(e){let t=this.tenants.get(e.apiKey);if(t)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,t),t.clients;for(;this.tenants.size>=qo;){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 t=[];if(await Promise.all(e.map(async r=>{try{await r.closeAll()}catch(o){t.push(String(o instanceof Error?o.message:o))}})),t.length>0)throw new Error(`failed to close ${t.length} tenant backhaul(s): ${t.join("; ")}`)}},Zn=512,Wo=1024,lt=class extends Error{constructor(){super(`conversation capacity reached (${Zn} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function Jo(n,e,t,r){let o=Ft({baseUrl:n.baseUrl,orgId:e.orgId,defaultApp:null,fnName:vt,apiUrl:n.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:n.baseUrl},catalog:n.catalog,subject:t.subject,release:"detach",sessionStartOptions:s=>{let i=r.get(s);return i===void 0?void 0:{sessionId:i}},onSessionOpen:(s,i)=>{r.set(s,i)}});return{clients:Ht(o),release:async()=>{await o.closeAll()}}}var dt=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,t){let r={id:t,subject:`${ft(e.apiKey)}:live:${t}`},o=new Map,s=this.opts.build?this.opts.build(e,r):Jo(this.opts,e,r,o);return{id:t,backhaul:s,attachments:0,nativeRefs:o}}attachEntry(e,t){let r=this.byCaller.get(e.apiKey);r===void 0&&(r=new Map,this.byCaller.set(e.apiKey,r));let o=r.get(t);if(o!==void 0)return r.delete(t),r.set(t,o),o.attachments+=1,o;this.evictOverflow();let s=this.build(e,t);return s.attachments=1,r.set(t,s),s}get conversationCount(){let e=0;for(let t of this.byCaller.values())e+=t.size;return e}evictOverflow(){for(;this.conversationCount>=Zn;){let e=this.byCaller.keys().next();if(e.done)throw new lt;let t=e.value,r=this.byCaller.get(t),o=!1;for(let[s,i]of r)if(!(i.attachments>0)){r.delete(s),this.dropHandleIndexFor(t,s),i.backhaul.release().catch(()=>{}),o=!0;break}if(!o)throw r.size===0&&this.byCaller.delete(t),new lt;r.size===0&&this.byCaller.delete(t)}}dropHandleIndexFor(e,t){for(let[r,o]of this.byHandle)o.apiKey===e&&o.conversationId===t&&this.byHandle.delete(r)}connectionFor(e){let t=null,r=!1,o=()=>(t===null&&(t=this.attachEntry(e,(0,pt.randomUUID)())),t),s={createResponse:i=>o().backhaul.clients.createResponse(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=>{let a=o().backhaul.clients.openAudio;if(a===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return a(i)},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)},{clients:s,bindResumeHandle:i=>{let a=t;if(a===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=Wo;){let c=this.byHandle.keys().next();if(c.done)break;this.byHandle.delete(c.value)}this.byHandle.set(i,{apiKey:e.apiKey,conversationId:a.id})},clientsForResume:async i=>{let a=this.byHandle.get(i),c=a===void 0?void 0:this.byCaller.get(a.apiKey),d=a===void 0||c===void 0?void 0:c.get(a.conversationId);if(a===void 0||d===void 0||a.apiKey!==e.apiKey)throw new Y("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 t=d,d.attachments+=1,d.backhaul.clients},release:async()=>{if(r)return;r=!0;let i=t;t=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 t=[];if(await Promise.all(e.map(async r=>{try{await r}catch(o){t.push(String(o instanceof Error?o.message:o))}})),t.length>0)throw new Error(`failed to detach ${t.length} conversation backhaul(s): ${t.join("; ")}`)}};var Ko=5e3,zo=3e3,Yo="/api/client-tokens";function Bt(n,e,t){n.writeHead(e,{"content-type":"application/json"}),n.end(JSON.stringify(t))}function er(n,e,t,r){Bt(n,e,{error:{message:t,type:r}})}async function Xo(n,e=fetch){let t=`${n.replace(/\/+$/,"")}${Yo}`;try{let r=await e(t,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(zo)});return r.status===404?{ready:!1,detail:`${t} answered 404 \u2014 this is not a uRun control plane`}:r.status>=500?{ready:!1,detail:`${t} answered HTTP ${r.status}`}:{ready:!0,detail:`${t} answered HTTP ${r.status}`}}catch(r){return{ready:!1,detail:`${t} unreachable: ${r instanceof Error?r.message:String(r)}`}}}function nr(n){let e=n.resolver??new st({gatewayUrl:n.baseUrl}),t=n.registry??new ct({baseUrl:n.baseUrl,apiUrl:n.apiUrl,catalog:n.catalog}),r=n.probeControlPlane??(()=>Xo(n.baseUrl)),o=async f=>e.resolve(Vn(f.headers.authorization)),s=f=>f instanceof ke?{status:f.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:f instanceof Le?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,i=Gn({clients:async f=>t.clientsFor(await o(f)),statusOf:s}),a=null,c=(0,tr.createServer)((f,g)=>{let S=new URL(f.url??"/","http://localhost");(async()=>{if(f.method==="GET"&&S.pathname==="/healthz"){Bt(g,200,{ok:!0});return}if(f.method==="GET"&&S.pathname==="/readyz"){let I=Date.now();if(!a||I-a.at>=Ko){let A=await r();a={at:I,...A}}Bt(g,a.ready?200:503,{ok:a.ready,control_plane:a.detail});return}if(S.pathname!=="/v1"&&!S.pathname.startsWith("/v1/")){er(g,404,`no route for ${f.method} ${S.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface, the Gemini Live WS upgrade path (/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent), plus /healthz and /readyz`,"invalid_request_error");return}i(f,g)})().catch(I=>{if(!g.headersSent){er(g,500,String(I instanceof Error?I.message:I),"proxy_error");return}g.end()})}),d=n.conversations??new dt({baseUrl:n.baseUrl,apiUrl:n.apiUrl,catalog:n.catalog}),h=[Rt(c,{authenticate:async f=>{let g=await e.resolve(f),S=d.connectionFor(g);return{clients:S.clients,scope:ft(g.apiKey),onBindResumeHandle:S.bindResumeHandle,clientsForResume:S.clientsForResume,release:S.release}}}),Ut(c,{clients:async f=>t.clientsFor(await o(f)),statusOf:s}).close];return c.on("upgrade",(f,g)=>{let S=new URL(f.url??"/","http://localhost");S.pathname===Xe||S.pathname===Ct||g.destroyed||(g.write(`HTTP/1.1 404 Not Found\r
60
+ `)),l.end()})}}var dn=(t,e)=>Object.assign(new Error(e),{status:t});function Ni(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}async function Di(t,e){if(!t.supportsAudio)throw dn(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 n;try{n=await t.supportsAudio(e)}catch(i){throw i instanceof re?dn(404,i.message):i}if(!n)throw dn(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 dn(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 r={audio:await t.openAudio(e)},o={clients:t,voice:n,audioSession:r};return e!==void 0&&(o.model=e),o}_();var cn=require("@urun-sh/core"),we=class extends Error{constructor(n,r=401){super(n);this.status=r;this.name="ProxyAuthError"}status},it=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function Et(t){let e=(t??"").trim();if(e==="")throw new we("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 we("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return n[1].trim()}var Ju=60,Ku=6e4,zu=4096;function Yu(t){return t===401||t===403}var ln=class{constructor(e){this.opts=e;this.mint=e.mint??cn.createClientToken,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new we("empty Authorization bearer token");let n=this.cache.get(e);if(n&&this.now()-n.at<Ku)return{apiKey:e,orgId:n.orgId};let r;try{r=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:Ju})).orgId}catch(o){throw o instanceof cn.ClientTokenError&&Yu(o.status)?(this.cache.delete(e),new we("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new it(`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 it("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>=zu){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()}};_();var yn=require("crypto");_();var At=require("@urun-sh/core");_();async function Fi(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()}_();_();_();function Bi(t,e,n,r){let o=t.response??{},i={request_id:n,consumer_id:r,stream:!0,kind:"chat",messages:e};return typeof o.instructions=="string"&&(i.instructions=o.instructions),Array.isArray(o.modalities)&&(i.modalities=o.modalities),typeof o.temperature=="number"&&(i.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(i.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(i.tools=o.tools),i}function ji(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),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),r}_();_();function qi(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 Xu="llm-resp";async function*dr(t,e){let n=`${Xu}:${e}`,r=`resp_${e}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};let o=new Map;for await(let i of t.stream(n).messages()){let s=i;if(s.t==="delta"){if(typeof s.delta=="string"&&(yield{type:"response.output_text.delta",item_id:r,delta:s.delta}),typeof s.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:r,delta:s.reasoning}),Array.isArray(s.tool_calls))for(let a of s.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(s.t==="response"){yield{type:"response.completed",response:{id:r,status:"completed",...s.body}};return}else if(s.t==="error"){yield qi(s);return}}}var Qu="llm",pn=class{constructor(e){this.session=e;this.sessionTag=e.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(e){e.session_tag=this.sessionTag,this.session.doc(Qu).set({requests:{[e.request_id]:{payload:e,consumer_id:e.consumer_id,stream:e.stream}}})}sendResponses(e,n){let r=ji(e,n,this.consumerId);return this.write(r),dr(this.session,n)}sendResponseCreate(e,n,r){let o=Bi(e,n,r,this.consumerId);return this.write(o),dr(this.session,r)}};var Hi=0;function Zu(){return Hi+=1,`req_${Date.now().toString(36)}_${Hi.toString(36)}`}var fn=class{transport;constructor(e){this.transport=new pn(e)}responses={create:async e=>{let n=Zu(),r=this.transport.sendResponses(e,n);return Object.assign((async function*(){yield*r})(),{requestId:n})}}};_();var Gi=require("@urun-sh/core"),lr=2;function ed(t){let e=t?.type;return e==="response.created"||e==="response.in_progress"}function td(t){let e=t?.type;return e==="response.completed"||e==="error"}function Vi(t){return async function(n,r){return(async function*(){for(let i=1;i<=lr;i++){let{app:s,entry:a,identity:u}=await t.sessionFor(n.model);r?.({sessionId:t.sessionIdOf?.(a)??null,identity:u});let d=!1,l=!1;try{let c=await t.createStream(a,n);for await(let y of c){if(td(y)&&(d=!0),ed(y)){if(i>1)continue}else l=!0;yield y}}catch(c){if(c instanceof Gi.SessionFailedError&&(c.kind==="never-live"||c.kind==="create-failed"))throw c;if(i<lr&&!l){await t.evict(s,a);continue}throw c}if(d)return;if(i<lr&&!l){await t.evict(s,a);continue}return}})()}}_();var nd=3,rd="llm",od=new Set(["paused","error"]);function Wi(t,e){let n=t.doc(rd),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 i=null,s=!1,a=new Set,u=[],d=l=>{if(!i){i=l;for(let c of[...a])c(l);a.clear()}};return r&&u.push(n.onConnectionState(l=>{s&&(l.consecutiveFailures<nd||d(new le(`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"){s=!0;return}s&&od.has(l.name)&&d(new le(`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:()=>i,onGone(l){return i?(l(i),()=>{}):(a.add(l),()=>a.delete(l))},dispose(){a.clear();for(let l of u.splice(0))l()}}}async function*cr(t,e){let n=e.gone();if(n)throw n;let r=t[Symbol.asyncIterator](),o,i=new Promise((s,a)=>{o=e.onGone(u=>a(u))});i.catch(()=>{});try{for(;;){let s=await Promise.race([r.next(),i]);if(s.done)return;yield s.value}}finally{o?.(),r.return?.(void 0)}}function Ji(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 i=r?o(void 0,r):o();if(typeof i.end!="function")throw new Error(`app "${e}" function "${n}" returned a session without end()`);return i}function id(t,e){let{baseUrl:n,orgId:r,auth:o,subject:i}=t,s=e.fnName??t.fnName,a=ut(e),u=t.sessionStartOptions?.(a),d=e.sharedApp?{sharedApp:!0,...u}:u,l=E=>({session:E,responses:new fn(E),gone:Wi(E,a)});if(o.lane==="jwt"){let E=(0,At.App)(e.appSlug,{baseUrl:n,orgId:r,jwt:o.jwt});return l(Ji(E,e.appSlug,s,d))}let{apiKey:c,gatewayUrl:y}=o,g=(0,At.App)(e.appSlug,{baseUrl:n,orgId:r,getAccessToken:async()=>(await(0,At.createClientToken)(c,{baseUrl:y,expiresIn:300,allowedFunctions:[`${e.appSlug}/${s}`],...i===void 0?{}:{subject:i},...e.sharedApp?{sharedApp:!0}:{}})).token});return l(Ji(g,e.appSlug,s,d))}function sd(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}var ad=new Set(["error","paused"]);function fr(t){let{auth:e,catalog:n}=t,r=null,o=s=>{let a=ut(s),u=id(t,s);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},i=new $t({defaultApp:t.defaultApp,fnName:t.fnName,openSession:o,closeSession:async s=>{if(s.gone.dispose(),t.release==="detach"){if(typeof s.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');s.session.detach();return}await s.session.end()},listApps:e.lane==="api-key"?()=>zr({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,listCatalog:n?()=>Fi(n):null,listPrices:n?()=>Nr(n):null,shared:e.lane==="api-key"?()=>Cr({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,sessionKey:sd});return r=i,t.defaultApp!==null&&i.seed(t.defaultApp,o({appSlug:t.defaultApp})),i}async function pr(t){let e=t.whenLive;if(typeof e!="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 e.call(t)}function mr(t,e=Co){let n=new WeakMap,r=new WeakMap,o=new WeakMap;return{createResponse:Vi({sessionFor:i=>t.sessionFor(i),sessionIdOf:i=>{let s=i.session.id;return typeof s=="string"&&s.length>0?s:null},evict:(i,s)=>t.evict(i,s),createStream:async(i,s)=>(await pr(i.session),cr(await i.responses.responses.create(s),i.gone))}),listModels:()=>t.modelList(),openRouterModels:()=>t.openRouterModels(),supportsImages:(i,s)=>t.supportsImages(i,s),listImageModels:()=>t.imageModelList(),catalogIdentity:i=>t.catalogIdentityFor(i),servingAppSlug:async i=>await t.servingAppSlugFor(i),supportsAudio:i=>t.supportsAudio(i),openAudio:async i=>{let{entry:s}=await t.sessionFor(i),a=n.get(s);return a||(a=(async()=>(await pr(s.session),To(s.session,await e())))(),n.set(s,a),a.catch(()=>n.delete(s))),a},openVideo:async i=>{let{entry:s}=await t.sessionFor(i),a=r.get(s);return a||(a=Po(s.session),r.set(s,a)),a},openVideoOut:async i=>{let{entry:s}=await t.sessionFor(i),a=o.get(s);return a||(a=Mo(s.session),o.set(s,a)),a},sessionHandle:async i=>(await t.handleFor(i)).handle,createResponseOn:async(i,s)=>{let{entry:a}=await t.sessionForHandle(i);return await pr(a.session),cr(await a.responses.responses.create(s),a.gone)},onSessionEnd:async(i,s)=>{let{entry:a}=await t.sessionForHandle(i),u=a.session;if(typeof u.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let d=!1;return u.onPhase(l=>{if(d||!ad.has(l.name))return;d=!0;let c=u.endsAt??null;s({timeLeftMs:c?Math.max(0,c.getTime()-Date.now()):null,reason:l.reason??`session ${l.name}`})})}}}var ud=512;function _n(t){return`urun-inference-proxy-${(0,yn.createHash)("sha256").update(t).digest("hex").slice(0,32)}`}var mn=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 r=this.opts.build(e);return{router:r.router,clients:this.label(r.clients,e)}}let n=fr({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:Fn,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:_n(e.apiKey),release:"detach"});return{router:n,clients:this.label(mr(n),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>=ud;){let o=this.tenants.entries().next();if(o.done)break;let[i,s]=o.value;this.tenants.delete(i),s.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("; ")}`)}},Ki=512,dd=1024,gn=class extends Error{constructor(){super(`conversation capacity reached (${Ki} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function ld(t,e,n,r){let o=fr({baseUrl:t.baseUrl,orgId:e.orgId,defaultApp:null,fnName:Fn,apiUrl:t.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:t.baseUrl},catalog:t.catalog,subject:n.subject,release:"detach",sessionStartOptions:i=>{let s=r.get(i);return s===void 0?void 0:{sessionId:s}},onSessionOpen:(i,s)=>{r.set(i,s)}});return{clients:mr(o),release:async()=>{await o.closeAll()}}}var hn=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,n){let r={id:n,subject:`${_n(e.apiKey)}:live:${n}`},o=new Map,i=this.opts.build?this.opts.build(e,r):ld(this.opts,e,r,o);return{id:n,backhaul:i,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 i=this.build(e,n);return i.attachments=1,r.set(n,i),i}get conversationCount(){let e=0;for(let n of this.byCaller.values())e+=n.size;return e}evictOverflow(){for(;this.conversationCount>=Ki;){let e=this.byCaller.keys().next();if(e.done)throw new gn;let n=e.value,r=this.byCaller.get(n),o=!1;for(let[i,s]of r)if(!(s.attachments>0)){r.delete(i),this.dropHandleIndexFor(n,i),s.backhaul.release().catch(()=>{}),o=!0;break}if(!o)throw r.size===0&&this.byCaller.delete(n),new gn;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,yn.randomUUID)())),n),i={createResponse:(s,a)=>o().backhaul.clients.createResponse(s,a),catalogIdentity:s=>o().backhaul.clients.catalogIdentity(s),servingAppSlug:s=>{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(s)},listModels:()=>o().backhaul.clients.listModels(),sessionHandle:s=>o().backhaul.clients.sessionHandle(s),createResponseOn:(s,a)=>o().backhaul.clients.createResponseOn(s,a),onSessionEnd:(s,a)=>o().backhaul.clients.onSessionEnd(s,a)};return i.openAudio=async s=>{let a=o().backhaul.clients.openAudio;if(a===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return a(s)},i.openVideo=async s=>{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(s)},i.openVideoOut=async s=>{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(s)},i.supportsAudio=async s=>{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(s)},{clients:i,bindResumeHandle:s=>{let a=n;if(a===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=dd;){let u=this.byHandle.keys().next();if(u.done)break;this.byHandle.delete(u.value)}this.byHandle.set(s,{apiKey:e.apiKey,conversationId:a.id})},clientsForResume:async s=>{let a=this.byHandle.get(s),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 le("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 s=n;n=null,s!==null&&(s.attachments-=1,!(s.attachments>0)&&await s.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("; ")}`)}};_();var wn="/inference-usage",zi=25e3;var cd=new Set(["Error","TypeError","SyntaxError","TimeoutError"]);function Yi(t){if(!(t instanceof Error))return`a ${typeof t}`;let e=cd.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 vn=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async lookup(e,n){let r=`${this.opts.apiUrl.replace(/\/+$/,"")}${wn}`,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(zi)})}catch(s){throw new Q(`the uRun usage surface (POST ${wn}) could not be reached within ${zi}ms: ${Yi(s)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new we("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 s=await o.text().catch(()=>null),a=s===null?"body unreadable":`${s.length}-character body`;throw new Q(`the uRun usage surface (POST ${wn}) answered HTTP ${o.status} (${a})`)}let i;try{i=await o.json()}catch(s){throw new Q(`the uRun usage surface (POST ${wn}) answered HTTP ${o.status} with a body that is not JSON: ${Yi(s)}`)}return wo(i,this.opts.onRejectedRow)}};_();var pd="/inference-ledger",fd=1e4,bn=class extends Error{ledgerFailureReason;constructor(e,n="duplicate"){super(e),this.name="LedgerDuplicateError",this.ledgerFailureReason=n}},hr=class extends Error{constructor(n,r){super(r);this.ledgerAlreadyWritten=n;this.name="LedgerAlreadyWrittenError"}ledgerAlreadyWritten},ae=class extends Error{ledgerFailureReason="surface";constructor(e){super(e),this.name="LedgerSurfaceError"}},yr=class extends we{ledgerFailureReason="auth";constructor(e){super(e),this.name="LedgerAuthError"}},md=[["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",Xi],["cost_nano_usd",Xi],["price_version",t=>t===null||typeof t=="string"]];function gd(t){for(let[e,n]of md){if(!(e in t))return`${e} (absent)`;if(!n(t[e]))return`${e} (${JSON.stringify(t[e])})`}return null}function hd(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"}:gd(r)!==null?{status:"unavailable"}:{status:"found",row:r}}function Xi(t){return t===null?!0:typeof t=="number"?Number.isFinite(t):typeof t!="string"?!1:/[^\s]/.test(t)&&Number.isFinite(Number(t))}function gr(t,e){return t===null?e===null:e===null?!1:Number(e)===t}function Qi(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 yd(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&&Qi(t.started_at,n.started_at)&&Qi(t.completed_at,n.completed_at)&&gr(t.prompt_tokens,n.prompt_tokens)&&gr(t.completion_tokens,n.completion_tokens)&&gr(t.output_units,n.output_units)}function _d(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new ae(`${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 ae(`${e} answered a row with no inference_id: ${JSON.stringify(t)}`);if(!("cost_nano_usd"in n))throw new ae(`${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 ae(`${e} answered cost_nano_usd ${JSON.stringify(o)}, which is neither a non-negative integer nor null`);let i=d=>{if(!(d in n))throw new ae(`${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 ae(`${e} answered a non-string ${d}: ${JSON.stringify(l)}`);if(!/[^\s]/.test(l))throw new ae(`${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},s=i("price_version"),a=i("unpriced_reason"),u=o!=null;if(u&&(s===null||a!==null))throw new ae(`${e} answered a PRICED row (cost_nano_usd ${String(o)}) that is not cleanly attributed \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(a)}`);if(!u&&(s!==null||a===null))throw new ae(`${e} answered an UNPRICED row that does not say why \u2014 price_version ${JSON.stringify(s)}, 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:s,unpriced_reason:a}}var kn=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async write(e,n){let r=`${this.opts.apiUrl.replace(/\/+$/,"")}${pd}`,o;try{o=await this.fetchImpl(r,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify(n),signal:AbortSignal.timeout(fd)})}catch(u){throw new ae(`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=hd(u);throw d.status==="found"&&yd(n,d.row)?new hr(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 bn(`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 bn(`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 yr("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 ae(`the uRun ledger surface at ${r} answered HTTP ${o.status}: ${u.slice(0,400)}`)}let i;try{i=await o.json()}catch(u){throw new ae(`the uRun ledger surface at ${r} answered a body that is not JSON: ${u instanceof Error?u.message:String(u)}`)}let s=i?.recorded;if(s===void 0)throw new ae(`the uRun ledger surface at ${r} answered a body with no "recorded" field`);let a=_d(s,`the uRun ledger surface at ${r}`);if(a.inference_id!==n.inference_id)throw new ae(`the uRun ledger surface at ${r} answered for inference_id ${a.inference_id} when asked about ${n.inference_id}`);return a}};var wd=5e3,vd=3e3,bd="/api/client-tokens";function _r(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function Zi(t,e,n,r){_r(t,e,{error:{message:n,type:r}})}async function kd(t,e=fetch){let n=`${t.replace(/\/+$/,"")}${bd}`;try{let r=await e(n,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(vd)});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 ts(t){let e=t.resolver??new ln({gatewayUrl:t.baseUrl}),n=t.registry??new mn({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),r=t.probeControlPlane??(()=>kd(t.baseUrl)),o=t.usageClient??new vn({apiUrl:t.apiUrl}),i=t.ledgerClient??new kn({apiUrl:t.apiUrl}),s=new qt(t.defaultMetrics===void 0?{}:{defaultMetrics:t.defaultMetrics}),a=async w=>e.resolve(Et(w.headers.authorization)),u=w=>w instanceof we?{status:w.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:w instanceof it?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,d=Li({clients:async w=>n.clientsFor(await a(w)),usage:{authorize:w=>Et(w.headers.authorization),lookup:(w,h)=>o.lookup(w,h)},statusOf:u,...t.requestLog?{requestLog:t.requestLog}:{},observe:w=>s.observe(w),ledger:{write:(w,h)=>i.write(Et(w.headers.authorization),h),observe:w=>s.observeLedger(w),...t.ledgerLog?{report:t.ledgerLog}:{}}}),l=null,c=(0,es.createServer)((w,h)=>{let b=new URL(w.url??"/","http://localhost");(async()=>{if(w.method==="GET"&&b.pathname==="/healthz"){_r(h,200,{ok:!0});return}if(w.method==="GET"&&b.pathname==="/readyz"){let B=Date.now();if(!l||B-l.at>=wd){let M=await r();l={at:B,...M}}_r(h,l.ready?200:503,{ok:l.ready,control_plane:l.detail});return}let R=b.pathname==="/v1"||b.pathname.startsWith("/v1/"),T=b.pathname.startsWith("/partners/");if(!R&&!T){Zi(h,404,`no route for ${w.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}d(w,h)})().catch(R=>{if(!h.headersSent){Zi(h,500,String(R instanceof Error?R.message:R),"proxy_error");return}h.end()})}),y=t.conversations??new hn({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),g=[Gn(c,{authenticate:async w=>{let h=await e.resolve(w),b=y.connectionFor(h);return{clients:b.clients,scope:_n(h.apiKey),onBindResumeHandle:b.bindResumeHandle,clientsForResume:b.clientsForResume,release:b.release}}}),Zn(c,{clients:async w=>n.clientsFor(await a(w)),statusOf:u}).close,tn(c,{resolver:{resolve:async w=>{let h=await e.resolve(Et(w.headers.authorization)),b=y.connectionFor(h),R;try{R=await Di(b.clients,Ni(w))}catch(T){throw b.release().catch(()=>{}),T}return{...R,release:b.release}}}})];c.on("upgrade",(w,h)=>{let b=new URL(w.url??"/","http://localhost");b.pathname===Wt||b.pathname===Xn||b.pathname===kt||h.destroyed||(h.write(`HTTP/1.1 404 Not Found\r
47
61
  Connection: close\r
48
62
  \r
49
- no WS route for ${S.pathname} \u2014 this endpoint serves only the Gemini Live and /v1/responses upgrade paths`),g.destroy())}),{server:c,closeAll:()=>t.closeAll(),closeWebSockets:async()=>{for(let f of h)await f();await d.detachAll()}}}async function rr(n,e,t){t&&await t();let r=new Promise(o=>n.close(()=>o()));n.closeAllConnections(),await r,await e()}async function Qo(){let n=ln(ge.default.env),{server:e,closeAll:t,closeWebSockets:r}=nr(n);await new Promise((s,i)=>{e.once("error",i),e.listen(n.port,_t,s)}),ge.default.stdout.write(`inference-proxy listening on http://${_t}:${n.port}/v1 (control plane ${n.baseUrl}, org API ${n.apiUrl}, catalog oracle ${n.catalog?"on":"off"})
50
- `);let o=async s=>{ge.default.stdout.write(`inference-proxy: ${s} \u2014 draining
51
- `),await rr(e,t,r)};await new Promise(s=>{ge.default.once("SIGINT",()=>{o("SIGINT").finally(s)}),ge.default.once("SIGTERM",()=>{o("SIGTERM").finally(s)})})}Qo().then(()=>ge.default.exit(0),n=>{ge.default.stderr.write(`inference-proxy: ${String(n instanceof Error?n.stack??n.message:n)}
52
- `),ge.default.exit(1)});
63
+ no WS route for ${b.pathname} \u2014 this endpoint serves only the Gemini Live, /v1/responses, and /v1/realtime upgrade paths`),h.destroy())});let E=async()=>{for(let w of g)await w();await y.detachAll()};return{server:c,metricsServer:ko(s),metrics:s,edgeIdentityServer:So(),closeAll:()=>n.closeAll(),closeWebSockets:E}}async function ns(t,e,n){n&&await n();let r=new Promise(o=>t.close(()=>o()));t.closeAllConnections(),await r,await io()||so(),await e()}function xd(){let t=null;return t=Zr({streams:ke.default,note:e=>{t?.isDead("stderr")!==!0&&Le(ke.default.stderr,e)}}),t}async function Rd(){xd();let t=Eo(ke.default.env),{server:e,metricsServer:n,edgeIdentityServer:r,closeAll:o,closeWebSockets:i}=ts(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(Ln,je,a)}),await new Promise((a,u)=>{r.once("error",u),r.listen(Dn,je,a)}),ke.default.stdout.write(`inference-proxy listening on http://${je}:${t.port}/v1 (metrics http://${je}:${Ln}${Ft}, edge identity http://${je}:${Dn}, control plane ${t.baseUrl}, org API ${t.apiUrl}, catalog oracle ${t.catalog?"on":"off"})
64
+ `);let s=async a=>{ke.default.stdout.write(`inference-proxy: ${a} \u2014 draining
65
+ `),await ns(e,o,i),await new Promise(u=>n.close(()=>u())),await new Promise(u=>r.close(()=>u()))};await new Promise(a=>{ke.default.once("SIGINT",()=>{s("SIGINT").finally(a)}),ke.default.once("SIGTERM",()=>{s("SIGTERM").finally(a)})})}Rd().then(()=>ke.default.exit(0),t=>{ke.default.stderr.write(`inference-proxy: ${String(t instanceof Error?t.stack??t.message:t)}
66
+ `),ke.default.exit(1)});
53
67
  /*! Bundled license information:
54
68
 
55
69
  reflect-metadata/Reflect.js: