@urun-sh/openai 0.5.3 → 0.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-5BZCM3RS.js +4 -0
- package/dist/chunk-DMD5UENK.js +1 -0
- package/dist/{chunk-G3NMGP4N.js → chunk-GBBY3PCZ.js} +1 -1
- package/dist/chunk-I2Q3B3OG.js +6 -0
- package/dist/chunk-K23AZPI4.js +1 -0
- package/dist/chunk-OI2OY32M.js +1 -0
- package/dist/chunk-QVF7NF7G.js +44 -0
- package/dist/gemini-live.cjs +2 -2
- package/dist/gemini-live.d.cts +6 -0
- package/dist/gemini-live.d.ts +6 -0
- package/dist/gemini-live.js +1 -1
- package/dist/hosted/bin.cjs +36 -22
- package/dist/hosted/bin.js +4 -4
- package/dist/hosted/index.cjs +32 -18
- package/dist/hosted/index.d.cts +140 -198
- package/dist/hosted/index.d.ts +63 -82
- package/dist/hosted/index.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -18
- package/dist/index.d.ts +2 -18
- package/dist/index.js +1 -1
- package/dist/models-NYMZrklp.d.cts +38 -0
- package/dist/models-NYMZrklp.d.ts +38 -0
- package/dist/pi-extension/index.cjs +6 -6
- package/dist/pi-extension/index.js +1 -1
- package/dist/pi-extension/standalone.cjs +49 -49
- package/dist/proxy/cli.cjs +41 -37
- package/dist/proxy/cli.js +1 -1
- package/dist/proxy/index.cjs +30 -21
- package/dist/proxy/index.d.cts +468 -2
- package/dist/proxy/index.d.ts +173 -2
- package/dist/proxy/index.js +6 -1
- package/dist/responses-turn-KOAoIqZ-.d.ts +200 -0
- package/dist/responses-turn-OrO4euEN.d.cts +513 -0
- package/package.json +3 -3
- package/dist/chunk-F2TEK34X.js +0 -1
- package/dist/chunk-FVD4NMXJ.js +0 -1
- package/dist/chunk-M6ICU4F5.js +0 -40
- package/dist/chunk-ODHCPZHI.js +0 -6
- package/dist/chunk-UVAY7Q7Z.js +0 -1
- package/dist/chunk-XHIIEA6Z.js +0 -1
- package/dist/server-BfME37pQ.d.cts +0 -200
- package/dist/server-l1himPxc.d.ts +0 -82
package/dist/chunk-M6ICU4F5.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
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};
|
package/dist/chunk-ODHCPZHI.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import{a as b}from"./chunk-F2TEK34X.js";import{a as E}from"./chunk-G3NMGP4N.js";import{a as R,c as h,d as P,e as _}from"./chunk-XHIIEA6Z.js";import{c as x,f as v,g as S,p as U}from"./chunk-CWJRDBDC.js";import{c as l}from"./chunk-YSFSRI3D.js";l();import{App as O,createClientToken as B}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:r}=await e.sessionFor(t.model),i=!1,u=!1;try{let c=await e.createStream(r,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,r);continue}throw c}if(i)return;if(a<2&&!u){await e.evict(s,r);continue}return}})()}}l();var M=3,F="llm",W=new Set(["paused","error"]);function A(e,n){let t=e.doc(F),o=typeof t.onConnectionState=="function",a=typeof e.onPhase=="function";if(!o&&!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,r=new Set,i=[],u=c=>{if(!s){s=c;for(let d of[...r])d(c);r.clear()}};return o&&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}" lost its compute binding (phase "${c.name}"). The entry is evicted; retry to get a fresh session. The named session itself is not over \u2014 it stays attachable by name.`))})),{gone:()=>s,onGone(c){return s?(c(s),()=>{}):(r.add(c),()=>r.delete(c))},dispose(){r.clear();for(let c of i.splice(0))c()}}}async function*g(e,n){let t=n.gone();if(t)throw t;let o=e[Symbol.asyncIterator](),a,s=new Promise((r,i)=>{a=n.onGone(u=>i(u))});s.catch(()=>{});try{for(;;){let r=await Promise.race([o.next(),s]);if(r.done)return;yield r.value}}finally{a?.(),o.return?.(void 0)}}function N(e,n,t){let o=e[t];if(typeof o!="function")throw new Error(`app "${n}" has no function "${t}" (set URUN_FUNCTION)`);let a=o();if(typeof a.end!="function")throw new Error(`app "${n}" function "${t}" returned a session without end()`);return a}function H(e,n){let{baseUrl:t,orgId:o,fnName:a,auth:s,subject:r}=e,i=p=>({session:p,responses:new E(p),gone:A(p,n)});if(s.lane==="jwt"){let p=O(n,{baseUrl:t,orgId:o,jwt:s.jwt});return i(N(p,n,a))}let{apiKey:u,gatewayUrl:c}=s,d=O(n,{baseUrl:t,orgId:o,getAccessToken:async()=>(await B(u,{baseUrl:c,expiresIn:300,allowedFunctions:[`${n}/${a}`],...r===void 0?{}:{subject:r}})).token});return i(N(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","paused"]);function xe(e){let{auth:n,catalog:t}=e,o=null,a=r=>{let i=H(e,r);return i.gone.onGone(()=>{o?.evict(r,i)}),i},s=new _({defaultApp:e.defaultApp,fnName:e.fnName,openSession:a,closeSession:async r=>{r.gone.dispose(),await r.session.end()},listApps:n.lane==="api-key"?()=>P({apiUrl:e.apiUrl,apiKey:n.apiKey}):null,listCatalog:t?()=>b(t):null,sessionKey:K});return o=s,e.defaultApp!==null&&s.seed(e.defaultApp,a(e.defaultApp)),s}function ve(e,n=S){let t=new WeakMap,o=new WeakMap,a=new WeakMap;return{createResponse:k({sessionFor:s=>e.sessionFor(s),evict:(s,r)=>e.evict(s,r),createStream:async(s,r)=>g(await s.responses.responses.create(r),s.gone)}),listModels:()=>e.modelList(),openAudio:async s=>{let{entry:r}=await e.sessionFor(s),i=t.get(r);return i||(i=(async()=>x(r.session,await n()))(),t.set(r,i),i.catch(()=>t.delete(r))),i},openVideo:async s=>{let{entry:r}=await e.sessionFor(s),i=o.get(r);return i||(i=v(r.session),o.set(r,i)),i},openVideoOut:async s=>{let{entry:r}=await e.sessionFor(s),i=a.get(r);return i||(i=U(r.session),a.set(r,i)),i},sessionHandle:async s=>(await e.handleFor(s)).handle,createResponseOn:async(s,r)=>{let{entry:i}=await e.sessionForHandle(s);return g(await i.responses.responses.create(r),i.gone)},onSessionEnd:async(s,r)=>{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;r({timeLeftMs:p?Math.max(0,p.getTime()-Date.now()):null,reason:d.reason??`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 I="0.5.3";var C=4141,Q=I,Oe={claude:{bin:"claude",env:"anthropic"},codex:{bin:"codex",env:"openai"},pi:{bin:"pi",env:"openai"},omp:{bin:"omp",env:"openai"},aider:{bin:"aider",env:"openai"},opencode:{bin:"opencode",env:"openai"},crush:{bin:"crush",env:"openai"}};function 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 Ne(e,n,t){let o={...e,...Z(n,t)};return delete o.URUN_API_KEY,delete o.URUN_JWT,o}function Ie(e,n,t){let o=["-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),r=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[...o,...r?[]:["-m",n],...c]}function $e(e,n){return n.some(o=>o==="--model"||o==="--provider"||o.startsWith("--model=")||o.startsWith("--provider="))?n:["--provider","urun","--model",e,...n]}function Ce(e,n){return n.some(o=>o==="--model"||o==="--provider"||o.startsWith("--model=")||o.startsWith("--provider="))?n:["--model",`urun/${e}`,...n]}function Te(e,n,t){let o=(e.URUN_PI_EXTENSION_ENTRY??"").trim();if(o){if(!t(o))throw new Error(`URUN_PI_EXTENSION_ENTRY points at ${o}, 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 o}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 T(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 Ge(e){if(e[0]!=="--port")return null;let n=T(e[1]);return e.splice(0,2),n}function Le(e){let n=e.indexOf("--port");if(n===-1)return C;let t=T(e[n+1]);return e.splice(n,2),t}function je(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 De(e){let n=ee(y(e,"URUN_BASE_URL")),t=y(e,"URUN_ORG_ID"),o=y(e,"URUN_APP"),a=(e.URUN_FUNCTION??"serve").trim()||"serve",s=(e.URUN_API_URL??"").trim(),r=s?$(s,"URUN_API_URL"):R,i=(e.URUN_JWT??"").trim();if(i)return{baseUrl:n,orgId:t,appId:o,fnName:a,apiUrl:r,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:o,fnName:a,apiUrl:r,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),o=t.hostname==="localhost"||t.hostname==="127.0.0.1"||t.hostname==="[::1]";if(t.protocol!=="https:"&&!o)throw new Error(`${n} must be https (got ${e}) \u2014 the org API key rides the request's Authorization header`);return e}function Me(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 o=(await t.json()).identity;if(o==null||typeof o!="object")return null;let{app:a,org:s,fn:r,base_url:i,proxy_version:u}=o;return typeof a!="string"||typeof s!="string"||typeof r!="string"||typeof i!="string"||typeof u!="string"?null:{app:a,org:s,fn:r,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 Fe(e,n,t=C,o=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,o);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 We(e,n=V.homedir()){return J.join(n,".urun","logs",`compat-proxy-${e}.log`)}var w=["log","info","warn","error","debug","trace"];function Be(e,n=process,t=console){let o=n.stdout.write,a=n.stderr.write,s=(i,...u)=>e.write(i,...u);n.stdout.write=s,n.stderr.write=s;let r=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=o,n.stderr.write=a;for(let i of w)t[i]=r[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 Ke(e){let n={stdout:!1,stderr:!1},t=[];for(let o of["stdout","stderr"]){let a=e.streams[o],s=r=>{L(r)&&(n[o]=!0);let i=r?.code;try{e.note(`${new Date().toISOString()} urun compat: process.${o} died (${String(i??(r instanceof Error?r.message:r))}) \u2014 the terminal is gone; launcher output continues in this log only
|
|
3
|
-
`)}catch{}};a.on("error",s),t.push([a,s])}return{isDead:o=>n[o],uninstall:()=>{for(let[o,a]of t)o.off("error",a)}}}function qe(e){let n=t=>{let o=t instanceof Error?`${t.stack??`${t.name}: ${t.message}`}`:String(t);try{e.appendSync(e.logPath,`${new Date().toISOString()} launcher crashed (in-process proxy): ${o}
|
|
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 Ye(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,ve as b,Oe as c,Z as d,Ne as e,Ie as f,$e as g,Ce as h,Te as i,Ge as j,Le as k,je as l,De as m,ee as n,$ as o,Me as p,oe as q,Fe as r,We as s,Be as t,He as u,Ke as v,qe as w,Ye as x};
|
package/dist/chunk-UVAY7Q7Z.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
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};
|
package/dist/chunk-XHIIEA6Z.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{c as f}from"./chunk-YSFSRI3D.js";f();var A="https://api.urun.sh/v1";function c(o){return o.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}var p=class extends Error{},l=class extends Error{},u="urs1.";function y(o,t){return u+Buffer.from(JSON.stringify([o,t]),"utf8").toString("base64url")}function m(o){if(!o.startsWith(u))return null;try{let t=JSON.parse(Buffer.from(o.slice(u.length),"base64url").toString("utf8"));if(Array.isArray(t)&&typeof t[0]=="string"&&typeof t[1]=="string")return{app:t[0],key:t[1]}}catch{}return null}async function $(o){let t=o.fetchImpl??fetch,e=`${o.apiUrl.replace(/\/+$/,"")}/apps`,s=await t(e,{headers:{Authorization:`Bearer ${o.apiKey}`,Accept:"application/json"}});if(!s.ok)throw new Error(`org apps listing failed: GET ${e} \u2192 ${s.status}`);let n=await s.json();if(!Array.isArray(n.apps))throw new Error(`org apps listing returned no "apps" array (GET ${e})`);if(n.truncated===!0)throw new Error(`org apps listing was truncated (GET ${e} returned ${n.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return n.apps}var w=6e4,g=class{constructor(t){this.opts=t}opts;pool=new Map;appsCache=null;seed(t,e){this.pool.set(t,Promise.resolve(e))}async deployedApps(){if(!this.opts.listApps)return[];let t=this.opts.appsTtlMs??w;if(this.appsCache&&Date.now()-this.appsCache.at<t)return this.appsCache.apps;let e=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:e},e}servable(t){return t.filter(e=>e.function_name===this.opts.fnName&&e.deployment_status==="active").map(e=>e.app_slug)}async availableIds(){let t=this.opts.defaultApp,e=new Set(t===null?[]:[t]);if(this.opts.listApps)for(let n of this.servable(await this.deployedApps()))e.add(n);for(let n of this.pool.keys())e.add(n);let s=[...e].filter(n=>n!==t).sort();return t===null?s:[t,...s]}async noDefaultApp(t){let e=await this.availableIds(),s=e.length>0?e.join(", "):"(none deployed)";throw new p(t===""?`no "model" was given and this endpoint has no default model \u2014 name one of your org's deployed "${this.opts.fnName}" apps; available models: ${s}`:`model "${t}" does not name a deployed "${this.opts.fnName}" app in your org, and this endpoint has no default model to fall back to; available models: ${s}`)}async resolveApp(t){let e=(t??"").trim(),s=this.opts.defaultApp;if(!e||e==="urun")return s===null?this.noDefaultApp(e):s;let n=c(e);if(n===s)return s;if(this.pool.has(n))return n;if(this.opts.listApps){let a=await this.deployedApps(),r=this.servable(a);if(r.includes(n))return n;let i=r.filter(d=>d.startsWith(`${n}-`));if(i.length===1)return i[0];if(i.length>1)throw new p(`model "${e}" is ambiguous across deployed apps ${i.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let h=a.find(d=>d.app_slug===n||d.app_slug.startsWith(`${n}-`));if(h)throw new p(`model "${e}" maps to app "${h.app_slug}", which is not an active "${this.opts.fnName}" app \u2014 deploy it with \`urun serve ${e}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.listCatalog){let r=(await this.opts.listCatalog()).find(i=>e===i.model_id||e===`${i.model_id}:${i.variant}`||n===c(i.model_id)||n===c(`${i.model_id}-${i.variant}`));if(r)throw new p(`model "${e}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${r.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return s===null?this.noDefaultApp(e):s}async sessionFor(t){let e=await this.resolveApp(t),s=this.pool.get(e);return s||(s=Promise.resolve(this.opts.openSession(e)),this.pool.set(e,s),s.catch(()=>this.pool.delete(e))),{app:e,entry:await s}}keyOf(t){let e=this.opts.sessionKey;if(!e)throw new Error("ModelRouter: sessionKey is not configured \u2014 the session-identity seam (handleFor/sessionForHandle) is unavailable on this router");let s=e(t);if(typeof s!="string"||s.length===0)throw new Error("ModelRouter: sessionKey returned an empty identity for a pooled session");return s}async handleFor(t){let{app:e,entry:s}=await this.sessionFor(t);return{app:e,handle:y(e,this.keyOf(s))}}async sessionForHandle(t){let e=m(t);if(!e)throw new l(`malformed session handle ${JSON.stringify(t)} \u2014 not issued by this proxy`);let{app:s,key:n}=e,a=this.pool.get(s),r=a?await a.then(i=>i,()=>null):null;if(r===null)throw new l(`the session behind this handle (app "${s}") is gone \u2014 closed, evicted after its pod died, or the proxy restarted; it cannot be reattached. Reconnect without a handle to start a new session.`);if(this.keyOf(r)!==n)throw new l(`the session behind this handle (app "${s}") was replaced \u2014 the original backhaul died and a new session serves this app now; the handle's session cannot be reattached. Reconnect without a handle to start a new session.`);return{app:s,entry:r}}async evict(t,e){let s=this.pool.get(t);!s||await s.then(a=>a,()=>null)!==e||this.pool.get(t)!==s||(this.pool.delete(t),await this.opts.closeSession(e).catch(()=>{}))}async modelList(){return{object:"list",data:(await this.availableIds()).map(e=>({id:e,object:"model",created:0,owned_by:"urun"}))}}async closeAll(){let t=[...this.pool.values()];this.pool.clear();let e=[];if(await Promise.all(t.map(async s=>{try{await this.opts.closeSession(await s)}catch(n){e.push(String(n instanceof Error?n.message:n))}})),e.length>0)throw new Error(`failed to close ${e.length} pooled session(s): ${e.join("; ")}`)}};export{A as a,p as b,l as c,$ as d,g as e};
|
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
import { IncomingMessage, Server } from 'node:http';
|
|
2
|
-
import { a as AudioBridge, h as VideoFrameLane, j as VideoOutLane } from './video-out-D20UuJ8G.cjs';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* WHAT a proxy serves — the `identity` block on `GET /stats`. Reuse of a
|
|
6
|
-
* running proxy is allowed iff every field matches the launching invocation
|
|
7
|
-
* EXACTLY: a partial match (same app, different fn; same everything, older
|
|
8
|
-
* proxy_version) silently routes the agent at the wrong backend, which is
|
|
9
|
-
* worse than any error.
|
|
10
|
-
*/
|
|
11
|
-
interface ProxyIdentity {
|
|
12
|
-
app: string;
|
|
13
|
-
org: string;
|
|
14
|
-
fn: string;
|
|
15
|
-
/**
|
|
16
|
-
* The control-plane URL the proxy's backhaul session opens against
|
|
17
|
-
* (URUN_BASE_URL verbatim — same org/app/fn on staging vs prod are
|
|
18
|
-
* DIFFERENT backends; review finding, #285). Compared exactly: a cosmetic
|
|
19
|
-
* difference (trailing slash) merely refuses reuse and spawns an ephemeral
|
|
20
|
-
* proxy — the safe direction.
|
|
21
|
-
*/
|
|
22
|
-
base_url: string;
|
|
23
|
-
proxy_version: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* The local OpenAI-compatible proxy — `npx @urun-sh/openai proxy`.
|
|
28
|
-
*
|
|
29
|
-
* A loopback HTTP server speaking the OpenAI REST surface (`/v1/models`,
|
|
30
|
-
* `/v1/responses`, `/v1/chat/completions`, SSE streaming included), so any
|
|
31
|
-
* OpenAI-env-var tool — coding agents first — integrates with uRun with ZERO
|
|
32
|
-
* code changes: point `OPENAI_BASE_URL` at it and keep making plain, local,
|
|
33
|
-
* "inefficient" HTTP requests. The proxy backhauls each request over the uRun
|
|
34
|
-
* session transport (the `@urun-sh/openai` Responses client → session doc
|
|
35
|
-
* lanes + streams — no public OpenAI-style HTTP leaves the machine), which is
|
|
36
|
-
* also where the delta-sync chat-state doc lane (urun-python
|
|
37
|
-
* `urun/serve/chat_state.py`) plugs in as the transport evolves.
|
|
38
|
-
*
|
|
39
|
-
* Local-trust model: binds 127.0.0.1 by default; `apiKey` (when set) must
|
|
40
|
-
* match the agent's `Authorization: Bearer` — otherwise any local bearer is
|
|
41
|
-
* accepted (the key the agent sends is NEVER forwarded upstream; uRun auth is
|
|
42
|
-
* the session's own).
|
|
43
|
-
*/
|
|
44
|
-
|
|
45
|
-
/** The request envelope every Responses-shaped upstream call carries. */
|
|
46
|
-
interface ResponsesCreateParams {
|
|
47
|
-
model?: string;
|
|
48
|
-
input: unknown;
|
|
49
|
-
stream?: boolean;
|
|
50
|
-
tools?: unknown;
|
|
51
|
-
tool_choice?: unknown;
|
|
52
|
-
temperature?: number;
|
|
53
|
-
max_output_tokens?: number;
|
|
54
|
-
top_p?: number;
|
|
55
|
-
stop?: string[];
|
|
56
|
-
/**
|
|
57
|
-
* Reasoning controls (urun-python #1667): forwarded VERBATIM to the serve
|
|
58
|
-
* envelope — `chat_template_kwargs.enable_thinking:false` is the think-off
|
|
59
|
-
* switch. Absent -> absent (no default injection; server-side validation).
|
|
60
|
-
*/
|
|
61
|
-
reasoning_effort?: string;
|
|
62
|
-
chat_template_kwargs?: Record<string, unknown>;
|
|
63
|
-
}
|
|
64
|
-
/** How one pinned session ended (the native phase machinery's terminal step). */
|
|
65
|
-
interface SessionEndInfo {
|
|
66
|
-
/**
|
|
67
|
-
* Milliseconds until the session's native deadline (`endsAt`), or null when
|
|
68
|
-
* the app declared no maximum session length. NOTE (missing primitive,
|
|
69
|
-
* called out in the PR): core exposes no PRE-expiry notice event — this
|
|
70
|
-
* callback fires AT terminal loss, so timeLeftMs is ~0 on expiry.
|
|
71
|
-
*/
|
|
72
|
-
timeLeftMs: number | null;
|
|
73
|
-
/** The terminal reason (expired / ended / error), for the loud close. */
|
|
74
|
-
reason: string;
|
|
75
|
-
}
|
|
76
|
-
/** The upstream calls the proxy makes — injectable (tests; alt transports). */
|
|
77
|
-
interface ProxyClients {
|
|
78
|
-
/** `UrunResponses(session).responses.create` — an async iterable of Responses stream events. */
|
|
79
|
-
createResponse(params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
|
|
80
|
-
/** `listModels(...)` result (an OpenAI model list object). */
|
|
81
|
-
listModels(): Promise<unknown>;
|
|
82
|
-
/**
|
|
83
|
-
* Open (or reuse) the NATIVE audio lanes on the pooled session `model` routes
|
|
84
|
-
* to — the same first-party machinery as RealtimeClient.enableAudio
|
|
85
|
-
* (transport/media.ts `enableSessionAudio`: AudioBridge over the session's
|
|
86
|
-
* `rt-audio-in`/`rt-audio-out` stream lanes at 24 kHz). One lane per pooled
|
|
87
|
-
* session; repeat calls return the same lane. Optional at the seam because
|
|
88
|
-
* text-only embeddings exist — but a surface that RECEIVES audio while the
|
|
89
|
-
* embedder wired no `openAudio` must fail LOUD, never drop chunks.
|
|
90
|
-
*/
|
|
91
|
-
openAudio?(model: string | undefined): Promise<ProxyAudioLane>;
|
|
92
|
-
/**
|
|
93
|
-
* Open (or reuse) the NATIVE video FRAME lane on the pooled session `model`
|
|
94
|
-
* routes to (transport/media.ts `enableSessionVideo`: discrete JPEG frames →
|
|
95
|
-
* `stream('rt-video-in').emit`, the §5 named-DATA image-bytes path — NOT the
|
|
96
|
-
* RTP media plane, which requires an already-H.264-encoded track). One lane
|
|
97
|
-
* per pooled session; repeat calls return the same lane. Optional at the seam
|
|
98
|
-
* because text-only embeddings exist — but a surface that RECEIVES video
|
|
99
|
-
* frames while the embedder wired no `openVideo` must fail LOUD, never drop
|
|
100
|
-
* frames.
|
|
101
|
-
*/
|
|
102
|
-
openVideo?(model: string | undefined): Promise<ProxyVideoLane>;
|
|
103
|
-
/**
|
|
104
|
-
* Open (or reuse) the NATIVE video OUTPUT lane on the pooled session
|
|
105
|
-
* `model` routes to (transport/video-out.ts `openSessionVideoOutLane`):
|
|
106
|
-
* decoded spec-v1 records consumed from the session's named §5
|
|
107
|
-
* `rt-video-out` downstream. The Live adapter fans each record onto the
|
|
108
|
-
* SAME Bidi WS as an extension server message (spec v2, WS-inline) — no
|
|
109
|
-
* side-channel transport. Optional at the seam; an ABSENT seam means the
|
|
110
|
-
* `urun.videoOut` capability is refused by omission in setupComplete
|
|
111
|
-
* (vanilla parity), while a wired seam that cannot open the lane must
|
|
112
|
-
* throw LOUD — never a quiet downgrade.
|
|
113
|
-
*/
|
|
114
|
-
openVideoOut?(model: string | undefined): Promise<ProxyVideoOutLane>;
|
|
115
|
-
/**
|
|
116
|
-
* SESSION-IDENTITY SEAM (ModelRouter.handleFor): the opaque stable handle
|
|
117
|
-
* for the pooled session currently serving `model`'s turns. Derived from
|
|
118
|
-
* native identity (app slug + uRun session id) — the same identity the
|
|
119
|
-
* serve-side session-affinity tag rides (urun-python#1556/#1582).
|
|
120
|
-
*/
|
|
121
|
-
sessionHandle(model: string | undefined): Promise<string>;
|
|
122
|
-
/**
|
|
123
|
-
* createResponse PINNED to the exact session a handle names
|
|
124
|
-
* (ModelRouter.sessionForHandle). Throws SessionGoneError LOUDLY when that
|
|
125
|
-
* session is gone or was replaced — never silently opens a fresh session
|
|
126
|
-
* while claiming resume. Deliberately NO re-home on this path: re-homing
|
|
127
|
-
* would swap the pinned session out from under the caller.
|
|
128
|
-
*/
|
|
129
|
-
createResponseOn(handle: string, params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
|
|
130
|
-
/**
|
|
131
|
-
* Subscribe to the pinned session's terminal end via core's NATIVE phase
|
|
132
|
-
* machinery (Session.onPhase → terminal 'expired'/'ended'/'error'). Fires
|
|
133
|
-
* `cb` once. Throws SessionGoneError if the handle's session is already
|
|
134
|
-
* gone — which doubles as the loud reattach check at resume time. Returns
|
|
135
|
-
* the unsubscribe.
|
|
136
|
-
*/
|
|
137
|
-
onSessionEnd(handle: string, cb: (end: SessionEndInfo) => void): Promise<() => void>;
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* The audio lane handle `openAudio` returns — structurally the transport
|
|
141
|
-
* AudioBridge (media.ts): base64 PCM16 @24 kHz mono in both directions.
|
|
142
|
-
*/
|
|
143
|
-
type ProxyAudioLane = Pick<AudioBridge, 'appendInputAudio' | 'onOutputAudio'>;
|
|
144
|
-
/**
|
|
145
|
-
* The video frame-lane handle `openVideo` returns — structurally the transport
|
|
146
|
-
* VideoFrameLane (media.ts): one raw encoded JPEG frame per call, input-only
|
|
147
|
-
* (Live-style protocols have no video OUT modality).
|
|
148
|
-
*/
|
|
149
|
-
type ProxyVideoLane = Pick<VideoFrameLane, 'sendInputFrame'>;
|
|
150
|
-
/**
|
|
151
|
-
* The video-OUT lane handle `openVideoOut` returns — structurally the
|
|
152
|
-
* transport VideoOutLane (video-out.ts): the codec plus frame/error fan-out
|
|
153
|
-
* the WS-inline delivery subscribes.
|
|
154
|
-
*/
|
|
155
|
-
type ProxyVideoOutLane = Pick<VideoOutLane, 'codec' | 'onOutputFrame' | 'onLaneError'>;
|
|
156
|
-
/**
|
|
157
|
-
* Per-request `ProxyClients` resolution — the seam the HOSTED multi-tenant
|
|
158
|
-
* server (`src/hosted/`) plugs into so ONE handler implementation serves
|
|
159
|
-
* every org: the hosted server resolves the caller's org from its Bearer API
|
|
160
|
-
* key and returns THAT org's backhaul. The local CLI passes a fixed
|
|
161
|
-
* `ProxyClients` instead; both go through the identical handler body below.
|
|
162
|
-
*
|
|
163
|
-
* Throwing from here is the loud path: the thrown error surfaces in the
|
|
164
|
-
* lane's native error envelope (see {@link ProxyHandlerOptions.statusOf}).
|
|
165
|
-
*/
|
|
166
|
-
type ProxyClientsFor = (req: IncomingMessage) => ProxyClients | Promise<ProxyClients>;
|
|
167
|
-
interface ProxyHandlerOptions {
|
|
168
|
-
/** A fixed backhaul (local CLI) or a per-request resolver (hosted server). */
|
|
169
|
-
clients: ProxyClients | ProxyClientsFor;
|
|
170
|
-
/** Optional bearer the local agent must present (never forwarded upstream). */
|
|
171
|
-
apiKey?: string;
|
|
172
|
-
/**
|
|
173
|
-
* WHAT this proxy serves (app/org/fn/base_url + proxy_version), surfaced as
|
|
174
|
-
* the `identity` block on `GET /stats` so a second `urun compat` invocation
|
|
175
|
-
* can reuse this proxy iff the identity matches its own exactly. Only the
|
|
176
|
-
* standalone `urun compat proxy` command sets it — a launch-mode proxy is
|
|
177
|
-
* child-owned (it dies with its child) and an embedder that omits it is
|
|
178
|
-
* simply never reused.
|
|
179
|
-
*/
|
|
180
|
-
identity?: ProxyIdentity;
|
|
181
|
-
/**
|
|
182
|
-
* Map a thrown error onto an HTTP status + error `type` before the generic
|
|
183
|
-
* 500. The hosted server uses it to turn its auth/tenancy failures into a
|
|
184
|
-
* 401 in the OpenAI envelope. Returning null means "not mine" — the error
|
|
185
|
-
* takes the ordinary loud 500 path.
|
|
186
|
-
*/
|
|
187
|
-
statusOf?: (err: unknown) => {
|
|
188
|
-
status: number;
|
|
189
|
-
openaiType: string;
|
|
190
|
-
anthropicType: string;
|
|
191
|
-
} | null;
|
|
192
|
-
}
|
|
193
|
-
/** The proxy server's own options: a FIXED backhaul (the local CLI lane). */
|
|
194
|
-
interface OpenAIProxyOptions extends ProxyHandlerOptions {
|
|
195
|
-
clients: ProxyClients;
|
|
196
|
-
}
|
|
197
|
-
/** Build (not listen) the proxy server — the caller owns listen/close. */
|
|
198
|
-
declare function createOpenAIProxy(options: OpenAIProxyOptions): Server;
|
|
199
|
-
|
|
200
|
-
export { type OpenAIProxyOptions as O, type ProxyClients as P, type ProxyIdentity as a, type ProxyVideoOutLane as b, createOpenAIProxy as c };
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { IncomingMessage, Server } from 'node:http';
|
|
2
|
-
import { a as AudioBridge, h as VideoFrameLane, j as VideoOutLane } from './video-out-CWesbk12.js';
|
|
3
|
-
|
|
4
|
-
interface ProxyIdentity {
|
|
5
|
-
app: string;
|
|
6
|
-
org: string;
|
|
7
|
-
fn: string;
|
|
8
|
-
|
|
9
|
-
base_url: string;
|
|
10
|
-
proxy_version: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
interface ResponsesCreateParams {
|
|
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
|
-
top_p?: number;
|
|
22
|
-
stop?: string[];
|
|
23
|
-
|
|
24
|
-
reasoning_effort?: string;
|
|
25
|
-
chat_template_kwargs?: Record<string, unknown>;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
interface SessionEndInfo {
|
|
29
|
-
|
|
30
|
-
timeLeftMs: number | null;
|
|
31
|
-
|
|
32
|
-
reason: string;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
interface ProxyClients {
|
|
36
|
-
|
|
37
|
-
createResponse(params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
|
|
38
|
-
|
|
39
|
-
listModels(): Promise<unknown>;
|
|
40
|
-
|
|
41
|
-
openAudio?(model: string | undefined): Promise<ProxyAudioLane>;
|
|
42
|
-
|
|
43
|
-
openVideo?(model: string | undefined): Promise<ProxyVideoLane>;
|
|
44
|
-
|
|
45
|
-
openVideoOut?(model: string | undefined): Promise<ProxyVideoOutLane>;
|
|
46
|
-
|
|
47
|
-
sessionHandle(model: string | undefined): Promise<string>;
|
|
48
|
-
|
|
49
|
-
createResponseOn(handle: string, params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
|
|
50
|
-
|
|
51
|
-
onSessionEnd(handle: string, cb: (end: SessionEndInfo) => void): Promise<() => void>;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
type ProxyAudioLane = Pick<AudioBridge, 'appendInputAudio' | 'onOutputAudio'>;
|
|
55
|
-
|
|
56
|
-
type ProxyVideoLane = Pick<VideoFrameLane, 'sendInputFrame'>;
|
|
57
|
-
|
|
58
|
-
type ProxyVideoOutLane = Pick<VideoOutLane, 'codec' | 'onOutputFrame' | 'onLaneError'>;
|
|
59
|
-
|
|
60
|
-
type ProxyClientsFor = (req: IncomingMessage) => ProxyClients | Promise<ProxyClients>;
|
|
61
|
-
interface ProxyHandlerOptions {
|
|
62
|
-
|
|
63
|
-
clients: ProxyClients | ProxyClientsFor;
|
|
64
|
-
|
|
65
|
-
apiKey?: string;
|
|
66
|
-
|
|
67
|
-
identity?: ProxyIdentity;
|
|
68
|
-
|
|
69
|
-
statusOf?: (err: unknown) => {
|
|
70
|
-
status: number;
|
|
71
|
-
openaiType: string;
|
|
72
|
-
anthropicType: string;
|
|
73
|
-
} | null;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
interface OpenAIProxyOptions extends ProxyHandlerOptions {
|
|
77
|
-
clients: ProxyClients;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
declare function createOpenAIProxy(options: OpenAIProxyOptions): Server;
|
|
81
|
-
|
|
82
|
-
export { type OpenAIProxyOptions as O, type ProxyClients as P, type ProxyIdentity as a, type ProxyVideoOutLane as b, createOpenAIProxy as c };
|