@urun-sh/openai 0.5.4 → 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 +65 -37
  17. package/dist/hosted/bin.js +3 -3
  18. package/dist/hosted/index.cjs +46 -19
  19. package/dist/hosted/index.d.cts +760 -272
  20. package/dist/hosted/index.d.ts +203 -107
  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 +54 -40
  33. package/dist/proxy/cli.js +14 -14
  34. package/dist/proxy/index.cjs +33 -22
  35. package/dist/proxy/index.d.cts +546 -4
  36. package/dist/proxy/index.d.ts +185 -4
  37. package/dist/proxy/index.js +1 -1
  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-4MSBS7M6.js +0 -1
  45. package/dist/chunk-5NXM4IO3.js +0 -2
  46. package/dist/chunk-CWJRDBDC.js +0 -1
  47. package/dist/chunk-OI2OY32M.js +0 -1
  48. package/dist/chunk-RQBD4OAA.js +0 -6
  49. package/dist/chunk-STFRT5OY.js +0 -1
  50. package/dist/chunk-VNM4M4P3.js +0 -40
  51. package/dist/server-DuH_5OD9.d.ts +0 -84
  52. package/dist/server-wmUnMewW.d.cts +0 -206
  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,41 +1,69 @@
1
1
  #!/usr/bin/env node
