Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/cortex-app-server/src/tools/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,23 @@ pub async fn fetch_url(args: Value) -> ToolResult {
Err(e) => return ToolResult::error(format!("Response is not valid UTF-8: {e}")),
};

// Truncate for display if too long
let truncated = if content.len() > 100_000 {
let truncated = truncate_content_for_display(content);

ToolResult::success(truncated)
}

fn truncate_content_for_display(content: String) -> String {
const MAX_DISPLAY_CHARS: usize = 100_000;

let full_size = content.chars().count();
if full_size > MAX_DISPLAY_CHARS {
let truncated: String = content.chars().take(MAX_DISPLAY_CHARS).collect();
format!(
"{}...\n[Truncated at 100000 chars, full size: {} chars]",
&content[..100_000],
content.len()
"{truncated}...\n[Truncated at {MAX_DISPLAY_CHARS} chars, full size: {full_size} chars]"
)
} else {
content
};

ToolResult::success(truncated)
}
}

/// Search the web.
Expand Down Expand Up @@ -133,3 +138,17 @@ pub async fn web_search(args: Value) -> ToolResult {
Err(e) => ToolResult::error(format!("Web search failed: {e}")),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn truncates_fetch_output_on_utf8_boundary_without_panic() {
let content = format!("{}{}b", "a".repeat(99_999), "é");

let truncated = truncate_content_for_display(content);

assert!(truncated.contains("é...\n[Truncated at 100000 chars, full size: 100001 chars]"));
}
}