Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion src/cortex-app-server/src/tools/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::{Path, PathBuf};

use serde_json::{Value, json};

use super::preview::truncate_at_char_boundary;
use super::types::ToolResult;

/// Read file contents.
Expand Down Expand Up @@ -119,7 +120,7 @@ pub async fn write_file(cwd: &Path, args: Value) -> ToolResult {
"filename": std::path::Path::new(path).file_name().and_then(|n| n.to_str()).unwrap_or(""),
"extension": extension,
"size": content.len(),
"content_preview": if content.len() > 500 { &content[..500] } else { content }
"content_preview": truncate_at_char_boundary(content, 500)
})),
},
Err(e) => ToolResult::error(format!("Failed to write file: {e}")),
Expand Down
1 change: 1 addition & 0 deletions src/cortex-app-server/src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod definitions;
mod executor;
mod filesystem;
mod planning;
mod preview;
mod search;
mod security;
mod shell;
Expand Down
38 changes: 38 additions & 0 deletions src/cortex-app-server/src/tools/preview.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//! Helpers for building tool output previews.

/// Truncate a string to at most `max_bytes` bytes without splitting a UTF-8 character.
pub(super) fn truncate_at_char_boundary(input: &str, max_bytes: usize) -> &str {
if input.len() <= max_bytes {
return input;
}

let mut end = max_bytes;
while end > 0 && !input.is_char_boundary(end) {
end -= 1;
}

&input[..end]
}

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

#[test]
fn keeps_ascii_at_requested_byte_limit() {
assert_eq!(truncate_at_char_boundary("abcdef", 3), "abc");
}

#[test]
fn backs_up_to_utf8_boundary() {
let input = format!("{}{}", "A".repeat(499), "\u{4E2D}");

assert_eq!(input.len(), 502);
assert_eq!(truncate_at_char_boundary(&input, 500), "A".repeat(499));
}

#[test]
fn returns_full_input_when_under_limit() {
assert_eq!(truncate_at_char_boundary("hello", 500), "hello");
}
}
8 changes: 5 additions & 3 deletions src/cortex-app-server/src/tools/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use serde_json::Value;
use tokio::process::Command;

use super::preview::truncate_at_char_boundary;
use super::types::ToolResult;

/// Fetch content from a URL.
Expand Down Expand Up @@ -79,9 +80,10 @@ pub async fn fetch_url(args: Value) -> ToolResult {

// Truncate for display if too long
let truncated = if content.len() > 100_000 {
let preview = truncate_at_char_boundary(&content, 100_000);
format!(
"{}...\n[Truncated at 100000 chars, full size: {} chars]",
&content[..100_000],
"{}...\n[Truncated at 100000 bytes, full size: {} bytes]",
preview,
content.len()
)
} else {
Expand Down Expand Up @@ -124,7 +126,7 @@ pub async fn web_search(args: Value) -> ToolResult {
let html = String::from_utf8_lossy(&output.stdout);
// Simple extraction of text
let truncated = if html.len() > 10_000 {
&html[..10_000]
truncate_at_char_boundary(&html, 10_000)
} else {
&html
};
Expand Down