@urun-sh/openai 0.3.0 → 0.3.1

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 (48) hide show
  1. package/dist/ResponsesClient-BYx3YLGo.d.ts +26 -0
  2. package/dist/ResponsesClient-Dft3bg3b.d.cts +26 -0
  3. package/dist/chunk-54VWZJU7.js +6 -0
  4. package/dist/chunk-5NXM4IO3.js +2 -0
  5. package/dist/chunk-CWJRDBDC.js +1 -0
  6. package/dist/chunk-G3NMGP4N.js +1 -0
  7. package/dist/chunk-M6ICU4F5.js +40 -0
  8. package/dist/chunk-UVAY7Q7Z.js +1 -0
  9. package/dist/{chunk-3ED25N7E.js → chunk-ZW6ENXZY.js} +1 -1
  10. package/dist/gemini-live.cjs +2 -0
  11. package/dist/gemini-live.d.cts +120 -0
  12. package/dist/gemini-live.d.ts +79 -0
  13. package/dist/gemini-live.js +1 -0
  14. package/dist/hosted/bin.cjs +18 -18
  15. package/dist/hosted/bin.js +1 -1
  16. package/dist/hosted/index.cjs +17 -17
  17. package/dist/hosted/index.d.cts +4 -3
  18. package/dist/hosted/index.d.ts +4 -3
  19. package/dist/hosted/index.js +1 -1
  20. package/dist/index.cjs +1 -1
  21. package/dist/index.d.cts +4 -4
  22. package/dist/index.d.ts +4 -4
  23. package/dist/index.js +1 -1
  24. package/dist/pi-extension/index.d.cts +2 -1
  25. package/dist/pi-extension/index.d.ts +2 -1
  26. package/dist/pi-extension/index.js +1 -1
  27. package/dist/pi-extension/standalone.cjs +53 -53
  28. package/dist/proxy/cli.cjs +37 -37
  29. package/dist/proxy/cli.js +1 -1
  30. package/dist/proxy/index.cjs +21 -21
  31. package/dist/proxy/index.d.cts +4 -31
  32. package/dist/proxy/index.d.ts +4 -6
  33. package/dist/proxy/index.js +1 -1
  34. package/dist/{server-DiQUIDDg.d.cts → server-BfME37pQ.d.cts} +20 -2
  35. package/dist/{server-BBKNv7hV.d.ts → server-l1himPxc.d.ts} +6 -2
  36. package/dist/translator-C9uPKypK.d.ts +130 -0
  37. package/dist/translator-CcDBEfvm.d.cts +227 -0
  38. package/dist/{ResponsesClient-y8g6OfNN.d.cts → types-lsVTbNcH.d.cts} +16 -24
  39. package/dist/{ResponsesClient-y8g6OfNN.d.ts → types-lsVTbNcH.d.ts} +8 -24
  40. package/dist/video-out-CWesbk12.d.ts +155 -0
  41. package/dist/video-out-D20UuJ8G.d.cts +298 -0
  42. package/package.json +12 -2
  43. package/dist/chunk-35LB5OHI.js +0 -1
  44. package/dist/chunk-EFBD3CGZ.js +0 -1
  45. package/dist/chunk-VG3X2LVG.js +0 -41
  46. package/dist/chunk-XHRPJIEK.js +0 -6
  47. package/dist/media-DCHTX3Ez.d.cts +0 -68
  48. package/dist/media-DCHTX3Ez.d.ts +0 -42