2
- "use strict";var hn=Object.create;var yt=Object.defineProperty;var gn=Object.getOwnPropertyDescriptor;var yn=Object.getOwnPropertyNames;var wn=Object.getPrototypeOf,_n=Object.prototype.hasOwnProperty;var vn=(t,n,e)=>()=>{if(e)throw e[0];try{return t&&(n=t(t=0)),n}catch(r){throw e=[r],r}};var kn=(t,n)=>()=>{try{return n||t((n={exports:{}}).exports,n),n.exports}catch(e){throw n=0,e}};var bn=(t,n,e,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of yn(n))!_n.call(t,o)&&o!==e&&yt(t,o,{get:()=>n[o],enumerable:!(r=gn(n,o))||r.enumerable});return t};var ke=(t,n,e)=>(e=t!=null?hn(wn(t)):{},bn(n||!t||!t.__esModule?yt(e,"default",{value:t,enumerable:!0}):e,t));var S=vn(()=>{"use strict"});var _t=kn(()=>{"use strict";S();var wt;(function(t){(function(n){var e=typeof globalThis=="object"?globalThis:typeof global=="object"?global:typeof self=="object"?self:typeof this=="object"?this:l(),r=o(t);typeof e.Reflect<"u"&&(r=o(e.Reflect,r)),n(r,e),typeof e.Reflect>"u"&&(e.Reflect=t);function o(d,p){return function(h,x){Object.defineProperty(d,h,{configurable:!0,writable:!0,value:x}),p&&p(h,x)}}function u(){try{return Function("return this;")()}catch{}}function i(){try{return(0,eval)("(function() { return this; })()")}catch{}}function l(){return u()||i()}})(function(n,e){var r=Object.prototype.hasOwnProperty,o=typeof Symbol=="function",u=o&&typeof Symbol.toPrimitive<"u"?Symbol.toPrimitive:"@@toPrimitive",i=o&&typeof Symbol.iterator<"u"?Symbol.iterator:"@@iterator",l=typeof Object.create=="function",d={__proto__:[]}instanceof Array,p=!l&&!d,h={create:l?function(){return We(Object.create(null))}:d?function(){return We({__proto__:null})}:function(){return We({})},has:p?function(s,a){return r.call(s,a)}:function(s,a){return a in s},get:p?function(s,a){return r.call(s,a)?s[a]:void 0}:function(s,a){return s[a]}},x=Object.getPrototypeOf(Function),A=typeof Map=="function"&&typeof Map.prototype.entries=="function"?Map:ln(),C=typeof Set=="function"&&typeof Set.prototype.entries=="function"?Set:dn(),T=typeof WeakMap=="function"?WeakMap:pn(),P=o?Symbol.for("@reflect-metadata:registry"):void 0,g=an(),V=un(g);function G(s,a,c,f){if(y(c)){if(!ct(s))throw new TypeError;if(!lt(a))throw new TypeError;return z(s,a)}else{if(!ct(s))throw new TypeError;if(!R(a))throw new TypeError;if(!R(f)&&!y(f)&&!L(f))throw new TypeError;return L(f)&&(f=void 0),c=Z(c),N(s,a,c,f)}}n("decorate",G);function K(s,a){function c(f,b){if(!R(f))throw new TypeError;if(!y(b)&&!on(b))throw new TypeError;ce(s,a,f,b)}return c}n("metadata",K);function W(s,a,c,f){if(!R(c))throw new TypeError;return y(f)||(f=Z(f)),ce(s,a,c,f)}n("defineMetadata",W);function Y(s,a,c){if(!R(a))throw new TypeError;return y(c)||(c=Z(c)),H(s,a,c)}n("hasMetadata",Y);function _(s,a,c){if(!R(a))throw new TypeError;return y(c)||(c=Z(c)),Q(s,a,c)}n("hasOwnMetadata",_);function D(s,a,c){if(!R(a))throw new TypeError;return y(c)||(c=Z(c)),ne(s,a,c)}n("getMetadata",D);function ee(s,a,c){if(!R(a))throw new TypeError;return y(c)||(c=Z(c)),$(s,a,c)}n("getOwnMetadata",ee);function J(s,a){if(!R(s))throw new TypeError;return y(a)||(a=Z(a)),pe(s,a)}n("getMetadataKeys",J);function X(s,a){if(!R(s))throw new TypeError;return y(a)||(a=Z(a)),fe(s,a)}n("getOwnMetadataKeys",X);function te(s,a,c){if(!R(a))throw new TypeError;if(y(c)||(c=Z(c)),!R(a))throw new TypeError;y(c)||(c=Z(c));var f=Ae(a,c,!1);return y(f)?!1:f.OrdinaryDeleteMetadata(s,a,c)}n("deleteMetadata",te);function z(s,a){for(var c=s.length-1;c>=0;--c){var f=s[c],b=f(a);if(!y(b)&&!L(b)){if(!lt(b))throw new TypeError;a=b}}return a}function N(s,a,c,f){for(var b=s.length-1;b>=0;--b){var B=s[b],q=B(a,c,f);if(!y(q)&&!L(q)){if(!R(q))throw new TypeError;f=q}}return f}function H(s,a,c){var f=Q(s,a,c);if(f)return!0;var b=Je(a);return L(b)?!1:H(s,b,c)}function Q(s,a,c){var f=Ae(a,c,!1);return y(f)?!1:ge(f.OrdinaryHasOwnMetadata(s,a,c))}function ne(s,a,c){var f=Q(s,a,c);if(f)return $(s,a,c);var b=Je(a);if(!L(b))return ne(s,b,c)}function $(s,a,c){var f=Ae(a,c,!1);if(!y(f))return f.OrdinaryGetOwnMetadata(s,a,c)}function ce(s,a,c,f){var b=Ae(c,f,!0);b.OrdinaryDefineOwnMetadata(s,a,c,f)}function pe(s,a){var c=fe(s,a),f=Je(s);if(f===null)return c;var b=pe(f,a);if(b.length<=0)return c;if(c.length<=0)return b;for(var B=new C,q=[],E=0,m=c;E<m.length;E++){var w=m[E],v=B.has(w);v||(B.add(w),q.push(w))}for(var k=0,O=b;k<O.length;k++){var w=O[k],v=B.has(w);v||(B.add(w),q.push(w))}return q}function fe(s,a){var c=Ae(s,a,!1);return c?c.OrdinaryOwnMetadataKeys(s,a):[]}function I(s){if(s===null)return 1;switch(typeof s){case"undefined":return 0;case"boolean":return 2;case"string":return 3;case"symbol":return 4;case"number":return 5;case"object":return s===null?1:6;default:return 6}}function y(s){return s===void 0}function L(s){return s===null}function U(s){return typeof s=="symbol"}function R(s){return typeof s=="object"?s!==null:typeof s=="function"}function ie(s,a){switch(I(s)){case 0:return s;case 1:return s;case 2:return s;case 3:return s;case 4:return s;case 5:return s}var c=a===3?"string":a===5?"number":"default",f=dt(s,u);if(f!==void 0){var b=f.call(s,c);if(R(b))throw new TypeError;return b}return he(s,c==="default"?"number":c)}function he(s,a){if(a==="string"){var c=s.toString;if(ve(c)){var f=c.call(s);if(!R(f))return f}var b=s.valueOf;if(ve(b)){var f=b.call(s);if(!R(f))return f}}else{var b=s.valueOf;if(ve(b)){var f=b.call(s);if(!R(f))return f}var B=s.toString;if(ve(B)){var f=B.call(s);if(!R(f))return f}}throw new TypeError}function ge(s){return!!s}function Ee(s){return""+s}function Z(s){var a=ie(s,3);return U(a)?a:Ee(a)}function ct(s){return Array.isArray?Array.isArray(s):s instanceof Object?s instanceof Array:Object.prototype.toString.call(s)==="[object Array]"}function ve(s){return typeof s=="function"}function lt(s){return typeof s=="function"}function on(s){switch(I(s)){case 3:return!0;case 4:return!0;default:return!1}}function qe(s,a){return s===a||s!==s&&a!==a}function dt(s,a){var c=s[a];if(c!=null){if(!ve(c))throw new TypeError;return c}}function pt(s){var a=dt(s,i);if(!ve(a))throw new TypeError;var c=a.call(s);if(!R(c))throw new TypeError;return c}function ft(s){return s.value}function mt(s){var a=s.next();return a.done?!1:a}function ht(s){var a=s.return;a&&a.call(s)}function Je(s){var a=Object.getPrototypeOf(s);if(typeof s!="function"||s===x||a!==x)return a;var c=s.prototype,f=c&&Object.getPrototypeOf(c);if(f==null||f===Object.prototype)return a;var b=f.constructor;return typeof b!="function"||b===s?a:b}function sn(){var s;!y(P)&&typeof e.Reflect<"u"&&!(P in e.Reflect)&&typeof e.Reflect.defineMetadata=="function"&&(s=cn(e.Reflect));var a,c,f,b=new T,B={registerProvider:q,getProvider:m,setProvider:v};return B;function q(k){if(!Object.isExtensible(B))throw new Error("Cannot add provider to a frozen registry.");switch(!0){case s===k:break;case y(a):a=k;break;case a===k:break;case y(c):c=k;break;case c===k:break;default:f===void 0&&(f=new C),f.add(k);break}}function E(k,O){if(!y(a)){if(a.isProviderFor(k,O))return a;if(!y(c)){if(c.isProviderFor(k,O))return a;if(!y(f))for(var M=pt(f);;){var F=mt(M);if(!F)return;var oe=ft(F);if(oe.isProviderFor(k,O))return ht(M),oe}}}if(!y(s)&&s.isProviderFor(k,O))return s}function m(k,O){var M=b.get(k),F;return y(M)||(F=M.get(O)),y(F)&&(F=E(k,O),y(F)||(y(M)&&(M=new A,b.set(k,M)),M.set(O,F))),F}function w(k){if(y(k))throw new TypeError;return a===k||c===k||!y(f)&&f.has(k)}function v(k,O,M){if(!w(M))throw new Error("Metadata provider not registered.");var F=m(k,O);if(F!==M){if(!y(F))return!1;var oe=b.get(k);y(oe)&&(oe=new A,b.set(k,oe)),oe.set(O,M)}return!0}}function an(){var s;return!y(P)&&R(e.Reflect)&&Object.isExtensible(e.Reflect)&&(s=e.Reflect[P]),y(s)&&(s=sn()),!y(P)&&R(e.Reflect)&&Object.isExtensible(e.Reflect)&&Object.defineProperty(e.Reflect,P,{enumerable:!1,configurable:!1,writable:!1,value:s}),s}function un(s){var a=new T,c={isProviderFor:function(w,v){var k=a.get(w);return y(k)?!1:k.has(v)},OrdinaryDefineOwnMetadata:q,OrdinaryHasOwnMetadata:b,OrdinaryGetOwnMetadata:B,OrdinaryOwnMetadataKeys:E,OrdinaryDeleteMetadata:m};return g.registerProvider(c),c;function f(w,v,k){var O=a.get(w),M=!1;if(y(O)){if(!k)return;O=new A,a.set(w,O),M=!0}var F=O.get(v);if(y(F)){if(!k)return;if(F=new A,O.set(v,F),!s.setProvider(w,v,c))throw O.delete(v),M&&a.delete(w),new Error("Wrong provider for target.")}return F}function b(w,v,k){var O=f(v,k,!1);return y(O)?!1:ge(O.has(w))}function B(w,v,k){var O=f(v,k,!1);if(!y(O))return O.get(w)}function q(w,v,k,O){var M=f(k,O,!0);M.set(w,v)}function E(w,v){var k=[],O=f(w,v,!1);if(y(O))return k;for(var M=O.keys(),F=pt(M),oe=0;;){var gt=mt(F);if(!gt)return k.length=oe,k;var fn=ft(gt);try{k[oe]=fn}catch(mn){try{ht(F)}finally{throw mn}}oe++}}function m(w,v,k){var O=f(v,k,!1);if(y(O)||!O.delete(w))return!1;if(O.size===0){var M=a.get(v);y(M)||(M.delete(k),M.size===0&&a.delete(M))}return!0}}function cn(s){var a=s.defineMetadata,c=s.hasOwnMetadata,f=s.getOwnMetadata,b=s.getOwnMetadataKeys,B=s.deleteMetadata,q=new T,E={isProviderFor:function(m,w){var v=q.get(m);return!y(v)&&v.has(w)?!0:b(m,w).length?(y(v)&&(v=new C,q.set(m,v)),v.add(w),!0):!1},OrdinaryDefineOwnMetadata:a,OrdinaryHasOwnMetadata:c,OrdinaryGetOwnMetadata:f,OrdinaryOwnMetadataKeys:b,OrdinaryDeleteMetadata:B};return E}function Ae(s,a,c){var f=g.getProvider(s,a);if(!y(f))return f;if(c){if(g.setProvider(s,a,V))return V;throw new Error("Illegal state.")}}function ln(){var s={},a=[],c=(function(){function E(m,w,v){this._index=0,this._keys=m,this._values=w,this._selector=v}return E.prototype["@@iterator"]=function(){return this},E.prototype[i]=function(){return this},E.prototype.next=function(){var m=this._index;if(m>=0&&m<this._keys.length){var w=this._selector(this._keys[m],this._values[m]);return m+1>=this._keys.length?(this._index=-1,this._keys=a,this._values=a):this._index++,{value:w,done:!1}}return{value:void 0,done:!0}},E.prototype.throw=function(m){throw this._index>=0&&(this._index=-1,this._keys=a,this._values=a),m},E.prototype.return=function(m){return this._index>=0&&(this._index=-1,this._keys=a,this._values=a),{value:m,done:!0}},E})(),f=(function(){function E(){this._keys=[],this._values=[],this._cacheKey=s,this._cacheIndex=-2}return Object.defineProperty(E.prototype,"size",{get:function(){return this._keys.length},enumerable:!0,configurable:!0}),E.prototype.has=function(m){return this._find(m,!1)>=0},E.prototype.get=function(m){var w=this._find(m,!1);return w>=0?this._values[w]:void 0},E.prototype.set=function(m,w){var v=this._find(m,!0);return this._values[v]=w,this},E.prototype.delete=function(m){var w=this._find(m,!1);if(w>=0){for(var v=this._keys.length,k=w+1;k<v;k++)this._keys[k-1]=this._keys[k],this._values[k-1]=this._values[k];return this._keys.length--,this._values.length--,qe(m,this._cacheKey)&&(this._cacheKey=s,this._cacheIndex=-2),!0}return!1},E.prototype.clear=function(){this._keys.length=0,this._values.length=0,this._cacheKey=s,this._cacheIndex=-2},E.prototype.keys=function(){return new c(this._keys,this._values,b)},E.prototype.values=function(){return new c(this._keys,this._values,B)},E.prototype.entries=function(){return new c(this._keys,this._values,q)},E.prototype["@@iterator"]=function(){return this.entries()},E.prototype[i]=function(){return this.entries()},E.prototype._find=function(m,w){if(!qe(this._cacheKey,m)){this._cacheIndex=-1;for(var v=0;v<this._keys.length;v++)if(qe(this._keys[v],m)){this._cacheIndex=v;break}}return this._cacheIndex<0&&w&&(this._cacheIndex=this._keys.length,this._keys.push(m),this._values.push(void 0)),this._cacheIndex},E})();return f;function b(E,m){return E}function B(E,m){return m}function q(E,m){return[E,m]}}function dn(){var s=(function(){function a(){this._map=new A}return Object.defineProperty(a.prototype,"size",{get:function(){return this._map.size},enumerable:!0,configurable:!0}),a.prototype.has=function(c){return this._map.has(c)},a.prototype.add=function(c){return this._map.set(c,c),this},a.prototype.delete=function(c){return this._map.delete(c)},a.prototype.clear=function(){this._map.clear()},a.prototype.keys=function(){return this._map.keys()},a.prototype.values=function(){return this._map.keys()},a.prototype.entries=function(){return this._map.entries()},a.prototype["@@iterator"]=function(){return this.keys()},a.prototype[i]=function(){return this.keys()},a})();return s}function pn(){var s=16,a=h.create(),c=f();return(function(){function m(){this._key=f()}return m.prototype.has=function(w){var v=b(w,!1);return v!==void 0?h.has(v,this._key):!1},m.prototype.get=function(w){var v=b(w,!1);return v!==void 0?h.get(v,this._key):void 0},m.prototype.set=function(w,v){var k=b(w,!0);return k[this._key]=v,this},m.prototype.delete=function(w){var v=b(w,!1);return v!==void 0?delete v[this._key]:!1},m.prototype.clear=function(){this._key=f()},m})();function f(){var m;do m="@@WeakMap@@"+E();while(h.has(a,m));return a[m]=!0,m}function b(m,w){if(!r.call(m,c)){if(!w)return;Object.defineProperty(m,c,{value:h.create()})}return m[c]}function B(m,w){for(var v=0;v<w;++v)m[v]=Math.random()*255|0;return m}function q(m){if(typeof Uint8Array=="function"){var w=new Uint8Array(m);return typeof crypto<"u"?crypto.getRandomValues(w):typeof msCrypto<"u"?msCrypto.getRandomValues(w):B(w,m),w}return B(new Array(m),m)}function E(){var m=q(s);m[6]=m[6]&79|64,m[8]=m[8]&191|128;for(var w="",v=0;v<s;++v){var k=m[v];(v===4||v===6||v===8)&&(w+="-"),k<16&&(w+="0"),w+=k.toString(16).toLowerCase()}return w}}function We(s){return s.__=void 0,delete s.__,s}})})(wt||(wt={}))});S();var Rs=ke(_t(),1),de=ke(require("process"),1);S();S();var An=ke(require("os"),1),On=ke(require("path"),1),In=require("url"),Tn=require("util");S();S();function ye(t){return t.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}S();var xn={chat:!0,code:!0,agent:!0,vl:!0},vt={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function Sn(t){let n=t.toLowerCase();return Object.hasOwn(vt,n)?vt[n]:null}function ze(t){let n=t.engine_args?.context_length;return typeof n=="number"&&Number.isFinite(n)&&n>0?n:null}function bt(t,n){let e=n.filter(u=>ye(`${u.model_id}-${u.variant}`)===t);if(e.length===1)return e[0];if(e.length>1)return kt(e);let r=n.filter(u=>ye(u.model_id)===t),o=new Set(r.map(u=>u.variant));return r.length>0&&o.size===1?kt(r):null}function kt(t){let n=t[0],e=i=>t.every(l=>(l[i]??void 0)===(n[i]??void 0))?n[i]:void 0,r=t.map(ze),o=r[0],u=o!=null&&r.every(i=>i===o)?o:null;return{model_id:n.model_id,variant:n.variant,gpu_spec:e("gpu_spec"),engine:e("engine"),task:e("task"),description:e("description"),size_gb:e("size_gb"),hf_repo:e("hf_repo"),engine_args:u!==null?{context_length:u}:{},updated_at:e("updated_at")}}function xt(t){let n=t.updated_at?Date.parse(t.updated_at):NaN;return Number.isFinite(n)?Math.floor(n/1e3):0}function St(t,n){let e=bt(t,n);if(!e)return{id:t,object:"model",created:0,owned_by:"urun"};let r=ze(e);return{id:t,object:"model",created:xt(e),owned_by:"urun",name:`${e.model_id}:${e.variant}`,...e.description?{description:e.description}:{},...e.task?{task:e.task}:{},...e.engine?{engine:e.engine}:{},...e.gpu_spec?{gpu_spec:e.gpu_spec}:{},...r!==null?{context_length:r}:{}}}function Rt(t,n){let e=[];for(let r of t){let o=bt(r,n);if(!o||!o.task||xn[o.task]!==!0)continue;let u=ze(o),i={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};u!==null&&(i.max_tokens={type:"integer",min:1,max:u,unit:"token"}),e.push({schema_version:"2.4",id:r,name:`${o.model_id}:${o.variant}`,created:xt(o),quantization:Sn(o.variant),description:o.description??"",hugging_face_id:o.hf_repo?o.hf_repo.split("@")[0]??o.hf_repo:"",input_modalities:[{type:"text",...u!==null?{supported_inputs:{max_context_length:{value:u,unit:"token"}}}:{}},...o.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:i}]})}return{data:e}}var re=class extends Error{},ae=class extends Error{},Ke="urs1.";function Rn(t,n){return Ke+Buffer.from(JSON.stringify([t,n]),"utf8").toString("base64url")}function En(t){if(!t.startsWith(Ke))return null;try{let n=JSON.parse(Buffer.from(t.slice(Ke.length),"base64url").toString("utf8"));if(Array.isArray(n)&&typeof n[0]=="string"&&typeof n[1]=="string")return{app:n[0],key:n[1]}}catch{}return null}async function At(t){let n=t.fetchImpl??fetch,e=`${t.apiUrl.replace(/\/+$/,"")}/apps`,r=await n(e,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new Error(`org apps listing failed: GET ${e} \u2192 ${r.status}`);let o=await r.json();if(!Array.isArray(o.apps))throw new Error(`org apps listing returned no "apps" array (GET ${e})`);if(o.truncated===!0)throw new Error(`org apps listing was truncated (GET ${e} returned ${o.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return o.apps}var Et=6e4,Ue=class{constructor(n){this.opts=n}opts;pool=new Map;appsCache=null;rowsCache=null;seed(n,e){this.pool.set(n,Promise.resolve(e))}async deployedApps(){if(!this.opts.listApps)return[];let n=this.opts.appsTtlMs??Et;if(this.appsCache&&Date.now()-this.appsCache.at<n)return this.appsCache.apps;let e=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:e},e}async catalogRows(){if(!this.opts.listCatalog)return[];let n=this.opts.appsTtlMs??Et;if(this.rowsCache&&Date.now()-this.rowsCache.at<n)return this.rowsCache.rows;let e=await this.opts.listCatalog();return this.rowsCache={at:Date.now(),rows:e},e}servable(n){return n.filter(e=>e.function_name===this.opts.fnName&&e.deployment_status==="active").map(e=>e.app_slug)}async availableIds(){let n=this.opts.defaultApp,e=new Set(n===null?[]:[n]);if(this.opts.listApps)for(let o of this.servable(await this.deployedApps()))e.add(o);for(let o of this.pool.keys())e.add(o);let r=[...e].filter(o=>o!==n).sort();return n===null?r:[n,...r]}async noDefaultApp(n){let e=await this.availableIds(),r=e.length>0?e.join(", "):"(none deployed)";throw new re(n===""?`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 "${n}" 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(n){let e=(n??"").trim(),r=this.opts.defaultApp;if(!e||e==="urun")return r===null?this.noDefaultApp(e):r;let o=ye(e);if(o===r)return r;if(this.pool.has(o))return o;if(this.opts.listApps){let u=await this.deployedApps(),i=this.servable(u);if(i.includes(o))return o;let l=i.filter(p=>p.startsWith(`${o}-`));if(l.length===1)return l[0];if(l.length>1)throw new re(`model "${e}" is ambiguous across deployed apps ${l.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let d=u.find(p=>p.app_slug===o||p.app_slug.startsWith(`${o}-`));if(d)throw new re(`model "${e}" maps to app "${d.app_slug}", which is not an active "${this.opts.fnName}" app \u2014 deploy it with \`urun serve ${e}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.listCatalog){let i=(await this.opts.listCatalog()).find(l=>e===l.model_id||e===`${l.model_id}:${l.variant}`||o===ye(l.model_id)||o===ye(`${l.model_id}-${l.variant}`));if(i)throw new re(`model "${e}" 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(e):r}async sessionFor(n){let e=await this.resolveApp(n),r=this.pool.get(e);return r||(r=Promise.resolve(this.opts.openSession(e)),this.pool.set(e,r),r.catch(()=>this.pool.delete(e))),{app:e,entry:await r}}keyOf(n){let e=this.opts.sessionKey;if(!e)throw new Error("ModelRouter: sessionKey is not configured \u2014 the session-identity seam (handleFor/sessionForHandle) is unavailable on this router");let r=e(n);if(typeof r!="string"||r.length===0)throw new Error("ModelRouter: sessionKey returned an empty identity for a pooled session");return r}async handleFor(n){let{app:e,entry:r}=await this.sessionFor(n);return{app:e,handle:Rn(e,this.keyOf(r))}}async sessionForHandle(n){let e=En(n);if(!e)throw new ae(`malformed session handle ${JSON.stringify(n)} \u2014 not issued by this proxy`);let{app:r,key:o}=e,u=this.pool.get(r),i=u?await u.then(l=>l,()=>null):null;if(i===null)throw new ae(`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 ae(`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(n,e){let r=this.pool.get(n);!r||await r.then(u=>u,()=>null)!==e||this.pool.get(n)!==r||(this.pool.delete(n),await this.opts.closeSession(e).catch(()=>{}))}async modelList(){let n=await this.availableIds(),e=await this.catalogRows().catch(()=>[]);return{object:"list",data:n.map(r=>St(r,e))}}async openRouterModels(){let n=await this.availableIds(),e=await this.catalogRows();return Rt(n,e)}async closeAll(){let n=[...this.pool.values()];this.pool.clear();let e=[];if(await Promise.all(n.map(async r=>{try{await this.opts.closeSession(await r)}catch(o){e.push(String(o instanceof Error?o.message:o))}})),e.length>0)throw new Error(`failed to close ${e.length} pooled session(s): ${e.join("; ")}`)}};function Ot(t,n="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(t).pathname))throw new Error(`${n} 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 Me(t,n="URUN_GATEWAY_URL"){let e=new URL(t),r=e.hostname==="localhost"||e.hostname==="127.0.0.1"||e.hostname==="[::1]";if(e.protocol!=="https:"&&!r)throw new Error(`${n} must be https (got ${t}) \u2014 the org API key rides the request's Authorization header`);return t}var Pn=8080,It="serve",Ye="0.0.0.0";function Cn(t,n,e){let r=(t[n]??"").trim();if(!r)throw new Error(`${n} is not set \u2014 ${e}`);return r}function Tt(t=process.env){let n=(t.PORT??"").trim(),e=n===""?Pn:Number(n);if(!Number.isInteger(e)||e<0||e>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(n)}`);let r=Ot(Me(Cn(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(),u=o?Me(o,"URUN_API_URL"):`${r.replace(/\/+$/,"")}/v1`,i=(t.URUN_CATALOG_URL??"").trim(),l=(t.URUN_CATALOG_ANON_KEY??"").trim();if(!!i!=!!l)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 d=i?{catalogUrl:Me(i,"URUN_CATALOG_URL"),anonKey:l}:null;return{port:e,baseUrl:r,apiUrl:u,catalog:d}}S();var tn=require("http");S();var qn=require("http");S();var Fn=require("crypto"),Gn=require("ws");S();S();var Xe=class{constructor(n,e){this.backend=n;this.opts=e}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(n){if(!this.source)throw new Error("startOutbound() not called");let e=Buffer.from(n,"base64"),r=Math.floor(e.byteLength/2),o=new Int16Array(r);for(let u=0;u<r;u++)o[u]=e.readInt16LE(u*2);this.source.onData(o)}async startInbound(n){if(n==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(n),this.sink.onframe=e=>{let r=Buffer.from(e.samples.buffer,e.samples.byteOffset,e.samples.byteLength).toString("base64");for(let o of this.outHandlers)o(r)}}onOutputAudio(n){return this.outHandlers.add(n),()=>this.outHandlers.delete(n)}};async function Ut(t,n,e=1e4){let r=new Xe(n,{sampleRate:24e3}),o=await r.startOutbound();await t.stream("rt-audio-in").attach(o);let u=await Un(t,e);return await r.startInbound(u),r}function Un(t,n){let e=t.stream("rt-audio-out");return e.track!=null?Promise.resolve(e.track):new Promise((r,o)=>{let u,i=setTimeout(()=>{u?.(),o(new Error(`enableSessionAudio: no downstream audio track within ${n}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},n),l=d=>{d!=null&&(clearTimeout(i),u?.(),r(d))};u=e.on?.("track",l),e.track!=null&&l(e.track)})}var Pt="rt-video-in",Ct=256e3;function Mt(t){let n=t.stream(Pt),e=n.emit;if(typeof e!="function")throw new Error(`enableSessionVideo: session.stream('${Pt}') 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>Ct)throw new Error(`enableSessionVideo: frame is ${r.byteLength} bytes \u2014 over the ${Ct}-byte data-channel budget (the SFU's 262144-byte SCTP ceiling drops oversize messages SILENTLY; send smaller/lower-quality frames)`);await e.call(n,r)}}}var Mn=24e3,Oe=480,Nn=111;async function $n(){let t=await import("opusscript"),n=t.default??t;return new n(Mn,1,2048)}async function Nt(){let t=await import("werift"),n=await $n();return{createSource:()=>{let e=new t.MediaStreamTrack({kind:"audio"}),r=Math.random()*4294967295>>>0,o=Math.random()*65535&65535,u=Math.random()*4294967295>>>0,i=new Int16Array(0);return{track:e,onData:l=>{let d=new Int16Array(i.length+l.length);d.set(i,0),d.set(l,i.length);let p=0;for(;d.length-p>=Oe;){let h=d.subarray(p,p+Oe);p+=Oe;let x=Buffer.from(h.buffer,h.byteOffset,h.byteLength),A=n.encode(x,Oe);o=o+1&65535,u=u+Oe>>>0;let C=new t.RtpHeader({version:2,payloadType:Nn,sequenceNumber:o,timestamp:u,ssrc:r,marker:!1}),T=new t.RtpPacket(C,A);e.writeRtp(T)}i=d.slice(p)}}},createSink:e=>{let r={onframe:null};return e.onReceiveRtp.subscribe(u=>{let i;try{i=n.decode(u.payload)}catch{return}let l=new Int16Array(i.length/2);for(let d=0;d<l.length;d++)l[d]=i.readInt16LE(d*2);r.onframe?.({samples:l})}),r}}}S();var Ne=require("@urun-sh/core"),Qe="rt-video-out";var Ln="h264";function Dn(t,n){if(!Number.isInteger(t.seq)||t.seq<0)throw new Error(`${n}: header seq must be a non-negative integer (got ${t.seq})`);if(!Number.isInteger(t.ptsUs))throw new Error(`${n}: header ptsUs must be an integer microsecond timestamp (got ${t.ptsUs})`);if(typeof t.codec!="string"||t.codec.length===0)throw new Error(`${n}: header codec must be a non-empty string`);if(typeof t.key!="boolean")throw new Error(`${n}: header key must be a boolean`)}function jn(t){if(t.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${t.byteLength} byte(s), no header length)`);let n=t[0]|t[1]<<8;if(t.byteLength<2+n)throw new Error(`decodeVideoOutRecord: truncated record (header says ${n} bytes, ${t.byteLength-2} present)`);let e;try{e=JSON.parse(new TextDecoder().decode(t.subarray(2,2+n)))}catch(i){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(i)})`)}let r=e;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;Dn(o,"decodeVideoOutRecord");let u=t.subarray(2+n);if(u.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:o,au:u.slice()}}function $t(t){let n=t.stream(Qe);if(typeof n.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${Qe}') 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 e=new Set,r=new Set,o=new Set,u=d=>{for(let p of r)p(d)},i=!1,l=async()=>{for await(let d of n.messages()){if(!(d instanceof Uint8Array)){u(new Error(`openSessionVideoOutLane: "${Qe}" delivered a non-BINARY \xA75 payload (${typeof d}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let p;try{p=jn(d)}catch(h){u(h instanceof Error?h:new Error(String(h)));continue}for(let h of e)h(p)}for(let d of o)d()};return{codec:Ln,onOutputFrame:d=>(e.add(d),i||(i=!0,l().catch(p=>u(p instanceof Error?p:new Error(String(p))))),()=>e.delete(d)),onLaneError:d=>(r.add(d),()=>r.delete(d)),onEnd:d=>(o.add(d),()=>o.delete(d))}}var Hn=24e3,io=`audio/pcm;rate=${Hn}`,Bn=Buffer.alloc(960).toString("base64");var tt=class t{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(n,e,r){this.requests[n]=(this.requests[n]??0)+1,this.localInBytes+=e,this.naiveInBytes+=r}modelEntry(n){let e=this.models[n];if(e)return e;let r=Object.keys(this.models).length>=Jn?"(other)":n;return this.models[r]??={requests:0,tokens_out:0}}model(n){this.modelEntry(n).requests+=1}track(n,e){let r=this,o=performance.now(),u=null,i=0;return(async function*(){for await(let d of n)d.type==="response.output_text.delta"&&typeof d.delta=="string"&&(u===null&&(u=performance.now(),r.ttftMs.push(u-o),r.ttftMs.length>512&&r.ttftMs.shift()),i+=1,r.tokensOut+=1,r.charsOut+=d.delta.length,e!==void 0&&(r.modelEntry(e).tokens_out+=1)),yield d;let l=(performance.now()-(u??o))/1e3;i>0&&l>0&&(r.tokRates.push(i/l),r.tokRates.length>512&&r.tokRates.shift())})()}static p50(n){if(!n.length)return null;let e=[...n].sort((r,o)=>r-o);return e[Math.floor(e.length/2)]}snapshot(){let n=Object.values(this.requests).reduce((e,r)=>e+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:n},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}}}};function rt(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function se(t,n,e){t.writeHead(n,{"content-type":"application/json"}),t.end(JSON.stringify(e))}function le(t,n,e,r="invalid_request_error"){se(t,n,{error:{message:e,type:r}})}function ue(t,n,e,r){se(t,n,{type:"error",error:{type:e,message:r}})}function ot(t,n,e){if(n==="anthropic"){se(t,404,{type:"error",error:{type:"not_found_error",message:e.message}});return}se(t,404,{error:{message:e.message,type:"invalid_request_error",code:"model_not_found"}})}var Jn=256,Wn=128;function st(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,Wn)}var Lt=64*1024*1024,$e=class extends Error{};async function zn(t){let n=[],e=0;for await(let o of t){if(e+=o.length,e>Lt)throw new $e(`request body exceeds ${Lt} bytes`);n.push(o)}let r=Buffer.concat(n).toString("utf8");return r?JSON.parse(r):{}}function be(t,n,e,r){return{id:t,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:n,choices:[{index:0,delta:e,finish_reason:r}]}}var Kn=256;function Yn(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 nt=class{conversations=new Map;thread(n,e){let r=Yn(e);if(n==null)return r;let o=this.conversations.get(String(n));if(!o)throw new Error(`unknown previous_response_id ${String(n)} (proxy restarts drop stored responses)`);return[...o,...r]}remember(n,e,r){this.conversations.set(n,[...e,{role:"assistant",content:r}]);for(let o of this.conversations.keys()){if(this.conversations.size<=Kn)break;this.conversations.delete(o)}}};function Ie(t){let n=t?.output;return Array.isArray(n)?n.filter(e=>e.type==="function_call"):[]}function we(t){return t.find(n=>n.type==="response.completed")?.response??null}function Le(t){let n=t?.usage;if(!n||typeof n!="object")return null;let e=i=>typeof i=="number"&&Number.isFinite(i)&&i>=0?i:0,r=e(n.input_tokens),o=e(n.output_tokens);if(r===0&&o===0)return null;let u=e(n.total_tokens)||r+o;return{input_tokens:r,output_tokens:o,total_tokens:u}}function Dt(t){let n=Le(t);return n?{prompt_tokens:n.input_tokens,completion_tokens:n.output_tokens,total_tokens:n.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function Vt(t){let n=t?.output;if(!Array.isArray(n))return"";let e="";for(let r of n)if(!(r.type!=="reasoning"||!Array.isArray(r.content)))for(let o of r.content)o.type==="reasoning_text"&&(e+=String(o.text??""));return e}function Te(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 _e=class{calls=new Map;add(n){let e=this.calls.get(n.tool_index),r=!e;return e||(e={args:""},this.calls.set(n.tool_index,e)),n.call_id&&(e.id=n.call_id),n.name&&(e.name=n.name),e.args+=n.delta,{call:e,isNew:r}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([n,e])=>({type:"function_call",call_id:e.id??`call_${n}`,name:e.name??"",arguments:e.args}))}},Xn=["<tool_call>","<function="];function Qn(t){return Xn.find(n=>t.includes(n))}function xe(t,n){return`serving row emitted a prose tool call (literal ${JSON.stringify(n)} 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 Se(t,n,e){if(!(!t||n>0))return Qn(e)}function Zn(t){if(Array.isArray(t))return t.map(n=>{let e=n.function;return!e||typeof e!="object"?n:{type:"function",name:e.name,description:e.description,parameters:e.parameters,strict:e.strict}})}var De=class extends Error{};function er(t,n){return Array.isArray(n)?n.map(e=>{let r=e;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 De(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):n}function tr(t){let n=[];for(let e of t){if(e.role==="tool"){n.push({type:"function_call_output",call_id:e.tool_call_id,output:typeof e.content=="string"?e.content:JSON.stringify(e.content??"")});continue}if(e.role==="assistant"&&Array.isArray(e.tool_calls)){typeof e.content=="string"&&e.content&&n.push({role:"assistant",content:e.content});for(let r of e.tool_calls){let o=r.function??{};n.push({type:"function_call",call_id:r.id,name:o.name,arguments:o.arguments})}continue}n.push({...e,content:er(e.role,e.content)})}return n}function Ze(t){return Ie(t).map((n,e)=>({index:e,id:n.call_id??n.id??`call_${e}`,type:"function",function:{name:n.name??"",arguments:n.arguments??"{}"}}))}function jt(t,n){let e=n?.output_text;return typeof e=="string"?e:t.filter(r=>r.type==="response.output_text.delta"&&typeof r.delta=="string").map(r=>r.delta).join("")}async function nr(t,n,e,r,o){let u=t.stream===!0,i;try{i=e.thread(t.previous_response_id,t.input)}catch(g){le(r,400,String(g instanceof Error?g.message:g));return}t.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(i))-Buffer.byteLength(JSON.stringify(t.input)));let l=`resp_${Math.random().toString(36).slice(2,14)}`,d=t.store!==!1,p=st(t);o.model(p);let h=Array.isArray(t.tools)&&t.tools.length>0,x;try{x=await n.createResponse({model:t.model,input:i,stream:u,tools:t.tools,tool_choice:t.tool_choice,temperature:t.temperature,top_p:t.top_p,stop:t.stop,max_output_tokens:t.max_output_tokens,...t.reasoning_effort!==void 0?{reasoning_effort:t.reasoning_effort}:{},...t.chat_template_kwargs!==void 0?{chat_template_kwargs:t.chat_template_kwargs}:{}})}catch(g){if(g instanceof re){ot(r,"openai",g);return}throw g}let A=[];if(u){rt(r);let g=I=>{r.write(`event: ${String(I.type??"message")}
3
- data: ${JSON.stringify(I)}
4
-
5
- `)},V=I=>I.response&&typeof I.response=="object"?{...I,response:{...I.response,id:l}}:I,G=`item_${l.slice(5)}`,K=`rs_${l.slice(5)}`,W=!1,Y=!1,_=0,D=()=>{W||(W=!0,g({type:"response.created",response:{id:l,object:"response",status:"in_progress"}}))},ee=!1,J=0,X="",te=()=>{ee&&(ee=!1,g({type:"response.reasoning_text.done",item_id:K,output_index:J,content_index:0,text:X}),g({type:"response.output_item.done",output_index:J,item:{id:K,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:X}],status:"completed"}}))},z=!1,N=0,H="",Q=I=>{z&&(z=!1,g({type:"response.output_text.done",item_id:G,output_index:N,content_index:0,text:H}),g({type:"response.content_part.done",item_id:G,output_index:N,content_index:0,part:{type:"output_text",text:H}}),g({type:"response.output_item.done",output_index:N,item:{id:G,type:"message",role:"assistant",status:I,content:[{type:"output_text",text:H}]}}))},ne=new _e,$=null,ce=-1,pe=I=>{if(!$)return;let y=$;$=null,g({type:"response.function_call_arguments.done",item_id:y.fcId,output_index:y.outputIndex,arguments:y.args}),g({type:"response.output_item.done",output_index:y.outputIndex,item:{id:y.fcId,type:"function_call",call_id:y.callId??y.fcId,name:y.name??"",arguments:y.args,status:I}})},fe=!1;for await(let I of o.track(x,p)){A.push(I);let y=String(I.type??"");if(y==="response.created"||y==="response.in_progress"){W=!0,g(V(I));continue}if(y==="response.output_item.added"){Y=!0,g(V(I));continue}if(y==="response.reasoning_text.delta"&&!Y){D(),ee||(ee=!0,J=_++,g({type:"response.output_item.added",output_index:J,item:{id:K,type:"reasoning",summary:[],content:[],status:"in_progress"}})),X+=String(I.delta??""),g({type:"response.reasoning_text.delta",item_id:K,output_index:J,content_index:0,delta:I.delta});continue}if(y==="response.output_text.delta"&&!Y){D(),te(),z||(z=!0,N=_++,g({type:"response.output_item.added",output_index:N,item:{id:G,type:"message",role:"assistant",status:"in_progress",content:[]}}),g({type:"response.content_part.added",item_id:G,output_index:N,content_index:0,part:{type:"output_text",text:""}})),H+=String(I.delta??""),g({type:y,item_id:G,output_index:N,content_index:0,delta:I.delta});continue}let L=Y?null:Te(I);if(L){if(D(),te(),Q("completed"),ne.add(L),L.tool_index!==ce){pe("completed"),ce=L.tool_index;let U=_++;$={fcId:`fc_${l.slice(5)}_${L.tool_index}`,outputIndex:U,callId:L.call_id,name:L.name,args:""},g({type:"response.output_item.added",output_index:U,item:{id:$.fcId,type:"function_call",call_id:$.callId??$.fcId,name:$.name??"",arguments:"",status:"in_progress"}})}$&&(L.call_id&&($.callId=L.call_id),L.name&&($.name=L.name),$.args+=L.delta,g({type:"response.function_call_arguments.delta",item_id:$.fcId,output_index:$.outputIndex,delta:L.delta}));continue}if(y==="response.completed"&&!Y){let U=Ie(I.response),R=Se(h,ne.size+U.length,H);if(R){fe=!0,g({type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:xe(p,R)}});break}te(),Q("completed"),pe("completed"),ne.size===0&&U.forEach(ie=>{let he=_++,ge=ie.id??ie.call_id??`fc_${he}`,Ee=ie.arguments??"",Z={id:ge,type:"function_call",call_id:ie.call_id??ge,name:ie.name??""};g({type:"response.output_item.added",output_index:he,item:{...Z,arguments:"",status:"in_progress"}}),g({type:"response.function_call_arguments.delta",item_id:ge,output_index:he,delta:Ee}),g({type:"response.function_call_arguments.done",item_id:ge,output_index:he,arguments:Ee}),g({type:"response.output_item.done",output_index:he,item:{...Z,arguments:Ee,status:"completed"}})}),g(V(I));continue}g(V(I))}te(),Q("incomplete"),pe("incomplete"),d&&!fe&&e.remember(l,i,jt(A,we(A))),r.write(`data: [DONE]
6
-
7
- `),r.end();return}let C=null;for await(let g of o.track(x,p)){if(A.push(g),g.type==="error"){le(r,502,JSON.stringify(g),"upstream_error");return}g.type==="response.completed"&&(C=g.response)}if(C==null){le(r,502,"upstream produced no response.completed event","upstream_error");return}let T=jt(A,C),P=Se(h,Ie(C).length,T);if(P){le(r,502,xe(p,P),"upstream_error");return}d&&e.remember(l,i,T),se(r,200,{...C,id:l})}async function rr(t,n,e,r){let o=t.messages;if(!Array.isArray(o)){le(e,400,"chat/completions requires a messages array");return}let u=String(t.model??"urun"),i=`chatcmpl-${Math.random().toString(36).slice(2,14)}`,l;try{l=tr(o)}catch(_){if(_ instanceof De){le(e,400,_.message);return}throw _}let d=st(t);r.model(d);let p=Array.isArray(t.tools)&&t.tools.length>0,h;try{h=await n.createResponse({model:t.model,input:l,stream:!0,tools:Zn(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}:{}})}catch(_){if(_ instanceof re){ot(e,"openai",_);return}throw _}if(t.stream===!0){rt(e);let _=[],D=new _e,ee="";e.write(`data: ${JSON.stringify(be(i,u,{role:"assistant"},null))}
8
-
9
- `);for await(let N of r.track(h,d)){_.push(N);let H=Te(N);if(N.type==="response.output_text.delta"&&typeof N.delta=="string")ee+=N.delta,e.write(`data: ${JSON.stringify(be(i,u,{content:N.delta},null))}
10
-
11
- `);else if(N.type==="response.reasoning_text.delta"&&typeof N.delta=="string")e.write(`data: ${JSON.stringify(be(i,u,{reasoning_content:N.delta},null))}
12
-
13
- `);else if(H){let{call:Q,isNew:ne}=D.add(H),$=ne?{index:H.tool_index,id:Q.id??`call_${H.tool_index}`,type:"function",function:{name:Q.name??"",arguments:H.delta}}:{index:H.tool_index,function:{arguments:H.delta}};e.write(`data: ${JSON.stringify(be(i,u,{tool_calls:[$]},null))}
14
-
15
- `)}else if(N.type==="error"){e.write(`data: ${JSON.stringify({error:N})}
16
-
17
- `),e.end();return}}let J=Ze(we(_)),X=Se(p,D.size+J.length,ee);if(X){e.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:xe(d,X)}})}
18
-
19
- `),e.end();return}D.size===0&&J.length>0&&e.write(`data: ${JSON.stringify(be(i,u,{tool_calls:J},null))}
20
-
21
- `);let te=D.size>0||J.length>0;e.write(`data: ${JSON.stringify(be(i,u,{},te?"tool_calls":"stop"))}
22
-
23
- `),Le(we(_))&&e.write(`data: ${JSON.stringify({id:i,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:u,choices:[],usage:Dt(we(_))})}
24
-
25
- `),e.write(`data: [DONE]
26
-
27
- `),e.end();return}let x="",A="",C=[],T=new _e;for await(let _ of r.track(h,d)){C.push(_),_.type==="response.output_text.delta"&&typeof _.delta=="string"&&(x+=_.delta),_.type==="response.reasoning_text.delta"&&typeof _.delta=="string"&&(A+=_.delta);let D=Te(_);if(D&&T.add(D),_.type==="error"){le(e,502,JSON.stringify(_),"upstream_error");return}}let P=we(C),g=Ze(P),V=g.length>0?g:Ze({output:T.items()}),G=Se(p,V.length,x);if(G){le(e,502,xe(d,G),"upstream_error");return}let K=V.map(({index:_,...D})=>D),W={role:"assistant",content:x||null},Y=A||Vt(P);Y&&(W.reasoning_content=Y),K.length>0&&(W.tool_calls=K),se(e,200,{id:i,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:u,choices:[{index:0,message:W,finish_reason:K.length>0?"tool_calls":"stop"}],usage:Dt(P)})}function or(t){return typeof t=="string"?t:Array.isArray(t)?t.filter(n=>n.type==="text").map(n=>String(n.text??"")).join(""):""}var Pe=class extends Error{};function je(t){return new Pe(`unsupported anthropic content block type "${t}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function sr(t){if(Array.isArray(t))return t.map(n=>{let e=n;return{type:"function",name:e.name,description:e.description,parameters:e.input_schema}})}function ir(t){if(t==null)return;let n=t.type;if(n==="auto")return"auto";if(n==="none")return"none";if(n==="any")return"required";if(n==="tool")return{type:"function",name:t.name};throw new Pe(`unsupported anthropic tool_choice type ${JSON.stringify(String(n))} \u2014 the proxy maps auto, none, any and tool`)}function ar(t){if(typeof t=="string")return t;if(t==null)return"";if(!Array.isArray(t))throw je(typeof t);let n="";for(let e of t){let r=String(e.type??"");if(r!=="text")throw je(`tool_result > ${r}`);n+=String(e.text??"")}return n}function ur(t){let{role:n,content:e}=t;if(typeof e=="string")return[{role:n,content:e}];if(e==null)return[];if(!Array.isArray(e))throw je(typeof e);let r=[],o="",u=()=>{o&&(r.push({role:n,content:o}),o="")};for(let i of e){let l=String(i.type??"");if(l==="text")o+=String(i.text??"");else if(l==="tool_use")u(),r.push({type:"function_call",call_id:i.id,name:i.name,arguments:JSON.stringify(i.input??{})});else if(l==="tool_result")u(),r.push({type:"function_call_output",call_id:i.tool_use_id,output:ar(i.content)});else{if(l==="thinking"||l==="redacted_thinking")continue;throw je(l)}}return u(),r}function et(t){return Ie(t).map((n,e)=>{let r;try{r=JSON.parse(n.arguments||"{}")}catch{throw new Error(`upstream function_call "${n.name??""}" arguments are not valid JSON`)}return{type:"tool_use",id:n.call_id??n.id??`toolu_${e}`,name:n.name??"",input:r}})}async function cr(t,n,e,r){let o=t.messages;if(!Array.isArray(o)){ue(e,400,"invalid_request_error","messages requires a messages array");return}if(typeof t.max_tokens!="number"||!Number.isInteger(t.max_tokens)||t.max_tokens<1){ue(e,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(t.top_k!==void 0){ue(e,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(_=>typeof _!="string"))){ue(e,400,"invalid_request_error","stop_sequences must be an array of strings");return}let u=String(t.model??"urun"),i=`msg_${Math.random().toString(36).slice(2,14)}`,l=[],d;try{t.system&&l.push({role:"system",content:or(t.system)});for(let _ of o)l.push(...ur(_));d=ir(t.tool_choice)}catch(_){if(_ instanceof Pe){ue(e,400,"invalid_request_error",_.message);return}throw _}let p=st(t);r.model(p);let h=Array.isArray(t.tools)&&t.tools.length>0,x;try{x=await n.createResponse({model:t.model,input:l,stream:!0,tools:sr(t.tools),tool_choice:d,temperature:t.temperature,max_output_tokens:t.max_tokens,top_p:t.top_p,stop:t.stop_sequences})}catch(_){if(_ instanceof re){ot(e,"anthropic",_);return}throw _}let A=(_,D)=>{e.destroyed||e.write(`event: ${_}
28
- data: ${JSON.stringify({type:_,...D})}
29
-
30
- `)};if(t.stream===!0){let _=r.track(x,p);rt(e),A("message_start",{message:{id:i,type:"message",role:"assistant",content:[],model:u,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let D=0,ee="",J=0,X="none",te=()=>X,z=-1,N=-1,H=new _e,Q=[],ne=()=>{X!=="none"&&(X="none",A("content_block_stop",{index:z}))},$=(U,R)=>{ne(),X=U,z=J++,A("content_block_start",{index:z,content_block:R})};for await(let U of _){if(e.destroyed)break;Q.push(U);let R=Te(U);if(U.type==="response.reasoning_text.delta"&&typeof U.delta=="string")te()!=="thinking"&&$("thinking",{type:"thinking",thinking:""}),A("content_block_delta",{index:z,delta:{type:"thinking_delta",thinking:U.delta}});else if(U.type==="response.output_text.delta"&&typeof U.delta=="string")te()!=="text"&&$("text",{type:"text",text:""}),D+=1,ee+=U.delta,A("content_block_delta",{index:z,delta:{type:"text_delta",text:U.delta}});else if(R){let{call:ie}=H.add(R);(te()!=="tool"||R.tool_index!==N)&&(N=R.tool_index,$("tool",{type:"tool_use",id:ie.id??`call_${R.tool_index}`,name:ie.name??"",input:{}})),R.delta&&A("content_block_delta",{index:z,delta:{type:"input_json_delta",partial_json:R.delta}})}else if(U.type==="error"){A("error",{error:{type:"api_error",message:JSON.stringify(U)}}),e.end();return}}if(e.destroyed)return;let ce=we(Q),pe=Ie(ce),fe=Se(h,H.size+pe.length,ee);if(fe){A("error",{error:{type:"api_error",message:xe(p,fe)}}),e.end();return}ne();let I=[];if(H.size===0)try{I=et(ce)}catch(U){A("error",{error:{type:"api_error",message:String(U instanceof Error?U.message:U)}}),e.end();return}J===0&&I.length===0&&($("text",{type:"text",text:""}),ne()),I.forEach(U=>{let R=J++;A("content_block_start",{index:R,content_block:{...U,input:{}}}),A("content_block_delta",{index:R,delta:{type:"input_json_delta",partial_json:JSON.stringify(U.input??{})}}),A("content_block_stop",{index:R})});let y=H.size>0||I.length>0,L=Le(ce);A("message_delta",{delta:{stop_reason:y?"tool_use":"end_turn"},usage:L?{input_tokens:L.input_tokens,output_tokens:L.output_tokens}:{output_tokens:D}}),A("message_stop",{}),e.end();return}let C="",T="",P=[],g=new _e;for await(let _ of r.track(x,p)){P.push(_),_.type==="response.output_text.delta"&&typeof _.delta=="string"&&(C+=_.delta),_.type==="response.reasoning_text.delta"&&typeof _.delta=="string"&&(T+=_.delta);let D=Te(_);if(D&&g.add(D),_.type==="error"){ue(e,502,"api_error",JSON.stringify(_));return}if(e.destroyed)break}if(e.destroyed)return;let V=we(P),G;try{G=et(V),G.length===0&&g.size>0&&(G=et({output:g.items()}))}catch(_){ue(e,502,"api_error",String(_ instanceof Error?_.message:_));return}let K=Se(h,G.length,C);if(K){ue(e,502,"api_error",xe(p,K));return}let W=[],Y=T||Vt(V);Y&&W.push({type:"thinking",thinking:Y}),(C||G.length===0)&&W.push({type:"text",text:C}),W.push(...G),se(e,200,{id:i,type:"message",role:"assistant",content:W,model:u,stop_reason:G.length>0?"tool_use":"end_turn",usage:Le(V)??{input_tokens:0,output_tokens:0}})}function Ht(t){let{clients:n,apiKey:e,identity:r,statusOf:o}=t,u=typeof n=="function"?n:()=>n,i=new WeakMap,l=p=>{let h=i.get(p);return h||(h=new nt,i.set(p,h)),h},d=new tt;return(p,h)=>{let x=new URL(p.url??"/","http://localhost"),A=x.pathname==="/v1/messages"||x.pathname.startsWith("/v1/messages/"),C=(T,P,g,V)=>A?ue(h,T,V,P):le(h,T,P,g);(async()=>{if(p.method==="GET"&&x.pathname==="/healthz"){se(h,200,{ok:!0});return}if(p.method==="GET"&&x.pathname==="/stats"){se(h,200,r?{identity:r,...d.snapshot()}:d.snapshot());return}if(e&&(p.headers.authorization??"")!==`Bearer ${e}`){C(401,"invalid local proxy api key","authentication_error","authentication_error");return}let T=await u(p);if(p.method==="GET"&&x.pathname==="/v1/models"){if(x.searchParams.get("format")==="openrouter"){if(!T.openRouterModels){C(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}se(h,200,await T.openRouterModels());return}se(h,200,await T.listModels());return}if(p.method!=="POST"){C(404,`no route for ${p.method} ${x.pathname}`,"invalid_request_error","not_found_error");return}let P;try{P=await zn(p)}catch(V){if(V instanceof $e){C(413,V.message,"request_too_large","request_too_large");return}C(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let g=Buffer.byteLength(JSON.stringify(P));if(x.pathname==="/v1/responses"){d.request("responses",g,g),await nr(P,T,l(T),h,d);return}if(x.pathname==="/v1/chat/completions"){d.request("chat_completions",g,g),await rr(P,T,h,d);return}if(x.pathname==="/v1/messages"){d.request("messages",g,g),await cr(P,T,h,d);return}if(x.pathname==="/v1/messages/count_tokens"){d.request("count_tokens",g,g),ue(h,404,"not_found_error","count_tokens is not implemented by the uRun proxy: the serve transport exposes no token-count lane, and the proxy will not fabricate counts");return}C(404,`no route for POST ${x.pathname}`,"invalid_request_error","not_found_error")})().catch(T=>{let P=String(T instanceof Error?T.message:T);if(!h.headersSent){let g=o?.(T)??null;if(g){C(g.status,P,g.openaiType,g.anthropicType);return}C(500,P,"proxy_error","api_error");return}h.destroyed||(A?h.write(`event: error
31
- data: ${JSON.stringify({type:"error",error:{type:"api_error",message:P}})}
32
-
33
- `):h.write(`data: ${JSON.stringify({error:{message:P,type:"upstream_error"}})}
34
-
35
- `)),h.end()})}}S();var He=require("@urun-sh/core"),me=class extends Error{constructor(e,r=401){super(e);this.status=r;this.name="ProxyAuthError"}status},Re=class extends Error{constructor(n){super(n),this.name="ControlPlaneUnavailableError"}};function Bt(t){let n=(t??"").trim();if(n==="")throw new me("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let e=/^Bearer[ \t]+(\S.*)$/i.exec(n);if(!e)throw new me("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return e[1].trim()}var lr=60,dr=6e4,pr=4096;function fr(t){return t===401||t===403}var Ve=class{constructor(n){this.opts=n;this.mint=n.mint??He.createClientToken,this.now=n.now??Date.now}opts;cache=new Map;mint;now;async resolve(n){if(n==="")throw new me("empty Authorization bearer token");let e=this.cache.get(n);if(e&&this.now()-e.at<dr)return{apiKey:n,orgId:e.orgId};let r;try{r=(await this.mint(n,{baseUrl:this.opts.gatewayUrl,expiresIn:lr})).orgId}catch(o){throw o instanceof He.ClientTokenError&&fr(o.status)?(this.cache.delete(n),new me("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new Re(`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 Re("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>=pr){let o=this.cache.keys().next();o.done||this.cache.delete(o.value)}return this.cache.set(n,{orgId:r,at:this.now()}),{apiKey:n,orgId:r}}clear(){this.cache.clear()}};S();var Zt=require("crypto");S();var Ce=require("@urun-sh/core");S();async function Ft(t){let e=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(!e.ok)throw new Error(`model_catalog fetch failed: ${e.status}`);return await e.json()}S();S();S();function Gt(t,n,e,r){let o=t.response??{},u={request_id:e,consumer_id:r,stream:!0,kind:"chat",messages:n};return typeof o.instructions=="string"&&(u.instructions=o.instructions),Array.isArray(o.modalities)&&(u.modalities=o.modalities),typeof o.temperature=="number"&&(u.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(u.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(u.tools=o.tools),u}function qt(t,n,e){let r={request_id:n,consumer_id:e,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),r}S();S();function Jt(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 n=t,e=n.body??{};return{type:"error",error:{type:"urun_error",code:n.code??null,message:e.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(t)}}}var mr="llm-resp";async function*it(t,n){let e=`${mr}:${n}`,r=`resp_${n}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};let o=new Map;for await(let u of t.stream(e).messages()){let i=u;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 l of i.tool_calls){let d=typeof l.index=="number"?l.index:0,p=o.get(d)??{};l.id&&(p.id=l.id),l.function?.name&&(p.name=l.function.name),o.set(d,p),yield{type:"response.function_call_arguments.delta",item_id:`fc_${n}_${d}`,tool_index:d,call_id:p.id,name:p.name,delta:l.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 Jt(i);return}}}var hr="llm",Be=class{constructor(n){this.session=n;this.sessionTag=n.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(n){n.session_tag=this.sessionTag,this.session.doc(hr).set({requests:{[n.request_id]:{payload:n,consumer_id:n.consumer_id,stream:n.stream}}})}sendResponses(n,e){let r=qt(n,e,this.consumerId);return this.write(r),it(this.session,e)}sendResponseCreate(n,e,r){let o=Gt(n,e,r,this.consumerId);return this.write(o),it(this.session,r)}};var Wt=0;function gr(){return Wt+=1,`req_${Date.now().toString(36)}_${Wt.toString(36)}`}var Fe=class{transport;constructor(n){this.transport=new Be(n)}responses={create:async n=>{let e=gr(),r=this.transport.sendResponses(n,e);return Object.assign((async function*(){yield*r})(),{requestId:e})}}};S();function yr(t){let n=t?.type;return n==="response.created"||n==="response.in_progress"}function wr(t){let n=t?.type;return n==="response.completed"||n==="error"}function zt(t){return async function(e){return(async function*(){for(let o=1;o<=2;o++){let{app:u,entry:i}=await t.sessionFor(e.model),l=!1,d=!1;try{let p=await t.createStream(i,e);for await(let h of p){if(wr(h)&&(l=!0),yr(h)){if(o>1)continue}else d=!0;yield h}}catch(p){if(o<2&&!d){await t.evict(u,i);continue}throw p}if(l)return;if(o<2&&!d){await t.evict(u,i);continue}return}})()}}S();var _r=3,vr="llm",kr=new Set(["paused","error"]);function Kt(t,n){let e=t.doc(vr),r=typeof e.onConnectionState=="function",o=typeof t.onPhase=="function";if(!r&&!o)throw new Error(`session for app "${n}" 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 u=null,i=new Set,l=[],d=p=>{if(!u){u=p;for(let h of[...i])h(p);i.clear()}};return r&&l.push(e.onConnectionState(p=>{p.consecutiveFailures<_r||d(new ae(`the pooled session for app "${n}" is gone: its doc backhaul failed ${p.consecutiveFailures} consecutive times (last close ${p.lastCloseCode===null?"by provider liveness reap":`code ${p.lastCloseCode}`}${p.lastCloseReason?`, reason ${JSON.stringify(p.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&&l.push(t.onPhase(p=>{kr.has(p.name)&&d(new ae(`the pooled session for app "${n}" lost its compute binding (phase "${p.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:()=>u,onGone(p){return u?(p(u),()=>{}):(i.add(p),()=>i.delete(p))},dispose(){i.clear();for(let p of l.splice(0))p()}}}async function*at(t,n){let e=n.gone();if(e)throw e;let r=t[Symbol.asyncIterator](),o,u=new Promise((i,l)=>{o=n.onGone(d=>l(d))});u.catch(()=>{});try{for(;;){let i=await Promise.race([r.next(),u]);if(i.done)return;yield i.value}}finally{o?.(),r.return?.(void 0)}}function Yt(t,n,e){let r=t[e];if(typeof r!="function")throw new Error(`app "${n}" has no function "${e}" (set URUN_FUNCTION)`);let o=r();if(typeof o.end!="function")throw new Error(`app "${n}" function "${e}" returned a session without end()`);return o}function br(t,n){let{baseUrl:e,orgId:r,fnName:o,auth:u,subject:i}=t,l=x=>({session:x,responses:new Fe(x),gone:Kt(x,n)});if(u.lane==="jwt"){let x=(0,Ce.App)(n,{baseUrl:e,orgId:r,jwt:u.jwt});return l(Yt(x,n,o))}let{apiKey:d,gatewayUrl:p}=u,h=(0,Ce.App)(n,{baseUrl:e,orgId:r,getAccessToken:async()=>(await(0,Ce.createClientToken)(d,{baseUrl:p,expiresIn:300,allowedFunctions:[`${n}/${o}`],...i===void 0?{}:{subject:i}})).token});return l(Yt(h,n,o))}function xr(t){let n=t.session.id;if(typeof n!="string"||n.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 n}var Sr=new Set(["error","paused"]);function Xt(t){let{auth:n,catalog:e}=t,r=null,o=i=>{let l=br(t,i);return l.gone.onGone(()=>{r?.evict(i,l)}),l},u=new Ue({defaultApp:t.defaultApp,fnName:t.fnName,openSession:o,closeSession:async i=>{i.gone.dispose(),await i.session.end()},listApps:n.lane==="api-key"?()=>At({apiUrl:t.apiUrl,apiKey:n.apiKey}):null,listCatalog:e?()=>Ft(e):null,sessionKey:xr});return r=u,t.defaultApp!==null&&u.seed(t.defaultApp,o(t.defaultApp)),u}function Qt(t,n=Nt){let e=new WeakMap,r=new WeakMap,o=new WeakMap;return{createResponse:zt({sessionFor:u=>t.sessionFor(u),evict:(u,i)=>t.evict(u,i),createStream:async(u,i)=>at(await u.responses.responses.create(i),u.gone)}),listModels:()=>t.modelList(),openRouterModels:()=>t.openRouterModels(),openAudio:async u=>{let{entry:i}=await t.sessionFor(u),l=e.get(i);return l||(l=(async()=>Ut(i.session,await n()))(),e.set(i,l),l.catch(()=>e.delete(i))),l},openVideo:async u=>{let{entry:i}=await t.sessionFor(u),l=r.get(i);return l||(l=Mt(i.session),r.set(i,l)),l},openVideoOut:async u=>{let{entry:i}=await t.sessionFor(u),l=o.get(i);return l||(l=$t(i.session),o.set(i,l)),l},sessionHandle:async u=>(await t.handleFor(u)).handle,createResponseOn:async(u,i)=>{let{entry:l}=await t.sessionForHandle(u);return at(await l.responses.responses.create(i),l.gone)},onSessionEnd:async(u,i)=>{let{entry:l}=await t.sessionForHandle(u),d=l.session;if(typeof d.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let p=!1;return d.onPhase(h=>{if(p||!Sr.has(h.name))return;p=!0;let x=d.endsAt??null;i({timeLeftMs:x?Math.max(0,x.getTime()-Date.now()):null,reason:h.reason??`session ${h.name}`})})}}}var Rr=512;function Er(t){return`urun-inference-proxy-${(0,Zt.createHash)("sha256").update(t).digest("hex").slice(0,32)}`}var Ge=class{constructor(n){this.opts=n}opts;tenants=new Map;build(n){if(this.opts.build)return this.opts.build(n);let e=Xt({baseUrl:this.opts.baseUrl,orgId:n.orgId,defaultApp:null,fnName:It,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:n.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:Er(n.apiKey)});return{router:e,clients:Qt(e)}}clientsFor(n){let e=this.tenants.get(n.apiKey);if(e)return this.tenants.delete(n.apiKey),this.tenants.set(n.apiKey,e),e.clients;for(;this.tenants.size>=Rr;){let o=this.tenants.entries().next();if(o.done)break;let[u,i]=o.value;this.tenants.delete(u),i.router.closeAll().catch(()=>{})}let r=this.build(n);return this.tenants.set(n.apiKey,r),r.clients}get size(){return this.tenants.size}async closeAll(){let n=[...this.tenants.values()].map(r=>r.router);this.tenants.clear();let e=[];if(await Promise.all(n.map(async r=>{try{await r.closeAll()}catch(o){e.push(String(o instanceof Error?o.message:o))}})),e.length>0)throw new Error(`failed to close ${e.length} tenant backhaul(s): ${e.join("; ")}`)}};var Ar=5e3,Or=3e3,Ir="/api/client-tokens";function ut(t,n,e){t.writeHead(n,{"content-type":"application/json"}),t.end(JSON.stringify(e))}function en(t,n,e,r){ut(t,n,{error:{message:e,type:r}})}async function Tr(t,n=fetch){let e=`${t.replace(/\/+$/,"")}${Ir}`;try{let r=await n(e,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(Or)});return r.status===404?{ready:!1,detail:`${e} answered 404 \u2014 this is not a uRun control plane`}:r.status>=500?{ready:!1,detail:`${e} answered HTTP ${r.status}`}:{ready:!0,detail:`${e} answered HTTP ${r.status}`}}catch(r){return{ready:!1,detail:`${e} unreachable: ${r instanceof Error?r.message:String(r)}`}}}function nn(t){let n=t.resolver??new Ve({gatewayUrl:t.baseUrl}),e=t.registry??new Ge({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),r=t.probeControlPlane??(()=>Tr(t.baseUrl)),o=async d=>n.resolve(Bt(d.headers.authorization)),u=Ht({clients:async d=>e.clientsFor(await o(d)),statusOf:d=>d instanceof me?{status:d.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:d instanceof Re?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null}),i=null;return{server:(0,tn.createServer)((d,p)=>{let h=new URL(d.url??"/","http://localhost");(async()=>{if(d.method==="GET"&&h.pathname==="/healthz"){ut(p,200,{ok:!0});return}if(d.method==="GET"&&h.pathname==="/readyz"){let x=Date.now();if(!i||x-i.at>=Ar){let A=await r();i={at:x,...A}}ut(p,i.ready?200:503,{ok:i.ready,control_plane:i.detail});return}if(h.pathname!=="/v1"&&!h.pathname.startsWith("/v1/")){en(p,404,`no route for ${d.method} ${h.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface plus /healthz and /readyz`,"invalid_request_error");return}u(d,p)})().catch(x=>{if(!p.headersSent){en(p,500,String(x instanceof Error?x.message:x),"proxy_error");return}p.end()})}),closeAll:()=>e.closeAll()}}async function rn(t,n){let e=new Promise(r=>t.close(()=>r()));t.closeAllConnections(),await e,await n()}async function Pr(){let t=Tt(de.default.env),{server:n,closeAll:e}=nn(t);await new Promise((o,u)=>{n.once("error",u),n.listen(t.port,Ye,o)}),de.default.stdout.write(`inference-proxy listening on http://${Ye}:${t.port}/v1 (control plane ${t.baseUrl}, org API ${t.apiUrl}, catalog oracle ${t.catalog?"on":"off"})
36
- `);let r=async o=>{de.default.stdout.write(`inference-proxy: ${o} \u2014 draining
37
- `),await rn(n,e)};await new Promise(o=>{de.default.once("SIGINT",()=>{r("SIGINT").finally(o)}),de.default.once("SIGTERM",()=>{r("SIGTERM").finally(o)})})}Pr().then(()=>de.default.exit(0),t=>{de.default.stderr.write(`inference-proxy: ${String(t instanceof Error?t.stack??t.message:t)}
38
- `),de.default.exit(1)});
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
12
+ Connection: close\r
13
+ \r
14
+ ${g}\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
18
+ Connection: close\r
19
+ Content-Type: text/plain; charset=utf-8\r
20
+ Content-Length: ${Buffer.byteLength(o)}\r
21
+ \r
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)}
29
+
30
+ `);r.write(`data: [DONE]
31
+
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))}
33
+
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))}
35
+
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))}
37
+
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))}
39
+
40
+ `)}else if(ne.type==="error"){o.streamError=!0,n.write(`data: ${JSON.stringify({error:ne})}
41
+
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)}})}
43
+
44
+ `),n.end();return}H.size===0&&z.length>0&&n.write(`data: ${JSON.stringify(rt(a,s,{tool_calls:z},null))}
45
+
46
+ `);let ge=H.size>0||z.length>0;n.write(`data: ${JSON.stringify(rt(a,s,{},ge?"tool_calls":"stop"))}
47
+
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))})}
49
+
50
+ `),n.write(`data: [DONE]
51
+
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})}
54
+
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}})}
57
+
58
+ `):l.write(`data: ${JSON.stringify({error:{message:T,type:"upstream_error"}})}
59
+
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
61
+ Connection: close\r
62
+ \r
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)});
39
67
  /*! Bundled license information:
40
68
 
41
69
  reflect-metadata/Reflect.js: