Files
ST-Amily2-Chat-Optimisation…/core/table-system/batch-filler.js
2025-08-24 11:52:00 +08:00

2 lines
15 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const _0x31264b=_0x40c5;(function(_0x6b1ef1,_0xc0b9b9){const _0x165e84=_0x40c5,_0x3317d6=_0x6b1ef1();while(!![]){try{const _0x3adc48=parseInt(_0x165e84(0xc0))/0x1+parseInt(_0x165e84(0xd6))/0x2*(parseInt(_0x165e84(0x11f))/0x3)+-parseInt(_0x165e84(0x120))/0x4*(-parseInt(_0x165e84(0x10d))/0x5)+-parseInt(_0x165e84(0xb8))/0x6*(parseInt(_0x165e84(0xa5))/0x7)+-parseInt(_0x165e84(0xf8))/0x8*(parseInt(_0x165e84(0x107))/0x9)+-parseInt(_0x165e84(0xe1))/0xa*(parseInt(_0x165e84(0xce))/0xb)+parseInt(_0x165e84(0x119))/0xc*(parseInt(_0x165e84(0xef))/0xd);if(_0x3adc48===_0xc0b9b9)break;else _0x3317d6['push'](_0x3317d6['shift']());}catch(_0x21ad81){_0x3317d6['push'](_0x3317d6['shift']());}}}(_0x2c3b,0xa60c3));import{getContext,extension_settings}from'/scripts/extensions.js';import{log}from'./logger.js';import{updateTableFromText}from'./manager.js';import{extensionName}from'../../utils/settings.js';import{renderTables}from'../../ui/table-bindings.js';import{getPresetPrompts,getMixedOrder}from'../../PresetSettings/index.js';import{callAI,generateRandomSeed}from'../api.js';import{extractBlocksByTags,applyExclusionRules}from'../utils/rag-tag-extractor.js';import{getBatchFillerRuleTemplate,getBatchFillerFlowTemplate,convertTablesToCsvString}from'./manager.js';let isFilling=![],manualStopRequested=![],currentBatch=0x0,totalBatches=0x0,chatHistoryLength=0x0,threshold=0x1e;const MAX_RETRIES=0x2,fillButton=()=>document[_0x31264b(0x11d)]('fill-table-now-btn');function updateButtonState(_0x3b8a55,_0xcef8ed=0x0,_0x2e89dc=0x0){const _0x2be588=_0x31264b,_0x4e5ac7=fillButton();if(!_0x4e5ac7)return;switch(_0x3b8a55){case _0x2be588(0xd5):let _0x22e739=_0x2e89dc>0x0?_0x2be588(0xad)+(_0x2e89dc+0x1)+')':'';_0x4e5ac7['textContent']=_0x2be588(0x11a)+_0xcef8ed+'/'+totalBatches+')'+_0x22e739,_0x4e5ac7[_0x2be588(0x111)]=![],isFilling=!![];break;case _0x2be588(0x118):_0x4e5ac7[_0x2be588(0xb0)]=_0x2be588(0xbf),_0x4e5ac7[_0x2be588(0x111)]=!![];break;case _0x2be588(0xdd):_0x4e5ac7[_0x2be588(0xb0)]=_0x2be588(0xc2),_0x4e5ac7[_0x2be588(0x111)]=![],isFilling=!![];break;case _0x2be588(0xd9):_0x4e5ac7[_0x2be588(0xb0)]=_0x2be588(0xb7),_0x4e5ac7[_0x2be588(0x111)]=![],isFilling=!![];break;case _0x2be588(0xfb):default:_0x4e5ac7['textContent']=_0x2be588(0x100),_0x4e5ac7[_0x2be588(0x111)]=![],isFilling=![],currentBatch=0x0,manualStopRequested=![];break;}}function _0x40c5(_0x2cf916,_0x5f4a27){const _0x2c3b7f=_0x2c3b();return _0x40c5=function(_0x40c58e,_0x93ad23){_0x40c58e=_0x40c58e-0xa5;let _0x133393=_0x2c3b7f[_0x40c58e];return _0x133393;},_0x40c5(_0x2cf916,_0x5f4a27);}async function callTableModel(_0xf907ec){const _0x557bee=_0x31264b;try{const _0x19c783=await callAI(_0xf907ec);if(!_0x19c783)throw new Error('API返回内容为空。');return _0x19c783;}catch(_0x39f248){return log(_0x557bee(0xac)+_0x39f248[_0x557bee(0xe5)],_0x557bee(0xd9)),toastr['error']('与模型通讯时发生异常:\x20'+_0x39f248['message'],_0x557bee(0xf7)),null;}}function getRawMessagesForSummary(_0x58a294,_0x34c849){const _0x18f914=_0x31264b,_0x1178cf=getContext(),_0x30f2bd=_0x1178cf[_0x18f914(0xc4)],_0x1ae226=extension_settings[extensionName],_0x4e1107=_0x30f2bd[_0x18f914(0x117)](_0x58a294-0x1,_0x34c849);if(_0x4e1107[_0x18f914(0xe8)]===0x0)return null;const _0x318ec2=_0x1178cf[_0x18f914(0xaa)]||'用户',_0x921b93=_0x1178cf[_0x18f914(0xe6)]||'角色',_0x223f4b=_0x1ae226[_0x18f914(0xd1)]??![],_0x2f2622=_0x223f4b?(_0x1ae226[_0x18f914(0xf4)]||'')['split'](',')['map'](_0x137b9=>_0x137b9[_0x18f914(0x104)]())[_0x18f914(0xc1)](Boolean):[],_0x1c418a=_0x1ae226[_0x18f914(0xab)]||[],_0x209c64=_0x4e1107[_0x18f914(0x110)]((_0x120895,_0x55c288)=>{const _0x12bdb5=_0x18f914;let _0x506231=_0x120895[_0x12bdb5(0xca)];if(_0x223f4b&&_0x2f2622[_0x12bdb5(0xe8)]>0x0){const _0x1d499c=extractBlocksByTags(_0x506231,_0x2f2622);_0x1d499c[_0x12bdb5(0xe8)]>0x0&&(_0x506231=_0x1d499c[_0x12bdb5(0x103)]('\x0a\x0a'));}_0x506231=applyExclusionRules(_0x506231,_0x1c418a);if(!_0x506231['trim']())return null;return{'floor':_0x58a294+_0x55c288,'author':_0x120895[_0x12bdb5(0xa7)]?_0x318ec2:_0x921b93,'authorType':_0x120895['is_user']?_0x12bdb5(0xe3):_0x12bdb5(0xbd),'content':_0x506231[_0x12bdb5(0x104)]()};})[_0x18f914(0xc1)](Boolean);return _0x209c64;}async function runBatchAttempt(_0x4590af,_0x5533a5){const _0x28b2ab=_0x31264b;try{if(manualStopRequested){log(_0x28b2ab(0xf6)+_0x4590af+'\x20开始前手动暂停。','warn'),updateButtonState(_0x28b2ab(0xdd));return;}updateButtonState(_0x28b2ab(0xd5),_0x4590af,_0x5533a5);const _0x37086a=(_0x4590af-0x1)*threshold+0x1,_0x5843c4=Math[_0x28b2ab(0xd0)](_0x37086a+threshold-0x1,chatHistoryLength);log(_0x28b2ab(0xeb)+_0x4590af+'/'+totalBatches+_0x28b2ab(0xb5)+_0x37086a+'-'+_0x5843c4+_0x28b2ab(0xb4)+(_0x5533a5+0x1)+'/'+(MAX_RETRIES+0x1)+')',_0x28b2ab(0xd8));const _0x49b119=getRawMessagesForSummary(_0x37086a,_0x5843c4);if(!_0x49b119||_0x49b119[_0x28b2ab(0xe8)]===0x0)throw new Error(_0x28b2ab(0xb9));const _0x350276=_0x49b119['map'](_0x119459=>'【第\x20'+_0x119459['floor']+_0x28b2ab(0xfd)+_0x119459['author']+':\x20'+_0x119459[_0x28b2ab(0xf0)])[_0x28b2ab(0x103)]('\x0a'),_0x6a7cdf=getBatchFillerRuleTemplate(),_0x2cbc77=getBatchFillerFlowTemplate(),_0x2eb646=convertTablesToCsvString(),_0x2dddf8=_0x2cbc77[_0x28b2ab(0xe4)](_0x28b2ab(0x108),_0x2eb646);let _0x9d10b;try{const _0x460b55=localStorage[_0x28b2ab(0xc6)](_0x28b2ab(0x10f));_0x460b55&&(_0x9d10b=JSON['parse'](_0x460b55));}catch(_0x235e46){console[_0x28b2ab(0xd9)](_0x28b2ab(0xbc),_0x235e46);}const _0x456ed4=getMixedOrder('batch_filler')||[],_0xce554b=getPresetPrompts(_0x28b2ab(0xfa)),_0x21a01b=[{'role':'system','content':generateRandomSeed()}];for(const _0x39781d of _0x456ed4){if(_0x39781d[_0x28b2ab(0xf3)]===_0x28b2ab(0xee))_0xce554b&&_0xce554b[_0x39781d[_0x28b2ab(0xea)]]&&_0x21a01b['push'](_0xce554b[_0x39781d[_0x28b2ab(0xea)]]);else{if(_0x39781d[_0x28b2ab(0xf3)]===_0x28b2ab(0xde))switch(_0x39781d['id']){case _0x28b2ab(0xbe):_0x21a01b[_0x28b2ab(0xfc)]({'role':_0x28b2ab(0x112),'content':_0x6a7cdf});break;case _0x28b2ab(0xcf):_0x21a01b[_0x28b2ab(0xfc)]({'role':_0x28b2ab(0x112),'content':_0x2dddf8});break;case'coreContent':_0x21a01b[_0x28b2ab(0xfc)]({'role':_0x28b2ab(0xe3),'content':_0x28b2ab(0xba)+_0x350276+_0x28b2ab(0xc5)});break;}}}if(!_0xce554b||_0xce554b['length']===0x0){const _0x3c7372=[{'role':'system','content':generateRandomSeed()}];_0x21a01b[_0x28b2ab(0xb1)](0x1,0x0,..._0x3c7372);}console[_0x28b2ab(0xd3)](_0x28b2ab(0x106)+_0x4590af+'/'+totalBatches+'\x20-\x20即将发送至\x20API\x20的内容'),console[_0x28b2ab(0x10e)](_0x21a01b),console[_0x28b2ab(0xbb)]();const _0x3c0a64=await callTableModel(_0x21a01b);console[_0x28b2ab(0xf1)](_0x28b2ab(0x106)+_0x4590af+'/'+totalBatches+'\x20-\x20收到\x20API\x20原始回复:',_0x3c0a64);if(!_0x3c0a64)throw new Error(_0x28b2ab(0xda));updateTableFromText(_0x3c0a64),renderTables(),log(_0x28b2ab(0xe7)+_0x4590af+'\x20处理成功。',_0x28b2ab(0x105)),currentBatch=_0x4590af,setTimeout(processNextBatch,0x3e8);}catch(_0x4367ee){log('批次\x20'+_0x4590af+_0x28b2ab(0xdf)+(_0x5533a5+0x1)+_0x28b2ab(0x114)+_0x4367ee[_0x28b2ab(0xe5)],'error'),_0x5533a5>=MAX_RETRIES?(log('批次\x20'+_0x4590af+_0x28b2ab(0xb6),_0x28b2ab(0xd9)),toastr[_0x28b2ab(0xd9)](_0x28b2ab(0xe7)+_0x4590af+'\x20多次失败请检查网络或API设置后手动继续。',_0x28b2ab(0xd2)),currentBatch=_0x4590af-0x1,updateButtonState(_0x28b2ab(0xd9))):(log(_0x28b2ab(0x116)+_0x4590af+_0x28b2ab(0x102),_0x28b2ab(0x10b)),setTimeout(()=>runBatchAttempt(_0x4590af,_0x5533a5+0x1),0xbb8));}}async function processNextBatch(){const _0x21ccc2=_0x31264b;if(manualStopRequested){log(_0x21ccc2(0xf6)+(currentBatch+0x1)+_0x21ccc2(0xe0),'warn'),updateButtonState(_0x21ccc2(0xdd));return;}if(currentBatch>=totalBatches){log(_0x21ccc2(0xd4),_0x21ccc2(0x105)),updateButtonState('idle');return;}runBatchAttempt(currentBatch+0x1,0x0);}function _0x2c3b(){const _0x46cb84=['\x20的内容...','index','正在处理批次\x20','楼层\x20','\x20-\x20收到\x20API\x20原始回复:','prompt','5577611nEToDV','content','log','\x20填表失败:\x20','type','historiographyTags','[楼层填表]\x20加载混合顺序失败:','任务已在批次\x20','通讯异常','1500016wEgCkF','warning','batch_filler','idle','push','\x20楼】\x20','当前没有聊天记录。','表格系统总开关已关闭,无法执行楼层填表。','立即填表','\x20填表完成','...','join','trim','success','[Amily2\x20立即远征]\x20批次\x20','45APFNSG','{{{Amily2TableData}}}','楼层填表失败:\x20','从上次暂停处继续处理...','warn','\x20超出了当前聊天记录长度\x20','23930skkfIF','dir','amily2_prompt_presets_v2_mixed_order','map','disabled','system','指定楼层范围内没有有效内容可处理。','\x20失败:\x20','聊天记录为空,无需填表。','将在3秒后自动重试批次\x20','slice','stopping','24iOjiOH','点击停止\x20(','startsWith','点击停止','getElementById','ceil','18723cYNGMg','132RZnUGV','\x20-\x20即将发送至\x20API\x20的内容','表格系统总开关已关闭,跳过楼层填表。','7sOStHJ','开始处理楼层\x20','is_user','batch-filling-threshold','表格系统总开关已关闭,无法执行批量填表。','name1','historiographyExclusionRules','与模型通讯时发生异常:\x20','\x20(尝试\x20','coreContent','无法开始','textContent','splice','parse','\x20个批次。',',\x20尝试\x20','\x20(楼层\x20','\x20已达到最大重试次数任务暂停。','继续填表\x20(出错)','7532562SGotXe','净化后无有效内容可处理。','请严格根据以下\x22对话记录\x22中的内容进行填写表格并按照指定的格式输出不要添加任何额外信息。\x0a\x0a<对话记录>\x0a','groupEnd','[批量填表]\x20加载混合顺序失败:','char','ruleTemplate','正在停止...','1249238vyhxMb','filter','继续填表','处理失败','chat','\x0a</对话记录>','getItem','[Amily2\x20楼层填表]\x20楼层\x20','请确保\x22规则提示词\x22和\x22流程提示词\x22都已填写。','value','mes','准备开始批量填表任务,共\x20','停战敕令已下达!将在当前批次完成后暂停。','floor','55KLbqBS','flowTemplate','min','historiographyTagExtractionEnabled','任务暂停','groupCollapsed','所有批次处理完毕!','processing','366MlPdnF','【第\x20','info','error','API返回内容为空。','table_system_enabled','规则或流程提示词为空,无法开始填表。','paused','conditional','\x20尝试\x20','\x20开始前手动暂停。','1068610yIBpOF','author','user','replace','message','name2','批次\x20','length'];_0x2c3b=function(){return _0x46cb84;};return _0x2c3b();}export function startBatchFilling(){const _0x4a9150=_0x31264b,_0x13e102=fillButton();if(!_0x13e102)return;const _0x2e9508=extension_settings[extensionName],_0x9dc759=_0x2e9508[_0x4a9150(0xdb)]!==![];if(!_0x9dc759){log('表格系统总开关已关闭,跳过批量填表。','info'),toastr[_0x4a9150(0xd8)](_0x4a9150(0xa9));return;}if(isFilling){if(_0x13e102[_0x4a9150(0xb0)][_0x4a9150(0x11b)](_0x4a9150(0x11c)))manualStopRequested=!![],updateButtonState('stopping'),log(_0x4a9150(0xcc),'warn');else _0x13e102[_0x4a9150(0xb0)]['startsWith'](_0x4a9150(0xc2))&&(manualStopRequested=![],log(_0x4a9150(0x10a),_0x4a9150(0xd8)),processNextBatch());return;}manualStopRequested=![];const _0x17095c=getContext();chatHistoryLength=_0x17095c['chat'][_0x4a9150(0xe8)],threshold=parseInt(document[_0x4a9150(0x11d)](_0x4a9150(0xa8))?.[_0x4a9150(0xc9)],0xa)||0x1e;const _0x467513=getBatchFillerRuleTemplate(),_0x4edbd1=getBatchFillerFlowTemplate();if(!_0x467513||!_0x4edbd1){log(_0x4a9150(0xdc),_0x4a9150(0xd9)),toastr[_0x4a9150(0xd9)]('请确保\x22规则提示词\x22和\x22流程提示词\x22都已填写。','无法开始');return;}if(chatHistoryLength===0x0){log(_0x4a9150(0x115),_0x4a9150(0xd8));return;}totalBatches=Math[_0x4a9150(0x11e)](chatHistoryLength/threshold),currentBatch=0x0,log(_0x4a9150(0xcb)+totalBatches+_0x4a9150(0xb3),_0x4a9150(0xd8)),processNextBatch();}export async function startFloorRangeFilling(_0x4362de,_0x15af53){const _0x40ba4d=_0x31264b,_0x2078ae=extension_settings[extensionName],_0x31c688=_0x2078ae[_0x40ba4d(0xdb)]!==![];if(!_0x31c688){log(_0x40ba4d(0x122),'info'),toastr['info'](_0x40ba4d(0xff));return;}const _0x34c847=getContext(),_0x5e790f=_0x34c847['chat'][_0x40ba4d(0xe8)];if(_0x15af53>_0x5e790f){toastr[_0x40ba4d(0xf9)]('结束楼层\x20'+_0x15af53+_0x40ba4d(0x10c)+_0x5e790f+'。');return;}const _0xfaffad=getBatchFillerRuleTemplate(),_0x43e0c9=getBatchFillerFlowTemplate();if(!_0xfaffad||!_0x43e0c9){log('规则或流程提示词为空,无法开始楼层填表。',_0x40ba4d(0xd9)),toastr['error'](_0x40ba4d(0xc8),_0x40ba4d(0xaf));return;}try{log(_0x40ba4d(0xa6)+_0x4362de+'-'+_0x15af53+_0x40ba4d(0xe9),_0x40ba4d(0xd8));const _0x578595=getRawMessagesForSummary(_0x4362de,_0x15af53);if(!_0x578595||_0x578595[_0x40ba4d(0xe8)]===0x0){toastr[_0x40ba4d(0xf9)](_0x40ba4d(0x113));return;}const _0x326c5b=_0x578595[_0x40ba4d(0x110)](_0x28e4af=>_0x40ba4d(0xd7)+_0x28e4af[_0x40ba4d(0xcd)]+_0x40ba4d(0xfd)+_0x28e4af[_0x40ba4d(0xe2)]+':\x20'+_0x28e4af['content'])[_0x40ba4d(0x103)]('\x0a'),_0x50437c=convertTablesToCsvString(),_0x312ae6=_0x43e0c9[_0x40ba4d(0xe4)](_0x40ba4d(0x108),_0x50437c);let _0x23dfa4;try{const _0x3a773f=localStorage[_0x40ba4d(0xc6)](_0x40ba4d(0x10f));_0x3a773f&&(_0x23dfa4=JSON[_0x40ba4d(0xb2)](_0x3a773f));}catch(_0xf9c733){console[_0x40ba4d(0xd9)](_0x40ba4d(0xf5),_0xf9c733);}const _0x138d33=getMixedOrder(_0x40ba4d(0xfa))||[],_0x49248c=getPresetPrompts(_0x40ba4d(0xfa)),_0x4e9046=[{'role':_0x40ba4d(0x112),'content':generateRandomSeed()}];for(const _0x4114be of _0x138d33){if(_0x4114be[_0x40ba4d(0xf3)]==='prompt')_0x49248c&&_0x49248c[_0x4114be[_0x40ba4d(0xea)]]&&_0x4e9046[_0x40ba4d(0xfc)](_0x49248c[_0x4114be[_0x40ba4d(0xea)]]);else{if(_0x4114be[_0x40ba4d(0xf3)]===_0x40ba4d(0xde))switch(_0x4114be['id']){case _0x40ba4d(0xbe):_0x4e9046['push']({'role':_0x40ba4d(0x112),'content':_0xfaffad});break;case _0x40ba4d(0xcf):_0x4e9046[_0x40ba4d(0xfc)]({'role':_0x40ba4d(0x112),'content':_0x312ae6});break;case _0x40ba4d(0xae):_0x4e9046[_0x40ba4d(0xfc)]({'role':'user','content':_0x40ba4d(0xba)+_0x326c5b+'\x0a</对话记录>'});break;}}}if(!_0x49248c||_0x49248c['length']===0x0){const _0x215333=[{'role':'system','content':generateRandomSeed()}];_0x4e9046[_0x40ba4d(0xb1)](0x1,0x0,..._0x215333);}console[_0x40ba4d(0xd3)](_0x40ba4d(0xc7)+_0x4362de+'-'+_0x15af53+_0x40ba4d(0x121)),console[_0x40ba4d(0x10e)](_0x4e9046),console[_0x40ba4d(0xbb)]();const _0x552106=await callTableModel(_0x4e9046);console[_0x40ba4d(0xf1)]('[Amily2\x20楼层填表]\x20楼层\x20'+_0x4362de+'-'+_0x15af53+_0x40ba4d(0xed),_0x552106);if(!_0x552106)throw new Error(_0x40ba4d(0xda));updateTableFromText(_0x552106),renderTables(),toastr[_0x40ba4d(0x105)](_0x40ba4d(0xec)+_0x4362de+'-'+_0x15af53+_0x40ba4d(0x101)),log(_0x40ba4d(0xec)+_0x4362de+'-'+_0x15af53+'\x20填表处理完成。',_0x40ba4d(0x105));}catch(_0x2e7e0b){log(_0x40ba4d(0xec)+_0x4362de+'-'+_0x15af53+_0x40ba4d(0xf2)+_0x2e7e0b[_0x40ba4d(0xe5)],_0x40ba4d(0xd9)),toastr[_0x40ba4d(0xd9)](_0x40ba4d(0x109)+_0x2e7e0b[_0x40ba4d(0xe5)],_0x40ba4d(0xc3));}}export async function startCurrentFloorFilling(){const _0x20e15c=_0x31264b,_0x5b29e0=getContext(),_0x58a639=_0x5b29e0[_0x20e15c(0xc4)]['length'];if(_0x58a639===0x0){toastr[_0x20e15c(0xd8)](_0x20e15c(0xfe));return;}log('准备填写当前楼层(第\x20'+_0x58a639+'\x20楼...',_0x20e15c(0xd8)),await startFloorRangeFilling(_0x58a639,_0x58a639);}