Skip to content

Commit a930e3c

Browse files
committed
feat: add agent path mapping and detection
1 parent fb21e44 commit a930e3c

2 files changed

Lines changed: 215 additions & 0 deletions

File tree

‎src/agents/detection.ts‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
4+
export class AgentDetectionError extends Error {}
5+
6+
export const KNOWN_AGENTS = ["claude", "codex", "opencode", "cursor", "windsurf"] as const;
7+
8+
export type AgentName = (typeof KNOWN_AGENTS)[number];
9+
10+
export type AgentPathMap = Record<
11+
AgentName,
12+
{
13+
project: string;
14+
global: string;
15+
}
16+
>;
17+
18+
export type DetectedAgent = {
19+
agent: AgentName;
20+
locations: Array<"project" | "global">;
21+
paths: string[];
22+
};
23+
24+
export type ResolveTargetAgentsOptions = {
25+
cwd?: string;
26+
homeDir?: string;
27+
explicitAgents?: AgentName[];
28+
promptWhenNone?: (availableAgents: AgentName[]) => AgentName[];
29+
};
30+
31+
export function getAgentPathMap(options: { cwd?: string; homeDir?: string } = {}): AgentPathMap {
32+
const cwd = path.resolve(options.cwd ?? process.cwd());
33+
const homeDir = path.resolve(options.homeDir ?? cwd);
34+
35+
return {
36+
claude: {
37+
project: path.join(cwd, ".claude", "skills"),
38+
global: path.join(homeDir, ".claude", "skills"),
39+
},
40+
codex: {
41+
project: path.join(cwd, ".codex", "skills"),
42+
global: path.join(homeDir, ".codex", "skills"),
43+
},
44+
opencode: {
45+
project: path.join(cwd, ".opencode", "skills"),
46+
global: path.join(homeDir, ".opencode", "skills"),
47+
},
48+
cursor: {
49+
project: path.join(cwd, ".cursor", "skills"),
50+
global: path.join(homeDir, ".cursor", "skills"),
51+
},
52+
windsurf: {
53+
project: path.join(cwd, ".windsurf", "skills"),
54+
global: path.join(homeDir, ".windsurf", "skills"),
55+
},
56+
};
57+
}
58+
59+
export function detectInstalledAgents(options: { cwd?: string; homeDir?: string } = {}): DetectedAgent[] {
60+
const pathMap = getAgentPathMap(options);
61+
const detected: DetectedAgent[] = [];
62+
63+
for (const agent of KNOWN_AGENTS) {
64+
const locations: Array<"project" | "global"> = [];
65+
const paths: string[] = [];
66+
67+
for (const location of ["project", "global"] as const) {
68+
const candidate = pathMap[agent][location];
69+
if (isDirectory(candidate)) {
70+
locations.push(location);
71+
paths.push(candidate);
72+
}
73+
}
74+
75+
if (locations.length > 0) {
76+
detected.push({
77+
agent,
78+
locations,
79+
paths,
80+
});
81+
}
82+
}
83+
84+
return detected.sort((left, right) => left.agent.localeCompare(right.agent));
85+
}
86+
87+
export function resolveTargetAgents(options: ResolveTargetAgentsOptions = {}): AgentName[] {
88+
if (options.explicitAgents && options.explicitAgents.length > 0) {
89+
return normalizeAgents(options.explicitAgents);
90+
}
91+
92+
const detected = detectInstalledAgents(options).map((item) => item.agent);
93+
if (detected.length > 0) {
94+
return normalizeAgents(detected);
95+
}
96+
97+
if (options.promptWhenNone) {
98+
const selected = options.promptWhenNone([...KNOWN_AGENTS]);
99+
return normalizeAgents(selected);
100+
}
101+
102+
throw new AgentDetectionError(
103+
"No supported agents detected. Specify --agent explicitly or install an agent skills directory."
104+
);
105+
}
106+
107+
function normalizeAgents(agents: AgentName[]): AgentName[] {
108+
const invalid = agents.filter((agent) => !KNOWN_AGENTS.includes(agent));
109+
if (invalid.length > 0) {
110+
throw new AgentDetectionError(`Unknown agent(s): ${invalid.join(", ")}`);
111+
}
112+
113+
return [...new Set(agents)].sort((left, right) => left.localeCompare(right)) as AgentName[];
114+
}
115+
116+
function isDirectory(candidatePath: string): boolean {
117+
try {
118+
return fs.statSync(candidatePath).isDirectory();
119+
} catch {
120+
return false;
121+
}
122+
}

