@urun-sh/openai 0.6.22 → 0.6.23
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-4W3ZPIR7.js → chunk-ERRG6R4T.js} +14 -14
- package/dist/{chunk-HCAKF2SQ.js → chunk-FHGSREBO.js} +1 -1
- package/dist/chunk-JOOR4RUT.js +2 -0
- package/dist/hosted/bin.cjs +13 -13
- package/dist/hosted/bin.js +1 -1
- package/dist/hosted/index.cjs +3 -3
- package/dist/hosted/index.d.cts +9 -0
- package/dist/hosted/index.d.ts +5 -0
- package/dist/hosted/index.js +1 -1
- package/dist/pi-extension/standalone.cjs +2 -2
- package/dist/proxy/cli.cjs +3 -3
- package/dist/proxy/cli.js +1 -1
- package/dist/proxy/index.cjs +1 -1
- package/dist/proxy/index.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-KSC5Q6PZ.js +0 -2
package/dist/proxy/index.cjs
CHANGED
|
@@ -16,7 +16,7 @@ Content-Type: text/plain\r
|
|
|
16
16
|
\r
|
|
17
17
|
${ii(n)}\r
|
|
18
18
|
`),t.destroy()}function ai(t){return`${JSON.stringify({event:"realtime_upgrade",ts:new Date().toISOString(),...t})}
|
|
19
|
-
`}function ui(t){Se(process.stdout,t)}function di(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function Zt(t){return t.audioSession?.audio.sessionId??void 0}function Ke(t,e){let n=new rr.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,i,s)=>{if(new URL(o.url??"/","http://localhost").pathname!==We)return;r.add(i),i.on("close",()=>{r.delete(i)});let d=x("rtup"),u=di(o),l=_=>{(e.log??ui)(ai(_))};l({stage:"accept",conn:d,model:u}),i.resume();let c=new AbortController,y=null,p=_=>{y??=_,c.abort(new Error(y))},k=()=>p("client socket closed"),E=()=>p("client socket closed (half-close)"),S=_=>p(`client socket error: ${_.message}`);i.once("close",k),i.once("end",E),i.once("error",S);let f=()=>{i.removeListener("close",k),i.removeListener("end",E),i.removeListener("error",S)},h=_=>{l({stage:"close",conn:d,model:u,..._})},g=_=>{let m=_,v=m.session!=null&&typeof m.session.sendResponseCreate=="function",P=m.clients!=null&&typeof m.clients.createResponse=="function";if(!v&&!P)throw new Error("resolver returned a binding without an upstream turn seam (session.sendResponseCreate or clients.createResponse required) \u2014 refusing the upgrade LOUD");return new Promise((b,I)=>{n.handleUpgrade(o,i,s,C=>{try{new xe({ws:C,binding:_,onClosed:(D,W)=>{h({while:"live",reason:W.reason})}}),l({stage:"admit",conn:d,model:u,...Zt(_)===void 0?{}:{session:Zt(_)}}),b()}catch(D){I(D)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o,c.signal).then(async _=>{if(c.signal.aborted){f();let m=Zt(_);try{await _.release?.()}catch(v){h({while:"queued",...m===void 0?{}:{session:m},reason:y??"client socket closed",release_failed:v instanceof Error?v.message:String(v)});return}h({while:"queued",...m===void 0?{}:{session:m},reason:y??"client socket closed",released:!0}),i.destroy();return}f(),await g(_)}).catch(_=>{let m=_ instanceof nr.SessionFailedError?_t(_):null,v=null;if(_!==null&&typeof _=="object"&&"status"in _){let I=_.status;typeof I=="number"&&(v=I)}let P=v??m?.status??502,b=_ instanceof Error?_.message:String(_);h({while:"queued",...c.signal.aborted?{reason:y??"client socket closed"}:{reason:b,status:P}}),i.destroyed||si(i,P,b)}).finally(()=>{r.delete(i)})}),async()=>{let o=Date.now()+5e3;for(;r.size>0&&Date.now()<o;)await new Promise(i=>setTimeout(i,25));await new Promise(i=>n.close(()=>i()))}}var se=(t,e)=>Object.assign(new Error(e),{status:t});function en(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function li(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("intent")?.trim();return n===""?void 0:n}var ci='"transcription" (a transcription session), or no intent at all (the realtime voice session)';async function tn(t,e,n,r){if(!t.supportsAudio)throw se(501,"the realtime lane needs a router-backed client (supportsAudio) to gate audio modality \u2014 hand-built clients without the catalog oracle are refused, never allowed silently");let o;try{o=await t.supportsAudio(e)}catch(u){throw u instanceof N?se(404,u.message):u}if(!o)throw se(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 se(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 i=!1;if(t.supportsTranscription)try{i=await t.supportsTranscription(e)}catch(u){throw u instanceof N?se(404,u.message):u}if(i&&!t.openTurnsOn)throw se(501,"a transcription-capable (stt) model needs a router-backed client (openTurnsOn) to open the engine\u2019s own turn/transcript lane (the stt named stream) pinned to the connection\u2019s pooled session \u2014 refusing loudly rather than binding a transcript-less transcription session");if(i&&t.transcriptStreamFor){let u;try{u=await t.transcriptStreamFor(e)}catch(l){throw l instanceof N?se(404,l.message):l}if(u!=="stt")throw se(501,`model ${JSON.stringify(e??"(default)")} overrides engine_args.transcript_stream with ${JSON.stringify(u)} \u2014 this lane consumes the engine\u2019s own transcripts from the platform\u2019s default "${"stt"}" stream and refuses loudly rather than listen on a stream the engine does not write`)}if(r!==void 0&&r!=="transcription")throw se(400,`intent ${JSON.stringify(r)} is not supported on this realtime lane \u2014 accepted values: ${ci}`);if(r==="transcription"&&!i)throw se(400,`model ${JSON.stringify(e??"(default)")} cannot serve the transcription intent \u2014 the transcription intent needs an stt model (the catalog task is not stt)`);let s=await t.sessionHandle(e),a={audio:await t.openAudioOn(s,n)},d={clients:{createResponse:async u=>await t.createResponseOn(s,u)},voice:o,audioSession:a};return i&&(d.turns=u=>t.openTurnsOn(s,u)),r==="transcription"&&(d.intent="transcription"),e!==void 0&&(d.model=e),d}function St(t,e){return{resolve:async(n,r)=>{if(e!==void 0){let o=n.headers.authorization,i=typeof o=="string"?o.trim():"",s=i.startsWith("Bearer ")?i.slice(7).trim():"";if(s===""||s!==e)throw se(401,"invalid local proxy api key")}return tn(t,en(n),r,li(n))}}}var xt=class extends Error{code;status;retryAfterSeconds;constructor(e,n,r,o){super(o),this.name="DecisionStreamFailure",this.code=e,this.status=n,this.retryAfterSeconds=r,Object.setPrototypeOf(this,new.target.prototype)}};var ze=class extends Error{constructor(){super("no ready decision slot on this proxy \u2014 the request was refused fast, not fenced; retry shortly (an out-of-band slot add was submitted)"),this.name="DecisionCapacityError"}};var K=class extends Error{};async function Rt(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new K(`request body exceeds the ${e}-byte limit`);let r=[],o=0;for await(let i of t){let s=i;if(o+=s.length,o>e)throw new K(`request body exceeds the ${e}-byte limit`);r.push(s)}return Buffer.concat(r)}async function Et(t,e){let n=t[Symbol.asyncIterator](),r=0,o=new ReadableStream({async pull(s){try{let a=await n.next();if(a.done){s.close();return}if(r+=a.value.length,r>e){s.error(new K(`request body exceeds the ${e}-byte limit`));return}s.enqueue(a.value)}catch(a){s.error(a)}},cancel(){n.return?.()}}),i;try{let s=new Headers;for(let[u,l]of Object.entries(t.headers))if(typeof l=="string")s.set(u,l);else if(Array.isArray(l))for(let c of l)s.append(u,c);let a={method:"POST",headers:s,body:o,duplex:"half"};i=await new Request(`https://proxy.internal${t.url??"/"}`,a).formData()}catch(s){throw s instanceof K?s:new Error(`malformed multipart body: ${String(s instanceof Error?s.message:s)}`)}return i}var or="/v1/images/generations",At="/v1/images/edits";function ir(t){return t===or||t===At}var sr="/v1/images/models";async function ar(t,e,n){if(t){if(t.model=ee({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}var M=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},pi=16*1024*1024,ur=64*1024*1024;function dr(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 Ye(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 lr(t,e,n,r){let o=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},r),i=null;for await(let c of o){if(c.type==="error"){let y=c.error??{};throw new M(502,`image upstream error: ${String(y.message??"upstream reported an error")}`)}if(c.type==="response.completed"){i=c.response??null;break}}if(i===null)throw new M(502,"image upstream produced no terminal response \u2014 refusing to fabricate an image");let s=i.images;if(!Array.isArray(s)||s.length!==1||typeof s[0]!="string"||s[0].length===0)throw new M(502,`image upstream returned ${Array.isArray(s)?s.length:"a non-array images field"} images for the single-shot (n=1) contract, which produces exactly one \u2014 refusing to truncate or fabricate`);let a=[],d=s[0].startsWith("data:")?s[0].slice(s[0].indexOf(";base64,")+8):s[0],u=Buffer.from(d,"base64");if(dr(u)===null)throw new M(502,`image upstream returned a payload that is not a PNG/JPEG/WebP image (${u.length} bytes) \u2014 refusing to pass it through`);return a.push(d),{images:a}}function cr(t){return t instanceof M?t:t instanceof N?new M(404,t.message):new M(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function pr(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 Ye(r,new M(404,e===void 0?`no image-capable default model is configured for this caller (${o}) \u2014 name an image-capable \`model\``:`model "${e}" is not ${n==="edit"?"edit-capable":"generation-capable"} for this caller (${o}; capability is read from the catalog's modality metadata, not the serve function name)`)),!1}function fr(t,e){t&&(t.outputUnits=e.images.length)}function mr(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 fi={model:!0,prompt:!0,response_format:!0,n:!0};async function hr(t,e,n){let r=n.maxBodyBytes??ur;try{if(t.method!=="POST")throw new M(405,`${String(t.method)} is not supported on ${or}`);let o;try{o=JSON.parse((await Rt(t,r)).toString("utf8")||"{}")}catch(c){throw c instanceof K?new M(413,c.message):new M(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new M(400,"request body must be a JSON object");let i=o;for(let c of Object.keys(i))if(!fi[c])throw new M(400,`request field "${c}" is not implemented: the native image request schema carries only the prompt (size, quality, style, seed, mask and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, n.`,c);let s=i.prompt;if(typeof s!="string"||s.trim().length===0)throw new M(400,"prompt is required and must be a non-empty string","prompt");let a=i.model===void 0?void 0:i.model;if(a!==void 0&&typeof a!="string")throw new M(400,"model must be a string","model");await ar(n.turn,n.clients,a);let d=i.n??1;if(d!==1)throw new M(400,`n=${JSON.stringify(d)} is not supported: the native image request is single-shot and produces exactly one image per request (no batch support yet)`,"n");let u=i.response_format??"b64_json";if(u!=="b64_json")throw new M(400,`response_format ${JSON.stringify(u)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");if(n.stats?.request("images_generations",s.length,s.length),!await pr(n.imageModels,a,"generation",e))return;let l=[{type:"input_text",text:s}];try{let c=await lr(n.clients,a,l,n.turn?re(n.turn):void 0);fr(n.turn,c),mr(e,c)}catch(c){throw cr(c)}}catch(o){if(o instanceof K){Ye(e,new M(413,o.message));return}if(o instanceof M){Ye(e,o);return}throw o}}var mi={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function gr(t,e,n){let r=n.maxBodyBytes??ur,o=n.maxImagePartBytes??pi;try{if(t.method!=="POST")throw new M(405,`${String(t.method)} is not supported on ${At}`);let i=String(t.headers["content-type"]??"");if(!i.toLowerCase().startsWith("multipart/form-data"))throw new M(400,`the edit endpoint consumes multipart/form-data, got ${i||"none"}`);let s;try{s=await Et(t,r)}catch(p){throw p instanceof K?new M(413,p.message):new M(400,String(p instanceof Error?p.message:p))}let a=s.get("prompt");if(typeof a!="string"||a.trim().length===0)throw new M(400,"prompt is required and must be a non-empty string field","prompt");let d=s.get("model");if(d!==null&&typeof d!="string")throw new M(400,"model must be a string field","model");let u=d===null?void 0:d;await ar(n.turn,n.clients,u);let l=s.get("response_format")??"b64_json";if(l!=="b64_json")throw new M(400,`response_format ${JSON.stringify(l)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");for(let p of s.keys())if(!mi[p])throw new M(400,`form field "${p}" is not implemented: the native image request schema carries the prompt and the image inputs only (mask, size, seed and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, image (or image[]).`,p);let c=[];for(let p of["image","image[]"])for(let k of s.getAll(p)){if(!(k instanceof File))throw new M(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",p);if(k.size>o)throw new M(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,p);let E=new Uint8Array(await k.arrayBuffer()),S=dr(E);if(S===null)throw new M(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",p);c.push({bytes:E,mime:S})}if(c.length===0)throw new M(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",a.length,a.length),!await pr(n.imageModels,u,"edit",e))return;let y=[{type:"input_text",text:a}];for(let{bytes:p,mime:k}of c)y.push({type:"input_image",image_url:`data:${k};base64,${Buffer.from(p).toString("base64")}`});try{let p=await lr(n.clients,u,y,n.turn?re(n.turn):void 0);fr(n.turn,p),mr(e,p)}catch(p){throw cr(p)}}catch(i){if(i instanceof K){Ye(e,new M(413,i.message));return}if(i instanceof M){Ye(e,i);return}throw i}}var Xe="/v1/audio/speech";function Rr(t){return t===Xe}var yr=4096,_r=18e4,wr=5e3,hi=64*1024*1024,vr=8,br=5,kr=1024*1024,Ne="voice_applied",Ot="X-Urun-Voice-Applied",T=class extends Error{status;param;retryAfterSeconds;constructor(e,n,r,o){super(n),this.status=e,this.param=r,this.retryAfterSeconds=o}};function nn(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 Tt=class extends T{constructor(e){super(502,e)}};function Er(t){for(let e=0;e<t.length;e+=1){let n=t.charCodeAt(e);if(n!==9&&!(n>=32&&n<=126)&&!(n>=128&&n<=255))return!1}return!0}function Ar(t){return new T(502,`the runtime's ${Ne} receipt ${JSON.stringify(t)} cannot be relayed as the ${Ot} header \u2014 it carries a character no HTTP header or trailer value can carry (a newline, another control character, or a non-Latin-1 character); the lane never serves an answer whose receipt it cannot deliver nor silently drops the runtime's own acknowledgment`)}async function gi(t,e,n){if(t){if(t.model=ee({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 Ir(t,e){if(!t)return;let n=e/(24e3*2);t.outputUnits=n,t.outputMinutes=n/60}var yi=2;function _i(t){for(let e=0;e+1<t.byteLength;e+=2)if(Math.abs(t.readInt16LE(e))>yi)return!1;return!0}function wi(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 Sr=new WeakMap,It=new WeakMap;function vi(t,e){let n=It.get(t)??0;if(n>=vr)throw new T(503,`this model's speech queue is full \u2014 ${vr} requests are already admitted (one in-flight turn plus the queued depth) and each holds a pooled session turn; retry after ${br}s`,void 0,br);It.set(t,n+1);let r=Sr.get(t)??Promise.resolve(),o,i=new Promise(s=>{o=s});return Sr.set(t,i),(async()=>{await r;try{return await e()}finally{It.set(t,(It.get(t)??1)-1),o()}})()}async function bi(t,e,n,r){if(typeof t.evictSpeechSession!="function")throw new T(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned session after a failed turn \u2014 refusing loudly rather than handing the next request the failed turn\u2019s audio");try{await t.evictSpeechSession(e)}catch(s){if(!(s instanceof Z))throw s}if(n?.return===void 0)return;let o=Promise.resolve(n.return()).then(()=>{},()=>{}),i;await Promise.race([o,new Promise(s=>{i=setTimeout(s,r)})]).finally(()=>{i!==void 0&&clearTimeout(i)})}function Re(t){return t instanceof T?t:t instanceof N?new T(404,t.message):new T(502,`speech upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function xr(t,e,n,r,o,i,s,a){let d=a??(()=>{});return vi(e,async()=>{if(s?.()===!0)return null;let u=!1,l=!1,c=!1,y,p=new Promise(_=>{y=_}),k=[],E=()=>{l&&!c&&k.length>=o.quietRunFrames&&(c=!0,y())},S=e.onOutputAudio(_=>{let m=Buffer.from(_,"base64");if(m.byteLength===0)return;let v=_i(m);if(!u){if(v)return;u=!0}if(v){k.push(m),E();return}for(let P of k)d(P);k.length=0,d(m),E()}),f=null,h,g,A;try{A=(await t.createResponseOn(n,{model:r.model,input:[{role:"user",content:[{type:"input_text",text:r.text}]}],stream:!1,...r.voice!==void 0?{voice:r.voice}:{}}))[Symbol.asyncIterator]();let m=new Promise(b=>{g=setTimeout(b,i.capMs)}),v=!1;for(;;){let b=A.next(),I=await Promise.race([b,m]);if(I===void 0){v=!0,b.catch(()=>{});break}if(I.done)break;let C=I.value;if(C.type==="error"){let D=C.error??{};throw await A.return?.(),new T(502,`speech upstream error: ${String(D.message??"upstream reported an error")}`)}if(C.type==="response.completed"){f=C.response??null,l=!0,E(),await A.return?.();break}}if(clearTimeout(g),v)throw new T(504,`the speech turn produced no terminal event within ${i.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(f===null)throw new T(502,"speech upstream produced no terminal response \u2014 refusing to fabricate audio");let P=!1;if(await Promise.race([p,new Promise(b=>{h=setTimeout(()=>{P=!0,b()},o.capMs)})]),h!==void 0&&clearTimeout(h),r.voice!==void 0){let b=f[Ne];if(typeof b!="string"||b!==r.voice){let I=`the runtime did not acknowledge the requested voice ${JSON.stringify(r.voice)} \u2014 the terminal response carried ${typeof b=="string"?`${Ne} ${JSON.stringify(b)}`:`no ${Ne} field`}; the speak-bridge companion is not deployed, and this lane never serves default-voice audio as the requested voice`;throw c?new Tt(I):new T(502,I)}}if(!u)throw new T(502,"the upstream produced no voice for this utterance \u2014 refusing to serve silence as success");if(P&&!c&&k.length<1)throw new T(502,`the upstream audio stream did not quiesce within ${o.capMs}ms of the turn's end (voiced frames still arriving) \u2014 refusing to serve audio whose tail cannot be vouched for`);return f}catch(_){throw _ instanceof Tt||await bi(t,n,A,i.returnBoundMs),_}finally{h!==void 0&&clearTimeout(h),g!==void 0&&clearTimeout(g),S()}})}function ki(t,e,n,r){let o=!1,i=0,s=a=>{t.destroy(a instanceof Error?a:new Error(String(a)))};return{sink(a){n()||(o||(o=!0,t.statusCode=200,t.setHeader("content-type","audio/pcm"),r&&t.setHeader("trailer",Ot)),t.write(a,d=>{d||(i+=a.byteLength,Ir(e,i))}),t.writableLength>kr&&(e&&(e.streamError=!0),s(new Error(`the streamed pcm answer's undrained buffer crossed ${kr} bytes (1 MiB \u2014 about 21 s of 24 kHz mono PCM16): the caller stopped reading the stream, and the lane bounds what it will buffer for one answer rather than let a stalled reader hold the process's memory; the transfer is aborted (an after-first-byte failure \u2014 the bytes whose writes the socket confirmed are billed) and the caller should retry when it can drain the audio as it arrives`))))},started:()=>o,async finish(a){if(!n()){if(r){let d=a[Ne];if(typeof d=="string"){if(!Er(d))throw Ar(d);t.addTrailers({[Ot]:d})}}await new Promise(d=>{t.end(()=>d())})}},fail:s}}async function Si(t,e,n){let r;try{r=await t(e)}catch(o){throw Re(o)}return r?!0:(nn(n,new T(404,e===void 0?"no speech-capable default model is configured for this caller (catalog task tts) \u2014 name a tts model":`model "${e}" is not speech-capable (catalog task is not tts) \u2014 this lane serves only text-to-speech models; chat models ride /v1/chat/completions`)),!1)}var xi={model:!0,input:!0,voice:!0,response_format:!0,stream_format:!0,speed:!0,instructions:!0},Ri="wav, pcm";async function Or(t,e,n){let r=n.maxBodyBytes??hi,o=!1;e.once("close",()=>{o=!0});let i=()=>o||e.destroyed;try{if(t.method!=="POST")throw new T(405,`${String(t.method)} is not supported on ${Xe}`);let s;try{s=JSON.parse((await Rt(t,r)).toString("utf8")||"{}")}catch(g){throw g instanceof K?new T(413,g.message):new T(400,"request body is not valid JSON")}if(s===null||typeof s!="object"||Array.isArray(s))throw new T(400,"request body must be a JSON object");let a=s;for(let g of Object.keys(a))if(!xi[g])throw new T(400,`request field "${g}" is not implemented: the speech request schema carries the utterance and its voice (the qwen3 backend fixes voice style at deploy time, and sample rate is pinned to the engine's native 24 kHz mono). Supported fields: model, input, voice, response_format, stream_format, speed, instructions.`,g);let d=a.input;if(typeof d!="string"||d.trim().length===0)throw new T(400,"input is required and must be a non-empty string","input");if(d.length>yr)throw new T(400,`input is ${d.length} chars \u2014 over the ${yr}-char limit (one utterance; the backend bounds generation text-shaped, and an unbounded text is an unbounded synthesis)`,"input");let u=a.model===void 0?void 0:a.model;if(u!==void 0&&typeof u!="string")throw new T(400,"model must be a string","model");let l=a.voice;if(l!==void 0&&(typeof l!="string"||l.trim().length===0))throw new T(400,"voice must be a non-empty string","voice");if(typeof n.clients.speechVoices!="function")throw new T(501,`the speech lane needs a router-backed client (speechVoices) to resolve the voice set GET /v1/models publishes \u2014 refusing loudly rather than validating voices of ${Xe} against a stale copy of the list`);let c;try{c=await n.clients.speechVoices(u)}catch(g){throw Re(g)}if(c.defect!==null)throw new T(500,`the voice set for this model cannot be resolved: ${c.defect} \u2014 fix the catalog row; the lane refuses to serve rather than validate against a set nobody vouched`);let y=typeof l=="string"?l:void 0;if(y!==void 0&&c.published.length>0&&!c.published.includes(y))throw new T(400,`voice ${JSON.stringify(y)} is not one of the voices this model publishes \u2014 accepted voices: ${c.published.join(", ")}`,"voice");let p=a.response_format??"wav";if(p!=="wav"&&p!=="pcm")throw new T(400,`response_format ${JSON.stringify(p)} is not supported: this lane serves the engine's raw 24 kHz mono PCM16, so the supported formats are ${Ri} \u2014 and the lane will not label bytes with a format it did not encode`,"response_format");if(a.stream_format!==void 0&&typeof a.stream_format!="string")throw new T(400,"stream_format, when present, must be a string","stream_format");let k=a.speed;if(k!==void 0){if(typeof k!="number"||!Number.isFinite(k))throw new T(400,"speed, when present, must be a number","speed");if(k!==1)throw new T(400,`speed=${JSON.stringify(k)} is not supported: the backend exposes no speed knob (the qwen3 engine's speaking rate is fixed), so a non-default speed would be silently ignored \u2014 1 (the OpenAI default) is the only accepted value`,"speed")}let E=a.instructions;if(E!==void 0){if(typeof E!="string")throw new T(400,"instructions, when present, must be a string","instructions");if(E.trim().length>0)throw new T(400,"instructions are not supported: the backend fixes the voice style (instruct) at deploy time, so a per-request instruction would be silently ignored \u2014 the deployment speaks with its configured style","instructions")}await gi(n.turn,n.clients,u);let S=Buffer.byteLength(JSON.stringify(a));if(n.stats?.request("audio_speech",S,S),!await Si(n.speechModels,u,e))return;if(typeof n.clients.openAudioOn!="function")throw new T(501,"a speech-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the same session identity as the response turn \u2014 refusing loudly rather than answering an utterance whose audio cannot be collected");if(typeof n.clients.speechSessionHandle!="function")throw new T(501,"a speech-capable model needs a router-backed client (speechSessionHandle) to resolve the speech lane's own pooled session \u2014 one no interactive binding shares, because the per-session audio bridge broadcasts every output frame to every subscriber \u2014 refusing loudly rather than collecting speech audio on a session a realtime/gemini lane can be speaking on");if(typeof n.clients.evictSpeechSession!="function")throw new T(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned speech session after a failed turn (a turn-cap timeout above all) \u2014 without it, the next request on the same model would silently collect the failed turn\u2019s still-emitting audio; refusing loudly rather than serving on an un-evictable speech slot");if(i())return;let f;try{f=await n.clients.speechSessionHandle(u)}catch(g){throw Re(g)}let h;try{h=await n.clients.openAudioOn(f)}catch(g){throw Re(g)}n.turn&&(n.turn.sessionId=h.sessionId);try{if(p==="wav"){let g=[],A=await xr(n.clients,h,f,{model:u,text:d,...y!==void 0?{voice:y}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??_r,returnBoundMs:n.turnReturnBoundMs??wr},i,m=>g.push(m));if(A===null||i())return;let _=Buffer.concat(g);if(y===void 0){let m=A[Ne];if(typeof m=="string"){if(!Er(m))throw Ar(m);e.setHeader(Ot,m)}}Ir(n.turn,_.byteLength),e.statusCode=200,e.setHeader("content-type","audio/wav"),e.end(Buffer.concat([wi(_.byteLength),_]))}else{let g=ki(e,n.turn,i,y===void 0),A;try{A=await xr(n.clients,h,f,{model:u,text:d,...y!==void 0?{voice:y}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??_r,returnBoundMs:n.turnReturnBoundMs??wr},i,g.sink)}catch(_){let m=Re(_);if(!g.started())throw m;n.turn&&(n.turn.streamError=!0),g.fail(m);return}if(A===null)return;try{await g.finish(A)}catch(_){let m=Re(_);n.turn&&(n.turn.streamError=!0),g.fail(m);return}}}catch(g){throw Re(g)}}catch(s){if(s instanceof K){nn(e,new T(413,s.message));return}if(s instanceof T){nn(e,s);return}throw s}}var Pt="/v1/audio/transcriptions";function Tr(t){return t===Pt}var Pr="/v1/audio/models",Ei=16*1024*1024,Ai=64*1024*1024,B=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}};function rn(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 Cr(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 Ii(t){if(!Cr(t))return null;let e=12,n=null,r=null;for(;e+8<=t.length;){let i=String.fromCharCode(t[e],t[e+1],t[e+2],t[e+3]),s=(t[e+4]|t[e+5]<<8|t[e+6]<<16|t[e+7]<<24)>>>0,a=e+8;if(i==="fmt "&&a+16<=t.length){let d=t[a+2]|t[a+3]<<8,u=(t[a+4]|t[a+5]<<8|t[a+6]<<16|t[a+7]<<24)>>>0,l=t[a+14]|t[a+15]<<8;n={sampleRate:u,channels:d,bitsPerSample:l}}else if(i==="data"){let d=t.length-a;r=Math.min(s===0||s===4294967295?d:s,d)}e=a+s+s%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 Oi(t,e,n){if(t){if(t.model=ee({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 Ti(t,e){t&&(t.outputUnits=e.seconds)}async function Pi(t,e,n,r,o){let i=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1,...r!==void 0?{language:r}:{}},o),s=[],a=null;for await(let d of i){if(s.push(d),d.type==="error"){let u=d.error??{},l=typeof u.type=="string"?u.type:"upstream_error",c=String(u.message??"upstream reported an error");throw new B(l==="invalid_request_error"?400:502,`transcription upstream error: ${c}`)}if(d.type==="response.completed"){a=d.response??null;break}}if(a===null)throw new B(502,"transcription upstream produced no terminal response \u2014 refusing to fabricate a transcript");return Me(s,a)}function Mr(t){return t instanceof B?t:t instanceof N?new B(404,t.message):new B(502,`transcription upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function Ci(t,e,n){let r;try{r=await t(e)}catch(o){throw Mr(o)}return r?!0:(rn(n,new B(404,e===void 0?"no transcription-capable default model is configured for this caller (catalog task stt) \u2014 name an stt model":`model "${e}" is not transcription-capable (catalog task is not stt) \u2014 this lane serves only speech-to-text models; tts models ride /v1/audio/speech and chat models ride /v1/chat/completions`)),!1)}var Mi=new Set(["file","model","language","response_format"]);async function $r(t,e,n){let r=n.maxBodyBytes??Ai,o=n.maxAudioPartBytes??Ei;try{if(t.method!=="POST")throw new B(405,`${String(t.method)} is not supported on ${Pt}`);let i=String(t.headers["content-type"]??"");if(!i.toLowerCase().startsWith("multipart/form-data"))throw new B(400,`the transcriptions endpoint consumes multipart/form-data, got ${i||"none"}`);let s;try{s=await Et(t,r)}catch(f){throw f instanceof K?new B(413,f.message):new B(400,String(f instanceof Error?f.message:f))}let a=new Map;for(let f of s.keys()){if(!Mi.has(f))throw new B(400,`form field "${f}" 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.`,f);a.set(f,(a.get(f)??0)+1)}for(let[f,h]of a)if(h>1)throw new B(400,`form field "${f}" 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`,f);let d=s.get("model");if(d!==null&&typeof d!="string")throw new B(400,"model must be a string field","model");let u=d===null?void 0:d,l=s.get("language");if(l!==null&&(typeof l!="string"||l.trim().length===0))throw new B(400,"language, when present, must be a non-empty string","language");let c=l===null?void 0:l,y=s.get("response_format")??"json";if(y!=="json")throw new B(400,`response_format ${JSON.stringify(y)} is not supported: timestamps/segments are not produced on this lane yet \u2014 the runtime answers terminal transcript text only, so json is the only output format`,"response_format");let p=s.get("file");if(!(p instanceof File))throw new B(400,"file must be an uploaded WAV: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths","file");if(p.size>o)throw new B(413,`file part exceeds the ${o}-byte limit (the serve protocol's MAX_AUDIO_BYTES)`,"file");let k=new Uint8Array(await p.arrayBuffer());if(!Cr(k))throw new B(400,"the audio payload is not a RIFF/WAVE container \u2014 this lane accepts WAV uploads only (detected by magic bytes, not the declared Content-Type); the runtime decodes PCM16 WAV and nothing else","file");let E=Ii(k);if(E===null)throw new B(400,"the WAV header cannot be read (no fmt /data chunk pair, or a zero rate/channel/depth) \u2014 the runtime decodes RIFF/WAVE PCM16 and this container cannot be honestly decoded or billed","file");if(await Oi(n.turn,n.clients,u),n.stats?.request("audio_transcriptions",k.length,k.length),!await Ci(n.transcriptionModels,u,e))return;let S=[{type:"input_audio",input_audio:{data:Buffer.from(k).toString("base64"),format:"wav"}}];try{let f=await Pi(n.clients,u,S,c,n.turn?re(n.turn):void 0);Ti(n.turn,E),e.statusCode=200,e.setHeader("content-type","application/json"),e.end(JSON.stringify({text:f}))}catch(f){throw Mr(f)}}catch(i){if(i instanceof K){rn(e,new B(413,i.message));return}if(i instanceof B){rn(e,i);return}throw i}}var Nr="/v1/usage/requests",Lr=1e4,$i=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,ge=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},Ue=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}};var Ur=1024*1024;function on(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new ge(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new ge(`"${e}" must be an array of uuid strings`);if(n.length>Lr)throw new ge(`"${e}" carries ${n.length} ids; at most ${Lr} 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 i of n){if(typeof i!="string"||!$i.test(i))throw new ge(`"${e}" contains ${JSON.stringify(i)}, 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(i)||(o.add(i),r.push(i))}return r}function Dr(t){return on(t,"inference_ids")}function Ct(t,e){let n=new Set(e),r=new Set;for(let o=0;o<t.length;o++){let i=t[o];if(!n.has(i.inference_id))throw new Ue(`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(i.inference_id))throw new Ue(`the usage surface returned ${i.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(i.inference_id)}}var Ut=require("@urun-sh/core");var Li=require("@urun-sh/core"),Qe=class extends Error{kind;code;retryable;constructor(e,n,r,o){super(n),this.name="DecisionStreamError",this.kind=e,this.code=r,this.retryable=o,Object.setPrototypeOf(this,new.target.prototype)}};var sn="/partners/huggingface/v1/billing/requests";function Ni(t){return on(t,"requestIds")}function Ui(t){return t>=200&&t<=399}function Di(t){let e=[];for(let n of t)n.cost_nano_usd!==null&&Ui(n.http_status)&&e.push({requestId:n.inference_id,costNanoUsd:n.cost_nano_usd});return{requests:e.length>0?e:null}}async function Fr(t,e){let n=Ni(t),r=await e(n);return Ct(r,n),Di(r)}var Fi={"/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 Bi(t){return Fi[t]??null}var Hr=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function qi(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function ji(t){return t==="surface"}function Hi(t){let e=t?.ledgerFailureReason;return Hr.includes(e)?e:"unknown"}var Vi=["no_response_head","no_org","model_unresolved","no_model_named","queue_full"],Br=["priced","unpriced"],Gi={written:Br,already_written:Br,skipped:Vi,failed:Hr},Bu=Object.entries(Gi).flatMap(([t,e])=>e.map(n=>({outcome:t,reason:n})));function Ji(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function Wi(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 Ki=256,qr=4096,et=12e4,zi=250,Yi=8e3,tt=[],jr=new Set,nt=0,Ze=!1;var Xi=new Set;function Qi(){return nt===0&&tt.length===0}function Zi(){if(Qi()){Ze=!1;for(let t of[...Xi])t()}}function Vr(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
|
|
19
|
+
`}function ui(t){Se(process.stdout,t)}function di(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function Zt(t){return t.audioSession?.audio.sessionId??void 0}function Ke(t,e){let n=new rr.WebSocketServer({noServer:!0}),r=new Set;return t.on("upgrade",(o,i,s)=>{if(new URL(o.url??"/","http://localhost").pathname!==We)return;r.add(i),i.on("close",()=>{r.delete(i)});let d=x("rtup"),u=di(o),l=_=>{(e.log??ui)(ai(_))};l({stage:"accept",conn:d,model:u}),i.resume();let c=new AbortController,y=null,p=_=>{y??=_,c.abort(new Error(y))},k=()=>p("client socket closed"),E=()=>p("client socket closed (half-close)"),S=_=>p(`client socket error: ${_.message}`);i.once("close",k),i.once("end",E),i.once("error",S);let f=()=>{i.removeListener("close",k),i.removeListener("end",E),i.removeListener("error",S)},h=_=>{l({stage:"close",conn:d,model:u,..._})},g=_=>{let m=_,v=m.session!=null&&typeof m.session.sendResponseCreate=="function",P=m.clients!=null&&typeof m.clients.createResponse=="function";if(!v&&!P)throw new Error("resolver returned a binding without an upstream turn seam (session.sendResponseCreate or clients.createResponse required) \u2014 refusing the upgrade LOUD");return new Promise((b,I)=>{n.handleUpgrade(o,i,s,C=>{try{new xe({ws:C,binding:_,onClosed:(D,W)=>{h({while:"live",reason:W.reason})}}),l({stage:"admit",conn:d,model:u,...Zt(_)===void 0?{}:{session:Zt(_)}}),b()}catch(D){I(D)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o,c.signal).then(async _=>{if(c.signal.aborted){f();let m=Zt(_);try{await _.release?.()}catch(v){h({while:"queued",...m===void 0?{}:{session:m},reason:y??"client socket closed",release_failed:v instanceof Error?v.message:String(v)});return}h({while:"queued",...m===void 0?{}:{session:m},reason:y??"client socket closed",released:!0}),i.destroy();return}f(),await g(_)}).catch(_=>{let m=_ instanceof nr.SessionFailedError?_t(_):null,v=null;if(_!==null&&typeof _=="object"&&"status"in _){let I=_.status;typeof I=="number"&&(v=I)}let P=v??m?.status??502,b=_ instanceof Error?_.message:String(_);h({while:"queued",...c.signal.aborted?{reason:y??"client socket closed"}:{reason:b,status:P}}),i.destroyed||si(i,P,b)}).finally(()=>{r.delete(i)})}),async()=>{let o=Date.now()+5e3;for(;r.size>0&&Date.now()<o;)await new Promise(i=>setTimeout(i,25));await new Promise(i=>n.close(()=>i()))}}var se=(t,e)=>Object.assign(new Error(e),{status:t});function en(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("model")?.trim();return n===""?void 0:n}function li(t){let n=new URL(t.url??"/","http://localhost").searchParams.get("intent")?.trim();return n===""?void 0:n}var ci='"transcription" (a transcription session), or no intent at all (the realtime voice session)';async function tn(t,e,n,r){if(!t.supportsAudio)throw se(501,"the realtime lane needs a router-backed client (supportsAudio) to gate audio modality \u2014 hand-built clients without the catalog oracle are refused, never allowed silently");let o;try{o=await t.supportsAudio(e)}catch(u){throw u instanceof N?se(404,u.message):u}if(!o)throw se(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 se(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 i=!1;if(t.supportsTranscription)try{i=await t.supportsTranscription(e)}catch(u){throw u instanceof N?se(404,u.message):u}if(i&&!t.openTurnsOn)throw se(501,"a transcription-capable (stt) model needs a router-backed client (openTurnsOn) to open the engine\u2019s own turn/transcript lane (the stt named stream) pinned to the connection\u2019s pooled session \u2014 refusing loudly rather than binding a transcript-less transcription session");if(i&&t.transcriptStreamFor){let u;try{u=await t.transcriptStreamFor(e)}catch(l){throw l instanceof N?se(404,l.message):l}if(u!=="stt")throw se(501,`model ${JSON.stringify(e??"(default)")} overrides engine_args.transcript_stream with ${JSON.stringify(u)} \u2014 this lane consumes the engine\u2019s own transcripts from the platform\u2019s default "${"stt"}" stream and refuses loudly rather than listen on a stream the engine does not write`)}if(r!==void 0&&r!=="transcription")throw se(400,`intent ${JSON.stringify(r)} is not supported on this realtime lane \u2014 accepted values: ${ci}`);if(r==="transcription"&&!i)throw se(400,`model ${JSON.stringify(e??"(default)")} cannot serve the transcription intent \u2014 the transcription intent needs an stt model (the catalog task is not stt)`);let s=await t.sessionHandle(e),a={audio:await t.openAudioOn(s,n)},d={clients:{createResponse:async u=>await t.createResponseOn(s,u)},voice:o,audioSession:a};return i&&(d.turns=u=>t.openTurnsOn(s,u)),r==="transcription"&&(d.intent="transcription"),e!==void 0&&(d.model=e),d}function St(t,e){return{resolve:async(n,r)=>{if(e!==void 0){let o=n.headers.authorization,i=typeof o=="string"?o.trim():"",s=i.startsWith("Bearer ")?i.slice(7).trim():"";if(s===""||s!==e)throw se(401,"invalid local proxy api key")}return tn(t,en(n),r,li(n))}}}var xt=class extends Error{code;status;retryAfterSeconds;constructor(e,n,r,o){super(o),this.name="DecisionStreamFailure",this.code=e,this.status=n,this.retryAfterSeconds=r,Object.setPrototypeOf(this,new.target.prototype)}};var ze=class extends Error{lastDemandAgeMs;constructor(e){super("no ready decision slot on this proxy \u2014 the request was refused fast, not fenced; retry shortly (an out-of-band slot add was submitted)"),this.name="DecisionCapacityError",this.lastDemandAgeMs=e}};var K=class extends Error{};async function Rt(t,e){let n=Number(t.headers["content-length"]??"0");if(Number.isFinite(n)&&n>e)throw new K(`request body exceeds the ${e}-byte limit`);let r=[],o=0;for await(let i of t){let s=i;if(o+=s.length,o>e)throw new K(`request body exceeds the ${e}-byte limit`);r.push(s)}return Buffer.concat(r)}async function Et(t,e){let n=t[Symbol.asyncIterator](),r=0,o=new ReadableStream({async pull(s){try{let a=await n.next();if(a.done){s.close();return}if(r+=a.value.length,r>e){s.error(new K(`request body exceeds the ${e}-byte limit`));return}s.enqueue(a.value)}catch(a){s.error(a)}},cancel(){n.return?.()}}),i;try{let s=new Headers;for(let[u,l]of Object.entries(t.headers))if(typeof l=="string")s.set(u,l);else if(Array.isArray(l))for(let c of l)s.append(u,c);let a={method:"POST",headers:s,body:o,duplex:"half"};i=await new Request(`https://proxy.internal${t.url??"/"}`,a).formData()}catch(s){throw s instanceof K?s:new Error(`malformed multipart body: ${String(s instanceof Error?s.message:s)}`)}return i}var or="/v1/images/generations",At="/v1/images/edits";function ir(t){return t===or||t===At}var sr="/v1/images/models";async function ar(t,e,n){if(t){if(t.model=ee({model:n}),t.stream=!1,typeof e.catalogIdentity!="function"){t.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(n);if(r!==null){t.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(n):null;t.catalog={kind:"not-a-catalog-model",model:o??n??null}}catch(r){t.catalog={kind:"unresolved",reason:`model ${JSON.stringify(n??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}var M=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}},pi=16*1024*1024,ur=64*1024*1024;function dr(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 Ye(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 lr(t,e,n,r){let o=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1},r),i=null;for await(let c of o){if(c.type==="error"){let y=c.error??{};throw new M(502,`image upstream error: ${String(y.message??"upstream reported an error")}`)}if(c.type==="response.completed"){i=c.response??null;break}}if(i===null)throw new M(502,"image upstream produced no terminal response \u2014 refusing to fabricate an image");let s=i.images;if(!Array.isArray(s)||s.length!==1||typeof s[0]!="string"||s[0].length===0)throw new M(502,`image upstream returned ${Array.isArray(s)?s.length:"a non-array images field"} images for the single-shot (n=1) contract, which produces exactly one \u2014 refusing to truncate or fabricate`);let a=[],d=s[0].startsWith("data:")?s[0].slice(s[0].indexOf(";base64,")+8):s[0],u=Buffer.from(d,"base64");if(dr(u)===null)throw new M(502,`image upstream returned a payload that is not a PNG/JPEG/WebP image (${u.length} bytes) \u2014 refusing to pass it through`);return a.push(d),{images:a}}function cr(t){return t instanceof M?t:t instanceof N?new M(404,t.message):new M(502,`image upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function pr(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 Ye(r,new M(404,e===void 0?`no image-capable default model is configured for this caller (${o}) \u2014 name an image-capable \`model\``:`model "${e}" is not ${n==="edit"?"edit-capable":"generation-capable"} for this caller (${o}; capability is read from the catalog's modality metadata, not the serve function name)`)),!1}function fr(t,e){t&&(t.outputUnits=e.images.length)}function mr(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 fi={model:!0,prompt:!0,response_format:!0,n:!0};async function hr(t,e,n){let r=n.maxBodyBytes??ur;try{if(t.method!=="POST")throw new M(405,`${String(t.method)} is not supported on ${or}`);let o;try{o=JSON.parse((await Rt(t,r)).toString("utf8")||"{}")}catch(c){throw c instanceof K?new M(413,c.message):new M(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new M(400,"request body must be a JSON object");let i=o;for(let c of Object.keys(i))if(!fi[c])throw new M(400,`request field "${c}" is not implemented: the native image request schema carries only the prompt (size, quality, style, seed, mask and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, n.`,c);let s=i.prompt;if(typeof s!="string"||s.trim().length===0)throw new M(400,"prompt is required and must be a non-empty string","prompt");let a=i.model===void 0?void 0:i.model;if(a!==void 0&&typeof a!="string")throw new M(400,"model must be a string","model");await ar(n.turn,n.clients,a);let d=i.n??1;if(d!==1)throw new M(400,`n=${JSON.stringify(d)} is not supported: the native image request is single-shot and produces exactly one image per request (no batch support yet)`,"n");let u=i.response_format??"b64_json";if(u!=="b64_json")throw new M(400,`response_format ${JSON.stringify(u)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");if(n.stats?.request("images_generations",s.length,s.length),!await pr(n.imageModels,a,"generation",e))return;let l=[{type:"input_text",text:s}];try{let c=await lr(n.clients,a,l,n.turn?re(n.turn):void 0);fr(n.turn,c),mr(e,c)}catch(c){throw cr(c)}}catch(o){if(o instanceof K){Ye(e,new M(413,o.message));return}if(o instanceof M){Ye(e,o);return}throw o}}var mi={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function gr(t,e,n){let r=n.maxBodyBytes??ur,o=n.maxImagePartBytes??pi;try{if(t.method!=="POST")throw new M(405,`${String(t.method)} is not supported on ${At}`);let i=String(t.headers["content-type"]??"");if(!i.toLowerCase().startsWith("multipart/form-data"))throw new M(400,`the edit endpoint consumes multipart/form-data, got ${i||"none"}`);let s;try{s=await Et(t,r)}catch(p){throw p instanceof K?new M(413,p.message):new M(400,String(p instanceof Error?p.message:p))}let a=s.get("prompt");if(typeof a!="string"||a.trim().length===0)throw new M(400,"prompt is required and must be a non-empty string field","prompt");let d=s.get("model");if(d!==null&&typeof d!="string")throw new M(400,"model must be a string field","model");let u=d===null?void 0:d;await ar(n.turn,n.clients,u);let l=s.get("response_format")??"b64_json";if(l!=="b64_json")throw new M(400,`response_format ${JSON.stringify(l)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");for(let p of s.keys())if(!mi[p])throw new M(400,`form field "${p}" is not implemented: the native image request schema carries the prompt and the image inputs only (mask, size, seed and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, image (or image[]).`,p);let c=[];for(let p of["image","image[]"])for(let k of s.getAll(p)){if(!(k instanceof File))throw new M(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",p);if(k.size>o)throw new M(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,p);let E=new Uint8Array(await k.arrayBuffer()),S=dr(E);if(S===null)throw new M(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",p);c.push({bytes:E,mime:S})}if(c.length===0)throw new M(400,"the edit endpoint requires at least one `image` file part","image");if(n.stats?.request("images_edits",a.length,a.length),!await pr(n.imageModels,u,"edit",e))return;let y=[{type:"input_text",text:a}];for(let{bytes:p,mime:k}of c)y.push({type:"input_image",image_url:`data:${k};base64,${Buffer.from(p).toString("base64")}`});try{let p=await lr(n.clients,u,y,n.turn?re(n.turn):void 0);fr(n.turn,p),mr(e,p)}catch(p){throw cr(p)}}catch(i){if(i instanceof K){Ye(e,new M(413,i.message));return}if(i instanceof M){Ye(e,i);return}throw i}}var Xe="/v1/audio/speech";function Rr(t){return t===Xe}var yr=4096,_r=18e4,wr=5e3,hi=64*1024*1024,vr=8,br=5,kr=1024*1024,Ne="voice_applied",Ot="X-Urun-Voice-Applied",T=class extends Error{status;param;retryAfterSeconds;constructor(e,n,r,o){super(n),this.status=e,this.param=r,this.retryAfterSeconds=o}};function nn(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 Tt=class extends T{constructor(e){super(502,e)}};function Er(t){for(let e=0;e<t.length;e+=1){let n=t.charCodeAt(e);if(n!==9&&!(n>=32&&n<=126)&&!(n>=128&&n<=255))return!1}return!0}function Ar(t){return new T(502,`the runtime's ${Ne} receipt ${JSON.stringify(t)} cannot be relayed as the ${Ot} header \u2014 it carries a character no HTTP header or trailer value can carry (a newline, another control character, or a non-Latin-1 character); the lane never serves an answer whose receipt it cannot deliver nor silently drops the runtime's own acknowledgment`)}async function gi(t,e,n){if(t){if(t.model=ee({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 Ir(t,e){if(!t)return;let n=e/(24e3*2);t.outputUnits=n,t.outputMinutes=n/60}var yi=2;function _i(t){for(let e=0;e+1<t.byteLength;e+=2)if(Math.abs(t.readInt16LE(e))>yi)return!1;return!0}function wi(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 Sr=new WeakMap,It=new WeakMap;function vi(t,e){let n=It.get(t)??0;if(n>=vr)throw new T(503,`this model's speech queue is full \u2014 ${vr} requests are already admitted (one in-flight turn plus the queued depth) and each holds a pooled session turn; retry after ${br}s`,void 0,br);It.set(t,n+1);let r=Sr.get(t)??Promise.resolve(),o,i=new Promise(s=>{o=s});return Sr.set(t,i),(async()=>{await r;try{return await e()}finally{It.set(t,(It.get(t)??1)-1),o()}})()}async function bi(t,e,n,r){if(typeof t.evictSpeechSession!="function")throw new T(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned session after a failed turn \u2014 refusing loudly rather than handing the next request the failed turn\u2019s audio");try{await t.evictSpeechSession(e)}catch(s){if(!(s instanceof Z))throw s}if(n?.return===void 0)return;let o=Promise.resolve(n.return()).then(()=>{},()=>{}),i;await Promise.race([o,new Promise(s=>{i=setTimeout(s,r)})]).finally(()=>{i!==void 0&&clearTimeout(i)})}function Re(t){return t instanceof T?t:t instanceof N?new T(404,t.message):new T(502,`speech upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function xr(t,e,n,r,o,i,s,a){let d=a??(()=>{});return vi(e,async()=>{if(s?.()===!0)return null;let u=!1,l=!1,c=!1,y,p=new Promise(_=>{y=_}),k=[],E=()=>{l&&!c&&k.length>=o.quietRunFrames&&(c=!0,y())},S=e.onOutputAudio(_=>{let m=Buffer.from(_,"base64");if(m.byteLength===0)return;let v=_i(m);if(!u){if(v)return;u=!0}if(v){k.push(m),E();return}for(let P of k)d(P);k.length=0,d(m),E()}),f=null,h,g,A;try{A=(await t.createResponseOn(n,{model:r.model,input:[{role:"user",content:[{type:"input_text",text:r.text}]}],stream:!1,...r.voice!==void 0?{voice:r.voice}:{}}))[Symbol.asyncIterator]();let m=new Promise(b=>{g=setTimeout(b,i.capMs)}),v=!1;for(;;){let b=A.next(),I=await Promise.race([b,m]);if(I===void 0){v=!0,b.catch(()=>{});break}if(I.done)break;let C=I.value;if(C.type==="error"){let D=C.error??{};throw await A.return?.(),new T(502,`speech upstream error: ${String(D.message??"upstream reported an error")}`)}if(C.type==="response.completed"){f=C.response??null,l=!0,E(),await A.return?.();break}}if(clearTimeout(g),v)throw new T(504,`the speech turn produced no terminal event within ${i.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(f===null)throw new T(502,"speech upstream produced no terminal response \u2014 refusing to fabricate audio");let P=!1;if(await Promise.race([p,new Promise(b=>{h=setTimeout(()=>{P=!0,b()},o.capMs)})]),h!==void 0&&clearTimeout(h),r.voice!==void 0){let b=f[Ne];if(typeof b!="string"||b!==r.voice){let I=`the runtime did not acknowledge the requested voice ${JSON.stringify(r.voice)} \u2014 the terminal response carried ${typeof b=="string"?`${Ne} ${JSON.stringify(b)}`:`no ${Ne} field`}; the speak-bridge companion is not deployed, and this lane never serves default-voice audio as the requested voice`;throw c?new Tt(I):new T(502,I)}}if(!u)throw new T(502,"the upstream produced no voice for this utterance \u2014 refusing to serve silence as success");if(P&&!c&&k.length<1)throw new T(502,`the upstream audio stream did not quiesce within ${o.capMs}ms of the turn's end (voiced frames still arriving) \u2014 refusing to serve audio whose tail cannot be vouched for`);return f}catch(_){throw _ instanceof Tt||await bi(t,n,A,i.returnBoundMs),_}finally{h!==void 0&&clearTimeout(h),g!==void 0&&clearTimeout(g),S()}})}function ki(t,e,n,r){let o=!1,i=0,s=a=>{t.destroy(a instanceof Error?a:new Error(String(a)))};return{sink(a){n()||(o||(o=!0,t.statusCode=200,t.setHeader("content-type","audio/pcm"),r&&t.setHeader("trailer",Ot)),t.write(a,d=>{d||(i+=a.byteLength,Ir(e,i))}),t.writableLength>kr&&(e&&(e.streamError=!0),s(new Error(`the streamed pcm answer's undrained buffer crossed ${kr} bytes (1 MiB \u2014 about 21 s of 24 kHz mono PCM16): the caller stopped reading the stream, and the lane bounds what it will buffer for one answer rather than let a stalled reader hold the process's memory; the transfer is aborted (an after-first-byte failure \u2014 the bytes whose writes the socket confirmed are billed) and the caller should retry when it can drain the audio as it arrives`))))},started:()=>o,async finish(a){if(!n()){if(r){let d=a[Ne];if(typeof d=="string"){if(!Er(d))throw Ar(d);t.addTrailers({[Ot]:d})}}await new Promise(d=>{t.end(()=>d())})}},fail:s}}async function Si(t,e,n){let r;try{r=await t(e)}catch(o){throw Re(o)}return r?!0:(nn(n,new T(404,e===void 0?"no speech-capable default model is configured for this caller (catalog task tts) \u2014 name a tts model":`model "${e}" is not speech-capable (catalog task is not tts) \u2014 this lane serves only text-to-speech models; chat models ride /v1/chat/completions`)),!1)}var xi={model:!0,input:!0,voice:!0,response_format:!0,stream_format:!0,speed:!0,instructions:!0},Ri="wav, pcm";async function Or(t,e,n){let r=n.maxBodyBytes??hi,o=!1;e.once("close",()=>{o=!0});let i=()=>o||e.destroyed;try{if(t.method!=="POST")throw new T(405,`${String(t.method)} is not supported on ${Xe}`);let s;try{s=JSON.parse((await Rt(t,r)).toString("utf8")||"{}")}catch(g){throw g instanceof K?new T(413,g.message):new T(400,"request body is not valid JSON")}if(s===null||typeof s!="object"||Array.isArray(s))throw new T(400,"request body must be a JSON object");let a=s;for(let g of Object.keys(a))if(!xi[g])throw new T(400,`request field "${g}" is not implemented: the speech request schema carries the utterance and its voice (the qwen3 backend fixes voice style at deploy time, and sample rate is pinned to the engine's native 24 kHz mono). Supported fields: model, input, voice, response_format, stream_format, speed, instructions.`,g);let d=a.input;if(typeof d!="string"||d.trim().length===0)throw new T(400,"input is required and must be a non-empty string","input");if(d.length>yr)throw new T(400,`input is ${d.length} chars \u2014 over the ${yr}-char limit (one utterance; the backend bounds generation text-shaped, and an unbounded text is an unbounded synthesis)`,"input");let u=a.model===void 0?void 0:a.model;if(u!==void 0&&typeof u!="string")throw new T(400,"model must be a string","model");let l=a.voice;if(l!==void 0&&(typeof l!="string"||l.trim().length===0))throw new T(400,"voice must be a non-empty string","voice");if(typeof n.clients.speechVoices!="function")throw new T(501,`the speech lane needs a router-backed client (speechVoices) to resolve the voice set GET /v1/models publishes \u2014 refusing loudly rather than validating voices of ${Xe} against a stale copy of the list`);let c;try{c=await n.clients.speechVoices(u)}catch(g){throw Re(g)}if(c.defect!==null)throw new T(500,`the voice set for this model cannot be resolved: ${c.defect} \u2014 fix the catalog row; the lane refuses to serve rather than validate against a set nobody vouched`);let y=typeof l=="string"?l:void 0;if(y!==void 0&&c.published.length>0&&!c.published.includes(y))throw new T(400,`voice ${JSON.stringify(y)} is not one of the voices this model publishes \u2014 accepted voices: ${c.published.join(", ")}`,"voice");let p=a.response_format??"wav";if(p!=="wav"&&p!=="pcm")throw new T(400,`response_format ${JSON.stringify(p)} is not supported: this lane serves the engine's raw 24 kHz mono PCM16, so the supported formats are ${Ri} \u2014 and the lane will not label bytes with a format it did not encode`,"response_format");if(a.stream_format!==void 0&&typeof a.stream_format!="string")throw new T(400,"stream_format, when present, must be a string","stream_format");let k=a.speed;if(k!==void 0){if(typeof k!="number"||!Number.isFinite(k))throw new T(400,"speed, when present, must be a number","speed");if(k!==1)throw new T(400,`speed=${JSON.stringify(k)} is not supported: the backend exposes no speed knob (the qwen3 engine's speaking rate is fixed), so a non-default speed would be silently ignored \u2014 1 (the OpenAI default) is the only accepted value`,"speed")}let E=a.instructions;if(E!==void 0){if(typeof E!="string")throw new T(400,"instructions, when present, must be a string","instructions");if(E.trim().length>0)throw new T(400,"instructions are not supported: the backend fixes the voice style (instruct) at deploy time, so a per-request instruction would be silently ignored \u2014 the deployment speaks with its configured style","instructions")}await gi(n.turn,n.clients,u);let S=Buffer.byteLength(JSON.stringify(a));if(n.stats?.request("audio_speech",S,S),!await Si(n.speechModels,u,e))return;if(typeof n.clients.openAudioOn!="function")throw new T(501,"a speech-capable model needs a router-backed client (openAudioOn) to open its native session audio bridge pinned to the same session identity as the response turn \u2014 refusing loudly rather than answering an utterance whose audio cannot be collected");if(typeof n.clients.speechSessionHandle!="function")throw new T(501,"a speech-capable model needs a router-backed client (speechSessionHandle) to resolve the speech lane's own pooled session \u2014 one no interactive binding shares, because the per-session audio bridge broadcasts every output frame to every subscriber \u2014 refusing loudly rather than collecting speech audio on a session a realtime/gemini lane can be speaking on");if(typeof n.clients.evictSpeechSession!="function")throw new T(501,"a speech-capable model needs a router-backed client (evictSpeechSession) to retire its pinned speech session after a failed turn (a turn-cap timeout above all) \u2014 without it, the next request on the same model would silently collect the failed turn\u2019s still-emitting audio; refusing loudly rather than serving on an un-evictable speech slot");if(i())return;let f;try{f=await n.clients.speechSessionHandle(u)}catch(g){throw Re(g)}let h;try{h=await n.clients.openAudioOn(f)}catch(g){throw Re(g)}n.turn&&(n.turn.sessionId=h.sessionId);try{if(p==="wav"){let g=[],A=await xr(n.clients,h,f,{model:u,text:d,...y!==void 0?{voice:y}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??_r,returnBoundMs:n.turnReturnBoundMs??wr},i,m=>g.push(m));if(A===null||i())return;let _=Buffer.concat(g);if(y===void 0){let m=A[Ne];if(typeof m=="string"){if(!Er(m))throw Ar(m);e.setHeader(Ot,m)}}Ir(n.turn,_.byteLength),e.statusCode=200,e.setHeader("content-type","audio/wav"),e.end(Buffer.concat([wi(_.byteLength),_]))}else{let g=ki(e,n.turn,i,y===void 0),A;try{A=await xr(n.clients,h,f,{model:u,text:d,...y!==void 0?{voice:y}:{}},{quietRunFrames:n.quietRunFrames??3,capMs:n.drainCapMs??2e3},{capMs:n.turnCapMs??_r,returnBoundMs:n.turnReturnBoundMs??wr},i,g.sink)}catch(_){let m=Re(_);if(!g.started())throw m;n.turn&&(n.turn.streamError=!0),g.fail(m);return}if(A===null)return;try{await g.finish(A)}catch(_){let m=Re(_);n.turn&&(n.turn.streamError=!0),g.fail(m);return}}}catch(g){throw Re(g)}}catch(s){if(s instanceof K){nn(e,new T(413,s.message));return}if(s instanceof T){nn(e,s);return}throw s}}var Pt="/v1/audio/transcriptions";function Tr(t){return t===Pt}var Pr="/v1/audio/models",Ei=16*1024*1024,Ai=64*1024*1024,B=class extends Error{status;param;constructor(e,n,r){super(n),this.status=e,this.param=r}};function rn(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 Cr(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 Ii(t){if(!Cr(t))return null;let e=12,n=null,r=null;for(;e+8<=t.length;){let i=String.fromCharCode(t[e],t[e+1],t[e+2],t[e+3]),s=(t[e+4]|t[e+5]<<8|t[e+6]<<16|t[e+7]<<24)>>>0,a=e+8;if(i==="fmt "&&a+16<=t.length){let d=t[a+2]|t[a+3]<<8,u=(t[a+4]|t[a+5]<<8|t[a+6]<<16|t[a+7]<<24)>>>0,l=t[a+14]|t[a+15]<<8;n={sampleRate:u,channels:d,bitsPerSample:l}}else if(i==="data"){let d=t.length-a;r=Math.min(s===0||s===4294967295?d:s,d)}e=a+s+s%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 Oi(t,e,n){if(t){if(t.model=ee({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 Ti(t,e){t&&(t.outputUnits=e.seconds)}async function Pi(t,e,n,r,o){let i=await t.createResponse({model:e,input:[{role:"user",content:n}],stream:!1,...r!==void 0?{language:r}:{}},o),s=[],a=null;for await(let d of i){if(s.push(d),d.type==="error"){let u=d.error??{},l=typeof u.type=="string"?u.type:"upstream_error",c=String(u.message??"upstream reported an error");throw new B(l==="invalid_request_error"?400:502,`transcription upstream error: ${c}`)}if(d.type==="response.completed"){a=d.response??null;break}}if(a===null)throw new B(502,"transcription upstream produced no terminal response \u2014 refusing to fabricate a transcript");return Me(s,a)}function Mr(t){return t instanceof B?t:t instanceof N?new B(404,t.message):new B(502,`transcription upstream failed: ${String(t instanceof Error?t.message:t)}`)}async function Ci(t,e,n){let r;try{r=await t(e)}catch(o){throw Mr(o)}return r?!0:(rn(n,new B(404,e===void 0?"no transcription-capable default model is configured for this caller (catalog task stt) \u2014 name an stt model":`model "${e}" is not transcription-capable (catalog task is not stt) \u2014 this lane serves only speech-to-text models; tts models ride /v1/audio/speech and chat models ride /v1/chat/completions`)),!1)}var Mi=new Set(["file","model","language","response_format"]);async function $r(t,e,n){let r=n.maxBodyBytes??Ai,o=n.maxAudioPartBytes??Ei;try{if(t.method!=="POST")throw new B(405,`${String(t.method)} is not supported on ${Pt}`);let i=String(t.headers["content-type"]??"");if(!i.toLowerCase().startsWith("multipart/form-data"))throw new B(400,`the transcriptions endpoint consumes multipart/form-data, got ${i||"none"}`);let s;try{s=await Et(t,r)}catch(f){throw f instanceof K?new B(413,f.message):new B(400,String(f instanceof Error?f.message:f))}let a=new Map;for(let f of s.keys()){if(!Mi.has(f))throw new B(400,`form field "${f}" 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.`,f);a.set(f,(a.get(f)??0)+1)}for(let[f,h]of a)if(h>1)throw new B(400,`form field "${f}" 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`,f);let d=s.get("model");if(d!==null&&typeof d!="string")throw new B(400,"model must be a string field","model");let u=d===null?void 0:d,l=s.get("language");if(l!==null&&(typeof l!="string"||l.trim().length===0))throw new B(400,"language, when present, must be a non-empty string","language");let c=l===null?void 0:l,y=s.get("response_format")??"json";if(y!=="json")throw new B(400,`response_format ${JSON.stringify(y)} is not supported: timestamps/segments are not produced on this lane yet \u2014 the runtime answers terminal transcript text only, so json is the only output format`,"response_format");let p=s.get("file");if(!(p instanceof File))throw new B(400,"file must be an uploaded WAV: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths","file");if(p.size>o)throw new B(413,`file part exceeds the ${o}-byte limit (the serve protocol's MAX_AUDIO_BYTES)`,"file");let k=new Uint8Array(await p.arrayBuffer());if(!Cr(k))throw new B(400,"the audio payload is not a RIFF/WAVE container \u2014 this lane accepts WAV uploads only (detected by magic bytes, not the declared Content-Type); the runtime decodes PCM16 WAV and nothing else","file");let E=Ii(k);if(E===null)throw new B(400,"the WAV header cannot be read (no fmt /data chunk pair, or a zero rate/channel/depth) \u2014 the runtime decodes RIFF/WAVE PCM16 and this container cannot be honestly decoded or billed","file");if(await Oi(n.turn,n.clients,u),n.stats?.request("audio_transcriptions",k.length,k.length),!await Ci(n.transcriptionModels,u,e))return;let S=[{type:"input_audio",input_audio:{data:Buffer.from(k).toString("base64"),format:"wav"}}];try{let f=await Pi(n.clients,u,S,c,n.turn?re(n.turn):void 0);Ti(n.turn,E),e.statusCode=200,e.setHeader("content-type","application/json"),e.end(JSON.stringify({text:f}))}catch(f){throw Mr(f)}}catch(i){if(i instanceof K){rn(e,new B(413,i.message));return}if(i instanceof B){rn(e,i);return}throw i}}var Nr="/v1/usage/requests",Lr=1e4,$i=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,ge=class extends Error{constructor(n,r=400){super(n);this.status=r;this.name="UsageRequestError"}status},Ue=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}};var Ur=1024*1024;function on(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new ge(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let n=t[e];if(!Array.isArray(n))throw new ge(`"${e}" must be an array of uuid strings`);if(n.length>Lr)throw new ge(`"${e}" carries ${n.length} ids; at most ${Lr} 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 i of n){if(typeof i!="string"||!$i.test(i))throw new ge(`"${e}" contains ${JSON.stringify(i)}, 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(i)||(o.add(i),r.push(i))}return r}function Dr(t){return on(t,"inference_ids")}function Ct(t,e){let n=new Set(e),r=new Set;for(let o=0;o<t.length;o++){let i=t[o];if(!n.has(i.inference_id))throw new Ue(`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(i.inference_id))throw new Ue(`the usage surface returned ${i.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(i.inference_id)}}var Ut=require("@urun-sh/core");var Li=require("@urun-sh/core"),Qe=class extends Error{kind;code;retryable;constructor(e,n,r,o){super(n),this.name="DecisionStreamError",this.kind=e,this.code=r,this.retryable=o,Object.setPrototypeOf(this,new.target.prototype)}};var sn="/partners/huggingface/v1/billing/requests";function Ni(t){return on(t,"requestIds")}function Ui(t){return t>=200&&t<=399}function Di(t){let e=[];for(let n of t)n.cost_nano_usd!==null&&Ui(n.http_status)&&e.push({requestId:n.inference_id,costNanoUsd:n.cost_nano_usd});return{requests:e.length>0?e:null}}async function Fr(t,e){let n=Ni(t),r=await e(n);return Ct(r,n),Di(r)}var Fi={"/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 Bi(t){return Fi[t]??null}var Hr=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function qi(t){let e=t?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function ji(t){return t==="surface"}function Hi(t){let e=t?.ledgerFailureReason;return Hr.includes(e)?e:"unknown"}var Vi=["no_response_head","no_org","model_unresolved","no_model_named","queue_full"],Br=["priced","unpriced"],Gi={written:Br,already_written:Br,skipped:Vi,failed:Hr},Bu=Object.entries(Gi).flatMap(([t,e])=>e.map(n=>({outcome:t,reason:n})));function Ji(t){return t.status===null?!1:t.status>=200&&t.status<400&&!t.stream_error}function Wi(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 Ki=256,qr=4096,et=12e4,zi=250,Yi=8e3,tt=[],jr=new Set,nt=0,Ze=!1;var Xi=new Set;function Qi(){return nt===0&&tt.length===0}function Zi(){if(Qi()){Ze=!1;for(let t of[...Xi])t()}}function Vr(t,e){return`${JSON.stringify({event:t,ts:new Date().toISOString(),...e})}
|
|
20
20
|
`}function es(t){Se(process.stdout,t)}function Gr(t,e){try{t(e)}catch{}}function Jr(t){let{record:e,ledger:n}=t;if(n===null)return;let r=n.report??es,o=Bi(e.path);if(o===null)return;let i=l=>{try{n.observe(l)}catch{}},s=(l,c)=>{i({outcome:"skipped",reason:l}),Gr(r,Vr("inference_ledger_skipped",{inference_id:e.inference_id,path:e.path,reason:l,detail:c}))};if(e.status===null){s("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){s("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"){s("model_unresolved",a.reason);return}let d;if(a.kind==="catalog")d={model_id:a.model_id,identity_kind:"catalog_model",variant:a.variant};else{if(a.model===null){s("no_model_named","the request resolved to a caller-org app but named no model, so there is no app slug to record and `model_id` cannot be written honestly");return}d={model_id:a.model,identity_kind:"caller_org_app",variant:null}}let u={inference_id:e.inference_id,...d,task:o,http_status:e.status,started_at:Wi(e),completed_at:e.ts,billable_outcome:Ji(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(tt.length>=qr){s("queue_full",`${tt.length} ledger writes are already waiting (capacity ${qr}, ${nt} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}tt.push({entry:u,req:t.req,record:e,ledger:n,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),nt<Ki&&(nt+=1,ts())}async function ts(){try{for(;;){let t=ns();if(t===void 0)return;jr.add(t);try{await rs(t)}finally{jr.delete(t)}}}finally{nt-=1,Zi()}}function ns(){let t=Date.now();for(;;){let e=tt.shift();if(e===void 0)return;let n=t-e.queuedAt;if(n<et)return e;De(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 ${et}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 rs(t){let{entry:e,record:n,ledger:r}=t;for(;;){t.attempts+=1;try{let o=await r.write(t.req,e);De(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 i=qi(o);if(i!==null){De(t,{outcome:"already_written",reason:i.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:i.cost_nano_usd,price_version:i.price_version,attempts:t.attempts});return}let s=Hi(o),a=Date.now()-t.queuedAt;if(Ze){De(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(ji(s)&&a<et){if(await is(os(t.attempts)),Ze||Date.now()-t.queuedAt>=et){let d=Date.now()-t.queuedAt;De(t,{outcome:"failed",reason:Ze?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:Ze?"dropped_on_exit":"dwell_expired",attempts:t.attempts,waited_ms:d,error:`this row's backoff outlived the ${et}ms dwell bound (waited ${d}ms over ${t.attempts} attempt(s)) and was given up on rather than starting an attempt that could only finish past it. Last failure: ${o instanceof Error?o.message:String(o)}`});return}continue}De(t,{outcome:"failed",reason:s},{event:"inference_ledger_write_failed",reason:s,attempts:t.attempts,waited_ms:a,error:o instanceof Error?o.message:String(o)});return}}}function os(t){return Math.min(Yi,zi*2**Math.max(0,t-1))}function is(t){return new Promise(e=>{setTimeout(e,t).unref?.()})}function De(t,e,n){if(t.settled)return;t.settled=!0;try{t.ledger.observe(e)}catch{}let{event:r,...o}=n;Gr(t.report,Vr(r,{inference_id:t.entry.inference_id,path:t.record.path,org:t.record.org,...o}))}var un="x-urun-profile",rt="voice";var ne=class extends Error{status=400},ss=/(?:[.!?:;]|\n)[)\]"'\u2019\u201d\u00bb\u300d\u300b\u3011\uff09\uff3d]?\s*$/,as=/\s/;function us(t){return ss.test(t)}function an(t,e,n){let r=t[e];if(r===void 0)return n;if(typeof r!="boolean")throw new ne(`stream_options.voice.${e} must be a boolean when present (got ${typeof r})`);return r}function Wr(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 ne(`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 ne(`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 ne(`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 Kr(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 ne(`stream_options must be a JSON object when present, e.g. {"profile":"voice","voice":{...}} (got ${JSON.stringify(r)})`);let o=r,i=o?.profile,s=!1;if(n!=null){let u=n.trim();if(u==="")throw new ne(`the ${un} header is empty \u2014 the only profile this lane accepts is "${rt}" (omit the header for the ordinary lane)`);if(u!==rt)throw new ne(`unknown profile "${u}" \u2014 the only profile this lane accepts is "${rt}"`);s=!0}if(i!==void 0){if(typeof i!="string"||i!==rt)throw new ne(`unknown profile ${JSON.stringify(i)} \u2014 the only profile this lane accepts is "${rt}"`);s=!0}if(!s)return null;if(e.stream!==!0)throw new ne("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 ne("stream_options.voice must be an object when present (flush_max_chars, flush_max_ms, sentence_flush, include_reasoning, metadata)");let d=a??{};return{flushMaxChars:Wr(d,"flush_max_chars",120,{min:2,minReason:"a hard split at a bound below 2 cannot keep UTF-16 surrogate pairs (astral characters) intact"}),flushMaxMs:Wr(d,"flush_max_ms",250,{max:2147483647}),sentenceFlush:an(d,"sentence_flush",!0),includeReasoning:an(d,"include_reasoning",!1),metadata:an(d,"metadata",!1)}}var Mt=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&&us(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(as.test(this.buffer[o]))return o+1;let n=Math.max(1,e),r=this.buffer.charCodeAt(n-1);return r>=55296&&r<=56319?n-1:n}clearTimer(){this.timer!==null&&(clearTimeout(this.timer),this.timer=null)}};function cn(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function ds(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 X(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function G(t,e,n,r="invalid_request_error",o){X(t,e,{error:o?{message:n,type:r,code:o}:{message:n,type:r}})}function pn(t,e){let n=e.error,r=n?.type??"upstream_error",o=n?.message??JSON.stringify(e);X(t,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:n?.code??null}})}function ue(t,e,n,r){X(t,e,{type:"error",error:{type:n,message:r}})}function Fe(t,e,n){if(e==="anthropic"){X(t,404,{type:"error",error:{type:"not_found_error",message:n.message}});return}X(t,404,{error:{message:n.message,type:"invalid_request_error",code:"model_not_found"}})}var ls=64*1024*1024,ot=class extends Error{};async function zr(t,e=ls){let n=[],r=0;for await(let i of t){if(r+=i.length,r>e)throw new ot(`request body exceeds ${e} bytes`);n.push(i)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function Ee(t,e,n,r){return{id:t,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:e,choices:[{index:0,delta:n,finish_reason:r}]}}function Qr(t){let e=t?.usage;if(!e||typeof e!="object")return{kind:"absent"};let n=e,r=d=>typeof d=="number"&&Number.isFinite(d)&&d>=0?d:null,o=r(n.input_tokens),i=r(n.output_tokens),s=d=>d??0,a=r(n.total_tokens)??s(o)+s(i);if(s(o)>0||s(i)>0){let d={input_tokens:s(o),output_tokens:s(i),total_tokens:a};return o===null||i===null?{kind:"partial",usage:d}:{kind:"counts",usage:d}}return o===null||i===null?{kind:"absent"}:{kind:"zero",usage:{input_tokens:0,output_tokens:0,total_tokens:a}}}function $t(t){let e=Qr(t);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function Yr(t){let e=$t(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 cs(t,e){let n=Qr(e);n.kind==="absent"||n.kind==="partial"||(t.promptTokens=n.usage.input_tokens,t.completionTokens=n.usage.output_tokens)}function ps(t){let e=t?.urun_timing;if(!e||typeof e!="object")return null;let n=e,r=d=>{let u=n[d];return typeof u=="number"&&Number.isSafeInteger(u)&&u>=0?u:null},o=r("queue_ms"),i=r("prefill_ms"),s=r("decode_ms"),a=r("total_ms");return o===null||i===null||s===null||a===null?null:{queue_ms:o,prefill_ms:i,decode_ms:s,total_ms:a}}function fs(t,e){let n=ps(e);n&&(t.timing=n)}function Ae(t,e){cs(t,e),fs(t,e)}async function Dt(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 Zr(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 ms(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 Lt=class extends Error{};function hs(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 Lt(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function gs(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:hs(n.role,n.content)})}return e}function dn(t){return Pe(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 ys(t,e,n,r,o,i){let s=He(i.id),a=t.stream===!0;if(i.model=ee(t),i.stream=a,await Dt(i,e,typeof t.model=="string"?t.model:void 0),a){let f;try{f=await Ge({body:t,clients:e,store:n,stats:o,responseId:s,turn:i})}catch(h){if(h instanceof N){Fe(r,"openai",h);return}if(h instanceof oe){G(r,h.status,h.message);return}throw h}cn(r);for await(let h of f.events)h.type==="error"&&(i.streamError=!0),h.type==="response.completed"&&Ae(i,h.response),r.write(`event: ${String(h.type??"message")}
|
|
21
21
|
data: ${JSON.stringify(h)}
|
|
22
22
|
|
package/dist/proxy/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{B as l,C as a,D as x,E as s,G as O,H as f,Y as I,a as n,b as p,c as m}from"../chunk-
|
|
1
|
+
import{B as l,C as a,D as x,E as s,G as O,H as f,Y as I,a as n,b as p,c as m}from"../chunk-ERRG6R4T.js";import{f as r,g as i}from"../chunk-CUDFHFNX.js";import{a as o,h as t}from"../chunk-3JWYIKHM.js";import"../chunk-6M5JK4YC.js";import{c as e}from"../chunk-YSFSRI3D.js";e();export{o as GEMINI_LIVE_PATH,a as OPENAI_REALTIME_PATH,l as OpenAIRealtimeConnection,n as ResumptionRegistry,i as SessionGoneError,r as UnknownModelError,t as VIDEO_OUT_SETUP_KEY,p as attachGeminiLive,x as attachOpenAIRealtime,I as createOpenAIProxy,f as fixedClientsResolver,O as realtimeBindingFor,s as realtimeModelOf,m as serveGeminiLiveSocket};
|
package/package.json
CHANGED
package/dist/chunk-KSC5Q6PZ.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{a as M}from"./chunk-K23AZPI4.js";import{I as j,J as B,N as _,O as K,P as q}from"./chunk-4W3ZPIR7.js";import{a as F}from"./chunk-76BZLCGT.js";import{a as x,b as G,d as $,g as A,h as W,j as E,k as H,l as V}from"./chunk-CUDFHFNX.js";import{c as R,f as D,g as N,p as U}from"./chunk-6M5JK4YC.js";import{c as v}from"./chunk-YSFSRI3D.js";v();import{App as z,createClientToken as de,COLD_BOOT_WHEN_LIVE_TIMEOUT_MS as ue,DEFAULT_WHEN_LIVE_TIMEOUT_MS as Z,isWakingPhase as pe}from"@urun-sh/core";v();import{SessionFailedError as te}from"@urun-sh/core";var L=2;function se(e){let s=e?.type;return s==="response.created"||s==="response.in_progress"}function ie(e){let s=e?.type;return s==="response.completed"||s==="error"}function Q(e){return async function(a,d){return(async function*(){for(let l=1;l<=L;l++){let{app:p,entry:f,identity:t}=await e.sessionFor(a.model);d?.({sessionId:e.sessionIdOf?.(f)??null,identity:t});let r=!1,c=!1;try{let m=await e.createStream(f,a);for await(let g of m){if(ie(g)&&(r=!0),se(g)){if(l>1)continue}else c=!0;yield g}}catch(m){if(m instanceof te&&(m.kind==="never-live"||m.kind==="create-failed")){try{e.onAdmissionVerdict?.(p,f,m)}catch{}throw m}if(l<L&&!c){await e.evict(p,f);continue}throw m}if(r)return;if(l<L&&!c){await e.evict(p,f);continue}return}})()}}import{randomUUID as fe}from"crypto";v();var re=3,ae="llm",le=new Set(["paused","error"]);function Y(e,s){let a=e.doc(ae),d=typeof a.onConnectionState=="function",u=typeof e.onPhase=="function";if(!d&&!u)throw new Error(`session for app "${s}" exposes neither a doc connection-state surface nor onPhase \u2014 session-gone detection requires @urun-sh/core Session (SessionDocument.onConnectionState / Session.onPhase); pooling a session with no native lifecycle signal would silently swallow requests dispatched after the platform closes it`);let l=null,p=!1,f=new Set,t=[],r=c=>{if(!l){l=c;for(let m of[...f])m(c);f.clear()}};return d&&t.push(a.onConnectionState(c=>{p&&(c.consecutiveFailures<re||r(new A(`the pooled session for app "${s}" is gone: its doc backhaul failed ${c.consecutiveFailures} consecutive times (last close ${c.lastCloseCode===null?"by provider liveness reap":`code ${c.lastCloseCode}`}${c.lastCloseReason?`, reason ${JSON.stringify(c.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`)))})),u&&t.push(e.onPhase(c=>{if(c.name==="live"){p=!0;return}p&&le.has(c.name)&&r(new A(`the pooled session for app "${s}" lost its compute binding (phase "${c.name}"). The entry is evicted; retry to get a fresh session. The named session itself is not over \u2014 it stays attachable by name.`))})),{gone:()=>l,onGone(c){return l?(c(l),()=>{}):(f.add(c),()=>f.delete(c))},dispose(){f.clear();for(let c of t.splice(0))c()}}}async function*C(e,s){let a=s.gone();if(a)throw a;let d=e[Symbol.asyncIterator](),u,l=new Promise((p,f)=>{u=s.onGone(t=>f(t))});l.catch(()=>{});try{for(;;){let p=await Promise.race([d.next(),l]);if(p.done)return;yield p.value}}finally{u?.(),d.return?.(void 0)}}var me=1,ge=4,he=4;function He(e,s){let a=(p,f)=>{let t=(e[p]??"").trim();if(!t)return f;let r=Number.parseInt(t,10);if(!Number.isInteger(r)||r<1)throw new Error(`${p} must be a positive integer (got ${JSON.stringify(e[p])})`);return r},d=(e.URUN_DECISION_POOL_MIN??"").trim();if(d==="0")return null;let u=d?Number.parseInt(d,10):me;if(!Number.isInteger(u)||u<0)throw new Error(`URUN_DECISION_POOL_MIN must be a non-negative integer (got ${JSON.stringify(e.URUN_DECISION_POOL_MIN)})`);let l=a("URUN_DECISION_POOL_MAX",Math.max(u,ge));return{min:u,max:l,idleGraceMs:3e5,inFlightCap:a("URUN_DECISION_POOL_INFLIGHT_CAP",he),replicaId:(e.URUN_REPLICA_ID??"").trim()||s||"proxy"}}function Ve(e){let s=(e.URUN_DECISION_STREAM_IDLE_TIMEOUT_S??"").trim();if(!s)return _;let a=Number.parseInt(s,10);if(!Number.isInteger(a)||a<=0)throw new Error(`URUN_DECISION_STREAM_IDLE_TIMEOUT_S must be a positive integer (got ${JSON.stringify(e.URUN_DECISION_STREAM_IDLE_TIMEOUT_S)})`);return a*1e3}function ee(e,s,a,d){let u=e[a];if(typeof u!="function")throw new Error(`app "${s}" has no function "${a}" (set URUN_FUNCTION)`);let l=d?u(void 0,d):u();if(typeof l.end!="function")throw new Error(`app "${s}" function "${a}" returned a session without end()`);return l}function oe(e,s,a,d){let{baseUrl:u,orgId:l,auth:p}=e,f=s.fnName??e.fnName,t=e.sessionStartOptions?.(a),r=d||t||s.sharedApp?{...s.sharedApp?{sharedApp:!0}:{},...t,...d}:void 0,c=o=>({session:o,responses:new F(o),gone:Y(o,a)});if(p.lane==="jwt"){let o=z(s.appSlug,{baseUrl:u,orgId:l,jwt:p.jwt});return c(ee(o,s.appSlug,f,r))}let{apiKey:m,gatewayUrl:g}=p,w=(typeof e.subject=="function"?e.subject():e.subject)??fe(),n=z(s.appSlug,{baseUrl:u,orgId:l,getAccessToken:async()=>(await de(m,{baseUrl:g,expiresIn:300,allowedFunctions:[`${s.appSlug}/${f}`],subject:w,...s.sharedApp?{sharedApp:!0}:{}})).token});return c(ee(n,s.appSlug,f,r))}function we(e){let s=e.session.id;if(typeof s!="string"||s.length===0)throw new Error("pooled session exposes no stable session id \u2014 the session-identity seam requires @urun-sh/core Session (session.id)");return s}function Se(e){if(e.kind==="never-live")return!0;if(e.kind!=="create-failed")return!1;let s=e.status.error?.httpStatus,a=e.status.error?.code;return s===410||s===409&&(a==="cancelled"||a==="expired")?!0:a==="client_ended"}var ye=new Set(["error","paused"]);function je(e){let{auth:s,catalog:a}=e,d=null,u=(p,f)=>{let t=oe(e,p,f);return e.onSessionOpen&&typeof t.session.id=="string"&&t.session.id!==""&&e.onSessionOpen(f,t.session.id),t.gone.onGone(()=>{d?.evict(f,t)}),t},l=new V({sharedOnlyListing:e.sharedOnlyListing===!0,defaultApp:e.defaultApp,fnName:e.fnName,openSession:u,closeSession:async p=>{if(p.gone.dispose(),e.release==="detach"){if(typeof p.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');p.session.detach();return}await p.session.end()},listApps:s.lane==="api-key"?()=>W({apiUrl:e.apiUrl,apiKey:s.apiKey}):null,listCatalog:a?()=>M(a):null,listPrices:a?()=>G(a):null,shared:s.lane==="api-key"?()=>x({apiUrl:e.apiUrl,apiKey:s.apiKey}):null,sessionKey:we});return d=l,e.defaultApp!==null&&l.seed(e.defaultApp,u({appSlug:e.defaultApp},E({appSlug:e.defaultApp}))),l}function Be(e,s){let a=e.decisionPool;if(!a)throw new Error("wireDecisionPool called without BackhaulConfig.decisionPool");let d=a.replicaId??"proxy",u=new Map,l=new Map,p=t=>{process.stderr.write(`[decision-pool] ${t}
|
|
2
|
-
`)},f=(t,r)=>{let c=u.get(r);if(c)return c;let m=new B({min:a.min,max:a.max,idleGraceMs:a.idleGraceMs,inFlightCap:a.inFlightCap,replicaId:d,openSlot:async g=>{let w=oe(e,t,r,{concurrencyKey:g.concurrencyKey});return w.gone.onGone(()=>m.evict(w)),await b(w.session),p(`slot attached pool=${r} slot=${g.concurrencyKey} session=${w.session.id??"(no id)"}`),w},closeSlot:async g=>{g.gone.dispose(),await g.session.end()},onDetach:(g,w)=>p(`slot detached pool=${r} slot=${g.concurrencyKey} reason=${w}`)});return u.set(r,m),m};return{async boot(){if(e.defaultApp===null)return;let t={appSlug:e.defaultApp};await f(t,E(t)).boot()},async sessionFor(t){let r=await s.resolveTargetFor(t),c=E(r),m=f(r,c).tryAcquireReady();if(!m)throw new j;let g=l.get(m.entry)??[];return g.push(m),l.set(m.entry,g),{app:c,entry:m.entry,identity:H(r)}},async warmFor(t){let r=await s.resolveTargetFor(t);await f(r,E(r)).boot()},end(t){let r=l.get(t)?.shift();r&&(l.get(t)?.length===0&&l.delete(t),r.release())},evict(t){for(let r of u.values())r.evict(t)},reap(){for(let t of u.values())t.reap()},async pingReady(t){for(let r of u.values())if(await r.pingReady(t))return!0;return!1},stats(){return[...u.entries()].map(([t,r])=>({poolKey:t,...r.stats()}))},async drain(t){await Promise.all([...u.values()].map(r=>r.drain({timeoutMs:t})))},async close(){await Promise.all([...u.values()].map(t=>t.close()))}}}function ve(e){return pe(e?.name)?e?.runtime?.wake?.expectedWakePath==="portable-golden-restore"?Z:ue:Z}async function b(e,s){let a=e.whenLive;if(typeof a!="function")throw new Error("pooled session exposes no native whenLive() \u2014 the audio lane must wait for the session's own admission (cold wake: queued \u2192 demand \u2192 promote) before wiring media; a session without the core Session.whenLive surface cannot be served audio");await a.call(e,{timeout:ve(e.phase),signal:s})}async function ne(e){if(typeof e.cancel=="function")try{await e.cancel();return}catch{}if(typeof e.detach!="function")throw new Error("pooled session exposes neither cancel() nor detach() \u2014 the client-gone abandon path requires the @urun-sh/core Session native lifecycle surface");e.detach()}function O(e){return e.reason instanceof Error?e.reason:new Error("the client socket closed")}function Ke(e,s=N,a,d,u=_){let l=new WeakMap,p=new WeakMap,f=new WeakMap,t=new WeakMap,r=new WeakMap,c=n=>{let o=n.session.id;return typeof o=="string"&&o.length>0?o:null},m=n=>{let o=l.get(n);if(!o){let i={waiting:0,gone:new AbortController};r.set(n,i),o=(async()=>{if(await b(n.session,i.gone.signal),i.gone.signal.aborted)throw O(i.gone.signal);r.get(n)===i&&r.delete(n);let h=await R(n.session,await s());return Object.assign(h,{sessionId:n.session.id})})(),l.set(n,o),o.catch(()=>{l.get(n)===o&&l.delete(n)}),o.then(()=>{r.get(n)===i&&r.delete(n)},()=>{r.get(n)===i&&r.delete(n)})}return o},g=n=>{let o=t.get(n);return o||(o=(async()=>{let i=await m(n);return{appendInputAudio:h=>i.appendInputAudio(h),onOutputAudio:h=>i.onOutputAudio(h),sessionId:c(n)}})(),t.set(n,o),o.catch(()=>{t.get(n)===o&&t.delete(n)})),o},w=async(n,o,i)=>{if(i?.aborted){let S=r.get(n);throw(l.get(n)===void 0||S!==void 0&&S.waiting===0)&&await ne(n.session),O(i)}let h=o(n),y=r.get(n);return y!==void 0&&y.waiting++,i===void 0?h:new Promise((S,P)=>{let T=()=>{y!==void 0&&--y.waiting===0&&r.get(n)===y&&(y.gone.abort(),r.delete(n),l.delete(n),t.delete(n),ne(n.session).catch(()=>{})),P(O(i))};i.addEventListener("abort",T,{once:!0});let k=()=>i.removeEventListener("abort",T);h.then(I=>{k(),S(I)},I=>{k(),P(I)})})};return{createResponse:Q({sessionFor:d?n=>d.sessionFor(n):n=>e.sessionFor(n),sessionIdOf:n=>c(n),evict:(n,o)=>{d?d.evict(o):e.evict(n,o)},onAdmissionVerdict:(n,o,i)=>{Se(i)&&(d?d.evict(o):e.evict(n,o),a?.(n,i.kind==="never-live"?"never-live":"create-failed"))},createStream:async(n,o)=>{try{await b(n.session);let i=K(C(await n.responses.responses.create(o),n.gone),u);if(!d)return i;let h=n;return(async function*(){try{yield*i}catch(S){throw q(S)&&d.evict(h),S}finally{d.end(h)}})()}catch(i){throw d&&d.end(n),i}}}),listModels:()=>e.modelList(),openRouterModels:()=>e.openRouterModels(),supportsImages:(n,o)=>e.supportsImages(n,o),listImageModels:()=>e.imageModelList(),catalogIdentity:n=>e.catalogIdentityFor(n),servingAppSlug:async n=>await e.servingAppSlugFor(n),supportsAudio:n=>e.supportsAudio(n),supportsSpeech:n=>e.supportsSpeech(n),speechVoices:n=>e.speechVoices(n),supportsTranscription:n=>e.supportsTranscription(n),transcriptStreamFor:n=>e.transcriptStreamFor(n),listSpeechToTextModels:()=>e.speechToTextModelList(),openAudio:async(n,o)=>{let{entry:i}=await e.sessionFor(n);return await w(i,m,o)},openAudioOn:async(n,o)=>{let{entry:i}=await e.sessionForHandle(n);return await w(i,g,o)},openTurnsOn:async function*(n,o){if(o?.aborted)throw O(o);let{entry:i}=await e.sessionForHandle(n);yield*$(i.session,o)},openVideo:async n=>{let{entry:o}=await e.sessionFor(n),i=p.get(o);return i||(i=D(o.session),p.set(o,i)),i},openVideoOut:async n=>{let{entry:o}=await e.sessionFor(n),i=f.get(o);return i||(i=U(o.session),f.set(o,i)),i},sessionHandle:async n=>(await e.handleFor(n)).handle,speechSessionHandle:async n=>(await e.speechHandleFor(n)).handle,evictSpeechSession:async n=>{let{app:o,entry:i}=await e.sessionForHandle(n);await e.evict(o,i)},createResponseOn:async(n,o)=>{let{entry:i}=await e.sessionForHandle(n);return await b(i.session),C(await i.responses.responses.create(o),i.gone)},onSessionEnd:async(n,o)=>{let{entry:i}=await e.sessionForHandle(n),h=i.session;if(typeof h.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let y=!1;return h.onPhase(S=>{if(y||!ye.has(S.name))return;y=!0;let P=h.endsAt??null;o({timeLeftMs:P?Math.max(0,P.getTime()-Date.now()):null,reason:S.reason??`session ${S.name}`})})}}}export{He as a,Ve as b,je as c,Be as d,Ke as e};
|