1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
use std::time::Instant;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use crate::app::TuiApp;
use crate::types::{Activity, ApproveMode, AskOption, Message, Role, SubmitMode};
impl TuiApp {
pub(crate) fn on_key(&mut self, k: KeyEvent) {
// On macOS, KeyEventKind may not always be reported correctly by some terminals
// (e.g., iTerm2, Terminal.app). We only filter out Release events to allow
// Press and Repeat events to be processed.
#[cfg(target_os = "macos")]
if k.kind == KeyEventKind::Release {
return;
}
#[cfg(not(target_os = "macos"))]
if k.kind != KeyEventKind::Press {
return;
}
match k.code {
// Enter: send or newline
KeyCode::Enter => {
if k.modifiers.contains(KeyModifiers::SHIFT) {
// Shift+Enter: insert newline at cursor position
self.ensure_char_boundary();
self.input.insert(self.cursor_pos, '\n');
self.cursor_pos += 1; // '\n' is 1 byte
} else if self.activity == Activity::Asking && self.waiting_for_ask {
// Handle ask confirmation or toggle selection in multi-select
self.handle_ask_enter();
} else if !self.input.trim().is_empty() {
self.send_input();
}
}
// Tab: switch between multiple questions or toggle approve mode
KeyCode::Tab if !k.modifiers.contains(KeyModifiers::SHIFT) => {
if self.activity == Activity::Asking
&& self.waiting_for_ask
&& self.ask_questions.len() > 1
{
self.switch_to_next_question();
}
}
// Escape: interrupt or clear input
KeyCode::Esc => {
// If in "Other" input mode, return to selection mode
if self.ask_other_input_active {
self.ask_other_input_active = false;
self.input.clear();
self.cursor_pos = 0;
// Uncheck the "Other" option if it was checked
for opt in &mut self.ask_options {
if opt.is_other {
opt.selected = false;
}
}
return;
}
if self.activity == Activity::Asking {
// Abort approval request
self.waiting_for_ask = false;
self.activity = Activity::Idle;
self.push_message(Message {
role: Role::System,
content: "⚠️ 已取消".into(),
});
if let Some(ask_tx) = &self.ask_tx {
ask_tx.try_send("abort".to_string()).ok();
}
} else if self.activity != Activity::Idle {
// Signal cancellation - backend will respond with Error event
// The events.rs handler will then process queue
self.cancel.cancel();
self.push_message(Message {
role: Role::System,
content: "⚡ 正在中断...".into(),
});
} else {
self.input.clear();
self.cursor_pos = 0;
}
}
// Ctrl+C: interrupt
KeyCode::Char('c') if k.modifiers.contains(KeyModifiers::CONTROL) => {
if self.activity != Activity::Idle {
self.cancel.cancel();
self.push_message(Message {
role: Role::System,
content: "⚡ 正在中断...".into(),
});
}
}
// Ctrl+D: exit
KeyCode::Char('d') if k.modifiers.contains(KeyModifiers::CONTROL) => {
self.exit = true;
}
// Ctrl+V: paste from clipboard
KeyCode::Char('v') if k.modifiers.contains(KeyModifiers::CONTROL) => {
// Try to get text from clipboard
if let Ok(mut clipboard) = arboard::Clipboard::new()
&& let Ok(text) = clipboard.get_text()
{
self.on_paste(&text);
}
}
// Backspace: delete char before cursor
KeyCode::Backspace => {
if self.cursor_pos > 0 {
let prev_pos = self.prev_char_boundary();
self.input.drain(prev_pos..self.cursor_pos);
self.cursor_pos = prev_pos;
}
}
// Delete: delete char at cursor
KeyCode::Delete => {
if self.cursor_pos < self.input.len() {
let next_pos = self.next_char_boundary();
self.input.drain(self.cursor_pos..next_pos);
}
}
// Space: toggle selection in multi-select mode or insert space
KeyCode::Char(' ')
if !k.modifiers.contains(KeyModifiers::ALT)
&& !k.modifiers.contains(KeyModifiers::CONTROL) =>
{
if self.activity == Activity::Asking
&& self.waiting_for_ask
&& self.ask_multi_select
&& !self.ask_options.is_empty()
&& !self.ask_other_input_active
{
// Toggle current selection (only when not in Other input mode)
self.toggle_ask_selection();
} else {
// Normal input: insert space
self.ensure_char_boundary();
self.input.insert(self.cursor_pos, ' ');
self.cursor_pos += 1;
if self.history_index.is_some() {
self.history_index = None;
self.history_draft.clear();
}
}
}
// Left arrow: move cursor left (one character)
KeyCode::Left => {
if self.cursor_pos > 0 {
self.cursor_pos = self.prev_char_boundary();
}
}
// Right arrow: move cursor right (one character)
KeyCode::Right => {
if self.cursor_pos < self.input.len() {
self.cursor_pos = self.next_char_boundary();
}
}
// Up arrow: ask selection, history navigation, or multiline cursor
KeyCode::Up if !k.modifiers.contains(KeyModifiers::ALT) => {
// If in "Other" input mode, allow multiline navigation
if self.ask_other_input_active && self.input.contains('\n') {
let (current_line_num, col_chars, _) = self.get_line_info();
if current_line_num > 1 {
let char_pos = self.byte_pos_to_char_pos();
let input_chars: Vec<char> = self.input.chars().collect();
let before_cursor_str: String = input_chars
[..char_pos.min(input_chars.len())]
.iter()
.collect();
// Previous line is before the last '\n' in before_cursor_str
let prev_lines_str =
&before_cursor_str[..before_cursor_str.rfind('\n').unwrap_or(0)];
let prev_line_start_char = prev_lines_str.chars().count();
// Find previous line length
let prev_line_end_char =
char_pos.saturating_sub(col_chars).saturating_sub(1); // -1 for the newline
let prev_line_len_chars =
prev_line_end_char.saturating_sub(prev_line_start_char);
// Move to same column (or end if shorter)
let target_char_pos =
prev_line_start_char + col_chars.min(prev_line_len_chars);
self.cursor_pos = self.char_pos_to_byte_pos(target_char_pos);
}
} else if self.activity == Activity::Asking
&& self.waiting_for_ask
&& !self.ask_options.is_empty()
&& !self.ask_other_input_active
{
// Ask selection (only when not in Other input mode)
if self.ask_selected_index > 0 {
self.ask_selected_index -= 1;
}
} else if self.input.contains('\n') {
let (current_line_num, col_chars, _) = self.get_line_info();
if current_line_num > 1 {
let char_pos = self.byte_pos_to_char_pos();
let input_chars: Vec<char> = self.input.chars().collect();
let before_cursor_str: String = input_chars
[..char_pos.min(input_chars.len())]
.iter()
.collect();
// Previous line is before the last '\n' in before_cursor_str
let prev_lines_str =
&before_cursor_str[..before_cursor_str.rfind('\n').unwrap_or(0)];
let prev_line_start_char = prev_lines_str.chars().count();
// Find previous line length
let prev_line_end_char =
char_pos.saturating_sub(col_chars).saturating_sub(1); // -1 for the newline
let prev_line_len_chars =
prev_line_end_char.saturating_sub(prev_line_start_char);
// Move to same column (or end if shorter)
let target_char_pos =
prev_line_start_char + col_chars.min(prev_line_len_chars);
self.cursor_pos = self.char_pos_to_byte_pos(target_char_pos);
}
} else if !self.input_history.is_empty() {
// Single-line: browse history
match self.history_index {
None => {
// Entering history mode: save current input as draft
self.history_draft = self.input.clone();
self.history_index = Some(self.input_history.len() - 1);
self.input = self.input_history[self.input_history.len() - 1].clone();
}
Some(idx) if idx > 0 => {
self.history_index = Some(idx - 1);
self.input = self.input_history[idx - 1].clone();
}
_ => {} // Already at oldest entry
}
self.cursor_pos = self.input.len();
}
}
// Down arrow: ask selection, history navigation, or multiline cursor
KeyCode::Down if !k.modifiers.contains(KeyModifiers::ALT) => {
// If in "Other" input mode, allow multiline navigation
if self.ask_other_input_active && self.input.contains('\n') {
let (current_line_num, col_chars, total_lines) = self.get_line_info();
if current_line_num < total_lines {
let char_pos = self.byte_pos_to_char_pos();
let input_chars: Vec<char> = self.input.chars().collect();
// Boundary check: char_pos must not exceed input_chars.len()
let safe_char_pos = char_pos.min(input_chars.len());
// Find next line start
let remaining_chars = &input_chars[safe_char_pos..];
let next_line_start_char = remaining_chars
.iter()
.position(|c| *c == '\n')
.map(|i| safe_char_pos + i + 1)
.unwrap_or_else(|| input_chars.len());
// Find next line end
let next_line_chars = &input_chars[next_line_start_char..];
let next_line_end_char = next_line_chars
.iter()
.position(|c| *c == '\n')
.map(|i| next_line_start_char + i)
.unwrap_or_else(|| input_chars.len());
let next_line_len_chars =
next_line_end_char.saturating_sub(next_line_start_char);
// Move to same column (or end if shorter)
let target_char_pos =
next_line_start_char + col_chars.min(next_line_len_chars);
self.cursor_pos = self.char_pos_to_byte_pos(target_char_pos);
}
} else if self.activity == Activity::Asking
&& self.waiting_for_ask
&& !self.ask_options.is_empty()
&& !self.ask_other_input_active
{
// Ask selection (only when not in Other input mode)
if self.ask_selected_index < self.ask_options.len() - 1 {
self.ask_selected_index += 1;
}
} else if self.input.contains('\n') {
let (current_line_num, col_chars, total_lines) = self.get_line_info();
if current_line_num < total_lines {
let char_pos = self.byte_pos_to_char_pos();
let input_chars: Vec<char> = self.input.chars().collect();
// Boundary check: char_pos must not exceed input_chars.len()
let safe_char_pos = char_pos.min(input_chars.len());
// Find next line start
let remaining_chars = &input_chars[safe_char_pos..];
let next_line_start_char = remaining_chars
.iter()
.position(|c| *c == '\n')
.map(|i| safe_char_pos + i + 1)
.unwrap_or_else(|| input_chars.len());
// Find next line end
let next_line_chars = &input_chars[next_line_start_char..];
let next_line_end_char = next_line_chars
.iter()
.position(|c| *c == '\n')
.map(|i| next_line_start_char + i)
.unwrap_or_else(|| input_chars.len());
let next_line_len_chars =
next_line_end_char.saturating_sub(next_line_start_char);
// Move to same column (or end if shorter)
let target_char_pos =
next_line_start_char + col_chars.min(next_line_len_chars);
self.cursor_pos = self.char_pos_to_byte_pos(target_char_pos);
}
} else if let Some(idx) = self.history_index {
// Single-line: browse history forward
if idx + 1 < self.input_history.len() {
self.history_index = Some(idx + 1);
self.input = self.input_history[idx + 1].clone();
} else {
// Back to draft (current unsent input)
self.history_index = None;
self.input = self.history_draft.clone();
self.history_draft.clear();
}
self.cursor_pos = self.input.len();
}
}
// Regular character input (except when Alt/Ctrl is held)
KeyCode::Char(c)
if !k.modifiers.contains(KeyModifiers::ALT)
&& !k.modifiers.contains(KeyModifiers::CONTROL) =>
{
self.ensure_char_boundary();
self.input.insert(self.cursor_pos, c);
self.cursor_pos += c.len_utf8();
// Exit history browsing mode on any character input
if self.history_index.is_some() {
self.history_index = None;
self.history_draft.clear();
}
}
// Alt+M: toggle approve mode
KeyCode::Char('m') if k.modifiers.contains(KeyModifiers::ALT) => {
self.approve_mode = self.approve_mode.next();
self.sync_approve_mode();
}
// Alt+T: toggle thinking collapse
KeyCode::Char('t') if k.modifiers.contains(KeyModifiers::ALT) => {
self.thinking_collapsed = !self.thinking_collapsed;
}
// Shift+Tab / BackTab: toggle approve mode
KeyCode::Tab if k.modifiers.contains(KeyModifiers::SHIFT) => {
self.approve_mode = self.approve_mode.next();
self.sync_approve_mode();
}
KeyCode::BackTab => {
self.approve_mode = self.approve_mode.next();
self.sync_approve_mode();
}
// Scroll: PageUp
KeyCode::PageUp => {
if self.auto_scroll {
self.auto_scroll = false;
// Set to max_scroll or at least 50 to start from bottom
self.scroll_offset = self.max_scroll.get().max(50);
}
self.scroll_offset = self.scroll_offset.saturating_sub(10);
}
// Scroll: PageDown
KeyCode::PageDown => {
if !self.auto_scroll {
self.scroll_offset = self.scroll_offset.saturating_add(10);
let max = self.max_scroll.get();
if max > 0 && self.scroll_offset >= max {
self.auto_scroll = true;
self.scroll_offset = 0;
}
}
}
// Scroll: Alt+Up (or Up when not idle)
KeyCode::Up if k.modifiers.contains(KeyModifiers::ALT) => {
if self.auto_scroll {
self.auto_scroll = false;
// Set to max_scroll or at least 50 to start from bottom
self.scroll_offset = self.max_scroll.get().max(50);
}
self.scroll_offset = self.scroll_offset.saturating_sub(1);
}
// Scroll: Alt+Down (or Down when not idle)
KeyCode::Down if k.modifiers.contains(KeyModifiers::ALT) => {
if !self.auto_scroll {
self.scroll_offset = self.scroll_offset.saturating_add(1);
let max = self.max_scroll.get();
if max > 0 && self.scroll_offset >= max {
self.auto_scroll = true;
self.scroll_offset = 0;
}
}
}
// Home: move cursor to start (if input has content) or scroll to top
KeyCode::Home => {
if !self.input.is_empty() {
self.cursor_pos = 0;
} else {
self.auto_scroll = false;
self.scroll_offset = 0;
}
}
// End: move cursor to end (if input has content) or scroll to bottom
KeyCode::End => {
if !self.input.is_empty() {
self.cursor_pos = self.input.len();
} else {
self.auto_scroll = true;
self.scroll_offset = 0;
self.new_message_while_scrolled.set(false); // Clear notification
}
}
_ => {}
}
}
// ============================================================================
// Unicode-safe cursor position helpers
// ============================================================================
/// Ensure cursor_pos is at a valid UTF-8 character boundary.
/// If not, move to the nearest valid boundary.
pub(crate) fn ensure_char_boundary(&mut self) {
if !self.input.is_char_boundary(self.cursor_pos) {
self.cursor_pos = self
.input
.char_indices()
.rfind(|(i, _)| *i <= self.cursor_pos)
.map(|(i, _)| i)
.unwrap_or(0);
}
}
/// Sync approve_mode to the shared atomic and notify agent task.
/// If switching to Auto and there's a pending approval, auto-approve it.
pub(crate) fn sync_approve_mode(&mut self) {
if let Some(ref shared) = self.shared_approve_mode {
shared.store(
self.approve_mode.to_u8(),
std::sync::atomic::Ordering::Relaxed,
);
}
// If switching to auto and agent is waiting for approval, auto-approve
if self.approve_mode == ApproveMode::Auto
&& self.waiting_for_ask
&& let Some(ref ask_tx) = self.ask_tx
{
ask_tx.try_send("y".to_string()).ok();
self.waiting_for_ask = false;
}
self.tx
.try_send(format!("/mode:{}", self.approve_mode))
.ok();
}
/// Find the byte position of the previous character boundary.
/// Returns 0 if cursor is at the start.
fn prev_char_boundary(&self) -> usize {
self.input
.char_indices()
.rfind(|(i, _)| *i < self.cursor_pos)
.map(|(i, _)| i)
.unwrap_or(0)
}
/// Find the byte position of the next character boundary.
/// Returns input.len() if cursor is at the end.
fn next_char_boundary(&self) -> usize {
self.input
.char_indices()
.find(|(i, _)| *i > self.cursor_pos)
.map(|(i, _)| i)
.unwrap_or_else(|| self.input.len())
}
/// Convert byte position to character position (count of chars before cursor).
fn byte_pos_to_char_pos(&self) -> usize {
self.input[..self.cursor_pos].chars().count()
}
/// Convert character position to byte position.
fn char_pos_to_byte_pos(&self, char_pos: usize) -> usize {
self.input
.char_indices()
.nth(char_pos)
.map(|(i, _)| i)
.unwrap_or_else(|| self.input.len())
}
/// Get current line info: (current_line_number, column_in_chars, total_lines)
fn get_line_info(&self) -> (usize, usize, usize) {
let before_cursor = &self.input[..self.cursor_pos];
let current_line_num = before_cursor.matches('\n').count() + 1;
let total_lines = self.input.lines().count().max(1);
let col_chars = before_cursor
.rfind('\n')
.map(|i| before_cursor[i + 1..].chars().count())
.unwrap_or_else(|| before_cursor.chars().count());
(current_line_num, col_chars, total_lines)
}
pub(crate) fn send_input(&mut self) {
self.show_welcome = false;
let input = self.input.trim().to_string();
self.input.clear();
self.cursor_pos = 0;
// Save to input history (skip duplicates of last entry)
if !input.is_empty() && self.input_history.last().map(|s| s.as_str()) != Some(&input) {
self.input_history.push(input.clone());
}
// Reset history browsing state
self.history_index = None;
self.history_draft.clear();
if self.waiting_for_ask {
// Respond to approval/ask question
self.waiting_for_ask = false;
self.push_message(Message {
role: Role::User,
content: input.clone(),
});
if let Some(ask_tx) = &self.ask_tx {
ask_tx.try_send(input).ok();
}
self.activity = Activity::Thinking;
self.auto_scroll = true;
} else if input.starts_with('/') {
// Command
self.handle_command(&input);
} else if self.activity == Activity::Idle {
// Send immediately
self.push_message(Message {
role: Role::User,
content: input.clone(),
});
self.tx.try_send(input).ok();
self.activity = Activity::Thinking;
self.request_start = Some(Instant::now());
self.auto_scroll = true;
} else {
// Queue message (AI is processing)
self.pending_messages.push(input.clone());
}
}
/// Handle Enter key in Ask mode - toggle selection in multi-select or confirm
fn handle_ask_enter(&mut self) {
// If in "Other" input mode, send the custom text
if self.ask_other_input_active {
self.confirm_ask_selection();
return;
}
// Check if current selection is "Other" option (single-select)
if !self.ask_multi_select && !self.ask_options.is_empty() {
let current_idx = self.ask_selected_index;
if current_idx < self.ask_options.len() && self.ask_options[current_idx].is_other {
// Enter "Other" input mode - user will type custom content
self.ask_other_input_active = true;
self.input.clear();
self.cursor_pos = 0;
return;
}
}
if self.ask_multi_select && !self.ask_options.is_empty() {
// Multi-select: toggle current selection
self.toggle_ask_selection();
} else {
// Single-select: confirm
self.confirm_ask_selection();
}
}
/// Toggle current selection in multi-select mode (used by Space and Enter)
fn toggle_ask_selection(&mut self) {
if !self.ask_multi_select || self.ask_options.is_empty() {
return;
}
let current_idx = self.ask_selected_index;
if current_idx < self.ask_options.len() {
let is_submit = self.ask_options[current_idx].is_submit;
let is_other = self.ask_options[current_idx].is_other;
// Toggle the selection
self.ask_options[current_idx].selected = !self.ask_options[current_idx].selected;
// If "Other" was just checked, enter text input mode
if is_other && self.ask_options[current_idx].selected {
self.ask_other_input_active = true;
self.input.clear();
self.cursor_pos = 0;
return;
}
// If "Other" was just unchecked, exit text input mode
if is_other && !self.ask_options[current_idx].selected {
self.ask_other_input_active = false;
self.input.clear();
self.cursor_pos = 0;
return;
}
// If Submit was just checked, confirm immediately in Option mode
if is_submit
&& self.ask_options[current_idx].selected
&& self.ask_submit_mode == SubmitMode::Option
{
self.confirm_ask_selection();
}
}
}
/// Switch to next question in multi-question mode
fn switch_to_next_question(&mut self) {
if self.ask_questions.len() <= 1 {
return;
}
// Save current question state
self.save_current_question_state();
// Move to next question
self.current_question_idx = (self.current_question_idx + 1) % self.ask_questions.len();
// Load next question state
self.load_question_state();
// Update the Ask message content to show new question
self.update_ask_message_for_current_question();
}
/// Update the Ask message to display current question
fn update_ask_message_for_current_question(&mut self) {
if self.current_question_idx >= self.ask_questions.len() {
return;
}
let q = &self.ask_questions[self.current_question_idx];
let mut content = String::new();
content.push_str("╔══════════════════════════════════════╗\n");
content.push_str(&format!(
"║ ⚡ 问题 {} / {} (Tab切换) ⚡ ║\n",
self.current_question_idx + 1,
self.ask_questions.len()
));
content.push_str("╚══════════════════════════════════════╝\n\n");
content.push_str(&q.question);
// Add Submit option for Option mode if needed
let mut display_options = q.options.clone();
if q.multi_select && q.submit_mode == SubmitMode::Option {
display_options.push(AskOption {
id: "__submit__".into(),
label: "✓ 提交".into(),
description: Some("确认选择并提交".into()),
selected: false,
is_submit: true,
is_other: false,
});
}
content.push_str("\n\n─────────────────────────────────────\n");
if q.multi_select {
match q.submit_mode {
SubmitMode::Direct => {
if self.current_question_idx < self.ask_questions.len() - 1 {
content.push_str("选项 (↑↓导航 Space/Enter切换 Enter下一题):\n");
} else {
content.push_str("选项 (↑↓导航 Space/Enter切换 Enter提交):\n");
}
}
SubmitMode::Option => {
content.push_str("选项 (↑↓导航 Space/Enter切换 选中[✓提交]):\n")
}
SubmitMode::Button => {
content.push_str("选项 (↑↓导航 Space/Enter切换 Enter提交):\n")
}
}
} else {
if self.current_question_idx < self.ask_questions.len() - 1 {
content.push_str("选项 (↑↓选择 Enter下一题):\n");
} else {
content.push_str("选项 (↑↓选择 Enter提交):\n");
}
}
for (i, opt) in display_options.iter().enumerate() {
if opt.is_submit {
// Submit option also shows as checkbox
let marker = if opt.selected { "[✓]" } else { "[ ]" };
content.push_str(&format!(
" {} {}{}\n",
marker,
opt.label,
opt.format_description()
));
} else {
let marker = if q.multi_select {
if opt.selected {
"[✓]".to_string()
} else {
"[ ]".to_string()
}
} else {
format!("[{}]", (b'A' + i as u8) as char)
};
content.push_str(&format!(
" {} {}{}\n",
marker,
opt.label,
opt.format_description()
));
}
}
// Update the last Ask message
if let Some(last_msg) = self.messages.last_mut()
&& last_msg.role == Role::Ask
{
last_msg.content = content;
}
}
/// Save current question state to ask_questions
fn save_current_question_state(&mut self) {
if self.current_question_idx < self.ask_questions.len() {
let q = &mut self.ask_questions[self.current_question_idx];
// Save options excluding the Submit option (it's dynamically added)
q.options = self
.ask_options
.iter()
.filter(|opt| !opt.is_submit)
.cloned()
.collect();
q.selected_index = self
.ask_selected_index
.min(q.options.len().saturating_sub(1));
q.multi_select = self.ask_multi_select;
q.submit_mode = self.ask_submit_mode.clone();
// Save "Other" input state
if self.ask_other_input_active && !self.input.trim().is_empty() {
q.other_input = Some(self.input.clone());
}
}
}
/// Load question state from ask_questions[current_idx]
fn load_question_state(&mut self) {
if self.current_question_idx < self.ask_questions.len() {
let q = &self.ask_questions[self.current_question_idx];
self.ask_options = q.options.clone();
self.ask_selected_index = q.selected_index;
self.ask_multi_select = q.multi_select;
self.ask_submit_mode = q.submit_mode.clone();
// Restore "Other" input state
if q.other_input.is_some() {
self.ask_other_input_active = true;
self.input = q.other_input.clone().unwrap_or_default();
self.cursor_pos = self.input.len();
} else {
self.ask_other_input_active = false;
self.input.clear();
self.cursor_pos = 0;
}
// Add Submit option for Option mode if needed
if self.ask_multi_select && self.ask_submit_mode == SubmitMode::Option {
self.ask_options.push(AskOption {
id: "__submit__".into(),
label: "提交".into(),
description: Some("确认并提交所有选择".into()),
selected: false,
is_submit: true,
is_other: false,
});
}
}
}
/// Confirm ask selection - send selected option(s) or custom input
pub(crate) fn confirm_ask_selection(&mut self) {
if !self.waiting_for_ask {
return;
}
// Handle "Other" input mode - send custom text
if self.ask_other_input_active {
if self.input.trim().is_empty() {
// Empty input in Other mode - don't submit, stay in input mode
return;
}
let custom_text = self.input.trim().to_string();
// Find the "Other" option and get its id
let other_id = self
.ask_options
.iter()
.find(|opt| opt.is_other)
.map(|opt| opt.id.clone())
.unwrap_or("other".to_string());
// In multi-select, include other_id with custom text
if self.ask_multi_select {
let mut selected_ids: Vec<String> = self
.ask_options
.iter()
.filter(|opt| opt.selected && !opt.is_submit && !opt.is_other)
.map(|opt| opt.id.clone())
.collect();
selected_ids.push(other_id);
let response =
serde_json::to_string(&selected_ids).unwrap_or_else(|_| "[]".to_string());
let mut display_labels: Vec<&str> = self
.ask_options
.iter()
.filter(|opt| opt.selected && !opt.is_submit && !opt.is_other)
.map(|opt| opt.label.as_str())
.collect();
display_labels.push(&custom_text);
let display_response = display_labels.join(", ");
self.waiting_for_ask = false;
self.activity = Activity::Thinking;
self.auto_scroll = true;
self.input.clear();
self.cursor_pos = 0;
self.ask_options.clear();
self.ask_selected_index = 0;
self.ask_multi_select = false;
self.ask_submit_mode = SubmitMode::default();
self.ask_other_input_active = false;
self.push_message(Message {
role: Role::User,
content: display_response,
});
if let Some(ask_tx) = &self.ask_tx {
ask_tx.try_send(response).ok();
}
return;
} else {
// Single-select with "Other" - send custom text directly
self.waiting_for_ask = false;
self.activity = Activity::Thinking;
self.auto_scroll = true;
self.push_message(Message {
role: Role::User,
content: custom_text.clone(),
});
if let Some(ask_tx) = &self.ask_tx {
ask_tx.try_send(custom_text).ok();
}
self.input.clear();
self.cursor_pos = 0;
self.ask_options.clear();
self.ask_selected_index = 0;
self.ask_multi_select = false;
self.ask_submit_mode = SubmitMode::default();
self.ask_other_input_active = false;
return;
}
}
// In Option submit mode, only submit when Submit option is selected (checked)
if self.ask_submit_mode == SubmitMode::Option && !self.ask_options.is_empty() {
// Find the Submit option
let submit_selected = self
.ask_options
.iter()
.find(|opt| opt.is_submit)
.map(|opt| opt.selected)
.unwrap_or(false);
if !submit_selected {
// Submit not selected, don't submit
return;
}
}
// Multi-question mode: check if all questions answered
if self.ask_questions.len() > 1 {
// Save current question state first
self.save_current_question_state();
// Check if we're at the last question
if self.current_question_idx < self.ask_questions.len() - 1 {
// Not at last question, switch to next
self.switch_to_next_question();
return;
}
// At last question - collect all answers and submit
let answers: std::collections::HashMap<String, serde_json::Value> = self
.ask_questions
.iter()
.map(|q| {
let answer = if q.multi_select && !q.options.is_empty() {
// Multi-select: collect selected ids
let selected_ids: Vec<&str> = q
.options
.iter()
.filter(|opt| opt.selected && !opt.is_submit)
.map(|opt| opt.id.as_str())
.collect();
serde_json::json!(selected_ids)
} else if !q.options.is_empty() {
// Single select: use selected index
serde_json::json!(
q.options
.get(q.selected_index)
.map(|o| o.id.clone())
.unwrap_or_default()
)
} else {
serde_json::json!("")
};
(q.id.clone(), answer)
})
.collect();
let response = serde_json::to_string(&answers).unwrap_or_else(|_| "{}".to_string());
let display_response = format!("已回答 {} 个问题", self.ask_questions.len());
// Clear state
self.waiting_for_ask = false;
self.activity = Activity::Thinking;
self.auto_scroll = true;
self.input.clear();
self.cursor_pos = 0;
self.ask_options.clear();
self.ask_selected_index = 0;
self.ask_multi_select = false;
self.ask_submit_mode = SubmitMode::default();
self.ask_other_input_active = false;
self.ask_questions.clear();
self.current_question_idx = 0;
// Send response
self.push_message(Message {
role: Role::User,
content: display_response,
});
if let Some(ask_tx) = &self.ask_tx {
ask_tx.try_send(response).ok();
}
return;
}
// Single question mode
self.waiting_for_ask = false;
self.activity = Activity::Thinking;
self.auto_scroll = true;
// Determine response based on mode
let (response, display_response) = if self.ask_multi_select && !self.ask_options.is_empty()
{
// Multi-select: collect all selected options (exclude Submit option)
let selected_ids: Vec<&str> = self
.ask_options
.iter()
.filter(|opt| opt.selected && !opt.is_submit)
.map(|opt| opt.id.as_str())
.collect();
// Send as JSON array
let response =
serde_json::to_string(&selected_ids).unwrap_or_else(|_| "[]".to_string());
// Display as comma-separated labels
let display_labels: Vec<&str> = self
.ask_options
.iter()
.filter(|opt| opt.selected && !opt.is_submit)
.map(|opt| opt.label.as_str())
.collect();
let display = if display_labels.is_empty() {
"未选择".to_string()
} else {
display_labels.join(", ")
};
(response, display)
} else if !self.ask_options.is_empty() && self.input.trim().is_empty() {
// Single select: use selected option's id
let selected = &self.ask_options[self.ask_selected_index];
let response = selected.id.clone();
let display = selected.label.clone();
(response, display)
} else if !self.input.trim().is_empty() {
// Custom text input
let text = self.input.trim().to_string();
(text.clone(), text)
} else if !self.ask_options.is_empty() {
// Default: first option
let selected = &self.ask_options[0];
let response = selected.id.clone();
let display = selected.label.clone();
(response, display)
} else {
("y".to_string(), "同意".to_string()) // Default approval
};
// Clear input and options
self.input.clear();
self.cursor_pos = 0;
self.ask_options.clear();
self.ask_selected_index = 0;
self.ask_multi_select = false;
self.ask_submit_mode = SubmitMode::default();
self.ask_other_input_active = false;
// Send response
self.push_message(Message {
role: Role::User,
content: display_response,
});
if let Some(ask_tx) = &self.ask_tx {
ask_tx.try_send(response).ok();
}
}
pub(crate) fn on_paste(&mut self, text: &str) {
self.ensure_char_boundary();
self.input.insert_str(self.cursor_pos, text);
self.cursor_pos += text.len(); // cursor_pos is byte position
}
}