use crate::interface::tool_middleware::ToolCall;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResultImage {
pub media_type: String,
pub data: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToolResultDraft {
pub text: String,
pub images: Vec<ResultImage>,
pub is_error: bool,
}
pub trait ToolResultTransformer: Send + Sync {
fn transform(&self, call: &ToolCall, draft: &mut ToolResultDraft);
}
pub fn apply(
transformers: &[std::sync::Arc<dyn ToolResultTransformer>],
call: &ToolCall,
draft: &mut ToolResultDraft,
) {
for t in transformers {
t.transform(call, draft);
}
}
pub struct TruncateText {
pub max_bytes: usize,
}
impl ToolResultTransformer for TruncateText {
fn transform(&self, _call: &ToolCall, draft: &mut ToolResultDraft) {
if draft.text.len() <= self.max_bytes {
return;
}
let kept = crate::text::truncate_at_char_boundary(&draft.text, self.max_bytes);
let dropped = draft.text.len() - kept.len();
draft.text = format!("{kept}\n… [{dropped} bytes truncated]");
}
}
pub struct RedactLiterals {
secrets: Vec<String>,
replacement: String,
}
impl RedactLiterals {
pub fn new(secrets: impl IntoIterator<Item = impl Into<String>>) -> Self {
Self {
secrets: secrets
.into_iter()
.map(Into::into)
.filter(|s: &String| !s.is_empty())
.collect(),
replacement: "<redacted>".to_string(),
}
}
}
impl ToolResultTransformer for RedactLiterals {
fn transform(&self, _call: &ToolCall, draft: &mut ToolResultDraft) {
for secret in &self.secrets {
if draft.text.contains(secret.as_str()) {
draft.text = draft.text.replace(secret.as_str(), &self.replacement);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;
fn call() -> ToolCall {
ToolCall::new("Bash", serde_json::json!({"command": "env"}))
}
fn draft(text: &str) -> ToolResultDraft {
ToolResultDraft {
text: text.to_string(),
images: Vec::new(),
is_error: false,
}
}
#[test]
fn truncation_says_where_it_cut() {
let t = TruncateText { max_bytes: 10 };
let mut d = draft("0123456789abcdef");
t.transform(&call(), &mut d);
assert!(d.text.starts_with("0123456789"));
assert!(d.text.contains("truncated"), "{}", d.text);
}
#[test]
fn truncation_leaves_a_short_result_exactly_as_it_was() {
let t = TruncateText { max_bytes: 100 };
let mut d = draft("short");
t.transform(&call(), &mut d);
assert_eq!(d.text, "short");
}
#[test]
fn truncation_never_splits_a_character() {
let t = TruncateText { max_bytes: 4 };
let mut d = draft("日本語のテキスト");
t.transform(&call(), &mut d);
assert!(d.text.starts_with("日"), "{}", d.text);
}
#[test]
fn a_known_secret_does_not_reach_the_model() {
let t = RedactLiterals::new(["sk-live-abc123"]);
let mut d = draft("ANTHROPIC_API_KEY=sk-live-abc123\nOTHER=fine");
t.transform(&call(), &mut d);
assert!(!d.text.contains("sk-live-abc123"), "{}", d.text);
assert!(d.text.contains("<redacted>"), "{}", d.text);
assert!(
d.text.contains("OTHER=fine"),
"the rest must survive: {}",
d.text
);
}
#[test]
fn a_failure_is_transformed_too() {
let t = RedactLiterals::new(["sk-live-abc123"]);
let mut d = ToolResultDraft {
text: "command failed: bad token sk-live-abc123".into(),
images: Vec::new(),
is_error: true,
};
t.transform(&call(), &mut d);
assert!(!d.text.contains("sk-live-abc123"));
}
#[test]
fn transformers_run_in_order() {
let chain: Vec<Arc<dyn ToolResultTransformer>> = vec![
Arc::new(RedactLiterals::new(["secret"])),
Arc::new(TruncateText { max_bytes: 12 }),
];
let mut d = draft("a secret and a great deal more text");
apply(&chain, &call(), &mut d);
assert!(!d.text.contains("secret"));
assert!(d.text.contains("truncated"));
}
#[test]
fn an_empty_chain_changes_nothing() {
let mut d = draft("untouched");
apply(&[], &call(), &mut d);
assert_eq!(d, draft("untouched"));
}
}