Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Single TUI plugin exported from `index.js` with logic split into `lib/`.
- Single TUI plugin via `Plugin.define` from `@opencode/plugin/tui`; `setup`
returns a cleanup that detaches the data listeners. No server-side plugin.
- LLM calls use the OpenAI chat completions API, not the Anthropic messages API.
- Command keybinds come from `resolveKeybinds()` in `lib/keybinds.js` (the
`keybinds` plugin option). V2 keymaps read the binding from the command
definition, so they cannot be remapped from `cli.json`.
- Configuration uses `options` (static) and the storage-backed `api.kv`
(runtime). No dotfile I/O.
- Auto TTS triggers on `session.execution.started` / `succeeded` /
Expand Down
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,38 @@ then `s`.
| `/tts-stop` | `escape` | Stop playback |
| `/tts-voice` | | Select TTS voice |

### Custom keybinds

V2 keymaps take the binding from the command definition, so plugin commands
cannot be remapped from `cli.json`. Set them through the `keybinds` plugin
option instead; keys you omit keep their default.

```json
{
"plugins": [
{
"package": "@renjfk/opencode-voice",
"options": {
"keybinds": { "record": "f5", "mic": "<leader>i", "voice": "<leader>o" }
}
}
]
}
```

| Key | Default | Command |
| ----------------- | ---------- | ------------- |
| `record` | `ctrl+r` | `/stt-record` |
| `submit` | `leader+r` | `/stt-submit` |
| `cancelRecording` | — | `/stt-stop` |
| `mic` | — | `/stt-mic` |
| `speak` | `leader+s` | `/tts-speak` |
| `toggleTts` | `leader+v` | `/tts-mode` |
| `stopPlayback` | `escape` | `/tts-stop` |
| `voice` | — | `/tts-voice` |

Use an empty string or `"none"` to disable a binding.

## How it works

