agents
Version:
A home for your AI agents
568 lines (567 loc) • 23.2 kB
JavaScript
import { i as _classPrivateFieldInitSpec, n as _classPrivateFieldSet2, r as _assertClassBrand, t as _classPrivateFieldGet2 } from "../classPrivateFieldGet2-DZBYAB34.js";
import { t as _classPrivateMethodInitSpec } from "../classPrivateMethodInitSpec-qMjJ6sHQ.js";
import { VoiceProviderError, logVoiceError, toVoiceError } from "./errors.js";
//#region src/voice/workers-ai.ts
/**
* Workers AI provider implementations for the voice pipeline.
*
* These are convenience classes that wrap the Workers AI binding
* (env.AI) for STT and TTS. They are not required — any object
* satisfying the provider interfaces works.
*/
var _ai = /* @__PURE__ */ new WeakMap();
var _model = /* @__PURE__ */ new WeakMap();
var _speaker = /* @__PURE__ */ new WeakMap();
/**
* Workers AI text-to-speech provider.
*
* @example
* ```ts
* class MyAgent extends VoiceAgent<Env> {
* tts = new WorkersAITTS(this.env.AI);
* }
* ```
*/
var WorkersAITTS = class {
constructor(ai, options) {
_classPrivateFieldInitSpec(this, _ai, void 0);
_classPrivateFieldInitSpec(this, _model, void 0);
_classPrivateFieldInitSpec(this, _speaker, void 0);
_classPrivateFieldSet2(_ai, this, ai);
_classPrivateFieldSet2(_model, this, options?.model ?? "@cf/deepgram/aura-1");
_classPrivateFieldSet2(_speaker, this, options?.speaker ?? "asteria");
}
async synthesize(text, signal) {
const response = await _classPrivateFieldGet2(_ai, this).run(_classPrivateFieldGet2(_model, this), {
text,
speaker: _classPrivateFieldGet2(_speaker, this)
}, {
returnRawResponse: true,
...signal ? { signal } : {}
});
if (!response.ok) {
logVoiceError({
component: "WorkersAITTS",
stage: "synthesize",
message: "Workers AI TTS request failed",
error: new VoiceProviderError("Workers AI TTS request failed", { status: response.status })
});
return null;
}
return await response.arrayBuffer();
}
};
var _ai2 = /* @__PURE__ */ new WeakMap();
var _sampleRate = /* @__PURE__ */ new WeakMap();
var _eotThreshold = /* @__PURE__ */ new WeakMap();
var _eagerEotThreshold = /* @__PURE__ */ new WeakMap();
var _eotTimeoutMs = /* @__PURE__ */ new WeakMap();
var _keyterms = /* @__PURE__ */ new WeakMap();
/**
* Workers AI continuous speech-to-text provider using the Flux model.
*
* Flux is a conversational STT model with built-in end-of-turn detection.
* A single session is created per call and receives all audio continuously.
* The model detects speech boundaries and fires `onUtterance` when a
* turn is complete — no client-side silence detection needed for STT.
*
* Recommended for `withVoice` (conversational voice agents).
*
* @example
* ```ts
* import { Agent } from "agents";
* import { withVoice, WorkersAIFluxSTT, WorkersAITTS } from "agents/voice";
*
* const VoiceAgent = withVoice(Agent);
*
* class MyAgent extends VoiceAgent<Env> {
* transcriber = new WorkersAIFluxSTT(this.env.AI);
* tts = new WorkersAITTS(this.env.AI);
*
* async onTurn(transcript, context) { ... }
* }
* ```
*/
var WorkersAIFluxSTT = class {
constructor(ai, options) {
_classPrivateFieldInitSpec(this, _ai2, void 0);
_classPrivateFieldInitSpec(this, _sampleRate, void 0);
_classPrivateFieldInitSpec(this, _eotThreshold, void 0);
_classPrivateFieldInitSpec(this, _eagerEotThreshold, void 0);
_classPrivateFieldInitSpec(this, _eotTimeoutMs, void 0);
_classPrivateFieldInitSpec(this, _keyterms, void 0);
_classPrivateFieldSet2(_ai2, this, ai);
_classPrivateFieldSet2(_sampleRate, this, options?.sampleRate ?? 16e3);
_classPrivateFieldSet2(_eotThreshold, this, options?.eotThreshold);
_classPrivateFieldSet2(_eagerEotThreshold, this, options?.eagerEotThreshold);
_classPrivateFieldSet2(_eotTimeoutMs, this, options?.eotTimeoutMs);
_classPrivateFieldSet2(_keyterms, this, options?.keyterms);
}
createSession(options) {
return new FluxSession(_classPrivateFieldGet2(_ai2, this), {
sampleRate: _classPrivateFieldGet2(_sampleRate, this),
eotThreshold: _classPrivateFieldGet2(_eotThreshold, this),
eagerEotThreshold: _classPrivateFieldGet2(_eagerEotThreshold, this),
eotTimeoutMs: _classPrivateFieldGet2(_eotTimeoutMs, this),
keyterms: _classPrivateFieldGet2(_keyterms, this)
}, options);
}
};
var _onInterim = /* @__PURE__ */ new WeakMap();
var _onSpeechStart = /* @__PURE__ */ new WeakMap();
var _onUtterance = /* @__PURE__ */ new WeakMap();
var _onFatalError = /* @__PURE__ */ new WeakMap();
var _ws = /* @__PURE__ */ new WeakMap();
var _connected = /* @__PURE__ */ new WeakMap();
var _closed = /* @__PURE__ */ new WeakMap();
var _fatalReported = /* @__PURE__ */ new WeakMap();
var _pendingChunks = /* @__PURE__ */ new WeakMap();
var _currentTranscript = /* @__PURE__ */ new WeakMap();
var _ready = /* @__PURE__ */ new WeakMap();
var _resolveReady = /* @__PURE__ */ new WeakMap();
var _rejectReady = /* @__PURE__ */ new WeakMap();
var _FluxSession_brand = /* @__PURE__ */ new WeakSet();
/**
* Per-call Flux transcription session. Lives for the entire call.
*
* Handles multi-turn conversations: on EndOfTurn, fires onUtterance
* and resets transcript state for the next turn. On StartOfTurn,
* clears accumulated text. The session stays alive across turns
* and is only closed on end_call or disconnect.
*/
var FluxSession = class {
constructor(ai, config, options) {
_classPrivateMethodInitSpec(this, _FluxSession_brand);
_classPrivateFieldInitSpec(this, _onInterim, void 0);
_classPrivateFieldInitSpec(this, _onSpeechStart, void 0);
_classPrivateFieldInitSpec(this, _onUtterance, void 0);
_classPrivateFieldInitSpec(this, _onFatalError, void 0);
_classPrivateFieldInitSpec(this, _ws, null);
_classPrivateFieldInitSpec(this, _connected, false);
_classPrivateFieldInitSpec(this, _closed, false);
_classPrivateFieldInitSpec(this, _fatalReported, false);
_classPrivateFieldInitSpec(this, _pendingChunks, []);
_classPrivateFieldInitSpec(this, _currentTranscript, "");
_classPrivateFieldInitSpec(this, _ready, void 0);
_classPrivateFieldInitSpec(this, _resolveReady, null);
_classPrivateFieldInitSpec(this, _rejectReady, null);
_classPrivateFieldSet2(_onInterim, this, options?.onInterim);
_classPrivateFieldSet2(_onSpeechStart, this, options?.onSpeechStart);
_classPrivateFieldSet2(_onUtterance, this, options?.onUtterance);
_classPrivateFieldSet2(_onFatalError, this, options?.onFatalError);
_classPrivateFieldSet2(_ready, this, new Promise((resolve, reject) => {
_classPrivateFieldSet2(_resolveReady, this, resolve);
_classPrivateFieldSet2(_rejectReady, this, reject);
}));
_classPrivateFieldGet2(_ready, this).catch(() => {});
_assertClassBrand(_FluxSession_brand, this, _connect).call(this, ai, config);
}
waitUntilReady() {
return _classPrivateFieldGet2(_ready, this);
}
feed(chunk) {
if (_classPrivateFieldGet2(_closed, this)) return;
if (_classPrivateFieldGet2(_connected, this) && _classPrivateFieldGet2(_ws, this)) _classPrivateFieldGet2(_ws, this).send(chunk);
else _classPrivateFieldGet2(_pendingChunks, this).push(chunk);
}
close() {
if (_classPrivateFieldGet2(_closed, this)) return;
_classPrivateFieldSet2(_closed, this, true);
_classPrivateFieldSet2(_pendingChunks, this, []);
if (_classPrivateFieldGet2(_ws, this)) {
try {
_classPrivateFieldGet2(_ws, this).close();
} catch {}
_classPrivateFieldSet2(_ws, this, null);
}
_classPrivateFieldSet2(_connected, this, false);
_assertClassBrand(_FluxSession_brand, this, _resolveReadiness).call(this);
}
};
async function _connect(ai, config) {
try {
const input = {
encoding: "linear16",
sample_rate: String(config.sampleRate)
};
if (config.eotThreshold != null) input.eot_threshold = String(config.eotThreshold);
if (config.eagerEotThreshold != null) input.eager_eot_threshold = String(config.eagerEotThreshold);
if (config.eotTimeoutMs != null) input.eot_timeout_ms = String(config.eotTimeoutMs);
if (config.keyterms?.length) input.keyterm = config.keyterms;
const resp = await ai.run("@cf/deepgram/flux", input, { websocket: true });
if (_classPrivateFieldGet2(_closed, this)) {
const ws = resp.webSocket;
if (ws) {
ws.accept();
ws.close();
}
_assertClassBrand(_FluxSession_brand, this, _resolveReadiness).call(this);
return;
}
const ws = resp.webSocket;
if (!ws) {
const error = /* @__PURE__ */ new Error("Workers AI Flux STT did not return a WebSocket");
logVoiceError({
component: "FluxSTT",
stage: "connection",
message: "Flux STT WebSocket upgrade failed",
error
});
_assertClassBrand(_FluxSession_brand, this, _reportFatal).call(this, error);
_assertClassBrand(_FluxSession_brand, this, _rejectReadiness).call(this, error);
return;
}
ws.accept();
_classPrivateFieldSet2(_ws, this, ws);
_classPrivateFieldSet2(_connected, this, true);
ws.addEventListener("message", (event) => {
_assertClassBrand(_FluxSession_brand, this, _handleMessage).call(this, event);
});
ws.addEventListener("close", () => {
_classPrivateFieldSet2(_connected, this, false);
if (_classPrivateFieldGet2(_closed, this)) return;
const error = /* @__PURE__ */ new Error("Workers AI Flux STT WebSocket closed unexpectedly");
logVoiceError({
component: "FluxSTT",
stage: "websocket_close",
message: "Flux STT WebSocket closed unexpectedly",
error
});
_assertClassBrand(_FluxSession_brand, this, _reportFatal).call(this, error);
});
ws.addEventListener("error", (event) => {
const error = new Error("Workers AI Flux STT WebSocket error", { cause: event });
logVoiceError({
component: "FluxSTT",
stage: "websocket",
message: "Flux STT WebSocket error",
error
});
_classPrivateFieldSet2(_connected, this, false);
_assertClassBrand(_FluxSession_brand, this, _reportFatal).call(this, error);
});
for (const chunk of _classPrivateFieldGet2(_pendingChunks, this)) ws.send(chunk);
_classPrivateFieldSet2(_pendingChunks, this, []);
_assertClassBrand(_FluxSession_brand, this, _resolveReadiness).call(this);
} catch (error) {
const voiceError = toVoiceError(error, "Flux STT connection failed");
logVoiceError({
component: "FluxSTT",
stage: "connection",
message: "Flux STT connection failed",
error: voiceError
});
_assertClassBrand(_FluxSession_brand, this, _reportFatal).call(this, voiceError);
_assertClassBrand(_FluxSession_brand, this, _rejectReadiness).call(this, voiceError);
}
}
function _resolveReadiness() {
const resolve = _classPrivateFieldGet2(_resolveReady, this);
if (!resolve) return;
_classPrivateFieldSet2(_resolveReady, this, null);
_classPrivateFieldSet2(_rejectReady, this, null);
resolve();
}
function _rejectReadiness(reason) {
const reject = _classPrivateFieldGet2(_rejectReady, this);
if (!reject) return;
_classPrivateFieldSet2(_resolveReady, this, null);
_classPrivateFieldSet2(_rejectReady, this, null);
reject(reason);
}
function _reportFatal(error) {
if (_classPrivateFieldGet2(_closed, this) || _classPrivateFieldGet2(_fatalReported, this)) return;
_classPrivateFieldSet2(_fatalReported, this, true);
_classPrivateFieldGet2(_onFatalError, this)?.call(this, error);
}
function _handleMessage(event) {
if (_classPrivateFieldGet2(_closed, this)) return;
try {
const data = typeof event.data === "string" ? JSON.parse(event.data) : null;
if (!data || !data.event) return;
const transcript = data.transcript ?? "";
switch (data.event) {
case "StartOfTurn":
_classPrivateFieldSet2(_currentTranscript, this, "");
_classPrivateFieldGet2(_onSpeechStart, this)?.call(this, transcript || void 0);
if (transcript) {
_classPrivateFieldSet2(_currentTranscript, this, transcript);
_classPrivateFieldGet2(_onInterim, this)?.call(this, transcript);
}
break;
case "Update":
if (transcript) {
_classPrivateFieldSet2(_currentTranscript, this, transcript);
_classPrivateFieldGet2(_onInterim, this)?.call(this, transcript);
}
break;
case "EndOfTurn": {
const finalTranscript = transcript || _classPrivateFieldGet2(_currentTranscript, this);
_classPrivateFieldSet2(_currentTranscript, this, "");
if (finalTranscript) _classPrivateFieldGet2(_onUtterance, this)?.call(this, finalTranscript);
break;
}
case "EagerEndOfTurn":
if (transcript) {
_classPrivateFieldSet2(_currentTranscript, this, transcript);
_classPrivateFieldGet2(_onInterim, this)?.call(this, transcript);
}
break;
case "TurnResumed":
_classPrivateFieldSet2(_currentTranscript, this, transcript);
if (transcript) _classPrivateFieldGet2(_onInterim, this)?.call(this, transcript);
break;
}
} catch {}
}
var _ai3 = /* @__PURE__ */ new WeakMap();
var _sampleRate2 = /* @__PURE__ */ new WeakMap();
var _language = /* @__PURE__ */ new WeakMap();
var _endpointingMs = /* @__PURE__ */ new WeakMap();
var _utteranceEndMs = /* @__PURE__ */ new WeakMap();
var _smartFormat = /* @__PURE__ */ new WeakMap();
var _punctuate = /* @__PURE__ */ new WeakMap();
var _keyterms2 = /* @__PURE__ */ new WeakMap();
/**
* Workers AI continuous speech-to-text provider using Nova 3.
*
* Nova 3 is a high-accuracy STT model with streaming WebSocket support.
* A single session is created per call and receives all audio continuously.
* Server-side VAD events and endpointing handle speech boundary detection.
*
* Recommended for `withVoiceInput` (dictation / voice input UIs).
*
* @example
* ```ts
* import { Agent } from "agents";
* import { withVoiceInput, WorkersAINova3STT } from "agents/voice";
*
* const InputAgent = withVoiceInput(Agent);
*
* class MyAgent extends InputAgent<Env> {
* transcriber = new WorkersAINova3STT(this.env.AI);
*
* onTranscript(text, connection) { ... }
* }
* ```
*/
var WorkersAINova3STT = class {
constructor(ai, options) {
_classPrivateFieldInitSpec(this, _ai3, void 0);
_classPrivateFieldInitSpec(this, _sampleRate2, void 0);
_classPrivateFieldInitSpec(this, _language, void 0);
_classPrivateFieldInitSpec(this, _endpointingMs, void 0);
_classPrivateFieldInitSpec(this, _utteranceEndMs, void 0);
_classPrivateFieldInitSpec(this, _smartFormat, void 0);
_classPrivateFieldInitSpec(this, _punctuate, void 0);
_classPrivateFieldInitSpec(this, _keyterms2, void 0);
_classPrivateFieldSet2(_ai3, this, ai);
_classPrivateFieldSet2(_sampleRate2, this, options?.sampleRate ?? 16e3);
_classPrivateFieldSet2(_language, this, options?.language ?? "en");
_classPrivateFieldSet2(_endpointingMs, this, options?.endpointingMs ?? 300);
_classPrivateFieldSet2(_utteranceEndMs, this, options?.utteranceEndMs ?? 1e3);
_classPrivateFieldSet2(_smartFormat, this, options?.smartFormat ?? true);
_classPrivateFieldSet2(_punctuate, this, options?.punctuate ?? true);
_classPrivateFieldSet2(_keyterms2, this, options?.keyterms);
}
createSession(options) {
return new Nova3Session(_classPrivateFieldGet2(_ai3, this), {
sampleRate: _classPrivateFieldGet2(_sampleRate2, this),
language: _classPrivateFieldGet2(_language, this),
endpointingMs: _classPrivateFieldGet2(_endpointingMs, this),
utteranceEndMs: _classPrivateFieldGet2(_utteranceEndMs, this),
smartFormat: _classPrivateFieldGet2(_smartFormat, this),
punctuate: _classPrivateFieldGet2(_punctuate, this),
keyterms: _classPrivateFieldGet2(_keyterms2, this)
}, options);
}
};
var _onInterim2 = /* @__PURE__ */ new WeakMap();
var _onUtterance2 = /* @__PURE__ */ new WeakMap();
var _onFatalError2 = /* @__PURE__ */ new WeakMap();
var _ws2 = /* @__PURE__ */ new WeakMap();
var _connected2 = /* @__PURE__ */ new WeakMap();
var _closed2 = /* @__PURE__ */ new WeakMap();
var _fatalReported2 = /* @__PURE__ */ new WeakMap();
var _pendingChunks2 = /* @__PURE__ */ new WeakMap();
var _ready2 = /* @__PURE__ */ new WeakMap();
var _resolveReady2 = /* @__PURE__ */ new WeakMap();
var _rejectReady2 = /* @__PURE__ */ new WeakMap();
var _finalizedSegments = /* @__PURE__ */ new WeakMap();
var _Nova3Session_brand = /* @__PURE__ */ new WeakSet();
/**
* Per-call Nova 3 transcription session. Lives for the entire call.
*
* Uses Nova 3's endpointing and VAD events to detect utterance
* boundaries. When a result arrives with `speech_final: true`,
* the accumulated finalized segments are emitted as an utterance.
*/
var Nova3Session = class {
constructor(ai, config, options) {
_classPrivateMethodInitSpec(this, _Nova3Session_brand);
_classPrivateFieldInitSpec(this, _onInterim2, void 0);
_classPrivateFieldInitSpec(this, _onUtterance2, void 0);
_classPrivateFieldInitSpec(this, _onFatalError2, void 0);
_classPrivateFieldInitSpec(this, _ws2, null);
_classPrivateFieldInitSpec(this, _connected2, false);
_classPrivateFieldInitSpec(this, _closed2, false);
_classPrivateFieldInitSpec(this, _fatalReported2, false);
_classPrivateFieldInitSpec(this, _pendingChunks2, []);
_classPrivateFieldInitSpec(this, _ready2, void 0);
_classPrivateFieldInitSpec(this, _resolveReady2, null);
_classPrivateFieldInitSpec(this, _rejectReady2, null);
_classPrivateFieldInitSpec(this, _finalizedSegments, []);
_classPrivateFieldSet2(_onInterim2, this, options?.onInterim);
_classPrivateFieldSet2(_onUtterance2, this, options?.onUtterance);
_classPrivateFieldSet2(_onFatalError2, this, options?.onFatalError);
_classPrivateFieldSet2(_ready2, this, new Promise((resolve, reject) => {
_classPrivateFieldSet2(_resolveReady2, this, resolve);
_classPrivateFieldSet2(_rejectReady2, this, reject);
}));
_classPrivateFieldGet2(_ready2, this).catch(() => {});
_assertClassBrand(_Nova3Session_brand, this, _connect2).call(this, ai, config);
}
waitUntilReady() {
return _classPrivateFieldGet2(_ready2, this);
}
feed(chunk) {
if (_classPrivateFieldGet2(_closed2, this)) return;
if (_classPrivateFieldGet2(_connected2, this) && _classPrivateFieldGet2(_ws2, this)) _classPrivateFieldGet2(_ws2, this).send(chunk);
else _classPrivateFieldGet2(_pendingChunks2, this).push(chunk);
}
close() {
if (_classPrivateFieldGet2(_closed2, this)) return;
_classPrivateFieldSet2(_closed2, this, true);
_classPrivateFieldSet2(_pendingChunks2, this, []);
if (_classPrivateFieldGet2(_ws2, this)) {
try {
_classPrivateFieldGet2(_ws2, this).close();
} catch {}
_classPrivateFieldSet2(_ws2, this, null);
}
_classPrivateFieldSet2(_connected2, this, false);
_assertClassBrand(_Nova3Session_brand, this, _resolveReadiness2).call(this);
}
};
async function _connect2(ai, config) {
try {
const input = {
encoding: "linear16",
sample_rate: String(config.sampleRate),
language: config.language,
interim_results: "true",
vad_events: "true",
endpointing: String(config.endpointingMs),
utterance_end_ms: String(config.utteranceEndMs),
smart_format: String(config.smartFormat),
punctuate: String(config.punctuate)
};
if (config.keyterms?.length) input.keyterm = config.keyterms;
const resp = await ai.run("@cf/deepgram/nova-3", input, { websocket: true });
if (_classPrivateFieldGet2(_closed2, this)) {
const ws = resp.webSocket;
if (ws) {
ws.accept();
ws.close();
}
_assertClassBrand(_Nova3Session_brand, this, _resolveReadiness2).call(this);
return;
}
const ws = resp.webSocket;
if (!ws) {
const error = /* @__PURE__ */ new Error("Workers AI Nova-3 STT did not return a WebSocket");
logVoiceError({
component: "Nova3STT",
stage: "connection",
message: "Nova-3 STT WebSocket upgrade failed",
error
});
_assertClassBrand(_Nova3Session_brand, this, _reportFatal2).call(this, error);
_assertClassBrand(_Nova3Session_brand, this, _rejectReadiness2).call(this, error);
return;
}
ws.accept();
_classPrivateFieldSet2(_ws2, this, ws);
_classPrivateFieldSet2(_connected2, this, true);
ws.addEventListener("message", (event) => {
_assertClassBrand(_Nova3Session_brand, this, _handleMessage2).call(this, event);
});
ws.addEventListener("close", () => {
_classPrivateFieldSet2(_connected2, this, false);
if (_classPrivateFieldGet2(_closed2, this)) return;
const error = /* @__PURE__ */ new Error("Workers AI Nova-3 STT WebSocket closed unexpectedly");
logVoiceError({
component: "Nova3STT",
stage: "websocket_close",
message: "Nova-3 STT WebSocket closed unexpectedly",
error
});
_assertClassBrand(_Nova3Session_brand, this, _reportFatal2).call(this, error);
});
ws.addEventListener("error", (event) => {
const error = new Error("Workers AI Nova-3 STT WebSocket error", { cause: event });
logVoiceError({
component: "Nova3STT",
stage: "websocket",
message: "Nova-3 STT WebSocket error",
error
});
_classPrivateFieldSet2(_connected2, this, false);
_assertClassBrand(_Nova3Session_brand, this, _reportFatal2).call(this, error);
});
for (const chunk of _classPrivateFieldGet2(_pendingChunks2, this)) ws.send(chunk);
_classPrivateFieldSet2(_pendingChunks2, this, []);
_assertClassBrand(_Nova3Session_brand, this, _resolveReadiness2).call(this);
} catch (error) {
const voiceError = toVoiceError(error, "Nova-3 STT connection failed");
logVoiceError({
component: "Nova3STT",
stage: "connection",
message: "Nova-3 STT connection failed",
error: voiceError
});
_assertClassBrand(_Nova3Session_brand, this, _reportFatal2).call(this, voiceError);
_assertClassBrand(_Nova3Session_brand, this, _rejectReadiness2).call(this, voiceError);
}
}
function _resolveReadiness2() {
const resolve = _classPrivateFieldGet2(_resolveReady2, this);
if (!resolve) return;
_classPrivateFieldSet2(_resolveReady2, this, null);
_classPrivateFieldSet2(_rejectReady2, this, null);
resolve();
}
function _rejectReadiness2(reason) {
const reject = _classPrivateFieldGet2(_rejectReady2, this);
if (!reject) return;
_classPrivateFieldSet2(_resolveReady2, this, null);
_classPrivateFieldSet2(_rejectReady2, this, null);
reject(reason);
}
function _reportFatal2(error) {
if (_classPrivateFieldGet2(_closed2, this) || _classPrivateFieldGet2(_fatalReported2, this)) return;
_classPrivateFieldSet2(_fatalReported2, this, true);
_classPrivateFieldGet2(_onFatalError2, this)?.call(this, error);
}
function _handleMessage2(event) {
if (_classPrivateFieldGet2(_closed2, this)) return;
try {
const data = typeof event.data === "string" ? JSON.parse(event.data) : null;
if (!data) return;
if (data.type === "Results") {
const transcript = data.channel?.alternatives?.[0]?.transcript ?? "";
if (data.is_final && transcript) _classPrivateFieldGet2(_finalizedSegments, this).push(transcript);
if (data.speech_final) {
const fullTranscript = _classPrivateFieldGet2(_finalizedSegments, this).join(" ").trim();
_classPrivateFieldSet2(_finalizedSegments, this, []);
if (fullTranscript) _classPrivateFieldGet2(_onUtterance2, this)?.call(this, fullTranscript);
} else if (!data.is_final && transcript) {
const display = _classPrivateFieldGet2(_finalizedSegments, this).length > 0 ? _classPrivateFieldGet2(_finalizedSegments, this).join(" ") + " " + transcript : transcript;
_classPrivateFieldGet2(_onInterim2, this)?.call(this, display);
}
}
} catch {}
}
//#endregion
export { WorkersAIFluxSTT, WorkersAINova3STT, WorkersAITTS };
//# sourceMappingURL=workers-ai.js.map