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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//! Dual-channel tool execution helpers.
use anyhow::Result;
use serde_json::Value;
use tracing::{debug, warn};
use crate::config::constants::tools;
use crate::tools::summarizers::{
Summarizer,
execution::BashSummarizer,
file_ops::{EditSummarizer, ReadSummarizer},
search::{GrepSummarizer, ListSummarizer},
};
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
/// ```rust,no_run
/// 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 {
serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string())
};
// 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::UNIFIED_SEARCH => {
match tool_intent::unified_search_action(&args).unwrap_or("grep") {
"grep" => {
let summarizer = GrepSummarizer::default();
match summarizer.summarize(&ui_content, None) {
Ok(llm_content) => {
debug!(
tool = tools::UNIFIED_SEARCH,
action = "grep",
ui_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).1,
llm_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).0,
savings_pct = %summarizer.estimate_savings(&ui_content, &llm_content).2,
"Applied grep summarization"
);
Ok(SplitToolResult::new(
tool_name.as_str(),
llm_content,
ui_content,
))
}
Err(e) => {
warn!(
tool = tools::UNIFIED_SEARCH,
action = "grep",
error = %e,
"Failed to summarize grep output, using simple result"
);
Ok(SplitToolResult::simple(tool_name.as_str(), ui_content))
}
}
}
"list" => {
let summarizer = ListSummarizer::default();
match summarizer.summarize(&ui_content, None) {
Ok(llm_content) => {
debug!(
tool = tools::UNIFIED_SEARCH,
action = "list",
ui_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).1,
llm_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).0,
savings_pct = %summarizer.estimate_savings(&ui_content, &llm_content).2,
"Applied list summarization"
);
Ok(SplitToolResult::new(
tool_name.as_str(),
llm_content,
ui_content,
))
}
Err(e) => {
warn!(
tool = tools::UNIFIED_SEARCH,
action = "list",
error = %e,
"Failed to summarize list output, using simple result"
);
Ok(SplitToolResult::simple(tool_name.as_str(), ui_content))
}
}
}
_ => Ok(SplitToolResult::simple(tool_name.as_str(), ui_content)),
}
}
tools::UNIFIED_FILE => {
match tool_intent::unified_file_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) => {
debug!(
tool = tools::UNIFIED_FILE,
action = "read",
ui_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).1,
llm_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).0,
savings_pct = %summarizer.estimate_savings(&ui_content, &llm_content).2,
"Applied unified_file 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 unified_file 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) => {
debug!(
tool = tools::UNIFIED_FILE,
action = "mutate",
ui_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).1,
llm_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).0,
savings_pct = %summarizer.estimate_savings(&ui_content, &llm_content).2,
"Applied unified_file 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 unified_file 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 => match tool_intent::unified_exec_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) => {
debug!(
tool = tools::UNIFIED_EXEC,
action = "run",
ui_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).1,
llm_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).0,
savings_pct = %summarizer.estimate_savings(&ui_content, &llm_content).2,
"Applied unified_exec 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 unified_exec 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) => {
debug!(
tool = tools::READ_FILE,
ui_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).1,
llm_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).0,
savings_pct = %summarizer.estimate_savings(&ui_content, &llm_content).2,
"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) => {
debug!(
tool = tools::RUN_PTY_CMD,
ui_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).1,
llm_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).0,
savings_pct = %summarizer.estimate_savings(&ui_content, &llm_content).2,
"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) => {
debug!(
tool = tool_name.as_str(),
ui_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).1,
llm_tokens = %summarizer.estimate_savings(&ui_content, &llm_content).0,
savings_pct = %summarizer.estimate_savings(&ui_content, &llm_content).2,
"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))
}
}
}
}