use std::fmt;
use std::io;
use std::path::{Path, PathBuf};
use crate::conversation::Error;
#[derive(Debug)]
struct Held {
message: String,
turn: PathBuf,
}
impl fmt::Display for Held {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let message = &self.message;
write!(f, "the turn is held and will not be sent again: {message}")
}
}
impl std::error::Error for Held {}
pub fn error(directory: &Path, n: u64, message: &str) -> Error {
let held = Held {
message: message.to_string(),
turn: directory.join(format!("{n}.jsonl")),
};
Error::Io(io::Error::other(held))
}
pub fn hint(error: &Error) -> Option<String> {
let Error::Io(error) = error else {
return None;
};
let held = error.get_ref()?.downcast_ref::<Held>()?;
let reason = held.turn.with_extension("reason");
Some(format!(
"the record never blocks the agent; the turn stays in {} with its reason in {}, \
and nothing sends it again by itself",
held.turn.display(),
reason.display()
))
}