diff --git a/src/mind/mod.rs b/src/mind/mod.rs index 06de469..8f77a47 100644 --- a/src/mind/mod.rs +++ b/src/mind/mod.rs @@ -182,12 +182,30 @@ pub struct Mind { impl Mind { pub fn new( - agent: Arc>, - shared: SharedMindState, config: SessionConfig, ui_tx: ui_channel::UiSender, turn_tx: mpsc::Sender<(Result, 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, 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,