@scotteza/opencode-spoken-replies 1.0.5 → 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
@@ -2,9 +2,9 @@
2
2
 
3
3
  OpenCode Spoken Replies lets OpenCode read assistant replies aloud as they arrive. Choose a voice, adjust the speed, and pause or replay speech from the terminal interface.
4
4
 
5
- This independently maintained fork builds on [opencode-tts-voice](https://github.com/semirhuduti/opencode-tts-voice). It is not maintained, endorsed, or supported by the original author.
5
+ This independently maintained fork builds on [opencode-tts-voice](https://github.com/semirhuduti/opencode-tts-voice) from Semir Huduti. It is not maintained, endorsed, or supported by the original author.
6
6
 
7
- If you appreciate the original author's work, please [buy Semir Huduti a coffee](https://www.buymeacoffee.com/semirhuduti).
7
+ If you appreciate the original author's work, please buy him a coffee using the link below .
8
8
 
9
9
  [![Buy Semir Huduti a coffee](https://cdn.buymeacoffee.com/buttons/default-orange.png)](https://www.buymeacoffee.com/semirhuduti)
10
10
 
@@ -27,6 +27,42 @@ Install the published npm package with:
27
27
  opencode plugin add @scotteza/opencode-spoken-replies
28
28
  ```
29
29
 
30
+ ### Windows setup
31
+
32
+ The TTS helper needs Node.js or Bun available to the OpenCode process. If OpenCode reports that it cannot find either runtime, install [Node.js](https://nodejs.org/en/download/) for Windows and enable the installer option to add Node to `PATH`. Then fully exit and restart OpenCode so it inherits the updated `PATH`. Verify Node is available in PowerShell with:
33
+
34
+ ```powershell
35
+ node --version
36
+ where.exe node
37
+ ```
38
+
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:
52
+
53
+ ```jsonc
54
+ {
55
+ "plugins": [
56
+ {
57
+ "package": "@scotteza/opencode-spoken-replies",
58
+ "options": {
59
+ "ttsServiceCommand": "C:\\Program Files\\nodejs\\node.exe"
60
+ }
61
+ }
62
+ ]
63
+ }
64
+ ```
65
+
30
66
  Or add it to the `plugins` array in your OpenCode `opencode.json` or `opencode.jsonc`:
31
67
 
32
68
  ```jsonc
@@ -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;
@@ -43,6 +43,7 @@ import { Rpc } from "@opencode/plugin/rpc";
43
43
 
44
44
  // src/voice-constants.ts
45
45
  var PLUGIN_ID = "opencode-spoken-replies";
46
+ var PLUGIN_PACKAGE = "@scotteza/opencode-spoken-replies";
46
47
  var KV_ENABLED = `${PLUGIN_ID}.enabled`;
47
48
  var DEFAULT_MODEL = "onnx-community/Kokoro-82M-v1.0-ONNX";
48
49
  var DEFAULT_CONFIG = {
@@ -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;
@@ -1434,6 +1453,7 @@ import * as path3 from "path";
1434
1453
 
1435
1454
  // src/voice-constants.ts
1436
1455
  var PLUGIN_ID = "opencode-spoken-replies";
1456
+ var PLUGIN_PACKAGE = "@scotteza/opencode-spoken-replies";
1437
1457
  var KV_ENABLED = `${PLUGIN_ID}.enabled`;
1438
1458
  var DEFAULT_MODEL = "onnx-community/Kokoro-82M-v1.0-ONNX";
1439
1459
  var DEFAULT_CONFIG = {
@@ -1478,7 +1498,7 @@ var PLAYER_CANDIDATES = ["ffplay", "mpv", "paplay", "aplay"];
1478
1498
 
1479
1499
  // src/playback/player-service.ts
1480
1500
  function toBasePlayer(playerBin) {
1481
- return path3.basename(playerBin).toLowerCase();
1501
+ return (playerBin.split(/[\\/]/).pop() ?? playerBin).toLowerCase().replace(/\.exe$/i, "");
1482
1502
  }
1483
1503
  function isMissingBinary(error) {
1484
1504
  return error !== null && typeof error === "object" && "code" in error && error.code === "ENOENT";
@@ -1491,17 +1511,19 @@ function isExecutable2(file) {
1491
1511
  return false;
1492
1512
  }
1493
1513
  }
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)
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)
1501
1520
  continue;
1502
- const candidate = path3.join(dir, playerBin);
1503
- if (isExecutable2(candidate))
1504
- 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
+ }
1505
1527
  }
1506
1528
  return;
1507
1529
  }
@@ -1548,7 +1570,7 @@ class PlayerService {
1548
1570
  this.logger.info("resolve player success", describePlayer(resolved));
1549
1571
  return resolved;
1550
1572
  }
1551
- 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()}`);
1552
1574
  }
1553
1575
  async playFile(file, current) {
1554
1576
  const playerBin = this.resolvePlayerBin();
@@ -2276,7 +2298,7 @@ class PlaybackPipeline {
2276
2298
  if (this.currentPlay)
2277
2299
  this.currentPlay.interrupted = true;
2278
2300
  await this.discardReadyAudio(bufferedReadyAudio);
2279
- this.toastError(`Playback failed. Command not found: ${this.config.audioPlayer}`);
2301
+ this.toastError(`Playback failed. Command not found: ${this.config.audioPlayer}. ${audioPlayerInstallHint()}`);
2280
2302
  return;
2281
2303
  }
2282
2304
  this.toastError(`Speech failed. ${message}`);
@@ -2981,6 +3003,46 @@ function resolveVoiceConfig(options) {
2981
3003
  };
2982
3004
  }
2983
3005
 
3006
+ // src/voice-plugin-options.ts
3007
+ function isRecord2(value) {
3008
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
3009
+ }
3010
+ function optionsFromEntry(entry) {
3011
+ if (isRecord2(entry) && entry.package === PLUGIN_PACKAGE && isRecord2(entry.options)) {
3012
+ return entry.options;
3013
+ }
3014
+ if (Array.isArray(entry) && entry[0] === PLUGIN_PACKAGE && isRecord2(entry[1])) {
3015
+ return entry[1];
3016
+ }
3017
+ return;
3018
+ }
3019
+ function findVoicePluginOptions(response) {
3020
+ const payload = isRecord2(response) && "data" in response ? response.data : response;
3021
+ const documents = Array.isArray(payload) ? payload : [payload];
3022
+ const options = {};
3023
+ let found = false;
3024
+ for (const document of documents) {
3025
+ if (!isRecord2(document))
3026
+ continue;
3027
+ const info = isRecord2(document.info) ? document.info : document;
3028
+ const entries = Array.isArray(info.plugins) ? info.plugins : Array.isArray(info.plugin) ? info.plugin : [];
3029
+ for (const entry of entries) {
3030
+ const configured = optionsFromEntry(entry);
3031
+ if (!configured)
3032
+ continue;
3033
+ Object.assign(options, configured);
3034
+ found = true;
3035
+ }
3036
+ }
3037
+ return found ? options : undefined;
3038
+ }
3039
+ function mergeVoicePluginOptions(configured, runtime) {
3040
+ return {
3041
+ ...configured ?? {},
3042
+ ...isRecord2(runtime) ? runtime : {}
3043
+ };
3044
+ }
3045
+
2984
3046
  // src/voice-rpc.ts
2985
3047
  import { Rpc } from "@opencode/plugin/rpc";
2986
3048
  var state = {
@@ -3018,7 +3080,15 @@ function Status(props) {
3018
3080
  var tui_plugin_default = Plugin.define({
3019
3081
  id: PLUGIN_ID,
3020
3082
  async setup(api) {
3021
- const config = resolveVoiceConfig(api.options);
3083
+ let configuredOptions;
3084
+ try {
3085
+ configuredOptions = findVoicePluginOptions(await api.client.config.get());
3086
+ } catch (error) {
3087
+ log.warn("could not read plugin options from OpenCode config", {
3088
+ error: String(error)
3089
+ });
3090
+ }
3091
+ const config = resolveVoiceConfig(mergeVoicePluginOptions(configuredOptions, api.options));
3022
3092
  const voice = api.client.rpc(VoiceRpc);
3023
3093
  const location = api.location ?? api.data.location.default();
3024
3094
  let enabled = true;
@@ -1,5 +1,6 @@
1
1
  import type { VoiceConfig } from "./voice-types.js";
2
2
  export declare const PLUGIN_ID = "opencode-spoken-replies";
3
+ export declare const PLUGIN_PACKAGE = "@scotteza/opencode-spoken-replies";
3
4
  export declare const KV_ENABLED = "opencode-spoken-replies.enabled";
4
5
  export declare const DEFAULT_MODEL = "onnx-community/Kokoro-82M-v1.0-ONNX";
5
6
  export declare const DEFAULT_CONFIG: VoiceConfig;
@@ -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 {};
@@ -0,0 +1,3 @@
1
+ import type { VoicePluginOptions } from "./voice-types.js";
2
+ export declare function findVoicePluginOptions(response: unknown): VoicePluginOptions | undefined;
3
+ export declare function mergeVoicePluginOptions(configured: VoicePluginOptions | undefined, runtime: unknown): VoicePluginOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scotteza/opencode-spoken-replies",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Local Kokoro text-to-speech for OpenCode assistant replies",
5
5
  "keywords": [
6
6
  "opencode",