Files
ST-Amily2-Chat-Optimisation…/core/summarizer.js
2025-07-16 21:34:02 +08:00

2 lines
14 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 _0x92248=_0x27c1;(function(_0x39c100,_0x5f021e){const _0x357c01=_0x27c1,_0x1733c7=_0x39c100();while(!![]){try{const _0x55b9a1=-parseInt(_0x357c01(0x225))/0x1*(-parseInt(_0x357c01(0x1e6))/0x2)+parseInt(_0x357c01(0x1e2))/0x3*(-parseInt(_0x357c01(0x235))/0x4)+parseInt(_0x357c01(0x1f4))/0x5+parseInt(_0x357c01(0x1f7))/0x6*(parseInt(_0x357c01(0x1f9))/0x7)+-parseInt(_0x357c01(0x20f))/0x8+-parseInt(_0x357c01(0x1f3))/0x9+parseInt(_0x357c01(0x23f))/0xa;if(_0x55b9a1===_0x5f021e)break;else _0x1733c7['push'](_0x1733c7['shift']());}catch(_0x1d40bc){_0x1733c7['push'](_0x1733c7['shift']());}}}(_0xfead,0xbc847));import{extension_settings,getContext}from'/scripts/extensions.js';import{characters}from'/script.js';import{extensionName}from'../utils/settings.js';import{extractContentByTag,replaceContentByTag,extractFullTagBlock}from'../utils/tagProcessor.js';import{getCombinedWorldbookContent}from'./lore.js';import{isGoogleEndpoint,convertToGoogleRequest,parseGoogleResponse,buildGoogleApiUrl}from'./utils/googleAdapter.js';import{intelligentPoll,createGooglePollingTask,progressTracker}from'./utils/pollingManager.js';let ChatCompletionService=undefined;function _0xfead(){const _0x35fb7f=['apiKey','timeEnd','done','groupCollapsed','11252664EUykCQ','2921660ilZyOc','[Amily2号-原始回复]','optimizationTargetTag','114yBbmHf','Authorization','320803XcZHKh','us-central1','[Amily2号-外交部]\x20执行“帝国直通车”协议直接通讯...','依赖缺失','/scripts/custom-request.js','API请求失败:\x20','mes','warn','includes','message','length','systemPrompt','text','Amily2-外交部','metadata','data','\x0a\x0a###AMILY2-SUMMARY###\x0a\x0a这里是根据对话生成的剧情摘要...\x0a\x0a[总结核心要求]:\x0a','[上下文参考]:\x0a','[Amily2-外交部]\x20发生严重错误:','外交任务总耗时','[Amily2号-外交任务]\x20','[Amily2号-Google外交部]\x20修正后的API地址:\x20','10395384DMtGbY','name2','response','loreSettings','\x20-\x20','extensions','ChatCompletionService','hostname','+总结','[Amily2号-外交部]\x20最终国书将发往:\x20','[Amily2号-外交部]\x20递交通关文牒至皇家信使...','filter','apiUrl','[Amily2-制裁]\x20系统完整性已受损所有外交活动被无限期中止。','content','Bearer\x20','json','请严格遵循以下指令:基于所有提供的背景和对话内容,生成一段精炼的剧情摘要。直接输出摘要文本,不要包含任何多余的解释、标签或前缀。\x0a\x0a[总结核心要求]:\x0a','summarizationEnabled','轮询完成但未获得有效响应','生成一段简短的剧情摘要。','is_user','113pczYNN','statusText','loreKeywords','processRequest','[输出格式与附加任务指令]:\x0a你的输出必须严格遵循以下完整结构\x0a\x0a','trim','groupEnd','worldbookEnabled','join','system','error','stringify','[世界书档案]:\x0a','API\x20URL\x20未配置。','replace','temperature','4QGzMkx','undefined','lorebookTarget','model','AMILY2_SYSTEM_PARALYZED','slice','[Amily2号-外交部]\x20执行“皇家密道”协议...','optimizationEnabled','summarizationPrompt','log','27019400REhuAc','application/json','[Amily2号-外交部]\x20未能召唤“皇家信使”部分高级功能如Claw代理将受限。请考虑更新SillyTavern版本。','choices','user','name','toLocaleTimeString','[Amily2号-外交部]\x20已成功召唤“皇家信使”(ChatCompletionService)。','push','endsWith','轮询失败:\x20','loreActivationMode','loreDepth','/v1','[Amily2-外交部]\x20未能获取AI响应内容','split','>\x20未找到或为空优化任务已跳过。','###AMILY2-SUMMARY###','这里是优化后的文本内容...','aiplatform.googleapis.com','[Amily2-外交部]\x20已将史册律法附加至国书准备发往下一站。','onAttempt','2989842wqKGPS','/v1/chat/completions','dir','loreInsertionPosition','2858KIWQxH','world','\x20|\x20模式:\x20','characterId','maxTokens','[轮询错误]','仅总结','mainPrompt','POST'];_0xfead=function(){return _0x35fb7f;};return _0xfead();}try{const module=await import(_0x92248(0x1fd));ChatCompletionService=module[_0x92248(0x215)],console['log'](_0x92248(0x1d3));}catch(_0x23c5b9){console[_0x92248(0x200)](_0x92248(0x241),_0x23c5b9);}function _0x27c1(_0x3c0f7f,_0x5b6997){const _0xfead73=_0xfead();return _0x27c1=function(_0x27c1a7,_0x4eca0c){_0x27c1a7=_0x27c1a7-0x1d3;let _0x393a94=_0xfead73[_0x27c1a7];return _0x393a94;},_0x27c1(_0x3c0f7f,_0x5b6997);}export async function checkAndFixWithAPI(_0x257205,_0x172d10){const _0xe48507=_0x92248;if(window[_0xe48507(0x239)]===!![])return console['error'](_0xe48507(0x21c)),null;const _0x379409=extension_settings[extensionName],_0x1e427d=_0x379409[_0xe48507(0x23c)],_0x5087a8=_0x379409[_0xe48507(0x221)];if(!_0x1e427d&&!_0x5087a8)return null;if(!_0x379409[_0xe48507(0x21b)]||!_0x379409[_0xe48507(0x21b)][_0xe48507(0x22a)]())return toastr[_0xe48507(0x22f)](_0xe48507(0x232),_0xe48507(0x206)),null;console['groupCollapsed'](_0xe48507(0x20d)+new Date()[_0xe48507(0x245)]()+_0xe48507(0x1e8)+(_0x1e427d?'优化':'')+(_0x5087a8?_0x1e427d?_0xe48507(0x217):_0xe48507(0x1ec):'')),console['time']('外交任务总耗时');try{const _0x23053f=_0x257205[_0xe48507(0x1ff)],_0x3bb1fa=_0x379409[_0xe48507(0x1f6)]||_0xe48507(0x21d);let _0x446f3b;if(_0x1e427d){_0x446f3b=extractFullTagBlock(_0x23053f,_0x3bb1fa);if(!_0x446f3b||extractContentByTag(_0x446f3b,_0x3bb1fa)?.[_0xe48507(0x22a)]()===''){console[_0xe48507(0x23e)]('[Amily2-外交部]\x20目标标签\x20<'+_0x3bb1fa+_0xe48507(0x1dc)),_0x446f3b=_0x23053f;if(!_0x5087a8)return console[_0xe48507(0x1f0)](_0xe48507(0x20c)),console[_0xe48507(0x22b)](),{'optimizedContent':_0x23053f,'summary':null};}}else _0x446f3b=_0x23053f;const _0x124e86=getContext(),_0x3e884c=_0x124e86['name1']||'用户',_0x4f80db=_0x124e86[_0xe48507(0x210)]||'角色',_0x43f09b=_0x172d10[_0xe48507(0x203)]>0x0&&_0x172d10[_0x172d10[_0xe48507(0x203)]-0x1][_0xe48507(0x224)]?_0x172d10[_0x172d10[_0xe48507(0x203)]-0x1]:null,_0x20e3c4=_0x43f09b?_0x172d10[_0xe48507(0x23a)](0x0,-0x1):_0x172d10,_0x43451d=_0x20e3c4['map'](_0x44ae28=>_0x44ae28['mes']&&_0x44ae28[_0xe48507(0x1ff)][_0xe48507(0x22a)]()?(_0x44ae28[_0xe48507(0x224)]?_0x3e884c:_0x4f80db)+':\x20'+_0x44ae28['mes']['trim']():null)[_0xe48507(0x21a)](Boolean)[_0xe48507(0x22d)]('\x0a');let _0x2f2a1a='';if(_0x379409[_0xe48507(0x22c)]){const _0x4c7676=characters[_0x124e86[_0xe48507(0x1e9)]];_0x4c7676?.[_0xe48507(0x208)]?.['extensions']?.[_0xe48507(0x1e7)]&&(_0x2f2a1a=await getCombinedWorldbookContent(_0x4c7676['data'][_0xe48507(0x214)][_0xe48507(0x1e7)]));}const _0x5e6ad2=[];_0x379409['mainPrompt']?.[_0xe48507(0x22a)]()&&_0x5e6ad2[_0xe48507(0x1d4)]({'role':_0xe48507(0x22e),'content':_0x379409[_0xe48507(0x1ed)][_0xe48507(0x22a)]()});if(_0x1e427d){if(_0x379409[_0xe48507(0x204)]?.['trim']())_0x5e6ad2['push']({'role':_0xe48507(0x22e),'content':_0x379409['systemPrompt']['trim']()});}if(_0x1e427d&&_0x5087a8){const _0x278041=(_0xe48507(0x229)+_0x446f3b[_0xe48507(0x233)](extractContentByTag(_0x446f3b,_0x3bb1fa),_0xe48507(0x1de))+_0xe48507(0x209)+(_0x379409[_0xe48507(0x23d)]?.['trim']()||_0xe48507(0x223)))['trim']();_0x5e6ad2[_0xe48507(0x1d4)]({'role':_0xe48507(0x22e),'content':_0x278041});}else{if(!_0x1e427d&&_0x5087a8){const _0x5294b5=_0xe48507(0x220)+_0x379409[_0xe48507(0x23d)][_0xe48507(0x22a)]();_0x5e6ad2[_0xe48507(0x1d4)]({'role':_0xe48507(0x22e),'content':_0x5294b5});}}if(_0x2f2a1a)_0x5e6ad2[_0xe48507(0x1d4)]({'role':'user','content':_0xe48507(0x231)+_0x2f2a1a});if(_0x43451d)_0x5e6ad2['push']({'role':_0xe48507(0x243),'content':_0xe48507(0x20a)+_0x43451d});let _0x4d7ed0=_0x43f09b?_0x3e884c+':\x20'+_0x43f09b[_0xe48507(0x1ff)]+'\x0a'+_0x4f80db+':\x20'+_0x446f3b:_0x446f3b;_0x5e6ad2[_0xe48507(0x1d4)]({'role':_0xe48507(0x243),'content':'[核心处理内容]:\x0a'+_0x4d7ed0}),console[_0xe48507(0x1f2)]('[Amily2号-最终国书内容\x20(发往AI)]'),console[_0xe48507(0x1e4)](_0x5e6ad2),console[_0xe48507(0x22b)]();const _0x24c709=isGoogleEndpoint(_0x379409['apiUrl']);let _0x1b3345=_0x379409['apiUrl'][_0xe48507(0x22a)]();const _0x24612d=_0x379409['model'];let _0x2679bd;if(_0x379409['forceProxyForCustomApi']){console['log'](_0xe48507(0x23b));if(typeof ChatCompletionService===_0xe48507(0x236)||!ChatCompletionService?.[_0xe48507(0x228)]){const _0x48cac9='无法使用“皇家密道”当前SillyTavern版本过低或缺少核心文件\x20/scripts/custom-request.js。';toastr[_0xe48507(0x22f)](_0x48cac9,_0xe48507(0x1fc));throw new Error(_0x48cac9);}try{const _0x211864={'stream':![],'messages':_0x5e6ad2,'max_tokens':_0x379409['maxTokens'],'temperature':_0x379409[_0xe48507(0x234)],'model':_0x379409[_0xe48507(0x238)],'chat_completion_source':'custom','custom_url':_0x379409[_0xe48507(0x21b)],'reverse_proxy':'/api/proxy'};console[_0xe48507(0x23e)](_0xe48507(0x219),_0x211864);const _0x163880=await ChatCompletionService[_0xe48507(0x228)](_0x211864,{},!![]);if(!_0x163880||!_0x163880['content'])throw new Error('皇家信使未能带回有效情报(响应为空)。');_0x2679bd=_0x163880[_0xe48507(0x21d)];}catch(_0x2016b4){console[_0xe48507(0x22f)]('通过“皇家密道”调用API时发生错误:',_0x2016b4);throw _0x2016b4;}}else{console['log'](_0xe48507(0x1fb));const _0x2e2c04=isGoogleEndpoint(_0x379409[_0xe48507(0x21b)]);let _0x52bf6d=_0x379409['apiUrl']['trim']();const _0x111551=_0x379409[_0xe48507(0x238)],_0x1fb196=_0x379409[_0xe48507(0x1ea)],_0x276a21=_0x379409['temperature'];let _0x120dd9;if(_0x2e2c04)_0x120dd9=buildGoogleApiUrl(_0x52bf6d,_0x111551),console[_0xe48507(0x23e)](_0xe48507(0x20e)+_0x120dd9);else{let _0x59f2b8=_0x52bf6d;_0x59f2b8[_0xe48507(0x1d5)]('/')&&(_0x59f2b8=_0x59f2b8[_0xe48507(0x23a)](0x0,-0x1)),_0x59f2b8[_0xe48507(0x1d5)](_0xe48507(0x1d9))&&(_0x59f2b8=_0x59f2b8[_0xe48507(0x23a)](0x0,-0x3)),_0x59f2b8['endsWith']('/v1/chat/completions')||_0x59f2b8[_0xe48507(0x1d5)]('/v1beta/openai')?_0x120dd9=_0x59f2b8:_0x120dd9=_0x59f2b8+_0xe48507(0x1e3);}console['log'](_0xe48507(0x218)+_0x120dd9);const _0x5dd8c4=_0x379409[_0xe48507(0x1ef)]?.[_0xe48507(0x22a)](),_0x26a8c3={'Content-Type':_0xe48507(0x240)};if(_0x2e2c04){const _0x1a7bf6=new URL(_0x52bf6d);_0x1a7bf6[_0xe48507(0x216)][_0xe48507(0x201)](_0xe48507(0x1df))||_0x52bf6d[_0xe48507(0x201)](_0xe48507(0x1fa))?_0x26a8c3[_0xe48507(0x1f8)]=_0xe48507(0x21e)+_0x5dd8c4:_0x26a8c3['X-goog-api-key']=_0x5dd8c4;}else _0x26a8c3[_0xe48507(0x1f8)]=_0xe48507(0x21e)+_0x5dd8c4;let _0x23717a;_0x2e2c04?_0x23717a=JSON[_0xe48507(0x230)](convertToGoogleRequest({'model':_0x111551,'messages':_0x5e6ad2,'max_tokens':_0x1fb196,'temperature':_0x276a21})):_0x23717a=JSON[_0xe48507(0x230)]({'model':_0x111551,'messages':_0x5e6ad2,'max_tokens':_0x1fb196,'temperature':_0x276a21,'stream':![]});const _0x63203d=await fetch(_0x120dd9,{'method':_0xe48507(0x1ee),'headers':_0x26a8c3,'body':_0x23717a});if(!_0x63203d['ok'])throw new Error(_0xe48507(0x1fe)+_0x63203d['status']+'\x20'+_0x63203d[_0xe48507(0x226)]+_0xe48507(0x213)+await _0x63203d[_0xe48507(0x205)]());let _0x3e2ed7=await _0x63203d[_0xe48507(0x21f)]();if(_0x2e2c04&&_0x3e2ed7['name']&&_0x3e2ed7[_0xe48507(0x207)]){console[_0xe48507(0x23e)]('[Amily2号-Google外交部]\x20收到异步操作ID启用轮询机制...');const _0x5c9927=_0x3e2ed7[_0xe48507(0x244)],_0x3a79dc=progressTracker(_0x5c9927,0x6);_0x3a79dc['start']();try{const _0x53afda=new URL(_0x52bf6d),_0x49c987=createGooglePollingTask(_0x5c9927,_0x53afda['origin'],_0x26a8c3),_0x369e67={'maxAttempts':0x6,'baseDelay':0xbb8,'shouldStop':_0xdbbfe3=>_0xdbbfe3[_0xe48507(0x1f1)],'onAttempt':(_0x27d689,_0x304ba3)=>{const _0x1eb4d8=_0xe48507;_0x3a79dc[_0x1eb4d8(0x1e1)](_0x27d689,_0x304ba3);},'onError':(_0x388b5e,_0x111fdb)=>{const _0x2a36a8=_0xe48507;_0x3a79dc[_0x2a36a8(0x22f)](_0x388b5e['message']);}},_0x5e2b35=await intelligentPoll(_0x49c987,_0x369e67);_0x3a79dc['complete']();if(!_0x5e2b35[_0xe48507(0x211)])throw new Error(_0xe48507(0x222));_0x3e2ed7=_0x5e2b35[_0xe48507(0x211)],_0x2679bd=parseGoogleResponse(_0x3e2ed7)?.[_0xe48507(0x242)]?.[0x0]?.[_0xe48507(0x202)]?.[_0xe48507(0x21d)];}catch(_0x592f1b){console[_0xe48507(0x22f)](_0xe48507(0x1eb),_0x592f1b),_0x3a79dc[_0xe48507(0x22f)](_0xe48507(0x1d6)+_0x592f1b[_0xe48507(0x202)]);throw new Error('轮询任务失败:\x20'+_0x592f1b['message']);}}else _0x2679bd=_0x2e2c04?parseGoogleResponse(_0x3e2ed7)?.[_0xe48507(0x242)]?.[0x0]?.[_0xe48507(0x202)]?.[_0xe48507(0x21d)]:_0x3e2ed7?.[_0xe48507(0x242)]?.[0x0]?.[_0xe48507(0x202)]?.[_0xe48507(0x21d)];}if(!_0x2679bd)return console[_0xe48507(0x22f)](_0xe48507(0x1da),_0x2679bd),null;console['groupCollapsed'](_0xe48507(0x1f5)),console[_0xe48507(0x23e)](_0x2679bd),console[_0xe48507(0x22b)]();let _0x20b32c=_0x23053f,_0xb7c396=null;if(_0x1e427d&&_0x5087a8){const _0x1608d9=_0xe48507(0x1dd),_0x1a4d42=_0x2679bd[_0xe48507(0x1db)](_0x1608d9),_0x30c254=_0x1a4d42[0x0]?.[_0xe48507(0x22a)]();_0xb7c396=_0x1a4d42[0x1]?.[_0xe48507(0x22a)]()||null;if(_0x30c254){const _0x4449f1=extractContentByTag(_0x30c254,_0x3bb1fa);_0x4449f1?.['trim']()&&(_0x20b32c=replaceContentByTag(_0x23053f,_0x3bb1fa,_0x4449f1));}}else{if(_0x1e427d){const _0x992855=extractContentByTag(_0x2679bd,_0x3bb1fa);_0x992855?.['trim']()&&(_0x20b32c=replaceContentByTag(_0x23053f,_0x3bb1fa,_0x992855));}else _0xb7c396=_0x2679bd[_0xe48507(0x22a)]();}const _0x18e606={'optimizedContent':_0x20b32c,'summary':_0xb7c396};return _0xb7c396&&_0x5087a8&&(_0x18e606[_0xe48507(0x212)]={'activationMode':_0x379409[_0xe48507(0x1d7)],'insertionPosition':_0x379409[_0xe48507(0x1e5)],'depth':_0x379409[_0xe48507(0x1d8)],'keywords':_0x379409[_0xe48507(0x227)],'target':_0x379409[_0xe48507(0x237)]},console['log'](_0xe48507(0x1e0),_0x18e606[_0xe48507(0x212)])),console[_0xe48507(0x1f0)](_0xe48507(0x20c)),console[_0xe48507(0x22b)](),_0x18e606;}catch(_0x4ddb30){return console['error'](_0xe48507(0x20b),_0x4ddb30),toastr[_0xe48507(0x22f)]('Amily2号任务失败:\x20'+_0x4ddb30[_0xe48507(0x202)],'严重错误'),console[_0xe48507(0x1f0)](_0xe48507(0x20c)),console[_0xe48507(0x22b)](),null;}}