@chatpanel/gateway 0.6.52 → 0.6.53

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chatpanel/gateway",
3
- "version": "0.6.52",
3
+ "version": "0.6.53",
4
4
  "description": "Local privacy gateway — redacts PII out of OpenAI/Anthropic API traffic before it reaches a model, then restores it in the reply. Point opencode, codex, aider, Claude Code, etc. at it.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/server.js CHANGED
@@ -42,6 +42,7 @@ import { STT_MODEL_CATALOG, isKnownSttModel, isValidCustomSttId, DEFAULT_STT_MOD
42
42
  import * as ttsEngine from './tts-engine.js';
43
43
  import { TTS_MODEL_CATALOG, TTS_VOICES, isKnownTtsModel, isValidCustomTtsId, isKnownVoice, isValidVoiceId, DEFAULT_TTS_MODEL, DEFAULT_TTS_VOICE, TTS_DTYPES, isValidTtsDtype, MAX_TTS_CHARS } from './tts-models.js';
44
44
  import { ttsDestination, synthesizeRemote, isValidRemoteVoice } from './tts-remote.js';
45
+ import * as ttsVoices from './tts-voices.js';
45
46
  import { resolvePro, checkQuota, consume, usage } from './freegate.js';
46
47
  import { publicConfig, applyConfigPatch, applyNerModelSelection, persistConfig, configPath } from './configstore.js';
47
48
  import { resolveDestination, aggregateModelsAsync, listDestinations } from './router.js';
@@ -52,7 +53,7 @@ import * as openai from './openai.js';
52
53
  import * as responses from './responses.js';
53
54
  import * as anthropic from './anthropic.js';
54
55
 
55
- export const VERSION = '0.6.52';
56
+ export const VERSION = '0.6.53';
56
57
 
57
58
  // WARM search tier — SQLite + FTS5 record store (falls back to an encrypted-JSON
58
59
  // store if SQLite can't load), fed by the extension's ingest sync + backup-ingest.
@@ -986,6 +987,7 @@ export function createGateway(cfg = loadConfig()) {
986
987
  // hide the picker rather than offer choices that cannot take effect.
987
988
  arch: ttsEngine.arch(),
988
989
  supportsVoices: ttsEngine.supportsVoices(),
990
+ supportsCustomVoices: ttsEngine.supportsCustomVoices(),
989
991
  sampleRate: ttsEngine.sampleRate(),
990
992
  voices: ttsEngine.arch() === 'vits' ? [] : TTS_VOICES.map((v) => ({ ...v, installed: ttsEngine.voiceOnDisk(v.id, active) })),
991
993
  dtype: cfg.tts?.dtype || 'auto',
@@ -1001,8 +1003,14 @@ export function createGateway(cfg = loadConfig()) {
1001
1003
  if (id && !(isKnownTtsModel(id) || isValidCustomTtsId(id))) return sendJson(res, 400, { error: { message: 'unknown or invalid model id', type: 'bad_model' } });
1002
1004
  // A voice id becomes a filename, so it is checked against the catalog AND
1003
1005
  // its shape before it is ever persisted.
1006
+ // A default voice may be a built-in Kokoro one OR a saved custom one; both
1007
+ // live in the same field, so both shapes are accepted and both validated.
1004
1008
  const voice = body && typeof body.voice === 'string' ? body.voice.trim() : null;
1005
- if (voice && !(isKnownVoice(voice) && isValidVoiceId(voice))) return sendJson(res, 400, { error: { message: 'unknown or invalid voice', type: 'bad_voice' } });
1009
+ if (voice) {
1010
+ const cid = ttsVoices.parseCustomVoice(voice);
1011
+ const okVoice = cid ? !!ttsVoices.getVoice(cid) : (isKnownVoice(voice) && isValidVoiceId(voice));
1012
+ if (!okVoice) return sendJson(res, 400, { error: { message: 'unknown or invalid voice', type: 'bad_voice' } });
1013
+ }
1006
1014
  const dtype = body && typeof body.dtype === 'string' && isValidTtsDtype(body.dtype) ? body.dtype : undefined;
1007
1015
  if (!cfg.tts) cfg.tts = { enabled: true, model: DEFAULT_TTS_MODEL, voice: DEFAULT_TTS_VOICE, allowDownload: true };
1008
1016
  if (id) cfg.tts.model = id;
@@ -1014,6 +1022,59 @@ export function createGateway(cfg = loadConfig()) {
1014
1022
  }
1015
1023
  }
1016
1024
 
1025
+ // --- Custom voices: a speaker embedding derived from a sample the user
1026
+ // recorded. The AUDIO is embedded in-process and then discarded; only the 512
1027
+ // floats are stored, under ~/.chatpanel, and they never leave this machine.
1028
+ // See src/tts-voices.js for why the rules here are tighter than elsewhere.
1029
+ if (pathname === '/tts/voices') {
1030
+ if (req.method === 'GET') {
1031
+ return sendJson(res, 200, {
1032
+ voices: ttsVoices.listVoices(),
1033
+ // Whether a saved voice can actually be USED right now depends on the
1034
+ // active model — only SpeechT5 takes an embedding. Saying so here stops
1035
+ // the UI offering voices that would be silently ignored.
1036
+ usable: ttsEngine.supportsCustomVoices(),
1037
+ embedder: diarizeEngine.DIARIZE_MODEL,
1038
+ embedderReady: diarizeEngine.isReady(),
1039
+ });
1040
+ }
1041
+ if (req.method === 'POST') {
1042
+ let body = null;
1043
+ try { body = JSON.parse((await readBody(req, cfg.maxBodyBytes)).toString('utf8')); } catch { body = null; }
1044
+ const name = body && typeof body.name === 'string' ? body.name.trim() : '';
1045
+ const pcm = body && Array.isArray(body.pcm) ? body.pcm : null;
1046
+ if (!name) return sendJson(res, 400, { error: { message: 'a name is required', type: 'bad_request' } });
1047
+ if (!pcm || pcm.length < 16000) {
1048
+ // Under a second of audio produces an embedding dominated by whatever
1049
+ // noise happened to be in it, and the resulting voice is arbitrary.
1050
+ return sendJson(res, 400, { error: { message: 'need at least 1 second of 16 kHz mono audio', type: 'sample_too_short' } });
1051
+ }
1052
+ try {
1053
+ // The embedder is the speaker model diarization already uses. If it is
1054
+ // not resident yet, start it and say so — a ~100 MB download is not
1055
+ // something to do silently while the user waits on a spinner.
1056
+ if (!diarizeEngine.isReady()) {
1057
+ diarizeEngine.download({ onLog: (m) => console.log(m) });
1058
+ return sendJson(res, 503, {
1059
+ error: { message: 'the speaker model is downloading (~100 MB) — try again in a moment', type: 'embedder_not_ready' },
1060
+ progress: diarizeEngine.progress(),
1061
+ });
1062
+ }
1063
+ const vec = await diarizeEngine.embed(Float32Array.from(pcm));
1064
+ const saved = ttsVoices.saveVoice({ name, vec });
1065
+ console.log(`[tts] saved custom voice "${saved.name}" (${saved.dim}-d, sample discarded)`);
1066
+ return sendJson(res, 201, { ...saved, usable: ttsEngine.supportsCustomVoices() });
1067
+ } catch (e) {
1068
+ return sendJson(res, 400, { error: { message: e.message, type: 'save_failed' } });
1069
+ }
1070
+ }
1071
+ if (req.method === 'DELETE') {
1072
+ const id = url.searchParams.get('id') || '';
1073
+ // Deleting someone's voice print is not a soft delete — the file is gone.
1074
+ return sendJson(res, 200, { deleted: ttsVoices.deleteVoice(id) });
1075
+ }
1076
+ }
1077
+
1017
1078
  // POST /tts — { text, voice?, speed? } → audio/wav — and its OpenAI-compatible
1018
1079
  // twin POST /v1/audio/speech ({ input, voice, speed, response_format }), so any
1019
1080
  // OpenAI client or tunnel can drive local speech with no ChatPanel-specific code.
@@ -1037,7 +1098,21 @@ export function createGateway(cfg = loadConfig()) {
1037
1098
  // not a Kokoro one), so the local catalog check would reject every valid id.
1038
1099
  const rawVoice = body && typeof body.voice === 'string' && body.voice.trim() ? body.voice.trim() : null;
1039
1100
  const voice = rawVoice || (dest ? dest.voice : (cfg.tts?.voice || DEFAULT_TTS_VOICE));
1040
- const voiceOk = dest ? isValidRemoteVoice(voice) : (isKnownVoice(voice) && isValidVoiceId(voice));
1101
+ // `custom:<id>` names a saved voice. It is resolved to an embedding here so
1102
+ // the engine never has to know where voices are stored.
1103
+ const customId = dest ? null : ttsVoices.parseCustomVoice(voice);
1104
+ let speakerEmbedding = null;
1105
+ if (customId) {
1106
+ const rec = ttsVoices.getVoice(customId);
1107
+ if (!rec) return sendJson(res, 404, { error: { message: 'no such saved voice', type: 'bad_voice' } });
1108
+ if (!ttsEngine.supportsCustomVoices() && ttsEngine.isReady()) {
1109
+ return sendJson(res, 409, { error: { message: `the active model (${ttsEngine.arch()}) cannot use a recorded voice — switch to SpeechT5`, type: 'voice_unsupported' } });
1110
+ }
1111
+ speakerEmbedding = rec.vec;
1112
+ }
1113
+ const voiceOk = customId ? true
1114
+ : dest ? isValidRemoteVoice(voice)
1115
+ : (isKnownVoice(voice) && isValidVoiceId(voice));
1041
1116
  if (!voiceOk) return sendJson(res, 400, { error: { message: 'unknown or invalid voice', type: 'bad_voice' } });
1042
1117
  // We synthesize WAV only. Say so rather than returning WAV bytes under an mp3
1043
1118
  // content-type — a client that trusts the header would play noise.
@@ -1073,7 +1148,7 @@ export function createGateway(cfg = loadConfig()) {
1073
1148
  dtype: cfg.tts?.dtype || 'auto',
1074
1149
  });
1075
1150
  if (!ok) return sendJson(res, 503, { error: { message: ttsEngine.health().error || 'tts model not ready', type: 'tts_unavailable' } });
1076
- const pcm = await ttsEngine.synth(text, { voice, speed });
1151
+ const pcm = await ttsEngine.synth(text, { voice, speed, speakerEmbedding });
1077
1152
  // The ACTIVE model's rate, not the constant: a VITS/MMS model emits 16 kHz
1078
1153
  // and writing it into a 24 kHz header plays it fast and chipmunked.
1079
1154
  const rate = ttsEngine.sampleRate();
package/src/tts-engine.js CHANGED
@@ -41,14 +41,22 @@ let _dtype = null;
41
41
  let _err = null;
42
42
  let _progress = null;
43
43
  let _initPromise = null;
44
- let _arch = null; // 'style-tts2' (Kokoro) | 'vits' (MMS et al)
44
+ let _arch = null; // 'style-tts2' (Kokoro) | 'vits' (MMS) | 'speecht5' (custom voices)
45
+ let _vocoder = null; // speecht5 only — mel → waveform
45
46
  let _rate = SAMPLE_RATE; // the ACTIVE model's output rate
46
47
  const _voices = new Map(); // voice id → Float32Array style bank
47
48
 
48
49
  // The architectures this engine can actually drive. Anything else is refused at
49
50
  // load with a message naming what it is, rather than failing later inside a
50
51
  // forward pass with a shape error nobody can act on.
51
- export const SUPPORTED_ARCH = { style_text_to_speech_2: 'style-tts2', vits: 'vits' };
52
+ export const SUPPORTED_ARCH = { style_text_to_speech_2: 'style-tts2', vits: 'vits', speecht5: 'speecht5' };
53
+
54
+ // SpeechT5 is the only architecture here that takes a SPEAKER EMBEDDING, which is
55
+ // what makes a custom voice possible at all: Kokoro's voices are fixed style banks
56
+ // and VITS is single-speaker, so neither can be pointed at a person. It needs a
57
+ // separate vocoder (mel → waveform), hence the extra model id.
58
+ export const SPEECHT5_VOCODER = 'Xenova/speecht5_hifigan';
59
+ export function supportsCustomVoices() { return _arch === 'speecht5'; }
52
60
 
53
61
  export function arch() { return _arch; }
54
62
  export function sampleRate() { return _rate; }
@@ -61,7 +69,7 @@ export function isReady() { return _state === 'ready' && !!_net; }
61
69
  export function progress() { return _progress; }
62
70
 
63
71
  export function health() {
64
- return { configured: _state !== 'off', ok: isReady(), state: _state, model: _model, dtype: _dtype, runtime: runtimeName(), error: _err, arch: _arch, sampleRate: _rate, voices: supportsVoices() };
72
+ return { configured: _state !== 'off', ok: isReady(), state: _state, model: _model, dtype: _dtype, runtime: runtimeName(), error: _err, arch: _arch, sampleRate: _rate, voices: supportsVoices(), customVoices: supportsCustomVoices() };
65
73
  }
66
74
 
67
75
  export function modelDir(modelId) {
@@ -151,15 +159,22 @@ async function loadModel(modelId, { log = () => {}, allowDownload = true, dtype:
151
159
  const onProgress = (p) => {
152
160
  if (p?.status === 'progress' && p.file) _progress = { model: modelId, file: p.file, pct: Math.round(p.progress || 0) };
153
161
  };
154
- const Klass = kind === 'vits' ? tf.VitsModel : tf.StyleTextToSpeech2Model;
162
+ const Klass = kind === 'vits' ? tf.VitsModel
163
+ : kind === 'speecht5' ? tf.SpeechT5ForTextToSpeech
164
+ : tf.StyleTextToSpeech2Model;
155
165
  const [net, tok] = await Promise.all([
156
166
  Klass.from_pretrained(modelId, { dtype, progress_callback: onProgress }),
157
167
  tf.AutoTokenizer.from_pretrained(modelId),
158
168
  ]);
169
+ // The vocoder is a second download and a second failure point, so it is loaded
170
+ // only for the architecture that needs one.
171
+ _vocoder = kind === 'speecht5'
172
+ ? await tf.SpeechT5HifiGan.from_pretrained(SPEECHT5_VOCODER, { dtype, progress_callback: onProgress })
173
+ : null;
159
174
  _net = net; _tok = tok; _model = modelId; _dtype = dtype; _arch = kind;
160
175
  // VITS/MMS emit 16 kHz; Kokoro 24 kHz. Take it from the model's own config
161
176
  // where it says so, because guessing wrong plays the voice at the wrong pitch.
162
- _rate = Number(net?.config?.sampling_rate) || (kind === 'vits' ? 16000 : SAMPLE_RATE);
177
+ _rate = Number(net?.config?.sampling_rate) || (kind === 'style-tts2' ? SAMPLE_RATE : 16000);
163
178
  _state = 'ready'; _err = null; _progress = null;
164
179
  log(`[tts] ready — model ${modelId} @ ${dtype} (${kind}, ${_rate} Hz, ${runtimeName()}, offline) — local speech active`);
165
180
  return true;
@@ -260,7 +275,7 @@ export function splitSentences(text, maxChars = 300) {
260
275
  * Synthesize ONE chunk. Returns 24 kHz mono Float32 PCM.
261
276
  * @param {string} text @param {{voice?: string, speed?: number}} [opts]
262
277
  */
263
- export async function synthChunk(text, { voice = DEFAULT_TTS_VOICE, speed = 1 } = {}) {
278
+ export async function synthChunk(text, { voice = DEFAULT_TTS_VOICE, speed = 1, speakerEmbedding = null } = {}) {
264
279
  if (!isReady()) throw new Error('tts model not ready');
265
280
  const tf = await import('@huggingface/transformers');
266
281
 
@@ -273,6 +288,20 @@ export async function synthChunk(text, { voice = DEFAULT_TTS_VOICE, speed = 1 }
273
288
  return out.waveform.data;
274
289
  }
275
290
 
291
+ // SpeechT5: conditioned by a 512-d speaker embedding, which is the whole point —
292
+ // it is the one architecture here that can be pointed at a person's voice.
293
+ // Without an embedding there is no voice to speak in, so this refuses rather
294
+ // than inventing one.
295
+ if (_arch === 'speecht5') {
296
+ if (!speakerEmbedding || speakerEmbedding.length !== 512) {
297
+ throw new Error('this model needs a saved voice — record one in Settings → Text-to-speech');
298
+ }
299
+ const { input_ids } = _tok(String(text));
300
+ const emb = new tf.Tensor('float32', Float32Array.from(speakerEmbedding), [1, 512]);
301
+ const { waveform } = await _net.generate_speech(input_ids, emb, { vocoder: _vocoder });
302
+ return waveform.data;
303
+ }
304
+
276
305
  const { phonemize } = await import('phonemizer');
277
306
 
278
307
  // G2P follows the VOICE, not the request — an American voice reading British
@@ -294,11 +323,11 @@ export async function synthChunk(text, { voice = DEFAULT_TTS_VOICE, speed = 1 }
294
323
  }
295
324
 
296
325
  /** Synthesize arbitrary-length text, chunk by chunk. `onChunk` sees each as it lands. */
297
- export async function synth(text, { voice = DEFAULT_TTS_VOICE, speed = 1, onChunk = null } = {}) {
326
+ export async function synth(text, { voice = DEFAULT_TTS_VOICE, speed = 1, speakerEmbedding = null, onChunk = null } = {}) {
298
327
  const chunks = splitSentences(text);
299
328
  const out = [];
300
329
  for (const c of chunks) {
301
- const pcm = await synthChunk(c, { voice, speed });
330
+ const pcm = await synthChunk(c, { voice, speed, speakerEmbedding });
302
331
  out.push(pcm);
303
332
  onChunk?.(pcm);
304
333
  }
@@ -337,6 +366,6 @@ export function toWav(pcm, sampleRate = SAMPLE_RATE) {
337
366
 
338
367
  export function _reset() {
339
368
  _state = 'off'; _model = null; _net = null; _tok = null; _dtype = null;
340
- _err = null; _progress = null; _initPromise = null; _arch = null; _rate = SAMPLE_RATE;
369
+ _err = null; _progress = null; _initPromise = null; _arch = null; _rate = SAMPLE_RATE; _vocoder = null;
341
370
  _voices.clear();
342
371
  }
package/src/tts-models.js CHANGED
@@ -55,6 +55,27 @@ export const TTS_MODEL_CATALOG = [
55
55
  voices: true,
56
56
  note: 'The Mandarin-tuned Kokoro. Same engine and voice mechanism as v1.0.',
57
57
  },
58
+ {
59
+ id: 'Xenova/speecht5_tts',
60
+ label: 'SpeechT5 — your own voice',
61
+ lang: 'English',
62
+ tier: 'custom',
63
+ arch: 'speecht5',
64
+ // PINNED to fp32, and not as a preference. At the runtime default (q8 on
65
+ // native) this model renders about half of all speaker embeddings as
66
+ // near-silence — measured: 91-92% of samples under the noise floor, where the
67
+ // same embeddings at fp32 produce clean speech at ~47% silence, which is just
68
+ // the pauses between words. The quantization is destroying the speaker
69
+ // conditioning, and the failure looks exactly like "voice cloning does not
70
+ // work" rather than "the precision is wrong".
71
+ dtype: 'fp32',
72
+ approxMB: 190, // plus the ~50 MB HiFi-GAN vocoder it needs
73
+ ramMB: 500,
74
+ sampleRate: 16000,
75
+ voices: false, // no built-in voices…
76
+ customVoices: true, // …but it is the ONE model here that can use yours
77
+ note: 'The only model here that speaks in a voice you record. Rougher than Kokoro, and the match is approximate — see the note under Your voices.',
78
+ },
58
79
  {
59
80
  // One MMS entry so the second architecture is DISCOVERABLE from the list rather
60
81
  // than only findable by search. The rest of the family (~1000 languages) is
@@ -123,6 +144,12 @@ export function ttsModelHasVoices(id) {
123
144
  return m ? m.voices !== false : true;
124
145
  }
125
146
 
147
+ // Can it speak in a voice the user recorded? Only SpeechT5 takes a speaker
148
+ // embedding — the other two are conditioned on something fixed.
149
+ export function ttsModelHasCustomVoices(id) {
150
+ return ttsModel(id)?.customVoices === true;
151
+ }
152
+
126
153
  export function ttsModel(id) {
127
154
  return TTS_MODEL_CATALOG.find((m) => m.id === id) || null;
128
155
  }
@@ -0,0 +1,99 @@
1
+ // Custom voices — a speaker embedding derived from a sample the user recorded.
2
+ //
3
+ // A voice print is biometric-adjacent data about a specific person, so the rules
4
+ // here are deliberately tighter than for anything else this gateway stores:
5
+ // • It NEVER leaves the machine. The embedding is computed in-process by the
6
+ // speaker model already on disk (the one diarization uses), written under
7
+ // ~/.chatpanel, and read back only to condition local synthesis.
8
+ // • The SAMPLE is never persisted. Only the 512 floats derived from it — audio
9
+ // of someone speaking is far more revealing than the vector, and keeping it
10
+ // would serve no purpose once the embedding exists.
11
+ // • Deleting means the file is gone, not flagged. There is no soft-delete for
12
+ // someone's voice.
13
+ //
14
+ // What this can and cannot do is stated plainly in the UI, and the reason lives
15
+ // here: SpeechT5 was trained against speechbrain x-vectors and we embed with
16
+ // wavlm-base-plus-sv, which is a different 512-d space. The result is a distinct,
17
+ // STABLE voice derived from the sample — the same sample always gives the same
18
+ // voice, and different speakers give clearly different ones — but it is not a
19
+ // faithful reproduction of the speaker. Calling that "cloning" without saying so
20
+ // would be a lie told by a feature name.
21
+
22
+ import { existsSync, mkdirSync, readFileSync, writeFileSync, readdirSync, rmSync } from 'node:fs';
23
+ import { join } from 'node:path';
24
+ import { randomUUID } from 'node:crypto';
25
+ import os from 'node:os';
26
+
27
+ export const EMBED_DIM = 512;
28
+ const MAX_VOICES = 20;
29
+ const MAX_NAME = 60;
30
+
31
+ export function voicesDir() {
32
+ return process.env.CHATPANEL_TTS_VOICES || join(os.homedir(), '.chatpanel', 'tts-voices');
33
+ }
34
+
35
+ // An id becomes a FILENAME and arrives from the wire, so it is shape-checked, not
36
+ // trusted. UUIDs are what we mint; the pattern is what we accept.
37
+ export function isValidVoiceRef(id) {
38
+ return /^[A-Za-z0-9_-]{1,64}$/.test(String(id || ''));
39
+ }
40
+
41
+ // The wire form is `custom:<id>` so one `voice` field can name either a built-in
42
+ // Kokoro voice or a saved one, with no ambiguity between the two namespaces.
43
+ export function parseCustomVoice(voice) {
44
+ const s = String(voice || '');
45
+ if (!s.startsWith('custom:')) return null;
46
+ const id = s.slice(7);
47
+ return isValidVoiceRef(id) ? id : null;
48
+ }
49
+
50
+ function fileFor(id) {
51
+ return join(voicesDir(), `${id}.json`);
52
+ }
53
+
54
+ export function listVoices() {
55
+ const dir = voicesDir();
56
+ if (!existsSync(dir)) return [];
57
+ const out = [];
58
+ for (const f of readdirSync(dir)) {
59
+ if (!f.endsWith('.json')) continue;
60
+ try {
61
+ const v = JSON.parse(readFileSync(join(dir, f), 'utf8'));
62
+ // The vector is deliberately NOT returned by the listing: the UI needs a
63
+ // name and an id, and 512 floats of someone's voice print have no business
64
+ // in a settings page's JSON.
65
+ if (v?.id && v?.name) out.push({ id: v.id, name: v.name, createdAt: v.createdAt || 0, dim: v.vec?.length || 0 });
66
+ } catch { /* a corrupt file must not break the list */ }
67
+ }
68
+ return out.sort((a, b) => b.createdAt - a.createdAt);
69
+ }
70
+
71
+ export function getVoice(id) {
72
+ if (!isValidVoiceRef(id)) return null;
73
+ try {
74
+ const v = JSON.parse(readFileSync(fileFor(id), 'utf8'));
75
+ return Array.isArray(v?.vec) && v.vec.length === EMBED_DIM ? v : null;
76
+ } catch { return null; }
77
+ }
78
+
79
+ /** Persist an embedding under a user-chosen name. Returns the stored record. */
80
+ export function saveVoice({ name, vec }) {
81
+ const clean = String(name || '').trim().slice(0, MAX_NAME);
82
+ if (!clean) throw new Error('a name is required');
83
+ if (!vec || vec.length !== EMBED_DIM) throw new Error(`expected a ${EMBED_DIM}-value embedding, got ${vec?.length || 0}`);
84
+ if (listVoices().length >= MAX_VOICES) throw new Error(`at most ${MAX_VOICES} saved voices — delete one first`);
85
+ const dir = voicesDir();
86
+ mkdirSync(dir, { recursive: true });
87
+ const rec = { id: randomUUID(), name: clean, createdAt: Date.now(), vec: Array.from(vec, (x) => Number(x) || 0) };
88
+ writeFileSync(fileFor(rec.id), JSON.stringify(rec));
89
+ return { id: rec.id, name: rec.name, createdAt: rec.createdAt, dim: rec.vec.length };
90
+ }
91
+
92
+ /** Remove one permanently. Returns whether there was anything to remove. */
93
+ export function deleteVoice(id) {
94
+ if (!isValidVoiceRef(id)) return false;
95
+ const p = fileFor(id);
96
+ if (!existsSync(p)) return false;
97
+ rmSync(p, { force: true });
98
+ return true;
99
+ }