@adea-ai/voice 0.0.0-stage → 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 +136 -2
- package/dist/cli/helpers-cli.d.ts +2 -0
- package/dist/cli/helpers-cli.js +117 -0
- package/dist/contract/contract.d.ts +157 -0
- package/dist/contract/errors.d.ts +26 -0
- package/dist/contract/index.d.ts +10 -0
- package/dist/contract/index.js +64 -0
- package/dist/contract/session.d.ts +71 -0
- package/dist/contract/turns.d.ts +91 -0
- package/dist/contract/wire.d.ts +279 -0
- package/dist/desktop/conversation.d.ts +86 -0
- package/dist/desktop/desktop-voice.d.ts +212 -0
- package/dist/desktop/helper-lock.d.ts +121 -0
- package/dist/desktop/helper-process.d.ts +96 -0
- package/dist/desktop/index.d.ts +9 -0
- package/dist/desktop/index.js +1454 -0
- package/dist/desktop/macho-digest.d.ts +9 -0
- package/dist/index-7n7epq1a.js +47 -0
- package/dist/index-a7d617zr.js +295 -0
- package/dist/index-ght8wwpd.js +156 -0
- package/dist/index-m4g2tpeq.js +522 -0
- package/dist/index-r4hz8gk0.js +10 -0
- package/dist/index-tvz9s9nz.js +77 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +96 -0
- package/dist/settings/index.d.ts +5 -0
- package/dist/settings/index.js +34 -0
- package/dist/settings/settings.d.ts +163 -0
- package/dist/web/index.d.ts +16 -0
- package/dist/web/index.js +1697 -0
- package/dist/web/stt/speech-recognition.d.ts +226 -0
- package/dist/web/tts/locale-match.d.ts +40 -0
- package/dist/web/tts/phonon.d.ts +139 -0
- package/dist/web/tts/player.d.ts +115 -0
- package/dist/web/tts/system-voices.d.ts +162 -0
- package/dist/web/turns.d.ts +91 -0
- package/package.json +68 -4
- package/src/cli/helpers-cli.ts +135 -0
- package/src/contract/contract.ts +393 -0
- package/src/contract/errors.ts +98 -0
- package/src/contract/index.ts +10 -0
- package/src/contract/session.ts +77 -0
- package/src/contract/turns.ts +48 -0
- package/src/contract/wire.ts +384 -0
- package/src/desktop/conversation.ts +347 -0
- package/src/desktop/desktop-voice.ts +1129 -0
- package/src/desktop/helper-lock.ts +550 -0
- package/src/desktop/helper-process.ts +408 -0
- package/src/desktop/index.ts +18 -0
- package/src/desktop/macho-digest.ts +135 -0
- package/src/index.ts +7 -0
- package/src/settings/index.ts +5 -0
- package/src/settings/settings.ts +478 -0
- package/src/web/index.ts +16 -0
- package/src/web/stt/speech-recognition.ts +1013 -0
- package/src/web/tts/locale-match.ts +131 -0
- package/src/web/tts/phonon.ts +386 -0
- package/src/web/tts/player.ts +271 -0
- package/src/web/tts/system-voices.ts +510 -0
- package/src/web/turns.ts +235 -0
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
import {
|
|
2
|
+
WIRE_VERSION2,
|
|
3
|
+
CONTRACT_MAJOR2,
|
|
4
|
+
parseWireLine2
|
|
5
|
+
} from "./index-7n7epq1a.js";
|
|
6
|
+
|
|
7
|
+
// src/desktop/helper-lock.ts
|
|
8
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
9
|
+
import { spawnSync } from "node:child_process";
|
|
10
|
+
import {
|
|
11
|
+
chmodSync,
|
|
12
|
+
existsSync,
|
|
13
|
+
lstatSync,
|
|
14
|
+
mkdirSync,
|
|
15
|
+
mkdtempSync,
|
|
16
|
+
readdirSync,
|
|
17
|
+
readFileSync as readFileSync2,
|
|
18
|
+
renameSync,
|
|
19
|
+
rmSync,
|
|
20
|
+
statSync,
|
|
21
|
+
writeFileSync
|
|
22
|
+
} from "node:fs";
|
|
23
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
24
|
+
import { tmpdir } from "node:os";
|
|
25
|
+
|
|
26
|
+
// src/desktop/macho-digest.ts
|
|
27
|
+
import { createHash } from "node:crypto";
|
|
28
|
+
import { readFileSync } from "node:fs";
|
|
29
|
+
var MH_MAGIC_64 = 4277009103;
|
|
30
|
+
var FAT_MAGIC = 3405691582;
|
|
31
|
+
var FAT_MAGIC_64 = 3405691583;
|
|
32
|
+
var LC_SEGMENT_64 = 25;
|
|
33
|
+
var LC_CODE_SIGNATURE = 29;
|
|
34
|
+
var HEADER_SIZE = 32;
|
|
35
|
+
var SIGNATURE_COMMAND_SIZE = 16;
|
|
36
|
+
var MAX_FAT_ARCHS = 16;
|
|
37
|
+
function isMachO2(bytes) {
|
|
38
|
+
if (bytes.byteLength < 8)
|
|
39
|
+
return false;
|
|
40
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
41
|
+
const little = view.getUint32(0, true);
|
|
42
|
+
if (little === MH_MAGIC_64 || little === 4277009102)
|
|
43
|
+
return true;
|
|
44
|
+
const big = view.getUint32(0, false);
|
|
45
|
+
return (big === FAT_MAGIC || big === FAT_MAGIC_64) && view.getUint32(4, false) > 0 && view.getUint32(4, false) <= MAX_FAT_ARCHS;
|
|
46
|
+
}
|
|
47
|
+
function machoCodeDigest2(bytes) {
|
|
48
|
+
if (bytes.byteLength < 8)
|
|
49
|
+
return;
|
|
50
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
51
|
+
const magic = view.getUint32(0, false);
|
|
52
|
+
if (magic === FAT_MAGIC || magic === FAT_MAGIC_64)
|
|
53
|
+
return fatDigest(bytes, view, magic === FAT_MAGIC_64);
|
|
54
|
+
return thinDigest(bytes);
|
|
55
|
+
}
|
|
56
|
+
function machoCodeDigestFile2(path) {
|
|
57
|
+
return machoCodeDigest2(new Uint8Array(readFileSync(path)));
|
|
58
|
+
}
|
|
59
|
+
function fatDigest(bytes, view, wide) {
|
|
60
|
+
const count = view.getUint32(4, false);
|
|
61
|
+
if (count === 0 || count > MAX_FAT_ARCHS)
|
|
62
|
+
return;
|
|
63
|
+
const entrySize = wide ? 32 : 20;
|
|
64
|
+
if (8 + count * entrySize > bytes.byteLength)
|
|
65
|
+
return;
|
|
66
|
+
const lines = [];
|
|
67
|
+
for (let index = 0;index < count; index += 1) {
|
|
68
|
+
const at = 8 + index * entrySize;
|
|
69
|
+
const cpuType = view.getUint32(at, false);
|
|
70
|
+
const cpuSubtype = view.getUint32(at + 4, false);
|
|
71
|
+
const offset = wide ? Number(view.getBigUint64(at + 8, false)) : view.getUint32(at + 8, false);
|
|
72
|
+
const size = wide ? Number(view.getBigUint64(at + 16, false)) : view.getUint32(at + 12, false);
|
|
73
|
+
if (offset + size > bytes.byteLength || size < HEADER_SIZE)
|
|
74
|
+
return;
|
|
75
|
+
const slice = thinDigest(bytes.subarray(offset, offset + size));
|
|
76
|
+
if (!slice)
|
|
77
|
+
return;
|
|
78
|
+
lines.push(`${cpuType}:${cpuSubtype}:${slice}
|
|
79
|
+
`);
|
|
80
|
+
}
|
|
81
|
+
lines.sort();
|
|
82
|
+
return createHash("sha256").update(`fat
|
|
83
|
+
${lines.join("")}`).digest("hex");
|
|
84
|
+
}
|
|
85
|
+
function thinDigest(bytes) {
|
|
86
|
+
if (bytes.byteLength < HEADER_SIZE)
|
|
87
|
+
return;
|
|
88
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
89
|
+
if (view.getUint32(0, true) !== MH_MAGIC_64)
|
|
90
|
+
return;
|
|
91
|
+
const commandCount = view.getUint32(16, true);
|
|
92
|
+
const commandsSize = view.getUint32(20, true);
|
|
93
|
+
const commandsEnd = HEADER_SIZE + commandsSize;
|
|
94
|
+
if (commandsEnd > bytes.byteLength)
|
|
95
|
+
return;
|
|
96
|
+
let signature;
|
|
97
|
+
const linkedit = [];
|
|
98
|
+
let offset = HEADER_SIZE;
|
|
99
|
+
for (let index = 0;index < commandCount; index += 1) {
|
|
100
|
+
if (offset + 8 > commandsEnd)
|
|
101
|
+
return;
|
|
102
|
+
const command = view.getUint32(offset, true);
|
|
103
|
+
const size = view.getUint32(offset + 4, true);
|
|
104
|
+
if (size < 8 || offset + size > commandsEnd)
|
|
105
|
+
return;
|
|
106
|
+
if (command === LC_CODE_SIGNATURE) {
|
|
107
|
+
if (signature || size !== SIGNATURE_COMMAND_SIZE)
|
|
108
|
+
return;
|
|
109
|
+
signature = { offset, dataOffset: view.getUint32(offset + 8, true) };
|
|
110
|
+
} else if (command === LC_SEGMENT_64 && size >= 72 && segmentName(bytes, offset + 8) === "__LINKEDIT") {
|
|
111
|
+
linkedit.push(offset);
|
|
112
|
+
}
|
|
113
|
+
offset += size;
|
|
114
|
+
}
|
|
115
|
+
if (offset !== commandsEnd)
|
|
116
|
+
return;
|
|
117
|
+
const end = signature ? signature.dataOffset : bytes.byteLength;
|
|
118
|
+
if (end > bytes.byteLength || end < commandsEnd)
|
|
119
|
+
return;
|
|
120
|
+
const code = Buffer.from(bytes.subarray(0, end));
|
|
121
|
+
for (const segment of linkedit) {
|
|
122
|
+
code.fill(0, segment + 32, segment + 40);
|
|
123
|
+
code.fill(0, segment + 48, segment + 56);
|
|
124
|
+
}
|
|
125
|
+
if (signature) {
|
|
126
|
+
code.copy(code, signature.offset, signature.offset + SIGNATURE_COMMAND_SIZE, commandsEnd);
|
|
127
|
+
code.fill(0, commandsEnd - SIGNATURE_COMMAND_SIZE, commandsEnd);
|
|
128
|
+
code.writeUInt32LE(commandCount - 1, 16);
|
|
129
|
+
code.writeUInt32LE(commandsSize - SIGNATURE_COMMAND_SIZE, 20);
|
|
130
|
+
}
|
|
131
|
+
let length = code.length;
|
|
132
|
+
while (length > 0 && code[length - 1] === 0)
|
|
133
|
+
length -= 1;
|
|
134
|
+
return createHash("sha256").update(code.subarray(0, length)).digest("hex");
|
|
135
|
+
}
|
|
136
|
+
function segmentName(bytes, at) {
|
|
137
|
+
let name = "";
|
|
138
|
+
for (let index = 0;index < 16; index += 1) {
|
|
139
|
+
const byte = bytes[at + index];
|
|
140
|
+
if (byte === 0)
|
|
141
|
+
break;
|
|
142
|
+
name += String.fromCharCode(byte);
|
|
143
|
+
}
|
|
144
|
+
return name;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// src/desktop/helper-lock.ts
|
|
148
|
+
var HELPER_LOCK_SCHEMA2 = 1;
|
|
149
|
+
var HELPER_NAMES2 = ["adea-voice-helper", "adea-transcribe-helper", "adea-tts-helper"];
|
|
150
|
+
var HELPER_TARGETS2 = ["macos-arm64", "macos-x64", "linux-x64", "linux-arm64", "windows-x64"];
|
|
151
|
+
var MAX_HELPER_ARCHIVE_BYTES2 = 256 * 1024 * 1024;
|
|
152
|
+
var STAGED_RECORD_FILE2 = "voice-helpers.json";
|
|
153
|
+
var SHA256 = /^[0-9a-f]{64}$/;
|
|
154
|
+
var SEMVER = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
|
|
155
|
+
var FILE_NAME = /^[A-Za-z0-9._-]+\.tar\.gz$/;
|
|
156
|
+
|
|
157
|
+
class HelperLockError2 extends Error {
|
|
158
|
+
constructor(message) {
|
|
159
|
+
super(message);
|
|
160
|
+
this.name = "HelperLockError";
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function validateHelperLock2(value) {
|
|
164
|
+
const fail = (message) => {
|
|
165
|
+
throw new HelperLockError2(`voice helper lock: ${message}`);
|
|
166
|
+
};
|
|
167
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
168
|
+
fail("must be a JSON object");
|
|
169
|
+
const lock = value;
|
|
170
|
+
if (lock.schema !== HELPER_LOCK_SCHEMA2)
|
|
171
|
+
fail(`schema must be ${HELPER_LOCK_SCHEMA2}`);
|
|
172
|
+
if (typeof lock.version !== "string" || !SEMVER.test(lock.version))
|
|
173
|
+
fail("version must be a semver");
|
|
174
|
+
if (typeof lock.base !== "string" || !/^https:\/\/[^\s]+\/$/.test(lock.base))
|
|
175
|
+
fail("base must be an https URL ending in /");
|
|
176
|
+
if (lock.helpers === null || typeof lock.helpers !== "object")
|
|
177
|
+
fail("helpers must be an object");
|
|
178
|
+
for (const [name, targets] of Object.entries(lock.helpers)) {
|
|
179
|
+
if (!HELPER_NAMES2.includes(name))
|
|
180
|
+
fail(`unknown helper ${name}`);
|
|
181
|
+
if (targets === null || typeof targets !== "object")
|
|
182
|
+
fail(`${name} must map targets to pins`);
|
|
183
|
+
for (const [target, pin] of Object.entries(targets)) {
|
|
184
|
+
if (!HELPER_TARGETS2.includes(target))
|
|
185
|
+
fail(`${name}: unknown target ${target}`);
|
|
186
|
+
const entry = pin;
|
|
187
|
+
if (typeof entry?.file !== "string" || !FILE_NAME.test(entry.file))
|
|
188
|
+
fail(`${name}/${target}: file must be a .tar.gz name`);
|
|
189
|
+
if (typeof entry.sha256 !== "string" || !SHA256.test(entry.sha256))
|
|
190
|
+
fail(`${name}/${target}: sha256 must be 64 hex`);
|
|
191
|
+
if (typeof entry.executableSha256 !== "string" || !SHA256.test(entry.executableSha256)) {
|
|
192
|
+
fail(`${name}/${target}: executableSha256 must be 64 hex`);
|
|
193
|
+
}
|
|
194
|
+
if (entry.codeSha256 !== undefined && (typeof entry.codeSha256 !== "string" || !SHA256.test(entry.codeSha256))) {
|
|
195
|
+
fail(`${name}/${target}: codeSha256 must be 64 hex`);
|
|
196
|
+
}
|
|
197
|
+
if (entry.bytes !== undefined && (typeof entry.bytes !== "number" || entry.bytes <= 0 || entry.bytes > MAX_HELPER_ARCHIVE_BYTES2)) {
|
|
198
|
+
fail(`${name}/${target}: bytes out of range`);
|
|
199
|
+
}
|
|
200
|
+
if (!entry.file.includes(lock.version))
|
|
201
|
+
fail(`${name}/${target}: file does not carry version ${String(lock.version)}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return value;
|
|
205
|
+
}
|
|
206
|
+
function readHelperLock2(path) {
|
|
207
|
+
let text;
|
|
208
|
+
try {
|
|
209
|
+
text = readFileSync2(path, "utf8");
|
|
210
|
+
} catch (error) {
|
|
211
|
+
throw new HelperLockError2(`voice helper lock: cannot read ${path}: ${String(error)}`);
|
|
212
|
+
}
|
|
213
|
+
let parsed;
|
|
214
|
+
try {
|
|
215
|
+
parsed = JSON.parse(text);
|
|
216
|
+
} catch (error) {
|
|
217
|
+
throw new HelperLockError2(`voice helper lock: ${path} is not JSON: ${String(error)}`);
|
|
218
|
+
}
|
|
219
|
+
return validateHelperLock2(parsed);
|
|
220
|
+
}
|
|
221
|
+
function helperPin2(lock, helper, target) {
|
|
222
|
+
return lock.helpers[helper]?.[target];
|
|
223
|
+
}
|
|
224
|
+
function helperArtifactUrl2(lock, helper, target) {
|
|
225
|
+
const pin = helperPin2(lock, helper, target);
|
|
226
|
+
if (!pin)
|
|
227
|
+
throw new HelperLockError2(`voice helper lock pins no ${helper} for ${target}`);
|
|
228
|
+
return `${lock.base}${helper}/${lock.version}/${pin.file}`;
|
|
229
|
+
}
|
|
230
|
+
function currentHelperTarget2(platform = process.platform, arch = process.arch) {
|
|
231
|
+
const os = platform === "darwin" ? "macos" : platform === "win32" ? "windows" : platform === "linux" ? "linux" : undefined;
|
|
232
|
+
const cpu = arch === "arm64" ? "arm64" : arch === "x64" ? "x64" : undefined;
|
|
233
|
+
if (!os || !cpu)
|
|
234
|
+
return;
|
|
235
|
+
const target = `${os}-${cpu}`;
|
|
236
|
+
return HELPER_TARGETS2.includes(target) ? target : undefined;
|
|
237
|
+
}
|
|
238
|
+
function sha256File2(path) {
|
|
239
|
+
return createHash2("sha256").update(readFileSync2(path)).digest("hex");
|
|
240
|
+
}
|
|
241
|
+
async function stageVoiceHelpers2(options) {
|
|
242
|
+
const log = options.log ?? (() => {
|
|
243
|
+
return;
|
|
244
|
+
});
|
|
245
|
+
const outDir = resolve(options.outDir);
|
|
246
|
+
const handshake = options.handshake ?? options.target === currentHelperTarget2();
|
|
247
|
+
const existing = readStagedRecord2(outDir);
|
|
248
|
+
const helpers = {};
|
|
249
|
+
const version = options.localDir ? `local+${options.lock.version}` : options.lock.version;
|
|
250
|
+
mkdirSync(outDir, { recursive: true });
|
|
251
|
+
try {
|
|
252
|
+
for (const helper of options.helpers) {
|
|
253
|
+
const executable = join(outDir, helper);
|
|
254
|
+
if (options.localDir) {
|
|
255
|
+
stageLocal(options.localDir, helper, outDir);
|
|
256
|
+
helpers[helper] = { sha256: "local", executableSha256: sha256File2(executable), ...codeDigestEntry(executable) };
|
|
257
|
+
} else {
|
|
258
|
+
const pin = helperPin2(options.lock, helper, options.target);
|
|
259
|
+
if (!pin)
|
|
260
|
+
throw new HelperLockError2(`voice helper lock pins no ${helper} for ${options.target}`);
|
|
261
|
+
const reuse = existing?.version === options.lock.version && existing.helpers[helper]?.sha256 === pin.sha256 && existsSync(executable) && sha256File2(executable) === pin.executableSha256;
|
|
262
|
+
if (reuse) {
|
|
263
|
+
log(`${helper} ${options.lock.version} already staged`);
|
|
264
|
+
} else {
|
|
265
|
+
const archive = await obtainArchive(options, helper, pin, log);
|
|
266
|
+
try {
|
|
267
|
+
extractArchive(archive, outDir, helper);
|
|
268
|
+
} finally {
|
|
269
|
+
if (!options.cacheDir)
|
|
270
|
+
rmSync(dirname(archive), { recursive: true, force: true });
|
|
271
|
+
}
|
|
272
|
+
const actual = existsSync(executable) ? sha256File2(executable) : "missing";
|
|
273
|
+
if (actual !== pin.executableSha256) {
|
|
274
|
+
throw new HelperLockError2(`${helper}: executable sha256 ${actual} is not the pinned ${pin.executableSha256}`);
|
|
275
|
+
}
|
|
276
|
+
chmodSync(executable, 493);
|
|
277
|
+
log(`staged ${helper} ${options.lock.version} (${options.target})`);
|
|
278
|
+
}
|
|
279
|
+
const code = codeDigestEntry(executable);
|
|
280
|
+
if (pin.codeSha256 && code.codeSha256 !== pin.codeSha256) {
|
|
281
|
+
throw new HelperLockError2(`${helper}: code digest ${code.codeSha256 ?? "unavailable"} is not the pinned ${pin.codeSha256}`);
|
|
282
|
+
}
|
|
283
|
+
helpers[helper] = { sha256: pin.sha256, executableSha256: pin.executableSha256, ...code };
|
|
284
|
+
}
|
|
285
|
+
if (handshake)
|
|
286
|
+
assertHandshake2(executable, options.localDir ? {} : { release: options.lock.version });
|
|
287
|
+
}
|
|
288
|
+
} catch (error) {
|
|
289
|
+
rmSync(outDir, { recursive: true, force: true });
|
|
290
|
+
throw error;
|
|
291
|
+
}
|
|
292
|
+
const record = {
|
|
293
|
+
schema: 1,
|
|
294
|
+
version,
|
|
295
|
+
target: options.target,
|
|
296
|
+
helpers,
|
|
297
|
+
files: supportFileDigests(outDir, Object.keys(helpers))
|
|
298
|
+
};
|
|
299
|
+
writeFileSync(join(outDir, STAGED_RECORD_FILE2), `${JSON.stringify(record, null, 2)}
|
|
300
|
+
`);
|
|
301
|
+
return record;
|
|
302
|
+
}
|
|
303
|
+
function verifyStagedVoiceHelpers2(options) {
|
|
304
|
+
const dir = resolve(options.dir);
|
|
305
|
+
const record = readStagedRecord2(dir);
|
|
306
|
+
if (!record)
|
|
307
|
+
throw new HelperLockError2(`no ${STAGED_RECORD_FILE2} in ${dir}: the voice helpers were not staged from the lock`);
|
|
308
|
+
if (record.version !== options.lock.version) {
|
|
309
|
+
throw new HelperLockError2(`staged voice helpers are ${record.version}; the lock pins ${options.lock.version}`);
|
|
310
|
+
}
|
|
311
|
+
if (record.target !== options.target)
|
|
312
|
+
throw new HelperLockError2(`staged voice helpers are for ${record.target}, not ${options.target}`);
|
|
313
|
+
const codesign = options.codesign ?? (process.platform === "darwin" && options.target.startsWith("macos-"));
|
|
314
|
+
for (const helper of options.helpers) {
|
|
315
|
+
const pin = helperPin2(options.lock, helper, options.target);
|
|
316
|
+
if (!pin)
|
|
317
|
+
throw new HelperLockError2(`voice helper lock pins no ${helper} for ${options.target}`);
|
|
318
|
+
const executable = join(dir, helper);
|
|
319
|
+
if (!existsSync(executable) || !lstatSync(executable).isFile())
|
|
320
|
+
throw new HelperLockError2(`${helper} is missing from ${dir}`);
|
|
321
|
+
const staged = record.helpers[helper];
|
|
322
|
+
if (staged && (staged.sha256 !== pin.sha256 || staged.executableSha256 !== pin.executableSha256)) {
|
|
323
|
+
throw new HelperLockError2(`${helper} in ${dir} was staged from ${staged.sha256}, not the pinned ${pin.sha256}`);
|
|
324
|
+
}
|
|
325
|
+
const actual = sha256File2(executable);
|
|
326
|
+
if (actual !== pin.executableSha256) {
|
|
327
|
+
const expected = pin.codeSha256 ?? staged?.codeSha256;
|
|
328
|
+
if (!expected) {
|
|
329
|
+
throw new HelperLockError2(`${helper} in ${dir} has sha256 ${actual}, not the pinned ${pin.executableSha256}, and neither the lock nor ${STAGED_RECORD_FILE2} records a code digest to compare a re-signed copy with (stage it with @adea-ai/voice 0.2.1 or later)`);
|
|
330
|
+
}
|
|
331
|
+
const code = machoCodeDigest2(new Uint8Array(readFileSync2(executable)));
|
|
332
|
+
if (code !== expected) {
|
|
333
|
+
throw new HelperLockError2(`${helper} in ${dir} has code digest ${code ?? "unavailable"}, not the pinned ${expected}`);
|
|
334
|
+
}
|
|
335
|
+
if (codesign)
|
|
336
|
+
assertSigned(executable);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
verifySupportFiles(dir, record);
|
|
340
|
+
if (options.handshake ?? options.target === currentHelperTarget2()) {
|
|
341
|
+
for (const helper of options.helpers) {
|
|
342
|
+
assertHandshake2(join(dir, helper), { release: options.lock.version, timeoutMs: options.handshakeTimeoutMs });
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
function readStagedRecord2(dir) {
|
|
347
|
+
const path = join(dir, STAGED_RECORD_FILE2);
|
|
348
|
+
if (!existsSync(path))
|
|
349
|
+
return;
|
|
350
|
+
try {
|
|
351
|
+
return JSON.parse(readFileSync2(path, "utf8"));
|
|
352
|
+
} catch {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
function assertHandshake2(executable, expect = {}) {
|
|
357
|
+
const timeoutMs = expect.timeoutMs ?? 1e4;
|
|
358
|
+
const run = spawnSync(executable, ["--version"], { encoding: "utf8", timeout: timeoutMs, killSignal: "SIGKILL" });
|
|
359
|
+
if (run.error || run.status !== 0) {
|
|
360
|
+
const reason = run.error ? run.error.code === "ETIMEDOUT" ? `did not answer within ${timeoutMs} ms` : run.error.message : run.signal ? `was killed by ${run.signal}` : `exited ${String(run.status)}`;
|
|
361
|
+
const detail = (run.stderr ?? "").trim().split(`
|
|
362
|
+
`)[0]?.slice(0, 300);
|
|
363
|
+
throw new HelperLockError2(`${executable} --version failed: ${reason}${detail ? ` (${detail})` : ""}`);
|
|
364
|
+
}
|
|
365
|
+
const version = run.stdout.split(`
|
|
366
|
+
`).map(parseWireLine2).find((event) => event?.event === "version");
|
|
367
|
+
if (!version || version.event !== "version")
|
|
368
|
+
throw new HelperLockError2(`${executable} printed no version handshake`);
|
|
369
|
+
if (version.wire !== WIRE_VERSION2 || version.contractMajor !== CONTRACT_MAJOR2) {
|
|
370
|
+
throw new HelperLockError2(`${executable} speaks wire ${version.wire} / contract ${version.contract}; need wire ${WIRE_VERSION2} / contract ${CONTRACT_MAJOR2}`);
|
|
371
|
+
}
|
|
372
|
+
if (expect.release !== undefined && version.release !== expect.release) {
|
|
373
|
+
throw new HelperLockError2(`${executable} reports release ${version.release ?? "(none)"}; the lock pins ${expect.release}`);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
function codeDigestEntry(executable) {
|
|
377
|
+
const digest = machoCodeDigest2(new Uint8Array(readFileSync2(executable)));
|
|
378
|
+
return digest ? { codeSha256: digest } : {};
|
|
379
|
+
}
|
|
380
|
+
function assertSigned(path) {
|
|
381
|
+
const run = spawnSync("codesign", ["--verify", "--strict", path], { encoding: "utf8", timeout: 30000 });
|
|
382
|
+
if (run.error || run.status !== 0) {
|
|
383
|
+
throw new HelperLockError2(`${path} is not validly signed: ${(run.stderr || run.error?.message || `exit ${String(run.status)}`).trim()}`);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
function walk(dir, root = dir) {
|
|
387
|
+
const entries = [];
|
|
388
|
+
for (const name of readdirSync(dir).sort()) {
|
|
389
|
+
const path = join(dir, name);
|
|
390
|
+
const stat = lstatSync(path);
|
|
391
|
+
entries.push([relative(root, path).split(sep).join("/"), stat]);
|
|
392
|
+
if (stat.isDirectory())
|
|
393
|
+
entries.push(...walk(path, root));
|
|
394
|
+
}
|
|
395
|
+
return entries;
|
|
396
|
+
}
|
|
397
|
+
function isSignatureFile(path) {
|
|
398
|
+
return path.split("/").includes("_CodeSignature");
|
|
399
|
+
}
|
|
400
|
+
function supportFileDigests(dir, helpers) {
|
|
401
|
+
const files = {};
|
|
402
|
+
for (const [path, stat] of walk(dir)) {
|
|
403
|
+
if (!stat.isFile() || helpers.includes(path) || path === STAGED_RECORD_FILE2 || isSignatureFile(path))
|
|
404
|
+
continue;
|
|
405
|
+
files[path] = fileDigest(join(dir, path));
|
|
406
|
+
}
|
|
407
|
+
return files;
|
|
408
|
+
}
|
|
409
|
+
function fileDigest(path) {
|
|
410
|
+
const bytes = new Uint8Array(readFileSync2(path));
|
|
411
|
+
if (isMachO2(bytes)) {
|
|
412
|
+
const code = machoCodeDigest2(bytes);
|
|
413
|
+
if (code)
|
|
414
|
+
return code;
|
|
415
|
+
}
|
|
416
|
+
return createHash2("sha256").update(bytes).digest("hex");
|
|
417
|
+
}
|
|
418
|
+
function verifySupportFiles(dir, record) {
|
|
419
|
+
for (const [path, expected] of Object.entries(record.files ?? {})) {
|
|
420
|
+
const absolute = join(dir, path);
|
|
421
|
+
if (!existsSync(absolute))
|
|
422
|
+
throw new HelperLockError2(`${path} is missing from ${dir}`);
|
|
423
|
+
const stat = lstatSync(absolute);
|
|
424
|
+
if (!stat.isFile())
|
|
425
|
+
throw new HelperLockError2(`${path} in ${dir} is not a regular file`);
|
|
426
|
+
const actual = fileDigest(absolute);
|
|
427
|
+
if (actual !== expected)
|
|
428
|
+
throw new HelperLockError2(`${path} in ${dir} has digest ${actual}, not the staged ${expected}`);
|
|
429
|
+
}
|
|
430
|
+
for (const [path, stat] of walk(dir)) {
|
|
431
|
+
if (stat.isSymbolicLink())
|
|
432
|
+
throw new HelperLockError2(`${path} in ${dir} is a symlink; packaged voice helpers must not contain symlinks`);
|
|
433
|
+
if (stat.isFile() && stat.size === 0 && path.split("/").includes("Frameworks")) {
|
|
434
|
+
throw new HelperLockError2(`${path} in ${dir} is an empty file (a flattened symlink?)`);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
async function obtainArchive(options, helper, pin, log) {
|
|
439
|
+
const cached = options.cacheDir ? join(options.cacheDir, `${pin.sha256}.tar.gz`) : undefined;
|
|
440
|
+
if (cached && existsSync(cached) && sha256File2(cached) === pin.sha256) {
|
|
441
|
+
log(`${helper}: using cached ${pin.file}`);
|
|
442
|
+
return cached;
|
|
443
|
+
}
|
|
444
|
+
const url = helperArtifactUrl2(options.lock, helper, options.target);
|
|
445
|
+
log(`${helper}: downloading ${url}`);
|
|
446
|
+
const response = await (options.fetch ?? fetch)(url, { redirect: "follow" });
|
|
447
|
+
if (!response.ok || !response.body)
|
|
448
|
+
throw new HelperLockError2(`${helper}: GET ${url} answered ${response.status}`);
|
|
449
|
+
const limit = pin.bytes ?? MAX_HELPER_ARCHIVE_BYTES2;
|
|
450
|
+
const hash = createHash2("sha256");
|
|
451
|
+
const chunks = [];
|
|
452
|
+
let total = 0;
|
|
453
|
+
const reader = response.body.getReader();
|
|
454
|
+
for (;; ) {
|
|
455
|
+
const { value, done } = await reader.read();
|
|
456
|
+
if (done)
|
|
457
|
+
break;
|
|
458
|
+
total += value.byteLength;
|
|
459
|
+
if (total > limit) {
|
|
460
|
+
await reader.cancel();
|
|
461
|
+
throw new HelperLockError2(`${helper}: ${url} is larger than the pinned ${limit} bytes`);
|
|
462
|
+
}
|
|
463
|
+
hash.update(value);
|
|
464
|
+
chunks.push(value);
|
|
465
|
+
}
|
|
466
|
+
const digest = hash.digest("hex");
|
|
467
|
+
if (digest !== pin.sha256)
|
|
468
|
+
throw new HelperLockError2(`${helper}: ${pin.file} has sha256 ${digest}, not the pinned ${pin.sha256}`);
|
|
469
|
+
const target = cached ?? join(mkdtempSync(join(tmpdir(), "adea-voice-helper-")), pin.file);
|
|
470
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
471
|
+
const partial = `${target}.partial`;
|
|
472
|
+
writeFileSync(partial, Buffer.concat(chunks));
|
|
473
|
+
renameSync(partial, target);
|
|
474
|
+
return target;
|
|
475
|
+
}
|
|
476
|
+
function extractArchive(archive, outDir, helper) {
|
|
477
|
+
const scratch = mkdtempSync(join(tmpdir(), "adea-voice-extract-"));
|
|
478
|
+
try {
|
|
479
|
+
const listing = spawnSync("tar", ["-tzf", archive], { encoding: "utf8" });
|
|
480
|
+
if (listing.status !== 0)
|
|
481
|
+
throw new HelperLockError2(`${helper}: cannot read ${archive}`);
|
|
482
|
+
for (const entry of listing.stdout.split(`
|
|
483
|
+
`).filter(Boolean)) {
|
|
484
|
+
if (entry.startsWith("/") || entry.split("/").includes(".."))
|
|
485
|
+
throw new HelperLockError2(`${helper}: unsafe archive entry ${entry}`);
|
|
486
|
+
}
|
|
487
|
+
const unpack = spawnSync("tar", ["-xzf", archive, "-C", scratch], { encoding: "utf8" });
|
|
488
|
+
if (unpack.status !== 0)
|
|
489
|
+
throw new HelperLockError2(`${helper}: cannot unpack ${archive}: ${unpack.stderr}`);
|
|
490
|
+
const executable = join(scratch, helper);
|
|
491
|
+
if (!existsSync(executable) || !statSync(executable).isFile())
|
|
492
|
+
throw new HelperLockError2(`${helper}: the archive holds no ${helper}`);
|
|
493
|
+
rmSync(join(outDir, helper), { force: true });
|
|
494
|
+
renameSync(executable, join(outDir, helper));
|
|
495
|
+
const frameworks = join(scratch, "Frameworks");
|
|
496
|
+
if (existsSync(frameworks)) {
|
|
497
|
+
const destination = join(outDir, "Frameworks");
|
|
498
|
+
mkdirSync(destination, { recursive: true });
|
|
499
|
+
for (const entry of spawnSync("ls", ["-1", frameworks], { encoding: "utf8" }).stdout.split(`
|
|
500
|
+
`).filter(Boolean)) {
|
|
501
|
+
rmSync(join(destination, entry), { recursive: true, force: true });
|
|
502
|
+
renameSync(join(frameworks, entry), join(destination, entry));
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
} finally {
|
|
506
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
function stageLocal(localDir, helper, outDir) {
|
|
510
|
+
const source = join(resolve(localDir), helper);
|
|
511
|
+
if (!existsSync(source))
|
|
512
|
+
throw new HelperLockError2(`${helper} is not in the local helper directory ${localDir}`);
|
|
513
|
+
const destination = join(outDir, helper);
|
|
514
|
+
rmSync(destination, { force: true });
|
|
515
|
+
writeFileSync(destination, readFileSync2(source));
|
|
516
|
+
chmodSync(destination, 493);
|
|
517
|
+
const frameworks = join(resolve(localDir), "Frameworks");
|
|
518
|
+
if (existsSync(frameworks))
|
|
519
|
+
spawnSync("cp", ["-R", frameworks, outDir]);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
export { isMachO2, machoCodeDigest2, machoCodeDigestFile2, HELPER_LOCK_SCHEMA2, HELPER_NAMES2, HELPER_TARGETS2, MAX_HELPER_ARCHIVE_BYTES2, STAGED_RECORD_FILE2, HelperLockError2, validateHelperLock2, readHelperLock2, helperPin2, helperArtifactUrl2, currentHelperTarget2, sha256File2, stageVoiceHelpers2, verifyStagedVoiceHelpers2, readStagedRecord2, assertHandshake2 };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// src/contract/errors.ts
|
|
2
|
+
var VOICE_ERROR_CODES2 = [
|
|
3
|
+
"denied",
|
|
4
|
+
"restricted",
|
|
5
|
+
"unavailable",
|
|
6
|
+
"no_speech",
|
|
7
|
+
"language_unsupported",
|
|
8
|
+
"assets_missing",
|
|
9
|
+
"assets_install_failed",
|
|
10
|
+
"assets_locale_limit",
|
|
11
|
+
"insufficient_storage",
|
|
12
|
+
"audio_overrun",
|
|
13
|
+
"busy",
|
|
14
|
+
"cancelled",
|
|
15
|
+
"bad_request",
|
|
16
|
+
"failed"
|
|
17
|
+
];
|
|
18
|
+
var WIRE_CODES = new Set(VOICE_ERROR_CODES2);
|
|
19
|
+
var LEGACY_CODES = {
|
|
20
|
+
noSpeech: "no_speech",
|
|
21
|
+
languageUnsupported: "language_unsupported",
|
|
22
|
+
assetsMissing: "assets_missing",
|
|
23
|
+
assetsInstallFailed: "assets_install_failed",
|
|
24
|
+
assetsLocaleLimit: "assets_locale_limit",
|
|
25
|
+
insufficientStorage: "insufficient_storage",
|
|
26
|
+
audioOverrun: "audio_overrun",
|
|
27
|
+
badRequest: "bad_request"
|
|
28
|
+
};
|
|
29
|
+
function knownVoiceErrorCode(value) {
|
|
30
|
+
if (typeof value !== "string")
|
|
31
|
+
return;
|
|
32
|
+
if (WIRE_CODES.has(value))
|
|
33
|
+
return value;
|
|
34
|
+
return Object.prototype.hasOwnProperty.call(LEGACY_CODES, value) ? LEGACY_CODES[value] : undefined;
|
|
35
|
+
}
|
|
36
|
+
function normalizeVoiceErrorCode2(value) {
|
|
37
|
+
return knownVoiceErrorCode(value) ?? "failed";
|
|
38
|
+
}
|
|
39
|
+
var DEFAULT_MESSAGES = {
|
|
40
|
+
denied: "Microphone or speech-recognition permission was denied.",
|
|
41
|
+
restricted: "Microphone or speech-recognition permission is restricted.",
|
|
42
|
+
unavailable: "This device or browser cannot run the selected speech engine.",
|
|
43
|
+
no_speech: "No speech was recognized before the session ended.",
|
|
44
|
+
language_unsupported: "The selected language has no on-device speech model.",
|
|
45
|
+
assets_missing: "The speech model for this language is not installed; it must be downloaded first.",
|
|
46
|
+
assets_install_failed: "Installing the speech model assets failed.",
|
|
47
|
+
assets_locale_limit: "Too many languages are reserved for on-device speech; release one before adding another.",
|
|
48
|
+
insufficient_storage: "There is not enough storage to install the speech model assets.",
|
|
49
|
+
audio_overrun: "Audio arrived faster than the speech engine could consume it.",
|
|
50
|
+
busy: "A speech session is already running.",
|
|
51
|
+
cancelled: "The operation was cancelled.",
|
|
52
|
+
bad_request: "The request was malformed or used the API incorrectly.",
|
|
53
|
+
failed: "The speech engine reported an unexpected failure."
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
class VoiceError2 extends Error {
|
|
57
|
+
code;
|
|
58
|
+
constructor(code, message = DEFAULT_MESSAGES[code]) {
|
|
59
|
+
super(message);
|
|
60
|
+
this.name = "VoiceError";
|
|
61
|
+
this.code = code;
|
|
62
|
+
}
|
|
63
|
+
static wrapping(error, fallback = "failed") {
|
|
64
|
+
if (error instanceof VoiceError2)
|
|
65
|
+
return error;
|
|
66
|
+
if (error instanceof DOMException && error.name === "AbortError")
|
|
67
|
+
return new VoiceError2("cancelled");
|
|
68
|
+
if (error instanceof Error && error.name === "AbortError")
|
|
69
|
+
return new VoiceError2("cancelled");
|
|
70
|
+
const shaped = error !== null && typeof error === "object" ? error : undefined;
|
|
71
|
+
const bridged = knownVoiceErrorCode(shaped?.code);
|
|
72
|
+
const message = typeof shaped?.message === "string" ? shaped.message : String(error);
|
|
73
|
+
return new VoiceError2(bridged ?? fallback, message);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export { VOICE_ERROR_CODES2, normalizeVoiceErrorCode2, VoiceError2 };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@adea-ai/voice`: the root entry re-exports the platform-neutral parts (contract and settings).
|
|
3
|
+
* Platform code lives on its own subpath so a browser bundle never pulls in `node:child_process`
|
|
4
|
+
* and a desktop shell never pulls in Web Audio: `@adea-ai/voice/web`, `@adea-ai/voice/desktop`.
|
|
5
|
+
*/
|
|
6
|
+
export * from "./contract/index";
|
|
7
|
+
export * from "./settings/index";
|