use serde_json::Value;
pub const CALL_INPUT_CHARS: usize = 600;
pub const CALL_OUTPUT_CHARS: usize = 1_500;
pub fn call_input(arguments: &Value) -> String {
let mut value = match arguments {
Value::String(text) => serde_json::from_str(text).unwrap_or_else(|_| arguments.clone()),
other => other.clone(),
};
if value.as_object().is_some_and(|o| o.is_empty()) || value.is_null() {
return String::new();
}
crate::sanitize::redact_json_for_owner(&mut value);
let text = match &value {
Value::String(text) => text.clone(),
other => serde_json::to_string_pretty(other).unwrap_or_default(),
};
crate::text::truncate_with_note(&text, CALL_INPUT_CHARS)
}
pub fn call_output(content: &str) -> String {
let mut text = content.trim();
if let Some(rest) = text.strip_prefix(aidaemon_policies::UNTRUSTED_PREFIX)
&& let Some(rest) = rest
.trim_end()
.strip_suffix(aidaemon_policies::UNTRUSTED_SUFFIX)
{
text = rest.split_once('\n').map_or("", |(_, data)| data).trim();
}
let text = crate::sanitize::redact_for_owner(text);
crate::text::truncate_with_note(&text, CALL_OUTPUT_CHARS)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn arguments_and_results_are_shown_alike() {
assert_eq!(call_input(&serde_json::json!({})), "");
assert!(
call_input(&serde_json::json!("{\"command\":\"ls -la\"}"))
.contains("\"command\": \"ls -la\"")
);
let framed = format!(
"{}terminal]\nhello\n{}",
aidaemon_policies::UNTRUSTED_PREFIX,
aidaemon_policies::UNTRUSTED_SUFFIX
);
assert_eq!(call_output(&framed), "hello");
}
}