1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! Dual-channel tool execution helpers.
use anyhow::Result;
use serde_json::Value;
use tracing::{debug, warn};
use vtcode_commons::serde_helpers::json_to_string_pretty;
use crate::config::constants::tools;
use crate::tools::summarizers::{
Summarizer,
execution::BashSummarizer,
file_ops::{EditSummarizer, ReadSummarizer},
};
use crate::tools::tool_intent;
use super::{SplitToolResult, ToolRegistry};
impl ToolRegistry {
/// Execute tool with dual-channel output (Phase 4: Split Tool Results).
///
/// This method enables significant token savings by separating:
/// - `llm_content`: Concise summary sent to LLM context (token-optimized)
/// - `ui_content`: Rich output displayed to user (full details)
///
/// For tools with registered summarizers, this can achieve 90-97% token reduction
/// on tool outputs while preserving full details for the UI.
///
/// # Example
/// This snippet is illustrative; registry setup and argument construction are
/// omitted because they depend on the caller's tool inventory.
/// ```rust,ignore
/// let result = registry.execute_tool_dual("grep_file", args).await?;
/// // result.llm_content: "Found 127 matches in 15 files. Key: src/tools/grep.rs (3)"
/// // result.ui_content: [Full formatted output with all 127 matches]
/// // Savings: ~98% token reduction
/// ```
pub async fn execute_tool_dual(&self, name: &str, args: Value) -> Result<SplitToolResult> {
// Execute the tool using existing infrastructure
let result = self.execute_tool_ref(name, &args).await?;
// Convert Value to string for UI content
let ui_content = if result.is_string() {
result.as_str().unwrap_or("").to_string()
} else {
json_to_string_pretty(&result)
};
// Get canonical tool name for summarizer lookup
// Resolve alias through registration lookup first
let tool_name = if let Some(registration) = self.inventory.registration_for(name) {
registration.name().to_string()
} else {
name.to_string() // Fallback to original name if not found
};
// Check if we have a summarizer for this tool
match tool_name.as_str() {
tools::CODE_SEARCH => Ok(SplitToolResult::simple(tool_name.as_str(), ui_content)),
tools::UNIFIED_FILE => match tool_intent::file_operation_action(&args).unwrap_or("read") {
"read" => {
let mut metadata = args.clone();
if let Value::Object(map) = &mut metadata
&& !map.contains_key("file_path")
{
let inferred_path = args
.get("path")
.or_else(|| args.get("file_path"))
.or_else(|| args.get("filepath"))
.or_else(|| args.get("target_path"))
.and_then(Value::as_str)
.map(str::to_string);
if let Some(path) = inferred_path {
map.insert("file_path".to_string(), Value::String(path));
}
}
let summarizer = ReadSummarizer::default();
match summarizer.summarize(&ui_content, Some(&metadata)) {
Ok(llm_content) => {
let savings = summarizer.estimate_savings(&ui_content, &llm_content);
debug!(
tool = tools::UNIFIED_FILE,
action = "read",
ui_tokens = %savings.ui_tokens,
llm_tokens = %savings.llm_tokens,
savings_pct = %savings.savings_percent,
"Applied file_operation read summarization"
);
Ok(SplitToolResult::new(tool_name.as_str(), llm_content, ui_content))
}
Err(e) => {
warn!(
tool = tools::UNIFIED_FILE,
action = "read",
error = %e,
"Failed to summarize file_operation read output, using simple result"
);
Ok(SplitToolResult::simple(tool_name.as_str(), ui_content))
}
}
}
"write" | "edit" | "patch" | "move" | "copy" | "delete" => {
let summarizer = EditSummarizer::default();
match summarizer.summarize(&ui_content, None) {
Ok(llm_content) => {
let savings = summarizer.estimate_savings(&ui_content, &llm_content);
debug!(
tool = tools::UNIFIED_FILE,
action = "mutate",
ui_tokens = %savings.ui_tokens,
llm_tokens = %savings.llm_tokens,
savings_pct = %savings.savings_percent,
"Applied file_operation mutation summarization"
);
Ok(SplitToolResult::new(tool_name.as_str(), llm_content, ui_content))
}
Err(e) => {
warn!(
tool = tools::UNIFIED_FILE,
action = "mutate",
error = %e,
"Failed to summarize file_operation mutation output, using simple result"
);
Ok(SplitToolResult::simple(tool_name.as_str(), ui_content))
}
}
}
_ => Ok(SplitToolResult::simple(tool_name.as_str(), ui_content)),
},
tools::UNIFIED_EXEC | tools::EXEC_COMMAND => {
match tool_intent::command_session_action(&args).unwrap_or("run") {
"run" | "code" => {
let summarizer = BashSummarizer::default();
let metadata = args.as_object().map(|_| args.clone());
match summarizer.summarize(&ui_content, metadata.as_ref()) {
Ok(llm_content) => {
let savings = summarizer.estimate_savings(&ui_content, &llm_content);
debug!(
tool = tools::UNIFIED_EXEC,
action = "run",
ui_tokens = %savings.ui_tokens,
llm_tokens = %savings.llm_tokens,
savings_pct = %savings.savings_percent,
"Applied command_session summarization"
);
Ok(SplitToolResult::new(tool_name.as_str(), llm_content, ui_content))
}
Err(e) => {
warn!(
tool = tools::UNIFIED_EXEC,
action = "run",
error = %e,
"Failed to summarize command_session output, using simple result"
);
Ok(SplitToolResult::simple(tool_name.as_str(), ui_content))
}
}
}
_ => Ok(SplitToolResult::simple(tool_name.as_str(), ui_content)),
}
}
tools::READ_FILE => {
// Apply read file summarization
let summarizer = ReadSummarizer::default();
// Extract file path from args if available for better summary
let metadata = args.as_object().map(|_| args.clone());
match summarizer.summarize(&ui_content, metadata.as_ref()) {
Ok(llm_content) => {
let savings = summarizer.estimate_savings(&ui_content, &llm_content);
debug!(
tool = tools::READ_FILE,
ui_tokens = %savings.ui_tokens,
llm_tokens = %savings.llm_tokens,
savings_pct = %savings.savings_percent,
"Applied read file summarization"
);
Ok(SplitToolResult::new(tool_name.as_str(), llm_content, ui_content))
}
Err(e) => {
warn!(
tool = tools::READ_FILE,
error = %e,
"Failed to summarize read output, using simple result"
);
Ok(SplitToolResult::simple(tool_name.as_str(), ui_content))
}
}
}
tools::RUN_PTY_CMD => {
// Apply bash execution summarization
let summarizer = BashSummarizer::default();
// Pass command info from args if available
let metadata = args.as_object().map(|_| args.clone());
match summarizer.summarize(&ui_content, metadata.as_ref()) {
Ok(llm_content) => {
let savings = summarizer.estimate_savings(&ui_content, &llm_content);
debug!(
tool = tools::RUN_PTY_CMD,
ui_tokens = %savings.ui_tokens,
llm_tokens = %savings.llm_tokens,
savings_pct = %savings.savings_percent,
"Applied bash summarization"
);
Ok(SplitToolResult::new(tool_name.as_str(), llm_content, ui_content))
}
Err(e) => {
warn!(
tool = tools::RUN_PTY_CMD,
error = %e,
"Failed to summarize bash output, using simple result"
);
Ok(SplitToolResult::simple(tool_name.as_str(), ui_content))
}
}
}
tools::WRITE_FILE | tools::EDIT_FILE | tools::APPLY_PATCH => {
// Apply edit/write file summarization
let summarizer = EditSummarizer::default();
match summarizer.summarize(&ui_content, None) {
Ok(llm_content) => {
let savings = summarizer.estimate_savings(&ui_content, &llm_content);
debug!(
tool = tool_name.as_str(),
ui_tokens = %savings.ui_tokens,
llm_tokens = %savings.llm_tokens,
savings_pct = %savings.savings_percent,
"Applied edit summarization"
);
Ok(SplitToolResult::new(tool_name.as_str(), llm_content, ui_content))
}
Err(e) => {
warn!(
tool = tool_name.as_str(),
error = %e,
"Failed to summarize edit output, using simple result"
);
Ok(SplitToolResult::simple(tool_name.as_str(), ui_content))
}
}
}
_ => {
// No summarizer registered, use same content for both channels
Ok(SplitToolResult::simple(tool_name.as_str(), ui_content))
}
}
}
}