feat: 发布 1.1.2,兼容接口 developer 角色与规则集读写

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
叫我小杨同学andCursor committed 2026-09-18 10:54:43 +08:00
1 parent 7642678a27
commit ea2632cd75
6 files changed
+239 -186

No files matched your search

+83
View File
@@ -712,6 +712,44 @@ function listMnemonIndexFiles(dshHome) {
return out;
}
function listPiAiApiFiles(dshHome, aiBase, filename) {
const out = [];
const seen = new Set();
const push = (fp) => {
if (!fp || !isFile(fp)) return;
let key = fp;
try { key = fs.realpathSync(fp); } catch { /* keep fp */ }
key = String(key).toLowerCase();
if (seen.has(key)) return;
seen.add(key);
out.push(fp);
};
const rel = ["@earendil-works", "pi-ai", "dist", "api", filename];
if (aiBase) {
push(path.join(aiBase, "..", ...rel));
push(path.join(aiBase, "dsh", "node_modules", ...rel));
push(path.join(aiBase, "..", "@deepseek-ai", "dsh", "node_modules", ...rel));
}
for (const home of [dshHome, ...desktopDshHomeGuesses()].filter(Boolean)) {
push(path.join(home, "profiles", "node_modules", ...rel));
push(path.join(home, "profiles", "node_modules", "@deepseek-ai", "dsh", "node_modules", ...rel));
const profilesRoot = path.join(home, "profiles");
if (!isDir(profilesRoot)) continue;
let names = [];
try {
names = fs.readdirSync(profilesRoot, { withFileTypes: true }).filter((d) => d.isDirectory());
} catch {
names = [];
}
for (const ent of names) {
const base = path.join(profilesRoot, ent.name, "node_modules");
push(path.join(base, ...rel));
push(path.join(base, "@deepseek-ai", "dsh", "node_modules", ...rel));
}
}
return out;
}
// 删 supervisor.cmd/vbs,不要留空文件,否则 doctor 每轮都会 schtasks /Delete。
export function stubDoctorSupervisor() {
if (process.platform !== "win32") return { skipped: "not_win32" };
@@ -2781,6 +2819,49 @@ const NEW_TOOL_PATCHES = [
],
markers: ["[dsh-purge] complete prompt keeps inject"],
},
{
// 自定义 OpenAI 兼容接口不认 developer;推理模型默认会把 system 收成这个角色,直接 400。
id: 42, name: "DEVELOPER_ROLE_TO_SYSTEM", layer: "0.1.5 兼容", layer_en: "0.1.5",
desc: "developer 角色改回 system,兼容接口不再 400 / remap developer role to system",
file: ["pi-ai-completions", "pi-ai-responses-shared"],
optional: true,
skipIfMarked: true,
replacements: [
{
pattern:
" const useDeveloperRole = model.reasoning && compat.supportsDeveloperRole;\n" +
" const role = useDeveloperRole ? \"developer\" : \"system\";",
replace:
" const useDeveloperRole = false; // [dsh-purge] compatible APIs reject developer\n" +
" const role = useDeveloperRole ? \"developer\" : \"system\";",
},
{
pattern:
" lastRole = msg.role;\n" +
" }\n" +
" return params;\n" +
"}\n" +
"function convertTools(",
replace:
" lastRole = msg.role;\n" +
" }\n" +
" for (const message of params) {\n" +
" if (message && message.role === \"developer\") message.role = \"system\";\n" +
" } // [dsh-purge] compatible APIs reject developer\n" +
" return params;\n" +
"}\n" +
"function convertTools(",
},
{
pattern:
" const role = model.reasoning && compat?.supportsDeveloperRole !== false ? \"developer\" : \"system\";",
replace:
" const role = \"system\"; // [dsh-purge] compatible APIs reject developer",
},
],
markersAny: true,
markers: ["[dsh-purge] compatible APIs reject developer"],
},
];
export const ALL_PATCHES = [...PATCHES, ...CODE_PATCHES, ...ENGINE_PATCHES, ...NEW_TOOL_PATCHES];
@@ -3101,6 +3182,8 @@ export function targetFiles(aiBase, dshHome = findDshHome()) {
"hooks-codex-nested": firstExisting([
path.join(aiBase, "dsh", "node_modules", "@deepseek-ai", "dsh-hooks-codex", "lib", "index.js"),
]),
"pi-ai-completions": allExisting(listPiAiApiFiles(dshHome, aiBase, "openai-completions.js")),
"pi-ai-responses-shared": allExisting(listPiAiApiFiles(dshHome, aiBase, "openai-responses-shared.js")),
};
}
+116 -125
View File
@@ -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);
}