@urun-sh/openai 0.5.3 → 0.5.5

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 (43) hide show
  1. package/dist/chunk-5BZCM3RS.js +4 -0
  2. package/dist/chunk-DMD5UENK.js +1 -0
  3. package/dist/{chunk-G3NMGP4N.js → chunk-GBBY3PCZ.js} +1 -1
  4. package/dist/chunk-I2Q3B3OG.js +6 -0
  5. package/dist/chunk-K23AZPI4.js +1 -0
  6. package/dist/chunk-OI2OY32M.js +1 -0
  7. package/dist/chunk-QVF7NF7G.js +44 -0
  8. package/dist/gemini-live.cjs +2 -2
  9. package/dist/gemini-live.d.cts +6 -0
  10. package/dist/gemini-live.d.ts +6 -0
  11. package/dist/gemini-live.js +1 -1
  12. package/dist/hosted/bin.cjs +36 -22
  13. package/dist/hosted/bin.js +4 -4
  14. package/dist/hosted/index.cjs +32 -18
  15. package/dist/hosted/index.d.cts +140 -198
  16. package/dist/hosted/index.d.ts +63 -82
  17. package/dist/hosted/index.js +1 -1
  18. package/dist/index.cjs +1 -1
  19. package/dist/index.d.cts +2 -18
  20. package/dist/index.d.ts +2 -18
  21. package/dist/index.js +1 -1
  22. package/dist/models-NYMZrklp.d.cts +38 -0
  23. package/dist/models-NYMZrklp.d.ts +38 -0
  24. package/dist/pi-extension/index.cjs +6 -6
  25. package/dist/pi-extension/index.js +1 -1
  26. package/dist/pi-extension/standalone.cjs +49 -49
  27. package/dist/proxy/cli.cjs +41 -37
  28. package/dist/proxy/cli.js +1 -1
  29. package/dist/proxy/index.cjs +30 -21
  30. package/dist/proxy/index.d.cts +468 -2
  31. package/dist/proxy/index.d.ts +173 -2
  32. package/dist/proxy/index.js +6 -1
  33. package/dist/responses-turn-KOAoIqZ-.d.ts +200 -0
  34. package/dist/responses-turn-OrO4euEN.d.cts +513 -0
  35. package/package.json +3 -3
  36. package/dist/chunk-F2TEK34X.js +0 -1
  37. package/dist/chunk-FVD4NMXJ.js +0 -1
  38. package/dist/chunk-M6ICU4F5.js +0 -40
  39. package/dist/chunk-ODHCPZHI.js +0 -6
  40. package/dist/chunk-UVAY7Q7Z.js +0 -1
  41. package/dist/chunk-XHIIEA6Z.js +0 -1
  42. package/dist/server-BfME37pQ.d.cts +0 -200
  43. package/dist/server-l1himPxc.d.ts +0 -82
@@ -2,202 +2,10 @@ import { Server } from 'node:http';
2
2
  import { createClientToken } from '@urun-sh/core';
3
3
  import { U as UrunResponses } from '../ResponsesClient-Dft3bg3b.cjs';
4
4
  import { b as UrunSessionLike } from '../types-lsVTbNcH.cjs';
5
- import { P as ProxyClients } from '../server-BfME37pQ.cjs';
5
+ import { S as SessionGoneError, P as ProxyClients, M as ModelRouter } from '../responses-turn-OrO4euEN.cjs';
6
+ import '../models-NYMZrklp.cjs';
6
7
  import '../video-out-D20UuJ8G.cjs';
7
8
 