‎tests/agents/detection.test.ts‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import fs from "node:fs";
2+
import os from "node:os";
3+
import path from "node:path";
4+
import { afterEach, describe, expect, test } from "vitest";
5+
import {
6+
AgentDetectionError,
7+
detectInstalledAgents,
8+
getAgentPathMap,
9+
resolveTargetAgents,
10+
} from "../../src/agents/detection";
11+
12+
const tempDirs: string[] = [];
13+
14+
function makeTempDir(prefix: string): string {
15+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
16+
tempDirs.push(dir);
17+
return dir;
18+
}
19+
20+
afterEach(() => {
21+
for (const dir of tempDirs.splice(0)) {
22+
fs.rmSync(dir, { recursive: true, force: true });
23+
}
24+
});
25+
26+
describe("agent detection", () => {
27+
test("returns project and global path mapping", () => {
28+
const cwd = makeTempDir("skillet-agents-");
29+
const homeDir = makeTempDir("skillet-agents-home-");
30+
31+
const map = getAgentPathMap({ cwd, homeDir });
32+
33+
expect(map.codex.project).toBe(path.join(cwd, ".codex", "skills"));
34+
expect(map.codex.global).toBe(path.join(homeDir, ".codex", "skills"));
35+
expect(map.claude.project).toBe(path.join(cwd, ".claude", "skills"));
36+
});
37+
38+
test("detects installed agents from existing directories", () => {
39+
const cwd = makeTempDir("skillet-agents-");
40+
const homeDir = makeTempDir("skillet-agents-home-");
41+
42+
fs.mkdirSync(path.join(cwd, ".codex", "skills"), { recursive: true });
43+
fs.mkdirSync(path.join(homeDir, ".claude", "skills"), { recursive: true });
44+
45+
const detected = detectInstalledAgents({ cwd, homeDir });
46+
47+
expect(detected.map((item) => item.agent)).toEqual(["claude", "codex"]);
48+
});
49+
50+
test("uses explicit agents without prompting", () => {
51+
const cwd = makeTempDir("skillet-agents-");
52+
const homeDir = makeTempDir("skillet-agents-home-");
53+
54+
const resolved = resolveTargetAgents({
55+
cwd,
56+
homeDir,
57+
explicitAgents: ["codex", "claude", "codex"],
58+
promptWhenNone: () => {
59+
throw new Error("prompt should not be called");
60+
},
61+
});
62+
63+
expect(resolved).toEqual(["claude", "codex"]);
64+
});
65+
66+
test("prompts when no agents are detected and none are specified", () => {
67+
const cwd = makeTempDir("skillet-agents-");
68+
const homeDir = makeTempDir("skillet-agents-home-");
69+
70+
const resolved = resolveTargetAgents({
71+
cwd,
72+
homeDir,
73+
promptWhenNone: (available) => {
74+
expect(available).toContain("codex");
75+
return ["codex"];
76+
},
77+
});
78+
79+
expect(resolved).toEqual(["codex"]);
80+
});
81+
82+
test("throws when no agents detected and prompt is unavailable", () => {
83+
const cwd = makeTempDir("skillet-agents-");
84+
const homeDir = makeTempDir("skillet-agents-home-");
85+
86+
expect(() =>
87+
resolveTargetAgents({
88+
cwd,
89+
homeDir,
90+
})
91+
).toThrowError(AgentDetectionError);
92+
});
93+
});

0 commit comments

Comments
 (0)