@urun-sh/openai 0.5.6 → 0.6.1

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.
Files changed (52) hide show
  1. package/dist/{ResponsesClient-BE3-hx3m.d.ts → ResponsesClient-CSSrOYD8.d.ts} +5 -1
  2. package/dist/{ResponsesClient-DjZWjlFH.d.cts → ResponsesClient-_OZERUjH.d.cts} +9 -1
  3. package/dist/{chunk-7M6LY6DR.js → chunk-3JWYIKHM.js} +1 -1
  4. package/dist/{chunk-BSHT6RZZ.js → chunk-6M5JK4YC.js} +1 -1
  5. package/dist/chunk-76BZLCGT.js +1 -0
  6. package/dist/chunk-7V2JKPIG.js +1 -0
  7. package/dist/{chunk-5HKWNK3O.js → chunk-K23AZPI4.js} +1 -1
  8. package/dist/chunk-T2LNHDPS.js +7 -0
  9. package/dist/chunk-TMQJY56F.js +4 -0
  10. package/dist/chunk-WEBOBE7Z.js +58 -0
  11. package/dist/chunk-X3GLH2HQ.js +1 -0
  12. package/dist/gemini-live.cjs +2 -2
  13. package/dist/gemini-live.d.cts +75 -7
  14. package/dist/gemini-live.d.ts +30 -7
  15. package/dist/gemini-live.js +1 -1
  16. package/dist/hosted/bin.cjs +37 -35
  17. package/dist/hosted/bin.js +1 -1
  18. package/dist/hosted/index.cjs +33 -31
  19. package/dist/hosted/index.d.cts +70 -4
  20. package/dist/hosted/index.d.ts +21 -4
  21. package/dist/hosted/index.js +1 -1
  22. package/dist/index.cjs +1 -1
  23. package/dist/index.d.cts +4 -4
  24. package/dist/index.d.ts +4 -4
  25. package/dist/index.js +1 -1
  26. package/dist/pi-extension/index.cjs +7 -7
  27. package/dist/pi-extension/index.d.cts +2 -2
  28. package/dist/pi-extension/index.d.ts +2 -2
  29. package/dist/pi-extension/index.js +1 -1
  30. package/dist/pi-extension/standalone.cjs +53 -53
  31. package/dist/proxy/cli.cjs +48 -46
  32. package/dist/proxy/cli.js +15 -14
  33. package/dist/proxy/index.cjs +25 -24
  34. package/dist/proxy/index.d.cts +142 -25
  35. package/dist/proxy/index.d.ts +22 -11
  36. package/dist/proxy/index.js +1 -1
  37. package/dist/{responses-turn-Dr37N3kH.d.ts → responses-turn-EwxfRJJg.d.ts} +187 -7
  38. package/dist/{responses-turn-tNUHq5b0.d.cts → responses-turn-vMefRdgJ.d.cts} +707 -23
  39. package/dist/{translator-Bh0Bp_Ie.d.cts → translator-CO8W_hmJ.d.cts} +1 -1
  40. package/dist/{translator-BCoRFaTs.d.ts → translator-Ddd65sXR.d.ts} +1 -1
  41. package/dist/{types-lsVTbNcH.d.cts → types-CHPtJx6d.d.cts} +13 -0
  42. package/dist/{types-lsVTbNcH.d.ts → types-CHPtJx6d.d.ts} +4 -0
  43. package/dist/{video-out-CCksIcj8.d.cts → video-out-BsuLqlID.d.cts} +1 -1
  44. package/dist/{video-out-IGQ4YQ-c.d.ts → video-out-Cawtm7SF.d.ts} +1 -1
  45. package/package.json +4 -2
  46. package/dist/chunk-2T2YYBVX.js +0 -1
  47. package/dist/chunk-3WAJD62J.js +0 -4
  48. package/dist/chunk-FP4RSAIE.js +0 -1
  49. package/dist/chunk-HR5H6S7L.js +0 -1
  50. package/dist/chunk-NY23USZF.js +0 -57
  51. package/dist/chunk-VLRMJRLS.js +0 -6
  52. /package/dist/{chunk-5CCJUNH7.js → chunk-YSFSRI3D.js} +0 -0
@@ -1,4 +1,4 @@
1
- import { k as VideoOutRecord } from './video-out-CCksIcj8.cjs';
1
+ import { k as VideoOutRecord } from './video-out-BsuLqlID.cjs';
2
2
 
3
3
  /**
4
4
  * Gemini Live (BidiGenerateContent) ⇄ uRun Responses translation — PURE
@@ -1,4 +1,4 @@
1
- import { k as VideoOutRecord } from './video-out-IGQ4YQ-c.js';
1
+ import { k as VideoOutRecord } from './video-out-Cawtm7SF.js';
2
2
 
3
3
  declare const GEMINI_LIVE_PATH = "/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent";
4
4
 
@@ -3,6 +3,19 @@ interface UrunDocLike {
3
3
  set(patch: Record<string, unknown>): void;
4
4
  get(path?: string, defaultValue?: unknown): unknown;
5
5
  on(event: 'change', handler: (snapshot: Record<string, unknown>) => void): () => void;
6
+ /**
7
+ * Whether the provider's initial sync has completed (core
8
+ * `SessionDocumentImpl.synced`) — true also for local-only docs. Absent on
9
+ * legacy/test doubles that never model a doc plane; the turn path's
10
+ * first-write gate skips when the surface is absent (it cannot hold a gate
11
+ * it cannot observe).
12
+ */
13
+ synced?: boolean;
14
+ /**
15
+ * One-shot first-sync callback (core `SessionDocumentImpl.onSynced`).
16
+ * Returns an unsubscribe fn. Absent on legacy/test doubles.
17
+ */
18
+ onSynced?(handler: () => void): () => void;
6
19
  }