8
- /**
9
- * Per-model → per-app routing for the compat proxy (owner directive
10
- * 2026-08-07): swapping the model in a coding harness routes the request to
11
- * the org's DEPLOYED app for that model. v1 is deployed-only — a uRun model
12
- * that is not deployed gets a loud 404 naming `urun serve <id>`; a later
13
- * phase (explicitly out of scope here; urun-infra#1490 shared-endpoints)
14
- * auto-creates from the model catalog on first request.
15
- *
16
- * MODEL-ID SURFACE (the documented mapping): a model may be named by
17
- * - the app slug itself ("qwen3-6-27b-bf16"), or
18
- * - the catalog id ("qwen3.6-27b"), or
19
- * - the catalog id:variant ("qwen3.6-27b:bf16"),
20
- * where slugification mirrors urun-cli `serve.py _default_app_name` exactly:
21
- * lowercase, every non-alphanumeric-non-dash character becomes "-", leading/
22
- * trailing dashes stripped (catalog id "qwen3.6-27b" + variant "bf16" → app
23
- * "qwen3-6-27b-bf16"). COLLISION RULE: an exact slug match always wins over
24
- * the catalog-id (prefix) interpretation.
25
- *
26
- * RESOLUTION ORDER (one canonical path, documented end to end):
27
- * 1. model absent / "urun" / an alias of the startup app → DEFAULT app.
28
- * 2. exact slug match on a deployed serve app → that app.
29
- * 3. catalog-id form matching exactly one deployed app → that app
30
- * (two or more candidates → loud ambiguity error naming them).
31
- * 4. the name maps to an org app that is NOT an active app exposing the
32
- * proxy's serve function → loud 404
33
- * naming `urun serve <model>` and the available models.
34
- * 5. the name matches a catalog row but no deployed app → loud 404
35
- * naming `urun serve <id>` (deployed-only v1).
36
- * 6. anything else — a model name outside the uRun namespace entirely
37
- * (e.g. the harness's own upstream default, "claude-*"/"gpt-*") →
38
- * DEFAULT app. This IS today's single-app contract, kept deliberately
39
- * so `urun compat <agent>` with the agent's stock model keeps working
40
- * with zero new env; the per-model /stats table records every such
41
- * mapping so it is visible, never silent. Models the proxy ADVERTISES
42
- * on /v1/models can never land here — they resolve (2/3) or fail loud
43
- * (4/5) above.
44
- *
45
- * NO-DEFAULT MODE (`defaultApp: null`) — the HOSTED multi-tenant lane
46
- * (`src/hosted/`): a shared endpoint serving every org has no "the app this
47
- * proxy was started for", so rules 1 and 6 have nothing to fall back TO.
48
- * Rather than inventing one (picking "some" app for a caller would be the
49
- * worst kind of silent divergence), both rules become the SAME loud
50
- * {@link UnknownModelError} that rules 4/5 already raise: name a deployed
51
- * model, here is the list. Rules 2–5 are byte-for-byte the local behavior —
52
- * one router, one resolution order, two configurations.
53
- */
54
- /** One org app row from `GET {orgApi}/apps` (urun-cli `ApiClient.list_apps`). */
55
- interface DeployedApp {
56
- app_slug: string;
57
- function_name?: string | null;
58
- deployment_status?: string | null;
59
- [k: string]: unknown;
60
- }
61
- /**
62
- * A session handle names a pooled session that no longer exists (closed,
63
- * evicted after its pod died, replaced by a re-home, or the proxy restarted).
64
- * The caller asked to REATTACH that exact session — opening a fresh one and
65
- * calling it "resumed" would be a silent lie, so this is always loud.
66
- */
67
- declare class SessionGoneError extends Error {
68
- }
69
- /** OpenAI-shaped model list (same shape as models.ts listModels). */
70
- interface RouterModelList {
71
- object: 'list';
72
- data: Array<{
73
- id: string;
74
- object: 'model';
75
- created: number;
76
- owned_by: string;
77
- }>;
78
- }
79
- interface ModelRouterOptions<S> {
80
- /**
81
- * The startup app slug (URUN_APP) — the DEFAULT model — or `null` for the
82
- * hosted multi-tenant lane, which has no per-proxy default app: there,
83
- * every request must NAME a deployed model and an unnamed/unknown one
84
- * fails loud instead of silently landing somewhere (see the module header,
85
- * "NO-DEFAULT MODE").
86
- */
87
- defaultApp: string | null;
88
- /** The serve function name every routed app must expose (URUN_FUNCTION). */
89
- fnName: string;
90
- /** Open a backhaul session for an app slug (called at most once per app). */
91
- openSession: (appSlug: string) => S | Promise<S>;
92
- /** Terminal release for one pool entry (Session.end() underneath). */
93
- closeSession: (entry: S) => Promise<void>;
94
- /**
95
- * List the org's deployed apps, or null when the credentials cannot
96
- * (URUN_JWT lane: the pre-vended token is scoped to the default app, so
97
- * there is no org listing AND no cross-app session — routing degrades to
98
- * the default-app-only contract, which is exactly today's behavior).
99
- */
100
- listApps: (() => Promise<DeployedApp[]>) | null;
101
- /**
102
- * Catalog rows (models.ts fetchCatalogRows) as the uRun-namespace oracle
103
- * for rule 5, or null when catalog access is not configured.
104
- */
105
- listCatalog: (() => Promise<Array<{
106
- model_id: string;
107
- variant: string;
108
- }>>) | null;
109
- /** Deployed-apps cache TTL (the list changes on deploys, not per request). */
110
- appsTtlMs?: number;
111
- /**
112
- * The stable NATIVE identity of one pooled entry (the uRun session id in
113
- * the proxy wiring — the same identity the serve-side session-affinity tag
114
- * rides, urun-python#1556). Powers the session-identity seam
115
- * ({@link ModelRouter.handleFor} / {@link ModelRouter.sessionForHandle});
116
- * a router without it fails LOUD on those calls, never approximates.
117
- */
118
- sessionKey?: (entry: S) => string;
119
- }
120
- /**
121
- * The session pool: one backhaul session per deployed app, keyed by app slug,
122
- * opened lazily on the first request that routes to it and reused for every
123
- * subsequent one. The startup app is seeded eagerly by the CLI. Sessions
124
- * close on proxy shutdown via {@link closeAll}; there is NO idle-close policy
125
- * (deliberate v1 simplification — noted as a follow-up in the PR).
126
- */
127
- declare class ModelRouter<S> {
128
- private readonly opts;
129
- private readonly pool;
130
- private appsCache;
131
- constructor(opts: ModelRouterOptions<S>);
132
- /** Seed an already-open session (the CLI's eagerly-opened startup app). */
133
- seed(appSlug: string, entry: S): void;
134
- private deployedApps;
135
- /** Apps this proxy may serve: active AND exposing the serve function. */
136
- private servable;
137
- private availableIds;
138
- /**
139
- * NO-DEFAULT MODE's terminal for rules 1 and 6: there is no app to fall
140
- * back to, so say so loudly and list what the CALLER'S org actually has.
141
- * Never returns.
142
- */
143
- private noDefaultApp;
144
- /**
145
- * Resolve a request's `model` to an app slug — the documented resolution
146
- * order from the module header. Throws {@link UnknownModelError} for a uRun
147
- * model that is not deployed (rules 4/5).
148
- */
149
- resolveApp(model: string | undefined): Promise<string>;
150
- /** The pooled session for a model — opened lazily, reused afterwards. */
151
- sessionFor(model: string | undefined): Promise<{
152
- app: string;
153
- entry: S;
154
- }>;
155
- private keyOf;
156
- /**
157
- * SESSION-IDENTITY SEAM (a): the opaque stable handle for the pooled
158
- * session currently serving `model`'s turns. Rides the SAME acquisition
159
- * path as every request ({@link sessionFor}) — the session opens lazily if
160
- * this model has none yet — and derives the handle from native identity
161
- * (app slug + uRun session id), zero bespoke bookkeeping.
162
- */
163
- handleFor(model: string | undefined): Promise<{
164
- app: string;
165
- handle: string;
166
- }>;
167
- /**
168
- * SESSION-IDENTITY SEAM (b): the exact pooled session a handle names.
169
- * NEVER opens a fresh session — a handle whose session is gone (closed,
170
- * evicted, re-homed to a replacement, proxy restarted) or malformed throws
171
- * {@link SessionGoneError} loudly. Resume is reattach-or-fail, not
172
- * reattach-or-quietly-restart.
173
- */
174
- sessionForHandle(handle: string): Promise<{
175
- app: string;
176
- entry: S;
177
- }>;
178
- /**
179
- * Drop ONE pooled session whose backhaul died (its pod was restarted /
180
- * drained / deleted) and release it — the next {@link sessionFor} opens a
181
- * fresh one, i.e. asks the control plane for a new assignment. Used by the
182
- * one-shot re-home (rehome.ts, urun-sh/urun-python#1592).
183
- *
184
- * IDENTITY-GUARDED (the same rule the pi lane's SessionPool follows): a
185
- * concurrent request that already re-homed this app has put a NEWER entry
186
- * under the key, and evicting that would close a healthy session out from
187
- * under it.
188
- */
189
- evict(app: string, entry: S): Promise<void>;
190
- /**
191
- * `GET /v1/models`: the org's deployed serve apps as model entries, the
192
- * default app FIRST. On the JWT lane (no org listing) this is the default
193
- * app plus any app already in the pool — the gap is called out loudly in
194
- * the PR, not papered over here.
195
- */
196
- modelList(): Promise<RouterModelList>;
197
- /** Close every pooled session (Session.end() underneath) — proxy shutdown. */
198
- closeAll(): Promise<void>;
199
- }
200
-
201
9
  /**
202
10
  * HOSTED AUTH — the Bearer key IS the identity AND the tenancy.
203
11
  *
@@ -398,6 +206,14 @@ interface CatalogConfig {
398
206
  */
