use std::io::Write;
use std::path::Path;
use serde_json::Value;
pub struct OpLog {
sink: Box<dyn Write>,
label: String,
}
impl OpLog {
pub fn create(path: &Path) -> std::io::Result<OpLog> {
let file = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(path)?;
Ok(OpLog {
sink: Box::new(file),
label: path.display().to_string(),
})
}
pub fn from_writer(writer: impl Write + 'static, label: &str) -> OpLog {
OpLog {
sink: Box::new(writer),
label: label.to_string(),
}
}
pub(crate) fn append(&mut self, entry: &Value) {
let mut line = entry.to_string().into_bytes();
line.push(b'\n');
if let Err(e) = self.sink.write_all(&line).and_then(|()| self.sink.flush()) {
eprintln!(
"embedmind: op-log write to {} failed ({e}); tool call unaffected",
self.label
);
}
}
}