@urun-sh/openai 0.3.0 → 0.3.2

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 (48) hide show
  1. package/dist/ResponsesClient-BYx3YLGo.d.ts +26 -0
  2. package/dist/ResponsesClient-Dft3bg3b.d.cts +26 -0
  3. package/dist/chunk-5NXM4IO3.js +2 -0
  4. package/dist/{chunk-3ED25N7E.js → chunk-AJYWEXQZ.js} +1 -1
  5. package/dist/chunk-CWJRDBDC.js +1 -0
  6. package/dist/chunk-FVFF5EWK.js +6 -0
  7. package/dist/chunk-G3NMGP4N.js +1 -0
  8. package/dist/chunk-M6ICU4F5.js +40 -0
  9. package/dist/chunk-UVAY7Q7Z.js +1 -0
  10. package/dist/gemini-live.cjs +2 -0
  11. package/dist/gemini-live.d.cts +120 -0
  12. package/dist/gemini-live.d.ts +79 -0
  13. package/dist/gemini-live.js +1 -0
  14. package/dist/hosted/bin.cjs +18 -18
  15. package/dist/hosted/bin.js +1 -1
  16. package/dist/hosted/index.cjs +17 -17
  17. package/dist/hosted/index.d.cts +4 -3
  18. package/dist/hosted/index.d.ts +4 -3
  19. package/dist/hosted/index.js +1 -1
  20. package/dist/index.cjs +1 -1
  21. package/dist/index.d.cts +4 -4
  22. package/dist/index.d.ts +4 -4
  23. package/dist/index.js +1 -1
  24. package/dist/pi-extension/index.d.cts +2 -1
  25. package/dist/pi-extension/index.d.ts +2 -1
  26. package/dist/pi-extension/index.js +1 -1
  27. package/dist/pi-extension/standalone.cjs +53 -53
  28. package/dist/proxy/cli.cjs +37 -37
  29. package/dist/proxy/cli.js +1 -1
  30. package/dist/proxy/index.cjs +21 -21
  31. package/dist/proxy/index.d.cts +4 -31
  32. package/dist/proxy/index.d.ts +4 -6
  33. package/dist/proxy/index.js +1 -1
  34. package/dist/{server-DiQUIDDg.d.cts → server-BfME37pQ.d.cts} +20 -2
  35. package/dist/{server-BBKNv7hV.d.ts → server-l1himPxc.d.ts} +6 -2
  36. package/dist/translator-C9uPKypK.d.ts +130 -0
  37. package/dist/translator-CcDBEfvm.d.cts +227 -0
  38. package/dist/{ResponsesClient-y8g6OfNN.d.cts → types-lsVTbNcH.d.cts} +16 -24
  39. package/dist/{ResponsesClient-y8g6OfNN.d.ts → types-lsVTbNcH.d.ts} +8 -24
  40. package/dist/video-out-CWesbk12.d.ts +155 -0
  41. package/dist/video-out-D20UuJ8G.d.cts +298 -0
  42. package/package.json +12 -2
  43. package/dist/chunk-35LB5OHI.js +0 -1
  44. package/dist/chunk-EFBD3CGZ.js +0 -1
  45. package/dist/chunk-VG3X2LVG.js +0 -41
  46. package/dist/chunk-XHRPJIEK.js +0 -6
  47. package/dist/media-DCHTX3Ez.d.cts +0 -68
  48. package/dist/media-DCHTX3Ez.d.ts +0 -42
