@hyperwindmill/caretaker-cli 0.29.0 → 0.30.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/dist/agents/dispatch.d.ts.map +1 -1
- package/dist/agents/dispatch.js +4 -1
- package/dist/cli/web/server.d.ts.map +1 -1
- package/dist/cli/web/server.js +29 -7
- package/dist/cli/web/session_scope.d.ts +11 -0
- package/dist/cli/web/session_scope.d.ts.map +1 -0
- package/dist/cli/web/session_scope.js +13 -0
- package/dist/cli/web/turn_guard.d.ts +9 -0
- package/dist/cli/web/turn_guard.d.ts.map +1 -0
- package/dist/cli/web/turn_guard.js +18 -0
- package/dist/cli/web/voice_builtin.d.ts +3 -0
- package/dist/cli/web/voice_builtin.d.ts.map +1 -0
- package/dist/cli/web/voice_builtin.js +55 -0
- package/dist/cli/web/voice_proxy.d.ts +33 -1
- package/dist/cli/web/voice_proxy.d.ts.map +1 -1
- package/dist/cli/web/voice_proxy.js +173 -27
- package/dist/commands/loader.js +2 -2
- package/dist/harness/tools/builtin/index.d.ts.map +1 -1
- package/dist/harness/tools/builtin/index.js +4 -0
- package/dist/harness/tools/builtin/set_workspace.d.ts +3 -0
- package/dist/harness/tools/builtin/set_workspace.d.ts.map +1 -0
- package/dist/harness/tools/builtin/set_workspace.js +49 -0
- package/dist/plugins/manifest.d.ts +1 -1
- package/dist/plugins/manifest.js +1 -1
- package/dist/session/store.d.ts +6 -0
- package/dist/session/store.d.ts.map +1 -1
- package/dist/session/store.js +8 -0
- package/dist/session/types.d.ts +4 -0
- package/dist/session/types.d.ts.map +1 -1
- package/dist/voice/pocket/catalog.d.ts +24 -0
- package/dist/voice/pocket/catalog.d.ts.map +1 -0
- package/dist/voice/pocket/catalog.js +152 -0
- package/dist/voice/pocket/engine.d.ts +17 -0
- package/dist/voice/pocket/engine.d.ts.map +1 -0
- package/dist/voice/pocket/engine.js +145 -0
- package/dist/voice/pocket/inference.d.ts +36 -0
- package/dist/voice/pocket/inference.d.ts.map +1 -0
- package/dist/voice/pocket/inference.js +228 -0
- package/dist/voice/pocket/install.d.ts +54 -0
- package/dist/voice/pocket/install.d.ts.map +1 -0
- package/dist/voice/pocket/install.js +275 -0
- package/dist/voice/pocket/tokenizer.d.ts +42 -0
- package/dist/voice/pocket/tokenizer.d.ts.map +1 -0
- package/dist/voice/pocket/tokenizer.js +279 -0
- package/dist/voice/pocket/voices.d.ts +9 -0
- package/dist/voice/pocket/voices.d.ts.map +1 -0
- package/dist/voice/pocket/voices.js +88 -0
- package/dist/voice/pronunciation.d.ts +16 -0
- package/dist/voice/pronunciation.d.ts.map +1 -0
- package/dist/voice/pronunciation.js +51 -0
- package/dist/webview/standalone.js +1508 -701
- package/dist/webview/standalone.js.map +4 -4
- package/package.json +3 -3
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
// Text front end of a Pocket TTS schema-3 bundle: the HF `tokenizers` Unigram model of
|
|
2
|
+
// tokenizer.json (Prepend "▁" normalizer, Metaspace pre-tokenizer, byte fallback, no
|
|
3
|
+
// post-processor) and upstream's prompt preparation and sentence chunking
|
|
4
|
+
// (pocket_tts/models/text_chunking.py at the bundle's upstream commit), ported literally.
|
|
5
|
+
const SPACE = '▁';
|
|
6
|
+
// HF tokenizers' unigram K_UNK_PENALTY.
|
|
7
|
+
const UNK_PENALTY = 10;
|
|
8
|
+
export class UnigramTokenizer {
|
|
9
|
+
pieces = new Map();
|
|
10
|
+
scores;
|
|
11
|
+
byteIds = [];
|
|
12
|
+
unkId;
|
|
13
|
+
unkScore;
|
|
14
|
+
maxPieceLen;
|
|
15
|
+
vocab;
|
|
16
|
+
constructor(json) {
|
|
17
|
+
const m = json.model;
|
|
18
|
+
if (m.type !== 'Unigram')
|
|
19
|
+
throw new Error(`unsupported tokenizer model: ${m.type}`);
|
|
20
|
+
this.vocab = m.vocab.map(([p]) => p);
|
|
21
|
+
this.scores = m.vocab.map(([, s]) => s);
|
|
22
|
+
this.unkId = m.unk_id;
|
|
23
|
+
this.unkScore = Math.min(...this.scores) - UNK_PENALTY;
|
|
24
|
+
let maxLen = 1;
|
|
25
|
+
m.vocab.forEach(([piece], id) => {
|
|
26
|
+
const byte = /^<0x([0-9A-F]{2})>$/.exec(piece);
|
|
27
|
+
if (byte) {
|
|
28
|
+
this.byteIds[parseInt(byte[1], 16)] = id;
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
// Special pieces (<unk>, <s>, …) are never matched inside text. Divergence from HF,
|
|
32
|
+
// which would split them out as added tokens: irrelevant for speech input.
|
|
33
|
+
if (/^<.*>$/.test(piece) && this.scores[id] === 0)
|
|
34
|
+
return;
|
|
35
|
+
this.pieces.set(piece, id);
|
|
36
|
+
maxLen = Math.max(maxLen, piece.length);
|
|
37
|
+
});
|
|
38
|
+
this.maxPieceLen = maxLen;
|
|
39
|
+
}
|
|
40
|
+
encode(text) {
|
|
41
|
+
if (text === '')
|
|
42
|
+
return [];
|
|
43
|
+
// Prepend normalizer, then Metaspace (spaces -> ▁, no second ▁ since one is there,
|
|
44
|
+
// split before every ▁).
|
|
45
|
+
const normalized = SPACE + text.replace(/ /g, SPACE);
|
|
46
|
+
const ids = [];
|
|
47
|
+
for (const word of normalized.match(/▁[^▁]*|[^▁]+/g) ?? [])
|
|
48
|
+
this.encodeWord(word, ids);
|
|
49
|
+
return ids;
|
|
50
|
+
}
|
|
51
|
+
// Viterbi over one pre-token: maximize the summed piece scores. Positions are UTF-16
|
|
52
|
+
// indices kept on code-point boundaries; ties keep the earlier (shorter) candidate, as HF.
|
|
53
|
+
encodeWord(word, out) {
|
|
54
|
+
const n = word.length;
|
|
55
|
+
const best = new Float64Array(n + 1).fill(-Infinity);
|
|
56
|
+
const from = new Int32Array(n + 1).fill(-1);
|
|
57
|
+
const via = new Int32Array(n + 1).fill(-1);
|
|
58
|
+
best[0] = 0;
|
|
59
|
+
for (let start = 0; start < n; start = nextBoundary(word, start)) {
|
|
60
|
+
const charEnd = nextBoundary(word, start);
|
|
61
|
+
let single = false;
|
|
62
|
+
for (let end = start + 1; end <= Math.min(n, start + this.maxPieceLen); end++) {
|
|
63
|
+
if (end < n && isLowSurrogate(word.charCodeAt(end)))
|
|
64
|
+
continue;
|
|
65
|
+
const id = this.pieces.get(word.slice(start, end));
|
|
66
|
+
if (id === undefined)
|
|
67
|
+
continue;
|
|
68
|
+
const score = best[start] + this.scores[id];
|
|
69
|
+
if (from[end] < 0 || score > best[end]) {
|
|
70
|
+
best[end] = score;
|
|
71
|
+
from[end] = start;
|
|
72
|
+
via[end] = id;
|
|
73
|
+
}
|
|
74
|
+
if (end === charEnd)
|
|
75
|
+
single = true;
|
|
76
|
+
}
|
|
77
|
+
if (!single) {
|
|
78
|
+
const score = best[start] + this.unkScore;
|
|
79
|
+
if (from[charEnd] < 0 || score > best[charEnd]) {
|
|
80
|
+
best[charEnd] = score;
|
|
81
|
+
from[charEnd] = start;
|
|
82
|
+
via[charEnd] = this.unkId;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// Backtrack; consecutive unknown spans are fused (HF fuse_unk) and then byte-encoded.
|
|
87
|
+
const tokens = [];
|
|
88
|
+
for (let end = n; end > 0; end = from[end]) {
|
|
89
|
+
const id = via[end];
|
|
90
|
+
const text = word.slice(from[end], end);
|
|
91
|
+
const last = tokens[tokens.length - 1];
|
|
92
|
+
if (id === this.unkId && last?.id === this.unkId)
|
|
93
|
+
last.text = text + last.text;
|
|
94
|
+
else
|
|
95
|
+
tokens.push({ id, text });
|
|
96
|
+
}
|
|
97
|
+
for (let i = tokens.length - 1; i >= 0; i--) {
|
|
98
|
+
const t = tokens[i];
|
|
99
|
+
if (t.id !== this.unkId) {
|
|
100
|
+
out.push(t.id);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const bytes = Buffer.from(t.text, 'utf8');
|
|
104
|
+
const byteIds = [...bytes].map((b) => this.byteIds[b]);
|
|
105
|
+
if (byteIds.every((id) => id !== undefined))
|
|
106
|
+
out.push(...byteIds);
|
|
107
|
+
else
|
|
108
|
+
out.push(this.unkId);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// Decoder chain Metaspace + ByteFallback + Fuse.
|
|
112
|
+
decode(ids) {
|
|
113
|
+
let out = '';
|
|
114
|
+
let bytes = [];
|
|
115
|
+
const flush = () => {
|
|
116
|
+
if (bytes.length === 0)
|
|
117
|
+
return;
|
|
118
|
+
try {
|
|
119
|
+
out += new TextDecoder('utf-8', { fatal: true }).decode(Uint8Array.from(bytes));
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
out += '�'.repeat(bytes.length);
|
|
123
|
+
}
|
|
124
|
+
bytes = [];
|
|
125
|
+
};
|
|
126
|
+
ids.forEach((id, i) => {
|
|
127
|
+
const piece = this.vocab[id] ?? '';
|
|
128
|
+
const byte = /^<0x([0-9A-F]{2})>$/.exec(piece);
|
|
129
|
+
if (byte) {
|
|
130
|
+
bytes.push(parseInt(byte[1], 16));
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
flush();
|
|
134
|
+
out += i === 0 ? piece.split(SPACE).join('') : piece.split(SPACE).join(' ');
|
|
135
|
+
});
|
|
136
|
+
flush();
|
|
137
|
+
return out;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
function isLowSurrogate(c) {
|
|
141
|
+
return c >= 0xdc00 && c <= 0xdfff;
|
|
142
|
+
}
|
|
143
|
+
function nextBoundary(s, i) {
|
|
144
|
+
return i + 1 < s.length && isLowSurrogate(s.charCodeAt(i + 1)) ? i + 2 : i + 1;
|
|
145
|
+
}
|
|
146
|
+
const TERMINAL = '.!?…';
|
|
147
|
+
const WEAK = ',;:-–—';
|
|
148
|
+
const CLOSERS = '"\'”’)]»';
|
|
149
|
+
// Python str.split() with no argument.
|
|
150
|
+
function pySplit(s) {
|
|
151
|
+
return s.split(/\s+/).filter(Boolean);
|
|
152
|
+
}
|
|
153
|
+
function rstripChars(s, chars) {
|
|
154
|
+
let end = s.length;
|
|
155
|
+
while (end > 0 && chars.includes(s[end - 1]))
|
|
156
|
+
end--;
|
|
157
|
+
return s.slice(0, end);
|
|
158
|
+
}
|
|
159
|
+
// upstream prepare_text_prompt: returns the prompt and the frames-after-EOS guess.
|
|
160
|
+
export function prepareTextPrompt(input, cfg) {
|
|
161
|
+
let text = input.trim();
|
|
162
|
+
const map = cfg.replace_characters ?? {};
|
|
163
|
+
if (Object.keys(map).length > 0) {
|
|
164
|
+
text = pySplit([...text].map((c) => map[c] ?? c).join('')).join(' ');
|
|
165
|
+
text = text.replace(/([.!?…])\s*[,;:]/g, '$1');
|
|
166
|
+
}
|
|
167
|
+
if (text === '')
|
|
168
|
+
throw new Error('Text prompt cannot be empty');
|
|
169
|
+
text = text.replace(/\n/g, ' ').replace(/\r/g, ' ').split(' ').join(' ');
|
|
170
|
+
if (cfg.remove_semicolons)
|
|
171
|
+
text = text.replace(/;/g, ',');
|
|
172
|
+
const framesAfterEosGuess = pySplit(text).length <= 4 ? 3 : 1;
|
|
173
|
+
// Python's `if not text[0].isupper(): upper()` is the same as always upper-casing.
|
|
174
|
+
if (cfg.capitalize_first_letter) {
|
|
175
|
+
const first = String.fromCodePoint(text.codePointAt(0));
|
|
176
|
+
text = first.toUpperCase() + text.slice(first.length);
|
|
177
|
+
}
|
|
178
|
+
if (cfg.append_terminal_punctuation)
|
|
179
|
+
text = ensureTerminalPunctuation(text);
|
|
180
|
+
if (cfg.pad_with_spaces_for_short_inputs && pySplit(text).length < 5)
|
|
181
|
+
text = ' '.repeat(8) + text;
|
|
182
|
+
return { text, framesAfterEosGuess };
|
|
183
|
+
}
|
|
184
|
+
function ensureTerminalPunctuation(text) {
|
|
185
|
+
const core = rstripChars(text, CLOSERS + ' ');
|
|
186
|
+
const closers = text.slice(core.length).trim();
|
|
187
|
+
if (!core || TERMINAL.includes(core[core.length - 1]))
|
|
188
|
+
return text;
|
|
189
|
+
if (WEAK.includes(core[core.length - 1]))
|
|
190
|
+
return rstripChars(core, WEAK + ' ') + '.' + closers;
|
|
191
|
+
return text + '.';
|
|
192
|
+
}
|
|
193
|
+
function isDigit(c) {
|
|
194
|
+
return c !== undefined && /^\p{Nd}$/u.test(c);
|
|
195
|
+
}
|
|
196
|
+
function boundaryIndices(tokens, boundary, tok) {
|
|
197
|
+
const out = [0];
|
|
198
|
+
let previousWasBoundary = false;
|
|
199
|
+
tokens.forEach((t, idx) => {
|
|
200
|
+
if (boundary.has(t)) {
|
|
201
|
+
previousWasBoundary = true;
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (previousWasBoundary) {
|
|
205
|
+
let decimal = false;
|
|
206
|
+
if (tok) {
|
|
207
|
+
const prefix = tok.decode(tokens.slice(0, idx));
|
|
208
|
+
const suffix = tok.decode(tokens.slice(idx));
|
|
209
|
+
decimal =
|
|
210
|
+
prefix.length >= 2 &&
|
|
211
|
+
prefix.endsWith('.') &&
|
|
212
|
+
isDigit(prefix[prefix.length - 2]) &&
|
|
213
|
+
isDigit(suffix[0]);
|
|
214
|
+
}
|
|
215
|
+
if (!decimal)
|
|
216
|
+
out.push(idx);
|
|
217
|
+
}
|
|
218
|
+
previousWasBoundary = false;
|
|
219
|
+
});
|
|
220
|
+
out.push(tokens.length);
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
223
|
+
function segments(tokens, bounds, tok) {
|
|
224
|
+
const out = [];
|
|
225
|
+
for (let i = 0; i < bounds.length - 1; i++) {
|
|
226
|
+
out.push([bounds[i + 1] - bounds[i], tok.decode(tokens.slice(bounds[i], bounds[i + 1]))]);
|
|
227
|
+
}
|
|
228
|
+
return out;
|
|
229
|
+
}
|
|
230
|
+
// upstream split_into_best_sentences.
|
|
231
|
+
export function splitIntoChunks(text, tok, cfg) {
|
|
232
|
+
const prepared = prepareTextPrompt(text, cfg).text.trim();
|
|
233
|
+
const tokens = tok.encode(prepared);
|
|
234
|
+
const sentenceEnd = new Set(tok.encode('.!...?').slice(1));
|
|
235
|
+
const sentences = segments(tokens, boundaryIndices(tokens, sentenceEnd, tok), tok);
|
|
236
|
+
const fallback = new Set(tok.encode(',;:').slice(1));
|
|
237
|
+
const refined = [];
|
|
238
|
+
for (const [count, sentence] of sentences) {
|
|
239
|
+
if (count <= cfg.max_token_per_chunk) {
|
|
240
|
+
refined.push([count, sentence]);
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
const sub = tok.encode(sentence.trim());
|
|
244
|
+
const parts = segments(sub, boundaryIndices(sub, fallback, null), tok);
|
|
245
|
+
if (parts.length > 1)
|
|
246
|
+
refined.push(...parts);
|
|
247
|
+
else
|
|
248
|
+
refined.push([count, sentence]);
|
|
249
|
+
}
|
|
250
|
+
const chunks = [];
|
|
251
|
+
let current = '';
|
|
252
|
+
let currentCount = 0;
|
|
253
|
+
for (const [count, sentence] of refined) {
|
|
254
|
+
if (current === '') {
|
|
255
|
+
current = sentence;
|
|
256
|
+
currentCount = count;
|
|
257
|
+
}
|
|
258
|
+
else if (currentCount + count > cfg.max_token_per_chunk) {
|
|
259
|
+
chunks.push(current.trim());
|
|
260
|
+
current = sentence;
|
|
261
|
+
currentCount = count;
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
current += ' ' + sentence;
|
|
265
|
+
currentCount += count;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
if (current !== '')
|
|
269
|
+
chunks.push(current.trim());
|
|
270
|
+
return chunks;
|
|
271
|
+
}
|
|
272
|
+
// What the model is fed: every chunk is prepared again (upstream does so in
|
|
273
|
+
// generate_audio_stream) and gets two extra frames after EOS on top of the guess.
|
|
274
|
+
export function prepareChunks(text, tok, cfg) {
|
|
275
|
+
return splitIntoChunks(text, tok, cfg).map((chunk) => {
|
|
276
|
+
const { text: prompt, framesAfterEosGuess } = prepareTextPrompt(chunk, cfg);
|
|
277
|
+
return { text: prompt, tokenIds: tok.encode(prompt), framesAfterEos: framesAfterEosGuess + 2 };
|
|
278
|
+
});
|
|
279
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type VoiceTensor = {
|
|
2
|
+
dtype: 'float32' | 'int64';
|
|
3
|
+
dims: number[];
|
|
4
|
+
data: Float32Array | BigInt64Array;
|
|
5
|
+
};
|
|
6
|
+
export type VoiceStates = Map<string, Map<string, VoiceTensor>>;
|
|
7
|
+
export declare function parseVoices(buf: Uint8Array): VoiceStates;
|
|
8
|
+
export declare function padInto(src: VoiceTensor, dims: readonly number[]): Float32Array | BigInt64Array;
|
|
9
|
+
//# sourceMappingURL=voices.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"voices.d.ts","sourceRoot":"","sources":["../../../src/voice/pocket/voices.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,WAAW,GAAG;IACxB,KAAK,EAAE,SAAS,GAAG,OAAO,CAAC;IAC3B,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,IAAI,EAAE,YAAY,GAAG,aAAa,CAAC;CACpC,CAAC;AAGF,MAAM,MAAM,WAAW,GAAG,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;AAIhE,wBAAgB,WAAW,CAAC,GAAG,EAAE,UAAU,GAAG,WAAW,CAkDxD;AAKD,wBAAgB,OAAO,CAAC,GAAG,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,YAAY,GAAG,aAAa,CAqB/F"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// voices.bin of a Pocket TTS schema-3 bundle ("PTVB1", little endian): per voice, the
|
|
2
|
+
// flow LM KV cache after prefilling BOS + the voice conditioning.
|
|
3
|
+
//
|
|
4
|
+
// "PTVB1" u32 voiceCount
|
|
5
|
+
// per voice: u16 nameLen name u16 tensorCount
|
|
6
|
+
// per tensor: u16 keyLen key("<module>/<key>") u8 dtype(0 f32, 1 i64) u8 ndim
|
|
7
|
+
// u32 dims[ndim] u32 byteLength bytes
|
|
8
|
+
const MAGIC = 'PTVB1';
|
|
9
|
+
export function parseVoices(buf) {
|
|
10
|
+
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
11
|
+
const text = new TextDecoder();
|
|
12
|
+
let p = 0;
|
|
13
|
+
const need = (n) => {
|
|
14
|
+
if (p + n > buf.byteLength)
|
|
15
|
+
throw new Error('voices.bin is truncated');
|
|
16
|
+
};
|
|
17
|
+
const u8 = () => (need(1), view.getUint8(p++));
|
|
18
|
+
const u16 = () => (need(2), (p += 2), view.getUint16(p - 2, true));
|
|
19
|
+
const u32 = () => (need(4), (p += 4), view.getUint32(p - 4, true));
|
|
20
|
+
const str = (n) => (need(n), (p += n), text.decode(buf.subarray(p - n, p)));
|
|
21
|
+
if (str(5) !== MAGIC)
|
|
22
|
+
throw new Error('voices.bin: bad magic');
|
|
23
|
+
const voices = new Map();
|
|
24
|
+
const count = u32();
|
|
25
|
+
for (let v = 0; v < count; v++) {
|
|
26
|
+
const name = str(u16());
|
|
27
|
+
const tensors = new Map();
|
|
28
|
+
const tensorCount = u16();
|
|
29
|
+
for (let t = 0; t < tensorCount; t++) {
|
|
30
|
+
const key = str(u16());
|
|
31
|
+
const code = u8();
|
|
32
|
+
const ndim = u8();
|
|
33
|
+
const dims = Array.from({ length: ndim }, u32);
|
|
34
|
+
const byteLength = u32();
|
|
35
|
+
need(byteLength);
|
|
36
|
+
// Copy: the tensor's offset in the file is not aligned for a typed array view (and
|
|
37
|
+
// Buffer#slice would not copy).
|
|
38
|
+
const bytes = new Uint8Array(byteLength);
|
|
39
|
+
bytes.set(buf.subarray(p, p + byteLength));
|
|
40
|
+
p += byteLength;
|
|
41
|
+
const elements = dims.reduce((a, b) => a * b, 1);
|
|
42
|
+
let tensor;
|
|
43
|
+
if (code === 0)
|
|
44
|
+
tensor = {
|
|
45
|
+
dtype: 'float32',
|
|
46
|
+
dims,
|
|
47
|
+
data: new Float32Array(bytes.buffer, 0, byteLength / 4),
|
|
48
|
+
};
|
|
49
|
+
else if (code === 1)
|
|
50
|
+
tensor = { dtype: 'int64', dims, data: new BigInt64Array(bytes.buffer, 0, byteLength / 8) };
|
|
51
|
+
else
|
|
52
|
+
throw new Error(`voices.bin: unknown dtype ${code} for ${name} ${key}`);
|
|
53
|
+
if (tensor.data.length !== elements)
|
|
54
|
+
throw new Error(`voices.bin: ${name} ${key} size does not match its dims`);
|
|
55
|
+
tensors.set(key, tensor);
|
|
56
|
+
}
|
|
57
|
+
voices.set(name, tensors);
|
|
58
|
+
}
|
|
59
|
+
if (p !== buf.byteLength)
|
|
60
|
+
throw new Error('voices.bin: trailing bytes');
|
|
61
|
+
return voices;
|
|
62
|
+
}
|
|
63
|
+
// Copy `src` into a zero-filled tensor of shape `dims` that may be larger along one axis
|
|
64
|
+
// (the voice KV cache [2,1,K,H,D] into the model's [2,1,1024,H,D]): the leading K rows
|
|
65
|
+
// of that axis are the voice, the rest stays zero.
|
|
66
|
+
export function padInto(src, dims) {
|
|
67
|
+
if (src.dims.length !== dims.length)
|
|
68
|
+
throw new Error(`rank mismatch: [${src.dims}] vs [${dims}]`);
|
|
69
|
+
const axes = dims.map((d, i) => i).filter((i) => dims[i] !== src.dims[i]);
|
|
70
|
+
if (axes.length > 1 || (axes.length === 1 && src.dims[axes[0]] > dims[axes[0]])) {
|
|
71
|
+
throw new Error(`cannot place [${src.dims}] into [${dims}]`);
|
|
72
|
+
}
|
|
73
|
+
const total = dims.reduce((a, b) => a * b, 1);
|
|
74
|
+
const out = src.dtype === 'float32' ? new Float32Array(total) : new BigInt64Array(total);
|
|
75
|
+
if (axes.length === 0) {
|
|
76
|
+
out.set(src.data);
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
const axis = axes[0];
|
|
80
|
+
const outer = dims.slice(0, axis).reduce((a, b) => a * b, 1);
|
|
81
|
+
const inner = dims.slice(axis + 1).reduce((a, b) => a * b, 1);
|
|
82
|
+
const srcBlock = src.dims[axis] * inner;
|
|
83
|
+
const dstBlock = dims[axis] * inner;
|
|
84
|
+
for (let o = 0; o < outer; o++) {
|
|
85
|
+
out.set(src.data.subarray(o * srcBlock, (o + 1) * srcBlock), o * dstBlock);
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { PronunciationEntry, VoiceConfig } from 'caretaker-types';
|
|
2
|
+
/** Shipped defaults by ISO 639-1 code. Generic terms only (caretaker is open
|
|
3
|
+
* source). Respellings tuned by ear on the Italian built-in voice; the other
|
|
4
|
+
* languages ship empty. */
|
|
5
|
+
export declare const DEFAULT_PRONUNCIATION: Readonly<Record<string, readonly PronunciationEntry[]>>;
|
|
6
|
+
/** ISO code of the language being synthesized, or null when the server cannot
|
|
7
|
+
* know it (the renderer's navigator.language never reaches the server). */
|
|
8
|
+
export declare function synthesisLanguage(voice: VoiceConfig): string | null;
|
|
9
|
+
/** The saved dictionary for `code` when present (it replaces the defaults),
|
|
10
|
+
* else the shipped defaults, else empty. */
|
|
11
|
+
export declare function dictionaryFor(voice: VoiceConfig, code: string): readonly PronunciationEntry[];
|
|
12
|
+
/** Replace every whole-word, case-insensitive occurrence of an entry's term
|
|
13
|
+
* with its `say`. Terms are literal; the longest wins; one pass, so a
|
|
14
|
+
* replacement is never matched again. */
|
|
15
|
+
export declare function applyPronunciation(text: string, entries: readonly PronunciationEntry[]): string;
|
|
16
|
+
//# sourceMappingURL=pronunciation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pronunciation.d.ts","sourceRoot":"","sources":["../../src/voice/pronunciation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAGvE;;4BAE4B;AAC5B,eAAO,MAAM,qBAAqB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,kBAAkB,EAAE,CAAC,CAYzF,CAAC;AAEF;4EAC4E;AAC5E,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,GAAG,IAAI,CAMnE;AAED;6CAC6C;AAC7C,wBAAgB,aAAa,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,kBAAkB,EAAE,CAE7F;AAID;;0CAE0C;AAC1C,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,kBAAkB,EAAE,GAAG,MAAM,CAa/F"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { POCKET_LANGUAGES } from './pocket/catalog.js';
|
|
2
|
+
/** Shipped defaults by ISO 639-1 code. Generic terms only (caretaker is open
|
|
3
|
+
* source). Respellings tuned by ear on the Italian built-in voice; the other
|
|
4
|
+
* languages ship empty. */
|
|
5
|
+
export const DEFAULT_PRONUNCIATION = {
|
|
6
|
+
it: [
|
|
7
|
+
{ term: 'merge request', say: 'mèrg riquèst' },
|
|
8
|
+
{ term: 'pull request', say: 'pùl riquèst' },
|
|
9
|
+
{ term: 'pipeline', say: 'pàiplain' },
|
|
10
|
+
{ term: 'deploy', say: 'diplòi' },
|
|
11
|
+
{ term: 'branch', say: 'brènc' },
|
|
12
|
+
{ term: 'feature', say: 'fìciar' },
|
|
13
|
+
{ term: 'build', say: 'bìld' },
|
|
14
|
+
{ term: 'review', say: 'riviù' },
|
|
15
|
+
{ term: 'stage', say: 'stèij' },
|
|
16
|
+
],
|
|
17
|
+
};
|
|
18
|
+
/** ISO code of the language being synthesized, or null when the server cannot
|
|
19
|
+
* know it (the renderer's navigator.language never reaches the server). */
|
|
20
|
+
export function synthesisLanguage(voice) {
|
|
21
|
+
if (voice.ttsEngine === 'builtin') {
|
|
22
|
+
return POCKET_LANGUAGES[voice.ttsLanguage]?.code ?? null;
|
|
23
|
+
}
|
|
24
|
+
const primary = voice.lang?.split('-')[0]?.trim().toLowerCase();
|
|
25
|
+
return primary || null;
|
|
26
|
+
}
|
|
27
|
+
/** The saved dictionary for `code` when present (it replaces the defaults),
|
|
28
|
+
* else the shipped defaults, else empty. */
|
|
29
|
+
export function dictionaryFor(voice, code) {
|
|
30
|
+
return voice.pronunciation?.[code] ?? DEFAULT_PRONUNCIATION[code] ?? [];
|
|
31
|
+
}
|
|
32
|
+
const escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
33
|
+
/** Replace every whole-word, case-insensitive occurrence of an entry's term
|
|
34
|
+
* with its `say`. Terms are literal; the longest wins; one pass, so a
|
|
35
|
+
* replacement is never matched again. */
|
|
36
|
+
export function applyPronunciation(text, entries) {
|
|
37
|
+
const say = new Map();
|
|
38
|
+
for (const e of entries) {
|
|
39
|
+
const term = e.term.trim();
|
|
40
|
+
if (term)
|
|
41
|
+
say.set(term.toLowerCase(), e.say);
|
|
42
|
+
}
|
|
43
|
+
if (say.size === 0)
|
|
44
|
+
return text;
|
|
45
|
+
const alternation = [...say.keys()]
|
|
46
|
+
.sort((a, b) => b.length - a.length)
|
|
47
|
+
.map(escapeRegExp)
|
|
48
|
+
.join('|');
|
|
49
|
+
const re = new RegExp(`(?<![\\p{L}\\p{N}])(?:${alternation})(?![\\p{L}\\p{N}])`, 'giu');
|
|
50
|
+
return text.replace(re, (m) => say.get(m.toLowerCase()) ?? m);
|
|
51
|
+
}
|