mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 11:30: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
+108
-12
@@ -90,12 +90,9 @@ function stripHarnessPersona(text) {
|
||||
}
|
||||
|
||||
function collectInjectText(purgeSections, fallbackText) {
|
||||
const fromSections = purgeSections
|
||||
.map((s) => String(s.text || "").trim())
|
||||
.filter(Boolean)
|
||||
.join("\n\n")
|
||||
.trim();
|
||||
if (fromSections) return fromSections;
|
||||
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() : "";
|
||||
}
|
||||
|
||||
@@ -116,6 +113,92 @@ function isPersonaFoldTarget(name, preferPrefix) {
|
||||
return PERSONA_PREFIX_NAMES.has(name) || PERSONA_LEGACY_NAMES.has(name);
|
||||
}
|
||||
|
||||
function eventList(session) {
|
||||
if (!session) return [];
|
||||
if (typeof session.snapshotEvents === "function") {
|
||||
try {
|
||||
const events = session.snapshotEvents();
|
||||
if (Array.isArray(events)) return events;
|
||||
} catch {}
|
||||
}
|
||||
if (Array.isArray(session.events)) return session.events;
|
||||
return [];
|
||||
}
|
||||
|
||||
function messageText(message) {
|
||||
if (typeof message === "string") return message;
|
||||
const content = message?.content;
|
||||
if (typeof content === "string") return content;
|
||||
if (!Array.isArray(content)) return "";
|
||||
return content.map((block) => (typeof block?.text === "string" ? block.text : "")).join("\n");
|
||||
}
|
||||
|
||||
export function existingSystemPrompt(context) {
|
||||
const events = eventList(context?.agent?.session || context?.session);
|
||||
let latest = "";
|
||||
for (const event of events) {
|
||||
if (event?.type !== "system/message") continue;
|
||||
const text = messageText(event.data?.message || event.data).trim();
|
||||
if (text) latest = text;
|
||||
}
|
||||
return latest;
|
||||
}
|
||||
|
||||
export function isFirstConversationTurn(context) {
|
||||
if (!context || typeof context !== "object") return true;
|
||||
const explicit = context.turn ?? context.turnIndex ?? context.userTurn ?? context.messageIndex;
|
||||
if (explicit != null && Number.isFinite(Number(explicit))) return Number(explicit) <= 1;
|
||||
const events = eventList(context.agent?.session || context.session);
|
||||
if (events.length > 0) return !events.some((event) => event?.type === "turn/end");
|
||||
const msgs = context.messages || context.history || [];
|
||||
if (!Array.isArray(msgs) || msgs.length === 0) return true;
|
||||
const users = msgs.filter((m) => {
|
||||
const role = String(m?.role || m?.type || m?.kind || "").toLowerCase();
|
||||
return role === "user" || role === "human";
|
||||
});
|
||||
return users.length <= 1;
|
||||
}
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
export function shouldInjectPrompt(context, injectOnce = true, injectText = "") {
|
||||
if (injectOnce === false) return true;
|
||||
if (isFirstConversationTurn(context)) return true;
|
||||
return !promptAlreadyInjected(existingSystemPrompt(context), injectText);
|
||||
}
|
||||
|
||||
export function isMnemonPluginMessage(message) {
|
||||
const source = message?.source;
|
||||
if (!source || typeof source !== "object") return false;
|
||||
const plugin = String(source.plugin || "").toLowerCase();
|
||||
return source.kind === "plugin" && plugin.includes("mnemon");
|
||||
}
|
||||
|
||||
function isMnemonSection(name) {
|
||||
return String(name || "").toLowerCase().includes("mnemon");
|
||||
}
|
||||
|
||||
function normalizeRewriteOptions(fallbackInject, options) {
|
||||
if (fallbackInject && typeof fallbackInject === "object" && !Array.isArray(fallbackInject)) {
|
||||
return {
|
||||
fallbackInject: typeof fallbackInject.fallbackInject === "string" ? fallbackInject.fallbackInject : "",
|
||||
dropMnemon: fallbackInject.dropMnemon !== false,
|
||||
dropPurgeAfterFold: fallbackInject.dropPurgeAfterFold !== false,
|
||||
pinPersonaText: typeof fallbackInject.pinPersonaText === "string" ? fallbackInject.pinPersonaText : "",
|
||||
};
|
||||
}
|
||||
return {
|
||||
fallbackInject: typeof fallbackInject === "string" ? fallbackInject : "",
|
||||
dropMnemon: options?.dropMnemon !== false,
|
||||
dropPurgeAfterFold: options?.dropPurgeAfterFold !== false,
|
||||
pinPersonaText: typeof options?.pinPersonaText === "string" ? options.pinPersonaText : "",
|
||||
};
|
||||
}
|
||||
|
||||
// 0.1.5 优先写入 persona-prefix;无 prefix 时写入 legacy persona。
|
||||
function foldInjectIntoPersona(sections, injectText) {
|
||||
const inject = typeof injectText === "string" ? injectText.trim() : "";
|
||||
@@ -138,23 +221,36 @@ function foldInjectIntoPersona(sections, injectText) {
|
||||
return [{ name: "deployment:persona-prefix", text: inject }, ...out];
|
||||
}
|
||||
|
||||
export function rewritePromptAssembly(assembled, fallbackInject = "") {
|
||||
export function rewritePromptAssembly(assembled, fallbackInject = "", options = {}) {
|
||||
if (!assembled || !Array.isArray(assembled.sections)) return assembled;
|
||||
const opts = normalizeRewriteOptions(fallbackInject, options);
|
||||
if (opts.pinPersonaText) {
|
||||
assembled.sections = [{ name: "deployment:persona-prefix", text: opts.pinPersonaText }];
|
||||
return assembled;
|
||||
}
|
||||
const rest = [];
|
||||
const purge = [];
|
||||
for (const section of assembled.sections) {
|
||||
if (!section || section.name === "harness:identity") continue;
|
||||
if (section.name === "dsh-purge:identity") continue;
|
||||
if (opts.dropMnemon && isMnemonSection(section.name)) continue;
|
||||
const next = { ...section };
|
||||
if (typeof next.text === "string" && !String(next.name).startsWith("dsh-purge")) {
|
||||
next.text = stripHarnessPersona(next.text);
|
||||
}
|
||||
if (String(next.name).startsWith("dsh-purge")) purge.push(next);
|
||||
else rest.push(next);
|
||||
if (String(next.name).startsWith("dsh-purge")) {
|
||||
if (!opts.fallbackInject) continue;
|
||||
purge.push(next);
|
||||
} else {
|
||||
rest.push(next);
|
||||
}
|
||||
}
|
||||
|
||||
// fallbackInject:section 被滤掉时从 prompt-inject.md 补回。
|
||||
const injectText = collectInjectText(purge, fallbackInject);
|
||||
assembled.sections = [...foldInjectIntoPersona(rest, injectText), ...purge];
|
||||
const injectText = opts.fallbackInject ? 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];
|
||||
return assembled;
|
||||
}
|
||||
Reference in new issue
Block a user