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
//! Event handling for the TUI
use anyhow::Result;
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
use std::time::Duration;
use tui_input::backend::crossterm::EventHandler;
use super::app::{App, AppMode, InputMode};
use super::media::MediaFile;
pub fn handle_events(app: &mut App) -> Result<bool> {
// Check if app should quit first
if app.should_quit {
return Ok(true);
}
if event::poll(Duration::from_millis(50))? {
if let Event::Key(key) = event::read()? {
if key.kind == KeyEventKind::Press {
return handle_key_event(app, key);
}
}
}
// Check again after processing events
Ok(app.should_quit)
}
fn handle_key_event(app: &mut App, key: crossterm::event::KeyEvent) -> Result<bool> {
match app.input_mode {
InputMode::Normal => handle_normal_mode(app, key),
InputMode::Editing => handle_editing_mode(app, key),
}
}
fn handle_normal_mode(app: &mut App, key: crossterm::event::KeyEvent) -> Result<bool> {
match key.code {
// Global commands
KeyCode::Char('q') => {
if key.modifiers.contains(KeyModifiers::CONTROL) {
app.quit();
return Ok(true);
} else {
// Just 'q' also quits in normal mode for convenience
app.quit();
return Ok(true);
}
}
KeyCode::Esc => {
app.quit();
return Ok(true);
}
KeyCode::Char('c') => {
if key.modifiers.contains(KeyModifiers::CONTROL) {
app.quit();
return Ok(true);
}
}
// Tab navigation
KeyCode::Tab => app.next_tab(),
KeyCode::BackTab => app.previous_tab(),
// Mode-specific number keys
KeyCode::Char('1') => app.switch_mode(AppMode::Chat),
KeyCode::Char('2') => app.switch_mode(AppMode::AgentSwarm),
KeyCode::Char('3') => app.switch_mode(AppMode::MediaBrowser),
KeyCode::Char('4') => app.switch_mode(AppMode::Settings),
KeyCode::Char('5') => app.switch_mode(AppMode::DistributedAgents),
KeyCode::Char('6') => app.switch_mode(AppMode::AdvancedReasoning),
// Navigation
KeyCode::Up | KeyCode::Char('k') => app.move_list_up(),
KeyCode::Down | KeyCode::Char('j') => app.move_list_down(),
// Mode-specific commands
_ => handle_mode_specific_normal(app, key)?,
}
Ok(false)
}
fn handle_mode_specific_normal(app: &mut App, key: crossterm::event::KeyEvent) -> Result<()> {
match app.mode {
AppMode::Chat => handle_chat_normal(app, key),
AppMode::AgentSwarm => handle_agent_normal(app, key),
AppMode::MediaBrowser => handle_media_normal(app, key),
AppMode::Settings => handle_settings_normal(app, key),
AppMode::DistributedAgents => handle_distributed_normal(app, key),
AppMode::AdvancedReasoning => handle_reasoning_normal(app, key),
AppMode::Search => handle_search_normal(app, key),
}
}
fn handle_chat_normal(app: &mut App, key: crossterm::event::KeyEvent) -> Result<()> {
match key.code {
KeyCode::Enter | KeyCode::Char('i') => {
app.input_mode = InputMode::Editing;
}
KeyCode::Char('e') => {
if key.modifiers.contains(KeyModifiers::CONTROL) {
// Export conversation to markdown
let markdown = app.export_to_markdown();
// In real implementation, save to file
std::fs::write("conversation_export.md", markdown)
.unwrap_or_else(|_| eprintln!("Failed to export conversation"));
}
}
KeyCode::Char('j') => {
if key.modifiers.contains(KeyModifiers::CONTROL) {
// Export conversation to JSON
if let Ok(json) = app.export_to_json() {
std::fs::write("conversation_export.json", json)
.unwrap_or_else(|_| eprintln!("Failed to export JSON"));
}
}
}
KeyCode::Char('l') => {
if key.modifiers.contains(KeyModifiers::CONTROL) {
// Clear chat history
app.clear_conversation();
}
}
KeyCode::Char('f') => {
if key.modifiers.contains(KeyModifiers::CONTROL) {
// Switch to search mode
app.mode = AppMode::Search;
app.input_mode = InputMode::Editing;
}
}
KeyCode::Char('1') => {
// Toggle auto-scroll
app.toggle_auto_scroll();
}
KeyCode::Char('2') => {
// Toggle timestamps
app.toggle_timestamps();
}
KeyCode::Char('3') => {
// Toggle media preview
app.toggle_media_preview();
}
KeyCode::Char('c') => {
// Clear chat history
app.messages.clear();
app.message_list_state.select(None);
}
KeyCode::Char('m') => {
// Switch to media browser to attach files
app.switch_mode(AppMode::MediaBrowser);
}
KeyCode::Char('a') => {
// Switch to agent mode
app.switch_mode(AppMode::AgentSwarm);
}
_ => {}
}
Ok(())
}
fn handle_agent_normal(app: &mut App, key: crossterm::event::KeyEvent) -> Result<()> {
match key.code {
KeyCode::Char('n') => {
// Create new agent
app.add_agent(
format!("Agent-{}", app.agents.len() + 1),
app.current_model.clone(),
vec!["print".to_string(), "ls".to_string()],
);
}
KeyCode::Delete | KeyCode::Char('d') => {
// Delete selected agent
app.remove_selected_agent();
}
KeyCode::Enter | KeyCode::Char('s') => {
// Start task for selected agent
app.input_mode = InputMode::Editing;
}
KeyCode::Char('m') => {
// View metrics for all agents
let metrics = app.get_agent_metrics();
// In real implementation, this would show a metrics panel
for metric in metrics {
eprintln!(
"Agent: {} | Status: {:?} | Uptime: {}s | Tools: {}",
metric.name, metric.status, metric.uptime_seconds, metric.tool_count
);
}
}
KeyCode::Char('p') => {
if key.modifiers.contains(KeyModifiers::CONTROL) {
// Pause/resume all agents (placeholder)
// Would toggle agent execution state
}
}
KeyCode::Char('r') => {
// Restart selected agent (placeholder)
// Would reset agent state
}
KeyCode::Char('c') => {
// Switch to chat mode
app.switch_mode(AppMode::Chat);
}
_ => {}
}
Ok(())
}
fn handle_media_normal(app: &mut App, key: crossterm::event::KeyEvent) -> Result<()> {
match key.code {
KeyCode::Enter | KeyCode::Char(' ') => {
// Toggle selection of current media file
app.toggle_media_selection();
}
KeyCode::Char('o') => {
// Open file dialog (placeholder)
// In a real implementation, you'd use a file picker
if let Ok(file) = MediaFile::from_path("example.jpg") {
app.add_media_file(file);
}
}
KeyCode::Char('c') => {
// Clear selection
app.clear_media_selection();
}
KeyCode::Delete | KeyCode::Char('d') => {
// Remove selected media file
if let Some(selected) = app.media_list_state.selected() {
if selected < app.media_files.len() {
app.media_files.remove(selected);
// Update selection indices
app.selected_media.retain(|&idx| idx != selected);
for idx in &mut app.selected_media {
if *idx > selected {
*idx -= 1;
}
}
// Adjust list selection
if app.media_files.is_empty() {
app.media_list_state.select(None);
} else if selected >= app.media_files.len() {
app.media_list_state.select(Some(app.media_files.len() - 1));
}
}
}
}
KeyCode::Char('b') => {
// Go back to chat with selected media
app.switch_mode(AppMode::Chat);
}
_ => {}
}
Ok(())
}
fn handle_settings_normal(_app: &mut App, _key: crossterm::event::KeyEvent) -> Result<()> {
// Settings mode navigation would go here
Ok(())
}
fn handle_editing_mode(app: &mut App, key: crossterm::event::KeyEvent) -> Result<bool> {
match key.code {
KeyCode::Enter => {
match app.mode {
AppMode::Chat => {
app.send_message()?;
app.input_mode = InputMode::Normal;
}
AppMode::AgentSwarm => {
// Start agent task
let task = app.input.value().to_string();
if !task.trim().is_empty() {
app.start_agent_task(task)?;
app.input.reset();
}
app.input_mode = InputMode::Normal;
}
AppMode::Search => {
// Execute search
app.search_query = app.input.value().to_string();
app.execute_search();
app.input.reset();
app.input_mode = InputMode::Normal;
}
_ => {
app.input_mode = InputMode::Normal;
}
}
}
KeyCode::Esc => {
app.input_mode = InputMode::Normal;
}
_ => {
app.input.handle_event(&Event::Key(key));
}
}
Ok(false)
}
fn handle_distributed_normal(_app: &mut App, key: crossterm::event::KeyEvent) -> Result<()> {
match key.code {
KeyCode::Char('s') => {
// Start distributed swarm (placeholder - would be async in real implementation)
// app.start_distributed_swarm("127.0.0.1:8080").await?;
}
KeyCode::Char('d') => {
// Stop distributed swarm (placeholder - would be async in real implementation)
// app.stop_distributed_swarm().await?;
}
KeyCode::Char('r') => {
// Refresh network status
// In a real implementation, this would update the agent status
}
KeyCode::Char('t') => {
// Test connection
// In a real implementation, this would ping all connected agents
}
_ => {}
}
Ok(())
}
fn handle_reasoning_normal(_app: &mut App, key: crossterm::event::KeyEvent) -> Result<()> {
match key.code {
KeyCode::Char('n') => {
// Start new reasoning session (placeholder - would be async in real implementation)
// let goal = create_planning_goal_from_input(&app.input.value());
// app.start_reasoning_session(goal).await?;
}
KeyCode::Char('p') => {
// View planning goals
// In a real implementation, this would show a dialog with active goals
}
KeyCode::Char('k') => {
// Browse knowledge base
// In a real implementation, this would show knowledge base contents
}
KeyCode::Char('e') => {
// Export reasoning chains
// In a real implementation, this would save reasoning chains to file
}
KeyCode::Char('i') => {
// Import knowledge
// In a real implementation, this would load knowledge from file
}
_ => {}
}
Ok(())
}
fn handle_search_normal(app: &mut App, key: crossterm::event::KeyEvent) -> Result<()> {
match key.code {
KeyCode::Char('i') | KeyCode::Char('/') => {
// Enter search input mode
app.input_mode = InputMode::Editing;
}
KeyCode::Down | KeyCode::Char('j') => {
// Navigate to next search result
app.next_search_result();
}
KeyCode::Up | KeyCode::Char('k') => {
// Navigate to previous search result
app.previous_search_result();
}
KeyCode::Esc => {
// Clear search and return to chat
app.clear_search();
}
KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
// Copy selected search result to clipboard (future enhancement)
if !app.search_results.is_empty() {
let result_idx = app.search_results[app.search_result_index];
if let Some(msg) = app.messages.get(result_idx) {
eprintln!("Would copy: {}", msg.content);
}
}
}
_ => {}
}
Ok(())
}