feat(tui): enhance system prompt with workspace structure information
This commit is contained in:
@@ -149,3 +149,45 @@ pub fn write_osc52(output: &mut impl Write, text: &str) -> std::io::Result<()> {
|
||||
output.flush()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Workspace Tree Builder
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Build a string representing the directory tree of `root`, up to `max_files` limits.
|
||||
/// Automatically respects `.gitignore` and `.ignore` via the `ignore` crate.
|
||||
pub fn build_workspace_tree(root: &Path, max_files: usize) -> String {
|
||||
use ignore::WalkBuilder;
|
||||
let mut tree = String::new();
|
||||
let mut count = 0;
|
||||
|
||||
// hidden(false) allows files like .github to be seen, but it still respects .gitignore
|
||||
// and ignores .git directories by default.
|
||||
for result in WalkBuilder::new(root).hidden(false).build() {
|
||||
if let Ok(entry) = result {
|
||||
if count >= max_files {
|
||||
tree.push_str("\n... (truncated)");
|
||||
break;
|
||||
}
|
||||
|
||||
let path = entry.path();
|
||||
if let Ok(rel) = path.strip_prefix(root) {
|
||||
let name = rel.to_string_lossy();
|
||||
if name.is_empty() {
|
||||
tree.push_str(".\n");
|
||||
} else {
|
||||
let depth = entry.depth();
|
||||
let indent = " ".repeat(depth.saturating_sub(1));
|
||||
let is_dir = entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false);
|
||||
let suffix = if is_dir { "/" } else { "" };
|
||||
|
||||
let filename = entry.file_name().to_string_lossy();
|
||||
tree.push_str(&format!("{indent}{filename}{suffix}\n"));
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tree.trim_end().to_string()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user