Skip to content

Commit 46fd649

Browse files
committed
feat: implement local skill find command
1 parent 91febc3 commit 46fd649

3 files changed

Lines changed: 210 additions & 0 deletions

File tree

‎src/cli.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env bun
22
import { cac } from "cac";
3+
import { runFindCommand } from "./commands/find";
34

45
const COMMANDS = ["add", "find", "check", "update", "init", "generate-lock"] as const;
56
type CommandName = (typeof COMMANDS)[number];
@@ -47,6 +48,10 @@ function toGlobalFlags(options: CommandOptions): GlobalFlags {
4748
}
4849

4950
function runCommand(command: CommandName, args: string[], flags: GlobalFlags): number {
51+
if (command === "find") {
52+
return runFindCommand(args, { verbose: flags.verbose });
53+
}
54+
5055
if (flags.verbose) {
5156
console.error(`Running ${command} with args: ${args.join(" ")}`);
5257
}

‎src/commands/find.ts‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { discoverSkills } from "../skills/discovery";
2+
3+
type WriteLine = (line: string) => void;
4+
5+
export type RunFindCommandOptions = {
6+
cwd?: string;
7+
homeDir?: string;
8+
verbose?: boolean;
9+
stdout?: WriteLine;
10+
stderr?: WriteLine;
11+
};
12+
13+
export function runFindCommand(args: string[], options: RunFindCommandOptions = {}): number {
14+
const stdout = options.stdout ?? ((line: string) => console.log(line));
15+
const stderr = options.stderr ?? ((line: string) => console.error(line));
16+
17+
const query = args.join(" ").trim();
18+
const queryLower = query.toLowerCase();
19+
20+
const result = discoverSkills({
21+
cwd: options.cwd,
22+
homeDir: options.homeDir,
23+
verbose: options.verbose === true,
24+
});
25+
26+
if (options.verbose) {
27+
for (const warning of result.warnings) {
28+
stderr(`Warning: ${warning.path}: ${warning.message}`);
29+
}
30+
}
31+
32+
const matches = result.skills
33+
.filter((skill) => {
34+
if (!queryLower) {
35+
return true;
36+
}
37+
38+
const name = skill.name.toLowerCase();
39+
const description = skill.description.toLowerCase();
40+
return name.includes(queryLower) || description.includes(queryLower);
41+
})
42+
.sort((left, right) => {
43+
const scoreLeft = scoreMatch(left, queryLower);
44+
const scoreRight = scoreMatch(right, queryLower);
45+
46+
if (scoreLeft !== scoreRight) {
47+
return scoreLeft - scoreRight;
48+
}
49+
50+
return left.name.localeCompare(right.name);
51+
});
52+
53+
if (matches.length === 0) {
54+
if (query) {
55+
stdout(`No skills found for "${query}".`);
56+
} else {
57+
stdout("No skills found.");
58+
}
59+
return 0;
60+
}
61+
62+
for (const skill of matches) {
63+
stdout(`${skill.name}\t${skill.description}\t${skill.path}`);
64+
}
65+
66+
return 0;
67+
}
68+
69+
function scoreMatch(skill: { name: string; description: string }, queryLower: string): number {
70+
if (!queryLower) {
71+
return 0;
72+
}
73+
74+
const name = skill.name.toLowerCase();
75+
const description = skill.description.toLowerCase();
76+
77+
if (name === queryLower) {
78+
return 0;
79+
}
80+
81+
if (name.startsWith(queryLower)) {
82+
return 1;
83+
}
84+
85+
if (name.includes(queryLower)) {
86+
return 2;
87+
}
88+
89+
if (description.includes(queryLower)) {
90+
return 3;
91+
}
92+
93+
return 4;
94+
}

‎tests/commands/find.test.ts‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 { runFindCommand } from "../../src/commands/find";
6+
7+
const tempDirs: string[] = [];
8+
9+
function makeTempDir(prefix: string): string {
10+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
11+
tempDirs.push(dir);
12+
return dir;
13+
}
14+
15+
function createSkill(baseDir: string, relativeDir: string, name: string, description: string): void {
16+
const skillDir = path.join(baseDir, relativeDir, name);
17+
fs.mkdirSync(skillDir, { recursive: true });
18+
fs.writeFileSync(
19+
path.join(skillDir, "SKILL.md"),
20+
`---\nname: ${name}\ndescription: ${description}\n---\n\n# ${name}\n`
21+
);
22+
}
23+
24+
afterEach(() => {
25+
for (const dir of tempDirs.splice(0)) {
26+
fs.rmSync(dir, { recursive: true, force: true });
27+
}
28+
});
29+
30+
describe("runFindCommand", () => {
31+
test("searches by name and description case-insensitively", () => {
32+
const cwd = makeTempDir("skillet-find-");
33+
const homeDir = path.join(cwd, "home");
34+
35+
createSkill(cwd, ".codex/skills", "alpha", "Alpha deployment workflows");
36+
createSkill(cwd, ".codex/skills", "gamma", "Utilities for alpha teams");
37+
createSkill(cwd, ".codex/skills", "beta", "Database migrations");
38+
39+
const lines: string[] = [];
40+
const exitCode = runFindCommand(["ALPHA"], {
41+
cwd,
42+
homeDir,
43+
stdout: (line) => lines.push(line),
44+
stderr: () => undefined,
45+
});
46+
47+
expect(exitCode).toBe(0);
48+
expect(lines[0]).toContain("alpha");
49+
expect(lines.join("\n")).toContain("gamma");
50+
expect(lines.join("\n")).not.toContain("beta");
51+
});
52+
53+
test("lists all discovered skills when query is omitted", () => {
54+
const cwd = makeTempDir("skillet-find-");
55+
const homeDir = path.join(cwd, "home");
56+
57+
createSkill(cwd, ".codex/skills", "zeta", "Zeta skill");
58+
createSkill(cwd, ".codex/skills", "alpha", "Alpha skill");
59+
60+
const lines: string[] = [];
61+
const exitCode = runFindCommand([], {
62+
cwd,
63+
homeDir,
64+
stdout: (line) => lines.push(line),
65+
stderr: () => undefined,
66+
});
67+
68+
expect(exitCode).toBe(0);
69+
expect(lines[0]).toContain("alpha");
70+
expect(lines[1]).toContain("zeta");
71+
});
72+
73+
test("returns zero with clear message when no results match", () => {
74+
const cwd = makeTempDir("skillet-find-");
75+
const homeDir = path.join(cwd, "home");
76+
createSkill(cwd, ".codex/skills", "alpha", "Alpha skill");
77+
78+
const lines: string[] = [];
79+
const exitCode = runFindCommand(["nomatch"], {
80+
cwd,
81+
homeDir,
82+
stdout: (line) => lines.push(line),
83+
stderr: () => undefined,
84+
});
85+
86+
expect(exitCode).toBe(0);
87+
expect(lines[0]).toBe('No skills found for "nomatch".');
88+
});
89+
90+
test("surfaces invalid skill warnings in verbose mode", () => {
91+
const cwd = makeTempDir("skillet-find-");
92+
const homeDir = path.join(cwd, "home");
93+
94+
createSkill(cwd, ".codex/skills", "alpha", "Alpha skill");
95+
const invalidDir = path.join(cwd, ".codex/skills", "broken");
96+
fs.mkdirSync(invalidDir, { recursive: true });
97+
fs.writeFileSync(path.join(invalidDir, "SKILL.md"), "not valid");
98+
99+
const stderr: string[] = [];
100+
runFindCommand([], {
101+
cwd,
102+
homeDir,
103+
verbose: true,
104+
stdout: () => undefined,
105+
stderr: (line) => stderr.push(line),
106+
});
107+
108+
expect(stderr.join("\n")).toContain("Warning:");
109+
expect(stderr.join("\n")).toContain("broken/SKILL.md");
110+
});
111+
});

0 commit comments

Comments
 (0)