装配只补缺不重排,消息整形也不再把尾部系统提示搬到前面。
工作台左轨的顺序被当成真实顺序用:补洞时只插缺的、不动已有的条目;整形把落单的 system 接在最后一条后面,之前是插在它前面,排出来的正文顺序跟左轨对不上。
This commit is contained in:
+32
-45
@@ -28,7 +28,7 @@ export const WORLD_BOOK_AFTER = "worldBookAfter";
|
||||
export const CURRENT_TURN = "currentTurn";
|
||||
export const POST_TURN = "postTurn";
|
||||
|
||||
/** 导入时插入的自有洞(不含共用的 chatHistory)。 */
|
||||
/** 运行时填洞的四个自有洞(不含共用的 chatHistory);缺失时自动补进顺序。 */
|
||||
export const APP_INSERTED_MARKERS = [
|
||||
WORLD_BOOK_BEFORE,
|
||||
WORLD_BOOK_AFTER,
|
||||
@@ -36,8 +36,6 @@ export const APP_INSERTED_MARKERS = [
|
||||
POST_TURN,
|
||||
] as const;
|
||||
|
||||
const APP_INSERTED_SET = new Set<string>(APP_INSERTED_MARKERS);
|
||||
|
||||
type MarkerDef = {
|
||||
id: string;
|
||||
name: string;
|
||||
@@ -78,8 +76,10 @@ function reindex(
|
||||
}
|
||||
|
||||
/**
|
||||
* 以 chatHistory 为锚点插入四个自有 marker。
|
||||
* 可重复调用:先摘掉已插入的自有洞,再按规则放回。
|
||||
* 以 chatHistory 为锚点补齐四个自有 marker:**只补缺的,不动已有的**。
|
||||
* 已经排进 `prompt_order` 的洞保留作者给的顺序——工作台左轨就是真实发送顺序;
|
||||
* 缺的洞按酒馆的规范位置插进来:历史前 → 对话历史 → 历史后 → 本轮对话 → … → 本轮之后。
|
||||
* 可重复调用:补齐之后再调一次是无操作(之前会把这四个洞重新搅回锚点,覆盖作者排的顺序)。
|
||||
*/
|
||||
export function applyExtendedMarkers(preset: PresetPackage): PresetPackage {
|
||||
let prompts = [...preset.prompts];
|
||||
@@ -87,59 +87,46 @@ export function applyExtendedMarkers(preset: PresetPackage): PresetPackage {
|
||||
prompts = upsertMarkerEntry(prompts, def);
|
||||
}
|
||||
|
||||
const rawOrder = [...preset.promptOrder]
|
||||
.sort((a, b) => a.orderIndex - b.orderIndex)
|
||||
.filter((item) => !APP_INSERTED_SET.has(item.promptId));
|
||||
|
||||
const histIndex = rawOrder.findIndex(
|
||||
(item) => item.promptId === CHAT_HISTORY_MARKER,
|
||||
const enabledOf = new Map(
|
||||
preset.promptOrder.map((item) => [item.promptId, item.enabled]),
|
||||
);
|
||||
const next = [...preset.promptOrder].sort((a, b) => a.orderIndex - b.orderIndex);
|
||||
const has = (id: string) => next.some((item) => item.promptId === id);
|
||||
const indexOf = (id: string) => next.findIndex((item) => item.promptId === id);
|
||||
const insertAt = (index: number, id: string) => {
|
||||
const item: PresetPromptOrderItem = {
|
||||
promptId: id,
|
||||
enabled: enabledOf.get(id) ?? true,
|
||||
orderIndex: 0,
|
||||
};
|
||||
next.splice(Math.max(0, Math.min(index, next.length)), 0, item);
|
||||
};
|
||||
|
||||
// 预设自带酒馆的 worldInfoBefore / worldInfoAfter(并启用)时,不插自有的世界书洞:
|
||||
// 内容直接填进预设那两条,位置上才对得上酒馆。
|
||||
const hasStBefore = rawOrder.some(
|
||||
const hasStBefore = next.some(
|
||||
(item) => item.promptId === WORLD_INFO_BEFORE_MARKER && item.enabled,
|
||||
);
|
||||
const hasStAfter = rawOrder.some(
|
||||
const hasStAfter = next.some(
|
||||
(item) => item.promptId === WORLD_INFO_AFTER_MARKER && item.enabled,
|
||||
);
|
||||
const inserted = (id: string): PresetPromptOrderItem => ({
|
||||
promptId: id,
|
||||
enabled: true,
|
||||
orderIndex: 0,
|
||||
});
|
||||
|
||||
const beforeMarkers = hasStBefore ? [] : [inserted(WORLD_BOOK_BEFORE)];
|
||||
const afterMarkers = hasStAfter ? [] : [inserted(WORLD_BOOK_AFTER)];
|
||||
|
||||
let next: PresetPromptOrderItem[];
|
||||
if (histIndex < 0) {
|
||||
next = reindex([
|
||||
...rawOrder,
|
||||
...beforeMarkers,
|
||||
inserted(CHAT_HISTORY_MARKER),
|
||||
...afterMarkers,
|
||||
inserted(CURRENT_TURN),
|
||||
inserted(POST_TURN),
|
||||
]);
|
||||
} else {
|
||||
const before = rawOrder.slice(0, histIndex);
|
||||
const history = rawOrder[histIndex]!;
|
||||
const after = rawOrder.slice(histIndex + 1);
|
||||
next = reindex([
|
||||
...before,
|
||||
...beforeMarkers,
|
||||
history,
|
||||
...afterMarkers,
|
||||
inserted(CURRENT_TURN),
|
||||
...after,
|
||||
inserted(POST_TURN),
|
||||
]);
|
||||
if (!has(CHAT_HISTORY_MARKER)) insertAt(next.length, CHAT_HISTORY_MARKER);
|
||||
if (!has(WORLD_BOOK_BEFORE) && !hasStBefore) insertAt(indexOf(CHAT_HISTORY_MARKER), WORLD_BOOK_BEFORE);
|
||||
if (!has(WORLD_BOOK_AFTER) && !hasStAfter) insertAt(indexOf(CHAT_HISTORY_MARKER) + 1, WORLD_BOOK_AFTER);
|
||||
if (!has(CURRENT_TURN)) {
|
||||
const afterWorldBook = indexOf(WORLD_BOOK_AFTER);
|
||||
insertAt(
|
||||
afterWorldBook >= 0 ? afterWorldBook + 1 : indexOf(CHAT_HISTORY_MARKER) + 1,
|
||||
CURRENT_TURN,
|
||||
);
|
||||
}
|
||||
if (!has(POST_TURN)) insertAt(next.length, POST_TURN);
|
||||
|
||||
return {
|
||||
...preset,
|
||||
prompts,
|
||||
promptOrder: next,
|
||||
promptOrder: reindex(next),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user