11import { runDialogueTurn } from '../dialogue/dialogueOrchestrator' ;
22import { loggers } from '../../lib/logger' ;
3- import { processVoiceCommand } from './voiceCommandProcessor ' ;
3+ import { VoiceCommandExecutor } from '../voiceCommand ' ;
44
55const logger = loggers . audio ;
66
@@ -330,8 +330,8 @@ export class ASRService {
330330 private onResultCallback : ( ( text : string ) => void ) | null = null ;
331331 private mode : 'command' | 'dictation' = 'command' ;
332332 private pendingRestartTimer : ReturnType < typeof setTimeout > | null = null ;
333- private presetTimers : ReturnType < typeof setTimeout > [ ] = [ ] ;
334333 private recognitionGeneration = 0 ;
334+ private voiceCommandExecutor : VoiceCommandExecutor ;
335335
336336 constructor ( config : ASRConfig = { } , state : ASRStateAdapter , tts : TTSService ) {
337337 this . isSupportedFlag =
@@ -346,6 +346,32 @@ export class ASRService {
346346 this . state = state ;
347347 this . tts = tts ;
348348
349+ // Initialize voice command executor
350+ this . voiceCommandExecutor = new VoiceCommandExecutor ( {
351+ systemControls : {
352+ play : ( ) => this . state . play ( ) ,
353+ pause : ( ) => this . state . pause ( ) ,
354+ reset : ( ) => this . state . reset ( ) ,
355+ setMuted : ( m ) => this . state . setMuted ( m ) ,
356+ } ,
357+ avatarControls : {
358+ setEmotion : ( e ) => this . state . setEmotion ( e ) ,
359+ setExpression : ( e ) => this . state . setExpression ( e ) ,
360+ setAnimation : ( a ) => this . state . setAnimation ( a ) ,
361+ setBehavior : ( b ) => this . state . setBehavior ( b ) ,
362+ speak : ( text ) => {
363+ void this . tts . speak ( text ) . catch ( ( err : unknown ) => {
364+ logger . warn ( 'Speech failed:' , err instanceof Error ? err . message : err ) ;
365+ } ) ;
366+ } ,
367+ } ,
368+ onUnhandled : async ( text : string ) => {
369+ if ( this . sendToBackend ) {
370+ await this . sendToDialogueService ( text ) ;
371+ }
372+ } ,
373+ } ) ;
374+
349375 if ( this . isSupportedFlag && typeof window !== 'undefined' ) {
350376 this . initRecognition ( ) ;
351377 }
@@ -462,12 +488,6 @@ export class ASRService {
462488 return errorMessages [ error ] || `语音识别失败: ${ error } ` ;
463489 }
464490
465- private speakSafely ( text : string ) : void {
466- void this . tts . speak ( text ) . catch ( ( error : unknown ) => {
467- logger . warn ( 'Speech failed safely:' , error instanceof Error ? error . message : error ) ;
468- } ) ;
469- }
470-
471491 start ( options ?: ASRStartOptions ) : boolean {
472492 if ( ! this . isSupportedFlag ) {
473493 logger . warn ( '浏览器不支持语音识别' ) ;
@@ -516,7 +536,7 @@ export class ASRService {
516536 }
517537
518538 stop ( ) : void {
519- this . clearPresetTimers ( ) ;
539+ this . voiceCommandExecutor . abort ( ) ;
520540 if ( this . pendingRestartTimer ) {
521541 clearTimeout ( this . pendingRestartTimer ) ;
522542 this . pendingRestartTimer = null ;
@@ -541,7 +561,7 @@ export class ASRService {
541561 */
542562 dispose ( ) : void {
543563 // Clear all preset timers first to prevent memory leaks
544- this . clearPresetTimers ( ) ;
564+ this . voiceCommandExecutor . abort ( ) ;
545565
546566 // Stop recognition and cleanup
547567 this . stop ( ) ;
@@ -562,7 +582,7 @@ export class ASRService {
562582 }
563583
564584 abort ( ) : void {
565- this . clearPresetTimers ( ) ;
585+ this . voiceCommandExecutor . abort ( ) ;
566586 if ( this . pendingRestartTimer ) {
567587 clearTimeout ( this . pendingRestartTimer ) ;
568588 this . pendingRestartTimer = null ;
@@ -578,29 +598,9 @@ export class ASRService {
578598 this . state . setBehavior ( 'idle' ) ;
579599 }
580600
581- // 处理语音输入 - 整合本地命令和后端对话
601+ // 处理语音输入 - 使用 VoiceCommandExecutor
582602 private async processVoiceInput ( text : string ) : Promise < void > {
583- // 首先检查是否是本地命令
584- const isLocalCommand = processVoiceCommand (
585- text ,
586- {
587- play : ( ) => this . state . play ( ) ,
588- pause : ( ) => this . state . pause ( ) ,
589- reset : ( ) => this . state . reset ( ) ,
590- setMuted : ( m ) => this . state . setMuted ( m ) ,
591- } ,
592- {
593- greeting : ( ) => this . performGreeting ( ) ,
594- dance : ( ) => this . performDance ( ) ,
595- nod : ( ) => this . performNod ( ) ,
596- shakeHead : ( ) => this . performShakeHead ( ) ,
597- } ,
598- ) ;
599-
600- // 如果不是本地命令且启用了后端发送,则发送到对话服务
601- if ( ! isLocalCommand && this . sendToBackend ) {
602- await this . sendToDialogueService ( text ) ;
603- }
603+ this . voiceCommandExecutor . execute ( text ) ;
604604 }
605605
606606 // 发送到对话服务
@@ -629,68 +629,23 @@ export class ASRService {
629629 }
630630 }
631631
632- private clearPresetTimers ( ) : void {
633- this . presetTimers . forEach ( clearTimeout ) ;
634- this . presetTimers = [ ] ;
635- }
636-
637- private schedulePresetReset ( fn : ( ) => void , delay : number ) : void {
638- this . presetTimers . push ( setTimeout ( fn , delay ) ) ;
639- }
640-
641- // 预设动作:打招呼
632+ // 预设动作:打招呼(委托给 VoiceCommandExecutor)
642633 performGreeting ( ) : void {
643- this . state . setEmotion ( 'happy' ) ;
644- this . state . setExpression ( 'smile' ) ;
645- this . state . setBehavior ( 'greeting' ) ;
646- this . state . setAnimation ( 'wave' ) ;
647-
648- this . speakSafely ( '您好!很高兴见到您!有什么可以帮助您的吗?' ) ;
649-
650- this . schedulePresetReset ( ( ) => {
651- this . state . setEmotion ( 'neutral' ) ;
652- this . state . setExpression ( 'neutral' ) ;
653- this . state . setBehavior ( 'idle' ) ;
654- this . state . setAnimation ( 'idle' ) ;
655- } , 4000 ) ;
634+ this . voiceCommandExecutor . presetActions . greeting ( ) ;
656635 }
657636
658- // 预设动作:跳舞
637+ // 预设动作:跳舞(委托给 VoiceCommandExecutor)
659638 performDance ( ) : void {
660- this . state . setAnimation ( 'dance' ) ;
661- this . state . setBehavior ( 'excited' ) ;
662- this . state . setEmotion ( 'happy' ) ;
663-
664- this . speakSafely ( '让我为您跳一支舞!' ) ;
665-
666- this . schedulePresetReset ( ( ) => {
667- this . state . setAnimation ( 'idle' ) ;
668- this . state . setBehavior ( 'idle' ) ;
669- this . state . setEmotion ( 'neutral' ) ;
670- } , 6000 ) ;
639+ this . voiceCommandExecutor . presetActions . dance ( ) ;
671640 }
672641
673- // 预设动作:点头
642+ // 预设动作:点头(委托给 VoiceCommandExecutor)
674643 performNod ( ) : void {
675- this . state . setAnimation ( 'nod' ) ;
676- this . state . setBehavior ( 'listening' ) ;
677-
678- this . speakSafely ( '好的,我明白了。' ) ;
679-
680- this . schedulePresetReset ( ( ) => {
681- this . state . setAnimation ( 'idle' ) ;
682- this . state . setBehavior ( 'idle' ) ;
683- } , 2000 ) ;
644+ this . voiceCommandExecutor . presetActions . nod ( ) ;
684645 }
685646
686- // 预设动作:摇头
647+ // 预设动作:摇头(委托给 VoiceCommandExecutor)
687648 performShakeHead ( ) : void {
688- this . state . setAnimation ( 'shakeHead' ) ;
689-
690- this . speakSafely ( '不太确定呢。' ) ;
691-
692- this . schedulePresetReset ( ( ) => {
693- this . state . setAnimation ( 'idle' ) ;
694- } , 2000 ) ;
649+ this . voiceCommandExecutor . presetActions . shakeHead ( ) ;
695650 }
696651}
0 commit comments