octomind 0.25.0

Session-based AI development assistant with conversational codebase interaction, multimodal vision support, built-in MCP tools, and multi-provider AI integration
Documentation
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
// Copyright 2026 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// User input handling module

use crate::config::Config;
use crate::mcp::get_available_functions;
use crate::session::estimate_full_context_tokens;
use crate::session::history::{append_to_session_history_file, load_session_history_from_file};
use anyhow::Result;
use colored::*;
use reedline::{
	default_emacs_keybindings, ColumnarMenu, EditCommand, Emacs, FileBackedHistory, History,
	HistoryItem, KeyCode, KeyModifiers, Keybindings, MenuBuilder, Reedline, ReedlineEvent,
	ReedlineMenu, Signal,
};
use std::io::Write;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;

/// Result of user input operation
#[derive(Debug)]
pub enum InputResult {
	/// Normal text input from user
	Text(String),
	/// Input was cancelled (Ctrl+C)
	Cancelled,
	/// User wants to exit (Ctrl+D)
	Exit,
	/// Add message to context without sending (Ctrl+G)
	AddWithoutSending(String),
}

use crate::log_info;

fn display_shortcuts_help() {
	println!();
	println!(
		"{}",
		"╭─ Keyboard Shortcuts ─────────────────────────────────────╮".bright_cyan()
	);
	println!(
		"{}",
		"│ /           - Commands (type /help for list)            │".bright_black()
	);
	println!(
		"{}",
		"│ @           - Fuzzy file completion (e.g., @src/ma)     │".bright_black()
	);
	println!(
		"{}",
		"│ Tab         - Complete command/file                     │".bright_black()
	);
	println!(
		"{}",
		"│ Shift+Tab   - Search history                            │".bright_black()
	);
	println!(
		"{}",
		"│ Ctrl+J      - Insert newline (multi-line input)         │".bright_black()
	);
	println!(
		"{}",
		"│ Ctrl+G      - Add message without sending to API        │".bright_black()
	);
	println!(
		"{}",
		"│ Ctrl+E      - Accept hint / Exit reverse search         │".bright_black()
	);
	println!(
		"{}",
		"│ Ctrl+R      - Search command history                    │".bright_black()
	);
	println!(
		"{}",
		"│ Ctrl+C      - Cancel current operation                  │".bright_black()
	);
	println!(
		"{}",
		"│ Ctrl+D      - Exit session                              │".bright_black()
	);
	println!(
		"{}",
		"│ Ctrl+P/N    - Navigate command history                  │".bright_black()
	);
	println!(
		"{}",
		"│ →           - Accept hint (when at end of line)         │".bright_black()
	);
	println!(
		"{}",
		"╰──────────────────────────────────────────────────────────╯".bright_cyan()
	);
	println!();

	let _ = std::io::stdout().flush();
}

fn calculate_context_percentage(
	current_context_tokens: u64,
	max_session_tokens_threshold: usize,
) -> Option<f64> {
	if max_session_tokens_threshold > 0 {
		Some(
			(current_context_tokens as f64 / max_session_tokens_threshold as f64 * 100.0)
				.min(100.0),
		)
	} else {
		None
	}
}

fn add_completion_menu_keybindings(keybindings: &mut Keybindings) {
	keybindings.add_binding(
		KeyModifiers::NONE,
		KeyCode::Tab,
		ReedlineEvent::UntilFound(vec![
			ReedlineEvent::Menu("completion_menu".to_string()),
			ReedlineEvent::MenuNext,
		]),
	);
}