@@ -0,0 +1,26 @@
1
+ import { b as UrunSessionLike } from './types-lsVTbNcH.js';
2
+
3
+ interface ResponsesStream extends AsyncIterable<unknown> {
4
+ readonly requestId: string;
5
+ }
6
+ declare class UrunResponses {
7
+ private readonly transport;
8
+ constructor(session: UrunSessionLike);
9
+ readonly responses: {
10
+ create: (params: {
11
+ model?: string;
12
+ input: unknown;
13
+ stream?: boolean;
14
+ tools?: unknown;
15
+ tool_choice?: unknown;
16
+ temperature?: number;
17
+ max_output_tokens?: number;
18
+ top_p?: number;
19
+ stop?: string[];
20
+ reasoning_effort?: string;
21
+ chat_template_kwargs?: Record<string, unknown>;
22
+ }) => Promise<ResponsesStream>;
23
+ };
24
+ }
25
+
26
+ export { UrunResponses as U };
@@ -0,0 +1,26 @@
1
+ import { b as UrunSessionLike } from './types-lsVTbNcH.cjs';
2
+
3
+ interface ResponsesStream extends AsyncIterable<unknown> {
4
+ readonly requestId: string;
5
+ }
6
+ declare class UrunResponses {
7
+ private readonly transport;
8
+ constructor(session: UrunSessionLike);
9
+ readonly responses: {
10
+ create: (params: {
11
+ model?: string;
12
+ input: unknown;
13
+ stream?: boolean;
14
+ tools?: unknown;
15
+ tool_choice?: unknown;
16
+ temperature?: number;
17
+ max_output_tokens?: number;
18
+ top_p?: number;
19
+ stop?: string[];
20
+ reasoning_effort?: string;
21
+ chat_template_kwargs?: Record<string, unknown>;
22
+ }) => Promise<ResponsesStream>;
23
+ };
24
+ }
25
+
26
+ export { UrunResponses as U };
@@ -0,0 +1,2 @@
1
+ import{a as I,o as A}from"./chunk-CWJRDBDC.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
+ `)}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 rt-audio-out 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};
@@ -1 +1 @@
1
- import{a as _,b as x,m as A,n as g}from"./chunk-XHRPJIEK.js";import{b as v}from"./chunk-VG3X2LVG.js";import{c as i}from"./chunk-YSFSRI3D.js";i();import{createClientToken as E,ClientTokenError as I}from"@urun-sh/core";var l=class extends Error{constructor(t,n=401){super(t);this.status=n;this.name="ProxyAuthError"}status},p=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function C(r){let e=(r??"").trim();if(e==="")throw new l("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let t=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!t)throw new l("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return t[1].trim()}var S=60,N=6e4,O=4096;function k(r){return r===401||r===403}var y=class{constructor(e){this.opts=e;this.mint=e.mint??E,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new l("empty Authorization bearer token");let t=this.cache.get(e);if(t&&this.now()-t.at<N)return{apiKey:e,orgId:t.orgId};let n;try{n=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:S})).orgId}catch(o){throw o instanceof I&&k(o.status)?(this.cache.delete(e),new l("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new p(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${o instanceof Error?o.message:String(o)}`)}if(!n)throw new p("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>=O){let o=this.cache.keys().next();o.done||this.cache.delete(o.value)}return this.cache.set(e,{orgId:n,at:this.now()}),{apiKey:e,orgId:n}}clear(){this.cache.clear()}};i();var $=8080,T="serve",X="0.0.0.0";function L(r,e,t){let n=(r[e]??"").trim();if(!n)throw new Error(`${e} is not set \u2014 ${t}`);return n}function W(r=process.env){let e=(r.PORT??"").trim(),t=e===""?$:Number(e);if(!Number.isInteger(t)||t<0||t>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let n=A(g(L(r,"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")),o=(r.URUN_API_URL??"").trim(),d=o?g(o,"URUN_API_URL"):`${n.replace(/\/+$/,"")}/v1`,a=(r.URUN_CATALOG_URL??"").trim(),m=(r.URUN_CATALOG_ANON_KEY??"").trim();if(!!a!=!!m)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 s=a?{catalogUrl:g(a,"URUN_CATALOG_URL"),anonKey:m}:null;return{port:t,baseUrl:n,apiUrl:d,catalog:s}}i();import{createHash as H}from"crypto";var z=512;function M(r){return`urun-inference-proxy-${H("sha256").update(r).digest("hex").slice(0,32)}`}var f=class{constructor(e){this.opts=e}opts;tenants=new Map;build(e){if(this.opts.build)return this.opts.build(e);let t=_({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:T,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:M(e.apiKey)});return{router:t,clients:x(t)}}clientsFor(e){let t=this.tenants.get(e.apiKey);if(t)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,t),t.clients;for(;this.tenants.size>=z;){let o=this.tenants.entries().next();if(o.done)break;let[d,a]=o.value;this.tenants.delete(d),a.router.closeAll().catch(()=>{})}let n=this.build(e);return this.tenants.set(e.apiKey,n),n.clients}get size(){return this.tenants.size}async closeAll(){let e=[...this.tenants.values()].map(n=>n.router);this.tenants.clear();let t=[];if(await Promise.all(e.map(async n=>{try{await n.closeAll()}catch(o){t.push(String(o instanceof Error?o.message:o))}})),t.length>0)throw new Error(`failed to close ${t.length} tenant backhaul(s): ${t.join("; ")}`)}};i();import{createServer as G}from"http";var B=5e3,F=3e3,K="/api/client-tokens";function U(r,e,t){r.writeHead(e,{"content-type":"application/json"}),r.end(JSON.stringify(t))}function R(r,e,t,n){U(r,e,{error:{message:t,type:n}})}async function D(r,e=fetch){let t=`${r.replace(/\/+$/,"")}${K}`;try{let n=await e(t,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(F)});return n.status===404?{ready:!1,detail:`${t} answered 404 \u2014 this is not a uRun control plane`}:n.status>=500?{ready:!1,detail:`${t} answered HTTP ${n.status}`}:{ready:!0,detail:`${t} answered HTTP ${n.status}`}}catch(n){return{ready:!1,detail:`${t} unreachable: ${n instanceof Error?n.message:String(n)}`}}}function le(r){let e=r.resolver??new y({gatewayUrl:r.baseUrl}),t=r.registry??new f({baseUrl:r.baseUrl,apiUrl:r.apiUrl,catalog:r.catalog}),n=r.probeControlPlane??(()=>D(r.baseUrl)),o=async s=>e.resolve(C(s.headers.authorization)),d=v({clients:async s=>t.clientsFor(await o(s)),statusOf:s=>s instanceof l?{status:s.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:s instanceof p?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null}),a=null;return{server:G((s,c)=>{let h=new URL(s.url??"/","http://localhost");(async()=>{if(s.method==="GET"&&h.pathname==="/healthz"){U(c,200,{ok:!0});return}if(s.method==="GET"&&h.pathname==="/readyz"){let u=Date.now();if(!a||u-a.at>=B){let P=await n();a={at:u,...P}}U(c,a.ready?200:503,{ok:a.ready,control_plane:a.detail});return}if(h.pathname!=="/v1"&&!h.pathname.startsWith("/v1/")){R(c,404,`no route for ${s.method} ${h.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface plus /healthz and /readyz`,"invalid_request_error");return}d(s,c)})().catch(u=>{if(!c.headersSent){R(c,500,String(u instanceof Error?u.message:u),"proxy_error");return}c.end()})}),closeAll:()=>t.closeAll()}}async function ce(r,e){let t=new Promise(n=>r.close(()=>n()));r.closeAllConnections(),await t,await e()}export{l as a,p as b,C as c,N as d,O as e,y as f,$ as g,T as h,X as i,W as j,z as k,M as l,f as m,B as n,F as o,le as p,ce as q};
1
+ import{a as _,b as x,m as A,n as g}from"./chunk-FVFF5EWK.js";import{a as v}from"./chunk-M6ICU4F5.js";import{c as i}from"./chunk-YSFSRI3D.js";i();import{createClientToken as E,ClientTokenError as I}from"@urun-sh/core";var l=class extends Error{constructor(t,n=401){super(t);this.status=n;this.name="ProxyAuthError"}status},p=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function C(r){let e=(r??"").trim();if(e==="")throw new l("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let t=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!t)throw new l("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return t[1].trim()}var S=60,N=6e4,O=4096;function k(r){return r===401||r===403}var y=class{constructor(e){this.opts=e;this.mint=e.mint??E,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new l("empty Authorization bearer token");let t=this.cache.get(e);if(t&&this.now()-t.at<N)return{apiKey:e,orgId:t.orgId};let n;try{n=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:S})).orgId}catch(o){throw o instanceof I&&k(o.status)?(this.cache.delete(e),new l("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new p(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${o instanceof Error?o.message:String(o)}`)}if(!n)throw new p("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>=O){let o=this.cache.keys().next();o.done||this.cache.delete(o.value)}return this.cache.set(e,{orgId:n,at:this.now()}),{apiKey:e,orgId:n}}clear(){this.cache.clear()}};i();var $=8080,T="serve",X="0.0.0.0";function L(r,e,t){let n=(r[e]??"").trim();if(!n)throw new Error(`${e} is not set \u2014 ${t}`);return n}function W(r=process.env){let e=(r.PORT??"").trim(),t=e===""?$:Number(e);if(!Number.isInteger(t)||t<0||t>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let n=A(g(L(r,"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")),o=(r.URUN_API_URL??"").trim(),d=o?g(o,"URUN_API_URL"):`${n.replace(/\/+$/,"")}/v1`,a=(r.URUN_CATALOG_URL??"").trim(),m=(r.URUN_CATALOG_ANON_KEY??"").trim();if(!!a!=!!m)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 s=a?{catalogUrl:g(a,"URUN_CATALOG_URL"),anonKey:m}:null;return{port:t,baseUrl:n,apiUrl:d,catalog:s}}i();import{createHash as H}from"crypto";var z=512;function M(r){return`urun-inference-proxy-${H("sha256").update(r).digest("hex").slice(0,32)}`}var f=class{constructor(e){this.opts=e}opts;tenants=new Map;build(e){if(this.opts.build)return this.opts.build(e);let t=_({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:T,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:M(e.apiKey)});return{router:t,clients:x(t)}}clientsFor(e){let t=this.tenants.get(e.apiKey);if(t)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,t),t.clients;for(;this.tenants.size>=z;){let o=this.tenants.entries().next();if(o.done)break;let[d,a]=o.value;this.tenants.delete(d),a.router.closeAll().catch(()=>{})}let n=this.build(e);return this.tenants.set(e.apiKey,n),n.clients}get size(){return this.tenants.size}async closeAll(){let e=[...this.tenants.values()].map(n=>n.router);this.tenants.clear();let t=[];if(await Promise.all(e.map(async n=>{try{await n.closeAll()}catch(o){t.push(String(o instanceof Error?o.message:o))}})),t.length>0)throw new Error(`failed to close ${t.length} tenant backhaul(s): ${t.join("; ")}`)}};i();import{createServer as G}from"http";var B=5e3,F=3e3,K="/api/client-tokens";function U(r,e,t){r.writeHead(e,{"content-type":"application/json"}),r.end(JSON.stringify(t))}function R(r,e,t,n){U(r,e,{error:{message:t,type:n}})}async function D(r,e=fetch){let t=`${r.replace(/\/+$/,"")}${K}`;try{let n=await e(t,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(F)});return n.status===404?{ready:!1,detail:`${t} answered 404 \u2014 this is not a uRun control plane`}:n.status>=500?{ready:!1,detail:`${t} answered HTTP ${n.status}`}:{ready:!0,detail:`${t} answered HTTP ${n.status}`}}catch(n){return{ready:!1,detail:`${t} unreachable: ${n instanceof Error?n.message:String(n)}`}}}function le(r){let e=r.resolver??new y({gatewayUrl:r.baseUrl}),t=r.registry??new f({baseUrl:r.baseUrl,apiUrl:r.apiUrl,catalog:r.catalog}),n=r.probeControlPlane??(()=>D(r.baseUrl)),o=async s=>e.resolve(C(s.headers.authorization)),d=v({clients:async s=>t.clientsFor(await o(s)),statusOf:s=>s instanceof l?{status:s.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:s instanceof p?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null}),a=null;return{server:G((s,c)=>{let h=new URL(s.url??"/","http://localhost");(async()=>{if(s.method==="GET"&&h.pathname==="/healthz"){U(c,200,{ok:!0});return}if(s.method==="GET"&&h.pathname==="/readyz"){let u=Date.now();if(!a||u-a.at>=B){let P=await n();a={at:u,...P}}U(c,a.ready?200:503,{ok:a.ready,control_plane:a.detail});return}if(h.pathname!=="/v1"&&!h.pathname.startsWith("/v1/")){R(c,404,`no route for ${s.method} ${h.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface plus /healthz and /readyz`,"invalid_request_error");return}d(s,c)})().catch(u=>{if(!c.headersSent){R(c,500,String(u instanceof Error?u.message:u),"proxy_error");return}c.end()})}),closeAll:()=>t.closeAll()}}async function ce(r,e){let t=new Promise(n=>r.close(()=>n()));r.closeAllConnections(),await t,await e()}export{l as a,p as b,C as c,N as d,O as e,y as f,$ as g,T as h,X as i,W as j,z as k,M as l,f as m,B as n,F as o,le as p,ce as q};
@@ -0,0 +1 @@
1
+ import{c as m}from"./chunk-YSFSRI3D.js";m();var B=24e3,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),a=new Int16Array(n);for(let s=0;s<n;s++)a[s]=t.readInt16LE(s*2);this.source.onData(a)}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("rt-audio-out").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 a of this.outHandlers)a(n)}}onOutputAudio(r){return this.outHandlers.add(r),()=>this.outHandlers.delete(r)}};async function W(e,r,t=1e4){let n=new w(r,{sampleRate:24e3}),a=await n.startOutbound();await e.stream("rt-audio-in").attach(a);let s=await R(e,t);return await n.startInbound(s),n}function R(e,r){let t=e.stream("rt-audio-out");return t.track!=null?Promise.resolve(t.track):new Promise((n,a)=>{let s,c=setTimeout(()=>{s?.(),a(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),s?.(),n(o))};s=t.on?.("track",u),t.track!=null&&u(t.track)})}var h="rt-video-in",y=256e3;function H(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 V=24e3,l=480,T=111;async function I(){let e=await import("opusscript"),r=e.default??e;return new r(V,1,2048)}async function N(){let e=await import("werift"),r=await I();return{createSource:()=>{let t=new e.MediaStreamTrack({kind:"audio"}),n=Math.random()*4294967295>>>0,a=Math.random()*65535&65535,s=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),O=r.encode(f,l);a=a+1&65535,s=s+l>>>0;let S=new e.RtpHeader({version:2,payloadType:T,sequenceNumber:a,timestamp:s,ssrc:n,marker:!1}),E=new e.RtpPacket(S,O);t.writeRtp(E)}c=o.slice(i)}}},createSink:t=>{let n={onframe:null};return t.onReceiveRtp.subscribe(s=>{let c;try{c=r.decode(s.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 $(){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=a=>{t.onframe?.(a)},t}}}m();import{WtControlDecoder as L,consumeWtDataStreams as U,encodeWtControlFrame as _,STREAM_END as x}from"@urun-sh/core";var p="rt-video-out",P="urun.videoOut.v1",b="h264";function g(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 q(e,r){if(g(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 k(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 a=n;g(a,"decodeVideoOutRecord");let s=e.subarray(2+r);if(s.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:a,au:s.slice()}}async function J(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:b,framing:P}}var Y="urun.videoOut.v2-ws";function G(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,a=new Set,s=o=>{for(let i of n)i(o)},c=!1,u=async()=>{for await(let o of r.messages()){if(!(o instanceof Uint8Array)){s(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=k(o)}catch(d){s(d instanceof Error?d:new Error(String(d)));continue}for(let d of t)d(i)}for(let o of a)o()};return{codec:b,onOutputFrame:o=>(t.add(o),c||(c=!0,u().catch(i=>s(i instanceof Error?i:new Error(String(i))))),()=>t.delete(o)),onLaneError:o=>(n.add(o),()=>n.delete(o)),onEnd:o=>(a.add(o),()=>a.delete(o))}}function D(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 Q(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 a=n(e.url,{serverCertificateHashes:e.certHashes.map(i=>({algorithm:"sha-256",value:D(i)}))});await a.ready;let s=await a.createBidirectionalStream();await s.writable.getWriter().write(_({t:"hello",token:e.token,sessionId:e.sessionId}));let c=s.readable.getReader(),u=new L,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 U(a.incomingUnidirectionalStreams,{onData:(i,d)=>{if(i===e.stream){if(d===x){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(k(d))}catch(f){r.onLaneError(f instanceof Error?f:new Error(String(f)))}}},onLaneError:i=>r.onLaneError(i)})}export{B as a,w as b,W as c,h as d,y as e,H as f,N as g,$ as h,p as i,P as j,b as k,q as l,k as m,J as n,Y as o,G as p,Q as q};
@@ -0,0 +1,6 @@
1
+ import{a as b}from"./chunk-F2TEK34X.js";import{a as v}from"./chunk-G3NMGP4N.js";import{a as P,c as h,d as U,e as _}from"./chunk-XHIIEA6Z.js";import{c as x,f as S,g as E,p as R}from"./chunk-CWJRDBDC.js";import{c as l}from"./chunk-YSFSRI3D.js";l();import{App as I,createClientToken as H}from"@urun-sh/core";l();function j(e){let n=e?.type;return n==="response.created"||n==="response.in_progress"}function D(e){let n=e?.type;return n==="response.completed"||n==="error"}function k(e){return async function(t){return(async function*(){for(let a=1;a<=2;a++){let{app:s,entry:o}=await e.sessionFor(t.model),i=!1,u=!1;try{let c=await e.createStream(o,t);for await(let d of c){if(D(d)&&(i=!0),j(d)){if(a>1)continue}else u=!0;yield d}}catch(c){if(a<2&&!u){await e.evict(s,o);continue}throw c}if(i)return;if(a<2&&!u){await e.evict(s,o);continue}return}})()}}l();var M=3,F="llm",W=new Set(["ended","expired","error"]);function A(e,n){let t=e.doc(F),r=typeof t.onConnectionState=="function",a=typeof e.onPhase=="function";if(!r&&!a)throw new Error(`session for app "${n}" 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 s=null,o=new Set,i=[],u=c=>{if(!s){s=c;for(let d of[...o])d(c);o.clear()}};return r&&i.push(t.onConnectionState(c=>{c.consecutiveFailures<M||u(new h(`the pooled session for app "${n}" is gone: its doc backhaul failed ${c.consecutiveFailures} consecutive times (last close ${c.lastCloseCode===null?"by provider liveness reap":`code ${c.lastCloseCode}`}${c.lastCloseReason?`, reason ${JSON.stringify(c.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`))})),a&&i.push(e.onPhase(c=>{W.has(c.name)&&u(new h(`the pooled session for app "${n}" is gone: it reached terminal phase "${c.name}". The entry is evicted; retry to get a fresh session.`))})),{gone:()=>s,onGone(c){return s?(c(s),()=>{}):(o.add(c),()=>o.delete(c))},dispose(){o.clear();for(let c of i.splice(0))c()}}}async function*g(e,n){let t=n.gone();if(t)throw t;let r=e[Symbol.asyncIterator](),a,s=new Promise((o,i)=>{a=n.onGone(u=>i(u))});s.catch(()=>{});try{for(;;){let o=await Promise.race([r.next(),s]);if(o.done)return;yield o.value}}finally{a?.(),r.return?.(void 0)}}function O(e,n,t){let r=e[t];if(typeof r!="function")throw new Error(`app "${n}" has no function "${t}" (set URUN_FUNCTION)`);let a=r();if(typeof a.end!="function")throw new Error(`app "${n}" function "${t}" returned a session without end()`);return a}function B(e,n){let{baseUrl:t,orgId:r,fnName:a,auth:s,subject:o}=e,i=p=>({session:p,responses:new v(p),gone:A(p,n)});if(s.lane==="jwt"){let p=I(n,{baseUrl:t,orgId:r,jwt:s.jwt});return i(O(p,n,a))}let{apiKey:u,gatewayUrl:c}=s,d=I(n,{baseUrl:t,orgId:r,getAccessToken:async()=>(await H(u,{baseUrl:c,expiresIn:300,allowedFunctions:[`${n}/${a}`],...o===void 0?{}:{subject:o}})).token});return i(O(d,n,a))}function K(e){let n=e.session.id;if(typeof n!="string"||n.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 n}var q=new Set(["error","ended","expired"]);function xe(e){let{auth:n,catalog:t}=e,r=null,a=o=>{let i=B(e,o);return i.gone.onGone(()=>{r?.evict(o,i)}),i},s=new _({defaultApp:e.defaultApp,fnName:e.fnName,openSession:a,closeSession:async o=>{o.gone.dispose(),await o.session.end()},listApps:n.lane==="api-key"?()=>U({apiUrl:e.apiUrl,apiKey:n.apiKey}):null,listCatalog:t?()=>b(t):null,sessionKey:K});return r=s,e.defaultApp!==null&&s.seed(e.defaultApp,a(e.defaultApp)),s}function Se(e,n=E){let t=new WeakMap,r=new WeakMap,a=new WeakMap;return{createResponse:k({sessionFor:s=>e.sessionFor(s),evict:(s,o)=>e.evict(s,o),createStream:async(s,o)=>g(await s.responses.responses.create(o),s.gone)}),listModels:()=>e.modelList(),openAudio:async s=>{let{entry:o}=await e.sessionFor(s),i=t.get(o);return i||(i=(async()=>x(o.session,await n()))(),t.set(o,i),i.catch(()=>t.delete(o))),i},openVideo:async s=>{let{entry:o}=await e.sessionFor(s),i=r.get(o);return i||(i=S(o.session),r.set(o,i)),i},openVideoOut:async s=>{let{entry:o}=await e.sessionFor(s),i=a.get(o);return i||(i=R(o.session),a.set(o,i)),i},sessionHandle:async s=>(await e.handleFor(s)).handle,createResponseOn:async(s,o)=>{let{entry:i}=await e.sessionForHandle(s);return g(await i.responses.responses.create(o),i.gone)},onSessionEnd:async(s,o)=>{let{entry:i}=await e.sessionForHandle(s),u=i.session;if(typeof u.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let c=!1;return u.onPhase(d=>{if(c||!q.has(d.name))return;c=!0;let p=u.endsAt??null;o({timeLeftMs:p?Math.max(0,p.getTime()-Date.now()):null,reason:d.name==="expired"?"session reached its maximum length":`session ${d.name}`})})}}}l();import V from"os";import J from"path";import{fileURLToPath as X}from"url";import{format as z}from"util";var N="0.3.2";var T=4141,Q=N,Ie={claude:{bin:"claude",env:"anthropic"},codex:{bin:"codex",env:"openai"},pi:{bin:"pi",env:"openai"},aider:{bin:"aider",env:"openai"},opencode:{bin:"opencode",env:"openai"},crush:{bin:"crush",env:"openai"}};function Z(e,n="openai"){if(n==="anthropic")return{ANTHROPIC_BASE_URL:`http://127.0.0.1:${e}`,ANTHROPIC_API_KEY:"urun-local"};let t=`http://127.0.0.1:${e}/v1`;return{OPENAI_BASE_URL:t,OPENAI_API_BASE:t,OPENAI_API_KEY:"urun-local"}}function Oe(e,n,t){let r={...e,...Z(n,t)};return delete r.URUN_API_KEY,delete r.URUN_JWT,r}function Ne(e,n,t){let r=["-c","model_providers.urun.name=urun","-c",`model_providers.urun.base_url="http://127.0.0.1:${e}/v1"`,"-c",'model_providers.urun.wire_api="responses"',"-c",'model_provider="urun"'],a=t.indexOf("--"),s=a===-1?t:t.slice(0,a),o=s.some(d=>d==="-m"||d==="--model"||d.startsWith("--model=")),i=t[0]==="exec",u=s.includes("--skip-git-repo-check"),c=i&&!u?["exec","--skip-git-repo-check",...t.slice(1)]:t;return[...r,...o?[]:["-m",n],...c]}function $e(e,n){return n.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?n:["--provider","urun","--model",e,...n]}function Te(e,n,t){let r=(e.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 a=X(new URL("../pi-extension/standalone.cjs",n));if(!t(a))throw new Error(`pi extension entry not found at ${a} \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 a}function C(e){let n=Number(e);if(!Number.isInteger(n)||n<0)throw new Error(`--port needs a non-negative integer (0 = OS-assigned ephemeral), got ${e}`);return n}function Ce(e){if(e[0]!=="--port")return null;let n=C(e[1]);return e.splice(0,2),n}function Ge(e){let n=e.indexOf("--port");if(n===-1)return T;let t=C(e[n+1]);return e.splice(n,2),t}function Le(e){if(e===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 e}function y(e,n){let t=(e[n]??"").trim();if(!t)throw new Error(`${n} 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 je(e){let n=ee(y(e,"URUN_BASE_URL")),t=y(e,"URUN_ORG_ID"),r=y(e,"URUN_APP"),a=(e.URUN_FUNCTION??"serve").trim()||"serve",s=(e.URUN_API_URL??"").trim(),o=s?$(s,"URUN_API_URL"):P,i=(e.URUN_JWT??"").trim();if(i)return{baseUrl:n,orgId:t,appId:r,fnName:a,apiUrl:o,auth:{lane:"jwt",jwt:i}};let u=(e.URUN_API_KEY??"").trim();if(u){let c=(e.URUN_GATEWAY_URL??"").trim(),d=c?$(c):void 0;return{baseUrl:n,orgId:t,appId:r,fnName:a,apiUrl:o,auth:{lane:"api-key",apiKey:u,gatewayUrl:d}}}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 ee(e,n="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(e).pathname))throw new Error(`${n} must be the session-gateway base (e.g. https://api.urun.sh), got ${e} \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 e}function $(e,n="URUN_GATEWAY_URL"){let t=new URL(e),r=t.hostname==="localhost"||t.hostname==="127.0.0.1"||t.hostname==="[::1]";if(t.protocol!=="https:"&&!r)throw new Error(`${n} must be https (got ${e}) \u2014 the org API key rides the request's Authorization header`);return e}function De(e){return{app:e.appId,org:e.orgId,fn:e.fnName,base_url:e.baseUrl,proxy_version:Q}}function ne(e,n){return e.app===n.app&&e.org===n.org&&e.fn===n.fn&&e.base_url===n.base_url&&e.proxy_version===n.proxy_version}var te=250;async function oe(e,n=te){try{let t=await fetch(`http://127.0.0.1:${e}/stats`,{signal:AbortSignal.timeout(n)});if(!t.ok)return null;let r=(await t.json()).identity;if(r==null||typeof r!="object")return null;let{app:a,org:s,fn:o,base_url:i,proxy_version:u}=r;return typeof a!="string"||typeof s!="string"||typeof o!="string"||typeof i!="string"||typeof u!="string"?null:{app:a,org:s,fn:o,base_url:i,proxy_version:u}}catch{return null}}var G=2e3;async function re(e,n=G){try{let t=await fetch(`http://127.0.0.1:${e}/v1/models`,{signal:AbortSignal.timeout(n)});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 Me(e,n,t=T,r=G){if(e!=null)return{mode:"spawn",port:e};let a=await oe(t);if(a!==null&&ne(a,n)){let s=await re(t,r);return s===null?{mode:"reuse",port:t,identity:a}:{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 Fe(e,n=V.homedir()){return J.join(n,".urun","logs",`compat-proxy-${e}.log`)}var w=["log","info","warn","error","debug","trace"];function We(e,n=process,t=console){let r=n.stdout.write,a=n.stderr.write,s=(i,...u)=>e.write(i,...u);n.stdout.write=s,n.stderr.write=s;let o=Object.fromEntries(w.map(i=>[i,t[i]]));for(let i of w)t[i]=(...u)=>{e.write(`${z(...u)}
2
+ `)};return()=>{n.stdout.write=r,n.stderr.write=a;for(let i of w)t[i]=o[i]}}var se=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function L(e){let n=e?.code;return typeof n=="string"&&se.has(n)}function He(e,n){try{return e.write(n),!0}catch(t){if(L(t))return!1;throw t}}function Be(e){let n={stdout:!1,stderr:!1},t=[];for(let r of["stdout","stderr"]){let a=e.streams[r],s=o=>{L(o)&&(n[r]=!0);let i=o?.code;try{e.note(`${new Date().toISOString()} urun compat: process.${r} died (${String(i??(o instanceof Error?o.message:o))}) \u2014 the terminal is gone; launcher output continues in this log only
3
+ `)}catch{}};a.on("error",s),t.push([a,s])}return{isDead:r=>n[r],uninstall:()=>{for(let[r,a]of t)r.off("error",a)}}}function Ke(e){let n=t=>{let r=t instanceof Error?`${t.stack??`${t.name}: ${t.message}`}`:String(t);try{e.appendSync(e.logPath,`${new Date().toISOString()} launcher crashed (in-process proxy): ${r}
4
+ `)}catch{}e.restore();try{e.writeStderr(`urun compat: the in-process proxy crashed (${t instanceof Error?t.message:String(t)}) \u2014 terminating the harness (its backend is gone); details: ${e.logPath}
5
+ `)}catch(a){try{e.appendSync(e.logPath,`${new Date().toISOString()} urun compat: the crash line could not reach stderr (${String(a?.code??a)}) \u2014 the terminal is gone
6
+ `)}catch{}}e.killChild(),e.exit(1)};return e.proc.on("uncaughtException",n),e.proc.on("unhandledRejection",n),()=>{e.proc.off("uncaughtException",n),e.proc.off("unhandledRejection",n)}}function qe(e,n){let t="pass --port 0 for an OS-assigned ephemeral port";return n?`port ${e} is held by another urun-openai proxy serving app ${n.app} (org ${n.org}, fn ${n.fn}, version ${n.proxy_version}) \u2014 reuse it by pointing your agent at http://127.0.0.1:${e}, or ${t}`:`port ${e} is already in use and does not answer the urun-openai /stats identity probe \u2014 ${t}`}export{xe as a,Se as b,Ie as c,Z as d,Oe as e,Ne as f,$e as g,Te as h,Ce as i,Ge as j,Le as k,je as l,ee as m,$ as n,De as o,oe as p,Me as q,Fe as r,We as s,He as t,Be as u,Ke as v,qe as w};
@@ -0,0 +1 @@
1
+ import{e as r}from"./chunk-UVAY7Q7Z.js";import{c as e}from"./chunk-YSFSRI3D.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};
@@ -0,0 +1,40 @@
1
+ import{b as D,c as de}from"./chunk-XHIIEA6Z.js";import{a as Se,c as Ae,d as j,e as fe,f as b,g as Oe,h as Ie,i as Ee,j as be,k as Pe,l as Ce,m as Te,n as Me,r as Le,t as Ne,u as qe,v as $e,w as Be,x as Ue}from"./chunk-5NXM4IO3.js";import{c as ie}from"./chunk-YSFSRI3D.js";ie();import{createServer as Xe}from"http";ie();import{randomUUID as Ge}from"crypto";import{WebSocketServer as Ke}from"ws";var We=256,me=class{snapshots=new Map;store(t,e){for(this.snapshots.set(t,e);this.snapshots.size>We;){let s=this.snapshots.keys().next().value;this.snapshots.delete(s)}}get(t){return this.snapshots.get(t)}},te=n=>n.slice(0,120),ge=class{constructor(t,e,s){this.ws=t;this.clients=e;this.registry=s;t.on("message",o=>this.onMessage(o)),t.on("close",()=>{this.unsubscribeEnd?.(),this.unsubscribeEnd=null,this.detachAudio?.(),this.detachVideoOut?.()})}ws;clients;registry;config=null;transcript=[];queue=Promise.resolve();proxyHandle=null;unsubscribeEnd=null;audio=null;detachAudio=null;detachVideoOut=null;video=null;activityOpen=!1;activeTurn=null;send(t){this.ws.readyState===this.ws.OPEN&&this.ws.send(JSON.stringify(t))}fail(t){if(t instanceof D){this.ws.close(Ae,te(t.message));return}if(t instanceof de){this.ws.close(fe,te(t.message));return}if(t instanceof b){this.ws.close(t.closeCode,te(t.message));return}this.ws.close(j,te(String(t instanceof Error?t.message:t)))}onMessage(t){try{let e=Oe(typeof t=="string"?t:Buffer.concat(Array.isArray(t)?t:[t]).toString("utf8"));if(e.kind==="setup"){this.handleSetup(e.setup).catch(o=>this.fail(o));return}if(!this.config)throw new b(`"${e.kind}" before setup \u2014 the first client message must be BidiGenerateContentSetup`);let s=this.config;if(e.kind==="clientContent"){let{items:o,turnComplete:a}=Me(e.clientContent);this.transcript.push(...o),a&&this.enqueue(()=>this.runTurn());return}if(e.kind==="realtimeInput"){let o=Ne(e.realtimeInput);if(o.kind==="activityStart"||o.kind==="activityEnd"){this.onActivity(o.kind,s);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:Le);return}this.transcript.push(...qe(e.toolResponse)),this.enqueue(()=>this.runTurn())}catch(e){this.fail(e)}}async handleSetup(t){if(this.config)throw new b("setup may only be sent once, as the first client message");let e=Pe(t),s=e.sessionResumption.handle;if(s!==null){let a=this.registry.get(s);if(!a)throw new b("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",fe);if(a.model!==e.model)throw new b(`sessionResumption.handle was issued for model "${a.model}" \u2014 the model cannot change on resume (got "${e.model}", per the Live API session-resumption contract)`);if(e.systemItems.length>0)throw new b("setup.systemInstruction cannot be changed on resume \u2014 it is part of the resumed conversation state");this.transcript=[...a.transcript],this.proxyHandle=a.proxyHandle,this.unsubscribeEnd=await this.clients.onSessionEnd(a.proxyHandle,u=>this.enqueue(()=>this.handleUpstreamEnd(u)))}else this.proxyHandle=await this.clients.sessionHandle(e.model),this.unsubscribeEnd=await this.clients.onSessionEnd(this.proxyHandle,a=>this.enqueue(()=>this.handleUpstreamEnd(a))),this.transcript.push(...e.systemItems);this.config=e;let o={};if(e.videoOut){let a=this.clients.openVideoOut;if(a){let u=await a.call(this.clients,e.model),c=u.onOutputFrame(l=>this.send(be(l))),p=u.onLaneError(l=>this.fail(l));this.detachVideoOut=()=>{c(),p()},o[Ie]=Ee(u.codec)}}this.send({setupComplete:o}),e.audioModality&&this.ensureAudioLane(),e.sessionResumption.enabled&&await this.mintResumptionUpdate()}ensureAudioLane(){if(this.audio)return this.audio;let t=this.config;if(!t)throw new b("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let e=this.clients.openAudio;if(!e)throw new b("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native rt-audio-in/rt-audio-out AudioBridge seam)",j);return this.audio=(async()=>{let s=await e.call(this.clients,t.model);return this.detachAudio=s.onOutputAudio(o=>this.send(Be(o))),s})(),this.audio.catch(s=>this.fail(s)),this.audio}appendAudio(t){this.ensureAudioLane().then(e=>e.appendInputAudio(t)).catch(e=>this.fail(e))}ensureVideoLane(){if(this.video)return this.video;let t=this.config;if(!t)throw new b("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let e=this.clients.openVideo;if(!e)throw new b("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",j);return this.video=Promise.resolve(e.call(this.clients,t.model)),this.video.catch(s=>this.fail(s)),this.video}appendVideoFrame(t){this.ensureVideoLane().then(e=>e.sendInputFrame(t)).catch(e=>this.fail(e))}onActivity(t,e){if(!e.activity.manualActivity)throw new b(`realtimeInput.${t} requires manual activity mode \u2014 set setup.realtimeInputConfig.automaticActivityDetection.disabled: true (automatic server-side activity detection is the declared platform follow-up)`);if(t==="activityStart"){if(this.activityOpen)throw new b("activityStart while an activity window is already open (send activityEnd first)");this.activityOpen=!0,e.activity.activityHandling==="START_OF_ACTIVITY_INTERRUPTS"&&this.activeTurn&&(this.activeTurn.interrupted=!0);return}if(!this.activityOpen)throw new b("activityEnd without an open activity window (send activityStart first)");this.activityOpen=!1}enqueue(t){this.queue=this.queue.then(t,()=>{}),this.queue=this.queue.catch(e=>this.fail(e))}async mintResumptionUpdate(){let t=this.config;if(!t?.sessionResumption.enabled||this.ws.readyState!==this.ws.OPEN)return;let e=await this.clients.sessionHandle(t.model);this.proxyHandle=e;let s=Ge();this.registry.store(s,{model:t.model,proxyHandle:e,transcript:[...this.transcript]}),this.send(Ce(s))}async handleUpstreamEnd(t){if(this.ws.readyState!==this.ws.OPEN||!this.config)return;this.unsubscribeEnd?.(),this.unsubscribeEnd=null;let e=null;try{e=await this.clients.sessionHandle(this.config.model)}catch{}if(e!==null&&e!==this.proxyHandle){this.proxyHandle=e,this.unsubscribeEnd=await this.clients.onSessionEnd(e,s=>this.enqueue(()=>this.handleUpstreamEnd(s))),this.config.sessionResumption.enabled&&await this.mintResumptionUpdate();return}this.send(Te(t.timeLeftMs)),this.ws.close(j,te(`upstream session ended: ${t.reason}`))}async runTurn(){let t=this.config;if(!t||this.ws.readyState!==this.ws.OPEN)return;let e={interrupted:!1};this.activeTurn=e;try{let s=await this.clients.createResponse({model:t.model,input:[...this.transcript],stream:!0,tools:t.tools,temperature:t.temperature,max_output_tokens:t.maxOutputTokens}),o,a=!1;for await(let p of s){if(e.interrupted)break;let l=p;if(l.type==="error")throw new b(`upstream error: ${JSON.stringify(l)}`,j);if(l.type==="response.completed"){o=l.response,a=!0;continue}let d=$e(l);d&&this.send(d)}if(e.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!a)throw new b("upstream produced no response.completed event",j);let{messages:u,assistantItems:c}=Ue(o);this.transcript.push(...c);for(let p of u)this.send(p);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},Ye=(n,t)=>{let e=n.headers["x-goog-api-key"];if(typeof e=="string")return e;let s=n.headers.authorization;return typeof s=="string"&&s.startsWith("Bearer ")?s.slice(7):t.searchParams.get("key")??void 0};function Fe(n,t){let e=new Ke({noServer:!0}),s=new me;n.on("upgrade",(o,a,u)=>{let c=new URL(o.url??"/","http://localhost");if(c.pathname!==Se){a.write(`HTTP/1.1 404 Not Found\r
2
+ Connection: close\r
3
+ \r
4
+ no WS route for ${c.pathname}`),a.destroy();return}if(t.apiKey&&Ye(o,c)!==t.apiKey){a.write(`HTTP/1.1 401 Unauthorized\r
5
+ Connection: close\r
6
+ \r
7
+ invalid local proxy api key`),a.destroy();return}e.handleUpgrade(o,a,u,p=>{new ge(p,t.clients,s)})})}var _e=class n{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(t,e,s){this.requests[t]=(this.requests[t]??0)+1,this.localInBytes+=e,this.naiveInBytes+=s}modelEntry(t){let e=this.models[t];if(e)return e;let s=Object.keys(this.models).length>=Qe?"(other)":t;return this.models[s]??={requests:0,tokens_out:0}}model(t){this.modelEntry(t).requests+=1}track(t,e){let s=this,o=performance.now(),a=null,u=0;return(async function*(){for await(let p of t)p.type==="response.output_text.delta"&&typeof p.delta=="string"&&(a===null&&(a=performance.now(),s.ttftMs.push(a-o),s.ttftMs.length>512&&s.ttftMs.shift()),u+=1,s.tokensOut+=1,s.charsOut+=p.delta.length,e!==void 0&&(s.modelEntry(e).tokens_out+=1)),yield p;let c=(performance.now()-(a??o))/1e3;u>0&&c>0&&(s.tokRates.push(u/c),s.tokRates.length>512&&s.tokRates.shift())})()}static p50(t){if(!t.length)return null;let e=[...t].sort((s,o)=>s-o);return e[Math.floor(e.length/2)]}snapshot(){let t=Object.values(this.requests).reduce((e,s)=>e+s,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:t},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}}}};function ke(n){n.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function U(n,t,e){n.writeHead(t,{"content-type":"application/json"}),n.end(JSON.stringify(e))}function H(n,t,e,s="invalid_request_error"){U(n,t,{error:{message:e,type:s}})}function B(n,t,e,s){U(n,t,{type:"error",error:{type:e,message:s}})}function xe(n,t,e){if(t==="anthropic"){U(n,404,{type:"error",error:{type:"not_found_error",message:e.message}});return}U(n,404,{error:{message:e.message,type:"invalid_request_error",code:"model_not_found"}})}var Qe=256,Ze=128;function ve(n){return typeof n.model!="string"||!n.model.trim()?"(default)":n.model.trim().slice(0,Ze)}var Je=64*1024*1024,ae=class extends Error{};async function et(n){let t=[],e=0;for await(let o of n){if(e+=o.length,e>Je)throw new ae(`request body exceeds ${Je} bytes`);t.push(o)}let s=Buffer.concat(t).toString("utf8");return s?JSON.parse(s):{}}function W(n,t,e,s){return{id:n,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:t,choices:[{index:0,delta:e,finish_reason:s}]}}var tt=256;function nt(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 we=class{conversations=new Map;thread(t,e){let s=nt(e);if(t==null)return s;let o=this.conversations.get(String(t));if(!o)throw new Error(`unknown previous_response_id ${String(t)} (proxy restarts drop stored responses)`);return[...o,...s]}remember(t,e,s){this.conversations.set(t,[...e,{role:"assistant",content:s}]);for(let o of this.conversations.keys()){if(this.conversations.size<=tt)break;this.conversations.delete(o)}}};function ne(n){let t=n?.output;return Array.isArray(t)?t.filter(e=>e.type==="function_call"):[]}function z(n){return n.find(t=>t.type==="response.completed")?.response??null}function ue(n){let t=n?.usage;if(!t||typeof t!="object")return null;let e=u=>typeof u=="number"&&Number.isFinite(u)&&u>=0?u:0,s=e(t.input_tokens),o=e(t.output_tokens);if(s===0&&o===0)return null;let a=e(t.total_tokens)||s+o;return{input_tokens:s,output_tokens:o,total_tokens:a}}function He(n){let t=ue(n);return t?{prompt_tokens:t.input_tokens,completion_tokens:t.output_tokens,total_tokens:t.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function De(n){let t=n?.output;if(!Array.isArray(t))return"";let e="";for(let s of t)if(!(s.type!=="reasoning"||!Array.isArray(s.content)))for(let o of s.content)o.type==="reasoning_text"&&(e+=String(o.text??""));return e}function se(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 G=class{calls=new Map;add(t){let e=this.calls.get(t.tool_index),s=!e;return e||(e={args:""},this.calls.set(t.tool_index,e)),t.call_id&&(e.id=t.call_id),t.name&&(e.name=t.name),e.args+=t.delta,{call:e,isNew:s}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([t,e])=>({type:"function_call",call_id:e.id??`call_${t}`,name:e.name??"",arguments:e.args}))}},st=["<tool_call>","<function="];function ot(n){return st.find(t=>n.includes(t))}function Y(n,t){return`serving row emitted a prose tool call (literal ${JSON.stringify(t)} 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 X(n,t,e){if(!(!n||t>0))return ot(e)}function rt(n){if(Array.isArray(n))return n.map(t=>{let e=t.function;return!e||typeof e!="object"?t:{type:"function",name:e.name,description:e.description,parameters:e.parameters,strict:e.strict}})}var le=class extends Error{};function it(n,t){return Array.isArray(t)?t.map(e=>{let s=e;if(s.type==="text")return{type:n==="assistant"?"output_text":"input_text",text:s.text};if(s.type==="image_url"){let o=s.image_url??{};return{type:"input_image",image_url:o.url,detail:o.detail}}throw new le(`unsupported chat content part type "${String(s.type)}" \u2014 the proxy translates text and image_url parts`)}):t}function at(n){let t=[];for(let e of n){if(e.role==="tool"){t.push({type:"function_call_output",call_id:e.tool_call_id,output:typeof e.content=="string"?e.content:JSON.stringify(e.content??"")});continue}if(e.role==="assistant"&&Array.isArray(e.tool_calls)){typeof e.content=="string"&&e.content&&t.push({role:"assistant",content:e.content});for(let s of e.tool_calls){let o=s.function??{};t.push({type:"function_call",call_id:s.id,name:o.name,arguments:o.arguments})}continue}t.push({...e,content:it(e.role,e.content)})}return t}function he(n){return ne(n).map((t,e)=>({index:e,id:t.call_id??t.id??`call_${e}`,type:"function",function:{name:t.name??"",arguments:t.arguments??"{}"}}))}function Ve(n,t){let e=t?.output_text;return typeof e=="string"?e:n.filter(s=>s.type==="response.output_text.delta"&&typeof s.delta=="string").map(s=>s.delta).join("")}async function ut(n,t,e,s,o){let a=n.stream===!0,u;try{u=e.thread(n.previous_response_id,n.input)}catch(r){H(s,400,String(r instanceof Error?r.message:r));return}n.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(u))-Buffer.byteLength(JSON.stringify(n.input)));let c=`resp_${Math.random().toString(36).slice(2,14)}`,p=n.store!==!1,l=ve(n);o.model(l);let d=Array.isArray(n.tools)&&n.tools.length>0,h;try{h=await t.createResponse({model:n.model,input:u,stream:a,tools:n.tools,tool_choice:n.tool_choice,temperature:n.temperature,max_output_tokens:n.max_output_tokens,...n.reasoning_effort!==void 0?{reasoning_effort:n.reasoning_effort}:{},...n.chat_template_kwargs!==void 0?{chat_template_kwargs:n.chat_template_kwargs}:{}})}catch(r){if(r instanceof D){xe(s,"openai",r);return}throw r}let m=[];if(a){ke(s);let r=f=>{s.write(`event: ${String(f.type??"message")}
8
+ data: ${JSON.stringify(f)}
9
+
10
+ `)},A=f=>f.response&&typeof f.response=="object"?{...f,response:{...f.response,id:c}}:f,O=`item_${c.slice(5)}`,M=`rs_${c.slice(5)}`,C=!1,L=!1,i=0,x=()=>{C||(C=!0,r({type:"response.created",response:{id:c,object:"response",status:"in_progress"}}))},q=!1,P=0,N="",$=()=>{q&&(q=!1,r({type:"response.reasoning_text.done",item_id:M,output_index:P,content_index:0,text:N}),r({type:"response.output_item.done",output_index:P,item:{id:M,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:N}],status:"completed"}}))},T=!1,w=0,S="",F=f=>{T&&(T=!1,r({type:"response.output_text.done",item_id:O,output_index:w,content_index:0,text:S}),r({type:"response.content_part.done",item_id:O,output_index:w,content_index:0,part:{type:"output_text",text:S}}),r({type:"response.output_item.done",output_index:w,item:{id:O,type:"message",role:"assistant",status:f,content:[{type:"output_text",text:S}]}}))},J=new G,v=null,K=-1,Q=f=>{if(!v)return;let R=v;v=null,r({type:"response.function_call_arguments.done",item_id:R.fcId,output_index:R.outputIndex,arguments:R.args}),r({type:"response.output_item.done",output_index:R.outputIndex,item:{id:R.fcId,type:"function_call",call_id:R.callId??R.fcId,name:R.name??"",arguments:R.args,status:f}})},Z=!1;for await(let f of o.track(h,l)){m.push(f);let R=String(f.type??"");if(R==="response.created"||R==="response.in_progress"){C=!0,r(A(f));continue}if(R==="response.output_item.added"){L=!0,r(A(f));continue}if(R==="response.reasoning_text.delta"&&!L){x(),q||(q=!0,P=i++,r({type:"response.output_item.added",output_index:P,item:{id:M,type:"reasoning",summary:[],content:[],status:"in_progress"}})),N+=String(f.delta??""),r({type:"response.reasoning_text.delta",item_id:M,output_index:P,content_index:0,delta:f.delta});continue}if(R==="response.output_text.delta"&&!L){x(),$(),T||(T=!0,w=i++,r({type:"response.output_item.added",output_index:w,item:{id:O,type:"message",role:"assistant",status:"in_progress",content:[]}}),r({type:"response.content_part.added",item_id:O,output_index:w,content_index:0,part:{type:"output_text",text:""}})),S+=String(f.delta??""),r({type:R,item_id:O,output_index:w,content_index:0,delta:f.delta});continue}let I=L?null:se(f);if(I){if(x(),$(),F("completed"),J.add(I),I.tool_index!==K){Q("completed"),K=I.tool_index;let g=i++;v={fcId:`fc_${c.slice(5)}_${I.tool_index}`,outputIndex:g,callId:I.call_id,name:I.name,args:""},r({type:"response.output_item.added",output_index:g,item:{id:v.fcId,type:"function_call",call_id:v.callId??v.fcId,name:v.name??"",arguments:"",status:"in_progress"}})}v&&(I.call_id&&(v.callId=I.call_id),I.name&&(v.name=I.name),v.args+=I.delta,r({type:"response.function_call_arguments.delta",item_id:v.fcId,output_index:v.outputIndex,delta:I.delta}));continue}if(R==="response.completed"&&!L){let g=ne(f.response),E=X(d,J.size+g.length,S);if(E){Z=!0,r({type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:Y(l,E)}});break}$(),F("completed"),Q("completed"),J.size===0&&g.forEach(V=>{let ee=i++,re=V.id??V.call_id??`fc_${ee}`,pe=V.arguments??"",Re={id:re,type:"function_call",call_id:V.call_id??re,name:V.name??""};r({type:"response.output_item.added",output_index:ee,item:{...Re,arguments:"",status:"in_progress"}}),r({type:"response.function_call_arguments.delta",item_id:re,output_index:ee,delta:pe}),r({type:"response.function_call_arguments.done",item_id:re,output_index:ee,arguments:pe}),r({type:"response.output_item.done",output_index:ee,item:{...Re,arguments:pe,status:"completed"}})}),r(A(f));continue}r(A(f))}$(),F("incomplete"),Q("incomplete"),p&&!Z&&e.remember(c,u,Ve(m,z(m))),s.write(`data: [DONE]
11
+
12
+ `),s.end();return}let k=null;for await(let r of o.track(h,l)){if(m.push(r),r.type==="error"){H(s,502,JSON.stringify(r),"upstream_error");return}r.type==="response.completed"&&(k=r.response)}if(k==null){H(s,502,"upstream produced no response.completed event","upstream_error");return}let _=Ve(m,k),y=X(d,ne(k).length,_);if(y){H(s,502,Y(l,y),"upstream_error");return}p&&e.remember(c,u,_),U(s,200,{...k,id:c})}async function lt(n,t,e,s){let o=n.messages;if(!Array.isArray(o)){H(e,400,"chat/completions requires a messages array");return}let a=String(n.model??"urun"),u=`chatcmpl-${Math.random().toString(36).slice(2,14)}`,c;try{c=at(o)}catch(i){if(i instanceof le){H(e,400,i.message);return}throw i}let p=ve(n);s.model(p);let l=Array.isArray(n.tools)&&n.tools.length>0,d;try{d=await t.createResponse({model:n.model,input:c,stream:!0,tools:rt(n.tools),tool_choice:n.tool_choice,temperature:n.temperature,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}:{}})}catch(i){if(i instanceof D){xe(e,"openai",i);return}throw i}if(n.stream===!0){ke(e);let i=[],x=new G,q="";e.write(`data: ${JSON.stringify(W(u,a,{role:"assistant"},null))}
13
+
14
+ `);for await(let w of s.track(d,p)){i.push(w);let S=se(w);if(w.type==="response.output_text.delta"&&typeof w.delta=="string")q+=w.delta,e.write(`data: ${JSON.stringify(W(u,a,{content:w.delta},null))}
15
+
16
+ `);else if(w.type==="response.reasoning_text.delta"&&typeof w.delta=="string")e.write(`data: ${JSON.stringify(W(u,a,{reasoning_content:w.delta},null))}
17
+
18
+ `);else if(S){let{call:F,isNew:J}=x.add(S),v=J?{index:S.tool_index,id:F.id??`call_${S.tool_index}`,type:"function",function:{name:F.name??"",arguments:S.delta}}:{index:S.tool_index,function:{arguments:S.delta}};e.write(`data: ${JSON.stringify(W(u,a,{tool_calls:[v]},null))}
19
+
20
+ `)}else if(w.type==="error"){e.write(`data: ${JSON.stringify({error:w})}
21
+
22
+ `),e.end();return}}let P=he(z(i)),N=X(l,x.size+P.length,q);if(N){e.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:Y(p,N)}})}
23
+
24
+ `),e.end();return}x.size===0&&P.length>0&&e.write(`data: ${JSON.stringify(W(u,a,{tool_calls:P},null))}
25
+
26
+ `);let $=x.size>0||P.length>0;e.write(`data: ${JSON.stringify(W(u,a,{},$?"tool_calls":"stop"))}
27
+
28
+ `),ue(z(i))&&e.write(`data: ${JSON.stringify({id:u,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:a,choices:[],usage:He(z(i))})}
29
+
30
+ `),e.write(`data: [DONE]
31
+
32
+ `),e.end();return}let h="",m="",k=[],_=new G;for await(let i of s.track(d,p)){k.push(i),i.type==="response.output_text.delta"&&typeof i.delta=="string"&&(h+=i.delta),i.type==="response.reasoning_text.delta"&&typeof i.delta=="string"&&(m+=i.delta);let x=se(i);if(x&&_.add(x),i.type==="error"){H(e,502,JSON.stringify(i),"upstream_error");return}}let y=z(k),r=he(y),A=r.length>0?r:he({output:_.items()}),O=X(l,A.length,h);if(O){H(e,502,Y(p,O),"upstream_error");return}let M=A.map(({index:i,...x})=>x),C={role:"assistant",content:h||null},L=m||De(y);L&&(C.reasoning_content=L),M.length>0&&(C.tool_calls=M),U(e,200,{id:u,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:a,choices:[{index:0,message:C,finish_reason:M.length>0?"tool_calls":"stop"}],usage:He(y)})}function ct(n){return typeof n=="string"?n:Array.isArray(n)?n.filter(t=>t.type==="text").map(t=>String(t.text??"")).join(""):""}var oe=class extends Error{};function ce(n){return new oe(`unsupported anthropic content block type "${n}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function pt(n){if(Array.isArray(n))return n.map(t=>{let e=t;return{type:"function",name:e.name,description:e.description,parameters:e.input_schema}})}function dt(n){if(n==null)return;let t=n.type;if(t==="auto")return"auto";if(t==="none")return"none";if(t==="any")return"required";if(t==="tool")return{type:"function",name:n.name};throw new oe(`unsupported anthropic tool_choice type ${JSON.stringify(String(t))} \u2014 the proxy maps auto, none, any and tool`)}function ft(n){if(typeof n=="string")return n;if(n==null)return"";if(!Array.isArray(n))throw ce(typeof n);let t="";for(let e of n){let s=String(e.type??"");if(s!=="text")throw ce(`tool_result > ${s}`);t+=String(e.text??"")}return t}function mt(n){let{role:t,content:e}=n;if(typeof e=="string")return[{role:t,content:e}];if(e==null)return[];if(!Array.isArray(e))throw ce(typeof e);let s=[],o="",a=()=>{o&&(s.push({role:t,content:o}),o="")};for(let u of e){let c=String(u.type??"");if(c==="text")o+=String(u.text??"");else if(c==="tool_use")a(),s.push({type:"function_call",call_id:u.id,name:u.name,arguments:JSON.stringify(u.input??{})});else if(c==="tool_result")a(),s.push({type:"function_call_output",call_id:u.tool_use_id,output:ft(u.content)});else{if(c==="thinking"||c==="redacted_thinking")continue;throw ce(c)}}return a(),s}function ye(n){return ne(n).map((t,e)=>{let s;try{s=JSON.parse(t.arguments||"{}")}catch{throw new Error(`upstream function_call "${t.name??""}" arguments are not valid JSON`)}return{type:"tool_use",id:t.call_id??t.id??`toolu_${e}`,name:t.name??"",input:s}})}async function gt(n,t,e,s){let o=n.messages;if(!Array.isArray(o)){B(e,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){B(e,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(n.top_k!==void 0){B(e,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(i=>typeof i!="string"))){B(e,400,"invalid_request_error","stop_sequences must be an array of strings");return}let a=String(n.model??"urun"),u=`msg_${Math.random().toString(36).slice(2,14)}`,c=[],p;try{n.system&&c.push({role:"system",content:ct(n.system)});for(let i of o)c.push(...mt(i));p=dt(n.tool_choice)}catch(i){if(i instanceof oe){B(e,400,"invalid_request_error",i.message);return}throw i}let l=ve(n);s.model(l);let d=Array.isArray(n.tools)&&n.tools.length>0,h;try{h=await t.createResponse({model:n.model,input:c,stream:!0,tools:pt(n.tools),tool_choice:p,temperature:n.temperature,max_output_tokens:n.max_tokens,top_p:n.top_p,stop:n.stop_sequences})}catch(i){if(i instanceof D){xe(e,"anthropic",i);return}throw i}let m=(i,x)=>{e.destroyed||e.write(`event: ${i}
33
+ data: ${JSON.stringify({type:i,...x})}
34
+
35
+ `)};if(n.stream===!0){let i=s.track(h,l);ke(e),m("message_start",{message:{id:u,type:"message",role:"assistant",content:[],model:a,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let x=0,q="",P=0,N="none",$=()=>N,T=-1,w=-1,S=new G,F=[],J=()=>{N!=="none"&&(N="none",m("content_block_stop",{index:T}))},v=(g,E)=>{J(),N=g,T=P++,m("content_block_start",{index:T,content_block:E})};for await(let g of i){if(e.destroyed)break;F.push(g);let E=se(g);if(g.type==="response.reasoning_text.delta"&&typeof g.delta=="string")$()!=="thinking"&&v("thinking",{type:"thinking",thinking:""}),m("content_block_delta",{index:T,delta:{type:"thinking_delta",thinking:g.delta}});else if(g.type==="response.output_text.delta"&&typeof g.delta=="string")$()!=="text"&&v("text",{type:"text",text:""}),x+=1,q+=g.delta,m("content_block_delta",{index:T,delta:{type:"text_delta",text:g.delta}});else if(E){let{call:V}=S.add(E);($()!=="tool"||E.tool_index!==w)&&(w=E.tool_index,v("tool",{type:"tool_use",id:V.id??`call_${E.tool_index}`,name:V.name??"",input:{}})),E.delta&&m("content_block_delta",{index:T,delta:{type:"input_json_delta",partial_json:E.delta}})}else if(g.type==="error"){m("error",{error:{type:"api_error",message:JSON.stringify(g)}}),e.end();return}}if(e.destroyed)return;let K=z(F),Q=ne(K),Z=X(d,S.size+Q.length,q);if(Z){m("error",{error:{type:"api_error",message:Y(l,Z)}}),e.end();return}J();let f=[];if(S.size===0)try{f=ye(K)}catch(g){m("error",{error:{type:"api_error",message:String(g instanceof Error?g.message:g)}}),e.end();return}P===0&&f.length===0&&(v("text",{type:"text",text:""}),J()),f.forEach(g=>{let E=P++;m("content_block_start",{index:E,content_block:{...g,input:{}}}),m("content_block_delta",{index:E,delta:{type:"input_json_delta",partial_json:JSON.stringify(g.input??{})}}),m("content_block_stop",{index:E})});let R=S.size>0||f.length>0,I=ue(K);m("message_delta",{delta:{stop_reason:R?"tool_use":"end_turn"},usage:I?{input_tokens:I.input_tokens,output_tokens:I.output_tokens}:{output_tokens:x}}),m("message_stop",{}),e.end();return}let k="",_="",y=[],r=new G;for await(let i of s.track(h,l)){y.push(i),i.type==="response.output_text.delta"&&typeof i.delta=="string"&&(k+=i.delta),i.type==="response.reasoning_text.delta"&&typeof i.delta=="string"&&(_+=i.delta);let x=se(i);if(x&&r.add(x),i.type==="error"){B(e,502,"api_error",JSON.stringify(i));return}if(e.destroyed)break}if(e.destroyed)return;let A=z(y),O;try{O=ye(A),O.length===0&&r.size>0&&(O=ye({output:r.items()}))}catch(i){B(e,502,"api_error",String(i instanceof Error?i.message:i));return}let M=X(d,O.length,k);if(M){B(e,502,"api_error",Y(l,M));return}let C=[],L=_||De(A);L&&C.push({type:"thinking",thinking:L}),(k||O.length===0)&&C.push({type:"text",text:k}),C.push(...O),U(e,200,{id:u,type:"message",role:"assistant",content:C,model:a,stop_reason:O.length>0?"tool_use":"end_turn",usage:ue(A)??{input_tokens:0,output_tokens:0}})}function ht(n){let{clients:t,apiKey:e,identity:s,statusOf:o}=n,a=typeof t=="function"?t:()=>t,u=new WeakMap,c=l=>{let d=u.get(l);return d||(d=new we,u.set(l,d)),d},p=new _e;return(l,d)=>{let h=new URL(l.url??"/","http://localhost"),m=h.pathname==="/v1/messages"||h.pathname.startsWith("/v1/messages/"),k=(_,y,r,A)=>m?B(d,_,A,y):H(d,_,y,r);(async()=>{if(l.method==="GET"&&h.pathname==="/healthz"){U(d,200,{ok:!0});return}if(l.method==="GET"&&h.pathname==="/stats"){U(d,200,s?{identity:s,...p.snapshot()}:p.snapshot());return}if(e&&(l.headers.authorization??"")!==`Bearer ${e}`){k(401,"invalid local proxy api key","authentication_error","authentication_error");return}let _=await a(l);if(l.method==="GET"&&h.pathname==="/v1/models"){U(d,200,await _.listModels());return}if(l.method!=="POST"){k(404,`no route for ${l.method} ${h.pathname}`,"invalid_request_error","not_found_error");return}let y;try{y=await et(l)}catch(A){if(A instanceof ae){k(413,A.message,"request_too_large","request_too_large");return}k(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let r=Buffer.byteLength(JSON.stringify(y));if(h.pathname==="/v1/responses"){p.request("responses",r,r),await ut(y,_,c(_),d,p);return}if(h.pathname==="/v1/chat/completions"){p.request("chat_completions",r,r),await lt(y,_,d,p);return}if(h.pathname==="/v1/messages"){p.request("messages",r,r),await gt(y,_,d,p);return}if(h.pathname==="/v1/messages/count_tokens"){p.request("count_tokens",r,r),B(d,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}k(404,`no route for POST ${h.pathname}`,"invalid_request_error","not_found_error")})().catch(_=>{let y=String(_ instanceof Error?_.message:_);if(!d.headersSent){let r=o?.(_)??null;if(r){k(r.status,y,r.openaiType,r.anthropicType);return}k(500,y,"proxy_error","api_error");return}d.destroyed||(m?d.write(`event: error
36
+ data: ${JSON.stringify({type:"error",error:{type:"api_error",message:y}})}
37
+
38
+ `):d.write(`data: ${JSON.stringify({error:{message:y,type:"upstream_error"}})}
39
+
40
+ `)),d.end()})}}function Ot(n){let{clients:t,apiKey:e}=n,s=Xe(ht(n));return Fe(s,{clients:t,apiKey:e}),s}export{ht as a,Ot as b};
@@ -0,0 +1 @@
1
+ import{c as a}from"./chunk-YSFSRI3D.js";a();function g(e,n,r,t){let o=e.response??{},i={request_id:r,consumer_id:t,stream:!0,kind:"chat",messages:n};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,n,r){let t={request_id:n,consumer_id:r,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(t.model=e.model),e.tools!==void 0&&(t.tools=e.tools),e.tool_choice!==void 0&&(t.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(t.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(t.max_output_tokens=e.max_output_tokens),typeof e.reasoning_effort=="string"&&(t.reasoning_effort=e.reasoning_effort),e.chat_template_kwargs!==void 0&&(t.chat_template_kwargs=e.chat_template_kwargs),t}a();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 n=e,r=n.body??{};return{type:"error",error:{type:"urun_error",code:n.code??null,message:r.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}a();var k="llm-resp";async function*d(e,n){let r=`${k}:${n}`,t=`resp_${n}`;yield{type:"response.created",response:{id:t,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:t,delta:s.delta}),typeof s.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:t,delta:s.reasoning}),Array.isArray(s.tool_calls))for(let u of s.tool_calls){let c=typeof u.index=="number"?u.index:0,p=o.get(c)??{};u.id&&(p.id=u.id),u.function?.name&&(p.name=u.function.name),o.set(c,p),yield{type:"response.function_call_arguments.delta",item_id:`fc_${n}_${c}`,tool_index:c,call_id:p.id,name:p.name,delta:u.function?.arguments??""}}}else if(s.t==="response"){yield{type:"response.completed",response:{id:t,status:"completed",...s.body}};return}else if(s.t==="error"){yield f(s);return}}}a();var w="llm",y=class{constructor(n){this.session=n;this.sessionTag=n.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(n){n.session_tag=this.sessionTag,this.session.doc(w).set({requests:{[n.request_id]:{payload:n,consumer_id:n.consumer_id,stream:n.stream}}})}sendResponses(n,r){let t=_(n,r,this.consumerId);return this.write(t),d(this.session,r)}sendResponseCreate(n,r,t){let o=g(n,r,t,this.consumerId);return this.write(o),d(this.session,t)}};export{g as a,_ as b,f as c,d,y as e};
@@ -0,0 +1,2 @@
1
+ "use strict";var he=Object.create;var E=Object.defineProperty;var ke=Object.getOwnPropertyDescriptor;var be=Object.getOwnPropertyNames;var Oe=Object.getPrototypeOf,_e=Object.prototype.hasOwnProperty;var Ae=(e,t)=>{for(var n in t)E(e,n,{get:t[n],enumerable:!0})},F=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of be(t))!_e.call(e,r)&&r!==n&&E(e,r,{get:()=>t[r],enumerable:!(o=ke(t,r))||o.enumerable});return e};var $=(e,t,n)=>(n=e!=null?he(Oe(e)):{},F(t||!e||!e.__esModule?E(n,"default",{value:e,enumerable:!0}):n,e)),ve=e=>F(E({},"__esModule",{value:!0}),e);var Pe={};Ae(Pe,{AudioBridge:()=>A,CLOSE_PROTOCOL:()=>B,CLOSE_SESSION_GONE:()=>ne,CLOSE_UNKNOWN_MODEL:()=>te,CLOSE_UPSTREAM:()=>U,GEMINI_AUDIO_IN_RATE:()=>O,GEMINI_AUDIO_OUT_MIME:()=>H,GEMINI_AUDIO_OUT_RATE:()=>V,GEMINI_LIVE_PATH:()=>ee,GeminiLiveError:()=>s,NATIVE_AUDIO_SAMPLE_RATE:()=>I,OUTBOUND_AUDIO_FLUSH_B64:()=>le,RT_VIDEO_IN_STREAM:()=>S,RT_VIDEO_OUT_STREAM:()=>b,SdkTransport:()=>M,VIDEO_FRAME_MESSAGE_BUDGET:()=>R,VIDEO_OUT_DEFAULT_CODEC:()=>T,VIDEO_OUT_FRAMING:()=>P,VIDEO_OUT_SETUP_KEY:()=>k,VIDEO_OUT_WS_FRAMING:()=>C,consumeVideoOut:()=>X,decodeResponsesLane:()=>v,decodeVideoOutRecord:()=>x,enableSessionAudio:()=>W,enableSessionVideo:()=>G,encodeVideoOutRecord:()=>K,goAwayMsg:()=>de,negotiateSessionVideoOutQuic:()=>Q,nodeAudioBackend:()=>q,openSessionVideoOutLane:()=>z,parseClientMessage:()=>oe,resamplePcm16:()=>j,responseCreateToEnvelope:()=>D,responsesCreateToEnvelope:()=>N,serverContentOfAudio:()=>ge,serverContentOfDelta:()=>me,sessionResumptionUpdateMsg:()=>ue,translateClientContent:()=>ce,translateRealtimeInput:()=>fe,translateSetup:()=>ae,translateToolResponse:()=>pe,turnEndOfCompleted:()=>ye,videoOutAck:()=>ie,videoOutChunkMsg:()=>se,weriftAudioBackend:()=>J});module.exports=ve(Pe);var I=24e3,A=class{constructor(t,n){this.backend=t;this.opts=n}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(t){if(!this.source)throw new Error("startOutbound() not called");let n=Buffer.from(t,"base64"),o=Math.floor(n.byteLength/2),r=new Int16Array(o);for(let i=0;i<o;i++)r[i]=n.readInt16LE(i*2);this.source.onData(r)}async startInbound(t){if(t==null)throw new Error('startInbound: no downstream audio track \u2014 the runtime audio producer has not been consumed yet (session.stream("rt-audio-out").track is null)');this.sink=this.backend.createSink(t),this.sink.onframe=n=>{let o=Buffer.from(n.samples.buffer,n.samples.byteOffset,n.samples.byteLength).toString("base64");for(let r of this.outHandlers)r(o)}}onOutputAudio(t){return this.outHandlers.add(t),()=>this.outHandlers.delete(t)}};async function W(e,t,n=1e4){let o=new A(t,{sampleRate:24e3}),r=await o.startOutbound();await e.stream("rt-audio-in").attach(r);let i=await Ee(e,n);return await o.startInbound(i),o}function Ee(e,t){let n=e.stream("rt-audio-out");return n.track!=null?Promise.resolve(n.track):new Promise((o,r)=>{let i,a=setTimeout(()=>{i?.(),r(new Error(`enableSessionAudio: no downstream audio track within ${t}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},t),c=d=>{d!=null&&(clearTimeout(a),i?.(),o(d))};i=n.on?.("track",c),n.track!=null&&c(n.track)})}var S="rt-video-in",R=256e3;function G(e){let t=e.stream(S),n=t.emit;if(typeof n!="function")throw new Error(`enableSessionVideo: session.stream('${S}') 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 o=>{if(o.byteLength===0)throw new Error("enableSessionVideo: refusing to send an empty video frame");if(o.byteLength>R)throw new Error(`enableSessionVideo: frame is ${o.byteLength} bytes \u2014 over the ${R}-byte data-channel budget (the SFU's 262144-byte SCTP ceiling drops oversize messages SILENTLY; send smaller/lower-quality frames)`);await n.call(t,o)}}}var Se=24e3,_=480,Re=111;async function Ie(){let e=await import("opusscript"),t=e.default??e;return new t(Se,1,2048)}async function J(){let e=await import("werift"),t=await Ie();return{createSource:()=>{let n=new e.MediaStreamTrack({kind:"audio"}),o=Math.random()*4294967295>>>0,r=Math.random()*65535&65535,i=Math.random()*4294967295>>>0,a=new Int16Array(0);return{track:n,onData:c=>{let d=new Int16Array(a.length+c.length);d.set(a,0),d.set(c,a.length);let u=0;for(;d.length-u>=_;){let l=d.subarray(u,u+_);u+=_;let f=Buffer.from(l.buffer,l.byteOffset,l.byteLength),p=t.encode(f,_);r=r+1&65535,i=i+_>>>0;let g=new e.RtpHeader({version:2,payloadType:Re,sequenceNumber:r,timestamp:i,ssrc:o,marker:!1}),w=new e.RtpPacket(g,p);n.writeRtp(w)}a=d.slice(u)}}},createSink:n=>{let o={onframe:null};return n.onReceiveRtp.subscribe(i=>{let a;try{a=t.decode(i.payload)}catch{return}let c=new Int16Array(a.length/2);for(let d=0;d<c.length;d++)c[d]=a.readInt16LE(d*2);o.onframe?.({samples:c})}),o}}}async function q(){let e=await import("@roamhq/wrtc");return{createSource:()=>{let t=new e.nonstandard.RTCAudioSource;return{track:t.createTrack(),onData:o=>t.onData({samples:o,sampleRate:24e3})}},createSink:t=>{let n={onframe:null},o=new e.nonstandard.RTCAudioSink(t);return o.ondata=r=>{n.onframe?.(r)},n}}}var y=require("@urun-sh/core"),b="rt-video-out",P="urun.videoOut.v1",T="h264";function Y(e,t){if(!Number.isInteger(e.seq)||e.seq<0)throw new Error(`${t}: header seq must be a non-negative integer (got ${e.seq})`);if(!Number.isInteger(e.ptsUs))throw new Error(`${t}: header ptsUs must be an integer microsecond timestamp (got ${e.ptsUs})`);if(typeof e.codec!="string"||e.codec.length===0)throw new Error(`${t}: header codec must be a non-empty string`);if(typeof e.key!="boolean")throw new Error(`${t}: header key must be a boolean`)}function K(e,t){if(Y(e,"encodeVideoOutRecord"),t.byteLength===0)throw new Error("encodeVideoOutRecord: refusing to encode an empty access unit");let n=new TextEncoder().encode(JSON.stringify({v:1,seq:e.seq,ptsUs:e.ptsUs,codec:e.codec,key:e.key}));if(n.byteLength>65535)throw new Error(`encodeVideoOutRecord: header JSON is ${n.byteLength} bytes \u2014 over the u16 length prefix`);let o=new Uint8Array(2+n.byteLength+t.byteLength);return o[0]=n.byteLength&255,o[1]=n.byteLength>>8,o.set(n,2),o.set(t,2+n.byteLength),o}function x(e){if(e.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${e.byteLength} byte(s), no header length)`);let t=e[0]|e[1]<<8;if(e.byteLength<2+t)throw new Error(`decodeVideoOutRecord: truncated record (header says ${t} bytes, ${e.byteLength-2} present)`);let n;try{n=JSON.parse(new TextDecoder().decode(e.subarray(2,2+t)))}catch(a){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(a)})`)}let o=n;if(o===null||typeof o!="object"||o.v!==1)throw new Error(`decodeVideoOutRecord: header is not spec v1 (v=${JSON.stringify(o?.v)}) \u2014 a newer producer must be met with a newer consumer, never silently misread`);let r=o;Y(r,"decodeVideoOutRecord");let i=e.subarray(2+t);if(i.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:r,au:i.slice()}}async function Q(e){let t=e.wtDataEndpoint;if(typeof t!="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 n=await t.call(e),o=n.sessionId??e.sessionId;if(typeof o!="string"||o.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:n.url,certHashes:n.certHashes,token:n.token,sessionId:o,stream:b,codec:T,framing:P}}var C="urun.videoOut.v2-ws";function z(e){let t=e.stream(b);if(typeof t.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${b}') 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 n=new Set,o=new Set,r=new Set,i=d=>{for(let u of o)u(d)},a=!1,c=async()=>{for await(let d of t.messages()){if(!(d instanceof Uint8Array)){i(new Error(`openSessionVideoOutLane: "${b}" delivered a non-BINARY \xA75 payload (${typeof d}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let u;try{u=x(d)}catch(l){i(l instanceof Error?l:new Error(String(l)));continue}for(let l of n)l(u)}for(let d of r)d()};return{codec:T,onOutputFrame:d=>(n.add(d),a||(a=!0,c().catch(u=>i(u instanceof Error?u:new Error(String(u))))),()=>n.delete(d)),onLaneError:d=>(o.add(d),()=>o.delete(d)),onEnd:d=>(r.add(d),()=>r.delete(d))}}function Te(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 t=new Uint8Array(e.length/2);for(let n=0;n<t.length;n++)t[n]=parseInt(e.slice(n*2,n*2+2),16);return t}async function X(e,t,n){let o=n??(typeof globalThis.WebTransport=="function"?(u,l)=>new globalThis.WebTransport(u,l):void 0);if(!o)throw new Error("consumeVideoOut: no WebTransport implementation \u2014 this platform has no globalThis.WebTransport; inject a connect factory (the third argument)");let r=o(e.url,{serverCertificateHashes:e.certHashes.map(u=>({algorithm:"sha-256",value:Te(u)}))});await r.ready;let i=await r.createBidirectionalStream();await i.writable.getWriter().write((0,y.encodeWtControlFrame)({t:"hello",token:e.token,sessionId:e.sessionId}));let a=i.readable.getReader(),c=new y.WtControlDecoder,d=!1;for(;!d;){let{value:u,done:l}=await a.read();if(l)throw new Error("consumeVideoOut: gateway closed the control stream before hello-ack");if(u)for(let f of c.push(u)){if(f.t==="hello-ack"){d=!0;break}throw new Error(`consumeVideoOut: gateway rejected hello (got ${JSON.stringify(f.t)} before hello-ack)`)}}await(0,y.consumeWtDataStreams)(r.incomingUnidirectionalStreams,{onData:(u,l)=>{if(u===e.stream){if(l===y.STREAM_END){t.onEnd?.();return}if(!(l instanceof Uint8Array)){t.onLaneError(new Error(`consumeVideoOut: "${e.stream}" delivered a non-BINARY \xA75 payload (${typeof l}) \u2014 spec v1 records ride BINARY envelopes only`));return}try{t.onFrame(x(l))}catch(f){t.onLaneError(f instanceof Error?f:new Error(String(f)))}}},onLaneError:u=>t.onLaneError(u)})}var ee="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",B=1007,te=1008,U=1011,ne=1008,s=class extends Error{constructor(n,o=B){super(n);this.closeCode=o}closeCode},m=e=>typeof e=="object"&&e!==null&&!Array.isArray(e)?e:null,L=["setup","clientContent","realtimeInput","toolResponse"];function oe(e){let t;try{t=JSON.parse(e)}catch{throw new s("client message is not valid JSON")}let n=m(t);if(!n)throw new s("client message must be a JSON object");let o=L.filter(c=>n[c]!==void 0),r=Object.keys(n).filter(c=>!L.includes(c));if(r.length>0)throw new s(`unknown client message field(s) ${JSON.stringify(r)} \u2014 expected exactly one of ${L.join(", ")}`);if(o.length!==1)throw new s(`client message must contain exactly one of ${L.join(", ")} (got ${o.length})`);let i=o[0],a=m(n[i]);if(!a)throw new s(`"${i}" must be a JSON object`);return{kind:i,[i]:a}}function re(e,t){if(typeof e=="string")return e;let n=m(e),o=n?n.parts:void 0;if(!Array.isArray(o))throw new s(`${t}: expected a Content with a parts array`);let r=[];for(let i of o){let a=m(i);if(a&&typeof a.text=="string"){r.push(a.text);continue}throw a&&(a.inlineData!==void 0||a.fileData!==void 0)?new s(`${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 s(`${t}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new s(`${t}: unsupported part ${JSON.stringify(i)}`)}return r.join(`
2
+ `)}var xe={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},k="urun.videoOut";function ie(e){return{framing:C,codec:e}}function se(e){let{seq:t,ptsUs:n,codec:o,key:r}=e.header;return{[k]:{v:1,seq:t,ptsUs:n,codec:o,key:r,data:Buffer.from(e.au).toString("base64")}}}var Ce={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},Le=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption",k],Z=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function Ue(e){let t=m(e);if(!t)throw new s("setup.realtimeInputConfig must be an object");for(let r of Object.keys(t))if(!(r==="automaticActivityDetection"||r==="activityHandling"))throw new s(r==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${r}"`);let n=m(t.automaticActivityDetection);if(t.automaticActivityDetection!==void 0&&!n)throw new s("realtimeInputConfig.automaticActivityDetection must be an object");for(let r of Object.keys(n??{}))if(r!=="disabled")throw new s(`automaticActivityDetection.${r} 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(n?.disabled!==!0)throw new s("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 o="START_OF_ACTIVITY_INTERRUPTS";if(t.activityHandling!==void 0){if(!Z.includes(t.activityHandling))throw new s(`unsupported activityHandling ${JSON.stringify(t.activityHandling)} \u2014 expected one of ${Z.join(", ")}`);o=t.activityHandling}return{manualActivity:!0,activityHandling:o}}var Ve=["temperature","maxOutputTokens","responseModalities"];function ae(e){for(let[f,p]of Object.entries(Ce))if(e[f]!==void 0)throw new s(p);for(let f of Object.keys(e))if(!Le.includes(f))throw new s(`unsupported setup field "${f}"`);if(typeof e.model!="string"||e.model.length===0)throw new s('setup.model is required (format "models/{model}")');let t=e.model.replace(/^models\//,""),n,o,r=!1,i=m(e.generationConfig);if(e.generationConfig!==void 0&&!i)throw new s("setup.generationConfig must be an object");if(i){for(let f of Object.keys(i))if(!Ve.includes(f))throw new s(`unsupported generationConfig field "${f}"`);if(i.temperature!==void 0){if(typeof i.temperature!="number")throw new s("generationConfig.temperature must be a number");n=i.temperature}if(i.maxOutputTokens!==void 0){if(typeof i.maxOutputTokens!="number")throw new s("generationConfig.maxOutputTokens must be a number");o=i.maxOutputTokens}if(i.responseModalities!==void 0){let f=Array.isArray(i.responseModalities)?i.responseModalities:[i.responseModalities];for(let p of f){let g=String(p).toUpperCase();if(g==="AUDIO"){r=!0;continue}if(g!=="TEXT")throw new s(`responseModalities ${JSON.stringify(p)} is not supported \u2014 this surface speaks TEXT and AUDIO (audio-out rides the native AudioBridge rt-audio-out lane)`)}}}let a=[];e.systemInstruction!==void 0&&a.push({role:"system",content:re(e.systemInstruction,"setup.systemInstruction")});let c;if(e.tools!==void 0){if(!Array.isArray(e.tools))throw new s("setup.tools must be an array");c=[];for(let f of e.tools){let p=m(f);if(!p)throw new s("setup.tools entries must be objects");for(let g of Object.keys(p))if(g!=="functionDeclarations")throw new s(`unsupported tool "${g}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(p.functionDeclarations))throw new s("tool.functionDeclarations must be an array");for(let g of p.functionDeclarations){let w=m(g);if(!w||typeof w.name!="string")throw new s("functionDeclarations entries require a string name");c.push({type:"function",name:w.name,description:w.description,parameters:w.parameters})}}c.length===0&&(c=void 0)}let d={enabled:!1,handle:null};if(e.sessionResumption!==void 0){let f=m(e.sessionResumption);if(!f)throw new s("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let p of Object.keys(f))if(p!=="handle")throw new s(`unsupported sessionResumption field "${p}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(f.handle!==void 0&&(typeof f.handle!="string"||f.handle.length===0))throw new s("sessionResumption.handle must be a non-empty string when present");d={enabled:!0,handle:typeof f.handle=="string"?f.handle:null}}let u=e.realtimeInputConfig===void 0?xe:Ue(e.realtimeInputConfig),l=!1;if(e[k]!==void 0){if(!m(e[k]))throw new s(`setup["${k}"] must be an object (the v1 opt-in is {})`);l=!0}return{model:t,systemItems:a,tools:c,temperature:n,maxOutputTokens:o,audioModality:r,activity:u,sessionResumption:d,videoOut:l}}function ue(e){return{sessionResumptionUpdate:{newHandle:e,resumable:!0}}}function de(e){let t=Math.max(0,e??0)/1e3;return{goAway:{timeLeft:`${Math.round(t*1e3)/1e3}s`}}}function ce(e){for(let o of Object.keys(e))if(o!=="turns"&&o!=="turnComplete")throw new s(`unsupported clientContent field "${o}"`);let t=e.turns===void 0?[]:e.turns;if(!Array.isArray(t))throw new s("clientContent.turns must be an array");let n=[];for(let o of t){let r=m(o);if(!r)throw new s("clientContent.turns entries must be Content objects");let i=r.role===void 0?"user":String(r.role).toLowerCase();if(i!=="user"&&i!=="model")throw new s(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(r.role)})`);n.push({role:i==="model"?"assistant":"user",content:re(r,"clientContent.turns")})}return{items:n,turnComplete:e.turnComplete===!0}}var O=16e3,V=24e3,H=`audio/pcm;rate=${V}`,le=Buffer.alloc(960).toString("base64");function j(e,t,n){if(t===n)return e;let o=Math.floor(e.length*n/t),r=new Int16Array(o),i=t/n,a=e.length-1;for(let c=0;c<o;c++){let d=c*i,u=Math.floor(d);if(u>=a){r[c]=e[a];continue}let l=d-u;r[c]=Math.round(e[u]+(e[u+1]-e[u])*l)}return r}function De(e){let t=m(e);if(!t)throw new s("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof t.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(t.mimeType))throw new s(`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 s("realtimeInput.video.data must be non-empty base64");let n=Buffer.from(t.data,"base64");if(n.length<3||n[0]!==255||n[1]!==216)throw new s("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(n)}function Ne(e){let t=m(e);if(!t)throw new s("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof t.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(t.mimeType))throw new s(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(t.mimeType)}`);let n=/;\s*rate=(\d+)/.exec(t.mimeType);if(n&&Number(n[1])!==O)throw new s(`realtimeInput.audio.mimeType declares rate=${n[1]} \u2014 the Live spec input format is ${O} Hz (audio/pcm;rate=${O})`);if(typeof t.data!="string"||t.data.length===0)throw new s("realtimeInput.audio.data must be non-empty base64");let o=Buffer.from(t.data,"base64");if(o.length===0||o.length%2!==0)throw new s(`realtimeInput.audio.data must decode to whole 16-bit samples (got ${o.length} bytes \u2014 not even)`);let r=new Int16Array(o.length/2);for(let a=0;a<r.length;a++)r[a]=o.readInt16LE(a*2);let i=j(r,O,V);return Buffer.from(i.buffer,i.byteOffset,i.byteLength).toString("base64")}function fe(e){if(e.mediaChunks!==void 0)throw new s('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 s("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let r=e.activityStart!==void 0?"activityStart":"activityEnd",i=m(e[r]);if(!i||Object.keys(i).length!==0)throw new s(`realtimeInput.${r} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:r}}let t=["text","audio","audioStreamEnd","video"];for(let o of Object.keys(e))if(!t.includes(o))throw new s(`unsupported realtimeInput field "${o}"`);let n=t.filter(o=>e[o]!==void 0);if(n.length!==1)throw new s(`realtimeInput must carry exactly one of ${t.join(", ")} (got ${n.length})`);if(e.audio!==void 0)return{kind:"audio",base64Pcm24k:Ne(e.audio)};if(e.video!==void 0)return{kind:"video",jpegFrame:De(e.video)};if(e.audioStreamEnd!==void 0){if(e.audioStreamEnd!==!0)throw new s("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof e.text!="string"||e.text.length===0)throw new s("realtimeInput.text must be a non-empty string");return{kind:"text",text:e.text}}function pe(e){for(let t of Object.keys(e))if(t!=="functionResponses")throw new s(`unsupported toolResponse field "${t}"`);if(!Array.isArray(e.functionResponses)||e.functionResponses.length===0)throw new s("toolResponse.functionResponses must be a non-empty array");return e.functionResponses.map(t=>{let n=m(t);if(!n||typeof n.id!="string"||n.id.length===0)throw new s("functionResponses entries require the string id matched from the toolCall");return{type:"function_call_output",call_id:n.id,output:JSON.stringify(n.response??null)}})}function me(e){return e.type!=="response.output_text.delta"||typeof e.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:e.delta}]}}}}function ge(e){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:H,data:e}}]}}}}function ye(e){let t=m(e)??{},n=Array.isArray(t.output)?t.output:[],o=[],r=[],i=typeof t.output_text=="string"?t.output_text:"";if(!i)for(let u of n){let l=m(u);if(!(l?.type!=="message"||!Array.isArray(l.content)))for(let f of l.content){let p=m(f);p?.type==="output_text"&&typeof p.text=="string"&&(i+=p.text)}}i&&r.push({role:"assistant",content:i});for(let u of n){let l=m(u);if(l?.type!=="function_call")continue;if(typeof l.call_id!="string"||typeof l.name!="string")throw new s(`upstream function_call item is missing call_id/name: ${JSON.stringify(u)}`,U);let f=typeof l.arguments=="string"&&l.arguments.length>0?l.arguments:"{}",p;try{p=JSON.parse(f)}catch{throw new s(`upstream function_call arguments are not valid JSON: ${f}`,U)}o.push({id:l.call_id,name:l.name,args:p}),r.push({type:"function_call",call_id:l.call_id,name:l.name,arguments:f})}let a=[];o.length>0&&a.push({toolCall:{functionCalls:o}}),a.push({serverContent:{generationComplete:!0}});let c=m(t.usage),d={serverContent:{turnComplete:!0}};if(c){let u={};typeof c.input_tokens=="number"&&(u.promptTokenCount=c.input_tokens),typeof c.output_tokens=="number"&&(u.responseTokenCount=c.output_tokens),typeof c.total_tokens=="number"&&(u.totalTokenCount=c.total_tokens),Object.keys(u).length>0&&(d.usageMetadata=u)}return a.push(d),{messages:a,assistantItems:r}}function D(e,t,n,o){let r=e.response??{},i={request_id:n,consumer_id:o,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 N(e,t,n){let o={request_id:t,consumer_id:n,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(o.model=e.model),e.tools!==void 0&&(o.tools=e.tools),e.tool_choice!==void 0&&(o.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(o.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(o.max_output_tokens=e.max_output_tokens),typeof e.reasoning_effort=="string"&&(o.reasoning_effort=e.reasoning_effort),e.chat_template_kwargs!==void 0&&(o.chat_template_kwargs=e.chat_template_kwargs),o}function we(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,n=t.body??{};return{type:"error",error:{type:"urun_error",code:t.code??null,message:n.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}var Me="llm-resp";async function*v(e,t){let n=`${Me}:${t}`,o=`resp_${t}`;yield{type:"response.created",response:{id:o,status:"in_progress"}};let r=new Map;for await(let i of e.stream(n).messages()){let a=i;if(a.t==="delta"){if(typeof a.delta=="string"&&(yield{type:"response.output_text.delta",item_id:o,delta:a.delta}),typeof a.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:o,delta:a.reasoning}),Array.isArray(a.tool_calls))for(let c of a.tool_calls){let d=typeof c.index=="number"?c.index:0,u=r.get(d)??{};c.id&&(u.id=c.id),c.function?.name&&(u.name=c.function.name),r.set(d,u),yield{type:"response.function_call_arguments.delta",item_id:`fc_${t}_${d}`,tool_index:d,call_id:u.id,name:u.name,delta:c.function?.arguments??""}}}else if(a.t==="response"){yield{type:"response.completed",response:{id:o,status:"completed",...a.body}};return}else if(a.t==="error"){yield we(a);return}}}var $e="llm",M=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($e).set({requests:{[t.request_id]:{payload:t,consumer_id:t.consumer_id,stream:t.stream}}})}sendResponses(t,n){let o=N(t,n,this.consumerId);return this.write(o),v(this.session,n)}sendResponseCreate(t,n,o){let r=D(t,n,o,this.consumerId);return this.write(r),v(this.session,o)}};0&&(module.exports={AudioBridge,CLOSE_PROTOCOL,CLOSE_SESSION_GONE,CLOSE_UNKNOWN_MODEL,CLOSE_UPSTREAM,GEMINI_AUDIO_IN_RATE,GEMINI_AUDIO_OUT_MIME,GEMINI_AUDIO_OUT_RATE,GEMINI_LIVE_PATH,GeminiLiveError,NATIVE_AUDIO_SAMPLE_RATE,OUTBOUND_AUDIO_FLUSH_B64,RT_VIDEO_IN_STREAM,RT_VIDEO_OUT_STREAM,SdkTransport,VIDEO_FRAME_MESSAGE_BUDGET,VIDEO_OUT_DEFAULT_CODEC,VIDEO_OUT_FRAMING,VIDEO_OUT_SETUP_KEY,VIDEO_OUT_WS_FRAMING,consumeVideoOut,decodeResponsesLane,decodeVideoOutRecord,enableSessionAudio,enableSessionVideo,encodeVideoOutRecord,goAwayMsg,negotiateSessionVideoOutQuic,nodeAudioBackend,openSessionVideoOutLane,parseClientMessage,resamplePcm16,responseCreateToEnvelope,responsesCreateToEnvelope,serverContentOfAudio,serverContentOfDelta,sessionResumptionUpdateMsg,translateClientContent,translateRealtimeInput,translateSetup,translateToolResponse,turnEndOfCompleted,videoOutAck,videoOutChunkMsg,weriftAudioBackend});
@@ -0,0 +1,120 @@
1
+ export { A as ActivityConfig, a as ActivityHandling, C as CLOSE_PROTOCOL, b as CLOSE_SESSION_GONE, c as CLOSE_UNKNOWN_MODEL, d as CLOSE_UPSTREAM, e as ClientMessage, G as GEMINI_AUDIO_IN_RATE, f as GEMINI_AUDIO_OUT_MIME, g as GEMINI_AUDIO_OUT_RATE, h as GEMINI_LIVE_PATH, i as GeminiLiveError, L as LiveSessionConfig, O as OUTBOUND_AUDIO_FLUSH_B64, R as RealtimeInputAction, T as TurnEnd, V as VIDEO_OUT_SETUP_KEY, j as goAwayMsg, p as parseClientMessage, r as resamplePcm16, s as serverContentOfAudio, k as serverContentOfDelta, l as sessionResumptionUpdateMsg, t as translateClientContent, m as translateRealtimeInput, n as translateSetup, o as translateToolResponse, q as turnEndOfCompleted, v as videoOutAck, u as videoOutChunkMsg } from './translator-CcDBEfvm.cjs';
2
+ import { b as UrunSessionLike } from './types-lsVTbNcH.cjs';
3
+ export { L as LaneMessage, a as LaneToolCallDelta, U as UrunDocLike, c as UrunStreamLike } from './types-lsVTbNcH.cjs';
4
+ export { A as AudioBackend, a as AudioBridge, b as AudioSink, c as AudioSource, N as NATIVE_AUDIO_SAMPLE_RATE, R as RT_VIDEO_IN_STREAM, d as RT_VIDEO_OUT_STREAM, V as VIDEO_FRAME_MESSAGE_BUDGET, e as VIDEO_OUT_DEFAULT_CODEC, f as VIDEO_OUT_FRAMING, g as VIDEO_OUT_WS_FRAMING, h as VideoFrameLane, i as VideoOutEndpoint, j as VideoOutLane, k as VideoOutRecord, l as VideoOutRecordHeader, m as VideoOutTransportSink, n as VideoOutWtConnect, o as VideoOutWtSession, p as consumeVideoOut, q as decodeVideoOutRecord, r as enableSessionAudio, s as enableSessionVideo, t as encodeVideoOutRecord, u as negotiateSessionVideoOutQuic, v as nodeAudioBackend, w as openSessionVideoOutLane, x as weriftAudioBackend } from './video-out-D20UuJ8G.cjs';
5
+
6
+ declare class SdkTransport {
7
+ private readonly session;
8
+ /**
9
+ * Session-affinity tag (urun-python #1556): every envelope this transport
10
+ * writes carries the SAME stable tag, so the serve engine adapter can
11
+ * continue the session's retained sequence state instead of re-prefilling
12
+ * the whole conversation each turn. The uRun session id when the SDK
13
+ * exposes it; otherwise minted ONCE here — one transport wraps exactly one
14
+ * persistent session, so a per-transport tag identifies the session too.
15
+ */
16
+ private readonly sessionTag;
17
+ constructor(session: UrunSessionLike);
18
+ get consumerId(): string;
19
+ private write;
20
+ sendResponses(params: {
21
+ model?: string;
22
+ input: unknown;
23
+ stream?: boolean;
24
+ tools?: unknown;
25
+ tool_choice?: unknown;
26
+ temperature?: number;
27
+ max_output_tokens?: number;
28
+ reasoning_effort?: string;
29
+ chat_template_kwargs?: Record<string, unknown>;
30
+ }, requestId: string): AsyncIterable<unknown>;
31
+ sendResponseCreate(ev: {
32
+ type: 'response.create';
33
+ response?: Record<string, unknown>;
34
+ }, conversation: Array<{
35
+ role: string;
36
+ content: unknown;
37
+ }>, requestId: string): AsyncIterable<unknown>;
38
+ }
39
+
40
+ /**
41
+ * Read the addressed lane and yield OpenAI Responses server-events.
42
+ *
43
+ * Delta messages decode field-by-field (a single lane message may carry
44
+ * several):
45
+ * - `delta` → `response.output_text.delta` (visible text, unchanged)
46
+ * - `reasoning` → `response.reasoning_text.delta` (OpenAI Responses
47
+ * reasoning-stream vocabulary)
48
+ * - `tool_calls` → `response.function_call_arguments.delta`, ENRICHED with
49
+ * `tool_index` / `call_id` / `name` (latched per index —
50
+ * chunk-style fragments carry id/name only on the first
51
+ * fragment). The full output_item lifecycle around these is
52
+ * synthesized by each consumer leg (proxy/pi), NOT here:
53
+ * emitting `response.output_item.added` from the decoder
54
+ * would flip the proxy's upstream-speaks-envelope
55
+ * passthrough while the text item still needs synthesis.
56
+ *
57
+ * Unknown fields on a delta message are ignored (a NEWER server against this
58
+ * decoder must degrade to whatever fields we know, never throw).
59
+ */
60
+ declare function decodeResponsesLane(session: UrunSessionLike, requestId: string): AsyncIterable<unknown>;
61
+
62
+ interface ServeEnvelope {
63
+ request_id: string;
64
+ consumer_id: string;
65
+ stream: boolean;
66
+ kind: 'chat' | 'responses';
67
+ model?: string;
68
+ messages?: Array<{
69
+ role: string;
70
+ content: unknown;
71
+ }>;
72
+ input?: unknown;
73
+ tools?: unknown;
74
+ tool_choice?: unknown;
75
+ temperature?: number;
76
+ max_output_tokens?: number;
77
+ modalities?: string[];
78
+ instructions?: string;
79
+ /**
80
+ * Reasoning controls (urun-python #1667, additive like `tools`): forwarded
81
+ * VERBATIM to the serve envelope. `chat_template_kwargs` reaches the
82
+ * tokenizer's own `apply_chat_template` (`{"enable_thinking": false}` is the
83
+ * Qwen3 think-off switch); `reasoning_effort` is the OpenAI top-level field
84
+ * delivered through the same template mechanism. Absent -> keys absent —
85
+ * the server's defaults apply, no client-side injection.
86
+ */
87
+ reasoning_effort?: string;
88
+ chat_template_kwargs?: Record<string, unknown>;
89
+ /**
90
+ * Session-affinity tag (urun-python #1556, additive like `tools` in the
91
+ * #1557 lane pair): the stable identity of THIS persistent uRun session.
92
+ * The serve engine adapter keys retained sequence state (KV + hybrid
93
+ * recurrent state) on it so consecutive turns prefill only their delta.
94
+ * Attached by the transport (one transport = one session = one tag);
95
+ * absent -> the server treats the request as stateless, today's behavior.
96
+ */
97
+ session_tag?: string;
98
+ }
99
+ /** Realtime: a response.create + the accumulated conversation turns → envelope. */
100
+ declare function responseCreateToEnvelope(ev: {
101
+ type: 'response.create';
102
+ response?: Record<string, unknown>;
103
+ }, conversation: Array<{
104
+ role: string;
105
+ content: unknown;
106
+ }>, requestId: string, consumerId: string): ServeEnvelope;
107
+ /** Responses: client.responses.create params → envelope. */
108
+ declare function responsesCreateToEnvelope(params: {
109
+ model?: string;
110
+ input: unknown;
111
+ stream?: boolean;
112
+ tools?: unknown;
113
+ tool_choice?: unknown;
114
+ temperature?: number;
115
+ max_output_tokens?: number;
116
+ reasoning_effort?: string;
117
+ chat_template_kwargs?: Record<string, unknown>;
118
+ }, requestId: string, consumerId: string): ServeEnvelope;
119
+
120
+ export { SdkTransport, type ServeEnvelope, UrunSessionLike, decodeResponsesLane, responseCreateToEnvelope, responsesCreateToEnvelope };
@@ -0,0 +1,79 @@
1
+ export { A as ActivityConfig, a as ActivityHandling, C as CLOSE_PROTOCOL, b as CLOSE_SESSION_GONE, c as CLOSE_UNKNOWN_MODEL, d as CLOSE_UPSTREAM, e as ClientMessage, G as GEMINI_AUDIO_IN_RATE, f as GEMINI_AUDIO_OUT_MIME, g as GEMINI_AUDIO_OUT_RATE, h as GEMINI_LIVE_PATH, i as GeminiLiveError, L as LiveSessionConfig, O as OUTBOUND_AUDIO_FLUSH_B64, R as RealtimeInputAction, T as TurnEnd, V as VIDEO_OUT_SETUP_KEY, j as goAwayMsg, p as parseClientMessage, r as resamplePcm16, s as serverContentOfAudio, k as serverContentOfDelta, l as sessionResumptionUpdateMsg, t as translateClientContent, m as translateRealtimeInput, n as translateSetup, o as translateToolResponse, q as turnEndOfCompleted, v as videoOutAck, u as videoOutChunkMsg } from './translator-C9uPKypK.js';
2
+ import { b as UrunSessionLike } from './types-lsVTbNcH.js';
3
+ export { L as LaneMessage, a as LaneToolCallDelta, U as UrunDocLike, c as UrunStreamLike } from './types-lsVTbNcH.js';
4
+ export { A as AudioBackend, a as AudioBridge, b as AudioSink, c as AudioSource, N as NATIVE_AUDIO_SAMPLE_RATE, R as RT_VIDEO_IN_STREAM, d as RT_VIDEO_OUT_STREAM, V as VIDEO_FRAME_MESSAGE_BUDGET, e as VIDEO_OUT_DEFAULT_CODEC, f as VIDEO_OUT_FRAMING, g as VIDEO_OUT_WS_FRAMING, h as VideoFrameLane, i as VideoOutEndpoint, j as VideoOutLane, k as VideoOutRecord, l as VideoOutRecordHeader, m as VideoOutTransportSink, n as VideoOutWtConnect, o as VideoOutWtSession, p as consumeVideoOut, q as decodeVideoOutRecord, r as enableSessionAudio, s as enableSessionVideo, t as encodeVideoOutRecord, u as negotiateSessionVideoOutQuic, v as nodeAudioBackend, w as openSessionVideoOutLane, x as weriftAudioBackend } from './video-out-CWesbk12.js';
5
+
6
+ declare class SdkTransport {
7
+ private readonly session;
8
+
9
+ private readonly sessionTag;
10
+ constructor(session: UrunSessionLike);
11
+ get consumerId(): string;
12
+ private write;
13
+ sendResponses(params: {
14
+ model?: string;
15
+ input: unknown;
16
+ stream?: boolean;
17
+ tools?: unknown;
18
+ tool_choice?: unknown;
19
+ temperature?: number;
20
+ max_output_tokens?: number;
21
+ reasoning_effort?: string;
22
+ chat_template_kwargs?: Record<string, unknown>;
23
+ }, requestId: string): AsyncIterable<unknown>;
24
+ sendResponseCreate(ev: {
25
+ type: 'response.create';
26
+ response?: Record<string, unknown>;
27
+ }, conversation: Array<{
28
+ role: string;
29
+ content: unknown;
30
+ }>, requestId: string): AsyncIterable<unknown>;
31
+ }
32
+
33
+ declare function decodeResponsesLane(session: UrunSessionLike, requestId: string): AsyncIterable<unknown>;
34
+
35
+ interface ServeEnvelope {
36
+ request_id: string;
37
+ consumer_id: string;
38
+ stream: boolean;
39
+ kind: 'chat' | 'responses';
40
+ model?: string;
41
+ messages?: Array<{
42
+ role: string;
43
+ content: unknown;
44
+ }>;
45
+ input?: unknown;
46
+ tools?: unknown;
47
+ tool_choice?: unknown;
48
+ temperature?: number;
49
+ max_output_tokens?: number;
50
+ modalities?: string[];
51
+ instructions?: string;
52
+
53
+ reasoning_effort?: string;
54
+ chat_template_kwargs?: Record<string, unknown>;
55
+
56
+ session_tag?: string;
57
+ }
58
+
59
+ declare function responseCreateToEnvelope(ev: {
60
+ type: 'response.create';
61
+ response?: Record<string, unknown>;
62
+ }, conversation: Array<{
63
+ role: string;
64
+ content: unknown;
65
+ }>, requestId: string, consumerId: string): ServeEnvelope;
66
+
67
+ declare function responsesCreateToEnvelope(params: {
68
+ model?: string;
69
+ input: unknown;
70
+ stream?: boolean;
71
+ tools?: unknown;
72
+ tool_choice?: unknown;
73
+ temperature?: number;
74
+ max_output_tokens?: number;
75
+ reasoning_effort?: string;
76
+ chat_template_kwargs?: Record<string, unknown>;
77
+ }, requestId: string, consumerId: string): ServeEnvelope;
78
+
79
+ export { SdkTransport, type ServeEnvelope, UrunSessionLike, decodeResponsesLane, responseCreateToEnvelope, responsesCreateToEnvelope };
@@ -0,0 +1 @@
1
+ import{a as c,b as C,c as L,d as M,e as R,f as D,g as f,h as N,i as v,j as g,k as x,l as k,m as y,n as G,o as B,p as F,q as P,r as H,s as W,t as b,u as w,v as K,w as h,x as Q}from"./chunk-5NXM4IO3.js";import{a as i,b as s,c as O,d,e as a,f as E,g as _,h as p,i as u,j as A,k as S,l as I,m as T,n as l,o as m,p as U,q as V}from"./chunk-CWJRDBDC.js";import{a as o,b as t,d as n,e as r}from"./chunk-UVAY7Q7Z.js";import{c as e}from"./chunk-YSFSRI3D.js";e();export{s as AudioBridge,C as CLOSE_PROTOCOL,R as CLOSE_SESSION_GONE,L as CLOSE_UNKNOWN_MODEL,M as CLOSE_UPSTREAM,B as GEMINI_AUDIO_IN_RATE,P as GEMINI_AUDIO_OUT_MIME,F as GEMINI_AUDIO_OUT_RATE,c as GEMINI_LIVE_PATH,D as GeminiLiveError,i as NATIVE_AUDIO_SAMPLE_RATE,H as OUTBOUND_AUDIO_FLUSH_B64,d as RT_VIDEO_IN_STREAM,u as RT_VIDEO_OUT_STREAM,r as SdkTransport,a as VIDEO_FRAME_MESSAGE_BUDGET,S as VIDEO_OUT_DEFAULT_CODEC,A as VIDEO_OUT_FRAMING,N as VIDEO_OUT_SETUP_KEY,m as VIDEO_OUT_WS_FRAMING,V as consumeVideoOut,n as decodeResponsesLane,T as decodeVideoOutRecord,O as enableSessionAudio,E as enableSessionVideo,I as encodeVideoOutRecord,y as goAwayMsg,l as negotiateSessionVideoOutQuic,p as nodeAudioBackend,U as openSessionVideoOutLane,f as parseClientMessage,W as resamplePcm16,o as responseCreateToEnvelope,t as responsesCreateToEnvelope,h as serverContentOfAudio,K as serverContentOfDelta,k as sessionResumptionUpdateMsg,G as translateClientContent,b as translateRealtimeInput,x as translateSetup,w as translateToolResponse,Q as turnEndOfCompleted,v as videoOutAck,g as videoOutChunkMsg,_ as weriftAudioBackend};