@urun-sh/openai 0.5.4 → 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.
@@ -0,0 +1,513 @@
1
+ import { C as CatalogRow } from './models-NYMZrklp.cjs';
2
+ import { IncomingMessage } from 'node:http';
3
+ import { a as AudioBridge, h as VideoFrameLane, j as VideoOutLane } from './video-out-D20UuJ8G.cjs';
4
+
5
+ /**
6
+ * WHAT a proxy serves — the `identity` block on `GET /stats`. Reuse of a
7
+ * running proxy is allowed iff every field matches the launching invocation
8
+ * EXACTLY: a partial match (same app, different fn; same everything, older
9
+ * proxy_version) silently routes the agent at the wrong backend, which is
10
+ * worse than any error.
11
+ */
12
+ interface ProxyIdentity {
13
+ app: string;
14
+ org: string;
15
+ fn: string;
16
+ /**
17
+ * The control-plane URL the proxy's backhaul session opens against
18
+ * (URUN_BASE_URL verbatim — same org/app/fn on staging vs prod are
19
+ * DIFFERENT backends; review finding, #285). Compared exactly: a cosmetic
20
+ * difference (trailing slash) merely refuses reuse and spawns an ephemeral
21
+ * proxy — the safe direction.
22
+ */
23
+ base_url: string;
24
+ proxy_version: string;
25
+ }
26
+
27
+ /**
28
+ * THE OPENROUTER PROVIDER DOCUMENT — `GET /v1/models?format=openrouter`.
29
+ *
30
+ * OpenRouter's provider monitor polls this document (schema 2.4, per
31
+ * openrouter.ai/docs/guides/community/for-providers) to list a provider's
32
+ * models on the marketplace. The `id` field is the EXACT model identifier
33
+ * OpenRouter sends back as `model` on chat completions — which here is the
34
+ * app slug, the same id the OpenAI surface lists. One id space, two views.
35
+ *
36
+ * MODALITY GATING: only rows whose catalog `task` names a chat-completions
37
+ * shape (chat | code | agent | vl) are declared. A voice/video/image app
38
+ * behind a chat surface would stream garbage into OpenRouter's baseline
39
+ * tests; models the catalog cannot vouch for are OMITTED, never guessed.
40
+ * With no catalog oracle configured the document is `{"data": []}` — honest
41
+ * emptiness, not invented capability (the chart turns the oracle on).
42
+ *
43
+ * PRICING is deliberately omitted entirely: the schema's rule is "a modality
44
+ * with no pricing array is simply unpriced", OpenRouter configures pricing
45
+ * during provider onboarding, and the per-model price surface is
46
+ * urun-infra U3 (model_prices) — when that lands, this module grows a
47
+ * `prices` input and emits the input/output pricing arrays.
48
+ *
49
+ * The full closed-value-domain schema ships as OpenAPI 3.1 at
50
+ * openrouter.ai/docs/assets/provider-monitor-schema-v2.openapi.json.
51
+ */
52
+
53
+ type OpenRouterProviderDoc = {
54
+ data: OpenRouterProviderModel[];
55
+ };
56
+ interface OpenRouterProviderModel {
57
+ schema_version: '2.4';
58
+ /** The EXACT id OpenRouter sends back as `model` — the app slug. */
59
+ id: string;
60
+ name: string;
61
+ created: number;
62
+ /** Valid enum: int4|int8|fp4|mxfp4|nvfp4|fp6|fp8|mxfp8|fp16|bf16|fp32|null. */
63
+ quantization: string | null;
64
+ description: string;
65
+ hugging_face_id: string;
66
+ input_modalities: Array<Record<string, unknown>>;
67
+ output_modalities: Array<Record<string, unknown>>;
68
+ }
69
+
70
+ /**
71
+ * Per-model → per-app routing for the compat proxy (owner directive
72
+ * 2026-08-07): swapping the model in a coding harness routes the request to
73
+ * the org's DEPLOYED app for that model. v1 is deployed-only — a uRun model
74
+ * that is not deployed gets a loud 404 naming `urun serve <id>`; a later
75
+ * phase (explicitly out of scope here; urun-infra#1490 shared-endpoints)
76
+ * auto-creates from the model catalog on first request.
77
+ *
78
+ * MODEL-ID SURFACE (the documented mapping): a model may be named by
79
+ * - the app slug itself ("qwen3-6-27b-bf16"), or
80
+ * - the catalog id ("qwen3.6-27b"), or
81
+ * - the catalog id:variant ("qwen3.6-27b:bf16"),
82
+ * where slugification mirrors urun-cli `serve.py _default_app_name` exactly:
83
+ * lowercase, every non-alphanumeric-non-dash character becomes "-", leading/
84
+ * trailing dashes stripped (catalog id "qwen3.6-27b" + variant "bf16" → app
85
+ * "qwen3-6-27b-bf16"). COLLISION RULE: an exact slug match always wins over
86
+ * the catalog-id (prefix) interpretation.
87
+ *
88
+ * RESOLUTION ORDER (one canonical path, documented end to end):
89
+ * 1. model absent / "urun" / an alias of the startup app → DEFAULT app.
90
+ * 2. exact slug match on a deployed serve app → that app.
91
+ * 3. catalog-id form matching exactly one deployed app → that app
92
+ * (two or more candidates → loud ambiguity error naming them).
93
+ * 4. the name maps to an org app that is NOT an active app exposing the
94
+ * proxy's serve function → loud 404
95
+ * naming `urun serve <model>` and the available models.
96
+ * 5. the name matches a catalog row but no deployed app → loud 404
97
+ * naming `urun serve <id>` (deployed-only v1).
98
+ * 6. anything else — a model name outside the uRun namespace entirely
99
+ * (e.g. the harness's own upstream default, "claude-*"/"gpt-*") →
100
+ * DEFAULT app. This IS today's single-app contract, kept deliberately
101
+ * so `urun compat <agent>` with the agent's stock model keeps working
102
+ * with zero new env; the per-model /stats table records every such
103
+ * mapping so it is visible, never silent. Models the proxy ADVERTISES
104
+ * on /v1/models can never land here — they resolve (2/3) or fail loud
105
+ * (4/5) above.
106
+ *
107
+ * NO-DEFAULT MODE (`defaultApp: null`) — the HOSTED multi-tenant lane
108
+ * (`src/hosted/`): a shared endpoint serving every org has no "the app this
109
+ * proxy was started for", so rules 1 and 6 have nothing to fall back TO.
110
+ * Rather than inventing one (picking "some" app for a caller would be the
111
+ * worst kind of silent divergence), both rules become the SAME loud
112
+ * {@link UnknownModelError} that rules 4/5 already raise: name a deployed
113
+ * model, here is the list. Rules 2–5 are byte-for-byte the local behavior —
114
+ * one router, one resolution order, two configurations.
115
+ */
116
+
117
+ /** One org app row from `GET {orgApi}/apps` (urun-cli `ApiClient.list_apps`). */
118
+ interface DeployedApp {
119
+ app_slug: string;
120
+ function_name?: string | null;
121
+ deployment_status?: string | null;
122
+ [k: string]: unknown;
123
+ }
124
+ /**
125
+ * A model that does not resolve to a deployed app the proxy may serve.
126
+ * Rendered by the server in each lane's NATIVE error format as a 404 — never
127
+ * silently served by the default app.
128
+ */
129
+ declare class UnknownModelError extends Error {
130
+ }
131
+ /**
132
+ * A session handle names a pooled session that no longer exists (closed,
133
+ * evicted after its pod died, replaced by a re-home, or the proxy restarted).
134
+ * The caller asked to REATTACH that exact session — opening a fresh one and
135
+ * calling it "resumed" would be a silent lie, so this is always loud.
136
+ */
137
+ declare class SessionGoneError extends Error {
138
+ }
139
+ /**
140
+ * OpenAI-shaped model list (same shape as models.ts listModels). When the
141
+ * catalog oracle is configured, each entry ALSO carries the catalog
142
+ * enrichment fields (additive JSON — OpenAI clients ignore unknown fields;
143
+ * the OpenRouter + Vercel AI Gateway provider listings need them). Entries
144
+ * whose slug matches no catalog row stay at the bare four fields.
145
+ */
146
+ interface RouterModelEntry {
147
+ id: string;
148
+ object: 'model';
149
+ created: number;
150
+ owned_by: string;
151
+ /** Display name — the canonical `<model_id>:<variant>` catalog ref. */
152
+ name?: string;
153
+ /** User-facing description from the catalog (console Endpoints copy). */
154
+ description?: string;
155
+ /** Catalog modality (chat | code | agent | vl | audio | image | ...). */
156
+ task?: string;
157
+ /** Serving engine (vllm | sglang | llamacpp | ...). */
158
+ engine?: string;
159
+ /** The catalog lane this app deploys, e.g. 'rtx6000:1'. */
160
+ gpu_spec?: string;
161
+ /** Context window in tokens (chat rows carry it in engine_args). */
162
+ context_length?: number;
163
+ }
164
+ interface RouterModelList {
165
+ object: 'list';
166
+ data: RouterModelEntry[];
167
+ }
168
+ interface ModelRouterOptions<S> {
169
+ /**
170
+ * The startup app slug (URUN_APP) — the DEFAULT model — or `null` for the
171
+ * hosted multi-tenant lane, which has no per-proxy default app: there,
172
+ * every request must NAME a deployed model and an unnamed/unknown one
173
+ * fails loud instead of silently landing somewhere (see the module header,
174
+ * "NO-DEFAULT MODE").
175
+ */
176
+ defaultApp: string | null;
177
+ /** The serve function name every routed app must expose (URUN_FUNCTION). */
178
+ fnName: string;
179
+ /** Open a backhaul session for an app slug (called at most once per app). */
180
+ openSession: (appSlug: string) => S | Promise<S>;
181
+ /** Terminal release for one pool entry (Session.end() underneath). */
182
+ closeSession: (entry: S) => Promise<void>;
183
+ /**
184
+ * List the org's deployed apps, or null when the credentials cannot
185
+ * (URUN_JWT lane: the pre-vended token is scoped to the default app, so
186
+ * there is no org listing AND no cross-app session — routing degrades to
187
+ * the default-app-only contract, which is exactly today's behavior).
188
+ */
189
+ listApps: (() => Promise<DeployedApp[]>) | null;
190
+ /**
191
+ * Catalog rows (models.ts fetchCatalogRows) as the uRun-namespace oracle
192
+ * for rule 5 AND the /v1/models enrichment + OpenRouter provider-doc
193
+ * source, or null when catalog access is not configured. Optional fields
194
+ * beyond model_id/variant are tolerated (thin rows still typecheck).
195
+ */
196
+ listCatalog: (() => Promise<CatalogRow[]>) | null;
197
+ /** Deployed-apps cache TTL (the list changes on deploys, not per request). */
198
+ appsTtlMs?: number;
199
+ /**
200
+ * The stable NATIVE identity of one pooled entry (the uRun session id in
201
+ * the proxy wiring — the same identity the serve-side session-affinity tag
202
+ * rides, urun-python#1556). Powers the session-identity seam
203
+ * ({@link ModelRouter.handleFor} / {@link ModelRouter.sessionForHandle});
204
+ * a router without it fails LOUD on those calls, never approximates.
205
+ */
206
+ sessionKey?: (entry: S) => string;
207
+ }
208
+ /**
209
+ * The image-capability modes the Images lane gates on (images.ts's
210
+ * ImageCapabilityMode — declared here because the router is the LOWER layer:
211
+ * the lane's gate type stays structurally satisfied by
212
+ * {@link ModelRouter.supportsImages} with no router→lane import).
213
+ */
214
+ type ImageCapabilityMode = 'generation' | 'edit';
215
+ /**
216
+ * The session pool: one backhaul session per deployed app, keyed by app slug,
217
+ * opened lazily on the first request that routes to it and reused for every
218
+ * subsequent one. The startup app is seeded eagerly by the CLI. Sessions
219
+ * close on proxy shutdown via {@link closeAll}; there is NO idle-close policy
220
+ * (deliberate v1 simplification — noted as a follow-up in the PR).
221
+ */
222
+ declare class ModelRouter<S> {
223
+ private readonly opts;
224
+ private readonly pool;
225
+ private appsCache;
226
+ private rowsCache;
227
+ constructor(opts: ModelRouterOptions<S>);
228
+ /** Seed an already-open session (the CLI's eagerly-opened startup app). */
229
+ seed(appSlug: string, entry: S): void;
230
+ private deployedApps;
231
+ /**
232
+ * Catalog rows with the same TTL discipline as {@link deployedApps} (the
233
+ * catalog changes on reseed migrations, not per request). Null when no
234
+ * oracle is configured — callers degrade to the bare four-field entries.
235
+ */
236
+ private catalogRows;
237
+ /** Apps this proxy may serve: active AND exposing the serve function. */
238
+ private servable;
239
+ private availableIds;
240
+ /**
241
+ * NO-DEFAULT MODE's terminal for rules 1 and 6: there is no app to fall
242
+ * back to, so say so loudly and list what the CALLER'S org actually has.
243
+ * Never returns.
244
+ */
245
+ private noDefaultApp;
246
+ /**
247
+ * Resolve a request's `model` to an app slug — the documented resolution
248
+ * order from the module header. Throws {@link UnknownModelError} for a uRun
249
+ * model that is not deployed (rules 4/5).
250
+ */
251
+ resolveApp(model: string | undefined): Promise<string>;
252
+ /** The pooled session for a model — opened lazily, reused afterwards. */
253
+ sessionFor(model: string | undefined): Promise<{
254
+ app: string;
255
+ entry: S;
256
+ }>;
257
+ private keyOf;
258
+ /**
259
+ * SESSION-IDENTITY SEAM (a): the opaque stable handle for the pooled
260
+ * session currently serving `model`'s turns. Rides the SAME acquisition
261
+ * path as every request ({@link sessionFor}) — the session opens lazily if
262
+ * this model has none yet — and derives the handle from native identity
263
+ * (app slug + uRun session id), zero bespoke bookkeeping.
264
+ */
265
+ handleFor(model: string | undefined): Promise<{
266
+ app: string;
267
+ handle: string;
268
+ }>;
269
+ /**
270
+ * SESSION-IDENTITY SEAM (b): the exact pooled session a handle names.
271
+ * NEVER opens a fresh session — a handle whose session is gone (closed,
272
+ * evicted, re-homed to a replacement, proxy restarted) or malformed throws
273
+ * {@link SessionGoneError} loudly. Resume is reattach-or-fail, not
274
+ * reattach-or-quietly-restart.
275
+ */
276
+ sessionForHandle(handle: string): Promise<{
277
+ app: string;
278
+ entry: S;
279
+ }>;
280
+ /**
281
+ * Drop ONE pooled session whose backhaul died (its pod was restarted /
282
+ * drained / deleted) and release it — the next {@link sessionFor} opens a
283
+ * fresh one, i.e. asks the control plane for a new assignment. Used by the
284
+ * one-shot re-home (rehome.ts, urun-sh/urun-python#1592).
285
+ *
286
+ * IDENTITY-GUARDED (the same rule the pi lane's SessionPool follows): a
287
+ * concurrent request that already re-homed this app has put a NEWER entry
288
+ * under the key, and evicting that would close a healthy session out from
289
+ * under it.
290
+ */
291
+ evict(app: string, entry: S): Promise<void>;
292
+ /**
293
+ * `GET /v1/models`: the org's deployed serve apps as model entries, the
294
+ * default app FIRST. On the JWT lane (no org listing) this is the default
295
+ * app plus any app already in the pool — the gap is called out loudly in
296
+ * the PR, not papered over here.
297
+ */
298
+ modelList(): Promise<RouterModelList>;
299
+ /**
300
+ * The OpenRouter PROVIDER document (`GET /v1/models?format=openrouter`):
301
+ * schema 2.4 per openrouter.ai/docs/guides/community/for-providers. Only
302
+ * chat-completions-shaped models are declared (task chat | code | agent |
303
+ * vl — a voice or video app behind a chat surface would stream garbage);
304
+ * models the catalog cannot vouch for are OMITTED, never guessed. Pricing
305
+ * is deliberately omitted entirely (schema rule: "A modality with no
306
+ * pricing array is simply unpriced" — OpenRouter configures pricing during
307
+ * provider onboarding; the per-model price surface is urun-infra U3).
308
+ */
309
+ openRouterModels(): Promise<OpenRouterProviderDoc>;
310
+ /**
311
+ * IMAGE CAPABILITY GATE (the Images lane's `imageModels` oracle, images.ts):
312
+ * is `model`'s deployed app a native destination for `mode`? Authorized
313
+ * resolution ({@link resolveApp} — undeployed uRun models throw
314
+ * {@link UnknownModelError}, which the lane renders as a 404) then the
315
+ * SAME catalog join as the /v1/models enrichment (PR421's rowForSlug):
316
+ * exact `<model_id>-<variant>` slug across ALL its GPU-placement rows, a
317
+ * bare model_id only when one variant owns it.
318
+ *
319
+ * Capability is read ONLY from catalog modality metadata, never from the
320
+ * serve function name: every row must carry task 'image' INVARIANTLY across
321
+ * the placements, and the mode comes from engine_args.expects_image —
322
+ * false ⇒ pure text-to-image ('generation'; diffusers v0.38.0
323
+ * pipeline_qwenimage_edit_plus raises torch.cat on an EMPTY image list, so
324
+ * an edit destination is never generation-capable); true or ABSENT (the
325
+ * native default) ⇒ 'edit'. The consensus rule is per-placement:
326
+ * capability must agree across every placement — a CONFLICTING flag (mixed
327
+ * true/false/absent) or a non-boolean one is a refused capability, never
328
+ * silently defaulted to edit.
329
+ *
330
+ * A CONFIGURED catalog oracle that FAILS propagates its rejection — the
331
+ * gate never fabricates a `false` (which would render as a misleading 404)
332
+ * out of an infrastructure outage. No oracle configured, no matching row,
333
+ * or a slug the catalog cannot vouch for ⇒ false (loud 404 upstream).
334
+ */
335
+ supportsImages(model: string | undefined, mode: ImageCapabilityMode): Promise<boolean>;
336
+ /** Close every pooled session (Session.end() underneath) — proxy shutdown. */
337
+ closeAll(): Promise<void>;
338
+ }
339
+
340
+ /**
341
+ * The shared Responses execution/store seam — the ONE execution + event-
342
+ * envelope path and the authorized tenant store, imported by BOTH the HTTP
343
+ * SSE lane (server.ts) and the `/v1/responses` WebSocket lane
344
+ * (responses-ws.ts). This module deliberately imports NO transport acceptor
345
+ * and NO HTTP lane module: its only imports are type-only, so the module
346
+ * graph server.ts → responses-ws.ts → responses-turn.ts is acyclic and no
347
+ * Vitest/Bun/Node module-evaluation order can observe an uninitialized
348
+ * binding (the constructor-failure class of bug this extraction removes).
349
+ */
350
+
351
+ /** The request envelope every Responses-shaped upstream call carries. */
352
+ interface ResponsesCreateParams {
353
+ model?: string;
354
+ input: unknown;
355
+ stream?: boolean;
356
+ tools?: unknown;
357
+ tool_choice?: unknown;
358
+ temperature?: number;
359
+ max_output_tokens?: number;
360
+ /**
361
+ * System/developer instructions, forwarded to the serve envelope — where
362
+ * they become ONE prepended `system`-role message (the protocol's native
363
+ * per-request system input; there is NO envelope-level instructions field).
364
+ * Typed `string` end to end (transport/encode.ts) so no cast bridges the seam.
365
+ */
366
+ instructions?: string;
367
+ top_p?: number;
368
+ stop?: string[];
369
+ /**
370
+ * Reasoning controls (urun-python #1667): forwarded VERBATIM to the serve
371
+ * envelope — `chat_template_kwargs.enable_thinking:false` is the think-off
372
+ * switch. Absent -> absent (no default injection; server-side validation).
373
+ */
374
+ reasoning_effort?: string;
375
+ chat_template_kwargs?: Record<string, unknown>;
376
+ }
377
+ /** How one pinned session ended (the native phase machinery's terminal step). */
378
+ interface SessionEndInfo {
379
+ /**
380
+ * Milliseconds until the session's native deadline (`endsAt`), or null when
381
+ * the app declared no maximum session length. NOTE (missing primitive,
382
+ * called out in the PR): core exposes no PRE-expiry notice event — this
383
+ * callback fires AT terminal loss, so timeLeftMs is ~0 on expiry.
384
+ */
385
+ timeLeftMs: number | null;
386
+ /** The terminal reason (expired / ended / error), for the loud close. */
387
+ reason: string;
388
+ }
389
+ /** The upstream calls the proxy makes — injectable (tests; alt transports). */
390
+ interface ProxyClients {
391
+ /** `UrunResponses(session).responses.create` — an async iterable of Responses stream events. */
392
+ createResponse(params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
393
+ /** `listModels(...)` result (an OpenAI model list object). */
394
+ listModels(): Promise<unknown>;
395
+ /**
396
+ * The OpenRouter provider document (`GET /v1/models?format=openrouter`).
397
+ * Optional at the seam: hand-built test clients may omit it, in which case
398
+ * the format=openrouter branch answers 501 — loud, never a silent empty.
399
+ */
400
+ openRouterModels?(): Promise<unknown>;
401
+ /**
402
+ * Open (or reuse) the NATIVE audio lanes on the pooled session `model` routes
403
+ * to — the same first-party machinery as RealtimeClient.enableAudio
404
+ * (transport/media.ts `enableSessionAudio`: AudioBridge over the session's
405
+ * `rt-audio-in`/`rt-audio-out` stream lanes at 24 kHz). One lane per pooled
406
+ * session; repeat calls return the same lane. Optional at the seam because
407
+ * text-only embeddings exist — but a surface that RECEIVES audio while the
408
+ * embedder wired no `openAudio` must fail LOUD, never drop chunks.
409
+ */
410
+ openAudio?(model: string | undefined): Promise<ProxyAudioLane>;
411
+ /**
412
+ * Open (or reuse) the NATIVE video FRAME lane on the pooled session `model`
413
+ * routes to (transport/media.ts `enableSessionVideo`: discrete JPEG frames →
414
+ * `stream('rt-video-in').emit`, the §5 named-DATA image-bytes path — NOT the
415
+ * RTP media plane, which requires an already-H.264-encoded track). One lane
416
+ * per pooled session; repeat calls return the same lane. Optional at the seam
417
+ * because text-only embeddings exist — but a surface that RECEIVES video
418
+ * frames while the embedder wired no `openVideo` must fail LOUD, never drop
419
+ * frames.
420
+ */
421
+ openVideo?(model: string | undefined): Promise<ProxyVideoLane>;
422
+ /**
423
+ * Open (or reuse) the NATIVE video OUTPUT lane on the pooled session
424
+ * `model` routes to (transport/video-out.ts `openSessionVideoOutLane`):
425
+ * decoded spec-v1 records consumed from the session's named §5
426
+ * `rt-video-out` downstream. The Live adapter fans each record onto the
427
+ * SAME Bidi WS as an extension server message (spec v2, WS-inline) — no
428
+ * side-channel transport. Optional at the seam; an ABSENT seam means the
429
+ * `urun.videoOut` capability is refused by omission in setupComplete
430
+ * (vanilla parity), while a wired seam that cannot open the lane must
431
+ * throw LOUD — never a quiet downgrade.
432
+ */
433
+ openVideoOut?(model: string | undefined): Promise<ProxyVideoOutLane>;
434
+ /**
435
+ * SESSION-IDENTITY SEAM (ModelRouter.handleFor): the opaque stable handle
436
+ * for the pooled session currently serving `model`'s turns. Derived from
437
+ * native identity (app slug + uRun session id) — the same identity the
438
+ * serve-side session-affinity tag rides (urun-python#1556/#1582).
439
+ */
440
+ sessionHandle(model: string | undefined): Promise<string>;
441
+ /**
442
+ * createResponse PINNED to the exact session a handle names
443
+ * (ModelRouter.sessionForHandle). Throws SessionGoneError LOUDLY when that
444
+ * session is gone or was replaced — never silently opens a fresh session
445
+ * while claiming resume. Deliberately NO re-home on this path: re-homing
446
+ * would swap the pinned session out from under the caller.
447
+ */
448
+ createResponseOn(handle: string, params: ResponsesCreateParams): Promise<AsyncIterable<unknown>> | AsyncIterable<unknown>;
449
+ /**
450
+ * Subscribe to the pinned session's terminal end via core's NATIVE phase
451
+ * machinery (Session.onPhase → terminal 'expired'/'ended'/'error'). Fires
452
+ * `cb` once. Throws SessionGoneError if the handle's session is already
453
+ * gone — which doubles as the loud reattach check at resume time. Returns
454
+ * the unsubscribe.
455
+ */
456
+ onSessionEnd(handle: string, cb: (end: SessionEndInfo) => void): Promise<() => void>;
457
+ }
458
+ /**
459
+ * The audio lane handle `openAudio` returns — structurally the transport
460
+ * AudioBridge (media.ts): base64 PCM16 @24 kHz mono in both directions.
461
+ */
462
+ type ProxyAudioLane = Pick<AudioBridge, 'appendInputAudio' | 'onOutputAudio'>;
463
+ /**
464
+ * The video frame-lane handle `openVideo` returns — structurally the
465
+ * transport VideoFrameLane (media.ts): one raw encoded JPEG frame per call,
466
+ * input-only (Live-style protocols have no video OUT modality).
467
+ */
468
+ type ProxyVideoLane = Pick<VideoFrameLane, 'sendInputFrame'>;
469
+ /**
470
+ * The video-OUT lane handle `openVideoOut` returns — structurally the
471
+ * transport VideoOutLane (video-out.ts): the codec plus frame/error fan-out
472
+ * the WS-inline delivery subscribes.
473
+ */
474
+ type ProxyVideoOutLane = Pick<VideoOutLane, 'codec' | 'onOutputFrame' | 'onLaneError'>;
475
+ /**
476
+ * Per-request `ProxyClients` resolution — the seam the HOSTED multi-tenant
477
+ * server (`src/hosted/`) plugs into so ONE handler implementation serves
478
+ * every org: the hosted server resolves the caller's org from its Bearer API
479
+ * key and returns THAT org's backhaul. The local CLI passes a fixed
480
+ * `ProxyClients` instead; both go through the identical handler body.
481
+ *
482
+ * Throwing from here is the loud path: the thrown error surfaces in the
483
+ * lane's native error envelope (see {@link ProxyHandlerOptions.statusOf}).
484
+ */
485
+ type ProxyClientsFor = (req: IncomingMessage) => ProxyClients | Promise<ProxyClients>;
486
+ interface ProxyHandlerOptions {
487
+ /** A fixed backhaul (local CLI) or a per-request resolver (hosted server). */
488
+ clients: ProxyClients | ProxyClientsFor;
489
+ /** Optional bearer the local agent must present (never forwarded upstream). */
490
+ apiKey?: string;
491
+ /**
492
+ * WHAT this proxy serves (app/org/fn/base_url + proxy_version), surfaced as
493
+ * the `identity` block on `GET /stats` so a second `urun compat` invocation
494
+ * can reuse this proxy iff the identity matches its own exactly. Only the
495
+ * standalone `urun compat proxy` command sets it — a launch-mode proxy is
496
+ * child-owned (it dies with its child) and an embedder that omits it is
497
+ * simply never reused.
498
+ */
499
+ identity?: ProxyIdentity;
500
+ /**
501
+ * Map a thrown error onto an HTTP status + error `type` before the generic
502
+ * 500. The hosted server uses it to turn its auth/tenancy failures into a
503
+ * 401 in the OpenAI envelope. Returning null means "not mine" — the error
504
+ * takes the ordinary loud 500 path.
505
+ */
506
+ statusOf?: (err: unknown) => {
507
+ status: number;
508
+ openaiType: string;
509
+ anthropicType: string;
510
+ } | null;
511
+ }
512
+
513
+ export { ModelRouter as M, type ProxyClients as P, SessionGoneError as S, UnknownModelError as U, type ProxyHandlerOptions as a, type ProxyIdentity as b, type ProxyVideoOutLane as c };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urun-sh/openai",
3
- "version": "0.5.4",
3
+ "version": "0.5.5",
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.4"
73
+ "@urun-sh/core": "^0.5.5"
74
74
  },
