openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
216 lines (215 loc) • 6.08 kB
JavaScript
import { n as OpenAIQuicksilverPendingAudio } from "./realtime-quicksilver-audio-buffer-DMJoYbid.mjs";
import { createCpuTrackedWorker, createPluginRuntimeProcessEntrypoint, resolveRuntimeWorkerArgv, resolveRuntimeWorkerUrl } from "openclaw/plugin-sdk/process-runtime";
import { EventEmitter } from "node:events";
//#region extensions/openai/realtime-quicksilver-socket.shared.ts
const QUICKSILVER_SOCKET_CONTROL_BYTES = 1048576;
const QUICKSILVER_SOCKET_AUDIO_BATCH_BYTES = 9600;
//#endregion
//#region extensions/openai/realtime-quicksilver-socket.ts
/** A local socket-shaped control adapter, not a second media transport. */
var OpenAIQuicksilverWorkerSocket = class OpenAIQuicksilverWorkerSocket extends EventEmitter {
static create(e, t, n, r) {
const i = resolveRuntimeWorkerUrl(createPluginRuntimeProcessEntrypoint("openAiRealtimeSocket", import.meta.url));
const a = createCpuTrackedWorker(i, {
workerData: {
url: e,
options: {
headers: t.headers,
maxPayload: t.maxPayload
},
...n
},
execArgv: resolveRuntimeWorkerArgv(i).slice(0, -1)
});
return new OpenAIQuicksilverWorkerSocket(a, r, n.audioFormat?.encoding === "g711_ulaw");
}
constructor(e, t, n) {
super();
this.worker = e;
this.callbacks = t;
this.telephony = n;
this.readyState = 0;
this.mediaStopped = false;
this.started = false;
this.inputInFlight = false;
this.sendInFlight = false;
this.failed = false;
this.exited = false;
this.sends = [];
this.sendBytes = 0;
this.closePosted = false;
this.pending = new OpenAIQuicksilverPendingAudio(n ? 4e4 : 24e4, 1);
e.on("message", (e) => this.receive(e));
e.on("error", () => {
if (!this.closePosted) this.fail();
});
e.on("exit", () => {
this.exited = true;
if (this.closeTimer) clearTimeout(this.closeTimer);
if (this.readyState !== 3) {
if (this.readyState !== 2 || !this.closePosted) this.fail();
this.finishClose(1006, "media worker exited");
}
});
}
send(e) {
if (this.readyState !== 1) return;
const t = Buffer.byteLength(e);
if (this.sends.length >= 128 || this.sendBytes + t > 1048576) {
this.fail();
return;
}
this.sends.push(e);
this.sendBytes += t;
this.flushSends();
}
sendAudio(e) {
if (this.mediaStopped || this.readyState >= 2) return;
this.pending.append(e);
this.flushInput();
}
setAudioOutputPort(e) {
if (this.audioOutputState) throw new Error("GPT-Live audio output is already bound");
if (this.mediaStopped || this.readyState !== 1) {
Atomics.store(new Int32Array(e.state), 0, 1);
e.port.close();
return;
}
this.audioOutputState = new Int32Array(e.state);
this.post({
type: "audio-output",
output: e
}, [e.port]);
}
startAudio() {
if (this.mediaStopped || this.started || this.readyState !== 1) return;
this.started = true;
this.post({ type: "start-audio" });
}
stopAudio() {
if (this.mediaStopped) return;
this.mediaStopped = true;
if (this.audioOutputState) Atomics.store(this.audioOutputState, 0, 1);
this.pending.clear();
this.post({ type: "stop-audio" });
}
close(e = 1e3, t = "session closed") {
if (this.readyState >= 2) return;
this.stopAudio();
this.readyState = 2;
this.closing = {
code: e,
reason: t
};
this.scheduleCloseDeadline();
this.flushSends();
}
scheduleCloseDeadline() {
if (this.closeTimer || this.exited) return;
this.closeTimer = setTimeout(() => {
this.worker.terminate();
}, 2e3);
this.closeTimer.unref();
}
closeTransport() {
if (!this.closing || this.closePosted) return;
this.closePosted = true;
this.post({
type: "close",
...this.closing
});
}
post(e, t = []) {
if (this.exited) return;
this.worker.postMessage(e, t);
}
flushInput() {
if (this.inputInFlight || this.mediaStopped || !this.pending.length || this.readyState !== 1) return;
this.inputInFlight = true;
this.post({
type: "audio",
audio: this.pending.take(this.telephony ? 1600 : QUICKSILVER_SOCKET_AUDIO_BATCH_BYTES)
});
}
flushSends() {
if (this.sendInFlight || this.readyState === 0 || this.readyState === 3 || this.closePosted) return;
const e = this.sends.shift();
if (e === void 0) {
this.closeTransport();
return;
}
this.sendBytes -= Buffer.byteLength(e);
this.sendInFlight = true;
this.post({
type: "send",
payload: e
});
}
receive(e) {
if (e.type === "input-ack") {
this.inputInFlight = false;
this.flushInput();
return;
}
if (e.type === "send-ack") {
this.sendInFlight = false;
this.flushSends();
return;
}
try {
if (e.type === "close") {
this.finishClose(e.code, e.reason);
return;
}
if (e.type === "error") {
if (!this.closePosted) this.fail();
return;
}
if (this.readyState >= 2) return;
switch (e.type) {
case "open":
this.readyState = 1;
this.flushInput();
this.emit("open");
break;
case "frame":
this.emit("message", Buffer.from(e.data.buffer, e.data.byteOffset, e.data.byteLength), e.isBinary);
break;
case "audio": if (!this.mediaStopped && !this.audioOutputState) this.callbacks.onAudio(Buffer.from(e.audio.buffer, e.audio.byteOffset, e.audio.byteLength));
}
} finally {
this.post({ type: "event-ack" });
}
}
finishClose(e, t) {
if (this.readyState === 3) return;
this.mediaStopped = true;
if (this.audioOutputState) Atomics.store(this.audioOutputState, 0, 1);
this.pending.clear();
this.sends.length = 0;
this.sendBytes = 0;
this.readyState = 3;
this.closing = void 0;
this.emit("close", e, Buffer.from(t));
}
fail() {
if (this.failed || this.readyState === 3) return;
this.failed = true;
try {
this.emit("error", /* @__PURE__ */ new Error("GPT-Live media worker transport failed"));
} finally {
this.stopAudio();
this.readyState = 2;
this.sends.length = 0;
this.sendBytes = 0;
this.closing = {
code: 1011,
reason: "media transport failed"
};
this.scheduleCloseDeadline();
this.closeTransport();
}
}
};
//#endregion
export { OpenAIQuicksilverWorkerSocket };