@urun-sh/openai 0.6.16 → 0.6.17
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-6SN4EJ7Q.js +7 -0
- package/dist/{chunk-2PIYH7O7.js → chunk-U7EJSC2N.js} +18 -18
- package/dist/hosted/bin.cjs +26 -26
- package/dist/hosted/bin.js +1 -1
- package/dist/hosted/index.cjs +26 -26
- package/dist/hosted/index.d.cts +1 -1
- package/dist/hosted/index.d.ts +1 -1
- package/dist/hosted/index.js +1 -1
- package/dist/pi-extension/standalone.cjs +46 -46
- package/dist/proxy/cli.cjs +5 -5
- package/dist/proxy/cli.js +1 -1
- package/dist/proxy/index.cjs +4 -4
- package/dist/proxy/index.d.cts +2 -2
- package/dist/proxy/index.d.ts +2 -2
- package/dist/proxy/index.js +1 -1
- package/dist/{responses-turn-EwxfRJJg.d.ts → responses-turn-B0zlsQGn.d.ts} +6 -0
- package/dist/{responses-turn-vMefRdgJ.d.cts → responses-turn-Dd-h46sQ.d.cts} +22 -1
- package/package.json +2 -2
- package/dist/chunk-5PZ6OVEK.js +0 -7
package/dist/proxy/index.cjs
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Connection: close\r
|
|
4
4
|
\r
|
|
5
5
|
${p}\r
|
|
6
|
-
`),a.destroy())}))});let s=null;return()=>(s??=(async()=>{for(let i of r.clients)i.close(1001,"server closing");await new Promise((i,a)=>r.close(d=>d===void 0?i():a(d)))})(),s)}function Mn(t,e,n=new Ie){new pt(t,e,n)}var he=require("ws");var Ln=require("crypto"),Nn="Inference-Id";function De(){return(0,Ln.randomUUID)()}function Ft(t){return t==="/v1"||t.startsWith("/v1/")}function Un(t,e){if(!Ft(t)||e===null)throw new Error(`no inference turn for ${t}: an inference lane was reached off the /v1 surface, so its response would carry no Inference-Id and could never be billed`);return e}function Fe(t){return`resp_${t}`}function Dn(t){return`chatcmpl-${t}`}function Fn(t){return`msg_${t}`}function re(t){return e=>{t.sessionId=e.sessionId,t.catalog=e.identity}}function Bn(t){let e=t.now??(()=>performance.now());return{event:"inference_request",ts:new Date().toISOString(),inference_id:t.turn.id,method:t.method,path:t.path,status:t.res.headersSent?t.res.statusCode:null,delivered:t.res.writableFinished,stream_error:t.turn.streamError,model:t.turn.model,stream:t.turn.stream,org:t.turn.org,duration_ms:e()-t.turn.startedAt,ttft_ms:t.turn.stream===!0?t.turn.ttftMs:null,prompt_tokens:t.turn.promptTokens,completion_tokens:t.turn.completionTokens,output_units:t.turn.outputUnits,session_id:t.turn.sessionId,queue_ms:t.turn.timing?.queue_ms??null,prefill_ms:t.turn.timing?.prefill_ms??null,decode_ms:t.turn.timing?.decode_ms??null,total_ms:t.turn.timing?.total_ms??null,catalog:t.turn.catalog}}function qn(t){return`${JSON.stringify(t)}
|
|
6
|
+
`),a.destroy())}))});let s=null;return()=>(s??=(async()=>{for(let i of r.clients)i.close(1001,"server closing");await new Promise((i,a)=>r.close(d=>d===void 0?i():a(d)))})(),s)}function Mn(t,e,n=new Ie){new pt(t,e,n)}var he=require("ws");var Ln=require("crypto"),Nn="Inference-Id";function De(){return(0,Ln.randomUUID)()}function Ft(t){return t==="/v1"||t.startsWith("/v1/")}function Un(t,e){if(!Ft(t)||e===null)throw new Error(`no inference turn for ${t}: an inference lane was reached off the /v1 surface, so its response would carry no Inference-Id and could never be billed`);return e}function Fe(t){return`resp_${t}`}function Dn(t){return`chatcmpl-${t}`}function Fn(t){return`msg_${t}`}function re(t){return e=>{t.sessionId=e.sessionId,t.catalog=e.identity}}function Bn(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,catalog:t.turn.catalog}}function qn(t){return`${JSON.stringify(t)}
|
|
7
7
|
`}var ho=256,yo=128;function Z(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,yo)}var ve=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>=ho?"(other)":e;return this.models[r]??={requests:0,tokens_out:0}}model(e){this.modelEntry(e).requests+=1}track(e,n,r){let o=this,s=performance.now(),i=null,a=0;return(async function*(){for await(let u of e)u.type==="response.output_text.delta"&&typeof u.delta=="string"&&(i===null&&(i=performance.now(),o.ttftMs.push(i-s),o.ttftMs.length>512&&o.ttftMs.shift(),r!==void 0&&(r.ttftMs=i-r.startedAt)),a+=1,o.tokensOut+=1,o.charsOut+=u.delta.length,n!==void 0&&(o.modelEntry(n).tokens_out+=1)),yield u;let d=(performance.now()-(i??s))/1e3;a>0&&d>0&&(o.tokRates.push(a/d),o.tokRates.length>512&&o.tokRates.shift())})()}static p50(e){if(!e.length)return null;let n=[...e].sort((r,o)=>r-o);return n[Math.floor(n.length/2)]}snapshot(){let e=Object.values(this.requests).reduce((n,r)=>n+r,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:t.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:t.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:e},persistent_connections:1,history_bytes_saved:Math.max(0,this.naiveInBytes-this.localInBytes),history_bytes_saved_pct:this.naiveInBytes>0?Math.round((1-this.localInBytes/this.naiveInBytes)*1e3)/10:null}}}},_o=256;function wo(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 be=class extends Error{},Oe=class{conversations=new Map;thread(e,n){let r=wo(n);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new be(`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<=_o)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},jn=new WeakMap;function Be(t){let e=jn.get(t);return e||(e=new Oe,jn.set(t,e)),e}function Te(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function ie(t){return t.find(e=>e.type==="response.completed")?.response??null}function Pe(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 ge=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}))}},vo=["<tool_call>","<function="];function bo(t){return vo.find(e=>t.includes(e))}function ke(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 Re(t,e,n){if(!(!t||e>0))return bo(n)}function Ce(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 ko(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 Bt(t,e){let n=ko(t);return n.length>0?n:e?[{role:"assistant",content:e}]:[]}var oe=class extends Error{constructor(n,r,o=400,s){super(n);this.code=r;this.status=o;this.param=s}code;status;param};function $e(t,e){try{return t.thread(e.previous_response_id,e.input)}catch(n){throw n instanceof be?new oe(n.message,"previous_response_not_found",400,"previous_response_id"):new oe(String(n instanceof Error?n.message:n),"invalid_input")}}function Hn(t){let e=t.text;if(e!==null&&typeof e=="object"){let n=e.format;if(n!==void 0)return n}}async function qt(t,e,n,r,o){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new oe("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}:{},...Hn(e)!==void 0?{response_format:Hn(e)}:{}},o)}async function qe(t){let{body:e,clients:n,store:r,stats:o}=t,s=$e(r,e);e.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(s))-Buffer.byteLength(JSON.stringify(e.input)));let i=t.responseId,a=t.remember??e.store!==!1,d=e.store!==!1,u=Z(e);o.model(u);let c=Array.isArray(e.tools)&&e.tools.length>0,l=t.turn,m=await qt(n,e,s,!0,l?re(l):void 0);return{responseId:i,events:Eo({events:m,conversation:s,responseId:i,modelKey:u,requestHadTools:c,remember:a,persist:d,store:r,stats:o,turn:t.turn})}}function Ro(t,e){let n=[...t].sort((d,u)=>d.output_index-u.output_index).map(({item:d})=>d),r=Array.isArray(e?.output)?e.output:[],o=r.filter(d=>d!==null&&typeof d=="object"&&d.type==="message"),s=n.filter(d=>d.type==="message").length===o.length,i=0;return[...n.map(d=>d.type==="message"&&s?So(o[i++],d):d),...xo(r,n)]}function xo(t,e){let n=e.some(u=>u.type==="message"),r=e.some(u=>u.type==="reasoning"),o=u=>String(u.call_id??u.id??""),s=e.filter(u=>u.type==="function_call"),i=t.filter(u=>u!==null&&typeof u=="object"&&u.type==="function_call").length,a=s.length===i,d=new Set(s.map(o));return t.filter(u=>{if(u===null||typeof u!="object")return!0;let c=u;return c.type==="message"?!n:c.type==="reasoning"?!r:c.type==="function_call"?!a&&!d.has(o(c)):!0})}function So(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*Eo(t){let{events:e,conversation:n,responseId:r,modelKey:o,requestHadTools:s,remember:i,persist:a,store:d,stats:u}=t,c=[],l=O=>O.response&&typeof O.response=="object"?{...O,response:{...O.response,id:r}}:O,m=`item_${r.slice(5)}`,p=!1,y=!1,I=0;function*x(){p||(p=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let _=[],h=(O,M)=>{_.push({output_index:O,item:M})},g=!1,E=0,f="",v=0,k="";function*$(){if(!g)return;g=!1,yield{type:"response.reasoning_text.done",item_id:f,output_index:E,content_index:0,text:k};let O={id:f,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:k}],status:"completed"};yield{type:"response.output_item.done",output_index:E,item:O},h(E,O),k=""}let b=!1,R=0,P="",D=0,K="",Q="";function*se(O){if(!b)return;b=!1,yield{type:"response.output_text.done",item_id:P,output_index:R,content_index:0,text:K},yield{type:"response.content_part.done",item_id:P,output_index:R,content_index:0,part:{type:"output_text",text:K}};let M={id:P,type:"message",role:"assistant",status:O,content:[{type:"output_text",text:K}]};yield{type:"response.output_item.done",output_index:R,item:M},h(R,M),K=""}let fe=new ge,j=null,H=-1;function*Y(O){if(!j)return;let M=j;j=null,yield{type:"response.function_call_arguments.done",item_id:M.fcId,output_index:M.outputIndex,arguments:M.args};let W={id:M.fcId,type:"function_call",call_id:M.callId??M.fcId,name:M.name??"",arguments:M.args,status:O};yield{type:"response.output_item.done",output_index:M.outputIndex,item:W},h(M.outputIndex,W)}let _e=!1;for await(let O of u.track(e,o,t.turn)){c.push(O);let M=String(O.type??"");if(M==="response.created"||M==="response.in_progress"){p=!0,yield l(O);continue}if(M==="response.output_item.added"){y=!0,yield l(O);continue}if(M==="response.reasoning_text.delta"&&!y){yield*x(),g||(g=!0,E=I++,v+=1,f=v===1?`rs_${r.slice(5)}`:`rs_${r.slice(5)}_${v}`,yield{type:"response.output_item.added",output_index:E,item:{id:f,type:"reasoning",summary:[],content:[],status:"in_progress"}}),k+=String(O.delta??""),yield{type:"response.reasoning_text.delta",item_id:f,output_index:E,content_index:0,delta:O.delta};continue}if(M==="response.output_text.delta"&&!y){yield*x(),yield*$(),b||(b=!0,R=I++,D+=1,P=D===1?m:`${m}_${D}`,yield{type:"response.output_item.added",output_index:R,item:{id:P,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:P,output_index:R,content_index:0,part:{type:"output_text",text:""}}),K+=String(O.delta??""),Q+=String(O.delta??""),yield{type:M,item_id:P,output_index:R,content_index:0,delta:O.delta};continue}let W=y?null:Pe(O);if(W){if(yield*x(),yield*$(),yield*se("completed"),fe.add(W),W.tool_index!==H){yield*Y("completed"),H=W.tool_index;let de=I++;j={fcId:`fc_${r.slice(5)}_${W.tool_index}`,outputIndex:de,callId:W.call_id,name:W.name,args:""},yield{type:"response.output_item.added",output_index:de,item:{id:j.fcId,type:"function_call",call_id:j.callId??j.fcId,name:j.name??"",arguments:"",status:"in_progress"}}}j&&(W.call_id&&(j.callId=W.call_id),W.name&&(j.name=W.name),j.args+=W.delta,yield{type:"response.function_call_arguments.delta",item_id:j.fcId,output_index:j.outputIndex,delta:W.delta});continue}if(M==="response.completed"&&!y){let de=Te(O.response),F=Re(s,fe.size+de.length,Q);if(F){_e=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:ke(o,F)}};break}if(yield*$(),yield*se("completed"),yield*Y("completed"),fe.size===0)for(let me of de){let Ae=I++,ot=me.id??me.call_id??`fc_${Ae}`,Nt=me.arguments??"",cn={id:ot,type:"function_call",call_id:me.call_id??ot,name:me.name??""};yield{type:"response.output_item.added",output_index:Ae,item:{...cn,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:ot,output_index:Ae,delta:Nt},yield{type:"response.function_call_arguments.done",item_id:ot,output_index:Ae,arguments:Nt};let pn={...cn,arguments:Nt,status:"completed"};yield{type:"response.output_item.done",output_index:Ae,item:pn},h(Ae,pn)}let X=Ro(_,O.response);yield X.length>0?l({...O,response:{...O.response,output:X}}):l(O);continue}yield l(O)}if(yield*$(),yield*se("incomplete"),yield*Y("incomplete"),i&&!_e){let O=ie(c);d.remember(r,n,Bt(O,Ce(c,O)),a)}}var Ao="/v1/responses",Io=64*1024*1024,Oo=8*1024*1024,To=16,Vn=32,Gn=3600*1e3,Po=/^[A-Za-z0-9._-]{1,256}$/;function ee(t,e,n){return{type:"error",status:t,...n!==void 0?{stream_id:n}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var Co=ee(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 Vt(){let t,e;return{promise:new Promise((r,o)=>{t=r,e=o}),resolve:t,reject:e}}async function $o(t){for(;t.bufferedAmount>Oo&&t.readyState===he.WebSocket.OPEN;){let{promise:e,resolve:n}=Vt();setTimeout(n,5),await e}}var jt=class extends Oe{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 be))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)}},Ht=class{constructor(e,n,r,o){this.ws=e;this.clients=n;this.stats=r;this.statusOf=o;this.store=new jt(Be(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(ee(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${Gn/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},Gn).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(ee(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(ee(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(ee(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(ee(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(ee(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(ee(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"||!Po.test(o.stream_id)){this.sendFrame(Co);return}if(s=o.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=Vn){this.sendFrame(ee(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${Vn}). Reuse an existing stream_id or open a new WebSocket connection.`,param:"stream_id"},s));return}}let i=o.generate===!1?()=>this.runWarmup(o,s):()=>this.runTurn(o,s),a=s??"",d=this.lanes.get(a)??Promise.resolve();this.lanes.set(a,d.then(i).catch(()=>{}))}namedLaneCount(){return this.lanes.has("")?this.lanes.size-1:this.lanes.size}async runTurn(e,n){if(!this.closed){await this.acquire();try{let r=Buffer.byteLength(JSON.stringify(e));this.stats.request("responses_ws",r,r);let o;try{o=await qe({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:Fe(De())})}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!==he.WebSocket.OPEN)break;this.send(i,n),await $o(this.ws)}}finally{this.activeTurns.delete(s)}}catch(r){this.failTurn(r,n,e)}finally{this.release()}}}runWarmup(e,n){if(this.closed)return;let r=Fe(De());try{let o=$e(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 oe)return ee(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 ee(404,{type:"invalid_request_error",code:"model_not_found",message:r},n);if(e instanceof ne)return ee(503,{type:"upstream_error",code:"session_gone",message:r},n);let o=this.statusOf?.(e)??null;return o?ee(o.status,{type:o.openaiType,message:r},n):ee(500,{type:"upstream_error",message:r},n)}async acquire(){for(;this.active>=To;){let{promise:e,resolve:n}=Vt();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!==he.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!==he.WebSocket.OPEN||this.ws.send(JSON.stringify(e))}};function mt(t,e,n){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${n}\r
|
|
8
8
|
`;t.end(`HTTP/1.1 ${e} ${r}\r
|
|
9
9
|
Connection: close\r
|
|
@@ -16,8 +16,8 @@ Content-Type: text/plain\r
|
|
|
16
16
|
\r
|
|
17
17
|
${Yo(n)}\r
|
|
18
18
|
`),t.destroy()}function Qo(t){return`${JSON.stringify({event:"realtime_upgrade",ts:new Date().toISOString(),...t})}
|
|
19
|
-
`}function Zo(t){xe(process.stdout,t)}function es(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function Xt(t){return t.audioSession?.audio.sessionId??void 0}function Ve(t,e){let n=new er.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,s,i)=>{if(new URL(o.url??"/","http://localhost").pathname!==He)return;r.add(s),s.on("close",()=>{r.delete(s)});let d=S("rtup"),u=es(o),c=f=>{(e.log??Zo)(Qo(f))};c({stage:"accept",conn:d,model:u}),s.resume();let l=new AbortController,m=null,p=f=>{m??=f,l.abort(new Error(m))},y=()=>p("client socket closed"),I=()=>p("client socket closed (half-close)"),x=f=>p(`client socket error: ${f.message}`);s.once("close",y),s.once("end",I),s.once("error",x);let _=()=>{s.removeListener("close",y),s.removeListener("end",I),s.removeListener("error",x)},h=f=>{c({stage:"close",conn:d,model:u,...f})},g=f=>{let v=f,k=v.session!=null&&typeof v.session.sendResponseCreate=="function",$=v.clients!=null&&typeof v.clients.createResponse=="function";if(!k&&!$)throw new Error("resolver returned a binding without an upstream turn seam (session.sendResponseCreate or clients.createResponse required) \u2014 refusing the upgrade LOUD");return new Promise((b,R)=>{n.handleUpgrade(o,s,i,P=>{try{new Se({ws:P,binding:f,onClosed:(D,K)=>{h({while:"live",reason:K.reason})}}),c({stage:"admit",conn:d,model:u,...Xt(f)===void 0?{}:{session:Xt(f)}}),b()}catch(D){R(D)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o,l.signal).then(async f=>{if(l.signal.aborted){_();let v=Xt(f);try{await f.release?.()}catch(k){h({while:"queued",...v===void 0?{}:{session:v},reason:m??"client socket closed",release_failed:k instanceof Error?k.message:String(k)});return}h({while:"queued",...v===void 0?{}:{session:v},reason:m??"client socket closed",released:!0}),s.destroy();return}_(),await g(f)}).catch(f=>{let v=f instanceof Zn.SessionFailedError?ht(f):null,k=null;if(f!==null&&typeof f=="object"&&"status"in f){let R=f.status;typeof R=="number"&&(k=R)}let $=k??v?.status??502,b=f instanceof Error?f.message:String(f);h({while:"queued",...l.signal.aborted?{reason:m??"client socket closed"}:{reason:b,status:$}}),s.destroyed||Xo(s,$,b)}).finally(()=>{r.delete(s)})}),async()=>{let o=Date.now()+5e3;for(;r.size>0&&Date.now()<o;)await new Promise(s=>setTimeout(s,25));await new Promise(s=>n.close(()=>s()))}}var Ge=(t,e)=>Object.assign(new Error(e),{status:t});function Qt(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}async function Zt(t,e,n){if(!t.supportsAudio)throw Ge(501,"the realtime lane needs a router-backed client (supportsAudio) to gate audio modality \u2014 hand-built clients without the catalog oracle are refused, never allowed silently");let r;try{r=await t.supportsAudio(e)}catch(a){throw a instanceof N?Ge(404,a.message):a}if(!r)throw Ge(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 Ge(501,"an audio-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the connection\u2019s pooled session \u2014 refusing loudly rather than binding a voiceless session");let o=await t.sessionHandle(e),s={audio:await t.openAudioOn(o,n)},i={clients:{createResponse:async a=>await t.createResponseOn(o,a)},voice:r,audioSession:s};return e!==void 0&&(i.model=e),i}function bt(t,e){return{resolve:async(n,r)=>{if(e!==void 0){let o=n.headers.authorization,s=typeof o=="string"?o.trim():"",i=s.startsWith("Bearer ")?s.slice(7).trim():"";if(i===""||i!==e)throw Ge(401,"invalid local proxy api key")}return Zt(t,Qt(n),r)}}}var z=class extends Error{};async function kt(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new z(`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 z(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function Rt(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 z(`request body exceeds the ${e}-byte limit`));return}i.enqueue(a.value)}catch(a){i.error(a)}},cancel(){n.return?.()}}),s;try{let i=new Headers;for(let[u,c]of Object.entries(t.headers))if(typeof c=="string")i.set(u,c);else if(Array.isArray(c))for(let l of c)i.append(u,l);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 z?i:new Error(`malformed multipart body: ${String(i instanceof Error?i.message:i)}`)}return s}var tr="/v1/images/generations",xt="/v1/images/edits";function nr(t){return t===tr||t===xt}var rr="/v1/images/models";async function or(t,e,n){if(t){if(t.model=Z({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 C=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},ts=16*1024*1024,sr=64*1024*1024;function ir(t){return t.length>=8&&t[0]===137&&t[1]===80&&t[2]===78&&t[3]===71&&t[4]===13&&t[5]===10&&t[6]===26&&t[7]===10?"image/png":t.length>=3&&t[0]===255&&t[1]===216&&t[2]===255?"image/jpeg":t.length>=12&&t[0]===82&&t[1]===73&&t[2]===70&&t[3]===70&&t[8]===87&&t[9]===69&&t[10]===66&&t[11]===80?"image/webp":null}function Je(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}async function ar(t,e,n,r){let o=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},r),s=null;for await(let l of o){if(l.type==="error"){let m=l.error??{};throw new C(502,`image upstream error: ${String(m.message??"upstream reported an error")}`)}if(l.type==="response.completed"){s=l.response??null;break}}if(s===null)throw new C(502,"image upstream produced no terminal response \u2014 refusing to fabricate an image");let i=s.images;if(!Array.isArray(i)||i.length!==1||typeof i[0]!="string"||i[0].length===0)throw new C(502,`image upstream returned ${Array.isArray(i)?i.length:"a non-array images field"} images for the single-shot (n=1) contract, which produces exactly one \u2014 refusing to truncate or fabricate`);let a=[],d=i[0].startsWith("data:")?i[0].slice(i[0].indexOf(";base64,")+8):i[0],u=Buffer.from(d,"base64");if(ir(u)===null)throw new C(502,`image upstream returned a payload that is not a PNG/JPEG/WebP image (${u.length} bytes) \u2014 refusing to pass it through`);return a.push(d),{images:a}}function ur(t){return t instanceof C?t:t instanceof N?new C(404,t.message):new C(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function dr(t,e,n,r){if(await t(e,n))return!0;let o=n==="edit"?"no image-input (expects_image) edit destination":"no pure text-to-image (expects_image=false) generation destination";return Je(r,new C(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 lr(t,e){t&&(t.outputUnits=e.images.length)}function cr(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 ns={model:!0,prompt:!0,response_format:!0,n:!0};async function pr(t,e,n){let r=n.maxBodyBytes??sr;try{if(t.method!=="POST")throw new C(405,`${String(t.method)} is not supported on ${tr}`);let o;try{o=JSON.parse((await kt(t,r)).toString("utf8")||"{}")}catch(l){throw l instanceof z?new C(413,l.message):new C(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new C(400,"request body must be a JSON object");let s=o;for(let l of Object.keys(s))if(!ns[l])throw new C(400,`request field "${l}" 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.`,l);let i=s.prompt;if(typeof i!="string"||i.trim().length===0)throw new C(400,"prompt is required and must be a non-empty string","prompt");let a=s.model===void 0?void 0:s.model;if(a!==void 0&&typeof a!="string")throw new C(400,"model must be a string","model");await or(n.turn,n.clients,a);let d=s.n??1;if(d!==1)throw new C(400,`n=${JSON.stringify(d)} is not supported: the native image request is single-shot and produces exactly one image per request (no batch support yet)`,"n");let u=s.response_format??"b64_json";if(u!=="b64_json")throw new C(400,`response_format ${JSON.stringify(u)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");if(n.stats?.request("images_generations",i.length,i.length),!await dr(n.imageModels,a,"generation",e))return;let c=[{type:"input_text",text:i}];try{let l=await ar(n.clients,a,c,n.turn?re(n.turn):void 0);lr(n.turn,l),cr(e,l)}catch(l){throw ur(l)}}catch(o){if(o instanceof z){Je(e,new C(413,o.message));return}if(o instanceof C){Je(e,o);return}throw o}}var rs={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function fr(t,e,n){let r=n.maxBodyBytes??sr,o=n.maxImagePartBytes??ts;try{if(t.method!=="POST")throw new C(405,`${String(t.method)} is not supported on ${xt}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new C(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await Rt(t,r)}catch(p){throw p instanceof z?new C(413,p.message):new C(400,String(p instanceof Error?p.message:p))}let a=i.get("prompt");if(typeof a!="string"||a.trim().length===0)throw new C(400,"prompt is required and must be a non-empty string field","prompt");let d=i.get("model");if(d!==null&&typeof d!="string")throw new C(400,"model must be a string field","model");let u=d===null?void 0:d;await or(n.turn,n.clients,u);let c=i.get("response_format")??"b64_json";if(c!=="b64_json")throw new C(400,`response_format ${JSON.stringify(c)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");for(let p of i.keys())if(!rs[p])throw new C(400,`form field "${p}" is not implemented: the native image request schema carries the prompt and the image inputs only (mask, size, seed and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, image (or image[]).`,p);let l=[];for(let p of["image","image[]"])for(let y of i.getAll(p)){if(!(y instanceof File))throw new C(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",p);if(y.size>o)throw new C(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,p);let I=new Uint8Array(await y.arrayBuffer()),x=ir(I);if(x===null)throw new C(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",p);l.push({bytes:I,mime:x})}if(l.length===0)throw new C(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",a.length,a.length),!await dr(n.imageModels,u,"edit",e))return;let m=[{type:"input_text",text:a}];for(let{bytes:p,mime:y}of l)m.push({type:"input_image",image_url:`data:${y};base64,${Buffer.from(p).toString("base64")}`});try{let p=await ar(n.clients,u,m,n.turn?re(n.turn):void 0);lr(n.turn,p),cr(e,p)}catch(p){throw ur(p)}}catch(s){if(s instanceof z){Je(e,new C(413,s.message));return}if(s instanceof C){Je(e,s);return}throw s}}var Ke="/v1/audio/speech";function _r(t){return t===Ke}var mr=4096,os=18e4,ss=5e3,is=64*1024*1024,gr=8,hr=5,Et="voice_applied",as="X-Urun-Voice-Applied",T=class extends Error{status;param;retryAfterSeconds;constructor(e,n,r,o){super(n),this.status=e,this.param=r,this.retryAfterSeconds=o}};function en(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 At=class extends T{constructor(e){super(502,e)}};async function us(t,e,n){if(t){if(t.model=Z({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 ds(t,e){t&&(t.outputUnits=e/(24e3*2))}var ls=2;function cs(t){for(let e=0;e+1<t.byteLength;e+=2)if(Math.abs(t.readInt16LE(e))>ls)return!1;return!0}function ps(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 yr=new WeakMap,St=new WeakMap;function fs(t,e){let n=St.get(t)??0;if(n>=gr)throw new T(503,`this model's speech queue is full \u2014 ${gr} requests are already admitted (one in-flight turn plus the queued depth) and each holds a pooled session turn; retry after ${hr}s`,void 0,hr);St.set(t,n+1);let r=yr.get(t)??Promise.resolve(),o,s=new Promise(i=>{o=i});return yr.set(t,s),(async()=>{await r;try{return await e()}finally{St.set(t,(St.get(t)??1)-1),o()}})()}async function ms(t,e,n,r){if(typeof t.evictSpeechSession!="function")throw new T(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned session after a failed turn \u2014 refusing loudly rather than handing the next request the failed turn\u2019s audio");try{await t.evictSpeechSession(e)}catch(i){if(!(i instanceof ne))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 We(t){return t instanceof T?t:t instanceof N?new T(404,t.message):new T(502,`speech upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function gs(t,e,n,r,o,s,i){return fs(e,async()=>{if(i?.()===!0)return null;let a=[],d=!1,u=0,c=!1,l=!1,m,p=new Promise(E=>{m=E}),y=()=>{c&&!l&&u>=o.quietRunFrames&&(l=!0,m())},I=e.onOutputAudio(E=>{let f=Buffer.from(E,"base64");if(f.byteLength===0)return;let v=cs(f);if(!d){if(v)return;d=!0}a.push(f),u=v?u+1:0,y()}),x=null,_,h,g;try{g=(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 f=new Promise(b=>{h=setTimeout(b,s.capMs)}),v=!1;for(;;){let b=g.next(),R=await Promise.race([b,f]);if(R===void 0){v=!0,b.catch(()=>{});break}if(R.done)break;let P=R.value;if(P.type==="error"){let D=P.error??{};throw await g.return?.(),new T(502,`speech upstream error: ${String(D.message??"upstream reported an error")}`)}if(P.type==="response.completed"){x=P.response??null,c=!0,y(),await g.return?.();break}}if(clearTimeout(h),v)throw new T(504,`the speech turn produced no terminal event within ${s.capMs}ms \u2014 the upstream stream never completed, so the pinned speech session is evicted (the next request opens a fresh one), the serialized lane is released and the caller can retry; a stuck turn must not hold every later request on this lane`);if(x===null)throw new T(502,"speech upstream produced no terminal response \u2014 refusing to fabricate audio");let k=!1;if(await Promise.race([p,new Promise(b=>{_=setTimeout(()=>{k=!0,b()},o.capMs)})]),_!==void 0&&clearTimeout(_),r.voice!==void 0){let b=x[Et];if(typeof b!="string"||b!==r.voice){let R=`the runtime did not acknowledge the requested voice ${JSON.stringify(r.voice)} \u2014 the terminal response carried ${typeof b=="string"?`${Et} ${JSON.stringify(b)}`:`no ${Et} field`}; the speak-bridge companion is not deployed, and this lane never serves default-voice audio as the requested voice`;throw l?new At(R):new T(502,R)}}if(!d)throw new T(502,"the upstream produced no voice for this utterance \u2014 refusing to serve silence as success");if(k&&!l&&u<1)throw new T(502,`the upstream audio stream did not quiesce within ${o.capMs}ms of the turn's end (voiced frames still arriving) \u2014 refusing to serve audio whose tail cannot be vouched for`);let $=u>0?a.slice(0,a.length-u):a;return{pcm:Buffer.concat($),completed:x}}catch(E){throw E instanceof At||await ms(t,n,g,s.returnBoundMs),E}finally{_!==void 0&&clearTimeout(_),h!==void 0&&clearTimeout(h),I()}})}async function hs(t,e,n){let r;try{r=await t(e)}catch(o){throw We(o)}return r?!0:(en(n,new T(404,e===void 0?"no speech-capable default model is configured for this caller (catalog task tts) \u2014 name a tts model":`model "${e}" is not speech-capable (catalog task is not tts) \u2014 this lane serves only text-to-speech models; chat models ride /v1/chat/completions`)),!1)}var ys={model:!0,input:!0,voice:!0,response_format:!0,stream_format:!0,speed:!0,instructions:!0},_s="wav, pcm";async function wr(t,e,n){let r=n.maxBodyBytes??is,o=!1;e.once("close",()=>{o=!0});let s=()=>o||e.destroyed;try{if(t.method!=="POST")throw new T(405,`${String(t.method)} is not supported on ${Ke}`);let i;try{i=JSON.parse((await kt(t,r)).toString("utf8")||"{}")}catch(g){throw g instanceof z?new T(413,g.message):new T(400,"request body is not valid JSON")}if(i===null||typeof i!="object"||Array.isArray(i))throw new T(400,"request body must be a JSON object");let a=i;for(let g of Object.keys(a))if(!ys[g])throw new T(400,`request field "${g}" 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.`,g);let d=a.input;if(typeof d!="string"||d.trim().length===0)throw new T(400,"input is required and must be a non-empty string","input");if(d.length>mr)throw new T(400,`input is ${d.length} chars \u2014 over the ${mr}-char limit (one utterance; the backend bounds generation text-shaped, and an unbounded text is an unbounded synthesis)`,"input");let u=a.model===void 0?void 0:a.model;if(u!==void 0&&typeof u!="string")throw new T(400,"model must be a string","model");let c=a.voice;if(c!==void 0&&(typeof c!="string"||c.trim().length===0))throw new T(400,"voice must be a non-empty string","voice");if(typeof n.clients.speechVoices!="function")throw new T(501,`the speech lane needs a router-backed client (speechVoices) to resolve the voice set GET /v1/models publishes \u2014 refusing loudly rather than validating voices of ${Ke} against a stale copy of the list`);let l;try{l=await n.clients.speechVoices(u)}catch(g){throw We(g)}if(l.defect!==null)throw new T(500,`the voice set for this model cannot be resolved: ${l.defect} \u2014 fix the catalog row; the lane refuses to serve rather than validate against a set nobody vouched`);let m=typeof c=="string"?c:void 0;if(m!==void 0&&l.published.length>0&&!l.published.includes(m))throw new T(400,`voice ${JSON.stringify(m)} is not one of the voices this model publishes \u2014 accepted voices: ${l.published.join(", ")}`,"voice");let p=a.response_format??"wav";if(p!=="wav"&&p!=="pcm")throw new T(400,`response_format ${JSON.stringify(p)} is not supported: this lane serves the engine's raw 24 kHz mono PCM16, so the supported formats are ${_s} \u2014 and the lane will not label bytes with a format it did not encode`,"response_format");if(a.stream_format!==void 0&&typeof a.stream_format!="string")throw new T(400,"stream_format, when present, must be a string","stream_format");let y=a.speed;if(y!==void 0){if(typeof y!="number"||!Number.isFinite(y))throw new T(400,"speed, when present, must be a number","speed");if(y!==1)throw new T(400,`speed=${JSON.stringify(y)} 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 I=a.instructions;if(I!==void 0){if(typeof I!="string")throw new T(400,"instructions, when present, must be a string","instructions");if(I.trim().length>0)throw new T(400,"instructions are not supported: the backend fixes the voice style (instruct) at deploy time, so a per-request instruction would be silently ignored \u2014 the deployment speaks with its configured style","instructions")}await us(n.turn,n.clients,u);let x=Buffer.byteLength(JSON.stringify(a));if(n.stats?.request("audio_speech",x,x),!await hs(n.speechModels,u,e))return;if(typeof n.clients.openAudioOn!="function")throw new T(501,"a speech-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the same session identity as the response turn \u2014 refusing loudly rather than answering an utterance whose audio cannot be collected");if(typeof n.clients.speechSessionHandle!="function")throw new T(501,"a speech-capable model needs a router-backed client (speechSessionHandle) to resolve the speech lane's own pooled session \u2014 one no interactive binding shares, because the per-session audio bridge broadcasts every output frame to every subscriber \u2014 refusing loudly rather than collecting speech audio on a session a realtime/gemini lane can be speaking on");if(typeof n.clients.evictSpeechSession!="function")throw new T(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned speech session after a failed turn (a turn-cap timeout above all) \u2014 without it, the next request on the same model would silently collect the failed turn\u2019s still-emitting audio; refusing loudly rather than serving on an un-evictable speech slot");if(s())return;let _;try{_=await n.clients.speechSessionHandle(u)}catch(g){throw We(g)}let h;try{h=await n.clients.openAudioOn(_)}catch(g){throw We(g)}n.turn&&(n.turn.sessionId=h.sessionId);try{let g=await gs(n.clients,h,_,{model:u,text:d,...m!==void 0?{voice:m}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??os,returnBoundMs:n.turnReturnBoundMs??ss},s);if(g===null||s())return;if(ds(n.turn,g.pcm.byteLength),e.statusCode=200,m===void 0){let E=g.completed[Et];typeof E=="string"&&e.setHeader(as,E)}p==="wav"?(e.setHeader("content-type","audio/wav"),e.end(Buffer.concat([ps(g.pcm.byteLength),g.pcm]))):(e.setHeader("content-type","audio/pcm"),e.end(g.pcm))}catch(g){throw We(g)}}catch(i){if(i instanceof z){en(e,new T(413,i.message));return}if(i instanceof T){en(e,i);return}throw i}}var It="/v1/audio/transcriptions";function vr(t){return t===It}var br="/v1/audio/models",ws=16*1024*1024,vs=64*1024*1024,B=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}};function tn(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 kr(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 bs(t){if(!kr(t))return null;let e=12,n=null,r=null;for(;e+8<=t.length;){let s=String.fromCharCode(t[e],t[e+1],t[e+2],t[e+3]),i=(t[e+4]|t[e+5]<<8|t[e+6]<<16|t[e+7]<<24)>>>0,a=e+8;if(s==="fmt "&&a+16<=t.length){let d=t[a+2]|t[a+3]<<8,u=(t[a+4]|t[a+5]<<8|t[a+6]<<16|t[a+7]<<24)>>>0,c=t[a+14]|t[a+15]<<8;n={sampleRate:u,channels:d,bitsPerSample:c}}else if(s==="data"){let d=t.length-a;r=Math.min(i===0||i===4294967295?d:i,d)}e=a+i+i%2}if(n===null||r===null||r===0||n.sampleRate===0||n.channels===0||n.bitsPerSample===0)return null;let o=r/(n.sampleRate*n.channels*(n.bitsPerSample/8));return!Number.isFinite(o)||o<0?null:{seconds:o,sampleRate:n.sampleRate,channels:n.channels,bitsPerSample:n.bitsPerSample}}async function ks(t,e,n){if(t){if(t.model=Z({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 Rs(t,e){t&&(t.outputUnits=e.seconds)}async function xs(t,e,n,r,o){let s=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1,...r!==void 0?{language:r}:{}},o),i=[],a=null;for await(let d of s){if(i.push(d),d.type==="error"){let u=d.error??{},c=typeof u.type=="string"?u.type:"upstream_error",l=String(u.message??"upstream reported an error");throw new B(c==="invalid_request_error"?400:502,`transcription upstream error: ${l}`)}if(d.type==="response.completed"){a=d.response??null;break}}if(a===null)throw new B(502,"transcription upstream produced no terminal response \u2014 refusing to fabricate a transcript");return Ce(i,a)}function Rr(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 Ss(t,e,n){let r;try{r=await t(e)}catch(o){throw Rr(o)}return r?!0:(tn(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 Es=new Set(["file","model","language","response_format"]);async function xr(t,e,n){let r=n.maxBodyBytes??vs,o=n.maxAudioPartBytes??ws;try{if(t.method!=="POST")throw new B(405,`${String(t.method)} is not supported on ${It}`);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 Rt(t,r)}catch(_){throw _ instanceof z?new B(413,_.message):new B(400,String(_ instanceof Error?_.message:_))}let a=new Map;for(let _ of i.keys()){if(!Es.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[_,h]of a)if(h>1)throw new B(400,`form field "${_}" appears ${h} 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 d=i.get("model");if(d!==null&&typeof d!="string")throw new B(400,"model must be a string field","model");let u=d===null?void 0:d,c=i.get("language");if(c!==null&&(typeof c!="string"||c.trim().length===0))throw new B(400,"language, when present, must be a non-empty string","language");let l=c===null?void 0:c,m=i.get("response_format")??"json";if(m!=="json")throw new B(400,`response_format ${JSON.stringify(m)} is not supported: timestamps/segments are not produced on this lane yet \u2014 the runtime answers terminal transcript text only, so json is the only output format`,"response_format");let p=i.get("file");if(!(p instanceof File))throw new B(400,"file must be an uploaded WAV: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths","file");if(p.size>o)throw new B(413,`file part exceeds the ${o}-byte limit (the serve protocol's MAX_AUDIO_BYTES)`,"file");let y=new Uint8Array(await p.arrayBuffer());if(!kr(y))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 I=bs(y);if(I===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 ks(n.turn,n.clients,u),n.stats?.request("audio_transcriptions",y.length,y.length),!await Ss(n.transcriptionModels,u,e))return;let x=[{type:"input_audio",input_audio:{data:Buffer.from(y).toString("base64"),format:"wav"}}];try{let _=await xs(n.clients,u,x,l,n.turn?re(n.turn):void 0);Rs(n.turn,I),e.statusCode=200,e.setHeader("content-type","application/json"),e.end(JSON.stringify({text:_}))}catch(_){throw Rr(_)}}catch(s){if(s instanceof z){tn(e,new B(413,s.message));return}if(s instanceof B){tn(e,s);return}throw s}}var Er="/v1/usage/requests",Sr=1e4,As=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,ye=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},Le=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}};var Ar=1024*1024;function nn(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new ye(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new ye(`"${e}" must be an array of uuid strings`);if(n.length>Sr)throw new ye(`"${e}" carries ${n.length} ids; at most ${Sr} may be looked up in one request. Split the batch \u2014 the ids past the limit are NOT silently dropped`);let r=[],o=new Set;for(let s of n){if(typeof s!="string"||!As.test(s))throw new ye(`"${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 Ir(t){return nn(t,"inference_ids")}function Ot(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 Le(`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 Le(`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 Mt=require("@urun-sh/core");var rn="/partners/huggingface/v1/billing/requests";function Is(t){return nn(t,"requestIds")}function Os(t){return t>=200&&t<=399}function Ts(t){let e=[];for(let n of t)n.cost_nano_usd!==null&&Os(n.http_status)&&e.push({requestId:n.inference_id,costNanoUsd:n.cost_nano_usd});return{requests:e.length>0?e:null}}async function Or(t,e){let n=Is(t),r=await e(n);return Ot(r,n),Ts(r)}var Ps={"/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 Cs(t){return Ps[t]??null}var $r=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function $s(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function Ms(t){return t==="surface"}function Ls(t){let e=t?.ledgerFailureReason;return $r.includes(e)?e:"unknown"}var Ns=["no_response_head","no_org","model_unresolved","no_model_named","queue_full"],Tr=["priced","unpriced"],Us={written:Tr,already_written:Tr,skipped:Ns,failed:$r},xu=Object.entries(Us).flatMap(([t,e])=>e.map(n=>({outcome:t,reason:n})));function Ds(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function Fs(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 Bs=256,Pr=4096,Ye=12e4,qs=250,js=8e3,Xe=[],Cr=new Set,Qe=0,ze=!1;var Hs=new Set;function Vs(){return Qe===0&&Xe.length===0}function Gs(){if(Vs()){ze=!1;for(let t of[...Hs])t()}}function Mr(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
|
|
20
|
-
`}function Js(t){xe(process.stdout,t)}function Lr(t,e){try{t(e)}catch{}}function Nr(t){let{record:e,ledger:n}=t;if(n===null)return;let r=n.report??Js,o=Cs(e.path);if(o===null)return;let s=c=>{try{n.observe(c)}catch{}},i=(c,l)=>{s({outcome:"skipped",reason:c}),Lr(r,Mr("inference_ledger_skipped",{inference_id:e.inference_id,path:e.path,reason:c,detail:l}))};if(e.status===null){i("no_response_head","no head flushed, so the caller never received an Inference-Id and no status can be recorded honestly");return}if(e.org===null){i("no_org","the request never resolved a tenant, so there is no org to attribute the row to");return}let a=e.catalog;if(a.kind==="unresolved"){i("model_unresolved",a.reason);return}let d;if(a.kind==="catalog")d={model_id:a.model_id,identity_kind:"catalog_model",variant:a.variant};else{if(a.model===null){i("no_model_named","the request resolved to a caller-org app but named no model, so there is no app slug to record and `model_id` cannot be written honestly");return}d={model_id:a.model,identity_kind:"caller_org_app",variant:null}}let u={inference_id:e.inference_id,...d,task:o,http_status:e.status,started_at:Fs(e),completed_at:e.ts,billable_outcome:Ds(e),session_id:e.session_id,prompt_tokens:e.prompt_tokens,completion_tokens:e.completion_tokens,output_units:e.output_units,queue_ms:e.queue_ms,prefill_ms:e.prefill_ms,decode_ms:e.decode_ms,total_ms:e.total_ms,ttft_ms:e.ttft_ms===null?null:Math.round(e.ttft_ms),e2e_ms:Math.round(e.duration_ms)};if(Xe.length>=Pr){i("queue_full",`${Xe.length} ledger writes are already waiting (capacity ${Pr}, ${Qe} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}Xe.push({entry:u,req:t.req,record:e,ledger:n,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),Qe<Bs&&(Qe+=1,Ws())}async function Ws(){try{for(;;){let t=Ks();if(t===void 0)return;Cr.add(t);try{await zs(t)}finally{Cr.delete(t)}}}finally{Qe-=1,Gs()}}function Ks(){let t=Date.now();for(;;){let e=Xe.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<Ye)return e;Ne(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 ${Ye}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 zs(t){let{entry:e,record:n,ledger:r}=t;for(;;){t.attempts+=1;try{let o=await r.write(t.req,e);Ne(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=$s(o);if(s!==null){Ne(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=Ls(o),a=Date.now()-t.queuedAt;if(ze){Ne(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(Ms(i)&&a<Ye){if(await Xs(Ys(t.attempts)),ze||Date.now()-t.queuedAt>=Ye){let d=Date.now()-t.queuedAt;Ne(t,{outcome:"failed",reason:ze?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:ze?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:d,error:`this row's backoff outlived the ${Ye}ms dwell bound (waited ${d}ms over ${t.attempts} attempt(s)) and was given up on rather than starting an attempt that could only finish past it. Last failure: ${o instanceof Error?o.message:String(o)}`});return}continue}Ne(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 Ys(t){return Math.min(js,qs*2**Math.max(0,t-1))}function Xs(t){return new Promise(e=>{setTimeout(e,t).unref?.()})}function Ne(t,e,n){if(t.settled)return;t.settled=!0;try{t.ledger.observe(e)}catch{}let{event:r,...o}=n;Lr(t.report,Mr(r,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...o}))}var sn="x-urun-profile",Ze="voice";var te=class extends Error{status=400},Qs=/(?:[.!?:;]|\n)[)\]"'\u2019\u201d\u00bb\u300d\u300b\u3011\uff09\uff3d]?\s*$/,Zs=/\s/;function ei(t){return Qs.test(t)}function on(t,e,n){let r=t[e];if(r===void 0)return n;if(typeof r!="boolean")throw new te(`stream_options.voice.${e} must be a boolean when present (got ${typeof r})`);return r}function Ur(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 te(`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 te(`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 te(`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 Dr(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 te(`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 u=n.trim();if(u==="")throw new te(`the ${sn} header is empty \u2014 the only profile this lane accepts is "${Ze}" (omit the header for the ordinary lane)`);if(u!==Ze)throw new te(`unknown profile "${u}" \u2014 the only profile this lane accepts is "${Ze}"`);i=!0}if(s!==void 0){if(typeof s!="string"||s!==Ze)throw new te(`unknown profile ${JSON.stringify(s)} \u2014 the only profile this lane accepts is "${Ze}"`);i=!0}if(!i)return null;if(e.stream!==!0)throw new te("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 te("stream_options.voice must be an object when present (flush_max_chars, flush_max_ms, sentence_flush, include_reasoning, metadata)");let d=a??{};return{flushMaxChars:Ur(d,"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:Ur(d,"flush_max_ms",250,{max:2147483647}),sentenceFlush:on(d,"sentence_flush",!0),includeReasoning:on(d,"include_reasoning",!1),metadata:on(d,"metadata",!1)}}var Tt=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&&ei(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(Zs.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)}};var ti=Ut(require("crypto"),1);var ni=6e4,Ou=ni/1e3+5;var Fr=15;var Tu=Fr*1e3,Pu=Fr*500;var et=class extends Error{constructor(n,r){super(`runtime answered ${n}`);this.status=n;this.body=r}status;body},tt=class extends Error{};function dn(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function ri(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 G(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function J(t,e,n,r="invalid_request_error",o){G(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function ln(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);G(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function ae(t,e,n,r){G(t,e,{type:"error",error:{type:n,message:r}})}function ue(t,e,n){if(e==="anthropic"){G(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}G(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var oi=64*1024*1024,nt=class extends Error{};async function Br(t,e=oi){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 Ee(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 Hr(t){let e=t?.usage;if(!e||typeof e!="object")return{kind:"absent"};let n=e,r=d=>typeof d=="number"&&Number.isFinite(d)&&d>=0?d:null,o=r(n.input_tokens),s=r(n.output_tokens),i=d=>d??0,a=r(n.total_tokens)??i(o)+i(s);if(i(o)>0||i(s)>0){let d={input_tokens:i(o),output_tokens:i(s),total_tokens:a};return o===null||s===null?{kind:"partial",usage:d}:{kind:"counts",usage:d}}return o===null||s===null?{kind:"absent"}:{kind:"zero",usage:{input_tokens:0,output_tokens:0,total_tokens:a}}}function Pt(t){let e=Hr(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function qr(t){let e=Pt(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 si(t,e){let n=Hr(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function ii(t){let e=t?.urun_timing;if(!e||typeof e!="object")return null;let n=e,r=d=>{let u=n[d];return typeof u=="number"&&Number.isSafeInteger(u)&&u>=0?u:null},o=r("queue_ms"),s=r("prefill_ms"),i=r("decode_ms"),a=r("total_ms");return o===null||s===null||i===null||a===null?null:{queue_ms:o,prefill_ms:s,decode_ms:i,total_ms:a}}function ai(t,e){let n=ii(e);n&&(t.timing=n)}function pe(t,e){si(t,e),ai(t,e)}async function Lt(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 Vr(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 ui(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 Ct=class extends Error{};function di(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 Ct(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function li(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:di(n.role,n.content)})}return e}function an(t){return Te(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 ci(t,e,n,r,o,s){let i=Fe(s.id),a=t.stream===!0;if(s.model=Z(t),s.stream=a,await Lt(s,e,typeof t.model=="string"?t.model:void 0),a){let _;try{_=await qe({body:t,clients:e,store:n,stats:o,responseId:i,turn:s})}catch(h){if(h instanceof N){ue(r,"openai",h);return}if(h instanceof oe){J(r,h.status,h.message);return}throw h}dn(r);for await(let h of _.events)h.type==="error"&&(s.streamError=!0),h.type==="response.completed"&&pe(s,h.response),r.write(`event: ${String(h.type??"message")}
|
|
19
|
+
`}function Zo(t){xe(process.stdout,t)}function es(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function Xt(t){return t.audioSession?.audio.sessionId??void 0}function Ve(t,e){let n=new er.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,s,i)=>{if(new URL(o.url??"/","http://localhost").pathname!==He)return;r.add(s),s.on("close",()=>{r.delete(s)});let d=S("rtup"),u=es(o),c=f=>{(e.log??Zo)(Qo(f))};c({stage:"accept",conn:d,model:u}),s.resume();let l=new AbortController,m=null,p=f=>{m??=f,l.abort(new Error(m))},y=()=>p("client socket closed"),I=()=>p("client socket closed (half-close)"),x=f=>p(`client socket error: ${f.message}`);s.once("close",y),s.once("end",I),s.once("error",x);let _=()=>{s.removeListener("close",y),s.removeListener("end",I),s.removeListener("error",x)},h=f=>{c({stage:"close",conn:d,model:u,...f})},g=f=>{let v=f,k=v.session!=null&&typeof v.session.sendResponseCreate=="function",$=v.clients!=null&&typeof v.clients.createResponse=="function";if(!k&&!$)throw new Error("resolver returned a binding without an upstream turn seam (session.sendResponseCreate or clients.createResponse required) \u2014 refusing the upgrade LOUD");return new Promise((b,R)=>{n.handleUpgrade(o,s,i,P=>{try{new Se({ws:P,binding:f,onClosed:(D,K)=>{h({while:"live",reason:K.reason})}}),c({stage:"admit",conn:d,model:u,...Xt(f)===void 0?{}:{session:Xt(f)}}),b()}catch(D){R(D)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o,l.signal).then(async f=>{if(l.signal.aborted){_();let v=Xt(f);try{await f.release?.()}catch(k){h({while:"queued",...v===void 0?{}:{session:v},reason:m??"client socket closed",release_failed:k instanceof Error?k.message:String(k)});return}h({while:"queued",...v===void 0?{}:{session:v},reason:m??"client socket closed",released:!0}),s.destroy();return}_(),await g(f)}).catch(f=>{let v=f instanceof Zn.SessionFailedError?ht(f):null,k=null;if(f!==null&&typeof f=="object"&&"status"in f){let R=f.status;typeof R=="number"&&(k=R)}let $=k??v?.status??502,b=f instanceof Error?f.message:String(f);h({while:"queued",...l.signal.aborted?{reason:m??"client socket closed"}:{reason:b,status:$}}),s.destroyed||Xo(s,$,b)}).finally(()=>{r.delete(s)})}),async()=>{let o=Date.now()+5e3;for(;r.size>0&&Date.now()<o;)await new Promise(s=>setTimeout(s,25));await new Promise(s=>n.close(()=>s()))}}var Ge=(t,e)=>Object.assign(new Error(e),{status:t});function Qt(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}async function Zt(t,e,n){if(!t.supportsAudio)throw Ge(501,"the realtime lane needs a router-backed client (supportsAudio) to gate audio modality \u2014 hand-built clients without the catalog oracle are refused, never allowed silently");let r;try{r=await t.supportsAudio(e)}catch(a){throw a instanceof N?Ge(404,a.message):a}if(!r)throw Ge(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 Ge(501,"an audio-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the connection\u2019s pooled session \u2014 refusing loudly rather than binding a voiceless session");let o=await t.sessionHandle(e),s={audio:await t.openAudioOn(o,n)},i={clients:{createResponse:async a=>await t.createResponseOn(o,a)},voice:r,audioSession:s};return e!==void 0&&(i.model=e),i}function bt(t,e){return{resolve:async(n,r)=>{if(e!==void 0){let o=n.headers.authorization,s=typeof o=="string"?o.trim():"",i=s.startsWith("Bearer ")?s.slice(7).trim():"";if(i===""||i!==e)throw Ge(401,"invalid local proxy api key")}return Zt(t,Qt(n),r)}}}var z=class extends Error{};async function kt(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new z(`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 z(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function Rt(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 z(`request body exceeds the ${e}-byte limit`));return}i.enqueue(a.value)}catch(a){i.error(a)}},cancel(){n.return?.()}}),s;try{let i=new Headers;for(let[u,c]of Object.entries(t.headers))if(typeof c=="string")i.set(u,c);else if(Array.isArray(c))for(let l of c)i.append(u,l);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 z?i:new Error(`malformed multipart body: ${String(i instanceof Error?i.message:i)}`)}return s}var tr="/v1/images/generations",xt="/v1/images/edits";function nr(t){return t===tr||t===xt}var rr="/v1/images/models";async function or(t,e,n){if(t){if(t.model=Z({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 C=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},ts=16*1024*1024,sr=64*1024*1024;function ir(t){return t.length>=8&&t[0]===137&&t[1]===80&&t[2]===78&&t[3]===71&&t[4]===13&&t[5]===10&&t[6]===26&&t[7]===10?"image/png":t.length>=3&&t[0]===255&&t[1]===216&&t[2]===255?"image/jpeg":t.length>=12&&t[0]===82&&t[1]===73&&t[2]===70&&t[3]===70&&t[8]===87&&t[9]===69&&t[10]===66&&t[11]===80?"image/webp":null}function Je(t,e){t.statusCode=e.status,t.setHeader("content-type","application/json"),t.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}async function ar(t,e,n,r){let o=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},r),s=null;for await(let l of o){if(l.type==="error"){let m=l.error??{};throw new C(502,`image upstream error: ${String(m.message??"upstream reported an error")}`)}if(l.type==="response.completed"){s=l.response??null;break}}if(s===null)throw new C(502,"image upstream produced no terminal response \u2014 refusing to fabricate an image");let i=s.images;if(!Array.isArray(i)||i.length!==1||typeof i[0]!="string"||i[0].length===0)throw new C(502,`image upstream returned ${Array.isArray(i)?i.length:"a non-array images field"} images for the single-shot (n=1) contract, which produces exactly one \u2014 refusing to truncate or fabricate`);let a=[],d=i[0].startsWith("data:")?i[0].slice(i[0].indexOf(";base64,")+8):i[0],u=Buffer.from(d,"base64");if(ir(u)===null)throw new C(502,`image upstream returned a payload that is not a PNG/JPEG/WebP image (${u.length} bytes) \u2014 refusing to pass it through`);return a.push(d),{images:a}}function ur(t){return t instanceof C?t:t instanceof N?new C(404,t.message):new C(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function dr(t,e,n,r){if(await t(e,n))return!0;let o=n==="edit"?"no image-input (expects_image) edit destination":"no pure text-to-image (expects_image=false) generation destination";return Je(r,new C(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 lr(t,e){t&&(t.outputUnits=e.images.length)}function cr(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 ns={model:!0,prompt:!0,response_format:!0,n:!0};async function pr(t,e,n){let r=n.maxBodyBytes??sr;try{if(t.method!=="POST")throw new C(405,`${String(t.method)} is not supported on ${tr}`);let o;try{o=JSON.parse((await kt(t,r)).toString("utf8")||"{}")}catch(l){throw l instanceof z?new C(413,l.message):new C(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new C(400,"request body must be a JSON object");let s=o;for(let l of Object.keys(s))if(!ns[l])throw new C(400,`request field "${l}" 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.`,l);let i=s.prompt;if(typeof i!="string"||i.trim().length===0)throw new C(400,"prompt is required and must be a non-empty string","prompt");let a=s.model===void 0?void 0:s.model;if(a!==void 0&&typeof a!="string")throw new C(400,"model must be a string","model");await or(n.turn,n.clients,a);let d=s.n??1;if(d!==1)throw new C(400,`n=${JSON.stringify(d)} is not supported: the native image request is single-shot and produces exactly one image per request (no batch support yet)`,"n");let u=s.response_format??"b64_json";if(u!=="b64_json")throw new C(400,`response_format ${JSON.stringify(u)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");if(n.stats?.request("images_generations",i.length,i.length),!await dr(n.imageModels,a,"generation",e))return;let c=[{type:"input_text",text:i}];try{let l=await ar(n.clients,a,c,n.turn?re(n.turn):void 0);lr(n.turn,l),cr(e,l)}catch(l){throw ur(l)}}catch(o){if(o instanceof z){Je(e,new C(413,o.message));return}if(o instanceof C){Je(e,o);return}throw o}}var rs={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function fr(t,e,n){let r=n.maxBodyBytes??sr,o=n.maxImagePartBytes??ts;try{if(t.method!=="POST")throw new C(405,`${String(t.method)} is not supported on ${xt}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new C(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await Rt(t,r)}catch(p){throw p instanceof z?new C(413,p.message):new C(400,String(p instanceof Error?p.message:p))}let a=i.get("prompt");if(typeof a!="string"||a.trim().length===0)throw new C(400,"prompt is required and must be a non-empty string field","prompt");let d=i.get("model");if(d!==null&&typeof d!="string")throw new C(400,"model must be a string field","model");let u=d===null?void 0:d;await or(n.turn,n.clients,u);let c=i.get("response_format")??"b64_json";if(c!=="b64_json")throw new C(400,`response_format ${JSON.stringify(c)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");for(let p of i.keys())if(!rs[p])throw new C(400,`form field "${p}" is not implemented: the native image request schema carries the prompt and the image inputs only (mask, size, seed and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, image (or image[]).`,p);let l=[];for(let p of["image","image[]"])for(let y of i.getAll(p)){if(!(y instanceof File))throw new C(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",p);if(y.size>o)throw new C(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,p);let I=new Uint8Array(await y.arrayBuffer()),x=ir(I);if(x===null)throw new C(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",p);l.push({bytes:I,mime:x})}if(l.length===0)throw new C(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",a.length,a.length),!await dr(n.imageModels,u,"edit",e))return;let m=[{type:"input_text",text:a}];for(let{bytes:p,mime:y}of l)m.push({type:"input_image",image_url:`data:${y};base64,${Buffer.from(p).toString("base64")}`});try{let p=await ar(n.clients,u,m,n.turn?re(n.turn):void 0);lr(n.turn,p),cr(e,p)}catch(p){throw ur(p)}}catch(s){if(s instanceof z){Je(e,new C(413,s.message));return}if(s instanceof C){Je(e,s);return}throw s}}var Ke="/v1/audio/speech";function _r(t){return t===Ke}var mr=4096,os=18e4,ss=5e3,is=64*1024*1024,gr=8,hr=5,Et="voice_applied",as="X-Urun-Voice-Applied",T=class extends Error{status;param;retryAfterSeconds;constructor(e,n,r,o){super(n),this.status=e,this.param=r,this.retryAfterSeconds=o}};function en(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 At=class extends T{constructor(e){super(502,e)}};async function us(t,e,n){if(t){if(t.model=Z({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 ds(t,e){if(!t)return;let n=e/(24e3*2);t.outputUnits=n,t.outputMinutes=n/60}var ls=2;function cs(t){for(let e=0;e+1<t.byteLength;e+=2)if(Math.abs(t.readInt16LE(e))>ls)return!1;return!0}function ps(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 yr=new WeakMap,St=new WeakMap;function fs(t,e){let n=St.get(t)??0;if(n>=gr)throw new T(503,`this model's speech queue is full \u2014 ${gr} requests are already admitted (one in-flight turn plus the queued depth) and each holds a pooled session turn; retry after ${hr}s`,void 0,hr);St.set(t,n+1);let r=yr.get(t)??Promise.resolve(),o,s=new Promise(i=>{o=i});return yr.set(t,s),(async()=>{await r;try{return await e()}finally{St.set(t,(St.get(t)??1)-1),o()}})()}async function ms(t,e,n,r){if(typeof t.evictSpeechSession!="function")throw new T(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned session after a failed turn \u2014 refusing loudly rather than handing the next request the failed turn\u2019s audio");try{await t.evictSpeechSession(e)}catch(i){if(!(i instanceof ne))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 We(t){return t instanceof T?t:t instanceof N?new T(404,t.message):new T(502,`speech upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function gs(t,e,n,r,o,s,i){return fs(e,async()=>{if(i?.()===!0)return null;let a=[],d=!1,u=0,c=!1,l=!1,m,p=new Promise(E=>{m=E}),y=()=>{c&&!l&&u>=o.quietRunFrames&&(l=!0,m())},I=e.onOutputAudio(E=>{let f=Buffer.from(E,"base64");if(f.byteLength===0)return;let v=cs(f);if(!d){if(v)return;d=!0}a.push(f),u=v?u+1:0,y()}),x=null,_,h,g;try{g=(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 f=new Promise(b=>{h=setTimeout(b,s.capMs)}),v=!1;for(;;){let b=g.next(),R=await Promise.race([b,f]);if(R===void 0){v=!0,b.catch(()=>{});break}if(R.done)break;let P=R.value;if(P.type==="error"){let D=P.error??{};throw await g.return?.(),new T(502,`speech upstream error: ${String(D.message??"upstream reported an error")}`)}if(P.type==="response.completed"){x=P.response??null,c=!0,y(),await g.return?.();break}}if(clearTimeout(h),v)throw new T(504,`the speech turn produced no terminal event within ${s.capMs}ms \u2014 the upstream stream never completed, so the pinned speech session is evicted (the next request opens a fresh one), the serialized lane is released and the caller can retry; a stuck turn must not hold every later request on this lane`);if(x===null)throw new T(502,"speech upstream produced no terminal response \u2014 refusing to fabricate audio");let k=!1;if(await Promise.race([p,new Promise(b=>{_=setTimeout(()=>{k=!0,b()},o.capMs)})]),_!==void 0&&clearTimeout(_),r.voice!==void 0){let b=x[Et];if(typeof b!="string"||b!==r.voice){let R=`the runtime did not acknowledge the requested voice ${JSON.stringify(r.voice)} \u2014 the terminal response carried ${typeof b=="string"?`${Et} ${JSON.stringify(b)}`:`no ${Et} field`}; the speak-bridge companion is not deployed, and this lane never serves default-voice audio as the requested voice`;throw l?new At(R):new T(502,R)}}if(!d)throw new T(502,"the upstream produced no voice for this utterance \u2014 refusing to serve silence as success");if(k&&!l&&u<1)throw new T(502,`the upstream audio stream did not quiesce within ${o.capMs}ms of the turn's end (voiced frames still arriving) \u2014 refusing to serve audio whose tail cannot be vouched for`);let $=u>0?a.slice(0,a.length-u):a;return{pcm:Buffer.concat($),completed:x}}catch(E){throw E instanceof At||await ms(t,n,g,s.returnBoundMs),E}finally{_!==void 0&&clearTimeout(_),h!==void 0&&clearTimeout(h),I()}})}async function hs(t,e,n){let r;try{r=await t(e)}catch(o){throw We(o)}return r?!0:(en(n,new T(404,e===void 0?"no speech-capable default model is configured for this caller (catalog task tts) \u2014 name a tts model":`model "${e}" is not speech-capable (catalog task is not tts) \u2014 this lane serves only text-to-speech models; chat models ride /v1/chat/completions`)),!1)}var ys={model:!0,input:!0,voice:!0,response_format:!0,stream_format:!0,speed:!0,instructions:!0},_s="wav, pcm";async function wr(t,e,n){let r=n.maxBodyBytes??is,o=!1;e.once("close",()=>{o=!0});let s=()=>o||e.destroyed;try{if(t.method!=="POST")throw new T(405,`${String(t.method)} is not supported on ${Ke}`);let i;try{i=JSON.parse((await kt(t,r)).toString("utf8")||"{}")}catch(g){throw g instanceof z?new T(413,g.message):new T(400,"request body is not valid JSON")}if(i===null||typeof i!="object"||Array.isArray(i))throw new T(400,"request body must be a JSON object");let a=i;for(let g of Object.keys(a))if(!ys[g])throw new T(400,`request field "${g}" 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.`,g);let d=a.input;if(typeof d!="string"||d.trim().length===0)throw new T(400,"input is required and must be a non-empty string","input");if(d.length>mr)throw new T(400,`input is ${d.length} chars \u2014 over the ${mr}-char limit (one utterance; the backend bounds generation text-shaped, and an unbounded text is an unbounded synthesis)`,"input");let u=a.model===void 0?void 0:a.model;if(u!==void 0&&typeof u!="string")throw new T(400,"model must be a string","model");let c=a.voice;if(c!==void 0&&(typeof c!="string"||c.trim().length===0))throw new T(400,"voice must be a non-empty string","voice");if(typeof n.clients.speechVoices!="function")throw new T(501,`the speech lane needs a router-backed client (speechVoices) to resolve the voice set GET /v1/models publishes \u2014 refusing loudly rather than validating voices of ${Ke} against a stale copy of the list`);let l;try{l=await n.clients.speechVoices(u)}catch(g){throw We(g)}if(l.defect!==null)throw new T(500,`the voice set for this model cannot be resolved: ${l.defect} \u2014 fix the catalog row; the lane refuses to serve rather than validate against a set nobody vouched`);let m=typeof c=="string"?c:void 0;if(m!==void 0&&l.published.length>0&&!l.published.includes(m))throw new T(400,`voice ${JSON.stringify(m)} is not one of the voices this model publishes \u2014 accepted voices: ${l.published.join(", ")}`,"voice");let p=a.response_format??"wav";if(p!=="wav"&&p!=="pcm")throw new T(400,`response_format ${JSON.stringify(p)} is not supported: this lane serves the engine's raw 24 kHz mono PCM16, so the supported formats are ${_s} \u2014 and the lane will not label bytes with a format it did not encode`,"response_format");if(a.stream_format!==void 0&&typeof a.stream_format!="string")throw new T(400,"stream_format, when present, must be a string","stream_format");let y=a.speed;if(y!==void 0){if(typeof y!="number"||!Number.isFinite(y))throw new T(400,"speed, when present, must be a number","speed");if(y!==1)throw new T(400,`speed=${JSON.stringify(y)} 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 I=a.instructions;if(I!==void 0){if(typeof I!="string")throw new T(400,"instructions, when present, must be a string","instructions");if(I.trim().length>0)throw new T(400,"instructions are not supported: the backend fixes the voice style (instruct) at deploy time, so a per-request instruction would be silently ignored \u2014 the deployment speaks with its configured style","instructions")}await us(n.turn,n.clients,u);let x=Buffer.byteLength(JSON.stringify(a));if(n.stats?.request("audio_speech",x,x),!await hs(n.speechModels,u,e))return;if(typeof n.clients.openAudioOn!="function")throw new T(501,"a speech-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the same session identity as the response turn \u2014 refusing loudly rather than answering an utterance whose audio cannot be collected");if(typeof n.clients.speechSessionHandle!="function")throw new T(501,"a speech-capable model needs a router-backed client (speechSessionHandle) to resolve the speech lane's own pooled session \u2014 one no interactive binding shares, because the per-session audio bridge broadcasts every output frame to every subscriber \u2014 refusing loudly rather than collecting speech audio on a session a realtime/gemini lane can be speaking on");if(typeof n.clients.evictSpeechSession!="function")throw new T(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned speech session after a failed turn (a turn-cap timeout above all) \u2014 without it, the next request on the same model would silently collect the failed turn\u2019s still-emitting audio; refusing loudly rather than serving on an un-evictable speech slot");if(s())return;let _;try{_=await n.clients.speechSessionHandle(u)}catch(g){throw We(g)}let h;try{h=await n.clients.openAudioOn(_)}catch(g){throw We(g)}n.turn&&(n.turn.sessionId=h.sessionId);try{let g=await gs(n.clients,h,_,{model:u,text:d,...m!==void 0?{voice:m}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??os,returnBoundMs:n.turnReturnBoundMs??ss},s);if(g===null||s())return;if(ds(n.turn,g.pcm.byteLength),e.statusCode=200,m===void 0){let E=g.completed[Et];typeof E=="string"&&e.setHeader(as,E)}p==="wav"?(e.setHeader("content-type","audio/wav"),e.end(Buffer.concat([ps(g.pcm.byteLength),g.pcm]))):(e.setHeader("content-type","audio/pcm"),e.end(g.pcm))}catch(g){throw We(g)}}catch(i){if(i instanceof z){en(e,new T(413,i.message));return}if(i instanceof T){en(e,i);return}throw i}}var It="/v1/audio/transcriptions";function vr(t){return t===It}var br="/v1/audio/models",ws=16*1024*1024,vs=64*1024*1024,B=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}};function tn(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 kr(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 bs(t){if(!kr(t))return null;let e=12,n=null,r=null;for(;e+8<=t.length;){let s=String.fromCharCode(t[e],t[e+1],t[e+2],t[e+3]),i=(t[e+4]|t[e+5]<<8|t[e+6]<<16|t[e+7]<<24)>>>0,a=e+8;if(s==="fmt "&&a+16<=t.length){let d=t[a+2]|t[a+3]<<8,u=(t[a+4]|t[a+5]<<8|t[a+6]<<16|t[a+7]<<24)>>>0,c=t[a+14]|t[a+15]<<8;n={sampleRate:u,channels:d,bitsPerSample:c}}else if(s==="data"){let d=t.length-a;r=Math.min(i===0||i===4294967295?d:i,d)}e=a+i+i%2}if(n===null||r===null||r===0||n.sampleRate===0||n.channels===0||n.bitsPerSample===0)return null;let o=r/(n.sampleRate*n.channels*(n.bitsPerSample/8));return!Number.isFinite(o)||o<0?null:{seconds:o,sampleRate:n.sampleRate,channels:n.channels,bitsPerSample:n.bitsPerSample}}async function ks(t,e,n){if(t){if(t.model=Z({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 Rs(t,e){t&&(t.outputUnits=e.seconds)}async function xs(t,e,n,r,o){let s=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1,...r!==void 0?{language:r}:{}},o),i=[],a=null;for await(let d of s){if(i.push(d),d.type==="error"){let u=d.error??{},c=typeof u.type=="string"?u.type:"upstream_error",l=String(u.message??"upstream reported an error");throw new B(c==="invalid_request_error"?400:502,`transcription upstream error: ${l}`)}if(d.type==="response.completed"){a=d.response??null;break}}if(a===null)throw new B(502,"transcription upstream produced no terminal response \u2014 refusing to fabricate a transcript");return Ce(i,a)}function Rr(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 Ss(t,e,n){let r;try{r=await t(e)}catch(o){throw Rr(o)}return r?!0:(tn(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 Es=new Set(["file","model","language","response_format"]);async function xr(t,e,n){let r=n.maxBodyBytes??vs,o=n.maxAudioPartBytes??ws;try{if(t.method!=="POST")throw new B(405,`${String(t.method)} is not supported on ${It}`);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 Rt(t,r)}catch(_){throw _ instanceof z?new B(413,_.message):new B(400,String(_ instanceof Error?_.message:_))}let a=new Map;for(let _ of i.keys()){if(!Es.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[_,h]of a)if(h>1)throw new B(400,`form field "${_}" appears ${h} 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 d=i.get("model");if(d!==null&&typeof d!="string")throw new B(400,"model must be a string field","model");let u=d===null?void 0:d,c=i.get("language");if(c!==null&&(typeof c!="string"||c.trim().length===0))throw new B(400,"language, when present, must be a non-empty string","language");let l=c===null?void 0:c,m=i.get("response_format")??"json";if(m!=="json")throw new B(400,`response_format ${JSON.stringify(m)} is not supported: timestamps/segments are not produced on this lane yet \u2014 the runtime answers terminal transcript text only, so json is the only output format`,"response_format");let p=i.get("file");if(!(p instanceof File))throw new B(400,"file must be an uploaded WAV: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths","file");if(p.size>o)throw new B(413,`file part exceeds the ${o}-byte limit (the serve protocol's MAX_AUDIO_BYTES)`,"file");let y=new Uint8Array(await p.arrayBuffer());if(!kr(y))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 I=bs(y);if(I===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 ks(n.turn,n.clients,u),n.stats?.request("audio_transcriptions",y.length,y.length),!await Ss(n.transcriptionModels,u,e))return;let x=[{type:"input_audio",input_audio:{data:Buffer.from(y).toString("base64"),format:"wav"}}];try{let _=await xs(n.clients,u,x,l,n.turn?re(n.turn):void 0);Rs(n.turn,I),e.statusCode=200,e.setHeader("content-type","application/json"),e.end(JSON.stringify({text:_}))}catch(_){throw Rr(_)}}catch(s){if(s instanceof z){tn(e,new B(413,s.message));return}if(s instanceof B){tn(e,s);return}throw s}}var Er="/v1/usage/requests",Sr=1e4,As=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,ye=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},Le=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}};var Ar=1024*1024;function nn(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new ye(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new ye(`"${e}" must be an array of uuid strings`);if(n.length>Sr)throw new ye(`"${e}" carries ${n.length} ids; at most ${Sr} may be looked up in one request. Split the batch \u2014 the ids past the limit are NOT silently dropped`);let r=[],o=new Set;for(let s of n){if(typeof s!="string"||!As.test(s))throw new ye(`"${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 Ir(t){return nn(t,"inference_ids")}function Ot(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 Le(`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 Le(`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 Mt=require("@urun-sh/core");var rn="/partners/huggingface/v1/billing/requests";function Is(t){return nn(t,"requestIds")}function Os(t){return t>=200&&t<=399}function Ts(t){let e=[];for(let n of t)n.cost_nano_usd!==null&&Os(n.http_status)&&e.push({requestId:n.inference_id,costNanoUsd:n.cost_nano_usd});return{requests:e.length>0?e:null}}async function Or(t,e){let n=Is(t),r=await e(n);return Ot(r,n),Ts(r)}var Ps={"/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 Cs(t){return Ps[t]??null}var $r=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function $s(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function Ms(t){return t==="surface"}function Ls(t){let e=t?.ledgerFailureReason;return $r.includes(e)?e:"unknown"}var Ns=["no_response_head","no_org","model_unresolved","no_model_named","queue_full"],Tr=["priced","unpriced"],Us={written:Tr,already_written:Tr,skipped:Ns,failed:$r},xu=Object.entries(Us).flatMap(([t,e])=>e.map(n=>({outcome:t,reason:n})));function Ds(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function Fs(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 Bs=256,Pr=4096,Ye=12e4,qs=250,js=8e3,Xe=[],Cr=new Set,Qe=0,ze=!1;var Hs=new Set;function Vs(){return Qe===0&&Xe.length===0}function Gs(){if(Vs()){ze=!1;for(let t of[...Hs])t()}}function Mr(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
|
|
20
|
+
`}function Js(t){xe(process.stdout,t)}function Lr(t,e){try{t(e)}catch{}}function Nr(t){let{record:e,ledger:n}=t;if(n===null)return;let r=n.report??Js,o=Cs(e.path);if(o===null)return;let s=c=>{try{n.observe(c)}catch{}},i=(c,l)=>{s({outcome:"skipped",reason:c}),Lr(r,Mr("inference_ledger_skipped",{inference_id:e.inference_id,path:e.path,reason:c,detail:l}))};if(e.status===null){i("no_response_head","no head flushed, so the caller never received an Inference-Id and no status can be recorded honestly");return}if(e.org===null){i("no_org","the request never resolved a tenant, so there is no org to attribute the row to");return}let a=e.catalog;if(a.kind==="unresolved"){i("model_unresolved",a.reason);return}let d;if(a.kind==="catalog")d={model_id:a.model_id,identity_kind:"catalog_model",variant:a.variant};else{if(a.model===null){i("no_model_named","the request resolved to a caller-org app but named no model, so there is no app slug to record and `model_id` cannot be written honestly");return}d={model_id:a.model,identity_kind:"caller_org_app",variant:null}}let u={inference_id:e.inference_id,...d,task:o,http_status:e.status,started_at:Fs(e),completed_at:e.ts,billable_outcome:Ds(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(Xe.length>=Pr){i("queue_full",`${Xe.length} ledger writes are already waiting (capacity ${Pr}, ${Qe} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}Xe.push({entry:u,req:t.req,record:e,ledger:n,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),Qe<Bs&&(Qe+=1,Ws())}async function Ws(){try{for(;;){let t=Ks();if(t===void 0)return;Cr.add(t);try{await zs(t)}finally{Cr.delete(t)}}}finally{Qe-=1,Gs()}}function Ks(){let t=Date.now();for(;;){let e=Xe.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<Ye)return e;Ne(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 ${Ye}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 zs(t){let{entry:e,record:n,ledger:r}=t;for(;;){t.attempts+=1;try{let o=await r.write(t.req,e);Ne(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=$s(o);if(s!==null){Ne(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=Ls(o),a=Date.now()-t.queuedAt;if(ze){Ne(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(Ms(i)&&a<Ye){if(await Xs(Ys(t.attempts)),ze||Date.now()-t.queuedAt>=Ye){let d=Date.now()-t.queuedAt;Ne(t,{outcome:"failed",reason:ze?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:ze?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:d,error:`this row's backoff outlived the ${Ye}ms dwell bound (waited ${d}ms over ${t.attempts} attempt(s)) and was given up on rather than starting an attempt that could only finish past it. Last failure: ${o instanceof Error?o.message:String(o)}`});return}continue}Ne(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 Ys(t){return Math.min(js,qs*2**Math.max(0,t-1))}function Xs(t){return new Promise(e=>{setTimeout(e,t).unref?.()})}function Ne(t,e,n){if(t.settled)return;t.settled=!0;try{t.ledger.observe(e)}catch{}let{event:r,...o}=n;Lr(t.report,Mr(r,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...o}))}var sn="x-urun-profile",Ze="voice";var te=class extends Error{status=400},Qs=/(?:[.!?:;]|\n)[)\]"'\u2019\u201d\u00bb\u300d\u300b\u3011\uff09\uff3d]?\s*$/,Zs=/\s/;function ei(t){return Qs.test(t)}function on(t,e,n){let r=t[e];if(r===void 0)return n;if(typeof r!="boolean")throw new te(`stream_options.voice.${e} must be a boolean when present (got ${typeof r})`);return r}function Ur(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 te(`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 te(`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 te(`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 Dr(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 te(`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 u=n.trim();if(u==="")throw new te(`the ${sn} header is empty \u2014 the only profile this lane accepts is "${Ze}" (omit the header for the ordinary lane)`);if(u!==Ze)throw new te(`unknown profile "${u}" \u2014 the only profile this lane accepts is "${Ze}"`);i=!0}if(s!==void 0){if(typeof s!="string"||s!==Ze)throw new te(`unknown profile ${JSON.stringify(s)} \u2014 the only profile this lane accepts is "${Ze}"`);i=!0}if(!i)return null;if(e.stream!==!0)throw new te("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 te("stream_options.voice must be an object when present (flush_max_chars, flush_max_ms, sentence_flush, include_reasoning, metadata)");let d=a??{};return{flushMaxChars:Ur(d,"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:Ur(d,"flush_max_ms",250,{max:2147483647}),sentenceFlush:on(d,"sentence_flush",!0),includeReasoning:on(d,"include_reasoning",!1),metadata:on(d,"metadata",!1)}}var Tt=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&&ei(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(Zs.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)}};var ti=Ut(require("crypto"),1);var ni=6e4,Ou=ni/1e3+5;var Fr=15;var Tu=Fr*1e3,Pu=Fr*500;var et=class extends Error{constructor(n,r){super(`runtime answered ${n}`);this.status=n;this.body=r}status;body},tt=class extends Error{};function dn(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function ri(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 G(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function J(t,e,n,r="invalid_request_error",o){G(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function ln(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);G(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function ae(t,e,n,r){G(t,e,{type:"error",error:{type:n,message:r}})}function ue(t,e,n){if(e==="anthropic"){G(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}G(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var oi=64*1024*1024,nt=class extends Error{};async function Br(t,e=oi){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 Ee(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 Hr(t){let e=t?.usage;if(!e||typeof e!="object")return{kind:"absent"};let n=e,r=d=>typeof d=="number"&&Number.isFinite(d)&&d>=0?d:null,o=r(n.input_tokens),s=r(n.output_tokens),i=d=>d??0,a=r(n.total_tokens)??i(o)+i(s);if(i(o)>0||i(s)>0){let d={input_tokens:i(o),output_tokens:i(s),total_tokens:a};return o===null||s===null?{kind:"partial",usage:d}:{kind:"counts",usage:d}}return o===null||s===null?{kind:"absent"}:{kind:"zero",usage:{input_tokens:0,output_tokens:0,total_tokens:a}}}function Pt(t){let e=Hr(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function qr(t){let e=Pt(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 si(t,e){let n=Hr(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function ii(t){let e=t?.urun_timing;if(!e||typeof e!="object")return null;let n=e,r=d=>{let u=n[d];return typeof u=="number"&&Number.isSafeInteger(u)&&u>=0?u:null},o=r("queue_ms"),s=r("prefill_ms"),i=r("decode_ms"),a=r("total_ms");return o===null||s===null||i===null||a===null?null:{queue_ms:o,prefill_ms:s,decode_ms:i,total_ms:a}}function ai(t,e){let n=ii(e);n&&(t.timing=n)}function pe(t,e){si(t,e),ai(t,e)}async function Lt(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 Vr(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 ui(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 Ct=class extends Error{};function di(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 Ct(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function li(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:di(n.role,n.content)})}return e}function an(t){return Te(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 ci(t,e,n,r,o,s){let i=Fe(s.id),a=t.stream===!0;if(s.model=Z(t),s.stream=a,await Lt(s,e,typeof t.model=="string"?t.model:void 0),a){let _;try{_=await qe({body:t,clients:e,store:n,stats:o,responseId:i,turn:s})}catch(h){if(h instanceof N){ue(r,"openai",h);return}if(h instanceof oe){J(r,h.status,h.message);return}throw h}dn(r);for await(let h of _.events)h.type==="error"&&(s.streamError=!0),h.type==="response.completed"&&pe(s,h.response),r.write(`event: ${String(h.type??"message")}
|
|
21
21
|
data: ${JSON.stringify(h)}
|
|
22
22
|
|
|
23
23
|
`);r.write(`data: [DONE]
|
|
@@ -49,7 +49,7 @@ data: ${JSON.stringify(h)}
|
|
|
49
49
|
`),n.end();return}finally{D?.flush(),D?.dispose()}}let y="",I="",x=[],_=new ge;for await(let b of r.track(p,l,o)){x.push(b),b.type==="response.output_text.delta"&&typeof b.delta=="string"&&(y+=b.delta),b.type==="response.reasoning_text.delta"&&typeof b.delta=="string"&&(I+=b.delta);let R=Pe(b);if(R&&_.add(R),b.type==="error"){ln(n,b);return}}let h=ie(x);pe(o,h);let g=an(h),E=g.length>0?g:an({output:_.items()}),f=Re(m,E.length,y);if(f){J(n,502,ke(l,f),"upstream_error");return}let v=E.map(({index:b,...R})=>R),k={role:"assistant",content:y||null},$=I||Vr(h);$&&(k.reasoning_content=$),v.length>0&&(k.tool_calls=v),G(n,200,{id:u,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:d,choices:[{index:0,message:k,finish_reason:v.length>0?"tool_calls":"stop"}],usage:qr(h)})}var fi="reflex-latest";async function mi(t,e,n,r,o){if(t===null||typeof t!="object"||Array.isArray(t)){J(n,400,"systemone requires a JSON object body");return}delete t.key;let s=typeof t.model=="string"&&t.model?t.model:fi,i=Z({model:s});if(o.model=i,o.stream=!1,t.state===void 0||t.state===null){J(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){J(n,400,"systemone requires a questions object (question id -> {kind, options, ...})");return}if(t.media!==void 0&&!Array.isArray(t.media)){J(n,400,"systemone media, when present, must be an array");return}if(r.model(i),await Lt(o,e,s),e.stateless?.enabled){let p;try{p=await e.stateless.targetFor(s)}catch(y){if(y instanceof N){ue(n,"openai",y);return}throw y}if(p!==null){try{let y=await e.stateless.decide(t,s);o.catalog=Dt(y.target),pe(o,y.completed),G(n,200,y.body)}catch(y){if(y instanceof N){ue(n,"openai",y);return}if(y instanceof et){G(n,y.status,y.body);return}if(y instanceof tt){J(n,503,y.message,"upstream_error");return}throw y}return}}let d;try{d=await e.createResponse({model:s,input:[{role:"user",content:JSON.stringify(t)}],stream:!0},re(o))}catch(p){if(p instanceof N){ue(n,"openai",p);return}throw p}let u=[];try{for await(let p of r.track(d,i,o))if(u.push(p),p.type==="error"){ln(n,p);return}}catch(p){if(p instanceof N){ue(n,"openai",p);return}throw p}let c=ie(u);if(c==null){J(n,502,"upstream produced no response.completed event","upstream_error");return}pe(o,c);let l=Ce(u,c),m;try{m=JSON.parse(l)}catch{J(n,502,"systemone answer was not valid JSON","upstream_error");return}if(m===null||typeof m!="object"||Array.isArray(m)){J(n,502,"systemone answer was not a JSON object","upstream_error");return}G(n,200,m)}function gi(t){return typeof t=="string"?t:Array.isArray(t)?t.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var rt=class extends Error{};function $t(t){return new rt(`unsupported anthropic content block type "${t}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function hi(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 yi(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 rt(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function _i(t){if(typeof t=="string")return t;if(t==null)return"";if(!Array.isArray(t))throw $t(typeof t);let e="";for(let n of t){let r=String(n.type??"");if(r!=="text")throw $t(`tool_result > ${r}`);e+=String(n.text??"")}return e}function wi(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 $t(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:_i(i.content)});else{if(a==="thinking"||a==="redacted_thinking")continue;throw $t(a)}}return s(),r}function un(t){return Te(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 vi(t,e,n,r,o){let s=t.messages;if(!Array.isArray(s)){ae(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){ae(n,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(t.top_k!==void 0){ae(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(k=>typeof k!="string"))){ae(n,400,"invalid_request_error","stop_sequences must be an array of strings");return}let i=String(t.model??"urun"),a=Fn(o.id),d=[],u;try{t.system&&d.push({role:"system",content:gi(t.system)});for(let k of s)d.push(...wi(k));u=yi(t.tool_choice)}catch(k){if(k instanceof rt){ae(n,400,"invalid_request_error",k.message);return}throw k}let c=Z(t);r.model(c),o.model=c,o.stream=t.stream===!0,await Lt(o,e,typeof t.model=="string"?t.model:void 0);let l=Array.isArray(t.tools)&&t.tools.length>0,m;try{m=await e.createResponse({model:t.model,input:d,stream:!0,tools:hi(t.tools),tool_choice:u,temperature:t.temperature,max_output_tokens:t.max_tokens,top_p:t.top_p,stop:t.stop_sequences},re(o))}catch(k){if(k instanceof N){ue(n,"anthropic",k);return}throw k}let p=(k,$)=>{n.destroyed||n.write(`event: ${k}
|
|
50
50
|
data: ${JSON.stringify({type:k,...$})}
|
|
51
51
|
|
|
52
|
-
`)};if(t.stream===!0){let k=r.track(m,c,o);dn(n),p("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:i,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let $=0,b="",R=0,P="none",D=()=>P,K=-1,Q=-1,se=new ge,fe=[],j=()=>{P!=="none"&&(P="none",p("content_block_stop",{index:K}))},H=(F,X)=>{j(),P=F,K=R++,p("content_block_start",{index:K,content_block:X})};for await(let F of k){if(n.destroyed)break;fe.push(F);let X=Pe(F);if(F.type==="response.reasoning_text.delta"&&typeof F.delta=="string")D()!=="thinking"&&H("thinking",{type:"thinking",thinking:""}),p("content_block_delta",{index:K,delta:{type:"thinking_delta",thinking:F.delta}});else if(F.type==="response.output_text.delta"&&typeof F.delta=="string")D()!=="text"&&H("text",{type:"text",text:""}),$+=1,b+=F.delta,p("content_block_delta",{index:K,delta:{type:"text_delta",text:F.delta}});else if(X){let{call:me}=se.add(X);(D()!=="tool"||X.tool_index!==Q)&&(Q=X.tool_index,H("tool",{type:"tool_use",id:me.id??`call_${X.tool_index}`,name:me.name??"",input:{}})),X.delta&&p("content_block_delta",{index:K,delta:{type:"input_json_delta",partial_json:X.delta}})}else if(F.type==="error"){o.streamError=!0,p("error",{error:{type:"api_error",message:JSON.stringify(F)}}),n.end();return}}if(n.destroyed)return;let Y=ie(fe),_e=Te(Y),O=Re(l,se.size+_e.length,b);if(O){o.streamError=!0,p("error",{error:{type:"api_error",message:ke(c,O)}}),n.end();return}j();let M=[];if(se.size===0)try{M=un(Y)}catch(F){o.streamError=!0,p("error",{error:{type:"api_error",message:String(F instanceof Error?F.message:F)}}),n.end();return}R===0&&M.length===0&&(H("text",{type:"text",text:""}),j()),M.forEach(F=>{let X=R++;p("content_block_start",{index:X,content_block:{...F,input:{}}}),p("content_block_delta",{index:X,delta:{type:"input_json_delta",partial_json:JSON.stringify(F.input??{})}}),p("content_block_stop",{index:X})});let W=se.size>0||M.length>0,de=Pt(Y);pe(o,Y),p("message_delta",{delta:{stop_reason:W?"tool_use":"end_turn"},usage:de?{input_tokens:de.input_tokens,output_tokens:de.output_tokens}:{output_tokens:$}}),p("message_stop",{}),n.end();return}let y="",I="",x=[],_=new ge;for await(let k of r.track(m,c,o)){x.push(k),k.type==="response.output_text.delta"&&typeof k.delta=="string"&&(y+=k.delta),k.type==="response.reasoning_text.delta"&&typeof k.delta=="string"&&(I+=k.delta);let $=Pe(k);if($&&_.add($),k.type==="error"){let b=k.error,R=b?.type==="invalid_request_error";ae(n,R?400:502,R?"invalid_request_error":"api_error",b?.message??JSON.stringify(k));return}if(n.destroyed)break}if(n.destroyed)return;let h=ie(x);pe(o,h);let g;try{g=un(h),g.length===0&&_.size>0&&(g=un({output:_.items()}))}catch(k){ae(n,502,"api_error",String(k instanceof Error?k.message:k));return}let E=Re(l,g.length,y);if(E){ae(n,502,"api_error",ke(c,E));return}let f=[],v=I||Vr(h);v&&f.push({type:"thinking",thinking:v}),(y||g.length===0)&&f.push({type:"text",text:y}),f.push(...g),G(n,200,{id:a,type:"message",role:"assistant",content:f,model:i,stop_reason:g.length>0?"tool_use":"end_turn",usage:Pt(h)??{input_tokens:0,output_tokens:0}})}function bi(t){xe(process.stdout,t)}function ki(t){let{clients:e,apiKey:n,identity:r,statusOf:o}=t,s=typeof e=="function"?e:()=>e,i=t.requestLog??bi,a=t.observe,d=new ve;return(u,c)=>{let l=new URL(u.url??"/","http://localhost"),m=l.pathname==="/v1/messages"||l.pathname.startsWith("/v1/messages/"),p=(h,g,E,f,v)=>m?ae(c,h,f,g):J(c,h,g,E,v),y=Ft(l.pathname),I=l.pathname.startsWith("/partners/"),x=y||I?{id:De(),startedAt:performance.now(),streamError:!1,model:null,stream:null,org:null,ttftMs:null,catalog:{kind:"unresolved",reason:"no lane resolved a model for this request"},promptTokens:null,completionTokens:null,outputUnits:null,sessionId:null,timing:null}:null;x&&(c.setHeader(Nn,x.id),c.once("close",()=>{let h=Bn({turn:x,method:u.method??"",path:l.pathname,res:c});i(qn(h)),a?.(h),Nr({record:h,ledger:t.ledger??null,req:u})}));let _=()=>Un(l.pathname,x);(async()=>{if(u.method==="GET"&&l.pathname==="/healthz"){G(c,200,{ok:!0});return}if(u.method==="GET"&&l.pathname==="/stats"){G(c,200,r?{identity:r,...d.snapshot()}:d.snapshot());return}if(n&&(u.headers.authorization??"")!==`Bearer ${n}`){p(401,"invalid local proxy api key","authentication_error","authentication_error");return}let h=l.pathname===Er?"canonical":l.pathname===rn?"huggingface":null;if(h){if(u.method!=="POST"){p(405,`${l.pathname} is a POST batch lookup`,"invalid_request_error","invalid_request_error");return}let v=t.usage;if(!v){p(501,`${l.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 k=v.authorize(u),$;try{$=await Br(u,Ar)}catch(P){if(P instanceof nt){p(413,P.message,"request_too_large","request_too_large");return}p(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let b=P=>v.lookup(k,P),R;try{if(h==="canonical"){let P=Ir($),D=await b(P);Ot(D,P),R={requests:D}}else R=await Or($,b)}catch(P){if(P instanceof ye){p(P.status,P.message,"invalid_request_error","invalid_request_error");return}throw P}G(c,200,R);return}if(l.pathname.startsWith("/partners/")){p(404,`no partner adapter for ${u.method} ${l.pathname} \u2014 the partner surface currently carries ${rn}`,"invalid_request_error","not_found_error");return}let g=await s(u);if(x&&(x.org=g.tenant??null),u.method==="GET"&&l.pathname==="/v1/models"){if(l.searchParams.get("format")==="openrouter"){if(!g.openRouterModels){p(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}G(c,200,await g.openRouterModels());return}G(c,200,await g.listModels());return}if(u.method==="GET"&&l.pathname===rr){if(!g.listImageModels){p(501,"GET /v1/images/models needs a router-backed client (listImageModels)","not_implemented","not_implemented_error");return}G(c,200,await g.listImageModels());return}if(nr(l.pathname)){if(!g.supportsImages){p(501,`the image lanes need a router-backed client (supportsImages) to gate model capability for ${l.pathname}`,"not_implemented","not_implemented_error");return}let v={clients:g,imageModels:g.supportsImages,stats:d,turn:_()};l.pathname===xt?await fr(u,c,v):await pr(u,c,v);return}if(u.method==="GET"&&l.pathname===br){if(!g.listSpeechToTextModels){p(501,"GET /v1/audio/models needs a router-backed client (listSpeechToTextModels)","not_implemented","not_implemented_error");return}G(c,200,await g.listSpeechToTextModels());return}if(vr(l.pathname)){if(!g.supportsTranscription){p(501,`the transcription lane needs a router-backed client (supportsTranscription) to gate model capability for ${It}`,"not_implemented","not_implemented_error");return}let v={clients:g,transcriptionModels:g.supportsTranscription,stats:d,turn:_()};await xr(u,c,v);return}if(_r(l.pathname)){if(!g.supportsSpeech){p(501,`the speech lane needs a router-backed client (supportsSpeech) to gate model capability for ${Ke}`,"not_implemented","not_implemented_error");return}let v={clients:g,speechModels:g.supportsSpeech,stats:d,turn:_()};await wr(u,c,v);return}if(u.method!=="POST"){p(404,`no route for ${u.method} ${l.pathname}`,"invalid_request_error","not_found_error");return}let E;try{E=await Br(u)}catch(v){if(v instanceof nt){p(413,v.message,"request_too_large","request_too_large");return}p(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let f=Buffer.byteLength(JSON.stringify(E));if(l.pathname==="/v1/responses"){d.request("responses",f,f),await ci(E,g,Be(g),c,d,_());return}if(l.pathname==="/v1/chat/completions"){d.request("chat_completions",f,f),await pi(E,g,c,d,_(),u.headers[sn]);return}if(l.pathname==="/v1/systemone"){d.request("systemone",f,f),await mi(E,g,c,d,_());return}if(l.pathname==="/v1/messages"){d.request("messages",f,f),await vi(E,g,c,d,_());return}if(l.pathname==="/v1/messages/count_tokens"){d.request("count_tokens",f,f),ae(c,404,"not_found_error","count_tokens is not implemented by the uRun proxy: the serve transport exposes no token-count lane, and the proxy will not fabricate counts");return}p(404,`no route for POST ${l.pathname}`,"invalid_request_error","not_found_error")})().catch(h=>{let g=String(h instanceof Error?h.message:h);if(!c.headersSent){let E=o?.(h)??null;if(E){p(E.status,g,E.openaiType,E.anthropicType);return}if(h instanceof Mt.SessionAllocationError){let f=gt(h.code);if(f){let v=h.retryAfterSeconds??(f.status===429?30:void 0);v!==void 0&&c.setHeader("Retry-After",String(v)),p(f.status,Gt(f,h.code??void 0),f.openaiType,f.anthropicType,f.code);return}}if(h instanceof Mt.SessionFailedError){let f=ht(h);if(f){let v=f.status===429?30:void 0;v!==void 0&&c.setHeader("Retry-After",String(v)),p(f.status,Gt(f,h.status.sessionId?`session ${h.status.sessionId}`:void 0),f.openaiType,f.anthropicType,f.code);return}}if(h instanceof Le){p(502,g,"upstream_error","api_error");return}p(500,g,"proxy_error","api_error","proxy_error");return}x&&(x.streamError=!0),c.destroyed||(m?c.write(`event: error
|
|
52
|
+
`)};if(t.stream===!0){let k=r.track(m,c,o);dn(n),p("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:i,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let $=0,b="",R=0,P="none",D=()=>P,K=-1,Q=-1,se=new ge,fe=[],j=()=>{P!=="none"&&(P="none",p("content_block_stop",{index:K}))},H=(F,X)=>{j(),P=F,K=R++,p("content_block_start",{index:K,content_block:X})};for await(let F of k){if(n.destroyed)break;fe.push(F);let X=Pe(F);if(F.type==="response.reasoning_text.delta"&&typeof F.delta=="string")D()!=="thinking"&&H("thinking",{type:"thinking",thinking:""}),p("content_block_delta",{index:K,delta:{type:"thinking_delta",thinking:F.delta}});else if(F.type==="response.output_text.delta"&&typeof F.delta=="string")D()!=="text"&&H("text",{type:"text",text:""}),$+=1,b+=F.delta,p("content_block_delta",{index:K,delta:{type:"text_delta",text:F.delta}});else if(X){let{call:me}=se.add(X);(D()!=="tool"||X.tool_index!==Q)&&(Q=X.tool_index,H("tool",{type:"tool_use",id:me.id??`call_${X.tool_index}`,name:me.name??"",input:{}})),X.delta&&p("content_block_delta",{index:K,delta:{type:"input_json_delta",partial_json:X.delta}})}else if(F.type==="error"){o.streamError=!0,p("error",{error:{type:"api_error",message:JSON.stringify(F)}}),n.end();return}}if(n.destroyed)return;let Y=ie(fe),_e=Te(Y),O=Re(l,se.size+_e.length,b);if(O){o.streamError=!0,p("error",{error:{type:"api_error",message:ke(c,O)}}),n.end();return}j();let M=[];if(se.size===0)try{M=un(Y)}catch(F){o.streamError=!0,p("error",{error:{type:"api_error",message:String(F instanceof Error?F.message:F)}}),n.end();return}R===0&&M.length===0&&(H("text",{type:"text",text:""}),j()),M.forEach(F=>{let X=R++;p("content_block_start",{index:X,content_block:{...F,input:{}}}),p("content_block_delta",{index:X,delta:{type:"input_json_delta",partial_json:JSON.stringify(F.input??{})}}),p("content_block_stop",{index:X})});let W=se.size>0||M.length>0,de=Pt(Y);pe(o,Y),p("message_delta",{delta:{stop_reason:W?"tool_use":"end_turn"},usage:de?{input_tokens:de.input_tokens,output_tokens:de.output_tokens}:{output_tokens:$}}),p("message_stop",{}),n.end();return}let y="",I="",x=[],_=new ge;for await(let k of r.track(m,c,o)){x.push(k),k.type==="response.output_text.delta"&&typeof k.delta=="string"&&(y+=k.delta),k.type==="response.reasoning_text.delta"&&typeof k.delta=="string"&&(I+=k.delta);let $=Pe(k);if($&&_.add($),k.type==="error"){let b=k.error,R=b?.type==="invalid_request_error";ae(n,R?400:502,R?"invalid_request_error":"api_error",b?.message??JSON.stringify(k));return}if(n.destroyed)break}if(n.destroyed)return;let h=ie(x);pe(o,h);let g;try{g=un(h),g.length===0&&_.size>0&&(g=un({output:_.items()}))}catch(k){ae(n,502,"api_error",String(k instanceof Error?k.message:k));return}let E=Re(l,g.length,y);if(E){ae(n,502,"api_error",ke(c,E));return}let f=[],v=I||Vr(h);v&&f.push({type:"thinking",thinking:v}),(y||g.length===0)&&f.push({type:"text",text:y}),f.push(...g),G(n,200,{id:a,type:"message",role:"assistant",content:f,model:i,stop_reason:g.length>0?"tool_use":"end_turn",usage:Pt(h)??{input_tokens:0,output_tokens:0}})}function bi(t){xe(process.stdout,t)}function ki(t){let{clients:e,apiKey:n,identity:r,statusOf:o}=t,s=typeof e=="function"?e:()=>e,i=t.requestLog??bi,a=t.observe,d=new ve;return(u,c)=>{let l=new URL(u.url??"/","http://localhost"),m=l.pathname==="/v1/messages"||l.pathname.startsWith("/v1/messages/"),p=(h,g,E,f,v)=>m?ae(c,h,f,g):J(c,h,g,E,v),y=Ft(l.pathname),I=l.pathname.startsWith("/partners/"),x=y||I?{id:De(),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;x&&(c.setHeader(Nn,x.id),c.once("close",()=>{let h=Bn({turn:x,method:u.method??"",path:l.pathname,res:c});i(qn(h)),a?.(h),Nr({record:h,ledger:t.ledger??null,req:u})}));let _=()=>Un(l.pathname,x);(async()=>{if(u.method==="GET"&&l.pathname==="/healthz"){G(c,200,{ok:!0});return}if(u.method==="GET"&&l.pathname==="/stats"){G(c,200,r?{identity:r,...d.snapshot()}:d.snapshot());return}if(n&&(u.headers.authorization??"")!==`Bearer ${n}`){p(401,"invalid local proxy api key","authentication_error","authentication_error");return}let h=l.pathname===Er?"canonical":l.pathname===rn?"huggingface":null;if(h){if(u.method!=="POST"){p(405,`${l.pathname} is a POST batch lookup`,"invalid_request_error","invalid_request_error");return}let v=t.usage;if(!v){p(501,`${l.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 k=v.authorize(u),$;try{$=await Br(u,Ar)}catch(P){if(P instanceof nt){p(413,P.message,"request_too_large","request_too_large");return}p(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let b=P=>v.lookup(k,P),R;try{if(h==="canonical"){let P=Ir($),D=await b(P);Ot(D,P),R={requests:D}}else R=await Or($,b)}catch(P){if(P instanceof ye){p(P.status,P.message,"invalid_request_error","invalid_request_error");return}throw P}G(c,200,R);return}if(l.pathname.startsWith("/partners/")){p(404,`no partner adapter for ${u.method} ${l.pathname} \u2014 the partner surface currently carries ${rn}`,"invalid_request_error","not_found_error");return}let g=await s(u);if(x&&(x.org=g.tenant??null),u.method==="GET"&&l.pathname==="/v1/models"){if(l.searchParams.get("format")==="openrouter"){if(!g.openRouterModels){p(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}G(c,200,await g.openRouterModels());return}G(c,200,await g.listModels());return}if(u.method==="GET"&&l.pathname===rr){if(!g.listImageModels){p(501,"GET /v1/images/models needs a router-backed client (listImageModels)","not_implemented","not_implemented_error");return}G(c,200,await g.listImageModels());return}if(nr(l.pathname)){if(!g.supportsImages){p(501,`the image lanes need a router-backed client (supportsImages) to gate model capability for ${l.pathname}`,"not_implemented","not_implemented_error");return}let v={clients:g,imageModels:g.supportsImages,stats:d,turn:_()};l.pathname===xt?await fr(u,c,v):await pr(u,c,v);return}if(u.method==="GET"&&l.pathname===br){if(!g.listSpeechToTextModels){p(501,"GET /v1/audio/models needs a router-backed client (listSpeechToTextModels)","not_implemented","not_implemented_error");return}G(c,200,await g.listSpeechToTextModels());return}if(vr(l.pathname)){if(!g.supportsTranscription){p(501,`the transcription lane needs a router-backed client (supportsTranscription) to gate model capability for ${It}`,"not_implemented","not_implemented_error");return}let v={clients:g,transcriptionModels:g.supportsTranscription,stats:d,turn:_()};await xr(u,c,v);return}if(_r(l.pathname)){if(!g.supportsSpeech){p(501,`the speech lane needs a router-backed client (supportsSpeech) to gate model capability for ${Ke}`,"not_implemented","not_implemented_error");return}let v={clients:g,speechModels:g.supportsSpeech,stats:d,turn:_()};await wr(u,c,v);return}if(u.method!=="POST"){p(404,`no route for ${u.method} ${l.pathname}`,"invalid_request_error","not_found_error");return}let E;try{E=await Br(u)}catch(v){if(v instanceof nt){p(413,v.message,"request_too_large","request_too_large");return}p(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let f=Buffer.byteLength(JSON.stringify(E));if(l.pathname==="/v1/responses"){d.request("responses",f,f),await ci(E,g,Be(g),c,d,_());return}if(l.pathname==="/v1/chat/completions"){d.request("chat_completions",f,f),await pi(E,g,c,d,_(),u.headers[sn]);return}if(l.pathname==="/v1/systemone"){d.request("systemone",f,f),await mi(E,g,c,d,_());return}if(l.pathname==="/v1/messages"){d.request("messages",f,f),await vi(E,g,c,d,_());return}if(l.pathname==="/v1/messages/count_tokens"){d.request("count_tokens",f,f),ae(c,404,"not_found_error","count_tokens is not implemented by the uRun proxy: the serve transport exposes no token-count lane, and the proxy will not fabricate counts");return}p(404,`no route for POST ${l.pathname}`,"invalid_request_error","not_found_error")})().catch(h=>{let g=String(h instanceof Error?h.message:h);if(!c.headersSent){let E=o?.(h)??null;if(E){p(E.status,g,E.openaiType,E.anthropicType);return}if(h instanceof Mt.SessionAllocationError){let f=gt(h.code);if(f){let v=h.retryAfterSeconds??(f.status===429?30:void 0);v!==void 0&&c.setHeader("Retry-After",String(v)),p(f.status,Gt(f,h.code??void 0),f.openaiType,f.anthropicType,f.code);return}}if(h instanceof Mt.SessionFailedError){let f=ht(h);if(f){let v=f.status===429?30:void 0;v!==void 0&&c.setHeader("Retry-After",String(v)),p(f.status,Gt(f,h.status.sessionId?`session ${h.status.sessionId}`:void 0),f.openaiType,f.anthropicType,f.code);return}}if(h instanceof Le){p(502,g,"upstream_error","api_error");return}p(500,g,"proxy_error","api_error","proxy_error");return}x&&(x.streamError=!0),c.destroyed||(m?c.write(`event: error
|
|
53
53
|
data: ${JSON.stringify({type:"error",error:{type:"api_error",message:g}})}
|
|
54
54
|
|
|
55
55
|
`):c.write(`data: ${JSON.stringify({error:{message:g,type:"upstream_error"}})}
|
package/dist/proxy/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Server, IncomingMessage } from 'node:http';
|
|
2
|
-
import { f as ProxyHandlerOptions, P as ProxyClients } from '../responses-turn-
|
|
3
|
-
export { D as DialObserver, a as DialReport, g as ProxyIdentity, h as ProxyVideoOutLane, S as SessionGoneError, U as UnknownModelError } from '../responses-turn-
|
|
2
|
+
import { f as ProxyHandlerOptions, P as ProxyClients } from '../responses-turn-Dd-h46sQ.cjs';
|
|
3
|
+
export { D as DialObserver, a as DialReport, g as ProxyIdentity, h as ProxyVideoOutLane, S as SessionGoneError, U as UnknownModelError } from '../responses-turn-Dd-h46sQ.cjs';
|
|
4
4
|
export { h as GEMINI_LIVE_PATH, V as VIDEO_OUT_SETUP_KEY } from '../translator-CO8W_hmJ.cjs';
|
|
5
5
|
import { WebSocket } from 'ws';
|
|
6
6
|
import '../models-DUdx_Y6X.cjs';
|
package/dist/proxy/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Server, IncomingMessage } from 'node:http';
|
|
2
|
-
import { f as ProxyHandlerOptions, P as ProxyClients } from '../responses-turn-
|
|
3
|
-
export { D as DialObserver, a as DialReport, g as ProxyIdentity, h as ProxyVideoOutLane, S as SessionGoneError, U as UnknownModelError } from '../responses-turn-
|
|
2
|
+
import { f as ProxyHandlerOptions, P as ProxyClients } from '../responses-turn-B0zlsQGn.js';
|
|
3
|
+
export { D as DialObserver, a as DialReport, g as ProxyIdentity, h as ProxyVideoOutLane, S as SessionGoneError, U as UnknownModelError } from '../responses-turn-B0zlsQGn.js';
|
|
4
4
|
export { h as GEMINI_LIVE_PATH, V as VIDEO_OUT_SETUP_KEY } from '../translator-Ddd65sXR.js';
|
|
5
5
|
import { WebSocket } from 'ws';
|
|
6
6
|
import '../models-DUdx_Y6X.js';
|
package/dist/proxy/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{B as l,C as a,D as x,E as s,F as O,G as f,S as I,a as n,b as p,c as m}from"../chunk-
|
|
1
|
+
import{B as l,C as a,D as x,E as s,F as O,G as f,S as I,a as n,b as p,c as m}from"../chunk-U7EJSC2N.js";import{d as r,e as i}from"../chunk-TMQJY56F.js";import{a as o,h as t}from"../chunk-3JWYIKHM.js";import"../chunk-6M5JK4YC.js";import{c as e}from"../chunk-YSFSRI3D.js";e();export{o as GEMINI_LIVE_PATH,a as OPENAI_REALTIME_PATH,l as OpenAIRealtimeConnection,n as ResumptionRegistry,i as SessionGoneError,r as UnknownModelError,t as VIDEO_OUT_SETUP_KEY,p as attachGeminiLive,x as attachOpenAIRealtime,I as createOpenAIProxy,f as fixedClientsResolver,O as realtimeBindingFor,s as realtimeModelOf,m as serveGeminiLiveSocket};
|
|
@@ -51,6 +51,8 @@ interface InferenceRecord {
|
|
|
51
51
|
readonly completion_tokens: number | null;
|
|
52
52
|
readonly output_units: number | null;
|
|
53
53
|
|
|
54
|
+
readonly output_minutes: number | null;
|
|
55
|
+
|
|
54
56
|
readonly session_id: string | null;
|
|
55
57
|
|
|
56
58
|
readonly queue_ms: number | null;
|
|
@@ -438,6 +440,8 @@ interface InferenceUsageRecord {
|
|
|
438
440
|
completion_tokens: number | null;
|
|
439
441
|
output_units: number | null;
|
|
440
442
|
|
|
443
|
+
output_minutes: number | null;
|
|
444
|
+
|
|
441
445
|
cost_nano_usd: number | null;
|
|
442
446
|
price_version: string | null;
|
|
443
447
|
priced_at: string | null;
|
|
@@ -480,6 +484,8 @@ interface LedgerWrite {
|
|
|
480
484
|
completion_tokens: number | null;
|
|
481
485
|
output_units: number | null;
|
|
482
486
|
|
|
487
|
+
output_minutes: number | null;
|
|
488
|
+
|
|
483
489
|
queue_ms: number | null;
|
|
484
490
|
prefill_ms: number | null;
|
|
485
491
|
decode_ms: number | null;
|
|
@@ -151,6 +151,8 @@ interface InferenceRecord {
|
|
|
151
151
|
readonly prompt_tokens: number | null;
|
|
152
152
|
readonly completion_tokens: number | null;
|
|
153
153
|
readonly output_units: number | null;
|
|
154
|
+
/** THE OUTPUT-MINUTE QUANTITY — see {@link InferenceTurn.outputMinutes}. */
|
|
155
|
+
readonly output_minutes: number | null;
|
|
154
156
|
/**
|
|
155
157
|
* THE INSTANCE that served this request — see
|
|
156
158
|
* {@link InferenceTurn.sessionId}. On the log line as well as the ledger,
|
|
@@ -1485,6 +1487,13 @@ interface InferenceUsageRecord {
|
|
|
1485
1487
|
prompt_tokens: number | null;
|
|
1486
1488
|
completion_tokens: number | null;
|
|
1487
1489
|
output_units: number | null;
|
|
1490
|
+
/**
|
|
1491
|
+
* THE OUTPUT-MINUTE QUANTITY — a text-to-speech row's delivered audio
|
|
1492
|
+
* duration in minutes, the quantity its `per_output_minute` price reads.
|
|
1493
|
+
* null = this modality has no such quantity (a token lane, an unmeasured
|
|
1494
|
+
* TTS answer) — never "zero of it".
|
|
1495
|
+
*/
|
|
1496
|
+
output_minutes: number | null;
|
|
1488
1497
|
/** NULL = NOT YET PRICED (ENG-317). Never "free". */
|
|
1489
1498
|
cost_nano_usd: number | null;
|
|
1490
1499
|
price_version: string | null;
|
|
@@ -1661,6 +1670,17 @@ interface LedgerWrite {
|
|
|
1661
1670
|
prompt_tokens: number | null;
|
|
1662
1671
|
completion_tokens: number | null;
|
|
1663
1672
|
output_units: number | null;
|
|
1673
|
+
/**
|
|
1674
|
+
* THE OUTPUT-MINUTE QUANTITY — the delivered audio duration of a
|
|
1675
|
+
* text-to-speech answer, in minutes, carried from the record
|
|
1676
|
+
* ({@link InferenceRecord.output_minutes}). The control plane prices a
|
|
1677
|
+
* `per_output_minute` task from THIS field; `output_units` stays the
|
|
1678
|
+
* per-image/per-clip quantity. NULL = the request has no such quantity —
|
|
1679
|
+
* a discarded answer, or a lane that never measured one. NEVER zero of it,
|
|
1680
|
+
* and never coerced: an unmeasured TTS row is written unpriced
|
|
1681
|
+
* (`output_minutes_absent`), not at a billed zero.
|
|
1682
|
+
*/
|
|
1683
|
+
output_minutes: number | null;
|
|
1664
1684
|
/**
|
|
1665
1685
|
* THE WEIGHT (ENG-417) — the serve runtime's own residency in whole
|
|
1666
1686
|
* milliseconds. An allocation input, never GPU time and never a cost. NULL
|
|
@@ -1736,7 +1756,8 @@ interface LedgerRecorded {
|
|
|
1736
1756
|
* Why the row carries no price, or null when it is priced. One of
|
|
1737
1757
|
* `not_a_catalog_model` | `outcome_not_billable` | `http_status_not_success`
|
|
1738
1758
|
* | `task_unit_undeclared` | `task_not_token_billed` | `token_counts_absent`
|
|
1739
|
-
* | `no_active_price` | `no_active_input_price` |
|
|
1759
|
+
* | `output_minutes_absent` | `no_active_price` | `no_active_input_price` |
|
|
1760
|
+
* `no_active_output_price`.
|
|
1740
1761
|
* It is a NORMAL answer, not an error: no PER-TOKEN rate has been seeded
|
|
1741
1762
|
* yet, so this lane has nothing to price with. (Not "the price book is
|
|
1742
1763
|
* empty" — `model_prices` has carried live `per_minute` rows since
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@urun-sh/openai",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.17",
|
|
4
4
|
"description": "OpenAI-compatible Realtime + Responses SDK over uRun session primitives (not websockets).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
"build:bin": "bun build --compile --external @roamhq/wrtc src/proxy/bin.ts --outfile dist/bin/urun-openai && node scripts/assert-bin-transport.mjs"
|
|
71
71
|
},
|
|
72
72
|
"peerDependencies": {
|
|
73
|
-
"@urun-sh/core": "^0.6.
|
|
73
|
+
"@urun-sh/core": "^0.6.17"
|
|
74
74
|
},
|
|
75
75
|
"dependencies": {
|
|
76
76
|
"@prometheus-io/client": "^0.16.1",
|
package/dist/chunk-5PZ6OVEK.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import{a as j,b as W}from"./chunk-VRCCLQ5P.js";import{C as re,D as oe,E as se,F as ie,H as ae,I as A,J as le,K as ce,L as ue,M as de,N as he,O as pe,P as ge,Q as fe,R as me,b as Z,d as ee,e as te,q as ne,r as O}from"./chunk-2PIYH7O7.js";import{e as Q,g as z}from"./chunk-TMQJY56F.js";import{a as X}from"./chunk-3JWYIKHM.js";import{c as p}from"./chunk-YSFSRI3D.js";p();import{Counter as S,Gauge as ye,Histogram as we,Registry as Ue,collectDefaultMetrics as Pe}from"@prometheus-io/client";import{createServer as $e}from"http";var It=9464,be="/metrics",I="(other)",Le="(none)",He=64,Me=128,De=["/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",ae,ce],Fe=new Set(De);function Ke(t){return Fe.has(t)?t:I}function Ge(t){return t===null?"none":t>=500?"5xx":t>=400?"4xx":t>=300?"3xx":t>=200?"2xx":"1xx"}var T=class{constructor(e){this.max=e}max;admitted=new Set;overflows=0;of(e,n=!0){return e===null||e===""?Le:this.admitted.has(e)?e:n?this.admitted.size>=this.max?(this.overflows+=1,I):(this.admitted.add(e),e):I}get size(){return this.admitted.size}get overflowCount(){return this.overflows}},N=class{registry=new Ue;modelLabel=new T(He);orgLabel=new T(Me);requests;duration;ttft;streamErrors;undelivered;ledgerWrites;ledgerQueue;labelCardinality;labelOverflows;constructor(e={}){this.requests=new S({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 we({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 we({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 S({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 S({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 S({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 ue)this.ledgerWrites.inc({outcome:n.outcome,reason:n.reason},0);this.ledgerQueue=new ye({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"},de()),this.ledgerQueue.set({measure:"oldest_age_seconds"},he()/1e3)}}),this.labelCardinality=new ye({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 S({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&&Pe({register:this.registry,prefix:"urun_inference_proxy_"})}observeLedger(e){this.ledgerWrites.inc({outcome:e.outcome,reason:e.reason})}observe(e){let n=Ke(e.path),o=e.status!==null&&!(e.status>=400&&e.status<500),r=this.modelLabel.overflowCount+this.orgLabel.overflowCount,a=this.modelLabel.of(e.model,o),s=this.orgLabel.of(e.org,o);this.modelLabel.overflowCount+this.orgLabel.overflowCount-r>0&&(this.modelLabel.overflowCount>r?this.labelOverflows.inc({label:"model"}):this.labelOverflows.inc({label:"org"}));let l=e.stream===!0?"true":"false";this.requests.inc({lane:n,method:e.method===""?I:e.method,status_class:Ge(e.status),stream:l,model:a,org:s}),this.duration.observe({lane:n,stream:l,model:a,org:s},e.duration_ms/1e3),e.ttft_ms!==null&&this.ttft.observe({lane:n,model:a,org:s},e.ttft_ms/1e3),e.stream_error&&this.streamErrors.inc({lane:n,model:a}),e.delivered||this.undelivered.inc({lane:n,model:a})}scrape(){return this.registry.metrics()}get contentType(){return this.registry.contentType}};function ve(t){return $e((e,n)=>{(async()=>{let o=(e.url??"/").split("?")[0];if(e.method!=="GET"||o!==be){n.writeHead(404,{"content-type":"text/plain"}),n.end(`no route for ${e.method} ${o} \u2014 this port serves only GET ${be}
|
|
2
|
-
`);return}let r=await t.scrape();n.writeHead(200,{"content-type":t.contentType}),n.end(r)})().catch(o=>{if(!n.headersSent){n.writeHead(500,{"content-type":"text/plain"}),n.end(`${String(o instanceof Error?o.message:o)}
|
|
3
|
-
`);return}n.end()})})}p();import{createHash as Ee}from"crypto";import{createServer as Be}from"http";var $t=9465,ze="x-urun-key-id",je="x-urun-key-fingerprint",We="urun-ratelimit-bucket:",Je="urun-ratelimit-tier:",_e="anonymous",qe=/^urun_sk_[A-Za-z0-9_-]{43}$/;function Ve(t){return Ee("sha256").update(`${We}${t}`).digest("hex")}function Ye(t){return Ee("sha256").update(`${Je}${t}`).digest("hex")}function Qe(t){let e=/^Bearer[ \t]+(\S.*)$/i.exec((t??"").trim());if(!e)return null;let n=e[1].trim();return qe.test(n)?n:null}function Se(){return Be((t,e)=>{let n=Qe(t.headers.authorization);e.setHeader(ze,n===null?_e:Ve(n)),e.setHeader(je,n===null?_e:Ye(n)),e.writeHead(200),e.end()})}p();import{createClientToken as Xe,ClientTokenError as Ze}from"@urun-sh/core";var m=class extends Error{constructor(n,o=401){super(n);this.status=o;this.name="ProxyAuthError"}status},_=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function R(t){let e=(t??"").trim();if(e==="")throw new m("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let n=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!n)throw new m("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return n[1].trim()}var et=60,tt=6e4,nt=4096;function rt(t){return t===401||t===403}var U=class{constructor(e){this.opts=e;this.mint=e.mint??Xe,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new m("empty Authorization bearer token");let n=this.cache.get(e);if(n&&this.now()-n.at<tt)return{apiKey:e,orgId:n.orgId};let o;try{o=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:et})).orgId}catch(r){throw r instanceof Ze&&rt(r.status)?(this.cache.delete(e),new m("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new _(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${r instanceof Error?r.message:String(r)}`)}if(!o)throw new _("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>=nt){let r=this.cache.keys().next();r.done||this.cache.delete(r.value)}return this.cache.set(e,{orgId:o,at:this.now()}),{apiKey:e,orgId:o}}clear(){this.cache.clear()}};p();var ot=8080,P="serve",Gt="0.0.0.0";function st(t,e,n){let o=(t[e]??"").trim();if(!o)throw new Error(`${e} is not set \u2014 ${n}`);return o}function Bt(t=process.env){let e=(t.PORT??"").trim(),n=e===""?ot: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 o=ne(O(st(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")),r=(t.URUN_API_URL??"").trim(),a=r?O(r,"URUN_API_URL"):`${o.replace(/\/+$/,"")}/v1`,s=(t.URUN_CATALOG_URL??"").trim(),i=(t.URUN_CATALOG_ANON_KEY??"").trim();if(!!s!=!!i)throw new Error("URUN_CATALOG_URL and URUN_CATALOG_ANON_KEY must be set together (or both left unset) \u2014 exactly one of them is set, which would silently disable the model-catalog oracle");let l=s?{catalogUrl:O(s,"URUN_CATALOG_URL"),anonKey:i}:null,u=(t.URUN_SESSION_TOKEN_SECRET??"").trim(),f=(t.URUN_SHARED_ORG_API_KEY??"").trim(),x=(t.URUN_SHARED_ORG_ID??"").trim();if(!!f!=!!x)throw new Error("URUN_SHARED_ORG_API_KEY and URUN_SHARED_ORG_ID must be set together (or both left unset) \u2014 exactly one of them is set, which would silently leave the shared endpoints on the session lane");return{port:n,baseUrl:o,apiUrl:a,catalog:l,decisionSecret:u,sharedOrgKey:f,sharedOrgId:x}}p();import{createHash as it,randomUUID as at}from"crypto";var lt=512;function M(t){return`urun-inference-proxy-${it("sha256").update(t).digest("hex").slice(0,32)}`}var $=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 i=this.opts.build(e);return{router:i.router,clients:this.label(i.clients,e)}}let n=0,o=M(e.apiKey),r=()=>{n+=1},a=j({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:P,sharedOnlyListing:!0,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:()=>n===0?o:`${o}#${n}`,onPoolWedged:r,release:"end"}),s=this.opts.decisionIntake?new fe({targetFor:i=>a.targetFor(i),apiUrl:this.opts.apiUrl,apiKey:e.apiKey,orgId:e.orgId,fnName:P,secret:this.opts.decisionIntake.secret,...this.opts.decisionIntake.shared?{shared:this.opts.decisionIntake.shared}:{}}):void 0;return{router:a,clients:this.label(W(a,void 0,r,s),e)}}clientsFor(e){let n=this.tenants.get(e.apiKey);if(n)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,n),n.clients;for(;this.tenants.size>=lt;){let r=this.tenants.entries().next();if(r.done)break;let[a,s]=r.value;this.tenants.delete(a),s.router.closeAll().catch(()=>{})}let o=this.build(e);return this.tenants.set(e.apiKey,o),o.clients}get size(){return this.tenants.size}async closeAll(){let e=[...this.tenants.values()].map(o=>o.router);this.tenants.clear();let n=[];if(await Promise.all(e.map(async o=>{try{await o.closeAll()}catch(r){n.push(String(r instanceof Error?r.message:r))}})),n.length>0)throw new Error(`failed to close ${n.length} tenant backhaul(s): ${n.join("; ")}`)}},Re=512,ct=1024,L=class extends Error{constructor(){super(`conversation capacity reached (${Re} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function ut(t){return{sessionStartOptions:e=>{if(e.startsWith(z))return;let n=t.get(e);return n===void 0?void 0:{sessionId:n}},onSessionOpen:(e,n)=>{e.startsWith(z)||t.set(e,n)}}}function dt(t,e,n,o){let r=j({baseUrl:t.baseUrl,orgId:e.orgId,defaultApp:null,fnName:P,sharedOnlyListing:!0,apiUrl:t.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:t.baseUrl},catalog:t.catalog,subject:n.subject,release:"detach",...ut(o)});return{clients:W(r),release:async()=>{await r.closeAll()}}}var H=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,n){let o={id:n,subject:`${M(e.apiKey)}:live:${n}`},r=new Map,a=this.opts.build?this.opts.build(e,o):dt(this.opts,e,o,r);return{id:n,backhaul:a,attachments:0,nativeRefs:r}}attachEntry(e,n){let o=this.byCaller.get(e.apiKey);o===void 0&&(o=new Map,this.byCaller.set(e.apiKey,o));let r=o.get(n);if(r!==void 0)return o.delete(n),o.set(n,r),r.attachments+=1,r;this.evictOverflow();let a=this.build(e,n);return a.attachments=1,o.set(n,a),a}get conversationCount(){let e=0;for(let n of this.byCaller.values())e+=n.size;return e}evictOverflow(){for(;this.conversationCount>=Re;){let e=this.byCaller.keys().next();if(e.done)throw new L;let n=e.value,o=this.byCaller.get(n),r=!1;for(let[a,s]of o)if(!(s.attachments>0)){o.delete(a),this.dropHandleIndexFor(n,a),s.backhaul.release().catch(()=>{}),r=!0;break}if(!r)throw o.size===0&&this.byCaller.delete(n),new L;o.size===0&&this.byCaller.delete(n)}}dropHandleIndexFor(e,n){for(let[o,r]of this.byHandle)r.apiKey===e&&r.conversationId===n&&this.byHandle.delete(o)}connectionFor(e){let n=null,o=!1,r=()=>(n===null&&(n=this.attachEntry(e,at())),n),a={createResponse:(s,i)=>r().backhaul.clients.createResponse(s,i),catalogIdentity:s=>r().backhaul.clients.catalogIdentity(s),servingAppSlug:s=>{let i=r().backhaul.clients.servingAppSlug;if(i===void 0)throw new Error("the conversation backhaul wires no servingAppSlug seam \u2014 the caller-org app a request resolves to cannot be known");return i(s)},listModels:()=>r().backhaul.clients.listModels(),sessionHandle:s=>r().backhaul.clients.sessionHandle(s),createResponseOn:(s,i)=>r().backhaul.clients.createResponseOn(s,i),onSessionEnd:(s,i)=>r().backhaul.clients.onSessionEnd(s,i)};return a.openAudio=async(s,i)=>{let l=r().backhaul.clients.openAudio;if(l===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return l(s,i)},a.openVideo=async s=>{let i=r().backhaul.clients.openVideo;if(i===void 0)throw new Error("the conversation backhaul wires no openVideo seam \u2014 video-in is unavailable on this deployment");return i(s)},a.openVideoOut=async s=>{let i=r().backhaul.clients.openVideoOut;if(i===void 0)throw new Error("the conversation backhaul wires no openVideoOut seam \u2014 video-out is unavailable on this deployment");return i(s)},a.supportsAudio=async s=>{let i=r().backhaul.clients.supportsAudio;if(i===void 0)throw new Error("the conversation backhaul wires no supportsAudio seam \u2014 the realtime lane cannot gate audio modality on this deployment");return i(s)},a.supportsSpeech=async s=>{let i=r().backhaul.clients.supportsSpeech;if(i===void 0)throw new Error("the conversation backhaul wires no supportsSpeech seam \u2014 the speech lane cannot gate model capability on this deployment");return i(s)},a.speechVoices=async s=>{let i=r().backhaul.clients.speechVoices;if(i===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 i(s)},a.speechSessionHandle=async s=>{let i=r().backhaul.clients.speechSessionHandle;if(i===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 i(s)},a.openAudioOn=async(s,i)=>{let l=r().backhaul.clients.openAudioOn;if(l===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 l(s,i)},a.evictSpeechSession=async s=>{let i=r().backhaul.clients.evictSpeechSession;if(i===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 i(s)},a.supportsTranscription=async s=>{let i=r().backhaul.clients.supportsTranscription;if(i===void 0)throw new Error("the conversation backhaul wires no supportsTranscription seam \u2014 the transcription lane cannot gate model capability on this deployment");return i(s)},a.listSpeechToTextModels=async()=>{let s=r().backhaul.clients.listSpeechToTextModels;if(s===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 s()},{clients:a,bindResumeHandle:s=>{let i=n;if(i===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=ct;){let l=this.byHandle.keys().next();if(l.done)break;this.byHandle.delete(l.value)}this.byHandle.set(s,{apiKey:e.apiKey,conversationId:i.id})},clientsForResume:async s=>{let i=this.byHandle.get(s),l=i===void 0?void 0:this.byCaller.get(i.apiKey),u=i===void 0||l===void 0?void 0:l.get(i.conversationId);if(i===void 0||u===void 0||i.apiKey!==e.apiKey)throw new Q("the conversation behind this resume handle is gone (evicted at capacity, or the replica restarted) or the handle is not yours \u2014 reconnect without a handle to start a new conversation");return n=u,u.attachments+=1,u.backhaul.clients},release:async()=>{if(o)return;o=!0;let s=n;n=null,s!==null&&(s.attachments-=1,!(s.attachments>0)&&await s.backhaul.release().catch(()=>{}))}}}async detachAll(){let e=[];for(let o of this.byCaller.values())for(let r of o.values())e.push(r.backhaul.release());this.byCaller.clear(),this.byHandle.clear();let n=[];if(await Promise.all(e.map(async o=>{try{await o}catch(r){n.push(String(r instanceof Error?r.message:r))}})),n.length>0)throw new Error(`failed to detach ${n.length} conversation backhaul(s): ${n.join("; ")}`)}};p();var ht="/inference-ledger",pt=1e4,D=class extends Error{ledgerFailureReason;constructor(e,n="duplicate"){super(e),this.name="LedgerDuplicateError",this.ledgerFailureReason=n}},q=class extends Error{constructor(n,o){super(o);this.ledgerAlreadyWritten=n;this.name="LedgerAlreadyWrittenError"}ledgerAlreadyWritten},g=class extends Error{ledgerFailureReason="surface";constructor(e){super(e),this.name="LedgerSurfaceError"}},V=class extends m{ledgerFailureReason="auth";constructor(e){super(e),this.name="LedgerAuthError"}},gt=[["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",xe],["cost_nano_usd",xe],["price_version",t=>t===null||typeof t=="string"]];function ft(t){for(let[e,n]of gt){if(!(e in t))return`${e} (absent)`;if(!n(t[e]))return`${e} (${JSON.stringify(t[e])})`}return null}function mt(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 o=e.existing;return o===null||typeof o!="object"||Array.isArray(o)?{status:"unavailable"}:ft(o)!==null?{status:"unavailable"}:{status:"found",row:o}}function xe(t){return t===null?!0:typeof t=="number"?Number.isFinite(t):typeof t!="string"?!1:/[^\s]/.test(t)&&Number.isFinite(Number(t))}function J(t,e){return t===null?e===null:e===null?!1:Number(e)===t}function Ce(t,e){if(typeof e!="string")return!1;let n=Date.parse(t),o=Date.parse(e);return Number.isFinite(n)&&Number.isFinite(o)&&n===o}function yt(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&&Ce(t.started_at,n.started_at)&&Ce(t.completed_at,n.completed_at)&&J(t.prompt_tokens,n.prompt_tokens)&&J(t.completion_tokens,n.completion_tokens)&&J(t.output_units,n.output_units)}function wt(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new g(`${e} answered a "recorded" that is not an object: ${JSON.stringify(t)}`);let n=t,o=n.inference_id;if(typeof o!="string"||o==="")throw new g(`${e} answered a row with no inference_id: ${JSON.stringify(t)}`);if(!("cost_nano_usd"in n))throw new g(`${e} answered a row with no cost_nano_usd field: ${JSON.stringify(t)}`);let r=n.cost_nano_usd;if(r!=null&&!(typeof r=="number"&&Number.isInteger(r)&&r>=0))throw new g(`${e} answered cost_nano_usd ${JSON.stringify(r)}, which is neither a non-negative integer nor null`);let a=u=>{if(!(u in n))throw new g(`${e} answered a row with no ${u} field: ${JSON.stringify(t)}`);let f=n[u];if(f===null)return null;if(typeof f!="string")throw new g(`${e} answered a non-string ${u}: ${JSON.stringify(f)}`);if(!/[^\s]/.test(f))throw new g(`${e} answered a BLANK ${u} (${JSON.stringify(f)}) \u2014 it satisfies "is a string" while naming nothing, which is exactly what inference_requests_priced_attribution_check exists to forbid`);return f},s=a("price_version"),i=a("unpriced_reason"),l=r!=null;if(l&&(s===null||i!==null))throw new g(`${e} answered a PRICED row (cost_nano_usd ${String(r)}) that is not cleanly attributed \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(i)}`);if(!l&&(s!==null||i===null))throw new g(`${e} answered an UNPRICED row that does not say why \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(i)}. NULL means NOT YET PRICED, and the reason is how an operator learns that traffic is being served which nobody can bill.`);return{inference_id:o,cost_nano_usd:r===void 0?null:r,price_version:s,unpriced_reason:i}}var F=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async write(e,n){let o=`${this.opts.apiUrl.replace(/\/+$/,"")}${ht}`,r;try{r=await this.fetchImpl(o,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify(n),signal:AbortSignal.timeout(pt)})}catch(l){throw new g(`the uRun ledger surface at ${o} could not be reached: ${l instanceof Error?l.message:String(l)}`)}if(r.status===409){let l=await r.text().catch(()=>""),u=mt(l);throw u.status==="found"&&yt(n,u.row)?new q(u.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.`):u.status==="not_in_this_org"?new D(`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 D(`the uRun control plane already holds a ledger row for inference_id ${n.inference_id} that is NOT the one being written${u.status==="unavailable"?" (and it could not be read back to compare)":""} \u2014 nothing was written and nothing was overwritten. Either this proxy wrote twice, or a row was fabricated ahead of it (this write is authenticated by the CALLER'S OWN org key). ${l.slice(0,400)}`)}if(r.status===401||r.status===403)throw await r.text().catch(()=>{}),new V("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(!r.ok){let l=await r.text().catch(u=>`<body unreadable: ${u instanceof Error?u.message:String(u)}>`);throw new g(`the uRun ledger surface at ${o} answered HTTP ${r.status}: ${l.slice(0,400)}`)}let a;try{a=await r.json()}catch(l){throw new g(`the uRun ledger surface at ${o} answered a body that is not JSON: ${l instanceof Error?l.message:String(l)}`)}let s=a?.recorded;if(s===void 0)throw new g(`the uRun ledger surface at ${o} answered a body with no "recorded" field`);let i=wt(s,`the uRun ledger surface at ${o}`);if(i.inference_id!==n.inference_id)throw new g(`the uRun ledger surface at ${o} answered for inference_id ${i.inference_id} when asked about ${n.inference_id}`);return i}};p();import{createServer as vt}from"http";p();var K="/inference-usage",ke=25e3;var bt=new Set(["Error","TypeError","SyntaxError","TimeoutError"]);function Oe(t){if(!(t instanceof Error))return`a ${typeof t}`;let e=bt.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 G=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async lookup(e,n){let o=`${this.opts.apiUrl.replace(/\/+$/,"")}${K}`,r;try{r=await this.fetchImpl(o,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify({inference_ids:n}),signal:AbortSignal.timeout(ke)})}catch(s){throw new A(`the uRun usage surface (POST ${K}) could not be reached within ${ke}ms: ${Oe(s)}`)}if(r.status===401||r.status===403)throw await r.text().catch(()=>{}),new m("the uRun org API key was rejected by the uRun control plane \u2014 check the key and that it is not revoked");if(!r.ok){let s=await r.text().catch(()=>null),i=s===null?"body unreadable":`${s.length}-character body`;throw new A(`the uRun usage surface (POST ${K}) answered HTTP ${r.status} (${i})`)}let a;try{a=await r.json()}catch(s){throw new A(`the uRun usage surface (POST ${K}) answered HTTP ${r.status} with a body that is not JSON: ${Oe(s)}`)}return le(a,this.opts.onRejectedRow)}};var _t=5e3,Et=3e3,St="/api/client-tokens";function Y(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function Ae(t,e,n,o){Y(t,e,{error:{message:n,type:o}})}async function Rt(t,e=fetch){let n=`${t.replace(/\/+$/,"")}${St}`;try{let o=await e(n,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(Et)});return o.status===404?{ready:!1,detail:`${n} answered 404 \u2014 this is not a uRun control plane`}:o.status>=500?{ready:!1,detail:`${n} answered HTTP ${o.status}`}:{ready:!0,detail:`${n} answered HTTP ${o.status}`}}catch(o){return{ready:!1,detail:`${n} unreachable: ${o instanceof Error?o.message:String(o)}`}}}function _n(t){let e=t.resolver??new U({gatewayUrl:t.baseUrl}),n=t.decisionSecret??"",o=t.sharedOrgKey&&t.sharedOrgId?{apiKey:t.sharedOrgKey}:void 0,r=t.registry??new $({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog,...n!==""?{decisionIntake:{secret:n,...o?{shared:o}:{}}}:{}});n!==""&&process.stderr.write(`inference-proxy: stateless direct path ENABLED \u2014 caller-org decisions dial ready runtimes directly (no session mint)
|
|
4
|
-
`);let a=t.probeControlPlane??(()=>Rt(t.baseUrl)),s=t.usageClient??new G({apiUrl:t.apiUrl}),i=t.ledgerClient??new F({apiUrl:t.apiUrl}),l=new N(t.defaultMetrics===void 0?{}:{defaultMetrics:t.defaultMetrics}),u=async c=>e.resolve(R(c.headers.authorization)),f=c=>c instanceof m?{status:c.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:c instanceof _?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,x=me({clients:async c=>r.clientsFor(await u(c)),usage:{authorize:c=>R(c.headers.authorization),lookup:(c,d)=>s.lookup(c,d)},statusOf:f,...t.requestLog?{requestLog:t.requestLog}:{},observe:c=>l.observe(c),ledger:{write:(c,d)=>i.write(R(c.headers.authorization),d),observe:c=>l.observeLedger(c),...t.ledgerLog?{report:t.ledgerLog}:{}}}),v=null,E=vt((c,d)=>{let h=new URL(c.url??"/","http://localhost");(async()=>{if(c.method==="GET"&&h.pathname==="/healthz"){Y(d,200,{ok:!0});return}if(c.method==="GET"&&h.pathname==="/readyz"){let k=Date.now();if(!v||k-v.at>=_t){let Ne=await a();v={at:k,...Ne}}Y(d,v.ready?200:503,{ok:v.ready,control_plane:v.detail});return}let y=h.pathname==="/v1"||h.pathname.startsWith("/v1/"),C=h.pathname.startsWith("/partners/");if(!y&&!C){Ae(d,404,`no route for ${c.method} ${h.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface, the partner adapter surface (/partners/...), the WS upgrade paths (/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent and /v1/realtime), plus /healthz and /readyz`,"invalid_request_error");return}x(c,d)})().catch(y=>{if(!d.headersSent){Ae(d,500,String(y instanceof Error?y.message:y),"proxy_error");return}d.end()})}),B=t.conversations??new H({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),Ie=[Z(E,{authenticate:async c=>{let d=await e.resolve(c),h=B.connectionFor(d);return{clients:h.clients,scope:M(d.apiKey),onBindResumeHandle:h.bindResumeHandle,clientsForResume:h.clientsForResume,release:h.release}}}),te(E,{clients:async c=>r.clientsFor(await u(c)),statusOf:f}).close,oe(E,{resolver:{resolve:async(c,d)=>{let h=await e.resolve(R(c.headers.authorization)),y=B.connectionFor(h),C;try{C=await ie(y.clients,se(c),d)}catch(k){throw y.release().catch(()=>{}),k}return{...C,release:y.release}}}})];E.on("upgrade",(c,d)=>{let h=new URL(c.url??"/","http://localhost");h.pathname===X||h.pathname===ee||h.pathname===re||d.destroyed||(d.write(`HTTP/1.1 404 Not Found\r
|
|
5
|
-
Connection: close\r
|
|
6
|
-
\r
|
|
7
|
-
no WS route for ${h.pathname} \u2014 this endpoint serves only the Gemini Live, /v1/responses, and /v1/realtime upgrade paths`),d.destroy())});let Te=async()=>{for(let c of Ie)await c();await B.detachAll()};return{server:E,metricsServer:ve(l),metrics:l,edgeIdentityServer:Se(),registry:r,closeAll:()=>r.closeAll(),closeWebSockets:Te}}async function En(t,e,n){n&&await n();let o=new Promise(r=>t.close(()=>r()));t.closeAllConnections(),await o,await pe()||ge(),await e()}export{It as a,be as b,I as c,Le as d,He as e,Me as f,Ke as g,Ge as h,T as i,N as j,ve as k,$t as l,ze as m,je as n,We as o,Je as p,_e as q,Ve as r,Ye as s,Qe as t,Se as u,m as v,_ as w,R as x,tt as y,nt as z,U as A,ot as B,P as C,Gt as D,Bt as E,lt as F,M as G,$ as H,Re as I,ct as J,L as K,H as L,ht as M,pt as N,D as O,g as P,wt as Q,F as R,_t as S,Et as T,_n as U,En as V};
|