@urun-sh/openai 0.6.15 → 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-UHCNCPW6.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-PAA7RD6B.js +0 -7
package/dist/proxy/cli.cjs
CHANGED
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
Connection: close\r
|
|
8
8
|
\r
|
|
9
9
|
${c}\r
|
|
10
|
-
`),a.destroy())}))});let s=null;return()=>(s??=(async()=>{for(let i of r.clients)i.close(1001,"server closing");await new Promise((i,a)=>r.close(u=>u===void 0?i():a(u)))})(),s)}var Se=require("ws");var wo=require("crypto"),_o="Inference-Id";function et(){return(0,wo.randomUUID)()}function Sn(t){return t==="/v1"||t.startsWith("/v1/")}function vo(t,e){if(!Sn(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 tt(t){return`resp_${t}`}function bo(t){return`chatcmpl-${t}`}function ko(t){return`msg_${t}`}function ie(t){return e=>{t.sessionId=e.sessionId,t.catalog=e.identity}}function So(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 xo(t){return`${JSON.stringify(t)}
|
|
10
|
+
`),a.destroy())}))});let s=null;return()=>(s??=(async()=>{for(let i of r.clients)i.close(1001,"server closing");await new Promise((i,a)=>r.close(u=>u===void 0?i():a(u)))})(),s)}var Se=require("ws");var wo=require("crypto"),_o="Inference-Id";function et(){return(0,wo.randomUUID)()}function Sn(t){return t==="/v1"||t.startsWith("/v1/")}function vo(t,e){if(!Sn(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 tt(t){return`resp_${t}`}function bo(t){return`chatcmpl-${t}`}function ko(t){return`msg_${t}`}function ie(t){return e=>{t.sessionId=e.sessionId,t.catalog=e.identity}}function So(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 xo(t){return`${JSON.stringify(t)}
|
|
11
11
|
`}var la=256,ca=128;function ne(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,ca)}var Pe=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>=la?"(other)":e;return this.models[r]??={requests:0,tokens_out:0}}model(e){this.modelEntry(e).requests+=1}track(e,n,r){let o=this,s=performance.now(),i=null,a=0;return(async function*(){for await(let d of e)d.type==="response.output_text.delta"&&typeof d.delta=="string"&&(i===null&&(i=performance.now(),o.ttftMs.push(i-s),o.ttftMs.length>512&&o.ttftMs.shift(),r!==void 0&&(r.ttftMs=i-r.startedAt)),a+=1,o.tokensOut+=1,o.charsOut+=d.delta.length,n!==void 0&&(o.modelEntry(n).tokens_out+=1)),yield d;let u=(performance.now()-(i??s))/1e3;a>0&&u>0&&(o.tokRates.push(a/u),o.tokRates.length>512&&o.tokRates.shift())})()}static p50(e){if(!e.length)return null;let n=[...e].sort((r,o)=>r-o);return n[Math.floor(n.length/2)]}snapshot(){let e=Object.values(this.requests).reduce((n,r)=>n+r,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:t.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:t.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:e},persistent_connections:1,history_bytes_saved:Math.max(0,this.naiveInBytes-this.localInBytes),history_bytes_saved_pct:this.naiveInBytes>0?Math.round((1-this.localInBytes/this.naiveInBytes)*1e3)/10:null}}}},pa=256;function fa(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 Ce=class extends Error{},Fe=class{conversations=new Map;thread(e,n){let r=fa(n);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new Ce(`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<=pa)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},Ro=new WeakMap;function nt(t){let e=Ro.get(t);return e||(e=new Fe,Ro.set(t,e)),e}function Be(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function ce(t){return t.find(e=>e.type==="response.completed")?.response??null}function je(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 ke=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}))}},ma=["<tool_call>","<function="];function ga(t){return ma.find(e=>t.includes(e))}function $e(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 Me(t,e,n){if(!(!t||e>0))return ga(n)}function qe(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 ha(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 xn(t,e){let n=ha(t);return n.length>0?n:e?[{role:"assistant",content:e}]:[]}var ae=class extends Error{constructor(n,r,o=400,s){super(n);this.code=r;this.status=o;this.param=s}code;status;param};function He(t,e){try{return t.thread(e.previous_response_id,e.input)}catch(n){throw n instanceof Ce?new ae(n.message,"previous_response_not_found",400,"previous_response_id"):new ae(String(n instanceof Error?n.message:n),"invalid_input")}}function Eo(t){let e=t.text;if(e!==null&&typeof e=="object"){let n=e.format;if(n!==void 0)return n}}async function Rn(t,e,n,r,o){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new ae("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}:{},...Eo(e)!==void 0?{response_format:Eo(e)}:{}},o)}async function rt(t){let{body:e,clients:n,store:r,stats:o}=t,s=He(r,e);e.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(s))-Buffer.byteLength(JSON.stringify(e.input)));let i=t.responseId,a=t.remember??e.store!==!1,u=e.store!==!1,d=ne(e);o.model(d);let l=Array.isArray(e.tools)&&e.tools.length>0,p=t.turn,m=await Rn(n,e,s,!0,p?ie(p):void 0);return{responseId:i,events:va({events:m,conversation:s,responseId:i,modelKey:d,requestHadTools:l,remember:a,persist:u,store:r,stats:o,turn:t.turn})}}function ya(t,e){let n=[...t].sort((u,d)=>u.output_index-d.output_index).map(({item:u})=>u),r=Array.isArray(e?.output)?e.output:[],o=r.filter(u=>u!==null&&typeof u=="object"&&u.type==="message"),s=n.filter(u=>u.type==="message").length===o.length,i=0;return[...n.map(u=>u.type==="message"&&s?_a(o[i++],u):u),...wa(r,n)]}function wa(t,e){let n=e.some(d=>d.type==="message"),r=e.some(d=>d.type==="reasoning"),o=d=>String(d.call_id??d.id??""),s=e.filter(d=>d.type==="function_call"),i=t.filter(d=>d!==null&&typeof d=="object"&&d.type==="function_call").length,a=s.length===i,u=new Set(s.map(o));return t.filter(d=>{if(d===null||typeof d!="object")return!0;let l=d;return l.type==="message"?!n:l.type==="reasoning"?!r:l.type==="function_call"?!a&&!u.has(o(l)):!0})}function _a(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*va(t){let{events:e,conversation:n,responseId:r,modelKey:o,requestHadTools:s,remember:i,persist:a,store:u,stats:d}=t,l=[],p=T=>T.response&&typeof T.response=="object"?{...T,response:{...T.response,id:r}}:T,m=`item_${r.slice(5)}`,c=!1,f=!1,g=0;function*_(){c||(c=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let y=[],h=(T,U)=>{y.push({output_index:T,item:U})},w=!1,A=0,v="",b=0,x="";function*L(){if(!w)return;w=!1,yield{type:"response.reasoning_text.done",item_id:v,output_index:A,content_index:0,text:x};let T={id:v,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:x}],status:"completed"};yield{type:"response.output_item.done",output_index:A,item:T},h(A,T),x=""}let S=!1,R=0,C="",F=0,Y="",te="";function*ue(T){if(!S)return;S=!1,yield{type:"response.output_text.done",item_id:C,output_index:R,content_index:0,text:Y},yield{type:"response.content_part.done",item_id:C,output_index:R,content_index:0,part:{type:"output_text",text:Y}};let U={id:C,type:"message",role:"assistant",status:T,content:[{type:"output_text",text:Y}]};yield{type:"response.output_item.done",output_index:R,item:U},h(R,U),Y=""}let _e=new ke,H=null,G=-1;function*Z(T){if(!H)return;let U=H;H=null,yield{type:"response.function_call_arguments.done",item_id:U.fcId,output_index:U.outputIndex,arguments:U.args};let z={id:U.fcId,type:"function_call",call_id:U.callId??U.fcId,name:U.name??"",arguments:U.args,status:T};yield{type:"response.output_item.done",output_index:U.outputIndex,item:z},h(U.outputIndex,z)}let Re=!1;for await(let T of d.track(e,o,t.turn)){l.push(T);let U=String(T.type??"");if(U==="response.created"||U==="response.in_progress"){c=!0,yield p(T);continue}if(U==="response.output_item.added"){f=!0,yield p(T);continue}if(U==="response.reasoning_text.delta"&&!f){yield*_(),w||(w=!0,A=g++,b+=1,v=b===1?`rs_${r.slice(5)}`:`rs_${r.slice(5)}_${b}`,yield{type:"response.output_item.added",output_index:A,item:{id:v,type:"reasoning",summary:[],content:[],status:"in_progress"}}),x+=String(T.delta??""),yield{type:"response.reasoning_text.delta",item_id:v,output_index:A,content_index:0,delta:T.delta};continue}if(U==="response.output_text.delta"&&!f){yield*_(),yield*L(),S||(S=!0,R=g++,F+=1,C=F===1?m:`${m}_${F}`,yield{type:"response.output_item.added",output_index:R,item:{id:C,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:C,output_index:R,content_index:0,part:{type:"output_text",text:""}}),Y+=String(T.delta??""),te+=String(T.delta??""),yield{type:U,item_id:C,output_index:R,content_index:0,delta:T.delta};continue}let z=f?null:je(T);if(z){if(yield*_(),yield*L(),yield*ue("completed"),_e.add(z),z.tool_index!==G){yield*Z("completed"),G=z.tool_index;let ge=g++;H={fcId:`fc_${r.slice(5)}_${z.tool_index}`,outputIndex:ge,callId:z.call_id,name:z.name,args:""},yield{type:"response.output_item.added",output_index:ge,item:{id:H.fcId,type:"function_call",call_id:H.callId??H.fcId,name:H.name??"",arguments:"",status:"in_progress"}}}H&&(z.call_id&&(H.callId=z.call_id),z.name&&(H.name=z.name),H.args+=z.delta,yield{type:"response.function_call_arguments.delta",item_id:H.fcId,output_index:H.outputIndex,delta:z.delta});continue}if(U==="response.completed"&&!f){let ge=Be(T.response),B=Me(s,_e.size+ge.length,te);if(B){Re=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:$e(o,B)}};break}if(yield*L(),yield*ue("completed"),yield*Z("completed"),_e.size===0)for(let ve of ge){let Ne=g++,yt=ve.id??ve.call_id??`fc_${Ne}`,sn=ve.arguments??"",sr={id:yt,type:"function_call",call_id:ve.call_id??yt,name:ve.name??""};yield{type:"response.output_item.added",output_index:Ne,item:{...sr,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:yt,output_index:Ne,delta:sn},yield{type:"response.function_call_arguments.done",item_id:yt,output_index:Ne,arguments:sn};let ir={...sr,arguments:sn,status:"completed"};yield{type:"response.output_item.done",output_index:Ne,item:ir},h(Ne,ir)}let ee=ya(y,T.response);yield ee.length>0?p({...T,response:{...T.response,output:ee}}):p(T);continue}yield p(T)}if(yield*L(),yield*ue("incomplete"),yield*Z("incomplete"),i&&!Re){let T=ce(l);u.remember(r,n,xn(T,qe(l,T)),a)}}var ba="/v1/responses",ka=64*1024*1024,Sa=8*1024*1024,xa=16,Ao=32,Oo=3600*1e3,Ra=/^[A-Za-z0-9._-]{1,256}$/;function re(t,e,n){return{type:"error",status:t,...n!==void 0?{stream_id:n}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var Ea=re(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 On(){let t,e;return{promise:new Promise((r,o)=>{t=r,e=o}),resolve:t,reject:e}}async function Aa(t){for(;t.bufferedAmount>Sa&&t.readyState===Se.WebSocket.OPEN;){let{promise:e,resolve:n}=On();setTimeout(n,5),await e}}var En=class extends Fe{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 Ce))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)}},An=class{constructor(e,n,r,o){this.ws=e;this.clients=n;this.stats=r;this.statusOf=o;this.store=new En(nt(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(re(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${Oo/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},Oo).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(re(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(re(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(re(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(re(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(re(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(re(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"||!Ra.test(o.stream_id)){this.sendFrame(Ea);return}if(s=o.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=Ao){this.sendFrame(re(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${Ao}). Reuse an existing stream_id or open a new WebSocket connection.`,param:"stream_id"},s));return}}let i=o.generate===!1?()=>this.runWarmup(o,s):()=>this.runTurn(o,s),a=s??"",u=this.lanes.get(a)??Promise.resolve();this.lanes.set(a,u.then(i).catch(()=>{}))}namedLaneCount(){return this.lanes.has("")?this.lanes.size-1:this.lanes.size}async runTurn(e,n){if(!this.closed){await this.acquire();try{let r=Buffer.byteLength(JSON.stringify(e));this.stats.request("responses_ws",r,r);let o;try{o=await rt({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:tt(et())})}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!==Se.WebSocket.OPEN)break;this.send(i,n),await Aa(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=tt(et());try{let o=He(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 ae)return re(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 re(404,{type:"invalid_request_error",code:"model_not_found",message:r},n);if(e instanceof X)return re(503,{type:"upstream_error",code:"session_gone",message:r},n);let o=this.statusOf?.(e)??null;return o?re(o.status,{type:o.openaiType,message:r},n):re(500,{type:"upstream_error",message:r},n)}async acquire(){for(;this.active>=xa;){let{promise:e,resolve:n}=On();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!==Se.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!==Se.WebSocket.OPEN||this.ws.send(JSON.stringify(e))}};function $t(t,e,n){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${n}\r
|
|
12
12
|
`;t.end(`HTTP/1.1 ${e} ${r}\r
|
|
13
13
|
Connection: close\r
|
|
14
14
|
Content-Type: text/plain; charset=utf-8\r
|
|
15
15
|
Content-Length: ${Buffer.byteLength(o)}\r
|
|
16
16
|
\r
|
|
17
|
-
${o}`)}function Io(t,e){let n=new Se.WebSocketServer({noServer:!0,maxPayload:ka}),r=new Pe,o=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,u=!1,d=(l,p,m)=>{let c;try{c=new URL(l.url??"/","http://localhost")}catch{$t(p,400,"malformed upgrade URL");return}if(p.on("error",()=>p.destroy()),p.on("close",()=>i.delete(p)),c.pathname!==ba){i.add(p);return}s.add(p);let f=!1;(async()=>{if(e.apiKey&&(l.headers.authorization??"")!==`Bearer ${e.apiKey}`){$t(p,401,"invalid local proxy api key");return}let g;try{g=await a(l)}catch(_){let y=e.statusOf?.(_)??null;$t(p,y?.status??500,"upstream resolver rejected the connection");return}u||p.destroyed||(f=!0,n.handleUpgrade(l,p,m,_=>{s.delete(p),o.add(_),_.on("close",()=>o.delete(_));try{new An(_,g,r,e.statusOf)}catch{o.delete(_),_.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!f&&!p.destroyed&&$t(p,500,"responses websocket upgrade failed")}).finally(()=>s.delete(p))};return t.on("upgrade",d),{stats:r,close:()=>{let{promise:l,resolve:p}=On();if(u)return p(),l;u=!0,t.off("upgrade",d);for(let c of s)c.destroy();s.clear();for(let c of i)c.destroy();i.clear();for(let c of o)c.close(1001,"server shutting down");let m=setTimeout(()=>{for(let c of o)c.readyState!==Se.WebSocket.CLOSED&&c.terminate()},2e3);return m.unref?.(),n.close(()=>{clearTimeout(m),p()}),l}}}var rs=require("@urun-sh/core");var To=require("@urun-sh/core"),Oa={no_capacity:429,queue_timeout:429,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502},Ia={no_capacity:"no ready GPU capacity for this model right now \u2014 the platform will cold-wake it on request; retry",queue_timeout:"the session was admitted but never became live within the cold-wake fence \u2014 the backend is busy or wedged; retry",cold_start_exceeded:"the cold boot exceeded its budget without serving \u2014 the platform keeps pursuing the wake; retry with a longer client timeout",queue_full:"the admission queue for this model is full \u2014 retry after the Retry-After period",admission_busy:"admission is momentarily contended (lock convoy) \u2014 retry shortly",admission_unavailable:"session admission is temporarily unavailable \u2014 retry",backend_error:"the control plane failed while admitting the session"};function In(t,e){let n=Ia[t.code];return e?`${n} (${e})`:n}function Mt(t){if(t==null)return null;let e=Oa[t];return e===void 0?null:{status:e,code:t,retryable:e!==502,openaiType:e===429?"capacity_error":"upstream_error",anthropicType:e===429?"rate_limit_error":e===503?"overloaded_error":"api_error"}}function Lt(t){if(t.kind==="create-failed")return Mt(t.status.error?.code??null);if(t.kind!=="never-live")return null;let e=(t.status.elapsedMs??0)>=To.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return Mt(e?"cold_start_exceeded":"queue_timeout")}var Co=Ee(require("os"),1),$o=Ee(require("path"),1),Mo=require("url"),Lo=require("util");var Po="0.6.
|
|
17
|
+
${o}`)}function Io(t,e){let n=new Se.WebSocketServer({noServer:!0,maxPayload:ka}),r=new Pe,o=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,u=!1,d=(l,p,m)=>{let c;try{c=new URL(l.url??"/","http://localhost")}catch{$t(p,400,"malformed upgrade URL");return}if(p.on("error",()=>p.destroy()),p.on("close",()=>i.delete(p)),c.pathname!==ba){i.add(p);return}s.add(p);let f=!1;(async()=>{if(e.apiKey&&(l.headers.authorization??"")!==`Bearer ${e.apiKey}`){$t(p,401,"invalid local proxy api key");return}let g;try{g=await a(l)}catch(_){let y=e.statusOf?.(_)??null;$t(p,y?.status??500,"upstream resolver rejected the connection");return}u||p.destroyed||(f=!0,n.handleUpgrade(l,p,m,_=>{s.delete(p),o.add(_),_.on("close",()=>o.delete(_));try{new An(_,g,r,e.statusOf)}catch{o.delete(_),_.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!f&&!p.destroyed&&$t(p,500,"responses websocket upgrade failed")}).finally(()=>s.delete(p))};return t.on("upgrade",d),{stats:r,close:()=>{let{promise:l,resolve:p}=On();if(u)return p(),l;u=!0,t.off("upgrade",d);for(let c of s)c.destroy();s.clear();for(let c of i)c.destroy();i.clear();for(let c of o)c.close(1001,"server shutting down");let m=setTimeout(()=>{for(let c of o)c.readyState!==Se.WebSocket.CLOSED&&c.terminate()},2e3);return m.unref?.(),n.close(()=>{clearTimeout(m),p()}),l}}}var rs=require("@urun-sh/core");var To=require("@urun-sh/core"),Oa={no_capacity:429,queue_timeout:429,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502},Ia={no_capacity:"no ready GPU capacity for this model right now \u2014 the platform will cold-wake it on request; retry",queue_timeout:"the session was admitted but never became live within the cold-wake fence \u2014 the backend is busy or wedged; retry",cold_start_exceeded:"the cold boot exceeded its budget without serving \u2014 the platform keeps pursuing the wake; retry with a longer client timeout",queue_full:"the admission queue for this model is full \u2014 retry after the Retry-After period",admission_busy:"admission is momentarily contended (lock convoy) \u2014 retry shortly",admission_unavailable:"session admission is temporarily unavailable \u2014 retry",backend_error:"the control plane failed while admitting the session"};function In(t,e){let n=Ia[t.code];return e?`${n} (${e})`:n}function Mt(t){if(t==null)return null;let e=Oa[t];return e===void 0?null:{status:e,code:t,retryable:e!==502,openaiType:e===429?"capacity_error":"upstream_error",anthropicType:e===429?"rate_limit_error":e===503?"overloaded_error":"api_error"}}function Lt(t){if(t.kind==="create-failed")return Mt(t.status.error?.code??null);if(t.kind!=="never-live")return null;let e=(t.status.elapsedMs??0)>=To.COLD_BOOT_WHEN_LIVE_TIMEOUT_MS;return Mt(e?"cold_start_exceeded":"queue_timeout")}var Co=Ee(require("os"),1),$o=Ee(require("path"),1),Mo=require("url"),Lo=require("util");var Po="0.6.17";var Uo=4141,Pa=Po,Nt={claude:{bin:"claude",env:"anthropic"},codex:{bin:"codex",env:"openai"},pi:{bin:"pi",env:"openai"},omp:{bin:"omp",env:"openai"},aider:{bin:"aider",env:"openai"},opencode:{bin:"opencode",env:"openai"},crush:{bin:"crush",env:"openai"}};function Cn(t,e="openai"){if(e==="anthropic")return{ANTHROPIC_BASE_URL:`http://127.0.0.1:${t}`,ANTHROPIC_API_KEY:"urun-local"};let n=`http://127.0.0.1:${t}/v1`;return{OPENAI_BASE_URL:n,OPENAI_API_BASE:n,OPENAI_API_KEY:"urun-local"}}function No(t,e,n){let r={...t,...Cn(e,n)};return delete r.URUN_API_KEY,delete r.URUN_JWT,delete r.URUN_SESSION_TOKEN_SECRET,r}function Do(t,e,n){let r=["-c","model_providers.urun.name=urun","-c",`model_providers.urun.base_url="http://127.0.0.1:${t}/v1"`,"-c",'model_providers.urun.wire_api="responses"',"-c",'model_provider="urun"'],o=n.indexOf("--"),s=o===-1?n:n.slice(0,o),i=s.some(l=>l==="-m"||l==="--model"||l.startsWith("--model=")),a=n[0]==="exec",u=s.includes("--skip-git-repo-check"),d=a&&!u?["exec","--skip-git-repo-check",...n.slice(1)]:n;return[...r,...i?[]:["-m",e],...d]}function Fo(t,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--provider","urun","--model",t,...e]}function Bo(t,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--model",`urun/${t}`,...e]}function jo(t,e,n){let r=(t.URUN_PI_EXTENSION_ENTRY??"").trim();if(r){if(!n(r))throw new Error(`URUN_PI_EXTENSION_ENTRY points at ${r}, which does not exist \u2014 the launcher that injected it (the vendored urun CLI shim) must materialize the pi extension bundle before spawning pi`);return r}let o=(0,Mo.fileURLToPath)(new URL("../pi-extension/standalone.cjs",e));if(!n(o))throw new Error(`pi extension entry not found at ${o} \u2014 the pi lane loads the self-contained @urun-sh/openai dist/pi-extension/standalone.cjs from the installed package (run \`urun compat pi\` via the npm-installed CLI, e.g. \`npx @urun-sh/openai\`), or pass an entry to pi yourself: \`pi -e <path-to-@urun-sh/openai>/dist/pi-extension/standalone.cjs\``);return o}function qo(t){let e=Number(t);if(!Number.isInteger(e)||e<0)throw new Error(`--port needs a non-negative integer (0 = OS-assigned ephemeral), got ${t}`);return e}function Ho(t){if(t[0]!=="--port")return null;let e=qo(t[1]);return t.splice(0,2),e}function $n(t){let e=t.indexOf("--port");if(e===-1)return Uo;let n=qo(t[e+1]);return t.splice(e,2),n}function Go(t){if(t===0)throw new Error("`env` cannot use --port 0: it starts no proxy, so the OS never assigns the ephemeral port \u2014 pass the actual port of the running proxy");return t}function Tn(t,e){let n=(t[e]??"").trim();if(!n)throw new Error(`${e} is not set. The proxy needs URUN_BASE_URL, URUN_ORG_ID, URUN_APP and either URUN_API_KEY (the org key; the proxy vends short-lived function-scoped client tokens from it via the SDK) or URUN_JWT (an explicit pre-vended token override) \u2014 plus optional URUN_FUNCTION, default "serve" \u2014 to open the backhaul session.`);return n}function Ge(t){let e=Ca(Tn(t,"URUN_BASE_URL")),n=Tn(t,"URUN_ORG_ID"),r=Tn(t,"URUN_APP"),o=(t.URUN_FUNCTION??"serve").trim()||"serve",s=(t.URUN_API_URL??"").trim(),i=s?Ut(s,"URUN_API_URL"):qr,a=(t.URUN_SESSION_TOKEN_SECRET??"").trim(),u=(t.URUN_JWT??"").trim();if(u)return{baseUrl:e,orgId:n,appId:r,fnName:o,apiUrl:i,decisionSecret:a,auth:{lane:"jwt",jwt:u}};let d=(t.URUN_API_KEY??"").trim();if(d){let l=(t.URUN_GATEWAY_URL??"").trim(),p=l?Ut(l):void 0;return{baseUrl:e,orgId:n,appId:r,fnName:o,apiUrl:i,decisionSecret:a,auth:{lane:"api-key",apiKey:d,gatewayUrl:p}}}throw new Error("neither URUN_API_KEY nor URUN_JWT is set. Set URUN_API_KEY (the org API key \u2014 the proxy vends short-lived, function-scoped client tokens from it via the SDK, so the key never reaches the agent process) or URUN_JWT (an explicit pre-vended token; when set it always wins). URUN_BASE_URL, URUN_ORG_ID and URUN_APP are also required.")}function Ca(t,e="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(t).pathname))throw new Error(`${e} must be the session-gateway base (e.g. https://api.urun.sh), got ${t} \u2014 a trailing /v1 is the org control-plane API form (that belongs in URUN_API_URL); session allocation 404s against it. Drop the /v1.`);return t}function Ut(t,e="URUN_GATEWAY_URL"){let n=new URL(t),r=n.hostname==="localhost"||n.hostname==="127.0.0.1"||n.hostname==="[::1]";if(n.protocol!=="https:"&&!r)throw new Error(`${e} must be https (got ${t}) \u2014 the org API key rides the request's Authorization header`);return t}function Mn(t){return{app:t.appId,org:t.orgId,fn:t.fnName,base_url:t.baseUrl,proxy_version:Pa}}function $a(t,e){return t.app===e.app&&t.org===e.org&&t.fn===e.fn&&t.base_url===e.base_url&&t.proxy_version===e.proxy_version}var Ma=250;async function Ln(t,e=Ma){try{let n=await fetch(`http://127.0.0.1:${t}/stats`,{signal:AbortSignal.timeout(e)});if(!n.ok)return null;let r=(await n.json()).identity;if(r==null||typeof r!="object")return null;let{app:o,org:s,fn:i,base_url:a,proxy_version:u}=r;return typeof o!="string"||typeof s!="string"||typeof i!="string"||typeof a!="string"||typeof u!="string"?null:{app:o,org:s,fn:i,base_url:a,proxy_version:u}}catch{return null}}var Vo=2e3;async function La(t,e=Vo){try{let n=await fetch(`http://127.0.0.1:${t}/v1/models`,{signal:AbortSignal.timeout(e)});return n.ok?null:`GET /v1/models answered HTTP ${n.status}`}catch(n){return`GET /v1/models failed (${String(n instanceof Error?n.message:n)})`}}async function Wo(t,e,n=Uo,r=Vo){if(t!=null)return{mode:"spawn",port:t};let o=await Ln(n);if(o!==null&&$a(o,e)){let s=await La(n,r);return s===null?{mode:"reuse",port:n,identity:o}:{mode:"spawn",port:0,reason:`a urun-openai proxy on port ${n} matches this invocation's identity but failed the serving-surface liveness check (${s}) \u2014 starting a fresh proxy on an ephemeral port instead of pointing the harness at it`}}return{mode:"spawn",port:0}}function Un(t,e=Co.default.homedir()){return $o.default.join(e,".urun","logs",`compat-proxy-${t}.log`)}var Pn=["log","info","warn","error","debug","trace"];function Jo(t,e=process,n=console){let r=e.stdout.write,o=e.stderr.write,s=(a,...u)=>t.write(a,...u);e.stdout.write=s,e.stderr.write=s;let i=Object.fromEntries(Pn.map(a=>[a,n[a]]));for(let a of Pn)n[a]=(...u)=>{t.write(`${(0,Lo.format)(...u)}
|
|
18
18
|
`)};return()=>{e.stdout.write=r,e.stderr.write=o;for(let a of Pn)n[a]=i[a]}}var Ua=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function Ko(t){let e=t?.code;return typeof e=="string"&&Ua.has(e)}function de(t,e){try{return t.write(e),!0}catch(n){if(Ko(n))return!1;throw n}}function zo(t){let e={stdout:!1,stderr:!1},n=[];for(let r of["stdout","stderr"]){let o=t.streams[r],s=i=>{Ko(i)&&(e[r]=!0);let a=i?.code;try{t.note(`${new Date().toISOString()} urun compat: process.${r} died (${String(a??(i instanceof Error?i.message:i))}) \u2014 the terminal is gone; launcher output continues in this log only
|
|
19
19
|
`)}catch{}};o.on("error",s),n.push([o,s])}return{isDead:r=>e[r],uninstall:()=>{for(let[r,o]of n)r.off("error",o)}}}function Yo(t){let e=n=>{let r=n instanceof Error?`${n.stack??`${n.name}: ${n.message}`}`:String(n);try{t.appendSync(t.logPath,`${new Date().toISOString()} launcher crashed (in-process proxy): ${r}
|
|
20
20
|
`)}catch{}t.restore();try{t.writeStderr(`urun compat: the in-process proxy crashed (${n instanceof Error?n.message:String(n)}) \u2014 terminating the harness (its backend is gone); details: ${t.logPath}
|
|
@@ -25,8 +25,8 @@ Content-Type: text/plain\r
|
|
|
25
25
|
\r
|
|
26
26
|
${Va(n)}\r
|
|
27
27
|
`),t.destroy()}function Ja(t){return`${JSON.stringify({event:"realtime_upgrade",ts:new Date().toISOString(),...t})}
|
|
28
|
-
`}function Ka(t){de(process.stdout,t)}function za(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function qn(t){return t.audioSession?.audio.sessionId??void 0}function Gn(t,e){let n=new os.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,s,i)=>{if(new URL(o.url??"/","http://localhost").pathname!==Hn)return;r.add(s),s.on("close",()=>{r.delete(s)});let u=E("rtup"),d=za(o),l=v=>{(e.log??Ka)(Ja(v))};l({stage:"accept",conn:u,model:d}),s.resume();let p=new AbortController,m=null,c=v=>{m??=v,p.abort(new Error(m))},f=()=>c("client socket closed"),g=()=>c("client socket closed (half-close)"),_=v=>c(`client socket error: ${v.message}`);s.once("close",f),s.once("end",g),s.once("error",_);let y=()=>{s.removeListener("close",f),s.removeListener("end",g),s.removeListener("error",_)},h=v=>{l({stage:"close",conn:u,model:d,...v})},w=v=>{let b=v,x=b.session!=null&&typeof b.session.sendResponseCreate=="function",L=b.clients!=null&&typeof b.clients.createResponse=="function";if(!x&&!L)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((S,R)=>{n.handleUpgrade(o,s,i,C=>{try{new st({ws:C,binding:v,onClosed:(F,Y)=>{h({while:"live",reason:Y.reason})}}),l({stage:"admit",conn:u,model:d,...qn(v)===void 0?{}:{session:qn(v)}}),S()}catch(F){R(F)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o,p.signal).then(async v=>{if(p.signal.aborted){y();let b=qn(v);try{await v.release?.()}catch(x){h({while:"queued",...b===void 0?{}:{session:b},reason:m??"client socket closed",release_failed:x instanceof Error?x.message:String(x)});return}h({while:"queued",...b===void 0?{}:{session:b},reason:m??"client socket closed",released:!0}),s.destroy();return}y(),await w(v)}).catch(v=>{let b=v instanceof rs.SessionFailedError?Lt(v):null,x=null;if(v!==null&&typeof v=="object"&&"status"in v){let R=v.status;typeof R=="number"&&(x=R)}let L=x??b?.status??502,S=v instanceof Error?v.message:String(v);h({while:"queued",...p.signal.aborted?{reason:m??"client socket closed"}:{reason:S,status:L}}),s.destroyed||Wa(s,L,S)}).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 it=(t,e)=>Object.assign(new Error(e),{status:t});function Ya(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}async function Xa(t,e,n){if(!t.supportsAudio)throw it(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?it(404,a.message):a}if(!r)throw it(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 it(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 ss(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 it(401,"invalid local proxy api key")}return Xa(t,Ya(n),r)}}}var Q=class extends Error{};async function qt(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new Q(`request body exceeds the ${e}-byte limit`);let r=[],o=0;for await(let s of t){let i=s;if(o+=i.length,o>e)throw new Q(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function Ht(t,e){let n=t[Symbol.asyncIterator](),r=0,o=new ReadableStream({async pull(i){try{let a=await n.next();if(a.done){i.close();return}if(r+=a.value.length,r>e){i.error(new Q(`request body exceeds the ${e}-byte limit`));return}i.enqueue(a.value)}catch(a){i.error(a)}},cancel(){n.return?.()}}),s;try{let i=new Headers;for(let[d,l]of Object.entries(t.headers))if(typeof l=="string")i.set(d,l);else if(Array.isArray(l))for(let p of l)i.append(d,p);let a={method:"POST",headers:i,body:o,duplex:"half"};s=await new Request(`https://proxy.internal${t.url??"/"}`,a).formData()}catch(i){throw i instanceof Q?i:new Error(`malformed multipart body: ${String(i instanceof Error?i.message:i)}`)}return s}var is="/v1/images/generations",Gt="/v1/images/edits";function as(t){return t===is||t===Gt}var us="/v1/images/models";async function ds(t,e,n){if(t){if(t.model=ne({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}var $=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},Qa=16*1024*1024,ls=64*1024*1024;function cs(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 at(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 ps(t,e,n,r){let o=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},r),s=null;for await(let p of o){if(p.type==="error"){let m=p.error??{};throw new $(502,`image upstream error: ${String(m.message??"upstream reported an error")}`)}if(p.type==="response.completed"){s=p.response??null;break}}if(s===null)throw new $(502,"image upstream produced no terminal response \u2014 refusing to fabricate an image");let i=s.images;if(!Array.isArray(i)||i.length!==1||typeof i[0]!="string"||i[0].length===0)throw new $(502,`image upstream returned ${Array.isArray(i)?i.length:"a non-array images field"} images for the single-shot (n=1) contract, which produces exactly one \u2014 refusing to truncate or fabricate`);let a=[],u=i[0].startsWith("data:")?i[0].slice(i[0].indexOf(";base64,")+8):i[0],d=Buffer.from(u,"base64");if(cs(d)===null)throw new $(502,`image upstream returned a payload that is not a PNG/JPEG/WebP image (${d.length} bytes) \u2014 refusing to pass it through`);return a.push(u),{images:a}}function fs(t){return t instanceof $?t:t instanceof N?new $(404,t.message):new $(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function ms(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 at(r,new $(404,e===void 0?`no image-capable default model is configured for this caller (${o}) \u2014 name an image-capable \`model\``:`model "${e}" is not ${n==="edit"?"edit-capable":"generation-capable"} for this caller (${o}; capability is read from the catalog's modality metadata, not the serve function name)`)),!1}function gs(t,e){t&&(t.outputUnits=e.images.length)}function hs(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 Za={model:!0,prompt:!0,response_format:!0,n:!0};async function ys(t,e,n){let r=n.maxBodyBytes??ls;try{if(t.method!=="POST")throw new $(405,`${String(t.method)} is not supported on ${is}`);let o;try{o=JSON.parse((await qt(t,r)).toString("utf8")||"{}")}catch(p){throw p instanceof Q?new $(413,p.message):new $(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new $(400,"request body must be a JSON object");let s=o;for(let p of Object.keys(s))if(!Za[p])throw new $(400,`request field "${p}" 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.`,p);let i=s.prompt;if(typeof i!="string"||i.trim().length===0)throw new $(400,"prompt is required and must be a non-empty string","prompt");let a=s.model===void 0?void 0:s.model;if(a!==void 0&&typeof a!="string")throw new $(400,"model must be a string","model");await ds(n.turn,n.clients,a);let u=s.n??1;if(u!==1)throw new $(400,`n=${JSON.stringify(u)} is not supported: the native image request is single-shot and produces exactly one image per request (no batch support yet)`,"n");let d=s.response_format??"b64_json";if(d!=="b64_json")throw new $(400,`response_format ${JSON.stringify(d)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");if(n.stats?.request("images_generations",i.length,i.length),!await ms(n.imageModels,a,"generation",e))return;let l=[{type:"input_text",text:i}];try{let p=await ps(n.clients,a,l,n.turn?ie(n.turn):void 0);gs(n.turn,p),hs(e,p)}catch(p){throw fs(p)}}catch(o){if(o instanceof Q){at(e,new $(413,o.message));return}if(o instanceof $){at(e,o);return}throw o}}var eu={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function ws(t,e,n){let r=n.maxBodyBytes??ls,o=n.maxImagePartBytes??Qa;try{if(t.method!=="POST")throw new $(405,`${String(t.method)} is not supported on ${Gt}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new $(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await Ht(t,r)}catch(c){throw c instanceof Q?new $(413,c.message):new $(400,String(c instanceof Error?c.message:c))}let a=i.get("prompt");if(typeof a!="string"||a.trim().length===0)throw new $(400,"prompt is required and must be a non-empty string field","prompt");let u=i.get("model");if(u!==null&&typeof u!="string")throw new $(400,"model must be a string field","model");let d=u===null?void 0:u;await ds(n.turn,n.clients,d);let l=i.get("response_format")??"b64_json";if(l!=="b64_json")throw new $(400,`response_format ${JSON.stringify(l)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");for(let c of i.keys())if(!eu[c])throw new $(400,`form field "${c}" 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[]).`,c);let p=[];for(let c of["image","image[]"])for(let f of i.getAll(c)){if(!(f instanceof File))throw new $(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",c);if(f.size>o)throw new $(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,c);let g=new Uint8Array(await f.arrayBuffer()),_=cs(g);if(_===null)throw new $(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",c);p.push({bytes:g,mime:_})}if(p.length===0)throw new $(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",a.length,a.length),!await ms(n.imageModels,d,"edit",e))return;let m=[{type:"input_text",text:a}];for(let{bytes:c,mime:f}of p)m.push({type:"input_image",image_url:`data:${f};base64,${Buffer.from(c).toString("base64")}`});try{let c=await ps(n.clients,d,m,n.turn?ie(n.turn):void 0);gs(n.turn,c),hs(e,c)}catch(c){throw fs(c)}}catch(s){if(s instanceof Q){at(e,new $(413,s.message));return}if(s instanceof $){at(e,s);return}throw s}}var dt="/v1/audio/speech";function Ss(t){return t===dt}var _s=4096,tu=18e4,nu=5e3,ru=64*1024*1024,vs=8,bs=5,Wt="voice_applied",ou="X-Urun-Voice-Applied",P=class extends Error{status;param;retryAfterSeconds;constructor(e,n,r,o){super(n),this.status=e,this.param=r,this.retryAfterSeconds=o}};function Vn(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 Jt=class extends P{constructor(e){super(502,e)}};async function su(t,e,n){if(t){if(t.model=ne({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 iu(t,e){t&&(t.outputUnits=e/(24e3*2))}var au=2;function uu(t){for(let e=0;e+1<t.byteLength;e+=2)if(Math.abs(t.readInt16LE(e))>au)return!1;return!0}function du(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 ks=new WeakMap,Vt=new WeakMap;function lu(t,e){let n=Vt.get(t)??0;if(n>=vs)throw new P(503,`this model's speech queue is full \u2014 ${vs} requests are already admitted (one in-flight turn plus the queued depth) and each holds a pooled session turn; retry after ${bs}s`,void 0,bs);Vt.set(t,n+1);let r=ks.get(t)??Promise.resolve(),o,s=new Promise(i=>{o=i});return ks.set(t,s),(async()=>{await r;try{return await e()}finally{Vt.set(t,(Vt.get(t)??1)-1),o()}})()}async function cu(t,e,n,r){if(typeof t.evictSpeechSession!="function")throw new P(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned session after a failed turn \u2014 refusing loudly rather than handing the next request the failed turn\u2019s audio");try{await t.evictSpeechSession(e)}catch(i){if(!(i instanceof X))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 ut(t){return t instanceof P?t:t instanceof N?new P(404,t.message):new P(502,`speech upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function pu(t,e,n,r,o,s,i){return lu(e,async()=>{if(i?.()===!0)return null;let a=[],u=!1,d=0,l=!1,p=!1,m,c=new Promise(A=>{m=A}),f=()=>{l&&!p&&d>=o.quietRunFrames&&(p=!0,m())},g=e.onOutputAudio(A=>{let v=Buffer.from(A,"base64");if(v.byteLength===0)return;let b=uu(v);if(!u){if(b)return;u=!0}a.push(v),d=b?d+1:0,f()}),_=null,y,h,w;try{w=(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 v=new Promise(S=>{h=setTimeout(S,s.capMs)}),b=!1;for(;;){let S=w.next(),R=await Promise.race([S,v]);if(R===void 0){b=!0,S.catch(()=>{});break}if(R.done)break;let C=R.value;if(C.type==="error"){let F=C.error??{};throw await w.return?.(),new P(502,`speech upstream error: ${String(F.message??"upstream reported an error")}`)}if(C.type==="response.completed"){_=C.response??null,l=!0,f(),await w.return?.();break}}if(clearTimeout(h),b)throw new P(504,`the speech turn produced no terminal event within ${s.capMs}ms \u2014 the upstream stream never completed, so the pinned speech session is evicted (the next request opens a fresh one), the serialized lane is released and the caller can retry; a stuck turn must not hold every later request on this lane`);if(_===null)throw new P(502,"speech upstream produced no terminal response \u2014 refusing to fabricate audio");let x=!1;if(await Promise.race([c,new Promise(S=>{y=setTimeout(()=>{x=!0,S()},o.capMs)})]),y!==void 0&&clearTimeout(y),r.voice!==void 0){let S=_[Wt];if(typeof S!="string"||S!==r.voice){let R=`the runtime did not acknowledge the requested voice ${JSON.stringify(r.voice)} \u2014 the terminal response carried ${typeof S=="string"?`${Wt} ${JSON.stringify(S)}`:`no ${Wt} field`}; the speak-bridge companion is not deployed, and this lane never serves default-voice audio as the requested voice`;throw p?new Jt(R):new P(502,R)}}if(!u)throw new P(502,"the upstream produced no voice for this utterance \u2014 refusing to serve silence as success");if(x&&!p&&d<1)throw new P(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 L=d>0?a.slice(0,a.length-d):a;return{pcm:Buffer.concat(L),completed:_}}catch(A){throw A instanceof Jt||await cu(t,n,w,s.returnBoundMs),A}finally{y!==void 0&&clearTimeout(y),h!==void 0&&clearTimeout(h),g()}})}async function fu(t,e,n){let r;try{r=await t(e)}catch(o){throw ut(o)}return r?!0:(Vn(n,new P(404,e===void 0?"no speech-capable default model is configured for this caller (catalog task tts) \u2014 name a tts model":`model "${e}" is not speech-capable (catalog task is not tts) \u2014 this lane serves only text-to-speech models; chat models ride /v1/chat/completions`)),!1)}var mu={model:!0,input:!0,voice:!0,response_format:!0,stream_format:!0,speed:!0,instructions:!0},gu="wav, pcm";async function xs(t,e,n){let r=n.maxBodyBytes??ru,o=!1;e.once("close",()=>{o=!0});let s=()=>o||e.destroyed;try{if(t.method!=="POST")throw new P(405,`${String(t.method)} is not supported on ${dt}`);let i;try{i=JSON.parse((await qt(t,r)).toString("utf8")||"{}")}catch(w){throw w instanceof Q?new P(413,w.message):new P(400,"request body is not valid JSON")}if(i===null||typeof i!="object"||Array.isArray(i))throw new P(400,"request body must be a JSON object");let a=i;for(let w of Object.keys(a))if(!mu[w])throw new P(400,`request field "${w}" 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.`,w);let u=a.input;if(typeof u!="string"||u.trim().length===0)throw new P(400,"input is required and must be a non-empty string","input");if(u.length>_s)throw new P(400,`input is ${u.length} chars \u2014 over the ${_s}-char limit (one utterance; the backend bounds generation text-shaped, and an unbounded text is an unbounded synthesis)`,"input");let d=a.model===void 0?void 0:a.model;if(d!==void 0&&typeof d!="string")throw new P(400,"model must be a string","model");let l=a.voice;if(l!==void 0&&(typeof l!="string"||l.trim().length===0))throw new P(400,"voice must be a non-empty string","voice");if(typeof n.clients.speechVoices!="function")throw new P(501,`the speech lane needs a router-backed client (speechVoices) to resolve the voice set GET /v1/models publishes \u2014 refusing loudly rather than validating voices of ${dt} against a stale copy of the list`);let p;try{p=await n.clients.speechVoices(d)}catch(w){throw ut(w)}if(p.defect!==null)throw new P(500,`the voice set for this model cannot be resolved: ${p.defect} \u2014 fix the catalog row; the lane refuses to serve rather than validate against a set nobody vouched`);let m=typeof l=="string"?l:void 0;if(m!==void 0&&p.published.length>0&&!p.published.includes(m))throw new P(400,`voice ${JSON.stringify(m)} is not one of the voices this model publishes \u2014 accepted voices: ${p.published.join(", ")}`,"voice");let c=a.response_format??"wav";if(c!=="wav"&&c!=="pcm")throw new P(400,`response_format ${JSON.stringify(c)} is not supported: this lane serves the engine's raw 24 kHz mono PCM16, so the supported formats are ${gu} \u2014 and the lane will not label bytes with a format it did not encode`,"response_format");if(a.stream_format!==void 0&&typeof a.stream_format!="string")throw new P(400,"stream_format, when present, must be a string","stream_format");let f=a.speed;if(f!==void 0){if(typeof f!="number"||!Number.isFinite(f))throw new P(400,"speed, when present, must be a number","speed");if(f!==1)throw new P(400,`speed=${JSON.stringify(f)} is not supported: the backend exposes no speed knob (the qwen3 engine's speaking rate is fixed), so a non-default speed would be silently ignored \u2014 1 (the OpenAI default) is the only accepted value`,"speed")}let g=a.instructions;if(g!==void 0){if(typeof g!="string")throw new P(400,"instructions, when present, must be a string","instructions");if(g.trim().length>0)throw new P(400,"instructions are not supported: the backend fixes the voice style (instruct) at deploy time, so a per-request instruction would be silently ignored \u2014 the deployment speaks with its configured style","instructions")}await su(n.turn,n.clients,d);let _=Buffer.byteLength(JSON.stringify(a));if(n.stats?.request("audio_speech",_,_),!await fu(n.speechModels,d,e))return;if(typeof n.clients.openAudioOn!="function")throw new P(501,"a speech-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the same session identity as the response turn \u2014 refusing loudly rather than answering an utterance whose audio cannot be collected");if(typeof n.clients.speechSessionHandle!="function")throw new P(501,"a speech-capable model needs a router-backed client (speechSessionHandle) to resolve the speech lane's own pooled session \u2014 one no interactive binding shares, because the per-session audio bridge broadcasts every output frame to every subscriber \u2014 refusing loudly rather than collecting speech audio on a session a realtime/gemini lane can be speaking on");if(typeof n.clients.evictSpeechSession!="function")throw new P(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned speech session after a failed turn (a turn-cap timeout above all) \u2014 without it, the next request on the same model would silently collect the failed turn\u2019s still-emitting audio; refusing loudly rather than serving on an un-evictable speech slot");if(s())return;let y;try{y=await n.clients.speechSessionHandle(d)}catch(w){throw ut(w)}let h;try{h=await n.clients.openAudioOn(y)}catch(w){throw ut(w)}n.turn&&(n.turn.sessionId=h.sessionId);try{let w=await pu(n.clients,h,y,{model:d,text:u,...m!==void 0?{voice:m}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??tu,returnBoundMs:n.turnReturnBoundMs??nu},s);if(w===null||s())return;if(iu(n.turn,w.pcm.byteLength),e.statusCode=200,m===void 0){let A=w.completed[Wt];typeof A=="string"&&e.setHeader(ou,A)}c==="wav"?(e.setHeader("content-type","audio/wav"),e.end(Buffer.concat([du(w.pcm.byteLength),w.pcm]))):(e.setHeader("content-type","audio/pcm"),e.end(w.pcm))}catch(w){throw ut(w)}}catch(i){if(i instanceof Q){Vn(e,new P(413,i.message));return}if(i instanceof P){Vn(e,i);return}throw i}}var Kt="/v1/audio/transcriptions";function Rs(t){return t===Kt}var Es="/v1/audio/models",hu=16*1024*1024,yu=64*1024*1024,j=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}};function Wn(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 As(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 wu(t){if(!As(t))return null;let e=12,n=null,r=null;for(;e+8<=t.length;){let s=String.fromCharCode(t[e],t[e+1],t[e+2],t[e+3]),i=(t[e+4]|t[e+5]<<8|t[e+6]<<16|t[e+7]<<24)>>>0,a=e+8;if(s==="fmt "&&a+16<=t.length){let u=t[a+2]|t[a+3]<<8,d=(t[a+4]|t[a+5]<<8|t[a+6]<<16|t[a+7]<<24)>>>0,l=t[a+14]|t[a+15]<<8;n={sampleRate:d,channels:u,bitsPerSample:l}}else if(s==="data"){let u=t.length-a;r=Math.min(i===0||i===4294967295?u:i,u)}e=a+i+i%2}if(n===null||r===null||r===0||n.sampleRate===0||n.channels===0||n.bitsPerSample===0)return null;let o=r/(n.sampleRate*n.channels*(n.bitsPerSample/8));return!Number.isFinite(o)||o<0?null:{seconds:o,sampleRate:n.sampleRate,channels:n.channels,bitsPerSample:n.bitsPerSample}}async function _u(t,e,n){if(t){if(t.model=ne({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 vu(t,e){t&&(t.outputUnits=e.seconds)}async function bu(t,e,n,r,o){let s=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1,...r!==void 0?{language:r}:{}},o),i=[],a=null;for await(let u of s){if(i.push(u),u.type==="error"){let d=u.error??{},l=typeof d.type=="string"?d.type:"upstream_error",p=String(d.message??"upstream reported an error");throw new j(l==="invalid_request_error"?400:502,`transcription upstream error: ${p}`)}if(u.type==="response.completed"){a=u.response??null;break}}if(a===null)throw new j(502,"transcription upstream produced no terminal response \u2014 refusing to fabricate a transcript");return qe(i,a)}function Os(t){return t instanceof j?t:t instanceof N?new j(404,t.message):new j(502,`transcription upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function ku(t,e,n){let r;try{r=await t(e)}catch(o){throw Os(o)}return r?!0:(Wn(n,new j(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 Su=new Set(["file","model","language","response_format"]);async function Is(t,e,n){let r=n.maxBodyBytes??yu,o=n.maxAudioPartBytes??hu;try{if(t.method!=="POST")throw new j(405,`${String(t.method)} is not supported on ${Kt}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new j(400,`the transcriptions endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await Ht(t,r)}catch(y){throw y instanceof Q?new j(413,y.message):new j(400,String(y instanceof Error?y.message:y))}let a=new Map;for(let y of i.keys()){if(!Su.has(y))throw new j(400,`form field "${y}" is not implemented: the native transcription request carries the audio clip and a language hint only (prompt, temperature, timestamps and similar knobs have no native surface on this lane). Supported fields: file, model, language, response_format.`,y);a.set(y,(a.get(y)??0)+1)}for(let[y,h]of a)if(h>1)throw new j(400,`form field "${y}" 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`,y);let u=i.get("model");if(u!==null&&typeof u!="string")throw new j(400,"model must be a string field","model");let d=u===null?void 0:u,l=i.get("language");if(l!==null&&(typeof l!="string"||l.trim().length===0))throw new j(400,"language, when present, must be a non-empty string","language");let p=l===null?void 0:l,m=i.get("response_format")??"json";if(m!=="json")throw new j(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 c=i.get("file");if(!(c instanceof File))throw new j(400,"file must be an uploaded WAV: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths","file");if(c.size>o)throw new j(413,`file part exceeds the ${o}-byte limit (the serve protocol's MAX_AUDIO_BYTES)`,"file");let f=new Uint8Array(await c.arrayBuffer());if(!As(f))throw new j(400,"the audio payload is not a RIFF/WAVE container \u2014 this lane accepts WAV uploads only (detected by magic bytes, not the declared Content-Type); the runtime decodes PCM16 WAV and nothing else","file");let g=wu(f);if(g===null)throw new j(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 _u(n.turn,n.clients,d),n.stats?.request("audio_transcriptions",f.length,f.length),!await ku(n.transcriptionModels,d,e))return;let _=[{type:"input_audio",input_audio:{data:Buffer.from(f).toString("base64"),format:"wav"}}];try{let y=await bu(n.clients,d,_,p,n.turn?ie(n.turn):void 0);vu(n.turn,g),e.statusCode=200,e.setHeader("content-type","application/json"),e.end(JSON.stringify({text:y}))}catch(y){throw Os(y)}}catch(s){if(s instanceof Q){Wn(e,new j(413,s.message));return}if(s instanceof j){Wn(e,s);return}throw s}}var Ps="/v1/usage/requests",Ts=1e4,xu=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,xe=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},We=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}};var Cs=1024*1024;function Jn(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new xe(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new xe(`"${e}" must be an array of uuid strings`);if(n.length>Ts)throw new xe(`"${e}" carries ${n.length} ids; at most ${Ts} 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"||!xu.test(s))throw new xe(`"${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 $s(t){return Jn(t,"inference_ids")}function zt(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 We(`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 We(`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 nn=require("@urun-sh/core");var Kn="/partners/huggingface/v1/billing/requests";function Ru(t){return Jn(t,"requestIds")}function Eu(t){return t>=200&&t<=399}function Au(t){let e=[];for(let n of t)n.cost_nano_usd!==null&&Eu(n.http_status)&&e.push({requestId:n.inference_id,costNanoUsd:n.cost_nano_usd});return{requests:e.length>0?e:null}}async function Ms(t,e){let n=Ru(t),r=await e(n);return zt(r,n),Au(r)}var Ou={"/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 Iu(t){return Ou[t]??null}var Ds=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function Tu(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function Pu(t){return t==="surface"}function Cu(t){let e=t?.ledgerFailureReason;return Ds.includes(e)?e:"unknown"}var $u=["no_response_head","no_org","model_unresolved","no_model_named","queue_full"],Ls=["priced","unpriced"],Mu={written:Ls,already_written:Ls,skipped:$u,failed:Ds},up=Object.entries(Mu).flatMap(([t,e])=>e.map(n=>({outcome:t,reason:n})));function Lu(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function Uu(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 Nu=256,Us=4096,ct=12e4,Du=250,Fu=8e3,pt=[],Ns=new Set,ft=0,lt=!1;var Bu=new Set;function ju(){return ft===0&&pt.length===0}function qu(){if(ju()){lt=!1;for(let t of[...Bu])t()}}function Fs(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
|
|
29
|
-
`}function Hu(t){de(process.stdout,t)}function Bs(t,e){try{t(e)}catch{}}function js(t){let{record:e,ledger:n}=t;if(n===null)return;let r=n.report??Hu,o=Iu(e.path);if(o===null)return;let s=l=>{try{n.observe(l)}catch{}},i=(l,p)=>{s({outcome:"skipped",reason:l}),Bs(r,Fs("inference_ledger_skipped",{inference_id:e.inference_id,path:e.path,reason:l,detail:p}))};if(e.status===null){i("no_response_head","no head flushed, so the caller never received an Inference-Id and no status can be recorded honestly");return}if(e.org===null){i("no_org","the request never resolved a tenant, so there is no org to attribute the row to");return}let a=e.catalog;if(a.kind==="unresolved"){i("model_unresolved",a.reason);return}let u;if(a.kind==="catalog")u={model_id:a.model_id,identity_kind:"catalog_model",variant:a.variant};else{if(a.model===null){i("no_model_named","the request resolved to a caller-org app but named no model, so there is no app slug to record and `model_id` cannot be written honestly");return}u={model_id:a.model,identity_kind:"caller_org_app",variant:null}}let d={inference_id:e.inference_id,...u,task:o,http_status:e.status,started_at:Uu(e),completed_at:e.ts,billable_outcome:Lu(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(pt.length>=Us){i("queue_full",`${pt.length} ledger writes are already waiting (capacity ${Us}, ${ft} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}pt.push({entry:d,req:t.req,record:e,ledger:n,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),ft<Nu&&(ft+=1,Gu())}async function Gu(){try{for(;;){let t=Vu();if(t===void 0)return;Ns.add(t);try{await Wu(t)}finally{Ns.delete(t)}}}finally{ft-=1,qu()}}function Vu(){let t=Date.now();for(;;){let e=pt.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<ct)return e;Je(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 ${ct}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 Wu(t){let{entry:e,record:n,ledger:r}=t;for(;;){t.attempts+=1;try{let o=await r.write(t.req,e);Je(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=Tu(o);if(s!==null){Je(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=Cu(o),a=Date.now()-t.queuedAt;if(lt){Je(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(Pu(i)&&a<ct){if(await Ku(Ju(t.attempts)),lt||Date.now()-t.queuedAt>=ct){let u=Date.now()-t.queuedAt;Je(t,{outcome:"failed",reason:lt?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:lt?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:u,error:`this row's backoff outlived the ${ct}ms dwell bound (waited ${u}ms over ${t.attempts} attempt(s)) and was given up on rather than starting an attempt that could only finish past it. Last failure: ${o instanceof Error?o.message:String(o)}`});return}continue}Je(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 Ju(t){return Math.min(Fu,Du*2**Math.max(0,t-1))}function Ku(t){return new Promise(e=>{setTimeout(e,t).unref?.()})}function Je(t,e,n){if(t.settled)return;t.settled=!0;try{t.ledger.observe(e)}catch{}let{event:r,...o}=n;Bs(t.report,Fs(r,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...o}))}var Yn="x-urun-profile",mt="voice";var oe=class extends Error{status=400},zu=/(?:[.!?:;]|\n)[)\]"'\u2019\u201d\u00bb\u300d\u300b\u3011\uff09\uff3d]?\s*$/,Yu=/\s/;function Xu(t){return zu.test(t)}function zn(t,e,n){let r=t[e];if(r===void 0)return n;if(typeof r!="boolean")throw new oe(`stream_options.voice.${e} must be a boolean when present (got ${typeof r})`);return r}function qs(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 oe(`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 oe(`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 oe(`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 Hs(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 oe(`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 d=n.trim();if(d==="")throw new oe(`the ${Yn} header is empty \u2014 the only profile this lane accepts is "${mt}" (omit the header for the ordinary lane)`);if(d!==mt)throw new oe(`unknown profile "${d}" \u2014 the only profile this lane accepts is "${mt}"`);i=!0}if(s!==void 0){if(typeof s!="string"||s!==mt)throw new oe(`unknown profile ${JSON.stringify(s)} \u2014 the only profile this lane accepts is "${mt}"`);i=!0}if(!i)return null;if(e.stream!==!0)throw new oe("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 oe("stream_options.voice must be an object when present (flush_max_chars, flush_max_ms, sentence_flush, include_reasoning, metadata)");let u=a??{};return{flushMaxChars:qs(u,"flush_max_chars",120,{min:2,minReason:"a hard split at a bound below 2 cannot keep UTF-16 surrogate pairs (astral characters) intact"}),flushMaxMs:qs(u,"flush_max_ms",250,{max:2147483647}),sentenceFlush:zn(u,"sentence_flush",!0),includeReasoning:zn(u,"include_reasoning",!1),metadata:zn(u,"metadata",!1)}}var Yt=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&&Xu(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(Yu.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 Xn=Ee(require("crypto"),1),Qu="urun-runtime-key-v1";function Zu(t,e){if(!t)throw new Error("deriveOrgRuntimeKey requires a master secret");if(!e)throw new Error("deriveOrgRuntimeKey requires an orgId");return Xn.createHmac("sha256",t).update(`${Qu}|${e}`,"ascii").digest("base64url")}var ed="decision-intake",Xt=6e4,td=Xt/1e3+5,Gs=3,Qn=15;function nd(t,e,n=60,r=Math.floor(Date.now()/1e3)){if(!t)throw new Error("cannot mint a decision token without a secret");let o=d=>Buffer.from(d).toString("base64url"),s=o(JSON.stringify({alg:"HS256",typ:"JWT"})),i={tenant:e.tenant,app:e.app,fn:e.fn,...e.provider?{provider:e.provider}:{},session_id:"decision-intake",sub:`machine:${e.tenant}/${e.app}/${e.fn}`,aud:ed,iat:r,exp:r+Math.max(1,n)},a=o(JSON.stringify(i)),u=Xn.createHmac("sha256",t).update(`${s}.${a}`,"ascii").digest("base64url");return`${s}.${a}.${u}`}function rd(t){if(t===null||typeof t!="object")return!1;let e=t;return typeof e.runtime_id=="string"&&e.runtime_id!==""&&typeof e.ready=="boolean"&&typeof e.address=="string"&&e.address!==""&&typeof e.last_seen=="string"&&!Number.isNaN(Date.parse(e.last_seen))}async function od(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/runtimes?app=${encodeURIComponent(t.app)}&fn=${encodeURIComponent(t.fn)}`,r;try{r=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"},signal:AbortSignal.timeout(Xt)})}catch(u){throw(u instanceof Error?u.name:void 0)==="TimeoutError"?new pe(`runtime presence read for (app=${t.app} fn=${t.fn}) timed out after ${Xt}ms \u2014 the control plane is not answering; no replica can be discovered`):u}if(!r.ok)throw new Error(`runtime presence read failed: GET ${n} \u2192 ${r.status}`);let o=await r.json();if(!Array.isArray(o.runtimes))throw new Error(`runtime presence read returned no "runtimes" array (GET ${n})`);let s=[],i=[];for(let u of o.runtimes){if(!rd(u)){s.push(JSON.stringify(u));continue}i.push(u)}if(s.length>0)throw new Error(`runtime presence read for (app=${t.app} fn=${t.fn}) returned ${s.length} malformed presence row(s) \u2014 the control plane's runtimes schema drifted (runtime_id/address/ready/last_seen are all required): ${s.slice(0,3).join("; ")}`);let a=(t.nowMs??Date.now())/1e3;return i.filter(u=>u.ready===!0&&a-Date.parse(u.last_seen)/1e3<=Qn)}var sd=Qn*1e3,id=Qn*500,ad=256,Qt=class{constructor(e){this.opts=e;this.nowMs=e.nowMs??Date.now}opts;replicas=new Map;nowMs;presenceCache=new Map;get enabled(){return this.opts.secret!==""}async targetFor(e){let n=await this.opts.targetFor(e);return n.sharedApp===!0&&this.opts.shared===void 0?null:n}async decide(e,n){let r=await this.targetFor(n);if(r===null)throw new pe("the shared-endpoint lane keeps the session dial \u2014 the stateless direct path serves caller-org functions");let o=r.appSlug,s=r.fnName??this.opts.fnName,i=r.sharedApp===!0,a=i?this.opts.shared.apiKey:this.opts.apiKey,u=`${a}\0${o}\0${s}`,d=await this.ensureRows(u,a,o,s);if(d.length===0)throw new pe(`no ready runtime for (org app=${o} fn=${s}) \u2014 nothing is serving decisions right now`);let l=null;for(let p=0;p<Gs;p++){let m=this.pick(d);if(m===null)throw new pe(`every discovered runtime for app=${o} failed at the transport layer \u2014 no replica took the decision`);let c=i&&r.sharedOrgId?r.sharedOrgId:this.opts.orgId,f=nd(Zu(this.opts.secret,c),{tenant:this.opts.orgId,app:o,fn:s,...i&&r.sharedOrgId?{provider:r.sharedOrgId}:{}},td);try{return{...await this.post(m,e,f),target:{...r,fnName:s}}}catch(g){if(g instanceof Le)throw g;l=g;let _=this.replicas.get(m)??{outstanding:0,cooldownUntil:0};_.cooldownUntil=this.nowMs()+5e3,this.replicas.set(m,_);let y=this.presenceCache.get(u);y&&(y.rows=y.rows.filter(h=>h.address!==m),y.rows.length===0&&this.presenceCache.delete(u))}}throw new pe(`no runtime for app=${o} answered within ${Gs} attempts: ${String(l instanceof Error?l.message:l)}`)}async ensureRows(e,n,r,o){let s=this.presenceCache.get(e);if(s)if(this.nowMs()-s.fetchedAtMs>sd)this.presenceCache.delete(e);else return this.nowMs()-s.fetchedAtMs>id&&!s.refreshing&&(s.refreshing=!0,this.readPresence(n,r,o).then(a=>{this.presenceCache.set(e,{rows:a,fetchedAtMs:this.nowMs(),refreshing:!1})}).catch(()=>{s.refreshing=!1})),s.rows;let i=await this.readPresence(n,r,o);for(;this.presenceCache.size>=ad;){let a=this.presenceCache.keys().next();if(a.done)break;this.presenceCache.delete(a.value)}return this.presenceCache.set(e,{rows:i,fetchedAtMs:this.nowMs(),refreshing:!1}),i}readPresence(e,n,r){return od({apiUrl:this.opts.apiUrl,apiKey:e,app:n,fn:r,fetchImpl:this.opts.fetchImpl,nowMs:this.nowMs()})}pick(e){let n=this.nowMs(),r=null,o=1/0;for(let s of e){let i=this.replicas.get(s.address);if(i!==void 0&&i.cooldownUntil>n)continue;let a=i?.outstanding??0;a<o&&(r=s.address,o=a)}return r}async post(e,n,r){let o=this.replicas.get(e)??{outstanding:0,cooldownUntil:0};o.outstanding+=1,this.replicas.set(e,o);try{let s=await fetch(`${e.replace(/\/+$/,"")}/v1/decision`,{method:"POST",headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},body:JSON.stringify(n),signal:AbortSignal.timeout(this.opts.timeoutMs??Xt)}),i=null;try{i=await s.json()}catch{i={error:{message:`runtime answered ${s.status} with a non-JSON body`,type:"upstream_error"}}}if(s.status>=500)throw new Error(`runtime ${e} answered ${s.status}`);if(s.status>=400)throw new Le(s.status,i);return{status:s.status,body:i,completed:ud(i)}}finally{o.outstanding-=1}}};function ud(t){if(t===null||typeof t!="object")return null;let e=t,n=e.usage;if(n===null||typeof n!="object")return e;let r=n;return{...e,usage:{input_tokens:r.prompt_tokens??r.input_tokens,output_tokens:r.completion_tokens??r.output_tokens}}}var Le=class extends Error{constructor(n,r){super(`runtime answered ${n}`);this.status=n;this.body=r}status;body},pe=class extends Error{};function tr(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function dd(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 W(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function K(t,e,n,r="invalid_request_error",o){W(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function nr(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);W(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function fe(t,e,n,r){W(t,e,{type:"error",error:{type:n,message:r}})}function me(t,e,n){if(e==="anthropic"){W(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}W(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var ld=64*1024*1024,gt=class extends Error{};async function Vs(t,e=ld){let n=[],r=0;for await(let s of t){if(r+=s.length,r>e)throw new gt(`request body exceeds ${e} bytes`);n.push(s)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function Ue(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 Ks(t){let e=t?.usage;if(!e||typeof e!="object")return{kind:"absent"};let n=e,r=u=>typeof u=="number"&&Number.isFinite(u)&&u>=0?u:null,o=r(n.input_tokens),s=r(n.output_tokens),i=u=>u??0,a=r(n.total_tokens)??i(o)+i(s);if(i(o)>0||i(s)>0){let u={input_tokens:i(o),output_tokens:i(s),total_tokens:a};return o===null||s===null?{kind:"partial",usage:u}:{kind:"counts",usage:u}}return o===null||s===null?{kind:"absent"}:{kind:"zero",usage:{input_tokens:0,output_tokens:0,total_tokens:a}}}function Zt(t){let e=Ks(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function Ws(t){let e=Zt(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 cd(t,e){let n=Ks(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function pd(t){let e=t?.urun_timing;if(!e||typeof e!="object")return null;let n=e,r=u=>{let d=n[u];return typeof d=="number"&&Number.isSafeInteger(d)&&d>=0?d:null},o=r("queue_ms"),s=r("prefill_ms"),i=r("decode_ms"),a=r("total_ms");return o===null||s===null||i===null||a===null?null:{queue_ms:o,prefill_ms:s,decode_ms:i,total_ms:a}}function fd(t,e){let n=pd(e);n&&(t.timing=n)}function we(t,e){cd(t,e),fd(t,e)}async function rn(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 zs(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 md(t){if(Array.isArray(t))return t.map(e=>{let n=e.function;return!n||typeof n!="object"?e:{type:"function",name:n.name,description:n.description,parameters:n.parameters,strict:n.strict}})}var en=class extends Error{};function gd(t,e){return Array.isArray(e)?e.map(n=>{let r=n;if(r.type==="text")return{type:t==="assistant"?"output_text":"input_text",text:r.text};if(r.type==="image_url"){let o=r.image_url??{};return{type:"input_image",image_url:o.url,detail:o.detail}}throw new en(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function hd(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:gd(n.role,n.content)})}return e}function Zn(t){return Be(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 yd(t,e,n,r,o,s){let i=tt(s.id),a=t.stream===!0;if(s.model=ne(t),s.stream=a,await rn(s,e,typeof t.model=="string"?t.model:void 0),a){let y;try{y=await rt({body:t,clients:e,store:n,stats:o,responseId:i,turn:s})}catch(h){if(h instanceof N){me(r,"openai",h);return}if(h instanceof ae){K(r,h.status,h.message);return}throw h}tr(r);for await(let h of y.events)h.type==="error"&&(s.streamError=!0),h.type==="response.completed"&&we(s,h.response),r.write(`event: ${String(h.type??"message")}
|
|
28
|
+
`}function Ka(t){de(process.stdout,t)}function za(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function qn(t){return t.audioSession?.audio.sessionId??void 0}function Gn(t,e){let n=new os.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,s,i)=>{if(new URL(o.url??"/","http://localhost").pathname!==Hn)return;r.add(s),s.on("close",()=>{r.delete(s)});let u=E("rtup"),d=za(o),l=v=>{(e.log??Ka)(Ja(v))};l({stage:"accept",conn:u,model:d}),s.resume();let p=new AbortController,m=null,c=v=>{m??=v,p.abort(new Error(m))},f=()=>c("client socket closed"),g=()=>c("client socket closed (half-close)"),_=v=>c(`client socket error: ${v.message}`);s.once("close",f),s.once("end",g),s.once("error",_);let y=()=>{s.removeListener("close",f),s.removeListener("end",g),s.removeListener("error",_)},h=v=>{l({stage:"close",conn:u,model:d,...v})},w=v=>{let b=v,x=b.session!=null&&typeof b.session.sendResponseCreate=="function",L=b.clients!=null&&typeof b.clients.createResponse=="function";if(!x&&!L)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((S,R)=>{n.handleUpgrade(o,s,i,C=>{try{new st({ws:C,binding:v,onClosed:(F,Y)=>{h({while:"live",reason:Y.reason})}}),l({stage:"admit",conn:u,model:d,...qn(v)===void 0?{}:{session:qn(v)}}),S()}catch(F){R(F)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o,p.signal).then(async v=>{if(p.signal.aborted){y();let b=qn(v);try{await v.release?.()}catch(x){h({while:"queued",...b===void 0?{}:{session:b},reason:m??"client socket closed",release_failed:x instanceof Error?x.message:String(x)});return}h({while:"queued",...b===void 0?{}:{session:b},reason:m??"client socket closed",released:!0}),s.destroy();return}y(),await w(v)}).catch(v=>{let b=v instanceof rs.SessionFailedError?Lt(v):null,x=null;if(v!==null&&typeof v=="object"&&"status"in v){let R=v.status;typeof R=="number"&&(x=R)}let L=x??b?.status??502,S=v instanceof Error?v.message:String(v);h({while:"queued",...p.signal.aborted?{reason:m??"client socket closed"}:{reason:S,status:L}}),s.destroyed||Wa(s,L,S)}).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 it=(t,e)=>Object.assign(new Error(e),{status:t});function Ya(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}async function Xa(t,e,n){if(!t.supportsAudio)throw it(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?it(404,a.message):a}if(!r)throw it(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 it(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 ss(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 it(401,"invalid local proxy api key")}return Xa(t,Ya(n),r)}}}var Q=class extends Error{};async function qt(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new Q(`request body exceeds the ${e}-byte limit`);let r=[],o=0;for await(let s of t){let i=s;if(o+=i.length,o>e)throw new Q(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function Ht(t,e){let n=t[Symbol.asyncIterator](),r=0,o=new ReadableStream({async pull(i){try{let a=await n.next();if(a.done){i.close();return}if(r+=a.value.length,r>e){i.error(new Q(`request body exceeds the ${e}-byte limit`));return}i.enqueue(a.value)}catch(a){i.error(a)}},cancel(){n.return?.()}}),s;try{let i=new Headers;for(let[d,l]of Object.entries(t.headers))if(typeof l=="string")i.set(d,l);else if(Array.isArray(l))for(let p of l)i.append(d,p);let a={method:"POST",headers:i,body:o,duplex:"half"};s=await new Request(`https://proxy.internal${t.url??"/"}`,a).formData()}catch(i){throw i instanceof Q?i:new Error(`malformed multipart body: ${String(i instanceof Error?i.message:i)}`)}return s}var is="/v1/images/generations",Gt="/v1/images/edits";function as(t){return t===is||t===Gt}var us="/v1/images/models";async function ds(t,e,n){if(t){if(t.model=ne({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}var $=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},Qa=16*1024*1024,ls=64*1024*1024;function cs(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 at(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 ps(t,e,n,r){let o=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},r),s=null;for await(let p of o){if(p.type==="error"){let m=p.error??{};throw new $(502,`image upstream error: ${String(m.message??"upstream reported an error")}`)}if(p.type==="response.completed"){s=p.response??null;break}}if(s===null)throw new $(502,"image upstream produced no terminal response \u2014 refusing to fabricate an image");let i=s.images;if(!Array.isArray(i)||i.length!==1||typeof i[0]!="string"||i[0].length===0)throw new $(502,`image upstream returned ${Array.isArray(i)?i.length:"a non-array images field"} images for the single-shot (n=1) contract, which produces exactly one \u2014 refusing to truncate or fabricate`);let a=[],u=i[0].startsWith("data:")?i[0].slice(i[0].indexOf(";base64,")+8):i[0],d=Buffer.from(u,"base64");if(cs(d)===null)throw new $(502,`image upstream returned a payload that is not a PNG/JPEG/WebP image (${d.length} bytes) \u2014 refusing to pass it through`);return a.push(u),{images:a}}function fs(t){return t instanceof $?t:t instanceof N?new $(404,t.message):new $(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function ms(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 at(r,new $(404,e===void 0?`no image-capable default model is configured for this caller (${o}) \u2014 name an image-capable \`model\``:`model "${e}" is not ${n==="edit"?"edit-capable":"generation-capable"} for this caller (${o}; capability is read from the catalog's modality metadata, not the serve function name)`)),!1}function gs(t,e){t&&(t.outputUnits=e.images.length)}function hs(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 Za={model:!0,prompt:!0,response_format:!0,n:!0};async function ys(t,e,n){let r=n.maxBodyBytes??ls;try{if(t.method!=="POST")throw new $(405,`${String(t.method)} is not supported on ${is}`);let o;try{o=JSON.parse((await qt(t,r)).toString("utf8")||"{}")}catch(p){throw p instanceof Q?new $(413,p.message):new $(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new $(400,"request body must be a JSON object");let s=o;for(let p of Object.keys(s))if(!Za[p])throw new $(400,`request field "${p}" 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.`,p);let i=s.prompt;if(typeof i!="string"||i.trim().length===0)throw new $(400,"prompt is required and must be a non-empty string","prompt");let a=s.model===void 0?void 0:s.model;if(a!==void 0&&typeof a!="string")throw new $(400,"model must be a string","model");await ds(n.turn,n.clients,a);let u=s.n??1;if(u!==1)throw new $(400,`n=${JSON.stringify(u)} is not supported: the native image request is single-shot and produces exactly one image per request (no batch support yet)`,"n");let d=s.response_format??"b64_json";if(d!=="b64_json")throw new $(400,`response_format ${JSON.stringify(d)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");if(n.stats?.request("images_generations",i.length,i.length),!await ms(n.imageModels,a,"generation",e))return;let l=[{type:"input_text",text:i}];try{let p=await ps(n.clients,a,l,n.turn?ie(n.turn):void 0);gs(n.turn,p),hs(e,p)}catch(p){throw fs(p)}}catch(o){if(o instanceof Q){at(e,new $(413,o.message));return}if(o instanceof $){at(e,o);return}throw o}}var eu={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function ws(t,e,n){let r=n.maxBodyBytes??ls,o=n.maxImagePartBytes??Qa;try{if(t.method!=="POST")throw new $(405,`${String(t.method)} is not supported on ${Gt}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new $(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await Ht(t,r)}catch(c){throw c instanceof Q?new $(413,c.message):new $(400,String(c instanceof Error?c.message:c))}let a=i.get("prompt");if(typeof a!="string"||a.trim().length===0)throw new $(400,"prompt is required and must be a non-empty string field","prompt");let u=i.get("model");if(u!==null&&typeof u!="string")throw new $(400,"model must be a string field","model");let d=u===null?void 0:u;await ds(n.turn,n.clients,d);let l=i.get("response_format")??"b64_json";if(l!=="b64_json")throw new $(400,`response_format ${JSON.stringify(l)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");for(let c of i.keys())if(!eu[c])throw new $(400,`form field "${c}" 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[]).`,c);let p=[];for(let c of["image","image[]"])for(let f of i.getAll(c)){if(!(f instanceof File))throw new $(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",c);if(f.size>o)throw new $(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,c);let g=new Uint8Array(await f.arrayBuffer()),_=cs(g);if(_===null)throw new $(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",c);p.push({bytes:g,mime:_})}if(p.length===0)throw new $(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",a.length,a.length),!await ms(n.imageModels,d,"edit",e))return;let m=[{type:"input_text",text:a}];for(let{bytes:c,mime:f}of p)m.push({type:"input_image",image_url:`data:${f};base64,${Buffer.from(c).toString("base64")}`});try{let c=await ps(n.clients,d,m,n.turn?ie(n.turn):void 0);gs(n.turn,c),hs(e,c)}catch(c){throw fs(c)}}catch(s){if(s instanceof Q){at(e,new $(413,s.message));return}if(s instanceof $){at(e,s);return}throw s}}var dt="/v1/audio/speech";function Ss(t){return t===dt}var _s=4096,tu=18e4,nu=5e3,ru=64*1024*1024,vs=8,bs=5,Wt="voice_applied",ou="X-Urun-Voice-Applied",P=class extends Error{status;param;retryAfterSeconds;constructor(e,n,r,o){super(n),this.status=e,this.param=r,this.retryAfterSeconds=o}};function Vn(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 Jt=class extends P{constructor(e){super(502,e)}};async function su(t,e,n){if(t){if(t.model=ne({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 iu(t,e){if(!t)return;let n=e/(24e3*2);t.outputUnits=n,t.outputMinutes=n/60}var au=2;function uu(t){for(let e=0;e+1<t.byteLength;e+=2)if(Math.abs(t.readInt16LE(e))>au)return!1;return!0}function du(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 ks=new WeakMap,Vt=new WeakMap;function lu(t,e){let n=Vt.get(t)??0;if(n>=vs)throw new P(503,`this model's speech queue is full \u2014 ${vs} requests are already admitted (one in-flight turn plus the queued depth) and each holds a pooled session turn; retry after ${bs}s`,void 0,bs);Vt.set(t,n+1);let r=ks.get(t)??Promise.resolve(),o,s=new Promise(i=>{o=i});return ks.set(t,s),(async()=>{await r;try{return await e()}finally{Vt.set(t,(Vt.get(t)??1)-1),o()}})()}async function cu(t,e,n,r){if(typeof t.evictSpeechSession!="function")throw new P(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned session after a failed turn \u2014 refusing loudly rather than handing the next request the failed turn\u2019s audio");try{await t.evictSpeechSession(e)}catch(i){if(!(i instanceof X))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 ut(t){return t instanceof P?t:t instanceof N?new P(404,t.message):new P(502,`speech upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function pu(t,e,n,r,o,s,i){return lu(e,async()=>{if(i?.()===!0)return null;let a=[],u=!1,d=0,l=!1,p=!1,m,c=new Promise(A=>{m=A}),f=()=>{l&&!p&&d>=o.quietRunFrames&&(p=!0,m())},g=e.onOutputAudio(A=>{let v=Buffer.from(A,"base64");if(v.byteLength===0)return;let b=uu(v);if(!u){if(b)return;u=!0}a.push(v),d=b?d+1:0,f()}),_=null,y,h,w;try{w=(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 v=new Promise(S=>{h=setTimeout(S,s.capMs)}),b=!1;for(;;){let S=w.next(),R=await Promise.race([S,v]);if(R===void 0){b=!0,S.catch(()=>{});break}if(R.done)break;let C=R.value;if(C.type==="error"){let F=C.error??{};throw await w.return?.(),new P(502,`speech upstream error: ${String(F.message??"upstream reported an error")}`)}if(C.type==="response.completed"){_=C.response??null,l=!0,f(),await w.return?.();break}}if(clearTimeout(h),b)throw new P(504,`the speech turn produced no terminal event within ${s.capMs}ms \u2014 the upstream stream never completed, so the pinned speech session is evicted (the next request opens a fresh one), the serialized lane is released and the caller can retry; a stuck turn must not hold every later request on this lane`);if(_===null)throw new P(502,"speech upstream produced no terminal response \u2014 refusing to fabricate audio");let x=!1;if(await Promise.race([c,new Promise(S=>{y=setTimeout(()=>{x=!0,S()},o.capMs)})]),y!==void 0&&clearTimeout(y),r.voice!==void 0){let S=_[Wt];if(typeof S!="string"||S!==r.voice){let R=`the runtime did not acknowledge the requested voice ${JSON.stringify(r.voice)} \u2014 the terminal response carried ${typeof S=="string"?`${Wt} ${JSON.stringify(S)}`:`no ${Wt} field`}; the speak-bridge companion is not deployed, and this lane never serves default-voice audio as the requested voice`;throw p?new Jt(R):new P(502,R)}}if(!u)throw new P(502,"the upstream produced no voice for this utterance \u2014 refusing to serve silence as success");if(x&&!p&&d<1)throw new P(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 L=d>0?a.slice(0,a.length-d):a;return{pcm:Buffer.concat(L),completed:_}}catch(A){throw A instanceof Jt||await cu(t,n,w,s.returnBoundMs),A}finally{y!==void 0&&clearTimeout(y),h!==void 0&&clearTimeout(h),g()}})}async function fu(t,e,n){let r;try{r=await t(e)}catch(o){throw ut(o)}return r?!0:(Vn(n,new P(404,e===void 0?"no speech-capable default model is configured for this caller (catalog task tts) \u2014 name a tts model":`model "${e}" is not speech-capable (catalog task is not tts) \u2014 this lane serves only text-to-speech models; chat models ride /v1/chat/completions`)),!1)}var mu={model:!0,input:!0,voice:!0,response_format:!0,stream_format:!0,speed:!0,instructions:!0},gu="wav, pcm";async function xs(t,e,n){let r=n.maxBodyBytes??ru,o=!1;e.once("close",()=>{o=!0});let s=()=>o||e.destroyed;try{if(t.method!=="POST")throw new P(405,`${String(t.method)} is not supported on ${dt}`);let i;try{i=JSON.parse((await qt(t,r)).toString("utf8")||"{}")}catch(w){throw w instanceof Q?new P(413,w.message):new P(400,"request body is not valid JSON")}if(i===null||typeof i!="object"||Array.isArray(i))throw new P(400,"request body must be a JSON object");let a=i;for(let w of Object.keys(a))if(!mu[w])throw new P(400,`request field "${w}" 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.`,w);let u=a.input;if(typeof u!="string"||u.trim().length===0)throw new P(400,"input is required and must be a non-empty string","input");if(u.length>_s)throw new P(400,`input is ${u.length} chars \u2014 over the ${_s}-char limit (one utterance; the backend bounds generation text-shaped, and an unbounded text is an unbounded synthesis)`,"input");let d=a.model===void 0?void 0:a.model;if(d!==void 0&&typeof d!="string")throw new P(400,"model must be a string","model");let l=a.voice;if(l!==void 0&&(typeof l!="string"||l.trim().length===0))throw new P(400,"voice must be a non-empty string","voice");if(typeof n.clients.speechVoices!="function")throw new P(501,`the speech lane needs a router-backed client (speechVoices) to resolve the voice set GET /v1/models publishes \u2014 refusing loudly rather than validating voices of ${dt} against a stale copy of the list`);let p;try{p=await n.clients.speechVoices(d)}catch(w){throw ut(w)}if(p.defect!==null)throw new P(500,`the voice set for this model cannot be resolved: ${p.defect} \u2014 fix the catalog row; the lane refuses to serve rather than validate against a set nobody vouched`);let m=typeof l=="string"?l:void 0;if(m!==void 0&&p.published.length>0&&!p.published.includes(m))throw new P(400,`voice ${JSON.stringify(m)} is not one of the voices this model publishes \u2014 accepted voices: ${p.published.join(", ")}`,"voice");let c=a.response_format??"wav";if(c!=="wav"&&c!=="pcm")throw new P(400,`response_format ${JSON.stringify(c)} is not supported: this lane serves the engine's raw 24 kHz mono PCM16, so the supported formats are ${gu} \u2014 and the lane will not label bytes with a format it did not encode`,"response_format");if(a.stream_format!==void 0&&typeof a.stream_format!="string")throw new P(400,"stream_format, when present, must be a string","stream_format");let f=a.speed;if(f!==void 0){if(typeof f!="number"||!Number.isFinite(f))throw new P(400,"speed, when present, must be a number","speed");if(f!==1)throw new P(400,`speed=${JSON.stringify(f)} is not supported: the backend exposes no speed knob (the qwen3 engine's speaking rate is fixed), so a non-default speed would be silently ignored \u2014 1 (the OpenAI default) is the only accepted value`,"speed")}let g=a.instructions;if(g!==void 0){if(typeof g!="string")throw new P(400,"instructions, when present, must be a string","instructions");if(g.trim().length>0)throw new P(400,"instructions are not supported: the backend fixes the voice style (instruct) at deploy time, so a per-request instruction would be silently ignored \u2014 the deployment speaks with its configured style","instructions")}await su(n.turn,n.clients,d);let _=Buffer.byteLength(JSON.stringify(a));if(n.stats?.request("audio_speech",_,_),!await fu(n.speechModels,d,e))return;if(typeof n.clients.openAudioOn!="function")throw new P(501,"a speech-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the same session identity as the response turn \u2014 refusing loudly rather than answering an utterance whose audio cannot be collected");if(typeof n.clients.speechSessionHandle!="function")throw new P(501,"a speech-capable model needs a router-backed client (speechSessionHandle) to resolve the speech lane's own pooled session \u2014 one no interactive binding shares, because the per-session audio bridge broadcasts every output frame to every subscriber \u2014 refusing loudly rather than collecting speech audio on a session a realtime/gemini lane can be speaking on");if(typeof n.clients.evictSpeechSession!="function")throw new P(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned speech session after a failed turn (a turn-cap timeout above all) \u2014 without it, the next request on the same model would silently collect the failed turn\u2019s still-emitting audio; refusing loudly rather than serving on an un-evictable speech slot");if(s())return;let y;try{y=await n.clients.speechSessionHandle(d)}catch(w){throw ut(w)}let h;try{h=await n.clients.openAudioOn(y)}catch(w){throw ut(w)}n.turn&&(n.turn.sessionId=h.sessionId);try{let w=await pu(n.clients,h,y,{model:d,text:u,...m!==void 0?{voice:m}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??tu,returnBoundMs:n.turnReturnBoundMs??nu},s);if(w===null||s())return;if(iu(n.turn,w.pcm.byteLength),e.statusCode=200,m===void 0){let A=w.completed[Wt];typeof A=="string"&&e.setHeader(ou,A)}c==="wav"?(e.setHeader("content-type","audio/wav"),e.end(Buffer.concat([du(w.pcm.byteLength),w.pcm]))):(e.setHeader("content-type","audio/pcm"),e.end(w.pcm))}catch(w){throw ut(w)}}catch(i){if(i instanceof Q){Vn(e,new P(413,i.message));return}if(i instanceof P){Vn(e,i);return}throw i}}var Kt="/v1/audio/transcriptions";function Rs(t){return t===Kt}var Es="/v1/audio/models",hu=16*1024*1024,yu=64*1024*1024,j=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}};function Wn(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 As(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 wu(t){if(!As(t))return null;let e=12,n=null,r=null;for(;e+8<=t.length;){let s=String.fromCharCode(t[e],t[e+1],t[e+2],t[e+3]),i=(t[e+4]|t[e+5]<<8|t[e+6]<<16|t[e+7]<<24)>>>0,a=e+8;if(s==="fmt "&&a+16<=t.length){let u=t[a+2]|t[a+3]<<8,d=(t[a+4]|t[a+5]<<8|t[a+6]<<16|t[a+7]<<24)>>>0,l=t[a+14]|t[a+15]<<8;n={sampleRate:d,channels:u,bitsPerSample:l}}else if(s==="data"){let u=t.length-a;r=Math.min(i===0||i===4294967295?u:i,u)}e=a+i+i%2}if(n===null||r===null||r===0||n.sampleRate===0||n.channels===0||n.bitsPerSample===0)return null;let o=r/(n.sampleRate*n.channels*(n.bitsPerSample/8));return!Number.isFinite(o)||o<0?null:{seconds:o,sampleRate:n.sampleRate,channels:n.channels,bitsPerSample:n.bitsPerSample}}async function _u(t,e,n){if(t){if(t.model=ne({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 vu(t,e){t&&(t.outputUnits=e.seconds)}async function bu(t,e,n,r,o){let s=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1,...r!==void 0?{language:r}:{}},o),i=[],a=null;for await(let u of s){if(i.push(u),u.type==="error"){let d=u.error??{},l=typeof d.type=="string"?d.type:"upstream_error",p=String(d.message??"upstream reported an error");throw new j(l==="invalid_request_error"?400:502,`transcription upstream error: ${p}`)}if(u.type==="response.completed"){a=u.response??null;break}}if(a===null)throw new j(502,"transcription upstream produced no terminal response \u2014 refusing to fabricate a transcript");return qe(i,a)}function Os(t){return t instanceof j?t:t instanceof N?new j(404,t.message):new j(502,`transcription upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function ku(t,e,n){let r;try{r=await t(e)}catch(o){throw Os(o)}return r?!0:(Wn(n,new j(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 Su=new Set(["file","model","language","response_format"]);async function Is(t,e,n){let r=n.maxBodyBytes??yu,o=n.maxAudioPartBytes??hu;try{if(t.method!=="POST")throw new j(405,`${String(t.method)} is not supported on ${Kt}`);let s=String(t.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new j(400,`the transcriptions endpoint consumes multipart/form-data, got ${s||"none"}`);let i;try{i=await Ht(t,r)}catch(y){throw y instanceof Q?new j(413,y.message):new j(400,String(y instanceof Error?y.message:y))}let a=new Map;for(let y of i.keys()){if(!Su.has(y))throw new j(400,`form field "${y}" is not implemented: the native transcription request carries the audio clip and a language hint only (prompt, temperature, timestamps and similar knobs have no native surface on this lane). Supported fields: file, model, language, response_format.`,y);a.set(y,(a.get(y)??0)+1)}for(let[y,h]of a)if(h>1)throw new j(400,`form field "${y}" 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`,y);let u=i.get("model");if(u!==null&&typeof u!="string")throw new j(400,"model must be a string field","model");let d=u===null?void 0:u,l=i.get("language");if(l!==null&&(typeof l!="string"||l.trim().length===0))throw new j(400,"language, when present, must be a non-empty string","language");let p=l===null?void 0:l,m=i.get("response_format")??"json";if(m!=="json")throw new j(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 c=i.get("file");if(!(c instanceof File))throw new j(400,"file must be an uploaded WAV: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths","file");if(c.size>o)throw new j(413,`file part exceeds the ${o}-byte limit (the serve protocol's MAX_AUDIO_BYTES)`,"file");let f=new Uint8Array(await c.arrayBuffer());if(!As(f))throw new j(400,"the audio payload is not a RIFF/WAVE container \u2014 this lane accepts WAV uploads only (detected by magic bytes, not the declared Content-Type); the runtime decodes PCM16 WAV and nothing else","file");let g=wu(f);if(g===null)throw new j(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 _u(n.turn,n.clients,d),n.stats?.request("audio_transcriptions",f.length,f.length),!await ku(n.transcriptionModels,d,e))return;let _=[{type:"input_audio",input_audio:{data:Buffer.from(f).toString("base64"),format:"wav"}}];try{let y=await bu(n.clients,d,_,p,n.turn?ie(n.turn):void 0);vu(n.turn,g),e.statusCode=200,e.setHeader("content-type","application/json"),e.end(JSON.stringify({text:y}))}catch(y){throw Os(y)}}catch(s){if(s instanceof Q){Wn(e,new j(413,s.message));return}if(s instanceof j){Wn(e,s);return}throw s}}var Ps="/v1/usage/requests",Ts=1e4,xu=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,xe=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},We=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}};var Cs=1024*1024;function Jn(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new xe(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new xe(`"${e}" must be an array of uuid strings`);if(n.length>Ts)throw new xe(`"${e}" carries ${n.length} ids; at most ${Ts} 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"||!xu.test(s))throw new xe(`"${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 $s(t){return Jn(t,"inference_ids")}function zt(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 We(`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 We(`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 nn=require("@urun-sh/core");var Kn="/partners/huggingface/v1/billing/requests";function Ru(t){return Jn(t,"requestIds")}function Eu(t){return t>=200&&t<=399}function Au(t){let e=[];for(let n of t)n.cost_nano_usd!==null&&Eu(n.http_status)&&e.push({requestId:n.inference_id,costNanoUsd:n.cost_nano_usd});return{requests:e.length>0?e:null}}async function Ms(t,e){let n=Ru(t),r=await e(n);return zt(r,n),Au(r)}var Ou={"/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 Iu(t){return Ou[t]??null}var Ds=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function Tu(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function Pu(t){return t==="surface"}function Cu(t){let e=t?.ledgerFailureReason;return Ds.includes(e)?e:"unknown"}var $u=["no_response_head","no_org","model_unresolved","no_model_named","queue_full"],Ls=["priced","unpriced"],Mu={written:Ls,already_written:Ls,skipped:$u,failed:Ds},up=Object.entries(Mu).flatMap(([t,e])=>e.map(n=>({outcome:t,reason:n})));function Lu(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function Uu(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 Nu=256,Us=4096,ct=12e4,Du=250,Fu=8e3,pt=[],Ns=new Set,ft=0,lt=!1;var Bu=new Set;function ju(){return ft===0&&pt.length===0}function qu(){if(ju()){lt=!1;for(let t of[...Bu])t()}}function Fs(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
|
|
29
|
+
`}function Hu(t){de(process.stdout,t)}function Bs(t,e){try{t(e)}catch{}}function js(t){let{record:e,ledger:n}=t;if(n===null)return;let r=n.report??Hu,o=Iu(e.path);if(o===null)return;let s=l=>{try{n.observe(l)}catch{}},i=(l,p)=>{s({outcome:"skipped",reason:l}),Bs(r,Fs("inference_ledger_skipped",{inference_id:e.inference_id,path:e.path,reason:l,detail:p}))};if(e.status===null){i("no_response_head","no head flushed, so the caller never received an Inference-Id and no status can be recorded honestly");return}if(e.org===null){i("no_org","the request never resolved a tenant, so there is no org to attribute the row to");return}let a=e.catalog;if(a.kind==="unresolved"){i("model_unresolved",a.reason);return}let u;if(a.kind==="catalog")u={model_id:a.model_id,identity_kind:"catalog_model",variant:a.variant};else{if(a.model===null){i("no_model_named","the request resolved to a caller-org app but named no model, so there is no app slug to record and `model_id` cannot be written honestly");return}u={model_id:a.model,identity_kind:"caller_org_app",variant:null}}let d={inference_id:e.inference_id,...u,task:o,http_status:e.status,started_at:Uu(e),completed_at:e.ts,billable_outcome:Lu(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(pt.length>=Us){i("queue_full",`${pt.length} ledger writes are already waiting (capacity ${Us}, ${ft} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}pt.push({entry:d,req:t.req,record:e,ledger:n,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),ft<Nu&&(ft+=1,Gu())}async function Gu(){try{for(;;){let t=Vu();if(t===void 0)return;Ns.add(t);try{await Wu(t)}finally{Ns.delete(t)}}}finally{ft-=1,qu()}}function Vu(){let t=Date.now();for(;;){let e=pt.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<ct)return e;Je(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 ${ct}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 Wu(t){let{entry:e,record:n,ledger:r}=t;for(;;){t.attempts+=1;try{let o=await r.write(t.req,e);Je(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=Tu(o);if(s!==null){Je(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=Cu(o),a=Date.now()-t.queuedAt;if(lt){Je(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(Pu(i)&&a<ct){if(await Ku(Ju(t.attempts)),lt||Date.now()-t.queuedAt>=ct){let u=Date.now()-t.queuedAt;Je(t,{outcome:"failed",reason:lt?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:lt?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:u,error:`this row's backoff outlived the ${ct}ms dwell bound (waited ${u}ms over ${t.attempts} attempt(s)) and was given up on rather than starting an attempt that could only finish past it. Last failure: ${o instanceof Error?o.message:String(o)}`});return}continue}Je(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 Ju(t){return Math.min(Fu,Du*2**Math.max(0,t-1))}function Ku(t){return new Promise(e=>{setTimeout(e,t).unref?.()})}function Je(t,e,n){if(t.settled)return;t.settled=!0;try{t.ledger.observe(e)}catch{}let{event:r,...o}=n;Bs(t.report,Fs(r,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...o}))}var Yn="x-urun-profile",mt="voice";var oe=class extends Error{status=400},zu=/(?:[.!?:;]|\n)[)\]"'\u2019\u201d\u00bb\u300d\u300b\u3011\uff09\uff3d]?\s*$/,Yu=/\s/;function Xu(t){return zu.test(t)}function zn(t,e,n){let r=t[e];if(r===void 0)return n;if(typeof r!="boolean")throw new oe(`stream_options.voice.${e} must be a boolean when present (got ${typeof r})`);return r}function qs(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 oe(`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 oe(`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 oe(`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 Hs(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 oe(`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 d=n.trim();if(d==="")throw new oe(`the ${Yn} header is empty \u2014 the only profile this lane accepts is "${mt}" (omit the header for the ordinary lane)`);if(d!==mt)throw new oe(`unknown profile "${d}" \u2014 the only profile this lane accepts is "${mt}"`);i=!0}if(s!==void 0){if(typeof s!="string"||s!==mt)throw new oe(`unknown profile ${JSON.stringify(s)} \u2014 the only profile this lane accepts is "${mt}"`);i=!0}if(!i)return null;if(e.stream!==!0)throw new oe("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 oe("stream_options.voice must be an object when present (flush_max_chars, flush_max_ms, sentence_flush, include_reasoning, metadata)");let u=a??{};return{flushMaxChars:qs(u,"flush_max_chars",120,{min:2,minReason:"a hard split at a bound below 2 cannot keep UTF-16 surrogate pairs (astral characters) intact"}),flushMaxMs:qs(u,"flush_max_ms",250,{max:2147483647}),sentenceFlush:zn(u,"sentence_flush",!0),includeReasoning:zn(u,"include_reasoning",!1),metadata:zn(u,"metadata",!1)}}var Yt=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&&Xu(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(Yu.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 Xn=Ee(require("crypto"),1),Qu="urun-runtime-key-v1";function Zu(t,e){if(!t)throw new Error("deriveOrgRuntimeKey requires a master secret");if(!e)throw new Error("deriveOrgRuntimeKey requires an orgId");return Xn.createHmac("sha256",t).update(`${Qu}|${e}`,"ascii").digest("base64url")}var ed="decision-intake",Xt=6e4,td=Xt/1e3+5,Gs=3,Qn=15;function nd(t,e,n=60,r=Math.floor(Date.now()/1e3)){if(!t)throw new Error("cannot mint a decision token without a secret");let o=d=>Buffer.from(d).toString("base64url"),s=o(JSON.stringify({alg:"HS256",typ:"JWT"})),i={tenant:e.tenant,app:e.app,fn:e.fn,...e.provider?{provider:e.provider}:{},session_id:"decision-intake",sub:`machine:${e.tenant}/${e.app}/${e.fn}`,aud:ed,iat:r,exp:r+Math.max(1,n)},a=o(JSON.stringify(i)),u=Xn.createHmac("sha256",t).update(`${s}.${a}`,"ascii").digest("base64url");return`${s}.${a}.${u}`}function rd(t){if(t===null||typeof t!="object")return!1;let e=t;return typeof e.runtime_id=="string"&&e.runtime_id!==""&&typeof e.ready=="boolean"&&typeof e.address=="string"&&e.address!==""&&typeof e.last_seen=="string"&&!Number.isNaN(Date.parse(e.last_seen))}async function od(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/runtimes?app=${encodeURIComponent(t.app)}&fn=${encodeURIComponent(t.fn)}`,r;try{r=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"},signal:AbortSignal.timeout(Xt)})}catch(u){throw(u instanceof Error?u.name:void 0)==="TimeoutError"?new pe(`runtime presence read for (app=${t.app} fn=${t.fn}) timed out after ${Xt}ms \u2014 the control plane is not answering; no replica can be discovered`):u}if(!r.ok)throw new Error(`runtime presence read failed: GET ${n} \u2192 ${r.status}`);let o=await r.json();if(!Array.isArray(o.runtimes))throw new Error(`runtime presence read returned no "runtimes" array (GET ${n})`);let s=[],i=[];for(let u of o.runtimes){if(!rd(u)){s.push(JSON.stringify(u));continue}i.push(u)}if(s.length>0)throw new Error(`runtime presence read for (app=${t.app} fn=${t.fn}) returned ${s.length} malformed presence row(s) \u2014 the control plane's runtimes schema drifted (runtime_id/address/ready/last_seen are all required): ${s.slice(0,3).join("; ")}`);let a=(t.nowMs??Date.now())/1e3;return i.filter(u=>u.ready===!0&&a-Date.parse(u.last_seen)/1e3<=Qn)}var sd=Qn*1e3,id=Qn*500,ad=256,Qt=class{constructor(e){this.opts=e;this.nowMs=e.nowMs??Date.now}opts;replicas=new Map;nowMs;presenceCache=new Map;get enabled(){return this.opts.secret!==""}async targetFor(e){let n=await this.opts.targetFor(e);return n.sharedApp===!0&&this.opts.shared===void 0?null:n}async decide(e,n){let r=await this.targetFor(n);if(r===null)throw new pe("the shared-endpoint lane keeps the session dial \u2014 the stateless direct path serves caller-org functions");let o=r.appSlug,s=r.fnName??this.opts.fnName,i=r.sharedApp===!0,a=i?this.opts.shared.apiKey:this.opts.apiKey,u=`${a}\0${o}\0${s}`,d=await this.ensureRows(u,a,o,s);if(d.length===0)throw new pe(`no ready runtime for (org app=${o} fn=${s}) \u2014 nothing is serving decisions right now`);let l=null;for(let p=0;p<Gs;p++){let m=this.pick(d);if(m===null)throw new pe(`every discovered runtime for app=${o} failed at the transport layer \u2014 no replica took the decision`);let c=i&&r.sharedOrgId?r.sharedOrgId:this.opts.orgId,f=nd(Zu(this.opts.secret,c),{tenant:this.opts.orgId,app:o,fn:s,...i&&r.sharedOrgId?{provider:r.sharedOrgId}:{}},td);try{return{...await this.post(m,e,f),target:{...r,fnName:s}}}catch(g){if(g instanceof Le)throw g;l=g;let _=this.replicas.get(m)??{outstanding:0,cooldownUntil:0};_.cooldownUntil=this.nowMs()+5e3,this.replicas.set(m,_);let y=this.presenceCache.get(u);y&&(y.rows=y.rows.filter(h=>h.address!==m),y.rows.length===0&&this.presenceCache.delete(u))}}throw new pe(`no runtime for app=${o} answered within ${Gs} attempts: ${String(l instanceof Error?l.message:l)}`)}async ensureRows(e,n,r,o){let s=this.presenceCache.get(e);if(s)if(this.nowMs()-s.fetchedAtMs>sd)this.presenceCache.delete(e);else return this.nowMs()-s.fetchedAtMs>id&&!s.refreshing&&(s.refreshing=!0,this.readPresence(n,r,o).then(a=>{this.presenceCache.set(e,{rows:a,fetchedAtMs:this.nowMs(),refreshing:!1})}).catch(()=>{s.refreshing=!1})),s.rows;let i=await this.readPresence(n,r,o);for(;this.presenceCache.size>=ad;){let a=this.presenceCache.keys().next();if(a.done)break;this.presenceCache.delete(a.value)}return this.presenceCache.set(e,{rows:i,fetchedAtMs:this.nowMs(),refreshing:!1}),i}readPresence(e,n,r){return od({apiUrl:this.opts.apiUrl,apiKey:e,app:n,fn:r,fetchImpl:this.opts.fetchImpl,nowMs:this.nowMs()})}pick(e){let n=this.nowMs(),r=null,o=1/0;for(let s of e){let i=this.replicas.get(s.address);if(i!==void 0&&i.cooldownUntil>n)continue;let a=i?.outstanding??0;a<o&&(r=s.address,o=a)}return r}async post(e,n,r){let o=this.replicas.get(e)??{outstanding:0,cooldownUntil:0};o.outstanding+=1,this.replicas.set(e,o);try{let s=await fetch(`${e.replace(/\/+$/,"")}/v1/decision`,{method:"POST",headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},body:JSON.stringify(n),signal:AbortSignal.timeout(this.opts.timeoutMs??Xt)}),i=null;try{i=await s.json()}catch{i={error:{message:`runtime answered ${s.status} with a non-JSON body`,type:"upstream_error"}}}if(s.status>=500)throw new Error(`runtime ${e} answered ${s.status}`);if(s.status>=400)throw new Le(s.status,i);return{status:s.status,body:i,completed:ud(i)}}finally{o.outstanding-=1}}};function ud(t){if(t===null||typeof t!="object")return null;let e=t,n=e.usage;if(n===null||typeof n!="object")return e;let r=n;return{...e,usage:{input_tokens:r.prompt_tokens??r.input_tokens,output_tokens:r.completion_tokens??r.output_tokens}}}var Le=class extends Error{constructor(n,r){super(`runtime answered ${n}`);this.status=n;this.body=r}status;body},pe=class extends Error{};function tr(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function dd(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 W(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function K(t,e,n,r="invalid_request_error",o){W(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function nr(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);W(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function fe(t,e,n,r){W(t,e,{type:"error",error:{type:n,message:r}})}function me(t,e,n){if(e==="anthropic"){W(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}W(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var ld=64*1024*1024,gt=class extends Error{};async function Vs(t,e=ld){let n=[],r=0;for await(let s of t){if(r+=s.length,r>e)throw new gt(`request body exceeds ${e} bytes`);n.push(s)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function Ue(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 Ks(t){let e=t?.usage;if(!e||typeof e!="object")return{kind:"absent"};let n=e,r=u=>typeof u=="number"&&Number.isFinite(u)&&u>=0?u:null,o=r(n.input_tokens),s=r(n.output_tokens),i=u=>u??0,a=r(n.total_tokens)??i(o)+i(s);if(i(o)>0||i(s)>0){let u={input_tokens:i(o),output_tokens:i(s),total_tokens:a};return o===null||s===null?{kind:"partial",usage:u}:{kind:"counts",usage:u}}return o===null||s===null?{kind:"absent"}:{kind:"zero",usage:{input_tokens:0,output_tokens:0,total_tokens:a}}}function Zt(t){let e=Ks(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function Ws(t){let e=Zt(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 cd(t,e){let n=Ks(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function pd(t){let e=t?.urun_timing;if(!e||typeof e!="object")return null;let n=e,r=u=>{let d=n[u];return typeof d=="number"&&Number.isSafeInteger(d)&&d>=0?d:null},o=r("queue_ms"),s=r("prefill_ms"),i=r("decode_ms"),a=r("total_ms");return o===null||s===null||i===null||a===null?null:{queue_ms:o,prefill_ms:s,decode_ms:i,total_ms:a}}function fd(t,e){let n=pd(e);n&&(t.timing=n)}function we(t,e){cd(t,e),fd(t,e)}async function rn(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 zs(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 md(t){if(Array.isArray(t))return t.map(e=>{let n=e.function;return!n||typeof n!="object"?e:{type:"function",name:n.name,description:n.description,parameters:n.parameters,strict:n.strict}})}var en=class extends Error{};function gd(t,e){return Array.isArray(e)?e.map(n=>{let r=n;if(r.type==="text")return{type:t==="assistant"?"output_text":"input_text",text:r.text};if(r.type==="image_url"){let o=r.image_url??{};return{type:"input_image",image_url:o.url,detail:o.detail}}throw new en(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function hd(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:gd(n.role,n.content)})}return e}function Zn(t){return Be(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 yd(t,e,n,r,o,s){let i=tt(s.id),a=t.stream===!0;if(s.model=ne(t),s.stream=a,await rn(s,e,typeof t.model=="string"?t.model:void 0),a){let y;try{y=await rt({body:t,clients:e,store:n,stats:o,responseId:i,turn:s})}catch(h){if(h instanceof N){me(r,"openai",h);return}if(h instanceof ae){K(r,h.status,h.message);return}throw h}tr(r);for await(let h of y.events)h.type==="error"&&(s.streamError=!0),h.type==="response.completed"&&we(s,h.response),r.write(`event: ${String(h.type??"message")}
|
|
30
30
|
data: ${JSON.stringify(h)}
|
|
31
31
|
|
|
32
32
|
`);r.write(`data: [DONE]
|
|
@@ -58,7 +58,7 @@ data: ${JSON.stringify(h)}
|
|
|
58
58
|
`),n.end();return}finally{F?.flush(),F?.dispose()}}let f="",g="",_=[],y=new ke;for await(let S of r.track(c,p,o)){_.push(S),S.type==="response.output_text.delta"&&typeof S.delta=="string"&&(f+=S.delta),S.type==="response.reasoning_text.delta"&&typeof S.delta=="string"&&(g+=S.delta);let R=je(S);if(R&&y.add(R),S.type==="error"){nr(n,S);return}}let h=ce(_);we(o,h);let w=Zn(h),A=w.length>0?w:Zn({output:y.items()}),v=Me(m,A.length,f);if(v){K(n,502,$e(p,v),"upstream_error");return}let b=A.map(({index:S,...R})=>R),x={role:"assistant",content:f||null},L=g||zs(h);L&&(x.reasoning_content=L),b.length>0&&(x.tool_calls=b),W(n,200,{id:d,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:u,choices:[{index:0,message:x,finish_reason:b.length>0?"tool_calls":"stop"}],usage:Ws(h)})}var _d="reflex-latest";async function vd(t,e,n,r,o){if(t===null||typeof t!="object"||Array.isArray(t)){K(n,400,"systemone requires a JSON object body");return}delete t.key;let s=typeof t.model=="string"&&t.model?t.model:_d,i=ne({model:s});if(o.model=i,o.stream=!1,t.state===void 0||t.state===null){K(n,400,"systemone requires a state (string or JSON structure)");return}let a=t.questions;if(a===null||typeof a!="object"||Array.isArray(a)||Object.keys(a).length===0){K(n,400,"systemone requires a questions object (question id -> {kind, options, ...})");return}if(t.media!==void 0&&!Array.isArray(t.media)){K(n,400,"systemone media, when present, must be an array");return}if(r.model(i),await rn(o,e,s),e.stateless?.enabled){let c;try{c=await e.stateless.targetFor(s)}catch(f){if(f instanceof N){me(n,"openai",f);return}throw f}if(c!==null){try{let f=await e.stateless.decide(t,s);o.catalog=Qe(f.target),we(o,f.completed),W(n,200,f.body)}catch(f){if(f instanceof N){me(n,"openai",f);return}if(f instanceof Le){W(n,f.status,f.body);return}if(f instanceof pe){K(n,503,f.message,"upstream_error");return}throw f}return}}let u;try{u=await e.createResponse({model:s,input:[{role:"user",content:JSON.stringify(t)}],stream:!0},ie(o))}catch(c){if(c instanceof N){me(n,"openai",c);return}throw c}let d=[];try{for await(let c of r.track(u,i,o))if(d.push(c),c.type==="error"){nr(n,c);return}}catch(c){if(c instanceof N){me(n,"openai",c);return}throw c}let l=ce(d);if(l==null){K(n,502,"upstream produced no response.completed event","upstream_error");return}we(o,l);let p=qe(d,l),m;try{m=JSON.parse(p)}catch{K(n,502,"systemone answer was not valid JSON","upstream_error");return}if(m===null||typeof m!="object"||Array.isArray(m)){K(n,502,"systemone answer was not a JSON object","upstream_error");return}W(n,200,m)}function bd(t){return typeof t=="string"?t:Array.isArray(t)?t.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var ht=class extends Error{};function tn(t){return new ht(`unsupported anthropic content block type "${t}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function kd(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 Sd(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 ht(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function xd(t){if(typeof t=="string")return t;if(t==null)return"";if(!Array.isArray(t))throw tn(typeof t);let e="";for(let n of t){let r=String(n.type??"");if(r!=="text")throw tn(`tool_result > ${r}`);e+=String(n.text??"")}return e}function Rd(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 tn(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:xd(i.content)});else{if(a==="thinking"||a==="redacted_thinking")continue;throw tn(a)}}return s(),r}function er(t){return Be(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 Ed(t,e,n,r,o){let s=t.messages;if(!Array.isArray(s)){fe(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){fe(n,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(t.top_k!==void 0){fe(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(x=>typeof x!="string"))){fe(n,400,"invalid_request_error","stop_sequences must be an array of strings");return}let i=String(t.model??"urun"),a=ko(o.id),u=[],d;try{t.system&&u.push({role:"system",content:bd(t.system)});for(let x of s)u.push(...Rd(x));d=Sd(t.tool_choice)}catch(x){if(x instanceof ht){fe(n,400,"invalid_request_error",x.message);return}throw x}let l=ne(t);r.model(l),o.model=l,o.stream=t.stream===!0,await rn(o,e,typeof t.model=="string"?t.model:void 0);let p=Array.isArray(t.tools)&&t.tools.length>0,m;try{m=await e.createResponse({model:t.model,input:u,stream:!0,tools:kd(t.tools),tool_choice:d,temperature:t.temperature,max_output_tokens:t.max_tokens,top_p:t.top_p,stop:t.stop_sequences},ie(o))}catch(x){if(x instanceof N){me(n,"anthropic",x);return}throw x}let c=(x,L)=>{n.destroyed||n.write(`event: ${x}
|
|
59
59
|
data: ${JSON.stringify({type:x,...L})}
|
|
60
60
|
|
|
61
|
-
`)};if(t.stream===!0){let x=r.track(m,l,o);tr(n),c("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:i,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let L=0,S="",R=0,C="none",F=()=>C,Y=-1,te=-1,ue=new ke,_e=[],H=()=>{C!=="none"&&(C="none",c("content_block_stop",{index:Y}))},G=(B,ee)=>{H(),C=B,Y=R++,c("content_block_start",{index:Y,content_block:ee})};for await(let B of x){if(n.destroyed)break;_e.push(B);let ee=je(B);if(B.type==="response.reasoning_text.delta"&&typeof B.delta=="string")F()!=="thinking"&&G("thinking",{type:"thinking",thinking:""}),c("content_block_delta",{index:Y,delta:{type:"thinking_delta",thinking:B.delta}});else if(B.type==="response.output_text.delta"&&typeof B.delta=="string")F()!=="text"&&G("text",{type:"text",text:""}),L+=1,S+=B.delta,c("content_block_delta",{index:Y,delta:{type:"text_delta",text:B.delta}});else if(ee){let{call:ve}=ue.add(ee);(F()!=="tool"||ee.tool_index!==te)&&(te=ee.tool_index,G("tool",{type:"tool_use",id:ve.id??`call_${ee.tool_index}`,name:ve.name??"",input:{}})),ee.delta&&c("content_block_delta",{index:Y,delta:{type:"input_json_delta",partial_json:ee.delta}})}else if(B.type==="error"){o.streamError=!0,c("error",{error:{type:"api_error",message:JSON.stringify(B)}}),n.end();return}}if(n.destroyed)return;let Z=ce(_e),Re=Be(Z),T=Me(p,ue.size+Re.length,S);if(T){o.streamError=!0,c("error",{error:{type:"api_error",message:$e(l,T)}}),n.end();return}H();let U=[];if(ue.size===0)try{U=er(Z)}catch(B){o.streamError=!0,c("error",{error:{type:"api_error",message:String(B instanceof Error?B.message:B)}}),n.end();return}R===0&&U.length===0&&(G("text",{type:"text",text:""}),H()),U.forEach(B=>{let ee=R++;c("content_block_start",{index:ee,content_block:{...B,input:{}}}),c("content_block_delta",{index:ee,delta:{type:"input_json_delta",partial_json:JSON.stringify(B.input??{})}}),c("content_block_stop",{index:ee})});let z=ue.size>0||U.length>0,ge=Zt(Z);we(o,Z),c("message_delta",{delta:{stop_reason:z?"tool_use":"end_turn"},usage:ge?{input_tokens:ge.input_tokens,output_tokens:ge.output_tokens}:{output_tokens:L}}),c("message_stop",{}),n.end();return}let f="",g="",_=[],y=new ke;for await(let x of r.track(m,l,o)){_.push(x),x.type==="response.output_text.delta"&&typeof x.delta=="string"&&(f+=x.delta),x.type==="response.reasoning_text.delta"&&typeof x.delta=="string"&&(g+=x.delta);let L=je(x);if(L&&y.add(L),x.type==="error"){let S=x.error,R=S?.type==="invalid_request_error";fe(n,R?400:502,R?"invalid_request_error":"api_error",S?.message??JSON.stringify(x));return}if(n.destroyed)break}if(n.destroyed)return;let h=ce(_);we(o,h);let w;try{w=er(h),w.length===0&&y.size>0&&(w=er({output:y.items()}))}catch(x){fe(n,502,"api_error",String(x instanceof Error?x.message:x));return}let A=Me(p,w.length,f);if(A){fe(n,502,"api_error",$e(l,A));return}let v=[],b=g||zs(h);b&&v.push({type:"thinking",thinking:b}),(f||w.length===0)&&v.push({type:"text",text:f}),v.push(...w),W(n,200,{id:a,type:"message",role:"assistant",content:v,model:i,stop_reason:w.length>0?"tool_use":"end_turn",usage:Zt(h)??{input_tokens:0,output_tokens:0}})}function Ad(t){de(process.stdout,t)}function Od(t){let{clients:e,apiKey:n,identity:r,statusOf:o}=t,s=typeof e=="function"?e:()=>e,i=t.requestLog??Ad,a=t.observe,u=new Pe;return(d,l)=>{let p=new URL(d.url??"/","http://localhost"),m=p.pathname==="/v1/messages"||p.pathname.startsWith("/v1/messages/"),c=(h,w,A,v,b)=>m?fe(l,h,v,w):K(l,h,w,A,b),f=Sn(p.pathname),g=p.pathname.startsWith("/partners/"),_=f||g?{id:et(),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;_&&(l.setHeader(_o,_.id),l.once("close",()=>{let h=So({turn:_,method:d.method??"",path:p.pathname,res:l});i(xo(h)),a?.(h),js({record:h,ledger:t.ledger??null,req:d})}));let y=()=>vo(p.pathname,_);(async()=>{if(d.method==="GET"&&p.pathname==="/healthz"){W(l,200,{ok:!0});return}if(d.method==="GET"&&p.pathname==="/stats"){W(l,200,r?{identity:r,...u.snapshot()}:u.snapshot());return}if(n&&(d.headers.authorization??"")!==`Bearer ${n}`){c(401,"invalid local proxy api key","authentication_error","authentication_error");return}let h=p.pathname===Ps?"canonical":p.pathname===Kn?"huggingface":null;if(h){if(d.method!=="POST"){c(405,`${p.pathname} is a POST batch lookup`,"invalid_request_error","invalid_request_error");return}let b=t.usage;if(!b){c(501,`${p.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 x=b.authorize(d),L;try{L=await Vs(d,Cs)}catch(C){if(C instanceof gt){c(413,C.message,"request_too_large","request_too_large");return}c(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let S=C=>b.lookup(x,C),R;try{if(h==="canonical"){let C=$s(L),F=await S(C);zt(F,C),R={requests:F}}else R=await Ms(L,S)}catch(C){if(C instanceof xe){c(C.status,C.message,"invalid_request_error","invalid_request_error");return}throw C}W(l,200,R);return}if(p.pathname.startsWith("/partners/")){c(404,`no partner adapter for ${d.method} ${p.pathname} \u2014 the partner surface currently carries ${Kn}`,"invalid_request_error","not_found_error");return}let w=await s(d);if(_&&(_.org=w.tenant??null),d.method==="GET"&&p.pathname==="/v1/models"){if(p.searchParams.get("format")==="openrouter"){if(!w.openRouterModels){c(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}W(l,200,await w.openRouterModels());return}W(l,200,await w.listModels());return}if(d.method==="GET"&&p.pathname===us){if(!w.listImageModels){c(501,"GET /v1/images/models needs a router-backed client (listImageModels)","not_implemented","not_implemented_error");return}W(l,200,await w.listImageModels());return}if(as(p.pathname)){if(!w.supportsImages){c(501,`the image lanes need a router-backed client (supportsImages) to gate model capability for ${p.pathname}`,"not_implemented","not_implemented_error");return}let b={clients:w,imageModels:w.supportsImages,stats:u,turn:y()};p.pathname===Gt?await ws(d,l,b):await ys(d,l,b);return}if(d.method==="GET"&&p.pathname===Es){if(!w.listSpeechToTextModels){c(501,"GET /v1/audio/models needs a router-backed client (listSpeechToTextModels)","not_implemented","not_implemented_error");return}W(l,200,await w.listSpeechToTextModels());return}if(Rs(p.pathname)){if(!w.supportsTranscription){c(501,`the transcription lane needs a router-backed client (supportsTranscription) to gate model capability for ${Kt}`,"not_implemented","not_implemented_error");return}let b={clients:w,transcriptionModels:w.supportsTranscription,stats:u,turn:y()};await Is(d,l,b);return}if(Ss(p.pathname)){if(!w.supportsSpeech){c(501,`the speech lane needs a router-backed client (supportsSpeech) to gate model capability for ${dt}`,"not_implemented","not_implemented_error");return}let b={clients:w,speechModels:w.supportsSpeech,stats:u,turn:y()};await xs(d,l,b);return}if(d.method!=="POST"){c(404,`no route for ${d.method} ${p.pathname}`,"invalid_request_error","not_found_error");return}let A;try{A=await Vs(d)}catch(b){if(b instanceof gt){c(413,b.message,"request_too_large","request_too_large");return}c(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let v=Buffer.byteLength(JSON.stringify(A));if(p.pathname==="/v1/responses"){u.request("responses",v,v),await yd(A,w,nt(w),l,u,y());return}if(p.pathname==="/v1/chat/completions"){u.request("chat_completions",v,v),await wd(A,w,l,u,y(),d.headers[Yn]);return}if(p.pathname==="/v1/systemone"){u.request("systemone",v,v),await vd(A,w,l,u,y());return}if(p.pathname==="/v1/messages"){u.request("messages",v,v),await Ed(A,w,l,u,y());return}if(p.pathname==="/v1/messages/count_tokens"){u.request("count_tokens",v,v),fe(l,404,"not_found_error","count_tokens is not implemented by the uRun proxy: the serve transport exposes no token-count lane, and the proxy will not fabricate counts");return}c(404,`no route for POST ${p.pathname}`,"invalid_request_error","not_found_error")})().catch(h=>{let w=String(h instanceof Error?h.message:h);if(!l.headersSent){let A=o?.(h)??null;if(A){c(A.status,w,A.openaiType,A.anthropicType);return}if(h instanceof nn.SessionAllocationError){let v=Mt(h.code);if(v){let b=h.retryAfterSeconds??(v.status===429?30:void 0);b!==void 0&&l.setHeader("Retry-After",String(b)),c(v.status,In(v,h.code??void 0),v.openaiType,v.anthropicType,v.code);return}}if(h instanceof nn.SessionFailedError){let v=Lt(h);if(v){let b=v.status===429?30:void 0;b!==void 0&&l.setHeader("Retry-After",String(b)),c(v.status,In(v,h.status.sessionId?`session ${h.status.sessionId}`:void 0),v.openaiType,v.anthropicType,v.code);return}}if(h instanceof We){c(502,w,"upstream_error","api_error");return}c(500,w,"proxy_error","api_error","proxy_error");return}_&&(_.streamError=!0),l.destroyed||(m?l.write(`event: error
|
|
61
|
+
`)};if(t.stream===!0){let x=r.track(m,l,o);tr(n),c("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:i,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let L=0,S="",R=0,C="none",F=()=>C,Y=-1,te=-1,ue=new ke,_e=[],H=()=>{C!=="none"&&(C="none",c("content_block_stop",{index:Y}))},G=(B,ee)=>{H(),C=B,Y=R++,c("content_block_start",{index:Y,content_block:ee})};for await(let B of x){if(n.destroyed)break;_e.push(B);let ee=je(B);if(B.type==="response.reasoning_text.delta"&&typeof B.delta=="string")F()!=="thinking"&&G("thinking",{type:"thinking",thinking:""}),c("content_block_delta",{index:Y,delta:{type:"thinking_delta",thinking:B.delta}});else if(B.type==="response.output_text.delta"&&typeof B.delta=="string")F()!=="text"&&G("text",{type:"text",text:""}),L+=1,S+=B.delta,c("content_block_delta",{index:Y,delta:{type:"text_delta",text:B.delta}});else if(ee){let{call:ve}=ue.add(ee);(F()!=="tool"||ee.tool_index!==te)&&(te=ee.tool_index,G("tool",{type:"tool_use",id:ve.id??`call_${ee.tool_index}`,name:ve.name??"",input:{}})),ee.delta&&c("content_block_delta",{index:Y,delta:{type:"input_json_delta",partial_json:ee.delta}})}else if(B.type==="error"){o.streamError=!0,c("error",{error:{type:"api_error",message:JSON.stringify(B)}}),n.end();return}}if(n.destroyed)return;let Z=ce(_e),Re=Be(Z),T=Me(p,ue.size+Re.length,S);if(T){o.streamError=!0,c("error",{error:{type:"api_error",message:$e(l,T)}}),n.end();return}H();let U=[];if(ue.size===0)try{U=er(Z)}catch(B){o.streamError=!0,c("error",{error:{type:"api_error",message:String(B instanceof Error?B.message:B)}}),n.end();return}R===0&&U.length===0&&(G("text",{type:"text",text:""}),H()),U.forEach(B=>{let ee=R++;c("content_block_start",{index:ee,content_block:{...B,input:{}}}),c("content_block_delta",{index:ee,delta:{type:"input_json_delta",partial_json:JSON.stringify(B.input??{})}}),c("content_block_stop",{index:ee})});let z=ue.size>0||U.length>0,ge=Zt(Z);we(o,Z),c("message_delta",{delta:{stop_reason:z?"tool_use":"end_turn"},usage:ge?{input_tokens:ge.input_tokens,output_tokens:ge.output_tokens}:{output_tokens:L}}),c("message_stop",{}),n.end();return}let f="",g="",_=[],y=new ke;for await(let x of r.track(m,l,o)){_.push(x),x.type==="response.output_text.delta"&&typeof x.delta=="string"&&(f+=x.delta),x.type==="response.reasoning_text.delta"&&typeof x.delta=="string"&&(g+=x.delta);let L=je(x);if(L&&y.add(L),x.type==="error"){let S=x.error,R=S?.type==="invalid_request_error";fe(n,R?400:502,R?"invalid_request_error":"api_error",S?.message??JSON.stringify(x));return}if(n.destroyed)break}if(n.destroyed)return;let h=ce(_);we(o,h);let w;try{w=er(h),w.length===0&&y.size>0&&(w=er({output:y.items()}))}catch(x){fe(n,502,"api_error",String(x instanceof Error?x.message:x));return}let A=Me(p,w.length,f);if(A){fe(n,502,"api_error",$e(l,A));return}let v=[],b=g||zs(h);b&&v.push({type:"thinking",thinking:b}),(f||w.length===0)&&v.push({type:"text",text:f}),v.push(...w),W(n,200,{id:a,type:"message",role:"assistant",content:v,model:i,stop_reason:w.length>0?"tool_use":"end_turn",usage:Zt(h)??{input_tokens:0,output_tokens:0}})}function Ad(t){de(process.stdout,t)}function Od(t){let{clients:e,apiKey:n,identity:r,statusOf:o}=t,s=typeof e=="function"?e:()=>e,i=t.requestLog??Ad,a=t.observe,u=new Pe;return(d,l)=>{let p=new URL(d.url??"/","http://localhost"),m=p.pathname==="/v1/messages"||p.pathname.startsWith("/v1/messages/"),c=(h,w,A,v,b)=>m?fe(l,h,v,w):K(l,h,w,A,b),f=Sn(p.pathname),g=p.pathname.startsWith("/partners/"),_=f||g?{id:et(),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;_&&(l.setHeader(_o,_.id),l.once("close",()=>{let h=So({turn:_,method:d.method??"",path:p.pathname,res:l});i(xo(h)),a?.(h),js({record:h,ledger:t.ledger??null,req:d})}));let y=()=>vo(p.pathname,_);(async()=>{if(d.method==="GET"&&p.pathname==="/healthz"){W(l,200,{ok:!0});return}if(d.method==="GET"&&p.pathname==="/stats"){W(l,200,r?{identity:r,...u.snapshot()}:u.snapshot());return}if(n&&(d.headers.authorization??"")!==`Bearer ${n}`){c(401,"invalid local proxy api key","authentication_error","authentication_error");return}let h=p.pathname===Ps?"canonical":p.pathname===Kn?"huggingface":null;if(h){if(d.method!=="POST"){c(405,`${p.pathname} is a POST batch lookup`,"invalid_request_error","invalid_request_error");return}let b=t.usage;if(!b){c(501,`${p.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 x=b.authorize(d),L;try{L=await Vs(d,Cs)}catch(C){if(C instanceof gt){c(413,C.message,"request_too_large","request_too_large");return}c(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let S=C=>b.lookup(x,C),R;try{if(h==="canonical"){let C=$s(L),F=await S(C);zt(F,C),R={requests:F}}else R=await Ms(L,S)}catch(C){if(C instanceof xe){c(C.status,C.message,"invalid_request_error","invalid_request_error");return}throw C}W(l,200,R);return}if(p.pathname.startsWith("/partners/")){c(404,`no partner adapter for ${d.method} ${p.pathname} \u2014 the partner surface currently carries ${Kn}`,"invalid_request_error","not_found_error");return}let w=await s(d);if(_&&(_.org=w.tenant??null),d.method==="GET"&&p.pathname==="/v1/models"){if(p.searchParams.get("format")==="openrouter"){if(!w.openRouterModels){c(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}W(l,200,await w.openRouterModels());return}W(l,200,await w.listModels());return}if(d.method==="GET"&&p.pathname===us){if(!w.listImageModels){c(501,"GET /v1/images/models needs a router-backed client (listImageModels)","not_implemented","not_implemented_error");return}W(l,200,await w.listImageModels());return}if(as(p.pathname)){if(!w.supportsImages){c(501,`the image lanes need a router-backed client (supportsImages) to gate model capability for ${p.pathname}`,"not_implemented","not_implemented_error");return}let b={clients:w,imageModels:w.supportsImages,stats:u,turn:y()};p.pathname===Gt?await ws(d,l,b):await ys(d,l,b);return}if(d.method==="GET"&&p.pathname===Es){if(!w.listSpeechToTextModels){c(501,"GET /v1/audio/models needs a router-backed client (listSpeechToTextModels)","not_implemented","not_implemented_error");return}W(l,200,await w.listSpeechToTextModels());return}if(Rs(p.pathname)){if(!w.supportsTranscription){c(501,`the transcription lane needs a router-backed client (supportsTranscription) to gate model capability for ${Kt}`,"not_implemented","not_implemented_error");return}let b={clients:w,transcriptionModels:w.supportsTranscription,stats:u,turn:y()};await Is(d,l,b);return}if(Ss(p.pathname)){if(!w.supportsSpeech){c(501,`the speech lane needs a router-backed client (supportsSpeech) to gate model capability for ${dt}`,"not_implemented","not_implemented_error");return}let b={clients:w,speechModels:w.supportsSpeech,stats:u,turn:y()};await xs(d,l,b);return}if(d.method!=="POST"){c(404,`no route for ${d.method} ${p.pathname}`,"invalid_request_error","not_found_error");return}let A;try{A=await Vs(d)}catch(b){if(b instanceof gt){c(413,b.message,"request_too_large","request_too_large");return}c(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let v=Buffer.byteLength(JSON.stringify(A));if(p.pathname==="/v1/responses"){u.request("responses",v,v),await yd(A,w,nt(w),l,u,y());return}if(p.pathname==="/v1/chat/completions"){u.request("chat_completions",v,v),await wd(A,w,l,u,y(),d.headers[Yn]);return}if(p.pathname==="/v1/systemone"){u.request("systemone",v,v),await vd(A,w,l,u,y());return}if(p.pathname==="/v1/messages"){u.request("messages",v,v),await Ed(A,w,l,u,y());return}if(p.pathname==="/v1/messages/count_tokens"){u.request("count_tokens",v,v),fe(l,404,"not_found_error","count_tokens is not implemented by the uRun proxy: the serve transport exposes no token-count lane, and the proxy will not fabricate counts");return}c(404,`no route for POST ${p.pathname}`,"invalid_request_error","not_found_error")})().catch(h=>{let w=String(h instanceof Error?h.message:h);if(!l.headersSent){let A=o?.(h)??null;if(A){c(A.status,w,A.openaiType,A.anthropicType);return}if(h instanceof nn.SessionAllocationError){let v=Mt(h.code);if(v){let b=h.retryAfterSeconds??(v.status===429?30:void 0);b!==void 0&&l.setHeader("Retry-After",String(b)),c(v.status,In(v,h.code??void 0),v.openaiType,v.anthropicType,v.code);return}}if(h instanceof nn.SessionFailedError){let v=Lt(h);if(v){let b=v.status===429?30:void 0;b!==void 0&&l.setHeader("Retry-After",String(b)),c(v.status,In(v,h.status.sessionId?`session ${h.status.sessionId}`:void 0),v.openaiType,v.anthropicType,v.code);return}}if(h instanceof We){c(502,w,"upstream_error","api_error");return}c(500,w,"proxy_error","api_error","proxy_error");return}_&&(_.streamError=!0),l.destroyed||(m?l.write(`event: error
|
|
62
62
|
data: ${JSON.stringify({type:"error",error:{type:"api_error",message:w}})}
|
|
63
63
|
|
|
64
64
|
`):l.write(`data: ${JSON.stringify({error:{message:w,type:"upstream_error"}})}
|
package/dist/proxy/cli.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{a as q,b as Y}from"../chunk-VRCCLQ5P.js";import"../chunk-K23AZPI4.js";import{A as M,Q as F,S as J,f as h,g as U,h as $,i as C,j as N,k as L,l as I,m as O,n as x,o as G,p as m,r as _,s as P,t as D,u as W,v as A,w as B,x as v,y as K,z as j}from"../chunk-
|
|
2
|
+
import{a as q,b as Y}from"../chunk-VRCCLQ5P.js";import"../chunk-K23AZPI4.js";import{A as M,Q as F,S as J,f as h,g as U,h as $,i as C,j as N,k as L,l as I,m as O,n as x,o as G,p as m,r as _,s as P,t as D,u as W,v as A,w as B,x as v,y as K,z as j}from"../chunk-U7EJSC2N.js";import"../chunk-76BZLCGT.js";import"../chunk-TMQJY56F.js";import"../chunk-3JWYIKHM.js";import{g as T}from"../chunk-6M5JK4YC.js";import"../chunk-7V2JKPIG.js";import{c as w}from"../chunk-YSFSRI3D.js";w();import{spawn as X}from"child_process";import{appendFileSync as k,createWriteStream as ue,existsSync as ce,mkdirSync as Z}from"fs";import ee from"path";import t from"process";w();import{ensureNodeWebRTC as se,nodeWebRTCUnavailableReason as ae}from"@urun-sh/core/internal";var le={ensureWebRTC:se,webrtcUnavailableReason:ae,audioBackend:T};async function H(e=le){try{await e.ensureWebRTC()}catch(r){return{ok:!1,reason:`the WebRTC bootstrap threw: ${String(r)}`}}let o=e.webrtcUnavailableReason();if(o)return{ok:!1,reason:`${o} \u2014 this artifact would join every session doc/KV-only and every token stream would error. Either "werift" is missing from the deployment directory (npm lane), or it was marked --external in a single-file build, where nothing can ever resolve it (bunfs: "Cannot find package 'werift' from '/$bunfs/root/\u2026'").`};try{(await e.audioBackend()).createSource()}catch(r){return{ok:!1,reason:`the werift/opus audio backend could not be constructed: ${String(r)}`}}return{ok:!0,reason:null}}function de(e,o){let r=(e.URUN_CATALOG_URL??"").trim(),u=r?_(r,"URUN_CATALOG_URL"):"",i=(e.URUN_CATALOG_ANON_KEY??"").trim();return!u||!i?(o(`urun compat: catalog oracle off (URUN_CATALOG_URL/URUN_CATALOG_ANON_KEY unset) \u2014 an undeployed uRun CATALOG model name falls through to the default app instead of a loud 404; deployed apps still resolve exactly, and /stats.models records every mapping
|
|
3
3
|
`),null):{catalogUrl:u,anonKey:i}}function pe(e){return q({baseUrl:e.baseUrl,orgId:e.orgId,defaultApp:e.appId,fnName:e.fnName,apiUrl:e.apiUrl,auth:e.auth,catalog:de(t.env,o=>t.stderr.write(o)),statelessDirect:te(e)})}function te(e){return e.decisionSecret!==""&&e.auth.lane==="api-key"}function fe(e,o){if(te(e))return t.stderr.write(`urun compat: stateless direct path ENABLED (POST /v1/decision answered by the serving pods)
|
|
4
4
|
`),new F({targetFor:r=>o.targetFor(r),apiUrl:e.apiUrl,fnName:e.fnName,apiKey:e.auth.apiKey,orgId:e.orgId,secret:e.decisionSecret})}async function V(e,o,r){let u=X(e,o,{stdio:"inherit",env:r});return new Promise(i=>{u.once("exit",s=>i(s??1)),u.once("error",s=>{t.stderr.write(`failed to launch ${e}: ${String(s)}
|
|
5
5
|
`),i(127)})})}function z(){return I(t.env,import.meta.url,ce)}function re(e){for(let[o,r]of Object.entries(U(e)))t.stdout.write(`export ${o}=${r}
|