/** * The model-facing `skill` tool. * * Parameters intentionally match Claude Code's Skill tool (`skill`, `args`) so skills that * instruct the model to "call the Skill tool with X" work without rewording. */ import { Text } from "@earendil-works/pi-tui"; import type { AgentToolResult, Theme, ToolDefinition, ToolRenderResultOptions, } from "@earendil-works/pi-coding-agent"; import { Type } from "typebox"; import type { Static } from "typebox"; import { findSkill, noSkillsError, notIndexedError, unknownSkillError } from "./registry.ts"; import type { Skill } from "./registry.ts"; import { formatSkillResult, listBundledFiles, readSkillFile } from "./skill-content.ts"; export const skillParameters = Type.Object({ skill: Type.String({ description: 'Skill name exactly as listed in , e.g. "grilling" or "/skill:grilling".', }), args: Type.Optional( Type.String({ description: "Optional request text appended after the skill instructions, like the arguments of /skill:.", }), ), }); export type SkillToolParams = Static; export interface SkillToolDetails { name: string; filePath: string; baseDir: string; lines: number; bundledFiles: string[]; } const DESCRIPTION = [ "Load a skill's full instructions by name.", "pi lists available skills with name, description, and location in ``.", "Call this tool instead of reading a `SKILL.md` file directly.", "Returns the skill body, the skill directory for resolving relative paths, and a list of bundled files.", ].join(" "); const PROMPT_GUIDELINES = [ "Load skills with the `skill` tool instead of reading their SKILL.md: when the user names a skill, when a task matches an entry in ``, or when loaded instructions say to call the Skill tool.", "After a skill loads, follow its instructions for the rest of the task and resolve relative paths against the skill directory the result reports.", ]; export type SkillRegistryGetter = () => readonly Skill[] | undefined; export function createSkillTool( getRegistry: SkillRegistryGetter, ): ToolDefinition { return { name: "skill", label: "Skill", description: DESCRIPTION, promptSnippet: "Load a skill's full instructions by name", promptGuidelines: PROMPT_GUIDELINES, parameters: skillParameters, exposure: "direct", annotations: { readOnlyHint: true, idempotentHint: true, openWorldHint: false, }, async execute(_toolCallId, params) { const registry = getRegistry(); if (registry === undefined) { throw notIndexedError(); } if (registry.length === 0) { throw noSkillsError(); } const skill = findSkill(registry, params.skill); if (!skill) { throw unknownSkillError(registry, params.skill); } const { body, truncated } = await readSkillFile(skill); const bundledFiles = await listBundledFiles(skill.baseDir); const text = formatSkillResult({ skill, body, args: params.args, bundledFiles, truncated, }); return { content: [{ type: "text", text }], details: { name: skill.name, filePath: skill.filePath, baseDir: skill.baseDir, lines: body === "" ? 0 : body.split("\n").length, bundledFiles: bundledFiles.files, }, }; }, renderCall(args: SkillToolParams, theme: Theme) { const title = theme.fg("toolTitle", theme.bold("skill ")) + theme.fg("accent", args.skill); const suffix = args.args ? ` ${theme.fg("dim", args.args)}` : ""; return new Text(title + suffix, 0, 0); }, renderResult( result: AgentToolResult, options: ToolRenderResultOptions, theme: Theme, ) { const textPart = result.content.find((part) => part.type === "text"); const fullText = textPart && textPart.type === "text" ? textPart.text : ""; if (options.expanded) { return new Text(fullText, 0, 0); } const summary = `Skill loaded: ${result.details.name} (${result.details.lines} lines)`; return new Text(theme.fg("muted", summary), 0, 0); }, }; }