@urun-sh/openai 0.5.6 → 0.6.0

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-45FJ5GTP.js +7 -0
  5. package/dist/{chunk-BSHT6RZZ.js → chunk-6M5JK4YC.js} +1 -1
  6. package/dist/{chunk-5HKWNK3O.js → chunk-K23AZPI4.js} +1 -1
  7. package/dist/chunk-QTVTCMJU.js +1 -0
  8. package/dist/chunk-TDRZZGUK.js +1 -0
  9. package/dist/chunk-TPH77ZOW.js +58 -0
  10. package/dist/chunk-WCMX5VOF.js +4 -0
  11. package/dist/chunk-YXK42SKC.js +1 -0
  12. package/dist/gemini-live.cjs +2 -2
  13. package/dist/gemini-live.d.cts +64 -7
  14. package/dist/gemini-live.d.ts +27 -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 +47 -45
  32. package/dist/proxy/cli.js +15 -14
  33. package/dist/proxy/index.cjs +28 -27
  34. package/dist/proxy/index.d.cts +54 -16
  35. package/dist/proxy/index.d.ts +18 -11
  36. package/dist/proxy/index.js +1 -1
  37. package/dist/{responses-turn-tNUHq5b0.d.cts → responses-turn-BfdBbey8.d.cts} +610 -23
  38. package/dist/{responses-turn-Dr37N3kH.d.ts → responses-turn-CdhIre_a.d.ts} +173 -7
  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 { b as UrunSessionLike } from './types-lsVTbNcH.js';
1
+ import { b as UrunSessionLike } from './types-CHPtJx6d.js';
2
2
 
3
3
  interface ResponsesStream extends AsyncIterable<unknown> {
4
4
  readonly requestId: string;
@@ -11,6 +11,8 @@ declare class UrunResponses {
11
11
  model?: string;
12
12
  input: unknown;
13
13
  stream?: boolean;
14
+
15
+ instructions?: string;
14
16
  tools?: unknown;
15
17
  tool_choice?: unknown;
16
18
  temperature?: number;
@@ -21,6 +23,8 @@ declare class UrunResponses {
21
23
  chat_template_kwargs?: Record<string, unknown>;
22
24
 
23
25
  response_format?: unknown;
26
+
27
+ voice?: string;
24
28
  }) => Promise<ResponsesStream>;
25
29
  };
26
30
  }
@@ -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
  interface ResponsesStream extends AsyncIterable<unknown> {
4
4
  readonly requestId: string;
@@ -11,6 +11,8 @@ declare class UrunResponses {
11
11
  model?: string;
12
12
  input: unknown;
13
13
  stream?: boolean;
14
+ /** Per-request system instructions — forwarded verbatim to the serve envelope. */
15
+ instructions?: string;
14
16
  tools?: unknown;
15
17
  tool_choice?: unknown;
16
18
  temperature?: number;
@@ -21,6 +23,12 @@ declare class UrunResponses {
21
23
  chat_template_kwargs?: Record<string, unknown>;
22
24
  /** ENG-310 structured output: forwarded verbatim; the serve runtime validates. */
23
25
  response_format?: unknown;
26
+ /**
27
+ * ENG-331 speech lane: the per-request TTS voice, forwarded verbatim
28
+ * on the serve envelope (encode.ts `ServeEnvelope.voice`); absent →
29
+ * key absent — the engine's catalog-configured default voice applies.
30
+ */
31
+ voice?: string;
24
32
  }) => Promise<ResponsesStream>;
25
33
  };
26
34
  }
