mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 12:00:22 +08:00
feat: 发布 1.1.3,提示词只注入一次并修复插件加载
This commit is contained in:
1 parent
ea2632cd75
commit
d49a3b5eed
10 files changed
+434
-61
No files matched your search
+79
-14
@@ -2,13 +2,17 @@ import fs from "node:fs";
|
||||
import { promises as fsp } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { spawn } from "node:child_process";
|
||||
import { AsyncLocalStorage } from "node:async_hooks";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import * as core from "./core.js";
|
||||
import * as rules from "./rules.js";
|
||||
import * as updater from "./update.js";
|
||||
import {
|
||||
GLOBAL_PROMPT_ORDER,
|
||||
existingSystemPrompt,
|
||||
isMnemonPluginMessage,
|
||||
rewritePromptAssembly,
|
||||
shouldInjectPrompt,
|
||||
} from "./identity.js";
|
||||
import * as rewind from "./rewind.js";
|
||||
|
||||
@@ -21,6 +25,8 @@ const DEFAULTS = {
|
||||
autoApplyOnStart: true,
|
||||
autoUpdateOnStart: true,
|
||||
autoRevertOnMissing: false,
|
||||
injectOnce: true,
|
||||
stripMnemon: true,
|
||||
postPrompt: "",
|
||||
postPromptOrder: 5100,
|
||||
verbose: false,
|
||||
@@ -108,12 +114,20 @@ function readInjectFile(injectFile) {
|
||||
}
|
||||
}
|
||||
|
||||
const assembleState = new AsyncLocalStorage();
|
||||
|
||||
function injectAllowedNow(config) {
|
||||
if (config.enabled === false) return false;
|
||||
const state = assembleState.getStore();
|
||||
return !state || state.allow !== false;
|
||||
}
|
||||
|
||||
export function registerPromptSections(ctx, config, injectFile) {
|
||||
ctx.systemPrompt.section({
|
||||
name: "dsh-purge",
|
||||
order: GLOBAL_PROMPT_ORDER,
|
||||
text: () => {
|
||||
if (!config.enabled) return "";
|
||||
if (!injectAllowedNow(config)) return "";
|
||||
return readInjectFile(injectFile);
|
||||
},
|
||||
});
|
||||
@@ -122,7 +136,7 @@ export function registerPromptSections(ctx, config, injectFile) {
|
||||
name: "dsh-purge:rules",
|
||||
order: GLOBAL_PROMPT_ORDER + 1,
|
||||
text: () => {
|
||||
if (!config.enabled) return "";
|
||||
if (!injectAllowedNow(config)) return "";
|
||||
try {
|
||||
return rules.activeRuleText(core.findDshHome());
|
||||
} catch {
|
||||
@@ -139,7 +153,7 @@ export function registerPromptSections(ctx, config, injectFile) {
|
||||
ctx.systemPrompt.section({
|
||||
name: "dsh-purge:post",
|
||||
order: config.postPromptOrder,
|
||||
text: postPrompt,
|
||||
text: () => (injectAllowedNow(config) ? postPrompt : ""),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -165,26 +179,50 @@ function readInjectFallback() {
|
||||
}
|
||||
}
|
||||
|
||||
function installIdentityOverride(ctx) {
|
||||
function installMnemonMessageStrip(ctx, config = {}) {
|
||||
if (config.stripMnemon === false) return;
|
||||
if (typeof ctx.on !== "function") return;
|
||||
const hook = async (_assembly, _context, next) => {
|
||||
const assembled = typeof next === "function" ? await next() : _assembly;
|
||||
return rewritePromptAssembly(assembled, readInjectFallback());
|
||||
if (ctx.__dshPurgeMnemonStrip) return;
|
||||
ctx.__dshPurgeMnemonStrip = true;
|
||||
const wrap = async (_payload, next) => {
|
||||
const decision = await next();
|
||||
if (!decision || !Array.isArray(decision.messages)) return decision;
|
||||
const messages = decision.messages.filter((message) => !isMnemonPluginMessage(message));
|
||||
if (messages.length === decision.messages.length) return decision;
|
||||
return { ...decision, messages };
|
||||
};
|
||||
// prepend:false 先于 phase-1 滤镜;prepend:true 在滤镜之后再折一次。
|
||||
try {
|
||||
ctx.on("system-prompt/assemble", hook, { global: true, prepend: false });
|
||||
} catch {}
|
||||
ctx.on("system-prompt/assemble", hook, { global: true, prepend: true });
|
||||
const late = () => {
|
||||
try {
|
||||
ctx.on("system-prompt/assemble", hook, { global: true, prepend: true });
|
||||
ctx.on("agent/pre-step", wrap, { global: true, prepend: true });
|
||||
} catch {}
|
||||
};
|
||||
late();
|
||||
queueMicrotask(late);
|
||||
setTimeout(late, 1500);
|
||||
}
|
||||
|
||||
function installIdentityOverride(ctx, config = {}) {
|
||||
if (typeof ctx.on !== "function") return;
|
||||
if (ctx.__dshPurgeAssembleHook) return;
|
||||
ctx.__dshPurgeAssembleHook = true;
|
||||
const hook = async (_assembly, context, next) => {
|
||||
const inject = config.enabled === false ? "" : readInjectFallback();
|
||||
const allow = config.enabled !== false && shouldInjectPrompt(context, config.injectOnce !== false, inject);
|
||||
const pin = !allow && existingSystemPrompt(context);
|
||||
return assembleState.run({ allow }, async () => {
|
||||
const assembled = typeof next === "function" ? await next() : _assembly;
|
||||
return rewritePromptAssembly(assembled, {
|
||||
fallbackInject: allow ? inject : "",
|
||||
dropMnemon: config.stripMnemon !== false,
|
||||
dropPurgeAfterFold: true,
|
||||
pinPersonaText: pin,
|
||||
});
|
||||
});
|
||||
};
|
||||
// 只挂一次:挂多次会让同一轮 assemble 折两遍,UI 也会显示两次注入。
|
||||
ctx.on("system-prompt/assemble", hook, { global: true, prepend: true });
|
||||
}
|
||||
|
||||
const LAUNCHER_SHIM_NAMES = ["dsh.cmd", "dsh.ps1", "dsh"];
|
||||
|
||||
function desktopShimNeedsPatch(dirs) {
|
||||
@@ -483,7 +521,8 @@ export function apply(ctx, config) {
|
||||
}
|
||||
});
|
||||
|
||||
installIdentityOverride(ctx);
|
||||
installIdentityOverride(ctx, cfg);
|
||||
installMnemonMessageStrip(ctx, cfg);
|
||||
const injectFile = core.findOverrideFile(core.findDshHome());
|
||||
registerPromptSections(ctx, cfg, injectFile);
|
||||
|
||||
@@ -726,6 +765,24 @@ function installWebServer(ctx, cfg) {
|
||||
}
|
||||
},
|
||||
}, "dsh-purge: status");
|
||||
host.webServer.register({
|
||||
kind: "exact",
|
||||
path: "/dsh-purge/last-user",
|
||||
handler: async (request, response) => {
|
||||
if (request.method !== "GET") {
|
||||
response.writeHead(405, { allow: "GET" });
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const url = new URL(request.url, "http://127.0.0.1");
|
||||
const sessionId = String(url.searchParams.get("sessionId") || "").trim();
|
||||
sendJson(response, 200, await rewind.readLastUserText(ctx, sessionId));
|
||||
} catch (e) {
|
||||
sendJson(response, 500, { ok: false, error: String(e) });
|
||||
}
|
||||
},
|
||||
}, "dsh-purge: last user");
|
||||
host.webServer.register({
|
||||
kind: "exact",
|
||||
path: "/dsh-purge/rewind/options",
|
||||
@@ -1088,3 +1145,11 @@ async function readJsonBody(request) {
|
||||
if (!raw) return {};
|
||||
return JSON.parse(raw);
|
||||
}
|
||||
|
||||
const plugin = {
|
||||
name,
|
||||
inject,
|
||||
apply,
|
||||
};
|
||||
|
||||
export default plugin;
|
||||
Reference in new issue
Block a user