mirror of
https://github.com/Wx-2025/ST-Amily2-Chat-Optimisation.git
synced 2026-06-06 13:55:51 +00:00
2 lines
31 KiB
JavaScript
2 lines
31 KiB
JavaScript
'use strict';const _0x437d66=_0x2a7d;(function(_0x12c3fb,_0x147091){const _0x2754bf=_0x2a7d,_0x56b3c5=_0x12c3fb();while(!![]){try{const _0x258f3b=parseInt(_0x2754bf(0x186))/0x1*(parseInt(_0x2754bf(0x1ad))/0x2)+parseInt(_0x2754bf(0x1dd))/0x3*(parseInt(_0x2754bf(0x168))/0x4)+-parseInt(_0x2754bf(0x1aa))/0x5+-parseInt(_0x2754bf(0x174))/0x6*(parseInt(_0x2754bf(0x1d7))/0x7)+parseInt(_0x2754bf(0x16e))/0x8*(-parseInt(_0x2754bf(0x1e6))/0x9)+-parseInt(_0x2754bf(0x189))/0xa*(parseInt(_0x2754bf(0x18d))/0xb)+parseInt(_0x2754bf(0x1e5))/0xc*(parseInt(_0x2754bf(0x1b3))/0xd);if(_0x258f3b===_0x147091)break;else _0x56b3c5['push'](_0x56b3c5['shift']());}catch(_0x6ecce2){_0x56b3c5['push'](_0x56b3c5['shift']());}}}(_0x5072,0xbb932));function _0x5072(){const _0x5c306b=['[翰林院]\x20未能获取SillyTavern上下文,初始化失败。','\x20个向量条目。','getTime','无法确定要清空的目标宝库。','sort','saveSettingsDebounced','No\x20messages\x20to\x20process.','getRequestHeaders','vector','[来源:\x20聊天记录,\x20楼层:\x20#','rerank','[翰林院-核心]\x20聊天记录凝识失败:\x20','extensionSettings','now','此操作将清空旧版数据,并开始使用新版数据库。此过程不可逆,是否继续?','[翰林院-核心]\x20ingestTextToHanlinyuan\x20失败:','final_score','top_n','advanced','[翰林院-日志]\x20查询成功,返回\x20','[翰林院-日志]\x20清空宝库API错误:','object','hanlinyuanRagProcessor','/api/vector/purge','sourceName','[翰林院-Rerank]\x20开始外部API重排序...','start','info','[翰林院-日志]\x20开始向量查询\x20(采用最终API交互模式)...','stringify','在insertVectors内部也无法获取collectionId','user','[翰林院-日志]\x20统计目标集合ID:\x20','未知来源','AbortError','relevance_score','\x20个条目。','[翰林院-日志]\x20发送到\x20/api/vector/query\x20的请求体:','\x20记录凝识范围:\x20','\x0a</','toISOString','4jzADMv','getContext','message','replace','输入文本为空',']\x20的消息已成功凝识。','8514808nLDGPt','\x20-\x20楼层\x20#','novel','外部Rerank完成','hashes','[翰林院-日志]\x20无法确定要清空的目标集合ID。','30QFrOfq','[来源:\x20','[翰林院-日志]\x20/api/vector/list\x20响应状态:\x20','status','test','[翰林院-日志]\x20/api/vector/query\x20响应状态:\x20','hanlinyuan-rag-core','小说录入','webllm','join','[翰林院-核心]\x20文本录入失败:\x20','第1章','[翰林院-核心]\x20已为宝库\x20','/api/vector/query','manual','floor','[翰林院-日志]\x20开始获取向量总数...','[翰林院-分块]\x20未知的来源类型\x20\x27','139351hFeOxO','\x20条结果。',',\x20第','987400qLqYka','substring','injection','forEach','11hdCqUr','lorebook','template','[翰林院-核心]\x20凝识任务已锁定忆识宝库ID:\x20','source','zh-CN','用户取消了迁移操作','chat','手动录入','\x20个块。','文本块和向量数量不匹配','宝库查询API错误\x20','send_date','聊天记录\x20#','mes','[翰林院-核心]\x20聊天记录凝识完成,成功插入\x20','/api/vector/list','[翰林院]\x20检索或注入时发生错误:','HANLINYUAN_RAG','POST','condensation','[翰林院-日志]\x20统计成功,向量总数:\x20','(已锁定:\x20','[翰林院-迁移]\x20用户确认迁移,正在清空旧宝库:\x20','toString','initialized','rearrangeChat','aborted','操作已取消。','6674515Etakwl','slice','核心未初始化','16EECXHx','results','json','[翰林院-核心]\x20processCondensation\x20失败:','clearJob','oldId','91zSCJea','世界书条目','is_user','condensationHistory','chat_history','[翰林院-迁移]\x20用户取消了迁移操作。','部分]','忆识存入API错误\x20','text','end','warning','queryMessageCount','enabled','min','find','position','[翰林院-日志]\x20查询目标集合ID:\x20','\x27的文本分割成\x20','[翰林院-Rerank]\x20外部Rerank失败,将仅使用内部加权。','rerank_score','[翰林院-核心]\x20将来源\x27','\x27,使用通用分块逻辑。','[翰林院-核心]\x20insertVectors被调用时未提供collectionId!','newId','error','reduce','notify','retrieval','[翰林院-日志]\x20/api/vector/query\x20响应内容:','max','vectors_rearrangeChat','batchSize','messageTypes','warn','map','trim','424718OJdoHC','findIndex','matchThreshold','success','翰林院忆识核心已启动\x20(V5.2-集成版),已注册到全局\x20hanlinyuanRagProcessor\x20对象。','/api/vector/insert','2897529CqXsoG','[翰林院-迁移]\x20旧宝库已清空,将向新宝库写入数据:\x20','[翰林院-核心]\x20已将\x20','push','unknown','filter','世界书','{{text}}','2552604NTWMXV','9dWwRaH','metadata','index','length','log','%%HANLINYUAN_RAG_INJECTION%%'];_0x5072=function(){return _0x5c306b;};return _0x5072();}import{extension_prompt_roles,setExtensionPrompt}from'/script.js';import*as _0x8ec9c2 from'./utils/context-utils.js';import{getCollectionIdInfo}from'./utils/context-utils.js';function _0x2a7d(_0x42659e,_0x5f11e7){const _0x507246=_0x5072();return _0x2a7d=function(_0x2a7d87,_0xf5cc07){_0x2a7d87=_0x2a7d87-0x163;let _0x90b598=_0x507246[_0x2a7d87];return _0x90b598;},_0x2a7d(_0x42659e,_0x5f11e7);}import{defaultSettings as _0x164183}from'./rag-settings.js';import*as _0x298642 from'./ingestion-manager.js';import{getEmbeddings,fetchEmbeddingModels as _0x5c719b,fetchRerankModels as _0x46b222,executeRerank,testApiConnection as _0x2fd8ca}from'./rag-api.js';const MODULE_NAME=_0x437d66(0x17a),OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME=_0x437d66(0x1d1);let context=null,settings=null,lockedCollectionId=null;export{initialize,getSettings,saveSettings,resetSettings,_0x2fd8ca as testApiConnection,_0x5c719b as fetchEmbeddingModels,_0x46b222 as fetchRerankModels,getVectorCount,purgeStorage,getMessagesForCondensation,processCondensation,ingestTextToHanlinyuan,getCollectionId,toggleSessionLock,isSessionLocked,getLockedSessionInfo};function initialize(){const _0x3e3d04=_0x437d66;context=SillyTavern[_0x3e3d04(0x169)]();if(!context){console[_0x3e3d04(0x1cb)](_0x3e3d04(0x1ec));return;}settings=getSettings(),!window[_0x3e3d04(0x202)]&&(window[_0x3e3d04(0x202)]={}),window[_0x3e3d04(0x202)][_0x3e3d04(0x1a7)]=rearrangeChat,window['hanlinyuanRagProcessor'][_0x3e3d04(0x1a6)]=!![],console[_0x3e3d04(0x1ea)](_0x3e3d04(0x1db));}async function ingestTextToHanlinyuan(_0x5e73c1,_0x8c3815=_0x437d66(0x182),_0x396767='',_0x542ec8=()=>{},_0x551f98=null,_0x3a0708=()=>{},_0x70d24=()=>{},_0x648108=null,_0x4a90b2=0x0,_0x542fbb=null){const _0x3696b5=_0x437d66;if(!_0x5e73c1||!_0x5e73c1[_0x3696b5(0x1d6)]())return{'success':![],'error':_0x3696b5(0x16c)};if(!settings)return{'success':![],'error':_0x3696b5(0x1ac)};try{let _0x3a7ed2=await getCollectionId();const _0x9ec78e=getCollectionIdInfo();if(_0x9ec78e[_0x3696b5(0x1b2)]&&_0x9ec78e[_0x3696b5(0x1b2)]===_0x3a7ed2&&_0x9ec78e[_0x3696b5(0x1b2)]!==_0x9ec78e[_0x3696b5(0x1ca)]){const _0x6f1bb4=confirm(_0x3696b5(0x1fa));if(_0x6f1bb4)_0x3a0708(_0x3696b5(0x1a4)+_0x9ec78e[_0x3696b5(0x1b2)],'warn'),await purgeStorage(_0x9ec78e[_0x3696b5(0x1b2)]),_0x3a7ed2=_0x9ec78e[_0x3696b5(0x1ca)],_0x3a0708('[翰林院-迁移]\x20旧宝库已清空,将向新宝库写入数据:\x20'+_0x3a7ed2,_0x3696b5(0x1da));else return _0x3a0708(_0x3696b5(0x1b8),_0x3696b5(0x207)),toastr[_0x3696b5(0x207)](_0x3696b5(0x1a9)),{'success':![],'error':_0x3696b5(0x193)};}if(!_0x3a7ed2)throw new Error('无法确定当前忆识宝库的ID,请确认角色已正确加载。');_0x3a0708('[翰林院-核心]\x20已锁定忆识宝库ID:\x20'+_0x3a7ed2,_0x3696b5(0x207)),_0x542ec8({'message':'正在智能分块...','processed':0x0,'total':0x1});const _0x6c59c5=splitIntoChunks(_0x5e73c1,_0x8c3815,{'sourceName':_0x396767}),_0x56749a=_0x6c59c5[_0x3696b5(0x1e9)];if(_0x551f98?.['aborted'])throw new Error('AbortError');_0x3a0708(_0x3696b5(0x1c7)+(_0x396767||_0x8c3815)+_0x3696b5(0x1c4)+_0x56749a+_0x3696b5(0x196),_0x3696b5(0x207));if(_0x56749a===0x0)return{'success':!![],'count':0x0};const _0x3805d1=settings[_0x3696b5(0x1ce)]['batchSize']||0x5;let _0x572c5b=_0x4a90b2;for(let _0x3ee710=_0x4a90b2;_0x3ee710<_0x56749a;_0x3ee710+=_0x3805d1){if(_0x551f98?.['aborted'])throw new Error(_0x3696b5(0x20e));const _0x4f2fc4=_0x6c59c5['slice'](_0x3ee710,_0x3ee710+_0x3805d1);_0x542ec8({'message':'正在处理\x20'+(_0x3ee710+0x1)+'-'+(_0x3ee710+_0x4f2fc4['length'])+'\x20块','processed':_0x3ee710,'total':_0x56749a});const _0x27191a=_0x4f2fc4[_0x3696b5(0x1d5)](_0x34950e=>_0x34950e[_0x3696b5(0x1bb)]),_0x4bd94d=await getEmbeddings(_0x27191a,_0x551f98);if(_0x551f98?.[_0x3696b5(0x1a8)])throw new Error(_0x3696b5(0x20e));if(_0x4f2fc4[_0x3696b5(0x1e9)]!==_0x4bd94d[_0x3696b5(0x1e9)])throw new Error('文本块和向量数量不匹配');const _0x5eb96f=_0x4f2fc4[_0x3696b5(0x1d5)]((_0x28a6e6,_0x41671c)=>({..._0x28a6e6,'vector':_0x4bd94d[_0x41671c]}));await insertVectors(_0x5eb96f,_0x551f98,_0x3a7ed2),_0x572c5b+=_0x4f2fc4[_0x3696b5(0x1e9)],_0x648108&&_0x298642['saveProgress'](_0x648108,_0x572c5b,_0x56749a),_0x70d24();}_0x648108&&_0x298642[_0x3696b5(0x1b1)](_0x648108);if(_0x542fbb){const _0x360b11=await getCollectionId(),_0x77f90d=_0x542fbb[_0x3696b5(0x1bc)]===0x0?context[_0x3696b5(0x194)][_0x3696b5(0x1e9)]:_0x542fbb['end'];settings[_0x3696b5(0x1b6)][_0x360b11]={'start':_0x542fbb[_0x3696b5(0x206)],'end':_0x77f90d,'timestamp':new Date()[_0x3696b5(0x167)]()},saveSettings(),_0x3a0708(_0x3696b5(0x180)+_0x360b11+'\x20记录凝识范围:\x20'+_0x542fbb[_0x3696b5(0x206)]+'-'+_0x77f90d,_0x3696b5(0x207));}return _0x3a0708('[翰林院-核心]\x20成功插入\x20'+_0x572c5b+_0x3696b5(0x1ed),_0x3696b5(0x1da)),{'success':!![],'count':_0x572c5b};}catch(_0x2a893a){if(_0x2a893a['name']===_0x3696b5(0x20e)){_0x3a0708('[翰林院-核心]\x20文本录入任务被用户中止。','warn');throw _0x2a893a;}return console['error'](_0x3696b5(0x1fb),_0x2a893a),_0x3a0708(_0x3696b5(0x17e)+_0x2a893a[_0x3696b5(0x16a)],_0x3696b5(0x1cb)),{'success':![],'error':_0x2a893a[_0x3696b5(0x16a)]};}}function getSettings(){const _0x3838d7=_0x437d66;if(!context||!context[_0x3838d7(0x1f8)])return structuredClone(_0x164183);let _0x4f3af6=context['extensionSettings'][MODULE_NAME];!_0x4f3af6&&(_0x4f3af6={},context[_0x3838d7(0x1f8)][MODULE_NAME]=_0x4f3af6);_0x4f3af6[_0x3838d7(0x1b6)]===undefined&&(_0x4f3af6[_0x3838d7(0x1b6)]={});for(const _0x3e032f in _0x164183){if(_0x4f3af6[_0x3e032f]===undefined)_0x4f3af6[_0x3e032f]=structuredClone(_0x164183[_0x3e032f]);else{if(typeof _0x164183[_0x3e032f]===_0x3838d7(0x201)&&!Array['isArray'](_0x164183[_0x3e032f])&&_0x164183[_0x3e032f]!==null)for(const _0x38cd62 in _0x164183[_0x3e032f]){_0x4f3af6[_0x3e032f][_0x38cd62]===undefined&&(_0x4f3af6[_0x3e032f][_0x38cd62]=_0x164183[_0x3e032f][_0x38cd62]);}}}return _0x4f3af6;}function saveSettings(){const _0x43abac=_0x437d66;if(context)context[_0x43abac(0x1f1)]();}function resetSettings(){const _0x111ba6=_0x437d66;context&&(context[_0x111ba6(0x1f8)][MODULE_NAME]=structuredClone(_0x164183),saveSettings());}function showNotification(_0x5addad,_0x5b8b5f='info'){toastr[_0x5b8b5f](_0x5addad);}function getTagForSource(_0x53d37e){const _0x291bed=_0x437d66;switch(_0x53d37e){case _0x291bed(0x1b7):return'聊天记录';case _0x291bed(0x18e):return _0x291bed(0x1e3);case _0x291bed(0x182):return _0x291bed(0x195);case _0x291bed(0x170):return _0x291bed(0x17b);default:return'资料';}}function splitIntoChunks(_0x40d91d,_0x11a85a,_0xb1532c={}){const _0x22d5e3=_0x437d66;switch(_0x11a85a){case _0x22d5e3(0x170):return _chunkForNovel(_0x40d91d,_0xb1532c);case'chat_history':return _chunkForChatHistory(_0x40d91d,_0xb1532c);case _0x22d5e3(0x18e):return _chunkForLorebook(_0x40d91d,_0xb1532c);case'manual':return _chunkForManual(_0x40d91d,_0xb1532c);default:console[_0x22d5e3(0x1d4)](_0x22d5e3(0x185)+_0x11a85a+_0x22d5e3(0x1c8));return _chunkForManual(_0x40d91d,{..._0xb1532c,'sourceName':_0xb1532c[_0x22d5e3(0x204)]||_0x22d5e3(0x20d)});}}function _chunkForNovel(_0x15e720,_0x179432){const _0x4263d9=_0x437d66,{chunkSize:_0x584b4d,overlap:_0x29e80f}=settings[_0x4263d9(0x1fe)],{sourceName:sourceName='小说'}=_0x179432,_0x3eaf6c=[];if(!_0x15e720||_0x584b4d<=0x0)return _0x3eaf6c;const _0x578230=/(第\s*[一二三四五六七八九十百千万零\d]+\s*卷)/gim,_0xb20c6b=/(第\s*[一二三四五六七八九十百千万零\d]+\s*[章回节部])|^(Chapter\s+\d+)/gim;let _0x2913de=0x0;const _0x41cd39=_0x15e720['split']('\x0a');let _0x5af136='第1卷',_0x422fbd=_0x4263d9(0x17f),_0x467401=[];function _0x3abac0(){const _0x5516d5=_0x4263d9;if(_0x467401[_0x5516d5(0x1e9)]===0x0)return;const _0x4c92d1=_0x467401[_0x5516d5(0x17d)]('\x0a');let _0x3cc836=0x0,_0x323a0e=0x1;while(_0x3cc836<_0x4c92d1['length']){const _0x1c7854=Math[_0x5516d5(0x1c0)](_0x3cc836+_0x584b4d,_0x4c92d1['length']),_0x14327e=_0x4c92d1['substring'](_0x3cc836,_0x1c7854);if(_0x14327e[_0x5516d5(0x1d6)]()[_0x5516d5(0x1e9)]>0x0){const _0x5b18de={'source':_0x5516d5(0x170),'sourceName':sourceName,'timestamp':new Date()['toISOString'](),'globalIndex':_0x2913de++,'volume':_0x5af136,'chapter':_0x422fbd,'section':_0x323a0e},_0x5b8dd6=getTagForSource(_0x5516d5(0x170)),_0x5cf7cd=_0x5516d5(0x175)+sourceName+',\x20'+_0x5af136+',\x20'+_0x422fbd+_0x5516d5(0x188)+_0x323a0e+'节]',_0x5459c5='<'+_0x5b8dd6+'>\x0a'+_0x5cf7cd+'\x0a'+_0x14327e+'\x0a</'+_0x5b8dd6+'>';_0x3eaf6c[_0x5516d5(0x1e0)]({'text':_0x5459c5,'metadata':_0x5b18de}),_0x323a0e++;}_0x3cc836+=_0x584b4d-_0x29e80f;if(_0x3cc836>=_0x4c92d1[_0x5516d5(0x1e9)])break;}_0x467401=[];}for(const _0x80f629 of _0x41cd39){const _0x18f06d=_0x80f629[_0x4263d9(0x1d6)]();if(_0x578230['test'](_0x18f06d))_0x3abac0(),_0x5af136=_0x18f06d,_0x422fbd='第1章';else _0xb20c6b[_0x4263d9(0x178)](_0x18f06d)?(_0x3abac0(),_0x422fbd=_0x18f06d):_0x467401[_0x4263d9(0x1e0)](_0x80f629);}_0x3abac0();if(_0x3eaf6c[_0x4263d9(0x1e9)]===0x0&&_0x15e720[_0x4263d9(0x1e9)]>0x0){let _0x262264=0x0,_0x351aa2=0x1;while(_0x262264<_0x15e720[_0x4263d9(0x1e9)]){const _0x258ec7=Math['min'](_0x262264+_0x584b4d,_0x15e720[_0x4263d9(0x1e9)]),_0xf831a8=_0x15e720[_0x4263d9(0x18a)](_0x262264,_0x258ec7),_0x4e7873={'source':_0x4263d9(0x170),'sourceName':sourceName,'timestamp':new Date()['toISOString'](),'globalIndex':_0x3eaf6c[_0x4263d9(0x1e9)],'volume':'第1卷','chapter':_0x4263d9(0x17f),'section':_0x351aa2},_0x392766=getTagForSource(_0x4263d9(0x170)),_0x5786fe='[来源:\x20'+sourceName+',\x20第1卷,\x20第1章,\x20第'+_0x351aa2+'节]',_0x55c8b9='<'+_0x392766+'>\x0a'+_0x5786fe+'\x0a'+_0xf831a8+_0x4263d9(0x166)+_0x392766+'>';_0x3eaf6c[_0x4263d9(0x1e0)]({'text':_0x55c8b9,'metadata':_0x4e7873}),_0x351aa2++,_0x262264+=_0x584b4d-_0x29e80f;}}return _0x3eaf6c;}function _chunkForChatHistory(_0x14d423,_0x2317cc){const _0x332b01=_0x437d66,{chunkSize:_0x5db4ed,overlap:_0x564f71}=settings[_0x332b01(0x1fe)],{floor:_0x182d73,is_user:_0x2a4944,timestamp:_0x8f71f2}=_0x2317cc,_0xfa7016=[];if(!_0x14d423||_0x5db4ed<=0x0)return _0xfa7016;let _0x3f4b30=0x1,_0x523492=0x0;while(_0x523492<_0x14d423[_0x332b01(0x1e9)]){const _0x22137b=Math[_0x332b01(0x1c0)](_0x523492+_0x5db4ed,_0x14d423['length']),_0x31b6f4=_0x14d423[_0x332b01(0x18a)](_0x523492,_0x22137b),_0x470289=_0x332b01(0x1f5)+_0x182d73+_0x332b01(0x188)+_0x3f4b30+'部分]',_0x255543=getTagForSource(_0x332b01(0x1b7)),_0x3a85f8='<'+_0x255543+'>\x0a'+_0x470289+'\x0a'+_0x31b6f4+_0x332b01(0x166)+_0x255543+'>';_0xfa7016[_0x332b01(0x1e0)]({'text':_0x3a85f8,'metadata':{'source':_0x332b01(0x1b7),'sourceName':_0x332b01(0x19a)+_0x182d73,'floor':_0x182d73,'part':_0x3f4b30,'is_user':_0x2a4944,'timestamp':_0x8f71f2}}),_0x3f4b30++,_0x523492+=_0x5db4ed-_0x564f71;if(_0x523492>=_0x14d423[_0x332b01(0x1e9)])break;}return _0xfa7016;}function _chunkForLorebook(_0x27c070,_0x3ea684){const _0x3a7861=_0x437d66,{chunkSize:_0x5f1379,overlap:_0x5eec46}=settings[_0x3a7861(0x1fe)],{sourceName:sourceName=_0x3a7861(0x1b4)}=_0x3ea684,_0x50837c=[];if(!_0x27c070||_0x5f1379<=0x0)return _0x50837c;let _0x3cee52=0x1,_0x270d8e=0x0;while(_0x270d8e<_0x27c070[_0x3a7861(0x1e9)]){const _0xe1e079=Math[_0x3a7861(0x1c0)](_0x270d8e+_0x5f1379,_0x27c070[_0x3a7861(0x1e9)]),_0x1b7ad4=_0x27c070[_0x3a7861(0x18a)](_0x270d8e,_0xe1e079),_0x177235='[来源:\x20世界书,\x20条目:\x20'+sourceName+_0x3a7861(0x188)+_0x3cee52+_0x3a7861(0x1b9),_0x1b7c4a=getTagForSource('lorebook'),_0x2b6d7e='<'+_0x1b7c4a+'>\x0a'+_0x177235+'\x0a'+_0x1b7ad4+_0x3a7861(0x166)+_0x1b7c4a+'>';_0x50837c['push']({'text':_0x2b6d7e,'metadata':{'source':_0x3a7861(0x18e),'sourceName':sourceName,'part':_0x3cee52,'timestamp':new Date()[_0x3a7861(0x167)]()}}),_0x3cee52++,_0x270d8e+=_0x5f1379-_0x5eec46;if(_0x270d8e>=_0x27c070[_0x3a7861(0x1e9)])break;}return _0x50837c;}function _chunkForManual(_0x5a3dd0,_0x2551f9){const _0x22c4d5=_0x437d66,{chunkSize:_0x1eb54c,overlap:_0x36fea4}=settings['advanced'],{sourceName:sourceName=_0x22c4d5(0x195)}=_0x2551f9,_0xb012b9=[];if(!_0x5a3dd0||_0x1eb54c<=0x0)return _0xb012b9;const _0x49d61d=new Date(),_0x12b146=_0x49d61d['toLocaleString'](_0x22c4d5(0x192));let _0x5b0909=0x1,_0x39f5cd=0x0;while(_0x39f5cd<_0x5a3dd0[_0x22c4d5(0x1e9)]){const _0x3f0422=Math['min'](_0x39f5cd+_0x1eb54c,_0x5a3dd0[_0x22c4d5(0x1e9)]),_0x105c5b=_0x5a3dd0['substring'](_0x39f5cd,_0x3f0422),_0xa176b9=_0x22c4d5(0x175)+sourceName+',\x20向量化录入时间:\x20'+_0x12b146+_0x22c4d5(0x188)+_0x5b0909+'部分]',_0x4e3726=getTagForSource(_0x22c4d5(0x182)),_0x5e4108='<'+_0x4e3726+'>\x0a'+_0xa176b9+'\x0a'+_0x105c5b+_0x22c4d5(0x166)+_0x4e3726+'>';_0xb012b9[_0x22c4d5(0x1e0)]({'text':_0x5e4108,'metadata':{'source':_0x22c4d5(0x182),'sourceName':sourceName,'part':_0x5b0909,'timestamp':_0x49d61d[_0x22c4d5(0x167)]()}}),_0x5b0909++,_0x39f5cd+=_0x1eb54c-_0x36fea4;if(_0x39f5cd>=_0x5a3dd0[_0x22c4d5(0x1e9)])break;}return _0xb012b9;}import{getCollectionId as _0x140067,getCharacterName}from'./utils/context-utils.js';async function getCollectionId(){return lockedCollectionId||await _0x140067();}async function toggleSessionLock(){return lockedCollectionId?(lockedCollectionId=null,![]):(lockedCollectionId=await _0x140067(),!![]);}function isSessionLocked(){return lockedCollectionId!==null;}function getLockedSessionInfo(){const _0x289e98=_0x437d66;if(!lockedCollectionId)return null;return{'id':lockedCollectionId,'name':_0x289e98(0x1a3)+lockedCollectionId[_0x289e98(0x18a)](0x0,0x8)+'...)'};}function generateHash(_0x4dec58){const _0x52b0eb=_0x437d66;let _0x3ce4ec=0x0;for(let _0x2cc10f=0x0;_0x2cc10f<_0x4dec58['length'];_0x2cc10f++){const _0x3cf8e5=_0x4dec58['charCodeAt'](_0x2cc10f);_0x3ce4ec=(_0x3ce4ec<<0x5)-_0x3ce4ec+_0x3cf8e5,_0x3ce4ec=_0x3ce4ec&_0x3ce4ec;}return Math['abs'](_0x3ce4ec)[_0x52b0eb(0x1a5)](0x24);}async function queryVectors(_0x4aad25){const _0x3da208=_0x437d66;console['log'](_0x3da208(0x208));const _0x1b4201=await getCollectionId();console['log'](_0x3da208(0x1c3)+_0x1b4201);const _0x2a8cf3=(await getEmbeddings([_0x4aad25]))[0x0],_0x4880fe={'collectionId':_0x1b4201,'searchText':_0x4aad25,'topK':settings[_0x3da208(0x1fe)]['maxResults'],'threshold':settings[_0x3da208(0x1fe)][_0x3da208(0x1d9)],'source':_0x3da208(0x17c),'embeddings':{[_0x4aad25]:_0x2a8cf3}};console[_0x3da208(0x1ea)](_0x3da208(0x164),JSON[_0x3da208(0x209)](_0x4880fe,null,0x2));const _0x2a5ffb=await fetch(_0x3da208(0x181),{'method':_0x3da208(0x1a0),'headers':context[_0x3da208(0x1f3)](),'body':JSON['stringify'](_0x4880fe)});console[_0x3da208(0x1ea)](_0x3da208(0x179)+_0x2a5ffb[_0x3da208(0x177)]);if(!_0x2a5ffb['ok']){const _0x304bb3=await _0x2a5ffb[_0x3da208(0x1bb)]();console[_0x3da208(0x1cb)]('[翰林院-日志]\x20宝库查询API错误:',_0x304bb3);throw new Error(_0x3da208(0x198)+_0x2a5ffb[_0x3da208(0x177)]+':\x20'+_0x304bb3);}const _0x51e15b=await _0x2a5ffb[_0x3da208(0x1af)]();console[_0x3da208(0x1ea)](_0x3da208(0x1cf),_0x51e15b);const _0x1e2c2b=_0x51e15b['metadata']||_0x51e15b[_0x3da208(0x1ae)]||_0x51e15b['data']||[];return console[_0x3da208(0x1ea)](_0x3da208(0x1ff)+_0x1e2c2b[_0x3da208(0x1e9)]+_0x3da208(0x187)),_0x1e2c2b;}async function insertVectors(_0x5bcf8a,_0x22c267=null,_0x2076b2){const _0x24d446=_0x437d66;if(!_0x2076b2){console['error'](_0x24d446(0x1c9)),_0x2076b2=await getCollectionId();if(!_0x2076b2)throw new Error(_0x24d446(0x20a));}if(_0x5bcf8a[_0x24d446(0x1e9)]===0x0)return{'success':!![],'count':0x0};const _0x1a4e27=_0x5bcf8a[_0x24d446(0x1d5)]((_0x4fd292,_0x1b66f5)=>({'hash':generateHash(_0x4fd292['text']+Date[_0x24d446(0x1f9)]()+_0x1b66f5),'text':_0x4fd292[_0x24d446(0x1bb)],'metadata':_0x4fd292[_0x24d446(0x1e7)]||{'source':_0x24d446(0x1e1),'timestamp':new Date()[_0x24d446(0x167)]()}})),_0x264fb2=_0x1a4e27[_0x24d446(0x1cc)]((_0xed677f,_0x135f24,_0x43025d)=>{const _0x1290e4=_0x24d446;return _0xed677f[_0x135f24['text']]=_0x5bcf8a[_0x43025d][_0x1290e4(0x1f4)],_0xed677f;},{}),_0x390b5b={'collectionId':_0x2076b2,'items':_0x1a4e27,'source':_0x24d446(0x17c),'embeddings':_0x264fb2},_0x4a7544=await fetch(_0x24d446(0x1dc),{'method':'POST','headers':context['getRequestHeaders'](),'body':JSON[_0x24d446(0x209)](_0x390b5b),'signal':_0x22c267});if(!_0x4a7544['ok']){const _0x129d25=await _0x4a7544[_0x24d446(0x1bb)]();console['error']('[翰林院-日志]\x20忆识存入API错误:',_0x129d25);throw new Error(_0x24d446(0x1ba)+_0x4a7544[_0x24d446(0x177)]+':\x20'+_0x129d25);}return{'success':!![],'count':_0x1a4e27[_0x24d446(0x1e9)]};}async function getVectorCount(){const _0x244068=_0x437d66;console[_0x244068(0x1ea)](_0x244068(0x184));const _0x1b1b1b=await getCollectionId();console[_0x244068(0x1ea)](_0x244068(0x20c)+_0x1b1b1b);const _0x5ee9b8={'collectionId':_0x1b1b1b,'source':_0x244068(0x17c),'embeddings':{}};console['log']('[翰林院-日志]\x20发送到\x20/api/vector/list\x20的请求体:',JSON['stringify'](_0x5ee9b8,null,0x2));const _0x51f67b=await fetch(_0x244068(0x19d),{'method':_0x244068(0x1a0),'headers':context['getRequestHeaders'](),'body':JSON[_0x244068(0x209)](_0x5ee9b8)});console['log'](_0x244068(0x176)+_0x51f67b[_0x244068(0x177)]);if(!_0x51f67b['ok']){const _0x38cd03=await _0x51f67b[_0x244068(0x1bb)]();return console[_0x244068(0x1cb)]('[翰林院-日志]\x20获取向量列表API错误:',_0x38cd03),0x0;}const _0x19761b=await _0x51f67b[_0x244068(0x1af)]();let _0x511786=0x0;if(Array['isArray'](_0x19761b))_0x511786=_0x19761b[_0x244068(0x1e9)];else _0x19761b&&_0x19761b['hashes']&&(_0x511786=_0x19761b[_0x244068(0x172)][_0x244068(0x1e9)]);return console[_0x244068(0x1ea)](_0x244068(0x1a2)+_0x511786),_0x511786;}async function purgeStorage(_0x438bb4=null){const _0x41d159=_0x437d66;console[_0x41d159(0x1ea)]('[翰林院-日志]\x20开始清空宝库...');const _0x51071e=_0x438bb4||await getCollectionId();if(!_0x51071e)return console['error'](_0x41d159(0x173)),toastr[_0x41d159(0x1cb)](_0x41d159(0x1ef)),![];console[_0x41d159(0x1ea)]('[翰林院-日志]\x20清空目标集合ID:\x20'+_0x51071e);const _0x4a00ec={'collectionId':_0x51071e};console[_0x41d159(0x1ea)]('[翰林院-日志]\x20发送到\x20/api/vector/purge\x20的请求体:',JSON[_0x41d159(0x209)](_0x4a00ec,null,0x2));const _0x2d920e=await fetch(_0x41d159(0x203),{'method':_0x41d159(0x1a0),'headers':context['getRequestHeaders'](),'body':JSON[_0x41d159(0x209)](_0x4a00ec)});console[_0x41d159(0x1ea)]('[翰林院-日志]\x20/api/vector/purge\x20响应状态:\x20'+_0x2d920e[_0x41d159(0x177)]);if(!_0x2d920e['ok']){const _0x58b7e9=await _0x2d920e[_0x41d159(0x1bb)]();console[_0x41d159(0x1cb)](_0x41d159(0x200),_0x58b7e9);}else console[_0x41d159(0x1ea)]('[翰林院-日志]\x20清空宝库API调用成功。');return _0x2d920e['ok'];}function getMessagesForCondensation(_0x1367a7=null){const _0x1d7e1a=_0x437d66;if(!settings[_0x1d7e1a(0x1a1)][_0x1d7e1a(0x1bf)])return showNotification('凝识之权未开启',_0x1d7e1a(0x1bd)),[];const {layerStart:_0x5a0d22,layerEnd:_0x2e4de8}=settings[_0x1d7e1a(0x1a1)],_0x94c935=_0x1367a7||settings['condensation'][_0x1d7e1a(0x1d3)],_0x2e00d3=context[_0x1d7e1a(0x194)]['length'],_0x581aa3=Math[_0x1d7e1a(0x1d0)](0x0,_0x5a0d22-0x1),_0xe88aaf=_0x2e4de8===0x0||_0x2e4de8>_0x2e00d3?_0x2e00d3:Math[_0x1d7e1a(0x1c0)](_0x2e00d3,_0x2e4de8),_0x542087=context[_0x1d7e1a(0x194)][_0x1d7e1a(0x1ab)](_0x581aa3,_0xe88aaf);return _0x542087[_0x1d7e1a(0x1e2)](_0x4867ad=>{const _0x369964=_0x1d7e1a,_0x9aee95=_0x4867ad[_0x369964(0x1b5)]===!![],_0x5880eb=_0x4867ad[_0x369964(0x1b5)]===![];if(!_0x4867ad['mes']||!_0x4867ad['mes'][_0x369964(0x1d6)]())return![];return _0x94c935[_0x369964(0x20b)]&&_0x9aee95||_0x94c935['ai']&&_0x5880eb;});}async function processCondensation(_0x4b01c5,_0x3c238d=()=>{},_0x195cbd=null){const _0x1d60d4=_0x437d66;if(!_0x4b01c5||_0x4b01c5[_0x1d60d4(0x1e9)]===0x0)return{'success':![],'error':_0x1d60d4(0x1f2)};try{let _0x271e11=await getCollectionId();const _0x2b2ace=getCollectionIdInfo();if(_0x2b2ace[_0x1d60d4(0x1b2)]&&_0x2b2ace['oldId']===_0x271e11&&_0x2b2ace[_0x1d60d4(0x1b2)]!==_0x2b2ace[_0x1d60d4(0x1ca)]){const _0x43ae9f=confirm(_0x1d60d4(0x1fa));if(_0x43ae9f)_0x3c238d(_0x1d60d4(0x1a4)+_0x2b2ace[_0x1d60d4(0x1b2)],_0x1d60d4(0x1d4)),await purgeStorage(_0x2b2ace['oldId']),_0x271e11=_0x2b2ace['newId'],_0x3c238d(_0x1d60d4(0x1de)+_0x271e11,_0x1d60d4(0x1da));else return _0x3c238d('[翰林院-迁移]\x20用户取消了迁移操作。',_0x1d60d4(0x207)),toastr['info'](_0x1d60d4(0x1a9)),{'success':![],'error':_0x1d60d4(0x193)};}if(!_0x271e11)throw new Error('无法确定当前忆识宝库的ID,请确认角色已正确加载。');_0x3c238d(_0x1d60d4(0x190)+_0x271e11,_0x1d60d4(0x207));const _0x238f82=[],_0x168a47=context['chat'];for(const _0xe43397 of _0x4b01c5){const _0x590105=(_0xe43397['mes']||'')[_0x1d60d4(0x16b)](/<[^>]*>/g,'')['trim']();if(_0x590105[_0x1d60d4(0x1e9)]===0x0)continue;let _0x475fa5;if(_0xe43397[_0x1d60d4(0x183)]!==undefined&&_0xe43397['floor']!==null)_0x475fa5=_0xe43397[_0x1d60d4(0x183)];else{const _0x1ccb41=_0x168a47['findIndex'](_0x4df4f8=>_0x4df4f8===_0xe43397);_0x475fa5=_0x1ccb41!==-0x1?_0x1ccb41+0x1:-0x1;}const _0xfca5d4=new Date(_0xe43397[_0x1d60d4(0x199)]),_0x291be2=isNaN(_0xfca5d4[_0x1d60d4(0x1ee)]())?new Date()[_0x1d60d4(0x167)]():_0xfca5d4['toISOString'](),_0x512a21=splitIntoChunks(_0x590105,_0x1d60d4(0x1b7),{'floor':_0x475fa5,'is_user':_0xe43397[_0x1d60d4(0x1b5)],'timestamp':_0x291be2});_0x238f82[_0x1d60d4(0x1e0)](..._0x512a21);}if(_0x238f82[_0x1d60d4(0x1e9)]===0x0)return{'success':!![],'count':0x0};_0x3c238d(_0x1d60d4(0x1df)+_0x4b01c5[_0x1d60d4(0x1e9)]+'\x20条消息分解为\x20'+_0x238f82[_0x1d60d4(0x1e9)]+'\x20个知识块,准备入库。',_0x1d60d4(0x207));const _0x4a0f72=settings['retrieval'][_0x1d60d4(0x1d2)]||0x5;let _0x1a8c80=0x0;for(let _0x2c0fe3=0x0;_0x2c0fe3<_0x238f82[_0x1d60d4(0x1e9)];_0x2c0fe3+=_0x4a0f72){const _0x3c765a=_0x238f82[_0x1d60d4(0x1ab)](_0x2c0fe3,_0x2c0fe3+_0x4a0f72),_0x3ac725=_0x3c765a[_0x1d60d4(0x1d5)](_0x3df9bd=>_0x3df9bd[_0x1d60d4(0x1bb)]),_0x4eca13=await getEmbeddings(_0x3ac725);if(_0x3c765a[_0x1d60d4(0x1e9)]!==_0x4eca13[_0x1d60d4(0x1e9)])throw new Error(_0x1d60d4(0x197));const _0x29b1cc=_0x3c765a[_0x1d60d4(0x1d5)]((_0x29bf3a,_0x1417e4)=>({..._0x29bf3a,'vector':_0x4eca13[_0x1417e4]}));await insertVectors(_0x29b1cc,null,_0x271e11),_0x1a8c80+=_0x3c765a[_0x1d60d4(0x1e9)];}if(_0x195cbd){const _0xd1bf40=_0x195cbd[_0x1d60d4(0x1bc)]===0x0?context['chat'][_0x1d60d4(0x1e9)]:_0x195cbd[_0x1d60d4(0x1bc)];settings['condensationHistory'][_0x271e11]={'start':_0x195cbd[_0x1d60d4(0x206)],'end':_0xd1bf40,'timestamp':new Date()[_0x1d60d4(0x167)]()},saveSettings(),_0x3c238d(_0x1d60d4(0x180)+_0x271e11+_0x1d60d4(0x165)+_0x195cbd[_0x1d60d4(0x206)]+'-'+_0xd1bf40,'info');}_0x3c238d(_0x1d60d4(0x19c)+_0x1a8c80+_0x1d60d4(0x163),_0x1d60d4(0x1da));const _0x1cb452=_0x4b01c5[_0x1d60d4(0x1d5)](_0x212011=>{const _0x4f759f=_0x1d60d4,_0x1f7bdd=_0x168a47[_0x4f759f(0x1d8)](_0x2ad05f=>_0x2ad05f===_0x212011),_0x589336=_0x1f7bdd!==-0x1?_0x1f7bdd+0x1:-0x1,_0x15289a=_0x212011[_0x4f759f(0x1b5)]?'用户':getCharacterName()||'AI';return'['+_0x15289a+_0x4f759f(0x16f)+_0x589336+_0x4f759f(0x16d);});return{'success':!![],'count':_0x1a8c80,'messages':_0x1cb452};}catch(_0x3646b3){return console['error'](_0x1d60d4(0x1b0),_0x3646b3),_0x3c238d(_0x1d60d4(0x1f7)+_0x3646b3[_0x1d60d4(0x16a)],'error'),{'success':![],'error':_0x3646b3['message']};}}async function rerankResults(_0x2e28a7,_0x17ecba,_0x2b9df7){const _0x2339bc=_0x437d66;let _0x33c90c=_0x2e28a7;if(_0x2b9df7[_0x2339bc(0x1f6)][_0x2339bc(0x1bf)]&&_0x2e28a7[_0x2339bc(0x1e9)]>0x0){console[_0x2339bc(0x1ea)](_0x2339bc(0x205));try{const _0x4c234b=_0x2e28a7[_0x2339bc(0x1d5)](_0x2f1df4=>_0x2f1df4[_0x2339bc(0x1bb)]),_0x264cd9=await executeRerank(_0x17ecba,_0x4c234b,_0x2b9df7[_0x2339bc(0x1f6)]),_0x50c8ad=_0x2e28a7[_0x2339bc(0x1d5)]((_0x2efbe2,_0x4ca9cd)=>({..._0x2efbe2,'original_index':_0x4ca9cd}));_0x33c90c=_0x50c8ad[_0x2339bc(0x1d5)](_0x3db867=>{const _0x2a7a2f=_0x2339bc,_0xa0ea20=_0x264cd9['results'][_0x2a7a2f(0x1c1)](_0x5cc14d=>_0x5cc14d[_0x2a7a2f(0x1e8)]===_0x3db867['original_index']),_0x25c732=_0xa0ea20?_0xa0ea20[_0x2a7a2f(0x20f)]:0x0;return{..._0x3db867,'rerank_score':_0x25c732};});if(_0x2b9df7['rerank'][_0x2339bc(0x1cd)])showNotification(_0x2339bc(0x171),_0x2339bc(0x1da));}catch(_0x2a9c41){console['error'](_0x2339bc(0x1c5),_0x2a9c41);if(_0x2b9df7['rerank']['notify'])showNotification('Rerank失败:\x20'+_0x2a9c41[_0x2339bc(0x16a)],_0x2339bc(0x1cb));_0x33c90c[_0x2339bc(0x18c)](_0xcf5f99=>_0xcf5f99[_0x2339bc(0x1c6)]=0x0);}}else _0x33c90c[_0x2339bc(0x18c)](_0x4157e8=>_0x4157e8[_0x2339bc(0x1c6)]=0x0);console['log']('[翰林院-Rerank]\x20开始元数据加权最终排序...');const _0x1e0d1f=context[_0x2339bc(0x194)][_0x2339bc(0x1e9)],_0x1522d7=_0x2b9df7[_0x2339bc(0x1f6)]['hybrid_alpha'],_0x3d2d7c=_0x33c90c[_0x2339bc(0x1d5)](_0x33627a=>{const _0x3f292f=_0x2339bc;let _0x3ff3a1=0x1;const _0x80911c=_0x33627a[_0x3f292f(0x1e7)]||{};switch(_0x80911c[_0x3f292f(0x191)]){case _0x3f292f(0x18e):_0x3ff3a1*=1.2;break;case _0x3f292f(0x182):_0x3ff3a1*=1.1;break;case _0x3f292f(0x1b7):if(_0x80911c[_0x3f292f(0x183)]&&_0x1e0d1f>0x0){const _0x500e54=_0x80911c[_0x3f292f(0x183)]/_0x1e0d1f;_0x3ff3a1*=0x1+_0x500e54;}break;}const _0xf1abfe=_0x33627a[_0x3f292f(0x1c6)]*_0x1522d7+(_0x33627a['score']||0x0)*(0x1-_0x1522d7),_0x2b639c=_0xf1abfe*_0x3ff3a1;return{..._0x33627a,'final_score':_0x2b639c};});return _0x3d2d7c[_0x2339bc(0x1f0)]((_0x5653ff,_0x4f5b34)=>(_0x4f5b34[_0x2339bc(0x1fc)]||0x0)-(_0x5653ff[_0x2339bc(0x1fc)]||0x0)),console[_0x2339bc(0x1ea)]('[翰林院-Rerank]\x20元数据加权排序完成。'),_0x3d2d7c['slice'](0x0,_0x2b9df7[_0x2339bc(0x1f6)][_0x2339bc(0x1fd)]);}async function rearrangeChat(_0x1a6672,_0x3b982f,_0x467676,_0xab4e48){const _0x35ff34=_0x437d66;setExtensionPrompt(_0x35ff34(0x19f),'',settings[_0x35ff34(0x18b)][_0x35ff34(0x1c2)],settings[_0x35ff34(0x18b)]['depth'],![],settings[_0x35ff34(0x18b)]['depth_role']);if(_0xab4e48==='quiet'||!settings[_0x35ff34(0x1ce)][_0x35ff34(0x1bf)])return;const _0x376243=_0x1a6672[_0x35ff34(0x1ab)](-settings[_0x35ff34(0x1fe)][_0x35ff34(0x1be)]);if(_0x376243[_0x35ff34(0x1e9)]===0x0)return;const _0x58091d=_0x376243['map'](_0x5a3de5=>_0x5a3de5[_0x35ff34(0x19b)])[_0x35ff34(0x17d)]('\x20')['replace'](/<[^>]*>/g,'')['trim']();if(!_0x58091d)return;try{const _0x527b29=await queryVectors(_0x58091d);if(_0x527b29[_0x35ff34(0x1e9)]===0x0)return;const _0x37ad10=await rerankResults(_0x527b29,_0x58091d,settings);if(_0x37ad10[_0x35ff34(0x1e9)]===0x0)return;const _0x1c3c3a=_0x37ad10[_0x35ff34(0x1d5)](_0x4e7d9f=>_0x4e7d9f[_0x35ff34(0x1bb)])[_0x35ff34(0x17d)]('\x0a\x0a');let _0xbe983f=settings[_0x35ff34(0x18b)][_0x35ff34(0x18f)]['replace'](_0x35ff34(0x1e4),_0x1c3c3a);_0xbe983f[_0x35ff34(0x1d6)]()&&(_0xbe983f=_0x35ff34(0x1eb)+_0xbe983f),setExtensionPrompt(_0x35ff34(0x19f),_0xbe983f,settings[_0x35ff34(0x18b)][_0x35ff34(0x1c2)],settings[_0x35ff34(0x18b)]['depth'],![],settings[_0x35ff34(0x18b)]['depth_role']);}catch(_0x25a475){console[_0x35ff34(0x1cb)](_0x35ff34(0x19e),_0x25a475);if(settings[_0x35ff34(0x1ce)][_0x35ff34(0x1cd)])showNotification('忆识检索失败:\x20'+_0x25a475[_0x35ff34(0x16a)],_0x35ff34(0x1cb));}}
|