@pi-chamber/web 0.3.3 → 0.4.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.
- package/README.md +1 -0
- package/bin/lib/cli-startup.js +1 -0
- package/dist/assets/CommitSection-0zl1puqf.js +1 -0
- package/dist/assets/DiagramEditor-CFV_TjE2.js +1 -0
- package/dist/assets/DiagramView-0I4PZUpw.js +1 -0
- package/dist/assets/{DiffView-DGD9DMS7.js → DiffView-BoTz2Zqe.js} +2 -2
- package/dist/assets/{FilesView-DB5VI7EN.js → FilesView-CVMNE9Pr.js} +6 -6
- package/dist/assets/GitView-DnsbCAWl.js +6 -0
- package/dist/assets/JsonTreeView-Cn1Mk0Wf.js +1 -0
- package/dist/assets/{MarkdownRendererImpl-V1HMFCjr.js → MarkdownRendererImpl-DJKD8tNq.js} +2 -2
- package/dist/assets/PatchDiff-GdGq3IlM.js +1 -0
- package/dist/assets/PierreDiffViewer-CdkAUt-S.js +136 -0
- package/dist/assets/SessionSwitcherContent-BTzVmIop.js +1 -0
- package/dist/assets/SessionSwitcherDropdown-DuPmnk3k.js +2 -0
- package/dist/assets/SessionUnreadDot-ogHL0INY.js +1 -0
- package/dist/assets/SettingsView-SHhVrRdh.js +4 -0
- package/dist/assets/SettingsWindow-D_D1qqLF.js +1 -0
- package/dist/assets/{ToolOutputDialog-2zKUdGYL.js → ToolOutputDialog-BHaf293m.js} +6 -6
- package/dist/assets/ToolPartDiffPreview-BTMXNcqG.js +8 -0
- package/dist/assets/{VirtualizedFileDiff-UMgLcO4Q.js → VirtualizedFileDiff-CRVvv23V.js} +4 -4
- package/dist/assets/{SessionSwitcherDropdown-o1RjhCfF.js → WindowsWindowControls-CTtxXnD1.js} +172 -123
- package/dist/assets/collapsible-BO4qEdib.js +1 -0
- package/dist/assets/computeVirtualFileMetrics-D1I8YrPV.js +3 -0
- package/dist/assets/contextFileOpenGuard-w5xG9Qz2.js +1 -0
- package/dist/assets/index-3zee2IYy.css +1 -0
- package/dist/assets/{index-BAqpUCjs.js → index-CI-cd-mw.js} +6 -6
- package/dist/assets/index-DbghctxK.js +1 -0
- package/dist/assets/isDiffPlainText-BK9bF1l5.js +1 -0
- package/dist/assets/isFilePlainText-Cua0RCRz.js +1 -0
- package/dist/assets/main-BNJQ6mSF.js +2 -0
- package/dist/assets/{main-CmkRaMOR.js → main-CoA--JT-.js} +6 -6
- package/dist/assets/markdownCore-BGlLZrb8.js +3 -0
- package/dist/assets/miniChat-DU2jFhXq.js +2 -0
- package/dist/assets/mobile-B3lJ_CJR.js +2 -0
- package/dist/assets/renderElectronMiniChatApp-BfXYv3zl.js +2 -0
- package/dist/assets/renderMobileApp-Cd_F7yMe.js +14 -0
- package/dist/assets/{runtimeSurface-CkHYYg0E.js → runtimeSurface-UriLeO95.js} +1 -1
- package/dist/assets/{useInlineCommentController-DD31jQNC.js → useInlineCommentController-5XANmO1k.js} +2 -2
- package/dist/assets/usePanelSlide-CBZw2Cjo.js +56 -0
- package/dist/assets/useSwitcherItems-Cc6Tx-LO.js +1 -0
- package/dist/assets/{useWindowTitle-DoHrUC5r.js → useWindowTitle-Bf8ZNN_R.js} +1 -1
- package/dist/assets/{vendor-codemirror-language-data-Dp_Z-0GD.js → vendor-codemirror-language-data-DxpwplIe.js} +2 -2
- package/dist/assets/{vendor-katex-CHD9BdDO.js → vendor-katex-CNckGUMB.js} +1 -1
- package/dist/assets/{vendor-marked-i_gIkIP3.js → vendor-marked-CX41MkDF.js} +1 -1
- package/dist/assets/{web-components-Cct1r-wn.js → web-components-g7DKkne_.js} +4 -4
- package/dist/index.html +5 -5
- package/dist/mini-chat.html +4 -4
- package/dist/mobile.html +4 -4
- package/dist/sw.js +1 -1
- package/package.json +1 -1
- package/server/index.js +27 -5
- package/server/lib/fs/routes.js +0 -21
- package/server/lib/git/DOCUMENTATION.md +13 -3
- package/server/lib/git/routes.js +80 -4
- package/server/lib/git/routes.test.js +135 -0
- package/server/lib/git/service.js +130 -31
- package/server/lib/git/service.test.js +146 -0
- package/server/lib/pi/routes.js +67 -2
- package/server/lib/pi/routes.test.js +43 -3
- package/server/lib/pi/session-daemon/DOCUMENTATION.md +4 -4
- package/server/lib/pi/session-daemon/message-entry-aliases.js +53 -13
- package/server/lib/pi/session-daemon/message-entry-aliases.test.js +110 -0
- package/server/lib/pi/session-daemon/session-daemon.js +181 -22
- package/server/lib/pi/session-daemon/session-daemon.test.js +123 -3
- package/server/lib/pi/session-daemon/session-retry-limits.js +55 -0
- package/server/lib/pi/session-daemon/session-retry-limits.test.js +53 -0
- package/server/lib/pi/session-daemon/skill-read-classifier.js +39 -0
- package/server/lib/pi/session-daemon/skill-read-classifier.test.js +41 -0
- package/server/lib/pi/session-daemon/supervisor.js +3 -0
- package/server/lib/pi/small-model-generation.js +104 -0
- package/server/lib/pi/small-model-generation.test.js +66 -0
- package/server/lib/preview/proxy-runtime.js +11 -1
- package/server/lib/preview/proxy-runtime.test.js +3 -0
- package/server/lib/relay/tunnel-host.js +1 -0
- package/server/lib/stt/DOCUMENTATION.md +38 -0
- package/server/lib/stt/audio.js +47 -0
- package/server/lib/stt/config-store.js +81 -0
- package/server/lib/stt/config-store.test.js +36 -0
- package/server/lib/stt/local/model-catalog.js +57 -0
- package/server/lib/stt/local/model-downloader.js +127 -0
- package/server/lib/stt/local/recognizer.js +64 -0
- package/server/lib/stt/local/sherpa-loader.js +43 -0
- package/server/lib/stt/local/worker-client.js +115 -0
- package/server/lib/stt/local/worker-process.js +64 -0
- package/server/lib/stt/protocol.js +38 -0
- package/server/lib/stt/protocol.test.js +17 -0
- package/server/lib/stt/providers/local-worker-provider.js +11 -0
- package/server/lib/stt/providers/openai-compatible-provider.js +31 -0
- package/server/lib/stt/providers/openai-compatible-provider.test.js +31 -0
- package/server/lib/stt/runtime.js +111 -0
- package/server/lib/stt/service.js +106 -0
- package/server/lib/stt/stream-manager.js +188 -0
- package/server/lib/stt/stream-manager.test.js +64 -0
- package/server/lib/ui-auth/DOCUMENTATION.md +1 -1
- package/server/lib/ui-auth/ui-auth.js +8 -3
- package/server/lib/ui-auth/ui-auth.test.js +8 -0
- package/server/lib/workspace/host.js +18 -2
- package/dist/assets/CommitSection-BAYzLtBf.js +0 -1
- package/dist/assets/DiagramEditor-D1yc4joK.js +0 -1
- package/dist/assets/DiagramView-CAlJiG11.js +0 -1
- package/dist/assets/GitView-CFlPcSn9.js +0 -6
- package/dist/assets/JsonTreeView-CoX1ZscV.js +0 -1
- package/dist/assets/PatchDiff-D6Gs7K5B.js +0 -1
- package/dist/assets/PierreDiffViewer-COYd5Qr-.js +0 -136
- package/dist/assets/SettingsView-BYqvfMTS.js +0 -4
- package/dist/assets/SettingsWindow-CtNS0tQn.js +0 -1
- package/dist/assets/ToolPartDiffPreview-CBFC9min.js +0 -8
- package/dist/assets/collapsible-Kx4bmsQM.js +0 -1
- package/dist/assets/computeVirtualFileMetrics-DdOCbMyK.js +0 -3
- package/dist/assets/contextFileOpenGuard-BRldMp0d.js +0 -1
- package/dist/assets/index-CuSNEV7s.js +0 -1
- package/dist/assets/index-riABQmQ_.css +0 -1
- package/dist/assets/isDiffPlainText-CGIf0q8p.js +0 -1
- package/dist/assets/isFilePlainText-D4hgTRxT.js +0 -1
- package/dist/assets/main-By_pvk90.js +0 -2
- package/dist/assets/markdownCore-CAVCyFnI.js +0 -2
- package/dist/assets/miniChat-_XK2gGZE.js +0 -2
- package/dist/assets/mobile-B4E4JqAf.js +0 -2
- package/dist/assets/renderElectronMiniChatApp-Ca4hZgIz.js +0 -1
- package/dist/assets/renderMobileApp-ClA5Cnmw.js +0 -14
- package/dist/assets/usePanelSlide-a4c67FIY.js +0 -56
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { createWriteStream } from 'node:fs';
|
|
2
|
+
import { mkdir, readFile, rename, rm, stat, statfs, writeFile } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { spawn } from 'node:child_process';
|
|
5
|
+
import { Readable } from 'node:stream';
|
|
6
|
+
import { pipeline } from 'node:stream/promises';
|
|
7
|
+
import { randomUUID } from 'node:crypto';
|
|
8
|
+
|
|
9
|
+
import { getLocalSttModelDir, getLocalSttModelSpec } from './model-catalog.js';
|
|
10
|
+
import { sha256File } from '../audio.js';
|
|
11
|
+
|
|
12
|
+
const MANIFEST_FILE = '.pichamber-stt-model.json';
|
|
13
|
+
const MIN_FREE_BYTES_AFTER_DOWNLOAD = 512 * 1024 * 1024;
|
|
14
|
+
|
|
15
|
+
async function readManifest(modelDir) {
|
|
16
|
+
try { return JSON.parse(await readFile(path.join(modelDir, MANIFEST_FILE), 'utf8')); }
|
|
17
|
+
catch { return null; }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function fileSnapshot(modelDir, relativePath) {
|
|
21
|
+
const fullPath = path.join(modelDir, relativePath);
|
|
22
|
+
const info = await stat(fullPath);
|
|
23
|
+
if (!info.isFile() || info.size <= 0) throw new Error(`Missing model file: ${relativePath}`);
|
|
24
|
+
return { path: relativePath, size: info.size, sha256: await sha256File(fullPath) };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function inspectLocalSttModel(modelsDir, modelId, { verifyChecksums = false } = {}) {
|
|
28
|
+
const spec = getLocalSttModelSpec(modelId);
|
|
29
|
+
const modelDir = getLocalSttModelDir(modelsDir, modelId);
|
|
30
|
+
try {
|
|
31
|
+
const manifest = await readManifest(modelDir);
|
|
32
|
+
if (!manifest || manifest.modelId !== modelId || manifest.archiveSha256 !== spec.sha256 || !Array.isArray(manifest.files)) {
|
|
33
|
+
return { installed: false, corrupt: Boolean(manifest) };
|
|
34
|
+
}
|
|
35
|
+
const expected = new Map(manifest.files.map((entry) => [entry.path, entry]));
|
|
36
|
+
for (const relativePath of spec.requiredFiles) {
|
|
37
|
+
const entry = expected.get(relativePath);
|
|
38
|
+
if (!entry) return { installed: false, corrupt: true };
|
|
39
|
+
const fullPath = path.join(modelDir, relativePath);
|
|
40
|
+
const info = await stat(fullPath);
|
|
41
|
+
if (!info.isFile() || info.size !== entry.size) return { installed: false, corrupt: true };
|
|
42
|
+
if (verifyChecksums && await sha256File(fullPath) !== entry.sha256) return { installed: false, corrupt: true };
|
|
43
|
+
}
|
|
44
|
+
return { installed: true, corrupt: false };
|
|
45
|
+
} catch {
|
|
46
|
+
return { installed: false, corrupt: true };
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function assertDiskSpace(modelsDir, archiveBytes) {
|
|
51
|
+
await mkdir(modelsDir, { recursive: true, mode: 0o700 });
|
|
52
|
+
if (typeof statfs !== 'function') return;
|
|
53
|
+
const info = await statfs(modelsDir);
|
|
54
|
+
const free = Number(info.bavail) * Number(info.bsize);
|
|
55
|
+
const required = archiveBytes * 2 + MIN_FREE_BYTES_AFTER_DOWNLOAD;
|
|
56
|
+
if (Number.isFinite(free) && free < required) throw new Error(`Not enough disk space. At least ${Math.ceil(required / 1024 / 1024)} MB free is required.`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function downloadArchive(spec, archivePath, onProgress, signal) {
|
|
60
|
+
const temporary = `${archivePath}.tmp-${randomUUID()}`;
|
|
61
|
+
const response = await fetch(spec.archiveUrl, { redirect: 'follow', signal });
|
|
62
|
+
if (!response.ok || !response.body) throw new Error(`Model download failed with HTTP ${response.status}`);
|
|
63
|
+
const contentLength = Number.parseInt(response.headers.get('content-length') || '', 10);
|
|
64
|
+
if (Number.isFinite(contentLength) && contentLength !== spec.sizeBytes) throw new Error('Model archive size did not match the pinned catalog');
|
|
65
|
+
let downloaded = 0;
|
|
66
|
+
const stream = Readable.fromWeb(response.body);
|
|
67
|
+
stream.on('data', (chunk) => {
|
|
68
|
+
downloaded += chunk.byteLength;
|
|
69
|
+
onProgress?.(downloaded, spec.sizeBytes);
|
|
70
|
+
});
|
|
71
|
+
try {
|
|
72
|
+
await pipeline(stream, createWriteStream(temporary, { mode: 0o600 }));
|
|
73
|
+
if (downloaded !== spec.sizeBytes) throw new Error('Model archive download was incomplete');
|
|
74
|
+
if (await sha256File(temporary) !== spec.sha256) throw new Error('Model archive checksum verification failed');
|
|
75
|
+
await rename(temporary, archivePath);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
await rm(temporary, { force: true }).catch(() => {});
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function extractArchive(archivePath, destination) {
|
|
83
|
+
await mkdir(destination, { recursive: true, mode: 0o700 });
|
|
84
|
+
await new Promise((resolve, reject) => {
|
|
85
|
+
const child = spawn('tar', ['xf', archivePath, '-C', destination], { stdio: 'ignore', windowsHide: true });
|
|
86
|
+
child.once('error', reject);
|
|
87
|
+
child.once('exit', (code) => code === 0 ? resolve() : reject(new Error(`Model extraction failed with exit code ${code}`)));
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export async function ensureLocalSttModel({ modelsDir, modelId, onProgress, signal }) {
|
|
92
|
+
const spec = getLocalSttModelSpec(modelId);
|
|
93
|
+
const modelDir = getLocalSttModelDir(modelsDir, modelId);
|
|
94
|
+
const inspection = await inspectLocalSttModel(modelsDir, modelId);
|
|
95
|
+
if (inspection.installed) return modelDir;
|
|
96
|
+
await rm(modelDir, { recursive: true, force: true });
|
|
97
|
+
await assertDiskSpace(modelsDir, spec.sizeBytes);
|
|
98
|
+
|
|
99
|
+
const downloadsDir = path.join(modelsDir, '.downloads');
|
|
100
|
+
await mkdir(downloadsDir, { recursive: true, mode: 0o700 });
|
|
101
|
+
const archivePath = path.join(downloadsDir, `${modelId}-${spec.sha256}.tar.bz2`);
|
|
102
|
+
try {
|
|
103
|
+
const archiveInfo = await stat(archivePath).catch(() => null);
|
|
104
|
+
if (!archiveInfo || archiveInfo.size !== spec.sizeBytes || await sha256File(archivePath) !== spec.sha256) {
|
|
105
|
+
await rm(archivePath, { force: true });
|
|
106
|
+
await downloadArchive(spec, archivePath, onProgress, signal);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const stagingRoot = path.join(modelsDir, `.staging-${modelId}-${randomUUID()}`);
|
|
110
|
+
try {
|
|
111
|
+
await extractArchive(archivePath, stagingRoot);
|
|
112
|
+
const stagedModelDir = path.join(stagingRoot, spec.extractedDir);
|
|
113
|
+
const files = [];
|
|
114
|
+
for (const relativePath of spec.requiredFiles) files.push(await fileSnapshot(stagedModelDir, relativePath));
|
|
115
|
+
await writeFile(path.join(stagedModelDir, MANIFEST_FILE), `${JSON.stringify({ version: 1, modelId, archiveSha256: spec.sha256, files }, null, 2)}\n`, { mode: 0o600 });
|
|
116
|
+
await rename(stagedModelDir, modelDir);
|
|
117
|
+
} finally {
|
|
118
|
+
await rm(stagingRoot, { recursive: true, force: true });
|
|
119
|
+
}
|
|
120
|
+
await rm(archivePath, { force: true });
|
|
121
|
+
return modelDir;
|
|
122
|
+
} catch (error) {
|
|
123
|
+
await rm(archivePath, { force: true }).catch(() => {});
|
|
124
|
+
await rm(modelDir, { recursive: true, force: true }).catch(() => {});
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { pcm16Peak, pcm16ToFloat32, PCM_SAMPLE_RATE } from '../audio.js';
|
|
5
|
+
import { getLocalSttModelDir, getLocalSttModelSpec } from './model-catalog.js';
|
|
6
|
+
import { loadSherpaOnnxNode } from './sherpa-loader.js';
|
|
7
|
+
|
|
8
|
+
const required = (value, label) => {
|
|
9
|
+
if (!existsSync(value)) throw new Error(`Missing ${label}`);
|
|
10
|
+
return value;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export class SherpaRecognizer {
|
|
14
|
+
constructor({ modelsDir, modelId, language, numThreads = 2 }) {
|
|
15
|
+
const spec = getLocalSttModelSpec(modelId);
|
|
16
|
+
const directory = getLocalSttModelDir(modelsDir, modelId);
|
|
17
|
+
const modelConfig = spec.type === 'whisper'
|
|
18
|
+
? {
|
|
19
|
+
whisper: {
|
|
20
|
+
encoder: required(path.join(directory, spec.files.encoder), 'Whisper encoder'),
|
|
21
|
+
decoder: required(path.join(directory, spec.files.decoder), 'Whisper decoder'),
|
|
22
|
+
language: typeof language === 'string' ? language : '',
|
|
23
|
+
task: 'transcribe',
|
|
24
|
+
tailPaddings: -1,
|
|
25
|
+
},
|
|
26
|
+
tokens: required(path.join(directory, spec.files.tokens), 'tokens'),
|
|
27
|
+
modelType: 'whisper', numThreads, provider: 'cpu', debug: 0,
|
|
28
|
+
}
|
|
29
|
+
: {
|
|
30
|
+
transducer: {
|
|
31
|
+
encoder: required(path.join(directory, spec.files.encoder), 'Parakeet encoder'),
|
|
32
|
+
decoder: required(path.join(directory, spec.files.decoder), 'Parakeet decoder'),
|
|
33
|
+
joiner: required(path.join(directory, spec.files.joiner), 'Parakeet joiner'),
|
|
34
|
+
},
|
|
35
|
+
tokens: required(path.join(directory, spec.files.tokens), 'tokens'),
|
|
36
|
+
modelType: 'nemo_transducer', numThreads, provider: 'cpu', debug: 0,
|
|
37
|
+
};
|
|
38
|
+
const sherpa = loadSherpaOnnxNode();
|
|
39
|
+
this.recognizer = new sherpa.OfflineRecognizer({
|
|
40
|
+
featConfig: { sampleRate: PCM_SAMPLE_RATE, featureDim: 80 },
|
|
41
|
+
modelConfig,
|
|
42
|
+
decodingMethod: 'greedy_search',
|
|
43
|
+
maxActivePaths: 4,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
transcribe(pcm16, sampleRate) {
|
|
48
|
+
if (sampleRate !== PCM_SAMPLE_RATE) throw new Error(`Unsupported STT sample rate: ${sampleRate}`);
|
|
49
|
+
if (!pcm16.byteLength) return '';
|
|
50
|
+
const peak = pcm16Peak(pcm16);
|
|
51
|
+
const gain = peak > 0 && peak < 19_660 ? Math.min(50, 19_660 / peak) : 1;
|
|
52
|
+
const stream = this.recognizer.createStream();
|
|
53
|
+
try {
|
|
54
|
+
const samples = pcm16ToFloat32(pcm16, gain);
|
|
55
|
+
if (stream.acceptWaveform.length <= 1) stream.acceptWaveform({ samples, sampleRate });
|
|
56
|
+
else stream.acceptWaveform(sampleRate, samples);
|
|
57
|
+
this.recognizer.decode(stream);
|
|
58
|
+
const result = this.recognizer.getResult(stream);
|
|
59
|
+
return String(result && typeof result === 'object' ? result.text ?? '' : result ?? '').trim();
|
|
60
|
+
} finally { stream.free?.(); }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
free() { this.recognizer?.free?.(); }
|
|
64
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
let cached;
|
|
7
|
+
|
|
8
|
+
const platformPackage = () => `sherpa-onnx-${process.platform === 'win32' ? 'win' : process.platform}-${process.arch}`;
|
|
9
|
+
const loaderKey = () => process.platform === 'linux' ? 'LD_LIBRARY_PATH' : process.platform === 'darwin' ? 'DYLD_LIBRARY_PATH' : process.platform === 'win32' ? 'PATH' : null;
|
|
10
|
+
|
|
11
|
+
function resolveLibraryDirectory() {
|
|
12
|
+
try {
|
|
13
|
+
const directory = path.dirname(require.resolve(`${platformPackage()}/package.json`));
|
|
14
|
+
const unpacked = directory.replace(`app.asar${path.sep}`, `app.asar.unpacked${path.sep}`);
|
|
15
|
+
return existsSync(unpacked) ? unpacked : directory;
|
|
16
|
+
} catch { return null; }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function applySherpaLoaderEnv(env) {
|
|
20
|
+
const key = loaderKey();
|
|
21
|
+
const directory = resolveLibraryDirectory();
|
|
22
|
+
if (!key || !directory) return;
|
|
23
|
+
const actualKey = Object.keys(env).find((entry) => entry.toLowerCase() === key.toLowerCase()) ?? key;
|
|
24
|
+
const values = String(env[actualKey] || '').split(path.delimiter).filter(Boolean);
|
|
25
|
+
if (!values.includes(directory)) env[actualKey] = [directory, ...values].join(path.delimiter);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function loadSherpaOnnxNode() {
|
|
29
|
+
if (cached) return cached;
|
|
30
|
+
try { cached = require('sherpa-onnx-node'); return cached; }
|
|
31
|
+
catch (firstError) {
|
|
32
|
+
const directory = resolveLibraryDirectory();
|
|
33
|
+
if (directory) {
|
|
34
|
+
applySherpaLoaderEnv(process.env);
|
|
35
|
+
const addon = path.join(directory, 'sherpa-onnx.node');
|
|
36
|
+
if (existsSync(addon)) {
|
|
37
|
+
try { cached = require(addon); return cached; }
|
|
38
|
+
catch { /* report the original package failure below */ }
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
throw new Error(`Failed to load sherpa-onnx-node for ${process.platform}-${process.arch}: ${firstError?.message || firstError}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { fork } from 'node:child_process';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
|
|
5
|
+
import { applySherpaLoaderEnv } from './sherpa-loader.js';
|
|
6
|
+
|
|
7
|
+
const IDLE_SHUTDOWN_MS = 5 * 60 * 1000;
|
|
8
|
+
const REQUEST_TIMEOUT_MS = 5 * 60 * 1000;
|
|
9
|
+
|
|
10
|
+
export class SttWorkerClient {
|
|
11
|
+
constructor({ idleShutdownMs = IDLE_SHUTDOWN_MS, requestTimeoutMs = REQUEST_TIMEOUT_MS } = {}) {
|
|
12
|
+
this.idleShutdownMs = idleShutdownMs;
|
|
13
|
+
this.requestTimeoutMs = requestTimeoutMs;
|
|
14
|
+
this.worker = null;
|
|
15
|
+
this.pending = new Map();
|
|
16
|
+
this.queue = Promise.resolve();
|
|
17
|
+
this.idleTimer = null;
|
|
18
|
+
this.stderrTail = '';
|
|
19
|
+
this.intentional = new WeakSet();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
transcribe(input) {
|
|
23
|
+
const operation = this.queue.catch(() => {}).then(() => this.request({ type: 'transcribe', ...input }));
|
|
24
|
+
this.queue = operation;
|
|
25
|
+
return operation;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
request(message) {
|
|
29
|
+
const worker = this.ensureWorker();
|
|
30
|
+
const requestId = randomUUID();
|
|
31
|
+
clearTimeout(this.idleTimer);
|
|
32
|
+
this.idleTimer = null;
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
const timeout = setTimeout(() => {
|
|
35
|
+
this.pending.delete(requestId);
|
|
36
|
+
reject(new Error('STT worker timed out'));
|
|
37
|
+
this.scheduleIdleShutdown();
|
|
38
|
+
}, this.requestTimeoutMs);
|
|
39
|
+
this.pending.set(requestId, { resolve, reject, timeout });
|
|
40
|
+
worker.send({ ...message, requestId }, (error) => {
|
|
41
|
+
if (!error) return;
|
|
42
|
+
const pending = this.pending.get(requestId);
|
|
43
|
+
if (!pending) return;
|
|
44
|
+
clearTimeout(pending.timeout);
|
|
45
|
+
this.pending.delete(requestId);
|
|
46
|
+
pending.reject(error);
|
|
47
|
+
this.scheduleIdleShutdown();
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
ensureWorker() {
|
|
53
|
+
if (this.worker?.connected && !this.worker.killed) return this.worker;
|
|
54
|
+
const env = { ...process.env };
|
|
55
|
+
applySherpaLoaderEnv(env);
|
|
56
|
+
const worker = fork(fileURLToPath(new URL('./worker-process.js', import.meta.url)), [], {
|
|
57
|
+
env,
|
|
58
|
+
execArgv: process.execArgv.filter((argument) => !argument.startsWith('--input-type')),
|
|
59
|
+
serialization: 'advanced',
|
|
60
|
+
stdio: ['ignore', 'ignore', 'pipe', 'ipc'],
|
|
61
|
+
windowsHide: true,
|
|
62
|
+
});
|
|
63
|
+
this.worker = worker;
|
|
64
|
+
this.stderrTail = '';
|
|
65
|
+
worker.stderr?.on('data', (chunk) => { this.stderrTail = `${this.stderrTail}${chunk}`.slice(-2000); });
|
|
66
|
+
worker.on('message', (message) => this.handleMessage(message));
|
|
67
|
+
worker.on('close', (code, signal) => this.handleExit(worker, code, signal));
|
|
68
|
+
return worker;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
handleMessage(message) {
|
|
72
|
+
if (message?.type !== 'response') return;
|
|
73
|
+
const pending = this.pending.get(message.requestId);
|
|
74
|
+
if (!pending) return;
|
|
75
|
+
clearTimeout(pending.timeout);
|
|
76
|
+
this.pending.delete(message.requestId);
|
|
77
|
+
if (message.ok) pending.resolve(message.result);
|
|
78
|
+
else pending.reject(new Error(message.error || 'STT worker failed'));
|
|
79
|
+
this.scheduleIdleShutdown();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
handleExit(worker, code, signal) {
|
|
83
|
+
if (this.worker !== worker) return;
|
|
84
|
+
this.worker = null;
|
|
85
|
+
if (this.intentional.has(worker)) return;
|
|
86
|
+
const detail = this.stderrTail.trim().slice(-500);
|
|
87
|
+
const error = new Error(`STT worker exited (code ${code ?? 'null'}${signal ? `, signal ${signal}` : ''})${detail ? `: ${detail}` : ''}`);
|
|
88
|
+
for (const pending of this.pending.values()) { clearTimeout(pending.timeout); pending.reject(error); }
|
|
89
|
+
this.pending.clear();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
scheduleIdleShutdown() {
|
|
93
|
+
if (this.pending.size || !this.worker) return;
|
|
94
|
+
clearTimeout(this.idleTimer);
|
|
95
|
+
this.idleTimer = setTimeout(() => { if (!this.pending.size) this.shutdownWorker(); }, this.idleShutdownMs);
|
|
96
|
+
this.idleTimer.unref?.();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
shutdownWorker() {
|
|
100
|
+
const worker = this.worker;
|
|
101
|
+
this.worker = null;
|
|
102
|
+
if (!worker) return;
|
|
103
|
+
this.intentional.add(worker);
|
|
104
|
+
try { worker.disconnect(); } catch {}
|
|
105
|
+
const timer = setTimeout(() => { try { worker.kill(); } catch {} }, 1000);
|
|
106
|
+
timer.unref?.();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
shutdown() {
|
|
110
|
+
clearTimeout(this.idleTimer);
|
|
111
|
+
for (const pending of this.pending.values()) { clearTimeout(pending.timeout); pending.reject(new Error('STT worker shut down')); }
|
|
112
|
+
this.pending.clear();
|
|
113
|
+
this.shutdownWorker();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { SherpaRecognizer } from './recognizer.js';
|
|
2
|
+
|
|
3
|
+
process.title = 'PiChamber STT Worker';
|
|
4
|
+
|
|
5
|
+
const MAX_ENGINES = 2;
|
|
6
|
+
const engines = new Map();
|
|
7
|
+
let work = Promise.resolve();
|
|
8
|
+
let closing = false;
|
|
9
|
+
|
|
10
|
+
const send = (message) => {
|
|
11
|
+
if (closing || !process.connected || !process.send) return;
|
|
12
|
+
try { process.send(message, (error) => { if (error) closing = true; }); }
|
|
13
|
+
catch { closing = true; }
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const toBuffer = (value) => {
|
|
17
|
+
if (Buffer.isBuffer(value)) return value;
|
|
18
|
+
if (value instanceof Uint8Array) return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
19
|
+
throw new Error('Invalid PCM16 worker payload');
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function engineFor(message) {
|
|
23
|
+
const language = typeof message.language === 'string' ? message.language.trim().toLowerCase() : '';
|
|
24
|
+
const key = `${message.modelsDir}\u0000${message.modelId}\u0000${language}`;
|
|
25
|
+
const existing = engines.get(key);
|
|
26
|
+
if (existing) {
|
|
27
|
+
engines.delete(key);
|
|
28
|
+
engines.set(key, existing);
|
|
29
|
+
return { engine: existing, modelLoadMs: 0 };
|
|
30
|
+
}
|
|
31
|
+
const loadStartedAt = performance.now();
|
|
32
|
+
const engine = new SherpaRecognizer({ modelsDir: message.modelsDir, modelId: message.modelId, language });
|
|
33
|
+
engines.set(key, engine);
|
|
34
|
+
while (engines.size > MAX_ENGINES) {
|
|
35
|
+
const oldestKey = engines.keys().next().value;
|
|
36
|
+
const oldest = engines.get(oldestKey);
|
|
37
|
+
engines.delete(oldestKey);
|
|
38
|
+
oldest?.free();
|
|
39
|
+
}
|
|
40
|
+
return { engine, modelLoadMs: Math.round(performance.now() - loadStartedAt) };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function handle(message) {
|
|
44
|
+
if (message?.type !== 'transcribe') throw new Error('Unknown STT worker request');
|
|
45
|
+
const loaded = engineFor(message);
|
|
46
|
+
const startedAt = performance.now();
|
|
47
|
+
const text = loaded.engine.transcribe(toBuffer(message.pcm16), message.sampleRate);
|
|
48
|
+
return { text, modelLoadMs: loaded.modelLoadMs, inferenceMs: Math.round(performance.now() - startedAt), rssBytes: process.memoryUsage().rss };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
process.on('message', (message) => {
|
|
52
|
+
work = work.then(async () => {
|
|
53
|
+
try { send({ type: 'response', requestId: message?.requestId, ok: true, result: await handle(message) }); }
|
|
54
|
+
catch (error) { send({ type: 'response', requestId: message?.requestId, ok: false, error: error?.message || 'STT inference failed' }); }
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
process.once('disconnect', () => {
|
|
59
|
+
closing = true;
|
|
60
|
+
void work.finally(() => {
|
|
61
|
+
for (const engine of engines.values()) engine.free();
|
|
62
|
+
process.exit(0);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export const STT_WS_PATH = '/api/stt/ws';
|
|
2
|
+
const STT_BINARY_HEADER_BYTES = 4;
|
|
3
|
+
const STT_MAX_CONTROL_BYTES = 16 * 1024;
|
|
4
|
+
export const STT_MAX_BINARY_FRAME_BYTES = 32 * 1024;
|
|
5
|
+
|
|
6
|
+
export function parseSttControlFrame(raw) {
|
|
7
|
+
const buffer = Buffer.isBuffer(raw) ? raw : Buffer.from(raw);
|
|
8
|
+
if (buffer.byteLength > STT_MAX_CONTROL_BYTES) throw new Error('STT control frame is too large');
|
|
9
|
+
const message = JSON.parse(buffer.toString('utf8'));
|
|
10
|
+
if (!message || typeof message !== 'object' || Array.isArray(message) || typeof message.type !== 'string') {
|
|
11
|
+
throw new Error('Invalid STT control frame');
|
|
12
|
+
}
|
|
13
|
+
return message;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function parseSttAudioFrame(raw) {
|
|
17
|
+
const frame = Buffer.isBuffer(raw) ? raw : Buffer.from(raw);
|
|
18
|
+
if (frame.byteLength < STT_BINARY_HEADER_BYTES || frame.byteLength > STT_MAX_BINARY_FRAME_BYTES) {
|
|
19
|
+
throw new Error('Invalid STT audio frame size');
|
|
20
|
+
}
|
|
21
|
+
const sequence = frame.readUInt32LE(0);
|
|
22
|
+
const pcm16 = frame.subarray(STT_BINARY_HEADER_BYTES);
|
|
23
|
+
if (pcm16.byteLength === 0 || pcm16.byteLength % 2 !== 0) throw new Error('Invalid PCM16 audio frame');
|
|
24
|
+
return { sequence, pcm16 };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function createSttAudioFrame(sequence, pcm16) {
|
|
28
|
+
if (!Number.isInteger(sequence) || sequence < 0 || sequence > 0xffffffff) throw new Error('Invalid STT sequence');
|
|
29
|
+
const frame = new Uint8Array(STT_BINARY_HEADER_BYTES + pcm16.byteLength);
|
|
30
|
+
new DataView(frame.buffer).setUint32(0, sequence, true);
|
|
31
|
+
frame.set(new Uint8Array(pcm16.buffer, pcm16.byteOffset, pcm16.byteLength), STT_BINARY_HEADER_BYTES);
|
|
32
|
+
return frame;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function parseRequestPathname(value) {
|
|
36
|
+
try { return new URL(value, 'http://localhost').pathname; }
|
|
37
|
+
catch { return typeof value === 'string' ? value.split('?')[0] : ''; }
|
|
38
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import { createSttAudioFrame, parseSttAudioFrame, parseSttControlFrame } from './protocol.js';
|
|
4
|
+
|
|
5
|
+
describe('STT protocol', () => {
|
|
6
|
+
test('round trips binary PCM frames without base64', () => {
|
|
7
|
+
const pcm = new Uint8Array([1, 2, 3, 4]);
|
|
8
|
+
const parsed = parseSttAudioFrame(createSttAudioFrame(42, pcm));
|
|
9
|
+
expect(parsed.sequence).toBe(42);
|
|
10
|
+
expect([...parsed.pcm16]).toEqual([...pcm]);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test('rejects odd PCM payloads and oversized controls', () => {
|
|
14
|
+
expect(() => parseSttAudioFrame(createSttAudioFrame(0, new Uint8Array([1])))).toThrow('PCM16');
|
|
15
|
+
expect(() => parseSttControlFrame(Buffer.from(`{"type":"start","padding":"${'x'.repeat(20_000)}"}`))).toThrow('too large');
|
|
16
|
+
});
|
|
17
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { PCM_SAMPLE_RATE } from '../audio.js';
|
|
2
|
+
|
|
3
|
+
export function createLocalWorkerProvider({ workerClient, modelsDir, modelId, language }) {
|
|
4
|
+
return {
|
|
5
|
+
sampleRate: PCM_SAMPLE_RATE,
|
|
6
|
+
async transcribe(pcm16) {
|
|
7
|
+
return workerClient.transcribe({ pcm16, sampleRate: PCM_SAMPLE_RATE, modelsDir, modelId, language });
|
|
8
|
+
},
|
|
9
|
+
close() {},
|
|
10
|
+
};
|
|
11
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import OpenAI, { toFile } from 'openai';
|
|
2
|
+
|
|
3
|
+
import { PCM_SAMPLE_RATE, pcm16ToWav } from '../audio.js';
|
|
4
|
+
|
|
5
|
+
function normalizeBaseUrl(value) {
|
|
6
|
+
if (typeof value !== 'string' || !value.trim()) throw new Error('STT provider URL is not configured');
|
|
7
|
+
const parsed = new URL(value.trim());
|
|
8
|
+
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') throw new Error('STT provider URL must use HTTP or HTTPS');
|
|
9
|
+
parsed.pathname = parsed.pathname.replace(/\/+$/, '');
|
|
10
|
+
if (!parsed.pathname.endsWith('/v1')) parsed.pathname = `${parsed.pathname}/v1`.replace(/\/+/g, '/');
|
|
11
|
+
return parsed.toString().replace(/\/$/, '');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function createOpenAICompatibleProvider({ baseUrl, apiKey, model, language }) {
|
|
15
|
+
if (typeof model !== 'string' || !model.trim()) throw new Error('STT provider model is not configured');
|
|
16
|
+
const client = new OpenAI({ baseURL: normalizeBaseUrl(baseUrl), apiKey: apiKey || 'not-required' });
|
|
17
|
+
return {
|
|
18
|
+
sampleRate: PCM_SAMPLE_RATE,
|
|
19
|
+
async transcribe(pcm16) {
|
|
20
|
+
const file = await toFile(pcm16ToWav(pcm16), 'recording.wav', { type: 'audio/wav' });
|
|
21
|
+
const result = await client.audio.transcriptions.create({
|
|
22
|
+
file,
|
|
23
|
+
model: model.trim(),
|
|
24
|
+
response_format: 'json',
|
|
25
|
+
...(language ? { language } : {}),
|
|
26
|
+
});
|
|
27
|
+
return { text: String(result.text ?? '').trim() };
|
|
28
|
+
},
|
|
29
|
+
close() {},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test } from 'bun:test';
|
|
2
|
+
import http from 'node:http';
|
|
3
|
+
|
|
4
|
+
import { createOpenAICompatibleProvider } from './openai-compatible-provider.js';
|
|
5
|
+
|
|
6
|
+
const servers = [];
|
|
7
|
+
afterEach(async () => Promise.all(servers.splice(0).map((server) => new Promise((resolve) => server.close(resolve)))));
|
|
8
|
+
|
|
9
|
+
describe('OpenAI-compatible STT provider', () => {
|
|
10
|
+
test('sends a WAV multipart request to the configured transcription endpoint', async () => {
|
|
11
|
+
let requestSnapshot;
|
|
12
|
+
const server = http.createServer(async (request, response) => {
|
|
13
|
+
const chunks = [];
|
|
14
|
+
for await (const chunk of request) chunks.push(chunk);
|
|
15
|
+
requestSnapshot = { url: request.url, authorization: request.headers.authorization, contentType: request.headers['content-type'], body: Buffer.concat(chunks) };
|
|
16
|
+
response.writeHead(200, { 'content-type': 'application/json' });
|
|
17
|
+
response.end(JSON.stringify({ text: 'remote transcript' }));
|
|
18
|
+
});
|
|
19
|
+
servers.push(server);
|
|
20
|
+
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
|
|
21
|
+
const address = server.address();
|
|
22
|
+
const provider = createOpenAICompatibleProvider({ baseUrl: `http://127.0.0.1:${address.port}`, apiKey: 'server-secret', model: 'whisper-test', language: 'en' });
|
|
23
|
+
const result = await provider.transcribe(Buffer.alloc(3200));
|
|
24
|
+
expect(result).toEqual({ text: 'remote transcript' });
|
|
25
|
+
expect(requestSnapshot.url).toBe('/v1/audio/transcriptions');
|
|
26
|
+
expect(requestSnapshot.authorization).toBe('Bearer server-secret');
|
|
27
|
+
expect(requestSnapshot.contentType).toStartWith('multipart/form-data; boundary=');
|
|
28
|
+
expect(requestSnapshot.body.includes(Buffer.from('RIFF'))).toBe(true);
|
|
29
|
+
expect(requestSnapshot.body.includes(Buffer.from('whisper-test'))).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { WebSocketServer } from 'ws';
|
|
2
|
+
|
|
3
|
+
import { STT_MAX_BINARY_FRAME_BYTES, STT_WS_PATH, parseRequestPathname, parseSttAudioFrame, parseSttControlFrame } from './protocol.js';
|
|
4
|
+
import { createSttService } from './service.js';
|
|
5
|
+
import { SttStreamManager } from './stream-manager.js';
|
|
6
|
+
|
|
7
|
+
const HEARTBEAT_MS = 30_000;
|
|
8
|
+
|
|
9
|
+
export function createSttRuntime({ app, server, express, uiAuthController, isRequestOriginAllowed, rejectWebSocketUpgrade, modelsDir, configFile }) {
|
|
10
|
+
const service = createSttService({ modelsDir, configFile });
|
|
11
|
+
const manager = new SttStreamManager({ createTranscriber: (providerConfigId) => service.createTranscriber(providerConfigId) });
|
|
12
|
+
|
|
13
|
+
app.get('/api/stt/status', async (_req, res) => {
|
|
14
|
+
try { res.json(await service.getStatus()); }
|
|
15
|
+
catch (error) { res.status(500).json({ error: error?.message || 'Failed to read STT status' }); }
|
|
16
|
+
});
|
|
17
|
+
app.put('/api/stt/config', express.json({ limit: '32kb' }), async (req, res) => {
|
|
18
|
+
try { res.json({ config: await service.updateConfig(req.body ?? {}) }); }
|
|
19
|
+
catch (error) { res.status(400).json({ error: error?.message || 'Invalid STT configuration' }); }
|
|
20
|
+
});
|
|
21
|
+
app.post('/api/stt/models/:modelId/download', async (req, res) => {
|
|
22
|
+
try { res.status(202).json(await service.requestModelDownload(req.params.modelId)); }
|
|
23
|
+
catch (error) { res.status(400).json({ error: error?.message || 'Failed to download STT model' }); }
|
|
24
|
+
});
|
|
25
|
+
app.delete('/api/stt/models/:modelId', async (req, res) => {
|
|
26
|
+
try { res.json(await service.deleteModel(req.params.modelId)); }
|
|
27
|
+
catch (error) { res.status(400).json({ error: error?.message || 'Failed to delete STT model' }); }
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
let wsServer = new WebSocketServer({ noServer: true, maxPayload: STT_MAX_BINARY_FRAME_BYTES });
|
|
31
|
+
wsServer.on('connection', (socket) => {
|
|
32
|
+
let recordingId = null;
|
|
33
|
+
let starting = false;
|
|
34
|
+
let closed = false;
|
|
35
|
+
const emit = (message) => {
|
|
36
|
+
if (socket.readyState === 1) socket.send(JSON.stringify(message));
|
|
37
|
+
};
|
|
38
|
+
emit({ version: 1, type: 'hello' });
|
|
39
|
+
const heartbeat = setInterval(() => { try { socket.ping(); } catch {} }, HEARTBEAT_MS);
|
|
40
|
+
heartbeat.unref?.();
|
|
41
|
+
|
|
42
|
+
socket.on('message', (raw, isBinary) => {
|
|
43
|
+
try {
|
|
44
|
+
if (isBinary) {
|
|
45
|
+
if (!recordingId) throw new Error('Start the STT recording before sending audio');
|
|
46
|
+
const frame = parseSttAudioFrame(raw);
|
|
47
|
+
manager.append(recordingId, frame.sequence, frame.pcm16);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const message = parseSttControlFrame(raw);
|
|
51
|
+
if (message.type === 'ping') { emit({ version: 1, type: 'pong' }); return; }
|
|
52
|
+
if (message.type === 'start') {
|
|
53
|
+
if (starting) throw new Error('STT recording is already starting');
|
|
54
|
+
if (recordingId && recordingId !== message.recordingId) throw new Error('Only one recording is allowed per connection');
|
|
55
|
+
recordingId = message.recordingId;
|
|
56
|
+
starting = true;
|
|
57
|
+
void manager.start({ recordingId, providerConfigId: message.providerConfigId, emit }).then(() => {
|
|
58
|
+
starting = false;
|
|
59
|
+
if (closed && recordingId) manager.detach(recordingId, emit);
|
|
60
|
+
}).catch((error) => {
|
|
61
|
+
starting = false;
|
|
62
|
+
emit({ version: 1, type: 'error', recordingId, code: error?.code || 'STT_START_FAILED', message: error?.message || 'Failed to start STT' });
|
|
63
|
+
recordingId = null;
|
|
64
|
+
});
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (!recordingId || message.recordingId !== recordingId) throw new Error('Invalid STT recording id');
|
|
68
|
+
if (message.type === 'finish') { manager.finish(recordingId, message.finalSequence); return; }
|
|
69
|
+
if (message.type === 'cancel') { manager.cancel(recordingId); recordingId = null; return; }
|
|
70
|
+
throw new Error('Unknown STT control frame');
|
|
71
|
+
} catch (error) {
|
|
72
|
+
emit({ version: 1, type: 'error', recordingId, code: error?.code || 'BAD_FRAME', message: error?.message || 'Invalid STT frame' });
|
|
73
|
+
if (error?.code === 'RECORDING_LIMIT' && recordingId) { manager.cancel(recordingId); recordingId = null; }
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
const detach = () => {
|
|
77
|
+
closed = true;
|
|
78
|
+
clearInterval(heartbeat);
|
|
79
|
+
if (recordingId) manager.detach(recordingId, emit);
|
|
80
|
+
};
|
|
81
|
+
socket.once('close', detach);
|
|
82
|
+
socket.on('error', () => {});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const upgradeHandler = (req, socket, head) => {
|
|
86
|
+
if (parseRequestPathname(req.url) !== STT_WS_PATH) return;
|
|
87
|
+
void (async () => {
|
|
88
|
+
try {
|
|
89
|
+
if (uiAuthController?.enabled) {
|
|
90
|
+
if (!await uiAuthController.ensureSessionToken(req, null)) { rejectWebSocketUpgrade(socket, 401, 'UI authentication required'); return; }
|
|
91
|
+
if (!await isRequestOriginAllowed(req)) { rejectWebSocketUpgrade(socket, 403, 'Invalid origin'); return; }
|
|
92
|
+
}
|
|
93
|
+
if (!wsServer) { rejectWebSocketUpgrade(socket, 503, 'STT WebSocket unavailable'); return; }
|
|
94
|
+
wsServer.handleUpgrade(req, socket, head, (ws) => wsServer.emit('connection', ws, req));
|
|
95
|
+
} catch { rejectWebSocketUpgrade(socket, 500, 'Upgrade failed'); }
|
|
96
|
+
})();
|
|
97
|
+
};
|
|
98
|
+
server.on('upgrade', upgradeHandler);
|
|
99
|
+
|
|
100
|
+
const shutdown = async () => {
|
|
101
|
+
server.off('upgrade', upgradeHandler);
|
|
102
|
+
manager.shutdown();
|
|
103
|
+
service.shutdown();
|
|
104
|
+
if (!wsServer) return;
|
|
105
|
+
for (const client of wsServer.clients) client.terminate();
|
|
106
|
+
const current = wsServer;
|
|
107
|
+
wsServer = null;
|
|
108
|
+
await Promise.race([new Promise((resolve) => current.close(resolve)), new Promise((resolve) => setTimeout(resolve, 1000))]);
|
|
109
|
+
};
|
|
110
|
+
return { shutdown };
|
|
111
|
+
}
|