use ag_contracts::{ActivityEvent, ActivityFacts, LineRange};
use serde_json::Value;
const FACT_LIMIT: usize = 240;
const PREVIEW_LIMIT: usize = 4096;
const TRUNCATED: &str = "\n… [truncated]";
const TARGET_KEYS: [&str; 12] = [
"pattern",
"query",
"Query",
"command",
"CommandLine",
"file_path",
"path",
"AbsolutePath",
"DirectoryPath",
"TargetFile",
"target_file",
"SearchPath",
];
#[derive(Default)]
pub(super) struct ActivityDetails {
pub exit_code: Option<i64>,
pub facts: ActivityFacts,
pub input: Option<String>,
pub output: Option<String>,
}
impl ActivityDetails {
pub(super) fn new(
input: Option<&Value>,
output: Option<&Value>,
exit_code: Option<i64>,
) -> Self {
let input = input.filter(|value| !value.is_null());
Self {
exit_code,
facts: ActivityFacts {
target: input.and_then(target),
..ActivityFacts::default()
},
input: input.map(|value| {
let value = [
"command",
"CommandLine",
"file_path",
"path",
"TargetFile",
"target_file",
]
.iter()
.find_map(|key| value.get(*key))
.unwrap_or(value);
match value.as_str() {
Some(text) => bounded(text),
None => bounded(&value.to_string()),
}
}),
output: output.and_then(preview),
}
}
pub(super) fn apply(self, event: &mut ActivityEvent) {
let facts = &mut event.facts;
facts.diff_stat = self.facts.diff_stat.or(facts.diff_stat);
facts.duration = self.facts.duration.or(facts.duration);
facts.failure = self.facts.failure.or(facts.failure);
facts.lines = self.facts.lines.or(facts.lines);
facts.reason = self.facts.reason.or(facts.reason.take());
facts.target = self.facts.target.or(facts.target.take());
event.input = self.input.or(event.input.take());
event.output = self.output.or(event.output.take());
event.exit_code = self.exit_code.or(event.exit_code);
}
}
pub(super) fn first_line(text: &str) -> Option<String> {
text.lines()
.map(str::trim)
.find(|line| !line.is_empty())
.and_then(bounded_fact)
}
pub(super) fn one_line(text: &str) -> Option<String> {
bounded_fact(&text.split_whitespace().collect::<Vec<_>>().join(" "))
}
pub(super) fn line_range(offset: Option<u64>, limit: Option<u64>) -> Option<LineRange> {
let offset = offset.filter(|offset| *offset > 0);
let limit = limit.filter(|limit| *limit > 0);
if offset.is_none() && limit.is_none() {
return None;
}
let first = offset.unwrap_or(1);
Some(LineRange {
first,
last: limit.map(|limit| first.saturating_add(limit - 1)),
})
}
fn target(input: &Value) -> Option<String> {
let value = if input.is_object() {
TARGET_KEYS.iter().find_map(|key| input.get(*key))?
} else {
input
};
value.as_str().and_then(one_line)
}
fn bounded_fact(line: &str) -> Option<String> {
let mut characters = line.chars().filter(|character| !character.is_control());
let mut bounded: String = characters.by_ref().take(FACT_LIMIT).collect();
if characters.next().is_some() {
bounded.push('…');
}
(!bounded.is_empty()).then_some(bounded)
}
pub(super) fn append_output(previous: Option<&str>, delta: &str) -> String {
let previous = previous.unwrap_or_default();
if previous.ends_with(TRUNCATED) {
return previous.to_string();
}
bounded(&format!("{previous}{}", bounded(delta)))
}
fn preview(value: &Value) -> Option<String> {
match value {
Value::Null => None,
Value::String(text) => Some(bounded(text)),
Value::Array(blocks) if blocks.iter().all(is_content_block) => {
let mut output = String::new();
for block in blocks {
let text = block
.as_str()
.or_else(|| block.get("text").and_then(Value::as_str))
.or_else(|| block.pointer("/content/text").and_then(Value::as_str));
if let Some(text) = text {
if !output.is_empty() {
output.push('\n');
}
output = append_output(Some(&output), text);
}
}
(!output.is_empty()).then_some(output)
}
_ => Some(bounded(&value.to_string())),
}
}
fn is_content_block(value: &Value) -> bool {
value.is_string()
|| value.get("text").is_some_and(Value::is_string)
|| value.pointer("/content/text").is_some_and(Value::is_string)
|| matches!(
value.get("type").and_then(Value::as_str),
Some("image" | "audio" | "resource" | "resource_link")
)
}
fn bounded(text: &str) -> String {
let mut characters = text
.chars()
.filter(|character| !character.is_control() || matches!(character, '\n' | '\t'));
let mut output: String = characters.by_ref().take(PREVIEW_LIMIT).collect();
if characters.next().is_some() {
output.push_str(TRUNCATED);
}
output
}
#[cfg(test)]
#[path = "activity_content_test.rs"]
mod tests;