Skip to content

Commit 3977fd0

Browse files
committed
feat(tui): stop voice recording on second press, report recorder failure
Review follow-up: without terminal release reporting the recording could not be stopped except by the 120s auto-stop, and a recorder that died immediately left the UI stuck showing recording. Pressing the key while recording now stops and transcribes (toggle fallback alongside hold-to-talk). VoiceRecorder.start takes an onFailure callback fired on spawn failure or spontaneous exit, and the prompt resets its state and shows an error toast.
1 parent 960975b commit 3977fd0

7 files changed

Lines changed: 68 additions & 11 deletions

File tree

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -950,13 +950,17 @@ export function Prompt(props: PromptProps) {
950950
}))
951951

952952
// Push-to-talk voice input: hold the configured key to record, release to transcribe.
953+
// Where the terminal never reports releases, pressing the key again stops instead.
953954
const [voiceRecording, setVoiceRecording] = createSignal(false)
954955
let voiceRecorder: VoiceRecorder | undefined
955956
let voiceStartTime = 0
956957
let voiceStopTimer: ReturnType<typeof setTimeout> | undefined
957958

958959
function startVoiceRecording() {
959-
if (voiceRecorder?.recording) return
960+
if (voiceRecorder?.recording) {
961+
void stopVoiceRecording("")
962+
return
963+
}
960964
if (!config.voice?.transcribe_command) {
961965
toast.show({ variant: "error", message: "Voice input needs voice.transcribe_command to be set" })
962966
return
@@ -967,7 +971,12 @@ export function Prompt(props: PromptProps) {
967971
return
968972
}
969973
voiceRecorder = new VoiceRecorder()
970-
voiceRecorder.start(spec)
974+
voiceRecorder.start(spec, () => {
975+
voiceRecorder?.dispose()
976+
voiceRecorder = undefined
977+
setVoiceRecording(false)
978+
toast.show({ variant: "error", message: "Voice recorder stopped unexpectedly" })
979+
})
971980
voiceStartTime = Date.now()
972981
setVoiceRecording(true)
973982
voiceStopTimer = setTimeout(() => void stopVoiceRecording(""), VOICE_MAX_RECORD_MS)
@@ -1013,7 +1022,7 @@ export function Prompt(props: PromptProps) {
10131022
bindings: [
10141023
...press.map((binding) => ({
10151024
...binding,
1016-
desc: "Start voice recording (release to transcribe)",
1025+
desc: "Start voice recording (release or press again to transcribe)",
10171026
group: "Prompt",
10181027
cmd: () => startVoiceRecording(),
10191028
})),
@@ -1682,7 +1691,7 @@ export function Prompt(props: PromptProps) {
16821691

16831692
const placeholderText = createMemo(() => {
16841693
const value = (() => {
1685-
if (voiceRecording()) return "Recording voice… release to transcribe"
1694+
if (voiceRecording()) return "Recording voice… release or press again to stop"
16861695
if (store.mode === "shell") {
16871696
if (!shell().length) return undefined
16881697
return `Run a command… "${shell()[store.placeholder % shell().length]}"`

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,21 @@ export class VoiceRecorder {
7474
return this.proc !== undefined
7575
}
7676

77-
start(spec: RecorderSpec): void {
77+
start(spec: RecorderSpec, onFailure?: () => void): void {
7878
if (this.proc) return
7979
const file = join(tmpdir(), `opencode-voice-${process.pid}-${Date.now()}.wav`)
8080
const proc = spawn(spec.command, spec.buildArgs(file), { stdio: "ignore" })
81-
proc.on("error", () => {
81+
const failed = () => {
8282
if (this.proc === proc) {
8383
this.proc = undefined
8484
this.file = undefined
85+
onFailure?.()
8586
}
86-
})
87+
}
88+
proc.on("error", failed)
89+
// A recorder that exits on its own (missing device, bad flags) failed;
90+
// normal stops clear this.proc first, so they never reach here.
91+
proc.on("exit", failed)
8792
this.proc = proc
8893
this.file = file
8994
}

‎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", "Hold to record voice input, release to transcribe into the prompt"),
203+
"prompt.voice.push_to_talk": keybind("none", "Record voice input; release or 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", "Hold to record voice input, release to transcribe into the prompt"),
173+
prompt_voice_push_to_talk: keybind("none", "Record voice input; release or 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"),

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,48 @@ describe("component.prompt.voice", () => {
186186
expect(recorder.recording).toBe(false)
187187
expect(await recorder.stop()).toBeUndefined()
188188
})
189+
190+
test("notifies onFailure when spawn fails", async () => {
191+
const recorder = new VoiceRecorder()
192+
let failures = 0
193+
recorder.start({ command: "opencode-voice-definitely-missing", buildArgs: (file) => [file] }, () => {
194+
failures += 1
195+
})
196+
await new Promise((resolve) => setTimeout(resolve, 200))
197+
expect(recorder.recording).toBe(false)
198+
expect(failures).toBe(1)
199+
})
200+
201+
test("notifies onFailure when the recorder exits on its own", async () => {
202+
const dir = fakeBinDir({ "fake-rec": "#!/bin/sh\nexit 3\n" })
203+
const recorder = new VoiceRecorder()
204+
let failures = 0
205+
recorder.start({ command: join(dir, "fake-rec"), buildArgs: (file) => [file] }, () => {
206+
failures += 1
207+
})
208+
const deadline = Date.now() + 5000
209+
while (recorder.recording && Date.now() < deadline) {
210+
await new Promise((resolve) => setTimeout(resolve, 50))
211+
}
212+
expect(recorder.recording).toBe(false)
213+
expect(failures).toBe(1)
214+
expect(await recorder.stop()).toBeUndefined()
215+
rmSync(dir, { recursive: true })
216+
})
217+
218+
test("does not notify onFailure on normal stop", async () => {
219+
const dir = fakeBinDir({ "fake-rec": fakeRecorder })
220+
const recorder = new VoiceRecorder()
221+
let failures = 0
222+
const before = listVoiceFiles()
223+
recorder.start({ command: join(dir, "fake-rec"), buildArgs: (file) => [file] }, () => {
224+
failures += 1
225+
})
226+
const file = await waitForNewVoiceFile(before).then(() => recorder.stop())
227+
expect(file).not.toBeUndefined()
228+
expect(failures).toBe(0)
229+
recorder.dispose()
230+
rmSync(dir, { recursive: true })
231+
})
189232
})
190233
})

‎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 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+
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.
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` | Hold to record voice input, release to transcribe into the prompt |
282+
| `prompt.voice.push_to_talk` | `none` | Record voice input; release or press again to transcribe |
283283

284284
## Input Editing
285285

0 commit comments

Comments
 (0)