feat(desktop): honest, unified dictation popups with Esc to cancel

Wave 3 of the 2026-09-28 design overhaul.

- Esc (no modifiers) cancels an in-progress dictation, recording or
  processing: VoiceModeService.requestEscape() queues the dead 'escape'
  action. Clicking the capsule cancels only while recording, so a stray
  click during processing no longer throws the text away.
- Errors show in the user's language: bootstrap now passes error.code to
  the tip (the translated branch never ran), and one core classifier
  (voice-error-message) feeds both the tip and the main-window alert.
- Capsule: bars stay flat without input (no fake "alive" wobble before the
  mic opens), an indeterminate bar replaces the made-up percentage, the
  partial transcript stays visible while processing, an Esc hint, and a
  brief "Inserted" state so success is visible.
- All six popups share popups/_shared (Pretendard, one panel radius,
  selection, kbd hints, reduced motion). History/command popups get
  titles, listbox roles and window sizes from popup-list-geometry (empty
  states were clipped); the command popup's "0" key is registered;
  result popup explains why it appeared and can be closed; captions keep
  a theme-independent dark backing on purpose.
- Popup themes follow the OS when set to system and the document lang
  follows the app language.
- Site hero demo mirrors the new capsule (done state, indeterminate bar,
  Esc hint; 10 locales).
This commit is contained in:
Yun Chan 2026-09-28 20:46:24 +09:00
parent f064c93197
commit 7b1210b5d1
41 changed files with 1565 additions and 1143 deletions

View file

