-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell-agents.mjs
More file actions
51 lines (46 loc) · 1.67 KB
/
Copy pathshell-agents.mjs
File metadata and controls
51 lines (46 loc) · 1.67 KB
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
45
46
47
48
49
50
51
import path from 'node:path';
import { fs } from '@eliware/common';
import { getHomeDirectory } from './platform.mjs';
async function readAgentsEntry(dir) {
const filePath = path.join(dir, 'AGENTS.md');
try {
// Resolve the real path safely; a symlink loop will cause an error.
const [content, stats] = await Promise.all([
fs.promises.readFile(filePath, 'utf8'),
fs.promises.lstat(filePath),
]);
let realPath;
try {
realPath = await fs.promises.realpath(filePath);
} catch (_e) {
// Skip entries that cannot be resolved, e.g., symlink loops.
void _e; // satisfy lint
return null;
}
return { dir, content, realPath, isSymlink: stats.isSymbolicLink() };
} catch (error) {
// Propagate non‑ENOENT errors so callers can handle them. Only swallow
// ENOENT to indicate missing file.
if (error?.code === 'ENOENT') return null;
throw error;
}
}
export async function readAgentsFromCwdAndParents(cwd, home = getHomeDirectory()) {
const entries = [];
const homeEntry = home ? await readAgentsEntry(path.resolve(home)) : null;
const seenRealPaths = new Set();
let current = path.resolve(cwd);
for (; ;) {
const entry = await readAgentsEntry(current);
if (entry && !seenRealPaths.has(entry.realPath)) {
seenRealPaths.add(entry.realPath);
entries.push(entry);
}
const parent = path.dirname(current);
if (parent === current) break;
current = parent;
}
const ordered = entries.reverse();
if (homeEntry && !seenRealPaths.has(homeEntry.realPath)) ordered.unshift(homeEntry);
return ordered.map(({ dir, content }) => `# AGENTS.md (${dir})\n${content.trim()}`).join('\n\n');
}