### STT pipeline
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ function toCommand(command) {
id: command.value,
title: command.title,
description: command.description,
bind: command.keybind,
bind: command.keybind || undefined,
palette: true,
slash: command.slash,
run: () => command.onSelect(),
Expand All @@ -264,7 +264,7 @@ export default Plugin.define({
const api = createApi(context, kv);

const sttCommands = registerSTT(api, kv, complete, prompts, options, logger);
const ttsCommands = registerTTS(api, kv, complete, prompts, logger);
const ttsCommands = registerTTS(api, kv, complete, prompts, options, logger);

context.keymap.layer(() => ({
mode: "global",
Expand Down
48 changes: 48 additions & 0 deletions lib/keybinds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Configurable keybinds for the plugin commands.
//
// V2 keymaps take the binding from the command definition, so a user cannot
// remap a plugin command from cli.json. This resolves the bindings from plugin
// options instead, keeping the upstream defaults when nothing is configured.
//
// Options (plugin options in cli.json / opencode.json):
// keybinds: {
// record, submit, cancelRecording, mic, // STT
// speak, toggleTts, stopPlayback, voice // TTS
// }
// An empty string or "none" disables the binding for that command.

export const DEFAULT_KEYBINDS = {
record: "ctrl+r",
submit: "<leader>r",
cancelRecording: "",
mic: "",
speak: "<leader>s",
toggleTts: "<leader>v",
stopPlayback: "escape",
voice: "",
};

function normalizeBind(value) {
if (typeof value !== "string") return null;
const trimmed = value.trim();
if (!trimmed || trimmed.toLowerCase() === "none") return "";
return trimmed;
}

/**
* Merge the `keybinds` plugin option over the defaults.
*
* @param {object} [options] - Plugin options
* @returns {Record<keyof typeof DEFAULT_KEYBINDS, string>}
*/
export function resolveKeybinds(options) {
const resolved = { ...DEFAULT_KEYBINDS };
const overrides = options?.keybinds;
if (!overrides || typeof overrides !== "object") return resolved;
for (const key of Object.keys(DEFAULT_KEYBINDS)) {
if (!(key in overrides)) continue;
const bind = normalizeBind(overrides[key]);
if (bind !== null) resolved[key] = bind;
}
return resolved;
}
9 changes: 7 additions & 2 deletions lib/stt.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import path from "node:path";
import os from "node:os";
import { spawn, execSync } from "node:child_process";

import { resolveKeybinds } from "./keybinds.js";

let sttApiEndpoint = null;
let sttApiModel = null;
let sttApiKeyEnv = null;
Expand Down Expand Up @@ -604,6 +606,7 @@ async function doTranscribePipeline(

export function registerSTT(api, kv, complete, prompts, opts, logger) {
const systemPrompt = prompts?.stt || STT_SYSTEM_PROMPT;
const binds = resolveKeybinds(opts);
const backend = detectAudioBackend();
logger?.log("STT", `Audio backend=${backend}`, "debug");
function toast(message, variant = "info") {
Expand Down Expand Up @@ -641,7 +644,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger) {
description: sttApiEndpoint
? "Toggle recording; press again to stop and transcribe via API"
: "Toggle recording; press again to stop and transcribe",
keybind: "ctrl+r",
keybind: binds.record,
slash: { name: "stt-record" },
onSelect() {
if (processing) {
Expand All @@ -663,7 +666,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger) {
description: sttApiEndpoint
? "Stop recording, transcribe via API, and submit prompt"
: "Stop recording, transcribe, and submit prompt",
keybind: "<leader>r",
keybind: binds.submit,
slash: { name: "stt-submit" },
onSelect() {
if (processing) {
Expand All @@ -682,6 +685,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger) {
title: "STT: cancel recording",
value: "stt.stop",
description: "Cancel current recording",
keybind: binds.cancelRecording,
slash: { name: "stt-stop" },
onSelect() {
if (recording) {
Expand Down Expand Up @@ -764,6 +768,7 @@ export function registerSTT(api, kv, complete, prompts, opts, logger) {
{
title: "STT: select microphone",
value: "stt.mic",
keybind: binds.mic,
description: "Choose audio input device",
slash: { name: "stt-mic" },
onSelect() {
Expand Down
11 changes: 7 additions & 4 deletions lib/tts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from "node:path";
import os from "node:os";
import { spawn } from "node:child_process";
import { getSessionTitle } from "./session.js";
import { resolveKeybinds } from "./keybinds.js";

const VOICES_DIR = path.join(os.homedir(), ".local", "share", "piper-voices");

Expand Down Expand Up @@ -96,7 +97,8 @@ async function getTurnAssistantText(api) {

// ---- Public API for TUI plugin ----

export function registerTTS(api, kv, complete, prompts, logger) {
export function registerTTS(api, kv, complete, prompts, opts, logger) {
const binds = resolveKeybinds(opts);
const client = api.client;
const systemAuto = prompts?.ttsAuto || SYSTEM_AUTO;
const systemManual = prompts?.ttsManual || SYSTEM_MANUAL;
Expand Down Expand Up @@ -380,7 +382,7 @@ export function registerTTS(api, kv, complete, prompts, logger) {
title: "TTS: speak last response",
value: "tts.speak-last",
description: "Read the last assistant response aloud (detailed)",
keybind: "<leader>s",
keybind: binds.speak,
slash: { name: "tts-speak" },
onSelect() {
speakLastResponse();
Expand All @@ -390,7 +392,7 @@ export function registerTTS(api, kv, complete, prompts, logger) {
title: "TTS: toggle",
value: "tts.mode",
description: "Toggle auto text-to-speech on/off",
keybind: "<leader>v",
keybind: binds.toggleTts,
slash: { name: "tts-mode" },
onSelect() {
const current = kv.get("tts.mode", "off");
Expand All @@ -406,7 +408,7 @@ export function registerTTS(api, kv, complete, prompts, logger) {
title: "TTS: stop playback",
value: "tts.stop",
description: "Stop current TTS playback",
keybind: "escape",
keybind: binds.stopPlayback,
slash: { name: "tts-stop" },
onSelect() {
// Returning false when nothing is playing lets Escape fall through to
Expand All @@ -419,6 +421,7 @@ export function registerTTS(api, kv, complete, prompts, logger) {
{
title: "TTS: select voice",
value: "tts.voice",
keybind: binds.voice,
description: "Choose TTS voice",
slash: { name: "tts-voice" },
onSelect() {
Expand Down
36 changes: 36 additions & 0 deletions test/keybinds.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { test } from "node:test";
import assert from "node:assert/strict";

import { DEFAULT_KEYBINDS, resolveKeybinds } from "../lib/keybinds.js";

test("keybinds: sin opciones devuelve los defaults de upstream", () => {
const binds = resolveKeybinds();
assert.deepEqual(binds, DEFAULT_KEYBINDS);
assert.equal(binds.record, "ctrl+r");
assert.equal(binds.stopPlayback, "escape");
});

test("keybinds: override reemplaza el default", () => {
const binds = resolveKeybinds({ keybinds: { record: "f5" } });
assert.equal(binds.record, "f5");
assert.equal(binds.submit, DEFAULT_KEYBINDS.submit);
});

test("keybinds: string vacío o 'none' desactiva el binding", () => {
const binds = resolveKeybinds({ keybinds: { record: "", speak: "none", mic: " " } });
assert.equal(binds.record, "");
assert.equal(binds.speak, "");
assert.equal(binds.mic, "");
});

test("keybinds: ignora claves desconocidas y valores no-string", () => {
const binds = resolveKeybinds({ keybinds: { record: 42, nope: "f9", mic: "f10" } });
assert.equal(binds.record, DEFAULT_KEYBINDS.record);
assert.equal(binds.mic, "f10");
assert.equal("nope" in binds, false);
});

test("keybinds: keybinds no-objeto se ignora", () => {
assert.deepEqual(resolveKeybinds({ keybinds: "f5" }), DEFAULT_KEYBINDS);
assert.deepEqual(resolveKeybinds({ keybinds: null }), DEFAULT_KEYBINDS);
});