use std::path::{Path, PathBuf};
use agent_client_protocol::v1::schema::Meta;
use rho_protocol::SubagentRef;
use serde_json::Value;
pub(super) const DELETED_FILE: &str = "rho/fileDeleted";
pub(super) const MOVED_FROM: &str = "rho/movedFrom";
pub(super) const TOOL_CANCELLED: &str = "rho/toolCancelled";
pub(super) const SUBAGENT: &str = "rho/subagent";
pub(super) fn flag(key: &str) -> Meta {
Meta::from_iter([(key.to_owned(), Value::Bool(true))])
}
pub(super) fn path(key: &str, path: &Path) -> Option<Meta> {
let path = path.to_str()?;
Some(Meta::from_iter([(key.to_owned(), Value::from(path))]))
}
pub(super) fn subagent(child: &SubagentRef) -> Option<Meta> {
let child = serde_json::to_value(child).ok()?;
Some(Meta::from_iter([(SUBAGENT.to_owned(), child)]))
}
pub(super) fn subagent_in(meta: Option<&Meta>) -> Option<SubagentRef> {
serde_json::from_value(meta?.get(SUBAGENT)?.clone()).ok()
}
pub(super) fn take_subagent(meta: &mut Option<Meta>) -> Option<SubagentRef> {
serde_json::from_value(take(meta, SUBAGENT)?).ok()
}
pub(super) fn take_flag(meta: &mut Option<Meta>, key: &str) -> bool {
take(meta, key).is_some_and(|marker| marker == true)
}
pub(super) fn take_path(meta: &mut Option<Meta>, key: &str) -> Option<PathBuf> {
take(meta, key)?.as_str().map(PathBuf::from)
}
fn take(meta: &mut Option<Meta>, key: &str) -> Option<Value> {
let marker = meta.as_mut()?.remove(key);
if meta.as_ref().is_some_and(Meta::is_empty) {
*meta = None;
}
marker
}