@urun-sh/openai 0.5.4 → 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{ResponsesClient-BYx3YLGo.d.ts → ResponsesClient-BE3-hx3m.d.ts} +2 -0
- package/dist/{ResponsesClient-Dft3bg3b.d.cts → ResponsesClient-DjZWjlFH.d.cts} +2 -0
- package/dist/chunk-2T2YYBVX.js +1 -0
- package/dist/chunk-3WAJD62J.js +4 -0
- package/dist/{chunk-K23AZPI4.js → chunk-5HKWNK3O.js} +1 -1
- package/dist/chunk-7M6LY6DR.js +2 -0
- package/dist/chunk-BSHT6RZZ.js +1 -0
- package/dist/{chunk-GBBY3PCZ.js → chunk-FP4RSAIE.js} +1 -1
- package/dist/chunk-HR5H6S7L.js +1 -0
- package/dist/chunk-NY23USZF.js +57 -0
- package/dist/chunk-VLRMJRLS.js +6 -0
- package/dist/gemini-live.cjs +2 -2
- package/dist/gemini-live.d.cts +17 -2
- package/dist/gemini-live.d.ts +7 -2
- package/dist/gemini-live.js +1 -1
- package/dist/hosted/bin.cjs +65 -37
- package/dist/hosted/bin.js +3 -3
- package/dist/hosted/index.cjs +46 -19
- package/dist/hosted/index.d.cts +760 -272
- package/dist/hosted/index.d.ts +203 -107
- 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/{models-NYMZrklp.d.cts → models-DUdx_Y6X.d.cts} +1 -1
- package/dist/pi-extension/index.cjs +7 -7
- package/dist/pi-extension/index.d.cts +1 -1
- package/dist/pi-extension/index.d.ts +1 -1
- package/dist/pi-extension/index.js +1 -1
- package/dist/pi-extension/standalone.cjs +48 -48
- package/dist/proxy/cli.cjs +54 -40
- package/dist/proxy/cli.js +14 -14
- package/dist/proxy/index.cjs +33 -22
- package/dist/proxy/index.d.cts +546 -4
- package/dist/proxy/index.d.ts +185 -4
- package/dist/proxy/index.js +1 -1
- package/dist/responses-turn-Dr37N3kH.d.ts +486 -0
- package/dist/responses-turn-tNUHq5b0.d.cts +1807 -0
- package/dist/{translator-C9uPKypK.d.ts → translator-BCoRFaTs.d.ts} +1 -1
- package/dist/{translator-CcDBEfvm.d.cts → translator-Bh0Bp_Ie.d.cts} +3 -3
- package/dist/{video-out-D20UuJ8G.d.cts → video-out-CCksIcj8.d.cts} +5 -5
- package/package.json +12 -11
- package/dist/chunk-4MSBS7M6.js +0 -1
- package/dist/chunk-5NXM4IO3.js +0 -2
- package/dist/chunk-CWJRDBDC.js +0 -1
- package/dist/chunk-OI2OY32M.js +0 -1
- package/dist/chunk-RQBD4OAA.js +0 -6
- package/dist/chunk-STFRT5OY.js +0 -1
- package/dist/chunk-VNM4M4P3.js +0 -40
- package/dist/server-DuH_5OD9.d.ts +0 -84
- package/dist/server-wmUnMewW.d.cts +0 -206
- /package/dist/{chunk-YSFSRI3D.js → chunk-5CCJUNH7.js} +0 -0
- /package/dist/{models-NYMZrklp.d.ts → models-DUdx_Y6X.d.ts} +0 -0
- /package/dist/{video-out-CWesbk12.d.ts → video-out-IGQ4YQ-c.d.ts} +0 -0
package/dist/proxy/cli.cjs
CHANGED
|
@@ -1,60 +1,74 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
"use strict";var Tn=Object.create;var ot=Object.defineProperty;var Cn=Object.getOwnPropertyDescriptor;var Un=Object.getOwnPropertyNames;var $n=Object.getPrototypeOf,Nn=Object.prototype.hasOwnProperty;var Ln=(e,n,t,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of Un(n))!Nn.call(e,r)&&r!==t&&ot(e,r,{get:()=>n[r],enumerable:!(o=Cn(n,r))||o.enumerable});return e};var ne=(e,n,t)=>(t=e!=null?Tn($n(e)):{},Ln(n||!e||!e.__esModule?ot(t,"default",{value:e,enumerable:!0}):t,e));var Mn=()=>typeof document>"u"?new URL(`file:${__filename}`).href:document.currentScript&&document.currentScript.tagName.toUpperCase()==="SCRIPT"?document.currentScript.src:new URL("main.js",document.baseURI).href,E=Mn();var tt=require("child_process"),Y=require("fs"),In=ne(require("path"),1),_=ne(require("process"),1);var de=require("@urun-sh/core");async function rt(e){let t=await(e.fetchImpl??fetch)(`${e.catalogUrl}/model_catalog?select=model_id,variant,gpu_spec,engine,task,description,size_gb,hf_repo,engine_args,updated_at`,{headers:{apikey:e.anonKey,Authorization:`Bearer ${e.anonKey}`}});if(!t.ok)throw new Error(`model_catalog fetch failed: ${t.status}`);return await t.json()}function st(e,n,t,o){let r=e.response??{},s={request_id:t,consumer_id:o,stream:!0,kind:"chat",messages:n};return typeof r.instructions=="string"&&(s.instructions=r.instructions),Array.isArray(r.modalities)&&(s.modalities=r.modalities),typeof r.temperature=="number"&&(s.temperature=r.temperature),typeof r.max_output_tokens=="number"&&(s.max_output_tokens=r.max_output_tokens),r.tools!==void 0&&(s.tools=r.tools),s}function it(e,n,t){let o={request_id:n,consumer_id:t,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(o.model=e.model),e.tools!==void 0&&(o.tools=e.tools),e.tool_choice!==void 0&&(o.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(o.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(o.max_output_tokens=e.max_output_tokens),typeof e.top_p=="number"&&(o.top_p=e.top_p),Array.isArray(e.stop)&&e.stop.length>0&&(o.stop=e.stop),typeof e.reasoning_effort=="string"&&(o.reasoning_effort=e.reasoning_effort),e.chat_template_kwargs!==void 0&&(o.chat_template_kwargs=e.chat_template_kwargs),o}function at(e){if(e instanceof Error)return{type:"error",error:{type:"urun_error",code:e.name||null,message:e.message}};if(e&&typeof e=="object"&&e.t==="error"){let n=e,t=n.body??{};return{type:"error",error:{type:"urun_error",code:n.code??null,message:t.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}var Dn="llm-resp";async function*Ue(e,n){let t=`${Dn}:${n}`,o=`resp_${n}`;yield{type:"response.created",response:{id:o,status:"in_progress"}};let r=new Map;for await(let s of e.stream(t).messages()){let i=s;if(i.t==="delta"){if(typeof i.delta=="string"&&(yield{type:"response.output_text.delta",item_id:o,delta:i.delta}),typeof i.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:o,delta:i.reasoning}),Array.isArray(i.tool_calls))for(let a of i.tool_calls){let c=typeof a.index=="number"?a.index:0,u=r.get(c)??{};a.id&&(u.id=a.id),a.function?.name&&(u.name=a.function.name),r.set(c,u),yield{type:"response.function_call_arguments.delta",item_id:`fc_${n}_${c}`,tool_index:c,call_id:u.id,name:u.name,delta:a.function?.arguments??""}}}else if(i.t==="response"){yield{type:"response.completed",response:{id:o,status:"completed",...i.body}};return}else if(i.t==="error"){yield at(i);return}}}var jn="llm",ye=class{constructor(n){this.session=n;this.sessionTag=n.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(n){n.session_tag=this.sessionTag,this.session.doc(jn).set({requests:{[n.request_id]:{payload:n,consumer_id:n.consumer_id,stream:n.stream}}})}sendResponses(n,t){let o=it(n,t,this.consumerId);return this.write(o),Ue(this.session,t)}sendResponseCreate(n,t,o){let r=st(n,t,o,this.consumerId);return this.write(r),Ue(this.session,o)}};var ut=0;function Vn(){return ut+=1,`req_${Date.now().toString(36)}_${ut.toString(36)}`}var we=class{transport;constructor(n){this.transport=new ye(n)}responses={create:async n=>{let t=Vn(),o=this.transport.sendResponses(n,t);return Object.assign((async function*(){yield*o})(),{requestId:t})}}};var $e=class{constructor(n,t){this.backend=n;this.opts=t}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(n){if(!this.source)throw new Error("startOutbound() not called");let t=Buffer.from(n,"base64"),o=Math.floor(t.byteLength/2),r=new Int16Array(o);for(let s=0;s<o;s++)r[s]=t.readInt16LE(s*2);this.source.onData(r)}async startInbound(n){if(n==null)throw new Error('startInbound: no downstream audio track \u2014 the runtime audio producer has not been consumed yet (session.stream("rt-audio-out").track is null)');this.sink=this.backend.createSink(n),this.sink.onframe=t=>{let o=Buffer.from(t.samples.buffer,t.samples.byteOffset,t.samples.byteLength).toString("base64");for(let r of this.outHandlers)r(o)}}onOutputAudio(n){return this.outHandlers.add(n),()=>this.outHandlers.delete(n)}};async function dt(e,n,t=1e4){let o=new $e(n,{sampleRate:24e3}),r=await o.startOutbound();await e.stream("rt-audio-in").attach(r);let s=await Bn(e,t);return await o.startInbound(s),o}function Bn(e,n){let t=e.stream("rt-audio-out");return t.track!=null?Promise.resolve(t.track):new Promise((o,r)=>{let s,i=setTimeout(()=>{s?.(),r(new Error(`enableSessionAudio: no downstream audio track within ${n}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},n),a=c=>{c!=null&&(clearTimeout(i),s?.(),o(c))};s=t.on?.("track",a),t.track!=null&&a(t.track)})}var ct="rt-video-in",lt=256e3;function pt(e){let n=e.stream(ct),t=n.emit;if(typeof t!="function")throw new Error(`enableSessionVideo: session.stream('${ct}') has no emit() \u2014 the named-DATA stream produce seam (@urun-sh/core SessionStream.emit, contract \xA75) is required for the video frame lane`);return{sendInputFrame:async o=>{if(o.byteLength===0)throw new Error("enableSessionVideo: refusing to send an empty video frame");if(o.byteLength>lt)throw new Error(`enableSessionVideo: frame is ${o.byteLength} bytes \u2014 over the ${lt}-byte data-channel budget (the SFU's 262144-byte SCTP ceiling drops oversize messages SILENTLY; send smaller/lower-quality frames)`);await t.call(n,o)}}}var Fn=24e3,le=480,Gn=111;async function Hn(){let e=await import("opusscript"),n=e.default??e;return new n(Fn,1,2048)}async function _e(){let e=await import("werift"),n=await Hn();return{createSource:()=>{let t=new e.MediaStreamTrack({kind:"audio"}),o=Math.random()*4294967295>>>0,r=Math.random()*65535&65535,s=Math.random()*4294967295>>>0,i=new Int16Array(0);return{track:t,onData:a=>{let c=new Int16Array(i.length+a.length);c.set(i,0),c.set(a,i.length);let u=0;for(;c.length-u>=le;){let l=c.subarray(u,u+le);u+=le;let p=Buffer.from(l.buffer,l.byteOffset,l.byteLength),g=n.encode(p,le);r=r+1&65535,s=s+le>>>0;let h=new e.RtpHeader({version:2,payloadType:Gn,sequenceNumber:r,timestamp:s,ssrc:o,marker:!1}),w=new e.RtpPacket(h,g);t.writeRtp(w)}i=c.slice(u)}}},createSink:t=>{let o={onframe:null};return t.onReceiveRtp.subscribe(s=>{let i;try{i=n.decode(s.payload)}catch{return}let a=new Int16Array(i.length/2);for(let c=0;c<a.length;c++)a[c]=i.readInt16LE(c*2);o.onframe?.({samples:a})}),o}}}var ve=require("@urun-sh/core"),Ne="rt-video-out";var qn="h264";function Jn(e,n){if(!Number.isInteger(e.seq)||e.seq<0)throw new Error(`${n}: header seq must be a non-negative integer (got ${e.seq})`);if(!Number.isInteger(e.ptsUs))throw new Error(`${n}: header ptsUs must be an integer microsecond timestamp (got ${e.ptsUs})`);if(typeof e.codec!="string"||e.codec.length===0)throw new Error(`${n}: header codec must be a non-empty string`);if(typeof e.key!="boolean")throw new Error(`${n}: header key must be a boolean`)}function Wn(e){if(e.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${e.byteLength} byte(s), no header length)`);let n=e[0]|e[1]<<8;if(e.byteLength<2+n)throw new Error(`decodeVideoOutRecord: truncated record (header says ${n} bytes, ${e.byteLength-2} present)`);let t;try{t=JSON.parse(new TextDecoder().decode(e.subarray(2,2+n)))}catch(i){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(i)})`)}let o=t;if(o===null||typeof o!="object"||o.v!==1)throw new Error(`decodeVideoOutRecord: header is not spec v1 (v=${JSON.stringify(o?.v)}) \u2014 a newer producer must be met with a newer consumer, never silently misread`);let r=o;Jn(r,"decodeVideoOutRecord");let s=e.subarray(2+n);if(s.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:r,au:s.slice()}}var ft="urun.videoOut.v2-ws";function mt(e){let n=e.stream(Ne);if(typeof n.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${Ne}') has no messages() \u2014 the named-DATA stream consume seam (@urun-sh/core SessionStream.messages, contract \xA75) is required for the video OUT lane`);let t=new Set,o=new Set,r=new Set,s=c=>{for(let u of o)u(c)},i=!1,a=async()=>{for await(let c of n.messages()){if(!(c instanceof Uint8Array)){s(new Error(`openSessionVideoOutLane: "${Ne}" delivered a non-BINARY \xA75 payload (${typeof c}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let u;try{u=Wn(c)}catch(l){s(l instanceof Error?l:new Error(String(l)));continue}for(let l of t)l(u)}for(let c of r)c()};return{codec:qn,onOutputFrame:c=>(t.add(c),i||(i=!0,a().catch(u=>s(u instanceof Error?u:new Error(String(u))))),()=>t.delete(c)),onLaneError:c=>(o.add(c),()=>o.delete(c)),onEnd:c=>(r.add(c),()=>r.delete(c))}}function Kn(e){let n=e?.type;return n==="response.created"||n==="response.in_progress"}function Yn(e){let n=e?.type;return n==="response.completed"||n==="error"}function gt(e){return async function(t){return(async function*(){for(let r=1;r<=2;r++){let{app:s,entry:i}=await e.sessionFor(t.model),a=!1,c=!1;try{let u=await e.createStream(i,t);for await(let l of u){if(Yn(l)&&(a=!0),Kn(l)){if(r>1)continue}else c=!0;yield l}}catch(u){if(r<2&&!c){await e.evict(s,i);continue}throw u}if(a)return;if(r<2&&!c){await e.evict(s,i);continue}return}})()}}function X(e){return e.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}var zn={chat:!0,code:!0,agent:!0,vl:!0},ht={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function Xn(e){let n=e.toLowerCase();return Object.hasOwn(ht,n)?ht[n]:null}function Le(e){let n=e.engine_args?.context_length;return typeof n=="number"&&Number.isFinite(n)&&n>0?n:null}function wt(e,n){let t=n.filter(s=>X(`${s.model_id}-${s.variant}`)===e);if(t.length===1)return t[0];if(t.length>1)return yt(t);let o=n.filter(s=>X(s.model_id)===e),r=new Set(o.map(s=>s.variant));return o.length>0&&r.size===1?yt(o):null}function yt(e){let n=e[0],t=i=>e.every(a=>(a[i]??void 0)===(n[i]??void 0))?n[i]:void 0,o=e.map(Le),r=o[0],s=r!=null&&o.every(i=>i===r)?r:null;return{model_id:n.model_id,variant:n.variant,gpu_spec:t("gpu_spec"),engine:t("engine"),task:t("task"),description:t("description"),size_gb:t("size_gb"),hf_repo:t("hf_repo"),engine_args:s!==null?{context_length:s}:{},updated_at:t("updated_at")}}function _t(e){let n=e.updated_at?Date.parse(e.updated_at):NaN;return Number.isFinite(n)?Math.floor(n/1e3):0}function vt(e,n){let t=wt(e,n);if(!t)return{id:e,object:"model",created:0,owned_by:"urun"};let o=Le(t);return{id:e,object:"model",created:_t(t),owned_by:"urun",name:`${t.model_id}:${t.variant}`,...t.description?{description:t.description}:{},...t.task?{task:t.task}:{},...t.engine?{engine:t.engine}:{},...t.gpu_spec?{gpu_spec:t.gpu_spec}:{},...o!==null?{context_length:o}:{}}}function kt(e,n){let t=[];for(let o of e){let r=wt(o,n);if(!r||!r.task||zn[r.task]!==!0)continue;let s=Le(r),i={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};s!==null&&(i.max_tokens={type:"integer",min:1,max:s,unit:"token"}),t.push({schema_version:"2.4",id:o,name:`${r.model_id}:${r.variant}`,created:_t(r),quantization:Xn(r.variant),description:r.description??"",hugging_face_id:r.hf_repo?r.hf_repo.split("@")[0]??r.hf_repo:"",input_modalities:[{type:"text",...s!==null?{supported_inputs:{max_context_length:{value:s,unit:"token"}}}:{}},...r.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:i}]})}return{data:t}}var bt="https://api.urun.sh/v1",N=class extends Error{},V=class extends Error{},Me="urs1.";function Qn(e,n){return Me+Buffer.from(JSON.stringify([e,n]),"utf8").toString("base64url")}function Zn(e){if(!e.startsWith(Me))return null;try{let n=JSON.parse(Buffer.from(e.slice(Me.length),"base64url").toString("utf8"));if(Array.isArray(n)&&typeof n[0]=="string"&&typeof n[1]=="string")return{app:n[0],key:n[1]}}catch{}return null}async function St(e){let n=e.fetchImpl??fetch,t=`${e.apiUrl.replace(/\/+$/,"")}/apps`,o=await n(t,{headers:{Authorization:`Bearer ${e.apiKey}`,Accept:"application/json"}});if(!o.ok)throw new Error(`org apps listing failed: GET ${t} \u2192 ${o.status}`);let r=await o.json();if(!Array.isArray(r.apps))throw new Error(`org apps listing returned no "apps" array (GET ${t})`);if(r.truncated===!0)throw new Error(`org apps listing was truncated (GET ${t} returned ${r.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return r.apps}var xt=6e4,ke=class{constructor(n){this.opts=n}opts;pool=new Map;appsCache=null;rowsCache=null;seed(n,t){this.pool.set(n,Promise.resolve(t))}async deployedApps(){if(!this.opts.listApps)return[];let n=this.opts.appsTtlMs??xt;if(this.appsCache&&Date.now()-this.appsCache.at<n)return this.appsCache.apps;let t=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:t},t}async catalogRows(){if(!this.opts.listCatalog)return[];let n=this.opts.appsTtlMs??xt;if(this.rowsCache&&Date.now()-this.rowsCache.at<n)return this.rowsCache.rows;let t=await this.opts.listCatalog();return this.rowsCache={at:Date.now(),rows:t},t}servable(n){return n.filter(t=>t.function_name===this.opts.fnName&&t.deployment_status==="active").map(t=>t.app_slug)}async availableIds(){let n=this.opts.defaultApp,t=new Set(n===null?[]:[n]);if(this.opts.listApps)for(let r of this.servable(await this.deployedApps()))t.add(r);for(let r of this.pool.keys())t.add(r);let o=[...t].filter(r=>r!==n).sort();return n===null?o:[n,...o]}async noDefaultApp(n){let t=await this.availableIds(),o=t.length>0?t.join(", "):"(none deployed)";throw new N(n===""?`no "model" was given and this endpoint has no default model \u2014 name one of your org's deployed "${this.opts.fnName}" apps; available models: ${o}`:`model "${n}" does not name a deployed "${this.opts.fnName}" app in your org, and this endpoint has no default model to fall back to; available models: ${o}`)}async resolveApp(n){let t=(n??"").trim(),o=this.opts.defaultApp;if(!t||t==="urun")return o===null?this.noDefaultApp(t):o;let r=X(t);if(r===o)return o;if(this.pool.has(r))return r;if(this.opts.listApps){let s=await this.deployedApps(),i=this.servable(s);if(i.includes(r))return r;let a=i.filter(u=>u.startsWith(`${r}-`));if(a.length===1)return a[0];if(a.length>1)throw new N(`model "${t}" is ambiguous across deployed apps ${a.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let c=s.find(u=>u.app_slug===r||u.app_slug.startsWith(`${r}-`));if(c)throw new N(`model "${t}" maps to app "${c.app_slug}", which is not an active "${this.opts.fnName}" app \u2014 deploy it with \`urun serve ${t}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.listCatalog){let i=(await this.opts.listCatalog()).find(a=>t===a.model_id||t===`${a.model_id}:${a.variant}`||r===X(a.model_id)||r===X(`${a.model_id}-${a.variant}`));if(i)throw new N(`model "${t}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${i.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return o===null?this.noDefaultApp(t):o}async sessionFor(n){let t=await this.resolveApp(n),o=this.pool.get(t);return o||(o=Promise.resolve(this.opts.openSession(t)),this.pool.set(t,o),o.catch(()=>this.pool.delete(t))),{app:t,entry:await o}}keyOf(n){let t=this.opts.sessionKey;if(!t)throw new Error("ModelRouter: sessionKey is not configured \u2014 the session-identity seam (handleFor/sessionForHandle) is unavailable on this router");let o=t(n);if(typeof o!="string"||o.length===0)throw new Error("ModelRouter: sessionKey returned an empty identity for a pooled session");return o}async handleFor(n){let{app:t,entry:o}=await this.sessionFor(n);return{app:t,handle:Qn(t,this.keyOf(o))}}async sessionForHandle(n){let t=Zn(n);if(!t)throw new V(`malformed session handle ${JSON.stringify(n)} \u2014 not issued by this proxy`);let{app:o,key:r}=t,s=this.pool.get(o),i=s?await s.then(a=>a,()=>null):null;if(i===null)throw new V(`the session behind this handle (app "${o}") is gone \u2014 closed, evicted after its pod died, or the proxy restarted; it cannot be reattached. Reconnect without a handle to start a new session.`);if(this.keyOf(i)!==r)throw new V(`the session behind this handle (app "${o}") was replaced \u2014 the original backhaul died and a new session serves this app now; the handle's session cannot be reattached. Reconnect without a handle to start a new session.`);return{app:o,entry:i}}async evict(n,t){let o=this.pool.get(n);!o||await o.then(s=>s,()=>null)!==t||this.pool.get(n)!==o||(this.pool.delete(n),await this.opts.closeSession(t).catch(()=>{}))}async modelList(){let n=await this.availableIds(),t=await this.catalogRows().catch(()=>[]);return{object:"list",data:n.map(o=>vt(o,t))}}async openRouterModels(){let n=await this.availableIds(),t=await this.catalogRows();return kt(n,t)}async closeAll(){let n=[...this.pool.values()];this.pool.clear();let t=[];if(await Promise.all(n.map(async o=>{try{await this.opts.closeSession(await o)}catch(r){t.push(String(r instanceof Error?r.message:r))}})),t.length>0)throw new Error(`failed to close ${t.length} pooled session(s): ${t.join("; ")}`)}};var eo=3,to="llm",no=new Set(["paused","error"]);function Rt(e,n){let t=e.doc(to),o=typeof t.onConnectionState=="function",r=typeof e.onPhase=="function";if(!o&&!r)throw new Error(`session for app "${n}" exposes neither a doc connection-state surface nor onPhase \u2014 session-gone detection requires @urun-sh/core Session (SessionDocument.onConnectionState / Session.onPhase); pooling a session with no native lifecycle signal would silently swallow requests dispatched after the platform closes it`);let s=null,i=new Set,a=[],c=u=>{if(!s){s=u;for(let l of[...i])l(u);i.clear()}};return o&&a.push(t.onConnectionState(u=>{u.consecutiveFailures<eo||c(new V(`the pooled session for app "${n}" is gone: its doc backhaul failed ${u.consecutiveFailures} consecutive times (last close ${u.lastCloseCode===null?"by provider liveness reap":`code ${u.lastCloseCode}`}${u.lastCloseReason?`, reason ${JSON.stringify(u.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`))})),r&&a.push(e.onPhase(u=>{no.has(u.name)&&c(new V(`the pooled session for app "${n}" lost its compute binding (phase "${u.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(u){return s?(u(s),()=>{}):(i.add(u),()=>i.delete(u))},dispose(){i.clear();for(let u of a.splice(0))u()}}}async function*De(e,n){let t=n.gone();if(t)throw t;let o=e[Symbol.asyncIterator](),r,s=new Promise((i,a)=>{r=n.onGone(c=>a(c))});s.catch(()=>{});try{for(;;){let i=await Promise.race([o.next(),s]);if(i.done)return;yield i.value}}finally{r?.(),o.return?.(void 0)}}function At(e,n,t){let o=e[t];if(typeof o!="function")throw new Error(`app "${n}" has no function "${t}" (set URUN_FUNCTION)`);let r=o();if(typeof r.end!="function")throw new Error(`app "${n}" function "${t}" returned a session without end()`);return r}function oo(e,n){let{baseUrl:t,orgId:o,fnName:r,auth:s,subject:i}=e,a=p=>({session:p,responses:new we(p),gone:Rt(p,n)});if(s.lane==="jwt"){let p=(0,de.App)(n,{baseUrl:t,orgId:o,jwt:s.jwt});return a(At(p,n,r))}let{apiKey:c,gatewayUrl:u}=s,l=(0,de.App)(n,{baseUrl:t,orgId:o,getAccessToken:async()=>(await(0,de.createClientToken)(c,{baseUrl:u,expiresIn:300,allowedFunctions:[`${n}/${r}`],...i===void 0?{}:{subject:i}})).token});return a(At(l,n,r))}function ro(e){let n=e.session.id;if(typeof n!="string"||n.length===0)throw new Error("pooled session exposes no stable session id \u2014 the session-identity seam requires @urun-sh/core Session (session.id)");return n}var so=new Set(["error","paused"]);function Et(e){let{auth:n,catalog:t}=e,o=null,r=i=>{let a=oo(e,i);return a.gone.onGone(()=>{o?.evict(i,a)}),a},s=new ke({defaultApp:e.defaultApp,fnName:e.fnName,openSession:r,closeSession:async i=>{i.gone.dispose(),await i.session.end()},listApps:n.lane==="api-key"?()=>St({apiUrl:e.apiUrl,apiKey:n.apiKey}):null,listCatalog:t?()=>rt(t):null,sessionKey:ro});return o=s,e.defaultApp!==null&&s.seed(e.defaultApp,r(e.defaultApp)),s}function Ot(e,n=_e){let t=new WeakMap,o=new WeakMap,r=new WeakMap;return{createResponse:gt({sessionFor:s=>e.sessionFor(s),evict:(s,i)=>e.evict(s,i),createStream:async(s,i)=>De(await s.responses.responses.create(i),s.gone)}),listModels:()=>e.modelList(),openRouterModels:()=>e.openRouterModels(),openAudio:async s=>{let{entry:i}=await e.sessionFor(s),a=t.get(i);return a||(a=(async()=>dt(i.session,await n()))(),t.set(i,a),a.catch(()=>t.delete(i))),a},openVideo:async s=>{let{entry:i}=await e.sessionFor(s),a=o.get(i);return a||(a=pt(i.session),o.set(i,a)),a},openVideoOut:async s=>{let{entry:i}=await e.sessionFor(s),a=r.get(i);return a||(a=mt(i.session),r.set(i,a)),a},sessionHandle:async s=>(await e.handleFor(s)).handle,createResponseOn:async(s,i)=>{let{entry:a}=await e.sessionForHandle(s);return De(await a.responses.responses.create(i),a.gone)},onSessionEnd:async(s,i)=>{let{entry:a}=await e.sessionForHandle(s),c=a.session;if(typeof c.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let u=!1;return c.onPhase(l=>{if(u||!so.has(l.name))return;u=!0;let p=c.endsAt??null;i({timeLeftMs:p?Math.max(0,p.getTime()-Date.now()):null,reason:l.reason??`session ${l.name}`})})}}}var Zt=require("http");var Wt=require("crypto"),Kt=require("ws");var Pt="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",ao=1007,Tt=1008,W=1011,je=1008,d=class extends Error{constructor(t,o=ao){super(t);this.closeCode=o}closeCode},O=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)?e:null,xe=["setup","clientContent","realtimeInput","toolResponse"];function Ct(e){let n;try{n=JSON.parse(e)}catch{throw new d("client message is not valid JSON")}let t=O(n);if(!t)throw new d("client message must be a JSON object");let o=xe.filter(a=>t[a]!==void 0),r=Object.keys(t).filter(a=>!xe.includes(a));if(r.length>0)throw new d(`unknown client message field(s) ${JSON.stringify(r)} \u2014 expected exactly one of ${xe.join(", ")}`);if(o.length!==1)throw new d(`client message must contain exactly one of ${xe.join(", ")} (got ${o.length})`);let s=o[0],i=O(t[s]);if(!i)throw new d(`"${s}" must be a JSON object`);return{kind:s,[s]:i}}function Ut(e,n){if(typeof e=="string")return e;let t=O(e),o=t?t.parts:void 0;if(!Array.isArray(o))throw new d(`${n}: expected a Content with a parts array`);let r=[];for(let s of o){let i=O(s);if(i&&typeof i.text=="string"){r.push(i.text);continue}throw i&&(i.inlineData!==void 0||i.fileData!==void 0)?new d(`${n}: 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 d(`${n}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new d(`${n}: unsupported part ${JSON.stringify(s)}`)}return r.join(`
|
|
3
|
-
`)}var uo={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},Q="urun.videoOut";function $t(e){return{framing:ft,codec:e}}function Nt(e){let{seq:n,ptsUs:t,codec:o,key:r}=e.header;return{[Q]:{v:1,seq:n,ptsUs:t,codec:o,key:r,data:Buffer.from(e.au).toString("base64")}}}var co={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},lo=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",Q],It=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function po(e){let n=O(e);if(!n)throw new d("setup.realtimeInputConfig must be an object");for(let r of Object.keys(n))if(!(r==="automaticActivityDetection"||r==="activityHandling"))throw new d(r==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${r}"`);let t=O(n.automaticActivityDetection);if(n.automaticActivityDetection!==void 0&&!t)throw new d("realtimeInputConfig.automaticActivityDetection must be an object");for(let r of Object.keys(t??{}))if(r!=="disabled")throw new d(`automaticActivityDetection.${r} tunes AUTOMATIC (server-side) activity detection \u2014 the declared platform follow-up (serve turn_detection events over the session stream lane); only "disabled: true" (manual activity mode) is supported in this slice`);if(t?.disabled!==!0)throw new d("automatic (server-side) activity detection is the declared platform follow-up (serve turn_detection turn_start/turn_end events over the session stream lane, not proxy DSP) \u2014 set realtimeInputConfig.automaticActivityDetection.disabled: true for manual activity mode");let o="START_OF_ACTIVITY_INTERRUPTS";if(n.activityHandling!==void 0){if(!It.includes(n.activityHandling))throw new d(`unsupported activityHandling ${JSON.stringify(n.activityHandling)} \u2014 expected one of ${It.join(", ")}`);o=n.activityHandling}return{manualActivity:!0,activityHandling:o}}var fo=["temperature","maxOutputTokens","responseModalities"];function Lt(e){for(let[p,g]of Object.entries(co))if(e[p]!==void 0)throw new d(g);for(let p of Object.keys(e))if(!lo.includes(p))throw new d(`unsupported setup field "${p}"`);if(typeof e.model!="string"||e.model.length===0)throw new d('setup.model is required (format "models/{model}")');let n=e.model.replace(/^models\//,""),t,o,r=!1,s=O(e.generationConfig);if(e.generationConfig!==void 0&&!s)throw new d("setup.generationConfig must be an object");if(s){for(let p of Object.keys(s))if(!fo.includes(p))throw new d(`unsupported generationConfig field "${p}"`);if(s.temperature!==void 0){if(typeof s.temperature!="number")throw new d("generationConfig.temperature must be a number");t=s.temperature}if(s.maxOutputTokens!==void 0){if(typeof s.maxOutputTokens!="number")throw new d("generationConfig.maxOutputTokens must be a number");o=s.maxOutputTokens}if(s.responseModalities!==void 0){let p=Array.isArray(s.responseModalities)?s.responseModalities:[s.responseModalities];for(let g of p){let h=String(g).toUpperCase();if(h==="AUDIO"){r=!0;continue}if(h!=="TEXT")throw new d(`responseModalities ${JSON.stringify(g)} is not supported \u2014 this surface speaks TEXT and AUDIO (audio-out rides the native AudioBridge rt-audio-out lane)`)}}}let i=[];e.systemInstruction!==void 0&&i.push({role:"system",content:Ut(e.systemInstruction,"setup.systemInstruction")});let a;if(e.tools!==void 0){if(!Array.isArray(e.tools))throw new d("setup.tools must be an array");a=[];for(let p of e.tools){let g=O(p);if(!g)throw new d("setup.tools entries must be objects");for(let h of Object.keys(g))if(h!=="functionDeclarations")throw new d(`unsupported tool "${h}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(g.functionDeclarations))throw new d("tool.functionDeclarations must be an array");for(let h of g.functionDeclarations){let w=O(h);if(!w||typeof w.name!="string")throw new d("functionDeclarations entries require a string name");a.push({type:"function",name:w.name,description:w.description,parameters:w.parameters})}}a.length===0&&(a=void 0)}let c={enabled:!1,handle:null};if(e.sessionResumption!==void 0){let p=O(e.sessionResumption);if(!p)throw new d("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let g of Object.keys(p))if(g!=="handle")throw new d(`unsupported sessionResumption field "${g}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(p.handle!==void 0&&(typeof p.handle!="string"||p.handle.length===0))throw new d("sessionResumption.handle must be a non-empty string when present");c={enabled:!0,handle:typeof p.handle=="string"?p.handle:null}}let u=e.realtimeInputConfig===void 0?uo:po(e.realtimeInputConfig),l=!1;if(e[Q]!==void 0){if(!O(e[Q]))throw new d(`setup["${Q}"] must be an object (the v1 opt-in is {})`);l=!0}return{model:n,systemItems:i,tools:a,temperature:t,maxOutputTokens:o,audioModality:r,activity:u,sessionResumption:c,videoOut:l}}function Mt(e){return{sessionResumptionUpdate:{newHandle:e,resumable:!0}}}function Dt(e){let n=Math.max(0,e??0)/1e3;return{goAway:{timeLeft:`${Math.round(n*1e3)/1e3}s`}}}function jt(e){for(let o of Object.keys(e))if(o!=="turns"&&o!=="turnComplete")throw new d(`unsupported clientContent field "${o}"`);let n=e.turns===void 0?[]:e.turns;if(!Array.isArray(n))throw new d("clientContent.turns must be an array");let t=[];for(let o of n){let r=O(o);if(!r)throw new d("clientContent.turns entries must be Content objects");let s=r.role===void 0?"user":String(r.role).toLowerCase();if(s!=="user"&&s!=="model")throw new d(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(r.role)})`);t.push({role:s==="model"?"assistant":"user",content:Ut(r,"clientContent.turns")})}return{items:t,turnComplete:e.turnComplete===!0}}var be=16e3,Vt=24e3,mo=`audio/pcm;rate=${Vt}`,Bt=Buffer.alloc(960).toString("base64");function go(e,n,t){if(n===t)return e;let o=Math.floor(e.length*t/n),r=new Int16Array(o),s=n/t,i=e.length-1;for(let a=0;a<o;a++){let c=a*s,u=Math.floor(c);if(u>=i){r[a]=e[i];continue}let l=c-u;r[a]=Math.round(e[u]+(e[u+1]-e[u])*l)}return r}function ho(e){let n=O(e);if(!n)throw new d("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof n.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(n.mimeType))throw new d(`realtimeInput.video.mimeType must be image/jpeg (the Live video input stream is individual JPEG frames), got ${JSON.stringify(n.mimeType)}`);if(typeof n.data!="string"||n.data.length===0)throw new d("realtimeInput.video.data must be non-empty base64");let t=Buffer.from(n.data,"base64");if(t.length<3||t[0]!==255||t[1]!==216)throw new d("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(t)}function yo(e){let n=O(e);if(!n)throw new d("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof n.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(n.mimeType))throw new d(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(n.mimeType)}`);let t=/;\s*rate=(\d+)/.exec(n.mimeType);if(t&&Number(t[1])!==be)throw new d(`realtimeInput.audio.mimeType declares rate=${t[1]} \u2014 the Live spec input format is ${be} Hz (audio/pcm;rate=${be})`);if(typeof n.data!="string"||n.data.length===0)throw new d("realtimeInput.audio.data must be non-empty base64");let o=Buffer.from(n.data,"base64");if(o.length===0||o.length%2!==0)throw new d(`realtimeInput.audio.data must decode to whole 16-bit samples (got ${o.length} bytes \u2014 not even)`);let r=new Int16Array(o.length/2);for(let i=0;i<r.length;i++)r[i]=o.readInt16LE(i*2);let s=go(r,be,Vt);return Buffer.from(s.buffer,s.byteOffset,s.byteLength).toString("base64")}function Ft(e){if(e.mediaChunks!==void 0)throw new d('realtimeInput.mediaChunks is deprecated by the Live API \u2014 send the audio stream via realtimeInput.audio ({mimeType:"audio/pcm;rate=16000", data}) instead');if(e.activityStart!==void 0||e.activityEnd!==void 0){if(Object.keys(e).length!==1||e.activityStart!==void 0&&e.activityEnd!==void 0)throw new d("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let r=e.activityStart!==void 0?"activityStart":"activityEnd",s=O(e[r]);if(!s||Object.keys(s).length!==0)throw new d(`realtimeInput.${r} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:r}}let n=["text","audio","audioStreamEnd","video"];for(let o of Object.keys(e))if(!n.includes(o))throw new d(`unsupported realtimeInput field "${o}"`);let t=n.filter(o=>e[o]!==void 0);if(t.length!==1)throw new d(`realtimeInput must carry exactly one of ${n.join(", ")} (got ${t.length})`);if(e.audio!==void 0)return{kind:"audio",base64Pcm24k:yo(e.audio)};if(e.video!==void 0)return{kind:"video",jpegFrame:ho(e.video)};if(e.audioStreamEnd!==void 0){if(e.audioStreamEnd!==!0)throw new d("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof e.text!="string"||e.text.length===0)throw new d("realtimeInput.text must be a non-empty string");return{kind:"text",text:e.text}}function Gt(e){for(let n of Object.keys(e))if(n!=="functionResponses")throw new d(`unsupported toolResponse field "${n}"`);if(!Array.isArray(e.functionResponses)||e.functionResponses.length===0)throw new d("toolResponse.functionResponses must be a non-empty array");return e.functionResponses.map(n=>{let t=O(n);if(!t||typeof t.id!="string"||t.id.length===0)throw new d("functionResponses entries require the string id matched from the toolCall");return{type:"function_call_output",call_id:t.id,output:JSON.stringify(t.response??null)}})}function Ht(e){return e.type!=="response.output_text.delta"||typeof e.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:e.delta}]}}}}function qt(e){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:mo,data:e}}]}}}}function Jt(e){let n=O(e)??{},t=Array.isArray(n.output)?n.output:[],o=[],r=[],s=typeof n.output_text=="string"?n.output_text:"";if(!s)for(let u of t){let l=O(u);if(!(l?.type!=="message"||!Array.isArray(l.content)))for(let p of l.content){let g=O(p);g?.type==="output_text"&&typeof g.text=="string"&&(s+=g.text)}}s&&r.push({role:"assistant",content:s});for(let u of t){let l=O(u);if(l?.type!=="function_call")continue;if(typeof l.call_id!="string"||typeof l.name!="string")throw new d(`upstream function_call item is missing call_id/name: ${JSON.stringify(u)}`,W);let p=typeof l.arguments=="string"&&l.arguments.length>0?l.arguments:"{}",g;try{g=JSON.parse(p)}catch{throw new d(`upstream function_call arguments are not valid JSON: ${p}`,W)}o.push({id:l.call_id,name:l.name,args:g}),r.push({type:"function_call",call_id:l.call_id,name:l.name,arguments:p})}let i=[];o.length>0&&i.push({toolCall:{functionCalls:o}}),i.push({serverContent:{generationComplete:!0}});let a=O(n.usage),c={serverContent:{turnComplete:!0}};if(a){let u={};typeof a.input_tokens=="number"&&(u.promptTokenCount=a.input_tokens),typeof a.output_tokens=="number"&&(u.responseTokenCount=a.output_tokens),typeof a.total_tokens=="number"&&(u.totalTokenCount=a.total_tokens),Object.keys(u).length>0&&(c.usageMetadata=u)}return i.push(c),{messages:i,assistantItems:r}}var wo=256,Ve=class{snapshots=new Map;store(n,t){for(this.snapshots.set(n,t);this.snapshots.size>wo;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(n){return this.snapshots.get(n)}},pe=e=>e.slice(0,120),Be=class{constructor(n,t,o){this.ws=n;this.clients=t;this.registry=o;n.on("message",r=>this.onMessage(r)),n.on("close",()=>{this.unsubscribeEnd?.(),this.unsubscribeEnd=null,this.detachAudio?.(),this.detachVideoOut?.()})}ws;clients;registry;config=null;transcript=[];queue=Promise.resolve();proxyHandle=null;unsubscribeEnd=null;audio=null;detachAudio=null;detachVideoOut=null;video=null;activityOpen=!1;activeTurn=null;send(n){this.ws.readyState===this.ws.OPEN&&this.ws.send(JSON.stringify(n))}fail(n){if(n instanceof N){this.ws.close(Tt,pe(n.message));return}if(n instanceof V){this.ws.close(je,pe(n.message));return}if(n instanceof d){this.ws.close(n.closeCode,pe(n.message));return}this.ws.close(W,pe(String(n instanceof Error?n.message:n)))}onMessage(n){try{let t=Ct(typeof n=="string"?n:Buffer.concat(Array.isArray(n)?n:[n]).toString("utf8"));if(t.kind==="setup"){this.handleSetup(t.setup).catch(r=>this.fail(r));return}if(!this.config)throw new d(`"${t.kind}" before setup \u2014 the first client message must be BidiGenerateContentSetup`);let o=this.config;if(t.kind==="clientContent"){let{items:r,turnComplete:s}=jt(t.clientContent);this.transcript.push(...r),s&&this.enqueue(()=>this.runTurn());return}if(t.kind==="realtimeInput"){let r=Ft(t.realtimeInput);if(r.kind==="activityStart"||r.kind==="activityEnd"){this.onActivity(r.kind,o);return}if(r.kind==="text"){this.transcript.push({role:"user",content:r.text}),this.enqueue(()=>this.runTurn());return}if(r.kind==="video"){this.appendVideoFrame(r.jpegFrame);return}this.appendAudio(r.kind==="audio"?r.base64Pcm24k:Bt);return}this.transcript.push(...Gt(t.toolResponse)),this.enqueue(()=>this.runTurn())}catch(t){this.fail(t)}}async handleSetup(n){if(this.config)throw new d("setup may only be sent once, as the first client message");let t=Lt(n),o=t.sessionResumption.handle;if(o!==null){let s=this.registry.get(o);if(!s)throw new d("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",je);if(s.model!==t.model)throw new d(`sessionResumption.handle was issued for model "${s.model}" \u2014 the model cannot change on resume (got "${t.model}", per the Live API session-resumption contract)`);if(t.systemItems.length>0)throw new d("setup.systemInstruction cannot be changed on resume \u2014 it is part of the resumed conversation state");this.transcript=[...s.transcript],this.proxyHandle=s.proxyHandle,this.unsubscribeEnd=await this.clients.onSessionEnd(s.proxyHandle,i=>this.enqueue(()=>this.handleUpstreamEnd(i)))}else this.proxyHandle=await this.clients.sessionHandle(t.model),this.unsubscribeEnd=await this.clients.onSessionEnd(this.proxyHandle,s=>this.enqueue(()=>this.handleUpstreamEnd(s))),this.transcript.push(...t.systemItems);this.config=t;let r={};if(t.videoOut){let s=this.clients.openVideoOut;if(s){let i=await s.call(this.clients,t.model),a=i.onOutputFrame(u=>this.send(Nt(u))),c=i.onLaneError(u=>this.fail(u));this.detachVideoOut=()=>{a(),c()},r[Q]=$t(i.codec)}}this.send({setupComplete:r}),t.audioModality&&this.ensureAudioLane(),t.sessionResumption.enabled&&await this.mintResumptionUpdate()}ensureAudioLane(){if(this.audio)return this.audio;let n=this.config;if(!n)throw new d("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let t=this.clients.openAudio;if(!t)throw new d("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native rt-audio-in/rt-audio-out AudioBridge seam)",W);return this.audio=(async()=>{let o=await t.call(this.clients,n.model);return this.detachAudio=o.onOutputAudio(r=>this.send(qt(r))),o})(),this.audio.catch(o=>this.fail(o)),this.audio}appendAudio(n){this.ensureAudioLane().then(t=>t.appendInputAudio(n)).catch(t=>this.fail(t))}ensureVideoLane(){if(this.video)return this.video;let n=this.config;if(!n)throw new d("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let t=this.clients.openVideo;if(!t)throw new d("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",W);return this.video=Promise.resolve(t.call(this.clients,n.model)),this.video.catch(o=>this.fail(o)),this.video}appendVideoFrame(n){this.ensureVideoLane().then(t=>t.sendInputFrame(n)).catch(t=>this.fail(t))}onActivity(n,t){if(!t.activity.manualActivity)throw new d(`realtimeInput.${n} requires manual activity mode \u2014 set setup.realtimeInputConfig.automaticActivityDetection.disabled: true (automatic server-side activity detection is the declared platform follow-up)`);if(n==="activityStart"){if(this.activityOpen)throw new d("activityStart while an activity window is already open (send activityEnd first)");this.activityOpen=!0,t.activity.activityHandling==="START_OF_ACTIVITY_INTERRUPTS"&&this.activeTurn&&(this.activeTurn.interrupted=!0);return}if(!this.activityOpen)throw new d("activityEnd without an open activity window (send activityStart first)");this.activityOpen=!1}enqueue(n){this.queue=this.queue.then(n,()=>{}),this.queue=this.queue.catch(t=>this.fail(t))}async mintResumptionUpdate(){let n=this.config;if(!n?.sessionResumption.enabled||this.ws.readyState!==this.ws.OPEN)return;let t=await this.clients.sessionHandle(n.model);this.proxyHandle=t;let o=(0,Wt.randomUUID)();this.registry.store(o,{model:n.model,proxyHandle:t,transcript:[...this.transcript]}),this.send(Mt(o))}async handleUpstreamEnd(n){if(this.ws.readyState!==this.ws.OPEN||!this.config)return;this.unsubscribeEnd?.(),this.unsubscribeEnd=null;let t=null;try{t=await this.clients.sessionHandle(this.config.model)}catch{}if(t!==null&&t!==this.proxyHandle){this.proxyHandle=t,this.unsubscribeEnd=await this.clients.onSessionEnd(t,o=>this.enqueue(()=>this.handleUpstreamEnd(o))),this.config.sessionResumption.enabled&&await this.mintResumptionUpdate();return}this.send(Dt(n.timeLeftMs)),this.ws.close(W,pe(`upstream session ended: ${n.reason}`))}async runTurn(){let n=this.config;if(!n||this.ws.readyState!==this.ws.OPEN)return;let t={interrupted:!1};this.activeTurn=t;try{let o=await this.clients.createResponse({model:n.model,input:[...this.transcript],stream:!0,tools:n.tools,temperature:n.temperature,max_output_tokens:n.maxOutputTokens}),r,s=!1;for await(let c of o){if(t.interrupted)break;let u=c;if(u.type==="error")throw new d(`upstream error: ${JSON.stringify(u)}`,W);if(u.type==="response.completed"){r=u.response,s=!0;continue}let l=Ht(u);l&&this.send(l)}if(t.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!s)throw new d("upstream produced no response.completed event",W);let{messages:i,assistantItems:a}=Jt(r);this.transcript.push(...a);for(let c of i)this.send(c);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},_o=(e,n)=>{let t=e.headers["x-goog-api-key"];if(typeof t=="string")return t;let o=e.headers.authorization;return typeof o=="string"&&o.startsWith("Bearer ")?o.slice(7):n.searchParams.get("key")??void 0};function Yt(e,n){let t=new Kt.WebSocketServer({noServer:!0}),o=new Ve;e.on("upgrade",(r,s,i)=>{let a=new URL(r.url??"/","http://localhost");if(a.pathname!==Pt){s.write(`HTTP/1.1 404 Not Found\r
|
|
2
|
+
"use strict";var Yo=Object.create;var vn=Object.defineProperty;var Xo=Object.getOwnPropertyDescriptor;var Qo=Object.getOwnPropertyNames;var Zo=Object.getPrototypeOf,es=Object.prototype.hasOwnProperty;var ts=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of Qo(e))!es.call(t,o)&&o!==n&&vn(t,o,{get:()=>e[o],enumerable:!(r=Xo(e,o))||r.enumerable});return t};var xe=(t,e,n)=>(n=t!=null?Yo(Zo(t)):{},ts(e||!t||!t.__esModule?vn(n,"default",{value:t,enumerable:!0}):n,t));var ns=()=>typeof document>"u"?new URL(`file:${__filename}`).href:document.currentScript&&document.currentScript.tagName.toUpperCase()==="SCRIPT"?document.currentScript.src:new URL("main.js",document.baseURI).href,I=ns();var yn=require("child_process"),Z=require("fs"),_n=xe(require("path"),1),C=xe(require("process"),1);var Me=require("@urun-sh/core");async function bn(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 kn(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 xn(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 Rn(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 rs="llm-resp";async function*Ot(t,e){let n=`${rs}:${e}`,r=`resp_${e}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};let o=new Map;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 Rn(i);return}}}var os="llm",et=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(os).set({requests:{[e.request_id]:{payload:e,consumer_id:e.consumer_id,stream:e.stream}}})}sendResponses(e,n){let r=xn(e,n,this.consumerId);return this.write(r),Ot(this.session,n)}sendResponseCreate(e,n,r){let o=kn(e,n,r,this.consumerId);return this.write(o),Ot(this.session,r)}};var Sn=0;function ss(){return Sn+=1,`req_${Date.now().toString(36)}_${Sn.toString(36)}`}var tt=class{transport;constructor(e){this.transport=new et(e)}responses={create:async e=>{let n=ss(),r=this.transport.sendResponses(e,n);return Object.assign((async function*(){yield*r})(),{requestId:n})}}};var In="audio",Tt=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 On(t,e,n=1e4){let r=new Tt(e,{sampleRate:24e3}),o=await r.startOutbound();await t.stream(In).attach(o);let s=await is(t,n);return await r.startInbound(s),r}function is(t,e){let n=t.stream(In);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 An="rt-video-in",En=256e3;function Tn(t){let e=t.stream(An),n=e.emit;if(typeof n!="function")throw new Error(`enableSessionVideo: session.stream('${An}') 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>En)throw new Error(`enableSessionVideo: frame is ${r.byteLength} bytes \u2014 over the ${En}-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 as=24e3,Ue=480,us=111;async function ds(){let t=await import("opusscript"),e=t.default??t;return new e(as,1,2048)}async function nt(){let t=await import("werift"),e=await ds();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>=Ue;){let l=u.subarray(d,d+Ue);d+=Ue;let c=Buffer.from(l.buffer,l.byteOffset,l.byteLength),f=e.encode(c,Ue);o=o+1&65535,s=s+Ue>>>0;let p=new t.RtpHeader({version:2,payloadType:us,sequenceNumber:o,timestamp:s,ssrc:r,marker:!1}),_=new t.RtpPacket(p,f);n.writeRtp(_)}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}}}var rt=require("@urun-sh/core"),Pt="rt-video-out";var ls="h264";function cs(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 ps(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;cs(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 Pn="urun.videoOut.v2-ws";function Cn(t){let e=t.stream(Pt);if(typeof e.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${Pt}') 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: "${Pt}" delivered a non-BINARY \xA75 payload (${typeof u}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let d;try{d=ps(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:ls,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 $n=require("@urun-sh/core"),Ct=2;function fs(t){let e=t?.type;return e==="response.created"||e==="response.in_progress"}function ms(t){let e=t?.type;return e==="response.completed"||e==="error"}function Un(t){return async function(n,r){return(async function*(){for(let s=1;s<=Ct;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 c=await t.createStream(a,n);for await(let f of c){if(ms(f)&&(d=!0),fs(f)){if(s>1)continue}else l=!0;yield f}}catch(c){if(c instanceof $n.SessionFailedError&&(c.kind==="never-live"||c.kind==="create-failed"))throw c;if(s<Ct&&!l){await t.evict(i,a);continue}throw c}if(d)return;if(s<Ct&&!l){await t.evict(i,a);continue}return}})()}}var Y=class extends Error{name="ModelPriceError"},$t="per_million_input_tokens",Ut="per_million_output_tokens",Nn="model_id,variant,unit,price_usd,effective_at,expires_at",Ln={per_request:!0,per_minute:!0,per_output_unit:!0},gs=5e3;function hs(t,e){let n=typeof t=="number"?t:typeof t=="string"&&t.trim()!==""?Number(t):NaN;if(!Number.isFinite(n)||n<0)throw new Y(`${e}: price_usd is not a non-negative number (${JSON.stringify(t)})`);return n}function Mn(t,e,n){let r=typeof t=="string"?Date.parse(t):NaN;if(!Number.isFinite(r))throw new Y(`${e}: ${n} is not a parseable timestamp (${JSON.stringify(t)})`);return r}function ys(t,e,n){return t===null?null:Mn(t,e,n)}function _s(t,e){if(t===$t)return"input";if(t===Ut)return"output";if(typeof t=="string"&&Object.hasOwn(Ln,t))return null;throw new Y(`${e}: unit ${JSON.stringify(t)} is neither a per-token rate (${$t} | ${Ut}) nor a known GPU-time unit (${Object.keys(Ln).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 ws(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 vs(t){if(!Array.isArray(t))throw new Y(`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 Y(`${o} is not an object: ${JSON.stringify(r)}`);let s=r;for(let d of Nn.split(","))if(!Object.hasOwn(s,d))throw new Y(`${o} has no "${d}" column: ${JSON.stringify(s)}`);let i=_s(s.unit,o);if(i===null)continue;let a=s.model_id;if(typeof a!="string"||a==="")throw new Y(`${o} has no model_id (${JSON.stringify(s)})`);let u=s.variant;if(!(u===null||typeof u=="string"&&u!==""))throw new Y(`${o} has a non-string variant (${JSON.stringify(u)})`);e.push({model_id:a,variant:u,side:i,usd:hs(s.price_usd,o),effective_at:Mn(s.effective_at,o,"effective_at"),expires_at:ys(s.expires_at,o,"expires_at")})}return e}function Dn(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=[],s=[];for(let[a,u]of n){let d=u.get(""),l=(c,f)=>{let p=c&&c.length>0?c:d?.[f];if(!p||p.length===0)return null;let _=ws(p,f);return"reason"in _?_:_.rate.usd===0?{reason:`the ${f} 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`}:_.rate};for(let[c,f]of r.get(a)){let p=u.get(c),_=l(p?.input,"input"),R=l(p?.output,"output");if(_!==null&&"reason"in _){s.push({model_id:a,variant:f,reason:_.reason});continue}if(R!==null&&"reason"in R){s.push({model_id:a,variant:f,reason:R.reason});continue}_===null||R===null||o.push({model_id:a,variant:f,pricing:{input:_.usd,output:R.usd}})}}let i=(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(i),refusals:s.sort(i)}}function Le(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 ot={prices:[],refusals:[]};async function Bn(t){let e=t.fetchImpl??fetch,n=`${$t},${Ut}`,r=await e(`${t.catalogUrl}/model_prices?select=${Nn}&unit=in.(${n})`,{headers:{apikey:t.anonKey,Authorization:`Bearer ${t.anonKey}`},signal:AbortSignal.timeout(gs)});if(!r.ok)throw new Error(`model_prices fetch failed: ${r.status}`);return vs(await r.json())}var G=class extends Error{};function bs(t){if(t===null||typeof t!="object")return null;let e=t.shared;if(e==null)return null;if(typeof e!="object")throw new G(`catalog "shared" block is ${typeof e}, expected an object`);let{shared_org_id:n,endpoints:r}=e;if(typeof n!="string"||n==="")throw new G(`catalog "shared" block has no shared_org_id: ${JSON.stringify(e)}`);if(!Array.isArray(r))throw new G(`catalog "shared" block endpoints is not an array: ${JSON.stringify(e)}`);let o=r.map((s,i)=>{if(s===null||typeof s!="object")throw new G(`catalog shared endpoints[${i}] is not an object: ${JSON.stringify(s)}`);let a=s;for(let u of["model_id","variant","app_slug","function"])if(typeof a[u]!="string"||a[u].length===0)throw new G(`catalog shared endpoints[${i}] (${JSON.stringify(a)}) has no valid "${u}"`);return a});return o.length===0?null:{shared_org_id:n,endpoints:o}}function st(t){return[...t.endpoints].sort((e,n)=>e.model_id.localeCompare(n.model_id)||e.variant.localeCompare(n.variant))}function Fn(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 G(`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 jn(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 G(`catalog read failed: GET ${n} \u2192 ${r.status}`);return bs(await r.json())}function ne(t){return t.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}var ks={chat:!0,code:!0,agent:!0,vl:!0},qn={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function xs(t){let e=t.toLowerCase();return Object.hasOwn(qn,e)?qn[e]:null}var Rs=["context_length","max_model_len"];function Ss(t){let e=Rs.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 it(t){let e=t.map(Ss),n=e[0];return n==null?null:e.every(r=>r===n)?n:null}function As(t,e){let n=e.filter(o=>ne(`${o.model_id}-${o.variant}`)===t);if(n.length>0)return Lt(n);let r=e.filter(o=>ne(o.model_id)===t);return r.length>0?Lt(r):null}function Lt(t){let e=t[0];return t.every(r=>r.model_id===e.model_id&&r.variant===e.variant)?{row:t.length===1?e:Es(t),matches:t}:null}function Es(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 Vn(t){let e=t.updated_at?Date.parse(t.updated_at):NaN;return Number.isFinite(e)?Math.floor(e/1e3):0}function Nt(t,e,n){let r=As(t,e);if(!r)return{id:t,object:"model",created:0,owned_by:"urun"};let{row:o,matches:s}=r,i=it(s),a=Le(o.model_id,o.variant,n);return{id:t,object:"model",created:Vn(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}:{}}}function Gn(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 Hn(t){return{input:[{type:"prompt",unit:"token",cost_usd:Gn(t.input)}],output:[{type:"completion",unit:"token",cost_usd:Gn(t.output)}]}}function Is(t,e){if(t.length===0)return null;let n=Lt(t);if(!n)return null;let{row:r,matches:o}=n;if(!r.task||ks[r.task]!==!0)return null;let s=it(o),i=Le(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 s!==null&&(a.max_tokens={type:"integer",min:1,max:s,unit:"token"}),{schema_version:"2.4",id:`${r.model_id}:${r.variant}`,name:`${r.model_id}:${r.variant}`,created:Vn(r),quantization:xs(r.variant),description:r.description??"",hugging_face_id:r.hf_repo?r.hf_repo.split("@")[0]??r.hf_repo:"",input_modalities:[{type:"text",...s!==null?{supported_inputs:{max_context_length:{value:s,unit:"token"}}}:{},...i?{pricing:Hn(i).input}:{}},...r.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:a,...i?{pricing:Hn(i).output}:{}}]}}function Wn(t,e,n){let r=[],o=new Set;for(let s of t){let i=`${s.model_id}:${s.variant}`;if(o.has(i))continue;o.add(i);let a=Is(e.filter(u=>u.model_id===s.model_id&&u.variant===s.variant),n);a&&r.push(a)}return{data:r}}var Zn="https://api.urun.sh/v1",B=class extends Error{},J=class extends Error{},Dt="urs1.";function Os(t,e){return Dt+Buffer.from(JSON.stringify([t,e]),"utf8").toString("base64url")}function Ts(t){if(!t.startsWith(Dt))return null;try{let e=JSON.parse(Buffer.from(t.slice(Dt.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 er(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 Jn(t){if(t.length===0)return[];if(!t.every(n=>(n.task??void 0)==="image"))return[];let e=Cs(t);return e===null?[]:[e]}function Kn(t,e,n,r,o){if(!o)throw new G(`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 G(`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 G(`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 zn(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 G(`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 tr="shared:";function Ne(t){return t.sharedApp?tr+t.appSlug:t.appSlug}function Yn(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 Mt(t,e){let n=e.filter(s=>ne(`${s.model_id}-${s.variant}`)===t);if(n.length>0)return n;let r=e.filter(s=>ne(s.model_id)===t);return new Set(r.map(s=>s.variant)).size===1?r:[]}function Ps(t){let e=t.engine_args?.expects_image;return e==null?"edit":e===!1?"generation":e===!0?"edit":null}function Cs(t){let e=t.map(Ps),n=e[0];return n!=null&&e.every(r=>r===n)?n:null}var Xn={stt:"stt",tts:"tts"};function Qn(t){if(t.length===0)return null;let e=t[0].task??void 0;return e===void 0||Xn[e]===void 0?null:t.every(n=>(n.task??void 0)===e)?Xn[e]:null}var at=6e4,ut=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??at;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??at;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??at;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??at;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 Y)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
|
+
`),ot}let n=Dn(e,Date.now());for(let r of n.refusals)this.warn(`urun /v1/models: refusing to price ${r.model_id}:${r.variant??"*"} \u2014 ${r.reason}
|
|
4
|
+
`);return n}warn(e){(this.opts.warn??(r=>console.warn(r.trimEnd())))(e)}servable(e){return e.filter(n=>n.function_name===this.opts.fnName&&n.deployment_status==="active").map(n=>n.app_slug)}async availableIds(){let e=this.opts.defaultApp,n=new Set(e===null?[]:[e]);if(this.opts.listApps)for(let o of this.servable(await this.deployedApps()))n.add(o);for(let o of this.pool.keys())o.startsWith(tr)||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 B(e===""?`no "model" was given and this endpoint has no default model \u2014 name one of your org's deployed "${this.opts.fnName}" apps; available models: ${r}`:`model "${e}" does not name a deployed "${this.opts.fnName}" app in your org, and this endpoint has no default model to fall back to; available models: ${r}`)}async resolveTarget(e){let n=(e??"").trim(),r=this.opts.defaultApp;if(!n||n==="urun")return r===null?this.noDefaultApp(n):{appSlug:r};let o=ne(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 B(`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 B(`model "${n}" maps to app "${u.app_slug}", which is not an active "${this.opts.fnName}" app \u2014 deploy it with \`urun serve ${n}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.shared){let s=await this.sharedBlock();if(s){let i=Fn(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}}}if(this.opts.listCatalog){let i=(await this.opts.listCatalog()).find(a=>n===a.model_id||n===`${a.model_id}:${a.variant}`||o===ne(a.model_id)||o===ne(`${a.model_id}-${a.variant}`));if(i)throw new B(`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=Yn(await this.resolveTarget(e));return n.kind==="catalog"?{model_id:n.model_id,variant:n.variant}:null}async servingAppSlugFor(e){return(await this.resolveTarget(e)).appSlug}async resolveApp(e){return Ne(await this.resolveTarget(e))}async sessionFor(e){let n=await this.resolveTarget(e),r=Ne(n),o=this.pool.get(r);return o||(o=Promise.resolve(this.opts.openSession(n)),this.pool.set(r,o),o.catch(()=>this.pool.delete(r))),{app:r,entry:await o,identity:Yn(n)}}keyOf(e){let n=this.opts.sessionKey;if(!n)throw new Error("ModelRouter: sessionKey is not configured \u2014 the session-identity seam (handleFor/sessionForHandle) is unavailable on this router");let r=n(e);if(typeof r!="string"||r.length===0)throw new Error("ModelRouter: sessionKey returned an empty identity for a pooled session");return r}async handleFor(e){let{app:n,entry:r}=await this.sessionFor(e);return{app:n,handle:Os(n,this.keyOf(r))}}async sessionForHandle(e){let n=Ts(e);if(!n)throw new J(`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 J(`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 J(`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=await this.availableIds(),[n,r]=await Promise.all([this.catalogRows().then(d=>({ok:!0,rows:d}),()=>({ok:!1,rows:[]})),this.pricesForListing()]),o=n.rows,s=n.ok?r:ot;n.ok||this.warn(`urun /v1/models: the model catalog could not be read \u2014 entries fall back to the bare OpenAI fields AND no model is priced on either lane (pricing follows catalog identity, so a half-priced listing would flicker on Hugging Face)
|
|
5
|
+
`);let i=await this.sharedBlock(),u=(i?st(i):[]).map(d=>{let l={id:`${d.model_id}:${d.variant}`,object:"model",created:0,owned_by:"urun",name:`${d.model_id}:${d.variant}`};d.gpu_spec&&(l.gpu_spec=d.gpu_spec),typeof d.warm=="number"&&(l.warm=d.warm);let c=o.filter(_=>_.model_id===d.model_id&&_.variant===d.variant),f=it(c);f!==null&&(l.context_length=f);let p=Le(d.model_id,d.variant,s);return p!==null&&(l.pricing=p),l});return{object:"list",data:[...e.map(d=>Nt(d,o,s)),...u]}}async openRouterModels(){let[e,n]=await Promise.all([this.catalogRows(),this.pricesForListing()]),r=await this.sharedBlock();return Wn(r?st(r):[],e,n)}async supportsImages(e,n){let r=await this.resolveTarget(e),o=await this.catalogRows();if(r.sharedApp){let s=Kn(String(e),r.modelId,r.variant,o,this.opts.listCatalog!==null);return zn(`${r.modelId}:${r.variant}`,s).includes(n)}return Jn(Mt(r.appSlug,o)).includes(n)}async supportsAudio(e){let n=await this.resolveTarget(e),r=await this.catalogRows();return n.sharedApp?Qn(r.filter(o=>o.model_id===n.modelId&&o.variant===n.variant))!==null:Qn(Mt(n.appSlug,r))!==null}async imageModelList(){let e=await this.catalogRows(),n=ot,r=[];for(let s of await this.availableIds()){let i=Jn(Mt(s,e));i.length!==0&&r.push({...Nt(s,e,n),image_modes:i})}let o=await this.sharedBlock();if(o)for(let s of st(o)){let i=Kn(`${s.model_id}:${s.variant}`,s.model_id,s.variant,e,this.opts.listCatalog!==null),a=zn(`${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 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("; ")}`)}};var $s=3,Us="llm",Ls=new Set(["paused","error"]);function nr(t,e){let n=t.doc(Us),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 c of[...a])c(l);a.clear()}};return r&&u.push(n.onConnectionState(l=>{i&&(l.consecutiveFailures<$s||d(new J(`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&&Ls.has(l.name)&&d(new J(`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*Bt(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 rr(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 Ns(t,e){let{baseUrl:n,orgId:r,auth:o,subject:s}=t,i=e.fnName??t.fnName,a=Ne(e),u=t.sessionStartOptions?.(a),d=e.sharedApp?{sharedApp:!0,...u}:u,l=_=>({session:_,responses:new tt(_),gone:nr(_,a)});if(o.lane==="jwt"){let _=(0,Me.App)(e.appSlug,{baseUrl:n,orgId:r,jwt:o.jwt});return l(rr(_,e.appSlug,i,d))}let{apiKey:c,gatewayUrl:f}=o,p=(0,Me.App)(e.appSlug,{baseUrl:n,orgId:r,getAccessToken:async()=>(await(0,Me.createClientToken)(c,{baseUrl:f,expiresIn:300,allowedFunctions:[`${e.appSlug}/${i}`],...s===void 0?{}:{subject:s},...e.sharedApp?{sharedApp:!0}:{}})).token});return l(rr(p,e.appSlug,i,d))}function Ms(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 Ds=new Set(["error","paused"]);function or(t){let{auth:e,catalog:n}=t,r=null,o=i=>{let a=Ne(i),u=Ns(t,i);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 ut({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"?()=>er({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,listCatalog:n?()=>bn(n):null,listPrices:n?()=>Bn(n):null,shared:e.lane==="api-key"?()=>jn({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,sessionKey:Ms});return r=s,t.defaultApp!==null&&s.seed(t.defaultApp,o({appSlug:t.defaultApp})),s}async function Ft(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 sr(t,e=nt){let n=new WeakMap,r=new WeakMap,o=new WeakMap;return{createResponse:Un({sessionFor:s=>t.sessionFor(s),sessionIdOf:s=>{let i=s.session.id;return typeof i=="string"&&i.length>0?i:null},evict:(s,i)=>t.evict(s,i),createStream:async(s,i)=>(await Ft(s.session),Bt(await s.responses.responses.create(i),s.gone))}),listModels:()=>t.modelList(),openRouterModels:()=>t.openRouterModels(),supportsImages:(s,i)=>t.supportsImages(s,i),listImageModels:()=>t.imageModelList(),catalogIdentity:s=>t.catalogIdentityFor(s),servingAppSlug:async s=>await t.servingAppSlugFor(s),supportsAudio:s=>t.supportsAudio(s),openAudio:async s=>{let{entry:i}=await t.sessionFor(s),a=n.get(i);return a||(a=(async()=>(await Ft(i.session),On(i.session,await e())))(),n.set(i,a),a.catch(()=>n.delete(i))),a},openVideo:async s=>{let{entry:i}=await t.sessionFor(s),a=r.get(i);return a||(a=Tn(i.session),r.set(i,a)),a},openVideoOut:async s=>{let{entry:i}=await t.sessionFor(s),a=o.get(i);return a||(a=Cn(i.session),o.set(i,a)),a},sessionHandle:async s=>(await t.handleFor(s)).handle,createResponseOn:async(s,i)=>{let{entry:a}=await t.sessionForHandle(s);return await Ft(a.session),Bt(await a.responses.responses.create(i),a.gone)},onSessionEnd:async(s,i)=>{let{entry:a}=await t.sessionForHandle(s),u=a.session;if(typeof u.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let d=!1;return u.onPhase(l=>{if(d||!Ds.has(l.name))return;d=!0;let c=u.endsAt??null;i({timeLeftMs:c?Math.max(0,c.getTime()-Date.now()):null,reason:l.reason??`session ${l.name}`})})}}}var Fo=require("http");var Rr=require("crypto"),Sr=require("ws");var ar="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",Fs=1007,ur=1008,re=1011,ct=1008,g=class extends Error{constructor(n,r=Fs){super(n);this.closeCode=r}closeCode},M=t=>typeof t=="object"&&t!==null&&!Array.isArray(t)?t:null,dt=["setup","clientContent","realtimeInput","toolResponse"];function dr(t){let e;try{e=JSON.parse(t)}catch{throw new g("client message is not valid JSON")}let n=M(e);if(!n)throw new g("client message must be a JSON object");let r=dt.filter(a=>n[a]!==void 0),o=Object.keys(n).filter(a=>!dt.includes(a));if(o.length>0)throw new g(`unknown client message field(s) ${JSON.stringify(o)} \u2014 expected exactly one of ${dt.join(", ")}`);if(r.length!==1)throw new g(`client message must contain exactly one of ${dt.join(", ")} (got ${r.length})`);let s=r[0],i=M(n[s]);if(!i)throw new g(`"${s}" must be a JSON object`);return{kind:s,[s]:i}}function lr(t,e){if(typeof t=="string")return t;let n=M(t),r=n?n.parts:void 0;if(!Array.isArray(r))throw new g(`${e}: expected a Content with a parts array`);let o=[];for(let s of r){let i=M(s);if(i&&typeof i.text=="string"){o.push(i.text);continue}throw i&&(i.inlineData!==void 0||i.fileData!==void 0)?new g(`${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 g(`${e}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new g(`${e}: unsupported part ${JSON.stringify(s)}`)}return o.join(`
|
|
6
|
+
`)}var js={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},he="urun.videoOut";function cr(t){return{framing:Pn,codec:t}}function pr(t){let{seq:e,ptsUs:n,codec:r,key:o}=t.header;return{[he]:{v:1,seq:e,ptsUs:n,codec:r,key:o,data:Buffer.from(t.au).toString("base64")}}}var qs={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},Gs=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",he],ir=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function Hs(t){let e=M(t);if(!e)throw new g("setup.realtimeInputConfig must be an object");for(let o of Object.keys(e))if(!(o==="automaticActivityDetection"||o==="activityHandling"))throw new g(o==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${o}"`);let n=M(e.automaticActivityDetection);if(e.automaticActivityDetection!==void 0&&!n)throw new g("realtimeInputConfig.automaticActivityDetection must be an object");for(let o of Object.keys(n??{}))if(o!=="disabled")throw new g(`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 g("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(!ir.includes(e.activityHandling))throw new g(`unsupported activityHandling ${JSON.stringify(e.activityHandling)} \u2014 expected one of ${ir.join(", ")}`);r=e.activityHandling}return{manualActivity:!0,activityHandling:r}}var Vs=["temperature","maxOutputTokens","responseModalities"];function fr(t){for(let[c,f]of Object.entries(qs))if(t[c]!==void 0)throw new g(f);for(let c of Object.keys(t))if(!Gs.includes(c))throw new g(`unsupported setup field "${c}"`);if(typeof t.model!="string"||t.model.length===0)throw new g('setup.model is required (format "models/{model}")');let e=t.model.replace(/^models\//,""),n,r,o=!1,s=M(t.generationConfig);if(t.generationConfig!==void 0&&!s)throw new g("setup.generationConfig must be an object");if(s){for(let c of Object.keys(s))if(!Vs.includes(c))throw new g(`unsupported generationConfig field "${c}"`);if(s.temperature!==void 0){if(typeof s.temperature!="number")throw new g("generationConfig.temperature must be a number");n=s.temperature}if(s.maxOutputTokens!==void 0){if(typeof s.maxOutputTokens!="number")throw new g("generationConfig.maxOutputTokens must be a number");r=s.maxOutputTokens}if(s.responseModalities!==void 0){let c=Array.isArray(s.responseModalities)?s.responseModalities:[s.responseModalities];for(let f of c){let p=String(f).toUpperCase();if(p==="AUDIO"){o=!0;continue}if(p!=="TEXT")throw new g(`responseModalities ${JSON.stringify(f)} 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:lr(t.systemInstruction,"setup.systemInstruction")});let a;if(t.tools!==void 0){if(!Array.isArray(t.tools))throw new g("setup.tools must be an array");a=[];for(let c of t.tools){let f=M(c);if(!f)throw new g("setup.tools entries must be objects");for(let p of Object.keys(f))if(p!=="functionDeclarations")throw new g(`unsupported tool "${p}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(f.functionDeclarations))throw new g("tool.functionDeclarations must be an array");for(let p of f.functionDeclarations){let _=M(p);if(!_||typeof _.name!="string")throw new g("functionDeclarations entries require a string name");a.push({type:"function",name:_.name,description:_.description,parameters:_.parameters})}}a.length===0&&(a=void 0)}let u={enabled:!1,handle:null};if(t.sessionResumption!==void 0){let c=M(t.sessionResumption);if(!c)throw new g("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let f of Object.keys(c))if(f!=="handle")throw new g(`unsupported sessionResumption field "${f}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(c.handle!==void 0&&(typeof c.handle!="string"||c.handle.length===0))throw new g("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?js:Hs(t.realtimeInputConfig),l=!1;if(t[he]!==void 0){if(!M(t[he]))throw new g(`setup["${he}"] 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 mr(t){return{sessionResumptionUpdate:{newHandle:t,resumable:!0}}}function gr(t){let e=Math.max(0,t??0)/1e3;return{goAway:{timeLeft:`${Math.round(e*1e3)/1e3}s`}}}function hr(t){for(let r of Object.keys(t))if(r!=="turns"&&r!=="turnComplete")throw new g(`unsupported clientContent field "${r}"`);let e=t.turns===void 0?[]:t.turns;if(!Array.isArray(e))throw new g("clientContent.turns must be an array");let n=[];for(let r of e){let o=M(r);if(!o)throw new g("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 g(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(o.role)})`);n.push({role:s==="model"?"assistant":"user",content:lr(o,"clientContent.turns")})}return{items:n,turnComplete:t.turnComplete===!0}}var lt=16e3,yr=24e3,Ws=`audio/pcm;rate=${yr}`,_r=Buffer.alloc(960).toString("base64");function Js(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 Ks(t){let e=M(t);if(!e)throw new g("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(e.mimeType))throw new g(`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 g("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 g("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(n)}function zs(t){let e=M(t);if(!e)throw new g("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(e.mimeType))throw new g(`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])!==lt)throw new g(`realtimeInput.audio.mimeType declares rate=${n[1]} \u2014 the Live spec input format is ${lt} Hz (audio/pcm;rate=${lt})`);if(typeof e.data!="string"||e.data.length===0)throw new g("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 g(`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=Js(o,lt,yr);return Buffer.from(s.buffer,s.byteOffset,s.byteLength).toString("base64")}function wr(t){if(t.mediaChunks!==void 0)throw new g('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 g("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let o=t.activityStart!==void 0?"activityStart":"activityEnd",s=M(t[o]);if(!s||Object.keys(s).length!==0)throw new g(`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 g(`unsupported realtimeInput field "${r}"`);let n=e.filter(r=>t[r]!==void 0);if(n.length!==1)throw new g(`realtimeInput must carry exactly one of ${e.join(", ")} (got ${n.length})`);if(t.audio!==void 0)return{kind:"audio",base64Pcm24k:zs(t.audio)};if(t.video!==void 0)return{kind:"video",jpegFrame:Ks(t.video)};if(t.audioStreamEnd!==void 0){if(t.audioStreamEnd!==!0)throw new g("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof t.text!="string"||t.text.length===0)throw new g("realtimeInput.text must be a non-empty string");return{kind:"text",text:t.text}}function vr(t){for(let e of Object.keys(t))if(e!=="functionResponses")throw new g(`unsupported toolResponse field "${e}"`);if(!Array.isArray(t.functionResponses)||t.functionResponses.length===0)throw new g("toolResponse.functionResponses must be a non-empty array");return t.functionResponses.map(e=>{let n=M(e);if(!n||typeof n.id!="string"||n.id.length===0)throw new g("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 br(t){return t.type!=="response.output_text.delta"||typeof t.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:t.delta}]}}}}function kr(t){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:Ws,data:t}}]}}}}function xr(t){let e=M(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=M(d);if(!(l?.type!=="message"||!Array.isArray(l.content)))for(let c of l.content){let f=M(c);f?.type==="output_text"&&typeof f.text=="string"&&(s+=f.text)}}s&&o.push({role:"assistant",content:s});for(let d of n){let l=M(d);if(l?.type!=="function_call")continue;if(typeof l.call_id!="string"||typeof l.name!="string")throw new g(`upstream function_call item is missing call_id/name: ${JSON.stringify(d)}`,re);let c=typeof l.arguments=="string"&&l.arguments.length>0?l.arguments:"{}",f;try{f=JSON.parse(c)}catch{throw new g(`upstream function_call arguments are not valid JSON: ${c}`,re)}r.push({id:l.call_id,name:l.name,args:f}),o.push({type:"function_call",call_id:l.call_id,name:l.name,arguments:c})}let i=[];r.length>0&&i.push({toolCall:{functionCalls:r}}),i.push({serverContent:{generationComplete:!0}});let a=M(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}}var Ys=256,jt=class{snapshots=new Map;store(e,n,r){for(this.snapshots.set(`${e}\0${n}`,r);this.snapshots.size>Ys;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(e,n){return this.snapshots.get(`${e}\0${n}`)}},Xs="local",De=t=>t.slice(0,120),qt=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 B){this.ws.close(ur,De(e.message));return}if(e instanceof J){this.ws.close(ct,De(e.message));return}if(e instanceof g){this.ws.close(e.closeCode,De(e.message));return}this.ws.close(re,De(String(e instanceof Error?e.message:e)))}onMessage(e){try{let n=dr(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 g(`"${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}=hr(n.clientContent);this.transcript.push(...o),s&&this.enqueue(()=>this.runTurn());return}if(n.kind==="realtimeInput"){let o=wr(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:_r);return}this.transcript.push(...vr(n.toolResponse)),this.enqueue(()=>this.runTurn())}catch(n){this.fail(n)}}async handleSetup(e){if(this.config)throw new g("setup may only be sent once, as the first client message");let n=fr(e),r=n.sessionResumption.handle;if(r!==null){let s=this.registry.get(this.scope,r);if(!s)throw new g("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",ct);if(s.model!==n.model)throw new g(`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 g("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 g(`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`,ct);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(pr(d))),u=i.onLaneError(d=>this.fail(d));this.detachVideoOut=()=>{a(),u()},o[he]=cr(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 g("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openAudio;if(!n)throw new g("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native AudioBridge seam over the canonical `audio` lane)",re);return this.audio=(async()=>{let r=await n.call(this.clients,e.model);return this.detachAudio=r.onOutputAudio(o=>this.send(kr(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 g("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openVideo;if(!n)throw new g("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",re);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 g(`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 g("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 g("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,Rr.randomUUID)();this.registry.store(this.scope,r,{model:e.model,proxyHandle:n,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(r),this.send(mr(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(gr(e.timeLeftMs)),this.ws.close(re,De(`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 g(`upstream error: ${JSON.stringify(d)}`,re);if(d.type==="response.completed"){o=d.response,s=!0;continue}let l=br(d);l&&this.send(l)}if(n.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!s)throw new g("upstream produced no response.completed event",re);let{messages:i,assistantItems:a}=xr(o);this.transcript.push(...a);for(let u of i)this.send(u);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},ye=class extends Error{constructor(n,r){super(r);this.status=n;this.name="UpgradeAuthError"}status};function Qs(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 ye(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 ye(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 ye(401,"missing credentials: send the org API key in the x-goog-api-key header or as Authorization: Bearer");return a??""}function Ar(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 Sr.WebSocketServer({noServer:!0}),o=new jt;t.on("upgrade",(i,a,u)=>{let d=new URL(i.url??"/","http://localhost");d.pathname===ar&&(a.on("error",()=>{}),(async()=>{let l=Qs(i,d,n),c;if(e.authenticate)c=await e.authenticate(l,i);else{if(e.apiKey!==void 0&&l!==e.apiKey)throw new ye(401,"invalid local proxy api key");c={clients:e.clients,scope:Xs}}r.handleUpgrade(i,a,u,f=>{new qt(f,c,o)})})().catch(l=>{let c=502;(l instanceof ye||typeof l=="object"&&l!==null&&"status"in l&&typeof l.status=="number")&&(c=l.status);let f=c===401?"Unauthorized":c===403?"Forbidden":c===502?"Bad Gateway":"Internal Server Error",p=l instanceof Error?l.message:String(l);a.destroyed||(a.write(`HTTP/1.1 ${c} ${f}\r
|
|
4
7
|
Connection: close\r
|
|
5
8
|
\r
|
|
6
|
-
|
|
9
|
+
${p}\r
|
|
10
|
+
`),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)}var pe=require("ws");var Er=require("crypto"),Ir="Inference-Id";function Be(){return(0,Er.randomUUID)()}function Gt(t){return t==="/v1"||t.startsWith("/v1/")}function Or(t,e){if(!Gt(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 Fe(t){return`resp_${t}`}function Tr(t){return`chatcmpl-${t}`}function Pr(t){return`msg_${t}`}function oe(t){return e=>{t.sessionId=e.sessionId,t.catalog=e.identity}}function Cr(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 $r(t){return`${JSON.stringify(t)}
|
|
11
|
+
`}var Zs=256,ei=128;function se(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,ei)}var _e=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>=Zs?"(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}}}},ti=256;function ni(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 we=class extends Error{},Re=class{conversations=new Map;thread(e,n){let r=ni(n);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new we(`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<=ti)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},Ur=new WeakMap;function je(t){let e=Ur.get(t);return e||(e=new Re,Ur.set(t,e)),e}function Se(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function ie(t){return t.find(e=>e.type==="response.completed")?.response??null}function Ae(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 ce=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}))}},ri=["<tool_call>","<function="];function oi(t){return ri.find(e=>t.includes(e))}function ve(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 be(t,e,n){if(!(!t||e>0))return oi(n)}function Ht(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 si(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 Vt(t,e){let n=si(t);return n.length>0?n:e?[{role:"assistant",content:e}]:[]}var X=class extends Error{constructor(n,r,o=400,s){super(n);this.code=r;this.status=o;this.param=s}code;status;param};function Ee(t,e){try{return t.thread(e.previous_response_id,e.input)}catch(n){throw n instanceof we?new X(n.message,"previous_response_not_found",400,"previous_response_id"):new X(String(n instanceof Error?n.message:n),"invalid_input")}}function Lr(t){let e=t.text;if(e!==null&&typeof e=="object"){let n=e.format;if(n!==void 0)return n}}async function Wt(t,e,n,r,o){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new X("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}:{},...Lr(e)!==void 0?{response_format:Lr(e)}:{}},o)}async function qe(t){let{body:e,clients:n,store:r,stats:o}=t,s=Ee(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=se(e);o.model(d);let l=Array.isArray(e.tools)&&e.tools.length>0,c=t.turn,f=await Wt(n,e,s,!0,c?oe(c):void 0);return{responseId:i,events:ii({events:f,conversation:s,responseId:i,modelKey:d,requestHadTools:l,remember:a,persist:u,store:r,stats:o,turn:t.turn})}}async function*ii(t){let{events:e,conversation:n,responseId:r,modelKey:o,requestHadTools:s,remember:i,persist:a,store:u,stats:d}=t,l=[],c=A=>A.response&&typeof A.response=="object"?{...A,response:{...A.response,id:r}}:A,f=`item_${r.slice(5)}`,p=`rs_${r.slice(5)}`,_=!1,R=!1,h=0;function*w(){_||(_=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let m=!1,v=0,E="";function*x(){m&&(m=!1,yield{type:"response.reasoning_text.done",item_id:p,output_index:v,content_index:0,text:E},yield{type:"response.output_item.done",output_index:v,item:{id:p,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:E}],status:"completed"}})}let b=!1,y=0,P="";function*$(A){b&&(b=!1,yield{type:"response.output_text.done",item_id:f,output_index:y,content_index:0,text:P},yield{type:"response.content_part.done",item_id:f,output_index:y,content_index:0,part:{type:"output_text",text:P}},yield{type:"response.output_item.done",output_index:y,item:{id:f,type:"message",role:"assistant",status:A,content:[{type:"output_text",text:P}]}})}let N=new ce,O=null,Q=-1;function*K(A){if(!O)return;let L=O;O=null,yield{type:"response.function_call_arguments.done",item_id:L.fcId,output_index:L.outputIndex,arguments:L.args},yield{type:"response.output_item.done",output_index:L.outputIndex,item:{id:L.fcId,type:"function_call",call_id:L.callId??L.fcId,name:L.name??"",arguments:L.args,status:A}}}let q=!1;for await(let A of d.track(e,o,t.turn)){l.push(A);let L=String(A.type??"");if(L==="response.created"||L==="response.in_progress"){_=!0,yield c(A);continue}if(L==="response.output_item.added"){R=!0,yield c(A);continue}if(L==="response.reasoning_text.delta"&&!R){yield*w(),m||(m=!0,v=h++,yield{type:"response.output_item.added",output_index:v,item:{id:p,type:"reasoning",summary:[],content:[],status:"in_progress"}}),E+=String(A.delta??""),yield{type:"response.reasoning_text.delta",item_id:p,output_index:v,content_index:0,delta:A.delta};continue}if(L==="response.output_text.delta"&&!R){yield*w(),yield*x(),b||(b=!0,y=h++,yield{type:"response.output_item.added",output_index:y,item:{id:f,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:f,output_index:y,content_index:0,part:{type:"output_text",text:""}}),P+=String(A.delta??""),yield{type:L,item_id:f,output_index:y,content_index:0,delta:A.delta};continue}let j=R?null:Ae(A);if(j){if(yield*w(),yield*x(),yield*$("completed"),N.add(j),j.tool_index!==Q){yield*K("completed"),Q=j.tool_index;let z=h++;O={fcId:`fc_${r.slice(5)}_${j.tool_index}`,outputIndex:z,callId:j.call_id,name:j.name,args:""},yield{type:"response.output_item.added",output_index:z,item:{id:O.fcId,type:"function_call",call_id:O.callId??O.fcId,name:O.name??"",arguments:"",status:"in_progress"}}}O&&(j.call_id&&(O.callId=j.call_id),j.name&&(O.name=j.name),O.args+=j.delta,yield{type:"response.function_call_arguments.delta",item_id:O.fcId,output_index:O.outputIndex,delta:j.delta});continue}if(L==="response.completed"&&!R){let z=Se(A.response),me=be(s,N.size+z.length,P);if(me){q=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:ve(o,me)}};break}if(yield*x(),yield*$("completed"),yield*K("completed"),N.size===0)for(let ge of z){let le=h++,te=ge.id??ge.call_id??`fc_${le}`,$e=ge.arguments??"",ke={id:te,type:"function_call",call_id:ge.call_id??te,name:ge.name??""};yield{type:"response.output_item.added",output_index:le,item:{...ke,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:te,output_index:le,delta:$e},yield{type:"response.function_call_arguments.done",item_id:te,output_index:le,arguments:$e},yield{type:"response.output_item.done",output_index:le,item:{...ke,arguments:$e,status:"completed"}}}yield c(A);continue}yield c(A)}if(yield*x(),yield*$("incomplete"),yield*K("incomplete"),i&&!q){let A=ie(l);u.remember(r,n,Vt(A,Ht(l,A)),a)}}var ai="/v1/responses",ui=64*1024*1024,di=8*1024*1024,li=16,Nr=32,Mr=3600*1e3,ci=/^[A-Za-z0-9._-]{1,256}$/;function H(t,e,n){return{type:"error",status:t,...n!==void 0?{stream_id:n}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var pi=H(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 zt(){let t,e;return{promise:new Promise((r,o)=>{t=r,e=o}),resolve:t,reject:e}}async function fi(t){for(;t.bufferedAmount>di&&t.readyState===pe.WebSocket.OPEN;){let{promise:e,resolve:n}=zt();setTimeout(n,5),await e}}var Jt=class extends Re{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 we))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)}},Kt=class{constructor(e,n,r,o){this.ws=e;this.clients=n;this.stats=r;this.statusOf=o;this.store=new Jt(je(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(H(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${Mr/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},Mr).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(H(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(H(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(H(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(H(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(H(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(H(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"||!ci.test(o.stream_id)){this.sendFrame(pi);return}if(s=o.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=Nr){this.sendFrame(H(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${Nr}). 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 qe({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:Fe(Be())})}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!==pe.WebSocket.OPEN)break;this.send(i,n),await fi(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=Fe(Be());try{let o=Ee(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 X)return H(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},n);if(e instanceof B)return H(404,{type:"invalid_request_error",code:"model_not_found",message:r},n);if(e instanceof J)return H(503,{type:"upstream_error",code:"session_gone",message:r},n);let o=this.statusOf?.(e)??null;return o?H(o.status,{type:o.openaiType,message:r},n):H(500,{type:"upstream_error",message:r},n)}async acquire(){for(;this.active>=li;){let{promise:e,resolve:n}=zt();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!==pe.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!==pe.WebSocket.OPEN||this.ws.send(JSON.stringify(e))}};function pt(t,e,n){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${n}\r
|
|
12
|
+
`;t.end(`HTTP/1.1 ${e} ${r}\r
|
|
7
13
|
Connection: close\r
|
|
14
|
+
Content-Type: text/plain; charset=utf-8\r
|
|
15
|
+
Content-Length: ${Buffer.byteLength(o)}\r
|
|
8
16
|
\r
|
|
9
|
-
invalid local proxy api key`),s.destroy();return}t.handleUpgrade(r,s,i,c=>{new Be(c,n.clients,o)})})}var He=class e{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(n,t,o){this.requests[n]=(this.requests[n]??0)+1,this.localInBytes+=t,this.naiveInBytes+=o}modelEntry(n){let t=this.models[n];if(t)return t;let o=Object.keys(this.models).length>=vo?"(other)":n;return this.models[o]??={requests:0,tokens_out:0}}model(n){this.modelEntry(n).requests+=1}track(n,t){let o=this,r=performance.now(),s=null,i=0;return(async function*(){for await(let c of n)c.type==="response.output_text.delta"&&typeof c.delta=="string"&&(s===null&&(s=performance.now(),o.ttftMs.push(s-r),o.ttftMs.length>512&&o.ttftMs.shift()),i+=1,o.tokensOut+=1,o.charsOut+=c.delta.length,t!==void 0&&(o.modelEntry(t).tokens_out+=1)),yield c;let a=(performance.now()-(s??r))/1e3;i>0&&a>0&&(o.tokRates.push(i/a),o.tokRates.length>512&&o.tokRates.shift())})()}static p50(n){if(!n.length)return null;let t=[...n].sort((o,r)=>o-r);return t[Math.floor(t.length/2)]}snapshot(){let n=Object.values(this.requests).reduce((t,o)=>t+o,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:e.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:e.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:n},persistent_connections:1,history_bytes_saved:Math.max(0,this.naiveInBytes-this.localInBytes),history_bytes_saved_pct:this.naiveInBytes>0?Math.round((1-this.localInBytes/this.naiveInBytes)*1e3)/10:null}}}};function Je(e){e.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function B(e,n,t){e.writeHead(n,{"content-type":"application/json"}),e.end(JSON.stringify(t))}function K(e,n,t,o="invalid_request_error"){B(e,n,{error:{message:t,type:o}})}function H(e,n,t,o){B(e,n,{type:"error",error:{type:t,message:o}})}function We(e,n,t){if(n==="anthropic"){B(e,404,{type:"error",error:{type:"not_found_error",message:t.message}});return}B(e,404,{error:{message:t.message,type:"invalid_request_error",code:"model_not_found"}})}var vo=256,ko=128;function Ke(e){return typeof e.model!="string"||!e.model.trim()?"(default)":e.model.trim().slice(0,ko)}var zt=64*1024*1024,Se=class extends Error{};async function xo(e){let n=[],t=0;for await(let r of e){if(t+=r.length,t>zt)throw new Se(`request body exceeds ${zt} bytes`);n.push(r)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function oe(e,n,t,o){return{id:e,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:n,choices:[{index:0,delta:t,finish_reason:o}]}}var bo=256;function So(e){if(typeof e=="string")return[{role:"user",content:e}];if(Array.isArray(e))return e;throw new Error("responses input must be a string or an array of messages/items")}var qe=class{conversations=new Map;thread(n,t){let o=So(t);if(n==null)return o;let r=this.conversations.get(String(n));if(!r)throw new Error(`unknown previous_response_id ${String(n)} (proxy restarts drop stored responses)`);return[...r,...o]}remember(n,t,o){this.conversations.set(n,[...t,{role:"assistant",content:o}]);for(let r of this.conversations.keys()){if(this.conversations.size<=bo)break;this.conversations.delete(r)}}};function fe(e){let n=e?.output;return Array.isArray(n)?n.filter(t=>t.type==="function_call"):[]}function Z(e){return e.find(n=>n.type==="response.completed")?.response??null}function Re(e){let n=e?.usage;if(!n||typeof n!="object")return null;let t=i=>typeof i=="number"&&Number.isFinite(i)&&i>=0?i:0,o=t(n.input_tokens),r=t(n.output_tokens);if(o===0&&r===0)return null;let s=t(n.total_tokens)||o+r;return{input_tokens:o,output_tokens:r,total_tokens:s}}function Xt(e){let n=Re(e);return n?{prompt_tokens:n.input_tokens,completion_tokens:n.output_tokens,total_tokens:n.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function en(e){let n=e?.output;if(!Array.isArray(n))return"";let t="";for(let o of n)if(!(o.type!=="reasoning"||!Array.isArray(o.content)))for(let r of o.content)r.type==="reasoning_text"&&(t+=String(r.text??""));return t}function me(e){return e.type!=="response.function_call_arguments.delta"||typeof e.tool_index!="number"?null:{tool_index:e.tool_index,call_id:typeof e.call_id=="string"?e.call_id:void 0,name:typeof e.name=="string"?e.name:void 0,delta:typeof e.delta=="string"?e.delta:""}}var ee=class{calls=new Map;add(n){let t=this.calls.get(n.tool_index),o=!t;return t||(t={args:""},this.calls.set(n.tool_index,t)),n.call_id&&(t.id=n.call_id),n.name&&(t.name=n.name),t.args+=n.delta,{call:t,isNew:o}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([n,t])=>({type:"function_call",call_id:t.id??`call_${n}`,name:t.name??"",arguments:t.args}))}},Ro=["<tool_call>","<function="];function Ao(e){return Ro.find(n=>e.includes(n))}function re(e,n){return`serving row emitted a prose tool call (literal ${JSON.stringify(n)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${e}" (see catalog parser_defaults)`}function se(e,n,t){if(!(!e||n>0))return Ao(t)}function Eo(e){if(Array.isArray(e))return e.map(n=>{let t=n.function;return!t||typeof t!="object"?n:{type:"function",name:t.name,description:t.description,parameters:t.parameters,strict:t.strict}})}var Ae=class extends Error{};function Oo(e,n){return Array.isArray(n)?n.map(t=>{let o=t;if(o.type==="text")return{type:e==="assistant"?"output_text":"input_text",text:o.text};if(o.type==="image_url"){let r=o.image_url??{};return{type:"input_image",image_url:r.url,detail:r.detail}}throw new Ae(`unsupported chat content part type "${String(o.type)}" \u2014 the proxy translates text and image_url parts`)}):n}function Io(e){let n=[];for(let t of e){if(t.role==="tool"){n.push({type:"function_call_output",call_id:t.tool_call_id,output:typeof t.content=="string"?t.content:JSON.stringify(t.content??"")});continue}if(t.role==="assistant"&&Array.isArray(t.tool_calls)){typeof t.content=="string"&&t.content&&n.push({role:"assistant",content:t.content});for(let o of t.tool_calls){let r=o.function??{};n.push({type:"function_call",call_id:o.id,name:r.name,arguments:r.arguments})}continue}n.push({...t,content:Oo(t.role,t.content)})}return n}function Fe(e){return fe(e).map((n,t)=>({index:t,id:n.call_id??n.id??`call_${t}`,type:"function",function:{name:n.name??"",arguments:n.arguments??"{}"}}))}function Qt(e,n){let t=n?.output_text;return typeof t=="string"?t:e.filter(o=>o.type==="response.output_text.delta"&&typeof o.delta=="string").map(o=>o.delta).join("")}async function Po(e,n,t,o,r){let s=e.stream===!0,i;try{i=t.thread(e.previous_response_id,e.input)}catch(f){K(o,400,String(f instanceof Error?f.message:f));return}e.previous_response_id!=null&&(r.naiveInBytes+=Buffer.byteLength(JSON.stringify(i))-Buffer.byteLength(JSON.stringify(e.input)));let a=`resp_${Math.random().toString(36).slice(2,14)}`,c=e.store!==!1,u=Ke(e);r.model(u);let l=Array.isArray(e.tools)&&e.tools.length>0,p;try{p=await n.createResponse({model:e.model,input:i,stream:s,tools:e.tools,tool_choice:e.tool_choice,temperature:e.temperature,top_p:e.top_p,stop:e.stop,max_output_tokens:e.max_output_tokens,...e.reasoning_effort!==void 0?{reasoning_effort:e.reasoning_effort}:{},...e.chat_template_kwargs!==void 0?{chat_template_kwargs:e.chat_template_kwargs}:{}})}catch(f){if(f instanceof N){We(o,"openai",f);return}throw f}let g=[];if(s){Je(o);let f=v=>{o.write(`event: ${String(v.type??"message")}
|
|
10
|
-
|
|
17
|
+
${o}`)}function Dr(t,e){let n=new pe.WebSocketServer({noServer:!0,maxPayload:ui}),r=new _e,o=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,u=!1,d=(l,c,f)=>{let p;try{p=new URL(l.url??"/","http://localhost")}catch{pt(c,400,"malformed upgrade URL");return}if(c.on("error",()=>c.destroy()),c.on("close",()=>i.delete(c)),p.pathname!==ai){i.add(c);return}s.add(c);let _=!1;(async()=>{if(e.apiKey&&(l.headers.authorization??"")!==`Bearer ${e.apiKey}`){pt(c,401,"invalid local proxy api key");return}let R;try{R=await a(l)}catch(h){let w=e.statusOf?.(h)??null;pt(c,w?.status??500,"upstream resolver rejected the connection");return}u||c.destroyed||(_=!0,n.handleUpgrade(l,c,f,h=>{s.delete(c),o.add(h),h.on("close",()=>o.delete(h));try{new Kt(h,R,r,e.statusOf)}catch{o.delete(h),h.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!_&&!c.destroyed&&pt(c,500,"responses websocket upgrade failed")}).finally(()=>s.delete(c))};return t.on("upgrade",d),{stats:r,close:()=>{let{promise:l,resolve:c}=zt();if(u)return c(),l;u=!0,t.off("upgrade",d);for(let p of s)p.destroy();s.clear();for(let p of i)p.destroy();i.clear();for(let p of o)p.close(1001,"server shutting down");let f=setTimeout(()=>{for(let p of o)p.readyState!==pe.WebSocket.CLOSED&&p.terminate()},2e3);return f.unref?.(),n.close(()=>{clearTimeout(f),c()}),l}}}var Hr=require("@urun-sh/core");var Br=require("@urun-sh/core"),mi={no_capacity:429,queue_timeout:429,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502};function ft(t){if(t==null)return null;let e=mi[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 mt(t){if(t.kind==="create-failed")return ft(t.status.error?.code??null);if(t.kind!=="never-live")return null;let e=(t.status.elapsedMs??0)>=Br.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return ft(e?"cold_start_exceeded":"queue_timeout")}var Vr=require("ws");var jr=require("crypto");function k(t){return`${t}_${(0,jr.randomUUID)().replace(/-/g,"").slice(0,24)}`}var gt=4*1024*1024,Yt=8*1024*1024,ht=512,yt=20*1024*1024;function F(t,e,n){return{type:"invalid_request_error",code:t,message:e,param:n??null}}function He(t,e){return{type:"server_error",code:t,message:e}}function U(t){return{type:"error",event_id:k("event"),error:t}}function Xt(){return{outputModalities:["text"],turnDetection:null,inputAudioTranscription:!1}}var S=class extends Error{constructor(n,r,o="invalid_value"){super(n);this.param=r;this.code=o;this.name="SessionUpdateError"}param;code},gi={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},hi={format:!0,transcription:!0,turn_detection:!0,noise_reduction:!0},yi={format:!0,voice:!0,speed:!0};function Fr(t,e){let n=t;if(!n||typeof n!="object"||n.type!=="audio/pcm"||n.rate!==void 0&&n.rate!==24e3)throw new S(`${e} must be {"type":"audio/pcm","rate":24000} \u2014 the session audio lane is 24 kHz mono PCM16 only`,e)}function Ge(t,e,n){if(!t||typeof t!="object"||Array.isArray(t))throw new S("session must be an object","session","invalid_request");let r=t,o=Object.keys(r).filter(i=>!(i in gi));if(o.length>0)throw new S(`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 S("model must be a string or null","model");s.model=a??void 0;break;case"instructions":if(a!==null&&typeof a!="string")throw new S("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 S("modalities must be a non-empty array","modalities");let u=[];for(let d of a){if(d!=="text"&&d!=="audio")throw new S(`modality ${JSON.stringify(d)} is not "text" or "audio"`,"modalities");if(d==="audio"&&!n.voice)throw new S('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 S("voice must be a string or null","voice");if(!n.voice)throw new S("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 S(`${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 S("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 S(`${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 S("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 S("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 S("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 S("turn_detection must be an object or null","turn_detection");let d=String(u.type??"");if(d!=="server_vad"&&d!=="semantic_vad")throw new S(`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 S("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 S("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 S(`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 S("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 S("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 S("audio.input must be an object","audio.input");for(let l of Object.keys(d))if(!(l in hi))throw new S(`audio.input field not supported by this backend: ${l}`,`audio.input.${l}`,"unsupported_field");if(d.format!==void 0&&Fr(d.format,"audio.input.format"),d.transcription!==null&&d.transcription!==void 0){let l=Ge({input_audio_transcription:d.transcription},s,n);s.inputAudioTranscription=l.inputAudioTranscription}if(d.turn_detection!==null&&d.turn_detection!==void 0){let l=Ge({turn_detection:d.turn_detection},s,n);s.turnDetection=l.turnDetection}if(d.noise_reduction!==null&&d.noise_reduction!==void 0)throw new S("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 S("audio.output must be an object","audio.output");for(let l of Object.keys(d))if(!(l in yi))throw new S(`audio.output field not supported by this backend: ${l}`,`audio.output.${l}`,"unsupported_field");if(d.format!==void 0&&Fr(d.format,"audio.output.format"),d.speed!==void 0)throw new S("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 S("audio.output.voice must be a string or null","audio.output.voice");if(!n.voice)throw new S("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 _t(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 Qt(t,e){let n=t;if(!n||typeof n!="object")throw new S("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 S("function_call_output item requires string call_id and output","item.call_id","invalid_request");return{id:typeof n.id=="string"?n.id:k("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 S('message item role must be "user", "assistant" or "system"',"item.role","invalid_request");if(!Array.isArray(n.content))throw new S("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 S("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 S(`${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 S("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 S("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 S(`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:k("item"),type:"message",role:r,content:o}}throw new S(`item type ${String(n.type)} is not supported \u2014 message and function_call_output items only`,"item.type","invalid_request")}function Zt(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 _i=1001,wi=1011,qr=t=>t.slice(0,120),vi=/^[A-Za-z0-9+/=]*$/,bi=2;function ki(t){let e=Buffer.from(t,"base64");for(let n=0;n+1<e.byteLength;n+=2)if(Math.abs(e.readInt16LE(n))>bi)return!1;return!0}var Ve=class{id=k("conn");ws;binding;onClosed;config=Xt();sessionId=k("sess");conversationId=k("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:k("event"),session:_t(this.config,this.sessionId)}),this.send({type:"conversation.created",event_id:k("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(U(He("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:k("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:k("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=k("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:k("event"),previous_item_id:s,item_id:o}),this.send({type:"conversation.item.created",event_id:k("event"),previous_item_id:s,item:this.itemToProtocol(this.items[this.items.length-1])}),this.send({type:"input_audio_buffer.speech_stopped",event_id:k("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:k("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(F("decode_error","frame is not decodable text"),"invalid_frame");return}if(Buffer.byteLength(n,"utf8")>gt){this.fail(F("frame_too_large",`frame exceeds the ${gt}-byte limit`),"invalid_frame");return}let r;try{r=JSON.parse(n)}catch{this.fail(F("invalid_json","frame is not valid JSON"),"invalid_frame");return}if(typeof r.type!="string"||r.type.length===0){this.fail(F("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(U({type:"invalid_request_error",code:s.code??"invalid_value",message:s.message,param:s.param??null}));return}this.fail(He(null,o instanceof Error?o.message:String(o)),"handler_fault")}}async handle(e){switch(e.type){case"session.update":this.config=Ge(e.session,this.config,{voice:this.binding.voice,turns:this.binding.turns!==void 0}),this.send({type:"session.updated",event_id:k("event"),session:_t(this.config,this.sessionId)});return;case"conversation.item.create":{let n=Qt(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:k("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(U(F("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:k("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(U(F("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.send({type:"conversation.item.retrieved",event_id:k("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(U(F("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:k("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(U(F("invalid_audio",'input_audio_buffer.append requires base64 "audio"',"audio")));return}if(!this.ensureVoice())return;if(!vi.test(e.audio)){this.send(U(F("invalid_audio","input_audio_buffer.append audio must be base64","audio")));return}let n=Buffer.byteLength(e.audio,"base64");if(this.pendingAudioBytes.length+n>yt){this.send(U(F("buffer_full",`uncommitted input audio exceeds the ${yt}-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:k("event")});return}case"input_audio_buffer.commit":{if(!this.ensureVoice())return;if(this.pendingAudioTotalBytes===0&&this.takeTurnTranscript()===null){this.send(U(F("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=k("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:k("event"),previous_item_id:o,item_id:r}),this.send({type:"conversation.item.created",event_id:k("event"),previous_item_id:o,item:this.itemToProtocol(this.items[this.items.length-1])});return}case"output_audio_buffer.clear":{this.send(U(F("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(U(F("unknown_event",`event type ${e.type} is not supported by this adapter`)))}}ensureVoice(){return!this.binding.voice||!this.binding.audioSession?(this.send(U(F("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(U({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(U(F("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(R=>R!=="text"&&R!=="audio")){this.send(U(F("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(U(F("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=k("req"),o=`resp_${r}`;this.activeResponseId=o;let s=this.items.length,{input:i,sawAudioOnlyItem:a}=Zt(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,f=n.tools!==void 0?n.tools:this.config.tools,p=n.tool_choice!==void 0?n.tool_choice:this.config.toolChoice,_={input:i,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}:{},...f!==void 0?{tools:f}:{},...p!==void 0?{tool_choice:p}:{}};for(let R of this.frozenAudio.splice(0))this.binding.audioSession?.audio.appendInputAudio(R);this.inFlight=(async()=>{let R=[],h=E=>{this.responseFlushed?this.send(E):R.push(E)},w=()=>{if(!this.responseFlushed){this.send({type:"response.created",event_id:k("event"),response:this.responseObject(o,"in_progress")}),this.send({type:"response.in_progress",event_id:k("event"),response:this.responseObject(o,"in_progress")});for(let E of R)this.send(E);R.length=0,this.responseFlushed=!0}},m={messageOpen:!1,messageText:"",tools:new Map,nextOutputIndex:0},v=null;try{a&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let E;try{E=await this.upstreamStream(_,r)}catch(x){this.emitUpstreamFault(x,o);return}try{for await(let x of E){if(this.cancelled)break;let b=x;if(b.type==="error"){let y=this.upstreamErrorBody(b);this.responseFlushed?this.send(U(y)):this.send({type:"response.failed",event_id:k("event"),response:this.failedResponse(o,y)});return}if(b.type==="response.output_text.delta"&&typeof b.delta=="string"){w(),m.messageOpen||(this.beginMessageItem(o,m.nextOutputIndex++),m.messageOpen=!0),h({type:"response.output_text.delta",event_id:k("event"),item_id:`${o}_msg_0`,response_id:o,output_index:0,content_index:0,delta:b.delta}),m.messageText+=b.delta;continue}if(b.type==="response.function_call_arguments.delta"){w();let y=typeof b.delta=="string"?b.delta:"",P=typeof b.call_id=="string"&&b.call_id.length>0?b.call_id:`${o}_fc_${m.nextOutputIndex}`,$=m.tools.get(P);if(!$){let N=typeof b.name=="string"&&b.name.length>0?b.name:void 0;$={outputIndex:m.nextOutputIndex++,name:N,args:""},m.tools.set(P,$),h({type:"response.output_item.added",event_id:k("event"),response_id:o,output_index:$.outputIndex,item:{id:P,object:"realtime.item",type:"function_call",call_id:P,name:N??"",arguments:"",status:"in_progress"}})}typeof b.name=="string"&&b.name.length>0&&($.name=b.name),$.args+=y,h({type:"response.function_call_arguments.delta",event_id:k("event"),item_id:P,response_id:o,output_index:$.outputIndex,call_id:P,name:$.name??"",delta:y});continue}if(b.type==="response.completed"){v=b.response??null;continue}}}catch(x){this.emitUpstreamFault(x,o);return}if(this.cancelled)return;this.closeOpenItems(o,m,s),w(),this.send({type:"response.done",event_id:k("event"),response:this.doneResponse(o,s,v)})}finally{this.inFlight=null,this.activeResponseId=null}})()}onOutputAudioFrame(e,n){if(this.cancelled)return;if(this.activeResponseId!==e){if(ki(n))return;!this.outOfTurnAudioWarned&&!this.closed&&(this.outOfTurnAudioWarned=!0,this.send(U({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:k("event"),response:this.responseObject(e,"in_progress")}),this.send({type:"response.in_progress",event_id:k("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:k("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:k("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:k("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:k("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:k("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:k("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:k("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:k("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:k("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:k("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:k("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}}emitUpstreamFault(e,n){if(this.cancelled)return;let r=He(null,e instanceof Error?e.message:String(e));this.responseFlushed?this.send(U(r)):this.send({type:"response.failed",event_id:k("event"),response:this.failedResponse(n,r)})}cancelResponse(){let e=this.inFlight;if(!e){this.send(U(F("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:k("event"),response:this.responseObject(n,"cancelled")})})}pushItem(e){this.items.push(e),this.items.length>ht&&(this.items.shift(),this.send(U({type:"invalid_request_error",code:"transcript_evicted",message:`the conversation transcript exceeded ${ht} 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>Yt){this.ws.close(_i,qr("outbound buffer exceeded")),this.teardown("outbound buffer exceeded");return}this.ws.send(JSON.stringify(e))}}fail(e,n){this.send(U(e)),this.ws.close(wi,qr(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=>Gr(this.id,`binding release failed: ${n instanceof Error?n.message:String(n)}`));try{this.ws.removeAllListeners("message")}catch{}this.onClosed?.(this),Gr(this.id,e)}}},en=[];function Gr(t,e){en.push({id:t,reason:e}),en.length>256&&en.shift()}var tn="/v1/realtime",xi=t=>t.slice(0,120);function Ri(t,e,n){let r=e===401||e===403?"Unauthorized":e===502?"Upstream Fault":"Upgrade Refused";t.write(`HTTP/1.1 ${e} ${r}\r
|
|
18
|
+
Connection: close\r
|
|
19
|
+
Content-Type: text/plain\r
|
|
20
|
+
\r
|
|
21
|
+
${xi(n)}\r
|
|
22
|
+
`),t.destroy()}function nn(t,e){let n=new Vr.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,s,i)=>{if(new URL(o.url??"/","http://localhost").pathname!==tn)return;r.add(s),s.on("close",()=>{r.delete(s)});let u=l=>{let c=l,f=c.session!=null&&typeof c.session.sendResponseCreate=="function",p=c.clients!=null&&typeof c.clients.createResponse=="function";if(!f&&!p)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((_,R)=>{n.handleUpgrade(o,s,i,h=>{try{new Ve({ws:h,binding:l}),_()}catch(w){R(w)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o).then(u).catch(l=>{let c=l instanceof Hr.SessionFailedError?mt(l):null,f=null;if(l!==null&&typeof l=="object"&&"status"in l){let R=l.status;typeof R=="number"&&(f=R)}let p=f??c?.status??502,_=l instanceof Error?l.message:String(l);s.destroyed||Ri(s,p,_)}).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()))}}var We=(t,e)=>Object.assign(new Error(e),{status:t});function Si(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}async function Ai(t,e){if(!t.supportsAudio)throw We(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(s){throw s instanceof B?We(404,s.message):s}if(!n)throw We(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 We(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}function Wr(t,e){return{resolve:async n=>{if(e!==void 0){let r=n.headers.authorization,o=typeof r=="string"?r.trim():"",s=o.startsWith("Bearer ")?o.slice(7).trim():"";if(s===""||s!==e)throw We(401,"invalid local proxy api key")}return Ai(t,Si(n))}}}var Kr=xe(require("os"),1),zr=xe(require("path"),1),Yr=require("url"),Xr=require("util");var Jr="0.5.6";var Qr=4141,Ii=Jr,vt={claude:{bin:"claude",env:"anthropic"},codex:{bin:"codex",env:"openai"},pi:{bin:"pi",env:"openai"},omp:{bin:"omp",env:"openai"},aider:{bin:"aider",env:"openai"},opencode:{bin:"opencode",env:"openai"},crush:{bin:"crush",env:"openai"}};function sn(t,e="openai"){if(e==="anthropic")return{ANTHROPIC_BASE_URL:`http://127.0.0.1:${t}`,ANTHROPIC_API_KEY:"urun-local"};let n=`http://127.0.0.1:${t}/v1`;return{OPENAI_BASE_URL:n,OPENAI_API_BASE:n,OPENAI_API_KEY:"urun-local"}}function Zr(t,e,n){let r={...t,...sn(e,n)};return delete r.URUN_API_KEY,delete r.URUN_JWT,r}function eo(t,e,n){let r=["-c","model_providers.urun.name=urun","-c",`model_providers.urun.base_url="http://127.0.0.1:${t}/v1"`,"-c",'model_providers.urun.wire_api="responses"',"-c",'model_provider="urun"'],o=n.indexOf("--"),s=o===-1?n:n.slice(0,o),i=s.some(l=>l==="-m"||l==="--model"||l.startsWith("--model=")),a=n[0]==="exec",u=s.includes("--skip-git-repo-check"),d=a&&!u?["exec","--skip-git-repo-check",...n.slice(1)]:n;return[...r,...i?[]:["-m",e],...d]}function to(t,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--provider","urun","--model",t,...e]}function no(t,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--model",`urun/${t}`,...e]}function ro(t,e,n){let r=(t.URUN_PI_EXTENSION_ENTRY??"").trim();if(r){if(!n(r))throw new Error(`URUN_PI_EXTENSION_ENTRY points at ${r}, which does not exist \u2014 the launcher that injected it (the vendored urun CLI shim) must materialize the pi extension bundle before spawning pi`);return r}let o=(0,Yr.fileURLToPath)(new URL("../pi-extension/standalone.cjs",e));if(!n(o))throw new Error(`pi extension entry not found at ${o} \u2014 the pi lane loads the self-contained @urun-sh/openai dist/pi-extension/standalone.cjs from the installed package (run \`urun compat pi\` via the npm-installed CLI, e.g. \`npx @urun-sh/openai\`), or pass an entry to pi yourself: \`pi -e <path-to-@urun-sh/openai>/dist/pi-extension/standalone.cjs\``);return o}function oo(t){let e=Number(t);if(!Number.isInteger(e)||e<0)throw new Error(`--port needs a non-negative integer (0 = OS-assigned ephemeral), got ${t}`);return e}function so(t){if(t[0]!=="--port")return null;let e=oo(t[1]);return t.splice(0,2),e}function an(t){let e=t.indexOf("--port");if(e===-1)return Qr;let n=oo(t[e+1]);return t.splice(e,2),n}function io(t){if(t===0)throw new Error("`env` cannot use --port 0: it starts no proxy, so the OS never assigns the ephemeral port \u2014 pass the actual port of the running proxy");return t}function rn(t,e){let n=(t[e]??"").trim();if(!n)throw new Error(`${e} is not set. The proxy needs URUN_BASE_URL, URUN_ORG_ID, URUN_APP and either URUN_API_KEY (the org key; the proxy vends short-lived function-scoped client tokens from it via the SDK) or URUN_JWT (an explicit pre-vended token override) \u2014 plus optional URUN_FUNCTION, default "serve" \u2014 to open the backhaul session.`);return n}function Ie(t){let e=Oi(rn(t,"URUN_BASE_URL")),n=rn(t,"URUN_ORG_ID"),r=rn(t,"URUN_APP"),o=(t.URUN_FUNCTION??"serve").trim()||"serve",s=(t.URUN_API_URL??"").trim(),i=s?wt(s,"URUN_API_URL"):Zn,a=(t.URUN_JWT??"").trim();if(a)return{baseUrl:e,orgId:n,appId:r,fnName:o,apiUrl:i,auth:{lane:"jwt",jwt:a}};let u=(t.URUN_API_KEY??"").trim();if(u){let d=(t.URUN_GATEWAY_URL??"").trim(),l=d?wt(d):void 0;return{baseUrl:e,orgId:n,appId:r,fnName:o,apiUrl:i,auth:{lane:"api-key",apiKey:u,gatewayUrl:l}}}throw new Error("neither URUN_API_KEY nor URUN_JWT is set. Set URUN_API_KEY (the org API key \u2014 the proxy vends short-lived, function-scoped client tokens from it via the SDK, so the key never reaches the agent process) or URUN_JWT (an explicit pre-vended token; when set it always wins). URUN_BASE_URL, URUN_ORG_ID and URUN_APP are also required.")}function Oi(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 wt(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}function un(t){return{app:t.appId,org:t.orgId,fn:t.fnName,base_url:t.baseUrl,proxy_version:Ii}}function Ti(t,e){return t.app===e.app&&t.org===e.org&&t.fn===e.fn&&t.base_url===e.base_url&&t.proxy_version===e.proxy_version}var Pi=250;async function dn(t,e=Pi){try{let n=await fetch(`http://127.0.0.1:${t}/stats`,{signal:AbortSignal.timeout(e)});if(!n.ok)return null;let r=(await n.json()).identity;if(r==null||typeof r!="object")return null;let{app:o,org:s,fn:i,base_url:a,proxy_version:u}=r;return typeof o!="string"||typeof s!="string"||typeof i!="string"||typeof a!="string"||typeof u!="string"?null:{app:o,org:s,fn:i,base_url:a,proxy_version:u}}catch{return null}}var ao=2e3;async function Ci(t,e=ao){try{let n=await fetch(`http://127.0.0.1:${t}/v1/models`,{signal:AbortSignal.timeout(e)});return n.ok?null:`GET /v1/models answered HTTP ${n.status}`}catch(n){return`GET /v1/models failed (${String(n instanceof Error?n.message:n)})`}}async function uo(t,e,n=Qr,r=ao){if(t!=null)return{mode:"spawn",port:t};let o=await dn(n);if(o!==null&&Ti(o,e)){let s=await Ci(n,r);return s===null?{mode:"reuse",port:n,identity:o}:{mode:"spawn",port:0,reason:`a urun-openai proxy on port ${n} matches this invocation's identity but failed the serving-surface liveness check (${s}) \u2014 starting a fresh proxy on an ephemeral port instead of pointing the harness at it`}}return{mode:"spawn",port:0}}function ln(t,e=Kr.default.homedir()){return zr.default.join(e,".urun","logs",`compat-proxy-${t}.log`)}var on=["log","info","warn","error","debug","trace"];function lo(t,e=process,n=console){let r=e.stdout.write,o=e.stderr.write,s=(a,...u)=>t.write(a,...u);e.stdout.write=s,e.stderr.write=s;let i=Object.fromEntries(on.map(a=>[a,n[a]]));for(let a of on)n[a]=(...u)=>{t.write(`${(0,Xr.format)(...u)}
|
|
23
|
+
`)};return()=>{e.stdout.write=r,e.stderr.write=o;for(let a of on)n[a]=i[a]}}var $i=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function co(t){let e=t?.code;return typeof e=="string"&&$i.has(e)}function ae(t,e){try{return t.write(e),!0}catch(n){if(co(n))return!1;throw n}}function po(t){let e={stdout:!1,stderr:!1},n=[];for(let r of["stdout","stderr"]){let o=t.streams[r],s=i=>{co(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
|
|
24
|
+
`)}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)}}}function fo(t){let e=n=>{let r=n instanceof Error?`${n.stack??`${n.name}: ${n.message}`}`:String(n);try{t.appendSync(t.logPath,`${new Date().toISOString()} launcher crashed (in-process proxy): ${r}
|
|
25
|
+
`)}catch{}t.restore();try{t.writeStderr(`urun compat: the in-process proxy crashed (${n instanceof Error?n.message:String(n)}) \u2014 terminating the harness (its backend is gone); details: ${t.logPath}
|
|
26
|
+
`)}catch(o){try{t.appendSync(t.logPath,`${new Date().toISOString()} urun compat: the crash line could not reach stderr (${String(o?.code??o)}) \u2014 the terminal is gone
|
|
27
|
+
`)}catch{}}t.killChild(),t.exit(1)};return t.proc.on("uncaughtException",e),t.proc.on("unhandledRejection",e),()=>{t.proc.off("uncaughtException",e),t.proc.off("unhandledRejection",e)}}function mo(t,e){let n="pass --port 0 for an OS-assigned ephemeral port";return e?`port ${t} is held by another urun-openai proxy serving app ${e.app} (org ${e.org}, fn ${e.fn}, version ${e.proxy_version}) \u2014 reuse it by pointing your agent at http://127.0.0.1:${t}, or ${n}`:`port ${t} is already in use and does not answer the urun-openai /stats identity probe \u2014 ${n}`}var go="/v1/images/generations",bt="/v1/images/edits";function ho(t){return t===go||t===bt}var yo="/v1/images/models";async function _o(t,e,n){if(t){if(t.model=se({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 T=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},Ui=16*1024*1024,wo=64*1024*1024;function vo(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 Je(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 ue=class extends Error{};async function Li(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new ue(`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 ue(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function bo(t,e,n,r){let o=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},r),s=null;for await(let c of o){if(c.type==="error"){let f=c.error??{};throw new T(502,`image upstream error: ${String(f.message??"upstream reported an error")}`)}if(c.type==="response.completed"){s=c.response??null;break}}if(s===null)throw new T(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 T(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(vo(d)===null)throw new T(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 ko(t){return t instanceof T?t:t instanceof B?new T(404,t.message):new T(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function xo(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 Je(r,new T(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 Ro(t,e){t&&(t.outputUnits=e.images.length)}function So(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 Ni={model:!0,prompt:!0,response_format:!0,n:!0};async function Ao(t,e,n){let r=n.maxBodyBytes??wo;try{if(t.method!=="POST")throw new T(405,`${String(t.method)} is not supported on ${go}`);let o;try{o=JSON.parse((await Li(t,r)).toString("utf8")||"{}")}catch(c){throw c instanceof ue?new T(413,c.message):new T(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new T(400,"request body must be a JSON object");let s=o;for(let c of Object.keys(s))if(!Ni[c])throw new T(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 i=s.prompt;if(typeof i!="string"||i.trim().length===0)throw new T(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 T(400,"model must be a string","model");await _o(n.turn,n.clients,a);let u=s.n??1;if(u!==1)throw new T(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 T(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 xo(n.imageModels,a,"generation",e))return;let l=[{type:"input_text",text:i}];try{let c=await bo(n.clients,a,l,n.turn?oe(n.turn):void 0);Ro(n.turn,c),So(e,c)}catch(c){throw ko(c)}}catch(o){if(o instanceof ue){Je(e,new T(413,o.message));return}if(o instanceof T){Je(e,o);return}throw o}}var Mi={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function Eo(t,e,n){let r=n.maxBodyBytes??wo,o=n.maxImagePartBytes??Ui;try{if(t.method!=="POST")throw new T(405,`${String(t.method)} is not supported on ${bt}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new T(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i=t[Symbol.asyncIterator](),a=0,u=new ReadableStream({async pull(h){try{let w=await i.next();if(w.done){h.close();return}if(a+=w.value.length,a>r){h.error(new ue(`request body exceeds the ${r}-byte limit`));return}h.enqueue(w.value)}catch(w){h.error(w)}},cancel(){i.return?.()}}),d;try{let h=new Headers;for(let[v,E]of Object.entries(t.headers))if(typeof E=="string")h.set(v,E);else if(Array.isArray(E))for(let x of E)h.append(v,x);let w={method:"POST",headers:h,body:u,duplex:"half"};d=await new Request(`https://proxy.internal${t.url??"/"}`,w).formData()}catch(h){throw h instanceof ue?new T(413,h.message):new T(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 T(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 T(400,"model must be a string field","model");let f=c===null?void 0:c;await _o(n.turn,n.clients,f);let p=d.get("response_format")??"b64_json";if(p!=="b64_json")throw new T(400,`response_format ${JSON.stringify(p)} 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(!Mi[h])throw new T(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 _=[];for(let h of["image","image[]"])for(let w of d.getAll(h)){if(!(w instanceof File))throw new T(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",h);if(w.size>o)throw new T(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,h);let m=new Uint8Array(await w.arrayBuffer()),v=vo(m);if(v===null)throw new T(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",h);_.push({bytes:m,mime:v})}if(_.length===0)throw new T(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",l.length,l.length),!await xo(n.imageModels,f,"edit",e))return;let R=[{type:"input_text",text:l}];for(let{bytes:h,mime:w}of _)R.push({type:"input_image",image_url:`data:${w};base64,${Buffer.from(h).toString("base64")}`});try{let h=await bo(n.clients,f,R,n.turn?oe(n.turn):void 0);Ro(n.turn,h),So(e,h)}catch(h){throw ko(h)}}catch(s){if(s instanceof ue){Je(e,new T(413,s.message));return}if(s instanceof T){Je(e,s);return}throw s}}var Oo="/v1/usage/requests",Io=1e4,Di=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,fe=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},Oe=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}};var To=1024*1024;function cn(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new fe(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new fe(`"${e}" must be an array of uuid strings`);if(n.length>Io)throw new fe(`"${e}" carries ${n.length} ids; at most ${Io} 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"||!Di.test(s))throw new fe(`"${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 Po(t){return cn(t,"inference_ids")}function kt(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 Oe(`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 Oe(`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 Et=require("@urun-sh/core");var pn="/partners/huggingface/v1/billing/requests";function Bi(t){return cn(t,"requestIds")}function Fi(t){return t>=200&&t<=399}function ji(t){let e=[];for(let n of t)n.cost_nano_usd!==null&&Fi(n.http_status)&&e.push({requestId:n.inference_id,costNanoUsd:n.cost_nano_usd});return{requests:e.length>0?e:null}}async function Co(t,e){let n=Bi(t),r=await e(n);return kt(r,n),ji(r)}var qi=new Map([["/v1/chat/completions","conversational"],["/v1/responses","conversational"],["/v1/messages","conversational"],["/v1/images/generations","text-to-image"],["/v1/images/edits","image-to-image"]]);function Gi(t){return qi.get(t)??null}var Hi=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function Vi(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function Wi(t){return t==="surface"}function Ji(t){let e=t?.ledgerFailureReason;return Hi.includes(e)?e:"unknown"}function Ki(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function zi(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 Yi=256,$o=4096,ze=12e4,Xi=250,Qi=8e3,Ye=[],Uo=new Set,Xe=0,Ke=!1;var Zi=new Set;function ea(){return Xe===0&&Ye.length===0}function ta(){if(ea()){Ke=!1;for(let t of[...Zi])t()}}function Lo(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
|
|
28
|
+
`}function na(t){ae(process.stdout,t)}function No(t,e){try{t(e)}catch{}}function Mo(t){let{record:e,ledger:n}=t;if(n===null)return;let r=n.report??na,o=Gi(e.path);if(o===null)return;let s=l=>{try{n.observe(l)}catch{}},i=(l,c)=>{s({outcome:"skipped",reason:l}),No(r,Lo("inference_ledger_skipped",{inference_id:e.inference_id,path:e.path,reason:l,detail:c}))};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:zi(e),completed_at:e.ts,billable_outcome:Ki(e),session_id:e.session_id,prompt_tokens:e.prompt_tokens,completion_tokens:e.completion_tokens,output_units:e.output_units,queue_ms:e.queue_ms,prefill_ms:e.prefill_ms,decode_ms:e.decode_ms,total_ms:e.total_ms};if(Ye.length>=$o){i("queue_full",`${Ye.length} ledger writes are already waiting (capacity ${$o}, ${Xe} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}Ye.push({entry:d,req:t.req,record:e,ledger:n,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),Xe<Yi&&(Xe+=1,ra())}async function ra(){try{for(;;){let t=oa();if(t===void 0)return;Uo.add(t);try{await sa(t)}finally{Uo.delete(t)}}}finally{Xe-=1,ta()}}function oa(){let t=Date.now();for(;;){let e=Ye.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<ze)return e;Te(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 ${ze}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 sa(t){let{entry:e,record:n,ledger:r}=t;for(;;){t.attempts+=1;try{let o=await r.write(t.req,e);Te(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=Vi(o);if(s!==null){Te(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=Ji(o),a=Date.now()-t.queuedAt;if(Ke){Te(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(Wi(i)&&a<ze){if(await aa(ia(t.attempts)),Ke||Date.now()-t.queuedAt>=ze){let u=Date.now()-t.queuedAt;Te(t,{outcome:"failed",reason:Ke?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:Ke?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:u,error:`this row's backoff outlived the ${ze}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}Te(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 ia(t){return Math.min(Qi,Xi*2**Math.max(0,t-1))}function aa(t){return new Promise(e=>{setTimeout(e,t).unref?.()})}function Te(t,e,n){if(t.settled)return;t.settled=!0;try{t.ledger.observe(e)}catch{}let{event:r,...o}=n;No(t.report,Lo(r,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...o}))}function gn(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function V(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function de(t,e,n,r="invalid_request_error",o){V(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function jo(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);V(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function ee(t,e,n,r){V(t,e,{type:"error",error:{type:n,message:r}})}function xt(t,e,n){if(e==="anthropic"){V(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}V(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var ua=64*1024*1024,Qe=class extends Error{};async function Do(t,e=ua){let n=[],r=0;for await(let s of t){if(r+=s.length,r>e)throw new Qe(`request body exceeds ${e} bytes`);n.push(s)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function Pe(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 qo(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 Rt(t){let e=qo(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function Bo(t){let e=Rt(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 da(t,e){let n=qo(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function la(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 ca(t,e){let n=la(e);n&&(t.timing=n)}function Ce(t,e){da(t,e),ca(t,e)}async function hn(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 Go(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 pa(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 St=class extends Error{};function fa(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 St(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function ma(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:fa(n.role,n.content)})}return e}function fn(t){return Se(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 ga(t,e,n,r,o,s){let i=Fe(s.id),a=t.stream===!0;if(s.model=se(t),s.stream=a,await hn(s,e,typeof t.model=="string"?t.model:void 0),a){let w;try{w=await qe({body:t,clients:e,store:n,stats:o,responseId:i,turn:s})}catch(m){if(m instanceof B){xt(r,"openai",m);return}if(m instanceof X){de(r,m.status,m.message);return}throw m}gn(r);for await(let m of w.events)m.type==="error"&&(s.streamError=!0),m.type==="response.completed"&&Ce(s,m.response),r.write(`event: ${String(m.type??"message")}
|
|
29
|
+
data: ${JSON.stringify(m)}
|
|
11
30
|
|
|
12
|
-
`)
|
|
31
|
+
`);r.write(`data: [DONE]
|
|
13
32
|
|
|
14
|
-
`),
|
|
33
|
+
`),r.end();return}let u;try{u=Ee(n,t)}catch(w){if(w instanceof X){de(r,w.status,w.message);return}throw w}t.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(u))-Buffer.byteLength(JSON.stringify(t.input)));let d=t.store!==!1,l=se(t);o.model(l);let c=Array.isArray(t.tools)&&t.tools.length>0,f;try{f=await Wt(e,t,u,!1,oe(s))}catch(w){if(w instanceof B){xt(r,"openai",w);return}if(w instanceof X){de(r,w.status,w.message);return}throw w}let p=[],_=null;for await(let w of o.track(f,l,s)){if(p.push(w),w.type==="error"){jo(r,w);return}w.type==="response.completed"&&(_=w.response)}if(_==null){de(r,502,"upstream produced no response.completed event","upstream_error");return}Ce(s,_);let R=Ht(p,_),h=be(c,Se(_).length,R);if(h){de(r,502,ve(l,h),"upstream_error");return}d&&n.remember(i,u,Vt(_,R),d),V(r,200,{..._,id:i})}async function ha(t,e,n,r,o){let s=t.messages;if(!Array.isArray(s)){de(n,400,"chat/completions requires a messages array");return}let i=String(t.model??"urun"),a=Tr(o.id),u;try{u=ma(s)}catch(y){if(y instanceof St){de(n,400,y.message);return}throw y}let d=se(t);r.model(d),o.model=d,o.stream=t.stream===!0,await hn(o,e,typeof t.model=="string"?t.model:void 0);let l=Array.isArray(t.tools)&&t.tools.length>0,c;try{c=await e.createResponse({model:t.model,input:u,stream:!0,tools:pa(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}:{}},oe(o))}catch(y){if(y instanceof B){xt(n,"openai",y);return}throw y}if(t.stream===!0){gn(n);let y=[],P=new ce,$="";n.write(`data: ${JSON.stringify(Pe(a,i,{role:"assistant"},null))}
|
|
15
34
|
|
|
16
|
-
`);for await(let
|
|
35
|
+
`);for await(let q of r.track(c,d,o)){y.push(q);let A=Ae(q);if(q.type==="response.output_text.delta"&&typeof q.delta=="string")$+=q.delta,n.write(`data: ${JSON.stringify(Pe(a,i,{content:q.delta},null))}
|
|
17
36
|
|
|
18
|
-
`);else if(
|
|
37
|
+
`);else if(q.type==="response.reasoning_text.delta"&&typeof q.delta=="string")n.write(`data: ${JSON.stringify(Pe(a,i,{reasoning_content:q.delta},null))}
|
|
19
38
|
|
|
20
|
-
`);else if(
|
|
39
|
+
`);else if(A){let{call:L,isNew:j}=P.add(A),z=j?{index:A.tool_index,id:L.id??`call_${A.tool_index}`,type:"function",function:{name:L.name??"",arguments:A.delta}}:{index:A.tool_index,function:{arguments:A.delta}};n.write(`data: ${JSON.stringify(Pe(a,i,{tool_calls:[z]},null))}
|
|
21
40
|
|
|
22
|
-
`)}else if(
|
|
41
|
+
`)}else if(q.type==="error"){o.streamError=!0,n.write(`data: ${JSON.stringify({error:q})}
|
|
23
42
|
|
|
24
|
-
`),
|
|
43
|
+
`),n.end();return}}let N=fn(ie(y)),O=be(l,P.size+N.length,$);if(O){o.streamError=!0,n.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:ve(d,O)}})}
|
|
25
44
|
|
|
26
|
-
`),
|
|
45
|
+
`),n.end();return}P.size===0&&N.length>0&&n.write(`data: ${JSON.stringify(Pe(a,i,{tool_calls:N},null))}
|
|
27
46
|
|
|
28
|
-
`);let
|
|
47
|
+
`);let Q=P.size>0||N.length>0;n.write(`data: ${JSON.stringify(Pe(a,i,{},Q?"tool_calls":"stop"))}
|
|
29
48
|
|
|
30
|
-
`)
|
|
49
|
+
`);let K=Rt(ie(y));Ce(o,ie(y)),K&&n.write(`data: ${JSON.stringify({id:a,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:i,choices:[],usage:Bo(ie(y))})}
|
|
31
50
|
|
|
32
|
-
`),
|
|
51
|
+
`),n.write(`data: [DONE]
|
|
33
52
|
|
|
34
|
-
`),
|
|
35
|
-
data: ${JSON.stringify({type:
|
|
53
|
+
`),n.end();return}let f="",p="",_=[],R=new ce;for await(let y of r.track(c,d,o)){_.push(y),y.type==="response.output_text.delta"&&typeof y.delta=="string"&&(f+=y.delta),y.type==="response.reasoning_text.delta"&&typeof y.delta=="string"&&(p+=y.delta);let P=Ae(y);if(P&&R.add(P),y.type==="error"){jo(n,y);return}}let h=ie(_);Ce(o,h);let w=fn(h),m=w.length>0?w:fn({output:R.items()}),v=be(l,m.length,f);if(v){de(n,502,ve(d,v),"upstream_error");return}let E=m.map(({index:y,...P})=>P),x={role:"assistant",content:f||null},b=p||Go(h);b&&(x.reasoning_content=b),E.length>0&&(x.tool_calls=E),V(n,200,{id:a,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:i,choices:[{index:0,message:x,finish_reason:E.length>0?"tool_calls":"stop"}],usage:Bo(h)})}function ya(t){return typeof t=="string"?t:Array.isArray(t)?t.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var Ze=class extends Error{};function At(t){return new Ze(`unsupported anthropic content block type "${t}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function _a(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 wa(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 Ze(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function va(t){if(typeof t=="string")return t;if(t==null)return"";if(!Array.isArray(t))throw At(typeof t);let e="";for(let n of t){let r=String(n.type??"");if(r!=="text")throw At(`tool_result > ${r}`);e+=String(n.text??"")}return e}function ba(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 At(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:va(i.content)});else{if(a==="thinking"||a==="redacted_thinking")continue;throw At(a)}}return s(),r}function mn(t){return Se(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 ka(t,e,n,r,o){let s=t.messages;if(!Array.isArray(s)){ee(n,400,"invalid_request_error","messages requires a messages array");return}if(typeof t.max_tokens!="number"||!Number.isInteger(t.max_tokens)||t.max_tokens<1){ee(n,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(t.top_k!==void 0){ee(n,400,"invalid_request_error","top_k is not supported: the uRun serve lane exposes temperature/top_p/stop_sequences only");return}if(t.stop_sequences!==void 0&&(!Array.isArray(t.stop_sequences)||t.stop_sequences.some(y=>typeof y!="string"))){ee(n,400,"invalid_request_error","stop_sequences must be an array of strings");return}let i=String(t.model??"urun"),a=Pr(o.id),u=[],d;try{t.system&&u.push({role:"system",content:ya(t.system)});for(let y of s)u.push(...ba(y));d=wa(t.tool_choice)}catch(y){if(y instanceof Ze){ee(n,400,"invalid_request_error",y.message);return}throw y}let l=se(t);r.model(l),o.model=l,o.stream=t.stream===!0,await hn(o,e,typeof t.model=="string"?t.model:void 0);let c=Array.isArray(t.tools)&&t.tools.length>0,f;try{f=await e.createResponse({model:t.model,input:u,stream:!0,tools:_a(t.tools),tool_choice:d,temperature:t.temperature,max_output_tokens:t.max_tokens,top_p:t.top_p,stop:t.stop_sequences},oe(o))}catch(y){if(y instanceof B){xt(n,"anthropic",y);return}throw y}let p=(y,P)=>{n.destroyed||n.write(`event: ${y}
|
|
54
|
+
data: ${JSON.stringify({type:y,...P})}
|
|
36
55
|
|
|
37
|
-
`)};if(
|
|
38
|
-
data: ${JSON.stringify({type:"error",error:{type:"api_error",message:
|
|
56
|
+
`)};if(t.stream===!0){let y=r.track(f,l,o);gn(n),p("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:i,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let P=0,$="",N=0,O="none",Q=()=>O,K=-1,q=-1,A=new ce,L=[],j=()=>{O!=="none"&&(O="none",p("content_block_stop",{index:K}))},z=(D,W)=>{j(),O=D,K=N++,p("content_block_start",{index:K,content_block:W})};for await(let D of y){if(n.destroyed)break;L.push(D);let W=Ae(D);if(D.type==="response.reasoning_text.delta"&&typeof D.delta=="string")Q()!=="thinking"&&z("thinking",{type:"thinking",thinking:""}),p("content_block_delta",{index:K,delta:{type:"thinking_delta",thinking:D.delta}});else if(D.type==="response.output_text.delta"&&typeof D.delta=="string")Q()!=="text"&&z("text",{type:"text",text:""}),P+=1,$+=D.delta,p("content_block_delta",{index:K,delta:{type:"text_delta",text:D.delta}});else if(W){let{call:wn}=A.add(W);(Q()!=="tool"||W.tool_index!==q)&&(q=W.tool_index,z("tool",{type:"tool_use",id:wn.id??`call_${W.tool_index}`,name:wn.name??"",input:{}})),W.delta&&p("content_block_delta",{index:K,delta:{type:"input_json_delta",partial_json:W.delta}})}else if(D.type==="error"){o.streamError=!0,p("error",{error:{type:"api_error",message:JSON.stringify(D)}}),n.end();return}}if(n.destroyed)return;let me=ie(L),ge=Se(me),le=be(c,A.size+ge.length,$);if(le){o.streamError=!0,p("error",{error:{type:"api_error",message:ve(l,le)}}),n.end();return}j();let te=[];if(A.size===0)try{te=mn(me)}catch(D){o.streamError=!0,p("error",{error:{type:"api_error",message:String(D instanceof Error?D.message:D)}}),n.end();return}N===0&&te.length===0&&(z("text",{type:"text",text:""}),j()),te.forEach(D=>{let W=N++;p("content_block_start",{index:W,content_block:{...D,input:{}}}),p("content_block_delta",{index:W,delta:{type:"input_json_delta",partial_json:JSON.stringify(D.input??{})}}),p("content_block_stop",{index:W})});let $e=A.size>0||te.length>0,ke=Rt(me);Ce(o,me),p("message_delta",{delta:{stop_reason:$e?"tool_use":"end_turn"},usage:ke?{input_tokens:ke.input_tokens,output_tokens:ke.output_tokens}:{output_tokens:P}}),p("message_stop",{}),n.end();return}let _="",R="",h=[],w=new ce;for await(let y of r.track(f,l,o)){h.push(y),y.type==="response.output_text.delta"&&typeof y.delta=="string"&&(_+=y.delta),y.type==="response.reasoning_text.delta"&&typeof y.delta=="string"&&(R+=y.delta);let P=Ae(y);if(P&&w.add(P),y.type==="error"){let $=y.error,N=$?.type==="invalid_request_error";ee(n,N?400:502,N?"invalid_request_error":"api_error",$?.message??JSON.stringify(y));return}if(n.destroyed)break}if(n.destroyed)return;let m=ie(h);Ce(o,m);let v;try{v=mn(m),v.length===0&&w.size>0&&(v=mn({output:w.items()}))}catch(y){ee(n,502,"api_error",String(y instanceof Error?y.message:y));return}let E=be(c,v.length,_);if(E){ee(n,502,"api_error",ve(l,E));return}let x=[],b=R||Go(m);b&&x.push({type:"thinking",thinking:b}),(_||v.length===0)&&x.push({type:"text",text:_}),x.push(...v),V(n,200,{id:a,type:"message",role:"assistant",content:x,model:i,stop_reason:v.length>0?"tool_use":"end_turn",usage:Rt(m)??{input_tokens:0,output_tokens:0}})}function xa(t){ae(process.stdout,t)}function Ra(t){let{clients:e,apiKey:n,identity:r,statusOf:o}=t,s=typeof e=="function"?e:()=>e,i=t.requestLog??xa,a=t.observe,u=new _e;return(d,l)=>{let c=new URL(d.url??"/","http://localhost"),f=c.pathname==="/v1/messages"||c.pathname.startsWith("/v1/messages/"),p=(m,v,E,x,b)=>f?ee(l,m,x,v):de(l,m,v,E,b),_=Gt(c.pathname),R=c.pathname.startsWith("/partners/"),h=_||R?{id:Be(),startedAt:performance.now(),streamError:!1,model:null,stream:null,org:null,ttftMs:null,catalog:{kind:"unresolved",reason:"no lane resolved a model for this request"},promptTokens:null,completionTokens:null,outputUnits:null,sessionId:null,timing:null}:null;h&&(l.setHeader(Ir,h.id),l.once("close",()=>{let m=Cr({turn:h,method:d.method??"",path:c.pathname,res:l});i($r(m)),a?.(m),Mo({record:m,ledger:t.ledger??null,req:d})}));let w=()=>Or(c.pathname,h);(async()=>{if(d.method==="GET"&&c.pathname==="/healthz"){V(l,200,{ok:!0});return}if(d.method==="GET"&&c.pathname==="/stats"){V(l,200,r?{identity:r,...u.snapshot()}:u.snapshot());return}if(n&&(d.headers.authorization??"")!==`Bearer ${n}`){p(401,"invalid local proxy api key","authentication_error","authentication_error");return}let m=c.pathname===Oo?"canonical":c.pathname===pn?"huggingface":null;if(m){if(d.method!=="POST"){p(405,`${c.pathname} is a POST batch lookup`,"invalid_request_error","invalid_request_error");return}let b=t.usage;if(!b){p(501,`${c.pathname} needs a usage seam (ProxyHandlerOptions.usage) to read the per-request inference ledger; this proxy has none, and answering an empty result would be indistinguishable from "nothing is priced yet"`,"not_implemented","not_implemented_error");return}let y=b.authorize(d),P;try{P=await Do(d,To)}catch(O){if(O instanceof Qe){p(413,O.message,"request_too_large","request_too_large");return}p(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let $=O=>b.lookup(y,O),N;try{if(m==="canonical"){let O=Po(P),Q=await $(O);kt(Q,O),N={requests:Q}}else N=await Co(P,$)}catch(O){if(O instanceof fe){p(O.status,O.message,"invalid_request_error","invalid_request_error");return}throw O}V(l,200,N);return}if(c.pathname.startsWith("/partners/")){p(404,`no partner adapter for ${d.method} ${c.pathname} \u2014 the partner surface currently carries ${pn}`,"invalid_request_error","not_found_error");return}let v=await s(d);if(h&&(h.org=v.tenant??null),d.method==="GET"&&c.pathname==="/v1/models"){if(c.searchParams.get("format")==="openrouter"){if(!v.openRouterModels){p(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}V(l,200,await v.openRouterModels());return}V(l,200,await v.listModels());return}if(d.method==="GET"&&c.pathname===yo){if(!v.listImageModels){p(501,"GET /v1/images/models needs a router-backed client (listImageModels)","not_implemented","not_implemented_error");return}V(l,200,await v.listImageModels());return}if(ho(c.pathname)){if(!v.supportsImages){p(501,`the image lanes need a router-backed client (supportsImages) to gate model capability for ${c.pathname}`,"not_implemented","not_implemented_error");return}let b={clients:v,imageModels:v.supportsImages,stats:u,turn:w()};c.pathname===bt?await Eo(d,l,b):await Ao(d,l,b);return}if(d.method!=="POST"){p(404,`no route for ${d.method} ${c.pathname}`,"invalid_request_error","not_found_error");return}let E;try{E=await Do(d)}catch(b){if(b instanceof Qe){p(413,b.message,"request_too_large","request_too_large");return}p(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let x=Buffer.byteLength(JSON.stringify(E));if(c.pathname==="/v1/responses"){u.request("responses",x,x),await ga(E,v,je(v),l,u,w());return}if(c.pathname==="/v1/chat/completions"){u.request("chat_completions",x,x),await ha(E,v,l,u,w());return}if(c.pathname==="/v1/messages"){u.request("messages",x,x),await ka(E,v,l,u,w());return}if(c.pathname==="/v1/messages/count_tokens"){u.request("count_tokens",x,x),ee(l,404,"not_found_error","count_tokens is not implemented by the uRun proxy: the serve transport exposes no token-count lane, and the proxy will not fabricate counts");return}p(404,`no route for POST ${c.pathname}`,"invalid_request_error","not_found_error")})().catch(m=>{let v=String(m instanceof Error?m.message:m);if(!l.headersSent){let E=o?.(m)??null;if(E){p(E.status,v,E.openaiType,E.anthropicType);return}if(m instanceof Et.SessionAllocationError){let x=ft(m.code);if(x){let b=m.retryAfterSeconds??(x.status===429?30:void 0);b!==void 0&&l.setHeader("Retry-After",String(b)),p(x.status,v,x.openaiType,x.anthropicType,x.code);return}}if(m instanceof Et.SessionFailedError){let x=mt(m);if(x){let b=x.status===429?30:void 0;b!==void 0&&l.setHeader("Retry-After",String(b)),p(x.status,v,x.openaiType,x.anthropicType,x.code);return}}if(m instanceof Oe){p(502,v,"upstream_error","api_error");return}p(500,v,"proxy_error","api_error","proxy_error");return}h&&(h.streamError=!0),l.destroyed||(f?l.write(`event: error
|
|
57
|
+
data: ${JSON.stringify({type:"error",error:{type:"api_error",message:v}})}
|
|
39
58
|
|
|
40
|
-
`):l.write(`data: ${JSON.stringify({error:{message:
|
|
59
|
+
`):l.write(`data: ${JSON.stringify({error:{message:v,type:"upstream_error"}})}
|
|
41
60
|
|
|
42
|
-
`)),l.end()})}}function
|
|
43
|
-
`)}
|
|
44
|
-
`)}
|
|
45
|
-
`)}
|
|
46
|
-
`)
|
|
47
|
-
`)
|
|
48
|
-
`),
|
|
49
|
-
`)
|
|
50
|
-
`)
|
|
51
|
-
`)
|
|
52
|
-
`),0):(
|
|
53
|
-
`),
|
|
54
|
-
`)
|
|
55
|
-
`)
|
|
56
|
-
`)}),_.default.stdout.write(`urun openai proxy on http://127.0.0.1:${u}/v1 \u2014 proxy logs: ${h}
|
|
57
|
-
`),l=vn(g)),o==="codex"&&(s=ln(u,ie(_.default.env).appId,s));let w=(0,tt.spawn)(r,s,{stdio:"inherit",env:cn(_.default.env,u,i)}),x=h!==null?bn({logPath:h,appendSync:Y.appendFileSync,restore:()=>{l?.(),l=null},writeStderr:y=>_.default.stderr.write(y),killChild:()=>w.kill("SIGTERM"),exit:y=>_.default.exit(y),proc:_.default}):null,f=await new Promise(y=>{w.once("exit",U=>y(U??1)),w.once("error",U=>{l?.(),l=null,Pe(_.default.stderr,`failed to launch ${r}: ${String(U)}
|
|
58
|
-
`),y(127)})});x?.();let b=null;try{c&&await c.close()}catch(y){b=y}finally{l?.(),l=null,p?.(),p=null,g?.end()}return b!==null&&Pe(_.default.stderr,`urun compat: proxy teardown failed after the harness exited (harness exit code ${f} preserved): ${String(b instanceof Error?b.message:b)}
|
|
59
|
-
`),f}}Yo().then(e=>_.default.exit(e),e=>{_.default.stderr.write(`${String(e instanceof Error?e.message:e)}
|
|
60
|
-
`),_.default.exit(1)});
|
|
61
|
+
`)),l.end()})}}function Ho(t){let{clients:e,apiKey:n}=t,r=(0,Fo.createServer)(Ra(t)),o=Dr(r,{clients:e,apiKey:n,statusOf:t.statusOf}),s=nn(r,{resolver:Wr(e,n)}),i=r.close.bind(r);return r.close=(a=>(o.close(),s(),i(a))),Ar(r,{clients:e,apiKey:n}),r}var It=require("@urun-sh/core/internal");var Sa={ensureWebRTC:It.ensureNodeWebRTC,webrtcUnavailableReason:It.nodeWebRTCUnavailableReason,audioBackend:nt};async function Vo(t=Sa){try{await t.ensureWebRTC()}catch(n){return{ok:!1,reason:`the WebRTC bootstrap threw: ${String(n)}`}}let e=t.webrtcUnavailableReason();if(e)return{ok:!1,reason:`${e} \u2014 this artifact would join every session doc/KV-only and every token stream would error. Either "werift" is missing from the deployment directory (npm lane), or it was marked --external in a single-file build, where nothing can ever resolve it (bunfs: "Cannot find package 'werift' from '/$bunfs/root/\u2026'").`};try{(await t.audioBackend()).createSource()}catch(n){return{ok:!1,reason:`the werift/opus audio backend could not be constructed: ${String(n)}`}}return{ok:!0,reason:null}}function Aa(t,e){let n=(t.URUN_CATALOG_URL??"").trim(),r=n?wt(n,"URUN_CATALOG_URL"):"",o=(t.URUN_CATALOG_ANON_KEY??"").trim();return!r||!o?(e(`urun compat: catalog oracle off (URUN_CATALOG_URL/URUN_CATALOG_ANON_KEY unset) \u2014 an undeployed uRun CATALOG model name falls through to the default app instead of a loud 404; deployed apps still resolve exactly, and /stats.models records every mapping
|
|
62
|
+
`),null):{catalogUrl:r,anonKey:o}}function Ea(t){return or({baseUrl:t.baseUrl,orgId:t.orgId,defaultApp:t.appId,fnName:t.fnName,apiUrl:t.apiUrl,auth:t.auth,catalog:Aa(C.default.env,e=>C.default.stderr.write(e))})}async function Wo(t,e,n){let r=(0,yn.spawn)(t,e,{stdio:"inherit",env:n});return new Promise(o=>{r.once("exit",s=>o(s??1)),r.once("error",s=>{C.default.stderr.write(`failed to launch ${t}: ${String(s)}
|
|
63
|
+
`),o(127)})})}function Jo(){return ro(C.default.env,I,Z.existsSync)}function zo(t){for(let[e,n]of Object.entries(sn(t)))C.default.stdout.write(`export ${e}=${n}
|
|
64
|
+
`)}async function Ko(t,e){let n=Ie(C.default.env),r=e?un(n):void 0,o=Ea(n),s=null,i=d=>{if(s===null){let l=a.address();if(l===null)throw new Error("urun compat: a /v1 request was recorded before the proxy socket was listening");s=ln(l.port),(0,Z.mkdirSync)(_n.default.dirname(s),{recursive:!0})}(0,Z.appendFileSync)(s,d)},a=Ho({clients:sr(o),identity:r,...e?{requestLog:i}:{}});try{await new Promise((d,l)=>{a.once("error",l),a.listen(t,"127.0.0.1",d)})}catch(d){throw await o.closeAll().catch(()=>{}),d.code==="EADDRINUSE"?new Error(mo(t,await dn(t))):d}let u=a.address().port;return e&&(C.default.stdout.write(`urun openai proxy listening on http://127.0.0.1:${u}/v1
|
|
65
|
+
`),zo(u)),{port:u,close:async()=>{let d=new Promise(l=>a.close(()=>l()));a.closeAllConnections(),await d,await o.closeAll()}}}async function Ia(){let t=C.default.argv.slice(2),e=so(t),n=t.shift()??"proxy";if(n==="env")return zo(io(e??an(t))),0;if(n==="selftest"){let{ok:o,reason:s}=await Vo();return o?(C.default.stdout.write(`urun-openai selftest: OK \u2014 werift WebRTC peer globals installed and the werift/opus audio backend constructed from this artifact
|
|
66
|
+
`),0):(C.default.stderr.write(`urun-openai selftest: FAIL \u2014 ${s}
|
|
67
|
+
`),1)}if(n==="proxy"){let o=e??an(t),s=await Ko(o,!0);return await new Promise(i=>{C.default.once("SIGINT",i),C.default.once("SIGTERM",i)}),await s.close(),0}let r=n==="run"?t.shift():n;{if(!r)throw new Error(`launch needs an agent (${Object.keys(vt).join("|")}) or \`-- <cmd>\``);if(r==="pi"){let m=Ie(C.default.env);return Wo("pi",["-e",Jo(),...to(m.appId,t)],C.default.env)}if(r==="omp"){let m=Ie(C.default.env);return Wo("omp",["-e",Jo(),...no(m.appId,t)],C.default.env)}let o,s,i="openai";if(r==="--"){if(o=t.shift()??"",s=t,!o)throw new Error("`-- <cmd>` needs a command to launch")}else{let m=vt[r];if(!m)throw new Error(`unknown command/agent ${r}; commands: proxy | env; agents: ${Object.keys(vt).join(" | ")} \u2014 or \`-- <cmd>\``);o=m.bin,s=[...m.args??[],...t],i=m.env}let a=await uo(e,un(Ie(C.default.env))),u=null,d=a.port;a.mode==="reuse"?C.default.stdout.write(`reusing the running urun openai proxy on http://127.0.0.1:${d} (app ${a.identity.app}, fn ${a.identity.fn}, v${a.identity.proxy_version})
|
|
68
|
+
`):a.reason&&C.default.stderr.write(`urun compat: ${a.reason}
|
|
69
|
+
`);let l=null,c=null,f=null,p=null;a.mode!=="reuse"&&(u=await Ko(a.port,!1),d=u.port,p=ln(d),(0,Z.mkdirSync)(_n.default.dirname(p),{recursive:!0}),f=(0,Z.createWriteStream)(p,{flags:"a"}),c=po({streams:C.default,note:m=>(0,Z.appendFileSync)(p,m)}).uninstall,f.on("error",m=>{l?.(),l=null,ae(C.default.stderr,`urun compat: proxy log ${p} failed: ${String(m)} \u2014 proxy output continues on the terminal
|
|
70
|
+
`)}),C.default.stdout.write(`urun openai proxy on http://127.0.0.1:${d}/v1 \u2014 proxy logs: ${p}
|
|
71
|
+
`),l=lo(f)),r==="codex"&&(s=eo(d,Ie(C.default.env).appId,s));let _=(0,yn.spawn)(o,s,{stdio:"inherit",env:Zr(C.default.env,d,i)}),R=p!==null?fo({logPath:p,appendSync:Z.appendFileSync,restore:()=>{l?.(),l=null},writeStderr:m=>C.default.stderr.write(m),killChild:()=>_.kill("SIGTERM"),exit:m=>C.default.exit(m),proc:C.default}):null,h=await new Promise(m=>{_.once("exit",v=>m(v??1)),_.once("error",v=>{l?.(),l=null,ae(C.default.stderr,`failed to launch ${o}: ${String(v)}
|
|
72
|
+
`),m(127)})});R?.();let w=null;try{u&&await u.close()}catch(m){w=m}finally{l?.(),l=null,c?.(),c=null,f?.end()}return w!==null&&ae(C.default.stderr,`urun compat: proxy teardown failed after the harness exited (harness exit code ${h} preserved): ${String(w instanceof Error?w.message:w)}
|
|
73
|
+
`),h}}Ia().then(t=>C.default.exit(t),t=>{C.default.stderr.write(`${String(t instanceof Error?t.message:t)}
|
|
74
|
+
`),C.default.exit(1)});
|