codetether_agent/agent/builtin/
agents_md.rs1use std::path::{Path, PathBuf};
13
14pub fn load_agents_md(start_dir: &Path) -> Option<(String, PathBuf)> {
22 load_all_agents_md(start_dir).into_iter().next()
23}
24
25pub fn load_all_agents_md(start_dir: &Path) -> Vec<(String, PathBuf)> {
33 let mut results = Vec::new();
34 let mut current = start_dir.to_path_buf();
35 let repo_root = find_git_root(start_dir);
36 loop {
37 if let Some(found) = load_file(¤t) {
38 results.push(found);
39 }
40 if repo_root.as_ref() == Some(¤t) || !current.pop() {
41 break;
42 }
43 }
44 results
45}
46
47fn load_file(dir: &Path) -> Option<(String, PathBuf)> {
48 let agents_path = dir.join("AGENTS.md");
49 std::fs::read_to_string(&agents_path)
50 .ok()
51 .map(|content| (content, agents_path))
52}
53
54fn find_git_root(start_dir: &Path) -> Option<PathBuf> {
55 let mut current = start_dir.to_path_buf();
56 loop {
57 if current.join(".git").exists() {
58 return Some(current);
59 }
60 if !current.pop() {
61 return None;
62 }
63 }
64}