use super::{
DepositError, USER_SENDER, deposit,
launch::{AdvanceLauncher, Launcher, ProbeOutcome, probe_and_launch},
};
use crate::prompt::{Clock, SystemClock};
use crate::template::GitRunner;
use std::ffi::OsStr;
use std::io;
use std::path::Path;
#[derive(Debug, thiserror::Error)]
pub enum MessageError {
#[error(transparent)]
Deposit(#[from] DepositError),
#[error(transparent)]
Layout(#[from] crate::workspace::LayoutError),
#[error("probe executor lock: {0}")]
Probe(#[source] io::Error),
#[error(transparent)]
UnknownAgent(#[from] crate::workspace::UnknownAgent),
}
pub fn cli_message(
workspace: &Path,
agent_id: &str,
content: &str,
sender: &str,
clock: &dyn Clock,
launcher: &dyn Launcher,
git: &dyn GitRunner,
) -> Result<ProbeOutcome, MessageError> {
deposit(workspace, agent_id, sender, content, clock, git)?;
probe_and_launch(workspace, agent_id, launcher).map_err(MessageError::Probe)
}
pub fn cli_run(
workspace: &Path,
agent: &str,
content: &str,
conv_branch: Option<&OsStr>,
driver_target: &Path,
) -> Result<ProbeOutcome, MessageError> {
let git = crate::template::RealGit::new();
crate::workspace::require(workspace)?;
crate::workspace::require_agent(
workspace,
agent,
"a message is addressed to an existing agent, by id or unique name (ARCH §2.11)",
&git,
)?;
let sender = resolve_cli_sender(conv_branch);
let launcher = AdvanceLauncher::with_exe(driver_target.to_path_buf());
cli_message(
workspace,
agent,
content,
&sender,
&SystemClock,
&launcher,
&git,
)
}
pub fn resolve_cli_sender(branch_env: Option<&OsStr>) -> String {
match branch_env {
Some(v) if !v.is_empty() => v.to_string_lossy().into_owned(),
_ => USER_SENDER.to_string(),
}
}