From c821da6128263ef2ab1522e424f7eee896d0be25 Mon Sep 17 00:00:00 2001 From: Fede Ciliberti Date: Sat, 31 Oct 2026 16:33:42 -0300 Subject: [PATCH] feat(keybinds): make command bindings configurable V2 keymaps take the binding from the command definition, so a plugin command cannot be remapped from cli.json. Expose the bindings through the `keybinds` plugin option instead, keeping the upstream defaults when nothing is set and allowing an empty string or "none" to disable a binding. Covers stt.record/submit/stop/mic and tts.speak-last/mode/stop/voice. The registerTTS signature now also receives the plugin options. --- AGENTS.md | 3 +++ README.md | 32 +++++++++++++++++++++++++++++ index.js | 4 ++-- lib/keybinds.js | 48 +++++++++++++++++++++++++++++++++++++++++++ lib/stt.js | 9 ++++++-- lib/tts.js | 11 ++++++---- test/keybinds.test.js | 36 ++++++++++++++++++++++++++++++++ 7 files changed, 135 insertions(+), 8 deletions(-) create mode 100644 lib/keybinds.js create mode 100644 test/keybinds.test.js diff --git a/AGENTS.md b/AGENTS.md index d9d71c4..8f7c38c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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` / diff --git a/README.md b/README.md index 1146a9e..dd43478 100644 --- a/README.md +++ b/README.md @@ -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": "i", "voice": "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 diff --git a/index.js b/index.js index aa2007e..f3404a3 100644 --- a/index.js +++ b/index.js @@ -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(), @@ -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", diff --git a/lib/keybinds.js b/lib/keybinds.js new file mode 100644 index 0000000..f0f2d4d --- /dev/null +++ b/lib/keybinds.js @@ -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: "r", + cancelRecording: "", + mic: "", + speak: "s", + toggleTts: "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} + */ +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; +} diff --git a/lib/stt.js b/lib/stt.js index fa6ea80..3fcaa7c 100644 --- a/lib/stt.js +++ b/lib/stt.js @@ -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; @@ -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") { @@ -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) { @@ -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: "r", + keybind: binds.submit, slash: { name: "stt-submit" }, onSelect() { if (processing) { @@ -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) { @@ -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() { diff --git a/lib/tts.js b/lib/tts.js index b55ae88..835602f 100644 --- a/lib/tts.js +++ b/lib/tts.js @@ -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"); @@ -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; @@ -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: "s", + keybind: binds.speak, slash: { name: "tts-speak" }, onSelect() { speakLastResponse(); @@ -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: "v", + keybind: binds.toggleTts, slash: { name: "tts-mode" }, onSelect() { const current = kv.get("tts.mode", "off"); @@ -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 @@ -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() { diff --git a/test/keybinds.test.js b/test/keybinds.test.js new file mode 100644 index 0000000..f80811d --- /dev/null +++ b/test/keybinds.test.js @@ -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); +});