@urun-sh/openai 0.5.6 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{ResponsesClient-BE3-hx3m.d.ts → ResponsesClient-CSSrOYD8.d.ts} +5 -1
- package/dist/{ResponsesClient-DjZWjlFH.d.cts → ResponsesClient-_OZERUjH.d.cts} +9 -1
- package/dist/{chunk-7M6LY6DR.js → chunk-3JWYIKHM.js} +1 -1
- package/dist/chunk-45FJ5GTP.js +7 -0
- package/dist/{chunk-BSHT6RZZ.js → chunk-6M5JK4YC.js} +1 -1
- package/dist/{chunk-5HKWNK3O.js → chunk-K23AZPI4.js} +1 -1
- package/dist/chunk-QTVTCMJU.js +1 -0
- package/dist/chunk-TDRZZGUK.js +1 -0
- package/dist/chunk-TPH77ZOW.js +58 -0
- package/dist/chunk-WCMX5VOF.js +4 -0
- package/dist/chunk-YXK42SKC.js +1 -0
- package/dist/gemini-live.cjs +2 -2
- package/dist/gemini-live.d.cts +64 -7
- package/dist/gemini-live.d.ts +27 -7
- package/dist/gemini-live.js +1 -1
- package/dist/hosted/bin.cjs +37 -35
- package/dist/hosted/bin.js +1 -1
- package/dist/hosted/index.cjs +33 -31
- package/dist/hosted/index.d.cts +70 -4
- package/dist/hosted/index.d.ts +21 -4
- package/dist/hosted/index.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1 -1
- package/dist/pi-extension/index.cjs +7 -7
- package/dist/pi-extension/index.d.cts +2 -2
- package/dist/pi-extension/index.d.ts +2 -2
- package/dist/pi-extension/index.js +1 -1
- package/dist/pi-extension/standalone.cjs +53 -53
- package/dist/proxy/cli.cjs +47 -45
- package/dist/proxy/cli.js +15 -14
- package/dist/proxy/index.cjs +28 -27
- package/dist/proxy/index.d.cts +54 -16
- package/dist/proxy/index.d.ts +18 -11
- package/dist/proxy/index.js +1 -1
- package/dist/{responses-turn-tNUHq5b0.d.cts → responses-turn-BfdBbey8.d.cts} +610 -23
- package/dist/{responses-turn-Dr37N3kH.d.ts → responses-turn-CdhIre_a.d.ts} +173 -7
- package/dist/{translator-Bh0Bp_Ie.d.cts → translator-CO8W_hmJ.d.cts} +1 -1
- package/dist/{translator-BCoRFaTs.d.ts → translator-Ddd65sXR.d.ts} +1 -1
- package/dist/{types-lsVTbNcH.d.cts → types-CHPtJx6d.d.cts} +13 -0
- package/dist/{types-lsVTbNcH.d.ts → types-CHPtJx6d.d.ts} +4 -0
- package/dist/{video-out-CCksIcj8.d.cts → video-out-BsuLqlID.d.cts} +1 -1
- package/dist/{video-out-IGQ4YQ-c.d.ts → video-out-Cawtm7SF.d.ts} +1 -1
- package/package.json +4 -2
- package/dist/chunk-2T2YYBVX.js +0 -1
- package/dist/chunk-3WAJD62J.js +0 -4
- package/dist/chunk-FP4RSAIE.js +0 -1
- package/dist/chunk-HR5H6S7L.js +0 -1
- package/dist/chunk-NY23USZF.js +0 -57
- package/dist/chunk-VLRMJRLS.js +0 -6
- /package/dist/{chunk-5CCJUNH7.js → chunk-YSFSRI3D.js} +0 -0
package/dist/chunk-NY23USZF.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import{c as Nt,d as G,e as Oe}from"./chunk-3WAJD62J.js";import{a as Mt,c as qt,d as le,e as Je,f as F,g as Bt,h as Ft,i as Dt,j as jt,k as Gt,l as Ht,m as Wt,n as Jt,r as Vt,t as zt,u as Kt,v as Yt,w as Xt,x as Qt}from"./chunk-7M6LY6DR.js";import{c as T}from"./chunk-5CCJUNH7.js";T();import{randomUUID as nr}from"crypto";import{WebSocketServer as rr}from"ws";var or=256,Ve=class{snapshots=new Map;store(e,t,r){for(this.snapshots.set(`${e}\0${t}`,r);this.snapshots.size>or;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(e,t){return this.snapshots.get(`${e}\0${t}`)}},sr="local",Te=n=>n.slice(0,120),ze=class{constructor(e,t,r){this.ws=e;this.call=t;this.registry=r;this.clients=t.clients,this.scope=t.scope,e.on("message",o=>this.onMessage(o)),e.on("close",()=>{this.unsubscribeEnd?.(),this.unsubscribeEnd=null,this.detachAudio?.(),this.detachVideoOut?.(),this.call.release?.().catch(()=>{})})}ws;call;registry;config=null;transcript=[];queue=Promise.resolve();proxyHandle=null;unsubscribeEnd=null;audio=null;detachAudio=null;detachVideoOut=null;video=null;activityOpen=!1;activeTurn=null;scope;clients;send(e){this.ws.readyState===this.ws.OPEN&&this.ws.send(JSON.stringify(e))}fail(e){if(e instanceof G){this.ws.close(qt,Te(e.message));return}if(e instanceof Oe){this.ws.close(Je,Te(e.message));return}if(e instanceof F){this.ws.close(e.closeCode,Te(e.message));return}this.ws.close(le,Te(String(e instanceof Error?e.message:e)))}onMessage(e){try{let t=Bt(typeof e=="string"?e:Buffer.concat(Array.isArray(e)?e:[e]).toString("utf8"));if(t.kind==="setup"){this.handleSetup(t.setup).catch(o=>this.fail(o));return}if(!this.config)throw new F(`"${t.kind}" before setup \u2014 the first client message must be BidiGenerateContentSetup`);let r=this.config;if(t.kind==="clientContent"){let{items:o,turnComplete:s}=Jt(t.clientContent);this.transcript.push(...o),s&&this.enqueue(()=>this.runTurn());return}if(t.kind==="realtimeInput"){let o=zt(t.realtimeInput);if(o.kind==="activityStart"||o.kind==="activityEnd"){this.onActivity(o.kind,r);return}if(o.kind==="text"){this.transcript.push({role:"user",content:o.text}),this.enqueue(()=>this.runTurn());return}if(o.kind==="video"){this.appendVideoFrame(o.jpegFrame);return}this.appendAudio(o.kind==="audio"?o.base64Pcm24k:Vt);return}this.transcript.push(...Kt(t.toolResponse)),this.enqueue(()=>this.runTurn())}catch(t){this.fail(t)}}async handleSetup(e){if(this.config)throw new F("setup may only be sent once, as the first client message");let t=Gt(e),r=t.sessionResumption.handle;if(r!==null){let s=this.registry.get(this.scope,r);if(!s)throw new F("unknown sessionResumption.handle \u2014 the Live session it names is gone (proxy restarted, handle evicted, or never issued) and cannot be resumed; reconnect without a handle to start a new session",Je);if(s.model!==t.model)throw new F(`sessionResumption.handle was issued for model "${s.model}" \u2014 the model cannot change on resume (got "${t.model}", per the Live API session-resumption contract)`);if(t.systemItems.length>0)throw new F("setup.systemInstruction cannot be changed on resume \u2014 it is part of the resumed conversation state");if(this.call.clientsForResume!==void 0&&(this.clients=await this.call.clientsForResume(r)),this.proxyHandle=await this.clients.sessionHandle(t.model),this.proxyHandle!==s.proxyHandle)throw new F(`sessionResumption.handle named pooled session "${s.proxyHandle}" but the re-attach landed on "${this.proxyHandle}" \u2014 the conversation's native session changed and the snapshot cannot be adopted; reconnect without a handle to start a new session`,Je);this.unsubscribeEnd=await this.clients.onSessionEnd(s.proxyHandle,i=>this.enqueue(()=>this.handleUpstreamEnd(i))),this.transcript=[...s.transcript]}else this.proxyHandle=await this.clients.sessionHandle(t.model),this.unsubscribeEnd=await this.clients.onSessionEnd(this.proxyHandle,s=>this.enqueue(()=>this.handleUpstreamEnd(s))),this.transcript.push(...t.systemItems);this.config=t;let o={};if(t.videoOut){let s=this.clients.openVideoOut;if(s){let i=await s.call(this.clients,t.model),a=i.onOutputFrame(u=>this.send(jt(u))),d=i.onLaneError(u=>this.fail(u));this.detachVideoOut=()=>{a(),d()},o[Ft]=Dt(i.codec)}}this.send({setupComplete:o}),t.audioModality&&this.ensureAudioLane(),t.sessionResumption.enabled&&await this.mintResumptionUpdate()}ensureAudioLane(){if(this.audio)return this.audio;let e=this.config;if(!e)throw new F("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let t=this.clients.openAudio;if(!t)throw new F("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native AudioBridge seam over the canonical `audio` lane)",le);return this.audio=(async()=>{let r=await t.call(this.clients,e.model);return this.detachAudio=r.onOutputAudio(o=>this.send(Xt(o))),r})(),this.audio.catch(r=>this.fail(r)),this.audio}appendAudio(e){this.ensureAudioLane().then(t=>t.appendInputAudio(e)).catch(t=>this.fail(t))}ensureVideoLane(){if(this.video)return this.video;let e=this.config;if(!e)throw new F("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let t=this.clients.openVideo;if(!t)throw new F("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",le);return this.video=Promise.resolve(t.call(this.clients,e.model)),this.video.catch(r=>this.fail(r)),this.video}appendVideoFrame(e){this.ensureVideoLane().then(t=>t.sendInputFrame(e)).catch(t=>this.fail(t))}onActivity(e,t){if(!t.activity.manualActivity)throw new F(`realtimeInput.${e} requires manual activity mode \u2014 set setup.realtimeInputConfig.automaticActivityDetection.disabled: true (automatic server-side activity detection is the declared platform follow-up)`);if(e==="activityStart"){if(this.activityOpen)throw new F("activityStart while an activity window is already open (send activityEnd first)");this.activityOpen=!0,t.activity.activityHandling==="START_OF_ACTIVITY_INTERRUPTS"&&this.activeTurn&&(this.activeTurn.interrupted=!0);return}if(!this.activityOpen)throw new F("activityEnd without an open activity window (send activityStart first)");this.activityOpen=!1}enqueue(e){this.queue=this.queue.then(e,()=>{}),this.queue=this.queue.catch(t=>this.fail(t))}async mintResumptionUpdate(){let e=this.config;if(!e?.sessionResumption.enabled||this.ws.readyState!==this.ws.OPEN)return;let t=await this.clients.sessionHandle(e.model);this.proxyHandle=t;let r=nr();this.registry.store(this.scope,r,{model:e.model,proxyHandle:t,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(r),this.send(Ht(r))}async handleUpstreamEnd(e){if(this.ws.readyState!==this.ws.OPEN||!this.config)return;this.unsubscribeEnd?.(),this.unsubscribeEnd=null;let t=null;try{t=await this.clients.sessionHandle(this.config.model)}catch{}if(t!==null&&t!==this.proxyHandle){this.proxyHandle=t,this.unsubscribeEnd=await this.clients.onSessionEnd(t,r=>this.enqueue(()=>this.handleUpstreamEnd(r))),this.config.sessionResumption.enabled&&await this.mintResumptionUpdate();return}this.send(Wt(e.timeLeftMs)),this.ws.close(le,Te(`upstream session ended: ${e.reason}`))}async runTurn(){let e=this.config;if(!e||this.ws.readyState!==this.ws.OPEN)return;let t={interrupted:!1};this.activeTurn=t;try{let r=await this.clients.createResponse({model:e.model,input:[...this.transcript],stream:!0,tools:e.tools,temperature:e.temperature,max_output_tokens:e.maxOutputTokens}),o,s=!1;for await(let d of r){if(t.interrupted)break;let u=d;if(u.type==="error")throw new F(`upstream error: ${JSON.stringify(u)}`,le);if(u.type==="response.completed"){o=u.response,s=!0;continue}let l=Yt(u);l&&this.send(l)}if(t.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!s)throw new F("upstream produced no response.completed event",le);let{messages:i,assistantItems:a}=Qt(o);this.transcript.push(...a);for(let d of i)this.send(d);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},ce=class extends Error{constructor(t,r){super(r);this.status=t;this.name="UpgradeAuthError"}status};function ir(n,e,t){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)})(n.headers["x-goog-api-key"]);let s=n.headers.authorization;typeof s=="string"&&s.startsWith("Bearer ")&&s.length>7&&r.add(s.slice(7));let i=e.searchParams.get("key")??void 0;if(i!==void 0&&i!==""&&r.add(i),r.size>1)throw new ce(401,`contradictory credentials: ${r.size} distinct keys presented across the x-goog-api-key/Authorization headers and the ?key= query parameter \u2014 present exactly one`);if(t&&i!==void 0&&i!=="")throw new ce(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&&t)throw new ce(401,"missing credentials: send the org API key in the x-goog-api-key header or as Authorization: Bearer");return a??""}function Zt(n,e){let t=e.authenticate!==void 0;if(t&&(e.clients!==void 0||e.apiKey!==void 0))throw new TypeError("attachGeminiLive: authenticate (hosted per-request resolution) is mutually exclusive with a fixed clients/apiKey lane");if(!t&&e.clients===void 0)throw new TypeError("attachGeminiLive: pass fixed `clients` (local lane) or `authenticate` (hosted lane)");let r=new rr({noServer:!0}),o=new Ve;n.on("upgrade",(i,a,d)=>{let u=new URL(i.url??"/","http://localhost");u.pathname===Mt&&(a.on("error",()=>{}),(async()=>{let l=ir(i,u,t),c;if(e.authenticate)c=await e.authenticate(l,i);else{if(e.apiKey!==void 0&&l!==e.apiKey)throw new ce(401,"invalid local proxy api key");c={clients:e.clients,scope:sr}}r.handleUpgrade(i,a,d,w=>{new ze(w,c,o)})})().catch(l=>{let c=502;(l instanceof ce||typeof l=="object"&&l!==null&&"status"in l&&typeof l.status=="number")&&(c=l.status);let w=c===401?"Unauthorized":c===403?"Forbidden":c===502?"Bad Gateway":"Internal Server Error",f=l instanceof Error?l.message:String(l);a.destroyed||(a.write(`HTTP/1.1 ${c} ${w}\r
|
|
2
|
-
Connection: close\r
|
|
3
|
-
\r
|
|
4
|
-
${f}\r
|
|
5
|
-
`),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 ts(n,e,t=new Ve){new ze(n,e,t)}T();T();import{randomUUID as ar}from"crypto";function v(n){return`${n}_${ar().replace(/-/g,"").slice(0,24)}`}var Ke=4*1024*1024,lt=8*1024*1024,Ye=512,Xe=20*1024*1024;function N(n,e,t){return{type:"invalid_request_error",code:n,message:e,param:t??null}}function Ce(n,e){return{type:"server_error",code:n,message:e}}function C(n){return{type:"error",event_id:v("event"),error:n}}function ct(){return{outputModalities:["text"],turnDetection:null,inputAudioTranscription:!1}}var b=class extends Error{constructor(t,r,o="invalid_value"){super(t);this.param=r;this.code=o;this.name="SessionUpdateError"}param;code},ur={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},dr={format:!0,transcription:!0,turn_detection:!0,noise_reduction:!0},lr={format:!0,voice:!0,speed:!0};function en(n,e){let t=n;if(!t||typeof t!="object"||t.type!=="audio/pcm"||t.rate!==void 0&&t.rate!==24e3)throw new b(`${e} must be {"type":"audio/pcm","rate":24000} \u2014 the session audio lane is 24 kHz mono PCM16 only`,e)}function Pe(n,e,t){if(!n||typeof n!="object"||Array.isArray(n))throw new b("session must be an object","session","invalid_request");let r=n,o=Object.keys(r).filter(i=>!(i in ur));if(o.length>0)throw new b(`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 b("model must be a string or null","model");s.model=a??void 0;break;case"instructions":if(a!==null&&typeof a!="string")throw new b("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 b("modalities must be a non-empty array","modalities");let d=[];for(let u of a){if(u!=="text"&&u!=="audio")throw new b(`modality ${JSON.stringify(u)} is not "text" or "audio"`,"modalities");if(u==="audio"&&!t.voice)throw new b('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 b("voice must be a string or null","voice");if(!t.voice)throw new b("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 b(`${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 b("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 b(`${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 b("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 b("metadata must be an object or null","metadata");s.metadata=a??null;break;case"turn_detection":{if(a===null){s.turnDetection=null;break}if(!t.turns)throw new b("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 b("turn_detection must be an object or null","turn_detection");let u=String(d.type??"");if(u!=="server_vad"&&u!=="semantic_vad")throw new b(`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(!t.turns)throw new b("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 b("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 b(`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 b("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 b("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 b("audio.input must be an object","audio.input");for(let l of Object.keys(u))if(!(l in dr))throw new b(`audio.input field not supported by this backend: ${l}`,`audio.input.${l}`,"unsupported_field");if(u.format!==void 0&&en(u.format,"audio.input.format"),u.transcription!==null&&u.transcription!==void 0){let l=Pe({input_audio_transcription:u.transcription},s,t);s.inputAudioTranscription=l.inputAudioTranscription}if(u.turn_detection!==null&&u.turn_detection!==void 0){let l=Pe({turn_detection:u.turn_detection},s,t);s.turnDetection=l.turnDetection}if(u.noise_reduction!==null&&u.noise_reduction!==void 0)throw new b("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 b("audio.output must be an object","audio.output");for(let l of Object.keys(u))if(!(l in lr))throw new b(`audio.output field not supported by this backend: ${l}`,`audio.output.${l}`,"unsupported_field");if(u.format!==void 0&&en(u.format,"audio.output.format"),u.speed!==void 0)throw new b("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 b("audio.output.voice must be a string or null","audio.output.voice");if(!t.voice)throw new b("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 Qe(n,e){return{id:e,object:"realtime.session",model:n.model??null,instructions:n.instructions??"",modalities:[...n.outputModalities],voice:n.voice??null,input_audio_format:"pcm16",output_audio_format:"pcm16",input_audio_transcription:n.inputAudioTranscription?{}:null,input_audio_noise_reduction:null,turn_detection:n.turnDetection?{type:"server_vad",create_response:n.turnDetection.createResponse,interrupt_response:n.turnDetection.interruptResponse}:null,temperature:n.temperature??null,max_response_output_tokens:n.maxOutputTokens??"inf",tool_choice:n.toolChoice??"auto",tools:n.tools??[],metadata:n.metadata??null}}function pt(n,e){let t=n;if(!t||typeof t!="object")throw new b("item must be an object","item","invalid_request");if(t.type==="function_call_output"){if(typeof t.call_id!="string"||typeof t.output!="string")throw new b("function_call_output item requires string call_id and output","item.call_id","invalid_request");return{id:typeof t.id=="string"?t.id:v("item"),type:"function_call_output",callId:t.call_id,output:t.output}}if(t.type==="message"){let r=t.role;if(r!=="user"&&r!=="assistant"&&r!=="system")throw new b('message item role must be "user", "assistant" or "system"',"item.role","invalid_request");if(!Array.isArray(t.content))throw new b("message item content must be an array","item.content","invalid_request");let o=[];for(let s of t.content){let i=s;if(!i||typeof i!="object")throw new b("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 b(`${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 b("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 b("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 b(`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 t.id=="string"?t.id:v("item"),type:"message",role:r,content:o}}throw new b(`item type ${String(t.type)} is not supported \u2014 message and function_call_output items only`,"item.type","invalid_request")}function mt(n){let e=[],t=!1;for(let r of n){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}t=!0;continue}}e.push({role:r.role,content:o})}return{input:e,sawAudioOnlyItem:t}}var cr=1001,pr=1011,tn=n=>n.slice(0,120),mr=/^[A-Za-z0-9+/=]*$/,fr=2;function gr(n){let e=Buffer.from(n,"base64");for(let t=0;t+1<e.byteLength;t+=2)if(Math.abs(e.readInt16LE(t))>fr)return!1;return!0}var Ue=class{id=v("conn");ws;binding;onClosed;config=ct();sessionId=v("sess");conversationId=v("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",t=>{this.onMessage(t)}),this.ws.on("close",()=>this.teardown("client disconnected")),this.ws.on("error",()=>this.teardown("client socket error")),this.send({type:"session.created",event_id:v("event"),session:Qe(this.config,this.sessionId)}),this.send({type:"conversation.created",event_id:v("event"),conversation:{id:this.conversationId,object:"realtime.conversation"}}),this.binding.turns&&this.startTurnLane()}startTurnLane(){let e=new AbortController;this.turnLaneAbort=e;let t=this.binding.turns;(async()=>{try{for await(let r of t()){if(this.closed||e.signal.aborted)return;this.onTurnEvent(r)}}catch(r){!this.closed&&!e.signal.aborted&&this.send(C(Ce("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 t=typeof e.text=="string"?e.text:"";if(t.length===0)return;this.turnTranscript.open=!0,this.turnTranscript.text+=t,this.config.inputAudioTranscription&&this.send({type:"conversation.item.input_audio_transcription.delta",event_id:v("event"),item_id:"input_audio_turn",content_index:0,delta:t});return}case"turn_start":{this.turnTranscript={open:!0,text:this.turnTranscript.text},this.send({type:"input_audio_buffer.speech_started",event_id:v("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 t=this.turnTranscript.text.trim(),r=t.length>0?t:null;this.turnTranscript={open:!1,text:""},this.lastCommittedTranscript=r,this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let o=v("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:v("event"),previous_item_id:s,item_id:o}),this.send({type:"conversation.item.created",event_id:v("event"),previous_item_id:s,item:this.itemToProtocol(this.items[this.items.length-1])}),this.send({type:"input_audio_buffer.speech_stopped",event_id:v("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:v("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 t;try{t=e.toString()}catch{this.fail(N("decode_error","frame is not decodable text"),"invalid_frame");return}if(Buffer.byteLength(t,"utf8")>Ke){this.fail(N("frame_too_large",`frame exceeds the ${Ke}-byte limit`),"invalid_frame");return}let r;try{r=JSON.parse(t)}catch{this.fail(N("invalid_json","frame is not valid JSON"),"invalid_frame");return}if(typeof r.type!="string"||r.type.length===0){this.fail(N("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(C({type:"invalid_request_error",code:s.code??"invalid_value",message:s.message,param:s.param??null}));return}this.fail(Ce(null,o instanceof Error?o.message:String(o)),"handler_fault")}}async handle(e){switch(e.type){case"session.update":this.config=Pe(e.session,this.config,{voice:this.binding.voice,turns:this.binding.turns!==void 0}),this.send({type:"session.updated",event_id:v("event"),session:Qe(this.config,this.sessionId)});return;case"conversation.item.create":{let t=pt(e.item,this.binding.voice),r=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem(t),this.send({type:"conversation.item.created",event_id:v("event"),previous_item_id:r,item:this.itemToProtocol(t)});return}case"conversation.item.delete":{let t=this.items.findIndex(r=>r.id===e.item_id);if(t<0){this.send(C(N("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.items.splice(t,1),this.send({type:"conversation.item.deleted",event_id:v("event"),item_id:e.item_id});return}case"conversation.item.retrieve":{let t=this.items.find(r=>r.id===e.item_id);if(!t){this.send(C(N("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.send({type:"conversation.item.retrieved",event_id:v("event"),item:this.itemToProtocol(t)});return}case"conversation.item.truncate":{let t=this.items.find(r=>r.id===e.item_id);if(!t||t.type!=="message"||t.role!=="assistant"){this.send(C(N("item_not_found",`no assistant message item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}t.truncated=!0,this.send({type:"conversation.item.truncated",event_id:v("event"),item_id:t.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(C(N("invalid_audio",'input_audio_buffer.append requires base64 "audio"',"audio")));return}if(!this.ensureVoice())return;if(!mr.test(e.audio)){this.send(C(N("invalid_audio","input_audio_buffer.append audio must be base64","audio")));return}let t=Buffer.byteLength(e.audio,"base64");if(this.pendingAudioBytes.length+t>Xe){this.send(C(N("buffer_full",`uncommitted input audio exceeds the ${Xe}-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+=t;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:v("event")});return}case"input_audio_buffer.commit":{if(!this.ensureVoice())return;if(this.pendingAudioTotalBytes===0&&this.takeTurnTranscript()===null){this.send(C(N("empty_buffer","input_audio_buffer.commit with no buffered audio","audio")));return}this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let t=this.takeTurnTranscript();t===null&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let r=v("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",...t!==null?{transcript:t}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:v("event"),previous_item_id:o,item_id:r}),this.send({type:"conversation.item.created",event_id:v("event"),previous_item_id:o,item:this.itemToProtocol(this.items[this.items.length-1])});return}case"output_audio_buffer.clear":{this.send(C(N("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(C(N("unknown_event",`event type ${e.type} is not supported by this adapter`)))}}ensureVoice(){return!this.binding.voice||!this.binding.audioSession?(this.send(C(N("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,t=e.openaiRealtimeClaims??(e.openaiRealtimeClaims=new Set);if(t.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)");t.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(C({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,t){let r=this.binding.session;if(r&&typeof r.sendResponseCreate=="function")return r.sendResponseCreate(e,e.input??[],t);let o=this.binding.clients;if(o&&typeof o.createResponse=="function")return await o.createResponse(e);throw new Error("realtime binding provides neither session.sendResponseCreate nor clients.createResponse \u2014 no upstream turn seam")}startResponse(e){if(this.inFlight){this.send(C(N("response_in_progress","a response is already in progress \u2014 cancel it with response.cancel first","response.create")));return}let t=e??{};if(t.modalities!==void 0){if(!Array.isArray(t.modalities)||t.modalities.some(R=>R!=="text"&&R!=="audio")){this.send(C(N("invalid_modalities",'response.modalities must be "text"/"audio"',"response.modalities")));return}if(t.modalities.includes("audio")&&!this.ensureVoice())return}if(t.input!==void 0){this.send(C(N("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=v("req"),o=`resp_${r}`;this.activeResponseId=o;let s=this.items.length,{input:i,sawAudioOnlyItem:a}=mt(this.items),d=typeof t.instructions=="string"?t.instructions:this.config.instructions,u=typeof t.temperature=="number"?t.temperature:this.config.temperature,l=t.max_output_tokens??this.config.maxOutputTokens,c=l==="inf"||l===void 0?void 0:l,w=t.tools!==void 0?t.tools:this.config.tools,f=t.tool_choice!==void 0?t.tool_choice:this.config.toolChoice,E={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}:{},...w!==void 0?{tools:w}:{},...f!==void 0?{tool_choice:f}:{}};for(let R of this.frozenAudio.splice(0))this.binding.audioSession?.audio.appendInputAudio(R);this.inFlight=(async()=>{let R=[],m=S=>{this.responseFlushed?this.send(S):R.push(S)},h=()=>{if(!this.responseFlushed){this.send({type:"response.created",event_id:v("event"),response:this.responseObject(o,"in_progress")}),this.send({type:"response.in_progress",event_id:v("event"),response:this.responseObject(o,"in_progress")});for(let S of R)this.send(S);R.length=0,this.responseFlushed=!0}},g={messageOpen:!1,messageText:"",tools:new Map,nextOutputIndex:0},y=null;try{a&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let S;try{S=await this.upstreamStream(E,r)}catch(x){this.emitUpstreamFault(x,o);return}try{for await(let x of S){if(this.cancelled)break;let _=x;if(_.type==="error"){let p=this.upstreamErrorBody(_);this.responseFlushed?this.send(C(p)):this.send({type:"response.failed",event_id:v("event"),response:this.failedResponse(o,p)});return}if(_.type==="response.output_text.delta"&&typeof _.delta=="string"){h(),g.messageOpen||(this.beginMessageItem(o,g.nextOutputIndex++),g.messageOpen=!0),m({type:"response.output_text.delta",event_id:v("event"),item_id:`${o}_msg_0`,response_id:o,output_index:0,content_index:0,delta:_.delta}),g.messageText+=_.delta;continue}if(_.type==="response.function_call_arguments.delta"){h();let p=typeof _.delta=="string"?_.delta:"",O=typeof _.call_id=="string"&&_.call_id.length>0?_.call_id:`${o}_fc_${g.nextOutputIndex}`,P=g.tools.get(O);if(!P){let L=typeof _.name=="string"&&_.name.length>0?_.name:void 0;P={outputIndex:g.nextOutputIndex++,name:L,args:""},g.tools.set(O,P),m({type:"response.output_item.added",event_id:v("event"),response_id:o,output_index:P.outputIndex,item:{id:O,object:"realtime.item",type:"function_call",call_id:O,name:L??"",arguments:"",status:"in_progress"}})}typeof _.name=="string"&&_.name.length>0&&(P.name=_.name),P.args+=p,m({type:"response.function_call_arguments.delta",event_id:v("event"),item_id:O,response_id:o,output_index:P.outputIndex,call_id:O,name:P.name??"",delta:p});continue}if(_.type==="response.completed"){y=_.response??null;continue}}}catch(x){this.emitUpstreamFault(x,o);return}if(this.cancelled)return;this.closeOpenItems(o,g,s),h(),this.send({type:"response.done",event_id:v("event"),response:this.doneResponse(o,s,y)})}finally{this.inFlight=null,this.activeResponseId=null}})()}onOutputAudioFrame(e,t){if(this.cancelled)return;if(this.activeResponseId!==e){if(gr(t))return;!this.outOfTurnAudioWarned&&!this.closed&&(this.outOfTurnAudioWarned=!0,this.send(C({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:v("event"),response:this.responseObject(e,"in_progress")}),this.send({type:"response.in_progress",event_id:v("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:v("event"),item_id:r,response_id:e,output_index:0,content_index:0,delta:t})}messageItemAdded=!1;messageItemOpenForAudio=!1;beginMessageItem(e,t){let r=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:v("event"),response_id:e,output_index:t,item:{id:r,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:v("event"),item_id:r,response_id:e,output_index:t,content_index:0,part:{type:"output_text",text:""}})}beginAudioMessageItem(e){let t=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:v("event"),response_id:e,output_index:0,item:{id:t,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:v("event"),item_id:t,response_id:e,output_index:0,content_index:0,part:{type:"output_audio",transcript:""}})}closeOpenItems(e,t,r){if(t.messageOpen||this.messageItemOpenForAudio){let o=`${e}_msg_0`,s=this.messageItemOpenForAudio;s&&this.send({type:"response.output_audio.done",event_id:v("event"),item_id:o,response_id:e,output_index:0,content_index:0}),t.messageText.length>0&&this.send({type:"response.output_text.done",event_id:v("event"),item_id:o,response_id:e,output_index:0,content_index:0,text:t.messageText}),this.send({type:"response.content_part.done",event_id:v("event"),item_id:o,response_id:e,output_index:0,content_index:0,part:s?{type:"output_audio",transcript:""}:{type:"output_text",text:t.messageText}});let i=[];s&&i.push({type:"output_audio"}),t.messageText.length>0&&i.push({type:"output_text",text:t.messageText});let a={id:o,type:"message",role:"assistant",content:i};this.pushItem(a),this.send({type:"response.output_item.done",event_id:v("event"),response_id:e,output_index:0,item:this.itemToProtocol(a)})}for(let[o,s]of t.tools){let i=s.args.length>0?s.args:"{}";this.send({type:"response.function_call_arguments.done",event_id:v("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:v("event"),response_id:e,output_index:s.outputIndex,item:this.itemToProtocol(a)})}this.messageItemOpenForAudio=!1,this.messageItemAdded=!1}responseObject(e,t,r){return{id:e,object:"realtime.response",status:t,conversation_id:this.conversationId,usage:null,...r??{}}}failedResponse(e,t){return this.responseObject(e,"failed",{status_details:{error:{code:t.code,message:t.message}}})}doneResponse(e,t,r){let o=r&&typeof r.usage=="object"&&r.usage!==null?r.usage:null,s=[];for(let i=t;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 t=e.error,r=t&&typeof t.message=="string"?t.message:JSON.stringify(e);return{type:"server_error",code:t&&typeof t.code=="string"?t.code:null,message:r}}emitUpstreamFault(e,t){if(this.cancelled)return;let r=Ce(null,e instanceof Error?e.message:String(e));this.responseFlushed?this.send(C(r)):this.send({type:"response.failed",event_id:v("event"),response:this.failedResponse(t,r)})}cancelResponse(){let e=this.inFlight;if(!e){this.send(C(N("no_response_to_cancel","no response is in progress","response.cancel")));return}this.cancelled=!0;let t=this.activeResponseId??"resp_";e.finally(()=>{this.send({type:"response.cancelled",event_id:v("event"),response:this.responseObject(t,"cancelled")})})}pushItem(e){this.items.push(e),this.items.length>Ye&&(this.items.shift(),this.send(C({type:"invalid_request_error",code:"transcript_evicted",message:`the conversation transcript exceeded ${Ye} items \u2014 the oldest item was evicted to keep the per-connection bound`})))}itemToProtocol(e){let t={id:e.id,object:"realtime.item"};if(e.type==="function_call")return{...t,type:"function_call",call_id:e.callId,name:e.name,arguments:e.arguments??"{}",status:"completed"};if(e.type==="function_call_output")return{...t,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{...t,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>lt){this.ws.close(cr,tn("outbound buffer exceeded")),this.teardown("outbound buffer exceeded");return}this.ws.send(JSON.stringify(e))}}fail(e,t){this.send(C(e)),this.ws.close(pr,tn(t)),this.teardown(t)}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(t=>nn(this.id,`binding release failed: ${t instanceof Error?t.message:String(t)}`));try{this.ws.removeAllListeners("message")}catch{}this.onClosed?.(this),nn(this.id,e)}}},ft=[];function nn(n,e){ft.push({id:n,reason:e}),ft.length>256&&ft.shift()}T();import{SessionFailedError as yr}from"@urun-sh/core";T();import{COLD_BOOT_WHEN_LIVE_TIMEOUT_MS as hr}from"@urun-sh/core";var _r={no_capacity:429,queue_timeout:429,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502};function Ze(n){if(n==null)return null;let e=_r[n];return e===void 0?null:{status:e,code:n,retryable:e!==502,openaiType:e===429?"capacity_error":"upstream_error",anthropicType:e===429?"rate_limit_error":e===503?"overloaded_error":"api_error"}}function et(n){if(n.kind==="create-failed")return Ze(n.status.error?.code??null);if(n.kind!=="never-live")return null;let e=(n.status.elapsedMs??0)>=hr;return Ze(e?"cold_start_exceeded":"queue_timeout")}import{WebSocketServer as vr}from"ws";var gt="/v1/realtime",wr=n=>n.slice(0,120);function xr(n,e,t){let r=e===401||e===403?"Unauthorized":e===502?"Upstream Fault":"Upgrade Refused";n.write(`HTTP/1.1 ${e} ${r}\r
|
|
6
|
-
Connection: close\r
|
|
7
|
-
Content-Type: text/plain\r
|
|
8
|
-
\r
|
|
9
|
-
${wr(t)}\r
|
|
10
|
-
`),n.destroy()}function ht(n,e){let t=new vr({noServer:!0}),r=new Set;return n.on("upgrade",(o,s,i)=>{if(new URL(o.url??"/","http://localhost").pathname!==gt)return;r.add(s),s.on("close",()=>{r.delete(s)});let d=l=>{let c=l,w=c.session!=null&&typeof c.session.sendResponseCreate=="function",f=c.clients!=null&&typeof c.clients.createResponse=="function";if(!w&&!f)throw new Error("resolver returned a binding without an upstream turn seam (session.sendResponseCreate or clients.createResponse required) \u2014 refusing the upgrade LOUD");return new Promise((E,R)=>{t.handleUpgrade(o,s,i,m=>{try{new Ue({ws:m,binding:l}),E()}catch(h){R(h)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o).then(d).catch(l=>{let c=l instanceof yr?et(l):null,w=null;if(l!==null&&typeof l=="object"&&"status"in l){let R=l.status;typeof R=="number"&&(w=R)}let f=w??c?.status??502,E=l instanceof Error?l.message:String(l);s.destroyed||xr(s,f,E)}).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=>t.close(()=>s()))}}T();T();T();import{createServer as Oo}from"http";T();import{WebSocketServer as Pr,WebSocket as qe}from"ws";T();T();import{randomUUID as br}from"crypto";var rn="Inference-Id";function Le(){return br()}function _t(n){return n==="/v1"||n.startsWith("/v1/")}function on(n,e){if(!_t(n)||e===null)throw new Error(`no inference turn for ${n}: 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 $e(n){return`resp_${n}`}function sn(n){return`chatcmpl-${n}`}function an(n){return`msg_${n}`}function Z(n){return e=>{n.sessionId=e.sessionId,n.catalog=e.identity}}function un(n){let e=n.now??(()=>performance.now());return{event:"inference_request",ts:new Date().toISOString(),inference_id:n.turn.id,method:n.method,path:n.path,status:n.res.headersSent?n.res.statusCode:null,delivered:n.res.writableFinished,stream_error:n.turn.streamError,model:n.turn.model,stream:n.turn.stream,org:n.turn.org,duration_ms:e()-n.turn.startedAt,ttft_ms:n.turn.stream===!0?n.turn.ttftMs:null,prompt_tokens:n.turn.promptTokens,completion_tokens:n.turn.completionTokens,output_units:n.turn.outputUnits,session_id:n.turn.sessionId,queue_ms:n.turn.timing?.queue_ms??null,prefill_ms:n.turn.timing?.prefill_ms??null,decode_ms:n.turn.timing?.decode_ms??null,total_ms:n.turn.timing?.total_ms??null,catalog:n.turn.catalog}}function dn(n){return`${JSON.stringify(n)}
|
|
11
|
-
`}var kr=256,Rr=128;function ee(n){return typeof n.model!="string"||!n.model.trim()?"(default)":n.model.trim().slice(0,Rr)}var pe=class n{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(e,t,r){this.requests[e]=(this.requests[e]??0)+1,this.localInBytes+=t,this.naiveInBytes+=r}modelEntry(e){let t=this.models[e];if(t)return t;let r=Object.keys(this.models).length>=kr?"(other)":e;return this.models[r]??={requests:0,tokens_out:0}}model(e){this.modelEntry(e).requests+=1}track(e,t,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,t!==void 0&&(o.modelEntry(t).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 t=[...e].sort((r,o)=>r-o);return t[Math.floor(t.length/2)]}snapshot(){let e=Object.values(this.requests).reduce((t,r)=>t+r,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:n.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:n.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:e},persistent_connections:1,history_bytes_saved:Math.max(0,this.naiveInBytes-this.localInBytes),history_bytes_saved_pct:this.naiveInBytes>0?Math.round((1-this.localInBytes/this.naiveInBytes)*1e3)/10:null}}}},Sr=256;function Ir(n){if(typeof n=="string")return[{role:"user",content:n}];if(Array.isArray(n))return n;throw new Error("responses input must be a string or an array of messages/items")}var me=class extends Error{},ye=class{conversations=new Map;thread(e,t){let r=Ir(t);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new me(`unknown previous_response_id ${String(e)} (proxy restarts drop stored responses)`);return[...o,...r]}remember(e,t,r,o=!0){this.conversations.set(e,[...t,...r]);for(let s of this.conversations.keys()){if(this.conversations.size<=Sr)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},ln=new WeakMap;function Ne(n){let e=ln.get(n);return e||(e=new ye,ln.set(n,e)),e}function ve(n){let e=n?.output;return Array.isArray(e)?e.filter(t=>t.type==="function_call"):[]}function te(n){return n.find(e=>e.type==="response.completed")?.response??null}function we(n){return n.type!=="response.function_call_arguments.delta"||typeof n.tool_index!="number"?null:{tool_index:n.tool_index,call_id:typeof n.call_id=="string"?n.call_id:void 0,name:typeof n.name=="string"?n.name:void 0,delta:typeof n.delta=="string"?n.delta:""}}var ie=class{calls=new Map;add(e){let t=this.calls.get(e.tool_index),r=!t;return t||(t={args:""},this.calls.set(e.tool_index,t)),e.call_id&&(t.id=e.call_id),e.name&&(t.name=e.name),t.args+=e.delta,{call:t,isNew:r}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([e,t])=>({type:"function_call",call_id:t.id??`call_${e}`,name:t.name??"",arguments:t.args}))}},Ar=["<tool_call>","<function="];function Er(n){return Ar.find(e=>n.includes(e))}function fe(n,e){return`serving row emitted a prose tool call (literal ${JSON.stringify(e)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${n}" (see catalog parser_defaults)`}function ge(n,e,t){if(!(!n||e>0))return Er(t)}function yt(n,e){let t=e?.output_text;return typeof t=="string"?t:n.filter(r=>r.type==="response.output_text.delta"&&typeof r.delta=="string").map(r=>r.delta).join("")}function Or(n){let e=n?.output;if(!Array.isArray(e))return[];let t=[];for(let r of e){if(r===null||typeof r!="object")continue;let o=r;o.type==="message"?t.push({role:"assistant",content:o.content??""}):o.type==="function_call"&&t.push({type:"function_call",call_id:o.call_id,name:o.name,arguments:o.arguments})}return t}function vt(n,e){let t=Or(n);return t.length>0?t:e?[{role:"assistant",content:e}]:[]}var K=class extends Error{constructor(t,r,o=400,s){super(t);this.code=r;this.status=o;this.param=s}code;status;param};function xe(n,e){try{return n.thread(e.previous_response_id,e.input)}catch(t){throw t instanceof me?new K(t.message,"previous_response_not_found",400,"previous_response_id"):new K(String(t instanceof Error?t.message:t),"invalid_input")}}function cn(n){let e=n.text;if(e!==null&&typeof e=="object"){let t=e.format;if(t!==void 0)return t}}async function wt(n,e,t,r,o){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new K("instructions must be a string","invalid_instructions");let s=typeof e.instructions=="string"?e.instructions:void 0;return n.createResponse({model:e.model,input:t,stream:r,tools:e.tools,tool_choice:e.tool_choice,temperature:e.temperature,top_p:e.top_p,stop:e.stop,...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}:{},...cn(e)!==void 0?{response_format:cn(e)}:{}},o)}async function Me(n){let{body:e,clients:t,store:r,stats:o}=n,s=xe(r,e);e.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(s))-Buffer.byteLength(JSON.stringify(e.input)));let i=n.responseId,a=n.remember??e.store!==!1,d=e.store!==!1,u=ee(e);o.model(u);let l=Array.isArray(e.tools)&&e.tools.length>0,c=n.turn,w=await wt(t,e,s,!0,c?Z(c):void 0);return{responseId:i,events:Tr({events:w,conversation:s,responseId:i,modelKey:u,requestHadTools:l,remember:a,persist:d,store:r,stats:o,turn:n.turn})}}async function*Tr(n){let{events:e,conversation:t,responseId:r,modelKey:o,requestHadTools:s,remember:i,persist:a,store:d,stats:u}=n,l=[],c=k=>k.response&&typeof k.response=="object"?{...k,response:{...k.response,id:r}}:k,w=`item_${r.slice(5)}`,f=`rs_${r.slice(5)}`,E=!1,R=!1,m=0;function*h(){E||(E=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let g=!1,y=0,S="";function*x(){g&&(g=!1,yield{type:"response.reasoning_text.done",item_id:f,output_index:y,content_index:0,text:S},yield{type:"response.output_item.done",output_index:y,item:{id:f,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:S}],status:"completed"}})}let _=!1,p=0,O="";function*P(k){_&&(_=!1,yield{type:"response.output_text.done",item_id:w,output_index:p,content_index:0,text:O},yield{type:"response.content_part.done",item_id:w,output_index:p,content_index:0,part:{type:"output_text",text:O}},yield{type:"response.output_item.done",output_index:p,item:{id:w,type:"message",role:"assistant",status:k,content:[{type:"output_text",text:O}]}})}let L=new ie,I=null,Y=-1;function*V(k){if(!I)return;let U=I;I=null,yield{type:"response.function_call_arguments.done",item_id:U.fcId,output_index:U.outputIndex,arguments:U.args},yield{type:"response.output_item.done",output_index:U.outputIndex,item:{id:U.fcId,type:"function_call",call_id:U.callId??U.fcId,name:U.name??"",arguments:U.args,status:k}}}let B=!1;for await(let k of u.track(e,o,n.turn)){l.push(k);let U=String(k.type??"");if(U==="response.created"||U==="response.in_progress"){E=!0,yield c(k);continue}if(U==="response.output_item.added"){R=!0,yield c(k);continue}if(U==="response.reasoning_text.delta"&&!R){yield*h(),g||(g=!0,y=m++,yield{type:"response.output_item.added",output_index:y,item:{id:f,type:"reasoning",summary:[],content:[],status:"in_progress"}}),S+=String(k.delta??""),yield{type:"response.reasoning_text.delta",item_id:f,output_index:y,content_index:0,delta:k.delta};continue}if(U==="response.output_text.delta"&&!R){yield*h(),yield*x(),_||(_=!0,p=m++,yield{type:"response.output_item.added",output_index:p,item:{id:w,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:w,output_index:p,content_index:0,part:{type:"output_text",text:""}}),O+=String(k.delta??""),yield{type:U,item_id:w,output_index:p,content_index:0,delta:k.delta};continue}let M=R?null:we(k);if(M){if(yield*h(),yield*x(),yield*P("completed"),L.add(M),M.tool_index!==Y){yield*V("completed"),Y=M.tool_index;let z=m++;I={fcId:`fc_${r.slice(5)}_${M.tool_index}`,outputIndex:z,callId:M.call_id,name:M.name,args:""},yield{type:"response.output_item.added",output_index:z,item:{id:I.fcId,type:"function_call",call_id:I.callId??I.fcId,name:I.name??"",arguments:"",status:"in_progress"}}}I&&(M.call_id&&(I.callId=M.call_id),M.name&&(I.name=M.name),I.args+=M.delta,yield{type:"response.function_call_arguments.delta",item_id:I.fcId,output_index:I.outputIndex,delta:M.delta});continue}if(U==="response.completed"&&!R){let z=ve(k.response),ue=ge(s,L.size+z.length,O);if(ue){B=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:fe(o,ue)}};break}if(yield*x(),yield*P("completed"),yield*V("completed"),L.size===0)for(let de of z){let se=m++,Q=de.id??de.call_id??`fc_${se}`,Ee=de.arguments??"",_e={id:Q,type:"function_call",call_id:de.call_id??Q,name:de.name??""};yield{type:"response.output_item.added",output_index:se,item:{..._e,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:Q,output_index:se,delta:Ee},yield{type:"response.function_call_arguments.done",item_id:Q,output_index:se,arguments:Ee},yield{type:"response.output_item.done",output_index:se,item:{..._e,arguments:Ee,status:"completed"}}}yield c(k);continue}yield c(k)}if(yield*x(),yield*P("incomplete"),yield*V("incomplete"),i&&!B){let k=te(l);d.remember(r,t,vt(k,yt(l,k)),a)}}var Cr="/v1/responses",Ur=64*1024*1024,Lr=8*1024*1024,$r=16,pn=32,mn=3600*1e3,Nr=/^[A-Za-z0-9._-]{1,256}$/;function H(n,e,t){return{type:"error",status:n,...t!==void 0?{stream_id:t}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var Mr=H(400,{type:"invalid_request_error",code:"invalid_stream_id",message:"The 'stream_id' field must be a non-empty string with at most 256 characters and may only contain letters, numbers, underscores, hyphens, and periods.",param:"stream_id"});function kt(){let n,e;return{promise:new Promise((r,o)=>{n=r,e=o}),resolve:n,reject:e}}async function qr(n){for(;n.bufferedAmount>Lr&&n.readyState===qe.OPEN;){let{promise:e,resolve:t}=kt();setTimeout(t,5),await e}}var xt=class extends ye{constructor(t){super();this.shared=t}shared;thread(t,r){if(t==null)return super.thread(t,r);try{return super.thread(t,r)}catch(o){if(!(o instanceof me))throw o;return this.shared.thread(t,r)}}remember(t,r,o,s=!0){s?this.shared.remember(t,r,o):super.remember(t,r,o)}forget(t){super.forget(t),this.shared.forget(t)}},bt=class{constructor(e,t,r,o){this.ws=e;this.clients=t;this.stats=r;this.statusOf=o;this.store=new xt(Ne(t)),e.on("message",(i,a)=>this.onMessage(i,a)),e.on("close",()=>this.dispose()),e.on("error",()=>this.dispose()),setTimeout(()=>{this.closed||(this.sendFrame(H(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${mn/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},mn).unref?.()}ws;clients;stats;statusOf;store;lanes=new Map;laneOfResponse=new Map;activeTurns=new Set;active=0;waiters=[];closed=!1;onMessage(e,t){if(this.closed)return;if(t){this.sendFrame(H(400,{type:"invalid_request_error",code:"binary_frame",message:"Responses WebSocket frames must be JSON text"})),this.ws.close(1003,"binary frames are not supported");return}let r;try{r=JSON.parse(e.toString())}catch{this.sendFrame(H(400,{type:"invalid_request_error",code:"invalid_json",message:"frame is not valid JSON"}));return}if(r===null||typeof r!="object"||Array.isArray(r)){this.sendFrame(H(400,{type:"invalid_request_error",code:"invalid_event",message:"frame must be a JSON object"}));return}let o=r;if(o.type!=="response.create"){this.sendFrame(H(400,{type:"invalid_request_error",code:"unknown_event",message:`unknown client event type ${JSON.stringify(o.type??null)}; only "response.create" is supported`}));return}if("stream"in o){this.sendFrame(H(400,{type:"invalid_request_error",code:"stream_is_implicit",message:"'stream' is implicit over the Responses WebSocket and must not be sent",param:"stream"}));return}if("background"in o){this.sendFrame(H(400,{type:"invalid_request_error",code:"background_not_supported",message:"'background' is not supported over the Responses WebSocket",param:"background"}));return}let s;if(o.stream_id!==void 0){if(typeof o.stream_id!="string"||!Nr.test(o.stream_id)){this.sendFrame(Mr);return}if(s=o.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=pn){this.sendFrame(H(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${pn}). 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,t){if(!this.closed){await this.acquire();try{let r=Buffer.byteLength(JSON.stringify(e));this.stats.request("responses_ws",r,r);let o;try{o=await Me({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:$e(Le())})}catch(i){this.failTurn(i,t,e);return}this.laneOfResponse.set(o.responseId,t??null);let s=o.events;this.activeTurns.add(s);try{for await(let i of s){if(this.closed||this.ws.readyState!==qe.OPEN)break;this.send(i,t),await qr(this.ws)}}finally{this.activeTurns.delete(s)}}catch(r){this.failTurn(r,t,e)}finally{this.release()}}}runWarmup(e,t){if(this.closed)return;let r=$e(Le());try{let o=xe(this.store,{...e,input:e.input??(e.previous_response_id!=null?[]:void 0)});this.store.remember(r,o,[],e.store!==!1),this.laneOfResponse.set(r,t??null),this.send({type:"response.created",response:{id:r,object:"response",status:"in_progress"}},t)}catch(o){this.failTurn(o,t,e)}}failTurn(e,t,r){this.sendFrame(this.errorFrameOf(e,t));let o=r.previous_response_id;typeof o=="string"&&this.laneOfResponse.get(o)===(t??null)&&(this.store.forget(o),this.laneOfResponse.delete(o))}errorFrameOf(e,t){let r=String(e instanceof Error?e.message:e);if(e instanceof K)return H(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},t);if(e instanceof G)return H(404,{type:"invalid_request_error",code:"model_not_found",message:r},t);if(e instanceof Oe)return H(503,{type:"upstream_error",code:"session_gone",message:r},t);let o=this.statusOf?.(e)??null;return o?H(o.status,{type:o.openaiType,message:r},t):H(500,{type:"upstream_error",message:r},t)}async acquire(){for(;this.active>=$r;){let{promise:e,resolve:t}=kt();this.waiters.push(t),await e}this.active++}release(){this.active--,this.waiters.shift()?.()}dispose(){if(!this.closed){this.closed=!0;for(let e of[...this.activeTurns])e.return(void 0);this.activeTurns.clear(),this.lanes.clear(),this.laneOfResponse.clear();for(let e of this.waiters.splice(0))e()}}send(e,t){if(this.closed||this.ws.readyState!==qe.OPEN)return;let r=t===void 0?e:{...e,stream_id:t};this.ws.send(JSON.stringify(r))}sendFrame(e){this.closed||this.ws.readyState!==qe.OPEN||this.ws.send(JSON.stringify(e))}};function tt(n,e,t){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${t}\r
|
|
12
|
-
`;n.end(`HTTP/1.1 ${e} ${r}\r
|
|
13
|
-
Connection: close\r
|
|
14
|
-
Content-Type: text/plain; charset=utf-8\r
|
|
15
|
-
Content-Length: ${Buffer.byteLength(o)}\r
|
|
16
|
-
\r
|
|
17
|
-
${o}`)}function fn(n,e){let t=new Pr({noServer:!0,maxPayload:Ur}),r=new pe,o=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,d=!1,u=(l,c,w)=>{let f;try{f=new URL(l.url??"/","http://localhost")}catch{tt(c,400,"malformed upgrade URL");return}if(c.on("error",()=>c.destroy()),c.on("close",()=>i.delete(c)),f.pathname!==Cr){i.add(c);return}s.add(c);let E=!1;(async()=>{if(e.apiKey&&(l.headers.authorization??"")!==`Bearer ${e.apiKey}`){tt(c,401,"invalid local proxy api key");return}let R;try{R=await a(l)}catch(m){let h=e.statusOf?.(m)??null;tt(c,h?.status??500,"upstream resolver rejected the connection");return}d||c.destroyed||(E=!0,t.handleUpgrade(l,c,w,m=>{s.delete(c),o.add(m),m.on("close",()=>o.delete(m));try{new bt(m,R,r,e.statusOf)}catch{o.delete(m),m.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!E&&!c.destroyed&&tt(c,500,"responses websocket upgrade failed")}).finally(()=>s.delete(c))};return n.on("upgrade",u),{stats:r,close:()=>{let{promise:l,resolve:c}=kt();if(d)return c(),l;d=!0,n.off("upgrade",u);for(let f of s)f.destroy();s.clear();for(let f of i)f.destroy();i.clear();for(let f of o)f.close(1001,"server shutting down");let w=setTimeout(()=>{for(let f of o)f.readyState!==qe.CLOSED&&f.terminate()},2e3);return w.unref?.(),t.close(()=>{clearTimeout(w),c()}),l}}}T();import Fr from"os";import Dr from"path";import{fileURLToPath as jr}from"url";import{format as Gr}from"util";var gn="0.5.6";var _n=4141,Hr=gn,Hs={claude:{bin:"claude",env:"anthropic"},codex:{bin:"codex",env:"openai"},pi:{bin:"pi",env:"openai"},omp:{bin:"omp",env:"openai"},aider:{bin:"aider",env:"openai"},opencode:{bin:"opencode",env:"openai"},crush:{bin:"crush",env:"openai"}};function Wr(n,e="openai"){if(e==="anthropic")return{ANTHROPIC_BASE_URL:`http://127.0.0.1:${n}`,ANTHROPIC_API_KEY:"urun-local"};let t=`http://127.0.0.1:${n}/v1`;return{OPENAI_BASE_URL:t,OPENAI_API_BASE:t,OPENAI_API_KEY:"urun-local"}}function Ws(n,e,t){let r={...n,...Wr(e,t)};return delete r.URUN_API_KEY,delete r.URUN_JWT,r}function Js(n,e,t){let r=["-c","model_providers.urun.name=urun","-c",`model_providers.urun.base_url="http://127.0.0.1:${n}/v1"`,"-c",'model_providers.urun.wire_api="responses"',"-c",'model_provider="urun"'],o=t.indexOf("--"),s=o===-1?t:t.slice(0,o),i=s.some(l=>l==="-m"||l==="--model"||l.startsWith("--model=")),a=t[0]==="exec",d=s.includes("--skip-git-repo-check"),u=a&&!d?["exec","--skip-git-repo-check",...t.slice(1)]:t;return[...r,...i?[]:["-m",e],...u]}function Vs(n,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--provider","urun","--model",n,...e]}function zs(n,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--model",`urun/${n}`,...e]}function Ks(n,e,t){let r=(n.URUN_PI_EXTENSION_ENTRY??"").trim();if(r){if(!t(r))throw new Error(`URUN_PI_EXTENSION_ENTRY points at ${r}, which does not exist \u2014 the launcher that injected it (the vendored urun CLI shim) must materialize the pi extension bundle before spawning pi`);return r}let o=jr(new URL("../pi-extension/standalone.cjs",e));if(!t(o))throw new Error(`pi extension entry not found at ${o} \u2014 the pi lane loads the self-contained @urun-sh/openai dist/pi-extension/standalone.cjs from the installed package (run \`urun compat pi\` via the npm-installed CLI, e.g. \`npx @urun-sh/openai\`), or pass an entry to pi yourself: \`pi -e <path-to-@urun-sh/openai>/dist/pi-extension/standalone.cjs\``);return o}function yn(n){let e=Number(n);if(!Number.isInteger(e)||e<0)throw new Error(`--port needs a non-negative integer (0 = OS-assigned ephemeral), got ${n}`);return e}function Ys(n){if(n[0]!=="--port")return null;let e=yn(n[1]);return n.splice(0,2),e}function Xs(n){let e=n.indexOf("--port");if(e===-1)return _n;let t=yn(n[e+1]);return n.splice(e,2),t}function Qs(n){if(n===0)throw new Error("`env` cannot use --port 0: it starts no proxy, so the OS never assigns the ephemeral port \u2014 pass the actual port of the running proxy");return n}function Rt(n,e){let t=(n[e]??"").trim();if(!t)throw new Error(`${e} is not set. The proxy needs URUN_BASE_URL, URUN_ORG_ID, URUN_APP and either URUN_API_KEY (the org key; the proxy vends short-lived function-scoped client tokens from it via the SDK) or URUN_JWT (an explicit pre-vended token override) \u2014 plus optional URUN_FUNCTION, default "serve" \u2014 to open the backhaul session.`);return t}function Zs(n){let e=Jr(Rt(n,"URUN_BASE_URL")),t=Rt(n,"URUN_ORG_ID"),r=Rt(n,"URUN_APP"),o=(n.URUN_FUNCTION??"serve").trim()||"serve",s=(n.URUN_API_URL??"").trim(),i=s?hn(s,"URUN_API_URL"):Nt,a=(n.URUN_JWT??"").trim();if(a)return{baseUrl:e,orgId:t,appId:r,fnName:o,apiUrl:i,auth:{lane:"jwt",jwt:a}};let d=(n.URUN_API_KEY??"").trim();if(d){let u=(n.URUN_GATEWAY_URL??"").trim(),l=u?hn(u):void 0;return{baseUrl:e,orgId:t,appId:r,fnName:o,apiUrl:i,auth:{lane:"api-key",apiKey:d,gatewayUrl:l}}}throw new Error("neither URUN_API_KEY nor URUN_JWT is set. Set URUN_API_KEY (the org API key \u2014 the proxy vends short-lived, function-scoped client tokens from it via the SDK, so the key never reaches the agent process) or URUN_JWT (an explicit pre-vended token; when set it always wins). URUN_BASE_URL, URUN_ORG_ID and URUN_APP are also required.")}function Jr(n,e="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(n).pathname))throw new Error(`${e} must be the session-gateway base (e.g. https://api.urun.sh), got ${n} \u2014 a trailing /v1 is the org control-plane API form (that belongs in URUN_API_URL); session allocation 404s against it. Drop the /v1.`);return n}function hn(n,e="URUN_GATEWAY_URL"){let t=new URL(n),r=t.hostname==="localhost"||t.hostname==="127.0.0.1"||t.hostname==="[::1]";if(t.protocol!=="https:"&&!r)throw new Error(`${e} must be https (got ${n}) \u2014 the org API key rides the request's Authorization header`);return n}function ei(n){return{app:n.appId,org:n.orgId,fn:n.fnName,base_url:n.baseUrl,proxy_version:Hr}}function Vr(n,e){return n.app===e.app&&n.org===e.org&&n.fn===e.fn&&n.base_url===e.base_url&&n.proxy_version===e.proxy_version}var zr=250;async function Kr(n,e=zr){try{let t=await fetch(`http://127.0.0.1:${n}/stats`,{signal:AbortSignal.timeout(e)});if(!t.ok)return null;let r=(await t.json()).identity;if(r==null||typeof r!="object")return null;let{app:o,org:s,fn:i,base_url:a,proxy_version:d}=r;return typeof o!="string"||typeof s!="string"||typeof i!="string"||typeof a!="string"||typeof d!="string"?null:{app:o,org:s,fn:i,base_url:a,proxy_version:d}}catch{return null}}var vn=2e3;async function Yr(n,e=vn){try{let t=await fetch(`http://127.0.0.1:${n}/v1/models`,{signal:AbortSignal.timeout(e)});return t.ok?null:`GET /v1/models answered HTTP ${t.status}`}catch(t){return`GET /v1/models failed (${String(t instanceof Error?t.message:t)})`}}async function ti(n,e,t=_n,r=vn){if(n!=null)return{mode:"spawn",port:n};let o=await Kr(t);if(o!==null&&Vr(o,e)){let s=await Yr(t,r);return s===null?{mode:"reuse",port:t,identity:o}:{mode:"spawn",port:0,reason:`a urun-openai proxy on port ${t} matches this invocation's identity but failed the serving-surface liveness check (${s}) \u2014 starting a fresh proxy on an ephemeral port instead of pointing the harness at it`}}return{mode:"spawn",port:0}}function ni(n,e=Fr.homedir()){return Dr.join(e,".urun","logs",`compat-proxy-${n}.log`)}var St=["log","info","warn","error","debug","trace"];function ri(n,e=process,t=console){let r=e.stdout.write,o=e.stderr.write,s=(a,...d)=>n.write(a,...d);e.stdout.write=s,e.stderr.write=s;let i=Object.fromEntries(St.map(a=>[a,t[a]]));for(let a of St)t[a]=(...d)=>{n.write(`${Gr(...d)}
|
|
18
|
-
`)};return()=>{e.stdout.write=r,e.stderr.write=o;for(let a of St)t[a]=i[a]}}var Xr=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function wn(n){let e=n?.code;return typeof e=="string"&&Xr.has(e)}function be(n,e){try{return n.write(e),!0}catch(t){if(wn(t))return!1;throw t}}function oi(n){let e={stdout:!1,stderr:!1},t=[];for(let r of["stdout","stderr"]){let o=n.streams[r],s=i=>{wn(i)&&(e[r]=!0);let a=i?.code;try{n.note(`${new Date().toISOString()} urun compat: process.${r} died (${String(a??(i instanceof Error?i.message:i))}) \u2014 the terminal is gone; launcher output continues in this log only
|
|
19
|
-
`)}catch{}};o.on("error",s),t.push([o,s])}return{isDead:r=>e[r],uninstall:()=>{for(let[r,o]of t)r.off("error",o)}}}function si(n){let e=t=>{let r=t instanceof Error?`${t.stack??`${t.name}: ${t.message}`}`:String(t);try{n.appendSync(n.logPath,`${new Date().toISOString()} launcher crashed (in-process proxy): ${r}
|
|
20
|
-
`)}catch{}n.restore();try{n.writeStderr(`urun compat: the in-process proxy crashed (${t instanceof Error?t.message:String(t)}) \u2014 terminating the harness (its backend is gone); details: ${n.logPath}
|
|
21
|
-
`)}catch(o){try{n.appendSync(n.logPath,`${new Date().toISOString()} urun compat: the crash line could not reach stderr (${String(o?.code??o)}) \u2014 the terminal is gone
|
|
22
|
-
`)}catch{}}n.killChild(),n.exit(1)};return n.proc.on("uncaughtException",e),n.proc.on("unhandledRejection",e),()=>{n.proc.off("uncaughtException",e),n.proc.off("unhandledRejection",e)}}function ii(n,e){let t="pass --port 0 for an OS-assigned ephemeral port";return e?`port ${n} is held by another urun-openai proxy serving app ${e.app} (org ${e.org}, fn ${e.fn}, version ${e.proxy_version}) \u2014 reuse it by pointing your agent at http://127.0.0.1:${n}, or ${t}`:`port ${n} is already in use and does not answer the urun-openai /stats identity probe \u2014 ${t}`}T();var xn="/v1/images/generations",nt="/v1/images/edits";function bn(n){return n===xn||n===nt}var kn="/v1/images/models";async function Rn(n,e,t){if(n){if(n.model=ee({model:t}),n.stream=!1,typeof e.catalogIdentity!="function"){n.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(t);if(r!==null){n.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(t):null;n.catalog={kind:"not-a-catalog-model",model:o??t??null}}catch(r){n.catalog={kind:"unresolved",reason:`model ${JSON.stringify(t??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}var A=class extends Error{status;param;constructor(e,t,r){super(t),this.status=e,this.param=r}},Qr=16*1024*1024,Sn=64*1024*1024;function In(n){return n.length>=8&&n[0]===137&&n[1]===80&&n[2]===78&&n[3]===71&&n[4]===13&&n[5]===10&&n[6]===26&&n[7]===10?"image/png":n.length>=3&&n[0]===255&&n[1]===216&&n[2]===255?"image/jpeg":n.length>=12&&n[0]===82&&n[1]===73&&n[2]===70&&n[3]===70&&n[8]===87&&n[9]===69&&n[10]===66&&n[11]===80?"image/webp":null}function Be(n,e){n.statusCode=e.status,n.setHeader("content-type","application/json"),n.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}var ne=class extends Error{};async function Zr(n,e){let t=Number(n.headers["content-length"]??"0");if(Number.isFinite(t)&&t>e)throw new ne(`request body exceeds the ${e}-byte limit`);let r=[],o=0;for await(let s of n){let i=s;if(o+=i.length,o>e)throw new ne(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function An(n,e,t,r){let o=await n.createResponse({model:e,input:[{role:"user",content:t}],stream:!1},r),s=null;for await(let c of o){if(c.type==="error"){let w=c.error??{};throw new A(502,`image upstream error: ${String(w.message??"upstream reported an error")}`)}if(c.type==="response.completed"){s=c.response??null;break}}if(s===null)throw new A(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 A(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(In(u)===null)throw new A(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 En(n){return n instanceof A?n:n instanceof G?new A(404,n.message):new A(502,`image upstream failed: ${String(n instanceof Error?n.message:n)}`)}async function On(n,e,t,r){if(await n(e,t))return!0;let o=t==="edit"?"no image-input (expects_image) edit destination":"no pure text-to-image (expects_image=false) generation destination";return Be(r,new A(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 ${t==="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 Tn(n,e){n&&(n.outputUnits=e.images.length)}function Pn(n,e){n.statusCode=200,n.setHeader("content-type","application/json"),n.end(JSON.stringify({created:Math.floor(Date.now()/1e3),data:e.images.map(t=>({b64_json:t}))}))}var eo={model:!0,prompt:!0,response_format:!0,n:!0};async function Cn(n,e,t){let r=t.maxBodyBytes??Sn;try{if(n.method!=="POST")throw new A(405,`${String(n.method)} is not supported on ${xn}`);let o;try{o=JSON.parse((await Zr(n,r)).toString("utf8")||"{}")}catch(c){throw c instanceof ne?new A(413,c.message):new A(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new A(400,"request body must be a JSON object");let s=o;for(let c of Object.keys(s))if(!eo[c])throw new A(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 A(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 A(400,"model must be a string","model");await Rn(t.turn,t.clients,a);let d=s.n??1;if(d!==1)throw new A(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 A(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(t.stats?.request("images_generations",i.length,i.length),!await On(t.imageModels,a,"generation",e))return;let l=[{type:"input_text",text:i}];try{let c=await An(t.clients,a,l,t.turn?Z(t.turn):void 0);Tn(t.turn,c),Pn(e,c)}catch(c){throw En(c)}}catch(o){if(o instanceof ne){Be(e,new A(413,o.message));return}if(o instanceof A){Be(e,o);return}throw o}}var to={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function Un(n,e,t){let r=t.maxBodyBytes??Sn,o=t.maxImagePartBytes??Qr;try{if(n.method!=="POST")throw new A(405,`${String(n.method)} is not supported on ${nt}`);let s=String(n.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new A(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i=n[Symbol.asyncIterator](),a=0,d=new ReadableStream({async pull(m){try{let h=await i.next();if(h.done){m.close();return}if(a+=h.value.length,a>r){m.error(new ne(`request body exceeds the ${r}-byte limit`));return}m.enqueue(h.value)}catch(h){m.error(h)}},cancel(){i.return?.()}}),u;try{let m=new Headers;for(let[y,S]of Object.entries(n.headers))if(typeof S=="string")m.set(y,S);else if(Array.isArray(S))for(let x of S)m.append(y,x);let h={method:"POST",headers:m,body:d,duplex:"half"};u=await new Request(`https://proxy.internal${n.url??"/"}`,h).formData()}catch(m){throw m instanceof ne?new A(413,m.message):new A(400,`malformed multipart body: ${String(m instanceof Error?m.message:m)}`)}let l=u.get("prompt");if(typeof l!="string"||l.trim().length===0)throw new A(400,"prompt is required and must be a non-empty string field","prompt");let c=u.get("model");if(c!==null&&typeof c!="string")throw new A(400,"model must be a string field","model");let w=c===null?void 0:c;await Rn(t.turn,t.clients,w);let f=u.get("response_format")??"b64_json";if(f!=="b64_json")throw new A(400,`response_format ${JSON.stringify(f)} 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 m of u.keys())if(!to[m])throw new A(400,`form field "${m}" 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[]).`,m);let E=[];for(let m of["image","image[]"])for(let h of u.getAll(m)){if(!(h instanceof File))throw new A(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",m);if(h.size>o)throw new A(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,m);let g=new Uint8Array(await h.arrayBuffer()),y=In(g);if(y===null)throw new A(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",m);E.push({bytes:g,mime:y})}if(E.length===0)throw new A(400,"the edit endpoint requires at least one `image` file part","image");if(t.stats?.request("images_edits",l.length,l.length),!await On(t.imageModels,w,"edit",e))return;let R=[{type:"input_text",text:l}];for(let{bytes:m,mime:h}of E)R.push({type:"input_image",image_url:`data:${h};base64,${Buffer.from(m).toString("base64")}`});try{let m=await An(t.clients,w,R,t.turn?Z(t.turn):void 0);Tn(t.turn,m),Pn(e,m)}catch(m){throw En(m)}}catch(s){if(s instanceof ne){Be(e,new A(413,s.message));return}if(s instanceof A){Be(e,s);return}throw s}}T();var qn="/v1/usage/requests",Ln=1e4,no=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,ae=class extends Error{constructor(t,r=400){super(t);this.status=r;this.name="UsageRequestError"}status},q=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}},rt=class extends q{constructor(e){super(e),this.name="UsageWithholdingError"}},Bn=1024*1024;function Et(n,e){if(n===null||typeof n!="object"||Array.isArray(n))throw new ae(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let t=n[e];if(!Array.isArray(t))throw new ae(`"${e}" must be an array of uuid strings`);if(t.length>Ln)throw new ae(`"${e}" carries ${t.length} ids; at most ${Ln} 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 t){if(typeof s!="string"||!no.test(s))throw new ae(`"${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 Fn(n){return Et(n,"inference_ids")}var ro=["inference_id","model_id","identity_kind","variant","task","http_status","started_at","completed_at","prompt_tokens","completion_tokens","output_units","cost_nano_usd","price_version","priced_at"];function ke(n,e,t){let r=n[e];if(typeof r!="string"||r==="")throw new q(`${t}: ${e} is not a non-empty string (got ${Re(r)})`);return r}function It(n,e,t){let r=n[e];if(r===null)return null;if(typeof r!="string"||r==="")throw new q(`${t}: ${e} is neither null nor a non-empty string (got ${Re(r)})`);return r}function Fe(n,e,t,r){let o=n[e];if(o===null)return null;let s=typeof o=="number"?o:typeof o=="string"&&/^-?\d+(\.\d+)?$/.test(o.trim())?Number(o.trim()):NaN;if(!Number.isFinite(s))throw new q(`${t}: ${e} is not a number (got ${Re(o)})`);if(r&&!Number.isSafeInteger(s))throw new q(`${t}: ${e} is not a safely representable integer \u2014 a billing quantity that silently loses digits must fail rather than be reported`);if(s<0)throw new q(`${t}: ${e} is negative`);return Object.is(s,-0)?0:s}var oo=40,$n=64,Nn=600;function Dn(n){let e=Object.keys(n).sort(),r=e.slice(0,oo).map(o=>o.length>$n?`${o.slice(0,$n)}\u2026`:o).join(", ");return r.length>Nn&&(r=`${r.slice(0,Nn)}\u2026`),`${e.length} keys present: [${r}]`}function Re(n){return n===null?"null":n===void 0?"undefined":Array.isArray(n)?"an array":typeof n=="object"?"an object":`a ${typeof n}`}function At(n){be(process.stdout,`${JSON.stringify({event:"usage_row_rejected",ts:new Date().toISOString(),where:n.where,reason:n.reason})}
|
|
23
|
-
`)}function so(n,e){let t;try{t=n(e)}catch{Mn(n,e);return}io(t)&&Promise.resolve(t).catch(()=>{Mn(n,e)})}function io(n){return typeof n?.then=="function"}function Mn(n,e){if(n!==At)try{At(e)}catch{}}function gi(n,e=At){let t=n?.requests;if(!Array.isArray(t)){let o=n!==null&&typeof n=="object"&&!Array.isArray(n)?Dn(n):Re(n);throw new q(`the usage surface answered without a "requests" array: got ${Re(t)} (${o})`)}let r=[];for(let o=0;o<t.length;o++){let s=`usage.requests[${o}]`;try{r.push(ao(t[o],s))}catch(i){if(i instanceof rt||!(i instanceof q))throw i;so(e,{where:s,reason:i.message})}}if(t.length>0&&r.length===0)throw new q(`the usage surface returned ${t.length} row(s) and NONE of them could be validated \u2014 answering an empty result here would be indistinguishable from "we hold nothing for these ids", which a billing caller treats as final and writes the whole batch off unbilled`);return r}function ao(n,e){if(n===null||typeof n!="object"||Array.isArray(n))throw new q(`${e} is not an object: got ${Re(n)}`);let t=n;if(Object.hasOwn(t,"gpu_seconds"))throw new rt(`${e} carries gpu_seconds \u2014 that column was DROPPED from the ledger (ENG-417): the cost of compute is metered once, on the instance, and a per-request figure counts the same GPU-second twice. Its reappearance means a second, competing measure has been reintroduced upstream`);for(let o of ro)if(!Object.hasOwn(t,o))throw new q(`${e} has no "${o}" column (${Dn(t)})`);let r=Fe(t,"http_status",e,!0);if(r===null)throw new q(`${e}: http_status is null, but the ledger column is NOT NULL`);return{inference_id:ke(t,"inference_id",e),model_id:ke(t,"model_id",e),identity_kind:ke(t,"identity_kind",e),variant:It(t,"variant",e),task:ke(t,"task",e),http_status:r,started_at:ke(t,"started_at",e),completed_at:ke(t,"completed_at",e),prompt_tokens:Fe(t,"prompt_tokens",e,!0),completion_tokens:Fe(t,"completion_tokens",e,!0),output_units:Fe(t,"output_units",e,!1),cost_nano_usd:Fe(t,"cost_nano_usd",e,!0),price_version:It(t,"price_version",e),priced_at:It(t,"priced_at",e)}}function ot(n,e){let t=new Set(e),r=new Set;for(let o=0;o<n.length;o++){let s=n[o];if(!t.has(s.inference_id))throw new q(`the usage surface returned an id at validated answer position ${o} that was not in the request (${e.length} asked, ${n.length} answered) \u2014 an answer carrying ids nobody asked about cannot be attributed to this caller`);if(r.has(s.inference_id))throw new q(`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)}}import{SessionAllocationError as To,SessionFailedError as Po}from"@urun-sh/core";T();var Ot="/partners/huggingface/v1/billing/requests";function uo(n){return Et(n,"requestIds")}function lo(n){return n>=200&&n<=399}function co(n){let e=[];for(let t of n)t.cost_nano_usd!==null&&lo(t.http_status)&&e.push({requestId:t.inference_id,costNanoUsd:t.cost_nano_usd});return{requests:e.length>0?e:null}}async function jn(n,e){let t=uo(n),r=await e(t);return ot(r,t),co(r)}T();var po=new Map([["/v1/chat/completions","conversational"],["/v1/responses","conversational"],["/v1/messages","conversational"],["/v1/images/generations","text-to-image"],["/v1/images/edits","image-to-image"]]);function mo(n){return po.get(n)??null}var fo=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function go(n){let e=n?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function ho(n){return n==="surface"}function _o(n){let e=n?.ledgerFailureReason;return fo.includes(e)?e:"unknown"}function yo(n){return n.status===null?!1:n.status>=200&&n.status<400&&!n.stream_error}function vo(n){let e=Date.parse(n.ts),t=Number.isFinite(n.duration_ms)?Math.max(0,n.duration_ms):0;return new Date(e-Math.round(t)).toISOString()}var wo=256,Gn=4096,De=12e4,xo=250,bo=8e3,re=[],Tt=new Set,je=0,Se=!1;function bi(){return re.length}function ki(n=Date.now()){let e=re[0];return e===void 0?0:Math.max(0,n-e.queuedAt)}var st=new Set;function Hn(){return je===0&&re.length===0}function Wn(){if(Hn()){Se=!1;for(let n of[...st])n()}}async function Ri(n=Jn){return Hn()?!0:await new Promise(e=>{let t=null,r=()=>{st.delete(r),t&&clearTimeout(t),e(!0)};st.add(r),t=setTimeout(()=>{st.delete(r),e(!1)},n),t.unref?.()})}function Si(){Se=!0;let n=[...re.splice(0,re.length),...Tt];for(let e of n)e.settled||he(e,{outcome:"failed",reason:"dropped_on_exit"},{event:"inference_ledger_write_failed",reason:"dropped_on_exit",attempts:e.attempts,error:`the proxy is shutting down and this row was still unplaced after the ${Jn}ms drain \u2014 the request was served and answered, and its billing row was never written`});return Wn(),n.length}var Jn=5e3;function Vn(n,e){return`${JSON.stringify({event:n,ts:new Date().toISOString(),...e})}
|
|
24
|
-
`}function ko(n){be(process.stdout,n)}function zn(n,e){try{n(e)}catch{}}function Kn(n){let{record:e,ledger:t}=n;if(t===null)return;let r=t.report??ko,o=mo(e.path);if(o===null)return;let s=l=>{try{t.observe(l)}catch{}},i=(l,c)=>{s({outcome:"skipped",reason:l}),zn(r,Vn("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:vo(e),completed_at:e.ts,billable_outcome:yo(e),session_id:e.session_id,prompt_tokens:e.prompt_tokens,completion_tokens:e.completion_tokens,output_units:e.output_units,queue_ms:e.queue_ms,prefill_ms:e.prefill_ms,decode_ms:e.decode_ms,total_ms:e.total_ms};if(re.length>=Gn){i("queue_full",`${re.length} ledger writes are already waiting (capacity ${Gn}, ${je} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}re.push({entry:u,req:n.req,record:e,ledger:t,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),je<wo&&(je+=1,Ro())}async function Ro(){try{for(;;){let n=So();if(n===void 0)return;Tt.add(n);try{await Io(n)}finally{Tt.delete(n)}}}finally{je-=1,Wn()}}function So(){let n=Date.now();for(;;){let e=re.shift();if(e===void 0)return;let t=n-e.queuedAt;if(t<De)return e;he(e,{outcome:"failed",reason:"dwell_expired"},{event:"inference_ledger_write_failed",reason:"dwell_expired",error:`this row waited ${t}ms for the control plane (bound ${De}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 Io(n){let{entry:e,record:t,ledger:r}=n;for(;;){n.attempts+=1;try{let o=await r.write(n.req,e);he(n,{outcome:"written",reason:o.cost_nano_usd===null?"unpriced":"priced"},{event:"inference_ledger_written",inference_id:o.inference_id,path:t.path,model_id:e.model_id,identity_kind:e.identity_kind,variant:e.variant,org:t.org,session_id:e.session_id,cost_nano_usd:o.cost_nano_usd,price_version:o.price_version,unpriced_reason:o.unpriced_reason,attempts:n.attempts});return}catch(o){let s=go(o);if(s!==null){he(n,{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:n.attempts});return}let i=_o(o),a=Date.now()-n.queuedAt;if(Se){he(n,{outcome:"failed",reason:"dropped_on_exit"},{event:"inference_ledger_write_failed",reason:"dropped_on_exit",attempts:n.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(ho(i)&&a<De){if(await Eo(Ao(n.attempts)),Se||Date.now()-n.queuedAt>=De){let d=Date.now()-n.queuedAt;he(n,{outcome:"failed",reason:Se?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:Se?"dropped_on_exit":"dwell_expired",attempts:n.attempts,waited_ms:d,error:`this row's backoff outlived the ${De}ms dwell bound (waited ${d}ms over ${n.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}he(n,{outcome:"failed",reason:i},{event:"inference_ledger_write_failed",reason:i,attempts:n.attempts,waited_ms:a,error:o instanceof Error?o.message:String(o)});return}}}function Ao(n){return Math.min(bo,xo*2**Math.max(0,n-1))}function Eo(n){return new Promise(e=>{setTimeout(e,n).unref?.()})}function he(n,e,t){if(n.settled)return;n.settled=!0;try{n.ledger.observe(e)}catch{}let{event:r,...o}=t;zn(n.report,Vn(r,{inference_id:n.entry.inference_id,path:n.record.path,org:n.record.org,...o}))}function Ut(n){n.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function W(n,e,t){n.writeHead(e,{"content-type":"application/json"}),n.end(JSON.stringify(t))}function oe(n,e,t,r="invalid_request_error",o){W(n,e,{error:o?{message:t,type:r,code:o}:{message:t,type:r}})}function Zn(n,e){let t=e.error,r=t?.type??"upstream_error",o=t?.message??JSON.stringify(e);W(n,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:t?.code??null}})}function X(n,e,t,r){W(n,e,{type:"error",error:{type:t,message:r}})}function it(n,e,t){if(e==="anthropic"){W(n,404,{type:"error",error:{type:"not_found_error",message:t.message}});return}W(n,404,{error:{message:t.message,type:"invalid_request_error",code:"model_not_found"}})}var Co=64*1024*1024,Ge=class extends Error{};async function Yn(n,e=Co){let t=[],r=0;for await(let s of n){if(r+=s.length,r>e)throw new Ge(`request body exceeds ${e} bytes`);t.push(s)}let o=Buffer.concat(t).toString("utf8");return o?JSON.parse(o):{}}function Ie(n,e,t,r){return{id:n,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:e,choices:[{index:0,delta:t,finish_reason:r}]}}function er(n){let e=n?.usage;if(!e||typeof e!="object")return{kind:"absent"};let t=e,r=d=>typeof d=="number"&&Number.isFinite(d)&&d>=0?d:null,o=r(t.input_tokens),s=r(t.output_tokens),i=d=>d??0,a=r(t.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 at(n){let e=er(n);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function Xn(n){let e=at(n);return e?{prompt_tokens:e.input_tokens,completion_tokens:e.output_tokens,total_tokens:e.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function Uo(n,e){let t=er(e);t.kind==="absent"||t.kind==="partial"||(n.promptTokens=t.usage.input_tokens,n.completionTokens=t.usage.output_tokens)}function Lo(n){let e=n?.urun_timing;if(!e||typeof e!="object")return null;let t=e,r=d=>{let u=t[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 $o(n,e){let t=Lo(e);t&&(n.timing=t)}function Ae(n,e){Uo(n,e),$o(n,e)}async function Lt(n,e,t){if(typeof e.catalogIdentity!="function"){n.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(t);if(r!==null){n.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(t):null;n.catalog={kind:"not-a-catalog-model",model:o??t??null}}catch(r){n.catalog={kind:"unresolved",reason:`model ${JSON.stringify(t??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}function tr(n){let e=n?.output;if(!Array.isArray(e))return"";let t="";for(let r of e)if(!(r.type!=="reasoning"||!Array.isArray(r.content)))for(let o of r.content)o.type==="reasoning_text"&&(t+=String(o.text??""));return t}function No(n){if(Array.isArray(n))return n.map(e=>{let t=e.function;return!t||typeof t!="object"?e:{type:"function",name:t.name,description:t.description,parameters:t.parameters,strict:t.strict}})}var ut=class extends Error{};function Mo(n,e){return Array.isArray(e)?e.map(t=>{let r=t;if(r.type==="text")return{type:n==="assistant"?"output_text":"input_text",text:r.text};if(r.type==="image_url"){let o=r.image_url??{};return{type:"input_image",image_url:o.url,detail:o.detail}}throw new ut(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function qo(n){let e=[];for(let t of n){if(t.role==="tool"){e.push({type:"function_call_output",call_id:t.tool_call_id,output:typeof t.content=="string"?t.content:JSON.stringify(t.content??"")});continue}if(t.role==="assistant"&&Array.isArray(t.tool_calls)){typeof t.content=="string"&&t.content&&e.push({role:"assistant",content:t.content});for(let r of t.tool_calls){let o=r.function??{};e.push({type:"function_call",call_id:r.id,name:o.name,arguments:o.arguments})}continue}e.push({...t,content:Mo(t.role,t.content)})}return e}function Pt(n){return ve(n).map((e,t)=>({index:t,id:e.call_id??e.id??`call_${t}`,type:"function",function:{name:e.name??"",arguments:e.arguments??"{}"}}))}async function Bo(n,e,t,r,o,s){let i=$e(s.id),a=n.stream===!0;if(s.model=ee(n),s.stream=a,await Lt(s,e,typeof n.model=="string"?n.model:void 0),a){let h;try{h=await Me({body:n,clients:e,store:t,stats:o,responseId:i,turn:s})}catch(g){if(g instanceof G){it(r,"openai",g);return}if(g instanceof K){oe(r,g.status,g.message);return}throw g}Ut(r);for await(let g of h.events)g.type==="error"&&(s.streamError=!0),g.type==="response.completed"&&Ae(s,g.response),r.write(`event: ${String(g.type??"message")}
|
|
25
|
-
data: ${JSON.stringify(g)}
|
|
26
|
-
|
|
27
|
-
`);r.write(`data: [DONE]
|
|
28
|
-
|
|
29
|
-
`),r.end();return}let d;try{d=xe(t,n)}catch(h){if(h instanceof K){oe(r,h.status,h.message);return}throw h}n.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(d))-Buffer.byteLength(JSON.stringify(n.input)));let u=n.store!==!1,l=ee(n);o.model(l);let c=Array.isArray(n.tools)&&n.tools.length>0,w;try{w=await wt(e,n,d,!1,Z(s))}catch(h){if(h instanceof G){it(r,"openai",h);return}if(h instanceof K){oe(r,h.status,h.message);return}throw h}let f=[],E=null;for await(let h of o.track(w,l,s)){if(f.push(h),h.type==="error"){Zn(r,h);return}h.type==="response.completed"&&(E=h.response)}if(E==null){oe(r,502,"upstream produced no response.completed event","upstream_error");return}Ae(s,E);let R=yt(f,E),m=ge(c,ve(E).length,R);if(m){oe(r,502,fe(l,m),"upstream_error");return}u&&t.remember(i,d,vt(E,R),u),W(r,200,{...E,id:i})}async function Fo(n,e,t,r,o){let s=n.messages;if(!Array.isArray(s)){oe(t,400,"chat/completions requires a messages array");return}let i=String(n.model??"urun"),a=sn(o.id),d;try{d=qo(s)}catch(p){if(p instanceof ut){oe(t,400,p.message);return}throw p}let u=ee(n);r.model(u),o.model=u,o.stream=n.stream===!0,await Lt(o,e,typeof n.model=="string"?n.model:void 0);let l=Array.isArray(n.tools)&&n.tools.length>0,c;try{c=await e.createResponse({model:n.model,input:d,stream:!0,tools:No(n.tools),tool_choice:n.tool_choice,temperature:n.temperature,top_p:n.top_p,stop:n.stop,max_output_tokens:n.max_completion_tokens??n.max_tokens,...n.reasoning_effort!==void 0?{reasoning_effort:n.reasoning_effort}:{},...n.chat_template_kwargs!==void 0?{chat_template_kwargs:n.chat_template_kwargs}:{},...n.response_format!==void 0?{response_format:n.response_format}:{}},Z(o))}catch(p){if(p instanceof G){it(t,"openai",p);return}throw p}if(n.stream===!0){Ut(t);let p=[],O=new ie,P="";t.write(`data: ${JSON.stringify(Ie(a,i,{role:"assistant"},null))}
|
|
30
|
-
|
|
31
|
-
`);for await(let B of r.track(c,u,o)){p.push(B);let k=we(B);if(B.type==="response.output_text.delta"&&typeof B.delta=="string")P+=B.delta,t.write(`data: ${JSON.stringify(Ie(a,i,{content:B.delta},null))}
|
|
32
|
-
|
|
33
|
-
`);else if(B.type==="response.reasoning_text.delta"&&typeof B.delta=="string")t.write(`data: ${JSON.stringify(Ie(a,i,{reasoning_content:B.delta},null))}
|
|
34
|
-
|
|
35
|
-
`);else if(k){let{call:U,isNew:M}=O.add(k),z=M?{index:k.tool_index,id:U.id??`call_${k.tool_index}`,type:"function",function:{name:U.name??"",arguments:k.delta}}:{index:k.tool_index,function:{arguments:k.delta}};t.write(`data: ${JSON.stringify(Ie(a,i,{tool_calls:[z]},null))}
|
|
36
|
-
|
|
37
|
-
`)}else if(B.type==="error"){o.streamError=!0,t.write(`data: ${JSON.stringify({error:B})}
|
|
38
|
-
|
|
39
|
-
`),t.end();return}}let L=Pt(te(p)),I=ge(l,O.size+L.length,P);if(I){o.streamError=!0,t.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:fe(u,I)}})}
|
|
40
|
-
|
|
41
|
-
`),t.end();return}O.size===0&&L.length>0&&t.write(`data: ${JSON.stringify(Ie(a,i,{tool_calls:L},null))}
|
|
42
|
-
|
|
43
|
-
`);let Y=O.size>0||L.length>0;t.write(`data: ${JSON.stringify(Ie(a,i,{},Y?"tool_calls":"stop"))}
|
|
44
|
-
|
|
45
|
-
`);let V=at(te(p));Ae(o,te(p)),V&&t.write(`data: ${JSON.stringify({id:a,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:i,choices:[],usage:Xn(te(p))})}
|
|
46
|
-
|
|
47
|
-
`),t.write(`data: [DONE]
|
|
48
|
-
|
|
49
|
-
`),t.end();return}let w="",f="",E=[],R=new ie;for await(let p of r.track(c,u,o)){E.push(p),p.type==="response.output_text.delta"&&typeof p.delta=="string"&&(w+=p.delta),p.type==="response.reasoning_text.delta"&&typeof p.delta=="string"&&(f+=p.delta);let O=we(p);if(O&&R.add(O),p.type==="error"){Zn(t,p);return}}let m=te(E);Ae(o,m);let h=Pt(m),g=h.length>0?h:Pt({output:R.items()}),y=ge(l,g.length,w);if(y){oe(t,502,fe(u,y),"upstream_error");return}let S=g.map(({index:p,...O})=>O),x={role:"assistant",content:w||null},_=f||tr(m);_&&(x.reasoning_content=_),S.length>0&&(x.tool_calls=S),W(t,200,{id:a,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:i,choices:[{index:0,message:x,finish_reason:S.length>0?"tool_calls":"stop"}],usage:Xn(m)})}function Do(n){return typeof n=="string"?n:Array.isArray(n)?n.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var He=class extends Error{};function dt(n){return new He(`unsupported anthropic content block type "${n}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function jo(n){if(Array.isArray(n))return n.map(e=>{let t=e;return{type:"function",name:t.name,description:t.description,parameters:t.input_schema}})}function Go(n){if(n==null)return;let e=n.type;if(e==="auto")return"auto";if(e==="none")return"none";if(e==="any")return"required";if(e==="tool")return{type:"function",name:n.name};throw new He(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function Ho(n){if(typeof n=="string")return n;if(n==null)return"";if(!Array.isArray(n))throw dt(typeof n);let e="";for(let t of n){let r=String(t.type??"");if(r!=="text")throw dt(`tool_result > ${r}`);e+=String(t.text??"")}return e}function Wo(n){let{role:e,content:t}=n;if(typeof t=="string")return[{role:e,content:t}];if(t==null)return[];if(!Array.isArray(t))throw dt(typeof t);let r=[],o="",s=()=>{o&&(r.push({role:e,content:o}),o="")};for(let i of t){let a=String(i.type??"");if(a==="text")o+=String(i.text??"");else if(a==="tool_use")s(),r.push({type:"function_call",call_id:i.id,name:i.name,arguments:JSON.stringify(i.input??{})});else if(a==="tool_result")s(),r.push({type:"function_call_output",call_id:i.tool_use_id,output:Ho(i.content)});else{if(a==="thinking"||a==="redacted_thinking")continue;throw dt(a)}}return s(),r}function Ct(n){return ve(n).map((e,t)=>{let r;try{r=JSON.parse(e.arguments||"{}")}catch{throw new Error(`upstream function_call "${e.name??""}" arguments are not valid JSON`)}return{type:"tool_use",id:e.call_id??e.id??`toolu_${t}`,name:e.name??"",input:r}})}async function Jo(n,e,t,r,o){let s=n.messages;if(!Array.isArray(s)){X(t,400,"invalid_request_error","messages requires a messages array");return}if(typeof n.max_tokens!="number"||!Number.isInteger(n.max_tokens)||n.max_tokens<1){X(t,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(n.top_k!==void 0){X(t,400,"invalid_request_error","top_k is not supported: the uRun serve lane exposes temperature/top_p/stop_sequences only");return}if(n.stop_sequences!==void 0&&(!Array.isArray(n.stop_sequences)||n.stop_sequences.some(p=>typeof p!="string"))){X(t,400,"invalid_request_error","stop_sequences must be an array of strings");return}let i=String(n.model??"urun"),a=an(o.id),d=[],u;try{n.system&&d.push({role:"system",content:Do(n.system)});for(let p of s)d.push(...Wo(p));u=Go(n.tool_choice)}catch(p){if(p instanceof He){X(t,400,"invalid_request_error",p.message);return}throw p}let l=ee(n);r.model(l),o.model=l,o.stream=n.stream===!0,await Lt(o,e,typeof n.model=="string"?n.model:void 0);let c=Array.isArray(n.tools)&&n.tools.length>0,w;try{w=await e.createResponse({model:n.model,input:d,stream:!0,tools:jo(n.tools),tool_choice:u,temperature:n.temperature,max_output_tokens:n.max_tokens,top_p:n.top_p,stop:n.stop_sequences},Z(o))}catch(p){if(p instanceof G){it(t,"anthropic",p);return}throw p}let f=(p,O)=>{t.destroyed||t.write(`event: ${p}
|
|
50
|
-
data: ${JSON.stringify({type:p,...O})}
|
|
51
|
-
|
|
52
|
-
`)};if(n.stream===!0){let p=r.track(w,l,o);Ut(t),f("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:i,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let O=0,P="",L=0,I="none",Y=()=>I,V=-1,B=-1,k=new ie,U=[],M=()=>{I!=="none"&&(I="none",f("content_block_stop",{index:V}))},z=($,J)=>{M(),I=$,V=L++,f("content_block_start",{index:V,content_block:J})};for await(let $ of p){if(t.destroyed)break;U.push($);let J=we($);if($.type==="response.reasoning_text.delta"&&typeof $.delta=="string")Y()!=="thinking"&&z("thinking",{type:"thinking",thinking:""}),f("content_block_delta",{index:V,delta:{type:"thinking_delta",thinking:$.delta}});else if($.type==="response.output_text.delta"&&typeof $.delta=="string")Y()!=="text"&&z("text",{type:"text",text:""}),O+=1,P+=$.delta,f("content_block_delta",{index:V,delta:{type:"text_delta",text:$.delta}});else if(J){let{call:$t}=k.add(J);(Y()!=="tool"||J.tool_index!==B)&&(B=J.tool_index,z("tool",{type:"tool_use",id:$t.id??`call_${J.tool_index}`,name:$t.name??"",input:{}})),J.delta&&f("content_block_delta",{index:V,delta:{type:"input_json_delta",partial_json:J.delta}})}else if($.type==="error"){o.streamError=!0,f("error",{error:{type:"api_error",message:JSON.stringify($)}}),t.end();return}}if(t.destroyed)return;let ue=te(U),de=ve(ue),se=ge(c,k.size+de.length,P);if(se){o.streamError=!0,f("error",{error:{type:"api_error",message:fe(l,se)}}),t.end();return}M();let Q=[];if(k.size===0)try{Q=Ct(ue)}catch($){o.streamError=!0,f("error",{error:{type:"api_error",message:String($ instanceof Error?$.message:$)}}),t.end();return}L===0&&Q.length===0&&(z("text",{type:"text",text:""}),M()),Q.forEach($=>{let J=L++;f("content_block_start",{index:J,content_block:{...$,input:{}}}),f("content_block_delta",{index:J,delta:{type:"input_json_delta",partial_json:JSON.stringify($.input??{})}}),f("content_block_stop",{index:J})});let Ee=k.size>0||Q.length>0,_e=at(ue);Ae(o,ue),f("message_delta",{delta:{stop_reason:Ee?"tool_use":"end_turn"},usage:_e?{input_tokens:_e.input_tokens,output_tokens:_e.output_tokens}:{output_tokens:O}}),f("message_stop",{}),t.end();return}let E="",R="",m=[],h=new ie;for await(let p of r.track(w,l,o)){m.push(p),p.type==="response.output_text.delta"&&typeof p.delta=="string"&&(E+=p.delta),p.type==="response.reasoning_text.delta"&&typeof p.delta=="string"&&(R+=p.delta);let O=we(p);if(O&&h.add(O),p.type==="error"){let P=p.error,L=P?.type==="invalid_request_error";X(t,L?400:502,L?"invalid_request_error":"api_error",P?.message??JSON.stringify(p));return}if(t.destroyed)break}if(t.destroyed)return;let g=te(m);Ae(o,g);let y;try{y=Ct(g),y.length===0&&h.size>0&&(y=Ct({output:h.items()}))}catch(p){X(t,502,"api_error",String(p instanceof Error?p.message:p));return}let S=ge(c,y.length,E);if(S){X(t,502,"api_error",fe(l,S));return}let x=[],_=R||tr(g);_&&x.push({type:"thinking",thinking:_}),(E||y.length===0)&&x.push({type:"text",text:E}),x.push(...y),W(t,200,{id:a,type:"message",role:"assistant",content:x,model:i,stop_reason:y.length>0?"tool_use":"end_turn",usage:at(g)??{input_tokens:0,output_tokens:0}})}function Vo(n){be(process.stdout,n)}function zo(n){let{clients:e,apiKey:t,identity:r,statusOf:o}=n,s=typeof e=="function"?e:()=>e,i=n.requestLog??Vo,a=n.observe,d=new pe;return(u,l)=>{let c=new URL(u.url??"/","http://localhost"),w=c.pathname==="/v1/messages"||c.pathname.startsWith("/v1/messages/"),f=(g,y,S,x,_)=>w?X(l,g,x,y):oe(l,g,y,S,_),E=_t(c.pathname),R=c.pathname.startsWith("/partners/"),m=E||R?{id:Le(),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;m&&(l.setHeader(rn,m.id),l.once("close",()=>{let g=un({turn:m,method:u.method??"",path:c.pathname,res:l});i(dn(g)),a?.(g),Kn({record:g,ledger:n.ledger??null,req:u})}));let h=()=>on(c.pathname,m);(async()=>{if(u.method==="GET"&&c.pathname==="/healthz"){W(l,200,{ok:!0});return}if(u.method==="GET"&&c.pathname==="/stats"){W(l,200,r?{identity:r,...d.snapshot()}:d.snapshot());return}if(t&&(u.headers.authorization??"")!==`Bearer ${t}`){f(401,"invalid local proxy api key","authentication_error","authentication_error");return}let g=c.pathname===qn?"canonical":c.pathname===Ot?"huggingface":null;if(g){if(u.method!=="POST"){f(405,`${c.pathname} is a POST batch lookup`,"invalid_request_error","invalid_request_error");return}let _=n.usage;if(!_){f(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 p=_.authorize(u),O;try{O=await Yn(u,Bn)}catch(I){if(I instanceof Ge){f(413,I.message,"request_too_large","request_too_large");return}f(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let P=I=>_.lookup(p,I),L;try{if(g==="canonical"){let I=Fn(O),Y=await P(I);ot(Y,I),L={requests:Y}}else L=await jn(O,P)}catch(I){if(I instanceof ae){f(I.status,I.message,"invalid_request_error","invalid_request_error");return}throw I}W(l,200,L);return}if(c.pathname.startsWith("/partners/")){f(404,`no partner adapter for ${u.method} ${c.pathname} \u2014 the partner surface currently carries ${Ot}`,"invalid_request_error","not_found_error");return}let y=await s(u);if(m&&(m.org=y.tenant??null),u.method==="GET"&&c.pathname==="/v1/models"){if(c.searchParams.get("format")==="openrouter"){if(!y.openRouterModels){f(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}W(l,200,await y.openRouterModels());return}W(l,200,await y.listModels());return}if(u.method==="GET"&&c.pathname===kn){if(!y.listImageModels){f(501,"GET /v1/images/models needs a router-backed client (listImageModels)","not_implemented","not_implemented_error");return}W(l,200,await y.listImageModels());return}if(bn(c.pathname)){if(!y.supportsImages){f(501,`the image lanes need a router-backed client (supportsImages) to gate model capability for ${c.pathname}`,"not_implemented","not_implemented_error");return}let _={clients:y,imageModels:y.supportsImages,stats:d,turn:h()};c.pathname===nt?await Un(u,l,_):await Cn(u,l,_);return}if(u.method!=="POST"){f(404,`no route for ${u.method} ${c.pathname}`,"invalid_request_error","not_found_error");return}let S;try{S=await Yn(u)}catch(_){if(_ instanceof Ge){f(413,_.message,"request_too_large","request_too_large");return}f(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let x=Buffer.byteLength(JSON.stringify(S));if(c.pathname==="/v1/responses"){d.request("responses",x,x),await Bo(S,y,Ne(y),l,d,h());return}if(c.pathname==="/v1/chat/completions"){d.request("chat_completions",x,x),await Fo(S,y,l,d,h());return}if(c.pathname==="/v1/messages"){d.request("messages",x,x),await Jo(S,y,l,d,h());return}if(c.pathname==="/v1/messages/count_tokens"){d.request("count_tokens",x,x),X(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}f(404,`no route for POST ${c.pathname}`,"invalid_request_error","not_found_error")})().catch(g=>{let y=String(g instanceof Error?g.message:g);if(!l.headersSent){let S=o?.(g)??null;if(S){f(S.status,y,S.openaiType,S.anthropicType);return}if(g instanceof To){let x=Ze(g.code);if(x){let _=g.retryAfterSeconds??(x.status===429?30:void 0);_!==void 0&&l.setHeader("Retry-After",String(_)),f(x.status,y,x.openaiType,x.anthropicType,x.code);return}}if(g instanceof Po){let x=et(g);if(x){let _=x.status===429?30:void 0;_!==void 0&&l.setHeader("Retry-After",String(_)),f(x.status,y,x.openaiType,x.anthropicType,x.code);return}}if(g instanceof q){f(502,y,"upstream_error","api_error");return}f(500,y,"proxy_error","api_error","proxy_error");return}m&&(m.streamError=!0),l.destroyed||(w?l.write(`event: error
|
|
53
|
-
data: ${JSON.stringify({type:"error",error:{type:"api_error",message:y}})}
|
|
54
|
-
|
|
55
|
-
`):l.write(`data: ${JSON.stringify({error:{message:y,type:"upstream_error"}})}
|
|
56
|
-
|
|
57
|
-
`)),l.end()})}}function Hi(n){let{clients:e,apiKey:t}=n,r=Oo(zo(n)),o=fn(r,{clients:e,apiKey:t,statusOf:n.statusOf}),s=ht(r,{resolver:Qn(e,t)}),i=r.close.bind(r);return r.close=(a=>(o.close(),s(),i(a))),Zt(r,{clients:e,apiKey:t}),r}var We=(n,e)=>Object.assign(new Error(e),{status:n});function Ko(n){let t=new URL(n.url??"/","http://localhost").searchParams.get("model")?.trim();return t===""?void 0:t}async function Yo(n,e){if(!n.supportsAudio)throw We(501,"the realtime lane needs a router-backed client (supportsAudio) to gate audio modality \u2014 hand-built clients without the catalog oracle are refused, never allowed silently");let t;try{t=await n.supportsAudio(e)}catch(s){throw s instanceof G?We(404,s.message):s}if(!t)throw We(404,`model ${JSON.stringify(e??"(default)")} is not audio-capable (catalog task is neither stt nor tts) \u2014 this realtime lane serves only audio-task models; chat models ride /v1/chat/completions`);if(!n.openAudio)throw We(501,"an audio-capable model needs a router-backed client (openAudio) to open its native session audio bridge \u2014 refusing loudly rather than binding a voiceless session");let r={audio:await n.openAudio(e)},o={clients:n,voice:t,audioSession:r};return e!==void 0&&(o.model=e),o}function Qn(n,e){return{resolve:async t=>{if(e!==void 0){let r=t.headers.authorization,o=typeof r=="string"?r.trim():"",s=o.startsWith("Bearer ")?o.slice(7).trim():"";if(s===""||s!==e)throw We(401,"invalid local proxy api key")}return Yo(n,Ko(t))}}}export{Ve as a,Zt as b,ts as c,Cr as d,fn as e,Ue as f,gt as g,ht as h,Ko as i,Yo as j,Qn as k,Hs as l,Wr as m,Ws as n,Js as o,Vs as p,zs as q,Ks as r,Ys as s,Xs as t,Qs as u,Zs as v,Jr as w,hn as x,ei as y,Kr as z,ti as A,ni as B,ri as C,be as D,oi as E,si as F,ii as G,qn as H,q as I,gi as J,Ot as K,bi as L,ki as M,Ri as N,Si as O,zo as P,Hi as Q};
|
package/dist/chunk-VLRMJRLS.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import{a as D,b as B}from"./chunk-HR5H6S7L.js";import{H as oe,I as C,J as se,K as ie,L as ae,M as le,N as ce,O as ue,P as de,b as Y,d as Q,e as X,g as Z,h as ee,i as te,j as re,w as ne,x}from"./chunk-NY23USZF.js";import{e as q}from"./chunk-3WAJD62J.js";import{a as V}from"./chunk-7M6LY6DR.js";import{c as p}from"./chunk-5CCJUNH7.js";p();import{Counter as R,Gauge as he,Histogram as pe,Registry as ke,collectDefaultMetrics as Oe}from"@prometheus-io/client";import{createServer as Ie}from"http";var xt=9464,ge="/metrics",S="(other)",Ne="(none)",Te=64,Pe=128,$e=["/v1/responses","/v1/chat/completions","/v1/messages","/v1/messages/count_tokens","/v1/models","/v1/images/models","/v1/images/generations","/v1/images/edits",oe,ie],Ue=new Set($e);function Le(t){return Ue.has(t)?t:S}function He(t){return t===null?"none":t>=500?"5xx":t>=400?"4xx":t>=300?"3xx":t>=200?"2xx":"1xx"}var A=class{constructor(e){this.max=e}max;admitted=new Set;overflows=0;of(e,r=!0){return e===null||e===""?Ne:this.admitted.has(e)?e:r?this.admitted.size>=this.max?(this.overflows+=1,S):(this.admitted.add(e),e):S}get size(){return this.admitted.size}get overflowCount(){return this.overflows}},k=class{registry=new ke;modelLabel=new A(Te);orgLabel=new A(Pe);requests;duration;ttft;streamErrors;undelivered;ledgerWrites;ledgerQueue;labelCardinality;labelOverflows;constructor(e={}){this.requests=new R({name:"urun_inference_requests_total",help:"Completed /v1 requests, by lane, method, HTTP status class, streaming, model and org.",labelNames:["lane","method","status_class","stream","model","org"],registers:[this.registry]}),this.duration=new pe({name:"urun_inference_request_duration_seconds",help:"Wall time from request receipt to response close. For a stream this is the WHOLE generation, not the time to the head.",labelNames:["lane","stream","model","org"],buckets:[.1,.25,.5,1,2.5,5,10,30,60,120,300,600],registers:[this.registry]}),this.ttft=new pe({name:"urun_inference_ttft_seconds",help:"Time to first output-text delta on a streamed response. The metric Hugging Face gates provider listing on (<5s for conversational models).",labelNames:["lane","model","org"],buckets:[.1,.25,.5,1,2,3,5,8,15,30,60],registers:[this.registry]}),this.streamErrors=new R({name:"urun_inference_stream_errors_total",help:"Responses that emitted an error frame AFTER the head flushed. These are HTTP 200 with a complete body, so no status-based metric can see them.",labelNames:["lane","model"],registers:[this.registry]}),this.undelivered=new R({name:"urun_inference_undelivered_total",help:"Responses whose socket did not finish flushing \u2014 the client vanished mid-answer.",labelNames:["lane","model"],registers:[this.registry]}),this.ledgerWrites=new R({name:"urun_inference_ledger_writes_total",help:"Terminal fate of the per-request ledger row for each billable /v1 request. outcome=written is the row landing (reason=priced|unpriced); outcome=skipped and outcome=failed are BOTH revenue that went unrecorded. failed/duplicate is the ENG-410 fabricated-row signal.",labelNames:["outcome","reason"],registers:[this.registry]}),this.ledgerQueue=new he({name:"urun_inference_ledger_queue",help:'The ledger write queue right now. measure="depth" is rows waiting for a worker; measure="oldest_age_seconds" is how long the head has waited \u2014 approaching the dwell bound means rows are about to be given up on, which is revenue lost.',labelNames:["measure"],registers:[this.registry],collect:()=>{this.ledgerQueue.set({measure:"depth"},ae()),this.ledgerQueue.set({measure:"oldest_age_seconds"},le()/1e3)}}),this.labelCardinality=new he({name:"urun_inference_label_cardinality",help:"Distinct values admitted for a bounded metric label. At its ceiling, further values report as (other).",labelNames:["label"],registers:[this.registry],collect:()=>{this.labelCardinality.set({label:"model"},this.modelLabel.size),this.labelCardinality.set({label:"org"},this.orgLabel.size)}}),this.labelOverflows=new R({name:"urun_inference_label_overflows_total",help:"Observations that would have taken a new label value but found the budget spent. Non-zero means a metric dimension is now partly blind.",labelNames:["label"],registers:[this.registry]}),e.defaultMetrics!==!1&&Oe({register:this.registry,prefix:"urun_inference_proxy_"})}observeLedger(e){this.ledgerWrites.inc({outcome:e.outcome,reason:e.reason})}observe(e){let r=Le(e.path),n=e.status!==null&&!(e.status>=400&&e.status<500),o=this.modelLabel.overflowCount+this.orgLabel.overflowCount,a=this.modelLabel.of(e.model,n),s=this.orgLabel.of(e.org,n);this.modelLabel.overflowCount+this.orgLabel.overflowCount-o>0&&(this.modelLabel.overflowCount>o?this.labelOverflows.inc({label:"model"}):this.labelOverflows.inc({label:"org"}));let c=e.stream===!0?"true":"false";this.requests.inc({lane:r,method:e.method===""?S:e.method,status_class:He(e.status),stream:c,model:a,org:s}),this.duration.observe({lane:r,stream:c,model:a,org:s},e.duration_ms/1e3),e.ttft_ms!==null&&this.ttft.observe({lane:r,model:a,org:s},e.ttft_ms/1e3),e.stream_error&&this.streamErrors.inc({lane:r,model:a}),e.delivered||this.undelivered.inc({lane:r,model:a})}scrape(){return this.registry.metrics()}get contentType(){return this.registry.contentType}};function fe(t){return Ie((e,r)=>{(async()=>{let n=(e.url??"/").split("?")[0];if(e.method!=="GET"||n!==ge){r.writeHead(404,{"content-type":"text/plain"}),r.end(`no route for ${e.method} ${n} \u2014 this port serves only GET ${ge}
|
|
2
|
-
`);return}let o=await t.scrape();r.writeHead(200,{"content-type":t.contentType}),r.end(o)})().catch(n=>{if(!r.headersSent){r.writeHead(500,{"content-type":"text/plain"}),r.end(`${String(n instanceof Error?n.message:n)}
|
|
3
|
-
`);return}r.end()})})}p();import{createHash as ye}from"crypto";import{createServer as Me}from"http";var Ot=9465,Fe="x-urun-key-id",De="x-urun-key-fingerprint",Be="urun-ratelimit-bucket:",Ge="urun-ratelimit-tier:",me="anonymous",ze=/^urun_sk_[A-Za-z0-9_-]{43}$/;function Ke(t){return ye("sha256").update(`${Be}${t}`).digest("hex")}function je(t){return ye("sha256").update(`${Ge}${t}`).digest("hex")}function We(t){let e=/^Bearer[ \t]+(\S.*)$/i.exec((t??"").trim());if(!e)return null;let r=e[1].trim();return ze.test(r)?r:null}function we(){return Me((t,e)=>{let r=We(t.headers.authorization);e.setHeader(Fe,r===null?me:Ke(r)),e.setHeader(De,r===null?me:je(r)),e.writeHead(200),e.end()})}p();import{createClientToken as Je,ClientTokenError as qe}from"@urun-sh/core";var m=class extends Error{constructor(r,n=401){super(r);this.status=n;this.name="ProxyAuthError"}status},v=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function E(t){let e=(t??"").trim();if(e==="")throw new m("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let r=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!r)throw new m("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return r[1].trim()}var Ve=60,Ye=6e4,Qe=4096;function Xe(t){return t===401||t===403}var O=class{constructor(e){this.opts=e;this.mint=e.mint??Je,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new m("empty Authorization bearer token");let r=this.cache.get(e);if(r&&this.now()-r.at<Ye)return{apiKey:e,orgId:r.orgId};let n;try{n=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:Ve})).orgId}catch(o){throw o instanceof qe&&Xe(o.status)?(this.cache.delete(e),new m("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new v(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${o instanceof Error?o.message:String(o)}`)}if(!n)throw new v("the control plane verified the API key but returned no org_id \u2014 refusing to route a request with no tenancy (control-plane contract violation)");if(this.cache.size>=Qe){let o=this.cache.keys().next();o.done||this.cache.delete(o.value)}return this.cache.set(e,{orgId:n,at:this.now()}),{apiKey:e,orgId:n}}clear(){this.cache.clear()}};p();var Ze=8080,G="serve",Lt="0.0.0.0";function et(t,e,r){let n=(t[e]??"").trim();if(!n)throw new Error(`${e} is not set \u2014 ${r}`);return n}function Ht(t=process.env){let e=(t.PORT??"").trim(),r=e===""?Ze:Number(e);if(!Number.isInteger(r)||r<0||r>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let n=ne(x(et(t,"URUN_BASE_URL","the hosted proxy needs the uRun session-gateway base it opens sessions and verifies API keys against (e.g. https://api.urun.sh)"),"URUN_BASE_URL")),o=(t.URUN_API_URL??"").trim(),a=o?x(o,"URUN_API_URL"):`${n.replace(/\/+$/,"")}/v1`,s=(t.URUN_CATALOG_URL??"").trim(),i=(t.URUN_CATALOG_ANON_KEY??"").trim();if(!!s!=!!i)throw new Error("URUN_CATALOG_URL and URUN_CATALOG_ANON_KEY must be set together (or both left unset) \u2014 exactly one of them is set, which would silently disable the model-catalog oracle");let c=s?{catalogUrl:x(s,"URUN_CATALOG_URL"),anonKey:i}:null;return{port:r,baseUrl:n,apiUrl:a,catalog:c}}p();import{createHash as tt,randomUUID as rt}from"crypto";var nt=512;function P(t){return`urun-inference-proxy-${tt("sha256").update(t).digest("hex").slice(0,32)}`}var I=class{constructor(e){this.opts=e}opts;tenants=new Map;label(e,r){return Object.assign(Object.create(e),{tenant:r.orgId})}build(e){if(this.opts.build){let n=this.opts.build(e);return{router:n.router,clients:this.label(n.clients,e)}}let r=D({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:G,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:P(e.apiKey),release:"detach"});return{router:r,clients:this.label(B(r),e)}}clientsFor(e){let r=this.tenants.get(e.apiKey);if(r)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,r),r.clients;for(;this.tenants.size>=nt;){let o=this.tenants.entries().next();if(o.done)break;let[a,s]=o.value;this.tenants.delete(a),s.router.closeAll().catch(()=>{})}let n=this.build(e);return this.tenants.set(e.apiKey,n),n.clients}get size(){return this.tenants.size}async closeAll(){let e=[...this.tenants.values()].map(n=>n.router);this.tenants.clear();let r=[];if(await Promise.all(e.map(async n=>{try{await n.closeAll()}catch(o){r.push(String(o instanceof Error?o.message:o))}})),r.length>0)throw new Error(`failed to close ${r.length} tenant backhaul(s): ${r.join("; ")}`)}},be=512,ot=1024,N=class extends Error{constructor(){super(`conversation capacity reached (${be} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function st(t,e,r,n){let o=D({baseUrl:t.baseUrl,orgId:e.orgId,defaultApp:null,fnName:G,apiUrl:t.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:t.baseUrl},catalog:t.catalog,subject:r.subject,release:"detach",sessionStartOptions:a=>{let s=n.get(a);return s===void 0?void 0:{sessionId:s}},onSessionOpen:(a,s)=>{n.set(a,s)}});return{clients:B(o),release:async()=>{await o.closeAll()}}}var T=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,r){let n={id:r,subject:`${P(e.apiKey)}:live:${r}`},o=new Map,a=this.opts.build?this.opts.build(e,n):st(this.opts,e,n,o);return{id:r,backhaul:a,attachments:0,nativeRefs:o}}attachEntry(e,r){let n=this.byCaller.get(e.apiKey);n===void 0&&(n=new Map,this.byCaller.set(e.apiKey,n));let o=n.get(r);if(o!==void 0)return n.delete(r),n.set(r,o),o.attachments+=1,o;this.evictOverflow();let a=this.build(e,r);return a.attachments=1,n.set(r,a),a}get conversationCount(){let e=0;for(let r of this.byCaller.values())e+=r.size;return e}evictOverflow(){for(;this.conversationCount>=be;){let e=this.byCaller.keys().next();if(e.done)throw new N;let r=e.value,n=this.byCaller.get(r),o=!1;for(let[a,s]of n)if(!(s.attachments>0)){n.delete(a),this.dropHandleIndexFor(r,a),s.backhaul.release().catch(()=>{}),o=!0;break}if(!o)throw n.size===0&&this.byCaller.delete(r),new N;n.size===0&&this.byCaller.delete(r)}}dropHandleIndexFor(e,r){for(let[n,o]of this.byHandle)o.apiKey===e&&o.conversationId===r&&this.byHandle.delete(n)}connectionFor(e){let r=null,n=!1,o=()=>(r===null&&(r=this.attachEntry(e,rt())),r),a={createResponse:(s,i)=>o().backhaul.clients.createResponse(s,i),catalogIdentity:s=>o().backhaul.clients.catalogIdentity(s),servingAppSlug:s=>{let i=o().backhaul.clients.servingAppSlug;if(i===void 0)throw new Error("the conversation backhaul wires no servingAppSlug seam \u2014 the caller-org app a request resolves to cannot be known");return i(s)},listModels:()=>o().backhaul.clients.listModels(),sessionHandle:s=>o().backhaul.clients.sessionHandle(s),createResponseOn:(s,i)=>o().backhaul.clients.createResponseOn(s,i),onSessionEnd:(s,i)=>o().backhaul.clients.onSessionEnd(s,i)};return a.openAudio=async s=>{let i=o().backhaul.clients.openAudio;if(i===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return i(s)},a.openVideo=async s=>{let i=o().backhaul.clients.openVideo;if(i===void 0)throw new Error("the conversation backhaul wires no openVideo seam \u2014 video-in is unavailable on this deployment");return i(s)},a.openVideoOut=async s=>{let i=o().backhaul.clients.openVideoOut;if(i===void 0)throw new Error("the conversation backhaul wires no openVideoOut seam \u2014 video-out is unavailable on this deployment");return i(s)},a.supportsAudio=async s=>{let i=o().backhaul.clients.supportsAudio;if(i===void 0)throw new Error("the conversation backhaul wires no supportsAudio seam \u2014 the realtime lane cannot gate audio modality on this deployment");return i(s)},{clients:a,bindResumeHandle:s=>{let i=r;if(i===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=ot;){let c=this.byHandle.keys().next();if(c.done)break;this.byHandle.delete(c.value)}this.byHandle.set(s,{apiKey:e.apiKey,conversationId:i.id})},clientsForResume:async s=>{let i=this.byHandle.get(s),c=i===void 0?void 0:this.byCaller.get(i.apiKey),u=i===void 0||c===void 0?void 0:c.get(i.conversationId);if(i===void 0||u===void 0||i.apiKey!==e.apiKey)throw new q("the conversation behind this resume handle is gone (evicted at capacity, or the replica restarted) or the handle is not yours \u2014 reconnect without a handle to start a new conversation");return r=u,u.attachments+=1,u.backhaul.clients},release:async()=>{if(n)return;n=!0;let s=r;r=null,s!==null&&(s.attachments-=1,!(s.attachments>0)&&await s.backhaul.release().catch(()=>{}))}}}async detachAll(){let e=[];for(let n of this.byCaller.values())for(let o of n.values())e.push(o.backhaul.release());this.byCaller.clear(),this.byHandle.clear();let r=[];if(await Promise.all(e.map(async n=>{try{await n}catch(o){r.push(String(o instanceof Error?o.message:o))}})),r.length>0)throw new Error(`failed to detach ${r.length} conversation backhaul(s): ${r.join("; ")}`)}};p();var it="/inference-ledger",at=1e4,$=class extends Error{ledgerFailureReason;constructor(e,r="duplicate"){super(e),this.name="LedgerDuplicateError",this.ledgerFailureReason=r}},K=class extends Error{constructor(r,n){super(n);this.ledgerAlreadyWritten=r;this.name="LedgerAlreadyWrittenError"}ledgerAlreadyWritten},g=class extends Error{ledgerFailureReason="surface";constructor(e){super(e),this.name="LedgerSurfaceError"}},j=class extends m{ledgerFailureReason="auth";constructor(e){super(e),this.name="LedgerAuthError"}},lt=[["inference_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["model_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["variant",t=>t===null||typeof t=="string"],["task",t=>typeof t=="string"&&/[^\s]/.test(t)],["http_status",t=>typeof t=="number"&&Number.isInteger(t)],["started_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["completed_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["prompt_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["completion_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["output_units",ve],["cost_nano_usd",ve],["price_version",t=>t===null||typeof t=="string"]];function ct(t){for(let[e,r]of lt){if(!(e in t))return`${e} (absent)`;if(!r(t[e]))return`${e} (${JSON.stringify(t[e])})`}return null}function ut(t){let e;try{e=JSON.parse(t)}catch{return{status:"unavailable"}}if(e===null||typeof e!="object"||Array.isArray(e))return{status:"unavailable"};let r=e.existing_status;if(r==="not_in_this_org")return{status:"not_in_this_org"};if(r!=="found")return{status:"unavailable"};let n=e.existing;return n===null||typeof n!="object"||Array.isArray(n)?{status:"unavailable"}:ct(n)!==null?{status:"unavailable"}:{status:"found",row:n}}function ve(t){return t===null?!0:typeof t=="number"?Number.isFinite(t):typeof t!="string"?!1:/[^\s]/.test(t)&&Number.isFinite(Number(t))}function z(t,e){return t===null?e===null:e===null?!1:Number(e)===t}function _e(t,e){if(typeof e!="string")return!1;let r=Date.parse(t),n=Date.parse(e);return Number.isFinite(r)&&Number.isFinite(n)&&r===n}function dt(t,e){let r=e;return r.inference_id===t.inference_id&&r.model_id===t.model_id&&(r.variant??null)===t.variant&&r.task===t.task&&r.http_status===t.http_status&&_e(t.started_at,r.started_at)&&_e(t.completed_at,r.completed_at)&&z(t.prompt_tokens,r.prompt_tokens)&&z(t.completion_tokens,r.completion_tokens)&&z(t.output_units,r.output_units)}function ht(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new g(`${e} answered a "recorded" that is not an object: ${JSON.stringify(t)}`);let r=t,n=r.inference_id;if(typeof n!="string"||n==="")throw new g(`${e} answered a row with no inference_id: ${JSON.stringify(t)}`);if(!("cost_nano_usd"in r))throw new g(`${e} answered a row with no cost_nano_usd field: ${JSON.stringify(t)}`);let o=r.cost_nano_usd;if(o!=null&&!(typeof o=="number"&&Number.isInteger(o)&&o>=0))throw new g(`${e} answered cost_nano_usd ${JSON.stringify(o)}, which is neither a non-negative integer nor null`);let a=u=>{if(!(u in r))throw new g(`${e} answered a row with no ${u} field: ${JSON.stringify(t)}`);let f=r[u];if(f===null)return null;if(typeof f!="string")throw new g(`${e} answered a non-string ${u}: ${JSON.stringify(f)}`);if(!/[^\s]/.test(f))throw new g(`${e} answered a BLANK ${u} (${JSON.stringify(f)}) \u2014 it satisfies "is a string" while naming nothing, which is exactly what inference_requests_priced_attribution_check exists to forbid`);return f},s=a("price_version"),i=a("unpriced_reason"),c=o!=null;if(c&&(s===null||i!==null))throw new g(`${e} answered a PRICED row (cost_nano_usd ${String(o)}) that is not cleanly attributed \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(i)}`);if(!c&&(s!==null||i===null))throw new g(`${e} answered an UNPRICED row that does not say why \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(i)}. NULL means NOT YET PRICED, and the reason is how an operator learns that traffic is being served which nobody can bill.`);return{inference_id:n,cost_nano_usd:o===void 0?null:o,price_version:s,unpriced_reason:i}}var U=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async write(e,r){let n=`${this.opts.apiUrl.replace(/\/+$/,"")}${it}`,o;try{o=await this.fetchImpl(n,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify(r),signal:AbortSignal.timeout(at)})}catch(c){throw new g(`the uRun ledger surface at ${n} could not be reached: ${c instanceof Error?c.message:String(c)}`)}if(o.status===409){let c=await o.text().catch(()=>""),u=ut(c);throw u.status==="found"&&dt(r,u.row)?new K(u.row,`the uRun control plane already holds THIS PROXY'S OWN row for inference_id ${r.inference_id} \u2014 every billable field matches the row being re-sent, so an earlier attempt landed and this retry is a no-op. Nothing was written and nothing was overwritten.`):u.status==="not_in_this_org"?new $(`inference_id ${r.inference_id} is ALREADY TAKEN by a ledger row this org cannot read. The id space is a global primary key, so this is not a retry and cannot be this proxy's own write \u2014 either two orgs collided on a uuid, or a row was written for this id by somebody else.`,"duplicate_foreign"):new $(`the uRun control plane already holds a ledger row for inference_id ${r.inference_id} that is NOT the one being written${u.status==="unavailable"?" (and it could not be read back to compare)":""} \u2014 nothing was written and nothing was overwritten. Either this proxy wrote twice, or a row was fabricated ahead of it (this write is authenticated by the CALLER'S OWN org key). ${c.slice(0,400)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new j("the uRun org API key was rejected by the uRun control plane when writing the inference ledger \u2014 the request was served but produced no billable row");if(!o.ok){let c=await o.text().catch(u=>`<body unreadable: ${u instanceof Error?u.message:String(u)}>`);throw new g(`the uRun ledger surface at ${n} answered HTTP ${o.status}: ${c.slice(0,400)}`)}let a;try{a=await o.json()}catch(c){throw new g(`the uRun ledger surface at ${n} answered a body that is not JSON: ${c instanceof Error?c.message:String(c)}`)}let s=a?.recorded;if(s===void 0)throw new g(`the uRun ledger surface at ${n} answered a body with no "recorded" field`);let i=ht(s,`the uRun ledger surface at ${n}`);if(i.inference_id!==r.inference_id)throw new g(`the uRun ledger surface at ${n} answered for inference_id ${i.inference_id} when asked about ${r.inference_id}`);return i}};p();import{createServer as gt}from"http";p();var L="/inference-usage",Re=25e3;var pt=new Set(["Error","TypeError","SyntaxError","TimeoutError"]);function Ee(t){if(!(t instanceof Error))return`a ${typeof t}`;let e=pt.has(t.name)?t.name:"Error",r=t.cause?.code;return typeof r=="string"&&/^[A-Z][A-Z0-9_]{0,31}$/.test(r)?`${e} (${r})`:e}var H=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async lookup(e,r){let n=`${this.opts.apiUrl.replace(/\/+$/,"")}${L}`,o;try{o=await this.fetchImpl(n,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify({inference_ids:r}),signal:AbortSignal.timeout(Re)})}catch(s){throw new C(`the uRun usage surface (POST ${L}) could not be reached within ${Re}ms: ${Ee(s)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new m("the uRun org API key was rejected by the uRun control plane \u2014 check the key and that it is not revoked");if(!o.ok){let s=await o.text().catch(()=>null),i=s===null?"body unreadable":`${s.length}-character body`;throw new C(`the uRun usage surface (POST ${L}) answered HTTP ${o.status} (${i})`)}let a;try{a=await o.json()}catch(s){throw new C(`the uRun usage surface (POST ${L}) answered HTTP ${o.status} with a body that is not JSON: ${Ee(s)}`)}return se(a,this.opts.onRejectedRow)}};var ft=5e3,mt=3e3,yt="/api/client-tokens";function W(t,e,r){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(r))}function xe(t,e,r,n){W(t,e,{error:{message:r,type:n}})}async function wt(t,e=fetch){let r=`${t.replace(/\/+$/,"")}${yt}`;try{let n=await e(r,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(mt)});return n.status===404?{ready:!1,detail:`${r} answered 404 \u2014 this is not a uRun control plane`}:n.status>=500?{ready:!1,detail:`${r} answered HTTP ${n.status}`}:{ready:!0,detail:`${r} answered HTTP ${n.status}`}}catch(n){return{ready:!1,detail:`${r} unreachable: ${n instanceof Error?n.message:String(n)}`}}}function pr(t){let e=t.resolver??new O({gatewayUrl:t.baseUrl}),r=t.registry??new I({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),n=t.probeControlPlane??(()=>wt(t.baseUrl)),o=t.usageClient??new H({apiUrl:t.apiUrl}),a=t.ledgerClient??new U({apiUrl:t.apiUrl}),s=new k(t.defaultMetrics===void 0?{}:{defaultMetrics:t.defaultMetrics}),i=async l=>e.resolve(E(l.headers.authorization)),c=l=>l instanceof m?{status:l.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:l instanceof v?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,u=de({clients:async l=>r.clientsFor(await i(l)),usage:{authorize:l=>E(l.headers.authorization),lookup:(l,d)=>o.lookup(l,d)},statusOf:c,...t.requestLog?{requestLog:t.requestLog}:{},observe:l=>s.observe(l),ledger:{write:(l,d)=>a.write(E(l.headers.authorization),d),observe:l=>s.observeLedger(l),...t.ledgerLog?{report:t.ledgerLog}:{}}}),f=null,_=gt((l,d)=>{let h=new URL(l.url??"/","http://localhost");(async()=>{if(l.method==="GET"&&h.pathname==="/healthz"){W(d,200,{ok:!0});return}if(l.method==="GET"&&h.pathname==="/readyz"){let J=Date.now();if(!f||J-f.at>=ft){let Ae=await n();f={at:J,...Ae}}W(d,f.ready?200:503,{ok:f.ready,control_plane:f.detail});return}let y=h.pathname==="/v1"||h.pathname.startsWith("/v1/"),F=h.pathname.startsWith("/partners/");if(!y&&!F){xe(d,404,`no route for ${l.method} ${h.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface, the partner adapter surface (/partners/...), the WS upgrade paths (/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent and /v1/realtime), plus /healthz and /readyz`,"invalid_request_error");return}u(l,d)})().catch(y=>{if(!d.headersSent){xe(d,500,String(y instanceof Error?y.message:y),"proxy_error");return}d.end()})}),M=t.conversations??new T({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),Ce=[Y(_,{authenticate:async l=>{let d=await e.resolve(l),h=M.connectionFor(d);return{clients:h.clients,scope:P(d.apiKey),onBindResumeHandle:h.bindResumeHandle,clientsForResume:h.clientsForResume,release:h.release}}}),X(_,{clients:async l=>r.clientsFor(await i(l)),statusOf:c}).close,ee(_,{resolver:{resolve:async l=>{let d=await e.resolve(E(l.headers.authorization)),h=M.connectionFor(d),y;try{y=await re(h.clients,te(l))}catch(F){throw h.release().catch(()=>{}),F}return{...y,release:h.release}}}})];_.on("upgrade",(l,d)=>{let h=new URL(l.url??"/","http://localhost");h.pathname===V||h.pathname===Q||h.pathname===Z||d.destroyed||(d.write(`HTTP/1.1 404 Not Found\r
|
|
4
|
-
Connection: close\r
|
|
5
|
-
\r
|
|
6
|
-
no WS route for ${h.pathname} \u2014 this endpoint serves only the Gemini Live, /v1/responses, and /v1/realtime upgrade paths`),d.destroy())});let Se=async()=>{for(let l of Ce)await l();await M.detachAll()};return{server:_,metricsServer:fe(s),metrics:s,edgeIdentityServer:we(),closeAll:()=>r.closeAll(),closeWebSockets:Se}}async function gr(t,e,r){r&&await r();let n=new Promise(o=>t.close(()=>o()));t.closeAllConnections(),await n,await ce()||ue(),await e()}export{xt as a,ge as b,S as c,Ne as d,Te as e,Pe as f,Le as g,He as h,A as i,k as j,fe as k,Ot as l,Fe as m,De as n,Be as o,Ge as p,me as q,Ke as r,je as s,We as t,we as u,m as v,v as w,E as x,Ye as y,Qe as z,O as A,Ze as B,G as C,Lt as D,Ht as E,nt as F,P as G,I as H,be as I,ot as J,N as K,T as L,it as M,at as N,$ as O,g as P,ht as Q,U as R,ft as S,mt as T,pr as U,gr as V};
|
|
File without changes
|