@@ -0,0 +1,298 @@
1
+ import { b as UrunSessionLike } from './types-lsVTbNcH.cjs';
2
+
3
+ /**
4
+ * The ONE native audio sample rate of the session transport: the SFU Opus leg
5
+ * runs 24 kHz mono, so every PCM16 chunk entering `AudioBridge.appendInputAudio`
6
+ * and every chunk emitted by `onOutputAudio` is 24 kHz mono 16-bit LE. Protocol
7
+ * adapters (OpenAI Realtime, Gemini Live) resample at THEIR edge to this rate.
8
+ */
9
+ declare const NATIVE_AUDIO_SAMPLE_RATE = 24000;
10
+ /** Pluggable audio backend so tests don't need real WebRTC. In production, `nodeAudioBackend()` wraps @roamhq/wrtc. */
11
+ interface AudioSource {
12
+ track: unknown;
13
+ onData(frame: Int16Array): void;
14
+ }
15
+ interface AudioSink {
16
+ onframe: ((frame: {
17
+ samples: Int16Array;
18
+ }) => void) | null;
19
+ }
20
+ interface AudioBackend {
21
+ createSource(): AudioSource;
22
+ /**
23
+ * Build a sink that pulls PCM16 frames OFF the runtime's downstream audio
24
+ * track (`remoteTrack`, the real Opus `MediaStreamTrack` the SFU now produces
25
+ * — see session-server-ts audio ingest). The production backend wires a real
26
+ * `RTCAudioSink(remoteTrack)`; the sink's `onframe` is invoked per decoded
27
+ * 10ms frame. `remoteTrack` is REQUIRED — a missing track is a wiring bug, not
28
+ * a silent no-op (a dead sink is exactly the placeholder this replaces).
29
+ */
30
+ createSink(remoteTrack: unknown): AudioSink;
31
+ }
32
+ declare class AudioBridge {
33
+ private readonly backend;
34
+ private readonly opts;
35
+ private source;
36
+ private sink;
37
+ private outHandlers;
38
+ constructor(backend: AudioBackend, opts: {
39
+ sampleRate: number;
40
+ });
41
+ startOutbound(): Promise<unknown>;
42
+ /** OpenAI: input_audio_buffer.append (base64 PCM16) → publish frames. */
43
+ appendInputAudio(base64Pcm16: string): void;
44
+ /**
45
+ * Wire the runtime's downstream audio track into the output-frame fan-out.
46
+ * `remoteTrack` is the REAL Opus `MediaStreamTrack` the SFU produces for this
47
+ * session (`session.stream('rt-audio-out').track`). The backend attaches a
48
+ * real `RTCAudioSink` to it; each decoded PCM16 frame is re-emitted as base64
49
+ * to every `onOutputAudio` handler (the OpenAI `response.audio.delta` source).
50
+ *
51
+ * Throws if `remoteTrack` is absent — the previous build silently created a
52
+ * dead sink that never fired, so voice OUT looked "wired" but produced no
53
+ * audio. A missing track here means the consume side did not attach yet; the
54
+ * caller must await the real track before calling this.
55
+ */
56
+ startInbound(remoteTrack: unknown): Promise<void>;
57
+ /** Emits base64 PCM16 → caller wraps as OpenAI response.audio.delta. */
58
+ onOutputAudio(handler: (base64Pcm16: string) => void): () => void;
59
+ }
60
+ /**
61
+ * The ONE canonical session-audio wiring — every protocol surface that carries
62
+ * audio over a uRun session (OpenAI Realtime `enableAudio`, the Gemini Live
63
+ * proxy lane) rides THIS function; none re-implements the dance:
64
+ *
65
+ * 1. `bridge.startOutbound()` → attach the local track to `rt-audio-in`
66
+ * 2. await the runtime's downstream track on `rt-audio-out` (consumed
67
+ * ASYNCHRONOUSLY after the WebRTC handshake — may not exist yet)
68
+ * 3. `bridge.startInbound(track)` → decoded PCM16 fans out via `onOutputAudio`
69
+ *
70
+ * Fails loud (rejects) if the runtime never produces voice OUT within
71
+ * `timeoutMs` — a video-only/text-only app is a wiring gap, not a silent no-op.
72
+ */
73
+ declare function enableSessionAudio(session: UrunSessionLike, backend: AudioBackend, timeoutMs?: number): Promise<AudioBridge>;
74
+ /**
75
+ * The named-DATA stream video frames ride browser→runtime (the video-frame twin
76
+ * of the `rt-audio-in` media lane). Apps consume it as
77
+ * `ctx.stream('rt-video-in', kind='data')` — each segment is one encoded JPEG
78
+ * frame (raw bytes, no envelope beyond the §5 stream framing).
79
+ */
80
+ declare const RT_VIDEO_IN_STREAM = "rt-video-in";
81
+ /**
82
+ * Per-frame wire-fit budget, the TS twin of urun-python
83
+ * `image_codec.IMAGE_MESSAGE_BUDGET`: mediasoup's transport `maxMessageSize`
84
+ * and Chrome's `a=max-message-size` are both 262144 bytes, and an SCTP message
85
+ * beyond the ceiling is dropped SILENTLY by the mediasoup worker (live root
86
+ * cause dev-usw2 2026-07-10: a 300,675-byte JPEG reached the SFU and simply
87
+ * never arrived). 256000 leaves slack for the §5 stream envelope framing. A
88
+ * frame over budget MUST be rejected loudly here — the transport would turn it
89
+ * into a silent drop.
90
+ */
91
+ declare const VIDEO_FRAME_MESSAGE_BUDGET = 256000;
92
+ /** The frame-lane handle: one encoded JPEG frame per call, in call order. */
93
+ interface VideoFrameLane {
94
+ sendInputFrame(jpegFrame: Uint8Array): Promise<void>;
95
+ }
96
+ /**
97
+ * The ONE canonical session video-frame wiring: discrete JPEG frames →
98
+ * `session.stream(RT_VIDEO_IN_STREAM).emit(bytes)` (core's §5 named-DATA
99
+ * produce). Fails loud if the session's stream surface has no `emit` — a
100
+ * frame-receiving surface without the data-produce seam is a wiring gap, never
101
+ * a silent drop. There is no downstream video leg: Live-style protocols output
102
+ * TEXT/AUDIO only, and the runtime's video OUT rides the existing consume path.
103
+ */
104
+ declare function enableSessionVideo(session: UrunSessionLike): VideoFrameLane;
105
+ /**
106
+ * Production backend for the werift transport (@urun-sh/core). Replaces the
107
+ * wrtc backend, whose `RTCAudioSink`/`RTCAudioSource` are incompatible with
108
+ * werift's read-only-`id` `MediaStreamTrack`.
109
+ */
110
+ declare function weriftAudioBackend(): Promise<AudioBackend>;
111
+ /**
112
+ * @deprecated wrtc backend — INCOMPATIBLE with @urun-sh/core's werift transport
113
+ * (its `RTCAudioSink`/`RTCAudioSource` set the foreign track's read-only `id`).
114
+ * Use {@link weriftAudioBackend}. Kept for any non-werift transport.
115
+ */
116
+ declare function nodeAudioBackend(): Promise<AudioBackend>;
117
+
118
+ /**
119
+ * Video OUTPUT over the named §5 WT/QUIC stream — the platform lane a
120
+ * beyond-parity protocol extension (Gemini Live `urun.videoOut`) points
121
+ * clients at, plus the client leg that consumes it.
122
+ *
123
+ * The delivery path is ENTIRELY first-party (urun-infra#1608, deployed):
124
+ * the runtime emits per-frame records on the named §5 data stream
125
+ * {@link RT_VIDEO_OUT_STREAM}; the relay's egress sink fans each §5 envelope
126
+ * to the wt-gateway, which multiplexes it onto ONE ordered reliable QUIC
127
+ * unidirectional stream per name on the leg's existing WebTransport session.
128
+ * Nothing here invents a transport or a second framing:
129
+ *
130
+ * - §5 envelope: core `encodeStreamEnvelope`/`decodeStreamEnvelope`
131
+ * (`[0x01][kind][payload]`) — the LOCKED twin, byte-identical to SCTP.
132
+ * - Uni-stream framing: core `consumeWtDataStreams`/`WtDataStreamParser`
133
+ * (`[0x01][nameLen][name utf8]` then repeated `[u32 LE len][envelope]`).
134
+ * - Control handshake: core `encodeWtControlFrame`/`WtControlDecoder`
135
+ * (`{t:'hello', token, sessionId}` → `hello-ack`), the same dance
136
+ * core's WebTransportSession performs.
137
+ *
138
+ * The ONLY new bytes are the per-record extension envelope (spec v1, below):
139
+ * a JSON header + one encoded access unit inside each §5 BINARY payload.
140
+ *
141
+ * EXTENSION ENVELOPE SPEC v1 (framing id {@link VIDEO_OUT_FRAMING}):
142
+ * record = [u16 LE headerLen][headerLen bytes UTF-8 JSON][AU bytes…]
143
+ * header = {"v":1,"seq":<int≥0>,"ptsUs":<int>,"codec":<string>,"key":<bool>}
144
+ * v MUST be 1 (any other value rejects the record loudly)
145
+ * seq per-stream monotonically increasing from 0 (gap = relay loss bug:
146
+ * the lane is ordered+reliable, so a gap is LOUD, never smoothed)
147
+ * ptsUs presentation timestamp in microseconds
148
+ * codec the AU's codec id (`"h264"` = Annex-B H.264 in v1; the record
149
+ * header is AUTHORITATIVE over the capability-level default)
150
+ * key true iff the AU decodes standalone (IDR/keyframe)
151
+ * AU exactly one encoded video access unit; MUST be non-empty
152
+ * One record per §5 BINARY envelope; the producer ends the stream with the
153
+ * §5 END envelope (core STREAM_END), never by dropping the connection.
154
+ */
155
+
156
+ /** The named §5 data stream video OUT rides (downstream twin of rt-video-in). */
157
+ declare const RT_VIDEO_OUT_STREAM = "rt-video-out";
158
+ /** Framing id advertised in the capability handshake; names spec v1 above. */
159
+ declare const VIDEO_OUT_FRAMING = "urun.videoOut.v1";
160
+ /** Capability-level default AU codec (per-record `header.codec` is authoritative). */
161
+ declare const VIDEO_OUT_DEFAULT_CODEC = "h264";
162
+ /** The parsed per-record JSON header (spec v1). */
163
+ interface VideoOutRecordHeader {
164
+ v: 1;
165
+ seq: number;
166
+ ptsUs: number;
167
+ codec: string;
168
+ key: boolean;
169
+ }
170
+ /** One decoded video-out record: header + one encoded access unit. */
171
+ interface VideoOutRecord {
172
+ header: VideoOutRecordHeader;
173
+ au: Uint8Array;
174
+ }
175
+ /** Encode ONE record (spec v1): `[u16 LE headerLen][JSON header][AU]`. */
176
+ declare function encodeVideoOutRecord(header: Omit<VideoOutRecordHeader, 'v'>, au: Uint8Array): Uint8Array;
177
+ /** Decode ONE record (spec v1). Every contract violation is LOUD. */
178
+ declare function decodeVideoOutRecord(payload: Uint8Array): VideoOutRecord;
179
+ /**
180
+ * The negotiated capability a granting proxy returns in `setupComplete` —
181
+ * everything the client leg needs to dial the relay's WT endpoint and read
182
+ * the named stream: the #1608 offer surface (url/certHashes/token), the
183
+ * session id the WT hello requires, and the stream/codec/framing brand.
184
+ */
185
+ interface VideoOutEndpoint {
186
+ transport: 'webtransport';
187
+ url: string;
188
+ /** sha-256 cert hashes, lowercase hex — WebTransport `serverCertificateHashes`. */
189
+ certHashes: string[];
190
+ /** The per-leg gateway token (#1608: the token's data flag subscribes the leg). */
191
+ token: string;
192
+ /** The uRun session id the WT hello names (`{t:'hello', token, sessionId}`). */
193
+ sessionId: string;
194
+ stream: typeof RT_VIDEO_OUT_STREAM;
195
+ codec: string;
196
+ framing: typeof VIDEO_OUT_FRAMING;
197
+ }
198
+ /**
199
+ * The ONE canonical session wiring for video OUT over QUIC: ask the core
200
+ * session for its §5-over-WT data-lane endpoint (the urun-infra#1608
201
+ * `TransportOfferResponse.webtransport` surface: url + certHashes + token,
202
+ * negotiated with `wtData: true`) and brand it with the rt-video-out stream
203
+ * name and framing. Fails LOUD when the core session exposes no
204
+ * `wtDataEndpoint()` — a session without the dial-negotiation data-lane seam
205
+ * is a missing platform primitive, never a silent skip and NEVER a cue to
206
+ * inline frames on some other lane.
207
+ */
208
+ declare function negotiateSessionVideoOutQuic(session: UrunSessionLike): Promise<VideoOutEndpoint>;
209
+ /** Framing id the WS-inline capability ack names (spec v2: chunks ride the
210
+ * Bidi WS itself as `{"urun.videoOut": {v:1, seq, ptsUs, codec, key, data}}`
211
+ * extension server messages — base64 AU inside JSON). */
212
+ declare const VIDEO_OUT_WS_FRAMING = "urun.videoOut.v2-ws";
213
+ /**
214
+ * The native video-OUT lane the compat surface fans onto the WS: decoded
215
+ * spec-v1 records from the session's named §5 `rt-video-out` downstream.
216
+ */
217
+ interface VideoOutLane {
218
+ /** Capability-level default codec (per-record `header.codec` is authoritative). */
219
+ codec: string;
220
+ onOutputFrame(handler: (record: VideoOutRecord) => void): () => void;
221
+ /** LOUD per-lane protocol errors (malformed record, non-BINARY payload). */
222
+ onLaneError(handler: (err: Error) => void): () => void;
223
+ /** The producer ended the stream — normal completion. */
224
+ onEnd(handler: () => void): () => void;
225
+ }
226
+ /**
227
+ * The ONE canonical session wiring for the compat surface's video OUT: consume
228
+ * the session's named §5 `rt-video-out` data stream via the core consume seam
229
+ * (`session.stream(name).messages()` — the downstream twin of the `emit`
230
+ * produce seam #309's video-in rides) and fan each decoded spec-v1 record to
231
+ * subscribers. The pump starts on the FIRST frame subscriber, so no frame is
232
+ * dropped before the adapter wires its fan-out. Fails LOUD (sync) when the
233
+ * stream surface has no `messages()`; malformed records surface on
234
+ * `onLaneError`, never silently skipped. Requires NO WebTransport anywhere —
235
+ * this lane is self-sufficient over whichever transport the session runs.
236
+ */
237
+ declare function openSessionVideoOutLane(session: UrunSessionLike): VideoOutLane;
238
+ /** Where decoded frames land. Errors are surfaced, never swallowed. */
239
+ interface VideoOutSink {
240
+ onFrame(record: VideoOutRecord): void;
241
+ /** The producer ended the stream (§5 END envelope) — normal completion. */
242
+ onEnd?(): void;
243
+ /** A per-stream protocol violation (bad record/envelope). LOUD, stream-fatal. */
244
+ onLaneError(err: Error): void;
245
+ }
246
+ interface WtByteReader {
247
+ read(): Promise<{
248
+ value?: Uint8Array;
249
+ done: boolean;
250
+ }>;
251
+ cancel?(reason?: unknown): Promise<void>;
252
+ }
253
+ /** Structural WebTransport slice (the DOM lib doesn't ship WT everywhere this
254
+ * package compiles — the same pattern as core's webtransport-session). */
255
+ interface VideoOutWtSession {
256
+ ready: Promise<unknown>;
257
+ incomingUnidirectionalStreams: {
258
+ getReader(): {
259
+ read(): Promise<{
260
+ value?: {
261
+ getReader(): WtByteReader;
262
+ };
263
+ done: boolean;
264
+ }>;
265
+ };
266
+ };
267
+ createBidirectionalStream(): Promise<{
268
+ writable: {
269
+ getWriter(): {
270
+ write(chunk: Uint8Array): Promise<void>;
271
+ };
272
+ };
273
+ readable: {
274
+ getReader(): WtByteReader;
275
+ };
276
+ }>;
277
+ close?(info?: unknown): void;
278
+ }
279
+ /** Dial seam: `(url, {serverCertificateHashes}) => WT session`. Defaults to
280
+ * `globalThis.WebTransport`; inject a factory where the platform has none. */
281
+ type VideoOutWtConnect = (url: string, options: {
282
+ serverCertificateHashes: Array<{
283
+ algorithm: 'sha-256';
284
+ value: Uint8Array;
285
+ }>;
286
+ }) => VideoOutWtSession;
287
+ /**
288
+ * Consume the negotiated video-out lane: dial the relay's WT endpoint, run
289
+ * the canonical hello/hello-ack control handshake, then accept the per-name
290
+ * uni streams via core `consumeWtDataStreams` and deliver each decoded
291
+ * record on the sink. Streams with OTHER names legitimately share the
292
+ * session (token deltas, captions) and are ignored here — they are not
293
+ * errors. Resolves when the session's accept loop ends; rejects loudly on
294
+ * dial/handshake/accept-loop death.
295
+ */
296
+ declare function consumeVideoOut(endpoint: VideoOutEndpoint, sink: VideoOutSink, connect?: VideoOutWtConnect): Promise<void>;
297
+
298
+ export { type AudioBackend as A, NATIVE_AUDIO_SAMPLE_RATE as N, RT_VIDEO_IN_STREAM as R, VIDEO_FRAME_MESSAGE_BUDGET as V, AudioBridge as a, type AudioSink as b, type AudioSource as c, RT_VIDEO_OUT_STREAM as d, VIDEO_OUT_DEFAULT_CODEC as e, VIDEO_OUT_FRAMING as f, VIDEO_OUT_WS_FRAMING as g, type VideoFrameLane as h, type VideoOutEndpoint as i, type VideoOutLane as j, type VideoOutRecord as k, type VideoOutRecordHeader as l, type VideoOutSink as m, type VideoOutWtConnect as n, type VideoOutWtSession as o, consumeVideoOut as p, decodeVideoOutRecord as q, enableSessionAudio as r, enableSessionVideo as s, encodeVideoOutRecord as t, negotiateSessionVideoOutQuic as u, nodeAudioBackend as v, openSessionVideoOutLane as w, weriftAudioBackend as x };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urun-sh/openai",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "OpenAI-compatible Realtime + Responses SDK over uRun session primitives (not websockets).",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -27,6 +27,16 @@
27
27
  "default": "./dist/proxy/index.cjs"
28
28
  }
29
29
  },
