Update secondary-filler.js

This commit is contained in:
2025-11-30 23:04:42 +08:00
committed by GitHub
parent 81d9c57290
commit 6941f87c7d

View File

@@ -96,9 +96,47 @@ export async function fillWithSecondaryApi(latestMessage, forceRun = false) {
} }
try { try {
let textToProcess = latestMessage.mes; // --- 延迟填表逻辑 (V151.0) ---
const delay = parseInt(settings.secondary_filler_delay || 0, 10);
const chat = context.chat;
let targetMessage;
let targetIndex;
if (delay > 0) {
// 如果有延迟,我们需要找到“延迟前”的那条消息
// chat.length - 1 是当前最新消息的索引
// 目标索引 = (chat.length - 1) - delay
targetIndex = (chat.length - 1) - delay;
if (targetIndex < 0) {
console.log(`[Amily2-副API] 延迟模式(${delay}): 历史楼层不足,跳过填表。`);
return;
}
targetMessage = chat[targetIndex];
// 检查目标消息是否是AI消息通常填表针对AI回复
// 如果目标消息是用户的消息而我们只想填AI的表这可能是一个问题。
// 但如果用户设置了延迟他们可能期望每隔几层填一次或者只填AI层。
// 现有的 `fillWithSecondaryApi` 是在 `CHAT_COMPLETION` 后调用的此时最新消息通常是AI消息。
// 如果延迟是奇数例如1目标消息可能是用户消息。
// 假设延迟是偶数例如2目标消息是上一条AI消息。
// 为了安全起见,如果目标消息是用户消息,我们可能应该跳过?或者依然填表(记录用户消息的表)?
// 目前表系统通常绑定在AI回复上。
// 如果 targetMessage.is_user我们尝试往回找最近的一条AI消息
// 不,这会乱套。严格按照楼层索引来。
console.log(`[Amily2-副API] 延迟模式生效: 当前总楼层 ${chat.length}, 延迟 ${delay}, 目标楼层索引 ${targetIndex}`);
} else {
// 无延迟,使用传入的最新消息
targetMessage = latestMessage;
targetIndex = chat.length - 1;
}
let textToProcess = targetMessage.mes;
if (!textToProcess || !textToProcess.trim()) { if (!textToProcess || !textToProcess.trim()) {
console.log("[Amily2-副API] 消息内容为空,跳过填表任务。"); console.log("[Amily2-副API] 目标消息内容为空,跳过填表任务。");
return; return;
} }
@@ -120,15 +158,15 @@ export async function fillWithSecondaryApi(latestMessage, forceRun = false) {
return; return;
} }
const context = getContext();
const userName = context.name1 || '用户'; const userName = context.name1 || '用户';
const characterName = context.name2 || '角色'; const characterName = context.name2 || '角色';
const chat = context.chat; // 寻找目标消息之前的最后一条用户消息
let lastUserMessage = null; let lastUserMessage = null;
let lastUserMessageIndex = -1; let lastUserMessageIndex = -1;
for (let i = chat.length - 2; i >= 0; i--) {
// 从 targetIndex - 1 开始往前找
for (let i = targetIndex - 1; i >= 0; i--) {
if (chat[i].is_user) { if (chat[i].is_user) {
lastUserMessage = chat[i]; lastUserMessage = chat[i];
lastUserMessageIndex = i; lastUserMessageIndex = i;
@@ -136,8 +174,8 @@ export async function fillWithSecondaryApi(latestMessage, forceRun = false) {
} }
} }
const currentInteractionContent = (lastUserMessage ? `${userName}(用户)最新消息:${lastUserMessage.mes}\n` : '') + const currentInteractionContent = (lastUserMessage ? `${userName}(用户)消息:${lastUserMessage.mes}\n` : '') +
`${characterName}AI最新消息,[核心处理内容]${textToProcess}`; `${characterName}AI消息[核心处理内容]${textToProcess}`;
let mixedOrder; let mixedOrder;
try { try {
@@ -185,7 +223,10 @@ export async function fillWithSecondaryApi(latestMessage, forceRun = false) {
const historyMessagesToGet = contextReadingLevel > 2 ? contextReadingLevel - 2 : 0; const historyMessagesToGet = contextReadingLevel > 2 ? contextReadingLevel - 2 : 0;
if (historyMessagesToGet > 0) { if (historyMessagesToGet > 0) {
const historyEndIndex = lastUserMessageIndex !== -1 ? lastUserMessageIndex : chat.length - 1; // 这里的 historyEndIndex 应该是我们上面计算出的 lastUserMessageIndex
// 如果没找到用户消息,则使用 targetIndex - 1
const historyEndIndex = lastUserMessageIndex !== -1 ? lastUserMessageIndex : Math.max(0, targetIndex - 1);
const historyContext = await getHistoryContext(historyMessagesToGet, historyEndIndex, tagsToExtract, exclusionRules); const historyContext = await getHistoryContext(historyMessagesToGet, historyEndIndex, tagsToExtract, exclusionRules);
if (historyContext) { if (historyContext) {
messages.push({ role: "system", content: historyContext }); messages.push({ role: "system", content: historyContext });
@@ -205,12 +246,10 @@ export async function fillWithSecondaryApi(latestMessage, forceRun = false) {
} }
} }
const fillingMode = settings.filling_mode || 'main-api'; console.groupCollapsed(`[Amily2 分步填表] 即将发送至 API 的内容`);
if (fillingMode === 'secondary-api') { console.log("发送给AI的提示词: ", JSON.stringify(messages, null, 2));
console.groupCollapsed(`[Amily2 分步填表] 即将发送至 API 的内容`); console.dir(messages);
console.dir(messages); console.groupEnd();
console.groupEnd();
}
let rawContent; let rawContent;
if (settings.nccsEnabled) { if (settings.nccsEnabled) {
@@ -230,14 +269,20 @@ export async function fillWithSecondaryApi(latestMessage, forceRun = false) {
updateTableFromText(rawContent); updateTableFromText(rawContent);
const currentContext = getContext(); // 保存到目标消息
if (currentContext.chat && currentContext.chat.length > 0) { if (saveStateToMessage(getMemoryState(), targetMessage)) {
const lastMessage = currentContext.chat[currentContext.chat.length - 1]; // 如果目标消息不是最新消息,我们可能需要重新渲染整个聊天记录或者特定消息的表格?
if (saveStateToMessage(getMemoryState(), lastMessage)) { // renderTables() 通常重新渲染所有可见表格
renderTables(); renderTables();
updateOrInsertTableInChat(); // updateOrInsertTableInChat 通常插入到DOM中
} // 我们可能需要传递 targetIndex 给 updateOrInsertTableInChat 吗?
// 目前 updateOrInsertTableInChat 似乎是查找 .mes_text 并插入。
// 如果我们更新了历史消息的数据,我们需要确保 DOM 也更新。
// 由于 SillyTavern 的消息渲染机制,如果消息已经在屏幕上,仅仅修改数据可能不会自动更新 DOM。
// 但是 renderTables() 应该会处理这个。
updateOrInsertTableInChat();
} }
saveChat(); saveChat();
} catch (error) { } catch (error) {