@urun-sh/openai 0.5.4 → 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-I2Q3B3OG.js +6 -0
- package/dist/chunk-QVF7NF7G.js +44 -0
- package/dist/hosted/bin.cjs +36 -22
- package/dist/hosted/bin.js +4 -4
- package/dist/hosted/index.cjs +33 -19
- package/dist/hosted/index.d.cts +140 -279
- package/dist/hosted/index.d.ts +63 -115
- package/dist/hosted/index.js +1 -1
- package/dist/pi-extension/index.cjs +6 -6
- package/dist/pi-extension/index.js +1 -1
- package/dist/proxy/cli.cjs +40 -36
- 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 +2 -2
- package/dist/chunk-4MSBS7M6.js +0 -1
- package/dist/chunk-RQBD4OAA.js +0 -6
- package/dist/chunk-STFRT5OY.js +0 -1
- package/dist/chunk-VNM4M4P3.js +0 -40
- package/dist/server-DuH_5OD9.d.ts +0 -84
- package/dist/server-wmUnMewW.d.cts +0 -206
package/dist/chunk-VNM4M4P3.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import{b as D,c as de}from"./chunk-4MSBS7M6.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 ye=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 q(n,t,e){n.writeHead(t,{"content-type":"application/json"}),n.end(JSON.stringify(e))}function H(n,t,e,s="invalid_request_error"){q(n,t,{error:{message:e,type:s}})}function U(n,t,e,s){q(n,t,{type:"error",error:{type:e,message:s}})}function xe(n,t,e){if(t==="anthropic"){q(n,404,{type:"error",error:{type:"not_found_error",message:e.message}});return}q(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,top_p:n.top_p,stop:n.stop,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
|
-
`)},S=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"}}))},$=!1,P=0,N="",B=()=>{$&&($=!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,k=0,A="",F=f=>{T&&(T=!1,r({type:"response.output_text.done",item_id:O,output_index:k,content_index:0,text:A}),r({type:"response.content_part.done",item_id:O,output_index:k,content_index:0,part:{type:"output_text",text:A}}),r({type:"response.output_item.done",output_index:k,item:{id:O,type:"message",role:"assistant",status:f,content:[{type:"output_text",text:A}]}}))},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(S(f));continue}if(R==="response.output_item.added"){L=!0,r(S(f));continue}if(R==="response.reasoning_text.delta"&&!L){x(),$||($=!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(),B(),T||(T=!0,k=i++,r({type:"response.output_item.added",output_index:k,item:{id:O,type:"message",role:"assistant",status:"in_progress",content:[]}}),r({type:"response.content_part.added",item_id:O,output_index:k,content_index:0,part:{type:"output_text",text:""}})),A+=String(f.delta??""),r({type:R,item_id:O,output_index:k,content_index:0,delta:f.delta});continue}let I=L?null:se(f);if(I){if(x(),B(),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,A);if(E){Z=!0,r({type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:Y(l,E)}});break}B(),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(S(f));continue}r(S(f))}B(),F("incomplete"),Q("incomplete"),p&&!Z&&e.remember(c,u,Ve(m,z(m))),s.write(`data: [DONE]
|
|
11
|
-
|
|
12
|
-
`),s.end();return}let w=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"&&(w=r.response)}if(w==null){H(s,502,"upstream produced no response.completed event","upstream_error");return}let _=Ve(m,w),y=X(d,ne(w).length,_);if(y){H(s,502,Y(l,y),"upstream_error");return}p&&e.remember(c,u,_),q(s,200,{...w,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,top_p:n.top_p,stop:n.stop,max_output_tokens:n.max_completion_tokens??n.max_tokens,...n.reasoning_effort!==void 0?{reasoning_effort:n.reasoning_effort}:{},...n.chat_template_kwargs!==void 0?{chat_template_kwargs:n.chat_template_kwargs}:{}})}catch(i){if(i instanceof D){xe(e,"openai",i);return}throw i}if(n.stream===!0){ke(e);let i=[],x=new G,$="";e.write(`data: ${JSON.stringify(W(u,a,{role:"assistant"},null))}
|
|
13
|
-
|
|
14
|
-
`);for await(let k of s.track(d,p)){i.push(k);let A=se(k);if(k.type==="response.output_text.delta"&&typeof k.delta=="string")$+=k.delta,e.write(`data: ${JSON.stringify(W(u,a,{content:k.delta},null))}
|
|
15
|
-
|
|
16
|
-
`);else if(k.type==="response.reasoning_text.delta"&&typeof k.delta=="string")e.write(`data: ${JSON.stringify(W(u,a,{reasoning_content:k.delta},null))}
|
|
17
|
-
|
|
18
|
-
`);else if(A){let{call:F,isNew:J}=x.add(A),v=J?{index:A.tool_index,id:F.id??`call_${A.tool_index}`,type:"function",function:{name:F.name??"",arguments:A.delta}}:{index:A.tool_index,function:{arguments:A.delta}};e.write(`data: ${JSON.stringify(W(u,a,{tool_calls:[v]},null))}
|
|
19
|
-
|
|
20
|
-
`)}else if(k.type==="error"){e.write(`data: ${JSON.stringify({error:k})}
|
|
21
|
-
|
|
22
|
-
`),e.end();return}}let P=he(z(i)),N=X(l,x.size+P.length,$);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 B=x.size>0||P.length>0;e.write(`data: ${JSON.stringify(W(u,a,{},B?"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="",w=[],_=new G;for await(let i of s.track(d,p)){w.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(w),r=he(y),S=r.length>0?r:he({output:_.items()}),O=X(l,S.length,h);if(O){H(e,502,Y(p,O),"upstream_error");return}let M=S.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),q(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 _e(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)){U(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){U(e,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(n.top_k!==void 0){U(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"))){U(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){U(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,$="",P=0,N="none",B=()=>N,T=-1,k=-1,A=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")B()!=="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")B()!=="text"&&v("text",{type:"text",text:""}),x+=1,$+=g.delta,m("content_block_delta",{index:T,delta:{type:"text_delta",text:g.delta}});else if(E){let{call:V}=A.add(E);(B()!=="tool"||E.tool_index!==k)&&(k=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,A.size+Q.length,$);if(Z){m("error",{error:{type:"api_error",message:Y(l,Z)}}),e.end();return}J();let f=[];if(A.size===0)try{f=_e(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=A.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 w="",_="",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"&&(w+=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"){U(e,502,"api_error",JSON.stringify(i));return}if(e.destroyed)break}if(e.destroyed)return;let S=z(y),O;try{O=_e(S),O.length===0&&r.size>0&&(O=_e({output:r.items()}))}catch(i){U(e,502,"api_error",String(i instanceof Error?i.message:i));return}let M=X(d,O.length,w);if(M){U(e,502,"api_error",Y(l,M));return}let C=[],L=_||De(S);L&&C.push({type:"thinking",thinking:L}),(w||O.length===0)&&C.push({type:"text",text:w}),C.push(...O),q(e,200,{id:u,type:"message",role:"assistant",content:C,model:a,stop_reason:O.length>0?"tool_use":"end_turn",usage:ue(S)??{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 ye;return(l,d)=>{let h=new URL(l.url??"/","http://localhost"),m=h.pathname==="/v1/messages"||h.pathname.startsWith("/v1/messages/"),w=(_,y,r,S)=>m?U(d,_,S,y):H(d,_,y,r);(async()=>{if(l.method==="GET"&&h.pathname==="/healthz"){q(d,200,{ok:!0});return}if(l.method==="GET"&&h.pathname==="/stats"){q(d,200,s?{identity:s,...p.snapshot()}:p.snapshot());return}if(e&&(l.headers.authorization??"")!==`Bearer ${e}`){w(401,"invalid local proxy api key","authentication_error","authentication_error");return}let _=await a(l);if(l.method==="GET"&&h.pathname==="/v1/models"){if(h.searchParams.get("format")==="openrouter"){if(!_.openRouterModels){w(501,"format=openrouter needs a router-backed client (openRouterModels)","not_implemented","not_implemented_error");return}q(d,200,await _.openRouterModels());return}q(d,200,await _.listModels());return}if(l.method!=="POST"){w(404,`no route for ${l.method} ${h.pathname}`,"invalid_request_error","not_found_error");return}let y;try{y=await et(l)}catch(S){if(S instanceof ae){w(413,S.message,"request_too_large","request_too_large");return}w(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),U(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}w(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){w(r.status,y,r.openaiType,r.anthropicType);return}w(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};
|
|
@@ -1,84 +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
|
-
openRouterModels?(): Promise<unknown>;
|
|
42
|
-
|
|
43
|
-
openAudio?(model: string | undefined): Promise<ProxyAudioLane>;
|
|
44
|
-
|
|
45
|
-
openVideo?(model: string | undefined): Promise<ProxyVideoLane>;
|
|
46
|
-
|
|
47
|
-
openVideoOut?(model: string | undefined): Promise<ProxyVideoOutLane>;
|
|
48
|
-
|
|
49
|
-
sessionHandle(model: string | undefined): Promise<string>;
|
|
50
|
-
|
|
51
|
-
createResponseOn(handle: string, params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
|
|
52
|
-
|
|
53
|
-
onSessionEnd(handle: string, cb: (end: SessionEndInfo) => void): Promise<() => void>;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
type ProxyAudioLane = Pick<AudioBridge, 'appendInputAudio' | 'onOutputAudio'>;
|
|
57
|
-
|
|
58
|
-
type ProxyVideoLane = Pick<VideoFrameLane, 'sendInputFrame'>;
|
|
59
|
-
|
|
60
|
-
type ProxyVideoOutLane = Pick<VideoOutLane, 'codec' | 'onOutputFrame' | 'onLaneError'>;
|
|
61
|
-
|
|
62
|
-
type ProxyClientsFor = (req: IncomingMessage) => ProxyClients | Promise<ProxyClients>;
|
|
63
|
-
interface ProxyHandlerOptions {
|
|
64
|
-
|
|
65
|
-
clients: ProxyClients | ProxyClientsFor;
|
|
66
|
-
|
|
67
|
-
apiKey?: string;
|
|
68
|
-
|
|
69
|
-
identity?: ProxyIdentity;
|
|
70
|
-
|
|
71
|
-
statusOf?: (err: unknown) => {
|
|
72
|
-
status: number;
|
|
73
|
-
openaiType: string;
|
|
74
|
-
anthropicType: string;
|
|
75
|
-
} | null;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
interface OpenAIProxyOptions extends ProxyHandlerOptions {
|
|
79
|
-
clients: ProxyClients;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
declare function createOpenAIProxy(options: OpenAIProxyOptions): Server;
|
|
83
|
-
|
|
84
|
-
export { type OpenAIProxyOptions as O, type ProxyClients as P, type ProxyIdentity as a, type ProxyVideoOutLane as b, createOpenAIProxy as c };
|
|
@@ -1,206 +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
|
-
* The OpenRouter provider document (`GET /v1/models?format=openrouter`).
|
|
84
|
-
* Optional at the seam: hand-built test clients may omit it, in which case
|
|
85
|
-
* the format=openrouter branch answers 501 — loud, never a silent empty.
|
|
86
|
-
*/
|
|
87
|
-
openRouterModels?(): Promise<unknown>;
|
|
88
|
-
/**
|
|
89
|
-
* Open (or reuse) the NATIVE audio lanes on the pooled session `model` routes
|
|
90
|
-
* to — the same first-party machinery as RealtimeClient.enableAudio
|
|
91
|
-
* (transport/media.ts `enableSessionAudio`: AudioBridge over the session's
|
|
92
|
-
* `rt-audio-in`/`rt-audio-out` stream lanes at 24 kHz). One lane per pooled
|
|
93
|
-
* session; repeat calls return the same lane. Optional at the seam because
|
|
94
|
-
* text-only embeddings exist — but a surface that RECEIVES audio while the
|
|
95
|
-
* embedder wired no `openAudio` must fail LOUD, never drop chunks.
|
|
96
|
-
*/
|
|
97
|
-
openAudio?(model: string | undefined): Promise<ProxyAudioLane>;
|
|
98
|
-
/**
|
|
99
|
-
* Open (or reuse) the NATIVE video FRAME lane on the pooled session `model`
|
|
100
|
-
* routes to (transport/media.ts `enableSessionVideo`: discrete JPEG frames →
|
|
101
|
-
* `stream('rt-video-in').emit`, the §5 named-DATA image-bytes path — NOT the
|
|
102
|
-
* RTP media plane, which requires an already-H.264-encoded track). One lane
|
|
103
|
-
* per pooled session; repeat calls return the same lane. Optional at the seam
|
|
104
|
-
* because text-only embeddings exist — but a surface that RECEIVES video
|
|
105
|
-
* frames while the embedder wired no `openVideo` must fail LOUD, never drop
|
|
106
|
-
* frames.
|
|
107
|
-
*/
|
|
108
|
-
openVideo?(model: string | undefined): Promise<ProxyVideoLane>;
|
|
109
|
-
/**
|
|
110
|
-
* Open (or reuse) the NATIVE video OUTPUT lane on the pooled session
|
|
111
|
-
* `model` routes to (transport/video-out.ts `openSessionVideoOutLane`):
|
|
112
|
-
* decoded spec-v1 records consumed from the session's named §5
|
|
113
|
-
* `rt-video-out` downstream. The Live adapter fans each record onto the
|
|
114
|
-
* SAME Bidi WS as an extension server message (spec v2, WS-inline) — no
|
|
115
|
-
* side-channel transport. Optional at the seam; an ABSENT seam means the
|
|
116
|
-
* `urun.videoOut` capability is refused by omission in setupComplete
|
|
117
|
-
* (vanilla parity), while a wired seam that cannot open the lane must
|
|
118
|
-
* throw LOUD — never a quiet downgrade.
|
|
119
|
-
*/
|
|
120
|
-
openVideoOut?(model: string | undefined): Promise<ProxyVideoOutLane>;
|
|
121
|
-
/**
|
|
122
|
-
* SESSION-IDENTITY SEAM (ModelRouter.handleFor): the opaque stable handle
|
|
123
|
-
* for the pooled session currently serving `model`'s turns. Derived from
|
|
124
|
-
* native identity (app slug + uRun session id) — the same identity the
|
|
125
|
-
* serve-side session-affinity tag rides (urun-python#1556/#1582).
|
|
126
|
-
*/
|
|
127
|
-
sessionHandle(model: string | undefined): Promise<string>;
|
|
128
|
-
/**
|
|
129
|
-
* createResponse PINNED to the exact session a handle names
|
|
130
|
-
* (ModelRouter.sessionForHandle). Throws SessionGoneError LOUDLY when that
|
|
131
|
-
* session is gone or was replaced — never silently opens a fresh session
|
|
132
|
-
* while claiming resume. Deliberately NO re-home on this path: re-homing
|
|
133
|
-
* would swap the pinned session out from under the caller.
|
|
134
|
-
*/
|
|
135
|
-
createResponseOn(handle: string, params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
|
|
136
|
-
/**
|
|
137
|
-
* Subscribe to the pinned session's terminal end via core's NATIVE phase
|
|
138
|
-
* machinery (Session.onPhase → terminal 'expired'/'ended'/'error'). Fires
|
|
139
|
-
* `cb` once. Throws SessionGoneError if the handle's session is already
|
|
140
|
-
* gone — which doubles as the loud reattach check at resume time. Returns
|
|
141
|
-
* the unsubscribe.
|
|
142
|
-
*/
|
|
143
|
-
onSessionEnd(handle: string, cb: (end: SessionEndInfo) => void): Promise<() => void>;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* The audio lane handle `openAudio` returns — structurally the transport
|
|
147
|
-
* AudioBridge (media.ts): base64 PCM16 @24 kHz mono in both directions.
|
|
148
|
-
*/
|
|
149
|
-
type ProxyAudioLane = Pick<AudioBridge, 'appendInputAudio' | 'onOutputAudio'>;
|
|
150
|
-
/**
|
|
151
|
-
* The video frame-lane handle `openVideo` returns — structurally the transport
|
|
152
|
-
* VideoFrameLane (media.ts): one raw encoded JPEG frame per call, input-only
|
|
153
|
-
* (Live-style protocols have no video OUT modality).
|
|
154
|
-
*/
|
|
155
|
-
type ProxyVideoLane = Pick<VideoFrameLane, 'sendInputFrame'>;
|
|
156
|
-
/**
|
|
157
|
-
* The video-OUT lane handle `openVideoOut` returns — structurally the
|
|
158
|
-
* transport VideoOutLane (video-out.ts): the codec plus frame/error fan-out
|
|
159
|
-
* the WS-inline delivery subscribes.
|
|
160
|
-
*/
|
|
161
|
-
type ProxyVideoOutLane = Pick<VideoOutLane, 'codec' | 'onOutputFrame' | 'onLaneError'>;
|
|
162
|
-
/**
|
|
163
|
-
* Per-request `ProxyClients` resolution — the seam the HOSTED multi-tenant
|
|
164
|
-
* server (`src/hosted/`) plugs into so ONE handler implementation serves
|
|
165
|
-
* every org: the hosted server resolves the caller's org from its Bearer API
|
|
166
|
-
* key and returns THAT org's backhaul. The local CLI passes a fixed
|
|
167
|
-
* `ProxyClients` instead; both go through the identical handler body below.
|
|
168
|
-
*
|
|
169
|
-
* Throwing from here is the loud path: the thrown error surfaces in the
|
|
170
|
-
* lane's native error envelope (see {@link ProxyHandlerOptions.statusOf}).
|
|
171
|
-
*/
|
|
172
|
-
type ProxyClientsFor = (req: IncomingMessage) => ProxyClients | Promise<ProxyClients>;
|
|
173
|
-
interface ProxyHandlerOptions {
|
|
174
|
-
/** A fixed backhaul (local CLI) or a per-request resolver (hosted server). */
|
|
175
|
-
clients: ProxyClients | ProxyClientsFor;
|
|
176
|
-
/** Optional bearer the local agent must present (never forwarded upstream). */
|
|
177
|
-
apiKey?: string;
|
|
178
|
-
/**
|
|
179
|
-
* WHAT this proxy serves (app/org/fn/base_url + proxy_version), surfaced as
|
|
180
|
-
* the `identity` block on `GET /stats` so a second `urun compat` invocation
|
|
181
|
-
* can reuse this proxy iff the identity matches its own exactly. Only the
|
|
182
|
-
* standalone `urun compat proxy` command sets it — a launch-mode proxy is
|
|
183
|
-
* child-owned (it dies with its child) and an embedder that omits it is
|
|
184
|
-
* simply never reused.
|
|
185
|
-
*/
|
|
186
|
-
identity?: ProxyIdentity;
|
|
187
|
-
/**
|
|
188
|
-
* Map a thrown error onto an HTTP status + error `type` before the generic
|
|
189
|
-
* 500. The hosted server uses it to turn its auth/tenancy failures into a
|
|
190
|
-
* 401 in the OpenAI envelope. Returning null means "not mine" — the error
|
|
191
|
-
* takes the ordinary loud 500 path.
|
|
192
|
-
*/
|
|
193
|
-
statusOf?: (err: unknown) => {
|
|
194
|
-
status: number;
|
|
195
|
-
openaiType: string;
|
|
196
|
-
anthropicType: string;
|
|
197
|
-
} | null;
|
|
198
|
-
}
|
|
199
|
-
/** The proxy server's own options: a FIXED backhaul (the local CLI lane). */
|
|
200
|
-
interface OpenAIProxyOptions extends ProxyHandlerOptions {
|
|
201
|
-
clients: ProxyClients;
|
|
202
|
-
}
|
|
203
|
-
/** Build (not listen) the proxy server — the caller owns listen/close. */
|
|
204
|
-
declare function createOpenAIProxy(options: OpenAIProxyOptions): Server;
|
|
205
|
-
|
|
206
|
-
export { type OpenAIProxyOptions as O, type ProxyClients as P, type ProxyIdentity as a, type ProxyVideoOutLane as b, createOpenAIProxy as c };
|