@urun-sh/openai 0.2.51 → 0.2.53

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.
@@ -1,14 +1,14 @@
1
1
  #!/usr/bin/env node
2
- "use strict";var He=Object.create;var j=Object.defineProperty;var Xe=Object.getOwnPropertyDescriptor;var Ve=Object.getOwnPropertyNames;var Qe=Object.getPrototypeOf,Ze=Object.prototype.hasOwnProperty;var et=(e,n)=>{for(var t in n)j(e,t,{get:n[t],enumerable:!0})},ce=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of Ve(n))!Ze.call(e,o)&&o!==t&&j(e,o,{get:()=>n[o],enumerable:!(r=Xe(n,o))||r.enumerable});return e};var D=(e,n,t)=>(t=e!=null?He(Qe(e)):{},ce(n||!e||!e.__esModule?j(t,"default",{value:e,enumerable:!0}):t,e)),tt=e=>ce(j({},"__esModule",{value:!0}),e);var It={};et(It,{buildClients:()=>Ye,buildRouter:()=>Ge});module.exports=tt(It);var nt=()=>typeof document>"u"?new URL(`file:${__filename}`).href:document.currentScript&&document.currentScript.tagName.toUpperCase()==="SCRIPT"?document.currentScript.src:new URL("main.js",document.baseURI).href,v=nt();var pe=require("child_process"),I=require("fs"),Ke=require("url"),qe=D(require("path"),1),f=D(require("process"),1),T=require("@urun-sh/core");function le(e,n,t,r){let o=e.response??{},s={request_id:t,consumer_id:r,stream:!0,kind:"chat",messages:n};return typeof o.instructions=="string"&&(s.instructions=o.instructions),Array.isArray(o.modalities)&&(s.modalities=o.modalities),typeof o.temperature=="number"&&(s.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(s.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(s.tools=o.tools),s}function de(e,n,t){let r={request_id:n,consumer_id:t,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(r.model=e.model),e.tools!==void 0&&(r.tools=e.tools),typeof e.temperature=="number"&&(r.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(r.max_output_tokens=e.max_output_tokens),r}function me(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,t=n.body??{};return{type:"error",error:{type:"urun_error",code:n.code??null,message:t.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}var rt="llm-resp";async function*z(e,n){let t=`${rt}:${n}`,r=`resp_${n}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};for await(let o of e.stream(t).messages()){let s=o;if(s.t==="delta")yield{type:"response.output_text.delta",item_id:r,delta:s.delta};else if(s.t==="response"){yield{type:"response.completed",response:{id:r,status:"completed",...s.body}};return}else if(s.t==="error"){yield me(s);return}}}var ot="llm",B=class{constructor(n){this.session=n}session;get consumerId(){return this.session.consumerId}write(n){this.session.doc(ot).set({requests:{[n.request_id]:{payload:n,consumer_id:n.consumer_id,stream:n.stream}}})}sendResponses(n,t){let r=de(n,t,this.consumerId);return this.write(r),z(this.session,t)}sendResponseCreate(n,t,r){let o=le(n,t,r,this.consumerId);return this.write(o),z(this.session,r)}};var fe=0;function st(){return fe+=1,`req_${Date.now().toString(36)}_${fe.toString(36)}`}var N=class{transport;constructor(n){this.transport=new B(n)}responses={create:async n=>{let t=st(),r=this.transport.sendResponses(n,t);return Object.assign((async function*(){yield*r})(),{requestId:t})}}};async function ye(e){let t=await(e.fetchImpl??fetch)(`${e.catalogUrl}/model_catalog?select=model_id,variant`,{headers:{apikey:e.anonKey,Authorization:`Bearer ${e.anonKey}`}});if(!t.ok)throw new Error(`model_catalog fetch failed: ${t.status}`);return await t.json()}var ke=require("http");var ge="https://api.urun.sh/v1";function H(e){return e.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}var k=class extends Error{};async function he(e){let n=e.fetchImpl??fetch,t=`${e.apiUrl.replace(/\/+$/,"")}/apps`,r=await n(t,{headers:{Authorization:`Bearer ${e.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new Error(`org apps listing failed: GET ${t} \u2192 ${r.status}`);let o=await r.json();if(!Array.isArray(o.apps))throw new Error(`org apps listing returned no "apps" array (GET ${t})`);if(o.truncated===!0)throw new Error(`org apps listing was truncated (GET ${t} returned ${o.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return o.apps}var it=6e4,J=class{constructor(n){this.opts=n}opts;pool=new Map;appsCache=null;seed(n,t){this.pool.set(n,Promise.resolve(t))}async deployedApps(){if(!this.opts.listApps)return[];let n=this.opts.appsTtlMs??it;if(this.appsCache&&Date.now()-this.appsCache.at<n)return this.appsCache.apps;let t=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:t},t}servable(n){return n.filter(t=>t.function_name===this.opts.fnName&&t.deployment_status==="active").map(t=>t.app_slug)}async availableIds(){let n=new Set([this.opts.defaultApp]);if(this.opts.listApps)for(let t of this.servable(await this.deployedApps()))n.add(t);for(let t of this.pool.keys())n.add(t);return[this.opts.defaultApp,...[...n].filter(t=>t!==this.opts.defaultApp).sort()]}async resolveApp(n){let t=(n??"").trim();if(!t||t==="urun")return this.opts.defaultApp;let r=H(t);if(r===this.opts.defaultApp)return this.opts.defaultApp;if(this.pool.has(r))return r;if(this.opts.listApps){let o=await this.deployedApps(),s=this.servable(o);if(s.includes(r))return r;let a=s.filter(u=>u.startsWith(`${r}-`));if(a.length===1)return a[0];if(a.length>1)throw new k(`model "${t}" is ambiguous across deployed apps ${a.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let i=o.find(u=>u.app_slug===r||u.app_slug.startsWith(`${r}-`));if(i)throw new k(`model "${t}" maps to app "${i.app_slug}", which is not an active "${this.opts.fnName}" app \u2014 deploy it with \`urun serve ${t}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.listCatalog){let s=(await this.opts.listCatalog()).find(a=>t===a.model_id||t===`${a.model_id}:${a.variant}`||r===H(a.model_id)||r===H(`${a.model_id}-${a.variant}`));if(s)throw new k(`model "${t}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${s.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return this.opts.defaultApp}async sessionFor(n){let t=await this.resolveApp(n),r=this.pool.get(t);return r||(r=Promise.resolve(this.opts.openSession(t)),this.pool.set(t,r),r.catch(()=>this.pool.delete(t))),{app:t,entry:await r}}async modelList(){return{object:"list",data:(await this.availableIds()).map(t=>({id:t,object:"model",created:0,owned_by:"urun"}))}}async closeAll(){let n=[...this.pool.values()];this.pool.clear();let t=[];if(await Promise.all(n.map(async r=>{try{await this.opts.closeSession(await r)}catch(o){t.push(String(o instanceof Error?o.message:o))}})),t.length>0)throw new Error(`failed to close ${t.length} pooled session(s): ${t.join("; ")}`)}};var X=class e{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(n,t,r){this.requests[n]=(this.requests[n]??0)+1,this.localInBytes+=t,this.naiveInBytes+=r}modelEntry(n){let t=this.models[n];if(t)return t;let r=Object.keys(this.models).length>=at?"(other)":n;return this.models[r]??={requests:0,tokens_out:0}}model(n){this.modelEntry(n).requests+=1}track(n,t){let r=this,o=performance.now(),s=null,a=0;return(async function*(){for await(let u of n)u.type==="response.output_text.delta"&&typeof u.delta=="string"&&(s===null&&(s=performance.now(),r.ttftMs.push(s-o),r.ttftMs.length>512&&r.ttftMs.shift()),a+=1,r.tokensOut+=1,r.charsOut+=u.delta.length,t!==void 0&&(r.modelEntry(t).tokens_out+=1)),yield u;let i=(performance.now()-(s??o))/1e3;a>0&&i>0&&(r.tokRates.push(a/i),r.tokRates.length>512&&r.tokRates.shift())})()}static p50(n){if(!n.length)return null;let t=[...n].sort((r,o)=>r-o);return t[Math.floor(t.length/2)]}snapshot(){let n=Object.values(this.requests).reduce((t,r)=>t+r,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:e.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:e.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:n},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 Q(e){e.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function R(e,n,t){e.writeHead(n,{"content-type":"application/json"}),e.end(JSON.stringify(t))}function _(e,n,t,r="invalid_request_error"){R(e,n,{error:{message:t,type:r}})}function Z(e,n,t){if(n==="anthropic"){R(e,404,{type:"error",error:{type:"not_found_error",message:t.message}});return}R(e,404,{error:{message:t.message,type:"invalid_request_error",code:"model_not_found"}})}var at=256,pt=128;function ee(e){return typeof e.model!="string"||!e.model.trim()?"(default)":e.model.trim().slice(0,pt)}var _e=64*1024*1024,q=class extends Error{};async function ut(e){let n=[],t=0;for await(let o of e){if(t+=o.length,t>_e)throw new q(`request body exceeds ${_e} bytes`);n.push(o)}let r=Buffer.concat(n).toString("utf8");return r?JSON.parse(r):{}}function K(e,n,t,r){return{id:e,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:n,choices:[{index:0,delta:t,finish_reason:r}]}}var ct=256;function lt(e){if(typeof e=="string")return[{role:"user",content:e}];if(Array.isArray(e))return e;throw new Error("responses input must be a string or an array of messages/items")}var V=class{conversations=new Map;thread(n,t){let r=lt(t);if(n==null)return r;let o=this.conversations.get(String(n));if(!o)throw new Error(`unknown previous_response_id ${String(n)} (proxy restarts drop stored responses)`);return[...o,...r]}remember(n,t,r){this.conversations.set(n,[...t,{role:"assistant",content:r}]);for(let o of this.conversations.keys()){if(this.conversations.size<=ct)break;this.conversations.delete(o)}}};function te(e){let n=e?.output;return Array.isArray(n)?n.filter(t=>t.type==="function_call"):[]}function L(e){return e.find(n=>n.type==="response.completed")?.response??null}function dt(e){if(Array.isArray(e))return e.map(n=>{let t=n.function;return!t||typeof t!="object"?n:{type:"function",name:t.name,description:t.description,parameters:t.parameters,strict:t.strict}})}var G=class extends Error{};function mt(e,n){return Array.isArray(n)?n.map(t=>{let r=t;if(r.type==="text")return{type:e==="assistant"?"output_text":"input_text",text:r.text};if(r.type==="image_url"){let o=r.image_url??{};return{type:"input_image",image_url:o.url,detail:o.detail}}throw new G(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):n}function ft(e){let n=[];for(let t of e){if(t.role==="tool"){n.push({type:"function_call_output",call_id:t.tool_call_id,output:typeof t.content=="string"?t.content:JSON.stringify(t.content??"")});continue}if(t.role==="assistant"&&Array.isArray(t.tool_calls)){typeof t.content=="string"&&t.content&&n.push({role:"assistant",content:t.content});for(let r of t.tool_calls){let o=r.function??{};n.push({type:"function_call",call_id:r.id,name:o.name,arguments:o.arguments})}continue}n.push({...t,content:mt(t.role,t.content)})}return n}function we(e){return te(e).map((n,t)=>({index:t,id:n.call_id??n.id??`call_${t}`,type:"function",function:{name:n.name??"",arguments:n.arguments??"{}"}}))}function xe(e,n){let t=n?.output_text;return typeof t=="string"?t:e.filter(r=>r.type==="response.output_text.delta"&&typeof r.delta=="string").map(r=>r.delta).join("")}async function yt(e,n,t,r,o){let s=e.stream===!0,a;try{a=t.thread(e.previous_response_id,e.input)}catch(p){_(r,400,String(p instanceof Error?p.message:p));return}e.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(a))-Buffer.byteLength(JSON.stringify(e.input)));let i=`resp_${Math.random().toString(36).slice(2,14)}`,u=e.store!==!1,d=ee(e);o.model(d);let l;try{l=await n.createResponse({model:e.model,input:a,stream:s,tools:e.tools,temperature:e.temperature,max_output_tokens:e.max_output_tokens})}catch(p){if(p instanceof k){Z(r,"openai",p);return}throw p}let m=[];if(s){Q(r);let p=y=>{r.write(`event: ${String(y.type??"message")}
3
- data: ${JSON.stringify(y)}
2
+ "use strict";var Xe=Object.create;var M=Object.defineProperty;var Ve=Object.getOwnPropertyDescriptor;var Qe=Object.getOwnPropertyNames;var Ze=Object.getPrototypeOf,et=Object.prototype.hasOwnProperty;var tt=(e,n)=>{for(var t in n)M(e,t,{get:n[t],enumerable:!0})},ce=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of Qe(n))!et.call(e,o)&&o!==t&&M(e,o,{get:()=>n[o],enumerable:!(r=Ve(n,o))||r.enumerable});return e};var D=(e,n,t)=>(t=e!=null?Xe(Ze(e)):{},ce(n||!e||!e.__esModule?M(t,"default",{value:e,enumerable:!0}):t,e)),nt=e=>ce(M({},"__esModule",{value:!0}),e);var Ot={};tt(Ot,{buildClients:()=>Fe,buildRouter:()=>Ye});module.exports=nt(Ot);var rt=()=>typeof document>"u"?new URL(`file:${__filename}`).href:document.currentScript&&document.currentScript.tagName.toUpperCase()==="SCRIPT"?document.currentScript.src:new URL("main.js",document.baseURI).href,v=rt();var pe=require("child_process"),I=require("fs"),Ge=D(require("path"),1),m=D(require("process"),1),T=require("@urun-sh/core");function le(e,n,t,r){let o=e.response??{},s={request_id:t,consumer_id:r,stream:!0,kind:"chat",messages:n};return typeof o.instructions=="string"&&(s.instructions=o.instructions),Array.isArray(o.modalities)&&(s.modalities=o.modalities),typeof o.temperature=="number"&&(s.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(s.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(s.tools=o.tools),s}function de(e,n,t){let r={request_id:n,consumer_id:t,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(r.model=e.model),e.tools!==void 0&&(r.tools=e.tools),typeof e.temperature=="number"&&(r.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(r.max_output_tokens=e.max_output_tokens),r}function me(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,t=n.body??{};return{type:"error",error:{type:"urun_error",code:n.code??null,message:t.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}var ot="llm-resp";async function*z(e,n){let t=`${ot}:${n}`,r=`resp_${n}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};for await(let o of e.stream(t).messages()){let s=o;if(s.t==="delta")yield{type:"response.output_text.delta",item_id:r,delta:s.delta};else if(s.t==="response"){yield{type:"response.completed",response:{id:r,status:"completed",...s.body}};return}else if(s.t==="error"){yield me(s);return}}}var st="llm",B=class{constructor(n){this.session=n}session;get consumerId(){return this.session.consumerId}write(n){this.session.doc(st).set({requests:{[n.request_id]:{payload:n,consumer_id:n.consumer_id,stream:n.stream}}})}sendResponses(n,t){let r=de(n,t,this.consumerId);return this.write(r),z(this.session,t)}sendResponseCreate(n,t,r){let o=le(n,t,r,this.consumerId);return this.write(o),z(this.session,r)}};var fe=0;function it(){return fe+=1,`req_${Date.now().toString(36)}_${fe.toString(36)}`}var N=class{transport;constructor(n){this.transport=new B(n)}responses={create:async n=>{let t=it(),r=this.transport.sendResponses(n,t);return Object.assign((async function*(){yield*r})(),{requestId:t})}}};async function ye(e){let t=await(e.fetchImpl??fetch)(`${e.catalogUrl}/model_catalog?select=model_id,variant`,{headers:{apikey:e.anonKey,Authorization:`Bearer ${e.anonKey}`}});if(!t.ok)throw new Error(`model_catalog fetch failed: ${t.status}`);return await t.json()}var ke=require("http");var ge="https://api.urun.sh/v1";function H(e){return e.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}var k=class extends Error{};async function he(e){let n=e.fetchImpl??fetch,t=`${e.apiUrl.replace(/\/+$/,"")}/apps`,r=await n(t,{headers:{Authorization:`Bearer ${e.apiKey}`,Accept:"application/json"}});if(!r.ok)throw new Error(`org apps listing failed: GET ${t} \u2192 ${r.status}`);let o=await r.json();if(!Array.isArray(o.apps))throw new Error(`org apps listing returned no "apps" array (GET ${t})`);if(o.truncated===!0)throw new Error(`org apps listing was truncated (GET ${t} returned ${o.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return o.apps}var at=6e4,J=class{constructor(n){this.opts=n}opts;pool=new Map;appsCache=null;seed(n,t){this.pool.set(n,Promise.resolve(t))}async deployedApps(){if(!this.opts.listApps)return[];let n=this.opts.appsTtlMs??at;if(this.appsCache&&Date.now()-this.appsCache.at<n)return this.appsCache.apps;let t=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:t},t}servable(n){return n.filter(t=>t.function_name===this.opts.fnName&&t.deployment_status==="active").map(t=>t.app_slug)}async availableIds(){let n=new Set([this.opts.defaultApp]);if(this.opts.listApps)for(let t of this.servable(await this.deployedApps()))n.add(t);for(let t of this.pool.keys())n.add(t);return[this.opts.defaultApp,...[...n].filter(t=>t!==this.opts.defaultApp).sort()]}async resolveApp(n){let t=(n??"").trim();if(!t||t==="urun")return this.opts.defaultApp;let r=H(t);if(r===this.opts.defaultApp)return this.opts.defaultApp;if(this.pool.has(r))return r;if(this.opts.listApps){let o=await this.deployedApps(),s=this.servable(o);if(s.includes(r))return r;let a=s.filter(u=>u.startsWith(`${r}-`));if(a.length===1)return a[0];if(a.length>1)throw new k(`model "${t}" is ambiguous across deployed apps ${a.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let i=o.find(u=>u.app_slug===r||u.app_slug.startsWith(`${r}-`));if(i)throw new k(`model "${t}" maps to app "${i.app_slug}", which is not an active "${this.opts.fnName}" app \u2014 deploy it with \`urun serve ${t}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.listCatalog){let s=(await this.opts.listCatalog()).find(a=>t===a.model_id||t===`${a.model_id}:${a.variant}`||r===H(a.model_id)||r===H(`${a.model_id}-${a.variant}`));if(s)throw new k(`model "${t}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${s.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return this.opts.defaultApp}async sessionFor(n){let t=await this.resolveApp(n),r=this.pool.get(t);return r||(r=Promise.resolve(this.opts.openSession(t)),this.pool.set(t,r),r.catch(()=>this.pool.delete(t))),{app:t,entry:await r}}async modelList(){return{object:"list",data:(await this.availableIds()).map(t=>({id:t,object:"model",created:0,owned_by:"urun"}))}}async closeAll(){let n=[...this.pool.values()];this.pool.clear();let t=[];if(await Promise.all(n.map(async r=>{try{await this.opts.closeSession(await r)}catch(o){t.push(String(o instanceof Error?o.message:o))}})),t.length>0)throw new Error(`failed to close ${t.length} pooled session(s): ${t.join("; ")}`)}};var X=class e{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(n,t,r){this.requests[n]=(this.requests[n]??0)+1,this.localInBytes+=t,this.naiveInBytes+=r}modelEntry(n){let t=this.models[n];if(t)return t;let r=Object.keys(this.models).length>=pt?"(other)":n;return this.models[r]??={requests:0,tokens_out:0}}model(n){this.modelEntry(n).requests+=1}track(n,t){let r=this,o=performance.now(),s=null,a=0;return(async function*(){for await(let u of n)u.type==="response.output_text.delta"&&typeof u.delta=="string"&&(s===null&&(s=performance.now(),r.ttftMs.push(s-o),r.ttftMs.length>512&&r.ttftMs.shift()),a+=1,r.tokensOut+=1,r.charsOut+=u.delta.length,t!==void 0&&(r.modelEntry(t).tokens_out+=1)),yield u;let i=(performance.now()-(s??o))/1e3;a>0&&i>0&&(r.tokRates.push(a/i),r.tokRates.length>512&&r.tokRates.shift())})()}static p50(n){if(!n.length)return null;let t=[...n].sort((r,o)=>r-o);return t[Math.floor(t.length/2)]}snapshot(){let n=Object.values(this.requests).reduce((t,r)=>t+r,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:e.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:e.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:n},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 Q(e){e.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function R(e,n,t){e.writeHead(n,{"content-type":"application/json"}),e.end(JSON.stringify(t))}function _(e,n,t,r="invalid_request_error"){R(e,n,{error:{message:t,type:r}})}function Z(e,n,t){if(n==="anthropic"){R(e,404,{type:"error",error:{type:"not_found_error",message:t.message}});return}R(e,404,{error:{message:t.message,type:"invalid_request_error",code:"model_not_found"}})}var pt=256,ut=128;function ee(e){return typeof e.model!="string"||!e.model.trim()?"(default)":e.model.trim().slice(0,ut)}var _e=64*1024*1024,q=class extends Error{};async function ct(e){let n=[],t=0;for await(let o of e){if(t+=o.length,t>_e)throw new q(`request body exceeds ${_e} bytes`);n.push(o)}let r=Buffer.concat(n).toString("utf8");return r?JSON.parse(r):{}}function K(e,n,t,r){return{id:e,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:n,choices:[{index:0,delta:t,finish_reason:r}]}}var lt=256;function dt(e){if(typeof e=="string")return[{role:"user",content:e}];if(Array.isArray(e))return e;throw new Error("responses input must be a string or an array of messages/items")}var V=class{conversations=new Map;thread(n,t){let r=dt(t);if(n==null)return r;let o=this.conversations.get(String(n));if(!o)throw new Error(`unknown previous_response_id ${String(n)} (proxy restarts drop stored responses)`);return[...o,...r]}remember(n,t,r){this.conversations.set(n,[...t,{role:"assistant",content:r}]);for(let o of this.conversations.keys()){if(this.conversations.size<=lt)break;this.conversations.delete(o)}}};function te(e){let n=e?.output;return Array.isArray(n)?n.filter(t=>t.type==="function_call"):[]}function L(e){return e.find(n=>n.type==="response.completed")?.response??null}function mt(e){if(Array.isArray(e))return e.map(n=>{let t=n.function;return!t||typeof t!="object"?n:{type:"function",name:t.name,description:t.description,parameters:t.parameters,strict:t.strict}})}var G=class extends Error{};function ft(e,n){return Array.isArray(n)?n.map(t=>{let r=t;if(r.type==="text")return{type:e==="assistant"?"output_text":"input_text",text:r.text};if(r.type==="image_url"){let o=r.image_url??{};return{type:"input_image",image_url:o.url,detail:o.detail}}throw new G(`unsupported chat content part type "${String(r.type)}" \u2014 the proxy translates text and image_url parts`)}):n}function yt(e){let n=[];for(let t of e){if(t.role==="tool"){n.push({type:"function_call_output",call_id:t.tool_call_id,output:typeof t.content=="string"?t.content:JSON.stringify(t.content??"")});continue}if(t.role==="assistant"&&Array.isArray(t.tool_calls)){typeof t.content=="string"&&t.content&&n.push({role:"assistant",content:t.content});for(let r of t.tool_calls){let o=r.function??{};n.push({type:"function_call",call_id:r.id,name:o.name,arguments:o.arguments})}continue}n.push({...t,content:ft(t.role,t.content)})}return n}function we(e){return te(e).map((n,t)=>({index:t,id:n.call_id??n.id??`call_${t}`,type:"function",function:{name:n.name??"",arguments:n.arguments??"{}"}}))}function xe(e,n){let t=n?.output_text;return typeof t=="string"?t:e.filter(r=>r.type==="response.output_text.delta"&&typeof r.delta=="string").map(r=>r.delta).join("")}async function gt(e,n,t,r,o){let s=e.stream===!0,a;try{a=t.thread(e.previous_response_id,e.input)}catch(p){_(r,400,String(p instanceof Error?p.message:p));return}e.previous_response_id!=null&&(o.naiveInBytes+=Buffer.byteLength(JSON.stringify(a))-Buffer.byteLength(JSON.stringify(e.input)));let i=`resp_${Math.random().toString(36).slice(2,14)}`,u=e.store!==!1,d=ee(e);o.model(d);let l;try{l=await n.createResponse({model:e.model,input:a,stream:s,tools:e.tools,temperature:e.temperature,max_output_tokens:e.max_output_tokens})}catch(p){if(p instanceof k){Z(r,"openai",p);return}throw p}let f=[];if(s){Q(r);let p=g=>{r.write(`event: ${String(g.type??"message")}
3
+ data: ${JSON.stringify(g)}
4
4
 
5
- `)},c=y=>y.response&&typeof y.response=="object"?{...y,response:{...y.response,id:i}}:y,h=`item_${i.slice(5)}`,S=!1,A=!1,g=!1,E="",P=y=>{g&&(g=!1,p({type:"response.output_text.done",item_id:h,output_index:0,content_index:0,text:E}),p({type:"response.content_part.done",item_id:h,output_index:0,content_index:0,part:{type:"output_text",text:E}}),p({type:"response.output_item.done",output_index:0,item:{id:h,type:"message",role:"assistant",status:y,content:[{type:"output_text",text:E}]}}))};for await(let y of o.track(l,d)){m.push(y);let U=String(y.type??"");if(U==="response.created"||U==="response.in_progress"){S=!0,p(c(y));continue}if(U==="response.output_item.added"){A=!0,p(c(y));continue}if(U==="response.output_text.delta"&&!A){S||(S=!0,p({type:"response.created",response:{id:i,object:"response",status:"in_progress"}})),g||(g=!0,p({type:"response.output_item.added",output_index:0,item:{id:h,type:"message",role:"assistant",status:"in_progress",content:[]}}),p({type:"response.content_part.added",item_id:h,output_index:0,content_index:0,part:{type:"output_text",text:""}})),E+=String(y.delta??""),p({type:U,item_id:h,output_index:0,content_index:0,delta:y.delta});continue}if(U==="response.completed"&&!A){let Fe=g;P("completed"),te(y.response).forEach((O,ze)=>{let $=(Fe?1:0)+ze,M=O.id??O.call_id??`fc_${$}`,F=O.arguments??"",ue={id:M,type:"function_call",call_id:O.call_id??M,name:O.name??""};p({type:"response.output_item.added",output_index:$,item:{...ue,arguments:"",status:"in_progress"}}),p({type:"response.function_call_arguments.delta",item_id:M,output_index:$,delta:F}),p({type:"response.function_call_arguments.done",item_id:M,output_index:$,arguments:F}),p({type:"response.output_item.done",output_index:$,item:{...ue,arguments:F,status:"completed"}})}),p(c(y));continue}p(c(y))}P("incomplete"),u&&t.remember(i,a,xe(m,L(m))),r.write(`data: [DONE]
5
+ `)},c=g=>g.response&&typeof g.response=="object"?{...g,response:{...g.response,id:i}}:g,y=`item_${i.slice(5)}`,S=!1,b=!1,h=!1,E="",P=g=>{h&&(h=!1,p({type:"response.output_text.done",item_id:y,output_index:0,content_index:0,text:E}),p({type:"response.content_part.done",item_id:y,output_index:0,content_index:0,part:{type:"output_text",text:E}}),p({type:"response.output_item.done",output_index:0,item:{id:y,type:"message",role:"assistant",status:g,content:[{type:"output_text",text:E}]}}))};for await(let g of o.track(l,d)){f.push(g);let U=String(g.type??"");if(U==="response.created"||U==="response.in_progress"){S=!0,p(c(g));continue}if(U==="response.output_item.added"){b=!0,p(c(g));continue}if(U==="response.output_text.delta"&&!b){S||(S=!0,p({type:"response.created",response:{id:i,object:"response",status:"in_progress"}})),h||(h=!0,p({type:"response.output_item.added",output_index:0,item:{id:y,type:"message",role:"assistant",status:"in_progress",content:[]}}),p({type:"response.content_part.added",item_id:y,output_index:0,content_index:0,part:{type:"output_text",text:""}})),E+=String(g.delta??""),p({type:U,item_id:y,output_index:0,content_index:0,delta:g.delta});continue}if(U==="response.completed"&&!b){let ze=h;P("completed"),te(g.response).forEach((O,He)=>{let $=(ze?1:0)+He,j=O.id??O.call_id??`fc_${$}`,W=O.arguments??"",ue={id:j,type:"function_call",call_id:O.call_id??j,name:O.name??""};p({type:"response.output_item.added",output_index:$,item:{...ue,arguments:"",status:"in_progress"}}),p({type:"response.function_call_arguments.delta",item_id:j,output_index:$,delta:W}),p({type:"response.function_call_arguments.done",item_id:j,output_index:$,arguments:W}),p({type:"response.output_item.done",output_index:$,item:{...ue,arguments:W,status:"completed"}})}),p(c(g));continue}p(c(g))}P("incomplete"),u&&t.remember(i,a,xe(f,L(f))),r.write(`data: [DONE]
6
6
 
7
- `),r.end();return}let w=null;for await(let p of o.track(l,d)){if(m.push(p),p.type==="error"){_(r,502,JSON.stringify(p),"upstream_error");return}p.type==="response.completed"&&(w=p.response)}if(w==null){_(r,502,"upstream produced no response.completed event","upstream_error");return}let x=xe(m,w);u&&t.remember(i,a,x),R(r,200,{...w,id:i})}async function gt(e,n,t,r){let o=e.messages;if(!Array.isArray(o)){_(t,400,"chat/completions requires a messages array");return}let s=String(e.model??"urun"),a=`chatcmpl-${Math.random().toString(36).slice(2,14)}`,i;try{i=ft(o)}catch(p){if(p instanceof G){_(t,400,p.message);return}throw p}let u=ee(e);r.model(u);let d;try{d=await n.createResponse({model:e.model,input:i,stream:!0,tools:dt(e.tools),temperature:e.temperature,max_output_tokens:e.max_completion_tokens??e.max_tokens})}catch(p){if(p instanceof k){Z(t,"openai",p);return}throw p}if(e.stream===!0){Q(t);let p=[];t.write(`data: ${JSON.stringify(K(a,s,{role:"assistant"},null))}
7
+ `),r.end();return}let w=null;for await(let p of o.track(l,d)){if(f.push(p),p.type==="error"){_(r,502,JSON.stringify(p),"upstream_error");return}p.type==="response.completed"&&(w=p.response)}if(w==null){_(r,502,"upstream produced no response.completed event","upstream_error");return}let x=xe(f,w);u&&t.remember(i,a,x),R(r,200,{...w,id:i})}async function ht(e,n,t,r){let o=e.messages;if(!Array.isArray(o)){_(t,400,"chat/completions requires a messages array");return}let s=String(e.model??"urun"),a=`chatcmpl-${Math.random().toString(36).slice(2,14)}`,i;try{i=yt(o)}catch(p){if(p instanceof G){_(t,400,p.message);return}throw p}let u=ee(e);r.model(u);let d;try{d=await n.createResponse({model:e.model,input:i,stream:!0,tools:mt(e.tools),temperature:e.temperature,max_output_tokens:e.max_completion_tokens??e.max_tokens})}catch(p){if(p instanceof k){Z(t,"openai",p);return}throw p}if(e.stream===!0){Q(t);let p=[];t.write(`data: ${JSON.stringify(K(a,s,{role:"assistant"},null))}
8
8
 
9
- `);for await(let h of r.track(d,u))if(p.push(h),h.type==="response.output_text.delta"&&typeof h.delta=="string")t.write(`data: ${JSON.stringify(K(a,s,{content:h.delta},null))}
9
+ `);for await(let y of r.track(d,u))if(p.push(y),y.type==="response.output_text.delta"&&typeof y.delta=="string")t.write(`data: ${JSON.stringify(K(a,s,{content:y.delta},null))}
10
10
 
11
- `);else if(h.type==="error"){t.write(`data: ${JSON.stringify({error:h})}
11
+ `);else if(y.type==="error"){t.write(`data: ${JSON.stringify({error:y})}
12
12
 
13
13
  `),t.end();return}let c=we(L(p));c.length>0&&t.write(`data: ${JSON.stringify(K(a,s,{tool_calls:c},null))}
14
14
 
@@ -16,17 +16,18 @@ data: ${JSON.stringify(y)}
16
16
 
17
17
  `),t.write(`data: [DONE]
18
18
 
19
- `),t.end();return}let l="",m=[];for await(let p of r.track(d,u))if(m.push(p),p.type==="response.output_text.delta"&&typeof p.delta=="string"&&(l+=p.delta),p.type==="error"){_(t,502,JSON.stringify(p),"upstream_error");return}let w=we(L(m)).map(({index:p,...c})=>c),x={role:"assistant",content:l||null};w.length>0&&(x.tool_calls=w),R(t,200,{id:a,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:s,choices:[{index:0,message:x,finish_reason:w.length>0?"tool_calls":"stop"}],usage:{prompt_tokens:0,completion_tokens:0,total_tokens:0}})}function ht(e){return typeof e=="string"?e:Array.isArray(e)?e.filter(n=>n.type==="text").map(n=>String(n.text??"")).join(""):""}var b=class extends Error{constructor(n){super(`unsupported anthropic content block type "${n}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}};function _t(e){if(Array.isArray(e))return e.map(n=>{let t=n;return{type:"function",name:t.name,description:t.description,parameters:t.input_schema}})}function wt(e){if(typeof e=="string")return e;if(e==null)return"";if(!Array.isArray(e))throw new b(typeof e);let n="";for(let t of e){let r=String(t.type??"");if(r!=="text")throw new b(`tool_result > ${r}`);n+=String(t.text??"")}return n}function xt(e){let{role:n,content:t}=e;if(typeof t=="string")return[{role:n,content:t}];if(t==null)return[];if(!Array.isArray(t))throw new b(typeof t);let r=[],o="",s=()=>{o&&(r.push({role:n,content:o}),o="")};for(let a of t){let i=String(a.type??"");if(i==="text")o+=String(a.text??"");else if(i==="tool_use")s(),r.push({type:"function_call",call_id:a.id,name:a.name,arguments:JSON.stringify(a.input??{})});else if(i==="tool_result")s(),r.push({type:"function_call_output",call_id:a.tool_use_id,output:wt(a.content)});else{if(i==="thinking"||i==="redacted_thinking")continue;throw new b(i)}}return s(),r}function ve(e){return te(e).map((n,t)=>{let r;try{r=JSON.parse(n.arguments||"{}")}catch{throw new Error(`upstream function_call "${n.name??""}" arguments are not valid JSON`)}return{type:"tool_use",id:n.call_id??n.id??`toolu_${t}`,name:n.name??"",input:r}})}async function vt(e,n,t,r){let o=e.messages;if(!Array.isArray(o)){_(t,400,"messages requires a messages array");return}let s=String(e.model??"urun"),a=`msg_${Math.random().toString(36).slice(2,14)}`,i=[];try{e.system&&i.push({role:"system",content:ht(e.system)});for(let c of o)i.push(...xt(c))}catch(c){if(c instanceof b){_(t,400,c.message);return}throw c}let u=ee(e);r.model(u);let d;try{d=await n.createResponse({model:e.model,input:i,stream:!0,tools:_t(e.tools),temperature:e.temperature,max_output_tokens:e.max_tokens})}catch(c){if(c instanceof k){Z(t,"anthropic",c);return}throw c}let l=(c,h)=>{t.write(`event: ${c}
20
- data: ${JSON.stringify({type:c,...h})}
21
-
22
- `)};if(e.stream===!0){let c=r.track(d,u);Q(t),l("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:s,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}}),l("content_block_start",{index:0,content_block:{type:"text",text:""}});let h=0,S=[];for await(let g of c)if(S.push(g),g.type==="response.output_text.delta"&&typeof g.delta=="string")h+=1,l("content_block_delta",{index:0,delta:{type:"text_delta",text:g.delta}});else if(g.type==="error"){l("error",{error:{type:"api_error",message:JSON.stringify(g)}}),t.end();return}let A;try{A=ve(L(S))}catch(g){l("error",{error:{type:"api_error",message:String(g instanceof Error?g.message:g)}}),t.end();return}l("content_block_stop",{index:0}),A.forEach((g,E)=>{let P=1+E;l("content_block_start",{index:P,content_block:{...g,input:{}}}),l("content_block_delta",{index:P,delta:{type:"input_json_delta",partial_json:JSON.stringify(g.input??{})}}),l("content_block_stop",{index:P})}),l("message_delta",{delta:{stop_reason:A.length>0?"tool_use":"end_turn"},usage:{output_tokens:h}}),l("message_stop",{}),t.end();return}let m="",w=[];for await(let c of r.track(d,u))if(w.push(c),c.type==="response.output_text.delta"&&typeof c.delta=="string"&&(m+=c.delta),c.type==="error"){_(t,502,JSON.stringify(c),"upstream_error");return}let x;try{x=ve(L(w))}catch(c){_(t,502,String(c instanceof Error?c.message:c),"upstream_error");return}let p=[];(m||x.length===0)&&p.push({type:"text",text:m}),p.push(...x),R(t,200,{id:a,type:"message",role:"assistant",content:p,model:s,stop_reason:x.length>0?"tool_use":"end_turn",usage:{input_tokens:0,output_tokens:0}})}function Re(e){let{clients:n,apiKey:t,identity:r}=e,o=new V,s=new X;return(0,ke.createServer)((a,i)=>{(async()=>{let u=new URL(a.url??"/","http://localhost");if(a.method==="GET"&&u.pathname==="/healthz"){R(i,200,{ok:!0});return}if(a.method==="GET"&&u.pathname==="/stats"){R(i,200,r?{identity:r,...s.snapshot()}:s.snapshot());return}if(t&&(a.headers.authorization??"")!==`Bearer ${t}`){_(i,401,"invalid local proxy api key","authentication_error");return}if(a.method==="GET"&&u.pathname==="/v1/models"){R(i,200,await n.listModels());return}if(a.method!=="POST"){_(i,404,`no route for ${a.method} ${u.pathname}`);return}let d;try{d=await ut(a)}catch(m){if(m instanceof q){_(i,413,m.message,"request_too_large");return}_(i,400,"request body is not valid JSON");return}let l=Buffer.byteLength(JSON.stringify(d));if(u.pathname==="/v1/responses"){s.request("responses",l,l),await yt(d,n,o,i,s);return}if(u.pathname==="/v1/chat/completions"){s.request("chat_completions",l,l),await gt(d,n,i,s);return}if(u.pathname==="/v1/messages"){s.request("messages",l,l),await vt(d,n,i,s);return}_(i,404,`no route for POST ${u.pathname}`)})().catch(u=>{i.headersSent?i.end():_(i,500,String(u),"proxy_error")})})}var be=D(require("os"),1),Se=D(require("path"),1),Ee=require("util");var Ae="0.2.51";var Pe=4141,Rt=Ae,W={claude:{bin:"claude",env:"anthropic"},codex:{bin:"codex",env:"openai"},pi:{bin:"pi",env:"openai"},aider:{bin:"aider",env:"openai"},opencode:{bin:"opencode",env:"openai"},crush:{bin:"crush",env:"openai"}};function oe(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 Ue(e,n,t){let r={...e,...oe(n,t)};return delete r.URUN_API_KEY,delete r.URUN_JWT,r}function Ie(e,n,t){let r=["-c","model_providers.urun.name=urun","-c",`model_providers.urun.base_url="http://127.0.0.1:${e}/v1"`,"-c",'model_providers.urun.wire_api="responses"',"-c",'model_provider="urun"'],o=t.indexOf("--"),a=(o===-1?t:t.slice(0,o)).some(i=>i==="-m"||i==="--model"||i.startsWith("--model="));return[...r,...a?[]:["-m",n],...t]}function Oe(e,n){return n.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?n:["--provider","urun","--model",e,...n]}function $e(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 Ne(e){if(e[0]!=="--port")return null;let n=$e(e[1]);return e.splice(0,2),n}function se(e){let n=e.indexOf("--port");if(n===-1)return Pe;let t=$e(e[n+1]);return e.splice(n,2),t}function Le(e){if(e===0)throw new Error("`env` cannot use --port 0: it starts no proxy, so the OS never assigns the ephemeral port \u2014 pass the actual port of the running proxy");return e}function ne(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 C(e){let n=At(ne(e,"URUN_BASE_URL")),t=ne(e,"URUN_ORG_ID"),r=ne(e,"URUN_APP"),o=(e.URUN_FUNCTION??"serve").trim()||"serve",s=(e.URUN_API_URL??"").trim(),a=s?Y(s,"URUN_API_URL"):ge,i=(e.URUN_JWT??"").trim();if(i)return{baseUrl:n,orgId:t,appId:r,fnName:o,apiUrl:a,auth:{lane:"jwt",jwt:i}};let u=(e.URUN_API_KEY??"").trim();if(u){let d=(e.URUN_GATEWAY_URL??"").trim(),l=d?Y(d):void 0;return{baseUrl:n,orgId:t,appId:r,fnName:o,apiUrl:a,auth:{lane:"api-key",apiKey:u,gatewayUrl:l}}}throw new Error("neither URUN_API_KEY nor URUN_JWT is set. Set URUN_API_KEY (the org API key \u2014 the proxy vends short-lived, function-scoped client tokens from it via the SDK, so the key never reaches the agent process) or URUN_JWT (an explicit pre-vended token; when set it always wins). URUN_BASE_URL, URUN_ORG_ID and URUN_APP are also required.")}function At(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 Y(e,n="URUN_GATEWAY_URL"){let t=new URL(e),r=t.hostname==="localhost"||t.hostname==="127.0.0.1"||t.hostname==="[::1]";if(t.protocol!=="https:"&&!r)throw new Error(`${n} must be https (got ${e}) \u2014 the org API key rides the request's Authorization header`);return e}function ie(e){return{app:e.appId,org:e.orgId,fn:e.fnName,base_url:e.baseUrl,proxy_version:Rt}}function bt(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 St=250;async function ae(e,n=St){try{let t=await fetch(`http://127.0.0.1:${e}/stats`,{signal:AbortSignal.timeout(n)});if(!t.ok)return null;let r=(await t.json()).identity;if(r==null||typeof r!="object")return null;let{app:o,org:s,fn:a,base_url:i,proxy_version:u}=r;return typeof o!="string"||typeof s!="string"||typeof a!="string"||typeof i!="string"||typeof u!="string"?null:{app:o,org:s,fn:a,base_url:i,proxy_version:u}}catch{return null}}async function Ce(e,n,t=Pe){if(e!=null)return{mode:"spawn",port:e};let r=await ae(t);return r!==null&&bt(r,n)?{mode:"reuse",port:t,identity:r}:{mode:"spawn",port:0}}function Te(e,n=be.default.homedir()){return Se.default.join(n,".urun","logs",`compat-proxy-${e}.log`)}var re=["log","info","warn","error","debug","trace"];function Me(e,n=process,t=console){let r=n.stdout.write,o=n.stderr.write,s=(i,...u)=>e.write(i,...u);n.stdout.write=s,n.stderr.write=s;let a=Object.fromEntries(re.map(i=>[i,t[i]]));for(let i of re)t[i]=(...u)=>{e.write(`${(0,Ee.format)(...u)}
23
- `)};return()=>{n.stdout.write=r,n.stderr.write=o;for(let i of re)t[i]=a[i]}}function je(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}`}function De(e,n,t){let r=e[t];if(typeof r!="function")throw new Error(`app "${n}" has no function "${t}" (set URUN_FUNCTION)`);let o=r();if(typeof o.end!="function")throw new Error(`app "${n}" function "${t}" returned a session without end()`);return o}function Be(e,n){let{baseUrl:t,orgId:r,fnName:o,auth:s}=e;if(s.lane==="jwt"){let l=(0,T.App)(n,{baseUrl:t,orgId:r,jwt:s.jwt}),m=De(l,n,o);return{session:m,responses:new N(m)}}let{apiKey:a,gatewayUrl:i}=s,u=(0,T.App)(n,{baseUrl:t,orgId:r,getAccessToken:async()=>(await(0,T.createClientToken)(a,{baseUrl:i,expiresIn:300,allowedFunctions:[`${n}/${o}`]})).token}),d=De(u,n,o);return{session:d,responses:new N(d)}}function Ge(e){let n=(f.default.env.URUN_CATALOG_URL??"").trim(),t=n?Y(n,"URUN_CATALOG_URL"):"",r=(f.default.env.URUN_CATALOG_ANON_KEY??"").trim();(!t||!r)&&f.default.stderr.write(`urun compat: catalog oracle off (URUN_CATALOG_URL/URUN_CATALOG_ANON_KEY unset) \u2014 an undeployed uRun CATALOG model name falls through to the default app instead of a loud 404; deployed apps still resolve exactly, and /stats.models records every mapping
24
- `);let o=e.auth,s=new J({defaultApp:e.appId,fnName:e.fnName,openSession:a=>Be(e,a),closeSession:async a=>{await a.session.end()},listApps:o.lane==="api-key"?()=>he({apiUrl:e.apiUrl,apiKey:o.apiKey}):null,listCatalog:t&&r?()=>ye({catalogUrl:t,anonKey:r}):null});return s.seed(e.appId,Be(e,e.appId)),s}function Ye(e){return{createResponse:async n=>{let{entry:t}=await e.sessionFor(n.model);return t.responses.responses.create(n)},listModels:()=>e.modelList()}}async function Et(e,n,t){let r=(0,pe.spawn)(e,n,{stdio:"inherit",env:t});return new Promise(o=>{r.once("exit",s=>o(s??1)),r.once("error",s=>{f.default.stderr.write(`failed to launch ${e}: ${String(s)}
25
- `),o(127)})})}function Pt(){let e=(0,Ke.fileURLToPath)(new URL("../pi-extension/index.js",v));if(!(0,I.existsSync)(e))throw new Error(`pi extension entry not found at ${e} \u2014 the pi lane loads @urun-sh/openai/pi-extension from the installed package (run \`urun compat pi\` via the npm-installed CLI, e.g. \`npx @urun-sh/openai\`), or pass it to pi yourself: \`pi -e <path-to-@urun-sh/openai>/dist/pi-extension.js\``);return e}function We(e){for(let[n,t]of Object.entries(oe(e)))f.default.stdout.write(`export ${n}=${t}
26
- `)}async function Je(e,n){let t=C(f.default.env),r=n?ie(t):void 0,o=Ge(t),s=Re({clients:Ye(o),identity:r});try{await new Promise((i,u)=>{s.once("error",u),s.listen(e,"127.0.0.1",i)})}catch(i){throw await o.closeAll().catch(()=>{}),i.code==="EADDRINUSE"?new Error(je(e,await ae(e))):i}let a=s.address().port;return n&&(f.default.stdout.write(`urun openai proxy listening on http://127.0.0.1:${a}/v1
27
- `),We(a)),{port:a,close:async()=>{let i=new Promise(u=>s.close(()=>u()));s.closeAllConnections(),await i,await o.closeAll()}}}async function Ut(){let e=f.default.argv.slice(2),n=Ne(e),t=e.shift()??"proxy";if(t==="env")return We(Le(n??se(e))),0;if(t==="proxy"){let o=n??se(e),s=await Je(o,!0);return await new Promise(a=>{f.default.once("SIGINT",a),f.default.once("SIGTERM",a)}),await s.close(),0}let r=t==="run"?e.shift():t;{if(!r)throw new Error(`launch needs an agent (${Object.keys(W).join("|")}) or \`-- <cmd>\``);if(r==="pi"){let p=C(f.default.env);return Et("pi",["-e",Pt(),...Oe(p.appId,e)],f.default.env)}let o,s,a="openai";if(r==="--"){if(o=e.shift()??"",s=e,!o)throw new Error("`-- <cmd>` needs a command to launch")}else{let p=W[r];if(!p)throw new Error(`unknown command/agent ${r}; commands: proxy | env; agents: ${Object.keys(W).join(" | ")} \u2014 or \`-- <cmd>\``);o=p.bin,s=[...p.args??[],...e],a=p.env}let i=await Ce(n,ie(C(f.default.env))),u=null,d=i.port;i.mode==="reuse"&&f.default.stdout.write(`reusing the running urun openai proxy on http://127.0.0.1:${d} (app ${i.identity.app}, fn ${i.identity.fn}, v${i.identity.proxy_version})
28
- `);let l=null,m=null;if(i.mode!=="reuse"){u=await Je(i.port,!1),d=u.port;let p=Te(d);(0,I.mkdirSync)(qe.default.dirname(p),{recursive:!0}),m=(0,I.createWriteStream)(p,{flags:"a"}),m.on("error",c=>{l?.(),l=null,f.default.stderr.write(`urun compat: proxy log ${p} failed: ${String(c)} \u2014 proxy output continues on the terminal
29
- `)}),f.default.stdout.write(`urun openai proxy on http://127.0.0.1:${d}/v1 \u2014 proxy logs: ${p}
30
- `),l=Me(m)}r==="codex"&&(s=Ie(d,C(f.default.env).appId,s));let w=(0,pe.spawn)(o,s,{stdio:"inherit",env:Ue(f.default.env,d,a)}),x=await new Promise(p=>{w.once("exit",c=>p(c??1)),w.once("error",c=>{l?.(),l=null,f.default.stderr.write(`failed to launch ${o}: ${String(c)}
31
- `),p(127)})});try{u&&await u.close()}finally{l?.(),l=null,m?.end()}return x}}Ut().then(e=>f.default.exit(e),e=>{f.default.stderr.write(`${String(e instanceof Error?e.message:e)}
32
- `),f.default.exit(1)});0&&(module.exports={buildClients,buildRouter});
19
+ `),t.end();return}let l="",f=[];for await(let p of r.track(d,u))if(f.push(p),p.type==="response.output_text.delta"&&typeof p.delta=="string"&&(l+=p.delta),p.type==="error"){_(t,502,JSON.stringify(p),"upstream_error");return}let w=we(L(f)).map(({index:p,...c})=>c),x={role:"assistant",content:l||null};w.length>0&&(x.tool_calls=w),R(t,200,{id:a,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:s,choices:[{index:0,message:x,finish_reason:w.length>0?"tool_calls":"stop"}],usage:{prompt_tokens:0,completion_tokens:0,total_tokens:0}})}function _t(e){return typeof e=="string"?e:Array.isArray(e)?e.filter(n=>n.type==="text").map(n=>String(n.text??"")).join(""):""}var A=class extends Error{constructor(n){super(`unsupported anthropic content block type "${n}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}};function wt(e){if(Array.isArray(e))return e.map(n=>{let t=n;return{type:"function",name:t.name,description:t.description,parameters:t.input_schema}})}function xt(e){if(typeof e=="string")return e;if(e==null)return"";if(!Array.isArray(e))throw new A(typeof e);let n="";for(let t of e){let r=String(t.type??"");if(r!=="text")throw new A(`tool_result > ${r}`);n+=String(t.text??"")}return n}function vt(e){let{role:n,content:t}=e;if(typeof t=="string")return[{role:n,content:t}];if(t==null)return[];if(!Array.isArray(t))throw new A(typeof t);let r=[],o="",s=()=>{o&&(r.push({role:n,content:o}),o="")};for(let a of t){let i=String(a.type??"");if(i==="text")o+=String(a.text??"");else if(i==="tool_use")s(),r.push({type:"function_call",call_id:a.id,name:a.name,arguments:JSON.stringify(a.input??{})});else if(i==="tool_result")s(),r.push({type:"function_call_output",call_id:a.tool_use_id,output:xt(a.content)});else{if(i==="thinking"||i==="redacted_thinking")continue;throw new A(i)}}return s(),r}function ve(e){return te(e).map((n,t)=>{let r;try{r=JSON.parse(n.arguments||"{}")}catch{throw new Error(`upstream function_call "${n.name??""}" arguments are not valid JSON`)}return{type:"tool_use",id:n.call_id??n.id??`toolu_${t}`,name:n.name??"",input:r}})}async function kt(e,n,t,r){let o=e.messages;if(!Array.isArray(o)){_(t,400,"messages requires a messages array");return}let s=String(e.model??"urun"),a=`msg_${Math.random().toString(36).slice(2,14)}`,i=[];try{e.system&&i.push({role:"system",content:_t(e.system)});for(let c of o)i.push(...vt(c))}catch(c){if(c instanceof A){_(t,400,c.message);return}throw c}let u=ee(e);r.model(u);let d;try{d=await n.createResponse({model:e.model,input:i,stream:!0,tools:wt(e.tools),temperature:e.temperature,max_output_tokens:e.max_tokens})}catch(c){if(c instanceof k){Z(t,"anthropic",c);return}throw c}let l=(c,y)=>{t.write(`event: ${c}
20
+ data: ${JSON.stringify({type:c,...y})}
21
+
22
+ `)};if(e.stream===!0){let c=r.track(d,u);Q(t),l("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:s,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}}),l("content_block_start",{index:0,content_block:{type:"text",text:""}});let y=0,S=[];for await(let h of c)if(S.push(h),h.type==="response.output_text.delta"&&typeof h.delta=="string")y+=1,l("content_block_delta",{index:0,delta:{type:"text_delta",text:h.delta}});else if(h.type==="error"){l("error",{error:{type:"api_error",message:JSON.stringify(h)}}),t.end();return}let b;try{b=ve(L(S))}catch(h){l("error",{error:{type:"api_error",message:String(h instanceof Error?h.message:h)}}),t.end();return}l("content_block_stop",{index:0}),b.forEach((h,E)=>{let P=1+E;l("content_block_start",{index:P,content_block:{...h,input:{}}}),l("content_block_delta",{index:P,delta:{type:"input_json_delta",partial_json:JSON.stringify(h.input??{})}}),l("content_block_stop",{index:P})}),l("message_delta",{delta:{stop_reason:b.length>0?"tool_use":"end_turn"},usage:{output_tokens:y}}),l("message_stop",{}),t.end();return}let f="",w=[];for await(let c of r.track(d,u))if(w.push(c),c.type==="response.output_text.delta"&&typeof c.delta=="string"&&(f+=c.delta),c.type==="error"){_(t,502,JSON.stringify(c),"upstream_error");return}let x;try{x=ve(L(w))}catch(c){_(t,502,String(c instanceof Error?c.message:c),"upstream_error");return}let p=[];(f||x.length===0)&&p.push({type:"text",text:f}),p.push(...x),R(t,200,{id:a,type:"message",role:"assistant",content:p,model:s,stop_reason:x.length>0?"tool_use":"end_turn",usage:{input_tokens:0,output_tokens:0}})}function Re(e){let{clients:n,apiKey:t,identity:r}=e,o=new V,s=new X;return(0,ke.createServer)((a,i)=>{(async()=>{let u=new URL(a.url??"/","http://localhost");if(a.method==="GET"&&u.pathname==="/healthz"){R(i,200,{ok:!0});return}if(a.method==="GET"&&u.pathname==="/stats"){R(i,200,r?{identity:r,...s.snapshot()}:s.snapshot());return}if(t&&(a.headers.authorization??"")!==`Bearer ${t}`){_(i,401,"invalid local proxy api key","authentication_error");return}if(a.method==="GET"&&u.pathname==="/v1/models"){R(i,200,await n.listModels());return}if(a.method!=="POST"){_(i,404,`no route for ${a.method} ${u.pathname}`);return}let d;try{d=await ct(a)}catch(f){if(f instanceof q){_(i,413,f.message,"request_too_large");return}_(i,400,"request body is not valid JSON");return}let l=Buffer.byteLength(JSON.stringify(d));if(u.pathname==="/v1/responses"){s.request("responses",l,l),await gt(d,n,o,i,s);return}if(u.pathname==="/v1/chat/completions"){s.request("chat_completions",l,l),await ht(d,n,i,s);return}if(u.pathname==="/v1/messages"){s.request("messages",l,l),await kt(d,n,i,s);return}_(i,404,`no route for POST ${u.pathname}`)})().catch(u=>{i.headersSent?i.end():_(i,500,String(u),"proxy_error")})})}var Ae=D(require("os"),1),Se=D(require("path"),1),Ee=require("url"),Pe=require("util");var be="0.2.53";var Ue=4141,bt=be,F={claude:{bin:"claude",env:"anthropic"},codex:{bin:"codex",env:"openai"},pi:{bin:"pi",env:"openai"},aider:{bin:"aider",env:"openai"},opencode:{bin:"opencode",env:"openai"},crush:{bin:"crush",env:"openai"}};function oe(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 Ie(e,n,t){let r={...e,...oe(n,t)};return delete r.URUN_API_KEY,delete r.URUN_JWT,r}function Oe(e,n,t){let r=["-c","model_providers.urun.name=urun","-c",`model_providers.urun.base_url="http://127.0.0.1:${e}/v1"`,"-c",'model_providers.urun.wire_api="responses"',"-c",'model_provider="urun"'],o=t.indexOf("--"),s=o===-1?t:t.slice(0,o),a=s.some(l=>l==="-m"||l==="--model"||l.startsWith("--model=")),i=t[0]==="exec",u=s.includes("--skip-git-repo-check"),d=i&&!u?["exec","--skip-git-repo-check",...t.slice(1)]:t;return[...r,...a?[]:["-m",n],...d]}function $e(e,n){return n.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?n:["--provider","urun","--model",e,...n]}function Ne(e,n,t){let r=(e.URUN_PI_EXTENSION_ENTRY??"").trim();if(r){if(!t(r))throw new Error(`URUN_PI_EXTENSION_ENTRY points at ${r}, which does not exist \u2014 the launcher that injected it (the vendored urun CLI shim) must materialize the pi extension bundle before spawning pi`);return r}let o=(0,Ee.fileURLToPath)(new URL("../pi-extension/standalone.cjs",n));if(!t(o))throw new Error(`pi extension entry not found at ${o} \u2014 the pi lane loads the self-contained @urun-sh/openai dist/pi-extension/standalone.cjs from the installed package (run \`urun compat pi\` via the npm-installed CLI, e.g. \`npx @urun-sh/openai\`), or pass an entry to pi yourself: \`pi -e <path-to-@urun-sh/openai>/dist/pi-extension/standalone.cjs\``);return o}function Le(e){let n=Number(e);if(!Number.isInteger(n)||n<0)throw new Error(`--port needs a non-negative integer (0 = OS-assigned ephemeral), got ${e}`);return n}function Ce(e){if(e[0]!=="--port")return null;let n=Le(e[1]);return e.splice(0,2),n}function se(e){let n=e.indexOf("--port");if(n===-1)return Ue;let t=Le(e[n+1]);return e.splice(n,2),t}function Te(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 ne(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 C(e){let n=At(ne(e,"URUN_BASE_URL")),t=ne(e,"URUN_ORG_ID"),r=ne(e,"URUN_APP"),o=(e.URUN_FUNCTION??"serve").trim()||"serve",s=(e.URUN_API_URL??"").trim(),a=s?Y(s,"URUN_API_URL"):ge,i=(e.URUN_JWT??"").trim();if(i)return{baseUrl:n,orgId:t,appId:r,fnName:o,apiUrl:a,auth:{lane:"jwt",jwt:i}};let u=(e.URUN_API_KEY??"").trim();if(u){let d=(e.URUN_GATEWAY_URL??"").trim(),l=d?Y(d):void 0;return{baseUrl:n,orgId:t,appId:r,fnName:o,apiUrl:a,auth:{lane:"api-key",apiKey:u,gatewayUrl:l}}}throw new Error("neither URUN_API_KEY nor URUN_JWT is set. Set URUN_API_KEY (the org API key \u2014 the proxy vends short-lived, function-scoped client tokens from it via the SDK, so the key never reaches the agent process) or URUN_JWT (an explicit pre-vended token; when set it always wins). URUN_BASE_URL, URUN_ORG_ID and URUN_APP are also required.")}function At(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 Y(e,n="URUN_GATEWAY_URL"){let t=new URL(e),r=t.hostname==="localhost"||t.hostname==="127.0.0.1"||t.hostname==="[::1]";if(t.protocol!=="https:"&&!r)throw new Error(`${n} must be https (got ${e}) \u2014 the org API key rides the request's Authorization header`);return e}function ie(e){return{app:e.appId,org:e.orgId,fn:e.fnName,base_url:e.baseUrl,proxy_version:bt}}function St(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 Et=250;async function ae(e,n=Et){try{let t=await fetch(`http://127.0.0.1:${e}/stats`,{signal:AbortSignal.timeout(n)});if(!t.ok)return null;let r=(await t.json()).identity;if(r==null||typeof r!="object")return null;let{app:o,org:s,fn:a,base_url:i,proxy_version:u}=r;return typeof o!="string"||typeof s!="string"||typeof a!="string"||typeof i!="string"||typeof u!="string"?null:{app:o,org:s,fn:a,base_url:i,proxy_version:u}}catch{return null}}async function je(e,n,t=Ue){if(e!=null)return{mode:"spawn",port:e};let r=await ae(t);return r!==null&&St(r,n)?{mode:"reuse",port:t,identity:r}:{mode:"spawn",port:0}}function Me(e,n=Ae.default.homedir()){return Se.default.join(n,".urun","logs",`compat-proxy-${e}.log`)}var re=["log","info","warn","error","debug","trace"];function De(e,n=process,t=console){let r=n.stdout.write,o=n.stderr.write,s=(i,...u)=>e.write(i,...u);n.stdout.write=s,n.stderr.write=s;let a=Object.fromEntries(re.map(i=>[i,t[i]]));for(let i of re)t[i]=(...u)=>{e.write(`${(0,Pe.format)(...u)}
23
+ `)};return()=>{n.stdout.write=r,n.stderr.write=o;for(let i of re)t[i]=a[i]}}function Be(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}`}function Je(e,n,t){let r=e[t];if(typeof r!="function")throw new Error(`app "${n}" has no function "${t}" (set URUN_FUNCTION)`);let o=r();if(typeof o.end!="function")throw new Error(`app "${n}" function "${t}" returned a session without end()`);return o}function Ke(e,n){let{baseUrl:t,orgId:r,fnName:o,auth:s}=e;if(s.lane==="jwt"){let l=(0,T.App)(n,{baseUrl:t,orgId:r,jwt:s.jwt}),f=Je(l,n,o);return{session:f,responses:new N(f)}}let{apiKey:a,gatewayUrl:i}=s,u=(0,T.App)(n,{baseUrl:t,orgId:r,getAccessToken:async()=>(await(0,T.createClientToken)(a,{baseUrl:i,expiresIn:300,allowedFunctions:[`${n}/${o}`]})).token}),d=Je(u,n,o);return{session:d,responses:new N(d)}}function Ye(e){let n=(m.default.env.URUN_CATALOG_URL??"").trim(),t=n?Y(n,"URUN_CATALOG_URL"):"",r=(m.default.env.URUN_CATALOG_ANON_KEY??"").trim();(!t||!r)&&m.default.stderr.write(`urun compat: catalog oracle off (URUN_CATALOG_URL/URUN_CATALOG_ANON_KEY unset) \u2014 an undeployed uRun CATALOG model name falls through to the default app instead of a loud 404; deployed apps still resolve exactly, and /stats.models records every mapping
24
+ `);let o=e.auth,s=new J({defaultApp:e.appId,fnName:e.fnName,openSession:a=>Ke(e,a),closeSession:async a=>{await a.session.end()},listApps:o.lane==="api-key"?()=>he({apiUrl:e.apiUrl,apiKey:o.apiKey}):null,listCatalog:t&&r?()=>ye({catalogUrl:t,anonKey:r}):null});return s.seed(e.appId,Ke(e,e.appId)),s}function Fe(e){return{createResponse:async n=>{let{entry:t}=await e.sessionFor(n.model);return t.responses.responses.create(n)},listModels:()=>e.modelList()}}async function Pt(e,n,t){let r=(0,pe.spawn)(e,n,{stdio:"inherit",env:t});return new Promise(o=>{r.once("exit",s=>o(s??1)),r.once("error",s=>{m.default.stderr.write(`failed to launch ${e}: ${String(s)}
25
+ `),o(127)})})}function Ut(){return Ne(m.default.env,v,I.existsSync)}function We(e){for(let[n,t]of Object.entries(oe(e)))m.default.stdout.write(`export ${n}=${t}
26
+ `)}async function qe(e,n){let t=C(m.default.env),r=n?ie(t):void 0,o=Ye(t),s=Re({clients:Fe(o),identity:r});try{await new Promise((i,u)=>{s.once("error",u),s.listen(e,"127.0.0.1",i)})}catch(i){throw await o.closeAll().catch(()=>{}),i.code==="EADDRINUSE"?new Error(Be(e,await ae(e))):i}let a=s.address().port;return n&&(m.default.stdout.write(`urun openai proxy listening on http://127.0.0.1:${a}/v1
27
+ `),We(a)),{port:a,close:async()=>{let i=new Promise(u=>s.close(()=>u()));s.closeAllConnections(),await i,await o.closeAll()}}}async function It(){let e=m.default.argv.slice(2),n=Ce(e),t=e.shift()??"proxy";if(t==="env")return We(Te(n??se(e))),0;if(t==="proxy"){let o=n??se(e),s=await qe(o,!0);return await new Promise(a=>{m.default.once("SIGINT",a),m.default.once("SIGTERM",a)}),await s.close(),0}let r=t==="run"?e.shift():t;{if(!r)throw new Error(`launch needs an agent (${Object.keys(F).join("|")}) or \`-- <cmd>\``);if(r==="pi"){let c=C(m.default.env);return Pt("pi",["-e",Ut(),...$e(c.appId,e)],m.default.env)}let o,s,a="openai";if(r==="--"){if(o=e.shift()??"",s=e,!o)throw new Error("`-- <cmd>` needs a command to launch")}else{let c=F[r];if(!c)throw new Error(`unknown command/agent ${r}; commands: proxy | env; agents: ${Object.keys(F).join(" | ")} \u2014 or \`-- <cmd>\``);o=c.bin,s=[...c.args??[],...e],a=c.env}let i=await je(n,ie(C(m.default.env))),u=null,d=i.port;i.mode==="reuse"&&m.default.stdout.write(`reusing the running urun openai proxy on http://127.0.0.1:${d} (app ${i.identity.app}, fn ${i.identity.fn}, v${i.identity.proxy_version})
28
+ `);let l=null,f=null;if(i.mode!=="reuse"){u=await qe(i.port,!1),d=u.port;let c=Me(d);(0,I.mkdirSync)(Ge.default.dirname(c),{recursive:!0}),f=(0,I.createWriteStream)(c,{flags:"a"}),f.on("error",y=>{l?.(),l=null,m.default.stderr.write(`urun compat: proxy log ${c} failed: ${String(y)} \u2014 proxy output continues on the terminal
29
+ `)}),m.default.stdout.write(`urun openai proxy on http://127.0.0.1:${d}/v1 \u2014 proxy logs: ${c}
30
+ `),l=De(f)}r==="codex"&&(s=Oe(d,C(m.default.env).appId,s));let w=(0,pe.spawn)(o,s,{stdio:"inherit",env:Ie(m.default.env,d,a)}),x=await new Promise(c=>{w.once("exit",y=>c(y??1)),w.once("error",y=>{l?.(),l=null,m.default.stderr.write(`failed to launch ${o}: ${String(y)}
31
+ `),c(127)})}),p=null;try{u&&await u.close()}catch(c){p=c}finally{l?.(),l=null,f?.end()}return p!==null&&m.default.stderr.write(`urun compat: proxy teardown failed after the harness exited (harness exit code ${x} preserved): ${String(p instanceof Error?p.message:p)}
32
+ `),x}}It().then(e=>m.default.exit(e),e=>{m.default.stderr.write(`${String(e instanceof Error?e.message:e)}
33
+ `),m.default.exit(1)});0&&(module.exports={buildClients,buildRouter});
package/dist/proxy/cli.js CHANGED
@@ -1,12 +1,13 @@
1
1
  #!/usr/bin/env node
2
- import{a as E}from"../chunk-OLE2YJO3.js";import{a as I}from"../chunk-KJ25VEMG.js";import{c as x}from"../chunk-FSS2A7IU.js";import{a as A,c as S,d as N}from"../chunk-PS4BLX7J.js";import{spawn as F}from"child_process";import{createWriteStream as ie,existsSync as ae,mkdirSync as pe}from"fs";import{fileURLToPath as ue}from"url";import ce from"path";import p from"process";import{App as J,createClientToken as de}from"@urun-sh/core";import Z from"os";import ee from"path";import{format as te}from"util";var O="0.2.51";var $=4141,ne=O,h={claude:{bin:"claude",env:"anthropic"},codex:{bin:"codex",env:"openai"},pi:{bin:"pi",env:"openai"},aider:{bin:"aider",env:"openai"},opencode:{bin:"opencode",env:"openai"},crush:{bin:"crush",env:"openai"}};function v(e,t="openai"){if(t==="anthropic")return{ANTHROPIC_BASE_URL:`http://127.0.0.1:${e}`,ANTHROPIC_API_KEY:"urun-local"};let n=`http://127.0.0.1:${e}/v1`;return{OPENAI_BASE_URL:n,OPENAI_API_BASE:n,OPENAI_API_KEY:"urun-local"}}function k(e,t,n){let r={...e,...v(t,n)};return delete r.URUN_API_KEY,delete r.URUN_JWT,r}function L(e,t,n){let r=["-c","model_providers.urun.name=urun","-c",`model_providers.urun.base_url="http://127.0.0.1:${e}/v1"`,"-c",'model_providers.urun.wire_api="responses"',"-c",'model_provider="urun"'],s=n.indexOf("--"),a=(s===-1?n:n.slice(0,s)).some(o=>o==="-m"||o==="--model"||o.startsWith("--model="));return[...r,...a?[]:["-m",t],...n]}function T(e,t){return t.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?t:["--provider","urun","--model",e,...t]}function C(e){let t=Number(e);if(!Number.isInteger(t)||t<0)throw new Error(`--port needs a non-negative integer (0 = OS-assigned ephemeral), got ${e}`);return t}function j(e){if(e[0]!=="--port")return null;let t=C(e[1]);return e.splice(0,2),t}function _(e){let t=e.indexOf("--port");if(t===-1)return $;let n=C(e[t+1]);return e.splice(t,2),n}function G(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 w(e,t){let n=(e[t]??"").trim();if(!n)throw new Error(`${t} 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 n}function f(e){let t=re(w(e,"URUN_BASE_URL")),n=w(e,"URUN_ORG_ID"),r=w(e,"URUN_APP"),s=(e.URUN_FUNCTION??"serve").trim()||"serve",i=(e.URUN_API_URL??"").trim(),a=i?g(i,"URUN_API_URL"):A,o=(e.URUN_JWT??"").trim();if(o)return{baseUrl:t,orgId:n,appId:r,fnName:s,apiUrl:a,auth:{lane:"jwt",jwt:o}};let u=(e.URUN_API_KEY??"").trim();if(u){let d=(e.URUN_GATEWAY_URL??"").trim(),l=d?g(d):void 0;return{baseUrl:t,orgId:n,appId:r,fnName:s,apiUrl:a,auth:{lane:"api-key",apiKey:u,gatewayUrl:l}}}throw new Error("neither URUN_API_KEY nor URUN_JWT is set. Set URUN_API_KEY (the org API key \u2014 the proxy vends short-lived, function-scoped client tokens from it via the SDK, so the key never reaches the agent process) or URUN_JWT (an explicit pre-vended token; when set it always wins). URUN_BASE_URL, URUN_ORG_ID and URUN_APP are also required.")}function re(e,t="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(e).pathname))throw new Error(`${t} 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 g(e,t="URUN_GATEWAY_URL"){let n=new URL(e),r=n.hostname==="localhost"||n.hostname==="127.0.0.1"||n.hostname==="[::1]";if(n.protocol!=="https:"&&!r)throw new Error(`${t} must be https (got ${e}) \u2014 the org API key rides the request's Authorization header`);return e}function R(e){return{app:e.appId,org:e.orgId,fn:e.fnName,base_url:e.baseUrl,proxy_version:ne}}function oe(e,t){return e.app===t.app&&e.org===t.org&&e.fn===t.fn&&e.base_url===t.base_url&&e.proxy_version===t.proxy_version}var se=250;async function P(e,t=se){try{let n=await fetch(`http://127.0.0.1:${e}/stats`,{signal:AbortSignal.timeout(t)});if(!n.ok)return null;let r=(await n.json()).identity;if(r==null||typeof r!="object")return null;let{app:s,org:i,fn:a,base_url:o,proxy_version:u}=r;return typeof s!="string"||typeof i!="string"||typeof a!="string"||typeof o!="string"||typeof u!="string"?null:{app:s,org:i,fn:a,base_url:o,proxy_version:u}}catch{return null}}async function K(e,t,n=$){if(e!=null)return{mode:"spawn",port:e};let r=await P(n);return r!==null&&oe(r,t)?{mode:"reuse",port:n,identity:r}:{mode:"spawn",port:0}}function D(e,t=Z.homedir()){return ee.join(t,".urun","logs",`compat-proxy-${e}.log`)}var U=["log","info","warn","error","debug","trace"];function W(e,t=process,n=console){let r=t.stdout.write,s=t.stderr.write,i=(o,...u)=>e.write(o,...u);t.stdout.write=i,t.stderr.write=i;let a=Object.fromEntries(U.map(o=>[o,n[o]]));for(let o of U)n[o]=(...u)=>{e.write(`${te(...u)}
3
- `)};return()=>{t.stdout.write=r,t.stderr.write=s;for(let o of U)n[o]=a[o]}}function M(e,t){let n="pass --port 0 for an OS-assigned ephemeral port";return t?`port ${e} is held by another urun-openai proxy serving app ${t.app} (org ${t.org}, fn ${t.fn}, version ${t.proxy_version}) \u2014 reuse it by pointing your agent at http://127.0.0.1:${e}, or ${n}`:`port ${e} is already in use and does not answer the urun-openai /stats identity probe \u2014 ${n}`}function Y(e,t,n){let r=e[n];if(typeof r!="function")throw new Error(`app "${t}" has no function "${n}" (set URUN_FUNCTION)`);let s=r();if(typeof s.end!="function")throw new Error(`app "${t}" function "${n}" returned a session without end()`);return s}function B(e,t){let{baseUrl:n,orgId:r,fnName:s,auth:i}=e;if(i.lane==="jwt"){let l=J(t,{baseUrl:n,orgId:r,jwt:i.jwt}),m=Y(l,t,s);return{session:m,responses:new x(m)}}let{apiKey:a,gatewayUrl:o}=i,u=J(t,{baseUrl:n,orgId:r,getAccessToken:async()=>(await de(a,{baseUrl:o,expiresIn:300,allowedFunctions:[`${t}/${s}`]})).token}),d=Y(u,t,s);return{session:d,responses:new x(d)}}function le(e){let t=(p.env.URUN_CATALOG_URL??"").trim(),n=t?g(t,"URUN_CATALOG_URL"):"",r=(p.env.URUN_CATALOG_ANON_KEY??"").trim();(!n||!r)&&p.stderr.write(`urun compat: catalog oracle off (URUN_CATALOG_URL/URUN_CATALOG_ANON_KEY unset) \u2014 an undeployed uRun CATALOG model name falls through to the default app instead of a loud 404; deployed apps still resolve exactly, and /stats.models records every mapping
4
- `);let s=e.auth,i=new N({defaultApp:e.appId,fnName:e.fnName,openSession:a=>B(e,a),closeSession:async a=>{await a.session.end()},listApps:s.lane==="api-key"?()=>S({apiUrl:e.apiUrl,apiKey:s.apiKey}):null,listCatalog:n&&r?()=>E({catalogUrl:n,anonKey:r}):null});return i.seed(e.appId,B(e,e.appId)),i}function me(e){return{createResponse:async t=>{let{entry:n}=await e.sessionFor(t.model);return n.responses.responses.create(t)},listModels:()=>e.modelList()}}async function ye(e,t,n){let r=F(e,t,{stdio:"inherit",env:n});return new Promise(s=>{r.once("exit",i=>s(i??1)),r.once("error",i=>{p.stderr.write(`failed to launch ${e}: ${String(i)}
5
- `),s(127)})})}function fe(){let e=ue(new URL("../pi-extension/index.js",import.meta.url));if(!ae(e))throw new Error(`pi extension entry not found at ${e} \u2014 the pi lane loads @urun-sh/openai/pi-extension from the installed package (run \`urun compat pi\` via the npm-installed CLI, e.g. \`npx @urun-sh/openai\`), or pass it to pi yourself: \`pi -e <path-to-@urun-sh/openai>/dist/pi-extension.js\``);return e}function H(e){for(let[t,n]of Object.entries(v(e)))p.stdout.write(`export ${t}=${n}
6
- `)}async function q(e,t){let n=f(p.env),r=t?R(n):void 0,s=le(n),i=I({clients:me(s),identity:r});try{await new Promise((o,u)=>{i.once("error",u),i.listen(e,"127.0.0.1",o)})}catch(o){throw await s.closeAll().catch(()=>{}),o.code==="EADDRINUSE"?new Error(M(e,await P(e))):o}let a=i.address().port;return t&&(p.stdout.write(`urun openai proxy listening on http://127.0.0.1:${a}/v1
7
- `),H(a)),{port:a,close:async()=>{let o=new Promise(u=>i.close(()=>u()));i.closeAllConnections(),await o,await s.closeAll()}}}async function ge(){let e=p.argv.slice(2),t=j(e),n=e.shift()??"proxy";if(n==="env")return H(G(t??_(e))),0;if(n==="proxy"){let s=t??_(e),i=await q(s,!0);return await new Promise(a=>{p.once("SIGINT",a),p.once("SIGTERM",a)}),await i.close(),0}let r=n==="run"?e.shift():n;{if(!r)throw new Error(`launch needs an agent (${Object.keys(h).join("|")}) or \`-- <cmd>\``);if(r==="pi"){let c=f(p.env);return ye("pi",["-e",fe(),...T(c.appId,e)],p.env)}let s,i,a="openai";if(r==="--"){if(s=e.shift()??"",i=e,!s)throw new Error("`-- <cmd>` needs a command to launch")}else{let c=h[r];if(!c)throw new Error(`unknown command/agent ${r}; commands: proxy | env; agents: ${Object.keys(h).join(" | ")} \u2014 or \`-- <cmd>\``);s=c.bin,i=[...c.args??[],...e],a=c.env}let o=await K(t,R(f(p.env))),u=null,d=o.port;o.mode==="reuse"&&p.stdout.write(`reusing the running urun openai proxy on http://127.0.0.1:${d} (app ${o.identity.app}, fn ${o.identity.fn}, v${o.identity.proxy_version})
8
- `);let l=null,m=null;if(o.mode!=="reuse"){u=await q(o.port,!1),d=u.port;let c=D(d);pe(ce.dirname(c),{recursive:!0}),m=ie(c,{flags:"a"}),m.on("error",y=>{l?.(),l=null,p.stderr.write(`urun compat: proxy log ${c} failed: ${String(y)} \u2014 proxy output continues on the terminal
9
- `)}),p.stdout.write(`urun openai proxy on http://127.0.0.1:${d}/v1 \u2014 proxy logs: ${c}
10
- `),l=W(m)}r==="codex"&&(i=L(d,f(p.env).appId,i));let b=F(s,i,{stdio:"inherit",env:k(p.env,d,a)}),V=await new Promise(c=>{b.once("exit",y=>c(y??1)),b.once("error",y=>{l?.(),l=null,p.stderr.write(`failed to launch ${s}: ${String(y)}
11
- `),c(127)})});try{u&&await u.close()}finally{l?.(),l=null,m?.end()}return V}}ge().then(e=>p.exit(e),e=>{p.stderr.write(`${String(e instanceof Error?e.message:e)}
12
- `),p.exit(1)});export{me as buildClients,le as buildRouter};
2
+ import{a as S}from"../chunk-OLE2YJO3.js";import{a as k}from"../chunk-KJ25VEMG.js";import{c as w}from"../chunk-FSS2A7IU.js";import{a as A,c as I,d as O}from"../chunk-PS4BLX7J.js";import{spawn as V}from"child_process";import{createWriteStream as ce,existsSync as ue,mkdirSync as de}from"fs";import le from"path";import p from"process";import{App as F,createClientToken as me}from"@urun-sh/core";import te from"os";import ne from"path";import{fileURLToPath as re}from"url";import{format as oe}from"util";var $="0.2.53";var L=4141,se=$,x={claude:{bin:"claude",env:"anthropic"},codex:{bin:"codex",env:"openai"},pi:{bin:"pi",env:"openai"},aider:{bin:"aider",env:"openai"},opencode:{bin:"opencode",env:"openai"},crush:{bin:"crush",env:"openai"}};function E(e,t="openai"){if(t==="anthropic")return{ANTHROPIC_BASE_URL:`http://127.0.0.1:${e}`,ANTHROPIC_API_KEY:"urun-local"};let n=`http://127.0.0.1:${e}/v1`;return{OPENAI_BASE_URL:n,OPENAI_API_BASE:n,OPENAI_API_KEY:"urun-local"}}function T(e,t,n){let r={...e,...E(t,n)};return delete r.URUN_API_KEY,delete r.URUN_JWT,r}function j(e,t,n){let r=["-c","model_providers.urun.name=urun","-c",`model_providers.urun.base_url="http://127.0.0.1:${e}/v1"`,"-c",'model_providers.urun.wire_api="responses"',"-c",'model_provider="urun"'],o=n.indexOf("--"),s=o===-1?n:n.slice(0,o),a=s.some(u=>u==="-m"||u==="--model"||u.startsWith("--model=")),i=n[0]==="exec",c=s.includes("--skip-git-repo-check"),l=i&&!c?["exec","--skip-git-repo-check",...n.slice(1)]:n;return[...r,...a?[]:["-m",t],...l]}function C(e,t){return t.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?t:["--provider","urun","--model",e,...t]}function G(e,t,n){let r=(e.URUN_PI_EXTENSION_ENTRY??"").trim();if(r){if(!n(r))throw new Error(`URUN_PI_EXTENSION_ENTRY points at ${r}, which does not exist \u2014 the launcher that injected it (the vendored urun CLI shim) must materialize the pi extension bundle before spawning pi`);return r}let o=re(new URL("../pi-extension/standalone.cjs",t));if(!n(o))throw new Error(`pi extension entry not found at ${o} \u2014 the pi lane loads the self-contained @urun-sh/openai dist/pi-extension/standalone.cjs from the installed package (run \`urun compat pi\` via the npm-installed CLI, e.g. \`npx @urun-sh/openai\`), or pass an entry to pi yourself: \`pi -e <path-to-@urun-sh/openai>/dist/pi-extension/standalone.cjs\``);return o}function K(e){let t=Number(e);if(!Number.isInteger(t)||t<0)throw new Error(`--port needs a non-negative integer (0 = OS-assigned ephemeral), got ${e}`);return t}function D(e){if(e[0]!=="--port")return null;let t=K(e[1]);return e.splice(0,2),t}function _(e){let t=e.indexOf("--port");if(t===-1)return L;let n=K(e[t+1]);return e.splice(t,2),n}function W(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 U(e,t){let n=(e[t]??"").trim();if(!n)throw new Error(`${t} 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 n}function h(e){let t=ie(U(e,"URUN_BASE_URL")),n=U(e,"URUN_ORG_ID"),r=U(e,"URUN_APP"),o=(e.URUN_FUNCTION??"serve").trim()||"serve",s=(e.URUN_API_URL??"").trim(),a=s?g(s,"URUN_API_URL"):A,i=(e.URUN_JWT??"").trim();if(i)return{baseUrl:t,orgId:n,appId:r,fnName:o,apiUrl:a,auth:{lane:"jwt",jwt:i}};let c=(e.URUN_API_KEY??"").trim();if(c){let l=(e.URUN_GATEWAY_URL??"").trim(),u=l?g(l):void 0;return{baseUrl:t,orgId:n,appId:r,fnName:o,apiUrl:a,auth:{lane:"api-key",apiKey:c,gatewayUrl:u}}}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 ie(e,t="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(e).pathname))throw new Error(`${t} 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 g(e,t="URUN_GATEWAY_URL"){let n=new URL(e),r=n.hostname==="localhost"||n.hostname==="127.0.0.1"||n.hostname==="[::1]";if(n.protocol!=="https:"&&!r)throw new Error(`${t} must be https (got ${e}) \u2014 the org API key rides the request's Authorization header`);return e}function P(e){return{app:e.appId,org:e.orgId,fn:e.fnName,base_url:e.baseUrl,proxy_version:se}}function ae(e,t){return e.app===t.app&&e.org===t.org&&e.fn===t.fn&&e.base_url===t.base_url&&e.proxy_version===t.proxy_version}var pe=250;async function R(e,t=pe){try{let n=await fetch(`http://127.0.0.1:${e}/stats`,{signal:AbortSignal.timeout(t)});if(!n.ok)return null;let r=(await n.json()).identity;if(r==null||typeof r!="object")return null;let{app:o,org:s,fn:a,base_url:i,proxy_version:c}=r;return typeof o!="string"||typeof s!="string"||typeof a!="string"||typeof i!="string"||typeof c!="string"?null:{app:o,org:s,fn:a,base_url:i,proxy_version:c}}catch{return null}}async function Y(e,t,n=L){if(e!=null)return{mode:"spawn",port:e};let r=await R(n);return r!==null&&ae(r,t)?{mode:"reuse",port:n,identity:r}:{mode:"spawn",port:0}}function J(e,t=te.homedir()){return ne.join(t,".urun","logs",`compat-proxy-${e}.log`)}var v=["log","info","warn","error","debug","trace"];function M(e,t=process,n=console){let r=t.stdout.write,o=t.stderr.write,s=(i,...c)=>e.write(i,...c);t.stdout.write=s,t.stderr.write=s;let a=Object.fromEntries(v.map(i=>[i,n[i]]));for(let i of v)n[i]=(...c)=>{e.write(`${oe(...c)}
3
+ `)};return()=>{t.stdout.write=r,t.stderr.write=o;for(let i of v)n[i]=a[i]}}function B(e,t){let n="pass --port 0 for an OS-assigned ephemeral port";return t?`port ${e} is held by another urun-openai proxy serving app ${t.app} (org ${t.org}, fn ${t.fn}, version ${t.proxy_version}) \u2014 reuse it by pointing your agent at http://127.0.0.1:${e}, or ${n}`:`port ${e} is already in use and does not answer the urun-openai /stats identity probe \u2014 ${n}`}function q(e,t,n){let r=e[n];if(typeof r!="function")throw new Error(`app "${t}" has no function "${n}" (set URUN_FUNCTION)`);let o=r();if(typeof o.end!="function")throw new Error(`app "${t}" function "${n}" returned a session without end()`);return o}function H(e,t){let{baseUrl:n,orgId:r,fnName:o,auth:s}=e;if(s.lane==="jwt"){let u=F(t,{baseUrl:n,orgId:r,jwt:s.jwt}),m=q(u,t,o);return{session:m,responses:new w(m)}}let{apiKey:a,gatewayUrl:i}=s,c=F(t,{baseUrl:n,orgId:r,getAccessToken:async()=>(await me(a,{baseUrl:i,expiresIn:300,allowedFunctions:[`${t}/${o}`]})).token}),l=q(c,t,o);return{session:l,responses:new w(l)}}function fe(e){let t=(p.env.URUN_CATALOG_URL??"").trim(),n=t?g(t,"URUN_CATALOG_URL"):"",r=(p.env.URUN_CATALOG_ANON_KEY??"").trim();(!n||!r)&&p.stderr.write(`urun compat: catalog oracle off (URUN_CATALOG_URL/URUN_CATALOG_ANON_KEY unset) \u2014 an undeployed uRun CATALOG model name falls through to the default app instead of a loud 404; deployed apps still resolve exactly, and /stats.models records every mapping
4
+ `);let o=e.auth,s=new O({defaultApp:e.appId,fnName:e.fnName,openSession:a=>H(e,a),closeSession:async a=>{await a.session.end()},listApps:o.lane==="api-key"?()=>I({apiUrl:e.apiUrl,apiKey:o.apiKey}):null,listCatalog:n&&r?()=>S({catalogUrl:n,anonKey:r}):null});return s.seed(e.appId,H(e,e.appId)),s}function ye(e){return{createResponse:async t=>{let{entry:n}=await e.sessionFor(t.model);return n.responses.responses.create(t)},listModels:()=>e.modelList()}}async function he(e,t,n){let r=V(e,t,{stdio:"inherit",env:n});return new Promise(o=>{r.once("exit",s=>o(s??1)),r.once("error",s=>{p.stderr.write(`failed to launch ${e}: ${String(s)}
5
+ `),o(127)})})}function ge(){return G(p.env,import.meta.url,ue)}function X(e){for(let[t,n]of Object.entries(E(e)))p.stdout.write(`export ${t}=${n}
6
+ `)}async function z(e,t){let n=h(p.env),r=t?P(n):void 0,o=fe(n),s=k({clients:ye(o),identity:r});try{await new Promise((i,c)=>{s.once("error",c),s.listen(e,"127.0.0.1",i)})}catch(i){throw await o.closeAll().catch(()=>{}),i.code==="EADDRINUSE"?new Error(B(e,await R(e))):i}let a=s.address().port;return t&&(p.stdout.write(`urun openai proxy listening on http://127.0.0.1:${a}/v1
7
+ `),X(a)),{port:a,close:async()=>{let i=new Promise(c=>s.close(()=>c()));s.closeAllConnections(),await i,await o.closeAll()}}}async function xe(){let e=p.argv.slice(2),t=D(e),n=e.shift()??"proxy";if(n==="env")return X(W(t??_(e))),0;if(n==="proxy"){let o=t??_(e),s=await z(o,!0);return await new Promise(a=>{p.once("SIGINT",a),p.once("SIGTERM",a)}),await s.close(),0}let r=n==="run"?e.shift():n;{if(!r)throw new Error(`launch needs an agent (${Object.keys(x).join("|")}) or \`-- <cmd>\``);if(r==="pi"){let d=h(p.env);return he("pi",["-e",ge(),...C(d.appId,e)],p.env)}let o,s,a="openai";if(r==="--"){if(o=e.shift()??"",s=e,!o)throw new Error("`-- <cmd>` needs a command to launch")}else{let d=x[r];if(!d)throw new Error(`unknown command/agent ${r}; commands: proxy | env; agents: ${Object.keys(x).join(" | ")} \u2014 or \`-- <cmd>\``);o=d.bin,s=[...d.args??[],...e],a=d.env}let i=await Y(t,P(h(p.env))),c=null,l=i.port;i.mode==="reuse"&&p.stdout.write(`reusing the running urun openai proxy on http://127.0.0.1:${l} (app ${i.identity.app}, fn ${i.identity.fn}, v${i.identity.proxy_version})
8
+ `);let u=null,m=null;if(i.mode!=="reuse"){c=await z(i.port,!1),l=c.port;let d=J(l);de(le.dirname(d),{recursive:!0}),m=ce(d,{flags:"a"}),m.on("error",y=>{u?.(),u=null,p.stderr.write(`urun compat: proxy log ${d} failed: ${String(y)} \u2014 proxy output continues on the terminal
9
+ `)}),p.stdout.write(`urun openai proxy on http://127.0.0.1:${l}/v1 \u2014 proxy logs: ${d}
10
+ `),u=M(m)}r==="codex"&&(s=j(l,h(p.env).appId,s));let b=V(o,s,{stdio:"inherit",env:T(p.env,l,a)}),N=await new Promise(d=>{b.once("exit",y=>d(y??1)),b.once("error",y=>{u?.(),u=null,p.stderr.write(`failed to launch ${o}: ${String(y)}
11
+ `),d(127)})}),f=null;try{c&&await c.close()}catch(d){f=d}finally{u?.(),u=null,m?.end()}return f!==null&&p.stderr.write(`urun compat: proxy teardown failed after the harness exited (harness exit code ${N} preserved): ${String(f instanceof Error?f.message:f)}
12
+ `),N}}xe().then(e=>p.exit(e),e=>{p.stderr.write(`${String(e instanceof Error?e.message:e)}
13
+ `),p.exit(1)});export{ye as buildClients,fe as buildRouter};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urun-sh/openai",
3
- "version": "0.2.51",
3
+ "version": "0.2.53",
4
4
  "description": "OpenAI-compatible Realtime + Responses SDK over uRun session primitives (not websockets).",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -44,13 +44,13 @@
44
44
  ],
45
45
  "scripts": {
46
46
  "build": "tsup && node ../../scripts/sanitize-dist.mjs .",
47
- "prepack": "pnpm build",
47
+ "prepack": "pnpm build && node scripts/assert-pack-contents.mjs",
48
48
  "test": "vitest run",
49
49
  "dev": "tsup --watch",
50
50
  "build:bin": "bun build --compile --external @roamhq/wrtc --external werift --external opusscript src/proxy/cli.ts --outfile dist/bin/urun-openai"
51
51
  },
52
52
  "peerDependencies": {
53
- "@urun-sh/core": "^0.2.51"
53
+ "@urun-sh/core": "^0.2.53"
54
54
  },
55
55
  "dependencies": {
56
56
  "openai": "4.104.0"
@@ -61,6 +61,7 @@
61
61
  "opusscript": "^0.1.1"
62
62
  },
63
63
  "devDependencies": {
64
+ "reflect-metadata": "^0.2.2",
64
65
  "tsup": "^8.5.0",
65
66
  "vitest": "^3.0.0",
66
67
  "werift": "^0.23.0",
@@ -76,4 +77,4 @@
76
77
  "bin": {
77
78
  "urun-openai": "./dist/proxy/cli.cjs"
78
79
  }
79
- }
80
+ }