7
20
  interface UrunStreamLike {
8
21
  messages(): AsyncIterable<unknown>;
@@ -3,6 +3,10 @@ interface UrunDocLike {
3
3
  set(patch: Record<string, unknown>): void;
4
4
  get(path?: string, defaultValue?: unknown): unknown;
5
5
  on(event: 'change', handler: (snapshot: Record<string, unknown>) => void): () => void;
6
+
7
+ synced?: boolean;
8
+
9
+ onSynced?(handler: () => void): () => void;
6
10
  }
7
11
  interface UrunStreamLike {
8
12
  messages(): AsyncIterable<unknown>;
@@ -1,4 +1,4 @@
1
- import { b as UrunSessionLike } from './types-lsVTbNcH.cjs';
1
+ import { b as UrunSessionLike } from './types-CHPtJx6d.cjs';
2
2
 
3
3
  /**
4
4
  * The ONE native audio sample rate of the session transport: the SFU Opus leg
@@ -1,4 +1,4 @@
1
- import { b as UrunSessionLike } from './types-lsVTbNcH.js';
1
+ import { b as UrunSessionLike } from './types-CHPtJx6d.js';
2
2
 
3
3
  declare const NATIVE_AUDIO_SAMPLE_RATE = 24000;
4
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urun-sh/openai",
3
- "version": "0.5.6",
3
+ "version": "0.6.1",
4
4
  "description": "OpenAI-compatible Realtime + Responses SDK over uRun session primitives (not websockets).",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -70,7 +70,7 @@
70
70
  "build:bin": "bun build --compile --external @roamhq/wrtc src/proxy/bin.ts --outfile dist/bin/urun-openai && node scripts/assert-bin-transport.mjs"
71
71
  },
72
72
  "peerDependencies": {
73
- "@urun-sh/core": "^0.5.6"
73
+ "@urun-sh/core": "^0.6.1"
74
74
  },
75
75
  "dependencies": {
76
76
  "@prometheus-io/client": "^0.16.1",
@@ -87,6 +87,8 @@
87
87
  "@types/node": "^24.0.0",
88
88
  "@types/ws": "^8.18.1",
89
89
  "@urun-sh/core": "workspace:*",
90
+ "ajv": "^8.17.1",
91
+ "ajv-formats": "^3.0.1",
90
92
  "esbuild": "^0.28.1",
91
93
  "happy-dom": "^20.9.0",
92
94
  "opusscript": "^0.1.1",
@@ -1 +0,0 @@
1
- import{c as p}from"./chunk-5CCJUNH7.js";p();function g(e,t,r,n){let o=e.response??{},i={request_id:r,consumer_id:n,stream:!0,kind:"chat",messages:t};return typeof o.instructions=="string"&&(i.instructions=o.instructions),Array.isArray(o.modalities)&&(i.modalities=o.modalities),typeof o.temperature=="number"&&(i.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(i.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(i.tools=o.tools),i}function _(e,t,r){let n={request_id:t,consumer_id:r,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(n.model=e.model),e.tools!==void 0&&(n.tools=e.tools),e.tool_choice!==void 0&&(n.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(n.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(n.max_output_tokens=e.max_output_tokens),typeof e.top_p=="number"&&(n.top_p=e.top_p),Array.isArray(e.stop)&&e.stop.length>0&&(n.stop=e.stop),typeof e.reasoning_effort=="string"&&(n.reasoning_effort=e.reasoning_effort),e.chat_template_kwargs!==void 0&&(n.chat_template_kwargs=e.chat_template_kwargs),e.response_format!==void 0&&(n.response_format=e.response_format),n}p();function f(e){if(e instanceof Error)return{type:"error",error:{type:"urun_error",code:e.name||null,message:e.message}};if(e&&typeof e=="object"&&e.t==="error"){let t=e,r=t.body??{},n=r.error,o=n?.message??r.message??`unreadable serve error body: ${JSON.stringify(t.body??null)}`;return{type:"error",error:{type:n?.type??"urun_error",code:t.code??n?.code??null,message:o}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}p();var k="llm-resp";async function*m(e,t){let r=`${k}:${t}`,n=`resp_${t}`;yield{type:"response.created",response:{id:n,status:"in_progress"}};let o=new Map;for await(let i of e.stream(r).messages()){let s=i;if(s.t==="delta"){if(typeof s.delta=="string"&&(yield{type:"response.output_text.delta",item_id:n,delta:s.delta}),typeof s.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:n,delta:s.reasoning}),Array.isArray(s.tool_calls))for(let u of s.tool_calls){let l=typeof u.index=="number"?u.index:0,a=o.get(l)??{};u.id&&(a.id=u.id),u.function?.name&&(a.name=u.function.name),o.set(l,a),yield{type:"response.function_call_arguments.delta",item_id:`fc_${t}_${l}`,tool_index:l,call_id:a.id,name:a.name,delta:u.function?.arguments??""}}}else if(s.t==="response"){yield{type:"response.completed",response:{id:n,status:"completed",...s.body}};return}else if(s.t==="error"){yield f(s);return}}}p();var w="llm",y=class{constructor(t){this.session=t;this.sessionTag=t.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(t){t.session_tag=this.sessionTag,this.session.doc(w).set({requests:{[t.request_id]:{payload:t,consumer_id:t.consumer_id,stream:t.stream}}})}sendResponses(t,r){let n=_(t,r,this.consumerId);return this.write(n),m(this.session,r)}sendResponseCreate(t,r,n){let o=g(t,r,n,this.consumerId);return this.write(o),m(this.session,n)}};export{g as a,_ as b,f as c,m as d,y as e};
@@ -1,4 +0,0 @@
1
- import{c as h}from"./chunk-5CCJUNH7.js";h();h();var p=class extends Error{};function ee(o){if(o===null||typeof o!="object")return null;let t=o.shared;if(t==null)return null;if(typeof t!="object")throw new p(`catalog "shared" block is ${typeof t}, expected an object`);let{shared_org_id:e,endpoints:n}=t;if(typeof e!="string"||e==="")throw new p(`catalog "shared" block has no shared_org_id: ${JSON.stringify(t)}`);if(!Array.isArray(n))throw new p(`catalog "shared" block endpoints is not an array: ${JSON.stringify(t)}`);let r=n.map((i,a)=>{if(i===null||typeof i!="object")throw new p(`catalog shared endpoints[${a}] is not an object: ${JSON.stringify(i)}`);let s=i;for(let d of["model_id","variant","app_slug","function"])if(typeof s[d]!="string"||s[d].length===0)throw new p(`catalog shared endpoints[${a}] (${JSON.stringify(s)}) has no valid "${d}"`);return s});return r.length===0?null:{shared_org_id:e,endpoints:r}}function S(o){return[...o.endpoints].sort((t,e)=>t.model_id.localeCompare(e.model_id)||t.variant.localeCompare(e.variant))}function N(o,t,e){let n=(t??"").trim();if(n==="")return null;let r=n,i=e,a=n.lastIndexOf(":");a>0&&(r=n.slice(0,a),i=n.slice(a+1));let s=o.endpoints.filter(l=>l.model_id===r);if(s.length===0)return null;if(i!==void 0){let l=s.find(u=>u.variant===i);return l?{shared_org_id:o.shared_org_id,endpoint:l}:null}if(s.length===1)return{shared_org_id:o.shared_org_id,endpoint:s[0]};let d=s.filter(l=>l.compat_default===!0);if(d.length!==1)throw new p(`shared model "${r}" has ${d.length} compat_default rows (exactly one required) among variants ${s.map(l=>l.variant).join(", ")}`);return{shared_org_id:o.shared_org_id,endpoint:d[0]}}async function ye(o){let t=o.fetchImpl??fetch,e=`${o.apiUrl.replace(/\/+$/,"")}/catalog`,n=await t(e,{headers:{Authorization:`Bearer ${o.apiKey}`,Accept:"application/json"}});if(!n.ok)throw new p(`catalog read failed: GET ${e} \u2192 ${n.status}`);return ee(await n.json())}h();function w(o){return o.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}h();var g=class extends Error{name="ModelPriceError"},M="per_million_input_tokens",A="per_million_output_tokens",j="model_id,variant,unit,price_usd,effective_at,expires_at",D={per_request:!0,per_minute:!0,per_output_unit:!0},te=5e3;function ne(o,t){let e=typeof o=="number"?o:typeof o=="string"&&o.trim()!==""?Number(o):NaN;if(!Number.isFinite(e)||e<0)throw new g(`${t}: price_usd is not a non-negative number (${JSON.stringify(o)})`);return e}function F(o,t,e){let n=typeof o=="string"?Date.parse(o):NaN;if(!Number.isFinite(n))throw new g(`${t}: ${e} is not a parseable timestamp (${JSON.stringify(o)})`);return n}function oe(o,t,e){return o===null?null:F(o,t,e)}function re(o,t){if(o===M)return"input";if(o===A)return"output";if(typeof o=="string"&&Object.hasOwn(D,o))return null;throw new g(`${t}: unit ${JSON.stringify(o)} is neither a per-token rate (${M} | ${A}) nor a known GPU-time unit (${Object.keys(D).join(" | ")}). Skipping it would publish no prices forever with no signal \u2014 if ENG-317 named the per-token units differently, teach this module the new names instead`)}function ie(o,t){let e=o.reduce((r,i)=>i.effective_at>r.effective_at?i:r,o[0]),n=o.find(r=>r.effective_at===e.effective_at&&r.usd!==e.usd);return n?{reason:`two ${t} rates are effective at the same instant (${e.usd} and ${n.usd} USD/Mtok), so the winner would be read order \u2014 this model is served UNPRICED until one of them is expired; expire one of them`}:{rate:e}}function se(o){if(!Array.isArray(o))throw new g(`model_prices read is not an array: ${JSON.stringify(o)}`);let t=[];for(let e=0;e<o.length;e++){let n=o[e],r=`model_prices[${e}]`;if(n===null||typeof n!="object"||Array.isArray(n))throw new g(`${r} is not an object: ${JSON.stringify(n)}`);let i=n;for(let l of j.split(","))if(!Object.hasOwn(i,l))throw new g(`${r} has no "${l}" column: ${JSON.stringify(i)}`);let a=re(i.unit,r);if(a===null)continue;let s=i.model_id;if(typeof s!="string"||s==="")throw new g(`${r} has no model_id (${JSON.stringify(i)})`);let d=i.variant;if(!(d===null||typeof d=="string"&&d!==""))throw new g(`${r} has a non-string variant (${JSON.stringify(d)})`);t.push({model_id:s,variant:d,side:a,usd:ne(i.price_usd,r),effective_at:F(i.effective_at,r,"effective_at"),expires_at:oe(i.expires_at,r,"expires_at")})}return t}function L(o,t=Date.now()){let e=new Map,n=new Map;for(let s of o){if(s.effective_at>t||s.expires_at!==null&&s.expires_at<=t)continue;let d=e.get(s.model_id);d||(d=new Map,e.set(s.model_id,d),n.set(s.model_id,new Map));let l=s.variant??"",u=d.get(l);u||(u={input:[],output:[]},d.set(l,u),n.get(s.model_id).set(l,s.variant)),u[s.side].push({effective_at:s.effective_at,usd:s.usd})}let r=[],i=[];for(let[s,d]of e){let l=d.get(""),u=(y,f)=>{let m=y&&y.length>0?y:l?.[f];if(!m||m.length===0)return null;let c=ie(m,f);return"reason"in c?c:c.rate.usd===0?{reason:`the ${f} rate in force is zero, which publishes as "free" and would top :cheapest routing \u2014 this model is served UNPRICED; delete the row if the model is not token-priced, or set a real rate`}:c.rate};for(let[y,f]of n.get(s)){let m=d.get(y),c=u(m?.input,"input"),v=u(m?.output,"output");if(c!==null&&"reason"in c){i.push({model_id:s,variant:f,reason:c.reason});continue}if(v!==null&&"reason"in v){i.push({model_id:s,variant:f,reason:v.reason});continue}c===null||v===null||r.push({model_id:s,variant:f,pricing:{input:c.usd,output:v.usd}})}}let a=(s,d)=>s.model_id.localeCompare(d.model_id)||(s.variant===null?1:0)-(d.variant===null?1:0)||(s.variant??"").localeCompare(d.variant??"");return{prices:r.sort(a),refusals:i.sort(a)}}function R(o,t,e){if(e.refusals.some(i=>i.model_id===o&&i.variant===t))return null;let n=e.prices.find(i=>i.model_id===o&&i.variant===t);if(n)return n.pricing;if(e.refusals.some(i=>i.model_id===o&&i.variant===null))return null;let r=e.prices.find(i=>i.model_id===o&&i.variant===null);return r?r.pricing:null}var P={prices:[],refusals:[]};async function $e(o){let t=o.fetchImpl??fetch,e=`${M},${A}`,n=await t(`${o.catalogUrl}/model_prices?select=${j}&unit=in.(${e})`,{headers:{apikey:o.anonKey,Authorization:`Bearer ${o.anonKey}`},signal:AbortSignal.timeout(te)});if(!n.ok)throw new Error(`model_prices fetch failed: ${n.status}`);return se(await n.json())}h();var ae={chat:!0,code:!0,agent:!0,vl:!0},U={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function le(o){let t=o.toLowerCase();return Object.hasOwn(U,t)?U[t]:null}var de=["context_length","max_model_len"];function ue(o){let t=de.map(e=>o.engine_args?.[e]).filter(e=>typeof e=="number"&&Number.isFinite(e)&&e>0);return t.length===0?null:t.every(e=>e===t[0])?t[0]:null}function x(o){let t=o.map(ue),e=t[0];return e==null?null:t.every(n=>n===e)?e:null}function pe(o,t){let e=t.filter(r=>w(`${r.model_id}-${r.variant}`)===o);if(e.length>0)return O(e);let n=t.filter(r=>w(r.model_id)===o);return n.length>0?O(n):null}function O(o){let t=o[0];return o.every(n=>n.model_id===t.model_id&&n.variant===t.variant)?{row:o.length===1?t:ce(o),matches:o}:null}function ce(o){let t=o[0],e=n=>o.every(r=>(r[n]??void 0)===(t[n]??void 0))?t[n]:void 0;return{model_id:t.model_id,variant:t.variant,gpu_spec:e("gpu_spec"),engine:e("engine"),task:e("task"),description:e("description"),size_gb:e("size_gb"),hf_repo:e("hf_repo"),updated_at:e("updated_at")}}function J(o){let t=o.updated_at?Date.parse(o.updated_at):NaN;return Number.isFinite(t)?Math.floor(t/1e3):0}function T(o,t,e){let n=pe(o,t);if(!n)return{id:o,object:"model",created:0,owned_by:"urun"};let{row:r,matches:i}=n,a=x(i),s=R(r.model_id,r.variant,e);return{id:o,object:"model",created:J(r),owned_by:"urun",name:`${r.model_id}:${r.variant}`,...r.description?{description:r.description}:{},...r.task?{task:r.task}:{},...r.engine?{engine:r.engine}:{},...r.gpu_spec?{gpu_spec:r.gpu_spec}:{},...a!==null?{context_length:a}:{},...s!==null?{pricing:s}:{}}}function B(o){let t=/^(\d+)(?:\.(\d+))?(?:e([+-]?\d+))?$/.exec(String(o));if(!t)throw new Error(`unrenderable per-token price: ${String(o)}`);let[,e="",n="",r=""]=t,i=e+n,a=e.length-6+(r===""?0:Number(r));return(a<=0?`0.${"0".repeat(-a)}${i}`:`${i.slice(0,a)}.${i.slice(a).padEnd(1,"0")}`).replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,"")}function K(o){return{input:[{type:"prompt",unit:"token",cost_usd:B(o.input)}],output:[{type:"completion",unit:"token",cost_usd:B(o.output)}]}}function ge(o,t){if(o.length===0)return null;let e=O(o);if(!e)return null;let{row:n,matches:r}=e;if(!n.task||ae[n.task]!==!0)return null;let i=x(r),a=R(n.model_id,n.variant,t),s={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};return i!==null&&(s.max_tokens={type:"integer",min:1,max:i,unit:"token"}),{schema_version:"2.4",id:`${n.model_id}:${n.variant}`,name:`${n.model_id}:${n.variant}`,created:J(n),quantization:le(n.variant),description:n.description??"",hugging_face_id:n.hf_repo?n.hf_repo.split("@")[0]??n.hf_repo:"",input_modalities:[{type:"text",...i!==null?{supported_inputs:{max_context_length:{value:i,unit:"token"}}}:{},...a?{pricing:K(a).input}:{}},...n.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:s,...a?{pricing:K(a).output}:{}}]}}function z(o,t,e){let n=[],r=new Set;for(let i of o){let a=`${i.model_id}:${i.variant}`;if(r.has(a))continue;r.add(a);let s=ge(t.filter(d=>d.model_id===i.model_id&&d.variant===i.variant),e);s&&n.push(s)}return{data:n}}var Ne="https://api.urun.sh/v1",_=class extends Error{},b=class extends Error{},I="urs1.";function fe(o,t){return I+Buffer.from(JSON.stringify([o,t]),"utf8").toString("base64url")}function he(o){if(!o.startsWith(I))return null;try{let t=JSON.parse(Buffer.from(o.slice(I.length),"base64url").toString("utf8"));if(Array.isArray(t)&&typeof t[0]=="string"&&typeof t[1]=="string")return{app:t[0],key:t[1]}}catch{}return null}async function De(o){let t=o.fetchImpl??fetch,e=`${o.apiUrl.replace(/\/+$/,"")}/apps`,n=await t(e,{headers:{Authorization:`Bearer ${o.apiKey}`,Accept:"application/json"}});if(!n.ok)throw new Error(`org apps listing failed: GET ${e} \u2192 ${n.status}`);let r=await n.json();if(!Array.isArray(r.apps))throw new Error(`org apps listing returned no "apps" array (GET ${e})`);if(r.truncated===!0)throw new Error(`org apps listing was truncated (GET ${e} returned ${r.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return r.apps}function H(o){if(o.length===0)return[];if(!o.every(e=>(e.task??void 0)==="image"))return[];let t=we(o);return t===null?[]:[t]}function q(o,t,e,n,r){if(!r)throw new p(`shared model "${o}" cannot be gated: no catalog oracle is configured, and a shared endpoint's modality must come from the model's OWN model_catalog row \u2014 never a default`);if(t===void 0||e===void 0)throw new p(`shared model "${o}" resolved without a catalog id + variant to join on`);let i=n.filter(a=>a.model_id===t&&a.variant===e);if(i.length===0)throw new p(`shared model "${o}" names endpoint ${t}:${e} but the catalog has no model_catalog row for it \u2014 the shared block cannot vouch a modality from nothing`);return i}function G(o,t){if(!t.every(r=>(r.task??void 0)==="image"))return[];let e=t.map(r=>r.engine_args?.expects_image),n=e[0];if(typeof n!="boolean"||!e.every(r=>r===n))throw new p(`shared image model "${o}" does not vouch an explicit image mode: its ${t.length} model_catalog row(s) carry engine_args.expects_image ${JSON.stringify(e)} \u2014 the shared lane requires a boolean (false = pure text-to-image generation, true = edit) and refuses to silently default to edit`);return[n===!1?"generation":"edit"]}var Z="shared:";function W(o){return o.sharedApp?Z+o.appSlug:o.appSlug}function X(o){return typeof o.modelId=="string"&&typeof o.variant=="string"?{kind:"catalog",model_id:o.modelId,variant:o.variant}:{kind:"not-a-catalog-model",model:o.appSlug}}function E(o,t){let e=t.filter(i=>w(`${i.model_id}-${i.variant}`)===o);if(e.length>0)return e;let n=t.filter(i=>w(i.model_id)===o);return new Set(n.map(i=>i.variant)).size===1?n:[]}function me(o){let t=o.engine_args?.expects_image;return t==null?"edit":t===!1?"generation":t===!0?"edit":null}function we(o){let t=o.map(me),e=t[0];return e!=null&&t.every(n=>n===e)?e:null}var V={stt:"stt",tts:"tts"};function Q(o){if(o.length===0)return null;let t=o[0].task??void 0;return t===void 0||V[t]===void 0?null:o.every(e=>(e.task??void 0)===t)?V[t]:null}var C=6e4,Y=class{constructor(t){this.opts=t}opts;pool=new Map;appsCache=null;rowsCache=null;seed(t,e){this.pool.set(t,Promise.resolve(e))}async deployedApps(){if(!this.opts.listApps)return[];let t=this.opts.appsTtlMs??C;if(this.appsCache&&Date.now()-this.appsCache.at<t)return this.appsCache.apps;let e=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:e},e}async sharedBlock(){if(!this.opts.shared)return null;let t=this.opts.appsTtlMs??C;if(this.sharedCache&&Date.now()-this.sharedCache.at<t)return this.sharedCache.block;let e=await this.opts.shared();return this.sharedCache={at:Date.now(),block:e},e}sharedCache=null;async catalogRows(){if(!this.opts.listCatalog)return[];let t=this.opts.appsTtlMs??C;if(this.rowsCache&&Date.now()-this.rowsCache.at<t)return this.rowsCache.rows;let e=await this.opts.listCatalog();return this.rowsCache={at:Date.now(),rows:e},e}pricesCache=null;async priceRows(){if(!this.opts.listPrices)return[];let t=this.opts.appsTtlMs??C;if(this.pricesCache&&Date.now()-this.pricesCache.at<t)return this.pricesCache.rows;let e=await this.opts.listPrices();return this.pricesCache={at:Date.now(),rows:e},e}async pricesForListing(){let t;try{t=await this.priceRows()}catch(n){if(n instanceof g)throw n;return this.warn(`urun /v1/models: the per-token price surface could not be read (${String(n instanceof Error?n.message:n)}) \u2014 every model is listed UNPRICED for now; Hugging Face will show no price until the read succeeds
2
- `),P}let e=L(t,Date.now());for(let n of e.refusals)this.warn(`urun /v1/models: refusing to price ${n.model_id}:${n.variant??"*"} \u2014 ${n.reason}
3
- `);return e}warn(t){(this.opts.warn??(n=>console.warn(n.trimEnd())))(t)}servable(t){return t.filter(e=>e.function_name===this.opts.fnName&&e.deployment_status==="active").map(e=>e.app_slug)}async availableIds(){let t=this.opts.defaultApp,e=new Set(t===null?[]:[t]);if(this.opts.listApps)for(let r of this.servable(await this.deployedApps()))e.add(r);for(let r of this.pool.keys())r.startsWith(Z)||e.add(r);let n=[...e].filter(r=>r!==t).sort();return t===null?n:[t,...n]}async noDefaultApp(t){let e=await this.availableIds(),n=e.length>0?e.join(", "):"(none deployed)";throw new _(t===""?`no "model" was given and this endpoint has no default model \u2014 name one of your org's deployed "${this.opts.fnName}" apps; available models: ${n}`:`model "${t}" does not name a deployed "${this.opts.fnName}" app in your org, and this endpoint has no default model to fall back to; available models: ${n}`)}async resolveTarget(t){let e=(t??"").trim(),n=this.opts.defaultApp;if(!e||e==="urun")return n===null?this.noDefaultApp(e):{appSlug:n};let r=w(e);if(r===n)return{appSlug:n};if(this.pool.has(r))return{appSlug:r};if(this.opts.listApps){let i=await this.deployedApps(),a=this.servable(i);if(a.includes(r))return{appSlug:r};let s=a.filter(l=>l.startsWith(`${r}-`));if(s.length===1)return{appSlug:s[0]};if(s.length>1)throw new _(`model "${e}" is ambiguous across deployed apps ${s.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let d=i.find(l=>l.app_slug===r||l.app_slug.startsWith(`${r}-`));if(d)throw new _(`model "${e}" maps to app "${d.app_slug}", which is not an active "${this.opts.fnName}" app \u2014 deploy it with \`urun serve ${e}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.shared){let i=await this.sharedBlock();if(i){let a=N(i,e);if(a)return{appSlug:a.endpoint.app_slug,fnName:a.endpoint.function,sharedApp:!0,modelId:a.endpoint.model_id,variant:a.endpoint.variant}}}if(this.opts.listCatalog){let a=(await this.opts.listCatalog()).find(s=>e===s.model_id||e===`${s.model_id}:${s.variant}`||r===w(s.model_id)||r===w(`${s.model_id}-${s.variant}`));if(a)throw new _(`model "${e}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${a.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return n===null?this.noDefaultApp(e):{appSlug:n}}async catalogIdentityFor(t){let e=X(await this.resolveTarget(t));return e.kind==="catalog"?{model_id:e.model_id,variant:e.variant}:null}async servingAppSlugFor(t){return(await this.resolveTarget(t)).appSlug}async resolveApp(t){return W(await this.resolveTarget(t))}async sessionFor(t){let e=await this.resolveTarget(t),n=W(e),r=this.pool.get(n);return r||(r=Promise.resolve(this.opts.openSession(e)),this.pool.set(n,r),r.catch(()=>this.pool.delete(n))),{app:n,entry:await r,identity:X(e)}}keyOf(t){let e=this.opts.sessionKey;if(!e)throw new Error("ModelRouter: sessionKey is not configured \u2014 the session-identity seam (handleFor/sessionForHandle) is unavailable on this router");let n=e(t);if(typeof n!="string"||n.length===0)throw new Error("ModelRouter: sessionKey returned an empty identity for a pooled session");return n}async handleFor(t){let{app:e,entry:n}=await this.sessionFor(t);return{app:e,handle:fe(e,this.keyOf(n))}}async sessionForHandle(t){let e=he(t);if(!e)throw new b(`malformed session handle ${JSON.stringify(t)} \u2014 not issued by this proxy`);let{app:n,key:r}=e,i=this.pool.get(n),a=i?await i.then(s=>s,()=>null):null;if(a===null)throw new b(`the session behind this handle (app "${n}") is gone \u2014 closed, evicted after its pod died, or the proxy restarted; it cannot be reattached. Reconnect without a handle to start a new session.`);if(this.keyOf(a)!==r)throw new b(`the session behind this handle (app "${n}") was replaced \u2014 the original backhaul died and a new session serves this app now; the handle's session cannot be reattached. Reconnect without a handle to start a new session.`);return{app:n,entry:a}}async evict(t,e){let n=this.pool.get(t);!n||await n.then(i=>i,()=>null)!==e||this.pool.get(t)!==n||(this.pool.delete(t),await this.opts.closeSession(e).catch(()=>{}))}async modelList(){let t=await this.availableIds(),[e,n]=await Promise.all([this.catalogRows().then(l=>({ok:!0,rows:l}),()=>({ok:!1,rows:[]})),this.pricesForListing()]),r=e.rows,i=e.ok?n:P;e.ok||this.warn(`urun /v1/models: the model catalog could not be read \u2014 entries fall back to the bare OpenAI fields AND no model is priced on either lane (pricing follows catalog identity, so a half-priced listing would flicker on Hugging Face)
4
- `);let a=await this.sharedBlock(),d=(a?S(a):[]).map(l=>{let u={id:`${l.model_id}:${l.variant}`,object:"model",created:0,owned_by:"urun",name:`${l.model_id}:${l.variant}`};l.gpu_spec&&(u.gpu_spec=l.gpu_spec),typeof l.warm=="number"&&(u.warm=l.warm);let y=r.filter(c=>c.model_id===l.model_id&&c.variant===l.variant),f=x(y);f!==null&&(u.context_length=f);let m=R(l.model_id,l.variant,i);return m!==null&&(u.pricing=m),u});return{object:"list",data:[...t.map(l=>T(l,r,i)),...d]}}async openRouterModels(){let[t,e]=await Promise.all([this.catalogRows(),this.pricesForListing()]),n=await this.sharedBlock();return z(n?S(n):[],t,e)}async supportsImages(t,e){let n=await this.resolveTarget(t),r=await this.catalogRows();if(n.sharedApp){let i=q(String(t),n.modelId,n.variant,r,this.opts.listCatalog!==null);return G(`${n.modelId}:${n.variant}`,i).includes(e)}return H(E(n.appSlug,r)).includes(e)}async supportsAudio(t){let e=await this.resolveTarget(t),n=await this.catalogRows();return e.sharedApp?Q(n.filter(r=>r.model_id===e.modelId&&r.variant===e.variant))!==null:Q(E(e.appSlug,n))!==null}async imageModelList(){let t=await this.catalogRows(),e=P,n=[];for(let i of await this.availableIds()){let a=H(E(i,t));a.length!==0&&n.push({...T(i,t,e),image_modes:a})}let r=await this.sharedBlock();if(r)for(let i of S(r)){let a=q(`${i.model_id}:${i.variant}`,i.model_id,i.variant,t,this.opts.listCatalog!==null),s=G(`${i.model_id}:${i.variant}`,a);if(s.length===0)continue;let d={id:`${i.model_id}:${i.variant}`,object:"model",created:0,owned_by:"urun",name:`${i.model_id}:${i.variant}`,image_modes:s};i.gpu_spec&&(d.gpu_spec=i.gpu_spec),typeof i.warm=="number"&&(d.warm=i.warm),n.push(d)}return{object:"list",data:n}}async closeAll(){let t=[...this.pool.values()],e=[];if(await Promise.all(t.map(async n=>{try{await this.opts.closeSession(await n)}catch(r){e.push(String(r instanceof Error?r.message:r))}})),e.length>0)throw new Error(`failed to close ${e.length} pooled session(s): ${e.join("; ")}`)}};export{ye as a,$e as b,Ne as c,_ as d,b as e,De as f,W as g,Y as h};
@@ -1 +0,0 @@
1
- import{e as r}from"./chunk-2T2YYBVX.js";import{c as e}from"./chunk-5CCJUNH7.js";e();var o=0;function i(){return o+=1,`req_${Date.now().toString(36)}_${o.toString(36)}`}var s=class{transport;constructor(n){this.transport=new r(n)}responses={create:async n=>{let t=i(),a=this.transport.sendResponses(n,t);return Object.assign((async function*(){yield*a})(),{requestId:t})}}};export{s as a};
@@ -1 +0,0 @@
1
- import{a as P}from"./chunk-5HKWNK3O.js";import{a as A}from"./chunk-FP4RSAIE.js";import{a as E,b as R,e as m,f as C,g as y,h as x}from"./chunk-3WAJD62J.js";import{c as v,f as k,g as O,p as b}from"./chunk-BSHT6RZZ.js";import{c as f}from"./chunk-5CCJUNH7.js";f();import{App as U,createClientToken as K}from"@urun-sh/core";f();import{SessionFailedError as D}from"@urun-sh/core";var w=2;function $(e){let s=e?.type;return s==="response.created"||s==="response.in_progress"}function _(e){let s=e?.type;return s==="response.completed"||s==="error"}function L(e){return async function(r,u){return(async function*(){for(let n=1;n<=w;n++){let{app:o,entry:t,identity:a}=await e.sessionFor(r.model);u?.({sessionId:e.sessionIdOf?.(t)??null,identity:a});let d=!1,i=!1;try{let p=await e.createStream(t,r);for await(let h of p){if(_(h)&&(d=!0),$(h)){if(n>1)continue}else i=!0;yield h}}catch(p){if(p instanceof D&&(p.kind==="never-live"||p.kind==="create-failed"))throw p;if(n<w&&!i){await e.evict(o,t);continue}throw p}if(d)return;if(n<w&&!i){await e.evict(o,t);continue}return}})()}}f();var N=3,B="llm",H=new Set(["paused","error"]);function F(e,s){let r=e.doc(B),u=typeof r.onConnectionState=="function",l=typeof e.onPhase=="function";if(!u&&!l)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 n=null,o=!1,t=new Set,a=[],d=i=>{if(!n){n=i;for(let p of[...t])p(i);t.clear()}};return u&&a.push(r.onConnectionState(i=>{o&&(i.consecutiveFailures<N||d(new m(`the pooled session for app "${s}" is gone: its doc backhaul failed ${i.consecutiveFailures} consecutive times (last close ${i.lastCloseCode===null?"by provider liveness reap":`code ${i.lastCloseCode}`}${i.lastCloseReason?`, reason ${JSON.stringify(i.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`)))})),l&&a.push(e.onPhase(i=>{if(i.name==="live"){o=!0;return}o&&H.has(i.name)&&d(new m(`the pooled session for app "${s}" lost its compute binding (phase "${i.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:()=>n,onGone(i){return n?(i(n),()=>{}):(t.add(i),()=>t.delete(i))},dispose(){t.clear();for(let i of a.splice(0))i()}}}async function*S(e,s){let r=s.gone();if(r)throw r;let u=e[Symbol.asyncIterator](),l,n=new Promise((o,t)=>{l=s.onGone(a=>t(a))});n.catch(()=>{});try{for(;;){let o=await Promise.race([u.next(),n]);if(o.done)return;yield o.value}}finally{l?.(),u.return?.(void 0)}}function M(e,s,r,u){let l=e[r];if(typeof l!="function")throw new Error(`app "${s}" has no function "${r}" (set URUN_FUNCTION)`);let n=u?l(void 0,u):l();if(typeof n.end!="function")throw new Error(`app "${s}" function "${r}" returned a session without end()`);return n}function j(e,s){let{baseUrl:r,orgId:u,auth:l,subject:n}=e,o=s.fnName??e.fnName,t=y(s),a=e.sessionStartOptions?.(t),d=s.sharedApp?{sharedApp:!0,...a}:a,i=c=>({session:c,responses:new A(c),gone:F(c,t)});if(l.lane==="jwt"){let c=U(s.appSlug,{baseUrl:r,orgId:u,jwt:l.jwt});return i(M(c,s.appSlug,o,d))}let{apiKey:p,gatewayUrl:h}=l,T=U(s.appSlug,{baseUrl:r,orgId:u,getAccessToken:async()=>(await K(p,{baseUrl:h,expiresIn:300,allowedFunctions:[`${s.appSlug}/${o}`],...n===void 0?{}:{subject:n},...s.sharedApp?{sharedApp:!0}:{}})).token});return i(M(T,s.appSlug,o,d))}function V(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}var W=new Set(["error","paused"]);function ue(e){let{auth:s,catalog:r}=e,u=null,l=o=>{let t=y(o),a=j(e,o);return e.onSessionOpen&&typeof a.session.id=="string"&&a.session.id!==""&&e.onSessionOpen(t,a.session.id),a.gone.onGone(()=>{u?.evict(t,a)}),a},n=new x({defaultApp:e.defaultApp,fnName:e.fnName,openSession:l,closeSession:async o=>{if(o.gone.dispose(),e.release==="detach"){if(typeof o.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');o.session.detach();return}await o.session.end()},listApps:s.lane==="api-key"?()=>C({apiUrl:e.apiUrl,apiKey:s.apiKey}):null,listCatalog:r?()=>P(r):null,listPrices:r?()=>R(r):null,shared:s.lane==="api-key"?()=>E({apiUrl:e.apiUrl,apiKey:s.apiKey}):null,sessionKey:V});return u=n,e.defaultApp!==null&&n.seed(e.defaultApp,l({appSlug:e.defaultApp})),n}async function g(e){let s=e.whenLive;if(typeof s!="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 s.call(e)}function pe(e,s=O){let r=new WeakMap,u=new WeakMap,l=new WeakMap;return{createResponse:L({sessionFor:n=>e.sessionFor(n),sessionIdOf:n=>{let o=n.session.id;return typeof o=="string"&&o.length>0?o:null},evict:(n,o)=>e.evict(n,o),createStream:async(n,o)=>(await g(n.session),S(await n.responses.responses.create(o),n.gone))}),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),openAudio:async n=>{let{entry:o}=await e.sessionFor(n),t=r.get(o);return t||(t=(async()=>(await g(o.session),v(o.session,await s())))(),r.set(o,t),t.catch(()=>r.delete(o))),t},openVideo:async n=>{let{entry:o}=await e.sessionFor(n),t=u.get(o);return t||(t=k(o.session),u.set(o,t)),t},openVideoOut:async n=>{let{entry:o}=await e.sessionFor(n),t=l.get(o);return t||(t=b(o.session),l.set(o,t)),t},sessionHandle:async n=>(await e.handleFor(n)).handle,createResponseOn:async(n,o)=>{let{entry:t}=await e.sessionForHandle(n);return await g(t.session),S(await t.responses.responses.create(o),t.gone)},onSessionEnd:async(n,o)=>{let{entry:t}=await e.sessionForHandle(n),a=t.session;if(typeof a.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let d=!1;return a.onPhase(i=>{if(d||!W.has(i.name))return;d=!0;let p=a.endsAt??null;o({timeLeftMs:p?Math.max(0,p.getTime()-Date.now()):null,reason:i.reason??`session ${i.name}`})})}}}export{ue as a,pe as b};
@@ -1,57 +0,0 @@
1
- import{c as Nt,d as G,e as Oe}from"./chunk-3WAJD62J.js";import{a as Mt,c as qt,d as le,e as Je,f as F,g as Bt,h as Ft,i as Dt,j as jt,k as Gt,l as Ht,m as Wt,n as Jt,r as Vt,t as zt,u as Kt,v as Yt,w as Xt,x as Qt}from"./chunk-7M6LY6DR.js";import{c as T}from"./chunk-5CCJUNH7.js";T();import{randomUUID as nr}from"crypto";import{WebSocketServer as rr}from"ws";var or=256,Ve=class{snapshots=new Map;store(e,t,r){for(this.snapshots.set(`${e}\0${t}`,r);this.snapshots.size>or;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(e,t){return this.snapshots.get(`${e}\0${t}`)}},sr="local",Te=n=>n.slice(0,120),ze=class{constructor(e,t,r){this.ws=e;this.call=t;this.registry=r;this.clients=t.clients,this.scope=t.scope,e.on("message",o=>this.onMessage(o)),e.on("close",()=>{this.unsubscribeEnd?.(),this.unsubscribeEnd=null,this.detachAudio?.(),this.detachVideoOut?.(),this.call.release?.().catch(()=>{})})}ws;call;registry;config=null;transcript=[];queue=Promise.resolve();proxyHandle=null;unsubscribeEnd=null;audio=null;detachAudio=null;detachVideoOut=null;video=null;activityOpen=!1;activeTurn=null;scope;clients;send(e){this.ws.readyState===this.ws.OPEN&&this.ws.send(JSON.stringify(e))}fail(e){if(e instanceof G){this.ws.close(qt,Te(e.message));return}if(e instanceof Oe){this.ws.close(Je,Te(e.message));return}if(e instanceof F){this.ws.close(e.closeCode,Te(e.message));return}this.ws.close(le,Te(String(e instanceof Error?e.message:e)))}onMessage(e){try{let t=Bt(typeof e=="string"?e:Buffer.concat(Array.isArray(e)?e:[e]).toString("utf8"));if(t.kind==="setup"){this.handleSetup(t.setup).catch(o=>this.fail(o));return}if(!this.config)throw new F(`"${t.kind}" before setup \u2014 the first client message must be BidiGenerateContentSetup`);let r=this.config;if(t.kind==="clientContent"){let{items:o,turnComplete:s}=Jt(t.clientContent);this.transcript.push(...o),s&&this.enqueue(()=>this.runTurn());return}if(t.kind==="realtimeInput"){let o=zt(t.realtimeInput);if(o.kind==="activityStart"||o.kind==="activityEnd"){this.onActivity(o.kind,r);return}if(o.kind==="text"){this.transcript.push({role:"user",content:o.text}),this.enqueue(()=>this.runTurn());return}if(o.kind==="video"){this.appendVideoFrame(o.jpegFrame);return}this.appendAudio(o.kind==="audio"?o.base64Pcm24k:Vt);return}this.transcript.push(...Kt(t.toolResponse)),this.enqueue(()=>this.runTurn())}catch(t){this.fail(t)}}async handleSetup(e){if(this.config)throw new F("setup may only be sent once, as the first client message");let t=Gt(e),r=t.sessionResumption.handle;if(r!==null){let s=this.registry.get(this.scope,r);if(!s)throw new F("unknown sessionResumption.handle \u2014 the Live session it names is gone (proxy restarted, handle evicted, or never issued) and cannot be resumed; reconnect without a handle to start a new session",Je);if(s.model!==t.model)throw new F(`sessionResumption.handle was issued for model "${s.model}" \u2014 the model cannot change on resume (got "${t.model}", per the Live API session-resumption contract)`);if(t.systemItems.length>0)throw new F("setup.systemInstruction cannot be changed on resume \u2014 it is part of the resumed conversation state");if(this.call.clientsForResume!==void 0&&(this.clients=await this.call.clientsForResume(r)),this.proxyHandle=await this.clients.sessionHandle(t.model),this.proxyHandle!==s.proxyHandle)throw new F(`sessionResumption.handle named pooled session "${s.proxyHandle}" but the re-attach landed on "${this.proxyHandle}" \u2014 the conversation's native session changed and the snapshot cannot be adopted; reconnect without a handle to start a new session`,Je);this.unsubscribeEnd=await this.clients.onSessionEnd(s.proxyHandle,i=>this.enqueue(()=>this.handleUpstreamEnd(i))),this.transcript=[...s.transcript]}else this.proxyHandle=await this.clients.sessionHandle(t.model),this.unsubscribeEnd=await this.clients.onSessionEnd(this.proxyHandle,s=>this.enqueue(()=>this.handleUpstreamEnd(s))),this.transcript.push(...t.systemItems);this.config=t;let o={};if(t.videoOut){let s=this.clients.openVideoOut;if(s){let i=await s.call(this.clients,t.model),a=i.onOutputFrame(u=>this.send(jt(u))),d=i.onLaneError(u=>this.fail(u));this.detachVideoOut=()=>{a(),d()},o[Ft]=Dt(i.codec)}}this.send({setupComplete:o}),t.audioModality&&this.ensureAudioLane(),t.sessionResumption.enabled&&await this.mintResumptionUpdate()}ensureAudioLane(){if(this.audio)return this.audio;let e=this.config;if(!e)throw new F("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let t=this.clients.openAudio;if(!t)throw new F("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native AudioBridge seam over the canonical `audio` lane)",le);return this.audio=(async()=>{let r=await t.call(this.clients,e.model);return this.detachAudio=r.onOutputAudio(o=>this.send(Xt(o))),r})(),this.audio.catch(r=>this.fail(r)),this.audio}appendAudio(e){this.ensureAudioLane().then(t=>t.appendInputAudio(e)).catch(t=>this.fail(t))}ensureVideoLane(){if(this.video)return this.video;let e=this.config;if(!e)throw new F("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let t=this.clients.openVideo;if(!t)throw new F("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",le);return this.video=Promise.resolve(t.call(this.clients,e.model)),this.video.catch(r=>this.fail(r)),this.video}appendVideoFrame(e){this.ensureVideoLane().then(t=>t.sendInputFrame(e)).catch(t=>this.fail(t))}onActivity(e,t){if(!t.activity.manualActivity)throw new F(`realtimeInput.${e} requires manual activity mode \u2014 set setup.realtimeInputConfig.automaticActivityDetection.disabled: true (automatic server-side activity detection is the declared platform follow-up)`);if(e==="activityStart"){if(this.activityOpen)throw new F("activityStart while an activity window is already open (send activityEnd first)");this.activityOpen=!0,t.activity.activityHandling==="START_OF_ACTIVITY_INTERRUPTS"&&this.activeTurn&&(this.activeTurn.interrupted=!0);return}if(!this.activityOpen)throw new F("activityEnd without an open activity window (send activityStart first)");this.activityOpen=!1}enqueue(e){this.queue=this.queue.then(e,()=>{}),this.queue=this.queue.catch(t=>this.fail(t))}async mintResumptionUpdate(){let e=this.config;if(!e?.sessionResumption.enabled||this.ws.readyState!==this.ws.OPEN)return;let t=await this.clients.sessionHandle(e.model);this.proxyHandle=t;let r=nr();this.registry.store(this.scope,r,{model:e.model,proxyHandle:t,transcript:[...this.transcript]}),this.call.onBindResumeHandle?.(r),this.send(Ht(r))}async handleUpstreamEnd(e){if(this.ws.readyState!==this.ws.OPEN||!this.config)return;this.unsubscribeEnd?.(),this.unsubscribeEnd=null;let t=null;try{t=await this.clients.sessionHandle(this.config.model)}catch{}if(t!==null&&t!==this.proxyHandle){this.proxyHandle=t,this.unsubscribeEnd=await this.clients.onSessionEnd(t,r=>this.enqueue(()=>this.handleUpstreamEnd(r))),this.config.sessionResumption.enabled&&await this.mintResumptionUpdate();return}this.send(Wt(e.timeLeftMs)),this.ws.close(le,Te(`upstream session ended: ${e.reason}`))}async runTurn(){let e=this.config;if(!e||this.ws.readyState!==this.ws.OPEN)return;let t={interrupted:!1};this.activeTurn=t;try{let r=await this.clients.createResponse({model:e.model,input:[...this.transcript],stream:!0,tools:e.tools,temperature:e.temperature,max_output_tokens:e.maxOutputTokens}),o,s=!1;for await(let d of r){if(t.interrupted)break;let u=d;if(u.type==="error")throw new F(`upstream error: ${JSON.stringify(u)}`,le);if(u.type==="response.completed"){o=u.response,s=!0;continue}let l=Yt(u);l&&this.send(l)}if(t.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!s)throw new F("upstream produced no response.completed event",le);let{messages:i,assistantItems:a}=Qt(o);this.transcript.push(...a);for(let d of i)this.send(d);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},ce=class extends Error{constructor(t,r){super(r);this.status=t;this.name="UpgradeAuthError"}status};function ir(n,e,t){let r=new Set;(d=>{if(typeof d=="string")d!==""&&r.add(d);else if(Array.isArray(d))for(let u of d)u!==""&&r.add(u)})(n.headers["x-goog-api-key"]);let s=n.headers.authorization;typeof s=="string"&&s.startsWith("Bearer ")&&s.length>7&&r.add(s.slice(7));let i=e.searchParams.get("key")??void 0;if(i!==void 0&&i!==""&&r.add(i),r.size>1)throw new ce(401,`contradictory credentials: ${r.size} distinct keys presented across the x-goog-api-key/Authorization headers and the ?key= query parameter \u2014 present exactly one`);if(t&&i!==void 0&&i!=="")throw new ce(401,"the ?key= query parameter is not accepted on the hosted endpoint \u2014 send the org API key in the x-goog-api-key header or as Authorization: Bearer");let a;for(let d of r)a=d;if(a===void 0&&t)throw new ce(401,"missing credentials: send the org API key in the x-goog-api-key header or as Authorization: Bearer");return a??""}function Zt(n,e){let t=e.authenticate!==void 0;if(t&&(e.clients!==void 0||e.apiKey!==void 0))throw new TypeError("attachGeminiLive: authenticate (hosted per-request resolution) is mutually exclusive with a fixed clients/apiKey lane");if(!t&&e.clients===void 0)throw new TypeError("attachGeminiLive: pass fixed `clients` (local lane) or `authenticate` (hosted lane)");let r=new rr({noServer:!0}),o=new Ve;n.on("upgrade",(i,a,d)=>{let u=new URL(i.url??"/","http://localhost");u.pathname===Mt&&(a.on("error",()=>{}),(async()=>{let l=ir(i,u,t),c;if(e.authenticate)c=await e.authenticate(l,i);else{if(e.apiKey!==void 0&&l!==e.apiKey)throw new ce(401,"invalid local proxy api key");c={clients:e.clients,scope:sr}}r.handleUpgrade(i,a,d,w=>{new ze(w,c,o)})})().catch(l=>{let c=502;(l instanceof ce||typeof l=="object"&&l!==null&&"status"in l&&typeof l.status=="number")&&(c=l.status);let w=c===401?"Unauthorized":c===403?"Forbidden":c===502?"Bad Gateway":"Internal Server Error",f=l instanceof Error?l.message:String(l);a.destroyed||(a.write(`HTTP/1.1 ${c} ${w}\r
2
- Connection: close\r
3
- \r
4
- ${f}\r
5
- `),a.destroy())}))});let s=null;return()=>(s??=(async()=>{for(let i of r.clients)i.close(1001,"server closing");await new Promise((i,a)=>r.close(d=>d===void 0?i():a(d)))})(),s)}function ts(n,e,t=new Ve){new ze(n,e,t)}T();T();import{randomUUID as ar}from"crypto";function v(n){return`${n}_${ar().replace(/-/g,"").slice(0,24)}`}var Ke=4*1024*1024,lt=8*1024*1024,Ye=512,Xe=20*1024*1024;function N(n,e,t){return{type:"invalid_request_error",code:n,message:e,param:t??null}}function Ce(n,e){return{type:"server_error",code:n,message:e}}function C(n){return{type:"error",event_id:v("event"),error:n}}function ct(){return{outputModalities:["text"],turnDetection:null,inputAudioTranscription:!1}}var b=class extends Error{constructor(t,r,o="invalid_value"){super(t);this.param=r;this.code=o;this.name="SessionUpdateError"}param;code},ur={model:!0,instructions:!0,modalities:!0,voice:!0,input_audio_format:!0,output_audio_format:!0,temperature:!0,max_output_tokens:!0,max_response_output_tokens:!0,tool_choice:!0,tools:!0,metadata:!0,turn_detection:!0,input_audio_transcription:!0,input_audio_noise_reduction:!0,audio:!0},dr={format:!0,transcription:!0,turn_detection:!0,noise_reduction:!0},lr={format:!0,voice:!0,speed:!0};function en(n,e){let t=n;if(!t||typeof t!="object"||t.type!=="audio/pcm"||t.rate!==void 0&&t.rate!==24e3)throw new b(`${e} must be {"type":"audio/pcm","rate":24000} \u2014 the session audio lane is 24 kHz mono PCM16 only`,e)}function Pe(n,e,t){if(!n||typeof n!="object"||Array.isArray(n))throw new b("session must be an object","session","invalid_request");let r=n,o=Object.keys(r).filter(i=>!(i in ur));if(o.length>0)throw new b(`session field(s) not supported by this backend: ${o.join(", ")}`,o[0],"unsupported_field");let s={...e,outputModalities:[...e.outputModalities]};for(let[i,a]of Object.entries(r))switch(i){case"model":if(a!==null&&typeof a!="string")throw new b("model must be a string or null","model");s.model=a??void 0;break;case"instructions":if(a!==null&&typeof a!="string")throw new b("instructions must be a string or null","instructions");s.instructions=a??void 0;break;case"modalities":{if(!Array.isArray(a)||a.length===0)throw new b("modalities must be a non-empty array","modalities");let d=[];for(let u of a){if(u!=="text"&&u!=="audio")throw new b(`modality ${JSON.stringify(u)} is not "text" or "audio"`,"modalities");if(u==="audio"&&!t.voice)throw new b('modality "audio" refused: the resolver did not provide an authoritative voice binding for the resolved backend \u2014 this model cannot speak on this session',"modalities","audio_not_supported");d.includes(u)||d.push(u)}s.outputModalities=d;break}case"voice":if(a===null){delete s.voice;break}if(typeof a!="string")throw new b("voice must be a string or null","voice");if(!t.voice)throw new b("voice refused: no authoritative voice binding for the resolved backend","voice","audio_not_supported");s.voice=a;break;case"input_audio_format":case"output_audio_format":if(a!=="pcm16")throw new b(`${i} must be "pcm16" \u2014 the session audio lane is 24 kHz mono PCM16 only`,i);break;case"temperature":if(a!==null&&(typeof a!="number"||!Number.isFinite(a)))throw new b("temperature must be a finite number or null","temperature");s.temperature=a??void 0;break;case"max_output_tokens":case"max_response_output_tokens":if(a!==null&&a!=="inf"&&(typeof a!="number"||!Number.isInteger(a)||a<1))throw new b(`${i} must be a positive integer, "inf" or null`,i);s.maxOutputTokens=a===null?void 0:a;break;case"tools":if(!Array.isArray(a))throw new b("tools must be an array","tools");s.tools=a;break;case"tool_choice":s.toolChoice=a;break;case"metadata":if(a!==null&&(!a||typeof a!="object"||Array.isArray(a)))throw new b("metadata must be an object or null","metadata");s.metadata=a??null;break;case"turn_detection":{if(a===null){s.turnDetection=null;break}if(!t.turns)throw new b("turn_detection is refused: the resolved backend does not wire its own turn boundary lane (stt stream with turn_start/turn_end) \u2014 there is no native VAD source to drive responses from. Drive turns manually: input_audio_buffer.commit + response.create.","turn_detection","unsupported_field");let d=a;if(!d||typeof d!="object"||Array.isArray(d))throw new b("turn_detection must be an object or null","turn_detection");let u=String(d.type??"");if(u!=="server_vad"&&u!=="semantic_vad")throw new b(`turn_detection.type ${JSON.stringify(d.type)} is not "server_vad"/"semantic_vad"`,"turn_detection.type");s.turnDetection={createResponse:d.create_response!==!1,interruptResponse:d.interrupt_response!==!1};break}case"input_audio_transcription":{if(a===null){s.inputAudioTranscription=!1;break}if(!t.turns)throw new b("input_audio_transcription is refused: the resolved backend does not wire a transcript lane \u2014 no native transcription source exists behind this session","input_audio_transcription","unsupported_field");if(!a||typeof a!="object"||Array.isArray(a))throw new b("input_audio_transcription must be an object or null","input_audio_transcription");for(let d of Object.keys(a))if(!["model","language","languages","prompt"].includes(d))throw new b(`input_audio_transcription field not honored by this backend: ${d} (transcription comes from the voice engine itself)`,`input_audio_transcription.${d}`,"unsupported_field");s.inputAudioTranscription=!0;break}case"input_audio_noise_reduction":if(a!==null)throw new b("input_audio_noise_reduction must be null on this backend: no noise-reduction primitive exists behind the session audio bridge","input_audio_noise_reduction","unsupported_field");break;case"audio":{if(a===null)break;let d=a;if(!d||typeof d!="object"||Array.isArray(d))throw new b("audio must be an object or null","audio");if(d.input!==void 0&&d.input!==null){let u=d.input;if(!u||typeof u!="object"||Array.isArray(u))throw new b("audio.input must be an object","audio.input");for(let l of Object.keys(u))if(!(l in dr))throw new b(`audio.input field not supported by this backend: ${l}`,`audio.input.${l}`,"unsupported_field");if(u.format!==void 0&&en(u.format,"audio.input.format"),u.transcription!==null&&u.transcription!==void 0){let l=Pe({input_audio_transcription:u.transcription},s,t);s.inputAudioTranscription=l.inputAudioTranscription}if(u.turn_detection!==null&&u.turn_detection!==void 0){let l=Pe({turn_detection:u.turn_detection},s,t);s.turnDetection=l.turnDetection}if(u.noise_reduction!==null&&u.noise_reduction!==void 0)throw new b("audio.input.noise_reduction must be null on this backend: no noise-reduction primitive","audio.input.noise_reduction","unsupported_field")}if(d.output!==void 0&&d.output!==null){let u=d.output;if(!u||typeof u!="object"||Array.isArray(u))throw new b("audio.output must be an object","audio.output");for(let l of Object.keys(u))if(!(l in lr))throw new b(`audio.output field not supported by this backend: ${l}`,`audio.output.${l}`,"unsupported_field");if(u.format!==void 0&&en(u.format,"audio.output.format"),u.speed!==void 0)throw new b("audio.output.speed is not supported on this backend: playback rate is fixed","audio.output.speed","unsupported_field");if(u.voice!==void 0)if(u.voice===null)delete s.voice;else{if(typeof u.voice!="string")throw new b("audio.output.voice must be a string or null","audio.output.voice");if(!t.voice)throw new b("audio.output.voice refused: no authoritative voice binding for the resolved backend","audio.output.voice","audio_not_supported");s.voice=u.voice}u.format!==void 0&&!s.outputModalities.includes("audio")&&(s.outputModalities=["text","audio"])}break}default:break}return s}function Qe(n,e){return{id:e,object:"realtime.session",model:n.model??null,instructions:n.instructions??"",modalities:[...n.outputModalities],voice:n.voice??null,input_audio_format:"pcm16",output_audio_format:"pcm16",input_audio_transcription:n.inputAudioTranscription?{}:null,input_audio_noise_reduction:null,turn_detection:n.turnDetection?{type:"server_vad",create_response:n.turnDetection.createResponse,interrupt_response:n.turnDetection.interruptResponse}:null,temperature:n.temperature??null,max_response_output_tokens:n.maxOutputTokens??"inf",tool_choice:n.toolChoice??"auto",tools:n.tools??[],metadata:n.metadata??null}}function pt(n,e){let t=n;if(!t||typeof t!="object")throw new b("item must be an object","item","invalid_request");if(t.type==="function_call_output"){if(typeof t.call_id!="string"||typeof t.output!="string")throw new b("function_call_output item requires string call_id and output","item.call_id","invalid_request");return{id:typeof t.id=="string"?t.id:v("item"),type:"function_call_output",callId:t.call_id,output:t.output}}if(t.type==="message"){let r=t.role;if(r!=="user"&&r!=="assistant"&&r!=="system")throw new b('message item role must be "user", "assistant" or "system"',"item.role","invalid_request");if(!Array.isArray(t.content))throw new b("message item content must be an array","item.content","invalid_request");let o=[];for(let s of t.content){let i=s;if(!i||typeof i!="object")throw new b("content part must be an object","item.content","invalid_request");if(i.type==="input_text"||r==="assistant"&&i.type==="output_text"){if(typeof i.text!="string")throw new b(`${String(i.type)} part requires string text`,"item.content.text","invalid_request");o.push({type:i.type,text:i.text});continue}if(i.type==="input_audio"){if(!e)throw new b("input_audio content refused: no authoritative voice binding for the resolved backend","item.content.type","audio_not_supported");if(typeof i.audio!="string")throw new b("input_audio part requires base64 audio","item.content.audio","invalid_request");o.push({type:"input_audio",...typeof i.transcript=="string"?{transcript:i.transcript}:{}});continue}throw new b(`content part type ${String(i.type)} is not supported on message items here (input_text / output_text / input_audio)`,"item.content.type","invalid_request")}return{id:typeof t.id=="string"?t.id:v("item"),type:"message",role:r,content:o}}throw new b(`item type ${String(t.type)} is not supported \u2014 message and function_call_output items only`,"item.type","invalid_request")}function mt(n){let e=[],t=!1;for(let r of n){if(r.type==="function_call"){e.push({type:"function_call",call_id:r.callId,name:r.name,arguments:r.arguments??"{}"});continue}if(r.type==="function_call_output"){e.push({type:"function_call_output",call_id:r.callId,output:r.output});continue}let o=[];for(let s of r.content??[]){if(s.type==="input_text"||s.type==="output_text"){o.push({type:s.type,text:s.text});continue}if(s.type==="input_audio"){let i=s.transcript;if(typeof i=="string"&&i.trim().length>0){o.push({type:"input_text",text:i});continue}t=!0;continue}}e.push({role:r.role,content:o})}return{input:e,sawAudioOnlyItem:t}}var cr=1001,pr=1011,tn=n=>n.slice(0,120),mr=/^[A-Za-z0-9+/=]*$/,fr=2;function gr(n){let e=Buffer.from(n,"base64");for(let t=0;t+1<e.byteLength;t+=2)if(Math.abs(e.readInt16LE(t))>fr)return!1;return!0}var Ue=class{id=v("conn");ws;binding;onClosed;config=ct();sessionId=v("sess");conversationId=v("conv");items=[];pendingAudioBytes=[];pendingAudioTotalBytes=0;frozenAudio=[];inFlight=null;activeResponseId=null;responseFlushed=!1;cancelled=!1;voiceClaimed=!1;audioLaneOwner=!1;audioUnsubscribe=null;outOfTurnAudioWarned=!1;audioOnlyTranscriptGapWarned=!1;closed=!1;turnLaneAbort=null;turnTranscript={open:!1,text:""};lastCommittedTranscript=null;constructor(e){this.ws=e.ws,this.binding=e.binding,this.onClosed=e.onClosed,this.binding.model&&(this.config.model=this.binding.model),this.ws.on("message",t=>{this.onMessage(t)}),this.ws.on("close",()=>this.teardown("client disconnected")),this.ws.on("error",()=>this.teardown("client socket error")),this.send({type:"session.created",event_id:v("event"),session:Qe(this.config,this.sessionId)}),this.send({type:"conversation.created",event_id:v("event"),conversation:{id:this.conversationId,object:"realtime.conversation"}}),this.binding.turns&&this.startTurnLane()}startTurnLane(){let e=new AbortController;this.turnLaneAbort=e;let t=this.binding.turns;(async()=>{try{for await(let r of t()){if(this.closed||e.signal.aborted)return;this.onTurnEvent(r)}}catch(r){!this.closed&&!e.signal.aborted&&this.send(C(Ce("turn_lane_fault",`the backend turn/transcript lane failed: ${r instanceof Error?r.message:String(r)}`)))}})()}onTurnEvent(e){switch(e.kind){case"delta":case"final":{let t=typeof e.text=="string"?e.text:"";if(t.length===0)return;this.turnTranscript.open=!0,this.turnTranscript.text+=t,this.config.inputAudioTranscription&&this.send({type:"conversation.item.input_audio_transcription.delta",event_id:v("event"),item_id:"input_audio_turn",content_index:0,delta:t});return}case"turn_start":{this.turnTranscript={open:!0,text:this.turnTranscript.text},this.send({type:"input_audio_buffer.speech_started",event_id:v("event"),item_id:"input_audio_turn",audio_start_ms:typeof e.startMs=="number"?e.startMs:0}),this.config.turnDetection?.interruptResponse&&this.inFlight&&this.cancelResponse();return}case"turn_end":{let t=this.turnTranscript.text.trim(),r=t.length>0?t:null;this.turnTranscript={open:!1,text:""},this.lastCommittedTranscript=r,this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let o=v("item"),s=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem({id:o,type:"message",role:"user",content:[{type:"input_audio",...r!==null?{transcript:r}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:v("event"),previous_item_id:s,item_id:o}),this.send({type:"conversation.item.created",event_id:v("event"),previous_item_id:s,item:this.itemToProtocol(this.items[this.items.length-1])}),this.send({type:"input_audio_buffer.speech_stopped",event_id:v("event"),item_id:o,audio_end_ms:typeof e.startMs=="number"?e.startMs:0}),this.config.inputAudioTranscription&&r!==null&&this.send({type:"conversation.item.input_audio_transcription.completed",event_id:v("event"),item_id:o,content_index:0,transcript:r}),this.config.turnDetection?.createResponse&&!this.inFlight&&this.startResponse({});return}case"turn_eager_end":case"turn_resumed":return;default:return}}takeTurnTranscript(){let e=this.lastCommittedTranscript;return this.lastCommittedTranscript=null,e}async onMessage(e){if(this.closed)return;let t;try{t=e.toString()}catch{this.fail(N("decode_error","frame is not decodable text"),"invalid_frame");return}if(Buffer.byteLength(t,"utf8")>Ke){this.fail(N("frame_too_large",`frame exceeds the ${Ke}-byte limit`),"invalid_frame");return}let r;try{r=JSON.parse(t)}catch{this.fail(N("invalid_json","frame is not valid JSON"),"invalid_frame");return}if(typeof r.type!="string"||r.type.length===0){this.fail(N("missing_type",'event is missing a string "type"'),"invalid_frame");return}try{await this.handle(r)}catch(o){let s=o;if(s&&typeof s=="object"&&s.name==="SessionUpdateError"){this.send(C({type:"invalid_request_error",code:s.code??"invalid_value",message:s.message,param:s.param??null}));return}this.fail(Ce(null,o instanceof Error?o.message:String(o)),"handler_fault")}}async handle(e){switch(e.type){case"session.update":this.config=Pe(e.session,this.config,{voice:this.binding.voice,turns:this.binding.turns!==void 0}),this.send({type:"session.updated",event_id:v("event"),session:Qe(this.config,this.sessionId)});return;case"conversation.item.create":{let t=pt(e.item,this.binding.voice),r=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem(t),this.send({type:"conversation.item.created",event_id:v("event"),previous_item_id:r,item:this.itemToProtocol(t)});return}case"conversation.item.delete":{let t=this.items.findIndex(r=>r.id===e.item_id);if(t<0){this.send(C(N("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.items.splice(t,1),this.send({type:"conversation.item.deleted",event_id:v("event"),item_id:e.item_id});return}case"conversation.item.retrieve":{let t=this.items.find(r=>r.id===e.item_id);if(!t){this.send(C(N("item_not_found",`no item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}this.send({type:"conversation.item.retrieved",event_id:v("event"),item:this.itemToProtocol(t)});return}case"conversation.item.truncate":{let t=this.items.find(r=>r.id===e.item_id);if(!t||t.type!=="message"||t.role!=="assistant"){this.send(C(N("item_not_found",`no assistant message item with id ${JSON.stringify(e.item_id)}`,"item_id")));return}t.truncated=!0,this.send({type:"conversation.item.truncated",event_id:v("event"),item_id:t.id,content_index:typeof e.content_index=="number"?e.content_index:0,audio_end_ms:typeof e.audio_end_ms=="number"?e.audio_end_ms:0});return}case"input_audio_buffer.append":{if(typeof e.audio!="string"||e.audio.length===0){this.send(C(N("invalid_audio",'input_audio_buffer.append requires base64 "audio"',"audio")));return}if(!this.ensureVoice())return;if(!mr.test(e.audio)){this.send(C(N("invalid_audio","input_audio_buffer.append audio must be base64","audio")));return}let t=Buffer.byteLength(e.audio,"base64");if(this.pendingAudioBytes.length+t>Xe){this.send(C(N("buffer_full",`uncommitted input audio exceeds the ${Xe}-byte bound \u2014 commit or clear first`,"audio")));return}if(this.inFlight){this.binding.audioSession.audio.appendInputAudio(e.audio);return}this.pendingAudioBytes.push(e.audio),this.pendingAudioTotalBytes+=t;return}case"input_audio_buffer.clear":{if(!this.ensureVoice())return;this.pendingAudioBytes.length=0,this.pendingAudioTotalBytes=0,this.send({type:"input_audio_buffer.cleared",event_id:v("event")});return}case"input_audio_buffer.commit":{if(!this.ensureVoice())return;if(this.pendingAudioTotalBytes===0&&this.takeTurnTranscript()===null){this.send(C(N("empty_buffer","input_audio_buffer.commit with no buffered audio","audio")));return}this.frozenAudio=this.pendingAudioBytes.splice(0),this.pendingAudioTotalBytes=0;let t=this.takeTurnTranscript();t===null&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let r=v("item"),o=this.items.length>0?this.items[this.items.length-1].id:null;this.pushItem({id:r,type:"message",role:"user",content:[{type:"input_audio",...t!==null?{transcript:t}:{}}]}),this.send({type:"input_audio_buffer.committed",event_id:v("event"),previous_item_id:o,item_id:r}),this.send({type:"conversation.item.created",event_id:v("event"),previous_item_id:o,item:this.itemToProtocol(this.items[this.items.length-1])});return}case"output_audio_buffer.clear":{this.send(C(N("output_clear_unavailable","output_audio_buffer.clear has no server-side playback queue to flush on this adapter: output audio is delivered as response.output_audio.delta events and interrupted turns are stopped with response.cancel")));return}case"response.create":this.startResponse(e.response??{});return;case"response.cancel":this.cancelResponse();return;default:this.send(C(N("unknown_event",`event type ${e.type} is not supported by this adapter`)))}}ensureVoice(){return!this.binding.voice||!this.binding.audioSession?(this.send(C(N("audio_not_supported","audio events require a voice binding from the resolver \u2014 this session was resolved text-only","audio"))),!1):(this.voiceClaimed||(this.claimVoiceBinding(),this.audioUnsubscribe=this.binding.audioSession.audio.onOutputAudio(e=>{this.onOutputAudioFrame(this.activeResponseId??"",e)}),this.voiceClaimed=!0),!0)}claimVoiceBinding(){let e=this.binding.audioSession,t=e.openaiRealtimeClaims??(e.openaiRealtimeClaims=new Set);if(t.has(e))throw new Error("voice binding already owned by another connection \u2014 the resolver handed one audio bridge to two realtime conversations, which must never happen (independent callers require private bindings)");t.add(e),this.audioLaneOwner=!0}releaseVoiceBinding(){let e=this.binding.audioSession;!e||!this.audioLaneOwner||(e.openaiRealtimeClaims?.delete(e),this.audioLaneOwner=!1)}warnAudioOnlyTranscriptGap(){this.audioOnlyTranscriptGapWarned||(this.audioOnlyTranscriptGapWarned=!0,this.send(C({type:"invalid_request_error",code:"audio_input_transcript_unavailable",message:"committed input audio is forwarded to the runtime audio lane but is NOT model-visible in the text turn lane: the resolved backend wires no turn/transcript lane (the voice engine's own stt stream with turn_start/turn_end + transcripts). Wire the transcribe/turn-detection leg upstream (the resolver supplies binding.turns) and committed audio gains its REAL transcript as the model-visible turn.",param:"input_audio_buffer.commit"})))}async upstreamStream(e,t){let r=this.binding.session;if(r&&typeof r.sendResponseCreate=="function")return r.sendResponseCreate(e,e.input??[],t);let o=this.binding.clients;if(o&&typeof o.createResponse=="function")return await o.createResponse(e);throw new Error("realtime binding provides neither session.sendResponseCreate nor clients.createResponse \u2014 no upstream turn seam")}startResponse(e){if(this.inFlight){this.send(C(N("response_in_progress","a response is already in progress \u2014 cancel it with response.cancel first","response.create")));return}let t=e??{};if(t.modalities!==void 0){if(!Array.isArray(t.modalities)||t.modalities.some(R=>R!=="text"&&R!=="audio")){this.send(C(N("invalid_modalities",'response.modalities must be "text"/"audio"',"response.modalities")));return}if(t.modalities.includes("audio")&&!this.ensureVoice())return}if(t.input!==void 0){this.send(C(N("response_input_unsupported","response.input is not supported by this adapter: build the context with conversation.item.create, then response.create","response.input")));return}this.cancelled=!1,this.responseFlushed=!1,this.messageItemAdded=!1;let r=v("req"),o=`resp_${r}`;this.activeResponseId=o;let s=this.items.length,{input:i,sawAudioOnlyItem:a}=mt(this.items),d=typeof t.instructions=="string"?t.instructions:this.config.instructions,u=typeof t.temperature=="number"?t.temperature:this.config.temperature,l=t.max_output_tokens??this.config.maxOutputTokens,c=l==="inf"||l===void 0?void 0:l,w=t.tools!==void 0?t.tools:this.config.tools,f=t.tool_choice!==void 0?t.tool_choice:this.config.toolChoice,E={input:i,stream:!0,...this.config.model?{model:this.config.model}:{},...d!==void 0?{instructions:d}:{},...u!==void 0?{temperature:u}:{},...c!==void 0?{max_output_tokens:c}:{},...w!==void 0?{tools:w}:{},...f!==void 0?{tool_choice:f}:{}};for(let R of this.frozenAudio.splice(0))this.binding.audioSession?.audio.appendInputAudio(R);this.inFlight=(async()=>{let R=[],m=S=>{this.responseFlushed?this.send(S):R.push(S)},h=()=>{if(!this.responseFlushed){this.send({type:"response.created",event_id:v("event"),response:this.responseObject(o,"in_progress")}),this.send({type:"response.in_progress",event_id:v("event"),response:this.responseObject(o,"in_progress")});for(let S of R)this.send(S);R.length=0,this.responseFlushed=!0}},g={messageOpen:!1,messageText:"",tools:new Map,nextOutputIndex:0},y=null;try{a&&this.binding.turns===void 0&&this.warnAudioOnlyTranscriptGap();let S;try{S=await this.upstreamStream(E,r)}catch(x){this.emitUpstreamFault(x,o);return}try{for await(let x of S){if(this.cancelled)break;let _=x;if(_.type==="error"){let p=this.upstreamErrorBody(_);this.responseFlushed?this.send(C(p)):this.send({type:"response.failed",event_id:v("event"),response:this.failedResponse(o,p)});return}if(_.type==="response.output_text.delta"&&typeof _.delta=="string"){h(),g.messageOpen||(this.beginMessageItem(o,g.nextOutputIndex++),g.messageOpen=!0),m({type:"response.output_text.delta",event_id:v("event"),item_id:`${o}_msg_0`,response_id:o,output_index:0,content_index:0,delta:_.delta}),g.messageText+=_.delta;continue}if(_.type==="response.function_call_arguments.delta"){h();let p=typeof _.delta=="string"?_.delta:"",O=typeof _.call_id=="string"&&_.call_id.length>0?_.call_id:`${o}_fc_${g.nextOutputIndex}`,P=g.tools.get(O);if(!P){let L=typeof _.name=="string"&&_.name.length>0?_.name:void 0;P={outputIndex:g.nextOutputIndex++,name:L,args:""},g.tools.set(O,P),m({type:"response.output_item.added",event_id:v("event"),response_id:o,output_index:P.outputIndex,item:{id:O,object:"realtime.item",type:"function_call",call_id:O,name:L??"",arguments:"",status:"in_progress"}})}typeof _.name=="string"&&_.name.length>0&&(P.name=_.name),P.args+=p,m({type:"response.function_call_arguments.delta",event_id:v("event"),item_id:O,response_id:o,output_index:P.outputIndex,call_id:O,name:P.name??"",delta:p});continue}if(_.type==="response.completed"){y=_.response??null;continue}}}catch(x){this.emitUpstreamFault(x,o);return}if(this.cancelled)return;this.closeOpenItems(o,g,s),h(),this.send({type:"response.done",event_id:v("event"),response:this.doneResponse(o,s,y)})}finally{this.inFlight=null,this.activeResponseId=null}})()}onOutputAudioFrame(e,t){if(this.cancelled)return;if(this.activeResponseId!==e){if(gr(t))return;!this.outOfTurnAudioWarned&&!this.closed&&(this.outOfTurnAudioWarned=!0,this.send(C({type:"invalid_request_error",code:"out_of_turn_audio",message:"the runtime produced voice OUT outside a response turn; this adapter delivers output audio only within response.output_audio.delta of an open turn \u2014 wire server-driven turns (VAD) upstream or keep speech inside response turns"})));return}this.responseFlushed||(this.send({type:"response.created",event_id:v("event"),response:this.responseObject(e,"in_progress")}),this.send({type:"response.in_progress",event_id:v("event"),response:this.responseObject(e,"in_progress")}),this.responseFlushed=!0);let r=`${e}_msg_0`;this.messageItemOpenForAudio||(this.beginAudioMessageItem(e),this.messageItemOpenForAudio=!0),this.send({type:"response.output_audio.delta",event_id:v("event"),item_id:r,response_id:e,output_index:0,content_index:0,delta:t})}messageItemAdded=!1;messageItemOpenForAudio=!1;beginMessageItem(e,t){let r=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:v("event"),response_id:e,output_index:t,item:{id:r,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:v("event"),item_id:r,response_id:e,output_index:t,content_index:0,part:{type:"output_text",text:""}})}beginAudioMessageItem(e){let t=`${e}_msg_0`;this.messageItemAdded||(this.messageItemAdded=!0,this.send({type:"response.output_item.added",event_id:v("event"),response_id:e,output_index:0,item:{id:t,object:"realtime.item",type:"message",role:"assistant",status:"in_progress",content:[]}})),this.send({type:"response.content_part.added",event_id:v("event"),item_id:t,response_id:e,output_index:0,content_index:0,part:{type:"output_audio",transcript:""}})}closeOpenItems(e,t,r){if(t.messageOpen||this.messageItemOpenForAudio){let o=`${e}_msg_0`,s=this.messageItemOpenForAudio;s&&this.send({type:"response.output_audio.done",event_id:v("event"),item_id:o,response_id:e,output_index:0,content_index:0}),t.messageText.length>0&&this.send({type:"response.output_text.done",event_id:v("event"),item_id:o,response_id:e,output_index:0,content_index:0,text:t.messageText}),this.send({type:"response.content_part.done",event_id:v("event"),item_id:o,response_id:e,output_index:0,content_index:0,part:s?{type:"output_audio",transcript:""}:{type:"output_text",text:t.messageText}});let i=[];s&&i.push({type:"output_audio"}),t.messageText.length>0&&i.push({type:"output_text",text:t.messageText});let a={id:o,type:"message",role:"assistant",content:i};this.pushItem(a),this.send({type:"response.output_item.done",event_id:v("event"),response_id:e,output_index:0,item:this.itemToProtocol(a)})}for(let[o,s]of t.tools){let i=s.args.length>0?s.args:"{}";this.send({type:"response.function_call_arguments.done",event_id:v("event"),item_id:o,response_id:e,output_index:s.outputIndex,call_id:o,name:s.name??"",arguments:i});let a={id:o,type:"function_call",callId:o,name:s.name,arguments:i};this.pushItem(a),this.send({type:"response.output_item.done",event_id:v("event"),response_id:e,output_index:s.outputIndex,item:this.itemToProtocol(a)})}this.messageItemOpenForAudio=!1,this.messageItemAdded=!1}responseObject(e,t,r){return{id:e,object:"realtime.response",status:t,conversation_id:this.conversationId,usage:null,...r??{}}}failedResponse(e,t){return this.responseObject(e,"failed",{status_details:{error:{code:t.code,message:t.message}}})}doneResponse(e,t,r){let o=r&&typeof r.usage=="object"&&r.usage!==null?r.usage:null,s=[];for(let i=t;i<this.items.length;i++)s.push(this.itemToProtocol(this.items[i]));return this.responseObject(e,"completed",{output:s,usage:o?{input_tokens:typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0,output_tokens:typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0,total_tokens:typeof o.total_tokens=="number"&&Number.isFinite(o.total_tokens)?o.total_tokens:(typeof o.input_tokens=="number"&&Number.isFinite(o.input_tokens)?o.input_tokens:0)+(typeof o.output_tokens=="number"&&Number.isFinite(o.output_tokens)?o.output_tokens:0),input_token_details:{cached_tokens:0,audio_tokens:0,text_tokens:0},output_token_details:{audio_tokens:0,text_tokens:0}}:null})}upstreamErrorBody(e){let t=e.error,r=t&&typeof t.message=="string"?t.message:JSON.stringify(e);return{type:"server_error",code:t&&typeof t.code=="string"?t.code:null,message:r}}emitUpstreamFault(e,t){if(this.cancelled)return;let r=Ce(null,e instanceof Error?e.message:String(e));this.responseFlushed?this.send(C(r)):this.send({type:"response.failed",event_id:v("event"),response:this.failedResponse(t,r)})}cancelResponse(){let e=this.inFlight;if(!e){this.send(C(N("no_response_to_cancel","no response is in progress","response.cancel")));return}this.cancelled=!0;let t=this.activeResponseId??"resp_";e.finally(()=>{this.send({type:"response.cancelled",event_id:v("event"),response:this.responseObject(t,"cancelled")})})}pushItem(e){this.items.push(e),this.items.length>Ye&&(this.items.shift(),this.send(C({type:"invalid_request_error",code:"transcript_evicted",message:`the conversation transcript exceeded ${Ye} items \u2014 the oldest item was evicted to keep the per-connection bound`})))}itemToProtocol(e){let t={id:e.id,object:"realtime.item"};if(e.type==="function_call")return{...t,type:"function_call",call_id:e.callId,name:e.name,arguments:e.arguments??"{}",status:"completed"};if(e.type==="function_call_output")return{...t,type:"function_call_output",call_id:e.callId,output:e.output};let r=(e.content??[]).map(o=>o.type==="input_text"||o.type==="output_text"?{type:o.type,text:o.text}:o.type==="input_audio"?{type:"input_audio",...o.transcript?{transcript:o.transcript}:{}}:{type:"output_audio",transcript:o.transcript??""});return{...t,type:"message",role:e.role,status:e.truncated?"incomplete":"completed",content:r}}send(e){if(!(this.closed||this.ws.readyState!==this.ws.OPEN)){if(this.ws.bufferedAmount>lt){this.ws.close(cr,tn("outbound buffer exceeded")),this.teardown("outbound buffer exceeded");return}this.ws.send(JSON.stringify(e))}}fail(e,t){this.send(C(e)),this.ws.close(pr,tn(t)),this.teardown(t)}teardown(e){if(!this.closed){this.closed=!0,this.turnLaneAbort?.abort(),this.turnLaneAbort=null,this.releaseVoiceBinding(),this.audioUnsubscribe?.(),this.audioUnsubscribe=null,this.binding.release&&Promise.resolve().then(()=>this.binding.release?.()).catch(t=>nn(this.id,`binding release failed: ${t instanceof Error?t.message:String(t)}`));try{this.ws.removeAllListeners("message")}catch{}this.onClosed?.(this),nn(this.id,e)}}},ft=[];function nn(n,e){ft.push({id:n,reason:e}),ft.length>256&&ft.shift()}T();import{SessionFailedError as yr}from"@urun-sh/core";T();import{COLD_BOOT_WHEN_LIVE_TIMEOUT_MS as hr}from"@urun-sh/core";var _r={no_capacity:429,queue_timeout:429,cold_start_exceeded:429,queue_full:429,admission_busy:503,admission_unavailable:503,backend_error:502};function Ze(n){if(n==null)return null;let e=_r[n];return e===void 0?null:{status:e,code:n,retryable:e!==502,openaiType:e===429?"capacity_error":"upstream_error",anthropicType:e===429?"rate_limit_error":e===503?"overloaded_error":"api_error"}}function et(n){if(n.kind==="create-failed")return Ze(n.status.error?.code??null);if(n.kind!=="never-live")return null;let e=(n.status.elapsedMs??0)>=hr;return Ze(e?"cold_start_exceeded":"queue_timeout")}import{WebSocketServer as vr}from"ws";var gt="/v1/realtime",wr=n=>n.slice(0,120);function xr(n,e,t){let r=e===401||e===403?"Unauthorized":e===502?"Upstream Fault":"Upgrade Refused";n.write(`HTTP/1.1 ${e} ${r}\r
6
- Connection: close\r
7
- Content-Type: text/plain\r
8
- \r
9
- ${wr(t)}\r
10
- `),n.destroy()}function ht(n,e){let t=new vr({noServer:!0}),r=new Set;return n.on("upgrade",(o,s,i)=>{if(new URL(o.url??"/","http://localhost").pathname!==gt)return;r.add(s),s.on("close",()=>{r.delete(s)});let d=l=>{let c=l,w=c.session!=null&&typeof c.session.sendResponseCreate=="function",f=c.clients!=null&&typeof c.clients.createResponse=="function";if(!w&&!f)throw new Error("resolver returned a binding without an upstream turn seam (session.sendResponseCreate or clients.createResponse required) \u2014 refusing the upgrade LOUD");return new Promise((E,R)=>{t.handleUpgrade(o,s,i,m=>{try{new Ue({ws:m,binding:l}),E()}catch(h){R(h)}})})};(typeof e.resolver=="function"?e.resolver:e.resolver.resolve.bind(e.resolver))(o).then(d).catch(l=>{let c=l instanceof yr?et(l):null,w=null;if(l!==null&&typeof l=="object"&&"status"in l){let R=l.status;typeof R=="number"&&(w=R)}let f=w??c?.status??502,E=l instanceof Error?l.message:String(l);s.destroyed||xr(s,f,E)}).finally(()=>{r.delete(s)})}),async()=>{let o=Date.now()+5e3;for(;r.size>0&&Date.now()<o;)await new Promise(s=>setTimeout(s,25));await new Promise(s=>t.close(()=>s()))}}T();T();T();import{createServer as Oo}from"http";T();import{WebSocketServer as Pr,WebSocket as qe}from"ws";T();T();import{randomUUID as br}from"crypto";var rn="Inference-Id";function Le(){return br()}function _t(n){return n==="/v1"||n.startsWith("/v1/")}function on(n,e){if(!_t(n)||e===null)throw new Error(`no inference turn for ${n}: an inference lane was reached off the /v1 surface, so its response would carry no Inference-Id and could never be billed`);return e}function $e(n){return`resp_${n}`}function sn(n){return`chatcmpl-${n}`}function an(n){return`msg_${n}`}function Z(n){return e=>{n.sessionId=e.sessionId,n.catalog=e.identity}}function un(n){let e=n.now??(()=>performance.now());return{event:"inference_request",ts:new Date().toISOString(),inference_id:n.turn.id,method:n.method,path:n.path,status:n.res.headersSent?n.res.statusCode:null,delivered:n.res.writableFinished,stream_error:n.turn.streamError,model:n.turn.model,stream:n.turn.stream,org:n.turn.org,duration_ms:e()-n.turn.startedAt,ttft_ms:n.turn.stream===!0?n.turn.ttftMs:null,prompt_tokens:n.turn.promptTokens,completion_tokens:n.turn.completionTokens,output_units:n.turn.outputUnits,session_id:n.turn.sessionId,queue_ms:n.turn.timing?.queue_ms??null,prefill_ms:n.turn.timing?.prefill_ms??null,decode_ms:n.turn.timing?.decode_ms??null,total_ms:n.turn.timing?.total_ms??null,catalog:n.turn.catalog}}function dn(n){return`${JSON.stringify(n)}
11
- `}var kr=256,Rr=128;function ee(n){return typeof n.model!="string"||!n.model.trim()?"(default)":n.model.trim().slice(0,Rr)}var pe=class n{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(e,t,r){this.requests[e]=(this.requests[e]??0)+1,this.localInBytes+=t,this.naiveInBytes+=r}modelEntry(e){let t=this.models[e];if(t)return t;let r=Object.keys(this.models).length>=kr?"(other)":e;return this.models[r]??={requests:0,tokens_out:0}}model(e){this.modelEntry(e).requests+=1}track(e,t,r){let o=this,s=performance.now(),i=null,a=0;return(async function*(){for await(let u of e)u.type==="response.output_text.delta"&&typeof u.delta=="string"&&(i===null&&(i=performance.now(),o.ttftMs.push(i-s),o.ttftMs.length>512&&o.ttftMs.shift(),r!==void 0&&(r.ttftMs=i-r.startedAt)),a+=1,o.tokensOut+=1,o.charsOut+=u.delta.length,t!==void 0&&(o.modelEntry(t).tokens_out+=1)),yield u;let d=(performance.now()-(i??s))/1e3;a>0&&d>0&&(o.tokRates.push(a/d),o.tokRates.length>512&&o.tokRates.shift())})()}static p50(e){if(!e.length)return null;let t=[...e].sort((r,o)=>r-o);return t[Math.floor(t.length/2)]}snapshot(){let e=Object.values(this.requests).reduce((t,r)=>t+r,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:n.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:n.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:e},persistent_connections:1,history_bytes_saved:Math.max(0,this.naiveInBytes-this.localInBytes),history_bytes_saved_pct:this.naiveInBytes>0?Math.round((1-this.localInBytes/this.naiveInBytes)*1e3)/10:null}}}},Sr=256;function Ir(n){if(typeof n=="string")return[{role:"user",content:n}];if(Array.isArray(n))return n;throw new Error("responses input must be a string or an array of messages/items")}var me=class extends Error{},ye=class{conversations=new Map;thread(e,t){let r=Ir(t);if(e==null)return r;let o=this.conversations.get(String(e));if(!o)throw new me(`unknown previous_response_id ${String(e)} (proxy restarts drop stored responses)`);return[...o,...r]}remember(e,t,r,o=!0){this.conversations.set(e,[...t,...r]);for(let s of this.conversations.keys()){if(this.conversations.size<=Sr)break;this.conversations.delete(s)}}forget(e){this.conversations.delete(e)}},ln=new WeakMap;function Ne(n){let e=ln.get(n);return e||(e=new ye,ln.set(n,e)),e}function ve(n){let e=n?.output;return Array.isArray(e)?e.filter(t=>t.type==="function_call"):[]}function te(n){return n.find(e=>e.type==="response.completed")?.response??null}function we(n){return n.type!=="response.function_call_arguments.delta"||typeof n.tool_index!="number"?null:{tool_index:n.tool_index,call_id:typeof n.call_id=="string"?n.call_id:void 0,name:typeof n.name=="string"?n.name:void 0,delta:typeof n.delta=="string"?n.delta:""}}var ie=class{calls=new Map;add(e){let t=this.calls.get(e.tool_index),r=!t;return t||(t={args:""},this.calls.set(e.tool_index,t)),e.call_id&&(t.id=e.call_id),e.name&&(t.name=e.name),t.args+=e.delta,{call:t,isNew:r}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([e,t])=>({type:"function_call",call_id:t.id??`call_${e}`,name:t.name??"",arguments:t.args}))}},Ar=["<tool_call>","<function="];function Er(n){return Ar.find(e=>n.includes(e))}function fe(n,e){return`serving row emitted a prose tool call (literal ${JSON.stringify(e)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${n}" (see catalog parser_defaults)`}function ge(n,e,t){if(!(!n||e>0))return Er(t)}function yt(n,e){let t=e?.output_text;return typeof t=="string"?t:n.filter(r=>r.type==="response.output_text.delta"&&typeof r.delta=="string").map(r=>r.delta).join("")}function Or(n){let e=n?.output;if(!Array.isArray(e))return[];let t=[];for(let r of e){if(r===null||typeof r!="object")continue;let o=r;o.type==="message"?t.push({role:"assistant",content:o.content??""}):o.type==="function_call"&&t.push({type:"function_call",call_id:o.call_id,name:o.name,arguments:o.arguments})}return t}function vt(n,e){let t=Or(n);return t.length>0?t:e?[{role:"assistant",content:e}]:[]}var K=class extends Error{constructor(t,r,o=400,s){super(t);this.code=r;this.status=o;this.param=s}code;status;param};function xe(n,e){try{return n.thread(e.previous_response_id,e.input)}catch(t){throw t instanceof me?new K(t.message,"previous_response_not_found",400,"previous_response_id"):new K(String(t instanceof Error?t.message:t),"invalid_input")}}function cn(n){let e=n.text;if(e!==null&&typeof e=="object"){let t=e.format;if(t!==void 0)return t}}async function wt(n,e,t,r,o){if(e.instructions!==void 0&&typeof e.instructions!="string")throw new K("instructions must be a string","invalid_instructions");let s=typeof e.instructions=="string"?e.instructions:void 0;return n.createResponse({model:e.model,input:t,stream:r,tools:e.tools,tool_choice:e.tool_choice,temperature:e.temperature,top_p:e.top_p,stop:e.stop,...s!==void 0?{instructions:s}:{},max_output_tokens:e.max_output_tokens,...e.reasoning_effort!==void 0?{reasoning_effort:e.reasoning_effort}:{},...e.chat_template_kwargs!==void 0?{chat_template_kwargs:e.chat_template_kwargs}:{},...cn(e)!==void 0?{response_format:cn(e)}:{}},o)}async function Me(n){let{body:e,clients:t,store:r,stats:o}=n,s=xe(r,e);e.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(s))-Buffer.byteLength(JSON.stringify(e.input)));let i=n.responseId,a=n.remember??e.store!==!1,d=e.store!==!1,u=ee(e);o.model(u);let l=Array.isArray(e.tools)&&e.tools.length>0,c=n.turn,w=await wt(t,e,s,!0,c?Z(c):void 0);return{responseId:i,events:Tr({events:w,conversation:s,responseId:i,modelKey:u,requestHadTools:l,remember:a,persist:d,store:r,stats:o,turn:n.turn})}}async function*Tr(n){let{events:e,conversation:t,responseId:r,modelKey:o,requestHadTools:s,remember:i,persist:a,store:d,stats:u}=n,l=[],c=k=>k.response&&typeof k.response=="object"?{...k,response:{...k.response,id:r}}:k,w=`item_${r.slice(5)}`,f=`rs_${r.slice(5)}`,E=!1,R=!1,m=0;function*h(){E||(E=!0,yield{type:"response.created",response:{id:r,object:"response",status:"in_progress"}})}let g=!1,y=0,S="";function*x(){g&&(g=!1,yield{type:"response.reasoning_text.done",item_id:f,output_index:y,content_index:0,text:S},yield{type:"response.output_item.done",output_index:y,item:{id:f,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:S}],status:"completed"}})}let _=!1,p=0,O="";function*P(k){_&&(_=!1,yield{type:"response.output_text.done",item_id:w,output_index:p,content_index:0,text:O},yield{type:"response.content_part.done",item_id:w,output_index:p,content_index:0,part:{type:"output_text",text:O}},yield{type:"response.output_item.done",output_index:p,item:{id:w,type:"message",role:"assistant",status:k,content:[{type:"output_text",text:O}]}})}let L=new ie,I=null,Y=-1;function*V(k){if(!I)return;let U=I;I=null,yield{type:"response.function_call_arguments.done",item_id:U.fcId,output_index:U.outputIndex,arguments:U.args},yield{type:"response.output_item.done",output_index:U.outputIndex,item:{id:U.fcId,type:"function_call",call_id:U.callId??U.fcId,name:U.name??"",arguments:U.args,status:k}}}let B=!1;for await(let k of u.track(e,o,n.turn)){l.push(k);let U=String(k.type??"");if(U==="response.created"||U==="response.in_progress"){E=!0,yield c(k);continue}if(U==="response.output_item.added"){R=!0,yield c(k);continue}if(U==="response.reasoning_text.delta"&&!R){yield*h(),g||(g=!0,y=m++,yield{type:"response.output_item.added",output_index:y,item:{id:f,type:"reasoning",summary:[],content:[],status:"in_progress"}}),S+=String(k.delta??""),yield{type:"response.reasoning_text.delta",item_id:f,output_index:y,content_index:0,delta:k.delta};continue}if(U==="response.output_text.delta"&&!R){yield*h(),yield*x(),_||(_=!0,p=m++,yield{type:"response.output_item.added",output_index:p,item:{id:w,type:"message",role:"assistant",status:"in_progress",content:[]}},yield{type:"response.content_part.added",item_id:w,output_index:p,content_index:0,part:{type:"output_text",text:""}}),O+=String(k.delta??""),yield{type:U,item_id:w,output_index:p,content_index:0,delta:k.delta};continue}let M=R?null:we(k);if(M){if(yield*h(),yield*x(),yield*P("completed"),L.add(M),M.tool_index!==Y){yield*V("completed"),Y=M.tool_index;let z=m++;I={fcId:`fc_${r.slice(5)}_${M.tool_index}`,outputIndex:z,callId:M.call_id,name:M.name,args:""},yield{type:"response.output_item.added",output_index:z,item:{id:I.fcId,type:"function_call",call_id:I.callId??I.fcId,name:I.name??"",arguments:"",status:"in_progress"}}}I&&(M.call_id&&(I.callId=M.call_id),M.name&&(I.name=M.name),I.args+=M.delta,yield{type:"response.function_call_arguments.delta",item_id:I.fcId,output_index:I.outputIndex,delta:M.delta});continue}if(U==="response.completed"&&!R){let z=ve(k.response),ue=ge(s,L.size+z.length,O);if(ue){B=!0,yield{type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:fe(o,ue)}};break}if(yield*x(),yield*P("completed"),yield*V("completed"),L.size===0)for(let de of z){let se=m++,Q=de.id??de.call_id??`fc_${se}`,Ee=de.arguments??"",_e={id:Q,type:"function_call",call_id:de.call_id??Q,name:de.name??""};yield{type:"response.output_item.added",output_index:se,item:{..._e,arguments:"",status:"in_progress"}},yield{type:"response.function_call_arguments.delta",item_id:Q,output_index:se,delta:Ee},yield{type:"response.function_call_arguments.done",item_id:Q,output_index:se,arguments:Ee},yield{type:"response.output_item.done",output_index:se,item:{..._e,arguments:Ee,status:"completed"}}}yield c(k);continue}yield c(k)}if(yield*x(),yield*P("incomplete"),yield*V("incomplete"),i&&!B){let k=te(l);d.remember(r,t,vt(k,yt(l,k)),a)}}var Cr="/v1/responses",Ur=64*1024*1024,Lr=8*1024*1024,$r=16,pn=32,mn=3600*1e3,Nr=/^[A-Za-z0-9._-]{1,256}$/;function H(n,e,t){return{type:"error",status:n,...t!==void 0?{stream_id:t}:{},error:{...e,...e.code!==void 0?{code:e.code}:{}}}}var Mr=H(400,{type:"invalid_request_error",code:"invalid_stream_id",message:"The 'stream_id' field must be a non-empty string with at most 256 characters and may only contain letters, numbers, underscores, hyphens, and periods.",param:"stream_id"});function kt(){let n,e;return{promise:new Promise((r,o)=>{n=r,e=o}),resolve:n,reject:e}}async function qr(n){for(;n.bufferedAmount>Lr&&n.readyState===qe.OPEN;){let{promise:e,resolve:t}=kt();setTimeout(t,5),await e}}var xt=class extends ye{constructor(t){super();this.shared=t}shared;thread(t,r){if(t==null)return super.thread(t,r);try{return super.thread(t,r)}catch(o){if(!(o instanceof me))throw o;return this.shared.thread(t,r)}}remember(t,r,o,s=!0){s?this.shared.remember(t,r,o):super.remember(t,r,o)}forget(t){super.forget(t),this.shared.forget(t)}},bt=class{constructor(e,t,r,o){this.ws=e;this.clients=t;this.stats=r;this.statusOf=o;this.store=new xt(Ne(t)),e.on("message",(i,a)=>this.onMessage(i,a)),e.on("close",()=>this.dispose()),e.on("error",()=>this.dispose()),setTimeout(()=>{this.closed||(this.sendFrame(H(400,{type:"invalid_request_error",code:"websocket_connection_limit_reached",message:`Responses websocket connection limit reached (${mn/6e4} minutes). Create a new websocket connection to continue.`})),this.ws.close(1001,"connection time limit reached"))},mn).unref?.()}ws;clients;stats;statusOf;store;lanes=new Map;laneOfResponse=new Map;activeTurns=new Set;active=0;waiters=[];closed=!1;onMessage(e,t){if(this.closed)return;if(t){this.sendFrame(H(400,{type:"invalid_request_error",code:"binary_frame",message:"Responses WebSocket frames must be JSON text"})),this.ws.close(1003,"binary frames are not supported");return}let r;try{r=JSON.parse(e.toString())}catch{this.sendFrame(H(400,{type:"invalid_request_error",code:"invalid_json",message:"frame is not valid JSON"}));return}if(r===null||typeof r!="object"||Array.isArray(r)){this.sendFrame(H(400,{type:"invalid_request_error",code:"invalid_event",message:"frame must be a JSON object"}));return}let o=r;if(o.type!=="response.create"){this.sendFrame(H(400,{type:"invalid_request_error",code:"unknown_event",message:`unknown client event type ${JSON.stringify(o.type??null)}; only "response.create" is supported`}));return}if("stream"in o){this.sendFrame(H(400,{type:"invalid_request_error",code:"stream_is_implicit",message:"'stream' is implicit over the Responses WebSocket and must not be sent",param:"stream"}));return}if("background"in o){this.sendFrame(H(400,{type:"invalid_request_error",code:"background_not_supported",message:"'background' is not supported over the Responses WebSocket",param:"background"}));return}let s;if(o.stream_id!==void 0){if(typeof o.stream_id!="string"||!Nr.test(o.stream_id)){this.sendFrame(Mr);return}if(s=o.stream_id,!this.lanes.has(s)&&this.namedLaneCount()>=pn){this.sendFrame(H(400,{type:"invalid_request_error",code:"websocket_stream_limit_reached",message:`This WebSocket connection has reached its maximum number of distinct stream IDs (${pn}). Reuse an existing stream_id or open a new WebSocket connection.`,param:"stream_id"},s));return}}let i=o.generate===!1?()=>this.runWarmup(o,s):()=>this.runTurn(o,s),a=s??"",d=this.lanes.get(a)??Promise.resolve();this.lanes.set(a,d.then(i).catch(()=>{}))}namedLaneCount(){return this.lanes.has("")?this.lanes.size-1:this.lanes.size}async runTurn(e,t){if(!this.closed){await this.acquire();try{let r=Buffer.byteLength(JSON.stringify(e));this.stats.request("responses_ws",r,r);let o;try{o=await Me({body:e,clients:this.clients,store:this.store,stats:this.stats,remember:!0,responseId:$e(Le())})}catch(i){this.failTurn(i,t,e);return}this.laneOfResponse.set(o.responseId,t??null);let s=o.events;this.activeTurns.add(s);try{for await(let i of s){if(this.closed||this.ws.readyState!==qe.OPEN)break;this.send(i,t),await qr(this.ws)}}finally{this.activeTurns.delete(s)}}catch(r){this.failTurn(r,t,e)}finally{this.release()}}}runWarmup(e,t){if(this.closed)return;let r=$e(Le());try{let o=xe(this.store,{...e,input:e.input??(e.previous_response_id!=null?[]:void 0)});this.store.remember(r,o,[],e.store!==!1),this.laneOfResponse.set(r,t??null),this.send({type:"response.created",response:{id:r,object:"response",status:"in_progress"}},t)}catch(o){this.failTurn(o,t,e)}}failTurn(e,t,r){this.sendFrame(this.errorFrameOf(e,t));let o=r.previous_response_id;typeof o=="string"&&this.laneOfResponse.get(o)===(t??null)&&(this.store.forget(o),this.laneOfResponse.delete(o))}errorFrameOf(e,t){let r=String(e instanceof Error?e.message:e);if(e instanceof K)return H(e.status,{type:"invalid_request_error",code:e.code,message:e.message,...e.param!==void 0?{param:e.param}:{}},t);if(e instanceof G)return H(404,{type:"invalid_request_error",code:"model_not_found",message:r},t);if(e instanceof Oe)return H(503,{type:"upstream_error",code:"session_gone",message:r},t);let o=this.statusOf?.(e)??null;return o?H(o.status,{type:o.openaiType,message:r},t):H(500,{type:"upstream_error",message:r},t)}async acquire(){for(;this.active>=$r;){let{promise:e,resolve:t}=kt();this.waiters.push(t),await e}this.active++}release(){this.active--,this.waiters.shift()?.()}dispose(){if(!this.closed){this.closed=!0;for(let e of[...this.activeTurns])e.return(void 0);this.activeTurns.clear(),this.lanes.clear(),this.laneOfResponse.clear();for(let e of this.waiters.splice(0))e()}}send(e,t){if(this.closed||this.ws.readyState!==qe.OPEN)return;let r=t===void 0?e:{...e,stream_id:t};this.ws.send(JSON.stringify(r))}sendFrame(e){this.closed||this.ws.readyState!==qe.OPEN||this.ws.send(JSON.stringify(e))}};function tt(n,e,t){let r=e===401?"Unauthorized":e===404?"Not Found":"Internal Server Error",o=`${t}\r
12
- `;n.end(`HTTP/1.1 ${e} ${r}\r
13
- Connection: close\r
14
- Content-Type: text/plain; charset=utf-8\r
15
- Content-Length: ${Buffer.byteLength(o)}\r
16
- \r
17
- ${o}`)}function fn(n,e){let t=new Pr({noServer:!0,maxPayload:Ur}),r=new pe,o=new Set,s=new Set,i=new Set,a=typeof e.clients=="function"?e.clients:()=>e.clients,d=!1,u=(l,c,w)=>{let f;try{f=new URL(l.url??"/","http://localhost")}catch{tt(c,400,"malformed upgrade URL");return}if(c.on("error",()=>c.destroy()),c.on("close",()=>i.delete(c)),f.pathname!==Cr){i.add(c);return}s.add(c);let E=!1;(async()=>{if(e.apiKey&&(l.headers.authorization??"")!==`Bearer ${e.apiKey}`){tt(c,401,"invalid local proxy api key");return}let R;try{R=await a(l)}catch(m){let h=e.statusOf?.(m)??null;tt(c,h?.status??500,"upstream resolver rejected the connection");return}d||c.destroyed||(E=!0,t.handleUpgrade(l,c,w,m=>{s.delete(c),o.add(m),m.on("close",()=>o.delete(m));try{new bt(m,R,r,e.statusOf)}catch{o.delete(m),m.close(1011,"responses websocket setup failed")}}))})().catch(()=>{!E&&!c.destroyed&&tt(c,500,"responses websocket upgrade failed")}).finally(()=>s.delete(c))};return n.on("upgrade",u),{stats:r,close:()=>{let{promise:l,resolve:c}=kt();if(d)return c(),l;d=!0,n.off("upgrade",u);for(let f of s)f.destroy();s.clear();for(let f of i)f.destroy();i.clear();for(let f of o)f.close(1001,"server shutting down");let w=setTimeout(()=>{for(let f of o)f.readyState!==qe.CLOSED&&f.terminate()},2e3);return w.unref?.(),t.close(()=>{clearTimeout(w),c()}),l}}}T();import Fr from"os";import Dr from"path";import{fileURLToPath as jr}from"url";import{format as Gr}from"util";var gn="0.5.6";var _n=4141,Hr=gn,Hs={claude:{bin:"claude",env:"anthropic"},codex:{bin:"codex",env:"openai"},pi:{bin:"pi",env:"openai"},omp:{bin:"omp",env:"openai"},aider:{bin:"aider",env:"openai"},opencode:{bin:"opencode",env:"openai"},crush:{bin:"crush",env:"openai"}};function Wr(n,e="openai"){if(e==="anthropic")return{ANTHROPIC_BASE_URL:`http://127.0.0.1:${n}`,ANTHROPIC_API_KEY:"urun-local"};let t=`http://127.0.0.1:${n}/v1`;return{OPENAI_BASE_URL:t,OPENAI_API_BASE:t,OPENAI_API_KEY:"urun-local"}}function Ws(n,e,t){let r={...n,...Wr(e,t)};return delete r.URUN_API_KEY,delete r.URUN_JWT,r}function Js(n,e,t){let r=["-c","model_providers.urun.name=urun","-c",`model_providers.urun.base_url="http://127.0.0.1:${n}/v1"`,"-c",'model_providers.urun.wire_api="responses"',"-c",'model_provider="urun"'],o=t.indexOf("--"),s=o===-1?t:t.slice(0,o),i=s.some(l=>l==="-m"||l==="--model"||l.startsWith("--model=")),a=t[0]==="exec",d=s.includes("--skip-git-repo-check"),u=a&&!d?["exec","--skip-git-repo-check",...t.slice(1)]:t;return[...r,...i?[]:["-m",e],...u]}function Vs(n,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--provider","urun","--model",n,...e]}function zs(n,e){return e.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?e:["--model",`urun/${n}`,...e]}function Ks(n,e,t){let r=(n.URUN_PI_EXTENSION_ENTRY??"").trim();if(r){if(!t(r))throw new Error(`URUN_PI_EXTENSION_ENTRY points at ${r}, which does not exist \u2014 the launcher that injected it (the vendored urun CLI shim) must materialize the pi extension bundle before spawning pi`);return r}let o=jr(new URL("../pi-extension/standalone.cjs",e));if(!t(o))throw new Error(`pi extension entry not found at ${o} \u2014 the pi lane loads the self-contained @urun-sh/openai dist/pi-extension/standalone.cjs from the installed package (run \`urun compat pi\` via the npm-installed CLI, e.g. \`npx @urun-sh/openai\`), or pass an entry to pi yourself: \`pi -e <path-to-@urun-sh/openai>/dist/pi-extension/standalone.cjs\``);return o}function yn(n){let e=Number(n);if(!Number.isInteger(e)||e<0)throw new Error(`--port needs a non-negative integer (0 = OS-assigned ephemeral), got ${n}`);return e}function Ys(n){if(n[0]!=="--port")return null;let e=yn(n[1]);return n.splice(0,2),e}function Xs(n){let e=n.indexOf("--port");if(e===-1)return _n;let t=yn(n[e+1]);return n.splice(e,2),t}function Qs(n){if(n===0)throw new Error("`env` cannot use --port 0: it starts no proxy, so the OS never assigns the ephemeral port \u2014 pass the actual port of the running proxy");return n}function Rt(n,e){let t=(n[e]??"").trim();if(!t)throw new Error(`${e} is not set. The proxy needs URUN_BASE_URL, URUN_ORG_ID, URUN_APP and either URUN_API_KEY (the org key; the proxy vends short-lived function-scoped client tokens from it via the SDK) or URUN_JWT (an explicit pre-vended token override) \u2014 plus optional URUN_FUNCTION, default "serve" \u2014 to open the backhaul session.`);return t}function Zs(n){let e=Jr(Rt(n,"URUN_BASE_URL")),t=Rt(n,"URUN_ORG_ID"),r=Rt(n,"URUN_APP"),o=(n.URUN_FUNCTION??"serve").trim()||"serve",s=(n.URUN_API_URL??"").trim(),i=s?hn(s,"URUN_API_URL"):Nt,a=(n.URUN_JWT??"").trim();if(a)return{baseUrl:e,orgId:t,appId:r,fnName:o,apiUrl:i,auth:{lane:"jwt",jwt:a}};let d=(n.URUN_API_KEY??"").trim();if(d){let u=(n.URUN_GATEWAY_URL??"").trim(),l=u?hn(u):void 0;return{baseUrl:e,orgId:t,appId:r,fnName:o,apiUrl:i,auth:{lane:"api-key",apiKey:d,gatewayUrl:l}}}throw new Error("neither URUN_API_KEY nor URUN_JWT is set. Set URUN_API_KEY (the org API key \u2014 the proxy vends short-lived, function-scoped client tokens from it via the SDK, so the key never reaches the agent process) or URUN_JWT (an explicit pre-vended token; when set it always wins). URUN_BASE_URL, URUN_ORG_ID and URUN_APP are also required.")}function Jr(n,e="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(n).pathname))throw new Error(`${e} must be the session-gateway base (e.g. https://api.urun.sh), got ${n} \u2014 a trailing /v1 is the org control-plane API form (that belongs in URUN_API_URL); session allocation 404s against it. Drop the /v1.`);return n}function hn(n,e="URUN_GATEWAY_URL"){let t=new URL(n),r=t.hostname==="localhost"||t.hostname==="127.0.0.1"||t.hostname==="[::1]";if(t.protocol!=="https:"&&!r)throw new Error(`${e} must be https (got ${n}) \u2014 the org API key rides the request's Authorization header`);return n}function ei(n){return{app:n.appId,org:n.orgId,fn:n.fnName,base_url:n.baseUrl,proxy_version:Hr}}function Vr(n,e){return n.app===e.app&&n.org===e.org&&n.fn===e.fn&&n.base_url===e.base_url&&n.proxy_version===e.proxy_version}var zr=250;async function Kr(n,e=zr){try{let t=await fetch(`http://127.0.0.1:${n}/stats`,{signal:AbortSignal.timeout(e)});if(!t.ok)return null;let r=(await t.json()).identity;if(r==null||typeof r!="object")return null;let{app:o,org:s,fn:i,base_url:a,proxy_version:d}=r;return typeof o!="string"||typeof s!="string"||typeof i!="string"||typeof a!="string"||typeof d!="string"?null:{app:o,org:s,fn:i,base_url:a,proxy_version:d}}catch{return null}}var vn=2e3;async function Yr(n,e=vn){try{let t=await fetch(`http://127.0.0.1:${n}/v1/models`,{signal:AbortSignal.timeout(e)});return t.ok?null:`GET /v1/models answered HTTP ${t.status}`}catch(t){return`GET /v1/models failed (${String(t instanceof Error?t.message:t)})`}}async function ti(n,e,t=_n,r=vn){if(n!=null)return{mode:"spawn",port:n};let o=await Kr(t);if(o!==null&&Vr(o,e)){let s=await Yr(t,r);return s===null?{mode:"reuse",port:t,identity:o}:{mode:"spawn",port:0,reason:`a urun-openai proxy on port ${t} matches this invocation's identity but failed the serving-surface liveness check (${s}) \u2014 starting a fresh proxy on an ephemeral port instead of pointing the harness at it`}}return{mode:"spawn",port:0}}function ni(n,e=Fr.homedir()){return Dr.join(e,".urun","logs",`compat-proxy-${n}.log`)}var St=["log","info","warn","error","debug","trace"];function ri(n,e=process,t=console){let r=e.stdout.write,o=e.stderr.write,s=(a,...d)=>n.write(a,...d);e.stdout.write=s,e.stderr.write=s;let i=Object.fromEntries(St.map(a=>[a,t[a]]));for(let a of St)t[a]=(...d)=>{n.write(`${Gr(...d)}
18
- `)};return()=>{e.stdout.write=r,e.stderr.write=o;for(let a of St)t[a]=i[a]}}var Xr=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function wn(n){let e=n?.code;return typeof e=="string"&&Xr.has(e)}function be(n,e){try{return n.write(e),!0}catch(t){if(wn(t))return!1;throw t}}function oi(n){let e={stdout:!1,stderr:!1},t=[];for(let r of["stdout","stderr"]){let o=n.streams[r],s=i=>{wn(i)&&(e[r]=!0);let a=i?.code;try{n.note(`${new Date().toISOString()} urun compat: process.${r} died (${String(a??(i instanceof Error?i.message:i))}) \u2014 the terminal is gone; launcher output continues in this log only
19
- `)}catch{}};o.on("error",s),t.push([o,s])}return{isDead:r=>e[r],uninstall:()=>{for(let[r,o]of t)r.off("error",o)}}}function si(n){let e=t=>{let r=t instanceof Error?`${t.stack??`${t.name}: ${t.message}`}`:String(t);try{n.appendSync(n.logPath,`${new Date().toISOString()} launcher crashed (in-process proxy): ${r}
20
- `)}catch{}n.restore();try{n.writeStderr(`urun compat: the in-process proxy crashed (${t instanceof Error?t.message:String(t)}) \u2014 terminating the harness (its backend is gone); details: ${n.logPath}
21
- `)}catch(o){try{n.appendSync(n.logPath,`${new Date().toISOString()} urun compat: the crash line could not reach stderr (${String(o?.code??o)}) \u2014 the terminal is gone
22
- `)}catch{}}n.killChild(),n.exit(1)};return n.proc.on("uncaughtException",e),n.proc.on("unhandledRejection",e),()=>{n.proc.off("uncaughtException",e),n.proc.off("unhandledRejection",e)}}function ii(n,e){let t="pass --port 0 for an OS-assigned ephemeral port";return e?`port ${n} is held by another urun-openai proxy serving app ${e.app} (org ${e.org}, fn ${e.fn}, version ${e.proxy_version}) \u2014 reuse it by pointing your agent at http://127.0.0.1:${n}, or ${t}`:`port ${n} is already in use and does not answer the urun-openai /stats identity probe \u2014 ${t}`}T();var xn="/v1/images/generations",nt="/v1/images/edits";function bn(n){return n===xn||n===nt}var kn="/v1/images/models";async function Rn(n,e,t){if(n){if(n.model=ee({model:t}),n.stream=!1,typeof e.catalogIdentity!="function"){n.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(t);if(r!==null){n.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(t):null;n.catalog={kind:"not-a-catalog-model",model:o??t??null}}catch(r){n.catalog={kind:"unresolved",reason:`model ${JSON.stringify(t??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}}var A=class extends Error{status;param;constructor(e,t,r){super(t),this.status=e,this.param=r}},Qr=16*1024*1024,Sn=64*1024*1024;function In(n){return n.length>=8&&n[0]===137&&n[1]===80&&n[2]===78&&n[3]===71&&n[4]===13&&n[5]===10&&n[6]===26&&n[7]===10?"image/png":n.length>=3&&n[0]===255&&n[1]===216&&n[2]===255?"image/jpeg":n.length>=12&&n[0]===82&&n[1]===73&&n[2]===70&&n[3]===70&&n[8]===87&&n[9]===69&&n[10]===66&&n[11]===80?"image/webp":null}function Be(n,e){n.statusCode=e.status,n.setHeader("content-type","application/json"),n.end(JSON.stringify({error:{message:e.message,type:e.status>=500?"upstream_error":"invalid_request_error",...e.param?{param:e.param}:{}}}))}var ne=class extends Error{};async function Zr(n,e){let t=Number(n.headers["content-length"]??"0");if(Number.isFinite(t)&&t>e)throw new ne(`request body exceeds the ${e}-byte limit`);let r=[],o=0;for await(let s of n){let i=s;if(o+=i.length,o>e)throw new ne(`request body exceeds the ${e}-byte limit`);r.push(i)}return Buffer.concat(r)}async function An(n,e,t,r){let o=await n.createResponse({model:e,input:[{role:"user",content:t}],stream:!1},r),s=null;for await(let c of o){if(c.type==="error"){let w=c.error??{};throw new A(502,`image upstream error: ${String(w.message??"upstream reported an error")}`)}if(c.type==="response.completed"){s=c.response??null;break}}if(s===null)throw new A(502,"image upstream produced no terminal response \u2014 refusing to fabricate an image");let i=s.images;if(!Array.isArray(i)||i.length!==1||typeof i[0]!="string"||i[0].length===0)throw new A(502,`image upstream returned ${Array.isArray(i)?i.length:"a non-array images field"} images for the single-shot (n=1) contract, which produces exactly one \u2014 refusing to truncate or fabricate`);let a=[],d=i[0].startsWith("data:")?i[0].slice(i[0].indexOf(";base64,")+8):i[0],u=Buffer.from(d,"base64");if(In(u)===null)throw new A(502,`image upstream returned a payload that is not a PNG/JPEG/WebP image (${u.length} bytes) \u2014 refusing to pass it through`);return a.push(d),{images:a}}function En(n){return n instanceof A?n:n instanceof G?new A(404,n.message):new A(502,`image upstream failed: ${String(n instanceof Error?n.message:n)}`)}async function On(n,e,t,r){if(await n(e,t))return!0;let o=t==="edit"?"no image-input (expects_image) edit destination":"no pure text-to-image (expects_image=false) generation destination";return Be(r,new A(404,e===void 0?`no image-capable default model is configured for this caller (${o}) \u2014 name an image-capable \`model\``:`model "${e}" is not ${t==="edit"?"edit-capable":"generation-capable"} for this caller (${o}; capability is read from the catalog's modality metadata, not the serve function name)`)),!1}function Tn(n,e){n&&(n.outputUnits=e.images.length)}function Pn(n,e){n.statusCode=200,n.setHeader("content-type","application/json"),n.end(JSON.stringify({created:Math.floor(Date.now()/1e3),data:e.images.map(t=>({b64_json:t}))}))}var eo={model:!0,prompt:!0,response_format:!0,n:!0};async function Cn(n,e,t){let r=t.maxBodyBytes??Sn;try{if(n.method!=="POST")throw new A(405,`${String(n.method)} is not supported on ${xn}`);let o;try{o=JSON.parse((await Zr(n,r)).toString("utf8")||"{}")}catch(c){throw c instanceof ne?new A(413,c.message):new A(400,"request body is not valid JSON")}if(o===null||typeof o!="object"||Array.isArray(o))throw new A(400,"request body must be a JSON object");let s=o;for(let c of Object.keys(s))if(!eo[c])throw new A(400,`request field "${c}" is not implemented: the native image request schema carries only the prompt (size, quality, style, seed, mask and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, n.`,c);let i=s.prompt;if(typeof i!="string"||i.trim().length===0)throw new A(400,"prompt is required and must be a non-empty string","prompt");let a=s.model===void 0?void 0:s.model;if(a!==void 0&&typeof a!="string")throw new A(400,"model must be a string","model");await Rn(t.turn,t.clients,a);let d=s.n??1;if(d!==1)throw new A(400,`n=${JSON.stringify(d)} is not supported: the native image request is single-shot and produces exactly one image per request (no batch support yet)`,"n");let u=s.response_format??"b64_json";if(u!=="b64_json")throw new A(400,`response_format ${JSON.stringify(u)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");if(t.stats?.request("images_generations",i.length,i.length),!await On(t.imageModels,a,"generation",e))return;let l=[{type:"input_text",text:i}];try{let c=await An(t.clients,a,l,t.turn?Z(t.turn):void 0);Tn(t.turn,c),Pn(e,c)}catch(c){throw En(c)}}catch(o){if(o instanceof ne){Be(e,new A(413,o.message));return}if(o instanceof A){Be(e,o);return}throw o}}var to={model:!0,prompt:!0,response_format:!0,image:!0,"image[]":!0};async function Un(n,e,t){let r=t.maxBodyBytes??Sn,o=t.maxImagePartBytes??Qr;try{if(n.method!=="POST")throw new A(405,`${String(n.method)} is not supported on ${nt}`);let s=String(n.headers["content-type"]??"");if(!s.toLowerCase().startsWith("multipart/form-data"))throw new A(400,`the edit endpoint consumes multipart/form-data, got ${s||"none"}`);let i=n[Symbol.asyncIterator](),a=0,d=new ReadableStream({async pull(m){try{let h=await i.next();if(h.done){m.close();return}if(a+=h.value.length,a>r){m.error(new ne(`request body exceeds the ${r}-byte limit`));return}m.enqueue(h.value)}catch(h){m.error(h)}},cancel(){i.return?.()}}),u;try{let m=new Headers;for(let[y,S]of Object.entries(n.headers))if(typeof S=="string")m.set(y,S);else if(Array.isArray(S))for(let x of S)m.append(y,x);let h={method:"POST",headers:m,body:d,duplex:"half"};u=await new Request(`https://proxy.internal${n.url??"/"}`,h).formData()}catch(m){throw m instanceof ne?new A(413,m.message):new A(400,`malformed multipart body: ${String(m instanceof Error?m.message:m)}`)}let l=u.get("prompt");if(typeof l!="string"||l.trim().length===0)throw new A(400,"prompt is required and must be a non-empty string field","prompt");let c=u.get("model");if(c!==null&&typeof c!="string")throw new A(400,"model must be a string field","model");let w=c===null?void 0:c;await Rn(t.turn,t.clients,w);let f=u.get("response_format")??"b64_json";if(f!=="b64_json")throw new A(400,`response_format ${JSON.stringify(f)} is not supported: the runtime returns bare base64 PNG and exposes no image hosting, so b64_json is the only output format`,"response_format");for(let m of u.keys())if(!to[m])throw new A(400,`form field "${m}" is not implemented: the native image request schema carries the prompt and the image inputs only (mask, size, seed and similar knobs are per-deployment engine config, not per-request fields). Supported fields: model, prompt, response_format, image (or image[]).`,m);let E=[];for(let m of["image","image[]"])for(let h of u.getAll(m)){if(!(h instanceof File))throw new A(400,"image must be an uploaded file: the proxy accepts only multipart file bytes and never fetches URLs or server-side paths",m);if(h.size>o)throw new A(413,`image part exceeds the ${o}-byte limit (the serve protocol's MAX_IMAGE_BYTES)`,m);let g=new Uint8Array(await h.arrayBuffer()),y=In(g);if(y===null)throw new A(400,"image payload is not a supported format \u2014 supported: PNG, JPEG, WebP (detected by magic bytes, not the declared Content-Type)",m);E.push({bytes:g,mime:y})}if(E.length===0)throw new A(400,"the edit endpoint requires at least one `image` file part","image");if(t.stats?.request("images_edits",l.length,l.length),!await On(t.imageModels,w,"edit",e))return;let R=[{type:"input_text",text:l}];for(let{bytes:m,mime:h}of E)R.push({type:"input_image",image_url:`data:${h};base64,${Buffer.from(m).toString("base64")}`});try{let m=await An(t.clients,w,R,t.turn?Z(t.turn):void 0);Tn(t.turn,m),Pn(e,m)}catch(m){throw En(m)}}catch(s){if(s instanceof ne){Be(e,new A(413,s.message));return}if(s instanceof A){Be(e,s);return}throw s}}T();var qn="/v1/usage/requests",Ln=1e4,no=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,ae=class extends Error{constructor(t,r=400){super(t);this.status=r;this.name="UsageRequestError"}status},q=class extends Error{constructor(e){super(e),this.name="UsageSurfaceError"}},rt=class extends q{constructor(e){super(e),this.name="UsageWithholdingError"}},Bn=1024*1024;function Et(n,e){if(n===null||typeof n!="object"||Array.isArray(n))throw new ae(`request body must be a JSON object of the form {"${e}": ["<uuid>", ...]}`);let t=n[e];if(!Array.isArray(t))throw new ae(`"${e}" must be an array of uuid strings`);if(t.length>Ln)throw new ae(`"${e}" carries ${t.length} ids; at most ${Ln} may be looked up in one request. Split the batch \u2014 the ids past the limit are NOT silently dropped`);let r=[],o=new Set;for(let s of t){if(typeof s!="string"||!no.test(s))throw new ae(`"${e}" contains ${JSON.stringify(s)}, which is not a LOWERCASE uuid \u2014 every uRun inference id is the lowercase uuid returned in the \`Inference-Id\` response header, and answering a differently-cased spelling would echo back an id the caller cannot match`);o.has(s)||(o.add(s),r.push(s))}return r}function Fn(n){return Et(n,"inference_ids")}var ro=["inference_id","model_id","identity_kind","variant","task","http_status","started_at","completed_at","prompt_tokens","completion_tokens","output_units","cost_nano_usd","price_version","priced_at"];function ke(n,e,t){let r=n[e];if(typeof r!="string"||r==="")throw new q(`${t}: ${e} is not a non-empty string (got ${Re(r)})`);return r}function It(n,e,t){let r=n[e];if(r===null)return null;if(typeof r!="string"||r==="")throw new q(`${t}: ${e} is neither null nor a non-empty string (got ${Re(r)})`);return r}function Fe(n,e,t,r){let o=n[e];if(o===null)return null;let s=typeof o=="number"?o:typeof o=="string"&&/^-?\d+(\.\d+)?$/.test(o.trim())?Number(o.trim()):NaN;if(!Number.isFinite(s))throw new q(`${t}: ${e} is not a number (got ${Re(o)})`);if(r&&!Number.isSafeInteger(s))throw new q(`${t}: ${e} is not a safely representable integer \u2014 a billing quantity that silently loses digits must fail rather than be reported`);if(s<0)throw new q(`${t}: ${e} is negative`);return Object.is(s,-0)?0:s}var oo=40,$n=64,Nn=600;function Dn(n){let e=Object.keys(n).sort(),r=e.slice(0,oo).map(o=>o.length>$n?`${o.slice(0,$n)}\u2026`:o).join(", ");return r.length>Nn&&(r=`${r.slice(0,Nn)}\u2026`),`${e.length} keys present: [${r}]`}function Re(n){return n===null?"null":n===void 0?"undefined":Array.isArray(n)?"an array":typeof n=="object"?"an object":`a ${typeof n}`}function At(n){be(process.stdout,`${JSON.stringify({event:"usage_row_rejected",ts:new Date().toISOString(),where:n.where,reason:n.reason})}
23
- `)}function so(n,e){let t;try{t=n(e)}catch{Mn(n,e);return}io(t)&&Promise.resolve(t).catch(()=>{Mn(n,e)})}function io(n){return typeof n?.then=="function"}function Mn(n,e){if(n!==At)try{At(e)}catch{}}function gi(n,e=At){let t=n?.requests;if(!Array.isArray(t)){let o=n!==null&&typeof n=="object"&&!Array.isArray(n)?Dn(n):Re(n);throw new q(`the usage surface answered without a "requests" array: got ${Re(t)} (${o})`)}let r=[];for(let o=0;o<t.length;o++){let s=`usage.requests[${o}]`;try{r.push(ao(t[o],s))}catch(i){if(i instanceof rt||!(i instanceof q))throw i;so(e,{where:s,reason:i.message})}}if(t.length>0&&r.length===0)throw new q(`the usage surface returned ${t.length} row(s) and NONE of them could be validated \u2014 answering an empty result here would be indistinguishable from "we hold nothing for these ids", which a billing caller treats as final and writes the whole batch off unbilled`);return r}function ao(n,e){if(n===null||typeof n!="object"||Array.isArray(n))throw new q(`${e} is not an object: got ${Re(n)}`);let t=n;if(Object.hasOwn(t,"gpu_seconds"))throw new rt(`${e} carries gpu_seconds \u2014 that column was DROPPED from the ledger (ENG-417): the cost of compute is metered once, on the instance, and a per-request figure counts the same GPU-second twice. Its reappearance means a second, competing measure has been reintroduced upstream`);for(let o of ro)if(!Object.hasOwn(t,o))throw new q(`${e} has no "${o}" column (${Dn(t)})`);let r=Fe(t,"http_status",e,!0);if(r===null)throw new q(`${e}: http_status is null, but the ledger column is NOT NULL`);return{inference_id:ke(t,"inference_id",e),model_id:ke(t,"model_id",e),identity_kind:ke(t,"identity_kind",e),variant:It(t,"variant",e),task:ke(t,"task",e),http_status:r,started_at:ke(t,"started_at",e),completed_at:ke(t,"completed_at",e),prompt_tokens:Fe(t,"prompt_tokens",e,!0),completion_tokens:Fe(t,"completion_tokens",e,!0),output_units:Fe(t,"output_units",e,!1),cost_nano_usd:Fe(t,"cost_nano_usd",e,!0),price_version:It(t,"price_version",e),priced_at:It(t,"priced_at",e)}}function ot(n,e){let t=new Set(e),r=new Set;for(let o=0;o<n.length;o++){let s=n[o];if(!t.has(s.inference_id))throw new q(`the usage surface returned an id at validated answer position ${o} that was not in the request (${e.length} asked, ${n.length} answered) \u2014 an answer carrying ids nobody asked about cannot be attributed to this caller`);if(r.has(s.inference_id))throw new q(`the usage surface returned ${s.inference_id} more than once \u2014 inference_id is the ledger primary key, so a duplicate means the upstream query is not the one we asked, and a caller that sums the entries would bill the request twice`);r.add(s.inference_id)}}import{SessionAllocationError as To,SessionFailedError as Po}from"@urun-sh/core";T();var Ot="/partners/huggingface/v1/billing/requests";function uo(n){return Et(n,"requestIds")}function lo(n){return n>=200&&n<=399}function co(n){let e=[];for(let t of n)t.cost_nano_usd!==null&&lo(t.http_status)&&e.push({requestId:t.inference_id,costNanoUsd:t.cost_nano_usd});return{requests:e.length>0?e:null}}async function jn(n,e){let t=uo(n),r=await e(t);return ot(r,t),co(r)}T();var po=new Map([["/v1/chat/completions","conversational"],["/v1/responses","conversational"],["/v1/messages","conversational"],["/v1/images/generations","text-to-image"],["/v1/images/edits","image-to-image"]]);function mo(n){return po.get(n)??null}var fo=["duplicate","duplicate_foreign","auth","surface","unknown","dwell_expired","dropped_on_exit"];function go(n){let e=n?.ledgerAlreadyWritten;return e!==null&&typeof e=="object"?e:null}function ho(n){return n==="surface"}function _o(n){let e=n?.ledgerFailureReason;return fo.includes(e)?e:"unknown"}function yo(n){return n.status===null?!1:n.status>=200&&n.status<400&&!n.stream_error}function vo(n){let e=Date.parse(n.ts),t=Number.isFinite(n.duration_ms)?Math.max(0,n.duration_ms):0;return new Date(e-Math.round(t)).toISOString()}var wo=256,Gn=4096,De=12e4,xo=250,bo=8e3,re=[],Tt=new Set,je=0,Se=!1;function bi(){return re.length}function ki(n=Date.now()){let e=re[0];return e===void 0?0:Math.max(0,n-e.queuedAt)}var st=new Set;function Hn(){return je===0&&re.length===0}function Wn(){if(Hn()){Se=!1;for(let n of[...st])n()}}async function Ri(n=Jn){return Hn()?!0:await new Promise(e=>{let t=null,r=()=>{st.delete(r),t&&clearTimeout(t),e(!0)};st.add(r),t=setTimeout(()=>{st.delete(r),e(!1)},n),t.unref?.()})}function Si(){Se=!0;let n=[...re.splice(0,re.length),...Tt];for(let e of n)e.settled||he(e,{outcome:"failed",reason:"dropped_on_exit"},{event:"inference_ledger_write_failed",reason:"dropped_on_exit",attempts:e.attempts,error:`the proxy is shutting down and this row was still unplaced after the ${Jn}ms drain \u2014 the request was served and answered, and its billing row was never written`});return Wn(),n.length}var Jn=5e3;function Vn(n,e){return`${JSON.stringify({event:n,ts:new Date().toISOString(),...e})}
24
- `}function ko(n){be(process.stdout,n)}function zn(n,e){try{n(e)}catch{}}function Kn(n){let{record:e,ledger:t}=n;if(t===null)return;let r=t.report??ko,o=mo(e.path);if(o===null)return;let s=l=>{try{t.observe(l)}catch{}},i=(l,c)=>{s({outcome:"skipped",reason:l}),zn(r,Vn("inference_ledger_skipped",{inference_id:e.inference_id,path:e.path,reason:l,detail:c}))};if(e.status===null){i("no_response_head","no head flushed, so the caller never received an Inference-Id and no status can be recorded honestly");return}if(e.org===null){i("no_org","the request never resolved a tenant, so there is no org to attribute the row to");return}let a=e.catalog;if(a.kind==="unresolved"){i("model_unresolved",a.reason);return}let d;if(a.kind==="catalog")d={model_id:a.model_id,identity_kind:"catalog_model",variant:a.variant};else{if(a.model===null){i("no_model_named","the request resolved to a caller-org app but named no model, so there is no app slug to record and `model_id` cannot be written honestly");return}d={model_id:a.model,identity_kind:"caller_org_app",variant:null}}let u={inference_id:e.inference_id,...d,task:o,http_status:e.status,started_at:vo(e),completed_at:e.ts,billable_outcome:yo(e),session_id:e.session_id,prompt_tokens:e.prompt_tokens,completion_tokens:e.completion_tokens,output_units:e.output_units,queue_ms:e.queue_ms,prefill_ms:e.prefill_ms,decode_ms:e.decode_ms,total_ms:e.total_ms};if(re.length>=Gn){i("queue_full",`${re.length} ledger writes are already waiting (capacity ${Gn}, ${je} in flight) \u2014 the control plane is not draining them fast enough and this row is lost`);return}re.push({entry:u,req:n.req,record:e,ledger:t,report:r,queuedAt:Date.now(),attempts:0,settled:!1}),je<wo&&(je+=1,Ro())}async function Ro(){try{for(;;){let n=So();if(n===void 0)return;Tt.add(n);try{await Io(n)}finally{Tt.delete(n)}}}finally{je-=1,Wn()}}function So(){let n=Date.now();for(;;){let e=re.shift();if(e===void 0)return;let t=n-e.queuedAt;if(t<De)return e;he(e,{outcome:"failed",reason:"dwell_expired"},{event:"inference_ledger_write_failed",reason:"dwell_expired",error:`this row waited ${t}ms for the control plane (bound ${De}ms) after ${e.attempts} attempt(s) and was given up on \u2014 the request was served and answered, and it will never be billed. Hugging Face writes a request off ~30 minutes after serving, so a row placed later than this is not worth the memory to keep trying`})}}async function Io(n){let{entry:e,record:t,ledger:r}=n;for(;;){n.attempts+=1;try{let o=await r.write(n.req,e);he(n,{outcome:"written",reason:o.cost_nano_usd===null?"unpriced":"priced"},{event:"inference_ledger_written",inference_id:o.inference_id,path:t.path,model_id:e.model_id,identity_kind:e.identity_kind,variant:e.variant,org:t.org,session_id:e.session_id,cost_nano_usd:o.cost_nano_usd,price_version:o.price_version,unpriced_reason:o.unpriced_reason,attempts:n.attempts});return}catch(o){let s=go(o);if(s!==null){he(n,{outcome:"already_written",reason:s.cost_nano_usd===null?"unpriced":"priced"},{event:"inference_ledger_already_written",inference_id:e.inference_id,model_id:e.model_id,identity_kind:e.identity_kind,variant:e.variant,cost_nano_usd:s.cost_nano_usd,price_version:s.price_version,attempts:n.attempts});return}let i=_o(o),a=Date.now()-n.queuedAt;if(Se){he(n,{outcome:"failed",reason:"dropped_on_exit"},{event:"inference_ledger_write_failed",reason:"dropped_on_exit",attempts:n.attempts,waited_ms:a,error:`the proxy is shutting down and this row was still being retried when the drain ran out \u2014 the request was served and answered, and its billing row was never written. Last failure: ${o instanceof Error?o.message:String(o)}`});return}if(ho(i)&&a<De){if(await Eo(Ao(n.attempts)),Se||Date.now()-n.queuedAt>=De){let d=Date.now()-n.queuedAt;he(n,{outcome:"failed",reason:Se?"dropped_on_exit":"dwell_expired"},{event:"inference_ledger_write_failed",reason:Se?"dropped_on_exit":"dwell_expired",attempts:n.attempts,waited_ms:d,error:`this row's backoff outlived the ${De}ms dwell bound (waited ${d}ms over ${n.attempts} attempt(s)) and was given up on rather than starting an attempt that could only finish past it. Last failure: ${o instanceof Error?o.message:String(o)}`});return}continue}he(n,{outcome:"failed",reason:i},{event:"inference_ledger_write_failed",reason:i,attempts:n.attempts,waited_ms:a,error:o instanceof Error?o.message:String(o)});return}}}function Ao(n){return Math.min(bo,xo*2**Math.max(0,n-1))}function Eo(n){return new Promise(e=>{setTimeout(e,n).unref?.()})}function he(n,e,t){if(n.settled)return;n.settled=!0;try{n.ledger.observe(e)}catch{}let{event:r,...o}=t;zn(n.report,Vn(r,{inference_id:n.entry.inference_id,path:n.record.path,org:n.record.org,...o}))}function Ut(n){n.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function W(n,e,t){n.writeHead(e,{"content-type":"application/json"}),n.end(JSON.stringify(t))}function oe(n,e,t,r="invalid_request_error",o){W(n,e,{error:o?{message:t,type:r,code:o}:{message:t,type:r}})}function Zn(n,e){let t=e.error,r=t?.type??"upstream_error",o=t?.message??JSON.stringify(e);W(n,r==="invalid_request_error"?400:502,{error:{message:o,type:r,code:t?.code??null}})}function X(n,e,t,r){W(n,e,{type:"error",error:{type:t,message:r}})}function it(n,e,t){if(e==="anthropic"){W(n,404,{type:"error",error:{type:"not_found_error",message:t.message}});return}W(n,404,{error:{message:t.message,type:"invalid_request_error",code:"model_not_found"}})}var Co=64*1024*1024,Ge=class extends Error{};async function Yn(n,e=Co){let t=[],r=0;for await(let s of n){if(r+=s.length,r>e)throw new Ge(`request body exceeds ${e} bytes`);t.push(s)}let o=Buffer.concat(t).toString("utf8");return o?JSON.parse(o):{}}function Ie(n,e,t,r){return{id:n,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:e,choices:[{index:0,delta:t,finish_reason:r}]}}function er(n){let e=n?.usage;if(!e||typeof e!="object")return{kind:"absent"};let t=e,r=d=>typeof d=="number"&&Number.isFinite(d)&&d>=0?d:null,o=r(t.input_tokens),s=r(t.output_tokens),i=d=>d??0,a=r(t.total_tokens)??i(o)+i(s);if(i(o)>0||i(s)>0){let d={input_tokens:i(o),output_tokens:i(s),total_tokens:a};return o===null||s===null?{kind:"partial",usage:d}:{kind:"counts",usage:d}}return o===null||s===null?{kind:"absent"}:{kind:"zero",usage:{input_tokens:0,output_tokens:0,total_tokens:a}}}function at(n){let e=er(n);return e.kind==="counts"||e.kind==="partial"?e.usage:null}function Xn(n){let e=at(n);return e?{prompt_tokens:e.input_tokens,completion_tokens:e.output_tokens,total_tokens:e.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function Uo(n,e){let t=er(e);t.kind==="absent"||t.kind==="partial"||(n.promptTokens=t.usage.input_tokens,n.completionTokens=t.usage.output_tokens)}function Lo(n){let e=n?.urun_timing;if(!e||typeof e!="object")return null;let t=e,r=d=>{let u=t[d];return typeof u=="number"&&Number.isSafeInteger(u)&&u>=0?u:null},o=r("queue_ms"),s=r("prefill_ms"),i=r("decode_ms"),a=r("total_ms");return o===null||s===null||i===null||a===null?null:{queue_ms:o,prefill_ms:s,decode_ms:i,total_ms:a}}function $o(n,e){let t=Lo(e);t&&(n.timing=t)}function Ae(n,e){Uo(n,e),$o(n,e)}async function Lt(n,e,t){if(typeof e.catalogIdentity!="function"){n.catalog={kind:"unresolved",reason:"this backhaul exposes no catalogIdentity seam, so the catalog (model_id, variant) that served the request cannot be known and no honest ledger row can be written"};return}try{let r=await e.catalogIdentity(t);if(r!==null){n.catalog={kind:"catalog",model_id:r.model_id,variant:r.variant};return}let o=typeof e.servingAppSlug=="function"?await e.servingAppSlug(t):null;n.catalog={kind:"not-a-catalog-model",model:o??t??null}}catch(r){n.catalog={kind:"unresolved",reason:`model ${JSON.stringify(t??null)} could not be resolved to a catalog identity: ${r instanceof Error?r.message:String(r)}`}}}function tr(n){let e=n?.output;if(!Array.isArray(e))return"";let t="";for(let r of e)if(!(r.type!=="reasoning"||!Array.isArray(r.content)))for(let o of r.content)o.type==="reasoning_text"&&(t+=String(o.text??""));return t}function No(n){if(Array.isArray(n))return n.map(e=>{let t=e.function;return!t||typeof t!="object"?e:{type:"function",name:t.name,description:t.description,parameters:t.parameters,strict:t.strict}})}var ut=class extends Error{};function Mo(n,e){return Array.isArray(e)?e.map(t=>{let r=t;if(r.type==="text")return{type:n==="assistant"?"output_text":"input_text",text:r.text};if(r.type==="image_url"){let o=r.image_url??{};return{type:"input_image",image_url:o.url,detail:o.detail}}throw new ut(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):e}function qo(n){let e=[];for(let t of n){if(t.role==="tool"){e.push({type:"function_call_output",call_id:t.tool_call_id,output:typeof t.content=="string"?t.content:JSON.stringify(t.content??"")});continue}if(t.role==="assistant"&&Array.isArray(t.tool_calls)){typeof t.content=="string"&&t.content&&e.push({role:"assistant",content:t.content});for(let r of t.tool_calls){let o=r.function??{};e.push({type:"function_call",call_id:r.id,name:o.name,arguments:o.arguments})}continue}e.push({...t,content:Mo(t.role,t.content)})}return e}function Pt(n){return ve(n).map((e,t)=>({index:t,id:e.call_id??e.id??`call_${t}`,type:"function",function:{name:e.name??"",arguments:e.arguments??"{}"}}))}async function Bo(n,e,t,r,o,s){let i=$e(s.id),a=n.stream===!0;if(s.model=ee(n),s.stream=a,await Lt(s,e,typeof n.model=="string"?n.model:void 0),a){let h;try{h=await Me({body:n,clients:e,store:t,stats:o,responseId:i,turn:s})}catch(g){if(g instanceof G){it(r,"openai",g);return}if(g instanceof K){oe(r,g.status,g.message);return}throw g}Ut(r);for await(let g of h.events)g.type==="error"&&(s.streamError=!0),g.type==="response.completed"&&Ae(s,g.response),r.write(`event: ${String(g.type??"message")}
25
- data: ${JSON.stringify(g)}
26
-
27
- `);r.write(`data: [DONE]
28
-
29
- `),r.end();return}let d;try{d=xe(t,n)}catch(h){if(h instanceof K){oe(r,h.status,h.message);return}throw h}n.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(d))-Buffer.byteLength(JSON.stringify(n.input)));let u=n.store!==!1,l=ee(n);o.model(l);let c=Array.isArray(n.tools)&&n.tools.length>0,w;try{w=await wt(e,n,d,!1,Z(s))}catch(h){if(h instanceof G){it(r,"openai",h);return}if(h instanceof K){oe(r,h.status,h.message);return}throw h}let f=[],E=null;for await(let h of o.track(w,l,s)){if(f.push(h),h.type==="error"){Zn(r,h);return}h.type==="response.completed"&&(E=h.response)}if(E==null){oe(r,502,"upstream produced no response.completed event","upstream_error");return}Ae(s,E);let R=yt(f,E),m=ge(c,ve(E).length,R);if(m){oe(r,502,fe(l,m),"upstream_error");return}u&&t.remember(i,d,vt(E,R),u),W(r,200,{...E,id:i})}async function Fo(n,e,t,r,o){let s=n.messages;if(!Array.isArray(s)){oe(t,400,"chat/completions requires a messages array");return}let i=String(n.model??"urun"),a=sn(o.id),d;try{d=qo(s)}catch(p){if(p instanceof ut){oe(t,400,p.message);return}throw p}let u=ee(n);r.model(u),o.model=u,o.stream=n.stream===!0,await Lt(o,e,typeof n.model=="string"?n.model:void 0);let l=Array.isArray(n.tools)&&n.tools.length>0,c;try{c=await e.createResponse({model:n.model,input:d,stream:!0,tools:No(n.tools),tool_choice:n.tool_choice,temperature:n.temperature,top_p:n.top_p,stop:n.stop,max_output_tokens:n.max_completion_tokens??n.max_tokens,...n.reasoning_effort!==void 0?{reasoning_effort:n.reasoning_effort}:{},...n.chat_template_kwargs!==void 0?{chat_template_kwargs:n.chat_template_kwargs}:{},...n.response_format!==void 0?{response_format:n.response_format}:{}},Z(o))}catch(p){if(p instanceof G){it(t,"openai",p);return}throw p}if(n.stream===!0){Ut(t);let p=[],O=new ie,P="";t.write(`data: ${JSON.stringify(Ie(a,i,{role:"assistant"},null))}
30
-
31
- `);for await(let B of r.track(c,u,o)){p.push(B);let k=we(B);if(B.type==="response.output_text.delta"&&typeof B.delta=="string")P+=B.delta,t.write(`data: ${JSON.stringify(Ie(a,i,{content:B.delta},null))}
32
-
33
- `);else if(B.type==="response.reasoning_text.delta"&&typeof B.delta=="string")t.write(`data: ${JSON.stringify(Ie(a,i,{reasoning_content:B.delta},null))}
34
-
35
- `);else if(k){let{call:U,isNew:M}=O.add(k),z=M?{index:k.tool_index,id:U.id??`call_${k.tool_index}`,type:"function",function:{name:U.name??"",arguments:k.delta}}:{index:k.tool_index,function:{arguments:k.delta}};t.write(`data: ${JSON.stringify(Ie(a,i,{tool_calls:[z]},null))}
36
-
37
- `)}else if(B.type==="error"){o.streamError=!0,t.write(`data: ${JSON.stringify({error:B})}
38
-
39
- `),t.end();return}}let L=Pt(te(p)),I=ge(l,O.size+L.length,P);if(I){o.streamError=!0,t.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:fe(u,I)}})}
40
-
41
- `),t.end();return}O.size===0&&L.length>0&&t.write(`data: ${JSON.stringify(Ie(a,i,{tool_calls:L},null))}
42
-
43
- `);let Y=O.size>0||L.length>0;t.write(`data: ${JSON.stringify(Ie(a,i,{},Y?"tool_calls":"stop"))}
44
-
45
- `);let V=at(te(p));Ae(o,te(p)),V&&t.write(`data: ${JSON.stringify({id:a,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:i,choices:[],usage:Xn(te(p))})}
46
-
47
- `),t.write(`data: [DONE]
48
-
49
- `),t.end();return}let w="",f="",E=[],R=new ie;for await(let p of r.track(c,u,o)){E.push(p),p.type==="response.output_text.delta"&&typeof p.delta=="string"&&(w+=p.delta),p.type==="response.reasoning_text.delta"&&typeof p.delta=="string"&&(f+=p.delta);let O=we(p);if(O&&R.add(O),p.type==="error"){Zn(t,p);return}}let m=te(E);Ae(o,m);let h=Pt(m),g=h.length>0?h:Pt({output:R.items()}),y=ge(l,g.length,w);if(y){oe(t,502,fe(u,y),"upstream_error");return}let S=g.map(({index:p,...O})=>O),x={role:"assistant",content:w||null},_=f||tr(m);_&&(x.reasoning_content=_),S.length>0&&(x.tool_calls=S),W(t,200,{id:a,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:i,choices:[{index:0,message:x,finish_reason:S.length>0?"tool_calls":"stop"}],usage:Xn(m)})}function Do(n){return typeof n=="string"?n:Array.isArray(n)?n.filter(e=>e.type==="text").map(e=>String(e.text??"")).join(""):""}var He=class extends Error{};function dt(n){return new He(`unsupported anthropic content block type "${n}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function jo(n){if(Array.isArray(n))return n.map(e=>{let t=e;return{type:"function",name:t.name,description:t.description,parameters:t.input_schema}})}function Go(n){if(n==null)return;let e=n.type;if(e==="auto")return"auto";if(e==="none")return"none";if(e==="any")return"required";if(e==="tool")return{type:"function",name:n.name};throw new He(`unsupported anthropic tool_choice type ${JSON.stringify(String(e))} \u2014 the proxy maps auto, none, any and tool`)}function Ho(n){if(typeof n=="string")return n;if(n==null)return"";if(!Array.isArray(n))throw dt(typeof n);let e="";for(let t of n){let r=String(t.type??"");if(r!=="text")throw dt(`tool_result > ${r}`);e+=String(t.text??"")}return e}function Wo(n){let{role:e,content:t}=n;if(typeof t=="string")return[{role:e,content:t}];if(t==null)return[];if(!Array.isArray(t))throw dt(typeof t);let r=[],o="",s=()=>{o&&(r.push({role:e,content:o}),o="")};for(let i of t){let a=String(i.type??"");if(a==="text")o+=String(i.text??"");else if(a==="tool_use")s(),r.push({type:"function_call",call_id:i.id,name:i.name,arguments:JSON.stringify(i.input??{})});else if(a==="tool_result")s(),r.push({type:"function_call_output",call_id:i.tool_use_id,output:Ho(i.content)});else{if(a==="thinking"||a==="redacted_thinking")continue;throw dt(a)}}return s(),r}function Ct(n){return ve(n).map((e,t)=>{let r;try{r=JSON.parse(e.arguments||"{}")}catch{throw new Error(`upstream function_call "${e.name??""}" arguments are not valid JSON`)}return{type:"tool_use",id:e.call_id??e.id??`toolu_${t}`,name:e.name??"",input:r}})}async function Jo(n,e,t,r,o){let s=n.messages;if(!Array.isArray(s)){X(t,400,"invalid_request_error","messages requires a messages array");return}if(typeof n.max_tokens!="number"||!Number.isInteger(n.max_tokens)||n.max_tokens<1){X(t,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(n.top_k!==void 0){X(t,400,"invalid_request_error","top_k is not supported: the uRun serve lane exposes temperature/top_p/stop_sequences only");return}if(n.stop_sequences!==void 0&&(!Array.isArray(n.stop_sequences)||n.stop_sequences.some(p=>typeof p!="string"))){X(t,400,"invalid_request_error","stop_sequences must be an array of strings");return}let i=String(n.model??"urun"),a=an(o.id),d=[],u;try{n.system&&d.push({role:"system",content:Do(n.system)});for(let p of s)d.push(...Wo(p));u=Go(n.tool_choice)}catch(p){if(p instanceof He){X(t,400,"invalid_request_error",p.message);return}throw p}let l=ee(n);r.model(l),o.model=l,o.stream=n.stream===!0,await Lt(o,e,typeof n.model=="string"?n.model:void 0);let c=Array.isArray(n.tools)&&n.tools.length>0,w;try{w=await e.createResponse({model:n.model,input:d,stream:!0,tools:jo(n.tools),tool_choice:u,temperature:n.temperature,max_output_tokens:n.max_tokens,top_p:n.top_p,stop:n.stop_sequences},Z(o))}catch(p){if(p instanceof G){it(t,"anthropic",p);return}throw p}let f=(p,O)=>{t.destroyed||t.write(`event: ${p}
50
- data: ${JSON.stringify({type:p,...O})}
51
-
52
- `)};if(n.stream===!0){let p=r.track(w,l,o);Ut(t),f("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:i,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let O=0,P="",L=0,I="none",Y=()=>I,V=-1,B=-1,k=new ie,U=[],M=()=>{I!=="none"&&(I="none",f("content_block_stop",{index:V}))},z=($,J)=>{M(),I=$,V=L++,f("content_block_start",{index:V,content_block:J})};for await(let $ of p){if(t.destroyed)break;U.push($);let J=we($);if($.type==="response.reasoning_text.delta"&&typeof $.delta=="string")Y()!=="thinking"&&z("thinking",{type:"thinking",thinking:""}),f("content_block_delta",{index:V,delta:{type:"thinking_delta",thinking:$.delta}});else if($.type==="response.output_text.delta"&&typeof $.delta=="string")Y()!=="text"&&z("text",{type:"text",text:""}),O+=1,P+=$.delta,f("content_block_delta",{index:V,delta:{type:"text_delta",text:$.delta}});else if(J){let{call:$t}=k.add(J);(Y()!=="tool"||J.tool_index!==B)&&(B=J.tool_index,z("tool",{type:"tool_use",id:$t.id??`call_${J.tool_index}`,name:$t.name??"",input:{}})),J.delta&&f("content_block_delta",{index:V,delta:{type:"input_json_delta",partial_json:J.delta}})}else if($.type==="error"){o.streamError=!0,f("error",{error:{type:"api_error",message:JSON.stringify($)}}),t.end();return}}if(t.destroyed)return;let ue=te(U),de=ve(ue),se=ge(c,k.size+de.length,P);if(se){o.streamError=!0,f("error",{error:{type:"api_error",message:fe(l,se)}}),t.end();return}M();let Q=[];if(k.size===0)try{Q=Ct(ue)}catch($){o.streamError=!0,f("error",{error:{type:"api_error",message:String($ instanceof Error?$.message:$)}}),t.end();return}L===0&&Q.length===0&&(z("text",{type:"text",text:""}),M()),Q.forEach($=>{let J=L++;f("content_block_start",{index:J,content_block:{...$,input:{}}}),f("content_block_delta",{index:J,delta:{type:"input_json_delta",partial_json:JSON.stringify($.input??{})}}),f("content_block_stop",{index:J})});let Ee=k.size>0||Q.length>0,_e=at(ue);Ae(o,ue),f("message_delta",{delta:{stop_reason:Ee?"tool_use":"end_turn"},usage:_e?{input_tokens:_e.input_tokens,output_tokens:_e.output_tokens}:{output_tokens:O}}),f("message_stop",{}),t.end();return}let E="",R="",m=[],h=new ie;for await(let p of r.track(w,l,o)){m.push(p),p.type==="response.output_text.delta"&&typeof p.delta=="string"&&(E+=p.delta),p.type==="response.reasoning_text.delta"&&typeof p.delta=="string"&&(R+=p.delta);let O=we(p);if(O&&h.add(O),p.type==="error"){let P=p.error,L=P?.type==="invalid_request_error";X(t,L?400:502,L?"invalid_request_error":"api_error",P?.message??JSON.stringify(p));return}if(t.destroyed)break}if(t.destroyed)return;let g=te(m);Ae(o,g);let y;try{y=Ct(g),y.length===0&&h.size>0&&(y=Ct({output:h.items()}))}catch(p){X(t,502,"api_error",String(p instanceof Error?p.message:p));return}let S=ge(c,y.length,E);if(S){X(t,502,"api_error",fe(l,S));return}let x=[],_=R||tr(g);_&&x.push({type:"thinking",thinking:_}),(E||y.length===0)&&x.push({type:"text",text:E}),x.push(...y),W(t,200,{id:a,type:"message",role:"assistant",content:x,model:i,stop_reason:y.length>0?"tool_use":"end_turn",usage:at(g)??{input_tokens:0,output_tokens:0}})}function Vo(n){be(process.stdout,n)}function zo(n){let{clients:e,apiKey:t,identity:r,statusOf:o}=n,s=typeof e=="function"?e:()=>e,i=n.requestLog??Vo,a=n.observe,d=new pe;return(u,l)=>{let c=new URL(u.url??"/","http://localhost"),w=c.pathname==="/v1/messages"||c.pathname.startsWith("/v1/messages/"),f=(g,y,S,x,_)=>w?X(l,g,x,y):oe(l,g,y,S,_),E=_t(c.pathname),R=c.pathname.startsWith("/partners/"),m=E||R?{id:Le(),startedAt:performance.now(),streamError:!1,model:null,stream:null,org:null,ttftMs:null,catalog:{kind:"unresolved",reason:"no lane resolved a model for this request"},promptTokens:null,completionTokens:null,outputUnits:null,sessionId:null,timing:null}:null;m&&(l.setHeader(rn,m.id),l.once("close",()=>{let g=un({turn:m,method:u.method??"",path:c.pathname,res:l});i(dn(g)),a?.(g),Kn({record:g,ledger:n.ledger??null,req:u})}));let h=()=>on(c.pathname,m);(async()=>{if(u.method==="GET"&&c.pathname==="/healthz"){W(l,200,{ok:!0});return}if(u.method==="GET"&&c.pathname==="/stats"){W(l,200,r?{identity:r,...d.snapshot()}:d.snapshot());return}if(t&&(u.headers.authorization??"")!==`Bearer ${t}`){f(401,"invalid local proxy api key","authentication_error","authentication_error");return}let g=c.pathname===qn?"canonical":c.pathname===Ot?"huggingface":null;if(g){if(u.method!=="POST"){f(405,`${c.pathname} is a POST batch lookup`,"invalid_request_error","invalid_request_error");return}let _=n.usage;if(!_){f(501,`${c.pathname} needs a usage seam (ProxyHandlerOptions.usage) to read the per-request inference ledger; this proxy has none, and answering an empty result would be indistinguishable from "nothing is priced yet"`,"not_implemented","not_implemented_error");return}let p=_.authorize(u),O;try{O=await Yn(u,Bn)}catch(I){if(I instanceof Ge){f(413,I.message,"request_too_large","request_too_large");return}f(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let P=I=>_.lookup(p,I),L;try{if(g==="canonical"){let I=Fn(O),Y=await P(I);ot(Y,I),L={requests:Y}}else L=await jn(O,P)}catch(I){if(I instanceof ae){f(I.status,I.message,"invalid_request_error","invalid_request_error");return}throw I}W(l,200,L);return}if(c.pathname.startsWith("/partners/")){f(404,`no partner adapter for ${u.method} ${c.pathname} \u2014 the partner surface currently carries ${Ot}`,"invalid_request_error","not_found_error");return}let y=await s(u);if(m&&(m.org=y.tenant??null),u.method==="GET"&&c.pathname==="/v1/models"){if(c.searchParams.get("format")==="openrouter"){if(!y.openRouterModels){f(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}W(l,200,await y.openRouterModels());return}W(l,200,await y.listModels());return}if(u.method==="GET"&&c.pathname===kn){if(!y.listImageModels){f(501,"GET /v1/images/models needs a router-backed client (listImageModels)","not_implemented","not_implemented_error");return}W(l,200,await y.listImageModels());return}if(bn(c.pathname)){if(!y.supportsImages){f(501,`the image lanes need a router-backed client (supportsImages) to gate model capability for ${c.pathname}`,"not_implemented","not_implemented_error");return}let _={clients:y,imageModels:y.supportsImages,stats:d,turn:h()};c.pathname===nt?await Un(u,l,_):await Cn(u,l,_);return}if(u.method!=="POST"){f(404,`no route for ${u.method} ${c.pathname}`,"invalid_request_error","not_found_error");return}let S;try{S=await Yn(u)}catch(_){if(_ instanceof Ge){f(413,_.message,"request_too_large","request_too_large");return}f(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let x=Buffer.byteLength(JSON.stringify(S));if(c.pathname==="/v1/responses"){d.request("responses",x,x),await Bo(S,y,Ne(y),l,d,h());return}if(c.pathname==="/v1/chat/completions"){d.request("chat_completions",x,x),await Fo(S,y,l,d,h());return}if(c.pathname==="/v1/messages"){d.request("messages",x,x),await Jo(S,y,l,d,h());return}if(c.pathname==="/v1/messages/count_tokens"){d.request("count_tokens",x,x),X(l,404,"not_found_error","count_tokens is not implemented by the uRun proxy: the serve transport exposes no token-count lane, and the proxy will not fabricate counts");return}f(404,`no route for POST ${c.pathname}`,"invalid_request_error","not_found_error")})().catch(g=>{let y=String(g instanceof Error?g.message:g);if(!l.headersSent){let S=o?.(g)??null;if(S){f(S.status,y,S.openaiType,S.anthropicType);return}if(g instanceof To){let x=Ze(g.code);if(x){let _=g.retryAfterSeconds??(x.status===429?30:void 0);_!==void 0&&l.setHeader("Retry-After",String(_)),f(x.status,y,x.openaiType,x.anthropicType,x.code);return}}if(g instanceof Po){let x=et(g);if(x){let _=x.status===429?30:void 0;_!==void 0&&l.setHeader("Retry-After",String(_)),f(x.status,y,x.openaiType,x.anthropicType,x.code);return}}if(g instanceof q){f(502,y,"upstream_error","api_error");return}f(500,y,"proxy_error","api_error","proxy_error");return}m&&(m.streamError=!0),l.destroyed||(w?l.write(`event: error
53
- data: ${JSON.stringify({type:"error",error:{type:"api_error",message:y}})}
54
-
55
- `):l.write(`data: ${JSON.stringify({error:{message:y,type:"upstream_error"}})}
56
-
57
- `)),l.end()})}}function Hi(n){let{clients:e,apiKey:t}=n,r=Oo(zo(n)),o=fn(r,{clients:e,apiKey:t,statusOf:n.statusOf}),s=ht(r,{resolver:Qn(e,t)}),i=r.close.bind(r);return r.close=(a=>(o.close(),s(),i(a))),Zt(r,{clients:e,apiKey:t}),r}var We=(n,e)=>Object.assign(new Error(e),{status:n});function Ko(n){let t=new URL(n.url??"/","http://localhost").searchParams.get("model")?.trim();return t===""?void 0:t}async function Yo(n,e){if(!n.supportsAudio)throw We(501,"the realtime lane needs a router-backed client (supportsAudio) to gate audio modality \u2014 hand-built clients without the catalog oracle are refused, never allowed silently");let t;try{t=await n.supportsAudio(e)}catch(s){throw s instanceof G?We(404,s.message):s}if(!t)throw We(404,`model ${JSON.stringify(e??"(default)")} is not audio-capable (catalog task is neither stt nor tts) \u2014 this realtime lane serves only audio-task models; chat models ride /v1/chat/completions`);if(!n.openAudio)throw We(501,"an audio-capable model needs a router-backed client (openAudio) to open its native session audio bridge \u2014 refusing loudly rather than binding a voiceless session");let r={audio:await n.openAudio(e)},o={clients:n,voice:t,audioSession:r};return e!==void 0&&(o.model=e),o}function Qn(n,e){return{resolve:async t=>{if(e!==void 0){let r=t.headers.authorization,o=typeof r=="string"?r.trim():"",s=o.startsWith("Bearer ")?o.slice(7).trim():"";if(s===""||s!==e)throw We(401,"invalid local proxy api key")}return Yo(n,Ko(t))}}}export{Ve as a,Zt as b,ts as c,Cr as d,fn as e,Ue as f,gt as g,ht as h,Ko as i,Yo as j,Qn as k,Hs as l,Wr as m,Ws as n,Js as o,Vs as p,zs as q,Ks as r,Ys as s,Xs as t,Qs as u,Zs as v,Jr as w,hn as x,ei as y,Kr as z,ti as A,ni as B,ri as C,be as D,oi as E,si as F,ii as G,qn as H,q as I,gi as J,Ot as K,bi as L,ki as M,Ri as N,Si as O,zo as P,Hi as Q};