@scotteza/opencode-spoken-replies 0.0.0-stage → 1.0.1
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 +22 -0
- package/README.md +138 -2
- package/dist/commands/voice-commands.d.ts +41 -0
- package/dist/commands/voice-menu.d.ts +5 -0
- package/dist/commands/voice-picker.d.ts +2 -0
- package/dist/latest/latest-message-store.d.ts +56 -0
- package/dist/messages/message-store.d.ts +18 -0
- package/dist/playback/playback-pipeline.d.ts +52 -0
- package/dist/playback/player-service.d.ts +30 -0
- package/dist/playback/queue.d.ts +112 -0
- package/dist/server-plugin.d.ts +3 -0
- package/dist/server-plugin.js +264 -0
- package/dist/session/idle-controller.d.ts +19 -0
- package/dist/session/question-controller.d.ts +20 -0
- package/dist/session/session-store.d.ts +25 -0
- package/dist/shared/timer-registry.d.ts +12 -0
- package/dist/shared/voice-utils.d.ts +8 -0
- package/dist/state/voice-state-store.d.ts +32 -0
- package/dist/state/voice-status.d.ts +2 -0
- package/dist/streaming/stream-buffer.d.ts +59 -0
- package/dist/streaming/streaming-controller.d.ts +33 -0
- package/dist/system-prompt-injection.d.ts +1 -0
- package/dist/tui-plugin.d.ts +4 -0
- package/dist/tui-plugin.js +3342 -0
- package/dist/voice-audio.d.ts +2 -0
- package/dist/voice-config.d.ts +2 -0
- package/dist/voice-constants.d.ts +7 -0
- package/dist/voice-helper-process.d.ts +26 -0
- package/dist/voice-helper-process.js +615 -0
- package/dist/voice-helper-protocol.d.ts +38 -0
- package/dist/voice-helper-runtime.d.ts +34 -0
- package/dist/voice-kokoro.d.ts +24 -0
- package/dist/voice-log.d.ts +12 -0
- package/dist/voice-rpc.d.ts +57 -0
- package/dist/voice-sanitize.d.ts +23 -0
- package/dist/voice-selection.d.ts +26 -0
- package/dist/voice-speed.d.ts +27 -0
- package/dist/voice-text.d.ts +9 -0
- package/dist/voice-types.d.ts +79 -0
- package/package.json +72 -4
|
@@ -0,0 +1,3342 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/tui-plugin.tsx
|
|
3
|
+
import { createComponent as _$createComponent } from "@opentui/solid";
|
|
4
|
+
import { setProp as _$setProp } from "@opentui/solid";
|
|
5
|
+
import { effect as _$effect } from "@opentui/solid";
|
|
6
|
+
import { insert as _$insert } from "@opentui/solid";
|
|
7
|
+
import { createElement as _$createElement } from "@opentui/solid";
|
|
8
|
+
import { Plugin } from "@opencode/plugin/tui";
|
|
9
|
+
import { createSignal, onCleanup } from "solid-js";
|
|
10
|
+
|
|
11
|
+
// src/voice-log.ts
|
|
12
|
+
import * as fs from "fs";
|
|
13
|
+
import * as os from "os";
|
|
14
|
+
import * as path from "path";
|
|
15
|
+
var LOG_PREFIX = "[opencode-tts-voice]";
|
|
16
|
+
var LOG_FILE_PREFIX = "opencode-tts-voice";
|
|
17
|
+
var LEVELS = {
|
|
18
|
+
debug: 10,
|
|
19
|
+
info: 20,
|
|
20
|
+
warn: 30,
|
|
21
|
+
error: 40,
|
|
22
|
+
silent: 50
|
|
23
|
+
};
|
|
24
|
+
var logFile;
|
|
25
|
+
var fileLogDisabled = false;
|
|
26
|
+
function readLevel() {
|
|
27
|
+
const value = process.env.OPENCODE_TTS_VOICE_LOG_LEVEL ?? process.env.OPENCODE_TTS_LOG_LEVEL;
|
|
28
|
+
switch (value?.toLowerCase()) {
|
|
29
|
+
case "debug":
|
|
30
|
+
case "info":
|
|
31
|
+
case "warn":
|
|
32
|
+
case "error":
|
|
33
|
+
case "silent":
|
|
34
|
+
return value.toLowerCase();
|
|
35
|
+
default:
|
|
36
|
+
return "warn";
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function readConsoleEnabled() {
|
|
40
|
+
const value = process.env.OPENCODE_TTS_VOICE_CONSOLE_LOG ?? process.env.OPENCODE_TTS_CONSOLE_LOG;
|
|
41
|
+
return value === "1" || value?.toLowerCase() === "true";
|
|
42
|
+
}
|
|
43
|
+
function normalize(fields) {
|
|
44
|
+
if (!fields)
|
|
45
|
+
return;
|
|
46
|
+
return Object.fromEntries(Object.entries(fields).filter(([, value]) => value !== undefined));
|
|
47
|
+
}
|
|
48
|
+
function timestampForFile(date) {
|
|
49
|
+
return date.toISOString().replace(/:/g, "").replace(/\.\d{3}Z$/, "");
|
|
50
|
+
}
|
|
51
|
+
function resolveLogFile() {
|
|
52
|
+
if (fileLogDisabled)
|
|
53
|
+
return;
|
|
54
|
+
if (logFile)
|
|
55
|
+
return logFile;
|
|
56
|
+
const home = os.homedir();
|
|
57
|
+
if (!process.env.XDG_DATA_HOME && !home) {
|
|
58
|
+
fileLogDisabled = true;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const dataDir = process.env.XDG_DATA_HOME ?? path.join(home, ".local", "share");
|
|
62
|
+
const dir = path.join(dataDir, "opencode", "log");
|
|
63
|
+
const file = path.join(dir, `${LOG_FILE_PREFIX}-${timestampForFile(new Date)}.log`);
|
|
64
|
+
try {
|
|
65
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
66
|
+
logFile = file;
|
|
67
|
+
return file;
|
|
68
|
+
} catch {
|
|
69
|
+
fileLogDisabled = true;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function formatFields(fields) {
|
|
74
|
+
if (!fields || Object.keys(fields).length === 0)
|
|
75
|
+
return "";
|
|
76
|
+
try {
|
|
77
|
+
return ` ${JSON.stringify(fields)}`;
|
|
78
|
+
} catch {
|
|
79
|
+
return ` ${String(fields)}`;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function writeFile(method, scope, message, fields) {
|
|
83
|
+
const file = resolveLogFile();
|
|
84
|
+
if (!file)
|
|
85
|
+
return;
|
|
86
|
+
try {
|
|
87
|
+
fs.appendFileSync(file, `${new Date().toISOString()} ${method.toUpperCase()} ${scope} ${message}${formatFields(fields)}
|
|
88
|
+
`);
|
|
89
|
+
} catch {
|
|
90
|
+
fileLogDisabled = true;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function write(method, scope, message, fields, options) {
|
|
94
|
+
if (LEVELS[method] < LEVELS[readLevel()])
|
|
95
|
+
return;
|
|
96
|
+
const payload = normalize(fields);
|
|
97
|
+
writeFile(method, scope, message, payload);
|
|
98
|
+
if (!(options?.console ?? readConsoleEnabled()))
|
|
99
|
+
return;
|
|
100
|
+
const line = `${LOG_PREFIX} ${scope} ${message}`;
|
|
101
|
+
if (payload && Object.keys(payload).length > 0) {
|
|
102
|
+
console[method](line, payload);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
console[method](line);
|
|
106
|
+
}
|
|
107
|
+
function createLogger(scope, options) {
|
|
108
|
+
return {
|
|
109
|
+
debug(message, fields) {
|
|
110
|
+
write("debug", scope, message, fields, options);
|
|
111
|
+
},
|
|
112
|
+
info(message, fields) {
|
|
113
|
+
write("info", scope, message, fields, options);
|
|
114
|
+
},
|
|
115
|
+
warn(message, fields) {
|
|
116
|
+
write("warn", scope, message, fields, options);
|
|
117
|
+
},
|
|
118
|
+
error(message, fields) {
|
|
119
|
+
write("error", scope, message, fields, options);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// src/voice-sanitize.ts
|
|
125
|
+
var CODE_BLOCK_PLACEHOLDER = "Omitted code block.";
|
|
126
|
+
var TABLE_PLACEHOLDER = "Omitted table.";
|
|
127
|
+
var IMAGE_PLACEHOLDER = "Omitted image.";
|
|
128
|
+
var INLINE_CODE_PLACEHOLDER = "Omitted inline code.";
|
|
129
|
+
var TECHNICAL_PLACEHOLDER = "Omitted technical output.";
|
|
130
|
+
var IDENTIFIER_PLACEHOLDER = "identifier omitted";
|
|
131
|
+
var BUILT_IN_FILE_EXTENSIONS = [
|
|
132
|
+
"astro",
|
|
133
|
+
"avif",
|
|
134
|
+
"cjs",
|
|
135
|
+
"css",
|
|
136
|
+
"csv",
|
|
137
|
+
"gif",
|
|
138
|
+
"go",
|
|
139
|
+
"html",
|
|
140
|
+
"jpeg",
|
|
141
|
+
"jpg",
|
|
142
|
+
"js",
|
|
143
|
+
"json",
|
|
144
|
+
"jsx",
|
|
145
|
+
"lock",
|
|
146
|
+
"md",
|
|
147
|
+
"mjs",
|
|
148
|
+
"png",
|
|
149
|
+
"py",
|
|
150
|
+
"rs",
|
|
151
|
+
"sh",
|
|
152
|
+
"svg",
|
|
153
|
+
"svelte",
|
|
154
|
+
"toml",
|
|
155
|
+
"ts",
|
|
156
|
+
"tsx",
|
|
157
|
+
"txt",
|
|
158
|
+
"vue",
|
|
159
|
+
"webp",
|
|
160
|
+
"xml",
|
|
161
|
+
"yaml",
|
|
162
|
+
"yml"
|
|
163
|
+
];
|
|
164
|
+
var EXTENSION_NAMES = {
|
|
165
|
+
astro: "Astro",
|
|
166
|
+
avif: "AVIF image",
|
|
167
|
+
cjs: "CommonJS",
|
|
168
|
+
css: "CSS",
|
|
169
|
+
csv: "CSV",
|
|
170
|
+
gif: "GIF image",
|
|
171
|
+
go: "Go",
|
|
172
|
+
html: "HTML",
|
|
173
|
+
jpeg: "JPEG image",
|
|
174
|
+
jpg: "JPEG image",
|
|
175
|
+
js: "JavaScript",
|
|
176
|
+
json: "JSON",
|
|
177
|
+
jsx: "JavaScript React",
|
|
178
|
+
lock: "lock",
|
|
179
|
+
md: "Markdown",
|
|
180
|
+
mjs: "JavaScript module",
|
|
181
|
+
png: "PNG image",
|
|
182
|
+
py: "Python",
|
|
183
|
+
rs: "Rust",
|
|
184
|
+
sh: "shell",
|
|
185
|
+
svg: "SVG image",
|
|
186
|
+
svelte: "Svelte",
|
|
187
|
+
toml: "TOML",
|
|
188
|
+
ts: "TypeScript",
|
|
189
|
+
tsx: "TypeScript React",
|
|
190
|
+
txt: "text",
|
|
191
|
+
vue: "Vue",
|
|
192
|
+
webp: "WebP image",
|
|
193
|
+
xml: "XML",
|
|
194
|
+
yaml: "YAML",
|
|
195
|
+
yml: "YAML"
|
|
196
|
+
};
|
|
197
|
+
var URL_PATTERN = /https?:\/\/\S+|www\.\S+/gi;
|
|
198
|
+
var UUID_PATTERN = /\b[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\b/gi;
|
|
199
|
+
var LONG_HEX_PATTERN = /\b[0-9a-f]{32,}\b/gi;
|
|
200
|
+
var PATHISH_PATTERN = /(^|[\s("'`])((?:[A-Za-z]:[\\/]|~[\\/]|\.{1,2}[\\/]|[\\/])?(?:[A-Za-z0-9_.-]*[A-Za-z0-9][\\/])+(?:[A-Za-z0-9_.-]*[A-Za-z0-9])|(?:[A-Za-z]:[\\/]|~[\\/]|\.{1,2}[\\/]|[\\/])(?:[A-Za-z0-9_.-]*[A-Za-z0-9])|[A-Za-z0-9_.-]*[A-Za-z0-9]\.([A-Za-z0-9]+))(?=$|[\s).,;:'"`])/g;
|
|
201
|
+
var HTTP_METHOD_CONTEXT = /\b(?:GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)\s+$/i;
|
|
202
|
+
var ABSOLUTE_FILESYSTEM_ROOTS = new Set([
|
|
203
|
+
"bin",
|
|
204
|
+
"boot",
|
|
205
|
+
"dev",
|
|
206
|
+
"etc",
|
|
207
|
+
"home",
|
|
208
|
+
"lib",
|
|
209
|
+
"lib64",
|
|
210
|
+
"media",
|
|
211
|
+
"mnt",
|
|
212
|
+
"opt",
|
|
213
|
+
"proc",
|
|
214
|
+
"root",
|
|
215
|
+
"run",
|
|
216
|
+
"sbin",
|
|
217
|
+
"srv",
|
|
218
|
+
"sys",
|
|
219
|
+
"tmp",
|
|
220
|
+
"usr",
|
|
221
|
+
"var",
|
|
222
|
+
"volumes",
|
|
223
|
+
"workspace",
|
|
224
|
+
"workspaces",
|
|
225
|
+
"users"
|
|
226
|
+
]);
|
|
227
|
+
function normalizeFileExtensions(value) {
|
|
228
|
+
const input = typeof value === "string" ? [value] : Array.isArray(value) ? value : [];
|
|
229
|
+
const normalized = [];
|
|
230
|
+
for (const item of input) {
|
|
231
|
+
if (typeof item !== "string")
|
|
232
|
+
continue;
|
|
233
|
+
const extension = item.trim().replace(/^\./, "").toLowerCase();
|
|
234
|
+
if (!/^[a-z0-9]+$/.test(extension))
|
|
235
|
+
continue;
|
|
236
|
+
if (!normalized.includes(extension))
|
|
237
|
+
normalized.push(extension);
|
|
238
|
+
}
|
|
239
|
+
return normalized;
|
|
240
|
+
}
|
|
241
|
+
function createSanitizerSettings(options) {
|
|
242
|
+
return {
|
|
243
|
+
fileExtensions: new Set([...BUILT_IN_FILE_EXTENSIONS, ...normalizeFileExtensions(options?.fileExtensions)])
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
function normalizeSpeechText(text) {
|
|
247
|
+
return text.replace(/\r/g, `
|
|
248
|
+
`).replace(/\u00a0/g, " ").replace(/[\t ]+/g, " ").replace(/\s+([,.!?;:])/g, "$1").replace(/([,!?;:])(?=[^\s\n])/g, "$1 ").replace(/\.([^\s\n])/g, (match, next, offset, input) => {
|
|
249
|
+
const previous = input[offset - 1];
|
|
250
|
+
return previous && /[A-Za-z0-9]/.test(previous) && /[a-z0-9]/.test(next) ? match : `. ${next}`;
|
|
251
|
+
}).replace(/[ \t]*\n[ \t]*/g, `
|
|
252
|
+
`).replace(/\n{2,}/g, `
|
|
253
|
+
`).replace(/[ \t]{2,}/g, " ").replace(/\b(?:and|or)[,.!?;:]*\s*$/i, "").trim();
|
|
254
|
+
}
|
|
255
|
+
function ensureSentence(text) {
|
|
256
|
+
const next = text.trim();
|
|
257
|
+
if (!next)
|
|
258
|
+
return "";
|
|
259
|
+
if (/[,;:]$/.test(next))
|
|
260
|
+
return `${next.slice(0, -1)}.`;
|
|
261
|
+
return /[.!?]$/.test(next) ? next : `${next}.`;
|
|
262
|
+
}
|
|
263
|
+
function isCodeFenceLine(text) {
|
|
264
|
+
return /^(```+|~~~+)/.test(text.trim());
|
|
265
|
+
}
|
|
266
|
+
function isHorizontalRule(text) {
|
|
267
|
+
return /^ {0,3}([-*_])(?:\s*\1){2,}\s*$/.test(text);
|
|
268
|
+
}
|
|
269
|
+
function isTableSeparatorLine(text) {
|
|
270
|
+
const trimmed = text.trim();
|
|
271
|
+
if (!trimmed.includes("|") || !trimmed.includes("-"))
|
|
272
|
+
return false;
|
|
273
|
+
return /^\|?[\s:-]+(?:\|[\s:-]+)+\|?$/.test(trimmed);
|
|
274
|
+
}
|
|
275
|
+
function isPotentialTableRow(text) {
|
|
276
|
+
const trimmed = text.trim();
|
|
277
|
+
if (!trimmed.includes("|"))
|
|
278
|
+
return false;
|
|
279
|
+
if (trimmed.startsWith("|") || trimmed.endsWith("|"))
|
|
280
|
+
return true;
|
|
281
|
+
return (trimmed.match(/\|/g)?.length ?? 0) >= 2;
|
|
282
|
+
}
|
|
283
|
+
function isDenseTechnicalLine(text) {
|
|
284
|
+
const trimmed = text.trim();
|
|
285
|
+
if (!trimmed)
|
|
286
|
+
return false;
|
|
287
|
+
if (/^at\s+\S+/.test(trimmed))
|
|
288
|
+
return true;
|
|
289
|
+
if (/^(@@|diff\s+--git|---\s+|\+\+\+\s+)/.test(trimmed))
|
|
290
|
+
return true;
|
|
291
|
+
if (/^[{}[\],:"'0-9A-Za-z_\s.-]+$/.test(trimmed) && /^[{[]/.test(trimmed) && /[}\]]$/.test(trimmed)) {
|
|
292
|
+
const symbols = trimmed.match(/[{}[\],:"]/g)?.length ?? 0;
|
|
293
|
+
return trimmed.length > 40 && symbols / trimmed.length > 0.12;
|
|
294
|
+
}
|
|
295
|
+
const symbols = trimmed.match(/[{}[\]<>|\\=;]/g)?.length ?? 0;
|
|
296
|
+
return trimmed.length > 100 && symbols / trimmed.length > 0.16;
|
|
297
|
+
}
|
|
298
|
+
function humanizeName(value) {
|
|
299
|
+
const leadingDot = value.startsWith(".");
|
|
300
|
+
const base = leadingDot ? value.slice(1) : value;
|
|
301
|
+
const normalized = base.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/[-_]+/g, " ").replace(/\.+/g, " dot ").replace(/\s+/g, " ").trim();
|
|
302
|
+
const spoken = normalized.toLowerCase() === "readme" ? "read me" : normalized;
|
|
303
|
+
return leadingDot ? `dot ${spoken}` : spoken;
|
|
304
|
+
}
|
|
305
|
+
function describeFile(file) {
|
|
306
|
+
const match = file.match(/^(.*)\.([A-Za-z0-9]+)$/);
|
|
307
|
+
if (!match)
|
|
308
|
+
return `file ${humanizeName(file)}`;
|
|
309
|
+
const [, rawName, rawExtension] = match;
|
|
310
|
+
const extension = rawExtension.toLowerCase();
|
|
311
|
+
const fileType = EXTENSION_NAMES[extension] ?? humanizeName(extension);
|
|
312
|
+
const name = rawName ? humanizeName(rawName) : "unnamed";
|
|
313
|
+
return `${name} ${fileType} file`;
|
|
314
|
+
}
|
|
315
|
+
function describeFolder(folder) {
|
|
316
|
+
return `the ${humanizeName(folder)} folder`;
|
|
317
|
+
}
|
|
318
|
+
function speakPath(value) {
|
|
319
|
+
const normalized = value.replace(/\\/g, "/").replace(/^[A-Za-z]:\//, "/").replace(/^['"`(]+|[,'"`.)]+$/g, "");
|
|
320
|
+
const parts = normalized.split("/").filter((part) => part && part !== "." && part !== ".." && part !== "~");
|
|
321
|
+
const last = parts.at(-1);
|
|
322
|
+
if (!last)
|
|
323
|
+
return "the current folder";
|
|
324
|
+
const hasFile = /\.([A-Za-z0-9]+)$/.test(last);
|
|
325
|
+
if (!hasFile)
|
|
326
|
+
return describeFolder(last);
|
|
327
|
+
const folders = parts.slice(0, -1);
|
|
328
|
+
const file = describeFile(last);
|
|
329
|
+
const parent = folders.at(-1);
|
|
330
|
+
return parent ? `the ${file} in ${describeFolder(parent)}` : `the ${file}`;
|
|
331
|
+
}
|
|
332
|
+
function speakSlashPhrase(value) {
|
|
333
|
+
return value.replace(/\//g, " slash ").replace(/\s+/g, " ").trim();
|
|
334
|
+
}
|
|
335
|
+
function pathParts(value) {
|
|
336
|
+
return value.replace(/\\/g, "/").replace(/^[A-Za-z]:\//, "/").split("/").filter(Boolean);
|
|
337
|
+
}
|
|
338
|
+
function startsWithExplicitPathMarker(value) {
|
|
339
|
+
return /^(?:[A-Za-z]:[\\/]|~[\\/]|\.{1,2}[\\/])/.test(value);
|
|
340
|
+
}
|
|
341
|
+
function isRecognizedAbsolutePath(value) {
|
|
342
|
+
if (!/^[\\/]/.test(value))
|
|
343
|
+
return false;
|
|
344
|
+
const root = pathParts(value)[0]?.toLowerCase();
|
|
345
|
+
return Boolean(root && ABSOLUTE_FILESYSTEM_ROOTS.has(root));
|
|
346
|
+
}
|
|
347
|
+
function classifyPathish(value, contextBefore, settings) {
|
|
348
|
+
const hasSlash = value.includes("/");
|
|
349
|
+
const hasBackslash = value.includes("\\");
|
|
350
|
+
const hasSeparator = hasSlash || hasBackslash;
|
|
351
|
+
const final = pathParts(value).at(-1) ?? value;
|
|
352
|
+
const extension = final.match(/\.([A-Za-z0-9]+)$/)?.[1]?.toLowerCase();
|
|
353
|
+
const hasKnownExtension = Boolean(extension && settings.fileExtensions.has(extension));
|
|
354
|
+
if (!hasSeparator)
|
|
355
|
+
return hasKnownExtension ? "file" : "none";
|
|
356
|
+
if (HTTP_METHOD_CONTEXT.test(contextBefore) && hasSlash && !hasBackslash)
|
|
357
|
+
return "slash";
|
|
358
|
+
if (hasBackslash || /^[A-Za-z]:[\\/]/.test(value))
|
|
359
|
+
return extension ? "file" : "folder";
|
|
360
|
+
if (startsWithExplicitPathMarker(value) || isRecognizedAbsolutePath(value))
|
|
361
|
+
return extension ? "file" : "folder";
|
|
362
|
+
if (/^[\\/]/.test(value))
|
|
363
|
+
return "slash";
|
|
364
|
+
return extension ? "file" : "slash";
|
|
365
|
+
}
|
|
366
|
+
function speakPathish(value, contextBefore, settings) {
|
|
367
|
+
const classification = classifyPathish(value, contextBefore, settings);
|
|
368
|
+
if (classification === "none")
|
|
369
|
+
return;
|
|
370
|
+
if (classification === "slash")
|
|
371
|
+
return speakSlashPhrase(value);
|
|
372
|
+
return speakPath(value);
|
|
373
|
+
}
|
|
374
|
+
function rewritePaths(text, settings) {
|
|
375
|
+
return text.replace(PATHISH_PATTERN, (match, prefix, value, _extension, offset) => {
|
|
376
|
+
const contextBefore = text.slice(0, offset + prefix.length);
|
|
377
|
+
const spoken = speakPathish(value, contextBefore, settings);
|
|
378
|
+
return spoken ? `${prefix}${spoken}` : match;
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
function speakReference(value, settings) {
|
|
382
|
+
return speakPathish(value, "", settings);
|
|
383
|
+
}
|
|
384
|
+
function shouldOmitInlineCode(value, settings) {
|
|
385
|
+
if (value.length > 80)
|
|
386
|
+
return true;
|
|
387
|
+
if (/\n/.test(value))
|
|
388
|
+
return true;
|
|
389
|
+
if (/^[A-Za-z_$][\w$.-]*$/.test(value))
|
|
390
|
+
return false;
|
|
391
|
+
if (speakReference(value, settings))
|
|
392
|
+
return false;
|
|
393
|
+
if (/\b(?:bun|curl|docker|git|node|npm|pnpm|python|yarn)\b/.test(value) && /\s/.test(value))
|
|
394
|
+
return true;
|
|
395
|
+
return /[{}[\]<>|;&$]/.test(value) && /\s/.test(value);
|
|
396
|
+
}
|
|
397
|
+
function speakInlineCode(value, settings) {
|
|
398
|
+
const trimmed = value.trim();
|
|
399
|
+
if (!trimmed)
|
|
400
|
+
return "";
|
|
401
|
+
if (shouldOmitInlineCode(trimmed, settings))
|
|
402
|
+
return INLINE_CODE_PLACEHOLDER;
|
|
403
|
+
const reference = speakReference(trimmed, settings);
|
|
404
|
+
if (reference)
|
|
405
|
+
return reference;
|
|
406
|
+
return humanizeName(trimmed.replace(/\$/g, ""));
|
|
407
|
+
}
|
|
408
|
+
function stripMarkdownEmphasis(text) {
|
|
409
|
+
return text.replace(/\*\*\s*([^*\n]*?\S[^*\n]*?)\s*\*\*/g, "$1").replace(/__\s*([^_\n]*?\S[^_\n]*?)\s*__/g, "$1").replace(/~~\s*([^~\n]*?\S[^~\n]*?)\s*~~/g, "$1").replace(/(^|\W)\*\s*([^*\n]*?\S[^*\n]*?)\s*\*(?=\W|$)/g, "$1$2").replace(/(^|\W)_\s*([^_\n]*?\S[^_\n]*?)\s*_(?=\W|$)/g, "$1$2");
|
|
410
|
+
}
|
|
411
|
+
function replaceCommaSeparators(text) {
|
|
412
|
+
return text.replace(/([.!?])?,\s+(?:(?:and|or)\s+)?/gi, (_match, punctuation) => {
|
|
413
|
+
return punctuation ? `${punctuation} ` : ". ";
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
function sanitizeInline(text, settings) {
|
|
417
|
+
return replaceCommaSeparators(rewritePaths(stripMarkdownEmphasis(text).replace(/\\([\\`*_[\]{}()#+\-.!>])/g, "$1").replace(/!\[([^\]]*)\]\([^)]+\)/g, (_match, alt) => alt.trim() || IMAGE_PLACEHOLDER).replace(/\[([^\]]+)\]\([^)]+\)/g, "$1").replace(/\[([^\]]+)\]\[[^\]]*\]/g, "$1").replace(/<https?:\/\/[^>]+>/gi, " ").replace(URL_PATTERN, " ").replace(UUID_PATTERN, IDENTIFIER_PLACEHOLDER).replace(LONG_HEX_PATTERN, IDENTIFIER_PLACEHOLDER).replace(/`([^`\n]+)`/g, (_match, code) => speakInlineCode(code, settings)).replace(/&/g, " and ").replace(/(?:->|=>)/g, " to ").replace(/[<>]/g, " ").replace(/[`]/g, " "), settings));
|
|
418
|
+
}
|
|
419
|
+
function stripLinePrefixes(line) {
|
|
420
|
+
let next = line.trim();
|
|
421
|
+
while (/^>\s?/.test(next)) {
|
|
422
|
+
next = next.replace(/^>\s?/, "").trimStart();
|
|
423
|
+
}
|
|
424
|
+
next = next.replace(/^\[![A-Za-z]+\]\s*/, "");
|
|
425
|
+
next = next.replace(/^(?:[-*+]|\d{1,3}[.)])\s+/, "");
|
|
426
|
+
next = next.replace(/^\[[ xX]\]\s+/, "");
|
|
427
|
+
return next;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
class SpeechSanitizer {
|
|
431
|
+
settings;
|
|
432
|
+
buffer = "";
|
|
433
|
+
inCodeBlock = false;
|
|
434
|
+
inTable = false;
|
|
435
|
+
pendingTableHeader;
|
|
436
|
+
atLineStart = true;
|
|
437
|
+
constructor(options) {
|
|
438
|
+
this.settings = createSanitizerSettings(options);
|
|
439
|
+
}
|
|
440
|
+
push(text, final = false) {
|
|
441
|
+
this.buffer += text;
|
|
442
|
+
const output = [];
|
|
443
|
+
while (true) {
|
|
444
|
+
if (this.inCodeBlock) {
|
|
445
|
+
if (this.skipCodeBlock(final))
|
|
446
|
+
continue;
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
if (this.inTable) {
|
|
450
|
+
const line = this.takeLine(final);
|
|
451
|
+
if (line === undefined)
|
|
452
|
+
break;
|
|
453
|
+
if (isPotentialTableRow(line) || !line.trim())
|
|
454
|
+
continue;
|
|
455
|
+
this.inTable = false;
|
|
456
|
+
output.push(this.processLine(line));
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
if (this.pendingTableHeader !== undefined) {
|
|
460
|
+
const line = this.takeLine(final);
|
|
461
|
+
if (line === undefined)
|
|
462
|
+
break;
|
|
463
|
+
if (isTableSeparatorLine(line)) {
|
|
464
|
+
output.push(TABLE_PLACEHOLDER);
|
|
465
|
+
this.pendingTableHeader = undefined;
|
|
466
|
+
this.inTable = true;
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
const header = this.pendingTableHeader;
|
|
470
|
+
this.pendingTableHeader = undefined;
|
|
471
|
+
output.push(this.processPlainLine(header, true), this.processLine(line));
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
const line = this.takeLine(final);
|
|
475
|
+
if (line === undefined) {
|
|
476
|
+
if (!final && this.buffer && !this.shouldHoldPartialLine()) {
|
|
477
|
+
const partial = this.takePartialText();
|
|
478
|
+
if (!partial)
|
|
479
|
+
break;
|
|
480
|
+
output.push(sanitizeInline(partial, this.settings));
|
|
481
|
+
this.atLineStart = false;
|
|
482
|
+
continue;
|
|
483
|
+
}
|
|
484
|
+
break;
|
|
485
|
+
}
|
|
486
|
+
output.push(this.processLine(line));
|
|
487
|
+
}
|
|
488
|
+
if (final) {
|
|
489
|
+
if (this.pendingTableHeader !== undefined) {
|
|
490
|
+
output.push(this.processPlainLine(this.pendingTableHeader, true));
|
|
491
|
+
this.pendingTableHeader = undefined;
|
|
492
|
+
}
|
|
493
|
+
this.buffer = "";
|
|
494
|
+
this.inCodeBlock = false;
|
|
495
|
+
this.inTable = false;
|
|
496
|
+
this.atLineStart = true;
|
|
497
|
+
}
|
|
498
|
+
return normalizeSpeechText(output.filter(Boolean).join(`
|
|
499
|
+
`));
|
|
500
|
+
}
|
|
501
|
+
takeLine(final) {
|
|
502
|
+
const newline = this.buffer.indexOf(`
|
|
503
|
+
`);
|
|
504
|
+
if (newline >= 0) {
|
|
505
|
+
const line = this.buffer.slice(0, newline).replace(/\r$/, "");
|
|
506
|
+
this.buffer = this.buffer.slice(newline + 1);
|
|
507
|
+
this.atLineStart = true;
|
|
508
|
+
return line;
|
|
509
|
+
}
|
|
510
|
+
if (!final || !this.buffer)
|
|
511
|
+
return;
|
|
512
|
+
const line = this.buffer.replace(/\r$/, "");
|
|
513
|
+
this.buffer = "";
|
|
514
|
+
this.atLineStart = true;
|
|
515
|
+
return line;
|
|
516
|
+
}
|
|
517
|
+
takePartialText() {
|
|
518
|
+
const whitespace = this.buffer.search(/\s+\S*$/);
|
|
519
|
+
const cut = whitespace >= 0 ? whitespace + (this.buffer.slice(whitespace).match(/^\s+/)?.[0].length ?? 0) : -1;
|
|
520
|
+
if (cut > 0) {
|
|
521
|
+
const text = this.buffer.slice(0, cut);
|
|
522
|
+
this.buffer = this.buffer.slice(cut);
|
|
523
|
+
return text;
|
|
524
|
+
}
|
|
525
|
+
if (/[.!?;:]$/.test(this.buffer) || this.buffer.length > 240) {
|
|
526
|
+
const text = this.buffer;
|
|
527
|
+
this.buffer = "";
|
|
528
|
+
return text;
|
|
529
|
+
}
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
skipCodeBlock(final) {
|
|
533
|
+
const match = this.buffer.match(/(^|\n)[ \t]{0,3}(```+|~~~+)/);
|
|
534
|
+
if (!match || match.index === undefined) {
|
|
535
|
+
if (final) {
|
|
536
|
+
this.buffer = "";
|
|
537
|
+
this.inCodeBlock = false;
|
|
538
|
+
return true;
|
|
539
|
+
}
|
|
540
|
+
const partialFence = this.buffer.match(/(^|\n)[ \t]{0,3}(`{1,2}|~{1,2})$/);
|
|
541
|
+
this.buffer = partialFence ? partialFence[0].replace(/^\n/, "") : "";
|
|
542
|
+
return false;
|
|
543
|
+
}
|
|
544
|
+
const fenceStart = match.index + (match[1] ? match[1].length : 0);
|
|
545
|
+
const lineEnd = this.buffer.indexOf(`
|
|
546
|
+
`, fenceStart);
|
|
547
|
+
this.buffer = lineEnd >= 0 ? this.buffer.slice(lineEnd + 1) : "";
|
|
548
|
+
this.inCodeBlock = false;
|
|
549
|
+
this.atLineStart = true;
|
|
550
|
+
return true;
|
|
551
|
+
}
|
|
552
|
+
shouldHoldPartialLine() {
|
|
553
|
+
if (!this.atLineStart)
|
|
554
|
+
return false;
|
|
555
|
+
const trimmed = this.buffer.trimStart();
|
|
556
|
+
if (!trimmed)
|
|
557
|
+
return true;
|
|
558
|
+
if (/^(#{1,6}\s|>|[-*+]\s|\d{1,3}[.)]\s|```|~~~)/.test(trimmed))
|
|
559
|
+
return true;
|
|
560
|
+
if (/^(#{1,6}|[-*+]|\d{1,3}[.)]?|`{1,2}|~{1,2})$/.test(trimmed))
|
|
561
|
+
return true;
|
|
562
|
+
if (isPotentialTableRow(trimmed))
|
|
563
|
+
return true;
|
|
564
|
+
return false;
|
|
565
|
+
}
|
|
566
|
+
processLine(line) {
|
|
567
|
+
const trimmed = line.trim();
|
|
568
|
+
if (!trimmed)
|
|
569
|
+
return "";
|
|
570
|
+
if (isCodeFenceLine(trimmed)) {
|
|
571
|
+
this.inCodeBlock = true;
|
|
572
|
+
return CODE_BLOCK_PLACEHOLDER;
|
|
573
|
+
}
|
|
574
|
+
if (isPotentialTableRow(trimmed)) {
|
|
575
|
+
this.pendingTableHeader = line;
|
|
576
|
+
return "";
|
|
577
|
+
}
|
|
578
|
+
return this.processPlainLine(line, true);
|
|
579
|
+
}
|
|
580
|
+
processPlainLine(line, lineBreak = false) {
|
|
581
|
+
const withoutPrefixes = stripLinePrefixes(line);
|
|
582
|
+
if (!withoutPrefixes)
|
|
583
|
+
return "";
|
|
584
|
+
if (isHorizontalRule(withoutPrefixes))
|
|
585
|
+
return "";
|
|
586
|
+
const heading = withoutPrefixes.match(/^#{1,6}\s+(.+?)\s*#*$/);
|
|
587
|
+
if (heading)
|
|
588
|
+
return ensureSentence(`Heading, ${sanitizeInline(heading[1], this.settings)}`);
|
|
589
|
+
if (isDenseTechnicalLine(withoutPrefixes))
|
|
590
|
+
return TECHNICAL_PLACEHOLDER;
|
|
591
|
+
const sanitized = sanitizeInline(withoutPrefixes, this.settings);
|
|
592
|
+
return lineBreak ? ensureSentence(sanitized) : sanitized;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
function createSpeechSanitizer(options) {
|
|
596
|
+
return new SpeechSanitizer(options);
|
|
597
|
+
}
|
|
598
|
+
function sanitizeSpeechText(text, options) {
|
|
599
|
+
return createSpeechSanitizer(options).push(text, true);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
// src/voice-text.ts
|
|
603
|
+
var STRONG_BOUNDARY = /[.!?\n]/;
|
|
604
|
+
function clampTextLength(text, maxTextLength) {
|
|
605
|
+
if (text.length <= maxTextLength)
|
|
606
|
+
return text;
|
|
607
|
+
return `${text.slice(0, Math.max(0, maxTextLength - 3)).trimEnd()}...`;
|
|
608
|
+
}
|
|
609
|
+
function normalizePreparedText(text) {
|
|
610
|
+
return text.replace(/\r/g, `
|
|
611
|
+
`).replace(/[ \t]+/g, " ").replace(/[ \t]*\n[ \t]*/g, `
|
|
612
|
+
`).replace(/\n{3,}/g, `
|
|
613
|
+
|
|
614
|
+
`).trim();
|
|
615
|
+
}
|
|
616
|
+
function prepareSanitizedSpeechText(text, maxTextLength) {
|
|
617
|
+
const cleaned = normalizePreparedText(text);
|
|
618
|
+
if (!cleaned)
|
|
619
|
+
return "";
|
|
620
|
+
return clampTextLength(cleaned, maxTextLength);
|
|
621
|
+
}
|
|
622
|
+
function prepareSpeechText(text, maxTextLength, options) {
|
|
623
|
+
return prepareSanitizedSpeechText(sanitizeSpeechText(text, options), maxTextLength);
|
|
624
|
+
}
|
|
625
|
+
function classifyPause(text, config) {
|
|
626
|
+
if (/[.!?;:]$/.test(text))
|
|
627
|
+
return config.sentencePauseMs;
|
|
628
|
+
return config.normalPauseMs;
|
|
629
|
+
}
|
|
630
|
+
function findBoundary(text, minCut, maxCut) {
|
|
631
|
+
for (let index = maxCut - 1;index >= minCut - 1; index -= 1) {
|
|
632
|
+
const char = text[index];
|
|
633
|
+
if (char && STRONG_BOUNDARY.test(char))
|
|
634
|
+
return index + 1;
|
|
635
|
+
}
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
function takeChunk(text, config, final) {
|
|
639
|
+
const input = text.trimStart();
|
|
640
|
+
if (!input)
|
|
641
|
+
return;
|
|
642
|
+
const newline = input.indexOf(`
|
|
643
|
+
`);
|
|
644
|
+
if (newline >= 0 && (final || newline > 0)) {
|
|
645
|
+
const raw = input.slice(0, newline).trim();
|
|
646
|
+
const rest = input.slice(newline + 1).trimStart();
|
|
647
|
+
const prepared = prepareSanitizedSpeechText(raw, config.maxSpeechChars);
|
|
648
|
+
return {
|
|
649
|
+
rest,
|
|
650
|
+
chunk: prepared ? {
|
|
651
|
+
text: prepared,
|
|
652
|
+
pauseMs: config.sentencePauseMs
|
|
653
|
+
} : undefined
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
const hardLimit = Math.min(config.maxSpeechChunkChars, input.length);
|
|
657
|
+
const softLimit = Math.min(config.streamFlushChars, hardLimit);
|
|
658
|
+
if (!final && input.length < softLimit)
|
|
659
|
+
return;
|
|
660
|
+
let cut = findBoundary(input, softLimit, hardLimit);
|
|
661
|
+
if (!cut && (final || input.length >= config.maxSpeechChunkChars)) {
|
|
662
|
+
cut = findBoundary(input, 1, hardLimit);
|
|
663
|
+
}
|
|
664
|
+
if (!cut && final)
|
|
665
|
+
cut = input.length;
|
|
666
|
+
if (!cut)
|
|
667
|
+
return;
|
|
668
|
+
const raw = input.slice(0, cut).trim();
|
|
669
|
+
const rest = input.slice(cut).trimStart();
|
|
670
|
+
const prepared = prepareSanitizedSpeechText(raw, config.maxSpeechChars);
|
|
671
|
+
return {
|
|
672
|
+
rest,
|
|
673
|
+
chunk: prepared ? {
|
|
674
|
+
text: prepared,
|
|
675
|
+
pauseMs: classifyPause(prepared, config)
|
|
676
|
+
} : undefined
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
function drainStreamChunks(text, config, final = false) {
|
|
680
|
+
const chunks = [];
|
|
681
|
+
let rest = text;
|
|
682
|
+
while (rest) {
|
|
683
|
+
const next = takeChunk(rest, config, final);
|
|
684
|
+
if (!next)
|
|
685
|
+
break;
|
|
686
|
+
rest = next.rest;
|
|
687
|
+
if (next.chunk)
|
|
688
|
+
chunks.push(next.chunk);
|
|
689
|
+
if (!final && rest.length < config.streamFlushChars)
|
|
690
|
+
break;
|
|
691
|
+
}
|
|
692
|
+
return { chunks, rest };
|
|
693
|
+
}
|
|
694
|
+
function splitPlaybackText(text, config) {
|
|
695
|
+
const prepared = prepareSpeechText(text, config.maxSpeechChars, config);
|
|
696
|
+
if (!prepared)
|
|
697
|
+
return [];
|
|
698
|
+
const chunks = [];
|
|
699
|
+
let rest = prepared;
|
|
700
|
+
while (rest) {
|
|
701
|
+
const next = takeChunk(rest, config, true);
|
|
702
|
+
if (!next)
|
|
703
|
+
break;
|
|
704
|
+
rest = next.rest;
|
|
705
|
+
if (next.chunk)
|
|
706
|
+
chunks.push(next.chunk);
|
|
707
|
+
}
|
|
708
|
+
return chunks;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
// src/shared/voice-utils.ts
|
|
712
|
+
var LOG_PREVIEW_LENGTH = 120;
|
|
713
|
+
function formatError(error) {
|
|
714
|
+
if (error instanceof Error && error.message)
|
|
715
|
+
return error.message;
|
|
716
|
+
return String(error);
|
|
717
|
+
}
|
|
718
|
+
function appendSpeechBuffer(buffer, text) {
|
|
719
|
+
const next = text.trim();
|
|
720
|
+
if (!next)
|
|
721
|
+
return buffer;
|
|
722
|
+
return buffer ? `${buffer} ${next}` : next;
|
|
723
|
+
}
|
|
724
|
+
function textPreview(text) {
|
|
725
|
+
const compact = text.replace(/\s+/g, " ").trim();
|
|
726
|
+
return compact.length > LOG_PREVIEW_LENGTH ? `${compact.slice(0, LOG_PREVIEW_LENGTH)}...` : compact;
|
|
727
|
+
}
|
|
728
|
+
function textFingerprint(text) {
|
|
729
|
+
let hash = 2166136261;
|
|
730
|
+
for (let index = 0;index < text.length; index += 1) {
|
|
731
|
+
hash ^= text.charCodeAt(index);
|
|
732
|
+
hash = Math.imul(hash, 16777619);
|
|
733
|
+
}
|
|
734
|
+
return `${text.length}:${(hash >>> 0).toString(16).padStart(8, "0")}`;
|
|
735
|
+
}
|
|
736
|
+
function activeSessionID(route) {
|
|
737
|
+
if (route.name !== "session")
|
|
738
|
+
return;
|
|
739
|
+
return typeof route.params?.sessionID === "string" ? route.params.sessionID : undefined;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
// src/commands/voice-commands.ts
|
|
743
|
+
class VoiceCommands {
|
|
744
|
+
route;
|
|
745
|
+
config;
|
|
746
|
+
state;
|
|
747
|
+
playback;
|
|
748
|
+
sessionStore;
|
|
749
|
+
latestStore;
|
|
750
|
+
toast;
|
|
751
|
+
log = createLogger("commands");
|
|
752
|
+
constructor(route, config, state, playback, sessionStore, latestStore, toast) {
|
|
753
|
+
this.route = route;
|
|
754
|
+
this.config = config;
|
|
755
|
+
this.state = state;
|
|
756
|
+
this.playback = playback;
|
|
757
|
+
this.sessionStore = sessionStore;
|
|
758
|
+
this.latestStore = latestStore;
|
|
759
|
+
this.toast = toast;
|
|
760
|
+
}
|
|
761
|
+
async toggleEnabled() {
|
|
762
|
+
const next = !this.state.snapshot().enabled;
|
|
763
|
+
this.log.info("toggle enabled", { next });
|
|
764
|
+
if (!next) {
|
|
765
|
+
await this.playback.reset(false);
|
|
766
|
+
this.state.setEnabled(false);
|
|
767
|
+
this.state.patch({ paused: false });
|
|
768
|
+
this.toast({ variant: "info", message: "Speech disabled" });
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
this.state.setEnabled(true);
|
|
772
|
+
this.state.patch({ paused: false, error: undefined });
|
|
773
|
+
this.state.notifyNow();
|
|
774
|
+
this.toast({ variant: "success", message: "Speech enabled" });
|
|
775
|
+
}
|
|
776
|
+
async togglePlayback(sessionID) {
|
|
777
|
+
const current = this.state.snapshot();
|
|
778
|
+
this.log.info("toggle playback", {
|
|
779
|
+
sessionID: sessionID ?? this.activeSessionID(),
|
|
780
|
+
paused: current.paused,
|
|
781
|
+
busy: current.busy
|
|
782
|
+
});
|
|
783
|
+
if (!current.enabled) {
|
|
784
|
+
this.toast({ variant: "warning", message: "Speech is disabled" });
|
|
785
|
+
return;
|
|
786
|
+
}
|
|
787
|
+
if (current.paused) {
|
|
788
|
+
this.playback.resume();
|
|
789
|
+
this.toast({ variant: "info", message: "Speech resumed" });
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
if (this.playback.isBusy()) {
|
|
793
|
+
this.playback.pause();
|
|
794
|
+
this.toast({ variant: "info", message: "Speech paused" });
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
await this.replayLatest(sessionID);
|
|
798
|
+
}
|
|
799
|
+
async replayLatest(sessionID) {
|
|
800
|
+
this.log.info("replay latest requested", { sessionID: sessionID ?? this.activeSessionID() });
|
|
801
|
+
if (!this.state.snapshot().enabled) {
|
|
802
|
+
this.toast({ variant: "warning", message: "Speech is disabled" });
|
|
803
|
+
return false;
|
|
804
|
+
}
|
|
805
|
+
const active = sessionID ?? this.activeSessionID();
|
|
806
|
+
if (!active) {
|
|
807
|
+
this.toast({ variant: "warning", message: "No active session" });
|
|
808
|
+
return false;
|
|
809
|
+
}
|
|
810
|
+
if (!await this.sessionStore.shouldSpeakSession(active)) {
|
|
811
|
+
this.toast({ variant: "warning", message: "Subagent speech is disabled" });
|
|
812
|
+
return false;
|
|
813
|
+
}
|
|
814
|
+
const latest = await this.latestStore.replayText(active);
|
|
815
|
+
if (!latest.text) {
|
|
816
|
+
this.log.warn("replay latest unavailable", { sessionID: active });
|
|
817
|
+
this.toast({ variant: "warning", message: "No assistant message available" });
|
|
818
|
+
return false;
|
|
819
|
+
}
|
|
820
|
+
const latestText = latest.text;
|
|
821
|
+
await this.playback.reset(false);
|
|
822
|
+
this.state.patch({ paused: false });
|
|
823
|
+
const chunks = splitPlaybackText(latestText, this.config);
|
|
824
|
+
chunks.forEach((chunk, index) => {
|
|
825
|
+
this.playback.enqueuePreparedChunk(chunk.text, chunk.pauseMs, "latest", {
|
|
826
|
+
origin: "replayLatest",
|
|
827
|
+
sessionID: active,
|
|
828
|
+
replaySource: latest.source,
|
|
829
|
+
fullTextLength: latestText.length,
|
|
830
|
+
chunkIndex: index,
|
|
831
|
+
chunkCount: chunks.length
|
|
832
|
+
});
|
|
833
|
+
});
|
|
834
|
+
if (!this.playback.isBusy()) {
|
|
835
|
+
this.toast({ variant: "warning", message: "No assistant message available" });
|
|
836
|
+
return false;
|
|
837
|
+
}
|
|
838
|
+
this.log.info("replay latest queued", {
|
|
839
|
+
sessionID: active,
|
|
840
|
+
textLength: latestText.length,
|
|
841
|
+
replaySource: latest.source,
|
|
842
|
+
fingerprint: textFingerprint(latestText),
|
|
843
|
+
preview: textPreview(latestText)
|
|
844
|
+
});
|
|
845
|
+
this.toast({ variant: "info", message: "Replaying latest assistant message" });
|
|
846
|
+
return true;
|
|
847
|
+
}
|
|
848
|
+
async historyEntries(sessionID) {
|
|
849
|
+
const active = sessionID ?? this.activeSessionID();
|
|
850
|
+
if (!active)
|
|
851
|
+
return [];
|
|
852
|
+
return this.latestStore.collectDisplayHistory(active);
|
|
853
|
+
}
|
|
854
|
+
async hasHistory(sessionID) {
|
|
855
|
+
const active = sessionID ?? this.activeSessionID();
|
|
856
|
+
if (!active)
|
|
857
|
+
return false;
|
|
858
|
+
return this.latestStore.hasPlayableHistory(active);
|
|
859
|
+
}
|
|
860
|
+
async playHistory(messageID, mode, sessionID) {
|
|
861
|
+
const active = sessionID ?? this.activeSessionID();
|
|
862
|
+
this.log.info("history playback requested", { sessionID: active, messageID, mode });
|
|
863
|
+
if (!active)
|
|
864
|
+
return false;
|
|
865
|
+
const continuation = await this.latestStore.collectContinuationHistory(active, messageID);
|
|
866
|
+
const selectedEntries = mode === "continue" ? continuation : continuation.slice(0, 1);
|
|
867
|
+
if (selectedEntries.length === 0) {
|
|
868
|
+
this.log.warn("history playback unavailable", { sessionID: active, messageID, mode });
|
|
869
|
+
return false;
|
|
870
|
+
}
|
|
871
|
+
if (!this.state.snapshot().enabled) {
|
|
872
|
+
this.state.setEnabled(true);
|
|
873
|
+
this.state.patch({ error: undefined });
|
|
874
|
+
this.state.notifyNow();
|
|
875
|
+
}
|
|
876
|
+
await this.playback.reset(false);
|
|
877
|
+
this.state.patch({ paused: false });
|
|
878
|
+
selectedEntries.forEach((entry, messageIndex) => {
|
|
879
|
+
const chunks = splitPlaybackText(entry.text, this.config);
|
|
880
|
+
chunks.forEach((chunk, chunkIndex) => {
|
|
881
|
+
this.playback.enqueuePreparedChunk(chunk.text, chunk.pauseMs, "history", {
|
|
882
|
+
origin: "historyPlayback",
|
|
883
|
+
sessionID: active,
|
|
884
|
+
messageID: entry.messageID,
|
|
885
|
+
replayMode: mode,
|
|
886
|
+
fullTextLength: entry.text.length,
|
|
887
|
+
messageIndex: entry.chronologicalIndex,
|
|
888
|
+
messageCount: selectedEntries.length,
|
|
889
|
+
chunkIndex,
|
|
890
|
+
chunkCount: chunks.length
|
|
891
|
+
});
|
|
892
|
+
});
|
|
893
|
+
this.log.info("history message queued", {
|
|
894
|
+
sessionID: active,
|
|
895
|
+
messageID: entry.messageID,
|
|
896
|
+
mode,
|
|
897
|
+
messageIndex,
|
|
898
|
+
chronologicalIndex: entry.chronologicalIndex,
|
|
899
|
+
textLength: entry.text.length,
|
|
900
|
+
fingerprint: textFingerprint(entry.text),
|
|
901
|
+
preview: textPreview(entry.text)
|
|
902
|
+
});
|
|
903
|
+
});
|
|
904
|
+
return this.playback.isBusy();
|
|
905
|
+
}
|
|
906
|
+
activeSessionID() {
|
|
907
|
+
return activeSessionID(this.route.current);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
// src/commands/voice-menu.ts
|
|
912
|
+
async function openVoiceMenu(api, actions, isDisposed) {
|
|
913
|
+
const entries = [
|
|
914
|
+
{ value: "choose", id: "tts.choose-voice", title: "Choose voice" },
|
|
915
|
+
{ value: "history", id: "tts.history", title: "Open assistant message history" },
|
|
916
|
+
{ value: "pause", id: "tts.play-pause", title: "Play or pause speech" },
|
|
917
|
+
{ value: "latest", id: "tts.latest", title: "Replay latest assistant message" },
|
|
918
|
+
{ value: "toggle", id: "tts.toggle", title: "Enable or disable speech" },
|
|
919
|
+
{ value: "slower", id: "tts.slower", title: "Slow down speech" },
|
|
920
|
+
{ value: "faster", id: "tts.faster", title: "Speed up speech" }
|
|
921
|
+
];
|
|
922
|
+
const selected = await api.ui.dialog.select({
|
|
923
|
+
title: "Voice shortcuts",
|
|
924
|
+
options: entries.map((entry) => {
|
|
925
|
+
const keys = api.keymap.shortcuts(entry.id);
|
|
926
|
+
const shortcut = keys.length ? ` [${keys.join(", ")}]` : "";
|
|
927
|
+
return { title: `${entry.title}${shortcut}`, value: entry.value };
|
|
928
|
+
})
|
|
929
|
+
});
|
|
930
|
+
if (isDisposed() || !selected)
|
|
931
|
+
return;
|
|
932
|
+
await actions[selected]();
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
// src/voice-selection.ts
|
|
936
|
+
var VOICE_CHOICES = [
|
|
937
|
+
...["heart", "alloy", "aoede", "bella", "jessica", "kore", "nicole", "nova", "river", "sarah", "sky"].map((name) => voiceChoice("af", name)),
|
|
938
|
+
...["adam", "echo", "eric", "fenrir", "liam", "michael", "onyx", "puck", "santa"].map((name) => voiceChoice("am", name)),
|
|
939
|
+
...["alice", "emma", "isabella", "lily"].map((name) => voiceChoice("bf", name)),
|
|
940
|
+
...["daniel", "fable", "george", "lewis"].map((name) => voiceChoice("bm", name))
|
|
941
|
+
];
|
|
942
|
+
function voiceChoice(prefix, name) {
|
|
943
|
+
return {
|
|
944
|
+
id: `${prefix}_${name}`,
|
|
945
|
+
name: name[0].toUpperCase() + name.slice(1),
|
|
946
|
+
accent: prefix[0] === "a" ? "American English" : "British English",
|
|
947
|
+
gender: prefix[1] === "f" ? "Female" : "Male"
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
function isSelectableVoice(value) {
|
|
951
|
+
return typeof value === "string" && VOICE_CHOICES.some((voice) => voice.id === value);
|
|
952
|
+
}
|
|
953
|
+
function createVoiceSelectionController(configuredVoice, defaultVoice, storage, onSaveError) {
|
|
954
|
+
const [saved, update] = storage.store("voice", { initial: { voice: null } });
|
|
955
|
+
const configured = typeof configuredVoice === "string" && configuredVoice.trim().length > 0;
|
|
956
|
+
let current = configured ? configuredVoice.trim() : isSelectableVoice(saved.voice) ? saved.voice : defaultVoice;
|
|
957
|
+
let pending = Promise.resolve();
|
|
958
|
+
return {
|
|
959
|
+
get voice() {
|
|
960
|
+
return current;
|
|
961
|
+
},
|
|
962
|
+
select(voice) {
|
|
963
|
+
if (!isSelectableVoice(voice))
|
|
964
|
+
return false;
|
|
965
|
+
current = voice;
|
|
966
|
+
if (!configured) {
|
|
967
|
+
pending = pending.then(() => update((draft) => {
|
|
968
|
+
draft.voice = voice;
|
|
969
|
+
})).catch(onSaveError);
|
|
970
|
+
}
|
|
971
|
+
return true;
|
|
972
|
+
},
|
|
973
|
+
flush() {
|
|
974
|
+
return pending;
|
|
975
|
+
}
|
|
976
|
+
};
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
// src/commands/voice-picker.ts
|
|
980
|
+
async function openVoicePicker(api, current, preview, select, isDisposed) {
|
|
981
|
+
const options = VOICE_CHOICES.map((voice) => ({
|
|
982
|
+
title: `${voice.name} \u2014 ${voice.gender}${voice.id === current ? " \xB7 Current" : ""}`,
|
|
983
|
+
description: voice.id,
|
|
984
|
+
category: voice.accent,
|
|
985
|
+
value: voice.id
|
|
986
|
+
}));
|
|
987
|
+
const selected = await api.ui.dialog.select({
|
|
988
|
+
title: "Choose voice",
|
|
989
|
+
placeholder: "Search voices",
|
|
990
|
+
current,
|
|
991
|
+
options,
|
|
992
|
+
search: (query, choices) => {
|
|
993
|
+
const terms = query.toLowerCase().trim().split(/\s+/).filter(Boolean);
|
|
994
|
+
return choices.filter((choice) => {
|
|
995
|
+
const voice = VOICE_CHOICES.find((entry) => entry.id === choice.value);
|
|
996
|
+
const searchable = `${choice.title} ${choice.description ?? ""} ${choice.category ?? ""}`.toLowerCase();
|
|
997
|
+
return terms.every((term) => {
|
|
998
|
+
if (term === "male" || term === "female")
|
|
999
|
+
return voice?.gender.toLowerCase() === term;
|
|
1000
|
+
return searchable.includes(term);
|
|
1001
|
+
});
|
|
1002
|
+
});
|
|
1003
|
+
},
|
|
1004
|
+
actions: [{
|
|
1005
|
+
bind: "ctrl+o",
|
|
1006
|
+
title: "Preview",
|
|
1007
|
+
onTrigger: (voice) => {
|
|
1008
|
+
if (!isDisposed())
|
|
1009
|
+
preview(voice);
|
|
1010
|
+
}
|
|
1011
|
+
}]
|
|
1012
|
+
});
|
|
1013
|
+
if (!isDisposed() && selected)
|
|
1014
|
+
select(selected);
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
// src/latest/latest-message-store.ts
|
|
1018
|
+
class LatestMessageStore {
|
|
1019
|
+
api;
|
|
1020
|
+
config;
|
|
1021
|
+
sessionStore;
|
|
1022
|
+
log = createLogger("latest");
|
|
1023
|
+
callbacks = new Set;
|
|
1024
|
+
cleanup;
|
|
1025
|
+
pending = new Map;
|
|
1026
|
+
constructor(api, config, sessionStore) {
|
|
1027
|
+
this.api = api;
|
|
1028
|
+
this.config = config;
|
|
1029
|
+
this.sessionStore = sessionStore;
|
|
1030
|
+
this.cleanup = sessionStore.onDeleted((sessionID) => this.notify(sessionID));
|
|
1031
|
+
}
|
|
1032
|
+
dispose() {
|
|
1033
|
+
this.cleanup();
|
|
1034
|
+
this.callbacks.clear();
|
|
1035
|
+
}
|
|
1036
|
+
onHistoryChanged(callback) {
|
|
1037
|
+
this.callbacks.add(callback);
|
|
1038
|
+
return () => {
|
|
1039
|
+
this.callbacks.delete(callback);
|
|
1040
|
+
};
|
|
1041
|
+
}
|
|
1042
|
+
refresh(sessionID) {
|
|
1043
|
+
this.sync(sessionID).then(() => this.notify(sessionID)).catch((error) => this.log.warn("history refresh failed", { error: formatError(error) }));
|
|
1044
|
+
}
|
|
1045
|
+
sync(sessionID) {
|
|
1046
|
+
const existing = this.pending.get(sessionID);
|
|
1047
|
+
if (existing)
|
|
1048
|
+
return existing;
|
|
1049
|
+
const request = this.api.data.session.message.sync(sessionID).finally(() => {
|
|
1050
|
+
if (this.pending.get(sessionID) === request)
|
|
1051
|
+
this.pending.delete(sessionID);
|
|
1052
|
+
});
|
|
1053
|
+
this.pending.set(sessionID, request);
|
|
1054
|
+
return request;
|
|
1055
|
+
}
|
|
1056
|
+
async replayText(sessionID) {
|
|
1057
|
+
const entries = await this.collectPlayableHistory(sessionID);
|
|
1058
|
+
return { text: entries.at(-1)?.text, source: "state" };
|
|
1059
|
+
}
|
|
1060
|
+
async collectDisplayHistory(sessionID, limit = 50) {
|
|
1061
|
+
return (await this.collectPlayableHistory(sessionID)).slice(-limit).reverse();
|
|
1062
|
+
}
|
|
1063
|
+
async collectContinuationHistory(sessionID, selectedMessageID) {
|
|
1064
|
+
const entries = await this.collectPlayableHistory(sessionID);
|
|
1065
|
+
const start = entries.findIndex((entry) => entry.messageID === selectedMessageID);
|
|
1066
|
+
return start === -1 ? [] : entries.slice(start);
|
|
1067
|
+
}
|
|
1068
|
+
async hasPlayableHistory(sessionID) {
|
|
1069
|
+
return (await this.collectPlayableHistory(sessionID)).length > 0;
|
|
1070
|
+
}
|
|
1071
|
+
async collectPlayableHistory(sessionID) {
|
|
1072
|
+
if (!await this.sessionStore.shouldSpeakSession(sessionID))
|
|
1073
|
+
return [];
|
|
1074
|
+
try {
|
|
1075
|
+
await this.sync(sessionID);
|
|
1076
|
+
} catch (error) {
|
|
1077
|
+
this.log.warn("history refresh failed", { sessionID, error: formatError(error) });
|
|
1078
|
+
}
|
|
1079
|
+
return this.api.data.session.message.list(sessionID).flatMap((message, index) => {
|
|
1080
|
+
const text = this.assistantText(message);
|
|
1081
|
+
if (!text || message.type !== "assistant" || !message.time.completed)
|
|
1082
|
+
return [];
|
|
1083
|
+
return [{
|
|
1084
|
+
sessionID,
|
|
1085
|
+
messageID: message.id,
|
|
1086
|
+
created: message.time.created,
|
|
1087
|
+
text,
|
|
1088
|
+
preview: textPreview(text),
|
|
1089
|
+
chronologicalIndex: index
|
|
1090
|
+
}];
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
1093
|
+
assistantText(message) {
|
|
1094
|
+
if (message.type !== "assistant")
|
|
1095
|
+
return;
|
|
1096
|
+
const text = message.content.filter((part) => part.type === "text" && this.config.speechBlocks.includes("message") || part.type === "reasoning" && this.config.speechBlocks.includes("reason")).map((part) => ("text" in part) ? part.text : "").join(" ");
|
|
1097
|
+
return prepareSpeechText(text, this.config.maxSpeechChars, this.config) || undefined;
|
|
1098
|
+
}
|
|
1099
|
+
notify(sessionID) {
|
|
1100
|
+
for (const callback of this.callbacks)
|
|
1101
|
+
callback(sessionID);
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
// src/playback/playback-pipeline.ts
|
|
1106
|
+
import * as fs2 from "fs/promises";
|
|
1107
|
+
|
|
1108
|
+
// src/voice-helper-runtime.ts
|
|
1109
|
+
import { spawn } from "child_process";
|
|
1110
|
+
import * as fsSync from "fs";
|
|
1111
|
+
import * as path2 from "path";
|
|
1112
|
+
import { fileURLToPath } from "url";
|
|
1113
|
+
import { createInterface } from "readline";
|
|
1114
|
+
function formatError2(error) {
|
|
1115
|
+
if (error instanceof Error && error.message)
|
|
1116
|
+
return error.message;
|
|
1117
|
+
return String(error);
|
|
1118
|
+
}
|
|
1119
|
+
function isExecutable(file) {
|
|
1120
|
+
try {
|
|
1121
|
+
fsSync.accessSync(file, fsSync.constants.X_OK);
|
|
1122
|
+
return true;
|
|
1123
|
+
} catch {
|
|
1124
|
+
return false;
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
function resolveExecutable(command, env) {
|
|
1128
|
+
if (command.includes(path2.sep))
|
|
1129
|
+
return isExecutable(command) ? command : undefined;
|
|
1130
|
+
const envPath = env.PATH ?? "";
|
|
1131
|
+
for (const dir of envPath.split(path2.delimiter)) {
|
|
1132
|
+
if (!dir)
|
|
1133
|
+
continue;
|
|
1134
|
+
const candidate = path2.join(dir, command);
|
|
1135
|
+
if (isExecutable(candidate))
|
|
1136
|
+
return candidate;
|
|
1137
|
+
}
|
|
1138
|
+
return;
|
|
1139
|
+
}
|
|
1140
|
+
function isJavaScriptRuntime(command) {
|
|
1141
|
+
const base = path2.basename(command).toLowerCase().replace(/\.exe$/, "");
|
|
1142
|
+
return base === "node" || base === "nodejs" || base === "bun";
|
|
1143
|
+
}
|
|
1144
|
+
function resolveDefaultRuntime(execPath, env) {
|
|
1145
|
+
if (isJavaScriptRuntime(execPath))
|
|
1146
|
+
return execPath;
|
|
1147
|
+
const bunInstall = env.BUN_INSTALL ? path2.join(env.BUN_INSTALL, "bin", "bun") : undefined;
|
|
1148
|
+
if (bunInstall && isExecutable(bunInstall))
|
|
1149
|
+
return bunInstall;
|
|
1150
|
+
const runtime = resolveExecutable("bun", env) ?? resolveExecutable("node", env) ?? resolveExecutable("nodejs", env);
|
|
1151
|
+
if (runtime)
|
|
1152
|
+
return runtime;
|
|
1153
|
+
throw new Error("No Node or Bun executable found for the TTS helper service. Install one, or set ttsServiceCommand.");
|
|
1154
|
+
}
|
|
1155
|
+
function resolveHelperServiceLaunch(config, helper, execPath = process.execPath, env = process.env) {
|
|
1156
|
+
let runtime;
|
|
1157
|
+
const defaultRuntime = () => {
|
|
1158
|
+
runtime ??= resolveDefaultRuntime(execPath, env);
|
|
1159
|
+
return runtime;
|
|
1160
|
+
};
|
|
1161
|
+
const replacePlaceholder = (value) => {
|
|
1162
|
+
if (value === "{helper}")
|
|
1163
|
+
return helper;
|
|
1164
|
+
if (value === "{runtime}" || value === "{node}")
|
|
1165
|
+
return defaultRuntime();
|
|
1166
|
+
return value;
|
|
1167
|
+
};
|
|
1168
|
+
const command = config.ttsServiceCommand ? replacePlaceholder(config.ttsServiceCommand) : defaultRuntime();
|
|
1169
|
+
const args = (config.ttsServiceCommand ? config.ttsServiceArgs ?? ["{helper}"] : ["{helper}"]).map(replacePlaceholder);
|
|
1170
|
+
return { command, args };
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
class TtsHelperRuntime {
|
|
1174
|
+
config;
|
|
1175
|
+
onStatus;
|
|
1176
|
+
logger;
|
|
1177
|
+
child;
|
|
1178
|
+
started;
|
|
1179
|
+
disposed = false;
|
|
1180
|
+
nextID = 1;
|
|
1181
|
+
pending = new Map;
|
|
1182
|
+
constructor(config, onStatus, logger) {
|
|
1183
|
+
this.config = config;
|
|
1184
|
+
this.onStatus = onStatus;
|
|
1185
|
+
this.logger = logger;
|
|
1186
|
+
}
|
|
1187
|
+
async* stream(text, epoch, voice = this.config.voice) {
|
|
1188
|
+
const request = {
|
|
1189
|
+
id: this.nextID++,
|
|
1190
|
+
epoch,
|
|
1191
|
+
queue: [],
|
|
1192
|
+
waiters: [],
|
|
1193
|
+
done: false
|
|
1194
|
+
};
|
|
1195
|
+
this.pending.set(request.id, request);
|
|
1196
|
+
try {
|
|
1197
|
+
await this.send({ type: "generate", id: request.id, epoch, text, config: { ...this.config, voice } });
|
|
1198
|
+
while (!this.disposed) {
|
|
1199
|
+
const item = request.queue.shift();
|
|
1200
|
+
if (item) {
|
|
1201
|
+
yield item;
|
|
1202
|
+
continue;
|
|
1203
|
+
}
|
|
1204
|
+
if (request.error)
|
|
1205
|
+
throw request.error;
|
|
1206
|
+
if (request.done)
|
|
1207
|
+
return;
|
|
1208
|
+
await this.wait(request);
|
|
1209
|
+
}
|
|
1210
|
+
} finally {
|
|
1211
|
+
this.pending.delete(request.id);
|
|
1212
|
+
if (!request.done && !this.disposed) {
|
|
1213
|
+
await this.send({ type: "cancel", id: request.id }).catch(() => {
|
|
1214
|
+
return;
|
|
1215
|
+
});
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
async cancelEpoch(epoch) {
|
|
1220
|
+
if (!this.child && !this.started)
|
|
1221
|
+
return;
|
|
1222
|
+
await this.send({ type: "cancel", epoch }).catch(() => {
|
|
1223
|
+
return;
|
|
1224
|
+
});
|
|
1225
|
+
}
|
|
1226
|
+
async dispose() {
|
|
1227
|
+
if (this.disposed)
|
|
1228
|
+
return;
|
|
1229
|
+
this.disposed = true;
|
|
1230
|
+
for (const request of this.pending.values()) {
|
|
1231
|
+
request.done = true;
|
|
1232
|
+
this.wake(request);
|
|
1233
|
+
}
|
|
1234
|
+
this.pending.clear();
|
|
1235
|
+
const child = this.child;
|
|
1236
|
+
this.child = undefined;
|
|
1237
|
+
if (!child)
|
|
1238
|
+
return;
|
|
1239
|
+
this.write(child, { type: "dispose" });
|
|
1240
|
+
child.stdin.end();
|
|
1241
|
+
setTimeout(() => {
|
|
1242
|
+
if (!child.killed)
|
|
1243
|
+
child.kill("SIGTERM");
|
|
1244
|
+
}, 5000).unref();
|
|
1245
|
+
}
|
|
1246
|
+
async send(message) {
|
|
1247
|
+
if (this.disposed)
|
|
1248
|
+
throw new Error("TTS helper is disposed");
|
|
1249
|
+
await this.ensureStarted();
|
|
1250
|
+
const child = this.child;
|
|
1251
|
+
if (!child)
|
|
1252
|
+
throw new Error("TTS helper failed to start");
|
|
1253
|
+
this.write(child, message);
|
|
1254
|
+
}
|
|
1255
|
+
write(child, message) {
|
|
1256
|
+
if (!child.stdin.writable)
|
|
1257
|
+
throw new Error("TTS helper stdin is closed");
|
|
1258
|
+
child.stdin.write(`${JSON.stringify(message)}
|
|
1259
|
+
`);
|
|
1260
|
+
}
|
|
1261
|
+
ensureStarted() {
|
|
1262
|
+
if (this.started)
|
|
1263
|
+
return this.started;
|
|
1264
|
+
this.started = this.start();
|
|
1265
|
+
return this.started;
|
|
1266
|
+
}
|
|
1267
|
+
async start() {
|
|
1268
|
+
const helper = fileURLToPath(new URL("./voice-helper-process.js", import.meta.url));
|
|
1269
|
+
const { command, args } = resolveHelperServiceLaunch(this.config, helper);
|
|
1270
|
+
const child = spawn(command, args, {
|
|
1271
|
+
env: {
|
|
1272
|
+
...process.env,
|
|
1273
|
+
OPENCODE_TTS_VOICE_HELPER_MODE: "process",
|
|
1274
|
+
OPENCODE_TTS_VOICE_LOG_LEVEL: process.env.OPENCODE_TTS_VOICE_HELPER_LOG_LEVEL ?? "silent",
|
|
1275
|
+
OPENCODE_TTS_VOICE_CONSOLE_LOG: "false"
|
|
1276
|
+
},
|
|
1277
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
1278
|
+
});
|
|
1279
|
+
this.child = child;
|
|
1280
|
+
this.logger.info("helper service started", { command, args, helper });
|
|
1281
|
+
let stderr = "";
|
|
1282
|
+
child.stderr.on("data", (chunk) => {
|
|
1283
|
+
stderr = `${stderr}${String(chunk)}`.slice(-2000);
|
|
1284
|
+
});
|
|
1285
|
+
createInterface({ input: child.stdout }).on("line", (line) => {
|
|
1286
|
+
const text = line.trim();
|
|
1287
|
+
if (!text)
|
|
1288
|
+
return;
|
|
1289
|
+
try {
|
|
1290
|
+
this.handleResponse(JSON.parse(text));
|
|
1291
|
+
} catch (error) {
|
|
1292
|
+
this.failAll(new Error(`Invalid TTS helper response: ${formatError2(error)}`));
|
|
1293
|
+
child.kill("SIGTERM");
|
|
1294
|
+
}
|
|
1295
|
+
});
|
|
1296
|
+
child.once("error", (error) => {
|
|
1297
|
+
this.failAll(error);
|
|
1298
|
+
this.child = undefined;
|
|
1299
|
+
this.started = undefined;
|
|
1300
|
+
});
|
|
1301
|
+
child.once("exit", (code, signal) => {
|
|
1302
|
+
this.logger.info("helper service exited", {
|
|
1303
|
+
code,
|
|
1304
|
+
signal,
|
|
1305
|
+
stderr: stderr.trim() || undefined
|
|
1306
|
+
});
|
|
1307
|
+
if (this.child === child)
|
|
1308
|
+
this.child = undefined;
|
|
1309
|
+
this.started = undefined;
|
|
1310
|
+
if (!this.disposed && (code !== 0 || this.pending.size > 0)) {
|
|
1311
|
+
this.failAll(new Error(`TTS helper exited with code ${code ?? "unknown"}${stderr.trim() ? `: ${stderr.trim()}` : ""}`));
|
|
1312
|
+
}
|
|
1313
|
+
});
|
|
1314
|
+
}
|
|
1315
|
+
handleResponse(message) {
|
|
1316
|
+
if (message.type === "status") {
|
|
1317
|
+
this.onStatus(message.status);
|
|
1318
|
+
return;
|
|
1319
|
+
}
|
|
1320
|
+
if (message.type === "error") {
|
|
1321
|
+
const error = new Error(message.error);
|
|
1322
|
+
if (typeof message.id !== "number") {
|
|
1323
|
+
this.failAll(error);
|
|
1324
|
+
return;
|
|
1325
|
+
}
|
|
1326
|
+
const request = this.pending.get(message.id);
|
|
1327
|
+
if (!request)
|
|
1328
|
+
return;
|
|
1329
|
+
request.error = error;
|
|
1330
|
+
request.done = true;
|
|
1331
|
+
this.wake(request);
|
|
1332
|
+
return;
|
|
1333
|
+
}
|
|
1334
|
+
const request = this.pending.get(message.id);
|
|
1335
|
+
if (!request || request.epoch !== message.epoch)
|
|
1336
|
+
return;
|
|
1337
|
+
if (message.type === "segment") {
|
|
1338
|
+
this.logger.info("helper segment received", {
|
|
1339
|
+
requestID: message.id,
|
|
1340
|
+
epoch: message.epoch,
|
|
1341
|
+
textLength: message.text.length,
|
|
1342
|
+
file: message.file
|
|
1343
|
+
});
|
|
1344
|
+
request.queue.push({ text: message.text, file: message.file });
|
|
1345
|
+
this.wake(request);
|
|
1346
|
+
return;
|
|
1347
|
+
}
|
|
1348
|
+
request.done = true;
|
|
1349
|
+
this.wake(request);
|
|
1350
|
+
}
|
|
1351
|
+
failAll(error) {
|
|
1352
|
+
const next = error instanceof Error ? error : new Error(formatError2(error));
|
|
1353
|
+
this.logger.error("helper failed", { error: next.message });
|
|
1354
|
+
for (const request of this.pending.values()) {
|
|
1355
|
+
request.error = next;
|
|
1356
|
+
request.done = true;
|
|
1357
|
+
this.wake(request);
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
wait(request) {
|
|
1361
|
+
return new Promise((resolve) => {
|
|
1362
|
+
request.waiters.push(resolve);
|
|
1363
|
+
});
|
|
1364
|
+
}
|
|
1365
|
+
wake(request) {
|
|
1366
|
+
const waiters = request.waiters.splice(0);
|
|
1367
|
+
for (const waiter of waiters)
|
|
1368
|
+
waiter();
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
// src/voice-speed.ts
|
|
1373
|
+
var MIN_VOICE_SPEED = 0.25;
|
|
1374
|
+
var MAX_VOICE_SPEED = 5;
|
|
1375
|
+
var VOICE_SPEED_STEP = 0.25;
|
|
1376
|
+
var MIN_SPEED_PERCENT = 25;
|
|
1377
|
+
var MAX_SPEED_PERCENT = 500;
|
|
1378
|
+
var SPEED_STEP_PERCENT = 25;
|
|
1379
|
+
function isValidVoiceSpeed(value) {
|
|
1380
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0;
|
|
1381
|
+
}
|
|
1382
|
+
function speedToPercent(speed) {
|
|
1383
|
+
const clamped = Math.max(MIN_VOICE_SPEED, Math.min(MAX_VOICE_SPEED, speed));
|
|
1384
|
+
return Math.round(clamped / VOICE_SPEED_STEP) * SPEED_STEP_PERCENT;
|
|
1385
|
+
}
|
|
1386
|
+
function adjustSpeedPercent(percent, direction) {
|
|
1387
|
+
return Math.max(MIN_SPEED_PERCENT, Math.min(MAX_SPEED_PERCENT, percent + direction * SPEED_STEP_PERCENT));
|
|
1388
|
+
}
|
|
1389
|
+
function normalizeVoiceSpeed(speed) {
|
|
1390
|
+
return speedToPercent(speed) / 100;
|
|
1391
|
+
}
|
|
1392
|
+
function formatVoiceSpeed(percent) {
|
|
1393
|
+
const whole = Math.floor(percent / 100);
|
|
1394
|
+
const fraction = percent % 100;
|
|
1395
|
+
if (fraction === 0)
|
|
1396
|
+
return String(whole);
|
|
1397
|
+
return `${whole}.${String(fraction).padStart(2, "0").replace(/0$/, "")}`;
|
|
1398
|
+
}
|
|
1399
|
+
function createVoiceSpeedController(configuredSpeed, defaultSpeed, storage, onSaveError) {
|
|
1400
|
+
const [saved, update] = storage.store("speed", { initial: { speed: null } });
|
|
1401
|
+
const configured = isValidVoiceSpeed(configuredSpeed);
|
|
1402
|
+
let current = speedToPercent(configured ? configuredSpeed : isValidVoiceSpeed(saved.speed) ? saved.speed : defaultSpeed);
|
|
1403
|
+
let pending = Promise.resolve();
|
|
1404
|
+
return {
|
|
1405
|
+
get speed() {
|
|
1406
|
+
return current / 100;
|
|
1407
|
+
},
|
|
1408
|
+
get percent() {
|
|
1409
|
+
return current;
|
|
1410
|
+
},
|
|
1411
|
+
adjust(direction) {
|
|
1412
|
+
current = adjustSpeedPercent(current, direction);
|
|
1413
|
+
if (!configured) {
|
|
1414
|
+
const next = current / 100;
|
|
1415
|
+
pending = pending.then(() => update((draft) => {
|
|
1416
|
+
draft.speed = next;
|
|
1417
|
+
})).catch(onSaveError);
|
|
1418
|
+
}
|
|
1419
|
+
return current / 100;
|
|
1420
|
+
},
|
|
1421
|
+
flush() {
|
|
1422
|
+
return pending;
|
|
1423
|
+
}
|
|
1424
|
+
};
|
|
1425
|
+
}
|
|
1426
|
+
function scalePauseForSpeed(pauseMs, speed) {
|
|
1427
|
+
return Math.round(pauseMs / speed);
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
// src/playback/player-service.ts
|
|
1431
|
+
import { spawn as spawn2 } from "child_process";
|
|
1432
|
+
import * as fsSync2 from "fs";
|
|
1433
|
+
import * as path3 from "path";
|
|
1434
|
+
|
|
1435
|
+
// src/voice-constants.ts
|
|
1436
|
+
var PLUGIN_ID = "semirhuduti.tts.voice";
|
|
1437
|
+
var KV_ENABLED = `${PLUGIN_ID}.enabled`;
|
|
1438
|
+
var DEFAULT_MODEL = "onnx-community/Kokoro-82M-v1.0-ONNX";
|
|
1439
|
+
var DEFAULT_CONFIG = {
|
|
1440
|
+
voice: "af_heart",
|
|
1441
|
+
speed: 1,
|
|
1442
|
+
device: "auto",
|
|
1443
|
+
dtype: "q8",
|
|
1444
|
+
model: DEFAULT_MODEL,
|
|
1445
|
+
cacheDir: undefined,
|
|
1446
|
+
audioPlayer: "auto",
|
|
1447
|
+
audioPlayerArgs: [],
|
|
1448
|
+
speakResponses: true,
|
|
1449
|
+
speakSubagentResponses: false,
|
|
1450
|
+
speakOnIdle: false,
|
|
1451
|
+
speakQuestions: true,
|
|
1452
|
+
idleAnnouncement: "Task completed.",
|
|
1453
|
+
speechBlocks: ["message", "idle"],
|
|
1454
|
+
maxSpeechChunkChars: 1000,
|
|
1455
|
+
streamFlushChars: 180,
|
|
1456
|
+
maxSpeechChars: 2000,
|
|
1457
|
+
fileExtensions: [],
|
|
1458
|
+
trimSilenceThreshold: 0.001,
|
|
1459
|
+
leadingAudioPadMs: 12,
|
|
1460
|
+
normalPauseMs: 240,
|
|
1461
|
+
sentencePauseMs: 100,
|
|
1462
|
+
ttsServiceCommand: undefined,
|
|
1463
|
+
ttsServiceArgs: undefined,
|
|
1464
|
+
shortcuts: {
|
|
1465
|
+
history: "f5",
|
|
1466
|
+
pause: "f6",
|
|
1467
|
+
skipLatest: "f7",
|
|
1468
|
+
toggle: "f8"
|
|
1469
|
+
}
|
|
1470
|
+
};
|
|
1471
|
+
var PLAYER_DEFAULT_ARGS = {
|
|
1472
|
+
ffplay: ["-nodisp", "-autoexit", "-loglevel", "error"],
|
|
1473
|
+
mpv: ["--no-terminal", "--really-quiet", "--force-window=no", "--audio-display=no", "--keep-open=no"],
|
|
1474
|
+
paplay: [],
|
|
1475
|
+
aplay: ["-q"]
|
|
1476
|
+
};
|
|
1477
|
+
var PLAYER_CANDIDATES = ["ffplay", "mpv", "paplay", "aplay"];
|
|
1478
|
+
|
|
1479
|
+
// src/playback/player-service.ts
|
|
1480
|
+
function toBasePlayer(playerBin) {
|
|
1481
|
+
return path3.basename(playerBin).toLowerCase();
|
|
1482
|
+
}
|
|
1483
|
+
function isMissingBinary(error) {
|
|
1484
|
+
return error !== null && typeof error === "object" && "code" in error && error.code === "ENOENT";
|
|
1485
|
+
}
|
|
1486
|
+
function isExecutable2(file) {
|
|
1487
|
+
try {
|
|
1488
|
+
fsSync2.accessSync(file, fsSync2.constants.X_OK);
|
|
1489
|
+
return true;
|
|
1490
|
+
} catch {
|
|
1491
|
+
return false;
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
function resolveExecutable2(playerBin) {
|
|
1495
|
+
if (playerBin.includes(path3.sep)) {
|
|
1496
|
+
return isExecutable2(playerBin) ? playerBin : undefined;
|
|
1497
|
+
}
|
|
1498
|
+
const envPath = process.env.PATH ?? "";
|
|
1499
|
+
for (const dir of envPath.split(path3.delimiter)) {
|
|
1500
|
+
if (!dir)
|
|
1501
|
+
continue;
|
|
1502
|
+
const candidate = path3.join(dir, playerBin);
|
|
1503
|
+
if (isExecutable2(candidate))
|
|
1504
|
+
return candidate;
|
|
1505
|
+
}
|
|
1506
|
+
return;
|
|
1507
|
+
}
|
|
1508
|
+
function buildPlayerArgs(playerBin, args, file) {
|
|
1509
|
+
const base = toBasePlayer(playerBin);
|
|
1510
|
+
const defaults = PLAYER_DEFAULT_ARGS[base] ?? [];
|
|
1511
|
+
return [...defaults, ...args, file];
|
|
1512
|
+
}
|
|
1513
|
+
function describePlayer(playerBin) {
|
|
1514
|
+
const backend = toBasePlayer(playerBin);
|
|
1515
|
+
return { audioPlayer: playerBin, backend, player: backend };
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
class PlayerService {
|
|
1519
|
+
config;
|
|
1520
|
+
logger;
|
|
1521
|
+
timers;
|
|
1522
|
+
onBackend;
|
|
1523
|
+
resolvedPlayer;
|
|
1524
|
+
constructor(config, logger, timers, onBackend) {
|
|
1525
|
+
this.config = config;
|
|
1526
|
+
this.logger = logger;
|
|
1527
|
+
this.timers = timers;
|
|
1528
|
+
this.onBackend = onBackend;
|
|
1529
|
+
}
|
|
1530
|
+
buildArgs(file) {
|
|
1531
|
+
const playerBin = this.resolvedPlayer ?? this.config.audioPlayer;
|
|
1532
|
+
return buildPlayerArgs(playerBin, this.config.audioPlayerArgs, file);
|
|
1533
|
+
}
|
|
1534
|
+
resolvePlayerBin() {
|
|
1535
|
+
if (this.resolvedPlayer)
|
|
1536
|
+
return this.resolvedPlayer;
|
|
1537
|
+
const preferred = this.config.audioPlayer.trim();
|
|
1538
|
+
const candidates = preferred && preferred !== "auto" ? [preferred, ...PLAYER_CANDIDATES] : [...PLAYER_CANDIDATES];
|
|
1539
|
+
this.logger.info("resolve player start", { preferred, candidates });
|
|
1540
|
+
for (const candidate of candidates) {
|
|
1541
|
+
const resolved = resolveExecutable2(candidate);
|
|
1542
|
+
this.logger.info("resolve player candidate", { candidate, resolved: resolved ?? null });
|
|
1543
|
+
if (!resolved)
|
|
1544
|
+
continue;
|
|
1545
|
+
this.resolvedPlayer = resolved;
|
|
1546
|
+
const backend = toBasePlayer(resolved);
|
|
1547
|
+
this.onBackend(backend);
|
|
1548
|
+
this.logger.info("resolve player success", describePlayer(resolved));
|
|
1549
|
+
return resolved;
|
|
1550
|
+
}
|
|
1551
|
+
throw new Error(`No supported audio player found. Install one of: ${PLAYER_CANDIDATES.join(", ")}`);
|
|
1552
|
+
}
|
|
1553
|
+
async playFile(file, current) {
|
|
1554
|
+
const playerBin = this.resolvePlayerBin();
|
|
1555
|
+
await new Promise((resolve, reject) => {
|
|
1556
|
+
const args = this.buildArgs(file);
|
|
1557
|
+
this.logger.info("playback spawn", {
|
|
1558
|
+
itemID: current.id,
|
|
1559
|
+
playerBin,
|
|
1560
|
+
args,
|
|
1561
|
+
file
|
|
1562
|
+
});
|
|
1563
|
+
const child = spawn2(playerBin, args, {
|
|
1564
|
+
stdio: ["ignore", "ignore", "pipe"]
|
|
1565
|
+
});
|
|
1566
|
+
current.child = child;
|
|
1567
|
+
let settled = false;
|
|
1568
|
+
let stderr = "";
|
|
1569
|
+
child.stderr?.on("data", (chunk) => {
|
|
1570
|
+
stderr = `${stderr}${String(chunk)}`.slice(-2000);
|
|
1571
|
+
});
|
|
1572
|
+
const done = (callback) => {
|
|
1573
|
+
if (settled)
|
|
1574
|
+
return;
|
|
1575
|
+
settled = true;
|
|
1576
|
+
callback();
|
|
1577
|
+
};
|
|
1578
|
+
child.once("error", (error) => {
|
|
1579
|
+
this.logger.error("playback process error", {
|
|
1580
|
+
itemID: current.id,
|
|
1581
|
+
playerBin,
|
|
1582
|
+
error: formatError(error),
|
|
1583
|
+
stderr: stderr || undefined
|
|
1584
|
+
});
|
|
1585
|
+
done(() => reject(error));
|
|
1586
|
+
});
|
|
1587
|
+
child.once("exit", (code, signal) => {
|
|
1588
|
+
this.logger.info("playback process exit", {
|
|
1589
|
+
itemID: current.id,
|
|
1590
|
+
playerBin,
|
|
1591
|
+
code,
|
|
1592
|
+
signal,
|
|
1593
|
+
interrupted: current.interrupted,
|
|
1594
|
+
stderr: stderr.trim() || undefined
|
|
1595
|
+
});
|
|
1596
|
+
if (current.interrupted && (signal || code !== 0)) {
|
|
1597
|
+
done(resolve);
|
|
1598
|
+
return;
|
|
1599
|
+
}
|
|
1600
|
+
if (code === 0) {
|
|
1601
|
+
done(resolve);
|
|
1602
|
+
return;
|
|
1603
|
+
}
|
|
1604
|
+
done(() => reject(new Error(`${toBasePlayer(playerBin)} exited with code ${code ?? "unknown"}`)));
|
|
1605
|
+
});
|
|
1606
|
+
});
|
|
1607
|
+
}
|
|
1608
|
+
stopChild(child) {
|
|
1609
|
+
if (!child || child.killed)
|
|
1610
|
+
return;
|
|
1611
|
+
this.logger.info("stop child", { pid: child.pid ?? null });
|
|
1612
|
+
child.kill("SIGTERM");
|
|
1613
|
+
this.timers.setTimeout(() => {
|
|
1614
|
+
if (!child.killed)
|
|
1615
|
+
child.kill("SIGKILL");
|
|
1616
|
+
}, 250);
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
// src/playback/queue.ts
|
|
1621
|
+
var STREAM_COALESCE_QUEUE_THRESHOLD = 4;
|
|
1622
|
+
var MAX_STREAM_QUEUE_ITEMS = 32;
|
|
1623
|
+
var RECENT_CHUNK_TRACE_LIMIT = 200;
|
|
1624
|
+
var RECENT_CHUNK_TRACE_WINDOW_MS = 5 * 60 * 1000;
|
|
1625
|
+
|
|
1626
|
+
class PlaybackQueue {
|
|
1627
|
+
sourceItems = [];
|
|
1628
|
+
readyAudio = [];
|
|
1629
|
+
recentChunkTraces = new Map;
|
|
1630
|
+
queueID = 0;
|
|
1631
|
+
readyAudioID = 0;
|
|
1632
|
+
enqueuePreparedChunk(text, pauseMs, source, epoch, maxSpeechChunkChars, trace, voice) {
|
|
1633
|
+
if (!text.trim())
|
|
1634
|
+
return [];
|
|
1635
|
+
const events = [];
|
|
1636
|
+
if (source === "stream") {
|
|
1637
|
+
const coalesced = this.coalesceStreamChunk(text, pauseMs, epoch, maxSpeechChunkChars, trace);
|
|
1638
|
+
if (coalesced)
|
|
1639
|
+
events.push(coalesced);
|
|
1640
|
+
}
|
|
1641
|
+
if (events.length === 0) {
|
|
1642
|
+
const fingerprint = textFingerprint(text);
|
|
1643
|
+
const duplicate = this.recentChunkTraces.get(fingerprint);
|
|
1644
|
+
const item = {
|
|
1645
|
+
id: this.queueID++,
|
|
1646
|
+
epoch,
|
|
1647
|
+
text,
|
|
1648
|
+
pauseMs,
|
|
1649
|
+
source,
|
|
1650
|
+
voice,
|
|
1651
|
+
trace
|
|
1652
|
+
};
|
|
1653
|
+
this.sourceItems.push(item);
|
|
1654
|
+
this.rememberChunkTrace(fingerprint, item, text);
|
|
1655
|
+
events.push({ type: "enqueued", item, fingerprint, duplicate });
|
|
1656
|
+
}
|
|
1657
|
+
if (source === "stream")
|
|
1658
|
+
events.push(...this.trimStreamQueue());
|
|
1659
|
+
return events;
|
|
1660
|
+
}
|
|
1661
|
+
takeSourceItem() {
|
|
1662
|
+
return this.sourceItems.shift();
|
|
1663
|
+
}
|
|
1664
|
+
takeReadyAudioItem() {
|
|
1665
|
+
return this.readyAudio.shift();
|
|
1666
|
+
}
|
|
1667
|
+
enqueueReadyAudio(item) {
|
|
1668
|
+
this.readyAudio.push(item);
|
|
1669
|
+
}
|
|
1670
|
+
createPauseReadyAudioItem(sourceItem) {
|
|
1671
|
+
return {
|
|
1672
|
+
id: this.readyAudioID++,
|
|
1673
|
+
epoch: sourceItem.epoch,
|
|
1674
|
+
sourceItem,
|
|
1675
|
+
kind: "pause",
|
|
1676
|
+
durationMs: sourceItem.pauseMs
|
|
1677
|
+
};
|
|
1678
|
+
}
|
|
1679
|
+
createAudioReadyItem(sourceItem, generated) {
|
|
1680
|
+
return {
|
|
1681
|
+
id: this.readyAudioID++,
|
|
1682
|
+
epoch: sourceItem.epoch,
|
|
1683
|
+
sourceItem,
|
|
1684
|
+
kind: "audio",
|
|
1685
|
+
text: generated.text,
|
|
1686
|
+
file: generated.file
|
|
1687
|
+
};
|
|
1688
|
+
}
|
|
1689
|
+
clearSourceItems() {
|
|
1690
|
+
this.sourceItems.length = 0;
|
|
1691
|
+
}
|
|
1692
|
+
clearReadyAudio() {
|
|
1693
|
+
return this.readyAudio.splice(0);
|
|
1694
|
+
}
|
|
1695
|
+
cloneQueueItems(items, epoch) {
|
|
1696
|
+
return items.map((item) => ({ ...item, epoch }));
|
|
1697
|
+
}
|
|
1698
|
+
bufferedAudioCount() {
|
|
1699
|
+
return this.readyAudio.filter((item) => item.kind === "audio").length;
|
|
1700
|
+
}
|
|
1701
|
+
snapshot() {
|
|
1702
|
+
return {
|
|
1703
|
+
queueLength: this.sourceItems.length,
|
|
1704
|
+
readyAudioLength: this.readyAudio.length,
|
|
1705
|
+
recentChunkTraceLength: this.recentChunkTraces.size
|
|
1706
|
+
};
|
|
1707
|
+
}
|
|
1708
|
+
coalesceStreamChunk(text, pauseMs, epoch, maxSpeechChunkChars, trace) {
|
|
1709
|
+
if (this.sourceItems.length < STREAM_COALESCE_QUEUE_THRESHOLD)
|
|
1710
|
+
return;
|
|
1711
|
+
const previous = this.sourceItems.at(-1);
|
|
1712
|
+
if (!previous || previous.source !== "stream" || previous.epoch !== epoch)
|
|
1713
|
+
return;
|
|
1714
|
+
const previousFingerprint = textFingerprint(previous.text);
|
|
1715
|
+
const merged = `${previous.text} ${text}`.trim();
|
|
1716
|
+
if (merged.length > maxSpeechChunkChars)
|
|
1717
|
+
return;
|
|
1718
|
+
previous.text = merged;
|
|
1719
|
+
previous.pauseMs = Math.max(previous.pauseMs, pauseMs);
|
|
1720
|
+
previous.trace = {
|
|
1721
|
+
...previous.trace,
|
|
1722
|
+
origin: "stream.coalesced",
|
|
1723
|
+
chunkCount: (previous.trace?.chunkCount ?? 1) + 1,
|
|
1724
|
+
restLength: trace?.restLength ?? previous.trace?.restLength
|
|
1725
|
+
};
|
|
1726
|
+
const fingerprint = textFingerprint(previous.text);
|
|
1727
|
+
this.recentChunkTraces.delete(previousFingerprint);
|
|
1728
|
+
this.rememberChunkTrace(fingerprint, previous, previous.text);
|
|
1729
|
+
return { type: "coalesced", item: previous, fingerprint, addedFingerprint: textFingerprint(text), addedTrace: trace };
|
|
1730
|
+
}
|
|
1731
|
+
rememberChunkTrace(fingerprint, item, text) {
|
|
1732
|
+
const now = Date.now();
|
|
1733
|
+
this.recentChunkTraces.set(fingerprint, {
|
|
1734
|
+
queuedAt: now,
|
|
1735
|
+
itemID: item.id,
|
|
1736
|
+
source: item.source,
|
|
1737
|
+
epoch: item.epoch,
|
|
1738
|
+
trace: item.trace,
|
|
1739
|
+
preview: textPreview(text)
|
|
1740
|
+
});
|
|
1741
|
+
for (const [key, value] of this.recentChunkTraces) {
|
|
1742
|
+
if (this.recentChunkTraces.size <= RECENT_CHUNK_TRACE_LIMIT && now - value.queuedAt <= RECENT_CHUNK_TRACE_WINDOW_MS)
|
|
1743
|
+
break;
|
|
1744
|
+
this.recentChunkTraces.delete(key);
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
trimStreamQueue() {
|
|
1748
|
+
if (this.sourceItems.length <= MAX_STREAM_QUEUE_ITEMS)
|
|
1749
|
+
return [];
|
|
1750
|
+
let dropCount = this.sourceItems.length - MAX_STREAM_QUEUE_ITEMS;
|
|
1751
|
+
for (let index = 0;index < this.sourceItems.length && dropCount > 0; ) {
|
|
1752
|
+
const item = this.sourceItems[index];
|
|
1753
|
+
if (!item || item.source !== "stream") {
|
|
1754
|
+
index += 1;
|
|
1755
|
+
continue;
|
|
1756
|
+
}
|
|
1757
|
+
this.sourceItems.splice(index, 1);
|
|
1758
|
+
dropCount -= 1;
|
|
1759
|
+
}
|
|
1760
|
+
if (dropCount === 0) {
|
|
1761
|
+
return [{ type: "stream-trimmed", queueLength: this.sourceItems.length, maxQueueLength: MAX_STREAM_QUEUE_ITEMS }];
|
|
1762
|
+
}
|
|
1763
|
+
return [];
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
|
|
1767
|
+
// src/playback/playback-pipeline.ts
|
|
1768
|
+
var READY_AUDIO_BUFFER = 1;
|
|
1769
|
+
|
|
1770
|
+
class PlaybackPipeline {
|
|
1771
|
+
config;
|
|
1772
|
+
state;
|
|
1773
|
+
timers;
|
|
1774
|
+
toast;
|
|
1775
|
+
log = createLogger("playback");
|
|
1776
|
+
runtime;
|
|
1777
|
+
player;
|
|
1778
|
+
queue = new PlaybackQueue;
|
|
1779
|
+
currentGenerate;
|
|
1780
|
+
currentPlay;
|
|
1781
|
+
queueWake;
|
|
1782
|
+
playbackWake;
|
|
1783
|
+
loopStarted = false;
|
|
1784
|
+
disposed = false;
|
|
1785
|
+
epoch = 0;
|
|
1786
|
+
lastToastAt = 0;
|
|
1787
|
+
constructor(config, state, timers, toast) {
|
|
1788
|
+
this.config = config;
|
|
1789
|
+
this.state = state;
|
|
1790
|
+
this.timers = timers;
|
|
1791
|
+
this.toast = toast;
|
|
1792
|
+
this.runtime = new TtsHelperRuntime(config, (status) => this.state.patch(status), createLogger("helper"));
|
|
1793
|
+
this.player = new PlayerService(config, this.log, timers, (backend) => this.state.patch({ backend }));
|
|
1794
|
+
}
|
|
1795
|
+
start() {
|
|
1796
|
+
if (this.loopStarted)
|
|
1797
|
+
return;
|
|
1798
|
+
this.loopStarted = true;
|
|
1799
|
+
this.runGenerationLoop();
|
|
1800
|
+
this.runPlaybackLoop();
|
|
1801
|
+
}
|
|
1802
|
+
enqueuePreparedChunk(text, pauseMs, source, trace) {
|
|
1803
|
+
const events = this.queue.enqueuePreparedChunk(text, pauseMs, source, this.epoch, this.config.maxSpeechChunkChars, trace);
|
|
1804
|
+
for (const event of events) {
|
|
1805
|
+
if (event.type === "enqueued") {
|
|
1806
|
+
if (event.duplicate) {
|
|
1807
|
+
this.log.warn("duplicate chunk enqueued", {
|
|
1808
|
+
itemID: event.item.id,
|
|
1809
|
+
source,
|
|
1810
|
+
epoch: event.item.epoch,
|
|
1811
|
+
textLength: text.length,
|
|
1812
|
+
fingerprint: event.fingerprint,
|
|
1813
|
+
preview: textPreview(text),
|
|
1814
|
+
trace,
|
|
1815
|
+
previousItemID: event.duplicate.itemID,
|
|
1816
|
+
previousSource: event.duplicate.source,
|
|
1817
|
+
previousEpoch: event.duplicate.epoch,
|
|
1818
|
+
previousAgeMs: Date.now() - event.duplicate.queuedAt,
|
|
1819
|
+
previousPreview: event.duplicate.preview,
|
|
1820
|
+
previousTrace: event.duplicate.trace
|
|
1821
|
+
});
|
|
1822
|
+
}
|
|
1823
|
+
this.log.info("chunk enqueued", {
|
|
1824
|
+
itemID: event.item.id,
|
|
1825
|
+
source,
|
|
1826
|
+
epoch: event.item.epoch,
|
|
1827
|
+
textLength: text.length,
|
|
1828
|
+
fingerprint: event.fingerprint,
|
|
1829
|
+
pauseMs,
|
|
1830
|
+
queueLength: this.queue.sourceItems.length,
|
|
1831
|
+
preview: textPreview(text),
|
|
1832
|
+
trace
|
|
1833
|
+
});
|
|
1834
|
+
continue;
|
|
1835
|
+
}
|
|
1836
|
+
if (event.type === "coalesced") {
|
|
1837
|
+
this.log.debug("stream chunk coalesced", {
|
|
1838
|
+
itemID: event.item.id,
|
|
1839
|
+
textLength: event.item.text.length,
|
|
1840
|
+
fingerprint: event.fingerprint,
|
|
1841
|
+
addedFingerprint: event.addedFingerprint,
|
|
1842
|
+
queueLength: this.queue.sourceItems.length,
|
|
1843
|
+
addedTrace: event.addedTrace,
|
|
1844
|
+
trace: event.item.trace
|
|
1845
|
+
});
|
|
1846
|
+
continue;
|
|
1847
|
+
}
|
|
1848
|
+
this.log.warn("stream queue trimmed", { queueLength: event.queueLength, maxQueueLength: event.maxQueueLength });
|
|
1849
|
+
}
|
|
1850
|
+
if (events.length > 0)
|
|
1851
|
+
this.notify();
|
|
1852
|
+
return events.length;
|
|
1853
|
+
}
|
|
1854
|
+
previewVoice(voice) {
|
|
1855
|
+
const state = this.state.snapshot();
|
|
1856
|
+
if (!state.enabled || state.paused) {
|
|
1857
|
+
this.toast({ variant: "warning", message: "Enable and resume speech to preview a voice" });
|
|
1858
|
+
return;
|
|
1859
|
+
}
|
|
1860
|
+
const wasBusy = this.isBusy();
|
|
1861
|
+
const events = this.queue.enqueuePreparedChunk("Hello! This is a preview of my voice.", 0, "preview", this.epoch, this.config.maxSpeechChunkChars, { origin: "voice.preview" }, voice);
|
|
1862
|
+
if (events.length) {
|
|
1863
|
+
if (wasBusy)
|
|
1864
|
+
this.toast({ variant: "info", message: "Voice preview queued after current speech" });
|
|
1865
|
+
this.notify();
|
|
1866
|
+
}
|
|
1867
|
+
}
|
|
1868
|
+
isBusy() {
|
|
1869
|
+
return Boolean(this.currentGenerate) || Boolean(this.currentPlay) || this.queue.readyAudio.length > 0 || this.queue.sourceItems.length > 0;
|
|
1870
|
+
}
|
|
1871
|
+
pause() {
|
|
1872
|
+
const requeued = this.collectBufferedSourceItems();
|
|
1873
|
+
const bufferedReadyAudio = this.queue.clearReadyAudio();
|
|
1874
|
+
this.log.info("pause queue", {
|
|
1875
|
+
queueLength: this.queue.sourceItems.length,
|
|
1876
|
+
readyAudioLength: bufferedReadyAudio.length,
|
|
1877
|
+
hasCurrentGenerate: Boolean(this.currentGenerate),
|
|
1878
|
+
hasCurrentPlay: Boolean(this.currentPlay),
|
|
1879
|
+
requeuedCount: requeued.length
|
|
1880
|
+
});
|
|
1881
|
+
this.state.patch({ paused: true });
|
|
1882
|
+
if (requeued.length > 0)
|
|
1883
|
+
this.queue.sourceItems.unshift(...requeued);
|
|
1884
|
+
if (this.currentGenerate) {
|
|
1885
|
+
this.currentGenerate.interrupted = true;
|
|
1886
|
+
}
|
|
1887
|
+
if (this.currentPlay) {
|
|
1888
|
+
this.currentPlay.interrupted = true;
|
|
1889
|
+
if (this.currentPlay.item.kind === "audio")
|
|
1890
|
+
this.player.stopChild(this.currentPlay.child);
|
|
1891
|
+
}
|
|
1892
|
+
if (bufferedReadyAudio.length > 0) {
|
|
1893
|
+
this.discardReadyAudio(bufferedReadyAudio);
|
|
1894
|
+
}
|
|
1895
|
+
this.notify();
|
|
1896
|
+
}
|
|
1897
|
+
resume() {
|
|
1898
|
+
this.log.info("resume queue", {
|
|
1899
|
+
queueLength: this.queue.sourceItems.length,
|
|
1900
|
+
readyAudioLength: this.queue.readyAudio.length
|
|
1901
|
+
});
|
|
1902
|
+
this.state.patch({ paused: false });
|
|
1903
|
+
this.notify();
|
|
1904
|
+
}
|
|
1905
|
+
async reset(requeueCurrent) {
|
|
1906
|
+
const requeued = requeueCurrent ? this.queue.cloneQueueItems(this.collectBufferedSourceItems(), this.epoch + 1) : [];
|
|
1907
|
+
const bufferedReadyAudio = this.queue.clearReadyAudio();
|
|
1908
|
+
this.log.info("reset queue", {
|
|
1909
|
+
requeueCurrent,
|
|
1910
|
+
queueLength: this.queue.sourceItems.length,
|
|
1911
|
+
readyAudioLength: bufferedReadyAudio.length,
|
|
1912
|
+
hasCurrentGenerate: Boolean(this.currentGenerate),
|
|
1913
|
+
hasCurrentPlay: Boolean(this.currentPlay),
|
|
1914
|
+
requeuedCount: requeued.length,
|
|
1915
|
+
nextEpoch: this.epoch + 1
|
|
1916
|
+
});
|
|
1917
|
+
this.epoch += 1;
|
|
1918
|
+
this.runtime.cancelEpoch(this.epoch - 1);
|
|
1919
|
+
this.queue.clearSourceItems();
|
|
1920
|
+
if (requeued.length > 0)
|
|
1921
|
+
this.queue.sourceItems.push(...requeued);
|
|
1922
|
+
if (this.currentGenerate) {
|
|
1923
|
+
this.currentGenerate.interrupted = true;
|
|
1924
|
+
this.runtime.cancelEpoch(this.currentGenerate.item.epoch);
|
|
1925
|
+
}
|
|
1926
|
+
if (this.currentPlay) {
|
|
1927
|
+
this.currentPlay.interrupted = true;
|
|
1928
|
+
if (this.currentPlay.item.kind === "audio")
|
|
1929
|
+
this.player.stopChild(this.currentPlay.child);
|
|
1930
|
+
}
|
|
1931
|
+
await this.discardReadyAudio(bufferedReadyAudio);
|
|
1932
|
+
this.notify();
|
|
1933
|
+
}
|
|
1934
|
+
async dispose() {
|
|
1935
|
+
if (this.disposed)
|
|
1936
|
+
return;
|
|
1937
|
+
const hadCurrentAudio = this.currentPlay?.item.kind === "audio";
|
|
1938
|
+
this.disposed = true;
|
|
1939
|
+
await this.reset(false);
|
|
1940
|
+
if (hadCurrentAudio)
|
|
1941
|
+
await this.timers.sleep(260);
|
|
1942
|
+
this.notify();
|
|
1943
|
+
this.log.info("dispose");
|
|
1944
|
+
await this.runtime.dispose().catch((error) => {
|
|
1945
|
+
this.log.warn("helper dispose failed", { error: formatError(error) });
|
|
1946
|
+
});
|
|
1947
|
+
}
|
|
1948
|
+
async runGenerationLoop() {
|
|
1949
|
+
this.log.info("generation loop started");
|
|
1950
|
+
while (!this.disposed) {
|
|
1951
|
+
const item = await this.takeNextQueueItem();
|
|
1952
|
+
if (!item || this.disposed)
|
|
1953
|
+
continue;
|
|
1954
|
+
this.log.info("queue item start", {
|
|
1955
|
+
itemID: item.id,
|
|
1956
|
+
source: item.source,
|
|
1957
|
+
textLength: item.text.length,
|
|
1958
|
+
fingerprint: textFingerprint(item.text),
|
|
1959
|
+
preview: textPreview(item.text),
|
|
1960
|
+
trace: item.trace,
|
|
1961
|
+
pauseMs: item.pauseMs,
|
|
1962
|
+
queueRemaining: this.queue.sourceItems.length
|
|
1963
|
+
});
|
|
1964
|
+
const current = {
|
|
1965
|
+
item,
|
|
1966
|
+
interrupted: false
|
|
1967
|
+
};
|
|
1968
|
+
this.currentGenerate = current;
|
|
1969
|
+
this.syncActivity();
|
|
1970
|
+
let segmentCount = 0;
|
|
1971
|
+
let queuedPause = false;
|
|
1972
|
+
try {
|
|
1973
|
+
for await (const generated of this.runtime.stream(item.text, item.epoch, item.voice)) {
|
|
1974
|
+
if (!await this.waitForReadyAudioCapacity(current)) {
|
|
1975
|
+
await fs2.unlink(generated.file).catch(() => {
|
|
1976
|
+
return;
|
|
1977
|
+
});
|
|
1978
|
+
break;
|
|
1979
|
+
}
|
|
1980
|
+
const ready = await this.createAudioReadyItem(item, generated);
|
|
1981
|
+
if (!ready) {
|
|
1982
|
+
if (current.interrupted || this.isStale(item))
|
|
1983
|
+
break;
|
|
1984
|
+
continue;
|
|
1985
|
+
}
|
|
1986
|
+
if (current.interrupted || this.isStale(item)) {
|
|
1987
|
+
await this.discardReadyAudio([ready]);
|
|
1988
|
+
break;
|
|
1989
|
+
}
|
|
1990
|
+
this.enqueueReadyAudio(ready);
|
|
1991
|
+
segmentCount += 1;
|
|
1992
|
+
}
|
|
1993
|
+
if (!current.interrupted && !this.isStale(item) && item.pauseMs > 0) {
|
|
1994
|
+
const canQueuePause = await this.waitForReadyAudioCapacity(current);
|
|
1995
|
+
if (canQueuePause && !current.interrupted && !this.isStale(item)) {
|
|
1996
|
+
this.enqueueReadyAudio(this.queue.createPauseReadyAudioItem(item));
|
|
1997
|
+
queuedPause = true;
|
|
1998
|
+
}
|
|
1999
|
+
}
|
|
2000
|
+
if (current.interrupted) {
|
|
2001
|
+
this.log.info("queue item interrupted", {
|
|
2002
|
+
itemID: item.id,
|
|
2003
|
+
source: item.source,
|
|
2004
|
+
segmentCount
|
|
2005
|
+
});
|
|
2006
|
+
continue;
|
|
2007
|
+
}
|
|
2008
|
+
if (this.isStale(item)) {
|
|
2009
|
+
this.log.warn("queue item stale after generate", { itemID: item.id, epoch: item.epoch, currentEpoch: this.epoch });
|
|
2010
|
+
continue;
|
|
2011
|
+
}
|
|
2012
|
+
this.log.info("queue item generated", {
|
|
2013
|
+
itemID: item.id,
|
|
2014
|
+
source: item.source,
|
|
2015
|
+
segmentCount,
|
|
2016
|
+
queuedPause,
|
|
2017
|
+
readyAudioLength: this.queue.readyAudio.length
|
|
2018
|
+
});
|
|
2019
|
+
} catch (error) {
|
|
2020
|
+
this.log.error("queue item failed", {
|
|
2021
|
+
itemID: item.id,
|
|
2022
|
+
source: item.source,
|
|
2023
|
+
phase: "generate",
|
|
2024
|
+
error: formatError(error)
|
|
2025
|
+
});
|
|
2026
|
+
if (!current.interrupted && !this.isStale(item)) {
|
|
2027
|
+
await this.handleRuntimeError(error);
|
|
2028
|
+
}
|
|
2029
|
+
} finally {
|
|
2030
|
+
if (this.currentGenerate === current)
|
|
2031
|
+
this.currentGenerate = undefined;
|
|
2032
|
+
this.syncActivity();
|
|
2033
|
+
this.notify();
|
|
2034
|
+
}
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
async runPlaybackLoop() {
|
|
2038
|
+
this.log.info("playback loop started");
|
|
2039
|
+
while (!this.disposed) {
|
|
2040
|
+
const item = await this.takeNextReadyAudio();
|
|
2041
|
+
if (!item || this.disposed)
|
|
2042
|
+
continue;
|
|
2043
|
+
const current = {
|
|
2044
|
+
id: item.id,
|
|
2045
|
+
item,
|
|
2046
|
+
interrupted: false
|
|
2047
|
+
};
|
|
2048
|
+
this.currentPlay = current;
|
|
2049
|
+
this.syncActivity();
|
|
2050
|
+
try {
|
|
2051
|
+
if (item.kind === "pause") {
|
|
2052
|
+
this.log.info("playback pause start", {
|
|
2053
|
+
readyAudioID: item.id,
|
|
2054
|
+
sourceItemID: item.sourceItem.id,
|
|
2055
|
+
durationMs: item.durationMs
|
|
2056
|
+
});
|
|
2057
|
+
if (item.durationMs > 0)
|
|
2058
|
+
await this.timers.sleep(scalePauseForSpeed(item.durationMs, this.config.speed));
|
|
2059
|
+
} else {
|
|
2060
|
+
this.log.info("ready audio start", {
|
|
2061
|
+
readyAudioID: item.id,
|
|
2062
|
+
sourceItemID: item.sourceItem.id,
|
|
2063
|
+
textLength: item.text.length,
|
|
2064
|
+
fingerprint: textFingerprint(item.text),
|
|
2065
|
+
preview: textPreview(item.text),
|
|
2066
|
+
readyAudioRemaining: this.queue.readyAudio.length
|
|
2067
|
+
});
|
|
2068
|
+
await this.player.playFile(item.file, current);
|
|
2069
|
+
}
|
|
2070
|
+
if (current.interrupted) {
|
|
2071
|
+
this.log.info("ready audio interrupted", {
|
|
2072
|
+
readyAudioID: item.id,
|
|
2073
|
+
sourceItemID: item.sourceItem.id,
|
|
2074
|
+
kind: item.kind
|
|
2075
|
+
});
|
|
2076
|
+
continue;
|
|
2077
|
+
}
|
|
2078
|
+
if (this.isStale(item.sourceItem)) {
|
|
2079
|
+
this.log.warn("ready audio stale after playback", {
|
|
2080
|
+
readyAudioID: item.id,
|
|
2081
|
+
sourceItemID: item.sourceItem.id,
|
|
2082
|
+
epoch: item.sourceItem.epoch,
|
|
2083
|
+
currentEpoch: this.epoch
|
|
2084
|
+
});
|
|
2085
|
+
continue;
|
|
2086
|
+
}
|
|
2087
|
+
this.log.info("ready audio complete", {
|
|
2088
|
+
readyAudioID: item.id,
|
|
2089
|
+
sourceItemID: item.sourceItem.id,
|
|
2090
|
+
kind: item.kind,
|
|
2091
|
+
source: item.sourceItem.source,
|
|
2092
|
+
trace: item.sourceItem.trace
|
|
2093
|
+
});
|
|
2094
|
+
} catch (error) {
|
|
2095
|
+
this.log.error("ready audio failed", {
|
|
2096
|
+
readyAudioID: item.id,
|
|
2097
|
+
sourceItemID: item.sourceItem.id,
|
|
2098
|
+
kind: item.kind,
|
|
2099
|
+
phase: "play",
|
|
2100
|
+
error: formatError(error)
|
|
2101
|
+
});
|
|
2102
|
+
if (!current.interrupted && !this.isStale(item.sourceItem)) {
|
|
2103
|
+
await this.handleRuntimeError(error);
|
|
2104
|
+
}
|
|
2105
|
+
} finally {
|
|
2106
|
+
if (item.kind === "audio")
|
|
2107
|
+
await fs2.unlink(item.file).catch(() => {
|
|
2108
|
+
return;
|
|
2109
|
+
});
|
|
2110
|
+
if (this.currentPlay === current)
|
|
2111
|
+
this.currentPlay = undefined;
|
|
2112
|
+
this.syncActivity();
|
|
2113
|
+
this.notify();
|
|
2114
|
+
}
|
|
2115
|
+
}
|
|
2116
|
+
}
|
|
2117
|
+
async takeNextQueueItem() {
|
|
2118
|
+
while (!this.disposed) {
|
|
2119
|
+
const state = this.state.snapshot();
|
|
2120
|
+
if (state.enabled && !state.paused && this.queue.bufferedAudioCount() < READY_AUDIO_BUFFER && this.queue.sourceItems.length > 0) {
|
|
2121
|
+
const item = this.queue.takeSourceItem();
|
|
2122
|
+
if (!item || this.isStale(item))
|
|
2123
|
+
continue;
|
|
2124
|
+
return item;
|
|
2125
|
+
}
|
|
2126
|
+
this.syncActivity();
|
|
2127
|
+
await this.waitForQueueSignal();
|
|
2128
|
+
}
|
|
2129
|
+
return;
|
|
2130
|
+
}
|
|
2131
|
+
async waitForReadyAudioCapacity(current) {
|
|
2132
|
+
while (!this.disposed && !current.interrupted && !this.isStale(current.item)) {
|
|
2133
|
+
const state = this.state.snapshot();
|
|
2134
|
+
if (state.enabled && !state.paused && this.queue.bufferedAudioCount() < READY_AUDIO_BUFFER) {
|
|
2135
|
+
return true;
|
|
2136
|
+
}
|
|
2137
|
+
this.syncActivity();
|
|
2138
|
+
await this.waitForQueueSignal();
|
|
2139
|
+
}
|
|
2140
|
+
return false;
|
|
2141
|
+
}
|
|
2142
|
+
async takeNextReadyAudio() {
|
|
2143
|
+
while (!this.disposed) {
|
|
2144
|
+
const state = this.state.snapshot();
|
|
2145
|
+
if (state.enabled && !state.paused && this.queue.readyAudio.length > 0) {
|
|
2146
|
+
const item = this.queue.takeReadyAudioItem();
|
|
2147
|
+
if (!item)
|
|
2148
|
+
continue;
|
|
2149
|
+
if (this.isStale(item.sourceItem)) {
|
|
2150
|
+
await this.discardReadyAudio([item]);
|
|
2151
|
+
continue;
|
|
2152
|
+
}
|
|
2153
|
+
return item;
|
|
2154
|
+
}
|
|
2155
|
+
this.syncActivity();
|
|
2156
|
+
await this.waitForPlaybackSignal();
|
|
2157
|
+
}
|
|
2158
|
+
return;
|
|
2159
|
+
}
|
|
2160
|
+
waitForQueueSignal() {
|
|
2161
|
+
return new Promise((resolve) => {
|
|
2162
|
+
this.queueWake = resolve;
|
|
2163
|
+
});
|
|
2164
|
+
}
|
|
2165
|
+
waitForPlaybackSignal() {
|
|
2166
|
+
return new Promise((resolve) => {
|
|
2167
|
+
this.playbackWake = resolve;
|
|
2168
|
+
});
|
|
2169
|
+
}
|
|
2170
|
+
notify() {
|
|
2171
|
+
const queueWake = this.queueWake;
|
|
2172
|
+
this.queueWake = undefined;
|
|
2173
|
+
queueWake?.();
|
|
2174
|
+
const playbackWake = this.playbackWake;
|
|
2175
|
+
this.playbackWake = undefined;
|
|
2176
|
+
playbackWake?.();
|
|
2177
|
+
this.syncActivity();
|
|
2178
|
+
}
|
|
2179
|
+
syncActivity() {
|
|
2180
|
+
this.state.patch({
|
|
2181
|
+
busy: this.isBusy(),
|
|
2182
|
+
generating: Boolean(this.currentGenerate),
|
|
2183
|
+
playing: Boolean(this.currentPlay)
|
|
2184
|
+
});
|
|
2185
|
+
}
|
|
2186
|
+
collectBufferedSourceItems() {
|
|
2187
|
+
const items = [];
|
|
2188
|
+
const seen = new Set;
|
|
2189
|
+
const add = (item, bufferSource) => {
|
|
2190
|
+
if (!item || this.isStale(item) || seen.has(item.id))
|
|
2191
|
+
return;
|
|
2192
|
+
seen.add(item.id);
|
|
2193
|
+
items.push(item);
|
|
2194
|
+
this.log.info("buffered item retained", {
|
|
2195
|
+
bufferSource,
|
|
2196
|
+
itemID: item.id,
|
|
2197
|
+
source: item.source,
|
|
2198
|
+
epoch: item.epoch,
|
|
2199
|
+
textLength: item.text.length,
|
|
2200
|
+
fingerprint: textFingerprint(item.text),
|
|
2201
|
+
preview: textPreview(item.text),
|
|
2202
|
+
trace: item.trace
|
|
2203
|
+
});
|
|
2204
|
+
};
|
|
2205
|
+
if (this.currentPlay?.item.kind === "audio")
|
|
2206
|
+
add(this.currentPlay.item.sourceItem, "currentPlay");
|
|
2207
|
+
for (const item of this.queue.readyAudio) {
|
|
2208
|
+
if (item.kind === "audio")
|
|
2209
|
+
add(item.sourceItem, "readyAudio");
|
|
2210
|
+
}
|
|
2211
|
+
add(this.currentGenerate?.item, "currentGenerate");
|
|
2212
|
+
return items;
|
|
2213
|
+
}
|
|
2214
|
+
isStale(item) {
|
|
2215
|
+
return item.epoch !== this.epoch || !this.state.snapshot().enabled;
|
|
2216
|
+
}
|
|
2217
|
+
enqueueReadyAudio(item) {
|
|
2218
|
+
this.queue.enqueueReadyAudio(item);
|
|
2219
|
+
if (item.kind === "pause") {
|
|
2220
|
+
this.log.info("pause queued", {
|
|
2221
|
+
readyAudioID: item.id,
|
|
2222
|
+
sourceItemID: item.sourceItem.id,
|
|
2223
|
+
source: item.sourceItem.source,
|
|
2224
|
+
durationMs: item.durationMs,
|
|
2225
|
+
readyAudioLength: this.queue.readyAudio.length,
|
|
2226
|
+
trace: item.sourceItem.trace
|
|
2227
|
+
});
|
|
2228
|
+
} else {
|
|
2229
|
+
this.log.info("audio queued", {
|
|
2230
|
+
readyAudioID: item.id,
|
|
2231
|
+
sourceItemID: item.sourceItem.id,
|
|
2232
|
+
source: item.sourceItem.source,
|
|
2233
|
+
textLength: item.text.length,
|
|
2234
|
+
fingerprint: textFingerprint(item.text),
|
|
2235
|
+
preview: textPreview(item.text),
|
|
2236
|
+
readyAudioLength: this.queue.readyAudio.length,
|
|
2237
|
+
trace: item.sourceItem.trace
|
|
2238
|
+
});
|
|
2239
|
+
}
|
|
2240
|
+
this.notify();
|
|
2241
|
+
}
|
|
2242
|
+
async createAudioReadyItem(sourceItem, generated) {
|
|
2243
|
+
const ready = this.queue.createAudioReadyItem(sourceItem, generated);
|
|
2244
|
+
if (this.isStale(sourceItem)) {
|
|
2245
|
+
await fs2.unlink(generated.file).catch(() => {
|
|
2246
|
+
return;
|
|
2247
|
+
});
|
|
2248
|
+
return;
|
|
2249
|
+
}
|
|
2250
|
+
return ready;
|
|
2251
|
+
}
|
|
2252
|
+
async discardReadyAudio(items) {
|
|
2253
|
+
if (!items.length)
|
|
2254
|
+
return;
|
|
2255
|
+
await Promise.all(items.map((item) => {
|
|
2256
|
+
if (item.kind !== "audio")
|
|
2257
|
+
return Promise.resolve();
|
|
2258
|
+
return fs2.unlink(item.file).catch(() => {
|
|
2259
|
+
return;
|
|
2260
|
+
});
|
|
2261
|
+
}));
|
|
2262
|
+
}
|
|
2263
|
+
async handleRuntimeError(error) {
|
|
2264
|
+
const message = formatError(error);
|
|
2265
|
+
this.log.error("runtime error", { error: message });
|
|
2266
|
+
this.state.patch({ error: message });
|
|
2267
|
+
if (isMissingBinary(error)) {
|
|
2268
|
+
const bufferedReadyAudio = this.queue.clearReadyAudio();
|
|
2269
|
+
this.state.setEnabled(false);
|
|
2270
|
+
this.state.patch({ paused: false });
|
|
2271
|
+
this.queue.clearSourceItems();
|
|
2272
|
+
if (this.currentGenerate) {
|
|
2273
|
+
this.currentGenerate.interrupted = true;
|
|
2274
|
+
this.runtime.cancelEpoch(this.currentGenerate.item.epoch);
|
|
2275
|
+
}
|
|
2276
|
+
if (this.currentPlay)
|
|
2277
|
+
this.currentPlay.interrupted = true;
|
|
2278
|
+
await this.discardReadyAudio(bufferedReadyAudio);
|
|
2279
|
+
this.toastError(`Playback failed. Command not found: ${this.config.audioPlayer}`);
|
|
2280
|
+
return;
|
|
2281
|
+
}
|
|
2282
|
+
this.toastError(`Speech failed. ${message}`);
|
|
2283
|
+
}
|
|
2284
|
+
toastError(message) {
|
|
2285
|
+
const now = Date.now();
|
|
2286
|
+
if (now - this.lastToastAt < 5000)
|
|
2287
|
+
return;
|
|
2288
|
+
this.lastToastAt = now;
|
|
2289
|
+
this.toast({ variant: "error", message });
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
|
|
2293
|
+
// src/session/session-store.ts
|
|
2294
|
+
class SessionStore {
|
|
2295
|
+
api;
|
|
2296
|
+
speakSubagentResponses;
|
|
2297
|
+
log = createLogger("session");
|
|
2298
|
+
sessionParents = new Map;
|
|
2299
|
+
sessionParentLookups = new Map;
|
|
2300
|
+
cleanupCallbacks = new Set;
|
|
2301
|
+
cleanup = [];
|
|
2302
|
+
disposed = false;
|
|
2303
|
+
constructor(api, speakSubagentResponses) {
|
|
2304
|
+
this.api = api;
|
|
2305
|
+
this.speakSubagentResponses = speakSubagentResponses;
|
|
2306
|
+
this.cleanup.push(api.data.on("session.deleted", (event) => this.forgetSession(event.data.sessionID)));
|
|
2307
|
+
}
|
|
2308
|
+
onDeleted(callback) {
|
|
2309
|
+
this.cleanupCallbacks.add(callback);
|
|
2310
|
+
return () => {
|
|
2311
|
+
this.cleanupCallbacks.delete(callback);
|
|
2312
|
+
};
|
|
2313
|
+
}
|
|
2314
|
+
cacheSession(sessionID, session) {
|
|
2315
|
+
const id = typeof session.id === "string" && session.id ? session.id : sessionID;
|
|
2316
|
+
const hasParentID = "parentID" in session;
|
|
2317
|
+
const parentID = typeof session.parentID === "string" && session.parentID ? session.parentID : undefined;
|
|
2318
|
+
if (hasParentID)
|
|
2319
|
+
this.sessionParents.set(id, parentID);
|
|
2320
|
+
this.sessionParentLookups.delete(id);
|
|
2321
|
+
this.log.debug("session cached", {
|
|
2322
|
+
sessionID: id,
|
|
2323
|
+
parentID: this.sessionParents.get(id) ?? null,
|
|
2324
|
+
subagent: Boolean(this.sessionParents.get(id))
|
|
2325
|
+
});
|
|
2326
|
+
}
|
|
2327
|
+
forgetSession(sessionID) {
|
|
2328
|
+
this.sessionParents.delete(sessionID);
|
|
2329
|
+
this.sessionParentLookups.delete(sessionID);
|
|
2330
|
+
for (const callback of this.cleanupCallbacks)
|
|
2331
|
+
callback(sessionID);
|
|
2332
|
+
}
|
|
2333
|
+
async shouldSpeakSession(sessionID) {
|
|
2334
|
+
if (this.speakSubagentResponses)
|
|
2335
|
+
return true;
|
|
2336
|
+
const parentID = await this.sessionParentID(sessionID);
|
|
2337
|
+
const allowed = !parentID;
|
|
2338
|
+
if (!allowed) {
|
|
2339
|
+
this.log.debug("speech skipped for subagent session", { sessionID, parentID });
|
|
2340
|
+
}
|
|
2341
|
+
return allowed;
|
|
2342
|
+
}
|
|
2343
|
+
async sessionParentID(sessionID) {
|
|
2344
|
+
if (this.sessionParents.has(sessionID))
|
|
2345
|
+
return this.sessionParents.get(sessionID);
|
|
2346
|
+
const existing = this.sessionParentLookups.get(sessionID);
|
|
2347
|
+
if (existing)
|
|
2348
|
+
return existing;
|
|
2349
|
+
const lookup = this.fetchSessionParentID(sessionID).finally(() => {
|
|
2350
|
+
this.sessionParentLookups.delete(sessionID);
|
|
2351
|
+
});
|
|
2352
|
+
this.sessionParentLookups.set(sessionID, lookup);
|
|
2353
|
+
return lookup;
|
|
2354
|
+
}
|
|
2355
|
+
dispose() {
|
|
2356
|
+
if (this.disposed)
|
|
2357
|
+
return;
|
|
2358
|
+
this.disposed = true;
|
|
2359
|
+
for (const off of this.cleanup)
|
|
2360
|
+
off();
|
|
2361
|
+
this.cleanup.length = 0;
|
|
2362
|
+
this.cleanupCallbacks.clear();
|
|
2363
|
+
}
|
|
2364
|
+
async fetchSessionParentID(sessionID) {
|
|
2365
|
+
try {
|
|
2366
|
+
const result = await this.api.client.session.get({ sessionID });
|
|
2367
|
+
this.cacheSession(sessionID, { id: result.id, parentID: result.parentID ?? null });
|
|
2368
|
+
return result.parentID;
|
|
2369
|
+
} catch (error) {
|
|
2370
|
+
this.log.warn("session lookup failed", { sessionID, error: formatError(error) });
|
|
2371
|
+
}
|
|
2372
|
+
return sessionID;
|
|
2373
|
+
}
|
|
2374
|
+
}
|
|
2375
|
+
|
|
2376
|
+
// src/session/idle-controller.ts
|
|
2377
|
+
class IdleController {
|
|
2378
|
+
api;
|
|
2379
|
+
config;
|
|
2380
|
+
state;
|
|
2381
|
+
playback;
|
|
2382
|
+
sessionStore;
|
|
2383
|
+
log = createLogger("idle");
|
|
2384
|
+
cleanup = [];
|
|
2385
|
+
disposed = false;
|
|
2386
|
+
constructor(api, config, state, playback, sessionStore) {
|
|
2387
|
+
this.api = api;
|
|
2388
|
+
this.config = config;
|
|
2389
|
+
this.state = state;
|
|
2390
|
+
this.playback = playback;
|
|
2391
|
+
this.sessionStore = sessionStore;
|
|
2392
|
+
this.cleanup.push(api.data.on("session.idle", (event) => this.runEventTask("session.idle", this.onSessionIdle(event.data.sessionID))));
|
|
2393
|
+
}
|
|
2394
|
+
dispose() {
|
|
2395
|
+
if (this.disposed)
|
|
2396
|
+
return;
|
|
2397
|
+
this.disposed = true;
|
|
2398
|
+
for (const off of this.cleanup)
|
|
2399
|
+
off();
|
|
2400
|
+
this.cleanup.length = 0;
|
|
2401
|
+
}
|
|
2402
|
+
runEventTask(eventName, task) {
|
|
2403
|
+
task.catch((error) => {
|
|
2404
|
+
this.log.warn("event handler failed", { eventName, error: formatError(error) });
|
|
2405
|
+
});
|
|
2406
|
+
}
|
|
2407
|
+
async onSessionIdle(sessionID) {
|
|
2408
|
+
if (!this.state.snapshot().enabled || !this.config.speakOnIdle || !this.config.speechBlocks.includes("idle"))
|
|
2409
|
+
return;
|
|
2410
|
+
if (!await this.sessionStore.shouldSpeakSession(sessionID))
|
|
2411
|
+
return;
|
|
2412
|
+
if (this.disposed || !this.state.snapshot().enabled)
|
|
2413
|
+
return;
|
|
2414
|
+
if (await this.sessionStore.sessionParentID(sessionID))
|
|
2415
|
+
return;
|
|
2416
|
+
if (this.disposed || !this.state.snapshot().enabled)
|
|
2417
|
+
return;
|
|
2418
|
+
const route = this.api.ui.router.current();
|
|
2419
|
+
const active = route.type === "session" ? route.sessionID : undefined;
|
|
2420
|
+
if (!active || active !== sessionID)
|
|
2421
|
+
return;
|
|
2422
|
+
this.log.info("session idle announcement", { sessionID });
|
|
2423
|
+
const chunks = splitPlaybackText(this.config.idleAnnouncement, this.config);
|
|
2424
|
+
chunks.forEach((chunk, index) => {
|
|
2425
|
+
this.playback.enqueuePreparedChunk(chunk.text, chunk.pauseMs, "idle", {
|
|
2426
|
+
origin: "session.idle",
|
|
2427
|
+
sessionID,
|
|
2428
|
+
fullTextLength: this.config.idleAnnouncement.length,
|
|
2429
|
+
chunkIndex: index,
|
|
2430
|
+
chunkCount: chunks.length
|
|
2431
|
+
});
|
|
2432
|
+
});
|
|
2433
|
+
}
|
|
2434
|
+
}
|
|
2435
|
+
|
|
2436
|
+
// src/session/question-controller.ts
|
|
2437
|
+
class QuestionController {
|
|
2438
|
+
api;
|
|
2439
|
+
config;
|
|
2440
|
+
state;
|
|
2441
|
+
playback;
|
|
2442
|
+
sessionStore;
|
|
2443
|
+
log = createLogger("question");
|
|
2444
|
+
cleanup = [];
|
|
2445
|
+
processedRequests = new Set;
|
|
2446
|
+
disposed = false;
|
|
2447
|
+
constructor(api, config, state, playback, sessionStore) {
|
|
2448
|
+
this.api = api;
|
|
2449
|
+
this.config = config;
|
|
2450
|
+
this.state = state;
|
|
2451
|
+
this.playback = playback;
|
|
2452
|
+
this.sessionStore = sessionStore;
|
|
2453
|
+
this.cleanup.push(api.data.on("form.created", (event) => this.runEventTask("form.created", this.onQuestionAsked(event.data.form))));
|
|
2454
|
+
}
|
|
2455
|
+
dispose() {
|
|
2456
|
+
if (this.disposed)
|
|
2457
|
+
return;
|
|
2458
|
+
this.disposed = true;
|
|
2459
|
+
for (const off of this.cleanup)
|
|
2460
|
+
off();
|
|
2461
|
+
this.cleanup.length = 0;
|
|
2462
|
+
this.processedRequests.clear();
|
|
2463
|
+
}
|
|
2464
|
+
runEventTask(eventName, task) {
|
|
2465
|
+
task.catch((error) => {
|
|
2466
|
+
this.log.warn("event handler failed", { eventName, error: formatError(error) });
|
|
2467
|
+
});
|
|
2468
|
+
}
|
|
2469
|
+
async onQuestionAsked(request) {
|
|
2470
|
+
if (this.processedRequests.has(request.id))
|
|
2471
|
+
return;
|
|
2472
|
+
this.processedRequests.add(request.id);
|
|
2473
|
+
if (!this.config.speakQuestions)
|
|
2474
|
+
return;
|
|
2475
|
+
const state = this.state.snapshot();
|
|
2476
|
+
if (!state.enabled || state.paused)
|
|
2477
|
+
return;
|
|
2478
|
+
const route = this.api.ui.router.current();
|
|
2479
|
+
const active = route.type === "session" ? route.sessionID : undefined;
|
|
2480
|
+
if (!active || active !== request.sessionID)
|
|
2481
|
+
return;
|
|
2482
|
+
const parentID = await this.sessionStore.sessionParentID(request.sessionID);
|
|
2483
|
+
if (this.disposed || parentID || !this.state.snapshot().enabled || this.state.snapshot().paused) {
|
|
2484
|
+
this.log.debug("question skipped for subagent session", { sessionID: request.sessionID, parentID, requestID: request.id });
|
|
2485
|
+
return;
|
|
2486
|
+
}
|
|
2487
|
+
const questionTexts = request.fields.filter((field) => !("hidden" in field) || !field.hidden).filter((field) => !("when" in field) || !field.when?.length).map((field, index) => ({ index, text: (field.title ?? "").trim() })).filter((question) => Boolean(question.text));
|
|
2488
|
+
if (questionTexts.length === 0)
|
|
2489
|
+
return;
|
|
2490
|
+
const currentQuestion = questionTexts[0];
|
|
2491
|
+
const fullText = currentQuestion.text;
|
|
2492
|
+
const currentRoute = this.api.ui.router.current();
|
|
2493
|
+
if (currentRoute.type !== "session" || currentRoute.sessionID !== request.sessionID)
|
|
2494
|
+
return;
|
|
2495
|
+
const chunks = splitPlaybackText(currentQuestion.text, this.config).map((chunk) => ({
|
|
2496
|
+
...chunk,
|
|
2497
|
+
questionIndex: currentQuestion.index
|
|
2498
|
+
}));
|
|
2499
|
+
this.log.info("question asked", {
|
|
2500
|
+
sessionID: request.sessionID,
|
|
2501
|
+
requestID: request.id,
|
|
2502
|
+
questionCount: questionTexts.length,
|
|
2503
|
+
spokenQuestionIndex: currentQuestion.index,
|
|
2504
|
+
chunkCount: chunks.length,
|
|
2505
|
+
fullTextLength: fullText.length
|
|
2506
|
+
});
|
|
2507
|
+
chunks.forEach((chunk, index) => {
|
|
2508
|
+
this.playback.enqueuePreparedChunk(chunk.text, chunk.pauseMs, "question", {
|
|
2509
|
+
origin: "question.asked",
|
|
2510
|
+
sessionID: request.sessionID,
|
|
2511
|
+
requestID: request.id,
|
|
2512
|
+
questionIndex: chunk.questionIndex,
|
|
2513
|
+
questionCount: questionTexts.length,
|
|
2514
|
+
fullTextLength: fullText.length,
|
|
2515
|
+
chunkIndex: index,
|
|
2516
|
+
chunkCount: chunks.length
|
|
2517
|
+
});
|
|
2518
|
+
});
|
|
2519
|
+
}
|
|
2520
|
+
}
|
|
2521
|
+
|
|
2522
|
+
// src/shared/timer-registry.ts
|
|
2523
|
+
class TimerRegistry {
|
|
2524
|
+
timers = new Set;
|
|
2525
|
+
disposed = false;
|
|
2526
|
+
setTimeout(callback, ms) {
|
|
2527
|
+
if (this.disposed)
|
|
2528
|
+
return;
|
|
2529
|
+
const timer = setTimeout(() => {
|
|
2530
|
+
this.timers.delete(timer);
|
|
2531
|
+
callback();
|
|
2532
|
+
}, ms);
|
|
2533
|
+
this.timers.add(timer);
|
|
2534
|
+
return timer;
|
|
2535
|
+
}
|
|
2536
|
+
clear(timer) {
|
|
2537
|
+
if (!timer)
|
|
2538
|
+
return;
|
|
2539
|
+
clearTimeout(timer);
|
|
2540
|
+
this.timers.delete(timer);
|
|
2541
|
+
}
|
|
2542
|
+
sleep(ms) {
|
|
2543
|
+
return new Promise((resolve) => {
|
|
2544
|
+
this.setTimeout(resolve, ms);
|
|
2545
|
+
});
|
|
2546
|
+
}
|
|
2547
|
+
dispose() {
|
|
2548
|
+
if (this.disposed)
|
|
2549
|
+
return;
|
|
2550
|
+
this.disposed = true;
|
|
2551
|
+
for (const timer of this.timers)
|
|
2552
|
+
clearTimeout(timer);
|
|
2553
|
+
this.timers.clear();
|
|
2554
|
+
}
|
|
2555
|
+
snapshot() {
|
|
2556
|
+
return { size: this.timers.size, disposed: this.disposed };
|
|
2557
|
+
}
|
|
2558
|
+
}
|
|
2559
|
+
|
|
2560
|
+
// src/state/voice-state-store.ts
|
|
2561
|
+
var LISTENER_NOTIFY_DELAY_MS = 50;
|
|
2562
|
+
|
|
2563
|
+
class VoiceStateStore {
|
|
2564
|
+
kv;
|
|
2565
|
+
timers;
|
|
2566
|
+
log = createLogger("state");
|
|
2567
|
+
listeners = new Set;
|
|
2568
|
+
listenerTimer;
|
|
2569
|
+
disposed = false;
|
|
2570
|
+
state;
|
|
2571
|
+
constructor(kv, config, timers) {
|
|
2572
|
+
this.kv = kv;
|
|
2573
|
+
this.timers = timers;
|
|
2574
|
+
this.state = {
|
|
2575
|
+
enabled: kv.get(KV_ENABLED, true),
|
|
2576
|
+
paused: false,
|
|
2577
|
+
busy: false,
|
|
2578
|
+
generating: false,
|
|
2579
|
+
playing: false,
|
|
2580
|
+
backend: config.audioPlayer,
|
|
2581
|
+
device: config.device,
|
|
2582
|
+
error: undefined
|
|
2583
|
+
};
|
|
2584
|
+
}
|
|
2585
|
+
subscribe(listener) {
|
|
2586
|
+
this.listeners.add(listener);
|
|
2587
|
+
return () => {
|
|
2588
|
+
this.listeners.delete(listener);
|
|
2589
|
+
};
|
|
2590
|
+
}
|
|
2591
|
+
snapshot() {
|
|
2592
|
+
return { ...this.state };
|
|
2593
|
+
}
|
|
2594
|
+
setEnabled(enabled) {
|
|
2595
|
+
this.kv.set(KV_ENABLED, enabled);
|
|
2596
|
+
this.patch({ enabled });
|
|
2597
|
+
}
|
|
2598
|
+
patch(patch) {
|
|
2599
|
+
let changed = false;
|
|
2600
|
+
const changedFields = {};
|
|
2601
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
2602
|
+
if (this.state[key] === value)
|
|
2603
|
+
continue;
|
|
2604
|
+
this.state[key] = value;
|
|
2605
|
+
changedFields[key] = value;
|
|
2606
|
+
changed = true;
|
|
2607
|
+
}
|
|
2608
|
+
if (!changed)
|
|
2609
|
+
return;
|
|
2610
|
+
this.log.debug("state changed", changedFields);
|
|
2611
|
+
this.scheduleListenerNotify();
|
|
2612
|
+
}
|
|
2613
|
+
notifyNow() {
|
|
2614
|
+
this.scheduleListenerNotify(true);
|
|
2615
|
+
}
|
|
2616
|
+
dispose() {
|
|
2617
|
+
if (this.disposed)
|
|
2618
|
+
return;
|
|
2619
|
+
this.disposed = true;
|
|
2620
|
+
this.timers.clear(this.listenerTimer);
|
|
2621
|
+
this.listenerTimer = undefined;
|
|
2622
|
+
this.listeners.clear();
|
|
2623
|
+
}
|
|
2624
|
+
scheduleListenerNotify(immediate = false) {
|
|
2625
|
+
if (this.disposed && !immediate)
|
|
2626
|
+
return;
|
|
2627
|
+
if (this.listenerTimer) {
|
|
2628
|
+
if (!immediate)
|
|
2629
|
+
return;
|
|
2630
|
+
this.timers.clear(this.listenerTimer);
|
|
2631
|
+
this.listenerTimer = undefined;
|
|
2632
|
+
}
|
|
2633
|
+
if (immediate) {
|
|
2634
|
+
for (const listener of this.listeners)
|
|
2635
|
+
listener();
|
|
2636
|
+
return;
|
|
2637
|
+
}
|
|
2638
|
+
this.listenerTimer = this.timers.setTimeout(() => {
|
|
2639
|
+
this.listenerTimer = undefined;
|
|
2640
|
+
for (const listener of this.listeners)
|
|
2641
|
+
listener();
|
|
2642
|
+
}, LISTENER_NOTIFY_DELAY_MS);
|
|
2643
|
+
}
|
|
2644
|
+
}
|
|
2645
|
+
|
|
2646
|
+
// src/state/voice-status.ts
|
|
2647
|
+
function voiceStatusLabel(state) {
|
|
2648
|
+
if (state.error)
|
|
2649
|
+
return "! sound error";
|
|
2650
|
+
if (!state.enabled)
|
|
2651
|
+
return "sound off";
|
|
2652
|
+
if (state.device === "loading")
|
|
2653
|
+
return "sound initializing";
|
|
2654
|
+
return "sound on";
|
|
2655
|
+
}
|
|
2656
|
+
|
|
2657
|
+
// src/streaming/stream-buffer.ts
|
|
2658
|
+
class StreamBuffer {
|
|
2659
|
+
sessionID;
|
|
2660
|
+
messageID;
|
|
2661
|
+
block;
|
|
2662
|
+
rawText = "";
|
|
2663
|
+
textLength = 0;
|
|
2664
|
+
buffer = "";
|
|
2665
|
+
sanitizer;
|
|
2666
|
+
completed = false;
|
|
2667
|
+
constructor(sessionID, messageID, block) {
|
|
2668
|
+
this.sessionID = sessionID;
|
|
2669
|
+
this.messageID = messageID;
|
|
2670
|
+
this.block = block;
|
|
2671
|
+
}
|
|
2672
|
+
applyDelta(delta, block, config, newStream) {
|
|
2673
|
+
this.block = block;
|
|
2674
|
+
this.sanitizer ??= createSpeechSanitizer(config);
|
|
2675
|
+
const startOffset = this.textLength;
|
|
2676
|
+
this.rawText += delta;
|
|
2677
|
+
this.textLength = this.rawText.length;
|
|
2678
|
+
this.buffer = appendSpeechBuffer(this.buffer, this.sanitizer.push(delta, false));
|
|
2679
|
+
const drained = drainStreamChunks(this.buffer, config, false);
|
|
2680
|
+
this.buffer = drained.rest;
|
|
2681
|
+
return {
|
|
2682
|
+
startOffset,
|
|
2683
|
+
endOffset: this.textLength,
|
|
2684
|
+
chunks: drained.chunks,
|
|
2685
|
+
rest: drained.rest,
|
|
2686
|
+
newStream
|
|
2687
|
+
};
|
|
2688
|
+
}
|
|
2689
|
+
storeSnapshot(block) {
|
|
2690
|
+
this.block = block;
|
|
2691
|
+
}
|
|
2692
|
+
applyFinal(text, block, config, newStream) {
|
|
2693
|
+
this.block = block;
|
|
2694
|
+
this.sanitizer ??= createSpeechSanitizer(config);
|
|
2695
|
+
let nextText = "";
|
|
2696
|
+
const startOffset = this.rawText.length;
|
|
2697
|
+
let cursorReset = false;
|
|
2698
|
+
if (text.length >= this.rawText.length && text.startsWith(this.rawText)) {
|
|
2699
|
+
nextText = text.slice(this.rawText.length);
|
|
2700
|
+
} else {
|
|
2701
|
+
cursorReset = true;
|
|
2702
|
+
}
|
|
2703
|
+
this.rawText = text;
|
|
2704
|
+
this.textLength = text.length;
|
|
2705
|
+
this.buffer = appendSpeechBuffer(this.buffer, this.sanitizer.push(nextText, true));
|
|
2706
|
+
const drained = drainStreamChunks(this.buffer, config, true);
|
|
2707
|
+
this.buffer = drained.rest;
|
|
2708
|
+
this.completed = true;
|
|
2709
|
+
this.buffer = "";
|
|
2710
|
+
return {
|
|
2711
|
+
startOffset,
|
|
2712
|
+
endOffset: this.textLength,
|
|
2713
|
+
nextText,
|
|
2714
|
+
chunks: drained.chunks,
|
|
2715
|
+
rest: drained.rest,
|
|
2716
|
+
finalFlush: true,
|
|
2717
|
+
cursorReset,
|
|
2718
|
+
newStream
|
|
2719
|
+
};
|
|
2720
|
+
}
|
|
2721
|
+
snapshot() {
|
|
2722
|
+
return {
|
|
2723
|
+
sessionID: this.sessionID,
|
|
2724
|
+
messageID: this.messageID,
|
|
2725
|
+
block: this.block,
|
|
2726
|
+
rawText: this.rawText,
|
|
2727
|
+
textLength: this.textLength,
|
|
2728
|
+
buffer: this.buffer,
|
|
2729
|
+
completed: this.completed
|
|
2730
|
+
};
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
|
|
2734
|
+
// src/streaming/streaming-controller.ts
|
|
2735
|
+
function streamKey(event, block) {
|
|
2736
|
+
return JSON.stringify([event.sessionID, event.assistantMessageID, block, event.ordinal]);
|
|
2737
|
+
}
|
|
2738
|
+
|
|
2739
|
+
class StreamingController {
|
|
2740
|
+
api;
|
|
2741
|
+
config;
|
|
2742
|
+
state;
|
|
2743
|
+
playback;
|
|
2744
|
+
sessionStore;
|
|
2745
|
+
log = createLogger("streaming");
|
|
2746
|
+
streams = new Map;
|
|
2747
|
+
tasks = new Map;
|
|
2748
|
+
cleanup = [];
|
|
2749
|
+
disposed = false;
|
|
2750
|
+
constructor(api, config, state, playback, sessionStore) {
|
|
2751
|
+
this.api = api;
|
|
2752
|
+
this.config = config;
|
|
2753
|
+
this.state = state;
|
|
2754
|
+
this.playback = playback;
|
|
2755
|
+
this.sessionStore = sessionStore;
|
|
2756
|
+
this.cleanup.push(api.data.on("session.text.delta", (event) => this.queue(event.data, "message", () => this.delta(event.data, "message"))), api.data.on("session.text.ended", (event) => this.queue(event.data, "message", () => this.end(event.data, "message"))), api.data.on("session.reasoning.delta", (event) => this.queue(event.data, "reason", () => this.delta(event.data, "reason"))), api.data.on("session.reasoning.ended", (event) => this.queue(event.data, "reason", () => this.end(event.data, "reason"))), api.data.on("session.execution.interrupted", (event) => this.clearSession(event.data.sessionID)), api.data.on("session.execution.failed", (event) => this.clearSession(event.data.sessionID)), sessionStore.onDeleted((sessionID) => {
|
|
2757
|
+
this.clearSession(sessionID);
|
|
2758
|
+
}));
|
|
2759
|
+
}
|
|
2760
|
+
clearSession(sessionID) {
|
|
2761
|
+
for (const [key, stream] of this.streams) {
|
|
2762
|
+
if (stream.sessionID === sessionID)
|
|
2763
|
+
this.streams.delete(key);
|
|
2764
|
+
}
|
|
2765
|
+
}
|
|
2766
|
+
dispose() {
|
|
2767
|
+
this.disposed = true;
|
|
2768
|
+
for (const off of this.cleanup)
|
|
2769
|
+
off();
|
|
2770
|
+
this.cleanup.length = 0;
|
|
2771
|
+
this.streams.clear();
|
|
2772
|
+
}
|
|
2773
|
+
queue(event, block, task) {
|
|
2774
|
+
const key = streamKey(event, block);
|
|
2775
|
+
const previous = this.tasks.get(key) ?? Promise.resolve();
|
|
2776
|
+
const next = previous.then(task).catch((error) => this.log.warn("stream event failed", { error: formatError(error) }));
|
|
2777
|
+
this.tasks.set(key, next);
|
|
2778
|
+
next.finally(() => {
|
|
2779
|
+
if (this.tasks.get(key) === next)
|
|
2780
|
+
this.tasks.delete(key);
|
|
2781
|
+
});
|
|
2782
|
+
}
|
|
2783
|
+
async canSpeak(event, block) {
|
|
2784
|
+
if (this.disposed || !this.config.speakResponses || !this.config.speechBlocks.includes(block))
|
|
2785
|
+
return false;
|
|
2786
|
+
if (!this.state.snapshot().enabled)
|
|
2787
|
+
return false;
|
|
2788
|
+
const route = this.api.ui.router.current();
|
|
2789
|
+
if (route.type !== "session")
|
|
2790
|
+
return false;
|
|
2791
|
+
const parentID = await this.sessionStore.sessionParentID(event.sessionID);
|
|
2792
|
+
const current = this.api.ui.router.current();
|
|
2793
|
+
const visible = current.type === "session" && current.sessionID === route.sessionID;
|
|
2794
|
+
const allowed = parentID ? this.config.speakSubagentResponses && parentID === route.sessionID : event.sessionID === route.sessionID;
|
|
2795
|
+
return !this.disposed && visible && allowed && this.state.snapshot().enabled;
|
|
2796
|
+
}
|
|
2797
|
+
async keepUp(event, block) {
|
|
2798
|
+
const key = streamKey(event, block);
|
|
2799
|
+
const previous = this.streams.get(key);
|
|
2800
|
+
if (previous?.completed)
|
|
2801
|
+
return;
|
|
2802
|
+
const stream = previous ?? new StreamBuffer(event.sessionID, event.assistantMessageID, block);
|
|
2803
|
+
this.streams.set(key, stream);
|
|
2804
|
+
while (this.streams.size > 200) {
|
|
2805
|
+
const removable = [...this.streams].find(([candidate, item]) => candidate !== key && (item.completed || item.sessionID !== event.sessionID));
|
|
2806
|
+
if (!removable)
|
|
2807
|
+
break;
|
|
2808
|
+
this.streams.delete(removable[0]);
|
|
2809
|
+
}
|
|
2810
|
+
return { stream, previous, speak: await this.canSpeak(event, block) };
|
|
2811
|
+
}
|
|
2812
|
+
async delta(event, block) {
|
|
2813
|
+
const current = await this.keepUp(event, block);
|
|
2814
|
+
if (!current || this.disposed)
|
|
2815
|
+
return;
|
|
2816
|
+
const { stream, previous, speak } = current;
|
|
2817
|
+
const result = stream.applyDelta(event.delta, block, this.config, !previous);
|
|
2818
|
+
if (!speak || !this.state.snapshot().enabled) {
|
|
2819
|
+
stream.buffer = "";
|
|
2820
|
+
return;
|
|
2821
|
+
}
|
|
2822
|
+
for (const chunk of result.chunks) {
|
|
2823
|
+
this.playback.enqueuePreparedChunk(chunk.text, chunk.pauseMs, "stream", {
|
|
2824
|
+
origin: "session.stream.delta",
|
|
2825
|
+
sessionID: event.sessionID,
|
|
2826
|
+
messageID: event.assistantMessageID,
|
|
2827
|
+
block
|
|
2828
|
+
});
|
|
2829
|
+
}
|
|
2830
|
+
}
|
|
2831
|
+
async end(event, block) {
|
|
2832
|
+
const current = await this.keepUp(event, block);
|
|
2833
|
+
if (!current || this.disposed)
|
|
2834
|
+
return;
|
|
2835
|
+
const { stream, previous, speak } = current;
|
|
2836
|
+
const result = stream.applyFinal(event.text, block, this.config, !previous);
|
|
2837
|
+
for (const chunk of speak && this.state.snapshot().enabled ? result.chunks : []) {
|
|
2838
|
+
this.playback.enqueuePreparedChunk(chunk.text, chunk.pauseMs, "stream", {
|
|
2839
|
+
origin: "session.stream.ended",
|
|
2840
|
+
sessionID: event.sessionID,
|
|
2841
|
+
messageID: event.assistantMessageID,
|
|
2842
|
+
block
|
|
2843
|
+
});
|
|
2844
|
+
}
|
|
2845
|
+
}
|
|
2846
|
+
}
|
|
2847
|
+
|
|
2848
|
+
// src/voice-config.ts
|
|
2849
|
+
function isRecord(value) {
|
|
2850
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
2851
|
+
}
|
|
2852
|
+
function readString(value, fallback) {
|
|
2853
|
+
if (typeof value !== "string")
|
|
2854
|
+
return fallback;
|
|
2855
|
+
const next = value.trim();
|
|
2856
|
+
return next || fallback;
|
|
2857
|
+
}
|
|
2858
|
+
function readOptionalString(value) {
|
|
2859
|
+
if (typeof value !== "string")
|
|
2860
|
+
return;
|
|
2861
|
+
const next = value.trim();
|
|
2862
|
+
return next || undefined;
|
|
2863
|
+
}
|
|
2864
|
+
function readBoolean(value, fallback) {
|
|
2865
|
+
if (typeof value === "boolean")
|
|
2866
|
+
return value;
|
|
2867
|
+
return fallback;
|
|
2868
|
+
}
|
|
2869
|
+
function readNumber(value, fallback, min) {
|
|
2870
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
2871
|
+
return fallback;
|
|
2872
|
+
if (min !== undefined && value < min)
|
|
2873
|
+
return fallback;
|
|
2874
|
+
return value;
|
|
2875
|
+
}
|
|
2876
|
+
function readInteger(value, fallback, min = 0) {
|
|
2877
|
+
const next = readNumber(value, fallback, min);
|
|
2878
|
+
return Math.floor(next);
|
|
2879
|
+
}
|
|
2880
|
+
function readStringList(value, fallback) {
|
|
2881
|
+
if (typeof value === "string") {
|
|
2882
|
+
const next = value.trim();
|
|
2883
|
+
return next ? [next] : fallback;
|
|
2884
|
+
}
|
|
2885
|
+
if (!Array.isArray(value))
|
|
2886
|
+
return fallback;
|
|
2887
|
+
const next = value.filter((item) => typeof item === "string").map((item) => item.trim()).filter(Boolean);
|
|
2888
|
+
return next.length ? next : fallback;
|
|
2889
|
+
}
|
|
2890
|
+
function readOptionalStringList(value) {
|
|
2891
|
+
if (typeof value === "string") {
|
|
2892
|
+
const next = value.trim();
|
|
2893
|
+
return next ? [next] : undefined;
|
|
2894
|
+
}
|
|
2895
|
+
if (!Array.isArray(value))
|
|
2896
|
+
return;
|
|
2897
|
+
const next = value.filter((item) => typeof item === "string").map((item) => item.trim()).filter(Boolean);
|
|
2898
|
+
return next.length ? next : undefined;
|
|
2899
|
+
}
|
|
2900
|
+
function readDevice(value) {
|
|
2901
|
+
switch (value) {
|
|
2902
|
+
case "auto":
|
|
2903
|
+
case "cpu":
|
|
2904
|
+
case "gpu":
|
|
2905
|
+
case "cuda":
|
|
2906
|
+
case "dml":
|
|
2907
|
+
case "wasm":
|
|
2908
|
+
case "webgpu":
|
|
2909
|
+
return value;
|
|
2910
|
+
default:
|
|
2911
|
+
return DEFAULT_CONFIG.device;
|
|
2912
|
+
}
|
|
2913
|
+
}
|
|
2914
|
+
function readDType(value) {
|
|
2915
|
+
switch (value) {
|
|
2916
|
+
case "fp32":
|
|
2917
|
+
case "fp16":
|
|
2918
|
+
case "q4":
|
|
2919
|
+
case "q4f16":
|
|
2920
|
+
case "q8":
|
|
2921
|
+
return value;
|
|
2922
|
+
default:
|
|
2923
|
+
return DEFAULT_CONFIG.dtype;
|
|
2924
|
+
}
|
|
2925
|
+
}
|
|
2926
|
+
function isVoiceBlock(value) {
|
|
2927
|
+
return value === "reason" || value === "message" || value === "idle";
|
|
2928
|
+
}
|
|
2929
|
+
function readVoiceBlocks(value) {
|
|
2930
|
+
if (Array.isArray(value) && value.length === 0)
|
|
2931
|
+
return [];
|
|
2932
|
+
const input = Array.isArray(value) ? value : typeof value === "string" ? [value] : DEFAULT_CONFIG.speechBlocks;
|
|
2933
|
+
const next = [];
|
|
2934
|
+
for (const item of input) {
|
|
2935
|
+
if (!isVoiceBlock(item) || next.includes(item))
|
|
2936
|
+
continue;
|
|
2937
|
+
next.push(item);
|
|
2938
|
+
}
|
|
2939
|
+
return next.length ? next : [...DEFAULT_CONFIG.speechBlocks];
|
|
2940
|
+
}
|
|
2941
|
+
function readShortcuts(value) {
|
|
2942
|
+
if (!isRecord(value))
|
|
2943
|
+
return { ...DEFAULT_CONFIG.shortcuts };
|
|
2944
|
+
return {
|
|
2945
|
+
history: readString(value.history, DEFAULT_CONFIG.shortcuts.history),
|
|
2946
|
+
pause: readString(value.pause, DEFAULT_CONFIG.shortcuts.pause),
|
|
2947
|
+
skipLatest: readString(value.skipLatest, DEFAULT_CONFIG.shortcuts.skipLatest),
|
|
2948
|
+
toggle: readString(value.toggle, DEFAULT_CONFIG.shortcuts.toggle)
|
|
2949
|
+
};
|
|
2950
|
+
}
|
|
2951
|
+
function resolveVoiceConfig(options) {
|
|
2952
|
+
const input = isRecord(options) ? options : {};
|
|
2953
|
+
const maxSpeechChunkChars = readInteger(input.maxSpeechChunkChars, DEFAULT_CONFIG.maxSpeechChunkChars, 32);
|
|
2954
|
+
const streamFlushChars = Math.min(maxSpeechChunkChars, readInteger(input.streamFlushChars, DEFAULT_CONFIG.streamFlushChars, 16));
|
|
2955
|
+
return {
|
|
2956
|
+
voice: readString(input.voice, DEFAULT_CONFIG.voice),
|
|
2957
|
+
speed: isValidVoiceSpeed(input.speed) ? normalizeVoiceSpeed(input.speed) : DEFAULT_CONFIG.speed,
|
|
2958
|
+
device: readDevice(input.device),
|
|
2959
|
+
dtype: readDType(input.dtype),
|
|
2960
|
+
model: readString(input.model, DEFAULT_CONFIG.model),
|
|
2961
|
+
cacheDir: readOptionalString(input.cacheDir),
|
|
2962
|
+
audioPlayer: readString(input.audioPlayer, DEFAULT_CONFIG.audioPlayer),
|
|
2963
|
+
audioPlayerArgs: readStringList(input.audioPlayerArgs, DEFAULT_CONFIG.audioPlayerArgs),
|
|
2964
|
+
speakResponses: readBoolean(input.speakResponses, DEFAULT_CONFIG.speakResponses),
|
|
2965
|
+
speakSubagentResponses: readBoolean(input.speakSubagentResponses, DEFAULT_CONFIG.speakSubagentResponses),
|
|
2966
|
+
speakOnIdle: readBoolean(input.speakOnIdle, DEFAULT_CONFIG.speakOnIdle),
|
|
2967
|
+
speakQuestions: readBoolean(input.speakQuestions, DEFAULT_CONFIG.speakQuestions),
|
|
2968
|
+
idleAnnouncement: readString(input.idleAnnouncement, DEFAULT_CONFIG.idleAnnouncement),
|
|
2969
|
+
speechBlocks: readVoiceBlocks(input.speechBlocks),
|
|
2970
|
+
maxSpeechChunkChars,
|
|
2971
|
+
streamFlushChars,
|
|
2972
|
+
maxSpeechChars: readInteger(input.maxSpeechChars, DEFAULT_CONFIG.maxSpeechChars, 64),
|
|
2973
|
+
fileExtensions: normalizeFileExtensions(input.fileExtensions),
|
|
2974
|
+
trimSilenceThreshold: readNumber(input.trimSilenceThreshold, DEFAULT_CONFIG.trimSilenceThreshold, 0),
|
|
2975
|
+
leadingAudioPadMs: readInteger(input.leadingAudioPadMs, DEFAULT_CONFIG.leadingAudioPadMs, 0),
|
|
2976
|
+
normalPauseMs: readInteger(input.normalPauseMs, DEFAULT_CONFIG.normalPauseMs, 0),
|
|
2977
|
+
sentencePauseMs: readInteger(input.sentencePauseMs, DEFAULT_CONFIG.sentencePauseMs, 0),
|
|
2978
|
+
ttsServiceCommand: readOptionalString(input.ttsServiceCommand),
|
|
2979
|
+
ttsServiceArgs: readOptionalStringList(input.ttsServiceArgs),
|
|
2980
|
+
shortcuts: readShortcuts(input.shortcuts)
|
|
2981
|
+
};
|
|
2982
|
+
}
|
|
2983
|
+
|
|
2984
|
+
// src/voice-rpc.ts
|
|
2985
|
+
import { Rpc } from "@opencode/plugin/rpc";
|
|
2986
|
+
var state = {
|
|
2987
|
+
type: "object",
|
|
2988
|
+
properties: { enabled: { type: "boolean" } },
|
|
2989
|
+
required: ["enabled"],
|
|
2990
|
+
additionalProperties: false
|
|
2991
|
+
};
|
|
2992
|
+
var VoiceRpc = Rpc.define({
|
|
2993
|
+
id: "semirhuduti.tts.voice",
|
|
2994
|
+
methods: {
|
|
2995
|
+
getEnabled: {
|
|
2996
|
+
input: { type: "object", additionalProperties: false },
|
|
2997
|
+
output: state
|
|
2998
|
+
},
|
|
2999
|
+
setEnabled: { input: state, output: state }
|
|
3000
|
+
},
|
|
3001
|
+
events: {
|
|
3002
|
+
stateChanged: { schema: state }
|
|
3003
|
+
}
|
|
3004
|
+
});
|
|
3005
|
+
|
|
3006
|
+
// src/tui-plugin.tsx
|
|
3007
|
+
var log = createLogger("plugin");
|
|
3008
|
+
function Status(props) {
|
|
3009
|
+
const [state, setState] = createSignal(props.state.snapshot());
|
|
3010
|
+
onCleanup(props.state.subscribe(() => setState(props.state.snapshot())));
|
|
3011
|
+
return (() => {
|
|
3012
|
+
var _el$ = _$createElement("text");
|
|
3013
|
+
_$insert(_el$, () => `[${voiceStatusLabel(state())} - ${formatVoiceSpeed(props.speed())}x - Ctrl+F9]`);
|
|
3014
|
+
_$effect((_$p) => _$setProp(_el$, "fg", props.api.theme.text.muted, _$p));
|
|
3015
|
+
return _el$;
|
|
3016
|
+
})();
|
|
3017
|
+
}
|
|
3018
|
+
var tui_plugin_default = Plugin.define({
|
|
3019
|
+
id: PLUGIN_ID,
|
|
3020
|
+
async setup(api) {
|
|
3021
|
+
const config = resolveVoiceConfig(api.options);
|
|
3022
|
+
const voice = api.client.rpc(VoiceRpc);
|
|
3023
|
+
const location = api.location ?? api.data.location.default();
|
|
3024
|
+
let enabled = true;
|
|
3025
|
+
let pendingUpdates = 0;
|
|
3026
|
+
let latestEvent;
|
|
3027
|
+
let onStateChanged;
|
|
3028
|
+
const removeStateListener = voice.events.on("stateChanged", (event) => {
|
|
3029
|
+
if (event.location.directory !== location.directory)
|
|
3030
|
+
return;
|
|
3031
|
+
const next = event.data.enabled;
|
|
3032
|
+
if (typeof next !== "boolean")
|
|
3033
|
+
return;
|
|
3034
|
+
latestEvent = next;
|
|
3035
|
+
if (pendingUpdates > 0)
|
|
3036
|
+
return;
|
|
3037
|
+
onStateChanged?.(next);
|
|
3038
|
+
});
|
|
3039
|
+
try {
|
|
3040
|
+
const result = await voice.getEnabled({}, {
|
|
3041
|
+
location
|
|
3042
|
+
});
|
|
3043
|
+
if (typeof result !== "object" || result === null || !("enabled" in result) || typeof result.enabled !== "boolean") {
|
|
3044
|
+
throw new Error("Invalid voice state response");
|
|
3045
|
+
}
|
|
3046
|
+
enabled = latestEvent ?? result.enabled;
|
|
3047
|
+
} catch (error) {
|
|
3048
|
+
removeStateListener();
|
|
3049
|
+
log.error("voice state unavailable", {
|
|
3050
|
+
error: String(error)
|
|
3051
|
+
});
|
|
3052
|
+
api.ui.toast.show({
|
|
3053
|
+
variant: "error",
|
|
3054
|
+
message: "Voice server plugin unavailable"
|
|
3055
|
+
});
|
|
3056
|
+
return;
|
|
3057
|
+
}
|
|
3058
|
+
const speedController = createVoiceSpeedController(api.options.speed, config.speed, api.storage, (error) => {
|
|
3059
|
+
log.warn("voice speed update failed", {
|
|
3060
|
+
error: String(error)
|
|
3061
|
+
});
|
|
3062
|
+
api.ui.toast.show({
|
|
3063
|
+
variant: "error",
|
|
3064
|
+
message: "Could not save speech speed"
|
|
3065
|
+
});
|
|
3066
|
+
});
|
|
3067
|
+
const selectionController = createVoiceSelectionController(api.options.voice, config.voice, api.storage, (error) => {
|
|
3068
|
+
log.warn("voice selection update failed", {
|
|
3069
|
+
error: String(error)
|
|
3070
|
+
});
|
|
3071
|
+
api.ui.toast.show({
|
|
3072
|
+
variant: "error",
|
|
3073
|
+
message: "Could not save voice selection"
|
|
3074
|
+
});
|
|
3075
|
+
});
|
|
3076
|
+
config.speed = speedController.speed;
|
|
3077
|
+
config.voice = selectionController.voice;
|
|
3078
|
+
const timers = new TimerRegistry;
|
|
3079
|
+
let pendingState = Promise.resolve();
|
|
3080
|
+
let disposed = false;
|
|
3081
|
+
let stateStore;
|
|
3082
|
+
let playback;
|
|
3083
|
+
stateStore = new VoiceStateStore({
|
|
3084
|
+
get: (_key, fallback) => typeof fallback === "boolean" ? enabled : fallback,
|
|
3085
|
+
set: (_key, value) => {
|
|
3086
|
+
if (typeof value !== "boolean")
|
|
3087
|
+
return;
|
|
3088
|
+
enabled = value;
|
|
3089
|
+
latestEvent = undefined;
|
|
3090
|
+
pendingUpdates++;
|
|
3091
|
+
pendingState = pendingState.then(() => voice.setEnabled({
|
|
3092
|
+
enabled: value
|
|
3093
|
+
}, {
|
|
3094
|
+
location
|
|
3095
|
+
})).then(() => {
|
|
3096
|
+
return;
|
|
3097
|
+
}).catch(async (error) => {
|
|
3098
|
+
log.warn("voice state update failed", {
|
|
3099
|
+
error: String(error)
|
|
3100
|
+
});
|
|
3101
|
+
api.ui.toast.show({
|
|
3102
|
+
variant: "error",
|
|
3103
|
+
message: "Could not save voice state"
|
|
3104
|
+
});
|
|
3105
|
+
try {
|
|
3106
|
+
if (disposed || pendingUpdates > 1)
|
|
3107
|
+
return;
|
|
3108
|
+
const saved = await voice.getEnabled({}, {
|
|
3109
|
+
location
|
|
3110
|
+
});
|
|
3111
|
+
if (disposed || pendingUpdates > 1)
|
|
3112
|
+
return;
|
|
3113
|
+
latestEvent = undefined;
|
|
3114
|
+
enabled = saved.enabled;
|
|
3115
|
+
if (!saved.enabled)
|
|
3116
|
+
await playback.reset(false);
|
|
3117
|
+
stateStore.patch({
|
|
3118
|
+
enabled: saved.enabled
|
|
3119
|
+
});
|
|
3120
|
+
} catch (restoreError) {
|
|
3121
|
+
log.warn("voice state recovery failed", {
|
|
3122
|
+
error: String(restoreError)
|
|
3123
|
+
});
|
|
3124
|
+
}
|
|
3125
|
+
}).finally(() => {
|
|
3126
|
+
pendingUpdates--;
|
|
3127
|
+
if (!disposed && pendingUpdates === 0 && latestEvent !== undefined)
|
|
3128
|
+
onStateChanged?.(latestEvent);
|
|
3129
|
+
});
|
|
3130
|
+
}
|
|
3131
|
+
}, config, timers);
|
|
3132
|
+
playback = new PlaybackPipeline(config, stateStore, timers, (toast) => api.ui.toast.show(toast));
|
|
3133
|
+
onStateChanged = (next) => {
|
|
3134
|
+
enabled = next;
|
|
3135
|
+
if (!next && stateStore.snapshot().enabled)
|
|
3136
|
+
playback.reset(false);
|
|
3137
|
+
stateStore.patch({
|
|
3138
|
+
enabled: next
|
|
3139
|
+
});
|
|
3140
|
+
};
|
|
3141
|
+
if (latestEvent !== undefined && latestEvent !== enabled)
|
|
3142
|
+
onStateChanged(latestEvent);
|
|
3143
|
+
const sessionStore = new SessionStore(api, config.speakSubagentResponses);
|
|
3144
|
+
const latestStore = new LatestMessageStore(api, config, sessionStore);
|
|
3145
|
+
const streaming = new StreamingController(api, config, stateStore, playback, sessionStore);
|
|
3146
|
+
const idle = new IdleController(api, config, stateStore, playback, sessionStore);
|
|
3147
|
+
const questions = new QuestionController(api, config, stateStore, playback, sessionStore);
|
|
3148
|
+
const removeIdleRefresh = api.data.on("session.idle", (event) => {
|
|
3149
|
+
const current = api.ui.router.current();
|
|
3150
|
+
if (current.type === "session" && current.sessionID === event.data.sessionID)
|
|
3151
|
+
latestStore.refresh(event.data.sessionID);
|
|
3152
|
+
});
|
|
3153
|
+
const route = {
|
|
3154
|
+
get current() {
|
|
3155
|
+
const current = api.ui.router.current();
|
|
3156
|
+
return current.type === "session" ? {
|
|
3157
|
+
name: "session",
|
|
3158
|
+
params: {
|
|
3159
|
+
sessionID: current.sessionID
|
|
3160
|
+
}
|
|
3161
|
+
} : {
|
|
3162
|
+
name: current.type
|
|
3163
|
+
};
|
|
3164
|
+
}
|
|
3165
|
+
};
|
|
3166
|
+
const commands = new VoiceCommands(route, config, stateStore, playback, sessionStore, latestStore, (toast) => api.ui.toast.show(toast));
|
|
3167
|
+
playback.start();
|
|
3168
|
+
const [speed, setSpeed] = createSignal(speedController.percent);
|
|
3169
|
+
const adjustSpeed = (direction) => {
|
|
3170
|
+
const next = speedController.adjust(direction);
|
|
3171
|
+
config.speed = next;
|
|
3172
|
+
setSpeed(speedController.percent);
|
|
3173
|
+
api.ui.toast.show({
|
|
3174
|
+
variant: "info",
|
|
3175
|
+
message: `Speech speed: ${formatVoiceSpeed(speedController.percent)}x`
|
|
3176
|
+
});
|
|
3177
|
+
};
|
|
3178
|
+
async function openHistory() {
|
|
3179
|
+
const sessionID = route.current.params?.sessionID;
|
|
3180
|
+
if (typeof sessionID !== "string")
|
|
3181
|
+
return;
|
|
3182
|
+
const entries = await commands.historyEntries(sessionID);
|
|
3183
|
+
if (disposed)
|
|
3184
|
+
return;
|
|
3185
|
+
if (route.current.params?.sessionID !== sessionID)
|
|
3186
|
+
return;
|
|
3187
|
+
if (!entries.length) {
|
|
3188
|
+
api.ui.toast.show({
|
|
3189
|
+
variant: "warning",
|
|
3190
|
+
message: "No assistant message available"
|
|
3191
|
+
});
|
|
3192
|
+
return;
|
|
3193
|
+
}
|
|
3194
|
+
const selected = await api.ui.dialog.select({
|
|
3195
|
+
title: "Assistant history",
|
|
3196
|
+
options: entries.map((entry) => ({
|
|
3197
|
+
title: entry.preview,
|
|
3198
|
+
value: entry.messageID
|
|
3199
|
+
})),
|
|
3200
|
+
actions: [{
|
|
3201
|
+
bind: "shift+return",
|
|
3202
|
+
title: "Play from selected",
|
|
3203
|
+
onTrigger: (messageID) => {
|
|
3204
|
+
api.ui.dialog.clear();
|
|
3205
|
+
if (route.current.params?.sessionID === sessionID) {
|
|
3206
|
+
commands.playHistory(messageID, "continue", sessionID);
|
|
3207
|
+
}
|
|
3208
|
+
}
|
|
3209
|
+
}]
|
|
3210
|
+
});
|
|
3211
|
+
if (disposed)
|
|
3212
|
+
return;
|
|
3213
|
+
if (selected && route.current.params?.sessionID === sessionID)
|
|
3214
|
+
await commands.playHistory(selected, "single", sessionID);
|
|
3215
|
+
}
|
|
3216
|
+
const voiceActions = {
|
|
3217
|
+
history: openHistory,
|
|
3218
|
+
pause: async () => {
|
|
3219
|
+
await commands.togglePlayback();
|
|
3220
|
+
},
|
|
3221
|
+
latest: async () => {
|
|
3222
|
+
await commands.replayLatest();
|
|
3223
|
+
},
|
|
3224
|
+
toggle: () => commands.toggleEnabled(),
|
|
3225
|
+
slower: async () => {
|
|
3226
|
+
adjustSpeed(-1);
|
|
3227
|
+
},
|
|
3228
|
+
faster: async () => {
|
|
3229
|
+
adjustSpeed(1);
|
|
3230
|
+
},
|
|
3231
|
+
choose: async () => {
|
|
3232
|
+
await openVoicePicker(api, config.voice, (voiceID) => playback.previewVoice(voiceID), (voiceID) => {
|
|
3233
|
+
if (!selectionController.select(voiceID))
|
|
3234
|
+
return;
|
|
3235
|
+
config.voice = voiceID;
|
|
3236
|
+
api.ui.toast.show({
|
|
3237
|
+
variant: "info",
|
|
3238
|
+
message: `Voice: ${voiceID}`
|
|
3239
|
+
});
|
|
3240
|
+
}, () => disposed);
|
|
3241
|
+
}
|
|
3242
|
+
};
|
|
3243
|
+
api.keymap.layer(() => ({
|
|
3244
|
+
mode: "global",
|
|
3245
|
+
commands: [{
|
|
3246
|
+
id: "tts.history",
|
|
3247
|
+
title: "Replay previous assistant message",
|
|
3248
|
+
group: "Voice",
|
|
3249
|
+
bind: config.shortcuts.history,
|
|
3250
|
+
palette: true,
|
|
3251
|
+
run: voiceActions.history
|
|
3252
|
+
}, {
|
|
3253
|
+
id: "tts.play-pause",
|
|
3254
|
+
title: "Play or pause speech",
|
|
3255
|
+
group: "Voice",
|
|
3256
|
+
bind: config.shortcuts.pause,
|
|
3257
|
+
palette: true,
|
|
3258
|
+
run: voiceActions.pause
|
|
3259
|
+
}, {
|
|
3260
|
+
id: "tts.latest",
|
|
3261
|
+
title: "Replay latest assistant message",
|
|
3262
|
+
group: "Voice",
|
|
3263
|
+
bind: config.shortcuts.skipLatest,
|
|
3264
|
+
palette: true,
|
|
3265
|
+
run: voiceActions.latest
|
|
3266
|
+
}, {
|
|
3267
|
+
id: "tts.toggle",
|
|
3268
|
+
title: "Toggle speech",
|
|
3269
|
+
group: "Voice",
|
|
3270
|
+
bind: config.shortcuts.toggle,
|
|
3271
|
+
palette: true,
|
|
3272
|
+
run: voiceActions.toggle
|
|
3273
|
+
}, {
|
|
3274
|
+
id: "tts.slower",
|
|
3275
|
+
title: "Slow down speech",
|
|
3276
|
+
group: "Voice",
|
|
3277
|
+
bind: "ctrl+f6",
|
|
3278
|
+
palette: true,
|
|
3279
|
+
run: voiceActions.slower
|
|
3280
|
+
}, {
|
|
3281
|
+
id: "tts.faster",
|
|
3282
|
+
title: "Speed up speech",
|
|
3283
|
+
group: "Voice",
|
|
3284
|
+
bind: "ctrl+f7",
|
|
3285
|
+
palette: true,
|
|
3286
|
+
run: voiceActions.faster
|
|
3287
|
+
}, {
|
|
3288
|
+
id: "tts.choose-voice",
|
|
3289
|
+
title: "Choose voice",
|
|
3290
|
+
group: "Voice",
|
|
3291
|
+
palette: true,
|
|
3292
|
+
run: voiceActions.choose
|
|
3293
|
+
}, {
|
|
3294
|
+
id: "tts.shortcuts",
|
|
3295
|
+
title: "Voice shortcuts",
|
|
3296
|
+
group: "Voice",
|
|
3297
|
+
bind: "ctrl+f9",
|
|
3298
|
+
palette: true,
|
|
3299
|
+
run: () => openVoiceMenu(api, voiceActions, () => disposed)
|
|
3300
|
+
}],
|
|
3301
|
+
bindings: ["tts.history", "tts.play-pause", "tts.latest", "tts.toggle", "tts.slower", "tts.faster", "tts.choose-voice", "tts.shortcuts"]
|
|
3302
|
+
}));
|
|
3303
|
+
const removeHome = api.ui.slot({
|
|
3304
|
+
append: "home.footer.status",
|
|
3305
|
+
render: () => _$createComponent(Status, {
|
|
3306
|
+
state: stateStore,
|
|
3307
|
+
api,
|
|
3308
|
+
speed
|
|
3309
|
+
})
|
|
3310
|
+
});
|
|
3311
|
+
const removeSession = api.ui.slot({
|
|
3312
|
+
append: "prompt.footer.status",
|
|
3313
|
+
render: () => _$createComponent(Status, {
|
|
3314
|
+
state: stateStore,
|
|
3315
|
+
api,
|
|
3316
|
+
speed
|
|
3317
|
+
})
|
|
3318
|
+
});
|
|
3319
|
+
return async () => {
|
|
3320
|
+
disposed = true;
|
|
3321
|
+
removeHome();
|
|
3322
|
+
removeSession();
|
|
3323
|
+
removeStateListener();
|
|
3324
|
+
removeIdleRefresh();
|
|
3325
|
+
streaming.dispose();
|
|
3326
|
+
idle.dispose();
|
|
3327
|
+
questions.dispose();
|
|
3328
|
+
latestStore.dispose();
|
|
3329
|
+
sessionStore.dispose();
|
|
3330
|
+
try {
|
|
3331
|
+
await Promise.all([speedController.flush(), selectionController.flush()]);
|
|
3332
|
+
await playback.dispose();
|
|
3333
|
+
} finally {
|
|
3334
|
+
stateStore.dispose();
|
|
3335
|
+
timers.dispose();
|
|
3336
|
+
}
|
|
3337
|
+
};
|
|
3338
|
+
}
|
|
3339
|
+
});
|
|
3340
|
+
export {
|
|
3341
|
+
tui_plugin_default as default
|
|
3342
|
+
};
|