Files
ST-Amily2-Chat-Optimisation/core/summarizer.js
2025-08-25 14:54:54 +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.
(function(_0x31e857,_0xfaa44d){const _0x4e3d60=_0x419e,_0x2c5f8a=_0x31e857();while(!![]){try{const _0x6c9d58=-parseInt(_0x4e3d60(0xf4))/0x1+-parseInt(_0x4e3d60(0xd5))/0x2*(parseInt(_0x4e3d60(0x9f))/0x3)+-parseInt(_0x4e3d60(0xe3))/0x4*(-parseInt(_0x4e3d60(0x93))/0x5)+-parseInt(_0x4e3d60(0xa6))/0x6*(-parseInt(_0x4e3d60(0xb6))/0x7)+parseInt(_0x4e3d60(0xeb))/0x8+parseInt(_0x4e3d60(0xf1))/0x9*(-parseInt(_0x4e3d60(0xa0))/0xa)+parseInt(_0x4e3d60(0xda))/0xb*(parseInt(_0x4e3d60(0xec))/0xc);if(_0x6c9d58===_0xfaa44d)break;else _0x2c5f8a['push'](_0x2c5f8a['shift']());}catch(_0x57a401){_0x2c5f8a['push'](_0x2c5f8a['shift']());}}}(_0x5693,0x37c19));import{extension_settings,getContext}from'/scripts/extensions.js';import{characters}from'/script.js';import{world_info}from'/scripts/world-info.js';import{extensionName}from'../utils/settings.js';import{extractContentByTag,replaceContentByTag,extractFullTagBlock}from'../utils/tagProcessor.js';import{isGoogleEndpoint,convertToGoogleRequest,parseGoogleResponse,buildGoogleApiUrl,buildPlotOptimizationGoogleRequest,parsePlotOptimizationGoogleResponse}from'./utils/googleAdapter.js';import{applyExclusionRules}from'./utils/rag-tag-extractor.js';function _0x419e(_0x912c1a,_0x8e1609){const _0x56936b=_0x5693();return _0x419e=function(_0x419ee2,_0x472742){_0x419ee2=_0x419ee2-0x91;let _0x4241bd=_0x56936b[_0x419ee2];return _0x4241bd;},_0x419e(_0x912c1a,_0x8e1609);}import{getCombinedWorldbookContent,getPlotOptimizedWorldbookContent}from'./lore.js';import{getBatchFillerFlowTemplate,convertTablesToCsvString,updateTableFromText,saveStateToMessage,getMemoryState}from'./table-system/manager.js';import{saveChat}from'/script.js';import{renderTables}from'../ui/table-bindings.js';import{getPresetPrompts,getMixedOrder}from'../PresetSettings/index.js';function _0x5693(){const _0x378575=['optimized','59845PYcHnD','error','replace','<世界书内容>\x0a','trim','plot','world','mainPrompt','plot_optimization','slice','[Amily2号-原始回复]','[Amily2-外交部]\x20未能获取AI响应内容','9cfJiyx','55800YWzujc','main-api','optimizationExclusionRules','请你在优化完成后,在正文标签外结合最新消息中的剧情、当前的表格内容进行填表任务:\x0a\x0a','preOptimizationTextUpdated','dispatchEvent','1890evAPjX','-->','[上下文参考]:\x0a','\x0a</前文内容>','timeEnd','toLocaleTimeString','name2','AMILY2_SYSTEM_PARALYZED','chat',']\x20发送给AI的最终请求内容','filter','\x5c$&','is_user','<Additional\x20instructionsv>Start\x20and\x20end\x20labels\x20correctly.<Additional\x20instructions>','AI响应为空或格式不正确。','message','3955CpCmtM','Amily2号任务失败:\x20','join','plotOpt_tableEnabled','[Amily2-内容排除]\x20正文优化内容排除规则已生效文本长度从\x20','\x20变为\x20','优化任务总耗时','[Amily2号-最终国书内容\x20(发往AI)]','log','[Amily2-外交部]\x20目标标签\x20<','<!--','plotOpt_rateMain','fillingMode','plotOpt_rateCuckold','length','conditional','groupEnd','[Amily2-外交部]\x20发生严重错误:','data','\x0a\x0a<Amily2Edit>\x0a<!--\x0a这里是你的填表内容\x0a-->\x0a</Amily2Edit><Additional\x20instructionsv>Optimisation\x20and\x20form\x20filling\x20have\x20been\x20completed.<Additional\x20instructions>','worldbook','extensions','{{{Amily2TableData}}}','##以下内容是故事发生的剧情中提取出的内容,已经转化为表格形式呈现给你,请将以下内容作为后续剧情的一部分参考:<表格内容>\x0a{{{Amily2TableDataContent}}}</表格内容>','optimizationExclusionEnabled','contextLimit','plotTag','[目标内容]:\x0a','push','optimizationEnabled','name1','158820hqrZzb','plotOpt_ratePersonal','[世界书档案]:\x0a','groupCollapsed','dir','2024ajgvYp','剧情优化任务失败:\x20','user','AI最新消息[核心处理内容]','plotOpt_enabled','lastPreOptimizationText','characterId','>,将保留原始消息。','coreContent','144srHkfU',']\x20用户输入为空跳过优化。','>\x20未找到或为空或内容已被完全排除优化任务已跳过。',']\x20剧情优化任务发生严重错误:','[Amily2-制裁]\x20系统完整性已受损所有外交活动被无限期中止。','systemPrompt','plotOpt_rateErotic','剧情优化任务总耗时','550928MsaLys','18168vxjrBm','<plot>','map','prompt','time','648BtLcYm','(用户)最新消息:','plotOpt_systemPrompt','87934aRqTng','plotOpt_mainPrompt','type','optimization','</世界书内容>','mes','filling_mode','worldbookEnabled','plotOpt_contextLimit','{{{Amily2TableDataContent}}}',']\x20从AI收到的原始回复','system'];_0x5693=function(){return _0x378575;};return _0x5693();}import{callAI,generateRandomSeed}from'./api.js';export async function processOptimization(_0x1d02cd,_0x192388){const _0x266d16=_0x419e;if(window[_0x266d16(0xad)]===!![])return console[_0x266d16(0x94)](_0x266d16(0xe7)),null;const _0x434c2e=extension_settings[extensionName],_0x4ed1d8=_0x434c2e[_0x266d16(0xd3)];if(!_0x4ed1d8)return null;console[_0x266d16(0xd8)]('[Amily2号-正文优化任务]\x20'+new Date()[_0x266d16(0xab)]()),console[_0x266d16(0xf0)]('优化任务总耗时');try{const _0x32c935=_0x1d02cd[_0x266d16(0xf9)];let _0x3cb828=_0x32c935;if(_0x434c2e[_0x266d16(0xce)]&&_0x434c2e[_0x266d16(0xa2)]?.['length']>0x0){const _0xb36678=_0x3cb828[_0x266d16(0xc4)];_0x3cb828=applyExclusionRules(_0x3cb828,_0x434c2e[_0x266d16(0xa2)]);const _0x177592=_0x3cb828[_0x266d16(0xc4)];_0xb36678!==_0x177592&&console[_0x266d16(0xbe)](_0x266d16(0xba)+_0xb36678+_0x266d16(0xbb)+_0x177592+'。');}const _0x1197e2=_0x434c2e['optimizationTargetTag']||'content',_0x113432=extractFullTagBlock(_0x3cb828,_0x1197e2);if(!_0x113432||extractContentByTag(_0x113432,_0x1197e2)?.[_0x266d16(0x97)]()==='')return console[_0x266d16(0xbe)](_0x266d16(0xbf)+_0x1197e2+_0x266d16(0xe5)),console[_0x266d16(0xaa)](_0x266d16(0xbc)),console['groupEnd'](),null;_0x3cb828=_0x113432;const _0x26717c=getContext(),_0x3e073c=_0x26717c[_0x266d16(0xd4)]||'用户',_0x1e8b5b=_0x26717c[_0x266d16(0xac)]||'角色',_0x3ebeb2=_0x192388[_0x266d16(0xc4)]>0x0&&_0x192388[_0x192388[_0x266d16(0xc4)]-0x1][_0x266d16(0xb2)]?_0x192388[_0x192388[_0x266d16(0xc4)]-0x1]:null,_0x50c15b=_0x3ebeb2?_0x192388['slice'](0x0,-0x1):_0x192388,_0x8289e9=_0x50c15b['map'](_0xe3acc6=>_0xe3acc6[_0x266d16(0xf9)]&&_0xe3acc6[_0x266d16(0xf9)]['trim']()?(_0xe3acc6[_0x266d16(0xb2)]?_0x3e073c:_0x1e8b5b)+':\x20'+_0xe3acc6[_0x266d16(0xf9)][_0x266d16(0x97)]():null)['filter'](Boolean)['join']('\x0a');let _0x21a43d='';if(_0x434c2e[_0x266d16(0xfb)]){const _0x4fe8cc=characters[_0x26717c[_0x266d16(0xe0)]];_0x4fe8cc?.[_0x266d16(0xc8)]?.[_0x266d16(0xcb)]?.['world']&&(_0x21a43d=await getCombinedWorldbookContent(_0x4fe8cc['data'][_0x266d16(0xcb)][_0x266d16(0x99)]));}const _0xef0435=getPresetPrompts(_0x266d16(0xf7)),_0x182a8b=[{'role':_0x266d16(0x91),'content':generateRandomSeed()}];let _0x14aef5=_0x3ebeb2?_0x3e073c+_0x266d16(0xf2)+_0x3ebeb2[_0x266d16(0xf9)]+'\x0a'+_0x1e8b5b+_0x266d16(0xdd)+_0x3cb828:_0x1e8b5b+_0x266d16(0xdd)+_0x3cb828;const _0x15887a=_0x434c2e[_0x266d16(0xfa)]||_0x266d16(0xa1);window[_0x266d16(0xdf)]=_0x14aef5,document[_0x266d16(0xa5)](new CustomEvent(_0x266d16(0xa4)));const _0x3a0386=getMixedOrder('optimization')||[];let _0x1207b0=0x0;for(const _0x5340a3 of _0x3a0386){if(_0x5340a3[_0x266d16(0xf6)]==='prompt')_0xef0435&&_0xef0435[_0x1207b0]&&(_0x182a8b[_0x266d16(0xd2)](_0xef0435[_0x1207b0]),_0x1207b0++);else{if(_0x5340a3[_0x266d16(0xf6)]===_0x266d16(0xc5))switch(_0x5340a3['id']){case _0x266d16(0x9a):_0x434c2e[_0x266d16(0x9a)]?.[_0x266d16(0x97)]()&&_0x182a8b[_0x266d16(0xd2)]({'role':_0x266d16(0x91),'content':_0x434c2e['mainPrompt'][_0x266d16(0x97)]()});break;case _0x266d16(0xe8):_0x434c2e[_0x266d16(0xe8)]?.[_0x266d16(0x97)]()&&_0x182a8b[_0x266d16(0xd2)]({'role':_0x266d16(0x91),'content':_0x434c2e['systemPrompt'][_0x266d16(0x97)]()});break;case _0x266d16(0xca):_0x21a43d&&_0x182a8b[_0x266d16(0xd2)]({'role':_0x266d16(0xdc),'content':_0x266d16(0xd7)+_0x21a43d});break;case'history':_0x8289e9&&_0x182a8b['push']({'role':_0x266d16(0xdc),'content':_0x266d16(0xa8)+_0x8289e9});break;case _0x266d16(0xc2):if(_0x4ed1d8&&_0x15887a===_0x266d16(0x92)){const _0x34a705=getBatchFillerFlowTemplate(),_0x386df5=convertTablesToCsvString(),_0x5be1b4=_0x34a705[_0x266d16(0x95)](_0x266d16(0xcc),_0x386df5);_0x182a8b[_0x266d16(0xd2)]({'role':'user','content':_0x14aef5}),_0x182a8b['push']({'role':_0x266d16(0x91),'content':_0x266d16(0xa3)+_0x5be1b4+_0x266d16(0xc9)});}else _0x182a8b[_0x266d16(0xd2)]({'role':_0x266d16(0xdc),'content':_0x266d16(0xd1)+_0x14aef5+_0x266d16(0xb3)});break;}}}window[_0x266d16(0xdf)]=_0x14aef5,document[_0x266d16(0xa5)](new CustomEvent(_0x266d16(0xa4))),console['groupCollapsed'](_0x266d16(0xbd)),console[_0x266d16(0xd9)](_0x182a8b),console[_0x266d16(0xc6)]();const _0x4611db=await callAI(_0x182a8b);if(!_0x4611db)return console[_0x266d16(0x94)](_0x266d16(0x9e)),null;console[_0x266d16(0xd8)](_0x266d16(0x9d)),console[_0x266d16(0xbe)](_0x4611db),console[_0x266d16(0xc6)]();let _0x2897df=_0x32c935;const _0x2bf1d5=extractContentByTag(_0x4611db,_0x1197e2);_0x2bf1d5?.[_0x266d16(0x97)]()?_0x2897df=replaceContentByTag(_0x32c935,_0x1197e2,_0x2bf1d5):console['warn']('[Amily2-外交部]\x20AI的回复中未找到有效的目标标签\x20<'+_0x1197e2+_0x266d16(0xe1));if(_0x4ed1d8&&_0x15887a===_0x266d16(0x92)){await updateTableFromText(_0x4611db);const _0x2053d4=getContext();if(_0x2053d4['chat']&&_0x2053d4[_0x266d16(0xae)][_0x266d16(0xc4)]>0x0){const _0x3b4dd1=_0x2053d4[_0x266d16(0xae)][_0x2053d4[_0x266d16(0xae)][_0x266d16(0xc4)]-0x1];saveStateToMessage(getMemoryState(),_0x3b4dd1)&&(await saveChat(),renderTables(),console[_0x266d16(0xbe)]('[Amily2-优化中填表]\x20流程已全部完成并已强制保存和刷新UI。'));}}const _0x430beb={'optimizedContent':_0x2897df};return console['timeEnd'](_0x266d16(0xbc)),console[_0x266d16(0xc6)](),_0x430beb;}catch(_0x54adfd){return console[_0x266d16(0x94)](_0x266d16(0xc7),_0x54adfd),toastr[_0x266d16(0x94)](_0x266d16(0xb7)+_0x54adfd['message'],'严重错误'),console[_0x266d16(0xaa)](_0x266d16(0xbc)),console[_0x266d16(0xc6)](),null;}}export async function processPlotOptimization(_0x150966,_0x4cfd87){const _0x1b0e8f=_0x419e,_0x2368c6=extension_settings[extensionName];if(_0x2368c6[_0x1b0e8f(0xde)]===![])return null;console[_0x1b0e8f(0xd8)]('['+extensionName+']\x20剧情优化任务启动...\x20'+new Date()[_0x1b0e8f(0xab)]()),console['time'](_0x1b0e8f(0xea));try{const _0x265d2f=_0x150966[_0x1b0e8f(0xf9)];if(!_0x265d2f||_0x265d2f[_0x1b0e8f(0x97)]()==='')return console['log']('['+extensionName+_0x1b0e8f(0xe4)),null;const _0x49d241=getContext(),_0x2edbb8=_0x49d241[_0x1b0e8f(0xd4)]||'用户',_0x5d07e3=_0x49d241[_0x1b0e8f(0xac)]||'角色',_0x3b6d33=getPresetPrompts(_0x1b0e8f(0x9b)),_0x3506b9=[{'role':_0x1b0e8f(0x91),'content':generateRandomSeed()}],_0x2833aa={'sulv1':_0x2368c6['plotOpt_rateMain']??0x1,'sulv2':_0x2368c6[_0x1b0e8f(0xd6)]??0x1,'sulv3':_0x2368c6[_0x1b0e8f(0xe9)]??0x1,'sulv4':_0x2368c6[_0x1b0e8f(0xc3)]??0x1};let _0xafa2d7=_0x2368c6[_0x1b0e8f(0xf5)]||'',_0x3aad51=_0x2368c6[_0x1b0e8f(0xf3)]||'';for(const _0x997c51 in _0x2833aa){const _0x2f2586=_0x2833aa[_0x997c51],_0x5b95e8=new RegExp(_0x997c51['replace'](/[-\/\\^$*+?.()|[\]{}]/g,_0x1b0e8f(0xb1)),'g');_0xafa2d7=_0xafa2d7['replace'](_0x5b95e8,_0x2f2586),_0x3aad51=_0x3aad51['replace'](_0x5b95e8,_0x2f2586);}const _0x37ee1f=await getPlotOptimizedWorldbookContent(_0x49d241,_0x2368c6);let _0x3bf0a1='';if(_0x2368c6[_0x1b0e8f(0xb9)])try{const {convertTablesToCsvStringForContentOnly:_0x3b95b6}=await import('./table-system/manager.js'),_0x29b5d4=_0x1b0e8f(0xcd),_0x335559=_0x3b95b6();_0x335559[_0x1b0e8f(0x97)]()&&(_0x3bf0a1=_0x29b5d4['replace'](_0x1b0e8f(0xfd),_0x335559));}catch(_0x3e8e84){console[_0x1b0e8f(0x94)]('[Amily2-表格系统]\x20注入表格内容时出错:',_0x3e8e84);}let _0x128154='';const _0x5402a3=_0x2368c6[_0x1b0e8f(0xfc)]||0x0;if(_0x5402a3>0x0&&_0x4cfd87[_0x1b0e8f(0xc4)]>0x0){const _0x1096c1=_0x4cfd87[_0x1b0e8f(0x9c)](-_0x5402a3);_0x128154=_0x1096c1[_0x1b0e8f(0xee)](_0x4a34ee=>{const _0x3cef6d=_0x1b0e8f;if(_0x4a34ee['mes']&&_0x4a34ee[_0x3cef6d(0xf9)][_0x3cef6d(0x97)]()){const _0x2bc50d=[{'start':_0x3cef6d(0xc0),'end':_0x3cef6d(0xa7)}],_0x15c1ea=applyExclusionRules(_0x4a34ee[_0x3cef6d(0xf9)][_0x3cef6d(0x97)](),_0x2bc50d);return _0x15c1ea?(_0x4a34ee['is_user']?_0x2edbb8:_0x5d07e3)+':\x20'+_0x15c1ea:null;}return null;})[_0x1b0e8f(0xb0)](Boolean)[_0x1b0e8f(0xb8)]('\x0a');}const _0x4b6bb8=getMixedOrder(_0x1b0e8f(0x9b))||[];let _0x2ef47e=0x0;for(const _0x4bdb39 of _0x4b6bb8){if(_0x4bdb39['type']===_0x1b0e8f(0xef))_0x3b6d33&&_0x3b6d33[_0x2ef47e]&&(_0x3506b9[_0x1b0e8f(0xd2)](_0x3b6d33[_0x2ef47e]),_0x2ef47e++);else{if(_0x4bdb39['type']===_0x1b0e8f(0xc5))switch(_0x4bdb39['id']){case _0x1b0e8f(0x9a):_0xafa2d7['trim']()&&_0x3506b9['push']({'role':_0x1b0e8f(0x91),'content':_0xafa2d7[_0x1b0e8f(0x97)]()});break;case'systemPrompt':_0x3aad51[_0x1b0e8f(0x97)]()&&_0x3506b9[_0x1b0e8f(0xd2)]({'role':'system','content':_0x3aad51[_0x1b0e8f(0x97)]()});break;case _0x1b0e8f(0xca):_0x37ee1f[_0x1b0e8f(0x97)]()&&_0x3506b9['push']({'role':_0x1b0e8f(0xdc),'content':_0x1b0e8f(0x96)+_0x37ee1f[_0x1b0e8f(0x97)]()+_0x1b0e8f(0xf8)});break;case'tableEnabled':_0x3bf0a1&&_0x3506b9[_0x1b0e8f(0xd2)]({'role':_0x1b0e8f(0xdc),'content':_0x3bf0a1});break;case _0x1b0e8f(0xcf):_0x128154&&_0x3506b9[_0x1b0e8f(0xd2)]({'role':_0x1b0e8f(0xdc),'content':'<前文内容>\x0a'+_0x128154+_0x1b0e8f(0xa9)});break;case _0x1b0e8f(0xe2):_0x3506b9[_0x1b0e8f(0xd2)]({'role':'user','content':'[核心处理内容]:\x0a'+_0x265d2f});break;case _0x1b0e8f(0xd0):_0x3506b9['push']({'role':'assistant','content':_0x1b0e8f(0xed)});break;}}}console['groupCollapsed']('['+extensionName+_0x1b0e8f(0xaf)),console[_0x1b0e8f(0xd9)](_0x3506b9),console[_0x1b0e8f(0xc6)]();const _0x2ff3f1=await callAI(_0x3506b9);if(!_0x2ff3f1)throw new Error(_0x1b0e8f(0xb4));console[_0x1b0e8f(0xd8)]('['+extensionName+_0x1b0e8f(0xfe)),console[_0x1b0e8f(0xbe)](_0x2ff3f1),console[_0x1b0e8f(0xc6)]();const _0x363e6c=extractContentByTag(_0x2ff3f1,_0x1b0e8f(0x98)),_0x34b5ca=_0x363e6c?.[_0x1b0e8f(0x97)]()?_0x363e6c[_0x1b0e8f(0x97)]():_0x2ff3f1[_0x1b0e8f(0x97)]();if(_0x34b5ca){let _0x35dfd8='',_0x2c02b9=_0x2368c6['plotOpt_finalSystemDirective']?.[_0x1b0e8f(0x97)]()||'';const _0x454ca9={'sulv1':_0x2368c6[_0x1b0e8f(0xc1)]??0x1,'sulv2':_0x2368c6[_0x1b0e8f(0xd6)]??0x1,'sulv3':_0x2368c6[_0x1b0e8f(0xe9)]??0x1,'sulv4':_0x2368c6[_0x1b0e8f(0xc3)]??0x1};for(const _0x42e7d4 in _0x454ca9){const _0x274fbc=_0x454ca9[_0x42e7d4],_0x2cbd5d=new RegExp(_0x42e7d4[_0x1b0e8f(0x95)](/[-\/\\^$*+?.()|[\]{}]/g,_0x1b0e8f(0xb1)),'g');_0x2c02b9=_0x2c02b9[_0x1b0e8f(0x95)](_0x2cbd5d,_0x274fbc);}return _0x2c02b9?_0x35dfd8=_0x2c02b9[_0x1b0e8f(0x95)](_0x1b0e8f(0xed),_0x34b5ca):_0x35dfd8=_0x34b5ca,{'contentToAppend':_0x35dfd8};}else return null;}catch(_0x4acee7){return console[_0x1b0e8f(0x94)]('['+extensionName+_0x1b0e8f(0xe6),_0x4acee7),toastr[_0x1b0e8f(0x94)](_0x1b0e8f(0xdb)+_0x4acee7[_0x1b0e8f(0xb5)],'严重错误'),null;}finally{console[_0x1b0e8f(0xaa)](_0x1b0e8f(0xea)),console[_0x1b0e8f(0xc6)]();}}