/// Calculate current context tokens for the session
/// This uses actual message count + system prompt + tools, NOT lifetime accumulated tokens
pub async fn calculate_current_context_tokens(
	messages: &[crate::session::Message],
	config: &Config,
	_role: &str,
) -> u64 {
	// Get available tools
	let tools = get_available_functions(config).await;

	// Calculate actual context tokens
	estimate_full_context_tokens(messages, Some(&tools)) as u64
}
pub fn read_user_input(
	estimated_cost: f64,
	octomind_config: &Config,
	role: &str,
	current_context_tokens: u64,
	max_session_tokens_threshold: usize,
	session_id: &str,
	inbox_pending: Arc<std::sync::Mutex<Option<String>>>,
) -> Result<InputResult> {
	// Create reedline with in-memory history and preloaded role history
	let mut history = FileBackedHistory::new(1000).expect("Error configuring history");
	if let Ok(lines) = load_session_history_from_file(role) {
		for line in lines {
			let _ = history.save(HistoryItem {
				id: None,
				start_timestamp: None,
				command_line: line,
				session_id: None,
				hostname: None,
				cwd: None,
				duration: None,
				exit_status: None,
				more_info: None,
			});
		}
	}
	let history = Box::new(history);

	let mut keybindings = default_emacs_keybindings();
	keybindings.add_binding(
		KeyModifiers::CONTROL,
		KeyCode::Char('j'),
		ReedlineEvent::Edit(vec![EditCommand::InsertNewline]),
	);
	keybindings.add_binding(
		KeyModifiers::CONTROL,
		KeyCode::Char('u'),
		ReedlineEvent::Edit(vec![
			EditCommand::CutFromLineStart,
			EditCommand::CutToLineEnd,
		]),
	);
	keybindings.add_binding(
		KeyModifiers::CONTROL,
		KeyCode::Char('a'),
		ReedlineEvent::Edit(vec![EditCommand::MoveToLineStart { select: false }]),
	);
	keybindings.add_binding(
		KeyModifiers::CONTROL,
		KeyCode::Char('e'),
		ReedlineEvent::Edit(vec![EditCommand::MoveToLineEnd { select: false }]),
	);
	// Note: Ctrl+G is handled in edit_mode.rs via line_state flag (no buffer modification)
	keybindings.add_binding(
		KeyModifiers::CONTROL,
		KeyCode::Char('e'),
		ReedlineEvent::HistoryHintComplete,
	);
	keybindings.add_binding(
		KeyModifiers::CONTROL,
		KeyCode::Char('p'),
		ReedlineEvent::UntilFound(vec![
			ReedlineEvent::MenuPrevious,
			ReedlineEvent::PreviousHistory,
		]),
	);
	keybindings.add_binding(
		KeyModifiers::CONTROL,
		KeyCode::Char('n'),
		ReedlineEvent::UntilFound(vec![ReedlineEvent::MenuNext, ReedlineEvent::NextHistory]),
	);
	keybindings.add_binding(
		KeyModifiers::NONE,
		KeyCode::Char('@'),
		ReedlineEvent::Multiple(vec![
			ReedlineEvent::Edit(vec![EditCommand::InsertChar('@')]),
			ReedlineEvent::Menu("completion_menu".to_string()),
		]),
	);
	add_completion_menu_keybindings(&mut keybindings);
	let config = Arc::new(octomind_config.clone());
	let role_name = role.to_string();
	let buffer_empty = Arc::new(AtomicBool::new(true));
	let reverse_search_active = Arc::new(AtomicBool::new(false));
	let hint_available = Arc::new(AtomicBool::new(false));
	let line_state = Arc::new(std::sync::Mutex::new(
		crate::session::chat::reedline_adapter::LineState::default(),
	));
	let edit_mode = Box::new(crate::session::chat::EmacsWithShortcutHelp::new(
		Emacs::new(keybindings),
		buffer_empty.clone(),
		reverse_search_active.clone(),
		hint_available.clone(),
		line_state.clone(),
	));

	let completion_menu = Box::new(
		ColumnarMenu::default()
			.with_name("completion_menu")
			.with_columns(4)
			.with_column_padding(2),
	);

	let line_editor = Reedline::create()
		.with_history(history)
		.with_completer(Box::new(
			crate::session::chat::reedline_adapter::ReedlineAdapter::new(
				config.clone(),
				role_name.clone(),
				buffer_empty.clone(),
				hint_available.clone(),
				line_state.clone(),
			),
		))
		.with_menu(ReedlineMenu::EngineCompleter(completion_menu))
		.with_highlighter(Box::new(
			crate::session::chat::reedline_adapter::ReedlineAdapter::new(
				config.clone(),
				role_name.clone(),
				buffer_empty.clone(),
				hint_available.clone(),
				line_state.clone(),
			),
		))
		.with_hinter(Box::new(
			crate::session::chat::reedline_adapter::ReedlineAdapter::new(
				config,
				role_name.clone(),
				buffer_empty,
				hint_available,
				line_state.clone(),
			),
		))
		.with_quick_completions(true)
		.use_bracketed_paste(true)
		.with_edit_mode(edit_mode);

	// Set up external printer for inbox notifications.
	// A background thread polls the inbox_pending flag and sends a
	// one-shot notification that reedline renders above the prompt.
	let printer = reedline::ExternalPrinter::<String>::new(5);
	let sender = printer.sender();
	let inbox_slot = inbox_pending;
	std::thread::spawn(move || {
		let mut notified = false;
		loop {
			std::thread::sleep(std::time::Duration::from_millis(100));
			let preview = inbox_slot.lock().ok().and_then(|g| g.clone());
			if let Some(preview) = preview {
				if !notified {
					let msg = format!(
						"\x1b[33m📨 Inbox message received ({preview}) — press Enter to process\x1b[0m"
					);
					if sender.send(msg).is_err() {
						break; // receiver dropped, reedline is gone
					}
					notified = true;
				}
			} else {
				notified = false;
			}
		}
	});
	let mut line_editor = line_editor
		.with_external_printer(printer)
		.with_poll_interval(std::time::Duration::from_millis(100));

	// Set prompt with cost and context percentage
	let prompt_text = if estimated_cost > 0.0 {
		let context_pct =
			calculate_context_percentage(current_context_tokens, max_session_tokens_threshold);

		if let Some(pct) = context_pct {
			format!("[${:.2}|{:.1}%]", estimated_cost, pct)
		} else {
			format!("[${:.2}|∞]", estimated_cost)
		}
	} else if max_session_tokens_threshold > 0 {
		// No cost but still show context percentage
		let context_pct =
			calculate_context_percentage(current_context_tokens, max_session_tokens_threshold);
		if let Some(pct) = context_pct {
			format!("[{:.1}%]", pct)
		} else {
			String::new()
		}
	} else {
		String::new()
	};
	let prompt_left = if prompt_text.is_empty() {
		String::new()
	} else {
		format!("{} ", prompt_text).bright_blue().to_string()
	};
	let prompt = crate::session::chat::ChatPrompt::new(
		prompt_left,
		"".bright_blue().to_string(),
		reverse_search_active,
	);

	// Clone line_state for use in the loop (original moved into edit_mode)
	let line_state_for_check = line_state.clone();

	// Read line with reedline
	loop {
		match line_editor.read_line(&prompt) {
			Ok(Signal::Success(line)) => {
				if line == "__show_shortcuts__" {
					display_shortcuts_help();
					continue;
				}
				if line.trim() == "?" {
					display_shortcuts_help();
					continue;
				}

				// Check if this is an "add without sending" request (Ctrl+G)
				// The flag is set in edit_mode.rs when user presses Ctrl+G
				let add_without_sending = if let Ok(mut state) = line_state_for_check.lock() {
					let flag = state.add_without_sending;
					state.add_without_sending = false; // Clear the flag
					flag
				} else {
					false
				};

				// Check if line starts with whitespace (bash-like behavior)
				// If it does, skip adding to history (both in-memory and persistent)
				let starts_with_whitespace = line.starts_with(char::is_whitespace);

				if !starts_with_whitespace {
					// Add to in-memory history (reedline handles this automatically)
					// Append to persistent file using role-based thread-safe method
					// This includes ALL inputs - both regular inputs and commands starting with '/'
					if let Err(e) = append_to_session_history_file(&role_name, &line) {
						// Don't fail if history can't be saved, just log it
						log_info!(
							"Could not append to history file for role '{}': {}",
							role,
							e
						);
					}
				}

				// User message persistence handled by ChatSession::add_user_message.

				return if add_without_sending {
					Ok(InputResult::AddWithoutSending(line))
				} else {
					Ok(InputResult::Text(line))
				};
			}
			Ok(Signal::CtrlC) => {
				// Ctrl+C - Return cancellation result
				return Ok(InputResult::Cancelled);
			}
			Ok(Signal::CtrlD) => {
				// Ctrl+D - Show resume command
				let resume_cmd = format!("octomind run --resume {}", session_id).bright_cyan();
				println!("\nTo continue this session, run: {}", resume_cmd);

				// Debug logging for session preservation
				if let Ok(sessions_dir) = crate::session::get_sessions_dir() {
					crate::log_debug!("Session files saved in: {}", sessions_dir.display());
				}
				crate::log_debug!("Session preserved for future reference.");
				return Ok(InputResult::Exit);
			}
			Ok(_) => {
				// reedline Signal is non-exhaustive — handle future variants gracefully
				continue;
			}
			Err(err) => {
				let msg = format!("{err:?}");
				// Terminal is broken/detached (e.g. another shell took over, resize event
				// while no TTY, or crossterm cursor-position timeout). Looping would spam
				// the same error endlessly — exit cleanly instead.
				if msg.contains("cursor position could not be read")
					|| msg.contains("not a terminal")
					|| msg.contains("inappropriate ioctl")
				{
					return Ok(InputResult::Exit);
				}
				log_info!("Reedline error: {}", msg);
				return Ok(InputResult::Text(String::new()));
			}
		}
	}
}