Skip to content

Commit 27433c0

Browse files
committed
fix(tui): guard voice key against auto-repeat by timing, document toggle
Review follow-up: the repeat filter never fired. The keyboard parser does not emit a repeat event type, and without event reporting a held key arrives as ordinary presses with no marker. Presses within a second of the previous one now count as key repeat (chained, so a whole hold is ignored); the terminal's repeated flag is honored too. An end-to-end test drives the real app with a fake recorder and asserts one recorder starts per simulated hold. Descriptions and docs now say press to start, press again to stop, since release events are not enabled.
1 parent d570acd commit 27433c0

8 files changed

Lines changed: 118 additions & 9 deletions

File tree

‎packages/tui/src/component/prompt/index.tsx‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
findRecorder,
4646
transcribeAudio,
4747
shouldDiscardTap,
48+
isRepeatPress,
4849
VOICE_MAX_RECORD_MS,
4950
} from "./voice"
5051
import { useRenderer, useTerminalDimensions } from "@opentui/solid"
@@ -958,11 +959,17 @@ export function Prompt(props: PromptProps) {
958959
let voiceStartTime = 0
959960
let voiceStopTimer: ReturnType<typeof setTimeout> | undefined
960961
let voiceStarting = false
962+
let voiceLastPress = 0
961963

962964
async function startVoiceRecording(event?: KeyEvent) {
963-
// Key repeat fires the press binding while the key is held; only a real
964-
// press may start or stop, so holding the key records without toggling.
965-
if (event?.eventType === "repeat") return
965+
// Holding the key fires this binding again for every auto-repeat, and the
966+
// TUI does not enable terminal event reporting, so repeats carry no marker.
967+
// Presses that follow the previous one closely are the key being held; only
968+
// a fresh press (after a quiet gap) may start or stop a recording.
969+
const now = Date.now()
970+
const repeating = isRepeatPress(now - voiceLastPress, event?.repeated)
971+
voiceLastPress = now
972+
if (repeating) return
966973
if (voiceRecorder?.recording) {
967974
void stopVoiceRecording("")
968975
return
@@ -1033,7 +1040,7 @@ export function Prompt(props: PromptProps) {
10331040
bindings: [
10341041
...press.map((binding) => ({
10351042
...binding,
1036-
desc: "Start voice recording (release or press again to transcribe)",
1043+
desc: "Start voice recording (press again to transcribe)",
10371044
group: "Prompt",
10381045
cmd: (context: CommandContext<Renderable, KeyEvent>) => void startVoiceRecording(context.event),
10391046
})),
@@ -1702,7 +1709,7 @@ export function Prompt(props: PromptProps) {
17021709

17031710
const placeholderText = createMemo(() => {
17041711
const value = (() => {
1705-
if (voiceRecording()) return "Recording voice… release or press again to stop"
1712+
if (voiceRecording()) return "Recording voice… press the key again to stop"
17061713
if (store.mode === "shell") {
17071714
if (!shell().length) return undefined
17081715
return `Run a command… "${shell()[store.placeholder % shell().length]}"`

‎packages/tui/src/component/prompt/voice.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { delimiter, join } from "node:path"
1313

1414
/** Holds shorter than this are treated as taps, not dictation. */
1515
export const VOICE_MIN_HOLD_MS = 350
16+
/** Presses closer together than this are the key repeating, not new presses. */
17+
export const VOICE_REPEAT_GUARD_MS = 1000
1618
/** Recordings stop automatically after this long. */
1719
export const VOICE_MAX_RECORD_MS = 120_000
1820
/** Placeholder replaced with the recorded wav path in the transcribe command. */
@@ -187,3 +189,14 @@ export function transcribeAudio(wavFile: string, template: string): Promise<stri
187189
export function shouldDiscardTap(holdMs: number): boolean {
188190
return holdMs < VOICE_MIN_HOLD_MS
189191
}
192+
193+
/**
194+
* True when a press is the key being held down rather than a fresh press.
195+
* Without terminal event reporting, auto-repeat arrives as ordinary presses
196+
* with no marker, so presses closer together than the guard window count as
197+
* repeats. The window must outlast the OS initial repeat delay (about 500 to
198+
* 660ms by default) and chains: every press, repeat or not, restarts it.
199+
*/
200+
export function isRepeatPress(sinceLastPressMs: number, repeated?: boolean): boolean {
201+
return repeated === true || sinceLastPressMs < VOICE_REPEAT_GUARD_MS
202+
}

‎packages/tui/src/config/keybind.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export const Definitions = {
200200
"prompt.stash": keybind("none", "Stash prompt"),
201201
"prompt.stash.pop": keybind("none", "Pop stashed prompt"),
202202
"prompt.stash.list": keybind("none", "List stashed prompts"),
203-
"prompt.voice.push_to_talk": keybind("none", "Record voice input; release or press again to transcribe"),
203+
"prompt.voice.push_to_talk": keybind("none", "Press to record voice input, press again to transcribe"),
204204

205205
"prompt.clear": keybind("ctrl+c", "Clear input field"),
206206
"prompt.paste": keybind({ key: "ctrl+v", preventDefault: false }, "Paste from clipboard"),

‎packages/tui/src/config/v1/keybind.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export const Definitions = {
170170
prompt_stash: keybind("none", "Stash prompt"),
171171
prompt_stash_pop: keybind("none", "Pop stashed prompt"),
172172
prompt_stash_list: keybind("none", "List stashed prompts"),
173-
prompt_voice_push_to_talk: keybind("none", "Record voice input; release or press again to transcribe"),
173+
prompt_voice_push_to_talk: keybind("none", "Press to record voice input, press again to transcribe"),
174174

175175
input_clear: keybind("ctrl+c", "Clear input field"),
176176
input_paste: keybind({ key: "ctrl+v", preventDefault: false }, "Paste from clipboard"),
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { afterEach, expect, test } from "bun:test"
2+
import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"
3+
import { tmpdir } from "node:os"
4+
import { join } from "node:path"
5+
import { createAppFixture } from "../fixture/app"
6+
7+
const originalPath = process.env.PATH
8+
afterEach(() => {
9+
process.env.PATH = originalPath
10+
})
11+
12+
// Drives the real app: a held key reaches the prompt as rapid ordinary presses
13+
// (terminals send no repeat marker without event reporting), and recording must
14+
// neither toggle off nor restart while it is held. The fake recorder logs each
15+
// start, so the assertion counts recorders instead of depending on the final
16+
// toggle state (an odd number of toggles would otherwise look correct).
17+
test("holding the voice key records once; a press after a quiet gap stops", async () => {
18+
const bin = mkdtempSync(join(tmpdir(), "opencode-voice-keys-"))
19+
const log = join(bin, "starts.log")
20+
const ffmpeg = join(bin, "ffmpeg")
21+
// Last argument is the output file, as ffmpegArgs builds it. Exit cleanly on INT.
22+
writeFileSync(
23+
ffmpeg,
24+
`#!/bin/sh\ntrap 'exit 0' INT\necho started >> '${log}'\nfor a in "$@"; do last="$a"; done\ntouch "$last"\nsleep 30 & wait\n`,
25+
)
26+
chmodSync(ffmpeg, 0o755)
27+
// Prepend: spawn() resolves ffmpeg through the child PATH.
28+
process.env.PATH = `${bin}:${originalPath}`
29+
const starts = () => (existsSync(log) ? readFileSync(log, "utf8").split("\n").filter(Boolean).length : 0)
30+
31+
await using setup = await createAppFixture({
32+
config: {
33+
animations: false,
34+
keybinds: { "prompt.voice.push_to_talk": "f7" },
35+
voice: { transcribe_command: "echo transcribed" },
36+
},
37+
})
38+
await setup.ready
39+
await setup.waitForFrame((frame) => frame.includes("ctrl+p commands"))
40+
41+
setup.mockInput.pressKey("F7")
42+
await setup.waitForFrame((frame) => frame.includes("Recording voice"))
43+
expect(starts()).toBe(1)
44+
45+
// Simulated hold: auto-repeat every ~33ms after the OS initial delay.
46+
for (let index = 0; index < 8; index++) {
47+
await Bun.sleep(33)
48+
setup.mockInput.pressKey("F7")
49+
}
50+
await Bun.sleep(200)
51+
await setup.renderOnce()
52+
expect(starts()).toBe(1)
53+
expect(setup.captureCharFrame()).toContain("Recording voice")
54+
55+
// A fresh press after a quiet gap stops and transcribes.
56+
await Bun.sleep(1100)
57+
setup.mockInput.pressKey("F7")
58+
await setup.waitForFrame((frame) => frame.includes("transcribed"))
59+
expect(setup.captureCharFrame()).not.toContain("Recording voice")
60+
expect(starts()).toBe(1)
61+
62+
rmSync(bin, { recursive: true })
63+
}, 30_000)

‎packages/tui/test/prompt/voice.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import {
77
buildTranscribeShellCommand,
88
ffmpegArgs,
99
findRecorder,
10+
isRepeatPress,
1011
shouldDiscardTap,
1112
soxArgs,
1213
transcribeAudio,
1314
VOICE_FILE_PLACEHOLDER,
1415
VOICE_MAX_RECORD_MS,
1516
VOICE_MIN_HOLD_MS,
17+
VOICE_REPEAT_GUARD_MS,
1618
} from "../../src/component/prompt/voice"
1719

1820
const originalPath = process.env.PATH
@@ -139,6 +141,30 @@ describe("component.prompt.voice", () => {
139141
})
140142
})
141143

144+
describe("isRepeatPress", () => {
145+
test("treats a press right after the previous one as key repeat", () => {
146+
expect(isRepeatPress(33)).toBe(true)
147+
expect(isRepeatPress(VOICE_REPEAT_GUARD_MS - 1)).toBe(true)
148+
})
149+
150+
test("treats a press after a quiet gap as a fresh press", () => {
151+
expect(isRepeatPress(VOICE_REPEAT_GUARD_MS)).toBe(false)
152+
expect(isRepeatPress(60_000)).toBe(false)
153+
})
154+
155+
test("honors the terminal's repeat marker regardless of timing", () => {
156+
expect(isRepeatPress(60_000, true)).toBe(true)
157+
expect(isRepeatPress(60_000, false)).toBe(false)
158+
})
159+
160+
test("a held key chains: each repeat restarts the window", () => {
161+
// First press at 0, OS initial delay 660ms, then repeats every 33ms.
162+
const presses = [0, 660, 693, 726, 759]
163+
const results = presses.map((now, index) => (index === 0 ? false : isRepeatPress(now - presses[index - 1]!)))
164+
expect(results).toEqual([false, true, true, true, true])
165+
})
166+
})
167+
142168
describe("shouldDiscardTap", () => {
143169
test("discards holds shorter than the minimum", () => {
144170
expect(shouldDiscardTap(VOICE_MIN_HOLD_MS - 1)).toBe(true)

‎services/www/src/docs/content/cli/config.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Configure mouse capture, scrolling, editor context, pastes, and prompt image pre
111111

112112
## Voice
113113

114-
Hold the `prompt.voice.push_to_talk` key to record from the microphone; releasing it (or pressing it again) transcribes the recording into the prompt. Recording needs `ffmpeg` or `sox` installed, and transcription runs your own shell command. Key release must reach the terminal (Kitty keyboard protocol with release reporting) or only the press half fires.
114+
Press the `prompt.voice.push_to_talk` key to start recording from the microphone, and press it again to stop and transcribe the recording into the prompt. Presses within a second of the previous one are treated as key repeat, so holding the key does not toggle. Recording needs `ffmpeg` or `sox` installed, and transcription runs your own shell command. OpenCode does not enable terminal key-release reporting, so releasing the key does not stop a recording today.
115115

116116
```json title="cli.json"
117117
{

‎services/www/src/docs/content/cli/keybinds.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ The retired `diff.toggle`, `diff.expand`, `diff.expand_all`, `diff.collapse`, an
279279
| `prompt.stash` | `none` | Stash prompt |
280280
| `prompt.stash.pop` | `none` | Pop stashed prompt |
281281
| `prompt.stash.list` | `none` | List stashed prompts |
282-
| `prompt.voice.push_to_talk` | `none` | Record voice input; release or press again to transcribe |
282+
| `prompt.voice.push_to_talk` | `none` | Press to record voice input, press again to transcribe |
283283

284284
## Input Editing
285285

0 commit comments

Comments
 (0)