@rivus/gateway 0.18.2 → 0.20.0

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.
@@ -0,0 +1,411 @@
1
+ // Transcribes one voice message. Usage: node transcribe.mjs <audio-path>
2
+ //
3
+ // Input is recognized by content, not by extension: Ogg/Opus (Feishu voice messages) or 16 kHz mono 16-bit PCM WAV. The
4
+ // Whisper model and the engine runtime (native addon or WebAssembly) come from ./model, written by fetch-model.mjs.
5
+ // RIVUS_AUDIO_LANGUAGE optionally fixes a Whisper language code such as zh or en; by default Whisper detects the
6
+ // language of every chunk, which also keeps code-mixed speech intact.
7
+ //
8
+ // Exit codes: 0 with only the transcript on stdout and nothing on stderr; 1 on an error, described on stderr; 2 when
9
+ // the audio contains no recognizable speech.
10
+ import fs from "node:fs";
11
+ import { access, readFile } from "node:fs/promises";
12
+ import { availableParallelism } from "node:os";
13
+ import { join } from "node:path";
14
+ import { fileURLToPath } from "node:url";
15
+
16
+ import { decodeByteText } from "./byte-tokens.mjs";
17
+
18
+ const MODEL_DIRECTORY = fileURLToPath(new URL("model/", import.meta.url));
19
+ const MODEL_FILES = [
20
+ "runtime.json",
21
+ "encoder.int8.onnx",
22
+ "decoder.int8.onnx",
23
+ "tokens.bytes.txt",
24
+ "silero_vad.int8.onnx"
25
+ ];
26
+ // Whisper features and silero VAD both run at 16 kHz.
27
+ const SAMPLE_RATE = 16_000;
28
+ const VAD_WINDOW = 512;
29
+ // sherpa-onnx decodes at most 29.5 s of a Whisper stream and silently drops the rest, so longer audio is cut into
30
+ // chunks of at most CHUNK_SECONDS. A chunk merges consecutive VAD segments, including the pauses between them, plus a
31
+ // little context on each side. silero's maxSpeechDuration only makes it look for an end more eagerly (a 28 s limit
32
+ // still produced a 33 s segment), so an over-long chunk is also cut at its quietest point.
33
+ const MAX_SPEECH_SECONDS = 20;
34
+ const CHUNK_SECONDS = 28;
35
+ const CHUNK_PAD_SECONDS = 0.25;
36
+ // Zero feature frames sherpa appends so Whisper can emit its end token. 300, the value sherpa's docs suggest for
37
+ // multilingual models, made turbo repeat short replies.
38
+ const TAIL_PADDINGS = 1000;
39
+ // openai/whisper's tokenizer LANGUAGES. sherpa-onnx exits the whole process on a code the model does not know; "yue"
40
+ // exists only in large-v3 and turbo.
41
+ export const WHISPER_LANGUAGES = new Set(
42
+ `en zh de es ru ko fr ja pt tr pl ca nl ar sv it id hi fi vi he uk el ms cs ro da hu ta no th ur hr bg lt la mi ml cy
43
+ sk te fa lv bn sr az sl kn et mk br eu is hy ne mn bs kk sq sw gl mr pa si km sn yo so af oc ka be tg sd gu am yi lo
44
+ uz fo ht ps tk nn mt sa lb my bo tl mg as tt haw ln ha ba jw su yue`.split(/\s+/)
45
+ );
46
+
47
+ // process.stdout and process.stderr are created on first access. When they are files, Node binds the fs.writeSync that
48
+ // is current at that moment, so they must exist before quietly() first replaces it.
49
+ const { stderr, stdout } = process;
50
+
51
+ class NoSpeechError extends Error {}
52
+
53
+ // The WebAssembly build writes its native log lines to fd 1 with fs.writeSync, and its JavaScript wrappers and the Opus
54
+ // decoder report errors with console.*. While fn runs, those writes are collected instead; callers add them to their
55
+ // error messages, and a successful run prints none of them.
56
+ function quietly(fn) {
57
+ const logged = [];
58
+ const { writeSync } = fs;
59
+ const { error, log, warn } = console;
60
+ fs.writeSync = (fd, data, offset = 0, length, ...rest) => {
61
+ if (fd !== 1 && fd !== 2) {
62
+ return writeSync(fd, data, offset, length, ...rest);
63
+ }
64
+ const text =
65
+ typeof data === "string"
66
+ ? data
67
+ : Buffer.from(data.buffer, data.byteOffset + offset, length ?? data.byteLength - offset).toString();
68
+ if (text.trim()) {
69
+ logged.push(text.trim());
70
+ }
71
+ return typeof data === "string" ? Buffer.byteLength(data) : (length ?? data.byteLength - offset);
72
+ };
73
+ console.log = console.error = console.warn = (...args) => logged.push(args.join(" "));
74
+ const restore = () => {
75
+ fs.writeSync = writeSync;
76
+ Object.assign(console, { error, log, warn });
77
+ };
78
+ try {
79
+ const result = fn(logged);
80
+ if (result instanceof Promise) {
81
+ return result.finally(restore);
82
+ }
83
+ restore();
84
+ return result;
85
+ } catch (thrown) {
86
+ restore();
87
+ throw thrown;
88
+ }
89
+ }
90
+
91
+ export function detectFormat(buffer) {
92
+ // An Ogg page header is 27 bytes plus a segment table of buffer[26] entries; an Opus stream's first packet is
93
+ // OpusHead.
94
+ if (buffer.length >= 27 && buffer.toString("latin1", 0, 4) === "OggS") {
95
+ const packet = 27 + buffer[26];
96
+ return buffer.toString("latin1", packet, packet + 8) === "OpusHead" ? "ogg-opus" : "ogg-other";
97
+ }
98
+ if (
99
+ buffer.length >= 12 &&
100
+ buffer.toString("latin1", 0, 4) === "RIFF" &&
101
+ buffer.toString("latin1", 8, 12) === "WAVE"
102
+ ) {
103
+ return "wav";
104
+ }
105
+ return "unknown";
106
+ }
107
+
108
+ async function decodeOggOpus(buffer) {
109
+ const { OggOpusDecoder } = await import("ogg-opus-decoder");
110
+ const decoder = new OggOpusDecoder({ sampleRate: SAMPLE_RATE });
111
+ try {
112
+ await decoder.ready;
113
+ return await quietly(async (logged) => {
114
+ const { channelData, errors, samplesDecoded } = await decoder.decodeFile(new Uint8Array(buffer));
115
+ if (!samplesDecoded) {
116
+ const details = [...errors.map((decodeError) => decodeError.message), ...logged].join("; ");
117
+ throw new Error(`the Ogg/Opus stream decoded to no audio${details ? `: ${details}` : ""}`);
118
+ }
119
+ return downmix(channelData, samplesDecoded);
120
+ });
121
+ } finally {
122
+ decoder.free();
123
+ }
124
+ }
125
+
126
+ function downmix(channelData, length) {
127
+ if (channelData.length === 1) {
128
+ return channelData[0].subarray(0, length);
129
+ }
130
+ const mono = new Float32Array(length);
131
+ for (const channel of channelData) {
132
+ for (let i = 0; i < length; i++) {
133
+ mono[i] += channel[i] / channelData.length;
134
+ }
135
+ }
136
+ return mono;
137
+ }
138
+
139
+ // sherpa's WAV reader keeps only the first channel and the transcriber has no resampler, so only 16 kHz mono 16-bit
140
+ // PCM (format code 1) with the fmt chunk first is accepted.
141
+ export function checkWav(buffer) {
142
+ if (
143
+ buffer.length < 44 ||
144
+ buffer.toString("latin1", 12, 16) !== "fmt " ||
145
+ buffer.readUInt16LE(20) !== 1 ||
146
+ buffer.readUInt16LE(22) !== 1 ||
147
+ buffer.readUInt32LE(24) !== SAMPLE_RATE ||
148
+ buffer.readUInt16LE(34) !== 16
149
+ ) {
150
+ throw new Error("WAV input must be 16 kHz mono 16-bit PCM");
151
+ }
152
+ }
153
+
154
+ function readWav(engine, buffer, path) {
155
+ const wave = engine.readWave(buffer, path);
156
+ if (!wave) {
157
+ throw new Error("failed to read the WAV file");
158
+ }
159
+ return wave.samples;
160
+ }
161
+
162
+ // The two packages expose the same engine through different call shapes; these adapters give the rest of the script
163
+ // one interface.
164
+ function nativeEngine(sherpa) {
165
+ return {
166
+ createRecognizer: (config) => {
167
+ const recognizer = new sherpa.OfflineRecognizer(config);
168
+ return (samples) => {
169
+ const stream = recognizer.createStream();
170
+ stream.acceptWaveform({ sampleRate: SAMPLE_RATE, samples });
171
+ recognizer.decode(stream);
172
+ return recognizer.getResult(stream);
173
+ };
174
+ },
175
+ createVad: (config, bufferSeconds) => {
176
+ const vad = new sherpa.Vad({ ...config, debug: false }, bufferSeconds);
177
+ return { ...bindVad(vad), front: () => vad.front(false) };
178
+ },
179
+ numThreads: Math.min(4, availableParallelism()),
180
+ readWave: (_buffer, path) => sherpa.readWave(path)
181
+ };
182
+ }
183
+
184
+ function wasmEngine(sherpa) {
185
+ // sherpa does not throw on a bad configuration: it logs and returns a null handle. An onnxruntime failure such as a
186
+ // corrupt model escapes as a C++ exception, which this build throws into JavaScript as a bare number.
187
+ const create = (what, factory) =>
188
+ quietly((logged) => {
189
+ let object;
190
+ try {
191
+ object = factory();
192
+ } catch (thrown) {
193
+ if (thrown instanceof Error) {
194
+ throw thrown;
195
+ }
196
+ throw new Error(`failed to create the ${what}: native exception (corrupt or incompatible model file?)`);
197
+ }
198
+ if (!object.handle) {
199
+ throw new Error(`failed to create the ${what}${logged.length ? `: ${logged.join("; ")}` : ""}`);
200
+ }
201
+ return object;
202
+ });
203
+ return {
204
+ createRecognizer: (config) => {
205
+ const recognizer = create("recognizer", () => sherpa.createOfflineRecognizer(config));
206
+ return (samples) =>
207
+ quietly(() => {
208
+ const stream = recognizer.createStream();
209
+ stream.acceptWaveform(SAMPLE_RATE, samples);
210
+ recognizer.decode(stream);
211
+ const result = recognizer.getResult(stream);
212
+ stream.free();
213
+ return result;
214
+ });
215
+ },
216
+ createVad: (config, bufferSeconds) => {
217
+ const vad = create("VAD", () =>
218
+ sherpa.createVad({ ...config, bufferSizeInSeconds: bufferSeconds, debug: 0, provider: "cpu" })
219
+ );
220
+ return Object.fromEntries(
221
+ Object.entries(bindVad(vad)).map(([name, method]) => [name, (...args) => quietly(() => method(...args))])
222
+ );
223
+ },
224
+ numThreads: 1,
225
+ readWave: (buffer) => quietly(() => sherpa.readWaveFromBinaryData(new Uint8Array(buffer)))
226
+ };
227
+ }
228
+
229
+ function bindVad(vad) {
230
+ return {
231
+ acceptWaveform: (samples) => vad.acceptWaveform(samples),
232
+ flush: () => vad.flush(),
233
+ front: () => vad.front(),
234
+ isEmpty: () => vad.isEmpty(),
235
+ pop: () => vad.pop()
236
+ };
237
+ }
238
+
239
+ async function loadEngine(runtime) {
240
+ if (runtime === "wasm") {
241
+ return wasmEngine(await quietly(async () => (await import("sherpa-onnx")).default));
242
+ }
243
+ try {
244
+ return nativeEngine((await import("sherpa-onnx-node")).default);
245
+ } catch (error) {
246
+ throw new Error(`cannot load the native sherpa-onnx addon; reinstall the transcriber: ${error.message}`);
247
+ }
248
+ }
249
+
250
+ // Whisper turns silence and noise into text, so silero VAD decides whether there is speech at all and where it is.
251
+ function speechSegments(engine, samples) {
252
+ const vad = engine.createVad(
253
+ {
254
+ numThreads: 1,
255
+ sampleRate: SAMPLE_RATE,
256
+ sileroVad: {
257
+ maxSpeechDuration: MAX_SPEECH_SECONDS,
258
+ minSilenceDuration: 0.5,
259
+ minSpeechDuration: 0.25,
260
+ model: join(MODEL_DIRECTORY, "silero_vad.int8.onnx"),
261
+ threshold: 0.5,
262
+ windowSize: VAD_WINDOW
263
+ }
264
+ },
265
+ Math.ceil(samples.length / SAMPLE_RATE) + 1
266
+ );
267
+ for (let i = 0; i + VAD_WINDOW <= samples.length; i += VAD_WINDOW) {
268
+ vad.acceptWaveform(samples.subarray(i, i + VAD_WINDOW));
269
+ }
270
+ vad.flush();
271
+ const segments = [];
272
+ for (; !vad.isEmpty(); vad.pop()) {
273
+ const { samples: speech, start } = vad.front();
274
+ segments.push({ end: start + speech.length, start });
275
+ }
276
+ return segments;
277
+ }
278
+
279
+ // Merges consecutive VAD segments into chunks, widens each by a little context without overlapping its neighbours,
280
+ // then splits any chunk that is still longer than CHUNK_SECONDS.
281
+ export function chunkSegments(segments, samples) {
282
+ const limit = CHUNK_SECONDS * SAMPLE_RATE;
283
+ const pad = CHUNK_PAD_SECONDS * SAMPLE_RATE;
284
+ const merged = [];
285
+ for (const segment of segments) {
286
+ const last = merged.at(-1);
287
+ if (last && segment.end - last.start <= limit - 2 * pad) {
288
+ last.end = segment.end;
289
+ } else {
290
+ merged.push({ ...segment });
291
+ }
292
+ }
293
+ return merged.flatMap((chunk, i) => {
294
+ let start = Math.max(chunk.start - pad, i ? merged[i - 1].end : 0);
295
+ const end = Math.min(chunk.end + pad, merged[i + 1]?.start ?? samples.length);
296
+ const pieces = [];
297
+ while (end - start > limit) {
298
+ const cut = quietestPoint(samples, start + limit - 5 * SAMPLE_RATE, start + limit);
299
+ pieces.push({ end: cut, start });
300
+ start = cut;
301
+ }
302
+ pieces.push({ end, start });
303
+ return pieces;
304
+ });
305
+ }
306
+
307
+ // Centre of the 100 ms window with the least energy in [from, to).
308
+ function quietestPoint(samples, from, to) {
309
+ const window = SAMPLE_RATE / 10;
310
+ let best = to;
311
+ let bestEnergy = Infinity;
312
+ for (let i = from; i + window <= to; i += window / 2) {
313
+ let energy = 0;
314
+ for (let j = i; j < i + window; j++) {
315
+ energy += samples[j] * samples[j];
316
+ }
317
+ if (energy < bestEnergy) {
318
+ best = i + window / 2;
319
+ bestEnergy = energy;
320
+ }
321
+ }
322
+ return best;
323
+ }
324
+
325
+ function recognizerConfig(engine, language) {
326
+ return {
327
+ featConfig: { featureDim: 80, sampleRate: SAMPLE_RATE },
328
+ modelConfig: {
329
+ debug: 0,
330
+ numThreads: engine.numThreads,
331
+ provider: "cpu",
332
+ tokens: join(MODEL_DIRECTORY, "tokens.bytes.txt"),
333
+ whisper: {
334
+ decoder: join(MODEL_DIRECTORY, "decoder.int8.onnx"),
335
+ encoder: join(MODEL_DIRECTORY, "encoder.int8.onnx"),
336
+ language,
337
+ tailPaddings: TAIL_PADDINGS,
338
+ task: "transcribe"
339
+ }
340
+ }
341
+ };
342
+ }
343
+
344
+ async function main() {
345
+ const audioPath = process.argv[2];
346
+ if (!audioPath) {
347
+ throw new Error("usage: node transcribe.mjs <audio-path>");
348
+ }
349
+ const language = process.env.RIVUS_AUDIO_LANGUAGE ?? "";
350
+ if (language && !WHISPER_LANGUAGES.has(language)) {
351
+ throw new Error(`RIVUS_AUDIO_LANGUAGE is not a Whisper language code: ${JSON.stringify(language)}`);
352
+ }
353
+ // sherpa-onnx does not throw on a missing model file but logs and returns a null handle, so missing files are
354
+ // reported before anything is constructed.
355
+ for (const name of MODEL_FILES) {
356
+ await access(join(MODEL_DIRECTORY, name)).catch(() => {
357
+ throw new Error(`the transcriber model is incomplete: ${join(MODEL_DIRECTORY, name)} is missing`);
358
+ });
359
+ }
360
+ const { runtime } = JSON.parse(await readFile(join(MODEL_DIRECTORY, "runtime.json"), "utf8"));
361
+
362
+ const buffer = await readFile(audioPath);
363
+ const format = detectFormat(buffer);
364
+ if (format !== "ogg-opus" && format !== "wav") {
365
+ throw new Error(`unsupported audio format (${format}); expected Ogg/Opus or WAV`);
366
+ }
367
+ if (format === "wav") {
368
+ checkWav(buffer);
369
+ }
370
+ const engine = await loadEngine(runtime);
371
+ const samples = format === "ogg-opus" ? await decodeOggOpus(buffer) : readWav(engine, buffer, audioPath);
372
+ const segments = samples.length ? speechSegments(engine, samples) : [];
373
+ if (!segments.length) {
374
+ throw new NoSpeechError("no speech detected");
375
+ }
376
+
377
+ const recognize = engine.createRecognizer(recognizerConfig(engine, language));
378
+ // Whisper has a single "zh" language and writes Mandarin in traditional or simplified characters, sometimes switching
379
+ // between the chunks of one message; sherpa-onnx has no initial prompt to steer it.
380
+ const { default: OpenCC } = await import("opencc-js/t2cn");
381
+ const toSimplified = OpenCC.Converter({ from: "t", to: "cn" });
382
+ const texts = chunkSegments(segments, samples).map(({ end, start }) => {
383
+ const result = recognize(samples.subarray(start, end));
384
+ const text = decodeByteText(result.text);
385
+ return (result.lang || language) === "zh" ? toSimplified(text) : text;
386
+ });
387
+ // English tokens carry their own leading space; Chinese ones have none.
388
+ const transcript = texts.join("").trim();
389
+ if (!/[\p{L}\p{N}]/u.test(transcript)) {
390
+ throw new NoSpeechError("no speech recognized");
391
+ }
392
+ stdout.write(`${transcript}\n`);
393
+ }
394
+
395
+ // Tests import the helpers above without the transcriber's npm dependencies, so main() loads those on first use.
396
+ if (import.meta.main) {
397
+ try {
398
+ await main();
399
+ } catch (error) {
400
+ // A WebAssembly abort is almost always malloc failing at the 2 GiB wasm32 heap limit. Other engine failures arrive
401
+ // as a bare number (a C++ exception) or an emscripten ExitStatus object.
402
+ const message =
403
+ error instanceof WebAssembly.RuntimeError
404
+ ? `sherpa-onnx wasm aborted (${error.message.split(".")[0]}); likely out of wasm memory`
405
+ : error instanceof Error
406
+ ? error.message
407
+ : `native exception ${JSON.stringify(error)} in sherpa-onnx`;
408
+ stderr.write(`transcribe: ${message}\n`);
409
+ process.exitCode = error instanceof NoSpeechError ? 2 : 1;
410
+ }
411
+ }
@@ -0,0 +1,13 @@
1
+ import { R as RecoveryControl, a as CreateRivusDeploymentAutomationPortInput, b as AutomationScheduler, c as CreateRivusDeploymentRuntimePortInput, d as RivusDeploymentRuntimePort, l as RivusDeploymentBackgroundSessionPort, o as CreateRivusDeploymentBackgroundSessionPortInput, s as CreateRivusDeploymentEndpointPortInput, t as RivusDeploymentBootstrapContext, u as RivusDeploymentEndpointPort } from "../chunks/rivus-deployment-cli-process.js";
2
+ import { Effect } from "effect";
3
+ //#region src/bootstrap/feishu.d.ts
4
+ export declare function createRivusDeploymentAdapters(context: RivusDeploymentBootstrapContext): Promise<{
5
+ dispose: () => Promise<void>;
6
+ createRecoveryControl: () => Effect.Effect<RecoveryControl, unknown, never>;
7
+ createBackgroundSession: ((input: CreateRivusDeploymentBackgroundSessionPortInput) => Effect.Effect<RivusDeploymentBackgroundSessionPort, unknown, never>) | undefined;
8
+ createAutomation: (input: CreateRivusDeploymentAutomationPortInput) => Effect.Effect<AutomationScheduler, unknown, never>;
9
+ createEndpoint: (input: CreateRivusDeploymentEndpointPortInput) => Effect.Effect<RivusDeploymentEndpointPort, unknown, never>;
10
+ createRuntime: (input: CreateRivusDeploymentRuntimePortInput) => Effect.Effect<RivusDeploymentRuntimePort, unknown>;
11
+ }>;
12
+ //#endregion
13
+ export {};