2026-04-04 02:46:32 -04:00
|
|
|
// user/ — User interface layer
|
2026-03-25 00:52:41 -04:00
|
|
|
//
|
2026-04-04 02:46:32 -04:00
|
|
|
// TUI, UI channel, parsing. The cognitive layer (session state
|
|
|
|
|
// machine, DMN, identity) lives in mind/.
|
2026-03-25 00:52:41 -04:00
|
|
|
|
2026-04-04 02:46:32 -04:00
|
|
|
pub mod chat;
|
|
|
|
|
pub mod context;
|
|
|
|
|
pub mod subconscious;
|
|
|
|
|
pub mod unconscious;
|
|
|
|
|
pub mod thalamus;
|
2026-04-05 21:16:49 -04:00
|
|
|
pub mod ui_channel;
|
|
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
use ratatui::crossterm::event::{Event, EventStream, KeyEventKind};
|
|
|
|
|
use futures::StreamExt;
|
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
use crate::mind::MindCommand;
|
|
|
|
|
use crate::user::{self as tui};
|
2026-04-04 02:46:32 -04:00
|
|
|
|
|
|
|
|
// --- TUI infrastructure (moved from tui/mod.rs) ---
|
|
|
|
|
|
2026-04-05 06:22:31 -04:00
|
|
|
use ratatui::crossterm::{
|
2026-04-05 18:57:54 -04:00
|
|
|
event::{EnableMouseCapture, DisableMouseCapture, KeyCode, KeyEvent, KeyModifiers},
|
2026-04-04 02:46:32 -04:00
|
|
|
terminal::{self, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
|
ExecutableCommand,
|
|
|
|
|
};
|
|
|
|
|
use ratatui::{
|
|
|
|
|
backend::CrosstermBackend,
|
|
|
|
|
};
|
|
|
|
|
use std::io;
|
|
|
|
|
|
2026-04-05 18:57:54 -04:00
|
|
|
use crate::user::ui_channel::{ContextInfo, SharedContextState, StatusInfo};
|
2026-04-04 02:46:32 -04:00
|
|
|
|
2026-04-05 19:03:06 -04:00
|
|
|
/// Build the screen legend from screen labels.
|
|
|
|
|
pub(crate) fn screen_legend_from(interact: &dyn ScreenView, screens: &[Box<dyn ScreenView>]) -> String {
|
|
|
|
|
let mut parts = vec![format!("F1={}", interact.label())];
|
|
|
|
|
for (i, s) in screens.iter().enumerate() {
|
|
|
|
|
parts.push(format!("F{}={}", i + 2, s.label()));
|
|
|
|
|
}
|
|
|
|
|
format!(" {} ", parts.join(" "))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cached legend — set once at startup by event_loop
|
|
|
|
|
static SCREEN_LEGEND: std::sync::OnceLock<String> = std::sync::OnceLock::new();
|
|
|
|
|
|
|
|
|
|
pub(crate) fn set_screen_legend(legend: String) {
|
|
|
|
|
let _ = SCREEN_LEGEND.set(legend);
|
|
|
|
|
}
|
|
|
|
|
|
user: ScreenView trait, overlay screens extracted from App
Convert F2-F5 screens to ScreenView trait with tick() method.
Each screen owns its view state (scroll, selection, expanded).
State persists across screen switches.
- ThalamusScreen: owns sampling_selected, scroll
- ConsciousScreen: owns scroll, selected, expanded
- SubconsciousScreen: owns selected, log_view, scroll
- UnconsciousScreen: owns scroll
Removed from App: Screen enum, debug_scroll, debug_selected,
debug_expanded, agent_selected, agent_log_view, sampling_selected,
set_screen(), per-screen key handling, draw dispatch.
App now only draws the interact (F1) screen. Overlay screens are
drawn by the event loop via ScreenView::tick. F-key routing and
screen instantiation to be wired in event_loop next.
InteractScreen (state-driven, reading from agent entries) is the
next step — will eliminate the input display race condition.
Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 17:54:40 -04:00
|
|
|
pub(crate) fn screen_legend() -> String {
|
2026-04-05 19:03:06 -04:00
|
|
|
SCREEN_LEGEND.get().cloned().unwrap_or_default()
|
user: ScreenView trait, overlay screens extracted from App
Convert F2-F5 screens to ScreenView trait with tick() method.
Each screen owns its view state (scroll, selection, expanded).
State persists across screen switches.
- ThalamusScreen: owns sampling_selected, scroll
- ConsciousScreen: owns scroll, selected, expanded
- SubconsciousScreen: owns selected, log_view, scroll
- UnconsciousScreen: owns scroll
Removed from App: Screen enum, debug_scroll, debug_selected,
debug_expanded, agent_selected, agent_log_view, sampling_selected,
set_screen(), per-screen key handling, draw dispatch.
App now only draws the interact (F1) screen. Overlay screens are
drawn by the event loop via ScreenView::tick. F-key routing and
screen instantiation to be wired in event_loop next.
InteractScreen (state-driven, reading from agent entries) is the
next step — will eliminate the input display race condition.
Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 17:54:40 -04:00
|
|
|
}
|
2026-04-04 02:46:32 -04:00
|
|
|
|
user: ScreenView trait, overlay screens extracted from App
Convert F2-F5 screens to ScreenView trait with tick() method.
Each screen owns its view state (scroll, selection, expanded).
State persists across screen switches.
- ThalamusScreen: owns sampling_selected, scroll
- ConsciousScreen: owns scroll, selected, expanded
- SubconsciousScreen: owns selected, log_view, scroll
- UnconsciousScreen: owns scroll
Removed from App: Screen enum, debug_scroll, debug_selected,
debug_expanded, agent_selected, agent_log_view, sampling_selected,
set_screen(), per-screen key handling, draw dispatch.
App now only draws the interact (F1) screen. Overlay screens are
drawn by the event loop via ScreenView::tick. F-key routing and
screen instantiation to be wired in event_loop next.
InteractScreen (state-driven, reading from agent entries) is the
next step — will eliminate the input display race condition.
Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 17:54:40 -04:00
|
|
|
/// Action returned from a screen's tick method.
|
|
|
|
|
pub enum ScreenAction {
|
|
|
|
|
/// Switch to screen at this index
|
|
|
|
|
Switch(usize),
|
|
|
|
|
/// Send a hotkey action to the Mind
|
|
|
|
|
Hotkey(HotkeyAction),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A screen that can draw itself and handle input.
|
2026-04-05 17:59:33 -04:00
|
|
|
pub(crate) trait ScreenView: Send {
|
user: ScreenView trait, overlay screens extracted from App
Convert F2-F5 screens to ScreenView trait with tick() method.
Each screen owns its view state (scroll, selection, expanded).
State persists across screen switches.
- ThalamusScreen: owns sampling_selected, scroll
- ConsciousScreen: owns scroll, selected, expanded
- SubconsciousScreen: owns selected, log_view, scroll
- UnconsciousScreen: owns scroll
Removed from App: Screen enum, debug_scroll, debug_selected,
debug_expanded, agent_selected, agent_log_view, sampling_selected,
set_screen(), per-screen key handling, draw dispatch.
App now only draws the interact (F1) screen. Overlay screens are
drawn by the event loop via ScreenView::tick. F-key routing and
screen instantiation to be wired in event_loop next.
InteractScreen (state-driven, reading from agent entries) is the
next step — will eliminate the input display race condition.
Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 17:54:40 -04:00
|
|
|
fn tick(&mut self, frame: &mut ratatui::Frame, area: ratatui::layout::Rect,
|
2026-04-05 18:57:54 -04:00
|
|
|
key: Option<ratatui::crossterm::event::KeyEvent>, app: &mut App) -> Option<ScreenAction>;
|
user: ScreenView trait, overlay screens extracted from App
Convert F2-F5 screens to ScreenView trait with tick() method.
Each screen owns its view state (scroll, selection, expanded).
State persists across screen switches.
- ThalamusScreen: owns sampling_selected, scroll
- ConsciousScreen: owns scroll, selected, expanded
- SubconsciousScreen: owns selected, log_view, scroll
- UnconsciousScreen: owns scroll
Removed from App: Screen enum, debug_scroll, debug_selected,
debug_expanded, agent_selected, agent_log_view, sampling_selected,
set_screen(), per-screen key handling, draw dispatch.
App now only draws the interact (F1) screen. Overlay screens are
drawn by the event loop via ScreenView::tick. F-key routing and
screen instantiation to be wired in event_loop next.
InteractScreen (state-driven, reading from agent entries) is the
next step — will eliminate the input display race condition.
Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 17:54:40 -04:00
|
|
|
fn label(&self) -> &'static str;
|
2026-04-04 02:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum HotkeyAction {
|
|
|
|
|
CycleReasoning, KillProcess, Interrupt, CycleAutonomy,
|
2026-04-04 14:06:42 -04:00
|
|
|
/// Adjust a sampling parameter: (param_index, delta)
|
|
|
|
|
/// 0=temperature, 1=top_p, 2=top_k
|
|
|
|
|
AdjustSampling(usize, f32),
|
2026-04-04 02:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub(crate) struct IdleInfo {
|
|
|
|
|
pub user_present: bool,
|
|
|
|
|
pub since_activity: f64,
|
|
|
|
|
pub activity_ewma: f64,
|
|
|
|
|
pub block_reason: String,
|
|
|
|
|
pub dreaming: bool,
|
|
|
|
|
pub sleeping: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub(crate) struct ChannelStatus {
|
|
|
|
|
pub name: String,
|
|
|
|
|
pub connected: bool,
|
|
|
|
|
pub unread: u32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct App {
|
|
|
|
|
pub(crate) status: StatusInfo,
|
|
|
|
|
pub(crate) activity: String,
|
|
|
|
|
pub running_processes: u32,
|
|
|
|
|
pub reasoning_effort: String,
|
2026-04-04 13:48:24 -04:00
|
|
|
pub temperature: f32,
|
|
|
|
|
pub top_p: f32,
|
|
|
|
|
pub top_k: u32,
|
2026-04-04 02:46:32 -04:00
|
|
|
pub(crate) active_tools: crate::user::ui_channel::SharedActiveTools,
|
|
|
|
|
pub should_quit: bool,
|
|
|
|
|
pub submitted: Vec<String>,
|
|
|
|
|
pub hotkey_actions: Vec<HotkeyAction>,
|
|
|
|
|
pub(crate) context_info: Option<ContextInfo>,
|
|
|
|
|
pub(crate) shared_context: SharedContextState,
|
|
|
|
|
pub(crate) agent_state: Vec<crate::subconscious::subconscious::AgentSnapshot>,
|
|
|
|
|
pub(crate) channel_status: Vec<ChannelStatus>,
|
|
|
|
|
pub(crate) idle_info: Option<IdleInfo>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl App {
|
|
|
|
|
pub fn new(model: String, shared_context: SharedContextState, active_tools: crate::user::ui_channel::SharedActiveTools) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
status: StatusInfo {
|
|
|
|
|
dmn_state: "resting".into(), dmn_turns: 0, dmn_max_turns: 20,
|
|
|
|
|
prompt_tokens: 0, completion_tokens: 0, model,
|
|
|
|
|
turn_tools: 0, context_budget: String::new(),
|
|
|
|
|
},
|
|
|
|
|
activity: String::new(),
|
2026-04-05 18:57:54 -04:00
|
|
|
running_processes: 0,
|
2026-04-04 02:46:32 -04:00
|
|
|
reasoning_effort: "none".to_string(),
|
2026-04-04 13:48:24 -04:00
|
|
|
temperature: 0.6,
|
|
|
|
|
top_p: 0.95,
|
|
|
|
|
top_k: 20,
|
2026-04-05 18:57:54 -04:00
|
|
|
active_tools,
|
2026-04-04 02:46:32 -04:00
|
|
|
should_quit: false, submitted: Vec::new(), hotkey_actions: Vec::new(),
|
|
|
|
|
context_info: None, shared_context,
|
user: ScreenView trait, overlay screens extracted from App
Convert F2-F5 screens to ScreenView trait with tick() method.
Each screen owns its view state (scroll, selection, expanded).
State persists across screen switches.
- ThalamusScreen: owns sampling_selected, scroll
- ConsciousScreen: owns scroll, selected, expanded
- SubconsciousScreen: owns selected, log_view, scroll
- UnconsciousScreen: owns scroll
Removed from App: Screen enum, debug_scroll, debug_selected,
debug_expanded, agent_selected, agent_log_view, sampling_selected,
set_screen(), per-screen key handling, draw dispatch.
App now only draws the interact (F1) screen. Overlay screens are
drawn by the event loop via ScreenView::tick. F-key routing and
screen instantiation to be wired in event_loop next.
InteractScreen (state-driven, reading from agent entries) is the
next step — will eliminate the input display race condition.
Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
2026-04-05 17:54:40 -04:00
|
|
|
agent_state: Vec::new(),
|
|
|
|
|
channel_status: Vec::new(), idle_info: None,
|
2026-04-04 02:46:32 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 18:57:54 -04:00
|
|
|
pub fn handle_global_key_old(&mut self, _key: KeyEvent) -> bool { false } // placeholder
|
2026-04-04 02:46:32 -04:00
|
|
|
|
2026-04-05 18:57:54 -04:00
|
|
|
/// Handle global keys only (Ctrl combos).
|
|
|
|
|
pub fn handle_global_key(&mut self, key: KeyEvent) -> bool {
|
2026-04-04 02:46:32 -04:00
|
|
|
if key.modifiers.contains(KeyModifiers::CONTROL) {
|
|
|
|
|
match key.code {
|
2026-04-05 18:57:54 -04:00
|
|
|
KeyCode::Char('c') => { self.should_quit = true; return true; }
|
|
|
|
|
KeyCode::Char('r') => { self.hotkey_actions.push(HotkeyAction::CycleReasoning); return true; }
|
|
|
|
|
KeyCode::Char('k') => { self.hotkey_actions.push(HotkeyAction::KillProcess); return true; }
|
|
|
|
|
KeyCode::Char('p') => { self.hotkey_actions.push(HotkeyAction::CycleAutonomy); return true; }
|
2026-04-04 02:46:32 -04:00
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-05 18:57:54 -04:00
|
|
|
false
|
2026-04-04 02:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_channel_status(&mut self, channels: Vec<(String, bool, u32)>) {
|
|
|
|
|
self.channel_status = channels.into_iter()
|
|
|
|
|
.map(|(name, connected, unread)| ChannelStatus { name, connected, unread })
|
|
|
|
|
.collect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn update_idle(&mut self, state: &crate::thalamus::idle::State) {
|
|
|
|
|
self.idle_info = Some(IdleInfo {
|
|
|
|
|
user_present: state.user_present(), since_activity: state.since_activity(),
|
|
|
|
|
activity_ewma: state.activity_ewma, block_reason: state.block_reason().to_string(),
|
|
|
|
|
dreaming: state.dreaming, sleeping: state.sleep_until.is_some(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 18:57:54 -04:00
|
|
|
pub fn init_terminal() -> io::Result<ratatui::Terminal<CrosstermBackend<io::Stdout>>> {
|
2026-04-04 02:46:32 -04:00
|
|
|
terminal::enable_raw_mode()?;
|
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
|
stdout.execute(EnterAlternateScreen)?;
|
|
|
|
|
stdout.execute(EnableMouseCapture)?;
|
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
2026-04-05 18:57:54 -04:00
|
|
|
ratatui::Terminal::new(backend)
|
2026-04-04 02:46:32 -04:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 18:57:54 -04:00
|
|
|
pub fn restore_terminal(terminal: &mut ratatui::Terminal<CrosstermBackend<io::Stdout>>) -> io::Result<()> {
|
2026-04-04 02:46:32 -04:00
|
|
|
terminal::disable_raw_mode()?;
|
|
|
|
|
terminal.backend_mut().execute(DisableMouseCapture)?;
|
|
|
|
|
terminal.backend_mut().execute(LeaveAlternateScreen)?;
|
|
|
|
|
terminal.show_cursor()
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 21:16:49 -04:00
|
|
|
/// Top-level entry point — creates Mind and UI, wires them together.
|
|
|
|
|
pub async fn start(cli: crate::user::CliArgs) -> Result<()> {
|
|
|
|
|
let (config, _figment) = crate::config::load_session(&cli)?;
|
|
|
|
|
|
|
|
|
|
if config.app.debug {
|
|
|
|
|
unsafe { std::env::set_var("POC_DEBUG", "1") };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (ui_tx, ui_rx) = ui_channel::channel();
|
|
|
|
|
let (turn_tx, turn_rx) = tokio::sync::mpsc::channel(1);
|
|
|
|
|
let (mind_tx, mind_rx) = tokio::sync::mpsc::unbounded_channel();
|
|
|
|
|
|
|
|
|
|
let mind = crate::mind::Mind::new(config, ui_tx.clone(), turn_tx);
|
|
|
|
|
|
|
|
|
|
let shared_context = mind.agent.lock().await.shared_context.clone();
|
|
|
|
|
let shared_active_tools = mind.agent.lock().await.active_tools.clone();
|
|
|
|
|
|
|
|
|
|
let mut result = Ok(());
|
|
|
|
|
tokio_scoped::scope(|s| {
|
|
|
|
|
// Mind event loop — init + run
|
|
|
|
|
s.spawn(async {
|
|
|
|
|
mind.init().await;
|
|
|
|
|
mind.run(mind_rx, turn_rx).await;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// UI event loop
|
|
|
|
|
s.spawn(async {
|
|
|
|
|
result = run(
|
|
|
|
|
tui::App::new(String::new(), shared_context, shared_active_tools),
|
2026-04-05 22:18:07 -04:00
|
|
|
&mind, mind_tx,
|
2026-04-05 21:16:49 -04:00
|
|
|
).await;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 22:18:07 -04:00
|
|
|
fn hotkey_cycle_reasoning(mind: &crate::mind::Mind) {
|
2026-04-05 21:16:49 -04:00
|
|
|
if let Ok(mut ag) = mind.agent.try_lock() {
|
|
|
|
|
let next = match ag.reasoning_effort.as_str() {
|
|
|
|
|
"none" => "low",
|
|
|
|
|
"low" => "high",
|
|
|
|
|
_ => "none",
|
|
|
|
|
};
|
|
|
|
|
ag.reasoning_effort = next.to_string();
|
|
|
|
|
let label = match next {
|
|
|
|
|
"none" => "off (monologue hidden)",
|
|
|
|
|
"low" => "low (brief monologue)",
|
|
|
|
|
"high" => "high (full monologue)",
|
|
|
|
|
_ => next,
|
|
|
|
|
};
|
2026-04-05 22:18:07 -04:00
|
|
|
ag.notify(format!("reasoning: {}", label));
|
2026-04-05 21:16:49 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 22:18:07 -04:00
|
|
|
async fn hotkey_kill_processes(mind: &crate::mind::Mind) {
|
|
|
|
|
let mut ag = mind.agent.lock().await;
|
|
|
|
|
let active_tools = ag.active_tools.clone();
|
2026-04-05 21:16:49 -04:00
|
|
|
let mut tools = active_tools.lock().unwrap();
|
|
|
|
|
if tools.is_empty() {
|
2026-04-05 22:18:07 -04:00
|
|
|
ag.notify("no running tools");
|
2026-04-05 21:16:49 -04:00
|
|
|
} else {
|
2026-04-05 22:18:07 -04:00
|
|
|
let count = tools.len();
|
2026-04-05 21:16:49 -04:00
|
|
|
for entry in tools.drain(..) {
|
|
|
|
|
entry.handle.abort();
|
|
|
|
|
}
|
2026-04-05 22:18:07 -04:00
|
|
|
ag.notify(format!("killed {} tools", count));
|
2026-04-05 21:16:49 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 22:18:07 -04:00
|
|
|
fn hotkey_cycle_autonomy(mind: &crate::mind::Mind) {
|
2026-04-05 21:16:49 -04:00
|
|
|
let mut s = mind.shared.lock().unwrap();
|
|
|
|
|
let label = match &s.dmn {
|
|
|
|
|
crate::mind::dmn::State::Engaged | crate::mind::dmn::State::Working | crate::mind::dmn::State::Foraging => {
|
|
|
|
|
s.dmn = crate::mind::dmn::State::Resting { since: std::time::Instant::now() };
|
|
|
|
|
"resting"
|
|
|
|
|
}
|
|
|
|
|
crate::mind::dmn::State::Resting { .. } => {
|
|
|
|
|
s.dmn = crate::mind::dmn::State::Paused;
|
|
|
|
|
"PAUSED"
|
|
|
|
|
}
|
|
|
|
|
crate::mind::dmn::State::Paused => {
|
|
|
|
|
crate::mind::dmn::set_off(true);
|
|
|
|
|
s.dmn = crate::mind::dmn::State::Off;
|
|
|
|
|
"OFF (persists across restarts)"
|
|
|
|
|
}
|
|
|
|
|
crate::mind::dmn::State::Off => {
|
|
|
|
|
crate::mind::dmn::set_off(false);
|
|
|
|
|
s.dmn = crate::mind::dmn::State::Foraging;
|
|
|
|
|
"foraging"
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
s.dmn_turns = 0;
|
|
|
|
|
drop(s);
|
2026-04-05 22:18:07 -04:00
|
|
|
if let Ok(mut ag) = mind.agent.try_lock() {
|
|
|
|
|
ag.notify(format!("DMN → {}", label));
|
|
|
|
|
}
|
2026-04-05 21:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hotkey_adjust_sampling(mind: &crate::mind::Mind, param: usize, delta: f32) {
|
|
|
|
|
if let Ok(mut ag) = mind.agent.try_lock() {
|
|
|
|
|
match param {
|
|
|
|
|
0 => ag.temperature = (ag.temperature + delta).clamp(0.0, 2.0),
|
|
|
|
|
1 => ag.top_p = (ag.top_p + delta).clamp(0.0, 1.0),
|
|
|
|
|
2 => ag.top_k = (ag.top_k as f32 + delta).max(0.0) as u32,
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn diff_mind_state(
|
|
|
|
|
cur: &crate::mind::MindState,
|
|
|
|
|
prev: &crate::mind::MindState,
|
|
|
|
|
dirty: &mut bool,
|
|
|
|
|
) {
|
|
|
|
|
if cur.dmn.label() != prev.dmn.label() || cur.dmn_turns != prev.dmn_turns {
|
|
|
|
|
*dirty = true;
|
|
|
|
|
}
|
|
|
|
|
// Input consumed — Mind started a turn with it
|
|
|
|
|
if !prev.input.is_empty() && cur.input.is_empty() {
|
|
|
|
|
*dirty = true;
|
|
|
|
|
}
|
|
|
|
|
if cur.turn_active != prev.turn_active {
|
|
|
|
|
*dirty = true;
|
|
|
|
|
}
|
|
|
|
|
if cur.scoring_in_flight != prev.scoring_in_flight {
|
|
|
|
|
*dirty = true;
|
|
|
|
|
}
|
|
|
|
|
if cur.compaction_in_flight != prev.compaction_in_flight {
|
|
|
|
|
*dirty = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn run(
|
|
|
|
|
mut app: tui::App,
|
|
|
|
|
mind: &crate::mind::Mind,
|
|
|
|
|
mind_tx: tokio::sync::mpsc::UnboundedSender<MindCommand>,
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
let agent = &mind.agent;
|
|
|
|
|
let shared_mind = &mind.shared;
|
|
|
|
|
let turn_watch = mind.turn_watch();
|
|
|
|
|
// UI-owned state
|
|
|
|
|
let mut idle_state = crate::thalamus::idle::State::new();
|
|
|
|
|
idle_state.load();
|
|
|
|
|
|
|
|
|
|
let (channel_tx, mut channel_rx) = tokio::sync::mpsc::channel::<Vec<(String, bool, u32)>>(4);
|
|
|
|
|
{
|
|
|
|
|
let tx = channel_tx.clone();
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
let result = crate::thalamus::channels::fetch_all_channels().await;
|
|
|
|
|
let _ = tx.send(result).await;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
let notify_rx = crate::thalamus::channels::subscribe_all();
|
|
|
|
|
|
|
|
|
|
let mut interact = crate::user::chat::InteractScreen::new(
|
2026-04-05 22:18:07 -04:00
|
|
|
mind.agent.clone(), mind.shared.clone(), mind_tx.clone(),
|
2026-04-05 21:16:49 -04:00
|
|
|
);
|
|
|
|
|
// Overlay screens: F2=conscious, F3=subconscious, F4=unconscious, F5=thalamus
|
|
|
|
|
let mut screens: Vec<Box<dyn tui::ScreenView>> = vec![
|
|
|
|
|
Box::new(crate::user::context::ConsciousScreen::new()),
|
|
|
|
|
Box::new(crate::user::subconscious::SubconsciousScreen::new()),
|
|
|
|
|
Box::new(crate::user::unconscious::UnconsciousScreen::new()),
|
|
|
|
|
Box::new(crate::user::thalamus::ThalamusScreen::new()),
|
|
|
|
|
];
|
|
|
|
|
let mut active_screen: usize = 0; // 0 = interact, 1-4 = overlay
|
|
|
|
|
tui::set_screen_legend(tui::screen_legend_from(&interact, &screens));
|
|
|
|
|
|
|
|
|
|
let mut terminal = tui::init_terminal()?;
|
|
|
|
|
let mut reader = EventStream::new();
|
|
|
|
|
|
|
|
|
|
let mut render_interval = tokio::time::interval(Duration::from_millis(50));
|
|
|
|
|
render_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
|
|
|
|
let mut dirty = true;
|
|
|
|
|
let mut prev_mind = shared_mind.lock().unwrap().clone();
|
|
|
|
|
let mut pending_key: Option<ratatui::crossterm::event::KeyEvent> = None;
|
|
|
|
|
|
|
|
|
|
terminal.hide_cursor()?;
|
|
|
|
|
|
2026-04-05 22:18:07 -04:00
|
|
|
if let Ok(mut ag) = agent.try_lock() { ag.notify("consciousness v0.3"); }
|
2026-04-05 21:16:49 -04:00
|
|
|
|
|
|
|
|
// Initial render
|
|
|
|
|
terminal.draw(|f| {
|
|
|
|
|
let area = f.area();
|
|
|
|
|
interact.tick(f, area, None, &mut app);
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
// Replay conversation after Mind init completes (non-blocking check)
|
|
|
|
|
let mut startup_done = false;
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
tokio::select! {
|
|
|
|
|
biased;
|
|
|
|
|
|
|
|
|
|
maybe_event = reader.next() => {
|
|
|
|
|
match maybe_event {
|
|
|
|
|
Some(Ok(Event::Key(key))) => {
|
|
|
|
|
if key.kind != KeyEventKind::Press { continue; }
|
|
|
|
|
idle_state.user_activity();
|
|
|
|
|
|
|
|
|
|
// F-keys switch screens
|
|
|
|
|
if let ratatui::crossterm::event::KeyCode::F(n) = key.code {
|
|
|
|
|
active_screen = match n {
|
|
|
|
|
1 => 0, // interact
|
|
|
|
|
n @ 2..=5 if (n as usize - 2) < screens.len() => n as usize - 1,
|
|
|
|
|
_ => active_screen,
|
|
|
|
|
};
|
|
|
|
|
if active_screen == 4 { // thalamus — refresh channels
|
|
|
|
|
let tx = channel_tx.clone();
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
let result = crate::thalamus::channels::fetch_all_channels().await;
|
|
|
|
|
let _ = tx.send(result).await;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
dirty = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Global keys (Ctrl combos) — only pass to screen if not consumed
|
|
|
|
|
if !app.handle_global_key(key) {
|
|
|
|
|
pending_key = Some(key);
|
|
|
|
|
}
|
|
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
Some(Ok(Event::Mouse(_mouse))) => {
|
|
|
|
|
// TODO: route to active screen
|
|
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
Some(Ok(Event::Resize(_w, _h))) => {
|
|
|
|
|
terminal.clear()?;
|
|
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
Some(Err(_)) => break,
|
|
|
|
|
None => break,
|
|
|
|
|
_ => continue,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_ = render_interval.tick() => {
|
|
|
|
|
idle_state.decay_ewma();
|
|
|
|
|
app.update_idle(&idle_state);
|
|
|
|
|
|
2026-04-05 21:21:08 -04:00
|
|
|
// One-time: mark startup done after Mind init
|
2026-04-05 21:16:49 -04:00
|
|
|
if !startup_done {
|
2026-04-05 22:18:07 -04:00
|
|
|
if let Ok(mut ag) = agent.try_lock() {
|
|
|
|
|
let model = ag.model().to_string();
|
|
|
|
|
ag.notify(format!("model: {}", model));
|
2026-04-05 21:16:49 -04:00
|
|
|
startup_done = true;
|
|
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Diff MindState — generate UI messages from changes
|
|
|
|
|
{
|
|
|
|
|
let cur = shared_mind.lock().unwrap();
|
2026-04-05 22:18:07 -04:00
|
|
|
diff_mind_state(&cur, &prev_mind, &mut dirty);
|
2026-04-05 21:16:49 -04:00
|
|
|
prev_mind = cur.clone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while let Ok(notif) = notify_rx.try_recv() {
|
|
|
|
|
let tx = channel_tx.clone();
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
let result = crate::thalamus::channels::fetch_all_channels().await;
|
|
|
|
|
let _ = tx.send(result).await;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Some(channels) = channel_rx.recv() => {
|
|
|
|
|
app.set_channel_status(channels);
|
|
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle hotkey actions
|
|
|
|
|
let actions: Vec<HotkeyAction> = app.hotkey_actions.drain(..).collect();
|
|
|
|
|
for action in actions {
|
|
|
|
|
match action {
|
2026-04-05 22:18:07 -04:00
|
|
|
HotkeyAction::CycleReasoning => hotkey_cycle_reasoning(mind),
|
|
|
|
|
HotkeyAction::KillProcess => hotkey_kill_processes(mind).await,
|
2026-04-05 21:16:49 -04:00
|
|
|
HotkeyAction::Interrupt => { let _ = mind_tx.send(MindCommand::Interrupt); }
|
2026-04-05 22:18:07 -04:00
|
|
|
HotkeyAction::CycleAutonomy => hotkey_cycle_autonomy(mind),
|
2026-04-05 21:16:49 -04:00
|
|
|
HotkeyAction::AdjustSampling(param, delta) => hotkey_adjust_sampling(mind, param, delta),
|
|
|
|
|
}
|
|
|
|
|
dirty = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if dirty {
|
|
|
|
|
let key = pending_key.take();
|
|
|
|
|
let mut screen_action = None;
|
|
|
|
|
if active_screen == 0 {
|
|
|
|
|
terminal.draw(|f| {
|
|
|
|
|
let area = f.area();
|
|
|
|
|
screen_action = interact.tick(f, area, key, &mut app);
|
|
|
|
|
})?;
|
|
|
|
|
} else {
|
|
|
|
|
let screen = &mut screens[active_screen - 1];
|
|
|
|
|
terminal.draw(|f| {
|
|
|
|
|
let area = f.area();
|
|
|
|
|
screen_action = screen.tick(f, area, key, &mut app);
|
|
|
|
|
})?;
|
|
|
|
|
}
|
|
|
|
|
if let Some(action) = screen_action {
|
|
|
|
|
match action {
|
|
|
|
|
tui::ScreenAction::Switch(i) => { active_screen = i; dirty = true; continue; }
|
|
|
|
|
tui::ScreenAction::Hotkey(h) => app.hotkey_actions.push(h),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
dirty = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if app.should_quit {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tui::restore_terminal(&mut terminal)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 02:46:32 -04:00
|
|
|
// --- CLI ---
|
|
|
|
|
|
|
|
|
|
use clap::{Parser, Subcommand};
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
|
#[command(name = "consciousness", about = "Substrate-independent AI agent")]
|
|
|
|
|
pub struct CliArgs {
|
|
|
|
|
/// Select active backend ("anthropic" or "openrouter")
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
pub backend: Option<String>,
|
|
|
|
|
|
|
|
|
|
/// Model override
|
|
|
|
|
#[arg(short, long)]
|
|
|
|
|
pub model: Option<String>,
|
|
|
|
|
|
|
|
|
|
/// API key override
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
pub api_key: Option<String>,
|
|
|
|
|
|
|
|
|
|
/// Base URL override
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
pub api_base: Option<String>,
|
|
|
|
|
|
|
|
|
|
/// Enable debug logging
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
pub debug: bool,
|
|
|
|
|
|
|
|
|
|
/// Print effective config with provenance and exit
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
pub show_config: bool,
|
|
|
|
|
|
|
|
|
|
/// Override all prompt assembly with this file
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
pub system_prompt_file: Option<PathBuf>,
|
|
|
|
|
|
|
|
|
|
/// Project memory directory
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
pub memory_project: Option<PathBuf>,
|
|
|
|
|
|
|
|
|
|
/// Max consecutive DMN turns
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
pub dmn_max_turns: Option<u32>,
|
|
|
|
|
|
2026-04-04 23:06:25 -04:00
|
|
|
/// Disable background agents (surface, observe, scoring)
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
pub no_agents: bool,
|
|
|
|
|
|
2026-04-04 02:46:32 -04:00
|
|
|
#[command(subcommand)]
|
|
|
|
|
pub command: Option<SubCmd>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Subcommand, Debug)]
|
|
|
|
|
pub enum SubCmd {
|
|
|
|
|
/// Print new output since last read and exit
|
|
|
|
|
Read {
|
|
|
|
|
/// Stream output continuously instead of exiting
|
|
|
|
|
#[arg(short, long)]
|
|
|
|
|
follow: bool,
|
|
|
|
|
/// Block until a complete response is received, then exit
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
block: bool,
|
|
|
|
|
},
|
|
|
|
|
/// Send a message to the running agent
|
|
|
|
|
Write {
|
|
|
|
|
/// The message to send
|
|
|
|
|
message: Vec<String>,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
|
pub async fn main() {
|
|
|
|
|
let cli = CliArgs::parse();
|
|
|
|
|
|
|
|
|
|
if cli.show_config {
|
|
|
|
|
match crate::config::load_app(&cli) {
|
|
|
|
|
Ok((app, figment)) => crate::config::show_config(&app, &figment),
|
|
|
|
|
Err(e) => {
|
|
|
|
|
eprintln!("Error loading config: {:#}", e);
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 21:16:49 -04:00
|
|
|
if let Err(e) = start(cli).await {
|
2026-04-05 06:22:31 -04:00
|
|
|
let _ = ratatui::crossterm::terminal::disable_raw_mode();
|
|
|
|
|
let _ = ratatui::crossterm::execute!(
|
2026-04-04 02:46:32 -04:00
|
|
|
std::io::stdout(),
|
2026-04-05 06:22:31 -04:00
|
|
|
ratatui::crossterm::terminal::LeaveAlternateScreen
|
2026-04-04 02:46:32 -04:00
|
|
|
);
|
|
|
|
|
eprintln!("Error: {:#}", e);
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|