Files
hose-use/src/cli/commands/skill.ts
T

116 lines
3.4 KiB
TypeScript

import { existsSync } from "node:fs";
import { join } from "node:path";
import {
EMBEDDED_SKILL_FILES,
getDefaultGlobalSkillDir,
getSkillContent,
installSkillFiles,
} from "../../core/embedded-skill.js";
import { booleanOption, stringOption } from "../parser.js";
import { printJson, printKeyValue } from "../printer.js";
import { isDryRun, mutationPreview, requireMutationConfirmation } from "../mutation.js";
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
export async function handleSkillCommand(
parsed: CliParsedArgs,
runtime: CliRuntime
): Promise<CliResult> {
const action = parsed.action || "status";
switch (action) {
case "dump":
case "cat":
case "show":
return {
stdout: `${getSkillContent()}\n`,
stderr: "",
exitCode: 0,
};
case "install":
case "init":
case "sync": {
const isLocal =
booleanOption(parsed.options, ["local"]) ||
booleanOption(parsed.options, ["workspace"]);
const customDir =
stringOption(parsed.options, ["dir", "target"]) || parsed.args[0];
const targetDir = customDir
? customDir
: isLocal
? join(process.cwd(), ".agents", "skills", "hose-use")
: getDefaultGlobalSkillDir();
if (isDryRun(parsed, runtime)) {
return mutationPreview(parsed, runtime, "安装 Agent Skill 文件", targetDir, {
fileCount: Object.keys(EMBEDDED_SKILL_FILES).length,
local: isLocal,
targetPolicy: customDir ? "explicit" : isLocal ? "workspace" : "global-canonical",
});
}
requireMutationConfirmation(parsed, runtime, "安装 Agent Skill 文件", `hose-use skill install${customDir ? ` --dir=${customDir}` : ""} --yes`);
const result = installSkillFiles(targetDir);
if (runtime.jsonMode) {
return {
stdout: printJson({
installed: true,
targetDir: result.targetDir,
fileCount: result.files.length,
files: result.files,
}),
stderr: "",
exitCode: 0,
};
}
return {
stdout: `[SUCCESS] hose-use 技能已成功安装至: ${result.targetDir}\n包含文件: ${result.files.join(", ")}\n`,
stderr: "",
exitCode: 0,
};
}
case "status":
case "get": {
const globalDir = getDefaultGlobalSkillDir();
const localDir = join(process.cwd(), ".agents", "skills", "hose-use");
const statusObj = {
global: {
dir: globalDir,
installed: existsSync(join(globalDir, "SKILL.md")),
},
local: {
dir: localDir,
installed: existsSync(join(localDir, "SKILL.md")),
},
};
if (runtime.jsonMode) {
return { stdout: printJson(statusObj), stderr: "", exitCode: 0 };
}
const output = printKeyValue({
"全局 Skill 目录": `${globalDir} [${statusObj.global.installed ? "已安装 ✅" : "未安装 ❌"}]`,
"本地 Skill 目录": `${localDir} [${statusObj.local.installed ? "已安装 ✅" : "未安装 ❌"}]`,
});
return {
stdout: `====== Agent Skill 状态 ======\n${output}\n\n提示: 运行 hose-use skill install 进行全局安装\n`,
stderr: "",
exitCode: 0,
};
}
default:
return {
stdout: "",
stderr: `未知 skill 子命令: ${action}。支持: install, status, dump\n`,
exitCode: 1,
};
}
}