mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-30 16:10:22 +08:00
feat: 发布 1.1.2,兼容接口 developer 角色与规则集读写
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
3f5c0c8281
commit
8f860d4a53
6 files changed
+239
-186
No files matched your search
+116
-125
@@ -951,141 +951,130 @@ function methodNotAllowed(request, response, allow) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function installRulesHttp(webServer) {
|
||||
async function handleRulesOp(op, body, queryId) {
|
||||
const dshHome = core.findDshHome();
|
||||
|
||||
webServer.register({
|
||||
kind: "exact",
|
||||
path: "/dsh-purge/rules/status",
|
||||
handler: async (request, response) => {
|
||||
if (methodNotAllowed(request, response, ["GET"])) return;
|
||||
try {
|
||||
sendJson(response, 200, { ok: true, ...(await rules.rulesStatus(dshHome)) });
|
||||
} catch (e) {
|
||||
sendJson(response, 500, { ok: false, error: String(e) });
|
||||
}
|
||||
},
|
||||
}, "dsh-purge: rules status");
|
||||
|
||||
webServer.register({
|
||||
kind: "exact",
|
||||
path: "/dsh-purge/rules/read",
|
||||
handler: async (request, response) => {
|
||||
if (methodNotAllowed(request, response, ["GET"])) return;
|
||||
try {
|
||||
const url = new URL(request.url ?? "", "http://localhost");
|
||||
const id = url.searchParams.get("id") ?? "";
|
||||
if (!rules.validRuleId(id)) {
|
||||
sendJson(response, 400, { ok: false, error: "invalid id" });
|
||||
return;
|
||||
}
|
||||
const content = await rules.readRule(dshHome, id);
|
||||
if (content === null) {
|
||||
sendJson(response, 404, { ok: false, error: `rule not found: ${id}` });
|
||||
return;
|
||||
}
|
||||
const meta = await rules.readRuleMeta(dshHome, id);
|
||||
sendJson(response, 200, { ok: true, id, name: meta.name, target: meta.target, content });
|
||||
} catch (e) {
|
||||
sendJson(response, 500, { ok: false, error: String(e) });
|
||||
}
|
||||
},
|
||||
}, "dsh-purge: rules read");
|
||||
|
||||
webServer.register({
|
||||
kind: "exact",
|
||||
path: "/dsh-purge/rules/save",
|
||||
handler: async (request, response) => {
|
||||
if (methodNotAllowed(request, response, ["POST"])) return;
|
||||
try {
|
||||
const body = await readJsonBody(request);
|
||||
const id = typeof body?.id === "string" ? body.id : "";
|
||||
const content = typeof body?.content === "string" ? body.content : null;
|
||||
if (!rules.validRuleId(id) || content === null) {
|
||||
sendJson(response, 400, { ok: false, error: "id and content (string) required" });
|
||||
return;
|
||||
}
|
||||
await rules.saveRule(dshHome, id, content, {
|
||||
name: typeof body?.name === "string" ? body.name : undefined,
|
||||
target: typeof body?.target === "string" ? body.target : undefined,
|
||||
});
|
||||
sendJson(response, 200, { ok: true, id });
|
||||
} catch (e) {
|
||||
sendJson(response, 500, { ok: false, error: String(e) });
|
||||
}
|
||||
},
|
||||
}, "dsh-purge: rules save");
|
||||
|
||||
webServer.register({
|
||||
kind: "exact",
|
||||
path: "/dsh-purge/rules/activate",
|
||||
handler: async (request, response) => {
|
||||
if (methodNotAllowed(request, response, ["POST"])) return;
|
||||
try {
|
||||
const body = await readJsonBody(request);
|
||||
const id = typeof body?.id === "string" ? body.id : "";
|
||||
if (!rules.validRuleId(id)) {
|
||||
sendJson(response, 400, { ok: false, error: "invalid id" });
|
||||
return;
|
||||
}
|
||||
const meta = await rules.activateRule(dshHome, id);
|
||||
sendJson(response, 200, {
|
||||
ok: true,
|
||||
id,
|
||||
name: meta.name,
|
||||
target: meta.target,
|
||||
agents_path: rules.targetPath(dshHome, meta.target),
|
||||
note: "已写入目标文件并注入 systemPrompt,新会话生效",
|
||||
});
|
||||
} catch (e) {
|
||||
sendJson(response, 500, { ok: false, error: String(e) });
|
||||
}
|
||||
},
|
||||
}, "dsh-purge: rules activate");
|
||||
|
||||
webServer.register({
|
||||
kind: "exact",
|
||||
path: "/dsh-purge/rules/delete",
|
||||
handler: async (request, response) => {
|
||||
if (methodNotAllowed(request, response, ["POST"])) return;
|
||||
try {
|
||||
const body = await readJsonBody(request);
|
||||
const id = typeof body?.id === "string" ? body.id : "";
|
||||
if (!rules.validRuleId(id)) {
|
||||
sendJson(response, 400, { ok: false, error: "invalid id" });
|
||||
return;
|
||||
}
|
||||
await rules.deleteRule(dshHome, id);
|
||||
sendJson(response, 200, { ok: true, id });
|
||||
} catch (e) {
|
||||
sendJson(response, 500, { ok: false, error: String(e) });
|
||||
}
|
||||
},
|
||||
}, "dsh-purge: rules delete");
|
||||
|
||||
webServer.register({
|
||||
kind: "exact",
|
||||
path: "/dsh-purge/rules/reset",
|
||||
handler: async (request, response) => {
|
||||
if (methodNotAllowed(request, response, ["POST"])) return;
|
||||
try {
|
||||
const r = await rules.resetToOriginal(dshHome);
|
||||
sendJson(response, 200, { ok: true, ...r, note: "已还原出厂状态(无全局指令);规则库保留" });
|
||||
} catch (e) {
|
||||
sendJson(response, 500, { ok: false, error: String(e) });
|
||||
}
|
||||
},
|
||||
}, "dsh-purge: rules reset");
|
||||
const id = typeof body?.id === "string" ? body.id : String(queryId || "");
|
||||
if (op === "status") {
|
||||
return { ok: true, ...(await rules.rulesStatus(dshHome)) };
|
||||
}
|
||||
if (op === "read") {
|
||||
if (!rules.validRuleId(id)) return { status: 400, ok: false, error: "invalid id" };
|
||||
const content = await rules.readRule(dshHome, id);
|
||||
if (content === null) return { status: 404, ok: false, error: `rule not found: ${id}` };
|
||||
const meta = await rules.readRuleMeta(dshHome, id);
|
||||
return { ok: true, id, name: meta.name, target: meta.target, content };
|
||||
}
|
||||
if (op === "save") {
|
||||
const content = typeof body?.content === "string" ? body.content : null;
|
||||
if (!rules.validRuleId(id) || content === null) {
|
||||
return { status: 400, ok: false, error: "id and content (string) required" };
|
||||
}
|
||||
await rules.saveRule(dshHome, id, content, {
|
||||
name: typeof body?.name === "string" ? body.name : undefined,
|
||||
target: typeof body?.target === "string" ? body.target : undefined,
|
||||
});
|
||||
return { ok: true, id };
|
||||
}
|
||||
if (op === "activate") {
|
||||
if (!rules.validRuleId(id)) return { status: 400, ok: false, error: "invalid id" };
|
||||
const meta = await rules.activateRule(dshHome, id);
|
||||
return {
|
||||
ok: true,
|
||||
id,
|
||||
name: meta.name,
|
||||
target: meta.target,
|
||||
agents_path: rules.targetPath(dshHome, meta.target),
|
||||
note: "已写入目标文件并注入 systemPrompt,新会话生效",
|
||||
};
|
||||
}
|
||||
if (op === "delete") {
|
||||
if (!rules.validRuleId(id)) return { status: 400, ok: false, error: "invalid id" };
|
||||
await rules.deleteRule(dshHome, id);
|
||||
return { ok: true, id };
|
||||
}
|
||||
if (op === "reset") {
|
||||
const r = await rules.resetToOriginal(dshHome);
|
||||
return { ok: true, ...r, note: "已还原出厂状态(无全局指令);规则库保留" };
|
||||
}
|
||||
return { status: 400, ok: false, error: `unknown op: ${op}` };
|
||||
}
|
||||
|
||||
function rulesHttpHandler(forcedOp) {
|
||||
return async (request, response) => {
|
||||
if (request.method === "OPTIONS") {
|
||||
sendOptions(response);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const url = new URL(request.url ?? "", "http://127.0.0.1");
|
||||
const tail = url.pathname.replace(/^\/dsh-purge\/rules\/?/, "").replace(/\/$/, "");
|
||||
let body = {};
|
||||
if (request.method === "POST") body = await readJsonBody(request);
|
||||
else if (methodNotAllowed(request, response, forcedOp ? ["GET", "POST"] : ["GET", "POST"])) return;
|
||||
const op = forcedOp || body.op || tail || "status";
|
||||
const result = await handleRulesOp(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 installRulesHttp(webServer) {
|
||||
const stop = [];
|
||||
stop.push(registerRoute(webServer, {
|
||||
kind: "exact",
|
||||
path: "/dsh-purge/rules",
|
||||
handler: rulesHttpHandler(""),
|
||||
}));
|
||||
stop.push(registerRoute(webServer, {
|
||||
kind: "prefix",
|
||||
path: "/dsh-purge/rules",
|
||||
handler: rulesHttpHandler(""),
|
||||
}));
|
||||
for (const op of ["status", "read", "save", "activate", "delete", "reset"]) {
|
||||
stop.push(registerRoute(webServer, {
|
||||
kind: "exact",
|
||||
path: `/dsh-purge/rules/${op}`,
|
||||
handler: rulesHttpHandler(op),
|
||||
}));
|
||||
}
|
||||
return () => {
|
||||
for (let i = stop.length - 1; i >= 0; i -= 1) {
|
||||
try { stop[i]?.(); } catch { /* ignore */ }
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const CORS_HEAD = {
|
||||
"access-control-allow-origin": "*",
|
||||
"access-control-allow-methods": "GET, POST, OPTIONS",
|
||||
"access-control-allow-headers": "content-type",
|
||||
};
|
||||
|
||||
function sendJson(response, status, payload) {
|
||||
response.writeHead(status, {
|
||||
"cache-control": "no-store",
|
||||
"content-type": "application/json; charset=utf-8",
|
||||
...CORS_HEAD,
|
||||
});
|
||||
response.end(JSON.stringify(payload));
|
||||
}
|
||||
|
||||
function sendOptions(response) {
|
||||
response.writeHead(204, { ...CORS_HEAD, "access-control-max-age": "86400" });
|
||||
response.end();
|
||||
}
|
||||
|
||||
function registerRoute(webServer, spec) {
|
||||
try {
|
||||
return webServer.register(spec);
|
||||
} catch (e) {
|
||||
console.warn("[dsh-purge] route", spec?.path, String(e && e.message ? e.message : e));
|
||||
return () => {};
|
||||
}
|
||||
}
|
||||
|
||||
async function readJsonBody(request) {
|
||||
const chunks = [];
|
||||
let size = 0;
|
||||
@@ -1095,5 +1084,7 @@ async function readJsonBody(request) {
|
||||
if (size > 512 * 1024) throw new Error("request body too large");
|
||||
chunks.push(buffer);
|
||||
}
|
||||
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
||||
const raw = Buffer.concat(chunks).toString("utf8").trim();
|
||||
if (!raw) return {};
|
||||
return JSON.parse(raw);
|
||||
}
|
||||
Reference in new issue
Block a user