@urun-sh/openai 0.5.6 → 0.6.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.
Files changed (52) hide show
  1. package/dist/{ResponsesClient-BE3-hx3m.d.ts → ResponsesClient-CSSrOYD8.d.ts} +5 -1
  2. package/dist/{ResponsesClient-DjZWjlFH.d.cts → ResponsesClient-_OZERUjH.d.cts} +9 -1
  3. package/dist/{chunk-7M6LY6DR.js → chunk-3JWYIKHM.js} +1 -1
  4. package/dist/chunk-45FJ5GTP.js +7 -0
  5. package/dist/{chunk-BSHT6RZZ.js → chunk-6M5JK4YC.js} +1 -1
  6. package/dist/{chunk-5HKWNK3O.js → chunk-K23AZPI4.js} +1 -1
  7. package/dist/chunk-QTVTCMJU.js +1 -0
  8. package/dist/chunk-TDRZZGUK.js +1 -0
  9. package/dist/chunk-TPH77ZOW.js +58 -0
  10. package/dist/chunk-WCMX5VOF.js +4 -0
  11. package/dist/chunk-YXK42SKC.js +1 -0
  12. package/dist/gemini-live.cjs +2 -2
  13. package/dist/gemini-live.d.cts +64 -7
  14. package/dist/gemini-live.d.ts +27 -7
  15. package/dist/gemini-live.js +1 -1
  16. package/dist/hosted/bin.cjs +37 -35
  17. package/dist/hosted/bin.js +1 -1
  18. package/dist/hosted/index.cjs +33 -31
  19. package/dist/hosted/index.d.cts +70 -4
  20. package/dist/hosted/index.d.ts +21 -4
  21. package/dist/hosted/index.js +1 -1
  22. package/dist/index.cjs +1 -1
  23. package/dist/index.d.cts +4 -4
  24. package/dist/index.d.ts +4 -4
  25. package/dist/index.js +1 -1
  26. package/dist/pi-extension/index.cjs +7 -7
  27. package/dist/pi-extension/index.d.cts +2 -2
  28. package/dist/pi-extension/index.d.ts +2 -2
  29. package/dist/pi-extension/index.js +1 -1
  30. package/dist/pi-extension/standalone.cjs +53 -53
  31. package/dist/proxy/cli.cjs +47 -45
  32. package/dist/proxy/cli.js +15 -14
  33. package/dist/proxy/index.cjs +28 -27
  34. package/dist/proxy/index.d.cts +54 -16
  35. package/dist/proxy/index.d.ts +18 -11
  36. package/dist/proxy/index.js +1 -1
  37. package/dist/{responses-turn-tNUHq5b0.d.cts → responses-turn-BfdBbey8.d.cts} +610 -23
  38. package/dist/{responses-turn-Dr37N3kH.d.ts → responses-turn-CdhIre_a.d.ts} +173 -7
  39. package/dist/{translator-Bh0Bp_Ie.d.cts → translator-CO8W_hmJ.d.cts} +1 -1
  40. package/dist/{translator-BCoRFaTs.d.ts → translator-Ddd65sXR.d.ts} +1 -1
  41. package/dist/{types-lsVTbNcH.d.cts → types-CHPtJx6d.d.cts} +13 -0
  42. package/dist/{types-lsVTbNcH.d.ts → types-CHPtJx6d.d.ts} +4 -0
  43. package/dist/{video-out-CCksIcj8.d.cts → video-out-BsuLqlID.d.cts} +1 -1
  44. package/dist/{video-out-IGQ4YQ-c.d.ts → video-out-Cawtm7SF.d.ts} +1 -1
  45. package/package.json +4 -2
  46. package/dist/chunk-2T2YYBVX.js +0 -1
  47. package/dist/chunk-3WAJD62J.js +0 -4
  48. package/dist/chunk-FP4RSAIE.js +0 -1
  49. package/dist/chunk-HR5H6S7L.js +0 -1
  50. package/dist/chunk-NY23USZF.js +0 -57
  51. package/dist/chunk-VLRMJRLS.js +0 -6
  52. /package/dist/{chunk-5CCJUNH7.js → chunk-YSFSRI3D.js} +0 -0
@@ -1,12 +1,12 @@
1
1
  import { Server } from 'node:http';
2
2
  import * as _prometheus_io_client from '@prometheus-io/client';
3
3
  import { Registry } from '@prometheus-io/client';
4
- import { c as LedgerOutcome, I as InferenceRecord, S as SessionGoneError, P as ProxyClients, M as ModelRouter, i as UsageRowReporter, b as InferenceUsageRecord, e as LedgerWrite, d as LedgerRecorded, L as LedgerFailure } from '../responses-turn-tNUHq5b0.cjs';
4
+ import { c as LedgerOutcome, I as InferenceRecord, S as SessionGoneError, P as ProxyClients, M as ModelRouter, i as UsageRowReporter, b as InferenceUsageRecord, e as LedgerWrite, d as LedgerRecorded, L as LedgerFailure } from '../responses-turn-BfdBbey8.cjs';
5
5
  import { createClientToken } from '@urun-sh/core';
6
- import { U as UrunResponses } from '../ResponsesClient-DjZWjlFH.cjs';
7
- import { b as UrunSessionLike } from '../types-lsVTbNcH.cjs';
6
+ import { U as UrunResponses } from '../ResponsesClient-_OZERUjH.cjs';
7
+ import { b as UrunSessionLike } from '../types-CHPtJx6d.cjs';
8
8
  import '../models-DUdx_Y6X.cjs';
9
- import '../video-out-CCksIcj8.cjs';
9
+ import '../video-out-BsuLqlID.cjs';
10
10
 
11
11
  /**
12
12
  * The container's metrics port — a MODULE CONSTANT, not an env var, per the
@@ -368,6 +368,15 @@ type OwnedSession = UrunSessionLike & {
368
368
  timeout?: number;
369
369
  signal?: AbortSignal;
370
370
  }) => Promise<void>;
371
+ /**
372
+ * Core's NATIVE admission-ticket cancel (`Session.cancel`): hands back the
373
+ * claim a still-QUEUED call waits on — once the call HAS a session, the
374
+ * installed handler refuses loudly. Optional here because only the
375
+ * client-gone abandon path requires it; that path falls back to the native
376
+ * `detach` when a session object lacks it (the queued shape does not exist
377
+ * there), and never approximates with `end()`.
378
+ */
379
+ cancel?: () => Promise<unknown>;
371
380
  };
372
381
  /**
373
382
  * One pooled backhaul: the session, its (stateful) Responses client, and the
@@ -449,6 +458,24 @@ interface TenantRegistryOptions {
449
458
  apiUrl: string;
450
459
  /** The model-catalog oracle for routing rule 5, or null. */
451
460
  catalog: CatalogConfig | null;
461
+ /**
462
+ * THE STATELESS DIRECT PATH (urun-ts #497): present ⇒ every tenant's
463
+ * clients carry a per-caller decision dial — discovery over the CALLER'S
464
+ * own key (`GET {apiUrl}/runtimes`), tokens minted with the caller's
465
+ * org/app/fn, the flat body POSTed to the ready runtime's `:8099/v1/decision`.
466
+ * `shared` (the shared org's API key, when mounted) extends the same dial
467
+ * to SHARED-catalog models: discovery under the shared key, the token's
468
+ * `tenant` stays the caller and `provider` names the pod org from the
469
+ * block's `shared_org_id` (the intake's provider-aware org gate). Absent
470
+ * ⇒ shared models keep the session dial. Absent ⇒ no dial exists and every
471
+ * decision rides the session lane byte-for-byte.
472
+ */
473
+ decisionIntake?: {
474
+ secret: string;
475
+ shared?: {
476
+ apiKey: string;
477
+ };
478
+ };
452
479
  /** Injected in tests; production builds real uRun backhauls. */
453
480
  build?: (caller: CallerIdentity) => {
454
481
  router: ModelRouter<PoolEntry>;
@@ -894,6 +921,39 @@ interface HostedConfig {
894
921
  apiUrl: string;
895
922
  /** The `model_catalog` oracle for routing rule 5, or null when unconfigured. */
896
923
  catalog: CatalogConfig | null;
924
+ /**
925
+ * THE STATELESS DIRECT PATH's credential (URUN_SESSION_TOKEN_SECRET) — the
926
+ * runtime scoped-token signing secret the decision intake verifies
927
+ * (urun-python decision_intake). A SECRET input, not a behavior knob: it is
928
+ * the mandate's allowed env class, and it is the SAME predicate the local
929
+ * lane enables on (cli.ts statelessDialEnabled), so the two lanes can never
930
+ * disagree. Absent ('') the stateless lane does not exist and every
931
+ * decision rides the session lane byte-for-byte. Present, decisions for
932
+ * CALLER-ORG serves="openai" apps dispatch straight to a ready runtime's
933
+ * `:8099/v1/decision`: discovery rides the CALLER'S OWN key
934
+ * (`GET {apiUrl}/runtimes`), and the per-decision token is minted with the
935
+ * caller's org/app/fn identity. Shared-catalog models keep the session
936
+ * dial — the v1 boundary #497 declares (the dial's own targetFor refuses
937
+ * them).
938
+ *
939
+ * OPTIONAL on this bag because legacy constructors may omit it
940
+ * (resolveHostedConfig always supplies it — '' when the env is absent);
941
+ * consumers read it as `?? ''`.
942
+ */
943
+ decisionSecret?: string;
944
+ /**
945
+ * THE SHARED LANE'S credential (URUN_SHARED_ORG_API_KEY): the shared org's
946
+ * API key, so the decision dial can discover — and its tokens can name via
947
+ * the block's `shared_org_id` — the SHARED org's pods. Optional: absent ⇒
948
+ * shared models keep the session dial (the stateless boundary narrows to
949
+ * "no shared credential mounted"). URUN_SHARED_ORG_ID must ride WITH it
950
+ * (both-or-neither, loud) purely as the identity statement of the key —
951
+ * the dial never re-spells the pod org; it reads `shared_org_id` from the
952
+ * block that resolved the lane, so key and pod org cannot drift.
953
+ */
954
+ sharedOrgKey?: string;
955
+ /** Identity statement accompanying `sharedOrgKey` (URUN_SHARED_ORG_ID). */
956
+ sharedOrgId?: string;
897
957
  }
898
958
  /**
899
959
  * Read and VALIDATE the deployment's external inputs. Every failure is loud
@@ -1031,6 +1091,12 @@ declare function createHostedProxy(options: HostedProxyOptions): {
1031
1091
  * The caller owns listen/close.
1032
1092
  */
1033
1093
  edgeIdentityServer: Server;
1094
+ /**
1095
+ * The tenant registry this proxy built (production wiring). Tests assert
1096
+ * the per-caller stateless dial through it; production code goes through
1097
+ * `callerOf`, never here.
1098
+ */
1099
+ registry: TenantRegistry;
1034
1100
  closeAll: () => Promise<void>;
1035
1101
  closeWebSockets: () => Promise<void>;
1036
1102
  };
@@ -1,12 +1,12 @@
1
1
  import { Server } from 'node:http';
2
2
  import * as _prometheus_io_client from '@prometheus-io/client';
3
3
  import { Registry } from '@prometheus-io/client';
4
- import { c as LedgerOutcome, I as InferenceRecord, S as SessionGoneError, P as ProxyClients, M as ModelRouter, i as UsageRowReporter, b as InferenceUsageRecord, e as LedgerWrite, d as LedgerRecorded, L as LedgerFailure } from '../responses-turn-Dr37N3kH.js';
4
+ import { c as LedgerOutcome, I as InferenceRecord, S as SessionGoneError, P as ProxyClients, M as ModelRouter, i as UsageRowReporter, b as InferenceUsageRecord, e as LedgerWrite, d as LedgerRecorded, L as LedgerFailure } from '../responses-turn-CdhIre_a.js';
5
5
  import { createClientToken } from '@urun-sh/core';
6
- import { U as UrunResponses } from '../ResponsesClient-BE3-hx3m.js';
7
- import { b as UrunSessionLike } from '../types-lsVTbNcH.js';
6
+ import { U as UrunResponses } from '../ResponsesClient-CSSrOYD8.js';
7
+ import { b as UrunSessionLike } from '../types-CHPtJx6d.js';
8
8
  import '../models-DUdx_Y6X.js';
