mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-30 03:40:22 +08:00
feat: 发布 1.1.10,空提示词拦截并修首轮注入
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
56ba72a035
commit
51a9d271dd
9 files changed
+334
-175
No files matched your search
+29
-20
@@ -90,10 +90,20 @@ function stripHarnessPersona(text) {
|
||||
}
|
||||
|
||||
function collectInjectText(purgeSections, fallbackText) {
|
||||
const fallback = typeof fallbackText === "string" ? fallbackText : "";
|
||||
if (fallback) return fallback;
|
||||
const main = purgeSections.find((section) => section.name === "dsh-purge");
|
||||
const fromMain = String(main?.text || "").trim();
|
||||
if (fromMain) return fromMain;
|
||||
return typeof fallbackText === "string" ? fallbackText.trim() : "";
|
||||
return String(main?.text || "");
|
||||
}
|
||||
|
||||
function assemblyHasExactInject(sections, inject) {
|
||||
if (!inject) return true;
|
||||
return sections.some((section) => String(section?.text || "").includes(inject));
|
||||
}
|
||||
|
||||
function ensureFullInject(sections, inject) {
|
||||
if (!inject || assemblyHasExactInject(sections, inject)) return sections;
|
||||
return [{ name: "dsh-purge", text: inject, order: -10000 }, ...sections];
|
||||
}
|
||||
|
||||
function stripIdentityPrefix(text) {
|
||||
@@ -135,13 +145,13 @@ function messageText(message) {
|
||||
|
||||
export function existingSystemPrompt(context) {
|
||||
const events = eventList(context?.agent?.session || context?.session);
|
||||
let latest = "";
|
||||
const texts = [];
|
||||
for (const event of events) {
|
||||
if (event?.type !== "system/message") continue;
|
||||
const text = messageText(event.data?.message || event.data).trim();
|
||||
if (text) latest = text;
|
||||
if (text) texts.push(text);
|
||||
}
|
||||
return latest;
|
||||
return texts.join("\n\n");
|
||||
}
|
||||
|
||||
export function isFirstConversationTurn(context) {
|
||||
@@ -162,13 +172,13 @@ export function isFirstConversationTurn(context) {
|
||||
export function promptAlreadyInjected(existing, injectText) {
|
||||
const inject = String(injectText || "").trim();
|
||||
if (!inject) return false;
|
||||
return String(existing || "").includes(inject.slice(0, Math.min(80, inject.length)));
|
||||
return String(existing || "").includes(inject);
|
||||
}
|
||||
|
||||
export function shouldInjectPrompt(context, injectOnce = true, injectText = "") {
|
||||
if (injectOnce === false) return true;
|
||||
if (isFirstConversationTurn(context)) return true;
|
||||
return !promptAlreadyInjected(existingSystemPrompt(context), injectText);
|
||||
export function shouldInjectPrompt(_context, _injectOnce = true, _injectText = "") {
|
||||
// 0.1.5 in-history:渲染少了注入就会再追加一条 system/message。
|
||||
// 用「最后一条是否含注入」决定本步掏空注入,会 A/B 死循环。每步都必须留下完整注入。
|
||||
return true;
|
||||
}
|
||||
|
||||
export function isMnemonPluginMessage(message) {
|
||||
@@ -199,7 +209,7 @@ function normalizeRewriteOptions(fallbackInject, options) {
|
||||
|
||||
// 0.1.5 优先写入 persona-prefix;无 prefix 时写入 legacy persona。
|
||||
function foldInjectIntoPersona(sections, injectText) {
|
||||
const inject = typeof injectText === "string" ? injectText.trim() : "";
|
||||
const inject = typeof injectText === "string" ? injectText : "";
|
||||
const names = sections.map((s) => String(s?.name || ""));
|
||||
const hasPrefix = names.some((n) => PERSONA_PREFIX_NAMES.has(n));
|
||||
let folded = false;
|
||||
@@ -207,16 +217,16 @@ function foldInjectIntoPersona(sections, injectText) {
|
||||
const name = String(section?.name || "");
|
||||
if (!isPersonaFoldTarget(name, hasPrefix)) return section;
|
||||
folded = true;
|
||||
const prior = stripIdentityPrefix(String(section.text || "").trim());
|
||||
const prior = stripIdentityPrefix(String(section.text || ""));
|
||||
if (!inject) return { ...section, text: prior };
|
||||
if (prior.includes(inject.slice(0, Math.min(80, inject.length)))) {
|
||||
return { ...section, text: prior };
|
||||
}
|
||||
if (prior.includes(inject)) return { ...section, text: prior };
|
||||
return { ...section, text: prior ? `${inject}\n\n${prior}` : inject };
|
||||
});
|
||||
if (folded) return out;
|
||||
if (!inject) return out;
|
||||
return [{ name: "deployment:persona-prefix", text: inject }, ...out];
|
||||
const orders = sections.map((s) => s?.order).filter((n) => Number.isFinite(n));
|
||||
const order = orders.length ? Math.min(0, ...orders) : 0;
|
||||
return [{ name: "deployment:persona-prefix", text: inject, order }, ...out];
|
||||
}
|
||||
|
||||
export function rewritePromptAssembly(assembled, fallbackInject = "", options = {}) {
|
||||
@@ -233,18 +243,17 @@ export function rewritePromptAssembly(assembled, fallbackInject = "", options =
|
||||
next.text = stripHarnessPersona(next.text);
|
||||
}
|
||||
if (String(next.name).startsWith("dsh-purge")) {
|
||||
if (!opts.fallbackInject) continue;
|
||||
purge.push(next);
|
||||
} else {
|
||||
rest.push(next);
|
||||
}
|
||||
}
|
||||
|
||||
const injectText = opts.fallbackInject ? collectInjectText(purge, opts.fallbackInject) : "";
|
||||
const injectText = collectInjectText(purge, opts.fallbackInject);
|
||||
const folded = foldInjectIntoPersona(rest, injectText);
|
||||
const extras = opts.dropPurgeAfterFold
|
||||
? purge.filter((section) => section.name !== "dsh-purge")
|
||||
: purge;
|
||||
assembled.sections = [...folded, ...extras];
|
||||
assembled.sections = ensureFullInject([...folded, ...extras], injectText);
|
||||
return assembled;
|
||||
}
|
||||
Reference in new issue
Block a user