// src/main/services/voice-intent.ts // 키 제스처 → 음성 세션 의도(start/stop/ignore) 매핑 정책. 순수 함수만 둔다 (서비스·IO 의존 없음). // // 예전엔 이 규칙이 VoiceModeService 의 _handlePress / _handleRelease / _resolveMode / // _resolveHoldMode 에 흩어져 있었고, '처리 중(processing)' 이라는 단계가 코드에 드러나지 않아 // 액션 큐의 직렬화 순서에 암묵적으로 기댔다. 정지가 전사·LLM 전체를 await 하는 동안 큐에 쌓인 // 입력이 세션이 끝난 뒤 다른 의미로 재생됐다(더블탭 정지의 두 번째 탭이 새 핸즈프리를 시작). // 이제 단계를 명시적으로 받아 의도를 돌려준다. 처리 중의 토글·누름은 'ignore' 다. // // 남는 한계: 처리가 더블탭 간격(TIMING.DOUBLE_PRESS_DURATION)보다 빨리 끝나 세션이 이미 idle 이면 // 두 번째 탭은 여전히 새 핸즈프리 세션을 시작한다. 의도 판단에 시간 정보가 필요한 경우로, 이 모듈의 // 범위 밖이다. import type { KeyBindingActionId, VoiceMode } from '@d3ro/core/types' /** 의도 판단에 쓰는 세션 단계 */ export type VoiceIntentPhase = 'idle' | 'recording' | 'processing' export interface VoiceGesture { type: 'press' | 'release' mode: VoiceMode /** hold-to-talk 여부 — release 에서 세션을 끊을지 결정한다 */ holdMode: boolean } export type VoiceIntent = | { kind: 'start'; mode: VoiceMode } | { kind: 'stop' } | { kind: 'ignore' } const IGNORE: VoiceIntent = { kind: 'ignore' } const STOP: VoiceIntent = { kind: 'stop' } /** * 세션 상태에서 의도 판단용 단계를 도출한다. 별도 상태 필드를 두지 않는다. * @param active 세션이 살아 있다 (비터미널) * @param stopRequested 이 세션에 정지가 이미 요청됐다 (오디오 해제 이후 = 전사/LLM 처리 중) */ export function deriveVoiceIntentPhase(active: boolean, stopRequested: boolean): VoiceIntentPhase { if (!active) return 'idle' return stopRequested ? 'processing' : 'recording' } /** * 키 제스처를 세션 의도로 바꾼다. * * | 제스처 | idle | recording | processing | * |---------------------------------|------------------|-----------|------------| * | press dictation | start dictation | ignore | ignore | * | press hands-free (토글) | start hands-free | stop | ignore | * | release dictation + holdMode | ignore | stop | ignore | * | release 그 외 (토글의 release) | ignore | ignore | ignore | * * 핸즈프리 세션도 dictation release(holdMode) 한 번으로 멈춘다 — 기본 바인딩에서 더블탭 정지의 * 첫 탭이 이 경로다(두 번째 탭은 processing 에서 ignore). */ export function resolveVoiceIntent(gesture: VoiceGesture, phase: VoiceIntentPhase): VoiceIntent { if (phase === 'processing') return IGNORE if (gesture.type === 'press') { if (gesture.mode === 'dictation') { if (!gesture.holdMode) { return phase === 'idle' ? { kind: 'start', mode: 'dictation' } : STOP } return phase === 'idle' ? { kind: 'start', mode: 'dictation' } : IGNORE } return phase === 'idle' ? { kind: 'start', mode: 'hands-free' } : STOP } if (gesture.holdMode && gesture.mode === 'dictation' && phase === 'recording') return STOP return IGNORE } /** 키바인딩 액션 → 세션 모드. 더블탭은 항상 핸즈프리다. */ export function resolveGestureMode(actionId: KeyBindingActionId, isDoublePress: boolean): VoiceMode { if (actionId === 'hands-free' || isDoublePress) return 'hands-free' return 'dictation' } /** * 키바인딩 스펙의 holdMode 설정을 그대로 존중한다. * 'command' 액션도 스펙/설정에 따라 토글(holdMode=false) 또는 홀드(holdMode=true)로 동작한다. */ export function resolveGestureHoldMode(_actionId: KeyBindingActionId, specHoldMode: boolean): boolean { return specHoldMode }