9
- import '../video-out-IGQ4YQ-c.js';
9
+ import '../video-out-Cawtm7SF.js';
10
10
 
11
11
  declare const METRICS_PORT = 9464;
12
12
 
@@ -133,6 +133,8 @@ type OwnedSession = UrunSessionLike & {
133
133
  timeout?: number;
134
134
  signal?: AbortSignal;
135
135
  }) => Promise<void>;
136
+
137
+ cancel?: () => Promise<unknown>;
136
138
  };
137
139
 
138
140
  type PoolEntry = {
@@ -150,6 +152,13 @@ interface TenantRegistryOptions {
150
152
 
151
153
  catalog: CatalogConfig | null;
152
154
 
155
+ decisionIntake?: {
156
+ secret: string;
157
+ shared?: {
158
+ apiKey: string;
159
+ };
160
+ };
161
+
153
162
  build?: (caller: CallerIdentity) => {
154
163
  router: ModelRouter<PoolEntry>;
155
164
  clients: ProxyClients;
@@ -291,6 +300,12 @@ interface HostedConfig {
291
300
  apiUrl: string;
292
301
 
293
302
  catalog: CatalogConfig | null;
303
+
304
+ decisionSecret?: string;
305
+
306
+ sharedOrgKey?: string;
307
+
308
+ sharedOrgId?: string;
294
309
  }
295
310
 
296
311
  declare function resolveHostedConfig(env?: NodeJS.ProcessEnv): HostedConfig;
@@ -329,6 +344,8 @@ declare function createHostedProxy(options: HostedProxyOptions): {
329
344
  metrics: InferenceMetrics;
330
345
 
331
346
  edgeIdentityServer: Server;
347
+
348
+ registry: TenantRegistry;
332
349
  closeAll: () => Promise<void>;
333
350
  closeWebSockets: () => Promise<void>;
334
351
  };
@@ -1 +1 @@
1
- import{A as x,B as v,C as m,D as y,E as B,F as H,G as P,H as u,I as G,J as U,K as F,L as X,M as Y,N as b,O as h,P as k,Q as K,R as V,S as j,T as W,U as q,a as r,b as o,c as t,d as E,e as n,f as _,g as a,h as T,i as O,j as i,k as s,l as I,m as p,n as A,o as R,p as S,q as N,r as C,s as D,t as L,u as M,v as d,w as f,x as l,y as c,z as g}from"../chunk-VLRMJRLS.js";import"../chunk-HR5H6S7L.js";import"../chunk-5HKWNK3O.js";import"../chunk-NY23USZF.js";import"../chunk-FP4RSAIE.js";import"../chunk-3WAJD62J.js";import"../chunk-7M6LY6DR.js";import"../chunk-BSHT6RZZ.js";import"../chunk-2T2YYBVX.js";import{c as e}from"../chunk-5CCJUNH7.js";e();export{N as ANONYMOUS_KEY_ID,y as BIND_HOST,R as BUCKET_DOMAIN,O as BoundedLabel,f as ControlPlaneUnavailableError,X as ConversationBackhauls,F as ConversationCapacityError,v as DEFAULT_PORT,I as EDGE_IDENTITY_PORT,A as EDGE_KEY_FINGERPRINT_HEADER,p as EDGE_KEY_ID_HEADER,i as InferenceMetrics,Y as LEDGER_FUNCTION_PATH,b as LEDGER_WRITE_TIMEOUT_MS,V as LedgerClient,h as LedgerDuplicateError,k as LedgerSurfaceError,g as MAX_CACHED_KEYS,G as MAX_CONVERSATIONS,U as MAX_CONVERSATION_HANDLES,n as MAX_MODEL_LABELS,_ as MAX_ORG_LABELS,H as MAX_TENANTS,o as METRICS_PATH,r as METRICS_PORT,E as NO_MODEL_LABEL,c as ORG_BINDING_TTL_MS,t as OTHER_LABEL,x as OrgResolver,d as ProxyAuthError,W as READINESS_TIMEOUT_MS,j as READINESS_TTL_MS,m as SERVE_FUNCTION,S as TIER_DOMAIN,u as TenantRegistry,l as bearerFrom,L as bearerTokenOf,M as createEdgeIdentityServer,q as createHostedProxy,s as createMetricsServer,C as edgeBucketId,D as edgeTierFingerprint,a as laneOf,K as parseLedgerRecorded,B as resolveHostedConfig,T as statusClassOf,P as tenantSubject};
1
+ import{A as x,B as v,C as m,D as y,E as B,F as H,G as P,H as u,I as G,J as U,K as F,L as X,M as Y,N as b,O as h,P as k,Q as K,R as V,S as j,T as W,U as q,a as r,b as o,c as t,d as E,e as n,f as _,g as a,h as T,i as O,j as i,k as s,l as I,m as p,n as A,o as R,p as S,q as N,r as C,s as D,t as L,u as M,v as d,w as f,x as l,y as c,z as g}from"../chunk-45FJ5GTP.js";import"../chunk-TDRZZGUK.js";import"../chunk-K23AZPI4.js";import"../chunk-TPH77ZOW.js";import"../chunk-YXK42SKC.js";import"../chunk-WCMX5VOF.js";import"../chunk-3JWYIKHM.js";import"../chunk-6M5JK4YC.js";import"../chunk-QTVTCMJU.js";import{c as e}from"../chunk-YSFSRI3D.js";e();export{N as ANONYMOUS_KEY_ID,y as BIND_HOST,R as BUCKET_DOMAIN,O as BoundedLabel,f as ControlPlaneUnavailableError,X as ConversationBackhauls,F as ConversationCapacityError,v as DEFAULT_PORT,I as EDGE_IDENTITY_PORT,A as EDGE_KEY_FINGERPRINT_HEADER,p as EDGE_KEY_ID_HEADER,i as InferenceMetrics,Y as LEDGER_FUNCTION_PATH,b as LEDGER_WRITE_TIMEOUT_MS,V as LedgerClient,h as LedgerDuplicateError,k as LedgerSurfaceError,g as MAX_CACHED_KEYS,G as MAX_CONVERSATIONS,U as MAX_CONVERSATION_HANDLES,n as MAX_MODEL_LABELS,_ as MAX_ORG_LABELS,H as MAX_TENANTS,o as METRICS_PATH,r as METRICS_PORT,E as NO_MODEL_LABEL,c as ORG_BINDING_TTL_MS,t as OTHER_LABEL,x as OrgResolver,d as ProxyAuthError,W as READINESS_TIMEOUT_MS,j as READINESS_TTL_MS,m as SERVE_FUNCTION,S as TIER_DOMAIN,u as TenantRegistry,l as bearerFrom,L as bearerTokenOf,M as createEdgeIdentityServer,q as createHostedProxy,s as createMetricsServer,C as edgeBucketId,D as edgeTierFingerprint,a as laneOf,K as parseLedgerRecorded,B as resolveHostedConfig,T as statusClassOf,P as tenantSubject};
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var Q=Object.create;var h=Object.defineProperty;var K=Object.getOwnPropertyDescriptor;var X=Object.getOwnPropertyNames;var Z=Object.getPrototypeOf,ee=Object.prototype.hasOwnProperty;var te=(e,t)=>{for(var n in t)h(e,n,{get:t[n],enumerable:!0})},x=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of X(t))!ee.call(e,o)&&o!==n&&h(e,o,{get:()=>t[o],enumerable:!(r=K(t,o))||r.enumerable});return e};var v=(e,t,n)=>(n=e!=null?Q(Z(e)):{},x(t||!e||!e.__esModule?h(n,"default",{value:e,enumerable:!0}):n,e)),ne=e=>x(h({},"__esModule",{value:!0}),e);var fe={};te(fe,{RT_VIDEO_OUT_STREAM:()=>y,SUPPORTED_REALTIME_CLIENT_EVENTS:()=>A,SUPPORTED_RESPONSES_EVENTS:()=>T,UrunRealtime:()=>_,UrunResponses:()=>k,VIDEO_OUT_DEFAULT_CODEC:()=>b,VIDEO_OUT_FRAMING:()=>R,VIDEO_OUT_WS_FRAMING:()=>H,consumeVideoOut:()=>F,decodeVideoOutRecord:()=>E,encodeVideoOutRecord:()=>N,isSupportedClientEvent:()=>j,isSupportedResponsesEvent:()=>J,listModels:()=>B,negotiateSessionVideoOutQuic:()=>W,nodeAudioBackend:()=>D,openSessionVideoOutLane:()=>q,toErrorEvent:()=>m,weriftAudioBackend:()=>P});module.exports=ne(fe);function I(e,t,n,r){let o=e.response??{},i={request_id:n,consumer_id:r,stream:!0,kind:"chat",messages:t};return typeof o.instructions=="string"&&(i.instructions=o.instructions),Array.isArray(o.modalities)&&(i.modalities=o.modalities),typeof o.temperature=="number"&&(i.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(i.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(i.tools=o.tools),i}function L(e,t,n){let r={request_id:t,consumer_id:n,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(r.model=e.model),e.tools!==void 0&&(r.tools=e.tools),e.tool_choice!==void 0&&(r.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(r.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(r.max_output_tokens=e.max_output_tokens),typeof e.top_p=="number"&&(r.top_p=e.top_p),Array.isArray(e.stop)&&e.stop.length>0&&(r.stop=e.stop),typeof e.reasoning_effort=="string"&&(r.reasoning_effort=e.reasoning_effort),e.chat_template_kwargs!==void 0&&(r.chat_template_kwargs=e.chat_template_kwargs),e.response_format!==void 0&&(r.response_format=e.response_format),r}function m(e){if(e instanceof Error)return{type:"error",error:{type:"urun_error",code:e.name||null,message:e.message}};if(e&&typeof e=="object"&&e.t==="error"){let t=e,n=t.body??{},r=n.error,o=r?.message??n.message??`unreadable serve error body: ${JSON.stringify(t.body??null)}`;return{type:"error",error:{type:r?.type??"urun_error",code:t.code??r?.code??null,message:o}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}var re="llm-resp";async function*S(e,t){let n=`${re}:${t}`,r=`resp_${t}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};let o=new Map;for await(let i of e.stream(n).messages()){let d=i;if(d.t==="delta"){if(typeof d.delta=="string"&&(yield{type:"response.output_text.delta",item_id:r,delta:d.delta}),typeof d.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:r,delta:d.reasoning}),Array.isArray(d.tool_calls))for(let u of d.tool_calls){let s=typeof u.index=="number"?u.index:0,a=o.get(s)??{};u.id&&(a.id=u.id),u.function?.name&&(a.name=u.function.name),o.set(s,a),yield{type:"response.function_call_arguments.delta",item_id:`fc_${t}_${s}`,tool_index:s,call_id:a.id,name:a.name,delta:u.function?.arguments??""}}}else if(d.t==="response"){yield{type:"response.completed",response:{id:r,status:"completed",...d.body}};return}else if(d.t==="error"){yield m(d);return}}}var oe="llm",g=class{constructor(t){this.session=t;this.sessionTag=t.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(t){t.session_tag=this.sessionTag,this.session.doc(oe).set({requests:{[t.request_id]:{payload:t,consumer_id:t.consumer_id,stream:t.stream}}})}sendResponses(t,n){let r=L(t,n,this.consumerId);return this.write(r),S(this.session,n)}sendResponseCreate(t,n,r){let o=I(t,n,r,this.consumerId);return this.write(o),S(this.session,r)}};var V="audio",O=class{constructor(t,n){this.backend=t;this.opts=n}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(t){if(!this.source)throw new Error("startOutbound() not called");let n=Buffer.from(t,"base64"),r=Math.floor(n.byteLength/2),o=new Int16Array(r);for(let i=0;i<r;i++)o[i]=n.readInt16LE(i*2);this.source.onData(o)}async startInbound(t){if(t==null)throw new Error('startInbound: no downstream audio track \u2014 the runtime audio producer has not been consumed yet (session.stream("audio").track is null)');this.sink=this.backend.createSink(t),this.sink.onframe=n=>{let r=Buffer.from(n.samples.buffer,n.samples.byteOffset,n.samples.byteLength).toString("base64");for(let o of this.outHandlers)o(r)}}onOutputAudio(t){return this.outHandlers.add(t),()=>this.outHandlers.delete(t)}};async function U(e,t,n=1e4){let r=new O(t,{sampleRate:24e3}),o=await r.startOutbound();await e.stream(V).attach(o);let i=await se(e,n);return await r.startInbound(i),r}function se(e,t){let n=e.stream(V);return n.track!=null?Promise.resolve(n.track):new Promise((r,o)=>{let i,d=setTimeout(()=>{i?.(),o(new Error(`enableSessionAudio: no downstream audio track within ${t}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},t),u=s=>{s!=null&&(clearTimeout(d),i?.(),r(s))};i=n.on?.("track",u),n.track!=null&&u(n.track)})}var ie=24e3,w=480,ae=111;async function de(){let e=await import("opusscript"),t=e.default??e;return new t(ie,1,2048)}async function P(){let e=await import("werift"),t=await de();return{createSource:()=>{let n=new e.MediaStreamTrack({kind:"audio"}),r=Math.random()*4294967295>>>0,o=Math.random()*65535&65535,i=Math.random()*4294967295>>>0,d=new Int16Array(0);return{track:n,onData:u=>{let s=new Int16Array(d.length+u.length);s.set(d,0),s.set(u,d.length);let a=0;for(;s.length-a>=w;){let c=s.subarray(a,a+w);a+=w;let l=Buffer.from(c.buffer,c.byteOffset,c.byteLength),Y=t.encode(l,w);o=o+1&65535,i=i+w>>>0;let G=new e.RtpHeader({version:2,payloadType:ae,sequenceNumber:o,timestamp:i,ssrc:r,marker:!1}),z=new e.RtpPacket(G,Y);n.writeRtp(z)}d=s.slice(a)}}},createSink:n=>{let r={onframe:null};return n.onReceiveRtp.subscribe(i=>{let d;try{d=t.decode(i.payload)}catch{return}let u=new Int16Array(d.length/2);for(let s=0;s<u.length;s++)u[s]=d.readInt16LE(s*2);r.onframe?.({samples:u})}),r}}}async function D(){let e=await import("@roamhq/wrtc");return{createSource:()=>{let t=new e.nonstandard.RTCAudioSource;return{track:t.createTrack(),onData:r=>t.onData({samples:r,sampleRate:24e3})}},createSink:t=>{let n={onframe:null},r=new e.nonstandard.RTCAudioSink(t);return r.ondata=o=>{n.onframe?.(o)},n}}}var C=0;function ue(){return C+=1,`rt_${Date.now().toString(36)}_${C.toString(36)}`}var _=class{constructor(t,n={}){this.session=t;this.opts=n;this.transport=new g(t)}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(t=>this.emit({type:"response.audio.delta",delta:t}))}on(t,n){return this.handlers.add(n),()=>this.handlers.delete(n)}emit(t){for(let n of this.handlers)n(t)}send(t){switch(t.type){case"conversation.item.create":{let n=(t.item?.content??[]).map(r=>r.text??"").join("");this.conversation.push({role:t.item?.role??"user",content:n});return}case"response.create":{let n=ue();this.currentRequestId=n;let r=this.transport.sendResponseCreate({type:"response.create",response:t.response},this.conversation,n);this.active=(async()=>{try{for await(let o of r)this.emit(o)}catch(o){this.emit(m(o))}})();return}case"input_audio_buffer.append":this.audio?.appendInputAudio(t.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 M=0;function ce(){return M+=1,`req_${Date.now().toString(36)}_${M.toString(36)}`}var k=class{transport;constructor(t){this.transport=new g(t)}responses={create:async t=>{let n=ce(),r=this.transport.sendResponses(t,n);return Object.assign((async function*(){yield*r})(),{requestId:n})}}};async function pe(e){let n=await(e.fetchImpl??fetch)(`${e.catalogUrl}/model_catalog?select=model_id,variant,gpu_spec,engine,task,description,size_gb,hf_repo,engine_args,updated_at`,{headers:{apikey:e.anonKey,Authorization:`Bearer ${e.anonKey}`}});if(!n.ok)throw new Error(`model_catalog fetch failed: ${n.status}`);return await n.json()}async function B(e){return{object:"list",data:(await pe(e)).map(n=>({id:`${n.model_id}:${n.variant}`,object:"model",created:0,owned_by:"urun"}))}}var f=require("@urun-sh/core"),y="rt-video-out",R="urun.videoOut.v1",b="h264";function $(e,t){if(!Number.isInteger(e.seq)||e.seq<0)throw new Error(`${t}: header seq must be a non-negative integer (got ${e.seq})`);if(!Number.isInteger(e.ptsUs))throw new Error(`${t}: header ptsUs must be an integer microsecond timestamp (got ${e.ptsUs})`);if(typeof e.codec!="string"||e.codec.length===0)throw new Error(`${t}: header codec must be a non-empty string`);if(typeof e.key!="boolean")throw new Error(`${t}: header key must be a boolean`)}function N(e,t){if($(e,"encodeVideoOutRecord"),t.byteLength===0)throw new Error("encodeVideoOutRecord: refusing to encode an empty access unit");let n=new TextEncoder().encode(JSON.stringify({v:1,seq:e.seq,ptsUs:e.ptsUs,codec:e.codec,key:e.key}));if(n.byteLength>65535)throw new Error(`encodeVideoOutRecord: header JSON is ${n.byteLength} bytes \u2014 over the u16 length prefix`);let r=new Uint8Array(2+n.byteLength+t.byteLength);return r[0]=n.byteLength&255,r[1]=n.byteLength>>8,r.set(n,2),r.set(t,2+n.byteLength),r}function E(e){if(e.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${e.byteLength} byte(s), no header length)`);let t=e[0]|e[1]<<8;if(e.byteLength<2+t)throw new Error(`decodeVideoOutRecord: truncated record (header says ${t} bytes, ${e.byteLength-2} present)`);let n;try{n=JSON.parse(new TextDecoder().decode(e.subarray(2,2+t)))}catch(d){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(d)})`)}let r=n;if(r===null||typeof r!="object"||r.v!==1)throw new Error(`decodeVideoOutRecord: header is not spec v1 (v=${JSON.stringify(r?.v)}) \u2014 a newer producer must be met with a newer consumer, never silently misread`);let o=r;$(o,"decodeVideoOutRecord");let i=e.subarray(2+t);if(i.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:o,au:i.slice()}}async function W(e){let t=e.wtDataEndpoint;if(typeof t!="function")throw new Error("negotiateSessionVideoOutQuic: the core session exposes no wtDataEndpoint() \u2014 the \xA75-over-QUIC dial seam (urun-infra#1608 TransportOfferResponse.webtransport, offered with wtData: true) is required for the video OUT lane; there is no WS fallback");let n=await t.call(e),r=n.sessionId??e.sessionId;if(typeof r!="string"||r.length===0)throw new Error("negotiateSessionVideoOutQuic: no session id \u2014 the WT hello requires one and neither the negotiated endpoint nor the core session carries it");return{transport:"webtransport",url:n.url,certHashes:n.certHashes,token:n.token,sessionId:r,stream:y,codec:b,framing:R}}var H="urun.videoOut.v2-ws";function q(e){let t=e.stream(y);if(typeof t.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${y}') has no messages() \u2014 the named-DATA stream consume seam (@urun-sh/core SessionStream.messages, contract \xA75) is required for the video OUT lane`);let n=new Set,r=new Set,o=new Set,i=s=>{for(let a of r)a(s)},d=!1,u=async()=>{for await(let s of t.messages()){if(!(s instanceof Uint8Array)){i(new Error(`openSessionVideoOutLane: "${y}" delivered a non-BINARY \xA75 payload (${typeof s}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let a;try{a=E(s)}catch(c){i(c instanceof Error?c:new Error(String(c)));continue}for(let c of n)c(a)}for(let s of o)s()};return{codec:b,onOutputFrame:s=>(n.add(s),d||(d=!0,u().catch(a=>i(a instanceof Error?a:new Error(String(a))))),()=>n.delete(s)),onLaneError:s=>(r.add(s),()=>r.delete(s)),onEnd:s=>(o.add(s),()=>o.delete(s))}}function le(e){if(e.length%2!==0||/[^0-9a-fA-F]/.test(e))throw new Error(`consumeVideoOut: certHashes entry is not hex sha-256 (${JSON.stringify(e)})`);let t=new Uint8Array(e.length/2);for(let n=0;n<t.length;n++)t[n]=parseInt(e.slice(n*2,n*2+2),16);return t}async function F(e,t,n){let r=n??(typeof globalThis.WebTransport=="function"?(a,c)=>new globalThis.WebTransport(a,c):void 0);if(!r)throw new Error("consumeVideoOut: no WebTransport implementation \u2014 this platform has no globalThis.WebTransport; inject a connect factory (the third argument)");let o=r(e.url,{serverCertificateHashes:e.certHashes.map(a=>({algorithm:"sha-256",value:le(a)}))});await o.ready;let i=await o.createBidirectionalStream();await i.writable.getWriter().write((0,f.encodeWtControlFrame)({t:"hello",token:e.token,sessionId:e.sessionId}));let d=i.readable.getReader(),u=new f.WtControlDecoder,s=!1;for(;!s;){let{value:a,done:c}=await d.read();if(c)throw new Error("consumeVideoOut: gateway closed the control stream before hello-ack");if(a)for(let l of u.push(a)){if(l.t==="hello-ack"){s=!0;break}throw new Error(`consumeVideoOut: gateway rejected hello (got ${JSON.stringify(l.t)} before hello-ack)`)}}await(0,f.consumeWtDataStreams)(o.incomingUnidirectionalStreams,{onData:(a,c)=>{if(a===e.stream){if(c===f.STREAM_END){t.onEnd?.();return}if(!(c instanceof Uint8Array)){t.onLaneError(new Error(`consumeVideoOut: "${e.stream}" delivered a non-BINARY \xA75 payload (${typeof c}) \u2014 spec v1 records ride BINARY envelopes only`));return}try{t.onFrame(E(c))}catch(l){t.onLaneError(l instanceof Error?l:new Error(String(l)))}}},onLaneError:a=>t.onLaneError(a)})}var A=["session.update","conversation.item.create","response.create","response.cancel","input_audio_buffer.append","input_audio_buffer.commit","input_audio_buffer.clear"],T=["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 j(e){return A.includes(e)}function J(e){return T.includes(e)}0&&(module.exports={RT_VIDEO_OUT_STREAM,SUPPORTED_REALTIME_CLIENT_EVENTS,SUPPORTED_RESPONSES_EVENTS,UrunRealtime,UrunResponses,VIDEO_OUT_DEFAULT_CODEC,VIDEO_OUT_FRAMING,VIDEO_OUT_WS_FRAMING,consumeVideoOut,decodeVideoOutRecord,encodeVideoOutRecord,isSupportedClientEvent,isSupportedResponsesEvent,listModels,negotiateSessionVideoOutQuic,nodeAudioBackend,openSessionVideoOutLane,toErrorEvent,weriftAudioBackend});
1
+ "use strict";var Q=Object.create;var h=Object.defineProperty;var X=Object.getOwnPropertyDescriptor;var Z=Object.getOwnPropertyNames;var ee=Object.getPrototypeOf,te=Object.prototype.hasOwnProperty;var ne=(e,t)=>{for(var n in t)h(e,n,{get:t[n],enumerable:!0})},L=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Z(t))!te.call(e,o)&&o!==n&&h(e,o,{get:()=>t[o],enumerable:!(r=X(t,o))||r.enumerable});return e};var S=(e,t,n)=>(n=e!=null?Q(ee(e)):{},L(t||!e||!e.__esModule?h(n,"default",{value:e,enumerable:!0}):n,e)),re=e=>L(h({},"__esModule",{value:!0}),e);var me={};ne(me,{RT_VIDEO_OUT_STREAM:()=>y,SUPPORTED_REALTIME_CLIENT_EVENTS:()=>T,SUPPORTED_RESPONSES_EVENTS:()=>x,UrunRealtime:()=>_,UrunResponses:()=>v,VIDEO_OUT_DEFAULT_CODEC:()=>E,VIDEO_OUT_FRAMING:()=>A,VIDEO_OUT_WS_FRAMING:()=>q,consumeVideoOut:()=>j,decodeVideoOutRecord:()=>b,encodeVideoOutRecord:()=>W,isSupportedClientEvent:()=>G,isSupportedResponsesEvent:()=>Y,listModels:()=>$,negotiateSessionVideoOutQuic:()=>H,nodeAudioBackend:()=>M,openSessionVideoOutLane:()=>F,toErrorEvent:()=>f,weriftAudioBackend:()=>D});module.exports=re(me);function I(e,t,n,r){let o=e.response??{},i={request_id:n,consumer_id:r,stream:!0,kind:"chat",messages:t};return typeof o.instructions=="string"&&(i.instructions=o.instructions),Array.isArray(o.modalities)&&(i.modalities=o.modalities),typeof o.temperature=="number"&&(i.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(i.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(i.tools=o.tools),i}function V(e,t,n){let r={request_id:t,consumer_id:n,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(r.model=e.model),e.tools!==void 0&&(r.tools=e.tools),e.tool_choice!==void 0&&(r.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(r.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(r.max_output_tokens=e.max_output_tokens),typeof e.top_p=="number"&&(r.top_p=e.top_p),Array.isArray(e.stop)&&e.stop.length>0&&(r.stop=e.stop),typeof e.reasoning_effort=="string"&&(r.reasoning_effort=e.reasoning_effort),typeof e.instructions=="string"&&(r.instructions=e.instructions),e.chat_template_kwargs!==void 0&&(r.chat_template_kwargs=e.chat_template_kwargs),e.response_format!==void 0&&(r.response_format=e.response_format),typeof e.voice=="string"&&(r.voice=e.voice),r}function f(e){if(e instanceof Error)return{type:"error",error:{type:"urun_error",code:e.name||null,message:e.message}};if(e&&typeof e=="object"&&e.t==="error"){let t=e,n=t.body??{},r=n.error,o=r?.message??n.message??`unreadable serve error body: ${JSON.stringify(t.body??null)}`;return{type:"error",error:{type:r?.type??"urun_error",code:t.code??r?.code??null,message:o}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}var oe="llm-resp";async function*O(e,t){let n=`${oe}:${t}`,r=`resp_${t}`;yield{type:"response.created",response:{id:r,status:"in_progress"}};let o=new Map;try{for await(let i of e.stream(n).messages()){let a=i;if(a.t==="delta"){if(typeof a.delta=="string"&&(yield{type:"response.output_text.delta",item_id:r,delta:a.delta}),typeof a.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:r,delta:a.reasoning}),Array.isArray(a.tool_calls))for(let c of a.tool_calls){let s=typeof c.index=="number"?c.index:0,d=o.get(s)??{};c.id&&(d.id=c.id),c.function?.name&&(d.name=c.function.name),o.set(s,d),yield{type:"response.function_call_arguments.delta",item_id:`fc_${t}_${s}`,tool_index:s,call_id:d.id,name:d.name,delta:c.function?.arguments??""}}}else if(a.t==="response"){yield{type:"response.completed",response:{id:r,status:"completed",...a.body}};return}else if(a.t==="error"){yield f(a);return}}}finally{try{e.doc("llm").set({requests:{[t]:null}})}catch{}}}var g=class e{constructor(t,n=e.DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS){this.session=t;this.docSyncTimeoutMs=n;this.sessionTag=t.sessionId??globalThis.crypto.randomUUID()}session;docSyncTimeoutMs;sessionTag;static DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS=15e3;get consumerId(){return this.session.consumerId}async ensureDocSynced(){let t=this.session.doc("llm");if(t.synced===!0||typeof t.onSynced!="function")return;let n;await new Promise((o,i)=>{let a=t.onSynced(()=>{clearTimeout(n),o()});n=setTimeout(()=>{a(),i(new Error(`[urun] llm doc plane never synced within ${this.docSyncTimeoutMs}ms \u2014 the request envelope write is withheld rather than raced into a possibly-lost pre-sync write (resumed-attach first-write-lost, prod-usw2 2026-09-23)`))},this.docSyncTimeoutMs)})}write(t){t.session_tag=this.sessionTag,this.session.doc("llm").set({requests:{[t.request_id]:{payload:t,consumer_id:t.consumer_id,stream:t.stream}}})}sendResponses(t,n){return this.writeGated(n,()=>{let r=V(t,n,this.consumerId);return this.write(r),O(this.session,n)})}sendResponseCreate(t,n,r){return this.writeGated(r,()=>{let o=I(t,n,r,this.consumerId);return this.write(o),O(this.session,r)})}async writeGated(t,n){try{await this.ensureDocSynced()}catch(r){throw new Error(`[urun] responses dial ${t} aborted before its envelope write: ${r instanceof Error?r.message:String(r)}`)}return n()}};var U="audio",R=class{constructor(t,n){this.backend=t;this.opts=n}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(t){if(!this.source)throw new Error("startOutbound() not called");let n=Buffer.from(t,"base64"),r=Math.floor(n.byteLength/2),o=new Int16Array(r);for(let i=0;i<r;i++)o[i]=n.readInt16LE(i*2);this.source.onData(o)}async startInbound(t){if(t==null)throw new Error('startInbound: no downstream audio track \u2014 the runtime audio producer has not been consumed yet (session.stream("audio").track is null)');this.sink=this.backend.createSink(t),this.sink.onframe=n=>{let r=Buffer.from(n.samples.buffer,n.samples.byteOffset,n.samples.byteLength).toString("base64");for(let o of this.outHandlers)o(r)}}onOutputAudio(t){return this.outHandlers.add(t),()=>this.outHandlers.delete(t)}};async function P(e,t,n=1e4){let r=new R(t,{sampleRate:24e3}),o=await r.startOutbound();await e.stream(U).attach(o);let i=await se(e,n);return await r.startInbound(i),r}function se(e,t){let n=e.stream(U);return n.track!=null?Promise.resolve(n.track):new Promise((r,o)=>{let i,a=setTimeout(()=>{i?.(),o(new Error(`enableSessionAudio: no downstream audio track within ${t}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},t),c=s=>{s!=null&&(clearTimeout(a),i?.(),r(s))};i=n.on?.("track",c),n.track!=null&&c(n.track)})}var ie=24e3,w=480,ae=111;async function de(){let e=await import("opusscript"),t=e.default??e;return new t(ie,1,2048)}async function D(){let e=await import("werift"),t=await de();return{createSource:()=>{let n=new e.MediaStreamTrack({kind:"audio"}),r=Math.random()*4294967295>>>0,o=Math.random()*65535&65535,i=Math.random()*4294967295>>>0,a=new Int16Array(0);return{track:n,onData:c=>{let s=new Int16Array(a.length+c.length);s.set(a,0),s.set(c,a.length);let d=0;for(;s.length-d>=w;){let u=s.subarray(d,d+w);d+=w;let l=Buffer.from(u.buffer,u.byteOffset,u.byteLength),J=t.encode(l,w);o=o+1&65535,i=i+w>>>0;let K=new e.RtpHeader({version:2,payloadType:ae,sequenceNumber:o,timestamp:i,ssrc:r,marker:!1}),z=new e.RtpPacket(K,J);n.writeRtp(z)}a=s.slice(d)}}},createSink:n=>{let r={onframe:null};return n.onReceiveRtp.subscribe(i=>{let a;try{a=t.decode(i.payload)}catch{return}let c=new Int16Array(a.length/2);for(let s=0;s<c.length;s++)c[s]=a.readInt16LE(s*2);r.onframe?.({samples:c})}),r}}}async function M(){let e=await import("@roamhq/wrtc");return{createSource:()=>{let t=new e.nonstandard.RTCAudioSource;return{track:t.createTrack(),onData:r=>t.onData({samples:r,sampleRate:24e3})}},createSink:t=>{let n={onframe:null},r=new e.nonstandard.RTCAudioSink(t);return r.ondata=o=>{n.onframe?.(o)},n}}}var C=0;function ce(){return C+=1,`rt_${Date.now().toString(36)}_${C.toString(36)}`}var _=class{constructor(t,n={}){this.session=t;this.opts=n;this.transport=new g(t)}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(t=>this.emit({type:"response.audio.delta",delta:t}))}on(t,n){return this.handlers.add(n),()=>this.handlers.delete(n)}emit(t){for(let n of this.handlers)n(t)}send(t){switch(t.type){case"conversation.item.create":{let n=(t.item?.content??[]).map(r=>r.text??"").join("");this.conversation.push({role:t.item?.role??"user",content:n});return}case"response.create":{let n=ce();this.currentRequestId=n,this.active=(async()=>{try{let r=await this.transport.sendResponseCreate({type:"response.create",response:t.response},this.conversation,n);for await(let o of r)this.emit(o)}catch(r){this.emit(f(r))}})();return}case"input_audio_buffer.append":this.audio?.appendInputAudio(t.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 B=0;function ue(){return B+=1,`req_${Date.now().toString(36)}_${B.toString(36)}`}var v=class{transport;constructor(t){this.transport=new g(t)}responses={create:async t=>{let n=ue(),r=await this.transport.sendResponses(t,n);return Object.assign((async function*(){yield*r})(),{requestId:n})}}};async function pe(e){let n=await(e.fetchImpl??fetch)(`${e.catalogUrl}/model_catalog?select=model_id,variant,gpu_spec,engine,task,description,size_gb,hf_repo,engine_args,updated_at`,{headers:{apikey:e.anonKey,Authorization:`Bearer ${e.anonKey}`}});if(!n.ok)throw new Error(`model_catalog fetch failed: ${n.status}`);return await n.json()}async function $(e){return{object:"list",data:(await pe(e)).map(n=>({id:`${n.model_id}:${n.variant}`,object:"model",created:0,owned_by:"urun"}))}}var m=require("@urun-sh/core"),y="rt-video-out",A="urun.videoOut.v1",E="h264";function N(e,t){if(!Number.isInteger(e.seq)||e.seq<0)throw new Error(`${t}: header seq must be a non-negative integer (got ${e.seq})`);if(!Number.isInteger(e.ptsUs))throw new Error(`${t}: header ptsUs must be an integer microsecond timestamp (got ${e.ptsUs})`);if(typeof e.codec!="string"||e.codec.length===0)throw new Error(`${t}: header codec must be a non-empty string`);if(typeof e.key!="boolean")throw new Error(`${t}: header key must be a boolean`)}function W(e,t){if(N(e,"encodeVideoOutRecord"),t.byteLength===0)throw new Error("encodeVideoOutRecord: refusing to encode an empty access unit");let n=new TextEncoder().encode(JSON.stringify({v:1,seq:e.seq,ptsUs:e.ptsUs,codec:e.codec,key:e.key}));if(n.byteLength>65535)throw new Error(`encodeVideoOutRecord: header JSON is ${n.byteLength} bytes \u2014 over the u16 length prefix`);let r=new Uint8Array(2+n.byteLength+t.byteLength);return r[0]=n.byteLength&255,r[1]=n.byteLength>>8,r.set(n,2),r.set(t,2+n.byteLength),r}function b(e){if(e.byteLength<2)throw new Error(`decodeVideoOutRecord: truncated record (${e.byteLength} byte(s), no header length)`);let t=e[0]|e[1]<<8;if(e.byteLength<2+t)throw new Error(`decodeVideoOutRecord: truncated record (header says ${t} bytes, ${e.byteLength-2} present)`);let n;try{n=JSON.parse(new TextDecoder().decode(e.subarray(2,2+t)))}catch(a){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(a)})`)}let r=n;if(r===null||typeof r!="object"||r.v!==1)throw new Error(`decodeVideoOutRecord: header is not spec v1 (v=${JSON.stringify(r?.v)}) \u2014 a newer producer must be met with a newer consumer, never silently misread`);let o=r;N(o,"decodeVideoOutRecord");let i=e.subarray(2+t);if(i.byteLength===0)throw new Error("decodeVideoOutRecord: empty access unit \u2014 a record with no AU bytes is a producer bug");return{header:o,au:i.slice()}}async function H(e){let t=e.wtDataEndpoint;if(typeof t!="function")throw new Error("negotiateSessionVideoOutQuic: the core session exposes no wtDataEndpoint() \u2014 the \xA75-over-QUIC dial seam (urun-infra#1608 TransportOfferResponse.webtransport, offered with wtData: true) is required for the video OUT lane; there is no WS fallback");let n=await t.call(e),r=n.sessionId??e.sessionId;if(typeof r!="string"||r.length===0)throw new Error("negotiateSessionVideoOutQuic: no session id \u2014 the WT hello requires one and neither the negotiated endpoint nor the core session carries it");return{transport:"webtransport",url:n.url,certHashes:n.certHashes,token:n.token,sessionId:r,stream:y,codec:E,framing:A}}var q="urun.videoOut.v2-ws";function F(e){let t=e.stream(y);if(typeof t.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${y}') has no messages() \u2014 the named-DATA stream consume seam (@urun-sh/core SessionStream.messages, contract \xA75) is required for the video OUT lane`);let n=new Set,r=new Set,o=new Set,i=s=>{for(let d of r)d(s)},a=!1,c=async()=>{for await(let s of t.messages()){if(!(s instanceof Uint8Array)){i(new Error(`openSessionVideoOutLane: "${y}" delivered a non-BINARY \xA75 payload (${typeof s}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let d;try{d=b(s)}catch(u){i(u instanceof Error?u:new Error(String(u)));continue}for(let u of n)u(d)}for(let s of o)s()};return{codec:E,onOutputFrame:s=>(n.add(s),a||(a=!0,c().catch(d=>i(d instanceof Error?d:new Error(String(d))))),()=>n.delete(s)),onLaneError:s=>(r.add(s),()=>r.delete(s)),onEnd:s=>(o.add(s),()=>o.delete(s))}}function le(e){if(e.length%2!==0||/[^0-9a-fA-F]/.test(e))throw new Error(`consumeVideoOut: certHashes entry is not hex sha-256 (${JSON.stringify(e)})`);let t=new Uint8Array(e.length/2);for(let n=0;n<t.length;n++)t[n]=parseInt(e.slice(n*2,n*2+2),16);return t}async function j(e,t,n){let r=n??(typeof globalThis.WebTransport=="function"?(d,u)=>new globalThis.WebTransport(d,u):void 0);if(!r)throw new Error("consumeVideoOut: no WebTransport implementation \u2014 this platform has no globalThis.WebTransport; inject a connect factory (the third argument)");let o=r(e.url,{serverCertificateHashes:e.certHashes.map(d=>({algorithm:"sha-256",value:le(d)}))});await o.ready;let i=await o.createBidirectionalStream();await i.writable.getWriter().write((0,m.encodeWtControlFrame)({t:"hello",token:e.token,sessionId:e.sessionId}));let a=i.readable.getReader(),c=new m.WtControlDecoder,s=!1;for(;!s;){let{value:d,done:u}=await a.read();if(u)throw new Error("consumeVideoOut: gateway closed the control stream before hello-ack");if(d)for(let l of c.push(d)){if(l.t==="hello-ack"){s=!0;break}throw new Error(`consumeVideoOut: gateway rejected hello (got ${JSON.stringify(l.t)} before hello-ack)`)}}await(0,m.consumeWtDataStreams)(o.incomingUnidirectionalStreams,{onData:(d,u)=>{if(d===e.stream){if(u===m.STREAM_END){t.onEnd?.();return}if(!(u instanceof Uint8Array)){t.onLaneError(new Error(`consumeVideoOut: "${e.stream}" delivered a non-BINARY \xA75 payload (${typeof u}) \u2014 spec v1 records ride BINARY envelopes only`));return}try{t.onFrame(b(u))}catch(l){t.onLaneError(l instanceof Error?l:new Error(String(l)))}}},onLaneError:d=>t.onLaneError(d)})}var T=["session.update","conversation.item.create","response.create","response.cancel","input_audio_buffer.append","input_audio_buffer.commit","input_audio_buffer.clear"],x=["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 G(e){return T.includes(e)}function Y(e){return x.includes(e)}0&&(module.exports={RT_VIDEO_OUT_STREAM,SUPPORTED_REALTIME_CLIENT_EVENTS,SUPPORTED_RESPONSES_EVENTS,UrunRealtime,UrunResponses,VIDEO_OUT_DEFAULT_CODEC,VIDEO_OUT_FRAMING,VIDEO_OUT_WS_FRAMING,consumeVideoOut,decodeVideoOutRecord,encodeVideoOutRecord,isSupportedClientEvent,isSupportedResponsesEvent,listModels,negotiateSessionVideoOutQuic,nodeAudioBackend,openSessionVideoOutLane,toErrorEvent,weriftAudioBackend});
package/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
- import { b as UrunSessionLike, L as LaneMessage } from './types-lsVTbNcH.cjs';
2
- import { A as AudioBackend } from './video-out-CCksIcj8.cjs';
3
- export { b as AudioSink, c as AudioSource, d as RT_VIDEO_OUT_STREAM, e as VIDEO_OUT_DEFAULT_CODEC, f as VIDEO_OUT_FRAMING, g as VIDEO_OUT_WS_FRAMING, i as VideoOutEndpoint, j as VideoOutLane, k as VideoOutRecord, l as VideoOutRecordHeader, m as VideoOutSink, n as VideoOutWtConnect, o as VideoOutWtSession, p as consumeVideoOut, q as decodeVideoOutRecord, t as encodeVideoOutRecord, u as negotiateSessionVideoOutQuic, v as nodeAudioBackend, w as openSessionVideoOutLane, x as weriftAudioBackend } from './video-out-CCksIcj8.cjs';
4
- export { U as UrunResponses } from './ResponsesClient-DjZWjlFH.cjs';
1
+ import { b as UrunSessionLike, L as LaneMessage } from './types-CHPtJx6d.cjs';
2
+ import { A as AudioBackend } from './video-out-BsuLqlID.cjs';
3
+ export { b as AudioSink, c as AudioSource, d as RT_VIDEO_OUT_STREAM, e as VIDEO_OUT_DEFAULT_CODEC, f as VIDEO_OUT_FRAMING, g as VIDEO_OUT_WS_FRAMING, i as VideoOutEndpoint, j as VideoOutLane, k as VideoOutRecord, l as VideoOutRecordHeader, m as VideoOutSink, n as VideoOutWtConnect, o as VideoOutWtSession, p as consumeVideoOut, q as decodeVideoOutRecord, t as encodeVideoOutRecord, u as negotiateSessionVideoOutQuic, v as nodeAudioBackend, w as openSessionVideoOutLane, x as weriftAudioBackend } from './video-out-BsuLqlID.cjs';
4
+ export { U as UrunResponses } from './ResponsesClient-_OZERUjH.cjs';
5
5
  export { M as ModelList, a as ModelObject, l as listModels } from './models-DUdx_Y6X.cjs';
6
6
  export { RealtimeClientEvent, RealtimeServerEvent } from 'openai/resources/beta/realtime/realtime';
7
7
  export { ResponseStreamEvent } from 'openai/resources/responses/responses';
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { b as UrunSessionLike, L as LaneMessage } from './types-lsVTbNcH.js';
2
- import { A as AudioBackend } from './video-out-IGQ4YQ-c.js';
3
- export { b as AudioSink, c as AudioSource, d as RT_VIDEO_OUT_STREAM, e as VIDEO_OUT_DEFAULT_CODEC, f as VIDEO_OUT_FRAMING, g as VIDEO_OUT_WS_FRAMING, i as VideoOutEndpoint, j as VideoOutLane, k as VideoOutRecord, l as VideoOutRecordHeader, m as VideoOutSink, n as VideoOutWtConnect, o as VideoOutWtSession, p as consumeVideoOut, q as decodeVideoOutRecord, t as encodeVideoOutRecord, u as negotiateSessionVideoOutQuic, v as nodeAudioBackend, w as openSessionVideoOutLane, x as weriftAudioBackend } from './video-out-IGQ4YQ-c.js';
4
- export { U as UrunResponses } from './ResponsesClient-BE3-hx3m.js';
1
+ import { b as UrunSessionLike, L as LaneMessage } from './types-CHPtJx6d.js';
2
+ import { A as AudioBackend } from './video-out-Cawtm7SF.js';
3
+ export { b as AudioSink, c as AudioSource, d as RT_VIDEO_OUT_STREAM, e as VIDEO_OUT_DEFAULT_CODEC, f as VIDEO_OUT_FRAMING, g as VIDEO_OUT_WS_FRAMING, i as VideoOutEndpoint, j as VideoOutLane, k as VideoOutRecord, l as VideoOutRecordHeader, m as VideoOutSink, n as VideoOutWtConnect, o as VideoOutWtSession, p as consumeVideoOut, q as decodeVideoOutRecord, t as encodeVideoOutRecord, u as negotiateSessionVideoOutQuic, v as nodeAudioBackend, w as openSessionVideoOutLane, x as weriftAudioBackend } from './video-out-Cawtm7SF.js';
4
+ export { U as UrunResponses } from './ResponsesClient-CSSrOYD8.js';
5
5
  export { M as ModelList, a as ModelObject, l as listModels } from './models-DUdx_Y6X.js';
6
6
  export { RealtimeClientEvent, RealtimeServerEvent } from 'openai/resources/beta/realtime/realtime';
7
7
  export { ResponseStreamEvent } from 'openai/resources/responses/responses';
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{b as S}from"./chunk-5HKWNK3O.js";import{a as _}from"./chunk-FP4RSAIE.js";import{c as p,g as v,h as f,i as R,j as y,k as O,l as h,m as x,n as g,o as k,p as A,q as T}from"./chunk-BSHT6RZZ.js";import{c as s,e as d}from"./chunk-2T2YYBVX.js";import{c as o}from"./chunk-5CCJUNH7.js";o();o();var u=0;function V(){return u+=1,`rt_${Date.now().toString(36)}_${u.toString(36)}`}var a=class{constructor(e,t={}){this.session=e;this.opts=t;this.transport=new d(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=V();this.currentRequestId=t;let n=this.transport.sendResponseCreate({type:"response.create",response:e.response},this.conversation,t);this.active=(async()=>{try{for await(let i of n)this.emit(i)}catch(i){this.emit(s(i))}})();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}};o();var m=["session.update","conversation.item.create","response.create","response.cancel","input_audio_buffer.append","input_audio_buffer.commit","input_audio_buffer.clear"],E=["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 w(r){return m.includes(r)}function I(r){return E.includes(r)}export{R as RT_VIDEO_OUT_STREAM,m as SUPPORTED_REALTIME_CLIENT_EVENTS,E as SUPPORTED_RESPONSES_EVENTS,a as UrunRealtime,_ as UrunResponses,O as VIDEO_OUT_DEFAULT_CODEC,y as VIDEO_OUT_FRAMING,k as VIDEO_OUT_WS_FRAMING,T as consumeVideoOut,x as decodeVideoOutRecord,h as encodeVideoOutRecord,w as isSupportedClientEvent,I as isSupportedResponsesEvent,S as listModels,g as negotiateSessionVideoOutQuic,f as nodeAudioBackend,A as openSessionVideoOutLane,s as toErrorEvent,v as weriftAudioBackend};
1
+ import{b as S}from"./chunk-K23AZPI4.js";import{a as _}from"./chunk-YXK42SKC.js";import{c as d,g as v,h as f,i as R,j as y,k as O,l as h,m as x,n as g,o as k,p as A,q as T}from"./chunk-6M5JK4YC.js";import{c as i,e as a}from"./chunk-QTVTCMJU.js";import{c as o}from"./chunk-YSFSRI3D.js";o();o();var p=0;function V(){return p+=1,`rt_${Date.now().toString(36)}_${p.toString(36)}`}var s=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 d(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(r=>r.text??"").join("");this.conversation.push({role:e.item?.role??"user",content:t});return}case"response.create":{let t=V();this.currentRequestId=t,this.active=(async()=>{try{let r=await this.transport.sendResponseCreate({type:"response.create",response:e.response},this.conversation,t);for await(let E of r)this.emit(E)}catch(r){this.emit(i(r))}})();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}};o();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 w(n){return l.includes(n)}function I(n){return m.includes(n)}export{R as RT_VIDEO_OUT_STREAM,l as SUPPORTED_REALTIME_CLIENT_EVENTS,m as SUPPORTED_RESPONSES_EVENTS,s as UrunRealtime,_ as UrunResponses,O as VIDEO_OUT_DEFAULT_CODEC,y as VIDEO_OUT_FRAMING,k as VIDEO_OUT_WS_FRAMING,T as consumeVideoOut,x as decodeVideoOutRecord,h as encodeVideoOutRecord,w as isSupportedClientEvent,I as isSupportedResponsesEvent,S as listModels,g as negotiateSessionVideoOutQuic,f as nodeAudioBackend,A as openSessionVideoOutLane,i as toErrorEvent,v as weriftAudioBackend};
@@ -1,8 +1,8 @@
1
- "use strict";var Me=Object.create;var F=Object.defineProperty;var Ue=Object.getOwnPropertyDescriptor;var Oe=Object.getOwnPropertyNames;var Ne=Object.getPrototypeOf,Le=Object.prototype.hasOwnProperty;var De=(e,t)=>{for(var n in t)F(e,n,{get:t[n],enumerable:!0})},se=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of Oe(t))!Le.call(e,r)&&r!==n&&F(e,r,{get:()=>t[r],enumerable:!(o=Ue(t,r))||o.enumerable});return e};var ie=(e,t,n)=>(n=e!=null?Me(Ne(e)):{},se(t||!e||!e.__esModule?F(n,"default",{value:e,enumerable:!0}):n,e)),Fe=e=>se(F({},"__esModule",{value:!0}),e);var mt={};De(mt,{SessionPool:()=>W,URUN_API:()=>ee,applyServeUsage:()=>Pe,coalesceSameRole:()=>$e,createFileDiagnosticSink:()=>be,createUrunExtension:()=>Ae,default:()=>gt,makeSessionFactory:()=>xe,makeStreamSimple:()=>Te,notify:()=>Z,piDiagnosticLogPath:()=>q,resolveSessionEnv:()=>Re,toResponsesInput:()=>Ee});module.exports=Fe(mt);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,m=je();var ve=require("@earendil-works/pi-ai"),M=require("fs"),ke=require("path");function ae(e,t,n,o){let r=e.response??{},l={request_id:n,consumer_id:o,stream:!0,kind:"chat",messages:t};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(e,t,n){let o={request_id:t,consumer_id:n,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(o.model=e.model),e.tools!==void 0&&(o.tools=e.tools),e.tool_choice!==void 0&&(o.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(o.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(o.max_output_tokens=e.max_output_tokens),typeof e.top_p=="number"&&(o.top_p=e.top_p),Array.isArray(e.stop)&&e.stop.length>0&&(o.stop=e.stop),typeof e.reasoning_effort=="string"&&(o.reasoning_effort=e.reasoning_effort),e.chat_template_kwargs!==void 0&&(o.chat_template_kwargs=e.chat_template_kwargs),e.response_format!==void 0&&(o.response_format=e.response_format),o}function ue(e){if(e instanceof Error)return{type:"error",error:{type:"urun_error",code:e.name||null,message:e.message}};if(e&&typeof e=="object"&&e.t==="error"){let t=e,n=t.body??{},o=n.error,r=o?.message??n.message??`unreadable serve error body: ${JSON.stringify(t.body??null)}`;return{type:"error",error:{type:o?.type??"urun_error",code:t.code??o?.code??null,message:r}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}var Ke="llm-resp";async function*Y(e,t){let n=`${Ke}:${t}`,o=`resp_${t}`;yield{type:"response.created",response:{id:o,status:"in_progress"}};let r=new Map;for await(let l of e.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_${t}_${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 Je="llm",j=class{constructor(t){this.session=t;this.sessionTag=t.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(t){t.session_tag=this.sessionTag,this.session.doc(Je).set({requests:{[t.request_id]:{payload:t,consumer_id:t.consumer_id,stream:t.stream}}})}sendResponses(t,n){let o=le(t,n,this.consumerId);return this.write(o),Y(this.session,n)}sendResponseCreate(t,n,o){let r=ae(t,n,o,this.consumerId);return this.write(r),Y(this.session,o)}};var ce=0;function Be(){return ce+=1,`req_${Date.now().toString(36)}_${ce.toString(36)}`}var K=class{transport;constructor(t){this.transport=new j(t)}responses={create:async t=>{let n=Be(),o=this.transport.sendResponses(t,n);return Object.assign((async function*(){yield*o})(),{requestId:n})}}};var J=require("fs"),pe=ie(require("os"),1),B=require("path"),fe=require("util");function q(e=pe.default.homedir()){return(0,B.join)(e,".urun","logs","pi-extension.log")}var de=!1;function ge(e,t){let n=q();try{(0,J.mkdirSync)((0,B.dirname)(n),{recursive:!0}),(0,J.appendFileSync)(n,`${new Date().toISOString()} [console.${e}] [urun] ${(0,fe.format)(...t)}
2
- `)}catch(o){if(!de){de=!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=e=>(...t)=>ge(e,t),X={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:(e,...t)=>{e||ge("assert",t.length>0?t:["Assertion failed"])}};function V(e){try{process.stderr.write(e)}catch{}}var Q="https://api.urun.sh/v1";async function me(e){let t=e.fetchImpl??fetch,n=`${e.apiUrl.replace(/\/+$/,"")}/apps`,o=await t(n,{headers:{Authorization:`Bearer ${e.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(e){let t=await me(e),n=t.filter(o=>o.function_name===e.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 "${e.fnName}" (GET ${e.apiUrl}/apps returned ${t.length} app(s), none servable) \u2014 deploy one with \`urun serve <model>\` first`);return n.sort()}var ee="urun-serve",He="serve",Ge=131072,ze=16384,ye=3,Ye=42e4,Xe=6e4,we=2,Ve=15e3;function Qe(e){let t=(e.URUN_API_KEY??"").trim();if(!t)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:t,apiUrl:(e.URUN_API_URL??"").trim()||Q,fnName:(e.URUN_FUNCTION??"").trim()||He}}function Re(e){let t=(e.URUN_BASE_URL??"").trim(),n=(e.URUN_ORG_ID??"").trim();if(!t)throw new Error("urun pi extension: URUN_BASE_URL is required to open a session");if(/\/v1\/*$/.test(new URL(t).pathname))throw new Error(`urun pi extension: URUN_BASE_URL must be the session-gateway base (e.g. https://api.urun.sh), got ${t} \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=(e.URUN_JWT??"").trim();if(o)return{baseUrl:t,orgId:n,auth:{lane:"jwt",jwt:o}};let r=(e.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=(e.URUN_GATEWAY_URL??"").trim()||void 0;return{baseUrl:t,orgId:n,auth:{lane:"api-key",apiKey:r,gatewayUrl:l}}}function be(e){let t=null;return n=>{t===null&&((0,M.mkdirSync)((0,ke.dirname)(e),{recursive:!0}),t=(0,M.openSync)(e,"a"));let o=n.detail?` ${JSON.stringify(n.detail)}`:"";(0,M.writeSync)(t,`${new Date().toISOString()} [${n.level}] [urun] ${n.message}${o}
4
- `)}}function xe(e){let t=be(q());return async(n,o,r)=>{let{baseUrl:l,orgId:i,auth:u}=Re(n),{App:g,createClientToken:a}=await e(),y=(u.lane==="jwt"?g(o,{baseUrl:l,orgId:i,jwt:u.jwt,diagnosticSink:t}):g(o,{baseUrl:l,orgId:i,diagnosticSink:t,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 _=y();if(typeof _.end!="function")throw new Error(`urun pi extension: app "${o}" function "${r}" returned a session without end()`);let T=_.connect;return typeof T=="function"&&await T.call(_),_}}var Ze=xe(async()=>{let{createRequire:e}=await import("module"),t=e(m);if(typeof globalThis.RTCPeerConnection>"u")try{t.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 t("@urun-sh/core")});function et(e,t){try{return Promise.resolve(e.end()).then(()=>{},n=>t(n))}catch(n){return t(n),Promise.resolve()}}function tt(e){if(e?.hasUI===!1)return!0;if(e?.hasUI===!0)return!1;let t=process.argv.slice(2);if(t.includes("-p")||t.includes("--print"))return!0;let n=t.indexOf("--mode");return n!==-1&&(t[n+1]==="json"||t[n+1]==="rpc")?!0:!process.stdin.isTTY||!process.stdout.isTTY}function Se(e,t){return`uRun: releasing the pooled "${e}" session failed \u2014 the backend session may stay allocated until the platform reclaims it: ${t instanceof Error?t.message:String(t)}`}var W=class{constructor(t,n,o,r=(l,i)=>X.warn(Se(l,i))){this.open=t;this.env=n;this.fnName=o;this.onReleaseError=r}open;env;fnName;onReleaseError;pool=new Map;releasing=new Set;acquire(t){let n=this.pool.get(t);if(!n){let o=Promise.resolve(this.open(this.env,t,this.fnName)).then(r=>({session:r,responses:new K(r)}));n=o,this.pool.set(t,o),o.catch(()=>{this.pool.get(t)===o&&this.pool.delete(t)})}return n}evict(t){let n=this.pool.get(t);if(!n)return Promise.resolve();this.pool.delete(t);let o=n.then(r=>et(r.session,l=>this.onReleaseError(t,l)),()=>{});return this.releasing.add(o),o.then(()=>this.releasing.delete(o)),o}closeAll(){let t=[...this.pool.keys()].map(n=>this.evict(n));return Promise.all([...t,...this.releasing]).then(()=>{})}};function Ee(e){let t=[];e.systemPrompt&&t.push({role:"system",content:e.systemPrompt});for(let n of e.messages){if(n.role==="toolResult"){t.push({type:"function_call_output",call_id:n.toolCallId,output:ot(n.content)});continue}if(n.role==="assistant"&&Array.isArray(n.content)){let r="",l=()=>{r&&(t.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(),t.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";t.push({role:o,content:nt(n.content)})}return $e(t)}function $e(e){let t=[];for(let n of e){let o=t[t.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(`
1
+ "use strict";var Me=Object.create;var F=Object.defineProperty;var Ue=Object.getOwnPropertyDescriptor;var Ne=Object.getOwnPropertyNames;var Le=Object.getPrototypeOf,De=Object.prototype.hasOwnProperty;var Fe=(t,e)=>{for(var n in e)F(t,n,{get:e[n],enumerable:!0})},ie=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Ne(e))!De.call(t,r)&&r!==n&&F(t,r,{get:()=>e[r],enumerable:!(o=Ue(e,r))||o.enumerable});return t};var ae=(t,e,n)=>(n=t!=null?Me(Le(t)):{},ie(e||!t||!t.__esModule?F(n,"default",{value:t,enumerable:!0}):n,t)),je=t=>ie(F({},"__esModule",{value:!0}),t);var ht={};Fe(ht,{SessionPool:()=>G,URUN_API:()=>te,applyServeUsage:()=>$e,coalesceSameRole:()=>Pe,createFileDiagnosticSink:()=>Se,createUrunExtension:()=>Ae,default:()=>mt,makeSessionFactory:()=>xe,makeStreamSimple:()=>Ce,notify:()=>ee,piDiagnosticLogPath:()=>H,resolveSessionEnv:()=>Re,toResponsesInput:()=>Ee});module.exports=je(ht);var Ke=()=>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,m=Ke();var ke=require("@earendil-works/pi-ai"),O=require("fs"),be=require("path");function le(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 ue(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.top_p=="number"&&(o.top_p=t.top_p),Array.isArray(t.stop)&&t.stop.length>0&&(o.stop=t.stop),typeof t.reasoning_effort=="string"&&(o.reasoning_effort=t.reasoning_effort),typeof t.instructions=="string"&&(o.instructions=t.instructions),t.chat_template_kwargs!==void 0&&(o.chat_template_kwargs=t.chat_template_kwargs),t.response_format!==void 0&&(o.response_format=t.response_format),typeof t.voice=="string"&&(o.voice=t.voice),o}function ce(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??{},o=n.error,r=o?.message??n.message??`unreadable serve error body: ${JSON.stringify(e.body??null)}`;return{type:"error",error:{type:o?.type??"urun_error",code:e.code??o?.code??null,message:r}}}return{type:"error",error:{type:"urun_error",code:null,message:String(t)}}}var Je="llm-resp";async function*V(t,e){let n=`${Je}:${e}`,o=`resp_${e}`;yield{type:"response.created",response:{id:o,status:"in_progress"}};let r=new Map;try{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 f=typeof u.index=="number"?u.index:0,a=r.get(f)??{};u.id&&(a.id=u.id),u.function?.name&&(a.name=u.function.name),r.set(f,a),yield{type:"response.function_call_arguments.delta",item_id:`fc_${e}_${f}`,tool_index:f,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 ce(i);return}}}finally{try{t.doc("llm").set({requests:{[e]:null}})}catch{}}}var K=class t{constructor(e,n=t.DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS){this.session=e;this.docSyncTimeoutMs=n;this.sessionTag=e.sessionId??globalThis.crypto.randomUUID()}session;docSyncTimeoutMs;sessionTag;static DEFAULT_DOC_SYNC_GATE_TIMEOUT_MS=15e3;get consumerId(){return this.session.consumerId}async ensureDocSynced(){let e=this.session.doc("llm");if(e.synced===!0||typeof e.onSynced!="function")return;let n;await new Promise((r,l)=>{let i=e.onSynced(()=>{clearTimeout(n),r()});n=setTimeout(()=>{i(),l(new Error(`[urun] llm doc plane never synced within ${this.docSyncTimeoutMs}ms \u2014 the request envelope write is withheld rather than raced into a possibly-lost pre-sync write (resumed-attach first-write-lost, prod-usw2 2026-09-23)`))},this.docSyncTimeoutMs)})}write(e){e.session_tag=this.sessionTag,this.session.doc("llm").set({requests:{[e.request_id]:{payload:e,consumer_id:e.consumer_id,stream:e.stream}}})}sendResponses(e,n){return this.writeGated(n,()=>{let o=ue(e,n,this.consumerId);return this.write(o),V(this.session,n)})}sendResponseCreate(e,n,o){return this.writeGated(o,()=>{let r=le(e,n,o,this.consumerId);return this.write(r),V(this.session,o)})}async writeGated(e,n){try{await this.ensureDocSynced()}catch(o){throw new Error(`[urun] responses dial ${e} aborted before its envelope write: ${o instanceof Error?o.message:String(o)}`)}return n()}};var de=0;function qe(){return de+=1,`req_${Date.now().toString(36)}_${de.toString(36)}`}var J=class{transport;constructor(e){this.transport=new K(e)}responses={create:async e=>{let n=qe(),o=await this.transport.sendResponses(e,n);return Object.assign((async function*(){yield*o})(),{requestId:n})}}};var q=require("fs"),ge=ae(require("os"),1),B=require("path"),fe=require("util");function H(t=ge.default.homedir()){return(0,B.join)(t,".urun","logs","pi-extension.log")}var pe=!1;function me(t,e){let n=H();try{(0,q.mkdirSync)((0,B.dirname)(n),{recursive:!0}),(0,q.appendFileSync)(n,`${new Date().toISOString()} [console.${t}] [urun] ${(0,fe.format)(...e)}
2
+ `)}catch(o){if(!pe){pe=!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 g=t=>(...e)=>me(t,e),X={log:g("log"),info:g("info"),warn:g("warn"),error:g("error"),debug:g("debug"),trace:g("trace"),dir:g("dir"),table:g("table"),group:g("group"),groupCollapsed:g("groupCollapsed"),groupEnd:g("groupEnd"),count:g("count"),countReset:g("countReset"),time:g("time"),timeLog:g("timeLog"),timeEnd:g("timeEnd"),assert:(t,...e)=>{t||me("assert",e.length>0?e:["Assertion failed"])}};function Q(t){try{process.stderr.write(t)}catch{}}var Z="https://api.urun.sh/v1";async function he(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 he(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 te="urun-serve",ze="serve",We=131072,Ye=16384,we=3,Ve=42e4,Xe=6e4,_e=2,Qe=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()||Z,fnName:(t.URUN_FUNCTION??"").trim()||ze}}function Re(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 Se(t){let e=null;return n=>{e===null&&((0,O.mkdirSync)((0,be.dirname)(t),{recursive:!0}),e=(0,O.openSync)(t,"a"));let o=n.detail?` ${JSON.stringify(n.detail)}`:"";(0,O.writeSync)(e,`${new Date().toISOString()} [${n.level}] [urun] ${n.message}${o}
4
+ `)}}function xe(t){let e=Se(H());return async(n,o,r)=>{let{baseUrl:l,orgId:i,auth:u}=Re(n),{App:f,createClientToken:a}=await t(),y=(u.lane==="jwt"?f(o,{baseUrl:l,orgId:i,jwt:u.jwt,diagnosticSink:e}):f(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 _=y();if(typeof _.end!="function")throw new Error(`urun pi extension: app "${o}" function "${r}" returned a session without end()`);let $=_.connect;return typeof $=="function"&&await $.call(_),_}}var et=xe(async()=>{let{createRequire:t}=await import("module"),e=t(m);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 tt(t,e){try{return Promise.resolve(t.end()).then(()=>{},n=>e(n))}catch(n){return e(n),Promise.resolve()}}function nt(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 Te(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 G=class{constructor(e,n,o,r=(l,i)=>X.warn(Te(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 J(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=>tt(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 Ee(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:rt(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:ot(n.content)})}return Pe(e)}function Pe(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
- `):t.push({...n})}return t}function nt(e){return typeof e=="string"?e:Array.isArray(e)?e.map(t=>t&&typeof t=="object"&&"text"in t?String(t.text):"").join(""):""}function ot(e){if(typeof e=="string")return e;if(!Array.isArray(e))return"";let t="";for(let n of e){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`);t+=String(n.text??"")}return t}function rt(e){if(!(!Array.isArray(e)||e.length===0))return e.map(t=>({type:"function",name:t.name,description:t.description,parameters:t.parameters}))}function _e(e,t){if(!t.trim())return{};try{return JSON.parse(t)}catch{throw new Error(`uRun serve error: tool call "${e}" arguments are not valid JSON: ${t}`)}}function st(e){let t=e?.output;return Array.isArray(t)?t.filter(n=>n.type==="function_call"):[]}function Pe(e,t){let n=t?.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||(e.input=o,e.output=r,e.totalTokens=typeof n.total_tokens=="number"?n.total_tokens:o+r)}function it(e){let t=e?.output;if(!Array.isArray(t))return"";let n="";for(let o of t)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 at=["<tool_call>","<function="];function lt(e,t){let n=at.find(o=>t.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 "${e}" (see catalog parser_defaults)`)}var H=class extends Error{};function ut(e){let t=e instanceof Error?e.message:String(e),n=e?.code;return/\b529\b/.test(t)||/overloaded/i.test(t)||n===529||n==="529"}function ct(e){return new Promise(t=>setTimeout(t,e))}function dt(e,t,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())),t),a=()=>u(()=>l(new Error("aborted")));if(o?.aborted){a();return}o?.addEventListener("abort",a,{once:!0}),Promise.resolve(e).then(s=>u(()=>r(s)),s=>u(()=>l(s)))})}async function*pt(e,t){let n=e[Symbol.asyncIterator](),o,r=new Promise((l,i)=>{o=()=>i(new Error("aborted")),t.addEventListener("abort",o,{once:!0})});try{for(;;){if(t.aborted)throw new Error("aborted");let l=await Promise.race([n.next(),r]);if(l.done)return;yield l.value}}finally{o&&t.removeEventListener("abort",o),n.return?.(void 0)}}function Z(e,t){if(e?.hasUI===!1){V(`${t}
7
- `);return}let n=e?.sendMessage;if(typeof n=="function")try{n.call(e,{customType:"urun-status",content:t,display:!0},{triggerTurn:!1})}catch{}}function Te(e,t={},n){let o=t.connectDeadlineMs??Ye,r=t.stallTimeoutMs??Xe,l=t.phaseHeartbeatMs??Ve;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,_=g?.signal,T=!1,L=()=>{T=!0,y.abort()};_&&(_.aborted?L():_.addEventListener("abort",L,{once:!0}));let U=!1,A,te=()=>{A&&clearTimeout(A),A=setTimeout(()=>{U=!0,y.abort()},r)},D=()=>{A&&clearTimeout(A),A=void 0},x=null,S=null,v=null,ne="",O=!1,G=()=>{if(!S)return;let p=S;S=null,a.push({type:"thinking_end",contentIndex:s.content.indexOf(p),content:p.thinking,partial:s})},oe=()=>{if(!x)return;let p=x;x=null,a.push({type:"text_end",contentIndex:s.content.indexOf(p),content:p.text,partial:s})},z=()=>{if(!v)return;let{call:p,args:k,contentIndex:E}=v;v=null,p.arguments=_e(p.name,k),a.push({type:"toolcall_end",contentIndex:E,toolCall:p,partial:s})},Ce=p=>{let k={type:"toolCall",id:String(p.call_id??p.id??`call_${s.content.length}`),name:String(p.name??""),arguments:{}};s.content.push(k);let E=s.content.length-1;a.push({type:"toolcall_start",contentIndex:E,partial:s});let N=typeof p.arguments=="string"?p.arguments:"";N&&a.push({type:"toolcall_delta",contentIndex:E,delta:N,partial:s}),k.arguments=_e(k.name,N),a.push({type:"toolcall_end",contentIndex:E,toolCall:k,partial:s})};return(async()=>{a.push({type:"start",partial:s});let p=!1,k,E=()=>{k&&clearInterval(k),k=void 0},N=Date.now();k=setInterval(()=>{let R=Math.round((Date.now()-N)/1e3);Z(n,`uRun: still opening the "${i.id}" session\u2026 (${R}s \u2014 serve apps scale to zero, first turn can cold-start ~6min)`)},l);let Ie=async R=>{let{responses:b}=await dt(e.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);E();let C,$;for(let c=1;c<=ye;c++)try{let w=await b.responses.create({model:i.id,input:R,stream:!0,tools:rt(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 P of pt(w,y.signal)){te();let h=P;if(h.type==="response.output_text.delta"){let d=typeof h.delta=="string"?h.delta:"";d&&(O=!0,G(),z(),x||(x={type:"text",text:""},s.content.push(x),a.push({type:"text_start",contentIndex:s.content.length-1,partial:s})),x.text+=d,ne+=d,a.push({type:"text_delta",contentIndex:s.content.indexOf(x),delta:d,partial:s}))}else if(h.type==="response.reasoning_text.delta"){let d=typeof h.delta=="string"?h.delta:"";d&&(O=!0,S||(S={type:"thinking",thinking:""},s.content.push(S),a.push({type:"thinking_start",contentIndex:s.content.length-1,partial:s})),S.thinking+=d,a.push({type:"thinking_delta",contentIndex:s.content.indexOf(S),delta:d,partial:s}))}else if(h.type==="response.function_call_arguments.delta"&&typeof h.tool_index=="number"){let d=h;if(O=!0,G(),oe(),!v||v.toolIndex!==d.tool_index){z();let re={type:"toolCall",id:d.call_id??`call_${d.tool_index}`,name:d.name??"",arguments:{}};s.content.push(re),v={call:re,args:"",contentIndex:s.content.length-1,toolIndex:d.tool_index},a.push({type:"toolcall_start",contentIndex:v.contentIndex,partial:s})}d.call_id&&(v.call.id=d.call_id),d.name&&(v.call.name=d.name);let I=typeof d.delta=="string"?d.delta:"";I&&(v.args+=I,a.push({type:"toolcall_delta",contentIndex:v.contentIndex,delta:I,partial:s}))}else if(h.type==="response.completed"){C=h.response;break}else if(h.type==="error"){let d=h.error,I=new Error(`uRun serve error: ${d?.message??"unknown error"}`);throw I.code=d?.code??null,I}}if(!C)throw new H(`uRun backhaul for "${i.id}" closed before the turn completed \u2014 the serving session ended mid-turn (its pod was restarted, drained or deleted).`);$=void 0;break}catch(w){if($=w,y.signal.aborted)throw w;if(c<ye&&ut(w)&&!O){await ct(c*500);continue}throw w}if($)throw $;return C};try{let R=Ee(u),b;for(let c=1;c<=we;c++)try{b=await Ie(R);break}catch(w){let P=U||w instanceof H;if(c>=we||!P||O||T)throw w;D(),e.evict(i.id),U=!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(),G(),oe(),z(),!s.content.some(c=>c.type==="toolCall"))for(let c of st(b))Ce(c);if(!s.content.some(c=>c.type==="thinking")){let c=it(b);if(c){let w={type:"thinking",thinking:""};s.content.push(w);let P=s.content.length-1;a.push({type:"thinking_start",contentIndex:P,partial:s}),w.thinking=c,a.push({type:"thinking_delta",contentIndex:P,delta:c,partial:s}),a.push({type:"thinking_end",contentIndex:P,content:c,partial:s})}}let C=s.content.filter(c=>c.type==="toolCall").length;if((u.tools?.length??0)>0&&C===0){let c=lt(i.id,ne);if(c)throw c}let $=C>0?"toolUse":b?.status==="incomplete"?"length":"stop";s.stopReason=$,Pe(s.usage,b),a.push({type:"done",reason:$,message:s}),a.end(s)}catch(R){if(p=!0,D(),T||_?.aborted&&!U)s.stopReason="aborted",s.errorMessage="aborted by user",a.push({type:"error",reason:"aborted",error:s});else{let b=U?`uRun stream stalled \u2014 no output for ${Math.round(r/1e3)}s; the model may be hung. Retry, or Ctrl-C to abort.`:R instanceof Error?R.message:String(R);s.stopReason="error",s.errorMessage=b,a.push({type:"error",reason:"error",error:s})}a.end(s)}finally{D(),E(),_&&_.removeEventListener("abort",L),p&&e.evict(i.id)}})(),a}}function ft(e){return{id:e,name:`uRun ${e}`,api:ee,reasoning:!0,input:["text"],cost:{input:0,output:0,cacheRead:0,cacheWrite:0},contextWindow:Ge,maxTokens:ze}}function Ae(e={}){return async t=>{let n=e.env??process.env,{apiKey:o,apiUrl:r,fnName:l}=Qe(n),i=await he({apiUrl:r,apiKey:o,fnName:l,fetchImpl:e.fetchImpl}),u=new W(e.openSession??Ze,n,l,(a,s)=>{let y=Se(a,s);tt(t)?V(`${y}
8
- `):X.warn(y)});t.on("session_shutdown",()=>u.closeAll());let g={name:"uRun",api:ee,baseUrl:"urun://session",apiKey:"$URUN_API_KEY",models:i.map(ft),streamSimple:Te(u,e.timing??{},t)};t.registerProvider("urun",g)}}var gt=Ae();0&&(module.exports={SessionPool,URUN_API,applyServeUsage,coalesceSameRole,createFileDiagnosticSink,createUrunExtension,makeSessionFactory,makeStreamSimple,notify,piDiagnosticLogPath,resolveSessionEnv,toResponsesInput});
6
+ `):e.push({...n})}return e}function ot(t){return typeof t=="string"?t:Array.isArray(t)?t.map(e=>e&&typeof e=="object"&&"text"in e?String(e.text):"").join(""):""}function rt(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 st(t){if(!(!Array.isArray(t)||t.length===0))return t.map(e=>({type:"function",name:e.name,description:e.description,parameters:e.parameters}))}function ve(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 it(t){let e=t?.output;return Array.isArray(e)?e.filter(n=>n.type==="function_call"):[]}function $e(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 at(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 lt=["<tool_call>","<function="];function ut(t,e){let n=lt.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 z=class extends Error{};function ct(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 dt(t){return new Promise(e=>setTimeout(e,t))}function pt(t,e,n,o){return new Promise((r,l)=>{let i=!1,u=s=>{i||(i=!0,clearTimeout(f),o?.removeEventListener("abort",a),s())},f=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*gt(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 ee(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 Ce(t,e={},n){let o=e.connectDeadlineMs??Ve,r=e.stallTimeoutMs??Xe,l=e.phaseHeartbeatMs??Qe;return(i,u,f)=>{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()},y=new AbortController,_=f?.signal,$=!1,L=()=>{$=!0,y.abort()};_&&(_.aborted?L():_.addEventListener("abort",L,{once:!0}));let M=!1,C,ne=()=>{C&&clearTimeout(C),C=setTimeout(()=>{M=!0,y.abort()},r)},D=()=>{C&&clearTimeout(C),C=void 0},S=null,x=null,v=null,oe="",U=!1,W=()=>{if(!x)return;let p=x;x=null,a.push({type:"thinking_end",contentIndex:s.content.indexOf(p),content:p.thinking,partial:s})},re=()=>{if(!S)return;let p=S;S=null,a.push({type:"text_end",contentIndex:s.content.indexOf(p),content:p.text,partial:s})},Y=()=>{if(!v)return;let{call:p,args:k,contentIndex:T}=v;v=null,p.arguments=ve(p.name,k),a.push({type:"toolcall_end",contentIndex:T,toolCall:p,partial:s})},Ie=p=>{let k={type:"toolCall",id:String(p.call_id??p.id??`call_${s.content.length}`),name:String(p.name??""),arguments:{}};s.content.push(k);let T=s.content.length-1;a.push({type:"toolcall_start",contentIndex:T,partial:s});let N=typeof p.arguments=="string"?p.arguments:"";N&&a.push({type:"toolcall_delta",contentIndex:T,delta:N,partial:s}),k.arguments=ve(k.name,N),a.push({type:"toolcall_end",contentIndex:T,toolCall:k,partial:s})};return(async()=>{a.push({type:"start",partial:s});let p=!1,k,T=()=>{k&&clearInterval(k),k=void 0},N=Date.now();k=setInterval(()=>{let b=Math.round((Date.now()-N)/1e3);ee(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 Oe=async b=>{let{responses:R}=await pt(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);T();let A,E;for(let c=1;c<=we;c++)try{let w=await R.responses.create({model:i.id,input:b,stream:!0,tools:st(u.tools),max_output_tokens:i.maxTokens,...f?.reasoning!=null&&f.reasoning!=="off"&&i.reasoning!==!1?{}:{chat_template_kwargs:{enable_thinking:!1}}});ne();for await(let P of gt(w,y.signal)){ne();let h=P;if(h.type==="response.output_text.delta"){let d=typeof h.delta=="string"?h.delta:"";d&&(U=!0,W(),Y(),S||(S={type:"text",text:""},s.content.push(S),a.push({type:"text_start",contentIndex:s.content.length-1,partial:s})),S.text+=d,oe+=d,a.push({type:"text_delta",contentIndex:s.content.indexOf(S),delta:d,partial:s}))}else if(h.type==="response.reasoning_text.delta"){let d=typeof h.delta=="string"?h.delta:"";d&&(U=!0,x||(x={type:"thinking",thinking:""},s.content.push(x),a.push({type:"thinking_start",contentIndex:s.content.length-1,partial:s})),x.thinking+=d,a.push({type:"thinking_delta",contentIndex:s.content.indexOf(x),delta:d,partial:s}))}else if(h.type==="response.function_call_arguments.delta"&&typeof h.tool_index=="number"){let d=h;if(U=!0,W(),re(),!v||v.toolIndex!==d.tool_index){Y();let se={type:"toolCall",id:d.call_id??`call_${d.tool_index}`,name:d.name??"",arguments:{}};s.content.push(se),v={call:se,args:"",contentIndex:s.content.length-1,toolIndex:d.tool_index},a.push({type:"toolcall_start",contentIndex:v.contentIndex,partial:s})}d.call_id&&(v.call.id=d.call_id),d.name&&(v.call.name=d.name);let I=typeof d.delta=="string"?d.delta:"";I&&(v.args+=I,a.push({type:"toolcall_delta",contentIndex:v.contentIndex,delta:I,partial:s}))}else if(h.type==="response.completed"){A=h.response;break}else if(h.type==="error"){let d=h.error,I=new Error(`uRun serve error: ${d?.message??"unknown error"}`);throw I.code=d?.code??null,I}}if(!A)throw new z(`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(w){if(E=w,y.signal.aborted)throw w;if(c<we&&ct(w)&&!U){await dt(c*500);continue}throw w}if(E)throw E;return A};try{let b=Ee(u),R;for(let c=1;c<=_e;c++)try{R=await Oe(b);break}catch(w){let P=M||w instanceof z;if(c>=_e||!P||U||$)throw w;D(),t.evict(i.id),M=!1,y=new AbortController,ee(n,`uRun: the "${i.id}" session ended mid-turn \u2014 re-homing to a fresh assignment and replaying the turn\u2026`)}if(D(),W(),re(),Y(),!s.content.some(c=>c.type==="toolCall"))for(let c of it(R))Ie(c);if(!s.content.some(c=>c.type==="thinking")){let c=at(R);if(c){let w={type:"thinking",thinking:""};s.content.push(w);let P=s.content.length-1;a.push({type:"thinking_start",contentIndex:P,partial:s}),w.thinking=c,a.push({type:"thinking_delta",contentIndex:P,delta:c,partial:s}),a.push({type:"thinking_end",contentIndex:P,content:c,partial:s})}}let A=s.content.filter(c=>c.type==="toolCall").length;if((u.tools?.length??0)>0&&A===0){let c=ut(i.id,oe);if(c)throw c}let E=A>0?"toolUse":R?.status==="incomplete"?"length":"stop";s.stopReason=E,$e(s.usage,R),a.push({type:"done",reason:E,message:s}),a.end(s)}catch(b){if(p=!0,D(),$||_?.aborted&&!M)s.stopReason="aborted",s.errorMessage="aborted by user",a.push({type:"error",reason:"aborted",error:s});else{let R=M?`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=R,a.push({type:"error",reason:"error",error:s})}a.end(s)}finally{D(),T(),_&&_.removeEventListener("abort",L),p&&t.evict(i.id)}})(),a}}function ft(t){return{id:t,name:`uRun ${t}`,api:te,reasoning:!0,input:["text"],cost:{input:0,output:0,cacheRead:0,cacheWrite:0},contextWindow:We,maxTokens:Ye}}function Ae(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 G(t.openSession??et,n,l,(a,s)=>{let y=Te(a,s);nt(e)?Q(`${y}
8
+ `):X.warn(y)});e.on("session_shutdown",()=>u.closeAll());let f={name:"uRun",api:te,baseUrl:"urun://session",apiKey:"$URUN_API_KEY",models:i.map(ft),streamSimple:Ce(u,t.timing??{},e)};e.registerProvider("urun",f)}}var mt=Ae();0&&(module.exports={SessionPool,URUN_API,applyServeUsage,coalesceSameRole,createFileDiagnosticSink,createUrunExtension,makeSessionFactory,makeStreamSimple,notify,piDiagnosticLogPath,resolveSessionEnv,toResponsesInput});
@@ -1,7 +1,7 @@
1
1
  import { ExtensionAPI } from '@earendil-works/pi-coding-agent';
2
2
  import { Api, AssistantMessage, Model, Context, SimpleStreamOptions, AssistantMessageEventStream } from '@earendil-works/pi-ai';
3
- import { U as UrunResponses } from '../ResponsesClient-DjZWjlFH.cjs';
4
- import { b as UrunSessionLike } from '../types-lsVTbNcH.cjs';
3
+ import { U as UrunResponses } from '../ResponsesClient-_OZERUjH.cjs';
4
+ import { b as UrunSessionLike } from '../types-CHPtJx6d.cjs';
5
5
 
6
6
  /** Where the pi lane's quiet diagnostics land (the compat log convention). */
7
7
  declare function piDiagnosticLogPath(home?: string): string;
@@ -1,7 +1,7 @@
1
1
  import { ExtensionAPI } from '@earendil-works/pi-coding-agent';
2
2
  import { Api, AssistantMessage, Model, Context, SimpleStreamOptions, AssistantMessageEventStream } from '@earendil-works/pi-ai';
3
- import { U as UrunResponses } from '../ResponsesClient-BE3-hx3m.js';
4
- import { b as UrunSessionLike } from '../types-lsVTbNcH.js';
3
+ import { U as UrunResponses } from '../ResponsesClient-CSSrOYD8.js';
4
+ import { b as UrunSessionLike } from '../types-CHPtJx6d.js';
5
5
 
6
6
  declare function piDiagnosticLogPath(home?: string): string;
7
7
 
@@ -1,4 +1,4 @@
1
- import{a as z}from"../chunk-FP4RSAIE.js";import{c as W,f as Q}from"../chunk-3WAJD62J.js";import"../chunk-2T2YYBVX.js";import{c as C}from"../chunk-5CCJUNH7.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)}
1
+ import{a as z}from"../chunk-YXK42SKC.js";import{c as W,f as Q}from"../chunk-WCMX5VOF.js";import"../chunk-QTVTCMJU.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
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
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
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(`