@urun-sh/openai 0.2.60 → 0.3.0

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.
@@ -0,0 +1,225 @@
1
+ import { Server } from 'node:http';
2
+ import { createClientToken } from '@urun-sh/core';
3
+ import { a as UrunSessionLike, U as UrunResponses } from '../ResponsesClient-y8g6OfNN.js';
4
+ import { P as ProxyClients } from '../server-BBKNv7hV.js';
5
+ import '../media-DCHTX3Ez.js';
6
+
7
+ interface DeployedApp {
8
+ app_slug: string;
9
+ function_name?: string | null;
10
+ deployment_status?: string | null;
11
+ [k: string]: unknown;
12
+ }
13
+
14
+ declare class SessionGoneError extends Error {
15
+ }
16
+
17
+ interface RouterModelList {
18
+ object: 'list';
19
+ data: Array<{
20
+ id: string;
21
+ object: 'model';
22
+ created: number;
23
+ owned_by: string;
24
+ }>;
25
+ }
26
+ interface ModelRouterOptions<S> {
27
+
28
+ defaultApp: string | null;
29
+
30
+ fnName: string;
31
+
32
+ openSession: (appSlug: string) => S | Promise<S>;
33
+
34
+ closeSession: (entry: S) => Promise<void>;
35
+
36
+ listApps: (() => Promise<DeployedApp[]>) | null;
37
+
38
+ listCatalog: (() => Promise<Array<{
39
+ model_id: string;
40
+ variant: string;
41
+ }>>) | null;
42
+
43
+ appsTtlMs?: number;
44
+
45
+ sessionKey?: (entry: S) => string;
46
+ }
47
+
48
+ declare class ModelRouter<S> {
49
+ private readonly opts;
50
+ private readonly pool;
51
+ private appsCache;
52
+ constructor(opts: ModelRouterOptions<S>);
53
+
54
+ seed(appSlug: string, entry: S): void;
55
+ private deployedApps;
56
+
57
+ private servable;
58
+ private availableIds;
59
+
60
+ private noDefaultApp;
61
+
62
+ resolveApp(model: string | undefined): Promise<string>;
63
+
64
+ sessionFor(model: string | undefined): Promise<{
65
+ app: string;
66
+ entry: S;
67
+ }>;
68
+ private keyOf;
69
+
70
+ handleFor(model: string | undefined): Promise<{
71
+ app: string;
72
+ handle: string;
73
+ }>;
74
+
75
+ sessionForHandle(handle: string): Promise<{
76
+ app: string;
77
+ entry: S;
78
+ }>;
79
+
80
+ evict(app: string, entry: S): Promise<void>;
81
+
82
+ modelList(): Promise<RouterModelList>;
83
+
84
+ closeAll(): Promise<void>;
85
+ }
86
+
87
+ declare class ProxyAuthError extends Error {
88
+ readonly status: number;
89
+ constructor(message: string, status?: number);
90
+ }
91
+
92
+ declare class ControlPlaneUnavailableError extends Error {
93
+ constructor(message: string);
94
+ }
95
+
96
+ interface CallerIdentity {
97
+ apiKey: string;
98
+ orgId: string;
99
+ }
100
+
101
+ declare function bearerFrom(headerValue: string | undefined): string;
102
+
103
+ declare const ORG_BINDING_TTL_MS = 60000;
104
+
105
+ declare const MAX_CACHED_KEYS = 4096;
106
+ interface OrgResolverOptions {
107
+
108
+ gatewayUrl: string;
109
+
110
+ mint?: typeof createClientToken;
111
+
112
+ now?: () => number;
113
+ }
114
+
115
+ declare class OrgResolver {
116
+ private readonly opts;
117
+ private readonly cache;
118
+ private readonly mint;
119
+ private readonly now;
120
+ constructor(opts: OrgResolverOptions);
121
+
122
+ resolve(apiKey: string): Promise<CallerIdentity>;
123
+
124
+ clear(): void;
125
+ }
126
+
127
+ interface SessionGoneWatch {
128
+
129
+ gone(): SessionGoneError | null;
130
+
131
+ onGone(cb: (err: SessionGoneError) => void): () => void;
132
+
133
+ dispose(): void;
134
+ }
135
+
136
+ interface CatalogConfig {
137
+ catalogUrl: string;
138
+ anonKey: string;
139
+ }
140
+
141
+ type OwnedSession = UrunSessionLike & {
142
+ end: () => Promise<unknown>;
143
+ id?: string;
144
+ endsAt?: Date | null;
145
+ onPhase?: (handler: (phase: {
146
+ name: string;
147
+ }) => void) => () => void;
148
+ };
149
+
150
+ type PoolEntry = {
151
+ session: OwnedSession;
152
+ responses: UrunResponses;
153
+ gone: SessionGoneWatch;
154
+ };
155
+
156
+ declare const MAX_TENANTS = 512;
157
+ interface TenantRegistryOptions {
158
+
159
+ baseUrl: string;
160
+
161
+ apiUrl: string;
162
+
163
+ catalog: CatalogConfig | null;
164
+
165
+ build?: (caller: CallerIdentity) => {
166
+ router: ModelRouter<PoolEntry>;
167
+ clients: ProxyClients;
168
+ };
169
+ }
170
+
171
+ declare function tenantSubject(apiKey: string): string;
172
+ declare class TenantRegistry {
173
+ private readonly opts;
174
+
175
+ private readonly tenants;
176
+ constructor(opts: TenantRegistryOptions);
177
+ private build;
178
+
179
+ clientsFor(caller: CallerIdentity): ProxyClients;
180
+
181
+ get size(): number;
182
+
183
+ closeAll(): Promise<void>;
184
+ }
185
+
186
+ declare const DEFAULT_PORT = 8080;
187
+
188
+ declare const SERVE_FUNCTION = "serve";
189
+
190
+ declare const BIND_HOST = "0.0.0.0";
191
+
192
+ interface HostedConfig {
193
+
194
+ port: number;
195
+
196
+ baseUrl: string;
197
+
198
+ apiUrl: string;
199
+
200
+ catalog: CatalogConfig | null;
201
+ }
202
+
203
+ declare function resolveHostedConfig(env?: NodeJS.ProcessEnv): HostedConfig;
204
+
205
+ declare const READINESS_TTL_MS = 5000;
206
+
207
+ declare const READINESS_TIMEOUT_MS = 3000;
208
+ interface HostedProxyOptions extends HostedConfig {
209
+
210
+ probeControlPlane?: () => Promise<{
211
+ ready: boolean;
212
+ detail: string;
213
+ }>;
214
+
215
+ resolver?: OrgResolver;
216
+
217
+ registry?: TenantRegistry;
218
+ }
219
+
220
+ declare function createHostedProxy(options: HostedProxyOptions): {
221
+ server: Server;
222
+ closeAll: () => Promise<void>;
223
+ };
224
+
225
+ export { BIND_HOST, type CallerIdentity, ControlPlaneUnavailableError, DEFAULT_PORT, type HostedConfig, type HostedProxyOptions, MAX_CACHED_KEYS, MAX_TENANTS, ORG_BINDING_TTL_MS, OrgResolver, type OrgResolverOptions, ProxyAuthError, READINESS_TIMEOUT_MS, READINESS_TTL_MS, SERVE_FUNCTION, TenantRegistry, type TenantRegistryOptions, bearerFrom, createHostedProxy, resolveHostedConfig, tenantSubject };
@@ -0,0 +1 @@
1
+ import{a as e,b as o,c as t,d as n,e as p,f as s,g as T,h as E,i as _,j as a,k as S,l as f,m as x,n as i,o as y,p as O}from"../chunk-3ED25N7E.js";import"../chunk-XHRPJIEK.js";import"../chunk-F2TEK34X.js";import"../chunk-VG3X2LVG.js";import"../chunk-35LB5OHI.js";import"../chunk-EFBD3CGZ.js";import"../chunk-XHIIEA6Z.js";import{c as r}from"../chunk-YSFSRI3D.js";r();export{_ as BIND_HOST,o as ControlPlaneUnavailableError,T as DEFAULT_PORT,p as MAX_CACHED_KEYS,S as MAX_TENANTS,n as ORG_BINDING_TTL_MS,s as OrgResolver,e as ProxyAuthError,y as READINESS_TIMEOUT_MS,i as READINESS_TTL_MS,E as SERVE_FUNCTION,x as TenantRegistry,t as bearerFrom,O as createHostedProxy,a as resolveHostedConfig,f as tenantSubject};
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{b as S}from"./chunk-OLE2YJO3.js";import{a as p,c as v,d as E}from"./chunk-VFHQZ4OM.js";import{a as s,b as a,c as f}from"./chunk-SSZL77P5.js";var u=0;function _(){return u+=1,`rt_${Date.now().toString(36)}_${u.toString(36)}`}var i=class{constructor(e,t={}){this.session=e;this.opts=t;this.transport=new a(e)}session;opts;transport;handlers=new Set;conversation=[];active=null;audio=null;currentRequestId=null;async enableAudio(){if(!this.opts.audioBackend)throw new Error("audioBackend required for audio");this.audio=await p(this.session,this.opts.audioBackend),this.audio.onOutputAudio(e=>this.emit({type:"response.audio.delta",delta:e}))}on(e,t){return this.handlers.add(t),()=>this.handlers.delete(t)}emit(e){for(let t of this.handlers)t(e)}send(e){switch(e.type){case"conversation.item.create":{let t=(e.item?.content??[]).map(n=>n.text??"").join("");this.conversation.push({role:e.item?.role??"user",content:t});return}case"response.create":{let t=_();this.currentRequestId=t;let n=this.transport.sendResponseCreate({type:"response.create",response:e.response},this.conversation,t);this.active=(async()=>{try{for await(let o of n)this.emit(o)}catch(o){this.emit(s(o))}})();return}case"input_audio_buffer.append":this.audio?.appendInputAudio(e.audio);return;case"input_audio_buffer.commit":case"input_audio_buffer.clear":return;case"response.cancel":return;default:return}}async drain(){this.active&&await this.active}};var l=["session.update","conversation.item.create","response.create","response.cancel","input_audio_buffer.append","input_audio_buffer.commit","input_audio_buffer.clear"],m=["response.created","response.output_text.delta","response.output_item.added","response.function_call_arguments.delta","response.image_generation_call.partial_image","response.completed","error"];function y(r){return l.includes(r)}function h(r){return m.includes(r)}export{l as SUPPORTED_REALTIME_CLIENT_EVENTS,m as SUPPORTED_RESPONSES_EVENTS,i as UrunRealtime,f as UrunResponses,y as isSupportedClientEvent,h as isSupportedResponsesEvent,S as listModels,E as nodeAudioBackend,s as toErrorEvent,v as weriftAudioBackend};
1
+ import{b as _}from"./chunk-F2TEK34X.js";import{a as u,c as E,d as f}from"./chunk-35LB5OHI.js";import{a as i,b as p,c as S}from"./chunk-EFBD3CGZ.js";import{c as r}from"./chunk-YSFSRI3D.js";r();r();var d=0;function y(){return d+=1,`rt_${Date.now().toString(36)}_${d.toString(36)}`}var a=class{constructor(e,t={}){this.session=e;this.opts=t;this.transport=new p(e)}session;opts;transport;handlers=new Set;conversation=[];active=null;audio=null;currentRequestId=null;async enableAudio(){if(!this.opts.audioBackend)throw new Error("audioBackend required for audio");this.audio=await u(this.session,this.opts.audioBackend),this.audio.onOutputAudio(e=>this.emit({type:"response.audio.delta",delta:e}))}on(e,t){return this.handlers.add(t),()=>this.handlers.delete(t)}emit(e){for(let t of this.handlers)t(e)}send(e){switch(e.type){case"conversation.item.create":{let t=(e.item?.content??[]).map(o=>o.text??"").join("");this.conversation.push({role:e.item?.role??"user",content:t});return}case"response.create":{let t=y();this.currentRequestId=t;let o=this.transport.sendResponseCreate({type:"response.create",response:e.response},this.conversation,t);this.active=(async()=>{try{for await(let s of o)this.emit(s)}catch(s){this.emit(i(s))}})();return}case"input_audio_buffer.append":this.audio?.appendInputAudio(e.audio);return;case"input_audio_buffer.commit":case"input_audio_buffer.clear":return;case"response.cancel":return;default:return}}async drain(){this.active&&await this.active}};r();var m=["session.update","conversation.item.create","response.create","response.cancel","input_audio_buffer.append","input_audio_buffer.commit","input_audio_buffer.clear"],v=["response.created","response.output_text.delta","response.output_item.added","response.function_call_arguments.delta","response.image_generation_call.partial_image","response.completed","error"];function h(n){return m.includes(n)}function R(n){return v.includes(n)}export{m as SUPPORTED_REALTIME_CLIENT_EVENTS,v as SUPPORTED_RESPONSES_EVENTS,a as UrunRealtime,S as UrunResponses,h as isSupportedClientEvent,R as isSupportedResponsesEvent,_ as listModels,f as nodeAudioBackend,i as toErrorEvent,E as weriftAudioBackend};
@@ -1,8 +1,8 @@
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,v=je();var ke=require("@earendil-works/pi-ai"),C=require("fs"),ve=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),typeof t.reasoning_effort=="string"&&(o.reasoning_effort=t.reasoning_effort),t.chat_template_kwargs!==void 0&&(o.chat_template_kwargs=t.chat_template_kwargs),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)}
1
+ "use strict";var Ce=Object.create;var F=Object.defineProperty;var Le=Object.getOwnPropertyDescriptor;var Me=Object.getOwnPropertyNames;var Ne=Object.getPrototypeOf,Oe=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))!Oe.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(Ne(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:()=>xe,createUrunExtension:()=>Pe,default:()=>dt,makeSessionFactory:()=>Se,makeStreamSimple:()=>Ie,notify:()=>Z,piDiagnosticLogPath:()=>J,resolveSessionEnv:()=>be,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),typeof t.reasoning_effort=="string"&&(o.reasoning_effort=t.reasoning_effort),t.chat_template_kwargs!==void 0&&(o.chat_template_kwargs=t.chat_template_kwargs),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 he(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,ye=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,ve.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(),h=(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 h!="function")throw new Error(`urun pi extension: app "${o}" has no function "${r}"`);let w=h();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(v);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(`
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 he(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,ye=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 be(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 xe(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=xe(J());return async(n,o,r)=>{let{baseUrl:l,orgId:i,auth:u}=be(n),{App:g,createClientToken:a}=await t(),h=(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 h!="function")throw new Error(`urun pi extension: app "${o}" has no function "${r}"`);let w=h();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
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,ke.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()},h=new AbortController,w=g?.signal,I=!1,N=()=>{I=!0,h.abort()};w&&(w.aborted?N():w.addEventListener("abort",N,{once:!0}));let L=!1,P,te=()=>{P&&clearTimeout(P),P=setTimeout(()=>{L=!0,h.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:k,contentIndex:R}=_;_=null,d.arguments=_e(d.name,k),a.push({type:"toolcall_end",contentIndex:R,toolCall:d,partial:s})},Ue=d=>{let k={type:"toolCall",id:String(d.call_id??d.id??`call_${s.content.length}`),name:String(d.name??""),arguments:{}};s.content.push(k);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}),k.arguments=_e(k.name,O),a.push({type:"toolcall_end",contentIndex:R,toolCall:k,partial:s})};return(async()=>{a.push({type:"start",partial:s});let d=!1,k,R=()=>{k&&clearInterval(k),k=void 0},O=Date.now();k=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.`),h.signal);R();let U,E;for(let p=1;p<=ye;p++)try{let y=await b.responses.create({model:i.id,input:x,stream:!0,tools:nt(u.tools),max_output_tokens:i.maxTokens,...g?.reasoning!=null&&g.reasoning!=="off"&&i.reasoning!==!1?{}:{chat_template_kwargs:{enable_thinking:!1}}});te();for await(let T of pt(y,h.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(y){if(E=y,h.signal.aborted)throw y;if(p<ye&&at(y)&&!M){await lt(p*500);continue}throw y}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(y){let T=L||y instanceof G;if(p>=we||!T||M||I)throw y;D(),t.evict(i.id),L=!1,h=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 y={type:"thinking",thinking:""};s.content.push(y);let T=s.content.length-1;a.push({type:"thinking_start",contentIndex:T,partial:s}),y.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 he({apiUrl:r,apiKey:o,fnName:l,fetchImpl:t.fetchImpl}),u=new H(t.openSession??Qe,n,l,(a,s)=>{let h=Ae(a,s);Ze(e)?Q(`${h}
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()},h=new AbortController,w=g?.signal,I=!1,O=()=>{I=!0,h.abort()};w&&(w.aborted?O():w.addEventListener("abort",O,{once:!0}));let L=!1,P,te=()=>{P&&clearTimeout(P),P=setTimeout(()=>{L=!0,h.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 N=typeof d.arguments=="string"?d.arguments:"";N&&a.push({type:"toolcall_delta",contentIndex:R,delta:N,partial:s}),v.arguments=_e(v.name,N),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},N=Date.now();v=setInterval(()=>{let b=Math.round((Date.now()-N)/1e3);Z(n,`uRun: still opening the "${i.id}" session\u2026 (${b}s \u2014 serve apps scale to zero, first turn can cold-start ~6min)`)},l);let $e=async b=>{let{responses:x}=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.`),h.signal);R();let U,E;for(let p=1;p<=ye;p++)try{let y=await x.responses.create({model:i.id,input:b,stream:!0,tools:nt(u.tools),max_output_tokens:i.maxTokens,...g?.reasoning!=null&&g.reasoning!=="off"&&i.reasoning!==!1?{}:{chat_template_kwargs:{enable_thinking:!1}}});te();for await(let T of pt(y,h.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(y){if(E=y,h.signal.aborted)throw y;if(p<ye&&at(y)&&!M){await lt(p*500);continue}throw y}if(E)throw E;return U};try{let b=Re(u),x;for(let p=1;p<=we;p++)try{x=await $e(b);break}catch(y){let T=L||y instanceof G;if(p>=we||!T||M||I)throw y;D(),t.evict(i.id),L=!1,h=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(x))Ue(p);if(!s.content.some(p=>p.type==="thinking")){let p=rt(x);if(p){let y={type:"thinking",thinking:""};s.content.push(y);let T=s.content.length-1;a.push({type:"thinking_start",contentIndex:T,partial:s}),y.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":x?.status==="incomplete"?"length":"stop";s.stopReason=E,Te(s.usage,x),a.push({type:"done",reason:E,message:s}),a.end(s)}catch(b){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 x=L?`uRun stream stalled \u2014 no output for ${Math.round(r/1e3)}s; the model may be hung. Retry, or Ctrl-C to abort.`:b instanceof Error?b.message:String(b);s.stopReason="error",s.errorMessage=x,a.push({type:"error",reason:"error",error:s})}a.end(s)}finally{D(),R(),w&&w.removeEventListener("abort",O),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 he({apiUrl:r,apiKey:o,fnName:l,fetchImpl:t.fetchImpl}),u=new H(t.openSession??Qe,n,l,(a,s)=>{let h=Ae(a,s);Ze(e)?Q(`${h}
8
8
  `):z.warn(h)});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,8 +1,8 @@
1
- import{c as X}from"../chunk-SSZL77P5.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 ke}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
- `)}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 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",ve="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()||ve}}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(ke(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:k,createClientToken:a}=await t(),m=(p.lane==="jwt"?k(o,{baseUrl:l,orgId:i,jwt:p.jwt,diagnosticSink:e}):k(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(`
1
+ import{c as z}from"../chunk-EFBD3CGZ.js";import{a as W,d as Q}from"../chunk-XHIIEA6Z.js";import{c as C}from"../chunk-YSFSRI3D.js";C();import{createAssistantMessageEventStream as he}from"@earendil-works/pi-ai";import{mkdirSync as we,openSync as _e,writeSync as ke}from"fs";import{dirname as ve}from"path";C();import{appendFileSync as de,mkdirSync as pe}from"fs";import fe from"os";import{dirname as ge,join as me}from"path";import{format as ye}from"util";function J(t=fe.homedir()){return me(t,".urun","logs","pi-extension.log")}var V=!1;function te(t,e){let n=J();try{pe(ge(n),{recursive:!0}),de(n,`${new Date().toISOString()} [console.${t}] [urun] ${ye(...e)}
2
+ `)}catch(o){if(!V){V=!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)=>te(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||te("assert",e.length>0?e:["Assertion failed"])}};function q(t){try{process.stderr.write(t)}catch{}}C();async function ne(t){let e=await Q(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 ae="urun-serve",xe="serve",Re=131072,be=16384,oe=3,Te=42e4,Ee=6e4,re=2,Se=15e3;function Ae(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()||W,fnName:(t.URUN_FUNCTION??"").trim()||xe}}function Ue(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 Ie(t){let e=null;return n=>{e===null&&(we(ve(t),{recursive:!0}),e=_e(t,"a"));let o=n.detail?` ${JSON.stringify(n.detail)}`:"";ke(e,`${new Date().toISOString()} [${n.level}] [urun] ${n.message}${o}
4
+ `)}}function Pe(t){let e=Ie(J());return async(n,o,s)=>{let{baseUrl:l,orgId:i,auth:p}=Ue(n),{App:k,createClientToken:a}=await t(),m=(p.lane==="jwt"?k(o,{baseUrl:l,orgId:i,jwt:p.jwt,diagnosticSink:e}):k(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 Ce=Pe(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 Ne(t,e){try{return Promise.resolve(t.end()).then(()=>{},n=>e(n))}catch(n){return e(n),Promise.resolve()}}function $e(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 le(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,s=(l,i)=>j.warn(le(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 z(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=>Ne(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 Le(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:De(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:Me(n.content)})}return Oe(e)}function Oe(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,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(k),o?.removeEventListener("abort",a),r())},k=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,k)=>{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=k?.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:x}=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 x.responses.create({model:i.id,input:v,stream:!0,tools:De(p.tools),max_output_tokens:i.maxTokens,...k?.reasoning!=null&&k.reasoning!=="off"&&i.reasoning!==!1?{}:{chat_template_kwargs:{enable_thinking:!1}}});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),x;for(let u=1;u<=oe;u++)try{x=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(x))le(u);if(!r.content.some(u=>u.type==="thinking")){let u=We(x);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":x?.status==="incomplete"?"length":"stop";r.stopReason=E,Ke(r.usage,x),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 x=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=x,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 k={name:"uRun",api:ie,baseUrl:"urun://session",apiKey:"$URUN_API_KEY",models:i.map(Xe),streamSimple:Be(p,t.timing??{},e)};e.registerProvider("urun",k)}}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};
6
+ `):e.push({...n})}return e}function Me(t){return typeof t=="string"?t:Array.isArray(t)?t.map(e=>e&&typeof e=="object"&&"text"in e?String(e.text):"").join(""):""}function De(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 Fe(t){if(!(!Array.isArray(t)||t.length===0))return t.map(e=>({type:"function",name:e.name,description:e.description,parameters:e.parameters}))}function se(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 Ke(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function We(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 Je(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 qe(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 D=class extends Error{};function He(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 Ye(t){return new Promise(e=>setTimeout(e,t))}function Ge(t,e,n,o){return new Promise((s,l)=>{let i=!1,p=r=>{i||(i=!0,clearTimeout(k),o?.removeEventListener("abort",a),r())},k=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*Be(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 ie(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 Xe(t,e={},n){let o=e.connectDeadlineMs??Te,s=e.stallTimeoutMs??Ee,l=e.phaseHeartbeatMs??Se;return(i,p,k)=>{let a=he(),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=k?.signal,A=!1,O=()=>{A=!0,m.abort()};h&&(h.aborted?O():h.addEventListener("abort",O,{once:!0}));let N=!1,U,Y=()=>{U&&clearTimeout(U),U=setTimeout(()=>{N=!0,m.abort()},s)},M=()=>{U&&clearTimeout(U),U=void 0},R=null,b=null,w=null,G="",$=!1,F=()=>{if(!b)return;let d=b;b=null,a.push({type:"thinking_end",contentIndex:r.content.indexOf(d),content:d.thinking,partial:r})},B=()=>{if(!R)return;let d=R;R=null,a.push({type:"text_end",contentIndex:r.content.indexOf(d),content:d.text,partial:r})},K=()=>{if(!w)return;let{call:d,args:_,contentIndex:T}=w;w=null,d.arguments=se(d.name,_),a.push({type:"toolcall_end",contentIndex:T,toolCall:d,partial:r})},ue=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 L=typeof d.arguments=="string"?d.arguments:"";L&&a.push({type:"toolcall_delta",contentIndex:T,delta:L,partial:r}),_.arguments=se(_.name,L),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},L=Date.now();_=setInterval(()=>{let v=Math.round((Date.now()-L)/1e3);ie(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 ce=async v=>{let{responses:x}=await Ge(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<=oe;u++)try{let y=await x.responses.create({model:i.id,input:v,stream:!0,tools:Fe(p.tools),max_output_tokens:i.maxTokens,...k?.reasoning!=null&&k.reasoning!=="off"&&i.reasoning!==!1?{}:{chat_template_kwargs:{enable_thinking:!1}}});Y();for await(let S of Be(y,m.signal)){Y();let g=S;if(g.type==="response.output_text.delta"){let c=typeof g.delta=="string"?g.delta:"";c&&($=!0,F(),K(),R||(R={type:"text",text:""},r.content.push(R),a.push({type:"text_start",contentIndex:r.content.length-1,partial:r})),R.text+=c,G+=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&&($=!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($=!0,F(),B(),!w||w.toolIndex!==c.tool_index){K();let X={type:"toolCall",id:c.call_id??`call_${c.tool_index}`,name:c.name??"",arguments:{}};r.content.push(X),w={call:X,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 D(`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<oe&&He(y)&&!$){await Ye(u*500);continue}throw y}if(E)throw E;return I};try{let v=Le(p),x;for(let u=1;u<=re;u++)try{x=await ce(v);break}catch(y){let S=N||y instanceof D;if(u>=re||!S||$||A)throw y;M(),t.evict(i.id),N=!1,m=new AbortController,ie(n,`uRun: the "${i.id}" session ended mid-turn \u2014 re-homing to a fresh assignment and replaying the turn\u2026`)}if(M(),F(),B(),K(),!r.content.some(u=>u.type==="toolCall"))for(let u of Ke(x))ue(u);if(!r.content.some(u=>u.type==="thinking")){let u=Je(x);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=qe(i.id,G);if(u)throw u}let E=I>0?"toolUse":x?.status==="incomplete"?"length":"stop";r.stopReason=E,We(r.usage,x),a.push({type:"done",reason:E,message:r}),a.end(r)}catch(v){if(d=!0,M(),A||h?.aborted&&!N)r.stopReason="aborted",r.errorMessage="aborted by user",a.push({type:"error",reason:"aborted",error:r});else{let x=N?`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=x,a.push({type:"error",reason:"error",error:r})}a.end(r)}finally{M(),T(),h&&h.removeEventListener("abort",O),d&&t.evict(i.id)}})(),a}}function ze(t){return{id:t,name:`uRun ${t}`,api:ae,reasoning:!0,input:["text"],cost:{input:0,output:0,cacheRead:0,cacheWrite:0},contextWindow:Re,maxTokens:be}}function Qe(t={}){return async e=>{let n=t.env??process.env,{apiKey:o,apiUrl:s,fnName:l}=Ae(n),i=await ne({apiUrl:s,apiKey:o,fnName:l,fetchImpl:t.fetchImpl}),p=new H(t.openSession??Ce,n,l,(a,r)=>{let m=le(a,r);$e(e)?q(`${m}
8
+ `):j.warn(m)});e.on("session_shutdown",()=>p.closeAll());let k={name:"uRun",api:ae,baseUrl:"urun://session",apiKey:"$URUN_API_KEY",models:i.map(ze),streamSimple:Xe(p,t.timing??{},e)};e.registerProvider("urun",k)}}var gt=Qe();export{H as SessionPool,ae as URUN_API,We as applyServeUsage,Oe as coalesceSameRole,Ie as createFileDiagnosticSink,Qe as createUrunExtension,gt as default,Pe as makeSessionFactory,Xe as makeStreamSimple,ie as notify,J as piDiagnosticLogPath,Ue as resolveSessionEnv,Le as toResponsesInput};