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
//! Key Handler Methods for Chat View
//!
//! Specialized key event handlers for overlay modes and main key handling.
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use tui_input::InputRequest;
use std::path::PathBuf;
use super::{is_cmd_pressed, ChatPanel, ChatView};
use crate::tui::command::{Command, ExportFormat, ModelProvider, HELP_TEXT};
use crate::tui::file_resolve::FileResolver;
use crate::tui::views::{TuiView, ViewAction};
use crate::tui::widgets::provider_modal::ModalEventHandler;
use crate::tui::widgets::task_box::RenderMode;
use super::hints::{detect_verb_in_input, verb_placeholder};
// ═══════════════════════════════════════════════════════════════════════════════
// Key Handler Methods
// ═══════════════════════════════════════════════════════════════════════════════
impl ChatView {
/// Handle key events when help overlay is visible
pub(super) fn handle_help_overlay_key(&mut self, key: KeyEvent) -> ViewAction {
match key.code {
// Escape, ?, F1 = Close help
KeyCode::Esc | KeyCode::Char('?') | KeyCode::F(1) => {
self.help_overlay.hide();
ViewAction::None
}
// j/Down = Scroll down
KeyCode::Char('j') | KeyCode::Down => {
// Max scroll based on content (HELP_SECTIONS has ~30 lines)
self.help_overlay.scroll_down(30);
ViewAction::None
}
// k/Up = Scroll up
KeyCode::Char('k') | KeyCode::Up => {
self.help_overlay.scroll_up();
ViewAction::None
}
// g = Scroll to top
KeyCode::Char('g') => {
self.help_overlay.scroll = 0;
ViewAction::None
}
// G = Scroll to bottom
KeyCode::Char('G') => {
self.help_overlay.scroll = 30;
ViewAction::None
}
// PageUp
KeyCode::PageUp => {
self.help_overlay.scroll_page_up(10);
ViewAction::None
}
// PageDown
KeyCode::PageDown => {
self.help_overlay.scroll_page_down(30, 10);
ViewAction::None
}
_ => ViewAction::None,
}
}
/// Handle key events when command palette is visible
pub(super) fn handle_palette_key(&mut self, key: KeyEvent) -> ViewAction {
match key.code {
KeyCode::Esc => {
self.command_palette.close();
ViewAction::None
}
KeyCode::Enter => {
if let Some(cmd_id) = self.command_palette.execute_selected() {
// Execute the selected command
self.input = tui_input::Input::new(format!("/{}", cmd_id));
self.input.handle(tui_input::InputRequest::GoToEnd);
// Trigger submit with the command
if let Some(message) = self.submit() {
let cmd = Command::parse(&message);
return match cmd {
Command::Help => {
self.add_nika_message(HELP_TEXT.to_string(), None);
ViewAction::None
}
Command::Clear => ViewAction::ChatClear,
Command::Exec { command } => ViewAction::ChatExec(command),
Command::Fetch { url, method } => ViewAction::ChatFetch(url, method),
Command::FetchError {
error,
hint,
example,
} => {
// Show helpful error message
self.add_nika_message(
format!("{}\n{}\n{}", error, hint, example),
None,
);
ViewAction::None
}
Command::Invoke {
tool,
server,
params,
} => ViewAction::ChatInvoke(tool, server, params),
Command::Agent {
goal,
max_turns,
mcp_servers,
} => ViewAction::ChatAgent(
goal,
max_turns,
self.deep_thinking,
mcp_servers,
),
Command::Mcp { action } => ViewAction::ChatMcp(action),
Command::Model { provider } => ViewAction::ChatModelSwitch(provider),
Command::Export { format, path } => {
// Export chat to JSON or YAML file
let result = match format {
ExportFormat::Json => self.export_session(path.as_deref()),
ExportFormat::Yaml => self.export_session_yaml(path.as_deref()),
};
match result {
Ok(filepath) => {
let format_name = match format {
ExportFormat::Json => "JSON",
ExportFormat::Yaml => "YAML workflow",
};
self.add_system_message(format!(
"📤 Chat exported as {} to: {}",
format_name, filepath
));
}
Err(e) => {
self.add_system_message(format!("❌ Export failed: {}", e));
}
}
ViewAction::None
}
Command::Run { path } => ViewAction::RunWorkflow(PathBuf::from(path)),
Command::Infer { prompt } | Command::Chat { message: prompt } => {
ViewAction::ChatInfer(prompt)
}
};
}
}
ViewAction::None
}
KeyCode::Up => {
self.command_palette.select_prev();
ViewAction::None
}
KeyCode::Down => {
self.command_palette.select_next();
ViewAction::None
}
KeyCode::Char(c) => {
self.command_palette.input_char(c);
ViewAction::None
}
KeyCode::Backspace => {
self.command_palette.backspace();
ViewAction::None
}
_ => ViewAction::None,
}
}
/// Handle key events for Provider Modal (full management)
pub(super) fn handle_provider_modal_key(&mut self, key: KeyEvent) -> ViewAction {
let result = ModalEventHandler::handle(&mut self.provider_modal, key);
// If the modal requested to close
if let Some(action) = &result.action {
use crate::tui::widgets::ModalAction;
match action {
ModalAction::Close => {
self.provider_modal.visible = false;
}
ModalAction::RefreshProviders => {
return ViewAction::VerifyProviders;
}
ModalAction::RefreshNativeModels => {
self.add_system_message("🔄 Refreshing native models...".to_string());
return ViewAction::RefreshNativeModels;
}
ModalAction::TestApiKey { provider } => {
self.add_system_message(format!("🔑 Testing {} API key...", provider));
return ViewAction::VerifyProviders;
}
ModalAction::PullModel { model } => {
self.add_system_message(format!("📥 Pulling model: {}...", model));
return ViewAction::PullNativeModel(model.clone());
}
ModalAction::DeleteModel { model } => {
self.add_system_message(format!("🗑️ Deleting model: {}...", model));
return ViewAction::DeleteNativeModel(model.clone());
}
ModalAction::SaveAndTestApiKey { provider, key } => {
use crate::secrets::{validate_key_format, NikaKeyring};
// Validate format first
if let Err(e) = validate_key_format(provider, key) {
self.add_system_message(format!("❌ Invalid key format: {}", e));
return ViewAction::None;
}
// Save to keyring
match NikaKeyring::set(provider, key) {
Ok(()) => {
self.add_system_message(format!(
"✅ Saved {} API key to keychain",
provider
));
// Trigger verification
return ViewAction::VerifyProviders;
}
Err(e) => {
self.add_system_message(format!("❌ Failed to save key: {}", e));
}
}
}
ModalAction::SaveApiKey { provider, key } => {
use crate::secrets::{validate_key_format, NikaKeyring};
// Validate format first
if let Err(e) = validate_key_format(provider, key) {
self.add_system_message(format!("❌ Invalid key format: {}", e));
return ViewAction::None;
}
// Save silently (no verification trigger)
match NikaKeyring::set(provider, key) {
Ok(()) => {
self.add_system_message(format!(
"✅ Saved {} API key to keychain",
provider
));
self.provider_modal.visible = false;
}
Err(e) => {
self.add_system_message(format!("❌ Failed to save key: {}", e));
}
}
}
ModalAction::SelectProvider { provider, model } => {
// Actually switch provider via ChatModelSwitch
use crate::tui::command::ModelProvider;
self.provider_modal.visible = false;
if let Some(model_provider) = ModelProvider::from_name(provider) {
self.add_system_message(format!(
"🔄 Switching to {} ({})",
provider, model
));
return ViewAction::ChatModelSwitch(model_provider);
} else {
self.add_system_message(format!(
"❌ Unknown provider: {} - available: claude, openai, mistral, groq, deepseek, native",
provider
));
}
}
ModalAction::CheckProvider { provider } => {
self.add_system_message(format!("🔍 Checking {} connection...", provider));
return ViewAction::VerifyProviders;
}
}
}
ViewAction::None
}
/// Try to autocomplete verb command with Tab
/// Returns Some(new_input) if autocomplete happened, None otherwise
pub(super) fn try_verb_autocomplete(&self) -> Option<String> {
let input = self.input.value();
if let Some((_verb_len, verb_color, is_complete, full_verb)) = detect_verb_in_input(input) {
if !is_complete {
// Partial match → complete the verb name
return Some(format!("/{} ", full_verb));
} else {
// Complete verb → check if we should insert placeholder
let rest = if input.len() > full_verb.len() + 1 {
&input[full_verb.len() + 1..] // +1 for /
} else {
""
};
if rest.trim().is_empty() {
// Insert first placeholder example
let placeholder = verb_placeholder(&verb_color, 0);
return Some(format!("/{} {}", full_verb, placeholder));
}
}
}
None
}
// ═══════════════════════════════════════════════════════════════════════════════
// Main Key Handler (extracted from mod.rs handle_key)
// ═══════════════════════════════════════════════════════════════════════════════
/// Handle main key events (after overlay dispatch)
/// Returns Some(ViewAction) if key was handled, None to fall through
pub(super) fn handle_main_keys(&mut self, key: KeyEvent) -> Option<ViewAction> {
// ───────────────────────────────────────────────────────────────────────────
// Global shortcuts (Cmd/Ctrl + key)
// ───────────────────────────────────────────────────────────────────────────
// Cmd/Ctrl+K = Command palette toggle
if is_cmd_pressed(key.modifiers) && key.code == KeyCode::Char('k') {
self.toggle_command_palette();
return Some(ViewAction::None);
}
// Cmd/Ctrl+P = Provider modal toggle
if is_cmd_pressed(key.modifiers) && key.code == KeyCode::Char('p') {
let opened = self.toggle_provider_modal();
if opened {
return Some(ViewAction::VerifyProviders);
}
return Some(ViewAction::None);
}
// Cmd/Ctrl+F = Start conversation search
if is_cmd_pressed(key.modifiers) && key.code == KeyCode::Char('f') {
self.start_search();
return Some(ViewAction::None);
}
// Cmd/Ctrl+D = Toggle DAG panel
if is_cmd_pressed(key.modifiers) && key.code == KeyCode::Char('d') {
self.toggle_dag_panel();
let status = if self.show_dag_panel {
"visible"
} else {
"hidden"
};
self.add_system_message(format!("🔀 DAG panel {}", status));
return Some(ViewAction::None);
}
// Cmd/Ctrl+T = Toggle deep thinking
if is_cmd_pressed(key.modifiers) && key.code == KeyCode::Char('t') {
self.toggle_deep_thinking();
let status = if self.deep_thinking {
"enabled"
} else {
"disabled"
};
self.add_system_message(format!("🧠 Deep thinking {}", status));
return Some(ViewAction::None);
}
// Alt+T = Toggle all thinking sections visibility
if key.modifiers.contains(KeyModifiers::ALT) && key.code == KeyCode::Char('t') {
self.toggle_all_thinking();
return Some(ViewAction::None);
}
// Cmd/Ctrl+M = Toggle infer/agent mode
if is_cmd_pressed(key.modifiers) && key.code == KeyCode::Char('m') {
self.toggle_mode();
self.add_system_message(format!(
"{} Switched to {} mode",
self.chat_mode.icon(),
self.chat_mode.label()
));
return Some(ViewAction::None);
}
// Cmd/Ctrl+C = Copy selection or input
if is_cmd_pressed(key.modifiers) && key.code == KeyCode::Char('c') {
if self.text_selection.is_some() {
if self.copy_selection() {
self.add_system_message("📋 Selection copied to clipboard");
self.clear_selection();
}
} else {
self.copy_to_clipboard();
}
return Some(ViewAction::None);
}
// Cmd/Ctrl+V = Paste from clipboard
if is_cmd_pressed(key.modifiers) && key.code == KeyCode::Char('v') {
self.paste_from_clipboard();
return Some(ViewAction::None);
}
// Ctrl+Z = Undo input edit
if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('z') {
if let Some((text, cursor)) = self.edit_history.undo() {
self.input = tui_input::Input::new(text);
let pos = cursor.min(self.input.value().len());
self.input.handle(InputRequest::GoToStart);
for _ in 0..pos {
self.input.handle(InputRequest::GoToNextChar);
}
}
return Some(ViewAction::None);
}
// Ctrl+Y = Redo input edit
if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('y') {
if let Some((text, cursor)) = self.edit_history.redo() {
self.input = tui_input::Input::new(text);
let pos = cursor.min(self.input.value().len());
self.input.handle(InputRequest::GoToStart);
for _ in 0..pos {
self.input.handle(InputRequest::GoToNextChar);
}
}
return Some(ViewAction::None);
}
// Cmd/Ctrl+R = Retry last failed MCP call
if is_cmd_pressed(key.modifiers) && key.code == KeyCode::Char('r') {
if let Some(failed) = self.last_failed_mcp.take() {
self.add_system_message("🔄 Retrying MCP call...".to_string());
return Some(ViewAction::ChatInvoke(
failed.tool,
Some(failed.server),
failed.params,
));
} else {
self.add_system_message("⚠️ No failed MCP call to retry".to_string());
}
return Some(ViewAction::None);
}
// Cmd/Ctrl+Left = Move to previous word
if is_cmd_pressed(key.modifiers) && key.code == KeyCode::Left {
self.cursor_prev_word();
return Some(ViewAction::None);
}
// Cmd/Ctrl+Right = Move to next word
if is_cmd_pressed(key.modifiers) && key.code == KeyCode::Right {
self.cursor_next_word();
return Some(ViewAction::None);
}
// Cmd/Ctrl+Backspace = Delete previous word
if is_cmd_pressed(key.modifiers) && key.code == KeyCode::Backspace {
self.delete_prev_word();
return Some(ViewAction::None);
}
// Cmd/Ctrl+A = Go to start of input
if is_cmd_pressed(key.modifiers) && key.code == KeyCode::Char('a') {
self.cursor_start();
return Some(ViewAction::None);
}
// Cmd/Ctrl+E = Go to end of input
if is_cmd_pressed(key.modifiers) && key.code == KeyCode::Char('e') {
self.cursor_end();
return Some(ViewAction::None);
}
// ───────────────────────────────────────────────────────────────────────────
// Ctrl+j/k for vim-style panel navigation
// ───────────────────────────────────────────────────────────────────────────
if key.modifiers.contains(KeyModifiers::CONTROL) {
if key.code == KeyCode::Char('j') {
self.focus_next_panel();
return Some(ViewAction::None);
}
if key.code == KeyCode::Char('k') {
self.focus_prev_panel();
return Some(ViewAction::None);
}
}
// ───────────────────────────────────────────────────────────────────────────
// F1 or ? = Toggle help overlay
// ───────────────────────────────────────────────────────────────────────────
if key.code == KeyCode::F(1)
|| (key.code == KeyCode::Char('?') && self.focused_panel != ChatPanel::Input)
{
self.help_overlay.toggle();
return Some(ViewAction::None);
}
// ───────────────────────────────────────────────────────────────────────────
// Tab = Autocomplete verb in Input panel
// ───────────────────────────────────────────────────────────────────────────
if key.code == KeyCode::Tab && self.focused_panel == ChatPanel::Input {
if let Some(completed) = self.try_verb_autocomplete() {
self.input = tui_input::Input::new(completed);
self.input.handle(InputRequest::GoToEnd);
}
return Some(ViewAction::None);
}
// ───────────────────────────────────────────────────────────────────────────
// Panel navigation: Left/Right when NOT in Input panel
// ───────────────────────────────────────────────────────────────────────────
if self.focused_panel != ChatPanel::Input {
if key.code == KeyCode::Right {
self.focus_next_panel();
return Some(ViewAction::None);
}
if key.code == KeyCode::Left {
self.focus_prev_panel();
return Some(ViewAction::None);
}
}
// ───────────────────────────────────────────────────────────────────────────
// Vim-style navigation when NOT in Input panel
// ───────────────────────────────────────────────────────────────────────────
if self.focused_panel != ChatPanel::Input {
match key.code {
// j/Down = Scroll down
KeyCode::Char('j') | KeyCode::Down => {
self.scroll_down();
return Some(ViewAction::None);
}
// k/Up = Scroll up
KeyCode::Char('k') | KeyCode::Up => {
self.scroll_up();
return Some(ViewAction::None);
}
// g = Go to top
KeyCode::Char('g') => {
self.scroll_to_top();
return Some(ViewAction::None);
}
// G = Go to bottom
KeyCode::Char('G') => {
self.scroll_to_bottom();
return Some(ViewAction::None);
}
// PageUp/PageDown
KeyCode::PageUp => {
self.page_up();
return Some(ViewAction::None);
}
KeyCode::PageDown => {
self.page_down();
return Some(ViewAction::None);
}
// y = Copy full message
KeyCode::Char('y') => {
if self.copy_selected_message(false) {
self.add_system_message("📋 Message copied to clipboard".to_string());
}
return Some(ViewAction::None);
}
// Y = Copy text only
KeyCode::Char('Y') => {
if self.copy_selected_message(true) {
self.add_system_message("📋 Text copied to clipboard".to_string());
}
return Some(ViewAction::None);
}
// t = Toggle thinking for selected message
KeyCode::Char('t') => {
let cursor = self.conversation_scroll.cursor;
if cursor < self.messages.len() && self.messages[cursor].thinking.is_some() {
self.toggle_thinking(cursor);
let state = if self.is_thinking_visible(cursor) {
"expanded"
} else {
"collapsed"
};
self.add_system_message(format!("🧠 Thinking {} for message", state));
}
return Some(ViewAction::None);
}
// Enter = Return focus to Input
KeyCode::Enter => {
self.focus_panel(ChatPanel::Input);
return Some(ViewAction::None);
}
// Escape = Return to Input
KeyCode::Esc => {
self.focus_panel(ChatPanel::Input);
return Some(ViewAction::None);
}
_ => {} // Fall through
}
}
// ───────────────────────────────────────────────────────────────────────────
// Normal key handling (Input panel active or no special handling above)
// ───────────────────────────────────────────────────────────────────────────
match key.code {
// Ctrl+J scrolls down (vi-like)
KeyCode::Char('j') if key.modifiers.contains(KeyModifiers::CONTROL) => {
self.scroll_down();
Some(ViewAction::None)
}
// 's' when empty switches to Studio
KeyCode::Char('s') if self.input.value().is_empty() => {
Some(ViewAction::SwitchView(TuiView::Studio))
}
// 'm' when empty cycles TaskBox render mode
KeyCode::Char('m') if self.input.value().is_empty() => {
self.cycle_task_box_render_mode();
let mode_name = match self.task_box_render_mode {
RenderMode::Compact => "Compact (1 line)",
RenderMode::Expanded => "Expanded (default)",
RenderMode::Full => "Full (all details)",
};
self.add_system_message(format!("📦 TaskBox mode: {}", mode_name));
Some(ViewAction::None)
}
// Shift+T or Ctrl+t toggles theme
KeyCode::Char('T') => Some(ViewAction::ToggleTheme),
KeyCode::Char('t') if key.modifiers.contains(KeyModifiers::CONTROL) => {
Some(ViewAction::ToggleTheme)
}
// "/" at start triggers command palette with verbs
KeyCode::Char('/') if self.input.value().is_empty() => {
self.toggle_command_palette();
self.command_palette.query = "/".to_string();
self.command_palette.update_filter();
Some(ViewAction::None)
}
// "@" triggers file mention
KeyCode::Char('@') => {
self.insert_char('@');
if !self.shown_file_hint {
self.add_system_message(
"💡 Type @filename to include file content".to_string(),
);
self.shown_file_hint = true;
}
Some(ViewAction::None)
}
// Shift+Enter inserts newline
KeyCode::Enter if key.modifiers.contains(KeyModifiers::SHIFT) => {
self.input.handle(InputRequest::InsertChar('\n'));
Some(ViewAction::None)
}
// Enter = Submit
KeyCode::Enter => self.handle_enter_key(),
// Ctrl+Up/Down = History navigation
KeyCode::Up if key.modifiers.contains(KeyModifiers::CONTROL) => {
self.history_up();
Some(ViewAction::None)
}
KeyCode::Down if key.modifiers.contains(KeyModifiers::CONTROL) => {
self.history_down();
Some(ViewAction::None)
}
// Up/Down = Scroll
KeyCode::Up => {
self.scroll_up();
Some(ViewAction::None)
}
KeyCode::Down => {
self.scroll_down();
Some(ViewAction::None)
}
// Left/Right = Cursor movement
KeyCode::Left => {
self.cursor_left();
Some(ViewAction::None)
}
KeyCode::Right => {
self.cursor_right();
Some(ViewAction::None)
}
// Backspace
KeyCode::Backspace => {
self.backspace();
Some(ViewAction::None)
}
// Character input
KeyCode::Char(c) => {
self.insert_char(c);
Some(ViewAction::None)
}
// Page scroll
KeyCode::PageUp => {
self.page_up();
Some(ViewAction::None)
}
KeyCode::PageDown => {
self.page_down();
Some(ViewAction::None)
}
// Home/End
KeyCode::Home => {
self.scroll = 0;
Some(ViewAction::None)
}
KeyCode::End => {
self.scroll_to_bottom();
Some(ViewAction::None)
}
// Esc = Switch to Studio view
KeyCode::Esc => Some(ViewAction::SwitchView(TuiView::Studio)),
_ => None,
}
}
/// Handle Enter key submission
fn handle_enter_key(&mut self) -> Option<ViewAction> {
if let Some(message) = self.submit() {
let cmd = Command::parse(&message);
match cmd {
Command::Help => {
self.add_nika_message(HELP_TEXT.to_string(), None);
Some(ViewAction::None)
}
Command::Clear => Some(ViewAction::ChatClear),
Command::Exec { command } => Some(ViewAction::ChatExec(command)),
Command::Fetch { url, method } => Some(ViewAction::ChatFetch(url, method)),
Command::FetchError {
error,
hint,
example,
} => {
self.add_nika_message(format!("{}\n{}\n{}", error, hint, example), None);
Some(ViewAction::None)
}
Command::Invoke {
tool,
server,
params,
} => Some(ViewAction::ChatInvoke(tool, server, params)),
Command::Agent {
goal,
max_turns,
mcp_servers,
} => Some(ViewAction::ChatAgent(
goal,
max_turns,
self.deep_thinking,
mcp_servers,
)),
Command::Mcp { action } => Some(ViewAction::ChatMcp(action)),
Command::Model { provider } => {
if provider == ModelProvider::List {
let providers = [
ModelProvider::Claude,
ModelProvider::OpenAI,
ModelProvider::Mistral,
ModelProvider::Groq,
ModelProvider::DeepSeek,
ModelProvider::Native,
];
let mut list_text =
String::from("Available providers (use /model <name>):\n");
for p in providers {
let status = if p.is_available() {
"available"
} else {
"missing API key"
};
list_text.push_str(&format!(
" - {}: {} ({})\n",
p.command_name(),
p.name(),
status
));
}
self.add_nika_message(list_text.trim_end().to_string(), None);
Some(ViewAction::None)
} else {
Some(ViewAction::ChatModelSwitch(provider))
}
}
Command::Export { format, path } => {
let result = match format {
ExportFormat::Json => self.export_session(path.as_deref()),
ExportFormat::Yaml => self.export_session_yaml(path.as_deref()),
};
match result {
Ok(filepath) => {
let format_name = match format {
ExportFormat::Json => "JSON",
ExportFormat::Yaml => "YAML workflow",
};
self.add_system_message(format!(
"📤 Chat exported as {} to: {}",
format_name, filepath
));
}
Err(e) => {
self.add_system_message(format!("❌ Export failed: {}", e));
}
}
Some(ViewAction::None)
}
Command::Run { path } => Some(ViewAction::RunWorkflow(PathBuf::from(path))),
Command::Infer { prompt } | Command::Chat { message: prompt } => {
let base_dir = std::env::current_dir().unwrap_or_default();
let expanded = FileResolver::resolve(&prompt, &base_dir);
Some(ViewAction::ChatInfer(expanded))
}
}
} else {
Some(ViewAction::None)
}
}
}