@@ -1,2 +1,2 @@
1
- import{a as I,o as A}from"./chunk-BSHT6RZZ.js";import{c as v}from"./chunk-5CCJUNH7.js";v();var J="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",k=1007,F=1008,R=1011,V=1008,n=class extends Error{constructor(o,i=k){super(o);this.closeCode=i}closeCode},d=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)?e:null,w=["setup","clientContent","realtimeInput","toolResponse"];function B(e){let t;try{t=JSON.parse(e)}catch{throw new n("client message is not valid JSON")}let o=d(t);if(!o)throw new n("client message must be a JSON object");let i=w.filter(c=>o[c]!==void 0),s=Object.keys(o).filter(c=>!w.includes(c));if(s.length>0)throw new n(`unknown client message field(s) ${JSON.stringify(s)} \u2014 expected exactly one of ${w.join(", ")}`);if(i.length!==1)throw new n(`client message must contain exactly one of ${w.join(", ")} (got ${i.length})`);let r=i[0],a=d(o[r]);if(!a)throw new n(`"${r}" must be a JSON object`);return{kind:r,[r]:a}}function C(e,t){if(typeof e=="string")return e;let o=d(e),i=o?o.parts:void 0;if(!Array.isArray(i))throw new n(`${t}: expected a Content with a parts array`);let s=[];for(let r of i){let a=d(r);if(a&&typeof a.text=="string"){s.push(a.text);continue}throw a&&(a.inlineData!==void 0||a.fileData!==void 0)?new n(`${t}: media parts (inlineData/fileData) are a declared follow-up slice (audio/video lanes over the proxy transport AudioBridge) \u2014 only text parts are supported in this slice`):a&&(a.functionCall!==void 0||a.functionResponse!==void 0)?new n(`${t}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new n(`${t}: unsupported part ${JSON.stringify(r)}`)}return s.join(`
1
+ import{a as I,o as A}from"./chunk-6M5JK4YC.js";import{c as v}from"./chunk-YSFSRI3D.js";v();var J="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",k=1007,F=1008,R=1011,V=1008,n=class extends Error{constructor(o,i=k){super(o);this.closeCode=i}closeCode},d=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)?e:null,w=["setup","clientContent","realtimeInput","toolResponse"];function B(e){let t;try{t=JSON.parse(e)}catch{throw new n("client message is not valid JSON")}let o=d(t);if(!o)throw new n("client message must be a JSON object");let i=w.filter(c=>o[c]!==void 0),s=Object.keys(o).filter(c=>!w.includes(c));if(s.length>0)throw new n(`unknown client message field(s) ${JSON.stringify(s)} \u2014 expected exactly one of ${w.join(", ")}`);if(i.length!==1)throw new n(`client message must contain exactly one of ${w.join(", ")} (got ${i.length})`);let r=i[0],a=d(o[r]);if(!a)throw new n(`"${r}" must be a JSON object`);return{kind:r,[r]:a}}function C(e,t){if(typeof e=="string")return e;let o=d(e),i=o?o.parts:void 0;if(!Array.isArray(i))throw new n(`${t}: expected a Content with a parts array`);let s=[];for(let r of i){let a=d(r);if(a&&typeof a.text=="string"){s.push(a.text);continue}throw a&&(a.inlineData!==void 0||a.fileData!==void 0)?new n(`${t}: media parts (inlineData/fileData) are a declared follow-up slice (audio/video lanes over the proxy transport AudioBridge) \u2014 only text parts are supported in this slice`):a&&(a.functionCall!==void 0||a.functionResponse!==void 0)?new n(`${t}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new n(`${t}: unsupported part ${JSON.stringify(r)}`)}return s.join(`
2
2
  `)}var _={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},h="urun.videoOut";function G(e){return{framing:A,codec:e}}function Y(e){let{seq:t,ptsUs:o,codec:i,key:s}=e.header;return{[h]:{v:1,seq:t,ptsUs:o,codec:i,key:s,data:Buffer.from(e.au).toString("base64")}}}var x={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},S=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",h],T=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function E(e){let t=d(e);if(!t)throw new n("setup.realtimeInputConfig must be an object");for(let s of Object.keys(t))if(!(s==="automaticActivityDetection"||s==="activityHandling"))throw new n(s==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${s}"`);let o=d(t.automaticActivityDetection);if(t.automaticActivityDetection!==void 0&&!o)throw new n("realtimeInputConfig.automaticActivityDetection must be an object");for(let s of Object.keys(o??{}))if(s!=="disabled")throw new n(`automaticActivityDetection.${s} tunes AUTOMATIC (server-side) activity detection \u2014 the declared platform follow-up (serve turn_detection events over the session stream lane); only "disabled: true" (manual activity mode) is supported in this slice`);if(o?.disabled!==!0)throw new n("automatic (server-side) activity detection is the declared platform follow-up (serve turn_detection turn_start/turn_end events over the session stream lane, not proxy DSP) \u2014 set realtimeInputConfig.automaticActivityDetection.disabled: true for manual activity mode");let i="START_OF_ACTIVITY_INTERRUPTS";if(t.activityHandling!==void 0){if(!T.includes(t.activityHandling))throw new n(`unsupported activityHandling ${JSON.stringify(t.activityHandling)} \u2014 expected one of ${T.join(", ")}`);i=t.activityHandling}return{manualActivity:!0,activityHandling:i}}var N=["temperature","maxOutputTokens","responseModalities"];function K(e){for(let[u,f]of Object.entries(x))if(e[u]!==void 0)throw new n(f);for(let u of Object.keys(e))if(!S.includes(u))throw new n(`unsupported setup field "${u}"`);if(typeof e.model!="string"||e.model.length===0)throw new n('setup.model is required (format "models/{model}")');let t=e.model.replace(/^models\//,""),o,i,s=!1,r=d(e.generationConfig);if(e.generationConfig!==void 0&&!r)throw new n("setup.generationConfig must be an object");if(r){for(let u of Object.keys(r))if(!N.includes(u))throw new n(`unsupported generationConfig field "${u}"`);if(r.temperature!==void 0){if(typeof r.temperature!="number")throw new n("generationConfig.temperature must be a number");o=r.temperature}if(r.maxOutputTokens!==void 0){if(typeof r.maxOutputTokens!="number")throw new n("generationConfig.maxOutputTokens must be a number");i=r.maxOutputTokens}if(r.responseModalities!==void 0){let u=Array.isArray(r.responseModalities)?r.responseModalities:[r.responseModalities];for(let f of u){let y=String(f).toUpperCase();if(y==="AUDIO"){s=!0;continue}if(y!=="TEXT")throw new n(`responseModalities ${JSON.stringify(f)} is not supported \u2014 this surface speaks TEXT and AUDIO (audio-out rides the native AudioBridge 'audio' lane)`)}}}let a=[];e.systemInstruction!==void 0&&a.push({role:"system",content:C(e.systemInstruction,"setup.systemInstruction")});let c;if(e.tools!==void 0){if(!Array.isArray(e.tools))throw new n("setup.tools must be an array");c=[];for(let u of e.tools){let f=d(u);if(!f)throw new n("setup.tools entries must be objects");for(let y of Object.keys(f))if(y!=="functionDeclarations")throw new n(`unsupported tool "${y}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(f.functionDeclarations))throw new n("tool.functionDeclarations must be an array");for(let y of f.functionDeclarations){let g=d(y);if(!g||typeof g.name!="string")throw new n("functionDeclarations entries require a string name");c.push({type:"function",name:g.name,description:g.description,parameters:g.parameters})}}c.length===0&&(c=void 0)}let m={enabled:!1,handle:null};if(e.sessionResumption!==void 0){let u=d(e.sessionResumption);if(!u)throw new n("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let f of Object.keys(u))if(f!=="handle")throw new n(`unsupported sessionResumption field "${f}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(u.handle!==void 0&&(typeof u.handle!="string"||u.handle.length===0))throw new n("sessionResumption.handle must be a non-empty string when present");m={enabled:!0,handle:typeof u.handle=="string"?u.handle:null}}let p=e.realtimeInputConfig===void 0?_:E(e.realtimeInputConfig),l=!1;if(e[h]!==void 0){if(!d(e[h]))throw new n(`setup["${h}"] must be an object (the v1 opt-in is {})`);l=!0}return{model:t,systemItems:a,tools:c,temperature:o,maxOutputTokens:i,audioModality:s,activity:p,sessionResumption:m,videoOut:l}}function q(e){return{sessionResumptionUpdate:{newHandle:e,resumable:!0}}}function W(e){let t=Math.max(0,e??0)/1e3;return{goAway:{timeLeft:`${Math.round(t*1e3)/1e3}s`}}}function X(e){for(let i of Object.keys(e))if(i!=="turns"&&i!=="turnComplete")throw new n(`unsupported clientContent field "${i}"`);let t=e.turns===void 0?[]:e.turns;if(!Array.isArray(t))throw new n("clientContent.turns must be an array");let o=[];for(let i of t){let s=d(i);if(!s)throw new n("clientContent.turns entries must be Content objects");let r=s.role===void 0?"user":String(s.role).toLowerCase();if(r!=="user"&&r!=="model")throw new n(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(s.role)})`);o.push({role:r==="model"?"assistant":"user",content:C(s,"clientContent.turns")})}return{items:o,turnComplete:e.turnComplete===!0}}var b=16e3,O=24e3,$=`audio/pcm;rate=${O}`,z=Buffer.alloc(960).toString("base64");function j(e,t,o){if(t===o)return e;let i=Math.floor(e.length*o/t),s=new Int16Array(i),r=t/o,a=e.length-1;for(let c=0;c<i;c++){let m=c*r,p=Math.floor(m);if(p>=a){s[c]=e[a];continue}let l=m-p;s[c]=Math.round(e[p]+(e[p+1]-e[p])*l)}return s}function U(e){let t=d(e);if(!t)throw new n("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof t.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(t.mimeType))throw new n(`realtimeInput.video.mimeType must be image/jpeg (the Live video input stream is individual JPEG frames), got ${JSON.stringify(t.mimeType)}`);if(typeof t.data!="string"||t.data.length===0)throw new n("realtimeInput.video.data must be non-empty base64");let o=Buffer.from(t.data,"base64");if(o.length<3||o[0]!==255||o[1]!==216)throw new n("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(o)}function D(e){let t=d(e);if(!t)throw new n("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof t.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(t.mimeType))throw new n(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(t.mimeType)}`);let o=/;\s*rate=(\d+)/.exec(t.mimeType);if(o&&Number(o[1])!==b)throw new n(`realtimeInput.audio.mimeType declares rate=${o[1]} \u2014 the Live spec input format is ${b} Hz (audio/pcm;rate=${b})`);if(typeof t.data!="string"||t.data.length===0)throw new n("realtimeInput.audio.data must be non-empty base64");let i=Buffer.from(t.data,"base64");if(i.length===0||i.length%2!==0)throw new n(`realtimeInput.audio.data must decode to whole 16-bit samples (got ${i.length} bytes \u2014 not even)`);let s=new Int16Array(i.length/2);for(let a=0;a<s.length;a++)s[a]=i.readInt16LE(a*2);let r=j(s,b,O);return Buffer.from(r.buffer,r.byteOffset,r.byteLength).toString("base64")}function Q(e){if(e.mediaChunks!==void 0)throw new n('realtimeInput.mediaChunks is deprecated by the Live API \u2014 send the audio stream via realtimeInput.audio ({mimeType:"audio/pcm;rate=16000", data}) instead');if(e.activityStart!==void 0||e.activityEnd!==void 0){if(Object.keys(e).length!==1||e.activityStart!==void 0&&e.activityEnd!==void 0)throw new n("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let s=e.activityStart!==void 0?"activityStart":"activityEnd",r=d(e[s]);if(!r||Object.keys(r).length!==0)throw new n(`realtimeInput.${s} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:s}}let t=["text","audio","audioStreamEnd","video"];for(let i of Object.keys(e))if(!t.includes(i))throw new n(`unsupported realtimeInput field "${i}"`);let o=t.filter(i=>e[i]!==void 0);if(o.length!==1)throw new n(`realtimeInput must carry exactly one of ${t.join(", ")} (got ${o.length})`);if(e.audio!==void 0)return{kind:"audio",base64Pcm24k:D(e.audio)};if(e.video!==void 0)return{kind:"video",jpegFrame:U(e.video)};if(e.audioStreamEnd!==void 0){if(e.audioStreamEnd!==!0)throw new n("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof e.text!="string"||e.text.length===0)throw new n("realtimeInput.text must be a non-empty string");return{kind:"text",text:e.text}}function Z(e){for(let t of Object.keys(e))if(t!=="functionResponses")throw new n(`unsupported toolResponse field "${t}"`);if(!Array.isArray(e.functionResponses)||e.functionResponses.length===0)throw new n("toolResponse.functionResponses must be a non-empty array");return e.functionResponses.map(t=>{let o=d(t);if(!o||typeof o.id!="string"||o.id.length===0)throw new n("functionResponses entries require the string id matched from the toolCall");return{type:"function_call_output",call_id:o.id,output:JSON.stringify(o.response??null)}})}function ee(e){return e.type!=="response.output_text.delta"||typeof e.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:e.delta}]}}}}function te(e){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:$,data:e}}]}}}}function ne(e){let t=d(e)??{},o=Array.isArray(t.output)?t.output:[],i=[],s=[],r=typeof t.output_text=="string"?t.output_text:"";if(!r)for(let p of o){let l=d(p);if(!(l?.type!=="message"||!Array.isArray(l.content)))for(let u of l.content){let f=d(u);f?.type==="output_text"&&typeof f.text=="string"&&(r+=f.text)}}r&&s.push({role:"assistant",content:r});for(let p of o){let l=d(p);if(l?.type!=="function_call")continue;if(typeof l.call_id!="string"||typeof l.name!="string")throw new n(`upstream function_call item is missing call_id/name: ${JSON.stringify(p)}`,R);let u=typeof l.arguments=="string"&&l.arguments.length>0?l.arguments:"{}",f;try{f=JSON.parse(u)}catch{throw new n(`upstream function_call arguments are not valid JSON: ${u}`,R)}i.push({id:l.call_id,name:l.name,args:f}),s.push({type:"function_call",call_id:l.call_id,name:l.name,arguments:u})}let a=[];i.length>0&&a.push({toolCall:{functionCalls:i}}),a.push({serverContent:{generationComplete:!0}});let c=d(t.usage),m={serverContent:{turnComplete:!0}};if(c){let p={};typeof c.input_tokens=="number"&&(p.promptTokenCount=c.input_tokens),typeof c.output_tokens=="number"&&(p.responseTokenCount=c.output_tokens),typeof c.total_tokens=="number"&&(p.totalTokenCount=c.total_tokens),Object.keys(p).length>0&&(m.usageMetadata=p)}return a.push(m),{messages:a,assistantItems:s}}export{J as a,k as b,F as c,R as d,V as e,n as f,B as g,h,G as i,Y as j,K as k,q as l,W as m,X as n,b as o,O as p,$ as q,z as r,j as s,Q as t,Z as u,ee as v,te as w,ne as x};
@@ -0,0 +1,7 @@
1
+ import{a as j,b as W}from"./chunk-TDRZZGUK.js";import{C as re,D as oe,E as se,F as ie,H as ae,I as A,J as le,K as ce,L as ue,M as de,N as he,O as pe,P as ge,Q as fe,R as me,b as Z,d as ee,e as te,q as ne,r as k}from"./chunk-TPH77ZOW.js";import{e as Q,g as z}from"./chunk-WCMX5VOF.js";import{a as X}from"./chunk-3JWYIKHM.js";import{c as p}from"./chunk-YSFSRI3D.js";p();import{Counter as R,Gauge as ye,Histogram as we,Registry as Ue,collectDefaultMetrics as Pe}from"@prometheus-io/client";import{createServer as $e}from"http";var It=9464,be="/metrics",I="(other)",Le="(none)",He=64,Me=128,De=["/v1/responses","/v1/chat/completions","/v1/systemone","/v1/messages","/v1/messages/count_tokens","/v1/models","/v1/images/models","/v1/images/generations","/v1/images/edits","/v1/audio/speech",ae,ce],Fe=new Set(De);function Ke(t){return Fe.has(t)?t:I}function Ge(t){return t===null?"none":t>=500?"5xx":t>=400?"4xx":t>=300?"3xx":t>=200?"2xx":"1xx"}var N=class{constructor(e){this.max=e}max;admitted=new Set;overflows=0;of(e,n=!0){return e===null||e===""?Le:this.admitted.has(e)?e:n?this.admitted.size>=this.max?(this.overflows+=1,I):(this.admitted.add(e),e):I}get size(){return this.admitted.size}get overflowCount(){return this.overflows}},T=class{registry=new Ue;modelLabel=new N(He);orgLabel=new N(Me);requests;duration;ttft;streamErrors;undelivered;ledgerWrites;ledgerQueue;labelCardinality;labelOverflows;constructor(e={}){this.requests=new R({name:"urun_inference_requests_total",help:"Completed /v1 requests, by lane, method, HTTP status class, streaming, model and org.",labelNames:["lane","method","status_class","stream","model","org"],registers:[this.registry]}),this.duration=new we({name:"urun_inference_request_duration_seconds",help:"Wall time from request receipt to response close. For a stream this is the WHOLE generation, not the time to the head.",labelNames:["lane","stream","model","org"],buckets:[.1,.25,.5,1,2.5,5,10,30,60,120,300,600],registers:[this.registry]}),this.ttft=new we({name:"urun_inference_ttft_seconds",help:"Time to first output-text delta on a streamed response. The metric Hugging Face gates provider listing on (<5s for conversational models).",labelNames:["lane","model","org"],buckets:[.1,.25,.5,1,2,3,5,8,15,30,60],registers:[this.registry]}),this.streamErrors=new R({name:"urun_inference_stream_errors_total",help:"Responses that emitted an error frame AFTER the head flushed. These are HTTP 200 with a complete body, so no status-based metric can see them.",labelNames:["lane","model"],registers:[this.registry]}),this.undelivered=new R({name:"urun_inference_undelivered_total",help:"Responses whose socket did not finish flushing \u2014 the client vanished mid-answer.",labelNames:["lane","model"],registers:[this.registry]}),this.ledgerWrites=new R({name:"urun_inference_ledger_writes_total",help:"Terminal fate of the per-request ledger row for each billable /v1 request. outcome=written is the row landing (reason=priced|unpriced); outcome=skipped and outcome=failed are BOTH revenue that went unrecorded. failed/duplicate is the ENG-410 fabricated-row signal.",labelNames:["outcome","reason"],registers:[this.registry]});for(let n of ue)this.ledgerWrites.inc({outcome:n.outcome,reason:n.reason},0);this.ledgerQueue=new ye({name:"urun_inference_ledger_queue",help:'The ledger write queue right now. measure="depth" is rows waiting for a worker; measure="oldest_age_seconds" is how long the head has waited \u2014 approaching the dwell bound means rows are about to be given up on, which is revenue lost.',labelNames:["measure"],registers:[this.registry],collect:()=>{this.ledgerQueue.set({measure:"depth"},de()),this.ledgerQueue.set({measure:"oldest_age_seconds"},he()/1e3)}}),this.labelCardinality=new ye({name:"urun_inference_label_cardinality",help:"Distinct values admitted for a bounded metric label. At its ceiling, further values report as (other).",labelNames:["label"],registers:[this.registry],collect:()=>{this.labelCardinality.set({label:"model"},this.modelLabel.size),this.labelCardinality.set({label:"org"},this.orgLabel.size)}}),this.labelOverflows=new R({name:"urun_inference_label_overflows_total",help:"Observations that would have taken a new label value but found the budget spent. Non-zero means a metric dimension is now partly blind.",labelNames:["label"],registers:[this.registry]}),e.defaultMetrics!==!1&&Pe({register:this.registry,prefix:"urun_inference_proxy_"})}observeLedger(e){this.ledgerWrites.inc({outcome:e.outcome,reason:e.reason})}observe(e){let n=Ke(e.path),o=e.status!==null&&!(e.status>=400&&e.status<500),r=this.modelLabel.overflowCount+this.orgLabel.overflowCount,a=this.modelLabel.of(e.model,o),s=this.orgLabel.of(e.org,o);this.modelLabel.overflowCount+this.orgLabel.overflowCount-r>0&&(this.modelLabel.overflowCount>r?this.labelOverflows.inc({label:"model"}):this.labelOverflows.inc({label:"org"}));let l=e.stream===!0?"true":"false";this.requests.inc({lane:n,method:e.method===""?I:e.method,status_class:Ge(e.status),stream:l,model:a,org:s}),this.duration.observe({lane:n,stream:l,model:a,org:s},e.duration_ms/1e3),e.ttft_ms!==null&&this.ttft.observe({lane:n,model:a,org:s},e.ttft_ms/1e3),e.stream_error&&this.streamErrors.inc({lane:n,model:a}),e.delivered||this.undelivered.inc({lane:n,model:a})}scrape(){return this.registry.metrics()}get contentType(){return this.registry.contentType}};function ve(t){return $e((e,n)=>{(async()=>{let o=(e.url??"/").split("?")[0];if(e.method!=="GET"||o!==be){n.writeHead(404,{"content-type":"text/plain"}),n.end(`no route for ${e.method} ${o} \u2014 this port serves only GET ${be}
2
+ `);return}let r=await t.scrape();n.writeHead(200,{"content-type":t.contentType}),n.end(r)})().catch(o=>{if(!n.headersSent){n.writeHead(500,{"content-type":"text/plain"}),n.end(`${String(o instanceof Error?o.message:o)}
3
+ `);return}n.end()})})}p();import{createHash as Ee}from"crypto";import{createServer as Be}from"http";var $t=9465,ze="x-urun-key-id",je="x-urun-key-fingerprint",We="urun-ratelimit-bucket:",Je="urun-ratelimit-tier:",_e="anonymous",qe=/^urun_sk_[A-Za-z0-9_-]{43}$/;function Ve(t){return Ee("sha256").update(`${We}${t}`).digest("hex")}function Ye(t){return Ee("sha256").update(`${Je}${t}`).digest("hex")}function Qe(t){let e=/^Bearer[ \t]+(\S.*)$/i.exec((t??"").trim());if(!e)return null;let n=e[1].trim();return qe.test(n)?n:null}function Re(){return Be((t,e)=>{let n=Qe(t.headers.authorization);e.setHeader(ze,n===null?_e:Ve(n)),e.setHeader(je,n===null?_e:Ye(n)),e.writeHead(200),e.end()})}p();import{createClientToken as Xe,ClientTokenError as Ze}from"@urun-sh/core";var m=class extends Error{constructor(n,o=401){super(n);this.status=o;this.name="ProxyAuthError"}status},_=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function S(t){let e=(t??"").trim();if(e==="")throw new m("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let n=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!n)throw new m("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return n[1].trim()}var et=60,tt=6e4,nt=4096;function rt(t){return t===401||t===403}var U=class{constructor(e){this.opts=e;this.mint=e.mint??Xe,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new m("empty Authorization bearer token");let n=this.cache.get(e);if(n&&this.now()-n.at<tt)return{apiKey:e,orgId:n.orgId};let o;try{o=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:et})).orgId}catch(r){throw r instanceof Ze&&rt(r.status)?(this.cache.delete(e),new m("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new _(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${r instanceof Error?r.message:String(r)}`)}if(!o)throw new _("the control plane verified the API key but returned no org_id \u2014 refusing to route a request with no tenancy (control-plane contract violation)");if(this.cache.size>=nt){let r=this.cache.keys().next();r.done||this.cache.delete(r.value)}return this.cache.set(e,{orgId:o,at:this.now()}),{apiKey:e,orgId:o}}clear(){this.cache.clear()}};p();var ot=8080,P="serve",Gt="0.0.0.0";function st(t,e,n){let o=(t[e]??"").trim();if(!o)throw new Error(`${e} is not set \u2014 ${n}`);return o}function Bt(t=process.env){let e=(t.PORT??"").trim(),n=e===""?ot:Number(e);if(!Number.isInteger(n)||n<0||n>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let o=ne(k(st(t,"URUN_BASE_URL","the hosted proxy needs the uRun session-gateway base it opens sessions and verifies API keys against (e.g. https://api.urun.sh)"),"URUN_BASE_URL")),r=(t.URUN_API_URL??"").trim(),a=r?k(r,"URUN_API_URL"):`${o.replace(/\/+$/,"")}/v1`,s=(t.URUN_CATALOG_URL??"").trim(),i=(t.URUN_CATALOG_ANON_KEY??"").trim();if(!!s!=!!i)throw new Error("URUN_CATALOG_URL and URUN_CATALOG_ANON_KEY must be set together (or both left unset) \u2014 exactly one of them is set, which would silently disable the model-catalog oracle");let l=s?{catalogUrl:k(s,"URUN_CATALOG_URL"),anonKey:i}:null,u=(t.URUN_SESSION_TOKEN_SECRET??"").trim(),f=(t.URUN_SHARED_ORG_API_KEY??"").trim(),x=(t.URUN_SHARED_ORG_ID??"").trim();if(!!f!=!!x)throw new Error("URUN_SHARED_ORG_API_KEY and URUN_SHARED_ORG_ID must be set together (or both left unset) \u2014 exactly one of them is set, which would silently leave the shared endpoints on the session lane");return{port:n,baseUrl:o,apiUrl:a,catalog:l,decisionSecret:u,sharedOrgKey:f,sharedOrgId:x}}p();import{createHash as it,randomUUID as at}from"crypto";var lt=512;function M(t){return`urun-inference-proxy-${it("sha256").update(t).digest("hex").slice(0,32)}`}var $=class{constructor(e){this.opts=e}opts;tenants=new Map;label(e,n){return Object.assign(Object.create(e),{tenant:n.orgId})}build(e){if(this.opts.build){let i=this.opts.build(e);return{router:i.router,clients:this.label(i.clients,e)}}let n=0,o=M(e.apiKey),r=()=>{n+=1},a=j({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:P,sharedOnlyListing:!0,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:()=>n===0?o:`${o}#${n}`,onPoolWedged:r,release:"end"}),s=this.opts.decisionIntake?new fe({targetFor:i=>a.targetFor(i),apiUrl:this.opts.apiUrl,apiKey:e.apiKey,orgId:e.orgId,fnName:P,secret:this.opts.decisionIntake.secret,...this.opts.decisionIntake.shared?{shared:this.opts.decisionIntake.shared}:{}}):void 0;return{router:a,clients:this.label(W(a,void 0,r,s),e)}}clientsFor(e){let n=this.tenants.get(e.apiKey);if(n)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,n),n.clients;for(;this.tenants.size>=lt;){let r=this.tenants.entries().next();if(r.done)break;let[a,s]=r.value;this.tenants.delete(a),s.router.closeAll().catch(()=>{})}let o=this.build(e);return this.tenants.set(e.apiKey,o),o.clients}get size(){return this.tenants.size}async closeAll(){let e=[...this.tenants.values()].map(o=>o.router);this.tenants.clear();let n=[];if(await Promise.all(e.map(async o=>{try{await o.closeAll()}catch(r){n.push(String(r instanceof Error?r.message:r))}})),n.length>0)throw new Error(`failed to close ${n.length} tenant backhaul(s): ${n.join("; ")}`)}},Se=512,ct=1024,L=class extends Error{constructor(){super(`conversation capacity reached (${Se} conversations on this replica) \u2014 every slot holds an active or resumable conversation; retry later or scale out`),this.name="ConversationCapacityError"}};function ut(t){return{sessionStartOptions:e=>{if(e.startsWith(z))return;let n=t.get(e);return n===void 0?void 0:{sessionId:n}},onSessionOpen:(e,n)=>{e.startsWith(z)||t.set(e,n)}}}function dt(t,e,n,o){let r=j({baseUrl:t.baseUrl,orgId:e.orgId,defaultApp:null,fnName:P,sharedOnlyListing:!0,apiUrl:t.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:t.baseUrl},catalog:t.catalog,subject:n.subject,release:"detach",...ut(o)});return{clients:W(r),release:async()=>{await r.closeAll()}}}var H=class{constructor(e){this.opts=e}opts;byCaller=new Map;byHandle=new Map;build(e,n){let o={id:n,subject:`${M(e.apiKey)}:live:${n}`},r=new Map,a=this.opts.build?this.opts.build(e,o):dt(this.opts,e,o,r);return{id:n,backhaul:a,attachments:0,nativeRefs:r}}attachEntry(e,n){let o=this.byCaller.get(e.apiKey);o===void 0&&(o=new Map,this.byCaller.set(e.apiKey,o));let r=o.get(n);if(r!==void 0)return o.delete(n),o.set(n,r),r.attachments+=1,r;this.evictOverflow();let a=this.build(e,n);return a.attachments=1,o.set(n,a),a}get conversationCount(){let e=0;for(let n of this.byCaller.values())e+=n.size;return e}evictOverflow(){for(;this.conversationCount>=Se;){let e=this.byCaller.keys().next();if(e.done)throw new L;let n=e.value,o=this.byCaller.get(n),r=!1;for(let[a,s]of o)if(!(s.attachments>0)){o.delete(a),this.dropHandleIndexFor(n,a),s.backhaul.release().catch(()=>{}),r=!0;break}if(!r)throw o.size===0&&this.byCaller.delete(n),new L;o.size===0&&this.byCaller.delete(n)}}dropHandleIndexFor(e,n){for(let[o,r]of this.byHandle)r.apiKey===e&&r.conversationId===n&&this.byHandle.delete(o)}connectionFor(e){let n=null,o=!1,r=()=>(n===null&&(n=this.attachEntry(e,at())),n),a={createResponse:(s,i)=>r().backhaul.clients.createResponse(s,i),catalogIdentity:s=>r().backhaul.clients.catalogIdentity(s),servingAppSlug:s=>{let i=r().backhaul.clients.servingAppSlug;if(i===void 0)throw new Error("the conversation backhaul wires no servingAppSlug seam \u2014 the caller-org app a request resolves to cannot be known");return i(s)},listModels:()=>r().backhaul.clients.listModels(),sessionHandle:s=>r().backhaul.clients.sessionHandle(s),createResponseOn:(s,i)=>r().backhaul.clients.createResponseOn(s,i),onSessionEnd:(s,i)=>r().backhaul.clients.onSessionEnd(s,i)};return a.openAudio=async(s,i)=>{let l=r().backhaul.clients.openAudio;if(l===void 0)throw new Error("the conversation backhaul wires no openAudio seam \u2014 voice is unavailable on this deployment");return l(s,i)},a.openVideo=async s=>{let i=r().backhaul.clients.openVideo;if(i===void 0)throw new Error("the conversation backhaul wires no openVideo seam \u2014 video-in is unavailable on this deployment");return i(s)},a.openVideoOut=async s=>{let i=r().backhaul.clients.openVideoOut;if(i===void 0)throw new Error("the conversation backhaul wires no openVideoOut seam \u2014 video-out is unavailable on this deployment");return i(s)},a.supportsAudio=async s=>{let i=r().backhaul.clients.supportsAudio;if(i===void 0)throw new Error("the conversation backhaul wires no supportsAudio seam \u2014 the realtime lane cannot gate audio modality on this deployment");return i(s)},a.supportsSpeech=async s=>{let i=r().backhaul.clients.supportsSpeech;if(i===void 0)throw new Error("the conversation backhaul wires no supportsSpeech seam \u2014 the speech lane cannot gate model capability on this deployment");return i(s)},a.speechVoices=async s=>{let i=r().backhaul.clients.speechVoices;if(i===void 0)throw new Error("the conversation backhaul wires no speechVoices seam \u2014 the speech lane cannot resolve the voice set /v1/models publishes on this deployment");return i(s)},a.speechSessionHandle=async s=>{let i=r().backhaul.clients.speechSessionHandle;if(i===void 0)throw new Error("the conversation backhaul wires no speechSessionHandle seam \u2014 the speech lane cannot resolve its own pooled session on this deployment");return i(s)},a.openAudioOn=async s=>{let i=r().backhaul.clients.openAudioOn;if(i===void 0)throw new Error("the conversation backhaul wires no openAudioOn seam \u2014 the speech lane cannot open its pinned native session audio on this deployment");return i(s)},a.evictSpeechSession=async s=>{let i=r().backhaul.clients.evictSpeechSession;if(i===void 0)throw new Error("the conversation backhaul wires no evictSpeechSession seam \u2014 the speech lane cannot retire its pinned speech session after a failed turn on this deployment");return i(s)},{clients:a,bindResumeHandle:s=>{let i=n;if(i===null)throw new Error("bindResumeHandle before the conversation opened \u2014 no conversation to bind to");for(;this.byHandle.size>=ct;){let l=this.byHandle.keys().next();if(l.done)break;this.byHandle.delete(l.value)}this.byHandle.set(s,{apiKey:e.apiKey,conversationId:i.id})},clientsForResume:async s=>{let i=this.byHandle.get(s),l=i===void 0?void 0:this.byCaller.get(i.apiKey),u=i===void 0||l===void 0?void 0:l.get(i.conversationId);if(i===void 0||u===void 0||i.apiKey!==e.apiKey)throw new Q("the conversation behind this resume handle is gone (evicted at capacity, or the replica restarted) or the handle is not yours \u2014 reconnect without a handle to start a new conversation");return n=u,u.attachments+=1,u.backhaul.clients},release:async()=>{if(o)return;o=!0;let s=n;n=null,s!==null&&(s.attachments-=1,!(s.attachments>0)&&await s.backhaul.release().catch(()=>{}))}}}async detachAll(){let e=[];for(let o of this.byCaller.values())for(let r of o.values())e.push(r.backhaul.release());this.byCaller.clear(),this.byHandle.clear();let n=[];if(await Promise.all(e.map(async o=>{try{await o}catch(r){n.push(String(r instanceof Error?r.message:r))}})),n.length>0)throw new Error(`failed to detach ${n.length} conversation backhaul(s): ${n.join("; ")}`)}};p();var ht="/inference-ledger",pt=1e4,D=class extends Error{ledgerFailureReason;constructor(e,n="duplicate"){super(e),this.name="LedgerDuplicateError",this.ledgerFailureReason=n}},q=class extends Error{constructor(n,o){super(o);this.ledgerAlreadyWritten=n;this.name="LedgerAlreadyWrittenError"}ledgerAlreadyWritten},g=class extends Error{ledgerFailureReason="surface";constructor(e){super(e),this.name="LedgerSurfaceError"}},V=class extends m{ledgerFailureReason="auth";constructor(e){super(e),this.name="LedgerAuthError"}},gt=[["inference_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["model_id",t=>typeof t=="string"&&/[^\s]/.test(t)],["variant",t=>t===null||typeof t=="string"],["task",t=>typeof t=="string"&&/[^\s]/.test(t)],["http_status",t=>typeof t=="number"&&Number.isInteger(t)],["started_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["completed_at",t=>typeof t=="string"&&!Number.isNaN(Date.parse(t))],["prompt_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["completion_tokens",t=>t===null||typeof t=="number"&&Number.isInteger(t)],["output_units",xe],["cost_nano_usd",xe],["price_version",t=>t===null||typeof t=="string"]];function ft(t){for(let[e,n]of gt){if(!(e in t))return`${e} (absent)`;if(!n(t[e]))return`${e} (${JSON.stringify(t[e])})`}return null}function mt(t){let e;try{e=JSON.parse(t)}catch{return{status:"unavailable"}}if(e===null||typeof e!="object"||Array.isArray(e))return{status:"unavailable"};let n=e.existing_status;if(n==="not_in_this_org")return{status:"not_in_this_org"};if(n!=="found")return{status:"unavailable"};let o=e.existing;return o===null||typeof o!="object"||Array.isArray(o)?{status:"unavailable"}:ft(o)!==null?{status:"unavailable"}:{status:"found",row:o}}function xe(t){return t===null?!0:typeof t=="number"?Number.isFinite(t):typeof t!="string"?!1:/[^\s]/.test(t)&&Number.isFinite(Number(t))}function J(t,e){return t===null?e===null:e===null?!1:Number(e)===t}function Ce(t,e){if(typeof e!="string")return!1;let n=Date.parse(t),o=Date.parse(e);return Number.isFinite(n)&&Number.isFinite(o)&&n===o}function yt(t,e){let n=e;return n.inference_id===t.inference_id&&n.model_id===t.model_id&&(n.variant??null)===t.variant&&n.task===t.task&&n.http_status===t.http_status&&Ce(t.started_at,n.started_at)&&Ce(t.completed_at,n.completed_at)&&J(t.prompt_tokens,n.prompt_tokens)&&J(t.completion_tokens,n.completion_tokens)&&J(t.output_units,n.output_units)}function wt(t,e){if(t===null||typeof t!="object"||Array.isArray(t))throw new g(`${e} answered a "recorded" that is not an object: ${JSON.stringify(t)}`);let n=t,o=n.inference_id;if(typeof o!="string"||o==="")throw new g(`${e} answered a row with no inference_id: ${JSON.stringify(t)}`);if(!("cost_nano_usd"in n))throw new g(`${e} answered a row with no cost_nano_usd field: ${JSON.stringify(t)}`);let r=n.cost_nano_usd;if(r!=null&&!(typeof r=="number"&&Number.isInteger(r)&&r>=0))throw new g(`${e} answered cost_nano_usd ${JSON.stringify(r)}, which is neither a non-negative integer nor null`);let a=u=>{if(!(u in n))throw new g(`${e} answered a row with no ${u} field: ${JSON.stringify(t)}`);let f=n[u];if(f===null)return null;if(typeof f!="string")throw new g(`${e} answered a non-string ${u}: ${JSON.stringify(f)}`);if(!/[^\s]/.test(f))throw new g(`${e} answered a BLANK ${u} (${JSON.stringify(f)}) \u2014 it satisfies "is a string" while naming nothing, which is exactly what inference_requests_priced_attribution_check exists to forbid`);return f},s=a("price_version"),i=a("unpriced_reason"),l=r!=null;if(l&&(s===null||i!==null))throw new g(`${e} answered a PRICED row (cost_nano_usd ${String(r)}) that is not cleanly attributed \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(i)}`);if(!l&&(s!==null||i===null))throw new g(`${e} answered an UNPRICED row that does not say why \u2014 price_version ${JSON.stringify(s)}, unpriced_reason ${JSON.stringify(i)}. NULL means NOT YET PRICED, and the reason is how an operator learns that traffic is being served which nobody can bill.`);return{inference_id:o,cost_nano_usd:r===void 0?null:r,price_version:s,unpriced_reason:i}}var F=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async write(e,n){let o=`${this.opts.apiUrl.replace(/\/+$/,"")}${ht}`,r;try{r=await this.fetchImpl(o,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify(n),signal:AbortSignal.timeout(pt)})}catch(l){throw new g(`the uRun ledger surface at ${o} could not be reached: ${l instanceof Error?l.message:String(l)}`)}if(r.status===409){let l=await r.text().catch(()=>""),u=mt(l);throw u.status==="found"&&yt(n,u.row)?new q(u.row,`the uRun control plane already holds THIS PROXY'S OWN row for inference_id ${n.inference_id} \u2014 every billable field matches the row being re-sent, so an earlier attempt landed and this retry is a no-op. Nothing was written and nothing was overwritten.`):u.status==="not_in_this_org"?new D(`inference_id ${n.inference_id} is ALREADY TAKEN by a ledger row this org cannot read. The id space is a global primary key, so this is not a retry and cannot be this proxy's own write \u2014 either two orgs collided on a uuid, or a row was written for this id by somebody else.`,"duplicate_foreign"):new D(`the uRun control plane already holds a ledger row for inference_id ${n.inference_id} that is NOT the one being written${u.status==="unavailable"?" (and it could not be read back to compare)":""} \u2014 nothing was written and nothing was overwritten. Either this proxy wrote twice, or a row was fabricated ahead of it (this write is authenticated by the CALLER'S OWN org key). ${l.slice(0,400)}`)}if(r.status===401||r.status===403)throw await r.text().catch(()=>{}),new V("the uRun org API key was rejected by the uRun control plane when writing the inference ledger \u2014 the request was served but produced no billable row");if(!r.ok){let l=await r.text().catch(u=>`<body unreadable: ${u instanceof Error?u.message:String(u)}>`);throw new g(`the uRun ledger surface at ${o} answered HTTP ${r.status}: ${l.slice(0,400)}`)}let a;try{a=await r.json()}catch(l){throw new g(`the uRun ledger surface at ${o} answered a body that is not JSON: ${l instanceof Error?l.message:String(l)}`)}let s=a?.recorded;if(s===void 0)throw new g(`the uRun ledger surface at ${o} answered a body with no "recorded" field`);let i=wt(s,`the uRun ledger surface at ${o}`);if(i.inference_id!==n.inference_id)throw new g(`the uRun ledger surface at ${o} answered for inference_id ${i.inference_id} when asked about ${n.inference_id}`);return i}};p();import{createServer as vt}from"http";p();var K="/inference-usage",Oe=25e3;var bt=new Set(["Error","TypeError","SyntaxError","TimeoutError"]);function ke(t){if(!(t instanceof Error))return`a ${typeof t}`;let e=bt.has(t.name)?t.name:"Error",n=t.cause?.code;return typeof n=="string"&&/^[A-Z][A-Z0-9_]{0,31}$/.test(n)?`${e} (${n})`:e}var G=class{constructor(e){this.opts=e;this.fetchImpl=e.fetchImpl??fetch}opts;fetchImpl;async lookup(e,n){let o=`${this.opts.apiUrl.replace(/\/+$/,"")}${K}`,r;try{r=await this.fetchImpl(o,{method:"POST",headers:{"content-type":"application/json",authorization:`Bearer ${e}`},body:JSON.stringify({inference_ids:n}),signal:AbortSignal.timeout(Oe)})}catch(s){throw new A(`the uRun usage surface (POST ${K}) could not be reached within ${Oe}ms: ${ke(s)}`)}if(r.status===401||r.status===403)throw await r.text().catch(()=>{}),new m("the uRun org API key was rejected by the uRun control plane \u2014 check the key and that it is not revoked");if(!r.ok){let s=await r.text().catch(()=>null),i=s===null?"body unreadable":`${s.length}-character body`;throw new A(`the uRun usage surface (POST ${K}) answered HTTP ${r.status} (${i})`)}let a;try{a=await r.json()}catch(s){throw new A(`the uRun usage surface (POST ${K}) answered HTTP ${r.status} with a body that is not JSON: ${ke(s)}`)}return le(a,this.opts.onRejectedRow)}};var _t=5e3,Et=3e3,Rt="/api/client-tokens";function Y(t,e,n){t.writeHead(e,{"content-type":"application/json"}),t.end(JSON.stringify(n))}function Ae(t,e,n,o){Y(t,e,{error:{message:n,type:o}})}async function St(t,e=fetch){let n=`${t.replace(/\/+$/,"")}${Rt}`;try{let o=await e(n,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(Et)});return o.status===404?{ready:!1,detail:`${n} answered 404 \u2014 this is not a uRun control plane`}:o.status>=500?{ready:!1,detail:`${n} answered HTTP ${o.status}`}:{ready:!0,detail:`${n} answered HTTP ${o.status}`}}catch(o){return{ready:!1,detail:`${n} unreachable: ${o instanceof Error?o.message:String(o)}`}}}function _n(t){let e=t.resolver??new U({gatewayUrl:t.baseUrl}),n=t.decisionSecret??"",o=t.sharedOrgKey&&t.sharedOrgId?{apiKey:t.sharedOrgKey}:void 0,r=t.registry??new $({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog,...n!==""?{decisionIntake:{secret:n,...o?{shared:o}:{}}}:{}});n!==""&&process.stderr.write(`inference-proxy: stateless direct path ENABLED \u2014 caller-org decisions dial ready runtimes directly (no session mint)
4
+ `);let a=t.probeControlPlane??(()=>St(t.baseUrl)),s=t.usageClient??new G({apiUrl:t.apiUrl}),i=t.ledgerClient??new F({apiUrl:t.apiUrl}),l=new T(t.defaultMetrics===void 0?{}:{defaultMetrics:t.defaultMetrics}),u=async c=>e.resolve(S(c.headers.authorization)),f=c=>c instanceof m?{status:c.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:c instanceof _?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null,x=me({clients:async c=>r.clientsFor(await u(c)),usage:{authorize:c=>S(c.headers.authorization),lookup:(c,d)=>s.lookup(c,d)},statusOf:f,...t.requestLog?{requestLog:t.requestLog}:{},observe:c=>l.observe(c),ledger:{write:(c,d)=>i.write(S(c.headers.authorization),d),observe:c=>l.observeLedger(c),...t.ledgerLog?{report:t.ledgerLog}:{}}}),v=null,E=vt((c,d)=>{let h=new URL(c.url??"/","http://localhost");(async()=>{if(c.method==="GET"&&h.pathname==="/healthz"){Y(d,200,{ok:!0});return}if(c.method==="GET"&&h.pathname==="/readyz"){let O=Date.now();if(!v||O-v.at>=_t){let Te=await a();v={at:O,...Te}}Y(d,v.ready?200:503,{ok:v.ready,control_plane:v.detail});return}let y=h.pathname==="/v1"||h.pathname.startsWith("/v1/"),C=h.pathname.startsWith("/partners/");if(!y&&!C){Ae(d,404,`no route for ${c.method} ${h.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface, the partner adapter surface (/partners/...), the WS upgrade paths (/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent and /v1/realtime), plus /healthz and /readyz`,"invalid_request_error");return}x(c,d)})().catch(y=>{if(!d.headersSent){Ae(d,500,String(y instanceof Error?y.message:y),"proxy_error");return}d.end()})}),B=t.conversations??new H({baseUrl:t.baseUrl,apiUrl:t.apiUrl,catalog:t.catalog}),Ie=[Z(E,{authenticate:async c=>{let d=await e.resolve(c),h=B.connectionFor(d);return{clients:h.clients,scope:M(d.apiKey),onBindResumeHandle:h.bindResumeHandle,clientsForResume:h.clientsForResume,release:h.release}}}),te(E,{clients:async c=>r.clientsFor(await u(c)),statusOf:f}).close,oe(E,{resolver:{resolve:async(c,d)=>{let h=await e.resolve(S(c.headers.authorization)),y=B.connectionFor(h),C;try{C=await ie(y.clients,se(c),d)}catch(O){throw y.release().catch(()=>{}),O}return{...C,release:y.release}}}})];E.on("upgrade",(c,d)=>{let h=new URL(c.url??"/","http://localhost");h.pathname===X||h.pathname===ee||h.pathname===re||d.destroyed||(d.write(`HTTP/1.1 404 Not Found\r
5
+ Connection: close\r
6
+ \r
7
+ no WS route for ${h.pathname} \u2014 this endpoint serves only the Gemini Live, /v1/responses, and /v1/realtime upgrade paths`),d.destroy())});let Ne=async()=>{for(let c of Ie)await c();await B.detachAll()};return{server:E,metricsServer:ve(l),metrics:l,edgeIdentityServer:Re(),registry:r,closeAll:()=>r.closeAll(),closeWebSockets:Ne}}async function En(t,e,n){n&&await n();let o=new Promise(r=>t.close(()=>r()));t.closeAllConnections(),await o,await pe()||ge(),await e()}export{It as a,be as b,I as c,Le as d,He as e,Me as f,Ke as g,Ge as h,N as i,T as j,ve as k,$t as l,ze as m,je as n,We as o,Je as p,_e as q,Ve as r,Ye as s,Qe as t,Re as u,m as v,_ as w,S as x,tt as y,nt as z,U as A,ot as B,P as C,Gt as D,Bt as E,lt as F,M as G,$ as H,Se as I,ct as J,L as K,H as L,ht as M,pt as N,D as O,g as P,wt as Q,F as R,_t as S,Et as T,_n as U,En as V};
@@ -1 +1 @@
1
- import{c as m}from"./chunk-5CCJUNH7.js";m();var W=24e3,b="audio",w=class{constructor(r,t){this.backend=r;this.opts=t}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(r){if(!this.source)throw new Error("startOutbound() not called");let t=Buffer.from(r,"base64"),n=Math.floor(t.byteLength/2),s=new Int16Array(n);for(let a=0;a<n;a++)s[a]=t.readInt16LE(a*2);this.source.onData(s)}async startInbound(r){if(r==null)throw new Error('startInbound: no downstream audio track \u2014 the runtime audio producer has not been consumed yet (session.stream("audio").track is null)');this.sink=this.backend.createSink(r),this.sink.onframe=t=>{let n=Buffer.from(t.samples.buffer,t.samples.byteOffset,t.samples.byteLength).toString("base64");for(let s of this.outHandlers)s(n)}}onOutputAudio(r){return this.outHandlers.add(r),()=>this.outHandlers.delete(r)}};async function H(e,r,t=1e4){let n=new w(r,{sampleRate:24e3}),s=await n.startOutbound();await e.stream(b).attach(s);let a=await V(e,t);return await n.startInbound(a),n}function V(e,r){let t=e.stream(b);return t.track!=null?Promise.resolve(t.track):new Promise((n,s)=>{let a,c=setTimeout(()=>{a?.(),s(new Error(`enableSessionAudio: no downstream audio track within ${r}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},r),u=o=>{o!=null&&(clearTimeout(c),a?.(),n(o))};a=t.on?.("track",u),t.track!=null&&u(t.track)})}var h="rt-video-in",y=256e3;function N(e){let r=e.stream(h),t=r.emit;if(typeof t!="function")throw new Error(`enableSessionVideo: session.stream('${h}') has no emit() \u2014 the named-DATA stream produce seam (@urun-sh/core SessionStream.emit, contract \xA75) is required for the video frame lane`);return{sendInputFrame:async n=>{if(n.byteLength===0)throw new Error("enableSessionVideo: refusing to send an empty video frame");if(n.byteLength>y)throw new Error(`enableSessionVideo: frame is ${n.byteLength} bytes \u2014 over the ${y}-byte data-channel budget (the SFU's 262144-byte SCTP ceiling drops oversize messages SILENTLY; send smaller/lower-quality frames)`);await t.call(r,n)}}}var T=24e3,l=480,I=111;async function L(){let e=await import("opusscript"),r=e.default??e;return new r(T,1,2048)}async function $(){let e=await import("werift"),r=await L();return{createSource:()=>{let t=new e.MediaStreamTrack({kind:"audio"}),n=Math.random()*4294967295>>>0,s=Math.random()*65535&65535,a=Math.random()*4294967295>>>0,c=new Int16Array(0);return{track:t,onData:u=>{let o=new Int16Array(c.length+u.length);o.set(c,0),o.set(u,c.length);let i=0;for(;o.length-i>=l;){let d=o.subarray(i,i+l);i+=l;let f=Buffer.from(d.buffer,d.byteOffset,d.byteLength),S=r.encode(f,l);s=s+1&65535,a=a+l>>>0;let E=new e.RtpHeader({version:2,payloadType:I,sequenceNumber:s,timestamp:a,ssrc:n,marker:!1}),A=new e.RtpPacket(E,S);t.writeRtp(A)}c=o.slice(i)}}},createSink:t=>{let n={onframe:null};return t.onReceiveRtp.subscribe(a=>{let c;try{c=r.decode(a.payload)}catch{return}let u=new Int16Array(c.length/2);for(let o=0;o<u.length;o++)u[o]=c.readInt16LE(o*2);n.onframe?.({samples:u})}),n}}}async function M(){let e=await import("@roamhq/wrtc");return{createSource:()=>{let r=new e.nonstandard.RTCAudioSource;return{track:r.createTrack(),onData:n=>r.onData({samples:n,sampleRate:24e3})}},createSink:r=>{let t={onframe:null},n=new e.nonstandard.RTCAudioSink(r);return n.ondata=s=>{t.onframe?.(s)},t}}}m();import{WtControlDecoder as U,consumeWtDataStreams as _,encodeWtControlFrame as x,STREAM_END as D}from"@urun-sh/core";var p="rt-video-out",P="urun.videoOut.v1",g="h264";function k(e,r){if(!Number.isInteger(e.seq)||e.seq<0)throw new Error(`${r}: header seq must be a non-negative integer (got ${e.seq})`);if(!Number.isInteger(e.ptsUs))throw new Error(`${r}: header ptsUs must be an integer microsecond timestamp (got ${e.ptsUs})`);if(typeof e.codec!="string"||e.codec.length===0)throw new Error(`${r}: header codec must be a non-empty string`);if(typeof e.key!="boolean")throw new Error(`${r}: header key must be a boolean`)}function J(e,r){if(k(e,"encodeVideoOutRecord"),r.byteLength===0)throw new Error("encodeVideoOutRecord: refusing to encode an empty access unit");let t=new TextEncoder().encode(JSON.stringify({v:1,seq:e.seq,ptsUs:e.ptsUs,codec:e.codec,key:e.key}));if(t.byteLength>65535)throw new Error(`encodeVideoOutRecord: header JSON is ${t.byteLength} bytes \u2014 over the u16 length prefix`);let n=new Uint8Array(2+t.byteLength+r.byteLength);return n[0]=t.byteLength&255,n[1]=t.byteLength>>8,n.set(t,2),n.set(r,2+t.byteLength),n}function O(e){if(e.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${e.byteLength} byte(s), no header length)`);let r=e[0]|e[1]<<8;if(e.byteLength<2+r)throw new Error(`decodeVideoOutRecord: truncated record (header says ${r} bytes, ${e.byteLength-2} present)`);let t;try{t=JSON.parse(new TextDecoder().decode(e.subarray(2,2+r)))}catch(c){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(c)})`)}let n=t;if(n===null||typeof n!="object"||n.v!==1)throw new Error(`decodeVideoOutRecord: header is not spec v1 (v=${JSON.stringify(n?.v)}) \u2014 a newer producer must be met with a newer consumer, never silently misread`);let s=n;k(s,"decodeVideoOutRecord");let a=e.subarray(2+r);if(a.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:s,au:a.slice()}}async function Y(e){let r=e.wtDataEndpoint;if(typeof r!="function")throw new Error("negotiateSessionVideoOutQuic: the core session exposes no wtDataEndpoint() \u2014 the \xA75-over-QUIC dial seam (urun-infra#1608 TransportOfferResponse.webtransport, offered with wtData: true) is required for the video OUT lane; there is no WS fallback");let t=await r.call(e),n=t.sessionId??e.sessionId;if(typeof n!="string"||n.length===0)throw new Error("negotiateSessionVideoOutQuic: no session id \u2014 the WT hello requires one and neither the negotiated endpoint nor the core session carries it");return{transport:"webtransport",url:t.url,certHashes:t.certHashes,token:t.token,sessionId:n,stream:p,codec:g,framing:P}}var G="urun.videoOut.v2-ws";function Q(e){let r=e.stream(p);if(typeof r.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${p}') has no messages() \u2014 the named-DATA stream consume seam (@urun-sh/core SessionStream.messages, contract \xA75) is required for the video OUT lane`);let t=new Set,n=new Set,s=new Set,a=o=>{for(let i of n)i(o)},c=!1,u=async()=>{for await(let o of r.messages()){if(!(o instanceof Uint8Array)){a(new Error(`openSessionVideoOutLane: "${p}" delivered a non-BINARY \xA75 payload (${typeof o}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let i;try{i=O(o)}catch(d){a(d instanceof Error?d:new Error(String(d)));continue}for(let d of t)d(i)}for(let o of s)o()};return{codec:g,onOutputFrame:o=>(t.add(o),c||(c=!0,u().catch(i=>a(i instanceof Error?i:new Error(String(i))))),()=>t.delete(o)),onLaneError:o=>(n.add(o),()=>n.delete(o)),onEnd:o=>(s.add(o),()=>s.delete(o))}}function B(e){if(e.length%2!==0||/[^0-9a-fA-F]/.test(e))throw new Error(`consumeVideoOut: certHashes entry is not hex sha-256 (${JSON.stringify(e)})`);let r=new Uint8Array(e.length/2);for(let t=0;t<r.length;t++)r[t]=parseInt(e.slice(t*2,t*2+2),16);return r}async function j(e,r,t){let n=t??(typeof globalThis.WebTransport=="function"?(i,d)=>new globalThis.WebTransport(i,d):void 0);if(!n)throw new Error("consumeVideoOut: no WebTransport implementation \u2014 this platform has no globalThis.WebTransport; inject a connect factory (the third argument)");let s=n(e.url,{serverCertificateHashes:e.certHashes.map(i=>({algorithm:"sha-256",value:B(i)}))});await s.ready;let a=await s.createBidirectionalStream();await a.writable.getWriter().write(x({t:"hello",token:e.token,sessionId:e.sessionId}));let c=a.readable.getReader(),u=new U,o=!1;for(;!o;){let{value:i,done:d}=await c.read();if(d)throw new Error("consumeVideoOut: gateway closed the control stream before hello-ack");if(i)for(let f of u.push(i)){if(f.t==="hello-ack"){o=!0;break}throw new Error(`consumeVideoOut: gateway rejected hello (got ${JSON.stringify(f.t)} before hello-ack)`)}}await _(s.incomingUnidirectionalStreams,{onData:(i,d)=>{if(i===e.stream){if(d===D){r.onEnd?.();return}if(!(d instanceof Uint8Array)){r.onLaneError(new Error(`consumeVideoOut: "${e.stream}" delivered a non-BINARY \xA75 payload (${typeof d}) \u2014 spec v1 records ride BINARY envelopes only`));return}try{r.onFrame(O(d))}catch(f){r.onLaneError(f instanceof Error?f:new Error(String(f)))}}},onLaneError:i=>r.onLaneError(i)})}export{W as a,w as b,H as c,h as d,y as e,N as f,$ as g,M as h,p as i,P as j,g as k,J as l,O as m,Y as n,G as o,Q as p,j as q};
1
+ import{c as m}from"./chunk-YSFSRI3D.js";m();var W=24e3,b="audio",w=class{constructor(r,t){this.backend=r;this.opts=t}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(r){if(!this.source)throw new Error("startOutbound() not called");let t=Buffer.from(r,"base64"),n=Math.floor(t.byteLength/2),s=new Int16Array(n);for(let a=0;a<n;a++)s[a]=t.readInt16LE(a*2);this.source.onData(s)}async startInbound(r){if(r==null)throw new Error('startInbound: no downstream audio track \u2014 the runtime audio producer has not been consumed yet (session.stream("audio").track is null)');this.sink=this.backend.createSink(r),this.sink.onframe=t=>{let n=Buffer.from(t.samples.buffer,t.samples.byteOffset,t.samples.byteLength).toString("base64");for(let s of this.outHandlers)s(n)}}onOutputAudio(r){return this.outHandlers.add(r),()=>this.outHandlers.delete(r)}};async function H(e,r,t=1e4){let n=new w(r,{sampleRate:24e3}),s=await n.startOutbound();await e.stream(b).attach(s);let a=await V(e,t);return await n.startInbound(a),n}function V(e,r){let t=e.stream(b);return t.track!=null?Promise.resolve(t.track):new Promise((n,s)=>{let a,c=setTimeout(()=>{a?.(),s(new Error(`enableSessionAudio: no downstream audio track within ${r}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},r),u=o=>{o!=null&&(clearTimeout(c),a?.(),n(o))};a=t.on?.("track",u),t.track!=null&&u(t.track)})}var h="rt-video-in",y=256e3;function N(e){let r=e.stream(h),t=r.emit;if(typeof t!="function")throw new Error(`enableSessionVideo: session.stream('${h}') has no emit() \u2014 the named-DATA stream produce seam (@urun-sh/core SessionStream.emit, contract \xA75) is required for the video frame lane`);return{sendInputFrame:async n=>{if(n.byteLength===0)throw new Error("enableSessionVideo: refusing to send an empty video frame");if(n.byteLength>y)throw new Error(`enableSessionVideo: frame is ${n.byteLength} bytes \u2014 over the ${y}-byte data-channel budget (the SFU's 262144-byte SCTP ceiling drops oversize messages SILENTLY; send smaller/lower-quality frames)`);await t.call(r,n)}}}var T=24e3,l=480,I=111;async function L(){let e=await import("opusscript"),r=e.default??e;return new r(T,1,2048)}async function $(){let e=await import("werift"),r=await L();return{createSource:()=>{let t=new e.MediaStreamTrack({kind:"audio"}),n=Math.random()*4294967295>>>0,s=Math.random()*65535&65535,a=Math.random()*4294967295>>>0,c=new Int16Array(0);return{track:t,onData:u=>{let o=new Int16Array(c.length+u.length);o.set(c,0),o.set(u,c.length);let i=0;for(;o.length-i>=l;){let d=o.subarray(i,i+l);i+=l;let f=Buffer.from(d.buffer,d.byteOffset,d.byteLength),S=r.encode(f,l);s=s+1&65535,a=a+l>>>0;let E=new e.RtpHeader({version:2,payloadType:I,sequenceNumber:s,timestamp:a,ssrc:n,marker:!1}),A=new e.RtpPacket(E,S);t.writeRtp(A)}c=o.slice(i)}}},createSink:t=>{let n={onframe:null};return t.onReceiveRtp.subscribe(a=>{let c;try{c=r.decode(a.payload)}catch{return}let u=new Int16Array(c.length/2);for(let o=0;o<u.length;o++)u[o]=c.readInt16LE(o*2);n.onframe?.({samples:u})}),n}}}async function M(){let e=await import("@roamhq/wrtc");return{createSource:()=>{let r=new e.nonstandard.RTCAudioSource;return{track:r.createTrack(),onData:n=>r.onData({samples:n,sampleRate:24e3})}},createSink:r=>{let t={onframe:null},n=new e.nonstandard.RTCAudioSink(r);return n.ondata=s=>{t.onframe?.(s)},t}}}m();import{WtControlDecoder as U,consumeWtDataStreams as _,encodeWtControlFrame as x,STREAM_END as D}from"@urun-sh/core";var p="rt-video-out",P="urun.videoOut.v1",g="h264";function k(e,r){if(!Number.isInteger(e.seq)||e.seq<0)throw new Error(`${r}: header seq must be a non-negative integer (got ${e.seq})`);if(!Number.isInteger(e.ptsUs))throw new Error(`${r}: header ptsUs must be an integer microsecond timestamp (got ${e.ptsUs})`);if(typeof e.codec!="string"||e.codec.length===0)throw new Error(`${r}: header codec must be a non-empty string`);if(typeof e.key!="boolean")throw new Error(`${r}: header key must be a boolean`)}function J(e,r){if(k(e,"encodeVideoOutRecord"),r.byteLength===0)throw new Error("encodeVideoOutRecord: refusing to encode an empty access unit");let t=new TextEncoder().encode(JSON.stringify({v:1,seq:e.seq,ptsUs:e.ptsUs,codec:e.codec,key:e.key}));if(t.byteLength>65535)throw new Error(`encodeVideoOutRecord: header JSON is ${t.byteLength} bytes \u2014 over the u16 length prefix`);let n=new Uint8Array(2+t.byteLength+r.byteLength);return n[0]=t.byteLength&255,n[1]=t.byteLength>>8,n.set(t,2),n.set(r,2+t.byteLength),n}function O(e){if(e.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${e.byteLength} byte(s), no header length)`);let r=e[0]|e[1]<<8;if(e.byteLength<2+r)throw new Error(`decodeVideoOutRecord: truncated record (header says ${r} bytes, ${e.byteLength-2} present)`);let t;try{t=JSON.parse(new TextDecoder().decode(e.subarray(2,2+r)))}catch(c){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(c)})`)}let n=t;if(n===null||typeof n!="object"||n.v!==1)throw new Error(`decodeVideoOutRecord: header is not spec v1 (v=${JSON.stringify(n?.v)}) \u2014 a newer producer must be met with a newer consumer, never silently misread`);let s=n;k(s,"decodeVideoOutRecord");let a=e.subarray(2+r);if(a.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:s,au:a.slice()}}async function Y(e){let r=e.wtDataEndpoint;if(typeof r!="function")throw new Error("negotiateSessionVideoOutQuic: the core session exposes no wtDataEndpoint() \u2014 the \xA75-over-QUIC dial seam (urun-infra#1608 TransportOfferResponse.webtransport, offered with wtData: true) is required for the video OUT lane; there is no WS fallback");let t=await r.call(e),n=t.sessionId??e.sessionId;if(typeof n!="string"||n.length===0)throw new Error("negotiateSessionVideoOutQuic: no session id \u2014 the WT hello requires one and neither the negotiated endpoint nor the core session carries it");return{transport:"webtransport",url:t.url,certHashes:t.certHashes,token:t.token,sessionId:n,stream:p,codec:g,framing:P}}var G="urun.videoOut.v2-ws";function Q(e){let r=e.stream(p);if(typeof r.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${p}') has no messages() \u2014 the named-DATA stream consume seam (@urun-sh/core SessionStream.messages, contract \xA75) is required for the video OUT lane`);let t=new Set,n=new Set,s=new Set,a=o=>{for(let i of n)i(o)},c=!1,u=async()=>{for await(let o of r.messages()){if(!(o instanceof Uint8Array)){a(new Error(`openSessionVideoOutLane: "${p}" delivered a non-BINARY \xA75 payload (${typeof o}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let i;try{i=O(o)}catch(d){a(d instanceof Error?d:new Error(String(d)));continue}for(let d of t)d(i)}for(let o of s)o()};return{codec:g,onOutputFrame:o=>(t.add(o),c||(c=!0,u().catch(i=>a(i instanceof Error?i:new Error(String(i))))),()=>t.delete(o)),onLaneError:o=>(n.add(o),()=>n.delete(o)),onEnd:o=>(s.add(o),()=>s.delete(o))}}function B(e){if(e.length%2!==0||/[^0-9a-fA-F]/.test(e))throw new Error(`consumeVideoOut: certHashes entry is not hex sha-256 (${JSON.stringify(e)})`);let r=new Uint8Array(e.length/2);for(let t=0;t<r.length;t++)r[t]=parseInt(e.slice(t*2,t*2+2),16);return r}async function j(e,r,t){let n=t??(typeof globalThis.WebTransport=="function"?(i,d)=>new globalThis.WebTransport(i,d):void 0);if(!n)throw new Error("consumeVideoOut: no WebTransport implementation \u2014 this platform has no globalThis.WebTransport; inject a connect factory (the third argument)");let s=n(e.url,{serverCertificateHashes:e.certHashes.map(i=>({algorithm:"sha-256",value:B(i)}))});await s.ready;let a=await s.createBidirectionalStream();await a.writable.getWriter().write(x({t:"hello",token:e.token,sessionId:e.sessionId}));let c=a.readable.getReader(),u=new U,o=!1;for(;!o;){let{value:i,done:d}=await c.read();if(d)throw new Error("consumeVideoOut: gateway closed the control stream before hello-ack");if(i)for(let f of u.push(i)){if(f.t==="hello-ack"){o=!0;break}throw new Error(`consumeVideoOut: gateway rejected hello (got ${JSON.stringify(f.t)} before hello-ack)`)}}await _(s.incomingUnidirectionalStreams,{onData:(i,d)=>{if(i===e.stream){if(d===D){r.onEnd?.();return}if(!(d instanceof Uint8Array)){r.onLaneError(new Error(`consumeVideoOut: "${e.stream}" delivered a non-BINARY \xA75 payload (${typeof d}) \u2014 spec v1 records ride BINARY envelopes only`));return}try{r.onFrame(O(d))}catch(f){r.onLaneError(f instanceof Error?f:new Error(String(f)))}}},onLaneError:i=>r.onLaneError(i)})}export{W as a,w as b,H as c,h as d,y as e,N as f,$ as g,M as h,p as i,P as j,g as k,J as l,O as m,Y as n,G as o,Q as p,j as q};
@@ -1 +1 @@
1
- import{c as n}from"./chunk-5CCJUNH7.js";n();async function o(e){let t=await(e.fetchImpl??fetch)(`${e.catalogUrl}/model_catalog?select=model_id,variant,gpu_spec,engine,task,description,size_gb,hf_repo,engine_args,updated_at`,{headers:{apikey:e.anonKey,Authorization:`Bearer ${e.anonKey}`}});if(!t.ok)throw new Error(`model_catalog fetch failed: ${t.status}`);return await t.json()}async function l(e){return{object:"list",data:(await o(e)).map(t=>({id:`${t.model_id}:${t.variant}`,object:"model",created:0,owned_by:"urun"}))}}export{o as a,l as b};
1
+ import{c as n}from"./chunk-YSFSRI3D.js";n();async function o(e){let t=await(e.fetchImpl??fetch)(`${e.catalogUrl}/model_catalog?select=model_id,variant,gpu_spec,engine,task,description,size_gb,hf_repo,engine_args,updated_at`,{headers:{apikey:e.anonKey,Authorization:`Bearer ${e.anonKey}`}});if(!t.ok)throw new Error(`model_catalog fetch failed: ${t.status}`);return await t.json()}async function l(e){return{object:"list",data:(await o(e)).map(t=>({id:`${t.model_id}:${t.variant}`,object:"model",created:0,owned_by:"urun"}))}}export{o as a,l as b};
@@ -0,0 +1 @@
1
+ import{c as a}from"./chunk-YSFSRI3D.js";a();function _(e,t,o,n){let r=e.response??{},i={request_id:o,consumer_id:n,stream:!0,kind:"chat",messages:t};return typeof r.instructions=="string"&&(i.instructions=r.instructions),Array.isArray(r.modalities)&&(i.modalities=r.modalities),typeof r.temperature=="number"&&(i.temperature=r.temperature),typeof r.max_output_tokens=="number"&&(i.max_output_tokens=r.max_output_tokens),r.tools!==void 0&&(i.tools=r.tools),i}function f(e,t,o){let n={request_id:t,consumer_id:o,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),typeof e.instructions=="string"&&(n.instructions=e.instructions),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),typeof e.voice=="string"&&(n.voice=e.voice),n}a();function y(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,o=t.body??{},n=o.error,r=n?.message??o.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:r}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}a();a();var w="llm-resp";async function*m(e,t){let o=`${w}:${t}`,n=`resp_${t}`;yield{type:"response.created",response:{id:n,status:"in_progress"}};let r=new Map;try{for await(let i of e.stream(o).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 d=typeof u.index=="number"?u.index:0,c=r.get(d)??{};u.id&&(c.id=u.id),u.function?.name&&(c.name=u.function.name),r.set(d,c),yield{type:"response.function_call_arguments.delta",item_id:`fc_${t}_${d}`,tool_index:d,call_id:c.id,name:c.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 y(s);return}}}finally{try{e.doc("llm").set({requests:{[t]:null}})}catch{}}}a();var k=class e{constructor(t,o=e.DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS){this.session=t;this.docSyncTimeoutMs=o;this.sessionTag=t.sessionId??globalThis.crypto.randomUUID()}session;docSyncTimeoutMs;sessionTag;static DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS=15e3;get consumerId(){return this.session.consumerId}async ensureDocSynced(){let t=this.session.doc("llm");if(t.synced===!0||typeof t.onSynced!="function")return;let o;await new Promise((r,i)=>{let s=t.onSynced(()=>{clearTimeout(o),r()});o=setTimeout(()=>{s(),i(new Error(`[urun] llm doc plane never synced within ${this.docSyncTimeoutMs}ms \u2014 the request envelope write is withheld rather than raced into a possibly-lost pre-sync write (resumed-attach first-write-lost, prod-usw2 2026-09-23)`))},this.docSyncTimeoutMs)})}write(t){t.session_tag=this.sessionTag,this.session.doc("llm").set({requests:{[t.request_id]:{payload:t,consumer_id:t.consumer_id,stream:t.stream}}})}sendResponses(t,o){return this.writeGated(o,()=>{let n=f(t,o,this.consumerId);return this.write(n),m(this.session,o)})}sendResponseCreate(t,o,n){return this.writeGated(n,()=>{let r=_(t,o,n,this.consumerId);return this.write(r),m(this.session,n)})}async writeGated(t,o){try{await this.ensureDocSynced()}catch(n){throw new Error(`[urun] responses dial ${t} aborted before its envelope write: ${n instanceof Error?n.message:String(n)}`)}return o()}};export{_ as a,f as b,y as c,m as d,k as e};
@@ -0,0 +1 @@
1
+ import{a as C}from"./chunk-K23AZPI4.js";import{a as L}from"./chunk-YXK42SKC.js";import{a as x,b as I,e as v,f as G,g as D,h as U,j as M}from"./chunk-WCMX5VOF.js";import{c as E,f as k,g as R,p as F}from"./chunk-6M5JK4YC.js";import{c as w}from"./chunk-YSFSRI3D.js";w();import{App as H,createClientToken as Y}from"@urun-sh/core";w();import{SessionFailedError as W}from"@urun-sh/core";var b=2;function q(e){let s=e?.type;return s==="response.created"||s==="response.in_progress"}function K(e){let s=e?.type;return s==="response.completed"||s==="error"}function $(e){return async function(i,p){return(async function*(){for(let r=1;r<=b;r++){let{app:d,entry:c,identity:l}=await e.sessionFor(i.model);p?.({sessionId:e.sessionIdOf?.(c)??null,identity:l});let h=!1,a=!1;try{let n=await e.createStream(c,i);for await(let o of n){if(K(o)&&(h=!0),q(o)){if(r>1)continue}else a=!0;yield o}}catch(n){if(n instanceof W&&(n.kind==="never-live"||n.kind==="create-failed")){try{e.onAdmissionVerdict?.(d,c,n)}catch{}throw n}if(r<b&&!a){await e.evict(d,c);continue}throw n}if(h)return;if(r<b&&!a){await e.evict(d,c);continue}return}})()}}w();var J=3,Q="llm",X=new Set(["paused","error"]);function j(e,s){let i=e.doc(Q),p=typeof i.onConnectionState=="function",u=typeof e.onPhase=="function";if(!p&&!u)throw new Error(`session for app "${s}" exposes neither a doc connection-state surface nor onPhase \u2014 session-gone detection requires @urun-sh/core Session (SessionDocument.onConnectionState / Session.onPhase); pooling a session with no native lifecycle signal would silently swallow requests dispatched after the platform closes it`);let r=null,d=!1,c=new Set,l=[],h=a=>{if(!r){r=a;for(let n of[...c])n(a);c.clear()}};return p&&l.push(i.onConnectionState(a=>{d&&(a.consecutiveFailures<J||h(new v(`the pooled session for app "${s}" is gone: its doc backhaul failed ${a.consecutiveFailures} consecutive times (last close ${a.lastCloseCode===null?"by provider liveness reap":`code ${a.lastCloseCode}`}${a.lastCloseReason?`, reason ${JSON.stringify(a.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`)))})),u&&l.push(e.onPhase(a=>{if(a.name==="live"){d=!0;return}d&&X.has(a.name)&&h(new v(`the pooled session for app "${s}" lost its compute binding (phase "${a.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:()=>r,onGone(a){return r?(a(r),()=>{}):(c.add(a),()=>c.delete(a))},dispose(){c.clear();for(let a of l.splice(0))a()}}}async function*A(e,s){let i=s.gone();if(i)throw i;let p=e[Symbol.asyncIterator](),u,r=new Promise((d,c)=>{u=s.onGone(l=>c(l))});r.catch(()=>{});try{for(;;){let d=await Promise.race([p.next(),r]);if(d.done)return;yield d.value}}finally{u?.(),p.return?.(void 0)}}function V(e,s,i,p){let u=e[i];if(typeof u!="function")throw new Error(`app "${s}" has no function "${i}" (set URUN_FUNCTION)`);let r=p?u(void 0,p):u();if(typeof r.end!="function")throw new Error(`app "${s}" function "${i}" returned a session without end()`);return r}function z(e,s,i){let{baseUrl:p,orgId:u,auth:r}=e,d=typeof e.subject=="function"?e.subject():e.subject,c=i.startsWith(D)&&d!==void 0?`${d}:speech`:d,l=s.fnName??e.fnName,h=e.sessionStartOptions?.(i),a=s.sharedApp?{sharedApp:!0,...h}:h,n=m=>({session:m,responses:new L(m),gone:j(m,i)});if(r.lane==="jwt"){let m=H(s.appSlug,{baseUrl:p,orgId:u,jwt:r.jwt});return n(V(m,s.appSlug,l,a))}let{apiKey:o,gatewayUrl:t}=r,f=H(s.appSlug,{baseUrl:p,orgId:u,getAccessToken:async()=>(await Y(o,{baseUrl:t,expiresIn:300,allowedFunctions:[`${s.appSlug}/${l}`],...c===void 0?{}:{subject:c},...s.sharedApp?{sharedApp:!0}:{}})).token});return n(V(f,s.appSlug,l,a))}function Z(e){let s=e.session.id;if(typeof s!="string"||s.length===0)throw new Error("pooled session exposes no stable session id \u2014 the session-identity seam requires @urun-sh/core Session (session.id)");return s}function ee(e){if(e.kind==="never-live")return!0;if(e.kind!=="create-failed")return!1;let s=e.status.error?.httpStatus,i=e.status.error?.code;return s===410||s===409&&(i==="cancelled"||i==="expired")?!0:i==="client_ended"}var ne=new Set(["error","paused"]);function ge(e){let{auth:s,catalog:i}=e,p=null,u=(d,c)=>{let l=z(e,d,c);return e.onSessionOpen&&typeof l.session.id=="string"&&l.session.id!==""&&e.onSessionOpen(c,l.session.id),l.gone.onGone(()=>{p?.evict(c,l)}),l},r=new M({sharedOnlyListing:e.sharedOnlyListing===!0,defaultApp:e.defaultApp,fnName:e.fnName,openSession:u,closeSession:async d=>{if(d.gone.dispose(),e.release==="detach"){if(typeof d.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');d.session.detach();return}await d.session.end()},listApps:s.lane==="api-key"?()=>G({apiUrl:e.apiUrl,apiKey:s.apiKey}):null,listCatalog:i?()=>C(i):null,listPrices:i?()=>I(i):null,shared:s.lane==="api-key"?()=>x({apiUrl:e.apiUrl,apiKey:s.apiKey}):null,sessionKey:Z});return p=r,e.defaultApp!==null&&e.statelessDirect!==!0&&r.seed(e.defaultApp,u({appSlug:e.defaultApp},U({appSlug:e.defaultApp}))),r}async function P(e,s){let i=e.whenLive;if(typeof i!="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 i.call(e,s===void 0?void 0:{signal:s})}async function N(e){if(typeof e.cancel=="function")try{await e.cancel();return}catch{}if(typeof e.detach!="function")throw new Error("pooled session exposes neither cancel() nor detach() \u2014 the client-gone abandon path requires the @urun-sh/core Session native lifecycle surface");e.detach()}function B(e){return e.reason instanceof Error?e.reason:new Error("the client socket closed")}function ve(e,s=R,i,p){let u=new WeakMap,r=new WeakMap,d=new WeakMap,c=new WeakMap,l=n=>{let o=n.session.id;return typeof o=="string"&&o.length>0?o:null},h=(n,o)=>{let t=u.get(n);return t||(t=(async()=>{await P(n.session,o);let f=await E(n.session,await s());return Object.assign(f,{sessionId:n.session.id})})(),u.set(n,t),t.catch(()=>u.delete(n))),t},a=n=>{let o=c.get(n);return o||(o=(async()=>{let t=await h(n);return{appendInputAudio:f=>t.appendInputAudio(f),onOutputAudio:f=>t.onOutputAudio(f),sessionId:l(n)}})(),c.set(n,o),o.catch(()=>c.delete(n))),o};return{createResponse:$({sessionFor:n=>e.sessionFor(n),sessionIdOf:n=>l(n),evict:(n,o)=>e.evict(n,o),onAdmissionVerdict:(n,o,t)=>{ee(t)&&(e.evict(n,o),i?.(n,t.kind==="never-live"?"never-live":"create-failed"))},createStream:async(n,o)=>(await P(n.session),A(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),supportsSpeech:n=>e.supportsSpeech(n),speechVoices:n=>e.speechVoices(n),openAudio:async(n,o)=>{let{entry:t}=await e.sessionFor(n);if(o?.aborted)throw await N(t.session),B(o);let f=h(t,o);return o===void 0?f:new Promise((m,S)=>{let y=()=>{N(t.session).catch(()=>{}),S(B(o))};o.addEventListener("abort",y,{once:!0});let O=()=>o.removeEventListener("abort",y);f.then(g=>{O(),m(g)},g=>{O(),S(g)})})},openAudioOn:async n=>a((await e.sessionForHandle(n)).entry),openVideo:async n=>{let{entry:o}=await e.sessionFor(n),t=r.get(o);return t||(t=k(o.session),r.set(o,t)),t},openVideoOut:async n=>{let{entry:o}=await e.sessionFor(n),t=d.get(o);return t||(t=F(o.session),d.set(o,t)),t},sessionHandle:async n=>(await e.handleFor(n)).handle,speechSessionHandle:async n=>(await e.speechHandleFor(n)).handle,evictSpeechSession:async n=>{let{app:o,entry:t}=await e.sessionForHandle(n);await e.evict(o,t)},createResponseOn:async(n,o)=>{let{entry:t}=await e.sessionForHandle(n);return await P(t.session),A(await t.responses.responses.create(o),t.gone)},onSessionEnd:async(n,o)=>{let{entry:t}=await e.sessionForHandle(n),f=t.session;if(typeof f.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let m=!1;return f.onPhase(S=>{if(m||!ne.has(S.name))return;m=!0;let y=f.endsAt??null;o({timeLeftMs:y?Math.max(0,y.getTime()-Date.now()):null,reason:S.reason??`session ${S.name}`})})},...p?{stateless:p}:{}}}export{ge as a,ve as b};