feat(cli): agent skills

This commit is contained in:
uinstinct 2026-01-20 13:25:56 +05:30
parent 5e57866172
commit 3ab5b5992b
4 changed files with 235 additions and 0 deletions

View file

@ -9,6 +9,7 @@ import { readFileTool } from "./readFile.js";
import { reportFailureTool } from "./reportFailure.js";
import { runTerminalCommandTool } from "./runTerminalCommand.js";
import { searchCodeTool } from "./searchCode.js";
import { SKILLS_TOOL_META } from "./skills.js";
import { statusTool } from "./status.js";
import { uploadArtifactTool } from "./uploadArtifact.js";
import { viewDiffTool } from "./viewDiff.js";
@ -28,6 +29,7 @@ export const ALL_BUILT_IN_TOOLS = [
searchCodeTool,
statusTool,
SUBAGENT_TOOL_META,
SKILLS_TOOL_META,
uploadArtifactTool,
viewDiffTool,
writeChecklistTool,

View file

@ -28,6 +28,7 @@ import { readFileTool } from "./readFile.js";
import { reportFailureTool } from "./reportFailure.js";
import { runTerminalCommandTool } from "./runTerminalCommand.js";
import { checkIfRipgrepIsInstalled, searchCodeTool } from "./searchCode.js";
import { skillsTool } from "./skills.js";
import { subagentTool } from "./subagent.js";
import {
isBetaSubagentToolEnabled,
@ -127,6 +128,8 @@ export async function getAllAvailableTools(
tools.push(await subagentTool());
}
tools.push(await skillsTool());
const mcpState = await serviceContainer.get<MCPServiceState>(
SERVICE_NAMES.MCP,
);

View file

@ -0,0 +1,84 @@
import { ContinueError, ContinueErrorReason } from "core/util/errors.js";
import { loadMarkdownSkills } from "../util/loadMarkdownSkills.js";
import { logger } from "../util/logger.js";
import { Tool } from "./types.js";
export const SKILLS_TOOL_META: Tool = {
name: "Skills",
displayName: "Skills",
description:
"Use this tool to read the content of a skill by its name. Skills contain detailed instructions for specific tasks.",
readonly: false,
isBuiltIn: true,
parameters: {
type: "object",
required: ["skill_name"],
properties: {
skill_name: {
type: "string",
description:
"The name of the skill to read. This should match the name from the available skills.",
},
},
},
run: async () => "",
};
export const skillsTool = async (): Promise<Tool> => {
const { skills } = await loadMarkdownSkills();
return {
...SKILLS_TOOL_META,
description: `Use this tool to read the content of a skill by its name. Skills contain detailed instructions for specific tasks. The skill name should match one of the available skills listed below:
${skills.map((skill) => `\nname: ${skill.name}\ndescription: ${skill.description}\n`)}`,
preprocess: async (args: any) => {
const { skill_name } = args;
return {
args,
preview: [
{
type: "text",
content: `Reading skill: ${skill_name}`,
},
],
};
},
run: async (args: any, context?: { toolCallId: string }) => {
const { skill_name } = args;
logger.debug("skill args", { args, context });
const skill = skills.find((s) => s.name === skill_name);
if (!skill) {
const availableSkills = skills.map((s) => s.name).join(", ");
throw new ContinueError(
ContinueErrorReason.SkillNotFound,
`Skill "${skill_name}" not found. Available skills: ${availableSkills || "none"}`,
);
}
const content = [
`<skill_name>${skill.name}</skill_name>`,
`<skill_description>${skill.description}</skill_description>`,
`<skill_content>${skill.content}</skill_content>`,
];
if (skill.files.length > 0) {
content.push(
...[
`<skill_files>${skill.files.join(",")}</skill_files>`,
`<other_instructions>Use the read file tool to access skill files as needed.</other_instructions>`,
],
);
}
return content.join("\n");
},
};
};

View file

@ -0,0 +1,146 @@
import * as fs from "fs";
import fsPromises from "fs/promises";
import * as path from "path";
import { parseMarkdownRule } from "@continuedev/config-yaml";
import { WalkerSync } from "ignore-walk";
import { z } from "zod";
import { env } from "../env.js";
export interface Skill {
name: string;
description: string;
path: string;
content: string;
files: string[];
}
export interface LoadSkillsResult {
skills: Skill[];
errors: { fatal: boolean; message: string }[];
}
const skillFrontmatterSchema = z.object({
name: z.string().min(1),
description: z.string().min(1),
});
const SKILLS_DIR = "skills";
/**get the relative path if the filePath is within the current working directory
* otherwise return the absolute path
*/
function getRelativePath(cwd: string, filePath: string) {
return filePath.startsWith(cwd)
? path.join(".", path.relative(cwd, filePath))
: filePath;
}
function getFilePathsInSkillDirectory(
cwd: string,
skillFilePath: string,
): string[] {
const skillDir = path.dirname(skillFilePath);
if (!fs.existsSync(skillDir)) return [];
const walker = new WalkerSync({
path: skillDir,
includeEmpty: false,
follow: false,
});
const files = walker.start().result as string[];
return files
.map((filePath) => path.join(skillDir, filePath))
.filter((filePath) => !filePath.endsWith("SKILL.md"))
.map((filePath) => getRelativePath(cwd, filePath));
}
/**get the SKILL.md files from the given directory */
async function getSkillFilesFromDir(dirPath: string): Promise<string[]> {
// check if dirPath exists
try {
await fsPromises.stat(dirPath);
} catch {
return [];
}
const skillDirs = (await fsPromises.readdir(dirPath, { withFileTypes: true }))
.filter((dir) => dir.isDirectory())
.map((dir) => path.join(dirPath, dir.name));
return (
await Promise.all(
skillDirs.map(async (skillDir) => {
try {
const skillFilePath = path.join(skillDir, "SKILL.md");
await fsPromises.stat(skillFilePath);
return skillFilePath;
} catch {
return null;
}
}),
)
).filter((path) => typeof path === "string");
}
export async function loadMarkdownSkills(): Promise<LoadSkillsResult> {
const errors: { fatal: boolean; message: string }[] = [];
const skills: Skill[] = [];
const cwd = process.cwd();
try {
const skillsDirs = [
path.join(cwd, ".continue", SKILLS_DIR),
path.join(cwd, ".claude", SKILLS_DIR),
path.join(env.continueHome, SKILLS_DIR),
];
const skillFilePaths = (
await Promise.all(
skillsDirs.map((skillDir) => getSkillFilesFromDir(skillDir)),
)
).flat();
await Promise.all(
skillFilePaths.map(async (skillFilePath) => {
try {
const content = await fsPromises.readFile(skillFilePath, "utf-8");
const { frontmatter, markdown } = parseMarkdownRule(content) as {
frontmatter: { name?: string; description?: string };
markdown: string;
};
const validatedFrontmatter =
skillFrontmatterSchema.parse(frontmatter);
const filesInSkillsDirectory = getFilePathsInSkillDirectory(
cwd,
skillFilePath,
);
skills.push({
...validatedFrontmatter,
content: markdown,
path: getRelativePath(cwd, skillFilePath),
files: filesInSkillsDirectory,
});
} catch (error) {
errors.push({
fatal: false,
message: `Failed to parse markdown skill file: ${error instanceof Error ? error.message : error}`,
});
}
}),
);
} catch (err) {
errors.push({
fatal: false,
message: `Error loading markdown skill files: ${err instanceof Error ? err.message : err}`,
});
}
return { skills, errors };
}