File size: 1,506 Bytes
0fc6e34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { existsSync, readdirSync, statSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { homedir } from "node:os";
import { parsers } from "../parsers/index.js";

/**
 * Resolve every config file for an agent under a project root.
 *
 * Most agents keep rules in project files (CLAUDE.md, AGENTS.md, …). DSH is
 * special: its bot-team instructions live in the global state file
 * `$HOME/.dsh/bot/state.json`, so that file is always included for `dsh`.
 */
export function findAgentFiles(root: string, agent: string): string[] {
  const parser = parsers[agent];
  if (!parser) return [];
  const files: string[] = [];

  for (const pattern of parser.filePatterns) {
    if (pattern.includes("*")) {
      const parts = pattern.split("/");
      const dirParts = parts.slice(0, -1);
      const filePattern = parts[parts.length - 1];
      const dir = resolve(root, ...dirParts);
      if (existsSync(dir) && statSync(dir).isDirectory()) {
        const regex = new RegExp("^" + filePattern.replace(/\*/g, ".*") + "$");
        for (const f of readdirSync(dir)) {
          if (regex.test(f)) files.push(join(dir, f));
        }
      }
    } else {
      const filePath = resolve(root, pattern);
      if (existsSync(filePath)) files.push(filePath);
    }
  }

  // DSH global bot-team instructions.
  if (agent === "dsh") {
    const statePath = join(homedir(), ".dsh", "bot", "state.json");
    if (existsSync(statePath)) files.push(statePath);
  }

  return files;
}