mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-30 16:10:22 +08:00
fix: 1.1.45 第二轮发送不再因 undefined.length 直接死掉。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
6f50df28f3
commit
11aa390cc3
5 files changed
+81
-62
No files matched your search
+19
-49
@@ -582,44 +582,27 @@ function withOwnServers(injectText) {
|
||||
}
|
||||
}
|
||||
|
||||
/** 同一会话内系统提示文本钉死,避免每步改写触发模型二次读人设后软拒绝。 */
|
||||
const pinnedSectionsBySession = new Map();
|
||||
|
||||
function sessionPinId(context) {
|
||||
const session = sessionOfContext(context);
|
||||
const id = session?.id ?? session?.header?.id;
|
||||
return id == null || id === "" ? "" : String(id);
|
||||
}
|
||||
|
||||
function clonePinnedSections(sections) {
|
||||
return sections.map((section) => {
|
||||
if (!section || typeof section !== "object") return section;
|
||||
return { ...section, text: typeof section.text === "string" ? section.text : section.text };
|
||||
});
|
||||
}
|
||||
|
||||
function sectionsFingerprint(sections) {
|
||||
return sections
|
||||
.map((section) => `${String(section?.name || "")}\n${typeof section?.text === "string" ? section.text : ""}`)
|
||||
.join("\n\u0001\n");
|
||||
}
|
||||
|
||||
function pinHasRoleOrCard(sections) {
|
||||
return sections.some((section) => {
|
||||
const name = String(section?.name || "");
|
||||
return name === "dsh-purge:redteam-role" || name === "dsh-purge:objective";
|
||||
});
|
||||
}
|
||||
|
||||
export function clearPinnedSystemPrompt(sessionId) {
|
||||
if (sessionId == null || sessionId === "") return;
|
||||
pinnedSectionsBySession.delete(String(sessionId));
|
||||
/** 组装结果里每段 text 必须是字符串;否则官方 renderPrompt 的 text.length 会直接炸掉。 */
|
||||
function sanitizeAssemblySections(sections) {
|
||||
const list = Array.isArray(sections) ? sections : [];
|
||||
const out = [];
|
||||
for (const section of list) {
|
||||
if (!section || typeof section !== "object") continue;
|
||||
if (typeof section.text === "function") {
|
||||
out.push(section);
|
||||
continue;
|
||||
}
|
||||
out.push({
|
||||
...section,
|
||||
text: typeof section.text === "string" ? section.text : "",
|
||||
});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function rewritePromptAssembly(assembled, fallbackInject = "", options = {}) {
|
||||
if (!assembled || !Array.isArray(assembled.sections)) return assembled;
|
||||
const opts = normalizeRewriteOptions(fallbackInject, options);
|
||||
const pinId = sessionPinId(opts.context);
|
||||
const rest = [];
|
||||
const purge = [];
|
||||
for (const section of assembled.sections) {
|
||||
@@ -659,21 +642,8 @@ export function rewritePromptAssembly(assembled, fallbackInject = "", options =
|
||||
? insertRoleSection(insertCardSection(sections, opts.cardText), opts.roleText)
|
||||
: withOps;
|
||||
}
|
||||
sections = leadWithInject(sections, injectText);
|
||||
|
||||
if (pinId && injectText.trim()) {
|
||||
const pinned = pinnedSectionsBySession.get(pinId);
|
||||
const nextHasRole = Boolean(opts.roleText || opts.cardText) || pinHasRoleOrCard(sections);
|
||||
if (pinned && !(nextHasRole && !pinHasRoleOrCard(pinned))) {
|
||||
assembled.sections = clonePinnedSections(pinned);
|
||||
return assembled;
|
||||
}
|
||||
const freeze = clonePinnedSections(sections);
|
||||
if (!pinned || sectionsFingerprint(freeze) !== sectionsFingerprint(pinned)) {
|
||||
pinnedSectionsBySession.set(pinId, freeze);
|
||||
}
|
||||
}
|
||||
|
||||
assembled.sections = sections;
|
||||
assembled.sections = sanitizeAssemblySections(leadWithInject(sections, injectText));
|
||||
if (!Array.isArray(assembled.contexts)) assembled.contexts = [];
|
||||
if (!Array.isArray(assembled.tools)) assembled.tools = [];
|
||||
return assembled;
|
||||
}
|
||||
Reference in new issue
Block a user