@nativestudio/local-runtime 0.0.0-stage → 0.3.0-rc.25
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/LICENSE +201 -0
- package/dist/BUNDLED_DEPENDENCIES.json +10 -0
- package/dist/THIRD_PARTY_NOTICES.md +2103 -0
- package/dist/agent-supervisor.mjs +95 -0
- package/dist/capture.mjs +3199 -0
- package/dist/entry.js +57572 -0
- package/dist/execution-identity.json +1 -0
- package/dist/hyperframe-dependencies.json +1 -0
- package/dist/hyperframe-gsap.js +11 -0
- package/dist/hyperframe-notices.txt +2530 -0
- package/dist/hyperframe.manifest.json +22 -0
- package/dist/hyperframe.runtime.iife.js +1081 -0
- package/dist/kokoro-worker.mjs +89 -0
- package/dist/native/helper.exe +0 -0
- package/dist/native/helper.json +1 -0
- package/dist/parent-lifetime.mjs +42 -0
- package/dist/supervisor.mjs +89 -0
- package/package.json +10 -3
- package/README.md +0 -3
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { constants } from "node:fs";
|
|
2
|
+
import { writeFile, open } from "node:fs/promises";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { performance } from "node:perf_hooks";
|
|
6
|
+
|
|
7
|
+
// 固定子进程桥接:文本只从私有文件读取,不放进 argv、进度或失败日志。
|
|
8
|
+
async function main() {
|
|
9
|
+
const file = await open(process.argv[2], constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
10
|
+
let raw;
|
|
11
|
+
try {
|
|
12
|
+
const stat = await file.stat();
|
|
13
|
+
if (!stat.isFile() || stat.nlink !== 1 || stat.size > 32768 || stat.mode & 0o077) throw Error();
|
|
14
|
+
raw = JSON.parse(await file.readFile("utf8"));
|
|
15
|
+
} finally {
|
|
16
|
+
await file.close();
|
|
17
|
+
}
|
|
18
|
+
const { engineDirectory, modelDirectory, request, output } = raw;
|
|
19
|
+
const require = createRequire(
|
|
20
|
+
join(engineDirectory, "node_modules/sherpa-onnx-node/package.json"),
|
|
21
|
+
);
|
|
22
|
+
const engine = require(
|
|
23
|
+
join(engineDirectory, "node_modules/sherpa-onnx-node/non-streaming-tts.js"),
|
|
24
|
+
);
|
|
25
|
+
const modelFile = (name) => join(modelDirectory, name);
|
|
26
|
+
const began = performance.now();
|
|
27
|
+
const tts = new engine.OfflineTts({
|
|
28
|
+
model: {
|
|
29
|
+
kokoro: {
|
|
30
|
+
model: modelFile("model.int8.onnx"),
|
|
31
|
+
voices: modelFile("voices.bin"),
|
|
32
|
+
tokens: modelFile("tokens.txt"),
|
|
33
|
+
lexicon: ["lexicon-us-en.txt", "lexicon-zh.txt"].map(modelFile).join(","),
|
|
34
|
+
dataDir: modelFile("espeak-ng-data"),
|
|
35
|
+
dictDir: modelFile("dict"),
|
|
36
|
+
},
|
|
37
|
+
numThreads: 1,
|
|
38
|
+
provider: "cpu",
|
|
39
|
+
debug: false,
|
|
40
|
+
},
|
|
41
|
+
maxNumSentences: 1,
|
|
42
|
+
});
|
|
43
|
+
const loaded = performance.now();
|
|
44
|
+
if (tts.sampleRate !== 24000 || tts.numSpeakers !== 103) throw Error();
|
|
45
|
+
// 本适配器等待完整生成结果;内部逐句算法不意味着对调用方流式交付。
|
|
46
|
+
const audio = tts.generate({ text: request.text, sid: request.voiceId, speed: request.speed });
|
|
47
|
+
const generated = performance.now();
|
|
48
|
+
const samples = audio.samples;
|
|
49
|
+
if (audio.sampleRate !== 24000 || !samples || samples.length < 2400 || samples.length > 1440000)
|
|
50
|
+
throw Error();
|
|
51
|
+
// 首次可持久化输出采用明确的 Float32→PCM16 编码,不把浮点帧称为无损 PCM16。
|
|
52
|
+
const wave = Buffer.alloc(44 + samples.length * 2);
|
|
53
|
+
wave.write("RIFF");
|
|
54
|
+
wave.writeUInt32LE(wave.length - 8, 4);
|
|
55
|
+
wave.write("WAVEfmt ", 8);
|
|
56
|
+
wave.writeUInt32LE(16, 16);
|
|
57
|
+
wave.writeUInt16LE(1, 20);
|
|
58
|
+
wave.writeUInt16LE(1, 22);
|
|
59
|
+
wave.writeUInt32LE(24000, 24);
|
|
60
|
+
wave.writeUInt32LE(48000, 28);
|
|
61
|
+
wave.writeUInt16LE(2, 32);
|
|
62
|
+
wave.writeUInt16LE(16, 34);
|
|
63
|
+
wave.write("data", 36);
|
|
64
|
+
wave.writeUInt32LE(samples.length * 2, 40);
|
|
65
|
+
let nonzero = false;
|
|
66
|
+
for (let index = 0; index < samples.length; index++) {
|
|
67
|
+
const value = samples[index];
|
|
68
|
+
if (!Number.isFinite(value)) throw Error();
|
|
69
|
+
const integer = Math.round(Math.max(-1, Math.min(1, value)) * (value < 0 ? 32768 : 32767));
|
|
70
|
+
nonzero ||= integer !== 0;
|
|
71
|
+
wave.writeInt16LE(integer, 44 + index * 2);
|
|
72
|
+
}
|
|
73
|
+
if (!nonzero) throw Error();
|
|
74
|
+
await writeFile(output, wave, { mode: 0o600, flag: "wx" });
|
|
75
|
+
const usage = process.resourceUsage();
|
|
76
|
+
process.stdout.write(
|
|
77
|
+
JSON.stringify({
|
|
78
|
+
modelLoadMs: loaded - began,
|
|
79
|
+
synthesisMs: generated - loaded,
|
|
80
|
+
cpuMs: (usage.userCPUTime + usage.systemCPUTime) / 1000,
|
|
81
|
+
peakRssBytes: usage.maxRSS * 1024,
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
// 不回传 Native 库的原始错误、路径或文本;父进程保留明确失败和资源回执。
|
|
86
|
+
main().catch(() => {
|
|
87
|
+
process.stderr.write("CPU_SPEECH_EXECUTION_FAILED");
|
|
88
|
+
process.exitCode = 1;
|
|
89
|
+
});
|
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"protocol":"NVS_WINDOWS_HELPER_V1","format":"PE_AMD64_NETFRAMEWORK48","sourceDigest":"7c72ed925f68ed26348e8eedf9ac194133475402368a780477221d13efbe0b29","sha256":"5865bcc8a60cb05053e62b1b244852127d96a732802338e572bc48ade2ad969e","byteSize":33280,"sdkVersion":"10.0.404","sdkSha512":"88ca5dac50562b619f852b1b9845f85243f00434be52d2149db18ab4dc05f527f3fca8e1dac82e87b9eeb46a98de1ba4a6d612714caf2222f37b95aeebb611b6","referenceVersion":"1.0.3","referenceSha512":"5d62a0c9e35a74d71341a215bd007c06b74236b11aafa7e8fdd7b539d41167d1c5cb48dc05268cf08579abae196a6a6c4a70bc0254ec002686654ba8170e3544"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
/** IPC 连接绑定精确父进程;Linux 同时复核 PID/start-token,避免 PID 重用。 */
|
|
3
|
+
export function watchOwnedParent(parent, token, controller) {
|
|
4
|
+
let lost = false;
|
|
5
|
+
const stop = () => {
|
|
6
|
+
lost = true;
|
|
7
|
+
controller.abort();
|
|
8
|
+
};
|
|
9
|
+
const alive = async () => {
|
|
10
|
+
if (!process.connected || process.ppid !== parent) return false;
|
|
11
|
+
if (process.platform !== "linux") return process.platform === "darwin";
|
|
12
|
+
try {
|
|
13
|
+
const text = await readFile("/proc/" + parent + "/stat", "utf8");
|
|
14
|
+
const fields = text.slice(text.lastIndexOf(")") + 2).split(/\s+/);
|
|
15
|
+
return fields[19] === token && fields[0] !== "Z";
|
|
16
|
+
} catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const check = async () => {
|
|
21
|
+
if (!(await alive())) stop();
|
|
22
|
+
};
|
|
23
|
+
process.once("disconnect", stop);
|
|
24
|
+
process.once("SIGTERM", () => controller.abort());
|
|
25
|
+
process.once("SIGINT", () => controller.abort());
|
|
26
|
+
void check();
|
|
27
|
+
const monitor = setInterval(() => {
|
|
28
|
+
void check();
|
|
29
|
+
}, 100);
|
|
30
|
+
return {
|
|
31
|
+
alive,
|
|
32
|
+
check,
|
|
33
|
+
get lost() {
|
|
34
|
+
return lost;
|
|
35
|
+
},
|
|
36
|
+
close() {
|
|
37
|
+
clearInterval(monitor);
|
|
38
|
+
process.removeListener("disconnect", stop);
|
|
39
|
+
if (process.connected) process.disconnect();
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { watchOwnedParent } from "./parent-lifetime.mjs";
|
|
3
|
+
|
|
4
|
+
if (!process.send || !["linux", "darwin"].includes(process.platform))
|
|
5
|
+
throw Error("OWNED_MEDIA_SUPERVISOR_REQUIRED");
|
|
6
|
+
const parent = Number(process.argv[2]),
|
|
7
|
+
token = process.argv[3];
|
|
8
|
+
if (!Number.isSafeInteger(parent) || parent < 2 || !token) throw Error("INVALID_PARENT");
|
|
9
|
+
const controller = new AbortController();
|
|
10
|
+
const watcher = watchOwnedParent(parent, token, controller);
|
|
11
|
+
let child,
|
|
12
|
+
force,
|
|
13
|
+
timer,
|
|
14
|
+
started = false,
|
|
15
|
+
closed = false;
|
|
16
|
+
const stop = () => controller.abort();
|
|
17
|
+
const terminate = () => {
|
|
18
|
+
if (child && child.exitCode === null && child.signalCode === null) child.kill("SIGKILL");
|
|
19
|
+
force ??= setTimeout(() => {
|
|
20
|
+
// Spawned detached by runner.ts: this group contains only this supervisor/command.
|
|
21
|
+
try {
|
|
22
|
+
process.kill(-process.pid, "SIGKILL");
|
|
23
|
+
} catch {
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
}, 1000);
|
|
27
|
+
};
|
|
28
|
+
controller.signal.addEventListener("abort", terminate, { once: true });
|
|
29
|
+
process.stdout.on("error", stop);
|
|
30
|
+
process.stderr.on("error", stop);
|
|
31
|
+
const startup = setTimeout(stop, 5000);
|
|
32
|
+
function finish(success) {
|
|
33
|
+
if (closed) return;
|
|
34
|
+
closed = true;
|
|
35
|
+
clearTimeout(startup);
|
|
36
|
+
clearTimeout(timer);
|
|
37
|
+
clearTimeout(force);
|
|
38
|
+
watcher.close();
|
|
39
|
+
process.exitCode = success && !controller.signal.aborted ? 0 : 1;
|
|
40
|
+
}
|
|
41
|
+
process.once("message", async (request) => {
|
|
42
|
+
clearTimeout(startup);
|
|
43
|
+
if (started) return;
|
|
44
|
+
started = true;
|
|
45
|
+
try {
|
|
46
|
+
if (
|
|
47
|
+
!request ||
|
|
48
|
+
typeof request.binary !== "string" ||
|
|
49
|
+
!request.binary ||
|
|
50
|
+
!Array.isArray(request.args) ||
|
|
51
|
+
!request.args.every((v) => typeof v === "string") ||
|
|
52
|
+
(request.cwd !== undefined && typeof request.cwd !== "string") ||
|
|
53
|
+
!Number.isSafeInteger(request.expiresAt) ||
|
|
54
|
+
request.expiresAt <= Date.now() ||
|
|
55
|
+
!Number.isSafeInteger(request.maxOutputBytes) ||
|
|
56
|
+
request.maxOutputBytes < 1 ||
|
|
57
|
+
!(await watcher.alive()) ||
|
|
58
|
+
controller.signal.aborted
|
|
59
|
+
)
|
|
60
|
+
return finish(false);
|
|
61
|
+
timer = setTimeout(stop, Math.min(2147483647, request.expiresAt - Date.now()));
|
|
62
|
+
child = spawn(request.binary, request.args, {
|
|
63
|
+
shell: false,
|
|
64
|
+
windowsHide: true,
|
|
65
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
66
|
+
...(request.cwd ? { cwd: request.cwd } : {}),
|
|
67
|
+
env: { PATH: process.env.PATH ?? "/usr/bin:/bin", LANG: "C", LC_ALL: "C" },
|
|
68
|
+
});
|
|
69
|
+
let stdout = 0,
|
|
70
|
+
stderr = 0;
|
|
71
|
+
child.stdout.on("data", (b) => {
|
|
72
|
+
stdout += b.length;
|
|
73
|
+
if (stdout > request.maxOutputBytes) stop();
|
|
74
|
+
});
|
|
75
|
+
child.stderr.on("data", (b) => {
|
|
76
|
+
stderr += b.length;
|
|
77
|
+
if (stderr > 65536) stop();
|
|
78
|
+
});
|
|
79
|
+
child.stdout.pipe(process.stdout, { end: false });
|
|
80
|
+
child.stderr.pipe(process.stderr, { end: false });
|
|
81
|
+
child.once("error", () => finish(false));
|
|
82
|
+
child.once("close", (code) => finish(code === 0));
|
|
83
|
+
if (controller.signal.aborted) stop();
|
|
84
|
+
} catch {
|
|
85
|
+
stop();
|
|
86
|
+
if (!child) finish(false);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
await watcher.check();
|
package/package.json
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nativestudio/local-runtime",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.0-rc.25",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=24.12 <25"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {},
|
|
10
|
+
"bin": {
|
|
11
|
+
"nvs-runtime": "./dist/entry.js"
|
|
12
|
+
}
|
|
6
13
|
}
|
package/README.md
DELETED