@ -16,10 +16,8 @@
import { EventEmitter } from 'events'
import { randomUUID } from 'crypto'
import { writeFile, mkdir } from 'fs/promises'
import { join } from 'path'
import { app } from 'electron'
import { getLogger } from './LoggerService'
import { createFsRecordingStore, type RecordingStore } from './recording/recording-store'
import { getAudioCaptureService } from './AudioCaptureService'
import { getSTTManager } from './stt/STTManager'
import type { TranscriptionResult } from './stt/types'
@ -85,7 +83,8 @@ export interface VoiceModeEvents {
current: AudioState
}) => void
'transcription-update': (payload: { text: string; isFinal: boolean }) => void
'session-completed': (payload: { session: VoiceSession; finalText: string }) => void
/** audioPath: 저장된 녹음 WAV 경로(완료 이벤트 전에 저장됨). 오디오가 없거나 저장 실패면 null */
'session-completed': (payload: { session: VoiceSession; finalText: string; audioPath: string | null }) => void
'session-cancelled': (payload: {
session: VoiceSession
reason: 'user' | 'timeout' | 'too-short'
@ -196,6 +195,11 @@ const NOTHING_PENDING: StopOutcome = { finished: Promise.resolve() }
// ============================================================
class VoiceModeService extends EventEmitter {
/** 녹음 저장 포트 — 경로·fs·WAV 인코딩은 어댑터가 맡는다 (DIP) */
constructor(private readonly _recordings: RecordingStore = createFsRecordingStore()) {
super()
}
private _session: VoiceSession | null = null
private _run: SessionRun | null = null
private _recognitionState = RecognitionState.IDLE
@ -472,6 +476,22 @@ class VoiceModeService extends EventEmitter {
this._cancelSession('user')
}
/**
* 수정자 없는 Esc — 녹음·처리 중인 세션을 취소한다(2026-09-28: 녹음 팁의 "Esc 취소").
* 액션 큐로 넣어 누름/뗌과 순서를 지킨다. 진행 중인 세션이 없으면 아무것도 하지 않고 false.
*/
requestEscape(): boolean {
if (!this.isActive) return false
this._enqueueAction({
type: 'escape',
timestamp: Date.now(),
mode: 'dictation',
actionId: 'dictation',
holdMode: false,
})
return true
}
// ── 상태 머신 ──────────────────────────────────────────
private _isInTerminalState(): boolean {
@ -916,7 +936,7 @@ class VoiceModeService extends EventEmitter {
severity: 'warning',
})
}
void this._completeSession(run, effectiveText)
await this._completeSession(run, effectiveText)
} else {
await this._processWithLLM(run, effectiveText, route)
}
@ -985,7 +1005,7 @@ class VoiceModeService extends EventEmitter {
const chainResult = await getChainService().execute(route.chainId, userText)
if (!this._isCurrent(run)) return
if (this._session) this._session.processedText = chainResult.finalText
void this._completeSession(run, chainResult.finalText)
await this._completeSession(run, chainResult.finalText)
} catch (error) {
if (!this._isCurrent(run)) return
logger.warn(`Chain execution failed: ${describe(error)}`)
@ -1010,7 +1030,7 @@ class VoiceModeService extends EventEmitter {
this._session.processedText = processedText
}
void this._completeSession(run, processedText)
await this._completeSession(run, processedText)
} catch (error) {
if (!this._isCurrent(run)) return
this._handleError(
@ -1066,71 +1086,48 @@ class VoiceModeService extends EventEmitter {
// ── 세션 완료/취소 ─────────────────────────────────────
/**
* 세션 완료 — 녹음을 먼저 저장하고(실제 경로를 이벤트에 싣는다) 완료를 알린 뒤, 삽입은 따로 돈다.
*
* 예전엔 완료 이벤트(히스토리 생성 + history_audio 동기화 예약)가 먼저 나가고 WAV 는 텍스트 삽입이
* 끝난 뒤에 써졌다 — 그사이 flush 가 돌면 "파일 없음"을 성공으로 처리해 녹음이 영영 올라가지 않았다.
*/
private async _completeSession(run: SessionRun, finalText: string): Promise<void> {
if (!this._isCurrent(run) || !this._session) return
const audioPath = await this._recordings.save(this._session.id, run.lastAudio)
// 저장하는 사이 취소·새 세션이 시작됐으면 완료를 알리지 않는다.
if (!this._isCurrent(run) || !this._session) return
const session = { ...this._session }
const lastAudio = run.lastAudio
const selectionCapture = run.selectionCapture
logger.info(`Session completed: "${finalText.substring(0, 50)}${finalText.length > 50 ? '...' : ''}"`)
// Speakly 패턴: 먼저 IDLE 복귀 (다음 핫키 즉시 사용 가능). 표시 계층이 완료 이벤트로 팁을 숨긴다.
this._setRecognitionState(RecognitionState.COMPLETED)
this._setAudioState(AudioState.STOPPED)
this.emit('session-completed', { session, finalText })
this.emit('session-completed', { session, finalText, audioPath })
this._endRun(run)
// 텍스트 삽입은 세션과 무관하게 비동기 실행 (세션 블로킹 방지)
if (configGet('autoInsert') && finalText.length > 0) {
try {
// 선택 텍스트 캡처가 클립보드를 쓰는 중이면 끝날 때까지 기다린다 (클립보드 경합 방지)
if (selectionCapture) await selectionCapture
const insertMethod = configGet('insertMethod')
await getTextInsertService().insertText(finalText, insertMethod)
} catch (error) {
logger.warn(`Text insert failed: ${describe(error)}`)
// 삽입 실패 시 표시 계층이 ResultPopup에 텍스트를 보여 준다
this.emit('insert-failed', { text: finalText, sessionId: session.id })
}
}
// 녹음 오디오 WAV 파일 저장 (비동기, 실패해도 무시)
void this._saveAudioFile(session.id, lastAudio)
void this._insertCompletedText(session.id, finalText, selectionCapture)
}
/** PCM 버퍼를 WAV 파일로 저장 */
private async _saveAudioFile(sessionId: string, pcmData: Buffer | null): Promise<void> {
if (!pcmData || pcmData.length === 0) return
private async _insertCompletedText(
sessionId: string,
finalText: string,
selectionCapture: Promise<unknown> | null,
): Promise<void> {
if (!configGet('autoInsert') || finalText.length === 0) return
try {
const recordingsDir = join(app.getPath('userData'), 'recordings')
await mkdir(recordingsDir, { recursive: true })
const wavPath = join(recordingsDir, `${sessionId}.wav`)
// WAV 헤더 생성 (16kHz, 16bit, mono)
const header = Buffer.alloc(44)
const dataSize = pcmData.length
const fileSize = dataSize + 36
header.write('RIFF', 0)
header.writeUInt32LE(fileSize, 4)
header.write('WAVE', 8)
header.write('fmt ', 12)
header.writeUInt32LE(16, 16) // fmt chunk size
header.writeUInt16LE(1, 20) // PCM format
header.writeUInt16LE(1, 22) // mono
header.writeUInt32LE(16000, 24) // sample rate
header.writeUInt32LE(32000, 28) // byte rate (16000 * 2)
header.writeUInt16LE(2, 32) // block align
header.writeUInt16LE(16, 34) // bits per sample
header.write('data', 36)
header.writeUInt32LE(dataSize, 40)
await writeFile(wavPath, Buffer.concat([header, pcmData]))
logger.info(`Audio saved: ${wavPath} (${Math.round(dataSize / 1024)}KB)`)
// 선택 텍스트 캡처가 클립보드를 쓰는 중이면 끝날 때까지 기다린다 (클립보드 경합 방지)
if (selectionCapture) await selectionCapture
const insertMethod = configGet('insertMethod')
await getTextInsertService().insertText(finalText, insertMethod)
} catch (error) {
logger.warn(`Audio save failed: ${describe(error)}`)
logger.warn(`Text insert failed: ${describe(error)}`)
// 삽입 실패 시 표시 계층이 ResultPopup에 텍스트를 보여 준다
this.emit('insert-failed', { text: finalText, sessionId })
}
}
@ -1353,6 +1350,13 @@ export function getVoiceModeService(): VoiceModeService {
return instance
}
/** 테스트 전용: 가짜 녹음 저장소를 주입한 싱글톤을 만든다. */
export function createVoiceModeServiceForTests(recordings: RecordingStore): VoiceModeService {
resetVoiceModeServiceForTests()
instance = new VoiceModeService(recordings)
return instance
}
export function resetVoiceModeServiceForTests(): void {
if (instance) {
instance.removeAllListeners()