@@ -373,11 +373,38 @@ export class WaveAIModel {
373373 } ) ;
374374 }
375375
376+ isValidMode ( mode : string ) : boolean {
377+ const telemetryEnabled = globalStore . get ( getSettingsKeyAtom ( "telemetry:enabled" ) ) ?? false ;
378+ if ( mode . startsWith ( "waveai@" ) && ! telemetryEnabled ) {
379+ return false ;
380+ }
381+
382+ const aiModeConfigs = globalStore . get ( this . aiModeConfigs ) ;
383+ if ( aiModeConfigs == null || ! ( mode in aiModeConfigs ) ) {
384+ return false ;
385+ }
386+
387+ return true ;
388+ }
389+
376390 setAIMode ( mode : string ) {
377- globalStore . set ( this . currentAIMode , mode ) ;
391+ if ( ! this . isValidMode ( mode ) ) {
392+ this . setAIModeToDefault ( ) ;
393+ } else {
394+ globalStore . set ( this . currentAIMode , mode ) ;
395+ RpcApi . SetRTInfoCommand ( TabRpcClient , {
396+ oref : this . orefContext ,
397+ data : { "waveai:mode" : mode } ,
398+ } ) ;
399+ }
400+ }
401+
402+ setAIModeToDefault ( ) {
403+ const defaultMode = globalStore . get ( this . defaultModeAtom ) ;
404+ globalStore . set ( this . currentAIMode , defaultMode ) ;
378405 RpcApi . SetRTInfoCommand ( TabRpcClient , {
379406 oref : this . orefContext ,
380- data : { "waveai:mode" : mode } ,
407+ data : { "waveai:mode" : null } ,
381408 } ) ;
382409 }
383410
@@ -386,34 +413,11 @@ export class WaveAIModel {
386413 oref : this . orefContext ,
387414 } ) ;
388415 const mode = rtInfo ?. [ "waveai:mode" ] ;
389-
390416 if ( mode == null ) {
391417 return ;
392418 }
393-
394- let shouldClear = false ;
395-
396- if ( mode . startsWith ( "waveai@" ) ) {
397- const telemetryEnabled = globalStore . get ( getSettingsKeyAtom ( "telemetry:enabled" ) ) ?? false ;
398- if ( ! telemetryEnabled ) {
399- shouldClear = true ;
400- }
401- }
402-
403- if ( ! shouldClear ) {
404- const aiModeConfigs = globalStore . get ( this . aiModeConfigs ) ;
405- if ( aiModeConfigs == null || ! ( mode in aiModeConfigs ) ) {
406- shouldClear = true ;
407- }
408- }
409-
410- if ( shouldClear ) {
411- const defaultMode = globalStore . get ( this . defaultModeAtom ) ;
412- globalStore . set ( this . currentAIMode , defaultMode ) ;
413- RpcApi . SetRTInfoCommand ( TabRpcClient , {
414- oref : this . orefContext ,
415- data : { "waveai:mode" : null } ,
416- } ) ;
419+ if ( ! this . isValidMode ( mode ) ) {
420+ this . setAIModeToDefault ( ) ;
417421 }
418422 }
419423
@@ -431,9 +435,15 @@ export class WaveAIModel {
431435 }
432436 globalStore . set ( this . chatId , chatIdValue ) ;
433437
434- const defaultMode = globalStore . get ( getSettingsKeyAtom ( "waveai:defaultmode" ) ) ?? "waveai@balanced" ;
435- const aiModeValue = rtInfo ?. [ "waveai:mode" ] ?? defaultMode ;
436- globalStore . set ( this . currentAIMode , aiModeValue ) ;
438+ const aiModeValue = rtInfo ?. [ "waveai:mode" ] ;
439+ if ( aiModeValue == null ) {
440+ const defaultMode = globalStore . get ( this . defaultModeAtom ) ;
441+ globalStore . set ( this . currentAIMode , defaultMode ) ;
442+ } else if ( this . isValidMode ( aiModeValue ) ) {
443+ globalStore . set ( this . currentAIMode , aiModeValue ) ;
444+ } else {
445+ this . setAIModeToDefault ( ) ;
446+ }
437447
438448 try {
439449 const chatData = await RpcApi . GetWaveAIChatCommand ( TabRpcClient , { chatid : chatIdValue } ) ;
0 commit comments