use crate::AppModel;
use crate::actions::stop_enabled;
use crate::boundary::{Action, reply};
use crate::cli_outbound::Cli;
use crate::git_tree::Agent;
use std::path::Path;
use super::ShellState;
pub(super) fn stop_selected(model: &mut AppModel, state: &mut ShellState, lernie: &Cli, bl: &Cli) {
let (Some(ws), Some(agent)) = (
model.focused_workspace().map(Path::to_path_buf),
model.focused_agent().map(|a| a.agent_id.clone()),
) else {
return;
};
let agents: Vec<Agent> = model
.focused_tree()
.map(|t| t.agents.clone())
.unwrap_or_default();
if !stop_enabled(Some(&agent), &agents) {
return;
}
stop_agent(model, lernie, bl, &ws, &agent, state.actions.stop_children);
}
pub(super) fn stop_agent(
model: &mut AppModel,
lernie: &Cli,
bl: &Cli,
ws: &Path,
agent: &str,
children: bool,
) {
let deps = model.boundary_deps(lernie, bl);
let action = Action::Stop {
workspace: ws.to_path_buf(),
agent: agent.to_owned(),
children,
};
let stopped = model.dispatch(&deps, &super::now_ts(), &action);
after_lernie(&stopped, model);
}
pub(super) fn scan_focused(model: &mut AppModel, lernie: &Cli, bl: &Cli) {
let Some(ws) = model.focused_workspace().map(Path::to_path_buf) else {
return;
};
scan_ws(model, lernie, bl, &ws);
}
pub(super) fn scan_ws(model: &mut AppModel, lernie: &Cli, bl: &Cli, ws: &Path) {
let deps = model.boundary_deps(lernie, bl);
let action = Action::Scan {
workspace: ws.to_path_buf(),
};
let scanned = model.dispatch(&deps, &super::now_ts(), &action);
after_lernie(&scanned, model);
}
pub(super) fn message(
model: &mut AppModel,
lernie: &Cli,
bl: &Cli,
ws: &Path,
agent: &str,
content: &str,
) -> bool {
let deps = model.boundary_deps(lernie, bl);
let action = Action::Message {
workspace: ws.to_path_buf(),
agent: agent.to_owned(),
content: content.to_owned(),
};
let sent = model.dispatch(&deps, &super::now_ts(), &action);
let cleared = after_lernie(&sent, model);
if cleared {
model.await_message(ws, agent, content);
}
cleared
}
pub(super) fn answer_hold(
model: &mut AppModel,
lernie: &Cli,
bl: &Cli,
ws: &Path,
agent: &str,
ruling: crate::control::judge::Ruling,
) {
let deps = model.boundary_deps(lernie, bl);
let action = Action::AnswerHold {
workspace: ws.to_path_buf(),
agent: agent.to_owned(),
ruling,
};
let answered = model.dispatch(&deps, &super::now_ts(), &action);
after_lernie(&answered, model);
}
pub(super) fn after_lernie(result: &Result<reply::Reply, String>, model: &mut AppModel) -> bool {
model.after_lernie_verb();
reply::cleared(result)
}