Skip to content

Commit d570acd

Browse files
committed
fix(tui): ignore key repeats and re-entry in voice push-to-talk
Review follow-up: holding the key fired the press binding on every key repeat, toggling the recorder on and off; presses during device enumeration raced past the recording check. Key repeats are now ignored via the event type, and a starting flag guards the enumeration window. The fake test recorder backgrounds its sleep so the INT trap fires immediately.
1 parent 81c2065 commit d570acd

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {
66
PasteEvent,
77
decodePasteBytes,
88
type KeyEvent,
9+
type Renderable,
910
} from "@opentui/core"
11+
import type { CommandContext } from "@opentui/keymap"
1012
import { createEffect, createMemo, onMount, createSignal, onCleanup, on, Show, Switch, Match, For } from "solid-js"
1113
import path from "path"
1214
import { useLocal } from "../../context/local"
@@ -955,17 +957,25 @@ export function Prompt(props: PromptProps) {
955957
let voiceRecorder: VoiceRecorder | undefined
956958
let voiceStartTime = 0
957959
let voiceStopTimer: ReturnType<typeof setTimeout> | undefined
960+
let voiceStarting = false
958961

959-
async function startVoiceRecording() {
962+
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
960966
if (voiceRecorder?.recording) {
961967
void stopVoiceRecording("")
962968
return
963969
}
970+
if (voiceStarting) return
964971
if (!config.voice?.transcribe_command) {
965972
toast.show({ variant: "error", message: "Voice input needs voice.transcribe_command to be set" })
966973
return
967974
}
975+
// findRecorder awaits device enumeration on Windows; guard re-entry.
976+
voiceStarting = true
968977
const spec = await findRecorder()
978+
voiceStarting = false
969979
if (!spec) {
970980
toast.show({ variant: "error", message: "Voice input needs ffmpeg or sox installed to record audio" })
971981
return
@@ -1025,7 +1035,7 @@ export function Prompt(props: PromptProps) {
10251035
...binding,
10261036
desc: "Start voice recording (release or press again to transcribe)",
10271037
group: "Prompt",
1028-
cmd: () => void startVoiceRecording(),
1038+
cmd: (context: CommandContext<Renderable, KeyEvent>) => void startVoiceRecording(context.event),
10291039
})),
10301040
// Releasing the same key stops the recording, so one configured key
10311041
// covers both halves of push-to-talk without extra configuration.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,10 @@ describe("component.prompt.voice", () => {
152152
})
153153

154154
describe("VoiceRecorder", () => {
155-
// Exits cleanly on SIGINT so stop() resolves without the SIGKILL fallback.
156-
const fakeRecorder = "#!/bin/sh\ntrap 'exit 0' INT\ntouch \"$1\"\nsleep 30\n"
155+
// Foreground sleep would delay the INT trap until it finishes; backgrounding
156+
// it lets the trap exit immediately so stop() resolves without the SIGKILL
157+
// fallback.
158+
const fakeRecorder = "#!/bin/sh\ntrap 'exit 0' INT\ntouch \"$1\"\nsleep 30 & wait\n"
157159

158160
function listVoiceFiles(dir: string): string[] {
159161
return readdirSync(dir).filter((name) => name.startsWith("opencode-voice-"))

0 commit comments

Comments
 (0)