@@ -217,12 +217,23 @@ impl Completer for ChatCompleter {
217
217
pub struct ChatHinter {
218
218
/// Command history for providing suggestions based on past commands
219
219
history : Vec < String > ,
220
+ /// Whether history-based hints are enabled
221
+ history_hints_enabled : bool ,
220
222
}
221
223
222
224
impl ChatHinter {
223
225
/// Creates a new ChatHinter instance
224
- pub fn new ( ) -> Self {
225
- Self { history : Vec :: new ( ) }
226
+ pub fn new ( os : & Os ) -> Self {
227
+ let history_hints_enabled = os
228
+ . database
229
+ . settings
230
+ . get_bool ( Setting :: ChatEnableHistoryHints )
231
+ . unwrap_or ( false ) ;
232
+
233
+ Self {
234
+ history : Vec :: new ( ) ,
235
+ history_hints_enabled,
236
+ }
226
237
}
227
238
228
239
/// Updates the history with a new command
@@ -247,12 +258,16 @@ impl ChatHinter {
247
258
. map ( |cmd| cmd[ line. len ( ) ..] . to_string ( ) ) ;
248
259
}
249
260
250
- // Try to find a hint from history
251
- self . history
252
- . iter ( )
253
- . rev ( ) // Start from most recent
254
- . find ( |cmd| cmd. starts_with ( line) && cmd. len ( ) > line. len ( ) )
255
- . map ( |cmd| cmd[ line. len ( ) ..] . to_string ( ) )
261
+ // Try to find a hint from history, but only if history hints are enabled
262
+ if self . history_hints_enabled {
263
+ self . history
264
+ . iter ( )
265
+ . rev ( ) // Start from most recent
266
+ . find ( |cmd| cmd. starts_with ( line) && cmd. len ( ) > line. len ( ) )
267
+ . map ( |cmd| cmd[ line. len ( ) ..] . to_string ( ) )
268
+ } else {
269
+ None
270
+ }
256
271
}
257
272
}
258
273
@@ -370,7 +385,7 @@ pub fn rl(
370
385
. build ( ) ;
371
386
let h = ChatHelper {
372
387
completer : ChatCompleter :: new ( sender, receiver) ,
373
- hinter : ChatHinter :: new ( ) ,
388
+ hinter : ChatHinter :: new ( os ) ,
374
389
validator : MultiLineValidator ,
375
390
} ;
376
391
let mut rl = Editor :: with_config ( config) ?;
@@ -403,6 +418,23 @@ mod tests {
403
418
use rustyline:: highlight:: Highlighter ;
404
419
405
420
use super :: * ;
421
+
422
+ // Helper function to create a mock Os for testing
423
+ fn create_mock_os ( ) -> Os {
424
+ use crate :: database:: settings:: Settings ;
425
+
426
+ Os {
427
+ env : crate :: os:: Env :: new ( ) ,
428
+ fs : crate :: os:: Fs :: new ( ) ,
429
+ sysinfo : crate :: os:: SysInfo :: new ( ) ,
430
+ database : crate :: database:: Database {
431
+ settings : Settings :: default ( ) ,
432
+ } ,
433
+ client : crate :: api_client:: ApiClient :: default ( ) ,
434
+ telemetry : crate :: telemetry:: TelemetryThread :: default ( ) ,
435
+ }
436
+ }
437
+
406
438
#[ test]
407
439
fn test_chat_completer_command_completion ( ) {
408
440
let ( prompt_request_sender, _) = std:: sync:: mpsc:: channel :: < Option < String > > ( ) ;
@@ -448,9 +480,10 @@ mod tests {
448
480
fn test_highlight_prompt_basic ( ) {
449
481
let ( prompt_request_sender, _) = std:: sync:: mpsc:: channel :: < Option < String > > ( ) ;
450
482
let ( _, prompt_response_receiver) = std:: sync:: mpsc:: channel :: < Vec < String > > ( ) ;
483
+ let mock_os = create_mock_os ( ) ;
451
484
let helper = ChatHelper {
452
485
completer : ChatCompleter :: new ( prompt_request_sender, prompt_response_receiver) ,
453
- hinter : ChatHinter :: new ( ) ,
486
+ hinter : ChatHinter :: new ( & mock_os ) ,
454
487
validator : MultiLineValidator ,
455
488
} ;
456
489
@@ -464,9 +497,10 @@ mod tests {
464
497
fn test_highlight_prompt_with_warning ( ) {
465
498
let ( prompt_request_sender, _) = std:: sync:: mpsc:: channel :: < Option < String > > ( ) ;
466
499
let ( _, prompt_response_receiver) = std:: sync:: mpsc:: channel :: < Vec < String > > ( ) ;
500
+ let mock_os = create_mock_os ( ) ;
467
501
let helper = ChatHelper {
468
502
completer : ChatCompleter :: new ( prompt_request_sender, prompt_response_receiver) ,
469
- hinter : ChatHinter :: new ( ) ,
503
+ hinter : ChatHinter :: new ( & mock_os ) ,
470
504
validator : MultiLineValidator ,
471
505
} ;
472
506
@@ -480,9 +514,10 @@ mod tests {
480
514
fn test_highlight_prompt_with_profile ( ) {
481
515
let ( prompt_request_sender, _) = std:: sync:: mpsc:: channel :: < Option < String > > ( ) ;
482
516
let ( _, prompt_response_receiver) = std:: sync:: mpsc:: channel :: < Vec < String > > ( ) ;
517
+ let mock_os = create_mock_os ( ) ;
483
518
let helper = ChatHelper {
484
519
completer : ChatCompleter :: new ( prompt_request_sender, prompt_response_receiver) ,
485
- hinter : ChatHinter :: new ( ) ,
520
+ hinter : ChatHinter :: new ( & mock_os ) ,
486
521
validator : MultiLineValidator ,
487
522
} ;
488
523
@@ -496,9 +531,10 @@ mod tests {
496
531
fn test_highlight_prompt_with_profile_and_warning ( ) {
497
532
let ( prompt_request_sender, _) = std:: sync:: mpsc:: channel :: < Option < String > > ( ) ;
498
533
let ( _, prompt_response_receiver) = std:: sync:: mpsc:: channel :: < Vec < String > > ( ) ;
534
+ let mock_os = create_mock_os ( ) ;
499
535
let helper = ChatHelper {
500
536
completer : ChatCompleter :: new ( prompt_request_sender, prompt_response_receiver) ,
501
- hinter : ChatHinter :: new ( ) ,
537
+ hinter : ChatHinter :: new ( & mock_os ) ,
502
538
validator : MultiLineValidator ,
503
539
} ;
504
540
@@ -515,9 +551,10 @@ mod tests {
515
551
fn test_highlight_prompt_invalid_format ( ) {
516
552
let ( prompt_request_sender, _) = std:: sync:: mpsc:: channel :: < Option < String > > ( ) ;
517
553
let ( _, prompt_response_receiver) = std:: sync:: mpsc:: channel :: < Vec < String > > ( ) ;
554
+ let mock_os = create_mock_os ( ) ;
518
555
let helper = ChatHelper {
519
556
completer : ChatCompleter :: new ( prompt_request_sender, prompt_response_receiver) ,
520
- hinter : ChatHinter :: new ( ) ,
557
+ hinter : ChatHinter :: new ( & mock_os ) ,
521
558
validator : MultiLineValidator ,
522
559
} ;
523
560
@@ -529,7 +566,8 @@ mod tests {
529
566
530
567
#[ test]
531
568
fn test_chat_hinter_command_hint ( ) {
532
- let hinter = ChatHinter :: new ( ) ;
569
+ let mock_os = create_mock_os ( ) ;
570
+ let hinter = ChatHinter :: new ( & mock_os) ;
533
571
534
572
// Test hint for a command
535
573
let line = "/he" ;
@@ -552,8 +590,15 @@ mod tests {
552
590
}
553
591
554
592
#[ test]
555
- fn test_chat_hinter_history_hint ( ) {
556
- let mut hinter = ChatHinter :: new ( ) ;
593
+ fn test_chat_hinter_history_hint_enabled ( ) {
594
+ let mut mock_os = create_mock_os ( ) ;
595
+ // Set the setting to true
596
+ mock_os. database . settings . 0 . insert (
597
+ Setting :: ChatEnableHistoryHints . as_ref ( ) . to_string ( ) ,
598
+ serde_json:: Value :: Bool ( true ) ,
599
+ ) ;
600
+
601
+ let mut hinter = ChatHinter :: new ( & mock_os) ;
557
602
558
603
// Add some history
559
604
hinter. update_history ( "Hello, world!" ) ;
@@ -568,4 +613,24 @@ mod tests {
568
613
let hint = hinter. hint ( line, pos, & ctx) ;
569
614
assert_eq ! ( hint, Some ( " are you?" . to_string( ) ) ) ;
570
615
}
616
+
617
+ #[ test]
618
+ fn test_chat_hinter_history_hint_disabled ( ) {
619
+ let mock_os = create_mock_os ( ) ;
620
+
621
+ let mut hinter = ChatHinter :: new ( & mock_os) ;
622
+
623
+ // Add some history
624
+ hinter. update_history ( "Hello, world!" ) ;
625
+ hinter. update_history ( "How are you?" ) ;
626
+
627
+ // Test hint from history when disabled
628
+ let line = "How" ;
629
+ let pos = line. len ( ) ;
630
+ let empty_history = DefaultHistory :: new ( ) ;
631
+ let ctx = Context :: new ( & empty_history) ;
632
+
633
+ let hint = hinter. hint ( line, pos, & ctx) ;
634
+ assert_eq ! ( hint, None ) ;
635
+ }
571
636
}
0 commit comments