@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.d.mts
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
/** Where the relay routes live by default; a host may mount them elsewhere. */
|
|
2
|
+
declare const VOICE_RELAY_DEFAULT_PATHS: {
|
|
3
|
+
readonly stt: "/voice/transcribe-stream";
|
|
4
|
+
readonly tts: "/voice/speak-stream";
|
|
5
|
+
readonly retranscribe: "/voice/retranscribe";
|
|
6
|
+
readonly speak: "/voice/speak";
|
|
7
|
+
};
|
|
8
|
+
type VoiceRelayRoute = keyof typeof VOICE_RELAY_DEFAULT_PATHS;
|
|
9
|
+
/**
|
|
10
|
+
* Query parameters the relay reads itself. Every other query parameter (or, for `speak`, every other
|
|
11
|
+
* body field) is the host's opaque scope (`sessionId`, `organizationId`, `spaceId`, …).
|
|
12
|
+
* `token` carries a bearer, since a browser cannot set headers on `new WebSocket()`.
|
|
13
|
+
*/
|
|
14
|
+
declare const RELAY_QUERY_PARAMS: readonly ["token", "language", "encoding", "voiceId", "languageCode"];
|
|
15
|
+
/** The 200 answer of `POST retranscribe`; errors are 400/401/403/413/415/429/501/502/504 with `{ message }`. */
|
|
16
|
+
interface RetranscribeResult {
|
|
17
|
+
transcript: string;
|
|
18
|
+
/** The pinned language, else the detected one. */
|
|
19
|
+
language: string | null;
|
|
20
|
+
/** How sure the provider is of the detected language; null with a pinned language. */
|
|
21
|
+
languageConfidence: number | null;
|
|
22
|
+
/** How sure the provider is of the words, so a client can compare two languages' transcripts. */
|
|
23
|
+
confidence: number | null;
|
|
24
|
+
}
|
|
25
|
+
/** The JSON body of `POST speak` (answer: `audio/mpeg`). Other fields are the host's scope. */
|
|
26
|
+
interface SpeakRequestBody {
|
|
27
|
+
text: string;
|
|
28
|
+
voiceId?: string | null;
|
|
29
|
+
languageCode?: string | null;
|
|
30
|
+
/** Speak on the expressive (v4) model so `[[...]]` audio tags are performed; for short clips. */
|
|
31
|
+
expressive?: boolean;
|
|
32
|
+
/** Speak on the low-latency model. */
|
|
33
|
+
lowLatency?: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The client side of a relay stream: a `ws` socket, a crossws peer, … behind a small shim
|
|
37
|
+
* (`wsRelaySocket`, the h3 entry's peer shim). The shim drops a send to a socket that is no longer
|
|
38
|
+
* open, ends the socket on a transport error, and tells text frames (`string`) from binary ones.
|
|
39
|
+
* `onClose` must fire once the socket is gone, also after the relay's own `close`.
|
|
40
|
+
*/
|
|
41
|
+
interface RelaySocket {
|
|
42
|
+
send(data: string | Uint8Array): void;
|
|
43
|
+
close(code?: number, reason?: string): void;
|
|
44
|
+
onMessage(listener: (data: string | Uint8Array) => void): void;
|
|
45
|
+
onClose(listener: () => void): void;
|
|
46
|
+
}
|
|
47
|
+
/** The part of a `ws` WebSocket (`ws` 8) the shims use. */
|
|
48
|
+
interface WsLike {
|
|
49
|
+
readonly readyState: number;
|
|
50
|
+
send(data: string | Uint8Array, options?: {
|
|
51
|
+
binary?: boolean;
|
|
52
|
+
}): void;
|
|
53
|
+
close(code?: number, reason?: string): void;
|
|
54
|
+
terminate(): void;
|
|
55
|
+
on(event: string, listener: (...args: any[]) => void): unknown;
|
|
56
|
+
}
|
|
57
|
+
/** A `ws` server-side socket (e.g. from `WebSocketServer.handleUpgrade`) as a `RelaySocket`. */
|
|
58
|
+
declare function wsRelaySocket(ws: WsLike): RelaySocket;
|
|
59
|
+
/** A rejected provider handshake (a non-101 answer). */
|
|
60
|
+
interface UpstreamRejection {
|
|
61
|
+
status: number;
|
|
62
|
+
statusText: string;
|
|
63
|
+
/** Reads the answer body; rejects when the read fails. It may echo the request, key included. */
|
|
64
|
+
body(): Promise<string>;
|
|
65
|
+
/** Drains the body unread. */
|
|
66
|
+
discard(): void;
|
|
67
|
+
}
|
|
68
|
+
interface UpstreamHandlers {
|
|
69
|
+
open(): void;
|
|
70
|
+
/** A text frame (every provider frame the relay reads is JSON text). */
|
|
71
|
+
message(data: string): void;
|
|
72
|
+
rejected(rejection: UpstreamRejection): void;
|
|
73
|
+
/** A transport error; its message may quote request headers (the key). */
|
|
74
|
+
error(error: {
|
|
75
|
+
name: string;
|
|
76
|
+
message: string;
|
|
77
|
+
code?: unknown;
|
|
78
|
+
}): void;
|
|
79
|
+
close(code: number, reason: string): void;
|
|
80
|
+
}
|
|
81
|
+
/** An outbound provider socket. Events fire only after `on` is called (dialling is asynchronous). */
|
|
82
|
+
interface UpstreamSocket {
|
|
83
|
+
readonly state: 'connecting' | 'open' | 'closed';
|
|
84
|
+
send(data: string | Uint8Array): void;
|
|
85
|
+
close(): void;
|
|
86
|
+
on(handlers: UpstreamHandlers): void;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Opens a provider socket whose handshake carries the key in a header. May throw synchronously
|
|
90
|
+
* (an invalid header value does); the relay answers that with fixed text and never logs the error.
|
|
91
|
+
*/
|
|
92
|
+
type UpstreamDialer = (url: URL, headers: Record<string, string>) => UpstreamSocket;
|
|
93
|
+
/** The `ws` client constructor (`import WebSocket from 'ws'`). */
|
|
94
|
+
type WsConstructor = new (url: string | URL, options: {
|
|
95
|
+
headers: Record<string, string>;
|
|
96
|
+
}) => WsLike;
|
|
97
|
+
/** A dialer on the `ws` client: pass `wsDialer(WebSocket)` with `import WebSocket from 'ws'`. */
|
|
98
|
+
declare function wsDialer(WebSocket: WsConstructor): UpstreamDialer;
|
|
99
|
+
/**
|
|
100
|
+
* The default dialer: `ws`, an optional peer dependency, loaded when the relay is created so a
|
|
101
|
+
* missing install fails at startup rather than on the first stream.
|
|
102
|
+
*/
|
|
103
|
+
declare function defaultDialer(): UpstreamDialer;
|
|
104
|
+
/** One request to the relay, as the host adapter read it, before any provider is contacted. */
|
|
105
|
+
interface RelayRequest<C = unknown> {
|
|
106
|
+
kind: VoiceRelayRoute;
|
|
107
|
+
/** The `Origin` header; the WebSocket routes refuse a missing or unlisted one. */
|
|
108
|
+
origin: string | null;
|
|
109
|
+
/** `?token=` (a browser cannot set headers on `new WebSocket()`); the HTTP routes carry theirs in `headers`. */
|
|
110
|
+
token: string | null;
|
|
111
|
+
headers: Record<string, string | undefined>;
|
|
112
|
+
/** The host's opaque parameters: every query parameter (or `speak` body field) the relay does not read itself. */
|
|
113
|
+
scope: Record<string, string>;
|
|
114
|
+
/** The requested language, raw; the relay checks it against `STT_LANGUAGES` itself. */
|
|
115
|
+
language: string | null;
|
|
116
|
+
/** The other parameters the relay reads (`encoding`, `voiceId`, `languageCode`), raw. */
|
|
117
|
+
params: Partial<Record<(typeof RELAY_QUERY_PARAMS)[number], string>>;
|
|
118
|
+
/** What the adapter adds for `authorize`: the h3 entry passes the web `Request` (and the `H3Event` on HTTP routes). */
|
|
119
|
+
context: C;
|
|
120
|
+
}
|
|
121
|
+
/** `authorize`'s refusal; the relay answers with this status and contacts no provider. */
|
|
122
|
+
interface RelayRefusal {
|
|
123
|
+
refuse: 400 | 401 | 403 | 404 | 409 | 413 | 429 | 501;
|
|
124
|
+
message?: string;
|
|
125
|
+
/** A machine-readable code for the error body (`ADD_ON_DISABLED`, `data_zone_blocked`, …). */
|
|
126
|
+
code?: string;
|
|
127
|
+
/** For 429: the `Retry-After` seconds. */
|
|
128
|
+
retryAfterS?: number;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* A provider key, or a function that reads it. A function is called only once a stream has passed
|
|
132
|
+
* every other gate (a request refused at the concurrency cap reads no key); `null` answers 501.
|
|
133
|
+
*/
|
|
134
|
+
type ProviderKey = string | (() => Promise<string | null>);
|
|
135
|
+
interface DeepgramGrant {
|
|
136
|
+
apiKey: ProviderKey;
|
|
137
|
+
/** Bare API host (`api.eu.deepgram.com`); must match `allowedHosts.deepgram`. There is no default. */
|
|
138
|
+
host: string;
|
|
139
|
+
/** `nova-3`. */
|
|
140
|
+
model: string;
|
|
141
|
+
/** Language when the request pins none: a code from `STT_LANGUAGES`, or `auto` (= `multi` on the stream, detection on re-transcription). */
|
|
142
|
+
defaultLanguage: string;
|
|
143
|
+
}
|
|
144
|
+
interface ElevenLabsGrant {
|
|
145
|
+
apiKey: ProviderKey;
|
|
146
|
+
/** The voice when the request names none or an invalid one. */
|
|
147
|
+
voiceId: string;
|
|
148
|
+
models: {
|
|
149
|
+
/** The streamed replies (`tts`) and expressive clips: a text-to-dialogue model (`eleven_v4_turbo`). */
|
|
150
|
+
stream: string;
|
|
151
|
+
/** Ordinary clips (`eleven_multilingual_v2`). */
|
|
152
|
+
clip: string;
|
|
153
|
+
/** Clips with `lowLatency` (`eleven_flash_v2_5`). */
|
|
154
|
+
clipFast: string;
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/** What `authorize` hands the relay: who pays, with which keys, and which words to listen for. */
|
|
158
|
+
interface VoiceGrant {
|
|
159
|
+
/** The principal the concurrency caps count per, and the audit lines name. */
|
|
160
|
+
principalId: string;
|
|
161
|
+
/** Needed for `stt` and `retranscribe`; without it they answer 501. */
|
|
162
|
+
deepgram?: DeepgramGrant;
|
|
163
|
+
/** Needed for `tts` and `speak`; without it they answer 501. */
|
|
164
|
+
elevenlabs?: ElevenLabsGrant;
|
|
165
|
+
/**
|
|
166
|
+
* Keyterms to prompt recognition with, already filtered by the host's opt-in rules. Gets the
|
|
167
|
+
* stream's language (`multi` for every language) or, for a detecting re-transcription, null.
|
|
168
|
+
* A rejection streams without keyterms rather than failing.
|
|
169
|
+
*/
|
|
170
|
+
keyterms?: (language: string | null) => Promise<readonly string[]>;
|
|
171
|
+
}
|
|
172
|
+
interface VoiceRelayHost<C = unknown> {
|
|
173
|
+
/** Called once per request, before any provider contact. Every gate (identity, add-ons, rate limits) lives here. */
|
|
174
|
+
authorize(request: RelayRequest<C>): Promise<VoiceGrant | RelayRefusal>;
|
|
175
|
+
/** Overrides of the default caps and timers. */
|
|
176
|
+
limits?: RelayLimitsInput;
|
|
177
|
+
/** One structured line per provider contact, ids, sizes and codes only. Default: `logger.warn(formatAuditLine(line))`. */
|
|
178
|
+
audit?(line: RelayAuditLine): void;
|
|
179
|
+
/** Default: `console`. Lines carry no audio, transcript, text or key. */
|
|
180
|
+
logger?: RelayLogger;
|
|
181
|
+
}
|
|
182
|
+
interface RelayLogger {
|
|
183
|
+
warn(message: string): void;
|
|
184
|
+
error(message: string): void;
|
|
185
|
+
log?(message: string): void;
|
|
186
|
+
}
|
|
187
|
+
interface RelayLimits {
|
|
188
|
+
stt: {
|
|
189
|
+
/** Open streams per principal (per process). */
|
|
190
|
+
maxConcurrent: number;
|
|
191
|
+
maxDurationMs: number;
|
|
192
|
+
/** Closed after this long without a client frame. */
|
|
193
|
+
idleMs: number;
|
|
194
|
+
/** Lead time of the `expiring` frame before `maxDurationMs`. */
|
|
195
|
+
expiryWarningMs: number;
|
|
196
|
+
};
|
|
197
|
+
tts: {
|
|
198
|
+
maxConcurrent: number;
|
|
199
|
+
maxDurationMs: number;
|
|
200
|
+
idleMs: number;
|
|
201
|
+
/** The provider closes an idle socket after 20 s; a keep-alive generates nothing. */
|
|
202
|
+
keepAliveMs: number;
|
|
203
|
+
/** Characters per socket; past it: `error { error: 'text limit' }`, close 1008. */
|
|
204
|
+
maxTextChars: number;
|
|
205
|
+
};
|
|
206
|
+
retranscribe: {
|
|
207
|
+
maxBytes: number;
|
|
208
|
+
timeoutMs: number;
|
|
209
|
+
};
|
|
210
|
+
speak: {
|
|
211
|
+
maxTextChars: number;
|
|
212
|
+
/** `expressive` is for short clips; longer text is spoken on the clip model. */
|
|
213
|
+
maxExpressiveChars: number;
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
type RelayLimitsInput = { [K in keyof RelayLimits]?: Partial<RelayLimits[K]>; };
|
|
217
|
+
interface VoiceRelayConfig {
|
|
218
|
+
/** Origins the WebSocket routes accept. Fails closed: an empty list or a missing `Origin` refuses (403). */
|
|
219
|
+
allowedOrigins: readonly string[] | ((origin: string) => boolean);
|
|
220
|
+
/** Opens provider sockets; default `ws` (an optional peer dependency). */
|
|
221
|
+
dial?: UpstreamDialer;
|
|
222
|
+
/** `fetch` for the HTTP routes; default the global one at call time. */
|
|
223
|
+
fetch?: typeof fetch;
|
|
224
|
+
/**
|
|
225
|
+
* The privacy parameters, declared for audits. Their types are literals: neither can be turned off,
|
|
226
|
+
* and the URL builders set them last whatever this says.
|
|
227
|
+
*/
|
|
228
|
+
privacy?: {
|
|
229
|
+
deepgramMipOptOut?: true;
|
|
230
|
+
elevenlabsEnableLogging?: false;
|
|
231
|
+
};
|
|
232
|
+
allowedHosts?: {
|
|
233
|
+
deepgram?: RegExp;
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
type RelayAuditLine = {
|
|
237
|
+
event: 'deepgram-stream-opened';
|
|
238
|
+
principalId: string;
|
|
239
|
+
host: string;
|
|
240
|
+
model: string | null;
|
|
241
|
+
language: string;
|
|
242
|
+
mipOptOut: string | null;
|
|
243
|
+
keyterms: number;
|
|
244
|
+
} | {
|
|
245
|
+
event: 'deepgram-retranscription-requested';
|
|
246
|
+
principalId: string;
|
|
247
|
+
host: string;
|
|
248
|
+
model: string | null;
|
|
249
|
+
bytes: number;
|
|
250
|
+
mipOptOut: string | null;
|
|
251
|
+
keyterms: number;
|
|
252
|
+
} | {
|
|
253
|
+
event: 'elevenlabs-stream-opened';
|
|
254
|
+
principalId: string;
|
|
255
|
+
voiceId: string;
|
|
256
|
+
model: string | null;
|
|
257
|
+
enableLogging: string | null;
|
|
258
|
+
} | {
|
|
259
|
+
event: 'elevenlabs-stream-closed';
|
|
260
|
+
principalId: string;
|
|
261
|
+
chars: number;
|
|
262
|
+
} | {
|
|
263
|
+
event: 'elevenlabs-speech-requested';
|
|
264
|
+
principalId: string;
|
|
265
|
+
voiceId: string;
|
|
266
|
+
model: string;
|
|
267
|
+
chars: number;
|
|
268
|
+
enableLogging: string | null;
|
|
269
|
+
};
|
|
270
|
+
/** The outcome of an HTTP route: the value to answer 200 with, or an error status and body. */
|
|
271
|
+
type RelayResult<T> = {
|
|
272
|
+
ok: true;
|
|
273
|
+
value: T;
|
|
274
|
+
} | {
|
|
275
|
+
ok: false;
|
|
276
|
+
status: number;
|
|
277
|
+
message: string;
|
|
278
|
+
code?: string;
|
|
279
|
+
retryAfterS?: number;
|
|
280
|
+
};
|
|
281
|
+
/** A spoken clip: the provider's audio stream, relayed as it arrives. */
|
|
282
|
+
interface SpokenClip {
|
|
283
|
+
contentType: 'audio/mpeg';
|
|
284
|
+
body: ReadableStream<Uint8Array>;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* A WebSocket request that passed every gate and holds a slot under its principal's cap. Hand it to
|
|
288
|
+
* `relay.stt` / `relay.tts` once the upgrade is done; if the upgrade fails, `release()` it. An
|
|
289
|
+
* admission not handed over within 10 s releases itself.
|
|
290
|
+
*/
|
|
291
|
+
interface VoiceAdmission {
|
|
292
|
+
readonly kind: 'stt' | 'tts';
|
|
293
|
+
readonly principalId: string;
|
|
294
|
+
release(): void;
|
|
295
|
+
}
|
|
296
|
+
interface VoiceRelay<C = unknown> {
|
|
297
|
+
/**
|
|
298
|
+
* Runs the gates of a WebSocket route (`stt`, `tts`) before the upgrade: the Origin allowlist, the
|
|
299
|
+
* host's `authorize`, the keyterm read, the per-principal cap and the key. A refusal is answered on
|
|
300
|
+
* the upgrade request (HTTP status); an admission holds a slot until its stream ends.
|
|
301
|
+
*/
|
|
302
|
+
admit(request: RelayRequest<C>): Promise<VoiceAdmission | RelayRefusal>;
|
|
303
|
+
/** After the upgrade: pipes the client socket to Deepgram's live stream. */
|
|
304
|
+
stt(client: RelaySocket, admission: VoiceAdmission): void;
|
|
305
|
+
/** After the upgrade: pipes the client socket to ElevenLabs' text-to-dialogue stream. */
|
|
306
|
+
tts(client: RelaySocket, admission: VoiceAdmission): void;
|
|
307
|
+
/** `POST retranscribe`: the body is raw 16 kHz linear16. Gates, then Deepgram prerecorded. */
|
|
308
|
+
retranscribe(request: RelayRequest<C>, pcm: Uint8Array): Promise<RelayResult<RetranscribeResult>>;
|
|
309
|
+
/** `POST speak`: gates, then one clip from ElevenLabs as an `audio/mpeg` stream. */
|
|
310
|
+
speak(request: RelayRequest<C>, body: SpeakRequestBody): Promise<RelayResult<SpokenClip>>;
|
|
311
|
+
/** Streams the principal holds open on a route (admitted, not yet closed). */
|
|
312
|
+
activeStreams(kind: 'stt' | 'tts', principalId: string): number;
|
|
313
|
+
/** The caps and timers in effect (defaults merged with the host's). */
|
|
314
|
+
readonly limits: RelayLimits;
|
|
315
|
+
}
|
|
316
|
+
declare const DEFAULT_RELAY_LIMITS: RelayLimits;
|
|
317
|
+
declare function isRelayRefusal(value: unknown): value is RelayRefusal;
|
|
318
|
+
/** The text an HTTP status line carries for a refusal. */
|
|
319
|
+
declare function refusalStatusText(refusal: RelayRefusal): string;
|
|
320
|
+
/**
|
|
321
|
+
* The voice relay: speech-to-text and text-to-speech streams, re-transcription and spoken clips,
|
|
322
|
+
* between a browser and Deepgram / ElevenLabs, holding the provider keys server-side. Framework-free;
|
|
323
|
+
* a host adapter (the h3 entry, a NestJS gateway) reads requests and sockets and calls it.
|
|
324
|
+
*
|
|
325
|
+
* Privacy, enforced here and not configurable: every Deepgram URL carries `mip_opt_out=true` and every
|
|
326
|
+
* ElevenLabs URL `enable_logging=false`, set last; a key is redacted from every relayed frame and log
|
|
327
|
+
* line; no audio, transcript or text is logged; the WebSocket Origin check fails closed. Who may use
|
|
328
|
+
* voice is the host's: `authorize` runs before any provider contact, and gets nothing from the relay.
|
|
329
|
+
*/
|
|
330
|
+
declare function createVoiceRelay<C = unknown>(config: VoiceRelayConfig, host: VoiceRelayHost<C>): VoiceRelay<C>;
|
|
331
|
+
/** Headers as the relay reads them: lower-case names, one string each. */
|
|
332
|
+
type RelayHeadersInput = Headers | Record<string, string | readonly string[] | undefined>;
|
|
333
|
+
/**
|
|
334
|
+
* A `RelayRequest` from the request URL (query: `token`, `language`, `encoding`, `voiceId`,
|
|
335
|
+
* `languageCode`, the rest is scope) and headers. For `speak`, pass the JSON body: its string fields
|
|
336
|
+
* other than the clip's own are scope too. The token is `?token=`, else an `Authorization: Bearer` header.
|
|
337
|
+
*/
|
|
338
|
+
declare function relayRequest<C>(input: {
|
|
339
|
+
kind: VoiceRelayRoute;
|
|
340
|
+
url: URL | string;
|
|
341
|
+
headers: RelayHeadersInput;
|
|
342
|
+
context: C;
|
|
343
|
+
body?: unknown;
|
|
344
|
+
}): RelayRequest<C>;
|
|
345
|
+
export { defaultDialer as A, RelaySocket as C, UpstreamSocket as D, UpstreamRejection as E, wsRelaySocket as M, VoiceRelayRoute as N, WsConstructor as O, VoiceRelayHost as S, UpstreamHandlers as T, RelayResult as _, isRelayRefusal as a, VoiceGrant as b, DeepgramGrant as c, RelayAuditLine as d, RelayLimits as f, RelayRequest as g, RelayRefusal as h, createVoiceRelay as i, wsDialer as j, WsLike as k, ElevenLabsGrant as l, RelayLogger as m, RelayHeadersInput as n, refusalStatusText as o, RelayLimitsInput as p, VoiceRelay as r, relayRequest as s, DEFAULT_RELAY_LIMITS as t, ProviderKey as u, SpokenClip as v, UpstreamDialer as w, VoiceRelayConfig as x, VoiceAdmission as y };
|