@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,7 @@
|
|
|
1
|
+
import type { VoiceConfig } from "./voice-types.js";
|
|
2
|
+
export declare const PLUGIN_ID = "semirhuduti.tts.voice";
|
|
3
|
+
export declare const KV_ENABLED = "semirhuduti.tts.voice.enabled";
|
|
4
|
+
export declare const DEFAULT_MODEL = "onnx-community/Kokoro-82M-v1.0-ONNX";
|
|
5
|
+
export declare const DEFAULT_CONFIG: VoiceConfig;
|
|
6
|
+
export declare const PLAYER_DEFAULT_ARGS: Record<string, string[]>;
|
|
7
|
+
export declare const PLAYER_CANDIDATES: readonly ["ffplay", "mpv", "paplay", "aplay"];
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { HelperRequest } from "./voice-helper-protocol.js";
|
|
2
|
+
import { KokoroRuntime } from "./voice-kokoro.js";
|
|
3
|
+
import type { VoiceConfig } from "./voice-types.js";
|
|
4
|
+
type ActiveRequest = {
|
|
5
|
+
id: number;
|
|
6
|
+
epoch: number;
|
|
7
|
+
cancelled: boolean;
|
|
8
|
+
};
|
|
9
|
+
export type ServiceState = {
|
|
10
|
+
runtime?: KokoroRuntime;
|
|
11
|
+
runtimeKey: string;
|
|
12
|
+
currentConfig?: VoiceConfig;
|
|
13
|
+
active?: ActiveRequest;
|
|
14
|
+
cancelledRequests: Set<number>;
|
|
15
|
+
cancelledEpoch: number;
|
|
16
|
+
tempDir?: Promise<string>;
|
|
17
|
+
sequence: number;
|
|
18
|
+
disposed: boolean;
|
|
19
|
+
work: Promise<void>;
|
|
20
|
+
cleanup?: Promise<void>;
|
|
21
|
+
};
|
|
22
|
+
export declare function createServiceState(): ServiceState;
|
|
23
|
+
export declare function runtimeIdentity(config: VoiceConfig): string;
|
|
24
|
+
export declare function cleanupServiceState(state: ServiceState): Promise<void>;
|
|
25
|
+
export declare function handleServiceMessage(state: ServiceState, message: HelperRequest): void;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,615 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/voice-helper-process.ts
|
|
3
|
+
import * as fs2 from "fs/promises";
|
|
4
|
+
import * as os2 from "os";
|
|
5
|
+
import * as path2 from "path";
|
|
6
|
+
import { stdin, stdout } from "process";
|
|
7
|
+
import { createInterface } from "readline";
|
|
8
|
+
import { pathToFileURL } from "url";
|
|
9
|
+
|
|
10
|
+
// src/voice-audio.ts
|
|
11
|
+
function trimSilence(input, sampleRate, threshold, leadingPadMs) {
|
|
12
|
+
if (!input.length || threshold <= 0)
|
|
13
|
+
return input;
|
|
14
|
+
let start = 0;
|
|
15
|
+
while (start < input.length && Math.abs(input[start] ?? 0) <= threshold)
|
|
16
|
+
start += 1;
|
|
17
|
+
if (start >= input.length)
|
|
18
|
+
return input;
|
|
19
|
+
let end = input.length - 1;
|
|
20
|
+
while (end > start && Math.abs(input[end] ?? 0) <= threshold)
|
|
21
|
+
end -= 1;
|
|
22
|
+
const pad = Math.max(0, Math.round(sampleRate * leadingPadMs / 1000));
|
|
23
|
+
const clippedStart = Math.max(0, start - pad);
|
|
24
|
+
return input.slice(clippedStart, end + 1);
|
|
25
|
+
}
|
|
26
|
+
function wavFromFloat32(audio, sampleRate) {
|
|
27
|
+
const pcm = new Int16Array(audio.length);
|
|
28
|
+
for (let index = 0;index < audio.length; index += 1) {
|
|
29
|
+
const sample = Math.max(-1, Math.min(1, audio[index] ?? 0));
|
|
30
|
+
pcm[index] = sample < 0 ? Math.round(sample * 32768) : Math.round(sample * 32767);
|
|
31
|
+
}
|
|
32
|
+
const dataSize = pcm.length * 2;
|
|
33
|
+
const buffer = Buffer.allocUnsafe(44 + dataSize);
|
|
34
|
+
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
35
|
+
buffer.write("RIFF", 0, "ascii");
|
|
36
|
+
view.setUint32(4, 36 + dataSize, true);
|
|
37
|
+
buffer.write("WAVE", 8, "ascii");
|
|
38
|
+
buffer.write("fmt ", 12, "ascii");
|
|
39
|
+
view.setUint32(16, 16, true);
|
|
40
|
+
view.setUint16(20, 1, true);
|
|
41
|
+
view.setUint16(22, 1, true);
|
|
42
|
+
view.setUint32(24, sampleRate, true);
|
|
43
|
+
view.setUint32(28, sampleRate * 2, true);
|
|
44
|
+
view.setUint16(32, 2, true);
|
|
45
|
+
view.setUint16(34, 16, true);
|
|
46
|
+
buffer.write("data", 36, "ascii");
|
|
47
|
+
view.setUint32(40, dataSize, true);
|
|
48
|
+
for (let index = 0;index < pcm.length; index += 1) {
|
|
49
|
+
view.setInt16(44 + index * 2, pcm[index] ?? 0, true);
|
|
50
|
+
}
|
|
51
|
+
return buffer;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/voice-kokoro.ts
|
|
55
|
+
function unique(values) {
|
|
56
|
+
return Array.from(new Set(values));
|
|
57
|
+
}
|
|
58
|
+
function candidateDevices(device) {
|
|
59
|
+
switch (device) {
|
|
60
|
+
case "cpu":
|
|
61
|
+
return ["cpu", "wasm"];
|
|
62
|
+
case "wasm":
|
|
63
|
+
return ["wasm", "cpu"];
|
|
64
|
+
case "webgpu":
|
|
65
|
+
return ["webgpu", "gpu", "cuda", "dml", "cpu", "wasm"];
|
|
66
|
+
case "gpu":
|
|
67
|
+
return ["gpu", "cuda", "dml", "webgpu", "cpu", "wasm"];
|
|
68
|
+
case "cuda":
|
|
69
|
+
return ["cuda", "gpu", "cpu", "wasm"];
|
|
70
|
+
case "dml":
|
|
71
|
+
return ["dml", "gpu", "cpu", "wasm"];
|
|
72
|
+
case "auto":
|
|
73
|
+
default:
|
|
74
|
+
return ["gpu", "cuda", "dml", "webgpu", "cpu", "wasm"];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function formatError(error) {
|
|
78
|
+
if (error instanceof Error && error.message)
|
|
79
|
+
return error.message;
|
|
80
|
+
return String(error);
|
|
81
|
+
}
|
|
82
|
+
async function prepareOnnxRuntime() {
|
|
83
|
+
const ort = await import("onnxruntime-node");
|
|
84
|
+
ort.env.logLevel = "error";
|
|
85
|
+
}
|
|
86
|
+
async function loadKokoroModule() {
|
|
87
|
+
return import("kokoro-js");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
class KokoroRuntime {
|
|
91
|
+
config;
|
|
92
|
+
onStatus;
|
|
93
|
+
logger;
|
|
94
|
+
loaded;
|
|
95
|
+
loading;
|
|
96
|
+
constructor(config, onStatus, logger) {
|
|
97
|
+
this.config = config;
|
|
98
|
+
this.onStatus = onStatus;
|
|
99
|
+
this.logger = logger;
|
|
100
|
+
}
|
|
101
|
+
async generate(text) {
|
|
102
|
+
this.logger.info("generate start", {
|
|
103
|
+
textLength: text.length,
|
|
104
|
+
voice: this.config.voice,
|
|
105
|
+
speed: this.config.speed
|
|
106
|
+
});
|
|
107
|
+
const runtime = await this.load();
|
|
108
|
+
const audio = await runtime.tts.generate(text, {
|
|
109
|
+
voice: this.config.voice,
|
|
110
|
+
speed: this.config.speed
|
|
111
|
+
});
|
|
112
|
+
this.logger.info("generate complete", {
|
|
113
|
+
textLength: text.length,
|
|
114
|
+
sampleRate: audio.sampling_rate,
|
|
115
|
+
samples: audio.data.length,
|
|
116
|
+
device: runtime.device
|
|
117
|
+
});
|
|
118
|
+
return {
|
|
119
|
+
audio: audio.data,
|
|
120
|
+
sampleRate: audio.sampling_rate
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
async* stream(text, speed = this.config.speed, voice = this.config.voice) {
|
|
124
|
+
this.logger.info("stream start", {
|
|
125
|
+
textLength: text.length,
|
|
126
|
+
voice,
|
|
127
|
+
speed
|
|
128
|
+
});
|
|
129
|
+
const runtime = await this.load();
|
|
130
|
+
let segmentCount = 0;
|
|
131
|
+
let totalSamples = 0;
|
|
132
|
+
for await (const audio of runtime.tts.stream(text, {
|
|
133
|
+
voice,
|
|
134
|
+
speed
|
|
135
|
+
})) {
|
|
136
|
+
segmentCount += 1;
|
|
137
|
+
totalSamples += audio.data.length;
|
|
138
|
+
this.logger.info("stream segment", {
|
|
139
|
+
textLength: audio.text.length,
|
|
140
|
+
sampleRate: audio.sampling_rate,
|
|
141
|
+
samples: audio.data.length,
|
|
142
|
+
segmentCount,
|
|
143
|
+
device: runtime.device
|
|
144
|
+
});
|
|
145
|
+
yield {
|
|
146
|
+
text: audio.text,
|
|
147
|
+
audio: audio.data,
|
|
148
|
+
sampleRate: audio.sampling_rate
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
this.logger.info("stream complete", {
|
|
152
|
+
textLength: text.length,
|
|
153
|
+
segmentCount,
|
|
154
|
+
totalSamples,
|
|
155
|
+
device: runtime.device
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
async dispose() {
|
|
159
|
+
const runtime = this.loaded ?? await this.loading?.catch(() => {
|
|
160
|
+
return;
|
|
161
|
+
});
|
|
162
|
+
this.loaded = undefined;
|
|
163
|
+
this.loading = undefined;
|
|
164
|
+
await runtime?.dispose();
|
|
165
|
+
}
|
|
166
|
+
async load() {
|
|
167
|
+
if (this.loaded)
|
|
168
|
+
return this.loaded;
|
|
169
|
+
if (this.loading)
|
|
170
|
+
return this.loading;
|
|
171
|
+
this.loading = this.create().then((runtime) => {
|
|
172
|
+
this.loaded = runtime;
|
|
173
|
+
this.logger.info("runtime ready", { device: runtime.device });
|
|
174
|
+
return runtime;
|
|
175
|
+
}).catch((error) => {
|
|
176
|
+
this.loading = undefined;
|
|
177
|
+
this.logger.error("runtime load failed", { error: formatError(error) });
|
|
178
|
+
this.onStatus({ device: this.config.device, error: formatError(error) });
|
|
179
|
+
throw error;
|
|
180
|
+
});
|
|
181
|
+
return this.loading;
|
|
182
|
+
}
|
|
183
|
+
async create() {
|
|
184
|
+
this.onStatus({ device: "loading", error: undefined });
|
|
185
|
+
this.logger.info("runtime import start", {
|
|
186
|
+
model: this.config.model,
|
|
187
|
+
dtype: this.config.dtype,
|
|
188
|
+
preferredDevice: this.config.device
|
|
189
|
+
});
|
|
190
|
+
await prepareOnnxRuntime();
|
|
191
|
+
if (this.config.cacheDir) {
|
|
192
|
+
const { env } = await import("@huggingface/transformers");
|
|
193
|
+
env.cacheDir = this.config.cacheDir;
|
|
194
|
+
}
|
|
195
|
+
const { KokoroTTS, TextSplitterStream } = await loadKokoroModule();
|
|
196
|
+
const loadKokoro = (device) => KokoroTTS.from_pretrained(this.config.model, {
|
|
197
|
+
dtype: this.config.dtype,
|
|
198
|
+
device
|
|
199
|
+
});
|
|
200
|
+
const errors = [];
|
|
201
|
+
for (const device of unique(candidateDevices(this.config.device))) {
|
|
202
|
+
this.logger.info("runtime init attempt", { device });
|
|
203
|
+
try {
|
|
204
|
+
const tts = await loadKokoro(device);
|
|
205
|
+
this.logger.info("runtime init success", { device });
|
|
206
|
+
this.onStatus({ device, error: undefined });
|
|
207
|
+
return {
|
|
208
|
+
dispose: async () => {
|
|
209
|
+
await tts.model.dispose();
|
|
210
|
+
},
|
|
211
|
+
tts: {
|
|
212
|
+
generate: async (text, input) => {
|
|
213
|
+
const audio = await tts.generate(text, input);
|
|
214
|
+
return {
|
|
215
|
+
data: audio.audio,
|
|
216
|
+
sampling_rate: audio.sampling_rate
|
|
217
|
+
};
|
|
218
|
+
},
|
|
219
|
+
stream: async function* (text, input) {
|
|
220
|
+
const splitter = new TextSplitterStream;
|
|
221
|
+
splitter.push(text);
|
|
222
|
+
splitter.close();
|
|
223
|
+
for await (const segment of tts.stream(splitter, input)) {
|
|
224
|
+
yield {
|
|
225
|
+
text: segment.text,
|
|
226
|
+
data: segment.audio.audio,
|
|
227
|
+
sampling_rate: segment.audio.sampling_rate
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
device
|
|
233
|
+
};
|
|
234
|
+
} catch (error) {
|
|
235
|
+
errors.push(`${device}: ${formatError(error)}`);
|
|
236
|
+
this.logger.warn("runtime init failed", {
|
|
237
|
+
device,
|
|
238
|
+
error: formatError(error)
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
throw new Error(`Failed to initialize Kokoro. ${errors.join("; ")}`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// src/voice-log.ts
|
|
247
|
+
import * as fs from "fs";
|
|
248
|
+
import * as os from "os";
|
|
249
|
+
import * as path from "path";
|
|
250
|
+
var LOG_PREFIX = "[opencode-tts-voice]";
|
|
251
|
+
var LOG_FILE_PREFIX = "opencode-tts-voice";
|
|
252
|
+
var LEVELS = {
|
|
253
|
+
debug: 10,
|
|
254
|
+
info: 20,
|
|
255
|
+
warn: 30,
|
|
256
|
+
error: 40,
|
|
257
|
+
silent: 50
|
|
258
|
+
};
|
|
259
|
+
var logFile;
|
|
260
|
+
var fileLogDisabled = false;
|
|
261
|
+
function readLevel() {
|
|
262
|
+
const value = process.env.OPENCODE_TTS_VOICE_LOG_LEVEL ?? process.env.OPENCODE_TTS_LOG_LEVEL;
|
|
263
|
+
switch (value?.toLowerCase()) {
|
|
264
|
+
case "debug":
|
|
265
|
+
case "info":
|
|
266
|
+
case "warn":
|
|
267
|
+
case "error":
|
|
268
|
+
case "silent":
|
|
269
|
+
return value.toLowerCase();
|
|
270
|
+
default:
|
|
271
|
+
return "warn";
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
function readConsoleEnabled() {
|
|
275
|
+
const value = process.env.OPENCODE_TTS_VOICE_CONSOLE_LOG ?? process.env.OPENCODE_TTS_CONSOLE_LOG;
|
|
276
|
+
return value === "1" || value?.toLowerCase() === "true";
|
|
277
|
+
}
|
|
278
|
+
function normalize(fields) {
|
|
279
|
+
if (!fields)
|
|
280
|
+
return;
|
|
281
|
+
return Object.fromEntries(Object.entries(fields).filter(([, value]) => value !== undefined));
|
|
282
|
+
}
|
|
283
|
+
function timestampForFile(date) {
|
|
284
|
+
return date.toISOString().replace(/:/g, "").replace(/\.\d{3}Z$/, "");
|
|
285
|
+
}
|
|
286
|
+
function resolveLogFile() {
|
|
287
|
+
if (fileLogDisabled)
|
|
288
|
+
return;
|
|
289
|
+
if (logFile)
|
|
290
|
+
return logFile;
|
|
291
|
+
const home = os.homedir();
|
|
292
|
+
if (!process.env.XDG_DATA_HOME && !home) {
|
|
293
|
+
fileLogDisabled = true;
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
const dataDir = process.env.XDG_DATA_HOME ?? path.join(home, ".local", "share");
|
|
297
|
+
const dir = path.join(dataDir, "opencode", "log");
|
|
298
|
+
const file = path.join(dir, `${LOG_FILE_PREFIX}-${timestampForFile(new Date)}.log`);
|
|
299
|
+
try {
|
|
300
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
301
|
+
logFile = file;
|
|
302
|
+
return file;
|
|
303
|
+
} catch {
|
|
304
|
+
fileLogDisabled = true;
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function formatFields(fields) {
|
|
309
|
+
if (!fields || Object.keys(fields).length === 0)
|
|
310
|
+
return "";
|
|
311
|
+
try {
|
|
312
|
+
return ` ${JSON.stringify(fields)}`;
|
|
313
|
+
} catch {
|
|
314
|
+
return ` ${String(fields)}`;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
function writeFile(method, scope, message, fields) {
|
|
318
|
+
const file = resolveLogFile();
|
|
319
|
+
if (!file)
|
|
320
|
+
return;
|
|
321
|
+
try {
|
|
322
|
+
fs.appendFileSync(file, `${new Date().toISOString()} ${method.toUpperCase()} ${scope} ${message}${formatFields(fields)}
|
|
323
|
+
`);
|
|
324
|
+
} catch {
|
|
325
|
+
fileLogDisabled = true;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
function write(method, scope, message, fields, options) {
|
|
329
|
+
if (LEVELS[method] < LEVELS[readLevel()])
|
|
330
|
+
return;
|
|
331
|
+
const payload = normalize(fields);
|
|
332
|
+
writeFile(method, scope, message, payload);
|
|
333
|
+
if (!(options?.console ?? readConsoleEnabled()))
|
|
334
|
+
return;
|
|
335
|
+
const line = `${LOG_PREFIX} ${scope} ${message}`;
|
|
336
|
+
if (payload && Object.keys(payload).length > 0) {
|
|
337
|
+
console[method](line, payload);
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
console[method](line);
|
|
341
|
+
}
|
|
342
|
+
function createLogger(scope, options) {
|
|
343
|
+
return {
|
|
344
|
+
debug(message, fields) {
|
|
345
|
+
write("debug", scope, message, fields, options);
|
|
346
|
+
},
|
|
347
|
+
info(message, fields) {
|
|
348
|
+
write("info", scope, message, fields, options);
|
|
349
|
+
},
|
|
350
|
+
warn(message, fields) {
|
|
351
|
+
write("warn", scope, message, fields, options);
|
|
352
|
+
},
|
|
353
|
+
error(message, fields) {
|
|
354
|
+
write("error", scope, message, fields, options);
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// src/voice-helper-process.ts
|
|
360
|
+
var logger = createLogger("helper-process");
|
|
361
|
+
var send = (message) => {
|
|
362
|
+
stdout.write(`${JSON.stringify(message)}
|
|
363
|
+
`);
|
|
364
|
+
};
|
|
365
|
+
function createServiceState() {
|
|
366
|
+
return {
|
|
367
|
+
runtimeKey: "",
|
|
368
|
+
cancelledRequests: new Set,
|
|
369
|
+
cancelledEpoch: -1,
|
|
370
|
+
sequence: 0,
|
|
371
|
+
disposed: false,
|
|
372
|
+
work: Promise.resolve()
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
var service = createServiceState();
|
|
376
|
+
function formatError2(error) {
|
|
377
|
+
if (error instanceof Error && error.message)
|
|
378
|
+
return error.message;
|
|
379
|
+
return String(error);
|
|
380
|
+
}
|
|
381
|
+
function runtimeIdentity(config) {
|
|
382
|
+
return JSON.stringify({
|
|
383
|
+
model: config.model,
|
|
384
|
+
dtype: config.dtype,
|
|
385
|
+
device: config.device,
|
|
386
|
+
cacheDir: config.cacheDir,
|
|
387
|
+
trimSilenceThreshold: config.trimSilenceThreshold,
|
|
388
|
+
leadingAudioPadMs: config.leadingAudioPadMs
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
function setTransformersCache(config) {
|
|
392
|
+
if (!config.cacheDir)
|
|
393
|
+
return;
|
|
394
|
+
process.env.TRANSFORMERS_CACHE = config.cacheDir;
|
|
395
|
+
process.env.HF_HOME ??= config.cacheDir;
|
|
396
|
+
}
|
|
397
|
+
function status(status) {
|
|
398
|
+
send({ type: "status", status });
|
|
399
|
+
}
|
|
400
|
+
function runtimeFor(state, config) {
|
|
401
|
+
const key = runtimeIdentity(config);
|
|
402
|
+
state.currentConfig = config;
|
|
403
|
+
if (state.runtime && state.runtimeKey === key)
|
|
404
|
+
return state.runtime;
|
|
405
|
+
setTransformersCache(config);
|
|
406
|
+
state.runtimeKey = key;
|
|
407
|
+
state.runtime = new KokoroRuntime(config, status, createLogger("kokoro"));
|
|
408
|
+
return state.runtime;
|
|
409
|
+
}
|
|
410
|
+
async function ensureTempDir(state) {
|
|
411
|
+
if (!state.tempDir) {
|
|
412
|
+
state.tempDir = fs2.mkdtemp(path2.join(os2.tmpdir(), "opencode-tts-voice-"));
|
|
413
|
+
logger.info("temp dir requested");
|
|
414
|
+
}
|
|
415
|
+
return state.tempDir;
|
|
416
|
+
}
|
|
417
|
+
async function writeAudioFile(state, audio, sampleRate, request) {
|
|
418
|
+
const config = state.currentConfig;
|
|
419
|
+
if (!config)
|
|
420
|
+
throw new Error("Missing voice config");
|
|
421
|
+
const dir = await ensureTempDir(state);
|
|
422
|
+
const trimmed = trimSilence(audio, sampleRate, config.trimSilenceThreshold, config.leadingAudioPadMs);
|
|
423
|
+
const file = path2.join(dir, `${Date.now()}-${request.id}-${state.sequence++}.wav`);
|
|
424
|
+
await fs2.writeFile(file, wavFromFloat32(trimmed, sampleRate));
|
|
425
|
+
logger.info("audio file written", {
|
|
426
|
+
file,
|
|
427
|
+
requestID: request.id,
|
|
428
|
+
sampleRate,
|
|
429
|
+
inputSamples: audio.length,
|
|
430
|
+
outputSamples: trimmed.length
|
|
431
|
+
});
|
|
432
|
+
return file;
|
|
433
|
+
}
|
|
434
|
+
function isCancelled(state, request) {
|
|
435
|
+
return request.cancelled || state.cancelledRequests.has(request.id) || request.epoch <= state.cancelledEpoch || state.disposed;
|
|
436
|
+
}
|
|
437
|
+
async function generate(state, input) {
|
|
438
|
+
const request = {
|
|
439
|
+
id: input.id,
|
|
440
|
+
epoch: input.epoch,
|
|
441
|
+
cancelled: false
|
|
442
|
+
};
|
|
443
|
+
state.active = request;
|
|
444
|
+
try {
|
|
445
|
+
if (isCancelled(state, request)) {
|
|
446
|
+
send({ type: "cancelled", id: request.id, epoch: request.epoch });
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
const tts = runtimeFor(state, input.config);
|
|
450
|
+
for await (const segment of tts.stream(input.text, input.config.speed, input.config.voice)) {
|
|
451
|
+
if (isCancelled(state, request))
|
|
452
|
+
break;
|
|
453
|
+
const file = await writeAudioFile(state, segment.audio, segment.sampleRate, request);
|
|
454
|
+
if (isCancelled(state, request)) {
|
|
455
|
+
await fs2.unlink(file).catch(() => {
|
|
456
|
+
return;
|
|
457
|
+
});
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
send({
|
|
461
|
+
type: "segment",
|
|
462
|
+
id: request.id,
|
|
463
|
+
epoch: request.epoch,
|
|
464
|
+
text: segment.text,
|
|
465
|
+
file
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
if (isCancelled(state, request)) {
|
|
469
|
+
send({ type: "cancelled", id: request.id, epoch: request.epoch });
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
send({ type: "complete", id: request.id, epoch: request.epoch });
|
|
473
|
+
} catch (error) {
|
|
474
|
+
send({ type: "error", id: request.id, epoch: request.epoch, error: formatError2(error) });
|
|
475
|
+
} finally {
|
|
476
|
+
if (state.active === request)
|
|
477
|
+
state.active = undefined;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
function cleanupServiceState(state) {
|
|
481
|
+
if (state.cleanup)
|
|
482
|
+
return state.cleanup;
|
|
483
|
+
state.disposed = true;
|
|
484
|
+
if (state.active)
|
|
485
|
+
state.active.cancelled = true;
|
|
486
|
+
state.cleanup = (async () => {
|
|
487
|
+
await state.work.catch(() => {
|
|
488
|
+
return;
|
|
489
|
+
});
|
|
490
|
+
try {
|
|
491
|
+
await state.runtime?.dispose();
|
|
492
|
+
} finally {
|
|
493
|
+
state.runtime = undefined;
|
|
494
|
+
const dir = state.tempDir;
|
|
495
|
+
state.tempDir = undefined;
|
|
496
|
+
if (dir) {
|
|
497
|
+
const value = await dir.catch(() => {
|
|
498
|
+
return;
|
|
499
|
+
});
|
|
500
|
+
if (value)
|
|
501
|
+
await fs2.rm(value, { recursive: true, force: true }).catch(() => {
|
|
502
|
+
return;
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
})();
|
|
507
|
+
return state.cleanup;
|
|
508
|
+
}
|
|
509
|
+
async function cleanup() {
|
|
510
|
+
await cleanupServiceState(service);
|
|
511
|
+
}
|
|
512
|
+
function handleServiceMessage(state, message) {
|
|
513
|
+
if (message.type === "generate") {
|
|
514
|
+
if (state.disposed) {
|
|
515
|
+
send({ type: "error", id: message.id, epoch: message.epoch, error: "TTS helper is disposed" });
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
state.work = state.work.then(() => generate(state, message));
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
if (message.type === "cancel") {
|
|
522
|
+
if (typeof message.id === "number")
|
|
523
|
+
state.cancelledRequests.add(message.id);
|
|
524
|
+
if (typeof message.epoch === "number")
|
|
525
|
+
state.cancelledEpoch = Math.max(state.cancelledEpoch, message.epoch);
|
|
526
|
+
if (state.active && isCancelled(state, state.active))
|
|
527
|
+
state.active.cancelled = true;
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
cleanupServiceState(state);
|
|
531
|
+
}
|
|
532
|
+
async function handle(message) {
|
|
533
|
+
if (message.type === "dispose") {
|
|
534
|
+
await cleanup();
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
handleServiceMessage(service, message);
|
|
538
|
+
}
|
|
539
|
+
async function main() {
|
|
540
|
+
const lines = createInterface({ input: stdin });
|
|
541
|
+
const stopInput = () => {
|
|
542
|
+
lines.close();
|
|
543
|
+
stdin.destroy();
|
|
544
|
+
};
|
|
545
|
+
const terminate = () => {
|
|
546
|
+
stopInput();
|
|
547
|
+
cleanup().catch((error) => {
|
|
548
|
+
send({ type: "error", error: formatError2(error) });
|
|
549
|
+
process.exitCode = 1;
|
|
550
|
+
});
|
|
551
|
+
};
|
|
552
|
+
process.once("SIGTERM", terminate);
|
|
553
|
+
try {
|
|
554
|
+
for await (const line of lines) {
|
|
555
|
+
const text = line.trim();
|
|
556
|
+
if (!text)
|
|
557
|
+
continue;
|
|
558
|
+
try {
|
|
559
|
+
const message = JSON.parse(text);
|
|
560
|
+
await handle(message);
|
|
561
|
+
if (message.type === "dispose")
|
|
562
|
+
break;
|
|
563
|
+
} catch (error) {
|
|
564
|
+
send({ type: "error", error: formatError2(error) });
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
} finally {
|
|
568
|
+
stopInput();
|
|
569
|
+
try {
|
|
570
|
+
await cleanup();
|
|
571
|
+
} finally {
|
|
572
|
+
process.removeListener("SIGTERM", terminate);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
function startWorkerMode() {
|
|
577
|
+
logger.info("helper worker mode started");
|
|
578
|
+
send = (message) => {
|
|
579
|
+
postMessage(message);
|
|
580
|
+
};
|
|
581
|
+
addEventListener("message", (event) => {
|
|
582
|
+
handle(event.data).catch((error) => {
|
|
583
|
+
send({ type: "error", error: formatError2(error) });
|
|
584
|
+
});
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
function startProcessMode() {
|
|
588
|
+
if (process.env.OPENCODE_TTS_VOICE_HELPER_MODE === "worker") {
|
|
589
|
+
throw new Error("Refusing to start helper process mode while worker mode is requested");
|
|
590
|
+
}
|
|
591
|
+
logger.info("helper process mode started");
|
|
592
|
+
main().catch((error) => {
|
|
593
|
+
send({ type: "error", error: formatError2(error) });
|
|
594
|
+
process.exitCode = 1;
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
function isEntrypoint() {
|
|
598
|
+
const entry = process.argv[1];
|
|
599
|
+
if (!entry)
|
|
600
|
+
return process.env.OPENCODE_TTS_VOICE_HELPER_MODE === "worker";
|
|
601
|
+
return pathToFileURL(path2.resolve(entry)).href === import.meta.url;
|
|
602
|
+
}
|
|
603
|
+
if (isEntrypoint()) {
|
|
604
|
+
if (process.env.OPENCODE_TTS_VOICE_HELPER_MODE === "worker") {
|
|
605
|
+
startWorkerMode();
|
|
606
|
+
} else {
|
|
607
|
+
startProcessMode();
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
export {
|
|
611
|
+
cleanupServiceState,
|
|
612
|
+
createServiceState,
|
|
613
|
+
handleServiceMessage,
|
|
614
|
+
runtimeIdentity
|
|
615
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { VoiceConfig, VoiceState } from "./voice-types.js";
|
|
2
|
+
export type RuntimeStatus = Partial<Pick<VoiceState, "device" | "error">>;
|
|
3
|
+
export type HelperRequest = {
|
|
4
|
+
type: "generate";
|
|
5
|
+
id: number;
|
|
6
|
+
epoch: number;
|
|
7
|
+
text: string;
|
|
8
|
+
config: VoiceConfig;
|
|
9
|
+
} | {
|
|
10
|
+
type: "cancel";
|
|
11
|
+
id?: number;
|
|
12
|
+
epoch?: number;
|
|
13
|
+
} | {
|
|
14
|
+
type: "dispose";
|
|
15
|
+
};
|
|
16
|
+
export type HelperResponse = {
|
|
17
|
+
type: "status";
|
|
18
|
+
status: RuntimeStatus;
|
|
19
|
+
} | {
|
|
20
|
+
type: "segment";
|
|
21
|
+
id: number;
|
|
22
|
+
epoch: number;
|
|
23
|
+
text: string;
|
|
24
|
+
file: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: "complete";
|
|
27
|
+
id: number;
|
|
28
|
+
epoch: number;
|
|
29
|
+
} | {
|
|
30
|
+
type: "cancelled";
|
|
31
|
+
id: number;
|
|
32
|
+
epoch: number;
|
|
33
|
+
} | {
|
|
34
|
+
type: "error";
|
|
35
|
+
id?: number;
|
|
36
|
+
epoch?: number;
|
|
37
|
+
error: string;
|
|
38
|
+
};
|