@sema-agent/core
Version:
Stateless, task-oriented AI agent core
56 lines (55 loc) • 1.71 kB
JavaScript
import { lstatSync, readdirSync } from "node:fs";
import { join, relative } from "node:path";
export const MEMORY_INDEX_FILENAME = "MEMORY.md";
export const DEFAULT_MAX_ENTRY_DEPTH = 3;
export function scanEntryFiles(dir, opts = {}) {
const maxDepth = opts.maxDepth ?? DEFAULT_MAX_ENTRY_DEPTH;
const out = [];
const walk = (d, depth) => {
let names;
try {
names = readdirSync(d);
}
catch {
if (d !== dir)
opts.onSkip?.(d, "unreadable");
return;
}
for (const name of names.sort()) {
if (name.startsWith(".") || name === MEMORY_INDEX_FILENAME) {
if (name.startsWith("."))
opts.onSkip?.(join(d, name), "dotfile");
continue;
}
const p = join(d, name);
let st;
try {
st = lstatSync(p);
}
catch {
opts.onSkip?.(p, "unreadable");
continue;
}
if (st.isSymbolicLink()) {
opts.onSkip?.(p, "symlink");
continue;
}
if (st.isDirectory()) {
if (depth + 1 <= maxDepth)
walk(p, depth + 1);
else
opts.onSkip?.(p, "depth");
continue;
}
if (!st.isFile())
continue;
if (!name.endsWith(".md")) {
opts.onSkip?.(p, "nonmd");
continue;
}
out.push({ path: p, slug: relative(dir, p).replace(/\.md$/, "") });
}
};
walk(dir, 0);
return out;
}