use crate::message::{ConversationMessage, MessageKind};
use crate::text;
use serde_json::Value;
pub fn message_files(msg: &ConversationMessage) -> Vec<String> {
match &msg.kind {
MessageKind::AssistantResponse(ar) => ar
.tool_calls
.iter()
.filter_map(|tc| path_argument(&tc.arguments))
.collect(),
_ => Vec::new(),
}
}
pub fn searchable_text(msg: &ConversationMessage) -> String {
match &msg.kind {
MessageKind::TextContent(tc) => text::sanitize(&tc.text),
MessageKind::AssistantResponse(ar) => {
let mut parts: Vec<String> = ar.thinking.clone();
if !ar.text.is_empty() {
parts.push(ar.text.clone());
}
for tc in &ar.tool_calls {
parts.push(format!(
"{} {}",
tc.name,
summarize_tool_args(&tc.arguments)
));
}
text::sanitize(&text::join_lines(&parts, " "))
}
MessageKind::ToolResultData(tr) => {
if tr.content.is_empty() {
text::sanitize(&tr.tool_name)
} else {
text::sanitize(&format!("{} {}", tr.tool_name, tr.content))
}
}
MessageKind::BashOutput(bo) => {
if bo.command.is_empty() {
text::sanitize(&bo.output)
} else if bo.output.is_empty() {
text::sanitize(&bo.command)
} else {
text::sanitize(&format!("{} {}", bo.command, bo.output))
}
}
}
}
pub fn summarize_tool_args(arguments: &Value) -> String {
if let Some(obj) = arguments.as_object() {
if let Some(path) = path_argument(arguments) {
return format!("path={path}");
}
for key in &["command", "query", "pattern", "description"] {
if let Some(val) = obj.get(*key) {
if let Some(s) = val.as_str() {
return format!("{}={}", key, text::clip(s, 160));
}
}
}
if obj.is_empty() {
return String::new();
}
let mut keys: Vec<&str> = obj.keys().map(std::string::String::as_str).collect();
keys.sort_unstable();
return keys.join(", ");
}
if let Some(s) = arguments.as_str() {
return text::clip(s, 160);
}
if arguments.is_null() {
return String::new();
}
text::clip(&arguments.to_string(), 160)
}
pub fn path_argument(arguments: &Value) -> Option<String> {
let obj = arguments.as_object()?;
for key in &["path", "file_path", "filePath", "file"] {
if let Some(val) = obj.get(*key) {
if let Some(s) = val.as_str() {
return Some(s.to_string());
}
}
}
None
}