75
75
  "dependencies": {
76
76
  "openai": "4.104.0",
@@ -1 +0,0 @@
1
- import{c as l}from"./chunk-YSFSRI3D.js";l();l();function p(s){return s.toLowerCase().replace(/[^\p{L}\p{N}-]/gu,"-").replace(/^-+|-+$/g,"")}l();var C={chat:!0,code:!0,agent:!0,vl:!0},y={int4:"int4",int8:"int8",fp4:"fp4",mxfp4:"mxfp4",nvfp4:"nvfp4",fp6:"fp6",fp8:"fp8",mxfp8:"mxfp8",fp16:"fp16",bf16:"bf16",fp32:"fp32"};function S(s){let e=s.toLowerCase();return Object.hasOwn(y,e)?y[e]:null}function g(s){let e=s.engine_args?.context_length;return typeof e=="number"&&Number.isFinite(e)&&e>0?e:null}function _(s,e){let t=e.filter(i=>p(`${i.model_id}-${i.variant}`)===s);if(t.length===1)return t[0];if(t.length>1)return v(t);let n=e.filter(i=>p(i.model_id)===s),o=new Set(n.map(i=>i.variant));return n.length>0&&o.size===1?v(n):null}function v(s){let e=s[0],t=r=>s.every(a=>(a[r]??void 0)===(e[r]??void 0))?e[r]:void 0,n=s.map(g),o=n[0],i=o!=null&&n.every(r=>r===o)?o:null;return{model_id:e.model_id,variant:e.variant,gpu_spec:t("gpu_spec"),engine:t("engine"),task:t("task"),description:t("description"),size_gb:t("size_gb"),hf_repo:t("hf_repo"),engine_args:i!==null?{context_length:i}:{},updated_at:t("updated_at")}}function b(s){let e=s.updated_at?Date.parse(s.updated_at):NaN;return Number.isFinite(e)?Math.floor(e/1e3):0}function R(s,e){let t=_(s,e);if(!t)return{id:s,object:"model",created:0,owned_by:"urun"};let n=g(t);return{id:s,object:"model",created:b(t),owned_by:"urun",name:`${t.model_id}:${t.variant}`,...t.description?{description:t.description}:{},...t.task?{task:t.task}:{},...t.engine?{engine:t.engine}:{},...t.gpu_spec?{gpu_spec:t.gpu_spec}:{},...n!==null?{context_length:n}:{}}}function x(s,e){let t=[];for(let n of s){let o=_(n,e);if(!o||!o.task||C[o.task]!==!0)continue;let i=g(o),r={temperature:{type:"range",min:0,max:2},top_p:{type:"range",min:0,max:1},stop:{type:"array",max_items:4}};i!==null&&(r.max_tokens={type:"integer",min:1,max:i,unit:"token"}),t.push({schema_version:"2.4",id:n,name:`${o.model_id}:${o.variant}`,created:b(o),quantization:S(o.variant),description:o.description??"",hugging_face_id:o.hf_repo?o.hf_repo.split("@")[0]??o.hf_repo:"",input_modalities:[{type:"text",...i!==null?{supported_inputs:{max_context_length:{value:i,unit:"token"}}}:{}},...o.task==="vl"?[{type:"image"}]:[]],output_modalities:[{type:"text",streaming:!0,supported_parameters:r}]})}return{data:t}}var I="https://api.urun.sh/v1",d=class extends Error{},u=class extends Error{},h="urs1.";function k(s,e){return h+Buffer.from(JSON.stringify([s,e]),"utf8").toString("base64url")}function P(s){if(!s.startsWith(h))return null;try{let e=JSON.parse(Buffer.from(s.slice(h.length),"base64url").toString("utf8"));if(Array.isArray(e)&&typeof e[0]=="string"&&typeof e[1]=="string")return{app:e[0],key:e[1]}}catch{}return null}async function F(s){let e=s.fetchImpl??fetch,t=`${s.apiUrl.replace(/\/+$/,"")}/apps`,n=await e(t,{headers:{Authorization:`Bearer ${s.apiKey}`,Accept:"application/json"}});if(!n.ok)throw new Error(`org apps listing failed: GET ${t} \u2192 ${n.status}`);let o=await n.json();if(!Array.isArray(o.apps))throw new Error(`org apps listing returned no "apps" array (GET ${t})`);if(o.truncated===!0)throw new Error(`org apps listing was truncated (GET ${t} returned ${o.apps.length} of more) \u2014 model discovery would silently omit deployed apps`);return o.apps}var A=6e4,$=class{constructor(e){this.opts=e}opts;pool=new Map;appsCache=null;rowsCache=null;seed(e,t){this.pool.set(e,Promise.resolve(t))}async deployedApps(){if(!this.opts.listApps)return[];let e=this.opts.appsTtlMs??A;if(this.appsCache&&Date.now()-this.appsCache.at<e)return this.appsCache.apps;let t=await this.opts.listApps();return this.appsCache={at:Date.now(),apps:t},t}async catalogRows(){if(!this.opts.listCatalog)return[];let e=this.opts.appsTtlMs??A;if(this.rowsCache&&Date.now()-this.rowsCache.at<e)return this.rowsCache.rows;let t=await this.opts.listCatalog();return this.rowsCache={at:Date.now(),rows:t},t}servable(e){return e.filter(t=>t.function_name===this.opts.fnName&&t.deployment_status==="active").map(t=>t.app_slug)}async availableIds(){let e=this.opts.defaultApp,t=new Set(e===null?[]:[e]);if(this.opts.listApps)for(let o of this.servable(await this.deployedApps()))t.add(o);for(let o of this.pool.keys())t.add(o);let n=[...t].filter(o=>o!==e).sort();return e===null?n:[e,...n]}async noDefaultApp(e){let t=await this.availableIds(),n=t.length>0?t.join(", "):"(none deployed)";throw new d(e===""?`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 "${e}" 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 resolveApp(e){let t=(e??"").trim(),n=this.opts.defaultApp;if(!t||t==="urun")return n===null?this.noDefaultApp(t):n;let o=p(t);if(o===n)return n;if(this.pool.has(o))return o;if(this.opts.listApps){let i=await this.deployedApps(),r=this.servable(i);if(r.includes(o))return o;let a=r.filter(c=>c.startsWith(`${o}-`));if(a.length===1)return a[0];if(a.length>1)throw new d(`model "${t}" is ambiguous across deployed apps ${a.join(", ")} \u2014 name the exact app slug (or catalog id:variant)`);let f=i.find(c=>c.app_slug===o||c.app_slug.startsWith(`${o}-`));if(f)throw new d(`model "${t}" maps to app "${f.app_slug}", which is not an active "${this.opts.fnName}" app \u2014 deploy it with \`urun serve ${t}\`; available models: ${(await this.availableIds()).join(", ")}`)}if(this.opts.listCatalog){let r=(await this.opts.listCatalog()).find(a=>t===a.model_id||t===`${a.model_id}:${a.variant}`||o===p(a.model_id)||o===p(`${a.model_id}-${a.variant}`));if(r)throw new d(`model "${t}" is in the uRun catalog but not deployed \u2014 deploy it with \`urun serve ${r.model_id}\`; available models: ${(await this.availableIds()).join(", ")}`)}return n===null?this.noDefaultApp(t):n}async sessionFor(e){let t=await this.resolveApp(e),n=this.pool.get(t);return n||(n=Promise.resolve(this.opts.openSession(t)),this.pool.set(t,n),n.catch(()=>this.pool.delete(t))),{app:t,entry:await n}}keyOf(e){let t=this.opts.sessionKey;if(!t)throw new Error("ModelRouter: sessionKey is not configured \u2014 the session-identity seam (handleFor/sessionForHandle) is unavailable on this router");let n=t(e);if(typeof n!="string"||n.length===0)throw new Error("ModelRouter: sessionKey returned an empty identity for a pooled session");return n}async handleFor(e){let{app:t,entry:n}=await this.sessionFor(e);return{app:t,handle:k(t,this.keyOf(n))}}async sessionForHandle(e){let t=P(e);if(!t)throw new u(`malformed session handle ${JSON.stringify(e)} \u2014 not issued by this proxy`);let{app:n,key:o}=t,i=this.pool.get(n),r=i?await i.then(a=>a,()=>null):null;if(r===null)throw new u(`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(r)!==o)throw new u(`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:r}}async evict(e,t){let n=this.pool.get(e);!n||await n.then(i=>i,()=>null)!==t||this.pool.get(e)!==n||(this.pool.delete(e),await this.opts.closeSession(t).catch(()=>{}))}async modelList(){let e=await this.availableIds(),t=await this.catalogRows().catch(()=>[]);return{object:"list",data:e.map(n=>R(n,t))}}async openRouterModels(){let e=await this.availableIds(),t=await this.catalogRows();return x(e,t)}async closeAll(){let e=[...this.pool.values()];this.pool.clear();let t=[];if(await Promise.all(e.map(async n=>{try{await this.opts.closeSession(await n)}catch(o){t.push(String(o instanceof Error?o.message:o))}})),t.length>0)throw new Error(`failed to close ${t.length} pooled session(s): ${t.join("; ")}`)}};export{I as a,d as b,u as c,F as d,$ as e};
@@ -1,6 +0,0 @@
1
- import{a as b}from"./chunk-K23AZPI4.js";import{a as E}from"./chunk-GBBY3PCZ.js";import{a as R,c as h,d as P,e as _}from"./chunk-4MSBS7M6.js";import{c as x,f as v,g as S,p as U}from"./chunk-CWJRDBDC.js";import{c as l}from"./chunk-YSFSRI3D.js";l();import{App as O,createClientToken as B}from"@urun-sh/core";l();function j(e){let n=e?.type;return n==="response.created"||n==="response.in_progress"}function D(e){let n=e?.type;return n==="response.completed"||n==="error"}function k(e){return async function(t){return(async function*(){for(let a=1;a<=2;a++){let{app:s,entry:r}=await e.sessionFor(t.model),i=!1,u=!1;try{let c=await e.createStream(r,t);for await(let d of c){if(D(d)&&(i=!0),j(d)){if(a>1)continue}else u=!0;yield d}}catch(c){if(a<2&&!u){await e.evict(s,r);continue}throw c}if(i)return;if(a<2&&!u){await e.evict(s,r);continue}return}})()}}l();var M=3,F="llm",W=new Set(["paused","error"]);function A(e,n){let t=e.doc(F),o=typeof t.onConnectionState=="function",a=typeof e.onPhase=="function";if(!o&&!a)throw new Error(`session for app "${n}" 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 s=null,r=new Set,i=[],u=c=>{if(!s){s=c;for(let d of[...r])d(c);r.clear()}};return o&&i.push(t.onConnectionState(c=>{c.consecutiveFailures<M||u(new h(`the pooled session for app "${n}" is gone: its doc backhaul failed ${c.consecutiveFailures} consecutive times (last close ${c.lastCloseCode===null?"by provider liveness reap":`code ${c.lastCloseCode}`}${c.lastCloseReason?`, reason ${JSON.stringify(c.lastCloseReason)}`:""}) \u2014 the platform closed the session (idle timeout or a serve-side crash). The entry is evicted; retry to get a fresh session.`))})),a&&i.push(e.onPhase(c=>{W.has(c.name)&&u(new h(`the pooled session for app "${n}" lost its compute binding (phase "${c.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:()=>s,onGone(c){return s?(c(s),()=>{}):(r.add(c),()=>r.delete(c))},dispose(){r.clear();for(let c of i.splice(0))c()}}}async function*g(e,n){let t=n.gone();if(t)throw t;let o=e[Symbol.asyncIterator](),a,s=new Promise((r,i)=>{a=n.onGone(u=>i(u))});s.catch(()=>{});try{for(;;){let r=await Promise.race([o.next(),s]);if(r.done)return;yield r.value}}finally{a?.(),o.return?.(void 0)}}function N(e,n,t){let o=e[t];if(typeof o!="function")throw new Error(`app "${n}" has no function "${t}" (set URUN_FUNCTION)`);let a=o();if(typeof a.end!="function")throw new Error(`app "${n}" function "${t}" returned a session without end()`);return a}function H(e,n){let{baseUrl:t,orgId:o,fnName:a,auth:s,subject:r}=e,i=p=>({session:p,responses:new E(p),gone:A(p,n)});if(s.lane==="jwt"){let p=O(n,{baseUrl:t,orgId:o,jwt:s.jwt});return i(N(p,n,a))}let{apiKey:u,gatewayUrl:c}=s,d=O(n,{baseUrl:t,orgId:o,getAccessToken:async()=>(await B(u,{baseUrl:c,expiresIn:300,allowedFunctions:[`${n}/${a}`],...r===void 0?{}:{subject:r}})).token});return i(N(d,n,a))}function K(e){let n=e.session.id;if(typeof n!="string"||n.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 n}var q=new Set(["error","paused"]);function xe(e){let{auth:n,catalog:t}=e,o=null,a=r=>{let i=H(e,r);return i.gone.onGone(()=>{o?.evict(r,i)}),i},s=new _({defaultApp:e.defaultApp,fnName:e.fnName,openSession:a,closeSession:async r=>{r.gone.dispose(),await r.session.end()},listApps:n.lane==="api-key"?()=>P({apiUrl:e.apiUrl,apiKey:n.apiKey}):null,listCatalog:t?()=>b(t):null,sessionKey:K});return o=s,e.defaultApp!==null&&s.seed(e.defaultApp,a(e.defaultApp)),s}function ve(e,n=S){let t=new WeakMap,o=new WeakMap,a=new WeakMap;return{createResponse:k({sessionFor:s=>e.sessionFor(s),evict:(s,r)=>e.evict(s,r),createStream:async(s,r)=>g(await s.responses.responses.create(r),s.gone)}),listModels:()=>e.modelList(),openRouterModels:()=>e.openRouterModels(),openAudio:async s=>{let{entry:r}=await e.sessionFor(s),i=t.get(r);return i||(i=(async()=>x(r.session,await n()))(),t.set(r,i),i.catch(()=>t.delete(r))),i},openVideo:async s=>{let{entry:r}=await e.sessionFor(s),i=o.get(r);return i||(i=v(r.session),o.set(r,i)),i},openVideoOut:async s=>{let{entry:r}=await e.sessionFor(s),i=a.get(r);return i||(i=U(r.session),a.set(r,i)),i},sessionHandle:async s=>(await e.handleFor(s)).handle,createResponseOn:async(s,r)=>{let{entry:i}=await e.sessionForHandle(s);return g(await i.responses.responses.create(r),i.gone)},onSessionEnd:async(s,r)=>{let{entry:i}=await e.sessionForHandle(s),u=i.session;if(typeof u.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let c=!1;return u.onPhase(d=>{if(c||!q.has(d.name))return;c=!0;let p=u.endsAt??null;r({timeLeftMs:p?Math.max(0,p.getTime()-Date.now()):null,reason:d.reason??`session ${d.name}`})})}}}l();import V from"os";import J from"path";import{fileURLToPath as X}from"url";import{format as z}from"util";var I="0.5.4";var C=4141,Q=I,Oe={claude:{bin:"claude",env:"anthropic"},codex:{bin:"codex",env:"openai"},pi:{bin:"pi",env:"openai"},omp:{bin:"omp",env:"openai"},aider:{bin:"aider",env:"openai"},opencode:{bin:"opencode",env:"openai"},crush:{bin:"crush",env:"openai"}};function Z(e,n="openai"){if(n==="anthropic")return{ANTHROPIC_BASE_URL:`http://127.0.0.1:${e}`,ANTHROPIC_API_KEY:"urun-local"};let t=`http://127.0.0.1:${e}/v1`;return{OPENAI_BASE_URL:t,OPENAI_API_BASE:t,OPENAI_API_KEY:"urun-local"}}function Ne(e,n,t){let o={...e,...Z(n,t)};return delete o.URUN_API_KEY,delete o.URUN_JWT,o}function Ie(e,n,t){let o=["-c","model_providers.urun.name=urun","-c",`model_providers.urun.base_url="http://127.0.0.1:${e}/v1"`,"-c",'model_providers.urun.wire_api="responses"',"-c",'model_provider="urun"'],a=t.indexOf("--"),s=a===-1?t:t.slice(0,a),r=s.some(d=>d==="-m"||d==="--model"||d.startsWith("--model=")),i=t[0]==="exec",u=s.includes("--skip-git-repo-check"),c=i&&!u?["exec","--skip-git-repo-check",...t.slice(1)]:t;return[...o,...r?[]:["-m",n],...c]}function $e(e,n){return n.some(o=>o==="--model"||o==="--provider"||o.startsWith("--model=")||o.startsWith("--provider="))?n:["--provider","urun","--model",e,...n]}function Ce(e,n){return n.some(o=>o==="--model"||o==="--provider"||o.startsWith("--model=")||o.startsWith("--provider="))?n:["--model",`urun/${e}`,...n]}function Te(e,n,t){let o=(e.URUN_PI_EXTENSION_ENTRY??"").trim();if(o){if(!t(o))throw new Error(`URUN_PI_EXTENSION_ENTRY points at ${o}, which does not exist \u2014 the launcher that injected it (the vendored urun CLI shim) must materialize the pi extension bundle before spawning pi`);return o}let a=X(new URL("../pi-extension/standalone.cjs",n));if(!t(a))throw new Error(`pi extension entry not found at ${a} \u2014 the pi lane loads the self-contained @urun-sh/openai dist/pi-extension/standalone.cjs from the installed package (run \`urun compat pi\` via the npm-installed CLI, e.g. \`npx @urun-sh/openai\`), or pass an entry to pi yourself: \`pi -e <path-to-@urun-sh/openai>/dist/pi-extension/standalone.cjs\``);return a}function T(e){let n=Number(e);if(!Number.isInteger(n)||n<0)throw new Error(`--port needs a non-negative integer (0 = OS-assigned ephemeral), got ${e}`);return n}function Ge(e){if(e[0]!=="--port")return null;let n=T(e[1]);return e.splice(0,2),n}function Le(e){let n=e.indexOf("--port");if(n===-1)return C;let t=T(e[n+1]);return e.splice(n,2),t}function je(e){if(e===0)throw new Error("`env` cannot use --port 0: it starts no proxy, so the OS never assigns the ephemeral port \u2014 pass the actual port of the running proxy");return e}function y(e,n){let t=(e[n]??"").trim();if(!t)throw new Error(`${n} is not set. The proxy needs URUN_BASE_URL, URUN_ORG_ID, URUN_APP and either URUN_API_KEY (the org key; the proxy vends short-lived function-scoped client tokens from it via the SDK) or URUN_JWT (an explicit pre-vended token override) \u2014 plus optional URUN_FUNCTION, default "serve" \u2014 to open the backhaul session.`);return t}function De(e){let n=ee(y(e,"URUN_BASE_URL")),t=y(e,"URUN_ORG_ID"),o=y(e,"URUN_APP"),a=(e.URUN_FUNCTION??"serve").trim()||"serve",s=(e.URUN_API_URL??"").trim(),r=s?$(s,"URUN_API_URL"):R,i=(e.URUN_JWT??"").trim();if(i)return{baseUrl:n,orgId:t,appId:o,fnName:a,apiUrl:r,auth:{lane:"jwt",jwt:i}};let u=(e.URUN_API_KEY??"").trim();if(u){let c=(e.URUN_GATEWAY_URL??"").trim(),d=c?$(c):void 0;return{baseUrl:n,orgId:t,appId:o,fnName:a,apiUrl:r,auth:{lane:"api-key",apiKey:u,gatewayUrl:d}}}throw new Error("neither URUN_API_KEY nor URUN_JWT is set. Set URUN_API_KEY (the org API key \u2014 the proxy vends short-lived, function-scoped client tokens from it via the SDK, so the key never reaches the agent process) or URUN_JWT (an explicit pre-vended token; when set it always wins). URUN_BASE_URL, URUN_ORG_ID and URUN_APP are also required.")}function ee(e,n="URUN_BASE_URL"){if(/\/v1\/*$/.test(new URL(e).pathname))throw new Error(`${n} must be the session-gateway base (e.g. https://api.urun.sh), got ${e} \u2014 a trailing /v1 is the org control-plane API form (that belongs in URUN_API_URL); session allocation 404s against it. Drop the /v1.`);return e}function $(e,n="URUN_GATEWAY_URL"){let t=new URL(e),o=t.hostname==="localhost"||t.hostname==="127.0.0.1"||t.hostname==="[::1]";if(t.protocol!=="https:"&&!o)throw new Error(`${n} must be https (got ${e}) \u2014 the org API key rides the request's Authorization header`);return e}function Me(e){return{app:e.appId,org:e.orgId,fn:e.fnName,base_url:e.baseUrl,proxy_version:Q}}function ne(e,n){return e.app===n.app&&e.org===n.org&&e.fn===n.fn&&e.base_url===n.base_url&&e.proxy_version===n.proxy_version}var te=250;async function oe(e,n=te){try{let t=await fetch(`http://127.0.0.1:${e}/stats`,{signal:AbortSignal.timeout(n)});if(!t.ok)return null;let o=(await t.json()).identity;if(o==null||typeof o!="object")return null;let{app:a,org:s,fn:r,base_url:i,proxy_version:u}=o;return typeof a!="string"||typeof s!="string"||typeof r!="string"||typeof i!="string"||typeof u!="string"?null:{app:a,org:s,fn:r,base_url:i,proxy_version:u}}catch{return null}}var G=2e3;async function re(e,n=G){try{let t=await fetch(`http://127.0.0.1:${e}/v1/models`,{signal:AbortSignal.timeout(n)});return t.ok?null:`GET /v1/models answered HTTP ${t.status}`}catch(t){return`GET /v1/models failed (${String(t instanceof Error?t.message:t)})`}}async function Fe(e,n,t=C,o=G){if(e!=null)return{mode:"spawn",port:e};let a=await oe(t);if(a!==null&&ne(a,n)){let s=await re(t,o);return s===null?{mode:"reuse",port:t,identity:a}:{mode:"spawn",port:0,reason:`a urun-openai proxy on port ${t} matches this invocation's identity but failed the serving-surface liveness check (${s}) \u2014 starting a fresh proxy on an ephemeral port instead of pointing the harness at it`}}return{mode:"spawn",port:0}}function We(e,n=V.homedir()){return J.join(n,".urun","logs",`compat-proxy-${e}.log`)}var w=["log","info","warn","error","debug","trace"];function Be(e,n=process,t=console){let o=n.stdout.write,a=n.stderr.write,s=(i,...u)=>e.write(i,...u);n.stdout.write=s,n.stderr.write=s;let r=Object.fromEntries(w.map(i=>[i,t[i]]));for(let i of w)t[i]=(...u)=>{e.write(`${z(...u)}
2
- `)};return()=>{n.stdout.write=o,n.stderr.write=a;for(let i of w)t[i]=r[i]}}var se=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function L(e){let n=e?.code;return typeof n=="string"&&se.has(n)}function He(e,n){try{return e.write(n),!0}catch(t){if(L(t))return!1;throw t}}function Ke(e){let n={stdout:!1,stderr:!1},t=[];for(let o of["stdout","stderr"]){let a=e.streams[o],s=r=>{L(r)&&(n[o]=!0);let i=r?.code;try{e.note(`${new Date().toISOString()} urun compat: process.${o} died (${String(i??(r instanceof Error?r.message:r))}) \u2014 the terminal is gone; launcher output continues in this log only
3
- `)}catch{}};a.on("error",s),t.push([a,s])}return{isDead:o=>n[o],uninstall:()=>{for(let[o,a]of t)o.off("error",a)}}}function qe(e){let n=t=>{let o=t instanceof Error?`${t.stack??`${t.name}: ${t.message}`}`:String(t);try{e.appendSync(e.logPath,`${new Date().toISOString()} launcher crashed (in-process proxy): ${o}
4
- `)}catch{}e.restore();try{e.writeStderr(`urun compat: the in-process proxy crashed (${t instanceof Error?t.message:String(t)}) \u2014 terminating the harness (its backend is gone); details: ${e.logPath}
5
- `)}catch(a){try{e.appendSync(e.logPath,`${new Date().toISOString()} urun compat: the crash line could not reach stderr (${String(a?.code??a)}) \u2014 the terminal is gone
6
- `)}catch{}}e.killChild(),e.exit(1)};return e.proc.on("uncaughtException",n),e.proc.on("unhandledRejection",n),()=>{e.proc.off("uncaughtException",n),e.proc.off("unhandledRejection",n)}}function Ye(e,n){let t="pass --port 0 for an OS-assigned ephemeral port";return n?`port ${e} is held by another urun-openai proxy serving app ${n.app} (org ${n.org}, fn ${n.fn}, version ${n.proxy_version}) \u2014 reuse it by pointing your agent at http://127.0.0.1:${e}, or ${t}`:`port ${e} is already in use and does not answer the urun-openai /stats identity probe \u2014 ${t}`}export{xe as a,ve as b,Oe as c,Z as d,Ne as e,Ie as f,$e as g,Ce as h,Te as i,Ge as j,Le as k,je as l,De as m,ee as n,$ as o,Me as p,oe as q,Fe as r,We as s,Be as t,He as u,Ke as v,qe as w,Ye as x};
@@ -1 +0,0 @@
1
- import{a as _,b as x,n as A,o as g}from"./chunk-RQBD4OAA.js";import{a as v}from"./chunk-VNM4M4P3.js";import{c as i}from"./chunk-YSFSRI3D.js";i();import{createClientToken as E,ClientTokenError as I}from"@urun-sh/core";var l=class extends Error{constructor(t,n=401){super(t);this.status=n;this.name="ProxyAuthError"}status},p=class extends Error{constructor(e){super(e),this.name="ControlPlaneUnavailableError"}};function C(r){let e=(r??"").trim();if(e==="")throw new l("missing Authorization header \u2014 send your uRun org API key as `Authorization: Bearer <key>`");let t=/^Bearer[ \t]+(\S.*)$/i.exec(e);if(!t)throw new l("malformed Authorization header \u2014 expected `Authorization: Bearer <urun org api key>`");return t[1].trim()}var S=60,N=6e4,O=4096;function k(r){return r===401||r===403}var y=class{constructor(e){this.opts=e;this.mint=e.mint??E,this.now=e.now??Date.now}opts;cache=new Map;mint;now;async resolve(e){if(e==="")throw new l("empty Authorization bearer token");let t=this.cache.get(e);if(t&&this.now()-t.at<N)return{apiKey:e,orgId:t.orgId};let n;try{n=(await this.mint(e,{baseUrl:this.opts.gatewayUrl,expiresIn:S})).orgId}catch(o){throw o instanceof I&&k(o.status)?(this.cache.delete(e),new l("the uRun org API key was rejected by the control plane \u2014 check the key and that it is not revoked")):new p(`could not verify the API key against the uRun control plane at ${this.opts.gatewayUrl}: ${o instanceof Error?o.message:String(o)}`)}if(!n)throw new p("the control plane verified the API key but returned no org_id \u2014 refusing to route a request with no tenancy (control-plane contract violation)");if(this.cache.size>=O){let o=this.cache.keys().next();o.done||this.cache.delete(o.value)}return this.cache.set(e,{orgId:n,at:this.now()}),{apiKey:e,orgId:n}}clear(){this.cache.clear()}};i();var $=8080,T="serve",X="0.0.0.0";function L(r,e,t){let n=(r[e]??"").trim();if(!n)throw new Error(`${e} is not set \u2014 ${t}`);return n}function W(r=process.env){let e=(r.PORT??"").trim(),t=e===""?$:Number(e);if(!Number.isInteger(t)||t<0||t>65535)throw new Error(`PORT must be an integer in 0..65535, got ${JSON.stringify(e)}`);let n=A(g(L(r,"URUN_BASE_URL","the hosted proxy needs the uRun session-gateway base it opens sessions and verifies API keys against (e.g. https://api.urun.sh)"),"URUN_BASE_URL")),o=(r.URUN_API_URL??"").trim(),d=o?g(o,"URUN_API_URL"):`${n.replace(/\/+$/,"")}/v1`,a=(r.URUN_CATALOG_URL??"").trim(),m=(r.URUN_CATALOG_ANON_KEY??"").trim();if(!!a!=!!m)throw new Error("URUN_CATALOG_URL and URUN_CATALOG_ANON_KEY must be set together (or both left unset) \u2014 exactly one of them is set, which would silently disable the model-catalog oracle");let s=a?{catalogUrl:g(a,"URUN_CATALOG_URL"),anonKey:m}:null;return{port:t,baseUrl:n,apiUrl:d,catalog:s}}i();import{createHash as H}from"crypto";var z=512;function M(r){return`urun-inference-proxy-${H("sha256").update(r).digest("hex").slice(0,32)}`}var f=class{constructor(e){this.opts=e}opts;tenants=new Map;build(e){if(this.opts.build)return this.opts.build(e);let t=_({baseUrl:this.opts.baseUrl,orgId:e.orgId,defaultApp:null,fnName:T,apiUrl:this.opts.apiUrl,auth:{lane:"api-key",apiKey:e.apiKey,gatewayUrl:this.opts.baseUrl},catalog:this.opts.catalog,subject:M(e.apiKey)});return{router:t,clients:x(t)}}clientsFor(e){let t=this.tenants.get(e.apiKey);if(t)return this.tenants.delete(e.apiKey),this.tenants.set(e.apiKey,t),t.clients;for(;this.tenants.size>=z;){let o=this.tenants.entries().next();if(o.done)break;let[d,a]=o.value;this.tenants.delete(d),a.router.closeAll().catch(()=>{})}let n=this.build(e);return this.tenants.set(e.apiKey,n),n.clients}get size(){return this.tenants.size}async closeAll(){let e=[...this.tenants.values()].map(n=>n.router);this.tenants.clear();let t=[];if(await Promise.all(e.map(async n=>{try{await n.closeAll()}catch(o){t.push(String(o instanceof Error?o.message:o))}})),t.length>0)throw new Error(`failed to close ${t.length} tenant backhaul(s): ${t.join("; ")}`)}};i();import{createServer as G}from"http";var B=5e3,F=3e3,K="/api/client-tokens";function U(r,e,t){r.writeHead(e,{"content-type":"application/json"}),r.end(JSON.stringify(t))}function R(r,e,t,n){U(r,e,{error:{message:t,type:n}})}async function D(r,e=fetch){let t=`${r.replace(/\/+$/,"")}${K}`;try{let n=await e(t,{method:"POST",headers:{"content-type":"application/json"},body:"{}",signal:AbortSignal.timeout(F)});return n.status===404?{ready:!1,detail:`${t} answered 404 \u2014 this is not a uRun control plane`}:n.status>=500?{ready:!1,detail:`${t} answered HTTP ${n.status}`}:{ready:!0,detail:`${t} answered HTTP ${n.status}`}}catch(n){return{ready:!1,detail:`${t} unreachable: ${n instanceof Error?n.message:String(n)}`}}}function le(r){let e=r.resolver??new y({gatewayUrl:r.baseUrl}),t=r.registry??new f({baseUrl:r.baseUrl,apiUrl:r.apiUrl,catalog:r.catalog}),n=r.probeControlPlane??(()=>D(r.baseUrl)),o=async s=>e.resolve(C(s.headers.authorization)),d=v({clients:async s=>t.clientsFor(await o(s)),statusOf:s=>s instanceof l?{status:s.status,openaiType:"authentication_error",anthropicType:"authentication_error"}:s instanceof p?{status:502,openaiType:"upstream_error",anthropicType:"api_error"}:null}),a=null;return{server:G((s,c)=>{let h=new URL(s.url??"/","http://localhost");(async()=>{if(s.method==="GET"&&h.pathname==="/healthz"){U(c,200,{ok:!0});return}if(s.method==="GET"&&h.pathname==="/readyz"){let u=Date.now();if(!a||u-a.at>=B){let P=await n();a={at:u,...P}}U(c,a.ready?200:503,{ok:a.ready,control_plane:a.detail});return}if(h.pathname!=="/v1"&&!h.pathname.startsWith("/v1/")){R(c,404,`no route for ${s.method} ${h.pathname} \u2014 this endpoint serves the OpenAI-compatible /v1 surface plus /healthz and /readyz`,"invalid_request_error");return}d(s,c)})().catch(u=>{if(!c.headersSent){R(c,500,String(u instanceof Error?u.message:u),"proxy_error");return}c.end()})}),closeAll:()=>t.closeAll()}}async function ce(r,e){let t=new Promise(n=>r.close(()=>n()));r.closeAllConnections(),await t,await e()}export{l as a,p as b,C as c,N as d,O as e,y as f,$ as g,T as h,X as i,W as j,z as k,M as l,f as m,B as n,F as o,le as p,ce as q};