mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 11:50:23 +08:00
feat: 发布 1.1.12,测试版 Skill 进入正式版
正式版带上 Skill 导入,并保留不再钉上一轮全文、按提交号标当前和拒绝跨站请求。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
commit
9c1a002cf5
9 files changed
+1389
-108
No files matched your search
+207
-3
@@ -3,6 +3,7 @@ import { promises as fsp } from "node:fs";
|
||||
import * as core from "./core.js";
|
||||
import * as hostctl from "./host.js";
|
||||
import * as rules from "./rules.js";
|
||||
import * as skills from "./skills.js";
|
||||
import * as updater from "./update.js";
|
||||
import {
|
||||
GLOBAL_PROMPT_ORDER,
|
||||
@@ -398,7 +399,8 @@ async function handlePurgeCommand(rawInput, config) {
|
||||
" /purge uninstall 卸载插件(已应用则先还原)\n" +
|
||||
" /purge edit 编辑注入文件 prompt-inject.md\n" +
|
||||
" /purge help 显示帮助\n" +
|
||||
"规则集切换见 /rules(list | use <id> | create | delete | reset)",
|
||||
"规则集切换见 /rules(list | use <id> | create | delete | reset)\n" +
|
||||
"官方 Skill 安装见 /skills(list | import <路径> | create <id> [说明] | delete <id>)",
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -512,6 +514,106 @@ async function handleRulesCommand(rawInput) {
|
||||
}
|
||||
}
|
||||
|
||||
function renderSkillsStatus(st) {
|
||||
const out = [];
|
||||
out.push("官方用户 Skill / $DSH_HOME/skills");
|
||||
out.push(` ${st.skills_dir}`);
|
||||
out.push(" 插件只安装目录。调用走官方:任务对上 description/whenToUse 时模型调 skill 工具;聊天输入 /名称 立即加载。");
|
||||
out.push(" 正文里写「激活」不会触发。官方认 name、description、whenToUse、disable-model-invocation、user-invocable。");
|
||||
out.push("");
|
||||
if (!st.skills.length) {
|
||||
out.push(" (暂无用户 Skill — 用 /skills import <压缩包或文件夹> 或设置页导入)");
|
||||
return out.join("\n");
|
||||
}
|
||||
for (const item of st.skills) {
|
||||
const mark = item.valid ? "·" : "!";
|
||||
const desc = item.description || "(无说明)";
|
||||
const how = item.valid
|
||||
? [
|
||||
item.userInvocable ? `/${item.id}` : "不可 /名称",
|
||||
item.modelInvocable ? "模型可调" : "模型不可调",
|
||||
].join(" · ")
|
||||
: "";
|
||||
out.push(` ${mark} ${item.id} ${desc} (${skills.formatSize(item.size)})`);
|
||||
if (how) out.push(` ${how}`);
|
||||
if (!item.valid && item.error) out.push(` ${item.error}`);
|
||||
}
|
||||
return out.join("\n");
|
||||
}
|
||||
|
||||
async function handleSkillsCommand(rawInput) {
|
||||
const args = (rawInput || "").trim().split(/\s+/).filter(Boolean);
|
||||
const sub = (args[0] || "list").toLowerCase();
|
||||
const dshHome = core.findDshHome();
|
||||
|
||||
switch (sub) {
|
||||
case "list":
|
||||
case "ls":
|
||||
case "s":
|
||||
return { kind: "success", text: renderSkillsStatus(await skills.skillsStatus(dshHome)) };
|
||||
case "create":
|
||||
case "new":
|
||||
case "c": {
|
||||
const id = args[1];
|
||||
if (!id) return { kind: "error", text: "用法: /skills create <id> [说明]" };
|
||||
if (!skills.validSkillName(id)) {
|
||||
return { kind: "error", text: "id 必须是 kebab-case,例如 code-review" };
|
||||
}
|
||||
const description = args.slice(2).join(" ").trim() || id;
|
||||
try {
|
||||
const saved = skills.toPublicSkill(dshHome, await skills.saveSkill(dshHome, id, "", description));
|
||||
return {
|
||||
kind: "success",
|
||||
text: `✓ 已写入官方目录 ${saved.path}\n新会话会进官方 Skill 目录;任务对上或输入 /${id} 时由官方加载。`,
|
||||
};
|
||||
} catch (e) {
|
||||
return { kind: "error", text: `安装失败: ${e.message}` };
|
||||
}
|
||||
}
|
||||
case "import":
|
||||
case "i": {
|
||||
const src = args.slice(1).join(" ").trim();
|
||||
if (!src) return { kind: "error", text: "用法: /skills import <压缩包或文件夹>" };
|
||||
try {
|
||||
const packed = await skills.importFromPath(dshHome, src);
|
||||
const names = packed.imported.map((item) => item.id).join(", ");
|
||||
return { kind: "success", text: `✓ 已导入到官方 $DSH_HOME/skills:${names}` };
|
||||
} catch (e) {
|
||||
return { kind: "error", text: `导入失败: ${e.message}` };
|
||||
}
|
||||
}
|
||||
case "delete":
|
||||
case "rm":
|
||||
case "d": {
|
||||
const id = args[1];
|
||||
if (!id) return { kind: "error", text: "用法: /skills delete <id>" };
|
||||
try {
|
||||
const removed = skills.toPublicSkill(dshHome, await skills.deleteSkill(dshHome, id));
|
||||
return {
|
||||
kind: "success",
|
||||
text: `✓ 已从官方目录删除 ${id}${removed.backup ? `\n备份:${removed.backup}` : ""}`,
|
||||
};
|
||||
} catch (e) {
|
||||
return { kind: "error", text: `删除失败: ${e.message}` };
|
||||
}
|
||||
}
|
||||
case "help":
|
||||
case "h":
|
||||
default:
|
||||
return {
|
||||
kind: "success",
|
||||
text: "官方 Skill 安装:\n" +
|
||||
" /skills list 列出 $DSH_HOME/skills\n" +
|
||||
" /skills create <id> [说明] 写入 <id>/SKILL.md\n" +
|
||||
" /skills import <路径> 导入压缩包或文件夹到官方目录\n" +
|
||||
" /skills delete <id> 删除该目录(官方会从目录拿掉)\n" +
|
||||
" 也可自己删 $DSH_HOME/skills/<id>\n" +
|
||||
" 调用:聊天输入 /<id>,或让模型按 description 调 skill 工具\n" +
|
||||
" 不注入 systemPrompt,不替代提示词",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function apply(ctx, config) {
|
||||
const cfg = { ...DEFAULTS, ...(config || {}) };
|
||||
if (!cfg.enabled) return;
|
||||
@@ -573,6 +675,12 @@ export function apply(ctx, config) {
|
||||
input: { hint: "list | use <id> | create <id> [别名] [目标] | delete <id> | reset | help" },
|
||||
handler: async (invocation) => handleRulesCommand(invocation.rawInput ?? ""),
|
||||
});
|
||||
commands.register({
|
||||
name: "skills",
|
||||
description: "官方用户 Skill($DSH_HOME/skills):list | import <路径> | create <id> [说明] | delete <id> | help",
|
||||
input: { hint: "list | import <路径> | create <id> [说明] | delete <id> | help" },
|
||||
handler: async (invocation) => handleSkillsCommand(invocation.rawInput ?? ""),
|
||||
});
|
||||
commands.register({
|
||||
name: "rewind",
|
||||
description: "回退:子代理直接回退一次;主代理 /rewind once 或 /rewind round",
|
||||
@@ -796,6 +904,7 @@ function installWebServer(ctx, cfg) {
|
||||
plugin_version: updater.localVersion(),
|
||||
update: updater.lastUpdateResult(),
|
||||
...updater.channelPublic(),
|
||||
...(await skills.skillsStatus(core.findDshHome()).catch(() => ({ skills: [] }))),
|
||||
});
|
||||
} catch (e) {
|
||||
sendJson(response, 500, { ok: false, error: String(e) });
|
||||
@@ -1131,6 +1240,7 @@ function installWebServer(ctx, cfg) {
|
||||
},
|
||||
}, "dsh-purge: override");
|
||||
installRulesHttp(host.webServer);
|
||||
installSkillsHttp(host.webServer);
|
||||
registerPluginRoute({
|
||||
kind: "exact",
|
||||
path: "/dsh-purge/restart",
|
||||
@@ -1235,6 +1345,99 @@ function rulesHttpHandler(forcedOp) {
|
||||
};
|
||||
}
|
||||
|
||||
async function handleSkillsOp(op, body, queryId) {
|
||||
const dshHome = core.findDshHome();
|
||||
const id = typeof body?.id === "string" ? body.id : String(queryId || "");
|
||||
if (op === "status") {
|
||||
return { ok: true, ...(await skills.skillsStatus(dshHome)) };
|
||||
}
|
||||
if (op === "read") {
|
||||
if (!skills.validSkillName(id)) return { status: 400, ok: false, error: "invalid skill name" };
|
||||
const item = await skills.readSkill(dshHome, id);
|
||||
if (!item) return { status: 404, ok: false, error: `skill not found: ${id}` };
|
||||
return { ok: true, ...skills.toPublicSkill(dshHome, item) };
|
||||
}
|
||||
if (op === "save") {
|
||||
if (!skills.validSkillName(id)) return { status: 400, ok: false, error: "id 必须是 kebab-case,例如 code-review" };
|
||||
const content = typeof body?.content === "string" ? body.content : "";
|
||||
const description = typeof body?.description === "string" ? body.description : "";
|
||||
const saved = await skills.saveSkill(dshHome, id, content, description);
|
||||
return { ok: true, ...skills.toPublicSkill(dshHome, saved) };
|
||||
}
|
||||
if (op === "delete") {
|
||||
if (!skills.validSkillName(id)) return { status: 400, ok: false, error: "invalid skill name" };
|
||||
const removed = await skills.deleteSkill(dshHome, id);
|
||||
return { ok: true, ...skills.toPublicSkill(dshHome, removed) };
|
||||
}
|
||||
if (op === "import") {
|
||||
if (body?.path || body?.source || body?.folder) {
|
||||
return { status: 400, ok: false, error: "请在本机选择压缩包或文件夹导入" };
|
||||
}
|
||||
if (typeof body?.data === "string" && body.data.trim()) {
|
||||
const packed = await skills.importArchive(dshHome, Buffer.from(body.data, "base64"), body.name || "skill.zip");
|
||||
return { ok: true, ...packed };
|
||||
}
|
||||
if (Array.isArray(body?.files) && body.files.length) {
|
||||
const packed = await skills.importFileTree(dshHome, body.files, body.name || "");
|
||||
return { ok: true, ...packed };
|
||||
}
|
||||
return { status: 400, ok: false, error: "请选择压缩包或文件夹" };
|
||||
}
|
||||
return { status: 400, ok: false, error: `unknown op: ${op}` };
|
||||
}
|
||||
|
||||
function skillsHttpHandler(forcedOp) {
|
||||
return async (request, response) => {
|
||||
if (request.method === "OPTIONS") {
|
||||
sendOptions(response);
|
||||
return;
|
||||
}
|
||||
if (request.method !== "GET" && request.method !== "POST") {
|
||||
response.writeHead(405, { allow: "GET, POST, OPTIONS" });
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const url = new URL(request.url ?? "", "http://127.0.0.1");
|
||||
const tail = skillRouteTail(url.pathname);
|
||||
const body = request.method === "POST" ? await readJsonBody(request, 14 * 1024 * 1024) : {};
|
||||
const op = forcedOp
|
||||
|| body.op
|
||||
|| tail
|
||||
|| url.searchParams.get("op")
|
||||
|| (url.searchParams.get("id") ? "read" : "status");
|
||||
const result = await handleSkillsOp(op, body, url.searchParams.get("id"));
|
||||
sendJson(response, result.status || 200, result);
|
||||
} catch (e) {
|
||||
sendJson(response, 500, { ok: false, error: String(e && e.message ? e.message : e) });
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function skillRouteTail(pathname) {
|
||||
const match = String(pathname || "").match(/^\/dsh-purge\/skills?(?:\/(.*))?$/i);
|
||||
if (!match) return "";
|
||||
return String(match[1] || "").replace(/\/$/, "");
|
||||
}
|
||||
|
||||
function installSkillsHttp(webServer) {
|
||||
const stop = [];
|
||||
const add = (spec) => stop.push(registerRoute(webServer, spec));
|
||||
for (const routePath of ["/dsh-purge/skills", "/dsh-purge/skill"]) {
|
||||
add({ kind: "exact", path: routePath, handler: skillsHttpHandler("") });
|
||||
}
|
||||
add({ kind: "prefix", path: "/dsh-purge/skills", handler: skillsHttpHandler("") });
|
||||
for (const op of ["status", "read", "save", "delete", "import"]) {
|
||||
add({ kind: "exact", path: `/dsh-purge/skills/${op}`, handler: skillsHttpHandler(op) });
|
||||
add({ kind: "exact", path: `/dsh-purge/skill/${op}`, handler: skillsHttpHandler(op) });
|
||||
}
|
||||
return () => {
|
||||
for (let i = stop.length - 1; i >= 0; i -= 1) {
|
||||
try { stop[i]?.(); } catch { /* ignore */ }
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function installRulesHttp(webServer) {
|
||||
const stop = [];
|
||||
stop.push(registerRoute(webServer, {
|
||||
@@ -1290,13 +1493,14 @@ function registerRoute(webServer, spec) {
|
||||
}
|
||||
}
|
||||
|
||||
async function readJsonBody(request) {
|
||||
async function readJsonBody(request, maxBytes = 512 * 1024) {
|
||||
const chunks = [];
|
||||
let size = 0;
|
||||
const limit = Number.isFinite(maxBytes) && maxBytes > 0 ? maxBytes : 512 * 1024;
|
||||
for await (const chunk of request) {
|
||||
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
||||
size += buffer.length;
|
||||
if (size > 512 * 1024) throw new Error("request body too large");
|
||||
if (size > limit) throw new Error("request body too large");
|
||||
chunks.push(buffer);
|
||||
}
|
||||
const raw = Buffer.concat(chunks).toString("utf8").trim();
|
||||
|
||||
Reference in new issue
Block a user