agent: Fix unnecessary "tool result too long" (#30798)

Oleksiy Syvokon created

Release Notes:

- N/A

Change summary

crates/agent/src/tool_use.rs | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)

Detailed changes

crates/agent/src/tool_use.rs 🔗

@@ -425,16 +425,17 @@ impl ToolUseState {
 
                 let content = match tool_result {
                     ToolResultContent::Text(text) => {
-                        let truncated = truncate_lines_to_byte_limit(&text, tool_output_limit);
-
-                        LanguageModelToolResultContent::Text(
+                        let text = if text.len() < tool_output_limit {
+                            text
+                        } else {
+                            let truncated = truncate_lines_to_byte_limit(&text, tool_output_limit);
                             format!(
                                 "Tool result too long. The first {} bytes:\n\n{}",
                                 truncated.len(),
                                 truncated
                             )
-                            .into(),
-                        )
+                        };
+                        LanguageModelToolResultContent::Text(text.into())
                     }
                     ToolResultContent::Image(language_model_image) => {
                         if language_model_image.estimate_tokens() < tool_output_limit {