128 lines
3.7 KiB
JavaScript
128 lines
3.7 KiB
JavaScript
/**
|
|
* Agent discovery and configuration
|
|
*/
|
|
import * as fs from "node:fs";
|
|
import * as path from "node:path";
|
|
import { getAgentDir, parseFrontmatter } from "@singularity-forge/coding-agent";
|
|
|
|
const PROJECT_AGENT_DIR_CANDIDATES = [".sf", ".pi"];
|
|
export function parseConflictsWith(value) {
|
|
if (typeof value !== "string") return undefined;
|
|
const conflicts = value
|
|
.split(",")
|
|
.map((s) => s.trim())
|
|
.filter(Boolean);
|
|
return conflicts.length > 0 ? conflicts : undefined;
|
|
}
|
|
function parseAgentTools(value) {
|
|
if (typeof value === "string") {
|
|
const tools = value
|
|
.split(",")
|
|
.map((tool) => tool.trim())
|
|
.filter(Boolean);
|
|
return tools.length > 0 ? tools : undefined;
|
|
}
|
|
if (Array.isArray(value)) {
|
|
const tools = value
|
|
.flatMap((tool) => (typeof tool === "string" ? tool.split(",") : []))
|
|
.map((tool) => tool.trim())
|
|
.filter(Boolean);
|
|
return tools.length > 0 ? tools : undefined;
|
|
}
|
|
return undefined;
|
|
}
|
|
function loadAgentsFromDir(dir, source) {
|
|
const agents = [];
|
|
if (!fs.existsSync(dir)) {
|
|
return agents;
|
|
}
|
|
let entries;
|
|
try {
|
|
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
} catch {
|
|
return agents;
|
|
}
|
|
for (const entry of entries) {
|
|
if (!entry.name.endsWith(".md")) continue;
|
|
if (!entry.isFile() && !entry.isSymbolicLink()) continue;
|
|
const filePath = path.join(dir, entry.name);
|
|
let content;
|
|
try {
|
|
content = fs.readFileSync(filePath, "utf-8");
|
|
} catch {
|
|
continue;
|
|
}
|
|
const { frontmatter, body } = parseFrontmatter(content);
|
|
if (
|
|
typeof frontmatter.name !== "string" ||
|
|
typeof frontmatter.description !== "string"
|
|
) {
|
|
continue;
|
|
}
|
|
const tools = parseAgentTools(frontmatter.tools);
|
|
const conflictsWith = parseConflictsWith(frontmatter.conflicts_with);
|
|
agents.push({
|
|
name: frontmatter.name,
|
|
description: frontmatter.description,
|
|
tools: tools && tools.length > 0 ? tools : undefined,
|
|
model: frontmatter.model,
|
|
conflictsWith,
|
|
systemPrompt: body,
|
|
source,
|
|
filePath,
|
|
});
|
|
}
|
|
return agents;
|
|
}
|
|
function isDirectory(p) {
|
|
try {
|
|
return fs.statSync(p).isDirectory();
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
function findNearestProjectAgentsDir(cwd) {
|
|
let currentDir = cwd;
|
|
while (true) {
|
|
// Prefer the documented project-local location while preserving support
|
|
// for older workarounds that placed agents under .pi/agents.
|
|
for (const configDir of PROJECT_AGENT_DIR_CANDIDATES) {
|
|
const candidate = path.join(currentDir, configDir, "agents");
|
|
if (isDirectory(candidate)) return candidate;
|
|
}
|
|
const parentDir = path.dirname(currentDir);
|
|
if (parentDir === currentDir) return null;
|
|
currentDir = parentDir;
|
|
}
|
|
}
|
|
export function discoverAgents(cwd, scope) {
|
|
const userDir = path.join(getAgentDir(), "agents");
|
|
const projectAgentsDir = findNearestProjectAgentsDir(cwd);
|
|
const userAgents =
|
|
scope === "project" ? [] : loadAgentsFromDir(userDir, "user");
|
|
const projectAgents =
|
|
scope === "user" || !projectAgentsDir
|
|
? []
|
|
: loadAgentsFromDir(projectAgentsDir, "project");
|
|
const agentMap = new Map();
|
|
if (scope === "both") {
|
|
for (const agent of userAgents) agentMap.set(agent.name, agent);
|
|
for (const agent of projectAgents) agentMap.set(agent.name, agent);
|
|
} else if (scope === "user") {
|
|
for (const agent of userAgents) agentMap.set(agent.name, agent);
|
|
} else {
|
|
for (const agent of projectAgents) agentMap.set(agent.name, agent);
|
|
}
|
|
return { agents: Array.from(agentMap.values()), projectAgentsDir };
|
|
}
|
|
export function formatAgentList(agents, maxItems) {
|
|
if (agents.length === 0) return { text: "none", remaining: 0 };
|
|
const listed = agents.slice(0, maxItems);
|
|
const remaining = agents.length - listed.length;
|
|
return {
|
|
text: listed
|
|
.map((a) => `${a.name} (${a.source}): ${a.description}`)
|
|
.join("; "),
|
|
remaining,
|
|
};
|
|
}
|