@urun-sh/openai 0.5.3 → 0.5.5
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.
- package/dist/chunk-5BZCM3RS.js +4 -0
- package/dist/chunk-DMD5UENK.js +1 -0
- package/dist/{chunk-G3NMGP4N.js → chunk-GBBY3PCZ.js} +1 -1
- package/dist/chunk-I2Q3B3OG.js +6 -0
- package/dist/chunk-K23AZPI4.js +1 -0
- package/dist/chunk-OI2OY32M.js +1 -0
- package/dist/chunk-QVF7NF7G.js +44 -0
- package/dist/gemini-live.cjs +2 -2
- package/dist/gemini-live.d.cts +6 -0
- package/dist/gemini-live.d.ts +6 -0
- package/dist/gemini-live.js +1 -1
- package/dist/hosted/bin.cjs +36 -22
- package/dist/hosted/bin.js +4 -4
- package/dist/hosted/index.cjs +32 -18
- package/dist/hosted/index.d.cts +140 -198
- package/dist/hosted/index.d.ts +63 -82
- package/dist/hosted/index.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -18
- package/dist/index.d.ts +2 -18
- package/dist/index.js +1 -1
- package/dist/models-NYMZrklp.d.cts +38 -0
- package/dist/models-NYMZrklp.d.ts +38 -0
- package/dist/pi-extension/index.cjs +6 -6
- package/dist/pi-extension/index.js +1 -1
- package/dist/pi-extension/standalone.cjs +49 -49
- package/dist/proxy/cli.cjs +41 -37
- package/dist/proxy/cli.js +1 -1
- package/dist/proxy/index.cjs +30 -21
- package/dist/proxy/index.d.cts +468 -2
- package/dist/proxy/index.d.ts +173 -2
- package/dist/proxy/index.js +6 -1
- package/dist/responses-turn-KOAoIqZ-.d.ts +200 -0
- package/dist/responses-turn-OrO4euEN.d.cts +513 -0
- package/package.json +3 -3
- package/dist/chunk-F2TEK34X.js +0 -1
- package/dist/chunk-FVD4NMXJ.js +0 -1
- package/dist/chunk-M6ICU4F5.js +0 -40
- package/dist/chunk-ODHCPZHI.js +0 -6
- package/dist/chunk-UVAY7Q7Z.js +0 -1
- package/dist/chunk-XHIIEA6Z.js +0 -1
- package/dist/server-BfME37pQ.d.cts +0 -200
- package/dist/server-l1himPxc.d.ts +0 -82
package/dist/hosted/bin.cjs
CHANGED
|
@@ -1,41 +1,55 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
"use strict";var an=Object.create;var gt=Object.defineProperty;var un=Object.getOwnPropertyDescriptor;var cn=Object.getOwnPropertyNames;var ln=Object.getPrototypeOf,dn=Object.prototype.hasOwnProperty;var pn=(t,n,e)=>()=>{if(e)throw e[0];try{return t&&(n=t(t=0)),n}catch(r){throw e=[r],r}};var fn=(t,n)=>()=>{try{return n||t((n={exports:{}}).exports,n),n.exports}catch(e){throw n=0,e}};var mn=(t,n,e,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of cn(n))!dn.call(t,o)&&o!==e&>(t,o,{get:()=>n[o],enumerable:!(r=un(n,o))||r.enumerable});return t};var ve=(t,n,e)=>(e=t!=null?an(ln(t)):{},mn(n||!t||!t.__esModule?gt(e,"default",{value:t,enumerable:!0}):e,t));var S=pn(()=>{"use strict"});var wt=fn(()=>{"use strict";S();var yt;(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 a(){try{return(0,eval)("(function() { return this; })()")}catch{}}function l(){return u()||a()}})(function(n,e){var r=Object.prototype.hasOwnProperty,o=typeof Symbol=="function",u=o&&typeof Symbol.toPrimitive<"u"?Symbol.toPrimitive:"@@toPrimitive",a=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 Je(Object.create(null))}:d?function(){return Je({__proto__:null})}:function(){return Je({})},has:p?function(s,i){return r.call(s,i)}:function(s,i){return i in s},get:p?function(s,i){return r.call(s,i)?s[i]:void 0}:function(s,i){return s[i]}},x=Object.getPrototypeOf(Function),A=typeof Map=="function"&&typeof Map.prototype.entries=="function"?Map:tn(),U=typeof Set=="function"&&typeof Set.prototype.entries=="function"?Set:nn(),P=typeof WeakMap=="function"?WeakMap:rn(),T=o?Symbol.for("@reflect-metadata:registry"):void 0,g=Qt(),F=Zt(g);function G(s,i,c,f){if(y(c)){if(!ut(s))throw new TypeError;if(!ct(i))throw new TypeError;return z(s,i)}else{if(!ut(s))throw new TypeError;if(!R(i))throw new TypeError;if(!R(f)&&!y(f)&&!L(f))throw new TypeError;return L(f)&&(f=void 0),c=Z(c),N(s,i,c,f)}}n("decorate",G);function K(s,i){function c(f,b){if(!R(f))throw new TypeError;if(!y(b)&&!Yt(b))throw new TypeError;ce(s,i,f,b)}return c}n("metadata",K);function W(s,i,c,f){if(!R(c))throw new TypeError;return y(f)||(f=Z(f)),ce(s,i,c,f)}n("defineMetadata",W);function Y(s,i,c){if(!R(i))throw new TypeError;return y(c)||(c=Z(c)),j(s,i,c)}n("hasMetadata",Y);function _(s,i,c){if(!R(i))throw new TypeError;return y(c)||(c=Z(c)),Q(s,i,c)}n("hasOwnMetadata",_);function D(s,i,c){if(!R(i))throw new TypeError;return y(c)||(c=Z(c)),ne(s,i,c)}n("getMetadata",D);function ee(s,i,c){if(!R(i))throw new TypeError;return y(c)||(c=Z(c)),$(s,i,c)}n("getOwnMetadata",ee);function J(s,i){if(!R(s))throw new TypeError;return y(i)||(i=Z(i)),pe(s,i)}n("getMetadataKeys",J);function X(s,i){if(!R(s))throw new TypeError;return y(i)||(i=Z(i)),fe(s,i)}n("getOwnMetadataKeys",X);function te(s,i,c){if(!R(i))throw new TypeError;if(y(c)||(c=Z(c)),!R(i))throw new TypeError;y(c)||(c=Z(c));var f=Ee(i,c,!1);return y(f)?!1:f.OrdinaryDeleteMetadata(s,i,c)}n("deleteMetadata",te);function z(s,i){for(var c=s.length-1;c>=0;--c){var f=s[c],b=f(i);if(!y(b)&&!L(b)){if(!ct(b))throw new TypeError;i=b}}return i}function N(s,i,c,f){for(var b=s.length-1;b>=0;--b){var V=s[b],q=V(i,c,f);if(!y(q)&&!L(q)){if(!R(q))throw new TypeError;f=q}}return f}function j(s,i,c){var f=Q(s,i,c);if(f)return!0;var b=qe(i);return L(b)?!1:j(s,b,c)}function Q(s,i,c){var f=Ee(i,c,!1);return y(f)?!1:ge(f.OrdinaryHasOwnMetadata(s,i,c))}function ne(s,i,c){var f=Q(s,i,c);if(f)return $(s,i,c);var b=qe(i);if(!L(b))return ne(s,b,c)}function $(s,i,c){var f=Ee(i,c,!1);if(!y(f))return f.OrdinaryGetOwnMetadata(s,i,c)}function ce(s,i,c,f){var b=Ee(c,f,!0);b.OrdinaryDefineOwnMetadata(s,i,c,f)}function pe(s,i){var c=fe(s,i),f=qe(s);if(f===null)return c;var b=pe(f,i);if(b.length<=0)return c;if(c.length<=0)return b;for(var V=new U,q=[],E=0,m=c;E<m.length;E++){var w=m[E],v=V.has(w);v||(V.add(w),q.push(w))}for(var k=0,O=b;k<O.length;k++){var w=O[k],v=V.has(w);v||(V.add(w),q.push(w))}return q}function fe(s,i){var c=Ee(s,i,!1);return c?c.OrdinaryOwnMetadataKeys(s,i):[]}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 C(s){return typeof s=="symbol"}function R(s){return typeof s=="object"?s!==null:typeof s=="function"}function se(s,i){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=i===3?"string":i===5?"number":"default",f=lt(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,i){if(i==="string"){var c=s.toString;if(_e(c)){var f=c.call(s);if(!R(f))return f}var b=s.valueOf;if(_e(b)){var f=b.call(s);if(!R(f))return f}}else{var b=s.valueOf;if(_e(b)){var f=b.call(s);if(!R(f))return f}var V=s.toString;if(_e(V)){var f=V.call(s);if(!R(f))return f}}throw new TypeError}function ge(s){return!!s}function Re(s){return""+s}function Z(s){var i=se(s,3);return C(i)?i:Re(i)}function ut(s){return Array.isArray?Array.isArray(s):s instanceof Object?s instanceof Array:Object.prototype.toString.call(s)==="[object Array]"}function _e(s){return typeof s=="function"}function ct(s){return typeof s=="function"}function Yt(s){switch(I(s)){case 3:return!0;case 4:return!0;default:return!1}}function Ge(s,i){return s===i||s!==s&&i!==i}function lt(s,i){var c=s[i];if(c!=null){if(!_e(c))throw new TypeError;return c}}function dt(s){var i=lt(s,a);if(!_e(i))throw new TypeError;var c=i.call(s);if(!R(c))throw new TypeError;return c}function pt(s){return s.value}function ft(s){var i=s.next();return i.done?!1:i}function mt(s){var i=s.return;i&&i.call(s)}function qe(s){var i=Object.getPrototypeOf(s);if(typeof s!="function"||s===x||i!==x)return i;var c=s.prototype,f=c&&Object.getPrototypeOf(c);if(f==null||f===Object.prototype)return i;var b=f.constructor;return typeof b!="function"||b===s?i:b}function Xt(){var s;!y(T)&&typeof e.Reflect<"u"&&!(T in e.Reflect)&&typeof e.Reflect.defineMetadata=="function"&&(s=en(e.Reflect));var i,c,f,b=new P,V={registerProvider:q,getProvider:m,setProvider:v};return V;function q(k){if(!Object.isExtensible(V))throw new Error("Cannot add provider to a frozen registry.");switch(!0){case s===k:break;case y(i):i=k;break;case i===k:break;case y(c):c=k;break;case c===k:break;default:f===void 0&&(f=new U),f.add(k);break}}function E(k,O){if(!y(i)){if(i.isProviderFor(k,O))return i;if(!y(c)){if(c.isProviderFor(k,O))return i;if(!y(f))for(var M=dt(f);;){var H=ft(M);if(!H)return;var oe=pt(H);if(oe.isProviderFor(k,O))return mt(M),oe}}}if(!y(s)&&s.isProviderFor(k,O))return s}function m(k,O){var M=b.get(k),H;return y(M)||(H=M.get(O)),y(H)&&(H=E(k,O),y(H)||(y(M)&&(M=new A,b.set(k,M)),M.set(O,H))),H}function w(k){if(y(k))throw new TypeError;return i===k||c===k||!y(f)&&f.has(k)}function v(k,O,M){if(!w(M))throw new Error("Metadata provider not registered.");var H=m(k,O);if(H!==M){if(!y(H))return!1;var oe=b.get(k);y(oe)&&(oe=new A,b.set(k,oe)),oe.set(O,M)}return!0}}function Qt(){var s;return!y(T)&&R(e.Reflect)&&Object.isExtensible(e.Reflect)&&(s=e.Reflect[T]),y(s)&&(s=Xt()),!y(T)&&R(e.Reflect)&&Object.isExtensible(e.Reflect)&&Object.defineProperty(e.Reflect,T,{enumerable:!1,configurable:!1,writable:!1,value:s}),s}function Zt(s){var i=new P,c={isProviderFor:function(w,v){var k=i.get(w);return y(k)?!1:k.has(v)},OrdinaryDefineOwnMetadata:q,OrdinaryHasOwnMetadata:b,OrdinaryGetOwnMetadata:V,OrdinaryOwnMetadataKeys:E,OrdinaryDeleteMetadata:m};return g.registerProvider(c),c;function f(w,v,k){var O=i.get(w),M=!1;if(y(O)){if(!k)return;O=new A,i.set(w,O),M=!0}var H=O.get(v);if(y(H)){if(!k)return;if(H=new A,O.set(v,H),!s.setProvider(w,v,c))throw O.delete(v),M&&i.delete(w),new Error("Wrong provider for target.")}return H}function b(w,v,k){var O=f(v,k,!1);return y(O)?!1:ge(O.has(w))}function V(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(),H=dt(M),oe=0;;){var ht=ft(H);if(!ht)return k.length=oe,k;var on=pt(ht);try{k[oe]=on}catch(sn){try{mt(H)}finally{throw sn}}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=i.get(v);y(M)||(M.delete(k),M.size===0&&i.delete(M))}return!0}}function en(s){var i=s.defineMetadata,c=s.hasOwnMetadata,f=s.getOwnMetadata,b=s.getOwnMetadataKeys,V=s.deleteMetadata,q=new P,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 U,q.set(m,v)),v.add(w),!0):!1},OrdinaryDefineOwnMetadata:i,OrdinaryHasOwnMetadata:c,OrdinaryGetOwnMetadata:f,OrdinaryOwnMetadataKeys:b,OrdinaryDeleteMetadata:V};return E}function Ee(s,i,c){var f=g.getProvider(s,i);if(!y(f))return f;if(c){if(g.setProvider(s,i,F))return F;throw new Error("Illegal state.")}}function tn(){var s={},i=[],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[a]=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=i,this._values=i):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=i,this._values=i),m},E.prototype.return=function(m){return this._index>=0&&(this._index=-1,this._keys=i,this._values=i),{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--,Ge(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,V)},E.prototype.entries=function(){return new c(this._keys,this._values,q)},E.prototype["@@iterator"]=function(){return this.entries()},E.prototype[a]=function(){return this.entries()},E.prototype._find=function(m,w){if(!Ge(this._cacheKey,m)){this._cacheIndex=-1;for(var v=0;v<this._keys.length;v++)if(Ge(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 V(E,m){return m}function q(E,m){return[E,m]}}function nn(){var s=(function(){function i(){this._map=new A}return Object.defineProperty(i.prototype,"size",{get:function(){return this._map.size},enumerable:!0,configurable:!0}),i.prototype.has=function(c){return this._map.has(c)},i.prototype.add=function(c){return this._map.set(c,c),this},i.prototype.delete=function(c){return this._map.delete(c)},i.prototype.clear=function(){this._map.clear()},i.prototype.keys=function(){return this._map.keys()},i.prototype.values=function(){return this._map.keys()},i.prototype.entries=function(){return this._map.entries()},i.prototype["@@iterator"]=function(){return this.keys()},i.prototype[a]=function(){return this.keys()},i})();return s}function rn(){var s=16,i=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(i,m));return i[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 V(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):V(w,m),w}return V(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 Je(s){return s.__=void 0,delete s.__,s}})})(yt||(yt={}))});S();var cs=ve(wt(),1),de=ve(require("process"),1);S();S();var wn=ve(require("os"),1),_n=ve(require("path"),1),vn=require("url"),kn=require("util");S();function We(t){return t.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}var re=class extends Error{},ie=class extends Error{},ze="urs1.";function hn(t,n){return ze+Buffer.from(JSON.stringify([t,n]),"utf8").toString("base64url")}function gn(t){if(!t.startsWith(ze))return null;try{let n=JSON.parse(Buffer.from(t.slice(ze.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 _t(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 yn=6e4,Ce=class{constructor(n){this.opts=n}opts;pool=new Map;appsCache=null;seed(n,e){this.pool.set(n,Promise.resolve(e))}async deployedApps(){if(!this.opts.listApps)return[];let n=this.opts.appsTtlMs??yn;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}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=We(e);if(o===r)return r;if(this.pool.has(o))return o;if(this.opts.listApps){let u=await this.deployedApps(),a=this.servable(u);if(a.includes(o))return o;let l=a.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 a=(await this.opts.listCatalog()).find(l=>e===l.model_id||e===`${l.model_id}:${l.variant}`||o===We(l.model_id)||o===We(`${l.model_id}-${l.variant}`));if(a)throw new re(`model "${e}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${a.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:hn(e,this.keyOf(r))}}async sessionForHandle(n){let e=gn(n);if(!e)throw new ie(`malformed session handle ${JSON.stringify(n)} \u2014 not issued by this proxy`);let{app:r,key:o}=e,u=this.pool.get(r),a=u?await u.then(l=>l,()=>null):null;if(a===null)throw new ie(`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(a)!==o)throw new ie(`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:a}}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(){return{object:"list",data:(await this.availableIds()).map(e=>({id:e,object:"model",created:0,owned_by:"urun"}))}}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 vt(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 Ue(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 bn=8080,kt="serve",Ke="0.0.0.0";function xn(t,n,e){let r=(t[n]??"").trim();if(!r)throw new Error(`${n} is not set \u2014 ${e}`);return r}function bt(t=process.env){let n=(t.PORT??"").trim(),e=n===""?bn: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=vt(Ue(xn(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?Ue(o,"URUN_API_URL"):`${r.replace(/\/+$/,"")}/v1`,a=(t.URUN_CATALOG_URL??"").trim(),l=(t.URUN_CATALOG_ANON_KEY??"").trim();if(!!a!=!!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=a?{catalogUrl:Ue(a,"URUN_CATALOG_URL"),anonKey:l}:null;return{port:e,baseUrl:r,apiUrl:u,catalog:d}}S();var Wt=require("http");S();var $n=require("http");S();var Mn=require("crypto"),Nn=require("ws");S();S();var Ye=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 Rt(t,n,e=1e4){let r=new Ye(n,{sampleRate:24e3}),o=await r.startOutbound();await t.stream("rt-audio-in").attach(o);let u=await Sn(t,e);return await r.startInbound(u),r}function Sn(t,n){let e=t.stream("rt-audio-out");return e.track!=null?Promise.resolve(e.track):new Promise((r,o)=>{let u,a=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(a),u?.(),r(d))};u=e.on?.("track",l),e.track!=null&&l(e.track)})}var xt="rt-video-in",St=256e3;function Et(t){let n=t.stream(xt),e=n.emit;if(typeof e!="function")throw new Error(`enableSessionVideo: session.stream('${xt}') 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>St)throw new Error(`enableSessionVideo: frame is ${r.byteLength} bytes \u2014 over the ${St}-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 Rn=24e3,Ae=480,En=111;async function An(){let t=await import("opusscript"),n=t.default??t;return new n(Rn,1,2048)}async function At(){let t=await import("werift"),n=await An();return{createSource:()=>{let e=new t.MediaStreamTrack({kind:"audio"}),r=Math.random()*4294967295>>>0,o=Math.random()*65535&65535,u=Math.random()*4294967295>>>0,a=new Int16Array(0);return{track:e,onData:l=>{let d=new Int16Array(a.length+l.length);d.set(a,0),d.set(l,a.length);let p=0;for(;d.length-p>=Ae;){let h=d.subarray(p,p+Ae);p+=Ae;let x=Buffer.from(h.buffer,h.byteOffset,h.byteLength),A=n.encode(x,Ae);o=o+1&65535,u=u+Ae>>>0;let U=new t.RtpHeader({version:2,payloadType:En,sequenceNumber:o,timestamp:u,ssrc:r,marker:!1}),P=new t.RtpPacket(U,A);e.writeRtp(P)}a=d.slice(p)}}},createSink:e=>{let r={onframe:null};return e.onReceiveRtp.subscribe(u=>{let a;try{a=n.decode(u.payload)}catch{return}let l=new Int16Array(a.length/2);for(let d=0;d<l.length;d++)l[d]=a.readInt16LE(d*2);r.onframe?.({samples:l})}),r}}}S();var Me=require("@urun-sh/core"),Xe="rt-video-out";var On="h264";function In(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 Tn(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(a){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(a)})`)}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;In(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 Ot(t){let n=t.stream(Xe);if(typeof n.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${Xe}') 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)},a=!1,l=async()=>{for await(let d of n.messages()){if(!(d instanceof Uint8Array)){u(new Error(`openSessionVideoOutLane: "${Xe}" delivered a non-BINARY \xA75 payload (${typeof d}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let p;try{p=Tn(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:On,onOutputFrame:d=>(e.add(d),a||(a=!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 Cn=24e3,Gr=`audio/pcm;rate=${Cn}`,Un=Buffer.alloc(960).toString("base64");var et=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>=Ln?"(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,a=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()),a+=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;a>0&&l>0&&(r.tokRates.push(a/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 nt(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function ue(t,n,e){t.writeHead(n,{"content-type":"application/json"}),t.end(JSON.stringify(e))}function le(t,n,e,r="invalid_request_error"){ue(t,n,{error:{message:e,type:r}})}function ae(t,n,e,r){ue(t,n,{type:"error",error:{type:e,message:r}})}function rt(t,n,e){if(n==="anthropic"){ue(t,404,{type:"error",error:{type:"not_found_error",message:e.message}});return}ue(t,404,{error:{message:e.message,type:"invalid_request_error",code:"model_not_found"}})}var Ln=256,Dn=128;function ot(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,Dn)}var It=64*1024*1024,Ne=class extends Error{};async function jn(t){let n=[],e=0;for await(let o of t){if(e+=o.length,e>It)throw new Ne(`request body exceeds ${It} bytes`);n.push(o)}let r=Buffer.concat(n).toString("utf8");return r?JSON.parse(r):{}}function ke(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 Vn=256;function Hn(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 tt=class{conversations=new Map;thread(n,e){let r=Hn(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<=Vn)break;this.conversations.delete(o)}}};function Oe(t){let n=t?.output;return Array.isArray(n)?n.filter(e=>e.type==="function_call"):[]}function ye(t){return t.find(n=>n.type==="response.completed")?.response??null}function $e(t){let n=t?.usage;if(!n||typeof n!="object")return null;let e=a=>typeof a=="number"&&Number.isFinite(a)&&a>=0?a: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 Tt(t){let n=$e(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 Ct(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 Ie(t){return t.type!=="response.function_call_arguments.delta"||typeof t.tool_index!="number"?null:{tool_index:t.tool_index,call_id:typeof t.call_id=="string"?t.call_id:void 0,name:typeof t.name=="string"?t.name:void 0,delta:typeof t.delta=="string"?t.delta:""}}var we=class{calls=new Map;add(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}))}},Bn=["<tool_call>","<function="];function Fn(t){return Bn.find(n=>t.includes(n))}function be(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 xe(t,n,e){if(!(!t||n>0))return Fn(e)}function Gn(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 Le=class extends Error{};function qn(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 Le(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):n}function Jn(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:qn(e.role,e.content)})}return n}function Qe(t){return Oe(t).map((n,e)=>({index:e,id:n.call_id??n.id??`call_${e}`,type:"function",function:{name:n.name??"",arguments:n.arguments??"{}"}}))}function Pt(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 Wn(t,n,e,r,o){let u=t.stream===!0,a;try{a=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(a))-Buffer.byteLength(JSON.stringify(t.input)));let l=`resp_${Math.random().toString(36).slice(2,14)}`,d=t.store!==!1,p=ot(t);o.model(p);let h=Array.isArray(t.tools)&&t.tools.length>0,x;try{x=await n.createResponse({model:t.model,input:a,stream:u,tools:t.tools,tool_choice:t.tool_choice,temperature:t.temperature,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){rt(r,"openai",g);return}throw g}let A=[];if(u){nt(r);let g=I=>{r.write(`event: ${String(I.type??"message")}
|
|
3
|
-
data: ${JSON.stringify(I)}
|
|
2
|
+
"use strict";var yr=Object.create;var Xt=Object.defineProperty;var gr=Object.getOwnPropertyDescriptor;var wr=Object.getOwnPropertyNames;var vr=Object.getPrototypeOf,_r=Object.prototype.hasOwnProperty;var kr=(n,e,t)=>()=>{if(t)throw t[0];try{return n&&(e=n(n=0)),e}catch(r){throw t=[r],r}};var br=(n,e)=>()=>{try{return e||n((e={exports:{}}).exports,e),e.exports}catch(t){throw e=0,t}};var xr=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of wr(e))!_r.call(n,o)&&o!==t&&Xt(n,o,{get:()=>e[o],enumerable:!(r=gr(e,o))||r.enumerable});return n};var Pe=(n,e,t)=>(t=n!=null?yr(vr(n)):{},xr(e||!n||!n.__esModule?Xt(t,"default",{value:n,enumerable:!0}):t,n));var O=kr(()=>{"use strict"});var Zt=br(()=>{"use strict";O();var Qt;(function(n){(function(e){var t=typeof globalThis=="object"?globalThis:typeof global=="object"?global:typeof self=="object"?self:typeof this=="object"?this:a(),r=o(n);typeof t.Reflect<"u"&&(r=o(t.Reflect,r)),e(r,t),typeof t.Reflect>"u"&&(t.Reflect=n);function o(c,d){return function(h,m){Object.defineProperty(c,h,{configurable:!0,writable:!0,value:m}),d&&d(h,m)}}function s(){try{return Function("return this;")()}catch{}}function i(){try{return(0,eval)("(function() { return this; })()")}catch{}}function a(){return s()||i()}})(function(e,t){var r=Object.prototype.hasOwnProperty,o=typeof Symbol=="function",s=o&&typeof Symbol.toPrimitive<"u"?Symbol.toPrimitive:"@@toPrimitive",i=o&&typeof Symbol.iterator<"u"?Symbol.iterator:"@@iterator",a=typeof Object.create=="function",c={__proto__:[]}instanceof Array,d=!a&&!c,h={create:a?function(){return yt(Object.create(null))}:c?function(){return yt({__proto__:null})}:function(){return yt({})},has:d?function(u,l){return r.call(u,l)}:function(u,l){return l in u},get:d?function(u,l){return r.call(u,l)?u[l]:void 0}:function(u,l){return u[l]}},m=Object.getPrototypeOf(Function),f=typeof Map=="function"&&typeof Map.prototype.entries=="function"?Map:dr(),g=typeof Set=="function"&&typeof Set.prototype.entries=="function"?Set:pr(),S=typeof WeakMap=="function"?WeakMap:fr(),I=o?Symbol.for("@reflect-metadata:registry"):void 0,A=ur(),$=cr(A);function K(u,l,p,y){if(E(p)){if(!Gt(u))throw new TypeError;if(!Vt(l))throw new TypeError;return ie(u,l)}else{if(!Gt(u))throw new TypeError;if(!U(l))throw new TypeError;if(!U(y)&&!E(y)&&!ee(y))throw new TypeError;return ee(y)&&(y=void 0),p=fe(p),q(u,l,p,y)}}e("decorate",K);function X(u,l){function p(y,R){if(!U(y))throw new TypeError;if(!E(R)&&!ir(R))throw new TypeError;ae(u,l,y,R)}return p}e("metadata",X);function Q(u,l,p,y){if(!U(p))throw new TypeError;return E(y)||(y=fe(y)),ae(u,l,p,y)}e("defineMetadata",Q);function se(u,l,p){if(!U(l))throw new TypeError;return E(p)||(p=fe(p)),J(u,l,p)}e("hasMetadata",se);function k(u,l,p){if(!U(l))throw new TypeError;return E(p)||(p=fe(p)),T(u,l,p)}e("hasOwnMetadata",k);function M(u,l,p){if(!U(l))throw new TypeError;return E(p)||(p=fe(p)),N(u,l,p)}e("getMetadata",M);function ne(u,l,p){if(!U(l))throw new TypeError;return E(p)||(p=fe(p)),H(u,l,p)}e("getOwnMetadata",ne);function Z(u,l){if(!U(u))throw new TypeError;return E(l)||(l=fe(l)),be(u,l)}e("getMetadataKeys",Z);function re(u,l){if(!U(u))throw new TypeError;return E(l)||(l=fe(l)),ce(u,l)}e("getOwnMetadataKeys",re);function B(u,l,p){if(!U(l))throw new TypeError;if(E(p)||(p=fe(p)),!U(l))throw new TypeError;E(p)||(p=fe(p));var y=De(l,p,!1);return E(y)?!1:y.OrdinaryDeleteMetadata(u,l,p)}e("deleteMetadata",B);function ie(u,l){for(var p=u.length-1;p>=0;--p){var y=u[p],R=y(l);if(!E(R)&&!ee(R)){if(!Vt(R))throw new TypeError;l=R}}return l}function q(u,l,p,y){for(var R=u.length-1;R>=0;--R){var j=u[R],W=j(l,p,y);if(!E(W)&&!ee(W)){if(!U(W))throw new TypeError;y=W}}return y}function J(u,l,p){var y=T(u,l,p);if(y)return!0;var R=ht(l);return ee(R)?!1:J(u,R,p)}function T(u,l,p){var y=De(l,p,!1);return E(y)?!1:jt(y.OrdinaryHasOwnMetadata(u,l,p))}function N(u,l,p){var y=T(u,l,p);if(y)return H(u,l,p);var R=ht(l);if(!ee(R))return N(u,R,p)}function H(u,l,p){var y=De(l,p,!1);if(!E(y))return y.OrdinaryGetOwnMetadata(u,l,p)}function ae(u,l,p,y){var R=De(p,y,!0);R.OrdinaryDefineOwnMetadata(u,l,p,y)}function be(u,l){var p=ce(u,l),y=ht(u);if(y===null)return p;var R=be(y,l);if(R.length<=0)return p;if(p.length<=0)return R;for(var j=new g,W=[],P=0,v=p;P<v.length;P++){var _=v[P],b=j.has(_);b||(j.add(_),W.push(_))}for(var x=0,C=R;x<C.length;x++){var _=C[x],b=j.has(_);b||(j.add(_),W.push(_))}return W}function ce(u,l){var p=De(u,l,!1);return p?p.OrdinaryOwnMetadataKeys(u,l):[]}function oe(u){if(u===null)return 1;switch(typeof u){case"undefined":return 0;case"boolean":return 2;case"string":return 3;case"symbol":return 4;case"number":return 5;case"object":return u===null?1:6;default:return 6}}function E(u){return u===void 0}function ee(u){return u===null}function L(u){return typeof u=="symbol"}function U(u){return typeof u=="object"?u!==null:typeof u=="function"}function qe(u,l){switch(oe(u)){case 0:return u;case 1:return u;case 2:return u;case 3:return u;case 4:return u;case 5:return u}var p=l===3?"string":l===5?"number":"default",y=qt(u,s);if(y!==void 0){var R=y.call(u,p);if(U(R))throw new TypeError;return R}return or(u,p==="default"?"number":p)}function or(u,l){if(l==="string"){var p=u.toString;if(Ie(p)){var y=p.call(u);if(!U(y))return y}var R=u.valueOf;if(Ie(R)){var y=R.call(u);if(!U(y))return y}}else{var R=u.valueOf;if(Ie(R)){var y=R.call(u);if(!U(y))return y}var j=u.toString;if(Ie(j)){var y=j.call(u);if(!U(y))return y}}throw new TypeError}function jt(u){return!!u}function sr(u){return""+u}function fe(u){var l=qe(u,3);return L(l)?l:sr(l)}function Gt(u){return Array.isArray?Array.isArray(u):u instanceof Object?u instanceof Array:Object.prototype.toString.call(u)==="[object Array]"}function Ie(u){return typeof u=="function"}function Vt(u){return typeof u=="function"}function ir(u){switch(oe(u)){case 3:return!0;case 4:return!0;default:return!1}}function mt(u,l){return u===l||u!==u&&l!==l}function qt(u,l){var p=u[l];if(p!=null){if(!Ie(p))throw new TypeError;return p}}function Wt(u){var l=qt(u,i);if(!Ie(l))throw new TypeError;var p=l.call(u);if(!U(p))throw new TypeError;return p}function Jt(u){return u.value}function Kt(u){var l=u.next();return l.done?!1:l}function zt(u){var l=u.return;l&&l.call(u)}function ht(u){var l=Object.getPrototypeOf(u);if(typeof u!="function"||u===m||l!==m)return l;var p=u.prototype,y=p&&Object.getPrototypeOf(p);if(y==null||y===Object.prototype)return l;var R=y.constructor;return typeof R!="function"||R===u?l:R}function ar(){var u;!E(I)&&typeof t.Reflect<"u"&&!(I in t.Reflect)&&typeof t.Reflect.defineMetadata=="function"&&(u=lr(t.Reflect));var l,p,y,R=new S,j={registerProvider:W,getProvider:v,setProvider:b};return j;function W(x){if(!Object.isExtensible(j))throw new Error("Cannot add provider to a frozen registry.");switch(!0){case u===x:break;case E(l):l=x;break;case l===x:break;case E(p):p=x;break;case p===x:break;default:y===void 0&&(y=new g),y.add(x);break}}function P(x,C){if(!E(l)){if(l.isProviderFor(x,C))return l;if(!E(p)){if(p.isProviderFor(x,C))return l;if(!E(y))for(var D=Wt(y);;){var G=Kt(D);if(!G)return;var le=Jt(G);if(le.isProviderFor(x,C))return zt(D),le}}}if(!E(u)&&u.isProviderFor(x,C))return u}function v(x,C){var D=R.get(x),G;return E(D)||(G=D.get(C)),E(G)&&(G=P(x,C),E(G)||(E(D)&&(D=new f,R.set(x,D)),D.set(C,G))),G}function _(x){if(E(x))throw new TypeError;return l===x||p===x||!E(y)&&y.has(x)}function b(x,C,D){if(!_(D))throw new Error("Metadata provider not registered.");var G=v(x,C);if(G!==D){if(!E(G))return!1;var le=R.get(x);E(le)&&(le=new f,R.set(x,le)),le.set(C,D)}return!0}}function ur(){var u;return!E(I)&&U(t.Reflect)&&Object.isExtensible(t.Reflect)&&(u=t.Reflect[I]),E(u)&&(u=ar()),!E(I)&&U(t.Reflect)&&Object.isExtensible(t.Reflect)&&Object.defineProperty(t.Reflect,I,{enumerable:!1,configurable:!1,writable:!1,value:u}),u}function cr(u){var l=new S,p={isProviderFor:function(_,b){var x=l.get(_);return E(x)?!1:x.has(b)},OrdinaryDefineOwnMetadata:W,OrdinaryHasOwnMetadata:R,OrdinaryGetOwnMetadata:j,OrdinaryOwnMetadataKeys:P,OrdinaryDeleteMetadata:v};return A.registerProvider(p),p;function y(_,b,x){var C=l.get(_),D=!1;if(E(C)){if(!x)return;C=new f,l.set(_,C),D=!0}var G=C.get(b);if(E(G)){if(!x)return;if(G=new f,C.set(b,G),!u.setProvider(_,b,p))throw C.delete(b),D&&l.delete(_),new Error("Wrong provider for target.")}return G}function R(_,b,x){var C=y(b,x,!1);return E(C)?!1:jt(C.has(_))}function j(_,b,x){var C=y(b,x,!1);if(!E(C))return C.get(_)}function W(_,b,x,C){var D=y(x,C,!0);D.set(_,b)}function P(_,b){var x=[],C=y(_,b,!1);if(E(C))return x;for(var D=C.keys(),G=Wt(D),le=0;;){var Yt=Kt(G);if(!Yt)return x.length=le,x;var mr=Jt(Yt);try{x[le]=mr}catch(hr){try{zt(G)}finally{throw hr}}le++}}function v(_,b,x){var C=y(b,x,!1);if(E(C)||!C.delete(_))return!1;if(C.size===0){var D=l.get(b);E(D)||(D.delete(x),D.size===0&&l.delete(D))}return!0}}function lr(u){var l=u.defineMetadata,p=u.hasOwnMetadata,y=u.getOwnMetadata,R=u.getOwnMetadataKeys,j=u.deleteMetadata,W=new S,P={isProviderFor:function(v,_){var b=W.get(v);return!E(b)&&b.has(_)?!0:R(v,_).length?(E(b)&&(b=new g,W.set(v,b)),b.add(_),!0):!1},OrdinaryDefineOwnMetadata:l,OrdinaryHasOwnMetadata:p,OrdinaryGetOwnMetadata:y,OrdinaryOwnMetadataKeys:R,OrdinaryDeleteMetadata:j};return P}function De(u,l,p){var y=A.getProvider(u,l);if(!E(y))return y;if(p){if(A.setProvider(u,l,$))return $;throw new Error("Illegal state.")}}function dr(){var u={},l=[],p=(function(){function P(v,_,b){this._index=0,this._keys=v,this._values=_,this._selector=b}return P.prototype["@@iterator"]=function(){return this},P.prototype[i]=function(){return this},P.prototype.next=function(){var v=this._index;if(v>=0&&v<this._keys.length){var _=this._selector(this._keys[v],this._values[v]);return v+1>=this._keys.length?(this._index=-1,this._keys=l,this._values=l):this._index++,{value:_,done:!1}}return{value:void 0,done:!0}},P.prototype.throw=function(v){throw this._index>=0&&(this._index=-1,this._keys=l,this._values=l),v},P.prototype.return=function(v){return this._index>=0&&(this._index=-1,this._keys=l,this._values=l),{value:v,done:!0}},P})(),y=(function(){function P(){this._keys=[],this._values=[],this._cacheKey=u,this._cacheIndex=-2}return Object.defineProperty(P.prototype,"size",{get:function(){return this._keys.length},enumerable:!0,configurable:!0}),P.prototype.has=function(v){return this._find(v,!1)>=0},P.prototype.get=function(v){var _=this._find(v,!1);return _>=0?this._values[_]:void 0},P.prototype.set=function(v,_){var b=this._find(v,!0);return this._values[b]=_,this},P.prototype.delete=function(v){var _=this._find(v,!1);if(_>=0){for(var b=this._keys.length,x=_+1;x<b;x++)this._keys[x-1]=this._keys[x],this._values[x-1]=this._values[x];return this._keys.length--,this._values.length--,mt(v,this._cacheKey)&&(this._cacheKey=u,this._cacheIndex=-2),!0}return!1},P.prototype.clear=function(){this._keys.length=0,this._values.length=0,this._cacheKey=u,this._cacheIndex=-2},P.prototype.keys=function(){return new p(this._keys,this._values,R)},P.prototype.values=function(){return new p(this._keys,this._values,j)},P.prototype.entries=function(){return new p(this._keys,this._values,W)},P.prototype["@@iterator"]=function(){return this.entries()},P.prototype[i]=function(){return this.entries()},P.prototype._find=function(v,_){if(!mt(this._cacheKey,v)){this._cacheIndex=-1;for(var b=0;b<this._keys.length;b++)if(mt(this._keys[b],v)){this._cacheIndex=b;break}}return this._cacheIndex<0&&_&&(this._cacheIndex=this._keys.length,this._keys.push(v),this._values.push(void 0)),this._cacheIndex},P})();return y;function R(P,v){return P}function j(P,v){return v}function W(P,v){return[P,v]}}function pr(){var u=(function(){function l(){this._map=new f}return Object.defineProperty(l.prototype,"size",{get:function(){return this._map.size},enumerable:!0,configurable:!0}),l.prototype.has=function(p){return this._map.has(p)},l.prototype.add=function(p){return this._map.set(p,p),this},l.prototype.delete=function(p){return this._map.delete(p)},l.prototype.clear=function(){this._map.clear()},l.prototype.keys=function(){return this._map.keys()},l.prototype.values=function(){return this._map.keys()},l.prototype.entries=function(){return this._map.entries()},l.prototype["@@iterator"]=function(){return this.keys()},l.prototype[i]=function(){return this.keys()},l})();return u}function fr(){var u=16,l=h.create(),p=y();return(function(){function v(){this._key=y()}return v.prototype.has=function(_){var b=R(_,!1);return b!==void 0?h.has(b,this._key):!1},v.prototype.get=function(_){var b=R(_,!1);return b!==void 0?h.get(b,this._key):void 0},v.prototype.set=function(_,b){var x=R(_,!0);return x[this._key]=b,this},v.prototype.delete=function(_){var b=R(_,!1);return b!==void 0?delete b[this._key]:!1},v.prototype.clear=function(){this._key=y()},v})();function y(){var v;do v="@@WeakMap@@"+P();while(h.has(l,v));return l[v]=!0,v}function R(v,_){if(!r.call(v,p)){if(!_)return;Object.defineProperty(v,p,{value:h.create()})}return v[p]}function j(v,_){for(var b=0;b<_;++b)v[b]=Math.random()*255|0;return v}function W(v){if(typeof Uint8Array=="function"){var _=new Uint8Array(v);return typeof crypto<"u"?crypto.getRandomValues(_):typeof msCrypto<"u"?msCrypto.getRandomValues(_):j(_,v),_}return j(new Array(v),v)}function P(){var v=W(u);v[6]=v[6]&79|64,v[8]=v[8]&191|128;for(var _="",b=0;b<u;++b){var x=v[b];(b===4||b===6||b===8)&&(_+="-"),x<16&&(_+="0"),_+=x.toString(16).toLowerCase()}return _}}function yt(u){return u.__=void 0,delete u.__,u}})})(Qt||(Qt={}))});O();var Di=Pe(Zt(),1),ge=Pe(require("process"),1);O();O();var Cr=Pe(require("os"),1),Tr=Pe(require("path"),1),Ur=require("url"),Mr=require("util");O();O();function he(n){return n.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}O();var Sr={chat:!0,code:!0,agent:!0,vl:!0},en={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function Rr(n){let e=n.toLowerCase();return Object.hasOwn(en,e)?en[e]:null}function gt(n){let e=n.engine_args?.context_length;return typeof e=="number"&&Number.isFinite(e)&&e>0?e:null}function nn(n,e){let t=e.filter(s=>he(`${s.model_id}-${s.variant}`)===n);if(t.length===1)return t[0];if(t.length>1)return tn(t);let r=e.filter(s=>he(s.model_id)===n),o=new Set(r.map(s=>s.variant));return r.length>0&&o.size===1?tn(r):null}function tn(n){let e=n[0],t=i=>n.every(a=>(a[i]??void 0)===(e[i]??void 0))?e[i]:void 0,r=n.map(gt),o=r[0],s=o!=null&&r.every(i=>i===o)?o:null;return{model_id:e.model_id,variant:e.variant,gpu_spec:t("gpu_spec"),engine:t("engine"),task:t("task"),description:t("description"),size_gb:t("size_gb"),hf_repo:t("hf_repo"),engine_args:s!==null?{context_length:s}:{},updated_at:t("updated_at")}}function rn(n){let e=n.updated_at?Date.parse(n.updated_at):NaN;return Number.isFinite(e)?Math.floor(e/1e3):0}function on(n,e){let t=nn(n,e);if(!t)return{id:n,object:"model",created:0,owned_by:"urun"};let r=gt(t);return{id:n,object:"model",created:rn(t),owned_by:"urun",name:`${t.model_id}:${t.variant}`,...t.description?{description:t.description}:{},...t.task?{task:t.task}:{},...t.engine?{engine:t.engine}:{},...t.gpu_spec?{gpu_spec:t.gpu_spec}:{},...r!==null?{context_length:r}:{}}}function sn(n,e){let t=[];for(let r of n){let o=nn(r,e);if(!o||!o.task||Sr[o.task]!==!0)continue;let s=gt(o),i={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};s!==null&&(i.max_tokens={type:"integer",min:1,max:s,unit:"token"}),t.push({schema_version:"2.4",id:r,name:`${o.model_id}:${o.variant}`,created:rn(o),quantization:Rr(o.variant),description:o.description??"",hugging_face_id:o.hf_repo?o.hf_repo.split("@")[0]??o.hf_repo:"",input_modalities:[{type:"text",...s!==null?{supported_inputs:{max_context_length:{value:s,unit:"token"}}}:{}},...o.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:i}]})}return{data:t}}var z=class extends Error{},Y=class extends Error{},wt="urs1.";function Er(n,e){return wt+Buffer.from(JSON.stringify([n,e]),"utf8").toString("base64url")}function Or(n){if(!n.startsWith(wt))return null;try{let e=JSON.parse(Buffer.from(n.slice(wt.length),"base64url").toString("utf8"));if(Array.isArray(e)&&typeof e[0]=="string"&&typeof e[1]=="string")return{app:e[0],key:e[1]}}catch{}return null}async function un(n){let e=n.fetchImpl??fetch,t=`${n.apiUrl.replace(/\/+$/,"")}/apps`,r=await e(t,{headers:{Authorization:`Bearer ${n.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new Error(`org apps listing failed: GET ${t} \u2192 ${r.status}`);let o=await r.json();if(!Array.isArray(o.apps))throw new Error(`org apps listing returned no "apps" array (GET ${t})`);if(o.truncated===!0)throw new Error(`org apps listing was truncated (GET ${t} returned ${o.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return o.apps}function Ar(n,e){let t=e.filter(s=>he(`${s.model_id}-${s.variant}`)===n);if(t.length>0)return t;let r=e.filter(s=>he(s.model_id)===n);return new Set(r.map(s=>s.variant)).size===1?r:[]}function Ir(n){let e=n.engine_args?.expects_image;return e==null?"edit":e===!1?"generation":e===!0?"edit":null}function Pr(n){let e=n.map(Ir),t=e[0];return t!=null&&e.every(r=>r===t)?t:null}var an=6e4,We=class{constructor(e){this.opts=e}opts;pool=new Map;appsCache=null;rowsCache=null;seed(e,t){this.pool.set(e,Promise.resolve(t))}async deployedApps(){if(!this.opts.listApps)return[];let e=this.opts.appsTtlMs??an;if(this.appsCache&&Date.now()-this.appsCache.at<e)return this.appsCache.apps;let t=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:t},t}async catalogRows(){if(!this.opts.listCatalog)return[];let e=this.opts.appsTtlMs??an;if(this.rowsCache&&Date.now()-this.rowsCache.at<e)return this.rowsCache.rows;let t=await this.opts.listCatalog();return this.rowsCache={at:Date.now(),rows:t},t}servable(e){return e.filter(t=>t.function_name===this.opts.fnName&&t.deployment_status==="active").map(t=>t.app_slug)}async availableIds(){let e=this.opts.defaultApp,t=new Set(e===null?[]:[e]);if(this.opts.listApps)for(let o of this.servable(await this.deployedApps()))t.add(o);for(let o of this.pool.keys())t.add(o);let r=[...t].filter(o=>o!==e).sort();return e===null?r:[e,...r]}async noDefaultApp(e){let t=await this.availableIds(),r=t.length>0?t.join(", "):"(none deployed)";throw new z(e===""?`no "model" was given and this endpoint has no default model \u2014 name one of your org's deployed "${this.opts.fnName}" apps; available models: ${r}`:`model "${e}" does not name a deployed "${this.opts.fnName}" app in your org, and this endpoint has no default model to fall back to; available models: ${r}`)}async resolveApp(e){let t=(e??"").trim(),r=this.opts.defaultApp;if(!t||t==="urun")return r===null?this.noDefaultApp(t):r;let o=he(t);if(o===r)return r;if(this.pool.has(o))return o;if(this.opts.listApps){let s=await this.deployedApps(),i=this.servable(s);if(i.includes(o))return o;let a=i.filter(d=>d.startsWith(`${o}-`));if(a.length===1)return a[0];if(a.length>1)throw new z(`model "${t}" is ambiguous across deployed apps ${a.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let c=s.find(d=>d.app_slug===o||d.app_slug.startsWith(`${o}-`));if(c)throw new z(`model "${t}" maps to app "${c.app_slug}", which is not an active "${this.opts.fnName}" app \u2014 deploy it with \`urun serve ${t}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.listCatalog){let i=(await this.opts.listCatalog()).find(a=>t===a.model_id||t===`${a.model_id}:${a.variant}`||o===he(a.model_id)||o===he(`${a.model_id}-${a.variant}`));if(i)throw new z(`model "${t}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${i.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return r===null?this.noDefaultApp(t):r}async sessionFor(e){let t=await this.resolveApp(e),r=this.pool.get(t);return r||(r=Promise.resolve(this.opts.openSession(t)),this.pool.set(t,r),r.catch(()=>this.pool.delete(t))),{app:t,entry:await r}}keyOf(e){let t=this.opts.sessionKey;if(!t)throw new Error("ModelRouter: sessionKey is not configured \u2014 the session-identity seam (handleFor/sessionForHandle) is unavailable on this router");let r=t(e);if(typeof r!="string"||r.length===0)throw new Error("ModelRouter: sessionKey returned an empty identity for a pooled session");return r}async handleFor(e){let{app:t,entry:r}=await this.sessionFor(e);return{app:t,handle:Er(t,this.keyOf(r))}}async sessionForHandle(e){let t=Or(e);if(!t)throw new Y(`malformed session handle ${JSON.stringify(e)} \u2014 not issued by this proxy`);let{app:r,key:o}=t,s=this.pool.get(r),i=s?await s.then(a=>a,()=>null):null;if(i===null)throw new Y(`the session behind this handle (app "${r}") is gone \u2014 closed, evicted after its pod died, or the proxy restarted; it cannot be reattached. Reconnect without a handle to start a new session.`);if(this.keyOf(i)!==o)throw new Y(`the session behind this handle (app "${r}") was replaced \u2014 the original backhaul died and a new session serves this app now; the handle's session cannot be reattached. Reconnect without a handle to start a new session.`);return{app:r,entry:i}}async evict(e,t){let r=this.pool.get(e);!r||await r.then(s=>s,()=>null)!==t||this.pool.get(e)!==r||(this.pool.delete(e),await this.opts.closeSession(t).catch(()=>{}))}async modelList(){let e=await this.availableIds(),t=await this.catalogRows().catch(()=>[]);return{object:"list",data:e.map(r=>on(r,t))}}async openRouterModels(){let e=await this.availableIds(),t=await this.catalogRows();return sn(e,t)}async supportsImages(e,t){let r=await this.resolveApp(e),o=await this.catalogRows(),s=Ar(r,o);return s.length===0||!s.every(i=>(i.task??void 0)==="image")?!1:Pr(s)===t}async closeAll(){let e=[...this.pool.values()];this.pool.clear();let t=[];if(await Promise.all(e.map(async r=>{try{await this.opts.closeSession(await r)}catch(o){t.push(String(o instanceof Error?o.message:o))}})),t.length>0)throw new Error(`failed to close ${t.length} pooled session(s): ${t.join("; ")}`)}};function cn(n,e="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(n).pathname))throw new Error(`${e} must be the session-gateway base (e.g. https://api.urun.sh), got ${n} \u2014 a trailing /v1 is the org control-plane API form (that belongs in URUN_API_URL); session allocation 404s against it. Drop the /v1.`);return n}function Je(n,e="URUN_GATEWAY_URL"){let t=new URL(n),r=t.hostname==="localhost"||t.hostname==="127.0.0.1"||t.hostname==="[::1]";if(t.protocol!=="https:"&&!r)throw new Error(`${e} must be https (got ${n}) \u2014 the org API key rides the request's Authorization header`);return n}var Nr=8080,vt="serve",_t="0.0.0.0";function $r(n,e,t){let r=(n[e]??"").trim();if(!r)throw new Error(`${e} is not set \u2014 ${t}`);return r}function ln(n=process.env){let e=(n.PORT??"").trim(),t=e===""?Nr:Number(e);if(!Number.isInteger(t)||t<0||t>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let r=cn(Je($r(n,"URUN_BASE_URL","the hosted proxy needs the uRun session-gateway base it opens sessions and verifies API keys against (e.g. https://api.urun.sh)"),"URUN_BASE_URL")),o=(n.URUN_API_URL??"").trim(),s=o?Je(o,"URUN_API_URL"):`${r.replace(/\/+$/,"")}/v1`,i=(n.URUN_CATALOG_URL??"").trim(),a=(n.URUN_CATALOG_ANON_KEY??"").trim();if(!!i!=!!a)throw new Error("URUN_CATALOG_URL and URUN_CATALOG_ANON_KEY must be set together (or both left unset) \u2014 exactly one of them is set, which would silently disable the model-catalog oracle");let c=i?{catalogUrl:Je(i,"URUN_CATALOG_URL"),anonKey:a}:null;return{port:t,baseUrl:r,apiUrl:s,catalog:c}}O();var tr=require("http");O();O();var kt=class{constructor(e,t){this.backend=e;this.opts=t}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(e){if(!this.source)throw new Error("startOutbound() not called");let t=Buffer.from(e,"base64"),r=Math.floor(t.byteLength/2),o=new Int16Array(r);for(let s=0;s<r;s++)o[s]=t.readInt16LE(s*2);this.source.onData(o)}async startInbound(e){if(e==null)throw new Error('startInbound: no downstream audio track \u2014 the runtime audio producer has not been consumed yet (session.stream("rt-audio-out").track is null)');this.sink=this.backend.createSink(e),this.sink.onframe=t=>{let r=Buffer.from(t.samples.buffer,t.samples.byteOffset,t.samples.byteLength).toString("base64");for(let o of this.outHandlers)o(r)}}onOutputAudio(e){return this.outHandlers.add(e),()=>this.outHandlers.delete(e)}};async function fn(n,e,t=1e4){let r=new kt(e,{sampleRate:24e3}),o=await r.startOutbound();await n.stream("rt-audio-in").attach(o);let s=await Lr(n,t);return await r.startInbound(s),r}function Lr(n,e){let t=n.stream("rt-audio-out");return t.track!=null?Promise.resolve(t.track):new Promise((r,o)=>{let s,i=setTimeout(()=>{s?.(),o(new Error(`enableSessionAudio: no downstream audio track within ${e}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},e),a=c=>{c!=null&&(clearTimeout(i),s?.(),r(c))};s=t.on?.("track",a),t.track!=null&&a(t.track)})}var dn="rt-video-in",pn=256e3;function mn(n){let e=n.stream(dn),t=e.emit;if(typeof t!="function")throw new Error(`enableSessionVideo: session.stream('${dn}') has no emit() \u2014 the named-DATA stream produce seam (@urun-sh/core SessionStream.emit, contract \xA75) is required for the video frame lane`);return{sendInputFrame:async r=>{if(r.byteLength===0)throw new Error("enableSessionVideo: refusing to send an empty video frame");if(r.byteLength>pn)throw new Error(`enableSessionVideo: frame is ${r.byteLength} bytes \u2014 over the ${pn}-byte data-channel budget (the SFU's 262144-byte SCTP ceiling drops oversize messages SILENTLY; send smaller/lower-quality frames)`);await t.call(e,r)}}}var Dr=24e3,Fe=480,Fr=111;async function Hr(){let n=await import("opusscript"),e=n.default??n;return new e(Dr,1,2048)}async function hn(){let n=await import("werift"),e=await Hr();return{createSource:()=>{let t=new n.MediaStreamTrack({kind:"audio"}),r=Math.random()*4294967295>>>0,o=Math.random()*65535&65535,s=Math.random()*4294967295>>>0,i=new Int16Array(0);return{track:t,onData:a=>{let c=new Int16Array(i.length+a.length);c.set(i,0),c.set(a,i.length);let d=0;for(;c.length-d>=Fe;){let h=c.subarray(d,d+Fe);d+=Fe;let m=Buffer.from(h.buffer,h.byteOffset,h.byteLength),f=e.encode(m,Fe);o=o+1&65535,s=s+Fe>>>0;let g=new n.RtpHeader({version:2,payloadType:Fr,sequenceNumber:o,timestamp:s,ssrc:r,marker:!1}),S=new n.RtpPacket(g,f);t.writeRtp(S)}i=c.slice(d)}}},createSink:t=>{let r={onframe:null};return t.onReceiveRtp.subscribe(s=>{let i;try{i=e.decode(s.payload)}catch{return}let a=new Int16Array(i.length/2);for(let c=0;c<a.length;c++)a[c]=i.readInt16LE(c*2);r.onframe?.({samples:a})}),r}}}O();var Ke=require("@urun-sh/core"),bt="rt-video-out";var Br="h264";function jr(n,e){if(!Number.isInteger(n.seq)||n.seq<0)throw new Error(`${e}: header seq must be a non-negative integer (got ${n.seq})`);if(!Number.isInteger(n.ptsUs))throw new Error(`${e}: header ptsUs must be an integer microsecond timestamp (got ${n.ptsUs})`);if(typeof n.codec!="string"||n.codec.length===0)throw new Error(`${e}: header codec must be a non-empty string`);if(typeof n.key!="boolean")throw new Error(`${e}: header key must be a boolean`)}function Gr(n){if(n.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${n.byteLength} byte(s), no header length)`);let e=n[0]|n[1]<<8;if(n.byteLength<2+e)throw new Error(`decodeVideoOutRecord: truncated record (header says ${e} bytes, ${n.byteLength-2} present)`);let t;try{t=JSON.parse(new TextDecoder().decode(n.subarray(2,2+e)))}catch(i){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(i)})`)}let r=t;if(r===null||typeof r!="object"||r.v!==1)throw new Error(`decodeVideoOutRecord: header is not spec v1 (v=${JSON.stringify(r?.v)}) \u2014 a newer producer must be met with a newer consumer, never silently misread`);let o=r;jr(o,"decodeVideoOutRecord");let s=n.subarray(2+e);if(s.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:o,au:s.slice()}}var yn="urun.videoOut.v2-ws";function gn(n){let e=n.stream(bt);if(typeof e.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${bt}') has no messages() \u2014 the named-DATA stream consume seam (@urun-sh/core SessionStream.messages, contract \xA75) is required for the video OUT lane`);let t=new Set,r=new Set,o=new Set,s=c=>{for(let d of r)d(c)},i=!1,a=async()=>{for await(let c of e.messages()){if(!(c instanceof Uint8Array)){s(new Error(`openSessionVideoOutLane: "${bt}" delivered a non-BINARY \xA75 payload (${typeof c}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let d;try{d=Gr(c)}catch(h){s(h instanceof Error?h:new Error(String(h)));continue}for(let h of t)h(d)}for(let c of o)c()};return{codec:Br,onOutputFrame:c=>(t.add(c),i||(i=!0,a().catch(d=>s(d instanceof Error?d:new Error(String(d))))),()=>t.delete(c)),onLaneError:c=>(r.add(c),()=>r.delete(c)),onEnd:c=>(o.add(c),()=>o.delete(c))}}var Xe="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",qr=1007,vn=1008,ye=1011,Qe=1008,w=class extends Error{constructor(t,r=qr){super(t);this.closeCode=r}closeCode},V=n=>typeof n=="object"&&n!==null&&!Array.isArray(n)?n:null,ze=["setup","clientContent","realtimeInput","toolResponse"];function _n(n){let e;try{e=JSON.parse(n)}catch{throw new w("client message is not valid JSON")}let t=V(e);if(!t)throw new w("client message must be a JSON object");let r=ze.filter(a=>t[a]!==void 0),o=Object.keys(t).filter(a=>!ze.includes(a));if(o.length>0)throw new w(`unknown client message field(s) ${JSON.stringify(o)} \u2014 expected exactly one of ${ze.join(", ")}`);if(r.length!==1)throw new w(`client message must contain exactly one of ${ze.join(", ")} (got ${r.length})`);let s=r[0],i=V(t[s]);if(!i)throw new w(`"${s}" must be a JSON object`);return{kind:s,[s]:i}}function kn(n,e){if(typeof n=="string")return n;let t=V(n),r=t?t.parts:void 0;if(!Array.isArray(r))throw new w(`${e}: expected a Content with a parts array`);let o=[];for(let s of r){let i=V(s);if(i&&typeof i.text=="string"){o.push(i.text);continue}throw i&&(i.inlineData!==void 0||i.fileData!==void 0)?new w(`${e}: media parts (inlineData/fileData) are a declared follow-up slice (audio/video lanes over the proxy transport AudioBridge) \u2014 only text parts are supported in this slice`):i&&(i.functionCall!==void 0||i.functionResponse!==void 0)?new w(`${e}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new w(`${e}: unsupported part ${JSON.stringify(s)}`)}return o.join(`
|
|
3
|
+
`)}var Wr={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},xe="urun.videoOut";function bn(n){return{framing:yn,codec:n}}function xn(n){let{seq:e,ptsUs:t,codec:r,key:o}=n.header;return{[xe]:{v:1,seq:e,ptsUs:t,codec:r,key:o,data:Buffer.from(n.au).toString("base64")}}}var Jr={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},Kr=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",xe],wn=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function zr(n){let e=V(n);if(!e)throw new w("setup.realtimeInputConfig must be an object");for(let o of Object.keys(e))if(!(o==="automaticActivityDetection"||o==="activityHandling"))throw new w(o==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${o}"`);let t=V(e.automaticActivityDetection);if(e.automaticActivityDetection!==void 0&&!t)throw new w("realtimeInputConfig.automaticActivityDetection must be an object");for(let o of Object.keys(t??{}))if(o!=="disabled")throw new w(`automaticActivityDetection.${o} tunes AUTOMATIC (server-side) activity detection \u2014 the declared platform follow-up (serve turn_detection events over the session stream lane); only "disabled: true" (manual activity mode) is supported in this slice`);if(t?.disabled!==!0)throw new w("automatic (server-side) activity detection is the declared platform follow-up (serve turn_detection turn_start/turn_end events over the session stream lane, not proxy DSP) \u2014 set realtimeInputConfig.automaticActivityDetection.disabled: true for manual activity mode");let r="START_OF_ACTIVITY_INTERRUPTS";if(e.activityHandling!==void 0){if(!wn.includes(e.activityHandling))throw new w(`unsupported activityHandling ${JSON.stringify(e.activityHandling)} \u2014 expected one of ${wn.join(", ")}`);r=e.activityHandling}return{manualActivity:!0,activityHandling:r}}var Yr=["temperature","maxOutputTokens","responseModalities"];function Sn(n){for(let[m,f]of Object.entries(Jr))if(n[m]!==void 0)throw new w(f);for(let m of Object.keys(n))if(!Kr.includes(m))throw new w(`unsupported setup field "${m}"`);if(typeof n.model!="string"||n.model.length===0)throw new w('setup.model is required (format "models/{model}")');let e=n.model.replace(/^models\//,""),t,r,o=!1,s=V(n.generationConfig);if(n.generationConfig!==void 0&&!s)throw new w("setup.generationConfig must be an object");if(s){for(let m of Object.keys(s))if(!Yr.includes(m))throw new w(`unsupported generationConfig field "${m}"`);if(s.temperature!==void 0){if(typeof s.temperature!="number")throw new w("generationConfig.temperature must be a number");t=s.temperature}if(s.maxOutputTokens!==void 0){if(typeof s.maxOutputTokens!="number")throw new w("generationConfig.maxOutputTokens must be a number");r=s.maxOutputTokens}if(s.responseModalities!==void 0){let m=Array.isArray(s.responseModalities)?s.responseModalities:[s.responseModalities];for(let f of m){let g=String(f).toUpperCase();if(g==="AUDIO"){o=!0;continue}if(g!=="TEXT")throw new w(`responseModalities ${JSON.stringify(f)} is not supported \u2014 this surface speaks TEXT and AUDIO (audio-out rides the native AudioBridge rt-audio-out lane)`)}}}let i=[];n.systemInstruction!==void 0&&i.push({role:"system",content:kn(n.systemInstruction,"setup.systemInstruction")});let a;if(n.tools!==void 0){if(!Array.isArray(n.tools))throw new w("setup.tools must be an array");a=[];for(let m of n.tools){let f=V(m);if(!f)throw new w("setup.tools entries must be objects");for(let g of Object.keys(f))if(g!=="functionDeclarations")throw new w(`unsupported tool "${g}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(f.functionDeclarations))throw new w("tool.functionDeclarations must be an array");for(let g of f.functionDeclarations){let S=V(g);if(!S||typeof S.name!="string")throw new w("functionDeclarations entries require a string name");a.push({type:"function",name:S.name,description:S.description,parameters:S.parameters})}}a.length===0&&(a=void 0)}let c={enabled:!1,handle:null};if(n.sessionResumption!==void 0){let m=V(n.sessionResumption);if(!m)throw new w("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let f of Object.keys(m))if(f!=="handle")throw new w(`unsupported sessionResumption field "${f}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(m.handle!==void 0&&(typeof m.handle!="string"||m.handle.length===0))throw new w("sessionResumption.handle must be a non-empty string when present");c={enabled:!0,handle:typeof m.handle=="string"?m.handle:null}}let d=n.realtimeInputConfig===void 0?Wr:zr(n.realtimeInputConfig),h=!1;if(n[xe]!==void 0){if(!V(n[xe]))throw new w(`setup["${xe}"] must be an object (the v1 opt-in is {})`);h=!0}return{model:e,systemItems:i,tools:a,temperature:t,maxOutputTokens:r,audioModality:o,activity:d,sessionResumption:c,videoOut:h}}function Rn(n){return{sessionResumptionUpdate:{newHandle:n,resumable:!0}}}function En(n){let e=Math.max(0,n??0)/1e3;return{goAway:{timeLeft:`${Math.round(e*1e3)/1e3}s`}}}function On(n){for(let r of Object.keys(n))if(r!=="turns"&&r!=="turnComplete")throw new w(`unsupported clientContent field "${r}"`);let e=n.turns===void 0?[]:n.turns;if(!Array.isArray(e))throw new w("clientContent.turns must be an array");let t=[];for(let r of e){let o=V(r);if(!o)throw new w("clientContent.turns entries must be Content objects");let s=o.role===void 0?"user":String(o.role).toLowerCase();if(s!=="user"&&s!=="model")throw new w(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(o.role)})`);t.push({role:s==="model"?"assistant":"user",content:kn(o,"clientContent.turns")})}return{items:t,turnComplete:n.turnComplete===!0}}var Ye=16e3,An=24e3,Xr=`audio/pcm;rate=${An}`,In=Buffer.alloc(960).toString("base64");function Qr(n,e,t){if(e===t)return n;let r=Math.floor(n.length*t/e),o=new Int16Array(r),s=e/t,i=n.length-1;for(let a=0;a<r;a++){let c=a*s,d=Math.floor(c);if(d>=i){o[a]=n[i];continue}let h=c-d;o[a]=Math.round(n[d]+(n[d+1]-n[d])*h)}return o}function Zr(n){let e=V(n);if(!e)throw new w("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(e.mimeType))throw new w(`realtimeInput.video.mimeType must be image/jpeg (the Live video input stream is individual JPEG frames), got ${JSON.stringify(e.mimeType)}`);if(typeof e.data!="string"||e.data.length===0)throw new w("realtimeInput.video.data must be non-empty base64");let t=Buffer.from(e.data,"base64");if(t.length<3||t[0]!==255||t[1]!==216)throw new w("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(t)}function eo(n){let e=V(n);if(!e)throw new w("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(e.mimeType))throw new w(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(e.mimeType)}`);let t=/;\s*rate=(\d+)/.exec(e.mimeType);if(t&&Number(t[1])!==Ye)throw new w(`realtimeInput.audio.mimeType declares rate=${t[1]} \u2014 the Live spec input format is ${Ye} Hz (audio/pcm;rate=${Ye})`);if(typeof e.data!="string"||e.data.length===0)throw new w("realtimeInput.audio.data must be non-empty base64");let r=Buffer.from(e.data,"base64");if(r.length===0||r.length%2!==0)throw new w(`realtimeInput.audio.data must decode to whole 16-bit samples (got ${r.length} bytes \u2014 not even)`);let o=new Int16Array(r.length/2);for(let i=0;i<o.length;i++)o[i]=r.readInt16LE(i*2);let s=Qr(o,Ye,An);return Buffer.from(s.buffer,s.byteOffset,s.byteLength).toString("base64")}function Pn(n){if(n.mediaChunks!==void 0)throw new w('realtimeInput.mediaChunks is deprecated by the Live API \u2014 send the audio stream via realtimeInput.audio ({mimeType:"audio/pcm;rate=16000", data}) instead');if(n.activityStart!==void 0||n.activityEnd!==void 0){if(Object.keys(n).length!==1||n.activityStart!==void 0&&n.activityEnd!==void 0)throw new w("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let o=n.activityStart!==void 0?"activityStart":"activityEnd",s=V(n[o]);if(!s||Object.keys(s).length!==0)throw new w(`realtimeInput.${o} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:o}}let e=["text","audio","audioStreamEnd","video"];for(let r of Object.keys(n))if(!e.includes(r))throw new w(`unsupported realtimeInput field "${r}"`);let t=e.filter(r=>n[r]!==void 0);if(t.length!==1)throw new w(`realtimeInput must carry exactly one of ${e.join(", ")} (got ${t.length})`);if(n.audio!==void 0)return{kind:"audio",base64Pcm24k:eo(n.audio)};if(n.video!==void 0)return{kind:"video",jpegFrame:Zr(n.video)};if(n.audioStreamEnd!==void 0){if(n.audioStreamEnd!==!0)throw new w("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof n.text!="string"||n.text.length===0)throw new w("realtimeInput.text must be a non-empty string");return{kind:"text",text:n.text}}function Cn(n){for(let e of Object.keys(n))if(e!=="functionResponses")throw new w(`unsupported toolResponse field "${e}"`);if(!Array.isArray(n.functionResponses)||n.functionResponses.length===0)throw new w("toolResponse.functionResponses must be a non-empty array");return n.functionResponses.map(e=>{let t=V(e);if(!t||typeof t.id!="string"||t.id.length===0)throw new w("functionResponses entries require the string id matched from the toolCall");return{type:"function_call_output",call_id:t.id,output:JSON.stringify(t.response??null)}})}function Tn(n){return n.type!=="response.output_text.delta"||typeof n.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:n.delta}]}}}}function Un(n){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:Xr,data:n}}]}}}}function Mn(n){let e=V(n)??{},t=Array.isArray(e.output)?e.output:[],r=[],o=[],s=typeof e.output_text=="string"?e.output_text:"";if(!s)for(let d of t){let h=V(d);if(!(h?.type!=="message"||!Array.isArray(h.content)))for(let m of h.content){let f=V(m);f?.type==="output_text"&&typeof f.text=="string"&&(s+=f.text)}}s&&o.push({role:"assistant",content:s});for(let d of t){let h=V(d);if(h?.type!=="function_call")continue;if(typeof h.call_id!="string"||typeof h.name!="string")throw new w(`upstream function_call item is missing call_id/name: ${JSON.stringify(d)}`,ye);let m=typeof h.arguments=="string"&&h.arguments.length>0?h.arguments:"{}",f;try{f=JSON.parse(m)}catch{throw new w(`upstream function_call arguments are not valid JSON: ${m}`,ye)}r.push({id:h.call_id,name:h.name,args:f}),o.push({type:"function_call",call_id:h.call_id,name:h.name,arguments:m})}let i=[];r.length>0&&i.push({toolCall:{functionCalls:r}}),i.push({serverContent:{generationComplete:!0}});let a=V(e.usage),c={serverContent:{turnComplete:!0}};if(a){let d={};typeof a.input_tokens=="number"&&(d.promptTokenCount=a.input_tokens),typeof a.output_tokens=="number"&&(d.responseTokenCount=a.output_tokens),typeof a.total_tokens=="number"&&(d.totalTokenCount=a.total_tokens),Object.keys(d).length>0&&(c.usageMetadata=d)}return i.push(c),{messages:i,assistantItems:o}}O();var Nn=require("crypto"),$n=require("ws");var to=256,xt=class{snapshots=new Map;store(e,t,r){for(this.snapshots.set(`${e}\0${t}`,r);this.snapshots.size>to;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(e,t){return this.snapshots.get(`${e}\0${t}`)}},no="local",He=n=>n.slice(0,120),St=class{constructor(e,t,r){this.ws=e;this.call=t;this.registry=r;this.clients=t.clients,this.scope=t.scope,e.on("message",o=>this.onMessage(o)),e.on("close",()=>{this.unsubscribeEnd?.(),this.unsubscribeEnd=null,this.detachAudio?.(),this.detachVideoOut?.(),this.call.release?.().catch(()=>{})})}ws;call;registry;config=null;transcript=[];queue=Promise.resolve();proxyHandle=null;unsubscribeEnd=null;audio=null;detachAudio=null;detachVideoOut=null;video=null;activityOpen=!1;activeTurn=null;scope;clients;send(e){this.ws.readyState===this.ws.OPEN&&this.ws.send(JSON.stringify(e))}fail(e){if(e instanceof z){this.ws.close(vn,He(e.message));return}if(e instanceof Y){this.ws.close(Qe,He(e.message));return}if(e instanceof w){this.ws.close(e.closeCode,He(e.message));return}this.ws.close(ye,He(String(e instanceof Error?e.message:e)))}onMessage(e){try{let t=_n(typeof e=="string"?e:Buffer.concat(Array.isArray(e)?e:[e]).toString("utf8"));if(t.kind==="setup"){this.handleSetup(t.setup).catch(o=>this.fail(o));return}if(!this.config)throw new w(`"${t.kind}" before setup \u2014 the first client message must be BidiGenerateContentSetup`);let r=this.config;if(t.kind==="clientContent"){let{items:o,turnComplete:s}=On(t.clientContent);this.transcript.push(...o),s&&this.enqueue(()=>this.runTurn());return}if(t.kind==="realtimeInput"){let o=Pn(t.realtimeInput);if(o.kind==="activityStart"||o.kind==="activityEnd"){this.onActivity(o.kind,r);return}if(o.kind==="text"){this.transcript.push({role:"user",content:o.text}),this.enqueue(()=>this.runTurn());return}if(o.kind==="video"){this.appendVideoFrame(o.jpegFrame);return}this.appendAudio(o.kind==="audio"?o.base64Pcm24k:In);return}this.transcript.push(...Cn(t.toolResponse)),this.enqueue(()=>this.runTurn())}catch(t){this.fail(t)}}async handleSetup(e){if(this.config)throw new w("setup may only be sent once, as the first client message");let t=Sn(e),r=t.sessionResumption.handle;if(r!==null){let s=this.registry.get(this.scope,r);if(!s)throw new w("unknown sessionResumption.handle \u2014 the Live session it names is gone (proxy restarted, handle evicted, or never issued) and cannot be resumed; reconnect without a handle to start a new session",Qe);if(s.model!==t.model)throw new w(`sessionResumption.handle was issued for model "${s.model}" \u2014 the model cannot change on resume (got "${t.model}", per the Live API session-resumption contract)`);if(t.systemItems.length>0)throw new w("setup.systemInstruction cannot be changed on resume \u2014 it is part of the resumed conversation state");if(this.call.clientsForResume!==void 0&&(this.clients=await this.call.clientsForResume(r)),this.proxyHandle=await this.clients.sessionHandle(t.model),this.proxyHandle!==s.proxyHandle)throw new w(`sessionResumption.handle named pooled session "${s.proxyHandle}" but the re-attach landed on "${this.proxyHandle}" \u2014 the conversation's native session changed and the snapshot cannot be adopted; reconnect without a handle to start a new session`,Qe);this.unsubscribeEnd=await this.clients.onSessionEnd(s.proxyHandle,i=>this.enqueue(()=>this.handleUpstreamEnd(i))),this.transcript=[...s.transcript]}else this.proxyHandle=await this.clients.sessionHandle(t.model),this.unsubscribeEnd=await this.clients.onSessionEnd(this.proxyHandle,s=>this.enqueue(()=>this.handleUpstreamEnd(s))),this.transcript.push(...t.systemItems);this.config=t;let o={};if(t.videoOut){let s=this.clients.openVideoOut;if(s){let i=await s.call(this.clients,t.model),a=i.onOutputFrame(d=>this.send(xn(d))),c=i.onLaneError(d=>this.fail(d));this.detachVideoOut=()=>{a(),c()},o[xe]=bn(i.codec)}}this.send({setupComplete:o}),t.audioModality&&this.ensureAudioLane(),t.sessionResumption.enabled&&await this.mintResumptionUpdate()}ensureAudioLane(){if(this.audio)return this.audio;let e=this.config;if(!e)throw new w("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let t=this.clients.openAudio;if(!t)throw new w("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native rt-audio-in/rt-audio-out AudioBridge seam)",ye);return this.audio=(async()=>{let r=await t.call(this.clients,e.model);return this.detachAudio=r.onOutputAudio(o=>this.send(Un(o))),r})(),this.audio.catch(r=>this.fail(r)),this.audio}appendAudio(e){this.ensureAudioLane().then(t=>t.appendInputAudio(e)).catch(t=>this.fail(t))}ensureVideoLane(){if(this.video)return this.video;let e=this.config;if(!e)throw new w("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let t=this.clients.openVideo;if(!t)throw new w("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",ye);return this.video=Promise.resolve(t.call(this.clients,e.model)),this.video.catch(r=>this.fail(r)),this.video}appendVideoFrame(e){this.ensureVideoLane().then(t=>t.sendInputFrame(e)).catch(t=>this.fail(t))}onActivity(e,t){if(!t.activity.manualActivity)throw new w(`realtimeInput.${e} requires manual activity mode \u2014 set setup.realtimeInputConfig.automaticActivityDetection.disabled: true (automatic server-side activity detection is the declared platform follow-up)`);if(e==="activityStart"){if(this.activityOpen)throw new w("activityStart while an activity window is already open (send activityEnd first)");this.activityOpen=!0,t.activity.activityHandling==="START_OF_ACTIVITY_INTERRUPTS"&&this.activeTurn&&(this.activeTurn.interrupted=!0);return}if(!this.activityOpen)throw new w("activityEnd without an open activity window (send activityStart first)");this.activityOpen=!1}enqueue(e){this.queue=this.queue.then(e,()=>{}),this.queue=this.queue.catch(t=>this.fail(t))}async mintResumptionUpdate(){let e=this.config;if(!e?.sessionResumption.enabled||this.ws.readyState!==this.ws.OPEN)return;let t=await this.clients.sessionHandle(e.model);this.proxyHandle=t;let r=(0,Nn.randomUUID)();this.registry.store(this.scope,r,{model:e.model,proxyHandle:t,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(r),this.send(Rn(r))}async handleUpstreamEnd(e){if(this.ws.readyState!==this.ws.OPEN||!this.config)return;this.unsubscribeEnd?.(),this.unsubscribeEnd=null;let t=null;try{t=await this.clients.sessionHandle(this.config.model)}catch{}if(t!==null&&t!==this.proxyHandle){this.proxyHandle=t,this.unsubscribeEnd=await this.clients.onSessionEnd(t,r=>this.enqueue(()=>this.handleUpstreamEnd(r))),this.config.sessionResumption.enabled&&await this.mintResumptionUpdate();return}this.send(En(e.timeLeftMs)),this.ws.close(ye,He(`upstream session ended: ${e.reason}`))}async runTurn(){let e=this.config;if(!e||this.ws.readyState!==this.ws.OPEN)return;let t={interrupted:!1};this.activeTurn=t;try{let r=await this.clients.createResponse({model:e.model,input:[...this.transcript],stream:!0,tools:e.tools,temperature:e.temperature,max_output_tokens:e.maxOutputTokens}),o,s=!1;for await(let c of r){if(t.interrupted)break;let d=c;if(d.type==="error")throw new w(`upstream error: ${JSON.stringify(d)}`,ye);if(d.type==="response.completed"){o=d.response,s=!0;continue}let h=Tn(d);h&&this.send(h)}if(t.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!s)throw new w("upstream produced no response.completed event",ye);let{messages:i,assistantItems:a}=Mn(o);this.transcript.push(...a);for(let c of i)this.send(c);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},Se=class extends Error{constructor(t,r){super(r);this.status=t;this.name="UpgradeAuthError"}status};function ro(n,e,t){let r=new Set;(c=>{if(typeof c=="string")c!==""&&r.add(c);else if(Array.isArray(c))for(let d of c)d!==""&&r.add(d)})(n.headers["x-goog-api-key"]);let s=n.headers.authorization;typeof s=="string"&&s.startsWith("Bearer ")&&s.length>7&&r.add(s.slice(7));let i=e.searchParams.get("key")??void 0;if(i!==void 0&&i!==""&&r.add(i),r.size>1)throw new Se(401,`contradictory credentials: ${r.size} distinct keys presented across the x-goog-api-key/Authorization headers and the ?key= query parameter \u2014 present exactly one`);if(t&&i!==void 0&&i!=="")throw new Se(401,"the ?key= query parameter is not accepted on the hosted endpoint \u2014 send the org API key in the x-goog-api-key header or as Authorization: Bearer");let a;for(let c of r)a=c;if(a===void 0&&t)throw new Se(401,"missing credentials: send the org API key in the x-goog-api-key header or as Authorization: Bearer");return a??""}function Rt(n,e){let t=e.authenticate!==void 0;if(t&&(e.clients!==void 0||e.apiKey!==void 0))throw new TypeError("attachGeminiLive: authenticate (hosted per-request resolution) is mutually exclusive with a fixed clients/apiKey lane");if(!t&&e.clients===void 0)throw new TypeError("attachGeminiLive: pass fixed `clients` (local lane) or `authenticate` (hosted lane)");let r=new $n.WebSocketServer({noServer:!0}),o=new xt;n.on("upgrade",(i,a,c)=>{let d=new URL(i.url??"/","http://localhost");d.pathname===Xe&&(a.on("error",()=>{}),(async()=>{let h=ro(i,d,t),m;if(e.authenticate)m=await e.authenticate(h,i);else{if(e.apiKey!==void 0&&h!==e.apiKey)throw new Se(401,"invalid local proxy api key");m={clients:e.clients,scope:no}}r.handleUpgrade(i,a,c,f=>{new St(f,m,o)})})().catch(h=>{let m=502;(h instanceof Se||typeof h=="object"&&h!==null&&"status"in h&&typeof h.status=="number")&&(m=h.status);let f=m===401?"Unauthorized":m===403?"Forbidden":m===502?"Bad Gateway":"Internal Server Error",g=h instanceof Error?h.message:String(h);a.destroyed||(a.write(`HTTP/1.1 ${m} ${f}\r
|
|
4
|
+
Connection: close\r
|
|
5
|
+
\r
|
|
6
|
+
${g}\r
|
|
7
|
+
`),a.destroy())}))});let s=null;return()=>(s??=(async()=>{for(let i of r.clients)i.close(1001,"server closing");await new Promise((i,a)=>r.close(c=>c===void 0?i():a(c)))})(),s)}O();var _e=require("ws");O();function Te(n){return typeof n.model!="string"||!n.model.trim()?"(default)":n.model.trim().slice(0,128)}var Re=class n{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(e,t,r){this.requests[e]=(this.requests[e]??0)+1,this.localInBytes+=t,this.naiveInBytes+=r}modelEntry(e){let t=this.models[e];if(t)return t;let r=Object.keys(this.models).length>=256?"(other)":e;return this.models[r]??={requests:0,tokens_out:0}}model(e){this.modelEntry(e).requests+=1}track(e,t){let r=this,o=performance.now(),s=null,i=0;return(async function*(){for await(let c of e)c.type==="response.output_text.delta"&&typeof c.delta=="string"&&(s===null&&(s=performance.now(),r.ttftMs.push(s-o),r.ttftMs.length>512&&r.ttftMs.shift()),i+=1,r.tokensOut+=1,r.charsOut+=c.delta.length,t!==void 0&&(r.modelEntry(t).tokens_out+=1)),yield c;let a=(performance.now()-(s??o))/1e3;i>0&&a>0&&(r.tokRates.push(i/a),r.tokRates.length>512&&r.tokRates.shift())})()}static p50(e){if(!e.length)return null;let t=[...e].sort((r,o)=>r-o);return t[Math.floor(t.length/2)]}snapshot(){let e=Object.values(this.requests).reduce((t,r)=>t+r,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:n.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:n.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:e},persistent_connections:1,history_bytes_saved:Math.max(0,this.naiveInBytes-this.localInBytes),history_bytes_saved_pct:this.naiveInBytes>0?Math.round((1-this.localInBytes/this.naiveInBytes)*1e3)/10:null}}}},oo=256;function so(n){if(typeof n=="string")return[{role:"user",content:n}];if(Array.isArray(n))return n;throw new Error("responses input must be a string or an array of messages/items")}var Ee=class extends Error{},Ce=class{conversations=new Map;thread(e,t){let r=so(t);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new Ee(`unknown previous_response_id ${String(e)} (proxy restarts drop stored responses)`);return[...o,...r]}remember(e,t,r,o=!0){this.conversations.set(e,[...t,...r]);for(let s of this.conversations.keys()){if(this.conversations.size<=oo)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},Ln=new WeakMap;function Be(n){let e=Ln.get(n);return e||(e=new Ce,Ln.set(n,e)),e}function Ue(n){let e=n?.output;return Array.isArray(e)?e.filter(t=>t.type==="function_call"):[]}function ve(n){return n.find(e=>e.type==="response.completed")?.response??null}function Me(n){return n.type!=="response.function_call_arguments.delta"||typeof n.tool_index!="number"?null:{tool_index:n.tool_index,call_id:typeof n.call_id=="string"?n.call_id:void 0,name:typeof n.name=="string"?n.name:void 0,delta:typeof n.delta=="string"?n.delta:""}}var we=class{calls=new Map;add(e){let t=this.calls.get(e.tool_index),r=!t;return t||(t={args:""},this.calls.set(e.tool_index,t)),e.call_id&&(t.id=e.call_id),e.name&&(t.name=e.name),t.args+=e.delta,{call:t,isNew:r}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([e,t])=>({type:"function_call",call_id:t.id??`call_${e}`,name:t.name??"",arguments:t.args}))}},io=["<tool_call>","<function="];function ao(n){return io.find(e=>n.includes(e))}function Oe(n,e){return`serving row emitted a prose tool call (literal ${JSON.stringify(e)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${n}" (see catalog parser_defaults)`}function Ae(n,e,t){if(!(!n||e>0))return ao(t)}function Et(n,e){let t=e?.output_text;return typeof t=="string"?t:n.filter(r=>r.type==="response.output_text.delta"&&typeof r.delta=="string").map(r=>r.delta).join("")}function uo(n){let e=n?.output;if(!Array.isArray(e))return[];let t=[];for(let r of e){if(r===null||typeof r!="object")continue;let o=r;o.type==="message"?t.push({role:"assistant",content:o.content??""}):o.type==="function_call"&&t.push({type:"function_call",call_id:o.call_id,name:o.name,arguments:o.arguments})}return t}function Ot(n,e){let t=uo(n);return t.length>0?t:e?[{role:"assistant",content:e}]:[]}var ue=class extends Error{constructor(t,r,o=400,s){super(t);this.code=r;this.status=o;this.param=s}code;status;param};function Ne(n,e){try{return n.thread(e.previous_response_id,e.input)}catch(t){throw t instanceof Ee?new ue(t.message,"previous_response_not_found",400,"previous_response_id"):new ue(String(t instanceof Error?t.message:t),"invalid_input")}}async function At(n,e,t,r){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new ue("instructions must be a string","invalid_instructions");let o=typeof e.instructions=="string"?e.instructions:void 0;return n.createResponse({model:e.model,input:t,stream:r,tools:e.tools,tool_choice:e.tool_choice,temperature:e.temperature,top_p:e.top_p,stop:e.stop,...o!==void 0?{instructions:o}:{},max_output_tokens:e.max_output_tokens,...e.reasoning_effort!==void 0?{reasoning_effort:e.reasoning_effort}:{},...e.chat_template_kwargs!==void 0?{chat_template_kwargs:e.chat_template_kwargs}:{}})}async function je(n){let{body:e,clients:t,store:r,stats:o}=n,s=Ne(r,e);e.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(s))-Buffer.byteLength(JSON.stringify(e.input)));let i=`resp_${Math.random().toString(36).slice(2,14)}`,a=n.remember??e.store!==!1,c=e.store!==!1,d=Te(e);o.model(d);let h=Array.isArray(e.tools)&&e.tools.length>0,m=await At(t,e,s,!0);return{responseId:i,events:co({events:m,conversation:s,responseId:i,modelKey:d,requestHadTools:h,remember:a,persist:c,store:r,stats:o})}}async function*co(n){let{events:e,conversation:t,responseId:r,modelKey:o,requestHadTools:s,remember:i,persist:a,store:c,stats:d}=n,h=[],m=T=>T.response&&typeof T.response=="object"?{...T,response:{...T.response,id:r}}:T,f=`item_${r.slice(5)}`,g=`rs_${r.slice(5)}`,S=!1,I=!1,A=0;function*$(){S||(S=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let K=!1,X=0,Q="";function*se(){K&&(K=!1,yield{type:"response.reasoning_text.done",item_id:g,output_index:X,content_index:0,text:Q},yield{type:"response.output_item.done",output_index:X,item:{id:g,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:Q}],status:"completed"}})}let k=!1,M=0,ne="";function*Z(T){k&&(k=!1,yield{type:"response.output_text.done",item_id:f,output_index:M,content_index:0,text:ne},yield{type:"response.content_part.done",item_id:f,output_index:M,content_index:0,part:{type:"output_text",text:ne}},yield{type:"response.output_item.done",output_index:M,item:{id:f,type:"message",role:"assistant",status:T,content:[{type:"output_text",text:ne}]}})}let re=new we,B=null,ie=-1;function*q(T){if(!B)return;let N=B;B=null,yield{type:"response.function_call_arguments.done",item_id:N.fcId,output_index:N.outputIndex,arguments:N.args},yield{type:"response.output_item.done",output_index:N.outputIndex,item:{id:N.fcId,type:"function_call",call_id:N.callId??N.fcId,name:N.name??"",arguments:N.args,status:T}}}let J=!1;for await(let T of d.track(e,o)){h.push(T);let N=String(T.type??"");if(N==="response.created"||N==="response.in_progress"){S=!0,yield m(T);continue}if(N==="response.output_item.added"){I=!0,yield m(T);continue}if(N==="response.reasoning_text.delta"&&!I){yield*$(),K||(K=!0,X=A++,yield{type:"response.output_item.added",output_index:X,item:{id:g,type:"reasoning",summary:[],content:[],status:"in_progress"}}),Q+=String(T.delta??""),yield{type:"response.reasoning_text.delta",item_id:g,output_index:X,content_index:0,delta:T.delta};continue}if(N==="response.output_text.delta"&&!I){yield*$(),yield*se(),k||(k=!0,M=A++,yield{type:"response.output_item.added",output_index:M,item:{id:f,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:f,output_index:M,content_index:0,part:{type:"output_text",text:""}}),ne+=String(T.delta??""),yield{type:N,item_id:f,output_index:M,content_index:0,delta:T.delta};continue}let H=I?null:Me(T);if(H){if(yield*$(),yield*se(),yield*Z("completed"),re.add(H),H.tool_index!==ie){yield*q("completed"),ie=H.tool_index;let ae=A++;B={fcId:`fc_${r.slice(5)}_${H.tool_index}`,outputIndex:ae,callId:H.call_id,name:H.name,args:""},yield{type:"response.output_item.added",output_index:ae,item:{id:B.fcId,type:"function_call",call_id:B.callId??B.fcId,name:B.name??"",arguments:"",status:"in_progress"}}}B&&(H.call_id&&(B.callId=H.call_id),H.name&&(B.name=H.name),B.args+=H.delta,yield{type:"response.function_call_arguments.delta",item_id:B.fcId,output_index:B.outputIndex,delta:H.delta});continue}if(N==="response.completed"&&!I){let ae=Ue(T.response),be=Ae(s,re.size+ae.length,ne);if(be){J=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:Oe(o,be)}};break}if(yield*se(),yield*Z("completed"),yield*q("completed"),re.size===0)for(let ce of ae){let oe=A++,E=ce.id??ce.call_id??`fc_${oe}`,ee=ce.arguments??"",L={id:E,type:"function_call",call_id:ce.call_id??E,name:ce.name??""};yield{type:"response.output_item.added",output_index:oe,item:{...L,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:E,output_index:oe,delta:ee},yield{type:"response.function_call_arguments.done",item_id:E,output_index:oe,arguments:ee},yield{type:"response.output_item.done",output_index:oe,item:{...L,arguments:ee,status:"completed"}}}yield m(T);continue}yield m(T)}if(yield*se(),yield*Z("incomplete"),yield*q("incomplete"),i&&!J){let T=ve(h);c.remember(r,t,Ot(T,Et(h,T)),a)}}var Ct="/v1/responses",lo=64*1024*1024,po=8*1024*1024,fo=16,Dn=32,Fn=3600*1e3,mo=/^[A-Za-z0-9._-]{1,256}$/;function te(n,e,t){return{type:"error",status:n,...t!==void 0?{stream_id:t}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var ho=te(400,{type:"invalid_request_error",code:"invalid_stream_id",message:"The 'stream_id' field must be a non-empty string with at most 256 characters and may only contain letters, numbers, underscores, hyphens, and periods.",param:"stream_id"});function Tt(){let n,e;return{promise:new Promise((r,o)=>{n=r,e=o}),resolve:n,reject:e}}async function yo(n){for(;n.bufferedAmount>po&&n.readyState===_e.WebSocket.OPEN;){let{promise:e,resolve:t}=Tt();setTimeout(t,5),await e}}var It=class extends Ce{constructor(t){super();this.shared=t}shared;thread(t,r){if(t==null)return super.thread(t,r);try{return super.thread(t,r)}catch(o){if(!(o instanceof Ee))throw o;return this.shared.thread(t,r)}}remember(t,r,o,s=!0){s?this.shared.remember(t,r,o):super.remember(t,r,o)}forget(t){super.forget(t),this.shared.forget(t)}},Pt=class{constructor(e,t,r,o){this.ws=e;this.clients=t;this.stats=r;this.statusOf=o;this.store=new It(Be(t)),e.on("message",(i,a)=>this.onMessage(i,a)),e.on("close",()=>this.dispose()),e.on("error",()=>this.dispose()),setTimeout(()=>{this.closed||(this.sendFrame(te(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${Fn/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},Fn).unref?.()}ws;clients;stats;statusOf;store;lanes=new Map;laneOfResponse=new Map;activeTurns=new Set;active=0;waiters=[];closed=!1;onMessage(e,t){if(this.closed)return;if(t){this.sendFrame(te(400,{type:"invalid_request_error",code:"binary_frame",message:"Responses WebSocket frames must be JSON text"})),this.ws.close(1003,"binary frames are not supported");return}let r;try{r=JSON.parse(e.toString())}catch{this.sendFrame(te(400,{type:"invalid_request_error",code:"invalid_json",message:"frame is not valid JSON"}));return}if(r===null||typeof r!="object"||Array.isArray(r)){this.sendFrame(te(400,{type:"invalid_request_error",code:"invalid_event",message:"frame must be a JSON object"}));return}let o=r;if(o.type!=="response.create"){this.sendFrame(te(400,{type:"invalid_request_error",code:"unknown_event",message:`unknown client event type ${JSON.stringify(o.type??null)}; only "response.create" is supported`}));return}if("stream"in o){this.sendFrame(te(400,{type:"invalid_request_error",code:"stream_is_implicit",message:"'stream' is implicit over the Responses WebSocket and must not be sent",param:"stream"}));return}if("background"in o){this.sendFrame(te(400,{type:"invalid_request_error",code:"background_not_supported",message:"'background' is not supported over the Responses WebSocket",param:"background"}));return}let s;if(o.stream_id!==void 0){if(typeof o.stream_id!="string"||!mo.test(o.stream_id)){this.sendFrame(ho);return}if(s=o.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=Dn){this.sendFrame(te(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${Dn}). Reuse an existing stream_id or open a new WebSocket connection.`,param:"stream_id"},s));return}}let i=o.generate===!1?()=>this.runWarmup(o,s):()=>this.runTurn(o,s),a=s??"",c=this.lanes.get(a)??Promise.resolve();this.lanes.set(a,c.then(i).catch(()=>{}))}namedLaneCount(){return this.lanes.has("")?this.lanes.size-1:this.lanes.size}async runTurn(e,t){if(!this.closed){await this.acquire();try{let r=Buffer.byteLength(JSON.stringify(e));this.stats.request("responses_ws",r,r);let o;try{o=await je({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0})}catch(i){this.failTurn(i,t,e);return}this.laneOfResponse.set(o.responseId,t??null);let s=o.events;this.activeTurns.add(s);try{for await(let i of s){if(this.closed||this.ws.readyState!==_e.WebSocket.OPEN)break;this.send(i,t),await yo(this.ws)}}finally{this.activeTurns.delete(s)}}catch(r){this.failTurn(r,t,e)}finally{this.release()}}}runWarmup(e,t){if(this.closed)return;let r=`resp_${Math.random().toString(36).slice(2,14)}`;try{let o=Ne(this.store,{...e,input:e.input??(e.previous_response_id!=null?[]:void 0)});this.store.remember(r,o,[],e.store!==!1),this.laneOfResponse.set(r,t??null),this.send({type:"response.created",response:{id:r,object:"response",status:"in_progress"}},t)}catch(o){this.failTurn(o,t,e)}}failTurn(e,t,r){this.sendFrame(this.errorFrameOf(e,t));let o=r.previous_response_id;typeof o=="string"&&this.laneOfResponse.get(o)===(t??null)&&(this.store.forget(o),this.laneOfResponse.delete(o))}errorFrameOf(e,t){let r=String(e instanceof Error?e.message:e);if(e instanceof ue)return te(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},t);if(e instanceof z)return te(404,{type:"invalid_request_error",code:"model_not_found",message:r},t);if(e instanceof Y)return te(503,{type:"upstream_error",code:"session_gone",message:r},t);let o=this.statusOf?.(e)??null;return o?te(o.status,{type:o.openaiType,message:r},t):te(500,{type:"upstream_error",message:r},t)}async acquire(){for(;this.active>=fo;){let{promise:e,resolve:t}=Tt();this.waiters.push(t),await e}this.active++}release(){this.active--,this.waiters.shift()?.()}dispose(){if(!this.closed){this.closed=!0;for(let e of[...this.activeTurns])e.return(void 0);this.activeTurns.clear(),this.lanes.clear(),this.laneOfResponse.clear();for(let e of this.waiters.splice(0))e()}}send(e,t){if(this.closed||this.ws.readyState!==_e.WebSocket.OPEN)return;let r=t===void 0?e:{...e,stream_id:t};this.ws.send(JSON.stringify(r))}sendFrame(e){this.closed||this.ws.readyState!==_e.WebSocket.OPEN||this.ws.send(JSON.stringify(e))}};function Ze(n,e,t){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${t}\r
|
|
8
|
+
`;n.end(`HTTP/1.1 ${e} ${r}\r
|
|
9
|
+
Connection: close\r
|
|
10
|
+
Content-Type: text/plain; charset=utf-8\r
|
|
11
|
+
Content-Length: ${Buffer.byteLength(o)}\r
|
|
12
|
+
\r
|
|
13
|
+
${o}`)}function Ut(n,e){let t=new _e.WebSocketServer({noServer:!0,maxPayload:lo}),r=new Re,o=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,c=!1,d=(h,m,f)=>{let g;try{g=new URL(h.url??"/","http://localhost")}catch{Ze(m,400,"malformed upgrade URL");return}if(m.on("error",()=>m.destroy()),m.on("close",()=>i.delete(m)),g.pathname!==Ct){i.add(m);return}s.add(m);let S=!1;(async()=>{if(e.apiKey&&(h.headers.authorization??"")!==`Bearer ${e.apiKey}`){Ze(m,401,"invalid local proxy api key");return}let I;try{I=await a(h)}catch(A){let $=e.statusOf?.(A)??null;Ze(m,$?.status??500,"upstream resolver rejected the connection");return}c||m.destroyed||(S=!0,t.handleUpgrade(h,m,f,A=>{s.delete(m),o.add(A),A.on("close",()=>o.delete(A));try{new Pt(A,I,r,e.statusOf)}catch{o.delete(A),A.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!S&&!m.destroyed&&Ze(m,500,"responses websocket upgrade failed")}).finally(()=>s.delete(m))};return n.on("upgrade",d),{stats:r,close:()=>{let{promise:h,resolve:m}=Tt();if(c)return m(),h;c=!0,n.off("upgrade",d);for(let g of s)g.destroy();s.clear();for(let g of i)g.destroy();i.clear();for(let g of o)g.close(1001,"server shutting down");let f=setTimeout(()=>{for(let g of o)g.readyState!==_e.WebSocket.CLOSED&&g.terminate()},2e3);return f.unref?.(),t.close(()=>{clearTimeout(f),m()}),h}}}O();var go=require("http");function $t(n){n.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function pe(n,e,t){n.writeHead(e,{"content-type":"application/json"}),n.end(JSON.stringify(t))}function de(n,e,t,r="invalid_request_error"){pe(n,e,{error:{message:t,type:r}})}function me(n,e,t,r){pe(n,e,{type:"error",error:{type:t,message:r}})}function et(n,e,t){if(e==="anthropic"){pe(n,404,{type:"error",error:{type:"not_found_error",message:t.message}});return}pe(n,404,{error:{message:t.message,type:"invalid_request_error",code:"model_not_found"}})}var Hn=64*1024*1024,tt=class extends Error{};async function wo(n){let e=[],t=0;for await(let o of n){if(t+=o.length,t>Hn)throw new tt(`request body exceeds ${Hn} bytes`);e.push(o)}let r=Buffer.concat(e).toString("utf8");return r?JSON.parse(r):{}}function $e(n,e,t,r){return{id:n,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:e,choices:[{index:0,delta:t,finish_reason:r}]}}function nt(n){let e=n?.usage;if(!e||typeof e!="object")return null;let t=i=>typeof i=="number"&&Number.isFinite(i)&&i>=0?i:0,r=t(e.input_tokens),o=t(e.output_tokens);if(r===0&&o===0)return null;let s=t(e.total_tokens)||r+o;return{input_tokens:r,output_tokens:o,total_tokens:s}}function Bn(n){let e=nt(n);return e?{prompt_tokens:e.input_tokens,completion_tokens:e.output_tokens,total_tokens:e.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function jn(n){let e=n?.output;if(!Array.isArray(e))return"";let t="";for(let r of e)if(!(r.type!=="reasoning"||!Array.isArray(r.content)))for(let o of r.content)o.type==="reasoning_text"&&(t+=String(o.text??""));return t}function vo(n){if(Array.isArray(n))return n.map(e=>{let t=e.function;return!t||typeof t!="object"?e:{type:"function",name:t.name,description:t.description,parameters:t.parameters,strict:t.strict}})}var rt=class extends Error{};function _o(n,e){return Array.isArray(e)?e.map(t=>{let r=t;if(r.type==="text")return{type:n==="assistant"?"output_text":"input_text",text:r.text};if(r.type==="image_url"){let o=r.image_url??{};return{type:"input_image",image_url:o.url,detail:o.detail}}throw new rt(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function ko(n){let e=[];for(let t of n){if(t.role==="tool"){e.push({type:"function_call_output",call_id:t.tool_call_id,output:typeof t.content=="string"?t.content:JSON.stringify(t.content??"")});continue}if(t.role==="assistant"&&Array.isArray(t.tool_calls)){typeof t.content=="string"&&t.content&&e.push({role:"assistant",content:t.content});for(let r of t.tool_calls){let o=r.function??{};e.push({type:"function_call",call_id:r.id,name:o.name,arguments:o.arguments})}continue}e.push({...t,content:_o(t.role,t.content)})}return e}function Mt(n){return Ue(n).map((e,t)=>({index:t,id:e.call_id??e.id??`call_${t}`,type:"function",function:{name:e.name??"",arguments:e.arguments??"{}"}}))}async function bo(n,e,t,r,o){if(n.stream===!0){let A;try{A=await je({body:n,clients:e,store:t,stats:o})}catch($){if($ instanceof z){et(r,"openai",$);return}if($ instanceof ue){de(r,$.status,$.message);return}throw $}$t(r);for await(let $ of A.events)r.write(`event: ${String($.type??"message")}
|
|
14
|
+
data: ${JSON.stringify($)}
|
|
4
15
|
|
|
5
|
-
`)
|
|
16
|
+
`);r.write(`data: [DONE]
|
|
6
17
|
|
|
7
|
-
`),r.end();return}let
|
|
18
|
+
`),r.end();return}let i;try{i=Ne(t,n)}catch(A){if(A instanceof ue){de(r,A.status,A.message);return}throw A}n.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(i))-Buffer.byteLength(JSON.stringify(n.input)));let a=`resp_${Math.random().toString(36).slice(2,14)}`,c=n.store!==!1,d=Te(n);o.model(d);let h=Array.isArray(n.tools)&&n.tools.length>0,m;try{m=await At(e,n,i,!1)}catch(A){if(A instanceof z){et(r,"openai",A);return}if(A instanceof ue){de(r,A.status,A.message);return}throw A}let f=[],g=null;for await(let A of o.track(m,d)){if(f.push(A),A.type==="error"){de(r,502,JSON.stringify(A),"upstream_error");return}A.type==="response.completed"&&(g=A.response)}if(g==null){de(r,502,"upstream produced no response.completed event","upstream_error");return}let S=Et(f,g),I=Ae(h,Ue(g).length,S);if(I){de(r,502,Oe(d,I),"upstream_error");return}c&&t.remember(a,i,Ot(g,S),c),pe(r,200,{...g,id:a})}async function xo(n,e,t,r){let o=n.messages;if(!Array.isArray(o)){de(t,400,"chat/completions requires a messages array");return}let s=String(n.model??"urun"),i=`chatcmpl-${Math.random().toString(36).slice(2,14)}`,a;try{a=ko(o)}catch(k){if(k instanceof rt){de(t,400,k.message);return}throw k}let c=Te(n);r.model(c);let d=Array.isArray(n.tools)&&n.tools.length>0,h;try{h=await e.createResponse({model:n.model,input:a,stream:!0,tools:vo(n.tools),tool_choice:n.tool_choice,temperature:n.temperature,top_p:n.top_p,stop:n.stop,max_output_tokens:n.max_completion_tokens??n.max_tokens,...n.reasoning_effort!==void 0?{reasoning_effort:n.reasoning_effort}:{},...n.chat_template_kwargs!==void 0?{chat_template_kwargs:n.chat_template_kwargs}:{}})}catch(k){if(k instanceof z){et(t,"openai",k);return}throw k}if(n.stream===!0){$t(t);let k=[],M=new we,ne="";t.write(`data: ${JSON.stringify($e(i,s,{role:"assistant"},null))}
|
|
8
19
|
|
|
9
|
-
`);for await(let
|
|
20
|
+
`);for await(let q of r.track(h,c)){k.push(q);let J=Me(q);if(q.type==="response.output_text.delta"&&typeof q.delta=="string")ne+=q.delta,t.write(`data: ${JSON.stringify($e(i,s,{content:q.delta},null))}
|
|
10
21
|
|
|
11
|
-
`);else if(
|
|
22
|
+
`);else if(q.type==="response.reasoning_text.delta"&&typeof q.delta=="string")t.write(`data: ${JSON.stringify($e(i,s,{reasoning_content:q.delta},null))}
|
|
12
23
|
|
|
13
|
-
`);else if(
|
|
24
|
+
`);else if(J){let{call:T,isNew:N}=M.add(J),H=N?{index:J.tool_index,id:T.id??`call_${J.tool_index}`,type:"function",function:{name:T.name??"",arguments:J.delta}}:{index:J.tool_index,function:{arguments:J.delta}};t.write(`data: ${JSON.stringify($e(i,s,{tool_calls:[H]},null))}
|
|
14
25
|
|
|
15
|
-
`)}else if(
|
|
26
|
+
`)}else if(q.type==="error"){t.write(`data: ${JSON.stringify({error:q})}
|
|
16
27
|
|
|
17
|
-
`),
|
|
28
|
+
`),t.end();return}}let Z=Mt(ve(k)),re=Ae(d,M.size+Z.length,ne);if(re){t.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:Oe(c,re)}})}
|
|
18
29
|
|
|
19
|
-
`),
|
|
30
|
+
`),t.end();return}M.size===0&&Z.length>0&&t.write(`data: ${JSON.stringify($e(i,s,{tool_calls:Z},null))}
|
|
20
31
|
|
|
21
|
-
`);let
|
|
32
|
+
`);let B=M.size>0||Z.length>0;t.write(`data: ${JSON.stringify($e(i,s,{},B?"tool_calls":"stop"))}
|
|
22
33
|
|
|
23
|
-
`)
|
|
34
|
+
`),nt(ve(k))&&t.write(`data: ${JSON.stringify({id:i,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:s,choices:[],usage:Bn(ve(k))})}
|
|
24
35
|
|
|
25
|
-
`),
|
|
36
|
+
`),t.write(`data: [DONE]
|
|
26
37
|
|
|
27
|
-
`),
|
|
28
|
-
data: ${JSON.stringify({type:
|
|
38
|
+
`),t.end();return}let m="",f="",g=[],S=new we;for await(let k of r.track(h,c)){g.push(k),k.type==="response.output_text.delta"&&typeof k.delta=="string"&&(m+=k.delta),k.type==="response.reasoning_text.delta"&&typeof k.delta=="string"&&(f+=k.delta);let M=Me(k);if(M&&S.add(M),k.type==="error"){de(t,502,JSON.stringify(k),"upstream_error");return}}let I=ve(g),A=Mt(I),$=A.length>0?A:Mt({output:S.items()}),K=Ae(d,$.length,m);if(K){de(t,502,Oe(c,K),"upstream_error");return}let X=$.map(({index:k,...M})=>M),Q={role:"assistant",content:m||null},se=f||jn(I);se&&(Q.reasoning_content=se),X.length>0&&(Q.tool_calls=X),pe(t,200,{id:i,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:s,choices:[{index:0,message:Q,finish_reason:X.length>0?"tool_calls":"stop"}],usage:Bn(I)})}function So(n){return typeof n=="string"?n:Array.isArray(n)?n.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var Ge=class extends Error{};function ot(n){return new Ge(`unsupported anthropic content block type "${n}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function Ro(n){if(Array.isArray(n))return n.map(e=>{let t=e;return{type:"function",name:t.name,description:t.description,parameters:t.input_schema}})}function Eo(n){if(n==null)return;let e=n.type;if(e==="auto")return"auto";if(e==="none")return"none";if(e==="any")return"required";if(e==="tool")return{type:"function",name:n.name};throw new Ge(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function Oo(n){if(typeof n=="string")return n;if(n==null)return"";if(!Array.isArray(n))throw ot(typeof n);let e="";for(let t of n){let r=String(t.type??"");if(r!=="text")throw ot(`tool_result > ${r}`);e+=String(t.text??"")}return e}function Ao(n){let{role:e,content:t}=n;if(typeof t=="string")return[{role:e,content:t}];if(t==null)return[];if(!Array.isArray(t))throw ot(typeof t);let r=[],o="",s=()=>{o&&(r.push({role:e,content:o}),o="")};for(let i of t){let a=String(i.type??"");if(a==="text")o+=String(i.text??"");else if(a==="tool_use")s(),r.push({type:"function_call",call_id:i.id,name:i.name,arguments:JSON.stringify(i.input??{})});else if(a==="tool_result")s(),r.push({type:"function_call_output",call_id:i.tool_use_id,output:Oo(i.content)});else{if(a==="thinking"||a==="redacted_thinking")continue;throw ot(a)}}return s(),r}function Nt(n){return Ue(n).map((e,t)=>{let r;try{r=JSON.parse(e.arguments||"{}")}catch{throw new Error(`upstream function_call "${e.name??""}" arguments are not valid JSON`)}return{type:"tool_use",id:e.call_id??e.id??`toolu_${t}`,name:e.name??"",input:r}})}async function Io(n,e,t,r){let o=n.messages;if(!Array.isArray(o)){me(t,400,"invalid_request_error","messages requires a messages array");return}if(typeof n.max_tokens!="number"||!Number.isInteger(n.max_tokens)||n.max_tokens<1){me(t,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(n.top_k!==void 0){me(t,400,"invalid_request_error","top_k is not supported: the uRun serve lane exposes temperature/top_p/stop_sequences only");return}if(n.stop_sequences!==void 0&&(!Array.isArray(n.stop_sequences)||n.stop_sequences.some(k=>typeof k!="string"))){me(t,400,"invalid_request_error","stop_sequences must be an array of strings");return}let s=String(n.model??"urun"),i=`msg_${Math.random().toString(36).slice(2,14)}`,a=[],c;try{n.system&&a.push({role:"system",content:So(n.system)});for(let k of o)a.push(...Ao(k));c=Eo(n.tool_choice)}catch(k){if(k instanceof Ge){me(t,400,"invalid_request_error",k.message);return}throw k}let d=Te(n);r.model(d);let h=Array.isArray(n.tools)&&n.tools.length>0,m;try{m=await e.createResponse({model:n.model,input:a,stream:!0,tools:Ro(n.tools),tool_choice:c,temperature:n.temperature,max_output_tokens:n.max_tokens,top_p:n.top_p,stop:n.stop_sequences})}catch(k){if(k instanceof z){et(t,"anthropic",k);return}throw k}let f=(k,M)=>{t.destroyed||t.write(`event: ${k}
|
|
39
|
+
data: ${JSON.stringify({type:k,...M})}
|
|
29
40
|
|
|
30
|
-
`)};if(
|
|
31
|
-
data: ${JSON.stringify({type:"error",error:{type:"api_error",message:
|
|
41
|
+
`)};if(n.stream===!0){let k=r.track(m,d);$t(t),f("message_start",{message:{id:i,type:"message",role:"assistant",content:[],model:s,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let M=0,ne="",Z=0,re="none",B=()=>re,ie=-1,q=-1,J=new we,T=[],N=()=>{re!=="none"&&(re="none",f("content_block_stop",{index:ie}))},H=(L,U)=>{N(),re=L,ie=Z++,f("content_block_start",{index:ie,content_block:U})};for await(let L of k){if(t.destroyed)break;T.push(L);let U=Me(L);if(L.type==="response.reasoning_text.delta"&&typeof L.delta=="string")B()!=="thinking"&&H("thinking",{type:"thinking",thinking:""}),f("content_block_delta",{index:ie,delta:{type:"thinking_delta",thinking:L.delta}});else if(L.type==="response.output_text.delta"&&typeof L.delta=="string")B()!=="text"&&H("text",{type:"text",text:""}),M+=1,ne+=L.delta,f("content_block_delta",{index:ie,delta:{type:"text_delta",text:L.delta}});else if(U){let{call:qe}=J.add(U);(B()!=="tool"||U.tool_index!==q)&&(q=U.tool_index,H("tool",{type:"tool_use",id:qe.id??`call_${U.tool_index}`,name:qe.name??"",input:{}})),U.delta&&f("content_block_delta",{index:ie,delta:{type:"input_json_delta",partial_json:U.delta}})}else if(L.type==="error"){f("error",{error:{type:"api_error",message:JSON.stringify(L)}}),t.end();return}}if(t.destroyed)return;let ae=ve(T),be=Ue(ae),ce=Ae(h,J.size+be.length,ne);if(ce){f("error",{error:{type:"api_error",message:Oe(d,ce)}}),t.end();return}N();let oe=[];if(J.size===0)try{oe=Nt(ae)}catch(L){f("error",{error:{type:"api_error",message:String(L instanceof Error?L.message:L)}}),t.end();return}Z===0&&oe.length===0&&(H("text",{type:"text",text:""}),N()),oe.forEach(L=>{let U=Z++;f("content_block_start",{index:U,content_block:{...L,input:{}}}),f("content_block_delta",{index:U,delta:{type:"input_json_delta",partial_json:JSON.stringify(L.input??{})}}),f("content_block_stop",{index:U})});let E=J.size>0||oe.length>0,ee=nt(ae);f("message_delta",{delta:{stop_reason:E?"tool_use":"end_turn"},usage:ee?{input_tokens:ee.input_tokens,output_tokens:ee.output_tokens}:{output_tokens:M}}),f("message_stop",{}),t.end();return}let g="",S="",I=[],A=new we;for await(let k of r.track(m,d)){I.push(k),k.type==="response.output_text.delta"&&typeof k.delta=="string"&&(g+=k.delta),k.type==="response.reasoning_text.delta"&&typeof k.delta=="string"&&(S+=k.delta);let M=Me(k);if(M&&A.add(M),k.type==="error"){me(t,502,"api_error",JSON.stringify(k));return}if(t.destroyed)break}if(t.destroyed)return;let $=ve(I),K;try{K=Nt($),K.length===0&&A.size>0&&(K=Nt({output:A.items()}))}catch(k){me(t,502,"api_error",String(k instanceof Error?k.message:k));return}let X=Ae(h,K.length,g);if(X){me(t,502,"api_error",Oe(d,X));return}let Q=[],se=S||jn($);se&&Q.push({type:"thinking",thinking:se}),(g||K.length===0)&&Q.push({type:"text",text:g}),Q.push(...K),pe(t,200,{id:i,type:"message",role:"assistant",content:Q,model:s,stop_reason:K.length>0?"tool_use":"end_turn",usage:nt($)??{input_tokens:0,output_tokens:0}})}function Gn(n){let{clients:e,apiKey:t,identity:r,statusOf:o}=n,s=typeof e=="function"?e:()=>e,i=new Re;return(a,c)=>{let d=new URL(a.url??"/","http://localhost"),h=d.pathname==="/v1/messages"||d.pathname.startsWith("/v1/messages/"),m=(f,g,S,I)=>h?me(c,f,I,g):de(c,f,g,S);(async()=>{if(a.method==="GET"&&d.pathname==="/healthz"){pe(c,200,{ok:!0});return}if(a.method==="GET"&&d.pathname==="/stats"){pe(c,200,r?{identity:r,...i.snapshot()}:i.snapshot());return}if(t&&(a.headers.authorization??"")!==`Bearer ${t}`){m(401,"invalid local proxy api key","authentication_error","authentication_error");return}let f=await s(a);if(a.method==="GET"&&d.pathname==="/v1/models"){if(d.searchParams.get("format")==="openrouter"){if(!f.openRouterModels){m(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}pe(c,200,await f.openRouterModels());return}pe(c,200,await f.listModels());return}if(a.method!=="POST"){m(404,`no route for ${a.method} ${d.pathname}`,"invalid_request_error","not_found_error");return}let g;try{g=await wo(a)}catch(I){if(I instanceof tt){m(413,I.message,"request_too_large","request_too_large");return}m(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let S=Buffer.byteLength(JSON.stringify(g));if(d.pathname==="/v1/responses"){i.request("responses",S,S),await bo(g,f,Be(f),c,i);return}if(d.pathname==="/v1/chat/completions"){i.request("chat_completions",S,S),await xo(g,f,c,i);return}if(d.pathname==="/v1/messages"){i.request("messages",S,S),await Io(g,f,c,i);return}if(d.pathname==="/v1/messages/count_tokens"){i.request("count_tokens",S,S),me(c,404,"not_found_error","count_tokens is not implemented by the uRun proxy: the serve transport exposes no token-count lane, and the proxy will not fabricate counts");return}m(404,`no route for POST ${d.pathname}`,"invalid_request_error","not_found_error")})().catch(f=>{let g=String(f instanceof Error?f.message:f);if(!c.headersSent){let S=o?.(f)??null;if(S){m(S.status,g,S.openaiType,S.anthropicType);return}m(500,g,"proxy_error","api_error");return}c.destroyed||(h?c.write(`event: error
|
|
42
|
+
data: ${JSON.stringify({type:"error",error:{type:"api_error",message:g}})}
|
|
32
43
|
|
|
33
|
-
`):
|
|
44
|
+
`):c.write(`data: ${JSON.stringify({error:{message:g,type:"upstream_error"}})}
|
|
34
45
|
|
|
35
|
-
`)),h.end()})}}S();var Ve=require("@urun-sh/core"),me=class extends Error{constructor(e,r=401){super(e);this.status=r;this.name="ProxyAuthError"}status},Se=class extends Error{constructor(n){super(n),this.name="ControlPlaneUnavailableError"}};function Mt(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 tr=60,nr=6e4,rr=4096;function or(t){return t===401||t===403}var je=class{constructor(n){this.opts=n;this.mint=n.mint??Ve.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<nr)return{apiKey:n,orgId:e.orgId};let r;try{r=(await this.mint(n,{baseUrl:this.opts.gatewayUrl,expiresIn:tr})).orgId}catch(o){throw o instanceof Ve.ClientTokenError&&or(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 Se(`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 Se("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>=rr){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 qt=require("crypto");S();var Pe=require("@urun-sh/core");S();async function Nt(t){let e=await(t.fetchImpl??fetch)(`${t.catalogUrl}/model_catalog?select=model_id,variant`,{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 $t(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 Lt(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.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 Dt(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 sr="llm-resp";async function*st(t,n){let e=`${sr}:${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 a=u;if(a.t==="delta"){if(typeof a.delta=="string"&&(yield{type:"response.output_text.delta",item_id:r,delta:a.delta}),typeof a.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:r,delta:a.reasoning}),Array.isArray(a.tool_calls))for(let l of a.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(a.t==="response"){yield{type:"response.completed",response:{id:r,status:"completed",...a.body}};return}else if(a.t==="error"){yield Dt(a);return}}}var ir="llm",He=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(ir).set({requests:{[n.request_id]:{payload:n,consumer_id:n.consumer_id,stream:n.stream}}})}sendResponses(n,e){let r=Lt(n,e,this.consumerId);return this.write(r),st(this.session,e)}sendResponseCreate(n,e,r){let o=$t(n,e,r,this.consumerId);return this.write(o),st(this.session,r)}};var jt=0;function ar(){return jt+=1,`req_${Date.now().toString(36)}_${jt.toString(36)}`}var Be=class{transport;constructor(n){this.transport=new He(n)}responses={create:async n=>{let e=ar(),r=this.transport.sendResponses(n,e);return Object.assign((async function*(){yield*r})(),{requestId:e})}}};S();function ur(t){let n=t?.type;return n==="response.created"||n==="response.in_progress"}function cr(t){let n=t?.type;return n==="response.completed"||n==="error"}function Vt(t){return async function(e){return(async function*(){for(let o=1;o<=2;o++){let{app:u,entry:a}=await t.sessionFor(e.model),l=!1,d=!1;try{let p=await t.createStream(a,e);for await(let h of p){if(cr(h)&&(l=!0),ur(h)){if(o>1)continue}else d=!0;yield h}}catch(p){if(o<2&&!d){await t.evict(u,a);continue}throw p}if(l)return;if(o<2&&!d){await t.evict(u,a);continue}return}})()}}S();var lr=3,dr="llm",pr=new Set(["paused","error"]);function Ht(t,n){let e=t.doc(dr),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,a=new Set,l=[],d=p=>{if(!u){u=p;for(let h of[...a])h(p);a.clear()}};return r&&l.push(e.onConnectionState(p=>{p.consecutiveFailures<lr||d(new ie(`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=>{pr.has(p.name)&&d(new ie(`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),()=>{}):(a.add(p),()=>a.delete(p))},dispose(){a.clear();for(let p of l.splice(0))p()}}}async function*it(t,n){let e=n.gone();if(e)throw e;let r=t[Symbol.asyncIterator](),o,u=new Promise((a,l)=>{o=n.onGone(d=>l(d))});u.catch(()=>{});try{for(;;){let a=await Promise.race([r.next(),u]);if(a.done)return;yield a.value}}finally{o?.(),r.return?.(void 0)}}function Bt(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 fr(t,n){let{baseUrl:e,orgId:r,fnName:o,auth:u,subject:a}=t,l=x=>({session:x,responses:new Be(x),gone:Ht(x,n)});if(u.lane==="jwt"){let x=(0,Pe.App)(n,{baseUrl:e,orgId:r,jwt:u.jwt});return l(Bt(x,n,o))}let{apiKey:d,gatewayUrl:p}=u,h=(0,Pe.App)(n,{baseUrl:e,orgId:r,getAccessToken:async()=>(await(0,Pe.createClientToken)(d,{baseUrl:p,expiresIn:300,allowedFunctions:[`${n}/${o}`],...a===void 0?{}:{subject:a}})).token});return l(Bt(h,n,o))}function mr(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 hr=new Set(["error","paused"]);function Ft(t){let{auth:n,catalog:e}=t,r=null,o=a=>{let l=fr(t,a);return l.gone.onGone(()=>{r?.evict(a,l)}),l},u=new Ce({defaultApp:t.defaultApp,fnName:t.fnName,openSession:o,closeSession:async a=>{a.gone.dispose(),await a.session.end()},listApps:n.lane==="api-key"?()=>_t({apiUrl:t.apiUrl,apiKey:n.apiKey}):null,listCatalog:e?()=>Nt(e):null,sessionKey:mr});return r=u,t.defaultApp!==null&&u.seed(t.defaultApp,o(t.defaultApp)),u}function Gt(t,n=At){let e=new WeakMap,r=new WeakMap,o=new WeakMap;return{createResponse:Vt({sessionFor:u=>t.sessionFor(u),evict:(u,a)=>t.evict(u,a),createStream:async(u,a)=>it(await u.responses.responses.create(a),u.gone)}),listModels:()=>t.modelList(),openAudio:async u=>{let{entry:a}=await t.sessionFor(u),l=e.get(a);return l||(l=(async()=>Rt(a.session,await n()))(),e.set(a,l),l.catch(()=>e.delete(a))),l},openVideo:async u=>{let{entry:a}=await t.sessionFor(u),l=r.get(a);return l||(l=Et(a.session),r.set(a,l)),l},openVideoOut:async u=>{let{entry:a}=await t.sessionFor(u),l=o.get(a);return l||(l=Ot(a.session),o.set(a,l)),l},sessionHandle:async u=>(await t.handleFor(u)).handle,createResponseOn:async(u,a)=>{let{entry:l}=await t.sessionForHandle(u);return it(await l.responses.responses.create(a),l.gone)},onSessionEnd:async(u,a)=>{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||!hr.has(h.name))return;p=!0;let x=d.endsAt??null;a({timeLeftMs:x?Math.max(0,x.getTime()-Date.now()):null,reason:h.reason??`session ${h.name}`})})}}}var gr=512;function yr(t){return`urun-inference-proxy-${(0,qt.createHash)("sha256").update(t).digest("hex").slice(0,32)}`}var Fe=class{constructor(n){this.opts=n}opts;tenants=new Map;build(n){if(this.opts.build)return this.opts.build(n);let e=Ft({baseUrl:this.opts.baseUrl,orgId:n.orgId,defaultApp:null,fnName:kt,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:n.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:yr(n.apiKey)});return{router:e,clients:Gt(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>=gr;){let o=this.tenants.entries().next();if(o.done)break;let[u,a]=o.value;this.tenants.delete(u),a.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 wr=5e3,_r=3e3,vr="/api/client-tokens";function at(t,n,e){t.writeHead(n,{"content-type":"application/json"}),t.end(JSON.stringify(e))}function Jt(t,n,e,r){at(t,n,{error:{message:e,type:r}})}async function kr(t,n=fetch){let e=`${t.replace(/\/+$/,"")}${vr}`;try{let r=await n(e,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(_r)});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 zt(t){let n=t.resolver??new je({gatewayUrl:t.baseUrl}),e=t.registry??new Fe({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),r=t.probeControlPlane??(()=>kr(t.baseUrl)),o=async d=>n.resolve(Mt(d.headers.authorization)),u=Ut({clients:async d=>e.clientsFor(await o(d)),statusOf:d=>d instanceof me?{status:d.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:d instanceof Se?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null}),a=null;return{server:(0,Wt.createServer)((d,p)=>{let h=new URL(d.url??"/","http://localhost");(async()=>{if(d.method==="GET"&&h.pathname==="/healthz"){at(p,200,{ok:!0});return}if(d.method==="GET"&&h.pathname==="/readyz"){let x=Date.now();if(!a||x-a.at>=wr){let A=await r();a={at:x,...A}}at(p,a.ready?200:503,{ok:a.ready,control_plane:a.detail});return}if(h.pathname!=="/v1"&&!h.pathname.startsWith("/v1/")){Jt(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){Jt(p,500,String(x instanceof Error?x.message:x),"proxy_error");return}p.end()})}),closeAll:()=>e.closeAll()}}async function Kt(t,n){let e=new Promise(r=>t.close(()=>r()));t.closeAllConnections(),await e,await n()}async function br(){let t=bt(de.default.env),{server:n,closeAll:e}=zt(t);await new Promise((o,u)=>{n.once("error",u),n.listen(t.port,Ke,o)}),de.default.stdout.write(`inference-proxy listening on http://${Ke}:${t.port}/v1 (control plane ${t.baseUrl}, org API ${t.apiUrl}, catalog oracle ${t.catalog?"on":"off"})
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
`),
|
|
46
|
+
`)),c.end()})}}O();var it=require("@urun-sh/core"),ke=class extends Error{constructor(t,r=401){super(t);this.status=r;this.name="ProxyAuthError"}status},Le=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function Vn(n){let e=(n??"").trim();if(e==="")throw new ke("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let t=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!t)throw new ke("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return t[1].trim()}var Po=60,Co=6e4,To=4096;function Uo(n){return n===401||n===403}var st=class{constructor(e){this.opts=e;this.mint=e.mint??it.createClientToken,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new ke("empty Authorization bearer token");let t=this.cache.get(e);if(t&&this.now()-t.at<Co)return{apiKey:e,orgId:t.orgId};let r;try{r=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:Po})).orgId}catch(o){throw o instanceof it.ClientTokenError&&Uo(o.status)?(this.cache.delete(e),new ke("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new Le(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${o instanceof Error?o.message:String(o)}`)}if(!r)throw new Le("the control plane verified the API key but returned no org_id \u2014 refusing to route a request with no tenancy (control-plane contract violation)");if(this.cache.size>=To){let o=this.cache.keys().next();o.done||this.cache.delete(o.value)}return this.cache.set(e,{orgId:r,at:this.now()}),{apiKey:e,orgId:r}}clear(){this.cache.clear()}};O();var pt=require("crypto");O();var Ve=require("@urun-sh/core");O();async function qn(n){let t=await(n.fetchImpl??fetch)(`${n.catalogUrl}/model_catalog?select=model_id,variant,gpu_spec,engine,task,description,size_gb,hf_repo,engine_args,updated_at`,{headers:{apikey:n.anonKey,Authorization:`Bearer ${n.anonKey}`}});if(!t.ok)throw new Error(`model_catalog fetch failed: ${t.status}`);return await t.json()}O();O();O();function Wn(n,e,t,r){let o=n.response??{},s={request_id:t,consumer_id:r,stream:!0,kind:"chat",messages:e};return typeof o.instructions=="string"&&(s.instructions=o.instructions),Array.isArray(o.modalities)&&(s.modalities=o.modalities),typeof o.temperature=="number"&&(s.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(s.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(s.tools=o.tools),s}function Jn(n,e,t){let r={request_id:e,consumer_id:t,stream:!!n.stream,kind:"responses",input:n.input};return n.model&&(r.model=n.model),n.tools!==void 0&&(r.tools=n.tools),n.tool_choice!==void 0&&(r.tool_choice=n.tool_choice),typeof n.temperature=="number"&&(r.temperature=n.temperature),typeof n.max_output_tokens=="number"&&(r.max_output_tokens=n.max_output_tokens),typeof n.top_p=="number"&&(r.top_p=n.top_p),Array.isArray(n.stop)&&n.stop.length>0&&(r.stop=n.stop),typeof n.reasoning_effort=="string"&&(r.reasoning_effort=n.reasoning_effort),n.chat_template_kwargs!==void 0&&(r.chat_template_kwargs=n.chat_template_kwargs),r}O();O();function Kn(n){if(n instanceof Error)return{type:"error",error:{type:"urun_error",code:n.name||null,message:n.message}};if(n&&typeof n=="object"&&n.t==="error"){let e=n,t=e.body??{};return{type:"error",error:{type:"urun_error",code:e.code??null,message:t.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(n)}}}var Mo="llm-resp";async function*Lt(n,e){let t=`${Mo}:${e}`,r=`resp_${e}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};let o=new Map;for await(let s of n.stream(t).messages()){let i=s;if(i.t==="delta"){if(typeof i.delta=="string"&&(yield{type:"response.output_text.delta",item_id:r,delta:i.delta}),typeof i.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:r,delta:i.reasoning}),Array.isArray(i.tool_calls))for(let a of i.tool_calls){let c=typeof a.index=="number"?a.index:0,d=o.get(c)??{};a.id&&(d.id=a.id),a.function?.name&&(d.name=a.function.name),o.set(c,d),yield{type:"response.function_call_arguments.delta",item_id:`fc_${e}_${c}`,tool_index:c,call_id:d.id,name:d.name,delta:a.function?.arguments??""}}}else if(i.t==="response"){yield{type:"response.completed",response:{id:r,status:"completed",...i.body}};return}else if(i.t==="error"){yield Kn(i);return}}}var No="llm",at=class{constructor(e){this.session=e;this.sessionTag=e.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(e){e.session_tag=this.sessionTag,this.session.doc(No).set({requests:{[e.request_id]:{payload:e,consumer_id:e.consumer_id,stream:e.stream}}})}sendResponses(e,t){let r=Jn(e,t,this.consumerId);return this.write(r),Lt(this.session,t)}sendResponseCreate(e,t,r){let o=Wn(e,t,r,this.consumerId);return this.write(o),Lt(this.session,r)}};var zn=0;function $o(){return zn+=1,`req_${Date.now().toString(36)}_${zn.toString(36)}`}var ut=class{transport;constructor(e){this.transport=new at(e)}responses={create:async e=>{let t=$o(),r=this.transport.sendResponses(e,t);return Object.assign((async function*(){yield*r})(),{requestId:t})}}};O();function Lo(n){let e=n?.type;return e==="response.created"||e==="response.in_progress"}function Do(n){let e=n?.type;return e==="response.completed"||e==="error"}function Yn(n){return async function(t){return(async function*(){for(let o=1;o<=2;o++){let{app:s,entry:i}=await n.sessionFor(t.model),a=!1,c=!1;try{let d=await n.createStream(i,t);for await(let h of d){if(Do(h)&&(a=!0),Lo(h)){if(o>1)continue}else c=!0;yield h}}catch(d){if(o<2&&!c){await n.evict(s,i);continue}throw d}if(a)return;if(o<2&&!c){await n.evict(s,i);continue}return}})()}}O();var Fo=3,Ho="llm",Bo=new Set(["paused","error"]);function Xn(n,e){let t=n.doc(Ho),r=typeof t.onConnectionState=="function",o=typeof n.onPhase=="function";if(!r&&!o)throw new Error(`session for app "${e}" exposes neither a doc connection-state surface nor onPhase \u2014 session-gone detection requires @urun-sh/core Session (SessionDocument.onConnectionState / Session.onPhase); pooling a session with no native lifecycle signal would silently swallow requests dispatched after the platform closes it`);let s=null,i=new Set,a=[],c=d=>{if(!s){s=d;for(let h of[...i])h(d);i.clear()}};return r&&a.push(t.onConnectionState(d=>{d.consecutiveFailures<Fo||c(new Y(`the pooled session for app "${e}" is gone: its doc backhaul failed ${d.consecutiveFailures} consecutive times (last close ${d.lastCloseCode===null?"by provider liveness reap":`code ${d.lastCloseCode}`}${d.lastCloseReason?`, reason ${JSON.stringify(d.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`))})),o&&a.push(n.onPhase(d=>{Bo.has(d.name)&&c(new Y(`the pooled session for app "${e}" lost its compute binding (phase "${d.name}"). The entry is evicted; retry to get a fresh session. The named session itself is not over \u2014 it stays attachable by name.`))})),{gone:()=>s,onGone(d){return s?(d(s),()=>{}):(i.add(d),()=>i.delete(d))},dispose(){i.clear();for(let d of a.splice(0))d()}}}async function*Dt(n,e){let t=e.gone();if(t)throw t;let r=n[Symbol.asyncIterator](),o,s=new Promise((i,a)=>{o=e.onGone(c=>a(c))});s.catch(()=>{});try{for(;;){let i=await Promise.race([r.next(),s]);if(i.done)return;yield i.value}}finally{o?.(),r.return?.(void 0)}}function Qn(n,e,t,r){let o=n[t];if(typeof o!="function")throw new Error(`app "${e}" has no function "${t}" (set URUN_FUNCTION)`);let s=r?o(void 0,r):o();if(typeof s.end!="function")throw new Error(`app "${e}" function "${t}" returned a session without end()`);return s}function jo(n,e){let{baseUrl:t,orgId:r,fnName:o,auth:s,subject:i}=n,a=n.sessionStartOptions?.(e),c=f=>({session:f,responses:new ut(f),gone:Xn(f,e)});if(s.lane==="jwt"){let f=(0,Ve.App)(e,{baseUrl:t,orgId:r,jwt:s.jwt});return c(Qn(f,e,o,a))}let{apiKey:d,gatewayUrl:h}=s,m=(0,Ve.App)(e,{baseUrl:t,orgId:r,getAccessToken:async()=>(await(0,Ve.createClientToken)(d,{baseUrl:h,expiresIn:300,allowedFunctions:[`${e}/${o}`],...i===void 0?{}:{subject:i}})).token});return c(Qn(m,e,o,a))}function Go(n){let e=n.session.id;if(typeof e!="string"||e.length===0)throw new Error("pooled session exposes no stable session id \u2014 the session-identity seam requires @urun-sh/core Session (session.id)");return e}var Vo=new Set(["error","paused"]);function Ft(n){let{auth:e,catalog:t}=n,r=null,o=i=>{let a=jo(n,i);return n.onSessionOpen&&typeof a.session.id=="string"&&a.session.id!==""&&n.onSessionOpen(i,a.session.id),a.gone.onGone(()=>{r?.evict(i,a)}),a},s=new We({defaultApp:n.defaultApp,fnName:n.fnName,openSession:o,closeSession:async i=>{if(i.gone.dispose(),n.release==="detach"){if(typeof i.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');i.session.detach();return}await i.session.end()},listApps:e.lane==="api-key"?()=>un({apiUrl:n.apiUrl,apiKey:e.apiKey}):null,listCatalog:t?()=>qn(t):null,sessionKey:Go});return r=s,n.defaultApp!==null&&s.seed(n.defaultApp,o(n.defaultApp)),s}function Ht(n,e=hn){let t=new WeakMap,r=new WeakMap,o=new WeakMap;return{createResponse:Yn({sessionFor:s=>n.sessionFor(s),evict:(s,i)=>n.evict(s,i),createStream:async(s,i)=>Dt(await s.responses.responses.create(i),s.gone)}),listModels:()=>n.modelList(),openRouterModels:()=>n.openRouterModels(),openAudio:async s=>{let{entry:i}=await n.sessionFor(s),a=t.get(i);return a||(a=(async()=>fn(i.session,await e()))(),t.set(i,a),a.catch(()=>t.delete(i))),a},openVideo:async s=>{let{entry:i}=await n.sessionFor(s),a=r.get(i);return a||(a=mn(i.session),r.set(i,a)),a},openVideoOut:async s=>{let{entry:i}=await n.sessionFor(s),a=o.get(i);return a||(a=gn(i.session),o.set(i,a)),a},sessionHandle:async s=>(await n.handleFor(s)).handle,createResponseOn:async(s,i)=>{let{entry:a}=await n.sessionForHandle(s);return Dt(await a.responses.responses.create(i),a.gone)},onSessionEnd:async(s,i)=>{let{entry:a}=await n.sessionForHandle(s),c=a.session;if(typeof c.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let d=!1;return c.onPhase(h=>{if(d||!Vo.has(h.name))return;d=!0;let m=c.endsAt??null;i({timeLeftMs:m?Math.max(0,m.getTime()-Date.now()):null,reason:h.reason??`session ${h.name}`})})}}}var qo=512;function ft(n){return`urun-inference-proxy-${(0,pt.createHash)("sha256").update(n).digest("hex").slice(0,32)}`}var ct=class{constructor(e){this.opts=e}opts;tenants=new Map;build(e){if(this.opts.build)return this.opts.build(e);let t=Ft({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:vt,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:ft(e.apiKey),release:"detach"});return{router:t,clients:Ht(t)}}clientsFor(e){let t=this.tenants.get(e.apiKey);if(t)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,t),t.clients;for(;this.tenants.size>=qo;){let o=this.tenants.entries().next();if(o.done)break;let[s,i]=o.value;this.tenants.delete(s),i.router.closeAll().catch(()=>{})}let r=this.build(e);return this.tenants.set(e.apiKey,r),r.clients}get size(){return this.tenants.size}async closeAll(){let e=[...this.tenants.values()].map(r=>r.router);this.tenants.clear();let t=[];if(await Promise.all(e.map(async r=>{try{await r.closeAll()}catch(o){t.push(String(o instanceof Error?o.message:o))}})),t.length>0)throw new Error(`failed to close ${t.length} tenant backhaul(s): ${t.join("; ")}`)}},Zn=512,Wo=1024,lt=class extends Error{constructor(){super(`conversation capacity reached (${Zn} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function Jo(n,e,t,r){let o=Ft({baseUrl:n.baseUrl,orgId:e.orgId,defaultApp:null,fnName:vt,apiUrl:n.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:n.baseUrl},catalog:n.catalog,subject:t.subject,release:"detach",sessionStartOptions:s=>{let i=r.get(s);return i===void 0?void 0:{sessionId:i}},onSessionOpen:(s,i)=>{r.set(s,i)}});return{clients:Ht(o),release:async()=>{await o.closeAll()}}}var dt=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,t){let r={id:t,subject:`${ft(e.apiKey)}:live:${t}`},o=new Map,s=this.opts.build?this.opts.build(e,r):Jo(this.opts,e,r,o);return{id:t,backhaul:s,attachments:0,nativeRefs:o}}attachEntry(e,t){let r=this.byCaller.get(e.apiKey);r===void 0&&(r=new Map,this.byCaller.set(e.apiKey,r));let o=r.get(t);if(o!==void 0)return r.delete(t),r.set(t,o),o.attachments+=1,o;this.evictOverflow();let s=this.build(e,t);return s.attachments=1,r.set(t,s),s}get conversationCount(){let e=0;for(let t of this.byCaller.values())e+=t.size;return e}evictOverflow(){for(;this.conversationCount>=Zn;){let e=this.byCaller.keys().next();if(e.done)throw new lt;let t=e.value,r=this.byCaller.get(t),o=!1;for(let[s,i]of r)if(!(i.attachments>0)){r.delete(s),this.dropHandleIndexFor(t,s),i.backhaul.release().catch(()=>{}),o=!0;break}if(!o)throw r.size===0&&this.byCaller.delete(t),new lt;r.size===0&&this.byCaller.delete(t)}}dropHandleIndexFor(e,t){for(let[r,o]of this.byHandle)o.apiKey===e&&o.conversationId===t&&this.byHandle.delete(r)}connectionFor(e){let t=null,r=!1,o=()=>(t===null&&(t=this.attachEntry(e,(0,pt.randomUUID)())),t),s={createResponse:i=>o().backhaul.clients.createResponse(i),listModels:()=>o().backhaul.clients.listModels(),sessionHandle:i=>o().backhaul.clients.sessionHandle(i),createResponseOn:(i,a)=>o().backhaul.clients.createResponseOn(i,a),onSessionEnd:(i,a)=>o().backhaul.clients.onSessionEnd(i,a)};return s.openAudio=async i=>{let a=o().backhaul.clients.openAudio;if(a===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return a(i)},s.openVideo=async i=>{let a=o().backhaul.clients.openVideo;if(a===void 0)throw new Error("the conversation backhaul wires no openVideo seam \u2014 video-in is unavailable on this deployment");return a(i)},s.openVideoOut=async i=>{let a=o().backhaul.clients.openVideoOut;if(a===void 0)throw new Error("the conversation backhaul wires no openVideoOut seam \u2014 video-out is unavailable on this deployment");return a(i)},{clients:s,bindResumeHandle:i=>{let a=t;if(a===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=Wo;){let c=this.byHandle.keys().next();if(c.done)break;this.byHandle.delete(c.value)}this.byHandle.set(i,{apiKey:e.apiKey,conversationId:a.id})},clientsForResume:async i=>{let a=this.byHandle.get(i),c=a===void 0?void 0:this.byCaller.get(a.apiKey),d=a===void 0||c===void 0?void 0:c.get(a.conversationId);if(a===void 0||d===void 0||a.apiKey!==e.apiKey)throw new Y("the conversation behind this resume handle is gone (evicted at capacity, or the replica restarted) or the handle is not yours \u2014 reconnect without a handle to start a new conversation");return t=d,d.attachments+=1,d.backhaul.clients},release:async()=>{if(r)return;r=!0;let i=t;t=null,i!==null&&(i.attachments-=1,!(i.attachments>0)&&await i.backhaul.release().catch(()=>{}))}}}async detachAll(){let e=[];for(let r of this.byCaller.values())for(let o of r.values())e.push(o.backhaul.release());this.byCaller.clear(),this.byHandle.clear();let t=[];if(await Promise.all(e.map(async r=>{try{await r}catch(o){t.push(String(o instanceof Error?o.message:o))}})),t.length>0)throw new Error(`failed to detach ${t.length} conversation backhaul(s): ${t.join("; ")}`)}};var Ko=5e3,zo=3e3,Yo="/api/client-tokens";function Bt(n,e,t){n.writeHead(e,{"content-type":"application/json"}),n.end(JSON.stringify(t))}function er(n,e,t,r){Bt(n,e,{error:{message:t,type:r}})}async function Xo(n,e=fetch){let t=`${n.replace(/\/+$/,"")}${Yo}`;try{let r=await e(t,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(zo)});return r.status===404?{ready:!1,detail:`${t} answered 404 \u2014 this is not a uRun control plane`}:r.status>=500?{ready:!1,detail:`${t} answered HTTP ${r.status}`}:{ready:!0,detail:`${t} answered HTTP ${r.status}`}}catch(r){return{ready:!1,detail:`${t} unreachable: ${r instanceof Error?r.message:String(r)}`}}}function nr(n){let e=n.resolver??new st({gatewayUrl:n.baseUrl}),t=n.registry??new ct({baseUrl:n.baseUrl,apiUrl:n.apiUrl,catalog:n.catalog}),r=n.probeControlPlane??(()=>Xo(n.baseUrl)),o=async f=>e.resolve(Vn(f.headers.authorization)),s=f=>f instanceof ke?{status:f.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:f instanceof Le?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,i=Gn({clients:async f=>t.clientsFor(await o(f)),statusOf:s}),a=null,c=(0,tr.createServer)((f,g)=>{let S=new URL(f.url??"/","http://localhost");(async()=>{if(f.method==="GET"&&S.pathname==="/healthz"){Bt(g,200,{ok:!0});return}if(f.method==="GET"&&S.pathname==="/readyz"){let I=Date.now();if(!a||I-a.at>=Ko){let A=await r();a={at:I,...A}}Bt(g,a.ready?200:503,{ok:a.ready,control_plane:a.detail});return}if(S.pathname!=="/v1"&&!S.pathname.startsWith("/v1/")){er(g,404,`no route for ${f.method} ${S.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface, the Gemini Live WS upgrade path (/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent), plus /healthz and /readyz`,"invalid_request_error");return}i(f,g)})().catch(I=>{if(!g.headersSent){er(g,500,String(I instanceof Error?I.message:I),"proxy_error");return}g.end()})}),d=n.conversations??new dt({baseUrl:n.baseUrl,apiUrl:n.apiUrl,catalog:n.catalog}),h=[Rt(c,{authenticate:async f=>{let g=await e.resolve(f),S=d.connectionFor(g);return{clients:S.clients,scope:ft(g.apiKey),onBindResumeHandle:S.bindResumeHandle,clientsForResume:S.clientsForResume,release:S.release}}}),Ut(c,{clients:async f=>t.clientsFor(await o(f)),statusOf:s}).close];return c.on("upgrade",(f,g)=>{let S=new URL(f.url??"/","http://localhost");S.pathname===Xe||S.pathname===Ct||g.destroyed||(g.write(`HTTP/1.1 404 Not Found\r
|
|
47
|
+
Connection: close\r
|
|
48
|
+
\r
|
|
49
|
+
no WS route for ${S.pathname} \u2014 this endpoint serves only the Gemini Live and /v1/responses upgrade paths`),g.destroy())}),{server:c,closeAll:()=>t.closeAll(),closeWebSockets:async()=>{for(let f of h)await f();await d.detachAll()}}}async function rr(n,e,t){t&&await t();let r=new Promise(o=>n.close(()=>o()));n.closeAllConnections(),await r,await e()}async function Qo(){let n=ln(ge.default.env),{server:e,closeAll:t,closeWebSockets:r}=nr(n);await new Promise((s,i)=>{e.once("error",i),e.listen(n.port,_t,s)}),ge.default.stdout.write(`inference-proxy listening on http://${_t}:${n.port}/v1 (control plane ${n.baseUrl}, org API ${n.apiUrl}, catalog oracle ${n.catalog?"on":"off"})
|
|
50
|
+
`);let o=async s=>{ge.default.stdout.write(`inference-proxy: ${s} \u2014 draining
|
|
51
|
+
`),await rr(e,t,r)};await new Promise(s=>{ge.default.once("SIGINT",()=>{o("SIGINT").finally(s)}),ge.default.once("SIGTERM",()=>{o("SIGTERM").finally(s)})})}Qo().then(()=>ge.default.exit(0),n=>{ge.default.stderr.write(`inference-proxy: ${String(n instanceof Error?n.stack??n.message:n)}
|
|
52
|
+
`),ge.default.exit(1)});
|
|
39
53
|
/*! Bundled license information:
|
|
40
54
|
|
|
41
55
|
reflect-metadata/Reflect.js:
|