UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

183 lines (182 loc) • 5.38 kB
import { n as createCpuTrackedWorker } from "../../worker-cpu-dNstslyu.mjs"; import { t as createPluginRuntimeProcessEntrypoint } from "../../runtime-process-manifest-DmUAODRC.mjs"; import { m as toStringifiedError, p as toErrorObject } from "../../error-coercion-hDrHOSGy.mjs"; import { a as resolveRuntimeWorkerUrl, n as resolveRuntimeWorkerArgv } from "../../runtime-worker-url-DRDkoS4J.mjs"; import { a as createDeferred } from "../../extension-shared-CJ4teJ7p.mjs"; import "../../process-runtime-DcBPEA8Y.mjs"; import "../../error-runtime-BM740gJj.mjs"; import { a as OpenAIQuicksilverPendingAudio } from "../../realtime-quicksilver-audio-buffer-BbbgaIzR.mjs"; //#region extensions/openai/realtime-quicksilver-peer.runtime.ts var OpenAIQuicksilverAudioPeer = class OpenAIQuicksilverAudioPeer { static async create(e) { e.signal?.throwIfAborted(); const t = resolveRuntimeWorkerUrl(createPluginRuntimeProcessEntrypoint("openAiRealtimeAudio", import.meta.url)); const n = createCpuTrackedWorker(t, { workerData: { iceServers: e.iceServers, reportMediaErrors: Boolean(e.callbacks.onMediaError), output: e.output }, execArgv: resolveRuntimeWorkerArgv(t).slice(0, -1), transferList: e.output ? [e.output.port] : [] }); const r = new OpenAIQuicksilverAudioPeer(n, e.callbacks, e.output); const abort = () => r.close(); e.signal?.addEventListener("abort", abort, { once: true }); try { await r.ready.promise; e.signal?.throwIfAborted(); return r; } catch (t) { r.close(); e.signal?.throwIfAborted(); throw t; } finally { e.signal?.removeEventListener("abort", abort); } } constructor(e, t, n) { this.worker = e; this.callbacks = t; this.output = n; this.closed = false; this.started = false; this.inputInFlight = false; this.outputGeneration = 0; this.pendingAudio = new OpenAIQuicksilverPendingAudio(); this.nextRequestId = 0; this.requests = /* @__PURE__ */ new Map(); this.ready = createDeferred(); e.on("message", (e) => { try { this.handleMessage(e); } catch (e) { this.fail(toStringifiedError(e)); } }); e.on("error", (e) => this.fail(toErrorObject(e, "GPT-Live audio worker failed"))); e.on("exit", (e) => { if (this.closeTimer) { clearTimeout(this.closeTimer); this.closeTimer = void 0; } if (!this.closed) this.fail(/* @__PURE__ */ new Error("GPT-Live audio worker exited unexpectedly (code " + e + ")")); }); } createOffer() { return this.request({ type: "offer", id: ++this.nextRequestId }); } async applyAnswer(e) { await this.request({ type: "answer", id: ++this.nextRequestId, sdp: e }); } adoptPendingAudio(e) { if (this.closed) { e.clear(); return; } if (this.inputInFlight || this.pendingAudio.length > 0) { e.clear(); throw new Error("GPT-Live WebRTC peer already owns pending audio"); } this.pendingAudio = e; this.flushInput(); } sendAudio(e) { if (this.closed) return; this.pendingAudio.append(e); this.flushInput(); } clearOutputAudio() { if (!this.closed) this.post({ type: "clear-output", generation: ++this.outputGeneration }); } close() { if (this.closed) return; this.closed = true; if (this.output) Atomics.store(new Int32Array(this.output.state, 0, 1), 0, 1); this.pendingAudio.clear(); const e = /* @__PURE__ */ new Error("GPT-Live audio worker closed"); this.ready.reject(e); for (const t of this.requests.values()) t.reject(e); this.requests.clear(); this.closeTimer = setTimeout(() => void this.worker.terminate(), 2e3); this.closeTimer.unref(); this.post({ type: "close" }); this.worker.unref(); } request(e) { if (this.closed) return Promise.reject(/* @__PURE__ */ new Error("GPT-Live audio worker closed")); return new Promise((t, n) => { this.requests.set(e.id, { resolve: t, reject: n }); this.post(e); }); } post(e, t = []) { this.worker.postMessage(e, t); } flushInput() { if (this.closed || this.inputInFlight || this.pendingAudio.length === 0) return; const e = this.pendingAudio.take(); this.inputInFlight = true; this.post({ type: "audio", audio: e }, [e.buffer]); } handleMessage(e) { if (this.closed) return; switch (e.type) { case "ready": this.started = true; this.ready.resolve(); return; case "result": case "request-error": { const t = this.requests.get(e.id); this.requests.delete(e.id); if (e.type === "result") t?.resolve(e.value); else t?.reject(new Error(e.message)); return; } case "input-ack": this.inputInFlight = false; this.flushInput(); return; case "audio": case "rtp": case "media-error": try { if (e.type === "audio") { if (e.generation !== this.outputGeneration) return; this.callbacks.onAudio(Buffer.from(e.audio.buffer, e.audio.byteOffset, e.audio.byteLength)); } else if (e.type === "rtp") this.callbacks.onRtpPacket?.(); else this.callbacks.onMediaError?.(new Error(e.message)); } finally { if (!this.closed) this.post({ type: `${e.type}-ack` }); } return; case "error": this.fail(new Error(e.message)); } } fail(e) { if (this.closed) return; const t = this.started; this.ready.reject(e); this.close(); if (t) this.callbacks.onError(e); } }; //#endregion export { OpenAIQuicksilverAudioPeer };