@urun-sh/openai 0.6.23 → 0.6.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-JOOR4RUT.js → chunk-PPPJ42Z7.js} +1 -1
- package/dist/{chunk-ERRG6R4T.js → chunk-SOAZAPCO.js} +1 -1
- package/dist/{chunk-FHGSREBO.js → chunk-VY333OKA.js} +4 -4
- package/dist/hosted/bin.cjs +10 -10
- package/dist/hosted/bin.js +1 -1
- package/dist/hosted/index.cjs +10 -10
- package/dist/hosted/index.js +1 -1
- package/dist/pi-extension/standalone.cjs +14 -14
- package/dist/proxy/cli.cjs +1 -1
- package/dist/proxy/cli.js +1 -1
- package/dist/proxy/index.js +1 -1
- package/package.json +2 -2
package/dist/hosted/index.cjs
CHANGED
|
@@ -7,26 +7,26 @@ Connection: close\r
|
|
|
7
7
|
\r
|
|
8
8
|
${f}\r
|
|
9
9
|
`),a.destroy())}))});let s=null;return()=>(s??=(async()=>{for(let i of r.clients)i.close(1001,"server closing");await new Promise((i,a)=>r.close(u=>u===void 0?i():a(u)))})(),s)}var Ee=require("ws");var hs=require("crypto"),gs="Inference-Id";function bt(){return(0,hs.randomUUID)()}function rr(t){return t==="/v1"||t.startsWith("/v1/")}function ys(t,e){if(!rr(t)||e===null)throw new Error(`no inference turn for ${t}: an inference lane was reached off the /v1 surface, so its response would carry no Inference-Id and could never be billed`);return e}function St(t){return`resp_${t}`}function _s(t){return`chatcmpl-${t}`}function ws(t){return`msg_${t}`}function le(t){return e=>{t.sessionId=e.sessionId,t.catalog=e.identity}}function vs(t){let e=t.now??(()=>performance.now());return{event:"inference_request",ts:new Date().toISOString(),inference_id:t.turn.id,method:t.method,path:t.path,status:t.res.headersSent?t.res.statusCode:null,delivered:t.res.writableFinished,stream_error:t.turn.streamError,model:t.turn.model,stream:t.turn.stream,org:t.turn.org,duration_ms:e()-t.turn.startedAt,ttft_ms:t.turn.stream===!0?t.turn.ttftMs:null,prompt_tokens:t.turn.promptTokens,completion_tokens:t.turn.completionTokens,output_units:t.turn.outputUnits,output_minutes:t.turn.outputMinutes,session_id:t.turn.sessionId,queue_ms:t.turn.timing?.queue_ms??null,prefill_ms:t.turn.timing?.prefill_ms??null,decode_ms:t.turn.timing?.decode_ms??null,total_ms:t.turn.timing?.total_ms??null,...t.turn.decision?{decision_resolve_ms:t.turn.decision.resolve_ms,decision_dial_ms:t.turn.decision.dial_ms,decision_first_event_ms:t.turn.decision.first_event_ms,decision_stream_ms:t.turn.decision.stream_ms,decision_question_fwd_ms:t.turn.decision.question_fwd_ms,decision_head_ms:t.turn.decision.head_ms}:{},catalog:t.turn.catalog}}function bs(t){return`${JSON.stringify(t)}
|
|
10
|
-
`}var mu=256,hu=128;function oe(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,hu)}var $e=class t{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(e,n,r){this.requests[e]=(this.requests[e]??0)+1,this.localInBytes+=n,this.naiveInBytes+=r}modelEntry(e){let n=this.models[e];if(n)return n;let r=Object.keys(this.models).length>=mu?"(other)":e;return this.models[r]??={requests:0,tokens_out:0}}model(e){this.modelEntry(e).requests+=1}track(e,n,r){let o=this,s=performance.now(),i=null,a=0;return(async function*(){for await(let l of e)l.type==="response.output_text.delta"&&typeof l.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+=l.delta.length,n!==void 0&&(o.modelEntry(n).tokens_out+=1)),yield l;let u=(performance.now()-(i??s))/1e3;a>0&&u>0&&(o.tokRates.push(a/u),o.tokRates.length>512&&o.tokRates.shift())})()}static p50(e){if(!e.length)return null;let n=[...e].sort((r,o)=>r-o);return n[Math.floor(n.length/2)]}snapshot(){let e=Object.values(this.requests).reduce((n,r)=>n+r,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:t.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:t.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:e},persistent_connections:1,history_bytes_saved:Math.max(0,this.naiveInBytes-this.localInBytes),history_bytes_saved_pct:this.naiveInBytes>0?Math.round((1-this.localInBytes/this.naiveInBytes)*1e3)/10:null}}}},gu=256;function yu(t){if(typeof t=="string")return[{role:"user",content:t}];if(Array.isArray(t))return t;throw new Error("responses input must be a string or an array of messages/items")}var Ne=class extends Error{},Ke=class{conversations=new Map;thread(e,n){let r=yu(n);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new Ne(`unknown previous_response_id ${String(e)} (proxy restarts drop stored responses)`);return[...o,...r]}remember(e,n,r,o=!0){this.conversations.set(e,[...n,...r]);for(let s of this.conversations.keys()){if(this.conversations.size<=gu)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},Ss=new WeakMap;function kt(t){let e=Ss.get(t);return e||(e=new Ke,Ss.set(t,e)),e}function ze(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function fe(t){return t.find(e=>e.type==="response.completed")?.response??null}function Ye(t){return t.type!=="response.function_call_arguments.delta"||typeof t.tool_index!="number"?null:{tool_index:t.tool_index,call_id:typeof t.call_id=="string"?t.call_id:void 0,name:typeof t.name=="string"?t.name:void 0,delta:typeof t.delta=="string"?t.delta:""}}var xe=class{calls=new Map;add(e){let n=this.calls.get(e.tool_index),r=!n;return n||(n={args:""},this.calls.set(e.tool_index,n)),e.call_id&&(n.id=e.call_id),e.name&&(n.name=e.name),n.args+=e.delta,{call:n,isNew:r}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([e,n])=>({type:"function_call",call_id:n.id??`call_${e}`,name:n.name??"",arguments:n.args}))}},_u=["<tool_call>","<function="];function wu(t){return _u.find(e=>t.includes(e))}function Ue(t,e){return`serving row emitted a prose tool call (literal ${JSON.stringify(e)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${t}" (see catalog parser_defaults)`}function De(t,e,n){if(!(!t||e>0))return wu(n)}function Xe(t,e){let n=e?.output_text;return typeof n=="string"?n:t.filter(r=>r.type==="response.output_text.delta"&&typeof r.delta=="string").map(r=>r.delta).join("")}function vu(t){let e=t?.output;if(!Array.isArray(e))return[];let n=[];for(let r of e){if(r===null||typeof r!="object")continue;let o=r;o.type==="message"?n.push({role:"assistant",content:o.content??""}):o.type==="function_call"&&n.push({type:"function_call",call_id:o.call_id,name:o.name,arguments:o.arguments})}return n}function or(t,e){let n=vu(t);return n.length>0?n:e?[{role:"assistant",content:e}]:[]}var de=class extends Error{constructor(n,r,o=400,s){super(n);this.code=r;this.status=o;this.param=s}code;status;param};function Qe(t,e){try{return t.thread(e.previous_response_id,e.input)}catch(n){throw n instanceof Ne?new de(n.message,"previous_response_not_found",400,"previous_response_id"):new de(String(n instanceof Error?n.message:n),"invalid_input")}}function ks(t){let e=t.text;if(e!==null&&typeof e=="object"){let n=e.format;if(n!==void 0)return n}}async function sr(t,e,n,r,o){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new de("instructions must be a string","invalid_instructions");let s=typeof e.instructions=="string"?e.instructions:void 0;return t.createResponse({model:e.model,input:n,stream:r,tools:e.tools,tool_choice:e.tool_choice,temperature:e.temperature,top_p:e.top_p,stop:e.stop,...s!==void 0?{instructions:s}:{},max_output_tokens:e.max_output_tokens,...e.reasoning_effort!==void 0?{reasoning_effort:e.reasoning_effort}:{},...e.chat_template_kwargs!==void 0?{chat_template_kwargs:e.chat_template_kwargs}:{},...ks(e)!==void 0?{response_format:ks(e)}:{}},o)}async function Rt(t){let{body:e,clients:n,store:r,stats:o}=t,s=Qe(r,e);e.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(s))-Buffer.byteLength(JSON.stringify(e.input)));let i=t.responseId,a=t.remember??e.store!==!1,u=e.store!==!1,l=oe(e);o.model(l);let d=Array.isArray(e.tools)&&e.tools.length>0,c=t.turn,y=await sr(n,e,s,!0,c?le(c):void 0);return{responseId:i,events:Ru({events:y,conversation:s,responseId:i,modelKey:l,requestHadTools:d,remember:a,persist:u,store:r,stats:o,turn:t.turn})}}function bu(t,e){let n=[...t].sort((u,l)=>u.output_index-l.output_index).map(({item:u})=>u),r=Array.isArray(e?.output)?e.output:[],o=r.filter(u=>u!==null&&typeof u=="object"&&u.type==="message"),s=n.filter(u=>u.type==="message").length===o.length,i=0;return[...n.map(u=>u.type==="message"&&s?ku(o[i++],u):u),...Su(r,n)]}function Su(t,e){let n=e.some(l=>l.type==="message"),r=e.some(l=>l.type==="reasoning"),o=l=>String(l.call_id??l.id??""),s=e.filter(l=>l.type==="function_call"),i=t.filter(l=>l!==null&&typeof l=="object"&&l.type==="function_call").length,a=s.length===i,u=new Set(s.map(o));return t.filter(l=>{if(l===null||typeof l!="object")return!0;let d=l;return d.type==="message"?!n:d.type==="reasoning"?!r:d.type==="function_call"?!a&&!u.has(o(d)):!0})}function ku(t,e){let n=a=>a!==null&&typeof a=="object"&&a.type==="output_text",r=Array.isArray(t.content)?t.content:[];if(r.filter(n).length!==1)return e;let s=(Array.isArray(e.content)?e.content:[]).find(n);if(s===void 0)return e;let i={...t};return i.id=e.id,t.role===void 0&&(i.role=e.role),t.status===void 0&&(i.status=e.status),i.content=r.map(a=>n(a)?{...a,text:s.text}:a),i}async function*Ru(t){let{events:e,conversation:n,responseId:r,modelKey:o,requestHadTools:s,remember:i,persist:a,store:u,stats:l}=t,d=[],c=C=>C.response&&typeof C.response=="object"?{...C,response:{...C.response,id:r}}:C,y=`item_${r.slice(5)}`,f=!1,p=!1,g=0;function*h(){f||(f=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let _=[],v=(C,$)=>{_.push({output_index:C,item:$})},m=!1,R=0,b="",w=0,S="";function*T(){if(!m)return;m=!1,yield{type:"response.reasoning_text.done",item_id:b,output_index:R,content_index:0,text:S};let C={id:b,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:S}],status:"completed"};yield{type:"response.output_item.done",output_index:R,item:C},v(R,C),S=""}let x=!1,O=0,P="",D=0,X="",re="";function*pe(C){if(!x)return;x=!1,yield{type:"response.output_text.done",item_id:P,output_index:O,content_index:0,text:X},yield{type:"response.content_part.done",item_id:P,output_index:O,content_index:0,part:{type:"output_text",text:X}};let $={id:P,type:"message",role:"assistant",status:C,content:[{type:"output_text",text:X}]};yield{type:"response.output_item.done",output_index:O,item:$},v(O,$),X=""}let ve=new xe,V=null,J=-1;function*Z(C){if(!V)return;let $=V;V=null,yield{type:"response.function_call_arguments.done",item_id:$.fcId,output_index:$.outputIndex,arguments:$.args};let z={id:$.fcId,type:"function_call",call_id:$.callId??$.fcId,name:$.name??"",arguments:$.args,status:C};yield{type:"response.output_item.done",output_index:$.outputIndex,item:z},v($.outputIndex,z)}let Te=!1;for await(let C of l.track(e,o,t.turn)){d.push(C);let $=String(C.type??"");if($==="response.created"||$==="response.in_progress"){f=!0,yield c(C);continue}if($==="response.output_item.added"){p=!0,yield c(C);continue}if($==="response.reasoning_text.delta"&&!p){yield*h(),m||(m=!0,R=g++,w+=1,b=w===1?`rs_${r.slice(5)}`:`rs_${r.slice(5)}_${w}`,yield{type:"response.output_item.added",output_index:R,item:{id:b,type:"reasoning",summary:[],content:[],status:"in_progress"}}),S+=String(C.delta??""),yield{type:"response.reasoning_text.delta",item_id:b,output_index:R,content_index:0,delta:C.delta};continue}if($==="response.output_text.delta"&&!p){yield*h(),yield*T(),x||(x=!0,O=g++,D+=1,P=D===1?y:`${y}_${D}`,yield{type:"response.output_item.added",output_index:O,item:{id:P,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:P,output_index:O,content_index:0,part:{type:"output_text",text:""}}),X+=String(C.delta??""),re+=String(C.delta??""),yield{type:$,item_id:P,output_index:O,content_index:0,delta:C.delta};continue}let z=p?null:Ye(C);if(z){if(yield*h(),yield*T(),yield*pe("completed"),ve.add(z),z.tool_index!==J){yield*Z("completed"),J=z.tool_index;let ge=g++;V={fcId:`fc_${r.slice(5)}_${z.tool_index}`,outputIndex:ge,callId:z.call_id,name:z.name,args:""},yield{type:"response.output_item.added",output_index:ge,item:{id:V.fcId,type:"function_call",call_id:V.callId??V.fcId,name:V.name??"",arguments:"",status:"in_progress"}}}V&&(z.call_id&&(V.callId=z.call_id),z.name&&(V.name=z.name),V.args+=z.delta,yield{type:"response.function_call_arguments.delta",item_id:V.fcId,output_index:V.outputIndex,delta:z.delta});continue}if($==="response.completed"&&!p){let ge=ze(C.response),F=De(s,ve.size+ge.length,re);if(F){Te=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:Ue(o,F)}};break}if(yield*T(),yield*pe("completed"),yield*Z("completed"),ve.size===0)for(let be of ge){let Ve=g++,Bt=be.id??be.call_id??`fc_${Ve}`,Gn=be.arguments??"",fo={id:Bt,type:"function_call",call_id:be.call_id??Bt,name:be.name??""};yield{type:"response.output_item.added",output_index:Ve,item:{...fo,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:Bt,output_index:Ve,delta:Gn},yield{type:"response.function_call_arguments.done",item_id:Bt,output_index:Ve,arguments:Gn};let mo={...fo,arguments:Gn,status:"completed"};yield{type:"response.output_item.done",output_index:Ve,item:mo},v(Ve,mo)}let ee=bu(_,C.response);yield ee.length>0?c({...C,response:{...C.response,output:ee}}):c(C);continue}yield c(C)}if(yield*T(),yield*pe("incomplete"),yield*Z("incomplete"),i&&!Te){let C=fe(d);u.remember(r,n,or(C,Xe(d,C)),a)}}var ur="/v1/responses",xu=64*1024*1024,Eu=8*1024*1024,Au=16,Rs=32,xs=3600*1e3,Iu=/^[A-Za-z0-9._-]{1,256}$/;function se(t,e,n){return{type:"error",status:t,...n!==void 0?{stream_id:n}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var Ou=se(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 lr(){let t,e;return{promise:new Promise((r,o)=>{t=r,e=o}),resolve:t,reject:e}}async function Tu(t){for(;t.bufferedAmount>Eu&&t.readyState===Ee.WebSocket.OPEN;){let{promise:e,resolve:n}=lr();setTimeout(n,5),await e}}var ir=class extends Ke{constructor(n){super();this.shared=n}shared;thread(n,r){if(n==null)return super.thread(n,r);try{return super.thread(n,r)}catch(o){if(!(o instanceof Ne))throw o;return this.shared.thread(n,r)}}remember(n,r,o,s=!0){s?this.shared.remember(n,r,o):super.remember(n,r,o)}forget(n){super.forget(n),this.shared.forget(n)}},ar=class{constructor(e,n,r,o){this.ws=e;this.clients=n;this.stats=r;this.statusOf=o;this.store=new ir(kt(n)),e.on("message",(i,a)=>this.onMessage(i,a)),e.on("close",()=>this.dispose()),e.on("error",()=>this.dispose()),setTimeout(()=>{this.closed||(this.sendFrame(se(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${xs/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},xs).unref?.()}ws;clients;stats;statusOf;store;lanes=new Map;laneOfResponse=new Map;activeTurns=new Set;active=0;waiters=[];closed=!1;onMessage(e,n){if(this.closed)return;if(n){this.sendFrame(se(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(se(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(se(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(se(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(se(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(se(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"||!Iu.test(o.stream_id)){this.sendFrame(Ou);return}if(s=o.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=Rs){this.sendFrame(se(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${Rs}). Reuse an existing stream_id or open a new WebSocket connection.`,param:"stream_id"},s));return}}let i=o.generate===!1?()=>this.runWarmup(o,s):()=>this.runTurn(o,s),a=s??"",u=this.lanes.get(a)??Promise.resolve();this.lanes.set(a,u.then(i).catch(()=>{}))}namedLaneCount(){return this.lanes.has("")?this.lanes.size-1:this.lanes.size}async runTurn(e,n){if(!this.closed){await this.acquire();try{let r=Buffer.byteLength(JSON.stringify(e));this.stats.request("responses_ws",r,r);let o;try{o=await Rt({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:St(bt())})}catch(i){this.failTurn(i,n,e);return}this.laneOfResponse.set(o.responseId,n??null);let s=o.events;this.activeTurns.add(s);try{for await(let i of s){if(this.closed||this.ws.readyState!==Ee.WebSocket.OPEN)break;this.send(i,n),await Tu(this.ws)}}finally{this.activeTurns.delete(s)}}catch(r){this.failTurn(r,n,e)}finally{this.release()}}}runWarmup(e,n){if(this.closed)return;let r=St(bt());try{let o=Qe(this.store,{...e,input:e.input??(e.previous_response_id!=null?[]:void 0)});this.store.remember(r,o,[],e.store!==!1),this.laneOfResponse.set(r,n??null),this.send({type:"response.created",response:{id:r,object:"response",status:"in_progress"}},n)}catch(o){this.failTurn(o,n,e)}}failTurn(e,n,r){this.sendFrame(this.errorFrameOf(e,n));let o=r.previous_response_id;typeof o=="string"&&this.laneOfResponse.get(o)===(n??null)&&(this.store.forget(o),this.laneOfResponse.delete(o))}errorFrameOf(e,n){let r=String(e instanceof Error?e.message:e);if(e instanceof de)return se(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},n);if(e instanceof N)return se(404,{type:"invalid_request_error",code:"model_not_found",message:r},n);if(e instanceof q)return se(503,{type:"upstream_error",code:"session_gone",message:r},n);let o=this.statusOf?.(e)??null;return o?se(o.status,{type:o.openaiType,message:r},n):se(500,{type:"upstream_error",message:r},n)}async acquire(){for(;this.active>=Au;){let{promise:e,resolve:n}=lr();this.waiters.push(n),await e}this.active++}release(){this.active--,this.waiters.shift()?.()}dispose(){if(!this.closed){this.closed=!0;for(let e of[...this.activeTurns])e.return(void 0);this.activeTurns.clear(),this.lanes.clear(),this.laneOfResponse.clear();for(let e of this.waiters.splice(0))e()}}send(e,n){if(this.closed||this.ws.readyState!==Ee.WebSocket.OPEN)return;let r=n===void 0?e:{...e,stream_id:n};this.ws.send(JSON.stringify(r))}sendFrame(e){this.closed||this.ws.readyState!==Ee.WebSocket.OPEN||this.ws.send(JSON.stringify(e))}};function nn(t,e,n){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${n}\r
|
|
10
|
+
`}var mu=256,hu=128;function oe(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,hu)}var $e=class t{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(e,n,r){this.requests[e]=(this.requests[e]??0)+1,this.localInBytes+=n,this.naiveInBytes+=r}modelEntry(e){let n=this.models[e];if(n)return n;let r=Object.keys(this.models).length>=mu?"(other)":e;return this.models[r]??={requests:0,tokens_out:0}}model(e){this.modelEntry(e).requests+=1}track(e,n,r){let o=this,s=performance.now(),i=null,a=0;return(async function*(){for await(let l of e)l.type==="response.output_text.delta"&&typeof l.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+=l.delta.length,n!==void 0&&(o.modelEntry(n).tokens_out+=1)),yield l;let u=(performance.now()-(i??s))/1e3;a>0&&u>0&&(o.tokRates.push(a/u),o.tokRates.length>512&&o.tokRates.shift())})()}static p50(e){if(!e.length)return null;let n=[...e].sort((r,o)=>r-o);return n[Math.floor(n.length/2)]}snapshot(){let e=Object.values(this.requests).reduce((n,r)=>n+r,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:t.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:t.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:e},persistent_connections:1,history_bytes_saved:Math.max(0,this.naiveInBytes-this.localInBytes),history_bytes_saved_pct:this.naiveInBytes>0?Math.round((1-this.localInBytes/this.naiveInBytes)*1e3)/10:null}}}},gu=256;function yu(t){if(typeof t=="string")return[{role:"user",content:t}];if(Array.isArray(t))return t;throw new Error("responses input must be a string or an array of messages/items")}var Ne=class extends Error{},Ke=class{conversations=new Map;thread(e,n){let r=yu(n);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new Ne(`unknown previous_response_id ${String(e)} (proxy restarts drop stored responses)`);return[...o,...r]}remember(e,n,r,o=!0){this.conversations.set(e,[...n,...r]);for(let s of this.conversations.keys()){if(this.conversations.size<=gu)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},Ss=new WeakMap;function kt(t){let e=Ss.get(t);return e||(e=new Ke,Ss.set(t,e)),e}function ze(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function fe(t){return t.find(e=>e.type==="response.completed")?.response??null}function Ye(t){return t.type!=="response.function_call_arguments.delta"||typeof t.tool_index!="number"?null:{tool_index:t.tool_index,call_id:typeof t.call_id=="string"?t.call_id:void 0,name:typeof t.name=="string"?t.name:void 0,delta:typeof t.delta=="string"?t.delta:""}}var xe=class{calls=new Map;add(e){let n=this.calls.get(e.tool_index),r=!n;return n||(n={args:""},this.calls.set(e.tool_index,n)),e.call_id&&(n.id=e.call_id),e.name&&(n.name=e.name),n.args+=e.delta,{call:n,isNew:r}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([e,n])=>({type:"function_call",call_id:n.id??`call_${e}`,name:n.name??"",arguments:n.args}))}},_u=["<tool_call>","<function="];function wu(t){return _u.find(e=>t.includes(e))}function Ue(t,e){return`serving row emitted a prose tool call (literal ${JSON.stringify(e)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${t}" (see catalog parser_defaults)`}function De(t,e,n){if(!(!t||e>0))return wu(n)}function Xe(t,e){let n=e?.output_text;return typeof n=="string"?n:t.filter(r=>r.type==="response.output_text.delta"&&typeof r.delta=="string").map(r=>r.delta).join("")}function vu(t){let e=t?.output;if(!Array.isArray(e))return[];let n=[];for(let r of e){if(r===null||typeof r!="object")continue;let o=r;o.type==="message"?n.push({role:"assistant",content:o.content??""}):o.type==="function_call"&&n.push({type:"function_call",call_id:o.call_id,name:o.name,arguments:o.arguments})}return n}function or(t,e){let n=vu(t);return n.length>0?n:e?[{role:"assistant",content:e}]:[]}var de=class extends Error{constructor(n,r,o=400,s){super(n);this.code=r;this.status=o;this.param=s}code;status;param};function Qe(t,e){try{return t.thread(e.previous_response_id,e.input)}catch(n){throw n instanceof Ne?new de(n.message,"previous_response_not_found",400,"previous_response_id"):new de(String(n instanceof Error?n.message:n),"invalid_input")}}function ks(t){let e=t.text;if(e!==null&&typeof e=="object"){let n=e.format;if(n!==void 0)return n}}async function sr(t,e,n,r,o){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new de("instructions must be a string","invalid_instructions");let s=typeof e.instructions=="string"?e.instructions:void 0;return t.createResponse({model:e.model,input:n,stream:r,tools:e.tools,tool_choice:e.tool_choice,temperature:e.temperature,top_p:e.top_p,stop:e.stop,...s!==void 0?{instructions:s}:{},max_output_tokens:e.max_output_tokens,...e.reasoning_effort!==void 0?{reasoning_effort:e.reasoning_effort}:{},...e.chat_template_kwargs!==void 0?{chat_template_kwargs:e.chat_template_kwargs}:{},...ks(e)!==void 0?{response_format:ks(e)}:{}},o)}async function Rt(t){let{body:e,clients:n,store:r,stats:o}=t,s=Qe(r,e);e.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(s))-Buffer.byteLength(JSON.stringify(e.input)));let i=t.responseId,a=t.remember??e.store!==!1,u=e.store!==!1,l=oe(e);o.model(l);let d=Array.isArray(e.tools)&&e.tools.length>0,c=t.turn,y=await sr(n,e,s,!0,c?le(c):void 0);return{responseId:i,events:Ru({events:y,conversation:s,responseId:i,modelKey:l,requestHadTools:d,remember:a,persist:u,store:r,stats:o,turn:t.turn})}}function bu(t,e){let n=[...t].sort((u,l)=>u.output_index-l.output_index).map(({item:u})=>u),r=Array.isArray(e?.output)?e.output:[],o=r.filter(u=>u!==null&&typeof u=="object"&&u.type==="message"),s=n.filter(u=>u.type==="message").length===o.length,i=0;return[...n.map(u=>u.type==="message"&&s?ku(o[i++],u):u),...Su(r,n)]}function Su(t,e){let n=e.some(l=>l.type==="message"),r=e.some(l=>l.type==="reasoning"),o=l=>String(l.call_id??l.id??""),s=e.filter(l=>l.type==="function_call"),i=t.filter(l=>l!==null&&typeof l=="object"&&l.type==="function_call").length,a=s.length===i,u=new Set(s.map(o));return t.filter(l=>{if(l===null||typeof l!="object")return!0;let d=l;return d.type==="message"?!n:d.type==="reasoning"?!r:d.type==="function_call"?!a&&!u.has(o(d)):!0})}function ku(t,e){let n=a=>a!==null&&typeof a=="object"&&a.type==="output_text",r=Array.isArray(t.content)?t.content:[];if(r.filter(n).length!==1)return e;let s=(Array.isArray(e.content)?e.content:[]).find(n);if(s===void 0)return e;let i={...t};return i.id=e.id,t.role===void 0&&(i.role=e.role),t.status===void 0&&(i.status=e.status),i.content=r.map(a=>n(a)?{...a,text:s.text}:a),i}async function*Ru(t){let{events:e,conversation:n,responseId:r,modelKey:o,requestHadTools:s,remember:i,persist:a,store:u,stats:l}=t,d=[],c=C=>C.response&&typeof C.response=="object"?{...C,response:{...C.response,id:r}}:C,y=`item_${r.slice(5)}`,f=!1,p=!1,g=0;function*h(){f||(f=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let w=[],v=(C,$)=>{w.push({output_index:C,item:$})},m=!1,R=0,b="",_=0,S="";function*T(){if(!m)return;m=!1,yield{type:"response.reasoning_text.done",item_id:b,output_index:R,content_index:0,text:S};let C={id:b,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:S}],status:"completed"};yield{type:"response.output_item.done",output_index:R,item:C},v(R,C),S=""}let x=!1,O=0,P="",D=0,X="",re="";function*pe(C){if(!x)return;x=!1,yield{type:"response.output_text.done",item_id:P,output_index:O,content_index:0,text:X},yield{type:"response.content_part.done",item_id:P,output_index:O,content_index:0,part:{type:"output_text",text:X}};let $={id:P,type:"message",role:"assistant",status:C,content:[{type:"output_text",text:X}]};yield{type:"response.output_item.done",output_index:O,item:$},v(O,$),X=""}let ve=new xe,V=null,J=-1;function*Z(C){if(!V)return;let $=V;V=null,yield{type:"response.function_call_arguments.done",item_id:$.fcId,output_index:$.outputIndex,arguments:$.args};let z={id:$.fcId,type:"function_call",call_id:$.callId??$.fcId,name:$.name??"",arguments:$.args,status:C};yield{type:"response.output_item.done",output_index:$.outputIndex,item:z},v($.outputIndex,z)}let Te=!1;for await(let C of l.track(e,o,t.turn)){d.push(C);let $=String(C.type??"");if($==="response.created"||$==="response.in_progress"){f=!0,yield c(C);continue}if($==="response.output_item.added"){p=!0,yield c(C);continue}if($==="response.reasoning_text.delta"&&!p){yield*h(),m||(m=!0,R=g++,_+=1,b=_===1?`rs_${r.slice(5)}`:`rs_${r.slice(5)}_${_}`,yield{type:"response.output_item.added",output_index:R,item:{id:b,type:"reasoning",summary:[],content:[],status:"in_progress"}}),S+=String(C.delta??""),yield{type:"response.reasoning_text.delta",item_id:b,output_index:R,content_index:0,delta:C.delta};continue}if($==="response.output_text.delta"&&!p){yield*h(),yield*T(),x||(x=!0,O=g++,D+=1,P=D===1?y:`${y}_${D}`,yield{type:"response.output_item.added",output_index:O,item:{id:P,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:P,output_index:O,content_index:0,part:{type:"output_text",text:""}}),X+=String(C.delta??""),re+=String(C.delta??""),yield{type:$,item_id:P,output_index:O,content_index:0,delta:C.delta};continue}let z=p?null:Ye(C);if(z){if(yield*h(),yield*T(),yield*pe("completed"),ve.add(z),z.tool_index!==J){yield*Z("completed"),J=z.tool_index;let ge=g++;V={fcId:`fc_${r.slice(5)}_${z.tool_index}`,outputIndex:ge,callId:z.call_id,name:z.name,args:""},yield{type:"response.output_item.added",output_index:ge,item:{id:V.fcId,type:"function_call",call_id:V.callId??V.fcId,name:V.name??"",arguments:"",status:"in_progress"}}}V&&(z.call_id&&(V.callId=z.call_id),z.name&&(V.name=z.name),V.args+=z.delta,yield{type:"response.function_call_arguments.delta",item_id:V.fcId,output_index:V.outputIndex,delta:z.delta});continue}if($==="response.completed"&&!p){let ge=ze(C.response),F=De(s,ve.size+ge.length,re);if(F){Te=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:Ue(o,F)}};break}if(yield*T(),yield*pe("completed"),yield*Z("completed"),ve.size===0)for(let be of ge){let Ve=g++,Bt=be.id??be.call_id??`fc_${Ve}`,Gn=be.arguments??"",fo={id:Bt,type:"function_call",call_id:be.call_id??Bt,name:be.name??""};yield{type:"response.output_item.added",output_index:Ve,item:{...fo,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:Bt,output_index:Ve,delta:Gn},yield{type:"response.function_call_arguments.done",item_id:Bt,output_index:Ve,arguments:Gn};let mo={...fo,arguments:Gn,status:"completed"};yield{type:"response.output_item.done",output_index:Ve,item:mo},v(Ve,mo)}let ee=bu(w,C.response);yield ee.length>0?c({...C,response:{...C.response,output:ee}}):c(C);continue}yield c(C)}if(yield*T(),yield*pe("incomplete"),yield*Z("incomplete"),i&&!Te){let C=fe(d);u.remember(r,n,or(C,Xe(d,C)),a)}}var ur="/v1/responses",xu=64*1024*1024,Eu=8*1024*1024,Au=16,Rs=32,xs=3600*1e3,Iu=/^[A-Za-z0-9._-]{1,256}$/;function se(t,e,n){return{type:"error",status:t,...n!==void 0?{stream_id:n}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var Ou=se(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 lr(){let t,e;return{promise:new Promise((r,o)=>{t=r,e=o}),resolve:t,reject:e}}async function Tu(t){for(;t.bufferedAmount>Eu&&t.readyState===Ee.WebSocket.OPEN;){let{promise:e,resolve:n}=lr();setTimeout(n,5),await e}}var ir=class extends Ke{constructor(n){super();this.shared=n}shared;thread(n,r){if(n==null)return super.thread(n,r);try{return super.thread(n,r)}catch(o){if(!(o instanceof Ne))throw o;return this.shared.thread(n,r)}}remember(n,r,o,s=!0){s?this.shared.remember(n,r,o):super.remember(n,r,o)}forget(n){super.forget(n),this.shared.forget(n)}},ar=class{constructor(e,n,r,o){this.ws=e;this.clients=n;this.stats=r;this.statusOf=o;this.store=new ir(kt(n)),e.on("message",(i,a)=>this.onMessage(i,a)),e.on("close",()=>this.dispose()),e.on("error",()=>this.dispose()),setTimeout(()=>{this.closed||(this.sendFrame(se(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${xs/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},xs).unref?.()}ws;clients;stats;statusOf;store;lanes=new Map;laneOfResponse=new Map;activeTurns=new Set;active=0;waiters=[];closed=!1;onMessage(e,n){if(this.closed)return;if(n){this.sendFrame(se(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(se(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(se(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(se(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(se(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(se(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"||!Iu.test(o.stream_id)){this.sendFrame(Ou);return}if(s=o.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=Rs){this.sendFrame(se(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${Rs}). Reuse an existing stream_id or open a new WebSocket connection.`,param:"stream_id"},s));return}}let i=o.generate===!1?()=>this.runWarmup(o,s):()=>this.runTurn(o,s),a=s??"",u=this.lanes.get(a)??Promise.resolve();this.lanes.set(a,u.then(i).catch(()=>{}))}namedLaneCount(){return this.lanes.has("")?this.lanes.size-1:this.lanes.size}async runTurn(e,n){if(!this.closed){await this.acquire();try{let r=Buffer.byteLength(JSON.stringify(e));this.stats.request("responses_ws",r,r);let o;try{o=await Rt({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:St(bt())})}catch(i){this.failTurn(i,n,e);return}this.laneOfResponse.set(o.responseId,n??null);let s=o.events;this.activeTurns.add(s);try{for await(let i of s){if(this.closed||this.ws.readyState!==Ee.WebSocket.OPEN)break;this.send(i,n),await Tu(this.ws)}}finally{this.activeTurns.delete(s)}}catch(r){this.failTurn(r,n,e)}finally{this.release()}}}runWarmup(e,n){if(this.closed)return;let r=St(bt());try{let o=Qe(this.store,{...e,input:e.input??(e.previous_response_id!=null?[]:void 0)});this.store.remember(r,o,[],e.store!==!1),this.laneOfResponse.set(r,n??null),this.send({type:"response.created",response:{id:r,object:"response",status:"in_progress"}},n)}catch(o){this.failTurn(o,n,e)}}failTurn(e,n,r){this.sendFrame(this.errorFrameOf(e,n));let o=r.previous_response_id;typeof o=="string"&&this.laneOfResponse.get(o)===(n??null)&&(this.store.forget(o),this.laneOfResponse.delete(o))}errorFrameOf(e,n){let r=String(e instanceof Error?e.message:e);if(e instanceof de)return se(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},n);if(e instanceof N)return se(404,{type:"invalid_request_error",code:"model_not_found",message:r},n);if(e instanceof q)return se(503,{type:"upstream_error",code:"session_gone",message:r},n);let o=this.statusOf?.(e)??null;return o?se(o.status,{type:o.openaiType,message:r},n):se(500,{type:"upstream_error",message:r},n)}async acquire(){for(;this.active>=Au;){let{promise:e,resolve:n}=lr();this.waiters.push(n),await e}this.active++}release(){this.active--,this.waiters.shift()?.()}dispose(){if(!this.closed){this.closed=!0;for(let e of[...this.activeTurns])e.return(void 0);this.activeTurns.clear(),this.lanes.clear(),this.laneOfResponse.clear();for(let e of this.waiters.splice(0))e()}}send(e,n){if(this.closed||this.ws.readyState!==Ee.WebSocket.OPEN)return;let r=n===void 0?e:{...e,stream_id:n};this.ws.send(JSON.stringify(r))}sendFrame(e){this.closed||this.ws.readyState!==Ee.WebSocket.OPEN||this.ws.send(JSON.stringify(e))}};function nn(t,e,n){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${n}\r
|
|
11
11
|
`;t.end(`HTTP/1.1 ${e} ${r}\r
|
|
12
12
|
Connection: close\r
|
|
13
13
|
Content-Type: text/plain; charset=utf-8\r
|
|
14
14
|
Content-Length: ${Buffer.byteLength(o)}\r
|
|
15
15
|
\r
|
|
16
|
-
${o}`)}function dr(t,e){let n=new Ee.WebSocketServer({noServer:!0,maxPayload:xu}),r=new $e,o=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,u=!1,l=(d,c,y)=>{let f;try{f=new URL(d.url??"/","http://localhost")}catch{nn(c,400,"malformed upgrade URL");return}if(c.on("error",()=>c.destroy()),c.on("close",()=>i.delete(c)),f.pathname!==ur){i.add(c);return}s.add(c);let p=!1;(async()=>{if(e.apiKey&&(d.headers.authorization??"")!==`Bearer ${e.apiKey}`){nn(c,401,"invalid local proxy api key");return}let g;try{g=await a(d)}catch(h){let _=e.statusOf?.(h)??null;nn(c,_?.status??500,"upstream resolver rejected the connection");return}u||c.destroyed||(p=!0,n.handleUpgrade(d,c,y,h=>{s.delete(c),o.add(h),h.on("close",()=>o.delete(h));try{new ar(h,g,r,e.statusOf)}catch{o.delete(h),h.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!p&&!c.destroyed&&nn(c,500,"responses websocket upgrade failed")}).finally(()=>s.delete(c))};return t.on("upgrade",l),{stats:r,close:()=>{let{promise:d,resolve:c}=lr();if(u)return c(),d;u=!0,t.off("upgrade",l);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 y=setTimeout(()=>{for(let f of o)f.readyState!==Ee.WebSocket.CLOSED&&f.terminate()},2e3);return y.unref?.(),n.close(()=>{clearTimeout(y),c()}),d}}}var Ls=require("@urun-sh/core");var Es=require("@urun-sh/core"),Pu={no_capacity:429,queue_timeout:429,no_attached_consumer:503,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502},Cu={no_capacity:"no ready GPU capacity for this model right now \u2014 the platform will cold-wake it on request; retry",queue_timeout:"the session was admitted but never became live within the cold-wake fence \u2014 the backend is busy or wedged; retry",cold_start_exceeded:"the cold boot exceeded its budget without serving \u2014 the platform keeps pursuing the wake; retry with a longer client timeout",queue_full:"the admission queue for this model is full \u2014 retry after the Retry-After period",admission_busy:"admission is momentarily contended (lock convoy) \u2014 retry shortly",admission_unavailable:"session admission is temporarily unavailable \u2014 retry",backend_error:"the control plane failed while admitting the session",no_attached_consumer:"no consumer is attached to serve this session right now \u2014 the platform is provisioning for the waiting demand; retry shortly"};function cr(t,e){let n=Cu[t.code];return e?`${n} (${e})`:n}function rn(t){if(t==null)return null;let e=Pu[t];return e===void 0?null:{status:e,code:t,retryable:e!==502,openaiType:t==="no_capacity"||t==="queue_full"?"capacity_error":t==="queue_timeout"||t==="cold_start_exceeded"||t==="no_attached_consumer"?"scheduling_error":"upstream_error",anthropicType:e===429?"rate_limit_error":e===503?"overloaded_error":"api_error"}}function on(t){if(t.kind==="create-failed")return rn(t.status.error?.code??null);if(t.kind!=="never-live")return null;let e=(t.status.elapsedMs??0)>=Es.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return rn(e?"cold_start_exceeded":"queue_timeout")}var Lu=Ht(require("os"),1),Mu=Ht(require("path"),1),$u=require("url"),Nu=require("util");function As(t,e="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(t).pathname))throw new Error(`${e} must be the session-gateway base (e.g. https://api.urun.sh), got ${t} \u2014 a trailing /v1 is the org control-plane API form (that belongs in URUN_API_URL); session allocation 404s against it. Drop the /v1.`);return t}function sn(t,e="URUN_GATEWAY_URL"){let n=new URL(t),r=n.hostname==="localhost"||n.hostname==="127.0.0.1"||n.hostname==="[::1]";if(n.protocol!=="https:"&&!r)throw new Error(`${e} must be https (got ${t}) \u2014 the org API key rides the request's Authorization header`);return t}var Uu=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function Du(t){let e=t?.code;return typeof e=="string"&&Uu.has(e)}function Ae(t,e){try{return t.write(e),!0}catch(n){if(Du(n))return!1;throw n}}var Ms=require("ws");var Cs=require("@urun-sh/core");var Os=require("crypto");function E(t){return`${t}_${(0,Os.randomUUID)().replace(/-/g,"").slice(0,24)}`}var an=4*1024*1024,pr=8*1024*1024,un=512,ln=20*1024*1024;function G(t,e,n){return{type:"invalid_request_error",code:t,message:e,param:n??null}}function Ze(t,e){return{type:"server_error",code:t,message:e}}function U(t){return{type:"error",event_id:E("event"),error:t}}function fr(){return{outputModalities:["text"],turnDetection:null,inputAudioTranscription:!1}}var I=class extends Error{constructor(n,r,o="invalid_value"){super(n);this.param=r;this.code=o;this.name="SessionUpdateError"}param;code},Fu={type:!0,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},Bu={format:!0,transcription:!0,turn_detection:!0,noise_reduction:!0},qu={format:!0,voice:!0,speed:!0};function Is(t,e){let n=t;if(!n||typeof n!="object"||n.type!=="audio/pcm"||n.rate!==void 0&&n.rate!==24e3)throw new I(`${e} must be {"type":"audio/pcm","rate":24000} \u2014 the session audio lane is 24 kHz mono PCM16 only`,e)}function xt(t,e,n){if(!t||typeof t!="object"||Array.isArray(t))throw new I("session must be an object","session","invalid_request");let r=t,o=Object.keys(r).filter(i=>!(i in Fu));if(o.length>0)throw new I(`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"type":{if(a!=="realtime"&&a!=="transcription")throw new I(`session.type ${JSON.stringify(a)} is not "realtime" or "transcription"`,"type");if(e.type!==void 0&&e.type!==a)throw new I(`session.type cannot change once established \u2014 this session is ${JSON.stringify(e.type)} and the update names ${JSON.stringify(a)}: the type is fixed at establishment (the connection's URL intent, the first session.update that names it, or the first input_audio_buffer.append, which establishes realtime)`,"type","invalid_request");if(a==="transcription"&&!n.turns)throw new I('session.type "transcription" is refused: the resolved backend wires no turn/transcript lane (the stt stream behind the engine\u2019s own transcripts) \u2014 there is no transcription source on this session',"type","unsupported_field");s.type=a;break}case"model":if(a!==null&&typeof a!="string")throw new I("model must be a string or null","model");s.model=a??void 0;break;case"instructions":if(a!==null&&typeof a!="string")throw new I("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 I("modalities must be a non-empty array","modalities");let u=[];for(let l of a){if(l!=="text"&&l!=="audio")throw new I(`modality ${JSON.stringify(l)} is not "text" or "audio"`,"modalities");if(l==="audio"&&!n.voice)throw new I('modality "audio" refused: the resolver did not provide an authoritative voice binding for the resolved backend \u2014 this model cannot speak on this session',"modalities","audio_not_supported");u.includes(l)||u.push(l)}s.outputModalities=u;break}case"voice":if(a===null){delete s.voice;break}if(typeof a!="string")throw new I("voice must be a string or null","voice");if(!n.voice)throw new I("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 I(`${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 I("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 I(`${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 I("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 I("metadata must be an object or null","metadata");s.metadata=a??null;break;case"turn_detection":{if(a===null){s.turnDetection=null;break}if(!n.turns)throw new I("turn_detection is refused: the resolved backend does not wire its own turn boundary lane (stt stream with turn_start/turn_end) \u2014 there is no native VAD source to drive responses from. Drive turns manually: input_audio_buffer.commit + response.create.","turn_detection","unsupported_field");let u=a;if(!u||typeof u!="object"||Array.isArray(u))throw new I("turn_detection must be an object or null","turn_detection");let l=String(u.type??"");if(l!=="server_vad"&&l!=="semantic_vad")throw new I(`turn_detection.type ${JSON.stringify(u.type)} is not "server_vad"/"semantic_vad"`,"turn_detection.type");s.turnDetection={createResponse:u.create_response!==!1,interruptResponse:u.interrupt_response!==!1};break}case"input_audio_transcription":{if(a===null){s.inputAudioTranscription=!1;break}if(!n.turns)throw new I("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 I("input_audio_transcription must be an object or null","input_audio_transcription");for(let l of Object.keys(a))if(!["model","language","languages","prompt"].includes(l))throw new I(`input_audio_transcription field not honored by this backend: ${l} (transcription comes from the voice engine itself)`,`input_audio_transcription.${l}`,"unsupported_field");let u=a.model;if(u!=null){let l=typeof u=="string"?u:JSON.stringify(u);if(typeof u!="string"||u!==n.model)throw new I(`input_audio_transcription.model ${JSON.stringify(l)} does not match the model this connection is bound to (${n.model===void 0?"the unnamed default app":JSON.stringify(n.model)}) \u2014 a transcription session runs the engine the connection bound and cannot be retuned to another model mid-connection`,"input_audio_transcription.model","unsupported_field")}s.inputAudioTranscription=!0;break}case"input_audio_noise_reduction":if(a!==null)throw new I("input_audio_noise_reduction must be null on this backend: no noise-reduction primitive exists behind the session audio bridge","input_audio_noise_reduction","unsupported_field");break;case"audio":{if(a===null)break;let u=a;if(!u||typeof u!="object"||Array.isArray(u))throw new I("audio must be an object or null","audio");if(u.input!==void 0&&u.input!==null){let l=u.input;if(!l||typeof l!="object"||Array.isArray(l))throw new I("audio.input must be an object","audio.input");for(let d of Object.keys(l))if(!(d in Bu))throw new I(`audio.input field not supported by this backend: ${d}`,`audio.input.${d}`,"unsupported_field");if(l.format!==void 0&&Is(l.format,"audio.input.format"),l.transcription!==null&&l.transcription!==void 0){let d=xt({input_audio_transcription:l.transcription},s,n);s.inputAudioTranscription=d.inputAudioTranscription}if(l.turn_detection!==null&&l.turn_detection!==void 0){let d=xt({turn_detection:l.turn_detection},s,n);s.turnDetection=d.turnDetection}if(l.noise_reduction!==null&&l.noise_reduction!==void 0)throw new I("audio.input.noise_reduction must be null on this backend: no noise-reduction primitive","audio.input.noise_reduction","unsupported_field")}if(u.output!==void 0&&u.output!==null){let l=u.output;if(!l||typeof l!="object"||Array.isArray(l))throw new I("audio.output must be an object","audio.output");for(let d of Object.keys(l))if(!(d in qu))throw new I(`audio.output field not supported by this backend: ${d}`,`audio.output.${d}`,"unsupported_field");if(l.format!==void 0&&Is(l.format,"audio.output.format"),l.speed!==void 0)throw new I("audio.output.speed is not supported on this backend: playback rate is fixed","audio.output.speed","unsupported_field");if(l.voice!==void 0)if(l.voice===null)delete s.voice;else{if(typeof l.voice!="string")throw new I("audio.output.voice must be a string or null","audio.output.voice");if(!n.voice)throw new I("audio.output.voice refused: no authoritative voice binding for the resolved backend","audio.output.voice","audio_not_supported");s.voice=l.voice}l.format!==void 0&&!s.outputModalities.includes("audio")&&(s.outputModalities=["text","audio"])}break}default:break}return s}function dn(t,e){return{id:e,object:"realtime.session",type:t.type??"realtime",model:t.model??null,instructions:t.instructions??"",modalities:[...t.outputModalities],voice:t.voice??null,input_audio_format:"pcm16",output_audio_format:"pcm16",input_audio_transcription:t.inputAudioTranscription?{}:null,input_audio_noise_reduction:null,turn_detection:t.turnDetection?{type:"server_vad",create_response:t.turnDetection.createResponse,interrupt_response:t.turnDetection.interruptResponse}:null,temperature:t.temperature??null,max_response_output_tokens:t.maxOutputTokens??"inf",tool_choice:t.toolChoice??"auto",tools:t.tools??[],metadata:t.metadata??null}}function mr(t,e){let n=t;if(!n||typeof n!="object")throw new I("item must be an object","item","invalid_request");if(n.type==="function_call_output"){if(typeof n.call_id!="string"||typeof n.output!="string")throw new I("function_call_output item requires string call_id and output","item.call_id","invalid_request");return{id:typeof n.id=="string"?n.id:E("item"),type:"function_call_output",callId:n.call_id,output:n.output}}if(n.type==="message"){let r=n.role;if(r!=="user"&&r!=="assistant"&&r!=="system")throw new I('message item role must be "user", "assistant" or "system"',"item.role","invalid_request");if(!Array.isArray(n.content))throw new I("message item content must be an array","item.content","invalid_request");let o=[];for(let s of n.content){let i=s;if(!i||typeof i!="object")throw new I("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 I(`${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 I("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 I("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 I(`content part type ${String(i.type)} is not supported on message items here (input_text / output_text / input_audio)`,"item.content.type","invalid_request")}return{id:typeof n.id=="string"?n.id:E("item"),type:"message",role:r,content:o}}throw new I(`item type ${String(n.type)} is not supported \u2014 message and function_call_output items only`,"item.type","invalid_request")}function hr(t){let e=[],n=!1;for(let r of t){if(r.type==="function_call"){e.push({type:"function_call",call_id:r.callId,name:r.name,arguments:r.arguments??"{}"});continue}if(r.type==="function_call_output"){e.push({type:"function_call_output",call_id:r.callId,output:r.output});continue}let o=[];for(let s of r.content??[]){if(s.type==="input_text"||s.type==="output_text"){o.push({type:s.type,text:s.text});continue}if(s.type==="input_audio"){let i=s.transcript;if(typeof i=="string"&&i.trim().length>0){o.push({type:"input_text",text:i});continue}n=!0;continue}}e.push({role:r.role,content:o})}return{input:e,sawAudioOnlyItem:n}}var Hu=1001,ju=1011,Ts=t=>t.slice(0,120),Gu=/^[A-Za-z0-9+/=]*$/,Vu=2;function Ju(t){let e=Buffer.from(t,"base64");for(let n=0;n+1<e.byteLength;n+=2)if(Math.abs(e.readInt16LE(n))>Vu)return!1;return!0}var Et=class{id=E("conn");ws;binding;onClosed;config=fr();sessionId=E("sess");conversationId=E("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;turnLaneIterator=null;turnTranscript={open:!1,text:"",final:!1};lastCommittedTranscript=null;pendingTranscriptItemId=null;manuallyDischargedTurnItem=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.binding.intent==="transcription"&&(this.config.type="transcription"),this.ws.on("message",n=>{this.onMessage(n)}),this.ws.on("close",()=>this.teardown("client disconnected")),this.ws.on("error",()=>this.teardown("client socket error")),this.send({type:"session.created",event_id:E("event"),session:dn(this.config,this.sessionId)}),this.send({type:"conversation.created",event_id:E("event"),conversation:{id:this.conversationId,object:"realtime.conversation"}}),this.binding.turns&&this.startTurnLane()}startTurnLane(){let e=new AbortController;this.turnLaneAbort=e;let n=this.binding.turns(e.signal)[Symbol.asyncIterator]();this.turnLaneIterator=n,(async()=>{try{for(;;){let r=await n.next();if(r.done||this.closed||e.signal.aborted)return;this.onTurnEvent(r.value)}}catch(r){!this.closed&&!e.signal.aborted&&this.send(U(Ze("turn_lane_fault",`the backend turn/transcript lane failed: ${r instanceof Error?r.message:String(r)}`)))}finally{this.turnLaneIterator===n&&(this.turnLaneIterator=null)}})()}isTranscriptionSession(){return this.binding.intent==="transcription"||this.config.type==="transcription"}transcriptionEventsOn(){return this.config.inputAudioTranscription||this.isTranscriptionSession()}completePendingTranscription(e){this.pendingTranscriptItemId=null;let n=this.turnTranscript.text.trim();this.turnTranscript={open:!1,text:"",final:!1},this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:e,content_index:0,transcript:n})}onTurnEvent(e){switch(e.kind){case"delta":case"final":{let n=typeof e.text=="string"?e.text:"",r=this.pendingTranscriptItemId;this.turnTranscript.open=!0,n.length>0&&(this.turnTranscript.text+=n),this.turnTranscript.final=e.kind==="final",this.transcriptionEventsOn()&&n.length>0&&this.send({type:"conversation.item.input_audio_transcription.delta",event_id:E("event"),item_id:r??"input_audio_turn",content_index:0,delta:n}),e.kind==="final"&&r!==null&&(this.completePendingTranscription(r),this.manuallyDischargedTurnItem=r);return}case"turn_start":{this.manuallyDischargedTurnItem=null,this.turnTranscript={open:!0,text:this.turnTranscript.text,final:!1},this.send({type:"input_audio_buffer.speech_started",event_id:E("event"),item_id:"input_audio_turn",audio_start_ms:typeof e.startMs=="number"?e.startMs:0}),this.config.turnDetection?.interruptResponse&&this.inFlight&&this.cancelResponse();return}case"turn_end":{let n=this.pendingTranscriptItemId;if(n!==null){this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:n,audio_end_ms:typeof e.startMs=="number"?e.startMs:0}),this.completePendingTranscription(n);return}let r=this.manuallyDischargedTurnItem;if(r!==null){this.manuallyDischargedTurnItem=null,this.turnTranscript={open:!1,text:"",final:!1},this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:r,audio_end_ms:typeof e.startMs=="number"?e.startMs:0});return}let o=this.turnTranscript.text.trim(),s=o.length>0?o:null;if(this.turnTranscript={open:!1,text:"",final:!1},this.lastCommittedTranscript=s,this.isTranscriptionSession()&&this.config.turnDetection===null){this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:"input_audio_turn",audio_end_ms:typeof e.startMs=="number"?e.startMs:0});return}this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let i=E("item"),a=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem({id:i,type:"message",role:"user",content:[{type:"input_audio",...s!==null?{transcript:s}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:E("event"),previous_item_id:a,item_id:i}),this.send({type:"conversation.item.created",event_id:E("event"),previous_item_id:a,item:this.itemToProtocol(this.items[this.items.length-1])}),this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:i,audio_end_ms:typeof e.startMs=="number"?e.startMs:0}),this.transcriptionEventsOn()&&s!==null&&this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:i,content_index:0,transcript:s}),this.config.turnDetection?.createResponse&&!this.inFlight&&!this.isTranscriptionSession()&&this.startResponse({});return}case"turn_eager_end":case"turn_resumed":return;default:return}}takeTurnTranscript(){let e=this.lastCommittedTranscript;return this.lastCommittedTranscript=null,e}async onMessage(e){if(this.closed)return;let n;try{n=e.toString()}catch{this.fail(G("decode_error","frame is not decodable text"),"invalid_frame");return}if(Buffer.byteLength(n,"utf8")>an){this.fail(G("frame_too_large",`frame exceeds the ${an}-byte limit`),"invalid_frame");return}let r;try{r=JSON.parse(n)}catch{this.fail(G("invalid_json","frame is not valid JSON"),"invalid_frame");return}if(typeof r.type!="string"||r.type.length===0){this.fail(G("missing_type",'event is missing a string "type"'),"invalid_frame");return}try{await this.handle(r)}catch(o){let s=o;if(s&&typeof s=="object"&&s.name==="SessionUpdateError"){this.send(U({type:"invalid_request_error",code:s.code??"invalid_value",message:s.message,param:s.param??null}));return}this.fail(Ze(null,o instanceof Error?o.message:String(o)),"handler_fault")}}async handle(e){switch(e.type){case"session.update":this.config=xt(e.session,this.config,{voice:this.binding.voice,turns:this.binding.turns!==void 0,model:this.binding.model}),this.send({type:"session.updated",event_id:E("event"),session:dn(this.config,this.sessionId)});return;case"conversation.item.create":{let n=mr(e.item,this.binding.voice),r=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem(n),this.send({type:"conversation.item.created",event_id:E("event"),previous_item_id:r,item:this.itemToProtocol(n)});return}case"conversation.item.delete":{let n=this.items.findIndex(r=>r.id===e.item_id);if(n<0){this.send(U(G("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.items.splice(n,1),this.send({type:"conversation.item.deleted",event_id:E("event"),item_id:e.item_id});return}case"conversation.item.retrieve":{let n=this.items.find(r=>r.id===e.item_id);if(!n){this.send(U(G("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.send({type:"conversation.item.retrieved",event_id:E("event"),item:this.itemToProtocol(n)});return}case"conversation.item.truncate":{let n=this.items.find(r=>r.id===e.item_id);if(!n||n.type!=="message"||n.role!=="assistant"){this.send(U(G("item_not_found",`no assistant message item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}n.truncated=!0,this.send({type:"conversation.item.truncated",event_id:E("event"),item_id:n.id,content_index:typeof e.content_index=="number"?e.content_index:0,audio_end_ms:typeof e.audio_end_ms=="number"?e.audio_end_ms:0});return}case"input_audio_buffer.append":{if(typeof e.audio!="string"||e.audio.length===0){this.send(U(G("invalid_audio",'input_audio_buffer.append requires base64 "audio"',"audio")));return}if(!this.ensureVoice())return;if(!Gu.test(e.audio)){this.send(U(G("invalid_audio","input_audio_buffer.append audio must be base64","audio")));return}let n=Buffer.byteLength(e.audio,"base64");if(this.pendingAudioTotalBytes+n>ln){this.send(U(G("buffer_full",`uncommitted input audio exceeds the ${ln}-byte bound \u2014 commit or clear first`,"audio")));return}if(this.config.type===void 0&&(this.config.type="realtime"),this.inFlight){this.binding.audioSession.audio.appendInputAudio(e.audio);return}if(this.isTranscriptionSession()){this.binding.audioSession.audio.appendInputAudio(e.audio),this.pendingAudioTotalBytes+=n;return}this.pendingAudioBytes.push(e.audio),this.pendingAudioTotalBytes+=n;return}case"input_audio_buffer.clear":{if(!this.ensureVoice())return;this.pendingAudioBytes.length=0,this.pendingAudioTotalBytes=0,this.lastCommittedTranscript=null,this.pendingTranscriptItemId===null&&(this.turnTranscript={open:!1,text:"",final:!1}),this.send({type:"input_audio_buffer.cleared",event_id:E("event")});return}case"input_audio_buffer.commit":{if(!this.ensureVoice())return;if(this.pendingAudioTotalBytes===0&&this.takeTurnTranscript()===null){this.send(U(G("empty_buffer","input_audio_buffer.commit with no buffered audio","audio")));return}this.isTranscriptionSession()&&this.pendingTranscriptItemId!==null&&this.completePendingTranscription(this.pendingTranscriptItemId),this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let n=this.takeTurnTranscript();n===null&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let r=E("item"),o=this.items.length>0?this.items[this.items.length-1].id:null;if(this.pushItem({id:r,type:"message",role:"user",content:[{type:"input_audio",...n!==null?{transcript:n}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:E("event"),previous_item_id:o,item_id:r}),this.send({type:"conversation.item.created",event_id:E("event"),previous_item_id:o,item:this.itemToProtocol(this.items[this.items.length-1])}),this.isTranscriptionSession())if(n!==null)this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:r,content_index:0,transcript:n});else if(this.turnTranscript.final){let s=this.turnTranscript.text.trim();this.turnTranscript={open:!1,text:"",final:!1},this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:r,content_index:0,transcript:s}),this.manuallyDischargedTurnItem=r}else this.pendingTranscriptItemId=r;return}case"output_audio_buffer.clear":{this.send(U(G("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":if(this.isTranscriptionSession()){this.send(U(G("unsupported","response.create is not supported on a transcription session \u2014 this session transcribes the streamed audio with the engine's own transcript lane and emits conversation.item.input_audio_transcription events; there are no model responses to create","response.create")));return}this.startResponse(e.response??{});return;case"response.cancel":this.cancelResponse();return;default:this.send(U(G("unknown_event",`event type ${e.type} is not supported by this adapter`)))}}ensureVoice(){return!this.binding.voice||!this.binding.audioSession?(this.send(U(G("audio_not_supported","audio events require a voice binding from the resolver \u2014 this session was resolved text-only","audio"))),!1):(this.voiceClaimed||(this.claimVoiceBinding(),this.audioUnsubscribe=this.binding.audioSession.audio.onOutputAudio(e=>{this.onOutputAudioFrame(this.activeResponseId??"",e)}),this.voiceClaimed=!0),!0)}claimVoiceBinding(){let e=this.binding.audioSession,n=e.openaiRealtimeClaims??(e.openaiRealtimeClaims=new Set);if(n.has(e))throw new Error("voice binding already owned by another connection \u2014 the resolver handed one audio bridge to two realtime conversations, which must never happen (independent callers require private bindings)");n.add(e),this.audioLaneOwner=!0}releaseVoiceBinding(){let e=this.binding.audioSession;!e||!this.audioLaneOwner||(e.openaiRealtimeClaims?.delete(e),this.audioLaneOwner=!1)}warnAudioOnlyTranscriptGap(){this.audioOnlyTranscriptGapWarned||(this.audioOnlyTranscriptGapWarned=!0,this.send(U({type:"invalid_request_error",code:"audio_input_transcript_unavailable",message:"committed input audio is forwarded to the runtime audio lane but is NOT model-visible in the text turn lane: the resolved backend wires no turn/transcript lane (the voice engine's own stt stream with turn_start/turn_end + transcripts). Wire the transcribe/turn-detection leg upstream (the resolver supplies binding.turns) and committed audio gains its REAL transcript as the model-visible turn.",param:"input_audio_buffer.commit"})))}async upstreamStream(e,n){let r=this.binding.session;if(r&&typeof r.sendResponseCreate=="function"){let s=e.response??e;return r.sendResponseCreate({type:"response.create",response:s},e.input??[],n)}let o=this.binding.clients;if(o&&typeof o.createResponse=="function"){let{response:s,input:i,...a}=e;return await o.createResponse({...a,input:i})}throw new Error("realtime binding provides neither session.sendResponseCreate nor clients.createResponse \u2014 no upstream turn seam")}startResponse(e){if(this.inFlight){this.send(U(G("response_in_progress","a response is already in progress \u2014 cancel it with response.cancel first","response.create")));return}let n=e??{};if(n.modalities!==void 0){if(!Array.isArray(n.modalities)||n.modalities.some(g=>g!=="text"&&g!=="audio")){this.send(U(G("invalid_modalities",'response.modalities must be "text"/"audio"',"response.modalities")));return}if(n.modalities.includes("audio")&&!this.ensureVoice())return}if(n.input!==void 0){this.send(U(G("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=E("req"),o=`resp_${r}`;this.activeResponseId=o;let s=this.items.length,{input:i,sawAudioOnlyItem:a}=hr(this.items),u=typeof n.instructions=="string"?n.instructions:this.config.instructions,l=typeof n.temperature=="number"?n.temperature:this.config.temperature,d=n.max_output_tokens??this.config.maxOutputTokens,c=d==="inf"||d===void 0?void 0:d,y=n.tools!==void 0?n.tools:this.config.tools,f=n.tool_choice!==void 0?n.tool_choice:this.config.toolChoice,p={input:i,stream:!0,...this.config.model?{model:this.config.model}:{},...u!==void 0?{instructions:u}:{},...l!==void 0?{temperature:l}:{},...c!==void 0?{max_output_tokens:c}:{},...y!==void 0?{tools:y}:{},...f!==void 0?{tool_choice:f}:{}};for(let g of this.frozenAudio.splice(0))this.binding.audioSession?.audio.appendInputAudio(g);this.inFlight=(async()=>{let g=[],h=R=>{this.responseFlushed?this.send(R):g.push(R)},_=()=>{if(!this.responseFlushed){this.send({type:"response.created",event_id:E("event"),response:this.responseObject(o,"in_progress")}),this.send({type:"response.in_progress",event_id:E("event"),response:this.responseObject(o,"in_progress")});for(let R of g)this.send(R);g.length=0,this.responseFlushed=!0}},v={messageOpen:!1,messageText:"",tools:new Map,nextOutputIndex:0},m=null;try{a&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let R;try{R=await this.upstreamStream(p,r)}catch(b){this.upstreamTurnFault(b,o);return}try{for await(let b of R){if(this.cancelled)break;let w=b;if(w.type==="error"){let S=this.upstreamErrorBody(w);this.responseFlushed?this.send(U(S)):this.send({type:"response.failed",event_id:E("event"),response:this.failedResponse(o,S)});return}if(w.type==="response.output_text.delta"&&typeof w.delta=="string"){_(),v.messageOpen||(this.beginMessageItem(o,v.nextOutputIndex++),v.messageOpen=!0),h({type:"response.output_text.delta",event_id:E("event"),item_id:`${o}_msg_0`,response_id:o,output_index:0,content_index:0,delta:w.delta}),v.messageText+=w.delta;continue}if(w.type==="response.function_call_arguments.delta"){_();let S=typeof w.delta=="string"?w.delta:"",T=typeof w.call_id=="string"&&w.call_id.length>0?w.call_id:`${o}_fc_${v.nextOutputIndex}`,x=v.tools.get(T);if(!x){let O=typeof w.name=="string"&&w.name.length>0?w.name:void 0;x={outputIndex:v.nextOutputIndex++,name:O,args:""},v.tools.set(T,x),h({type:"response.output_item.added",event_id:E("event"),response_id:o,output_index:x.outputIndex,item:{id:T,object:"realtime.item",type:"function_call",call_id:T,name:O??"",arguments:"",status:"in_progress"}})}typeof w.name=="string"&&w.name.length>0&&(x.name=w.name),x.args+=S,h({type:"response.function_call_arguments.delta",event_id:E("event"),item_id:T,response_id:o,output_index:x.outputIndex,call_id:T,name:x.name??"",delta:S});continue}if(w.type==="response.completed"){m=w.response??null;continue}}}catch(b){this.upstreamTurnFault(b,o);return}if(this.cancelled)return;this.closeOpenItems(o,v,s),_(),this.send({type:"response.done",event_id:E("event"),response:this.doneResponse(o,s,m)})}finally{this.inFlight=null,this.activeResponseId=null}})()}onOutputAudioFrame(e,n){if(this.cancelled)return;if(this.activeResponseId!==e){if(Ju(n))return;!this.outOfTurnAudioWarned&&!this.closed&&(this.outOfTurnAudioWarned=!0,this.send(U({type:"invalid_request_error",code:"out_of_turn_audio",message:"the runtime produced voice OUT outside a response turn; this adapter delivers output audio only within response.output_audio.delta of an open turn \u2014 wire server-driven turns (VAD) upstream or keep speech inside response turns"})));return}this.responseFlushed||(this.send({type:"response.created",event_id:E("event"),response:this.responseObject(e,"in_progress")}),this.send({type:"response.in_progress",event_id:E("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:E("event"),item_id:r,response_id:e,output_index:0,content_index:0,delta:n})}messageItemAdded=!1;messageItemOpenForAudio=!1;beginMessageItem(e,n){let r=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:E("event"),response_id:e,output_index:n,item:{id:r,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:E("event"),item_id:r,response_id:e,output_index:n,content_index:0,part:{type:"output_text",text:""}})}beginAudioMessageItem(e){let n=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:E("event"),response_id:e,output_index:0,item:{id:n,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:E("event"),item_id:n,response_id:e,output_index:0,content_index:0,part:{type:"output_audio",transcript:""}})}closeOpenItems(e,n,r){if(n.messageOpen||this.messageItemOpenForAudio){let o=`${e}_msg_0`,s=this.messageItemOpenForAudio;s&&this.send({type:"response.output_audio.done",event_id:E("event"),item_id:o,response_id:e,output_index:0,content_index:0}),n.messageText.length>0&&this.send({type:"response.output_text.done",event_id:E("event"),item_id:o,response_id:e,output_index:0,content_index:0,text:n.messageText}),this.send({type:"response.content_part.done",event_id:E("event"),item_id:o,response_id:e,output_index:0,content_index:0,part:s?{type:"output_audio",transcript:""}:{type:"output_text",text:n.messageText}});let i=[];s&&i.push({type:"output_audio"}),n.messageText.length>0&&i.push({type:"output_text",text:n.messageText});let a={id:o,type:"message",role:"assistant",content:i};this.pushItem(a),this.send({type:"response.output_item.done",event_id:E("event"),response_id:e,output_index:0,item:this.itemToProtocol(a)})}for(let[o,s]of n.tools){let i=s.args.length>0?s.args:"{}";this.send({type:"response.function_call_arguments.done",event_id:E("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:E("event"),response_id:e,output_index:s.outputIndex,item:this.itemToProtocol(a)})}this.messageItemOpenForAudio=!1,this.messageItemAdded=!1}responseObject(e,n,r){return{id:e,object:"realtime.response",status:n,conversation_id:this.conversationId,usage:null,...r??{}}}failedResponse(e,n){return this.responseObject(e,"failed",{status_details:{error:{code:n.code,message:n.message}}})}doneResponse(e,n,r){let o=r&&typeof r.usage=="object"&&r.usage!==null?r.usage:null,s=[];for(let i=n;i<this.items.length;i++)s.push(this.itemToProtocol(this.items[i]));return this.responseObject(e,"completed",{output:s,usage:o?{input_tokens:typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0,output_tokens:typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0,total_tokens:typeof o.total_tokens=="number"&&Number.isFinite(o.total_tokens)?o.total_tokens:(typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0)+(typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0),input_token_details:{cached_tokens:0,audio_tokens:0,text_tokens:0},output_token_details:{audio_tokens:0,text_tokens:0}}:null})}upstreamErrorBody(e){let n=e.error,r=n&&typeof n.message=="string"?n.message:JSON.stringify(e);return{type:"server_error",code:n&&typeof n.code=="string"?n.code:null,message:r}}failSessionGone(e){let n=e instanceof Error?e.message:String(e);this.fail(Ze("session_gone",`this realtime connection's pooled session \u2014 the one its audio bridge and every turn ride \u2014 is gone: ${n}`),"session_gone")}upstreamTurnFault(e,n){if(e instanceof q){this.failSessionGone(e);return}if(!this.cancelled){if(!this.responseFlushed&&!(e instanceof Cs.SessionFailedError&&(e.kind==="never-live"||e.kind==="create-failed"))){this.failSessionGone(e);return}this.emitUpstreamFault(e,n)}}emitUpstreamFault(e,n){let r=Ze(null,e instanceof Error?e.message:String(e));this.responseFlushed?this.send(U(r)):this.send({type:"response.failed",event_id:E("event"),response:this.failedResponse(n,r)})}cancelResponse(){let e=this.inFlight;if(!e){this.send(U(G("no_response_to_cancel","no response is in progress","response.cancel")));return}this.cancelled=!0;let n=this.activeResponseId??"resp_";e.finally(()=>{this.send({type:"response.cancelled",event_id:E("event"),response:this.responseObject(n,"cancelled")})})}pushItem(e){this.items.push(e),this.items.length>un&&(this.items.shift(),this.send(U({type:"invalid_request_error",code:"transcript_evicted",message:`the conversation transcript exceeded ${un} items \u2014 the oldest item was evicted to keep the per-connection bound`})))}itemToProtocol(e){let n={id:e.id,object:"realtime.item"};if(e.type==="function_call")return{...n,type:"function_call",call_id:e.callId,name:e.name,arguments:e.arguments??"{}",status:"completed"};if(e.type==="function_call_output")return{...n,type:"function_call_output",call_id:e.callId,output:e.output};let r=(e.content??[]).map(o=>o.type==="input_text"||o.type==="output_text"?{type:o.type,text:o.text}:o.type==="input_audio"?{type:"input_audio",...o.transcript?{transcript:o.transcript}:{}}:{type:"output_audio",transcript:o.transcript??""});return{...n,type:"message",role:e.role,status:e.truncated?"incomplete":"completed",content:r}}send(e){if(!(this.closed||this.ws.readyState!==this.ws.OPEN)){if(this.ws.bufferedAmount>pr){this.ws.close(Hu,Ts("outbound buffer exceeded")),this.teardown("outbound buffer exceeded");return}this.ws.send(JSON.stringify(e))}}fail(e,n){this.send(U(e)),this.ws.close(ju,Ts(n)),this.teardown(n)}teardown(e){if(this.closed)return;this.closed=!0,this.turnLaneAbort?.abort(new Error("the realtime connection closed \u2014 the turn lane consumer is gone")),this.turnLaneAbort=null;let n=this.turnLaneIterator;if(this.turnLaneIterator=null,n!==null){let r=n.return?.();r&&r.catch(()=>{})}this.releaseVoiceBinding(),this.audioUnsubscribe?.(),this.audioUnsubscribe=null,this.binding.release&&Promise.resolve().then(()=>this.binding.release?.()).catch(r=>Ps(this.id,`binding release failed: ${r instanceof Error?r.message:String(r)}`));try{this.ws.removeAllListeners("message")}catch{}this.onClosed?.(this,{reason:e}),Ps(this.id,e)}},gr=[];function Ps(t,e){gr.push({id:t,reason:e}),gr.length>256&&gr.shift()}var At="/v1/realtime",Wu=t=>t.slice(0,120);function Ku(t,e,n){let r=e===401||e===403?"Unauthorized":e===502?"Upstream Fault":"Upgrade Refused";t.write(`HTTP/1.1 ${e} ${r}\r
|
|
16
|
+
${o}`)}function dr(t,e){let n=new Ee.WebSocketServer({noServer:!0,maxPayload:xu}),r=new $e,o=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,u=!1,l=(d,c,y)=>{let f;try{f=new URL(d.url??"/","http://localhost")}catch{nn(c,400,"malformed upgrade URL");return}if(c.on("error",()=>c.destroy()),c.on("close",()=>i.delete(c)),f.pathname!==ur){i.add(c);return}s.add(c);let p=!1;(async()=>{if(e.apiKey&&(d.headers.authorization??"")!==`Bearer ${e.apiKey}`){nn(c,401,"invalid local proxy api key");return}let g;try{g=await a(d)}catch(h){let w=e.statusOf?.(h)??null;nn(c,w?.status??500,"upstream resolver rejected the connection");return}u||c.destroyed||(p=!0,n.handleUpgrade(d,c,y,h=>{s.delete(c),o.add(h),h.on("close",()=>o.delete(h));try{new ar(h,g,r,e.statusOf)}catch{o.delete(h),h.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!p&&!c.destroyed&&nn(c,500,"responses websocket upgrade failed")}).finally(()=>s.delete(c))};return t.on("upgrade",l),{stats:r,close:()=>{let{promise:d,resolve:c}=lr();if(u)return c(),d;u=!0,t.off("upgrade",l);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 y=setTimeout(()=>{for(let f of o)f.readyState!==Ee.WebSocket.CLOSED&&f.terminate()},2e3);return y.unref?.(),n.close(()=>{clearTimeout(y),c()}),d}}}var Ls=require("@urun-sh/core");var Es=require("@urun-sh/core"),Pu={no_capacity:429,queue_timeout:429,no_attached_consumer:503,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502},Cu={no_capacity:"no ready GPU capacity for this model right now \u2014 the platform will cold-wake it on request; retry",queue_timeout:"the session was admitted but never became live within the cold-wake fence \u2014 the backend is busy or wedged; retry",cold_start_exceeded:"the cold boot exceeded its budget without serving \u2014 the platform keeps pursuing the wake; retry with a longer client timeout",queue_full:"the admission queue for this model is full \u2014 retry after the Retry-After period",admission_busy:"admission is momentarily contended (lock convoy) \u2014 retry shortly",admission_unavailable:"session admission is temporarily unavailable \u2014 retry",backend_error:"the control plane failed while admitting the session",no_attached_consumer:"no consumer is attached to serve this session right now \u2014 the platform is provisioning for the waiting demand; retry shortly"};function cr(t,e){let n=Cu[t.code];return e?`${n} (${e})`:n}function rn(t){if(t==null)return null;let e=Pu[t];return e===void 0?null:{status:e,code:t,retryable:e!==502,openaiType:t==="no_capacity"||t==="queue_full"?"capacity_error":t==="queue_timeout"||t==="cold_start_exceeded"||t==="no_attached_consumer"?"scheduling_error":"upstream_error",anthropicType:e===429?"rate_limit_error":e===503?"overloaded_error":"api_error"}}function on(t){if(t.kind==="create-failed")return rn(t.status.error?.code??null);if(t.kind!=="never-live")return null;let e=(t.status.elapsedMs??0)>=Es.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return rn(e?"cold_start_exceeded":"queue_timeout")}var Lu=Ht(require("os"),1),Mu=Ht(require("path"),1),$u=require("url"),Nu=require("util");function As(t,e="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(t).pathname))throw new Error(`${e} must be the session-gateway base (e.g. https://api.urun.sh), got ${t} \u2014 a trailing /v1 is the org control-plane API form (that belongs in URUN_API_URL); session allocation 404s against it. Drop the /v1.`);return t}function sn(t,e="URUN_GATEWAY_URL"){let n=new URL(t),r=n.hostname==="localhost"||n.hostname==="127.0.0.1"||n.hostname==="[::1]";if(n.protocol!=="https:"&&!r)throw new Error(`${e} must be https (got ${t}) \u2014 the org API key rides the request's Authorization header`);return t}var Uu=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function Du(t){let e=t?.code;return typeof e=="string"&&Uu.has(e)}function Ae(t,e){try{return t.write(e),!0}catch(n){if(Du(n))return!1;throw n}}var Ms=require("ws");var Cs=require("@urun-sh/core");var Os=require("crypto");function E(t){return`${t}_${(0,Os.randomUUID)().replace(/-/g,"").slice(0,24)}`}var an=4*1024*1024,pr=8*1024*1024,un=512,ln=20*1024*1024;function G(t,e,n){return{type:"invalid_request_error",code:t,message:e,param:n??null}}function Ze(t,e){return{type:"server_error",code:t,message:e}}function U(t){return{type:"error",event_id:E("event"),error:t}}function fr(){return{outputModalities:["text"],turnDetection:null,inputAudioTranscription:!1}}var I=class extends Error{constructor(n,r,o="invalid_value"){super(n);this.param=r;this.code=o;this.name="SessionUpdateError"}param;code},Fu={type:!0,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},Bu={format:!0,transcription:!0,turn_detection:!0,noise_reduction:!0},qu={format:!0,voice:!0,speed:!0};function Is(t,e){let n=t;if(!n||typeof n!="object"||n.type!=="audio/pcm"||n.rate!==void 0&&n.rate!==24e3)throw new I(`${e} must be {"type":"audio/pcm","rate":24000} \u2014 the session audio lane is 24 kHz mono PCM16 only`,e)}function xt(t,e,n){if(!t||typeof t!="object"||Array.isArray(t))throw new I("session must be an object","session","invalid_request");let r=t,o=Object.keys(r).filter(i=>!(i in Fu));if(o.length>0)throw new I(`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"type":{if(a!=="realtime"&&a!=="transcription")throw new I(`session.type ${JSON.stringify(a)} is not "realtime" or "transcription"`,"type");if(e.type!==void 0&&e.type!==a)throw new I(`session.type cannot change once established \u2014 this session is ${JSON.stringify(e.type)} and the update names ${JSON.stringify(a)}: the type is fixed at establishment (the connection's URL intent, the first session.update that names it, or the first input_audio_buffer.append, which establishes realtime)`,"type","invalid_request");if(a==="transcription"&&!n.turns)throw new I('session.type "transcription" is refused: the resolved backend wires no turn/transcript lane (the stt stream behind the engine\u2019s own transcripts) \u2014 there is no transcription source on this session',"type","unsupported_field");s.type=a;break}case"model":if(a!==null&&typeof a!="string")throw new I("model must be a string or null","model");s.model=a??void 0;break;case"instructions":if(a!==null&&typeof a!="string")throw new I("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 I("modalities must be a non-empty array","modalities");let u=[];for(let l of a){if(l!=="text"&&l!=="audio")throw new I(`modality ${JSON.stringify(l)} is not "text" or "audio"`,"modalities");if(l==="audio"&&!n.voice)throw new I('modality "audio" refused: the resolver did not provide an authoritative voice binding for the resolved backend \u2014 this model cannot speak on this session',"modalities","audio_not_supported");u.includes(l)||u.push(l)}s.outputModalities=u;break}case"voice":if(a===null){delete s.voice;break}if(typeof a!="string")throw new I("voice must be a string or null","voice");if(!n.voice)throw new I("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 I(`${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 I("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 I(`${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 I("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 I("metadata must be an object or null","metadata");s.metadata=a??null;break;case"turn_detection":{if(a===null){s.turnDetection=null;break}if(!n.turns)throw new I("turn_detection is refused: the resolved backend does not wire its own turn boundary lane (stt stream with turn_start/turn_end) \u2014 there is no native VAD source to drive responses from. Drive turns manually: input_audio_buffer.commit + response.create.","turn_detection","unsupported_field");let u=a;if(!u||typeof u!="object"||Array.isArray(u))throw new I("turn_detection must be an object or null","turn_detection");let l=String(u.type??"");if(l!=="server_vad"&&l!=="semantic_vad")throw new I(`turn_detection.type ${JSON.stringify(u.type)} is not "server_vad"/"semantic_vad"`,"turn_detection.type");s.turnDetection={createResponse:u.create_response!==!1,interruptResponse:u.interrupt_response!==!1};break}case"input_audio_transcription":{if(a===null){s.inputAudioTranscription=!1;break}if(!n.turns)throw new I("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 I("input_audio_transcription must be an object or null","input_audio_transcription");for(let l of Object.keys(a))if(!["model","language","languages","prompt"].includes(l))throw new I(`input_audio_transcription field not honored by this backend: ${l} (transcription comes from the voice engine itself)`,`input_audio_transcription.${l}`,"unsupported_field");let u=a.model;if(u!=null){let l=typeof u=="string"?u:JSON.stringify(u);if(typeof u!="string"||u!==n.model)throw new I(`input_audio_transcription.model ${JSON.stringify(l)} does not match the model this connection is bound to (${n.model===void 0?"the unnamed default app":JSON.stringify(n.model)}) \u2014 a transcription session runs the engine the connection bound and cannot be retuned to another model mid-connection`,"input_audio_transcription.model","unsupported_field")}s.inputAudioTranscription=!0;break}case"input_audio_noise_reduction":if(a!==null)throw new I("input_audio_noise_reduction must be null on this backend: no noise-reduction primitive exists behind the session audio bridge","input_audio_noise_reduction","unsupported_field");break;case"audio":{if(a===null)break;let u=a;if(!u||typeof u!="object"||Array.isArray(u))throw new I("audio must be an object or null","audio");if(u.input!==void 0&&u.input!==null){let l=u.input;if(!l||typeof l!="object"||Array.isArray(l))throw new I("audio.input must be an object","audio.input");for(let d of Object.keys(l))if(!(d in Bu))throw new I(`audio.input field not supported by this backend: ${d}`,`audio.input.${d}`,"unsupported_field");if(l.format!==void 0&&Is(l.format,"audio.input.format"),l.transcription!==null&&l.transcription!==void 0){let d=xt({input_audio_transcription:l.transcription},s,n);s.inputAudioTranscription=d.inputAudioTranscription}if(l.turn_detection!==null&&l.turn_detection!==void 0){let d=xt({turn_detection:l.turn_detection},s,n);s.turnDetection=d.turnDetection}if(l.noise_reduction!==null&&l.noise_reduction!==void 0)throw new I("audio.input.noise_reduction must be null on this backend: no noise-reduction primitive","audio.input.noise_reduction","unsupported_field")}if(u.output!==void 0&&u.output!==null){let l=u.output;if(!l||typeof l!="object"||Array.isArray(l))throw new I("audio.output must be an object","audio.output");for(let d of Object.keys(l))if(!(d in qu))throw new I(`audio.output field not supported by this backend: ${d}`,`audio.output.${d}`,"unsupported_field");if(l.format!==void 0&&Is(l.format,"audio.output.format"),l.speed!==void 0)throw new I("audio.output.speed is not supported on this backend: playback rate is fixed","audio.output.speed","unsupported_field");if(l.voice!==void 0)if(l.voice===null)delete s.voice;else{if(typeof l.voice!="string")throw new I("audio.output.voice must be a string or null","audio.output.voice");if(!n.voice)throw new I("audio.output.voice refused: no authoritative voice binding for the resolved backend","audio.output.voice","audio_not_supported");s.voice=l.voice}l.format!==void 0&&!s.outputModalities.includes("audio")&&(s.outputModalities=["text","audio"])}break}default:break}return s}function dn(t,e){return{id:e,object:"realtime.session",type:t.type??"realtime",model:t.model??null,instructions:t.instructions??"",modalities:[...t.outputModalities],voice:t.voice??null,input_audio_format:"pcm16",output_audio_format:"pcm16",input_audio_transcription:t.inputAudioTranscription?{}:null,input_audio_noise_reduction:null,turn_detection:t.turnDetection?{type:"server_vad",create_response:t.turnDetection.createResponse,interrupt_response:t.turnDetection.interruptResponse}:null,temperature:t.temperature??null,max_response_output_tokens:t.maxOutputTokens??"inf",tool_choice:t.toolChoice??"auto",tools:t.tools??[],metadata:t.metadata??null}}function mr(t,e){let n=t;if(!n||typeof n!="object")throw new I("item must be an object","item","invalid_request");if(n.type==="function_call_output"){if(typeof n.call_id!="string"||typeof n.output!="string")throw new I("function_call_output item requires string call_id and output","item.call_id","invalid_request");return{id:typeof n.id=="string"?n.id:E("item"),type:"function_call_output",callId:n.call_id,output:n.output}}if(n.type==="message"){let r=n.role;if(r!=="user"&&r!=="assistant"&&r!=="system")throw new I('message item role must be "user", "assistant" or "system"',"item.role","invalid_request");if(!Array.isArray(n.content))throw new I("message item content must be an array","item.content","invalid_request");let o=[];for(let s of n.content){let i=s;if(!i||typeof i!="object")throw new I("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 I(`${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 I("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 I("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 I(`content part type ${String(i.type)} is not supported on message items here (input_text / output_text / input_audio)`,"item.content.type","invalid_request")}return{id:typeof n.id=="string"?n.id:E("item"),type:"message",role:r,content:o}}throw new I(`item type ${String(n.type)} is not supported \u2014 message and function_call_output items only`,"item.type","invalid_request")}function hr(t){let e=[],n=!1;for(let r of t){if(r.type==="function_call"){e.push({type:"function_call",call_id:r.callId,name:r.name,arguments:r.arguments??"{}"});continue}if(r.type==="function_call_output"){e.push({type:"function_call_output",call_id:r.callId,output:r.output});continue}let o=[];for(let s of r.content??[]){if(s.type==="input_text"||s.type==="output_text"){o.push({type:s.type,text:s.text});continue}if(s.type==="input_audio"){let i=s.transcript;if(typeof i=="string"&&i.trim().length>0){o.push({type:"input_text",text:i});continue}n=!0;continue}}e.push({role:r.role,content:o})}return{input:e,sawAudioOnlyItem:n}}var Hu=1001,ju=1011,Ts=t=>t.slice(0,120),Gu=/^[A-Za-z0-9+/=]*$/,Vu=2;function Ju(t){let e=Buffer.from(t,"base64");for(let n=0;n+1<e.byteLength;n+=2)if(Math.abs(e.readInt16LE(n))>Vu)return!1;return!0}var Et=class{id=E("conn");ws;binding;onClosed;config=fr();sessionId=E("sess");conversationId=E("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;turnLaneIterator=null;turnTranscript={open:!1,text:"",final:!1};lastCommittedTranscript=null;pendingTranscriptItemId=null;manuallyDischargedTurnItem=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.binding.intent==="transcription"&&(this.config.type="transcription"),this.ws.on("message",n=>{this.onMessage(n)}),this.ws.on("close",()=>this.teardown("client disconnected")),this.ws.on("error",()=>this.teardown("client socket error")),this.send({type:"session.created",event_id:E("event"),session:dn(this.config,this.sessionId)}),this.send({type:"conversation.created",event_id:E("event"),conversation:{id:this.conversationId,object:"realtime.conversation"}}),this.binding.turns&&this.startTurnLane()}startTurnLane(){let e=new AbortController;this.turnLaneAbort=e;let n=this.binding.turns(e.signal)[Symbol.asyncIterator]();this.turnLaneIterator=n,(async()=>{try{for(;;){let r=await n.next();if(r.done||this.closed||e.signal.aborted)return;this.onTurnEvent(r.value)}}catch(r){!this.closed&&!e.signal.aborted&&this.send(U(Ze("turn_lane_fault",`the backend turn/transcript lane failed: ${r instanceof Error?r.message:String(r)}`)))}finally{this.turnLaneIterator===n&&(this.turnLaneIterator=null)}})()}isTranscriptionSession(){return this.binding.intent==="transcription"||this.config.type==="transcription"}transcriptionEventsOn(){return this.config.inputAudioTranscription||this.isTranscriptionSession()}completePendingTranscription(e){this.pendingTranscriptItemId=null;let n=this.turnTranscript.text.trim();this.turnTranscript={open:!1,text:"",final:!1},this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:e,content_index:0,transcript:n})}onTurnEvent(e){switch(e.kind){case"delta":case"final":{let n=typeof e.text=="string"?e.text:"",r=this.pendingTranscriptItemId;this.turnTranscript.open=!0,n.length>0&&(this.turnTranscript.text+=n),this.turnTranscript.final=e.kind==="final",this.transcriptionEventsOn()&&n.length>0&&this.send({type:"conversation.item.input_audio_transcription.delta",event_id:E("event"),item_id:r??"input_audio_turn",content_index:0,delta:n}),e.kind==="final"&&r!==null&&(this.completePendingTranscription(r),this.manuallyDischargedTurnItem=r);return}case"turn_start":{this.manuallyDischargedTurnItem=null,this.turnTranscript={open:!0,text:this.turnTranscript.text,final:!1},this.send({type:"input_audio_buffer.speech_started",event_id:E("event"),item_id:"input_audio_turn",audio_start_ms:typeof e.startMs=="number"?e.startMs:0}),this.config.turnDetection?.interruptResponse&&this.inFlight&&this.cancelResponse();return}case"turn_end":{let n=this.pendingTranscriptItemId;if(n!==null){this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:n,audio_end_ms:typeof e.startMs=="number"?e.startMs:0}),this.completePendingTranscription(n);return}let r=this.manuallyDischargedTurnItem;if(r!==null){this.manuallyDischargedTurnItem=null,this.turnTranscript={open:!1,text:"",final:!1},this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:r,audio_end_ms:typeof e.startMs=="number"?e.startMs:0});return}let o=this.turnTranscript.text.trim(),s=o.length>0?o:null;if(this.turnTranscript={open:!1,text:"",final:!1},this.lastCommittedTranscript=s,this.isTranscriptionSession()&&this.config.turnDetection===null){this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:"input_audio_turn",audio_end_ms:typeof e.startMs=="number"?e.startMs:0});return}this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let i=E("item"),a=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem({id:i,type:"message",role:"user",content:[{type:"input_audio",...s!==null?{transcript:s}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:E("event"),previous_item_id:a,item_id:i}),this.send({type:"conversation.item.created",event_id:E("event"),previous_item_id:a,item:this.itemToProtocol(this.items[this.items.length-1])}),this.send({type:"input_audio_buffer.speech_stopped",event_id:E("event"),item_id:i,audio_end_ms:typeof e.startMs=="number"?e.startMs:0}),this.transcriptionEventsOn()&&s!==null&&this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:i,content_index:0,transcript:s}),this.config.turnDetection?.createResponse&&!this.inFlight&&!this.isTranscriptionSession()&&this.startResponse({});return}case"turn_eager_end":case"turn_resumed":return;default:return}}takeTurnTranscript(){let e=this.lastCommittedTranscript;return this.lastCommittedTranscript=null,e}async onMessage(e){if(this.closed)return;let n;try{n=e.toString()}catch{this.fail(G("decode_error","frame is not decodable text"),"invalid_frame");return}if(Buffer.byteLength(n,"utf8")>an){this.fail(G("frame_too_large",`frame exceeds the ${an}-byte limit`),"invalid_frame");return}let r;try{r=JSON.parse(n)}catch{this.fail(G("invalid_json","frame is not valid JSON"),"invalid_frame");return}if(typeof r.type!="string"||r.type.length===0){this.fail(G("missing_type",'event is missing a string "type"'),"invalid_frame");return}try{await this.handle(r)}catch(o){let s=o;if(s&&typeof s=="object"&&s.name==="SessionUpdateError"){this.send(U({type:"invalid_request_error",code:s.code??"invalid_value",message:s.message,param:s.param??null}));return}this.fail(Ze(null,o instanceof Error?o.message:String(o)),"handler_fault")}}async handle(e){switch(e.type){case"session.update":this.config=xt(e.session,this.config,{voice:this.binding.voice,turns:this.binding.turns!==void 0,model:this.binding.model}),this.send({type:"session.updated",event_id:E("event"),session:dn(this.config,this.sessionId)});return;case"conversation.item.create":{let n=mr(e.item,this.binding.voice),r=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem(n),this.send({type:"conversation.item.created",event_id:E("event"),previous_item_id:r,item:this.itemToProtocol(n)});return}case"conversation.item.delete":{let n=this.items.findIndex(r=>r.id===e.item_id);if(n<0){this.send(U(G("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.items.splice(n,1),this.send({type:"conversation.item.deleted",event_id:E("event"),item_id:e.item_id});return}case"conversation.item.retrieve":{let n=this.items.find(r=>r.id===e.item_id);if(!n){this.send(U(G("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.send({type:"conversation.item.retrieved",event_id:E("event"),item:this.itemToProtocol(n)});return}case"conversation.item.truncate":{let n=this.items.find(r=>r.id===e.item_id);if(!n||n.type!=="message"||n.role!=="assistant"){this.send(U(G("item_not_found",`no assistant message item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}n.truncated=!0,this.send({type:"conversation.item.truncated",event_id:E("event"),item_id:n.id,content_index:typeof e.content_index=="number"?e.content_index:0,audio_end_ms:typeof e.audio_end_ms=="number"?e.audio_end_ms:0});return}case"input_audio_buffer.append":{if(typeof e.audio!="string"||e.audio.length===0){this.send(U(G("invalid_audio",'input_audio_buffer.append requires base64 "audio"',"audio")));return}if(!this.ensureVoice())return;if(!Gu.test(e.audio)){this.send(U(G("invalid_audio","input_audio_buffer.append audio must be base64","audio")));return}let n=Buffer.byteLength(e.audio,"base64");if(this.pendingAudioTotalBytes+n>ln){this.send(U(G("buffer_full",`uncommitted input audio exceeds the ${ln}-byte bound \u2014 commit or clear first`,"audio")));return}if(this.config.type===void 0&&(this.config.type="realtime"),this.inFlight){this.binding.audioSession.audio.appendInputAudio(e.audio);return}if(this.isTranscriptionSession()){this.binding.audioSession.audio.appendInputAudio(e.audio),this.pendingAudioTotalBytes+=n;return}this.pendingAudioBytes.push(e.audio),this.pendingAudioTotalBytes+=n;return}case"input_audio_buffer.clear":{if(!this.ensureVoice())return;this.pendingAudioBytes.length=0,this.pendingAudioTotalBytes=0,this.lastCommittedTranscript=null,this.pendingTranscriptItemId===null&&(this.turnTranscript={open:!1,text:"",final:!1}),this.send({type:"input_audio_buffer.cleared",event_id:E("event")});return}case"input_audio_buffer.commit":{if(!this.ensureVoice())return;if(this.pendingAudioTotalBytes===0&&this.takeTurnTranscript()===null){this.send(U(G("empty_buffer","input_audio_buffer.commit with no buffered audio","audio")));return}this.isTranscriptionSession()&&this.pendingTranscriptItemId!==null&&this.completePendingTranscription(this.pendingTranscriptItemId),this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let n=this.takeTurnTranscript();n===null&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let r=E("item"),o=this.items.length>0?this.items[this.items.length-1].id:null;if(this.pushItem({id:r,type:"message",role:"user",content:[{type:"input_audio",...n!==null?{transcript:n}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:E("event"),previous_item_id:o,item_id:r}),this.send({type:"conversation.item.created",event_id:E("event"),previous_item_id:o,item:this.itemToProtocol(this.items[this.items.length-1])}),this.isTranscriptionSession())if(n!==null)this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:r,content_index:0,transcript:n});else if(this.turnTranscript.final){let s=this.turnTranscript.text.trim();this.turnTranscript={open:!1,text:"",final:!1},this.send({type:"conversation.item.input_audio_transcription.completed",event_id:E("event"),item_id:r,content_index:0,transcript:s}),this.manuallyDischargedTurnItem=r}else this.pendingTranscriptItemId=r;return}case"output_audio_buffer.clear":{this.send(U(G("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":if(this.isTranscriptionSession()){this.send(U(G("unsupported","response.create is not supported on a transcription session \u2014 this session transcribes the streamed audio with the engine's own transcript lane and emits conversation.item.input_audio_transcription events; there are no model responses to create","response.create")));return}this.startResponse(e.response??{});return;case"response.cancel":this.cancelResponse();return;default:this.send(U(G("unknown_event",`event type ${e.type} is not supported by this adapter`)))}}ensureVoice(){return!this.binding.voice||!this.binding.audioSession?(this.send(U(G("audio_not_supported","audio events require a voice binding from the resolver \u2014 this session was resolved text-only","audio"))),!1):(this.voiceClaimed||(this.claimVoiceBinding(),this.audioUnsubscribe=this.binding.audioSession.audio.onOutputAudio(e=>{this.onOutputAudioFrame(this.activeResponseId??"",e)}),this.voiceClaimed=!0),!0)}claimVoiceBinding(){let e=this.binding.audioSession,n=e.openaiRealtimeClaims??(e.openaiRealtimeClaims=new Set);if(n.has(e))throw new Error("voice binding already owned by another connection \u2014 the resolver handed one audio bridge to two realtime conversations, which must never happen (independent callers require private bindings)");n.add(e),this.audioLaneOwner=!0}releaseVoiceBinding(){let e=this.binding.audioSession;!e||!this.audioLaneOwner||(e.openaiRealtimeClaims?.delete(e),this.audioLaneOwner=!1)}warnAudioOnlyTranscriptGap(){this.audioOnlyTranscriptGapWarned||(this.audioOnlyTranscriptGapWarned=!0,this.send(U({type:"invalid_request_error",code:"audio_input_transcript_unavailable",message:"committed input audio is forwarded to the runtime audio lane but is NOT model-visible in the text turn lane: the resolved backend wires no turn/transcript lane (the voice engine's own stt stream with turn_start/turn_end + transcripts). Wire the transcribe/turn-detection leg upstream (the resolver supplies binding.turns) and committed audio gains its REAL transcript as the model-visible turn.",param:"input_audio_buffer.commit"})))}async upstreamStream(e,n){let r=this.binding.session;if(r&&typeof r.sendResponseCreate=="function"){let s=e.response??e;return r.sendResponseCreate({type:"response.create",response:s},e.input??[],n)}let o=this.binding.clients;if(o&&typeof o.createResponse=="function"){let{response:s,input:i,...a}=e;return await o.createResponse({...a,input:i})}throw new Error("realtime binding provides neither session.sendResponseCreate nor clients.createResponse \u2014 no upstream turn seam")}startResponse(e){if(this.inFlight){this.send(U(G("response_in_progress","a response is already in progress \u2014 cancel it with response.cancel first","response.create")));return}let n=e??{};if(n.modalities!==void 0){if(!Array.isArray(n.modalities)||n.modalities.some(g=>g!=="text"&&g!=="audio")){this.send(U(G("invalid_modalities",'response.modalities must be "text"/"audio"',"response.modalities")));return}if(n.modalities.includes("audio")&&!this.ensureVoice())return}if(n.input!==void 0){this.send(U(G("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=E("req"),o=`resp_${r}`;this.activeResponseId=o;let s=this.items.length,{input:i,sawAudioOnlyItem:a}=hr(this.items),u=typeof n.instructions=="string"?n.instructions:this.config.instructions,l=typeof n.temperature=="number"?n.temperature:this.config.temperature,d=n.max_output_tokens??this.config.maxOutputTokens,c=d==="inf"||d===void 0?void 0:d,y=n.tools!==void 0?n.tools:this.config.tools,f=n.tool_choice!==void 0?n.tool_choice:this.config.toolChoice,p={input:i,stream:!0,...this.config.model?{model:this.config.model}:{},...u!==void 0?{instructions:u}:{},...l!==void 0?{temperature:l}:{},...c!==void 0?{max_output_tokens:c}:{},...y!==void 0?{tools:y}:{},...f!==void 0?{tool_choice:f}:{}};for(let g of this.frozenAudio.splice(0))this.binding.audioSession?.audio.appendInputAudio(g);this.inFlight=(async()=>{let g=[],h=R=>{this.responseFlushed?this.send(R):g.push(R)},w=()=>{if(!this.responseFlushed){this.send({type:"response.created",event_id:E("event"),response:this.responseObject(o,"in_progress")}),this.send({type:"response.in_progress",event_id:E("event"),response:this.responseObject(o,"in_progress")});for(let R of g)this.send(R);g.length=0,this.responseFlushed=!0}},v={messageOpen:!1,messageText:"",tools:new Map,nextOutputIndex:0},m=null;try{a&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let R;try{R=await this.upstreamStream(p,r)}catch(b){this.upstreamTurnFault(b,o);return}try{for await(let b of R){if(this.cancelled)break;let _=b;if(_.type==="error"){let S=this.upstreamErrorBody(_);this.responseFlushed?this.send(U(S)):this.send({type:"response.failed",event_id:E("event"),response:this.failedResponse(o,S)});return}if(_.type==="response.output_text.delta"&&typeof _.delta=="string"){w(),v.messageOpen||(this.beginMessageItem(o,v.nextOutputIndex++),v.messageOpen=!0),h({type:"response.output_text.delta",event_id:E("event"),item_id:`${o}_msg_0`,response_id:o,output_index:0,content_index:0,delta:_.delta}),v.messageText+=_.delta;continue}if(_.type==="response.function_call_arguments.delta"){w();let S=typeof _.delta=="string"?_.delta:"",T=typeof _.call_id=="string"&&_.call_id.length>0?_.call_id:`${o}_fc_${v.nextOutputIndex}`,x=v.tools.get(T);if(!x){let O=typeof _.name=="string"&&_.name.length>0?_.name:void 0;x={outputIndex:v.nextOutputIndex++,name:O,args:""},v.tools.set(T,x),h({type:"response.output_item.added",event_id:E("event"),response_id:o,output_index:x.outputIndex,item:{id:T,object:"realtime.item",type:"function_call",call_id:T,name:O??"",arguments:"",status:"in_progress"}})}typeof _.name=="string"&&_.name.length>0&&(x.name=_.name),x.args+=S,h({type:"response.function_call_arguments.delta",event_id:E("event"),item_id:T,response_id:o,output_index:x.outputIndex,call_id:T,name:x.name??"",delta:S});continue}if(_.type==="response.completed"){m=_.response??null;continue}}}catch(b){this.upstreamTurnFault(b,o);return}if(this.cancelled)return;this.closeOpenItems(o,v,s),w(),this.send({type:"response.done",event_id:E("event"),response:this.doneResponse(o,s,m)})}finally{this.inFlight=null,this.activeResponseId=null}})()}onOutputAudioFrame(e,n){if(this.cancelled)return;if(this.activeResponseId!==e){if(Ju(n))return;!this.outOfTurnAudioWarned&&!this.closed&&(this.outOfTurnAudioWarned=!0,this.send(U({type:"invalid_request_error",code:"out_of_turn_audio",message:"the runtime produced voice OUT outside a response turn; this adapter delivers output audio only within response.output_audio.delta of an open turn \u2014 wire server-driven turns (VAD) upstream or keep speech inside response turns"})));return}this.responseFlushed||(this.send({type:"response.created",event_id:E("event"),response:this.responseObject(e,"in_progress")}),this.send({type:"response.in_progress",event_id:E("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:E("event"),item_id:r,response_id:e,output_index:0,content_index:0,delta:n})}messageItemAdded=!1;messageItemOpenForAudio=!1;beginMessageItem(e,n){let r=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:E("event"),response_id:e,output_index:n,item:{id:r,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:E("event"),item_id:r,response_id:e,output_index:n,content_index:0,part:{type:"output_text",text:""}})}beginAudioMessageItem(e){let n=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:E("event"),response_id:e,output_index:0,item:{id:n,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:E("event"),item_id:n,response_id:e,output_index:0,content_index:0,part:{type:"output_audio",transcript:""}})}closeOpenItems(e,n,r){if(n.messageOpen||this.messageItemOpenForAudio){let o=`${e}_msg_0`,s=this.messageItemOpenForAudio;s&&this.send({type:"response.output_audio.done",event_id:E("event"),item_id:o,response_id:e,output_index:0,content_index:0}),n.messageText.length>0&&this.send({type:"response.output_text.done",event_id:E("event"),item_id:o,response_id:e,output_index:0,content_index:0,text:n.messageText}),this.send({type:"response.content_part.done",event_id:E("event"),item_id:o,response_id:e,output_index:0,content_index:0,part:s?{type:"output_audio",transcript:""}:{type:"output_text",text:n.messageText}});let i=[];s&&i.push({type:"output_audio"}),n.messageText.length>0&&i.push({type:"output_text",text:n.messageText});let a={id:o,type:"message",role:"assistant",content:i};this.pushItem(a),this.send({type:"response.output_item.done",event_id:E("event"),response_id:e,output_index:0,item:this.itemToProtocol(a)})}for(let[o,s]of n.tools){let i=s.args.length>0?s.args:"{}";this.send({type:"response.function_call_arguments.done",event_id:E("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:E("event"),response_id:e,output_index:s.outputIndex,item:this.itemToProtocol(a)})}this.messageItemOpenForAudio=!1,this.messageItemAdded=!1}responseObject(e,n,r){return{id:e,object:"realtime.response",status:n,conversation_id:this.conversationId,usage:null,...r??{}}}failedResponse(e,n){return this.responseObject(e,"failed",{status_details:{error:{code:n.code,message:n.message}}})}doneResponse(e,n,r){let o=r&&typeof r.usage=="object"&&r.usage!==null?r.usage:null,s=[];for(let i=n;i<this.items.length;i++)s.push(this.itemToProtocol(this.items[i]));return this.responseObject(e,"completed",{output:s,usage:o?{input_tokens:typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0,output_tokens:typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0,total_tokens:typeof o.total_tokens=="number"&&Number.isFinite(o.total_tokens)?o.total_tokens:(typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0)+(typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0),input_token_details:{cached_tokens:0,audio_tokens:0,text_tokens:0},output_token_details:{audio_tokens:0,text_tokens:0}}:null})}upstreamErrorBody(e){let n=e.error,r=n&&typeof n.message=="string"?n.message:JSON.stringify(e);return{type:"server_error",code:n&&typeof n.code=="string"?n.code:null,message:r}}failSessionGone(e){let n=e instanceof Error?e.message:String(e);this.fail(Ze("session_gone",`this realtime connection's pooled session \u2014 the one its audio bridge and every turn ride \u2014 is gone: ${n}`),"session_gone")}upstreamTurnFault(e,n){if(e instanceof q){this.failSessionGone(e);return}if(!this.cancelled){if(!this.responseFlushed&&!(e instanceof Cs.SessionFailedError&&(e.kind==="never-live"||e.kind==="create-failed"))){this.failSessionGone(e);return}this.emitUpstreamFault(e,n)}}emitUpstreamFault(e,n){let r=Ze(null,e instanceof Error?e.message:String(e));this.responseFlushed?this.send(U(r)):this.send({type:"response.failed",event_id:E("event"),response:this.failedResponse(n,r)})}cancelResponse(){let e=this.inFlight;if(!e){this.send(U(G("no_response_to_cancel","no response is in progress","response.cancel")));return}this.cancelled=!0;let n=this.activeResponseId??"resp_";e.finally(()=>{this.send({type:"response.cancelled",event_id:E("event"),response:this.responseObject(n,"cancelled")})})}pushItem(e){this.items.push(e),this.items.length>un&&(this.items.shift(),this.send(U({type:"invalid_request_error",code:"transcript_evicted",message:`the conversation transcript exceeded ${un} items \u2014 the oldest item was evicted to keep the per-connection bound`})))}itemToProtocol(e){let n={id:e.id,object:"realtime.item"};if(e.type==="function_call")return{...n,type:"function_call",call_id:e.callId,name:e.name,arguments:e.arguments??"{}",status:"completed"};if(e.type==="function_call_output")return{...n,type:"function_call_output",call_id:e.callId,output:e.output};let r=(e.content??[]).map(o=>o.type==="input_text"||o.type==="output_text"?{type:o.type,text:o.text}:o.type==="input_audio"?{type:"input_audio",...o.transcript?{transcript:o.transcript}:{}}:{type:"output_audio",transcript:o.transcript??""});return{...n,type:"message",role:e.role,status:e.truncated?"incomplete":"completed",content:r}}send(e){if(!(this.closed||this.ws.readyState!==this.ws.OPEN)){if(this.ws.bufferedAmount>pr){this.ws.close(Hu,Ts("outbound buffer exceeded")),this.teardown("outbound buffer exceeded");return}this.ws.send(JSON.stringify(e))}}fail(e,n){this.send(U(e)),this.ws.close(ju,Ts(n)),this.teardown(n)}teardown(e){if(this.closed)return;this.closed=!0,this.turnLaneAbort?.abort(new Error("the realtime connection closed \u2014 the turn lane consumer is gone")),this.turnLaneAbort=null;let n=this.turnLaneIterator;if(this.turnLaneIterator=null,n!==null){let r=n.return?.();r&&r.catch(()=>{})}this.releaseVoiceBinding(),this.audioUnsubscribe?.(),this.audioUnsubscribe=null,this.binding.release&&Promise.resolve().then(()=>this.binding.release?.()).catch(r=>Ps(this.id,`binding release failed: ${r instanceof Error?r.message:String(r)}`));try{this.ws.removeAllListeners("message")}catch{}this.onClosed?.(this,{reason:e}),Ps(this.id,e)}},gr=[];function Ps(t,e){gr.push({id:t,reason:e}),gr.length>256&&gr.shift()}var At="/v1/realtime",Wu=t=>t.slice(0,120);function Ku(t,e,n){let r=e===401||e===403?"Unauthorized":e===502?"Upstream Fault":"Upgrade Refused";t.write(`HTTP/1.1 ${e} ${r}\r
|
|
17
17
|
Connection: close\r
|
|
18
18
|
Content-Type: text/plain\r
|
|
19
19
|
\r
|
|
20
20
|
${Wu(n)}\r
|
|
21
21
|
`),t.destroy()}function zu(t){return`${JSON.stringify({event:"realtime_upgrade",ts:new Date().toISOString(),...t})}
|
|
22
|
-
`}function Yu(t){Ae(process.stdout,t)}function Xu(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function yr(t){return t.audioSession?.audio.sessionId??void 0}function cn(t,e){let n=new Ms.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,s,i)=>{if(new URL(o.url??"/","http://localhost").pathname!==At)return;r.add(s),s.on("close",()=>{r.delete(s)});let u=E("rtup"),l=Xu(o),d=b=>{(e.log??Yu)(zu(b))};d({stage:"accept",conn:u,model:l}),s.resume();let c=new AbortController,y=null,f=b=>{y??=b,c.abort(new Error(y))},p=()=>f("client socket closed"),g=()=>f("client socket closed (half-close)"),h=b=>f(`client socket error: ${b.message}`);s.once("close",p),s.once("end",g),s.once("error",h);let _=()=>{s.removeListener("close",p),s.removeListener("end",g),s.removeListener("error",h)},v=b=>{d({stage:"close",conn:u,model:l,...b})},m=b=>{let w=b,S=w.session!=null&&typeof w.session.sendResponseCreate=="function",T=w.clients!=null&&typeof w.clients.createResponse=="function";if(!S&&!T)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((x,O)=>{n.handleUpgrade(o,s,i,P=>{try{new Et({ws:P,binding:b,onClosed:(D,X)=>{v({while:"live",reason:X.reason})}}),d({stage:"admit",conn:u,model:l,...yr(b)===void 0?{}:{session:yr(b)}}),x()}catch(D){O(D)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o,c.signal).then(async b=>{if(c.signal.aborted){_();let w=yr(b);try{await b.release?.()}catch(S){v({while:"queued",...w===void 0?{}:{session:w},reason:y??"client socket closed",release_failed:S instanceof Error?S.message:String(S)});return}v({while:"queued",...w===void 0?{}:{session:w},reason:y??"client socket closed",released:!0}),s.destroy();return}_(),await m(b)}).catch(b=>{let w=b instanceof Ls.SessionFailedError?on(b):null,S=null;if(b!==null&&typeof b=="object"&&"status"in b){let O=b.status;typeof O=="number"&&(S=O)}let T=S??w?.status??502,x=b instanceof Error?b.message:String(b);v({while:"queued",...c.signal.aborted?{reason:y??"client socket closed"}:{reason:x,status:T}}),s.destroyed||Ku(s,T,x)}).finally(()=>{r.delete(s)})}),async()=>{let o=Date.now()+5e3;for(;r.size>0&&Date.now()<o;)await new Promise(s=>setTimeout(s,25));await new Promise(s=>n.close(()=>s()))}}var Zl=require("http");var et=class extends Error{code;status;retryAfterSeconds;constructor(e,n,r,o){super(o),this.name="DecisionStreamFailure",this.code=e,this.status=n,this.retryAfterSeconds=r,Object.setPrototypeOf(this,new.target.prototype)}},It=class extends et{constructor(){super("draining",503,5,"decision pool is draining (GOAWAY): no new decisions may start; retry on the next replica")}},_r=class extends et{constructor(e){super("acquire_timeout",429,30,`no decision slot capacity within ${e}ms (all slots at their in-flight cap)`)}},Fe=class extends Error{lastDemandAgeMs;constructor(e){super("no ready decision slot on this proxy \u2014 the request was refused fast, not fenced; retry shortly (an out-of-band slot add was submitted)"),this.name="DecisionCapacityError",this.lastDemandAgeMs=e}},pn=class{constructor(e){this.opts=e;if(!Number.isInteger(e.min)||e.min<0)throw new Error("decision pool min must be a non-negative integer");if(!Number.isInteger(e.max)||e.max<e.min||e.max<1)throw new Error("decision pool max must be an integer >= min and >= 1");if(e.idleGraceMs!==void 0&&e.idleGraceMs<0)throw new Error("decision pool idleGraceMs must be >= 0");if(!Number.isInteger(e.inFlightCap)||e.inFlightCap<1)throw new Error("decision pool inFlightCap must be an integer >= 1");if(!e.replicaId)throw new Error("decision pool replicaId must be non-empty");this.now=e.now??Date.now,this.idleGraceMs=e.idleGraceMs??3e4}opts;slots=[];nextIndex=0;lastDemandAt=null;missCount=0;missCountPastWindow=0;lastMissAgeMs=null;idleGraceMs;waiters=[];now;closed=!1;draining=!1;drainComplete=null;slotKey(e){return`decision:${this.opts.replicaId}:${e}`}async boot(){this.closed||this.draining||await Promise.all(this.slots.map(e=>e.opening??Promise.resolve()))}openForDemand(){let e=this.slots.filter(n=>n.state==="paused"&&!n.opening).sort((n,r)=>n.slot.index-r.slot.index);return e.length>0?(this.openRuntime(e[0]),e[0]):this.ensureSlot()}ensureSlot(){let e=this.nextIndex++,r={slot:{index:e,concurrencyKey:this.slotKey(e)},state:"queued",entry:null,inFlight:0,idleSince:null,opening:null};return this.slots.push(r),this.openRuntime(r),r}openRuntime(e){e.opening||(e.state="queued",e.opening=(async()=>{try{let n=await this.opts.openSlot(e.slot);if(this.closed||this.draining){await this.opts.closeSlot(n).catch(()=>{}),this.signalDrain();return}e.entry=n,e.state="attached",e.idleSince=this.now(),this.pump()}catch(n){e.entry=null,e.state="paused",e.opening=null,this.opts.onDetach?.(e.slot,n instanceof Error?n.message:String(n)),this.pump();return}e.opening=null})())}async acquire(){if(this.closed)throw new Error("decision pool is closed");if(this.draining)throw new It;this.lastDemandAt=this.now();let e=this.opts.acquireTimeoutMs??6e5,n=Date.now();for(;;){let r=this.tryLease();if(r)return r;if(this.draining)throw new It;if(!this.slots.some(u=>u.state==="queued")&&this.slots.length<this.opts.max){this.openForDemand();continue}let s=n+e-Date.now();if(s<=0)throw new _r(e);let i,a={resolve:()=>{},settled:!1};try{await Promise.race([new Promise(u=>{a.resolve=u,this.waiters.push(a)}),new Promise(u=>{i=setTimeout(u,s)})])}finally{clearTimeout(i);let u=this.waiters.indexOf(a);u!==-1&&this.waiters.splice(u,1),a.settled=!0}if(this.closed)throw new Error("decision pool is closed");if(this.draining)throw new It}}tryAcquireReady(){if(this.closed)return null;let e=this.lastDemandAt===null?null:this.now()-this.lastDemandAt;this.lastDemandAt=this.now();let n=this.tryLease();return n||(!this.slots.some(r=>r.state==="queued")&&this.slots.length<this.opts.max&&this.openForDemand(),this.missCount+=1,e!==null&&e>this.idleGraceMs&&(this.missCountPastWindow+=1),this.lastMissAgeMs=e,null)}async pingReady(e){if(this.closed)return!1;for(let n=0;n<this.slots.length;n++){let r=this.slots[n];if(r.state!=="attached"||r.entry===null)continue;let o=r.entry;if(o===null||typeof o!="object"||!("session"in o))return!1;let s=o.session;if(typeof s!="object"||s===null||!("whenLive"in s))return!1;let i=s.whenLive;if(typeof i!="function")return!1;let a=r.entry;try{return await i.call(s,{timeout:e}),!0}catch{this.evict(a),r.entry!==null&&r.entry!==a&&n--;continue}}return!1}tryLease(){let e=null;for(let s of this.slots)s.state!=="attached"||s.inFlight>=this.opts.inFlightCap||(!e||s.inFlight<e.inFlight)&&(e=s);if(!e)return null;let n=e.entry;if(n===null)return null;let r=e;r.inFlight+=1;let o=!1;return{slot:r.slot,entry:n,release:()=>{o||(o=!0,r.inFlight=Math.max(0,r.inFlight-1),r.inFlight===0&&(r.idleSince=this.now()),this.pump(),this.reap(),this.signalDrain())}}}pump(){for(;this.waiters.length>0;){let e=this.waiters.shift();if(!e.settled){e.settled=!0,e.resolve();return}}}evict(e){let n=this.slots.find(r=>r.entry===e);n&&(n.entry=null,n.state="paused",n.opening=null,this.opts.onDetach?.(n.slot,"runtime detached"),this.signalDrain())}reap(){if(this.closed||this.draining)return;let e=this.now(),n=this.slots.filter(r=>r.state==="attached"&&r.inFlight===0&&r.idleSince!==null&&e-r.idleSince>=this.idleGraceMs).sort((r,o)=>(r.idleSince??0)-(o.idleSince??0)||o.slot.index-r.slot.index);for(let r of n)this.endSlot(r)}endSlot(e){e.state="paused";let n=e.entry;e.entry=null,this.slots.splice(this.slots.indexOf(e),1),n&&this.opts.closeSlot(n).catch(()=>{})}stats(){let e=this.slots.filter(o=>o.state==="attached").length,n=this.slots.filter(o=>o.state==="queued").length,r=this.slots.filter(o=>o.state==="paused").length;return{requested:this.slots.length,attached:e,queued:n,paused:r,inFlight:this.slots.reduce((o,s)=>o+s.inFlight,0),draining:this.draining,misses:this.missCount,missesPastWindow:this.missCountPastWindow,lastMissAgeMs:this.lastMissAgeMs}}signalDrain(){if(!(!this.draining||this.drainComplete===null)&&this.slots.reduce((e,n)=>e+n.inFlight,0)===0){let e=this.drainComplete;this.drainComplete=null,e.resolve()}}async drain(e){if(!this.closed){this.draining=!0;for(let n of this.waiters.splice(0))n.settled||(n.settled=!0,n.resolve());if(this.slots.reduce((n,r)=>n+r.inFlight,0)>0){if(!this.drainComplete){let r,o=new Promise(s=>{r=s});this.drainComplete={promise:o,resolve:r}}let n=this.drainComplete;await Promise.race([n.promise,new Promise(r=>setTimeout(r,e?.timeoutMs??5e3))])}await this.close()}}async close(){this.closed=!0;for(let o of this.waiters.splice(0))o.settled||(o.settled=!0,o.resolve());let e=this.slots.map(o=>o.entry).filter(o=>o!==null);this.slots.length=0;let r=(await Promise.allSettled(e.map(o=>this.opts.closeSlot(o)))).filter(o=>o.status==="rejected");if(r.length>0)throw new Error(`failed to end ${r.length} decision pool session(s)`)}};var Q=class extends Error{};async function fn(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new Q(`request body exceeds the ${e}-byte limit`);let r=[],o=0;for await(let s of t){let i=s;if(o+=i.length,o>e)throw new Q(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function mn(t,e){let n=t[Symbol.asyncIterator](),r=0,o=new ReadableStream({async pull(i){try{let a=await n.next();if(a.done){i.close();return}if(r+=a.value.length,r>e){i.error(new Q(`request body exceeds the ${e}-byte limit`));return}i.enqueue(a.value)}catch(a){i.error(a)}},cancel(){n.return?.()}}),s;try{let i=new Headers;for(let[l,d]of Object.entries(t.headers))if(typeof d=="string")i.set(l,d);else if(Array.isArray(d))for(let c of d)i.append(l,c);let a={method:"POST",headers:i,body:o,duplex:"half"};s=await new Request(`https://proxy.internal${t.url??"/"}`,a).formData()}catch(i){throw i instanceof Q?i:new Error(`malformed multipart body: ${String(i instanceof Error?i.message:i)}`)}return s}var $s="/v1/images/generations",hn="/v1/images/edits";function Ns(t){return t===$s||t===hn}var Us="/v1/images/models";async function Ds(t,e,n){if(t){if(t.model=oe({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}var M=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},Qu=16*1024*1024,Fs=64*1024*1024;function Bs(t){return t.length>=8&&t[0]===137&&t[1]===80&&t[2]===78&&t[3]===71&&t[4]===13&&t[5]===10&&t[6]===26&&t[7]===10?"image/png":t.length>=3&&t[0]===255&&t[1]===216&&t[2]===255?"image/jpeg":t.length>=12&&t[0]===82&&t[1]===73&&t[2]===70&&t[3]===70&&t[8]===87&&t[9]===69&&t[10]===66&&t[11]===80?"image/webp":null}function Ot(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}async function qs(t,e,n,r){let o=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},r),s=null;for await(let c of o){if(c.type==="error"){let y=c.error??{};throw new M(502,`image upstream error: ${String(y.message??"upstream reported an error")}`)}if(c.type==="response.completed"){s=c.response??null;break}}if(s===null)throw new M(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 M(502,`image upstream returned ${Array.isArray(i)?i.length:"a non-array images field"} images for the single-shot (n=1) contract, which produces exactly one \u2014 refusing to truncate or fabricate`);let a=[],u=i[0].startsWith("data:")?i[0].slice(i[0].indexOf(";base64,")+8):i[0],l=Buffer.from(u,"base64");if(Bs(l)===null)throw new M(502,`image upstream returned a payload that is not a PNG/JPEG/WebP image (${l.length} bytes) \u2014 refusing to pass it through`);return a.push(u),{images:a}}function Hs(t){return t instanceof M?t:t instanceof N?new M(404,t.message):new M(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function js(t,e,n,r){if(await t(e,n))return!0;let o=n==="edit"?"no image-input (expects_image) edit destination":"no pure text-to-image (expects_image=false) generation destination";return Ot(r,new M(404,e===void 0?`no image-capable default model is configured for this caller (${o}) \u2014 name an image-capable \`model\``:`model "${e}" is not ${n==="edit"?"edit-capable":"generation-capable"} for this caller (${o}; capability is read from the catalog's modality metadata, not the serve function name)`)),!1}function Gs(t,e){t&&(t.outputUnits=e.images.length)}function Vs(t,e){t.statusCode=200,t.setHeader("content-type","application/json"),t.end(JSON.stringify({created:Math.floor(Date.now()/1e3),data:e.images.map(n=>({b64_json:n}))}))}var Zu={model:!0,prompt:!0,response_format:!0,n:!0};async function Js(t,e,n){let r=n.maxBodyBytes??Fs;try{if(t.method!=="POST")throw new M(405,`${String(t.method)} is not supported on ${$s}`);let o;try{o=JSON.parse((await fn(t,r)).toString("utf8")||"{}")}catch(c){throw c instanceof Q?new M(413,c.message):new M(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new M(400,"request body must be a JSON object");let s=o;for(let c of Object.keys(s))if(!Zu[c])throw new M(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 M(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 M(400,"model must be a string","model");await Ds(n.turn,n.clients,a);let u=s.n??1;if(u!==1)throw new M(400,`n=${JSON.stringify(u)} is not supported: the native image request is single-shot and produces exactly one image per request (no batch support yet)`,"n");let l=s.response_format??"b64_json";if(l!=="b64_json")throw new M(400,`response_format ${JSON.stringify(l)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");if(n.stats?.request("images_generations",i.length,i.length),!await js(n.imageModels,a,"generation",e))return;let d=[{type:"input_text",text:i}];try{let c=await qs(n.clients,a,d,n.turn?le(n.turn):void 0);Gs(n.turn,c),Vs(e,c)}catch(c){throw Hs(c)}}catch(o){if(o instanceof Q){Ot(e,new M(413,o.message));return}if(o instanceof M){Ot(e,o);return}throw o}}var el={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function Ws(t,e,n){let r=n.maxBodyBytes??Fs,o=n.maxImagePartBytes??Qu;try{if(t.method!=="POST")throw new M(405,`${String(t.method)} is not supported on ${hn}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new M(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await mn(t,r)}catch(f){throw f instanceof Q?new M(413,f.message):new M(400,String(f instanceof Error?f.message:f))}let a=i.get("prompt");if(typeof a!="string"||a.trim().length===0)throw new M(400,"prompt is required and must be a non-empty string field","prompt");let u=i.get("model");if(u!==null&&typeof u!="string")throw new M(400,"model must be a string field","model");let l=u===null?void 0:u;await Ds(n.turn,n.clients,l);let d=i.get("response_format")??"b64_json";if(d!=="b64_json")throw new M(400,`response_format ${JSON.stringify(d)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");for(let f of i.keys())if(!el[f])throw new M(400,`form field "${f}" 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[]).`,f);let c=[];for(let f of["image","image[]"])for(let p of i.getAll(f)){if(!(p instanceof File))throw new M(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",f);if(p.size>o)throw new M(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,f);let g=new Uint8Array(await p.arrayBuffer()),h=Bs(g);if(h===null)throw new M(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",f);c.push({bytes:g,mime:h})}if(c.length===0)throw new M(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",a.length,a.length),!await js(n.imageModels,l,"edit",e))return;let y=[{type:"input_text",text:a}];for(let{bytes:f,mime:p}of c)y.push({type:"input_image",image_url:`data:${p};base64,${Buffer.from(f).toString("base64")}`});try{let f=await qs(n.clients,l,y,n.turn?le(n.turn):void 0);Gs(n.turn,f),Vs(e,f)}catch(f){throw Hs(f)}}catch(s){if(s instanceof Q){Ot(e,new M(413,s.message));return}if(s instanceof M){Ot(e,s);return}throw s}}var Tt="/v1/audio/speech";function ni(t){return t===Tt}var Ks=4096,zs=18e4,Ys=5e3,tl=64*1024*1024,Xs=8,Qs=5,Zs=1024*1024,tt="voice_applied",yn="X-Urun-Voice-Applied",L=class extends Error{status;param;retryAfterSeconds;constructor(e,n,r,o){super(n),this.status=e,this.param=r,this.retryAfterSeconds=o}};function wr(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),e.retryAfterSeconds!==void 0&&t.setHeader("retry-after",String(e.retryAfterSeconds)),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}var _n=class extends L{constructor(e){super(502,e)}};function ri(t){for(let e=0;e<t.length;e+=1){let n=t.charCodeAt(e);if(n!==9&&!(n>=32&&n<=126)&&!(n>=128&&n<=255))return!1}return!0}function oi(t){return new L(502,`the runtime's ${tt} receipt ${JSON.stringify(t)} cannot be relayed as the ${yn} header \u2014 it carries a character no HTTP header or trailer value can carry (a newline, another control character, or a non-Latin-1 character); the lane never serves an answer whose receipt it cannot deliver nor silently drops the runtime's own acknowledgment`)}async function nl(t,e,n){if(t){if(t.model=oe({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}function si(t,e){if(!t)return;let n=e/(24e3*2);t.outputUnits=n,t.outputMinutes=n/60}var rl=2;function ol(t){for(let e=0;e+1<t.byteLength;e+=2)if(Math.abs(t.readInt16LE(e))>rl)return!1;return!0}function sl(t){let e=24e3,n=2,r=1,o=Buffer.alloc(44);return o.write("RIFF",0,"ascii"),o.writeUInt32LE(36+t,4),o.write("WAVE",8,"ascii"),o.write("fmt ",12,"ascii"),o.writeUInt32LE(16,16),o.writeUInt16LE(1,20),o.writeUInt16LE(r,22),o.writeUInt32LE(e,24),o.writeUInt32LE(e*r*n,28),o.writeUInt16LE(r*n,32),o.writeUInt16LE(16,34),o.write("data",36,"ascii"),o.writeUInt32LE(t,40),o}var ei=new WeakMap,gn=new WeakMap;function il(t,e){let n=gn.get(t)??0;if(n>=Xs)throw new L(503,`this model's speech queue is full \u2014 ${Xs} requests are already admitted (one in-flight turn plus the queued depth) and each holds a pooled session turn; retry after ${Qs}s`,void 0,Qs);gn.set(t,n+1);let r=ei.get(t)??Promise.resolve(),o,s=new Promise(i=>{o=i});return ei.set(t,s),(async()=>{await r;try{return await e()}finally{gn.set(t,(gn.get(t)??1)-1),o()}})()}async function al(t,e,n,r){if(typeof t.evictSpeechSession!="function")throw new L(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned session after a failed turn \u2014 refusing loudly rather than handing the next request the failed turn\u2019s audio");try{await t.evictSpeechSession(e)}catch(i){if(!(i instanceof q))throw i}if(n?.return===void 0)return;let o=Promise.resolve(n.return()).then(()=>{},()=>{}),s;await Promise.race([o,new Promise(i=>{s=setTimeout(i,r)})]).finally(()=>{s!==void 0&&clearTimeout(s)})}function Be(t){return t instanceof L?t:t instanceof N?new L(404,t.message):new L(502,`speech upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function ti(t,e,n,r,o,s,i,a){let u=a??(()=>{});return il(e,async()=>{if(i?.()===!0)return null;let l=!1,d=!1,c=!1,y,f=new Promise(b=>{y=b}),p=[],g=()=>{d&&!c&&p.length>=o.quietRunFrames&&(c=!0,y())},h=e.onOutputAudio(b=>{let w=Buffer.from(b,"base64");if(w.byteLength===0)return;let S=ol(w);if(!l){if(S)return;l=!0}if(S){p.push(w),g();return}for(let T of p)u(T);p.length=0,u(w),g()}),_=null,v,m,R;try{R=(await t.createResponseOn(n,{model:r.model,input:[{role:"user",content:[{type:"input_text",text:r.text}]}],stream:!1,...r.voice!==void 0?{voice:r.voice}:{}}))[Symbol.asyncIterator]();let w=new Promise(x=>{m=setTimeout(x,s.capMs)}),S=!1;for(;;){let x=R.next(),O=await Promise.race([x,w]);if(O===void 0){S=!0,x.catch(()=>{});break}if(O.done)break;let P=O.value;if(P.type==="error"){let D=P.error??{};throw await R.return?.(),new L(502,`speech upstream error: ${String(D.message??"upstream reported an error")}`)}if(P.type==="response.completed"){_=P.response??null,d=!0,g(),await R.return?.();break}}if(clearTimeout(m),S)throw new L(504,`the speech turn produced no terminal event within ${s.capMs}ms \u2014 the upstream stream never completed, so the pinned speech session is evicted (the next request opens a fresh one), the serialized lane is released and the caller can retry; a stuck turn must not hold every later request on this lane`);if(_===null)throw new L(502,"speech upstream produced no terminal response \u2014 refusing to fabricate audio");let T=!1;if(await Promise.race([f,new Promise(x=>{v=setTimeout(()=>{T=!0,x()},o.capMs)})]),v!==void 0&&clearTimeout(v),r.voice!==void 0){let x=_[tt];if(typeof x!="string"||x!==r.voice){let O=`the runtime did not acknowledge the requested voice ${JSON.stringify(r.voice)} \u2014 the terminal response carried ${typeof x=="string"?`${tt} ${JSON.stringify(x)}`:`no ${tt} field`}; the speak-bridge companion is not deployed, and this lane never serves default-voice audio as the requested voice`;throw c?new _n(O):new L(502,O)}}if(!l)throw new L(502,"the upstream produced no voice for this utterance \u2014 refusing to serve silence as success");if(T&&!c&&p.length<1)throw new L(502,`the upstream audio stream did not quiesce within ${o.capMs}ms of the turn's end (voiced frames still arriving) \u2014 refusing to serve audio whose tail cannot be vouched for`);return _}catch(b){throw b instanceof _n||await al(t,n,R,s.returnBoundMs),b}finally{v!==void 0&&clearTimeout(v),m!==void 0&&clearTimeout(m),h()}})}function ul(t,e,n,r){let o=!1,s=0,i=a=>{t.destroy(a instanceof Error?a:new Error(String(a)))};return{sink(a){n()||(o||(o=!0,t.statusCode=200,t.setHeader("content-type","audio/pcm"),r&&t.setHeader("trailer",yn)),t.write(a,u=>{u||(s+=a.byteLength,si(e,s))}),t.writableLength>Zs&&(e&&(e.streamError=!0),i(new Error(`the streamed pcm answer's undrained buffer crossed ${Zs} bytes (1 MiB \u2014 about 21 s of 24 kHz mono PCM16): the caller stopped reading the stream, and the lane bounds what it will buffer for one answer rather than let a stalled reader hold the process's memory; the transfer is aborted (an after-first-byte failure \u2014 the bytes whose writes the socket confirmed are billed) and the caller should retry when it can drain the audio as it arrives`))))},started:()=>o,async finish(a){if(!n()){if(r){let u=a[tt];if(typeof u=="string"){if(!ri(u))throw oi(u);t.addTrailers({[yn]:u})}}await new Promise(u=>{t.end(()=>u())})}},fail:i}}async function ll(t,e,n){let r;try{r=await t(e)}catch(o){throw Be(o)}return r?!0:(wr(n,new L(404,e===void 0?"no speech-capable default model is configured for this caller (catalog task tts) \u2014 name a tts model":`model "${e}" is not speech-capable (catalog task is not tts) \u2014 this lane serves only text-to-speech models; chat models ride /v1/chat/completions`)),!1)}var dl={model:!0,input:!0,voice:!0,response_format:!0,stream_format:!0,speed:!0,instructions:!0},cl="wav, pcm";async function ii(t,e,n){let r=n.maxBodyBytes??tl,o=!1;e.once("close",()=>{o=!0});let s=()=>o||e.destroyed;try{if(t.method!=="POST")throw new L(405,`${String(t.method)} is not supported on ${Tt}`);let i;try{i=JSON.parse((await fn(t,r)).toString("utf8")||"{}")}catch(m){throw m instanceof Q?new L(413,m.message):new L(400,"request body is not valid JSON")}if(i===null||typeof i!="object"||Array.isArray(i))throw new L(400,"request body must be a JSON object");let a=i;for(let m of Object.keys(a))if(!dl[m])throw new L(400,`request field "${m}" is not implemented: the speech request schema carries the utterance and its voice (the qwen3 backend fixes voice style at deploy time, and sample rate is pinned to the engine's native 24 kHz mono). Supported fields: model, input, voice, response_format, stream_format, speed, instructions.`,m);let u=a.input;if(typeof u!="string"||u.trim().length===0)throw new L(400,"input is required and must be a non-empty string","input");if(u.length>Ks)throw new L(400,`input is ${u.length} chars \u2014 over the ${Ks}-char limit (one utterance; the backend bounds generation text-shaped, and an unbounded text is an unbounded synthesis)`,"input");let l=a.model===void 0?void 0:a.model;if(l!==void 0&&typeof l!="string")throw new L(400,"model must be a string","model");let d=a.voice;if(d!==void 0&&(typeof d!="string"||d.trim().length===0))throw new L(400,"voice must be a non-empty string","voice");if(typeof n.clients.speechVoices!="function")throw new L(501,`the speech lane needs a router-backed client (speechVoices) to resolve the voice set GET /v1/models publishes \u2014 refusing loudly rather than validating voices of ${Tt} against a stale copy of the list`);let c;try{c=await n.clients.speechVoices(l)}catch(m){throw Be(m)}if(c.defect!==null)throw new L(500,`the voice set for this model cannot be resolved: ${c.defect} \u2014 fix the catalog row; the lane refuses to serve rather than validate against a set nobody vouched`);let y=typeof d=="string"?d:void 0;if(y!==void 0&&c.published.length>0&&!c.published.includes(y))throw new L(400,`voice ${JSON.stringify(y)} is not one of the voices this model publishes \u2014 accepted voices: ${c.published.join(", ")}`,"voice");let f=a.response_format??"wav";if(f!=="wav"&&f!=="pcm")throw new L(400,`response_format ${JSON.stringify(f)} is not supported: this lane serves the engine's raw 24 kHz mono PCM16, so the supported formats are ${cl} \u2014 and the lane will not label bytes with a format it did not encode`,"response_format");if(a.stream_format!==void 0&&typeof a.stream_format!="string")throw new L(400,"stream_format, when present, must be a string","stream_format");let p=a.speed;if(p!==void 0){if(typeof p!="number"||!Number.isFinite(p))throw new L(400,"speed, when present, must be a number","speed");if(p!==1)throw new L(400,`speed=${JSON.stringify(p)} is not supported: the backend exposes no speed knob (the qwen3 engine's speaking rate is fixed), so a non-default speed would be silently ignored \u2014 1 (the OpenAI default) is the only accepted value`,"speed")}let g=a.instructions;if(g!==void 0){if(typeof g!="string")throw new L(400,"instructions, when present, must be a string","instructions");if(g.trim().length>0)throw new L(400,"instructions are not supported: the backend fixes the voice style (instruct) at deploy time, so a per-request instruction would be silently ignored \u2014 the deployment speaks with its configured style","instructions")}await nl(n.turn,n.clients,l);let h=Buffer.byteLength(JSON.stringify(a));if(n.stats?.request("audio_speech",h,h),!await ll(n.speechModels,l,e))return;if(typeof n.clients.openAudioOn!="function")throw new L(501,"a speech-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the same session identity as the response turn \u2014 refusing loudly rather than answering an utterance whose audio cannot be collected");if(typeof n.clients.speechSessionHandle!="function")throw new L(501,"a speech-capable model needs a router-backed client (speechSessionHandle) to resolve the speech lane's own pooled session \u2014 one no interactive binding shares, because the per-session audio bridge broadcasts every output frame to every subscriber \u2014 refusing loudly rather than collecting speech audio on a session a realtime/gemini lane can be speaking on");if(typeof n.clients.evictSpeechSession!="function")throw new L(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned speech session after a failed turn (a turn-cap timeout above all) \u2014 without it, the next request on the same model would silently collect the failed turn\u2019s still-emitting audio; refusing loudly rather than serving on an un-evictable speech slot");if(s())return;let _;try{_=await n.clients.speechSessionHandle(l)}catch(m){throw Be(m)}let v;try{v=await n.clients.openAudioOn(_)}catch(m){throw Be(m)}n.turn&&(n.turn.sessionId=v.sessionId);try{if(f==="wav"){let m=[],R=await ti(n.clients,v,_,{model:l,text:u,...y!==void 0?{voice:y}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??zs,returnBoundMs:n.turnReturnBoundMs??Ys},s,w=>m.push(w));if(R===null||s())return;let b=Buffer.concat(m);if(y===void 0){let w=R[tt];if(typeof w=="string"){if(!ri(w))throw oi(w);e.setHeader(yn,w)}}si(n.turn,b.byteLength),e.statusCode=200,e.setHeader("content-type","audio/wav"),e.end(Buffer.concat([sl(b.byteLength),b]))}else{let m=ul(e,n.turn,s,y===void 0),R;try{R=await ti(n.clients,v,_,{model:l,text:u,...y!==void 0?{voice:y}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??zs,returnBoundMs:n.turnReturnBoundMs??Ys},s,m.sink)}catch(b){let w=Be(b);if(!m.started())throw w;n.turn&&(n.turn.streamError=!0),m.fail(w);return}if(R===null)return;try{await m.finish(R)}catch(b){let w=Be(b);n.turn&&(n.turn.streamError=!0),m.fail(w);return}}}catch(m){throw Be(m)}}catch(i){if(i instanceof Q){wr(e,new L(413,i.message));return}if(i instanceof L){wr(e,i);return}throw i}}var wn="/v1/audio/transcriptions";function ai(t){return t===wn}var ui="/v1/audio/models",pl=16*1024*1024,fl=64*1024*1024,B=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}};function vr(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}function li(t){return t.length>=12&&t[0]===82&&t[1]===73&&t[2]===70&&t[3]===70&&t[8]===87&&t[9]===65&&t[10]===86&&t[11]===69}function ml(t){if(!li(t))return null;let e=12,n=null,r=null;for(;e+8<=t.length;){let s=String.fromCharCode(t[e],t[e+1],t[e+2],t[e+3]),i=(t[e+4]|t[e+5]<<8|t[e+6]<<16|t[e+7]<<24)>>>0,a=e+8;if(s==="fmt "&&a+16<=t.length){let u=t[a+2]|t[a+3]<<8,l=(t[a+4]|t[a+5]<<8|t[a+6]<<16|t[a+7]<<24)>>>0,d=t[a+14]|t[a+15]<<8;n={sampleRate:l,channels:u,bitsPerSample:d}}else if(s==="data"){let u=t.length-a;r=Math.min(i===0||i===4294967295?u:i,u)}e=a+i+i%2}if(n===null||r===null||r===0||n.sampleRate===0||n.channels===0||n.bitsPerSample===0)return null;let o=r/(n.sampleRate*n.channels*(n.bitsPerSample/8));return!Number.isFinite(o)||o<0?null:{seconds:o,sampleRate:n.sampleRate,channels:n.channels,bitsPerSample:n.bitsPerSample}}async function hl(t,e,n){if(t){if(t.model=oe({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}function gl(t,e){t&&(t.outputUnits=e.seconds)}async function yl(t,e,n,r,o){let s=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1,...r!==void 0?{language:r}:{}},o),i=[],a=null;for await(let u of s){if(i.push(u),u.type==="error"){let l=u.error??{},d=typeof l.type=="string"?l.type:"upstream_error",c=String(l.message??"upstream reported an error");throw new B(d==="invalid_request_error"?400:502,`transcription upstream error: ${c}`)}if(u.type==="response.completed"){a=u.response??null;break}}if(a===null)throw new B(502,"transcription upstream produced no terminal response \u2014 refusing to fabricate a transcript");return Xe(i,a)}function di(t){return t instanceof B?t:t instanceof N?new B(404,t.message):new B(502,`transcription upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function _l(t,e,n){let r;try{r=await t(e)}catch(o){throw di(o)}return r?!0:(vr(n,new B(404,e===void 0?"no transcription-capable default model is configured for this caller (catalog task stt) \u2014 name an stt model":`model "${e}" is not transcription-capable (catalog task is not stt) \u2014 this lane serves only speech-to-text models; tts models ride /v1/audio/speech and chat models ride /v1/chat/completions`)),!1)}var wl=new Set(["file","model","language","response_format"]);async function ci(t,e,n){let r=n.maxBodyBytes??fl,o=n.maxAudioPartBytes??pl;try{if(t.method!=="POST")throw new B(405,`${String(t.method)} is not supported on ${wn}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new B(400,`the transcriptions endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await mn(t,r)}catch(_){throw _ instanceof Q?new B(413,_.message):new B(400,String(_ instanceof Error?_.message:_))}let a=new Map;for(let _ of i.keys()){if(!wl.has(_))throw new B(400,`form field "${_}" is not implemented: the native transcription request carries the audio clip and a language hint only (prompt, temperature, timestamps and similar knobs have no native surface on this lane). Supported fields: file, model, language, response_format.`,_);a.set(_,(a.get(_)??0)+1)}for(let[_,v]of a)if(v>1)throw new B(400,`form field "${_}" appears ${v} times: the lane reads exactly one value per field, and a repeat is refused rather than silently discarded \u2014 resend the request with each field once`,_);let u=i.get("model");if(u!==null&&typeof u!="string")throw new B(400,"model must be a string field","model");let l=u===null?void 0:u,d=i.get("language");if(d!==null&&(typeof d!="string"||d.trim().length===0))throw new B(400,"language, when present, must be a non-empty string","language");let c=d===null?void 0:d,y=i.get("response_format")??"json";if(y!=="json")throw new B(400,`response_format ${JSON.stringify(y)} is not supported: timestamps/segments are not produced on this lane yet \u2014 the runtime answers terminal transcript text only, so json is the only output format`,"response_format");let f=i.get("file");if(!(f instanceof File))throw new B(400,"file must be an uploaded WAV: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths","file");if(f.size>o)throw new B(413,`file part exceeds the ${o}-byte limit (the serve protocol's MAX_AUDIO_BYTES)`,"file");let p=new Uint8Array(await f.arrayBuffer());if(!li(p))throw new B(400,"the audio payload is not a RIFF/WAVE container \u2014 this lane accepts WAV uploads only (detected by magic bytes, not the declared Content-Type); the runtime decodes PCM16 WAV and nothing else","file");let g=ml(p);if(g===null)throw new B(400,"the WAV header cannot be read (no fmt /data chunk pair, or a zero rate/channel/depth) \u2014 the runtime decodes RIFF/WAVE PCM16 and this container cannot be honestly decoded or billed","file");if(await hl(n.turn,n.clients,l),n.stats?.request("audio_transcriptions",p.length,p.length),!await _l(n.transcriptionModels,l,e))return;let h=[{type:"input_audio",input_audio:{data:Buffer.from(p).toString("base64"),format:"wav"}}];try{let _=await yl(n.clients,l,h,c,n.turn?le(n.turn):void 0);gl(n.turn,g),e.statusCode=200,e.setHeader("content-type","application/json"),e.end(JSON.stringify({text:_}))}catch(_){throw di(_)}}catch(s){if(s instanceof Q){vr(e,new B(413,s.message));return}if(s instanceof B){vr(e,s);return}throw s}}var bn="/v1/usage/requests",pi=1e4,vl=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,Ie=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},H=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}},vn=class extends H{constructor(e){super(e),this.name="UsageWithholdingError"}},gi=1024*1024;function kr(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new Ie(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new Ie(`"${e}" must be an array of uuid strings`);if(n.length>pi)throw new Ie(`"${e}" carries ${n.length} ids; at most ${pi} may be looked up in one request. Split the batch \u2014 the ids past the limit are NOT silently dropped`);let r=[],o=new Set;for(let s of n){if(typeof s!="string"||!vl.test(s))throw new Ie(`"${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 yi(t){return kr(t,"inference_ids")}var bl=["inference_id","model_id","identity_kind","variant","task","http_status","started_at","completed_at","prompt_tokens","completion_tokens","output_units","output_minutes","cost_nano_usd","price_version","priced_at"];function nt(t,e,n){let r=t[e];if(typeof r!="string"||r==="")throw new H(`${n}: ${e} is not a non-empty string (got ${ot(r)})`);return r}function br(t,e,n){let r=t[e];if(r===null)return null;if(typeof r!="string"||r==="")throw new H(`${n}: ${e} is neither null nor a non-empty string (got ${ot(r)})`);return r}function rt(t,e,n,r){let o=t[e];if(o===null)return null;let s=typeof o=="number"?o:typeof o=="string"&&/^-?\d+(\.\d+)?$/.test(o.trim())?Number(o.trim()):NaN;if(!Number.isFinite(s))throw new H(`${n}: ${e} is not a number (got ${ot(o)})`);if(r&&!Number.isSafeInteger(s))throw new H(`${n}: ${e} is not a safely representable integer \u2014 a billing quantity that silently loses digits must fail rather than be reported`);if(s<0)throw new H(`${n}: ${e} is negative`);return Object.is(s,-0)?0:s}var Sl=40,fi=64,mi=600;function _i(t){let e=Object.keys(t).sort(),r=e.slice(0,Sl).map(o=>o.length>fi?`${o.slice(0,fi)}\u2026`:o).join(", ");return r.length>mi&&(r=`${r.slice(0,mi)}\u2026`),`${e.length} keys present: [${r}]`}function ot(t){return t===null?"null":t===void 0?"undefined":Array.isArray(t)?"an array":typeof t=="object"?"an object":`a ${typeof t}`}function Sr(t){Ae(process.stdout,`${JSON.stringify({event:"usage_row_rejected",ts:new Date().toISOString(),where:t.where,reason:t.reason})}
|
|
22
|
+
`}function Yu(t){Ae(process.stdout,t)}function Xu(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function yr(t){return t.audioSession?.audio.sessionId??void 0}function cn(t,e){let n=new Ms.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,s,i)=>{if(new URL(o.url??"/","http://localhost").pathname!==At)return;r.add(s),s.on("close",()=>{r.delete(s)});let u=E("rtup"),l=Xu(o),d=b=>{(e.log??Yu)(zu(b))};d({stage:"accept",conn:u,model:l}),s.resume();let c=new AbortController,y=null,f=b=>{y??=b,c.abort(new Error(y))},p=()=>f("client socket closed"),g=()=>f("client socket closed (half-close)"),h=b=>f(`client socket error: ${b.message}`);s.once("close",p),s.once("end",g),s.once("error",h);let w=()=>{s.removeListener("close",p),s.removeListener("end",g),s.removeListener("error",h)},v=b=>{d({stage:"close",conn:u,model:l,...b})},m=b=>{let _=b,S=_.session!=null&&typeof _.session.sendResponseCreate=="function",T=_.clients!=null&&typeof _.clients.createResponse=="function";if(!S&&!T)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((x,O)=>{n.handleUpgrade(o,s,i,P=>{try{new Et({ws:P,binding:b,onClosed:(D,X)=>{v({while:"live",reason:X.reason})}}),d({stage:"admit",conn:u,model:l,...yr(b)===void 0?{}:{session:yr(b)}}),x()}catch(D){O(D)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o,c.signal).then(async b=>{if(c.signal.aborted){w();let _=yr(b);try{await b.release?.()}catch(S){v({while:"queued",..._===void 0?{}:{session:_},reason:y??"client socket closed",release_failed:S instanceof Error?S.message:String(S)});return}v({while:"queued",..._===void 0?{}:{session:_},reason:y??"client socket closed",released:!0}),s.destroy();return}w(),await m(b)}).catch(b=>{let _=b instanceof Ls.SessionFailedError?on(b):null,S=null;if(b!==null&&typeof b=="object"&&"status"in b){let O=b.status;typeof O=="number"&&(S=O)}let T=S??_?.status??502,x=b instanceof Error?b.message:String(b);v({while:"queued",...c.signal.aborted?{reason:y??"client socket closed"}:{reason:x,status:T}}),s.destroyed||Ku(s,T,x)}).finally(()=>{r.delete(s)})}),async()=>{let o=Date.now()+5e3;for(;r.size>0&&Date.now()<o;)await new Promise(s=>setTimeout(s,25));await new Promise(s=>n.close(()=>s()))}}var Zl=require("http");var et=class extends Error{code;status;retryAfterSeconds;constructor(e,n,r,o){super(o),this.name="DecisionStreamFailure",this.code=e,this.status=n,this.retryAfterSeconds=r,Object.setPrototypeOf(this,new.target.prototype)}},It=class extends et{constructor(){super("draining",503,5,"decision pool is draining (GOAWAY): no new decisions may start; retry on the next replica")}},_r=class extends et{constructor(e){super("acquire_timeout",429,30,`no decision slot capacity within ${e}ms (all slots at their in-flight cap)`)}},Fe=class extends Error{lastDemandAgeMs;constructor(e){super("no ready decision slot on this proxy \u2014 the request was refused fast, not fenced; retry shortly (an out-of-band slot add was submitted)"),this.name="DecisionCapacityError",this.lastDemandAgeMs=e}},pn=class{constructor(e){this.opts=e;if(!Number.isInteger(e.min)||e.min<0)throw new Error("decision pool min must be a non-negative integer");if(!Number.isInteger(e.max)||e.max<e.min||e.max<1)throw new Error("decision pool max must be an integer >= min and >= 1");if(e.idleGraceMs!==void 0&&e.idleGraceMs<0)throw new Error("decision pool idleGraceMs must be >= 0");if(!Number.isInteger(e.inFlightCap)||e.inFlightCap<1)throw new Error("decision pool inFlightCap must be an integer >= 1");if(!e.replicaId)throw new Error("decision pool replicaId must be non-empty");this.now=e.now??Date.now,this.idleGraceMs=e.idleGraceMs??3e4}opts;slots=[];nextIndex=0;lastDemandAt=null;missCount=0;missCountPastWindow=0;lastMissAgeMs=null;idleGraceMs;waiters=[];now;closed=!1;draining=!1;drainComplete=null;slotKey(e){return`decision:${this.opts.replicaId}:${e}`}async boot(){this.closed||this.draining||await Promise.all(this.slots.map(e=>e.opening??Promise.resolve()))}openForDemand(){let e=this.slots.filter(n=>n.state==="paused"&&!n.opening).sort((n,r)=>n.slot.index-r.slot.index);return e.length>0?(this.openRuntime(e[0]),e[0]):this.ensureSlot()}ensureSlot(){let e=this.nextIndex++,r={slot:{index:e,concurrencyKey:this.slotKey(e)},state:"queued",entry:null,inFlight:0,idleSince:null,opening:null};return this.slots.push(r),this.openRuntime(r),r}openRuntime(e){e.opening||(e.state="queued",e.opening=(async()=>{try{let n=await this.opts.openSlot(e.slot);if(this.closed||this.draining){await this.opts.closeSlot(n).catch(()=>{}),this.signalDrain();return}e.entry=n,e.state="attached",e.idleSince=this.now(),this.pump()}catch(n){e.entry=null,e.state="paused",e.opening=null,this.opts.onDetach?.(e.slot,n instanceof Error?n.message:String(n)),this.pump();return}e.opening=null})())}async acquire(){if(this.closed)throw new Error("decision pool is closed");if(this.draining)throw new It;this.lastDemandAt=this.now();let e=this.opts.acquireTimeoutMs??6e5,n=Date.now();for(;;){let r=this.tryLease();if(r)return r;if(this.draining)throw new It;if(!this.slots.some(u=>u.state==="queued")&&this.slots.length<this.opts.max){this.openForDemand();continue}let s=n+e-Date.now();if(s<=0)throw new _r(e);let i,a={resolve:()=>{},settled:!1};try{await Promise.race([new Promise(u=>{a.resolve=u,this.waiters.push(a)}),new Promise(u=>{i=setTimeout(u,s)})])}finally{clearTimeout(i);let u=this.waiters.indexOf(a);u!==-1&&this.waiters.splice(u,1),a.settled=!0}if(this.closed)throw new Error("decision pool is closed");if(this.draining)throw new It}}tryAcquireReady(){if(this.closed)return null;let e=this.lastDemandAt===null?null:this.now()-this.lastDemandAt;this.lastDemandAt=this.now();let n=this.tryLease();return n||(!this.slots.some(r=>r.state==="queued")&&this.slots.length<this.opts.max&&this.openForDemand(),this.missCount+=1,e!==null&&e>this.idleGraceMs&&(this.missCountPastWindow+=1),this.lastMissAgeMs=e,null)}async pingReady(e){if(this.closed)return!1;for(let n=0;n<this.slots.length;n++){let r=this.slots[n];if(r.state!=="attached"||r.entry===null)continue;let o=r.entry;if(o===null||typeof o!="object"||!("session"in o))return!1;let s=o.session;if(typeof s!="object"||s===null||!("whenLive"in s))return!1;let i=s.whenLive;if(typeof i!="function")return!1;let a=r.entry;try{return await i.call(s,{timeout:e}),!0}catch{this.evict(a),r.entry!==null&&r.entry!==a&&n--;continue}}return!1}tryLease(){let e=null;for(let s of this.slots)s.state!=="attached"||s.inFlight>=this.opts.inFlightCap||(!e||s.inFlight<e.inFlight)&&(e=s);if(!e)return null;let n=e.entry;if(n===null)return null;let r=e;r.inFlight+=1;let o=!1;return{slot:r.slot,entry:n,release:()=>{o||(o=!0,r.inFlight=Math.max(0,r.inFlight-1),r.inFlight===0&&(r.idleSince=this.now()),this.pump(),this.reap(),this.signalDrain())}}}pump(){for(;this.waiters.length>0;){let e=this.waiters.shift();if(!e.settled){e.settled=!0,e.resolve();return}}}evict(e){let n=this.slots.find(r=>r.entry===e);n&&(n.entry=null,n.state="paused",n.opening=null,this.opts.onDetach?.(n.slot,"runtime detached"),this.signalDrain())}reap(){if(this.closed||this.draining)return;let e=this.now(),n=this.slots.filter(r=>r.state==="attached"&&r.inFlight===0&&r.idleSince!==null&&e-r.idleSince>=this.idleGraceMs).sort((r,o)=>(r.idleSince??0)-(o.idleSince??0)||o.slot.index-r.slot.index);for(let r of n)this.endSlot(r)}endSlot(e){e.state="paused";let n=e.entry;e.entry=null,this.slots.splice(this.slots.indexOf(e),1),n&&this.opts.closeSlot(n).catch(()=>{})}stats(){let e=this.slots.filter(o=>o.state==="attached").length,n=this.slots.filter(o=>o.state==="queued").length,r=this.slots.filter(o=>o.state==="paused").length;return{requested:this.slots.length,attached:e,queued:n,paused:r,inFlight:this.slots.reduce((o,s)=>o+s.inFlight,0),draining:this.draining,misses:this.missCount,missesPastWindow:this.missCountPastWindow,lastMissAgeMs:this.lastMissAgeMs}}signalDrain(){if(!(!this.draining||this.drainComplete===null)&&this.slots.reduce((e,n)=>e+n.inFlight,0)===0){let e=this.drainComplete;this.drainComplete=null,e.resolve()}}async drain(e){if(!this.closed){this.draining=!0;for(let n of this.waiters.splice(0))n.settled||(n.settled=!0,n.resolve());if(this.slots.reduce((n,r)=>n+r.inFlight,0)>0){if(!this.drainComplete){let r,o=new Promise(s=>{r=s});this.drainComplete={promise:o,resolve:r}}let n=this.drainComplete;await Promise.race([n.promise,new Promise(r=>setTimeout(r,e?.timeoutMs??5e3))])}await this.close()}}async close(){this.closed=!0;for(let o of this.waiters.splice(0))o.settled||(o.settled=!0,o.resolve());let e=this.slots.map(o=>o.entry).filter(o=>o!==null);this.slots.length=0;let r=(await Promise.allSettled(e.map(o=>this.opts.closeSlot(o)))).filter(o=>o.status==="rejected");if(r.length>0)throw new Error(`failed to end ${r.length} decision pool session(s)`)}};var Q=class extends Error{};async function fn(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new Q(`request body exceeds the ${e}-byte limit`);let r=[],o=0;for await(let s of t){let i=s;if(o+=i.length,o>e)throw new Q(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function mn(t,e){let n=t[Symbol.asyncIterator](),r=0,o=new ReadableStream({async pull(i){try{let a=await n.next();if(a.done){i.close();return}if(r+=a.value.length,r>e){i.error(new Q(`request body exceeds the ${e}-byte limit`));return}i.enqueue(a.value)}catch(a){i.error(a)}},cancel(){n.return?.()}}),s;try{let i=new Headers;for(let[l,d]of Object.entries(t.headers))if(typeof d=="string")i.set(l,d);else if(Array.isArray(d))for(let c of d)i.append(l,c);let a={method:"POST",headers:i,body:o,duplex:"half"};s=await new Request(`https://proxy.internal${t.url??"/"}`,a).formData()}catch(i){throw i instanceof Q?i:new Error(`malformed multipart body: ${String(i instanceof Error?i.message:i)}`)}return s}var $s="/v1/images/generations",hn="/v1/images/edits";function Ns(t){return t===$s||t===hn}var Us="/v1/images/models";async function Ds(t,e,n){if(t){if(t.model=oe({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}var M=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},Qu=16*1024*1024,Fs=64*1024*1024;function Bs(t){return t.length>=8&&t[0]===137&&t[1]===80&&t[2]===78&&t[3]===71&&t[4]===13&&t[5]===10&&t[6]===26&&t[7]===10?"image/png":t.length>=3&&t[0]===255&&t[1]===216&&t[2]===255?"image/jpeg":t.length>=12&&t[0]===82&&t[1]===73&&t[2]===70&&t[3]===70&&t[8]===87&&t[9]===69&&t[10]===66&&t[11]===80?"image/webp":null}function Ot(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}async function qs(t,e,n,r){let o=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},r),s=null;for await(let c of o){if(c.type==="error"){let y=c.error??{};throw new M(502,`image upstream error: ${String(y.message??"upstream reported an error")}`)}if(c.type==="response.completed"){s=c.response??null;break}}if(s===null)throw new M(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 M(502,`image upstream returned ${Array.isArray(i)?i.length:"a non-array images field"} images for the single-shot (n=1) contract, which produces exactly one \u2014 refusing to truncate or fabricate`);let a=[],u=i[0].startsWith("data:")?i[0].slice(i[0].indexOf(";base64,")+8):i[0],l=Buffer.from(u,"base64");if(Bs(l)===null)throw new M(502,`image upstream returned a payload that is not a PNG/JPEG/WebP image (${l.length} bytes) \u2014 refusing to pass it through`);return a.push(u),{images:a}}function Hs(t){return t instanceof M?t:t instanceof N?new M(404,t.message):new M(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function js(t,e,n,r){if(await t(e,n))return!0;let o=n==="edit"?"no image-input (expects_image) edit destination":"no pure text-to-image (expects_image=false) generation destination";return Ot(r,new M(404,e===void 0?`no image-capable default model is configured for this caller (${o}) \u2014 name an image-capable \`model\``:`model "${e}" is not ${n==="edit"?"edit-capable":"generation-capable"} for this caller (${o}; capability is read from the catalog's modality metadata, not the serve function name)`)),!1}function Gs(t,e){t&&(t.outputUnits=e.images.length)}function Vs(t,e){t.statusCode=200,t.setHeader("content-type","application/json"),t.end(JSON.stringify({created:Math.floor(Date.now()/1e3),data:e.images.map(n=>({b64_json:n}))}))}var Zu={model:!0,prompt:!0,response_format:!0,n:!0};async function Js(t,e,n){let r=n.maxBodyBytes??Fs;try{if(t.method!=="POST")throw new M(405,`${String(t.method)} is not supported on ${$s}`);let o;try{o=JSON.parse((await fn(t,r)).toString("utf8")||"{}")}catch(c){throw c instanceof Q?new M(413,c.message):new M(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new M(400,"request body must be a JSON object");let s=o;for(let c of Object.keys(s))if(!Zu[c])throw new M(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 M(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 M(400,"model must be a string","model");await Ds(n.turn,n.clients,a);let u=s.n??1;if(u!==1)throw new M(400,`n=${JSON.stringify(u)} is not supported: the native image request is single-shot and produces exactly one image per request (no batch support yet)`,"n");let l=s.response_format??"b64_json";if(l!=="b64_json")throw new M(400,`response_format ${JSON.stringify(l)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");if(n.stats?.request("images_generations",i.length,i.length),!await js(n.imageModels,a,"generation",e))return;let d=[{type:"input_text",text:i}];try{let c=await qs(n.clients,a,d,n.turn?le(n.turn):void 0);Gs(n.turn,c),Vs(e,c)}catch(c){throw Hs(c)}}catch(o){if(o instanceof Q){Ot(e,new M(413,o.message));return}if(o instanceof M){Ot(e,o);return}throw o}}var el={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function Ws(t,e,n){let r=n.maxBodyBytes??Fs,o=n.maxImagePartBytes??Qu;try{if(t.method!=="POST")throw new M(405,`${String(t.method)} is not supported on ${hn}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new M(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await mn(t,r)}catch(f){throw f instanceof Q?new M(413,f.message):new M(400,String(f instanceof Error?f.message:f))}let a=i.get("prompt");if(typeof a!="string"||a.trim().length===0)throw new M(400,"prompt is required and must be a non-empty string field","prompt");let u=i.get("model");if(u!==null&&typeof u!="string")throw new M(400,"model must be a string field","model");let l=u===null?void 0:u;await Ds(n.turn,n.clients,l);let d=i.get("response_format")??"b64_json";if(d!=="b64_json")throw new M(400,`response_format ${JSON.stringify(d)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");for(let f of i.keys())if(!el[f])throw new M(400,`form field "${f}" 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[]).`,f);let c=[];for(let f of["image","image[]"])for(let p of i.getAll(f)){if(!(p instanceof File))throw new M(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",f);if(p.size>o)throw new M(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,f);let g=new Uint8Array(await p.arrayBuffer()),h=Bs(g);if(h===null)throw new M(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",f);c.push({bytes:g,mime:h})}if(c.length===0)throw new M(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",a.length,a.length),!await js(n.imageModels,l,"edit",e))return;let y=[{type:"input_text",text:a}];for(let{bytes:f,mime:p}of c)y.push({type:"input_image",image_url:`data:${p};base64,${Buffer.from(f).toString("base64")}`});try{let f=await qs(n.clients,l,y,n.turn?le(n.turn):void 0);Gs(n.turn,f),Vs(e,f)}catch(f){throw Hs(f)}}catch(s){if(s instanceof Q){Ot(e,new M(413,s.message));return}if(s instanceof M){Ot(e,s);return}throw s}}var Tt="/v1/audio/speech";function ni(t){return t===Tt}var Ks=4096,zs=18e4,Ys=5e3,tl=64*1024*1024,Xs=8,Qs=5,Zs=1024*1024,tt="voice_applied",yn="X-Urun-Voice-Applied",L=class extends Error{status;param;retryAfterSeconds;constructor(e,n,r,o){super(n),this.status=e,this.param=r,this.retryAfterSeconds=o}};function wr(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),e.retryAfterSeconds!==void 0&&t.setHeader("retry-after",String(e.retryAfterSeconds)),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}var _n=class extends L{constructor(e){super(502,e)}};function ri(t){for(let e=0;e<t.length;e+=1){let n=t.charCodeAt(e);if(n!==9&&!(n>=32&&n<=126)&&!(n>=128&&n<=255))return!1}return!0}function oi(t){return new L(502,`the runtime's ${tt} receipt ${JSON.stringify(t)} cannot be relayed as the ${yn} header \u2014 it carries a character no HTTP header or trailer value can carry (a newline, another control character, or a non-Latin-1 character); the lane never serves an answer whose receipt it cannot deliver nor silently drops the runtime's own acknowledgment`)}async function nl(t,e,n){if(t){if(t.model=oe({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}function si(t,e){if(!t)return;let n=e/(24e3*2);t.outputUnits=n,t.outputMinutes=n/60}var rl=2;function ol(t){for(let e=0;e+1<t.byteLength;e+=2)if(Math.abs(t.readInt16LE(e))>rl)return!1;return!0}function sl(t){let e=24e3,n=2,r=1,o=Buffer.alloc(44);return o.write("RIFF",0,"ascii"),o.writeUInt32LE(36+t,4),o.write("WAVE",8,"ascii"),o.write("fmt ",12,"ascii"),o.writeUInt32LE(16,16),o.writeUInt16LE(1,20),o.writeUInt16LE(r,22),o.writeUInt32LE(e,24),o.writeUInt32LE(e*r*n,28),o.writeUInt16LE(r*n,32),o.writeUInt16LE(16,34),o.write("data",36,"ascii"),o.writeUInt32LE(t,40),o}var ei=new WeakMap,gn=new WeakMap;function il(t,e){let n=gn.get(t)??0;if(n>=Xs)throw new L(503,`this model's speech queue is full \u2014 ${Xs} requests are already admitted (one in-flight turn plus the queued depth) and each holds a pooled session turn; retry after ${Qs}s`,void 0,Qs);gn.set(t,n+1);let r=ei.get(t)??Promise.resolve(),o,s=new Promise(i=>{o=i});return ei.set(t,s),(async()=>{await r;try{return await e()}finally{gn.set(t,(gn.get(t)??1)-1),o()}})()}async function al(t,e,n,r){if(typeof t.evictSpeechSession!="function")throw new L(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned session after a failed turn \u2014 refusing loudly rather than handing the next request the failed turn\u2019s audio");try{await t.evictSpeechSession(e)}catch(i){if(!(i instanceof q))throw i}if(n?.return===void 0)return;let o=Promise.resolve(n.return()).then(()=>{},()=>{}),s;await Promise.race([o,new Promise(i=>{s=setTimeout(i,r)})]).finally(()=>{s!==void 0&&clearTimeout(s)})}function Be(t){return t instanceof L?t:t instanceof N?new L(404,t.message):new L(502,`speech upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function ti(t,e,n,r,o,s,i,a){let u=a??(()=>{});return il(e,async()=>{if(i?.()===!0)return null;let l=!1,d=!1,c=!1,y,f=new Promise(b=>{y=b}),p=[],g=()=>{d&&!c&&p.length>=o.quietRunFrames&&(c=!0,y())},h=e.onOutputAudio(b=>{let _=Buffer.from(b,"base64");if(_.byteLength===0)return;let S=ol(_);if(!l){if(S)return;l=!0}if(S){p.push(_),g();return}for(let T of p)u(T);p.length=0,u(_),g()}),w=null,v,m,R;try{R=(await t.createResponseOn(n,{model:r.model,input:[{role:"user",content:[{type:"input_text",text:r.text}]}],stream:!1,...r.voice!==void 0?{voice:r.voice}:{}}))[Symbol.asyncIterator]();let _=new Promise(x=>{m=setTimeout(x,s.capMs)}),S=!1;for(;;){let x=R.next(),O=await Promise.race([x,_]);if(O===void 0){S=!0,x.catch(()=>{});break}if(O.done)break;let P=O.value;if(P.type==="error"){let D=P.error??{};throw await R.return?.(),new L(502,`speech upstream error: ${String(D.message??"upstream reported an error")}`)}if(P.type==="response.completed"){w=P.response??null,d=!0,g(),await R.return?.();break}}if(clearTimeout(m),S)throw new L(504,`the speech turn produced no terminal event within ${s.capMs}ms \u2014 the upstream stream never completed, so the pinned speech session is evicted (the next request opens a fresh one), the serialized lane is released and the caller can retry; a stuck turn must not hold every later request on this lane`);if(w===null)throw new L(502,"speech upstream produced no terminal response \u2014 refusing to fabricate audio");let T=!1;if(await Promise.race([f,new Promise(x=>{v=setTimeout(()=>{T=!0,x()},o.capMs)})]),v!==void 0&&clearTimeout(v),r.voice!==void 0){let x=w[tt];if(typeof x!="string"||x!==r.voice){let O=`the runtime did not acknowledge the requested voice ${JSON.stringify(r.voice)} \u2014 the terminal response carried ${typeof x=="string"?`${tt} ${JSON.stringify(x)}`:`no ${tt} field`}; the speak-bridge companion is not deployed, and this lane never serves default-voice audio as the requested voice`;throw c?new _n(O):new L(502,O)}}if(!l)throw new L(502,"the upstream produced no voice for this utterance \u2014 refusing to serve silence as success");if(T&&!c&&p.length<1)throw new L(502,`the upstream audio stream did not quiesce within ${o.capMs}ms of the turn's end (voiced frames still arriving) \u2014 refusing to serve audio whose tail cannot be vouched for`);return w}catch(b){throw b instanceof _n||await al(t,n,R,s.returnBoundMs),b}finally{v!==void 0&&clearTimeout(v),m!==void 0&&clearTimeout(m),h()}})}function ul(t,e,n,r){let o=!1,s=0,i=a=>{t.destroy(a instanceof Error?a:new Error(String(a)))};return{sink(a){n()||(o||(o=!0,t.statusCode=200,t.setHeader("content-type","audio/pcm"),r&&t.setHeader("trailer",yn)),t.write(a,u=>{u||(s+=a.byteLength,si(e,s))}),t.writableLength>Zs&&(e&&(e.streamError=!0),i(new Error(`the streamed pcm answer's undrained buffer crossed ${Zs} bytes (1 MiB \u2014 about 21 s of 24 kHz mono PCM16): the caller stopped reading the stream, and the lane bounds what it will buffer for one answer rather than let a stalled reader hold the process's memory; the transfer is aborted (an after-first-byte failure \u2014 the bytes whose writes the socket confirmed are billed) and the caller should retry when it can drain the audio as it arrives`))))},started:()=>o,async finish(a){if(!n()){if(r){let u=a[tt];if(typeof u=="string"){if(!ri(u))throw oi(u);t.addTrailers({[yn]:u})}}await new Promise(u=>{t.end(()=>u())})}},fail:i}}async function ll(t,e,n){let r;try{r=await t(e)}catch(o){throw Be(o)}return r?!0:(wr(n,new L(404,e===void 0?"no speech-capable default model is configured for this caller (catalog task tts) \u2014 name a tts model":`model "${e}" is not speech-capable (catalog task is not tts) \u2014 this lane serves only text-to-speech models; chat models ride /v1/chat/completions`)),!1)}var dl={model:!0,input:!0,voice:!0,response_format:!0,stream_format:!0,speed:!0,instructions:!0},cl="wav, pcm";async function ii(t,e,n){let r=n.maxBodyBytes??tl,o=!1;e.once("close",()=>{o=!0});let s=()=>o||e.destroyed;try{if(t.method!=="POST")throw new L(405,`${String(t.method)} is not supported on ${Tt}`);let i;try{i=JSON.parse((await fn(t,r)).toString("utf8")||"{}")}catch(m){throw m instanceof Q?new L(413,m.message):new L(400,"request body is not valid JSON")}if(i===null||typeof i!="object"||Array.isArray(i))throw new L(400,"request body must be a JSON object");let a=i;for(let m of Object.keys(a))if(!dl[m])throw new L(400,`request field "${m}" is not implemented: the speech request schema carries the utterance and its voice (the qwen3 backend fixes voice style at deploy time, and sample rate is pinned to the engine's native 24 kHz mono). Supported fields: model, input, voice, response_format, stream_format, speed, instructions.`,m);let u=a.input;if(typeof u!="string"||u.trim().length===0)throw new L(400,"input is required and must be a non-empty string","input");if(u.length>Ks)throw new L(400,`input is ${u.length} chars \u2014 over the ${Ks}-char limit (one utterance; the backend bounds generation text-shaped, and an unbounded text is an unbounded synthesis)`,"input");let l=a.model===void 0?void 0:a.model;if(l!==void 0&&typeof l!="string")throw new L(400,"model must be a string","model");let d=a.voice;if(d!==void 0&&(typeof d!="string"||d.trim().length===0))throw new L(400,"voice must be a non-empty string","voice");if(typeof n.clients.speechVoices!="function")throw new L(501,`the speech lane needs a router-backed client (speechVoices) to resolve the voice set GET /v1/models publishes \u2014 refusing loudly rather than validating voices of ${Tt} against a stale copy of the list`);let c;try{c=await n.clients.speechVoices(l)}catch(m){throw Be(m)}if(c.defect!==null)throw new L(500,`the voice set for this model cannot be resolved: ${c.defect} \u2014 fix the catalog row; the lane refuses to serve rather than validate against a set nobody vouched`);let y=typeof d=="string"?d:void 0;if(y!==void 0&&c.published.length>0&&!c.published.includes(y))throw new L(400,`voice ${JSON.stringify(y)} is not one of the voices this model publishes \u2014 accepted voices: ${c.published.join(", ")}`,"voice");let f=a.response_format??"wav";if(f!=="wav"&&f!=="pcm")throw new L(400,`response_format ${JSON.stringify(f)} is not supported: this lane serves the engine's raw 24 kHz mono PCM16, so the supported formats are ${cl} \u2014 and the lane will not label bytes with a format it did not encode`,"response_format");if(a.stream_format!==void 0&&typeof a.stream_format!="string")throw new L(400,"stream_format, when present, must be a string","stream_format");let p=a.speed;if(p!==void 0){if(typeof p!="number"||!Number.isFinite(p))throw new L(400,"speed, when present, must be a number","speed");if(p!==1)throw new L(400,`speed=${JSON.stringify(p)} is not supported: the backend exposes no speed knob (the qwen3 engine's speaking rate is fixed), so a non-default speed would be silently ignored \u2014 1 (the OpenAI default) is the only accepted value`,"speed")}let g=a.instructions;if(g!==void 0){if(typeof g!="string")throw new L(400,"instructions, when present, must be a string","instructions");if(g.trim().length>0)throw new L(400,"instructions are not supported: the backend fixes the voice style (instruct) at deploy time, so a per-request instruction would be silently ignored \u2014 the deployment speaks with its configured style","instructions")}await nl(n.turn,n.clients,l);let h=Buffer.byteLength(JSON.stringify(a));if(n.stats?.request("audio_speech",h,h),!await ll(n.speechModels,l,e))return;if(typeof n.clients.openAudioOn!="function")throw new L(501,"a speech-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the same session identity as the response turn \u2014 refusing loudly rather than answering an utterance whose audio cannot be collected");if(typeof n.clients.speechSessionHandle!="function")throw new L(501,"a speech-capable model needs a router-backed client (speechSessionHandle) to resolve the speech lane's own pooled session \u2014 one no interactive binding shares, because the per-session audio bridge broadcasts every output frame to every subscriber \u2014 refusing loudly rather than collecting speech audio on a session a realtime/gemini lane can be speaking on");if(typeof n.clients.evictSpeechSession!="function")throw new L(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned speech session after a failed turn (a turn-cap timeout above all) \u2014 without it, the next request on the same model would silently collect the failed turn\u2019s still-emitting audio; refusing loudly rather than serving on an un-evictable speech slot");if(s())return;let w;try{w=await n.clients.speechSessionHandle(l)}catch(m){throw Be(m)}let v;try{v=await n.clients.openAudioOn(w)}catch(m){throw Be(m)}n.turn&&(n.turn.sessionId=v.sessionId);try{if(f==="wav"){let m=[],R=await ti(n.clients,v,w,{model:l,text:u,...y!==void 0?{voice:y}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??zs,returnBoundMs:n.turnReturnBoundMs??Ys},s,_=>m.push(_));if(R===null||s())return;let b=Buffer.concat(m);if(y===void 0){let _=R[tt];if(typeof _=="string"){if(!ri(_))throw oi(_);e.setHeader(yn,_)}}si(n.turn,b.byteLength),e.statusCode=200,e.setHeader("content-type","audio/wav"),e.end(Buffer.concat([sl(b.byteLength),b]))}else{let m=ul(e,n.turn,s,y===void 0),R;try{R=await ti(n.clients,v,w,{model:l,text:u,...y!==void 0?{voice:y}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??zs,returnBoundMs:n.turnReturnBoundMs??Ys},s,m.sink)}catch(b){let _=Be(b);if(!m.started())throw _;n.turn&&(n.turn.streamError=!0),m.fail(_);return}if(R===null)return;try{await m.finish(R)}catch(b){let _=Be(b);n.turn&&(n.turn.streamError=!0),m.fail(_);return}}}catch(m){throw Be(m)}}catch(i){if(i instanceof Q){wr(e,new L(413,i.message));return}if(i instanceof L){wr(e,i);return}throw i}}var wn="/v1/audio/transcriptions";function ai(t){return t===wn}var ui="/v1/audio/models",pl=16*1024*1024,fl=64*1024*1024,B=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}};function vr(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}function li(t){return t.length>=12&&t[0]===82&&t[1]===73&&t[2]===70&&t[3]===70&&t[8]===87&&t[9]===65&&t[10]===86&&t[11]===69}function ml(t){if(!li(t))return null;let e=12,n=null,r=null;for(;e+8<=t.length;){let s=String.fromCharCode(t[e],t[e+1],t[e+2],t[e+3]),i=(t[e+4]|t[e+5]<<8|t[e+6]<<16|t[e+7]<<24)>>>0,a=e+8;if(s==="fmt "&&a+16<=t.length){let u=t[a+2]|t[a+3]<<8,l=(t[a+4]|t[a+5]<<8|t[a+6]<<16|t[a+7]<<24)>>>0,d=t[a+14]|t[a+15]<<8;n={sampleRate:l,channels:u,bitsPerSample:d}}else if(s==="data"){let u=t.length-a;r=Math.min(i===0||i===4294967295?u:i,u)}e=a+i+i%2}if(n===null||r===null||r===0||n.sampleRate===0||n.channels===0||n.bitsPerSample===0)return null;let o=r/(n.sampleRate*n.channels*(n.bitsPerSample/8));return!Number.isFinite(o)||o<0?null:{seconds:o,sampleRate:n.sampleRate,channels:n.channels,bitsPerSample:n.bitsPerSample}}async function hl(t,e,n){if(t){if(t.model=oe({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}function gl(t,e){t&&(t.outputUnits=e.seconds)}async function yl(t,e,n,r,o){let s=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1,...r!==void 0?{language:r}:{}},o),i=[],a=null;for await(let u of s){if(i.push(u),u.type==="error"){let l=u.error??{},d=typeof l.type=="string"?l.type:"upstream_error",c=String(l.message??"upstream reported an error");throw new B(d==="invalid_request_error"?400:502,`transcription upstream error: ${c}`)}if(u.type==="response.completed"){a=u.response??null;break}}if(a===null)throw new B(502,"transcription upstream produced no terminal response \u2014 refusing to fabricate a transcript");return Xe(i,a)}function di(t){return t instanceof B?t:t instanceof N?new B(404,t.message):new B(502,`transcription upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function _l(t,e,n){let r;try{r=await t(e)}catch(o){throw di(o)}return r?!0:(vr(n,new B(404,e===void 0?"no transcription-capable default model is configured for this caller (catalog task stt) \u2014 name an stt model":`model "${e}" is not transcription-capable (catalog task is not stt) \u2014 this lane serves only speech-to-text models; tts models ride /v1/audio/speech and chat models ride /v1/chat/completions`)),!1)}var wl=new Set(["file","model","language","response_format"]);async function ci(t,e,n){let r=n.maxBodyBytes??fl,o=n.maxAudioPartBytes??pl;try{if(t.method!=="POST")throw new B(405,`${String(t.method)} is not supported on ${wn}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new B(400,`the transcriptions endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await mn(t,r)}catch(w){throw w instanceof Q?new B(413,w.message):new B(400,String(w instanceof Error?w.message:w))}let a=new Map;for(let w of i.keys()){if(!wl.has(w))throw new B(400,`form field "${w}" is not implemented: the native transcription request carries the audio clip and a language hint only (prompt, temperature, timestamps and similar knobs have no native surface on this lane). Supported fields: file, model, language, response_format.`,w);a.set(w,(a.get(w)??0)+1)}for(let[w,v]of a)if(v>1)throw new B(400,`form field "${w}" appears ${v} times: the lane reads exactly one value per field, and a repeat is refused rather than silently discarded \u2014 resend the request with each field once`,w);let u=i.get("model");if(u!==null&&typeof u!="string")throw new B(400,"model must be a string field","model");let l=u===null?void 0:u,d=i.get("language");if(d!==null&&(typeof d!="string"||d.trim().length===0))throw new B(400,"language, when present, must be a non-empty string","language");let c=d===null?void 0:d,y=i.get("response_format")??"json";if(y!=="json")throw new B(400,`response_format ${JSON.stringify(y)} is not supported: timestamps/segments are not produced on this lane yet \u2014 the runtime answers terminal transcript text only, so json is the only output format`,"response_format");let f=i.get("file");if(!(f instanceof File))throw new B(400,"file must be an uploaded WAV: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths","file");if(f.size>o)throw new B(413,`file part exceeds the ${o}-byte limit (the serve protocol's MAX_AUDIO_BYTES)`,"file");let p=new Uint8Array(await f.arrayBuffer());if(!li(p))throw new B(400,"the audio payload is not a RIFF/WAVE container \u2014 this lane accepts WAV uploads only (detected by magic bytes, not the declared Content-Type); the runtime decodes PCM16 WAV and nothing else","file");let g=ml(p);if(g===null)throw new B(400,"the WAV header cannot be read (no fmt /data chunk pair, or a zero rate/channel/depth) \u2014 the runtime decodes RIFF/WAVE PCM16 and this container cannot be honestly decoded or billed","file");if(await hl(n.turn,n.clients,l),n.stats?.request("audio_transcriptions",p.length,p.length),!await _l(n.transcriptionModels,l,e))return;let h=[{type:"input_audio",input_audio:{data:Buffer.from(p).toString("base64"),format:"wav"}}];try{let w=await yl(n.clients,l,h,c,n.turn?le(n.turn):void 0);gl(n.turn,g),e.statusCode=200,e.setHeader("content-type","application/json"),e.end(JSON.stringify({text:w}))}catch(w){throw di(w)}}catch(s){if(s instanceof Q){vr(e,new B(413,s.message));return}if(s instanceof B){vr(e,s);return}throw s}}var bn="/v1/usage/requests",pi=1e4,vl=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,Ie=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},H=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}},vn=class extends H{constructor(e){super(e),this.name="UsageWithholdingError"}},gi=1024*1024;function kr(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new Ie(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new Ie(`"${e}" must be an array of uuid strings`);if(n.length>pi)throw new Ie(`"${e}" carries ${n.length} ids; at most ${pi} may be looked up in one request. Split the batch \u2014 the ids past the limit are NOT silently dropped`);let r=[],o=new Set;for(let s of n){if(typeof s!="string"||!vl.test(s))throw new Ie(`"${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 yi(t){return kr(t,"inference_ids")}var bl=["inference_id","model_id","identity_kind","variant","task","http_status","started_at","completed_at","prompt_tokens","completion_tokens","output_units","output_minutes","cost_nano_usd","price_version","priced_at"];function nt(t,e,n){let r=t[e];if(typeof r!="string"||r==="")throw new H(`${n}: ${e} is not a non-empty string (got ${ot(r)})`);return r}function br(t,e,n){let r=t[e];if(r===null)return null;if(typeof r!="string"||r==="")throw new H(`${n}: ${e} is neither null nor a non-empty string (got ${ot(r)})`);return r}function rt(t,e,n,r){let o=t[e];if(o===null)return null;let s=typeof o=="number"?o:typeof o=="string"&&/^-?\d+(\.\d+)?$/.test(o.trim())?Number(o.trim()):NaN;if(!Number.isFinite(s))throw new H(`${n}: ${e} is not a number (got ${ot(o)})`);if(r&&!Number.isSafeInteger(s))throw new H(`${n}: ${e} is not a safely representable integer \u2014 a billing quantity that silently loses digits must fail rather than be reported`);if(s<0)throw new H(`${n}: ${e} is negative`);return Object.is(s,-0)?0:s}var Sl=40,fi=64,mi=600;function _i(t){let e=Object.keys(t).sort(),r=e.slice(0,Sl).map(o=>o.length>fi?`${o.slice(0,fi)}\u2026`:o).join(", ");return r.length>mi&&(r=`${r.slice(0,mi)}\u2026`),`${e.length} keys present: [${r}]`}function ot(t){return t===null?"null":t===void 0?"undefined":Array.isArray(t)?"an array":typeof t=="object"?"an object":`a ${typeof t}`}function Sr(t){Ae(process.stdout,`${JSON.stringify({event:"usage_row_rejected",ts:new Date().toISOString(),where:t.where,reason:t.reason})}
|
|
23
23
|
`)}function kl(t,e){let n;try{n=t(e)}catch{hi(t,e);return}Rl(n)&&Promise.resolve(n).catch(()=>{hi(t,e)})}function Rl(t){return typeof t?.then=="function"}function hi(t,e){if(t!==Sr)try{Sr(e)}catch{}}function wi(t,e=Sr){let n=t?.requests;if(!Array.isArray(n)){let o=t!==null&&typeof t=="object"&&!Array.isArray(t)?_i(t):ot(t);throw new H(`the usage surface answered without a "requests" array: got ${ot(n)} (${o})`)}let r=[];for(let o=0;o<n.length;o++){let s=`usage.requests[${o}]`;try{r.push(xl(n[o],s))}catch(i){if(i instanceof vn||!(i instanceof H))throw i;kl(e,{where:s,reason:i.message})}}if(n.length>0&&r.length===0)throw new H(`the usage surface returned ${n.length} row(s) and NONE of them could be validated \u2014 answering an empty result here would be indistinguishable from "we hold nothing for these ids", which a billing caller treats as final and writes the whole batch off unbilled`);return r}function xl(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new H(`${e} is not an object: got ${ot(t)}`);let n=t;if(Object.hasOwn(n,"gpu_seconds"))throw new vn(`${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 bl)if(!Object.hasOwn(n,o))throw new H(`${e} has no "${o}" column (${_i(n)})`);let r=rt(n,"http_status",e,!0);if(r===null)throw new H(`${e}: http_status is null, but the ledger column is NOT NULL`);return{inference_id:nt(n,"inference_id",e),model_id:nt(n,"model_id",e),identity_kind:nt(n,"identity_kind",e),variant:br(n,"variant",e),task:nt(n,"task",e),http_status:r,started_at:nt(n,"started_at",e),completed_at:nt(n,"completed_at",e),prompt_tokens:rt(n,"prompt_tokens",e,!0),completion_tokens:rt(n,"completion_tokens",e,!0),output_units:rt(n,"output_units",e,!1),output_minutes:rt(n,"output_minutes",e,!1),cost_nano_usd:rt(n,"cost_nano_usd",e,!0),price_version:br(n,"price_version",e),priced_at:br(n,"priced_at",e)}}function Sn(t,e){let n=new Set(e),r=new Set;for(let o=0;o<t.length;o++){let s=t[o];if(!n.has(s.inference_id))throw new H(`the usage surface returned an id at validated answer position ${o} that was not in the request (${e.length} asked, ${t.length} answered) \u2014 an answer carrying ids nobody asked about cannot be attributed to this caller`);if(r.has(s.inference_id))throw new H(`the usage surface returned ${s.inference_id} more than once \u2014 inference_id is the ledger primary key, so a duplicate means the upstream query is not the one we asked, and a caller that sums the entries would bill the request twice`);r.add(s.inference_id)}}var In=require("@urun-sh/core");var Rr=require("@urun-sh/core"),we=class extends Error{kind;code;retryable;constructor(e,n,r,o){super(n),this.name="DecisionStreamError",this.kind=e,this.code=r,this.retryable=o,Object.setPrototypeOf(this,new.target.prototype)}},xr=33e4;function vi(t,e=xr){return(async function*(){let r=t[Symbol.asyncIterator]();for(;;){let o,s=new Promise((l,d)=>{o=d}),i=setTimeout(()=>o(new kn),e),a;try{a=await Promise.race([r.next(),s])}catch(l){throw l instanceof q||l instanceof Rr.SessionFailedError&&l.kind==="transport-failed"?l:l instanceof kn?(await r.return?.(void 0),new we("upstream_idle_timeout",`decision lane produced no event for ${e}ms (upstream wedged) \u2014 the session is untouched; retry this decision`,"upstream_idle_timeout",!0)):new we("upstream_threw",`decision stream failed: ${String(l instanceof Error?l.message:l)}`,"upstream_threw",!1)}finally{clearTimeout(i)}if(a.done)return;let u=a.value;if(typeof u=="object"&&u!==null&&"type"in u&&u.type==="error")throw new we("upstream_error","decision stream ended with an upstream error event","upstream_error",!1);yield u}})()}var kn=class extends Error{constructor(){super("stream idle timeout")}};function bi(t){return t instanceof we?!1:t instanceof q?!0:t instanceof Rr.SessionFailedError?t.kind==="transport-failed":!1}var Pt="/partners/huggingface/v1/billing/requests";function El(t){return kr(t,"requestIds")}function Al(t){return t>=200&&t<=399}function Il(t){let e=[];for(let n of t)n.cost_nano_usd!==null&&Al(n.http_status)&&e.push({requestId:n.inference_id,costNanoUsd:n.cost_nano_usd});return{requests:e.length>0?e:null}}async function Si(t,e){let n=El(t),r=await e(n);return Sn(r,n),Il(r)}var Ol={"/v1/chat/completions":"conversational","/v1/responses":"conversational","/v1/messages":"conversational","/v1/systemone":"conversational","/v1/images/generations":"text-to-image","/v1/images/edits":"image-to-image","/v1/audio/speech":"text-to-speech","/v1/audio/transcriptions":"speech-to-text"};function Tl(t){return Ol[t]??null}var Ei=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function Pl(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function Cl(t){return t==="surface"}function Ll(t){let e=t?.ledgerFailureReason;return Ei.includes(e)?e:"unknown"}var Ml=["no_response_head","no_org","model_unresolved","no_model_named","queue_full"],ki=["priced","unpriced"],$l={written:ki,already_written:ki,skipped:Ml,failed:Ei},Ai=Object.entries($l).flatMap(([t,e])=>e.map(n=>({outcome:t,reason:n})));function Nl(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function Ul(t){let e=Date.parse(t.ts),n=Number.isFinite(t.duration_ms)?Math.max(0,t.duration_ms):0;return new Date(e-Math.round(n)).toISOString()}var Dl=256,Ri=4096,Lt=12e4,Fl=250,Bl=8e3,qe=[],xi=new Set,Mt=0,Ct=!1;function Ii(){return qe.length}function Oi(t=Date.now()){let e=qe[0];return e===void 0?0:Math.max(0,t-e.queuedAt)}var ql=new Set;function Hl(){return Mt===0&&qe.length===0}function jl(){if(Hl()){Ct=!1;for(let t of[...ql])t()}}function Ti(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
|
|
24
|
-
`}function Gl(t){Ae(process.stdout,t)}function Pi(t,e){try{t(e)}catch{}}function Ci(t){let{record:e,ledger:n}=t;if(n===null)return;let r=n.report??Gl,o=Tl(e.path);if(o===null)return;let s=d=>{try{n.observe(d)}catch{}},i=(d,c)=>{s({outcome:"skipped",reason:d}),Pi(r,Ti("inference_ledger_skipped",{inference_id:e.inference_id,path:e.path,reason:d,detail:c}))};if(e.status===null){i("no_response_head","no head flushed, so the caller never received an Inference-Id and no status can be recorded honestly");return}if(e.org===null){i("no_org","the request never resolved a tenant, so there is no org to attribute the row to");return}let a=e.catalog;if(a.kind==="unresolved"){i("model_unresolved",a.reason);return}let u;if(a.kind==="catalog")u={model_id:a.model_id,identity_kind:"catalog_model",variant:a.variant};else{if(a.model===null){i("no_model_named","the request resolved to a caller-org app but named no model, so there is no app slug to record and `model_id` cannot be written honestly");return}u={model_id:a.model,identity_kind:"caller_org_app",variant:null}}let l={inference_id:e.inference_id,...u,task:o,http_status:e.status,started_at:Ul(e),completed_at:e.ts,billable_outcome:Nl(e),session_id:e.session_id,prompt_tokens:e.prompt_tokens,completion_tokens:e.completion_tokens,output_units:e.output_units,output_minutes:e.output_minutes,queue_ms:e.queue_ms,prefill_ms:e.prefill_ms,decode_ms:e.decode_ms,total_ms:e.total_ms,ttft_ms:e.ttft_ms===null?null:Math.round(e.ttft_ms),e2e_ms:Math.round(e.duration_ms)};if(qe.length>=Ri){i("queue_full",`${qe.length} ledger writes are already waiting (capacity ${Ri}, ${Mt} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}qe.push({entry:l,req:t.req,record:e,ledger:n,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),Mt<Dl&&(Mt+=1,Vl())}async function Vl(){try{for(;;){let t=Jl();if(t===void 0)return;xi.add(t);try{await Wl(t)}finally{xi.delete(t)}}}finally{Mt-=1,jl()}}function Jl(){let t=Date.now();for(;;){let e=qe.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<Lt)return e;st(e,{outcome:"failed",reason:"dwell_expired"},{event:"inference_ledger_write_failed",reason:"dwell_expired",error:`this row waited ${n}ms for the control plane (bound ${Lt}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 Wl(t){let{entry:e,record:n,ledger:r}=t;for(;;){t.attempts+=1;try{let o=await r.write(t.req,e);st(t,{outcome:"written",reason:o.cost_nano_usd===null?"unpriced":"priced"},{event:"inference_ledger_written",inference_id:o.inference_id,path:n.path,model_id:e.model_id,identity_kind:e.identity_kind,variant:e.variant,org:n.org,session_id:e.session_id,cost_nano_usd:o.cost_nano_usd,price_version:o.price_version,unpriced_reason:o.unpriced_reason,attempts:t.attempts});return}catch(o){let s=Pl(o);if(s!==null){st(t,{outcome:"already_written",reason:s.cost_nano_usd===null?"unpriced":"priced"},{event:"inference_ledger_already_written",inference_id:e.inference_id,model_id:e.model_id,identity_kind:e.identity_kind,variant:e.variant,cost_nano_usd:s.cost_nano_usd,price_version:s.price_version,attempts:t.attempts});return}let i=Ll(o),a=Date.now()-t.queuedAt;if(Ct){st(t,{outcome:"failed",reason:"dropped_on_exit"},{event:"inference_ledger_write_failed",reason:"dropped_on_exit",attempts:t.attempts,waited_ms:a,error:`the proxy is shutting down and this row was still being retried when the drain ran out \u2014 the request was served and answered, and its billing row was never written. Last failure: ${o instanceof Error?o.message:String(o)}`});return}if(Cl(i)&&a<Lt){if(await zl(Kl(t.attempts)),Ct||Date.now()-t.queuedAt>=Lt){let u=Date.now()-t.queuedAt;st(t,{outcome:"failed",reason:Ct?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:Ct?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:u,error:`this row's backoff outlived the ${Lt}ms dwell bound (waited ${u}ms over ${t.attempts} attempt(s)) and was given up on rather than starting an attempt that could only finish past it. Last failure: ${o instanceof Error?o.message:String(o)}`});return}continue}st(t,{outcome:"failed",reason:i},{event:"inference_ledger_write_failed",reason:i,attempts:t.attempts,waited_ms:a,error:o instanceof Error?o.message:String(o)});return}}}function Kl(t){return Math.min(Bl,Fl*2**Math.max(0,t-1))}function zl(t){return new Promise(e=>{setTimeout(e,t).unref?.()})}function st(t,e,n){if(t.settled)return;t.settled=!0;try{t.ledger.observe(e)}catch{}let{event:r,...o}=n;Pi(t.report,Ti(r,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...o}))}var Ar="x-urun-profile",$t="voice";var ie=class extends Error{status=400},Yl=/(?:[.!?:;]|\n)[)\]"'\u2019\u201d\u00bb\u300d\u300b\u3011\uff09\uff3d]?\s*$/,Xl=/\s/;function Ql(t){return Yl.test(t)}function Er(t,e,n){let r=t[e];if(r===void 0)return n;if(typeof r!="boolean")throw new ie(`stream_options.voice.${e} must be a boolean when present (got ${typeof r})`);return r}function Li(t,e,n,r={}){let o=t[e];if(o===void 0)return n;if(typeof o!="number"||!Number.isFinite(o)||o<=0)throw new ie(`stream_options.voice.${e} must be a positive finite number when present (got ${JSON.stringify(o)})`);if(r.min!==void 0&&o<r.min)throw new ie(`stream_options.voice.${e} must be at least ${r.min} when present (got ${JSON.stringify(o)})${r.minReason?` \u2014 ${r.minReason}`:""}`);if(r.max!==void 0&&o>r.max)throw new ie(`stream_options.voice.${e} must be at most ${r.max} when present (got ${JSON.stringify(o)}) \u2014 larger values wrap Node's setTimeout to a 1ms delay`);return o}function Mi(t,e){let n=Array.isArray(t)?t[0]:t,r=e.stream_options??void 0;if(r!==void 0&&(typeof r!="object"||Array.isArray(r)))throw new ie(`stream_options must be a JSON object when present, e.g. {"profile":"voice","voice":{...}} (got ${JSON.stringify(r)})`);let o=r,s=o?.profile,i=!1;if(n!=null){let l=n.trim();if(l==="")throw new ie(`the ${Ar} header is empty \u2014 the only profile this lane accepts is "${$t}" (omit the header for the ordinary lane)`);if(l!==$t)throw new ie(`unknown profile "${l}" \u2014 the only profile this lane accepts is "${$t}"`);i=!0}if(s!==void 0){if(typeof s!="string"||s!==$t)throw new ie(`unknown profile ${JSON.stringify(s)} \u2014 the only profile this lane accepts is "${$t}"`);i=!0}if(!i)return null;if(e.stream!==!0)throw new ie("the voice profile is streaming-only \u2014 send stream: true");let a=o?.voice;if(a!==void 0&&(typeof a!="object"||a===null||Array.isArray(a)))throw new ie("stream_options.voice must be an object when present (flush_max_chars, flush_max_ms, sentence_flush, include_reasoning, metadata)");let u=a??{};return{flushMaxChars:Li(u,"flush_max_chars",120,{min:2,minReason:"a hard split at a bound below 2 cannot keep UTF-16 surrogate pairs (astral characters) intact"}),flushMaxMs:Li(u,"flush_max_ms",250,{max:2147483647}),sentenceFlush:Er(u,"sentence_flush",!0),includeReasoning:Er(u,"include_reasoning",!1),metadata:Er(u,"metadata",!1)}}var Rn=class{constructor(e,n){this.config=e;this.sink=n}config;sink;buffer="";timer=null;push(e){e!==""&&(this.buffer===""&&(this.timer=setTimeout(()=>{this.timer=null,this.flush()},this.config.flushMaxMs)),this.buffer+=e,this.drain())}flush(){if(this.clearTimer(),this.buffer==="")return;let e=this.buffer;this.buffer="",this.sink(e)}dispose(){this.clearTimer()}drain(){let e=this.config.flushMaxChars;for(;this.buffer.length>e;){let n=this.boundedCut();this.sink(this.buffer.slice(0,n)),this.buffer=this.buffer.slice(n)}(this.buffer.length>=e||this.config.sentenceFlush&&Ql(this.buffer))&&this.flush(),this.buffer===""&&this.clearTimer()}boundedCut(){let e=Math.min(Math.floor(this.config.flushMaxChars),this.buffer.length);for(let o=e-1;o>0;o--)if(Xl.test(this.buffer[o]))return o+1;let n=Math.max(1,e),r=this.buffer.charCodeAt(n-1);return r>=55296&&r<=56319?n-1:n}clearTimer(){this.timer!==null&&(clearTimeout(this.timer),this.timer=null)}};function Tr(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function ed(t){t.writeHead(200,{"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache",connection:"keep-alive","x-accel-buffering":"no"}),t.flushHeaders(),t.socket?.setNoDelay(!0)}function te(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function K(t,e,n,r="invalid_request_error",o){te(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function Pr(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);te(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function me(t,e,n,r){te(t,e,{type:"error",error:{type:n,message:r}})}function it(t,e,n){if(e==="anthropic"){te(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}te(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var td=64*1024*1024,Nt=class extends Error{};async function $i(t,e=td){let n=[],r=0;for await(let s of t){if(r+=s.length,r>e)throw new Nt(`request body exceeds ${e} bytes`);n.push(s)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function He(t,e,n,r){return{id:t,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:e,choices:[{index:0,delta:n,finish_reason:r}]}}function Ui(t){let e=t?.usage;if(!e||typeof e!="object")return{kind:"absent"};let n=e,r=u=>typeof u=="number"&&Number.isFinite(u)&&u>=0?u:null,o=r(n.input_tokens),s=r(n.output_tokens),i=u=>u??0,a=r(n.total_tokens)??i(o)+i(s);if(i(o)>0||i(s)>0){let u={input_tokens:i(o),output_tokens:i(s),total_tokens:a};return o===null||s===null?{kind:"partial",usage:u}:{kind:"counts",usage:u}}return o===null||s===null?{kind:"absent"}:{kind:"zero",usage:{input_tokens:0,output_tokens:0,total_tokens:a}}}function xn(t){let e=Ui(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function Ni(t){let e=xn(t);return e?{prompt_tokens:e.input_tokens,completion_tokens:e.output_tokens,total_tokens:e.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function nd(t,e){let n=Ui(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function rd(t){let e=t?.urun_timing;if(!e||typeof e!="object")return null;let n=e,r=u=>{let l=n[u];return typeof l=="number"&&Number.isSafeInteger(l)&&l>=0?l: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 od(t,e){let n=rd(e);n&&(t.timing=n)}function je(t,e){nd(t,e),od(t,e)}async function On(t,e,n){if(typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}function Di(t){let e=t?.output;if(!Array.isArray(e))return"";let n="";for(let r of e)if(!(r.type!=="reasoning"||!Array.isArray(r.content)))for(let o of r.content)o.type==="reasoning_text"&&(n+=String(o.text??""));return n}function sd(t){if(Array.isArray(t))return t.map(e=>{let n=e.function;return!n||typeof n!="object"?e:{type:"function",name:n.name,description:n.description,parameters:n.parameters,strict:n.strict}})}var En=class extends Error{};function id(t,e){return Array.isArray(e)?e.map(n=>{let r=n;if(r.type==="text")return{type:t==="assistant"?"output_text":"input_text",text:r.text};if(r.type==="image_url"){let o=r.image_url??{};return{type:"input_image",image_url:o.url,detail:o.detail}}throw new En(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function ad(t){let e=[];for(let n of t){if(n.role==="tool"){e.push({type:"function_call_output",call_id:n.tool_call_id,output:typeof n.content=="string"?n.content:JSON.stringify(n.content??"")});continue}if(n.role==="assistant"&&Array.isArray(n.tool_calls)){typeof n.content=="string"&&n.content&&e.push({role:"assistant",content:n.content});for(let r of n.tool_calls){let o=r.function??{};e.push({type:"function_call",call_id:r.id,name:o.name,arguments:o.arguments})}continue}e.push({...n,content:id(n.role,n.content)})}return e}function Ir(t){return ze(t).map((e,n)=>({index:n,id:e.call_id??e.id??`call_${n}`,type:"function",function:{name:e.name??"",arguments:e.arguments??"{}"}}))}async function ud(t,e,n,r,o,s){let i=St(s.id),a=t.stream===!0;if(s.model=oe(t),s.stream=a,await On(s,e,typeof t.model=="string"?t.model:void 0),a){let _;try{_=await Rt({body:t,clients:e,store:n,stats:o,responseId:i,turn:s})}catch(v){if(v instanceof N){it(r,"openai",v);return}if(v instanceof de){K(r,v.status,v.message);return}throw v}Tr(r);for await(let v of _.events)v.type==="error"&&(s.streamError=!0),v.type==="response.completed"&&je(s,v.response),r.write(`event: ${String(v.type??"message")}
|
|
24
|
+
`}function Gl(t){Ae(process.stdout,t)}function Pi(t,e){try{t(e)}catch{}}function Ci(t){let{record:e,ledger:n}=t;if(n===null)return;let r=n.report??Gl,o=Tl(e.path);if(o===null)return;let s=d=>{try{n.observe(d)}catch{}},i=(d,c)=>{s({outcome:"skipped",reason:d}),Pi(r,Ti("inference_ledger_skipped",{inference_id:e.inference_id,path:e.path,reason:d,detail:c}))};if(e.status===null){i("no_response_head","no head flushed, so the caller never received an Inference-Id and no status can be recorded honestly");return}if(e.org===null){i("no_org","the request never resolved a tenant, so there is no org to attribute the row to");return}let a=e.catalog;if(a.kind==="unresolved"){i("model_unresolved",a.reason);return}let u;if(a.kind==="catalog")u={model_id:a.model_id,identity_kind:"catalog_model",variant:a.variant};else{if(a.model===null){i("no_model_named","the request resolved to a caller-org app but named no model, so there is no app slug to record and `model_id` cannot be written honestly");return}u={model_id:a.model,identity_kind:"caller_org_app",variant:null}}let l={inference_id:e.inference_id,...u,task:o,http_status:e.status,started_at:Ul(e),completed_at:e.ts,billable_outcome:Nl(e),session_id:e.session_id,prompt_tokens:e.prompt_tokens,completion_tokens:e.completion_tokens,output_units:e.output_units,output_minutes:e.output_minutes,queue_ms:e.queue_ms,prefill_ms:e.prefill_ms,decode_ms:e.decode_ms,total_ms:e.total_ms,ttft_ms:e.ttft_ms===null?null:Math.round(e.ttft_ms),e2e_ms:Math.round(e.duration_ms)};if(qe.length>=Ri){i("queue_full",`${qe.length} ledger writes are already waiting (capacity ${Ri}, ${Mt} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}qe.push({entry:l,req:t.req,record:e,ledger:n,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),Mt<Dl&&(Mt+=1,Vl())}async function Vl(){try{for(;;){let t=Jl();if(t===void 0)return;xi.add(t);try{await Wl(t)}finally{xi.delete(t)}}}finally{Mt-=1,jl()}}function Jl(){let t=Date.now();for(;;){let e=qe.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<Lt)return e;st(e,{outcome:"failed",reason:"dwell_expired"},{event:"inference_ledger_write_failed",reason:"dwell_expired",error:`this row waited ${n}ms for the control plane (bound ${Lt}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 Wl(t){let{entry:e,record:n,ledger:r}=t;for(;;){t.attempts+=1;try{let o=await r.write(t.req,e);st(t,{outcome:"written",reason:o.cost_nano_usd===null?"unpriced":"priced"},{event:"inference_ledger_written",inference_id:o.inference_id,path:n.path,model_id:e.model_id,identity_kind:e.identity_kind,variant:e.variant,org:n.org,session_id:e.session_id,cost_nano_usd:o.cost_nano_usd,price_version:o.price_version,unpriced_reason:o.unpriced_reason,attempts:t.attempts});return}catch(o){let s=Pl(o);if(s!==null){st(t,{outcome:"already_written",reason:s.cost_nano_usd===null?"unpriced":"priced"},{event:"inference_ledger_already_written",inference_id:e.inference_id,model_id:e.model_id,identity_kind:e.identity_kind,variant:e.variant,cost_nano_usd:s.cost_nano_usd,price_version:s.price_version,attempts:t.attempts});return}let i=Ll(o),a=Date.now()-t.queuedAt;if(Ct){st(t,{outcome:"failed",reason:"dropped_on_exit"},{event:"inference_ledger_write_failed",reason:"dropped_on_exit",attempts:t.attempts,waited_ms:a,error:`the proxy is shutting down and this row was still being retried when the drain ran out \u2014 the request was served and answered, and its billing row was never written. Last failure: ${o instanceof Error?o.message:String(o)}`});return}if(Cl(i)&&a<Lt){if(await zl(Kl(t.attempts)),Ct||Date.now()-t.queuedAt>=Lt){let u=Date.now()-t.queuedAt;st(t,{outcome:"failed",reason:Ct?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:Ct?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:u,error:`this row's backoff outlived the ${Lt}ms dwell bound (waited ${u}ms over ${t.attempts} attempt(s)) and was given up on rather than starting an attempt that could only finish past it. Last failure: ${o instanceof Error?o.message:String(o)}`});return}continue}st(t,{outcome:"failed",reason:i},{event:"inference_ledger_write_failed",reason:i,attempts:t.attempts,waited_ms:a,error:o instanceof Error?o.message:String(o)});return}}}function Kl(t){return Math.min(Bl,Fl*2**Math.max(0,t-1))}function zl(t){return new Promise(e=>{setTimeout(e,t).unref?.()})}function st(t,e,n){if(t.settled)return;t.settled=!0;try{t.ledger.observe(e)}catch{}let{event:r,...o}=n;Pi(t.report,Ti(r,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...o}))}var Ar="x-urun-profile",$t="voice";var ie=class extends Error{status=400},Yl=/(?:[.!?:;]|\n)[)\]"'\u2019\u201d\u00bb\u300d\u300b\u3011\uff09\uff3d]?\s*$/,Xl=/\s/;function Ql(t){return Yl.test(t)}function Er(t,e,n){let r=t[e];if(r===void 0)return n;if(typeof r!="boolean")throw new ie(`stream_options.voice.${e} must be a boolean when present (got ${typeof r})`);return r}function Li(t,e,n,r={}){let o=t[e];if(o===void 0)return n;if(typeof o!="number"||!Number.isFinite(o)||o<=0)throw new ie(`stream_options.voice.${e} must be a positive finite number when present (got ${JSON.stringify(o)})`);if(r.min!==void 0&&o<r.min)throw new ie(`stream_options.voice.${e} must be at least ${r.min} when present (got ${JSON.stringify(o)})${r.minReason?` \u2014 ${r.minReason}`:""}`);if(r.max!==void 0&&o>r.max)throw new ie(`stream_options.voice.${e} must be at most ${r.max} when present (got ${JSON.stringify(o)}) \u2014 larger values wrap Node's setTimeout to a 1ms delay`);return o}function Mi(t,e){let n=Array.isArray(t)?t[0]:t,r=e.stream_options??void 0;if(r!==void 0&&(typeof r!="object"||Array.isArray(r)))throw new ie(`stream_options must be a JSON object when present, e.g. {"profile":"voice","voice":{...}} (got ${JSON.stringify(r)})`);let o=r,s=o?.profile,i=!1;if(n!=null){let l=n.trim();if(l==="")throw new ie(`the ${Ar} header is empty \u2014 the only profile this lane accepts is "${$t}" (omit the header for the ordinary lane)`);if(l!==$t)throw new ie(`unknown profile "${l}" \u2014 the only profile this lane accepts is "${$t}"`);i=!0}if(s!==void 0){if(typeof s!="string"||s!==$t)throw new ie(`unknown profile ${JSON.stringify(s)} \u2014 the only profile this lane accepts is "${$t}"`);i=!0}if(!i)return null;if(e.stream!==!0)throw new ie("the voice profile is streaming-only \u2014 send stream: true");let a=o?.voice;if(a!==void 0&&(typeof a!="object"||a===null||Array.isArray(a)))throw new ie("stream_options.voice must be an object when present (flush_max_chars, flush_max_ms, sentence_flush, include_reasoning, metadata)");let u=a??{};return{flushMaxChars:Li(u,"flush_max_chars",120,{min:2,minReason:"a hard split at a bound below 2 cannot keep UTF-16 surrogate pairs (astral characters) intact"}),flushMaxMs:Li(u,"flush_max_ms",250,{max:2147483647}),sentenceFlush:Er(u,"sentence_flush",!0),includeReasoning:Er(u,"include_reasoning",!1),metadata:Er(u,"metadata",!1)}}var Rn=class{constructor(e,n){this.config=e;this.sink=n}config;sink;buffer="";timer=null;push(e){e!==""&&(this.buffer===""&&(this.timer=setTimeout(()=>{this.timer=null,this.flush()},this.config.flushMaxMs)),this.buffer+=e,this.drain())}flush(){if(this.clearTimer(),this.buffer==="")return;let e=this.buffer;this.buffer="",this.sink(e)}dispose(){this.clearTimer()}drain(){let e=this.config.flushMaxChars;for(;this.buffer.length>e;){let n=this.boundedCut();this.sink(this.buffer.slice(0,n)),this.buffer=this.buffer.slice(n)}(this.buffer.length>=e||this.config.sentenceFlush&&Ql(this.buffer))&&this.flush(),this.buffer===""&&this.clearTimer()}boundedCut(){let e=Math.min(Math.floor(this.config.flushMaxChars),this.buffer.length);for(let o=e-1;o>0;o--)if(Xl.test(this.buffer[o]))return o+1;let n=Math.max(1,e),r=this.buffer.charCodeAt(n-1);return r>=55296&&r<=56319?n-1:n}clearTimer(){this.timer!==null&&(clearTimeout(this.timer),this.timer=null)}};function Tr(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function ed(t){t.writeHead(200,{"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache",connection:"keep-alive","x-accel-buffering":"no"}),t.flushHeaders(),t.socket?.setNoDelay(!0)}function te(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function K(t,e,n,r="invalid_request_error",o){te(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function Pr(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);te(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function me(t,e,n,r){te(t,e,{type:"error",error:{type:n,message:r}})}function it(t,e,n){if(e==="anthropic"){te(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}te(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var td=64*1024*1024,Nt=class extends Error{};async function $i(t,e=td){let n=[],r=0;for await(let s of t){if(r+=s.length,r>e)throw new Nt(`request body exceeds ${e} bytes`);n.push(s)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function He(t,e,n,r){return{id:t,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:e,choices:[{index:0,delta:n,finish_reason:r}]}}function Ui(t){let e=t?.usage;if(!e||typeof e!="object")return{kind:"absent"};let n=e,r=u=>typeof u=="number"&&Number.isFinite(u)&&u>=0?u:null,o=r(n.input_tokens),s=r(n.output_tokens),i=u=>u??0,a=r(n.total_tokens)??i(o)+i(s);if(i(o)>0||i(s)>0){let u={input_tokens:i(o),output_tokens:i(s),total_tokens:a};return o===null||s===null?{kind:"partial",usage:u}:{kind:"counts",usage:u}}return o===null||s===null?{kind:"absent"}:{kind:"zero",usage:{input_tokens:0,output_tokens:0,total_tokens:a}}}function xn(t){let e=Ui(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function Ni(t){let e=xn(t);return e?{prompt_tokens:e.input_tokens,completion_tokens:e.output_tokens,total_tokens:e.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function nd(t,e){let n=Ui(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function rd(t){let e=t?.urun_timing;if(!e||typeof e!="object")return null;let n=e,r=u=>{let l=n[u];return typeof l=="number"&&Number.isSafeInteger(l)&&l>=0?l: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 od(t,e){let n=rd(e);n&&(t.timing=n)}function je(t,e){nd(t,e),od(t,e)}async function On(t,e,n){if(typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}function Di(t){let e=t?.output;if(!Array.isArray(e))return"";let n="";for(let r of e)if(!(r.type!=="reasoning"||!Array.isArray(r.content)))for(let o of r.content)o.type==="reasoning_text"&&(n+=String(o.text??""));return n}function sd(t){if(Array.isArray(t))return t.map(e=>{let n=e.function;return!n||typeof n!="object"?e:{type:"function",name:n.name,description:n.description,parameters:n.parameters,strict:n.strict}})}var En=class extends Error{};function id(t,e){return Array.isArray(e)?e.map(n=>{let r=n;if(r.type==="text")return{type:t==="assistant"?"output_text":"input_text",text:r.text};if(r.type==="image_url"){let o=r.image_url??{};return{type:"input_image",image_url:o.url,detail:o.detail}}throw new En(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function ad(t){let e=[];for(let n of t){if(n.role==="tool"){e.push({type:"function_call_output",call_id:n.tool_call_id,output:typeof n.content=="string"?n.content:JSON.stringify(n.content??"")});continue}if(n.role==="assistant"&&Array.isArray(n.tool_calls)){typeof n.content=="string"&&n.content&&e.push({role:"assistant",content:n.content});for(let r of n.tool_calls){let o=r.function??{};e.push({type:"function_call",call_id:r.id,name:o.name,arguments:o.arguments})}continue}e.push({...n,content:id(n.role,n.content)})}return e}function Ir(t){return ze(t).map((e,n)=>({index:n,id:e.call_id??e.id??`call_${n}`,type:"function",function:{name:e.name??"",arguments:e.arguments??"{}"}}))}async function ud(t,e,n,r,o,s){let i=St(s.id),a=t.stream===!0;if(s.model=oe(t),s.stream=a,await On(s,e,typeof t.model=="string"?t.model:void 0),a){let w;try{w=await Rt({body:t,clients:e,store:n,stats:o,responseId:i,turn:s})}catch(v){if(v instanceof N){it(r,"openai",v);return}if(v instanceof de){K(r,v.status,v.message);return}throw v}Tr(r);for await(let v of w.events)v.type==="error"&&(s.streamError=!0),v.type==="response.completed"&&je(s,v.response),r.write(`event: ${String(v.type??"message")}
|
|
25
25
|
data: ${JSON.stringify(v)}
|
|
26
26
|
|
|
27
27
|
`);r.write(`data: [DONE]
|
|
28
28
|
|
|
29
|
-
`),r.end();return}let u;try{u=Qe(n,t)}catch(
|
|
29
|
+
`),r.end();return}let u;try{u=Qe(n,t)}catch(w){if(w instanceof de){K(r,w.status,w.message);return}throw w}t.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(u))-Buffer.byteLength(JSON.stringify(t.input)));let l=t.store!==!1,d=oe(t);o.model(d);let c=Array.isArray(t.tools)&&t.tools.length>0,y;try{y=await sr(e,t,u,!1,le(s))}catch(w){if(w instanceof N){it(r,"openai",w);return}if(w instanceof de){K(r,w.status,w.message);return}throw w}let f=[],p=null;for await(let w of o.track(y,d,s)){if(f.push(w),w.type==="error"){Pr(r,w);return}w.type==="response.completed"&&(p=w.response)}if(p==null){K(r,502,"upstream produced no response.completed event","upstream_error");return}je(s,p);let g=Xe(f,p),h=De(c,ze(p).length,g);if(h){K(r,502,Ue(d,h),"upstream_error");return}l&&n.remember(i,u,or(p,g),l),te(r,200,{...p,id:i})}async function ld(t,e,n,r,o,s){let i=t.messages;if(!Array.isArray(i)){K(n,400,"chat/completions requires a messages array");return}let a;try{a=Mi(s,t)}catch(x){if(x instanceof ie){K(n,x.status,x.message);return}throw x}let u=String(t.model??"urun"),l=_s(o.id),d;try{d=ad(i)}catch(x){if(x instanceof En){K(n,400,x.message);return}throw x}let c=oe(t);r.model(c),o.model=c,o.stream=t.stream===!0,await On(o,e,typeof t.model=="string"?t.model:void 0);let y=Array.isArray(t.tools)&&t.tools.length>0,f;try{f=await e.createResponse({model:t.model,input:d,stream:!0,tools:sd(t.tools),tool_choice:t.tool_choice,temperature:t.temperature,top_p:t.top_p,stop:t.stop,max_output_tokens:t.max_completion_tokens??t.max_tokens,...t.reasoning_effort!==void 0?{reasoning_effort:t.reasoning_effort}:{},...t.chat_template_kwargs!==void 0?{chat_template_kwargs:t.chat_template_kwargs}:{},...t.response_format!==void 0?{response_format:t.response_format}:{}},le(o))}catch(x){if(x instanceof N){it(n,"openai",x);return}throw x}if(t.stream===!0){a?ed(n):Tr(n);let x=[],O=new xe,P="",D=a?new Rn(a,re=>{n.write(`data: ${JSON.stringify(He(l,u,{content:re},null))}
|
|
30
30
|
|
|
31
31
|
`)}):null,X=re=>{D?.flush(),n.write(re),n.end()};try{a?.metadata&&n.write(`data: ${JSON.stringify({object:"chat.completion.metadata",id:l,model:u,interruptible:!0,tts:{sample_rate_hz:24e3,voice_hint:null}})}
|
|
32
32
|
|
|
@@ -50,10 +50,10 @@ data: ${JSON.stringify(v)}
|
|
|
50
50
|
|
|
51
51
|
`),n.write(`data: [DONE]
|
|
52
52
|
|
|
53
|
-
`),n.end();return}finally{D?.flush(),D?.dispose()}}let p="",g="",h=[],
|
|
53
|
+
`),n.end();return}finally{D?.flush(),D?.dispose()}}let p="",g="",h=[],w=new xe;for await(let x of r.track(f,c,o)){h.push(x),x.type==="response.output_text.delta"&&typeof x.delta=="string"&&(p+=x.delta),x.type==="response.reasoning_text.delta"&&typeof x.delta=="string"&&(g+=x.delta);let O=Ye(x);if(O&&w.add(O),x.type==="error"){Pr(n,x);return}}let v=fe(h);je(o,v);let m=Ir(v),R=m.length>0?m:Ir({output:w.items()}),b=De(y,R.length,p);if(b){K(n,502,Ue(c,b),"upstream_error");return}let _=R.map(({index:x,...O})=>O),S={role:"assistant",content:p||null},T=g||Di(v);T&&(S.reasoning_content=T),_.length>0&&(S.tool_calls=_),te(n,200,{id:l,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:u,choices:[{index:0,message:S,finish_reason:_.length>0?"tool_calls":"stop"}],usage:Ni(v)})}var Cr="sense-latest";async function dd(t,e,n,r,o){if(t===null||typeof t!="object"||Array.isArray(t)){K(n,400,"systemone requires a JSON object body");return}delete t.key;let s=typeof t.model=="string"&&t.model?t.model:Cr,i=oe({model:s});if(o.model=i,o.stream=!1,t.state===void 0||t.state===null){K(n,400,"systemone requires a state (string or JSON structure)");return}let a=t.questions;if(a===null||typeof a!="object"||Array.isArray(a)||Object.keys(a).length===0){K(n,400,"systemone requires a questions object (question id -> {kind, options, ...})");return}if(t.media!==void 0&&!Array.isArray(t.media)){K(n,400,"systemone media, when present, must be an array");return}let u={resolve_ms:null,dial_ms:null,first_event_ms:null,stream_ms:null,question_fwd_ms:null,head_ms:null};o.decision=u;let l=performance.now();r.model(i),await On(o,e,s),u.resolve_ms=Math.round(performance.now()-l);let d,c=performance.now();try{d=await e.createResponse({model:s,input:[{role:"user",content:JSON.stringify(t)}],stream:!0},le(o))}catch(w){if(w instanceof N){it(n,"openai",w);return}if(w instanceof Fe){n.setHeader("retry-after","2"),K(n,503,w.message,"capacity_error");return}throw w}u.dial_ms=Math.round(performance.now()-c);let y=[],f=null;try{for await(let w of r.track(d,i,o))if(f===null&&(f=performance.now(),u.first_event_ms=Math.round(f-c)),y.push(w),w.type==="error"){Pr(n,w);return}}catch(w){if(w instanceof N){f!==null&&(u.stream_ms=Math.round(performance.now()-f)),it(n,"openai",w);return}if(w instanceof Fe){f!==null&&(u.stream_ms=Math.round(performance.now()-f)),n.setHeader("retry-after","2"),K(n,503,w.message,"capacity_error");return}throw f!==null&&(u.stream_ms=Math.round(performance.now()-f)),w}f!==null&&(u.stream_ms=Math.round(performance.now()-f));let p=fe(y);if(p==null){K(n,502,"upstream produced no response.completed event","upstream_error");return}je(o,p);let g=Xe(y,p),h;try{h=JSON.parse(g)}catch{K(n,502,"systemone answer was not valid JSON","upstream_error");return}if(h===null||typeof h!="object"||Array.isArray(h)){K(n,502,"systemone answer was not a JSON object","upstream_error");return}if(typeof h=="object"&&h!==null&&!Array.isArray(h)&&"timing"in h){let w=h.timing;if(typeof w=="object"&&w!==null&&!Array.isArray(w)&&"question_fwd_ms"in w&&"head_ms"in w){let v=m=>typeof m=="number"&&Number.isFinite(m)&&m>=0?Math.round(m):null;u.question_fwd_ms=v(w.question_fwd_ms),u.head_ms=v(w.head_ms)}}te(n,200,h)}function cd(t){return typeof t=="string"?t:Array.isArray(t)?t.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var Ut=class extends Error{};function An(t){return new Ut(`unsupported anthropic content block type "${t}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function pd(t){if(Array.isArray(t))return t.map(e=>{let n=e;return{type:"function",name:n.name,description:n.description,parameters:n.input_schema}})}function fd(t){if(t==null)return;let e=t.type;if(e==="auto")return"auto";if(e==="none")return"none";if(e==="any")return"required";if(e==="tool")return{type:"function",name:t.name};throw new Ut(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function md(t){if(typeof t=="string")return t;if(t==null)return"";if(!Array.isArray(t))throw An(typeof t);let e="";for(let n of t){let r=String(n.type??"");if(r!=="text")throw An(`tool_result > ${r}`);e+=String(n.text??"")}return e}function hd(t){let{role:e,content:n}=t;if(typeof n=="string")return[{role:e,content:n}];if(n==null)return[];if(!Array.isArray(n))throw An(typeof n);let r=[],o="",s=()=>{o&&(r.push({role:e,content:o}),o="")};for(let i of n){let a=String(i.type??"");if(a==="text")o+=String(i.text??"");else if(a==="tool_use")s(),r.push({type:"function_call",call_id:i.id,name:i.name,arguments:JSON.stringify(i.input??{})});else if(a==="tool_result")s(),r.push({type:"function_call_output",call_id:i.tool_use_id,output:md(i.content)});else{if(a==="thinking"||a==="redacted_thinking")continue;throw An(a)}}return s(),r}function Or(t){return ze(t).map((e,n)=>{let r;try{r=JSON.parse(e.arguments||"{}")}catch{throw new Error(`upstream function_call "${e.name??""}" arguments are not valid JSON`)}return{type:"tool_use",id:e.call_id??e.id??`toolu_${n}`,name:e.name??"",input:r}})}async function gd(t,e,n,r,o){let s=t.messages;if(!Array.isArray(s)){me(n,400,"invalid_request_error","messages requires a messages array");return}if(typeof t.max_tokens!="number"||!Number.isInteger(t.max_tokens)||t.max_tokens<1){me(n,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(t.top_k!==void 0){me(n,400,"invalid_request_error","top_k is not supported: the uRun serve lane exposes temperature/top_p/stop_sequences only");return}if(t.stop_sequences!==void 0&&(!Array.isArray(t.stop_sequences)||t.stop_sequences.some(S=>typeof S!="string"))){me(n,400,"invalid_request_error","stop_sequences must be an array of strings");return}let i=String(t.model??"urun"),a=ws(o.id),u=[],l;try{t.system&&u.push({role:"system",content:cd(t.system)});for(let S of s)u.push(...hd(S));l=fd(t.tool_choice)}catch(S){if(S instanceof Ut){me(n,400,"invalid_request_error",S.message);return}throw S}let d=oe(t);r.model(d),o.model=d,o.stream=t.stream===!0,await On(o,e,typeof t.model=="string"?t.model:void 0);let c=Array.isArray(t.tools)&&t.tools.length>0,y;try{y=await e.createResponse({model:t.model,input:u,stream:!0,tools:pd(t.tools),tool_choice:l,temperature:t.temperature,max_output_tokens:t.max_tokens,top_p:t.top_p,stop:t.stop_sequences},le(o))}catch(S){if(S instanceof N){it(n,"anthropic",S);return}throw S}let f=(S,T)=>{n.destroyed||n.write(`event: ${S}
|
|
54
54
|
data: ${JSON.stringify({type:S,...T})}
|
|
55
55
|
|
|
56
|
-
`)};if(t.stream===!0){let S=r.track(y,d,o);Tr(n),f("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:i,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let T=0,x="",O=0,P="none",D=()=>P,X=-1,re=-1,pe=new xe,ve=[],V=()=>{P!=="none"&&(P="none",f("content_block_stop",{index:X}))},J=(F,ee)=>{V(),P=F,X=O++,f("content_block_start",{index:X,content_block:ee})};for await(let F of S){if(n.destroyed)break;ve.push(F);let ee=Ye(F);if(F.type==="response.reasoning_text.delta"&&typeof F.delta=="string")D()!=="thinking"&&J("thinking",{type:"thinking",thinking:""}),f("content_block_delta",{index:X,delta:{type:"thinking_delta",thinking:F.delta}});else if(F.type==="response.output_text.delta"&&typeof F.delta=="string")D()!=="text"&&J("text",{type:"text",text:""}),T+=1,x+=F.delta,f("content_block_delta",{index:X,delta:{type:"text_delta",text:F.delta}});else if(ee){let{call:be}=pe.add(ee);(D()!=="tool"||ee.tool_index!==re)&&(re=ee.tool_index,J("tool",{type:"tool_use",id:be.id??`call_${ee.tool_index}`,name:be.name??"",input:{}})),ee.delta&&f("content_block_delta",{index:X,delta:{type:"input_json_delta",partial_json:ee.delta}})}else if(F.type==="error"){o.streamError=!0,f("error",{error:{type:"api_error",message:JSON.stringify(F)}}),n.end();return}}if(n.destroyed)return;let Z=fe(ve),Te=ze(Z),C=De(c,pe.size+Te.length,x);if(C){o.streamError=!0,f("error",{error:{type:"api_error",message:Ue(d,C)}}),n.end();return}V();let $=[];if(pe.size===0)try{$=Or(Z)}catch(F){o.streamError=!0,f("error",{error:{type:"api_error",message:String(F instanceof Error?F.message:F)}}),n.end();return}O===0&&$.length===0&&(J("text",{type:"text",text:""}),V()),$.forEach(F=>{let ee=O++;f("content_block_start",{index:ee,content_block:{...F,input:{}}}),f("content_block_delta",{index:ee,delta:{type:"input_json_delta",partial_json:JSON.stringify(F.input??{})}}),f("content_block_stop",{index:ee})});let z=pe.size>0||$.length>0,ge=xn(Z);je(o,Z),f("message_delta",{delta:{stop_reason:z?"tool_use":"end_turn"},usage:ge?{input_tokens:ge.input_tokens,output_tokens:ge.output_tokens}:{output_tokens:T}}),f("message_stop",{}),n.end();return}let p="",g="",h=[],
|
|
56
|
+
`)};if(t.stream===!0){let S=r.track(y,d,o);Tr(n),f("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:i,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let T=0,x="",O=0,P="none",D=()=>P,X=-1,re=-1,pe=new xe,ve=[],V=()=>{P!=="none"&&(P="none",f("content_block_stop",{index:X}))},J=(F,ee)=>{V(),P=F,X=O++,f("content_block_start",{index:X,content_block:ee})};for await(let F of S){if(n.destroyed)break;ve.push(F);let ee=Ye(F);if(F.type==="response.reasoning_text.delta"&&typeof F.delta=="string")D()!=="thinking"&&J("thinking",{type:"thinking",thinking:""}),f("content_block_delta",{index:X,delta:{type:"thinking_delta",thinking:F.delta}});else if(F.type==="response.output_text.delta"&&typeof F.delta=="string")D()!=="text"&&J("text",{type:"text",text:""}),T+=1,x+=F.delta,f("content_block_delta",{index:X,delta:{type:"text_delta",text:F.delta}});else if(ee){let{call:be}=pe.add(ee);(D()!=="tool"||ee.tool_index!==re)&&(re=ee.tool_index,J("tool",{type:"tool_use",id:be.id??`call_${ee.tool_index}`,name:be.name??"",input:{}})),ee.delta&&f("content_block_delta",{index:X,delta:{type:"input_json_delta",partial_json:ee.delta}})}else if(F.type==="error"){o.streamError=!0,f("error",{error:{type:"api_error",message:JSON.stringify(F)}}),n.end();return}}if(n.destroyed)return;let Z=fe(ve),Te=ze(Z),C=De(c,pe.size+Te.length,x);if(C){o.streamError=!0,f("error",{error:{type:"api_error",message:Ue(d,C)}}),n.end();return}V();let $=[];if(pe.size===0)try{$=Or(Z)}catch(F){o.streamError=!0,f("error",{error:{type:"api_error",message:String(F instanceof Error?F.message:F)}}),n.end();return}O===0&&$.length===0&&(J("text",{type:"text",text:""}),V()),$.forEach(F=>{let ee=O++;f("content_block_start",{index:ee,content_block:{...F,input:{}}}),f("content_block_delta",{index:ee,delta:{type:"input_json_delta",partial_json:JSON.stringify(F.input??{})}}),f("content_block_stop",{index:ee})});let z=pe.size>0||$.length>0,ge=xn(Z);je(o,Z),f("message_delta",{delta:{stop_reason:z?"tool_use":"end_turn"},usage:ge?{input_tokens:ge.input_tokens,output_tokens:ge.output_tokens}:{output_tokens:T}}),f("message_stop",{}),n.end();return}let p="",g="",h=[],w=new xe;for await(let S of r.track(y,d,o)){h.push(S),S.type==="response.output_text.delta"&&typeof S.delta=="string"&&(p+=S.delta),S.type==="response.reasoning_text.delta"&&typeof S.delta=="string"&&(g+=S.delta);let T=Ye(S);if(T&&w.add(T),S.type==="error"){let x=S.error,O=x?.type==="invalid_request_error";me(n,O?400:502,O?"invalid_request_error":"api_error",x?.message??JSON.stringify(S));return}if(n.destroyed)break}if(n.destroyed)return;let v=fe(h);je(o,v);let m;try{m=Or(v),m.length===0&&w.size>0&&(m=Or({output:w.items()}))}catch(S){me(n,502,"api_error",String(S instanceof Error?S.message:S));return}let R=De(c,m.length,p);if(R){me(n,502,"api_error",Ue(d,R));return}let b=[],_=g||Di(v);_&&b.push({type:"thinking",thinking:_}),(p||m.length===0)&&b.push({type:"text",text:p}),b.push(...m),te(n,200,{id:a,type:"message",role:"assistant",content:b,model:i,stop_reason:m.length>0?"tool_use":"end_turn",usage:xn(v)??{input_tokens:0,output_tokens:0}})}function yd(t){Ae(process.stdout,t)}function Fi(t){let{clients:e,apiKey:n,identity:r,statusOf:o}=t,s=typeof e=="function"?e:()=>e,i=t.requestLog??yd,a=t.observe,u=new $e;return(l,d)=>{let c=new URL(l.url??"/","http://localhost"),y=c.pathname==="/v1/messages"||c.pathname.startsWith("/v1/messages/"),f=(v,m,R,b,_)=>y?me(d,v,b,m):K(d,v,m,R,_),p=rr(c.pathname),g=c.pathname.startsWith("/partners/"),h=p||g?{id:bt(),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,outputMinutes:null,sessionId:null,timing:null}:null;h&&(d.setHeader(gs,h.id),d.once("close",()=>{let v=vs({turn:h,method:l.method??"",path:c.pathname,res:d});i(bs(v)),a?.(v),Ci({record:v,ledger:t.ledger??null,req:l})}));let w=()=>ys(c.pathname,h);(async()=>{if(l.method==="GET"&&c.pathname==="/healthz"){te(d,200,{ok:!0});return}if(l.method==="GET"&&c.pathname==="/stats"){te(d,200,r?{identity:r,...u.snapshot()}:u.snapshot());return}if(n&&(l.headers.authorization??"")!==`Bearer ${n}`){f(401,"invalid local proxy api key","authentication_error","authentication_error");return}let v=c.pathname===bn?"canonical":c.pathname===Pt?"huggingface":null;if(v){if(l.method!=="POST"){f(405,`${c.pathname} is a POST batch lookup`,"invalid_request_error","invalid_request_error");return}let _=t.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 S=_.authorize(l),T;try{T=await $i(l,gi)}catch(P){if(P instanceof Nt){f(413,P.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=P=>_.lookup(S,P),O;try{if(v==="canonical"){let P=yi(T),D=await x(P);Sn(D,P),O={requests:D}}else O=await Si(T,x)}catch(P){if(P instanceof Ie){f(P.status,P.message,"invalid_request_error","invalid_request_error");return}throw P}te(d,200,O);return}if(l.method==="GET"&&c.pathname===Le){let _=await s(l);if(h&&(h.org=_.tenant??null),!_.openRouterModels){f(501,`GET ${Le} needs a router-backed client (openRouterModels)`,"not_implemented","not_implemented_error");return}te(d,200,await _.openRouterModels());return}if(c.pathname.startsWith("/partners/")){f(404,`no partner adapter for ${l.method} ${c.pathname} \u2014 the partner surface currently carries ${Pt} and GET ${Le}`,"invalid_request_error","not_found_error");return}let m=await s(l);if(h&&(h.org=m.tenant??null),l.method==="GET"&&c.pathname==="/v1/models"){if(c.searchParams.get("format")==="openrouter"){d.writeHead(308,{location:Le}),d.end();return}te(d,200,await m.listModels());return}if(l.method==="GET"&&c.pathname===Us){if(!m.listImageModels){f(501,"GET /v1/images/models needs a router-backed client (listImageModels)","not_implemented","not_implemented_error");return}te(d,200,await m.listImageModels());return}if(Ns(c.pathname)){if(!m.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:m,imageModels:m.supportsImages,stats:u,turn:w()};c.pathname===hn?await Ws(l,d,_):await Js(l,d,_);return}if(l.method==="GET"&&c.pathname===ui){if(!m.listSpeechToTextModels){f(501,"GET /v1/audio/models needs a router-backed client (listSpeechToTextModels)","not_implemented","not_implemented_error");return}te(d,200,await m.listSpeechToTextModels());return}if(ai(c.pathname)){if(!m.supportsTranscription){f(501,`the transcription lane needs a router-backed client (supportsTranscription) to gate model capability for ${wn}`,"not_implemented","not_implemented_error");return}let _={clients:m,transcriptionModels:m.supportsTranscription,stats:u,turn:w()};await ci(l,d,_);return}if(ni(c.pathname)){if(!m.supportsSpeech){f(501,`the speech lane needs a router-backed client (supportsSpeech) to gate model capability for ${Tt}`,"not_implemented","not_implemented_error");return}let _={clients:m,speechModels:m.supportsSpeech,stats:u,turn:w()};await ii(l,d,_);return}if(l.method!=="POST"){f(404,`no route for ${l.method} ${c.pathname}`,"invalid_request_error","not_found_error");return}let R;try{R=await $i(l)}catch(_){if(_ instanceof Nt){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 b=Buffer.byteLength(JSON.stringify(R));if(c.pathname==="/v1/responses"){u.request("responses",b,b),await ud(R,m,kt(m),d,u,w());return}if(c.pathname==="/v1/chat/completions"){u.request("chat_completions",b,b),await ld(R,m,d,u,w(),l.headers[Ar]);return}if(c.pathname==="/v1/systemone"){u.request("systemone",b,b),await dd(R,m,d,u,w());return}if(c.pathname==="/v1/messages"){u.request("messages",b,b),await gd(R,m,d,u,w());return}if(c.pathname==="/v1/messages/count_tokens"){u.request("count_tokens",b,b),me(d,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(v=>{let m=String(v instanceof Error?v.message:v);if(!d.headersSent){let b=o?.(v)??null;if(b){f(b.status,m,b.openaiType,b.anthropicType);return}if(v instanceof In.SessionAllocationError){let _=rn(v.code);if(_){let S=v.retryAfterSeconds??(_.status===429?30:void 0);S!==void 0&&d.setHeader("Retry-After",String(S)),f(_.status,cr(_,v.code??void 0),_.openaiType,_.anthropicType,_.code);return}}if(v instanceof In.SessionFailedError){let _=on(v);if(_){let S=_.status===429?30:void 0;S!==void 0&&d.setHeader("Retry-After",String(S)),f(_.status,cr(_,v.status.sessionId?`session ${v.status.sessionId}`:void 0),_.openaiType,_.anthropicType,_.code);return}}if(v instanceof H){f(502,m,"upstream_error","api_error");return}if(v instanceof we){v.retryable&&d.setHeader("Retry-After","1"),f(502,m,"upstream_error","api_error",v.code);return}if(v instanceof et){d.setHeader("Retry-After",String(v.retryAfterSeconds)),f(v.status,m,v.status===429?"capacity_error":"upstream_error",v.status===429?"rate_limit_error":"overloaded_error",v.code);return}f(500,m,"proxy_error","api_error","proxy_error");return}h&&(h.streamError=!0);let R=v instanceof we?v.code:null;d.destroyed||(y?d.write(`event: error
|
|
57
57
|
data: ${JSON.stringify({type:"error",error:{type:"api_error",message:m}})}
|
|
58
58
|
|
|
59
59
|
`):d.write(`data: ${JSON.stringify({error:{message:m,type:"upstream_error",code:R}})}
|
|
@@ -61,8 +61,8 @@ data: ${JSON.stringify({type:"error",error:{type:"api_error",message:m}})}
|
|
|
61
61
|
`)),d.end()})}}var he=(t,e)=>Object.assign(new Error(e),{status:t});function Bi(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function qi(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("intent")?.trim();return n===""?void 0:n}var _d='"transcription" (a transcription session), or no intent at all (the realtime voice session)';async function Hi(t,e,n,r){if(!t.supportsAudio)throw he(501,"the realtime lane needs a router-backed client (supportsAudio) to gate audio modality \u2014 hand-built clients without the catalog oracle are refused, never allowed silently");let o;try{o=await t.supportsAudio(e)}catch(l){throw l instanceof N?he(404,l.message):l}if(!o)throw he(404,`model ${JSON.stringify(e??"(default)")} is not audio-capable (catalog task is neither stt nor tts) \u2014 this realtime lane serves only audio-task models; chat models ride /v1/chat/completions`);if(!t.openAudioOn)throw he(501,"an audio-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the connection\u2019s pooled session \u2014 refusing loudly rather than binding a voiceless session");let s=!1;if(t.supportsTranscription)try{s=await t.supportsTranscription(e)}catch(l){throw l instanceof N?he(404,l.message):l}if(s&&!t.openTurnsOn)throw he(501,"a transcription-capable (stt) model needs a router-backed client (openTurnsOn) to open the engine\u2019s own turn/transcript lane (the stt named stream) pinned to the connection\u2019s pooled session \u2014 refusing loudly rather than binding a transcript-less transcription session");if(s&&t.transcriptStreamFor){let l;try{l=await t.transcriptStreamFor(e)}catch(d){throw d instanceof N?he(404,d.message):d}if(l!=="stt")throw he(501,`model ${JSON.stringify(e??"(default)")} overrides engine_args.transcript_stream with ${JSON.stringify(l)} \u2014 this lane consumes the engine\u2019s own transcripts from the platform\u2019s default "${"stt"}" stream and refuses loudly rather than listen on a stream the engine does not write`)}if(r!==void 0&&r!=="transcription")throw he(400,`intent ${JSON.stringify(r)} is not supported on this realtime lane \u2014 accepted values: ${_d}`);if(r==="transcription"&&!s)throw he(400,`model ${JSON.stringify(e??"(default)")} cannot serve the transcription intent \u2014 the transcription intent needs an stt model (the catalog task is not stt)`);let i=await t.sessionHandle(e),a={audio:await t.openAudioOn(i,n)},u={clients:{createResponse:async l=>await t.createResponseOn(i,l)},voice:o,audioSession:a};return s&&(u.turns=l=>t.openTurnsOn(i,l)),r==="transcription"&&(u.intent="transcription"),e!==void 0&&(u.model=e),u}var ne=require("@prometheus-io/client"),ji=require("http");var Gi=9464,Tn="/metrics",at="(other)",Lr="(none)",Mr=64,$r=128,wd=["/v1/responses","/v1/chat/completions","/v1/systemone","/v1/messages","/v1/messages/count_tokens","/v1/models","/v1/images/models","/v1/images/generations","/v1/images/edits","/v1/audio/speech","/v1/audio/models","/v1/audio/transcriptions",bn,Pt,Le],vd=new Set(wd);function Nr(t){return vd.has(t)?t:at}function Ur(t){return t===null?"none":t>=500?"5xx":t>=400?"4xx":t>=300?"3xx":t>=200?"2xx":"1xx"}var ut=class{constructor(e){this.max=e}max;admitted=new Set;overflows=0;of(e,n=!0){return e===null||e===""?Lr:this.admitted.has(e)?e:n?this.admitted.size>=this.max?(this.overflows+=1,at):(this.admitted.add(e),e):at}get size(){return this.admitted.size}get overflowCount(){return this.overflows}},lt=class{registry=new ne.Registry;modelLabel=new ut(Mr);orgLabel=new ut($r);requests;duration;ttft;streamErrors;undelivered;ledgerWrites;ledgerQueue;labelCardinality;labelOverflows;constructor(e={}){this.requests=new ne.Counter({name:"urun_inference_requests_total",help:"Completed /v1 requests, by lane, method, HTTP status class, streaming, model and org.",labelNames:["lane","method","status_class","stream","model","org"],registers:[this.registry]}),this.duration=new ne.Histogram({name:"urun_inference_request_duration_seconds",help:"Wall time from request receipt to response close. For a stream this is the WHOLE generation, not the time to the head.",labelNames:["lane","stream","model","org"],buckets:[.1,.25,.5,1,2.5,5,10,30,60,120,300,600],registers:[this.registry]}),this.ttft=new ne.Histogram({name:"urun_inference_ttft_seconds",help:"Time to first output-text delta on a streamed response. The metric Hugging Face gates provider listing on (<5s for conversational models).",labelNames:["lane","model","org"],buckets:[.1,.25,.5,1,2,3,5,8,15,30,60],registers:[this.registry]}),this.streamErrors=new ne.Counter({name:"urun_inference_stream_errors_total",help:"Responses that emitted an error frame AFTER the head flushed. These are HTTP 200 with a complete body, so no status-based metric can see them.",labelNames:["lane","model"],registers:[this.registry]}),this.undelivered=new ne.Counter({name:"urun_inference_undelivered_total",help:"Responses whose socket did not finish flushing \u2014 the client vanished mid-answer.",labelNames:["lane","model"],registers:[this.registry]}),this.ledgerWrites=new ne.Counter({name:"urun_inference_ledger_writes_total",help:"Terminal fate of the per-request ledger row for each billable /v1 request. outcome=written is the row landing (reason=priced|unpriced); outcome=skipped and outcome=failed are BOTH revenue that went unrecorded. failed/duplicate is the ENG-410 fabricated-row signal.",labelNames:["outcome","reason"],registers:[this.registry]});for(let n of Ai)this.ledgerWrites.inc({outcome:n.outcome,reason:n.reason},0);this.ledgerQueue=new ne.Gauge({name:"urun_inference_ledger_queue",help:'The ledger write queue right now. measure="depth" is rows waiting for a worker; measure="oldest_age_seconds" is how long the head has waited \u2014 approaching the dwell bound means rows are about to be given up on, which is revenue lost.',labelNames:["measure"],registers:[this.registry],collect:()=>{this.ledgerQueue.set({measure:"depth"},Ii()),this.ledgerQueue.set({measure:"oldest_age_seconds"},Oi()/1e3)}}),this.labelCardinality=new ne.Gauge({name:"urun_inference_label_cardinality",help:"Distinct values admitted for a bounded metric label. At its ceiling, further values report as (other).",labelNames:["label"],registers:[this.registry],collect:()=>{this.labelCardinality.set({label:"model"},this.modelLabel.size),this.labelCardinality.set({label:"org"},this.orgLabel.size)}}),this.labelOverflows=new ne.Counter({name:"urun_inference_label_overflows_total",help:"Observations that would have taken a new label value but found the budget spent. Non-zero means a metric dimension is now partly blind.",labelNames:["label"],registers:[this.registry]}),e.defaultMetrics!==!1&&(0,ne.collectDefaultMetrics)({register:this.registry,prefix:"urun_inference_proxy_"})}observeLedger(e){this.ledgerWrites.inc({outcome:e.outcome,reason:e.reason})}observe(e){let n=Nr(e.path),r=e.status!==null&&!(e.status>=400&&e.status<500),o=this.modelLabel.overflowCount+this.orgLabel.overflowCount,s=this.modelLabel.of(e.model,r),i=this.orgLabel.of(e.org,r);this.modelLabel.overflowCount+this.orgLabel.overflowCount-o>0&&(this.modelLabel.overflowCount>o?this.labelOverflows.inc({label:"model"}):this.labelOverflows.inc({label:"org"}));let u=e.stream===!0?"true":"false";this.requests.inc({lane:n,method:e.method===""?at:e.method,status_class:Ur(e.status),stream:u,model:s,org:i}),this.duration.observe({lane:n,stream:u,model:s,org:i},e.duration_ms/1e3),e.ttft_ms!==null&&this.ttft.observe({lane:n,model:s,org:i},e.ttft_ms/1e3),e.stream_error&&this.streamErrors.inc({lane:n,model:s}),e.delivered||this.undelivered.inc({lane:n,model:s})}scrape(){return this.registry.metrics()}get contentType(){return this.registry.contentType}};function Pn(t){return(0,ji.createServer)((e,n)=>{(async()=>{let r=(e.url??"/").split("?")[0];if(e.method!=="GET"||r!==Tn){n.writeHead(404,{"content-type":"text/plain"}),n.end(`no route for ${e.method} ${r} \u2014 this port serves only GET ${Tn}
|
|
62
62
|
`);return}let o=await t.scrape();n.writeHead(200,{"content-type":t.contentType}),n.end(o)})().catch(r=>{if(!n.headersSent){n.writeHead(500,{"content-type":"text/plain"}),n.end(`${String(r instanceof Error?r.message:r)}
|
|
63
63
|
`);return}n.end()})})}var Dr=require("crypto"),Vi=require("http"),Ji=9465,Fr="x-urun-key-id",Br="x-urun-key-fingerprint",qr="urun-ratelimit-bucket:",Hr="urun-ratelimit-tier:",Cn="anonymous",bd=/^urun_sk_[A-Za-z0-9_-]{43}$/;function jr(t){return(0,Dr.createHash)("sha256").update(`${qr}${t}`).digest("hex")}function Gr(t){return(0,Dr.createHash)("sha256").update(`${Hr}${t}`).digest("hex")}function Vr(t){let e=/^Bearer[ \t]+(\S.*)$/i.exec((t??"").trim());if(!e)return null;let n=e[1].trim();return bd.test(n)?n:null}function Ln(){return(0,Vi.createServer)((t,e)=>{let n=Vr(t.headers.authorization);e.setHeader(Fr,n===null?Cn:jr(n)),e.setHeader(Br,n===null?Cn:Gr(n)),e.writeHead(200),e.end()})}var Mn=require("@urun-sh/core"),ae=class extends Error{constructor(n,r=401){super(n);this.status=r;this.name="ProxyAuthError"}status},Oe=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function Ge(t){let e=(t??"").trim();if(e==="")throw new ae("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let n=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!n)throw new ae("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return n[1].trim()}var Sd=60,Jr=6e4,Wr=4096;function kd(t){return t===401||t===403}var dt=class{constructor(e){this.opts=e;this.mint=e.mint??Mn.createClientToken,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new ae("empty Authorization bearer token");let n=this.cache.get(e);if(n&&this.now()-n.at<Jr)return{apiKey:e,orgId:n.orgId};let r;try{r=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:Sd})).orgId}catch(o){throw o instanceof Mn.ClientTokenError&&kd(o.status)?(this.cache.delete(e),new ae("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new Oe(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${o instanceof Error?o.message:String(o)}`)}if(!r)throw new Oe("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>=Wr){let o=this.cache.keys().next();o.done||this.cache.delete(o.value)}return this.cache.set(e,{orgId:r,at:this.now()}),{apiKey:e,orgId:r}}clear(){this.cache.clear()}};var Bn=require("crypto");var ce=require("@urun-sh/core");function Wi(t,e,n,r){let o=t.response??{},s={request_id:n,consumer_id:r,stream:!0,kind:"chat",messages:e};return typeof o.instructions=="string"&&(s.instructions=o.instructions),Array.isArray(o.modalities)&&(s.modalities=o.modalities),typeof o.temperature=="number"&&(s.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(s.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(s.tools=o.tools),s}function Ki(t,e,n){let r={request_id:e,consumer_id:n,stream:!!t.stream,kind:"responses",input:t.input};return t.model&&(r.model=t.model),t.tools!==void 0&&(r.tools=t.tools),t.tool_choice!==void 0&&(r.tool_choice=t.tool_choice),typeof t.temperature=="number"&&(r.temperature=t.temperature),typeof t.max_output_tokens=="number"&&(r.max_output_tokens=t.max_output_tokens),typeof t.top_p=="number"&&(r.top_p=t.top_p),Array.isArray(t.stop)&&t.stop.length>0&&(r.stop=t.stop),typeof t.reasoning_effort=="string"&&(r.reasoning_effort=t.reasoning_effort),typeof t.instructions=="string"&&(r.instructions=t.instructions),t.chat_template_kwargs!==void 0&&(r.chat_template_kwargs=t.chat_template_kwargs),t.response_format!==void 0&&(r.response_format=t.response_format),typeof t.voice=="string"&&(r.voice=t.voice),typeof t.language=="string"&&(r.language=t.language),r}function zi(t){if(t instanceof Error)return{type:"error",error:{type:"urun_error",code:t.name||null,message:t.message}};if(t&&typeof t=="object"&&t.t==="error"){let e=t,n=e.body??{},r=n.error,o=r?.message??n.message??`unreadable serve error body: ${JSON.stringify(e.body??null)}`;return{type:"error",error:{type:r?.type??"urun_error",code:e.code??r?.code??null,message:o}}}return{type:"error",error:{type:"urun_error",code:null,message:String(t)}}}var Rd="llm-resp";async function*Kr(t,e){let n=`${Rd}:${e}`,r=`resp_${e}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};let o=new Map;try{for await(let s of t.stream(n).messages()){let i=s;if(i.t==="delta"){if(typeof i.delta=="string"&&(yield{type:"response.output_text.delta",item_id:r,delta:i.delta}),typeof i.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:r,delta:i.reasoning}),Array.isArray(i.tool_calls))for(let a of i.tool_calls){let u=typeof a.index=="number"?a.index:0,l=o.get(u)??{};a.id&&(l.id=a.id),a.function?.name&&(l.name=a.function.name),o.set(u,l),yield{type:"response.function_call_arguments.delta",item_id:`fc_${e}_${u}`,tool_index:u,call_id:l.id,name:l.name,delta:a.function?.arguments??""}}}else if(i.t==="response"){yield{type:"response.completed",response:{id:r,status:"completed",...i.body}};return}else if(i.t==="error"){yield zi(i);return}}}finally{try{t.doc("llm").set({requests:{[e]:null}})}catch{}}}var Nn=class t{constructor(e,n=t.DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS){this.session=e;this.docSyncTimeoutMs=n;this.sessionTag=e.sessionId??globalThis.crypto.randomUUID()}session;docSyncTimeoutMs;sessionTag;static DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS=15e3;get consumerId(){return this.session.consumerId}async ensureDocSynced(){let e=this.session.doc("llm");if(e.synced===!0||typeof e.onSynced!="function")return;let n;await new Promise((o,s)=>{let i=e.onSynced(()=>{clearTimeout(n),o()});n=setTimeout(()=>{i(),s(new Error(`[urun] llm doc plane never synced within ${this.docSyncTimeoutMs}ms \u2014 the request envelope write is withheld rather than raced into a possibly-lost pre-sync write (resumed-attach first-write-lost, prod-usw2 2026-09-23)`))},this.docSyncTimeoutMs)})}write(e){e.session_tag=this.sessionTag,this.session.doc("llm").set({requests:{[e.request_id]:{payload:e,consumer_id:e.consumer_id,stream:e.stream}}})}sendResponses(e,n){return this.writeGated(n,()=>{let r=Ki(e,n,this.consumerId);return this.write(r),Kr(this.session,n)})}sendResponseCreate(e,n,r){return this.writeGated(r,()=>{let o=Wi(e,n,r,this.consumerId);return this.write(o),Kr(this.session,r)})}async writeGated(e,n){try{await this.ensureDocSynced()}catch(r){throw new Error(`[urun] responses dial ${e} aborted before its envelope write: ${r instanceof Error?r.message:String(r)}`)}return n()}};var Yi=0;function xd(){return Yi+=1,`req_${Date.now().toString(36)}_${Yi.toString(36)}`}var Un=class{transport;constructor(e){this.transport=new Nn(e)}responses={create:async e=>{let n=xd(),r=await this.transport.sendResponses(e,n);return Object.assign((async function*(){yield*r})(),{requestId:n})}}};async function Xi(t){let n=await(t.fetchImpl??fetch)(`${t.catalogUrl}/model_catalog?select=model_id,variant,gpu_spec,engine,task,description,size_gb,hf_repo,engine_args,updated_at`,{headers:{apikey:t.anonKey,Authorization:`Bearer ${t.anonKey}`}});if(!n.ok)throw new Error(`model_catalog fetch failed: ${n.status}`);return await n.json()}var Qi=require("@urun-sh/core"),zr=2;function Ed(t){let e=t?.type;return e==="response.created"||e==="response.in_progress"}function Ad(t){let e=t?.type;return e==="response.completed"||e==="error"}function Zi(t){return async function(n,r){return(async function*(){for(let s=1;s<=zr;s++){let{app:i,entry:a,identity:u}=await t.sessionFor(n.model);r?.({sessionId:t.sessionIdOf?.(a)??null,identity:u});let l=!1,d=!1;try{let c=await t.createStream(a,n);for await(let y of c){if(Ad(y)&&(l=!0),Ed(y)){if(s>1)continue}else d=!0;yield y}}catch(c){if(c instanceof Qi.SessionFailedError&&(c.kind==="never-live"||c.kind==="create-failed")){try{t.onAdmissionVerdict?.(i,a,c)}catch{}throw c}if(s<zr&&!d){await t.evict(i,a);continue}throw c}if(l)return;if(s<zr&&!d){await t.evict(i,a);continue}return}})()}}var ra=require("crypto");var Id=3,Od="llm",Td=new Set(["paused","error"]);function ea(t,e){let n=t.doc(Od),r=typeof n.onConnectionState=="function",o=typeof t.onPhase=="function";if(!r&&!o)throw new Error(`session for app "${e}" exposes neither a doc connection-state surface nor onPhase \u2014 session-gone detection requires @urun-sh/core Session (SessionDocument.onConnectionState / Session.onPhase); pooling a session with no native lifecycle signal would silently swallow requests dispatched after the platform closes it`);let s=null,i=!1,a=new Set,u=[],l=d=>{if(!s){s=d;for(let c of[...a])c(d);a.clear()}};return r&&u.push(n.onConnectionState(d=>{i&&(d.consecutiveFailures<Id||l(new q(`the pooled session for app "${e}" is gone: its doc backhaul failed ${d.consecutiveFailures} consecutive times (last close ${d.lastCloseCode===null?"by provider liveness reap":`code ${d.lastCloseCode}`}${d.lastCloseReason?`, reason ${JSON.stringify(d.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`)))})),o&&u.push(t.onPhase(d=>{if(d.name==="live"){i=!0;return}i&&Td.has(d.name)&&l(new q(`the pooled session for app "${e}" lost its compute binding (phase "${d.name}"). The entry is evicted; retry to get a fresh session. The named session itself is not over \u2014 it stays attachable by name.`))})),{gone:()=>s,onGone(d){return s?(d(s),()=>{}):(a.add(d),()=>a.delete(d))},dispose(){a.clear();for(let d of u.splice(0))d()}}}async function*Yr(t,e){let n=e.gone();if(n)throw n;let r=t[Symbol.asyncIterator](),o,s=new Promise((i,a)=>{o=e.onGone(u=>a(u))});s.catch(()=>{});try{for(;;){let i=await Promise.race([r.next(),s]);if(i.done)return;yield i.value}}finally{o?.(),r.return?.(void 0)}}var Cd=1,Ld=4,Md=4;function oa(t,e){let n=(i,a)=>{let u=(t[i]??"").trim();if(!u)return a;let l=Number.parseInt(u,10);if(!Number.isInteger(l)||l<1)throw new Error(`${i} must be a positive integer (got ${JSON.stringify(t[i])})`);return l},r=(t.URUN_DECISION_POOL_MIN??"").trim();if(r==="0")return null;let o=r?Number.parseInt(r,10):Cd;if(!Number.isInteger(o)||o<0)throw new Error(`URUN_DECISION_POOL_MIN must be a non-negative integer (got ${JSON.stringify(t.URUN_DECISION_POOL_MIN)})`);let s=n("URUN_DECISION_POOL_MAX",Math.max(o,Ld));return{min:o,max:s,idleGraceMs:3e4,inFlightCap:n("URUN_DECISION_POOL_INFLIGHT_CAP",Md),replicaId:(t.URUN_REPLICA_ID??"").trim()||e||"proxy"}}function ta(t,e,n,r){let o=t[n];if(typeof o!="function")throw new Error(`app "${e}" has no function "${n}" (set URUN_FUNCTION)`);let s=r?o(void 0,r):o();if(typeof s.end!="function")throw new Error(`app "${e}" function "${n}" returned a session without end()`);return s}function sa(t,e,n,r){let{baseUrl:o,orgId:s,auth:i}=t,a=e.fnName??t.fnName,u=t.sessionStartOptions?.(n),l=r||u||e.sharedApp?{...e.sharedApp?{sharedApp:!0}:{},...u,...r}:void 0,d=g=>({session:g,responses:new Un(g),gone:ea(g,n)});if(i.lane==="jwt"){let g=(0,ce.App)(e.appSlug,{baseUrl:o,orgId:s,jwt:i.jwt});return d(ta(g,e.appSlug,a,l))}let{apiKey:c,gatewayUrl:y}=i,f=(typeof t.subject=="function"?t.subject():t.subject)??(0,ra.randomUUID)(),p=(0,ce.App)(e.appSlug,{baseUrl:o,orgId:s,getAccessToken:async()=>(await(0,ce.createClientToken)(c,{baseUrl:y,expiresIn:300,allowedFunctions:[`${e.appSlug}/${a}`],subject:f,...e.sharedApp?{sharedApp:!0}:{}})).token});return d(ta(p,e.appSlug,a,l))}function $d(t){let e=t.session.id;if(typeof e!="string"||e.length===0)throw new Error("pooled session exposes no stable session id \u2014 the session-identity seam requires @urun-sh/core Session (session.id)");return e}function Nd(t){if(t.kind==="never-live")return!0;if(t.kind!=="create-failed")return!1;let e=t.status.error?.httpStatus,n=t.status.error?.code;return e===410||e===409&&(n==="cancelled"||n==="expired")?!0:n==="client_ended"}var Ud=new Set(["error","paused"]);function Xr(t){let{auth:e,catalog:n}=t,r=null,o=(i,a)=>{let u=sa(t,i,a);return t.onSessionOpen&&typeof u.session.id=="string"&&u.session.id!==""&&t.onSessionOpen(a,u.session.id),u.gone.onGone(()=>{r?.evict(a,u)}),u},s=new en({sharedOnlyListing:t.sharedOnlyListing===!0,defaultApp:t.defaultApp,fnName:t.fnName,openSession:o,closeSession:async i=>{if(i.gone.dispose(),t.release==="detach"){if(typeof i.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');i.session.detach();return}await i.session.end()},listApps:e.lane==="api-key"?()=>cs({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,listCatalog:n?()=>Xi(n):null,listPrices:n?()=>Wo(n):null,shared:e.lane==="api-key"?()=>Ho({apiUrl:t.apiUrl,apiKey:e.apiKey}):null,sessionKey:$d});return r=s,t.defaultApp!==null&&s.seed(t.defaultApp,o({appSlug:t.defaultApp},Re({appSlug:t.defaultApp}))),s}function ia(t,e){let n=t.decisionPool;if(!n)throw new Error("wireDecisionPool called without BackhaulConfig.decisionPool");let r=n.replicaId??"proxy",o=new Map,s=new Map,i=u=>{process.stderr.write(`[decision-pool] ${u}
|
|
64
|
-
`)},a=(u,l)=>{let d=o.get(l);if(d)return d;let c=new pn({min:n.min,max:n.max,idleGraceMs:n.idleGraceMs,inFlightCap:n.inFlightCap,replicaId:r,openSlot:async y=>{let f=sa(t,u,l,{concurrencyKey:y.concurrencyKey});return f.gone.onGone(()=>c.evict(f)),await Fn(f.session),i(`slot attached pool=${l} slot=${y.concurrencyKey} session=${f.session.id??"(no id)"}`),f},closeSlot:async y=>{y.gone.dispose(),await y.session.end()},onDetach:(y,f)=>i(`slot detached pool=${l} slot=${y.concurrencyKey} reason=${f}`)});return o.set(l,c),c};return{async boot(){if(t.defaultApp===null)return;let u={appSlug:t.defaultApp};await a(u,Re(u)).boot()},async sessionFor(u){let l=await e.resolveTargetFor(u),d=Re(l),c=a(l,d).tryAcquireReady();if(!c)throw new Fe(a(l,d).stats().lastMissAgeMs);let y=s.get(c.entry)??[];return y.push(c),s.set(c.entry,y),{app:d,entry:c.entry,identity:Zt(l)}},async warmFor(u){let l=await e.resolveTargetFor(u);await a(l,Re(l)).boot()},end(u){let l=s.get(u)?.shift();l&&(s.get(u)?.length===0&&s.delete(u),l.release())},evict(u){for(let l of o.values())l.evict(u)},reap(){for(let u of o.values())u.reap()},async pingReady(u){for(let l of o.values())if(await l.pingReady(u))return!0;return!1},stats(){return[...o.entries()].map(([u,l])=>({poolKey:u,...l.stats()}))},async drain(u){await Promise.all([...o.values()].map(l=>l.drain({timeoutMs:u})))},async close(){await Promise.all([...o.values()].map(u=>u.close()))}}}function Dd(t){return(0,ce.isWakingPhase)(t?.name)?t?.runtime?.wake?.expectedWakePath==="portable-golden-restore"?ce.DEFAULT_WHEN_LIVE_TIMEOUT_MS:ce.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS:ce.DEFAULT_WHEN_LIVE_TIMEOUT_MS}async function Fn(t,e){let n=t.whenLive;if(typeof n!="function")throw new Error("pooled session exposes no native whenLive() \u2014 the audio lane must wait for the session's own admission (cold wake: queued \u2192 demand \u2192 promote) before wiring media; a session without the core Session.whenLive surface cannot be served audio");await n.call(t,{timeout:Dd(t.phase),signal:e})}async function na(t){if(typeof t.cancel=="function")try{await t.cancel();return}catch{}if(typeof t.detach!="function")throw new Error("pooled session exposes neither cancel() nor detach() \u2014 the client-gone abandon path requires the @urun-sh/core Session native lifecycle surface");t.detach()}function Dn(t){return t.reason instanceof Error?t.reason:new Error("the client socket closed")}function Qr(t,e=bo,n,r,o=xr){let s=new WeakMap,i=new WeakMap,a=new WeakMap,u=new WeakMap,l=new WeakMap,d=p=>{let g=p.session.id;return typeof g=="string"&&g.length>0?g:null},c=p=>{let g=s.get(p);if(!g){let h={waiting:0,gone:new AbortController};l.set(p,h),g=(async()=>{if(await Fn(p.session,h.gone.signal),h.gone.signal.aborted)throw Dn(h.gone.signal);l.get(p)===h&&l.delete(p);let _=await wo(p.session,await e());return Object.assign(_,{sessionId:p.session.id})})(),s.set(p,g),g.catch(()=>{s.get(p)===g&&s.delete(p)}),g.then(()=>{l.get(p)===h&&l.delete(p)},()=>{l.get(p)===h&&l.delete(p)})}return g},y=p=>{let g=u.get(p);return g||(g=(async()=>{let h=await c(p);return{appendInputAudio:_=>h.appendInputAudio(_),onOutputAudio:_=>h.onOutputAudio(_),sessionId:d(p)}})(),u.set(p,g),g.catch(()=>{u.get(p)===g&&u.delete(p)})),g},f=async(p,g,h)=>{if(h?.aborted){let m=l.get(p);throw(s.get(p)===void 0||m!==void 0&&m.waiting===0)&&await na(p.session),Dn(h)}let _=g(p),v=l.get(p);return v!==void 0&&v.waiting++,h===void 0?_:new Promise((m,R)=>{let b=()=>{v!==void 0&&--v.waiting===0&&l.get(p)===v&&(v.gone.abort(),l.delete(p),s.delete(p),u.delete(p),na(p.session).catch(()=>{})),R(Dn(h))};h.addEventListener("abort",b,{once:!0});let w=()=>h.removeEventListener("abort",b);_.then(S=>{w(),m(S)},S=>{w(),R(S)})})};return{createResponse:Zi({sessionFor:r?p=>r.sessionFor(p):p=>t.sessionFor(p),sessionIdOf:p=>d(p),evict:(p,g)=>{r?r.evict(g):t.evict(p,g)},onAdmissionVerdict:(p,g,h)=>{Nd(h)&&(r?r.evict(g):t.evict(p,g),n?.(p,h.kind==="never-live"?"never-live":"create-failed"))},createStream:async(p,g)=>{try{await Fn(p.session);let h=vi(Yr(await p.responses.responses.create(g),p.gone),o);if(!r)return h;let _=p;return(async function*(){try{yield*h}catch(m){throw bi(m)&&r.evict(_),m}finally{r.end(_)}})()}catch(h){throw r&&r.end(p),h}}}),listModels:()=>t.modelList(),openRouterModels:()=>t.openRouterModels(),supportsImages:(p,g)=>t.supportsImages(p,g),listImageModels:()=>t.imageModelList(),catalogIdentity:p=>t.catalogIdentityFor(p),servingAppSlug:async p=>await t.servingAppSlugFor(p),supportsAudio:p=>t.supportsAudio(p),supportsSpeech:p=>t.supportsSpeech(p),speechVoices:p=>t.speechVoices(p),supportsTranscription:p=>t.supportsTranscription(p),transcriptStreamFor:p=>t.transcriptStreamFor(p),listSpeechToTextModels:()=>t.speechToTextModelList(),openAudio:async(p,g)=>{let{entry:h}=await t.sessionFor(p);return await f(h,c,g)},openAudioOn:async(p,g)=>{let{entry:h}=await t.sessionForHandle(p);return await f(h,y,g)},openTurnsOn:async function*(p,g){if(g?.aborted)throw Dn(g);let{entry:h}=await t.sessionForHandle(p);yield*is(h.session,g)},openVideo:async p=>{let{entry:g}=await t.sessionFor(p),h=i.get(g);return h||(h=vo(g.session),i.set(g,h)),h},openVideoOut:async p=>{let{entry:g}=await t.sessionFor(p),h=a.get(g);return h||(h=ko(g.session),a.set(g,h)),h},sessionHandle:async p=>(await t.handleFor(p)).handle,speechSessionHandle:async p=>(await t.speechHandleFor(p)).handle,evictSpeechSession:async p=>{let{app:g,entry:h}=await t.sessionForHandle(p);await t.evict(g,h)},createResponseOn:async(p,g)=>{let{entry:h}=await t.sessionForHandle(p);return await Fn(h.session),Yr(await h.responses.responses.create(g),h.gone)},onSessionEnd:async(p,g)=>{let{entry:h}=await t.sessionForHandle(p),_=h.session;if(typeof _.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let v=!1;return _.onPhase(m=>{if(v||!Ud.has(m.name))return;v=!0;let R=_.endsAt??null;g({timeLeftMs:R?Math.max(0,R.getTime()-Date.now()):null,reason:m.reason??`session ${m.name}`})})}}}var Zr=8080,Dt="serve",aa="0.0.0.0";function Fd(t,e,n){let r=(t[e]??"").trim();if(!r)throw new Error(`${e} is not set \u2014 ${n}`);return r}function ua(t=process.env){let e=(t.PORT??"").trim(),n=e===""?Zr:Number(e);if(!Number.isInteger(n)||n<0||n>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let r=As(sn(Fd(t,"URUN_BASE_URL","the hosted proxy needs the uRun session-gateway base it opens sessions and verifies API keys against (e.g. https://api.urun.sh)"),"URUN_BASE_URL")),o=(t.URUN_API_URL??"").trim(),s=o?sn(o,"URUN_API_URL"):`${r.replace(/\/+$/,"")}/v1`,i=(t.URUN_CATALOG_URL??"").trim(),a=(t.URUN_CATALOG_ANON_KEY??"").trim();if(!!i!=!!a)throw new Error("URUN_CATALOG_URL and URUN_CATALOG_ANON_KEY must be set together (or both left unset) \u2014 exactly one of them is set, which would silently disable the model-catalog oracle");let u=i?{catalogUrl:sn(i,"URUN_CATALOG_URL"),anonKey:a}:null;return{port:n,baseUrl:r,apiUrl:s,catalog:u}}var eo=512;function Ft(t){return`urun-inference-proxy-${(0,Bn.createHash)("sha256").update(t).digest("hex").slice(0,32)}`}var ct=class{constructor(e){this.opts=e}opts;tenants=new Map;label(e,n){return Object.assign(Object.create(e),{tenant:n.orgId})}build(e){if(this.opts.build){let s=this.opts.build(e);return s.decision&&this.ensureDecisionReaper(),{router:s.router,clients:this.label(s.clients,e),decision:s.decision}}let n={baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:Dt,sharedOnlyListing:!0,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,release:"end",decisionPool:oa(process.env,process.env.HOSTNAME)??void 0},r=Xr(n),o=n.decisionPool?ia(n,r):null;return o&&this.ensureDecisionReaper(),{router:r,clients:this.label(Qr(r,void 0,void 0,o??void 0),e),decision:o??void 0}}ensureDecisionReaper(){if(this.decisionReaper!==null)return;let e=setInterval(()=>{for(let n of this.tenants.values())n.decision?.reap()},5e3);e.unref(),this.decisionReaper=e}decisionReaper=null;clientsFor(e){let n=this.tenants.get(e.apiKey);if(n)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,n),n.clients;for(;this.tenants.size>=eo;){let o=this.tenants.entries().next();if(o.done)break;let[s,i]=o.value;this.tenants.delete(s),i.router.closeAll().catch(()=>{})}let r=this.build(e);return this.tenants.set(e.apiKey,r),r.clients}get size(){return this.tenants.size}*decisionPools(){for(let e of this.tenants.values())e.decision&&(yield e.decision)}decisionWarmerFor(e){return this.clientsFor(e),this.tenants.get(e.apiKey)?.decision??null}async closeAll(){this.decisionReaper!==null&&(clearInterval(this.decisionReaper),this.decisionReaper=null);let e=[...this.tenants.values()];this.tenants.clear();let n=[];if(await Promise.all(e.map(async r=>{let o=[r.router.closeAll().catch(s=>{n.push(String(s instanceof Error?s.message:s))})];r.decision&&o.push(r.decision.close().catch(s=>{n.push(String(s instanceof Error?s.message:s))})),await Promise.all(o)})),n.length>0)throw new Error(`failed to close ${n.length} tenant backhaul(s): ${n.join("; ")}`)}},qn=512,to=1024,pt=class extends Error{constructor(){super(`conversation capacity reached (${qn} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function Bd(t){return{sessionStartOptions:e=>{if(e.startsWith(wt))return;let n=t.get(e);return n===void 0?void 0:{sessionId:n}},onSessionOpen:(e,n)=>{e.startsWith(wt)||t.set(e,n)}}}function qd(t,e,n,r){let o=Xr({baseUrl:t.baseUrl,orgId:e.orgId,defaultApp:null,fnName:Dt,sharedOnlyListing:!0,apiUrl:t.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:t.baseUrl},catalog:t.catalog,subject:n.subject,release:"detach",...Bd(r)});return{clients:Qr(o),release:async()=>{await o.closeAll()}}}var ft=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,n){let r={id:n,subject:`${Ft(e.apiKey)}:live:${n}`},o=new Map,s=this.opts.build?this.opts.build(e,r):qd(this.opts,e,r,o);return{id:n,backhaul:s,attachments:0,nativeRefs:o}}attachEntry(e,n){let r=this.byCaller.get(e.apiKey);r===void 0&&(r=new Map,this.byCaller.set(e.apiKey,r));let o=r.get(n);if(o!==void 0)return r.delete(n),r.set(n,o),o.attachments+=1,o;this.evictOverflow();let s=this.build(e,n);return s.attachments=1,r.set(n,s),s}get conversationCount(){let e=0;for(let n of this.byCaller.values())e+=n.size;return e}evictOverflow(){for(;this.conversationCount>=qn;){let e=this.byCaller.keys().next();if(e.done)throw new pt;let n=e.value,r=this.byCaller.get(n),o=!1;for(let[s,i]of r)if(!(i.attachments>0)){r.delete(s),this.dropHandleIndexFor(n,s),i.backhaul.release().catch(()=>{}),o=!0;break}if(!o)throw r.size===0&&this.byCaller.delete(n),new pt;r.size===0&&this.byCaller.delete(n)}}dropHandleIndexFor(e,n){for(let[r,o]of this.byHandle)o.apiKey===e&&o.conversationId===n&&this.byHandle.delete(r)}connectionFor(e){let n=null,r=!1,o=()=>(n===null&&(n=this.attachEntry(e,(0,Bn.randomUUID)())),n),s={createResponse:(i,a)=>o().backhaul.clients.createResponse(i,a),catalogIdentity:i=>o().backhaul.clients.catalogIdentity(i),servingAppSlug:i=>{let a=o().backhaul.clients.servingAppSlug;if(a===void 0)throw new Error("the conversation backhaul wires no servingAppSlug seam \u2014 the caller-org app a request resolves to cannot be known");return a(i)},listModels:()=>o().backhaul.clients.listModels(),sessionHandle:i=>o().backhaul.clients.sessionHandle(i),createResponseOn:(i,a)=>o().backhaul.clients.createResponseOn(i,a),onSessionEnd:(i,a)=>o().backhaul.clients.onSessionEnd(i,a)};return s.openAudio=async(i,a)=>{let u=o().backhaul.clients.openAudio;if(u===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return u(i,a)},s.openVideo=async i=>{let a=o().backhaul.clients.openVideo;if(a===void 0)throw new Error("the conversation backhaul wires no openVideo seam \u2014 video-in is unavailable on this deployment");return a(i)},s.openVideoOut=async i=>{let a=o().backhaul.clients.openVideoOut;if(a===void 0)throw new Error("the conversation backhaul wires no openVideoOut seam \u2014 video-out is unavailable on this deployment");return a(i)},s.supportsAudio=async i=>{let a=o().backhaul.clients.supportsAudio;if(a===void 0)throw new Error("the conversation backhaul wires no supportsAudio seam \u2014 the realtime lane cannot gate audio modality on this deployment");return a(i)},s.supportsSpeech=async i=>{let a=o().backhaul.clients.supportsSpeech;if(a===void 0)throw new Error("the conversation backhaul wires no supportsSpeech seam \u2014 the speech lane cannot gate model capability on this deployment");return a(i)},s.speechVoices=async i=>{let a=o().backhaul.clients.speechVoices;if(a===void 0)throw new Error("the conversation backhaul wires no speechVoices seam \u2014 the speech lane cannot resolve the voice set /v1/models publishes on this deployment");return a(i)},s.speechSessionHandle=async i=>{let a=o().backhaul.clients.speechSessionHandle;if(a===void 0)throw new Error("the conversation backhaul wires no speechSessionHandle seam \u2014 the speech lane cannot resolve its own pooled session on this deployment");return a(i)},s.openAudioOn=async(i,a)=>{let u=o().backhaul.clients.openAudioOn;if(u===void 0)throw new Error("the conversation backhaul wires no openAudioOn seam \u2014 the speech and realtime lanes cannot open their pinned native session audio on this deployment");return u(i,a)},s.openTurnsOn=(i,a)=>{let u=o().backhaul.clients.openTurnsOn;if(u===void 0)throw new Error("the conversation backhaul wires no openTurnsOn seam \u2014 the realtime transcription lane cannot open the backend turn/transcript lane on this deployment");return u(i,a)},s.evictSpeechSession=async i=>{let a=o().backhaul.clients.evictSpeechSession;if(a===void 0)throw new Error("the conversation backhaul wires no evictSpeechSession seam \u2014 the speech lane cannot retire its pinned speech session after a failed turn on this deployment");return a(i)},s.supportsTranscription=async i=>{let a=o().backhaul.clients.supportsTranscription;if(a===void 0)throw new Error("the conversation backhaul wires no supportsTranscription seam \u2014 the transcription lane cannot gate model capability on this deployment");return a(i)},s.transcriptStreamFor=async i=>{let a=o().backhaul.clients.transcriptStreamFor;if(a===void 0)throw new Error("the conversation backhaul wires no transcriptStreamFor seam \u2014 the realtime transcription lane cannot verify the model's engine_args.transcript_stream on this deployment");return a(i)},s.listSpeechToTextModels=async()=>{let i=o().backhaul.clients.listSpeechToTextModels;if(i===void 0)throw new Error("the conversation backhaul wires no listSpeechToTextModels seam \u2014 the transcription lane cannot list the stt models on this deployment");return i()},{clients:s,bindResumeHandle:i=>{let a=n;if(a===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=to;){let u=this.byHandle.keys().next();if(u.done)break;this.byHandle.delete(u.value)}this.byHandle.set(i,{apiKey:e.apiKey,conversationId:a.id})},clientsForResume:async i=>{let a=this.byHandle.get(i),u=a===void 0?void 0:this.byCaller.get(a.apiKey),l=a===void 0||u===void 0?void 0:u.get(a.conversationId);if(a===void 0||l===void 0||a.apiKey!==e.apiKey)throw new 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 n=l,l.attachments+=1,l.backhaul.clients},release:async()=>{if(r)return;r=!0;let i=n;n=null,i!==null&&(i.attachments-=1,!(i.attachments>0)&&await i.backhaul.release().catch(()=>{}))}}}async detachAll(){let e=[];for(let r of this.byCaller.values())for(let o of r.values())e.push(o.backhaul.release());this.byCaller.clear(),this.byHandle.clear();let n=[];if(await Promise.all(e.map(async r=>{try{await r}catch(o){n.push(String(o instanceof Error?o.message:o))}})),n.length>0)throw new Error(`failed to detach ${n.length} conversation backhaul(s): ${n.join("; ")}`)}};var Hn="/inference-usage",la=25e3;var Hd=new Set(["Error","TypeError","SyntaxError","TimeoutError"]);function da(t){if(!(t instanceof Error))return`a ${typeof t}`;let e=Hd.has(t.name)?t.name:"Error",n=t.cause?.code;return typeof n=="string"&&/^[A-Z][A-Z0-9_]{0,31}$/.test(n)?`${e} (${n})`:e}var jn=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async lookup(e,n){let r=`${this.opts.apiUrl.replace(/\/+$/,"")}${Hn}`,o;try{o=await this.fetchImpl(r,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify({inference_ids:n}),signal:AbortSignal.timeout(la)})}catch(i){throw new H(`the uRun usage surface (POST ${Hn}) could not be reached within ${la}ms: ${da(i)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new ae("the uRun org API key was rejected by the uRun control plane \u2014 check the key and that it is not revoked");if(!o.ok){let i=await o.text().catch(()=>null),a=i===null?"body unreadable":`${i.length}-character body`;throw new H(`the uRun usage surface (POST ${Hn}) answered HTTP ${o.status} (${a})`)}let s;try{s=await o.json()}catch(i){throw new H(`the uRun usage surface (POST ${Hn}) answered HTTP ${o.status} with a body that is not JSON: ${da(i)}`)}return wi(s,this.opts.onRejectedRow)}};var io="/inference-ledger",ao=1e4,mt=class extends Error{ledgerFailureReason;constructor(e,n="duplicate"){super(e),this.name="LedgerDuplicateError",this.ledgerFailureReason=n}},oo=class extends Error{constructor(n,r){super(r);this.ledgerAlreadyWritten=n;this.name="LedgerAlreadyWrittenError"}ledgerAlreadyWritten},Y=class extends Error{ledgerFailureReason="surface";constructor(e){super(e),this.name="LedgerSurfaceError"}},so=class extends ae{ledgerFailureReason="auth";constructor(e){super(e),this.name="LedgerAuthError"}},jd=[["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",no],["output_minutes",no],["cost_nano_usd",no],["price_version",t=>t===null||typeof t=="string"]];function Gd(t){for(let[e,n]of jd){if(!(e in t))return`${e} (absent)`;if(!n(t[e]))return`${e} (${JSON.stringify(t[e])})`}return null}function Vd(t){let e;try{e=JSON.parse(t)}catch{return{status:"unavailable"}}if(e===null||typeof e!="object"||Array.isArray(e))return{status:"unavailable"};let n=e.existing_status;if(n==="not_in_this_org")return{status:"not_in_this_org"};if(n!=="found")return{status:"unavailable"};let r=e.existing;return r===null||typeof r!="object"||Array.isArray(r)?{status:"unavailable"}:Gd(r)!==null?{status:"unavailable"}:{status:"found",row:r}}function no(t){return t===null?!0:typeof t=="number"?Number.isFinite(t):typeof t!="string"?!1:/[^\s]/.test(t)&&Number.isFinite(Number(t))}function ro(t,e){return t===null?e===null:e===null?!1:Number(e)===t}function Jd(t,e){return t===null?e===null:e===null?!1:Number(t.toFixed(9))===Number(e)}function ca(t,e){if(typeof e!="string")return!1;let n=Date.parse(t),r=Date.parse(e);return Number.isFinite(n)&&Number.isFinite(r)&&n===r}function Wd(t,e){let n=e;return n.inference_id===t.inference_id&&n.model_id===t.model_id&&(n.variant??null)===t.variant&&n.task===t.task&&n.http_status===t.http_status&&ca(t.started_at,n.started_at)&&ca(t.completed_at,n.completed_at)&&ro(t.prompt_tokens,n.prompt_tokens)&&ro(t.completion_tokens,n.completion_tokens)&&ro(t.output_units,n.output_units)&&Jd(t.output_minutes,n.output_minutes)}function uo(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new Y(`${e} answered a "recorded" that is not an object: ${JSON.stringify(t)}`);let n=t,r=n.inference_id;if(typeof r!="string"||r==="")throw new Y(`${e} answered a row with no inference_id: ${JSON.stringify(t)}`);if(!("cost_nano_usd"in n))throw new Y(`${e} answered a row with no cost_nano_usd field: ${JSON.stringify(t)}`);let o=n.cost_nano_usd;if(o!=null&&!(typeof o=="number"&&Number.isInteger(o)&&o>=0))throw new Y(`${e} answered cost_nano_usd ${JSON.stringify(o)}, which is neither a non-negative integer nor null`);let s=l=>{if(!(l in n))throw new Y(`${e} answered a row with no ${l} field: ${JSON.stringify(t)}`);let d=n[l];if(d===null)return null;if(typeof d!="string")throw new Y(`${e} answered a non-string ${l}: ${JSON.stringify(d)}`);if(!/[^\s]/.test(d))throw new Y(`${e} answered a BLANK ${l} (${JSON.stringify(d)}) \u2014 it satisfies "is a string" while naming nothing, which is exactly what inference_requests_priced_attribution_check exists to forbid`);return d},i=s("price_version"),a=s("unpriced_reason"),u=o!=null;if(u&&(i===null||a!==null))throw new Y(`${e} answered a PRICED row (cost_nano_usd ${String(o)}) that is not cleanly attributed \u2014 price_version ${JSON.stringify(i)}, unpriced_reason ${JSON.stringify(a)}`);if(!u&&(i!==null||a===null))throw new Y(`${e} answered an UNPRICED row that does not say why \u2014 price_version ${JSON.stringify(i)}, unpriced_reason ${JSON.stringify(a)}. NULL means NOT YET PRICED, and the reason is how an operator learns that traffic is being served which nobody can bill.`);return{inference_id:r,cost_nano_usd:o===void 0?null:o,price_version:i,unpriced_reason:a}}var ht=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async write(e,n){let r=`${this.opts.apiUrl.replace(/\/+$/,"")}${io}`,o;try{o=await this.fetchImpl(r,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify(n),signal:AbortSignal.timeout(ao)})}catch(u){throw new Y(`the uRun ledger surface at ${r} could not be reached: ${u instanceof Error?u.message:String(u)}`)}if(o.status===409){let u=await o.text().catch(()=>""),l=Vd(u);throw l.status==="found"&&Wd(n,l.row)?new oo(l.row,`the uRun control plane already holds THIS PROXY'S OWN row for inference_id ${n.inference_id} \u2014 every billable field matches the row being re-sent, so an earlier attempt landed and this retry is a no-op. Nothing was written and nothing was overwritten.`):l.status==="not_in_this_org"?new mt(`inference_id ${n.inference_id} is ALREADY TAKEN by a ledger row this org cannot read. The id space is a global primary key, so this is not a retry and cannot be this proxy's own write \u2014 either two orgs collided on a uuid, or a row was written for this id by somebody else.`,"duplicate_foreign"):new mt(`the uRun control plane already holds a ledger row for inference_id ${n.inference_id} that is NOT the one being written${l.status==="unavailable"?" (and it could not be read back to compare)":""} \u2014 nothing was written and nothing was overwritten. Either this proxy wrote twice, or a row was fabricated ahead of it (this write is authenticated by the CALLER'S OWN org key). ${u.slice(0,400)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new so("the uRun org API key was rejected by the uRun control plane when writing the inference ledger \u2014 the request was served but produced no billable row");if(!o.ok){let u=await o.text().catch(l=>`<body unreadable: ${l instanceof Error?l.message:String(l)}>`);throw new Y(`the uRun ledger surface at ${r} answered HTTP ${o.status}: ${u.slice(0,400)}`)}let s;try{s=await o.json()}catch(u){throw new Y(`the uRun ledger surface at ${r} answered a body that is not JSON: ${u instanceof Error?u.message:String(u)}`)}let i=s?.recorded;if(i===void 0)throw new Y(`the uRun ledger surface at ${r} answered a body with no "recorded" field`);let a=uo(i,`the uRun ledger surface at ${r}`);if(a.inference_id!==n.inference_id)throw new Y(`the uRun ledger surface at ${r} answered for inference_id ${a.inference_id} when asked about ${n.inference_id}`);return a}};var co=5e3,Kd=1e3,zd=1500,po=3e3,Yd="/api/client-tokens";function lo(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function pa(t,e,n,r){lo(t,e,{error:{message:n,type:r}})}async function Xd(t,e=fetch){let n=`${t.replace(/\/+$/,"")}${Yd}`;try{let r=await e(n,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(po)});return r.status===404?{ready:!1,detail:`${n} answered 404 \u2014 this is not a uRun control plane`}:r.status>=500?{ready:!1,detail:`${n} answered HTTP ${r.status}`}:{ready:!0,detail:`${n} answered HTTP ${r.status}`}}catch(r){return{ready:!1,detail:`${n} unreachable: ${r instanceof Error?r.message:String(r)}`}}}function ma(t){let e=t.resolver??new dt({gatewayUrl:t.baseUrl}),n=t.registry??new ct({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),r=t.probeControlPlane??(()=>Xd(t.baseUrl)),o=async()=>{let m=null;for(let b of n.decisionPools()){m=b;break}if(!m)return null;let R=await m.pingReady(zd);return{ok:R,detail:R?"a decision slot answered its session ping":"no decision slot answered its session ping"}},s=t.usageClient??new jn({apiUrl:t.apiUrl}),i=t.ledgerClient??new ht({apiUrl:t.apiUrl}),a=process.env.URUN_SHARED_ORG_ID?.trim(),u=process.env.URUN_SHARED_ORG_API_KEY?.trim();a&&u&&(async()=>{try{await n.decisionWarmerFor({orgId:a,apiKey:u})?.warmFor(Cr)}catch(m){process.stderr.write(`[decision-warm] out-of-band warm failed (best-effort, retrying on next demand): ${String(m instanceof Error?m.message:m)}
|
|
65
|
-
`)}})();let l=new lt(t.defaultMetrics===void 0?{}:{defaultMetrics:t.defaultMetrics}),d=async m=>e.resolve(Ge(m.headers.authorization)),c=m=>m instanceof ae?{status:m.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:m instanceof Oe?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,y=Fi({clients:async m=>n.clientsFor(await d(m)),usage:{authorize:m=>Ge(m.headers.authorization),lookup:(m,R)=>s.lookup(m,R)},statusOf:c,...t.requestLog?{requestLog:t.requestLog}:{},observe:m=>l.observe(m),ledger:{write:(m,R)=>i.write(Ge(m.headers.authorization),R),observe:m=>l.observeLedger(m),...t.ledgerLog?{report:t.ledgerLog}:{}}}),f=null,p=null,g=(0,fa.createServer)((m,R)=>{let b=new URL(m.url??"/","http://localhost");(async()=>{if(m.method==="GET"&&b.pathname==="/healthz"){lo(R,200,{ok:!0});return}if(m.method==="GET"&&b.pathname==="/readyz"){let T=Date.now();if((!f||T-f.at>=co)&&(f={at:T,...await r()}),!p||T-p.at>=Kd){let P=await o();p=P===null?{at:T,ok:!0,detail:null}:{at:T,ok:P.ok,detail:P.detail}}let x=(f?.ready??!1)&&p.ok,O=p.detail===null?f?.detail??"control plane not yet probed":`${f?.detail??"control plane not yet probed"}; ${p.detail}`;lo(R,x?200:503,{ok:x,control_plane:O});return}let
|
|
64
|
+
`)},a=(u,l)=>{let d=o.get(l);if(d)return d;let c=new pn({min:n.min,max:n.max,idleGraceMs:n.idleGraceMs,inFlightCap:n.inFlightCap,replicaId:r,openSlot:async y=>{let f=sa(t,u,l,{concurrencyKey:y.concurrencyKey});return f.gone.onGone(()=>c.evict(f)),await Fn(f.session),i(`slot attached pool=${l} slot=${y.concurrencyKey} session=${f.session.id??"(no id)"}`),f},closeSlot:async y=>{y.gone.dispose(),await y.session.end()},onDetach:(y,f)=>i(`slot detached pool=${l} slot=${y.concurrencyKey} reason=${f}`)});return o.set(l,c),c};return{async boot(){if(t.defaultApp===null)return;let u={appSlug:t.defaultApp};await a(u,Re(u)).boot()},async sessionFor(u){let l=await e.resolveTargetFor(u),d=Re(l),c=a(l,d).tryAcquireReady();if(!c)throw new Fe(a(l,d).stats().lastMissAgeMs);let y=s.get(c.entry)??[];return y.push(c),s.set(c.entry,y),{app:d,entry:c.entry,identity:Zt(l)}},async warmFor(u){let l=await e.resolveTargetFor(u);await a(l,Re(l)).boot()},end(u){let l=s.get(u)?.shift();l&&(s.get(u)?.length===0&&s.delete(u),l.release())},evict(u){for(let l of o.values())l.evict(u)},reap(){for(let u of o.values())u.reap()},async pingReady(u){for(let l of o.values())if(await l.pingReady(u))return!0;return!1},stats(){return[...o.entries()].map(([u,l])=>({poolKey:u,...l.stats()}))},async drain(u){await Promise.all([...o.values()].map(l=>l.drain({timeoutMs:u})))},async close(){await Promise.all([...o.values()].map(u=>u.close()))}}}function Dd(t){return(0,ce.isWakingPhase)(t?.name)?t?.runtime?.wake?.expectedWakePath==="portable-golden-restore"?ce.DEFAULT_WHEN_LIVE_TIMEOUT_MS:ce.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS:ce.DEFAULT_WHEN_LIVE_TIMEOUT_MS}async function Fn(t,e){let n=t.whenLive;if(typeof n!="function")throw new Error("pooled session exposes no native whenLive() \u2014 the audio lane must wait for the session's own admission (cold wake: queued \u2192 demand \u2192 promote) before wiring media; a session without the core Session.whenLive surface cannot be served audio");await n.call(t,{timeout:Dd(t.phase),signal:e})}async function na(t){if(typeof t.cancel=="function")try{await t.cancel();return}catch{}if(typeof t.detach!="function")throw new Error("pooled session exposes neither cancel() nor detach() \u2014 the client-gone abandon path requires the @urun-sh/core Session native lifecycle surface");t.detach()}function Dn(t){return t.reason instanceof Error?t.reason:new Error("the client socket closed")}function Qr(t,e=bo,n,r,o=xr){let s=new WeakMap,i=new WeakMap,a=new WeakMap,u=new WeakMap,l=new WeakMap,d=p=>{let g=p.session.id;return typeof g=="string"&&g.length>0?g:null},c=p=>{let g=s.get(p);if(!g){let h={waiting:0,gone:new AbortController};l.set(p,h),g=(async()=>{if(await Fn(p.session,h.gone.signal),h.gone.signal.aborted)throw Dn(h.gone.signal);l.get(p)===h&&l.delete(p);let w=await wo(p.session,await e());return Object.assign(w,{sessionId:p.session.id})})(),s.set(p,g),g.catch(()=>{s.get(p)===g&&s.delete(p)}),g.then(()=>{l.get(p)===h&&l.delete(p)},()=>{l.get(p)===h&&l.delete(p)})}return g},y=p=>{let g=u.get(p);return g||(g=(async()=>{let h=await c(p);return{appendInputAudio:w=>h.appendInputAudio(w),onOutputAudio:w=>h.onOutputAudio(w),sessionId:d(p)}})(),u.set(p,g),g.catch(()=>{u.get(p)===g&&u.delete(p)})),g},f=async(p,g,h)=>{if(h?.aborted){let m=l.get(p);throw(s.get(p)===void 0||m!==void 0&&m.waiting===0)&&await na(p.session),Dn(h)}let w=g(p),v=l.get(p);return v!==void 0&&v.waiting++,h===void 0?w:new Promise((m,R)=>{let b=()=>{v!==void 0&&--v.waiting===0&&l.get(p)===v&&(v.gone.abort(),l.delete(p),s.delete(p),u.delete(p),na(p.session).catch(()=>{})),R(Dn(h))};h.addEventListener("abort",b,{once:!0});let _=()=>h.removeEventListener("abort",b);w.then(S=>{_(),m(S)},S=>{_(),R(S)})})};return{createResponse:Zi({sessionFor:r?p=>r.sessionFor(p):p=>t.sessionFor(p),sessionIdOf:p=>d(p),evict:(p,g)=>{r?r.evict(g):t.evict(p,g)},onAdmissionVerdict:(p,g,h)=>{Nd(h)&&(r?r.evict(g):t.evict(p,g),n?.(p,h.kind==="never-live"?"never-live":"create-failed"))},createStream:async(p,g)=>{try{await Fn(p.session);let h=vi(Yr(await p.responses.responses.create(g),p.gone),o);if(!r)return h;let w=p;return(async function*(){try{yield*h}catch(m){throw bi(m)&&r.evict(w),m}finally{r.end(w)}})()}catch(h){throw r&&r.end(p),h}}}),listModels:()=>t.modelList(),openRouterModels:()=>t.openRouterModels(),supportsImages:(p,g)=>t.supportsImages(p,g),listImageModels:()=>t.imageModelList(),catalogIdentity:p=>t.catalogIdentityFor(p),servingAppSlug:async p=>await t.servingAppSlugFor(p),supportsAudio:p=>t.supportsAudio(p),supportsSpeech:p=>t.supportsSpeech(p),speechVoices:p=>t.speechVoices(p),supportsTranscription:p=>t.supportsTranscription(p),transcriptStreamFor:p=>t.transcriptStreamFor(p),listSpeechToTextModels:()=>t.speechToTextModelList(),openAudio:async(p,g)=>{let{entry:h}=await t.sessionFor(p);return await f(h,c,g)},openAudioOn:async(p,g)=>{let{entry:h}=await t.sessionForHandle(p);return await f(h,y,g)},openTurnsOn:async function*(p,g){if(g?.aborted)throw Dn(g);let{entry:h}=await t.sessionForHandle(p);yield*is(h.session,g)},openVideo:async p=>{let{entry:g}=await t.sessionFor(p),h=i.get(g);return h||(h=vo(g.session),i.set(g,h)),h},openVideoOut:async p=>{let{entry:g}=await t.sessionFor(p),h=a.get(g);return h||(h=ko(g.session),a.set(g,h)),h},sessionHandle:async p=>(await t.handleFor(p)).handle,speechSessionHandle:async p=>(await t.speechHandleFor(p)).handle,evictSpeechSession:async p=>{let{app:g,entry:h}=await t.sessionForHandle(p);await t.evict(g,h)},createResponseOn:async(p,g)=>{let{entry:h}=await t.sessionForHandle(p);return await Fn(h.session),Yr(await h.responses.responses.create(g),h.gone)},onSessionEnd:async(p,g)=>{let{entry:h}=await t.sessionForHandle(p),w=h.session;if(typeof w.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let v=!1;return w.onPhase(m=>{if(v||!Ud.has(m.name))return;v=!0;let R=w.endsAt??null;g({timeLeftMs:R?Math.max(0,R.getTime()-Date.now()):null,reason:m.reason??`session ${m.name}`})})}}}var Zr=8080,Dt="serve",aa="0.0.0.0";function Fd(t,e,n){let r=(t[e]??"").trim();if(!r)throw new Error(`${e} is not set \u2014 ${n}`);return r}function ua(t=process.env){let e=(t.PORT??"").trim(),n=e===""?Zr:Number(e);if(!Number.isInteger(n)||n<0||n>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let r=As(sn(Fd(t,"URUN_BASE_URL","the hosted proxy needs the uRun session-gateway base it opens sessions and verifies API keys against (e.g. https://api.urun.sh)"),"URUN_BASE_URL")),o=(t.URUN_API_URL??"").trim(),s=o?sn(o,"URUN_API_URL"):`${r.replace(/\/+$/,"")}/v1`,i=(t.URUN_CATALOG_URL??"").trim(),a=(t.URUN_CATALOG_ANON_KEY??"").trim();if(!!i!=!!a)throw new Error("URUN_CATALOG_URL and URUN_CATALOG_ANON_KEY must be set together (or both left unset) \u2014 exactly one of them is set, which would silently disable the model-catalog oracle");let u=i?{catalogUrl:sn(i,"URUN_CATALOG_URL"),anonKey:a}:null;return{port:n,baseUrl:r,apiUrl:s,catalog:u}}var eo=512;function Ft(t){return`urun-inference-proxy-${(0,Bn.createHash)("sha256").update(t).digest("hex").slice(0,32)}`}var ct=class{constructor(e){this.opts=e}opts;tenants=new Map;label(e,n){return Object.assign(Object.create(e),{tenant:n.orgId})}build(e){if(this.opts.build){let s=this.opts.build(e);return s.decision&&this.ensureDecisionReaper(),{router:s.router,clients:this.label(s.clients,e),decision:s.decision}}let n={baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:Dt,sharedOnlyListing:!0,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,release:"end",decisionPool:oa(process.env,process.env.HOSTNAME)??void 0},r=Xr(n),o=n.decisionPool?ia(n,r):null;return o&&this.ensureDecisionReaper(),{router:r,clients:this.label(Qr(r,void 0,void 0,o??void 0),e),decision:o??void 0}}ensureDecisionReaper(){if(this.decisionReaper!==null)return;let e=setInterval(()=>{for(let n of this.tenants.values())n.decision?.reap()},5e3);e.unref(),this.decisionReaper=e}decisionReaper=null;clientsFor(e){let n=this.tenants.get(e.apiKey);if(n)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,n),n.clients;for(;this.tenants.size>=eo;){let o=this.tenants.entries().next();if(o.done)break;let[s,i]=o.value;this.tenants.delete(s),i.router.closeAll().catch(()=>{})}let r=this.build(e);return this.tenants.set(e.apiKey,r),r.clients}get size(){return this.tenants.size}*decisionPools(){for(let e of this.tenants.values())e.decision&&(yield e.decision)}decisionWarmerFor(e){return this.clientsFor(e),this.tenants.get(e.apiKey)?.decision??null}async closeAll(){this.decisionReaper!==null&&(clearInterval(this.decisionReaper),this.decisionReaper=null);let e=[...this.tenants.values()];this.tenants.clear();let n=[];if(await Promise.all(e.map(async r=>{let o=[r.router.closeAll().catch(s=>{n.push(String(s instanceof Error?s.message:s))})];r.decision&&o.push(r.decision.close().catch(s=>{n.push(String(s instanceof Error?s.message:s))})),await Promise.all(o)})),n.length>0)throw new Error(`failed to close ${n.length} tenant backhaul(s): ${n.join("; ")}`)}},qn=512,to=1024,pt=class extends Error{constructor(){super(`conversation capacity reached (${qn} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function Bd(t){return{sessionStartOptions:e=>{if(e.startsWith(wt))return;let n=t.get(e);return n===void 0?void 0:{sessionId:n}},onSessionOpen:(e,n)=>{e.startsWith(wt)||t.set(e,n)}}}function qd(t,e,n,r){let o=Xr({baseUrl:t.baseUrl,orgId:e.orgId,defaultApp:null,fnName:Dt,sharedOnlyListing:!0,apiUrl:t.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:t.baseUrl},catalog:t.catalog,subject:n.subject,release:"detach",...Bd(r)});return{clients:Qr(o),release:async()=>{await o.closeAll()}}}var ft=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,n){let r={id:n,subject:`${Ft(e.apiKey)}:live:${n}`},o=new Map,s=this.opts.build?this.opts.build(e,r):qd(this.opts,e,r,o);return{id:n,backhaul:s,attachments:0,nativeRefs:o}}attachEntry(e,n){let r=this.byCaller.get(e.apiKey);r===void 0&&(r=new Map,this.byCaller.set(e.apiKey,r));let o=r.get(n);if(o!==void 0)return r.delete(n),r.set(n,o),o.attachments+=1,o;this.evictOverflow();let s=this.build(e,n);return s.attachments=1,r.set(n,s),s}get conversationCount(){let e=0;for(let n of this.byCaller.values())e+=n.size;return e}evictOverflow(){for(;this.conversationCount>=qn;){let e=this.byCaller.keys().next();if(e.done)throw new pt;let n=e.value,r=this.byCaller.get(n),o=!1;for(let[s,i]of r)if(!(i.attachments>0)){r.delete(s),this.dropHandleIndexFor(n,s),i.backhaul.release().catch(()=>{}),o=!0;break}if(!o)throw r.size===0&&this.byCaller.delete(n),new pt;r.size===0&&this.byCaller.delete(n)}}dropHandleIndexFor(e,n){for(let[r,o]of this.byHandle)o.apiKey===e&&o.conversationId===n&&this.byHandle.delete(r)}connectionFor(e){let n=null,r=!1,o=()=>(n===null&&(n=this.attachEntry(e,(0,Bn.randomUUID)())),n),s={createResponse:(i,a)=>o().backhaul.clients.createResponse(i,a),catalogIdentity:i=>o().backhaul.clients.catalogIdentity(i),servingAppSlug:i=>{let a=o().backhaul.clients.servingAppSlug;if(a===void 0)throw new Error("the conversation backhaul wires no servingAppSlug seam \u2014 the caller-org app a request resolves to cannot be known");return a(i)},listModels:()=>o().backhaul.clients.listModels(),sessionHandle:i=>o().backhaul.clients.sessionHandle(i),createResponseOn:(i,a)=>o().backhaul.clients.createResponseOn(i,a),onSessionEnd:(i,a)=>o().backhaul.clients.onSessionEnd(i,a)};return s.openAudio=async(i,a)=>{let u=o().backhaul.clients.openAudio;if(u===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return u(i,a)},s.openVideo=async i=>{let a=o().backhaul.clients.openVideo;if(a===void 0)throw new Error("the conversation backhaul wires no openVideo seam \u2014 video-in is unavailable on this deployment");return a(i)},s.openVideoOut=async i=>{let a=o().backhaul.clients.openVideoOut;if(a===void 0)throw new Error("the conversation backhaul wires no openVideoOut seam \u2014 video-out is unavailable on this deployment");return a(i)},s.supportsAudio=async i=>{let a=o().backhaul.clients.supportsAudio;if(a===void 0)throw new Error("the conversation backhaul wires no supportsAudio seam \u2014 the realtime lane cannot gate audio modality on this deployment");return a(i)},s.supportsSpeech=async i=>{let a=o().backhaul.clients.supportsSpeech;if(a===void 0)throw new Error("the conversation backhaul wires no supportsSpeech seam \u2014 the speech lane cannot gate model capability on this deployment");return a(i)},s.speechVoices=async i=>{let a=o().backhaul.clients.speechVoices;if(a===void 0)throw new Error("the conversation backhaul wires no speechVoices seam \u2014 the speech lane cannot resolve the voice set /v1/models publishes on this deployment");return a(i)},s.speechSessionHandle=async i=>{let a=o().backhaul.clients.speechSessionHandle;if(a===void 0)throw new Error("the conversation backhaul wires no speechSessionHandle seam \u2014 the speech lane cannot resolve its own pooled session on this deployment");return a(i)},s.openAudioOn=async(i,a)=>{let u=o().backhaul.clients.openAudioOn;if(u===void 0)throw new Error("the conversation backhaul wires no openAudioOn seam \u2014 the speech and realtime lanes cannot open their pinned native session audio on this deployment");return u(i,a)},s.openTurnsOn=(i,a)=>{let u=o().backhaul.clients.openTurnsOn;if(u===void 0)throw new Error("the conversation backhaul wires no openTurnsOn seam \u2014 the realtime transcription lane cannot open the backend turn/transcript lane on this deployment");return u(i,a)},s.evictSpeechSession=async i=>{let a=o().backhaul.clients.evictSpeechSession;if(a===void 0)throw new Error("the conversation backhaul wires no evictSpeechSession seam \u2014 the speech lane cannot retire its pinned speech session after a failed turn on this deployment");return a(i)},s.supportsTranscription=async i=>{let a=o().backhaul.clients.supportsTranscription;if(a===void 0)throw new Error("the conversation backhaul wires no supportsTranscription seam \u2014 the transcription lane cannot gate model capability on this deployment");return a(i)},s.transcriptStreamFor=async i=>{let a=o().backhaul.clients.transcriptStreamFor;if(a===void 0)throw new Error("the conversation backhaul wires no transcriptStreamFor seam \u2014 the realtime transcription lane cannot verify the model's engine_args.transcript_stream on this deployment");return a(i)},s.listSpeechToTextModels=async()=>{let i=o().backhaul.clients.listSpeechToTextModels;if(i===void 0)throw new Error("the conversation backhaul wires no listSpeechToTextModels seam \u2014 the transcription lane cannot list the stt models on this deployment");return i()},{clients:s,bindResumeHandle:i=>{let a=n;if(a===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=to;){let u=this.byHandle.keys().next();if(u.done)break;this.byHandle.delete(u.value)}this.byHandle.set(i,{apiKey:e.apiKey,conversationId:a.id})},clientsForResume:async i=>{let a=this.byHandle.get(i),u=a===void 0?void 0:this.byCaller.get(a.apiKey),l=a===void 0||u===void 0?void 0:u.get(a.conversationId);if(a===void 0||l===void 0||a.apiKey!==e.apiKey)throw new 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 n=l,l.attachments+=1,l.backhaul.clients},release:async()=>{if(r)return;r=!0;let i=n;n=null,i!==null&&(i.attachments-=1,!(i.attachments>0)&&await i.backhaul.release().catch(()=>{}))}}}async detachAll(){let e=[];for(let r of this.byCaller.values())for(let o of r.values())e.push(o.backhaul.release());this.byCaller.clear(),this.byHandle.clear();let n=[];if(await Promise.all(e.map(async r=>{try{await r}catch(o){n.push(String(o instanceof Error?o.message:o))}})),n.length>0)throw new Error(`failed to detach ${n.length} conversation backhaul(s): ${n.join("; ")}`)}};var Hn="/inference-usage",la=25e3;var Hd=new Set(["Error","TypeError","SyntaxError","TimeoutError"]);function da(t){if(!(t instanceof Error))return`a ${typeof t}`;let e=Hd.has(t.name)?t.name:"Error",n=t.cause?.code;return typeof n=="string"&&/^[A-Z][A-Z0-9_]{0,31}$/.test(n)?`${e} (${n})`:e}var jn=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async lookup(e,n){let r=`${this.opts.apiUrl.replace(/\/+$/,"")}${Hn}`,o;try{o=await this.fetchImpl(r,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify({inference_ids:n}),signal:AbortSignal.timeout(la)})}catch(i){throw new H(`the uRun usage surface (POST ${Hn}) could not be reached within ${la}ms: ${da(i)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new ae("the uRun org API key was rejected by the uRun control plane \u2014 check the key and that it is not revoked");if(!o.ok){let i=await o.text().catch(()=>null),a=i===null?"body unreadable":`${i.length}-character body`;throw new H(`the uRun usage surface (POST ${Hn}) answered HTTP ${o.status} (${a})`)}let s;try{s=await o.json()}catch(i){throw new H(`the uRun usage surface (POST ${Hn}) answered HTTP ${o.status} with a body that is not JSON: ${da(i)}`)}return wi(s,this.opts.onRejectedRow)}};var io="/inference-ledger",ao=1e4,mt=class extends Error{ledgerFailureReason;constructor(e,n="duplicate"){super(e),this.name="LedgerDuplicateError",this.ledgerFailureReason=n}},oo=class extends Error{constructor(n,r){super(r);this.ledgerAlreadyWritten=n;this.name="LedgerAlreadyWrittenError"}ledgerAlreadyWritten},Y=class extends Error{ledgerFailureReason="surface";constructor(e){super(e),this.name="LedgerSurfaceError"}},so=class extends ae{ledgerFailureReason="auth";constructor(e){super(e),this.name="LedgerAuthError"}},jd=[["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",no],["output_minutes",no],["cost_nano_usd",no],["price_version",t=>t===null||typeof t=="string"]];function Gd(t){for(let[e,n]of jd){if(!(e in t))return`${e} (absent)`;if(!n(t[e]))return`${e} (${JSON.stringify(t[e])})`}return null}function Vd(t){let e;try{e=JSON.parse(t)}catch{return{status:"unavailable"}}if(e===null||typeof e!="object"||Array.isArray(e))return{status:"unavailable"};let n=e.existing_status;if(n==="not_in_this_org")return{status:"not_in_this_org"};if(n!=="found")return{status:"unavailable"};let r=e.existing;return r===null||typeof r!="object"||Array.isArray(r)?{status:"unavailable"}:Gd(r)!==null?{status:"unavailable"}:{status:"found",row:r}}function no(t){return t===null?!0:typeof t=="number"?Number.isFinite(t):typeof t!="string"?!1:/[^\s]/.test(t)&&Number.isFinite(Number(t))}function ro(t,e){return t===null?e===null:e===null?!1:Number(e)===t}function Jd(t,e){return t===null?e===null:e===null?!1:Number(t.toFixed(9))===Number(e)}function ca(t,e){if(typeof e!="string")return!1;let n=Date.parse(t),r=Date.parse(e);return Number.isFinite(n)&&Number.isFinite(r)&&n===r}function Wd(t,e){let n=e;return n.inference_id===t.inference_id&&n.model_id===t.model_id&&(n.variant??null)===t.variant&&n.task===t.task&&n.http_status===t.http_status&&ca(t.started_at,n.started_at)&&ca(t.completed_at,n.completed_at)&&ro(t.prompt_tokens,n.prompt_tokens)&&ro(t.completion_tokens,n.completion_tokens)&&ro(t.output_units,n.output_units)&&Jd(t.output_minutes,n.output_minutes)}function uo(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new Y(`${e} answered a "recorded" that is not an object: ${JSON.stringify(t)}`);let n=t,r=n.inference_id;if(typeof r!="string"||r==="")throw new Y(`${e} answered a row with no inference_id: ${JSON.stringify(t)}`);if(!("cost_nano_usd"in n))throw new Y(`${e} answered a row with no cost_nano_usd field: ${JSON.stringify(t)}`);let o=n.cost_nano_usd;if(o!=null&&!(typeof o=="number"&&Number.isInteger(o)&&o>=0))throw new Y(`${e} answered cost_nano_usd ${JSON.stringify(o)}, which is neither a non-negative integer nor null`);let s=l=>{if(!(l in n))throw new Y(`${e} answered a row with no ${l} field: ${JSON.stringify(t)}`);let d=n[l];if(d===null)return null;if(typeof d!="string")throw new Y(`${e} answered a non-string ${l}: ${JSON.stringify(d)}`);if(!/[^\s]/.test(d))throw new Y(`${e} answered a BLANK ${l} (${JSON.stringify(d)}) \u2014 it satisfies "is a string" while naming nothing, which is exactly what inference_requests_priced_attribution_check exists to forbid`);return d},i=s("price_version"),a=s("unpriced_reason"),u=o!=null;if(u&&(i===null||a!==null))throw new Y(`${e} answered a PRICED row (cost_nano_usd ${String(o)}) that is not cleanly attributed \u2014 price_version ${JSON.stringify(i)}, unpriced_reason ${JSON.stringify(a)}`);if(!u&&(i!==null||a===null))throw new Y(`${e} answered an UNPRICED row that does not say why \u2014 price_version ${JSON.stringify(i)}, unpriced_reason ${JSON.stringify(a)}. NULL means NOT YET PRICED, and the reason is how an operator learns that traffic is being served which nobody can bill.`);return{inference_id:r,cost_nano_usd:o===void 0?null:o,price_version:i,unpriced_reason:a}}var ht=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async write(e,n){let r=`${this.opts.apiUrl.replace(/\/+$/,"")}${io}`,o;try{o=await this.fetchImpl(r,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify(n),signal:AbortSignal.timeout(ao)})}catch(u){throw new Y(`the uRun ledger surface at ${r} could not be reached: ${u instanceof Error?u.message:String(u)}`)}if(o.status===409){let u=await o.text().catch(()=>""),l=Vd(u);throw l.status==="found"&&Wd(n,l.row)?new oo(l.row,`the uRun control plane already holds THIS PROXY'S OWN row for inference_id ${n.inference_id} \u2014 every billable field matches the row being re-sent, so an earlier attempt landed and this retry is a no-op. Nothing was written and nothing was overwritten.`):l.status==="not_in_this_org"?new mt(`inference_id ${n.inference_id} is ALREADY TAKEN by a ledger row this org cannot read. The id space is a global primary key, so this is not a retry and cannot be this proxy's own write \u2014 either two orgs collided on a uuid, or a row was written for this id by somebody else.`,"duplicate_foreign"):new mt(`the uRun control plane already holds a ledger row for inference_id ${n.inference_id} that is NOT the one being written${l.status==="unavailable"?" (and it could not be read back to compare)":""} \u2014 nothing was written and nothing was overwritten. Either this proxy wrote twice, or a row was fabricated ahead of it (this write is authenticated by the CALLER'S OWN org key). ${u.slice(0,400)}`)}if(o.status===401||o.status===403)throw await o.text().catch(()=>{}),new so("the uRun org API key was rejected by the uRun control plane when writing the inference ledger \u2014 the request was served but produced no billable row");if(!o.ok){let u=await o.text().catch(l=>`<body unreadable: ${l instanceof Error?l.message:String(l)}>`);throw new Y(`the uRun ledger surface at ${r} answered HTTP ${o.status}: ${u.slice(0,400)}`)}let s;try{s=await o.json()}catch(u){throw new Y(`the uRun ledger surface at ${r} answered a body that is not JSON: ${u instanceof Error?u.message:String(u)}`)}let i=s?.recorded;if(i===void 0)throw new Y(`the uRun ledger surface at ${r} answered a body with no "recorded" field`);let a=uo(i,`the uRun ledger surface at ${r}`);if(a.inference_id!==n.inference_id)throw new Y(`the uRun ledger surface at ${r} answered for inference_id ${a.inference_id} when asked about ${n.inference_id}`);return a}};var co=5e3,Kd=1e3,zd=1500,po=3e3,Yd="/api/client-tokens";function lo(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function pa(t,e,n,r){lo(t,e,{error:{message:n,type:r}})}async function Xd(t,e=fetch){let n=`${t.replace(/\/+$/,"")}${Yd}`;try{let r=await e(n,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(po)});return r.status===404?{ready:!1,detail:`${n} answered 404 \u2014 this is not a uRun control plane`}:r.status>=500?{ready:!1,detail:`${n} answered HTTP ${r.status}`}:{ready:!0,detail:`${n} answered HTTP ${r.status}`}}catch(r){return{ready:!1,detail:`${n} unreachable: ${r instanceof Error?r.message:String(r)}`}}}function ma(t){let e=t.resolver??new dt({gatewayUrl:t.baseUrl}),n=t.registry??new ct({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),r=t.probeControlPlane??(()=>Xd(t.baseUrl)),o=async()=>{let m=null;for(let _ of n.decisionPools()){m=_;break}if(!m)return null;let R=m.stats();if(R.some(_=>_.draining))return{ok:!1,detail:"decision pool draining"};if(R.every(_=>_.attached===0&&_.inFlight===0))return{ok:!0,detail:"no active decision slot; gateway accepts on-demand resume"};let b=await m.pingReady(zd);return{ok:b,detail:b?"a decision slot answered its session ping":"no decision slot answered its session ping"}},s=t.usageClient??new jn({apiUrl:t.apiUrl}),i=t.ledgerClient??new ht({apiUrl:t.apiUrl}),a=process.env.URUN_SHARED_ORG_ID?.trim(),u=process.env.URUN_SHARED_ORG_API_KEY?.trim();a&&u&&(async()=>{try{await n.decisionWarmerFor({orgId:a,apiKey:u})?.warmFor(Cr)}catch(m){process.stderr.write(`[decision-warm] out-of-band warm failed (best-effort, retrying on next demand): ${String(m instanceof Error?m.message:m)}
|
|
65
|
+
`)}})();let l=new lt(t.defaultMetrics===void 0?{}:{defaultMetrics:t.defaultMetrics}),d=async m=>e.resolve(Ge(m.headers.authorization)),c=m=>m instanceof ae?{status:m.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:m instanceof Oe?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,y=Fi({clients:async m=>n.clientsFor(await d(m)),usage:{authorize:m=>Ge(m.headers.authorization),lookup:(m,R)=>s.lookup(m,R)},statusOf:c,...t.requestLog?{requestLog:t.requestLog}:{},observe:m=>l.observe(m),ledger:{write:(m,R)=>i.write(Ge(m.headers.authorization),R),observe:m=>l.observeLedger(m),...t.ledgerLog?{report:t.ledgerLog}:{}}}),f=null,p=null,g=(0,fa.createServer)((m,R)=>{let b=new URL(m.url??"/","http://localhost");(async()=>{if(m.method==="GET"&&b.pathname==="/healthz"){lo(R,200,{ok:!0});return}if(m.method==="GET"&&b.pathname==="/readyz"){let T=Date.now();if((!f||T-f.at>=co)&&(f={at:T,...await r()}),!p||T-p.at>=Kd){let P=await o();p=P===null?{at:T,ok:!0,detail:null}:{at:T,ok:P.ok,detail:P.detail}}let x=(f?.ready??!1)&&p.ok,O=p.detail===null?f?.detail??"control plane not yet probed":`${f?.detail??"control plane not yet probed"}; ${p.detail}`;lo(R,x?200:503,{ok:x,control_plane:O});return}let _=b.pathname==="/v1"||b.pathname.startsWith("/v1/"),S=b.pathname.startsWith("/partners/");if(!_&&!S){pa(R,404,`no route for ${m.method} ${b.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface, the partner adapter surface (/partners/...), the WS upgrade paths (/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent and /v1/realtime), plus /healthz and /readyz`,"invalid_request_error");return}y(m,R)})().catch(_=>{if(!R.headersSent){pa(R,500,String(_ instanceof Error?_.message:_),"proxy_error");return}R.end()})}),h=t.conversations??new ft({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),w=[nr(g,{authenticate:async m=>{let R=await e.resolve(m),b=h.connectionFor(R);return{clients:b.clients,scope:Ft(R.apiKey),onBindResumeHandle:b.bindResumeHandle,clientsForResume:b.clientsForResume,release:b.release}}}),dr(g,{clients:async m=>n.clientsFor(await d(m)),statusOf:c}).close,cn(g,{resolver:{resolve:async(m,R)=>{let b=await e.resolve(Ge(m.headers.authorization)),_=h.connectionFor(b),S;try{S=await Hi(_.clients,Bi(m),R,qi(m))}catch(T){throw _.release().catch(()=>{}),T}return{...S,release:_.release}}}})];g.on("upgrade",(m,R)=>{let b=new URL(m.url??"/","http://localhost");b.pathname===Wt||b.pathname===ur||b.pathname===At||R.destroyed||(R.write(`HTTP/1.1 404 Not Found\r
|
|
66
66
|
Connection: close\r
|
|
67
67
|
\r
|
|
68
|
-
no WS route for ${b.pathname} \u2014 this endpoint serves only the Gemini Live, /v1/responses, and /v1/realtime upgrade paths`),R.destroy())});let v=async()=>{for(let m of
|
|
68
|
+
no WS route for ${b.pathname} \u2014 this endpoint serves only the Gemini Live, /v1/responses, and /v1/realtime upgrade paths`),R.destroy())});let v=async()=>{for(let m of w)await m();await h.detachAll()};return{server:g,metricsServer:Pn(l),metrics:l,edgeIdentityServer:Ln(),registry:n,closeAll:()=>n.closeAll(),closeWebSockets:v}}0&&(module.exports={ANONYMOUS_KEY_ID,BIND_HOST,BUCKET_DOMAIN,BoundedLabel,ControlPlaneUnavailableError,ConversationBackhauls,ConversationCapacityError,DEFAULT_PORT,EDGE_IDENTITY_PORT,EDGE_KEY_FINGERPRINT_HEADER,EDGE_KEY_ID_HEADER,InferenceMetrics,LEDGER_FUNCTION_PATH,LEDGER_WRITE_TIMEOUT_MS,LedgerClient,LedgerDuplicateError,LedgerSurfaceError,MAX_CACHED_KEYS,MAX_CONVERSATIONS,MAX_CONVERSATION_HANDLES,MAX_MODEL_LABELS,MAX_ORG_LABELS,MAX_TENANTS,METRICS_PATH,METRICS_PORT,NO_MODEL_LABEL,ORG_BINDING_TTL_MS,OTHER_LABEL,OrgResolver,ProxyAuthError,READINESS_TIMEOUT_MS,READINESS_TTL_MS,SERVE_FUNCTION,TIER_DOMAIN,TenantRegistry,bearerFrom,bearerTokenOf,createEdgeIdentityServer,createHostedProxy,createMetricsServer,edgeBucketId,edgeTierFingerprint,laneOf,parseLedgerRecorded,resolveHostedConfig,statusClassOf,tenantSubject});
|