use std::path::{Path, PathBuf};
use super::PromptSave;
const UNSENT_PROMPT_FILE: &str = "unsent-prompt.txt";
pub(super) struct UnsentPrompt(PathBuf);
impl UnsentPrompt {
pub(super) fn in_folder(artifacts: &Path) -> Self {
Self(artifacts.join(UNSENT_PROMPT_FILE))
}
pub(super) fn track(&self, save: &PromptSave, sent: &str) {
let written = match save {
PromptSave::First(_) => {
crate::config_writer::write_bytes_atomically(&self.0, sent.as_bytes())
}
PromptSave::OnceSent(_) => self.clear(),
PromptSave::Saved => Ok(()),
};
if let Err(error) = written {
tracing::warn!(
path = %self.0.display(),
%error,
"could not note the prompt the turn's run has yet to send"
);
}
}
pub(super) fn read(&self) -> Option<String> {
std::fs::read_to_string(&self.0).ok()
}
pub(super) fn sent(&self) {
if let Err(error) = self.clear() {
tracing::warn!(
path = %self.0.display(),
%error,
"could not clear the prompt the turn's run sent"
);
}
}
fn clear(&self) -> std::io::Result<()> {
match std::fs::remove_file(&self.0) {
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
removed => removed,
}
}
}