pub mod filter;
mod reduce;
#[cfg(test)]
mod tests;
use serde_json::{Map, Value};
use crate::cli::{AgentArgs, Cli};
use crate::error::AppResult;
pub use filter::{Filter, FilterOp};
use reduce::{
array_mut, dedupe, locate_array_key, project, reconcile_counts, sort_by, truncate_strings,
};
pub const RESULT_ARRAY_KEYS: &[&str] = &["hits", "results", "items", "checks", "commands", "data"];
pub const EMITTED_COUNT_KEYS: &[&str] = &["emitted"];
#[derive(Debug, Clone, Default)]
pub struct AgentOps {
select: Vec<String>,
filters: Vec<Filter>,
sort_by: Option<String>,
dedupe_by: Option<String>,
max_items: Option<usize>,
count_only: bool,
truncate_content: Option<usize>,
max_output_bytes: Option<u64>,
}
#[derive(Debug, Clone, Copy, Default)]
struct Surface {
input_count: usize,
output_count: usize,
limited: bool,
content_truncated: bool,
output_truncated: bool,
}
impl AgentOps {
pub fn from_cli(cli: &Cli) -> AppResult<Self> {
Self::from_args(&cli.agent)
}
pub fn from_args(args: &AgentArgs) -> AppResult<Self> {
let select: Vec<String> = args
.select
.iter()
.flat_map(|s| s.split(','))
.map(str::trim)
.filter(|s| !s.is_empty())
.map(ToString::to_string)
.collect();
let mut filters = Vec::with_capacity(args.filter.len());
for expr in &args.filter {
filters.push(Filter::parse(expr)?);
}
let non_empty = |s: &Option<String>| s.clone().filter(|v| !v.is_empty());
Ok(Self {
select,
filters,
sort_by: non_empty(&args.sort_by),
dedupe_by: non_empty(&args.dedupe_by),
max_items: args.max_items,
count_only: args.count_only,
truncate_content: args.truncate_content,
max_output_bytes: args.max_output_bytes,
})
}
pub fn is_active(&self) -> bool {
!self.select.is_empty()
|| !self.filters.is_empty()
|| self.sort_by.is_some()
|| self.dedupe_by.is_some()
|| self.max_items.is_some()
|| self.count_only
|| self.truncate_content.is_some()
}
pub fn apply_to_bytes(&self, raw: &[u8]) -> Vec<u8> {
if raw.is_empty() {
return Vec::new();
}
let Ok(mut env) = serde_json::from_slice::<Value>(raw) else {
return raw.to_vec();
};
if !env.is_object() || env.get("ok").and_then(Value::as_bool) == Some(false) {
return raw.to_vec();
}
self.apply_to_envelope(&mut env);
match serde_json::to_vec(&env) {
Ok(mut buf) => {
buf.push(b'\n');
buf
}
Err(_) => raw.to_vec(),
}
}
pub fn apply_to_envelope(&self, env: &mut Value) {
if env.get("ok").and_then(Value::as_bool) == Some(false) {
return;
}
let mut surface = Surface::default();
let Some(data) = env.get_mut("data") else {
return;
};
let array_key = locate_array_key(data);
let strip: Vec<&str> = array_key.as_deref().into_iter().collect();
if let Some(list) = array_mut(data, array_key.as_deref()) {
surface.input_count = list.len();
if !self.filters.is_empty() {
list.retain(|el| self.filters.iter().all(|f| f.matches(el, &strip)));
}
if let Some(key) = &self.sort_by {
sort_by(list, key, &strip);
}
if let Some(key) = &self.dedupe_by {
dedupe(list, key, &strip);
}
if let Some(max) = self.max_items {
surface.limited = list.len() > max;
list.truncate(max);
}
if !self.select.is_empty() {
for el in list.iter_mut() {
*el = project(el, &self.select, &strip);
}
}
surface.output_count = list.len();
reconcile_counts(data, surface.output_count);
} else {
surface.input_count = 1;
surface.output_count = 1;
if !self.select.is_empty() {
*data = project(data, &self.select, &strip);
}
}
if self.count_only {
let count = surface.output_count;
*data = Value::Object(Map::from_iter([("count".to_string(), Value::from(count))]));
}
if let Some(limit) = self.truncate_content {
surface.content_truncated = truncate_strings(data, limit);
}
if let Some(obj) = env.as_object_mut() {
obj.insert("agent_surface".to_string(), surface.to_value());
}
if let Some(max) = self.max_output_bytes {
surface.output_truncated = self.enforce_output_budget(env, array_key.as_deref(), max);
if surface.output_truncated {
if let Some(data) = env.get_mut("data") {
if let Some(list) = array_mut(data, array_key.as_deref()) {
surface.output_count = list.len();
}
reconcile_counts(data, surface.output_count);
}
}
if let Some(obj) = env.as_object_mut() {
obj.insert("agent_surface".to_string(), surface.to_value());
}
}
}
fn enforce_output_budget(&self, env: &mut Value, array_key: Option<&str>, max: u64) -> bool {
let mut dropped = false;
loop {
let len = match serde_json::to_vec(env) {
Ok(buf) => buf.len() as u64,
Err(_) => return dropped,
};
if len <= max {
return dropped;
}
let Some(data) = env.get_mut("data") else {
return dropped;
};
let Some(list) = array_mut(data, array_key) else {
return dropped;
};
if list.pop().is_none() {
return true;
}
dropped = true;
}
}
}
impl Surface {
fn to_value(self) -> Value {
Value::Object(Map::from_iter([
("input_count".to_string(), Value::from(self.input_count)),
("output_count".to_string(), Value::from(self.output_count)),
("limited".to_string(), Value::Bool(self.limited)),
(
"content_truncated".to_string(),
Value::Bool(self.content_truncated),
),
(
"output_truncated".to_string(),
Value::Bool(self.output_truncated),
),
]))
}
}