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