/** * AGENTS.md 质量检查(/init-check 与 CLI --check 共用) */ import { join } from "node:path"; import { existsSync, readFileSync } from "node:fs"; import type { NotifyLevel } from "./core.ts"; export type NotifyFn = (message: string, level?: NotifyLevel) => void; const CANDIDATES = ["AGENTS.md", "agent.md", "AGENT.md", "agents.md"]; const CHECKS: { key: string; pattern: RegExp }[] = [ { key: "项目概述", pattern: /项目概述|概述|简介|overview/i }, { key: "快速命令", pattern: /快速命令|命令|command|install|build|test/i }, { key: "目录结构", pattern: /目录结构|结构|structure|src|目录/i }, { key: "代码风格", pattern: /代码风格|风格|规范|style|convention|lint/i }, { key: "禁止规则", pattern: /禁止|never|禁止规则|🚫|boundary|边界/i }, { key: "Git 工作流", pattern: /git|提交|commit|分支|branch|workflow/i }, ]; export function checkAgentsMd(root: string, notify: NotifyFn): number { const found = CANDIDATES.find((f) => existsSync(join(root, f))); if (!found) { notify("❌ 未找到 AGENTS.md", "warning"); notify("💡 运行 codet-pi-init / /init 生成一份", "info"); return 0; } const content = readFileSync(join(root, found), "utf-8"); const size = content.length; const lines = content.split("\n").length; notify(`📄 找到: ${found}`, "info"); notify(` 大小: ${size} 字符, ${lines} 行`, "info"); notify("\n--- 质量检查 ---", "info"); let passedCount = 0; for (const check of CHECKS) { const passed = check.pattern.test(content); if (passed) passedCount++; const icon = passed ? "✅" : "❌"; const status = passed ? "已覆盖" : "缺失"; notify(` ${icon} ${check.key}: ${status}`, passed ? "info" : "warning"); } const score = Math.round((passedCount / CHECKS.length) * 100); notify(`\n📊 完整度评分: ${score}% (${passedCount}/${CHECKS.length})`, "info"); if (score < 50) { notify("💡 建议运行 codet-pi-init --force 重新生成", "warning"); } return score; }