marona 5.7.0

Marona Agents, MCP server and App authoring, B2A, D2A, realtime and A2A
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/** Standard media streams shared by browser, native and headless consumers. */
export const audioFormats = ["audio/webm;codecs=opus", "audio/ogg;codecs=opus", "audio/mpeg"];
function object(value) {
    if (!value || typeof value !== "object" || Array.isArray(value))
        throw new Error("Invalid realtime media object");
    return value;
}
function positive(value, maximum = Number.MAX_SAFE_INTEGER) {
    if (typeof value !== "number" || !Number.isSafeInteger(value) || value <= 0 || value > maximum)
        throw new Error("Invalid audio metadata");
    return value;
}
function identifier(value) {
    if (typeof value !== "string" || !value.trim())
        throw new Error("Audio identity is required");
    return value;
}
export class RealtimeMedia {
    audioFormats;
    constructor(audioFormats = ["audio/ogg;codecs=opus", "audio/mpeg", "audio/webm;codecs=opus"]) {
        if (!Array.isArray(audioFormats) || audioFormats.length < 1 || audioFormats.length > 8)
            throw new Error("Provide one to eight audio MIME types");
        this.audioFormats = Object.freeze(audioFormats.map(identifier));
    }
    toJSON() { return { version: "1.0", audio_formats: [...this.audioFormats] }; }
    validateNegotiation(capabilities) {
        const media = object(capabilities.media);
        if (media.version !== "1.0" || media.delivery !== "http" || !this.audioFormats.includes(String(object(media.audio_format).mime_type))) {
            throw new Error("The runtime did not negotiate a supported realtime media format");
        }
    }
}
export class RealtimeAudio {
    streamId;
    responseId;
    uri;
    mimeType;
    codec;
    sampleRate;
    channels;
    expiresAt;
    maxBytes;
    constructor(value) {
        const data = object(value), format = object(data.format);
        if (data.version !== "1.0")
            throw new Error("Unsupported realtime media version");
        this.streamId = identifier(data.stream_id);
        this.responseId = identifier(data.response_id);
        this.uri = identifier(data.uri);
        const uri = new URL(this.uri);
        const loopback = uri.hostname === "localhost" || uri.hostname === "[::1]" || /^127\.(?:\d{1,3}\.){2}\d{1,3}$/.test(uri.hostname);
        if (uri.username || uri.password || uri.hash || /^https?:\/\/[^/?#]*@/.test(this.uri) || !(uri.protocol === "https:" || uri.protocol === "http:" && loopback))
            throw new Error("Audio URL must use HTTPS or loopback HTTP");
        this.mimeType = identifier(format.mime_type);
        this.codec = identifier(format.codec);
        this.sampleRate = positive(format.sample_rate);
        this.channels = positive(format.channels);
        const expected = this.mimeType === "audio/mpeg" ? ["mp3", 24000] : ["audio/ogg;codecs=opus", "audio/webm;codecs=opus"].includes(this.mimeType) ? ["opus", 48000] : [];
        if (this.codec !== expected[0] || this.sampleRate !== expected[1] || this.channels !== 1)
            throw new Error("Audio format metadata is inconsistent");
        this.expiresAt = positive(data.expires_at);
        this.maxBytes = positive(data.max_bytes, 2 * 1024 * 1024);
    }
    toJSON() {
        return { version: "1.0", stream_id: this.streamId, response_id: this.responseId, uri: this.uri,
            format: { mime_type: this.mimeType, codec: this.codec, sample_rate: this.sampleRate, channels: this.channels },
            expires_at: this.expiresAt, max_bytes: this.maxBytes };
    }
    content() {
        if (new URL(this.uri).protocol !== "https:")
            throw new Error("B2A media references require HTTPS");
        return { type: "audio", mime_type: this.mimeType, uri: this.uri, representation_id: this.responseId };
    }
    async *chunks(signal) {
        if (this.expiresAt * 1000 <= Date.now())
            throw new Error("The audio stream has expired");
        const controller = new AbortController();
        const abort = () => controller.abort(signal?.reason);
        if (signal?.aborted)
            abort();
        else
            signal?.addEventListener("abort", abort, { once: true });
        let reader;
        let timer;
        try {
            timer = setTimeout(() => controller.abort(), 30_000);
            const response = await fetch(this.uri, { headers: { Accept: this.mimeType }, redirect: "error", credentials: "omit", signal: controller.signal });
            clearTimeout(timer);
            if (!response.ok || !response.body)
                throw new Error("Audio stream is unavailable");
            if (response.headers.get("content-type")?.split(";", 1)[0] !== this.mimeType.split(";", 1)[0])
                throw new Error("Unexpected audio MIME type");
            reader = response.body.getReader();
            let received = 0;
            for (;;) {
                timer = setTimeout(() => controller.abort(), 30_000);
                const { done, value } = await reader.read();
                clearTimeout(timer);
                if (done)
                    break;
                received += value.length;
                if (received > this.maxBytes)
                    throw new Error("Audio exceeds its resource limit");
                yield value;
            }
        }
        finally {
            clearTimeout(timer);
            signal?.removeEventListener("abort", abort);
            await reader?.cancel().catch(() => { });
            controller.abort();
        }
    }
    async writeTo(destination, signal) {
        let size = 0;
        for await (const chunk of this.chunks(signal)) {
            await destination.write(chunk);
            size += chunk.length;
        }
        return size;
    }
}
/** Optional browser output adapter. The stream contract itself has no DOM dependency. */
export class AudioPlayer extends EventTarget {
    element;
    media;
    pending = new Map();
    queue = [];
    active;
    preparation;
    retry;
    blocked = false;
    constructor(element) {
        super();
        if (!element && typeof Audio === "undefined")
            throw new Error("Provide a media output on this platform; headless clients can consume RealtimeAudio.chunks()");
        this.element = element ?? new Audio();
        this.element.crossOrigin = "anonymous";
        const streaming = audioFormats.filter(format => typeof MediaSource !== "undefined" && MediaSource.isTypeSupported(format));
        // Native progressive playback has different support from MediaSource append buffers.
        const native = ["audio/mpeg", "audio/ogg;codecs=opus", "audio/webm;codecs=opus"].filter(format => this.element.canPlayType(format));
        this.media = new RealtimeMedia(streaming.length ? streaming : native);
    }
    get needsUserGesture() { return this.blocked; }
    /** Call directly from the user's Start click, before awaiting a network response. */
    prepare() {
        if (this.active)
            return this.resume();
        if (this.preparation)
            return this.preparation.promise;
        // A short silent PCM WAV activates the same media element used for every reply.
        const bytes = new Uint8Array(844), view = new DataView(bytes.buffer);
        const text = (at, value) => { for (let i = 0; i < value.length; i++)
            bytes[at + i] = value.charCodeAt(i); };
        text(0, "RIFF");
        view.setUint32(4, 836, true);
        text(8, "WAVEfmt ");
        view.setUint32(16, 16, true);
        view.setUint16(20, 1, true);
        view.setUint16(22, 1, true);
        view.setUint32(24, 8000, true);
        view.setUint32(28, 16000, true);
        view.setUint16(32, 2, true);
        view.setUint16(34, 16, true);
        text(36, "data");
        view.setUint32(40, 800, true);
        const uri = URL.createObjectURL(new Blob([bytes], { type: "audio/wav" }));
        let resolve, reject;
        const promise = new Promise((yes, no) => { resolve = yes; reject = no; });
        const operation = { promise, cancel: (error) => finish(error) };
        const finish = (error) => {
            if (this.preparation !== operation)
                return;
            this.preparation = undefined;
            this.element.pause();
            URL.revokeObjectURL(uri);
            if (error)
                reject(error);
            else
                resolve();
            this.advance();
        };
        this.preparation = operation;
        this.element.src = uri;
        // Do not defer play(): mobile engines require the original click activation.
        try {
            void this.element.play().then(() => finish(), error => finish(error));
        }
        catch (error) {
            finish(error);
        }
        return promise;
    }
    /** Resume the current response after playbackblocked; no audio or Agent request is replayed. */
    resume() { return this.retry?.() ?? Promise.resolve(); }
    play(audio) {
        const previous = this.pending.get(audio.streamId);
        if (previous)
            return previous;
        if (this.pending.size >= 1024)
            return Promise.reject(new Error("Create a new player for the next conversation"));
        const promise = new Promise((resolve, reject) => this.queue.push({ audio, resolve, reject }));
        this.pending.set(audio.streamId, promise);
        this.advance();
        return promise;
    }
    advance() {
        if (this.preparation || this.active || !this.queue.length)
            return;
        const next = this.queue.shift();
        this.active = next;
        const controller = new AbortController();
        let objectUrl;
        let source;
        const cleanup = () => {
            controller.abort();
            source?.removeEventListener("sourceopen", sourceOpen);
            if (objectUrl) {
                URL.revokeObjectURL(objectUrl);
                objectUrl = undefined;
            }
            this.element.removeEventListener("ended", ended);
            this.element.removeEventListener("error", mediaFailed);
            this.element.removeEventListener("playing", playing);
            if (this.active === next) {
                this.active = undefined;
                this.retry = undefined;
                this.blocked = false;
            }
        };
        const sourceOpen = () => {
            const load = async () => {
                const buffer = source.addSourceBuffer(next.audio.mimeType);
                for await (const chunk of next.audio.chunks(controller.signal)) {
                    await new Promise((resolve, reject) => {
                        const detach = () => { buffer.removeEventListener("updateend", done); buffer.removeEventListener("error", error); controller.signal.removeEventListener("abort", error); };
                        const done = () => { detach(); resolve(); };
                        const error = () => { detach(); reject(new Error("Audio delivery stopped")); };
                        if (controller.signal.aborted) {
                            error();
                            return;
                        }
                        buffer.addEventListener("updateend", done, { once: true });
                        buffer.addEventListener("error", error, { once: true });
                        controller.signal.addEventListener("abort", error, { once: true });
                        try {
                            buffer.appendBuffer(chunk.slice().buffer);
                        }
                        catch (failure) {
                            detach();
                            reject(failure);
                        }
                    });
                }
                if (source?.readyState === "open" && !controller.signal.aborted)
                    source.endOfStream();
            };
            void load().catch(error => { if (this.active === next)
                failed(error); });
        };
        const ended = () => { next.resolve(); this.advance(); };
        const failed = (error) => { next.reject(error); this.advance(); };
        const mediaFailed = () => failed(new DOMException(this.element.error?.message || "Audio playback failed", "MediaPlaybackError"));
        const playing = () => { if (this.active === next) {
            this.blocked = false;
            this.dispatchEvent(new Event("playing"));
        } };
        const resolve = next.resolve, reject = next.reject;
        next.resolve = () => { cleanup(); resolve(); };
        next.reject = error => { cleanup(); reject(error); };
        if (next.audio.expiresAt * 1000 <= Date.now()) {
            failed(new Error("The audio stream has expired"));
            return;
        }
        if (!this.media.audioFormats.includes(next.audio.mimeType)) {
            failed(new DOMException("Unsupported audio format", "NotSupportedError"));
            return;
        }
        this.element.addEventListener("ended", ended, { once: true });
        this.element.addEventListener("error", mediaFailed, { once: true });
        this.element.addEventListener("playing", playing);
        if (typeof MediaSource !== "undefined" && MediaSource.isTypeSupported(next.audio.mimeType)) {
            source = new MediaSource();
            source.addEventListener("sourceopen", sourceOpen, { once: true });
            objectUrl = URL.createObjectURL(source);
            this.element.src = objectUrl;
        }
        else {
            this.element.src = next.audio.uri;
        }
        this.retry = () => {
            let started;
            try {
                started = this.element.play();
            }
            catch (error) {
                started = Promise.reject(error);
            }
            return started.catch(error => {
                if (this.active !== next)
                    return;
                if (error?.name === "NotAllowedError") {
                    this.blocked = true;
                    this.dispatchEvent(new Event("playbackblocked"));
                }
                else {
                    failed(error);
                }
                throw error;
            });
        };
        void this.retry().catch(() => { });
    }
    stop() {
        const error = new DOMException("Audio playback stopped", "AbortError");
        for (const item of this.queue.splice(0))
            item.reject(error);
        this.preparation?.cancel(error);
        this.active?.reject(error);
        this.active = undefined;
        this.retry = undefined;
        this.blocked = false;
        this.element.pause();
        this.element.removeAttribute("src");
        this.element.load();
    }
}
/** Standards-based browser WebRTC media and ordered JSON events. */
export class RealtimeWebRTC extends EventTarget {
    audio;
    peer;
    channel;
    input;
    closed = false;
    pending = [];
    listening = false;
    connected;
    failed;
    ready = new Promise((resolve, reject) => { this.connected = resolve; this.failed = reject; });
    constructor(audio = document.createElement('audio')) {
        super();
        this.audio = audio;
        audio.autoplay = true;
        this.ready.catch(() => { });
    }
    get readyState() { return this.closed ? 3 : this.channel?.readyState === 'open' ? 1 : 0; }
    addEventListener(type, listener, options) {
        super.addEventListener(type, listener, options);
        if (type === 'message' && !this.listening) {
            this.listening = true;
            queueMicrotask(() => { for (const event of this.pending.splice(0))
                this.dispatchEvent(event); });
        }
    }
    /** Application broker keeps developer credentials server-side. */
    static async connect(endpoint, options = {}) {
        const url = new URL(endpoint, globalThis.location?.href);
        if (url.username || url.password || url.hash || (url.protocol !== 'https:' && !(url.protocol === 'http:' && ['localhost', '127.0.0.1', '[::1]'].includes(url.hostname))))
            throw new Error('WebRTC signaling requires HTTPS');
        const peer = new RealtimeWebRTC(options.audio);
        try {
            const offer = await peer.offer(options.iceServers, options.microphone);
            const response = await fetch(url, { method: 'POST', headers: { ...options.headers, 'Content-Type': 'application/json' }, body: JSON.stringify(offer), redirect: 'error', signal: AbortSignal.timeout(30000) });
            if (!response.ok)
                throw new Error('WebRTC signaling was rejected');
            const reader = response.body.getReader();
            const chunks = [];
            let size = 0;
            try {
                while (true) {
                    const { done, value } = await reader.read();
                    if (done)
                        break;
                    size += value.length;
                    if (size > 131072)
                        throw new Error('WebRTC answer exceeds limit');
                    chunks.push(value);
                }
            }
            finally {
                await reader.cancel();
            }
            const body = new Uint8Array(size);
            let position = 0;
            for (const chunk of chunks) {
                body.set(chunk, position);
                position += chunk.length;
            }
            await peer.answer(JSON.parse(new TextDecoder().decode(body)));
            await Promise.race([peer.ready, new Promise((_, reject) => { const timer = setTimeout(() => reject(new Error('WebRTC connection timed out')), 30000); peer.ready.finally(() => clearTimeout(timer)).catch(() => { }); })]);
            return peer;
        }
        catch (error) {
            peer.close();
            throw error;
        }
    }
    async offer(iceServers = [], microphone = false) {
        if (this.peer || this.closed)
            throw new Error('Peer already used');
        const peer = this.peer = new RTCPeerConnection({ iceServers });
        peer.ontrack = event => {
            if (event.track.kind !== 'audio')
                return;
            this.audio.srcObject = new MediaStream([event.track]);
            this.audio.play().catch(error => this.dispatchEvent(new CustomEvent('playbackblocked', { detail: error })));
        };
        peer.onconnectionstatechange = () => {
            if (peer.connectionState === 'failed') {
                this.dispatchEvent(new Event('error'));
                this.close();
            }
        };
        if (microphone) {
            this.input = await navigator.mediaDevices.getUserMedia({ audio: true });
            for (const track of this.input.getTracks())
                peer.addTrack(track, this.input);
        }
        else
            peer.addTransceiver('audio', { direction: 'recvonly' });
        const channel = this.channel = peer.createDataChannel('marona', { ordered: true });
        channel.onopen = () => { this.connected(); this.dispatchEvent(new Event('open')); };
        channel.onclose = () => this.close();
        channel.onmessage = event => {
            if (typeof event.data !== 'string' || new TextEncoder().encode(event.data).length > 262144) {
                this.dispatchEvent(new Event('error'));
                this.close();
                return;
            }
            if (this.listening)
                this.dispatchEvent(new MessageEvent('message', { data: event.data }));
            else if (this.pending.length < 256)
                this.pending.push(new MessageEvent('message', { data: event.data }));
            else
                this.close();
        };
        const gathered = new Promise((resolve, reject) => {
            const timer = setTimeout(() => reject(new Error('ICE gathering timed out')), 15000);
            peer.addEventListener('icegatheringstatechange', () => { if (peer.iceGatheringState === 'complete') {
                clearTimeout(timer);
                resolve();
            } });
        });
        await peer.setLocalDescription(await peer.createOffer());
        await gathered;
        return { type: 'offer', sdp: peer.localDescription.sdp };
    }
    async answer(value) {
        if (value.type !== 'answer' || typeof value.sdp !== 'string' || value.sdp.length > 65536 || !value.sdp.includes('a=fingerprint:'))
            throw new Error('Invalid WebRTC answer');
        await this.peer.setRemoteDescription(value);
    }
    send(event) {
        if (this.readyState !== 1 || new TextEncoder().encode(event).length > 262144 || this.channel.bufferedAmount > 1048576)
            throw new Error('WebRTC connection is unavailable or backpressured');
        this.channel.send(event);
    }
    async resume() { await this.audio.play(); }
    close() {
        if (this.closed)
            return;
        this.closed = true;
        this.failed(new Error('WebRTC connection closed'));
        this.input?.getTracks().forEach(track => track.stop());
        this.channel?.close();
        this.peer?.close();
        this.audio.pause();
        this.audio.srcObject = null;
        this.pending = [];
        this.dispatchEvent(new Event('close'));
    }
}