// 项目发现层:从项目根目录收集 5 类输入文件。
// 遍历有深度与数量上限;跳过依赖/构建目录;所有跳过与读取失败都写入
// unknowns,保证报告能说明「哪些没看」。
///|
const MAX_DEPTH : Int32 = 6
///|
const MAX_FILES : Int32 = 500
///|
pub(all) struct ProjectFile {
path : String
kind : String
text : String
}
///|
pub(all) struct ProjectLoad {
root : String
files : Array[ProjectFile]
unknowns : Array[String]
}
///|
priv struct Candidate {
path : String
abs : String
kind : String
}
///|
fn join_path(dir : String, name : String) -> String {
if dir == "" {
name
} else if dir.has_suffix("/") {
dir + name
} else {
dir + "/" + name
}
}
///|
fn dirname(path : String) -> String {
match path.rev_find("/") {
Some(index) => path[:index].to_owned()
None => ""
}
}
///|
fn should_skip_dir(name : String) -> Bool {
match name {
".git"
| "node_modules"
| "_build"
| ".mooncakes"
| "dist"
| "target"
| "build"
| ".venv"
| "venv"
| "__pycache__"
| ".omc"
| ".omx"
| ".next"
| ".cache" => true
_ => false
}
}
///|
fn is_script_name(name : String) -> Bool {
name.has_suffix(".sh") ||
name.has_suffix(".bash") ||
name.has_suffix(".ps1") ||
name.has_suffix(".py") ||
name.has_suffix(".js") ||
name.has_suffix(".mjs") ||
name.has_suffix(".ts")
}
///|
fn is_settings_name(name : String) -> Bool {
name == "settings.json" || name == "settings.local.json"
}
///|
/// 按文件名与相对路径判定输入类型;返回空串表示不采集。
fn classify(name : String, rel : String) -> String {
if name == "mcp.json" ||
name == ".mcp.json" ||
name == "claude_desktop_config.json" {
return "mcp_config"
}
if is_settings_name(name) &&
(rel.contains(".claude/") || rel.contains(".codex/")) {
return "settings"
}
if name == "SKILL.md" {
return "skill"
}
if name == "tools.json" ||
name == "tools-list.json" ||
name.has_suffix(".tools.json") {
return "tools_snapshot"
}
if is_script_name(name) {
return "script"
}
""
}
///|
fn walk(
abs_dir : String,
rel_dir : String,
depth : Int32,
candidates : Array[Candidate],
unknowns : Array[String],
) -> Unit {
if depth > MAX_DEPTH {
return
}
let names = @fs.read_dir(abs_dir).to_owned() catch {
_ => {
let label = if rel_dir == "" { "." } else { rel_dir }
unknowns.push("无法读取目录: " + label)
return
}
}
names.sort()
for name in names {
if name == "" {
continue
}
if candidates.length() >= MAX_FILES {
unknowns.push(
"文件数量超过上限 " +
MAX_FILES.to_string() +
",遍历已截断",
)
return
}
let abs = join_path(abs_dir, name)
let rel = join_path(rel_dir, name)
let is_dir = @fs.is_dir(abs) catch { _ => false }
if is_dir {
if should_skip_dir(name) {
continue
}
walk(abs, rel, depth + 1, candidates, unknowns)
continue
}
let kind = classify(name, rel)
if kind != "" {
candidates.push({ path: rel, abs, kind, })
}
}
}
///|
fn under_any(path : String, dirs : Array[String]) -> Bool {
for dir in dirs {
if dir == "" {
if !path.contains("/") {
return true
}
} else if path.has_prefix(dir + "/") {
return true
}
}
false
}
///|
/// 扫描项目根目录,返回所有可解析输入。
/// 脚本只在已发现 SKILL.md 的目录下采集,避免把普通源码仓库的脚本
/// 全部当成 Agent 能力。
pub fn load_project(root : String) -> ProjectLoad {
let candidates : Array[Candidate] = []
let unknowns : Array[String] = []
walk(root, "", 0, candidates, unknowns)
let skill_dirs : Array[String] = []
for candidate in candidates {
if candidate.kind == "skill" {
skill_dirs.push(dirname(candidate.path))
}
}
let files : Array[ProjectFile] = []
for candidate in candidates {
if candidate.kind == "script" && !under_any(candidate.path, skill_dirs) {
continue
}
let text = @fs.read_file_to_string(candidate.abs) catch {
_ => {
unknowns.push("无法读取文件: " + candidate.path)
continue
}
}
files.push({ path: candidate.path, kind: candidate.kind, text, })
}
return { root, files, unknowns, }
}