@urun-sh/openai 0.2.57 → 0.2.58
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/pi-extension/index.cjs +6 -6
- package/dist/pi-extension/index.d.cts +12 -2
- package/dist/pi-extension/index.d.ts +4 -2
- package/dist/pi-extension/index.js +6 -6
- package/dist/pi-extension/standalone.cjs +52 -52
- package/dist/proxy/cli.cjs +27 -25
- package/dist/proxy/cli.js +16 -14
- package/package.json +4 -4
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var Ce=Object.create;var F=Object.defineProperty;var Le=Object.getOwnPropertyDescriptor;var Me=Object.getOwnPropertyNames;var Oe=Object.getPrototypeOf,Ne=Object.prototype.hasOwnProperty;var De=(t,e)=>{for(var n in e)F(t,n,{get:e[n],enumerable:!0})},se=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Me(e))!Ne.call(t,r)&&r!==n&&F(t,r,{get:()=>e[r],enumerable:!(o=Le(e,r))||o.enumerable});return t};var ie=(t,e,n)=>(n=t!=null?Ce(Oe(t)):{},se(e||!t||!t.__esModule?F(n,"default",{value:t,enumerable:!0}):n,t)),Fe=t=>se(F({},"__esModule",{value:!0}),t);var ft={};De(ft,{SessionPool:()=>H,URUN_API:()=>ee,applyServeUsage:()=>Te,coalesceSameRole:()=>Ee,createFileDiagnosticSink:()=>be,createUrunExtension:()=>Pe,default:()=>dt,makeSessionFactory:()=>Se,makeStreamSimple:()=>Ie,notify:()=>Z,piDiagnosticLogPath:()=>J,resolveSessionEnv:()=>xe,toResponsesInput:()=>Re});module.exports=Fe(ft);var je=()=>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,k=je();var ve=require("@earendil-works/pi-ai"),C=require("fs"),ke=require("path");function ae(t,e,n,o){let r=t.response??{},l={request_id:n,consumer_id:o,stream:!0,kind:"chat",messages:e};return typeof r.instructions=="string"&&(l.instructions=r.instructions),Array.isArray(r.modalities)&&(l.modalities=r.modalities),typeof r.temperature=="number"&&(l.temperature=r.temperature),typeof r.max_output_tokens=="number"&&(l.max_output_tokens=r.max_output_tokens),r.tools!==void 0&&(l.tools=r.tools),l}function le(t,e,n){let o={request_id:e,consumer_id:n,stream:!!t.stream,kind:"responses",input:t.input};return t.model&&(o.model=t.model),t.tools!==void 0&&(o.tools=t.tools),t.tool_choice!==void 0&&(o.tool_choice=t.tool_choice),typeof t.temperature=="number"&&(o.temperature=t.temperature),typeof t.max_output_tokens=="number"&&(o.max_output_tokens=t.max_output_tokens),o}function ue(t){if(t instanceof Error)return{type:"error",error:{type:"urun_error",code:t.name||null,message:t.message}};if(t&&typeof t=="object"&&t.t==="error"){let e=t,n=e.body??{};return{type:"error",error:{type:"urun_error",code:e.code??null,message:n.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(t)}}}var Ke="llm-resp";async function*X(t,e){let n=`${Ke}:${e}`,o=`resp_${e}`;yield{type:"response.created",response:{id:o,status:"in_progress"}};let r=new Map;for await(let l of t.stream(n).messages()){let i=l;if(i.t==="delta"){if(typeof i.delta=="string"&&(yield{type:"response.output_text.delta",item_id:o,delta:i.delta}),typeof i.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:o,delta:i.reasoning}),Array.isArray(i.tool_calls))for(let u of i.tool_calls){let g=typeof u.index=="number"?u.index:0,a=r.get(g)??{};u.id&&(a.id=u.id),u.function?.name&&(a.name=u.function.name),r.set(g,a),yield{type:"response.function_call_arguments.delta",item_id:`fc_${e}_${g}`,tool_index:g,call_id:a.id,name:a.name,delta:u.function?.arguments??""}}}else if(i.t==="response"){yield{type:"response.completed",response:{id:o,status:"completed",...i.body}};return}else if(i.t==="error"){yield ue(i);return}}}var qe="llm",j=class{constructor(e){this.session=e;this.sessionTag=e.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(e){e.session_tag=this.sessionTag,this.session.doc(qe).set({requests:{[e.request_id]:{payload:e,consumer_id:e.consumer_id,stream:e.stream}}})}sendResponses(e,n){let o=le(e,n,this.consumerId);return this.write(o),X(this.session,n)}sendResponseCreate(e,n,o){let r=ae(e,n,o,this.consumerId);return this.write(r),X(this.session,o)}};var pe=0;function We(){return pe+=1,`req_${Date.now().toString(36)}_${pe.toString(36)}`}var K=class{transport;constructor(e){this.transport=new j(e)}responses={create:async e=>{let n=We(),o=this.transport.sendResponses(e,n);return Object.assign((async function*(){yield*o})(),{requestId:n})}}};var q=require("fs"),de=ie(require("os"),1),W=require("path"),fe=require("util");function J(t=de.default.homedir()){return(0,W.join)(t,".urun","logs","pi-extension.log")}var ce=!1;function ge(t,e){let n=J();try{(0,q.mkdirSync)((0,W.dirname)(n),{recursive:!0}),(0,q.appendFileSync)(n,`${new Date().toISOString()} [console.${t}] [urun] ${(0,fe.format)(...e)}
|
|
2
2
|
`)}catch(o){if(!ce){ce=!0;try{process.stderr.write(`[urun] pi-extension quiet console: cannot write ${n} (${String(o)}); further urun-side console output will be dropped
|
|
3
|
-
`)}catch{}}}}var f=t=>(...e)=>ge(t,e),z={log:f("log"),info:f("info"),warn:f("warn"),error:f("error"),debug:f("debug"),trace:f("trace"),dir:f("dir"),table:f("table"),group:f("group"),groupCollapsed:f("groupCollapsed"),groupEnd:f("groupEnd"),count:f("count"),countReset:f("countReset"),time:f("time"),timeLog:f("timeLog"),timeEnd:f("timeEnd"),assert:(t,...e)=>{t||ge("assert",e.length>0?e:["Assertion failed"])}};function Q(t){try{process.stderr.write(t)}catch{}}var V="https://api.urun.sh/v1";async function me(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/apps`,o=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"}});if(!o.ok)throw new Error(`org apps listing failed: GET ${n} \u2192 ${o.status}`);let r=await o.json();if(!Array.isArray(r.apps))throw new Error(`org apps listing returned no "apps" array (GET ${n})`);if(r.truncated===!0)throw new Error(`org apps listing was truncated (GET ${n} returned ${r.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return r.apps}async function
|
|
4
|
-
`)}}function Se(t){let e=be(J());return async(n,o,r)=>{let{baseUrl:l,orgId:i,auth:
|
|
3
|
+
`)}catch{}}}}var f=t=>(...e)=>ge(t,e),z={log:f("log"),info:f("info"),warn:f("warn"),error:f("error"),debug:f("debug"),trace:f("trace"),dir:f("dir"),table:f("table"),group:f("group"),groupCollapsed:f("groupCollapsed"),groupEnd:f("groupEnd"),count:f("count"),countReset:f("countReset"),time:f("time"),timeLog:f("timeLog"),timeEnd:f("timeEnd"),assert:(t,...e)=>{t||ge("assert",e.length>0?e:["Assertion failed"])}};function Q(t){try{process.stderr.write(t)}catch{}}var V="https://api.urun.sh/v1";async function me(t){let e=t.fetchImpl??fetch,n=`${t.apiUrl.replace(/\/+$/,"")}/apps`,o=await e(n,{headers:{Authorization:`Bearer ${t.apiKey}`,Accept:"application/json"}});if(!o.ok)throw new Error(`org apps listing failed: GET ${n} \u2192 ${o.status}`);let r=await o.json();if(!Array.isArray(r.apps))throw new Error(`org apps listing returned no "apps" array (GET ${n})`);if(r.truncated===!0)throw new Error(`org apps listing was truncated (GET ${n} returned ${r.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return r.apps}async function ye(t){let e=await me(t),n=e.filter(o=>o.function_name===t.fnName&&o.deployment_status==="active").map(o=>o.app_slug);if(n.length===0)throw new Error(`urun pi extension: the org has no active deployed app exposing "${t.fnName}" (GET ${t.apiUrl}/apps returned ${e.length} app(s), none servable) \u2014 deploy one with \`urun serve <model>\` first`);return n.sort()}var ee="urun-serve",Je="serve",He=131072,Ge=16384,he=3,Be=42e4,Ye=6e4,we=2,Xe=15e3;function ze(t){let e=(t.URUN_API_KEY??"").trim();if(!e)throw new Error("urun pi extension: URUN_API_KEY is required \u2014 model discovery lists the org's deployed serve apps via the org API (URUN_JWT alone cannot list apps)");return{apiKey:e,apiUrl:(t.URUN_API_URL??"").trim()||V,fnName:(t.URUN_FUNCTION??"").trim()||Je}}function xe(t){let e=(t.URUN_BASE_URL??"").trim(),n=(t.URUN_ORG_ID??"").trim();if(!e)throw new Error("urun pi extension: URUN_BASE_URL is required to open a session");if(/\/v1\/*$/.test(new URL(e).pathname))throw new Error(`urun pi extension: URUN_BASE_URL must be the session-gateway base (e.g. https://api.urun.sh), got ${e} \u2014 a trailing /v1 is the org API form (URUN_API_URL); session allocation 404s against it. Drop the /v1.`);if(!n)throw new Error("urun pi extension: URUN_ORG_ID is required to open a session");let o=(t.URUN_JWT??"").trim();if(o)return{baseUrl:e,orgId:n,auth:{lane:"jwt",jwt:o}};let r=(t.URUN_API_KEY??"").trim();if(!r)throw new Error("urun pi extension: URUN_JWT or URUN_API_KEY is required to open a session");let l=(t.URUN_GATEWAY_URL??"").trim()||void 0;return{baseUrl:e,orgId:n,auth:{lane:"api-key",apiKey:r,gatewayUrl:l}}}function be(t){let e=null;return n=>{e===null&&((0,C.mkdirSync)((0,ke.dirname)(t),{recursive:!0}),e=(0,C.openSync)(t,"a"));let o=n.detail?` ${JSON.stringify(n.detail)}`:"";(0,C.writeSync)(e,`${new Date().toISOString()} [${n.level}] [urun] ${n.message}${o}
|
|
4
|
+
`)}}function Se(t){let e=be(J());return async(n,o,r)=>{let{baseUrl:l,orgId:i,auth:u}=xe(n),{App:g,createClientToken:a}=await t(),y=(u.lane==="jwt"?g(o,{baseUrl:l,orgId:i,jwt:u.jwt,diagnosticSink:e}):g(o,{baseUrl:l,orgId:i,diagnosticSink:e,getAccessToken:async()=>(await a(u.apiKey,{baseUrl:u.gatewayUrl,expiresIn:300,allowedFunctions:[`${o}/${r}`]})).token}))[r];if(typeof y!="function")throw new Error(`urun pi extension: app "${o}" has no function "${r}"`);let w=y();if(typeof w.end!="function")throw new Error(`urun pi extension: app "${o}" function "${r}" returned a session without end()`);let I=w.connect;return typeof I=="function"&&await I.call(w),w}}var Qe=Se(async()=>{let{createRequire:t}=await import("module"),e=t(k);if(typeof globalThis.RTCPeerConnection>"u")try{e.resolve("werift")}catch{throw new Error('urun pi extension: token streams require a WebRTC data channel, and this Node runtime has no RTCPeerConnection and no "werift" backend installed (@urun-sh/core\'s optional Node WebRTC backend \u2014 it was probably stripped by a --no-optional install). Install werift (`npm i werift`) and retry.')}return e("@urun-sh/core")});function Ve(t,e){try{return Promise.resolve(t.end()).then(()=>{},n=>e(n))}catch(n){return e(n),Promise.resolve()}}function Ze(t){if(t?.hasUI===!1)return!0;if(t?.hasUI===!0)return!1;let e=process.argv.slice(2);if(e.includes("-p")||e.includes("--print"))return!0;let n=e.indexOf("--mode");return n!==-1&&(e[n+1]==="json"||e[n+1]==="rpc")?!0:!process.stdin.isTTY||!process.stdout.isTTY}function Ae(t,e){return`uRun: releasing the pooled "${t}" session failed \u2014 the backend session may stay allocated until the platform reclaims it: ${e instanceof Error?e.message:String(e)}`}var H=class{constructor(e,n,o,r=(l,i)=>z.warn(Ae(l,i))){this.open=e;this.env=n;this.fnName=o;this.onReleaseError=r}open;env;fnName;onReleaseError;pool=new Map;releasing=new Set;acquire(e){let n=this.pool.get(e);if(!n){let o=Promise.resolve(this.open(this.env,e,this.fnName)).then(r=>({session:r,responses:new K(r)}));n=o,this.pool.set(e,o),o.catch(()=>{this.pool.get(e)===o&&this.pool.delete(e)})}return n}evict(e){let n=this.pool.get(e);if(!n)return Promise.resolve();this.pool.delete(e);let o=n.then(r=>Ve(r.session,l=>this.onReleaseError(e,l)),()=>{});return this.releasing.add(o),o.then(()=>this.releasing.delete(o)),o}closeAll(){let e=[...this.pool.keys()].map(n=>this.evict(n));return Promise.all([...e,...this.releasing]).then(()=>{})}};function Re(t){let e=[];t.systemPrompt&&e.push({role:"system",content:t.systemPrompt});for(let n of t.messages){if(n.role==="toolResult"){e.push({type:"function_call_output",call_id:n.toolCallId,output:tt(n.content)});continue}if(n.role==="assistant"&&Array.isArray(n.content)){let r="",l=()=>{r&&(e.push({role:"assistant",content:r}),r="")};for(let i of n.content)if(i.type==="text")r+=String(i.text??"");else if(i.type==="toolCall")l(),e.push({type:"function_call",call_id:String(i.id??""),name:String(i.name??""),arguments:JSON.stringify(i.arguments??{})});else{if(i.type==="thinking")continue;throw new Error(`urun pi extension: unsupported assistant content part "${String(i.type)}"`)}l();continue}let o=n.role==="assistant"?"assistant":"user";e.push({role:o,content:et(n.content)})}return Ee(e)}function Ee(t){let e=[];for(let n of t){let o=e[e.length-1];o&&"role"in o&&"role"in n&&o.role===n.role?o.content=[o.content,n.content].filter(r=>r.length>0).join(`
|
|
5
5
|
|
|
6
|
-
`):e.push({...n})}return e}function
|
|
7
|
-
`);return}let n=t?.sendMessage;if(typeof n=="function")try{n.call(t,{customType:"urun-status",content:e,display:!0},{triggerTurn:!1})}catch{}}function
|
|
8
|
-
`):z.warn(
|
|
6
|
+
`):e.push({...n})}return e}function et(t){return typeof t=="string"?t:Array.isArray(t)?t.map(e=>e&&typeof e=="object"&&"text"in e?String(e.text):"").join(""):""}function tt(t){if(typeof t=="string")return t;if(!Array.isArray(t))return"";let e="";for(let n of t){let o=String(n.type??"");if(o!=="text")throw new Error(`urun pi extension: unsupported toolResult content part "${o}" \u2014 the serve lane carries text tool results only`);e+=String(n.text??"")}return e}function nt(t){if(!(!Array.isArray(t)||t.length===0))return t.map(e=>({type:"function",name:e.name,description:e.description,parameters:e.parameters}))}function _e(t,e){if(!e.trim())return{};try{return JSON.parse(e)}catch{throw new Error(`uRun serve error: tool call "${t}" arguments are not valid JSON: ${e}`)}}function ot(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function Te(t,e){let n=e?.usage;if(!n||typeof n!="object")return;let o=typeof n.input_tokens=="number"?n.input_tokens:void 0,r=typeof n.output_tokens=="number"?n.output_tokens:void 0;o===void 0||r===void 0||(t.input=o,t.output=r,t.totalTokens=typeof n.total_tokens=="number"?n.total_tokens:o+r)}function rt(t){let e=t?.output;if(!Array.isArray(e))return"";let n="";for(let o of e)if(!(o.type!=="reasoning"||!Array.isArray(o.content)))for(let r of o.content)r.type==="reasoning_text"&&(n+=String(r.text??""));return n}var st=["<tool_call>","<function="];function it(t,e){let n=st.find(o=>e.includes(o));if(n)return new Error(`serving row emitted a prose tool call (literal ${JSON.stringify(n)} 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 "${t}" (see catalog parser_defaults)`)}var G=class extends Error{};function at(t){let e=t instanceof Error?t.message:String(t),n=t?.code;return/\b529\b/.test(e)||/overloaded/i.test(e)||n===529||n==="529"}function lt(t){return new Promise(e=>setTimeout(e,t))}function ut(t,e,n,o){return new Promise((r,l)=>{let i=!1,u=s=>{i||(i=!0,clearTimeout(g),o?.removeEventListener("abort",a),s())},g=setTimeout(()=>u(()=>l(n())),e),a=()=>u(()=>l(new Error("aborted")));if(o?.aborted){a();return}o?.addEventListener("abort",a,{once:!0}),Promise.resolve(t).then(s=>u(()=>r(s)),s=>u(()=>l(s)))})}async function*pt(t,e){let n=t[Symbol.asyncIterator](),o,r=new Promise((l,i)=>{o=()=>i(new Error("aborted")),e.addEventListener("abort",o,{once:!0})});try{for(;;){if(e.aborted)throw new Error("aborted");let l=await Promise.race([n.next(),r]);if(l.done)return;yield l.value}}finally{o&&e.removeEventListener("abort",o),n.return?.(void 0)}}function Z(t,e){if(t?.hasUI===!1){Q(`${e}
|
|
7
|
+
`);return}let n=t?.sendMessage;if(typeof n=="function")try{n.call(t,{customType:"urun-status",content:e,display:!0},{triggerTurn:!1})}catch{}}function Ie(t,e={},n){let o=e.connectDeadlineMs??Be,r=e.stallTimeoutMs??Ye,l=e.phaseHeartbeatMs??Xe;return(i,u,g)=>{let a=(0,ve.createAssistantMessageEventStream)(),s={role:"assistant",content:[],api:i.api,provider:i.provider,model:i.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()},y=new AbortController,w=g?.signal,I=!1,N=()=>{I=!0,y.abort()};w&&(w.aborted?N():w.addEventListener("abort",N,{once:!0}));let L=!1,P,te=()=>{P&&clearTimeout(P),P=setTimeout(()=>{L=!0,y.abort()},r)},D=()=>{P&&clearTimeout(P),P=void 0},S=null,A=null,_=null,ne="",M=!1,B=()=>{if(!A)return;let d=A;A=null,a.push({type:"thinking_end",contentIndex:s.content.indexOf(d),content:d.thinking,partial:s})},oe=()=>{if(!S)return;let d=S;S=null,a.push({type:"text_end",contentIndex:s.content.indexOf(d),content:d.text,partial:s})},Y=()=>{if(!_)return;let{call:d,args:v,contentIndex:R}=_;_=null,d.arguments=_e(d.name,v),a.push({type:"toolcall_end",contentIndex:R,toolCall:d,partial:s})},Ue=d=>{let v={type:"toolCall",id:String(d.call_id??d.id??`call_${s.content.length}`),name:String(d.name??""),arguments:{}};s.content.push(v);let R=s.content.length-1;a.push({type:"toolcall_start",contentIndex:R,partial:s});let O=typeof d.arguments=="string"?d.arguments:"";O&&a.push({type:"toolcall_delta",contentIndex:R,delta:O,partial:s}),v.arguments=_e(v.name,O),a.push({type:"toolcall_end",contentIndex:R,toolCall:v,partial:s})};return(async()=>{a.push({type:"start",partial:s});let d=!1,v,R=()=>{v&&clearInterval(v),v=void 0},O=Date.now();v=setInterval(()=>{let x=Math.round((Date.now()-O)/1e3);Z(n,`uRun: still opening the "${i.id}" session\u2026 (${x}s \u2014 serve apps scale to zero, first turn can cold-start ~6min)`)},l);let $e=async x=>{let{responses:b}=await ut(t.acquire(i.id),o,()=>new Error(`uRun session for "${i.id}" did not connect within ${Math.round(o/1e3)}s \u2014 likely cold-starting (scaled to zero) or queued behind capacity. Retry shortly.`),y.signal);R();let U,E;for(let p=1;p<=he;p++)try{let h=await b.responses.create({model:i.id,input:x,stream:!0,tools:nt(u.tools),max_output_tokens:i.maxTokens});te();for await(let T of pt(h,y.signal)){te();let m=T;if(m.type==="response.output_text.delta"){let c=typeof m.delta=="string"?m.delta:"";c&&(M=!0,B(),Y(),S||(S={type:"text",text:""},s.content.push(S),a.push({type:"text_start",contentIndex:s.content.length-1,partial:s})),S.text+=c,ne+=c,a.push({type:"text_delta",contentIndex:s.content.indexOf(S),delta:c,partial:s}))}else if(m.type==="response.reasoning_text.delta"){let c=typeof m.delta=="string"?m.delta:"";c&&(M=!0,A||(A={type:"thinking",thinking:""},s.content.push(A),a.push({type:"thinking_start",contentIndex:s.content.length-1,partial:s})),A.thinking+=c,a.push({type:"thinking_delta",contentIndex:s.content.indexOf(A),delta:c,partial:s}))}else if(m.type==="response.function_call_arguments.delta"&&typeof m.tool_index=="number"){let c=m;if(M=!0,B(),oe(),!_||_.toolIndex!==c.tool_index){Y();let re={type:"toolCall",id:c.call_id??`call_${c.tool_index}`,name:c.name??"",arguments:{}};s.content.push(re),_={call:re,args:"",contentIndex:s.content.length-1,toolIndex:c.tool_index},a.push({type:"toolcall_start",contentIndex:_.contentIndex,partial:s})}c.call_id&&(_.call.id=c.call_id),c.name&&(_.call.name=c.name);let $=typeof c.delta=="string"?c.delta:"";$&&(_.args+=$,a.push({type:"toolcall_delta",contentIndex:_.contentIndex,delta:$,partial:s}))}else if(m.type==="response.completed"){U=m.response;break}else if(m.type==="error"){let c=m.error,$=new Error(`uRun serve error: ${c?.message??"unknown error"}`);throw $.code=c?.code??null,$}}if(!U)throw new G(`uRun backhaul for "${i.id}" closed before the turn completed \u2014 the serving session ended mid-turn (its pod was restarted, drained or deleted).`);E=void 0;break}catch(h){if(E=h,y.signal.aborted)throw h;if(p<he&&at(h)&&!M){await lt(p*500);continue}throw h}if(E)throw E;return U};try{let x=Re(u),b;for(let p=1;p<=we;p++)try{b=await $e(x);break}catch(h){let T=L||h instanceof G;if(p>=we||!T||M||I)throw h;D(),t.evict(i.id),L=!1,y=new AbortController,Z(n,`uRun: the "${i.id}" session ended mid-turn \u2014 re-homing to a fresh assignment and replaying the turn\u2026`)}if(D(),B(),oe(),Y(),!s.content.some(p=>p.type==="toolCall"))for(let p of ot(b))Ue(p);if(!s.content.some(p=>p.type==="thinking")){let p=rt(b);if(p){let h={type:"thinking",thinking:""};s.content.push(h);let T=s.content.length-1;a.push({type:"thinking_start",contentIndex:T,partial:s}),h.thinking=p,a.push({type:"thinking_delta",contentIndex:T,delta:p,partial:s}),a.push({type:"thinking_end",contentIndex:T,content:p,partial:s})}}let U=s.content.filter(p=>p.type==="toolCall").length;if((u.tools?.length??0)>0&&U===0){let p=it(i.id,ne);if(p)throw p}let E=U>0?"toolUse":b?.status==="incomplete"?"length":"stop";s.stopReason=E,Te(s.usage,b),a.push({type:"done",reason:E,message:s}),a.end(s)}catch(x){if(d=!0,D(),I||w?.aborted&&!L)s.stopReason="aborted",s.errorMessage="aborted by user",a.push({type:"error",reason:"aborted",error:s});else{let b=L?`uRun stream stalled \u2014 no output for ${Math.round(r/1e3)}s; the model may be hung. Retry, or Ctrl-C to abort.`:x instanceof Error?x.message:String(x);s.stopReason="error",s.errorMessage=b,a.push({type:"error",reason:"error",error:s})}a.end(s)}finally{D(),R(),w&&w.removeEventListener("abort",N),d&&t.evict(i.id)}})(),a}}function ct(t){return{id:t,name:`uRun ${t}`,api:ee,reasoning:!0,input:["text"],cost:{input:0,output:0,cacheRead:0,cacheWrite:0},contextWindow:He,maxTokens:Ge}}function Pe(t={}){return async e=>{let n=t.env??process.env,{apiKey:o,apiUrl:r,fnName:l}=ze(n),i=await ye({apiUrl:r,apiKey:o,fnName:l,fetchImpl:t.fetchImpl}),u=new H(t.openSession??Qe,n,l,(a,s)=>{let y=Ae(a,s);Ze(e)?Q(`${y}
|
|
8
|
+
`):z.warn(y)});e.on("session_shutdown",()=>u.closeAll());let g={name:"uRun",api:ee,baseUrl:"urun://session",apiKey:"$URUN_API_KEY",models:i.map(ct),streamSimple:Ie(u,t.timing??{},e)};e.registerProvider("urun",g)}}var dt=Pe();0&&(module.exports={SessionPool,URUN_API,applyServeUsage,coalesceSameRole,createFileDiagnosticSink,createUrunExtension,makeSessionFactory,makeStreamSimple,notify,piDiagnosticLogPath,resolveSessionEnv,toResponsesInput});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ExtensionAPI } from '@earendil-works/pi-coding-agent';
|
|
2
|
-
import { Api, Model, Context, SimpleStreamOptions, AssistantMessageEventStream } from '@earendil-works/pi-ai';
|
|
2
|
+
import { Api, AssistantMessage, Model, Context, SimpleStreamOptions, AssistantMessageEventStream } from '@earendil-works/pi-ai';
|
|
3
3
|
import { a as UrunSessionLike, U as UrunResponses } from '../ResponsesClient-BDl1OIRP.cjs';
|
|
4
4
|
|
|
5
5
|
/** Where the pi lane's quiet diagnostics land (the compat log convention). */
|
|
@@ -268,6 +268,16 @@ type ResponsesInputItem = {
|
|
|
268
268
|
declare function toResponsesInput(context: Context): ResponsesInputItem[];
|
|
269
269
|
/** Fold consecutive same-role message items into one, joining with a blank line. */
|
|
270
270
|
declare function coalesceSameRole(items: ResponsesInputItem[]): ResponsesInputItem[];
|
|
271
|
+
/**
|
|
272
|
+
* REAL usage of a completed Responses body → pi's usage fields. The serve
|
|
273
|
+
* runtime emits `usage: { input_tokens, output_tokens, total_tokens }` on
|
|
274
|
+
* every terminal body (urun-python #1606; same source the proxy lane's
|
|
275
|
+
* `usageOf` reads). Without this the extension reported all-zero usage, so
|
|
276
|
+
* pi's meter fell back to chunk-count estimation — the "35 tok/s" ghost while
|
|
277
|
+
* real delivery ran ~100. Old servers that sent no usage keep the zeros
|
|
278
|
+
* (explicitly degraded, never fabricated counts).
|
|
279
|
+
*/
|
|
280
|
+
declare function applyServeUsage(usage: AssistantMessage['usage'], completed: Record<string, unknown> | undefined): void;
|
|
271
281
|
/**
|
|
272
282
|
* Push a human aliveness line via pi's display-only side-channel
|
|
273
283
|
* (`pi.sendMessage`, no new turn). Feature-detected + swallowed so a pi build
|
|
@@ -302,4 +312,4 @@ declare function createUrunExtension(opts?: UrunExtensionOptions): (pi: Extensio
|
|
|
302
312
|
/** The pi extension factory (default export — what pi's loader invokes). */
|
|
303
313
|
declare const _default: (pi: ExtensionAPI) => Promise<void>;
|
|
304
314
|
|
|
305
|
-
export { type CoreDiagnosticLike, type CoreModuleLike, type OwnedSession, type ResponsesInputItem, type SessionFactory, SessionPool, type StreamTiming, URUN_API, type UrunExtensionOptions, coalesceSameRole, createFileDiagnosticSink, createUrunExtension, _default as default, makeSessionFactory, makeStreamSimple, notify, piDiagnosticLogPath, resolveSessionEnv, toResponsesInput };
|
|
315
|
+
export { type CoreDiagnosticLike, type CoreModuleLike, type OwnedSession, type ResponsesInputItem, type SessionFactory, SessionPool, type StreamTiming, URUN_API, type UrunExtensionOptions, applyServeUsage, coalesceSameRole, createFileDiagnosticSink, createUrunExtension, _default as default, makeSessionFactory, makeStreamSimple, notify, piDiagnosticLogPath, resolveSessionEnv, toResponsesInput };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ExtensionAPI } from '@earendil-works/pi-coding-agent';
|
|
2
|
-
import { Api, Model, Context, SimpleStreamOptions, AssistantMessageEventStream } from '@earendil-works/pi-ai';
|
|
2
|
+
import { Api, AssistantMessage, Model, Context, SimpleStreamOptions, AssistantMessageEventStream } from '@earendil-works/pi-ai';
|
|
3
3
|
import { a as UrunSessionLike, U as UrunResponses } from '../ResponsesClient-BDl1OIRP.js';
|
|
4
4
|
|
|
5
5
|
declare function piDiagnosticLogPath(home?: string): string;
|
|
@@ -97,6 +97,8 @@ declare function toResponsesInput(context: Context): ResponsesInputItem[];
|
|
|
97
97
|
|
|
98
98
|
declare function coalesceSameRole(items: ResponsesInputItem[]): ResponsesInputItem[];
|
|
99
99
|
|
|
100
|
+
declare function applyServeUsage(usage: AssistantMessage['usage'], completed: Record<string, unknown> | undefined): void;
|
|
101
|
+
|
|
100
102
|
declare function notify(pi: ExtensionAPI | undefined, text: string): void;
|
|
101
103
|
|
|
102
104
|
interface StreamTiming {
|
|
@@ -118,4 +120,4 @@ declare function createUrunExtension(opts?: UrunExtensionOptions): (pi: Extensio
|
|
|
118
120
|
|
|
119
121
|
declare const _default: (pi: ExtensionAPI) => Promise<void>;
|
|
120
122
|
|
|
121
|
-
export { type CoreDiagnosticLike, type CoreModuleLike, type OwnedSession, type ResponsesInputItem, type SessionFactory, SessionPool, type StreamTiming, URUN_API, type UrunExtensionOptions, coalesceSameRole, createFileDiagnosticSink, createUrunExtension, _default as default, makeSessionFactory, makeStreamSimple, notify, piDiagnosticLogPath, resolveSessionEnv, toResponsesInput };
|
|
123
|
+
export { type CoreDiagnosticLike, type CoreModuleLike, type OwnedSession, type ResponsesInputItem, type SessionFactory, SessionPool, type StreamTiming, URUN_API, type UrunExtensionOptions, applyServeUsage, coalesceSameRole, createFileDiagnosticSink, createUrunExtension, _default as default, makeSessionFactory, makeStreamSimple, notify, piDiagnosticLogPath, resolveSessionEnv, toResponsesInput };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import{c as X}from"../chunk-62IL5R2N.js";import{a as K,d as z}from"../chunk-VXTNG2TP.js";import{createAssistantMessageEventStream as ye}from"@earendil-works/pi-ai";import{mkdirSync as he,openSync as we,writeSync as
|
|
1
|
+
import{c as X}from"../chunk-62IL5R2N.js";import{a as K,d as z}from"../chunk-VXTNG2TP.js";import{createAssistantMessageEventStream as ye}from"@earendil-works/pi-ai";import{mkdirSync as he,openSync as we,writeSync as _e}from"fs";import{dirname as ve}from"path";import{appendFileSync as ce,mkdirSync as de}from"fs";import pe from"os";import{dirname as fe,join as ge}from"path";import{format as me}from"util";function W(t=pe.homedir()){return ge(t,".urun","logs","pi-extension.log")}var Q=!1;function ee(t,e){let n=W();try{de(fe(n),{recursive:!0}),ce(n,`${new Date().toISOString()} [console.${t}] [urun] ${me(...e)}
|
|
2
2
|
`)}catch(o){if(!Q){Q=!0;try{process.stderr.write(`[urun] pi-extension quiet console: cannot write ${n} (${String(o)}); further urun-side console output will be dropped
|
|
3
|
-
`)}catch{}}}}var f=t=>(...e)=>ee(t,e),J={log:f("log"),info:f("info"),warn:f("warn"),error:f("error"),debug:f("debug"),trace:f("trace"),dir:f("dir"),table:f("table"),group:f("group"),groupCollapsed:f("groupCollapsed"),groupEnd:f("groupEnd"),count:f("count"),countReset:f("countReset"),time:f("time"),timeLog:f("timeLog"),timeEnd:f("timeEnd"),assert:(t,...e)=>{t||ee("assert",e.length>0?e:["Assertion failed"])}};function
|
|
4
|
-
`)}}function Ie(t){let e=Ue(W());return async(n,o,
|
|
3
|
+
`)}catch{}}}}var f=t=>(...e)=>ee(t,e),J={log:f("log"),info:f("info"),warn:f("warn"),error:f("error"),debug:f("debug"),trace:f("trace"),dir:f("dir"),table:f("table"),group:f("group"),groupCollapsed:f("groupCollapsed"),groupEnd:f("groupEnd"),count:f("count"),countReset:f("countReset"),time:f("time"),timeLog:f("timeLog"),timeEnd:f("timeEnd"),assert:(t,...e)=>{t||ee("assert",e.length>0?e:["Assertion failed"])}};function j(t){try{process.stderr.write(t)}catch{}}async function te(t){let e=await z(t),n=e.filter(o=>o.function_name===t.fnName&&o.deployment_status==="active").map(o=>o.app_slug);if(n.length===0)throw new Error(`urun pi extension: the org has no active deployed app exposing "${t.fnName}" (GET ${t.apiUrl}/apps returned ${e.length} app(s), none servable) \u2014 deploy one with \`urun serve <model>\` first`);return n.sort()}var ie="urun-serve",ke="serve",xe=131072,Re=16384,ne=3,be=42e4,Te=6e4,oe=2,Ee=15e3;function Se(t){let e=(t.URUN_API_KEY??"").trim();if(!e)throw new Error("urun pi extension: URUN_API_KEY is required \u2014 model discovery lists the org's deployed serve apps via the org API (URUN_JWT alone cannot list apps)");return{apiKey:e,apiUrl:(t.URUN_API_URL??"").trim()||K,fnName:(t.URUN_FUNCTION??"").trim()||ke}}function Ae(t){let e=(t.URUN_BASE_URL??"").trim(),n=(t.URUN_ORG_ID??"").trim();if(!e)throw new Error("urun pi extension: URUN_BASE_URL is required to open a session");if(/\/v1\/*$/.test(new URL(e).pathname))throw new Error(`urun pi extension: URUN_BASE_URL must be the session-gateway base (e.g. https://api.urun.sh), got ${e} \u2014 a trailing /v1 is the org API form (URUN_API_URL); session allocation 404s against it. Drop the /v1.`);if(!n)throw new Error("urun pi extension: URUN_ORG_ID is required to open a session");let o=(t.URUN_JWT??"").trim();if(o)return{baseUrl:e,orgId:n,auth:{lane:"jwt",jwt:o}};let s=(t.URUN_API_KEY??"").trim();if(!s)throw new Error("urun pi extension: URUN_JWT or URUN_API_KEY is required to open a session");let l=(t.URUN_GATEWAY_URL??"").trim()||void 0;return{baseUrl:e,orgId:n,auth:{lane:"api-key",apiKey:s,gatewayUrl:l}}}function Ue(t){let e=null;return n=>{e===null&&(he(ve(t),{recursive:!0}),e=we(t,"a"));let o=n.detail?` ${JSON.stringify(n.detail)}`:"";_e(e,`${new Date().toISOString()} [${n.level}] [urun] ${n.message}${o}
|
|
4
|
+
`)}}function Ie(t){let e=Ue(W());return async(n,o,s)=>{let{baseUrl:l,orgId:i,auth:p}=Ae(n),{App:x,createClientToken:a}=await t(),m=(p.lane==="jwt"?x(o,{baseUrl:l,orgId:i,jwt:p.jwt,diagnosticSink:e}):x(o,{baseUrl:l,orgId:i,diagnosticSink:e,getAccessToken:async()=>(await a(p.apiKey,{baseUrl:p.gatewayUrl,expiresIn:300,allowedFunctions:[`${o}/${s}`]})).token}))[s];if(typeof m!="function")throw new Error(`urun pi extension: app "${o}" has no function "${s}"`);let h=m();if(typeof h.end!="function")throw new Error(`urun pi extension: app "${o}" function "${s}" returned a session without end()`);let A=h.connect;return typeof A=="function"&&await A.call(h),h}}var Pe=Ie(async()=>{let{createRequire:t}=await import("module"),e=t(import.meta.url);if(typeof globalThis.RTCPeerConnection>"u")try{e.resolve("werift")}catch{throw new Error('urun pi extension: token streams require a WebRTC data channel, and this Node runtime has no RTCPeerConnection and no "werift" backend installed (@urun-sh/core\'s optional Node WebRTC backend \u2014 it was probably stripped by a --no-optional install). Install werift (`npm i werift`) and retry.')}return e("@urun-sh/core")});function Ce(t,e){try{return Promise.resolve(t.end()).then(()=>{},n=>e(n))}catch(n){return e(n),Promise.resolve()}}function Ne(t){if(t?.hasUI===!1)return!0;if(t?.hasUI===!0)return!1;let e=process.argv.slice(2);if(e.includes("-p")||e.includes("--print"))return!0;let n=e.indexOf("--mode");return n!==-1&&(e[n+1]==="json"||e[n+1]==="rpc")?!0:!process.stdin.isTTY||!process.stdout.isTTY}function ae(t,e){return`uRun: releasing the pooled "${t}" session failed \u2014 the backend session may stay allocated until the platform reclaims it: ${e instanceof Error?e.message:String(e)}`}var q=class{constructor(e,n,o,s=(l,i)=>J.warn(ae(l,i))){this.open=e;this.env=n;this.fnName=o;this.onReleaseError=s}open;env;fnName;onReleaseError;pool=new Map;releasing=new Set;acquire(e){let n=this.pool.get(e);if(!n){let o=Promise.resolve(this.open(this.env,e,this.fnName)).then(s=>({session:s,responses:new X(s)}));n=o,this.pool.set(e,o),o.catch(()=>{this.pool.get(e)===o&&this.pool.delete(e)})}return n}evict(e){let n=this.pool.get(e);if(!n)return Promise.resolve();this.pool.delete(e);let o=n.then(s=>Ce(s.session,l=>this.onReleaseError(e,l)),()=>{});return this.releasing.add(o),o.then(()=>this.releasing.delete(o)),o}closeAll(){let e=[...this.pool.keys()].map(n=>this.evict(n));return Promise.all([...e,...this.releasing]).then(()=>{})}};function $e(t){let e=[];t.systemPrompt&&e.push({role:"system",content:t.systemPrompt});for(let n of t.messages){if(n.role==="toolResult"){e.push({type:"function_call_output",call_id:n.toolCallId,output:Me(n.content)});continue}if(n.role==="assistant"&&Array.isArray(n.content)){let s="",l=()=>{s&&(e.push({role:"assistant",content:s}),s="")};for(let i of n.content)if(i.type==="text")s+=String(i.text??"");else if(i.type==="toolCall")l(),e.push({type:"function_call",call_id:String(i.id??""),name:String(i.name??""),arguments:JSON.stringify(i.arguments??{})});else{if(i.type==="thinking")continue;throw new Error(`urun pi extension: unsupported assistant content part "${String(i.type)}"`)}l();continue}let o=n.role==="assistant"?"assistant":"user";e.push({role:o,content:Oe(n.content)})}return Le(e)}function Le(t){let e=[];for(let n of t){let o=e[e.length-1];o&&"role"in o&&"role"in n&&o.role===n.role?o.content=[o.content,n.content].filter(s=>s.length>0).join(`
|
|
5
5
|
|
|
6
|
-
`):e.push({...n})}return e}function Oe(t){return typeof t=="string"?t:Array.isArray(t)?t.map(e=>e&&typeof e=="object"&&"text"in e?String(e.text):"").join(""):""}function Me(t){if(typeof t=="string")return t;if(!Array.isArray(t))return"";let e="";for(let n of t){let o=String(n.type??"");if(o!=="text")throw new Error(`urun pi extension: unsupported toolResult content part "${o}" \u2014 the serve lane carries text tool results only`);e+=String(n.text??"")}return e}function De(t){if(!(!Array.isArray(t)||t.length===0))return t.map(e=>({type:"function",name:e.name,description:e.description,parameters:e.parameters}))}function re(t,e){if(!e.trim())return{};try{return JSON.parse(e)}catch{throw new Error(`uRun serve error: tool call "${t}" arguments are not valid JSON: ${e}`)}}function Fe(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function Ke(t){let e=t?.output;if(!Array.isArray(e))return"";let n="";for(let o of e)if(!(o.type!=="reasoning"||!Array.isArray(o.content)))for(let
|
|
7
|
-
`);return}let n=t?.sendMessage;if(typeof n=="function")try{n.call(t,{customType:"urun-status",content:e,display:!0},{triggerTurn:!1})}catch{}}function
|
|
8
|
-
`):J.warn(m)});e.on("session_shutdown",()=>p.closeAll());let
|
|
6
|
+
`):e.push({...n})}return e}function Oe(t){return typeof t=="string"?t:Array.isArray(t)?t.map(e=>e&&typeof e=="object"&&"text"in e?String(e.text):"").join(""):""}function Me(t){if(typeof t=="string")return t;if(!Array.isArray(t))return"";let e="";for(let n of t){let o=String(n.type??"");if(o!=="text")throw new Error(`urun pi extension: unsupported toolResult content part "${o}" \u2014 the serve lane carries text tool results only`);e+=String(n.text??"")}return e}function De(t){if(!(!Array.isArray(t)||t.length===0))return t.map(e=>({type:"function",name:e.name,description:e.description,parameters:e.parameters}))}function re(t,e){if(!e.trim())return{};try{return JSON.parse(e)}catch{throw new Error(`uRun serve error: tool call "${t}" arguments are not valid JSON: ${e}`)}}function Fe(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function Ke(t,e){let n=e?.usage;if(!n||typeof n!="object")return;let o=typeof n.input_tokens=="number"?n.input_tokens:void 0,s=typeof n.output_tokens=="number"?n.output_tokens:void 0;o===void 0||s===void 0||(t.input=o,t.output=s,t.totalTokens=typeof n.total_tokens=="number"?n.total_tokens:o+s)}function We(t){let e=t?.output;if(!Array.isArray(e))return"";let n="";for(let o of e)if(!(o.type!=="reasoning"||!Array.isArray(o.content)))for(let s of o.content)s.type==="reasoning_text"&&(n+=String(s.text??""));return n}var Je=["<tool_call>","<function="];function je(t,e){let n=Je.find(o=>e.includes(o));if(n)return new Error(`serving row emitted a prose tool call (literal ${JSON.stringify(n)} 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 "${t}" (see catalog parser_defaults)`)}var M=class extends Error{};function qe(t){let e=t instanceof Error?t.message:String(t),n=t?.code;return/\b529\b/.test(e)||/overloaded/i.test(e)||n===529||n==="529"}function He(t){return new Promise(e=>setTimeout(e,t))}function Ye(t,e,n,o){return new Promise((s,l)=>{let i=!1,p=r=>{i||(i=!0,clearTimeout(x),o?.removeEventListener("abort",a),r())},x=setTimeout(()=>p(()=>l(n())),e),a=()=>p(()=>l(new Error("aborted")));if(o?.aborted){a();return}o?.addEventListener("abort",a,{once:!0}),Promise.resolve(t).then(r=>p(()=>s(r)),r=>p(()=>l(r)))})}async function*Ge(t,e){let n=t[Symbol.asyncIterator](),o,s=new Promise((l,i)=>{o=()=>i(new Error("aborted")),e.addEventListener("abort",o,{once:!0})});try{for(;;){if(e.aborted)throw new Error("aborted");let l=await Promise.race([n.next(),s]);if(l.done)return;yield l.value}}finally{o&&e.removeEventListener("abort",o),n.return?.(void 0)}}function se(t,e){if(t?.hasUI===!1){j(`${e}
|
|
7
|
+
`);return}let n=t?.sendMessage;if(typeof n=="function")try{n.call(t,{customType:"urun-status",content:e,display:!0},{triggerTurn:!1})}catch{}}function Be(t,e={},n){let o=e.connectDeadlineMs??be,s=e.stallTimeoutMs??Te,l=e.phaseHeartbeatMs??Ee;return(i,p,x)=>{let a=ye(),r={role:"assistant",content:[],api:i.api,provider:i.provider,model:i.id,usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{input:0,output:0,cacheRead:0,cacheWrite:0,total:0}},stopReason:"stop",timestamp:Date.now()},m=new AbortController,h=x?.signal,A=!1,L=()=>{A=!0,m.abort()};h&&(h.aborted?L():h.addEventListener("abort",L,{once:!0}));let C=!1,U,H=()=>{U&&clearTimeout(U),U=setTimeout(()=>{C=!0,m.abort()},s)},O=()=>{U&&clearTimeout(U),U=void 0},R=null,b=null,w=null,Y="",N=!1,D=()=>{if(!b)return;let d=b;b=null,a.push({type:"thinking_end",contentIndex:r.content.indexOf(d),content:d.thinking,partial:r})},G=()=>{if(!R)return;let d=R;R=null,a.push({type:"text_end",contentIndex:r.content.indexOf(d),content:d.text,partial:r})},F=()=>{if(!w)return;let{call:d,args:_,contentIndex:T}=w;w=null,d.arguments=re(d.name,_),a.push({type:"toolcall_end",contentIndex:T,toolCall:d,partial:r})},le=d=>{let _={type:"toolCall",id:String(d.call_id??d.id??`call_${r.content.length}`),name:String(d.name??""),arguments:{}};r.content.push(_);let T=r.content.length-1;a.push({type:"toolcall_start",contentIndex:T,partial:r});let $=typeof d.arguments=="string"?d.arguments:"";$&&a.push({type:"toolcall_delta",contentIndex:T,delta:$,partial:r}),_.arguments=re(_.name,$),a.push({type:"toolcall_end",contentIndex:T,toolCall:_,partial:r})};return(async()=>{a.push({type:"start",partial:r});let d=!1,_,T=()=>{_&&clearInterval(_),_=void 0},$=Date.now();_=setInterval(()=>{let v=Math.round((Date.now()-$)/1e3);se(n,`uRun: still opening the "${i.id}" session\u2026 (${v}s \u2014 serve apps scale to zero, first turn can cold-start ~6min)`)},l);let ue=async v=>{let{responses:k}=await Ye(t.acquire(i.id),o,()=>new Error(`uRun session for "${i.id}" did not connect within ${Math.round(o/1e3)}s \u2014 likely cold-starting (scaled to zero) or queued behind capacity. Retry shortly.`),m.signal);T();let I,E;for(let u=1;u<=ne;u++)try{let y=await k.responses.create({model:i.id,input:v,stream:!0,tools:De(p.tools),max_output_tokens:i.maxTokens});H();for await(let S of Ge(y,m.signal)){H();let g=S;if(g.type==="response.output_text.delta"){let c=typeof g.delta=="string"?g.delta:"";c&&(N=!0,D(),F(),R||(R={type:"text",text:""},r.content.push(R),a.push({type:"text_start",contentIndex:r.content.length-1,partial:r})),R.text+=c,Y+=c,a.push({type:"text_delta",contentIndex:r.content.indexOf(R),delta:c,partial:r}))}else if(g.type==="response.reasoning_text.delta"){let c=typeof g.delta=="string"?g.delta:"";c&&(N=!0,b||(b={type:"thinking",thinking:""},r.content.push(b),a.push({type:"thinking_start",contentIndex:r.content.length-1,partial:r})),b.thinking+=c,a.push({type:"thinking_delta",contentIndex:r.content.indexOf(b),delta:c,partial:r}))}else if(g.type==="response.function_call_arguments.delta"&&typeof g.tool_index=="number"){let c=g;if(N=!0,D(),G(),!w||w.toolIndex!==c.tool_index){F();let B={type:"toolCall",id:c.call_id??`call_${c.tool_index}`,name:c.name??"",arguments:{}};r.content.push(B),w={call:B,args:"",contentIndex:r.content.length-1,toolIndex:c.tool_index},a.push({type:"toolcall_start",contentIndex:w.contentIndex,partial:r})}c.call_id&&(w.call.id=c.call_id),c.name&&(w.call.name=c.name);let P=typeof c.delta=="string"?c.delta:"";P&&(w.args+=P,a.push({type:"toolcall_delta",contentIndex:w.contentIndex,delta:P,partial:r}))}else if(g.type==="response.completed"){I=g.response;break}else if(g.type==="error"){let c=g.error,P=new Error(`uRun serve error: ${c?.message??"unknown error"}`);throw P.code=c?.code??null,P}}if(!I)throw new M(`uRun backhaul for "${i.id}" closed before the turn completed \u2014 the serving session ended mid-turn (its pod was restarted, drained or deleted).`);E=void 0;break}catch(y){if(E=y,m.signal.aborted)throw y;if(u<ne&&qe(y)&&!N){await He(u*500);continue}throw y}if(E)throw E;return I};try{let v=$e(p),k;for(let u=1;u<=oe;u++)try{k=await ue(v);break}catch(y){let S=C||y instanceof M;if(u>=oe||!S||N||A)throw y;O(),t.evict(i.id),C=!1,m=new AbortController,se(n,`uRun: the "${i.id}" session ended mid-turn \u2014 re-homing to a fresh assignment and replaying the turn\u2026`)}if(O(),D(),G(),F(),!r.content.some(u=>u.type==="toolCall"))for(let u of Fe(k))le(u);if(!r.content.some(u=>u.type==="thinking")){let u=We(k);if(u){let y={type:"thinking",thinking:""};r.content.push(y);let S=r.content.length-1;a.push({type:"thinking_start",contentIndex:S,partial:r}),y.thinking=u,a.push({type:"thinking_delta",contentIndex:S,delta:u,partial:r}),a.push({type:"thinking_end",contentIndex:S,content:u,partial:r})}}let I=r.content.filter(u=>u.type==="toolCall").length;if((p.tools?.length??0)>0&&I===0){let u=je(i.id,Y);if(u)throw u}let E=I>0?"toolUse":k?.status==="incomplete"?"length":"stop";r.stopReason=E,Ke(r.usage,k),a.push({type:"done",reason:E,message:r}),a.end(r)}catch(v){if(d=!0,O(),A||h?.aborted&&!C)r.stopReason="aborted",r.errorMessage="aborted by user",a.push({type:"error",reason:"aborted",error:r});else{let k=C?`uRun stream stalled \u2014 no output for ${Math.round(s/1e3)}s; the model may be hung. Retry, or Ctrl-C to abort.`:v instanceof Error?v.message:String(v);r.stopReason="error",r.errorMessage=k,a.push({type:"error",reason:"error",error:r})}a.end(r)}finally{O(),T(),h&&h.removeEventListener("abort",L),d&&t.evict(i.id)}})(),a}}function Xe(t){return{id:t,name:`uRun ${t}`,api:ie,reasoning:!0,input:["text"],cost:{input:0,output:0,cacheRead:0,cacheWrite:0},contextWindow:xe,maxTokens:Re}}function ze(t={}){return async e=>{let n=t.env??process.env,{apiKey:o,apiUrl:s,fnName:l}=Se(n),i=await te({apiUrl:s,apiKey:o,fnName:l,fetchImpl:t.fetchImpl}),p=new q(t.openSession??Pe,n,l,(a,r)=>{let m=ae(a,r);Ne(e)?j(`${m}
|
|
8
|
+
`):J.warn(m)});e.on("session_shutdown",()=>p.closeAll());let x={name:"uRun",api:ie,baseUrl:"urun://session",apiKey:"$URUN_API_KEY",models:i.map(Xe),streamSimple:Be(p,t.timing??{},e)};e.registerProvider("urun",x)}}var ft=ze();export{q as SessionPool,ie as URUN_API,Ke as applyServeUsage,Le as coalesceSameRole,Ue as createFileDiagnosticSink,ze as createUrunExtension,ft as default,Ie as makeSessionFactory,Be as makeStreamSimple,se as notify,W as piDiagnosticLogPath,Ae as resolveSessionEnv,$e as toResponsesInput};
|