@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,6 +1,6 @@
1
1
  import { C as CatalogRow } from './models-DUdx_Y6X.js';
2
2
  import { IncomingMessage } from 'node:http';
3
- import { a as AudioBridge, h as VideoFrameLane, j as VideoOutLane } from './video-out-IGQ4YQ-c.js';
3
+ import { a as AudioBridge, h as VideoFrameLane, j as VideoOutLane } from './video-out-Cawtm7SF.js';
4
4
 
5
5
  interface ProxyIdentity {
6
6
  app: string;
@@ -74,11 +74,26 @@ interface SharedEndpointRow {
74
74
  warm?: number | null;
75
75
 
76
76
  compat_default?: boolean | null;
77
+
78
+ output_free?: boolean | null;
79
+
80
+ ready?: boolean | null;
81
+
82
+ ready_runtimes?: number | null;
83
+
84
+ ready_since?: string | null;
77
85
  }
78
86
 
79
87
  interface SharedCatalogBlock {
80
88
  shared_org_id: string;
81
89
  endpoints: SharedEndpointRow[];
90
+
91
+ regions?: string[] | null;
92
+
93
+ compliance?: {
94
+ zdr: boolean;
95
+ hipaa: boolean;
96
+ } | null;
82
97
  }
83
98
 
84
99
  interface TokenPricing {
@@ -104,6 +119,16 @@ interface ModelPriceRow {
104
119
  type OpenRouterProviderDoc = {
105
120
  data: OpenRouterProviderModel[];
106
121
  };
122
+
123
+ interface OpenRouterDatacenter {
124
+ country_code: string;
125
+ region?: string;
126
+ }
127
+
128
+ interface OpenRouterCompliance {
129
+ zdr: boolean;
130
+ hipaa: boolean;
131
+ }
107
132
  interface OpenRouterProviderModel {
108
133
  schema_version: '2.4';
109
134
 
@@ -112,17 +137,36 @@ interface OpenRouterProviderModel {
112
137
  created: number;
113
138
 
114
139
  quantization: string | null;
140
+
141
+ tokenizer?: string;
115
142
  description: string;
116
143
  hugging_face_id: string;
144
+
145
+ is_ready: boolean;
146
+
147
+ is_free: boolean;
117
148
  input_modalities: Array<Record<string, unknown>>;
118
149
  output_modalities: Array<Record<string, unknown>>;
150
+
151
+ datacenters?: OpenRouterDatacenter[];
152
+
153
+ compliance?: OpenRouterCompliance;
154
+ }
155
+
156
+ interface SpeechVoicesResolution {
157
+
158
+ readonly published: readonly string[];
159
+
160
+ readonly defect: string | null;
119
161
  }
120
162
 
121
163
  interface DeployedApp {
122
164
  app_slug: string;
165
+
123
166
  function_name?: string | null;
167
+
168
+ serves?: string | null;
124
169
  deployment_status?: string | null;
125
- [k: string]: unknown;
126
170
  }
127
171
 
128
172
  declare class UnknownModelError extends Error {
@@ -152,6 +196,8 @@ interface RouterModelEntry {
152
196
  pricing?: TokenPricing;
153
197
 
154
198
  warm?: number;
199
+
200
+ voices?: string[];
155
201
  }
156
202
  interface RouterModelList {
157
203
  object: 'list';
@@ -176,6 +222,8 @@ interface RouteTarget {
176
222
  modelId?: string;
177
223
 
178
224
  variant?: string;
225
+
226
+ sharedOrgId?: string;
179
227
  }
180
228
  interface ModelRouterOptions<S> {
181
229
 
@@ -183,7 +231,7 @@ interface ModelRouterOptions<S> {
183
231
 
184
232
  fnName: string;
185
233
 
186
- openSession: (target: RouteTarget) => S | Promise<S>;
234
+ openSession: (target: RouteTarget, poolKey: string) => S | Promise<S>;
187
235
 
188
236
  closeSession: (entry: S) => Promise<void>;
189
237
 
@@ -200,6 +248,8 @@ interface ModelRouterOptions<S> {
200
248
  sessionKey?: (entry: S) => string;
201
249
 
202
250
  shared?: (() => Promise<SharedCatalogBlock | null>) | null;
251
+
252
+ sharedOnlyListing?: boolean;
203
253
  }
204
254
 
205
255
  type ImageCapabilityMode = 'generation' | 'edit';
@@ -224,6 +274,8 @@ declare class ModelRouter<S> {
224
274
 
225
275
  private pricesForListing;
226
276
 
277
+ private outputFreeKeys;
278
+
227
279
  private warn;
228
280
 
229
281
  private servable;
@@ -240,6 +292,8 @@ declare class ModelRouter<S> {
240
292
 
241
293
  servingAppSlugFor(model: string | undefined): Promise<string>;
242
294
 
295
+ targetFor(model: string | undefined): Promise<RouteTarget>;
296
+
243
297
  resolveApp(model: string | undefined): Promise<string>;
244
298
 
245
299
  sessionFor(model: string | undefined): Promise<{
@@ -247,6 +301,14 @@ declare class ModelRouter<S> {
247
301
  entry: S;
248
302
  identity: TurnCatalogIdentity;
249
303
  }>;
304
+
305
+ speechSessionFor(model: string | undefined): Promise<{
306
+ app: string;
307
+ entry: S;
308
+ identity: TurnCatalogIdentity;
309
+ }>;
310
+
311
+ private acquire;
250
312
  private keyOf;
251
313
 
252
314
  handleFor(model: string | undefined): Promise<{
@@ -254,6 +316,11 @@ declare class ModelRouter<S> {
254
316
  handle: string;
255
317
  }>;
256
318
 
319
+ speechHandleFor(model: string | undefined): Promise<{
320
+ app: string;
321
+ handle: string;
322
+ }>;
323
+
257
324
  sessionForHandle(handle: string): Promise<{
258
325
  app: string;
259
326
  entry: S;
@@ -269,11 +336,82 @@ declare class ModelRouter<S> {
269
336
 
270
337
  supportsAudio(model: string | undefined): Promise<boolean>;
271
338
 
339
+ supportsSpeech(model: string | undefined): Promise<boolean>;
340
+
341
+ speechVoices(model: string | undefined): Promise<SpeechVoicesResolution>;
342
+
272
343
  imageModelList(): Promise<RouterImageModelList>;
273
344
 
274
345
  closeAll(): Promise<void>;
275
346
  }
276
347
 
348
+ interface RuntimePresenceRow {
349
+ runtime_id: string;
350
+
351
+ address: string;
352
+ ready: boolean;
353
+
354
+ last_seen: string;
355
+ status?: string | null;
356
+ }
357
+
358
+ interface DialTarget {
359
+ appSlug: string;
360
+ fnName?: string;
361
+ sharedApp?: boolean;
362
+ modelId?: string;
363
+ variant?: string;
364
+ sharedOrgId?: string;
365
+ }
366
+ interface StatelessDecisionDialOptions {
367
+
368
+ targetFor: (model: string | undefined) => Promise<DialTarget>;
369
+ apiUrl: string;
370
+ apiKey: string;
371
+
372
+ orgId: string;
373
+
374
+ fnName: string;
375
+
376
+ secret: string;
377
+
378
+ shared?: {
379
+ apiKey: string;
380
+ };
381
+ fetchImpl?: typeof fetch;
382
+ nowMs?: () => number;
383
+
384
+ timeoutMs?: number;
385
+ }
386
+
387
+ declare class StatelessDecisionDial {
388
+ private readonly opts;
389
+ private readonly replicas;
390
+ private readonly nowMs;
391
+
392
+ private readonly presenceCache;
393
+ constructor(opts: StatelessDecisionDialOptions);
394
+
395
+ get enabled(): boolean;
396
+
397
+ targetFor(model: string | undefined): Promise<DialTarget | null>;
398
+
399
+ decide(body: Record<string, unknown>, model: string | undefined): Promise<{
400
+ status: number;
401
+ body: unknown;
402
+ completed: unknown;
403
+ target: DialTarget & {
404
+ fnName: string;
405
+ };
406
+ }>;
407
+
408
+ private ensureRows;
409
+ private readPresence;
410
+
411
+ pick(rows: RuntimePresenceRow[]): string | null;
412
+ private post;
413
+ }
414
+
277
415
  interface InferenceUsageRecord {
278
416
 
279
417
  inference_id: string;
@@ -338,6 +476,10 @@ interface LedgerWrite {
338
476
  prefill_ms: number | null;
339
477
  decode_ms: number | null;
340
478
  total_ms: number | null;
479
+
480
+ ttft_ms: number | null;
481
+
482
+ e2e_ms: number | null;
341
483
  }
342
484
 
343
485
  interface LedgerRecorded {
@@ -360,16 +502,19 @@ interface LedgerFailure {
360
502
  declare const LEDGER_SKIP_REASONS: readonly ["no_response_head", "no_org", "model_unresolved", "no_model_named", "queue_full"];
361
503
  type LedgerSkipReason = (typeof LEDGER_SKIP_REASONS)[number];
362
504
 
505
+ declare const LEDGER_PRICING_STATES: readonly ["priced", "unpriced"];
506
+ type LedgerPricingState = (typeof LEDGER_PRICING_STATES)[number];
507
+
363
508
  type LedgerOutcome =
364
509
 
365
510
  {
366
511
  outcome: 'written';
367
- reason: 'priced' | 'unpriced';
512
+ reason: LedgerPricingState;
368
513
  }
369
514
 
370
515
  | {
371
516
  outcome: 'already_written';
372
- reason: 'priced' | 'unpriced';
517
+ reason: LedgerPricingState;
373
518
  }
374
519
 
375
520
  | {
@@ -408,6 +553,8 @@ interface ResponsesCreateParams {
408
553
  chat_template_kwargs?: Record<string, unknown>;
409
554
 
410
555
  response_format?: unknown;
556
+
557
+ voice?: string;
411
558
  }
412
559
 
413
560
  interface SessionEndInfo {
@@ -431,6 +578,10 @@ interface ProxyClients {
431
578
 
432
579
  supportsAudio?(model: string | undefined): Promise<boolean>;
433
580
 
581
+ supportsSpeech?(model: string | undefined): Promise<boolean>;
582
+
583
+ speechVoices?(model: string | undefined): Promise<SpeechVoicesResolution>;
584
+
434
585
  listImageModels?(): Promise<unknown>;
435
586
 
436
587
  catalogIdentity(model: string | undefined): Promise<{
@@ -440,7 +591,9 @@ interface ProxyClients {
440
591
 
441
592
  servingAppSlug?(model: string | undefined): Promise<string | null>;
442
593
 
443
- openAudio?(model: string | undefined): Promise<ProxyAudioLane>;
594
+ openAudio?(model: string | undefined, signal?: AbortSignal): Promise<ProxyAudioLane>;
595
+
596
+ openAudioOn?(handle: string): Promise<PinnedAudioLane>;
444
597
 
445
598
  openVideo?(model: string | undefined): Promise<ProxyVideoLane>;
446
599
 
@@ -448,12 +601,25 @@ interface ProxyClients {
448
601
 
449
602
  sessionHandle(model: string | undefined): Promise<string>;
450
603
 
604
+ speechSessionHandle?(model: string | undefined): Promise<string>;
605
+
606
+ evictSpeechSession?(handle: string): Promise<void>;
607
+
451
608
  createResponseOn(handle: string, params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
452
609
 
453
610
  onSessionEnd(handle: string, cb: (end: SessionEndInfo) => void): Promise<() => void>;
611
+
612
+ readonly stateless?: StatelessDecisionDial;
454
613
  }
455
614
 
456
- type ProxyAudioLane = Pick<AudioBridge, 'appendInputAudio' | 'onOutputAudio'>;
615
+ type ProxyAudioLane = Pick<AudioBridge, 'appendInputAudio' | 'onOutputAudio'> & {
616
+ sessionId?: string | null;
617
+ };
618
+
619
+ interface PinnedAudioLane extends ProxyAudioLane {
620
+
621
+ readonly sessionId: string | null;
622
+ }
457
623
 
458
624
  type ProxyVideoLane = Pick<VideoFrameLane, 'sendInputFrame'>;
459
625
 
@@ -1,4 +1,4 @@
1
- import { k as VideoOutRecord } from './video-out-CCksIcj8.cjs';
1
+ import { k as VideoOutRecord } from './video-out-BsuLqlID.cjs';
2
2
 
3
3
  /**
4
4
  * Gemini Live (BidiGenerateContent) ⇄ uRun Responses translation — PURE
@@ -1,4 +1,4 @@
1
- import { k as VideoOutRecord } from './video-out-IGQ4YQ-c.js';
1
+ import { k as VideoOutRecord } from './video-out-Cawtm7SF.js';
2
2
 
3
3
  declare const GEMINI_LIVE_PATH = "/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent";
4
4
 
@@ -3,6 +3,19 @@ interface UrunDocLike {
3
3
  set(patch: Record<string, unknown>): void;
4
4
  get(path?: string, defaultValue?: unknown): unknown;
5
5
  on(event: 'change', handler: (snapshot: Record<string, unknown>) => void): () => void;
6
+ /**
7
+ * Whether the provider's initial sync has completed (core
8
+ * `SessionDocumentImpl.synced`) — true also for local-only docs. Absent on
9
+ * legacy/test doubles that never model a doc plane; the turn path's
10
+ * first-write gate skips when the surface is absent (it cannot hold a gate
11
+ * it cannot observe).
12
+ */
13
+ synced?: boolean;
14
+ /**
15
+ * One-shot first-sync callback (core `SessionDocumentImpl.onSynced`).
16
+ * Returns an unsubscribe fn. Absent on legacy/test doubles.
17
+ */
18
+ onSynced?(handler: () => void): () => void;
6
19
  }
7
20
  interface UrunStreamLike {
8
21
  messages(): AsyncIterable<unknown>;
@@ -3,6 +3,10 @@ interface UrunDocLike {
3
3
  set(patch: Record<string, unknown>): void;
4
4
  get(path?: string, defaultValue?: unknown): unknown;
5
5
  on(event: 'change', handler: (snapshot: Record<string, unknown>) => void): () => void;
6
+
7
+ synced?: boolean;
8
+
9
+ onSynced?(handler: () => void): () => void;
6
10
  }
7
11
  interface UrunStreamLike {
8
12
  messages(): AsyncIterable<unknown>;
@@ -1,4 +1,4 @@
1
- import { b as UrunSessionLike } from './types-lsVTbNcH.cjs';
1
+ import { b as UrunSessionLike } from './types-CHPtJx6d.cjs';
2
2
 
3
3
  /**
4
4
  * The ONE native audio sample rate of the session transport: the SFU Opus leg
@@ -1,4 +1,4 @@
1
- import { b as UrunSessionLike } from './types-lsVTbNcH.js';
1
+ import { b as UrunSessionLike } from './types-CHPtJx6d.js';
2
2
 
3
3
  declare const NATIVE_AUDIO_SAMPLE_RATE = 24000;
4
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urun-sh/openai",
3
- "version": "0.5.6",
3
+ "version": "0.6.0",
4
4
  "description": "OpenAI-compatible Realtime + Responses SDK over uRun session primitives (not websockets).",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -70,7 +70,7 @@
70
70
  "build:bin": "bun build --compile --external @roamhq/wrtc src/proxy/bin.ts --outfile dist/bin/urun-openai && node scripts/assert-bin-transport.mjs"
71
71
  },
72
72
  "peerDependencies": {
73
- "@urun-sh/core": "^0.5.6"
73
+ "@urun-sh/core": "^0.6.0"
74
74
  },
75
75
  "dependencies": {
76
76
  "@prometheus-io/client": "^0.16.1",
@@ -87,6 +87,8 @@
87
87
  "@types/node": "^24.0.0",
88
88
  "@types/ws": "^8.18.1",
89
89
  "@urun-sh/core": "workspace:*",
90
+ "ajv": "^8.17.1",
91
+ "ajv-formats": "^3.0.1",
90
92
  "esbuild": "^0.28.1",
91
93
  "happy-dom": "^20.9.0",
92
94
  "opusscript": "^0.1.1",
@@ -1 +0,0 @@
1
- import{c as p}from"./chunk-5CCJUNH7.js";p();function g(e,t,r,n){let o=e.response??{},i={request_id:r,consumer_id:n,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 _(e,t,r){let n={request_id:t,consumer_id:r,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(n.model=e.model),e.tools!==void 0&&(n.tools=e.tools),e.tool_choice!==void 0&&(n.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(n.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(n.max_output_tokens=e.max_output_tokens),typeof e.top_p=="number"&&(n.top_p=e.top_p),Array.isArray(e.stop)&&e.stop.length>0&&(n.stop=e.stop),typeof e.reasoning_effort=="string"&&(n.reasoning_effort=e.reasoning_effort),e.chat_template_kwargs!==void 0&&(n.chat_template_kwargs=e.chat_template_kwargs),e.response_format!==void 0&&(n.response_format=e.response_format),n}p();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,r=t.body??{},n=r.error,o=n?.message??r.message??`unreadable serve error body: ${JSON.stringify(t.body??null)}`;return{type:"error",error:{type:n?.type??"urun_error",code:t.code??n?.code??null,message:o}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}p();var k="llm-resp";async function*m(e,t){let r=`${k}:${t}`,n=`resp_${t}`;yield{type:"response.created",response:{id:n,status:"in_progress"}};let o=new Map;for await(let i of e.stream(r).messages()){let s=i;if(s.t==="delta"){if(typeof s.delta=="string"&&(yield{type:"response.output_text.delta",item_id:n,delta:s.delta}),typeof s.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:n,delta:s.reasoning}),Array.isArray(s.tool_calls))for(let u of s.tool_calls){let l=typeof u.index=="number"?u.index:0,a=o.get(l)??{};u.id&&(a.id=u.id),u.function?.name&&(a.name=u.function.name),o.set(l,a),yield{type:"response.function_call_arguments.delta",item_id:`fc_${t}_${l}`,tool_index:l,call_id:a.id,name:a.name,delta:u.function?.arguments??""}}}else if(s.t==="response"){yield{type:"response.completed",response:{id:n,status:"completed",...s.body}};return}else if(s.t==="error"){yield f(s);return}}}p();var w="llm",y=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(w).set({requests:{[t.request_id]:{payload:t,consumer_id:t.consumer_id,stream:t.stream}}})}sendResponses(t,r){let n=_(t,r,this.consumerId);return this.write(n),m(this.session,r)}sendResponseCreate(t,r,n){let o=g(t,r,n,this.consumerId);return this.write(o),m(this.session,n)}};export{g as a,_ as b,f as c,m as d,y as e};
@@ -1,4 +0,0 @@
1
- import{c as h}from"./chunk-5CCJUNH7.js";h();h();var p=class extends Error{};function ee(o){if(o===null||typeof o!="object")return null;let t=o.shared;if(t==null)return null;if(typeof t!="object")throw new p(`catalog "shared" block is ${typeof t}, expected an object`);let{shared_org_id:e,endpoints:n}=t;if(typeof e!="string"||e==="")throw new p(`catalog "shared" block has no shared_org_id: ${JSON.stringify(t)}`);if(!Array.isArray(n))throw new p(`catalog "shared" block endpoints is not an array: ${JSON.stringify(t)}`);let r=n.map((i,a)=>{if(i===null||typeof i!="object")throw new p(`catalog shared endpoints[${a}] is not an object: ${JSON.stringify(i)}`);let s=i;for(let d of["model_id","variant","app_slug","function"])if(typeof s[d]!="string"||s[d].length===0)throw new p(`catalog shared endpoints[${a}] (${JSON.stringify(s)}) has no valid "${d}"`);return s});return r.length===0?null:{shared_org_id:e,endpoints:r}}function S(o){return[...o.endpoints].sort((t,e)=>t.model_id.localeCompare(e.model_id)||t.variant.localeCompare(e.variant))}function N(o,t,e){let n=(t??"").trim();if(n==="")return null;let r=n,i=e,a=n.lastIndexOf(":");a>0&&(r=n.slice(0,a),i=n.slice(a+1));let s=o.endpoints.filter(l=>l.model_id===r);if(s.length===0)return null;if(i!==void 0){let l=s.find(u=>u.variant===i);return l?{shared_org_id:o.shared_org_id,endpoint:l}:null}if(s.length===1)return{shared_org_id:o.shared_org_id,endpoint:s[0]};let d=s.filter(l=>l.compat_default===!0);if(d.length!==1)throw new p(`shared model "${r}" has ${d.length} compat_default rows (exactly one required) among variants ${s.map(l=>l.variant).join(", ")}`);return{shared_org_id:o.shared_org_id,endpoint:d[0]}}async function ye(o){let t=o.fetchImpl??fetch,e=`${o.apiUrl.replace(/\/+$/,"")}/catalog`,n=await t(e,{headers:{Authorization:`Bearer ${o.apiKey}`,Accept:"application/json"}});if(!n.ok)throw new p(`catalog read failed: GET ${e} \u2192 ${n.status}`);return ee(await n.json())}h();function w(o){return o.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}h();var g=class extends Error{name="ModelPriceError"},M="per_million_input_tokens",A="per_million_output_tokens",j="model_id,variant,unit,price_usd,effective_at,expires_at",D={per_request:!0,per_minute:!0,per_output_unit:!0},te=5e3;function ne(o,t){let e=typeof o=="number"?o:typeof o=="string"&&o.trim()!==""?Number(o):NaN;if(!Number.isFinite(e)||e<0)throw new g(`${t}: price_usd is not a non-negative number (${JSON.stringify(o)})`);return e}function F(o,t,e){let n=typeof o=="string"?Date.parse(o):NaN;if(!Number.isFinite(n))throw new g(`${t}: ${e} is not a parseable timestamp (${JSON.stringify(o)})`);return n}function oe(o,t,e){return o===null?null:F(o,t,e)}function re(o,t){if(o===M)return"input";if(o===A)return"output";if(typeof o=="string"&&Object.hasOwn(D,o))return null;throw new g(`${t}: unit ${JSON.stringify(o)} is neither a per-token rate (${M} | ${A}) nor a known GPU-time unit (${Object.keys(D).join(" | ")}). Skipping it would publish no prices forever with no signal \u2014 if ENG-317 named the per-token units differently, teach this module the new names instead`)}function ie(o,t){let e=o.reduce((r,i)=>i.effective_at>r.effective_at?i:r,o[0]),n=o.find(r=>r.effective_at===e.effective_at&&r.usd!==e.usd);return n?{reason:`two ${t} rates are effective at the same instant (${e.usd} and ${n.usd} USD/Mtok), so the winner would be read order \u2014 this model is served UNPRICED until one of them is expired; expire one of them`}:{rate:e}}function se(o){if(!Array.isArray(o))throw new g(`model_prices read is not an array: ${JSON.stringify(o)}`);let t=[];for(let e=0;e<o.length;e++){let n=o[e],r=`model_prices[${e}]`;if(n===null||typeof n!="object"||Array.isArray(n))throw new g(`${r} is not an object: ${JSON.stringify(n)}`);let i=n;for(let l of j.split(","))if(!Object.hasOwn(i,l))throw new g(`${r} has no "${l}" column: ${JSON.stringify(i)}`);let a=re(i.unit,r);if(a===null)continue;let s=i.model_id;if(typeof s!="string"||s==="")throw new g(`${r} has no model_id (${JSON.stringify(i)})`);let d=i.variant;if(!(d===null||typeof d=="string"&&d!==""))throw new g(`${r} has a non-string variant (${JSON.stringify(d)})`);t.push({model_id:s,variant:d,side:a,usd:ne(i.price_usd,r),effective_at:F(i.effective_at,r,"effective_at"),expires_at:oe(i.expires_at,r,"expires_at")})}return t}function L(o,t=Date.now()){let e=new Map,n=new Map;for(let s of o){if(s.effective_at>t||s.expires_at!==null&&s.expires_at<=t)continue;let d=e.get(s.model_id);d||(d=new Map,e.set(s.model_id,d),n.set(s.model_id,new Map));let l=s.variant??"",u=d.get(l);u||(u={input:[],output:[]},d.set(l,u),n.get(s.model_id).set(l,s.variant)),u[s.side].push({effective_at:s.effective_at,usd:s.usd})}let r=[],i=[];for(let[s,d]of e){let l=d.get(""),u=(y,f)=>{let m=y&&y.length>0?y:l?.[f];if(!m||m.length===0)return null;let c=ie(m,f);return"reason"in c?c:c.rate.usd===0?{reason:`the ${f} rate in force is zero, which publishes as "free" and would top :cheapest routing \u2014 this model is served UNPRICED; delete the row if the model is not token-priced, or set a real rate`}:c.rate};for(let[y,f]of n.get(s)){let m=d.get(y),c=u(m?.input,"input"),v=u(m?.output,"output");if(c!==null&&"reason"in c){i.push({model_id:s,variant:f,reason:c.reason});continue}if(v!==null&&"reason"in v){i.push({model_id:s,variant:f,reason:v.reason});continue}c===null||v===null||r.push({model_id:s,variant:f,pricing:{input:c.usd,output:v.usd}})}}let a=(s,d)=>s.model_id.localeCompare(d.model_id)||(s.variant===null?1:0)-(d.variant===null?1:0)||(s.variant??"").localeCompare(d.variant??"");return{prices:r.sort(a),refusals:i.sort(a)}}function R(o,t,e){if(e.refusals.some(i=>i.model_id===o&&i.variant===t))return null;let n=e.prices.find(i=>i.model_id===o&&i.variant===t);if(n)return n.pricing;if(e.refusals.some(i=>i.model_id===o&&i.variant===null))return null;let r=e.prices.find(i=>i.model_id===o&&i.variant===null);return r?r.pricing:null}var P={prices:[],refusals:[]};async function $e(o){let t=o.fetchImpl??fetch,e=`${M},${A}`,n=await t(`${o.catalogUrl}/model_prices?select=${j}&unit=in.(${e})`,{headers:{apikey:o.anonKey,Authorization:`Bearer ${o.anonKey}`},signal:AbortSignal.timeout(te)});if(!n.ok)throw new Error(`model_prices fetch failed: ${n.status}`);return se(await n.json())}h();var ae={chat:!0,code:!0,agent:!0,vl:!0},U={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function le(o){let t=o.toLowerCase();return Object.hasOwn(U,t)?U[t]:null}var de=["context_length","max_model_len"];function ue(o){let t=de.map(e=>o.engine_args?.[e]).filter(e=>typeof e=="number"&&Number.isFinite(e)&&e>0);return t.length===0?null:t.every(e=>e===t[0])?t[0]:null}function x(o){let t=o.map(ue),e=t[0];return e==null?null:t.every(n=>n===e)?e:null}function pe(o,t){let e=t.filter(r=>w(`${r.model_id}-${r.variant}`)===o);if(e.length>0)return O(e);let n=t.filter(r=>w(r.model_id)===o);return n.length>0?O(n):null}function O(o){let t=o[0];return o.every(n=>n.model_id===t.model_id&&n.variant===t.variant)?{row:o.length===1?t:ce(o),matches:o}:null}function ce(o){let t=o[0],e=n=>o.every(r=>(r[n]??void 0)===(t[n]??void 0))?t[n]:void 0;return{model_id:t.model_id,variant:t.variant,gpu_spec:e("gpu_spec"),engine:e("engine"),task:e("task"),description:e("description"),size_gb:e("size_gb"),hf_repo:e("hf_repo"),updated_at:e("updated_at")}}function J(o){let t=o.updated_at?Date.parse(o.updated_at):NaN;return Number.isFinite(t)?Math.floor(t/1e3):0}function T(o,t,e){let n=pe(o,t);if(!n)return{id:o,object:"model",created:0,owned_by:"urun"};let{row:r,matches:i}=n,a=x(i),s=R(r.model_id,r.variant,e);return{id:o,object:"model",created:J(r),owned_by:"urun",name:`${r.model_id}:${r.variant}`,...r.description?{description:r.description}:{},...r.task?{task:r.task}:{},...r.engine?{engine:r.engine}:{},...r.gpu_spec?{gpu_spec:r.gpu_spec}:{},...a!==null?{context_length:a}:{},...s!==null?{pricing:s}:{}}}function B(o){let t=/^(\d+)(?:\.(\d+))?(?:e([+-]?\d+))?$/.exec(String(o));if(!t)throw new Error(`unrenderable per-token price: ${String(o)}`);let[,e="",n="",r=""]=t,i=e+n,a=e.length-6+(r===""?0:Number(r));return(a<=0?`0.${"0".repeat(-a)}${i}`:`${i.slice(0,a)}.${i.slice(a).padEnd(1,"0")}`).replace(/(\.\d*?)0+$/,"$1").replace(/\.$/,"")}function K(o){return{input:[{type:"prompt",unit:"token",cost_usd:B(o.input)}],output:[{type:"completion",unit:"token",cost_usd:B(o.output)}]}}function ge(o,t){if(o.length===0)return null;let e=O(o);if(!e)return null;let{row:n,matches:r}=e;if(!n.task||ae[n.task]!==!0)return null;let i=x(r),a=R(n.model_id,n.variant,t),s={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};return i!==null&&(s.max_tokens={type:"integer",min:1,max:i,unit:"token"}),{schema_version:"2.4",id:`${n.model_id}:${n.variant}`,name:`${n.model_id}:${n.variant}`,created:J(n),quantization:le(n.variant),description:n.description??"",hugging_face_id:n.hf_repo?n.hf_repo.split("@")[0]??n.hf_repo:"",input_modalities:[{type:"text",...i!==null?{supported_inputs:{max_context_length:{value:i,unit:"token"}}}:{},...a?{pricing:K(a).input}:{}},...n.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:s,...a?{pricing:K(a).output}:{}}]}}function z(o,t,e){let n=[],r=new Set;for(let i of o){let a=`${i.model_id}:${i.variant}`;if(r.has(a))continue;r.add(a);let s=ge(t.filter(d=>d.model_id===i.model_id&&d.variant===i.variant),e);s&&n.push(s)}return{data:n}}var Ne="https://api.urun.sh/v1",_=class extends Error{},b=class extends Error{},I="urs1.";function fe(o,t){return I+Buffer.from(JSON.stringify([o,t]),"utf8").toString("base64url")}function he(o){if(!o.startsWith(I))return null;try{let t=JSON.parse(Buffer.from(o.slice(I.length),"base64url").toString("utf8"));if(Array.isArray(t)&&typeof t[0]=="string"&&typeof t[1]=="string")return{app:t[0],key:t[1]}}catch{}return null}async function De(o){let t=o.fetchImpl??fetch,e=`${o.apiUrl.replace(/\/+$/,"")}/apps`,n=await t(e,{headers:{Authorization:`Bearer ${o.apiKey}`,Accept:"application/json"}});if(!n.ok)throw new Error(`org apps listing failed: GET ${e} \u2192 ${n.status}`);let r=await n.json();if(!Array.isArray(r.apps))throw new Error(`org apps listing returned no "apps" array (GET ${e})`);if(r.truncated===!0)throw new Error(`org apps listing was truncated (GET ${e} returned ${r.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return r.apps}function H(o){if(o.length===0)return[];if(!o.every(e=>(e.task??void 0)==="image"))return[];let t=we(o);return t===null?[]:[t]}function q(o,t,e,n,r){if(!r)throw new p(`shared model "${o}" cannot be gated: no catalog oracle is configured, and a shared endpoint's modality must come from the model's OWN model_catalog row \u2014 never a default`);if(t===void 0||e===void 0)throw new p(`shared model "${o}" resolved without a catalog id + variant to join on`);let i=n.filter(a=>a.model_id===t&&a.variant===e);if(i.length===0)throw new p(`shared model "${o}" names endpoint ${t}:${e} but the catalog has no model_catalog row for it \u2014 the shared block cannot vouch a modality from nothing`);return i}function G(o,t){if(!t.every(r=>(r.task??void 0)==="image"))return[];let e=t.map(r=>r.engine_args?.expects_image),n=e[0];if(typeof n!="boolean"||!e.every(r=>r===n))throw new p(`shared image model "${o}" does not vouch an explicit image mode: its ${t.length} model_catalog row(s) carry engine_args.expects_image ${JSON.stringify(e)} \u2014 the shared lane requires a boolean (false = pure text-to-image generation, true = edit) and refuses to silently default to edit`);return[n===!1?"generation":"edit"]}var Z="shared:";function W(o){return o.sharedApp?Z+o.appSlug:o.appSlug}function X(o){return typeof o.modelId=="string"&&typeof o.variant=="string"?{kind:"catalog",model_id:o.modelId,variant:o.variant}:{kind:"not-a-catalog-model",model:o.appSlug}}function E(o,t){let e=t.filter(i=>w(`${i.model_id}-${i.variant}`)===o);if(e.length>0)return e;let n=t.filter(i=>w(i.model_id)===o);return new Set(n.map(i=>i.variant)).size===1?n:[]}function me(o){let t=o.engine_args?.expects_image;return t==null?"edit":t===!1?"generation":t===!0?"edit":null}function we(o){let t=o.map(me),e=t[0];return e!=null&&t.every(n=>n===e)?e:null}var V={stt:"stt",tts:"tts"};function Q(o){if(o.length===0)return null;let t=o[0].task??void 0;return t===void 0||V[t]===void 0?null:o.every(e=>(e.task??void 0)===t)?V[t]:null}var C=6e4,Y=class{constructor(t){this.opts=t}opts;pool=new Map;appsCache=null;rowsCache=null;seed(t,e){this.pool.set(t,Promise.resolve(e))}async deployedApps(){if(!this.opts.listApps)return[];let t=this.opts.appsTtlMs??C;if(this.appsCache&&Date.now()-this.appsCache.at<t)return this.appsCache.apps;let e=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:e},e}async sharedBlock(){if(!this.opts.shared)return null;let t=this.opts.appsTtlMs??C;if(this.sharedCache&&Date.now()-this.sharedCache.at<t)return this.sharedCache.block;let e=await this.opts.shared();return this.sharedCache={at:Date.now(),block:e},e}sharedCache=null;async catalogRows(){if(!this.opts.listCatalog)return[];let t=this.opts.appsTtlMs??C;if(this.rowsCache&&Date.now()-this.rowsCache.at<t)return this.rowsCache.rows;let e=await this.opts.listCatalog();return this.rowsCache={at:Date.now(),rows:e},e}pricesCache=null;async priceRows(){if(!this.opts.listPrices)return[];let t=this.opts.appsTtlMs??C;if(this.pricesCache&&Date.now()-this.pricesCache.at<t)return this.pricesCache.rows;let e=await this.opts.listPrices();return this.pricesCache={at:Date.now(),rows:e},e}async pricesForListing(){let t;try{t=await this.priceRows()}catch(n){if(n instanceof g)throw n;return this.warn(`urun /v1/models: the per-token price surface could not be read (${String(n instanceof Error?n.message:n)}) \u2014 every model is listed UNPRICED for now; Hugging Face will show no price until the read succeeds
2
- `),P}let e=L(t,Date.now());for(let n of e.refusals)this.warn(`urun /v1/models: refusing to price ${n.model_id}:${n.variant??"*"} \u2014 ${n.reason}
3
- `);return e}warn(t){(this.opts.warn??(n=>console.warn(n.trimEnd())))(t)}servable(t){return t.filter(e=>e.function_name===this.opts.fnName&&e.deployment_status==="active").map(e=>e.app_slug)}async availableIds(){let t=this.opts.defaultApp,e=new Set(t===null?[]:[t]);if(this.opts.listApps)for(let r of this.servable(await this.deployedApps()))e.add(r);for(let r of this.pool.keys())r.startsWith(Z)||e.add(r);let n=[...e].filter(r=>r!==t).sort();return t===null?n:[t,...n]}async noDefaultApp(t){let e=await this.availableIds(),n=e.length>0?e.join(", "):"(none deployed)";throw new _(t===""?`no "model" was given and this endpoint has no default model \u2014 name one of your org's deployed "${this.opts.fnName}" apps; available models: ${n}`:`model "${t}" does not name a deployed "${this.opts.fnName}" app in your org, and this endpoint has no default model to fall back to; available models: ${n}`)}async resolveTarget(t){let e=(t??"").trim(),n=this.opts.defaultApp;if(!e||e==="urun")return n===null?this.noDefaultApp(e):{appSlug:n};let r=w(e);if(r===n)return{appSlug:n};if(this.pool.has(r))return{appSlug:r};if(this.opts.listApps){let i=await this.deployedApps(),a=this.servable(i);if(a.includes(r))return{appSlug:r};let s=a.filter(l=>l.startsWith(`${r}-`));if(s.length===1)return{appSlug:s[0]};if(s.length>1)throw new _(`model "${e}" is ambiguous across deployed apps ${s.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let d=i.find(l=>l.app_slug===r||l.app_slug.startsWith(`${r}-`));if(d)throw new _(`model "${e}" maps to app "${d.app_slug}", which is not an active "${this.opts.fnName}" app \u2014 deploy it with \`urun serve ${e}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.shared){let i=await this.sharedBlock();if(i){let a=N(i,e);if(a)return{appSlug:a.endpoint.app_slug,fnName:a.endpoint.function,sharedApp:!0,modelId:a.endpoint.model_id,variant:a.endpoint.variant}}}if(this.opts.listCatalog){let a=(await this.opts.listCatalog()).find(s=>e===s.model_id||e===`${s.model_id}:${s.variant}`||r===w(s.model_id)||r===w(`${s.model_id}-${s.variant}`));if(a)throw new _(`model "${e}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${a.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return n===null?this.noDefaultApp(e):{appSlug:n}}async catalogIdentityFor(t){let e=X(await this.resolveTarget(t));return e.kind==="catalog"?{model_id:e.model_id,variant:e.variant}:null}async servingAppSlugFor(t){return(await this.resolveTarget(t)).appSlug}async resolveApp(t){return W(await this.resolveTarget(t))}async sessionFor(t){let e=await this.resolveTarget(t),n=W(e),r=this.pool.get(n);return r||(r=Promise.resolve(this.opts.openSession(e)),this.pool.set(n,r),r.catch(()=>this.pool.delete(n))),{app:n,entry:await r,identity:X(e)}}keyOf(t){let e=this.opts.sessionKey;if(!e)throw new Error("ModelRouter: sessionKey is not configured \u2014 the session-identity seam (handleFor/sessionForHandle) is unavailable on this router");let n=e(t);if(typeof n!="string"||n.length===0)throw new Error("ModelRouter: sessionKey returned an empty identity for a pooled session");return n}async handleFor(t){let{app:e,entry:n}=await this.sessionFor(t);return{app:e,handle:fe(e,this.keyOf(n))}}async sessionForHandle(t){let e=he(t);if(!e)throw new b(`malformed session handle ${JSON.stringify(t)} \u2014 not issued by this proxy`);let{app:n,key:r}=e,i=this.pool.get(n),a=i?await i.then(s=>s,()=>null):null;if(a===null)throw new b(`the session behind this handle (app "${n}") is gone \u2014 closed, evicted after its pod died, or the proxy restarted; it cannot be reattached. Reconnect without a handle to start a new session.`);if(this.keyOf(a)!==r)throw new b(`the session behind this handle (app "${n}") was replaced \u2014 the original backhaul died and a new session serves this app now; the handle's session cannot be reattached. Reconnect without a handle to start a new session.`);return{app:n,entry:a}}async evict(t,e){let n=this.pool.get(t);!n||await n.then(i=>i,()=>null)!==e||this.pool.get(t)!==n||(this.pool.delete(t),await this.opts.closeSession(e).catch(()=>{}))}async modelList(){let t=await this.availableIds(),[e,n]=await Promise.all([this.catalogRows().then(l=>({ok:!0,rows:l}),()=>({ok:!1,rows:[]})),this.pricesForListing()]),r=e.rows,i=e.ok?n:P;e.ok||this.warn(`urun /v1/models: the model catalog could not be read \u2014 entries fall back to the bare OpenAI fields AND no model is priced on either lane (pricing follows catalog identity, so a half-priced listing would flicker on Hugging Face)
4
- `);let a=await this.sharedBlock(),d=(a?S(a):[]).map(l=>{let u={id:`${l.model_id}:${l.variant}`,object:"model",created:0,owned_by:"urun",name:`${l.model_id}:${l.variant}`};l.gpu_spec&&(u.gpu_spec=l.gpu_spec),typeof l.warm=="number"&&(u.warm=l.warm);let y=r.filter(c=>c.model_id===l.model_id&&c.variant===l.variant),f=x(y);f!==null&&(u.context_length=f);let m=R(l.model_id,l.variant,i);return m!==null&&(u.pricing=m),u});return{object:"list",data:[...t.map(l=>T(l,r,i)),...d]}}async openRouterModels(){let[t,e]=await Promise.all([this.catalogRows(),this.pricesForListing()]),n=await this.sharedBlock();return z(n?S(n):[],t,e)}async supportsImages(t,e){let n=await this.resolveTarget(t),r=await this.catalogRows();if(n.sharedApp){let i=q(String(t),n.modelId,n.variant,r,this.opts.listCatalog!==null);return G(`${n.modelId}:${n.variant}`,i).includes(e)}return H(E(n.appSlug,r)).includes(e)}async supportsAudio(t){let e=await this.resolveTarget(t),n=await this.catalogRows();return e.sharedApp?Q(n.filter(r=>r.model_id===e.modelId&&r.variant===e.variant))!==null:Q(E(e.appSlug,n))!==null}async imageModelList(){let t=await this.catalogRows(),e=P,n=[];for(let i of await this.availableIds()){let a=H(E(i,t));a.length!==0&&n.push({...T(i,t,e),image_modes:a})}let r=await this.sharedBlock();if(r)for(let i of S(r)){let a=q(`${i.model_id}:${i.variant}`,i.model_id,i.variant,t,this.opts.listCatalog!==null),s=G(`${i.model_id}:${i.variant}`,a);if(s.length===0)continue;let d={id:`${i.model_id}:${i.variant}`,object:"model",created:0,owned_by:"urun",name:`${i.model_id}:${i.variant}`,image_modes:s};i.gpu_spec&&(d.gpu_spec=i.gpu_spec),typeof i.warm=="number"&&(d.warm=i.warm),n.push(d)}return{object:"list",data:n}}async closeAll(){let t=[...this.pool.values()],e=[];if(await Promise.all(t.map(async n=>{try{await this.opts.closeSession(await n)}catch(r){e.push(String(r instanceof Error?r.message:r))}})),e.length>0)throw new Error(`failed to close ${e.length} pooled session(s): ${e.join("; ")}`)}};export{ye as a,$e as b,Ne as c,_ as d,b as e,De as f,W as g,Y as h};
@@ -1 +0,0 @@
1
- import{e as r}from"./chunk-2T2YYBVX.js";import{c as e}from"./chunk-5CCJUNH7.js";e();var o=0;function i(){return o+=1,`req_${Date.now().toString(36)}_${o.toString(36)}`}var s=class{transport;constructor(n){this.transport=new r(n)}responses={create:async n=>{let t=i(),a=this.transport.sendResponses(n,t);return Object.assign((async function*(){yield*a})(),{requestId:t})}}};export{s as a};
@@ -1 +0,0 @@
1
- import{a as P}from"./chunk-5HKWNK3O.js";import{a as A}from"./chunk-FP4RSAIE.js";import{a as E,b as R,e as m,f as C,g as y,h as x}from"./chunk-3WAJD62J.js";import{c as v,f as k,g as O,p as b}from"./chunk-BSHT6RZZ.js";import{c as f}from"./chunk-5CCJUNH7.js";f();import{App as U,createClientToken as K}from"@urun-sh/core";f();import{SessionFailedError as D}from"@urun-sh/core";var w=2;function $(e){let s=e?.type;return s==="response.created"||s==="response.in_progress"}function _(e){let s=e?.type;return s==="response.completed"||s==="error"}function L(e){return async function(r,u){return(async function*(){for(let n=1;n<=w;n++){let{app:o,entry:t,identity:a}=await e.sessionFor(r.model);u?.({sessionId:e.sessionIdOf?.(t)??null,identity:a});let d=!1,i=!1;try{let p=await e.createStream(t,r);for await(let h of p){if(_(h)&&(d=!0),$(h)){if(n>1)continue}else i=!0;yield h}}catch(p){if(p instanceof D&&(p.kind==="never-live"||p.kind==="create-failed"))throw p;if(n<w&&!i){await e.evict(o,t);continue}throw p}if(d)return;if(n<w&&!i){await e.evict(o,t);continue}return}})()}}f();var N=3,B="llm",H=new Set(["paused","error"]);function F(e,s){let r=e.doc(B),u=typeof r.onConnectionState=="function",l=typeof e.onPhase=="function";if(!u&&!l)throw new Error(`session for app "${s}" exposes neither a doc connection-state surface nor onPhase \u2014 session-gone detection requires @urun-sh/core Session (SessionDocument.onConnectionState / Session.onPhase); pooling a session with no native lifecycle signal would silently swallow requests dispatched after the platform closes it`);let n=null,o=!1,t=new Set,a=[],d=i=>{if(!n){n=i;for(let p of[...t])p(i);t.clear()}};return u&&a.push(r.onConnectionState(i=>{o&&(i.consecutiveFailures<N||d(new m(`the pooled session for app "${s}" is gone: its doc backhaul failed ${i.consecutiveFailures} consecutive times (last close ${i.lastCloseCode===null?"by provider liveness reap":`code ${i.lastCloseCode}`}${i.lastCloseReason?`, reason ${JSON.stringify(i.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`)))})),l&&a.push(e.onPhase(i=>{if(i.name==="live"){o=!0;return}o&&H.has(i.name)&&d(new m(`the pooled session for app "${s}" lost its compute binding (phase "${i.name}"). The entry is evicted; retry to get a fresh session. The named session itself is not over \u2014 it stays attachable by name.`))})),{gone:()=>n,onGone(i){return n?(i(n),()=>{}):(t.add(i),()=>t.delete(i))},dispose(){t.clear();for(let i of a.splice(0))i()}}}async function*S(e,s){let r=s.gone();if(r)throw r;let u=e[Symbol.asyncIterator](),l,n=new Promise((o,t)=>{l=s.onGone(a=>t(a))});n.catch(()=>{});try{for(;;){let o=await Promise.race([u.next(),n]);if(o.done)return;yield o.value}}finally{l?.(),u.return?.(void 0)}}function M(e,s,r,u){let l=e[r];if(typeof l!="function")throw new Error(`app "${s}" has no function "${r}" (set URUN_FUNCTION)`);let n=u?l(void 0,u):l();if(typeof n.end!="function")throw new Error(`app "${s}" function "${r}" returned a session without end()`);return n}function j(e,s){let{baseUrl:r,orgId:u,auth:l,subject:n}=e,o=s.fnName??e.fnName,t=y(s),a=e.sessionStartOptions?.(t),d=s.sharedApp?{sharedApp:!0,...a}:a,i=c=>({session:c,responses:new A(c),gone:F(c,t)});if(l.lane==="jwt"){let c=U(s.appSlug,{baseUrl:r,orgId:u,jwt:l.jwt});return i(M(c,s.appSlug,o,d))}let{apiKey:p,gatewayUrl:h}=l,T=U(s.appSlug,{baseUrl:r,orgId:u,getAccessToken:async()=>(await K(p,{baseUrl:h,expiresIn:300,allowedFunctions:[`${s.appSlug}/${o}`],...n===void 0?{}:{subject:n},...s.sharedApp?{sharedApp:!0}:{}})).token});return i(M(T,s.appSlug,o,d))}function V(e){let s=e.session.id;if(typeof s!="string"||s.length===0)throw new Error("pooled session exposes no stable session id \u2014 the session-identity seam requires @urun-sh/core Session (session.id)");return s}var W=new Set(["error","paused"]);function ue(e){let{auth:s,catalog:r}=e,u=null,l=o=>{let t=y(o),a=j(e,o);return e.onSessionOpen&&typeof a.session.id=="string"&&a.session.id!==""&&e.onSessionOpen(t,a.session.id),a.gone.onGone(()=>{u?.evict(t,a)}),a},n=new x({defaultApp:e.defaultApp,fnName:e.fnName,openSession:l,closeSession:async o=>{if(o.gone.dispose(),e.release==="detach"){if(typeof o.session.detach!="function")throw new Error('pooled session exposes no native detach() \u2014 the release:"detach" lane requires @urun-sh/core Session.detach');o.session.detach();return}await o.session.end()},listApps:s.lane==="api-key"?()=>C({apiUrl:e.apiUrl,apiKey:s.apiKey}):null,listCatalog:r?()=>P(r):null,listPrices:r?()=>R(r):null,shared:s.lane==="api-key"?()=>E({apiUrl:e.apiUrl,apiKey:s.apiKey}):null,sessionKey:V});return u=n,e.defaultApp!==null&&n.seed(e.defaultApp,l({appSlug:e.defaultApp})),n}async function g(e){let s=e.whenLive;if(typeof s!="function")throw new Error("pooled session exposes no native whenLive() \u2014 the audio lane must wait for the session's own admission (cold wake: queued \u2192 demand \u2192 promote) before wiring media; a session without the core Session.whenLive surface cannot be served audio");await s.call(e)}function pe(e,s=O){let r=new WeakMap,u=new WeakMap,l=new WeakMap;return{createResponse:L({sessionFor:n=>e.sessionFor(n),sessionIdOf:n=>{let o=n.session.id;return typeof o=="string"&&o.length>0?o:null},evict:(n,o)=>e.evict(n,o),createStream:async(n,o)=>(await g(n.session),S(await n.responses.responses.create(o),n.gone))}),listModels:()=>e.modelList(),openRouterModels:()=>e.openRouterModels(),supportsImages:(n,o)=>e.supportsImages(n,o),listImageModels:()=>e.imageModelList(),catalogIdentity:n=>e.catalogIdentityFor(n),servingAppSlug:async n=>await e.servingAppSlugFor(n),supportsAudio:n=>e.supportsAudio(n),openAudio:async n=>{let{entry:o}=await e.sessionFor(n),t=r.get(o);return t||(t=(async()=>(await g(o.session),v(o.session,await s())))(),r.set(o,t),t.catch(()=>r.delete(o))),t},openVideo:async n=>{let{entry:o}=await e.sessionFor(n),t=u.get(o);return t||(t=k(o.session),u.set(o,t)),t},openVideoOut:async n=>{let{entry:o}=await e.sessionFor(n),t=l.get(o);return t||(t=b(o.session),l.set(o,t)),t},sessionHandle:async n=>(await e.handleFor(n)).handle,createResponseOn:async(n,o)=>{let{entry:t}=await e.sessionForHandle(n);return await g(t.session),S(await t.responses.responses.create(o),t.gone)},onSessionEnd:async(n,o)=>{let{entry:t}=await e.sessionForHandle(n),a=t.session;if(typeof a.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let d=!1;return a.onPhase(i=>{if(d||!W.has(i.name))return;d=!0;let p=a.endsAt??null;o({timeLeftMs:p?Math.max(0,p.getTime()-Date.now()):null,reason:i.reason??`session ${i.name}`})})}}}export{ue as a,pe as b};