mind: absorb agent creation into Mind::new()

Mind::new() takes config + ui_tx + turn_tx, creates the agent,
conversation log, shared state internally. The top-level run()
is now just: load config, create channels, Mind::new, init, spawn,
run event_loop.

Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2026-04-05 04:20:49 -04:00
parent 91033fe754
commit e449cda40f

View file

@ -182,12 +182,30 @@ pub struct Mind {
impl Mind {
pub fn new(
agent: Arc<Mutex<Agent>>,
shared: SharedMindState,
config: SessionConfig,
ui_tx: ui_channel::UiSender,
turn_tx: mpsc::Sender<(Result<TurnResult>, StreamTarget)>,
) -> Self {
let shared_context = ui_channel::shared_context_state();
let shared_active_tools = ui_channel::shared_active_tools();
let client = ApiClient::new(&config.api_base, &config.api_key, &config.model);
let conversation_log = log::ConversationLog::new(
config.session_dir.join("conversation.jsonl"),
).ok();
let agent = Arc::new(Mutex::new(Agent::new(
client,
config.system_prompt.clone(),
config.context_parts.clone(),
config.app.clone(),
config.prompt_file.clone(),
conversation_log,
shared_context,
shared_active_tools,
)));
let shared = shared_mind_state(config.app.dmn.max_turns);
let (turn_watch, _) = tokio::sync::watch::channel(false);
Self { agent, shared, config, ui_tx, turn_tx, turn_handle: None, turn_watch }
}
@ -375,46 +393,23 @@ pub async fn run(cli: crate::user::CliArgs) -> Result<()> {
unsafe { std::env::set_var("POC_DEBUG", "1") };
}
// Create UI channel
let (ui_tx, ui_rx) = ui_channel::channel();
// Shared state
let shared_context = ui_channel::shared_context_state();
let shared_active_tools = ui_channel::shared_active_tools();
let client = ApiClient::new(&config.api_base, &config.api_key, &config.model);
let conversation_log = log::ConversationLog::new(
config.session_dir.join("conversation.jsonl"),
).ok();
let agent = Arc::new(Mutex::new(Agent::new(
client,
config.system_prompt.clone(),
config.context_parts.clone(),
config.app.clone(),
config.prompt_file.clone(),
conversation_log,
shared_context.clone(),
shared_active_tools.clone(),
)));
let (turn_tx, turn_rx) = mpsc::channel::<(Result<TurnResult>, StreamTarget)>(1);
let (mind_tx, mind_rx) = tokio::sync::mpsc::unbounded_channel();
let shared_mind = shared_mind_state(config.app.dmn.max_turns);
let mut mind = Mind::new(agent, shared_mind.clone(), config, ui_tx.clone(), turn_tx);
let mut mind = Mind::new(config, ui_tx.clone(), turn_tx);
mind.init().await;
let ui_agent = mind.agent.clone();
let shared_mind = mind.shared.clone();
let shared_context = mind.agent.lock().await.shared_context.clone();
let shared_active_tools = mind.agent.lock().await.active_tools.clone();
let turn_watch = mind.turn_watch();
// Spawn Mind event loop
tokio::spawn(async move {
mind.run(mind_rx, turn_rx).await;
});
// Run UI event loop
crate::user::event_loop::run(
tui::App::new(String::new(), shared_context, shared_active_tools),
ui_agent, shared_mind, turn_watch, mind_tx, ui_tx, ui_rx,