399
207
  type OwnedSession = UrunSessionLike & {
400
208
  end: () => Promise<unknown>;
209
+ /**
210
+ * The SDK's NATIVE local release (core `Session.detach`): drop the media
211
+ * transport and decrement the reference count while the named session
212
+ * stays live and attachable. Optional here because only the
213
+ * `release: 'detach'` lane requires it — that lane fails LOUD when a
214
+ * session object lacks it (no approximation, no fake detach).
215
+ */
216
+ detach?: () => void;
401
217
  id?: string;
402
218
  endsAt?: Date | null;
403
219
  onPhase?: (handler: (phase: {
@@ -511,6 +327,121 @@ declare class TenantRegistry {
511
327
  /** Shutdown: end every pooled session through `Session.end()`. */
512
328
  closeAll(): Promise<void>;
513
329
  }
330
+ /** The most distinct LIVE conversations one replica keeps private backhauls for. */
331
+ declare const MAX_CONVERSATIONS = 512;
332
+ /** The most resume handles the conversation index remembers (bounded memory). */
333
+ declare const MAX_CONVERSATION_HANDLES = 1024;
334
+ /**
335
+ * Every conversation slot holds a conversation (active or resumable) — a NEW
336
+ * bootstrap/setup is refused. Explicit and honest: never an LRU-detach of
337
+ * someone else's conversation to admit a new one.
338
+ */
339
+ declare class ConversationCapacityError extends Error {
340
+ constructor();
341
+ }
342
+ /**
343
+ * One conversation's private backhaul. Its pooled sessions are reachable by
344
+ * NO other conversation of any caller: the token subject is conversation-
345
+ * unique, so the platform's own session dedupe never coalesces them.
346
+ */
347
+ interface PrivateConversationBackhaul {
348
+ clients: ProxyClients;
349
+ /**
350
+ * LOCAL release of every pooled session — `release: 'detach'` underneath,
351
+ * i.e. core's NATIVE `Session.detach()`: the media transport drops, the
352
+ * refcount decrements, the NAMED session stays live and attachable. Never
353
+ * `Session.end()`: no caller has explicitly ended anything.
354
+ */
355
+ release(): Promise<void>;
356
+ }
357
+ interface ConversationBackhaulOptions {
358
+ /** The session-gateway base sessions are opened against. */
359
+ baseUrl: string;
360
+ /** The org control-plane API base (`GET {apiUrl}/apps`). */
361
+ apiUrl: string;
362
+ /** The model-catalog oracle for routing rule 5, or null. */
363
+ catalog: CatalogConfig | null;
364
+ /** Injected in tests; production builds real uRun backhauls. */
365
+ build?: (caller: CallerIdentity, conversation: {
366
+ id: string;
367
+ subject: string;
368
+ }) => PrivateConversationBackhaul;
369
+ }
370
+ /**
371
+ * The per-CONNECTION Live/realtime seam — transport-neutral (Gemini Live,
372
+ * OpenAI Realtime, ...). `clients` lazily opens the caller's PRIVATE
373
+ * conversation backhaul on first use; `bindResumeHandle` indexes each handle
374
+ * the protocol lane mints; `clientsForResume` re-attaches the conversation a
375
+ * handle belongs to — SAME native identity, owner binding enforced, loud
376
+ * SessionGoneError when the conversation is gone; `release` drops THIS
377
+ * attachment when the socket closes.
378
+ */
379
+ interface PrivateConversationConnection {
380
+ clients: ProxyClients;
381
+ bindResumeHandle(handle: string): void;
382
+ clientsForResume(handle: string): Promise<ProxyClients>;
383
+ /**
384
+ * Release THIS attachment: called by the surface when its socket closes.
385
+ * When the LAST attachment of a conversation goes, its native transport is
386
+ * detached immediately — while the conversation's native session
387
+ * REFERENCES (and its resume handles) are RETAINED, so a later resume
388
+ * re-attaches the SAME named session through canonical attach-by-name.
389
+ * Idempotent.
390
+ */
391
+ release(): Promise<void>;
392
+ }
393
+ /**
394
+ * Process-local registry of per-conversation private backhauls, bounded by
395
+ * {@link MAX_CONVERSATIONS}. A conversation whose last attachment closes
396
+ * KEEPS its entry: its native session references + resume handles survive so
397
+ * a later resume re-attaches the SAME named session (canonical
398
+ * attach-by-name). At capacity, only such DETACHED entries are evicted
399
+ * (oldest first — their resume capability is the only thing lost, the named
400
+ * sessions stay attachable server-side); ACTIVE conversations are never
401
+ * touched, and when every slot is active a new bootstrap/setup is refused
402
+ * with an explicit capacity error. NO HA CLAIM: this registry is
403
+ * per-process; a replica restart loses every conversation (loud gone), and
404
+ * no other replica can serve one.
405
+ */
406
+ declare class ConversationBackhauls {
407
+ private readonly opts;
408
+ /** apiKey → conversationId → entry (insertion-ordered = LRU). */
409
+ private readonly byCaller;
410
+ /** resume handle → owning conversation (the owner binding's index). */
411
+ private readonly byHandle;
412
+ constructor(opts: ConversationBackhaulOptions);
413
+ private build;
414
+ /** Find (LRU-refresh) or create the conversation entry, then ATTACH to it. */
415
+ private attachEntry;
416
+ private get conversationCount();
417
+ /**
418
+ * Free a slot for a NEW conversation: evict the OLDEST DETACHED
419
+ * conversation (one whose last attachment already closed — only its resume
420
+ * metadata is lost, no live transport is touched, no session is ended).
421
+ * ACTIVE conversations are never evicted. When every slot holds an active
422
+ * conversation, refuse LOUD with an explicit capacity error — the new
423
+ * bootstrap/setup fails, existing callers are untouched.
424
+ */
425
+ private evictOverflow;
426
+ private dropHandleIndexFor;
427
+ /**
428
+ * The per-upgrade seam for one authenticated caller — ONE attachment. The
429
+ * conversation backhaul opens on the FIRST seam call of a fresh connection;
430
+ * a resumed connection replaces `clients` wholesale via `clientsForResume`
431
+ * before any seam call, so the lazy path is never taken on resume. The
432
+ * attachment is released by `release()` (the socket's close path): when the
433
+ * LAST attachment of a conversation goes, its native transport is detached
434
+ * immediately and its resume metadata is retained — no resource keepalive,
435
+ * no resume loss.
436
+ */
437
+ connectionFor(caller: CallerIdentity): PrivateConversationConnection;
438
+ /**
439
+ * Drain-time LOCAL release: detach every conversation's sessions (native
440
+ * detach — the named sessions pause server-side, nothing is ended). Handles
441
+ * and conversations are forgotten; process exit is the conversation's end.
442
+ */
443
+ detachAll(): Promise<void>;
444
+ }
514
445
 
515
446
  /**
516
447
  * The hosted endpoint's configuration.
@@ -590,7 +521,12 @@ declare function resolveHostedConfig(env?: NodeJS.ProcessEnv): HostedConfig;
590
521
  * or list apps can serve nothing and must be pulled out of
591
522
  * the Service rather than answering 502s.
592
523
  * * /v1/... the compat surface, org-scoped by the Bearer key.
593
- * * anything else → 404 in the OpenAI error envelope.
524
+ * WS /ws/google.ai.generativelanguage.v1beta.GenerativeService.
525
+ * BidiGenerateContent — the Gemini Live surface, org-scoped by the
526
+ * Gemini credential (x-goog-api-key / Authorization: Bearer; `?key=`
527
+ * refused). Ingress/charts must allowlist that path.
528
+ * * anything else → 404 in the OpenAI error envelope (unclaimed WS
529
+ * upgrades get their own final refusal).
594
530
  *
595
531
  * WHAT IS DELIBERATELY NOT HERE: `/stats`. The local proxy exposes it for the
596
532
  * launcher's reuse probe; on a shared endpoint it would publish one tenant's
@@ -617,14 +553,20 @@ interface HostedProxyOptions extends HostedConfig {
617
553
  resolver?: OrgResolver;
618
554
  /** Injected in tests so backhauls are stubbed at the seam. */
619
555
  registry?: TenantRegistry;
556
+ /** Injected in tests so conversation backhauls are stubbed at the seam. */
557
+ conversations?: ConversationBackhauls;
620
558
  }
621
559
  /**
622
- * Build (not listen) the hosted endpoint. The caller owns listen/close, and
623
- * `closeAll` releases every pooled backhaul through `Session.end()`.
560
+ * Build (not listen) the hosted endpoint. The caller owns listen/close,
561
+ * `closeAll` releases every pooled backhaul through `Session.end()`, and
562
+ * `closeWebSockets` gracefully closes every attached WS surface — a drain
563
+ * must run it BEFORE `server.close()`/`closeAllConnections()` (see
564
+ * {@link drainServer}).
624
565
  */
625
566
  declare function createHostedProxy(options: HostedProxyOptions): {
626
567
  server: Server;
627
568
  closeAll: () => Promise<void>;
569
+ closeWebSockets: () => Promise<void>;
628
570
  };
629
571
 
630
- export { BIND_HOST, type CallerIdentity, ControlPlaneUnavailableError, DEFAULT_PORT, type HostedConfig, type HostedProxyOptions, MAX_CACHED_KEYS, MAX_TENANTS, ORG_BINDING_TTL_MS, OrgResolver, type OrgResolverOptions, ProxyAuthError, READINESS_TIMEOUT_MS, READINESS_TTL_MS, SERVE_FUNCTION, TenantRegistry, type TenantRegistryOptions, bearerFrom, createHostedProxy, resolveHostedConfig, tenantSubject };
572
+ export { BIND_HOST, type CallerIdentity, ControlPlaneUnavailableError, type ConversationBackhaulOptions, ConversationBackhauls, ConversationCapacityError, DEFAULT_PORT, type HostedConfig, type HostedProxyOptions, MAX_CACHED_KEYS, MAX_CONVERSATIONS, MAX_CONVERSATION_HANDLES, MAX_TENANTS, ORG_BINDING_TTL_MS, OrgResolver, type OrgResolverOptions, type PrivateConversationBackhaul, type PrivateConversationConnection, ProxyAuthError, READINESS_TIMEOUT_MS, READINESS_TTL_MS, SERVE_FUNCTION, TenantRegistry, type TenantRegistryOptions, bearerFrom, createHostedProxy, resolveHostedConfig, tenantSubject };
@@ -2,89 +2,10 @@ import { Server } from 'node:http';
2
2
  import { createClientToken } from '@urun-sh/core';
3
3
  import { U as UrunResponses } from '../ResponsesClient-BYx3YLGo.js';
4
4
  import { b as UrunSessionLike } from '../types-lsVTbNcH.js';
5
- import { P as ProxyClients } from '../server-l1himPxc.js';
5
+ import { S as SessionGoneError, P as ProxyClients, M as ModelRouter } from '../responses-turn-KOAoIqZ-.js';
6
+ import '../models-NYMZrklp.js';
6
7
  import '../video-out-CWesbk12.js';
7
8
 
8
- interface DeployedApp {
9
- app_slug: string;
10
- function_name?: string | null;
11
- deployment_status?: string | null;
12
- [k: string]: unknown;
13
- }
14
-
15
- declare class SessionGoneError extends Error {
16
- }
17
-
18
- interface RouterModelList {
19
- object: 'list';
20
- data: Array<{
21
- id: string;
22
- object: 'model';
23
- created: number;
24
- owned_by: string;
25
- }>;
26
- }
27
- interface ModelRouterOptions<S> {
28
-
29
- defaultApp: string | null;
30
-
31
- fnName: string;
32
-
33
- openSession: (appSlug: string) => S | Promise<S>;
34
-
35
- closeSession: (entry: S) => Promise<void>;
36
-
37
- listApps: (() => Promise<DeployedApp[]>) | null;
38
-
39
- listCatalog: (() => Promise<Array<{
40
- model_id: string;
41
- variant: string;
42
- }>>) | null;
43
-
44
- appsTtlMs?: number;
45
-
46
- sessionKey?: (entry: S) => string;
47
- }
48
-
49
- declare class ModelRouter<S> {
50
- private readonly opts;
51
- private readonly pool;
52
- private appsCache;
53
- constructor(opts: ModelRouterOptions<S>);
54
-
55
- seed(appSlug: string, entry: S): void;
56
- private deployedApps;
57
-
58
- private servable;
59
- private availableIds;
60
-
61
- private noDefaultApp;
62
-
63
- resolveApp(model: string | undefined): Promise<string>;
64
-
65
- sessionFor(model: string | undefined): Promise<{
66
- app: string;
67
- entry: S;
68
- }>;
69
- private keyOf;
70
-
71
- handleFor(model: string | undefined): Promise<{
72
- app: string;
73
- handle: string;
74
- }>;
75
-
76
- sessionForHandle(handle: string): Promise<{
77
- app: string;
78
- entry: S;
79
- }>;
80
-
81
- evict(app: string, entry: S): Promise<void>;
82
-
83
- modelList(): Promise<RouterModelList>;
84
-
85
- closeAll(): Promise<void>;
86
- }
87
-
88
9
  declare class ProxyAuthError extends Error {
89
10
  readonly status: number;
90
11
  constructor(message: string, status?: number);
@@ -141,6 +62,8 @@ interface CatalogConfig {
141
62
 
142
63
  type OwnedSession = UrunSessionLike & {
143
64
  end: () => Promise<unknown>;
65
+
66
+ detach?: () => void;
144
67
  id?: string;
145
68
  endsAt?: Date | null;
146
69
  onPhase?: (handler: (phase: {
@@ -185,6 +108,61 @@ declare class TenantRegistry {
185
108
  closeAll(): Promise<void>;
186
109
  }
187
110
 
111
+ declare const MAX_CONVERSATIONS = 512;
112
+
113
+ declare const MAX_CONVERSATION_HANDLES = 1024;
114
+
115
+ declare class ConversationCapacityError extends Error {
116
+ constructor();
117
+ }
118
+
119
+ interface PrivateConversationBackhaul {
120
+ clients: ProxyClients;
121
+
122
+ release(): Promise<void>;
123
+ }
124
+ interface ConversationBackhaulOptions {
125
+
126
+ baseUrl: string;
127
+
128
+ apiUrl: string;
129
+
130
+ catalog: CatalogConfig | null;
131
+
132
+ build?: (caller: CallerIdentity, conversation: {
133
+ id: string;
134
+ subject: string;
135
+ }) => PrivateConversationBackhaul;
136
+ }
137
+
138
+ interface PrivateConversationConnection {
139
+ clients: ProxyClients;
140
+ bindResumeHandle(handle: string): void;
141
+ clientsForResume(handle: string): Promise<ProxyClients>;
142
+
143
+ release(): Promise<void>;
144
+ }
145
+
146
+ declare class ConversationBackhauls {
147
+ private readonly opts;
148
+
149
+ private readonly byCaller;
150
+
151
+ private readonly byHandle;
152
+ constructor(opts: ConversationBackhaulOptions);
153
+ private build;
154
+
155
+ private attachEntry;
156
+ private get conversationCount();
157
+
158
+ private evictOverflow;
159
+ private dropHandleIndexFor;
160
+
161
+ connectionFor(caller: CallerIdentity): PrivateConversationConnection;
162
+
163
+ detachAll(): Promise<void>;
164
+ }
165
+
188
166
  declare const DEFAULT_PORT = 8080;
189
167
 
190
168
  declare const SERVE_FUNCTION = "serve";
@@ -217,11 +195,14 @@ interface HostedProxyOptions extends HostedConfig {
217
195
  resolver?: OrgResolver;
218
196
 
219
197
  registry?: TenantRegistry;
198
+
199
+ conversations?: ConversationBackhauls;
220
200
  }
221
201
 
222
202
  declare function createHostedProxy(options: HostedProxyOptions): {
223
203
  server: Server;
224
204
  closeAll: () => Promise<void>;
205
+ closeWebSockets: () => Promise<void>;
225
206
  };
226
207
 
227
- export { BIND_HOST, type CallerIdentity, ControlPlaneUnavailableError, DEFAULT_PORT, type HostedConfig, type HostedProxyOptions, MAX_CACHED_KEYS, MAX_TENANTS, ORG_BINDING_TTL_MS, OrgResolver, type OrgResolverOptions, ProxyAuthError, READINESS_TIMEOUT_MS, READINESS_TTL_MS, SERVE_FUNCTION, TenantRegistry, type TenantRegistryOptions, bearerFrom, createHostedProxy, resolveHostedConfig, tenantSubject };
208
+ export { BIND_HOST, type CallerIdentity, ControlPlaneUnavailableError, type ConversationBackhaulOptions, ConversationBackhauls, ConversationCapacityError, DEFAULT_PORT, type HostedConfig, type HostedProxyOptions, MAX_CACHED_KEYS, MAX_CONVERSATIONS, MAX_CONVERSATION_HANDLES, MAX_TENANTS, ORG_BINDING_TTL_MS, OrgResolver, type OrgResolverOptions, type PrivateConversationBackhaul, type PrivateConversationConnection, ProxyAuthError, READINESS_TIMEOUT_MS, READINESS_TTL_MS, SERVE_FUNCTION, TenantRegistry, type TenantRegistryOptions, bearerFrom, createHostedProxy, resolveHostedConfig, tenantSubject };
@@ -1 +1 @@
1
- import{a as e,b as o,c as t,d as n,e as p,f as s,g as T,h as E,i as _,j as a,k as S,l as f,m as x,n as i,o as y,p as O}from"../chunk-FVD4NMXJ.js";import"../chunk-ODHCPZHI.js";import"../chunk-F2TEK34X.js";import"../chunk-M6ICU4F5.js";import"../chunk-G3NMGP4N.js";import"../chunk-XHIIEA6Z.js";import"../chunk-5NXM4IO3.js";import"../chunk-CWJRDBDC.js";import"../chunk-UVAY7Q7Z.js";import{c as r}from"../chunk-YSFSRI3D.js";r();export{_ as BIND_HOST,o as ControlPlaneUnavailableError,T as DEFAULT_PORT,p as MAX_CACHED_KEYS,S as MAX_TENANTS,n as ORG_BINDING_TTL_MS,s as OrgResolver,e as ProxyAuthError,y as READINESS_TIMEOUT_MS,i as READINESS_TTL_MS,E as SERVE_FUNCTION,x as TenantRegistry,t as bearerFrom,O as createHostedProxy,a as resolveHostedConfig,f as tenantSubject};
1
+ import{a as r,b as e,c as t,d as n,e as a,f as s,g as i,h as p,i as E,j as C,k as S,l as T,m as _,n as O,o as N,p as A,q as l,r as v,s as f,t as x}from"../chunk-5BZCM3RS.js";import"../chunk-I2Q3B3OG.js";import"../chunk-K23AZPI4.js";import"../chunk-QVF7NF7G.js";import"../chunk-GBBY3PCZ.js";import"../chunk-DMD5UENK.js";import"../chunk-5NXM4IO3.js";import"../chunk-CWJRDBDC.js";import"../chunk-OI2OY32M.js";import{c as o}from"../chunk-YSFSRI3D.js";o();export{E as BIND_HOST,e as ControlPlaneUnavailableError,l as ConversationBackhauls,A as ConversationCapacityError,i as DEFAULT_PORT,a as MAX_CACHED_KEYS,O as MAX_CONVERSATIONS,N as MAX_CONVERSATION_HANDLES,S as MAX_TENANTS,n as ORG_BINDING_TTL_MS,s as OrgResolver,r as ProxyAuthError,f as READINESS_TIMEOUT_MS,v as READINESS_TTL_MS,p as SERVE_FUNCTION,_ as TenantRegistry,t as bearerFrom,x as createHostedProxy,C as resolveHostedConfig,T 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 z=Object.getOwnPropertyNames;var X=Object.getPrototypeOf,Z=Object.prototype.hasOwnProperty;var ee=(e,t)=>{for(var r in t)h(e,r,{get:t[r],enumerable:!0})},x=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of z(t))!Z.call(e,o)&&o!==r&&h(e,o,{get:()=>t[o],enumerable:!(n=K(t,o))||n.enumerable});return e};var b=(e,t,r)=>(r=e!=null?Q(X(e)):{},x(t||!e||!e.__esModule?h(r,"default",{value:e,enumerable:!0}):r,e)),te=e=>x(h({},"__esModule",{value:!0}),e);var le={};ee(le,{RT_VIDEO_OUT_STREAM:()=>w,SUPPORTED_REALTIME_CLIENT_EVENTS:()=>A,SUPPORTED_RESPONSES_EVENTS:()=>T,UrunRealtime:()=>k,UrunResponses:()=>_,VIDEO_OUT_DEFAULT_CODEC:()=>E,VIDEO_OUT_FRAMING:()=>R,VIDEO_OUT_WS_FRAMING:()=>W,consumeVideoOut:()=>q,decodeVideoOutRecord:()=>v,encodeVideoOutRecord:()=>$,isSupportedClientEvent:()=>F,isSupportedResponsesEvent:()=>j,listModels:()=>B,negotiateSessionVideoOutQuic:()=>N,nodeAudioBackend:()=>P,openSessionVideoOutLane:()=>H,toErrorEvent:()=>f,weriftAudioBackend:()=>U});module.exports=te(le);function I(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 L(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.reasoning_effort=="string"&&(n.reasoning_effort=e.reasoning_effort),e.chat_template_kwargs!==void 0&&(n.chat_template_kwargs=e.chat_template_kwargs),n}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??{};return{type:"error",error:{type:"urun_error",code:t.code??null,message:r.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}var re="llm-resp";async function*S(e,t){let r=`${re}:${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 d=i;if(d.t==="delta"){if(typeof d.delta=="string"&&(yield{type:"response.output_text.delta",item_id:n,delta:d.delta}),typeof d.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:n,delta:d.reasoning}),Array.isArray(d.tool_calls))for(let c of d.tool_calls){let s=typeof c.index=="number"?c.index:0,a=o.get(s)??{};c.id&&(a.id=c.id),c.function?.name&&(a.name=c.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:c.function?.arguments??""}}}else if(d.t==="response"){yield{type:"response.completed",response:{id:n,status:"completed",...d.body}};return}else if(d.t==="error"){yield f(d);return}}}var ne="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(ne).set({requests:{[t.request_id]:{payload:t,consumer_id:t.consumer_id,stream:t.stream}}})}sendResponses(t,r){let n=L(t,r,this.consumerId);return this.write(n),S(this.session,r)}sendResponseCreate(t,r,n){let o=I(t,r,n,this.consumerId);return this.write(o),S(this.session,n)}};var O=class{constructor(t,r){this.backend=t;this.opts=r}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 r=Buffer.from(t,"base64"),n=Math.floor(r.byteLength/2),o=new Int16Array(n);for(let i=0;i<n;i++)o[i]=r.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("rt-audio-out").track is null)');this.sink=this.backend.createSink(t),this.sink.onframe=r=>{let n=Buffer.from(r.samples.buffer,r.samples.byteOffset,r.samples.byteLength).toString("base64");for(let o of this.outHandlers)o(n)}}onOutputAudio(t){return this.outHandlers.add(t),()=>this.outHandlers.delete(t)}};async function V(e,t,r=1e4){let n=new O(t,{sampleRate:24e3}),o=await n.startOutbound();await e.stream("rt-audio-in").attach(o);let i=await oe(e,r);return await n.startInbound(i),n}function oe(e,t){let r=e.stream("rt-audio-out");return r.track!=null?Promise.resolve(r.track):new Promise((n,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),c=s=>{s!=null&&(clearTimeout(d),i?.(),n(s))};i=r.on?.("track",c),r.track!=null&&c(r.track)})}var se=24e3,y=480,ie=111;async function ae(){let e=await import("opusscript"),t=e.default??e;return new t(se,1,2048)}async function U(){let e=await import("werift"),t=await ae();return{createSource:()=>{let r=new e.MediaStreamTrack({kind:"audio"}),n=Math.random()*4294967295>>>0,o=Math.random()*65535&65535,i=Math.random()*4294967295>>>0,d=new Int16Array(0);return{track:r,onData:c=>{let s=new Int16Array(d.length+c.length);s.set(d,0),s.set(c,d.length);let a=0;for(;s.length-a>=y;){let u=s.subarray(a,a+y);a+=y;let l=Buffer.from(u.buffer,u.byteOffset,u.byteLength),Y=t.encode(l,y);o=o+1&65535,i=i+y>>>0;let G=new e.RtpHeader({version:2,payloadType:ie,sequenceNumber:o,timestamp:i,ssrc:n,marker:!1}),J=new e.RtpPacket(G,Y);r.writeRtp(J)}d=s.slice(a)}}},createSink:r=>{let n={onframe:null};return r.onReceiveRtp.subscribe(i=>{let d;try{d=t.decode(i.payload)}catch{return}let c=new Int16Array(d.length/2);for(let s=0;s<c.length;s++)c[s]=d.readInt16LE(s*2);n.onframe?.({samples:c})}),n}}}async function P(){let e=await import("@roamhq/wrtc");return{createSource:()=>{let t=new e.nonstandard.RTCAudioSource;return{track:t.createTrack(),onData:n=>t.onData({samples:n,sampleRate:24e3})}},createSink:t=>{let r={onframe:null},n=new e.nonstandard.RTCAudioSink(t);return n.ondata=o=>{r.onframe?.(o)},r}}}var D=0;function de(){return D+=1,`rt_${Date.now().toString(36)}_${D.toString(36)}`}var k=class{constructor(t,r={}){this.session=t;this.opts=r;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 V(this.session,this.opts.audioBackend),this.audio.onOutputAudio(t=>this.emit({type:"response.audio.delta",delta:t}))}on(t,r){return this.handlers.add(r),()=>this.handlers.delete(r)}emit(t){for(let r of this.handlers)r(t)}send(t){switch(t.type){case"conversation.item.create":{let r=(t.item?.content??[]).map(n=>n.text??"").join("");this.conversation.push({role:t.item?.role??"user",content:r});return}case"response.create":{let r=de();this.currentRequestId=r;let n=this.transport.sendResponseCreate({type:"response.create",response:t.response},this.conversation,r);this.active=(async()=>{try{for await(let o of n)this.emit(o)}catch(o){this.emit(f(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 C=0;function ce(){return C+=1,`req_${Date.now().toString(36)}_${C.toString(36)}`}var _=class{transport;constructor(t){this.transport=new g(t)}responses={create:async t=>{let r=ce(),n=this.transport.sendResponses(t,r);return Object.assign((async function*(){yield*n})(),{requestId:r})}}};async function ue(e){let r=await(e.fetchImpl??fetch)(`${e.catalogUrl}/model_catalog?select=model_id,variant`,{headers:{apikey:e.anonKey,Authorization:`Bearer ${e.anonKey}`}});if(!r.ok)throw new Error(`model_catalog fetch failed: ${r.status}`);return await r.json()}async function B(e){return{object:"list",data:(await ue(e)).map(r=>({id:`${r.model_id}:${r.variant}`,object:"model",created:0,owned_by:"urun"}))}}var m=require("@urun-sh/core"),w="rt-video-out",R="urun.videoOut.v1",E="h264";function M(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 $(e,t){if(M(e,"encodeVideoOutRecord"),t.byteLength===0)throw new Error("encodeVideoOutRecord: refusing to encode an empty access unit");let r=new TextEncoder().encode(JSON.stringify({v:1,seq:e.seq,ptsUs:e.ptsUs,codec:e.codec,key:e.key}));if(r.byteLength>65535)throw new Error(`encodeVideoOutRecord: header JSON is ${r.byteLength} bytes \u2014 over the u16 length prefix`);let n=new Uint8Array(2+r.byteLength+t.byteLength);return n[0]=r.byteLength&255,n[1]=r.byteLength>>8,n.set(r,2),n.set(t,2+r.byteLength),n}function v(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 r;try{r=JSON.parse(new TextDecoder().decode(e.subarray(2,2+t)))}catch(d){throw new Error(`decodeVideoOutRecord: header is not valid JSON (${String(d)})`)}let n=r;if(n===null||typeof n!="object"||n.v!==1)throw new Error(`decodeVideoOutRecord: header is not spec v1 (v=${JSON.stringify(n?.v)}) \u2014 a newer producer must be met with a newer consumer, never silently misread`);let o=n;M(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 N(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 r=await t.call(e),n=r.sessionId??e.sessionId;if(typeof n!="string"||n.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:r.url,certHashes:r.certHashes,token:r.token,sessionId:n,stream:w,codec:E,framing:R}}var W="urun.videoOut.v2-ws";function H(e){let t=e.stream(w);if(typeof t.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${w}') 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 r=new Set,n=new Set,o=new Set,i=s=>{for(let a of n)a(s)},d=!1,c=async()=>{for await(let s of t.messages()){if(!(s instanceof Uint8Array)){i(new Error(`openSessionVideoOutLane: "${w}" delivered a non-BINARY \xA75 payload (${typeof s}) \u2014 spec v1 records ride BINARY envelopes only`));continue}let a;try{a=v(s)}catch(u){i(u instanceof Error?u:new Error(String(u)));continue}for(let u of r)u(a)}for(let s of o)s()};return{codec:E,onOutputFrame:s=>(r.add(s),d||(d=!0,c().catch(a=>i(a instanceof Error?a:new Error(String(a))))),()=>r.delete(s)),onLaneError:s=>(n.add(s),()=>n.delete(s)),onEnd:s=>(o.add(s),()=>o.delete(s))}}function pe(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 r=0;r<t.length;r++)t[r]=parseInt(e.slice(r*2,r*2+2),16);return t}async function q(e,t,r){let n=r??(typeof globalThis.WebTransport=="function"?(a,u)=>new globalThis.WebTransport(a,u):void 0);if(!n)throw new Error("consumeVideoOut: no WebTransport implementation \u2014 this platform has no globalThis.WebTransport; inject a connect factory (the third argument)");let o=n(e.url,{serverCertificateHashes:e.certHashes.map(a=>({algorithm:"sha-256",value:pe(a)}))});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 d=i.readable.getReader(),c=new m.WtControlDecoder,s=!1;for(;!s;){let{value:a,done:u}=await d.read();if(u)throw new Error("consumeVideoOut: gateway closed the control stream before hello-ack");if(a)for(let l of c.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,m.consumeWtDataStreams)(o.incomingUnidirectionalStreams,{onData:(a,u)=>{if(a===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(v(u))}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 F(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 z=Object.create;var h=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var K=Object.getOwnPropertyNames;var X=Object.getPrototypeOf,Z=Object.prototype.hasOwnProperty;var ee=(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 K(t))!Z.call(e,o)&&o!==n&&h(e,o,{get:()=>t[o],enumerable:!(r=Q(t,o))||r.enumerable});return e};var v=(e,t,n)=>(n=e!=null?z(X(e)):{},x(t||!e||!e.__esModule?h(n,"default",{value:e,enumerable:!0}):n,e)),te=e=>x(h({},"__esModule",{value:!0}),e);var le={};ee(le,{RT_VIDEO_OUT_STREAM:()=>w,SUPPORTED_REALTIME_CLIENT_EVENTS:()=>A,SUPPORTED_RESPONSES_EVENTS:()=>T,UrunRealtime:()=>k,UrunResponses:()=>_,VIDEO_OUT_DEFAULT_CODEC:()=>b,VIDEO_OUT_FRAMING:()=>R,VIDEO_OUT_WS_FRAMING:()=>W,consumeVideoOut:()=>q,decodeVideoOutRecord:()=>E,encodeVideoOutRecord:()=>$,isSupportedClientEvent:()=>F,isSupportedResponsesEvent:()=>j,listModels:()=>B,negotiateSessionVideoOutQuic:()=>N,nodeAudioBackend:()=>P,openSessionVideoOutLane:()=>H,toErrorEvent:()=>m,weriftAudioBackend:()=>U});module.exports=te(le);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),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??{};return{type:"error",error:{type:"urun_error",code:t.code??null,message:n.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}var ne="llm-resp";async function*S(e,t){let n=`${ne}:${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 re="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(re).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 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("rt-audio-out").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 V(e,t,n=1e4){let r=new O(t,{sampleRate:24e3}),o=await r.startOutbound();await e.stream("rt-audio-in").attach(o);let i=await oe(e,n);return await r.startInbound(i),r}function oe(e,t){let n=e.stream("rt-audio-out");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 se=24e3,y=480,ie=111;async function ae(){let e=await import("opusscript"),t=e.default??e;return new t(se,1,2048)}async function U(){let e=await import("werift"),t=await ae();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>=y;){let c=s.subarray(a,a+y);a+=y;let l=Buffer.from(c.buffer,c.byteOffset,c.byteLength),Y=t.encode(l,y);o=o+1&65535,i=i+y>>>0;let G=new e.RtpHeader({version:2,payloadType:ie,sequenceNumber:o,timestamp:i,ssrc:r,marker:!1}),J=new e.RtpPacket(G,Y);n.writeRtp(J)}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 P(){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 D=0;function de(){return D+=1,`rt_${Date.now().toString(36)}_${D.toString(36)}`}var k=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 V(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=de();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 C=0;function ue(){return C+=1,`req_${Date.now().toString(36)}_${C.toString(36)}`}var _=class{transport;constructor(t){this.transport=new g(t)}responses={create:async t=>{let n=ue(),r=this.transport.sendResponses(t,n);return Object.assign((async function*(){yield*r})(),{requestId:n})}}};async function ce(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 ce(e)).map(n=>({id:`${n.model_id}:${n.variant}`,object:"model",created:0,owned_by:"urun"}))}}var f=require("@urun-sh/core"),w="rt-video-out",R="urun.videoOut.v1",b="h264";function M(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 $(e,t){if(M(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;M(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 N(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:w,codec:b,framing:R}}var W="urun.videoOut.v2-ws";function H(e){let t=e.stream(w);if(typeof t.messages!="function")throw new Error(`openSessionVideoOutLane: session.stream('${w}') 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: "${w}" 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 pe(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 q(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:pe(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 F(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});