@skaile/forge-voice 0.0.0-stage → 0.1.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.
- package/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/README.md +329 -2
- package/dist/browser/index.d.mts +2102 -0
- package/dist/browser/index.mjs +5808 -0
- package/dist/browser/index.mjs.map +1 -0
- package/dist/h3/index.d.mts +26 -0
- package/dist/h3/index.mjs +234 -0
- package/dist/h3/index.mjs.map +1 -0
- package/dist/index.d.mts +138 -0
- package/dist/index.mjs +1 -0
- package/dist/relay.d.mts +345 -0
- package/dist/relay.mjs +1194 -0
- package/dist/relay.mjs.map +1 -0
- package/dist/server/index.d.mts +57 -0
- package/dist/server/index.mjs +1 -0
- package/dist/speech-terms.d.mts +237 -0
- package/dist/wire.mjs +792 -0
- package/dist/wire.mjs.map +1 -0
- package/package.json +90 -4
package/dist/relay.mjs
ADDED
|
@@ -0,0 +1,1194 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
// src/server/audit.ts
|
|
3
|
+
const PREFIX = "VOICE_PROVIDER_PRIVACY:";
|
|
4
|
+
/**
|
|
5
|
+
* The audit line as one log string, e.g. `VOICE_PROVIDER_PRIVACY: Deepgram stream opened: user=u1
|
|
6
|
+
* host=api.eu.deepgram.com model=nova-3 language=de mip_opt_out=true keyterms=3`. The privacy values
|
|
7
|
+
* are read back from the URL actually sent. No audio, transcript, text, keyterm or key ever appears.
|
|
8
|
+
*/
|
|
9
|
+
function formatAuditLine(line) {
|
|
10
|
+
switch (line.event) {
|
|
11
|
+
case "deepgram-stream-opened": return `${PREFIX} Deepgram stream opened: user=${line.principalId} host=${line.host} model=${line.model} language=${line.language} mip_opt_out=${line.mipOptOut} keyterms=${line.keyterms}`;
|
|
12
|
+
case "deepgram-retranscription-requested": return `${PREFIX} Deepgram re-transcription requested: user=${line.principalId} host=${line.host} model=${line.model} bytes=${line.bytes} mip_opt_out=${line.mipOptOut} keyterms=${line.keyterms}`;
|
|
13
|
+
case "elevenlabs-stream-opened": return `${PREFIX} ElevenLabs stream opened: user=${line.principalId} voice=${line.voiceId} model=${line.model} enable_logging=${line.enableLogging}`;
|
|
14
|
+
case "elevenlabs-stream-closed": return `${PREFIX} ElevenLabs stream closed: user=${line.principalId} chars=${line.chars}`;
|
|
15
|
+
case "elevenlabs-speech-requested": return `${PREFIX} ElevenLabs speech requested: voice=${line.voiceId} model=${line.model} chars=${line.chars} enable_logging=${line.enableLogging}`;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
// src/server/deepgram-request.ts
|
|
19
|
+
/**
|
|
20
|
+
* The one place a Deepgram request URL is built. Deepgram keeps audio and transcripts in memory
|
|
21
|
+
* only for requests that opt out of its Model Improvement Program (`mip_opt_out=true`), so the
|
|
22
|
+
* opt-out is set here, last, and cannot be omitted or overridden by a caller.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* A bare Deepgram API host (`api.deepgram.com`, `api.eu.deepgram.com`): no scheme, port or path,
|
|
26
|
+
* so the key in the request header goes to Deepgram and nowhere else.
|
|
27
|
+
*/
|
|
28
|
+
const DEEPGRAM_HOST_PATTERN = /^([a-z0-9-]+\.)+deepgram\.com$/;
|
|
29
|
+
const DEEPGRAM_MIP_OPT_OUT = "true";
|
|
30
|
+
/**
|
|
31
|
+
* Whether `host` is a bare host (no scheme, port, path or fragment, which could redirect the key) that
|
|
32
|
+
* the pattern matches as a whole. An unanchored or `g`-flagged pattern cannot widen this.
|
|
33
|
+
*/
|
|
34
|
+
function isAllowedDeepgramHost(host, hostPattern = DEEPGRAM_HOST_PATTERN) {
|
|
35
|
+
if (!/^[a-z0-9.-]+$/.test(host)) return false;
|
|
36
|
+
const match = new RegExp(hostPattern.source, hostPattern.flags.replace(/[gy]/g, "")).exec(host);
|
|
37
|
+
return match !== null && match.index === 0 && match[0] === host;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A list value repeats the parameter (`keyterm=a&keyterm=b`), as Deepgram reads it; each value is
|
|
41
|
+
* percent-encoded by `URLSearchParams`, so a term can never add a parameter of its own. Throws for a
|
|
42
|
+
* host that does not match the pattern.
|
|
43
|
+
*/
|
|
44
|
+
function buildDeepgramUrl(host, protocol, path, params, { hostPattern = DEEPGRAM_HOST_PATTERN } = {}) {
|
|
45
|
+
if (!isAllowedDeepgramHost(host, hostPattern)) throw new Error(`Not a Deepgram API host: ${host}`);
|
|
46
|
+
const url = new URL(`${protocol}://${host}${path}`);
|
|
47
|
+
for (const [key, value] of Object.entries(params)) if (typeof value === "string") url.searchParams.set(key, value);
|
|
48
|
+
else {
|
|
49
|
+
url.searchParams.delete(key);
|
|
50
|
+
for (const item of value) url.searchParams.append(key, item);
|
|
51
|
+
}
|
|
52
|
+
url.searchParams.set("mip_opt_out", DEEPGRAM_MIP_OPT_OUT);
|
|
53
|
+
return url;
|
|
54
|
+
}
|
|
55
|
+
// src/server/elevenlabs-request.ts
|
|
56
|
+
/**
|
|
57
|
+
* The one place an ElevenLabs request URL is built. `enable_logging=false` is ElevenLabs'
|
|
58
|
+
* per-request Zero Retention Mode: the text and audio of the request are not stored. It is set
|
|
59
|
+
* here, last, and cannot be omitted or overridden by a caller, the same way `buildDeepgramUrl`
|
|
60
|
+
* forces Deepgram's opt-out.
|
|
61
|
+
*/
|
|
62
|
+
const ELEVENLABS_HOST = "api.elevenlabs.io";
|
|
63
|
+
const ELEVENLABS_ENABLE_LOGGING = "false";
|
|
64
|
+
/** `wss` for the streaming-input sockets (the text-to-dialogue stream), else `https`. */
|
|
65
|
+
function buildElevenLabsUrl(path, params = {}, { protocol = "https" } = {}) {
|
|
66
|
+
const url = new URL(`${protocol}://${ELEVENLABS_HOST}${path}`);
|
|
67
|
+
for (const [key, value] of Object.entries(params)) url.searchParams.set(key, value);
|
|
68
|
+
url.searchParams.set("enable_logging", ELEVENLABS_ENABLE_LOGGING);
|
|
69
|
+
return url;
|
|
70
|
+
}
|
|
71
|
+
// src/server/key-redact.ts
|
|
72
|
+
/** A key shorter than this is not redacted: it would blank ordinary words out of a message. */
|
|
73
|
+
const MIN_REDACTED_KEY_LENGTH = 8;
|
|
74
|
+
/** A provider's text with the key it was called with removed, for a reply or a log line. */
|
|
75
|
+
function withoutKey(text, key) {
|
|
76
|
+
return key.length >= MIN_REDACTED_KEY_LENGTH ? text.split(key).join("[redacted]") : text;
|
|
77
|
+
}
|
|
78
|
+
// src/shared/audio-tags.ts
|
|
79
|
+
/**
|
|
80
|
+
* Audio tags in a spoken reply: `[[whispers]]`, `[[laughs]]`. The reply-format contract between an
|
|
81
|
+
* agent that writes tags and the speech model that performs them. The stored text keeps them; what a
|
|
82
|
+
* chat shows of a voice reply, what Talk counts as heard (`heardChars`) and what a server reuses of one
|
|
83
|
+
* (history, search, recaps) is the text without them. Only voice replies have tags: anywhere else
|
|
84
|
+
* `[[...]]` is code or a wiki link. Browser and server import the same functions, so both count
|
|
85
|
+
* offsets the same way; keep this file free of runtime-specific imports.
|
|
86
|
+
*/
|
|
87
|
+
/** `[[` + letters, spaces, commas, hyphens or apostrophes (at most 40) + `]]`. */
|
|
88
|
+
const TAG_SOURCE = "\\[\\[[\\p{L} ,'-]{1,40}\\]\\]";
|
|
89
|
+
const anyDoubleBracket = /\[\[[^\]\n]*\]\]/gu;
|
|
90
|
+
/**
|
|
91
|
+
* The ElevenLabs v4 form: `[[x]]` becomes `[x]`; a malformed double-bracket tag is dropped.
|
|
92
|
+
*/
|
|
93
|
+
function toElevenLabsTags(text) {
|
|
94
|
+
const valid = new RegExp(`^${TAG_SOURCE}$`, "u");
|
|
95
|
+
return text.replace(anyDoubleBracket, (tag) => valid.test(tag) ? tag.slice(1, -1) : "");
|
|
96
|
+
}
|
|
97
|
+
const codeSpan = /```[\s\S]*?(?:```|$)|`[^`\n]+`/g;
|
|
98
|
+
/** The [start, end) ranges of fenced code blocks and inline code spans in `text`. */
|
|
99
|
+
function codeRanges(text) {
|
|
100
|
+
return [...text.matchAll(codeSpan)].map((m) => [m.index, m.index + m[0].length]);
|
|
101
|
+
}
|
|
102
|
+
function inCode(ranges, at) {
|
|
103
|
+
return ranges.some(([start, end]) => at >= start && at < end);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* A voice reply's text for reuse as plain text (history, search, recaps, read-aloud): every
|
|
107
|
+
* `[[...]]` removed, malformed ones too since Flash reads them aloud, but none inside code. Text
|
|
108
|
+
* without one comes back unchanged; a removed tag takes one adjacent space, nothing else is touched.
|
|
109
|
+
* Callers outside `/voice/speak` apply it to voice replies only. `inCodeToo`: code as well, for
|
|
110
|
+
* `/voice/speak`, where the fast model would read a tag aloud wherever it is.
|
|
111
|
+
* Not for offsets: `stripAudioTags`.
|
|
112
|
+
*/
|
|
113
|
+
function stripAudioTagsForText(text, { inCodeToo = false } = {}) {
|
|
114
|
+
if (!text.includes("[[")) return text;
|
|
115
|
+
const ranges = inCodeToo ? [] : codeRanges(text);
|
|
116
|
+
let out = "";
|
|
117
|
+
let pos = 0;
|
|
118
|
+
for (const match of text.matchAll(anyDoubleBracket)) {
|
|
119
|
+
if (inCode(ranges, match.index)) continue;
|
|
120
|
+
out += text.slice(pos, match.index);
|
|
121
|
+
let end = match.index + match[0].length;
|
|
122
|
+
const next = text[end];
|
|
123
|
+
if (next === " " && (out === "" || /\s$/.test(out))) end += 1;
|
|
124
|
+
else if ((next === void 0 || !/[\p{L}\p{N}]/u.test(next)) && out.endsWith(" ")) out = out.slice(0, -1);
|
|
125
|
+
pos = end;
|
|
126
|
+
}
|
|
127
|
+
return out + text.slice(pos);
|
|
128
|
+
}
|
|
129
|
+
const STT_LANGUAGE_SET = /* @__PURE__ */ new Set([
|
|
130
|
+
"multi",
|
|
131
|
+
"en",
|
|
132
|
+
"en-US",
|
|
133
|
+
"en-GB",
|
|
134
|
+
"de",
|
|
135
|
+
"de-CH",
|
|
136
|
+
"fr",
|
|
137
|
+
"es",
|
|
138
|
+
"it",
|
|
139
|
+
"nl",
|
|
140
|
+
"pt",
|
|
141
|
+
"pt-BR",
|
|
142
|
+
"pl",
|
|
143
|
+
"sv",
|
|
144
|
+
"da",
|
|
145
|
+
"no",
|
|
146
|
+
"fi",
|
|
147
|
+
"tr",
|
|
148
|
+
"ru",
|
|
149
|
+
"uk",
|
|
150
|
+
"ja",
|
|
151
|
+
"ko",
|
|
152
|
+
"hi"
|
|
153
|
+
]);
|
|
154
|
+
/** The requested language when it is on the list, else null (the caller falls back to the deployment default). */
|
|
155
|
+
function parseSttLanguage(value) {
|
|
156
|
+
return typeof value === "string" && STT_LANGUAGE_SET.has(value) ? value : null;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* The ElevenLabs `language_code` for a pinned STT language: the bare ISO 639-1 part,
|
|
160
|
+
* or null for `multi` / anything malformed.
|
|
161
|
+
*/
|
|
162
|
+
function ttsLanguageCode(value) {
|
|
163
|
+
const lang = parseSttLanguage(typeof value === "string" ? value.trim() : value);
|
|
164
|
+
if (!lang || lang === "multi") return null;
|
|
165
|
+
return lang.slice(0, 2);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* ElevenLabs refuses `language_code` on models that do not enforce a language; today
|
|
169
|
+
* that is everything except the v2.5 Flash and Turbo models and the v3 / v4 family
|
|
170
|
+
* (which Talk's text-to-dialogue stream uses).
|
|
171
|
+
*/
|
|
172
|
+
function modelAcceptsLanguageCode(modelId) {
|
|
173
|
+
return /^eleven_(flash|turbo)_v2_5$/.test(modelId) || /^eleven_v[34](_|$)/.test(modelId);
|
|
174
|
+
}
|
|
175
|
+
// src/shared/wire.ts
|
|
176
|
+
/** Where the relay routes live by default; a host may mount them elsewhere. */
|
|
177
|
+
const VOICE_RELAY_DEFAULT_PATHS = {
|
|
178
|
+
stt: "/voice/transcribe-stream",
|
|
179
|
+
tts: "/voice/speak-stream",
|
|
180
|
+
retranscribe: "/voice/retranscribe",
|
|
181
|
+
speak: "/voice/speak"
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* Query parameters the relay reads itself. Every other query parameter (or, for `speak`, every other
|
|
185
|
+
* body field) is the host's opaque scope (`sessionId`, `organizationId`, `spaceId`, …).
|
|
186
|
+
* `token` carries a bearer, since a browser cannot set headers on `new WebSocket()`.
|
|
187
|
+
*/
|
|
188
|
+
const RELAY_QUERY_PARAMS = [
|
|
189
|
+
"token",
|
|
190
|
+
"language",
|
|
191
|
+
"encoding",
|
|
192
|
+
"voiceId",
|
|
193
|
+
"languageCode"
|
|
194
|
+
];
|
|
195
|
+
/** Closed list: the value selects upstream URL params. Anything else means the container format. */
|
|
196
|
+
function parseSttEncoding(value) {
|
|
197
|
+
return value === "linear16" ? "linear16" : null;
|
|
198
|
+
}
|
|
199
|
+
/** The sample rate of `linear16` audio, on the STT socket and the re-transcription body alike. */
|
|
200
|
+
const LINEAR16_SAMPLE_RATE = 16e3;
|
|
201
|
+
/** Every client frame is a short JSON text frame of at most this many bytes; binary frames are ignored. */
|
|
202
|
+
const TTS_MAX_CLIENT_FRAME_BYTES = 65536;
|
|
203
|
+
/** The PCM the TTS stream answers with: s16le, mono, this rate. */
|
|
204
|
+
const TTS_STREAM_SAMPLE_RATE = 24e3;
|
|
205
|
+
/** The body's content type: raw `linear16` at 16 kHz. */
|
|
206
|
+
const RETRANSCRIBE_CONTENT_TYPE = "audio/l16";
|
|
207
|
+
/** 30 s of 16 kHz mono linear16: the client's utterance ring holds no more. Above it: 413. */
|
|
208
|
+
const RETRANSCRIBE_MAX_BYTES = 96e4;
|
|
209
|
+
// src/server/retranscribe.ts
|
|
210
|
+
/**
|
|
211
|
+
* Deepgram prerecorded transcription of one buffered utterance (16 kHz linear16), with language
|
|
212
|
+
* detection unless a language is pinned. Neither the audio nor the transcript is logged; a refused
|
|
213
|
+
* keyterm prompt (400) is retried once without keyterms.
|
|
214
|
+
*/
|
|
215
|
+
async function transcribe(rt, grant, apiKey, audio, pinned) {
|
|
216
|
+
const { deepgram, principalId } = grant;
|
|
217
|
+
const fixedLanguage = pinned && pinned !== "multi" ? pinned : null;
|
|
218
|
+
let keyterms = [];
|
|
219
|
+
try {
|
|
220
|
+
keyterms = await grant.keyterms?.(fixedLanguage) ?? [];
|
|
221
|
+
} catch {
|
|
222
|
+
rt.logger.warn("Speech keyterms could not be read; re-transcribing without them");
|
|
223
|
+
}
|
|
224
|
+
const urlWith = (terms) => buildDeepgramUrl(deepgram.host, "https", "/v1/listen", {
|
|
225
|
+
model: deepgram.model,
|
|
226
|
+
encoding: "linear16",
|
|
227
|
+
sample_rate: String(LINEAR16_SAMPLE_RATE),
|
|
228
|
+
smart_format: "true",
|
|
229
|
+
...fixedLanguage ? { language: fixedLanguage } : { detect_language: "true" },
|
|
230
|
+
...terms.length > 0 ? { keyterm: terms } : {}
|
|
231
|
+
}, { hostPattern: rt.deepgramHostPattern });
|
|
232
|
+
let response = await request(rt, principalId, apiKey, urlWith(keyterms), audio);
|
|
233
|
+
if (typeof response !== "string" && response.status === 400 && keyterms.length > 0) {
|
|
234
|
+
rt.logger.warn("Deepgram re-transcription refused the keyterms: status=400; retrying without them");
|
|
235
|
+
response = await request(rt, principalId, apiKey, urlWith([]), audio);
|
|
236
|
+
}
|
|
237
|
+
if (typeof response === "string") return response;
|
|
238
|
+
if (!response.ok) {
|
|
239
|
+
rt.logger.warn(`Deepgram re-transcription rejected: status=${response.status}`);
|
|
240
|
+
return "error";
|
|
241
|
+
}
|
|
242
|
+
return parse(rt, response, fixedLanguage);
|
|
243
|
+
}
|
|
244
|
+
async function request(rt, principalId, apiKey, url, audio) {
|
|
245
|
+
rt.audit({
|
|
246
|
+
event: "deepgram-retranscription-requested",
|
|
247
|
+
principalId,
|
|
248
|
+
host: url.host,
|
|
249
|
+
model: url.searchParams.get("model"),
|
|
250
|
+
bytes: audio.length,
|
|
251
|
+
mipOptOut: url.searchParams.get("mip_opt_out"),
|
|
252
|
+
keyterms: url.searchParams.getAll("keyterm").length
|
|
253
|
+
});
|
|
254
|
+
try {
|
|
255
|
+
return await rt.fetch()(url, {
|
|
256
|
+
method: "POST",
|
|
257
|
+
headers: {
|
|
258
|
+
Authorization: `Token ${apiKey}`,
|
|
259
|
+
"Content-Type": "application/octet-stream"
|
|
260
|
+
},
|
|
261
|
+
body: new Uint8Array(audio),
|
|
262
|
+
signal: AbortSignal.timeout(rt.limits.retranscribe.timeoutMs)
|
|
263
|
+
});
|
|
264
|
+
} catch (err) {
|
|
265
|
+
const timedOut = err?.name === "TimeoutError";
|
|
266
|
+
rt.logger.warn(`Deepgram re-transcription ${timedOut ? "timed out" : "failed before a response"}`);
|
|
267
|
+
return timedOut ? "timeout" : "error";
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
async function parse(rt, response, fixedLanguage) {
|
|
271
|
+
let payload;
|
|
272
|
+
try {
|
|
273
|
+
payload = await response.json();
|
|
274
|
+
} catch {
|
|
275
|
+
rt.logger.warn("Deepgram re-transcription answered with an unreadable body");
|
|
276
|
+
return "error";
|
|
277
|
+
}
|
|
278
|
+
const channel = payload.results?.channels?.[0];
|
|
279
|
+
const transcript = channel?.alternatives?.[0]?.transcript;
|
|
280
|
+
const detected = typeof channel?.detected_language === "string" ? channel.detected_language : null;
|
|
281
|
+
const confidence = channel?.language_confidence;
|
|
282
|
+
const wordsConfidence = channel?.alternatives?.[0]?.confidence;
|
|
283
|
+
return {
|
|
284
|
+
transcript: typeof transcript === "string" ? transcript : "",
|
|
285
|
+
confidence: typeof wordsConfidence === "number" && Number.isFinite(wordsConfidence) ? wordsConfidence : null,
|
|
286
|
+
language: fixedLanguage ?? detected,
|
|
287
|
+
languageConfidence: fixedLanguage === null && typeof confidence === "number" && Number.isFinite(confidence) ? confidence : null
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
// src/server/sockets.ts
|
|
291
|
+
const WS_CONNECTING = 0;
|
|
292
|
+
const WS_OPEN = 1;
|
|
293
|
+
/** A `ws` server-side socket (e.g. from `WebSocketServer.handleUpgrade`) as a `RelaySocket`. */
|
|
294
|
+
function wsRelaySocket(ws) {
|
|
295
|
+
ws.on("error", () => ws.terminate());
|
|
296
|
+
return {
|
|
297
|
+
send(data) {
|
|
298
|
+
if (ws.readyState === WS_OPEN) ws.send(data, { binary: typeof data !== "string" });
|
|
299
|
+
},
|
|
300
|
+
close: (code, reason) => ws.close(code, reason),
|
|
301
|
+
onMessage(listener) {
|
|
302
|
+
ws.on("message", (raw, isBinary) => {
|
|
303
|
+
if (isBinary) listener(typeof raw === "string" ? new TextEncoder().encode(raw) : raw);
|
|
304
|
+
else listener(typeof raw === "string" ? raw : new TextDecoder().decode(raw));
|
|
305
|
+
});
|
|
306
|
+
},
|
|
307
|
+
onClose: (listener) => void ws.on("close", () => listener())
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
/** A dialer on the `ws` client: pass `wsDialer(WebSocket)` with `import WebSocket from 'ws'`. */
|
|
311
|
+
function wsDialer(WebSocket) {
|
|
312
|
+
return (url, headers) => {
|
|
313
|
+
const ws = new WebSocket(url, { headers });
|
|
314
|
+
return {
|
|
315
|
+
get state() {
|
|
316
|
+
return ws.readyState === WS_CONNECTING ? "connecting" : ws.readyState === WS_OPEN ? "open" : "closed";
|
|
317
|
+
},
|
|
318
|
+
send: (data) => ws.send(data),
|
|
319
|
+
close: () => ws.close(),
|
|
320
|
+
on(handlers) {
|
|
321
|
+
ws.on("open", () => handlers.open());
|
|
322
|
+
ws.on("message", (raw) => handlers.message(typeof raw === "string" ? raw : new TextDecoder().decode(raw)));
|
|
323
|
+
ws.on("unexpected-response", (_request, res) => {
|
|
324
|
+
let guarded = false;
|
|
325
|
+
const guard = (onError) => {
|
|
326
|
+
if (!guarded) res.on("error", onError);
|
|
327
|
+
guarded = true;
|
|
328
|
+
};
|
|
329
|
+
handlers.rejected({
|
|
330
|
+
status: res.statusCode ?? 0,
|
|
331
|
+
statusText: res.statusMessage ?? "",
|
|
332
|
+
body: () => new Promise((resolve, reject) => {
|
|
333
|
+
let body = "";
|
|
334
|
+
guard(() => reject(/* @__PURE__ */ new Error("body read failed")));
|
|
335
|
+
res.on("data", (chunk) => {
|
|
336
|
+
body += new TextDecoder().decode(chunk);
|
|
337
|
+
});
|
|
338
|
+
res.on("end", () => resolve(body));
|
|
339
|
+
}),
|
|
340
|
+
discard() {
|
|
341
|
+
guard(() => void 0);
|
|
342
|
+
res.resume();
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
ws.on("error", (e) => handlers.error(e));
|
|
347
|
+
ws.on("close", (code, reason) => handlers.close(code, new TextDecoder().decode(reason)));
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* The default dialer: `ws`, an optional peer dependency, loaded when the relay is created so a
|
|
354
|
+
* missing install fails at startup rather than on the first stream.
|
|
355
|
+
*/
|
|
356
|
+
function defaultDialer() {
|
|
357
|
+
let mod;
|
|
358
|
+
try {
|
|
359
|
+
mod = createRequire(import.meta.url)("ws");
|
|
360
|
+
} catch {
|
|
361
|
+
throw new Error("@skaile/forge-voice/server: install 'ws', or pass `dial` to createVoiceRelay");
|
|
362
|
+
}
|
|
363
|
+
return wsDialer(mod.WebSocket ?? mod);
|
|
364
|
+
}
|
|
365
|
+
// src/server/speak.ts
|
|
366
|
+
/**
|
|
367
|
+
* Budget for one text-to-speech call, scaled with the text: the stream can run as long as the clip,
|
|
368
|
+
* so 20 s of headroom plus 20 ms per character (3 min 40 s at 10k characters). A total budget: it
|
|
369
|
+
* also bounds the error-body read and the audio relayed; expiry while the audio is relayed ends it early.
|
|
370
|
+
*/
|
|
371
|
+
function ttsTimeoutMs(textLength) {
|
|
372
|
+
return 2e4 + textLength * 20;
|
|
373
|
+
}
|
|
374
|
+
const TIMEOUT_MESSAGE = "ElevenLabs did not answer in time";
|
|
375
|
+
/** A transport failure answers fixed text: an invalid header value fails inside `fetch` with an error that quotes the key. */
|
|
376
|
+
const TRANSPORT_MESSAGE = "ElevenLabs could not be reached";
|
|
377
|
+
/** By name: the rejection is a DOMException, which is not an `Error` in every realm. */
|
|
378
|
+
function isTimeout(err) {
|
|
379
|
+
return err?.name === "TimeoutError";
|
|
380
|
+
}
|
|
381
|
+
function errorName(err) {
|
|
382
|
+
const name = err?.name;
|
|
383
|
+
return typeof name === "string" && /^[A-Za-z]{1,64}$/.test(name) ? name : "Error";
|
|
384
|
+
}
|
|
385
|
+
/** Plain ElevenLabs streaming text-to-speech; the answer streams while the rest is still being synthesised. */
|
|
386
|
+
async function speakPlain(rt, clip) {
|
|
387
|
+
const { apiKey, text, voiceId, model } = clip;
|
|
388
|
+
const url = buildElevenLabsUrl(`/v1/text-to-speech/${voiceId}/stream`, { output_format: "mp3_44100_128" });
|
|
389
|
+
rt.audit({
|
|
390
|
+
event: "elevenlabs-speech-requested",
|
|
391
|
+
principalId: clip.principalId,
|
|
392
|
+
voiceId,
|
|
393
|
+
model,
|
|
394
|
+
chars: text.length,
|
|
395
|
+
enableLogging: url.searchParams.get("enable_logging")
|
|
396
|
+
});
|
|
397
|
+
let response;
|
|
398
|
+
try {
|
|
399
|
+
response = await rt.fetch()(url, {
|
|
400
|
+
method: "POST",
|
|
401
|
+
headers: {
|
|
402
|
+
"xi-api-key": apiKey,
|
|
403
|
+
"Content-Type": "application/json"
|
|
404
|
+
},
|
|
405
|
+
body: JSON.stringify({
|
|
406
|
+
text,
|
|
407
|
+
model_id: model,
|
|
408
|
+
...clip.languageCode ? { language_code: clip.languageCode } : {}
|
|
409
|
+
}),
|
|
410
|
+
signal: AbortSignal.timeout(ttsTimeoutMs(text.length))
|
|
411
|
+
});
|
|
412
|
+
} catch (err) {
|
|
413
|
+
if (isTimeout(err)) return {
|
|
414
|
+
ok: false,
|
|
415
|
+
status: 504,
|
|
416
|
+
message: TIMEOUT_MESSAGE
|
|
417
|
+
};
|
|
418
|
+
rt.logger.warn(`ElevenLabs speech request failed: ${errorName(err)}`);
|
|
419
|
+
return {
|
|
420
|
+
ok: false,
|
|
421
|
+
status: 502,
|
|
422
|
+
message: TRANSPORT_MESSAGE
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
if (!response.ok) {
|
|
426
|
+
let errorText;
|
|
427
|
+
try {
|
|
428
|
+
errorText = await response.text();
|
|
429
|
+
} catch (err) {
|
|
430
|
+
if (isTimeout(err)) return {
|
|
431
|
+
ok: false,
|
|
432
|
+
status: 504,
|
|
433
|
+
message: TIMEOUT_MESSAGE
|
|
434
|
+
};
|
|
435
|
+
rt.logger.warn(`ElevenLabs error body read failed: ${errorName(err)}`);
|
|
436
|
+
return {
|
|
437
|
+
ok: false,
|
|
438
|
+
status: 502,
|
|
439
|
+
message: TRANSPORT_MESSAGE
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
return {
|
|
443
|
+
ok: false,
|
|
444
|
+
status: 502,
|
|
445
|
+
message: `ElevenLabs API error: ${withoutKey(errorText, apiKey)}`
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
if (!response.body) return {
|
|
449
|
+
ok: false,
|
|
450
|
+
status: 502,
|
|
451
|
+
message: TRANSPORT_MESSAGE
|
|
452
|
+
};
|
|
453
|
+
return {
|
|
454
|
+
ok: true,
|
|
455
|
+
value: {
|
|
456
|
+
contentType: "audio/mpeg",
|
|
457
|
+
body: response.body
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* The text with its audio tags (`[laughs]`) through text-to-dialogue on the stream model, so a clip
|
|
463
|
+
* sounds like the streamed replies. Null on any failure (the caller falls back to the plain model);
|
|
464
|
+
* only the status is logged, since an error body may echo the text.
|
|
465
|
+
*/
|
|
466
|
+
async function speakExpressive(rt, clip) {
|
|
467
|
+
const { apiKey, text, voiceId, model, languageCode } = clip;
|
|
468
|
+
const url = buildElevenLabsUrl("/v1/text-to-dialogue/stream", { output_format: "mp3_44100_128" });
|
|
469
|
+
rt.audit({
|
|
470
|
+
event: "elevenlabs-speech-requested",
|
|
471
|
+
principalId: clip.principalId,
|
|
472
|
+
voiceId,
|
|
473
|
+
model,
|
|
474
|
+
chars: text.length,
|
|
475
|
+
enableLogging: url.searchParams.get("enable_logging")
|
|
476
|
+
});
|
|
477
|
+
try {
|
|
478
|
+
const response = await rt.fetch()(url, {
|
|
479
|
+
method: "POST",
|
|
480
|
+
headers: {
|
|
481
|
+
"xi-api-key": apiKey,
|
|
482
|
+
"Content-Type": "application/json"
|
|
483
|
+
},
|
|
484
|
+
body: JSON.stringify({
|
|
485
|
+
inputs: [{
|
|
486
|
+
text,
|
|
487
|
+
voice_id: voiceId
|
|
488
|
+
}],
|
|
489
|
+
model_id: model,
|
|
490
|
+
...languageCode && modelAcceptsLanguageCode(model) ? { language_code: languageCode } : {}
|
|
491
|
+
}),
|
|
492
|
+
signal: AbortSignal.timeout(ttsTimeoutMs(text.length))
|
|
493
|
+
});
|
|
494
|
+
if (!response.ok || !response.body) {
|
|
495
|
+
rt.logger.warn(`ElevenLabs expressive speech failed: ${response.status}; falling back`);
|
|
496
|
+
await response.body?.cancel().catch(() => void 0);
|
|
497
|
+
return null;
|
|
498
|
+
}
|
|
499
|
+
return {
|
|
500
|
+
contentType: "audio/mpeg",
|
|
501
|
+
body: response.body
|
|
502
|
+
};
|
|
503
|
+
} catch (err) {
|
|
504
|
+
rt.logger.warn(`ElevenLabs expressive speech failed: ${isTimeout(err) ? "timeout" : "transport"}; falling back`);
|
|
505
|
+
return null;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
// src/server/runtime.ts
|
|
509
|
+
/** Dials a provider socket; a dial that throws (an invalid header value quotes the key) yields null, its error dropped. */
|
|
510
|
+
function openUpstream(rt, url, headers) {
|
|
511
|
+
try {
|
|
512
|
+
return rt.dial(url, headers);
|
|
513
|
+
} catch {
|
|
514
|
+
return null;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
/** Tells the browser the stream cannot start, with fixed text, and frees its slot once it closes. */
|
|
518
|
+
function refuseClient(client, release, error) {
|
|
519
|
+
client.onClose(release);
|
|
520
|
+
client.send(JSON.stringify({
|
|
521
|
+
type: "error",
|
|
522
|
+
error
|
|
523
|
+
}));
|
|
524
|
+
client.close(1011, "upstream-error");
|
|
525
|
+
}
|
|
526
|
+
// src/server/stt.ts
|
|
527
|
+
/**
|
|
528
|
+
* The stream's language: the requested one, else the default. nova-3 has no `detect_language` on a
|
|
529
|
+
* stream; `multi` is its multilingual mode, so a default of `auto` streams as `multi`.
|
|
530
|
+
*/
|
|
531
|
+
function sttStreamLanguage(language, defaultLanguage) {
|
|
532
|
+
return language ?? (defaultLanguage === "auto" ? "multi" : defaultLanguage);
|
|
533
|
+
}
|
|
534
|
+
/** Mean word confidence, or undefined when Deepgram sent no scored words. */
|
|
535
|
+
function meanWordConfidence(words) {
|
|
536
|
+
const scores = words.map((w) => w.confidence).filter((c) => typeof c === "number");
|
|
537
|
+
return scores.length > 0 ? scores.reduce((sum, c) => sum + c, 0) / scores.length : void 0;
|
|
538
|
+
}
|
|
539
|
+
const finiteOrUndefined = (value) => typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
540
|
+
/** Pipes an upgraded client socket to a Deepgram live stream until either side ends. */
|
|
541
|
+
function pipeStt(rt, client, stream) {
|
|
542
|
+
const { apiKey, deepgram, principalId, release } = stream;
|
|
543
|
+
const limits = rt.limits.stt;
|
|
544
|
+
const lang = sttStreamLanguage(stream.language, deepgram.defaultLanguage);
|
|
545
|
+
const url = buildDeepgramUrl(deepgram.host, "wss", "/v1/listen", {
|
|
546
|
+
model: deepgram.model,
|
|
547
|
+
language: lang,
|
|
548
|
+
smart_format: "true",
|
|
549
|
+
interim_results: "true",
|
|
550
|
+
endpointing: "300",
|
|
551
|
+
vad_events: "true",
|
|
552
|
+
...stream.encoding === "linear16" ? {
|
|
553
|
+
encoding: "linear16",
|
|
554
|
+
sample_rate: String(LINEAR16_SAMPLE_RATE)
|
|
555
|
+
} : {},
|
|
556
|
+
...stream.keyterms.length > 0 ? { keyterm: stream.keyterms } : {}
|
|
557
|
+
}, { hostPattern: rt.deepgramHostPattern });
|
|
558
|
+
rt.audit({
|
|
559
|
+
event: "deepgram-stream-opened",
|
|
560
|
+
principalId,
|
|
561
|
+
host: url.host,
|
|
562
|
+
model: url.searchParams.get("model"),
|
|
563
|
+
language: lang,
|
|
564
|
+
mipOptOut: url.searchParams.get("mip_opt_out"),
|
|
565
|
+
keyterms: url.searchParams.getAll("keyterm").length
|
|
566
|
+
});
|
|
567
|
+
const upstream = openUpstream(rt, url, { Authorization: `Token ${apiKey}` });
|
|
568
|
+
if (!upstream) {
|
|
569
|
+
rt.logger.error("Deepgram stream could not be opened");
|
|
570
|
+
refuseClient(client, release, "Deepgram could not be reached");
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
let clientGone = false;
|
|
574
|
+
const send = (frame) => {
|
|
575
|
+
if (!clientGone) client.send(JSON.stringify(frame));
|
|
576
|
+
};
|
|
577
|
+
const maxTimer = setTimeout(() => client.close(1e3, "max-duration"), limits.maxDurationMs);
|
|
578
|
+
const expiryTimer = setTimeout(() => send({
|
|
579
|
+
type: "expiring",
|
|
580
|
+
inMs: limits.expiryWarningMs
|
|
581
|
+
}), limits.maxDurationMs - limits.expiryWarningMs);
|
|
582
|
+
let idleTimer;
|
|
583
|
+
const resetIdle = () => {
|
|
584
|
+
clearTimeout(idleTimer);
|
|
585
|
+
idleTimer = setTimeout(() => client.close(1e3, "idle-timeout"), limits.idleMs);
|
|
586
|
+
};
|
|
587
|
+
resetIdle();
|
|
588
|
+
upstream.on({
|
|
589
|
+
open: () => send({ type: "ready" }),
|
|
590
|
+
message(raw) {
|
|
591
|
+
try {
|
|
592
|
+
const data = JSON.parse(raw);
|
|
593
|
+
if (data.type === "SpeechStarted") send({
|
|
594
|
+
type: "speech_started",
|
|
595
|
+
timestamp: finiteOrUndefined(data.timestamp)
|
|
596
|
+
});
|
|
597
|
+
else if (data.type === "Results") {
|
|
598
|
+
const alt = data.channel?.alternatives?.[0];
|
|
599
|
+
if (alt) {
|
|
600
|
+
const words = Array.isArray(alt.words) ? alt.words : [];
|
|
601
|
+
const detected = Array.isArray(alt.languages) && typeof alt.languages[0] === "string" ? alt.languages[0] : null;
|
|
602
|
+
send({
|
|
603
|
+
type: "transcript",
|
|
604
|
+
transcript: alt.transcript || "",
|
|
605
|
+
is_final: data.is_final ?? false,
|
|
606
|
+
speech_final: data.speech_final ?? false,
|
|
607
|
+
start: finiteOrUndefined(data.start),
|
|
608
|
+
duration: finiteOrUndefined(data.duration),
|
|
609
|
+
wordsStart: finiteOrUndefined(words[0]?.start),
|
|
610
|
+
wordsEnd: finiteOrUndefined(words.at(-1)?.end),
|
|
611
|
+
confidence: meanWordConfidence(words),
|
|
612
|
+
language: detected ?? (lang === "multi" ? void 0 : lang)
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
} catch {
|
|
617
|
+
rt.logger.error("Deepgram message parse error");
|
|
618
|
+
}
|
|
619
|
+
},
|
|
620
|
+
rejected(res) {
|
|
621
|
+
res.body().then((body) => {
|
|
622
|
+
rt.logger.error(`Deepgram rejected connection: ${res.status} ${res.statusText} - ${withoutKey(body, apiKey)}`);
|
|
623
|
+
send({
|
|
624
|
+
type: "error",
|
|
625
|
+
error: `Deepgram rejected: ${res.status} ${res.statusText} ${withoutKey(body, apiKey)}`
|
|
626
|
+
});
|
|
627
|
+
client.close(1011, "upstream-error");
|
|
628
|
+
}, () => rt.logger.error(`Deepgram rejected connection: ${res.status} ${res.statusText} - <body read failed>`));
|
|
629
|
+
},
|
|
630
|
+
error(e) {
|
|
631
|
+
const message = withoutKey(e.message, apiKey);
|
|
632
|
+
rt.logger.error(`Deepgram WS error: ${message}`);
|
|
633
|
+
send({
|
|
634
|
+
type: "error",
|
|
635
|
+
error: `Deepgram error: ${message}`
|
|
636
|
+
});
|
|
637
|
+
client.close(1011, "upstream-error");
|
|
638
|
+
},
|
|
639
|
+
close(code, reason) {
|
|
640
|
+
rt.logger.log?.(`Deepgram WS closed: ${code} ${reason}`);
|
|
641
|
+
send({ type: "closed" });
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
client.onMessage((data) => {
|
|
645
|
+
resetIdle();
|
|
646
|
+
if (upstream.state !== "open") return;
|
|
647
|
+
if (typeof data !== "string") {
|
|
648
|
+
upstream.send(data);
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
try {
|
|
652
|
+
if (JSON.parse(data)?.type === "stop") upstream.send(JSON.stringify({ type: "CloseStream" }));
|
|
653
|
+
} catch {}
|
|
654
|
+
});
|
|
655
|
+
client.onClose(() => {
|
|
656
|
+
clientGone = true;
|
|
657
|
+
clearTimeout(maxTimer);
|
|
658
|
+
clearTimeout(expiryTimer);
|
|
659
|
+
clearTimeout(idleTimer);
|
|
660
|
+
release();
|
|
661
|
+
try {
|
|
662
|
+
upstream.close();
|
|
663
|
+
} catch {}
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
// src/server/tts.ts
|
|
667
|
+
function base64ToBytes(base64) {
|
|
668
|
+
const binary = atob(base64);
|
|
669
|
+
const bytes = new Uint8Array(binary.length);
|
|
670
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
671
|
+
return bytes;
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Pipes an upgraded client socket to ElevenLabs' text-to-dialogue stream: the reply text streams in
|
|
675
|
+
* as the agent writes it, raw PCM (s16le, mono, 24 kHz) streams back, and the key never reaches the browser.
|
|
676
|
+
*/
|
|
677
|
+
function pipeTts(rt, client, stream) {
|
|
678
|
+
const { apiKey, model, voiceId, principalId, release } = stream;
|
|
679
|
+
const limits = rt.limits.tts;
|
|
680
|
+
const url = buildElevenLabsUrl("/v1/text-to-dialogue/stream-input", {
|
|
681
|
+
model_id: model,
|
|
682
|
+
output_format: `pcm_${TTS_STREAM_SAMPLE_RATE}`,
|
|
683
|
+
sync_alignment: "true",
|
|
684
|
+
...stream.languageCode ? { language_code: stream.languageCode } : {}
|
|
685
|
+
}, { protocol: "wss" });
|
|
686
|
+
rt.audit({
|
|
687
|
+
event: "elevenlabs-stream-opened",
|
|
688
|
+
principalId,
|
|
689
|
+
voiceId,
|
|
690
|
+
model: url.searchParams.get("model_id"),
|
|
691
|
+
enableLogging: url.searchParams.get("enable_logging")
|
|
692
|
+
});
|
|
693
|
+
const upstream = openUpstream(rt, url, { "xi-api-key": apiKey });
|
|
694
|
+
if (!upstream) {
|
|
695
|
+
rt.logger.error("ElevenLabs stream could not be opened");
|
|
696
|
+
refuseClient(client, release, "ElevenLabs could not be reached");
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
const pending = [];
|
|
700
|
+
let chars = 0;
|
|
701
|
+
let done = false;
|
|
702
|
+
let ended = false;
|
|
703
|
+
let clientGone = false;
|
|
704
|
+
const send = (frame) => {
|
|
705
|
+
if (!clientGone) client.send(JSON.stringify(frame));
|
|
706
|
+
};
|
|
707
|
+
const toUpstream = (payload) => {
|
|
708
|
+
const raw = JSON.stringify(payload);
|
|
709
|
+
if (upstream.state === "open") upstream.send(raw);
|
|
710
|
+
else if (upstream.state === "connecting") pending.push(raw);
|
|
711
|
+
};
|
|
712
|
+
const fail = (error) => {
|
|
713
|
+
if (done) return;
|
|
714
|
+
done = true;
|
|
715
|
+
send({
|
|
716
|
+
type: "error",
|
|
717
|
+
error
|
|
718
|
+
});
|
|
719
|
+
client.close(1011, "upstream-error");
|
|
720
|
+
};
|
|
721
|
+
const maxTimer = setTimeout(() => client.close(1e3, "max-duration"), limits.maxDurationMs);
|
|
722
|
+
let idleTimer;
|
|
723
|
+
const resetIdle = () => {
|
|
724
|
+
clearTimeout(idleTimer);
|
|
725
|
+
idleTimer = setTimeout(() => client.close(1e3, "idle-timeout"), limits.idleMs);
|
|
726
|
+
};
|
|
727
|
+
resetIdle();
|
|
728
|
+
const keepAlive = setInterval(() => {
|
|
729
|
+
if (!done && upstream.state === "open") upstream.send(JSON.stringify({ keep_alive: true }));
|
|
730
|
+
}, limits.keepAliveMs);
|
|
731
|
+
upstream.on({
|
|
732
|
+
open() {
|
|
733
|
+
upstream.send(JSON.stringify({ voices: [voiceId] }));
|
|
734
|
+
for (const raw of pending.splice(0)) upstream.send(raw);
|
|
735
|
+
send({
|
|
736
|
+
type: "ready",
|
|
737
|
+
sampleRate: TTS_STREAM_SAMPLE_RATE
|
|
738
|
+
});
|
|
739
|
+
},
|
|
740
|
+
message(raw) {
|
|
741
|
+
let data;
|
|
742
|
+
try {
|
|
743
|
+
data = JSON.parse(raw);
|
|
744
|
+
} catch {
|
|
745
|
+
rt.logger.error("ElevenLabs stream message parse error");
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
if (!data || typeof data !== "object") return;
|
|
749
|
+
if (typeof data.error === "string" || typeof data.code === "number" && typeof data.message === "string") {
|
|
750
|
+
const code = typeof data.error === "string" ? data.error : String(data.code);
|
|
751
|
+
rt.logger.error(`ElevenLabs stream error: ${code}`);
|
|
752
|
+
fail(code);
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
if (typeof data.audio === "string" && data.audio.length > 0 && !clientGone) {
|
|
756
|
+
const alignment = data.alignment;
|
|
757
|
+
if (alignment && Array.isArray(alignment.chars)) send({
|
|
758
|
+
type: "alignment",
|
|
759
|
+
text: alignment.chars.map(String).join("")
|
|
760
|
+
});
|
|
761
|
+
client.send(base64ToBytes(data.audio));
|
|
762
|
+
}
|
|
763
|
+
if (data.is_final === true && !done && !ended) {
|
|
764
|
+
fail("early-final");
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
if (data.is_final === true && !done) {
|
|
768
|
+
done = true;
|
|
769
|
+
send({ type: "done" });
|
|
770
|
+
client.close(1e3, "done");
|
|
771
|
+
}
|
|
772
|
+
},
|
|
773
|
+
rejected(res) {
|
|
774
|
+
res.discard();
|
|
775
|
+
rt.logger.error(`ElevenLabs rejected stream: ${res.status} ${res.statusText}`);
|
|
776
|
+
fail(String(res.status));
|
|
777
|
+
},
|
|
778
|
+
error(e) {
|
|
779
|
+
if (clientGone) return;
|
|
780
|
+
rt.logger.error(`ElevenLabs stream WS error: ${e.name}`);
|
|
781
|
+
fail(typeof e.code === "string" ? e.code : "upstream-error");
|
|
782
|
+
},
|
|
783
|
+
close(code) {
|
|
784
|
+
if (!clientGone) fail(`upstream-closed-${code}`);
|
|
785
|
+
}
|
|
786
|
+
});
|
|
787
|
+
client.onMessage((data) => {
|
|
788
|
+
resetIdle();
|
|
789
|
+
if (typeof data !== "string" || done) return;
|
|
790
|
+
let frame;
|
|
791
|
+
try {
|
|
792
|
+
frame = JSON.parse(data);
|
|
793
|
+
} catch {
|
|
794
|
+
return;
|
|
795
|
+
}
|
|
796
|
+
if (frame?.type === "text" && typeof frame.text === "string") {
|
|
797
|
+
chars += frame.text.length;
|
|
798
|
+
if (chars > limits.maxTextChars) {
|
|
799
|
+
done = true;
|
|
800
|
+
send({
|
|
801
|
+
type: "error",
|
|
802
|
+
error: "text limit"
|
|
803
|
+
});
|
|
804
|
+
client.close(1008, "text-limit");
|
|
805
|
+
return;
|
|
806
|
+
}
|
|
807
|
+
if (frame.text.length > 0) {
|
|
808
|
+
toUpstream({ inputs: [{
|
|
809
|
+
text: frame.text,
|
|
810
|
+
voice_id: voiceId
|
|
811
|
+
}] });
|
|
812
|
+
if (frame.flush === true) toUpstream({ flush: true });
|
|
813
|
+
}
|
|
814
|
+
} else if (frame?.type === "end" && !ended) {
|
|
815
|
+
ended = true;
|
|
816
|
+
toUpstream({ close_socket: true });
|
|
817
|
+
}
|
|
818
|
+
});
|
|
819
|
+
client.onClose(() => {
|
|
820
|
+
clientGone = true;
|
|
821
|
+
clearTimeout(maxTimer);
|
|
822
|
+
clearTimeout(idleTimer);
|
|
823
|
+
clearInterval(keepAlive);
|
|
824
|
+
release();
|
|
825
|
+
try {
|
|
826
|
+
upstream.close();
|
|
827
|
+
} catch {}
|
|
828
|
+
rt.audit({
|
|
829
|
+
event: "elevenlabs-stream-closed",
|
|
830
|
+
principalId,
|
|
831
|
+
chars
|
|
832
|
+
});
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
// src/server/relay.ts
|
|
836
|
+
const DEFAULT_RELAY_LIMITS = {
|
|
837
|
+
stt: {
|
|
838
|
+
maxConcurrent: 5,
|
|
839
|
+
maxDurationMs: 6e5,
|
|
840
|
+
idleMs: 9e4,
|
|
841
|
+
expiryWarningMs: 3e4
|
|
842
|
+
},
|
|
843
|
+
tts: {
|
|
844
|
+
maxConcurrent: 3,
|
|
845
|
+
maxDurationMs: 3e5,
|
|
846
|
+
idleMs: 12e4,
|
|
847
|
+
keepAliveMs: 1e4,
|
|
848
|
+
maxTextChars: 1e4
|
|
849
|
+
},
|
|
850
|
+
retranscribe: {
|
|
851
|
+
maxBytes: RETRANSCRIBE_MAX_BYTES,
|
|
852
|
+
timeoutMs: 15e3
|
|
853
|
+
},
|
|
854
|
+
speak: {
|
|
855
|
+
maxTextChars: 1e4,
|
|
856
|
+
maxExpressiveChars: 200
|
|
857
|
+
}
|
|
858
|
+
};
|
|
859
|
+
/** An admission must reach `relay.stt` / `relay.tts` within this, or its slot is freed. */
|
|
860
|
+
const ADMISSION_TTL_MS = 1e4;
|
|
861
|
+
/** ElevenLabs voice ids are opaque alphanumeric strings; anything else could reshape the request path. */
|
|
862
|
+
const VOICE_ID = /^[A-Za-z0-9]+$/;
|
|
863
|
+
const STATUS_TEXT = {
|
|
864
|
+
400: "Bad Request",
|
|
865
|
+
401: "Unauthorized",
|
|
866
|
+
403: "Forbidden",
|
|
867
|
+
404: "Not Found",
|
|
868
|
+
409: "Conflict",
|
|
869
|
+
413: "Payload Too Large",
|
|
870
|
+
429: "Too Many Requests",
|
|
871
|
+
501: "Not Implemented"
|
|
872
|
+
};
|
|
873
|
+
function isRelayRefusal(value) {
|
|
874
|
+
return typeof value === "object" && value !== null && "refuse" in value;
|
|
875
|
+
}
|
|
876
|
+
/** The text an HTTP status line carries for a refusal. */
|
|
877
|
+
function refusalStatusText(refusal) {
|
|
878
|
+
return STATUS_TEXT[refusal.refuse] ?? "Error";
|
|
879
|
+
}
|
|
880
|
+
function refused(refusal) {
|
|
881
|
+
return {
|
|
882
|
+
ok: false,
|
|
883
|
+
status: refusal.refuse,
|
|
884
|
+
message: refusal.message ?? refusalStatusText(refusal),
|
|
885
|
+
...refusal.code ? { code: refusal.code } : {},
|
|
886
|
+
...refusal.retryAfterS !== void 0 ? { retryAfterS: refusal.retryAfterS } : {}
|
|
887
|
+
};
|
|
888
|
+
}
|
|
889
|
+
const fail = (status, message) => ({
|
|
890
|
+
ok: false,
|
|
891
|
+
status,
|
|
892
|
+
message
|
|
893
|
+
});
|
|
894
|
+
/** Per-principal stream counters. Per process: with several replicas the cap is per instance. */
|
|
895
|
+
var Slots = class {
|
|
896
|
+
active = /* @__PURE__ */ new Map();
|
|
897
|
+
count(principalId) {
|
|
898
|
+
return this.active.get(principalId) ?? 0;
|
|
899
|
+
}
|
|
900
|
+
take(principalId) {
|
|
901
|
+
this.active.set(principalId, this.count(principalId) + 1);
|
|
902
|
+
let released = false;
|
|
903
|
+
return () => {
|
|
904
|
+
if (released) return;
|
|
905
|
+
released = true;
|
|
906
|
+
const next = this.count(principalId) - 1;
|
|
907
|
+
if (next <= 0) this.active.delete(principalId);
|
|
908
|
+
else this.active.set(principalId, next);
|
|
909
|
+
};
|
|
910
|
+
}
|
|
911
|
+
};
|
|
912
|
+
function mergeLimits(input) {
|
|
913
|
+
return {
|
|
914
|
+
stt: {
|
|
915
|
+
...DEFAULT_RELAY_LIMITS.stt,
|
|
916
|
+
...input?.stt
|
|
917
|
+
},
|
|
918
|
+
tts: {
|
|
919
|
+
...DEFAULT_RELAY_LIMITS.tts,
|
|
920
|
+
...input?.tts
|
|
921
|
+
},
|
|
922
|
+
retranscribe: {
|
|
923
|
+
...DEFAULT_RELAY_LIMITS.retranscribe,
|
|
924
|
+
...input?.retranscribe
|
|
925
|
+
},
|
|
926
|
+
speak: {
|
|
927
|
+
...DEFAULT_RELAY_LIMITS.speak,
|
|
928
|
+
...input?.speak
|
|
929
|
+
}
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
/**
|
|
933
|
+
* The voice relay: speech-to-text and text-to-speech streams, re-transcription and spoken clips,
|
|
934
|
+
* between a browser and Deepgram / ElevenLabs, holding the provider keys server-side. Framework-free;
|
|
935
|
+
* a host adapter (the h3 entry, a NestJS gateway) reads requests and sockets and calls it.
|
|
936
|
+
*
|
|
937
|
+
* Privacy, enforced here and not configurable: every Deepgram URL carries `mip_opt_out=true` and every
|
|
938
|
+
* ElevenLabs URL `enable_logging=false`, set last; a key is redacted from every relayed frame and log
|
|
939
|
+
* line; no audio, transcript or text is logged; the WebSocket Origin check fails closed. Who may use
|
|
940
|
+
* voice is the host's: `authorize` runs before any provider contact, and gets nothing from the relay.
|
|
941
|
+
*/
|
|
942
|
+
function createVoiceRelay(config, host) {
|
|
943
|
+
const logger = host.logger ?? console;
|
|
944
|
+
const rt = {
|
|
945
|
+
dial: config.dial ?? defaultDialer(),
|
|
946
|
+
fetch: () => config.fetch ?? globalThis.fetch,
|
|
947
|
+
logger,
|
|
948
|
+
audit: host.audit ? (line) => host.audit(line) : (line) => logger.warn(formatAuditLine(line)),
|
|
949
|
+
limits: mergeLimits(host.limits),
|
|
950
|
+
deepgramHostPattern: config.allowedHosts?.deepgram ?? DEEPGRAM_HOST_PATTERN
|
|
951
|
+
};
|
|
952
|
+
const slots = {
|
|
953
|
+
stt: new Slots(),
|
|
954
|
+
tts: new Slots()
|
|
955
|
+
};
|
|
956
|
+
const pending = /* @__PURE__ */ new WeakMap();
|
|
957
|
+
const allowed = config.allowedOrigins;
|
|
958
|
+
const originAllowed = (origin) => {
|
|
959
|
+
if (!origin) return false;
|
|
960
|
+
return typeof allowed === "function" ? allowed(origin) : allowed.includes(origin);
|
|
961
|
+
};
|
|
962
|
+
const deepgramHostAllowed = (deepgram) => {
|
|
963
|
+
const ok = isAllowedDeepgramHost(deepgram.host, rt.deepgramHostPattern);
|
|
964
|
+
if (!ok) logger.error(`Deepgram host not allowed: ${deepgram.host}`);
|
|
965
|
+
return ok;
|
|
966
|
+
};
|
|
967
|
+
const readKey = async (key) => typeof key === "string" ? key : key();
|
|
968
|
+
/** Names and terms to listen for; a failed read streams without them rather than not at all. */
|
|
969
|
+
const readKeyterms = async (grant, language) => {
|
|
970
|
+
try {
|
|
971
|
+
return await grant.keyterms?.(language) ?? [];
|
|
972
|
+
} catch {
|
|
973
|
+
logger.warn("Speech keyterms could not be read; streaming without them");
|
|
974
|
+
return [];
|
|
975
|
+
}
|
|
976
|
+
};
|
|
977
|
+
/** The cap, then the key, then (the key read may have awaited) the cap again, then the slot. */
|
|
978
|
+
const reserve = async (kind, principalId, key) => {
|
|
979
|
+
const max = rt.limits[kind].maxConcurrent;
|
|
980
|
+
if (slots[kind].count(principalId) >= max) return { refuse: 429 };
|
|
981
|
+
const apiKey = await readKey(key);
|
|
982
|
+
if (!apiKey) return { refuse: 501 };
|
|
983
|
+
if (slots[kind].count(principalId) >= max) return { refuse: 429 };
|
|
984
|
+
return {
|
|
985
|
+
apiKey,
|
|
986
|
+
release: slots[kind].take(principalId)
|
|
987
|
+
};
|
|
988
|
+
};
|
|
989
|
+
const admission = (entry, principalId) => {
|
|
990
|
+
const handle = {
|
|
991
|
+
kind: entry.kind,
|
|
992
|
+
principalId,
|
|
993
|
+
release: () => {
|
|
994
|
+
clearTimeout(unclaimed);
|
|
995
|
+
if (pending.delete(handle)) entry.stream.release();
|
|
996
|
+
}
|
|
997
|
+
};
|
|
998
|
+
const unclaimed = setTimeout(handle.release, ADMISSION_TTL_MS);
|
|
999
|
+
unclaimed.unref?.();
|
|
1000
|
+
pending.set(handle, {
|
|
1001
|
+
...entry,
|
|
1002
|
+
unclaimed
|
|
1003
|
+
});
|
|
1004
|
+
return handle;
|
|
1005
|
+
};
|
|
1006
|
+
const claim = (handle, kind) => {
|
|
1007
|
+
const entry = pending.get(handle);
|
|
1008
|
+
if (!entry || entry.kind !== kind) throw new TypeError(`Not an open ${kind} admission of this relay`);
|
|
1009
|
+
clearTimeout(entry.unclaimed);
|
|
1010
|
+
pending.delete(handle);
|
|
1011
|
+
return entry;
|
|
1012
|
+
};
|
|
1013
|
+
/** A stream that fails to start (a throwing audit hook or send) still frees its slot. */
|
|
1014
|
+
const piped = (stream, pipe) => {
|
|
1015
|
+
try {
|
|
1016
|
+
pipe();
|
|
1017
|
+
} catch (err) {
|
|
1018
|
+
stream.release();
|
|
1019
|
+
throw err;
|
|
1020
|
+
}
|
|
1021
|
+
};
|
|
1022
|
+
return {
|
|
1023
|
+
async admit(request) {
|
|
1024
|
+
if (request.kind !== "stt" && request.kind !== "tts") throw new TypeError(`admit() is for the WebSocket routes, not ${request.kind}`);
|
|
1025
|
+
if (!originAllowed(request.origin)) return { refuse: 403 };
|
|
1026
|
+
const grant = await host.authorize(request);
|
|
1027
|
+
if (isRelayRefusal(grant)) return grant;
|
|
1028
|
+
if (request.kind === "stt") {
|
|
1029
|
+
const deepgram = grant.deepgram;
|
|
1030
|
+
if (!deepgram || !deepgramHostAllowed(deepgram)) return { refuse: 501 };
|
|
1031
|
+
const language = parseSttLanguage(request.language);
|
|
1032
|
+
const keyterms = await readKeyterms(grant, sttStreamLanguage(language, deepgram.defaultLanguage));
|
|
1033
|
+
const slot = await reserve("stt", grant.principalId, deepgram.apiKey);
|
|
1034
|
+
if (isRelayRefusal(slot)) return slot;
|
|
1035
|
+
const stream = {
|
|
1036
|
+
principalId: grant.principalId,
|
|
1037
|
+
apiKey: slot.apiKey,
|
|
1038
|
+
deepgram,
|
|
1039
|
+
language,
|
|
1040
|
+
encoding: parseSttEncoding(request.params.encoding),
|
|
1041
|
+
keyterms,
|
|
1042
|
+
release: slot.release
|
|
1043
|
+
};
|
|
1044
|
+
return admission({
|
|
1045
|
+
kind: "stt",
|
|
1046
|
+
stream
|
|
1047
|
+
}, grant.principalId);
|
|
1048
|
+
}
|
|
1049
|
+
const elevenlabs = grant.elevenlabs;
|
|
1050
|
+
if (!elevenlabs) return { refuse: 501 };
|
|
1051
|
+
const slot = await reserve("tts", grant.principalId, elevenlabs.apiKey);
|
|
1052
|
+
if (isRelayRefusal(slot)) return slot;
|
|
1053
|
+
const requested = request.params.voiceId?.trim();
|
|
1054
|
+
const model = elevenlabs.models.stream;
|
|
1055
|
+
const stream = {
|
|
1056
|
+
principalId: grant.principalId,
|
|
1057
|
+
apiKey: slot.apiKey,
|
|
1058
|
+
model,
|
|
1059
|
+
voiceId: requested && VOICE_ID.test(requested) ? requested : elevenlabs.voiceId,
|
|
1060
|
+
languageCode: modelAcceptsLanguageCode(model) ? ttsLanguageCode(request.params.languageCode) : null,
|
|
1061
|
+
release: slot.release
|
|
1062
|
+
};
|
|
1063
|
+
return admission({
|
|
1064
|
+
kind: "tts",
|
|
1065
|
+
stream
|
|
1066
|
+
}, grant.principalId);
|
|
1067
|
+
},
|
|
1068
|
+
stt(client, handle) {
|
|
1069
|
+
const entry = claim(handle, "stt");
|
|
1070
|
+
if (entry.kind === "stt") piped(entry.stream, () => pipeStt(rt, client, entry.stream));
|
|
1071
|
+
},
|
|
1072
|
+
tts(client, handle) {
|
|
1073
|
+
const entry = claim(handle, "tts");
|
|
1074
|
+
if (entry.kind === "tts") piped(entry.stream, () => pipeTts(rt, client, entry.stream));
|
|
1075
|
+
},
|
|
1076
|
+
async retranscribe(request, pcm) {
|
|
1077
|
+
const limits = rt.limits.retranscribe;
|
|
1078
|
+
if (pcm.length === 0) return fail(400, "audio/l16 body is required");
|
|
1079
|
+
if (pcm.length > limits.maxBytes) return fail(413, `audio exceeds ${limits.maxBytes} bytes`);
|
|
1080
|
+
const grant = await host.authorize(request);
|
|
1081
|
+
if (isRelayRefusal(grant)) return refused(grant);
|
|
1082
|
+
const raw = request.language?.trim() || null;
|
|
1083
|
+
const language = parseSttLanguage(raw);
|
|
1084
|
+
if (raw !== null && language === null) return fail(400, "Unsupported language");
|
|
1085
|
+
const deepgram = grant.deepgram;
|
|
1086
|
+
if (!deepgram || !deepgramHostAllowed(deepgram)) return fail(501, "STT not configured");
|
|
1087
|
+
const apiKey = await readKey(deepgram.apiKey);
|
|
1088
|
+
if (!apiKey) return fail(501, "STT not configured");
|
|
1089
|
+
const result = await transcribe(rt, {
|
|
1090
|
+
...grant,
|
|
1091
|
+
deepgram
|
|
1092
|
+
}, apiKey, pcm, language);
|
|
1093
|
+
if (result === "timeout") return fail(504, "Deepgram did not answer in time");
|
|
1094
|
+
if (result === "error") return fail(502, "Deepgram transcription failed");
|
|
1095
|
+
return {
|
|
1096
|
+
ok: true,
|
|
1097
|
+
value: result
|
|
1098
|
+
};
|
|
1099
|
+
},
|
|
1100
|
+
async speak(request, body) {
|
|
1101
|
+
const limits = rt.limits.speak;
|
|
1102
|
+
const text = typeof body.text === "string" ? stripAudioTagsForText(body.text, { inCodeToo: true }) : "";
|
|
1103
|
+
if (!text.trim()) return fail(400, "text is required");
|
|
1104
|
+
const tagged = body.expressive === true ? toElevenLabsTags(body.text) : null;
|
|
1105
|
+
const expressiveText = tagged !== null && tagged.length <= limits.maxExpressiveChars ? tagged : null;
|
|
1106
|
+
if (text.length > limits.maxTextChars) return fail(413, `text exceeds ${limits.maxTextChars} characters`);
|
|
1107
|
+
const grant = await host.authorize(request);
|
|
1108
|
+
if (isRelayRefusal(grant)) return refused(grant);
|
|
1109
|
+
const elevenlabs = grant.elevenlabs;
|
|
1110
|
+
if (!elevenlabs) return fail(501, "TTS not configured");
|
|
1111
|
+
const apiKey = await readKey(elevenlabs.apiKey);
|
|
1112
|
+
if (!apiKey) return fail(501, "TTS not configured");
|
|
1113
|
+
const requested = typeof body.voiceId === "string" ? body.voiceId.trim() : "";
|
|
1114
|
+
const voiceId = requested && VOICE_ID.test(requested) ? requested : elevenlabs.voiceId;
|
|
1115
|
+
if (expressiveText !== null) {
|
|
1116
|
+
const clip = await speakExpressive(rt, {
|
|
1117
|
+
principalId: grant.principalId,
|
|
1118
|
+
apiKey,
|
|
1119
|
+
text: expressiveText,
|
|
1120
|
+
voiceId,
|
|
1121
|
+
model: elevenlabs.models.stream,
|
|
1122
|
+
languageCode: ttsLanguageCode(body.languageCode)
|
|
1123
|
+
});
|
|
1124
|
+
if (clip) return {
|
|
1125
|
+
ok: true,
|
|
1126
|
+
value: clip
|
|
1127
|
+
};
|
|
1128
|
+
}
|
|
1129
|
+
const model = body.lowLatency === true ? elevenlabs.models.clipFast : elevenlabs.models.clip;
|
|
1130
|
+
return speakPlain(rt, {
|
|
1131
|
+
principalId: grant.principalId,
|
|
1132
|
+
apiKey,
|
|
1133
|
+
text,
|
|
1134
|
+
voiceId,
|
|
1135
|
+
model,
|
|
1136
|
+
languageCode: modelAcceptsLanguageCode(model) ? ttsLanguageCode(body.languageCode) : null
|
|
1137
|
+
});
|
|
1138
|
+
},
|
|
1139
|
+
activeStreams: (kind, principalId) => slots[kind].count(principalId),
|
|
1140
|
+
limits: rt.limits
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
function normalizeHeaders(headers) {
|
|
1144
|
+
const out = {};
|
|
1145
|
+
if (typeof headers.forEach === "function" && typeof headers.get === "function") {
|
|
1146
|
+
headers.forEach((value, name) => {
|
|
1147
|
+
out[name.toLowerCase()] = value;
|
|
1148
|
+
});
|
|
1149
|
+
return out;
|
|
1150
|
+
}
|
|
1151
|
+
for (const [name, value] of Object.entries(headers)) out[name.toLowerCase()] = typeof value === "string" || value === void 0 ? value : value.join(", ");
|
|
1152
|
+
return out;
|
|
1153
|
+
}
|
|
1154
|
+
const RESERVED = new Set(RELAY_QUERY_PARAMS);
|
|
1155
|
+
const SPEAK_FIELDS = /* @__PURE__ */ new Set([
|
|
1156
|
+
"text",
|
|
1157
|
+
"voiceId",
|
|
1158
|
+
"languageCode",
|
|
1159
|
+
"expressive",
|
|
1160
|
+
"lowLatency"
|
|
1161
|
+
]);
|
|
1162
|
+
/**
|
|
1163
|
+
* A `RelayRequest` from the request URL (query: `token`, `language`, `encoding`, `voiceId`,
|
|
1164
|
+
* `languageCode`, the rest is scope) and headers. For `speak`, pass the JSON body: its string fields
|
|
1165
|
+
* other than the clip's own are scope too. The token is `?token=`, else an `Authorization: Bearer` header.
|
|
1166
|
+
*/
|
|
1167
|
+
function relayRequest(input) {
|
|
1168
|
+
const url = typeof input.url === "string" ? new URL(input.url, "http://localhost") : input.url;
|
|
1169
|
+
const headers = normalizeHeaders(input.headers);
|
|
1170
|
+
const scope = {};
|
|
1171
|
+
const params = {};
|
|
1172
|
+
for (const [key, value] of url.searchParams) if (RESERVED.has(key)) params[key] = value;
|
|
1173
|
+
else scope[key] = value;
|
|
1174
|
+
if (input.kind === "speak" && input.body && typeof input.body === "object") {
|
|
1175
|
+
const body = input.body;
|
|
1176
|
+
for (const [key, value] of Object.entries(body)) if (!SPEAK_FIELDS.has(key) && typeof value === "string") scope[key] = value;
|
|
1177
|
+
if (typeof body.languageCode === "string") params.languageCode = body.languageCode;
|
|
1178
|
+
if (typeof body.voiceId === "string") params.voiceId = body.voiceId;
|
|
1179
|
+
}
|
|
1180
|
+
const bearer = /^Bearer\s+(.+)$/i.exec(headers.authorization ?? "")?.[1] ?? null;
|
|
1181
|
+
return {
|
|
1182
|
+
kind: input.kind,
|
|
1183
|
+
origin: headers.origin ?? null,
|
|
1184
|
+
token: params.token ?? bearer,
|
|
1185
|
+
headers,
|
|
1186
|
+
scope,
|
|
1187
|
+
language: params.language ?? null,
|
|
1188
|
+
params,
|
|
1189
|
+
context: input.context
|
|
1190
|
+
};
|
|
1191
|
+
}
|
|
1192
|
+
export { DEEPGRAM_HOST_PATTERN as _, relayRequest as a, formatAuditLine as b, defaultDialer as c, RETRANSCRIBE_CONTENT_TYPE as d, TTS_MAX_CLIENT_FRAME_BYTES as f, buildElevenLabsUrl as g, ELEVENLABS_HOST as h, refusalStatusText as i, wsDialer as l, withoutKey as m, createVoiceRelay as n, sttStreamLanguage as o, VOICE_RELAY_DEFAULT_PATHS as p, isRelayRefusal as r, ttsTimeoutMs as s, DEFAULT_RELAY_LIMITS as t, wsRelaySocket as u, buildDeepgramUrl as v, isAllowedDeepgramHost as y };
|
|
1193
|
+
|
|
1194
|
+
//# sourceMappingURL=relay.mjs.map
|