30
+ "./gemini-live": {
31
+ "import": {
32
+ "types": "./dist/gemini-live.d.ts",
33
+ "default": "./dist/gemini-live.js"
34
+ },
35
+ "require": {
36
+ "types": "./dist/gemini-live.d.cts",
37
+ "default": "./dist/gemini-live.cjs"
38
+ }
39
+ },
30
40
  "./hosted": {
31
41
  "import": {
32
42
  "types": "./dist/hosted/index.d.ts",
@@ -60,7 +70,7 @@
60
70
  "build:bin": "bun build --compile --external @roamhq/wrtc src/proxy/bin.ts --outfile dist/bin/urun-openai && node scripts/assert-bin-transport.mjs"
61
71
  },
62
72
  "peerDependencies": {
63
- "@urun-sh/core": "^0.3.0"
73
+ "@urun-sh/core": "^0.3.1"
64
74
  },
65
75
  "dependencies": {
66
76
  "openai": "4.104.0",
@@ -1 +0,0 @@
1
- import{c as m}from"./chunk-YSFSRI3D.js";m();var l=class{constructor(n,e){this.backend=n;this.opts=e}backend;opts;source=null;sink=null;outHandlers=new Set;async startOutbound(){return this.source=this.backend.createSource(),this.source.track}appendInputAudio(n){if(!this.source)throw new Error("startOutbound() not called");let e=Buffer.from(n,"base64"),t=Math.floor(e.byteLength/2),a=new Int16Array(t);for(let o=0;o<t;o++)a[o]=e.readInt16LE(o*2);this.source.onData(a)}async startInbound(n){if(n==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(n),this.sink.onframe=e=>{let t=Buffer.from(e.samples.buffer,e.samples.byteOffset,e.samples.byteLength).toString("base64");for(let a of this.outHandlers)a(t)}}onOutputAudio(n){return this.outHandlers.add(n),()=>this.outHandlers.delete(n)}};async function E(r,n,e=1e4){let t=new l(n,{sampleRate:24e3}),a=await t.startOutbound();await r.stream("rt-audio-in").attach(a);let o=await S(r,e);return await t.startInbound(o),t}function S(r,n){let e=r.stream("rt-audio-out");return e.track!=null?Promise.resolve(e.track):new Promise((t,a)=>{let o,i=setTimeout(()=>{o?.(),a(new Error(`enableSessionAudio: no downstream audio track within ${n}ms (runtime produced no voice OUT / SFU audio consumer never attached)`))},n),u=s=>{s!=null&&(clearTimeout(i),o?.(),t(s))};o=e.on?.("track",u),e.track!=null&&u(e.track)})}var p="rt-video-in",k=256e3;function g(r){let n=r.stream(p),e=n.emit;if(typeof e!="function")throw new Error(`enableSessionVideo: session.stream('${p}') has no emit() \u2014 the named-DATA stream produce seam (@urun-sh/core SessionStream.emit, contract \xA75) is required for the video frame lane`);return{sendInputFrame:async t=>{if(t.byteLength===0)throw new Error("enableSessionVideo: refusing to send an empty video frame");if(t.byteLength>k)throw new Error(`enableSessionVideo: frame is ${t.byteLength} bytes \u2014 over the ${k}-byte data-channel budget (the SFU's 262144-byte SCTP ceiling drops oversize messages SILENTLY; send smaller/lower-quality frames)`);await e.call(n,t)}}}var y=24e3,d=480,v=111;async function T(){let r=await import("opusscript"),n=r.default??r;return new n(y,1,2048)}async function P(){let r=await import("werift"),n=await T();return{createSource:()=>{let e=new r.MediaStreamTrack({kind:"audio"}),t=Math.random()*4294967295>>>0,a=Math.random()*65535&65535,o=Math.random()*4294967295>>>0,i=new Int16Array(0);return{track:e,onData:u=>{let s=new Int16Array(i.length+u.length);s.set(i,0),s.set(u,i.length);let c=0;for(;s.length-c>=d;){let f=s.subarray(c,c+d);c+=d;let w=Buffer.from(f.buffer,f.byteOffset,f.byteLength),A=n.encode(w,d);a=a+1&65535,o=o+d>>>0;let b=new r.RtpHeader({version:2,payloadType:v,sequenceNumber:a,timestamp:o,ssrc:t,marker:!1}),h=new r.RtpPacket(b,A);e.writeRtp(h)}i=s.slice(c)}}},createSink:e=>{let t={onframe:null};return e.onReceiveRtp.subscribe(o=>{let i;try{i=n.decode(o.payload)}catch{return}let u=new Int16Array(i.length/2);for(let s=0;s<u.length;s++)u[s]=i.readInt16LE(s*2);t.onframe?.({samples:u})}),t}}}async function _(){let r=await import("@roamhq/wrtc");return{createSource:()=>{let n=new r.nonstandard.RTCAudioSource;return{track:n.createTrack(),onData:t=>n.onData({samples:t,sampleRate:24e3})}},createSink:n=>{let e={onframe:null},t=new r.nonstandard.RTCAudioSink(n);return t.ondata=a=>{e.onframe?.(a)},e}}}export{E as a,g as b,P as c,_ as d};
@@ -1 +0,0 @@
1
- import{c as i}from"./chunk-YSFSRI3D.js";i();function _(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 n=e,r=n.body??{};return{type:"error",error:{type:"urun_error",code:n.code??null,message:r.message??"unknown error"}}}return{type:"error",error:{type:"urun_error",code:null,message:String(e)}}}i();i();i();function f(e,n,r,t){let o=e.response??{},a={request_id:r,consumer_id:t,stream:!0,kind:"chat",messages:n};return typeof o.instructions=="string"&&(a.instructions=o.instructions),Array.isArray(o.modalities)&&(a.modalities=o.modalities),typeof o.temperature=="number"&&(a.temperature=o.temperature),typeof o.max_output_tokens=="number"&&(a.max_output_tokens=o.max_output_tokens),o.tools!==void 0&&(a.tools=o.tools),a}function y(e,n,r){let t={request_id:n,consumer_id:r,stream:!!e.stream,kind:"responses",input:e.input};return e.model&&(t.model=e.model),e.tools!==void 0&&(t.tools=e.tools),e.tool_choice!==void 0&&(t.tool_choice=e.tool_choice),typeof e.temperature=="number"&&(t.temperature=e.temperature),typeof e.max_output_tokens=="number"&&(t.max_output_tokens=e.max_output_tokens),typeof e.reasoning_effort=="string"&&(t.reasoning_effort=e.reasoning_effort),e.chat_template_kwargs!==void 0&&(t.chat_template_kwargs=e.chat_template_kwargs),t}i();var b="llm-resp";async function*g(e,n){let r=`${b}:${n}`,t=`resp_${n}`;yield{type:"response.created",response:{id:t,status:"in_progress"}};let o=new Map;for await(let a of e.stream(r).messages()){let s=a;if(s.t==="delta"){if(typeof s.delta=="string"&&(yield{type:"response.output_text.delta",item_id:t,delta:s.delta}),typeof s.reasoning=="string"&&(yield{type:"response.reasoning_text.delta",item_id:t,delta:s.reasoning}),Array.isArray(s.tool_calls))for(let u of s.tool_calls){let c=typeof u.index=="number"?u.index:0,p=o.get(c)??{};u.id&&(p.id=u.id),u.function?.name&&(p.name=u.function.name),o.set(c,p),yield{type:"response.function_call_arguments.delta",item_id:`fc_${n}_${c}`,tool_index:c,call_id:p.id,name:p.name,delta:u.function?.arguments??""}}}else if(s.t==="response"){yield{type:"response.completed",response:{id:t,status:"completed",...s.body}};return}else if(s.t==="error"){yield _(s);return}}}var h="llm",d=class{constructor(n){this.session=n;this.sessionTag=n.sessionId??globalThis.crypto.randomUUID()}session;sessionTag;get consumerId(){return this.session.consumerId}write(n){n.session_tag=this.sessionTag,this.session.doc(h).set({requests:{[n.request_id]:{payload:n,consumer_id:n.consumer_id,stream:n.stream}}})}sendResponses(n,r){let t=y(n,r,this.consumerId);return this.write(t),g(this.session,r)}sendResponseCreate(n,r,t){let o=f(n,r,t,this.consumerId);return this.write(o),g(this.session,t)}};var k=0;function v(){return k+=1,`req_${Date.now().toString(36)}_${k.toString(36)}`}var w=class{transport;constructor(n){this.transport=new d(n)}responses={create:async n=>{let r=v(),t=this.transport.sendResponses(n,r);return Object.assign((async function*(){yield*t})(),{requestId:r})}}};export{_ as a,d as b,w as c};
@@ -1,41 +0,0 @@
1
- import{b as V,c as ge}from"./chunk-XHIIEA6Z.js";import{c as W}from"./chunk-YSFSRI3D.js";W();var Ee="/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent",Xe=1007,Te=1008,J=1011,ye=1008,i=class extends Error{constructor(e,o=Xe){super(e);this.closeCode=o}closeCode},A=t=>typeof t=="object"&&t!==null&&!Array.isArray(t)?t:null,ue=["setup","clientContent","realtimeInput","toolResponse"];function Pe(t){let n;try{n=JSON.parse(t)}catch{throw new i("client message is not valid JSON")}let e=A(n);if(!e)throw new i("client message must be a JSON object");let o=ue.filter(d=>e[d]!==void 0),s=Object.keys(e).filter(d=>!ue.includes(d));if(s.length>0)throw new i(`unknown client message field(s) ${JSON.stringify(s)} \u2014 expected exactly one of ${ue.join(", ")}`);if(o.length!==1)throw new i(`client message must contain exactly one of ${ue.join(", ")} (got ${o.length})`);let r=o[0],a=A(e[r]);if(!a)throw new i(`"${r}" must be a JSON object`);return{kind:r,[r]:a}}function Ne(t,n){if(typeof t=="string")return t;let e=A(t),o=e?e.parts:void 0;if(!Array.isArray(o))throw new i(`${n}: expected a Content with a parts array`);let s=[];for(let r of o){let a=A(r);if(a&&typeof a.text=="string"){s.push(a.text);continue}throw a&&(a.inlineData!==void 0||a.fileData!==void 0)?new i(`${n}: media parts (inlineData/fileData) are a declared follow-up slice (audio/video lanes over the proxy transport AudioBridge) \u2014 only text parts are supported in this slice`):a&&(a.functionCall!==void 0||a.functionResponse!==void 0)?new i(`${n}: functionCall/functionResponse parts are not valid here \u2014 the Live API exchanges tool traffic via the dedicated toolCall/toolResponse messages`):new i(`${n}: unsupported part ${JSON.stringify(r)}`)}return s.join(`
2
- `)}var Qe={manualActivity:!1,activityHandling:"START_OF_ACTIVITY_INTERRUPTS"},Ze={inputAudioTranscription:"setup.inputAudioTranscription is part of the declared audio follow-up slice",outputAudioTranscription:"setup.outputAudioTranscription is part of the declared audio follow-up slice"},et=["model","generationConfig","systemInstruction","tools","realtimeInputConfig","sessionResumption"],be=["START_OF_ACTIVITY_INTERRUPTS","NO_INTERRUPTION"];function tt(t){let n=A(t);if(!n)throw new i("setup.realtimeInputConfig must be an object");for(let s of Object.keys(n))if(!(s==="automaticActivityDetection"||s==="activityHandling"))throw new i(s==="turnCoverage"?"realtimeInputConfig.turnCoverage is part of the declared audio follow-up slice":`unsupported realtimeInputConfig field "${s}"`);let e=A(n.automaticActivityDetection);if(n.automaticActivityDetection!==void 0&&!e)throw new i("realtimeInputConfig.automaticActivityDetection must be an object");for(let s of Object.keys(e??{}))if(s!=="disabled")throw new i(`automaticActivityDetection.${s} tunes AUTOMATIC (server-side) activity detection \u2014 the declared platform follow-up (serve turn_detection events over the session stream lane); only "disabled: true" (manual activity mode) is supported in this slice`);if(e?.disabled!==!0)throw new i("automatic (server-side) activity detection is the declared platform follow-up (serve turn_detection turn_start/turn_end events over the session stream lane, not proxy DSP) \u2014 set realtimeInputConfig.automaticActivityDetection.disabled: true for manual activity mode");let o="START_OF_ACTIVITY_INTERRUPTS";if(n.activityHandling!==void 0){if(!be.includes(n.activityHandling))throw new i(`unsupported activityHandling ${JSON.stringify(n.activityHandling)} \u2014 expected one of ${be.join(", ")}`);o=n.activityHandling}return{manualActivity:!0,activityHandling:o}}var nt=["temperature","maxOutputTokens","responseModalities"];function Me(t){for(let[u,f]of Object.entries(Ze))if(t[u]!==void 0)throw new i(f);for(let u of Object.keys(t))if(!et.includes(u))throw new i(`unsupported setup field "${u}"`);if(typeof t.model!="string"||t.model.length===0)throw new i('setup.model is required (format "models/{model}")');let n=t.model.replace(/^models\//,""),e,o,s=!1,r=A(t.generationConfig);if(t.generationConfig!==void 0&&!r)throw new i("setup.generationConfig must be an object");if(r){for(let u of Object.keys(r))if(!nt.includes(u))throw new i(`unsupported generationConfig field "${u}"`);if(r.temperature!==void 0){if(typeof r.temperature!="number")throw new i("generationConfig.temperature must be a number");e=r.temperature}if(r.maxOutputTokens!==void 0){if(typeof r.maxOutputTokens!="number")throw new i("generationConfig.maxOutputTokens must be a number");o=r.maxOutputTokens}if(r.responseModalities!==void 0){let u=Array.isArray(r.responseModalities)?r.responseModalities:[r.responseModalities];for(let f of u){let g=String(f).toUpperCase();if(g==="AUDIO"){s=!0;continue}if(g!=="TEXT")throw new i(`responseModalities ${JSON.stringify(f)} is not supported \u2014 this surface speaks TEXT and AUDIO (audio-out rides the native AudioBridge rt-audio-out lane)`)}}}let a=[];t.systemInstruction!==void 0&&a.push({role:"system",content:Ne(t.systemInstruction,"setup.systemInstruction")});let d;if(t.tools!==void 0){if(!Array.isArray(t.tools))throw new i("setup.tools must be an array");d=[];for(let u of t.tools){let f=A(u);if(!f)throw new i("setup.tools entries must be objects");for(let g of Object.keys(f))if(g!=="functionDeclarations")throw new i(`unsupported tool "${g}" \u2014 this slice translates functionDeclarations only (no googleSearch/codeExecution)`);if(!Array.isArray(f.functionDeclarations))throw new i("tool.functionDeclarations must be an array");for(let g of f.functionDeclarations){let h=A(g);if(!h||typeof h.name!="string")throw new i("functionDeclarations entries require a string name");d.push({type:"function",name:h.name,description:h.description,parameters:h.parameters})}}d.length===0&&(d=void 0)}let m={enabled:!1,handle:null};if(t.sessionResumption!==void 0){let u=A(t.sessionResumption);if(!u)throw new i("setup.sessionResumption must be an object (SessionResumptionConfig)");for(let f of Object.keys(u))if(f!=="handle")throw new i(`unsupported sessionResumption field "${f}" \u2014 this lane implements SessionResumptionConfig.handle only`);if(u.handle!==void 0&&(typeof u.handle!="string"||u.handle.length===0))throw new i("sessionResumption.handle must be a non-empty string when present");m={enabled:!0,handle:typeof u.handle=="string"?u.handle:null}}let p=t.realtimeInputConfig===void 0?Qe:tt(t.realtimeInputConfig);return{model:n,systemItems:a,tools:d,temperature:e,maxOutputTokens:o,audioModality:s,activity:p,sessionResumption:m}}function $e(t){return{sessionResumptionUpdate:{newHandle:t,resumable:!0}}}function Le(t){let n=Math.max(0,t??0)/1e3;return{goAway:{timeLeft:`${Math.round(n*1e3)/1e3}s`}}}function Ue(t){for(let o of Object.keys(t))if(o!=="turns"&&o!=="turnComplete")throw new i(`unsupported clientContent field "${o}"`);let n=t.turns===void 0?[]:t.turns;if(!Array.isArray(n))throw new i("clientContent.turns must be an array");let e=[];for(let o of n){let s=A(o);if(!s)throw new i("clientContent.turns entries must be Content objects");let r=s.role===void 0?"user":String(s.role).toLowerCase();if(r!=="user"&&r!=="model")throw new i(`clientContent turn role must be "user" or "model" (got ${JSON.stringify(s.role)})`);e.push({role:r==="model"?"assistant":"user",content:Ne(s,"clientContent.turns")})}return{items:e,turnComplete:t.turnComplete===!0}}var le=16e3,qe=24e3,ot=`audio/pcm;rate=${qe}`,je=Buffer.alloc(960).toString("base64");function st(t,n,e){if(n===e)return t;let o=Math.floor(t.length*e/n),s=new Int16Array(o),r=n/e,a=t.length-1;for(let d=0;d<o;d++){let m=d*r,p=Math.floor(m);if(p>=a){s[d]=t[a];continue}let u=m-p;s[d]=Math.round(t[p]+(t[p+1]-t[p])*u)}return s}function rt(t){let n=A(t);if(!n)throw new i("realtimeInput.video must be a Blob object {mimeType, data}");if(typeof n.mimeType!="string"||!/^image\/jpeg\s*(;|$)/.test(n.mimeType))throw new i(`realtimeInput.video.mimeType must be image/jpeg (the Live video input stream is individual JPEG frames), got ${JSON.stringify(n.mimeType)}`);if(typeof n.data!="string"||n.data.length===0)throw new i("realtimeInput.video.data must be non-empty base64");let e=Buffer.from(n.data,"base64");if(e.length<3||e[0]!==255||e[1]!==216)throw new i("realtimeInput.video.data does not decode to a JPEG frame (missing FF D8 SOI marker)");return new Uint8Array(e)}function it(t){let n=A(t);if(!n)throw new i("realtimeInput.audio must be a Blob object {mimeType, data}");if(typeof n.mimeType!="string"||!/^audio\/pcm\s*(;|$)/.test(n.mimeType))throw new i(`realtimeInput.audio.mimeType must be audio/pcm (16-bit LE PCM @16kHz mono per the Live spec), got ${JSON.stringify(n.mimeType)}`);let e=/;\s*rate=(\d+)/.exec(n.mimeType);if(e&&Number(e[1])!==le)throw new i(`realtimeInput.audio.mimeType declares rate=${e[1]} \u2014 the Live spec input format is ${le} Hz (audio/pcm;rate=${le})`);if(typeof n.data!="string"||n.data.length===0)throw new i("realtimeInput.audio.data must be non-empty base64");let o=Buffer.from(n.data,"base64");if(o.length===0||o.length%2!==0)throw new i(`realtimeInput.audio.data must decode to whole 16-bit samples (got ${o.length} bytes \u2014 not even)`);let s=new Int16Array(o.length/2);for(let a=0;a<s.length;a++)s[a]=o.readInt16LE(a*2);let r=st(s,le,qe);return Buffer.from(r.buffer,r.byteOffset,r.byteLength).toString("base64")}function Be(t){if(t.mediaChunks!==void 0)throw new i('realtimeInput.mediaChunks is deprecated by the Live API \u2014 send the audio stream via realtimeInput.audio ({mimeType:"audio/pcm;rate=16000", data}) instead');if(t.activityStart!==void 0||t.activityEnd!==void 0){if(Object.keys(t).length!==1||t.activityStart!==void 0&&t.activityEnd!==void 0)throw new i("realtimeInput must carry exactly one field when signaling activity (activityStart XOR activityEnd, nothing else)");let s=t.activityStart!==void 0?"activityStart":"activityEnd",r=A(t[s]);if(!r||Object.keys(r).length!==0)throw new i(`realtimeInput.${s} must be an empty object (ActivityStart/ActivityEnd are bare markers)`);return{kind:s}}let n=["text","audio","audioStreamEnd","video"];for(let o of Object.keys(t))if(!n.includes(o))throw new i(`unsupported realtimeInput field "${o}"`);let e=n.filter(o=>t[o]!==void 0);if(e.length!==1)throw new i(`realtimeInput must carry exactly one of ${n.join(", ")} (got ${e.length})`);if(t.audio!==void 0)return{kind:"audio",base64Pcm24k:it(t.audio)};if(t.video!==void 0)return{kind:"video",jpegFrame:rt(t.video)};if(t.audioStreamEnd!==void 0){if(t.audioStreamEnd!==!0)throw new i("realtimeInput.audioStreamEnd must be true when present");return{kind:"audioStreamEnd"}}if(typeof t.text!="string"||t.text.length===0)throw new i("realtimeInput.text must be a non-empty string");return{kind:"text",text:t.text}}function De(t){for(let n of Object.keys(t))if(n!=="functionResponses")throw new i(`unsupported toolResponse field "${n}"`);if(!Array.isArray(t.functionResponses)||t.functionResponses.length===0)throw new i("toolResponse.functionResponses must be a non-empty array");return t.functionResponses.map(n=>{let e=A(n);if(!e||typeof e.id!="string"||e.id.length===0)throw new i("functionResponses entries require the string id matched from the toolCall");return{type:"function_call_output",call_id:e.id,output:JSON.stringify(e.response??null)}})}function He(t){return t.type!=="response.output_text.delta"||typeof t.delta!="string"?null:{serverContent:{modelTurn:{role:"model",parts:[{text:t.delta}]}}}}function Je(t){return{serverContent:{modelTurn:{role:"model",parts:[{inlineData:{mimeType:ot,data:t}}]}}}}function Fe(t){let n=A(t)??{},e=Array.isArray(n.output)?n.output:[],o=[],s=[],r=typeof n.output_text=="string"?n.output_text:"";if(!r)for(let p of e){let u=A(p);if(!(u?.type!=="message"||!Array.isArray(u.content)))for(let f of u.content){let g=A(f);g?.type==="output_text"&&typeof g.text=="string"&&(r+=g.text)}}r&&s.push({role:"assistant",content:r});for(let p of e){let u=A(p);if(u?.type!=="function_call")continue;if(typeof u.call_id!="string"||typeof u.name!="string")throw new i(`upstream function_call item is missing call_id/name: ${JSON.stringify(p)}`,J);let f=typeof u.arguments=="string"&&u.arguments.length>0?u.arguments:"{}",g;try{g=JSON.parse(f)}catch{throw new i(`upstream function_call arguments are not valid JSON: ${f}`,J)}o.push({id:u.call_id,name:u.name,args:g}),s.push({type:"function_call",call_id:u.call_id,name:u.name,arguments:f})}let a=[];o.length>0&&a.push({toolCall:{functionCalls:o}}),a.push({serverContent:{generationComplete:!0}});let d=A(n.usage),m={serverContent:{turnComplete:!0}};if(d){let p={};typeof d.input_tokens=="number"&&(p.promptTokenCount=d.input_tokens),typeof d.output_tokens=="number"&&(p.responseTokenCount=d.output_tokens),typeof d.total_tokens=="number"&&(p.totalTokenCount=d.total_tokens),Object.keys(p).length>0&&(m.usageMetadata=p)}return a.push(m),{messages:a,assistantItems:s}}W();import{createServer as dt}from"http";W();import{randomUUID as at}from"crypto";import{WebSocketServer as ut}from"ws";var lt=256,he=class{snapshots=new Map;store(n,e){for(this.snapshots.set(n,e);this.snapshots.size>lt;){let o=this.snapshots.keys().next().value;this.snapshots.delete(o)}}get(n){return this.snapshots.get(n)}},oe=t=>t.slice(0,120),_e=class{constructor(n,e,o){this.ws=n;this.clients=e;this.registry=o;n.on("message",s=>this.onMessage(s)),n.on("close",()=>{this.unsubscribeEnd?.(),this.unsubscribeEnd=null,this.detachAudio?.()})}ws;clients;registry;config=null;transcript=[];queue=Promise.resolve();proxyHandle=null;unsubscribeEnd=null;audio=null;detachAudio=null;video=null;activityOpen=!1;activeTurn=null;send(n){this.ws.readyState===this.ws.OPEN&&this.ws.send(JSON.stringify(n))}fail(n){if(n instanceof V){this.ws.close(Te,oe(n.message));return}if(n instanceof ge){this.ws.close(ye,oe(n.message));return}if(n instanceof i){this.ws.close(n.closeCode,oe(n.message));return}this.ws.close(J,oe(String(n instanceof Error?n.message:n)))}onMessage(n){try{let e=Pe(typeof n=="string"?n:Buffer.concat(Array.isArray(n)?n:[n]).toString("utf8"));if(e.kind==="setup"){this.handleSetup(e.setup).catch(s=>this.fail(s));return}if(!this.config)throw new i(`"${e.kind}" before setup \u2014 the first client message must be BidiGenerateContentSetup`);let o=this.config;if(e.kind==="clientContent"){let{items:s,turnComplete:r}=Ue(e.clientContent);this.transcript.push(...s),r&&this.enqueue(()=>this.runTurn());return}if(e.kind==="realtimeInput"){let s=Be(e.realtimeInput);if(s.kind==="activityStart"||s.kind==="activityEnd"){this.onActivity(s.kind,o);return}if(s.kind==="text"){this.transcript.push({role:"user",content:s.text}),this.enqueue(()=>this.runTurn());return}if(s.kind==="video"){this.appendVideoFrame(s.jpegFrame);return}this.appendAudio(s.kind==="audio"?s.base64Pcm24k:je);return}this.transcript.push(...De(e.toolResponse)),this.enqueue(()=>this.runTurn())}catch(e){this.fail(e)}}async handleSetup(n){if(this.config)throw new i("setup may only be sent once, as the first client message");let e=Me(n),o=e.sessionResumption.handle;if(o!==null){let s=this.registry.get(o);if(!s)throw new i("unknown sessionResumption.handle \u2014 the Live session it names is gone (proxy restarted, handle evicted, or never issued) and cannot be resumed; reconnect without a handle to start a new session",ye);if(s.model!==e.model)throw new i(`sessionResumption.handle was issued for model "${s.model}" \u2014 the model cannot change on resume (got "${e.model}", per the Live API session-resumption contract)`);if(e.systemItems.length>0)throw new i("setup.systemInstruction cannot be changed on resume \u2014 it is part of the resumed conversation state");this.transcript=[...s.transcript],this.proxyHandle=s.proxyHandle,this.unsubscribeEnd=await this.clients.onSessionEnd(s.proxyHandle,r=>this.enqueue(()=>this.handleUpstreamEnd(r)))}else this.proxyHandle=await this.clients.sessionHandle(e.model),this.unsubscribeEnd=await this.clients.onSessionEnd(this.proxyHandle,s=>this.enqueue(()=>this.handleUpstreamEnd(s))),this.transcript.push(...e.systemItems);this.config=e,this.send({setupComplete:{}}),e.audioModality&&this.ensureAudioLane(),e.sessionResumption.enabled&&await this.mintResumptionUpdate()}ensureAudioLane(){if(this.audio)return this.audio;let n=this.config;if(!n)throw new i("audio before setup \u2014 the first client message must be BidiGenerateContentSetup");let e=this.clients.openAudio;if(!e)throw new i("audio lane not wired: this proxy embedding provides no ProxyClients.openAudio (the native rt-audio-in/rt-audio-out AudioBridge seam)",J);return this.audio=(async()=>{let o=await e.call(this.clients,n.model);return this.detachAudio=o.onOutputAudio(s=>this.send(Je(s))),o})(),this.audio.catch(o=>this.fail(o)),this.audio}appendAudio(n){this.ensureAudioLane().then(e=>e.appendInputAudio(n)).catch(e=>this.fail(e))}ensureVideoLane(){if(this.video)return this.video;let n=this.config;if(!n)throw new i("video before setup \u2014 the first client message must be BidiGenerateContentSetup");let e=this.clients.openVideo;if(!e)throw new i("video lane not wired: this proxy embedding provides no ProxyClients.openVideo (the native rt-video-in frame-lane seam, transport/media.ts enableSessionVideo)",J);return this.video=Promise.resolve(e.call(this.clients,n.model)),this.video.catch(o=>this.fail(o)),this.video}appendVideoFrame(n){this.ensureVideoLane().then(e=>e.sendInputFrame(n)).catch(e=>this.fail(e))}onActivity(n,e){if(!e.activity.manualActivity)throw new i(`realtimeInput.${n} requires manual activity mode \u2014 set setup.realtimeInputConfig.automaticActivityDetection.disabled: true (automatic server-side activity detection is the declared platform follow-up)`);if(n==="activityStart"){if(this.activityOpen)throw new i("activityStart while an activity window is already open (send activityEnd first)");this.activityOpen=!0,e.activity.activityHandling==="START_OF_ACTIVITY_INTERRUPTS"&&this.activeTurn&&(this.activeTurn.interrupted=!0);return}if(!this.activityOpen)throw new i("activityEnd without an open activity window (send activityStart first)");this.activityOpen=!1}enqueue(n){this.queue=this.queue.then(n,()=>{}),this.queue=this.queue.catch(e=>this.fail(e))}async mintResumptionUpdate(){let n=this.config;if(!n?.sessionResumption.enabled||this.ws.readyState!==this.ws.OPEN)return;let e=await this.clients.sessionHandle(n.model);this.proxyHandle=e;let o=at();this.registry.store(o,{model:n.model,proxyHandle:e,transcript:[...this.transcript]}),this.send($e(o))}async handleUpstreamEnd(n){if(this.ws.readyState!==this.ws.OPEN||!this.config)return;this.unsubscribeEnd?.(),this.unsubscribeEnd=null;let e=null;try{e=await this.clients.sessionHandle(this.config.model)}catch{}if(e!==null&&e!==this.proxyHandle){this.proxyHandle=e,this.unsubscribeEnd=await this.clients.onSessionEnd(e,o=>this.enqueue(()=>this.handleUpstreamEnd(o))),this.config.sessionResumption.enabled&&await this.mintResumptionUpdate();return}this.send(Le(n.timeLeftMs)),this.ws.close(J,oe(`upstream session ended: ${n.reason}`))}async runTurn(){let n=this.config;if(!n||this.ws.readyState!==this.ws.OPEN)return;let e={interrupted:!1};this.activeTurn=e;try{let o=await this.clients.createResponse({model:n.model,input:[...this.transcript],stream:!0,tools:n.tools,temperature:n.temperature,max_output_tokens:n.maxOutputTokens}),s,r=!1;for await(let m of o){if(e.interrupted)break;let p=m;if(p.type==="error")throw new i(`upstream error: ${JSON.stringify(p)}`,J);if(p.type==="response.completed"){s=p.response,r=!0;continue}let u=He(p);u&&this.send(u)}if(e.interrupted){this.send({serverContent:{interrupted:!0}});return}if(!r)throw new i("upstream produced no response.completed event",J);let{messages:a,assistantItems:d}=Fe(s);this.transcript.push(...d);for(let m of a)this.send(m);await this.mintResumptionUpdate()}finally{this.activeTurn=null}}},ct=(t,n)=>{let e=t.headers["x-goog-api-key"];if(typeof e=="string")return e;let o=t.headers.authorization;return typeof o=="string"&&o.startsWith("Bearer ")?o.slice(7):n.searchParams.get("key")??void 0};function Ge(t,n){let e=new ut({noServer:!0}),o=new he;t.on("upgrade",(s,r,a)=>{let d=new URL(s.url??"/","http://localhost");if(d.pathname!==Ee){r.write(`HTTP/1.1 404 Not Found\r
3
- Connection: close\r
4
- \r
5
- no WS route for ${d.pathname}`),r.destroy();return}if(n.apiKey&&ct(s,d)!==n.apiKey){r.write(`HTTP/1.1 401 Unauthorized\r
6
- Connection: close\r
7
- \r
8
- invalid local proxy api key`),r.destroy();return}e.handleUpgrade(s,r,a,m=>{new _e(m,n.clients,o)})})}var ve=class t{startedAt=Date.now();requests={};models={};ttftMs=[];tokRates=[];tokensOut=0;charsOut=0;localInBytes=0;naiveInBytes=0;request(n,e,o){this.requests[n]=(this.requests[n]??0)+1,this.localInBytes+=e,this.naiveInBytes+=o}modelEntry(n){let e=this.models[n];if(e)return e;let o=Object.keys(this.models).length>=pt?"(other)":n;return this.models[o]??={requests:0,tokens_out:0}}model(n){this.modelEntry(n).requests+=1}track(n,e){let o=this,s=performance.now(),r=null,a=0;return(async function*(){for await(let m of n)m.type==="response.output_text.delta"&&typeof m.delta=="string"&&(r===null&&(r=performance.now(),o.ttftMs.push(r-s),o.ttftMs.length>512&&o.ttftMs.shift()),a+=1,o.tokensOut+=1,o.charsOut+=m.delta.length,e!==void 0&&(o.modelEntry(e).tokens_out+=1)),yield m;let d=(performance.now()-(r??s))/1e3;a>0&&d>0&&(o.tokRates.push(a/d),o.tokRates.length>512&&o.tokRates.shift())})()}static p50(n){if(!n.length)return null;let e=[...n].sort((o,s)=>o-s);return e[Math.floor(e.length/2)]}snapshot(){let n=Object.values(this.requests).reduce((e,o)=>e+o,0);return{uptime_s:Math.round((Date.now()-this.startedAt)/1e3),requests:this.requests,models:this.models,tokens_out:this.tokensOut,chars_out:this.charsOut,ttft_ms:{p50:t.p50(this.ttftMs),last:this.ttftMs.at(-1)??null},tok_per_s:{p50:t.p50(this.tokRates),last:this.tokRates.at(-1)??null},traffic:{local_request_bytes:this.localInBytes,naive_baseline:{request_bytes:this.naiveInBytes,connections:n},persistent_connections:1,history_bytes_saved:Math.max(0,this.naiveInBytes-this.localInBytes),history_bytes_saved_pct:this.naiveInBytes>0?Math.round((1-this.localInBytes/this.naiveInBytes)*1e3)/10:null}}}};function Re(t){t.writeHead(200,{"content-type":"text/event-stream","cache-control":"no-cache",connection:"keep-alive"})}function B(t,n,e){t.writeHead(n,{"content-type":"application/json"}),t.end(JSON.stringify(e))}function F(t,n,e,o="invalid_request_error"){B(t,n,{error:{message:e,type:o}})}function j(t,n,e,o){B(t,n,{type:"error",error:{type:e,message:o}})}function Ae(t,n,e){if(n==="anthropic"){B(t,404,{type:"error",error:{type:"not_found_error",message:e.message}});return}B(t,404,{error:{message:e.message,type:"invalid_request_error",code:"model_not_found"}})}var pt=256,ft=128;function Se(t){return typeof t.model!="string"||!t.model.trim()?"(default)":t.model.trim().slice(0,ft)}var Ve=64*1024*1024,ce=class extends Error{};async function mt(t){let n=[],e=0;for await(let s of t){if(e+=s.length,e>Ve)throw new ce(`request body exceeds ${Ve} bytes`);n.push(s)}let o=Buffer.concat(n).toString("utf8");return o?JSON.parse(o):{}}function X(t,n,e,o){return{id:t,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:n,choices:[{index:0,delta:e,finish_reason:o}]}}var gt=256;function yt(t){if(typeof t=="string")return[{role:"user",content:t}];if(Array.isArray(t))return t;throw new Error("responses input must be a string or an array of messages/items")}var xe=class{conversations=new Map;thread(n,e){let o=yt(e);if(n==null)return o;let s=this.conversations.get(String(n));if(!s)throw new Error(`unknown previous_response_id ${String(n)} (proxy restarts drop stored responses)`);return[...s,...o]}remember(n,e,o){this.conversations.set(n,[...e,{role:"assistant",content:o}]);for(let s of this.conversations.keys()){if(this.conversations.size<=gt)break;this.conversations.delete(s)}}};function se(t){let n=t?.output;return Array.isArray(n)?n.filter(e=>e.type==="function_call"):[]}function z(t){return t.find(n=>n.type==="response.completed")?.response??null}function de(t){let n=t?.usage;if(!n||typeof n!="object")return null;let e=a=>typeof a=="number"&&Number.isFinite(a)&&a>=0?a:0,o=e(n.input_tokens),s=e(n.output_tokens);if(o===0&&s===0)return null;let r=e(n.total_tokens)||o+s;return{input_tokens:o,output_tokens:s,total_tokens:r}}function ze(t){let n=de(t);return n?{prompt_tokens:n.input_tokens,completion_tokens:n.output_tokens,total_tokens:n.total_tokens}:{prompt_tokens:0,completion_tokens:0,total_tokens:0}}function Ye(t){let n=t?.output;if(!Array.isArray(n))return"";let e="";for(let o of n)if(!(o.type!=="reasoning"||!Array.isArray(o.content)))for(let s of o.content)s.type==="reasoning_text"&&(e+=String(s.text??""));return e}function re(t){return t.type!=="response.function_call_arguments.delta"||typeof t.tool_index!="number"?null:{tool_index:t.tool_index,call_id:typeof t.call_id=="string"?t.call_id:void 0,name:typeof t.name=="string"?t.name:void 0,delta:typeof t.delta=="string"?t.delta:""}}var K=class{calls=new Map;add(n){let e=this.calls.get(n.tool_index),o=!e;return e||(e={args:""},this.calls.set(n.tool_index,e)),n.call_id&&(e.id=n.call_id),n.name&&(e.name=n.name),e.args+=n.delta,{call:e,isNew:o}}get size(){return this.calls.size}items(){return[...this.calls.entries()].map(([n,e])=>({type:"function_call",call_id:e.id??`call_${n}`,name:e.name??"",arguments:e.args}))}},ht=["<tool_call>","<function="];function _t(t){return ht.find(n=>t.includes(n))}function Q(t,n){return`serving row emitted a prose tool call (literal ${JSON.stringify(n)} in the assistant text) for a request that carried tools, and no structured tool_calls arrived \u2014 the tool-call parser is not configured on the model row "${t}" (see catalog parser_defaults)`}function Z(t,n,e){if(!(!t||n>0))return _t(e)}function wt(t){if(Array.isArray(t))return t.map(n=>{let e=n.function;return!e||typeof e!="object"?n:{type:"function",name:e.name,description:e.description,parameters:e.parameters,strict:e.strict}})}var pe=class extends Error{};function kt(t,n){return Array.isArray(n)?n.map(e=>{let o=e;if(o.type==="text")return{type:t==="assistant"?"output_text":"input_text",text:o.text};if(o.type==="image_url"){let s=o.image_url??{};return{type:"input_image",image_url:s.url,detail:s.detail}}throw new pe(`unsupported chat content part type "${String(o.type)}" \u2014 the proxy translates text and image_url parts`)}):n}function vt(t){let n=[];for(let e of t){if(e.role==="tool"){n.push({type:"function_call_output",call_id:e.tool_call_id,output:typeof e.content=="string"?e.content:JSON.stringify(e.content??"")});continue}if(e.role==="assistant"&&Array.isArray(e.tool_calls)){typeof e.content=="string"&&e.content&&n.push({role:"assistant",content:e.content});for(let o of e.tool_calls){let s=o.function??{};n.push({type:"function_call",call_id:o.id,name:s.name,arguments:s.arguments})}continue}n.push({...e,content:kt(e.role,e.content)})}return n}function we(t){return se(t).map((n,e)=>({index:e,id:n.call_id??n.id??`call_${e}`,type:"function",function:{name:n.name??"",arguments:n.arguments??"{}"}}))}function Ke(t,n){let e=n?.output_text;return typeof e=="string"?e:t.filter(o=>o.type==="response.output_text.delta"&&typeof o.delta=="string").map(o=>o.delta).join("")}async function xt(t,n,e,o,s){let r=t.stream===!0,a;try{a=e.thread(t.previous_response_id,t.input)}catch(l){F(o,400,String(l instanceof Error?l.message:l));return}t.previous_response_id!=null&&(s.naiveInBytes+=Buffer.byteLength(JSON.stringify(a))-Buffer.byteLength(JSON.stringify(t.input)));let d=`resp_${Math.random().toString(36).slice(2,14)}`,m=t.store!==!1,p=Se(t);s.model(p);let u=Array.isArray(t.tools)&&t.tools.length>0,f;try{f=await n.createResponse({model:t.model,input:a,stream:r,tools:t.tools,tool_choice:t.tool_choice,temperature:t.temperature,max_output_tokens:t.max_output_tokens,...t.reasoning_effort!==void 0?{reasoning_effort:t.reasoning_effort}:{},...t.chat_template_kwargs!==void 0?{chat_template_kwargs:t.chat_template_kwargs}:{}})}catch(l){if(l instanceof V){Ae(o,"openai",l);return}throw l}let g=[];if(r){Re(o);let l=y=>{o.write(`event: ${String(y.type??"message")}
9
- data: ${JSON.stringify(y)}
10
-
11
- `)},b=y=>y.response&&typeof y.response=="object"?{...y,response:{...y.response,id:d}}:y,O=`item_${d.slice(5)}`,M=`rs_${d.slice(5)}`,P=!1,$=!1,c=0,x=()=>{P||(P=!0,l({type:"response.created",response:{id:d,object:"response",status:"in_progress"}}))},U=!1,T=0,L="",q=()=>{U&&(U=!1,l({type:"response.reasoning_text.done",item_id:M,output_index:T,content_index:0,text:L}),l({type:"response.output_item.done",output_index:T,item:{id:M,type:"reasoning",summary:[],content:[{type:"reasoning_text",text:L}],status:"completed"}}))},N=!1,v=0,I="",D=y=>{N&&(N=!1,l({type:"response.output_text.done",item_id:O,output_index:v,content_index:0,text:I}),l({type:"response.content_part.done",item_id:O,output_index:v,content_index:0,part:{type:"output_text",text:I}}),l({type:"response.output_item.done",output_index:v,item:{id:O,type:"message",role:"assistant",status:y,content:[{type:"output_text",text:I}]}}))},H=new K,R=null,Y=-1,ee=y=>{if(!R)return;let S=R;R=null,l({type:"response.function_call_arguments.done",item_id:S.fcId,output_index:S.outputIndex,arguments:S.args}),l({type:"response.output_item.done",output_index:S.outputIndex,item:{id:S.fcId,type:"function_call",call_id:S.callId??S.fcId,name:S.name??"",arguments:S.args,status:y}})},te=!1;for await(let y of s.track(f,p)){g.push(y);let S=String(y.type??"");if(S==="response.created"||S==="response.in_progress"){P=!0,l(b(y));continue}if(S==="response.output_item.added"){$=!0,l(b(y));continue}if(S==="response.reasoning_text.delta"&&!$){x(),U||(U=!0,T=c++,l({type:"response.output_item.added",output_index:T,item:{id:M,type:"reasoning",summary:[],content:[],status:"in_progress"}})),L+=String(y.delta??""),l({type:"response.reasoning_text.delta",item_id:M,output_index:T,content_index:0,delta:y.delta});continue}if(S==="response.output_text.delta"&&!$){x(),q(),N||(N=!0,v=c++,l({type:"response.output_item.added",output_index:v,item:{id:O,type:"message",role:"assistant",status:"in_progress",content:[]}}),l({type:"response.content_part.added",item_id:O,output_index:v,content_index:0,part:{type:"output_text",text:""}})),I+=String(y.delta??""),l({type:S,item_id:O,output_index:v,content_index:0,delta:y.delta});continue}let C=$?null:re(y);if(C){if(x(),q(),D("completed"),H.add(C),C.tool_index!==Y){ee("completed"),Y=C.tool_index;let _=c++;R={fcId:`fc_${d.slice(5)}_${C.tool_index}`,outputIndex:_,callId:C.call_id,name:C.name,args:""},l({type:"response.output_item.added",output_index:_,item:{id:R.fcId,type:"function_call",call_id:R.callId??R.fcId,name:R.name??"",arguments:"",status:"in_progress"}})}R&&(C.call_id&&(R.callId=C.call_id),C.name&&(R.name=C.name),R.args+=C.delta,l({type:"response.function_call_arguments.delta",item_id:R.fcId,output_index:R.outputIndex,delta:C.delta}));continue}if(S==="response.completed"&&!$){let _=se(y.response),E=Z(u,H.size+_.length,I);if(E){te=!0,l({type:"error",error:{type:"urun_error",code:"tool_call_parser_missing",message:Q(p,E)}});break}q(),D("completed"),ee("completed"),H.size===0&&_.forEach(G=>{let ne=c++,ae=G.id??G.call_id??`fc_${ne}`,me=G.arguments??"",Ie={id:ae,type:"function_call",call_id:G.call_id??ae,name:G.name??""};l({type:"response.output_item.added",output_index:ne,item:{...Ie,arguments:"",status:"in_progress"}}),l({type:"response.function_call_arguments.delta",item_id:ae,output_index:ne,delta:me}),l({type:"response.function_call_arguments.done",item_id:ae,output_index:ne,arguments:me}),l({type:"response.output_item.done",output_index:ne,item:{...Ie,arguments:me,status:"completed"}})}),l(b(y));continue}l(b(y))}q(),D("incomplete"),ee("incomplete"),m&&!te&&e.remember(d,a,Ke(g,z(g))),o.write(`data: [DONE]
12
-
13
- `),o.end();return}let h=null;for await(let l of s.track(f,p)){if(g.push(l),l.type==="error"){F(o,502,JSON.stringify(l),"upstream_error");return}l.type==="response.completed"&&(h=l.response)}if(h==null){F(o,502,"upstream produced no response.completed event","upstream_error");return}let k=Ke(g,h),w=Z(u,se(h).length,k);if(w){F(o,502,Q(p,w),"upstream_error");return}m&&e.remember(d,a,k),B(o,200,{...h,id:d})}async function Rt(t,n,e,o){let s=t.messages;if(!Array.isArray(s)){F(e,400,"chat/completions requires a messages array");return}let r=String(t.model??"urun"),a=`chatcmpl-${Math.random().toString(36).slice(2,14)}`,d;try{d=vt(s)}catch(c){if(c instanceof pe){F(e,400,c.message);return}throw c}let m=Se(t);o.model(m);let p=Array.isArray(t.tools)&&t.tools.length>0,u;try{u=await n.createResponse({model:t.model,input:d,stream:!0,tools:wt(t.tools),tool_choice:t.tool_choice,temperature:t.temperature,max_output_tokens:t.max_completion_tokens??t.max_tokens,...t.reasoning_effort!==void 0?{reasoning_effort:t.reasoning_effort}:{},...t.chat_template_kwargs!==void 0?{chat_template_kwargs:t.chat_template_kwargs}:{}})}catch(c){if(c instanceof V){Ae(e,"openai",c);return}throw c}if(t.stream===!0){Re(e);let c=[],x=new K,U="";e.write(`data: ${JSON.stringify(X(a,r,{role:"assistant"},null))}
14
-
15
- `);for await(let v of o.track(u,m)){c.push(v);let I=re(v);if(v.type==="response.output_text.delta"&&typeof v.delta=="string")U+=v.delta,e.write(`data: ${JSON.stringify(X(a,r,{content:v.delta},null))}
16
-
17
- `);else if(v.type==="response.reasoning_text.delta"&&typeof v.delta=="string")e.write(`data: ${JSON.stringify(X(a,r,{reasoning_content:v.delta},null))}
18
-
19
- `);else if(I){let{call:D,isNew:H}=x.add(I),R=H?{index:I.tool_index,id:D.id??`call_${I.tool_index}`,type:"function",function:{name:D.name??"",arguments:I.delta}}:{index:I.tool_index,function:{arguments:I.delta}};e.write(`data: ${JSON.stringify(X(a,r,{tool_calls:[R]},null))}
20
-
21
- `)}else if(v.type==="error"){e.write(`data: ${JSON.stringify({error:v})}
22
-
23
- `),e.end();return}}let T=we(z(c)),L=Z(p,x.size+T.length,U);if(L){e.write(`data: ${JSON.stringify({error:{type:"urun_error",code:"tool_call_parser_missing",message:Q(m,L)}})}
24
-
25
- `),e.end();return}x.size===0&&T.length>0&&e.write(`data: ${JSON.stringify(X(a,r,{tool_calls:T},null))}
26
-
27
- `);let q=x.size>0||T.length>0;e.write(`data: ${JSON.stringify(X(a,r,{},q?"tool_calls":"stop"))}
28
-
29
- `),de(z(c))&&e.write(`data: ${JSON.stringify({id:a,object:"chat.completion.chunk",created:Math.floor(Date.now()/1e3),model:r,choices:[],usage:ze(z(c))})}
30
-
31
- `),e.write(`data: [DONE]
32
-
33
- `),e.end();return}let f="",g="",h=[],k=new K;for await(let c of o.track(u,m)){h.push(c),c.type==="response.output_text.delta"&&typeof c.delta=="string"&&(f+=c.delta),c.type==="response.reasoning_text.delta"&&typeof c.delta=="string"&&(g+=c.delta);let x=re(c);if(x&&k.add(x),c.type==="error"){F(e,502,JSON.stringify(c),"upstream_error");return}}let w=z(h),l=we(w),b=l.length>0?l:we({output:k.items()}),O=Z(p,b.length,f);if(O){F(e,502,Q(m,O),"upstream_error");return}let M=b.map(({index:c,...x})=>x),P={role:"assistant",content:f||null},$=g||Ye(w);$&&(P.reasoning_content=$),M.length>0&&(P.tool_calls=M),B(e,200,{id:a,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:r,choices:[{index:0,message:P,finish_reason:M.length>0?"tool_calls":"stop"}],usage:ze(w)})}function At(t){return typeof t=="string"?t:Array.isArray(t)?t.filter(n=>n.type==="text").map(n=>String(n.text??"")).join(""):""}var ie=class extends Error{};function fe(t){return new ie(`unsupported anthropic content block type "${t}" \u2014 the proxy translates text, tool_use and tool_result blocks`)}function St(t){if(Array.isArray(t))return t.map(n=>{let e=n;return{type:"function",name:e.name,description:e.description,parameters:e.input_schema}})}function It(t){if(t==null)return;let n=t.type;if(n==="auto")return"auto";if(n==="none")return"none";if(n==="any")return"required";if(n==="tool")return{type:"function",name:t.name};throw new ie(`unsupported anthropic tool_choice type ${JSON.stringify(String(n))} \u2014 the proxy maps auto, none, any and tool`)}function bt(t){if(typeof t=="string")return t;if(t==null)return"";if(!Array.isArray(t))throw fe(typeof t);let n="";for(let e of t){let o=String(e.type??"");if(o!=="text")throw fe(`tool_result > ${o}`);n+=String(e.text??"")}return n}function Ot(t){let{role:n,content:e}=t;if(typeof e=="string")return[{role:n,content:e}];if(e==null)return[];if(!Array.isArray(e))throw fe(typeof e);let o=[],s="",r=()=>{s&&(o.push({role:n,content:s}),s="")};for(let a of e){let d=String(a.type??"");if(d==="text")s+=String(a.text??"");else if(d==="tool_use")r(),o.push({type:"function_call",call_id:a.id,name:a.name,arguments:JSON.stringify(a.input??{})});else if(d==="tool_result")r(),o.push({type:"function_call_output",call_id:a.tool_use_id,output:bt(a.content)});else{if(d==="thinking"||d==="redacted_thinking")continue;throw fe(d)}}return r(),o}function ke(t){return se(t).map((n,e)=>{let o;try{o=JSON.parse(n.arguments||"{}")}catch{throw new Error(`upstream function_call "${n.name??""}" arguments are not valid JSON`)}return{type:"tool_use",id:n.call_id??n.id??`toolu_${e}`,name:n.name??"",input:o}})}async function Ct(t,n,e,o){let s=t.messages;if(!Array.isArray(s)){j(e,400,"invalid_request_error","messages requires a messages array");return}if(typeof t.max_tokens!="number"||!Number.isInteger(t.max_tokens)||t.max_tokens<1){j(e,400,"invalid_request_error","max_tokens is required and must be a positive integer");return}if(t.top_k!==void 0){j(e,400,"invalid_request_error","top_k is not supported: the uRun serve lane exposes temperature/top_p/stop_sequences only");return}if(t.stop_sequences!==void 0&&(!Array.isArray(t.stop_sequences)||t.stop_sequences.some(c=>typeof c!="string"))){j(e,400,"invalid_request_error","stop_sequences must be an array of strings");return}let r=String(t.model??"urun"),a=`msg_${Math.random().toString(36).slice(2,14)}`,d=[],m;try{t.system&&d.push({role:"system",content:At(t.system)});for(let c of s)d.push(...Ot(c));m=It(t.tool_choice)}catch(c){if(c instanceof ie){j(e,400,"invalid_request_error",c.message);return}throw c}let p=Se(t);o.model(p);let u=Array.isArray(t.tools)&&t.tools.length>0,f;try{f=await n.createResponse({model:t.model,input:d,stream:!0,tools:St(t.tools),tool_choice:m,temperature:t.temperature,max_output_tokens:t.max_tokens,top_p:t.top_p,stop:t.stop_sequences})}catch(c){if(c instanceof V){Ae(e,"anthropic",c);return}throw c}let g=(c,x)=>{e.destroyed||e.write(`event: ${c}
34
- data: ${JSON.stringify({type:c,...x})}
35
-
36
- `)};if(t.stream===!0){let c=o.track(f,p);Re(e),g("message_start",{message:{id:a,type:"message",role:"assistant",content:[],model:r,stop_reason:null,usage:{input_tokens:0,output_tokens:0}}});let x=0,U="",T=0,L="none",q=()=>L,N=-1,v=-1,I=new K,D=[],H=()=>{L!=="none"&&(L="none",g("content_block_stop",{index:N}))},R=(_,E)=>{H(),L=_,N=T++,g("content_block_start",{index:N,content_block:E})};for await(let _ of c){if(e.destroyed)break;D.push(_);let E=re(_);if(_.type==="response.reasoning_text.delta"&&typeof _.delta=="string")q()!=="thinking"&&R("thinking",{type:"thinking",thinking:""}),g("content_block_delta",{index:N,delta:{type:"thinking_delta",thinking:_.delta}});else if(_.type==="response.output_text.delta"&&typeof _.delta=="string")q()!=="text"&&R("text",{type:"text",text:""}),x+=1,U+=_.delta,g("content_block_delta",{index:N,delta:{type:"text_delta",text:_.delta}});else if(E){let{call:G}=I.add(E);(q()!=="tool"||E.tool_index!==v)&&(v=E.tool_index,R("tool",{type:"tool_use",id:G.id??`call_${E.tool_index}`,name:G.name??"",input:{}})),E.delta&&g("content_block_delta",{index:N,delta:{type:"input_json_delta",partial_json:E.delta}})}else if(_.type==="error"){g("error",{error:{type:"api_error",message:JSON.stringify(_)}}),e.end();return}}if(e.destroyed)return;let Y=z(D),ee=se(Y),te=Z(u,I.size+ee.length,U);if(te){g("error",{error:{type:"api_error",message:Q(p,te)}}),e.end();return}H();let y=[];if(I.size===0)try{y=ke(Y)}catch(_){g("error",{error:{type:"api_error",message:String(_ instanceof Error?_.message:_)}}),e.end();return}T===0&&y.length===0&&(R("text",{type:"text",text:""}),H()),y.forEach(_=>{let E=T++;g("content_block_start",{index:E,content_block:{..._,input:{}}}),g("content_block_delta",{index:E,delta:{type:"input_json_delta",partial_json:JSON.stringify(_.input??{})}}),g("content_block_stop",{index:E})});let S=I.size>0||y.length>0,C=de(Y);g("message_delta",{delta:{stop_reason:S?"tool_use":"end_turn"},usage:C?{input_tokens:C.input_tokens,output_tokens:C.output_tokens}:{output_tokens:x}}),g("message_stop",{}),e.end();return}let h="",k="",w=[],l=new K;for await(let c of o.track(f,p)){w.push(c),c.type==="response.output_text.delta"&&typeof c.delta=="string"&&(h+=c.delta),c.type==="response.reasoning_text.delta"&&typeof c.delta=="string"&&(k+=c.delta);let x=re(c);if(x&&l.add(x),c.type==="error"){j(e,502,"api_error",JSON.stringify(c));return}if(e.destroyed)break}if(e.destroyed)return;let b=z(w),O;try{O=ke(b),O.length===0&&l.size>0&&(O=ke({output:l.items()}))}catch(c){j(e,502,"api_error",String(c instanceof Error?c.message:c));return}let M=Z(u,O.length,h);if(M){j(e,502,"api_error",Q(p,M));return}let P=[],$=k||Ye(b);$&&P.push({type:"thinking",thinking:$}),(h||O.length===0)&&P.push({type:"text",text:h}),P.push(...O),B(e,200,{id:a,type:"message",role:"assistant",content:P,model:r,stop_reason:O.length>0?"tool_use":"end_turn",usage:de(b)??{input_tokens:0,output_tokens:0}})}function Et(t){let{clients:n,apiKey:e,identity:o,statusOf:s}=t,r=typeof n=="function"?n:()=>n,a=new WeakMap,d=p=>{let u=a.get(p);return u||(u=new xe,a.set(p,u)),u},m=new ve;return(p,u)=>{let f=new URL(p.url??"/","http://localhost"),g=f.pathname==="/v1/messages"||f.pathname.startsWith("/v1/messages/"),h=(k,w,l,b)=>g?j(u,k,b,w):F(u,k,w,l);(async()=>{if(p.method==="GET"&&f.pathname==="/healthz"){B(u,200,{ok:!0});return}if(p.method==="GET"&&f.pathname==="/stats"){B(u,200,o?{identity:o,...m.snapshot()}:m.snapshot());return}if(e&&(p.headers.authorization??"")!==`Bearer ${e}`){h(401,"invalid local proxy api key","authentication_error","authentication_error");return}let k=await r(p);if(p.method==="GET"&&f.pathname==="/v1/models"){B(u,200,await k.listModels());return}if(p.method!=="POST"){h(404,`no route for ${p.method} ${f.pathname}`,"invalid_request_error","not_found_error");return}let w;try{w=await mt(p)}catch(b){if(b instanceof ce){h(413,b.message,"request_too_large","request_too_large");return}h(400,"request body is not valid JSON","invalid_request_error","invalid_request_error");return}let l=Buffer.byteLength(JSON.stringify(w));if(f.pathname==="/v1/responses"){m.request("responses",l,l),await xt(w,k,d(k),u,m);return}if(f.pathname==="/v1/chat/completions"){m.request("chat_completions",l,l),await Rt(w,k,u,m);return}if(f.pathname==="/v1/messages"){m.request("messages",l,l),await Ct(w,k,u,m);return}if(f.pathname==="/v1/messages/count_tokens"){m.request("count_tokens",l,l),j(u,404,"not_found_error","count_tokens is not implemented by the uRun proxy: the serve transport exposes no token-count lane, and the proxy will not fabricate counts");return}h(404,`no route for POST ${f.pathname}`,"invalid_request_error","not_found_error")})().catch(k=>{let w=String(k instanceof Error?k.message:k);if(!u.headersSent){let l=s?.(k)??null;if(l){h(l.status,w,l.openaiType,l.anthropicType);return}h(500,w,"proxy_error","api_error");return}u.destroyed||(g?u.write(`event: error
37
- data: ${JSON.stringify({type:"error",error:{type:"api_error",message:w}})}
38
-
39
- `):u.write(`data: ${JSON.stringify({error:{message:w,type:"upstream_error"}})}
40
-
41
- `)),u.end()})}}function Jt(t){let{clients:n,apiKey:e}=t,o=dt(Et(t));return Ge(o,{clients:n,apiKey:e}),o}export{Ee as a,Et as b,Jt as c};
@@ -1,6 +0,0 @@
1
- import{a as b}from"./chunk-F2TEK34X.js";import{a as x,b as E,c as S}from"./chunk-35LB5OHI.js";import{c as v}from"./chunk-EFBD3CGZ.js";import{a as R,c as h,d as P,e as U}from"./chunk-XHIIEA6Z.js";import{c as p}from"./chunk-YSFSRI3D.js";p();import{App as A,createClientToken as H}from"@urun-sh/core";p();function L(e){let n=e?.type;return n==="response.created"||n==="response.in_progress"}function j(e){let n=e?.type;return n==="response.completed"||n==="error"}function _(e){return async function(t){return(async function*(){for(let o=1;o<=2;o++){let{app:s,entry:i}=await e.sessionFor(t.model),a=!1,u=!1;try{let c=await e.createStream(i,t);for await(let d of c){if(j(d)&&(a=!0),L(d)){if(o>1)continue}else u=!0;yield d}}catch(c){if(o<2&&!u){await e.evict(s,i);continue}throw c}if(a)return;if(o<2&&!u){await e.evict(s,i);continue}return}})()}}p();var D=3,M="llm",F=new Set(["ended","expired","error"]);function k(e,n){let t=e.doc(M),r=typeof t.onConnectionState=="function",o=typeof e.onPhase=="function";if(!r&&!o)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,i=new Set,a=[],u=c=>{if(!s){s=c;for(let d of[...i])d(c);i.clear()}};return r&&a.push(t.onConnectionState(c=>{c.consecutiveFailures<D||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.`))})),o&&a.push(e.onPhase(c=>{F.has(c.name)&&u(new h(`the pooled session for app "${n}" is gone: it reached terminal phase "${c.name}". The entry is evicted; retry to get a fresh session.`))})),{gone:()=>s,onGone(c){return s?(c(s),()=>{}):(i.add(c),()=>i.delete(c))},dispose(){i.clear();for(let c of a.splice(0))c()}}}async function*g(e,n){let t=n.gone();if(t)throw t;let r=e[Symbol.asyncIterator](),o,s=new Promise((i,a)=>{o=n.onGone(u=>a(u))});s.catch(()=>{});try{for(;;){let i=await Promise.race([r.next(),s]);if(i.done)return;yield i.value}}finally{o?.(),r.return?.(void 0)}}function I(e,n,t){let r=e[t];if(typeof r!="function")throw new Error(`app "${n}" has no function "${t}" (set URUN_FUNCTION)`);let o=r();if(typeof o.end!="function")throw new Error(`app "${n}" function "${t}" returned a session without end()`);return o}function W(e,n){let{baseUrl:t,orgId:r,fnName:o,auth:s,subject:i}=e,a=l=>({session:l,responses:new v(l),gone:k(l,n)});if(s.lane==="jwt"){let l=A(n,{baseUrl:t,orgId:r,jwt:s.jwt});return a(I(l,n,o))}let{apiKey:u,gatewayUrl:c}=s,d=A(n,{baseUrl:t,orgId:r,getAccessToken:async()=>(await H(u,{baseUrl:c,expiresIn:300,allowedFunctions:[`${n}/${o}`],...i===void 0?{}:{subject:i}})).token});return a(I(d,n,o))}function B(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 K=new Set(["error","ended","expired"]);function ye(e){let{auth:n,catalog:t}=e,r=null,o=i=>{let a=W(e,i);return a.gone.onGone(()=>{r?.evict(i,a)}),a},s=new U({defaultApp:e.defaultApp,fnName:e.fnName,openSession:o,closeSession:async i=>{i.gone.dispose(),await i.session.end()},listApps:n.lane==="api-key"?()=>P({apiUrl:e.apiUrl,apiKey:n.apiKey}):null,listCatalog:t?()=>b(t):null,sessionKey:B});return r=s,e.defaultApp!==null&&s.seed(e.defaultApp,o(e.defaultApp)),s}function we(e,n=S){let t=new WeakMap,r=new WeakMap;return{createResponse:_({sessionFor:o=>e.sessionFor(o),evict:(o,s)=>e.evict(o,s),createStream:async(o,s)=>g(await o.responses.responses.create(s),o.gone)}),listModels:()=>e.modelList(),openAudio:async o=>{let{entry:s}=await e.sessionFor(o),i=t.get(s);return i||(i=(async()=>x(s.session,await n()))(),t.set(s,i),i.catch(()=>t.delete(s))),i},openVideo:async o=>{let{entry:s}=await e.sessionFor(o),i=r.get(s);return i||(i=E(s.session),r.set(s,i)),i},sessionHandle:async o=>(await e.handleFor(o)).handle,createResponseOn:async(o,s)=>{let{entry:i}=await e.sessionForHandle(o);return g(await i.responses.responses.create(s),i.gone)},onSessionEnd:async(o,s)=>{let{entry:i}=await e.sessionForHandle(o),a=i.session;if(typeof a.onPhase!="function")throw new Error("pooled session exposes no onPhase \u2014 session-end (goAway) wiring requires @urun-sh/core Session");let u=!1;return a.onPhase(c=>{if(u||!K.has(c.name))return;u=!0;let d=a.endsAt??null;s({timeLeftMs:d?Math.max(0,d.getTime()-Date.now()):null,reason:c.name==="expired"?"session reached its maximum length":`session ${c.name}`})})}}}p();import q from"os";import J from"path";import{fileURLToPath as V}from"url";import{format as X}from"util";var N="0.3.0";var $=4141,z=N,ke={claude:{bin:"claude",env:"anthropic"},codex:{bin:"codex",env:"openai"},pi:{bin:"pi",env:"openai"},aider:{bin:"aider",env:"openai"},opencode:{bin:"opencode",env:"openai"},crush:{bin:"crush",env:"openai"}};function Q(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 Ae(e,n,t){let r={...e,...Q(n,t)};return delete r.URUN_API_KEY,delete r.URUN_JWT,r}function Ie(e,n,t){let r=["-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"'],o=t.indexOf("--"),s=o===-1?t:t.slice(0,o),i=s.some(d=>d==="-m"||d==="--model"||d.startsWith("--model=")),a=t[0]==="exec",u=s.includes("--skip-git-repo-check"),c=a&&!u?["exec","--skip-git-repo-check",...t.slice(1)]:t;return[...r,...i?[]:["-m",n],...c]}function Ne(e,n){return n.some(r=>r==="--model"||r==="--provider"||r.startsWith("--model=")||r.startsWith("--provider="))?n:["--provider","urun","--model",e,...n]}function Oe(e,n,t){let r=(e.URUN_PI_EXTENSION_ENTRY??"").trim();if(r){if(!t(r))throw new Error(`URUN_PI_EXTENSION_ENTRY points at ${r}, 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 r}let o=V(new URL("../pi-extension/standalone.cjs",n));if(!t(o))throw new Error(`pi extension entry not found at ${o} \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 o}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 $e(e){if(e[0]!=="--port")return null;let n=T(e[1]);return e.splice(0,2),n}function Te(e){let n=e.indexOf("--port");if(n===-1)return $;let t=T(e[n+1]);return e.splice(n,2),t}function Ce(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 Ge(e){let n=Z(y(e,"URUN_BASE_URL")),t=y(e,"URUN_ORG_ID"),r=y(e,"URUN_APP"),o=(e.URUN_FUNCTION??"serve").trim()||"serve",s=(e.URUN_API_URL??"").trim(),i=s?O(s,"URUN_API_URL"):R,a=(e.URUN_JWT??"").trim();if(a)return{baseUrl:n,orgId:t,appId:r,fnName:o,apiUrl:i,auth:{lane:"jwt",jwt:a}};let u=(e.URUN_API_KEY??"").trim();if(u){let c=(e.URUN_GATEWAY_URL??"").trim(),d=c?O(c):void 0;return{baseUrl:n,orgId:t,appId:r,fnName:o,apiUrl:i,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 Z(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 O(e,n="URUN_GATEWAY_URL"){let t=new URL(e),r=t.hostname==="localhost"||t.hostname==="127.0.0.1"||t.hostname==="[::1]";if(t.protocol!=="https:"&&!r)throw new Error(`${n} must be https (got ${e}) \u2014 the org API key rides the request's Authorization header`);return e}function Le(e){return{app:e.appId,org:e.orgId,fn:e.fnName,base_url:e.baseUrl,proxy_version:z}}function ee(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 ne=250;async function te(e,n=ne){try{let t=await fetch(`http://127.0.0.1:${e}/stats`,{signal:AbortSignal.timeout(n)});if(!t.ok)return null;let r=(await t.json()).identity;if(r==null||typeof r!="object")return null;let{app:o,org:s,fn:i,base_url:a,proxy_version:u}=r;return typeof o!="string"||typeof s!="string"||typeof i!="string"||typeof a!="string"||typeof u!="string"?null:{app:o,org:s,fn:i,base_url:a,proxy_version:u}}catch{return null}}var C=2e3;async function oe(e,n=C){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 je(e,n,t=$,r=C){if(e!=null)return{mode:"spawn",port:e};let o=await te(t);if(o!==null&&ee(o,n)){let s=await oe(t,r);return s===null?{mode:"reuse",port:t,identity:o}:{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 De(e,n=q.homedir()){return J.join(n,".urun","logs",`compat-proxy-${e}.log`)}var w=["log","info","warn","error","debug","trace"];function Me(e,n=process,t=console){let r=n.stdout.write,o=n.stderr.write,s=(a,...u)=>e.write(a,...u);n.stdout.write=s,n.stderr.write=s;let i=Object.fromEntries(w.map(a=>[a,t[a]]));for(let a of w)t[a]=(...u)=>{e.write(`${X(...u)}
2
- `)};return()=>{n.stdout.write=r,n.stderr.write=o;for(let a of w)t[a]=i[a]}}var re=new Set(["EPIPE","EIO","EBADF","ENXIO"]);function G(e){let n=e?.code;return typeof n=="string"&&re.has(n)}function Fe(e,n){try{return e.write(n),!0}catch(t){if(G(t))return!1;throw t}}function He(e){let n={stdout:!1,stderr:!1},t=[];for(let r of["stdout","stderr"]){let o=e.streams[r],s=i=>{G(i)&&(n[r]=!0);let a=i?.code;try{e.note(`${new Date().toISOString()} urun compat: process.${r} died (${String(a??(i instanceof Error?i.message:i))}) \u2014 the terminal is gone; launcher output continues in this log only
3
- `)}catch{}};o.on("error",s),t.push([o,s])}return{isDead:r=>n[r],uninstall:()=>{for(let[r,o]of t)r.off("error",o)}}}function We(e){let n=t=>{let r=t instanceof Error?`${t.stack??`${t.name}: ${t.message}`}`:String(t);try{e.appendSync(e.logPath,`${new Date().toISOString()} launcher crashed (in-process proxy): ${r}
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(o){try{e.appendSync(e.logPath,`${new Date().toISOString()} urun compat: the crash line could not reach stderr (${String(o?.code??o)}) \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 Be(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{ye as a,we as b,ke as c,Q as d,Ae as e,Ie as f,Ne as g,Oe as h,$e as i,Te as j,Ce as k,Ge as l,Z as m,O as n,Le as o,te as p,je as q,De as r,Me as s,Fe as t,He as u,We as v,Be as w};
@@ -1,68 +0,0 @@
1
- /** Pluggable audio backend so tests don't need real WebRTC. In production, `nodeAudioBackend()` wraps @roamhq/wrtc. */
2
- interface AudioSource {
3
- track: unknown;
4
- onData(frame: Int16Array): void;
5
- }
6
- interface AudioSink {
7
- onframe: ((frame: {
8
- samples: Int16Array;
9
- }) => void) | null;
10
- }
11
- interface AudioBackend {
12
- createSource(): AudioSource;
13
- /**
14
- * Build a sink that pulls PCM16 frames OFF the runtime's downstream audio
15
- * track (`remoteTrack`, the real Opus `MediaStreamTrack` the SFU now produces
16
- * — see session-server-ts audio ingest). The production backend wires a real
17
- * `RTCAudioSink(remoteTrack)`; the sink's `onframe` is invoked per decoded
18
- * 10ms frame. `remoteTrack` is REQUIRED — a missing track is a wiring bug, not
19
- * a silent no-op (a dead sink is exactly the placeholder this replaces).
20
- */
21
- createSink(remoteTrack: unknown): AudioSink;
22
- }
23
- declare class AudioBridge {
24
- private readonly backend;
25
- private readonly opts;
26
- private source;
27
- private sink;
28
- private outHandlers;
29
- constructor(backend: AudioBackend, opts: {
30
- sampleRate: number;
31
- });
32
- startOutbound(): Promise<unknown>;
33
- /** OpenAI: input_audio_buffer.append (base64 PCM16) → publish frames. */
34
- appendInputAudio(base64Pcm16: string): void;
35
- /**
36
- * Wire the runtime's downstream audio track into the output-frame fan-out.
37
- * `remoteTrack` is the REAL Opus `MediaStreamTrack` the SFU produces for this
38
- * session (`session.stream('rt-audio-out').track`). The backend attaches a
39
- * real `RTCAudioSink` to it; each decoded PCM16 frame is re-emitted as base64
40
- * to every `onOutputAudio` handler (the OpenAI `response.audio.delta` source).
41
- *
42
- * Throws if `remoteTrack` is absent — the previous build silently created a
43
- * dead sink that never fired, so voice OUT looked "wired" but produced no
44
- * audio. A missing track here means the consume side did not attach yet; the
45
- * caller must await the real track before calling this.
46
- */
47
- startInbound(remoteTrack: unknown): Promise<void>;
48
- /** Emits base64 PCM16 → caller wraps as OpenAI response.audio.delta. */
49
- onOutputAudio(handler: (base64Pcm16: string) => void): () => void;
50
- }
51
- /** The frame-lane handle: one encoded JPEG frame per call, in call order. */
52
- interface VideoFrameLane {
53
- sendInputFrame(jpegFrame: Uint8Array): Promise<void>;
54
- }
55
- /**
56
- * Production backend for the werift transport (@urun-sh/core). Replaces the
57
- * wrtc backend, whose `RTCAudioSink`/`RTCAudioSource` are incompatible with
58
- * werift's read-only-`id` `MediaStreamTrack`.
59
- */
60
- declare function weriftAudioBackend(): Promise<AudioBackend>;
61
- /**
62
- * @deprecated wrtc backend — INCOMPATIBLE with @urun-sh/core's werift transport
63
- * (its `RTCAudioSink`/`RTCAudioSource` set the foreign track's read-only `id`).
64
- * Use {@link weriftAudioBackend}. Kept for any non-werift transport.
65
- */
66
- declare function nodeAudioBackend(): Promise<AudioBackend>;
67
-
68
- export { type AudioBackend as A, type VideoFrameLane as V, AudioBridge as a, type AudioSink as b, type AudioSource as c, nodeAudioBackend as n, weriftAudioBackend as w };