@urun-sh/openai 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-YXK42SKC.js → chunk-76BZLCGT.js} +1 -1
- package/dist/chunk-7V2JKPIG.js +1 -0
- package/dist/chunk-T2LNHDPS.js +7 -0
- package/dist/chunk-TMQJY56F.js +4 -0
- package/dist/chunk-WEBOBE7Z.js +58 -0
- package/dist/chunk-X3GLH2HQ.js +1 -0
- package/dist/gemini-live.cjs +1 -1
- package/dist/gemini-live.d.cts +11 -0
- package/dist/gemini-live.d.ts +3 -0
- package/dist/gemini-live.js +1 -1
- package/dist/hosted/bin.cjs +36 -36
- package/dist/hosted/bin.js +1 -1
- package/dist/hosted/index.cjs +32 -32
- package/dist/hosted/index.d.cts +1 -1
- package/dist/hosted/index.d.ts +1 -1
- package/dist/hosted/index.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/pi-extension/index.cjs +6 -6
- package/dist/pi-extension/index.js +1 -1
- package/dist/pi-extension/standalone.cjs +52 -52
- package/dist/proxy/cli.cjs +46 -46
- package/dist/proxy/cli.js +1 -1
- package/dist/proxy/index.cjs +26 -26
- package/dist/proxy/index.d.cts +94 -15
- package/dist/proxy/index.d.ts +7 -3
- package/dist/proxy/index.js +1 -1
- package/dist/{responses-turn-CdhIre_a.d.ts → responses-turn-EwxfRJJg.d.ts} +15 -1
- package/dist/{responses-turn-BfdBbey8.d.cts → responses-turn-vMefRdgJ.d.cts} +112 -15
- package/package.json +2 -2
- package/dist/chunk-45FJ5GTP.js +0 -7
- package/dist/chunk-QTVTCMJU.js +0 -1
- package/dist/chunk-TDRZZGUK.js +0 -1
- package/dist/chunk-TPH77ZOW.js +0 -58
- package/dist/chunk-WCMX5VOF.js +0 -4
package/dist/proxy/index.cjs
CHANGED
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`)}var Ur={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},oe="urun.videoOut";function on(t){return{framing:Zt,codec:t}}function sn(t){let{seq:e,ptsUs:n,codec:o,key:r}=t.header;return{[oe]:{v:1,seq:e,ptsUs:n,codec:o,key:r,data:Buffer.from(t.au).toString("base64")}}}var Lr={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},Nr=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",oe],en=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function Dr(t){let e=D(t);if(!e)throw new w("setup.realtimeInputConfig must be an object");for(let r of Object.keys(e))if(!(r==="automaticActivityDetection"||r==="activityHandling"))throw new w(r==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${r}"`);let n=D(e.automaticActivityDetection);if(e.automaticActivityDetection!==void 0&&!n)throw new w("realtimeInputConfig.automaticActivityDetection must be an object");for(let r of Object.keys(n??{}))if(r!=="disabled")throw new w(`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(n?.disabled!==!0)throw new w("automatic (server-side) activity detection is the declared platform follow-up (serve turn_detection turn_start/turn_end events over the session stream lane, not proxy DSP) \u2014 set realtimeInputConfig.automaticActivityDetection.disabled: true for manual activity mode");let o="START_OF_ACTIVITY_INTERRUPTS";if(e.activityHandling!==void 0){if(!en.includes(e.activityHandling))throw new w(`unsupported activityHandling ${JSON.stringify(e.activityHandling)} \u2014 expected one of ${en.join(", ")}`);o=e.activityHandling}return{manualActivity:!0,activityHandling:o}}var Br=["temperature","maxOutputTokens","responseModalities"];function an(t){for(let[d,m]of Object.entries(Lr))if(t[d]!==void 0)throw new w(m);for(let d of Object.keys(t))if(!Nr.includes(d))throw new w(`unsupported setup field "${d}"`);if(typeof t.model!="string"||t.model.length===0)throw new w('setup.model is required (format "models/{model}")');let e=t.model.replace(/^models\//,""),n,o,r=!1,s=D(t.generationConfig);if(t.generationConfig!==void 0&&!s)throw new w("setup.generationConfig must be an object");if(s){for(let d of Object.keys(s))if(!Br.includes(d))throw new w(`unsupported generationConfig field "${d}"`);if(s.temperature!==void 0){if(typeof s.temperature!="number")throw new w("generationConfig.temperature must be a number");n=s.temperature}if(s.maxOutputTokens!==void 0){if(typeof s.maxOutputTokens!="number")throw new w("generationConfig.maxOutputTokens must be a number");o=s.maxOutputTokens}if(s.responseModalities!==void 0){let d=Array.isArray(s.responseModalities)?s.responseModalities:[s.responseModalities];for(let m of d){let p=String(m).toUpperCase();if(p==="AUDIO"){r=!0;continue}if(p!=="TEXT")throw new w(`responseModalities ${JSON.stringify(m)} 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:rn(t.systemInstruction,"setup.systemInstruction")});let a;if(t.tools!==void 0){if(!Array.isArray(t.tools))throw new w("setup.tools must be an array");a=[];for(let d of t.tools){let m=D(d);if(!m)throw new w("setup.tools entries must be objects");for(let p of Object.keys(m))if(p!=="functionDeclarations")throw new w(`unsupported tool "${p}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(m.functionDeclarations))throw new w("tool.functionDeclarations must be an array");for(let p of m.functionDeclarations){let k=D(p);if(!k||typeof k.name!="string")throw new w("functionDeclarations entries require a string name");a.push({type:"function",name:k.name,description:k.description,parameters:k.parameters})}}a.length===0&&(a=void 0)}let l={enabled:!1,handle:null};if(t.sessionResumption!==void 0){let d=D(t.sessionResumption);if(!d)throw new w("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let m of Object.keys(d))if(m!=="handle")throw new w(`unsupported sessionResumption field "${m}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(d.handle!==void 0&&(typeof d.handle!="string"||d.handle.length===0))throw new w("sessionResumption.handle must be a non-empty string when present");l={enabled:!0,handle:typeof d.handle=="string"?d.handle:null}}let u=t.realtimeInputConfig===void 0?Ur:Dr(t.realtimeInputConfig),c=!1;if(t[oe]!==void 0){if(!D(t[oe]))throw new w(`setup["${oe}"] must be an object (the v1 opt-in is {})`);c=!0}return{model:e,systemItems:i,tools:a,temperature:n,maxOutputTokens:o,audioModality:r,activity:u,sessionResumption:l,videoOut:c}}function un(t){return{sessionResumptionUpdate:{newHandle:t,resumable:!0}}}function dn(t){let e=Math.max(0,t??0)/1e3;return{goAway:{timeLeft:`${Math.round(e*1e3)/1e3}s`}}}function ln(t){for(let o of Object.keys(t))if(o!=="turns"&&o!=="turnComplete")throw new w(`unsupported clientContent field "${o}"`);let e=t.turns===void 0?[]:t.turns;if(!Array.isArray(e))throw new w("clientContent.turns must be an array");let n=[];for(let o of e){let r=D(o);if(!r)throw new w("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 w(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(r.role)})`);n.push({role:s==="model"?"assistant":"user",content:rn(r,"clientContent.turns")})}return{items:n,turnComplete:t.turnComplete===!0}}var nt=16e3,cn=24e3,Fr=`audio/pcm;rate=${cn}`,pn=Buffer.alloc(960).toString("base64");function qr(t,e,n){if(e===n)return t;let o=Math.floor(t.length*n/e),r=new Int16Array(o),s=e/n,i=t.length-1;for(let a=0;a<o;a++){let l=a*s,u=Math.floor(l);if(u>=i){r[a]=t[i];continue}let c=l-u;r[a]=Math.round(t[u]+(t[u+1]-t[u])*c)}return r}function jr(t){let e=D(t);if(!e)throw new w("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(e.mimeType))throw new w(`realtimeInput.video.mimeType must be image/jpeg (the Live video input stream is individual JPEG frames), got ${JSON.stringify(e.mimeType)}`);if(typeof e.data!="string"||e.data.length===0)throw new w("realtimeInput.video.data must be non-empty base64");let n=Buffer.from(e.data,"base64");if(n.length<3||n[0]!==255||n[1]!==216)throw new w("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(n)}function Hr(t){let e=D(t);if(!e)throw new w("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(e.mimeType))throw new w(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(e.mimeType)}`);let n=/;\s*rate=(\d+)/.exec(e.mimeType);if(n&&Number(n[1])!==nt)throw new w(`realtimeInput.audio.mimeType declares rate=${n[1]} \u2014 the Live spec input format is ${nt} Hz (audio/pcm;rate=${nt})`);if(typeof e.data!="string"||e.data.length===0)throw new w("realtimeInput.audio.data must be non-empty base64");let o=Buffer.from(e.data,"base64");if(o.length===0||o.length%2!==0)throw new w(`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=qr(r,nt,cn);return Buffer.from(s.buffer,s.byteOffset,s.byteLength).toString("base64")}function fn(t){if(t.mediaChunks!==void 0)throw new w('realtimeInput.mediaChunks is deprecated by the Live API \u2014 send the audio stream via realtimeInput.audio ({mimeType:"audio/pcm;rate=16000", data}) instead');if(t.activityStart!==void 0||t.activityEnd!==void 0){if(Object.keys(t).length!==1||t.activityStart!==void 0&&t.activityEnd!==void 0)throw new w("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let r=t.activityStart!==void 0?"activityStart":"activityEnd",s=D(t[r]);if(!s||Object.keys(s).length!==0)throw new w(`realtimeInput.${r} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:r}}let e=["text","audio","audioStreamEnd","video"];for(let o of Object.keys(t))if(!e.includes(o))throw new w(`unsupported realtimeInput field "${o}"`);let n=e.filter(o=>t[o]!==void 0);if(n.length!==1)throw new w(`realtimeInput must carry exactly one of ${e.join(", ")} (got ${n.length})`);if(t.audio!==void 0)return{kind:"audio",base64Pcm24k:Hr(t.audio)};if(t.video!==void 0)return{kind:"video",jpegFrame:jr(t.video)};if(t.audioStreamEnd!==void 0){if(t.audioStreamEnd!==!0)throw new w("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof t.text!="string"||t.text.length===0)throw new w("realtimeInput.text must be a non-empty string");return{kind:"text",text:t.text}}function mn(t){for(let e of Object.keys(t))if(e!=="functionResponses")throw new w(`unsupported toolResponse field "${e}"`);if(!Array.isArray(t.functionResponses)||t.functionResponses.length===0)throw new w("toolResponse.functionResponses must be a non-empty array");return t.functionResponses.map(e=>{let n=D(e);if(!n||typeof n.id!="string"||n.id.length===0)throw new w("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 gn(t){return t.type!=="response.output_text.delta"||typeof t.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:t.delta}]}}}}function hn(t){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:Fr,data:t}}]}}}}function yn(t){let e=D(t)??{},n=Array.isArray(e.output)?e.output:[],o=[],r=[],s=typeof e.output_text=="string"?e.output_text:"";if(!s)for(let u of n){let c=D(u);if(!(c?.type!=="message"||!Array.isArray(c.content)))for(let d of c.content){let m=D(d);m?.type==="output_text"&&typeof m.text=="string"&&(s+=m.text)}}s&&r.push({role:"assistant",content:s});for(let u of n){let c=D(u);if(c?.type!=="function_call")continue;if(typeof c.call_id!="string"||typeof c.name!="string")throw new w(`upstream function_call item is missing call_id/name: ${JSON.stringify(u)}`,se);let d=typeof c.arguments=="string"&&c.arguments.length>0?c.arguments:"{}",m;try{m=JSON.parse(d)}catch{throw new w(`upstream function_call arguments are not valid JSON: ${d}`,se)}o.push({id:c.call_id,name:c.name,args:m}),r.push({type:"function_call",call_id:c.call_id,name:c.name,arguments:d})}let i=[];o.length>0&&i.push({toolCall:{functionCalls:o}}),i.push({serverContent:{generationComplete:!0}});let a=D(e.usage),l={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&&(l.usageMetadata=u)}return i.push(l),{messages:i,assistantItems:r}}var Vr=256,ke=class{snapshots=new Map;store(e,n,o){for(this.snapshots.set(`${e}\0${n}`,o);this.snapshots.size>Vr;){let r=this.snapshots.keys().next().value;this.snapshots.delete(r)}}get(e,n){return this.snapshots.get(`${e}\0${n}`)}},Gr="local",Ce=t=>t.slice(0,120),it=class{constructor(e,n,o){this.ws=e;this.call=n;this.registry=o;this.clients=n.clients,this.scope=n.scope,e.on("message",r=>this.onMessage(r)),e.on("close",()=>{this.unsubscribeEnd?.(),this.unsubscribeEnd=null,this.detachAudio?.(),this.detachVideoOut?.(),this.call.release?.().catch(()=>{})})}ws;call;registry;config=null;transcript=[];queue=Promise.resolve();proxyHandle=null;unsubscribeEnd=null;audio=null;detachAudio=null;detachVideoOut=null;video=null;activityOpen=!1;activeTurn=null;scope;clients;send(e){this.ws.readyState===this.ws.OPEN&&this.ws.send(JSON.stringify(e))}fail(e){if(e instanceof U){this.ws.close(tn,Ce(e.message));return}if(e instanceof Q){this.ws.close(st,Ce(e.message));return}if(e instanceof w){this.ws.close(e.closeCode,Ce(e.message));return}this.ws.close(se,Ce(String(e instanceof Error?e.message:e)))}onMessage(e){try{let n=nn(typeof e=="string"?e:Buffer.concat(Array.isArray(e)?e:[e]).toString("utf8"));if(n.kind==="setup"){this.handleSetup(n.setup).catch(r=>this.fail(r));return}if(!this.config)throw new w(`"${n.kind}" before setup \u2014 the first client message must be BidiGenerateContentSetup`);let o=this.config;if(n.kind==="clientContent"){let{items:r,turnComplete:s}=ln(n.clientContent);this.transcript.push(...r),s&&this.enqueue(()=>this.runTurn());return}if(n.kind==="realtimeInput"){let r=fn(n.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:pn);return}this.transcript.push(...mn(n.toolResponse)),this.enqueue(()=>this.runTurn())}catch(n){this.fail(n)}}async handleSetup(e){if(this.config)throw new w("setup may only be sent once, as the first client message");let n=an(e),o=n.sessionResumption.handle;if(o!==null){let s=this.registry.get(this.scope,o);if(!s)throw new w("unknown sessionResumption.handle \u2014 the Live session it names is gone (proxy restarted, handle evicted, or never issued) and cannot be resumed; reconnect without a handle to start a new session",st);if(s.model!==n.model)throw new w(`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 w("setup.systemInstruction cannot be changed on resume \u2014 it is part of the resumed conversation state");if(this.call.clientsForResume!==void 0&&(this.clients=await this.call.clientsForResume(o)),this.proxyHandle=await this.clients.sessionHandle(n.model),this.proxyHandle!==s.proxyHandle)throw new w(`sessionResumption.handle named pooled session "${s.proxyHandle}" but the re-attach landed on "${this.proxyHandle}" \u2014 the conversation's native session changed and the snapshot cannot be adopted; reconnect without a handle to start a new session`,st);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 r={};if(n.videoOut){let s=this.clients.openVideoOut;if(s){let i=await s.call(this.clients,n.model),a=i.onOutputFrame(u=>this.send(sn(u))),l=i.onLaneError(u=>this.fail(u));this.detachVideoOut=()=>{a(),l()},r[oe]=on(i.codec)}}this.send({setupComplete:r}),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 w("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openAudio;if(!n)throw new w("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native AudioBridge seam over the canonical `audio` lane)",se);return this.audio=(async()=>{let o=await n.call(this.clients,e.model);return this.detachAudio=o.onOutputAudio(r=>this.send(hn(r))),o})(),this.audio.catch(o=>this.fail(o)),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 w("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openVideo;if(!n)throw new w("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",se);return this.video=Promise.resolve(n.call(this.clients,e.model)),this.video.catch(o=>this.fail(o)),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 w(`realtimeInput.${e} requires manual activity mode \u2014 set setup.realtimeInputConfig.automaticActivityDetection.disabled: true (automatic server-side activity detection is the declared platform follow-up)`);if(e==="activityStart"){if(this.activityOpen)throw new w("activityStart while an activity window is already open (send activityEnd first)");this.activityOpen=!0,n.activity.activityHandling==="START_OF_ACTIVITY_INTERRUPTS"&&this.activeTurn&&(this.activeTurn.interrupted=!0);return}if(!this.activityOpen)throw new w("activityEnd without an open activity window (send activityStart first)");this.activityOpen=!1}enqueue(e){this.queue=this.queue.then(e,()=>{}),this.queue=this.queue.catch(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 o=(0,_n.randomUUID)();this.registry.store(this.scope,o,{model:e.model,proxyHandle:n,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(o),this.send(un(o))}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,o=>this.enqueue(()=>this.handleUpstreamEnd(o))),this.config.sessionResumption.enabled&&await this.mintResumptionUpdate();return}this.send(dn(e.timeLeftMs)),this.ws.close(se,Ce(`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 o=await this.clients.createResponse({model:e.model,input:[...this.transcript],stream:!0,tools:e.tools,temperature:e.temperature,max_output_tokens:e.maxOutputTokens}),r,s=!1;for await(let l of o){if(n.interrupted)break;let u=l;if(u.type==="error")throw new w(`upstream error: ${JSON.stringify(u)}`,se);if(u.type==="response.completed"){r=u.response,s=!0;continue}let c=gn(u);c&&this.send(c)}if(n.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!s)throw new w("upstream produced no response.completed event",se);let{messages:i,assistantItems:a}=yn(r);this.transcript.push(...a);for(let l of i)this.send(l);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},me=class extends Error{constructor(n,o){super(o);this.status=n;this.name="UpgradeAuthError"}status};function Jr(t,e,n){let o=new Set;(l=>{if(typeof l=="string")l!==""&&o.add(l);else if(Array.isArray(l))for(let u of l)u!==""&&o.add(u)})(t.headers["x-goog-api-key"]);let s=t.headers.authorization;typeof s=="string"&&s.startsWith("Bearer ")&&s.length>7&&o.add(s.slice(7));let i=e.searchParams.get("key")??void 0;if(i!==void 0&&i!==""&&o.add(i),o.size>1)throw new me(401,`contradictory credentials: ${o.size} distinct keys presented across the x-goog-api-key/Authorization headers and the ?key= query parameter \u2014 present exactly one`);if(n&&i!==void 0&&i!=="")throw new me(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 l of o)a=l;if(a===void 0&&n)throw new me(401,"missing credentials: send the org API key in the x-goog-api-key header or as Authorization: Bearer");return a??""}function at(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 o=new wn.WebSocketServer({noServer:!0}),r=new ke;t.on("upgrade",(i,a,l)=>{let u=new URL(i.url??"/","http://localhost");u.pathname===ot&&(a.on("error",()=>{}),(async()=>{let c=Jr(i,u,n),d;if(e.authenticate)d=await e.authenticate(c,i);else{if(e.apiKey!==void 0&&c!==e.apiKey)throw new me(401,"invalid local proxy api key");d={clients:e.clients,scope:Gr}}o.handleUpgrade(i,a,l,m=>{new it(m,d,r)})})().catch(c=>{let d=502;(c instanceof me||typeof c=="object"&&c!==null&&"status"in c&&typeof c.status=="number")&&(d=c.status);let m=d===401?"Unauthorized":d===403?"Forbidden":d===502?"Bad Gateway":"Internal Server Error",p=c instanceof Error?c.message:String(c);a.destroyed||(a.write(`HTTP/1.1 ${d} ${m}\r
|
|
1
|
+
"use strict";var Br=Object.create;var rt=Object.defineProperty;var Fr=Object.getOwnPropertyDescriptor;var qr=Object.getOwnPropertyNames;var jr=Object.getPrototypeOf,Hr=Object.prototype.hasOwnProperty;var Vr=(t,e)=>{for(var n in e)rt(t,n,{get:e[n],enumerable:!0})},un=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of qr(e))!Hr.call(t,o)&&o!==n&&rt(t,o,{get:()=>e[o],enumerable:!(r=Fr(e,o))||r.enumerable});return t};var Mt=(t,e,n)=>(n=t!=null?Br(jr(t)):{},un(e||!t||!t.__esModule?rt(n,"default",{value:t,enumerable:!0}):n,t)),Gr=t=>un(rt({},"__esModule",{value:!0}),t);var pi={};Vr(pi,{GEMINI_LIVE_PATH:()=>ut,OPENAI_REALTIME_PATH:()=>qe,OpenAIRealtimeConnection:()=>Re,ResumptionRegistry:()=>Se,SessionGoneError:()=>ee,UnknownModelError:()=>N,VIDEO_OUT_SETUP_KEY:()=>de,attachGeminiLive:()=>ct,attachOpenAIRealtime:()=>je,createOpenAIProxy:()=>Dr,fixedClientsResolver:()=>wt,realtimeBindingFor:()=>Yt,realtimeModelOf:()=>zt,serveGeminiLiveSocket:()=>On});module.exports=Gr(pi);var Lr=require("http");var An=require("crypto"),In=require("ws");var N=class extends Error{},ee=class extends Error{};function Lt(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}}var ot=require("@urun-sh/core");var dn="urun.videoOut.v2-ws";var ut="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",zr=1007,cn=1008,le=1011,dt=1008,w=class extends Error{constructor(n,r=zr){super(n);this.closeCode=r}closeCode},F=t=>typeof t=="object"&&t!==null&&!Array.isArray(t)?t:null,st=["setup","clientContent","realtimeInput","toolResponse"];function pn(t){let e;try{e=JSON.parse(t)}catch{throw new w("client message is not valid JSON")}let n=F(e);if(!n)throw new w("client message must be a JSON object");let r=st.filter(a=>n[a]!==void 0),o=Object.keys(n).filter(a=>!st.includes(a));if(o.length>0)throw new w(`unknown client message field(s) ${JSON.stringify(o)} \u2014 expected exactly one of ${st.join(", ")}`);if(r.length!==1)throw new w(`client message must contain exactly one of ${st.join(", ")} (got ${r.length})`);let s=r[0],i=F(n[s]);if(!i)throw new w(`"${s}" must be a JSON object`);return{kind:s,[s]:i}}function fn(t,e){if(typeof t=="string")return t;let n=F(t),r=n?n.parts:void 0;if(!Array.isArray(r))throw new w(`${e}: expected a Content with a parts array`);let o=[];for(let s of r){let i=F(s);if(i&&typeof i.text=="string"){o.push(i.text);continue}throw i&&(i.inlineData!==void 0||i.fileData!==void 0)?new w(`${e}: media parts (inlineData/fileData) are a declared follow-up slice (audio/video lanes over the proxy transport AudioBridge) \u2014 only text parts are supported in this slice`):i&&(i.functionCall!==void 0||i.functionResponse!==void 0)?new w(`${e}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new w(`${e}: unsupported part ${JSON.stringify(s)}`)}return o.join(`
|
|
2
|
+
`)}var Yr={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},de="urun.videoOut";function mn(t){return{framing:dn,codec:t}}function gn(t){let{seq:e,ptsUs:n,codec:r,key:o}=t.header;return{[de]:{v:1,seq:e,ptsUs:n,codec:r,key:o,data:Buffer.from(t.au).toString("base64")}}}var Xr={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},Qr=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",de],ln=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function Zr(t){let e=F(t);if(!e)throw new w("setup.realtimeInputConfig must be an object");for(let o of Object.keys(e))if(!(o==="automaticActivityDetection"||o==="activityHandling"))throw new w(o==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${o}"`);let n=F(e.automaticActivityDetection);if(e.automaticActivityDetection!==void 0&&!n)throw new w("realtimeInputConfig.automaticActivityDetection must be an object");for(let o of Object.keys(n??{}))if(o!=="disabled")throw new w(`automaticActivityDetection.${o} tunes AUTOMATIC (server-side) activity detection \u2014 the declared platform follow-up (serve turn_detection events over the session stream lane); only "disabled: true" (manual activity mode) is supported in this slice`);if(n?.disabled!==!0)throw new w("automatic (server-side) activity detection is the declared platform follow-up (serve turn_detection turn_start/turn_end events over the session stream lane, not proxy DSP) \u2014 set realtimeInputConfig.automaticActivityDetection.disabled: true for manual activity mode");let r="START_OF_ACTIVITY_INTERRUPTS";if(e.activityHandling!==void 0){if(!ln.includes(e.activityHandling))throw new w(`unsupported activityHandling ${JSON.stringify(e.activityHandling)} \u2014 expected one of ${ln.join(", ")}`);r=e.activityHandling}return{manualActivity:!0,activityHandling:r}}var eo=["temperature","maxOutputTokens","responseModalities"];function hn(t){for(let[c,g]of Object.entries(Xr))if(t[c]!==void 0)throw new w(g);for(let c of Object.keys(t))if(!Qr.includes(c))throw new w(`unsupported setup field "${c}"`);if(typeof t.model!="string"||t.model.length===0)throw new w('setup.model is required (format "models/{model}")');let e=t.model.replace(/^models\//,""),n,r,o=!1,s=F(t.generationConfig);if(t.generationConfig!==void 0&&!s)throw new w("setup.generationConfig must be an object");if(s){for(let c of Object.keys(s))if(!eo.includes(c))throw new w(`unsupported generationConfig field "${c}"`);if(s.temperature!==void 0){if(typeof s.temperature!="number")throw new w("generationConfig.temperature must be a number");n=s.temperature}if(s.maxOutputTokens!==void 0){if(typeof s.maxOutputTokens!="number")throw new w("generationConfig.maxOutputTokens must be a number");r=s.maxOutputTokens}if(s.responseModalities!==void 0){let c=Array.isArray(s.responseModalities)?s.responseModalities:[s.responseModalities];for(let g of c){let p=String(g).toUpperCase();if(p==="AUDIO"){o=!0;continue}if(p!=="TEXT")throw new w(`responseModalities ${JSON.stringify(g)} 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:fn(t.systemInstruction,"setup.systemInstruction")});let a;if(t.tools!==void 0){if(!Array.isArray(t.tools))throw new w("setup.tools must be an array");a=[];for(let c of t.tools){let g=F(c);if(!g)throw new w("setup.tools entries must be objects");for(let p of Object.keys(g))if(p!=="functionDeclarations")throw new w(`unsupported tool "${p}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(g.functionDeclarations))throw new w("tool.functionDeclarations must be an array");for(let p of g.functionDeclarations){let v=F(p);if(!v||typeof v.name!="string")throw new w("functionDeclarations entries require a string name");a.push({type:"function",name:v.name,description:v.description,parameters:v.parameters})}}a.length===0&&(a=void 0)}let d={enabled:!1,handle:null};if(t.sessionResumption!==void 0){let c=F(t.sessionResumption);if(!c)throw new w("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let g of Object.keys(c))if(g!=="handle")throw new w(`unsupported sessionResumption field "${g}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(c.handle!==void 0&&(typeof c.handle!="string"||c.handle.length===0))throw new w("sessionResumption.handle must be a non-empty string when present");d={enabled:!0,handle:typeof c.handle=="string"?c.handle:null}}let u=t.realtimeInputConfig===void 0?Yr:Zr(t.realtimeInputConfig),l=!1;if(t[de]!==void 0){if(!F(t[de]))throw new w(`setup["${de}"] 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:u,sessionResumption:d,videoOut:l}}function yn(t){return{sessionResumptionUpdate:{newHandle:t,resumable:!0}}}function _n(t){let e=Math.max(0,t??0)/1e3;return{goAway:{timeLeft:`${Math.round(e*1e3)/1e3}s`}}}function wn(t){for(let r of Object.keys(t))if(r!=="turns"&&r!=="turnComplete")throw new w(`unsupported clientContent field "${r}"`);let e=t.turns===void 0?[]:t.turns;if(!Array.isArray(e))throw new w("clientContent.turns must be an array");let n=[];for(let r of e){let o=F(r);if(!o)throw new w("clientContent.turns entries must be Content objects");let s=o.role===void 0?"user":String(o.role).toLowerCase();if(s!=="user"&&s!=="model")throw new w(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(o.role)})`);n.push({role:s==="model"?"assistant":"user",content:fn(o,"clientContent.turns")})}return{items:n,turnComplete:t.turnComplete===!0}}var it=16e3,vn=24e3,to=`audio/pcm;rate=${vn}`,bn=Buffer.alloc(960).toString("base64");function no(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 d=a*s,u=Math.floor(d);if(u>=i){o[a]=t[i];continue}let l=d-u;o[a]=Math.round(t[u]+(t[u+1]-t[u])*l)}return o}function ro(t){let e=F(t);if(!e)throw new w("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(e.mimeType))throw new w(`realtimeInput.video.mimeType must be image/jpeg (the Live video input stream is individual JPEG frames), got ${JSON.stringify(e.mimeType)}`);if(typeof e.data!="string"||e.data.length===0)throw new w("realtimeInput.video.data must be non-empty base64");let n=Buffer.from(e.data,"base64");if(n.length<3||n[0]!==255||n[1]!==216)throw new w("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(n)}function oo(t){let e=F(t);if(!e)throw new w("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof e.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(e.mimeType))throw new w(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(e.mimeType)}`);let n=/;\s*rate=(\d+)/.exec(e.mimeType);if(n&&Number(n[1])!==it)throw new w(`realtimeInput.audio.mimeType declares rate=${n[1]} \u2014 the Live spec input format is ${it} Hz (audio/pcm;rate=${it})`);if(typeof e.data!="string"||e.data.length===0)throw new w("realtimeInput.audio.data must be non-empty base64");let r=Buffer.from(e.data,"base64");if(r.length===0||r.length%2!==0)throw new w(`realtimeInput.audio.data must decode to whole 16-bit samples (got ${r.length} bytes \u2014 not even)`);let o=new Int16Array(r.length/2);for(let i=0;i<o.length;i++)o[i]=r.readInt16LE(i*2);let s=no(o,it,vn);return Buffer.from(s.buffer,s.byteOffset,s.byteLength).toString("base64")}function kn(t){if(t.mediaChunks!==void 0)throw new w('realtimeInput.mediaChunks is deprecated by the Live API \u2014 send the audio stream via realtimeInput.audio ({mimeType:"audio/pcm;rate=16000", data}) instead');if(t.activityStart!==void 0||t.activityEnd!==void 0){if(Object.keys(t).length!==1||t.activityStart!==void 0&&t.activityEnd!==void 0)throw new w("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let o=t.activityStart!==void 0?"activityStart":"activityEnd",s=F(t[o]);if(!s||Object.keys(s).length!==0)throw new w(`realtimeInput.${o} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:o}}let e=["text","audio","audioStreamEnd","video"];for(let r of Object.keys(t))if(!e.includes(r))throw new w(`unsupported realtimeInput field "${r}"`);let n=e.filter(r=>t[r]!==void 0);if(n.length!==1)throw new w(`realtimeInput must carry exactly one of ${e.join(", ")} (got ${n.length})`);if(t.audio!==void 0)return{kind:"audio",base64Pcm24k:oo(t.audio)};if(t.video!==void 0)return{kind:"video",jpegFrame:ro(t.video)};if(t.audioStreamEnd!==void 0){if(t.audioStreamEnd!==!0)throw new w("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof t.text!="string"||t.text.length===0)throw new w("realtimeInput.text must be a non-empty string");return{kind:"text",text:t.text}}function Rn(t){for(let e of Object.keys(t))if(e!=="functionResponses")throw new w(`unsupported toolResponse field "${e}"`);if(!Array.isArray(t.functionResponses)||t.functionResponses.length===0)throw new w("toolResponse.functionResponses must be a non-empty array");return t.functionResponses.map(e=>{let n=F(e);if(!n||typeof n.id!="string"||n.id.length===0)throw new w("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 xn(t){return t.type!=="response.output_text.delta"||typeof t.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:t.delta}]}}}}function Sn(t){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:to,data:t}}]}}}}function En(t){let e=F(t)??{},n=Array.isArray(e.output)?e.output:[],r=[],o=[],s=typeof e.output_text=="string"?e.output_text:"";if(!s)for(let u of n){let l=F(u);if(!(l?.type!=="message"||!Array.isArray(l.content)))for(let c of l.content){let g=F(c);g?.type==="output_text"&&typeof g.text=="string"&&(s+=g.text)}}s&&o.push({role:"assistant",content:s});for(let u of n){let l=F(u);if(l?.type!=="function_call")continue;if(typeof l.call_id!="string"||typeof l.name!="string")throw new w(`upstream function_call item is missing call_id/name: ${JSON.stringify(u)}`,le);let c=typeof l.arguments=="string"&&l.arguments.length>0?l.arguments:"{}",g;try{g=JSON.parse(c)}catch{throw new w(`upstream function_call arguments are not valid JSON: ${c}`,le)}r.push({id:l.call_id,name:l.name,args:g}),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=F(e.usage),d={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&&(d.usageMetadata=u)}return i.push(d),{messages:i,assistantItems:o}}var so=256,Se=class{snapshots=new Map;store(e,n,r){for(this.snapshots.set(`${e}\0${n}`,r);this.snapshots.size>so;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(e,n){return this.snapshots.get(`${e}\0${n}`)}},io="local",Le=t=>t.slice(0,120),lt=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 N){this.ws.close(cn,Le(e.message));return}if(e instanceof ee){this.ws.close(dt,Le(e.message));return}if(e instanceof w){this.ws.close(e.closeCode,Le(e.message));return}this.ws.close(le,Le(String(e instanceof Error?e.message:e)))}onMessage(e){try{let n=pn(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 w(`"${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}=wn(n.clientContent);this.transcript.push(...o),s&&this.enqueue(()=>this.runTurn());return}if(n.kind==="realtimeInput"){let o=kn(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:bn);return}this.transcript.push(...Rn(n.toolResponse)),this.enqueue(()=>this.runTurn())}catch(n){this.fail(n)}}async handleSetup(e){if(this.config)throw new w("setup may only be sent once, as the first client message");let n=hn(e),r=n.sessionResumption.handle;if(r!==null){let s=this.registry.get(this.scope,r);if(!s)throw new w("unknown sessionResumption.handle \u2014 the Live session it names is gone (proxy restarted, handle evicted, or never issued) and cannot be resumed; reconnect without a handle to start a new session",dt);if(s.model!==n.model)throw new w(`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 w("setup.systemInstruction cannot be changed on resume \u2014 it is part of the resumed conversation state");if(this.call.clientsForResume!==void 0&&(this.clients=await this.call.clientsForResume(r)),this.proxyHandle=await this.clients.sessionHandle(n.model),this.proxyHandle!==s.proxyHandle)throw new w(`sessionResumption.handle named pooled session "${s.proxyHandle}" but the re-attach landed on "${this.proxyHandle}" \u2014 the conversation's native session changed and the snapshot cannot be adopted; reconnect without a handle to start a new session`,dt);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(u=>this.send(gn(u))),d=i.onLaneError(u=>this.fail(u));this.detachVideoOut=()=>{a(),d()},o[de]=mn(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 w("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openAudio;if(!n)throw new w("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native AudioBridge seam over the canonical `audio` lane)",le);return this.audio=(async()=>{let r=await n.call(this.clients,e.model);return this.detachAudio=r.onOutputAudio(o=>this.send(Sn(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 w("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let n=this.clients.openVideo;if(!n)throw new w("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",le);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 w(`realtimeInput.${e} requires manual activity mode \u2014 set setup.realtimeInputConfig.automaticActivityDetection.disabled: true (automatic server-side activity detection is the declared platform follow-up)`);if(e==="activityStart"){if(this.activityOpen)throw new w("activityStart while an activity window is already open (send activityEnd first)");this.activityOpen=!0,n.activity.activityHandling==="START_OF_ACTIVITY_INTERRUPTS"&&this.activeTurn&&(this.activeTurn.interrupted=!0);return}if(!this.activityOpen)throw new w("activityEnd without an open activity window (send activityStart first)");this.activityOpen=!1}enqueue(e){this.queue=this.queue.then(e,()=>{}),this.queue=this.queue.catch(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,An.randomUUID)();this.registry.store(this.scope,r,{model:e.model,proxyHandle:n,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(r),this.send(yn(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(_n(e.timeLeftMs)),this.ws.close(le,Le(`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 d of r){if(n.interrupted)break;let u=d;if(u.type==="error")throw new w(`upstream error: ${JSON.stringify(u)}`,le);if(u.type==="response.completed"){o=u.response,s=!0;continue}let l=xn(u);l&&this.send(l)}if(n.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!s)throw new w("upstream produced no response.completed event",le);let{messages:i,assistantItems:a}=En(o);this.transcript.push(...a);for(let d of i)this.send(d);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},ye=class extends Error{constructor(n,r){super(r);this.status=n;this.name="UpgradeAuthError"}status};function ao(t,e,n){let r=new Set;(d=>{if(typeof d=="string")d!==""&&r.add(d);else if(Array.isArray(d))for(let u of d)u!==""&&r.add(u)})(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 d of r)a=d;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 ct(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 In.WebSocketServer({noServer:!0}),o=new Se;t.on("upgrade",(i,a,d)=>{let u=new URL(i.url??"/","http://localhost");u.pathname===ut&&(a.on("error",()=>{}),(async()=>{let l=ao(i,u,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:io}}r.handleUpgrade(i,a,d,g=>{new lt(g,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 g=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} ${g}\r
|
|
3
3
|
Connection: close\r
|
|
4
4
|
\r
|
|
5
5
|
${p}\r
|
|
6
|
-
`),a.destroy())}))});let s=null;return()=>(s??=(async()=>{for(let i of
|
|
7
|
-
`}var Wr=256,Kr=128;function X(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,Kr)}var ge=class t{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(e,n,o){this.requests[e]=(this.requests[e]??0)+1,this.localInBytes+=n,this.naiveInBytes+=o}modelEntry(e){let n=this.models[e];if(n)return n;let o=Object.keys(this.models).length>=Wr?"(other)":e;return this.models[o]??={requests:0,tokens_out:0}}model(e){this.modelEntry(e).requests+=1}track(e,n,o){let r=this,s=performance.now(),i=null,a=0;return(async function*(){for await(let u of e)u.type==="response.output_text.delta"&&typeof u.delta=="string"&&(i===null&&(i=performance.now(),r.ttftMs.push(i-s),r.ttftMs.length>512&&r.ttftMs.shift(),o!==void 0&&(o.ttftMs=i-o.startedAt)),a+=1,r.tokensOut+=1,r.charsOut+=u.delta.length,n!==void 0&&(r.modelEntry(n).tokens_out+=1)),yield u;let l=(performance.now()-(i??s))/1e3;a>0&&l>0&&(r.tokRates.push(a/l),r.tokRates.length>512&&r.tokRates.shift())})()}static p50(e){if(!e.length)return null;let n=[...e].sort((o,r)=>o-r);return n[Math.floor(n.length/2)]}snapshot(){let e=Object.values(this.requests).reduce((n,o)=>n+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: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}}}},zr=256;function Yr(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 he=class extends Error{},Re=class{conversations=new Map;thread(e,n){let o=Yr(n);if(e==null)return o;let r=this.conversations.get(String(e));if(!r)throw new he(`unknown previous_response_id ${String(e)} (proxy restarts drop stored responses)`);return[...r,...o]}remember(e,n,o,r=!0){this.conversations.set(e,[...n,...o]);for(let s of this.conversations.keys()){if(this.conversations.size<=zr)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},In=new WeakMap;function Ue(t){let e=In.get(t);return e||(e=new Re,In.set(t,e)),e}function xe(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function ee(t){return t.find(e=>e.type==="response.completed")?.response??null}function Se(t){return t.type!=="response.function_call_arguments.delta"||typeof t.tool_index!="number"?null:{tool_index:t.tool_index,call_id:typeof t.call_id=="string"?t.call_id:void 0,name:typeof t.name=="string"?t.name:void 0,delta:typeof t.delta=="string"?t.delta:""}}var de=class{calls=new Map;add(e){let n=this.calls.get(e.tool_index),o=!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:o}}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}))}},Xr=["<tool_call>","<function="];function Qr(t){return Xr.find(e=>t.includes(e))}function ye(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 _e(t,e,n){if(!(!t||e>0))return Qr(n)}function ut(t,e){let n=e?.output_text;return typeof n=="string"?n:t.filter(o=>o.type==="response.output_text.delta"&&typeof o.delta=="string").map(o=>o.delta).join("")}function Zr(t){let e=t?.output;if(!Array.isArray(e))return[];let n=[];for(let o of e){if(o===null||typeof o!="object")continue;let r=o;r.type==="message"?n.push({role:"assistant",content:r.content??""}):r.type==="function_call"&&n.push({type:"function_call",call_id:r.call_id,name:r.name,arguments:r.arguments})}return n}function Tt(t,e){let n=Zr(t);return n.length>0?n:e?[{role:"assistant",content:e}]:[]}var Y=class extends Error{constructor(n,o,r=400,s){super(n);this.code=o;this.status=r;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 he?new Y(n.message,"previous_response_not_found",400,"previous_response_id"):new Y(String(n instanceof Error?n.message:n),"invalid_input")}}function On(t){let e=t.text;if(e!==null&&typeof e=="object"){let n=e.format;if(n!==void 0)return n}}async function Pt(t,e,n,o,r){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new Y("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:o,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}:{},...On(e)!==void 0?{response_format:On(e)}:{}},r)}async function Le(t){let{body:e,clients:n,store:o,stats:r}=t,s=Ee(o,e);e.previous_response_id!=null&&(r.naiveInBytes+=Buffer.byteLength(JSON.stringify(s))-Buffer.byteLength(JSON.stringify(e.input)));let i=t.responseId,a=t.remember??e.store!==!1,l=e.store!==!1,u=X(e);r.model(u);let c=Array.isArray(e.tools)&&e.tools.length>0,d=t.turn,m=await Pt(n,e,s,!0,d?Z(d):void 0);return{responseId:i,events:eo({events:m,conversation:s,responseId:i,modelKey:u,requestHadTools:c,remember:a,persist:l,store:o,stats:r,turn:t.turn})}}async function*eo(t){let{events:e,conversation:n,responseId:o,modelKey:r,requestHadTools:s,remember:i,persist:a,store:l,stats:u}=t,c=[],d=T=>T.response&&typeof T.response=="object"?{...T,response:{...T.response,id:o}}:T,m=`item_${o.slice(5)}`,p=`rs_${o.slice(5)}`,k=!1,O=!1,y=0;function*v(){k||(k=!0,yield{type:"response.created",response:{id:o,object:"response",status:"in_progress"}})}let _=!1,h=0,S="";function*g(){_&&(_=!1,yield{type:"response.reasoning_text.done",item_id:p,output_index:h,content_index:0,text:S},yield{type:"response.output_item.done",output_index:h,item:{id:p,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:S}],status:"completed"}})}let b=!1,f=0,R="";function*I(T){b&&(b=!1,yield{type:"response.output_text.done",item_id:m,output_index:f,content_index:0,text:R},yield{type:"response.content_part.done",item_id:m,output_index:f,content_index:0,part:{type:"output_text",text:R}},yield{type:"response.output_item.done",output_index:f,item:{id:m,type:"message",role:"assistant",status:T,content:[{type:"output_text",text:R}]}})}let $=new de,A=null,G=-1;function*J(T){if(!A)return;let N=A;A=null,yield{type:"response.function_call_arguments.done",item_id:N.fcId,output_index:N.outputIndex,arguments:N.args},yield{type:"response.output_item.done",output_index:N.outputIndex,item:{id:N.fcId,type:"function_call",call_id:N.callId??N.fcId,name:N.name??"",arguments:N.args,status:T}}}let V=!1;for await(let T of u.track(e,r,t.turn)){c.push(T);let N=String(T.type??"");if(N==="response.created"||N==="response.in_progress"){k=!0,yield d(T);continue}if(N==="response.output_item.added"){O=!0,yield d(T);continue}if(N==="response.reasoning_text.delta"&&!O){yield*v(),_||(_=!0,h=y++,yield{type:"response.output_item.added",output_index:h,item:{id:p,type:"reasoning",summary:[],content:[],status:"in_progress"}}),S+=String(T.delta??""),yield{type:"response.reasoning_text.delta",item_id:p,output_index:h,content_index:0,delta:T.delta};continue}if(N==="response.output_text.delta"&&!O){yield*v(),yield*g(),b||(b=!0,f=y++,yield{type:"response.output_item.added",output_index:f,item:{id:m,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:m,output_index:f,content_index:0,part:{type:"output_text",text:""}}),R+=String(T.delta??""),yield{type:N,item_id:m,output_index:f,content_index:0,delta:T.delta};continue}let q=O?null:Se(T);if(q){if(yield*v(),yield*g(),yield*I("completed"),$.add(q),q.tool_index!==G){yield*J("completed"),G=q.tool_index;let z=y++;A={fcId:`fc_${o.slice(5)}_${q.tool_index}`,outputIndex:z,callId:q.call_id,name:q.name,args:""},yield{type:"response.output_item.added",output_index:z,item:{id:A.fcId,type:"function_call",call_id:A.callId??A.fcId,name:A.name??"",arguments:"",status:"in_progress"}}}A&&(q.call_id&&(A.callId=q.call_id),q.name&&(A.name=q.name),A.args+=q.delta,yield{type:"response.function_call_arguments.delta",item_id:A.fcId,output_index:A.outputIndex,delta:q.delta});continue}if(N==="response.completed"&&!O){let z=xe(T.response),pe=_e(s,$.size+z.length,R);if(pe){V=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:ye(r,pe)}};break}if(yield*g(),yield*I("completed"),yield*J("completed"),$.size===0)for(let fe of z){let ue=y++,re=fe.id??fe.call_id??`fc_${ue}`,Pe=fe.arguments??"",be={id:re,type:"function_call",call_id:fe.call_id??re,name:fe.name??""};yield{type:"response.output_item.added",output_index:ue,item:{...be,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:re,output_index:ue,delta:Pe},yield{type:"response.function_call_arguments.done",item_id:re,output_index:ue,arguments:Pe},yield{type:"response.output_item.done",output_index:ue,item:{...be,arguments:Pe,status:"completed"}}}yield d(T);continue}yield d(T)}if(yield*g(),yield*I("incomplete"),yield*J("incomplete"),i&&!V){let T=ee(c);l.remember(o,n,Tt(T,ut(c,T)),a)}}var to="/v1/responses",no=64*1024*1024,ro=8*1024*1024,oo=16,Tn=32,Pn=3600*1e3,so=/^[A-Za-z0-9._-]{1,256}$/;function W(t,e,n){return{type:"error",status:t,...n!==void 0?{stream_id:n}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var io=W(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 Mt(){let t,e;return{promise:new Promise((o,r)=>{t=o,e=r}),resolve:t,reject:e}}async function ao(t){for(;t.bufferedAmount>ro&&t.readyState===le.WebSocket.OPEN;){let{promise:e,resolve:n}=Mt();setTimeout(n,5),await e}}var Ct=class extends Re{constructor(n){super();this.shared=n}shared;thread(n,o){if(n==null)return super.thread(n,o);try{return super.thread(n,o)}catch(r){if(!(r instanceof he))throw r;return this.shared.thread(n,o)}}remember(n,o,r,s=!0){s?this.shared.remember(n,o,r):super.remember(n,o,r)}forget(n){super.forget(n),this.shared.forget(n)}},$t=class{constructor(e,n,o,r){this.ws=e;this.clients=n;this.stats=o;this.statusOf=r;this.store=new Ct(Ue(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(W(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${Pn/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},Pn).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(W(400,{type:"invalid_request_error",code:"binary_frame",message:"Responses WebSocket frames must be JSON text"})),this.ws.close(1003,"binary frames are not supported");return}let o;try{o=JSON.parse(e.toString())}catch{this.sendFrame(W(400,{type:"invalid_request_error",code:"invalid_json",message:"frame is not valid JSON"}));return}if(o===null||typeof o!="object"||Array.isArray(o)){this.sendFrame(W(400,{type:"invalid_request_error",code:"invalid_event",message:"frame must be a JSON object"}));return}let r=o;if(r.type!=="response.create"){this.sendFrame(W(400,{type:"invalid_request_error",code:"unknown_event",message:`unknown client event type ${JSON.stringify(r.type??null)}; only "response.create" is supported`}));return}if("stream"in r){this.sendFrame(W(400,{type:"invalid_request_error",code:"stream_is_implicit",message:"'stream' is implicit over the Responses WebSocket and must not be sent",param:"stream"}));return}if("background"in r){this.sendFrame(W(400,{type:"invalid_request_error",code:"background_not_supported",message:"'background' is not supported over the Responses WebSocket",param:"background"}));return}let s;if(r.stream_id!==void 0){if(typeof r.stream_id!="string"||!so.test(r.stream_id)){this.sendFrame(io);return}if(s=r.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=Tn){this.sendFrame(W(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${Tn}). Reuse an existing stream_id or open a new WebSocket connection.`,param:"stream_id"},s));return}}let i=r.generate===!1?()=>this.runWarmup(r,s):()=>this.runTurn(r,s),a=s??"",l=this.lanes.get(a)??Promise.resolve();this.lanes.set(a,l.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 o=Buffer.byteLength(JSON.stringify(e));this.stats.request("responses_ws",o,o);let r;try{r=await Le({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:Me($e())})}catch(i){this.failTurn(i,n,e);return}this.laneOfResponse.set(r.responseId,n??null);let s=r.events;this.activeTurns.add(s);try{for await(let i of s){if(this.closed||this.ws.readyState!==le.WebSocket.OPEN)break;this.send(i,n),await ao(this.ws)}}finally{this.activeTurns.delete(s)}}catch(o){this.failTurn(o,n,e)}finally{this.release()}}}runWarmup(e,n){if(this.closed)return;let o=Me($e());try{let r=Ee(this.store,{...e,input:e.input??(e.previous_response_id!=null?[]:void 0)});this.store.remember(o,r,[],e.store!==!1),this.laneOfResponse.set(o,n??null),this.send({type:"response.created",response:{id:o,object:"response",status:"in_progress"}},n)}catch(r){this.failTurn(r,n,e)}}failTurn(e,n,o){this.sendFrame(this.errorFrameOf(e,n));let r=o.previous_response_id;typeof r=="string"&&this.laneOfResponse.get(r)===(n??null)&&(this.store.forget(r),this.laneOfResponse.delete(r))}errorFrameOf(e,n){let o=String(e instanceof Error?e.message:e);if(e instanceof Y)return W(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},n);if(e instanceof U)return W(404,{type:"invalid_request_error",code:"model_not_found",message:o},n);if(e instanceof Q)return W(503,{type:"upstream_error",code:"session_gone",message:o},n);let r=this.statusOf?.(e)??null;return r?W(r.status,{type:r.openaiType,message:o},n):W(500,{type:"upstream_error",message:o},n)}async acquire(){for(;this.active>=oo;){let{promise:e,resolve:n}=Mt();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!==le.WebSocket.OPEN)return;let o=n===void 0?e:{...e,stream_id:n};this.ws.send(JSON.stringify(o))}sendFrame(e){this.closed||this.ws.readyState!==le.WebSocket.OPEN||this.ws.send(JSON.stringify(e))}};function dt(t,e,n){let o=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",r=`${n}\r
|
|
8
|
-
`;t.end(`HTTP/1.1 ${e} ${
|
|
6
|
+
`),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(d=>d===void 0?i():a(d)))})(),s)}function On(t,e,n=new Se){new lt(t,e,n)}var ge=require("ws");var Tn=require("crypto"),Pn="Inference-Id";function Ne(){return(0,Tn.randomUUID)()}function Nt(t){return t==="/v1"||t.startsWith("/v1/")}function Cn(t,e){if(!Nt(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 Ue(t){return`resp_${t}`}function $n(t){return`chatcmpl-${t}`}function Mn(t){return`msg_${t}`}function te(t){return e=>{t.sessionId=e.sessionId,t.catalog=e.identity}}function Ln(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 Nn(t){return`${JSON.stringify(t)}
|
|
7
|
+
`}var uo=256,lo=128;function Q(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,lo)}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>=uo?"(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 u of e)u.type==="response.output_text.delta"&&typeof u.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+=u.delta.length,n!==void 0&&(o.modelEntry(n).tokens_out+=1)),yield u;let d=(performance.now()-(i??s))/1e3;a>0&&d>0&&(o.tokRates.push(a/d),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}}}},co=256;function po(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{},Ee=class{conversations=new Map;thread(e,n){let r=po(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<=co)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},Un=new WeakMap;function De(t){let e=Un.get(t);return e||(e=new Ee,Un.set(t,e)),e}function Ae(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function re(t){return t.find(e=>e.type==="response.completed")?.response??null}function Ie(t){return t.type!=="response.function_call_arguments.delta"||typeof t.tool_index!="number"?null:{tool_index:t.tool_index,call_id:typeof t.call_id=="string"?t.call_id:void 0,name:typeof t.name=="string"?t.name:void 0,delta:typeof t.delta=="string"?t.delta:""}}var me=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}))}},fo=["<tool_call>","<function="];function mo(t){return fo.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 mo(n)}function Oe(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 go(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 Ut(t,e){let n=go(t);return n.length>0?n:e?[{role:"assistant",content:e}]:[]}var ne=class extends Error{constructor(n,r,o=400,s){super(n);this.code=r;this.status=o;this.param=s}code;status;param};function Te(t,e){try{return t.thread(e.previous_response_id,e.input)}catch(n){throw n instanceof we?new ne(n.message,"previous_response_not_found",400,"previous_response_id"):new ne(String(n instanceof Error?n.message:n),"invalid_input")}}function Dn(t){let e=t.text;if(e!==null&&typeof e=="object"){let n=e.format;if(n!==void 0)return n}}async function Dt(t,e,n,r,o){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new ne("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}:{},...Dn(e)!==void 0?{response_format:Dn(e)}:{}},o)}async function Be(t){let{body:e,clients:n,store:r,stats:o}=t,s=Te(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,d=e.store!==!1,u=Q(e);o.model(u);let l=Array.isArray(e.tools)&&e.tools.length>0,c=t.turn,g=await Dt(n,e,s,!0,c?te(c):void 0);return{responseId:i,events:wo({events:g,conversation:s,responseId:i,modelKey:u,requestHadTools:l,remember:a,persist:d,store:r,stats:o,turn:t.turn})}}function ho(t,e){let n=[...t].sort((d,u)=>d.output_index-u.output_index).map(({item:d})=>d),r=Array.isArray(e?.output)?e.output:[],o=r.filter(d=>d!==null&&typeof d=="object"&&d.type==="message"),s=n.filter(d=>d.type==="message").length===o.length,i=0;return[...n.map(d=>d.type==="message"&&s?_o(o[i++],d):d),...yo(r,n)]}function yo(t,e){let n=e.some(u=>u.type==="message"),r=e.some(u=>u.type==="reasoning"),o=u=>String(u.call_id??u.id??""),s=e.filter(u=>u.type==="function_call"),i=t.filter(u=>u!==null&&typeof u=="object"&&u.type==="function_call").length,a=s.length===i,d=new Set(s.map(o));return t.filter(u=>{if(u===null||typeof u!="object")return!0;let l=u;return l.type==="message"?!n:l.type==="reasoning"?!r:l.type==="function_call"?!a&&!d.has(o(l)):!0})}function _o(t,e){let n=a=>a!==null&&typeof a=="object"&&a.type==="output_text",r=Array.isArray(t.content)?t.content:[];if(r.filter(n).length!==1)return e;let s=(Array.isArray(e.content)?e.content:[]).find(n);if(s===void 0)return e;let i={...t};return i.id=e.id,t.role===void 0&&(i.role=e.role),t.status===void 0&&(i.status=e.status),i.content=r.map(a=>n(a)?{...a,text:s.text}:a),i}async function*wo(t){let{events:e,conversation:n,responseId:r,modelKey:o,requestHadTools:s,remember:i,persist:a,store:d,stats:u}=t,l=[],c=P=>P.response&&typeof P.response=="object"?{...P,response:{...P.response,id:r}}:P,g=`item_${r.slice(5)}`,p=!1,v=!1,A=0;function*R(){p||(p=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let y=[],_=(P,M)=>{y.push({output_index:P,item:M})},h=!1,S=0,m="",b=0,f="";function*k(){if(!h)return;h=!1,yield{type:"response.reasoning_text.done",item_id:m,output_index:S,content_index:0,text:f};let P={id:m,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:f}],status:"completed"};yield{type:"response.output_item.done",output_index:S,item:P},_(S,P),f=""}let I=!1,O=0,C="",J=0,z="",W="";function*Y(P){if(!I)return;I=!1,yield{type:"response.output_text.done",item_id:C,output_index:O,content_index:0,text:z},yield{type:"response.content_part.done",item_id:C,output_index:O,content_index:0,part:{type:"output_text",text:z}};let M={id:C,type:"message",role:"assistant",status:P,content:[{type:"output_text",text:z}]};yield{type:"response.output_item.done",output_index:O,item:M},_(O,M),z=""}let ie=new me,q=null,ae=-1;function*pe(P){if(!q)return;let M=q;q=null,yield{type:"response.function_call_arguments.done",item_id:M.fcId,output_index:M.outputIndex,arguments:M.args};let V={id:M.fcId,type:"function_call",call_id:M.callId??M.fcId,name:M.name??"",arguments:M.args,status:P};yield{type:"response.output_item.done",output_index:M.outputIndex,item:V},_(M.outputIndex,V)}let tt=!1;for await(let P of u.track(e,o,t.turn)){l.push(P);let M=String(P.type??"");if(M==="response.created"||M==="response.in_progress"){p=!0,yield c(P);continue}if(M==="response.output_item.added"){v=!0,yield c(P);continue}if(M==="response.reasoning_text.delta"&&!v){yield*R(),h||(h=!0,S=A++,b+=1,m=b===1?`rs_${r.slice(5)}`:`rs_${r.slice(5)}_${b}`,yield{type:"response.output_item.added",output_index:S,item:{id:m,type:"reasoning",summary:[],content:[],status:"in_progress"}}),f+=String(P.delta??""),yield{type:"response.reasoning_text.delta",item_id:m,output_index:S,content_index:0,delta:P.delta};continue}if(M==="response.output_text.delta"&&!v){yield*R(),yield*k(),I||(I=!0,O=A++,J+=1,C=J===1?g:`${g}_${J}`,yield{type:"response.output_item.added",output_index:O,item:{id:C,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:C,output_index:O,content_index:0,part:{type:"output_text",text:""}}),z+=String(P.delta??""),W+=String(P.delta??""),yield{type:M,item_id:C,output_index:O,content_index:0,delta:P.delta};continue}let V=v?null:Ie(P);if(V){if(yield*R(),yield*k(),yield*Y("completed"),ie.add(V),V.tool_index!==ae){yield*pe("completed"),ae=V.tool_index;let ue=A++;q={fcId:`fc_${r.slice(5)}_${V.tool_index}`,outputIndex:ue,callId:V.call_id,name:V.name,args:""},yield{type:"response.output_item.added",output_index:ue,item:{id:q.fcId,type:"function_call",call_id:q.callId??q.fcId,name:q.name??"",arguments:"",status:"in_progress"}}}q&&(V.call_id&&(q.callId=V.call_id),V.name&&(q.name=V.name),q.args+=V.delta,yield{type:"response.function_call_arguments.delta",item_id:q.fcId,output_index:q.outputIndex,delta:V.delta});continue}if(M==="response.completed"&&!v){let ue=Ae(P.response),D=be(s,ie.size+ue.length,W);if(D){tt=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:ve(o,D)}};break}if(yield*k(),yield*Y("completed"),yield*pe("completed"),ie.size===0)for(let fe of ue){let xe=A++,nt=fe.id??fe.call_id??`fc_${xe}`,$t=fe.arguments??"",sn={id:nt,type:"function_call",call_id:fe.call_id??nt,name:fe.name??""};yield{type:"response.output_item.added",output_index:xe,item:{...sn,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:nt,output_index:xe,delta:$t},yield{type:"response.function_call_arguments.done",item_id:nt,output_index:xe,arguments:$t};let an={...sn,arguments:$t,status:"completed"};yield{type:"response.output_item.done",output_index:xe,item:an},_(xe,an)}let X=ho(y,P.response);yield X.length>0?c({...P,response:{...P.response,output:X}}):c(P);continue}yield c(P)}if(yield*k(),yield*Y("incomplete"),yield*pe("incomplete"),i&&!tt){let P=re(l);d.remember(r,n,Ut(P,Oe(l,P)),a)}}var vo="/v1/responses",bo=64*1024*1024,ko=8*1024*1024,Ro=16,Bn=32,Fn=3600*1e3,xo=/^[A-Za-z0-9._-]{1,256}$/;function Z(t,e,n){return{type:"error",status:t,...n!==void 0?{stream_id:n}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var So=Z(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 qt(){let t,e;return{promise:new Promise((r,o)=>{t=r,e=o}),resolve:t,reject:e}}async function Eo(t){for(;t.bufferedAmount>ko&&t.readyState===ge.WebSocket.OPEN;){let{promise:e,resolve:n}=qt();setTimeout(n,5),await e}}var Bt=class extends Ee{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)}},Ft=class{constructor(e,n,r,o){this.ws=e;this.clients=n;this.stats=r;this.statusOf=o;this.store=new Bt(De(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(Z(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${Fn/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},Fn).unref?.()}ws;clients;stats;statusOf;store;lanes=new Map;laneOfResponse=new Map;activeTurns=new Set;active=0;waiters=[];closed=!1;onMessage(e,n){if(this.closed)return;if(n){this.sendFrame(Z(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(Z(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(Z(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(Z(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(Z(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(Z(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"||!xo.test(o.stream_id)){this.sendFrame(So);return}if(s=o.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=Bn){this.sendFrame(Z(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${Bn}). 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??"",d=this.lanes.get(a)??Promise.resolve();this.lanes.set(a,d.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 Be({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:Ue(Ne())})}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!==ge.WebSocket.OPEN)break;this.send(i,n),await Eo(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=Ue(Ne());try{let o=Te(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 ne)return Z(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},n);if(e instanceof N)return Z(404,{type:"invalid_request_error",code:"model_not_found",message:r},n);if(e instanceof ee)return Z(503,{type:"upstream_error",code:"session_gone",message:r},n);let o=this.statusOf?.(e)??null;return o?Z(o.status,{type:o.openaiType,message:r},n):Z(500,{type:"upstream_error",message:r},n)}async acquire(){for(;this.active>=Ro;){let{promise:e,resolve:n}=qt();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!==ge.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!==ge.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
|
|
8
|
+
`;t.end(`HTTP/1.1 ${e} ${r}\r
|
|
9
9
|
Connection: close\r
|
|
10
10
|
Content-Type: text/plain; charset=utf-8\r
|
|
11
|
-
Content-Length: ${Buffer.byteLength(
|
|
11
|
+
Content-Length: ${Buffer.byteLength(o)}\r
|
|
12
12
|
\r
|
|
13
|
-
${r}`)}function Cn(t,e){let n=new le.WebSocketServer({noServer:!0,maxPayload:no}),o=new ge,r=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,l=!1,u=(c,d,m)=>{let p;try{p=new URL(c.url??"/","http://localhost")}catch{dt(d,400,"malformed upgrade URL");return}if(d.on("error",()=>d.destroy()),d.on("close",()=>i.delete(d)),p.pathname!==to){i.add(d);return}s.add(d);let k=!1;(async()=>{if(e.apiKey&&(c.headers.authorization??"")!==`Bearer ${e.apiKey}`){dt(d,401,"invalid local proxy api key");return}let O;try{O=await a(c)}catch(y){let v=e.statusOf?.(y)??null;dt(d,v?.status??500,"upstream resolver rejected the connection");return}l||d.destroyed||(k=!0,n.handleUpgrade(c,d,m,y=>{s.delete(d),r.add(y),y.on("close",()=>r.delete(y));try{new $t(y,O,o,e.statusOf)}catch{r.delete(y),y.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!k&&!d.destroyed&&dt(d,500,"responses websocket upgrade failed")}).finally(()=>s.delete(d))};return t.on("upgrade",u),{stats:o,close:()=>{let{promise:c,resolve:d}=Mt();if(l)return d(),c;l=!0,t.off("upgrade",u);for(let p of s)p.destroy();s.clear();for(let p of i)p.destroy();i.clear();for(let p of r)p.close(1001,"server shutting down");let m=setTimeout(()=>{for(let p of r)p.readyState!==le.WebSocket.CLOSED&&p.terminate()},2e3);return m.unref?.(),n.close(()=>{clearTimeout(m),d()}),c}}}var Dn=require("@urun-sh/core");var $n=require("@urun-sh/core"),uo={no_capacity:429,queue_timeout:429,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502},lo={no_capacity:"no ready GPU capacity for this model right now \u2014 the platform will cold-wake it on request; retry",queue_timeout:"the session was admitted but never became live within the cold-wake fence \u2014 the backend is busy or wedged; retry",cold_start_exceeded:"the cold boot exceeded its budget without serving \u2014 the platform keeps pursuing the wake; retry with a longer client timeout",queue_full:"the admission queue for this model is full \u2014 retry after the Retry-After period",admission_busy:"admission is momentarily contended (lock convoy) \u2014 retry shortly",admission_unavailable:"session admission is temporarily unavailable \u2014 retry",backend_error:"the control plane failed while admitting the session"};function Ut(t,e){let n=lo[t.code];return e?`${n} (${e})`:n}function lt(t){if(t==null)return null;let e=uo[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 ct(t){if(t.kind==="create-failed")return lt(t.status.error?.code??null);if(t.kind!=="never-live")return null;let e=(t.status.elapsedMs??0)>=$n.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return lt(e?"cold_start_exceeded":"queue_timeout")}var co=At(require("os"),1),po=At(require("path"),1),fo=require("url"),mo=require("util");var go=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function ho(t){let e=t?.code;return typeof e=="string"&&go.has(e)}function we(t,e){try{return t.write(e),!0}catch(n){if(ho(n))return!1;throw n}}var Bn=require("ws");var Un=require("crypto");function x(t){return`${t}_${(0,Un.randomUUID)().replace(/-/g,"").slice(0,24)}`}var pt=4*1024*1024,Lt=8*1024*1024,ft=512,mt=20*1024*1024;function F(t,e,n){return{type:"invalid_request_error",code:t,message:e,param:n??null}}function De(t,e){return{type:"server_error",code:t,message:e}}function L(t){return{type:"error",event_id:x("event"),error:t}}function Nt(){return{outputModalities:["text"],turnDetection:null,inputAudioTranscription:!1}}var E=class extends Error{constructor(n,o,r="invalid_value"){super(n);this.param=o;this.code=r;this.name="SessionUpdateError"}param;code},yo={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},_o={format:!0,transcription:!0,turn_detection:!0,noise_reduction:!0},wo={format:!0,voice:!0,speed:!0};function Mn(t,e){let n=t;if(!n||typeof n!="object"||n.type!=="audio/pcm"||n.rate!==void 0&&n.rate!==24e3)throw new E(`${e} must be {"type":"audio/pcm","rate":24000} \u2014 the session audio lane is 24 kHz mono PCM16 only`,e)}function Ne(t,e,n){if(!t||typeof t!="object"||Array.isArray(t))throw new E("session must be an object","session","invalid_request");let o=t,r=Object.keys(o).filter(i=>!(i in yo));if(r.length>0)throw new E(`session field(s) not supported by this backend: ${r.join(", ")}`,r[0],"unsupported_field");let s={...e,outputModalities:[...e.outputModalities]};for(let[i,a]of Object.entries(o))switch(i){case"model":if(a!==null&&typeof a!="string")throw new E("model must be a string or null","model");s.model=a??void 0;break;case"instructions":if(a!==null&&typeof a!="string")throw new E("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 E("modalities must be a non-empty array","modalities");let l=[];for(let u of a){if(u!=="text"&&u!=="audio")throw new E(`modality ${JSON.stringify(u)} is not "text" or "audio"`,"modalities");if(u==="audio"&&!n.voice)throw new E('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");l.includes(u)||l.push(u)}s.outputModalities=l;break}case"voice":if(a===null){delete s.voice;break}if(typeof a!="string")throw new E("voice must be a string or null","voice");if(!n.voice)throw new E("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 E(`${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 E("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 E(`${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 E("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 E("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 E("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 l=a;if(!l||typeof l!="object"||Array.isArray(l))throw new E("turn_detection must be an object or null","turn_detection");let u=String(l.type??"");if(u!=="server_vad"&&u!=="semantic_vad")throw new E(`turn_detection.type ${JSON.stringify(l.type)} is not "server_vad"/"semantic_vad"`,"turn_detection.type");s.turnDetection={createResponse:l.create_response!==!1,interruptResponse:l.interrupt_response!==!1};break}case"input_audio_transcription":{if(a===null){s.inputAudioTranscription=!1;break}if(!n.turns)throw new E("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 E("input_audio_transcription must be an object or null","input_audio_transcription");for(let l of Object.keys(a))if(!["model","language","languages","prompt"].includes(l))throw new E(`input_audio_transcription field not honored by this backend: ${l} (transcription comes from the voice engine itself)`,`input_audio_transcription.${l}`,"unsupported_field");s.inputAudioTranscription=!0;break}case"input_audio_noise_reduction":if(a!==null)throw new E("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 l=a;if(!l||typeof l!="object"||Array.isArray(l))throw new E("audio must be an object or null","audio");if(l.input!==void 0&&l.input!==null){let u=l.input;if(!u||typeof u!="object"||Array.isArray(u))throw new E("audio.input must be an object","audio.input");for(let c of Object.keys(u))if(!(c in _o))throw new E(`audio.input field not supported by this backend: ${c}`,`audio.input.${c}`,"unsupported_field");if(u.format!==void 0&&Mn(u.format,"audio.input.format"),u.transcription!==null&&u.transcription!==void 0){let c=Ne({input_audio_transcription:u.transcription},s,n);s.inputAudioTranscription=c.inputAudioTranscription}if(u.turn_detection!==null&&u.turn_detection!==void 0){let c=Ne({turn_detection:u.turn_detection},s,n);s.turnDetection=c.turnDetection}if(u.noise_reduction!==null&&u.noise_reduction!==void 0)throw new E("audio.input.noise_reduction must be null on this backend: no noise-reduction primitive","audio.input.noise_reduction","unsupported_field")}if(l.output!==void 0&&l.output!==null){let u=l.output;if(!u||typeof u!="object"||Array.isArray(u))throw new E("audio.output must be an object","audio.output");for(let c of Object.keys(u))if(!(c in wo))throw new E(`audio.output field not supported by this backend: ${c}`,`audio.output.${c}`,"unsupported_field");if(u.format!==void 0&&Mn(u.format,"audio.output.format"),u.speed!==void 0)throw new E("audio.output.speed is not supported on this backend: playback rate is fixed","audio.output.speed","unsupported_field");if(u.voice!==void 0)if(u.voice===null)delete s.voice;else{if(typeof u.voice!="string")throw new E("audio.output.voice must be a string or null","audio.output.voice");if(!n.voice)throw new E("audio.output.voice refused: no authoritative voice binding for the resolved backend","audio.output.voice","audio_not_supported");s.voice=u.voice}u.format!==void 0&&!s.outputModalities.includes("audio")&&(s.outputModalities=["text","audio"])}break}default:break}return s}function gt(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 Dt(t,e){let n=t;if(!n||typeof n!="object")throw new E("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 E("function_call_output item requires string call_id and output","item.call_id","invalid_request");return{id:typeof n.id=="string"?n.id:x("item"),type:"function_call_output",callId:n.call_id,output:n.output}}if(n.type==="message"){let o=n.role;if(o!=="user"&&o!=="assistant"&&o!=="system")throw new E('message item role must be "user", "assistant" or "system"',"item.role","invalid_request");if(!Array.isArray(n.content))throw new E("message item content must be an array","item.content","invalid_request");let r=[];for(let s of n.content){let i=s;if(!i||typeof i!="object")throw new E("content part must be an object","item.content","invalid_request");if(i.type==="input_text"||o==="assistant"&&i.type==="output_text"){if(typeof i.text!="string")throw new E(`${String(i.type)} part requires string text`,"item.content.text","invalid_request");r.push({type:i.type,text:i.text});continue}if(i.type==="input_audio"){if(!e)throw new E("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 E("input_audio part requires base64 audio","item.content.audio","invalid_request");r.push({type:"input_audio",...typeof i.transcript=="string"?{transcript:i.transcript}:{}});continue}throw new E(`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:x("item"),type:"message",role:o,content:r}}throw new E(`item type ${String(n.type)} is not supported \u2014 message and function_call_output items only`,"item.type","invalid_request")}function Bt(t){let e=[],n=!1;for(let o of t){if(o.type==="function_call"){e.push({type:"function_call",call_id:o.callId,name:o.name,arguments:o.arguments??"{}"});continue}if(o.type==="function_call_output"){e.push({type:"function_call_output",call_id:o.callId,output:o.output});continue}let r=[];for(let s of o.content??[]){if(s.type==="input_text"||s.type==="output_text"){r.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){r.push({type:"input_text",text:i});continue}n=!0;continue}}e.push({role:o.role,content:r})}return{input:e,sawAudioOnlyItem:n}}var vo=1001,bo=1011,Ln=t=>t.slice(0,120),ko=/^[A-Za-z0-9+/=]*$/,Ro=2;function xo(t){let e=Buffer.from(t,"base64");for(let n=0;n+1<e.byteLength;n+=2)if(Math.abs(e.readInt16LE(n))>Ro)return!1;return!0}var ve=class{id=x("conn");ws;binding;onClosed;config=Nt();sessionId=x("sess");conversationId=x("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:x("event"),session:gt(this.config,this.sessionId)}),this.send({type:"conversation.created",event_id:x("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 o of n()){if(this.closed||e.signal.aborted)return;this.onTurnEvent(o)}}catch(o){!this.closed&&!e.signal.aborted&&this.send(L(De("turn_lane_fault",`the backend turn/transcript lane failed: ${o instanceof Error?o.message:String(o)}`)))}})()}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:x("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:x("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(),o=n.length>0?n:null;this.turnTranscript={open:!1,text:""},this.lastCommittedTranscript=o,this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let r=x("item"),s=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem({id:r,type:"message",role:"user",content:[{type:"input_audio",...o!==null?{transcript:o}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:x("event"),previous_item_id:s,item_id:r}),this.send({type:"conversation.item.created",event_id:x("event"),previous_item_id:s,item:this.itemToProtocol(this.items[this.items.length-1])}),this.send({type:"input_audio_buffer.speech_stopped",event_id:x("event"),item_id:r,audio_end_ms:typeof e.startMs=="number"?e.startMs:0}),this.config.inputAudioTranscription&&o!==null&&this.send({type:"conversation.item.input_audio_transcription.completed",event_id:x("event"),item_id:r,content_index:0,transcript:o}),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")>pt){this.fail(F("frame_too_large",`frame exceeds the ${pt}-byte limit`),"invalid_frame");return}let o;try{o=JSON.parse(n)}catch{this.fail(F("invalid_json","frame is not valid JSON"),"invalid_frame");return}if(typeof o.type!="string"||o.type.length===0){this.fail(F("missing_type",'event is missing a string "type"'),"invalid_frame");return}try{await this.handle(o)}catch(r){let s=r;if(s&&typeof s=="object"&&s.name==="SessionUpdateError"){this.send(L({type:"invalid_request_error",code:s.code??"invalid_value",message:s.message,param:s.param??null}));return}this.fail(De(null,r instanceof Error?r.message:String(r)),"handler_fault")}}async handle(e){switch(e.type){case"session.update":this.config=Ne(e.session,this.config,{voice:this.binding.voice,turns:this.binding.turns!==void 0}),this.send({type:"session.updated",event_id:x("event"),session:gt(this.config,this.sessionId)});return;case"conversation.item.create":{let n=Dt(e.item,this.binding.voice),o=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem(n),this.send({type:"conversation.item.created",event_id:x("event"),previous_item_id:o,item:this.itemToProtocol(n)});return}case"conversation.item.delete":{let n=this.items.findIndex(o=>o.id===e.item_id);if(n<0){this.send(L(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:x("event"),item_id:e.item_id});return}case"conversation.item.retrieve":{let n=this.items.find(o=>o.id===e.item_id);if(!n){this.send(L(F("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.send({type:"conversation.item.retrieved",event_id:x("event"),item:this.itemToProtocol(n)});return}case"conversation.item.truncate":{let n=this.items.find(o=>o.id===e.item_id);if(!n||n.type!=="message"||n.role!=="assistant"){this.send(L(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:x("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(L(F("invalid_audio",'input_audio_buffer.append requires base64 "audio"',"audio")));return}if(!this.ensureVoice())return;if(!ko.test(e.audio)){this.send(L(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>mt){this.send(L(F("buffer_full",`uncommitted input audio exceeds the ${mt}-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:x("event")});return}case"input_audio_buffer.commit":{if(!this.ensureVoice())return;if(this.pendingAudioTotalBytes===0&&this.takeTurnTranscript()===null){this.send(L(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 o=x("item"),r=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem({id:o,type:"message",role:"user",content:[{type:"input_audio",...n!==null?{transcript:n}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:x("event"),previous_item_id:r,item_id:o}),this.send({type:"conversation.item.created",event_id:x("event"),previous_item_id:r,item:this.itemToProtocol(this.items[this.items.length-1])});return}case"output_audio_buffer.clear":{this.send(L(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(L(F("unknown_event",`event type ${e.type} is not supported by this adapter`)))}}ensureVoice(){return!this.binding.voice||!this.binding.audioSession?(this.send(L(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(L({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 o=this.binding.session;if(o&&typeof o.sendResponseCreate=="function"){let s=e.response??e;return o.sendResponseCreate({type:"response.create",response:s},e.input??[],n)}let r=this.binding.clients;if(r&&typeof r.createResponse=="function"){let{response:s,input:i,...a}=e;return await r.createResponse({...a,input:i})}throw new Error("realtime binding provides neither session.sendResponseCreate nor clients.createResponse \u2014 no upstream turn seam")}startResponse(e){if(this.inFlight){this.send(L(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(O=>O!=="text"&&O!=="audio")){this.send(L(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(L(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 o=x("req"),r=`resp_${o}`;this.activeResponseId=r;let s=this.items.length,{input:i,sawAudioOnlyItem:a}=Bt(this.items),l=typeof n.instructions=="string"?n.instructions:this.config.instructions,u=typeof n.temperature=="number"?n.temperature:this.config.temperature,c=n.max_output_tokens??this.config.maxOutputTokens,d=c==="inf"||c===void 0?void 0:c,m=n.tools!==void 0?n.tools:this.config.tools,p=n.tool_choice!==void 0?n.tool_choice:this.config.toolChoice,k={input:i,stream:!0,...this.config.model?{model:this.config.model}:{},...l!==void 0?{instructions:l}:{},...u!==void 0?{temperature:u}:{},...d!==void 0?{max_output_tokens:d}:{},...m!==void 0?{tools:m}:{},...p!==void 0?{tool_choice:p}:{}};for(let O of this.frozenAudio.splice(0))this.binding.audioSession?.audio.appendInputAudio(O);this.inFlight=(async()=>{let O=[],y=S=>{this.responseFlushed?this.send(S):O.push(S)},v=()=>{if(!this.responseFlushed){this.send({type:"response.created",event_id:x("event"),response:this.responseObject(r,"in_progress")}),this.send({type:"response.in_progress",event_id:x("event"),response:this.responseObject(r,"in_progress")});for(let S of O)this.send(S);O.length=0,this.responseFlushed=!0}},_={messageOpen:!1,messageText:"",tools:new Map,nextOutputIndex:0},h=null;try{a&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let S;try{S=await this.upstreamStream(k,o)}catch(g){this.emitUpstreamFault(g,r);return}try{for await(let g of S){if(this.cancelled)break;let b=g;if(b.type==="error"){let f=this.upstreamErrorBody(b);this.responseFlushed?this.send(L(f)):this.send({type:"response.failed",event_id:x("event"),response:this.failedResponse(r,f)});return}if(b.type==="response.output_text.delta"&&typeof b.delta=="string"){v(),_.messageOpen||(this.beginMessageItem(r,_.nextOutputIndex++),_.messageOpen=!0),y({type:"response.output_text.delta",event_id:x("event"),item_id:`${r}_msg_0`,response_id:r,output_index:0,content_index:0,delta:b.delta}),_.messageText+=b.delta;continue}if(b.type==="response.function_call_arguments.delta"){v();let f=typeof b.delta=="string"?b.delta:"",R=typeof b.call_id=="string"&&b.call_id.length>0?b.call_id:`${r}_fc_${_.nextOutputIndex}`,I=_.tools.get(R);if(!I){let $=typeof b.name=="string"&&b.name.length>0?b.name:void 0;I={outputIndex:_.nextOutputIndex++,name:$,args:""},_.tools.set(R,I),y({type:"response.output_item.added",event_id:x("event"),response_id:r,output_index:I.outputIndex,item:{id:R,object:"realtime.item",type:"function_call",call_id:R,name:$??"",arguments:"",status:"in_progress"}})}typeof b.name=="string"&&b.name.length>0&&(I.name=b.name),I.args+=f,y({type:"response.function_call_arguments.delta",event_id:x("event"),item_id:R,response_id:r,output_index:I.outputIndex,call_id:R,name:I.name??"",delta:f});continue}if(b.type==="response.completed"){h=b.response??null;continue}}}catch(g){this.emitUpstreamFault(g,r);return}if(this.cancelled)return;this.closeOpenItems(r,_,s),v(),this.send({type:"response.done",event_id:x("event"),response:this.doneResponse(r,s,h)})}finally{this.inFlight=null,this.activeResponseId=null}})()}onOutputAudioFrame(e,n){if(this.cancelled)return;if(this.activeResponseId!==e){if(xo(n))return;!this.outOfTurnAudioWarned&&!this.closed&&(this.outOfTurnAudioWarned=!0,this.send(L({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:x("event"),response:this.responseObject(e,"in_progress")}),this.send({type:"response.in_progress",event_id:x("event"),response:this.responseObject(e,"in_progress")}),this.responseFlushed=!0);let o=`${e}_msg_0`;this.messageItemOpenForAudio||(this.beginAudioMessageItem(e),this.messageItemOpenForAudio=!0),this.send({type:"response.output_audio.delta",event_id:x("event"),item_id:o,response_id:e,output_index:0,content_index:0,delta:n})}messageItemAdded=!1;messageItemOpenForAudio=!1;beginMessageItem(e,n){let o=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:x("event"),response_id:e,output_index:n,item:{id:o,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:x("event"),item_id:o,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:x("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:x("event"),item_id:n,response_id:e,output_index:0,content_index:0,part:{type:"output_audio",transcript:""}})}closeOpenItems(e,n,o){if(n.messageOpen||this.messageItemOpenForAudio){let r=`${e}_msg_0`,s=this.messageItemOpenForAudio;s&&this.send({type:"response.output_audio.done",event_id:x("event"),item_id:r,response_id:e,output_index:0,content_index:0}),n.messageText.length>0&&this.send({type:"response.output_text.done",event_id:x("event"),item_id:r,response_id:e,output_index:0,content_index:0,text:n.messageText}),this.send({type:"response.content_part.done",event_id:x("event"),item_id:r,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:r,type:"message",role:"assistant",content:i};this.pushItem(a),this.send({type:"response.output_item.done",event_id:x("event"),response_id:e,output_index:0,item:this.itemToProtocol(a)})}for(let[r,s]of n.tools){let i=s.args.length>0?s.args:"{}";this.send({type:"response.function_call_arguments.done",event_id:x("event"),item_id:r,response_id:e,output_index:s.outputIndex,call_id:r,name:s.name??"",arguments:i});let a={id:r,type:"function_call",callId:r,name:s.name,arguments:i};this.pushItem(a),this.send({type:"response.output_item.done",event_id:x("event"),response_id:e,output_index:s.outputIndex,item:this.itemToProtocol(a)})}this.messageItemOpenForAudio=!1,this.messageItemAdded=!1}responseObject(e,n,o){return{id:e,object:"realtime.response",status:n,conversation_id:this.conversationId,usage:null,...o??{}}}failedResponse(e,n){return this.responseObject(e,"failed",{status_details:{error:{code:n.code,message:n.message}}})}doneResponse(e,n,o){let r=o&&typeof o.usage=="object"&&o.usage!==null?o.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:r?{input_tokens:typeof r.input_tokens=="number"&&Number.isFinite(r.input_tokens)?r.input_tokens:0,output_tokens:typeof r.output_tokens=="number"&&Number.isFinite(r.output_tokens)?r.output_tokens:0,total_tokens:typeof r.total_tokens=="number"&&Number.isFinite(r.total_tokens)?r.total_tokens:(typeof r.input_tokens=="number"&&Number.isFinite(r.input_tokens)?r.input_tokens:0)+(typeof r.output_tokens=="number"&&Number.isFinite(r.output_tokens)?r.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,o=n&&typeof n.message=="string"?n.message:JSON.stringify(e);return{type:"server_error",code:n&&typeof n.code=="string"?n.code:null,message:o}}emitUpstreamFault(e,n){if(this.cancelled)return;let o=De(null,e instanceof Error?e.message:String(e));this.responseFlushed?this.send(L(o)):this.send({type:"response.failed",event_id:x("event"),response:this.failedResponse(n,o)})}cancelResponse(){let e=this.inFlight;if(!e){this.send(L(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:x("event"),response:this.responseObject(n,"cancelled")})})}pushItem(e){this.items.push(e),this.items.length>ft&&(this.items.shift(),this.send(L({type:"invalid_request_error",code:"transcript_evicted",message:`the conversation transcript exceeded ${ft} 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 o=(e.content??[]).map(r=>r.type==="input_text"||r.type==="output_text"?{type:r.type,text:r.text}:r.type==="input_audio"?{type:"input_audio",...r.transcript?{transcript:r.transcript}:{}}:{type:"output_audio",transcript:r.transcript??""});return{...n,type:"message",role:e.role,status:e.truncated?"incomplete":"completed",content:o}}send(e){if(!(this.closed||this.ws.readyState!==this.ws.OPEN)){if(this.ws.bufferedAmount>Lt){this.ws.close(vo,Ln("outbound buffer exceeded")),this.teardown("outbound buffer exceeded");return}this.ws.send(JSON.stringify(e))}}fail(e,n){this.send(L(e)),this.ws.close(bo,Ln(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=>Nn(this.id,`binding release failed: ${n instanceof Error?n.message:String(n)}`));try{this.ws.removeAllListeners("message")}catch{}this.onClosed?.(this,{reason:e}),Nn(this.id,e)}}},Ft=[];function Nn(t,e){Ft.push({id:t,reason:e}),Ft.length>256&&Ft.shift()}var Be="/v1/realtime",So=t=>t.slice(0,120);function Eo(t,e,n){let o=e===401||e===403?"Unauthorized":e===502?"Upstream Fault":"Upgrade Refused";t.write(`HTTP/1.1 ${e} ${o}\r
|
|
13
|
+
${o}`)}function qn(t,e){let n=new ge.WebSocketServer({noServer:!0,maxPayload:bo}),r=new _e,o=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,d=!1,u=(l,c,g)=>{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!==vo){i.add(c);return}s.add(c);let v=!1;(async()=>{if(e.apiKey&&(l.headers.authorization??"")!==`Bearer ${e.apiKey}`){pt(c,401,"invalid local proxy api key");return}let A;try{A=await a(l)}catch(R){let y=e.statusOf?.(R)??null;pt(c,y?.status??500,"upstream resolver rejected the connection");return}d||c.destroyed||(v=!0,n.handleUpgrade(l,c,g,R=>{s.delete(c),o.add(R),R.on("close",()=>o.delete(R));try{new Ft(R,A,r,e.statusOf)}catch{o.delete(R),R.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!v&&!c.destroyed&&pt(c,500,"responses websocket upgrade failed")}).finally(()=>s.delete(c))};return t.on("upgrade",u),{stats:r,close:()=>{let{promise:l,resolve:c}=qt();if(d)return c(),l;d=!0,t.off("upgrade",u);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 g=setTimeout(()=>{for(let p of o)p.readyState!==ge.WebSocket.CLOSED&&p.terminate()},2e3);return g.unref?.(),n.close(()=>{clearTimeout(g),c()}),l}}}var Kn=require("@urun-sh/core");var jn=require("@urun-sh/core"),Ao={no_capacity:429,queue_timeout:429,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502},Io={no_capacity:"no ready GPU capacity for this model right now \u2014 the platform will cold-wake it on request; retry",queue_timeout:"the session was admitted but never became live within the cold-wake fence \u2014 the backend is busy or wedged; retry",cold_start_exceeded:"the cold boot exceeded its budget without serving \u2014 the platform keeps pursuing the wake; retry with a longer client timeout",queue_full:"the admission queue for this model is full \u2014 retry after the Retry-After period",admission_busy:"admission is momentarily contended (lock convoy) \u2014 retry shortly",admission_unavailable:"session admission is temporarily unavailable \u2014 retry",backend_error:"the control plane failed while admitting the session"};function jt(t,e){let n=Io[t.code];return e?`${n} (${e})`:n}function ft(t){if(t==null)return null;let e=Ao[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)>=jn.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return ft(e?"cold_start_exceeded":"queue_timeout")}var Oo=Mt(require("os"),1),To=Mt(require("path"),1),Po=require("url"),Co=require("util");var $o=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function Mo(t){let e=t?.code;return typeof e=="string"&&$o.has(e)}function ke(t,e){try{return t.write(e),!0}catch(n){if(Mo(n))return!1;throw n}}var zn=require("ws");var Wn=require("@urun-sh/core");var Vn=require("crypto");function x(t){return`${t}_${(0,Vn.randomUUID)().replace(/-/g,"").slice(0,24)}`}var gt=4*1024*1024,Ht=8*1024*1024,ht=512,yt=20*1024*1024;function j(t,e,n){return{type:"invalid_request_error",code:t,message:e,param:n??null}}function Pe(t,e){return{type:"server_error",code:t,message:e}}function U(t){return{type:"error",event_id:x("event"),error:t}}function Vt(){return{outputModalities:["text"],turnDetection:null,inputAudioTranscription:!1}}var E=class extends Error{constructor(n,r,o="invalid_value"){super(n);this.param=r;this.code=o;this.name="SessionUpdateError"}param;code},Lo={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},No={format:!0,transcription:!0,turn_detection:!0,noise_reduction:!0},Uo={format:!0,voice:!0,speed:!0};function Hn(t,e){let n=t;if(!n||typeof n!="object"||n.type!=="audio/pcm"||n.rate!==void 0&&n.rate!==24e3)throw new E(`${e} must be {"type":"audio/pcm","rate":24000} \u2014 the session audio lane is 24 kHz mono PCM16 only`,e)}function Fe(t,e,n){if(!t||typeof t!="object"||Array.isArray(t))throw new E("session must be an object","session","invalid_request");let r=t,o=Object.keys(r).filter(i=>!(i in Lo));if(o.length>0)throw new E(`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 E("model must be a string or null","model");s.model=a??void 0;break;case"instructions":if(a!==null&&typeof a!="string")throw new E("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 E("modalities must be a non-empty array","modalities");let d=[];for(let u of a){if(u!=="text"&&u!=="audio")throw new E(`modality ${JSON.stringify(u)} is not "text" or "audio"`,"modalities");if(u==="audio"&&!n.voice)throw new E('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");d.includes(u)||d.push(u)}s.outputModalities=d;break}case"voice":if(a===null){delete s.voice;break}if(typeof a!="string")throw new E("voice must be a string or null","voice");if(!n.voice)throw new E("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 E(`${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 E("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 E(`${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 E("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 E("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 E("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 d=a;if(!d||typeof d!="object"||Array.isArray(d))throw new E("turn_detection must be an object or null","turn_detection");let u=String(d.type??"");if(u!=="server_vad"&&u!=="semantic_vad")throw new E(`turn_detection.type ${JSON.stringify(d.type)} is not "server_vad"/"semantic_vad"`,"turn_detection.type");s.turnDetection={createResponse:d.create_response!==!1,interruptResponse:d.interrupt_response!==!1};break}case"input_audio_transcription":{if(a===null){s.inputAudioTranscription=!1;break}if(!n.turns)throw new E("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 E("input_audio_transcription must be an object or null","input_audio_transcription");for(let d of Object.keys(a))if(!["model","language","languages","prompt"].includes(d))throw new E(`input_audio_transcription field not honored by this backend: ${d} (transcription comes from the voice engine itself)`,`input_audio_transcription.${d}`,"unsupported_field");s.inputAudioTranscription=!0;break}case"input_audio_noise_reduction":if(a!==null)throw new E("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 d=a;if(!d||typeof d!="object"||Array.isArray(d))throw new E("audio must be an object or null","audio");if(d.input!==void 0&&d.input!==null){let u=d.input;if(!u||typeof u!="object"||Array.isArray(u))throw new E("audio.input must be an object","audio.input");for(let l of Object.keys(u))if(!(l in No))throw new E(`audio.input field not supported by this backend: ${l}`,`audio.input.${l}`,"unsupported_field");if(u.format!==void 0&&Hn(u.format,"audio.input.format"),u.transcription!==null&&u.transcription!==void 0){let l=Fe({input_audio_transcription:u.transcription},s,n);s.inputAudioTranscription=l.inputAudioTranscription}if(u.turn_detection!==null&&u.turn_detection!==void 0){let l=Fe({turn_detection:u.turn_detection},s,n);s.turnDetection=l.turnDetection}if(u.noise_reduction!==null&&u.noise_reduction!==void 0)throw new E("audio.input.noise_reduction must be null on this backend: no noise-reduction primitive","audio.input.noise_reduction","unsupported_field")}if(d.output!==void 0&&d.output!==null){let u=d.output;if(!u||typeof u!="object"||Array.isArray(u))throw new E("audio.output must be an object","audio.output");for(let l of Object.keys(u))if(!(l in Uo))throw new E(`audio.output field not supported by this backend: ${l}`,`audio.output.${l}`,"unsupported_field");if(u.format!==void 0&&Hn(u.format,"audio.output.format"),u.speed!==void 0)throw new E("audio.output.speed is not supported on this backend: playback rate is fixed","audio.output.speed","unsupported_field");if(u.voice!==void 0)if(u.voice===null)delete s.voice;else{if(typeof u.voice!="string")throw new E("audio.output.voice must be a string or null","audio.output.voice");if(!n.voice)throw new E("audio.output.voice refused: no authoritative voice binding for the resolved backend","audio.output.voice","audio_not_supported");s.voice=u.voice}u.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 Gt(t,e){let n=t;if(!n||typeof n!="object")throw new E("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 E("function_call_output item requires string call_id and output","item.call_id","invalid_request");return{id:typeof n.id=="string"?n.id:x("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 E('message item role must be "user", "assistant" or "system"',"item.role","invalid_request");if(!Array.isArray(n.content))throw new E("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 E("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 E(`${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 E("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 E("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 E(`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:x("item"),type:"message",role:r,content:o}}throw new E(`item type ${String(n.type)} is not supported \u2014 message and function_call_output items only`,"item.type","invalid_request")}function Jt(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 Do=1001,Bo=1011,Gn=t=>t.slice(0,120),Fo=/^[A-Za-z0-9+/=]*$/,qo=2;function jo(t){let e=Buffer.from(t,"base64");for(let n=0;n+1<e.byteLength;n+=2)if(Math.abs(e.readInt16LE(n))>qo)return!1;return!0}var Re=class{id=x("conn");ws;binding;onClosed;config=Vt();sessionId=x("sess");conversationId=x("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:x("event"),session:_t(this.config,this.sessionId)}),this.send({type:"conversation.created",event_id:x("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(Pe("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:x("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:x("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=x("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:x("event"),previous_item_id:s,item_id:o}),this.send({type:"conversation.item.created",event_id:x("event"),previous_item_id:s,item:this.itemToProtocol(this.items[this.items.length-1])}),this.send({type:"input_audio_buffer.speech_stopped",event_id:x("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:x("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(j("decode_error","frame is not decodable text"),"invalid_frame");return}if(Buffer.byteLength(n,"utf8")>gt){this.fail(j("frame_too_large",`frame exceeds the ${gt}-byte limit`),"invalid_frame");return}let r;try{r=JSON.parse(n)}catch{this.fail(j("invalid_json","frame is not valid JSON"),"invalid_frame");return}if(typeof r.type!="string"||r.type.length===0){this.fail(j("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(Pe(null,o instanceof Error?o.message:String(o)),"handler_fault")}}async handle(e){switch(e.type){case"session.update":this.config=Fe(e.session,this.config,{voice:this.binding.voice,turns:this.binding.turns!==void 0}),this.send({type:"session.updated",event_id:x("event"),session:_t(this.config,this.sessionId)});return;case"conversation.item.create":{let n=Gt(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:x("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(j("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:x("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(j("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.send({type:"conversation.item.retrieved",event_id:x("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(j("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:x("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(j("invalid_audio",'input_audio_buffer.append requires base64 "audio"',"audio")));return}if(!this.ensureVoice())return;if(!Fo.test(e.audio)){this.send(U(j("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(j("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:x("event")});return}case"input_audio_buffer.commit":{if(!this.ensureVoice())return;if(this.pendingAudioTotalBytes===0&&this.takeTurnTranscript()===null){this.send(U(j("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=x("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:x("event"),previous_item_id:o,item_id:r}),this.send({type:"conversation.item.created",event_id:x("event"),previous_item_id:o,item:this.itemToProtocol(this.items[this.items.length-1])});return}case"output_audio_buffer.clear":{this.send(U(j("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(j("unknown_event",`event type ${e.type} is not supported by this adapter`)))}}ensureVoice(){return!this.binding.voice||!this.binding.audioSession?(this.send(U(j("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"){let s=e.response??e;return r.sendResponseCreate({type:"response.create",response:s},e.input??[],n)}let o=this.binding.clients;if(o&&typeof o.createResponse=="function"){let{response:s,input:i,...a}=e;return await o.createResponse({...a,input:i})}throw new Error("realtime binding provides neither session.sendResponseCreate nor clients.createResponse \u2014 no upstream turn seam")}startResponse(e){if(this.inFlight){this.send(U(j("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(A=>A!=="text"&&A!=="audio")){this.send(U(j("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(j("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=x("req"),o=`resp_${r}`;this.activeResponseId=o;let s=this.items.length,{input:i,sawAudioOnlyItem:a}=Jt(this.items),d=typeof n.instructions=="string"?n.instructions:this.config.instructions,u=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,g=n.tools!==void 0?n.tools:this.config.tools,p=n.tool_choice!==void 0?n.tool_choice:this.config.toolChoice,v={input:i,stream:!0,...this.config.model?{model:this.config.model}:{},...d!==void 0?{instructions:d}:{},...u!==void 0?{temperature:u}:{},...c!==void 0?{max_output_tokens:c}:{},...g!==void 0?{tools:g}:{},...p!==void 0?{tool_choice:p}:{}};for(let A of this.frozenAudio.splice(0))this.binding.audioSession?.audio.appendInputAudio(A);this.inFlight=(async()=>{let A=[],R=S=>{this.responseFlushed?this.send(S):A.push(S)},y=()=>{if(!this.responseFlushed){this.send({type:"response.created",event_id:x("event"),response:this.responseObject(o,"in_progress")}),this.send({type:"response.in_progress",event_id:x("event"),response:this.responseObject(o,"in_progress")});for(let S of A)this.send(S);A.length=0,this.responseFlushed=!0}},_={messageOpen:!1,messageText:"",tools:new Map,nextOutputIndex:0},h=null;try{a&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let S;try{S=await this.upstreamStream(v,r)}catch(m){this.upstreamTurnFault(m,o);return}try{for await(let m of S){if(this.cancelled)break;let b=m;if(b.type==="error"){let f=this.upstreamErrorBody(b);this.responseFlushed?this.send(U(f)):this.send({type:"response.failed",event_id:x("event"),response:this.failedResponse(o,f)});return}if(b.type==="response.output_text.delta"&&typeof b.delta=="string"){y(),_.messageOpen||(this.beginMessageItem(o,_.nextOutputIndex++),_.messageOpen=!0),R({type:"response.output_text.delta",event_id:x("event"),item_id:`${o}_msg_0`,response_id:o,output_index:0,content_index:0,delta:b.delta}),_.messageText+=b.delta;continue}if(b.type==="response.function_call_arguments.delta"){y();let f=typeof b.delta=="string"?b.delta:"",k=typeof b.call_id=="string"&&b.call_id.length>0?b.call_id:`${o}_fc_${_.nextOutputIndex}`,I=_.tools.get(k);if(!I){let O=typeof b.name=="string"&&b.name.length>0?b.name:void 0;I={outputIndex:_.nextOutputIndex++,name:O,args:""},_.tools.set(k,I),R({type:"response.output_item.added",event_id:x("event"),response_id:o,output_index:I.outputIndex,item:{id:k,object:"realtime.item",type:"function_call",call_id:k,name:O??"",arguments:"",status:"in_progress"}})}typeof b.name=="string"&&b.name.length>0&&(I.name=b.name),I.args+=f,R({type:"response.function_call_arguments.delta",event_id:x("event"),item_id:k,response_id:o,output_index:I.outputIndex,call_id:k,name:I.name??"",delta:f});continue}if(b.type==="response.completed"){h=b.response??null;continue}}}catch(m){this.upstreamTurnFault(m,o);return}if(this.cancelled)return;this.closeOpenItems(o,_,s),y(),this.send({type:"response.done",event_id:x("event"),response:this.doneResponse(o,s,h)})}finally{this.inFlight=null,this.activeResponseId=null}})()}onOutputAudioFrame(e,n){if(this.cancelled)return;if(this.activeResponseId!==e){if(jo(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:x("event"),response:this.responseObject(e,"in_progress")}),this.send({type:"response.in_progress",event_id:x("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:x("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:x("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:x("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:x("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:x("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:x("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:x("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:x("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:x("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:x("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:x("event"),response_id:e,output_index:s.outputIndex,item:this.itemToProtocol(a)})}this.messageItemOpenForAudio=!1,this.messageItemAdded=!1}responseObject(e,n,r){return{id:e,object:"realtime.response",status:n,conversation_id:this.conversationId,usage:null,...r??{}}}failedResponse(e,n){return this.responseObject(e,"failed",{status_details:{error:{code:n.code,message:n.message}}})}doneResponse(e,n,r){let o=r&&typeof r.usage=="object"&&r.usage!==null?r.usage:null,s=[];for(let i=n;i<this.items.length;i++)s.push(this.itemToProtocol(this.items[i]));return this.responseObject(e,"completed",{output:s,usage:o?{input_tokens:typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0,output_tokens:typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0,total_tokens:typeof o.total_tokens=="number"&&Number.isFinite(o.total_tokens)?o.total_tokens:(typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0)+(typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0),input_token_details:{cached_tokens:0,audio_tokens:0,text_tokens:0},output_token_details:{audio_tokens:0,text_tokens:0}}:null})}upstreamErrorBody(e){let n=e.error,r=n&&typeof n.message=="string"?n.message:JSON.stringify(e);return{type:"server_error",code:n&&typeof n.code=="string"?n.code:null,message:r}}failSessionGone(e){let n=e instanceof Error?e.message:String(e);this.fail(Pe("session_gone",`this realtime connection's pooled session \u2014 the one its audio bridge and every turn ride \u2014 is gone: ${n}`),"session_gone")}upstreamTurnFault(e,n){if(e instanceof ee){this.failSessionGone(e);return}if(!this.cancelled){if(!this.responseFlushed&&!(e instanceof Wn.SessionFailedError&&(e.kind==="never-live"||e.kind==="create-failed"))){this.failSessionGone(e);return}this.emitUpstreamFault(e,n)}}emitUpstreamFault(e,n){let r=Pe(null,e instanceof Error?e.message:String(e));this.responseFlushed?this.send(U(r)):this.send({type:"response.failed",event_id:x("event"),response:this.failedResponse(n,r)})}cancelResponse(){let e=this.inFlight;if(!e){this.send(U(j("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:x("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>Ht){this.ws.close(Do,Gn("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(Bo,Gn(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=>Jn(this.id,`binding release failed: ${n instanceof Error?n.message:String(n)}`));try{this.ws.removeAllListeners("message")}catch{}this.onClosed?.(this,{reason:e}),Jn(this.id,e)}}},Wt=[];function Jn(t,e){Wt.push({id:t,reason:e}),Wt.length>256&&Wt.shift()}var qe="/v1/realtime",Ho=t=>t.slice(0,120);function Vo(t,e,n){let r=e===401||e===403?"Unauthorized":e===502?"Upstream Fault":"Upgrade Refused";t.write(`HTTP/1.1 ${e} ${r}\r
|
|
14
14
|
Connection: close\r
|
|
15
15
|
Content-Type: text/plain\r
|
|
16
16
|
\r
|
|
17
|
-
${
|
|
18
|
-
`),t.destroy()}function
|
|
19
|
-
`}function Io(t){we(process.stdout,t)}function Oo(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function qt(t){return t.audioSession?.audio.sessionId??void 0}function Fe(t,e){let n=new Bn.WebSocketServer({noServer:!0}),o=new Set;return t.on("upgrade",(r,s,i)=>{if(new URL(r.url??"/","http://localhost").pathname!==Be)return;o.add(s),s.on("close",()=>{o.delete(s)});let l=x("rtup"),u=Oo(r),c=g=>{(e.log??Io)(Ao(g))};c({stage:"accept",conn:l,model:u}),s.resume();let d=new AbortController,m=null,p=g=>{m??=g,d.abort(new Error(m))},k=()=>p("client socket closed"),O=()=>p("client socket closed (half-close)"),y=g=>p(`client socket error: ${g.message}`);s.once("close",k),s.once("end",O),s.once("error",y);let v=()=>{s.removeListener("close",k),s.removeListener("end",O),s.removeListener("error",y)},_=g=>{c({stage:"close",conn:l,model:u,...g})},h=g=>{let b=g,f=b.session!=null&&typeof b.session.sendResponseCreate=="function",R=b.clients!=null&&typeof b.clients.createResponse=="function";if(!f&&!R)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((I,$)=>{n.handleUpgrade(r,s,i,A=>{try{new ve({ws:A,binding:g,onClosed:(G,J)=>{_({while:"live",reason:J.reason})}}),c({stage:"admit",conn:l,model:u,...qt(g)===void 0?{}:{session:qt(g)}}),I()}catch(G){$(G)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(r,d.signal).then(async g=>{if(d.signal.aborted){v();let b=qt(g);try{await g.release?.()}catch(f){_({while:"queued",...b===void 0?{}:{session:b},reason:m??"client socket closed",release_failed:f instanceof Error?f.message:String(f)});return}_({while:"queued",...b===void 0?{}:{session:b},reason:m??"client socket closed",released:!0}),s.destroy();return}v(),await h(g)}).catch(g=>{let b=g instanceof Dn.SessionFailedError?ct(g):null,f=null;if(g!==null&&typeof g=="object"&&"status"in g){let $=g.status;typeof $=="number"&&(f=$)}let R=f??b?.status??502,I=g instanceof Error?g.message:String(g);_({while:"queued",...d.signal.aborted?{reason:m??"client socket closed"}:{reason:I,status:R}}),s.destroyed||Eo(s,R,I)}).finally(()=>{o.delete(s)})}),async()=>{let r=Date.now()+5e3;for(;o.size>0&&Date.now()<r;)await new Promise(s=>setTimeout(s,25));await new Promise(s=>n.close(()=>s()))}}var qe=(t,e)=>Object.assign(new Error(e),{status:t});function jt(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}async function Ht(t,e,n){if(!t.supportsAudio)throw qe(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 o;try{o=await t.supportsAudio(e)}catch(i){throw i instanceof U?qe(404,i.message):i}if(!o)throw qe(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 qe(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,n)},s={clients:t,voice:o,audioSession:r};return e!==void 0&&(s.model=e),s}function ht(t,e){return{resolve:async(n,o)=>{if(e!==void 0){let r=n.headers.authorization,s=typeof r=="string"?r.trim():"",i=s.startsWith("Bearer ")?s.slice(7).trim():"";if(i===""||i!==e)throw qe(401,"invalid local proxy api key")}return Ht(t,jt(n),o)}}}var Fn="/v1/images/generations",yt="/v1/images/edits";function qn(t){return t===Fn||t===yt}var jn="/v1/images/models";async function Hn(t,e,n){if(t){if(t.model=X({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 o=await e.catalogIdentity(n);if(o!==null){t.catalog={kind:"catalog",model_id:o.model_id,variant:o.variant};return}let r=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:r??n??null}}catch(o){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${o instanceof Error?o.message:String(o)}`}}}}var C=class extends Error{status;param;constructor(e,n,o){super(n),this.status=e,this.param=o}},To=16*1024*1024,Vn=64*1024*1024;function Gn(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 ie=class extends Error{};async function Po(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new ie(`request body exceeds the ${e}-byte limit`);let o=[],r=0;for await(let s of t){let i=s;if(r+=i.length,r>e)throw new ie(`request body exceeds the ${e}-byte limit`);o.push(i)}return Buffer.concat(o)}async function Jn(t,e,n,o){let r=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},o),s=null;for await(let d of r){if(d.type==="error"){let m=d.error??{};throw new C(502,`image upstream error: ${String(m.message??"upstream reported an error")}`)}if(d.type==="response.completed"){s=d.response??null;break}}if(s===null)throw new C(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 C(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=[],l=i[0].startsWith("data:")?i[0].slice(i[0].indexOf(";base64,")+8):i[0],u=Buffer.from(l,"base64");if(Gn(u)===null)throw new C(502,`image upstream returned a payload that is not a PNG/JPEG/WebP image (${u.length} bytes) \u2014 refusing to pass it through`);return a.push(l),{images:a}}function Wn(t){return t instanceof C?t:t instanceof U?new C(404,t.message):new C(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function Kn(t,e,n,o){if(await t(e,n))return!0;let r=n==="edit"?"no image-input (expects_image) edit destination":"no pure text-to-image (expects_image=false) generation destination";return je(o,new C(404,e===void 0?`no image-capable default model is configured for this caller (${r}) \u2014 name an image-capable \`model\``:`model "${e}" is not ${n==="edit"?"edit-capable":"generation-capable"} for this caller (${r}; capability is read from the catalog's modality metadata, not the serve function name)`)),!1}function zn(t,e){t&&(t.outputUnits=e.images.length)}function Yn(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 Co={model:!0,prompt:!0,response_format:!0,n:!0};async function Xn(t,e,n){let o=n.maxBodyBytes??Vn;try{if(t.method!=="POST")throw new C(405,`${String(t.method)} is not supported on ${Fn}`);let r;try{r=JSON.parse((await Po(t,o)).toString("utf8")||"{}")}catch(d){throw d instanceof ie?new C(413,d.message):new C(400,"request body is not valid JSON")}if(r===null||typeof r!="object"||Array.isArray(r))throw new C(400,"request body must be a JSON object");let s=r;for(let d of Object.keys(s))if(!Co[d])throw new C(400,`request field "${d}" 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.`,d);let i=s.prompt;if(typeof i!="string"||i.trim().length===0)throw new C(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 C(400,"model must be a string","model");await Hn(n.turn,n.clients,a);let l=s.n??1;if(l!==1)throw new C(400,`n=${JSON.stringify(l)} is not supported: the native image request is single-shot and produces exactly one image per request (no batch support yet)`,"n");let u=s.response_format??"b64_json";if(u!=="b64_json")throw new C(400,`response_format ${JSON.stringify(u)} 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 Kn(n.imageModels,a,"generation",e))return;let c=[{type:"input_text",text:i}];try{let d=await Jn(n.clients,a,c,n.turn?Z(n.turn):void 0);zn(n.turn,d),Yn(e,d)}catch(d){throw Wn(d)}}catch(r){if(r instanceof ie){je(e,new C(413,r.message));return}if(r instanceof C){je(e,r);return}throw r}}var $o={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function Qn(t,e,n){let o=n.maxBodyBytes??Vn,r=n.maxImagePartBytes??To;try{if(t.method!=="POST")throw new C(405,`${String(t.method)} is not supported on ${yt}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new C(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i=t[Symbol.asyncIterator](),a=0,l=new ReadableStream({async pull(y){try{let v=await i.next();if(v.done){y.close();return}if(a+=v.value.length,a>o){y.error(new ie(`request body exceeds the ${o}-byte limit`));return}y.enqueue(v.value)}catch(v){y.error(v)}},cancel(){i.return?.()}}),u;try{let y=new Headers;for(let[h,S]of Object.entries(t.headers))if(typeof S=="string")y.set(h,S);else if(Array.isArray(S))for(let g of S)y.append(h,g);let v={method:"POST",headers:y,body:l,duplex:"half"};u=await new Request(`https://proxy.internal${t.url??"/"}`,v).formData()}catch(y){throw y instanceof ie?new C(413,y.message):new C(400,`malformed multipart body: ${String(y instanceof Error?y.message:y)}`)}let c=u.get("prompt");if(typeof c!="string"||c.trim().length===0)throw new C(400,"prompt is required and must be a non-empty string field","prompt");let d=u.get("model");if(d!==null&&typeof d!="string")throw new C(400,"model must be a string field","model");let m=d===null?void 0:d;await Hn(n.turn,n.clients,m);let p=u.get("response_format")??"b64_json";if(p!=="b64_json")throw new C(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 y of u.keys())if(!$o[y])throw new C(400,`form field "${y}" 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[]).`,y);let k=[];for(let y of["image","image[]"])for(let v of u.getAll(y)){if(!(v instanceof File))throw new C(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",y);if(v.size>r)throw new C(413,`image part exceeds the ${r}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,y);let _=new Uint8Array(await v.arrayBuffer()),h=Gn(_);if(h===null)throw new C(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",y);k.push({bytes:_,mime:h})}if(k.length===0)throw new C(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",c.length,c.length),!await Kn(n.imageModels,m,"edit",e))return;let O=[{type:"input_text",text:c}];for(let{bytes:y,mime:v}of k)O.push({type:"input_image",image_url:`data:${v};base64,${Buffer.from(y).toString("base64")}`});try{let y=await Jn(n.clients,m,O,n.turn?Z(n.turn):void 0);zn(n.turn,y),Yn(e,y)}catch(y){throw Wn(y)}}catch(s){if(s instanceof ie){je(e,new C(413,s.message));return}if(s instanceof C){je(e,s);return}throw s}}var Ve="/v1/audio/speech";function rr(t){return t===Ve}var Zn=4096,Mo=18e4,Uo=5e3,Lo=64*1024*1024,er=8,tr=5,wt="voice_applied",No="X-Urun-Voice-Applied",P=class extends Error{status;param;retryAfterSeconds;constructor(e,n,o,r){super(n),this.status=e,this.param=o,this.retryAfterSeconds=r}};function Vt(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),e.retryAfterSeconds!==void 0&&t.setHeader("retry-after",String(e.retryAfterSeconds)),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}var Ae=class extends Error{},vt=class extends P{constructor(e){super(502,e)}};async function Do(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new Ae(`request body exceeds the ${e}-byte limit`);let o=[],r=0;for await(let s of t){let i=s;if(r+=i.length,r>e)throw new Ae(`request body exceeds the ${e}-byte limit`);o.push(i)}return Buffer.concat(o)}async function Bo(t,e,n){if(t){if(t.model=X({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 o=await e.catalogIdentity(n);if(o!==null){t.catalog={kind:"catalog",model_id:o.model_id,variant:o.variant};return}let r=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:r??n??null}}catch(o){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${o instanceof Error?o.message:String(o)}`}}}}function Fo(t,e){t&&(t.outputUnits=e/(24e3*2))}var qo=2;function jo(t){for(let e=0;e+1<t.byteLength;e+=2)if(Math.abs(t.readInt16LE(e))>qo)return!1;return!0}function Ho(t){let e=24e3,n=2,o=1,r=Buffer.alloc(44);return r.write("RIFF",0,"ascii"),r.writeUInt32LE(36+t,4),r.write("WAVE",8,"ascii"),r.write("fmt ",12,"ascii"),r.writeUInt32LE(16,16),r.writeUInt16LE(1,20),r.writeUInt16LE(o,22),r.writeUInt32LE(e,24),r.writeUInt32LE(e*o*n,28),r.writeUInt16LE(o*n,32),r.writeUInt16LE(16,34),r.write("data",36,"ascii"),r.writeUInt32LE(t,40),r}var nr=new WeakMap,_t=new WeakMap;function Vo(t,e){let n=_t.get(t)??0;if(n>=er)throw new P(503,`this model's speech queue is full \u2014 ${er} requests are already admitted (one in-flight turn plus the queued depth) and each holds a pooled session turn; retry after ${tr}s`,void 0,tr);_t.set(t,n+1);let o=nr.get(t)??Promise.resolve(),r,s=new Promise(i=>{r=i});return nr.set(t,s),(async()=>{await o;try{return await e()}finally{_t.set(t,(_t.get(t)??1)-1),r()}})()}async function Go(t,e,n,o){if(typeof t.evictSpeechSession!="function")throw new P(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned session after a failed turn \u2014 refusing loudly rather than handing the next request the failed turn\u2019s audio");try{await t.evictSpeechSession(e)}catch(i){if(!(i instanceof Q))throw i}if(n?.return===void 0)return;let r=Promise.resolve(n.return()).then(()=>{},()=>{}),s;await Promise.race([r,new Promise(i=>{s=setTimeout(i,o)})]).finally(()=>{s!==void 0&&clearTimeout(s)})}function He(t){return t instanceof P?t:t instanceof U?new P(404,t.message):new P(502,`speech upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function Jo(t,e,n,o,r,s,i){return Vo(e,async()=>{if(i?.()===!0)return null;let a=[],l=!1,u=0,c=!1,d=!1,m,p=new Promise(S=>{m=S}),k=()=>{c&&!d&&u>=r.quietRunFrames&&(d=!0,m())},O=e.onOutputAudio(S=>{let g=Buffer.from(S,"base64");if(g.byteLength===0)return;let b=jo(g);if(!l){if(b)return;l=!0}a.push(g),u=b?u+1:0,k()}),y=null,v,_,h;try{h=(await t.createResponseOn(n,{model:o.model,input:[{role:"user",content:[{type:"input_text",text:o.text}]}],stream:!1,...o.voice!==void 0?{voice:o.voice}:{}}))[Symbol.asyncIterator]();let g=new Promise(I=>{_=setTimeout(I,s.capMs)}),b=!1;for(;;){let I=h.next(),$=await Promise.race([I,g]);if($===void 0){b=!0,I.catch(()=>{});break}if($.done)break;let A=$.value;if(A.type==="error"){let G=A.error??{};throw await h.return?.(),new P(502,`speech upstream error: ${String(G.message??"upstream reported an error")}`)}if(A.type==="response.completed"){y=A.response??null,c=!0,k(),await h.return?.();break}}if(clearTimeout(_),b)throw new P(504,`the speech turn produced no terminal event within ${s.capMs}ms \u2014 the upstream stream never completed, so the pinned speech session is evicted (the next request opens a fresh one), the serialized lane is released and the caller can retry; a stuck turn must not hold every later request on this lane`);if(y===null)throw new P(502,"speech upstream produced no terminal response \u2014 refusing to fabricate audio");let f=!1;if(await Promise.race([p,new Promise(I=>{v=setTimeout(()=>{f=!0,I()},r.capMs)})]),v!==void 0&&clearTimeout(v),o.voice!==void 0){let I=y[wt];if(typeof I!="string"||I!==o.voice){let $=`the runtime did not acknowledge the requested voice ${JSON.stringify(o.voice)} \u2014 the terminal response carried ${typeof I=="string"?`${wt} ${JSON.stringify(I)}`:`no ${wt} field`}; the speak-bridge companion is not deployed, and this lane never serves default-voice audio as the requested voice`;throw d?new vt($):new P(502,$)}}if(!l)throw new P(502,"the upstream produced no voice for this utterance \u2014 refusing to serve silence as success");if(f&&!d&&u<1)throw new P(502,`the upstream audio stream did not quiesce within ${r.capMs}ms of the turn's end (voiced frames still arriving) \u2014 refusing to serve audio whose tail cannot be vouched for`);let R=u>0?a.slice(0,a.length-u):a;return{pcm:Buffer.concat(R),completed:y}}catch(S){throw S instanceof vt||await Go(t,n,h,s.returnBoundMs),S}finally{v!==void 0&&clearTimeout(v),_!==void 0&&clearTimeout(_),O()}})}async function Wo(t,e,n){let o;try{o=await t(e)}catch(r){throw He(r)}return o?!0:(Vt(n,new P(404,e===void 0?"no speech-capable default model is configured for this caller (catalog task tts) \u2014 name a tts model":`model "${e}" is not speech-capable (catalog task is not tts) \u2014 this lane serves only text-to-speech models; chat models ride /v1/chat/completions`)),!1)}var Ko={model:!0,input:!0,voice:!0,response_format:!0,stream_format:!0,speed:!0,instructions:!0},zo="wav, pcm";async function or(t,e,n){let o=n.maxBodyBytes??Lo,r=!1;e.once("close",()=>{r=!0});let s=()=>r||e.destroyed;try{if(t.method!=="POST")throw new P(405,`${String(t.method)} is not supported on ${Ve}`);let i;try{i=JSON.parse((await Do(t,o)).toString("utf8")||"{}")}catch(h){throw h instanceof Ae?new P(413,h.message):new P(400,"request body is not valid JSON")}if(i===null||typeof i!="object"||Array.isArray(i))throw new P(400,"request body must be a JSON object");let a=i;for(let h of Object.keys(a))if(!Ko[h])throw new P(400,`request field "${h}" is not implemented: the speech request schema carries the utterance and its voice (the qwen3 backend fixes voice style at deploy time, and sample rate is pinned to the engine's native 24 kHz mono). Supported fields: model, input, voice, response_format, stream_format, speed, instructions.`,h);let l=a.input;if(typeof l!="string"||l.trim().length===0)throw new P(400,"input is required and must be a non-empty string","input");if(l.length>Zn)throw new P(400,`input is ${l.length} chars \u2014 over the ${Zn}-char limit (one utterance; the backend bounds generation text-shaped, and an unbounded text is an unbounded synthesis)`,"input");let u=a.model===void 0?void 0:a.model;if(u!==void 0&&typeof u!="string")throw new P(400,"model must be a string","model");let c=a.voice;if(c!==void 0&&(typeof c!="string"||c.trim().length===0))throw new P(400,"voice must be a non-empty string","voice");if(typeof n.clients.speechVoices!="function")throw new P(501,`the speech lane needs a router-backed client (speechVoices) to resolve the voice set GET /v1/models publishes \u2014 refusing loudly rather than validating voices of ${Ve} against a stale copy of the list`);let d;try{d=await n.clients.speechVoices(u)}catch(h){throw He(h)}if(d.defect!==null)throw new P(500,`the voice set for this model cannot be resolved: ${d.defect} \u2014 fix the catalog row; the lane refuses to serve rather than validate against a set nobody vouched`);let m=typeof c=="string"?c:void 0;if(m!==void 0&&d.published.length>0&&!d.published.includes(m))throw new P(400,`voice ${JSON.stringify(m)} is not one of the voices this model publishes \u2014 accepted voices: ${d.published.join(", ")}`,"voice");let p=a.response_format??"wav";if(p!=="wav"&&p!=="pcm")throw new P(400,`response_format ${JSON.stringify(p)} is not supported: this lane serves the engine's raw 24 kHz mono PCM16, so the supported formats are ${zo} \u2014 and the lane will not label bytes with a format it did not encode`,"response_format");if(a.stream_format!==void 0&&typeof a.stream_format!="string")throw new P(400,"stream_format, when present, must be a string","stream_format");let k=a.speed;if(k!==void 0){if(typeof k!="number"||!Number.isFinite(k))throw new P(400,"speed, when present, must be a number","speed");if(k!==1)throw new P(400,`speed=${JSON.stringify(k)} is not supported: the backend exposes no speed knob (the qwen3 engine's speaking rate is fixed), so a non-default speed would be silently ignored \u2014 1 (the OpenAI default) is the only accepted value`,"speed")}let O=a.instructions;if(O!==void 0){if(typeof O!="string")throw new P(400,"instructions, when present, must be a string","instructions");if(O.trim().length>0)throw new P(400,"instructions are not supported: the backend fixes the voice style (instruct) at deploy time, so a per-request instruction would be silently ignored \u2014 the deployment speaks with its configured style","instructions")}await Bo(n.turn,n.clients,u);let y=Buffer.byteLength(JSON.stringify(a));if(n.stats?.request("audio_speech",y,y),!await Wo(n.speechModels,u,e))return;if(typeof n.clients.openAudioOn!="function")throw new P(501,"a speech-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the same session identity as the response turn \u2014 refusing loudly rather than answering an utterance whose audio cannot be collected");if(typeof n.clients.speechSessionHandle!="function")throw new P(501,"a speech-capable model needs a router-backed client (speechSessionHandle) to resolve the speech lane's own pooled session \u2014 one no interactive binding shares, because the per-session audio bridge broadcasts every output frame to every subscriber \u2014 refusing loudly rather than collecting speech audio on a session a realtime/gemini lane can be speaking on");if(typeof n.clients.evictSpeechSession!="function")throw new P(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned speech session after a failed turn (a turn-cap timeout above all) \u2014 without it, the next request on the same model would silently collect the failed turn\u2019s still-emitting audio; refusing loudly rather than serving on an un-evictable speech slot");if(s())return;let v;try{v=await n.clients.speechSessionHandle(u)}catch(h){throw He(h)}let _;try{_=await n.clients.openAudioOn(v)}catch(h){throw He(h)}n.turn&&(n.turn.sessionId=_.sessionId);try{let h=await Jo(n.clients,_,v,{model:u,text:l,...m!==void 0?{voice:m}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??Mo,returnBoundMs:n.turnReturnBoundMs??Uo},s);if(h===null||s())return;if(Fo(n.turn,h.pcm.byteLength),e.statusCode=200,m===void 0){let S=h.completed[wt];typeof S=="string"&&e.setHeader(No,S)}p==="wav"?(e.setHeader("content-type","audio/wav"),e.end(Buffer.concat([Ho(h.pcm.byteLength),h.pcm]))):(e.setHeader("content-type","audio/pcm"),e.end(h.pcm))}catch(h){throw He(h)}}catch(i){if(i instanceof Ae){Vt(e,new P(413,i.message));return}if(i instanceof P){Vt(e,i);return}throw i}}var ir="/v1/usage/requests",sr=1e4,Yo=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,ce=class extends Error{constructor(n,o=400){super(n);this.status=o;this.name="UsageRequestError"}status},Ie=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}};var ar=1024*1024;function Gt(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new ce(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new ce(`"${e}" must be an array of uuid strings`);if(n.length>sr)throw new ce(`"${e}" carries ${n.length} ids; at most ${sr} may be looked up in one request. Split the batch \u2014 the ids past the limit are NOT silently dropped`);let o=[],r=new Set;for(let s of n){if(typeof s!="string"||!Yo.test(s))throw new ce(`"${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`);r.has(s)||(r.add(s),o.push(s))}return o}function ur(t){return Gt(t,"inference_ids")}function bt(t,e){let n=new Set(e),o=new Set;for(let r=0;r<t.length;r++){let s=t[r];if(!n.has(s.inference_id))throw new Ie(`the usage surface returned an id at validated answer position ${r} 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(o.has(s.inference_id))throw new Ie(`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`);o.add(s.inference_id)}}var St=require("@urun-sh/core");var Jt="/partners/huggingface/v1/billing/requests";function Xo(t){return Gt(t,"requestIds")}function Qo(t){return t>=200&&t<=399}function Zo(t){let e=[];for(let n of t)n.cost_nano_usd!==null&&Qo(n.http_status)&&e.push({requestId:n.inference_id,costNanoUsd:n.cost_nano_usd});return{requests:e.length>0?e:null}}async function dr(t,e){let n=Xo(t),o=await e(n);return bt(o,n),Zo(o)}var es={"/v1/chat/completions":"conversational","/v1/responses":"conversational","/v1/messages":"conversational","/v1/systemone":"conversational","/v1/images/generations":"text-to-image","/v1/images/edits":"image-to-image","/v1/audio/speech":"text-to-speech"};function ts(t){return es[t]??null}var fr=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function ns(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function rs(t){return t==="surface"}function os(t){let e=t?.ledgerFailureReason;return fr.includes(e)?e:"unknown"}var ss=["no_response_head","no_org","model_unresolved","no_model_named","queue_full"],lr=["priced","unpriced"],is={written:lr,already_written:lr,skipped:ss,failed:fr},Pa=Object.entries(is).flatMap(([t,e])=>e.map(n=>({outcome:t,reason:n})));function as(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function us(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 ds=256,cr=4096,Je=12e4,ls=250,cs=8e3,We=[],pr=new Set,Ke=0,Ge=!1;var ps=new Set;function fs(){return Ke===0&&We.length===0}function ms(){if(fs()){Ge=!1;for(let t of[...ps])t()}}function mr(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
|
|
20
|
-
`}function gs(t){we(process.stdout,t)}function gr(t,e){try{t(e)}catch{}}function hr(t){let{record:e,ledger:n}=t;if(n===null)return;let o=n.report??gs,r=ts(e.path);if(r===null)return;let s=c=>{try{n.observe(c)}catch{}},i=(c,d)=>{s({outcome:"skipped",reason:c}),gr(o,mr("inference_ledger_skipped",{inference_id:e.inference_id,path:e.path,reason:c,detail:d}))};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 l;if(a.kind==="catalog")l={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}l={model_id:a.model,identity_kind:"caller_org_app",variant:null}}let u={inference_id:e.inference_id,...l,task:r,http_status:e.status,started_at:us(e),completed_at:e.ts,billable_outcome:as(e),session_id:e.session_id,prompt_tokens:e.prompt_tokens,completion_tokens:e.completion_tokens,output_units:e.output_units,queue_ms:e.queue_ms,prefill_ms:e.prefill_ms,decode_ms:e.decode_ms,total_ms:e.total_ms,ttft_ms:e.ttft_ms===null?null:Math.round(e.ttft_ms),e2e_ms:Math.round(e.duration_ms)};if(We.length>=cr){i("queue_full",`${We.length} ledger writes are already waiting (capacity ${cr}, ${Ke} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}We.push({entry:u,req:t.req,record:e,ledger:n,report:o,queuedAt:Date.now(),attempts:0,settled:!1}),Ke<ds&&(Ke+=1,hs())}async function hs(){try{for(;;){let t=ys();if(t===void 0)return;pr.add(t);try{await _s(t)}finally{pr.delete(t)}}}finally{Ke-=1,ms()}}function ys(){let t=Date.now();for(;;){let e=We.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<Je)return e;Oe(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 ${Je}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 _s(t){let{entry:e,record:n,ledger:o}=t;for(;;){t.attempts+=1;try{let r=await o.write(t.req,e);Oe(t,{outcome:"written",reason:r.cost_nano_usd===null?"unpriced":"priced"},{event:"inference_ledger_written",inference_id:r.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:r.cost_nano_usd,price_version:r.price_version,unpriced_reason:r.unpriced_reason,attempts:t.attempts});return}catch(r){let s=ns(r);if(s!==null){Oe(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=os(r),a=Date.now()-t.queuedAt;if(Ge){Oe(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: ${r instanceof Error?r.message:String(r)}`});return}if(rs(i)&&a<Je){if(await vs(ws(t.attempts)),Ge||Date.now()-t.queuedAt>=Je){let l=Date.now()-t.queuedAt;Oe(t,{outcome:"failed",reason:Ge?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:Ge?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:l,error:`this row's backoff outlived the ${Je}ms dwell bound (waited ${l}ms over ${t.attempts} attempt(s)) and was given up on rather than starting an attempt that could only finish past it. Last failure: ${r instanceof Error?r.message:String(r)}`});return}continue}Oe(t,{outcome:"failed",reason:i},{event:"inference_ledger_write_failed",reason:i,attempts:t.attempts,waited_ms:a,error:r instanceof Error?r.message:String(r)});return}}}function ws(t){return Math.min(cs,ls*2**Math.max(0,t-1))}function vs(t){return new Promise(e=>{setTimeout(e,t).unref?.()})}function Oe(t,e,n){if(t.settled)return;t.settled=!0;try{t.ledger.observe(e)}catch{}let{event:o,...r}=n;gr(t.report,mr(o,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...r}))}var bs=At(require("crypto"),1);var ks=6e4,Ma=ks/1e3+5;var yr=15;var Ua=yr*1e3,La=yr*500;var ze=class extends Error{constructor(n,o){super(`runtime answered ${n}`);this.status=n;this.body=o}status;body},Ye=class extends Error{};function zt(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function j(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function H(t,e,n,o="invalid_request_error",r){j(t,e,{error:r?{message:n,type:o,code:r}:{message:n,type:o}})}function Yt(t,e){let n=e.error,o=n?.type??"upstream_error",r=n?.message??JSON.stringify(e);j(t,o==="invalid_request_error"?400:502,{error:{message:r,type:o,code:n?.code??null}})}function te(t,e,n,o){j(t,e,{type:"error",error:{type:n,message:o}})}function ne(t,e,n){if(e==="anthropic"){j(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}j(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var Rs=64*1024*1024,Xe=class extends Error{};async function _r(t,e=Rs){let n=[],o=0;for await(let s of t){if(o+=s.length,o>e)throw new Xe(`request body exceeds ${e} bytes`);n.push(s)}let r=Buffer.concat(n).toString("utf8");return r?JSON.parse(r):{}}function Te(t,e,n,o){return{id:t,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:e,choices:[{index:0,delta:n,finish_reason:o}]}}function br(t){let e=t?.usage;if(!e||typeof e!="object")return{kind:"absent"};let n=e,o=l=>typeof l=="number"&&Number.isFinite(l)&&l>=0?l:null,r=o(n.input_tokens),s=o(n.output_tokens),i=l=>l??0,a=o(n.total_tokens)??i(r)+i(s);if(i(r)>0||i(s)>0){let l={input_tokens:i(r),output_tokens:i(s),total_tokens:a};return r===null||s===null?{kind:"partial",usage:l}:{kind:"counts",usage:l}}return r===null||s===null?{kind:"absent"}:{kind:"zero",usage:{input_tokens:0,output_tokens:0,total_tokens:a}}}function kt(t){let e=br(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function wr(t){let e=kt(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 xs(t,e){let n=br(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function Ss(t){let e=t?.urun_timing;if(!e||typeof e!="object")return null;let n=e,o=l=>{let u=n[l];return typeof u=="number"&&Number.isSafeInteger(u)&&u>=0?u:null},r=o("queue_ms"),s=o("prefill_ms"),i=o("decode_ms"),a=o("total_ms");return r===null||s===null||i===null||a===null?null:{queue_ms:r,prefill_ms:s,decode_ms:i,total_ms:a}}function Es(t,e){let n=Ss(e);n&&(t.timing=n)}function ae(t,e){xs(t,e),Es(t,e)}async function Et(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 o=await e.catalogIdentity(n);if(o!==null){t.catalog={kind:"catalog",model_id:o.model_id,variant:o.variant};return}let r=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:r??n??null}}catch(o){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${o instanceof Error?o.message:String(o)}`}}}function kr(t){let e=t?.output;if(!Array.isArray(e))return"";let n="";for(let o of e)if(!(o.type!=="reasoning"||!Array.isArray(o.content)))for(let r of o.content)r.type==="reasoning_text"&&(n+=String(r.text??""));return n}function As(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 Rt=class extends Error{};function Is(t,e){return Array.isArray(e)?e.map(n=>{let o=n;if(o.type==="text")return{type:t==="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 Rt(`unsupported chat content part type "${String(o.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function Os(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 o of n.tool_calls){let r=o.function??{};e.push({type:"function_call",call_id:o.id,name:r.name,arguments:r.arguments})}continue}e.push({...n,content:Is(n.role,n.content)})}return e}function Wt(t){return xe(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 Ts(t,e,n,o,r,s){let i=Me(s.id),a=t.stream===!0;if(s.model=X(t),s.stream=a,await Et(s,e,typeof t.model=="string"?t.model:void 0),a){let v;try{v=await Le({body:t,clients:e,store:n,stats:r,responseId:i,turn:s})}catch(_){if(_ instanceof U){ne(o,"openai",_);return}if(_ instanceof Y){H(o,_.status,_.message);return}throw _}zt(o);for await(let _ of v.events)_.type==="error"&&(s.streamError=!0),_.type==="response.completed"&&ae(s,_.response),o.write(`event: ${String(_.type??"message")}
|
|
17
|
+
${Ho(n)}\r
|
|
18
|
+
`),t.destroy()}function Go(t){return`${JSON.stringify({event:"realtime_upgrade",ts:new Date().toISOString(),...t})}
|
|
19
|
+
`}function Jo(t){ke(process.stdout,t)}function Wo(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function Kt(t){return t.audioSession?.audio.sessionId??void 0}function je(t,e){let n=new zn.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,s,i)=>{if(new URL(o.url??"/","http://localhost").pathname!==qe)return;r.add(s),s.on("close",()=>{r.delete(s)});let d=x("rtup"),u=Wo(o),l=m=>{(e.log??Jo)(Go(m))};l({stage:"accept",conn:d,model:u}),s.resume();let c=new AbortController,g=null,p=m=>{g??=m,c.abort(new Error(g))},v=()=>p("client socket closed"),A=()=>p("client socket closed (half-close)"),R=m=>p(`client socket error: ${m.message}`);s.once("close",v),s.once("end",A),s.once("error",R);let y=()=>{s.removeListener("close",v),s.removeListener("end",A),s.removeListener("error",R)},_=m=>{l({stage:"close",conn:d,model:u,...m})},h=m=>{let b=m,f=b.session!=null&&typeof b.session.sendResponseCreate=="function",k=b.clients!=null&&typeof b.clients.createResponse=="function";if(!f&&!k)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((I,O)=>{n.handleUpgrade(o,s,i,C=>{try{new Re({ws:C,binding:m,onClosed:(J,z)=>{_({while:"live",reason:z.reason})}}),l({stage:"admit",conn:d,model:u,...Kt(m)===void 0?{}:{session:Kt(m)}}),I()}catch(J){O(J)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o,c.signal).then(async m=>{if(c.signal.aborted){y();let b=Kt(m);try{await m.release?.()}catch(f){_({while:"queued",...b===void 0?{}:{session:b},reason:g??"client socket closed",release_failed:f instanceof Error?f.message:String(f)});return}_({while:"queued",...b===void 0?{}:{session:b},reason:g??"client socket closed",released:!0}),s.destroy();return}y(),await h(m)}).catch(m=>{let b=m instanceof Kn.SessionFailedError?mt(m):null,f=null;if(m!==null&&typeof m=="object"&&"status"in m){let O=m.status;typeof O=="number"&&(f=O)}let k=f??b?.status??502,I=m instanceof Error?m.message:String(m);_({while:"queued",...c.signal.aborted?{reason:g??"client socket closed"}:{reason:I,status:k}}),s.destroyed||Vo(s,k,I)}).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 He=(t,e)=>Object.assign(new Error(e),{status:t});function zt(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}async function Yt(t,e,n){if(!t.supportsAudio)throw He(501,"the realtime lane needs a router-backed client (supportsAudio) to gate audio modality \u2014 hand-built clients without the catalog oracle are refused, never allowed silently");let r;try{r=await t.supportsAudio(e)}catch(a){throw a instanceof N?He(404,a.message):a}if(!r)throw He(404,`model ${JSON.stringify(e??"(default)")} is not audio-capable (catalog task is neither stt nor tts) \u2014 this realtime lane serves only audio-task models; chat models ride /v1/chat/completions`);if(!t.openAudioOn)throw He(501,"an audio-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the connection\u2019s pooled session \u2014 refusing loudly rather than binding a voiceless session");let o=await t.sessionHandle(e),s={audio:await t.openAudioOn(o,n)},i={clients:{createResponse:async a=>await t.createResponseOn(o,a)},voice:r,audioSession:s};return e!==void 0&&(i.model=e),i}function wt(t,e){return{resolve:async(n,r)=>{if(e!==void 0){let o=n.headers.authorization,s=typeof o=="string"?o.trim():"",i=s.startsWith("Bearer ")?s.slice(7).trim():"";if(i===""||i!==e)throw He(401,"invalid local proxy api key")}return Yt(t,zt(n),r)}}}var K=class extends Error{};async function vt(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new K(`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 K(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function bt(t,e){let n=t[Symbol.asyncIterator](),r=0,o=new ReadableStream({async pull(i){try{let a=await n.next();if(a.done){i.close();return}if(r+=a.value.length,r>e){i.error(new K(`request body exceeds the ${e}-byte limit`));return}i.enqueue(a.value)}catch(a){i.error(a)}},cancel(){n.return?.()}}),s;try{let i=new Headers;for(let[u,l]of Object.entries(t.headers))if(typeof l=="string")i.set(u,l);else if(Array.isArray(l))for(let c of l)i.append(u,c);let a={method:"POST",headers:i,body:o,duplex:"half"};s=await new Request(`https://proxy.internal${t.url??"/"}`,a).formData()}catch(i){throw i instanceof K?i:new Error(`malformed multipart body: ${String(i instanceof Error?i.message:i)}`)}return s}var Yn="/v1/images/generations",kt="/v1/images/edits";function Xn(t){return t===Yn||t===kt}var Qn="/v1/images/models";async function Zn(t,e,n){if(t){if(t.model=Q({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 $=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},Ko=16*1024*1024,er=64*1024*1024;function tr(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 Ve(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}async function nr(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 g=c.error??{};throw new $(502,`image upstream error: ${String(g.message??"upstream reported an error")}`)}if(c.type==="response.completed"){s=c.response??null;break}}if(s===null)throw new $(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 $(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=[],d=i[0].startsWith("data:")?i[0].slice(i[0].indexOf(";base64,")+8):i[0],u=Buffer.from(d,"base64");if(tr(u)===null)throw new $(502,`image upstream returned a payload that is not a PNG/JPEG/WebP image (${u.length} bytes) \u2014 refusing to pass it through`);return a.push(d),{images:a}}function rr(t){return t instanceof $?t:t instanceof N?new $(404,t.message):new $(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function or(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 Ve(r,new $(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 sr(t,e){t&&(t.outputUnits=e.images.length)}function ir(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 zo={model:!0,prompt:!0,response_format:!0,n:!0};async function ar(t,e,n){let r=n.maxBodyBytes??er;try{if(t.method!=="POST")throw new $(405,`${String(t.method)} is not supported on ${Yn}`);let o;try{o=JSON.parse((await vt(t,r)).toString("utf8")||"{}")}catch(c){throw c instanceof K?new $(413,c.message):new $(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new $(400,"request body must be a JSON object");let s=o;for(let c of Object.keys(s))if(!zo[c])throw new $(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 $(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 $(400,"model must be a string","model");await Zn(n.turn,n.clients,a);let d=s.n??1;if(d!==1)throw new $(400,`n=${JSON.stringify(d)} is not supported: the native image request is single-shot and produces exactly one image per request (no batch support yet)`,"n");let u=s.response_format??"b64_json";if(u!=="b64_json")throw new $(400,`response_format ${JSON.stringify(u)} 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 or(n.imageModels,a,"generation",e))return;let l=[{type:"input_text",text:i}];try{let c=await nr(n.clients,a,l,n.turn?te(n.turn):void 0);sr(n.turn,c),ir(e,c)}catch(c){throw rr(c)}}catch(o){if(o instanceof K){Ve(e,new $(413,o.message));return}if(o instanceof $){Ve(e,o);return}throw o}}var Yo={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function ur(t,e,n){let r=n.maxBodyBytes??er,o=n.maxImagePartBytes??Ko;try{if(t.method!=="POST")throw new $(405,`${String(t.method)} is not supported on ${kt}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new $(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await bt(t,r)}catch(p){throw p instanceof K?new $(413,p.message):new $(400,String(p instanceof Error?p.message:p))}let a=i.get("prompt");if(typeof a!="string"||a.trim().length===0)throw new $(400,"prompt is required and must be a non-empty string field","prompt");let d=i.get("model");if(d!==null&&typeof d!="string")throw new $(400,"model must be a string field","model");let u=d===null?void 0:d;await Zn(n.turn,n.clients,u);let l=i.get("response_format")??"b64_json";if(l!=="b64_json")throw new $(400,`response_format ${JSON.stringify(l)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");for(let p of i.keys())if(!Yo[p])throw new $(400,`form field "${p}" 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[]).`,p);let c=[];for(let p of["image","image[]"])for(let v of i.getAll(p)){if(!(v instanceof File))throw new $(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",p);if(v.size>o)throw new $(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,p);let A=new Uint8Array(await v.arrayBuffer()),R=tr(A);if(R===null)throw new $(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",p);c.push({bytes:A,mime:R})}if(c.length===0)throw new $(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",a.length,a.length),!await or(n.imageModels,u,"edit",e))return;let g=[{type:"input_text",text:a}];for(let{bytes:p,mime:v}of c)g.push({type:"input_image",image_url:`data:${v};base64,${Buffer.from(p).toString("base64")}`});try{let p=await nr(n.clients,u,g,n.turn?te(n.turn):void 0);sr(n.turn,p),ir(e,p)}catch(p){throw rr(p)}}catch(s){if(s instanceof K){Ve(e,new $(413,s.message));return}if(s instanceof $){Ve(e,s);return}throw s}}var Je="/v1/audio/speech";function fr(t){return t===Je}var dr=4096,Xo=18e4,Qo=5e3,Zo=64*1024*1024,lr=8,cr=5,xt="voice_applied",es="X-Urun-Voice-Applied",T=class extends Error{status;param;retryAfterSeconds;constructor(e,n,r,o){super(n),this.status=e,this.param=r,this.retryAfterSeconds=o}};function Xt(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),e.retryAfterSeconds!==void 0&&t.setHeader("retry-after",String(e.retryAfterSeconds)),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}var St=class extends T{constructor(e){super(502,e)}};async function ts(t,e,n){if(t){if(t.model=Q({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}function ns(t,e){t&&(t.outputUnits=e/(24e3*2))}var rs=2;function os(t){for(let e=0;e+1<t.byteLength;e+=2)if(Math.abs(t.readInt16LE(e))>rs)return!1;return!0}function ss(t){let e=24e3,n=2,r=1,o=Buffer.alloc(44);return o.write("RIFF",0,"ascii"),o.writeUInt32LE(36+t,4),o.write("WAVE",8,"ascii"),o.write("fmt ",12,"ascii"),o.writeUInt32LE(16,16),o.writeUInt16LE(1,20),o.writeUInt16LE(r,22),o.writeUInt32LE(e,24),o.writeUInt32LE(e*r*n,28),o.writeUInt16LE(r*n,32),o.writeUInt16LE(16,34),o.write("data",36,"ascii"),o.writeUInt32LE(t,40),o}var pr=new WeakMap,Rt=new WeakMap;function is(t,e){let n=Rt.get(t)??0;if(n>=lr)throw new T(503,`this model's speech queue is full \u2014 ${lr} requests are already admitted (one in-flight turn plus the queued depth) and each holds a pooled session turn; retry after ${cr}s`,void 0,cr);Rt.set(t,n+1);let r=pr.get(t)??Promise.resolve(),o,s=new Promise(i=>{o=i});return pr.set(t,s),(async()=>{await r;try{return await e()}finally{Rt.set(t,(Rt.get(t)??1)-1),o()}})()}async function as(t,e,n,r){if(typeof t.evictSpeechSession!="function")throw new T(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned session after a failed turn \u2014 refusing loudly rather than handing the next request the failed turn\u2019s audio");try{await t.evictSpeechSession(e)}catch(i){if(!(i instanceof ee))throw i}if(n?.return===void 0)return;let o=Promise.resolve(n.return()).then(()=>{},()=>{}),s;await Promise.race([o,new Promise(i=>{s=setTimeout(i,r)})]).finally(()=>{s!==void 0&&clearTimeout(s)})}function Ge(t){return t instanceof T?t:t instanceof N?new T(404,t.message):new T(502,`speech upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function us(t,e,n,r,o,s,i){return is(e,async()=>{if(i?.()===!0)return null;let a=[],d=!1,u=0,l=!1,c=!1,g,p=new Promise(S=>{g=S}),v=()=>{l&&!c&&u>=o.quietRunFrames&&(c=!0,g())},A=e.onOutputAudio(S=>{let m=Buffer.from(S,"base64");if(m.byteLength===0)return;let b=os(m);if(!d){if(b)return;d=!0}a.push(m),u=b?u+1:0,v()}),R=null,y,_,h;try{h=(await t.createResponseOn(n,{model:r.model,input:[{role:"user",content:[{type:"input_text",text:r.text}]}],stream:!1,...r.voice!==void 0?{voice:r.voice}:{}}))[Symbol.asyncIterator]();let m=new Promise(I=>{_=setTimeout(I,s.capMs)}),b=!1;for(;;){let I=h.next(),O=await Promise.race([I,m]);if(O===void 0){b=!0,I.catch(()=>{});break}if(O.done)break;let C=O.value;if(C.type==="error"){let J=C.error??{};throw await h.return?.(),new T(502,`speech upstream error: ${String(J.message??"upstream reported an error")}`)}if(C.type==="response.completed"){R=C.response??null,l=!0,v(),await h.return?.();break}}if(clearTimeout(_),b)throw new T(504,`the speech turn produced no terminal event within ${s.capMs}ms \u2014 the upstream stream never completed, so the pinned speech session is evicted (the next request opens a fresh one), the serialized lane is released and the caller can retry; a stuck turn must not hold every later request on this lane`);if(R===null)throw new T(502,"speech upstream produced no terminal response \u2014 refusing to fabricate audio");let f=!1;if(await Promise.race([p,new Promise(I=>{y=setTimeout(()=>{f=!0,I()},o.capMs)})]),y!==void 0&&clearTimeout(y),r.voice!==void 0){let I=R[xt];if(typeof I!="string"||I!==r.voice){let O=`the runtime did not acknowledge the requested voice ${JSON.stringify(r.voice)} \u2014 the terminal response carried ${typeof I=="string"?`${xt} ${JSON.stringify(I)}`:`no ${xt} field`}; the speak-bridge companion is not deployed, and this lane never serves default-voice audio as the requested voice`;throw c?new St(O):new T(502,O)}}if(!d)throw new T(502,"the upstream produced no voice for this utterance \u2014 refusing to serve silence as success");if(f&&!c&&u<1)throw new T(502,`the upstream audio stream did not quiesce within ${o.capMs}ms of the turn's end (voiced frames still arriving) \u2014 refusing to serve audio whose tail cannot be vouched for`);let k=u>0?a.slice(0,a.length-u):a;return{pcm:Buffer.concat(k),completed:R}}catch(S){throw S instanceof St||await as(t,n,h,s.returnBoundMs),S}finally{y!==void 0&&clearTimeout(y),_!==void 0&&clearTimeout(_),A()}})}async function ds(t,e,n){let r;try{r=await t(e)}catch(o){throw Ge(o)}return r?!0:(Xt(n,new T(404,e===void 0?"no speech-capable default model is configured for this caller (catalog task tts) \u2014 name a tts model":`model "${e}" is not speech-capable (catalog task is not tts) \u2014 this lane serves only text-to-speech models; chat models ride /v1/chat/completions`)),!1)}var ls={model:!0,input:!0,voice:!0,response_format:!0,stream_format:!0,speed:!0,instructions:!0},cs="wav, pcm";async function mr(t,e,n){let r=n.maxBodyBytes??Zo,o=!1;e.once("close",()=>{o=!0});let s=()=>o||e.destroyed;try{if(t.method!=="POST")throw new T(405,`${String(t.method)} is not supported on ${Je}`);let i;try{i=JSON.parse((await vt(t,r)).toString("utf8")||"{}")}catch(h){throw h instanceof K?new T(413,h.message):new T(400,"request body is not valid JSON")}if(i===null||typeof i!="object"||Array.isArray(i))throw new T(400,"request body must be a JSON object");let a=i;for(let h of Object.keys(a))if(!ls[h])throw new T(400,`request field "${h}" is not implemented: the speech request schema carries the utterance and its voice (the qwen3 backend fixes voice style at deploy time, and sample rate is pinned to the engine's native 24 kHz mono). Supported fields: model, input, voice, response_format, stream_format, speed, instructions.`,h);let d=a.input;if(typeof d!="string"||d.trim().length===0)throw new T(400,"input is required and must be a non-empty string","input");if(d.length>dr)throw new T(400,`input is ${d.length} chars \u2014 over the ${dr}-char limit (one utterance; the backend bounds generation text-shaped, and an unbounded text is an unbounded synthesis)`,"input");let u=a.model===void 0?void 0:a.model;if(u!==void 0&&typeof u!="string")throw new T(400,"model must be a string","model");let l=a.voice;if(l!==void 0&&(typeof l!="string"||l.trim().length===0))throw new T(400,"voice must be a non-empty string","voice");if(typeof n.clients.speechVoices!="function")throw new T(501,`the speech lane needs a router-backed client (speechVoices) to resolve the voice set GET /v1/models publishes \u2014 refusing loudly rather than validating voices of ${Je} against a stale copy of the list`);let c;try{c=await n.clients.speechVoices(u)}catch(h){throw Ge(h)}if(c.defect!==null)throw new T(500,`the voice set for this model cannot be resolved: ${c.defect} \u2014 fix the catalog row; the lane refuses to serve rather than validate against a set nobody vouched`);let g=typeof l=="string"?l:void 0;if(g!==void 0&&c.published.length>0&&!c.published.includes(g))throw new T(400,`voice ${JSON.stringify(g)} is not one of the voices this model publishes \u2014 accepted voices: ${c.published.join(", ")}`,"voice");let p=a.response_format??"wav";if(p!=="wav"&&p!=="pcm")throw new T(400,`response_format ${JSON.stringify(p)} is not supported: this lane serves the engine's raw 24 kHz mono PCM16, so the supported formats are ${cs} \u2014 and the lane will not label bytes with a format it did not encode`,"response_format");if(a.stream_format!==void 0&&typeof a.stream_format!="string")throw new T(400,"stream_format, when present, must be a string","stream_format");let v=a.speed;if(v!==void 0){if(typeof v!="number"||!Number.isFinite(v))throw new T(400,"speed, when present, must be a number","speed");if(v!==1)throw new T(400,`speed=${JSON.stringify(v)} is not supported: the backend exposes no speed knob (the qwen3 engine's speaking rate is fixed), so a non-default speed would be silently ignored \u2014 1 (the OpenAI default) is the only accepted value`,"speed")}let A=a.instructions;if(A!==void 0){if(typeof A!="string")throw new T(400,"instructions, when present, must be a string","instructions");if(A.trim().length>0)throw new T(400,"instructions are not supported: the backend fixes the voice style (instruct) at deploy time, so a per-request instruction would be silently ignored \u2014 the deployment speaks with its configured style","instructions")}await ts(n.turn,n.clients,u);let R=Buffer.byteLength(JSON.stringify(a));if(n.stats?.request("audio_speech",R,R),!await ds(n.speechModels,u,e))return;if(typeof n.clients.openAudioOn!="function")throw new T(501,"a speech-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the same session identity as the response turn \u2014 refusing loudly rather than answering an utterance whose audio cannot be collected");if(typeof n.clients.speechSessionHandle!="function")throw new T(501,"a speech-capable model needs a router-backed client (speechSessionHandle) to resolve the speech lane's own pooled session \u2014 one no interactive binding shares, because the per-session audio bridge broadcasts every output frame to every subscriber \u2014 refusing loudly rather than collecting speech audio on a session a realtime/gemini lane can be speaking on");if(typeof n.clients.evictSpeechSession!="function")throw new T(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned speech session after a failed turn (a turn-cap timeout above all) \u2014 without it, the next request on the same model would silently collect the failed turn\u2019s still-emitting audio; refusing loudly rather than serving on an un-evictable speech slot");if(s())return;let y;try{y=await n.clients.speechSessionHandle(u)}catch(h){throw Ge(h)}let _;try{_=await n.clients.openAudioOn(y)}catch(h){throw Ge(h)}n.turn&&(n.turn.sessionId=_.sessionId);try{let h=await us(n.clients,_,y,{model:u,text:d,...g!==void 0?{voice:g}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??Xo,returnBoundMs:n.turnReturnBoundMs??Qo},s);if(h===null||s())return;if(ns(n.turn,h.pcm.byteLength),e.statusCode=200,g===void 0){let S=h.completed[xt];typeof S=="string"&&e.setHeader(es,S)}p==="wav"?(e.setHeader("content-type","audio/wav"),e.end(Buffer.concat([ss(h.pcm.byteLength),h.pcm]))):(e.setHeader("content-type","audio/pcm"),e.end(h.pcm))}catch(h){throw Ge(h)}}catch(i){if(i instanceof K){Xt(e,new T(413,i.message));return}if(i instanceof T){Xt(e,i);return}throw i}}var Et="/v1/audio/transcriptions";function gr(t){return t===Et}var hr="/v1/audio/models",ps=16*1024*1024,fs=64*1024*1024,B=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}};function Qt(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}function yr(t){return t.length>=12&&t[0]===82&&t[1]===73&&t[2]===70&&t[3]===70&&t[8]===87&&t[9]===65&&t[10]===86&&t[11]===69}function ms(t){if(!yr(t))return null;let e=12,n=null,r=null;for(;e+8<=t.length;){let s=String.fromCharCode(t[e],t[e+1],t[e+2],t[e+3]),i=(t[e+4]|t[e+5]<<8|t[e+6]<<16|t[e+7]<<24)>>>0,a=e+8;if(s==="fmt "&&a+16<=t.length){let d=t[a+2]|t[a+3]<<8,u=(t[a+4]|t[a+5]<<8|t[a+6]<<16|t[a+7]<<24)>>>0,l=t[a+14]|t[a+15]<<8;n={sampleRate:u,channels:d,bitsPerSample:l}}else if(s==="data"){let d=t.length-a;r=Math.min(i===0||i===4294967295?d:i,d)}e=a+i+i%2}if(n===null||r===null||r===0||n.sampleRate===0||n.channels===0||n.bitsPerSample===0)return null;let o=r/(n.sampleRate*n.channels*(n.bitsPerSample/8));return!Number.isFinite(o)||o<0?null:{seconds:o,sampleRate:n.sampleRate,channels:n.channels,bitsPerSample:n.bitsPerSample}}async function gs(t,e,n){if(t){if(t.model=Q({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}function hs(t,e){t&&(t.outputUnits=e.seconds)}async function ys(t,e,n,r,o){let s=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1,...r!==void 0?{language:r}:{}},o),i=[],a=null;for await(let d of s){if(i.push(d),d.type==="error"){let u=d.error??{},l=typeof u.type=="string"?u.type:"upstream_error",c=String(u.message??"upstream reported an error");throw new B(l==="invalid_request_error"?400:502,`transcription upstream error: ${c}`)}if(d.type==="response.completed"){a=d.response??null;break}}if(a===null)throw new B(502,"transcription upstream produced no terminal response \u2014 refusing to fabricate a transcript");return Oe(i,a)}function _r(t){return t instanceof B?t:t instanceof N?new B(404,t.message):new B(502,`transcription upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function _s(t,e,n){let r;try{r=await t(e)}catch(o){throw _r(o)}return r?!0:(Qt(n,new B(404,e===void 0?"no transcription-capable default model is configured for this caller (catalog task stt) \u2014 name an stt model":`model "${e}" is not transcription-capable (catalog task is not stt) \u2014 this lane serves only speech-to-text models; tts models ride /v1/audio/speech and chat models ride /v1/chat/completions`)),!1)}var ws=new Set(["file","model","language","response_format"]);async function wr(t,e,n){let r=n.maxBodyBytes??fs,o=n.maxAudioPartBytes??ps;try{if(t.method!=="POST")throw new B(405,`${String(t.method)} is not supported on ${Et}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new B(400,`the transcriptions endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await bt(t,r)}catch(y){throw y instanceof K?new B(413,y.message):new B(400,String(y instanceof Error?y.message:y))}let a=new Map;for(let y of i.keys()){if(!ws.has(y))throw new B(400,`form field "${y}" is not implemented: the native transcription request carries the audio clip and a language hint only (prompt, temperature, timestamps and similar knobs have no native surface on this lane). Supported fields: file, model, language, response_format.`,y);a.set(y,(a.get(y)??0)+1)}for(let[y,_]of a)if(_>1)throw new B(400,`form field "${y}" appears ${_} times: the lane reads exactly one value per field, and a repeat is refused rather than silently discarded \u2014 resend the request with each field once`,y);let d=i.get("model");if(d!==null&&typeof d!="string")throw new B(400,"model must be a string field","model");let u=d===null?void 0:d,l=i.get("language");if(l!==null&&(typeof l!="string"||l.trim().length===0))throw new B(400,"language, when present, must be a non-empty string","language");let c=l===null?void 0:l,g=i.get("response_format")??"json";if(g!=="json")throw new B(400,`response_format ${JSON.stringify(g)} is not supported: timestamps/segments are not produced on this lane yet \u2014 the runtime answers terminal transcript text only, so json is the only output format`,"response_format");let p=i.get("file");if(!(p instanceof File))throw new B(400,"file must be an uploaded WAV: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths","file");if(p.size>o)throw new B(413,`file part exceeds the ${o}-byte limit (the serve protocol's MAX_AUDIO_BYTES)`,"file");let v=new Uint8Array(await p.arrayBuffer());if(!yr(v))throw new B(400,"the audio payload is not a RIFF/WAVE container \u2014 this lane accepts WAV uploads only (detected by magic bytes, not the declared Content-Type); the runtime decodes PCM16 WAV and nothing else","file");let A=ms(v);if(A===null)throw new B(400,"the WAV header cannot be read (no fmt /data chunk pair, or a zero rate/channel/depth) \u2014 the runtime decodes RIFF/WAVE PCM16 and this container cannot be honestly decoded or billed","file");if(await gs(n.turn,n.clients,u),n.stats?.request("audio_transcriptions",v.length,v.length),!await _s(n.transcriptionModels,u,e))return;let R=[{type:"input_audio",input_audio:{data:Buffer.from(v).toString("base64"),format:"wav"}}];try{let y=await ys(n.clients,u,R,c,n.turn?te(n.turn):void 0);hs(n.turn,A),e.statusCode=200,e.setHeader("content-type","application/json"),e.end(JSON.stringify({text:y}))}catch(y){throw _r(y)}}catch(s){if(s instanceof K){Qt(e,new B(413,s.message));return}if(s instanceof B){Qt(e,s);return}throw s}}var br="/v1/usage/requests",vr=1e4,vs=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,he=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},Ce=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}};var kr=1024*1024;function Zt(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new he(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new he(`"${e}" must be an array of uuid strings`);if(n.length>vr)throw new he(`"${e}" carries ${n.length} ids; at most ${vr} 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"||!vs.test(s))throw new he(`"${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 Rr(t){return Zt(t,"inference_ids")}function At(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 Ce(`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 Ce(`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 Pt=require("@urun-sh/core");var en="/partners/huggingface/v1/billing/requests";function bs(t){return Zt(t,"requestIds")}function ks(t){return t>=200&&t<=399}function Rs(t){let e=[];for(let n of t)n.cost_nano_usd!==null&&ks(n.http_status)&&e.push({requestId:n.inference_id,costNanoUsd:n.cost_nano_usd});return{requests:e.length>0?e:null}}async function xr(t,e){let n=bs(t),r=await e(n);return At(r,n),Rs(r)}var xs={"/v1/chat/completions":"conversational","/v1/responses":"conversational","/v1/messages":"conversational","/v1/systemone":"conversational","/v1/images/generations":"text-to-image","/v1/images/edits":"image-to-image","/v1/audio/speech":"text-to-speech","/v1/audio/transcriptions":"speech-to-text"};function Ss(t){return xs[t]??null}var Ir=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function Es(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function As(t){return t==="surface"}function Is(t){let e=t?.ledgerFailureReason;return Ir.includes(e)?e:"unknown"}var Os=["no_response_head","no_org","model_unresolved","no_model_named","queue_full"],Sr=["priced","unpriced"],Ts={written:Sr,already_written:Sr,skipped:Os,failed:Ir},fu=Object.entries(Ts).flatMap(([t,e])=>e.map(n=>({outcome:t,reason:n})));function Ps(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function Cs(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 $s=256,Er=4096,Ke=12e4,Ms=250,Ls=8e3,ze=[],Ar=new Set,Ye=0,We=!1;var Ns=new Set;function Us(){return Ye===0&&ze.length===0}function Ds(){if(Us()){We=!1;for(let t of[...Ns])t()}}function Or(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
|
|
20
|
+
`}function Bs(t){ke(process.stdout,t)}function Tr(t,e){try{t(e)}catch{}}function Pr(t){let{record:e,ledger:n}=t;if(n===null)return;let r=n.report??Bs,o=Ss(e.path);if(o===null)return;let s=l=>{try{n.observe(l)}catch{}},i=(l,c)=>{s({outcome:"skipped",reason:l}),Tr(r,Or("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 d;if(a.kind==="catalog")d={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}d={model_id:a.model,identity_kind:"caller_org_app",variant:null}}let u={inference_id:e.inference_id,...d,task:o,http_status:e.status,started_at:Cs(e),completed_at:e.ts,billable_outcome:Ps(e),session_id:e.session_id,prompt_tokens:e.prompt_tokens,completion_tokens:e.completion_tokens,output_units:e.output_units,queue_ms:e.queue_ms,prefill_ms:e.prefill_ms,decode_ms:e.decode_ms,total_ms:e.total_ms,ttft_ms:e.ttft_ms===null?null:Math.round(e.ttft_ms),e2e_ms:Math.round(e.duration_ms)};if(ze.length>=Er){i("queue_full",`${ze.length} ledger writes are already waiting (capacity ${Er}, ${Ye} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}ze.push({entry:u,req:t.req,record:e,ledger:n,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),Ye<$s&&(Ye+=1,Fs())}async function Fs(){try{for(;;){let t=qs();if(t===void 0)return;Ar.add(t);try{await js(t)}finally{Ar.delete(t)}}}finally{Ye-=1,Ds()}}function qs(){let t=Date.now();for(;;){let e=ze.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<Ke)return e;$e(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 ${Ke}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 js(t){let{entry:e,record:n,ledger:r}=t;for(;;){t.attempts+=1;try{let o=await r.write(t.req,e);$e(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=Es(o);if(s!==null){$e(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=Is(o),a=Date.now()-t.queuedAt;if(We){$e(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(As(i)&&a<Ke){if(await Vs(Hs(t.attempts)),We||Date.now()-t.queuedAt>=Ke){let d=Date.now()-t.queuedAt;$e(t,{outcome:"failed",reason:We?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:We?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:d,error:`this row's backoff outlived the ${Ke}ms dwell bound (waited ${d}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}$e(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 Hs(t){return Math.min(Ls,Ms*2**Math.max(0,t-1))}function Vs(t){return new Promise(e=>{setTimeout(e,t).unref?.()})}function $e(t,e,n){if(t.settled)return;t.settled=!0;try{t.ledger.observe(e)}catch{}let{event:r,...o}=n;Tr(t.report,Or(r,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...o}))}var Gs=Mt(require("crypto"),1);var Js=6e4,hu=Js/1e3+5;var Cr=15;var yu=Cr*1e3,_u=Cr*500;var Xe=class extends Error{constructor(n,r){super(`runtime answered ${n}`);this.status=n;this.body=r}status;body},Qe=class extends Error{};function rn(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function H(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function G(t,e,n,r="invalid_request_error",o){H(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function on(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);H(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function oe(t,e,n,r){H(t,e,{type:"error",error:{type:n,message:r}})}function se(t,e,n){if(e==="anthropic"){H(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}H(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var Ws=64*1024*1024,Ze=class extends Error{};async function $r(t,e=Ws){let n=[],r=0;for await(let s of t){if(r+=s.length,r>e)throw new Ze(`request body exceeds ${e} bytes`);n.push(s)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function Me(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 Nr(t){let e=t?.usage;if(!e||typeof e!="object")return{kind:"absent"};let n=e,r=d=>typeof d=="number"&&Number.isFinite(d)&&d>=0?d:null,o=r(n.input_tokens),s=r(n.output_tokens),i=d=>d??0,a=r(n.total_tokens)??i(o)+i(s);if(i(o)>0||i(s)>0){let d={input_tokens:i(o),output_tokens:i(s),total_tokens:a};return o===null||s===null?{kind:"partial",usage:d}:{kind:"counts",usage:d}}return o===null||s===null?{kind:"absent"}:{kind:"zero",usage:{input_tokens:0,output_tokens:0,total_tokens:a}}}function It(t){let e=Nr(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function Mr(t){let e=It(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 Ks(t,e){let n=Nr(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function zs(t){let e=t?.urun_timing;if(!e||typeof e!="object")return null;let n=e,r=d=>{let u=n[d];return typeof u=="number"&&Number.isSafeInteger(u)&&u>=0?u: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 Ys(t,e){let n=zs(e);n&&(t.timing=n)}function ce(t,e){Ks(t,e),Ys(t,e)}async function Ct(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 Ur(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 Xs(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 Ot=class extends Error{};function Qs(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 Ot(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function Zs(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:Qs(n.role,n.content)})}return e}function tn(t){return Ae(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 ei(t,e,n,r,o,s){let i=Ue(s.id),a=t.stream===!0;if(s.model=Q(t),s.stream=a,await Ct(s,e,typeof t.model=="string"?t.model:void 0),a){let y;try{y=await Be({body:t,clients:e,store:n,stats:o,responseId:i,turn:s})}catch(_){if(_ instanceof N){se(r,"openai",_);return}if(_ instanceof ne){G(r,_.status,_.message);return}throw _}rn(r);for await(let _ of y.events)_.type==="error"&&(s.streamError=!0),_.type==="response.completed"&&ce(s,_.response),r.write(`event: ${String(_.type??"message")}
|
|
21
21
|
data: ${JSON.stringify(_)}
|
|
22
22
|
|
|
23
|
-
`);
|
|
23
|
+
`);r.write(`data: [DONE]
|
|
24
24
|
|
|
25
|
-
`),
|
|
25
|
+
`),r.end();return}let d;try{d=Te(n,t)}catch(y){if(y instanceof ne){G(r,y.status,y.message);return}throw y}t.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(d))-Buffer.byteLength(JSON.stringify(t.input)));let u=t.store!==!1,l=Q(t);o.model(l);let c=Array.isArray(t.tools)&&t.tools.length>0,g;try{g=await Dt(e,t,d,!1,te(s))}catch(y){if(y instanceof N){se(r,"openai",y);return}if(y instanceof ne){G(r,y.status,y.message);return}throw y}let p=[],v=null;for await(let y of o.track(g,l,s)){if(p.push(y),y.type==="error"){on(r,y);return}y.type==="response.completed"&&(v=y.response)}if(v==null){G(r,502,"upstream produced no response.completed event","upstream_error");return}ce(s,v);let A=Oe(p,v),R=be(c,Ae(v).length,A);if(R){G(r,502,ve(l,R),"upstream_error");return}u&&n.remember(i,d,Ut(v,A),u),H(r,200,{...v,id:i})}async function ti(t,e,n,r,o){let s=t.messages;if(!Array.isArray(s)){G(n,400,"chat/completions requires a messages array");return}let i=String(t.model??"urun"),a=$n(o.id),d;try{d=Zs(s)}catch(f){if(f instanceof Ot){G(n,400,f.message);return}throw f}let u=Q(t);r.model(u),o.model=u,o.stream=t.stream===!0,await Ct(o,e,typeof t.model=="string"?t.model:void 0);let l=Array.isArray(t.tools)&&t.tools.length>0;if(t.stream!==!0&&e.stateless?.enabled){let f;try{f=await e.stateless.targetFor(typeof t.model=="string"?t.model:void 0)}catch(k){if(k instanceof N){se(n,"openai",k);return}throw k}if(f!==null){try{let k=await e.stateless.decide(t,typeof t.model=="string"?t.model:void 0);o.catalog=Lt(k.target),ce(o,k.completed),H(n,200,{...k.body,id:a})}catch(k){if(k instanceof N){se(n,"openai",k);return}if(k instanceof Xe){H(n,k.status,k.body);return}if(k instanceof Qe){G(n,503,k.message,"upstream_error");return}throw k}return}}let c;try{c=await e.createResponse({model:t.model,input:d,stream:!0,tools:Xs(t.tools),tool_choice:t.tool_choice,temperature:t.temperature,top_p:t.top_p,stop:t.stop,max_output_tokens:t.max_completion_tokens??t.max_tokens,...t.reasoning_effort!==void 0?{reasoning_effort:t.reasoning_effort}:{},...t.chat_template_kwargs!==void 0?{chat_template_kwargs:t.chat_template_kwargs}:{},...t.response_format!==void 0?{response_format:t.response_format}:{}},te(o))}catch(f){if(f instanceof N){se(n,"openai",f);return}throw f}if(t.stream===!0){rn(n);let f=[],k=new me,I="";n.write(`data: ${JSON.stringify(Me(a,i,{role:"assistant"},null))}
|
|
26
26
|
|
|
27
|
-
`);for await(let
|
|
27
|
+
`);for await(let W of r.track(c,u,o)){f.push(W);let Y=Ie(W);if(W.type==="response.output_text.delta"&&typeof W.delta=="string")I+=W.delta,n.write(`data: ${JSON.stringify(Me(a,i,{content:W.delta},null))}
|
|
28
28
|
|
|
29
|
-
`);else if(
|
|
29
|
+
`);else if(W.type==="response.reasoning_text.delta"&&typeof W.delta=="string")n.write(`data: ${JSON.stringify(Me(a,i,{reasoning_content:W.delta},null))}
|
|
30
30
|
|
|
31
|
-
`);else if(
|
|
31
|
+
`);else if(Y){let{call:ie,isNew:q}=k.add(Y),ae=q?{index:Y.tool_index,id:ie.id??`call_${Y.tool_index}`,type:"function",function:{name:ie.name??"",arguments:Y.delta}}:{index:Y.tool_index,function:{arguments:Y.delta}};n.write(`data: ${JSON.stringify(Me(a,i,{tool_calls:[ae]},null))}
|
|
32
32
|
|
|
33
|
-
`)}else if(
|
|
33
|
+
`)}else if(W.type==="error"){o.streamError=!0,n.write(`data: ${JSON.stringify({error:W})}
|
|
34
34
|
|
|
35
|
-
`),n.end();return}}let
|
|
35
|
+
`),n.end();return}}let O=tn(re(f)),C=be(l,k.size+O.length,I);if(C){o.streamError=!0,n.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:ve(u,C)}})}
|
|
36
36
|
|
|
37
|
-
`),n.end();return}
|
|
37
|
+
`),n.end();return}k.size===0&&O.length>0&&n.write(`data: ${JSON.stringify(Me(a,i,{tool_calls:O},null))}
|
|
38
38
|
|
|
39
|
-
`);let
|
|
39
|
+
`);let J=k.size>0||O.length>0;n.write(`data: ${JSON.stringify(Me(a,i,{},J?"tool_calls":"stop"))}
|
|
40
40
|
|
|
41
|
-
`);let
|
|
41
|
+
`);let z=It(re(f));ce(o,re(f)),z&&n.write(`data: ${JSON.stringify({id:a,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:i,choices:[],usage:Mr(re(f))})}
|
|
42
42
|
|
|
43
43
|
`),n.write(`data: [DONE]
|
|
44
44
|
|
|
45
|
-
`),n.end();return}let
|
|
46
|
-
data: ${JSON.stringify({type:f,...
|
|
45
|
+
`),n.end();return}let g="",p="",v=[],A=new me;for await(let f of r.track(c,u,o)){v.push(f),f.type==="response.output_text.delta"&&typeof f.delta=="string"&&(g+=f.delta),f.type==="response.reasoning_text.delta"&&typeof f.delta=="string"&&(p+=f.delta);let k=Ie(f);if(k&&A.add(k),f.type==="error"){on(n,f);return}}let R=re(v);ce(o,R);let y=tn(R),_=y.length>0?y:tn({output:A.items()}),h=be(l,_.length,g);if(h){G(n,502,ve(u,h),"upstream_error");return}let S=_.map(({index:f,...k})=>k),m={role:"assistant",content:g||null},b=p||Ur(R);b&&(m.reasoning_content=b),S.length>0&&(m.tool_calls=S),H(n,200,{id:a,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:i,choices:[{index:0,message:m,finish_reason:S.length>0?"tool_calls":"stop"}],usage:Mr(R)})}var ni="reflex-latest";async function ri(t,e,n,r,o){if(t===null||typeof t!="object"||Array.isArray(t)){G(n,400,"systemone requires a JSON object body");return}delete t.key;let s=typeof t.model=="string"&&t.model?t.model:ni,i=Q({model:s});if(o.model=i,o.stream=!1,t.state===void 0||t.state===null){G(n,400,"systemone requires a state (string or JSON structure)");return}let a=t.questions;if(a===null||typeof a!="object"||Array.isArray(a)||Object.keys(a).length===0){G(n,400,"systemone requires a questions object (question id -> {kind, options, ...})");return}if(t.media!==void 0&&!Array.isArray(t.media)){G(n,400,"systemone media, when present, must be an array");return}if(r.model(i),await Ct(o,e,s),e.stateless?.enabled){let p;try{p=await e.stateless.targetFor(s)}catch(v){if(v instanceof N){se(n,"openai",v);return}throw v}if(p!==null){try{let v=await e.stateless.decide(t,s);o.catalog=Lt(v.target),ce(o,v.completed),H(n,200,v.body)}catch(v){if(v instanceof N){se(n,"openai",v);return}if(v instanceof Xe){H(n,v.status,v.body);return}if(v instanceof Qe){G(n,503,v.message,"upstream_error");return}throw v}return}}let d;try{d=await e.createResponse({model:s,input:[{role:"user",content:JSON.stringify(t)}],stream:!0},te(o))}catch(p){if(p instanceof N){se(n,"openai",p);return}throw p}let u=[];try{for await(let p of r.track(d,i,o))if(u.push(p),p.type==="error"){on(n,p);return}}catch(p){if(p instanceof N){se(n,"openai",p);return}throw p}let l=re(u);if(l==null){G(n,502,"upstream produced no response.completed event","upstream_error");return}ce(o,l);let c=Oe(u,l),g;try{g=JSON.parse(c)}catch{G(n,502,"systemone answer was not valid JSON","upstream_error");return}if(g===null||typeof g!="object"||Array.isArray(g)){G(n,502,"systemone answer was not a JSON object","upstream_error");return}H(n,200,g)}function oi(t){return typeof t=="string"?t:Array.isArray(t)?t.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var et=class extends Error{};function Tt(t){return new et(`unsupported anthropic content block type "${t}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function si(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 ii(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 et(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function ai(t){if(typeof t=="string")return t;if(t==null)return"";if(!Array.isArray(t))throw Tt(typeof t);let e="";for(let n of t){let r=String(n.type??"");if(r!=="text")throw Tt(`tool_result > ${r}`);e+=String(n.text??"")}return e}function ui(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 Tt(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:ai(i.content)});else{if(a==="thinking"||a==="redacted_thinking")continue;throw Tt(a)}}return s(),r}function nn(t){return Ae(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 di(t,e,n,r,o){let s=t.messages;if(!Array.isArray(s)){oe(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){oe(n,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(t.top_k!==void 0){oe(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(f=>typeof f!="string"))){oe(n,400,"invalid_request_error","stop_sequences must be an array of strings");return}let i=String(t.model??"urun"),a=Mn(o.id),d=[],u;try{t.system&&d.push({role:"system",content:oi(t.system)});for(let f of s)d.push(...ui(f));u=ii(t.tool_choice)}catch(f){if(f instanceof et){oe(n,400,"invalid_request_error",f.message);return}throw f}let l=Q(t);r.model(l),o.model=l,o.stream=t.stream===!0,await Ct(o,e,typeof t.model=="string"?t.model:void 0);let c=Array.isArray(t.tools)&&t.tools.length>0,g;try{g=await e.createResponse({model:t.model,input:d,stream:!0,tools:si(t.tools),tool_choice:u,temperature:t.temperature,max_output_tokens:t.max_tokens,top_p:t.top_p,stop:t.stop_sequences},te(o))}catch(f){if(f instanceof N){se(n,"anthropic",f);return}throw f}let p=(f,k)=>{n.destroyed||n.write(`event: ${f}
|
|
46
|
+
data: ${JSON.stringify({type:f,...k})}
|
|
47
47
|
|
|
48
|
-
`)};if(t.stream===!0){let f=
|
|
48
|
+
`)};if(t.stream===!0){let f=r.track(g,l,o);rn(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 k=0,I="",O=0,C="none",J=()=>C,z=-1,W=-1,Y=new me,ie=[],q=()=>{C!=="none"&&(C="none",p("content_block_stop",{index:z}))},ae=(D,X)=>{q(),C=D,z=O++,p("content_block_start",{index:z,content_block:X})};for await(let D of f){if(n.destroyed)break;ie.push(D);let X=Ie(D);if(D.type==="response.reasoning_text.delta"&&typeof D.delta=="string")J()!=="thinking"&&ae("thinking",{type:"thinking",thinking:""}),p("content_block_delta",{index:z,delta:{type:"thinking_delta",thinking:D.delta}});else if(D.type==="response.output_text.delta"&&typeof D.delta=="string")J()!=="text"&&ae("text",{type:"text",text:""}),k+=1,I+=D.delta,p("content_block_delta",{index:z,delta:{type:"text_delta",text:D.delta}});else if(X){let{call:fe}=Y.add(X);(J()!=="tool"||X.tool_index!==W)&&(W=X.tool_index,ae("tool",{type:"tool_use",id:fe.id??`call_${X.tool_index}`,name:fe.name??"",input:{}})),X.delta&&p("content_block_delta",{index:z,delta:{type:"input_json_delta",partial_json:X.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 pe=re(ie),tt=Ae(pe),P=be(c,Y.size+tt.length,I);if(P){o.streamError=!0,p("error",{error:{type:"api_error",message:ve(l,P)}}),n.end();return}q();let M=[];if(Y.size===0)try{M=nn(pe)}catch(D){o.streamError=!0,p("error",{error:{type:"api_error",message:String(D instanceof Error?D.message:D)}}),n.end();return}O===0&&M.length===0&&(ae("text",{type:"text",text:""}),q()),M.forEach(D=>{let X=O++;p("content_block_start",{index:X,content_block:{...D,input:{}}}),p("content_block_delta",{index:X,delta:{type:"input_json_delta",partial_json:JSON.stringify(D.input??{})}}),p("content_block_stop",{index:X})});let V=Y.size>0||M.length>0,ue=It(pe);ce(o,pe),p("message_delta",{delta:{stop_reason:V?"tool_use":"end_turn"},usage:ue?{input_tokens:ue.input_tokens,output_tokens:ue.output_tokens}:{output_tokens:k}}),p("message_stop",{}),n.end();return}let v="",A="",R=[],y=new me;for await(let f of r.track(g,l,o)){R.push(f),f.type==="response.output_text.delta"&&typeof f.delta=="string"&&(v+=f.delta),f.type==="response.reasoning_text.delta"&&typeof f.delta=="string"&&(A+=f.delta);let k=Ie(f);if(k&&y.add(k),f.type==="error"){let I=f.error,O=I?.type==="invalid_request_error";oe(n,O?400:502,O?"invalid_request_error":"api_error",I?.message??JSON.stringify(f));return}if(n.destroyed)break}if(n.destroyed)return;let _=re(R);ce(o,_);let h;try{h=nn(_),h.length===0&&y.size>0&&(h=nn({output:y.items()}))}catch(f){oe(n,502,"api_error",String(f instanceof Error?f.message:f));return}let S=be(c,h.length,v);if(S){oe(n,502,"api_error",ve(l,S));return}let m=[],b=A||Ur(_);b&&m.push({type:"thinking",thinking:b}),(v||h.length===0)&&m.push({type:"text",text:v}),m.push(...h),H(n,200,{id:a,type:"message",role:"assistant",content:m,model:i,stop_reason:h.length>0?"tool_use":"end_turn",usage:It(_)??{input_tokens:0,output_tokens:0}})}function li(t){ke(process.stdout,t)}function ci(t){let{clients:e,apiKey:n,identity:r,statusOf:o}=t,s=typeof e=="function"?e:()=>e,i=t.requestLog??li,a=t.observe,d=new _e;return(u,l)=>{let c=new URL(u.url??"/","http://localhost"),g=c.pathname==="/v1/messages"||c.pathname.startsWith("/v1/messages/"),p=(_,h,S,m,b)=>g?oe(l,_,m,h):G(l,_,h,S,b),v=Nt(c.pathname),A=c.pathname.startsWith("/partners/"),R=v||A?{id:Ne(),startedAt:performance.now(),streamError:!1,model:null,stream:null,org:null,ttftMs:null,catalog:{kind:"unresolved",reason:"no lane resolved a model for this request"},promptTokens:null,completionTokens:null,outputUnits:null,sessionId:null,timing:null}:null;R&&(l.setHeader(Pn,R.id),l.once("close",()=>{let _=Ln({turn:R,method:u.method??"",path:c.pathname,res:l});i(Nn(_)),a?.(_),Pr({record:_,ledger:t.ledger??null,req:u})}));let y=()=>Cn(c.pathname,R);(async()=>{if(u.method==="GET"&&c.pathname==="/healthz"){H(l,200,{ok:!0});return}if(u.method==="GET"&&c.pathname==="/stats"){H(l,200,r?{identity:r,...d.snapshot()}:d.snapshot());return}if(n&&(u.headers.authorization??"")!==`Bearer ${n}`){p(401,"invalid local proxy api key","authentication_error","authentication_error");return}let _=c.pathname===br?"canonical":c.pathname===en?"huggingface":null;if(_){if(u.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 f=b.authorize(u),k;try{k=await $r(u,kr)}catch(C){if(C instanceof Ze){p(413,C.message,"request_too_large","request_too_large");return}p(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let I=C=>b.lookup(f,C),O;try{if(_==="canonical"){let C=Rr(k),J=await I(C);At(J,C),O={requests:J}}else O=await xr(k,I)}catch(C){if(C instanceof he){p(C.status,C.message,"invalid_request_error","invalid_request_error");return}throw C}H(l,200,O);return}if(c.pathname.startsWith("/partners/")){p(404,`no partner adapter for ${u.method} ${c.pathname} \u2014 the partner surface currently carries ${en}`,"invalid_request_error","not_found_error");return}let h=await s(u);if(R&&(R.org=h.tenant??null),u.method==="GET"&&c.pathname==="/v1/models"){if(c.searchParams.get("format")==="openrouter"){if(!h.openRouterModels){p(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}H(l,200,await h.openRouterModels());return}H(l,200,await h.listModels());return}if(u.method==="GET"&&c.pathname===Qn){if(!h.listImageModels){p(501,"GET /v1/images/models needs a router-backed client (listImageModels)","not_implemented","not_implemented_error");return}H(l,200,await h.listImageModels());return}if(Xn(c.pathname)){if(!h.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:h,imageModels:h.supportsImages,stats:d,turn:y()};c.pathname===kt?await ur(u,l,b):await ar(u,l,b);return}if(u.method==="GET"&&c.pathname===hr){if(!h.listSpeechToTextModels){p(501,"GET /v1/audio/models needs a router-backed client (listSpeechToTextModels)","not_implemented","not_implemented_error");return}H(l,200,await h.listSpeechToTextModels());return}if(gr(c.pathname)){if(!h.supportsTranscription){p(501,`the transcription lane needs a router-backed client (supportsTranscription) to gate model capability for ${Et}`,"not_implemented","not_implemented_error");return}let b={clients:h,transcriptionModels:h.supportsTranscription,stats:d,turn:y()};await wr(u,l,b);return}if(fr(c.pathname)){if(!h.supportsSpeech){p(501,`the speech lane needs a router-backed client (supportsSpeech) to gate model capability for ${Je}`,"not_implemented","not_implemented_error");return}let b={clients:h,speechModels:h.supportsSpeech,stats:d,turn:y()};await mr(u,l,b);return}if(u.method!=="POST"){p(404,`no route for ${u.method} ${c.pathname}`,"invalid_request_error","not_found_error");return}let S;try{S=await $r(u)}catch(b){if(b instanceof Ze){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 m=Buffer.byteLength(JSON.stringify(S));if(c.pathname==="/v1/responses"){d.request("responses",m,m),await ei(S,h,De(h),l,d,y());return}if(c.pathname==="/v1/chat/completions"){d.request("chat_completions",m,m),await ti(S,h,l,d,y());return}if(c.pathname==="/v1/systemone"){d.request("systemone",m,m),await ri(S,h,l,d,y());return}if(c.pathname==="/v1/messages"){d.request("messages",m,m),await di(S,h,l,d,y());return}if(c.pathname==="/v1/messages/count_tokens"){d.request("count_tokens",m,m),oe(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(_=>{let h=String(_ instanceof Error?_.message:_);if(!l.headersSent){let S=o?.(_)??null;if(S){p(S.status,h,S.openaiType,S.anthropicType);return}if(_ instanceof Pt.SessionAllocationError){let m=ft(_.code);if(m){let b=_.retryAfterSeconds??(m.status===429?30:void 0);b!==void 0&&l.setHeader("Retry-After",String(b)),p(m.status,jt(m,_.code??void 0),m.openaiType,m.anthropicType,m.code);return}}if(_ instanceof Pt.SessionFailedError){let m=mt(_);if(m){let b=m.status===429?30:void 0;b!==void 0&&l.setHeader("Retry-After",String(b)),p(m.status,jt(m,_.status.sessionId?`session ${_.status.sessionId}`:void 0),m.openaiType,m.anthropicType,m.code);return}}if(_ instanceof Ce){p(502,h,"upstream_error","api_error");return}p(500,h,"proxy_error","api_error","proxy_error");return}R&&(R.streamError=!0),l.destroyed||(g?l.write(`event: error
|
|
49
49
|
data: ${JSON.stringify({type:"error",error:{type:"api_error",message:h}})}
|
|
50
50
|
|
|
51
|
-
`):
|
|
51
|
+
`):l.write(`data: ${JSON.stringify({error:{message:h,type:"upstream_error"}})}
|
|
52
52
|
|
|
53
|
-
`)),
|
|
53
|
+
`)),l.end()})}}function Dr(t){let{clients:e,apiKey:n}=t,r=(0,Lr.createServer)(ci(t)),o=qn(r,{clients:e,apiKey:n,statusOf:t.statusOf}),s=je(r,{resolver:wt(e,n)}),i=r.close.bind(r);return r.close=(a=>(o.close(),s(),i(a))),ct(r,{clients:e,apiKey:n}),r}0&&(module.exports={GEMINI_LIVE_PATH,OPENAI_REALTIME_PATH,OpenAIRealtimeConnection,ResumptionRegistry,SessionGoneError,UnknownModelError,VIDEO_OUT_SETUP_KEY,attachGeminiLive,attachOpenAIRealtime,createOpenAIProxy,fixedClientsResolver,realtimeBindingFor,realtimeModelOf,serveGeminiLiveSocket});
|