@scotteza/opencode-spoken-replies 1.0.6 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -36,7 +36,19 @@ node --version
36
36
  where.exe node
37
37
  ```
38
38
 
39
- Windows also needs an audio player on `PATH`; install a Windows build of `ffplay` or `mpv`. If OpenCode still cannot find Node, set `ttsServiceCommand` in this plugin's `options` to the full path to `node.exe`, for example:
39
+ Windows also needs an audio player on `PATH`. One option is FFmpeg, which includes `ffplay`; install it with WinGet:
40
+
41
+ ```powershell
42
+ winget install --id Gyan.FFmpeg -e
43
+ ```
44
+
45
+ Open a new PowerShell window and verify that `ffplay` is available:
46
+
47
+ ```powershell
48
+ where.exe ffplay
49
+ ```
50
+
51
+ Then fully exit and restart OpenCode so it picks up the updated `PATH`. You can also install a Windows build of `mpv`. If OpenCode still cannot find Node, set `ttsServiceCommand` in this plugin's `options` to the full path to `node.exe`, for example:
40
52
 
41
53
  ```jsonc
42
54
  {
@@ -0,0 +1,2 @@
1
+ export declare function nodeRuntimeInstallHint(platform?: NodeJS.Platform): "On Windows, install Node.js LTS with `winget install --id OpenJS.NodeJS.LTS --exact`, then restart OpenCode." | "Install Node.js or Bun and make sure it is available on PATH, then restart OpenCode.";
2
+ export declare function audioPlayerInstallHint(platform?: NodeJS.Platform): "On Windows, install ffplay with `winget install --id Gyan.FFmpeg -e`, then restart OpenCode." | "Install an audio player such as ffplay (`brew install ffmpeg`) or mpv, then restart OpenCode." | "Install ffplay or mpv and make sure it is available on PATH (on Ubuntu/Debian, run `sudo apt install ffmpeg`).";
@@ -9,7 +9,7 @@ export type PlayFileTask = {
9
9
  };
10
10
  export declare function toBasePlayer(playerBin: string): string;
11
11
  export declare function isMissingBinary(error: unknown): error is NodeJS.ErrnoException;
12
- export declare function resolveExecutable(playerBin: string): string | undefined;
12
+ export declare function resolveExecutable(playerBin: string, env?: NodeJS.ProcessEnv, platform?: NodeJS.Platform): string | undefined;
13
13
  export declare function buildPlayerArgs(playerBin: string, args: string[], file: string): string[];
14
14
  export declare function describePlayer(playerBin: string): {
15
15
  audioPlayer: string;
@@ -1111,6 +1111,25 @@ import * as fsSync from "fs";
1111
1111
  import * as path2 from "path";
1112
1112
  import { fileURLToPath } from "url";
1113
1113
  import { createInterface } from "readline";
1114
+
1115
+ // src/platform-help.ts
1116
+ function nodeRuntimeInstallHint(platform = process.platform) {
1117
+ if (platform === "win32") {
1118
+ return "On Windows, install Node.js LTS with `winget install --id OpenJS.NodeJS.LTS --exact`, then restart OpenCode.";
1119
+ }
1120
+ return "Install Node.js or Bun and make sure it is available on PATH, then restart OpenCode.";
1121
+ }
1122
+ function audioPlayerInstallHint(platform = process.platform) {
1123
+ if (platform === "win32") {
1124
+ return "On Windows, install ffplay with `winget install --id Gyan.FFmpeg -e`, then restart OpenCode.";
1125
+ }
1126
+ if (platform === "darwin") {
1127
+ return "Install an audio player such as ffplay (`brew install ffmpeg`) or mpv, then restart OpenCode.";
1128
+ }
1129
+ return "Install ffplay or mpv and make sure it is available on PATH (on Ubuntu/Debian, run `sudo apt install ffmpeg`).";
1130
+ }
1131
+
1132
+ // src/voice-helper-runtime.ts
1114
1133
  function formatError2(error) {
1115
1134
  if (error instanceof Error && error.message)
1116
1135
  return error.message;
@@ -1150,7 +1169,7 @@ function resolveDefaultRuntime(execPath, env) {
1150
1169
  const runtime = resolveExecutable("bun", env) ?? resolveExecutable("node", env) ?? resolveExecutable("nodejs", env);
1151
1170
  if (runtime)
1152
1171
  return runtime;
1153
- throw new Error("No Node or Bun executable found for the TTS helper service. Install one, or set ttsServiceCommand.");
1172
+ throw new Error(`No Node or Bun executable found for the TTS helper service. ${nodeRuntimeInstallHint()} Alternatively, set ttsServiceCommand.`);
1154
1173
  }
1155
1174
  function resolveHelperServiceLaunch(config, helper, execPath = process.execPath, env = process.env) {
1156
1175
  let runtime;
@@ -1479,7 +1498,7 @@ var PLAYER_CANDIDATES = ["ffplay", "mpv", "paplay", "aplay"];
1479
1498
 
1480
1499
  // src/playback/player-service.ts
1481
1500
  function toBasePlayer(playerBin) {
1482
- return path3.basename(playerBin).toLowerCase();
1501
+ return (playerBin.split(/[\\/]/).pop() ?? playerBin).toLowerCase().replace(/\.exe$/i, "");
1483
1502
  }
1484
1503
  function isMissingBinary(error) {
1485
1504
  return error !== null && typeof error === "object" && "code" in error && error.code === "ENOENT";
@@ -1492,17 +1511,19 @@ function isExecutable2(file) {
1492
1511
  return false;
1493
1512
  }
1494
1513
  }
1495
- function resolveExecutable2(playerBin) {
1496
- if (playerBin.includes(path3.sep)) {
1497
- return isExecutable2(playerBin) ? playerBin : undefined;
1498
- }
1499
- const envPath = process.env.PATH ?? "";
1500
- for (const dir of envPath.split(path3.delimiter)) {
1501
- if (!dir)
1514
+ function resolveExecutable2(playerBin, env = process.env, platform = process.platform) {
1515
+ const hasPath = playerBin.includes(path3.sep) || platform === "win32" && playerBin.includes("/");
1516
+ const directories = hasPath ? [""] : (env.PATH ?? env.Path ?? "").split(platform === "win32" ? ";" : path3.delimiter);
1517
+ const extensions = platform === "win32" && !path3.extname(playerBin) ? (env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean) : [""];
1518
+ for (const dir of directories) {
1519
+ if (!hasPath && !dir)
1502
1520
  continue;
1503
- const candidate = path3.join(dir, playerBin);
1504
- if (isExecutable2(candidate))
1505
- return candidate;
1521
+ const base = hasPath ? playerBin : path3.join(dir, playerBin);
1522
+ for (const extension of extensions) {
1523
+ const candidate = `${base}${extension}`;
1524
+ if (isExecutable2(candidate))
1525
+ return candidate;
1526
+ }
1506
1527
  }
1507
1528
  return;
1508
1529
  }
@@ -1549,7 +1570,7 @@ class PlayerService {
1549
1570
  this.logger.info("resolve player success", describePlayer(resolved));
1550
1571
  return resolved;
1551
1572
  }
1552
- throw new Error(`No supported audio player found. Install one of: ${PLAYER_CANDIDATES.join(", ")}`);
1573
+ throw new Error(`No supported audio player found. Install one of: ${PLAYER_CANDIDATES.join(", ")}. ${audioPlayerInstallHint()}`);
1553
1574
  }
1554
1575
  async playFile(file, current) {
1555
1576
  const playerBin = this.resolvePlayerBin();
@@ -2277,7 +2298,7 @@ class PlaybackPipeline {
2277
2298
  if (this.currentPlay)
2278
2299
  this.currentPlay.interrupted = true;
2279
2300
  await this.discardReadyAudio(bufferedReadyAudio);
2280
- this.toastError(`Playback failed. Command not found: ${this.config.audioPlayer}`);
2301
+ this.toastError(`Playback failed. Command not found: ${this.config.audioPlayer}. ${audioPlayerInstallHint()}`);
2281
2302
  return;
2282
2303
  }
2283
2304
  this.toastError(`Speech failed. ${message}`);
@@ -108,6 +108,29 @@ function formatError(error) {
108
108
  return error.message;
109
109
  return String(error);
110
110
  }
111
+ async function runWithCpuFallback(runtime, operation, fallback, logger) {
112
+ try {
113
+ return { runtime, result: await operation(runtime) };
114
+ } catch (error) {
115
+ if (runtime.device === "cpu")
116
+ throw error;
117
+ logger.warn("accelerated inference failed; retrying on CPU", {
118
+ device: runtime.device,
119
+ error: formatError(error)
120
+ });
121
+ let cpuRuntime;
122
+ try {
123
+ cpuRuntime = await fallback();
124
+ } catch (cpuError) {
125
+ throw new Error(`Inference failed on ${runtime.device}: ${formatError(error)}. CPU fallback failed: ${formatError(cpuError)}`, { cause: cpuError });
126
+ }
127
+ try {
128
+ return { runtime: cpuRuntime, result: await operation(cpuRuntime) };
129
+ } catch (cpuError) {
130
+ throw new Error(`Inference failed on ${runtime.device}: ${formatError(error)}. CPU retry failed: ${formatError(cpuError)}`, { cause: cpuError });
131
+ }
132
+ }
133
+ }
111
134
  async function prepareOnnxRuntime() {
112
135
  const ort = await import("onnxruntime-node");
113
136
  ort.env.logLevel = "error";
@@ -144,11 +167,11 @@ class KokoroRuntime {
144
167
  voice: this.config.voice,
145
168
  speed: this.config.speed
146
169
  });
147
- const runtime = await this.load();
148
- const audio = await runtime.tts.generate(text, {
170
+ const initialRuntime = await this.load();
171
+ const { runtime, result: audio } = await this.withCpuFallback(initialRuntime, (candidate) => candidate.tts.generate(text, {
149
172
  voice: this.config.voice,
150
173
  speed: this.config.speed
151
- });
174
+ }));
152
175
  this.logger.info("generate complete", {
153
176
  textLength: text.length,
154
177
  sampleRate: audio.sampling_rate,
@@ -166,13 +189,16 @@ class KokoroRuntime {
166
189
  voice,
167
190
  speed
168
191
  });
169
- const runtime = await this.load();
192
+ const initialRuntime = await this.load();
193
+ const { runtime, result: segments } = await this.withCpuFallback(initialRuntime, async (candidate) => {
194
+ const output = [];
195
+ for await (const audio of candidate.tts.stream(text, { voice, speed }))
196
+ output.push(audio);
197
+ return output;
198
+ });
170
199
  let segmentCount = 0;
171
200
  let totalSamples = 0;
172
- for await (const audio of runtime.tts.stream(text, {
173
- voice,
174
- speed
175
- })) {
201
+ for (const audio of segments) {
176
202
  segmentCount += 1;
177
203
  totalSamples += audio.data.length;
178
204
  this.logger.info("stream segment", {
@@ -203,12 +229,12 @@ class KokoroRuntime {
203
229
  this.loading = undefined;
204
230
  await runtime?.dispose();
205
231
  }
206
- async load() {
232
+ async load(deviceOverride) {
207
233
  if (this.loaded)
208
234
  return this.loaded;
209
235
  if (this.loading)
210
236
  return this.loading;
211
- this.loading = this.create().then((runtime) => {
237
+ this.loading = this.create(deviceOverride).then((runtime) => {
212
238
  this.loaded = runtime;
213
239
  this.logger.info("runtime ready", { device: runtime.device });
214
240
  return runtime;
@@ -220,7 +246,22 @@ class KokoroRuntime {
220
246
  });
221
247
  return this.loading;
222
248
  }
223
- async create() {
249
+ async withCpuFallback(runtime, operation) {
250
+ return runWithCpuFallback(runtime, operation, () => this.replaceWithCpu(runtime), this.logger);
251
+ }
252
+ async replaceWithCpu(runtime) {
253
+ if (runtime.device === "cpu")
254
+ return runtime;
255
+ if (this.loaded === runtime)
256
+ this.loaded = undefined;
257
+ this.loading = undefined;
258
+ await runtime.dispose().catch((error) => {
259
+ this.logger.warn("failed to dispose accelerated runtime before CPU fallback", { error: formatError(error) });
260
+ });
261
+ this.logger.info("runtime CPU fallback start", { previousDevice: runtime.device });
262
+ return this.load(["cpu"]);
263
+ }
264
+ async create(deviceOverride) {
224
265
  this.onStatus({ device: "loading", error: undefined });
225
266
  this.logger.info("runtime import start", {
226
267
  model: this.config.model,
@@ -238,7 +279,7 @@ class KokoroRuntime {
238
279
  device
239
280
  });
240
281
  const errors = [];
241
- const devices = filterCandidateDevices(candidateDevices(this.config.device), availableProviders());
282
+ const devices = deviceOverride ?? filterCandidateDevices(candidateDevices(this.config.device), availableProviders());
242
283
  for (const device of devices) {
243
284
  this.logger.info("runtime init attempt", { device });
244
285
  try {
@@ -2,6 +2,27 @@ import type { VoiceConfig, VoiceState } from "./voice-types.js";
2
2
  import type { VoiceLogger } from "./voice-log.js";
3
3
  type TransformDevice = "auto" | "cpu" | "gpu" | "cuda" | "dml" | "wasm" | "webgpu";
4
4
  type RuntimeStatus = Partial<Pick<VoiceState, "device" | "error">>;
5
+ export type LoadedRuntime = {
6
+ dispose(): Promise<void>;
7
+ tts: {
8
+ generate(text: string, input: {
9
+ voice: string;
10
+ speed: number;
11
+ }): Promise<{
12
+ data: Float32Array;
13
+ sampling_rate: number;
14
+ }>;
15
+ stream(text: string, input: {
16
+ voice: string;
17
+ speed: number;
18
+ }): AsyncGenerator<{
19
+ text: string;
20
+ data: Float32Array;
21
+ sampling_rate: number;
22
+ }, void, void>;
23
+ };
24
+ device: string;
25
+ };
5
26
  export declare function candidateDevices(device: VoiceConfig["device"]): TransformDevice[];
6
27
  type AvailableProviders = {
7
28
  cuda: boolean;
@@ -9,6 +30,10 @@ type AvailableProviders = {
9
30
  webgpu: boolean;
10
31
  };
11
32
  export declare function filterCandidateDevices(devices: TransformDevice[], available: AvailableProviders): TransformDevice[];
33
+ export declare function runWithCpuFallback<Result>(runtime: LoadedRuntime, operation: (runtime: LoadedRuntime) => Promise<Result>, fallback: () => Promise<LoadedRuntime>, logger: VoiceLogger): Promise<{
34
+ runtime: LoadedRuntime;
35
+ result: Result;
36
+ }>;
12
37
  export declare class KokoroRuntime {
13
38
  private readonly config;
14
39
  private readonly onStatus;
@@ -27,6 +52,8 @@ export declare class KokoroRuntime {
27
52
  }, void, unknown>;
28
53
  dispose(): Promise<void>;
29
54
  private load;
55
+ private withCpuFallback;
56
+ private replaceWithCpu;
30
57
  private create;
31
58
  }
32
59
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scotteza/opencode-spoken-replies",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "Local Kokoro text-to-speech for OpenCode assistant replies",
5
5
  "keywords": [
6
6
  "opencode",