@urun-sh/openai 0.5.6 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{ResponsesClient-BE3-hx3m.d.ts → ResponsesClient-CSSrOYD8.d.ts} +5 -1
- package/dist/{ResponsesClient-DjZWjlFH.d.cts → ResponsesClient-_OZERUjH.d.cts} +9 -1
- package/dist/{chunk-7M6LY6DR.js → chunk-3JWYIKHM.js} +1 -1
- package/dist/{chunk-BSHT6RZZ.js → chunk-6M5JK4YC.js} +1 -1
- package/dist/chunk-76BZLCGT.js +1 -0
- package/dist/chunk-7V2JKPIG.js +1 -0
- package/dist/{chunk-5HKWNK3O.js → chunk-K23AZPI4.js} +1 -1
- package/dist/chunk-T2LNHDPS.js +7 -0
- package/dist/chunk-TMQJY56F.js +4 -0
- package/dist/chunk-WEBOBE7Z.js +58 -0
- package/dist/chunk-X3GLH2HQ.js +1 -0
- package/dist/gemini-live.cjs +2 -2
- package/dist/gemini-live.d.cts +75 -7
- package/dist/gemini-live.d.ts +30 -7
- package/dist/gemini-live.js +1 -1
- package/dist/hosted/bin.cjs +37 -35
- package/dist/hosted/bin.js +1 -1
- package/dist/hosted/index.cjs +33 -31
- package/dist/hosted/index.d.cts +70 -4
- package/dist/hosted/index.d.ts +21 -4
- package/dist/hosted/index.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1 -1
- package/dist/pi-extension/index.cjs +7 -7
- package/dist/pi-extension/index.d.cts +2 -2
- package/dist/pi-extension/index.d.ts +2 -2
- package/dist/pi-extension/index.js +1 -1
- package/dist/pi-extension/standalone.cjs +53 -53
- package/dist/proxy/cli.cjs +48 -46
- package/dist/proxy/cli.js +15 -14
- package/dist/proxy/index.cjs +25 -24
- package/dist/proxy/index.d.cts +142 -25
- package/dist/proxy/index.d.ts +22 -11
- package/dist/proxy/index.js +1 -1
- package/dist/{responses-turn-Dr37N3kH.d.ts → responses-turn-EwxfRJJg.d.ts} +187 -7
- package/dist/{responses-turn-tNUHq5b0.d.cts → responses-turn-vMefRdgJ.d.cts} +707 -23
- package/dist/{translator-Bh0Bp_Ie.d.cts → translator-CO8W_hmJ.d.cts} +1 -1
- package/dist/{translator-BCoRFaTs.d.ts → translator-Ddd65sXR.d.ts} +1 -1
- package/dist/{types-lsVTbNcH.d.cts → types-CHPtJx6d.d.cts} +13 -0
- package/dist/{types-lsVTbNcH.d.ts → types-CHPtJx6d.d.ts} +4 -0
- package/dist/{video-out-CCksIcj8.d.cts → video-out-BsuLqlID.d.cts} +1 -1
- package/dist/{video-out-IGQ4YQ-c.d.ts → video-out-Cawtm7SF.d.ts} +1 -1
- package/package.json +4 -2
- package/dist/chunk-2T2YYBVX.js +0 -1
- package/dist/chunk-3WAJD62J.js +0 -4
- package/dist/chunk-FP4RSAIE.js +0 -1
- package/dist/chunk-HR5H6S7L.js +0 -1
- package/dist/chunk-NY23USZF.js +0 -57
- package/dist/chunk-VLRMJRLS.js +0 -6
- /package/dist/{chunk-5CCJUNH7.js → chunk-YSFSRI3D.js} +0 -0
package/dist/hosted/bin.cjs
CHANGED
|
@@ -1,69 +1,71 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
"use strict";var ms=Object.create;var Or=Object.defineProperty;var gs=Object.getOwnPropertyDescriptor;var hs=Object.getOwnPropertyNames;var ys=Object.getPrototypeOf,_s=Object.prototype.hasOwnProperty;var ws=(t,e,n)=>()=>{if(n)throw n[0];try{return t&&(e=t(t=0)),e}catch(r){throw n=[r],r}};var vs=(t,e)=>()=>{try{return e||t((e={exports:{}}).exports,e),e.exports}catch(n){throw e=0,n}};var bs=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of hs(e))!_s.call(t,o)&&o!==n&&Or(t,o,{get:()=>e[o],enumerable:!(r=gs(e,o))||r.enumerable});return t};var ze=(t,e,n)=>(n=t!=null?ms(ys(t)):{},bs(e||!t||!t.__esModule?Or(n,"default",{value:t,enumerable:!0}):n,t));var _=ws(()=>{"use strict"});var Tr=vs(()=>{"use strict";_();var Ir;(function(t){(function(e){var n=typeof globalThis=="object"?globalThis:typeof global=="object"?global:typeof self=="object"?self:typeof this=="object"?this:a(),r=o(t);typeof n.Reflect<"u"&&(r=o(n.Reflect,r)),e(r,n),typeof n.Reflect>"u"&&(n.Reflect=t);function o(u,d){return function(l,c){Object.defineProperty(u,l,{configurable:!0,writable:!0,value:c}),d&&d(l,c)}}function i(){try{return Function("return this;")()}catch{}}function s(){try{return(0,eval)("(function() { return this; })()")}catch{}}function a(){return i()||s()}})(function(e,n){var r=Object.prototype.hasOwnProperty,o=typeof Symbol=="function",i=o&&typeof Symbol.toPrimitive<"u"?Symbol.toPrimitive:"@@toPrimitive",s=o&&typeof Symbol.iterator<"u"?Symbol.iterator:"@@iterator",a=typeof Object.create=="function",u={__proto__:[]}instanceof Array,d=!a&&!u,l={create:a?function(){return Sn(Object.create(null))}:u?function(){return Sn({__proto__:null})}:function(){return Sn({})},has:d?function(p,f){return r.call(p,f)}:function(p,f){return f in p},get:d?function(p,f){return r.call(p,f)?p[f]:void 0}:function(p,f){return p[f]}},c=Object.getPrototypeOf(Function),y=typeof Map=="function"&&typeof Map.prototype.entries=="function"?Map:ds(),g=typeof Set=="function"&&typeof Set.prototype.entries=="function"?Set:ls(),E=typeof WeakMap=="function"?WeakMap:cs(),w=o?Symbol.for("@reflect-metadata:registry"):void 0,h=ss(),b=as(h);function R(p,f,m,v){if(P(m)){if(!vr(p))throw new TypeError;if(!br(f))throw new TypeError;return ge(p,f)}else{if(!vr(p))throw new TypeError;if(!D(f))throw new TypeError;if(!D(v)&&!P(v)&&!he(v))throw new TypeError;return he(v)&&(v=void 0),m=Se(m),ce(p,f,m,v)}}e("decorate",R);function T(p,f){function m(v,C){if(!D(v))throw new TypeError;if(!P(C)&&!os(C))throw new TypeError;ue(p,f,v,C)}return m}e("metadata",T);function B(p,f,m,v){if(!D(m))throw new TypeError;return P(v)||(v=Se(v)),ue(p,f,m,v)}e("defineMetadata",B);function M(p,f,m){if(!D(f))throw new TypeError;return P(m)||(m=Se(m)),ne(p,f,m)}e("hasMetadata",M);function $(p,f,m){if(!D(f))throw new TypeError;return P(m)||(m=Se(m)),N(p,f,m)}e("hasOwnMetadata",$);function k(p,f,m){if(!D(f))throw new TypeError;return P(m)||(m=Se(m)),J(p,f,m)}e("getMetadata",k);function H(p,f,m){if(!D(f))throw new TypeError;return P(m)||(m=Se(m)),X(p,f,m)}e("getOwnMetadata",H);function W(p,f){if(!D(p))throw new TypeError;return P(f)||(f=Se(f)),xe(p,f)}e("getMetadataKeys",W);function z(p,f){if(!D(p))throw new TypeError;return P(f)||(f=Se(f)),Re(p,f)}e("getOwnMetadataKeys",z);function j(p,f,m){if(!D(f))throw new TypeError;if(P(m)||(m=Se(m)),!D(f))throw new TypeError;P(m)||(m=Se(m));var v=st(f,m,!1);return P(v)?!1:v.OrdinaryDeleteMetadata(p,f,m)}e("deleteMetadata",j);function ge(p,f){for(var m=p.length-1;m>=0;--m){var v=p[m],C=v(f);if(!P(C)&&!he(C)){if(!br(C))throw new TypeError;f=C}}return f}function ce(p,f,m,v){for(var C=p.length-1;C>=0;--C){var Z=p[C],ie=Z(f,m,v);if(!P(ie)&&!he(ie)){if(!D(ie))throw new TypeError;v=ie}}return v}function ne(p,f,m){var v=N(p,f,m);if(v)return!0;var C=Rn(f);return he(C)?!1:ne(p,C,m)}function N(p,f,m){var v=st(f,m,!1);return P(v)?!1:wr(v.OrdinaryHasOwnMetadata(p,f,m))}function J(p,f,m){var v=N(p,f,m);if(v)return X(p,f,m);var C=Rn(f);if(!he(C))return J(p,C,m)}function X(p,f,m){var v=st(f,m,!1);if(!P(v))return v.OrdinaryGetOwnMetadata(p,f,m)}function ue(p,f,m,v){var C=st(m,v,!0);C.OrdinaryDefineOwnMetadata(p,f,m,v)}function xe(p,f){var m=Re(p,f),v=Rn(p);if(v===null)return m;var C=xe(v,f);if(C.length<=0)return m;if(m.length<=0)return C;for(var Z=new g,ie=[],q=0,S=m;q<S.length;q++){var A=S[q],O=Z.has(A);O||(Z.add(A),ie.push(A))}for(var I=0,G=C;I<G.length;I++){var A=G[I],O=Z.has(A);O||(Z.add(A),ie.push(A))}return ie}function Re(p,f){var m=st(p,f,!1);return m?m.OrdinaryOwnMetadataKeys(p,f):[]}function ve(p){if(p===null)return 1;switch(typeof p){case"undefined":return 0;case"boolean":return 2;case"string":return 3;case"symbol":return 4;case"number":return 5;case"object":return p===null?1:6;default:return 6}}function P(p){return p===void 0}function he(p){return p===null}function Ue(p){return typeof p=="symbol"}function D(p){return typeof p=="object"?p!==null:typeof p=="function"}function de(p,f){switch(ve(p)){case 0:return p;case 1:return p;case 2:return p;case 3:return p;case 4:return p;case 5:return p}var m=f===3?"string":f===5?"number":"default",v=kr(p,i);if(v!==void 0){var C=v.call(p,m);if(D(C))throw new TypeError;return C}return Ot(p,m==="default"?"number":m)}function Ot(p,f){if(f==="string"){var m=p.toString;if(Ke(m)){var v=m.call(p);if(!D(v))return v}var C=p.valueOf;if(Ke(C)){var v=C.call(p);if(!D(v))return v}}else{var C=p.valueOf;if(Ke(C)){var v=C.call(p);if(!D(v))return v}var Z=p.toString;if(Ke(Z)){var v=Z.call(p);if(!D(v))return v}}throw new TypeError}function wr(p){return!!p}function rs(p){return""+p}function Se(p){var f=de(p,3);return Ue(f)?f:rs(f)}function vr(p){return Array.isArray?Array.isArray(p):p instanceof Object?p instanceof Array:Object.prototype.toString.call(p)==="[object Array]"}function Ke(p){return typeof p=="function"}function br(p){return typeof p=="function"}function os(p){switch(ve(p)){case 3:return!0;case 4:return!0;default:return!1}}function xn(p,f){return p===f||p!==p&&f!==f}function kr(p,f){var m=p[f];if(m!=null){if(!Ke(m))throw new TypeError;return m}}function xr(p){var f=kr(p,s);if(!Ke(f))throw new TypeError;var m=f.call(p);if(!D(m))throw new TypeError;return m}function Rr(p){return p.value}function Sr(p){var f=p.next();return f.done?!1:f}function Er(p){var f=p.return;f&&f.call(p)}function Rn(p){var f=Object.getPrototypeOf(p);if(typeof p!="function"||p===c||f!==c)return f;var m=p.prototype,v=m&&Object.getPrototypeOf(m);if(v==null||v===Object.prototype)return f;var C=v.constructor;return typeof C!="function"||C===p?f:C}function is(){var p;!P(w)&&typeof n.Reflect<"u"&&!(w in n.Reflect)&&typeof n.Reflect.defineMetadata=="function"&&(p=us(n.Reflect));var f,m,v,C=new E,Z={registerProvider:ie,getProvider:S,setProvider:O};return Z;function ie(I){if(!Object.isExtensible(Z))throw new Error("Cannot add provider to a frozen registry.");switch(!0){case p===I:break;case P(f):f=I;break;case f===I:break;case P(m):m=I;break;case m===I:break;default:v===void 0&&(v=new g),v.add(I);break}}function q(I,G){if(!P(f)){if(f.isProviderFor(I,G))return f;if(!P(m)){if(m.isProviderFor(I,G))return f;if(!P(v))for(var K=xr(v);;){var ee=Sr(K);if(!ee)return;var be=Rr(ee);if(be.isProviderFor(I,G))return Er(K),be}}}if(!P(p)&&p.isProviderFor(I,G))return p}function S(I,G){var K=C.get(I),ee;return P(K)||(ee=K.get(G)),P(ee)&&(ee=q(I,G),P(ee)||(P(K)&&(K=new y,C.set(I,K)),K.set(G,ee))),ee}function A(I){if(P(I))throw new TypeError;return f===I||m===I||!P(v)&&v.has(I)}function O(I,G,K){if(!A(K))throw new Error("Metadata provider not registered.");var ee=S(I,G);if(ee!==K){if(!P(ee))return!1;var be=C.get(I);P(be)&&(be=new y,C.set(I,be)),be.set(G,K)}return!0}}function ss(){var p;return!P(w)&&D(n.Reflect)&&Object.isExtensible(n.Reflect)&&(p=n.Reflect[w]),P(p)&&(p=is()),!P(w)&&D(n.Reflect)&&Object.isExtensible(n.Reflect)&&Object.defineProperty(n.Reflect,w,{enumerable:!1,configurable:!1,writable:!1,value:p}),p}function as(p){var f=new E,m={isProviderFor:function(A,O){var I=f.get(A);return P(I)?!1:I.has(O)},OrdinaryDefineOwnMetadata:ie,OrdinaryHasOwnMetadata:C,OrdinaryGetOwnMetadata:Z,OrdinaryOwnMetadataKeys:q,OrdinaryDeleteMetadata:S};return h.registerProvider(m),m;function v(A,O,I){var G=f.get(A),K=!1;if(P(G)){if(!I)return;G=new y,f.set(A,G),K=!0}var ee=G.get(O);if(P(ee)){if(!I)return;if(ee=new y,G.set(O,ee),!p.setProvider(A,O,m))throw G.delete(O),K&&f.delete(A),new Error("Wrong provider for target.")}return ee}function C(A,O,I){var G=v(O,I,!1);return P(G)?!1:wr(G.has(A))}function Z(A,O,I){var G=v(O,I,!1);if(!P(G))return G.get(A)}function ie(A,O,I,G){var K=v(I,G,!0);K.set(A,O)}function q(A,O){var I=[],G=v(A,O,!1);if(P(G))return I;for(var K=G.keys(),ee=xr(K),be=0;;){var Ar=Sr(ee);if(!Ar)return I.length=be,I;var ps=Rr(Ar);try{I[be]=ps}catch(fs){try{Er(ee)}finally{throw fs}}be++}}function S(A,O,I){var G=v(O,I,!1);if(P(G)||!G.delete(A))return!1;if(G.size===0){var K=f.get(O);P(K)||(K.delete(I),K.size===0&&f.delete(K))}return!0}}function us(p){var f=p.defineMetadata,m=p.hasOwnMetadata,v=p.getOwnMetadata,C=p.getOwnMetadataKeys,Z=p.deleteMetadata,ie=new E,q={isProviderFor:function(S,A){var O=ie.get(S);return!P(O)&&O.has(A)?!0:C(S,A).length?(P(O)&&(O=new g,ie.set(S,O)),O.add(A),!0):!1},OrdinaryDefineOwnMetadata:f,OrdinaryHasOwnMetadata:m,OrdinaryGetOwnMetadata:v,OrdinaryOwnMetadataKeys:C,OrdinaryDeleteMetadata:Z};return q}function st(p,f,m){var v=h.getProvider(p,f);if(!P(v))return v;if(m){if(h.setProvider(p,f,b))return b;throw new Error("Illegal state.")}}function ds(){var p={},f=[],m=(function(){function q(S,A,O){this._index=0,this._keys=S,this._values=A,this._selector=O}return q.prototype["@@iterator"]=function(){return this},q.prototype[s]=function(){return this},q.prototype.next=function(){var S=this._index;if(S>=0&&S<this._keys.length){var A=this._selector(this._keys[S],this._values[S]);return S+1>=this._keys.length?(this._index=-1,this._keys=f,this._values=f):this._index++,{value:A,done:!1}}return{value:void 0,done:!0}},q.prototype.throw=function(S){throw this._index>=0&&(this._index=-1,this._keys=f,this._values=f),S},q.prototype.return=function(S){return this._index>=0&&(this._index=-1,this._keys=f,this._values=f),{value:S,done:!0}},q})(),v=(function(){function q(){this._keys=[],this._values=[],this._cacheKey=p,this._cacheIndex=-2}return Object.defineProperty(q.prototype,"size",{get:function(){return this._keys.length},enumerable:!0,configurable:!0}),q.prototype.has=function(S){return this._find(S,!1)>=0},q.prototype.get=function(S){var A=this._find(S,!1);return A>=0?this._values[A]:void 0},q.prototype.set=function(S,A){var O=this._find(S,!0);return this._values[O]=A,this},q.prototype.delete=function(S){var A=this._find(S,!1);if(A>=0){for(var O=this._keys.length,I=A+1;I<O;I++)this._keys[I-1]=this._keys[I],this._values[I-1]=this._values[I];return this._keys.length--,this._values.length--,xn(S,this._cacheKey)&&(this._cacheKey=p,this._cacheIndex=-2),!0}return!1},q.prototype.clear=function(){this._keys.length=0,this._values.length=0,this._cacheKey=p,this._cacheIndex=-2},q.prototype.keys=function(){return new m(this._keys,this._values,C)},q.prototype.values=function(){return new m(this._keys,this._values,Z)},q.prototype.entries=function(){return new m(this._keys,this._values,ie)},q.prototype["@@iterator"]=function(){return this.entries()},q.prototype[s]=function(){return this.entries()},q.prototype._find=function(S,A){if(!xn(this._cacheKey,S)){this._cacheIndex=-1;for(var O=0;O<this._keys.length;O++)if(xn(this._keys[O],S)){this._cacheIndex=O;break}}return this._cacheIndex<0&&A&&(this._cacheIndex=this._keys.length,this._keys.push(S),this._values.push(void 0)),this._cacheIndex},q})();return v;function C(q,S){return q}function Z(q,S){return S}function ie(q,S){return[q,S]}}function ls(){var p=(function(){function f(){this._map=new y}return Object.defineProperty(f.prototype,"size",{get:function(){return this._map.size},enumerable:!0,configurable:!0}),f.prototype.has=function(m){return this._map.has(m)},f.prototype.add=function(m){return this._map.set(m,m),this},f.prototype.delete=function(m){return this._map.delete(m)},f.prototype.clear=function(){this._map.clear()},f.prototype.keys=function(){return this._map.keys()},f.prototype.values=function(){return this._map.keys()},f.prototype.entries=function(){return this._map.entries()},f.prototype["@@iterator"]=function(){return this.keys()},f.prototype[s]=function(){return this.keys()},f})();return p}function cs(){var p=16,f=l.create(),m=v();return(function(){function S(){this._key=v()}return S.prototype.has=function(A){var O=C(A,!1);return O!==void 0?l.has(O,this._key):!1},S.prototype.get=function(A){var O=C(A,!1);return O!==void 0?l.get(O,this._key):void 0},S.prototype.set=function(A,O){var I=C(A,!0);return I[this._key]=O,this},S.prototype.delete=function(A){var O=C(A,!1);return O!==void 0?delete O[this._key]:!1},S.prototype.clear=function(){this._key=v()},S})();function v(){var S;do S="@@WeakMap@@"+q();while(l.has(f,S));return f[S]=!0,S}function C(S,A){if(!r.call(S,m)){if(!A)return;Object.defineProperty(S,m,{value:l.create()})}return S[m]}function Z(S,A){for(var O=0;O<A;++O)S[O]=Math.random()*255|0;return S}function ie(S){if(typeof Uint8Array=="function"){var A=new Uint8Array(S);return typeof crypto<"u"?crypto.getRandomValues(A):typeof msCrypto<"u"?msCrypto.getRandomValues(A):Z(A,S),A}return Z(new Array(S),S)}function q(){var S=ie(p);S[6]=S[6]&79|64,S[8]=S[8]&191|128;for(var A="",O=0;O<p;++O){var I=S[O];(O===4||O===6||O===8)&&(A+="-"),I<16&&(A+="0"),A+=I.toString(16).toLowerCase()}return A}}function Sn(p){return p.__=void 0,delete p.__,p}})})(Ir||(Ir={}))});_();var Np=ze(Tr(),1),ke=ze(require("process"),1);_();var Bs=ze(require("os"),1),js=ze(require("path"),1),qs=require("url"),Hs=require("util");_();_();var pe=class extends Error{};function ks(t){if(t===null||typeof t!="object")return null;let e=t.shared;if(e==null)return null;if(typeof e!="object")throw new pe(`catalog "shared" block is ${typeof e}, expected an object`);let{shared_org_id:n,endpoints:r}=e;if(typeof n!="string"||n==="")throw new pe(`catalog "shared" block has no shared_org_id: ${JSON.stringify(e)}`);if(!Array.isArray(r))throw new pe(`catalog "shared" block endpoints is not an array: ${JSON.stringify(e)}`);let o=r.map((i,s)=>{if(i===null||typeof i!="object")throw new pe(`catalog shared endpoints[${s}] is not an object: ${JSON.stringify(i)}`);let a=i;for(let u of["model_id","variant","app_slug","function"])if(typeof a[u]!="string"||a[u].length===0)throw new pe(`catalog shared endpoints[${s}] (${JSON.stringify(a)}) has no valid "${u}"`);return a});return o.length===0?null:{shared_org_id:n,endpoints:o}}function It(t){return[...t.endpoints].sort((e,n)=>e.model_id.localeCompare(n.model_id)||e.variant.localeCompare(n.variant))}function Pr(t,e,n){let r=(e??"").trim();if(r==="")return null;let o=r,i=n,s=r.lastIndexOf(":");s>0&&(o=r.slice(0,s),i=r.slice(s+1));let a=t.endpoints.filter(d=>d.model_id===o);if(a.length===0)return null;if(i!==void 0){let d=a.find(l=>l.variant===i);return d?{shared_org_id:t.shared_org_id,endpoint:d}:null}if(a.length===1)return{shared_org_id:t.shared_org_id,endpoint:a[0]};let u=a.filter(d=>d.compat_default===!0);if(u.length!==1)throw new pe(`shared model "${o}" has ${u.length} compat_default rows (exactly one required) among variants ${a.map(d=>d.variant).join(", ")}`);return{shared_org_id:t.shared_org_id,endpoint:u[0]}}async function Cr(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/catalog`,r=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new pe(`catalog read failed: GET ${n} \u2192 ${r.status}`);return ks(await r.json())}_();function Ae(t){return t.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}_();var ye=class extends Error{name="ModelPriceError"},En="per_million_input_tokens",An="per_million_output_tokens",Mr="model_id,variant,unit,price_usd,effective_at,expires_at",$r={per_request:!0,per_minute:!0,per_output_unit:!0},xs=5e3;function Rs(t,e){let n=typeof t=="number"?t:typeof t=="string"&&t.trim()!==""?Number(t):NaN;if(!Number.isFinite(n)||n<0)throw new ye(`${e}: price_usd is not a non-negative number (${JSON.stringify(t)})`);return n}function Ur(t,e,n){let r=typeof t=="string"?Date.parse(t):NaN;if(!Number.isFinite(r))throw new ye(`${e}: ${n} is not a parseable timestamp (${JSON.stringify(t)})`);return r}function Ss(t,e,n){return t===null?null:Ur(t,e,n)}function Es(t,e){if(t===En)return"input";if(t===An)return"output";if(typeof t=="string"&&Object.hasOwn($r,t))return null;throw new ye(`${e}: unit ${JSON.stringify(t)} is neither a per-token rate (${En} | ${An}) nor a known GPU-time unit (${Object.keys($r).join(" | ")}). Skipping it would publish no prices forever with no signal \u2014 if ENG-317 named the per-token units differently, teach this module the new names instead`)}function As(t,e){let n=t.reduce((o,i)=>i.effective_at>o.effective_at?i:o,t[0]),r=t.find(o=>o.effective_at===n.effective_at&&o.usd!==n.usd);return r?{reason:`two ${e} rates are effective at the same instant (${n.usd} and ${r.usd} USD/Mtok), so the winner would be read order \u2014 this model is served UNPRICED until one of them is expired; expire one of them`}:{rate:n}}function Os(t){if(!Array.isArray(t))throw new ye(`model_prices read is not an array: ${JSON.stringify(t)}`);let e=[];for(let n=0;n<t.length;n++){let r=t[n],o=`model_prices[${n}]`;if(r===null||typeof r!="object"||Array.isArray(r))throw new ye(`${o} is not an object: ${JSON.stringify(r)}`);let i=r;for(let d of Mr.split(","))if(!Object.hasOwn(i,d))throw new ye(`${o} has no "${d}" column: ${JSON.stringify(i)}`);let s=Es(i.unit,o);if(s===null)continue;let a=i.model_id;if(typeof a!="string"||a==="")throw new ye(`${o} has no model_id (${JSON.stringify(i)})`);let u=i.variant;if(!(u===null||typeof u=="string"&&u!==""))throw new ye(`${o} has a non-string variant (${JSON.stringify(u)})`);e.push({model_id:a,variant:u,side:s,usd:Rs(i.price_usd,o),effective_at:Ur(i.effective_at,o,"effective_at"),expires_at:Ss(i.expires_at,o,"expires_at")})}return e}function Lr(t,e=Date.now()){let n=new Map,r=new Map;for(let a of t){if(a.effective_at>e||a.expires_at!==null&&a.expires_at<=e)continue;let u=n.get(a.model_id);u||(u=new Map,n.set(a.model_id,u),r.set(a.model_id,new Map));let d=a.variant??"",l=u.get(d);l||(l={input:[],output:[]},u.set(d,l),r.get(a.model_id).set(d,a.variant)),l[a.side].push({effective_at:a.effective_at,usd:a.usd})}let o=[],i=[];for(let[a,u]of n){let d=u.get(""),l=(c,y)=>{let g=c&&c.length>0?c:d?.[y];if(!g||g.length===0)return null;let E=As(g,y);return"reason"in E?E:E.rate.usd===0?{reason:`the ${y} rate in force is zero, which publishes as "free" and would top :cheapest routing \u2014 this model is served UNPRICED; delete the row if the model is not token-priced, or set a real rate`}:E.rate};for(let[c,y]of r.get(a)){let g=u.get(c),E=l(g?.input,"input"),w=l(g?.output,"output");if(E!==null&&"reason"in E){i.push({model_id:a,variant:y,reason:E.reason});continue}if(w!==null&&"reason"in w){i.push({model_id:a,variant:y,reason:w.reason});continue}E===null||w===null||o.push({model_id:a,variant:y,pricing:{input:E.usd,output:w.usd}})}}let s=(a,u)=>a.model_id.localeCompare(u.model_id)||(a.variant===null?1:0)-(u.variant===null?1:0)||(a.variant??"").localeCompare(u.variant??"");return{prices:o.sort(s),refusals:i.sort(s)}}function at(t,e,n){if(n.refusals.some(i=>i.model_id===t&&i.variant===e))return null;let r=n.prices.find(i=>i.model_id===t&&i.variant===e);if(r)return r.pricing;if(n.refusals.some(i=>i.model_id===t&&i.variant===null))return null;let o=n.prices.find(i=>i.model_id===t&&i.variant===null);return o?o.pricing:null}var Tt={prices:[],refusals:[]};async function Nr(t){let e=t.fetchImpl??fetch,n=`${En},${An}`,r=await e(`${t.catalogUrl}/model_prices?select=${Mr}&unit=in.(${n})`,{headers:{apikey:t.anonKey,Authorization:`Bearer ${t.anonKey}`},signal:AbortSignal.timeout(xs)});if(!r.ok)throw new Error(`model_prices fetch failed: ${r.status}`);return Os(await r.json())}_();var Is={chat:!0,code:!0,agent:!0,vl:!0},Dr={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function Ts(t){let e=t.toLowerCase();return Object.hasOwn(Dr,e)?Dr[e]:null}var Ps=["context_length","max_model_len"];function Cs(t){let e=Ps.map(n=>t.engine_args?.[n]).filter(n=>typeof n=="number"&&Number.isFinite(n)&&n>0);return e.length===0?null:e.every(n=>n===e[0])?e[0]:null}function Pt(t){let e=t.map(Cs),n=e[0];return n==null?null:e.every(r=>r===n)?n:null}function $s(t,e){let n=e.filter(o=>Ae(`${o.model_id}-${o.variant}`)===t);if(n.length>0)return On(n);let r=e.filter(o=>Ae(o.model_id)===t);return r.length>0?On(r):null}function On(t){let e=t[0];return t.every(r=>r.model_id===e.model_id&&r.variant===e.variant)?{row:t.length===1?e:Ms(t),matches:t}:null}function Ms(t){let e=t[0],n=r=>t.every(o=>(o[r]??void 0)===(e[r]??void 0))?e[r]:void 0;return{model_id:e.model_id,variant:e.variant,gpu_spec:n("gpu_spec"),engine:n("engine"),task:n("task"),description:n("description"),size_gb:n("size_gb"),hf_repo:n("hf_repo"),updated_at:n("updated_at")}}function jr(t){let e=t.updated_at?Date.parse(t.updated_at):NaN;return Number.isFinite(e)?Math.floor(e/1e3):0}function In(t,e,n){let r=$s(t,e);if(!r)return{id:t,object:"model",created:0,owned_by:"urun"};let{row:o,matches:i}=r,s=Pt(i),a=at(o.model_id,o.variant,n);return{id:t,object:"model",created:jr(o),owned_by:"urun",name:`${o.model_id}:${o.variant}`,...o.description?{description:o.description}:{},...o.task?{task:o.task}:{},...o.engine?{engine:o.engine}:{},...o.gpu_spec?{gpu_spec:o.gpu_spec}:{},...s!==null?{context_length:s}:{},...a!==null?{pricing:a}:{}}}function Fr(t){let e=/^(\d+)(?:\.(\d+))?(?:e([+-]?\d+))?$/.exec(String(t));if(!e)throw new Error(`unrenderable per-token price: ${String(t)}`);let[,n="",r="",o=""]=e,i=n+r,s=n.length-6+(o===""?0:Number(o));return(s<=0?`0.${"0".repeat(-s)}${i}`:`${i.slice(0,s)}.${i.slice(s).padEnd(1,"0")}`).replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,"")}function Br(t){return{input:[{type:"prompt",unit:"token",cost_usd:Fr(t.input)}],output:[{type:"completion",unit:"token",cost_usd:Fr(t.output)}]}}function Us(t,e){if(t.length===0)return null;let n=On(t);if(!n)return null;let{row:r,matches:o}=n;if(!r.task||Is[r.task]!==!0)return null;let i=Pt(o),s=at(r.model_id,r.variant,e),a={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};return i!==null&&(a.max_tokens={type:"integer",min:1,max:i,unit:"token"}),{schema_version:"2.4",id:`${r.model_id}:${r.variant}`,name:`${r.model_id}:${r.variant}`,created:jr(r),quantization:Ts(r.variant),description:r.description??"",hugging_face_id:r.hf_repo?r.hf_repo.split("@")[0]??r.hf_repo:"",input_modalities:[{type:"text",...i!==null?{supported_inputs:{max_context_length:{value:i,unit:"token"}}}:{},...s?{pricing:Br(s).input}:{}},...r.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:a,...s?{pricing:Br(s).output}:{}}]}}function qr(t,e,n){let r=[],o=new Set;for(let i of t){let s=`${i.model_id}:${i.variant}`;if(o.has(s))continue;o.add(s);let a=Us(e.filter(u=>u.model_id===i.model_id&&u.variant===i.variant),n);a&&r.push(a)}return{data:r}}var re=class extends Error{},le=class extends Error{},Pn="urs1.";function Ls(t,e){return Pn+Buffer.from(JSON.stringify([t,e]),"utf8").toString("base64url")}function Ns(t){if(!t.startsWith(Pn))return null;try{let e=JSON.parse(Buffer.from(t.slice(Pn.length),"base64url").toString("utf8"));if(Array.isArray(e)&&typeof e[0]=="string"&&typeof e[1]=="string")return{app:e[0],key:e[1]}}catch{}return null}async function zr(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/apps`,r=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new Error(`org apps listing failed: GET ${n} \u2192 ${r.status}`);let o=await r.json();if(!Array.isArray(o.apps))throw new Error(`org apps listing returned no "apps" array (GET ${n})`);if(o.truncated===!0)throw new Error(`org apps listing was truncated (GET ${n} returned ${o.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return o.apps}function Hr(t){if(t.length===0)return[];if(!t.every(n=>(n.task??void 0)==="image"))return[];let e=Fs(t);return e===null?[]:[e]}function Gr(t,e,n,r,o){if(!o)throw new pe(`shared model "${t}" cannot be gated: no catalog oracle is configured, and a shared endpoint's modality must come from the model's OWN model_catalog row \u2014 never a default`);if(e===void 0||n===void 0)throw new pe(`shared model "${t}" resolved without a catalog id + variant to join on`);let i=r.filter(s=>s.model_id===e&&s.variant===n);if(i.length===0)throw new pe(`shared model "${t}" names endpoint ${e}:${n} but the catalog has no model_catalog row for it \u2014 the shared block cannot vouch a modality from nothing`);return i}function Vr(t,e){if(!e.every(o=>(o.task??void 0)==="image"))return[];let n=e.map(o=>o.engine_args?.expects_image),r=n[0];if(typeof r!="boolean"||!n.every(o=>o===r))throw new pe(`shared image model "${t}" does not vouch an explicit image mode: its ${e.length} model_catalog row(s) carry engine_args.expects_image ${JSON.stringify(n)} \u2014 the shared lane requires a boolean (false = pure text-to-image generation, true = edit) and refuses to silently default to edit`);return[r===!1?"generation":"edit"]}var Yr="shared:";function ut(t){return t.sharedApp?Yr+t.appSlug:t.appSlug}function Wr(t){return typeof t.modelId=="string"&&typeof t.variant=="string"?{kind:"catalog",model_id:t.modelId,variant:t.variant}:{kind:"not-a-catalog-model",model:t.appSlug}}function Tn(t,e){let n=e.filter(i=>Ae(`${i.model_id}-${i.variant}`)===t);if(n.length>0)return n;let r=e.filter(i=>Ae(i.model_id)===t);return new Set(r.map(i=>i.variant)).size===1?r:[]}function Ds(t){let e=t.engine_args?.expects_image;return e==null?"edit":e===!1?"generation":e===!0?"edit":null}function Fs(t){let e=t.map(Ds),n=e[0];return n!=null&&e.every(r=>r===n)?n:null}var Jr={stt:"stt",tts:"tts"};function Kr(t){if(t.length===0)return null;let e=t[0].task??void 0;return e===void 0||Jr[e]===void 0?null:t.every(n=>(n.task??void 0)===e)?Jr[e]:null}var Ct=6e4,$t=class{constructor(e){this.opts=e}opts;pool=new Map;appsCache=null;rowsCache=null;seed(e,n){this.pool.set(e,Promise.resolve(n))}async deployedApps(){if(!this.opts.listApps)return[];let e=this.opts.appsTtlMs??Ct;if(this.appsCache&&Date.now()-this.appsCache.at<e)return this.appsCache.apps;let n=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:n},n}async sharedBlock(){if(!this.opts.shared)return null;let e=this.opts.appsTtlMs??Ct;if(this.sharedCache&&Date.now()-this.sharedCache.at<e)return this.sharedCache.block;let n=await this.opts.shared();return this.sharedCache={at:Date.now(),block:n},n}sharedCache=null;async catalogRows(){if(!this.opts.listCatalog)return[];let e=this.opts.appsTtlMs??Ct;if(this.rowsCache&&Date.now()-this.rowsCache.at<e)return this.rowsCache.rows;let n=await this.opts.listCatalog();return this.rowsCache={at:Date.now(),rows:n},n}pricesCache=null;async priceRows(){if(!this.opts.listPrices)return[];let e=this.opts.appsTtlMs??Ct;if(this.pricesCache&&Date.now()-this.pricesCache.at<e)return this.pricesCache.rows;let n=await this.opts.listPrices();return this.pricesCache={at:Date.now(),rows:n},n}async pricesForListing(){let e;try{e=await this.priceRows()}catch(r){if(r instanceof ye)throw r;return this.warn(`urun /v1/models: the per-token price surface could not be read (${String(r instanceof Error?r.message:r)}) \u2014 every model is listed UNPRICED for now; Hugging Face will show no price until the read succeeds
|
|
3
|
-
`),
|
|
4
|
-
`);return n}warn(e){(this.opts.warn??(r=>console.warn(r.trimEnd())))(e)}servable(e){return e.filter(n=>n.
|
|
5
|
-
`);let
|
|
6
|
-
`)}catch{}};o.on("error",
|
|
7
|
-
`}function
|
|
8
|
-
`)}function
|
|
2
|
+
"use strict";var da=Object.create;var io=Object.defineProperty;var la=Object.getOwnPropertyDescriptor;var ca=Object.getOwnPropertyNames;var pa=Object.getPrototypeOf,fa=Object.prototype.hasOwnProperty;var ma=(t,e,n)=>()=>{if(n)throw n[0];try{return t&&(e=t(t=0)),e}catch(r){throw n=[r],r}};var ga=(t,e)=>()=>{try{return e||t((e={exports:{}}).exports,e),e.exports}catch(n){throw e=0,n}};var ha=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of ca(e))!fa.call(t,o)&&o!==n&&io(t,o,{get:()=>e[o],enumerable:!(r=la(e,o))||r.enumerable});return t};var ze=(t,e,n)=>(n=t!=null?da(pa(t)):{},ha(e||!t||!t.__esModule?io(n,"default",{value:t,enumerable:!0}):n,t));var k=ma(()=>{"use strict"});var uo=ga(()=>{"use strict";k();var ao;(function(t){(function(e){var n=typeof globalThis=="object"?globalThis:typeof global=="object"?global:typeof self=="object"?self:typeof this=="object"?this:a(),r=o(t);typeof n.Reflect<"u"&&(r=o(n.Reflect,r)),e(r,n),typeof n.Reflect>"u"&&(n.Reflect=t);function o(u,d){return function(l,p){Object.defineProperty(u,l,{configurable:!0,writable:!0,value:p}),d&&d(l,p)}}function s(){try{return Function("return this;")()}catch{}}function i(){try{return(0,eval)("(function() { return this; })()")}catch{}}function a(){return s()||i()}})(function(e,n){var r=Object.prototype.hasOwnProperty,o=typeof Symbol=="function",s=o&&typeof Symbol.toPrimitive<"u"?Symbol.toPrimitive:"@@toPrimitive",i=o&&typeof Symbol.iterator<"u"?Symbol.iterator:"@@iterator",a=typeof Object.create=="function",u={__proto__:[]}instanceof Array,d=!a&&!u,l={create:a?function(){return Zn(Object.create(null))}:u?function(){return Zn({__proto__:null})}:function(){return Zn({})},has:d?function(f,g){return r.call(f,g)}:function(f,g){return g in f},get:d?function(f,g){return r.call(f,g)?f[g]:void 0}:function(f,g){return f[g]}},p=Object.getPrototypeOf(Function),y=typeof Map=="function"&&typeof Map.prototype.entries=="function"?Map:oa(),c=typeof Set=="function"&&typeof Set.prototype.entries=="function"?Set:sa(),m=typeof WeakMap=="function"?WeakMap:ia(),w=o?Symbol.for("@reflect-metadata:registry"):void 0,R=ta(),h=na(R);function v(f,g,_,O){if(I(_)){if(!vt(f))throw new TypeError;if(!Zr(g))throw new TypeError;return de(f,g)}else{if(!vt(f))throw new TypeError;if(!F(g))throw new TypeError;if(!F(O)&&!I(O)&&!Y(O))throw new TypeError;return Y(O)&&(O=void 0),_=_e(_),fe(f,g,_,O)}}e("decorate",v);function b(f,g){function _(O,U){if(!F(O))throw new TypeError;if(!I(U)&&!Zi(U))throw new TypeError;Se(f,g,O,U)}return _}e("metadata",b);function $(f,g,_,O){if(!F(_))throw new TypeError;return I(O)||(O=_e(O)),Se(f,g,_,O)}e("defineMetadata",$);function S(f,g,_){if(!F(g))throw new TypeError;return I(_)||(_=_e(_)),se(f,g,_)}e("hasMetadata",S);function E(f,g,_){if(!F(g))throw new TypeError;return I(_)||(_=_e(_)),ie(f,g,_)}e("hasOwnMetadata",E);function x(f,g,_){if(!F(g))throw new TypeError;return I(_)||(_=_e(_)),ke(f,g,_)}e("getMetadata",x);function L(f,g,_){if(!F(g))throw new TypeError;return I(_)||(_=_e(_)),Z(f,g,_)}e("getOwnMetadata",L);function B(f,g){if(!F(f))throw new TypeError;return I(g)||(g=_e(g)),Ae(f,g)}e("getMetadataKeys",B);function q(f,g){if(!F(f))throw new TypeError;return I(g)||(g=_e(g)),Ke(f,g)}e("getOwnMetadataKeys",q);function V(f,g,_){if(!F(g))throw new TypeError;if(I(_)||(_=_e(_)),!F(g))throw new TypeError;I(_)||(_=_e(_));var O=bt(g,_,!1);return I(O)?!1:O.OrdinaryDeleteMetadata(f,g,_)}e("deleteMetadata",V);function de(f,g){for(var _=f.length-1;_>=0;--_){var O=f[_],U=O(g);if(!I(U)&&!Y(U)){if(!Zr(U))throw new TypeError;g=U}}return g}function fe(f,g,_,O){for(var U=f.length-1;U>=0;--U){var ne=f[U],le=ne(g,_,O);if(!I(le)&&!Y(le)){if(!F(le))throw new TypeError;O=le}}return O}function se(f,g,_){var O=ie(f,g,_);if(O)return!0;var U=Qn(g);return Y(U)?!1:se(f,U,_)}function ie(f,g,_){var O=bt(g,_,!1);return I(O)?!1:De(O.OrdinaryHasOwnMetadata(f,g,_))}function ke(f,g,_){var O=ie(f,g,_);if(O)return Z(f,g,_);var U=Qn(g);if(!Y(U))return ke(f,U,_)}function Z(f,g,_){var O=bt(g,_,!1);if(!I(O))return O.OrdinaryGetOwnMetadata(f,g,_)}function Se(f,g,_,O){var U=bt(_,O,!0);U.OrdinaryDefineOwnMetadata(f,g,_,O)}function Ae(f,g){var _=Ke(f,g),O=Qn(f);if(O===null)return _;var U=Ae(O,g);if(U.length<=0)return _;if(_.length<=0)return U;for(var ne=new c,le=[],W=0,T=_;W<T.length;W++){var P=T[W],C=ne.has(P);C||(ne.add(P),le.push(P))}for(var M=0,J=U;M<J.length;M++){var P=J[M],C=ne.has(P);C||(ne.add(P),le.push(P))}return le}function Ke(f,g){var _=bt(f,g,!1);return _?_.OrdinaryOwnMetadataKeys(f,g):[]}function H(f){if(f===null)return 1;switch(typeof f){case"undefined":return 0;case"boolean":return 2;case"string":return 3;case"symbol":return 4;case"number":return 5;case"object":return f===null?1:6;default:return 6}}function I(f){return f===void 0}function Y(f){return f===null}function Ie(f){return typeof f=="symbol"}function F(f){return typeof f=="object"?f!==null:typeof f=="function"}function me(f,g){switch(H(f)){case 0:return f;case 1:return f;case 2:return f;case 3:return f;case 4:return f;case 5:return f}var _=g===3?"string":g===5?"number":"default",O=eo(f,s);if(O!==void 0){var U=O.call(f,_);if(F(U))throw new TypeError;return U}return Ce(f,_==="default"?"number":_)}function Ce(f,g){if(g==="string"){var _=f.toString;if(Fe(_)){var O=_.call(f);if(!F(O))return O}var U=f.valueOf;if(Fe(U)){var O=U.call(f);if(!F(O))return O}}else{var U=f.valueOf;if(Fe(U)){var O=U.call(f);if(!F(O))return O}var ne=f.toString;if(Fe(ne)){var O=ne.call(f);if(!F(O))return O}}throw new TypeError}function De(f){return!!f}function at(f){return""+f}function _e(f){var g=me(f,3);return Ie(g)?g:at(g)}function vt(f){return Array.isArray?Array.isArray(f):f instanceof Object?f instanceof Array:Object.prototype.toString.call(f)==="[object Array]"}function Fe(f){return typeof f=="function"}function Zr(f){return typeof f=="function"}function Zi(f){switch(H(f)){case 3:return!0;case 4:return!0;default:return!1}}function Xn(f,g){return f===g||f!==f&&g!==g}function eo(f,g){var _=f[g];if(_!=null){if(!Fe(_))throw new TypeError;return _}}function to(f){var g=eo(f,i);if(!Fe(g))throw new TypeError;var _=g.call(f);if(!F(_))throw new TypeError;return _}function no(f){return f.value}function ro(f){var g=f.next();return g.done?!1:g}function oo(f){var g=f.return;g&&g.call(f)}function Qn(f){var g=Object.getPrototypeOf(f);if(typeof f!="function"||f===p||g!==p)return g;var _=f.prototype,O=_&&Object.getPrototypeOf(_);if(O==null||O===Object.prototype)return g;var U=O.constructor;return typeof U!="function"||U===f?g:U}function ea(){var f;!I(w)&&typeof n.Reflect<"u"&&!(w in n.Reflect)&&typeof n.Reflect.defineMetadata=="function"&&(f=ra(n.Reflect));var g,_,O,U=new m,ne={registerProvider:le,getProvider:T,setProvider:C};return ne;function le(M){if(!Object.isExtensible(ne))throw new Error("Cannot add provider to a frozen registry.");switch(!0){case f===M:break;case I(g):g=M;break;case g===M:break;case I(_):_=M;break;case _===M:break;default:O===void 0&&(O=new c),O.add(M);break}}function W(M,J){if(!I(g)){if(g.isProviderFor(M,J))return g;if(!I(_)){if(_.isProviderFor(M,J))return g;if(!I(O))for(var X=to(O);;){var re=ro(X);if(!re)return;var Te=no(re);if(Te.isProviderFor(M,J))return oo(X),Te}}}if(!I(f)&&f.isProviderFor(M,J))return f}function T(M,J){var X=U.get(M),re;return I(X)||(re=X.get(J)),I(re)&&(re=W(M,J),I(re)||(I(X)&&(X=new y,U.set(M,X)),X.set(J,re))),re}function P(M){if(I(M))throw new TypeError;return g===M||_===M||!I(O)&&O.has(M)}function C(M,J,X){if(!P(X))throw new Error("Metadata provider not registered.");var re=T(M,J);if(re!==X){if(!I(re))return!1;var Te=U.get(M);I(Te)&&(Te=new y,U.set(M,Te)),Te.set(J,X)}return!0}}function ta(){var f;return!I(w)&&F(n.Reflect)&&Object.isExtensible(n.Reflect)&&(f=n.Reflect[w]),I(f)&&(f=ea()),!I(w)&&F(n.Reflect)&&Object.isExtensible(n.Reflect)&&Object.defineProperty(n.Reflect,w,{enumerable:!1,configurable:!1,writable:!1,value:f}),f}function na(f){var g=new m,_={isProviderFor:function(P,C){var M=g.get(P);return I(M)?!1:M.has(C)},OrdinaryDefineOwnMetadata:le,OrdinaryHasOwnMetadata:U,OrdinaryGetOwnMetadata:ne,OrdinaryOwnMetadataKeys:W,OrdinaryDeleteMetadata:T};return R.registerProvider(_),_;function O(P,C,M){var J=g.get(P),X=!1;if(I(J)){if(!M)return;J=new y,g.set(P,J),X=!0}var re=J.get(C);if(I(re)){if(!M)return;if(re=new y,J.set(C,re),!f.setProvider(P,C,_))throw J.delete(C),X&&g.delete(P),new Error("Wrong provider for target.")}return re}function U(P,C,M){var J=O(C,M,!1);return I(J)?!1:De(J.has(P))}function ne(P,C,M){var J=O(C,M,!1);if(!I(J))return J.get(P)}function le(P,C,M,J){var X=O(M,J,!0);X.set(P,C)}function W(P,C){var M=[],J=O(P,C,!1);if(I(J))return M;for(var X=J.keys(),re=to(X),Te=0;;){var so=ro(re);if(!so)return M.length=Te,M;var aa=no(so);try{M[Te]=aa}catch(ua){try{oo(re)}finally{throw ua}}Te++}}function T(P,C,M){var J=O(C,M,!1);if(I(J)||!J.delete(P))return!1;if(J.size===0){var X=g.get(C);I(X)||(X.delete(M),X.size===0&&g.delete(X))}return!0}}function ra(f){var g=f.defineMetadata,_=f.hasOwnMetadata,O=f.getOwnMetadata,U=f.getOwnMetadataKeys,ne=f.deleteMetadata,le=new m,W={isProviderFor:function(T,P){var C=le.get(T);return!I(C)&&C.has(P)?!0:U(T,P).length?(I(C)&&(C=new c,le.set(T,C)),C.add(P),!0):!1},OrdinaryDefineOwnMetadata:g,OrdinaryHasOwnMetadata:_,OrdinaryGetOwnMetadata:O,OrdinaryOwnMetadataKeys:U,OrdinaryDeleteMetadata:ne};return W}function bt(f,g,_){var O=R.getProvider(f,g);if(!I(O))return O;if(_){if(R.setProvider(f,g,h))return h;throw new Error("Illegal state.")}}function oa(){var f={},g=[],_=(function(){function W(T,P,C){this._index=0,this._keys=T,this._values=P,this._selector=C}return W.prototype["@@iterator"]=function(){return this},W.prototype[i]=function(){return this},W.prototype.next=function(){var T=this._index;if(T>=0&&T<this._keys.length){var P=this._selector(this._keys[T],this._values[T]);return T+1>=this._keys.length?(this._index=-1,this._keys=g,this._values=g):this._index++,{value:P,done:!1}}return{value:void 0,done:!0}},W.prototype.throw=function(T){throw this._index>=0&&(this._index=-1,this._keys=g,this._values=g),T},W.prototype.return=function(T){return this._index>=0&&(this._index=-1,this._keys=g,this._values=g),{value:T,done:!0}},W})(),O=(function(){function W(){this._keys=[],this._values=[],this._cacheKey=f,this._cacheIndex=-2}return Object.defineProperty(W.prototype,"size",{get:function(){return this._keys.length},enumerable:!0,configurable:!0}),W.prototype.has=function(T){return this._find(T,!1)>=0},W.prototype.get=function(T){var P=this._find(T,!1);return P>=0?this._values[P]:void 0},W.prototype.set=function(T,P){var C=this._find(T,!0);return this._values[C]=P,this},W.prototype.delete=function(T){var P=this._find(T,!1);if(P>=0){for(var C=this._keys.length,M=P+1;M<C;M++)this._keys[M-1]=this._keys[M],this._values[M-1]=this._values[M];return this._keys.length--,this._values.length--,Xn(T,this._cacheKey)&&(this._cacheKey=f,this._cacheIndex=-2),!0}return!1},W.prototype.clear=function(){this._keys.length=0,this._values.length=0,this._cacheKey=f,this._cacheIndex=-2},W.prototype.keys=function(){return new _(this._keys,this._values,U)},W.prototype.values=function(){return new _(this._keys,this._values,ne)},W.prototype.entries=function(){return new _(this._keys,this._values,le)},W.prototype["@@iterator"]=function(){return this.entries()},W.prototype[i]=function(){return this.entries()},W.prototype._find=function(T,P){if(!Xn(this._cacheKey,T)){this._cacheIndex=-1;for(var C=0;C<this._keys.length;C++)if(Xn(this._keys[C],T)){this._cacheIndex=C;break}}return this._cacheIndex<0&&P&&(this._cacheIndex=this._keys.length,this._keys.push(T),this._values.push(void 0)),this._cacheIndex},W})();return O;function U(W,T){return W}function ne(W,T){return T}function le(W,T){return[W,T]}}function sa(){var f=(function(){function g(){this._map=new y}return Object.defineProperty(g.prototype,"size",{get:function(){return this._map.size},enumerable:!0,configurable:!0}),g.prototype.has=function(_){return this._map.has(_)},g.prototype.add=function(_){return this._map.set(_,_),this},g.prototype.delete=function(_){return this._map.delete(_)},g.prototype.clear=function(){this._map.clear()},g.prototype.keys=function(){return this._map.keys()},g.prototype.values=function(){return this._map.keys()},g.prototype.entries=function(){return this._map.entries()},g.prototype["@@iterator"]=function(){return this.keys()},g.prototype[i]=function(){return this.keys()},g})();return f}function ia(){var f=16,g=l.create(),_=O();return(function(){function T(){this._key=O()}return T.prototype.has=function(P){var C=U(P,!1);return C!==void 0?l.has(C,this._key):!1},T.prototype.get=function(P){var C=U(P,!1);return C!==void 0?l.get(C,this._key):void 0},T.prototype.set=function(P,C){var M=U(P,!0);return M[this._key]=C,this},T.prototype.delete=function(P){var C=U(P,!1);return C!==void 0?delete C[this._key]:!1},T.prototype.clear=function(){this._key=O()},T})();function O(){var T;do T="@@WeakMap@@"+W();while(l.has(g,T));return g[T]=!0,T}function U(T,P){if(!r.call(T,_)){if(!P)return;Object.defineProperty(T,_,{value:l.create()})}return T[_]}function ne(T,P){for(var C=0;C<P;++C)T[C]=Math.random()*255|0;return T}function le(T){if(typeof Uint8Array=="function"){var P=new Uint8Array(T);return typeof crypto<"u"?crypto.getRandomValues(P):typeof msCrypto<"u"?msCrypto.getRandomValues(P):ne(P,T),P}return ne(new Array(T),T)}function W(){var T=le(f);T[6]=T[6]&79|64,T[8]=T[8]&191|128;for(var P="",C=0;C<f;++C){var M=T[C];(C===4||C===6||C===8)&&(P+="-"),M<16&&(P+="0"),P+=M.toString(16).toLowerCase()}return P}}function Zn(f){return f.__=void 0,delete f.__,f}})})(ao||(ao={}))});k();var rg=ze(uo(),1),Pe=ze(require("process"),1);k();var Ba=ze(require("os"),1),ja=ze(require("path"),1),qa=require("url"),Ha=require("util");k();k();var ce=class extends Error{},ya=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;function wa(t){if(typeof t!="string")return!1;let e=ya.exec(t);if(!e)return!1;let[,n,r,o,s,i,a]=e.map(Number);if(!Number.isFinite(Date.parse(t)))return!1;let u=new Date(Date.UTC(n,r-1,o,s,i,a));return u.getUTCFullYear()===n&&u.getUTCMonth()===r-1&&u.getUTCDate()===o&&u.getUTCHours()===s&&u.getUTCMinutes()===i&&u.getUTCSeconds()===a}function _a(t){if(t===null||typeof t!="object")return null;let e=t.shared;if(e==null)return null;if(typeof e!="object")throw new ce(`catalog "shared" block is ${typeof e}, expected an object`);let{shared_org_id:n,endpoints:r,regions:o,compliance:s}=e;if(typeof n!="string"||n==="")throw new ce(`catalog "shared" block has no shared_org_id: ${JSON.stringify(e)}`);if(!Array.isArray(r))throw new ce(`catalog "shared" block endpoints is not an array: ${JSON.stringify(e)}`);let i=r.map((a,u)=>{if(a===null||typeof a!="object")throw new ce(`catalog shared endpoints[${u}] is not an object: ${JSON.stringify(a)}`);let d=a;for(let p of["model_id","variant","app_slug","function"])if(typeof d[p]!="string"||d[p].length===0)throw new ce(`catalog shared endpoints[${u}] (${JSON.stringify(d)}) has no valid "${p}"`);let l=(p,y)=>{if(d[p]!==void 0&&d[p]!==null&&!y)throw new ce(`catalog shared endpoints[${u}] (${d.model_id}:${d.variant}) carries a malformed "${p}": ${JSON.stringify(d[p])}`)};l("ready",typeof d.ready=="boolean"),l("ready_runtimes",typeof d.ready_runtimes=="number"&&Number.isInteger(d.ready_runtimes)&&d.ready_runtimes>=0),l("ready_since",wa(d.ready_since));for(let p of["model_id","variant"]){let y=d[p];if(y.includes(":"))throw new ce(`catalog shared endpoints[${u}] has "${p}" containing ":" (${JSON.stringify(y)}) \u2014 the external identity <model_id>:<variant> would be unparseable (resolveSharedModel splits at the LAST colon); platform catalog defect`)}return l("output_free",typeof d.output_free=="boolean"),d});if(o!=null&&(!Array.isArray(o)||!o.every(a=>typeof a=="string"&&a.length>0)))throw new ce(`catalog "shared" block regions is malformed: ${JSON.stringify(o)}`);if(s!=null){let a=s;if(typeof s!="object"||typeof a.zdr!="boolean"||typeof a.hipaa!="boolean")throw new ce(`catalog "shared" block compliance is malformed (operator-declared zdr + hipaa booleans required): ${JSON.stringify(s)}`);for(let u of Object.keys(a))if(u!=="zdr"&&u!=="hipaa")throw new ce(`catalog "shared" block compliance carries "${u}" beyond the declared zdr/hipaa contract: ${JSON.stringify(s)}`)}return i.length===0?null:{shared_org_id:n,endpoints:i,...o!=null?{regions:o}:{},...s!=null?{compliance:s}:{}}}function ut(t){return[...t.endpoints].sort((e,n)=>e.model_id.localeCompare(n.model_id)||e.variant.localeCompare(n.variant))}function lo(t,e,n){let r=(e??"").trim();if(r==="")return null;let o=r,s=n,i=r.lastIndexOf(":");i>0&&(o=r.slice(0,i),s=r.slice(i+1));let a=t.endpoints.filter(d=>d.model_id===o);if(a.length===0)return null;if(s!==void 0){let d=a.find(l=>l.variant===s);return d?{shared_org_id:t.shared_org_id,endpoint:d}:null}if(a.length===1)return{shared_org_id:t.shared_org_id,endpoint:a[0]};let u=a.filter(d=>d.compat_default===!0);if(u.length!==1)throw new ce(`shared model "${o}" has ${u.length} compat_default rows (exactly one required) among variants ${a.map(d=>d.variant).join(", ")}`);return{shared_org_id:t.shared_org_id,endpoint:u[0]}}async function co(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/catalog`,r=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new ce(`catalog read failed: GET ${n} \u2192 ${r.status}`);return _a(await r.json())}k();function Be(t){return t.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}k();var Re=class extends Error{name="ModelPriceError"},er="per_million_input_tokens",tr="per_million_output_tokens",fo="model_id,variant,unit,price_usd,effective_at,expires_at",po={per_request:!0,per_minute:!0,per_output_unit:!0},va=5e3;function ba(t,e){let n=typeof t=="number"?t:typeof t=="string"&&t.trim()!==""?Number(t):NaN;if(!Number.isFinite(n)||n<0)throw new Re(`${e}: price_usd is not a non-negative number (${JSON.stringify(t)})`);return n}function mo(t,e,n){let r=typeof t=="string"?Date.parse(t):NaN;if(!Number.isFinite(r))throw new Re(`${e}: ${n} is not a parseable timestamp (${JSON.stringify(t)})`);return r}function ka(t,e,n){return t===null?null:mo(t,e,n)}function Sa(t,e){if(t===er)return"input";if(t===tr)return"output";if(typeof t=="string"&&Object.hasOwn(po,t))return null;throw new Re(`${e}: unit ${JSON.stringify(t)} is neither a per-token rate (${er} | ${tr}) nor a known GPU-time unit (${Object.keys(po).join(" | ")}). Skipping it would publish no prices forever with no signal \u2014 if ENG-317 named the per-token units differently, teach this module the new names instead`)}function Ra(t,e){let n=t.reduce((o,s)=>s.effective_at>o.effective_at?s:o,t[0]),r=t.find(o=>o.effective_at===n.effective_at&&o.usd!==n.usd);return r?{reason:`two ${e} rates are effective at the same instant (${n.usd} and ${r.usd} USD/Mtok), so the winner would be read order \u2014 this model is served UNPRICED until one of them is expired; expire one of them`}:{rate:n}}function nr(t,e){return`${t}\0${e}`}function xa(t){if(!Array.isArray(t))throw new Re(`model_prices read is not an array: ${JSON.stringify(t)}`);let e=[];for(let n=0;n<t.length;n++){let r=t[n],o=`model_prices[${n}]`;if(r===null||typeof r!="object"||Array.isArray(r))throw new Re(`${o} is not an object: ${JSON.stringify(r)}`);let s=r;for(let d of fo.split(","))if(!Object.hasOwn(s,d))throw new Re(`${o} has no "${d}" column: ${JSON.stringify(s)}`);let i=Sa(s.unit,o);if(i===null)continue;let a=s.model_id;if(typeof a!="string"||a==="")throw new Re(`${o} has no model_id (${JSON.stringify(s)})`);let u=s.variant;if(!(u===null||typeof u=="string"&&u!==""))throw new Re(`${o} has a non-string variant (${JSON.stringify(u)})`);e.push({model_id:a,variant:u,side:i,usd:ba(s.price_usd,o),effective_at:mo(s.effective_at,o,"effective_at"),expires_at:ka(s.expires_at,o,"expires_at")})}return e}function go(t,e=Date.now(),n){let r=new Map,o=new Map;for(let u of t){if(u.effective_at>e||u.expires_at!==null&&u.expires_at<=e)continue;let d=r.get(u.model_id);d||(d=new Map,r.set(u.model_id,d),o.set(u.model_id,new Map));let l=u.variant??"",p=d.get(l);p||(p={input:[],output:[]},d.set(l,p),o.get(u.model_id).set(l,u.variant)),p[u.side].push({effective_at:u.effective_at,usd:u.usd})}let s=[],i=[];for(let[u,d]of r){let l=d.get(""),p=(y,c,m)=>{let w=y&&y.length>0?y:l?.[c];if(!w||w.length===0)return null;let R=Ra(w,c);return"reason"in R?R:R.rate.usd===0?c==="output"&&m!==null&&n?.has(nr(u,m))?R.rate:{reason:`the ${c} rate in force is zero, which publishes as "free" and would top :cheapest routing \u2014 this model is served UNPRICED; delete the row if the model is not token-priced, or set a real rate`}:R.rate};for(let[y,c]of o.get(u)){let m=d.get(y),w=p(m?.input,"input",c),R=p(m?.output,"output",c);if(w!==null&&"reason"in w){i.push({model_id:u,variant:c,reason:w.reason});continue}if(R!==null&&"reason"in R){i.push({model_id:u,variant:c,reason:R.reason});continue}w===null||R===null||s.push({model_id:u,variant:c,pricing:{input:w.usd,output:R.usd}})}}let a=(u,d)=>u.model_id.localeCompare(d.model_id)||(u.variant===null?1:0)-(d.variant===null?1:0)||(u.variant??"").localeCompare(d.variant??"");return{prices:s.sort(a),refusals:i.sort(a)}}function kt(t,e,n){if(n.refusals.some(s=>s.model_id===t&&s.variant===e))return null;let r=n.prices.find(s=>s.model_id===t&&s.variant===e);if(r)return r.pricing;if(n.refusals.some(s=>s.model_id===t&&s.variant===null))return null;let o=n.prices.find(s=>s.model_id===t&&s.variant===null);return o?o.pricing:null}var St={prices:[],refusals:[]};async function ho(t){let e=t.fetchImpl??fetch,n=`${er},${tr}`,r=await e(`${t.catalogUrl}/model_prices?select=${fo}&unit=in.(${n})`,{headers:{apikey:t.anonKey,Authorization:`Bearer ${t.anonKey}`},signal:AbortSignal.timeout(va)});if(!r.ok)throw new Error(`model_prices fetch failed: ${r.status}`);return xa(await r.json())}k();var Ea={chat:!0,code:!0,agent:!0,vl:!0},yo={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function Oa(t){let e=t.toLowerCase();return Object.hasOwn(yo,e)?yo[e]:null}var wo={"us-east-1":"US","us-east-2":"US","us-west-1":"US","us-west-2":"US","ca-central-1":"CA","ca-west-1":"CA","sa-east-1":"BR","eu-central-1":"DE","eu-central-2":"CH","eu-west-1":"IE","eu-west-2":"GB","eu-west-3":"FR","eu-north-1":"SE","eu-south-1":"IT","eu-south-2":"ES","af-south-1":"ZA","ap-east-1":"HK","ap-south-1":"IN","ap-south-2":"IN","ap-northeast-1":"JP","ap-northeast-2":"KR","ap-northeast-3":"JP","ap-southeast-1":"SG","ap-southeast-2":"AU","ap-southeast-3":"ID","ap-southeast-4":"AU","ap-southeast-5":"MY","ap-southeast-7":"TH","il-central-1":"IL","me-central-1":"AE","me-south-1":"BH"};function Aa(t,e){if(!t||t.length===0)return[];let n=[],r=[];for(let o of t){let s=Object.hasOwn(wo,o)?wo[o]:null;if(s===null){r.push(o);continue}n.some(a=>a.region===o)||n.push({country_code:s,region:o})}return r.length>0&&e(`urun openrouter doc: shared block region(s) ${r.join(", ")} have no country mapping \u2014 omitted from datacenters, never guessed`),n}var _o={qwen:"Qwen",qwen1:"Qwen",qwen2:"Qwen",qwen3:"Qwen",gemma:"Gemma",diffusiongemma:"Gemma",gpt:"GPT",glm:"GLM",deepseek:"DeepSeek",mistral:"Mistral"},vo={2:"Llama2",3:"Llama3"};function Ia(t){if(!t)return null;let e=t.split("@")[0],n=e.indexOf("/"),r=(n===-1?e:e.slice(n+1)).toLowerCase().split(/[^a-z0-9]+/).filter(o=>o.length>0);for(let o=0;o<r.length;o+=1){let s=r[o];if(Object.hasOwn(_o,s))return _o[s];let i=r[o+1];if(s==="llama"&&i!==void 0&&Object.hasOwn(vo,i))return vo[i];if(s==="phi"&&i!==void 0&&/^\d/.test(i))return"Phi"}return null}var Ta=["context_length","max_model_len"];function Pa(t){let e=Ta.map(n=>t.engine_args?.[n]).filter(n=>typeof n=="number"&&Number.isFinite(n)&&n>0);return e.length===0?null:e.every(n=>n===e[0])?e[0]:null}function Jt(t){let e=t.map(Pa),n=e[0];return n==null?null:e.every(r=>r===n)?n:null}function Ca(t){let e=t.engine_args?.reasoning_parser;return e==null||e===""?!1:typeof e=="string"?!0:null}function rr(t){if(t.length===0)return null;let e=t.map(Ca),n=e[0];return n==null?null:e.every(r=>r===n)?n:null}function $a(t,e){let n=e.filter(o=>Be(`${o.model_id}-${o.variant}`)===t);if(n.length>0)return Wt(n);let r=e.filter(o=>Be(o.model_id)===t);return r.length>0?Wt(r):null}function Wt(t){let e=t[0];return t.every(r=>r.model_id===e.model_id&&r.variant===e.variant)?{row:t.length===1?e:Ma(t),matches:t}:null}function Ma(t){let e=t[0],n=r=>t.every(o=>(o[r]??void 0)===(e[r]??void 0))?e[r]:void 0;return{model_id:e.model_id,variant:e.variant,gpu_spec:n("gpu_spec"),engine:n("engine"),task:n("task"),description:n("description"),size_gb:n("size_gb"),hf_repo:n("hf_repo"),updated_at:n("updated_at")}}function So(t){let e=t.updated_at?Date.parse(t.updated_at):NaN;return Number.isFinite(e)?Math.floor(e/1e3):0}function Kt(t,e,n){let r=$a(t,e);if(!r)return{id:t,object:"model",created:0,owned_by:"urun"};let{row:o,matches:s}=r,i=Jt(s),a=kt(o.model_id,o.variant,n),u=Ro(o,s),d=rr(s);return{id:t,object:"model",created:So(o),owned_by:"urun",name:`${o.model_id}:${o.variant}`,...o.description?{description:o.description}:{},...o.task?{task:o.task}:{},...o.engine?{engine:o.engine}:{},...o.gpu_spec?{gpu_spec:o.gpu_spec}:{},...i!==null?{context_length:i}:{},...a!==null?{pricing:a}:{},...u.published.length>0?{voices:[...u.published]}:{},...d!==null?{capabilities:{reasoning:d}}:{}}}var La=["Vivian","Serena","Uncle_Fu","Dylan","Eric","Ryan","Aiden","Ono_Anna","Sohee"];function Ro(t,e){if((t.task??void 0)!=="tts")return{published:[],defect:null};let n=e.map(s=>s.engine_args?.voices),r=n[0];if(Array.isArray(r)&&r.length>0&&r.every(s=>typeof s=="string")&&n.every(s=>Array.isArray(s)&&s.length===r.length&&s.every((i,a)=>i===r[a])))return{published:[...r],defect:null};let o=n.some(s=>s!=null)?`the catalog's engine_args.voices for ${t.model_id}:${t.variant} could not be vouched (empty, malformed, or not an identical non-empty string array on every GPU placement)`:null;return t.model_id==="qwen3-tts-customvoice"?{published:[...La],defect:o}:{published:[],defect:o}}function or(t){let e=t.length>0?Wt(t):null;return e===null?{published:[],defect:null}:Ro(e.row,e.matches)}function bo(t){let e=/^(\d+)(?:\.(\d+))?(?:e([+-]?\d+))?$/.exec(String(t));if(!e)throw new Error(`unrenderable per-token price: ${String(t)}`);let[,n="",r="",o=""]=e,s=n+r,i=n.length-6+(o===""?0:Number(o));return(i<=0?`0.${"0".repeat(-i)}${s}`:`${s.slice(0,i)}.${s.slice(i).padEnd(1,"0")}`).replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,"")}function ko(t){return{input:[{type:"prompt",unit:"token",cost_usd:bo(t.input)}],output:[{type:"completion",unit:"token",cost_usd:bo(t.output)}]}}function Ua(t,e,n,r){if(e.length===0)return null;let o=Wt(e);if(!o)return null;let{row:s,matches:i}=o;if(!s.task||Ea[s.task]!==!0)return null;let a=Jt(i),u=kt(s.model_id,s.variant,n),d=Ia(s.hf_repo),l={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};return a!==null&&(l.max_tokens={type:"integer",min:1,max:a,unit:"token"}),{schema_version:"2.4",id:`${s.model_id}:${s.variant}`,name:`${s.model_id}:${s.variant}`,created:So(s),quantization:Oa(s.variant),...d!==null?{tokenizer:d}:{},description:s.description??"",hugging_face_id:s.hf_repo?s.hf_repo.split("@")[0]??s.hf_repo:"",is_ready:t.ready===!0&&u!==null,is_free:!1,input_modalities:[{type:"text",...a!==null?{supported_inputs:{max_context_length:{value:a,unit:"token"}}}:{},...u?{pricing:ko(u).input}:{}},...s.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:l,...u?{pricing:ko(u).output}:{}}],...r.datacenters.length>0?{datacenters:r.datacenters}:{},...r.compliance?{compliance:r.compliance}:{}}}function xo(t,e,n,r){let o=r??(d=>console.warn(d.trimEnd())),s=t?ut(t):[];if(s.length>0){let d=s.filter(l=>l.ready===void 0);if(d.length===s.length)o("urun openrouter doc: readiness seam absent from shared block; declaring every endpoint not ready");else if(d.length>0){let l=d.map(p=>`${p.model_id}:${p.variant}`).join(", ");o(`urun openrouter doc: readiness seam partially deployed \u2014 endpoints ${l} carry no ready field and are declared not ready`)}}let i={datacenters:Aa(t?.regions,o),compliance:t?.compliance??null},a=[],u=new Set;for(let d of s){let l=`${d.model_id}:${d.variant}`;if(u.has(l))continue;u.add(l);let p=Ua(d,e.filter(y=>y.model_id===d.model_id&&y.variant===d.variant),n,i);p&&a.push(p)}return{data:a}}var z=class extends Error{},pe=class extends Error{},ir="urs1.";function Eo(t,e){return ir+Buffer.from(JSON.stringify([t,e]),"utf8").toString("base64url")}function Na(t){if(!t.startsWith(ir))return null;try{let e=JSON.parse(Buffer.from(t.slice(ir.length),"base64url").toString("utf8"));if(Array.isArray(e)&&typeof e[0]=="string"&&typeof e[1]=="string")return{app:e[0],key:e[1]}}catch{}return null}async function To(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/apps`,r=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new Error(`org apps listing failed: GET ${n} \u2192 ${r.status}`);let o=await r.json();if(!Array.isArray(o.apps))throw new Error(`org apps listing returned no "apps" array (GET ${n})`);if(o.truncated===!0)throw new Error(`org apps listing was truncated (GET ${n} returned ${o.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return o.apps}function Oo(t){if(t.length===0)return[];if(!t.every(n=>(n.task??void 0)==="image"))return[];let e=Fa(t);return e===null?[]:[e]}function sr(t,e,n,r,o){if(!o)throw new ce(`shared model "${t}" cannot be gated: no catalog oracle is configured, and a shared endpoint's modality must come from the model's OWN model_catalog row \u2014 never a default`);if(e===void 0||n===void 0)throw new ce(`shared model "${t}" resolved without a catalog id + variant to join on`);let s=r.filter(i=>i.model_id===e&&i.variant===n);if(s.length===0)throw new ce(`shared model "${t}" names endpoint ${e}:${n} but the catalog has no model_catalog row for it \u2014 the shared block cannot vouch a modality from nothing`);return s}function Ao(t,e){if(!e.every(o=>(o.task??void 0)==="image"))return[];let n=e.map(o=>o.engine_args?.expects_image),r=n[0];if(typeof r!="boolean"||!n.every(o=>o===r))throw new ce(`shared image model "${t}" does not vouch an explicit image mode: its ${e.length} model_catalog row(s) carry engine_args.expects_image ${JSON.stringify(n)} \u2014 the shared lane requires a boolean (false = pure text-to-image generation, true = edit) and refuses to silently default to edit`);return[r===!1?"generation":"edit"]}var Po="shared:",Xe="speech:";function Rt(t){return t.sharedApp?Po+t.appSlug:t.appSlug}function xt(t){return typeof t.modelId=="string"&&typeof t.variant=="string"?{kind:"catalog",model_id:t.modelId,variant:t.variant}:{kind:"not-a-catalog-model",model:t.appSlug}}function Ye(t,e){let n=e.filter(s=>Be(`${s.model_id}-${s.variant}`)===t);if(n.length>0)return n;let r=e.filter(s=>Be(s.model_id)===t);return new Set(r.map(s=>s.variant)).size===1?r:[]}function Da(t){let e=t.engine_args?.expects_image;return e==null?"edit":e===!1?"generation":e===!0?"edit":null}function Fa(t){let e=t.map(Da),n=e[0];return n!=null&&e.every(r=>r===n)?n:null}var Io={stt:"stt",tts:"tts"};function Ge(t){if(t.length===0)return null;let e=t[0].task??void 0;return e===void 0||Io[e]===void 0?null:t.every(n=>(n.task??void 0)===e)?Io[e]:null}var zt=6e4,Yt=class{constructor(e){this.opts=e}opts;pool=new Map;appsCache=null;rowsCache=null;seed(e,n){this.pool.set(e,Promise.resolve(n))}async deployedApps(){if(!this.opts.listApps)return[];let e=this.opts.appsTtlMs??zt;if(this.appsCache&&Date.now()-this.appsCache.at<e)return this.appsCache.apps;let n=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:n},n}async sharedBlock(){if(!this.opts.shared)return null;let e=this.opts.appsTtlMs??zt;if(this.sharedCache&&Date.now()-this.sharedCache.at<e)return this.sharedCache.block;let n=await this.opts.shared();return this.sharedCache={at:Date.now(),block:n},n}sharedCache=null;async catalogRows(){if(!this.opts.listCatalog)return[];let e=this.opts.appsTtlMs??zt;if(this.rowsCache&&Date.now()-this.rowsCache.at<e)return this.rowsCache.rows;let n=await this.opts.listCatalog();return this.rowsCache={at:Date.now(),rows:n},n}pricesCache=null;async priceRows(){if(!this.opts.listPrices)return[];let e=this.opts.appsTtlMs??zt;if(this.pricesCache&&Date.now()-this.pricesCache.at<e)return this.pricesCache.rows;let n=await this.opts.listPrices();return this.pricesCache={at:Date.now(),rows:n},n}async pricesForListing(e){let n;try{n=await this.priceRows()}catch(o){if(o instanceof Re)throw o;return this.warn(`urun /v1/models: the per-token price surface could not be read (${String(o instanceof Error?o.message:o)}) \u2014 every model is listed UNPRICED for now; Hugging Face will show no price until the read succeeds
|
|
3
|
+
`),St}let r=go(n,Date.now(),e);for(let o of r.refusals)this.warn(`urun /v1/models: refusing to price ${o.model_id}:${o.variant??"*"} \u2014 ${o.reason}
|
|
4
|
+
`);return r}outputFreeKeys(e){let n=new Set;if(!e)return n;for(let r of e.endpoints)r.output_free===!0&&n.add(nr(r.model_id,r.variant));return n}warn(e){(this.opts.warn??(r=>console.warn(r.trimEnd())))(e)}servable(e){return e.filter(n=>n.serves==="openai"&&n.deployment_status==="active").map(n=>n.app_slug)}async availableIds(){let e=this.opts.defaultApp,n=new Set(e===null?[]:[e]);if(this.opts.listApps)for(let o of this.servable(await this.deployedApps()))n.add(o);for(let o of this.pool.keys())o.startsWith(Po)||o.startsWith(Xe)||n.add(o);let r=[...n].filter(o=>o!==e).sort();return e===null?r:[e,...r]}async noDefaultApp(e){let n=await this.availableIds(),r=n.length>0?n.join(", "):"(none deployed)";throw new z(e===""?`no "model" was given and this endpoint has no default model \u2014 name one of your org's deployed apps that declare serves="openai"; available models: ${r}`:`model "${e}" does not name a deployed app in your org that declares serves="openai", and this endpoint has no default model to fall back to; available models: ${r}`)}async resolveTarget(e){let n=(e??"").trim(),r=this.opts.defaultApp;if(!n||n==="urun")return r===null?this.noDefaultApp(n):{appSlug:r};let o=Be(n);if(o===r)return{appSlug:r};if(this.pool.has(o))return{appSlug:o};if(this.opts.listApps){let s=await this.deployedApps(),i=this.servable(s);if(i.includes(o))return{appSlug:o};let a=i.filter(d=>d.startsWith(`${o}-`));if(a.length===1)return{appSlug:a[0]};if(a.length>1)throw new z(`model "${n}" is ambiguous across deployed apps ${a.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let u=s.find(d=>d.app_slug===o||d.app_slug.startsWith(`${o}-`));if(u)throw new z(`model "${n}" maps to app "${u.app_slug}", which has no function declaring serves="openai" \u2014 add serves="openai" to its @app.function or deploy it with \`urun serve ${n}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.shared){let s=await this.sharedBlock();if(s){let i=lo(s,n);if(i)return{appSlug:i.endpoint.app_slug,fnName:i.endpoint.function,sharedApp:!0,modelId:i.endpoint.model_id,variant:i.endpoint.variant,sharedOrgId:s.shared_org_id}}}if(this.opts.listCatalog){let i=(await this.opts.listCatalog()).find(a=>n===a.model_id||n===`${a.model_id}:${a.variant}`||o===Be(a.model_id)||o===Be(`${a.model_id}-${a.variant}`));if(i)throw new z(`model "${n}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${i.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return r===null?this.noDefaultApp(n):{appSlug:r}}async catalogIdentityFor(e){let n=xt(await this.resolveTarget(e));return n.kind==="catalog"?{model_id:n.model_id,variant:n.variant}:null}async servingAppSlugFor(e){return(await this.resolveTarget(e)).appSlug}async targetFor(e){return this.resolveTarget(e)}async resolveApp(e){return Rt(await this.resolveTarget(e))}async sessionFor(e){let n=await this.resolveTarget(e);return this.acquire(n,Rt(n))}async speechSessionFor(e){let n=await this.resolveTarget(e);return this.acquire(n,Xe+Rt(n))}async acquire(e,n){let r=this.pool.get(n);return r||(r=Promise.resolve(this.opts.openSession(e,n)),this.pool.set(n,r),r.catch(()=>this.pool.delete(n))),{app:n,entry:await r,identity:xt(e)}}keyOf(e){let n=this.opts.sessionKey;if(!n)throw new Error("ModelRouter: sessionKey is not configured \u2014 the session-identity seam (handleFor/sessionForHandle) is unavailable on this router");let r=n(e);if(typeof r!="string"||r.length===0)throw new Error("ModelRouter: sessionKey returned an empty identity for a pooled session");return r}async handleFor(e){let{app:n,entry:r}=await this.sessionFor(e);return{app:n,handle:Eo(n,this.keyOf(r))}}async speechHandleFor(e){let{app:n,entry:r}=await this.speechSessionFor(e);return{app:n,handle:Eo(n,this.keyOf(r))}}async sessionForHandle(e){let n=Na(e);if(!n)throw new pe(`malformed session handle ${JSON.stringify(e)} \u2014 not issued by this proxy`);let{app:r,key:o}=n,s=this.pool.get(r),i=s?await s.then(a=>a,()=>null):null;if(i===null)throw new pe(`the session behind this handle (app "${r}") is gone \u2014 closed, evicted after its pod died, or the proxy restarted; it cannot be reattached. Reconnect without a handle to start a new session.`);if(this.keyOf(i)!==o)throw new pe(`the session behind this handle (app "${r}") was replaced \u2014 the original backhaul died and a new session serves this app now; the handle's session cannot be reattached. Reconnect without a handle to start a new session.`);return{app:r,entry:i}}async evict(e,n){let r=this.pool.get(e);!r||await r.then(s=>s,()=>null)!==n||this.pool.get(e)!==r||(this.pool.delete(e),await this.opts.closeSession(n).catch(()=>{}))}async modelList(){let e=this.opts.sharedOnlyListing?[]:await this.availableIds(),n=await this.sharedBlock(),[r,o]=await Promise.all([this.catalogRows().then(d=>({ok:!0,rows:d}),()=>({ok:!1,rows:[]})),this.pricesForListing(this.outputFreeKeys(n))]),s=r.rows,i=r.ok?o:St;r.ok||this.warn(`urun /v1/models: the model catalog could not be read \u2014 entries fall back to the bare OpenAI fields AND no model is priced on either lane (pricing follows catalog identity, so a half-priced listing would flicker on Hugging Face)
|
|
5
|
+
`);let u=(n?ut(n):[]).map(d=>{let l={id:`${d.model_id}:${d.variant}`,object:"model",created:0,owned_by:"urun",name:`${d.model_id}:${d.variant}`};d.gpu_spec&&(l.gpu_spec=d.gpu_spec),typeof d.warm=="number"&&(l.warm=d.warm);let p=s.filter(R=>R.model_id===d.model_id&&R.variant===d.variant),y=Jt(p);y!==null&&(l.context_length=y);let c=rr(p);c!==null&&(l.capabilities={reasoning:c});let m=kt(d.model_id,d.variant,i);m!==null&&(l.pricing=m);let w=or(p);return w.published.length>0&&(l.voices=[...w.published]),l});return{object:"list",data:this.opts.sharedOnlyListing?u:[...e.map(d=>Kt(d,s,i)),...u]}}async openRouterModels(){let e=await this.sharedBlock(),[n,r]=await Promise.all([this.catalogRows(),this.pricesForListing(this.outputFreeKeys(e))]);return xo(e,n,r,o=>this.warn(o))}async supportsImages(e,n){let r=await this.resolveTarget(e),o=await this.catalogRows();if(r.sharedApp){let s=sr(String(e),r.modelId,r.variant,o,this.opts.listCatalog!==null);return Ao(`${r.modelId}:${r.variant}`,s).includes(n)}return Oo(Ye(r.appSlug,o)).includes(n)}async supportsAudio(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Ge(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))!==null:Ge(Ye(n.appSlug,r))!==null}async supportsSpeech(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Ge(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))==="tts":Ge(Ye(n.appSlug,r))==="tts"}async supportsTranscription(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Ge(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))==="stt":Ge(Ye(n.appSlug,r))==="stt"}async speechVoices(e){let n=await this.resolveTarget(e),r=await this.catalogRows(),o=n.sharedApp?r.filter(s=>s.model_id===n.modelId&&s.variant===n.variant):Ye(n.appSlug,r);return or(o)}async imageModelList(){let e=await this.catalogRows(),n=St,r=[];for(let s of await this.availableIds()){let i=Oo(Ye(s,e));i.length!==0&&r.push({...Kt(s,e,n),image_modes:i})}let o=await this.sharedBlock();if(o)for(let s of ut(o)){let i=sr(`${s.model_id}:${s.variant}`,s.model_id,s.variant,e,this.opts.listCatalog!==null),a=Ao(`${s.model_id}:${s.variant}`,i);if(a.length===0)continue;let u={id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun",name:`${s.model_id}:${s.variant}`,image_modes:a};s.gpu_spec&&(u.gpu_spec=s.gpu_spec),typeof s.warm=="number"&&(u.warm=s.warm),r.push(u)}return{object:"list",data:r}}async speechToTextModelList(){let e=await this.catalogRows(),n=St,r=[];for(let s of await this.availableIds())Ge(Ye(s,e))==="stt"&&r.push(Kt(s,e,n));let o=await this.sharedBlock();if(o)for(let s of ut(o)){let i=sr(`${s.model_id}:${s.variant}`,s.model_id,s.variant,e,this.opts.listCatalog!==null);if(Ge(i)!=="stt")continue;let a={id:`${s.model_id}:${s.variant}`,object:"model",created:0,owned_by:"urun",name:`${s.model_id}:${s.variant}`};s.gpu_spec&&(a.gpu_spec=s.gpu_spec),typeof s.warm=="number"&&(a.warm=s.warm),r.push(a)}return{object:"list",data:r}}async closeAll(){let e=[...this.pool.values()],n=[];if(await Promise.all(e.map(async r=>{try{await this.opts.closeSession(await r)}catch(o){n.push(String(o instanceof Error?o.message:o))}})),n.length>0)throw new Error(`failed to close ${n.length} pooled session(s): ${n.join("; ")}`)}};function Co(t,e="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(t).pathname))throw new Error(`${e} must be the session-gateway base (e.g. https://api.urun.sh), got ${t} \u2014 a trailing /v1 is the org control-plane API form (that belongs in URUN_API_URL); session allocation 404s against it. Drop the /v1.`);return t}function Xt(t,e="URUN_GATEWAY_URL"){let n=new URL(t),r=n.hostname==="localhost"||n.hostname==="127.0.0.1"||n.hostname==="[::1]";if(n.protocol!=="https:"&&!r)throw new Error(`${e} must be https (got ${t}) \u2014 the org API key rides the request's Authorization header`);return t}var Ga=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function $o(t){let e=t?.code;return typeof e=="string"&&Ga.has(e)}function $e(t,e){try{return t.write(e),!0}catch(n){if($o(n))return!1;throw n}}function Mo(t){let e={stdout:!1,stderr:!1},n=[];for(let r of["stdout","stderr"]){let o=t.streams[r],s=i=>{$o(i)&&(e[r]=!0);let a=i?.code;try{t.note(`${new Date().toISOString()} urun compat: process.${r} died (${String(a??(i instanceof Error?i.message:i))}) \u2014 the terminal is gone; launcher output continues in this log only
|
|
6
|
+
`)}catch{}};o.on("error",s),n.push([o,s])}return{isDead:r=>e[r],uninstall:()=>{for(let[r,o]of n)r.off("error",o)}}}k();var ye=require("@prometheus-io/client"),os=require("http");k();var Va={"/v1/chat/completions":"conversational","/v1/responses":"conversational","/v1/messages":"conversational","/v1/systemone":"conversational","/v1/images/generations":"text-to-image","/v1/images/edits":"image-to-image","/v1/audio/speech":"text-to-speech","/v1/audio/transcriptions":"speech-to-text"};function Wa(t){return Va[t]??null}var No=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function Ja(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function Ka(t){return t==="surface"}function za(t){let e=t?.ledgerFailureReason;return No.includes(e)?e:"unknown"}var Ya=["no_response_head","no_org","model_unresolved","no_model_named","queue_full"],Lo=["priced","unpriced"],Xa={written:Lo,already_written:Lo,skipped:Ya,failed:No},Do=Object.entries(Xa).flatMap(([t,e])=>e.map(n=>({outcome:t,reason:n})));function Qa(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function Za(t){let e=Date.parse(t.ts),n=Number.isFinite(t.duration_ms)?Math.max(0,t.duration_ms):0;return new Date(e-Math.round(n)).toISOString()}var eu=256,Uo=4096,Et=12e4,tu=250,nu=8e3,je=[],ar=new Set,Ot=0,dt=!1;function Fo(){return je.length}function Bo(t=Date.now()){let e=je[0];return e===void 0?0:Math.max(0,t-e.queuedAt)}var Qt=new Set;function jo(){return Ot===0&&je.length===0}function qo(){if(jo()){dt=!1;for(let t of[...Qt])t()}}async function Ho(t=Vo){return jo()?!0:await new Promise(e=>{let n=null,r=()=>{Qt.delete(r),n&&clearTimeout(n),e(!0)};Qt.add(r),n=setTimeout(()=>{Qt.delete(r),e(!1)},t),n.unref?.()})}function Go(){dt=!0;let t=[...je.splice(0,je.length),...ar];for(let e of t)e.settled||Qe(e,{outcome:"failed",reason:"dropped_on_exit"},{event:"inference_ledger_write_failed",reason:"dropped_on_exit",attempts:e.attempts,error:`the proxy is shutting down and this row was still unplaced after the ${Vo}ms drain \u2014 the request was served and answered, and its billing row was never written`});return qo(),t.length}var Vo=5e3;function Wo(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
|
|
7
|
+
`}function ru(t){$e(process.stdout,t)}function Jo(t,e){try{t(e)}catch{}}function Ko(t){let{record:e,ledger:n}=t;if(n===null)return;let r=n.report??ru,o=Wa(e.path);if(o===null)return;let s=l=>{try{n.observe(l)}catch{}},i=(l,p)=>{s({outcome:"skipped",reason:l}),Jo(r,Wo("inference_ledger_skipped",{inference_id:e.inference_id,path:e.path,reason:l,detail:p}))};if(e.status===null){i("no_response_head","no head flushed, so the caller never received an Inference-Id and no status can be recorded honestly");return}if(e.org===null){i("no_org","the request never resolved a tenant, so there is no org to attribute the row to");return}let a=e.catalog;if(a.kind==="unresolved"){i("model_unresolved",a.reason);return}let u;if(a.kind==="catalog")u={model_id:a.model_id,identity_kind:"catalog_model",variant:a.variant};else{if(a.model===null){i("no_model_named","the request resolved to a caller-org app but named no model, so there is no app slug to record and `model_id` cannot be written honestly");return}u={model_id:a.model,identity_kind:"caller_org_app",variant:null}}let d={inference_id:e.inference_id,...u,task:o,http_status:e.status,started_at:Za(e),completed_at:e.ts,billable_outcome:Qa(e),session_id:e.session_id,prompt_tokens:e.prompt_tokens,completion_tokens:e.completion_tokens,output_units:e.output_units,queue_ms:e.queue_ms,prefill_ms:e.prefill_ms,decode_ms:e.decode_ms,total_ms:e.total_ms,ttft_ms:e.ttft_ms===null?null:Math.round(e.ttft_ms),e2e_ms:Math.round(e.duration_ms)};if(je.length>=Uo){i("queue_full",`${je.length} ledger writes are already waiting (capacity ${Uo}, ${Ot} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}je.push({entry:d,req:t.req,record:e,ledger:n,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),Ot<eu&&(Ot+=1,ou())}async function ou(){try{for(;;){let t=su();if(t===void 0)return;ar.add(t);try{await iu(t)}finally{ar.delete(t)}}}finally{Ot-=1,qo()}}function su(){let t=Date.now();for(;;){let e=je.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<Et)return e;Qe(e,{outcome:"failed",reason:"dwell_expired"},{event:"inference_ledger_write_failed",reason:"dwell_expired",error:`this row waited ${n}ms for the control plane (bound ${Et}ms) after ${e.attempts} attempt(s) and was given up on \u2014 the request was served and answered, and it will never be billed. Hugging Face writes a request off ~30 minutes after serving, so a row placed later than this is not worth the memory to keep trying`})}}async function iu(t){let{entry:e,record:n,ledger:r}=t;for(;;){t.attempts+=1;try{let o=await r.write(t.req,e);Qe(t,{outcome:"written",reason:o.cost_nano_usd===null?"unpriced":"priced"},{event:"inference_ledger_written",inference_id:o.inference_id,path:n.path,model_id:e.model_id,identity_kind:e.identity_kind,variant:e.variant,org:n.org,session_id:e.session_id,cost_nano_usd:o.cost_nano_usd,price_version:o.price_version,unpriced_reason:o.unpriced_reason,attempts:t.attempts});return}catch(o){let s=Ja(o);if(s!==null){Qe(t,{outcome:"already_written",reason:s.cost_nano_usd===null?"unpriced":"priced"},{event:"inference_ledger_already_written",inference_id:e.inference_id,model_id:e.model_id,identity_kind:e.identity_kind,variant:e.variant,cost_nano_usd:s.cost_nano_usd,price_version:s.price_version,attempts:t.attempts});return}let i=za(o),a=Date.now()-t.queuedAt;if(dt){Qe(t,{outcome:"failed",reason:"dropped_on_exit"},{event:"inference_ledger_write_failed",reason:"dropped_on_exit",attempts:t.attempts,waited_ms:a,error:`the proxy is shutting down and this row was still being retried when the drain ran out \u2014 the request was served and answered, and its billing row was never written. Last failure: ${o instanceof Error?o.message:String(o)}`});return}if(Ka(i)&&a<Et){if(await uu(au(t.attempts)),dt||Date.now()-t.queuedAt>=Et){let u=Date.now()-t.queuedAt;Qe(t,{outcome:"failed",reason:dt?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:dt?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:u,error:`this row's backoff outlived the ${Et}ms dwell bound (waited ${u}ms over ${t.attempts} attempt(s)) and was given up on rather than starting an attempt that could only finish past it. Last failure: ${o instanceof Error?o.message:String(o)}`});return}continue}Qe(t,{outcome:"failed",reason:i},{event:"inference_ledger_write_failed",reason:i,attempts:t.attempts,waited_ms:a,error:o instanceof Error?o.message:String(o)});return}}}function au(t){return Math.min(nu,tu*2**Math.max(0,t-1))}function uu(t){return new Promise(e=>{setTimeout(e,t).unref?.()})}function Qe(t,e,n){if(t.settled)return;t.settled=!0;try{t.ledger.observe(e)}catch{}let{event:r,...o}=n;Jo(t.report,Wo(r,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...o}))}k();k();var en="/v1/usage/requests",zo=1e4,du=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,Ve=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},te=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}},Zt=class extends te{constructor(e){super(e),this.name="UsageWithholdingError"}},Zo=1024*1024;function lr(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new Ve(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new Ve(`"${e}" must be an array of uuid strings`);if(n.length>zo)throw new Ve(`"${e}" carries ${n.length} ids; at most ${zo} may be looked up in one request. Split the batch \u2014 the ids past the limit are NOT silently dropped`);let r=[],o=new Set;for(let s of n){if(typeof s!="string"||!du.test(s))throw new Ve(`"${e}" contains ${JSON.stringify(s)}, which is not a LOWERCASE uuid \u2014 every uRun inference id is the lowercase uuid returned in the \`Inference-Id\` response header, and answering a differently-cased spelling would echo back an id the caller cannot match`);o.has(s)||(o.add(s),r.push(s))}return r}function es(t){return lr(t,"inference_ids")}var lu=["inference_id","model_id","identity_kind","variant","task","http_status","started_at","completed_at","prompt_tokens","completion_tokens","output_units","cost_nano_usd","price_version","priced_at"];function lt(t,e,n){let r=t[e];if(typeof r!="string"||r==="")throw new te(`${n}: ${e} is not a non-empty string (got ${ct(r)})`);return r}function ur(t,e,n){let r=t[e];if(r===null)return null;if(typeof r!="string"||r==="")throw new te(`${n}: ${e} is neither null nor a non-empty string (got ${ct(r)})`);return r}function At(t,e,n,r){let o=t[e];if(o===null)return null;let s=typeof o=="number"?o:typeof o=="string"&&/^-?\d+(\.\d+)?$/.test(o.trim())?Number(o.trim()):NaN;if(!Number.isFinite(s))throw new te(`${n}: ${e} is not a number (got ${ct(o)})`);if(r&&!Number.isSafeInteger(s))throw new te(`${n}: ${e} is not a safely representable integer \u2014 a billing quantity that silently loses digits must fail rather than be reported`);if(s<0)throw new te(`${n}: ${e} is negative`);return Object.is(s,-0)?0:s}var cu=40,Yo=64,Xo=600;function ts(t){let e=Object.keys(t).sort(),r=e.slice(0,cu).map(o=>o.length>Yo?`${o.slice(0,Yo)}\u2026`:o).join(", ");return r.length>Xo&&(r=`${r.slice(0,Xo)}\u2026`),`${e.length} keys present: [${r}]`}function ct(t){return t===null?"null":t===void 0?"undefined":Array.isArray(t)?"an array":typeof t=="object"?"an object":`a ${typeof t}`}function dr(t){$e(process.stdout,`${JSON.stringify({event:"usage_row_rejected",ts:new Date().toISOString(),where:t.where,reason:t.reason})}
|
|
8
|
+
`)}function pu(t,e){let n;try{n=t(e)}catch{Qo(t,e);return}fu(n)&&Promise.resolve(n).catch(()=>{Qo(t,e)})}function fu(t){return typeof t?.then=="function"}function Qo(t,e){if(t!==dr)try{dr(e)}catch{}}function ns(t,e=dr){let n=t?.requests;if(!Array.isArray(n)){let o=t!==null&&typeof t=="object"&&!Array.isArray(t)?ts(t):ct(t);throw new te(`the usage surface answered without a "requests" array: got ${ct(n)} (${o})`)}let r=[];for(let o=0;o<n.length;o++){let s=`usage.requests[${o}]`;try{r.push(mu(n[o],s))}catch(i){if(i instanceof Zt||!(i instanceof te))throw i;pu(e,{where:s,reason:i.message})}}if(n.length>0&&r.length===0)throw new te(`the usage surface returned ${n.length} row(s) and NONE of them could be validated \u2014 answering an empty result here would be indistinguishable from "we hold nothing for these ids", which a billing caller treats as final and writes the whole batch off unbilled`);return r}function mu(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new te(`${e} is not an object: got ${ct(t)}`);let n=t;if(Object.hasOwn(n,"gpu_seconds"))throw new Zt(`${e} carries gpu_seconds \u2014 that column was DROPPED from the ledger (ENG-417): the cost of compute is metered once, on the instance, and a per-request figure counts the same GPU-second twice. Its reappearance means a second, competing measure has been reintroduced upstream`);for(let o of lu)if(!Object.hasOwn(n,o))throw new te(`${e} has no "${o}" column (${ts(n)})`);let r=At(n,"http_status",e,!0);if(r===null)throw new te(`${e}: http_status is null, but the ledger column is NOT NULL`);return{inference_id:lt(n,"inference_id",e),model_id:lt(n,"model_id",e),identity_kind:lt(n,"identity_kind",e),variant:ur(n,"variant",e),task:lt(n,"task",e),http_status:r,started_at:lt(n,"started_at",e),completed_at:lt(n,"completed_at",e),prompt_tokens:At(n,"prompt_tokens",e,!0),completion_tokens:At(n,"completion_tokens",e,!0),output_units:At(n,"output_units",e,!1),cost_nano_usd:At(n,"cost_nano_usd",e,!0),price_version:ur(n,"price_version",e),priced_at:ur(n,"priced_at",e)}}function tn(t,e){let n=new Set(e),r=new Set;for(let o=0;o<t.length;o++){let s=t[o];if(!n.has(s.inference_id))throw new te(`the usage surface returned an id at validated answer position ${o} that was not in the request (${e.length} asked, ${t.length} answered) \u2014 an answer carrying ids nobody asked about cannot be attributed to this caller`);if(r.has(s.inference_id))throw new te(`the usage surface returned ${s.inference_id} more than once \u2014 inference_id is the ledger primary key, so a duplicate means the upstream query is not the one we asked, and a caller that sums the entries would bill the request twice`);r.add(s.inference_id)}}var It="/partners/huggingface/v1/billing/requests";function gu(t){return lr(t,"requestIds")}function hu(t){return t>=200&&t<=399}function yu(t){let e=[];for(let n of t)n.cost_nano_usd!==null&&hu(n.http_status)&&e.push({requestId:n.inference_id,costNanoUsd:n.cost_nano_usd});return{requests:e.length>0?e:null}}async function rs(t,e){let n=gu(t),r=await e(n);return tn(r,n),yu(r)}var cr=9464,nn="/metrics",rn="(other)",wu="(none)",_u=64,vu=128,bu=["/v1/responses","/v1/chat/completions","/v1/systemone","/v1/messages","/v1/messages/count_tokens","/v1/models","/v1/images/models","/v1/images/generations","/v1/images/edits","/v1/audio/speech","/v1/audio/models","/v1/audio/transcriptions",en,It],ku=new Set(bu);function Su(t){return ku.has(t)?t:rn}function Ru(t){return t===null?"none":t>=500?"5xx":t>=400?"4xx":t>=300?"3xx":t>=200?"2xx":"1xx"}var on=class{constructor(e){this.max=e}max;admitted=new Set;overflows=0;of(e,n=!0){return e===null||e===""?wu:this.admitted.has(e)?e:n?this.admitted.size>=this.max?(this.overflows+=1,rn):(this.admitted.add(e),e):rn}get size(){return this.admitted.size}get overflowCount(){return this.overflows}},sn=class{registry=new ye.Registry;modelLabel=new on(_u);orgLabel=new on(vu);requests;duration;ttft;streamErrors;undelivered;ledgerWrites;ledgerQueue;labelCardinality;labelOverflows;constructor(e={}){this.requests=new ye.Counter({name:"urun_inference_requests_total",help:"Completed /v1 requests, by lane, method, HTTP status class, streaming, model and org.",labelNames:["lane","method","status_class","stream","model","org"],registers:[this.registry]}),this.duration=new ye.Histogram({name:"urun_inference_request_duration_seconds",help:"Wall time from request receipt to response close. For a stream this is the WHOLE generation, not the time to the head.",labelNames:["lane","stream","model","org"],buckets:[.1,.25,.5,1,2.5,5,10,30,60,120,300,600],registers:[this.registry]}),this.ttft=new ye.Histogram({name:"urun_inference_ttft_seconds",help:"Time to first output-text delta on a streamed response. The metric Hugging Face gates provider listing on (<5s for conversational models).",labelNames:["lane","model","org"],buckets:[.1,.25,.5,1,2,3,5,8,15,30,60],registers:[this.registry]}),this.streamErrors=new ye.Counter({name:"urun_inference_stream_errors_total",help:"Responses that emitted an error frame AFTER the head flushed. These are HTTP 200 with a complete body, so no status-based metric can see them.",labelNames:["lane","model"],registers:[this.registry]}),this.undelivered=new ye.Counter({name:"urun_inference_undelivered_total",help:"Responses whose socket did not finish flushing \u2014 the client vanished mid-answer.",labelNames:["lane","model"],registers:[this.registry]}),this.ledgerWrites=new ye.Counter({name:"urun_inference_ledger_writes_total",help:"Terminal fate of the per-request ledger row for each billable /v1 request. outcome=written is the row landing (reason=priced|unpriced); outcome=skipped and outcome=failed are BOTH revenue that went unrecorded. failed/duplicate is the ENG-410 fabricated-row signal.",labelNames:["outcome","reason"],registers:[this.registry]});for(let n of Do)this.ledgerWrites.inc({outcome:n.outcome,reason:n.reason},0);this.ledgerQueue=new ye.Gauge({name:"urun_inference_ledger_queue",help:'The ledger write queue right now. measure="depth" is rows waiting for a worker; measure="oldest_age_seconds" is how long the head has waited \u2014 approaching the dwell bound means rows are about to be given up on, which is revenue lost.',labelNames:["measure"],registers:[this.registry],collect:()=>{this.ledgerQueue.set({measure:"depth"},Fo()),this.ledgerQueue.set({measure:"oldest_age_seconds"},Bo()/1e3)}}),this.labelCardinality=new ye.Gauge({name:"urun_inference_label_cardinality",help:"Distinct values admitted for a bounded metric label. At its ceiling, further values report as (other).",labelNames:["label"],registers:[this.registry],collect:()=>{this.labelCardinality.set({label:"model"},this.modelLabel.size),this.labelCardinality.set({label:"org"},this.orgLabel.size)}}),this.labelOverflows=new ye.Counter({name:"urun_inference_label_overflows_total",help:"Observations that would have taken a new label value but found the budget spent. Non-zero means a metric dimension is now partly blind.",labelNames:["label"],registers:[this.registry]}),e.defaultMetrics!==!1&&(0,ye.collectDefaultMetrics)({register:this.registry,prefix:"urun_inference_proxy_"})}observeLedger(e){this.ledgerWrites.inc({outcome:e.outcome,reason:e.reason})}observe(e){let n=Su(e.path),r=e.status!==null&&!(e.status>=400&&e.status<500),o=this.modelLabel.overflowCount+this.orgLabel.overflowCount,s=this.modelLabel.of(e.model,r),i=this.orgLabel.of(e.org,r);this.modelLabel.overflowCount+this.orgLabel.overflowCount-o>0&&(this.modelLabel.overflowCount>o?this.labelOverflows.inc({label:"model"}):this.labelOverflows.inc({label:"org"}));let u=e.stream===!0?"true":"false";this.requests.inc({lane:n,method:e.method===""?rn:e.method,status_class:Ru(e.status),stream:u,model:s,org:i}),this.duration.observe({lane:n,stream:u,model:s,org:i},e.duration_ms/1e3),e.ttft_ms!==null&&this.ttft.observe({lane:n,model:s,org:i},e.ttft_ms/1e3),e.stream_error&&this.streamErrors.inc({lane:n,model:s}),e.delivered||this.undelivered.inc({lane:n,model:s})}scrape(){return this.registry.metrics()}get contentType(){return this.registry.contentType}};function ss(t){return(0,os.createServer)((e,n)=>{(async()=>{let r=(e.url??"/").split("?")[0];if(e.method!=="GET"||r!==nn){n.writeHead(404,{"content-type":"text/plain"}),n.end(`no route for ${e.method} ${r} \u2014 this port serves only GET ${nn}
|
|
9
9
|
`);return}let o=await t.scrape();n.writeHead(200,{"content-type":t.contentType}),n.end(o)})().catch(r=>{if(!n.headersSent){n.writeHead(500,{"content-type":"text/plain"}),n.end(`${String(r instanceof Error?r.message:r)}
|
|
10
|
-
`);return}n.end()})})}
|
|
11
|
-
`)}var Ha={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},qe="urun.videoOut";function Fo(t){return{framing:$o,codec:t}}function Bo(t){let{seq:e,ptsUs:n,codec:r,key:o}=t.header;return{[qe]:{v:1,seq:e,ptsUs:n,codec:r,key:o,data:Buffer.from(t.au).toString("base64")}}}var Ga={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},Va=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",qe],Uo=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function Wa(t){let e=te(t);if(!e)throw new x("setup.realtimeInputConfig must be an object");for(let o of Object.keys(e))if(!(o==="automaticActivityDetection"||o==="activityHandling"))throw new x(o==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${o}"`);let n=te(e.automaticActivityDetection);if(e.automaticActivityDetection!==void 0&&!n)throw new x("realtimeInputConfig.automaticActivityDetection must be an object");for(let o of Object.keys(n??{}))if(o!=="disabled")throw new x(`automaticActivityDetection.${o} tunes AUTOMATIC (server-side) activity detection \u2014 the declared platform follow-up (serve turn_detection events over the session stream lane); only "disabled: true" (manual activity mode) is supported in this slice`);if(n?.disabled!==!0)throw new x("automatic (server-side) activity detection is the declared platform follow-up (serve turn_detection turn_start/turn_end events over the session stream lane, not proxy DSP) \u2014 set realtimeInputConfig.automaticActivityDetection.disabled: true for manual activity mode");let r="START_OF_ACTIVITY_INTERRUPTS";if(e.activityHandling!==void 0){if(!Uo.includes(e.activityHandling))throw new x(`unsupported activityHandling ${JSON.stringify(e.activityHandling)} \u2014 expected one of ${Uo.join(", ")}`);r=e.activityHandling}return{manualActivity:!0,activityHandling:r}}var Ja=["temperature","maxOutputTokens","responseModalities"];function jo(t){for(let[c,y]of Object.entries(Ga))if(t[c]!==void 0)throw new x(y);for(let c of Object.keys(t))if(!Va.includes(c))throw new x(`unsupported setup field "${c}"`);if(typeof t.model!="string"||t.model.length===0)throw new x('setup.model is required (format "models/{model}")');let e=t.model.replace(/^models\//,""),n,r,o=!1,i=te(t.generationConfig);if(t.generationConfig!==void 0&&!i)throw new x("setup.generationConfig must be an object");if(i){for(let c of Object.keys(i))if(!Ja.includes(c))throw new x(`unsupported generationConfig field "${c}"`);if(i.temperature!==void 0){if(typeof i.temperature!="number")throw new x("generationConfig.temperature must be a number");n=i.temperature}if(i.maxOutputTokens!==void 0){if(typeof i.maxOutputTokens!="number")throw new x("generationConfig.maxOutputTokens must be a number");r=i.maxOutputTokens}if(i.responseModalities!==void 0){let c=Array.isArray(i.responseModalities)?i.responseModalities:[i.responseModalities];for(let y of c){let g=String(y).toUpperCase();if(g==="AUDIO"){o=!0;continue}if(g!=="TEXT")throw new x(`responseModalities ${JSON.stringify(y)} is not supported \u2014 this surface speaks TEXT and AUDIO (audio-out rides the native AudioBridge 'audio' lane)`)}}}let s=[];t.systemInstruction!==void 0&&s.push({role:"system",content:Do(t.systemInstruction,"setup.systemInstruction")});let a;if(t.tools!==void 0){if(!Array.isArray(t.tools))throw new x("setup.tools must be an array");a=[];for(let c of t.tools){let y=te(c);if(!y)throw new x("setup.tools entries must be objects");for(let g of Object.keys(y))if(g!=="functionDeclarations")throw new x(`unsupported tool "${g}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(y.functionDeclarations))throw new x("tool.functionDeclarations must be an array");for(let g of y.functionDeclarations){let E=te(g);if(!E||typeof E.name!="string")throw new x("functionDeclarations entries require a string name");a.push({type:"function",name:E.name,description:E.description,parameters:E.parameters})}}a.length===0&&(a=void 0)}let u={enabled:!1,handle:null};if(t.sessionResumption!==void 0){let c=te(t.sessionResumption);if(!c)throw new x("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let y of Object.keys(c))if(y!=="handle")throw new x(`unsupported sessionResumption field "${y}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(c.handle!==void 0&&(typeof c.handle!="string"||c.handle.length===0))throw new x("sessionResumption.handle must be a non-empty string when present");u={enabled:!0,handle:typeof c.handle=="string"?c.handle:null}}let d=t.realtimeInputConfig===void 0?Ha:Wa(t.realtimeInputConfig),l=!1;if(t[qe]!==void 0){if(!te(t[qe]))throw new x(`setup["${qe}"] must be an object (the v1 opt-in is {})`);l=!0}return{model:e,systemItems:s,tools:a,temperature:n,maxOutputTokens:r,audioModality:o,activity:d,sessionResumption:u,videoOut:l}}function qo(t){return{sessionResumptionUpdate:{newHandle:t,resumable:!0}}}function Ho(t){let e=Math.max(0,t??0)/1e3;return{goAway:{timeLeft:`${Math.round(e*1e3)/1e3}s`}}}function Go(t){for(let r of Object.keys(t))if(r!=="turns"&&r!=="turnComplete")throw new x(`unsupported clientContent field "${r}"`);let e=t.turns===void 0?[]:t.turns;if(!Array.isArray(e))throw new x("clientContent.turns must be an array");let n=[];for(let r of e){let o=te(r);if(!o)throw new x("clientContent.turns entries must be Content objects");let i=o.role===void 0?"user":String(o.role).toLowerCase();if(i!=="user"&&i!=="model")throw new x(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(o.role)})`);n.push({role:i==="model"?"assistant":"user",content:Do(o,"clientContent.turns")})}return{items:n,turnComplete:t.turnComplete===!0}}var Vt=16e3,Vo=24e3,Ka=`audio/pcm;rate=${Vo}`,Wo=Buffer.alloc(960).toString("base64");function za(t,e,n){if(e===n)return t;let r=Math.floor(t.length*n/e),o=new Int16Array(r),i=e/n,s=t.length-1;for(let a=0;a<r;a++){let u=a*i,d=Math.floor(u);if(d>=s){o[a]=t[s];continue}let l=u-d;o[a]=Math.round(t[d]+(t[d+1]-t[d])*l)}return o}function Ya(t){let e=te(t);if(!e)throw new x("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(e.mimeType))throw new x(`realtimeInput.video.mimeType must be image/jpeg (the Live video input stream is individual JPEG frames), got ${JSON.stringify(e.mimeType)}`);if(typeof e.data!="string"||e.data.length===0)throw new x("realtimeInput.video.data must be non-empty base64");let n=Buffer.from(e.data,"base64");if(n.length<3||n[0]!==255||n[1]!==216)throw new x("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(n)}function Xa(t){let e=te(t);if(!e)throw new x("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(e.mimeType))throw new x(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(e.mimeType)}`);let n=/;\s*rate=(\d+)/.exec(e.mimeType);if(n&&Number(n[1])!==Vt)throw new x(`realtimeInput.audio.mimeType declares rate=${n[1]} \u2014 the Live spec input format is ${Vt} Hz (audio/pcm;rate=${Vt})`);if(typeof e.data!="string"||e.data.length===0)throw new x("realtimeInput.audio.data must be non-empty base64");let r=Buffer.from(e.data,"base64");if(r.length===0||r.length%2!==0)throw new x(`realtimeInput.audio.data must decode to whole 16-bit samples (got ${r.length} bytes \u2014 not even)`);let o=new Int16Array(r.length/2);for(let s=0;s<o.length;s++)o[s]=r.readInt16LE(s*2);let i=za(o,Vt,Vo);return Buffer.from(i.buffer,i.byteOffset,i.byteLength).toString("base64")}function Jo(t){if(t.mediaChunks!==void 0)throw new x('realtimeInput.mediaChunks is deprecated by the Live API \u2014 send the audio stream via realtimeInput.audio ({mimeType:"audio/pcm;rate=16000", data}) instead');if(t.activityStart!==void 0||t.activityEnd!==void 0){if(Object.keys(t).length!==1||t.activityStart!==void 0&&t.activityEnd!==void 0)throw new x("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let o=t.activityStart!==void 0?"activityStart":"activityEnd",i=te(t[o]);if(!i||Object.keys(i).length!==0)throw new x(`realtimeInput.${o} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:o}}let e=["text","audio","audioStreamEnd","video"];for(let r of Object.keys(t))if(!e.includes(r))throw new x(`unsupported realtimeInput field "${r}"`);let n=e.filter(r=>t[r]!==void 0);if(n.length!==1)throw new x(`realtimeInput must carry exactly one of ${e.join(", ")} (got ${n.length})`);if(t.audio!==void 0)return{kind:"audio",base64Pcm24k:Xa(t.audio)};if(t.video!==void 0)return{kind:"video",jpegFrame:Ya(t.video)};if(t.audioStreamEnd!==void 0){if(t.audioStreamEnd!==!0)throw new x("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof t.text!="string"||t.text.length===0)throw new x("realtimeInput.text must be a non-empty string");return{kind:"text",text:t.text}}function Ko(t){for(let e of Object.keys(t))if(e!=="functionResponses")throw new x(`unsupported toolResponse field "${e}"`);if(!Array.isArray(t.functionResponses)||t.functionResponses.length===0)throw new x("toolResponse.functionResponses must be a non-empty array");return t.functionResponses.map(e=>{let n=te(e);if(!n||typeof n.id!="string"||n.id.length===0)throw new x("functionResponses entries require the string id matched from the toolCall");return{type:"function_call_output",call_id:n.id,output:JSON.stringify(n.response??null)}})}function zo(t){return t.type!=="response.output_text.delta"||typeof t.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:t.delta}]}}}}function Yo(t){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:Ka,data:t}}]}}}}function Xo(t){let e=te(t)??{},n=Array.isArray(e.output)?e.output:[],r=[],o=[],i=typeof e.output_text=="string"?e.output_text:"";if(!i)for(let d of n){let l=te(d);if(!(l?.type!=="message"||!Array.isArray(l.content)))for(let c of l.content){let y=te(c);y?.type==="output_text"&&typeof y.text=="string"&&(i+=y.text)}}i&&o.push({role:"assistant",content:i});for(let d of n){let l=te(d);if(l?.type!=="function_call")continue;if(typeof l.call_id!="string"||typeof l.name!="string")throw new x(`upstream function_call item is missing call_id/name: ${JSON.stringify(d)}`,Ie);let c=typeof l.arguments=="string"&&l.arguments.length>0?l.arguments:"{}",y;try{y=JSON.parse(c)}catch{throw new x(`upstream function_call arguments are not valid JSON: ${c}`,Ie)}r.push({id:l.call_id,name:l.name,args:y}),o.push({type:"function_call",call_id:l.call_id,name:l.name,arguments:c})}let s=[];r.length>0&&s.push({toolCall:{functionCalls:r}}),s.push({serverContent:{generationComplete:!0}});let a=te(e.usage),u={serverContent:{turnComplete:!0}};if(a){let d={};typeof a.input_tokens=="number"&&(d.promptTokenCount=a.input_tokens),typeof a.output_tokens=="number"&&(d.responseTokenCount=a.output_tokens),typeof a.total_tokens=="number"&&(d.totalTokenCount=a.total_tokens),Object.keys(d).length>0&&(u.usageMetadata=d)}return s.push(u),{messages:s,assistantItems:o}}_();var Qo=require("crypto"),Zo=require("ws");var Qa=256,qn=class{snapshots=new Map;store(e,n,r){for(this.snapshots.set(`${e}\0${n}`,r);this.snapshots.size>Qa;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(e,n){return this.snapshots.get(`${e}\0${n}`)}},Za="local",mt=t=>t.slice(0,120),Hn=class{constructor(e,n,r){this.ws=e;this.call=n;this.registry=r;this.clients=n.clients,this.scope=n.scope,e.on("message",o=>this.onMessage(o)),e.on("close",()=>{this.unsubscribeEnd?.(),this.unsubscribeEnd=null,this.detachAudio?.(),this.detachVideoOut?.(),this.call.release?.().catch(()=>{})})}ws;call;registry;config=null;transcript=[];queue=Promise.resolve();proxyHandle=null;unsubscribeEnd=null;audio=null;detachAudio=null;detachVideoOut=null;video=null;activityOpen=!1;activeTurn=null;scope;clients;send(e){this.ws.readyState===this.ws.OPEN&&this.ws.send(JSON.stringify(e))}fail(e){if(e instanceof re){this.ws.close(Lo,mt(e.message));return}if(e instanceof le){this.ws.close(Jt,mt(e.message));return}if(e instanceof x){this.ws.close(e.closeCode,mt(e.message));return}this.ws.close(Ie,mt(String(e instanceof Error?e.message:e)))}onMessage(e){try{let n=No(typeof e=="string"?e:Buffer.concat(Array.isArray(e)?e:[e]).toString("utf8"));if(n.kind==="setup"){this.handleSetup(n.setup).catch(o=>this.fail(o));return}if(!this.config)throw new x(`"${n.kind}" before setup \u2014 the first client message must be BidiGenerateContentSetup`);let r=this.config;if(n.kind==="clientContent"){let{items:o,turnComplete:i}=Go(n.clientContent);this.transcript.push(...o),i&&this.enqueue(()=>this.runTurn());return}if(n.kind==="realtimeInput"){let o=Jo(n.realtimeInput);if(o.kind==="activityStart"||o.kind==="activityEnd"){this.onActivity(o.kind,r);return}if(o.kind==="text"){this.transcript.push({role:"user",content:o.text}),this.enqueue(()=>this.runTurn());return}if(o.kind==="video"){this.appendVideoFrame(o.jpegFrame);return}this.appendAudio(o.kind==="audio"?o.base64Pcm24k:Wo);return}this.transcript.push(...Ko(n.toolResponse)),this.enqueue(()=>this.runTurn())}catch(n){this.fail(n)}}async handleSetup(e){if(this.config)throw new x("setup may only be sent once, as the first client message");let n=jo(e),r=n.sessionResumption.handle;if(r!==null){let i=this.registry.get(this.scope,r);if(!i)throw new x("unknown sessionResumption.handle \u2014 the Live session it names is gone (proxy restarted, handle evicted, or never issued) and cannot be resumed; reconnect without a handle to start a new session",Jt);if(i.model!==n.model)throw new x(`sessionResumption.handle was issued for model "${i.model}" \u2014 the model cannot change on resume (got "${n.model}", per the Live API session-resumption contract)`);if(n.systemItems.length>0)throw new x("setup.systemInstruction cannot be changed on resume \u2014 it is part of the resumed conversation state");if(this.call.clientsForResume!==void 0&&(this.clients=await this.call.clientsForResume(r)),this.proxyHandle=await this.clients.sessionHandle(n.model),this.proxyHandle!==i.proxyHandle)throw new x(`sessionResumption.handle named pooled session "${i.proxyHandle}" but the re-attach landed on "${this.proxyHandle}" \u2014 the conversation's native session changed and the snapshot cannot be adopted; reconnect without a handle to start a new session`,Jt);this.unsubscribeEnd=await this.clients.onSessionEnd(i.proxyHandle,s=>this.enqueue(()=>this.handleUpstreamEnd(s))),this.transcript=[...i.transcript]}else this.proxyHandle=await this.clients.sessionHandle(n.model),this.unsubscribeEnd=await this.clients.onSessionEnd(this.proxyHandle,i=>this.enqueue(()=>this.handleUpstreamEnd(i))),this.transcript.push(...n.systemItems);this.config=n;let o={};if(n.videoOut){let i=this.clients.openVideoOut;if(i){let s=await i.call(this.clients,n.model),a=s.onOutputFrame(d=>this.send(Bo(d))),u=s.onLaneError(d=>this.fail(d));this.detachVideoOut=()=>{a(),u()},o[qe]=Fo(s.codec)}}this.send({setupComplete:o}),n.audioModality&&this.ensureAudioLane(),n.sessionResumption.enabled&&await this.mintResumptionUpdate()}ensureAudioLane(){if(this.audio)return this.audio;let e=this.config;if(!e)throw new x("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openAudio;if(!n)throw new x("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native AudioBridge seam over the canonical `audio` lane)",Ie);return this.audio=(async()=>{let r=await n.call(this.clients,e.model);return this.detachAudio=r.onOutputAudio(o=>this.send(Yo(o))),r})(),this.audio.catch(r=>this.fail(r)),this.audio}appendAudio(e){this.ensureAudioLane().then(n=>n.appendInputAudio(e)).catch(n=>this.fail(n))}ensureVideoLane(){if(this.video)return this.video;let e=this.config;if(!e)throw new x("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openVideo;if(!n)throw new x("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",Ie);return this.video=Promise.resolve(n.call(this.clients,e.model)),this.video.catch(r=>this.fail(r)),this.video}appendVideoFrame(e){this.ensureVideoLane().then(n=>n.sendInputFrame(e)).catch(n=>this.fail(n))}onActivity(e,n){if(!n.activity.manualActivity)throw new x(`realtimeInput.${e} requires manual activity mode \u2014 set setup.realtimeInputConfig.automaticActivityDetection.disabled: true (automatic server-side activity detection is the declared platform follow-up)`);if(e==="activityStart"){if(this.activityOpen)throw new x("activityStart while an activity window is already open (send activityEnd first)");this.activityOpen=!0,n.activity.activityHandling==="START_OF_ACTIVITY_INTERRUPTS"&&this.activeTurn&&(this.activeTurn.interrupted=!0);return}if(!this.activityOpen)throw new x("activityEnd without an open activity window (send activityStart first)");this.activityOpen=!1}enqueue(e){this.queue=this.queue.then(e,()=>{}),this.queue=this.queue.catch(n=>this.fail(n))}async mintResumptionUpdate(){let e=this.config;if(!e?.sessionResumption.enabled||this.ws.readyState!==this.ws.OPEN)return;let n=await this.clients.sessionHandle(e.model);this.proxyHandle=n;let r=(0,Qo.randomUUID)();this.registry.store(this.scope,r,{model:e.model,proxyHandle:n,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(r),this.send(qo(r))}async handleUpstreamEnd(e){if(this.ws.readyState!==this.ws.OPEN||!this.config)return;this.unsubscribeEnd?.(),this.unsubscribeEnd=null;let n=null;try{n=await this.clients.sessionHandle(this.config.model)}catch{}if(n!==null&&n!==this.proxyHandle){this.proxyHandle=n,this.unsubscribeEnd=await this.clients.onSessionEnd(n,r=>this.enqueue(()=>this.handleUpstreamEnd(r))),this.config.sessionResumption.enabled&&await this.mintResumptionUpdate();return}this.send(Ho(e.timeLeftMs)),this.ws.close(Ie,mt(`upstream session ended: ${e.reason}`))}async runTurn(){let e=this.config;if(!e||this.ws.readyState!==this.ws.OPEN)return;let n={interrupted:!1};this.activeTurn=n;try{let r=await this.clients.createResponse({model:e.model,input:[...this.transcript],stream:!0,tools:e.tools,temperature:e.temperature,max_output_tokens:e.maxOutputTokens}),o,i=!1;for await(let u of r){if(n.interrupted)break;let d=u;if(d.type==="error")throw new x(`upstream error: ${JSON.stringify(d)}`,Ie);if(d.type==="response.completed"){o=d.response,i=!0;continue}let l=zo(d);l&&this.send(l)}if(n.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!i)throw new x("upstream produced no response.completed event",Ie);let{messages:s,assistantItems:a}=Xo(o);this.transcript.push(...a);for(let u of s)this.send(u);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},He=class extends Error{constructor(n,r){super(r);this.status=n;this.name="UpgradeAuthError"}status};function eu(t,e,n){let r=new Set;(u=>{if(typeof u=="string")u!==""&&r.add(u);else if(Array.isArray(u))for(let d of u)d!==""&&r.add(d)})(t.headers["x-goog-api-key"]);let i=t.headers.authorization;typeof i=="string"&&i.startsWith("Bearer ")&&i.length>7&&r.add(i.slice(7));let s=e.searchParams.get("key")??void 0;if(s!==void 0&&s!==""&&r.add(s),r.size>1)throw new He(401,`contradictory credentials: ${r.size} distinct keys presented across the x-goog-api-key/Authorization headers and the ?key= query parameter \u2014 present exactly one`);if(n&&s!==void 0&&s!=="")throw new He(401,"the ?key= query parameter is not accepted on the hosted endpoint \u2014 send the org API key in the x-goog-api-key header or as Authorization: Bearer");let a;for(let u of r)a=u;if(a===void 0&&n)throw new He(401,"missing credentials: send the org API key in the x-goog-api-key header or as Authorization: Bearer");return a??""}function Gn(t,e){let n=e.authenticate!==void 0;if(n&&(e.clients!==void 0||e.apiKey!==void 0))throw new TypeError("attachGeminiLive: authenticate (hosted per-request resolution) is mutually exclusive with a fixed clients/apiKey lane");if(!n&&e.clients===void 0)throw new TypeError("attachGeminiLive: pass fixed `clients` (local lane) or `authenticate` (hosted lane)");let r=new Zo.WebSocketServer({noServer:!0}),o=new qn;t.on("upgrade",(s,a,u)=>{let d=new URL(s.url??"/","http://localhost");d.pathname===Wt&&(a.on("error",()=>{}),(async()=>{let l=eu(s,d,n),c;if(e.authenticate)c=await e.authenticate(l,s);else{if(e.apiKey!==void 0&&l!==e.apiKey)throw new He(401,"invalid local proxy api key");c={clients:e.clients,scope:Za}}r.handleUpgrade(s,a,u,y=>{new Hn(y,c,o)})})().catch(l=>{let c=502;(l instanceof He||typeof l=="object"&&l!==null&&"status"in l&&typeof l.status=="number")&&(c=l.status);let y=c===401?"Unauthorized":c===403?"Forbidden":c===502?"Bad Gateway":"Internal Server Error",g=l instanceof Error?l.message:String(l);a.destroyed||(a.write(`HTTP/1.1 ${c} ${y}\r
|
|
10
|
+
`);return}n.end()})})}k();var pr=require("crypto"),as=require("http"),fr=9465,xu="x-urun-key-id",Eu="x-urun-key-fingerprint",Ou="urun-ratelimit-bucket:",Au="urun-ratelimit-tier:",is="anonymous",Iu=/^urun_sk_[A-Za-z0-9_-]{43}$/;function Tu(t){return(0,pr.createHash)("sha256").update(`${Ou}${t}`).digest("hex")}function Pu(t){return(0,pr.createHash)("sha256").update(`${Au}${t}`).digest("hex")}function Cu(t){let e=/^Bearer[ \t]+(\S.*)$/i.exec((t??"").trim());if(!e)return null;let n=e[1].trim();return Iu.test(n)?n:null}function us(){return(0,as.createServer)((t,e)=>{let n=Cu(t.headers.authorization);e.setHeader(xu,n===null?is:Tu(n)),e.setHeader(Eu,n===null?is:Pu(n)),e.writeHead(200),e.end()})}k();var $u=8080,an="serve",Ze="0.0.0.0";function Mu(t,e,n){let r=(t[e]??"").trim();if(!r)throw new Error(`${e} is not set \u2014 ${n}`);return r}function ds(t=process.env){let e=(t.PORT??"").trim(),n=e===""?$u:Number(e);if(!Number.isInteger(n)||n<0||n>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let r=Co(Xt(Mu(t,"URUN_BASE_URL","the hosted proxy needs the uRun session-gateway base it opens sessions and verifies API keys against (e.g. https://api.urun.sh)"),"URUN_BASE_URL")),o=(t.URUN_API_URL??"").trim(),s=o?Xt(o,"URUN_API_URL"):`${r.replace(/\/+$/,"")}/v1`,i=(t.URUN_CATALOG_URL??"").trim(),a=(t.URUN_CATALOG_ANON_KEY??"").trim();if(!!i!=!!a)throw new Error("URUN_CATALOG_URL and URUN_CATALOG_ANON_KEY must be set together (or both left unset) \u2014 exactly one of them is set, which would silently disable the model-catalog oracle");let u=i?{catalogUrl:Xt(i,"URUN_CATALOG_URL"),anonKey:a}:null,d=(t.URUN_SESSION_TOKEN_SECRET??"").trim(),l=(t.URUN_SHARED_ORG_API_KEY??"").trim(),p=(t.URUN_SHARED_ORG_ID??"").trim();if(!!l!=!!p)throw new Error("URUN_SHARED_ORG_API_KEY and URUN_SHARED_ORG_ID must be set together (or both left unset) \u2014 exactly one of them is set, which would silently leave the shared endpoints on the session lane");return{port:n,baseUrl:r,apiUrl:s,catalog:u,decisionSecret:d,sharedOrgKey:l,sharedOrgId:p}}k();var Yi=require("http");k();k();var ps="audio",mr=class{constructor(e,n){this.backend=e;this.opts=n}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(e){if(!this.source)throw new Error("startOutbound() not called");let n=Buffer.from(e,"base64"),r=Math.floor(n.byteLength/2),o=new Int16Array(r);for(let s=0;s<r;s++)o[s]=n.readInt16LE(s*2);this.source.onData(o)}async startInbound(e){if(e==null)throw new Error('startInbound: no downstream audio track \u2014 the runtime audio producer has not been consumed yet (session.stream("audio").track is null)');this.sink=this.backend.createSink(e),this.sink.onframe=n=>{let r=Buffer.from(n.samples.buffer,n.samples.byteOffset,n.samples.byteLength).toString("base64");for(let o of this.outHandlers)o(r)}}onOutputAudio(e){return this.outHandlers.add(e),()=>this.outHandlers.delete(e)}};async function fs(t,e,n=1e4){let r=new mr(e,{sampleRate:24e3}),o=await r.startOutbound();await t.stream(ps).attach(o);let s=await Lu(t,n);return await r.startInbound(s),r}function Lu(t,e){let n=t.stream(ps);return n.track!=null?Promise.resolve(n.track):new Promise((r,o)=>{let s,i=setTimeout(()=>{s?.(),o(new Error(`enableSessionAudio: no downstream audio track within ${e}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},e),a=u=>{u!=null&&(clearTimeout(i),s?.(),r(u))};s=n.on?.("track",a),n.track!=null&&a(n.track)})}var ls="rt-video-in",cs=256e3;function ms(t){let e=t.stream(ls),n=e.emit;if(typeof n!="function")throw new Error(`enableSessionVideo: session.stream('${ls}') has no emit() \u2014 the named-DATA stream produce seam (@urun-sh/core SessionStream.emit, contract \xA75) is required for the video frame lane`);return{sendInputFrame:async r=>{if(r.byteLength===0)throw new Error("enableSessionVideo: refusing to send an empty video frame");if(r.byteLength>cs)throw new Error(`enableSessionVideo: frame is ${r.byteLength} bytes \u2014 over the ${cs}-byte data-channel budget (the SFU's 262144-byte SCTP ceiling drops oversize messages SILENTLY; send smaller/lower-quality frames)`);await n.call(e,r)}}}var Uu=24e3,Tt=480,Nu=111;async function Du(){let t=await import("opusscript"),e=t.default??t;return new e(Uu,1,2048)}async function gs(){let t=await import("werift"),e=await Du();return{createSource:()=>{let n=new t.MediaStreamTrack({kind:"audio"}),r=Math.random()*4294967295>>>0,o=Math.random()*65535&65535,s=Math.random()*4294967295>>>0,i=new Int16Array(0);return{track:n,onData:a=>{let u=new Int16Array(i.length+a.length);u.set(i,0),u.set(a,i.length);let d=0;for(;u.length-d>=Tt;){let l=u.subarray(d,d+Tt);d+=Tt;let p=Buffer.from(l.buffer,l.byteOffset,l.byteLength),y=e.encode(p,Tt);o=o+1&65535,s=s+Tt>>>0;let c=new t.RtpHeader({version:2,payloadType:Nu,sequenceNumber:o,timestamp:s,ssrc:r,marker:!1}),m=new t.RtpPacket(c,y);n.writeRtp(m)}i=u.slice(d)}}},createSink:n=>{let r={onframe:null};return n.onReceiveRtp.subscribe(s=>{let i;try{i=e.decode(s.payload)}catch{return}let a=new Int16Array(i.length/2);for(let u=0;u<a.length;u++)a[u]=i.readInt16LE(u*2);r.onframe?.({samples:a})}),r}}}k();var un=require("@urun-sh/core"),gr="rt-video-out";var Fu="h264";function Bu(t,e){if(!Number.isInteger(t.seq)||t.seq<0)throw new Error(`${e}: header seq must be a non-negative integer (got ${t.seq})`);if(!Number.isInteger(t.ptsUs))throw new Error(`${e}: header ptsUs must be an integer microsecond timestamp (got ${t.ptsUs})`);if(typeof t.codec!="string"||t.codec.length===0)throw new Error(`${e}: header codec must be a non-empty string`);if(typeof t.key!="boolean")throw new Error(`${e}: header key must be a boolean`)}function ju(t){if(t.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${t.byteLength} byte(s), no header length)`);let e=t[0]|t[1]<<8;if(t.byteLength<2+e)throw new Error(`decodeVideoOutRecord: truncated record (header says ${e} bytes, ${t.byteLength-2} present)`);let n;try{n=JSON.parse(new TextDecoder().decode(t.subarray(2,2+e)))}catch(i){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(i)})`)}let r=n;if(r===null||typeof r!="object"||r.v!==1)throw new Error(`decodeVideoOutRecord: header is not spec v1 (v=${JSON.stringify(r?.v)}) \u2014 a newer producer must be met with a newer consumer, never silently misread`);let o=r;Bu(o,"decodeVideoOutRecord");let s=t.subarray(2+e);if(s.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:o,au:s.slice()}}var hs="urun.videoOut.v2-ws";function ys(t){let e=t.stream(gr);if(typeof e.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${gr}') has no messages() \u2014 the named-DATA stream consume seam (@urun-sh/core SessionStream.messages, contract \xA75) is required for the video OUT lane`);let n=new Set,r=new Set,o=new Set,s=u=>{for(let d of r)d(u)},i=!1,a=async()=>{for await(let u of e.messages()){if(!(u instanceof Uint8Array)){s(new Error(`openSessionVideoOutLane: "${gr}" delivered a non-BINARY \xA75 payload (${typeof u}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let d;try{d=ju(u)}catch(l){s(l instanceof Error?l:new Error(String(l)));continue}for(let l of n)l(d)}for(let u of o)u()};return{codec:Fu,onOutputFrame:u=>(n.add(u),i||(i=!0,a().catch(d=>s(d instanceof Error?d:new Error(String(d))))),()=>n.delete(u)),onLaneError:u=>(r.add(u),()=>r.delete(u)),onEnd:u=>(o.add(u),()=>o.delete(u))}}var pn="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",qu=1007,_s=1008,qe=1011,fn=1008,A=class extends Error{constructor(n,r=qu){super(n);this.closeCode=r}closeCode},oe=t=>typeof t=="object"&&t!==null&&!Array.isArray(t)?t:null,dn=["setup","clientContent","realtimeInput","toolResponse"];function vs(t){let e;try{e=JSON.parse(t)}catch{throw new A("client message is not valid JSON")}let n=oe(e);if(!n)throw new A("client message must be a JSON object");let r=dn.filter(a=>n[a]!==void 0),o=Object.keys(n).filter(a=>!dn.includes(a));if(o.length>0)throw new A(`unknown client message field(s) ${JSON.stringify(o)} \u2014 expected exactly one of ${dn.join(", ")}`);if(r.length!==1)throw new A(`client message must contain exactly one of ${dn.join(", ")} (got ${r.length})`);let s=r[0],i=oe(n[s]);if(!i)throw new A(`"${s}" must be a JSON object`);return{kind:s,[s]:i}}function bs(t,e){if(typeof t=="string")return t;let n=oe(t),r=n?n.parts:void 0;if(!Array.isArray(r))throw new A(`${e}: expected a Content with a parts array`);let o=[];for(let s of r){let i=oe(s);if(i&&typeof i.text=="string"){o.push(i.text);continue}throw i&&(i.inlineData!==void 0||i.fileData!==void 0)?new A(`${e}: media parts (inlineData/fileData) are a declared follow-up slice (audio/video lanes over the proxy transport AudioBridge) \u2014 only text parts are supported in this slice`):i&&(i.functionCall!==void 0||i.functionResponse!==void 0)?new A(`${e}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new A(`${e}: unsupported part ${JSON.stringify(s)}`)}return o.join(`
|
|
11
|
+
`)}var Hu={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},et="urun.videoOut";function ks(t){return{framing:hs,codec:t}}function Ss(t){let{seq:e,ptsUs:n,codec:r,key:o}=t.header;return{[et]:{v:1,seq:e,ptsUs:n,codec:r,key:o,data:Buffer.from(t.au).toString("base64")}}}var Gu={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},Vu=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",et],ws=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function Wu(t){let e=oe(t);if(!e)throw new A("setup.realtimeInputConfig must be an object");for(let o of Object.keys(e))if(!(o==="automaticActivityDetection"||o==="activityHandling"))throw new A(o==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${o}"`);let n=oe(e.automaticActivityDetection);if(e.automaticActivityDetection!==void 0&&!n)throw new A("realtimeInputConfig.automaticActivityDetection must be an object");for(let o of Object.keys(n??{}))if(o!=="disabled")throw new A(`automaticActivityDetection.${o} tunes AUTOMATIC (server-side) activity detection \u2014 the declared platform follow-up (serve turn_detection events over the session stream lane); only "disabled: true" (manual activity mode) is supported in this slice`);if(n?.disabled!==!0)throw new A("automatic (server-side) activity detection is the declared platform follow-up (serve turn_detection turn_start/turn_end events over the session stream lane, not proxy DSP) \u2014 set realtimeInputConfig.automaticActivityDetection.disabled: true for manual activity mode");let r="START_OF_ACTIVITY_INTERRUPTS";if(e.activityHandling!==void 0){if(!ws.includes(e.activityHandling))throw new A(`unsupported activityHandling ${JSON.stringify(e.activityHandling)} \u2014 expected one of ${ws.join(", ")}`);r=e.activityHandling}return{manualActivity:!0,activityHandling:r}}var Ju=["temperature","maxOutputTokens","responseModalities"];function Rs(t){for(let[p,y]of Object.entries(Gu))if(t[p]!==void 0)throw new A(y);for(let p of Object.keys(t))if(!Vu.includes(p))throw new A(`unsupported setup field "${p}"`);if(typeof t.model!="string"||t.model.length===0)throw new A('setup.model is required (format "models/{model}")');let e=t.model.replace(/^models\//,""),n,r,o=!1,s=oe(t.generationConfig);if(t.generationConfig!==void 0&&!s)throw new A("setup.generationConfig must be an object");if(s){for(let p of Object.keys(s))if(!Ju.includes(p))throw new A(`unsupported generationConfig field "${p}"`);if(s.temperature!==void 0){if(typeof s.temperature!="number")throw new A("generationConfig.temperature must be a number");n=s.temperature}if(s.maxOutputTokens!==void 0){if(typeof s.maxOutputTokens!="number")throw new A("generationConfig.maxOutputTokens must be a number");r=s.maxOutputTokens}if(s.responseModalities!==void 0){let p=Array.isArray(s.responseModalities)?s.responseModalities:[s.responseModalities];for(let y of p){let c=String(y).toUpperCase();if(c==="AUDIO"){o=!0;continue}if(c!=="TEXT")throw new A(`responseModalities ${JSON.stringify(y)} is not supported \u2014 this surface speaks TEXT and AUDIO (audio-out rides the native AudioBridge 'audio' lane)`)}}}let i=[];t.systemInstruction!==void 0&&i.push({role:"system",content:bs(t.systemInstruction,"setup.systemInstruction")});let a;if(t.tools!==void 0){if(!Array.isArray(t.tools))throw new A("setup.tools must be an array");a=[];for(let p of t.tools){let y=oe(p);if(!y)throw new A("setup.tools entries must be objects");for(let c of Object.keys(y))if(c!=="functionDeclarations")throw new A(`unsupported tool "${c}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(y.functionDeclarations))throw new A("tool.functionDeclarations must be an array");for(let c of y.functionDeclarations){let m=oe(c);if(!m||typeof m.name!="string")throw new A("functionDeclarations entries require a string name");a.push({type:"function",name:m.name,description:m.description,parameters:m.parameters})}}a.length===0&&(a=void 0)}let u={enabled:!1,handle:null};if(t.sessionResumption!==void 0){let p=oe(t.sessionResumption);if(!p)throw new A("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let y of Object.keys(p))if(y!=="handle")throw new A(`unsupported sessionResumption field "${y}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(p.handle!==void 0&&(typeof p.handle!="string"||p.handle.length===0))throw new A("sessionResumption.handle must be a non-empty string when present");u={enabled:!0,handle:typeof p.handle=="string"?p.handle:null}}let d=t.realtimeInputConfig===void 0?Hu:Wu(t.realtimeInputConfig),l=!1;if(t[et]!==void 0){if(!oe(t[et]))throw new A(`setup["${et}"] must be an object (the v1 opt-in is {})`);l=!0}return{model:e,systemItems:i,tools:a,temperature:n,maxOutputTokens:r,audioModality:o,activity:d,sessionResumption:u,videoOut:l}}function xs(t){return{sessionResumptionUpdate:{newHandle:t,resumable:!0}}}function Es(t){let e=Math.max(0,t??0)/1e3;return{goAway:{timeLeft:`${Math.round(e*1e3)/1e3}s`}}}function Os(t){for(let r of Object.keys(t))if(r!=="turns"&&r!=="turnComplete")throw new A(`unsupported clientContent field "${r}"`);let e=t.turns===void 0?[]:t.turns;if(!Array.isArray(e))throw new A("clientContent.turns must be an array");let n=[];for(let r of e){let o=oe(r);if(!o)throw new A("clientContent.turns entries must be Content objects");let s=o.role===void 0?"user":String(o.role).toLowerCase();if(s!=="user"&&s!=="model")throw new A(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(o.role)})`);n.push({role:s==="model"?"assistant":"user",content:bs(o,"clientContent.turns")})}return{items:n,turnComplete:t.turnComplete===!0}}var ln=16e3,As=24e3,Ku=`audio/pcm;rate=${As}`,Is=Buffer.alloc(960).toString("base64");function zu(t,e,n){if(e===n)return t;let r=Math.floor(t.length*n/e),o=new Int16Array(r),s=e/n,i=t.length-1;for(let a=0;a<r;a++){let u=a*s,d=Math.floor(u);if(d>=i){o[a]=t[i];continue}let l=u-d;o[a]=Math.round(t[d]+(t[d+1]-t[d])*l)}return o}function Yu(t){let e=oe(t);if(!e)throw new A("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(e.mimeType))throw new A(`realtimeInput.video.mimeType must be image/jpeg (the Live video input stream is individual JPEG frames), got ${JSON.stringify(e.mimeType)}`);if(typeof e.data!="string"||e.data.length===0)throw new A("realtimeInput.video.data must be non-empty base64");let n=Buffer.from(e.data,"base64");if(n.length<3||n[0]!==255||n[1]!==216)throw new A("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(n)}function Xu(t){let e=oe(t);if(!e)throw new A("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(e.mimeType))throw new A(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(e.mimeType)}`);let n=/;\s*rate=(\d+)/.exec(e.mimeType);if(n&&Number(n[1])!==ln)throw new A(`realtimeInput.audio.mimeType declares rate=${n[1]} \u2014 the Live spec input format is ${ln} Hz (audio/pcm;rate=${ln})`);if(typeof e.data!="string"||e.data.length===0)throw new A("realtimeInput.audio.data must be non-empty base64");let r=Buffer.from(e.data,"base64");if(r.length===0||r.length%2!==0)throw new A(`realtimeInput.audio.data must decode to whole 16-bit samples (got ${r.length} bytes \u2014 not even)`);let o=new Int16Array(r.length/2);for(let i=0;i<o.length;i++)o[i]=r.readInt16LE(i*2);let s=zu(o,ln,As);return Buffer.from(s.buffer,s.byteOffset,s.byteLength).toString("base64")}function Ts(t){if(t.mediaChunks!==void 0)throw new A('realtimeInput.mediaChunks is deprecated by the Live API \u2014 send the audio stream via realtimeInput.audio ({mimeType:"audio/pcm;rate=16000", data}) instead');if(t.activityStart!==void 0||t.activityEnd!==void 0){if(Object.keys(t).length!==1||t.activityStart!==void 0&&t.activityEnd!==void 0)throw new A("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let o=t.activityStart!==void 0?"activityStart":"activityEnd",s=oe(t[o]);if(!s||Object.keys(s).length!==0)throw new A(`realtimeInput.${o} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:o}}let e=["text","audio","audioStreamEnd","video"];for(let r of Object.keys(t))if(!e.includes(r))throw new A(`unsupported realtimeInput field "${r}"`);let n=e.filter(r=>t[r]!==void 0);if(n.length!==1)throw new A(`realtimeInput must carry exactly one of ${e.join(", ")} (got ${n.length})`);if(t.audio!==void 0)return{kind:"audio",base64Pcm24k:Xu(t.audio)};if(t.video!==void 0)return{kind:"video",jpegFrame:Yu(t.video)};if(t.audioStreamEnd!==void 0){if(t.audioStreamEnd!==!0)throw new A("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof t.text!="string"||t.text.length===0)throw new A("realtimeInput.text must be a non-empty string");return{kind:"text",text:t.text}}function Ps(t){for(let e of Object.keys(t))if(e!=="functionResponses")throw new A(`unsupported toolResponse field "${e}"`);if(!Array.isArray(t.functionResponses)||t.functionResponses.length===0)throw new A("toolResponse.functionResponses must be a non-empty array");return t.functionResponses.map(e=>{let n=oe(e);if(!n||typeof n.id!="string"||n.id.length===0)throw new A("functionResponses entries require the string id matched from the toolCall");return{type:"function_call_output",call_id:n.id,output:JSON.stringify(n.response??null)}})}function Cs(t){return t.type!=="response.output_text.delta"||typeof t.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:t.delta}]}}}}function $s(t){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:Ku,data:t}}]}}}}function Ms(t){let e=oe(t)??{},n=Array.isArray(e.output)?e.output:[],r=[],o=[],s=typeof e.output_text=="string"?e.output_text:"";if(!s)for(let d of n){let l=oe(d);if(!(l?.type!=="message"||!Array.isArray(l.content)))for(let p of l.content){let y=oe(p);y?.type==="output_text"&&typeof y.text=="string"&&(s+=y.text)}}s&&o.push({role:"assistant",content:s});for(let d of n){let l=oe(d);if(l?.type!=="function_call")continue;if(typeof l.call_id!="string"||typeof l.name!="string")throw new A(`upstream function_call item is missing call_id/name: ${JSON.stringify(d)}`,qe);let p=typeof l.arguments=="string"&&l.arguments.length>0?l.arguments:"{}",y;try{y=JSON.parse(p)}catch{throw new A(`upstream function_call arguments are not valid JSON: ${p}`,qe)}r.push({id:l.call_id,name:l.name,args:y}),o.push({type:"function_call",call_id:l.call_id,name:l.name,arguments:p})}let i=[];r.length>0&&i.push({toolCall:{functionCalls:r}}),i.push({serverContent:{generationComplete:!0}});let a=oe(e.usage),u={serverContent:{turnComplete:!0}};if(a){let d={};typeof a.input_tokens=="number"&&(d.promptTokenCount=a.input_tokens),typeof a.output_tokens=="number"&&(d.responseTokenCount=a.output_tokens),typeof a.total_tokens=="number"&&(d.totalTokenCount=a.total_tokens),Object.keys(d).length>0&&(u.usageMetadata=d)}return i.push(u),{messages:i,assistantItems:o}}k();var Ls=require("crypto"),Us=require("ws");var Qu=256,hr=class{snapshots=new Map;store(e,n,r){for(this.snapshots.set(`${e}\0${n}`,r);this.snapshots.size>Qu;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(e,n){return this.snapshots.get(`${e}\0${n}`)}},Zu="local",Pt=t=>t.slice(0,120),yr=class{constructor(e,n,r){this.ws=e;this.call=n;this.registry=r;this.clients=n.clients,this.scope=n.scope,e.on("message",o=>this.onMessage(o)),e.on("close",()=>{this.unsubscribeEnd?.(),this.unsubscribeEnd=null,this.detachAudio?.(),this.detachVideoOut?.(),this.call.release?.().catch(()=>{})})}ws;call;registry;config=null;transcript=[];queue=Promise.resolve();proxyHandle=null;unsubscribeEnd=null;audio=null;detachAudio=null;detachVideoOut=null;video=null;activityOpen=!1;activeTurn=null;scope;clients;send(e){this.ws.readyState===this.ws.OPEN&&this.ws.send(JSON.stringify(e))}fail(e){if(e instanceof z){this.ws.close(_s,Pt(e.message));return}if(e instanceof pe){this.ws.close(fn,Pt(e.message));return}if(e instanceof A){this.ws.close(e.closeCode,Pt(e.message));return}this.ws.close(qe,Pt(String(e instanceof Error?e.message:e)))}onMessage(e){try{let n=vs(typeof e=="string"?e:Buffer.concat(Array.isArray(e)?e:[e]).toString("utf8"));if(n.kind==="setup"){this.handleSetup(n.setup).catch(o=>this.fail(o));return}if(!this.config)throw new A(`"${n.kind}" before setup \u2014 the first client message must be BidiGenerateContentSetup`);let r=this.config;if(n.kind==="clientContent"){let{items:o,turnComplete:s}=Os(n.clientContent);this.transcript.push(...o),s&&this.enqueue(()=>this.runTurn());return}if(n.kind==="realtimeInput"){let o=Ts(n.realtimeInput);if(o.kind==="activityStart"||o.kind==="activityEnd"){this.onActivity(o.kind,r);return}if(o.kind==="text"){this.transcript.push({role:"user",content:o.text}),this.enqueue(()=>this.runTurn());return}if(o.kind==="video"){this.appendVideoFrame(o.jpegFrame);return}this.appendAudio(o.kind==="audio"?o.base64Pcm24k:Is);return}this.transcript.push(...Ps(n.toolResponse)),this.enqueue(()=>this.runTurn())}catch(n){this.fail(n)}}async handleSetup(e){if(this.config)throw new A("setup may only be sent once, as the first client message");let n=Rs(e),r=n.sessionResumption.handle;if(r!==null){let s=this.registry.get(this.scope,r);if(!s)throw new A("unknown sessionResumption.handle \u2014 the Live session it names is gone (proxy restarted, handle evicted, or never issued) and cannot be resumed; reconnect without a handle to start a new session",fn);if(s.model!==n.model)throw new A(`sessionResumption.handle was issued for model "${s.model}" \u2014 the model cannot change on resume (got "${n.model}", per the Live API session-resumption contract)`);if(n.systemItems.length>0)throw new A("setup.systemInstruction cannot be changed on resume \u2014 it is part of the resumed conversation state");if(this.call.clientsForResume!==void 0&&(this.clients=await this.call.clientsForResume(r)),this.proxyHandle=await this.clients.sessionHandle(n.model),this.proxyHandle!==s.proxyHandle)throw new A(`sessionResumption.handle named pooled session "${s.proxyHandle}" but the re-attach landed on "${this.proxyHandle}" \u2014 the conversation's native session changed and the snapshot cannot be adopted; reconnect without a handle to start a new session`,fn);this.unsubscribeEnd=await this.clients.onSessionEnd(s.proxyHandle,i=>this.enqueue(()=>this.handleUpstreamEnd(i))),this.transcript=[...s.transcript]}else this.proxyHandle=await this.clients.sessionHandle(n.model),this.unsubscribeEnd=await this.clients.onSessionEnd(this.proxyHandle,s=>this.enqueue(()=>this.handleUpstreamEnd(s))),this.transcript.push(...n.systemItems);this.config=n;let o={};if(n.videoOut){let s=this.clients.openVideoOut;if(s){let i=await s.call(this.clients,n.model),a=i.onOutputFrame(d=>this.send(Ss(d))),u=i.onLaneError(d=>this.fail(d));this.detachVideoOut=()=>{a(),u()},o[et]=ks(i.codec)}}this.send({setupComplete:o}),n.audioModality&&this.ensureAudioLane(),n.sessionResumption.enabled&&await this.mintResumptionUpdate()}ensureAudioLane(){if(this.audio)return this.audio;let e=this.config;if(!e)throw new A("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openAudio;if(!n)throw new A("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native AudioBridge seam over the canonical `audio` lane)",qe);return this.audio=(async()=>{let r=await n.call(this.clients,e.model);return this.detachAudio=r.onOutputAudio(o=>this.send($s(o))),r})(),this.audio.catch(r=>this.fail(r)),this.audio}appendAudio(e){this.ensureAudioLane().then(n=>n.appendInputAudio(e)).catch(n=>this.fail(n))}ensureVideoLane(){if(this.video)return this.video;let e=this.config;if(!e)throw new A("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openVideo;if(!n)throw new A("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",qe);return this.video=Promise.resolve(n.call(this.clients,e.model)),this.video.catch(r=>this.fail(r)),this.video}appendVideoFrame(e){this.ensureVideoLane().then(n=>n.sendInputFrame(e)).catch(n=>this.fail(n))}onActivity(e,n){if(!n.activity.manualActivity)throw new A(`realtimeInput.${e} requires manual activity mode \u2014 set setup.realtimeInputConfig.automaticActivityDetection.disabled: true (automatic server-side activity detection is the declared platform follow-up)`);if(e==="activityStart"){if(this.activityOpen)throw new A("activityStart while an activity window is already open (send activityEnd first)");this.activityOpen=!0,n.activity.activityHandling==="START_OF_ACTIVITY_INTERRUPTS"&&this.activeTurn&&(this.activeTurn.interrupted=!0);return}if(!this.activityOpen)throw new A("activityEnd without an open activity window (send activityStart first)");this.activityOpen=!1}enqueue(e){this.queue=this.queue.then(e,()=>{}),this.queue=this.queue.catch(n=>this.fail(n))}async mintResumptionUpdate(){let e=this.config;if(!e?.sessionResumption.enabled||this.ws.readyState!==this.ws.OPEN)return;let n=await this.clients.sessionHandle(e.model);this.proxyHandle=n;let r=(0,Ls.randomUUID)();this.registry.store(this.scope,r,{model:e.model,proxyHandle:n,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(r),this.send(xs(r))}async handleUpstreamEnd(e){if(this.ws.readyState!==this.ws.OPEN||!this.config)return;this.unsubscribeEnd?.(),this.unsubscribeEnd=null;let n=null;try{n=await this.clients.sessionHandle(this.config.model)}catch{}if(n!==null&&n!==this.proxyHandle){this.proxyHandle=n,this.unsubscribeEnd=await this.clients.onSessionEnd(n,r=>this.enqueue(()=>this.handleUpstreamEnd(r))),this.config.sessionResumption.enabled&&await this.mintResumptionUpdate();return}this.send(Es(e.timeLeftMs)),this.ws.close(qe,Pt(`upstream session ended: ${e.reason}`))}async runTurn(){let e=this.config;if(!e||this.ws.readyState!==this.ws.OPEN)return;let n={interrupted:!1};this.activeTurn=n;try{let r=await this.clients.createResponse({model:e.model,input:[...this.transcript],stream:!0,tools:e.tools,temperature:e.temperature,max_output_tokens:e.maxOutputTokens}),o,s=!1;for await(let u of r){if(n.interrupted)break;let d=u;if(d.type==="error")throw new A(`upstream error: ${JSON.stringify(d)}`,qe);if(d.type==="response.completed"){o=d.response,s=!0;continue}let l=Cs(d);l&&this.send(l)}if(n.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!s)throw new A("upstream produced no response.completed event",qe);let{messages:i,assistantItems:a}=Ms(o);this.transcript.push(...a);for(let u of i)this.send(u);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},tt=class extends Error{constructor(n,r){super(r);this.status=n;this.name="UpgradeAuthError"}status};function ed(t,e,n){let r=new Set;(u=>{if(typeof u=="string")u!==""&&r.add(u);else if(Array.isArray(u))for(let d of u)d!==""&&r.add(d)})(t.headers["x-goog-api-key"]);let s=t.headers.authorization;typeof s=="string"&&s.startsWith("Bearer ")&&s.length>7&&r.add(s.slice(7));let i=e.searchParams.get("key")??void 0;if(i!==void 0&&i!==""&&r.add(i),r.size>1)throw new tt(401,`contradictory credentials: ${r.size} distinct keys presented across the x-goog-api-key/Authorization headers and the ?key= query parameter \u2014 present exactly one`);if(n&&i!==void 0&&i!=="")throw new tt(401,"the ?key= query parameter is not accepted on the hosted endpoint \u2014 send the org API key in the x-goog-api-key header or as Authorization: Bearer");let a;for(let u of r)a=u;if(a===void 0&&n)throw new tt(401,"missing credentials: send the org API key in the x-goog-api-key header or as Authorization: Bearer");return a??""}function wr(t,e){let n=e.authenticate!==void 0;if(n&&(e.clients!==void 0||e.apiKey!==void 0))throw new TypeError("attachGeminiLive: authenticate (hosted per-request resolution) is mutually exclusive with a fixed clients/apiKey lane");if(!n&&e.clients===void 0)throw new TypeError("attachGeminiLive: pass fixed `clients` (local lane) or `authenticate` (hosted lane)");let r=new Us.WebSocketServer({noServer:!0}),o=new hr;t.on("upgrade",(i,a,u)=>{let d=new URL(i.url??"/","http://localhost");d.pathname===pn&&(a.on("error",()=>{}),(async()=>{let l=ed(i,d,n),p;if(e.authenticate)p=await e.authenticate(l,i);else{if(e.apiKey!==void 0&&l!==e.apiKey)throw new tt(401,"invalid local proxy api key");p={clients:e.clients,scope:Zu}}r.handleUpgrade(i,a,u,y=>{new yr(y,p,o)})})().catch(l=>{let p=502;(l instanceof tt||typeof l=="object"&&l!==null&&"status"in l&&typeof l.status=="number")&&(p=l.status);let y=p===401?"Unauthorized":p===403?"Forbidden":p===502?"Bad Gateway":"Internal Server Error",c=l instanceof Error?l.message:String(l);a.destroyed||(a.write(`HTTP/1.1 ${p} ${y}\r
|
|
12
12
|
Connection: close\r
|
|
13
13
|
\r
|
|
14
|
-
${
|
|
15
|
-
`),a.destroy())}))});let
|
|
16
|
-
`}var tu=256,nu=128;function Pe(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,nu)}var Ge=class t{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(e,n,r){this.requests[e]=(this.requests[e]??0)+1,this.localInBytes+=n,this.naiveInBytes+=r}modelEntry(e){let n=this.models[e];if(n)return n;let r=Object.keys(this.models).length>=tu?"(other)":e;return this.models[r]??={requests:0,tokens_out:0}}model(e){this.modelEntry(e).requests+=1}track(e,n,r){let o=this,i=performance.now(),s=null,a=0;return(async function*(){for await(let d of e)d.type==="response.output_text.delta"&&typeof d.delta=="string"&&(s===null&&(s=performance.now(),o.ttftMs.push(s-i),o.ttftMs.length>512&&o.ttftMs.shift(),r!==void 0&&(r.ttftMs=s-r.startedAt)),a+=1,o.tokensOut+=1,o.charsOut+=d.delta.length,n!==void 0&&(o.modelEntry(n).tokens_out+=1)),yield d;let u=(performance.now()-(s??i))/1e3;a>0&&u>0&&(o.tokRates.push(a/u),o.tokRates.length>512&&o.tokRates.shift())})()}static p50(e){if(!e.length)return null;let n=[...e].sort((r,o)=>r-o);return n[Math.floor(n.length/2)]}snapshot(){let e=Object.values(this.requests).reduce((n,r)=>n+r,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:t.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:t.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:e},persistent_connections:1,history_bytes_saved:Math.max(0,this.naiveInBytes-this.localInBytes),history_bytes_saved_pct:this.naiveInBytes>0?Math.round((1-this.localInBytes/this.naiveInBytes)*1e3)/10:null}}}},ru=256;function ou(t){if(typeof t=="string")return[{role:"user",content:t}];if(Array.isArray(t))return t;throw new Error("responses input must be a string or an array of messages/items")}var Ve=class extends Error{},Ze=class{conversations=new Map;thread(e,n){let r=ou(n);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new Ve(`unknown previous_response_id ${String(e)} (proxy restarts drop stored responses)`);return[...o,...r]}remember(e,n,r,o=!0){this.conversations.set(e,[...n,...r]);for(let i of this.conversations.keys()){if(this.conversations.size<=ru)break;this.conversations.delete(i)}}forget(e){this.conversations.delete(e)}},ai=new WeakMap;function yt(t){let e=ai.get(t);return e||(e=new Ze,ai.set(t,e)),e}function et(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function Ce(t){return t.find(e=>e.type==="response.completed")?.response??null}function tt(t){return t.type!=="response.function_call_arguments.delta"||typeof t.tool_index!="number"?null:{tool_index:t.tool_index,call_id:typeof t.call_id=="string"?t.call_id:void 0,name:typeof t.name=="string"?t.name:void 0,delta:typeof t.delta=="string"?t.delta:""}}var De=class{calls=new Map;add(e){let n=this.calls.get(e.tool_index),r=!n;return n||(n={args:""},this.calls.set(e.tool_index,n)),e.call_id&&(n.id=e.call_id),e.name&&(n.name=e.name),n.args+=e.delta,{call:n,isNew:r}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([e,n])=>({type:"function_call",call_id:n.id??`call_${e}`,name:n.name??"",arguments:n.args}))}},iu=["<tool_call>","<function="];function su(t){return iu.find(e=>t.includes(e))}function We(t,e){return`serving row emitted a prose tool call (literal ${JSON.stringify(e)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${t}" (see catalog parser_defaults)`}function Je(t,e,n){if(!(!t||e>0))return su(n)}function Wn(t,e){let n=e?.output_text;return typeof n=="string"?n:t.filter(r=>r.type==="response.output_text.delta"&&typeof r.delta=="string").map(r=>r.delta).join("")}function au(t){let e=t?.output;if(!Array.isArray(e))return[];let n=[];for(let r of e){if(r===null||typeof r!="object")continue;let o=r;o.type==="message"?n.push({role:"assistant",content:o.content??""}):o.type==="function_call"&&n.push({type:"function_call",call_id:o.call_id,name:o.name,arguments:o.arguments})}return n}function Jn(t,e){let n=au(t);return n.length>0?n:e?[{role:"assistant",content:e}]:[]}var _e=class extends Error{constructor(n,r,o=400,i){super(n);this.code=r;this.status=o;this.param=i}code;status;param};function nt(t,e){try{return t.thread(e.previous_response_id,e.input)}catch(n){throw n instanceof Ve?new _e(n.message,"previous_response_not_found",400,"previous_response_id"):new _e(String(n instanceof Error?n.message:n),"invalid_input")}}function ui(t){let e=t.text;if(e!==null&&typeof e=="object"){let n=e.format;if(n!==void 0)return n}}async function Kn(t,e,n,r,o){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new _e("instructions must be a string","invalid_instructions");let i=typeof e.instructions=="string"?e.instructions:void 0;return t.createResponse({model:e.model,input:n,stream:r,tools:e.tools,tool_choice:e.tool_choice,temperature:e.temperature,top_p:e.top_p,stop:e.stop,...i!==void 0?{instructions:i}:{},max_output_tokens:e.max_output_tokens,...e.reasoning_effort!==void 0?{reasoning_effort:e.reasoning_effort}:{},...e.chat_template_kwargs!==void 0?{chat_template_kwargs:e.chat_template_kwargs}:{},...ui(e)!==void 0?{response_format:ui(e)}:{}},o)}async function _t(t){let{body:e,clients:n,store:r,stats:o}=t,i=nt(r,e);e.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(i))-Buffer.byteLength(JSON.stringify(e.input)));let s=t.responseId,a=t.remember??e.store!==!1,u=e.store!==!1,d=Pe(e);o.model(d);let l=Array.isArray(e.tools)&&e.tools.length>0,c=t.turn,y=await Kn(n,e,i,!0,c?Te(c):void 0);return{responseId:s,events:uu({events:y,conversation:i,responseId:s,modelKey:d,requestHadTools:l,remember:a,persist:u,store:r,stats:o,turn:t.turn})}}async function*uu(t){let{events:e,conversation:n,responseId:r,modelKey:o,requestHadTools:i,remember:s,persist:a,store:u,stats:d}=t,l=[],c=N=>N.response&&typeof N.response=="object"?{...N,response:{...N.response,id:r}}:N,y=`item_${r.slice(5)}`,g=`rs_${r.slice(5)}`,E=!1,w=!1,h=0;function*b(){E||(E=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let R=!1,T=0,B="";function*M(){R&&(R=!1,yield{type:"response.reasoning_text.done",item_id:g,output_index:T,content_index:0,text:B},yield{type:"response.output_item.done",output_index:T,item:{id:g,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:B}],status:"completed"}})}let $=!1,k=0,H="";function*W(N){$&&($=!1,yield{type:"response.output_text.done",item_id:y,output_index:k,content_index:0,text:H},yield{type:"response.content_part.done",item_id:y,output_index:k,content_index:0,part:{type:"output_text",text:H}},yield{type:"response.output_item.done",output_index:k,item:{id:y,type:"message",role:"assistant",status:N,content:[{type:"output_text",text:H}]}})}let z=new De,j=null,ge=-1;function*ce(N){if(!j)return;let J=j;j=null,yield{type:"response.function_call_arguments.done",item_id:J.fcId,output_index:J.outputIndex,arguments:J.args},yield{type:"response.output_item.done",output_index:J.outputIndex,item:{id:J.fcId,type:"function_call",call_id:J.callId??J.fcId,name:J.name??"",arguments:J.args,status:N}}}let ne=!1;for await(let N of d.track(e,o,t.turn)){l.push(N);let J=String(N.type??"");if(J==="response.created"||J==="response.in_progress"){E=!0,yield c(N);continue}if(J==="response.output_item.added"){w=!0,yield c(N);continue}if(J==="response.reasoning_text.delta"&&!w){yield*b(),R||(R=!0,T=h++,yield{type:"response.output_item.added",output_index:T,item:{id:g,type:"reasoning",summary:[],content:[],status:"in_progress"}}),B+=String(N.delta??""),yield{type:"response.reasoning_text.delta",item_id:g,output_index:T,content_index:0,delta:N.delta};continue}if(J==="response.output_text.delta"&&!w){yield*b(),yield*M(),$||($=!0,k=h++,yield{type:"response.output_item.added",output_index:k,item:{id:y,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:y,output_index:k,content_index:0,part:{type:"output_text",text:""}}),H+=String(N.delta??""),yield{type:J,item_id:y,output_index:k,content_index:0,delta:N.delta};continue}let X=w?null:tt(N);if(X){if(yield*b(),yield*M(),yield*W("completed"),z.add(X),X.tool_index!==ge){yield*ce("completed"),ge=X.tool_index;let ue=h++;j={fcId:`fc_${r.slice(5)}_${X.tool_index}`,outputIndex:ue,callId:X.call_id,name:X.name,args:""},yield{type:"response.output_item.added",output_index:ue,item:{id:j.fcId,type:"function_call",call_id:j.callId??j.fcId,name:j.name??"",arguments:"",status:"in_progress"}}}j&&(X.call_id&&(j.callId=X.call_id),X.name&&(j.name=X.name),j.args+=X.delta,yield{type:"response.function_call_arguments.delta",item_id:j.fcId,output_index:j.outputIndex,delta:X.delta});continue}if(J==="response.completed"&&!w){let ue=et(N.response),xe=Je(i,z.size+ue.length,H);if(xe){ne=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:We(o,xe)}};break}if(yield*M(),yield*W("completed"),yield*ce("completed"),z.size===0)for(let Re of ue){let ve=h++,P=Re.id??Re.call_id??`fc_${ve}`,he=Re.arguments??"",Ue={id:P,type:"function_call",call_id:Re.call_id??P,name:Re.name??""};yield{type:"response.output_item.added",output_index:ve,item:{...Ue,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:P,output_index:ve,delta:he},yield{type:"response.function_call_arguments.done",item_id:P,output_index:ve,arguments:he},yield{type:"response.output_item.done",output_index:ve,item:{...Ue,arguments:he,status:"completed"}}}yield c(N);continue}yield c(N)}if(yield*M(),yield*W("incomplete"),yield*ce("incomplete"),s&&!ne){let N=Ce(l);u.remember(r,n,Jn(N,Wn(l,N)),a)}}var Xn="/v1/responses",du=64*1024*1024,lu=8*1024*1024,cu=16,di=32,li=3600*1e3,pu=/^[A-Za-z0-9._-]{1,256}$/;function fe(t,e,n){return{type:"error",status:t,...n!==void 0?{stream_id:n}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var fu=fe(400,{type:"invalid_request_error",code:"invalid_stream_id",message:"The 'stream_id' field must be a non-empty string with at most 256 characters and may only contain letters, numbers, underscores, hyphens, and periods.",param:"stream_id"});function Qn(){let t,e;return{promise:new Promise((r,o)=>{t=r,e=o}),resolve:t,reject:e}}async function mu(t){for(;t.bufferedAmount>lu&&t.readyState===Fe.WebSocket.OPEN;){let{promise:e,resolve:n}=Qn();setTimeout(n,5),await e}}var zn=class extends Ze{constructor(n){super();this.shared=n}shared;thread(n,r){if(n==null)return super.thread(n,r);try{return super.thread(n,r)}catch(o){if(!(o instanceof Ve))throw o;return this.shared.thread(n,r)}}remember(n,r,o,i=!0){i?this.shared.remember(n,r,o):super.remember(n,r,o)}forget(n){super.forget(n),this.shared.forget(n)}},Yn=class{constructor(e,n,r,o){this.ws=e;this.clients=n;this.stats=r;this.statusOf=o;this.store=new zn(yt(n)),e.on("message",(s,a)=>this.onMessage(s,a)),e.on("close",()=>this.dispose()),e.on("error",()=>this.dispose()),setTimeout(()=>{this.closed||(this.sendFrame(fe(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${li/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},li).unref?.()}ws;clients;stats;statusOf;store;lanes=new Map;laneOfResponse=new Map;activeTurns=new Set;active=0;waiters=[];closed=!1;onMessage(e,n){if(this.closed)return;if(n){this.sendFrame(fe(400,{type:"invalid_request_error",code:"binary_frame",message:"Responses WebSocket frames must be JSON text"})),this.ws.close(1003,"binary frames are not supported");return}let r;try{r=JSON.parse(e.toString())}catch{this.sendFrame(fe(400,{type:"invalid_request_error",code:"invalid_json",message:"frame is not valid JSON"}));return}if(r===null||typeof r!="object"||Array.isArray(r)){this.sendFrame(fe(400,{type:"invalid_request_error",code:"invalid_event",message:"frame must be a JSON object"}));return}let o=r;if(o.type!=="response.create"){this.sendFrame(fe(400,{type:"invalid_request_error",code:"unknown_event",message:`unknown client event type ${JSON.stringify(o.type??null)}; only "response.create" is supported`}));return}if("stream"in o){this.sendFrame(fe(400,{type:"invalid_request_error",code:"stream_is_implicit",message:"'stream' is implicit over the Responses WebSocket and must not be sent",param:"stream"}));return}if("background"in o){this.sendFrame(fe(400,{type:"invalid_request_error",code:"background_not_supported",message:"'background' is not supported over the Responses WebSocket",param:"background"}));return}let i;if(o.stream_id!==void 0){if(typeof o.stream_id!="string"||!pu.test(o.stream_id)){this.sendFrame(fu);return}if(i=o.stream_id,!this.lanes.has(i)&&this.namedLaneCount()>=di){this.sendFrame(fe(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${di}). Reuse an existing stream_id or open a new WebSocket connection.`,param:"stream_id"},i));return}}let s=o.generate===!1?()=>this.runWarmup(o,i):()=>this.runTurn(o,i),a=i??"",u=this.lanes.get(a)??Promise.resolve();this.lanes.set(a,u.then(s).catch(()=>{}))}namedLaneCount(){return this.lanes.has("")?this.lanes.size-1:this.lanes.size}async runTurn(e,n){if(!this.closed){await this.acquire();try{let r=Buffer.byteLength(JSON.stringify(e));this.stats.request("responses_ws",r,r);let o;try{o=await _t({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:ht(gt())})}catch(s){this.failTurn(s,n,e);return}this.laneOfResponse.set(o.responseId,n??null);let i=o.events;this.activeTurns.add(i);try{for await(let s of i){if(this.closed||this.ws.readyState!==Fe.WebSocket.OPEN)break;this.send(s,n),await mu(this.ws)}}finally{this.activeTurns.delete(i)}}catch(r){this.failTurn(r,n,e)}finally{this.release()}}}runWarmup(e,n){if(this.closed)return;let r=ht(gt());try{let o=nt(this.store,{...e,input:e.input??(e.previous_response_id!=null?[]:void 0)});this.store.remember(r,o,[],e.store!==!1),this.laneOfResponse.set(r,n??null),this.send({type:"response.created",response:{id:r,object:"response",status:"in_progress"}},n)}catch(o){this.failTurn(o,n,e)}}failTurn(e,n,r){this.sendFrame(this.errorFrameOf(e,n));let o=r.previous_response_id;typeof o=="string"&&this.laneOfResponse.get(o)===(n??null)&&(this.store.forget(o),this.laneOfResponse.delete(o))}errorFrameOf(e,n){let r=String(e instanceof Error?e.message:e);if(e instanceof _e)return fe(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},n);if(e instanceof re)return fe(404,{type:"invalid_request_error",code:"model_not_found",message:r},n);if(e instanceof le)return fe(503,{type:"upstream_error",code:"session_gone",message:r},n);let o=this.statusOf?.(e)??null;return o?fe(o.status,{type:o.openaiType,message:r},n):fe(500,{type:"upstream_error",message:r},n)}async acquire(){for(;this.active>=cu;){let{promise:e,resolve:n}=Qn();this.waiters.push(n),await e}this.active++}release(){this.active--,this.waiters.shift()?.()}dispose(){if(!this.closed){this.closed=!0;for(let e of[...this.activeTurns])e.return(void 0);this.activeTurns.clear(),this.lanes.clear(),this.laneOfResponse.clear();for(let e of this.waiters.splice(0))e()}}send(e,n){if(this.closed||this.ws.readyState!==Fe.WebSocket.OPEN)return;let r=n===void 0?e:{...e,stream_id:n};this.ws.send(JSON.stringify(r))}sendFrame(e){this.closed||this.ws.readyState!==Fe.WebSocket.OPEN||this.ws.send(JSON.stringify(e))}};function Kt(t,e,n){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${n}\r
|
|
14
|
+
${c}\r
|
|
15
|
+
`),a.destroy())}))});let s=null;return()=>(s??=(async()=>{for(let i of r.clients)i.close(1001,"server closing");await new Promise((i,a)=>r.close(u=>u===void 0?i():a(u)))})(),s)}k();var Je=require("ws");k();k();var Ns=require("crypto"),Ds="Inference-Id";function Ct(){return(0,Ns.randomUUID)()}function _r(t){return t==="/v1"||t.startsWith("/v1/")}function Fs(t,e){if(!_r(t)||e===null)throw new Error(`no inference turn for ${t}: an inference lane was reached off the /v1 surface, so its response would carry no Inference-Id and could never be billed`);return e}function $t(t){return`resp_${t}`}function Bs(t){return`chatcmpl-${t}`}function js(t){return`msg_${t}`}function xe(t){return e=>{t.sessionId=e.sessionId,t.catalog=e.identity}}function qs(t){let e=t.now??(()=>performance.now());return{event:"inference_request",ts:new Date().toISOString(),inference_id:t.turn.id,method:t.method,path:t.path,status:t.res.headersSent?t.res.statusCode:null,delivered:t.res.writableFinished,stream_error:t.turn.streamError,model:t.turn.model,stream:t.turn.stream,org:t.turn.org,duration_ms:e()-t.turn.startedAt,ttft_ms:t.turn.stream===!0?t.turn.ttftMs:null,prompt_tokens:t.turn.promptTokens,completion_tokens:t.turn.completionTokens,output_units:t.turn.outputUnits,session_id:t.turn.sessionId,queue_ms:t.turn.timing?.queue_ms??null,prefill_ms:t.turn.timing?.prefill_ms??null,decode_ms:t.turn.timing?.decode_ms??null,total_ms:t.turn.timing?.total_ms??null,catalog:t.turn.catalog}}function Hs(t){return`${JSON.stringify(t)}
|
|
16
|
+
`}var td=256,nd=128;function ve(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,nd)}var nt=class t{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(e,n,r){this.requests[e]=(this.requests[e]??0)+1,this.localInBytes+=n,this.naiveInBytes+=r}modelEntry(e){let n=this.models[e];if(n)return n;let r=Object.keys(this.models).length>=td?"(other)":e;return this.models[r]??={requests:0,tokens_out:0}}model(e){this.modelEntry(e).requests+=1}track(e,n,r){let o=this,s=performance.now(),i=null,a=0;return(async function*(){for await(let d of e)d.type==="response.output_text.delta"&&typeof d.delta=="string"&&(i===null&&(i=performance.now(),o.ttftMs.push(i-s),o.ttftMs.length>512&&o.ttftMs.shift(),r!==void 0&&(r.ttftMs=i-r.startedAt)),a+=1,o.tokensOut+=1,o.charsOut+=d.delta.length,n!==void 0&&(o.modelEntry(n).tokens_out+=1)),yield d;let u=(performance.now()-(i??s))/1e3;a>0&&u>0&&(o.tokRates.push(a/u),o.tokRates.length>512&&o.tokRates.shift())})()}static p50(e){if(!e.length)return null;let n=[...e].sort((r,o)=>r-o);return n[Math.floor(n.length/2)]}snapshot(){let e=Object.values(this.requests).reduce((n,r)=>n+r,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:t.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:t.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:e},persistent_connections:1,history_bytes_saved:Math.max(0,this.naiveInBytes-this.localInBytes),history_bytes_saved_pct:this.naiveInBytes>0?Math.round((1-this.localInBytes/this.naiveInBytes)*1e3)/10:null}}}},rd=256;function od(t){if(typeof t=="string")return[{role:"user",content:t}];if(Array.isArray(t))return t;throw new Error("responses input must be a string or an array of messages/items")}var rt=class extends Error{},pt=class{conversations=new Map;thread(e,n){let r=od(n);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new rt(`unknown previous_response_id ${String(e)} (proxy restarts drop stored responses)`);return[...o,...r]}remember(e,n,r,o=!0){this.conversations.set(e,[...n,...r]);for(let s of this.conversations.keys()){if(this.conversations.size<=rd)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},Gs=new WeakMap;function Mt(t){let e=Gs.get(t);return e||(e=new pt,Gs.set(t,e)),e}function ft(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function Me(t){return t.find(e=>e.type==="response.completed")?.response??null}function mt(t){return t.type!=="response.function_call_arguments.delta"||typeof t.tool_index!="number"?null:{tool_index:t.tool_index,call_id:typeof t.call_id=="string"?t.call_id:void 0,name:typeof t.name=="string"?t.name:void 0,delta:typeof t.delta=="string"?t.delta:""}}var We=class{calls=new Map;add(e){let n=this.calls.get(e.tool_index),r=!n;return n||(n={args:""},this.calls.set(e.tool_index,n)),e.call_id&&(n.id=e.call_id),e.name&&(n.name=e.name),n.args+=e.delta,{call:n,isNew:r}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([e,n])=>({type:"function_call",call_id:n.id??`call_${e}`,name:n.name??"",arguments:n.args}))}},sd=["<tool_call>","<function="];function id(t){return sd.find(e=>t.includes(e))}function ot(t,e){return`serving row emitted a prose tool call (literal ${JSON.stringify(e)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${t}" (see catalog parser_defaults)`}function st(t,e,n){if(!(!t||e>0))return id(n)}function gt(t,e){let n=e?.output_text;return typeof n=="string"?n:t.filter(r=>r.type==="response.output_text.delta"&&typeof r.delta=="string").map(r=>r.delta).join("")}function ad(t){let e=t?.output;if(!Array.isArray(e))return[];let n=[];for(let r of e){if(r===null||typeof r!="object")continue;let o=r;o.type==="message"?n.push({role:"assistant",content:o.content??""}):o.type==="function_call"&&n.push({type:"function_call",call_id:o.call_id,name:o.name,arguments:o.arguments})}return n}function vr(t,e){let n=ad(t);return n.length>0?n:e?[{role:"assistant",content:e}]:[]}var Ee=class extends Error{constructor(n,r,o=400,s){super(n);this.code=r;this.status=o;this.param=s}code;status;param};function ht(t,e){try{return t.thread(e.previous_response_id,e.input)}catch(n){throw n instanceof rt?new Ee(n.message,"previous_response_not_found",400,"previous_response_id"):new Ee(String(n instanceof Error?n.message:n),"invalid_input")}}function Vs(t){let e=t.text;if(e!==null&&typeof e=="object"){let n=e.format;if(n!==void 0)return n}}async function br(t,e,n,r,o){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new Ee("instructions must be a string","invalid_instructions");let s=typeof e.instructions=="string"?e.instructions:void 0;return t.createResponse({model:e.model,input:n,stream:r,tools:e.tools,tool_choice:e.tool_choice,temperature:e.temperature,top_p:e.top_p,stop:e.stop,...s!==void 0?{instructions:s}:{},max_output_tokens:e.max_output_tokens,...e.reasoning_effort!==void 0?{reasoning_effort:e.reasoning_effort}:{},...e.chat_template_kwargs!==void 0?{chat_template_kwargs:e.chat_template_kwargs}:{},...Vs(e)!==void 0?{response_format:Vs(e)}:{}},o)}async function Lt(t){let{body:e,clients:n,store:r,stats:o}=t,s=ht(r,e);e.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(s))-Buffer.byteLength(JSON.stringify(e.input)));let i=t.responseId,a=t.remember??e.store!==!1,u=e.store!==!1,d=ve(e);o.model(d);let l=Array.isArray(e.tools)&&e.tools.length>0,p=t.turn,y=await br(n,e,s,!0,p?xe(p):void 0);return{responseId:i,events:cd({events:y,conversation:s,responseId:i,modelKey:d,requestHadTools:l,remember:a,persist:u,store:r,stats:o,turn:t.turn})}}function ud(t,e){let n=[...t].sort((u,d)=>u.output_index-d.output_index).map(({item:u})=>u),r=Array.isArray(e?.output)?e.output:[],o=r.filter(u=>u!==null&&typeof u=="object"&&u.type==="message"),s=n.filter(u=>u.type==="message").length===o.length,i=0;return[...n.map(u=>u.type==="message"&&s?ld(o[i++],u):u),...dd(r,n)]}function dd(t,e){let n=e.some(d=>d.type==="message"),r=e.some(d=>d.type==="reasoning"),o=d=>String(d.call_id??d.id??""),s=e.filter(d=>d.type==="function_call"),i=t.filter(d=>d!==null&&typeof d=="object"&&d.type==="function_call").length,a=s.length===i,u=new Set(s.map(o));return t.filter(d=>{if(d===null||typeof d!="object")return!0;let l=d;return l.type==="message"?!n:l.type==="reasoning"?!r:l.type==="function_call"?!a&&!u.has(o(l)):!0})}function ld(t,e){let n=a=>a!==null&&typeof a=="object"&&a.type==="output_text",r=Array.isArray(t.content)?t.content:[];if(r.filter(n).length!==1)return e;let s=(Array.isArray(e.content)?e.content:[]).find(n);if(s===void 0)return e;let i={...t};return i.id=e.id,t.role===void 0&&(i.role=e.role),t.status===void 0&&(i.status=e.status),i.content=r.map(a=>n(a)?{...a,text:s.text}:a),i}async function*cd(t){let{events:e,conversation:n,responseId:r,modelKey:o,requestHadTools:s,remember:i,persist:a,store:u,stats:d}=t,l=[],p=H=>H.response&&typeof H.response=="object"?{...H,response:{...H.response,id:r}}:H,y=`item_${r.slice(5)}`,c=!1,m=!1,w=0;function*R(){c||(c=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let h=[],v=(H,I)=>{h.push({output_index:H,item:I})},b=!1,$=0,S="",E=0,x="";function*L(){if(!b)return;b=!1,yield{type:"response.reasoning_text.done",item_id:S,output_index:$,content_index:0,text:x};let H={id:S,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:x}],status:"completed"};yield{type:"response.output_item.done",output_index:$,item:H},v($,H),x=""}let B=!1,q=0,V="",de=0,fe="",se="";function*ie(H){if(!B)return;B=!1,yield{type:"response.output_text.done",item_id:V,output_index:q,content_index:0,text:fe},yield{type:"response.content_part.done",item_id:V,output_index:q,content_index:0,part:{type:"output_text",text:fe}};let I={id:V,type:"message",role:"assistant",status:H,content:[{type:"output_text",text:fe}]};yield{type:"response.output_item.done",output_index:q,item:I},v(q,I),fe=""}let ke=new We,Z=null,Se=-1;function*Ae(H){if(!Z)return;let I=Z;Z=null,yield{type:"response.function_call_arguments.done",item_id:I.fcId,output_index:I.outputIndex,arguments:I.args};let Y={id:I.fcId,type:"function_call",call_id:I.callId??I.fcId,name:I.name??"",arguments:I.args,status:H};yield{type:"response.output_item.done",output_index:I.outputIndex,item:Y},v(I.outputIndex,Y)}let Ke=!1;for await(let H of d.track(e,o,t.turn)){l.push(H);let I=String(H.type??"");if(I==="response.created"||I==="response.in_progress"){c=!0,yield p(H);continue}if(I==="response.output_item.added"){m=!0,yield p(H);continue}if(I==="response.reasoning_text.delta"&&!m){yield*R(),b||(b=!0,$=w++,E+=1,S=E===1?`rs_${r.slice(5)}`:`rs_${r.slice(5)}_${E}`,yield{type:"response.output_item.added",output_index:$,item:{id:S,type:"reasoning",summary:[],content:[],status:"in_progress"}}),x+=String(H.delta??""),yield{type:"response.reasoning_text.delta",item_id:S,output_index:$,content_index:0,delta:H.delta};continue}if(I==="response.output_text.delta"&&!m){yield*R(),yield*L(),B||(B=!0,q=w++,de+=1,V=de===1?y:`${y}_${de}`,yield{type:"response.output_item.added",output_index:q,item:{id:V,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:V,output_index:q,content_index:0,part:{type:"output_text",text:""}}),fe+=String(H.delta??""),se+=String(H.delta??""),yield{type:I,item_id:V,output_index:q,content_index:0,delta:H.delta};continue}let Y=m?null:mt(H);if(Y){if(yield*R(),yield*L(),yield*ie("completed"),ke.add(Y),Y.tool_index!==Se){yield*Ae("completed"),Se=Y.tool_index;let Ie=w++;Z={fcId:`fc_${r.slice(5)}_${Y.tool_index}`,outputIndex:Ie,callId:Y.call_id,name:Y.name,args:""},yield{type:"response.output_item.added",output_index:Ie,item:{id:Z.fcId,type:"function_call",call_id:Z.callId??Z.fcId,name:Z.name??"",arguments:"",status:"in_progress"}}}Z&&(Y.call_id&&(Z.callId=Y.call_id),Y.name&&(Z.name=Y.name),Z.args+=Y.delta,yield{type:"response.function_call_arguments.delta",item_id:Z.fcId,output_index:Z.outputIndex,delta:Y.delta});continue}if(I==="response.completed"&&!m){let Ie=ft(H.response),F=st(s,ke.size+Ie.length,se);if(F){Ke=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:ot(o,F)}};break}if(yield*L(),yield*ie("completed"),yield*Ae("completed"),ke.size===0)for(let Ce of Ie){let De=w++,at=Ce.id??Ce.call_id??`fc_${De}`,_e=Ce.arguments??"",vt={id:at,type:"function_call",call_id:Ce.call_id??at,name:Ce.name??""};yield{type:"response.output_item.added",output_index:De,item:{...vt,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:at,output_index:De,delta:_e},yield{type:"response.function_call_arguments.done",item_id:at,output_index:De,arguments:_e};let Fe={...vt,arguments:_e,status:"completed"};yield{type:"response.output_item.done",output_index:De,item:Fe},v(De,Fe)}let me=ud(h,H.response);yield me.length>0?p({...H,response:{...H.response,output:me}}):p(H);continue}yield p(H)}if(yield*L(),yield*ie("incomplete"),yield*Ae("incomplete"),i&&!Ke){let H=Me(l);u.remember(r,n,vr(H,gt(l,H)),a)}}var Rr="/v1/responses",pd=64*1024*1024,fd=8*1024*1024,md=16,Ws=32,Js=3600*1e3,gd=/^[A-Za-z0-9._-]{1,256}$/;function be(t,e,n){return{type:"error",status:t,...n!==void 0?{stream_id:n}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var hd=be(400,{type:"invalid_request_error",code:"invalid_stream_id",message:"The 'stream_id' field must be a non-empty string with at most 256 characters and may only contain letters, numbers, underscores, hyphens, and periods.",param:"stream_id"});function xr(){let t,e;return{promise:new Promise((r,o)=>{t=r,e=o}),resolve:t,reject:e}}async function yd(t){for(;t.bufferedAmount>fd&&t.readyState===Je.WebSocket.OPEN;){let{promise:e,resolve:n}=xr();setTimeout(n,5),await e}}var kr=class extends pt{constructor(n){super();this.shared=n}shared;thread(n,r){if(n==null)return super.thread(n,r);try{return super.thread(n,r)}catch(o){if(!(o instanceof rt))throw o;return this.shared.thread(n,r)}}remember(n,r,o,s=!0){s?this.shared.remember(n,r,o):super.remember(n,r,o)}forget(n){super.forget(n),this.shared.forget(n)}},Sr=class{constructor(e,n,r,o){this.ws=e;this.clients=n;this.stats=r;this.statusOf=o;this.store=new kr(Mt(n)),e.on("message",(i,a)=>this.onMessage(i,a)),e.on("close",()=>this.dispose()),e.on("error",()=>this.dispose()),setTimeout(()=>{this.closed||(this.sendFrame(be(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${Js/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},Js).unref?.()}ws;clients;stats;statusOf;store;lanes=new Map;laneOfResponse=new Map;activeTurns=new Set;active=0;waiters=[];closed=!1;onMessage(e,n){if(this.closed)return;if(n){this.sendFrame(be(400,{type:"invalid_request_error",code:"binary_frame",message:"Responses WebSocket frames must be JSON text"})),this.ws.close(1003,"binary frames are not supported");return}let r;try{r=JSON.parse(e.toString())}catch{this.sendFrame(be(400,{type:"invalid_request_error",code:"invalid_json",message:"frame is not valid JSON"}));return}if(r===null||typeof r!="object"||Array.isArray(r)){this.sendFrame(be(400,{type:"invalid_request_error",code:"invalid_event",message:"frame must be a JSON object"}));return}let o=r;if(o.type!=="response.create"){this.sendFrame(be(400,{type:"invalid_request_error",code:"unknown_event",message:`unknown client event type ${JSON.stringify(o.type??null)}; only "response.create" is supported`}));return}if("stream"in o){this.sendFrame(be(400,{type:"invalid_request_error",code:"stream_is_implicit",message:"'stream' is implicit over the Responses WebSocket and must not be sent",param:"stream"}));return}if("background"in o){this.sendFrame(be(400,{type:"invalid_request_error",code:"background_not_supported",message:"'background' is not supported over the Responses WebSocket",param:"background"}));return}let s;if(o.stream_id!==void 0){if(typeof o.stream_id!="string"||!gd.test(o.stream_id)){this.sendFrame(hd);return}if(s=o.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=Ws){this.sendFrame(be(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${Ws}). Reuse an existing stream_id or open a new WebSocket connection.`,param:"stream_id"},s));return}}let i=o.generate===!1?()=>this.runWarmup(o,s):()=>this.runTurn(o,s),a=s??"",u=this.lanes.get(a)??Promise.resolve();this.lanes.set(a,u.then(i).catch(()=>{}))}namedLaneCount(){return this.lanes.has("")?this.lanes.size-1:this.lanes.size}async runTurn(e,n){if(!this.closed){await this.acquire();try{let r=Buffer.byteLength(JSON.stringify(e));this.stats.request("responses_ws",r,r);let o;try{o=await Lt({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:$t(Ct())})}catch(i){this.failTurn(i,n,e);return}this.laneOfResponse.set(o.responseId,n??null);let s=o.events;this.activeTurns.add(s);try{for await(let i of s){if(this.closed||this.ws.readyState!==Je.WebSocket.OPEN)break;this.send(i,n),await yd(this.ws)}}finally{this.activeTurns.delete(s)}}catch(r){this.failTurn(r,n,e)}finally{this.release()}}}runWarmup(e,n){if(this.closed)return;let r=$t(Ct());try{let o=ht(this.store,{...e,input:e.input??(e.previous_response_id!=null?[]:void 0)});this.store.remember(r,o,[],e.store!==!1),this.laneOfResponse.set(r,n??null),this.send({type:"response.created",response:{id:r,object:"response",status:"in_progress"}},n)}catch(o){this.failTurn(o,n,e)}}failTurn(e,n,r){this.sendFrame(this.errorFrameOf(e,n));let o=r.previous_response_id;typeof o=="string"&&this.laneOfResponse.get(o)===(n??null)&&(this.store.forget(o),this.laneOfResponse.delete(o))}errorFrameOf(e,n){let r=String(e instanceof Error?e.message:e);if(e instanceof Ee)return be(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},n);if(e instanceof z)return be(404,{type:"invalid_request_error",code:"model_not_found",message:r},n);if(e instanceof pe)return be(503,{type:"upstream_error",code:"session_gone",message:r},n);let o=this.statusOf?.(e)??null;return o?be(o.status,{type:o.openaiType,message:r},n):be(500,{type:"upstream_error",message:r},n)}async acquire(){for(;this.active>=md;){let{promise:e,resolve:n}=xr();this.waiters.push(n),await e}this.active++}release(){this.active--,this.waiters.shift()?.()}dispose(){if(!this.closed){this.closed=!0;for(let e of[...this.activeTurns])e.return(void 0);this.activeTurns.clear(),this.lanes.clear(),this.laneOfResponse.clear();for(let e of this.waiters.splice(0))e()}}send(e,n){if(this.closed||this.ws.readyState!==Je.WebSocket.OPEN)return;let r=n===void 0?e:{...e,stream_id:n};this.ws.send(JSON.stringify(r))}sendFrame(e){this.closed||this.ws.readyState!==Je.WebSocket.OPEN||this.ws.send(JSON.stringify(e))}};function mn(t,e,n){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${n}\r
|
|
17
17
|
`;t.end(`HTTP/1.1 ${e} ${r}\r
|
|
18
18
|
Connection: close\r
|
|
19
19
|
Content-Type: text/plain; charset=utf-8\r
|
|
20
20
|
Content-Length: ${Buffer.byteLength(o)}\r
|
|
21
21
|
\r
|
|
22
|
-
${o}`)}function Zn(t,e){let n=new Fe.WebSocketServer({noServer:!0,maxPayload:du}),r=new Ge,o=new Set,i=new Set,s=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,u=!1,d=(l,c,y)=>{let g;try{g=new URL(l.url??"/","http://localhost")}catch{Kt(c,400,"malformed upgrade URL");return}if(c.on("error",()=>c.destroy()),c.on("close",()=>s.delete(c)),g.pathname!==Xn){s.add(c);return}i.add(c);let E=!1;(async()=>{if(e.apiKey&&(l.headers.authorization??"")!==`Bearer ${e.apiKey}`){Kt(c,401,"invalid local proxy api key");return}let w;try{w=await a(l)}catch(h){let b=e.statusOf?.(h)??null;Kt(c,b?.status??500,"upstream resolver rejected the connection");return}u||c.destroyed||(E=!0,n.handleUpgrade(l,c,y,h=>{i.delete(c),o.add(h),h.on("close",()=>o.delete(h));try{new Yn(h,w,r,e.statusOf)}catch{o.delete(h),h.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!E&&!c.destroyed&&Kt(c,500,"responses websocket upgrade failed")}).finally(()=>i.delete(c))};return t.on("upgrade",d),{stats:r,close:()=>{let{promise:l,resolve:c}=Qn();if(u)return c(),l;u=!0,t.off("upgrade",d);for(let g of i)g.destroy();i.clear();for(let g of s)g.destroy();s.clear();for(let g of o)g.close(1001,"server shutting down");let y=setTimeout(()=>{for(let g of o)g.readyState!==Fe.WebSocket.CLOSED&&g.terminate()},2e3);return y.unref?.(),n.close(()=>{clearTimeout(y),c()}),l}}}_();_();var hi=require("@urun-sh/core");_();var ci=require("@urun-sh/core"),gu={no_capacity:429,queue_timeout:429,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502};function zt(t){if(t==null)return null;let e=gu[t];return e===void 0?null:{status:e,code:t,retryable:e!==502,openaiType:e===429?"capacity_error":"upstream_error",anthropicType:e===429?"rate_limit_error":e===503?"overloaded_error":"api_error"}}function Yt(t){if(t.kind==="create-failed")return zt(t.status.error?.code??null);if(t.kind!=="never-live")return null;let e=(t.status.elapsedMs??0)>=ci.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return zt(e?"cold_start_exceeded":"queue_timeout")}var yi=require("ws");_();_();var fi=require("crypto");function U(t){return`${t}_${(0,fi.randomUUID)().replace(/-/g,"").slice(0,24)}`}var Xt=4*1024*1024,er=8*1024*1024,Qt=512,Zt=20*1024*1024;function oe(t,e,n){return{type:"invalid_request_error",code:t,message:e,param:n??null}}function vt(t,e){return{type:"server_error",code:t,message:e}}function Y(t){return{type:"error",event_id:U("event"),error:t}}function tr(){return{outputModalities:["text"],turnDetection:null,inputAudioTranscription:!1}}var L=class extends Error{constructor(n,r,o="invalid_value"){super(n);this.param=r;this.code=o;this.name="SessionUpdateError"}param;code},hu={model:!0,instructions:!0,modalities:!0,voice:!0,input_audio_format:!0,output_audio_format:!0,temperature:!0,max_output_tokens:!0,max_response_output_tokens:!0,tool_choice:!0,tools:!0,metadata:!0,turn_detection:!0,input_audio_transcription:!0,input_audio_noise_reduction:!0,audio:!0},yu={format:!0,transcription:!0,turn_detection:!0,noise_reduction:!0},_u={format:!0,voice:!0,speed:!0};function pi(t,e){let n=t;if(!n||typeof n!="object"||n.type!=="audio/pcm"||n.rate!==void 0&&n.rate!==24e3)throw new L(`${e} must be {"type":"audio/pcm","rate":24000} \u2014 the session audio lane is 24 kHz mono PCM16 only`,e)}function wt(t,e,n){if(!t||typeof t!="object"||Array.isArray(t))throw new L("session must be an object","session","invalid_request");let r=t,o=Object.keys(r).filter(s=>!(s in hu));if(o.length>0)throw new L(`session field(s) not supported by this backend: ${o.join(", ")}`,o[0],"unsupported_field");let i={...e,outputModalities:[...e.outputModalities]};for(let[s,a]of Object.entries(r))switch(s){case"model":if(a!==null&&typeof a!="string")throw new L("model must be a string or null","model");i.model=a??void 0;break;case"instructions":if(a!==null&&typeof a!="string")throw new L("instructions must be a string or null","instructions");i.instructions=a??void 0;break;case"modalities":{if(!Array.isArray(a)||a.length===0)throw new L("modalities must be a non-empty array","modalities");let u=[];for(let d of a){if(d!=="text"&&d!=="audio")throw new L(`modality ${JSON.stringify(d)} is not "text" or "audio"`,"modalities");if(d==="audio"&&!n.voice)throw new L('modality "audio" refused: the resolver did not provide an authoritative voice binding for the resolved backend \u2014 this model cannot speak on this session',"modalities","audio_not_supported");u.includes(d)||u.push(d)}i.outputModalities=u;break}case"voice":if(a===null){delete i.voice;break}if(typeof a!="string")throw new L("voice must be a string or null","voice");if(!n.voice)throw new L("voice refused: no authoritative voice binding for the resolved backend","voice","audio_not_supported");i.voice=a;break;case"input_audio_format":case"output_audio_format":if(a!=="pcm16")throw new L(`${s} must be "pcm16" \u2014 the session audio lane is 24 kHz mono PCM16 only`,s);break;case"temperature":if(a!==null&&(typeof a!="number"||!Number.isFinite(a)))throw new L("temperature must be a finite number or null","temperature");i.temperature=a??void 0;break;case"max_output_tokens":case"max_response_output_tokens":if(a!==null&&a!=="inf"&&(typeof a!="number"||!Number.isInteger(a)||a<1))throw new L(`${s} must be a positive integer, "inf" or null`,s);i.maxOutputTokens=a===null?void 0:a;break;case"tools":if(!Array.isArray(a))throw new L("tools must be an array","tools");i.tools=a;break;case"tool_choice":i.toolChoice=a;break;case"metadata":if(a!==null&&(!a||typeof a!="object"||Array.isArray(a)))throw new L("metadata must be an object or null","metadata");i.metadata=a??null;break;case"turn_detection":{if(a===null){i.turnDetection=null;break}if(!n.turns)throw new L("turn_detection is refused: the resolved backend does not wire its own turn boundary lane (stt stream with turn_start/turn_end) \u2014 there is no native VAD source to drive responses from. Drive turns manually: input_audio_buffer.commit + response.create.","turn_detection","unsupported_field");let u=a;if(!u||typeof u!="object"||Array.isArray(u))throw new L("turn_detection must be an object or null","turn_detection");let d=String(u.type??"");if(d!=="server_vad"&&d!=="semantic_vad")throw new L(`turn_detection.type ${JSON.stringify(u.type)} is not "server_vad"/"semantic_vad"`,"turn_detection.type");i.turnDetection={createResponse:u.create_response!==!1,interruptResponse:u.interrupt_response!==!1};break}case"input_audio_transcription":{if(a===null){i.inputAudioTranscription=!1;break}if(!n.turns)throw new L("input_audio_transcription is refused: the resolved backend does not wire a transcript lane \u2014 no native transcription source exists behind this session","input_audio_transcription","unsupported_field");if(!a||typeof a!="object"||Array.isArray(a))throw new L("input_audio_transcription must be an object or null","input_audio_transcription");for(let u of Object.keys(a))if(!["model","language","languages","prompt"].includes(u))throw new L(`input_audio_transcription field not honored by this backend: ${u} (transcription comes from the voice engine itself)`,`input_audio_transcription.${u}`,"unsupported_field");i.inputAudioTranscription=!0;break}case"input_audio_noise_reduction":if(a!==null)throw new L("input_audio_noise_reduction must be null on this backend: no noise-reduction primitive exists behind the session audio bridge","input_audio_noise_reduction","unsupported_field");break;case"audio":{if(a===null)break;let u=a;if(!u||typeof u!="object"||Array.isArray(u))throw new L("audio must be an object or null","audio");if(u.input!==void 0&&u.input!==null){let d=u.input;if(!d||typeof d!="object"||Array.isArray(d))throw new L("audio.input must be an object","audio.input");for(let l of Object.keys(d))if(!(l in yu))throw new L(`audio.input field not supported by this backend: ${l}`,`audio.input.${l}`,"unsupported_field");if(d.format!==void 0&&pi(d.format,"audio.input.format"),d.transcription!==null&&d.transcription!==void 0){let l=wt({input_audio_transcription:d.transcription},i,n);i.inputAudioTranscription=l.inputAudioTranscription}if(d.turn_detection!==null&&d.turn_detection!==void 0){let l=wt({turn_detection:d.turn_detection},i,n);i.turnDetection=l.turnDetection}if(d.noise_reduction!==null&&d.noise_reduction!==void 0)throw new L("audio.input.noise_reduction must be null on this backend: no noise-reduction primitive","audio.input.noise_reduction","unsupported_field")}if(u.output!==void 0&&u.output!==null){let d=u.output;if(!d||typeof d!="object"||Array.isArray(d))throw new L("audio.output must be an object","audio.output");for(let l of Object.keys(d))if(!(l in _u))throw new L(`audio.output field not supported by this backend: ${l}`,`audio.output.${l}`,"unsupported_field");if(d.format!==void 0&&pi(d.format,"audio.output.format"),d.speed!==void 0)throw new L("audio.output.speed is not supported on this backend: playback rate is fixed","audio.output.speed","unsupported_field");if(d.voice!==void 0)if(d.voice===null)delete i.voice;else{if(typeof d.voice!="string")throw new L("audio.output.voice must be a string or null","audio.output.voice");if(!n.voice)throw new L("audio.output.voice refused: no authoritative voice binding for the resolved backend","audio.output.voice","audio_not_supported");i.voice=d.voice}d.format!==void 0&&!i.outputModalities.includes("audio")&&(i.outputModalities=["text","audio"])}break}default:break}return i}function en(t,e){return{id:e,object:"realtime.session",model:t.model??null,instructions:t.instructions??"",modalities:[...t.outputModalities],voice:t.voice??null,input_audio_format:"pcm16",output_audio_format:"pcm16",input_audio_transcription:t.inputAudioTranscription?{}:null,input_audio_noise_reduction:null,turn_detection:t.turnDetection?{type:"server_vad",create_response:t.turnDetection.createResponse,interrupt_response:t.turnDetection.interruptResponse}:null,temperature:t.temperature??null,max_response_output_tokens:t.maxOutputTokens??"inf",tool_choice:t.toolChoice??"auto",tools:t.tools??[],metadata:t.metadata??null}}function nr(t,e){let n=t;if(!n||typeof n!="object")throw new L("item must be an object","item","invalid_request");if(n.type==="function_call_output"){if(typeof n.call_id!="string"||typeof n.output!="string")throw new L("function_call_output item requires string call_id and output","item.call_id","invalid_request");return{id:typeof n.id=="string"?n.id:U("item"),type:"function_call_output",callId:n.call_id,output:n.output}}if(n.type==="message"){let r=n.role;if(r!=="user"&&r!=="assistant"&&r!=="system")throw new L('message item role must be "user", "assistant" or "system"',"item.role","invalid_request");if(!Array.isArray(n.content))throw new L("message item content must be an array","item.content","invalid_request");let o=[];for(let i of n.content){let s=i;if(!s||typeof s!="object")throw new L("content part must be an object","item.content","invalid_request");if(s.type==="input_text"||r==="assistant"&&s.type==="output_text"){if(typeof s.text!="string")throw new L(`${String(s.type)} part requires string text`,"item.content.text","invalid_request");o.push({type:s.type,text:s.text});continue}if(s.type==="input_audio"){if(!e)throw new L("input_audio content refused: no authoritative voice binding for the resolved backend","item.content.type","audio_not_supported");if(typeof s.audio!="string")throw new L("input_audio part requires base64 audio","item.content.audio","invalid_request");o.push({type:"input_audio",...typeof s.transcript=="string"?{transcript:s.transcript}:{}});continue}throw new L(`content part type ${String(s.type)} is not supported on message items here (input_text / output_text / input_audio)`,"item.content.type","invalid_request")}return{id:typeof n.id=="string"?n.id:U("item"),type:"message",role:r,content:o}}throw new L(`item type ${String(n.type)} is not supported \u2014 message and function_call_output items only`,"item.type","invalid_request")}function rr(t){let e=[],n=!1;for(let r of t){if(r.type==="function_call"){e.push({type:"function_call",call_id:r.callId,name:r.name,arguments:r.arguments??"{}"});continue}if(r.type==="function_call_output"){e.push({type:"function_call_output",call_id:r.callId,output:r.output});continue}let o=[];for(let i of r.content??[]){if(i.type==="input_text"||i.type==="output_text"){o.push({type:i.type,text:i.text});continue}if(i.type==="input_audio"){let s=i.transcript;if(typeof s=="string"&&s.trim().length>0){o.push({type:"input_text",text:s});continue}n=!0;continue}}e.push({role:r.role,content:o})}return{input:e,sawAudioOnlyItem:n}}var wu=1001,vu=1011,mi=t=>t.slice(0,120),bu=/^[A-Za-z0-9+/=]*$/,ku=2;function xu(t){let e=Buffer.from(t,"base64");for(let n=0;n+1<e.byteLength;n+=2)if(Math.abs(e.readInt16LE(n))>ku)return!1;return!0}var bt=class{id=U("conn");ws;binding;onClosed;config=tr();sessionId=U("sess");conversationId=U("conv");items=[];pendingAudioBytes=[];pendingAudioTotalBytes=0;frozenAudio=[];inFlight=null;activeResponseId=null;responseFlushed=!1;cancelled=!1;voiceClaimed=!1;audioLaneOwner=!1;audioUnsubscribe=null;outOfTurnAudioWarned=!1;audioOnlyTranscriptGapWarned=!1;closed=!1;turnLaneAbort=null;turnTranscript={open:!1,text:""};lastCommittedTranscript=null;constructor(e){this.ws=e.ws,this.binding=e.binding,this.onClosed=e.onClosed,this.binding.model&&(this.config.model=this.binding.model),this.ws.on("message",n=>{this.onMessage(n)}),this.ws.on("close",()=>this.teardown("client disconnected")),this.ws.on("error",()=>this.teardown("client socket error")),this.send({type:"session.created",event_id:U("event"),session:en(this.config,this.sessionId)}),this.send({type:"conversation.created",event_id:U("event"),conversation:{id:this.conversationId,object:"realtime.conversation"}}),this.binding.turns&&this.startTurnLane()}startTurnLane(){let e=new AbortController;this.turnLaneAbort=e;let n=this.binding.turns;(async()=>{try{for await(let r of n()){if(this.closed||e.signal.aborted)return;this.onTurnEvent(r)}}catch(r){!this.closed&&!e.signal.aborted&&this.send(Y(vt("turn_lane_fault",`the backend turn/transcript lane failed: ${r instanceof Error?r.message:String(r)}`)))}})()}onTurnEvent(e){switch(e.kind){case"delta":case"final":{let n=typeof e.text=="string"?e.text:"";if(n.length===0)return;this.turnTranscript.open=!0,this.turnTranscript.text+=n,this.config.inputAudioTranscription&&this.send({type:"conversation.item.input_audio_transcription.delta",event_id:U("event"),item_id:"input_audio_turn",content_index:0,delta:n});return}case"turn_start":{this.turnTranscript={open:!0,text:this.turnTranscript.text},this.send({type:"input_audio_buffer.speech_started",event_id:U("event"),item_id:"input_audio_turn",audio_start_ms:typeof e.startMs=="number"?e.startMs:0}),this.config.turnDetection?.interruptResponse&&this.inFlight&&this.cancelResponse();return}case"turn_end":{let n=this.turnTranscript.text.trim(),r=n.length>0?n:null;this.turnTranscript={open:!1,text:""},this.lastCommittedTranscript=r,this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let o=U("item"),i=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem({id:o,type:"message",role:"user",content:[{type:"input_audio",...r!==null?{transcript:r}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:U("event"),previous_item_id:i,item_id:o}),this.send({type:"conversation.item.created",event_id:U("event"),previous_item_id:i,item:this.itemToProtocol(this.items[this.items.length-1])}),this.send({type:"input_audio_buffer.speech_stopped",event_id:U("event"),item_id:o,audio_end_ms:typeof e.startMs=="number"?e.startMs:0}),this.config.inputAudioTranscription&&r!==null&&this.send({type:"conversation.item.input_audio_transcription.completed",event_id:U("event"),item_id:o,content_index:0,transcript:r}),this.config.turnDetection?.createResponse&&!this.inFlight&&this.startResponse({});return}case"turn_eager_end":case"turn_resumed":return;default:return}}takeTurnTranscript(){let e=this.lastCommittedTranscript;return this.lastCommittedTranscript=null,e}async onMessage(e){if(this.closed)return;let n;try{n=e.toString()}catch{this.fail(oe("decode_error","frame is not decodable text"),"invalid_frame");return}if(Buffer.byteLength(n,"utf8")>Xt){this.fail(oe("frame_too_large",`frame exceeds the ${Xt}-byte limit`),"invalid_frame");return}let r;try{r=JSON.parse(n)}catch{this.fail(oe("invalid_json","frame is not valid JSON"),"invalid_frame");return}if(typeof r.type!="string"||r.type.length===0){this.fail(oe("missing_type",'event is missing a string "type"'),"invalid_frame");return}try{await this.handle(r)}catch(o){let i=o;if(i&&typeof i=="object"&&i.name==="SessionUpdateError"){this.send(Y({type:"invalid_request_error",code:i.code??"invalid_value",message:i.message,param:i.param??null}));return}this.fail(vt(null,o instanceof Error?o.message:String(o)),"handler_fault")}}async handle(e){switch(e.type){case"session.update":this.config=wt(e.session,this.config,{voice:this.binding.voice,turns:this.binding.turns!==void 0}),this.send({type:"session.updated",event_id:U("event"),session:en(this.config,this.sessionId)});return;case"conversation.item.create":{let n=nr(e.item,this.binding.voice),r=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem(n),this.send({type:"conversation.item.created",event_id:U("event"),previous_item_id:r,item:this.itemToProtocol(n)});return}case"conversation.item.delete":{let n=this.items.findIndex(r=>r.id===e.item_id);if(n<0){this.send(Y(oe("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.items.splice(n,1),this.send({type:"conversation.item.deleted",event_id:U("event"),item_id:e.item_id});return}case"conversation.item.retrieve":{let n=this.items.find(r=>r.id===e.item_id);if(!n){this.send(Y(oe("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.send({type:"conversation.item.retrieved",event_id:U("event"),item:this.itemToProtocol(n)});return}case"conversation.item.truncate":{let n=this.items.find(r=>r.id===e.item_id);if(!n||n.type!=="message"||n.role!=="assistant"){this.send(Y(oe("item_not_found",`no assistant message item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}n.truncated=!0,this.send({type:"conversation.item.truncated",event_id:U("event"),item_id:n.id,content_index:typeof e.content_index=="number"?e.content_index:0,audio_end_ms:typeof e.audio_end_ms=="number"?e.audio_end_ms:0});return}case"input_audio_buffer.append":{if(typeof e.audio!="string"||e.audio.length===0){this.send(Y(oe("invalid_audio",'input_audio_buffer.append requires base64 "audio"',"audio")));return}if(!this.ensureVoice())return;if(!bu.test(e.audio)){this.send(Y(oe("invalid_audio","input_audio_buffer.append audio must be base64","audio")));return}let n=Buffer.byteLength(e.audio,"base64");if(this.pendingAudioBytes.length+n>Zt){this.send(Y(oe("buffer_full",`uncommitted input audio exceeds the ${Zt}-byte bound \u2014 commit or clear first`,"audio")));return}if(this.inFlight){this.binding.audioSession.audio.appendInputAudio(e.audio);return}this.pendingAudioBytes.push(e.audio),this.pendingAudioTotalBytes+=n;return}case"input_audio_buffer.clear":{if(!this.ensureVoice())return;this.pendingAudioBytes.length=0,this.pendingAudioTotalBytes=0,this.send({type:"input_audio_buffer.cleared",event_id:U("event")});return}case"input_audio_buffer.commit":{if(!this.ensureVoice())return;if(this.pendingAudioTotalBytes===0&&this.takeTurnTranscript()===null){this.send(Y(oe("empty_buffer","input_audio_buffer.commit with no buffered audio","audio")));return}this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let n=this.takeTurnTranscript();n===null&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let r=U("item"),o=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem({id:r,type:"message",role:"user",content:[{type:"input_audio",...n!==null?{transcript:n}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:U("event"),previous_item_id:o,item_id:r}),this.send({type:"conversation.item.created",event_id:U("event"),previous_item_id:o,item:this.itemToProtocol(this.items[this.items.length-1])});return}case"output_audio_buffer.clear":{this.send(Y(oe("output_clear_unavailable","output_audio_buffer.clear has no server-side playback queue to flush on this adapter: output audio is delivered as response.output_audio.delta events and interrupted turns are stopped with response.cancel")));return}case"response.create":this.startResponse(e.response??{});return;case"response.cancel":this.cancelResponse();return;default:this.send(Y(oe("unknown_event",`event type ${e.type} is not supported by this adapter`)))}}ensureVoice(){return!this.binding.voice||!this.binding.audioSession?(this.send(Y(oe("audio_not_supported","audio events require a voice binding from the resolver \u2014 this session was resolved text-only","audio"))),!1):(this.voiceClaimed||(this.claimVoiceBinding(),this.audioUnsubscribe=this.binding.audioSession.audio.onOutputAudio(e=>{this.onOutputAudioFrame(this.activeResponseId??"",e)}),this.voiceClaimed=!0),!0)}claimVoiceBinding(){let e=this.binding.audioSession,n=e.openaiRealtimeClaims??(e.openaiRealtimeClaims=new Set);if(n.has(e))throw new Error("voice binding already owned by another connection \u2014 the resolver handed one audio bridge to two realtime conversations, which must never happen (independent callers require private bindings)");n.add(e),this.audioLaneOwner=!0}releaseVoiceBinding(){let e=this.binding.audioSession;!e||!this.audioLaneOwner||(e.openaiRealtimeClaims?.delete(e),this.audioLaneOwner=!1)}warnAudioOnlyTranscriptGap(){this.audioOnlyTranscriptGapWarned||(this.audioOnlyTranscriptGapWarned=!0,this.send(Y({type:"invalid_request_error",code:"audio_input_transcript_unavailable",message:"committed input audio is forwarded to the runtime audio lane but is NOT model-visible in the text turn lane: the resolved backend wires no turn/transcript lane (the voice engine's own stt stream with turn_start/turn_end + transcripts). Wire the transcribe/turn-detection leg upstream (the resolver supplies binding.turns) and committed audio gains its REAL transcript as the model-visible turn.",param:"input_audio_buffer.commit"})))}async upstreamStream(e,n){let r=this.binding.session;if(r&&typeof r.sendResponseCreate=="function")return r.sendResponseCreate(e,e.input??[],n);let o=this.binding.clients;if(o&&typeof o.createResponse=="function")return await o.createResponse(e);throw new Error("realtime binding provides neither session.sendResponseCreate nor clients.createResponse \u2014 no upstream turn seam")}startResponse(e){if(this.inFlight){this.send(Y(oe("response_in_progress","a response is already in progress \u2014 cancel it with response.cancel first","response.create")));return}let n=e??{};if(n.modalities!==void 0){if(!Array.isArray(n.modalities)||n.modalities.some(w=>w!=="text"&&w!=="audio")){this.send(Y(oe("invalid_modalities",'response.modalities must be "text"/"audio"',"response.modalities")));return}if(n.modalities.includes("audio")&&!this.ensureVoice())return}if(n.input!==void 0){this.send(Y(oe("response_input_unsupported","response.input is not supported by this adapter: build the context with conversation.item.create, then response.create","response.input")));return}this.cancelled=!1,this.responseFlushed=!1,this.messageItemAdded=!1;let r=U("req"),o=`resp_${r}`;this.activeResponseId=o;let i=this.items.length,{input:s,sawAudioOnlyItem:a}=rr(this.items),u=typeof n.instructions=="string"?n.instructions:this.config.instructions,d=typeof n.temperature=="number"?n.temperature:this.config.temperature,l=n.max_output_tokens??this.config.maxOutputTokens,c=l==="inf"||l===void 0?void 0:l,y=n.tools!==void 0?n.tools:this.config.tools,g=n.tool_choice!==void 0?n.tool_choice:this.config.toolChoice,E={input:s,stream:!0,...this.config.model?{model:this.config.model}:{},...u!==void 0?{instructions:u}:{},...d!==void 0?{temperature:d}:{},...c!==void 0?{max_output_tokens:c}:{},...y!==void 0?{tools:y}:{},...g!==void 0?{tool_choice:g}:{}};for(let w of this.frozenAudio.splice(0))this.binding.audioSession?.audio.appendInputAudio(w);this.inFlight=(async()=>{let w=[],h=B=>{this.responseFlushed?this.send(B):w.push(B)},b=()=>{if(!this.responseFlushed){this.send({type:"response.created",event_id:U("event"),response:this.responseObject(o,"in_progress")}),this.send({type:"response.in_progress",event_id:U("event"),response:this.responseObject(o,"in_progress")});for(let B of w)this.send(B);w.length=0,this.responseFlushed=!0}},R={messageOpen:!1,messageText:"",tools:new Map,nextOutputIndex:0},T=null;try{a&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let B;try{B=await this.upstreamStream(E,r)}catch(M){this.emitUpstreamFault(M,o);return}try{for await(let M of B){if(this.cancelled)break;let $=M;if($.type==="error"){let k=this.upstreamErrorBody($);this.responseFlushed?this.send(Y(k)):this.send({type:"response.failed",event_id:U("event"),response:this.failedResponse(o,k)});return}if($.type==="response.output_text.delta"&&typeof $.delta=="string"){b(),R.messageOpen||(this.beginMessageItem(o,R.nextOutputIndex++),R.messageOpen=!0),h({type:"response.output_text.delta",event_id:U("event"),item_id:`${o}_msg_0`,response_id:o,output_index:0,content_index:0,delta:$.delta}),R.messageText+=$.delta;continue}if($.type==="response.function_call_arguments.delta"){b();let k=typeof $.delta=="string"?$.delta:"",H=typeof $.call_id=="string"&&$.call_id.length>0?$.call_id:`${o}_fc_${R.nextOutputIndex}`,W=R.tools.get(H);if(!W){let z=typeof $.name=="string"&&$.name.length>0?$.name:void 0;W={outputIndex:R.nextOutputIndex++,name:z,args:""},R.tools.set(H,W),h({type:"response.output_item.added",event_id:U("event"),response_id:o,output_index:W.outputIndex,item:{id:H,object:"realtime.item",type:"function_call",call_id:H,name:z??"",arguments:"",status:"in_progress"}})}typeof $.name=="string"&&$.name.length>0&&(W.name=$.name),W.args+=k,h({type:"response.function_call_arguments.delta",event_id:U("event"),item_id:H,response_id:o,output_index:W.outputIndex,call_id:H,name:W.name??"",delta:k});continue}if($.type==="response.completed"){T=$.response??null;continue}}}catch(M){this.emitUpstreamFault(M,o);return}if(this.cancelled)return;this.closeOpenItems(o,R,i),b(),this.send({type:"response.done",event_id:U("event"),response:this.doneResponse(o,i,T)})}finally{this.inFlight=null,this.activeResponseId=null}})()}onOutputAudioFrame(e,n){if(this.cancelled)return;if(this.activeResponseId!==e){if(xu(n))return;!this.outOfTurnAudioWarned&&!this.closed&&(this.outOfTurnAudioWarned=!0,this.send(Y({type:"invalid_request_error",code:"out_of_turn_audio",message:"the runtime produced voice OUT outside a response turn; this adapter delivers output audio only within response.output_audio.delta of an open turn \u2014 wire server-driven turns (VAD) upstream or keep speech inside response turns"})));return}this.responseFlushed||(this.send({type:"response.created",event_id:U("event"),response:this.responseObject(e,"in_progress")}),this.send({type:"response.in_progress",event_id:U("event"),response:this.responseObject(e,"in_progress")}),this.responseFlushed=!0);let r=`${e}_msg_0`;this.messageItemOpenForAudio||(this.beginAudioMessageItem(e),this.messageItemOpenForAudio=!0),this.send({type:"response.output_audio.delta",event_id:U("event"),item_id:r,response_id:e,output_index:0,content_index:0,delta:n})}messageItemAdded=!1;messageItemOpenForAudio=!1;beginMessageItem(e,n){let r=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:U("event"),response_id:e,output_index:n,item:{id:r,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:U("event"),item_id:r,response_id:e,output_index:n,content_index:0,part:{type:"output_text",text:""}})}beginAudioMessageItem(e){let n=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:U("event"),response_id:e,output_index:0,item:{id:n,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:U("event"),item_id:n,response_id:e,output_index:0,content_index:0,part:{type:"output_audio",transcript:""}})}closeOpenItems(e,n,r){if(n.messageOpen||this.messageItemOpenForAudio){let o=`${e}_msg_0`,i=this.messageItemOpenForAudio;i&&this.send({type:"response.output_audio.done",event_id:U("event"),item_id:o,response_id:e,output_index:0,content_index:0}),n.messageText.length>0&&this.send({type:"response.output_text.done",event_id:U("event"),item_id:o,response_id:e,output_index:0,content_index:0,text:n.messageText}),this.send({type:"response.content_part.done",event_id:U("event"),item_id:o,response_id:e,output_index:0,content_index:0,part:i?{type:"output_audio",transcript:""}:{type:"output_text",text:n.messageText}});let s=[];i&&s.push({type:"output_audio"}),n.messageText.length>0&&s.push({type:"output_text",text:n.messageText});let a={id:o,type:"message",role:"assistant",content:s};this.pushItem(a),this.send({type:"response.output_item.done",event_id:U("event"),response_id:e,output_index:0,item:this.itemToProtocol(a)})}for(let[o,i]of n.tools){let s=i.args.length>0?i.args:"{}";this.send({type:"response.function_call_arguments.done",event_id:U("event"),item_id:o,response_id:e,output_index:i.outputIndex,call_id:o,name:i.name??"",arguments:s});let a={id:o,type:"function_call",callId:o,name:i.name,arguments:s};this.pushItem(a),this.send({type:"response.output_item.done",event_id:U("event"),response_id:e,output_index:i.outputIndex,item:this.itemToProtocol(a)})}this.messageItemOpenForAudio=!1,this.messageItemAdded=!1}responseObject(e,n,r){return{id:e,object:"realtime.response",status:n,conversation_id:this.conversationId,usage:null,...r??{}}}failedResponse(e,n){return this.responseObject(e,"failed",{status_details:{error:{code:n.code,message:n.message}}})}doneResponse(e,n,r){let o=r&&typeof r.usage=="object"&&r.usage!==null?r.usage:null,i=[];for(let s=n;s<this.items.length;s++)i.push(this.itemToProtocol(this.items[s]));return this.responseObject(e,"completed",{output:i,usage:o?{input_tokens:typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0,output_tokens:typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0,total_tokens:typeof o.total_tokens=="number"&&Number.isFinite(o.total_tokens)?o.total_tokens:(typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0)+(typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0),input_token_details:{cached_tokens:0,audio_tokens:0,text_tokens:0},output_token_details:{audio_tokens:0,text_tokens:0}}:null})}upstreamErrorBody(e){let n=e.error,r=n&&typeof n.message=="string"?n.message:JSON.stringify(e);return{type:"server_error",code:n&&typeof n.code=="string"?n.code:null,message:r}}emitUpstreamFault(e,n){if(this.cancelled)return;let r=vt(null,e instanceof Error?e.message:String(e));this.responseFlushed?this.send(Y(r)):this.send({type:"response.failed",event_id:U("event"),response:this.failedResponse(n,r)})}cancelResponse(){let e=this.inFlight;if(!e){this.send(Y(oe("no_response_to_cancel","no response is in progress","response.cancel")));return}this.cancelled=!0;let n=this.activeResponseId??"resp_";e.finally(()=>{this.send({type:"response.cancelled",event_id:U("event"),response:this.responseObject(n,"cancelled")})})}pushItem(e){this.items.push(e),this.items.length>Qt&&(this.items.shift(),this.send(Y({type:"invalid_request_error",code:"transcript_evicted",message:`the conversation transcript exceeded ${Qt} items \u2014 the oldest item was evicted to keep the per-connection bound`})))}itemToProtocol(e){let n={id:e.id,object:"realtime.item"};if(e.type==="function_call")return{...n,type:"function_call",call_id:e.callId,name:e.name,arguments:e.arguments??"{}",status:"completed"};if(e.type==="function_call_output")return{...n,type:"function_call_output",call_id:e.callId,output:e.output};let r=(e.content??[]).map(o=>o.type==="input_text"||o.type==="output_text"?{type:o.type,text:o.text}:o.type==="input_audio"?{type:"input_audio",...o.transcript?{transcript:o.transcript}:{}}:{type:"output_audio",transcript:o.transcript??""});return{...n,type:"message",role:e.role,status:e.truncated?"incomplete":"completed",content:r}}send(e){if(!(this.closed||this.ws.readyState!==this.ws.OPEN)){if(this.ws.bufferedAmount>er){this.ws.close(wu,mi("outbound buffer exceeded")),this.teardown("outbound buffer exceeded");return}this.ws.send(JSON.stringify(e))}}fail(e,n){this.send(Y(e)),this.ws.close(vu,mi(n)),this.teardown(n)}teardown(e){if(!this.closed){this.closed=!0,this.turnLaneAbort?.abort(),this.turnLaneAbort=null,this.releaseVoiceBinding(),this.audioUnsubscribe?.(),this.audioUnsubscribe=null,this.binding.release&&Promise.resolve().then(()=>this.binding.release?.()).catch(n=>gi(this.id,`binding release failed: ${n instanceof Error?n.message:String(n)}`));try{this.ws.removeAllListeners("message")}catch{}this.onClosed?.(this),gi(this.id,e)}}},or=[];function gi(t,e){or.push({id:t,reason:e}),or.length>256&&or.shift()}var kt="/v1/realtime",Ru=t=>t.slice(0,120);function Su(t,e,n){let r=e===401||e===403?"Unauthorized":e===502?"Upstream Fault":"Upgrade Refused";t.write(`HTTP/1.1 ${e} ${r}\r
|
|
22
|
+
${o}`)}function Er(t,e){let n=new Je.WebSocketServer({noServer:!0,maxPayload:pd}),r=new nt,o=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,u=!1,d=(l,p,y)=>{let c;try{c=new URL(l.url??"/","http://localhost")}catch{mn(p,400,"malformed upgrade URL");return}if(p.on("error",()=>p.destroy()),p.on("close",()=>i.delete(p)),c.pathname!==Rr){i.add(p);return}s.add(p);let m=!1;(async()=>{if(e.apiKey&&(l.headers.authorization??"")!==`Bearer ${e.apiKey}`){mn(p,401,"invalid local proxy api key");return}let w;try{w=await a(l)}catch(R){let h=e.statusOf?.(R)??null;mn(p,h?.status??500,"upstream resolver rejected the connection");return}u||p.destroyed||(m=!0,n.handleUpgrade(l,p,y,R=>{s.delete(p),o.add(R),R.on("close",()=>o.delete(R));try{new Sr(R,w,r,e.statusOf)}catch{o.delete(R),R.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!m&&!p.destroyed&&mn(p,500,"responses websocket upgrade failed")}).finally(()=>s.delete(p))};return t.on("upgrade",d),{stats:r,close:()=>{let{promise:l,resolve:p}=xr();if(u)return p(),l;u=!0,t.off("upgrade",d);for(let c of s)c.destroy();s.clear();for(let c of i)c.destroy();i.clear();for(let c of o)c.close(1001,"server shutting down");let y=setTimeout(()=>{for(let c of o)c.readyState!==Je.WebSocket.CLOSED&&c.terminate()},2e3);return y.unref?.(),n.close(()=>{clearTimeout(y),p()}),l}}}k();k();var ei=require("@urun-sh/core");k();var Ks=require("@urun-sh/core"),wd={no_capacity:429,queue_timeout:429,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502},_d={no_capacity:"no ready GPU capacity for this model right now \u2014 the platform will cold-wake it on request; retry",queue_timeout:"the session was admitted but never became live within the cold-wake fence \u2014 the backend is busy or wedged; retry",cold_start_exceeded:"the cold boot exceeded its budget without serving \u2014 the platform keeps pursuing the wake; retry with a longer client timeout",queue_full:"the admission queue for this model is full \u2014 retry after the Retry-After period",admission_busy:"admission is momentarily contended (lock convoy) \u2014 retry shortly",admission_unavailable:"session admission is temporarily unavailable \u2014 retry",backend_error:"the control plane failed while admitting the session"};function Or(t,e){let n=_d[t.code];return e?`${n} (${e})`:n}function gn(t){if(t==null)return null;let e=wd[t];return e===void 0?null:{status:e,code:t,retryable:e!==502,openaiType:e===429?"capacity_error":"upstream_error",anthropicType:e===429?"rate_limit_error":e===503?"overloaded_error":"api_error"}}function hn(t){if(t.kind==="create-failed")return gn(t.status.error?.code??null);if(t.kind!=="never-live")return null;let e=(t.status.elapsedMs??0)>=Ks.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return gn(e?"cold_start_exceeded":"queue_timeout")}var ti=require("ws");k();var Zs=require("@urun-sh/core");k();var Ys=require("crypto");function N(t){return`${t}_${(0,Ys.randomUUID)().replace(/-/g,"").slice(0,24)}`}var yn=4*1024*1024,Ar=8*1024*1024,wn=512,_n=20*1024*1024;function ae(t,e,n){return{type:"invalid_request_error",code:t,message:e,param:n??null}}function yt(t,e){return{type:"server_error",code:t,message:e}}function Q(t){return{type:"error",event_id:N("event"),error:t}}function Ir(){return{outputModalities:["text"],turnDetection:null,inputAudioTranscription:!1}}var j=class extends Error{constructor(n,r,o="invalid_value"){super(n);this.param=r;this.code=o;this.name="SessionUpdateError"}param;code},vd={model:!0,instructions:!0,modalities:!0,voice:!0,input_audio_format:!0,output_audio_format:!0,temperature:!0,max_output_tokens:!0,max_response_output_tokens:!0,tool_choice:!0,tools:!0,metadata:!0,turn_detection:!0,input_audio_transcription:!0,input_audio_noise_reduction:!0,audio:!0},bd={format:!0,transcription:!0,turn_detection:!0,noise_reduction:!0},kd={format:!0,voice:!0,speed:!0};function zs(t,e){let n=t;if(!n||typeof n!="object"||n.type!=="audio/pcm"||n.rate!==void 0&&n.rate!==24e3)throw new j(`${e} must be {"type":"audio/pcm","rate":24000} \u2014 the session audio lane is 24 kHz mono PCM16 only`,e)}function Ut(t,e,n){if(!t||typeof t!="object"||Array.isArray(t))throw new j("session must be an object","session","invalid_request");let r=t,o=Object.keys(r).filter(i=>!(i in vd));if(o.length>0)throw new j(`session field(s) not supported by this backend: ${o.join(", ")}`,o[0],"unsupported_field");let s={...e,outputModalities:[...e.outputModalities]};for(let[i,a]of Object.entries(r))switch(i){case"model":if(a!==null&&typeof a!="string")throw new j("model must be a string or null","model");s.model=a??void 0;break;case"instructions":if(a!==null&&typeof a!="string")throw new j("instructions must be a string or null","instructions");s.instructions=a??void 0;break;case"modalities":{if(!Array.isArray(a)||a.length===0)throw new j("modalities must be a non-empty array","modalities");let u=[];for(let d of a){if(d!=="text"&&d!=="audio")throw new j(`modality ${JSON.stringify(d)} is not "text" or "audio"`,"modalities");if(d==="audio"&&!n.voice)throw new j('modality "audio" refused: the resolver did not provide an authoritative voice binding for the resolved backend \u2014 this model cannot speak on this session',"modalities","audio_not_supported");u.includes(d)||u.push(d)}s.outputModalities=u;break}case"voice":if(a===null){delete s.voice;break}if(typeof a!="string")throw new j("voice must be a string or null","voice");if(!n.voice)throw new j("voice refused: no authoritative voice binding for the resolved backend","voice","audio_not_supported");s.voice=a;break;case"input_audio_format":case"output_audio_format":if(a!=="pcm16")throw new j(`${i} must be "pcm16" \u2014 the session audio lane is 24 kHz mono PCM16 only`,i);break;case"temperature":if(a!==null&&(typeof a!="number"||!Number.isFinite(a)))throw new j("temperature must be a finite number or null","temperature");s.temperature=a??void 0;break;case"max_output_tokens":case"max_response_output_tokens":if(a!==null&&a!=="inf"&&(typeof a!="number"||!Number.isInteger(a)||a<1))throw new j(`${i} must be a positive integer, "inf" or null`,i);s.maxOutputTokens=a===null?void 0:a;break;case"tools":if(!Array.isArray(a))throw new j("tools must be an array","tools");s.tools=a;break;case"tool_choice":s.toolChoice=a;break;case"metadata":if(a!==null&&(!a||typeof a!="object"||Array.isArray(a)))throw new j("metadata must be an object or null","metadata");s.metadata=a??null;break;case"turn_detection":{if(a===null){s.turnDetection=null;break}if(!n.turns)throw new j("turn_detection is refused: the resolved backend does not wire its own turn boundary lane (stt stream with turn_start/turn_end) \u2014 there is no native VAD source to drive responses from. Drive turns manually: input_audio_buffer.commit + response.create.","turn_detection","unsupported_field");let u=a;if(!u||typeof u!="object"||Array.isArray(u))throw new j("turn_detection must be an object or null","turn_detection");let d=String(u.type??"");if(d!=="server_vad"&&d!=="semantic_vad")throw new j(`turn_detection.type ${JSON.stringify(u.type)} is not "server_vad"/"semantic_vad"`,"turn_detection.type");s.turnDetection={createResponse:u.create_response!==!1,interruptResponse:u.interrupt_response!==!1};break}case"input_audio_transcription":{if(a===null){s.inputAudioTranscription=!1;break}if(!n.turns)throw new j("input_audio_transcription is refused: the resolved backend does not wire a transcript lane \u2014 no native transcription source exists behind this session","input_audio_transcription","unsupported_field");if(!a||typeof a!="object"||Array.isArray(a))throw new j("input_audio_transcription must be an object or null","input_audio_transcription");for(let u of Object.keys(a))if(!["model","language","languages","prompt"].includes(u))throw new j(`input_audio_transcription field not honored by this backend: ${u} (transcription comes from the voice engine itself)`,`input_audio_transcription.${u}`,"unsupported_field");s.inputAudioTranscription=!0;break}case"input_audio_noise_reduction":if(a!==null)throw new j("input_audio_noise_reduction must be null on this backend: no noise-reduction primitive exists behind the session audio bridge","input_audio_noise_reduction","unsupported_field");break;case"audio":{if(a===null)break;let u=a;if(!u||typeof u!="object"||Array.isArray(u))throw new j("audio must be an object or null","audio");if(u.input!==void 0&&u.input!==null){let d=u.input;if(!d||typeof d!="object"||Array.isArray(d))throw new j("audio.input must be an object","audio.input");for(let l of Object.keys(d))if(!(l in bd))throw new j(`audio.input field not supported by this backend: ${l}`,`audio.input.${l}`,"unsupported_field");if(d.format!==void 0&&zs(d.format,"audio.input.format"),d.transcription!==null&&d.transcription!==void 0){let l=Ut({input_audio_transcription:d.transcription},s,n);s.inputAudioTranscription=l.inputAudioTranscription}if(d.turn_detection!==null&&d.turn_detection!==void 0){let l=Ut({turn_detection:d.turn_detection},s,n);s.turnDetection=l.turnDetection}if(d.noise_reduction!==null&&d.noise_reduction!==void 0)throw new j("audio.input.noise_reduction must be null on this backend: no noise-reduction primitive","audio.input.noise_reduction","unsupported_field")}if(u.output!==void 0&&u.output!==null){let d=u.output;if(!d||typeof d!="object"||Array.isArray(d))throw new j("audio.output must be an object","audio.output");for(let l of Object.keys(d))if(!(l in kd))throw new j(`audio.output field not supported by this backend: ${l}`,`audio.output.${l}`,"unsupported_field");if(d.format!==void 0&&zs(d.format,"audio.output.format"),d.speed!==void 0)throw new j("audio.output.speed is not supported on this backend: playback rate is fixed","audio.output.speed","unsupported_field");if(d.voice!==void 0)if(d.voice===null)delete s.voice;else{if(typeof d.voice!="string")throw new j("audio.output.voice must be a string or null","audio.output.voice");if(!n.voice)throw new j("audio.output.voice refused: no authoritative voice binding for the resolved backend","audio.output.voice","audio_not_supported");s.voice=d.voice}d.format!==void 0&&!s.outputModalities.includes("audio")&&(s.outputModalities=["text","audio"])}break}default:break}return s}function vn(t,e){return{id:e,object:"realtime.session",model:t.model??null,instructions:t.instructions??"",modalities:[...t.outputModalities],voice:t.voice??null,input_audio_format:"pcm16",output_audio_format:"pcm16",input_audio_transcription:t.inputAudioTranscription?{}:null,input_audio_noise_reduction:null,turn_detection:t.turnDetection?{type:"server_vad",create_response:t.turnDetection.createResponse,interrupt_response:t.turnDetection.interruptResponse}:null,temperature:t.temperature??null,max_response_output_tokens:t.maxOutputTokens??"inf",tool_choice:t.toolChoice??"auto",tools:t.tools??[],metadata:t.metadata??null}}function Tr(t,e){let n=t;if(!n||typeof n!="object")throw new j("item must be an object","item","invalid_request");if(n.type==="function_call_output"){if(typeof n.call_id!="string"||typeof n.output!="string")throw new j("function_call_output item requires string call_id and output","item.call_id","invalid_request");return{id:typeof n.id=="string"?n.id:N("item"),type:"function_call_output",callId:n.call_id,output:n.output}}if(n.type==="message"){let r=n.role;if(r!=="user"&&r!=="assistant"&&r!=="system")throw new j('message item role must be "user", "assistant" or "system"',"item.role","invalid_request");if(!Array.isArray(n.content))throw new j("message item content must be an array","item.content","invalid_request");let o=[];for(let s of n.content){let i=s;if(!i||typeof i!="object")throw new j("content part must be an object","item.content","invalid_request");if(i.type==="input_text"||r==="assistant"&&i.type==="output_text"){if(typeof i.text!="string")throw new j(`${String(i.type)} part requires string text`,"item.content.text","invalid_request");o.push({type:i.type,text:i.text});continue}if(i.type==="input_audio"){if(!e)throw new j("input_audio content refused: no authoritative voice binding for the resolved backend","item.content.type","audio_not_supported");if(typeof i.audio!="string")throw new j("input_audio part requires base64 audio","item.content.audio","invalid_request");o.push({type:"input_audio",...typeof i.transcript=="string"?{transcript:i.transcript}:{}});continue}throw new j(`content part type ${String(i.type)} is not supported on message items here (input_text / output_text / input_audio)`,"item.content.type","invalid_request")}return{id:typeof n.id=="string"?n.id:N("item"),type:"message",role:r,content:o}}throw new j(`item type ${String(n.type)} is not supported \u2014 message and function_call_output items only`,"item.type","invalid_request")}function Pr(t){let e=[],n=!1;for(let r of t){if(r.type==="function_call"){e.push({type:"function_call",call_id:r.callId,name:r.name,arguments:r.arguments??"{}"});continue}if(r.type==="function_call_output"){e.push({type:"function_call_output",call_id:r.callId,output:r.output});continue}let o=[];for(let s of r.content??[]){if(s.type==="input_text"||s.type==="output_text"){o.push({type:s.type,text:s.text});continue}if(s.type==="input_audio"){let i=s.transcript;if(typeof i=="string"&&i.trim().length>0){o.push({type:"input_text",text:i});continue}n=!0;continue}}e.push({role:r.role,content:o})}return{input:e,sawAudioOnlyItem:n}}var Sd=1001,Rd=1011,Xs=t=>t.slice(0,120),xd=/^[A-Za-z0-9+/=]*$/,Ed=2;function Od(t){let e=Buffer.from(t,"base64");for(let n=0;n+1<e.byteLength;n+=2)if(Math.abs(e.readInt16LE(n))>Ed)return!1;return!0}var Nt=class{id=N("conn");ws;binding;onClosed;config=Ir();sessionId=N("sess");conversationId=N("conv");items=[];pendingAudioBytes=[];pendingAudioTotalBytes=0;frozenAudio=[];inFlight=null;activeResponseId=null;responseFlushed=!1;cancelled=!1;voiceClaimed=!1;audioLaneOwner=!1;audioUnsubscribe=null;outOfTurnAudioWarned=!1;audioOnlyTranscriptGapWarned=!1;closed=!1;turnLaneAbort=null;turnTranscript={open:!1,text:""};lastCommittedTranscript=null;constructor(e){this.ws=e.ws,this.binding=e.binding,this.onClosed=e.onClosed,this.binding.model&&(this.config.model=this.binding.model),this.ws.on("message",n=>{this.onMessage(n)}),this.ws.on("close",()=>this.teardown("client disconnected")),this.ws.on("error",()=>this.teardown("client socket error")),this.send({type:"session.created",event_id:N("event"),session:vn(this.config,this.sessionId)}),this.send({type:"conversation.created",event_id:N("event"),conversation:{id:this.conversationId,object:"realtime.conversation"}}),this.binding.turns&&this.startTurnLane()}startTurnLane(){let e=new AbortController;this.turnLaneAbort=e;let n=this.binding.turns;(async()=>{try{for await(let r of n()){if(this.closed||e.signal.aborted)return;this.onTurnEvent(r)}}catch(r){!this.closed&&!e.signal.aborted&&this.send(Q(yt("turn_lane_fault",`the backend turn/transcript lane failed: ${r instanceof Error?r.message:String(r)}`)))}})()}onTurnEvent(e){switch(e.kind){case"delta":case"final":{let n=typeof e.text=="string"?e.text:"";if(n.length===0)return;this.turnTranscript.open=!0,this.turnTranscript.text+=n,this.config.inputAudioTranscription&&this.send({type:"conversation.item.input_audio_transcription.delta",event_id:N("event"),item_id:"input_audio_turn",content_index:0,delta:n});return}case"turn_start":{this.turnTranscript={open:!0,text:this.turnTranscript.text},this.send({type:"input_audio_buffer.speech_started",event_id:N("event"),item_id:"input_audio_turn",audio_start_ms:typeof e.startMs=="number"?e.startMs:0}),this.config.turnDetection?.interruptResponse&&this.inFlight&&this.cancelResponse();return}case"turn_end":{let n=this.turnTranscript.text.trim(),r=n.length>0?n:null;this.turnTranscript={open:!1,text:""},this.lastCommittedTranscript=r,this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let o=N("item"),s=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem({id:o,type:"message",role:"user",content:[{type:"input_audio",...r!==null?{transcript:r}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:N("event"),previous_item_id:s,item_id:o}),this.send({type:"conversation.item.created",event_id:N("event"),previous_item_id:s,item:this.itemToProtocol(this.items[this.items.length-1])}),this.send({type:"input_audio_buffer.speech_stopped",event_id:N("event"),item_id:o,audio_end_ms:typeof e.startMs=="number"?e.startMs:0}),this.config.inputAudioTranscription&&r!==null&&this.send({type:"conversation.item.input_audio_transcription.completed",event_id:N("event"),item_id:o,content_index:0,transcript:r}),this.config.turnDetection?.createResponse&&!this.inFlight&&this.startResponse({});return}case"turn_eager_end":case"turn_resumed":return;default:return}}takeTurnTranscript(){let e=this.lastCommittedTranscript;return this.lastCommittedTranscript=null,e}async onMessage(e){if(this.closed)return;let n;try{n=e.toString()}catch{this.fail(ae("decode_error","frame is not decodable text"),"invalid_frame");return}if(Buffer.byteLength(n,"utf8")>yn){this.fail(ae("frame_too_large",`frame exceeds the ${yn}-byte limit`),"invalid_frame");return}let r;try{r=JSON.parse(n)}catch{this.fail(ae("invalid_json","frame is not valid JSON"),"invalid_frame");return}if(typeof r.type!="string"||r.type.length===0){this.fail(ae("missing_type",'event is missing a string "type"'),"invalid_frame");return}try{await this.handle(r)}catch(o){let s=o;if(s&&typeof s=="object"&&s.name==="SessionUpdateError"){this.send(Q({type:"invalid_request_error",code:s.code??"invalid_value",message:s.message,param:s.param??null}));return}this.fail(yt(null,o instanceof Error?o.message:String(o)),"handler_fault")}}async handle(e){switch(e.type){case"session.update":this.config=Ut(e.session,this.config,{voice:this.binding.voice,turns:this.binding.turns!==void 0}),this.send({type:"session.updated",event_id:N("event"),session:vn(this.config,this.sessionId)});return;case"conversation.item.create":{let n=Tr(e.item,this.binding.voice),r=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem(n),this.send({type:"conversation.item.created",event_id:N("event"),previous_item_id:r,item:this.itemToProtocol(n)});return}case"conversation.item.delete":{let n=this.items.findIndex(r=>r.id===e.item_id);if(n<0){this.send(Q(ae("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.items.splice(n,1),this.send({type:"conversation.item.deleted",event_id:N("event"),item_id:e.item_id});return}case"conversation.item.retrieve":{let n=this.items.find(r=>r.id===e.item_id);if(!n){this.send(Q(ae("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.send({type:"conversation.item.retrieved",event_id:N("event"),item:this.itemToProtocol(n)});return}case"conversation.item.truncate":{let n=this.items.find(r=>r.id===e.item_id);if(!n||n.type!=="message"||n.role!=="assistant"){this.send(Q(ae("item_not_found",`no assistant message item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}n.truncated=!0,this.send({type:"conversation.item.truncated",event_id:N("event"),item_id:n.id,content_index:typeof e.content_index=="number"?e.content_index:0,audio_end_ms:typeof e.audio_end_ms=="number"?e.audio_end_ms:0});return}case"input_audio_buffer.append":{if(typeof e.audio!="string"||e.audio.length===0){this.send(Q(ae("invalid_audio",'input_audio_buffer.append requires base64 "audio"',"audio")));return}if(!this.ensureVoice())return;if(!xd.test(e.audio)){this.send(Q(ae("invalid_audio","input_audio_buffer.append audio must be base64","audio")));return}let n=Buffer.byteLength(e.audio,"base64");if(this.pendingAudioBytes.length+n>_n){this.send(Q(ae("buffer_full",`uncommitted input audio exceeds the ${_n}-byte bound \u2014 commit or clear first`,"audio")));return}if(this.inFlight){this.binding.audioSession.audio.appendInputAudio(e.audio);return}this.pendingAudioBytes.push(e.audio),this.pendingAudioTotalBytes+=n;return}case"input_audio_buffer.clear":{if(!this.ensureVoice())return;this.pendingAudioBytes.length=0,this.pendingAudioTotalBytes=0,this.send({type:"input_audio_buffer.cleared",event_id:N("event")});return}case"input_audio_buffer.commit":{if(!this.ensureVoice())return;if(this.pendingAudioTotalBytes===0&&this.takeTurnTranscript()===null){this.send(Q(ae("empty_buffer","input_audio_buffer.commit with no buffered audio","audio")));return}this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let n=this.takeTurnTranscript();n===null&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let r=N("item"),o=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem({id:r,type:"message",role:"user",content:[{type:"input_audio",...n!==null?{transcript:n}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:N("event"),previous_item_id:o,item_id:r}),this.send({type:"conversation.item.created",event_id:N("event"),previous_item_id:o,item:this.itemToProtocol(this.items[this.items.length-1])});return}case"output_audio_buffer.clear":{this.send(Q(ae("output_clear_unavailable","output_audio_buffer.clear has no server-side playback queue to flush on this adapter: output audio is delivered as response.output_audio.delta events and interrupted turns are stopped with response.cancel")));return}case"response.create":this.startResponse(e.response??{});return;case"response.cancel":this.cancelResponse();return;default:this.send(Q(ae("unknown_event",`event type ${e.type} is not supported by this adapter`)))}}ensureVoice(){return!this.binding.voice||!this.binding.audioSession?(this.send(Q(ae("audio_not_supported","audio events require a voice binding from the resolver \u2014 this session was resolved text-only","audio"))),!1):(this.voiceClaimed||(this.claimVoiceBinding(),this.audioUnsubscribe=this.binding.audioSession.audio.onOutputAudio(e=>{this.onOutputAudioFrame(this.activeResponseId??"",e)}),this.voiceClaimed=!0),!0)}claimVoiceBinding(){let e=this.binding.audioSession,n=e.openaiRealtimeClaims??(e.openaiRealtimeClaims=new Set);if(n.has(e))throw new Error("voice binding already owned by another connection \u2014 the resolver handed one audio bridge to two realtime conversations, which must never happen (independent callers require private bindings)");n.add(e),this.audioLaneOwner=!0}releaseVoiceBinding(){let e=this.binding.audioSession;!e||!this.audioLaneOwner||(e.openaiRealtimeClaims?.delete(e),this.audioLaneOwner=!1)}warnAudioOnlyTranscriptGap(){this.audioOnlyTranscriptGapWarned||(this.audioOnlyTranscriptGapWarned=!0,this.send(Q({type:"invalid_request_error",code:"audio_input_transcript_unavailable",message:"committed input audio is forwarded to the runtime audio lane but is NOT model-visible in the text turn lane: the resolved backend wires no turn/transcript lane (the voice engine's own stt stream with turn_start/turn_end + transcripts). Wire the transcribe/turn-detection leg upstream (the resolver supplies binding.turns) and committed audio gains its REAL transcript as the model-visible turn.",param:"input_audio_buffer.commit"})))}async upstreamStream(e,n){let r=this.binding.session;if(r&&typeof r.sendResponseCreate=="function"){let s=e.response??e;return r.sendResponseCreate({type:"response.create",response:s},e.input??[],n)}let o=this.binding.clients;if(o&&typeof o.createResponse=="function"){let{response:s,input:i,...a}=e;return await o.createResponse({...a,input:i})}throw new Error("realtime binding provides neither session.sendResponseCreate nor clients.createResponse \u2014 no upstream turn seam")}startResponse(e){if(this.inFlight){this.send(Q(ae("response_in_progress","a response is already in progress \u2014 cancel it with response.cancel first","response.create")));return}let n=e??{};if(n.modalities!==void 0){if(!Array.isArray(n.modalities)||n.modalities.some(w=>w!=="text"&&w!=="audio")){this.send(Q(ae("invalid_modalities",'response.modalities must be "text"/"audio"',"response.modalities")));return}if(n.modalities.includes("audio")&&!this.ensureVoice())return}if(n.input!==void 0){this.send(Q(ae("response_input_unsupported","response.input is not supported by this adapter: build the context with conversation.item.create, then response.create","response.input")));return}this.cancelled=!1,this.responseFlushed=!1,this.messageItemAdded=!1;let r=N("req"),o=`resp_${r}`;this.activeResponseId=o;let s=this.items.length,{input:i,sawAudioOnlyItem:a}=Pr(this.items),u=typeof n.instructions=="string"?n.instructions:this.config.instructions,d=typeof n.temperature=="number"?n.temperature:this.config.temperature,l=n.max_output_tokens??this.config.maxOutputTokens,p=l==="inf"||l===void 0?void 0:l,y=n.tools!==void 0?n.tools:this.config.tools,c=n.tool_choice!==void 0?n.tool_choice:this.config.toolChoice,m={input:i,stream:!0,...this.config.model?{model:this.config.model}:{},...u!==void 0?{instructions:u}:{},...d!==void 0?{temperature:d}:{},...p!==void 0?{max_output_tokens:p}:{},...y!==void 0?{tools:y}:{},...c!==void 0?{tool_choice:c}:{}};for(let w of this.frozenAudio.splice(0))this.binding.audioSession?.audio.appendInputAudio(w);this.inFlight=(async()=>{let w=[],R=$=>{this.responseFlushed?this.send($):w.push($)},h=()=>{if(!this.responseFlushed){this.send({type:"response.created",event_id:N("event"),response:this.responseObject(o,"in_progress")}),this.send({type:"response.in_progress",event_id:N("event"),response:this.responseObject(o,"in_progress")});for(let $ of w)this.send($);w.length=0,this.responseFlushed=!0}},v={messageOpen:!1,messageText:"",tools:new Map,nextOutputIndex:0},b=null;try{a&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let $;try{$=await this.upstreamStream(m,r)}catch(S){this.upstreamTurnFault(S,o);return}try{for await(let S of $){if(this.cancelled)break;let E=S;if(E.type==="error"){let x=this.upstreamErrorBody(E);this.responseFlushed?this.send(Q(x)):this.send({type:"response.failed",event_id:N("event"),response:this.failedResponse(o,x)});return}if(E.type==="response.output_text.delta"&&typeof E.delta=="string"){h(),v.messageOpen||(this.beginMessageItem(o,v.nextOutputIndex++),v.messageOpen=!0),R({type:"response.output_text.delta",event_id:N("event"),item_id:`${o}_msg_0`,response_id:o,output_index:0,content_index:0,delta:E.delta}),v.messageText+=E.delta;continue}if(E.type==="response.function_call_arguments.delta"){h();let x=typeof E.delta=="string"?E.delta:"",L=typeof E.call_id=="string"&&E.call_id.length>0?E.call_id:`${o}_fc_${v.nextOutputIndex}`,B=v.tools.get(L);if(!B){let q=typeof E.name=="string"&&E.name.length>0?E.name:void 0;B={outputIndex:v.nextOutputIndex++,name:q,args:""},v.tools.set(L,B),R({type:"response.output_item.added",event_id:N("event"),response_id:o,output_index:B.outputIndex,item:{id:L,object:"realtime.item",type:"function_call",call_id:L,name:q??"",arguments:"",status:"in_progress"}})}typeof E.name=="string"&&E.name.length>0&&(B.name=E.name),B.args+=x,R({type:"response.function_call_arguments.delta",event_id:N("event"),item_id:L,response_id:o,output_index:B.outputIndex,call_id:L,name:B.name??"",delta:x});continue}if(E.type==="response.completed"){b=E.response??null;continue}}}catch(S){this.upstreamTurnFault(S,o);return}if(this.cancelled)return;this.closeOpenItems(o,v,s),h(),this.send({type:"response.done",event_id:N("event"),response:this.doneResponse(o,s,b)})}finally{this.inFlight=null,this.activeResponseId=null}})()}onOutputAudioFrame(e,n){if(this.cancelled)return;if(this.activeResponseId!==e){if(Od(n))return;!this.outOfTurnAudioWarned&&!this.closed&&(this.outOfTurnAudioWarned=!0,this.send(Q({type:"invalid_request_error",code:"out_of_turn_audio",message:"the runtime produced voice OUT outside a response turn; this adapter delivers output audio only within response.output_audio.delta of an open turn \u2014 wire server-driven turns (VAD) upstream or keep speech inside response turns"})));return}this.responseFlushed||(this.send({type:"response.created",event_id:N("event"),response:this.responseObject(e,"in_progress")}),this.send({type:"response.in_progress",event_id:N("event"),response:this.responseObject(e,"in_progress")}),this.responseFlushed=!0);let r=`${e}_msg_0`;this.messageItemOpenForAudio||(this.beginAudioMessageItem(e),this.messageItemOpenForAudio=!0),this.send({type:"response.output_audio.delta",event_id:N("event"),item_id:r,response_id:e,output_index:0,content_index:0,delta:n})}messageItemAdded=!1;messageItemOpenForAudio=!1;beginMessageItem(e,n){let r=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:N("event"),response_id:e,output_index:n,item:{id:r,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:N("event"),item_id:r,response_id:e,output_index:n,content_index:0,part:{type:"output_text",text:""}})}beginAudioMessageItem(e){let n=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:N("event"),response_id:e,output_index:0,item:{id:n,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:N("event"),item_id:n,response_id:e,output_index:0,content_index:0,part:{type:"output_audio",transcript:""}})}closeOpenItems(e,n,r){if(n.messageOpen||this.messageItemOpenForAudio){let o=`${e}_msg_0`,s=this.messageItemOpenForAudio;s&&this.send({type:"response.output_audio.done",event_id:N("event"),item_id:o,response_id:e,output_index:0,content_index:0}),n.messageText.length>0&&this.send({type:"response.output_text.done",event_id:N("event"),item_id:o,response_id:e,output_index:0,content_index:0,text:n.messageText}),this.send({type:"response.content_part.done",event_id:N("event"),item_id:o,response_id:e,output_index:0,content_index:0,part:s?{type:"output_audio",transcript:""}:{type:"output_text",text:n.messageText}});let i=[];s&&i.push({type:"output_audio"}),n.messageText.length>0&&i.push({type:"output_text",text:n.messageText});let a={id:o,type:"message",role:"assistant",content:i};this.pushItem(a),this.send({type:"response.output_item.done",event_id:N("event"),response_id:e,output_index:0,item:this.itemToProtocol(a)})}for(let[o,s]of n.tools){let i=s.args.length>0?s.args:"{}";this.send({type:"response.function_call_arguments.done",event_id:N("event"),item_id:o,response_id:e,output_index:s.outputIndex,call_id:o,name:s.name??"",arguments:i});let a={id:o,type:"function_call",callId:o,name:s.name,arguments:i};this.pushItem(a),this.send({type:"response.output_item.done",event_id:N("event"),response_id:e,output_index:s.outputIndex,item:this.itemToProtocol(a)})}this.messageItemOpenForAudio=!1,this.messageItemAdded=!1}responseObject(e,n,r){return{id:e,object:"realtime.response",status:n,conversation_id:this.conversationId,usage:null,...r??{}}}failedResponse(e,n){return this.responseObject(e,"failed",{status_details:{error:{code:n.code,message:n.message}}})}doneResponse(e,n,r){let o=r&&typeof r.usage=="object"&&r.usage!==null?r.usage:null,s=[];for(let i=n;i<this.items.length;i++)s.push(this.itemToProtocol(this.items[i]));return this.responseObject(e,"completed",{output:s,usage:o?{input_tokens:typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0,output_tokens:typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0,total_tokens:typeof o.total_tokens=="number"&&Number.isFinite(o.total_tokens)?o.total_tokens:(typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0)+(typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0),input_token_details:{cached_tokens:0,audio_tokens:0,text_tokens:0},output_token_details:{audio_tokens:0,text_tokens:0}}:null})}upstreamErrorBody(e){let n=e.error,r=n&&typeof n.message=="string"?n.message:JSON.stringify(e);return{type:"server_error",code:n&&typeof n.code=="string"?n.code:null,message:r}}failSessionGone(e){let n=e instanceof Error?e.message:String(e);this.fail(yt("session_gone",`this realtime connection's pooled session \u2014 the one its audio bridge and every turn ride \u2014 is gone: ${n}`),"session_gone")}upstreamTurnFault(e,n){if(e instanceof pe){this.failSessionGone(e);return}if(!this.cancelled){if(!this.responseFlushed&&!(e instanceof Zs.SessionFailedError&&(e.kind==="never-live"||e.kind==="create-failed"))){this.failSessionGone(e);return}this.emitUpstreamFault(e,n)}}emitUpstreamFault(e,n){let r=yt(null,e instanceof Error?e.message:String(e));this.responseFlushed?this.send(Q(r)):this.send({type:"response.failed",event_id:N("event"),response:this.failedResponse(n,r)})}cancelResponse(){let e=this.inFlight;if(!e){this.send(Q(ae("no_response_to_cancel","no response is in progress","response.cancel")));return}this.cancelled=!0;let n=this.activeResponseId??"resp_";e.finally(()=>{this.send({type:"response.cancelled",event_id:N("event"),response:this.responseObject(n,"cancelled")})})}pushItem(e){this.items.push(e),this.items.length>wn&&(this.items.shift(),this.send(Q({type:"invalid_request_error",code:"transcript_evicted",message:`the conversation transcript exceeded ${wn} items \u2014 the oldest item was evicted to keep the per-connection bound`})))}itemToProtocol(e){let n={id:e.id,object:"realtime.item"};if(e.type==="function_call")return{...n,type:"function_call",call_id:e.callId,name:e.name,arguments:e.arguments??"{}",status:"completed"};if(e.type==="function_call_output")return{...n,type:"function_call_output",call_id:e.callId,output:e.output};let r=(e.content??[]).map(o=>o.type==="input_text"||o.type==="output_text"?{type:o.type,text:o.text}:o.type==="input_audio"?{type:"input_audio",...o.transcript?{transcript:o.transcript}:{}}:{type:"output_audio",transcript:o.transcript??""});return{...n,type:"message",role:e.role,status:e.truncated?"incomplete":"completed",content:r}}send(e){if(!(this.closed||this.ws.readyState!==this.ws.OPEN)){if(this.ws.bufferedAmount>Ar){this.ws.close(Sd,Xs("outbound buffer exceeded")),this.teardown("outbound buffer exceeded");return}this.ws.send(JSON.stringify(e))}}fail(e,n){this.send(Q(e)),this.ws.close(Rd,Xs(n)),this.teardown(n)}teardown(e){if(!this.closed){this.closed=!0,this.turnLaneAbort?.abort(),this.turnLaneAbort=null,this.releaseVoiceBinding(),this.audioUnsubscribe?.(),this.audioUnsubscribe=null,this.binding.release&&Promise.resolve().then(()=>this.binding.release?.()).catch(n=>Qs(this.id,`binding release failed: ${n instanceof Error?n.message:String(n)}`));try{this.ws.removeAllListeners("message")}catch{}this.onClosed?.(this,{reason:e}),Qs(this.id,e)}}},Cr=[];function Qs(t,e){Cr.push({id:t,reason:e}),Cr.length>256&&Cr.shift()}var Dt="/v1/realtime",Ad=t=>t.slice(0,120);function Id(t,e,n){let r=e===401||e===403?"Unauthorized":e===502?"Upstream Fault":"Upgrade Refused";t.write(`HTTP/1.1 ${e} ${r}\r
|
|
23
23
|
Connection: close\r
|
|
24
24
|
Content-Type: text/plain\r
|
|
25
25
|
\r
|
|
26
|
-
${
|
|
27
|
-
`),t.destroy()}function tn(t,e){let n=new yi.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,i,s)=>{if(new URL(o.url??"/","http://localhost").pathname!==kt)return;r.add(i),i.on("close",()=>{r.delete(i)});let u=l=>{let c=l,y=c.session!=null&&typeof c.session.sendResponseCreate=="function",g=c.clients!=null&&typeof c.clients.createResponse=="function";if(!y&&!g)throw new Error("resolver returned a binding without an upstream turn seam (session.sendResponseCreate or clients.createResponse required) \u2014 refusing the upgrade LOUD");return new Promise((E,w)=>{n.handleUpgrade(o,i,s,h=>{try{new bt({ws:h,binding:l}),E()}catch(b){w(b)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o).then(u).catch(l=>{let c=l instanceof hi.SessionFailedError?Yt(l):null,y=null;if(l!==null&&typeof l=="object"&&"status"in l){let w=l.status;typeof w=="number"&&(y=w)}let g=y??c?.status??502,E=l instanceof Error?l.message:String(l);i.destroyed||Su(i,g,E)}).finally(()=>{r.delete(i)})}),async()=>{let o=Date.now()+5e3;for(;r.size>0&&Date.now()<o;)await new Promise(i=>setTimeout(i,25));await new Promise(i=>n.close(()=>i()))}}_();_();var Tu=require("http");_();var _i="/v1/images/generations",nn="/v1/images/edits";function wi(t){return t===_i||t===nn}var vi="/v1/images/models";async function bi(t,e,n){if(t){if(t.model=Pe({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}var V=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},Eu=16*1024*1024,ki=64*1024*1024;function xi(t){return t.length>=8&&t[0]===137&&t[1]===80&&t[2]===78&&t[3]===71&&t[4]===13&&t[5]===10&&t[6]===26&&t[7]===10?"image/png":t.length>=3&&t[0]===255&&t[1]===216&&t[2]===255?"image/jpeg":t.length>=12&&t[0]===82&&t[1]===73&&t[2]===70&&t[3]===70&&t[8]===87&&t[9]===69&&t[10]===66&&t[11]===80?"image/webp":null}function xt(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}var $e=class extends Error{};async function Au(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new $e(`request body exceeds the ${e}-byte limit`);let r=[],o=0;for await(let i of t){let s=i;if(o+=s.length,o>e)throw new $e(`request body exceeds the ${e}-byte limit`);r.push(s)}return Buffer.concat(r)}async function Ri(t,e,n,r){let o=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},r),i=null;for await(let c of o){if(c.type==="error"){let y=c.error??{};throw new V(502,`image upstream error: ${String(y.message??"upstream reported an error")}`)}if(c.type==="response.completed"){i=c.response??null;break}}if(i===null)throw new V(502,"image upstream produced no terminal response \u2014 refusing to fabricate an image");let s=i.images;if(!Array.isArray(s)||s.length!==1||typeof s[0]!="string"||s[0].length===0)throw new V(502,`image upstream returned ${Array.isArray(s)?s.length:"a non-array images field"} images for the single-shot (n=1) contract, which produces exactly one \u2014 refusing to truncate or fabricate`);let a=[],u=s[0].startsWith("data:")?s[0].slice(s[0].indexOf(";base64,")+8):s[0],d=Buffer.from(u,"base64");if(xi(d)===null)throw new V(502,`image upstream returned a payload that is not a PNG/JPEG/WebP image (${d.length} bytes) \u2014 refusing to pass it through`);return a.push(u),{images:a}}function Si(t){return t instanceof V?t:t instanceof re?new V(404,t.message):new V(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function Ei(t,e,n,r){if(await t(e,n))return!0;let o=n==="edit"?"no image-input (expects_image) edit destination":"no pure text-to-image (expects_image=false) generation destination";return xt(r,new V(404,e===void 0?`no image-capable default model is configured for this caller (${o}) \u2014 name an image-capable \`model\``:`model "${e}" is not ${n==="edit"?"edit-capable":"generation-capable"} for this caller (${o}; capability is read from the catalog's modality metadata, not the serve function name)`)),!1}function Ai(t,e){t&&(t.outputUnits=e.images.length)}function Oi(t,e){t.statusCode=200,t.setHeader("content-type","application/json"),t.end(JSON.stringify({created:Math.floor(Date.now()/1e3),data:e.images.map(n=>({b64_json:n}))}))}var Ou={model:!0,prompt:!0,response_format:!0,n:!0};async function Ii(t,e,n){let r=n.maxBodyBytes??ki;try{if(t.method!=="POST")throw new V(405,`${String(t.method)} is not supported on ${_i}`);let o;try{o=JSON.parse((await Au(t,r)).toString("utf8")||"{}")}catch(c){throw c instanceof $e?new V(413,c.message):new V(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new V(400,"request body must be a JSON object");let i=o;for(let c of Object.keys(i))if(!Ou[c])throw new V(400,`request field "${c}" is not implemented: the native image request schema carries only the prompt (size, quality, style, seed, mask and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, n.`,c);let s=i.prompt;if(typeof s!="string"||s.trim().length===0)throw new V(400,"prompt is required and must be a non-empty string","prompt");let a=i.model===void 0?void 0:i.model;if(a!==void 0&&typeof a!="string")throw new V(400,"model must be a string","model");await bi(n.turn,n.clients,a);let u=i.n??1;if(u!==1)throw new V(400,`n=${JSON.stringify(u)} is not supported: the native image request is single-shot and produces exactly one image per request (no batch support yet)`,"n");let d=i.response_format??"b64_json";if(d!=="b64_json")throw new V(400,`response_format ${JSON.stringify(d)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");if(n.stats?.request("images_generations",s.length,s.length),!await Ei(n.imageModels,a,"generation",e))return;let l=[{type:"input_text",text:s}];try{let c=await Ri(n.clients,a,l,n.turn?Te(n.turn):void 0);Ai(n.turn,c),Oi(e,c)}catch(c){throw Si(c)}}catch(o){if(o instanceof $e){xt(e,new V(413,o.message));return}if(o instanceof V){xt(e,o);return}throw o}}var Iu={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function Ti(t,e,n){let r=n.maxBodyBytes??ki,o=n.maxImagePartBytes??Eu;try{if(t.method!=="POST")throw new V(405,`${String(t.method)} is not supported on ${nn}`);let i=String(t.headers["content-type"]??"");if(!i.toLowerCase().startsWith("multipart/form-data"))throw new V(400,`the edit endpoint consumes multipart/form-data, got ${i||"none"}`);let s=t[Symbol.asyncIterator](),a=0,u=new ReadableStream({async pull(h){try{let b=await s.next();if(b.done){h.close();return}if(a+=b.value.length,a>r){h.error(new $e(`request body exceeds the ${r}-byte limit`));return}h.enqueue(b.value)}catch(b){h.error(b)}},cancel(){s.return?.()}}),d;try{let h=new Headers;for(let[T,B]of Object.entries(t.headers))if(typeof B=="string")h.set(T,B);else if(Array.isArray(B))for(let M of B)h.append(T,M);let b={method:"POST",headers:h,body:u,duplex:"half"};d=await new Request(`https://proxy.internal${t.url??"/"}`,b).formData()}catch(h){throw h instanceof $e?new V(413,h.message):new V(400,`malformed multipart body: ${String(h instanceof Error?h.message:h)}`)}let l=d.get("prompt");if(typeof l!="string"||l.trim().length===0)throw new V(400,"prompt is required and must be a non-empty string field","prompt");let c=d.get("model");if(c!==null&&typeof c!="string")throw new V(400,"model must be a string field","model");let y=c===null?void 0:c;await bi(n.turn,n.clients,y);let g=d.get("response_format")??"b64_json";if(g!=="b64_json")throw new V(400,`response_format ${JSON.stringify(g)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");for(let h of d.keys())if(!Iu[h])throw new V(400,`form field "${h}" is not implemented: the native image request schema carries the prompt and the image inputs only (mask, size, seed and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, image (or image[]).`,h);let E=[];for(let h of["image","image[]"])for(let b of d.getAll(h)){if(!(b instanceof File))throw new V(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",h);if(b.size>o)throw new V(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,h);let R=new Uint8Array(await b.arrayBuffer()),T=xi(R);if(T===null)throw new V(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",h);E.push({bytes:R,mime:T})}if(E.length===0)throw new V(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",l.length,l.length),!await Ei(n.imageModels,y,"edit",e))return;let w=[{type:"input_text",text:l}];for(let{bytes:h,mime:b}of E)w.push({type:"input_image",image_url:`data:${b};base64,${Buffer.from(h).toString("base64")}`});try{let h=await Ri(n.clients,y,w,n.turn?Te(n.turn):void 0);Ai(n.turn,h),Oi(e,h)}catch(h){throw Si(h)}}catch(i){if(i instanceof $e){xt(e,new V(413,i.message));return}if(i instanceof V){xt(e,i);return}throw i}}var un=require("@urun-sh/core");function ar(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function me(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function Me(t,e,n,r="invalid_request_error",o){me(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function $i(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);me(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function Ee(t,e,n,r){me(t,e,{type:"error",error:{type:n,message:r}})}function rn(t,e,n){if(e==="anthropic"){me(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}me(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var Pu=64*1024*1024,Rt=class extends Error{};async function Pi(t,e=Pu){let n=[],r=0;for await(let i of t){if(r+=i.length,r>e)throw new Rt(`request body exceeds ${e} bytes`);n.push(i)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function rt(t,e,n,r){return{id:t,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:e,choices:[{index:0,delta:n,finish_reason:r}]}}function Mi(t){let e=t?.usage;if(!e||typeof e!="object")return{kind:"absent"};let n=e,r=u=>typeof u=="number"&&Number.isFinite(u)&&u>=0?u:null,o=r(n.input_tokens),i=r(n.output_tokens),s=u=>u??0,a=r(n.total_tokens)??s(o)+s(i);if(s(o)>0||s(i)>0){let u={input_tokens:s(o),output_tokens:s(i),total_tokens:a};return o===null||i===null?{kind:"partial",usage:u}:{kind:"counts",usage:u}}return o===null||i===null?{kind:"absent"}:{kind:"zero",usage:{input_tokens:0,output_tokens:0,total_tokens:a}}}function on(t){let e=Mi(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function Ci(t){let e=on(t);return e?{prompt_tokens:e.input_tokens,completion_tokens:e.output_tokens,total_tokens:e.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function Cu(t,e){let n=Mi(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function $u(t){let e=t?.urun_timing;if(!e||typeof e!="object")return null;let n=e,r=u=>{let d=n[u];return typeof d=="number"&&Number.isSafeInteger(d)&&d>=0?d:null},o=r("queue_ms"),i=r("prefill_ms"),s=r("decode_ms"),a=r("total_ms");return o===null||i===null||s===null||a===null?null:{queue_ms:o,prefill_ms:i,decode_ms:s,total_ms:a}}function Mu(t,e){let n=$u(e);n&&(t.timing=n)}function ot(t,e){Cu(t,e),Mu(t,e)}async function ur(t,e,n){if(typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}function Ui(t){let e=t?.output;if(!Array.isArray(e))return"";let n="";for(let r of e)if(!(r.type!=="reasoning"||!Array.isArray(r.content)))for(let o of r.content)o.type==="reasoning_text"&&(n+=String(o.text??""));return n}function Uu(t){if(Array.isArray(t))return t.map(e=>{let n=e.function;return!n||typeof n!="object"?e:{type:"function",name:n.name,description:n.description,parameters:n.parameters,strict:n.strict}})}var sn=class extends Error{};function Lu(t,e){return Array.isArray(e)?e.map(n=>{let r=n;if(r.type==="text")return{type:t==="assistant"?"output_text":"input_text",text:r.text};if(r.type==="image_url"){let o=r.image_url??{};return{type:"input_image",image_url:o.url,detail:o.detail}}throw new sn(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function Nu(t){let e=[];for(let n of t){if(n.role==="tool"){e.push({type:"function_call_output",call_id:n.tool_call_id,output:typeof n.content=="string"?n.content:JSON.stringify(n.content??"")});continue}if(n.role==="assistant"&&Array.isArray(n.tool_calls)){typeof n.content=="string"&&n.content&&e.push({role:"assistant",content:n.content});for(let r of n.tool_calls){let o=r.function??{};e.push({type:"function_call",call_id:r.id,name:o.name,arguments:o.arguments})}continue}e.push({...n,content:Lu(n.role,n.content)})}return e}function ir(t){return et(t).map((e,n)=>({index:n,id:e.call_id??e.id??`call_${n}`,type:"function",function:{name:e.name??"",arguments:e.arguments??"{}"}}))}async function Du(t,e,n,r,o,i){let s=ht(i.id),a=t.stream===!0;if(i.model=Pe(t),i.stream=a,await ur(i,e,typeof t.model=="string"?t.model:void 0),a){let b;try{b=await _t({body:t,clients:e,store:n,stats:o,responseId:s,turn:i})}catch(R){if(R instanceof re){rn(r,"openai",R);return}if(R instanceof _e){Me(r,R.status,R.message);return}throw R}ar(r);for await(let R of b.events)R.type==="error"&&(i.streamError=!0),R.type==="response.completed"&&ot(i,R.response),r.write(`event: ${String(R.type??"message")}
|
|
28
|
-
data: ${JSON.stringify(R)}
|
|
26
|
+
${Ad(n)}\r
|
|
27
|
+
`),t.destroy()}function Td(t){return`${JSON.stringify({event:"realtime_upgrade",ts:new Date().toISOString(),...t})}
|
|
28
|
+
`}function Pd(t){$e(process.stdout,t)}function Cd(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function $r(t){return t.audioSession?.audio.sessionId??void 0}function bn(t,e){let n=new ti.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,s,i)=>{if(new URL(o.url??"/","http://localhost").pathname!==Dt)return;r.add(s),s.on("close",()=>{r.delete(s)});let u=N("rtup"),d=Cd(o),l=S=>{(e.log??Pd)(Td(S))};l({stage:"accept",conn:u,model:d}),s.resume();let p=new AbortController,y=null,c=S=>{y??=S,p.abort(new Error(y))},m=()=>c("client socket closed"),w=()=>c("client socket closed (half-close)"),R=S=>c(`client socket error: ${S.message}`);s.once("close",m),s.once("end",w),s.once("error",R);let h=()=>{s.removeListener("close",m),s.removeListener("end",w),s.removeListener("error",R)},v=S=>{l({stage:"close",conn:u,model:d,...S})},b=S=>{let E=S,x=E.session!=null&&typeof E.session.sendResponseCreate=="function",L=E.clients!=null&&typeof E.clients.createResponse=="function";if(!x&&!L)throw new Error("resolver returned a binding without an upstream turn seam (session.sendResponseCreate or clients.createResponse required) \u2014 refusing the upgrade LOUD");return new Promise((B,q)=>{n.handleUpgrade(o,s,i,V=>{try{new Nt({ws:V,binding:S,onClosed:(de,fe)=>{v({while:"live",reason:fe.reason})}}),l({stage:"admit",conn:u,model:d,...$r(S)===void 0?{}:{session:$r(S)}}),B()}catch(de){q(de)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o,p.signal).then(async S=>{if(p.signal.aborted){h();let E=$r(S);try{await S.release?.()}catch(x){v({while:"queued",...E===void 0?{}:{session:E},reason:y??"client socket closed",release_failed:x instanceof Error?x.message:String(x)});return}v({while:"queued",...E===void 0?{}:{session:E},reason:y??"client socket closed",released:!0}),s.destroy();return}h(),await b(S)}).catch(S=>{let E=S instanceof ei.SessionFailedError?hn(S):null,x=null;if(S!==null&&typeof S=="object"&&"status"in S){let q=S.status;typeof q=="number"&&(x=q)}let L=x??E?.status??502,B=S instanceof Error?S.message:String(S);v({while:"queued",...p.signal.aborted?{reason:y??"client socket closed"}:{reason:B,status:L}}),s.destroyed||Id(s,L,B)}).finally(()=>{r.delete(s)})}),async()=>{let o=Date.now()+5e3;for(;r.size>0&&Date.now()<o;)await new Promise(s=>setTimeout(s,25));await new Promise(s=>n.close(()=>s()))}}k();k();var hl=require("http");k();k();var he=class extends Error{};async function kn(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new he(`request body exceeds the ${e}-byte limit`);let r=[],o=0;for await(let s of t){let i=s;if(o+=i.length,o>e)throw new he(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function Sn(t,e){let n=t[Symbol.asyncIterator](),r=0,o=new ReadableStream({async pull(i){try{let a=await n.next();if(a.done){i.close();return}if(r+=a.value.length,r>e){i.error(new he(`request body exceeds the ${e}-byte limit`));return}i.enqueue(a.value)}catch(a){i.error(a)}},cancel(){n.return?.()}}),s;try{let i=new Headers;for(let[d,l]of Object.entries(t.headers))if(typeof l=="string")i.set(d,l);else if(Array.isArray(l))for(let p of l)i.append(d,p);let a={method:"POST",headers:i,body:o,duplex:"half"};s=await new Request(`https://proxy.internal${t.url??"/"}`,a).formData()}catch(i){throw i instanceof he?i:new Error(`malformed multipart body: ${String(i instanceof Error?i.message:i)}`)}return s}var ni="/v1/images/generations",Rn="/v1/images/edits";function ri(t){return t===ni||t===Rn}var oi="/v1/images/models";async function si(t,e,n){if(t){if(t.model=ve({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}var K=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},$d=16*1024*1024,ii=64*1024*1024;function ai(t){return t.length>=8&&t[0]===137&&t[1]===80&&t[2]===78&&t[3]===71&&t[4]===13&&t[5]===10&&t[6]===26&&t[7]===10?"image/png":t.length>=3&&t[0]===255&&t[1]===216&&t[2]===255?"image/jpeg":t.length>=12&&t[0]===82&&t[1]===73&&t[2]===70&&t[3]===70&&t[8]===87&&t[9]===69&&t[10]===66&&t[11]===80?"image/webp":null}function Ft(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}async function ui(t,e,n,r){let o=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},r),s=null;for await(let p of o){if(p.type==="error"){let y=p.error??{};throw new K(502,`image upstream error: ${String(y.message??"upstream reported an error")}`)}if(p.type==="response.completed"){s=p.response??null;break}}if(s===null)throw new K(502,"image upstream produced no terminal response \u2014 refusing to fabricate an image");let i=s.images;if(!Array.isArray(i)||i.length!==1||typeof i[0]!="string"||i[0].length===0)throw new K(502,`image upstream returned ${Array.isArray(i)?i.length:"a non-array images field"} images for the single-shot (n=1) contract, which produces exactly one \u2014 refusing to truncate or fabricate`);let a=[],u=i[0].startsWith("data:")?i[0].slice(i[0].indexOf(";base64,")+8):i[0],d=Buffer.from(u,"base64");if(ai(d)===null)throw new K(502,`image upstream returned a payload that is not a PNG/JPEG/WebP image (${d.length} bytes) \u2014 refusing to pass it through`);return a.push(u),{images:a}}function di(t){return t instanceof K?t:t instanceof z?new K(404,t.message):new K(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function li(t,e,n,r){if(await t(e,n))return!0;let o=n==="edit"?"no image-input (expects_image) edit destination":"no pure text-to-image (expects_image=false) generation destination";return Ft(r,new K(404,e===void 0?`no image-capable default model is configured for this caller (${o}) \u2014 name an image-capable \`model\``:`model "${e}" is not ${n==="edit"?"edit-capable":"generation-capable"} for this caller (${o}; capability is read from the catalog's modality metadata, not the serve function name)`)),!1}function ci(t,e){t&&(t.outputUnits=e.images.length)}function pi(t,e){t.statusCode=200,t.setHeader("content-type","application/json"),t.end(JSON.stringify({created:Math.floor(Date.now()/1e3),data:e.images.map(n=>({b64_json:n}))}))}var Md={model:!0,prompt:!0,response_format:!0,n:!0};async function fi(t,e,n){let r=n.maxBodyBytes??ii;try{if(t.method!=="POST")throw new K(405,`${String(t.method)} is not supported on ${ni}`);let o;try{o=JSON.parse((await kn(t,r)).toString("utf8")||"{}")}catch(p){throw p instanceof he?new K(413,p.message):new K(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new K(400,"request body must be a JSON object");let s=o;for(let p of Object.keys(s))if(!Md[p])throw new K(400,`request field "${p}" is not implemented: the native image request schema carries only the prompt (size, quality, style, seed, mask and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, n.`,p);let i=s.prompt;if(typeof i!="string"||i.trim().length===0)throw new K(400,"prompt is required and must be a non-empty string","prompt");let a=s.model===void 0?void 0:s.model;if(a!==void 0&&typeof a!="string")throw new K(400,"model must be a string","model");await si(n.turn,n.clients,a);let u=s.n??1;if(u!==1)throw new K(400,`n=${JSON.stringify(u)} is not supported: the native image request is single-shot and produces exactly one image per request (no batch support yet)`,"n");let d=s.response_format??"b64_json";if(d!=="b64_json")throw new K(400,`response_format ${JSON.stringify(d)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");if(n.stats?.request("images_generations",i.length,i.length),!await li(n.imageModels,a,"generation",e))return;let l=[{type:"input_text",text:i}];try{let p=await ui(n.clients,a,l,n.turn?xe(n.turn):void 0);ci(n.turn,p),pi(e,p)}catch(p){throw di(p)}}catch(o){if(o instanceof he){Ft(e,new K(413,o.message));return}if(o instanceof K){Ft(e,o);return}throw o}}var Ld={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function mi(t,e,n){let r=n.maxBodyBytes??ii,o=n.maxImagePartBytes??$d;try{if(t.method!=="POST")throw new K(405,`${String(t.method)} is not supported on ${Rn}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new K(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await Sn(t,r)}catch(c){throw c instanceof he?new K(413,c.message):new K(400,String(c instanceof Error?c.message:c))}let a=i.get("prompt");if(typeof a!="string"||a.trim().length===0)throw new K(400,"prompt is required and must be a non-empty string field","prompt");let u=i.get("model");if(u!==null&&typeof u!="string")throw new K(400,"model must be a string field","model");let d=u===null?void 0:u;await si(n.turn,n.clients,d);let l=i.get("response_format")??"b64_json";if(l!=="b64_json")throw new K(400,`response_format ${JSON.stringify(l)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");for(let c of i.keys())if(!Ld[c])throw new K(400,`form field "${c}" is not implemented: the native image request schema carries the prompt and the image inputs only (mask, size, seed and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, image (or image[]).`,c);let p=[];for(let c of["image","image[]"])for(let m of i.getAll(c)){if(!(m instanceof File))throw new K(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",c);if(m.size>o)throw new K(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,c);let w=new Uint8Array(await m.arrayBuffer()),R=ai(w);if(R===null)throw new K(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",c);p.push({bytes:w,mime:R})}if(p.length===0)throw new K(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",a.length,a.length),!await li(n.imageModels,d,"edit",e))return;let y=[{type:"input_text",text:a}];for(let{bytes:c,mime:m}of p)y.push({type:"input_image",image_url:`data:${m};base64,${Buffer.from(c).toString("base64")}`});try{let c=await ui(n.clients,d,y,n.turn?xe(n.turn):void 0);ci(n.turn,c),pi(e,c)}catch(c){throw di(c)}}catch(s){if(s instanceof he){Ft(e,new K(413,s.message));return}if(s instanceof K){Ft(e,s);return}throw s}}k();var jt="/v1/audio/speech";function _i(t){return t===jt}var gi=4096,Ud=18e4,Nd=5e3,Dd=64*1024*1024,hi=8,yi=5,En="voice_applied",Fd="X-Urun-Voice-Applied",G=class extends Error{status;param;retryAfterSeconds;constructor(e,n,r,o){super(n),this.status=e,this.param=r,this.retryAfterSeconds=o}};function Mr(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),e.retryAfterSeconds!==void 0&&t.setHeader("retry-after",String(e.retryAfterSeconds)),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}var On=class extends G{constructor(e){super(502,e)}};async function Bd(t,e,n){if(t){if(t.model=ve({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}function jd(t,e){t&&(t.outputUnits=e/(24e3*2))}var qd=2;function Hd(t){for(let e=0;e+1<t.byteLength;e+=2)if(Math.abs(t.readInt16LE(e))>qd)return!1;return!0}function Gd(t){let e=24e3,n=2,r=1,o=Buffer.alloc(44);return o.write("RIFF",0,"ascii"),o.writeUInt32LE(36+t,4),o.write("WAVE",8,"ascii"),o.write("fmt ",12,"ascii"),o.writeUInt32LE(16,16),o.writeUInt16LE(1,20),o.writeUInt16LE(r,22),o.writeUInt32LE(e,24),o.writeUInt32LE(e*r*n,28),o.writeUInt16LE(r*n,32),o.writeUInt16LE(16,34),o.write("data",36,"ascii"),o.writeUInt32LE(t,40),o}var wi=new WeakMap,xn=new WeakMap;function Vd(t,e){let n=xn.get(t)??0;if(n>=hi)throw new G(503,`this model's speech queue is full \u2014 ${hi} requests are already admitted (one in-flight turn plus the queued depth) and each holds a pooled session turn; retry after ${yi}s`,void 0,yi);xn.set(t,n+1);let r=wi.get(t)??Promise.resolve(),o,s=new Promise(i=>{o=i});return wi.set(t,s),(async()=>{await r;try{return await e()}finally{xn.set(t,(xn.get(t)??1)-1),o()}})()}async function Wd(t,e,n,r){if(typeof t.evictSpeechSession!="function")throw new G(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned session after a failed turn \u2014 refusing loudly rather than handing the next request the failed turn\u2019s audio");try{await t.evictSpeechSession(e)}catch(i){if(!(i instanceof pe))throw i}if(n?.return===void 0)return;let o=Promise.resolve(n.return()).then(()=>{},()=>{}),s;await Promise.race([o,new Promise(i=>{s=setTimeout(i,r)})]).finally(()=>{s!==void 0&&clearTimeout(s)})}function Bt(t){return t instanceof G?t:t instanceof z?new G(404,t.message):new G(502,`speech upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function Jd(t,e,n,r,o,s,i){return Vd(e,async()=>{if(i?.()===!0)return null;let a=[],u=!1,d=0,l=!1,p=!1,y,c=new Promise($=>{y=$}),m=()=>{l&&!p&&d>=o.quietRunFrames&&(p=!0,y())},w=e.onOutputAudio($=>{let S=Buffer.from($,"base64");if(S.byteLength===0)return;let E=Hd(S);if(!u){if(E)return;u=!0}a.push(S),d=E?d+1:0,m()}),R=null,h,v,b;try{b=(await t.createResponseOn(n,{model:r.model,input:[{role:"user",content:[{type:"input_text",text:r.text}]}],stream:!1,...r.voice!==void 0?{voice:r.voice}:{}}))[Symbol.asyncIterator]();let S=new Promise(B=>{v=setTimeout(B,s.capMs)}),E=!1;for(;;){let B=b.next(),q=await Promise.race([B,S]);if(q===void 0){E=!0,B.catch(()=>{});break}if(q.done)break;let V=q.value;if(V.type==="error"){let de=V.error??{};throw await b.return?.(),new G(502,`speech upstream error: ${String(de.message??"upstream reported an error")}`)}if(V.type==="response.completed"){R=V.response??null,l=!0,m(),await b.return?.();break}}if(clearTimeout(v),E)throw new G(504,`the speech turn produced no terminal event within ${s.capMs}ms \u2014 the upstream stream never completed, so the pinned speech session is evicted (the next request opens a fresh one), the serialized lane is released and the caller can retry; a stuck turn must not hold every later request on this lane`);if(R===null)throw new G(502,"speech upstream produced no terminal response \u2014 refusing to fabricate audio");let x=!1;if(await Promise.race([c,new Promise(B=>{h=setTimeout(()=>{x=!0,B()},o.capMs)})]),h!==void 0&&clearTimeout(h),r.voice!==void 0){let B=R[En];if(typeof B!="string"||B!==r.voice){let q=`the runtime did not acknowledge the requested voice ${JSON.stringify(r.voice)} \u2014 the terminal response carried ${typeof B=="string"?`${En} ${JSON.stringify(B)}`:`no ${En} field`}; the speak-bridge companion is not deployed, and this lane never serves default-voice audio as the requested voice`;throw p?new On(q):new G(502,q)}}if(!u)throw new G(502,"the upstream produced no voice for this utterance \u2014 refusing to serve silence as success");if(x&&!p&&d<1)throw new G(502,`the upstream audio stream did not quiesce within ${o.capMs}ms of the turn's end (voiced frames still arriving) \u2014 refusing to serve audio whose tail cannot be vouched for`);let L=d>0?a.slice(0,a.length-d):a;return{pcm:Buffer.concat(L),completed:R}}catch($){throw $ instanceof On||await Wd(t,n,b,s.returnBoundMs),$}finally{h!==void 0&&clearTimeout(h),v!==void 0&&clearTimeout(v),w()}})}async function Kd(t,e,n){let r;try{r=await t(e)}catch(o){throw Bt(o)}return r?!0:(Mr(n,new G(404,e===void 0?"no speech-capable default model is configured for this caller (catalog task tts) \u2014 name a tts model":`model "${e}" is not speech-capable (catalog task is not tts) \u2014 this lane serves only text-to-speech models; chat models ride /v1/chat/completions`)),!1)}var zd={model:!0,input:!0,voice:!0,response_format:!0,stream_format:!0,speed:!0,instructions:!0},Yd="wav, pcm";async function vi(t,e,n){let r=n.maxBodyBytes??Dd,o=!1;e.once("close",()=>{o=!0});let s=()=>o||e.destroyed;try{if(t.method!=="POST")throw new G(405,`${String(t.method)} is not supported on ${jt}`);let i;try{i=JSON.parse((await kn(t,r)).toString("utf8")||"{}")}catch(b){throw b instanceof he?new G(413,b.message):new G(400,"request body is not valid JSON")}if(i===null||typeof i!="object"||Array.isArray(i))throw new G(400,"request body must be a JSON object");let a=i;for(let b of Object.keys(a))if(!zd[b])throw new G(400,`request field "${b}" is not implemented: the speech request schema carries the utterance and its voice (the qwen3 backend fixes voice style at deploy time, and sample rate is pinned to the engine's native 24 kHz mono). Supported fields: model, input, voice, response_format, stream_format, speed, instructions.`,b);let u=a.input;if(typeof u!="string"||u.trim().length===0)throw new G(400,"input is required and must be a non-empty string","input");if(u.length>gi)throw new G(400,`input is ${u.length} chars \u2014 over the ${gi}-char limit (one utterance; the backend bounds generation text-shaped, and an unbounded text is an unbounded synthesis)`,"input");let d=a.model===void 0?void 0:a.model;if(d!==void 0&&typeof d!="string")throw new G(400,"model must be a string","model");let l=a.voice;if(l!==void 0&&(typeof l!="string"||l.trim().length===0))throw new G(400,"voice must be a non-empty string","voice");if(typeof n.clients.speechVoices!="function")throw new G(501,`the speech lane needs a router-backed client (speechVoices) to resolve the voice set GET /v1/models publishes \u2014 refusing loudly rather than validating voices of ${jt} against a stale copy of the list`);let p;try{p=await n.clients.speechVoices(d)}catch(b){throw Bt(b)}if(p.defect!==null)throw new G(500,`the voice set for this model cannot be resolved: ${p.defect} \u2014 fix the catalog row; the lane refuses to serve rather than validate against a set nobody vouched`);let y=typeof l=="string"?l:void 0;if(y!==void 0&&p.published.length>0&&!p.published.includes(y))throw new G(400,`voice ${JSON.stringify(y)} is not one of the voices this model publishes \u2014 accepted voices: ${p.published.join(", ")}`,"voice");let c=a.response_format??"wav";if(c!=="wav"&&c!=="pcm")throw new G(400,`response_format ${JSON.stringify(c)} is not supported: this lane serves the engine's raw 24 kHz mono PCM16, so the supported formats are ${Yd} \u2014 and the lane will not label bytes with a format it did not encode`,"response_format");if(a.stream_format!==void 0&&typeof a.stream_format!="string")throw new G(400,"stream_format, when present, must be a string","stream_format");let m=a.speed;if(m!==void 0){if(typeof m!="number"||!Number.isFinite(m))throw new G(400,"speed, when present, must be a number","speed");if(m!==1)throw new G(400,`speed=${JSON.stringify(m)} is not supported: the backend exposes no speed knob (the qwen3 engine's speaking rate is fixed), so a non-default speed would be silently ignored \u2014 1 (the OpenAI default) is the only accepted value`,"speed")}let w=a.instructions;if(w!==void 0){if(typeof w!="string")throw new G(400,"instructions, when present, must be a string","instructions");if(w.trim().length>0)throw new G(400,"instructions are not supported: the backend fixes the voice style (instruct) at deploy time, so a per-request instruction would be silently ignored \u2014 the deployment speaks with its configured style","instructions")}await Bd(n.turn,n.clients,d);let R=Buffer.byteLength(JSON.stringify(a));if(n.stats?.request("audio_speech",R,R),!await Kd(n.speechModels,d,e))return;if(typeof n.clients.openAudioOn!="function")throw new G(501,"a speech-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the same session identity as the response turn \u2014 refusing loudly rather than answering an utterance whose audio cannot be collected");if(typeof n.clients.speechSessionHandle!="function")throw new G(501,"a speech-capable model needs a router-backed client (speechSessionHandle) to resolve the speech lane's own pooled session \u2014 one no interactive binding shares, because the per-session audio bridge broadcasts every output frame to every subscriber \u2014 refusing loudly rather than collecting speech audio on a session a realtime/gemini lane can be speaking on");if(typeof n.clients.evictSpeechSession!="function")throw new G(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned speech session after a failed turn (a turn-cap timeout above all) \u2014 without it, the next request on the same model would silently collect the failed turn\u2019s still-emitting audio; refusing loudly rather than serving on an un-evictable speech slot");if(s())return;let h;try{h=await n.clients.speechSessionHandle(d)}catch(b){throw Bt(b)}let v;try{v=await n.clients.openAudioOn(h)}catch(b){throw Bt(b)}n.turn&&(n.turn.sessionId=v.sessionId);try{let b=await Jd(n.clients,v,h,{model:d,text:u,...y!==void 0?{voice:y}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??Ud,returnBoundMs:n.turnReturnBoundMs??Nd},s);if(b===null||s())return;if(jd(n.turn,b.pcm.byteLength),e.statusCode=200,y===void 0){let $=b.completed[En];typeof $=="string"&&e.setHeader(Fd,$)}c==="wav"?(e.setHeader("content-type","audio/wav"),e.end(Buffer.concat([Gd(b.pcm.byteLength),b.pcm]))):(e.setHeader("content-type","audio/pcm"),e.end(b.pcm))}catch(b){throw Bt(b)}}catch(i){if(i instanceof he){Mr(e,new G(413,i.message));return}if(i instanceof G){Mr(e,i);return}throw i}}k();var An="/v1/audio/transcriptions";function bi(t){return t===An}var ki="/v1/audio/models",Xd=16*1024*1024,Qd=64*1024*1024,ee=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}};function Lr(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}function Si(t){return t.length>=12&&t[0]===82&&t[1]===73&&t[2]===70&&t[3]===70&&t[8]===87&&t[9]===65&&t[10]===86&&t[11]===69}function Zd(t){if(!Si(t))return null;let e=12,n=null,r=null;for(;e+8<=t.length;){let s=String.fromCharCode(t[e],t[e+1],t[e+2],t[e+3]),i=(t[e+4]|t[e+5]<<8|t[e+6]<<16|t[e+7]<<24)>>>0,a=e+8;if(s==="fmt "&&a+16<=t.length){let u=t[a+2]|t[a+3]<<8,d=(t[a+4]|t[a+5]<<8|t[a+6]<<16|t[a+7]<<24)>>>0,l=t[a+14]|t[a+15]<<8;n={sampleRate:d,channels:u,bitsPerSample:l}}else if(s==="data"){let u=t.length-a;r=Math.min(i===0||i===4294967295?u:i,u)}e=a+i+i%2}if(n===null||r===null||r===0||n.sampleRate===0||n.channels===0||n.bitsPerSample===0)return null;let o=r/(n.sampleRate*n.channels*(n.bitsPerSample/8));return!Number.isFinite(o)||o<0?null:{seconds:o,sampleRate:n.sampleRate,channels:n.channels,bitsPerSample:n.bitsPerSample}}async function el(t,e,n){if(t){if(t.model=ve({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}function tl(t,e){t&&(t.outputUnits=e.seconds)}async function nl(t,e,n,r,o){let s=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1,...r!==void 0?{language:r}:{}},o),i=[],a=null;for await(let u of s){if(i.push(u),u.type==="error"){let d=u.error??{},l=typeof d.type=="string"?d.type:"upstream_error",p=String(d.message??"upstream reported an error");throw new ee(l==="invalid_request_error"?400:502,`transcription upstream error: ${p}`)}if(u.type==="response.completed"){a=u.response??null;break}}if(a===null)throw new ee(502,"transcription upstream produced no terminal response \u2014 refusing to fabricate a transcript");return gt(i,a)}function Ri(t){return t instanceof ee?t:t instanceof z?new ee(404,t.message):new ee(502,`transcription upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function rl(t,e,n){let r;try{r=await t(e)}catch(o){throw Ri(o)}return r?!0:(Lr(n,new ee(404,e===void 0?"no transcription-capable default model is configured for this caller (catalog task stt) \u2014 name an stt model":`model "${e}" is not transcription-capable (catalog task is not stt) \u2014 this lane serves only speech-to-text models; tts models ride /v1/audio/speech and chat models ride /v1/chat/completions`)),!1)}var ol=new Set(["file","model","language","response_format"]);async function xi(t,e,n){let r=n.maxBodyBytes??Qd,o=n.maxAudioPartBytes??Xd;try{if(t.method!=="POST")throw new ee(405,`${String(t.method)} is not supported on ${An}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new ee(400,`the transcriptions endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await Sn(t,r)}catch(h){throw h instanceof he?new ee(413,h.message):new ee(400,String(h instanceof Error?h.message:h))}let a=new Map;for(let h of i.keys()){if(!ol.has(h))throw new ee(400,`form field "${h}" is not implemented: the native transcription request carries the audio clip and a language hint only (prompt, temperature, timestamps and similar knobs have no native surface on this lane). Supported fields: file, model, language, response_format.`,h);a.set(h,(a.get(h)??0)+1)}for(let[h,v]of a)if(v>1)throw new ee(400,`form field "${h}" appears ${v} times: the lane reads exactly one value per field, and a repeat is refused rather than silently discarded \u2014 resend the request with each field once`,h);let u=i.get("model");if(u!==null&&typeof u!="string")throw new ee(400,"model must be a string field","model");let d=u===null?void 0:u,l=i.get("language");if(l!==null&&(typeof l!="string"||l.trim().length===0))throw new ee(400,"language, when present, must be a non-empty string","language");let p=l===null?void 0:l,y=i.get("response_format")??"json";if(y!=="json")throw new ee(400,`response_format ${JSON.stringify(y)} is not supported: timestamps/segments are not produced on this lane yet \u2014 the runtime answers terminal transcript text only, so json is the only output format`,"response_format");let c=i.get("file");if(!(c instanceof File))throw new ee(400,"file must be an uploaded WAV: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths","file");if(c.size>o)throw new ee(413,`file part exceeds the ${o}-byte limit (the serve protocol's MAX_AUDIO_BYTES)`,"file");let m=new Uint8Array(await c.arrayBuffer());if(!Si(m))throw new ee(400,"the audio payload is not a RIFF/WAVE container \u2014 this lane accepts WAV uploads only (detected by magic bytes, not the declared Content-Type); the runtime decodes PCM16 WAV and nothing else","file");let w=Zd(m);if(w===null)throw new ee(400,"the WAV header cannot be read (no fmt /data chunk pair, or a zero rate/channel/depth) \u2014 the runtime decodes RIFF/WAVE PCM16 and this container cannot be honestly decoded or billed","file");if(await el(n.turn,n.clients,d),n.stats?.request("audio_transcriptions",m.length,m.length),!await rl(n.transcriptionModels,d,e))return;let R=[{type:"input_audio",input_audio:{data:Buffer.from(m).toString("base64"),format:"wav"}}];try{let h=await nl(n.clients,d,R,p,n.turn?xe(n.turn):void 0);tl(n.turn,w),e.statusCode=200,e.setHeader("content-type","application/json"),e.end(JSON.stringify({text:h}))}catch(h){throw Ri(h)}}catch(s){if(s instanceof he){Lr(e,new ee(413,s.message));return}if(s instanceof ee){Lr(e,s);return}throw s}}var Mn=require("@urun-sh/core");k();var Ur=ze(require("crypto"),1),sl="urun-runtime-key-v1";function il(t,e){if(!t)throw new Error("deriveOrgRuntimeKey requires a master secret");if(!e)throw new Error("deriveOrgRuntimeKey requires an orgId");return Ur.createHmac("sha256",t).update(`${sl}|${e}`,"ascii").digest("base64url")}var al="decision-intake",In=6e4,ul=In/1e3+5,Ei=3,Nr=15;function dl(t,e,n=60,r=Math.floor(Date.now()/1e3)){if(!t)throw new Error("cannot mint a decision token without a secret");let o=d=>Buffer.from(d).toString("base64url"),s=o(JSON.stringify({alg:"HS256",typ:"JWT"})),i={tenant:e.tenant,app:e.app,fn:e.fn,...e.provider?{provider:e.provider}:{},session_id:"decision-intake",sub:`machine:${e.tenant}/${e.app}/${e.fn}`,aud:al,iat:r,exp:r+Math.max(1,n)},a=o(JSON.stringify(i)),u=Ur.createHmac("sha256",t).update(`${s}.${a}`,"ascii").digest("base64url");return`${s}.${a}.${u}`}function ll(t){if(t===null||typeof t!="object")return!1;let e=t;return typeof e.runtime_id=="string"&&e.runtime_id!==""&&typeof e.ready=="boolean"&&typeof e.address=="string"&&e.address!==""&&typeof e.last_seen=="string"&&!Number.isNaN(Date.parse(e.last_seen))}async function cl(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/runtimes?app=${encodeURIComponent(t.app)}&fn=${encodeURIComponent(t.fn)}`,r;try{r=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"},signal:AbortSignal.timeout(In)})}catch(u){throw(u instanceof Error?u.name:void 0)==="TimeoutError"?new Le(`runtime presence read for (app=${t.app} fn=${t.fn}) timed out after ${In}ms \u2014 the control plane is not answering; no replica can be discovered`):u}if(!r.ok)throw new Error(`runtime presence read failed: GET ${n} \u2192 ${r.status}`);let o=await r.json();if(!Array.isArray(o.runtimes))throw new Error(`runtime presence read returned no "runtimes" array (GET ${n})`);let s=[],i=[];for(let u of o.runtimes){if(!ll(u)){s.push(JSON.stringify(u));continue}i.push(u)}if(s.length>0)throw new Error(`runtime presence read for (app=${t.app} fn=${t.fn}) returned ${s.length} malformed presence row(s) \u2014 the control plane's runtimes schema drifted (runtime_id/address/ready/last_seen are all required): ${s.slice(0,3).join("; ")}`);let a=(t.nowMs??Date.now())/1e3;return i.filter(u=>u.ready===!0&&a-Date.parse(u.last_seen)/1e3<=Nr)}var pl=Nr*1e3,fl=Nr*500,ml=256,Tn=class{constructor(e){this.opts=e;this.nowMs=e.nowMs??Date.now}opts;replicas=new Map;nowMs;presenceCache=new Map;get enabled(){return this.opts.secret!==""}async targetFor(e){let n=await this.opts.targetFor(e);return n.sharedApp===!0&&this.opts.shared===void 0?null:n}async decide(e,n){let r=await this.targetFor(n);if(r===null)throw new Le("the shared-endpoint lane keeps the session dial \u2014 the stateless direct path serves caller-org functions");let o=r.appSlug,s=r.fnName??this.opts.fnName,i=r.sharedApp===!0,a=i?this.opts.shared.apiKey:this.opts.apiKey,u=`${a}\0${o}\0${s}`,d=await this.ensureRows(u,a,o,s);if(d.length===0)throw new Le(`no ready runtime for (org app=${o} fn=${s}) \u2014 nothing is serving decisions right now`);let l=null;for(let p=0;p<Ei;p++){let y=this.pick(d);if(y===null)throw new Le(`every discovered runtime for app=${o} failed at the transport layer \u2014 no replica took the decision`);let c=i&&r.sharedOrgId?r.sharedOrgId:this.opts.orgId,m=dl(il(this.opts.secret,c),{tenant:this.opts.orgId,app:o,fn:s,...i&&r.sharedOrgId?{provider:r.sharedOrgId}:{}},ul);try{return{...await this.post(y,e,m),target:{...r,fnName:s}}}catch(w){if(w instanceof it)throw w;l=w;let R=this.replicas.get(y)??{outstanding:0,cooldownUntil:0};R.cooldownUntil=this.nowMs()+5e3,this.replicas.set(y,R);let h=this.presenceCache.get(u);h&&(h.rows=h.rows.filter(v=>v.address!==y),h.rows.length===0&&this.presenceCache.delete(u))}}throw new Le(`no runtime for app=${o} answered within ${Ei} attempts: ${String(l instanceof Error?l.message:l)}`)}async ensureRows(e,n,r,o){let s=this.presenceCache.get(e);if(s)if(this.nowMs()-s.fetchedAtMs>pl)this.presenceCache.delete(e);else return this.nowMs()-s.fetchedAtMs>fl&&!s.refreshing&&(s.refreshing=!0,this.readPresence(n,r,o).then(a=>{this.presenceCache.set(e,{rows:a,fetchedAtMs:this.nowMs(),refreshing:!1})}).catch(()=>{s.refreshing=!1})),s.rows;let i=await this.readPresence(n,r,o);for(;this.presenceCache.size>=ml;){let a=this.presenceCache.keys().next();if(a.done)break;this.presenceCache.delete(a.value)}return this.presenceCache.set(e,{rows:i,fetchedAtMs:this.nowMs(),refreshing:!1}),i}readPresence(e,n,r){return cl({apiUrl:this.opts.apiUrl,apiKey:e,app:n,fn:r,fetchImpl:this.opts.fetchImpl,nowMs:this.nowMs()})}pick(e){let n=this.nowMs(),r=null,o=1/0;for(let s of e){let i=this.replicas.get(s.address);if(i!==void 0&&i.cooldownUntil>n)continue;let a=i?.outstanding??0;a<o&&(r=s.address,o=a)}return r}async post(e,n,r){let o=this.replicas.get(e)??{outstanding:0,cooldownUntil:0};o.outstanding+=1,this.replicas.set(e,o);try{let s=await fetch(`${e.replace(/\/+$/,"")}/v1/decision`,{method:"POST",headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},body:JSON.stringify(n),signal:AbortSignal.timeout(this.opts.timeoutMs??In)}),i=null;try{i=await s.json()}catch{i={error:{message:`runtime answered ${s.status} with a non-JSON body`,type:"upstream_error"}}}if(s.status>=500)throw new Error(`runtime ${e} answered ${s.status}`);if(s.status>=400)throw new it(s.status,i);return{status:s.status,body:i,completed:gl(i)}}finally{o.outstanding-=1}}};function gl(t){if(t===null||typeof t!="object")return null;let e=t,n=e.usage;if(n===null||typeof n!="object")return e;let r=n;return{...e,usage:{input_tokens:r.prompt_tokens??r.input_tokens,output_tokens:r.completion_tokens??r.output_tokens}}}var it=class extends Error{constructor(n,r){super(`runtime answered ${n}`);this.status=n;this.body=r}status;body},Le=class extends Error{};function Br(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function ue(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function ge(t,e,n,r="invalid_request_error",o){ue(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function jr(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);ue(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function Ue(t,e,n,r){ue(t,e,{type:"error",error:{type:n,message:r}})}function Ne(t,e,n){if(e==="anthropic"){ue(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}ue(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var yl=64*1024*1024,qt=class extends Error{};async function Oi(t,e=yl){let n=[],r=0;for await(let s of t){if(r+=s.length,r>e)throw new qt(`request body exceeds ${e} bytes`);n.push(s)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function wt(t,e,n,r){return{id:t,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:e,choices:[{index:0,delta:n,finish_reason:r}]}}function Ii(t){let e=t?.usage;if(!e||typeof e!="object")return{kind:"absent"};let n=e,r=u=>typeof u=="number"&&Number.isFinite(u)&&u>=0?u:null,o=r(n.input_tokens),s=r(n.output_tokens),i=u=>u??0,a=r(n.total_tokens)??i(o)+i(s);if(i(o)>0||i(s)>0){let u={input_tokens:i(o),output_tokens:i(s),total_tokens:a};return o===null||s===null?{kind:"partial",usage:u}:{kind:"counts",usage:u}}return o===null||s===null?{kind:"absent"}:{kind:"zero",usage:{input_tokens:0,output_tokens:0,total_tokens:a}}}function Pn(t){let e=Ii(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function Ai(t){let e=Pn(t);return e?{prompt_tokens:e.input_tokens,completion_tokens:e.output_tokens,total_tokens:e.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function wl(t,e){let n=Ii(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function _l(t){let e=t?.urun_timing;if(!e||typeof e!="object")return null;let n=e,r=u=>{let d=n[u];return typeof d=="number"&&Number.isSafeInteger(d)&&d>=0?d:null},o=r("queue_ms"),s=r("prefill_ms"),i=r("decode_ms"),a=r("total_ms");return o===null||s===null||i===null||a===null?null:{queue_ms:o,prefill_ms:s,decode_ms:i,total_ms:a}}function vl(t,e){let n=_l(e);n&&(t.timing=n)}function He(t,e){wl(t,e),vl(t,e)}async function Ln(t,e,n){if(typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}function Ti(t){let e=t?.output;if(!Array.isArray(e))return"";let n="";for(let r of e)if(!(r.type!=="reasoning"||!Array.isArray(r.content)))for(let o of r.content)o.type==="reasoning_text"&&(n+=String(o.text??""));return n}function bl(t){if(Array.isArray(t))return t.map(e=>{let n=e.function;return!n||typeof n!="object"?e:{type:"function",name:n.name,description:n.description,parameters:n.parameters,strict:n.strict}})}var Cn=class extends Error{};function kl(t,e){return Array.isArray(e)?e.map(n=>{let r=n;if(r.type==="text")return{type:t==="assistant"?"output_text":"input_text",text:r.text};if(r.type==="image_url"){let o=r.image_url??{};return{type:"input_image",image_url:o.url,detail:o.detail}}throw new Cn(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function Sl(t){let e=[];for(let n of t){if(n.role==="tool"){e.push({type:"function_call_output",call_id:n.tool_call_id,output:typeof n.content=="string"?n.content:JSON.stringify(n.content??"")});continue}if(n.role==="assistant"&&Array.isArray(n.tool_calls)){typeof n.content=="string"&&n.content&&e.push({role:"assistant",content:n.content});for(let r of n.tool_calls){let o=r.function??{};e.push({type:"function_call",call_id:r.id,name:o.name,arguments:o.arguments})}continue}e.push({...n,content:kl(n.role,n.content)})}return e}function Dr(t){return ft(t).map((e,n)=>({index:n,id:e.call_id??e.id??`call_${n}`,type:"function",function:{name:e.name??"",arguments:e.arguments??"{}"}}))}async function Rl(t,e,n,r,o,s){let i=$t(s.id),a=t.stream===!0;if(s.model=ve(t),s.stream=a,await Ln(s,e,typeof t.model=="string"?t.model:void 0),a){let h;try{h=await Lt({body:t,clients:e,store:n,stats:o,responseId:i,turn:s})}catch(v){if(v instanceof z){Ne(r,"openai",v);return}if(v instanceof Ee){ge(r,v.status,v.message);return}throw v}Br(r);for await(let v of h.events)v.type==="error"&&(s.streamError=!0),v.type==="response.completed"&&He(s,v.response),r.write(`event: ${String(v.type??"message")}
|
|
29
|
+
data: ${JSON.stringify(v)}
|
|
29
30
|
|
|
30
31
|
`);r.write(`data: [DONE]
|
|
31
32
|
|
|
32
|
-
`),r.end();return}let u;try{u=
|
|
33
|
+
`),r.end();return}let u;try{u=ht(n,t)}catch(h){if(h instanceof Ee){ge(r,h.status,h.message);return}throw h}t.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(u))-Buffer.byteLength(JSON.stringify(t.input)));let d=t.store!==!1,l=ve(t);o.model(l);let p=Array.isArray(t.tools)&&t.tools.length>0,y;try{y=await br(e,t,u,!1,xe(s))}catch(h){if(h instanceof z){Ne(r,"openai",h);return}if(h instanceof Ee){ge(r,h.status,h.message);return}throw h}let c=[],m=null;for await(let h of o.track(y,l,s)){if(c.push(h),h.type==="error"){jr(r,h);return}h.type==="response.completed"&&(m=h.response)}if(m==null){ge(r,502,"upstream produced no response.completed event","upstream_error");return}He(s,m);let w=gt(c,m),R=st(p,ft(m).length,w);if(R){ge(r,502,ot(l,R),"upstream_error");return}d&&n.remember(i,u,vr(m,w),d),ue(r,200,{...m,id:i})}async function xl(t,e,n,r,o){let s=t.messages;if(!Array.isArray(s)){ge(n,400,"chat/completions requires a messages array");return}let i=String(t.model??"urun"),a=Bs(o.id),u;try{u=Sl(s)}catch(x){if(x instanceof Cn){ge(n,400,x.message);return}throw x}let d=ve(t);r.model(d),o.model=d,o.stream=t.stream===!0,await Ln(o,e,typeof t.model=="string"?t.model:void 0);let l=Array.isArray(t.tools)&&t.tools.length>0;if(t.stream!==!0&&e.stateless?.enabled){let x;try{x=await e.stateless.targetFor(typeof t.model=="string"?t.model:void 0)}catch(L){if(L instanceof z){Ne(n,"openai",L);return}throw L}if(x!==null){try{let L=await e.stateless.decide(t,typeof t.model=="string"?t.model:void 0);o.catalog=xt(L.target),He(o,L.completed),ue(n,200,{...L.body,id:a})}catch(L){if(L instanceof z){Ne(n,"openai",L);return}if(L instanceof it){ue(n,L.status,L.body);return}if(L instanceof Le){ge(n,503,L.message,"upstream_error");return}throw L}return}}let p;try{p=await e.createResponse({model:t.model,input:u,stream:!0,tools:bl(t.tools),tool_choice:t.tool_choice,temperature:t.temperature,top_p:t.top_p,stop:t.stop,max_output_tokens:t.max_completion_tokens??t.max_tokens,...t.reasoning_effort!==void 0?{reasoning_effort:t.reasoning_effort}:{},...t.chat_template_kwargs!==void 0?{chat_template_kwargs:t.chat_template_kwargs}:{},...t.response_format!==void 0?{response_format:t.response_format}:{}},xe(o))}catch(x){if(x instanceof z){Ne(n,"openai",x);return}throw x}if(t.stream===!0){Br(n);let x=[],L=new We,B="";n.write(`data: ${JSON.stringify(wt(a,i,{role:"assistant"},null))}
|
|
33
34
|
|
|
34
|
-
`);for await(let
|
|
35
|
+
`);for await(let se of r.track(p,d,o)){x.push(se);let ie=mt(se);if(se.type==="response.output_text.delta"&&typeof se.delta=="string")B+=se.delta,n.write(`data: ${JSON.stringify(wt(a,i,{content:se.delta},null))}
|
|
35
36
|
|
|
36
|
-
`);else if(
|
|
37
|
+
`);else if(se.type==="response.reasoning_text.delta"&&typeof se.delta=="string")n.write(`data: ${JSON.stringify(wt(a,i,{reasoning_content:se.delta},null))}
|
|
37
38
|
|
|
38
|
-
`);else if(
|
|
39
|
+
`);else if(ie){let{call:ke,isNew:Z}=L.add(ie),Se=Z?{index:ie.tool_index,id:ke.id??`call_${ie.tool_index}`,type:"function",function:{name:ke.name??"",arguments:ie.delta}}:{index:ie.tool_index,function:{arguments:ie.delta}};n.write(`data: ${JSON.stringify(wt(a,i,{tool_calls:[Se]},null))}
|
|
39
40
|
|
|
40
|
-
`)}else if(
|
|
41
|
+
`)}else if(se.type==="error"){o.streamError=!0,n.write(`data: ${JSON.stringify({error:se})}
|
|
41
42
|
|
|
42
|
-
`),n.end();return}}let
|
|
43
|
+
`),n.end();return}}let q=Dr(Me(x)),V=st(l,L.size+q.length,B);if(V){o.streamError=!0,n.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:ot(d,V)}})}
|
|
43
44
|
|
|
44
|
-
`),n.end();return}
|
|
45
|
+
`),n.end();return}L.size===0&&q.length>0&&n.write(`data: ${JSON.stringify(wt(a,i,{tool_calls:q},null))}
|
|
45
46
|
|
|
46
|
-
`);let
|
|
47
|
+
`);let de=L.size>0||q.length>0;n.write(`data: ${JSON.stringify(wt(a,i,{},de?"tool_calls":"stop"))}
|
|
47
48
|
|
|
48
|
-
`);let
|
|
49
|
+
`);let fe=Pn(Me(x));He(o,Me(x)),fe&&n.write(`data: ${JSON.stringify({id:a,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:i,choices:[],usage:Ai(Me(x))})}
|
|
49
50
|
|
|
50
51
|
`),n.write(`data: [DONE]
|
|
51
52
|
|
|
52
|
-
`),n.end();return}let y="",
|
|
53
|
-
data: ${JSON.stringify({type:
|
|
53
|
+
`),n.end();return}let y="",c="",m=[],w=new We;for await(let x of r.track(p,d,o)){m.push(x),x.type==="response.output_text.delta"&&typeof x.delta=="string"&&(y+=x.delta),x.type==="response.reasoning_text.delta"&&typeof x.delta=="string"&&(c+=x.delta);let L=mt(x);if(L&&w.add(L),x.type==="error"){jr(n,x);return}}let R=Me(m);He(o,R);let h=Dr(R),v=h.length>0?h:Dr({output:w.items()}),b=st(l,v.length,y);if(b){ge(n,502,ot(d,b),"upstream_error");return}let $=v.map(({index:x,...L})=>L),S={role:"assistant",content:y||null},E=c||Ti(R);E&&(S.reasoning_content=E),$.length>0&&(S.tool_calls=$),ue(n,200,{id:a,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:i,choices:[{index:0,message:S,finish_reason:$.length>0?"tool_calls":"stop"}],usage:Ai(R)})}var El="reflex-latest";async function Ol(t,e,n,r,o){if(t===null||typeof t!="object"||Array.isArray(t)){ge(n,400,"systemone requires a JSON object body");return}delete t.key;let s=typeof t.model=="string"&&t.model?t.model:El,i=ve({model:s});if(o.model=i,o.stream=!1,t.state===void 0||t.state===null){ge(n,400,"systemone requires a state (string or JSON structure)");return}let a=t.questions;if(a===null||typeof a!="object"||Array.isArray(a)||Object.keys(a).length===0){ge(n,400,"systemone requires a questions object (question id -> {kind, options, ...})");return}if(t.media!==void 0&&!Array.isArray(t.media)){ge(n,400,"systemone media, when present, must be an array");return}if(r.model(i),await Ln(o,e,s),e.stateless?.enabled){let c;try{c=await e.stateless.targetFor(s)}catch(m){if(m instanceof z){Ne(n,"openai",m);return}throw m}if(c!==null){try{let m=await e.stateless.decide(t,s);o.catalog=xt(m.target),He(o,m.completed),ue(n,200,m.body)}catch(m){if(m instanceof z){Ne(n,"openai",m);return}if(m instanceof it){ue(n,m.status,m.body);return}if(m instanceof Le){ge(n,503,m.message,"upstream_error");return}throw m}return}}let u;try{u=await e.createResponse({model:s,input:[{role:"user",content:JSON.stringify(t)}],stream:!0},xe(o))}catch(c){if(c instanceof z){Ne(n,"openai",c);return}throw c}let d=[];try{for await(let c of r.track(u,i,o))if(d.push(c),c.type==="error"){jr(n,c);return}}catch(c){if(c instanceof z){Ne(n,"openai",c);return}throw c}let l=Me(d);if(l==null){ge(n,502,"upstream produced no response.completed event","upstream_error");return}He(o,l);let p=gt(d,l),y;try{y=JSON.parse(p)}catch{ge(n,502,"systemone answer was not valid JSON","upstream_error");return}if(y===null||typeof y!="object"||Array.isArray(y)){ge(n,502,"systemone answer was not a JSON object","upstream_error");return}ue(n,200,y)}function Al(t){return typeof t=="string"?t:Array.isArray(t)?t.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var Ht=class extends Error{};function $n(t){return new Ht(`unsupported anthropic content block type "${t}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function Il(t){if(Array.isArray(t))return t.map(e=>{let n=e;return{type:"function",name:n.name,description:n.description,parameters:n.input_schema}})}function Tl(t){if(t==null)return;let e=t.type;if(e==="auto")return"auto";if(e==="none")return"none";if(e==="any")return"required";if(e==="tool")return{type:"function",name:t.name};throw new Ht(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function Pl(t){if(typeof t=="string")return t;if(t==null)return"";if(!Array.isArray(t))throw $n(typeof t);let e="";for(let n of t){let r=String(n.type??"");if(r!=="text")throw $n(`tool_result > ${r}`);e+=String(n.text??"")}return e}function Cl(t){let{role:e,content:n}=t;if(typeof n=="string")return[{role:e,content:n}];if(n==null)return[];if(!Array.isArray(n))throw $n(typeof n);let r=[],o="",s=()=>{o&&(r.push({role:e,content:o}),o="")};for(let i of n){let a=String(i.type??"");if(a==="text")o+=String(i.text??"");else if(a==="tool_use")s(),r.push({type:"function_call",call_id:i.id,name:i.name,arguments:JSON.stringify(i.input??{})});else if(a==="tool_result")s(),r.push({type:"function_call_output",call_id:i.tool_use_id,output:Pl(i.content)});else{if(a==="thinking"||a==="redacted_thinking")continue;throw $n(a)}}return s(),r}function Fr(t){return ft(t).map((e,n)=>{let r;try{r=JSON.parse(e.arguments||"{}")}catch{throw new Error(`upstream function_call "${e.name??""}" arguments are not valid JSON`)}return{type:"tool_use",id:e.call_id??e.id??`toolu_${n}`,name:e.name??"",input:r}})}async function $l(t,e,n,r,o){let s=t.messages;if(!Array.isArray(s)){Ue(n,400,"invalid_request_error","messages requires a messages array");return}if(typeof t.max_tokens!="number"||!Number.isInteger(t.max_tokens)||t.max_tokens<1){Ue(n,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(t.top_k!==void 0){Ue(n,400,"invalid_request_error","top_k is not supported: the uRun serve lane exposes temperature/top_p/stop_sequences only");return}if(t.stop_sequences!==void 0&&(!Array.isArray(t.stop_sequences)||t.stop_sequences.some(x=>typeof x!="string"))){Ue(n,400,"invalid_request_error","stop_sequences must be an array of strings");return}let i=String(t.model??"urun"),a=js(o.id),u=[],d;try{t.system&&u.push({role:"system",content:Al(t.system)});for(let x of s)u.push(...Cl(x));d=Tl(t.tool_choice)}catch(x){if(x instanceof Ht){Ue(n,400,"invalid_request_error",x.message);return}throw x}let l=ve(t);r.model(l),o.model=l,o.stream=t.stream===!0,await Ln(o,e,typeof t.model=="string"?t.model:void 0);let p=Array.isArray(t.tools)&&t.tools.length>0,y;try{y=await e.createResponse({model:t.model,input:u,stream:!0,tools:Il(t.tools),tool_choice:d,temperature:t.temperature,max_output_tokens:t.max_tokens,top_p:t.top_p,stop:t.stop_sequences},xe(o))}catch(x){if(x instanceof z){Ne(n,"anthropic",x);return}throw x}let c=(x,L)=>{n.destroyed||n.write(`event: ${x}
|
|
54
|
+
data: ${JSON.stringify({type:x,...L})}
|
|
54
55
|
|
|
55
|
-
`)};if(t.stream===!0){let
|
|
56
|
-
data: ${JSON.stringify({type:"error",error:{type:"api_error",message:
|
|
56
|
+
`)};if(t.stream===!0){let x=r.track(y,l,o);Br(n),c("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:i,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let L=0,B="",q=0,V="none",de=()=>V,fe=-1,se=-1,ie=new We,ke=[],Z=()=>{V!=="none"&&(V="none",c("content_block_stop",{index:fe}))},Se=(F,me)=>{Z(),V=F,fe=q++,c("content_block_start",{index:fe,content_block:me})};for await(let F of x){if(n.destroyed)break;ke.push(F);let me=mt(F);if(F.type==="response.reasoning_text.delta"&&typeof F.delta=="string")de()!=="thinking"&&Se("thinking",{type:"thinking",thinking:""}),c("content_block_delta",{index:fe,delta:{type:"thinking_delta",thinking:F.delta}});else if(F.type==="response.output_text.delta"&&typeof F.delta=="string")de()!=="text"&&Se("text",{type:"text",text:""}),L+=1,B+=F.delta,c("content_block_delta",{index:fe,delta:{type:"text_delta",text:F.delta}});else if(me){let{call:Ce}=ie.add(me);(de()!=="tool"||me.tool_index!==se)&&(se=me.tool_index,Se("tool",{type:"tool_use",id:Ce.id??`call_${me.tool_index}`,name:Ce.name??"",input:{}})),me.delta&&c("content_block_delta",{index:fe,delta:{type:"input_json_delta",partial_json:me.delta}})}else if(F.type==="error"){o.streamError=!0,c("error",{error:{type:"api_error",message:JSON.stringify(F)}}),n.end();return}}if(n.destroyed)return;let Ae=Me(ke),Ke=ft(Ae),H=st(p,ie.size+Ke.length,B);if(H){o.streamError=!0,c("error",{error:{type:"api_error",message:ot(l,H)}}),n.end();return}Z();let I=[];if(ie.size===0)try{I=Fr(Ae)}catch(F){o.streamError=!0,c("error",{error:{type:"api_error",message:String(F instanceof Error?F.message:F)}}),n.end();return}q===0&&I.length===0&&(Se("text",{type:"text",text:""}),Z()),I.forEach(F=>{let me=q++;c("content_block_start",{index:me,content_block:{...F,input:{}}}),c("content_block_delta",{index:me,delta:{type:"input_json_delta",partial_json:JSON.stringify(F.input??{})}}),c("content_block_stop",{index:me})});let Y=ie.size>0||I.length>0,Ie=Pn(Ae);He(o,Ae),c("message_delta",{delta:{stop_reason:Y?"tool_use":"end_turn"},usage:Ie?{input_tokens:Ie.input_tokens,output_tokens:Ie.output_tokens}:{output_tokens:L}}),c("message_stop",{}),n.end();return}let m="",w="",R=[],h=new We;for await(let x of r.track(y,l,o)){R.push(x),x.type==="response.output_text.delta"&&typeof x.delta=="string"&&(m+=x.delta),x.type==="response.reasoning_text.delta"&&typeof x.delta=="string"&&(w+=x.delta);let L=mt(x);if(L&&h.add(L),x.type==="error"){let B=x.error,q=B?.type==="invalid_request_error";Ue(n,q?400:502,q?"invalid_request_error":"api_error",B?.message??JSON.stringify(x));return}if(n.destroyed)break}if(n.destroyed)return;let v=Me(R);He(o,v);let b;try{b=Fr(v),b.length===0&&h.size>0&&(b=Fr({output:h.items()}))}catch(x){Ue(n,502,"api_error",String(x instanceof Error?x.message:x));return}let $=st(p,b.length,m);if($){Ue(n,502,"api_error",ot(l,$));return}let S=[],E=w||Ti(v);E&&S.push({type:"thinking",thinking:E}),(m||b.length===0)&&S.push({type:"text",text:m}),S.push(...b),ue(n,200,{id:a,type:"message",role:"assistant",content:S,model:i,stop_reason:b.length>0?"tool_use":"end_turn",usage:Pn(v)??{input_tokens:0,output_tokens:0}})}function Ml(t){$e(process.stdout,t)}function Pi(t){let{clients:e,apiKey:n,identity:r,statusOf:o}=t,s=typeof e=="function"?e:()=>e,i=t.requestLog??Ml,a=t.observe,u=new nt;return(d,l)=>{let p=new URL(d.url??"/","http://localhost"),y=p.pathname==="/v1/messages"||p.pathname.startsWith("/v1/messages/"),c=(v,b,$,S,E)=>y?Ue(l,v,S,b):ge(l,v,b,$,E),m=_r(p.pathname),w=p.pathname.startsWith("/partners/"),R=m||w?{id:Ct(),startedAt:performance.now(),streamError:!1,model:null,stream:null,org:null,ttftMs:null,catalog:{kind:"unresolved",reason:"no lane resolved a model for this request"},promptTokens:null,completionTokens:null,outputUnits:null,sessionId:null,timing:null}:null;R&&(l.setHeader(Ds,R.id),l.once("close",()=>{let v=qs({turn:R,method:d.method??"",path:p.pathname,res:l});i(Hs(v)),a?.(v),Ko({record:v,ledger:t.ledger??null,req:d})}));let h=()=>Fs(p.pathname,R);(async()=>{if(d.method==="GET"&&p.pathname==="/healthz"){ue(l,200,{ok:!0});return}if(d.method==="GET"&&p.pathname==="/stats"){ue(l,200,r?{identity:r,...u.snapshot()}:u.snapshot());return}if(n&&(d.headers.authorization??"")!==`Bearer ${n}`){c(401,"invalid local proxy api key","authentication_error","authentication_error");return}let v=p.pathname===en?"canonical":p.pathname===It?"huggingface":null;if(v){if(d.method!=="POST"){c(405,`${p.pathname} is a POST batch lookup`,"invalid_request_error","invalid_request_error");return}let E=t.usage;if(!E){c(501,`${p.pathname} needs a usage seam (ProxyHandlerOptions.usage) to read the per-request inference ledger; this proxy has none, and answering an empty result would be indistinguishable from "nothing is priced yet"`,"not_implemented","not_implemented_error");return}let x=E.authorize(d),L;try{L=await Oi(d,Zo)}catch(V){if(V instanceof qt){c(413,V.message,"request_too_large","request_too_large");return}c(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let B=V=>E.lookup(x,V),q;try{if(v==="canonical"){let V=es(L),de=await B(V);tn(de,V),q={requests:de}}else q=await rs(L,B)}catch(V){if(V instanceof Ve){c(V.status,V.message,"invalid_request_error","invalid_request_error");return}throw V}ue(l,200,q);return}if(p.pathname.startsWith("/partners/")){c(404,`no partner adapter for ${d.method} ${p.pathname} \u2014 the partner surface currently carries ${It}`,"invalid_request_error","not_found_error");return}let b=await s(d);if(R&&(R.org=b.tenant??null),d.method==="GET"&&p.pathname==="/v1/models"){if(p.searchParams.get("format")==="openrouter"){if(!b.openRouterModels){c(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}ue(l,200,await b.openRouterModels());return}ue(l,200,await b.listModels());return}if(d.method==="GET"&&p.pathname===oi){if(!b.listImageModels){c(501,"GET /v1/images/models needs a router-backed client (listImageModels)","not_implemented","not_implemented_error");return}ue(l,200,await b.listImageModels());return}if(ri(p.pathname)){if(!b.supportsImages){c(501,`the image lanes need a router-backed client (supportsImages) to gate model capability for ${p.pathname}`,"not_implemented","not_implemented_error");return}let E={clients:b,imageModels:b.supportsImages,stats:u,turn:h()};p.pathname===Rn?await mi(d,l,E):await fi(d,l,E);return}if(d.method==="GET"&&p.pathname===ki){if(!b.listSpeechToTextModels){c(501,"GET /v1/audio/models needs a router-backed client (listSpeechToTextModels)","not_implemented","not_implemented_error");return}ue(l,200,await b.listSpeechToTextModels());return}if(bi(p.pathname)){if(!b.supportsTranscription){c(501,`the transcription lane needs a router-backed client (supportsTranscription) to gate model capability for ${An}`,"not_implemented","not_implemented_error");return}let E={clients:b,transcriptionModels:b.supportsTranscription,stats:u,turn:h()};await xi(d,l,E);return}if(_i(p.pathname)){if(!b.supportsSpeech){c(501,`the speech lane needs a router-backed client (supportsSpeech) to gate model capability for ${jt}`,"not_implemented","not_implemented_error");return}let E={clients:b,speechModels:b.supportsSpeech,stats:u,turn:h()};await vi(d,l,E);return}if(d.method!=="POST"){c(404,`no route for ${d.method} ${p.pathname}`,"invalid_request_error","not_found_error");return}let $;try{$=await Oi(d)}catch(E){if(E instanceof qt){c(413,E.message,"request_too_large","request_too_large");return}c(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let S=Buffer.byteLength(JSON.stringify($));if(p.pathname==="/v1/responses"){u.request("responses",S,S),await Rl($,b,Mt(b),l,u,h());return}if(p.pathname==="/v1/chat/completions"){u.request("chat_completions",S,S),await xl($,b,l,u,h());return}if(p.pathname==="/v1/systemone"){u.request("systemone",S,S),await Ol($,b,l,u,h());return}if(p.pathname==="/v1/messages"){u.request("messages",S,S),await $l($,b,l,u,h());return}if(p.pathname==="/v1/messages/count_tokens"){u.request("count_tokens",S,S),Ue(l,404,"not_found_error","count_tokens is not implemented by the uRun proxy: the serve transport exposes no token-count lane, and the proxy will not fabricate counts");return}c(404,`no route for POST ${p.pathname}`,"invalid_request_error","not_found_error")})().catch(v=>{let b=String(v instanceof Error?v.message:v);if(!l.headersSent){let $=o?.(v)??null;if($){c($.status,b,$.openaiType,$.anthropicType);return}if(v instanceof Mn.SessionAllocationError){let S=gn(v.code);if(S){let E=v.retryAfterSeconds??(S.status===429?30:void 0);E!==void 0&&l.setHeader("Retry-After",String(E)),c(S.status,Or(S,v.code??void 0),S.openaiType,S.anthropicType,S.code);return}}if(v instanceof Mn.SessionFailedError){let S=hn(v);if(S){let E=S.status===429?30:void 0;E!==void 0&&l.setHeader("Retry-After",String(E)),c(S.status,Or(S,v.status.sessionId?`session ${v.status.sessionId}`:void 0),S.openaiType,S.anthropicType,S.code);return}}if(v instanceof te){c(502,b,"upstream_error","api_error");return}c(500,b,"proxy_error","api_error","proxy_error");return}R&&(R.streamError=!0),l.destroyed||(y?l.write(`event: error
|
|
57
|
+
data: ${JSON.stringify({type:"error",error:{type:"api_error",message:b}})}
|
|
57
58
|
|
|
58
|
-
`):l.write(`data: ${JSON.stringify({error:{message:
|
|
59
|
+
`):l.write(`data: ${JSON.stringify({error:{message:b,type:"upstream_error"}})}
|
|
59
60
|
|
|
60
|
-
`)),l.end()})}}var dn=(t,e)=>Object.assign(new Error(e),{status:t});function Ni(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}async function Di(t,e){if(!t.supportsAudio)throw dn(501,"the realtime lane needs a router-backed client (supportsAudio) to gate audio modality \u2014 hand-built clients without the catalog oracle are refused, never allowed silently");let n;try{n=await t.supportsAudio(e)}catch(i){throw i instanceof re?dn(404,i.message):i}if(!n)throw dn(404,`model ${JSON.stringify(e??"(default)")} is not audio-capable (catalog task is neither stt nor tts) \u2014 this realtime lane serves only audio-task models; chat models ride /v1/chat/completions`);if(!t.openAudio)throw dn(501,"an audio-capable model needs a router-backed client (openAudio) to open its native session audio bridge \u2014 refusing loudly rather than binding a voiceless session");let r={audio:await t.openAudio(e)},o={clients:t,voice:n,audioSession:r};return e!==void 0&&(o.model=e),o}_();var cn=require("@urun-sh/core"),we=class extends Error{constructor(n,r=401){super(n);this.status=r;this.name="ProxyAuthError"}status},it=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function Et(t){let e=(t??"").trim();if(e==="")throw new we("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let n=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!n)throw new we("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return n[1].trim()}var Ju=60,Ku=6e4,zu=4096;function Yu(t){return t===401||t===403}var ln=class{constructor(e){this.opts=e;this.mint=e.mint??cn.createClientToken,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new we("empty Authorization bearer token");let n=this.cache.get(e);if(n&&this.now()-n.at<Ku)return{apiKey:e,orgId:n.orgId};let r;try{r=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:Ju})).orgId}catch(o){throw o instanceof cn.ClientTokenError&&Yu(o.status)?(this.cache.delete(e),new we("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new it(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${o instanceof Error?o.message:String(o)}`)}if(!r)throw new it("the control plane verified the API key but returned no org_id \u2014 refusing to route a request with no tenancy (control-plane contract violation)");if(this.cache.size>=zu){let o=this.cache.keys().next();o.done||this.cache.delete(o.value)}return this.cache.set(e,{orgId:r,at:this.now()}),{apiKey:e,orgId:r}}clear(){this.cache.clear()}};_();var yn=require("crypto");_();var At=require("@urun-sh/core");_();async function Fi(t){let n=await(t.fetchImpl??fetch)(`${t.catalogUrl}/model_catalog?select=model_id,variant,gpu_spec,engine,task,description,size_gb,hf_repo,engine_args,updated_at`,{headers:{apikey:t.anonKey,Authorization:`Bearer ${t.anonKey}`}});if(!n.ok)throw new Error(`model_catalog fetch failed: ${n.status}`);return await n.json()}_();_();_();function Bi(t,e,n,r){let o=t.response??{},i={request_id:n,consumer_id:r,stream:!0,kind:"chat",messages:e};return typeof o.instructions=="string"&&(i.instructions=o.instructions),Array.isArray(o.modalities)&&(i.modalities=o.modalities),typeof o.temperature=="number"&&(i.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(i.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(i.tools=o.tools),i}function ji(t,e,n){let r={request_id:e,consumer_id:n,stream:!!t.stream,kind:"responses",input:t.input};return t.model&&(r.model=t.model),t.tools!==void 0&&(r.tools=t.tools),t.tool_choice!==void 0&&(r.tool_choice=t.tool_choice),typeof t.temperature=="number"&&(r.temperature=t.temperature),typeof t.max_output_tokens=="number"&&(r.max_output_tokens=t.max_output_tokens),typeof t.top_p=="number"&&(r.top_p=t.top_p),Array.isArray(t.stop)&&t.stop.length>0&&(r.stop=t.stop),typeof t.reasoning_effort=="string"&&(r.reasoning_effort=t.reasoning_effort),t.chat_template_kwargs!==void 0&&(r.chat_template_kwargs=t.chat_template_kwargs),t.response_format!==void 0&&(r.response_format=t.response_format),r}_();_();function qi(t){if(t instanceof Error)return{type:"error",error:{type:"urun_error",code:t.name||null,message:t.message}};if(t&&typeof t=="object"&&t.t==="error"){let e=t,n=e.body??{},r=n.error,o=r?.message??n.message??`unreadable serve error body: ${JSON.stringify(e.body??null)}`;return{type:"error",error:{type:r?.type??"urun_error",code:e.code??r?.code??null,message:o}}}return{type:"error",error:{type:"urun_error",code:null,message:String(t)}}}var Xu="llm-resp";async function*dr(t,e){let n=`${Xu}:${e}`,r=`resp_${e}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};let o=new Map;for await(let i of t.stream(n).messages()){let s=i;if(s.t==="delta"){if(typeof s.delta=="string"&&(yield{type:"response.output_text.delta",item_id:r,delta:s.delta}),typeof s.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:r,delta:s.reasoning}),Array.isArray(s.tool_calls))for(let a of s.tool_calls){let u=typeof a.index=="number"?a.index:0,d=o.get(u)??{};a.id&&(d.id=a.id),a.function?.name&&(d.name=a.function.name),o.set(u,d),yield{type:"response.function_call_arguments.delta",item_id:`fc_${e}_${u}`,tool_index:u,call_id:d.id,name:d.name,delta:a.function?.arguments??""}}}else if(s.t==="response"){yield{type:"response.completed",response:{id:r,status:"completed",...s.body}};return}else if(s.t==="error"){yield qi(s);return}}}var Qu="llm",pn=class{constructor(e){this.session=e;this.sessionTag=e.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(e){e.session_tag=this.sessionTag,this.session.doc(Qu).set({requests:{[e.request_id]:{payload:e,consumer_id:e.consumer_id,stream:e.stream}}})}sendResponses(e,n){let r=ji(e,n,this.consumerId);return this.write(r),dr(this.session,n)}sendResponseCreate(e,n,r){let o=Bi(e,n,r,this.consumerId);return this.write(o),dr(this.session,r)}};var Hi=0;function Zu(){return Hi+=1,`req_${Date.now().toString(36)}_${Hi.toString(36)}`}var fn=class{transport;constructor(e){this.transport=new pn(e)}responses={create:async e=>{let n=Zu(),r=this.transport.sendResponses(e,n);return Object.assign((async function*(){yield*r})(),{requestId:n})}}};_();var Gi=require("@urun-sh/core"),lr=2;function ed(t){let e=t?.type;return e==="response.created"||e==="response.in_progress"}function td(t){let e=t?.type;return e==="response.completed"||e==="error"}function Vi(t){return async function(n,r){return(async function*(){for(let i=1;i<=lr;i++){let{app:s,entry:a,identity:u}=await t.sessionFor(n.model);r?.({sessionId:t.sessionIdOf?.(a)??null,identity:u});let d=!1,l=!1;try{let c=await t.createStream(a,n);for await(let y of c){if(td(y)&&(d=!0),ed(y)){if(i>1)continue}else l=!0;yield y}}catch(c){if(c instanceof Gi.SessionFailedError&&(c.kind==="never-live"||c.kind==="create-failed"))throw c;if(i<lr&&!l){await t.evict(s,a);continue}throw c}if(d)return;if(i<lr&&!l){await t.evict(s,a);continue}return}})()}}_();var nd=3,rd="llm",od=new Set(["paused","error"]);function Wi(t,e){let n=t.doc(rd),r=typeof n.onConnectionState=="function",o=typeof t.onPhase=="function";if(!r&&!o)throw new Error(`session for app "${e}" exposes neither a doc connection-state surface nor onPhase \u2014 session-gone detection requires @urun-sh/core Session (SessionDocument.onConnectionState / Session.onPhase); pooling a session with no native lifecycle signal would silently swallow requests dispatched after the platform closes it`);let i=null,s=!1,a=new Set,u=[],d=l=>{if(!i){i=l;for(let c of[...a])c(l);a.clear()}};return r&&u.push(n.onConnectionState(l=>{s&&(l.consecutiveFailures<nd||d(new le(`the pooled session for app "${e}" is gone: its doc backhaul failed ${l.consecutiveFailures} consecutive times (last close ${l.lastCloseCode===null?"by provider liveness reap":`code ${l.lastCloseCode}`}${l.lastCloseReason?`, reason ${JSON.stringify(l.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`)))})),o&&u.push(t.onPhase(l=>{if(l.name==="live"){s=!0;return}s&&od.has(l.name)&&d(new le(`the pooled session for app "${e}" lost its compute binding (phase "${l.name}"). The entry is evicted; retry to get a fresh session. The named session itself is not over \u2014 it stays attachable by name.`))})),{gone:()=>i,onGone(l){return i?(l(i),()=>{}):(a.add(l),()=>a.delete(l))},dispose(){a.clear();for(let l of u.splice(0))l()}}}async function*cr(t,e){let n=e.gone();if(n)throw n;let r=t[Symbol.asyncIterator](),o,i=new Promise((s,a)=>{o=e.onGone(u=>a(u))});i.catch(()=>{});try{for(;;){let s=await Promise.race([r.next(),i]);if(s.done)return;yield s.value}}finally{o?.(),r.return?.(void 0)}}function Ji(t,e,n,r){let o=t[n];if(typeof o!="function")throw new Error(`app "${e}" has no function "${n}" (set URUN_FUNCTION)`);let i=r?o(void 0,r):o();if(typeof i.end!="function")throw new Error(`app "${e}" function "${n}" returned a session without end()`);return i}function id(t,e){let{baseUrl:n,orgId:r,auth:o,subject:i}=t,s=e.fnName??t.fnName,a=ut(e),u=t.sessionStartOptions?.(a),d=e.sharedApp?{sharedApp:!0,...u}:u,l=E=>({session:E,responses:new fn(E),gone:Wi(E,a)});if(o.lane==="jwt"){let E=(0,At.App)(e.appSlug,{baseUrl:n,orgId:r,jwt:o.jwt});return l(Ji(E,e.appSlug,s,d))}let{apiKey:c,gatewayUrl:y}=o,g=(0,At.App)(e.appSlug,{baseUrl:n,orgId:r,getAccessToken:async()=>(await(0,At.createClientToken)(c,{baseUrl:y,expiresIn:300,allowedFunctions:[`${e.appSlug}/${s}`],...i===void 0?{}:{subject:i},...e.sharedApp?{sharedApp:!0}:{}})).token});return l(Ji(g,e.appSlug,s,d))}function sd(t){let e=t.session.id;if(typeof e!="string"||e.length===0)throw new Error("pooled session exposes no stable session id \u2014 the session-identity seam requires @urun-sh/core Session (session.id)");return e}var ad=new Set(["error","paused"]);function fr(t){let{auth:e,catalog:n}=t,r=null,o=s=>{let a=ut(s),u=id(t,s);return t.onSessionOpen&&typeof u.session.id=="string"&&u.session.id!==""&&t.onSessionOpen(a,u.session.id),u.gone.onGone(()=>{r?.evict(a,u)}),u},i=new $t({defaultApp:t.defaultApp,fnName:t.fnName,openSession:o,closeSession:async s=>{if(s.gone.dispose(),t.release==="detach"){if(typeof s.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');s.session.detach();return}await s.session.end()},listApps:e.lane==="api-key"?()=>zr({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,listCatalog:n?()=>Fi(n):null,listPrices:n?()=>Nr(n):null,shared:e.lane==="api-key"?()=>Cr({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,sessionKey:sd});return r=i,t.defaultApp!==null&&i.seed(t.defaultApp,o({appSlug:t.defaultApp})),i}async function pr(t){let e=t.whenLive;if(typeof e!="function")throw new Error("pooled session exposes no native whenLive() \u2014 the audio lane must wait for the session's own admission (cold wake: queued \u2192 demand \u2192 promote) before wiring media; a session without the core Session.whenLive surface cannot be served audio");await e.call(t)}function mr(t,e=Co){let n=new WeakMap,r=new WeakMap,o=new WeakMap;return{createResponse:Vi({sessionFor:i=>t.sessionFor(i),sessionIdOf:i=>{let s=i.session.id;return typeof s=="string"&&s.length>0?s:null},evict:(i,s)=>t.evict(i,s),createStream:async(i,s)=>(await pr(i.session),cr(await i.responses.responses.create(s),i.gone))}),listModels:()=>t.modelList(),openRouterModels:()=>t.openRouterModels(),supportsImages:(i,s)=>t.supportsImages(i,s),listImageModels:()=>t.imageModelList(),catalogIdentity:i=>t.catalogIdentityFor(i),servingAppSlug:async i=>await t.servingAppSlugFor(i),supportsAudio:i=>t.supportsAudio(i),openAudio:async i=>{let{entry:s}=await t.sessionFor(i),a=n.get(s);return a||(a=(async()=>(await pr(s.session),To(s.session,await e())))(),n.set(s,a),a.catch(()=>n.delete(s))),a},openVideo:async i=>{let{entry:s}=await t.sessionFor(i),a=r.get(s);return a||(a=Po(s.session),r.set(s,a)),a},openVideoOut:async i=>{let{entry:s}=await t.sessionFor(i),a=o.get(s);return a||(a=Mo(s.session),o.set(s,a)),a},sessionHandle:async i=>(await t.handleFor(i)).handle,createResponseOn:async(i,s)=>{let{entry:a}=await t.sessionForHandle(i);return await pr(a.session),cr(await a.responses.responses.create(s),a.gone)},onSessionEnd:async(i,s)=>{let{entry:a}=await t.sessionForHandle(i),u=a.session;if(typeof u.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let d=!1;return u.onPhase(l=>{if(d||!ad.has(l.name))return;d=!0;let c=u.endsAt??null;s({timeLeftMs:c?Math.max(0,c.getTime()-Date.now()):null,reason:l.reason??`session ${l.name}`})})}}}var ud=512;function _n(t){return`urun-inference-proxy-${(0,yn.createHash)("sha256").update(t).digest("hex").slice(0,32)}`}var mn=class{constructor(e){this.opts=e}opts;tenants=new Map;label(e,n){return Object.assign(Object.create(e),{tenant:n.orgId})}build(e){if(this.opts.build){let r=this.opts.build(e);return{router:r.router,clients:this.label(r.clients,e)}}let n=fr({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:Fn,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:_n(e.apiKey),release:"detach"});return{router:n,clients:this.label(mr(n),e)}}clientsFor(e){let n=this.tenants.get(e.apiKey);if(n)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,n),n.clients;for(;this.tenants.size>=ud;){let o=this.tenants.entries().next();if(o.done)break;let[i,s]=o.value;this.tenants.delete(i),s.router.closeAll().catch(()=>{})}let r=this.build(e);return this.tenants.set(e.apiKey,r),r.clients}get size(){return this.tenants.size}async closeAll(){let e=[...this.tenants.values()].map(r=>r.router);this.tenants.clear();let n=[];if(await Promise.all(e.map(async r=>{try{await r.closeAll()}catch(o){n.push(String(o instanceof Error?o.message:o))}})),n.length>0)throw new Error(`failed to close ${n.length} tenant backhaul(s): ${n.join("; ")}`)}},Ki=512,dd=1024,gn=class extends Error{constructor(){super(`conversation capacity reached (${Ki} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function ld(t,e,n,r){let o=fr({baseUrl:t.baseUrl,orgId:e.orgId,defaultApp:null,fnName:Fn,apiUrl:t.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:t.baseUrl},catalog:t.catalog,subject:n.subject,release:"detach",sessionStartOptions:i=>{let s=r.get(i);return s===void 0?void 0:{sessionId:s}},onSessionOpen:(i,s)=>{r.set(i,s)}});return{clients:mr(o),release:async()=>{await o.closeAll()}}}var hn=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,n){let r={id:n,subject:`${_n(e.apiKey)}:live:${n}`},o=new Map,i=this.opts.build?this.opts.build(e,r):ld(this.opts,e,r,o);return{id:n,backhaul:i,attachments:0,nativeRefs:o}}attachEntry(e,n){let r=this.byCaller.get(e.apiKey);r===void 0&&(r=new Map,this.byCaller.set(e.apiKey,r));let o=r.get(n);if(o!==void 0)return r.delete(n),r.set(n,o),o.attachments+=1,o;this.evictOverflow();let i=this.build(e,n);return i.attachments=1,r.set(n,i),i}get conversationCount(){let e=0;for(let n of this.byCaller.values())e+=n.size;return e}evictOverflow(){for(;this.conversationCount>=Ki;){let e=this.byCaller.keys().next();if(e.done)throw new gn;let n=e.value,r=this.byCaller.get(n),o=!1;for(let[i,s]of r)if(!(s.attachments>0)){r.delete(i),this.dropHandleIndexFor(n,i),s.backhaul.release().catch(()=>{}),o=!0;break}if(!o)throw r.size===0&&this.byCaller.delete(n),new gn;r.size===0&&this.byCaller.delete(n)}}dropHandleIndexFor(e,n){for(let[r,o]of this.byHandle)o.apiKey===e&&o.conversationId===n&&this.byHandle.delete(r)}connectionFor(e){let n=null,r=!1,o=()=>(n===null&&(n=this.attachEntry(e,(0,yn.randomUUID)())),n),i={createResponse:(s,a)=>o().backhaul.clients.createResponse(s,a),catalogIdentity:s=>o().backhaul.clients.catalogIdentity(s),servingAppSlug:s=>{let a=o().backhaul.clients.servingAppSlug;if(a===void 0)throw new Error("the conversation backhaul wires no servingAppSlug seam \u2014 the caller-org app a request resolves to cannot be known");return a(s)},listModels:()=>o().backhaul.clients.listModels(),sessionHandle:s=>o().backhaul.clients.sessionHandle(s),createResponseOn:(s,a)=>o().backhaul.clients.createResponseOn(s,a),onSessionEnd:(s,a)=>o().backhaul.clients.onSessionEnd(s,a)};return i.openAudio=async s=>{let a=o().backhaul.clients.openAudio;if(a===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return a(s)},i.openVideo=async s=>{let a=o().backhaul.clients.openVideo;if(a===void 0)throw new Error("the conversation backhaul wires no openVideo seam \u2014 video-in is unavailable on this deployment");return a(s)},i.openVideoOut=async s=>{let a=o().backhaul.clients.openVideoOut;if(a===void 0)throw new Error("the conversation backhaul wires no openVideoOut seam \u2014 video-out is unavailable on this deployment");return a(s)},i.supportsAudio=async s=>{let a=o().backhaul.clients.supportsAudio;if(a===void 0)throw new Error("the conversation backhaul wires no supportsAudio seam \u2014 the realtime lane cannot gate audio modality on this deployment");return a(s)},{clients:i,bindResumeHandle:s=>{let a=n;if(a===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=dd;){let u=this.byHandle.keys().next();if(u.done)break;this.byHandle.delete(u.value)}this.byHandle.set(s,{apiKey:e.apiKey,conversationId:a.id})},clientsForResume:async s=>{let a=this.byHandle.get(s),u=a===void 0?void 0:this.byCaller.get(a.apiKey),d=a===void 0||u===void 0?void 0:u.get(a.conversationId);if(a===void 0||d===void 0||a.apiKey!==e.apiKey)throw new le("the conversation behind this resume handle is gone (evicted at capacity, or the replica restarted) or the handle is not yours \u2014 reconnect without a handle to start a new conversation");return n=d,d.attachments+=1,d.backhaul.clients},release:async()=>{if(r)return;r=!0;let s=n;n=null,s!==null&&(s.attachments-=1,!(s.attachments>0)&&await s.backhaul.release().catch(()=>{}))}}}async detachAll(){let e=[];for(let r of this.byCaller.values())for(let o of r.values())e.push(o.backhaul.release());this.byCaller.clear(),this.byHandle.clear();let n=[];if(await Promise.all(e.map(async r=>{try{await r}catch(o){n.push(String(o instanceof Error?o.message:o))}})),n.length>0)throw new Error(`failed to detach ${n.length} conversation backhaul(s): ${n.join("; ")}`)}};_();var wn="/inference-usage",zi=25e3;var cd=new Set(["Error","TypeError","SyntaxError","TimeoutError"]);function Yi(t){if(!(t instanceof Error))return`a ${typeof t}`;let e=cd.has(t.name)?t.name:"Error",n=t.cause?.code;return typeof n=="string"&&/^[A-Z][A-Z0-9_]{0,31}$/.test(n)?`${e} (${n})`:e}var vn=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async lookup(e,n){let r=`${this.opts.apiUrl.replace(/\/+$/,"")}${wn}`,o;try{o=await this.fetchImpl(r,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify({inference_ids:n}),signal:AbortSignal.timeout(zi)})}catch(s){throw new Q(`the uRun usage surface (POST ${wn}) could not be reached within ${zi}ms: ${Yi(s)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new we("the uRun org API key was rejected by the uRun control plane \u2014 check the key and that it is not revoked");if(!o.ok){let s=await o.text().catch(()=>null),a=s===null?"body unreadable":`${s.length}-character body`;throw new Q(`the uRun usage surface (POST ${wn}) answered HTTP ${o.status} (${a})`)}let i;try{i=await o.json()}catch(s){throw new Q(`the uRun usage surface (POST ${wn}) answered HTTP ${o.status} with a body that is not JSON: ${Yi(s)}`)}return wo(i,this.opts.onRejectedRow)}};_();var pd="/inference-ledger",fd=1e4,bn=class extends Error{ledgerFailureReason;constructor(e,n="duplicate"){super(e),this.name="LedgerDuplicateError",this.ledgerFailureReason=n}},hr=class extends Error{constructor(n,r){super(r);this.ledgerAlreadyWritten=n;this.name="LedgerAlreadyWrittenError"}ledgerAlreadyWritten},ae=class extends Error{ledgerFailureReason="surface";constructor(e){super(e),this.name="LedgerSurfaceError"}},yr=class extends we{ledgerFailureReason="auth";constructor(e){super(e),this.name="LedgerAuthError"}},md=[["inference_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["model_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["variant",t=>t===null||typeof t=="string"],["task",t=>typeof t=="string"&&/[^\s]/.test(t)],["http_status",t=>typeof t=="number"&&Number.isInteger(t)],["started_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["completed_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["prompt_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["completion_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["output_units",Xi],["cost_nano_usd",Xi],["price_version",t=>t===null||typeof t=="string"]];function gd(t){for(let[e,n]of md){if(!(e in t))return`${e} (absent)`;if(!n(t[e]))return`${e} (${JSON.stringify(t[e])})`}return null}function hd(t){let e;try{e=JSON.parse(t)}catch{return{status:"unavailable"}}if(e===null||typeof e!="object"||Array.isArray(e))return{status:"unavailable"};let n=e.existing_status;if(n==="not_in_this_org")return{status:"not_in_this_org"};if(n!=="found")return{status:"unavailable"};let r=e.existing;return r===null||typeof r!="object"||Array.isArray(r)?{status:"unavailable"}:gd(r)!==null?{status:"unavailable"}:{status:"found",row:r}}function Xi(t){return t===null?!0:typeof t=="number"?Number.isFinite(t):typeof t!="string"?!1:/[^\s]/.test(t)&&Number.isFinite(Number(t))}function gr(t,e){return t===null?e===null:e===null?!1:Number(e)===t}function Qi(t,e){if(typeof e!="string")return!1;let n=Date.parse(t),r=Date.parse(e);return Number.isFinite(n)&&Number.isFinite(r)&&n===r}function yd(t,e){let n=e;return n.inference_id===t.inference_id&&n.model_id===t.model_id&&(n.variant??null)===t.variant&&n.task===t.task&&n.http_status===t.http_status&&Qi(t.started_at,n.started_at)&&Qi(t.completed_at,n.completed_at)&&gr(t.prompt_tokens,n.prompt_tokens)&&gr(t.completion_tokens,n.completion_tokens)&&gr(t.output_units,n.output_units)}function _d(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new ae(`${e} answered a "recorded" that is not an object: ${JSON.stringify(t)}`);let n=t,r=n.inference_id;if(typeof r!="string"||r==="")throw new ae(`${e} answered a row with no inference_id: ${JSON.stringify(t)}`);if(!("cost_nano_usd"in n))throw new ae(`${e} answered a row with no cost_nano_usd field: ${JSON.stringify(t)}`);let o=n.cost_nano_usd;if(o!=null&&!(typeof o=="number"&&Number.isInteger(o)&&o>=0))throw new ae(`${e} answered cost_nano_usd ${JSON.stringify(o)}, which is neither a non-negative integer nor null`);let i=d=>{if(!(d in n))throw new ae(`${e} answered a row with no ${d} field: ${JSON.stringify(t)}`);let l=n[d];if(l===null)return null;if(typeof l!="string")throw new ae(`${e} answered a non-string ${d}: ${JSON.stringify(l)}`);if(!/[^\s]/.test(l))throw new ae(`${e} answered a BLANK ${d} (${JSON.stringify(l)}) \u2014 it satisfies "is a string" while naming nothing, which is exactly what inference_requests_priced_attribution_check exists to forbid`);return l},s=i("price_version"),a=i("unpriced_reason"),u=o!=null;if(u&&(s===null||a!==null))throw new ae(`${e} answered a PRICED row (cost_nano_usd ${String(o)}) that is not cleanly attributed \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(a)}`);if(!u&&(s!==null||a===null))throw new ae(`${e} answered an UNPRICED row that does not say why \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(a)}. NULL means NOT YET PRICED, and the reason is how an operator learns that traffic is being served which nobody can bill.`);return{inference_id:r,cost_nano_usd:o===void 0?null:o,price_version:s,unpriced_reason:a}}var kn=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async write(e,n){let r=`${this.opts.apiUrl.replace(/\/+$/,"")}${pd}`,o;try{o=await this.fetchImpl(r,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify(n),signal:AbortSignal.timeout(fd)})}catch(u){throw new ae(`the uRun ledger surface at ${r} could not be reached: ${u instanceof Error?u.message:String(u)}`)}if(o.status===409){let u=await o.text().catch(()=>""),d=hd(u);throw d.status==="found"&&yd(n,d.row)?new hr(d.row,`the uRun control plane already holds THIS PROXY'S OWN row for inference_id ${n.inference_id} \u2014 every billable field matches the row being re-sent, so an earlier attempt landed and this retry is a no-op. Nothing was written and nothing was overwritten.`):d.status==="not_in_this_org"?new bn(`inference_id ${n.inference_id} is ALREADY TAKEN by a ledger row this org cannot read. The id space is a global primary key, so this is not a retry and cannot be this proxy's own write \u2014 either two orgs collided on a uuid, or a row was written for this id by somebody else.`,"duplicate_foreign"):new bn(`the uRun control plane already holds a ledger row for inference_id ${n.inference_id} that is NOT the one being written${d.status==="unavailable"?" (and it could not be read back to compare)":""} \u2014 nothing was written and nothing was overwritten. Either this proxy wrote twice, or a row was fabricated ahead of it (this write is authenticated by the CALLER'S OWN org key). ${u.slice(0,400)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new yr("the uRun org API key was rejected by the uRun control plane when writing the inference ledger \u2014 the request was served but produced no billable row");if(!o.ok){let u=await o.text().catch(d=>`<body unreadable: ${d instanceof Error?d.message:String(d)}>`);throw new ae(`the uRun ledger surface at ${r} answered HTTP ${o.status}: ${u.slice(0,400)}`)}let i;try{i=await o.json()}catch(u){throw new ae(`the uRun ledger surface at ${r} answered a body that is not JSON: ${u instanceof Error?u.message:String(u)}`)}let s=i?.recorded;if(s===void 0)throw new ae(`the uRun ledger surface at ${r} answered a body with no "recorded" field`);let a=_d(s,`the uRun ledger surface at ${r}`);if(a.inference_id!==n.inference_id)throw new ae(`the uRun ledger surface at ${r} answered for inference_id ${a.inference_id} when asked about ${n.inference_id}`);return a}};var wd=5e3,vd=3e3,bd="/api/client-tokens";function _r(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function Zi(t,e,n,r){_r(t,e,{error:{message:n,type:r}})}async function kd(t,e=fetch){let n=`${t.replace(/\/+$/,"")}${bd}`;try{let r=await e(n,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(vd)});return r.status===404?{ready:!1,detail:`${n} answered 404 \u2014 this is not a uRun control plane`}:r.status>=500?{ready:!1,detail:`${n} answered HTTP ${r.status}`}:{ready:!0,detail:`${n} answered HTTP ${r.status}`}}catch(r){return{ready:!1,detail:`${n} unreachable: ${r instanceof Error?r.message:String(r)}`}}}function ts(t){let e=t.resolver??new ln({gatewayUrl:t.baseUrl}),n=t.registry??new mn({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),r=t.probeControlPlane??(()=>kd(t.baseUrl)),o=t.usageClient??new vn({apiUrl:t.apiUrl}),i=t.ledgerClient??new kn({apiUrl:t.apiUrl}),s=new qt(t.defaultMetrics===void 0?{}:{defaultMetrics:t.defaultMetrics}),a=async w=>e.resolve(Et(w.headers.authorization)),u=w=>w instanceof we?{status:w.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:w instanceof it?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,d=Li({clients:async w=>n.clientsFor(await a(w)),usage:{authorize:w=>Et(w.headers.authorization),lookup:(w,h)=>o.lookup(w,h)},statusOf:u,...t.requestLog?{requestLog:t.requestLog}:{},observe:w=>s.observe(w),ledger:{write:(w,h)=>i.write(Et(w.headers.authorization),h),observe:w=>s.observeLedger(w),...t.ledgerLog?{report:t.ledgerLog}:{}}}),l=null,c=(0,es.createServer)((w,h)=>{let b=new URL(w.url??"/","http://localhost");(async()=>{if(w.method==="GET"&&b.pathname==="/healthz"){_r(h,200,{ok:!0});return}if(w.method==="GET"&&b.pathname==="/readyz"){let B=Date.now();if(!l||B-l.at>=wd){let M=await r();l={at:B,...M}}_r(h,l.ready?200:503,{ok:l.ready,control_plane:l.detail});return}let R=b.pathname==="/v1"||b.pathname.startsWith("/v1/"),T=b.pathname.startsWith("/partners/");if(!R&&!T){Zi(h,404,`no route for ${w.method} ${b.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface, the partner adapter surface (/partners/...), the WS upgrade paths (/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent and /v1/realtime), plus /healthz and /readyz`,"invalid_request_error");return}d(w,h)})().catch(R=>{if(!h.headersSent){Zi(h,500,String(R instanceof Error?R.message:R),"proxy_error");return}h.end()})}),y=t.conversations??new hn({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),g=[Gn(c,{authenticate:async w=>{let h=await e.resolve(w),b=y.connectionFor(h);return{clients:b.clients,scope:_n(h.apiKey),onBindResumeHandle:b.bindResumeHandle,clientsForResume:b.clientsForResume,release:b.release}}}),Zn(c,{clients:async w=>n.clientsFor(await a(w)),statusOf:u}).close,tn(c,{resolver:{resolve:async w=>{let h=await e.resolve(Et(w.headers.authorization)),b=y.connectionFor(h),R;try{R=await Di(b.clients,Ni(w))}catch(T){throw b.release().catch(()=>{}),T}return{...R,release:b.release}}}})];c.on("upgrade",(w,h)=>{let b=new URL(w.url??"/","http://localhost");b.pathname===Wt||b.pathname===Xn||b.pathname===kt||h.destroyed||(h.write(`HTTP/1.1 404 Not Found\r
|
|
61
|
+
`)),l.end()})}}var Un=(t,e)=>Object.assign(new Error(e),{status:t});function Ci(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}async function $i(t,e,n){if(!t.supportsAudio)throw Un(501,"the realtime lane needs a router-backed client (supportsAudio) to gate audio modality \u2014 hand-built clients without the catalog oracle are refused, never allowed silently");let r;try{r=await t.supportsAudio(e)}catch(a){throw a instanceof z?Un(404,a.message):a}if(!r)throw Un(404,`model ${JSON.stringify(e??"(default)")} is not audio-capable (catalog task is neither stt nor tts) \u2014 this realtime lane serves only audio-task models; chat models ride /v1/chat/completions`);if(!t.openAudioOn)throw Un(501,"an audio-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the connection\u2019s pooled session \u2014 refusing loudly rather than binding a voiceless session");let o=await t.sessionHandle(e),s={audio:await t.openAudioOn(o,n)},i={clients:{createResponse:async a=>await t.createResponseOn(o,a)},voice:r,audioSession:s};return e!==void 0&&(i.model=e),i}k();var Dn=require("@urun-sh/core"),Oe=class extends Error{constructor(n,r=401){super(n);this.status=r;this.name="ProxyAuthError"}status},_t=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function Gt(t){let e=(t??"").trim();if(e==="")throw new Oe("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let n=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!n)throw new Oe("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return n[1].trim()}var Ll=60,Ul=6e4,Nl=4096;function Dl(t){return t===401||t===403}var Nn=class{constructor(e){this.opts=e;this.mint=e.mint??Dn.createClientToken,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new Oe("empty Authorization bearer token");let n=this.cache.get(e);if(n&&this.now()-n.at<Ul)return{apiKey:e,orgId:n.orgId};let r;try{r=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:Ll})).orgId}catch(o){throw o instanceof Dn.ClientTokenError&&Dl(o.status)?(this.cache.delete(e),new Oe("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new _t(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${o instanceof Error?o.message:String(o)}`)}if(!r)throw new _t("the control plane verified the API key but returned no org_id \u2014 refusing to route a request with no tenancy (control-plane contract violation)");if(this.cache.size>=Nl){let o=this.cache.keys().next();o.done||this.cache.delete(o.value)}return this.cache.set(e,{orgId:r,at:this.now()}),{apiKey:e,orgId:r}}clear(){this.cache.clear()}};k();var Vn=require("crypto");k();var Vt=require("@urun-sh/core");k();k();k();k();function Mi(t,e,n,r){let o=t.response??{},s={request_id:n,consumer_id:r,stream:!0,kind:"chat",messages:e};return typeof o.instructions=="string"&&(s.instructions=o.instructions),Array.isArray(o.modalities)&&(s.modalities=o.modalities),typeof o.temperature=="number"&&(s.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(s.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(s.tools=o.tools),s}function Li(t,e,n){let r={request_id:e,consumer_id:n,stream:!!t.stream,kind:"responses",input:t.input};return t.model&&(r.model=t.model),t.tools!==void 0&&(r.tools=t.tools),t.tool_choice!==void 0&&(r.tool_choice=t.tool_choice),typeof t.temperature=="number"&&(r.temperature=t.temperature),typeof t.max_output_tokens=="number"&&(r.max_output_tokens=t.max_output_tokens),typeof t.top_p=="number"&&(r.top_p=t.top_p),Array.isArray(t.stop)&&t.stop.length>0&&(r.stop=t.stop),typeof t.reasoning_effort=="string"&&(r.reasoning_effort=t.reasoning_effort),typeof t.instructions=="string"&&(r.instructions=t.instructions),t.chat_template_kwargs!==void 0&&(r.chat_template_kwargs=t.chat_template_kwargs),t.response_format!==void 0&&(r.response_format=t.response_format),typeof t.voice=="string"&&(r.voice=t.voice),typeof t.language=="string"&&(r.language=t.language),r}k();k();function Ui(t){if(t instanceof Error)return{type:"error",error:{type:"urun_error",code:t.name||null,message:t.message}};if(t&&typeof t=="object"&&t.t==="error"){let e=t,n=e.body??{},r=n.error,o=r?.message??n.message??`unreadable serve error body: ${JSON.stringify(e.body??null)}`;return{type:"error",error:{type:r?.type??"urun_error",code:e.code??r?.code??null,message:o}}}return{type:"error",error:{type:"urun_error",code:null,message:String(t)}}}var Fl="llm-resp";async function*qr(t,e){let n=`${Fl}:${e}`,r=`resp_${e}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};let o=new Map;try{for await(let s of t.stream(n).messages()){let i=s;if(i.t==="delta"){if(typeof i.delta=="string"&&(yield{type:"response.output_text.delta",item_id:r,delta:i.delta}),typeof i.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:r,delta:i.reasoning}),Array.isArray(i.tool_calls))for(let a of i.tool_calls){let u=typeof a.index=="number"?a.index:0,d=o.get(u)??{};a.id&&(d.id=a.id),a.function?.name&&(d.name=a.function.name),o.set(u,d),yield{type:"response.function_call_arguments.delta",item_id:`fc_${e}_${u}`,tool_index:u,call_id:d.id,name:d.name,delta:a.function?.arguments??""}}}else if(i.t==="response"){yield{type:"response.completed",response:{id:r,status:"completed",...i.body}};return}else if(i.t==="error"){yield Ui(i);return}}}finally{try{t.doc("llm").set({requests:{[e]:null}})}catch{}}}var Bn=class t{constructor(e,n=t.DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS){this.session=e;this.docSyncTimeoutMs=n;this.sessionTag=e.sessionId??globalThis.crypto.randomUUID()}session;docSyncTimeoutMs;sessionTag;static DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS=15e3;get consumerId(){return this.session.consumerId}async ensureDocSynced(){let e=this.session.doc("llm");if(e.synced===!0||typeof e.onSynced!="function")return;let n;await new Promise((o,s)=>{let i=e.onSynced(()=>{clearTimeout(n),o()});n=setTimeout(()=>{i(),s(new Error(`[urun] llm doc plane never synced within ${this.docSyncTimeoutMs}ms \u2014 the request envelope write is withheld rather than raced into a possibly-lost pre-sync write (resumed-attach first-write-lost, prod-usw2 2026-09-23)`))},this.docSyncTimeoutMs)})}write(e){e.session_tag=this.sessionTag,this.session.doc("llm").set({requests:{[e.request_id]:{payload:e,consumer_id:e.consumer_id,stream:e.stream}}})}sendResponses(e,n){return this.writeGated(n,()=>{let r=Li(e,n,this.consumerId);return this.write(r),qr(this.session,n)})}sendResponseCreate(e,n,r){return this.writeGated(r,()=>{let o=Mi(e,n,r,this.consumerId);return this.write(o),qr(this.session,r)})}async writeGated(e,n){try{await this.ensureDocSynced()}catch(r){throw new Error(`[urun] responses dial ${e} aborted before its envelope write: ${r instanceof Error?r.message:String(r)}`)}return n()}};var Ni=0;function Bl(){return Ni+=1,`req_${Date.now().toString(36)}_${Ni.toString(36)}`}var jn=class{transport;constructor(e){this.transport=new Bn(e)}responses={create:async e=>{let n=Bl(),r=await this.transport.sendResponses(e,n);return Object.assign((async function*(){yield*r})(),{requestId:n})}}};k();async function Di(t){let n=await(t.fetchImpl??fetch)(`${t.catalogUrl}/model_catalog?select=model_id,variant,gpu_spec,engine,task,description,size_gb,hf_repo,engine_args,updated_at`,{headers:{apikey:t.anonKey,Authorization:`Bearer ${t.anonKey}`}});if(!n.ok)throw new Error(`model_catalog fetch failed: ${n.status}`);return await n.json()}k();var Fi=require("@urun-sh/core"),Hr=2;function jl(t){let e=t?.type;return e==="response.created"||e==="response.in_progress"}function ql(t){let e=t?.type;return e==="response.completed"||e==="error"}function Bi(t){return async function(n,r){return(async function*(){for(let s=1;s<=Hr;s++){let{app:i,entry:a,identity:u}=await t.sessionFor(n.model);r?.({sessionId:t.sessionIdOf?.(a)??null,identity:u});let d=!1,l=!1;try{let p=await t.createStream(a,n);for await(let y of p){if(ql(y)&&(d=!0),jl(y)){if(s>1)continue}else l=!0;yield y}}catch(p){if(p instanceof Fi.SessionFailedError&&(p.kind==="never-live"||p.kind==="create-failed")){try{t.onAdmissionVerdict?.(i,a,p)}catch{}throw p}if(s<Hr&&!l){await t.evict(i,a);continue}throw p}if(d)return;if(s<Hr&&!l){await t.evict(i,a);continue}return}})()}}k();var Hl=3,Gl="llm",Vl=new Set(["paused","error"]);function ji(t,e){let n=t.doc(Gl),r=typeof n.onConnectionState=="function",o=typeof t.onPhase=="function";if(!r&&!o)throw new Error(`session for app "${e}" exposes neither a doc connection-state surface nor onPhase \u2014 session-gone detection requires @urun-sh/core Session (SessionDocument.onConnectionState / Session.onPhase); pooling a session with no native lifecycle signal would silently swallow requests dispatched after the platform closes it`);let s=null,i=!1,a=new Set,u=[],d=l=>{if(!s){s=l;for(let p of[...a])p(l);a.clear()}};return r&&u.push(n.onConnectionState(l=>{i&&(l.consecutiveFailures<Hl||d(new pe(`the pooled session for app "${e}" is gone: its doc backhaul failed ${l.consecutiveFailures} consecutive times (last close ${l.lastCloseCode===null?"by provider liveness reap":`code ${l.lastCloseCode}`}${l.lastCloseReason?`, reason ${JSON.stringify(l.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`)))})),o&&u.push(t.onPhase(l=>{if(l.name==="live"){i=!0;return}i&&Vl.has(l.name)&&d(new pe(`the pooled session for app "${e}" lost its compute binding (phase "${l.name}"). The entry is evicted; retry to get a fresh session. The named session itself is not over \u2014 it stays attachable by name.`))})),{gone:()=>s,onGone(l){return s?(l(s),()=>{}):(a.add(l),()=>a.delete(l))},dispose(){a.clear();for(let l of u.splice(0))l()}}}async function*Gr(t,e){let n=e.gone();if(n)throw n;let r=t[Symbol.asyncIterator](),o,s=new Promise((i,a)=>{o=e.onGone(u=>a(u))});s.catch(()=>{});try{for(;;){let i=await Promise.race([r.next(),s]);if(i.done)return;yield i.value}}finally{o?.(),r.return?.(void 0)}}function qi(t,e,n,r){let o=t[n];if(typeof o!="function")throw new Error(`app "${e}" has no function "${n}" (set URUN_FUNCTION)`);let s=r?o(void 0,r):o();if(typeof s.end!="function")throw new Error(`app "${e}" function "${n}" returned a session without end()`);return s}function Wl(t,e,n){let{baseUrl:r,orgId:o,auth:s}=t,i=typeof t.subject=="function"?t.subject():t.subject,a=n.startsWith(Xe)&&i!==void 0?`${i}:speech`:i,u=e.fnName??t.fnName,d=t.sessionStartOptions?.(n),l=e.sharedApp?{sharedApp:!0,...d}:d,p=w=>({session:w,responses:new jn(w),gone:ji(w,n)});if(s.lane==="jwt"){let w=(0,Vt.App)(e.appSlug,{baseUrl:r,orgId:o,jwt:s.jwt});return p(qi(w,e.appSlug,u,l))}let{apiKey:y,gatewayUrl:c}=s,m=(0,Vt.App)(e.appSlug,{baseUrl:r,orgId:o,getAccessToken:async()=>(await(0,Vt.createClientToken)(y,{baseUrl:c,expiresIn:300,allowedFunctions:[`${e.appSlug}/${u}`],...a===void 0?{}:{subject:a},...e.sharedApp?{sharedApp:!0}:{}})).token});return p(qi(m,e.appSlug,u,l))}function Jl(t){let e=t.session.id;if(typeof e!="string"||e.length===0)throw new Error("pooled session exposes no stable session id \u2014 the session-identity seam requires @urun-sh/core Session (session.id)");return e}function Kl(t){if(t.kind==="never-live")return!0;if(t.kind!=="create-failed")return!1;let e=t.status.error?.httpStatus,n=t.status.error?.code;return e===410||e===409&&(n==="cancelled"||n==="expired")?!0:n==="client_ended"}var zl=new Set(["error","paused"]);function Jr(t){let{auth:e,catalog:n}=t,r=null,o=(i,a)=>{let u=Wl(t,i,a);return t.onSessionOpen&&typeof u.session.id=="string"&&u.session.id!==""&&t.onSessionOpen(a,u.session.id),u.gone.onGone(()=>{r?.evict(a,u)}),u},s=new Yt({sharedOnlyListing:t.sharedOnlyListing===!0,defaultApp:t.defaultApp,fnName:t.fnName,openSession:o,closeSession:async i=>{if(i.gone.dispose(),t.release==="detach"){if(typeof i.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');i.session.detach();return}await i.session.end()},listApps:e.lane==="api-key"?()=>To({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,listCatalog:n?()=>Di(n):null,listPrices:n?()=>ho(n):null,shared:e.lane==="api-key"?()=>co({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,sessionKey:Jl});return r=s,t.defaultApp!==null&&t.statelessDirect!==!0&&s.seed(t.defaultApp,o({appSlug:t.defaultApp},Rt({appSlug:t.defaultApp}))),s}async function Vr(t,e){let n=t.whenLive;if(typeof n!="function")throw new Error("pooled session exposes no native whenLive() \u2014 the audio lane must wait for the session's own admission (cold wake: queued \u2192 demand \u2192 promote) before wiring media; a session without the core Session.whenLive surface cannot be served audio");await n.call(t,e===void 0?void 0:{signal:e})}async function Hi(t){if(typeof t.cancel=="function")try{await t.cancel();return}catch{}if(typeof t.detach!="function")throw new Error("pooled session exposes neither cancel() nor detach() \u2014 the client-gone abandon path requires the @urun-sh/core Session native lifecycle surface");t.detach()}function Wr(t){return t.reason instanceof Error?t.reason:new Error("the client socket closed")}function Kr(t,e=gs,n,r){let o=new WeakMap,s=new WeakMap,i=new WeakMap,a=new WeakMap,u=new WeakMap,d=c=>{let m=c.session.id;return typeof m=="string"&&m.length>0?m:null},l=c=>{let m=o.get(c);if(!m){let w={waiting:0,gone:new AbortController};u.set(c,w),m=(async()=>{if(await Vr(c.session,w.gone.signal),w.gone.signal.aborted)throw Wr(w.gone.signal);u.get(c)===w&&u.delete(c);let R=await fs(c.session,await e());return Object.assign(R,{sessionId:c.session.id})})(),o.set(c,m),m.catch(()=>{o.get(c)===m&&o.delete(c)}),m.then(()=>{u.get(c)===w&&u.delete(c)},()=>{u.get(c)===w&&u.delete(c)})}return m},p=c=>{let m=a.get(c);return m||(m=(async()=>{let w=await l(c);return{appendInputAudio:R=>w.appendInputAudio(R),onOutputAudio:R=>w.onOutputAudio(R),sessionId:d(c)}})(),a.set(c,m),m.catch(()=>{a.get(c)===m&&a.delete(c)})),m},y=async(c,m,w)=>{if(w?.aborted){let v=u.get(c);throw(o.get(c)===void 0||v!==void 0&&v.waiting===0)&&await Hi(c.session),Wr(w)}let R=m(c),h=u.get(c);return h!==void 0&&h.waiting++,w===void 0?R:new Promise((v,b)=>{let $=()=>{h!==void 0&&--h.waiting===0&&u.get(c)===h&&(h.gone.abort(),u.delete(c),o.delete(c),a.delete(c),Hi(c.session).catch(()=>{})),b(Wr(w))};w.addEventListener("abort",$,{once:!0});let S=()=>w.removeEventListener("abort",$);R.then(E=>{S(),v(E)},E=>{S(),b(E)})})};return{createResponse:Bi({sessionFor:c=>t.sessionFor(c),sessionIdOf:c=>d(c),evict:(c,m)=>t.evict(c,m),onAdmissionVerdict:(c,m,w)=>{Kl(w)&&(t.evict(c,m),n?.(c,w.kind==="never-live"?"never-live":"create-failed"))},createStream:async(c,m)=>(await Vr(c.session),Gr(await c.responses.responses.create(m),c.gone))}),listModels:()=>t.modelList(),openRouterModels:()=>t.openRouterModels(),supportsImages:(c,m)=>t.supportsImages(c,m),listImageModels:()=>t.imageModelList(),catalogIdentity:c=>t.catalogIdentityFor(c),servingAppSlug:async c=>await t.servingAppSlugFor(c),supportsAudio:c=>t.supportsAudio(c),supportsSpeech:c=>t.supportsSpeech(c),speechVoices:c=>t.speechVoices(c),supportsTranscription:c=>t.supportsTranscription(c),listSpeechToTextModels:()=>t.speechToTextModelList(),openAudio:async(c,m)=>{let{entry:w}=await t.sessionFor(c);return await y(w,l,m)},openAudioOn:async(c,m)=>{let{entry:w}=await t.sessionForHandle(c);return await y(w,p,m)},openVideo:async c=>{let{entry:m}=await t.sessionFor(c),w=s.get(m);return w||(w=ms(m.session),s.set(m,w)),w},openVideoOut:async c=>{let{entry:m}=await t.sessionFor(c),w=i.get(m);return w||(w=ys(m.session),i.set(m,w)),w},sessionHandle:async c=>(await t.handleFor(c)).handle,speechSessionHandle:async c=>(await t.speechHandleFor(c)).handle,evictSpeechSession:async c=>{let{app:m,entry:w}=await t.sessionForHandle(c);await t.evict(m,w)},createResponseOn:async(c,m)=>{let{entry:w}=await t.sessionForHandle(c);return await Vr(w.session),Gr(await w.responses.responses.create(m),w.gone)},onSessionEnd:async(c,m)=>{let{entry:w}=await t.sessionForHandle(c),R=w.session;if(typeof R.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let h=!1;return R.onPhase(v=>{if(h||!zl.has(v.name))return;h=!0;let b=R.endsAt??null;m({timeLeftMs:b?Math.max(0,b.getTime()-Date.now()):null,reason:v.reason??`session ${v.name}`})})},...r?{stateless:r}:{}}}var Yl=512;function Wn(t){return`urun-inference-proxy-${(0,Vn.createHash)("sha256").update(t).digest("hex").slice(0,32)}`}var qn=class{constructor(e){this.opts=e}opts;tenants=new Map;label(e,n){return Object.assign(Object.create(e),{tenant:n.orgId})}build(e){if(this.opts.build){let a=this.opts.build(e);return{router:a.router,clients:this.label(a.clients,e)}}let n=0,r=Wn(e.apiKey),o=()=>{n+=1},s=Jr({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:an,sharedOnlyListing:!0,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:()=>n===0?r:`${r}#${n}`,onPoolWedged:o,release:"end"}),i=this.opts.decisionIntake?new Tn({targetFor:a=>s.targetFor(a),apiUrl:this.opts.apiUrl,apiKey:e.apiKey,orgId:e.orgId,fnName:an,secret:this.opts.decisionIntake.secret,...this.opts.decisionIntake.shared?{shared:this.opts.decisionIntake.shared}:{}}):void 0;return{router:s,clients:this.label(Kr(s,void 0,o,i),e)}}clientsFor(e){let n=this.tenants.get(e.apiKey);if(n)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,n),n.clients;for(;this.tenants.size>=Yl;){let o=this.tenants.entries().next();if(o.done)break;let[s,i]=o.value;this.tenants.delete(s),i.router.closeAll().catch(()=>{})}let r=this.build(e);return this.tenants.set(e.apiKey,r),r.clients}get size(){return this.tenants.size}async closeAll(){let e=[...this.tenants.values()].map(r=>r.router);this.tenants.clear();let n=[];if(await Promise.all(e.map(async r=>{try{await r.closeAll()}catch(o){n.push(String(o instanceof Error?o.message:o))}})),n.length>0)throw new Error(`failed to close ${n.length} tenant backhaul(s): ${n.join("; ")}`)}},Gi=512,Xl=1024,Hn=class extends Error{constructor(){super(`conversation capacity reached (${Gi} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function Ql(t){return{sessionStartOptions:e=>{if(e.startsWith(Xe))return;let n=t.get(e);return n===void 0?void 0:{sessionId:n}},onSessionOpen:(e,n)=>{e.startsWith(Xe)||t.set(e,n)}}}function Zl(t,e,n,r){let o=Jr({baseUrl:t.baseUrl,orgId:e.orgId,defaultApp:null,fnName:an,sharedOnlyListing:!0,apiUrl:t.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:t.baseUrl},catalog:t.catalog,subject:n.subject,release:"detach",...Ql(r)});return{clients:Kr(o),release:async()=>{await o.closeAll()}}}var Gn=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,n){let r={id:n,subject:`${Wn(e.apiKey)}:live:${n}`},o=new Map,s=this.opts.build?this.opts.build(e,r):Zl(this.opts,e,r,o);return{id:n,backhaul:s,attachments:0,nativeRefs:o}}attachEntry(e,n){let r=this.byCaller.get(e.apiKey);r===void 0&&(r=new Map,this.byCaller.set(e.apiKey,r));let o=r.get(n);if(o!==void 0)return r.delete(n),r.set(n,o),o.attachments+=1,o;this.evictOverflow();let s=this.build(e,n);return s.attachments=1,r.set(n,s),s}get conversationCount(){let e=0;for(let n of this.byCaller.values())e+=n.size;return e}evictOverflow(){for(;this.conversationCount>=Gi;){let e=this.byCaller.keys().next();if(e.done)throw new Hn;let n=e.value,r=this.byCaller.get(n),o=!1;for(let[s,i]of r)if(!(i.attachments>0)){r.delete(s),this.dropHandleIndexFor(n,s),i.backhaul.release().catch(()=>{}),o=!0;break}if(!o)throw r.size===0&&this.byCaller.delete(n),new Hn;r.size===0&&this.byCaller.delete(n)}}dropHandleIndexFor(e,n){for(let[r,o]of this.byHandle)o.apiKey===e&&o.conversationId===n&&this.byHandle.delete(r)}connectionFor(e){let n=null,r=!1,o=()=>(n===null&&(n=this.attachEntry(e,(0,Vn.randomUUID)())),n),s={createResponse:(i,a)=>o().backhaul.clients.createResponse(i,a),catalogIdentity:i=>o().backhaul.clients.catalogIdentity(i),servingAppSlug:i=>{let a=o().backhaul.clients.servingAppSlug;if(a===void 0)throw new Error("the conversation backhaul wires no servingAppSlug seam \u2014 the caller-org app a request resolves to cannot be known");return a(i)},listModels:()=>o().backhaul.clients.listModels(),sessionHandle:i=>o().backhaul.clients.sessionHandle(i),createResponseOn:(i,a)=>o().backhaul.clients.createResponseOn(i,a),onSessionEnd:(i,a)=>o().backhaul.clients.onSessionEnd(i,a)};return s.openAudio=async(i,a)=>{let u=o().backhaul.clients.openAudio;if(u===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return u(i,a)},s.openVideo=async i=>{let a=o().backhaul.clients.openVideo;if(a===void 0)throw new Error("the conversation backhaul wires no openVideo seam \u2014 video-in is unavailable on this deployment");return a(i)},s.openVideoOut=async i=>{let a=o().backhaul.clients.openVideoOut;if(a===void 0)throw new Error("the conversation backhaul wires no openVideoOut seam \u2014 video-out is unavailable on this deployment");return a(i)},s.supportsAudio=async i=>{let a=o().backhaul.clients.supportsAudio;if(a===void 0)throw new Error("the conversation backhaul wires no supportsAudio seam \u2014 the realtime lane cannot gate audio modality on this deployment");return a(i)},s.supportsSpeech=async i=>{let a=o().backhaul.clients.supportsSpeech;if(a===void 0)throw new Error("the conversation backhaul wires no supportsSpeech seam \u2014 the speech lane cannot gate model capability on this deployment");return a(i)},s.speechVoices=async i=>{let a=o().backhaul.clients.speechVoices;if(a===void 0)throw new Error("the conversation backhaul wires no speechVoices seam \u2014 the speech lane cannot resolve the voice set /v1/models publishes on this deployment");return a(i)},s.speechSessionHandle=async i=>{let a=o().backhaul.clients.speechSessionHandle;if(a===void 0)throw new Error("the conversation backhaul wires no speechSessionHandle seam \u2014 the speech lane cannot resolve its own pooled session on this deployment");return a(i)},s.openAudioOn=async(i,a)=>{let u=o().backhaul.clients.openAudioOn;if(u===void 0)throw new Error("the conversation backhaul wires no openAudioOn seam \u2014 the speech and realtime lanes cannot open their pinned native session audio on this deployment");return u(i,a)},s.evictSpeechSession=async i=>{let a=o().backhaul.clients.evictSpeechSession;if(a===void 0)throw new Error("the conversation backhaul wires no evictSpeechSession seam \u2014 the speech lane cannot retire its pinned speech session after a failed turn on this deployment");return a(i)},s.supportsTranscription=async i=>{let a=o().backhaul.clients.supportsTranscription;if(a===void 0)throw new Error("the conversation backhaul wires no supportsTranscription seam \u2014 the transcription lane cannot gate model capability on this deployment");return a(i)},s.listSpeechToTextModels=async()=>{let i=o().backhaul.clients.listSpeechToTextModels;if(i===void 0)throw new Error("the conversation backhaul wires no listSpeechToTextModels seam \u2014 the transcription lane cannot list the stt models on this deployment");return i()},{clients:s,bindResumeHandle:i=>{let a=n;if(a===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=Xl;){let u=this.byHandle.keys().next();if(u.done)break;this.byHandle.delete(u.value)}this.byHandle.set(i,{apiKey:e.apiKey,conversationId:a.id})},clientsForResume:async i=>{let a=this.byHandle.get(i),u=a===void 0?void 0:this.byCaller.get(a.apiKey),d=a===void 0||u===void 0?void 0:u.get(a.conversationId);if(a===void 0||d===void 0||a.apiKey!==e.apiKey)throw new pe("the conversation behind this resume handle is gone (evicted at capacity, or the replica restarted) or the handle is not yours \u2014 reconnect without a handle to start a new conversation");return n=d,d.attachments+=1,d.backhaul.clients},release:async()=>{if(r)return;r=!0;let i=n;n=null,i!==null&&(i.attachments-=1,!(i.attachments>0)&&await i.backhaul.release().catch(()=>{}))}}}async detachAll(){let e=[];for(let r of this.byCaller.values())for(let o of r.values())e.push(o.backhaul.release());this.byCaller.clear(),this.byHandle.clear();let n=[];if(await Promise.all(e.map(async r=>{try{await r}catch(o){n.push(String(o instanceof Error?o.message:o))}})),n.length>0)throw new Error(`failed to detach ${n.length} conversation backhaul(s): ${n.join("; ")}`)}};k();var Jn="/inference-usage",Vi=25e3;var ec=new Set(["Error","TypeError","SyntaxError","TimeoutError"]);function Wi(t){if(!(t instanceof Error))return`a ${typeof t}`;let e=ec.has(t.name)?t.name:"Error",n=t.cause?.code;return typeof n=="string"&&/^[A-Z][A-Z0-9_]{0,31}$/.test(n)?`${e} (${n})`:e}var Kn=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async lookup(e,n){let r=`${this.opts.apiUrl.replace(/\/+$/,"")}${Jn}`,o;try{o=await this.fetchImpl(r,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify({inference_ids:n}),signal:AbortSignal.timeout(Vi)})}catch(i){throw new te(`the uRun usage surface (POST ${Jn}) could not be reached within ${Vi}ms: ${Wi(i)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new Oe("the uRun org API key was rejected by the uRun control plane \u2014 check the key and that it is not revoked");if(!o.ok){let i=await o.text().catch(()=>null),a=i===null?"body unreadable":`${i.length}-character body`;throw new te(`the uRun usage surface (POST ${Jn}) answered HTTP ${o.status} (${a})`)}let s;try{s=await o.json()}catch(i){throw new te(`the uRun usage surface (POST ${Jn}) answered HTTP ${o.status} with a body that is not JSON: ${Wi(i)}`)}return ns(s,this.opts.onRejectedRow)}};k();var tc="/inference-ledger",nc=1e4,zn=class extends Error{ledgerFailureReason;constructor(e,n="duplicate"){super(e),this.name="LedgerDuplicateError",this.ledgerFailureReason=n}},Yr=class extends Error{constructor(n,r){super(r);this.ledgerAlreadyWritten=n;this.name="LedgerAlreadyWrittenError"}ledgerAlreadyWritten},we=class extends Error{ledgerFailureReason="surface";constructor(e){super(e),this.name="LedgerSurfaceError"}},Xr=class extends Oe{ledgerFailureReason="auth";constructor(e){super(e),this.name="LedgerAuthError"}},rc=[["inference_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["model_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["variant",t=>t===null||typeof t=="string"],["task",t=>typeof t=="string"&&/[^\s]/.test(t)],["http_status",t=>typeof t=="number"&&Number.isInteger(t)],["started_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["completed_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["prompt_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["completion_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["output_units",Ji],["cost_nano_usd",Ji],["price_version",t=>t===null||typeof t=="string"]];function oc(t){for(let[e,n]of rc){if(!(e in t))return`${e} (absent)`;if(!n(t[e]))return`${e} (${JSON.stringify(t[e])})`}return null}function sc(t){let e;try{e=JSON.parse(t)}catch{return{status:"unavailable"}}if(e===null||typeof e!="object"||Array.isArray(e))return{status:"unavailable"};let n=e.existing_status;if(n==="not_in_this_org")return{status:"not_in_this_org"};if(n!=="found")return{status:"unavailable"};let r=e.existing;return r===null||typeof r!="object"||Array.isArray(r)?{status:"unavailable"}:oc(r)!==null?{status:"unavailable"}:{status:"found",row:r}}function Ji(t){return t===null?!0:typeof t=="number"?Number.isFinite(t):typeof t!="string"?!1:/[^\s]/.test(t)&&Number.isFinite(Number(t))}function zr(t,e){return t===null?e===null:e===null?!1:Number(e)===t}function Ki(t,e){if(typeof e!="string")return!1;let n=Date.parse(t),r=Date.parse(e);return Number.isFinite(n)&&Number.isFinite(r)&&n===r}function ic(t,e){let n=e;return n.inference_id===t.inference_id&&n.model_id===t.model_id&&(n.variant??null)===t.variant&&n.task===t.task&&n.http_status===t.http_status&&Ki(t.started_at,n.started_at)&&Ki(t.completed_at,n.completed_at)&&zr(t.prompt_tokens,n.prompt_tokens)&&zr(t.completion_tokens,n.completion_tokens)&&zr(t.output_units,n.output_units)}function ac(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new we(`${e} answered a "recorded" that is not an object: ${JSON.stringify(t)}`);let n=t,r=n.inference_id;if(typeof r!="string"||r==="")throw new we(`${e} answered a row with no inference_id: ${JSON.stringify(t)}`);if(!("cost_nano_usd"in n))throw new we(`${e} answered a row with no cost_nano_usd field: ${JSON.stringify(t)}`);let o=n.cost_nano_usd;if(o!=null&&!(typeof o=="number"&&Number.isInteger(o)&&o>=0))throw new we(`${e} answered cost_nano_usd ${JSON.stringify(o)}, which is neither a non-negative integer nor null`);let s=d=>{if(!(d in n))throw new we(`${e} answered a row with no ${d} field: ${JSON.stringify(t)}`);let l=n[d];if(l===null)return null;if(typeof l!="string")throw new we(`${e} answered a non-string ${d}: ${JSON.stringify(l)}`);if(!/[^\s]/.test(l))throw new we(`${e} answered a BLANK ${d} (${JSON.stringify(l)}) \u2014 it satisfies "is a string" while naming nothing, which is exactly what inference_requests_priced_attribution_check exists to forbid`);return l},i=s("price_version"),a=s("unpriced_reason"),u=o!=null;if(u&&(i===null||a!==null))throw new we(`${e} answered a PRICED row (cost_nano_usd ${String(o)}) that is not cleanly attributed \u2014 price_version ${JSON.stringify(i)}, unpriced_reason ${JSON.stringify(a)}`);if(!u&&(i!==null||a===null))throw new we(`${e} answered an UNPRICED row that does not say why \u2014 price_version ${JSON.stringify(i)}, unpriced_reason ${JSON.stringify(a)}. NULL means NOT YET PRICED, and the reason is how an operator learns that traffic is being served which nobody can bill.`);return{inference_id:r,cost_nano_usd:o===void 0?null:o,price_version:i,unpriced_reason:a}}var Yn=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async write(e,n){let r=`${this.opts.apiUrl.replace(/\/+$/,"")}${tc}`,o;try{o=await this.fetchImpl(r,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify(n),signal:AbortSignal.timeout(nc)})}catch(u){throw new we(`the uRun ledger surface at ${r} could not be reached: ${u instanceof Error?u.message:String(u)}`)}if(o.status===409){let u=await o.text().catch(()=>""),d=sc(u);throw d.status==="found"&&ic(n,d.row)?new Yr(d.row,`the uRun control plane already holds THIS PROXY'S OWN row for inference_id ${n.inference_id} \u2014 every billable field matches the row being re-sent, so an earlier attempt landed and this retry is a no-op. Nothing was written and nothing was overwritten.`):d.status==="not_in_this_org"?new zn(`inference_id ${n.inference_id} is ALREADY TAKEN by a ledger row this org cannot read. The id space is a global primary key, so this is not a retry and cannot be this proxy's own write \u2014 either two orgs collided on a uuid, or a row was written for this id by somebody else.`,"duplicate_foreign"):new zn(`the uRun control plane already holds a ledger row for inference_id ${n.inference_id} that is NOT the one being written${d.status==="unavailable"?" (and it could not be read back to compare)":""} \u2014 nothing was written and nothing was overwritten. Either this proxy wrote twice, or a row was fabricated ahead of it (this write is authenticated by the CALLER'S OWN org key). ${u.slice(0,400)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new Xr("the uRun org API key was rejected by the uRun control plane when writing the inference ledger \u2014 the request was served but produced no billable row");if(!o.ok){let u=await o.text().catch(d=>`<body unreadable: ${d instanceof Error?d.message:String(d)}>`);throw new we(`the uRun ledger surface at ${r} answered HTTP ${o.status}: ${u.slice(0,400)}`)}let s;try{s=await o.json()}catch(u){throw new we(`the uRun ledger surface at ${r} answered a body that is not JSON: ${u instanceof Error?u.message:String(u)}`)}let i=s?.recorded;if(i===void 0)throw new we(`the uRun ledger surface at ${r} answered a body with no "recorded" field`);let a=ac(i,`the uRun ledger surface at ${r}`);if(a.inference_id!==n.inference_id)throw new we(`the uRun ledger surface at ${r} answered for inference_id ${a.inference_id} when asked about ${n.inference_id}`);return a}};var uc=5e3,dc=3e3,lc="/api/client-tokens";function Qr(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function zi(t,e,n,r){Qr(t,e,{error:{message:n,type:r}})}async function cc(t,e=fetch){let n=`${t.replace(/\/+$/,"")}${lc}`;try{let r=await e(n,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(dc)});return r.status===404?{ready:!1,detail:`${n} answered 404 \u2014 this is not a uRun control plane`}:r.status>=500?{ready:!1,detail:`${n} answered HTTP ${r.status}`}:{ready:!0,detail:`${n} answered HTTP ${r.status}`}}catch(r){return{ready:!1,detail:`${n} unreachable: ${r instanceof Error?r.message:String(r)}`}}}function Xi(t){let e=t.resolver??new Nn({gatewayUrl:t.baseUrl}),n=t.decisionSecret??"",r=t.sharedOrgKey&&t.sharedOrgId?{apiKey:t.sharedOrgKey}:void 0,o=t.registry??new qn({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog,...n!==""?{decisionIntake:{secret:n,...r?{shared:r}:{}}}:{}});n!==""&&process.stderr.write(`inference-proxy: stateless direct path ENABLED \u2014 caller-org decisions dial ready runtimes directly (no session mint)
|
|
62
|
+
`);let s=t.probeControlPlane??(()=>cc(t.baseUrl)),i=t.usageClient??new Kn({apiUrl:t.apiUrl}),a=t.ledgerClient??new Yn({apiUrl:t.apiUrl}),u=new sn(t.defaultMetrics===void 0?{}:{defaultMetrics:t.defaultMetrics}),d=async h=>e.resolve(Gt(h.headers.authorization)),l=h=>h instanceof Oe?{status:h.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:h instanceof _t?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,p=Pi({clients:async h=>o.clientsFor(await d(h)),usage:{authorize:h=>Gt(h.headers.authorization),lookup:(h,v)=>i.lookup(h,v)},statusOf:l,...t.requestLog?{requestLog:t.requestLog}:{},observe:h=>u.observe(h),ledger:{write:(h,v)=>a.write(Gt(h.headers.authorization),v),observe:h=>u.observeLedger(h),...t.ledgerLog?{report:t.ledgerLog}:{}}}),y=null,c=(0,Yi.createServer)((h,v)=>{let b=new URL(h.url??"/","http://localhost");(async()=>{if(h.method==="GET"&&b.pathname==="/healthz"){Qr(v,200,{ok:!0});return}if(h.method==="GET"&&b.pathname==="/readyz"){let E=Date.now();if(!y||E-y.at>=uc){let x=await s();y={at:E,...x}}Qr(v,y.ready?200:503,{ok:y.ready,control_plane:y.detail});return}let $=b.pathname==="/v1"||b.pathname.startsWith("/v1/"),S=b.pathname.startsWith("/partners/");if(!$&&!S){zi(v,404,`no route for ${h.method} ${b.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface, the partner adapter surface (/partners/...), the WS upgrade paths (/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent and /v1/realtime), plus /healthz and /readyz`,"invalid_request_error");return}p(h,v)})().catch($=>{if(!v.headersSent){zi(v,500,String($ instanceof Error?$.message:$),"proxy_error");return}v.end()})}),m=t.conversations??new Gn({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),w=[wr(c,{authenticate:async h=>{let v=await e.resolve(h),b=m.connectionFor(v);return{clients:b.clients,scope:Wn(v.apiKey),onBindResumeHandle:b.bindResumeHandle,clientsForResume:b.clientsForResume,release:b.release}}}),Er(c,{clients:async h=>o.clientsFor(await d(h)),statusOf:l}).close,bn(c,{resolver:{resolve:async(h,v)=>{let b=await e.resolve(Gt(h.headers.authorization)),$=m.connectionFor(b),S;try{S=await $i($.clients,Ci(h),v)}catch(E){throw $.release().catch(()=>{}),E}return{...S,release:$.release}}}})];c.on("upgrade",(h,v)=>{let b=new URL(h.url??"/","http://localhost");b.pathname===pn||b.pathname===Rr||b.pathname===Dt||v.destroyed||(v.write(`HTTP/1.1 404 Not Found\r
|
|
61
63
|
Connection: close\r
|
|
62
64
|
\r
|
|
63
|
-
no WS route for ${b.pathname} \u2014 this endpoint serves only the Gemini Live, /v1/responses, and /v1/realtime upgrade paths`),
|
|
64
|
-
`);let
|
|
65
|
-
`),await
|
|
66
|
-
`),
|
|
65
|
+
no WS route for ${b.pathname} \u2014 this endpoint serves only the Gemini Live, /v1/responses, and /v1/realtime upgrade paths`),v.destroy())});let R=async()=>{for(let h of w)await h();await m.detachAll()};return{server:c,metricsServer:ss(u),metrics:u,edgeIdentityServer:us(),registry:o,closeAll:()=>o.closeAll(),closeWebSockets:R}}async function Qi(t,e,n){n&&await n();let r=new Promise(o=>t.close(()=>o()));t.closeAllConnections(),await r,await Ho()||Go(),await e()}function pc(){let t=null;return t=Mo({streams:Pe.default,note:e=>{t?.isDead("stderr")!==!0&&$e(Pe.default.stderr,e)}}),t}async function fc(){pc();let t=ds(Pe.default.env),{server:e,metricsServer:n,edgeIdentityServer:r,closeAll:o,closeWebSockets:s}=Xi(t);await new Promise((a,u)=>{e.once("error",u),e.listen(t.port,Ze,a)}),await new Promise((a,u)=>{n.once("error",u),n.listen(cr,Ze,a)}),await new Promise((a,u)=>{r.once("error",u),r.listen(fr,Ze,a)}),Pe.default.stdout.write(`inference-proxy listening on http://${Ze}:${t.port}/v1 (metrics http://${Ze}:${cr}${nn}, edge identity http://${Ze}:${fr}, control plane ${t.baseUrl}, org API ${t.apiUrl}, catalog oracle ${t.catalog?"on":"off"})
|
|
66
|
+
`);let i=async a=>{Pe.default.stdout.write(`inference-proxy: ${a} \u2014 draining
|
|
67
|
+
`),await Qi(e,o,s),await new Promise(u=>n.close(()=>u())),await new Promise(u=>r.close(()=>u()))};await new Promise(a=>{Pe.default.once("SIGINT",()=>{i("SIGINT").finally(a)}),Pe.default.once("SIGTERM",()=>{i("SIGTERM").finally(a)})})}fc().then(()=>Pe.default.exit(0),t=>{Pe.default.stderr.write(`inference-proxy: ${String(t instanceof Error?t.stack??t.message:t)}
|
|
68
|
+
`),Pe.default.exit(1)});
|
|
67
69
|
/*! Bundled license information:
|
|
68
70
|
|
|
69
71
|
reflect-metadata/Reflect.js:
|