mirror of
https://github.com/Wx-2025/ST-Amily2-Chat-Optimisation.git
synced 2026-06-06 19:45:50 +00:00
2 lines
37 KiB
JavaScript
2 lines
37 KiB
JavaScript
'use strict';const _0x177034=_0x1ae4;(function(_0x4827f8,_0x21b5b0){const _0x33fa21=_0x1ae4,_0x2f5c05=_0x4827f8();while(!![]){try{const _0x54da21=parseInt(_0x33fa21(0xdc))/0x1+-parseInt(_0x33fa21(0x15d))/0x2*(parseInt(_0x33fa21(0x12b))/0x3)+-parseInt(_0x33fa21(0xcf))/0x4+parseInt(_0x33fa21(0xdb))/0x5*(-parseInt(_0x33fa21(0x145))/0x6)+-parseInt(_0x33fa21(0x177))/0x7+parseInt(_0x33fa21(0xd7))/0x8*(parseInt(_0x33fa21(0x122))/0x9)+parseInt(_0x33fa21(0x144))/0xa;if(_0x54da21===_0x21b5b0)break;else _0x2f5c05['push'](_0x2f5c05['shift']());}catch(_0x482c17){_0x2f5c05['push'](_0x2f5c05['shift']());}}}(_0x4888,0x9e406));import{extension_prompt_roles,setExtensionPrompt}from'/script.js';import*as _0x231504 from'./utils/context-utils.js';import{getCollectionIdInfo,getCharacterId,getCharacterStableId}from'./utils/context-utils.js';import{defaultSettings as _0x2ac380}from'./rag-settings.js';import*as _0x1aa8ce from'./ingestion-manager.js';import{getEmbeddings,fetchEmbeddingModels as _0x73f638,fetchRerankModels as _0x1976e4,executeRerank,testApiConnection as _0x4b51a3}from'./rag-api.js';const MODULE_NAME=_0x177034(0xea),OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME='vectors_rearrangeChat';let context=null,settings=null,lockedCollectionId=null;export{initialize,getSettings,saveSettings,resetSettings,_0x4b51a3 as testApiConnection,_0x73f638 as fetchEmbeddingModels,_0x1976e4 as fetchRerankModels,getVectorCount,purgeStorage,getMessagesForCondensation,processCondensation,ingestTextToHanlinyuan,getCollectionId,toggleSessionLock,isSessionLocked,getLockedSessionInfo,addKnowledgeBase,removeKnowledgeBase,getKnowledgeBases,toggleKnowledgeBase};function initialize(){const _0x17d8af=_0x177034;context=SillyTavern[_0x17d8af(0x163)]();if(!context){console[_0x17d8af(0xd2)](_0x17d8af(0x131));return;}settings=getSettings(),!window[_0x17d8af(0x102)]&&(window[_0x17d8af(0x102)]={}),window[_0x17d8af(0x102)]['rearrangeChat']=rearrangeChat,window['hanlinyuanRagProcessor']['initialized']=!![],console[_0x17d8af(0xc7)](_0x17d8af(0xdd));}async function ingestTextToHanlinyuan(_0xa96460,_0x1b55fa=_0x177034(0x15c),_0x37026a={},_0x2b4095=()=>{},_0x3cc889=null,_0x25b4c5=()=>{},_0x213cf6=()=>{},_0x131981=null,_0x1936ff=0x0){const _0x486915=_0x177034;if(!_0xa96460||!_0xa96460[_0x486915(0x159)]())return{'success':![],'error':_0x486915(0x154)};if(!settings)return{'success':![],'error':'核心未初始化'};try{const _0x3d9ecb=getCollectionIdInfo(),_0xf1807a=await _0x24c28a();if(_0x3d9ecb[_0x486915(0xfb)]&&_0x3d9ecb[_0x486915(0xfb)]===_0xf1807a&&_0x3d9ecb[_0x486915(0xfb)]!==_0x3d9ecb[_0x486915(0x173)]){const _0x17b1a7=confirm(_0x486915(0x16a));if(_0x17b1a7)_0x25b4c5(_0x486915(0x119)+_0x3d9ecb[_0x486915(0xfb)],_0x486915(0x186)),await purgeStorage(_0x3d9ecb[_0x486915(0xfb)]),_0x25b4c5(_0x486915(0xe7),_0x486915(0x13a));else return _0x25b4c5('[翰林院-迁移]\x20用户取消了迁移操作。',_0x486915(0xe8)),toastr[_0x486915(0xe8)](_0x486915(0x187)),{'success':![],'error':_0x486915(0x107)};}let _0x2a8a2b,_0x18131a;const _0x48c0f1=new Date()[_0x486915(0x148)](_0x486915(0x19b),{'hour12':![]}),_0x50bf5e=getCharacterName()||'未知角色';switch(_0x1b55fa){case _0x486915(0xe0):const _0x39e332=_0x37026a[_0x486915(0xee)]||{},_0x2a409f=_0x39e332[_0x486915(0x179)]??'?',_0x5e25b1=_0x39e332[_0x486915(0x15a)]===0x0?'末':_0x39e332[_0x486915(0x15a)]??'?';_0x2a8a2b=_0x50bf5e+':\x20'+_0x2a409f+'楼-'+_0x5e25b1+'楼';break;case _0x486915(0x121):const _0x3d2a4d=_0x37026a[_0x486915(0x10f)]||_0x486915(0x11a),_0x322695=_0x37026a[_0x486915(0x109)]||_0x486915(0x103);_0x2a8a2b=_0x3d2a4d+':\x20'+_0x322695;break;case _0x486915(0x120):_0x2a8a2b=_0x486915(0x188)+(_0x37026a[_0x486915(0xf1)]||'未知小说');break;case _0x486915(0x15c):default:_0x2a8a2b='手动录入:\x20'+_0x48c0f1;break;}const _0x3cdcfd=Object['values'](getKnowledgeBases()),_0x2420e7=_0x3cdcfd[_0x486915(0xf8)](_0x23e2ea=>_0x23e2ea[_0x486915(0xd4)]===_0x2a8a2b);if(_0x2420e7)_0x18131a=_0x2420e7['id'],_0x25b4c5('[翰林院-核心]\x20检测到同名知识库\x20\x22'+_0x2a8a2b+_0x486915(0x170),_0x486915(0xe8));else{_0x25b4c5(_0x486915(0xef)+_0x2a8a2b+_0x486915(0x165),'info');const _0x4c346a=addKnowledgeBase(_0x2a8a2b);_0x18131a=_0x4c346a['id'];}const _0x3a0053=getCharacterStableId(),_0x1cb555=_0x3a0053+'_'+_0x18131a;_0x25b4c5(_0x486915(0x19f)+_0x2a8a2b+_0x486915(0x184)+_0x1cb555+')',_0x486915(0x13a)),_0x25b4c5(_0x486915(0xfe)+_0x1cb555,'info'),_0x2b4095({'message':'正在智能分块...','processed':0x0,'total':0x1});const _0x3961bc=splitIntoChunks(_0xa96460,_0x1b55fa,_0x37026a),_0x107f1b=_0x3961bc[_0x486915(0x18a)];if(_0x3cc889?.[_0x486915(0x125)])throw new Error(_0x486915(0x17a));_0x25b4c5(_0x486915(0x182)+_0x2a8a2b+_0x486915(0xd9)+_0x107f1b+_0x486915(0xfc),'info');if(_0x107f1b===0x0)return{'success':!![],'count':0x0};const _0x395f31=settings[_0x486915(0x113)][_0x486915(0x106)]||0x5;let _0x392548=_0x1936ff;for(let _0x56fd7d=_0x1936ff;_0x56fd7d<_0x107f1b;_0x56fd7d+=_0x395f31){if(_0x3cc889?.['aborted'])throw new Error(_0x486915(0x17a));const _0x3ca5d3=_0x3961bc['slice'](_0x56fd7d,_0x56fd7d+_0x395f31);_0x2b4095({'message':_0x486915(0xe2)+(_0x56fd7d+0x1)+'-'+(_0x56fd7d+_0x3ca5d3[_0x486915(0x18a)])+'\x20块','processed':_0x56fd7d,'total':_0x107f1b});const _0x5dcef2=_0x3ca5d3[_0x486915(0x12a)](_0x168473=>_0x168473[_0x486915(0xf2)]),_0x2aff06=await getEmbeddings(_0x5dcef2,_0x3cc889);if(_0x3cc889?.[_0x486915(0x125)])throw new Error(_0x486915(0x17a));if(_0x3ca5d3[_0x486915(0x18a)]!==_0x2aff06[_0x486915(0x18a)])throw new Error('文本块和向量数量不匹配');const _0x44b6fb=_0x3ca5d3[_0x486915(0x12a)]((_0x4e15c1,_0x11cf1d)=>({..._0x4e15c1,'vector':_0x2aff06[_0x11cf1d]}));await insertVectors(_0x44b6fb,_0x3cc889,_0x1cb555),_0x392548+=_0x3ca5d3[_0x486915(0x18a)],_0x131981&&_0x1aa8ce[_0x486915(0x118)](_0x131981,_0x392548,_0x107f1b),await _0x213cf6();}return _0x131981&&_0x1aa8ce[_0x486915(0x16b)](_0x131981),_0x25b4c5(_0x486915(0x101)+_0x392548+_0x486915(0x13f),_0x486915(0x13a)),{'success':!![],'count':_0x392548};}catch(_0x1670e5){if(_0x1670e5[_0x486915(0xd4)]===_0x486915(0x17a)){_0x25b4c5(_0x486915(0x10b),_0x486915(0x186));throw _0x1670e5;}return console[_0x486915(0xd2)](_0x486915(0x112),_0x1670e5),_0x25b4c5(_0x486915(0x1a1)+_0x1670e5[_0x486915(0x164)],_0x486915(0xd2)),{'success':![],'error':_0x1670e5[_0x486915(0x164)]};}}function getSettings(){const _0x3fc22c=_0x177034;if(!context||!context['extensionSettings'])return structuredClone(_0x2ac380);let _0x54edf8=context['extensionSettings'][MODULE_NAME];!_0x54edf8&&(_0x54edf8={},context[_0x3fc22c(0x141)][MODULE_NAME]=_0x54edf8);_0x54edf8[_0x3fc22c(0xfa)]===undefined&&(_0x54edf8[_0x3fc22c(0xfa)]={});_0x54edf8[_0x3fc22c(0x157)]===undefined&&(_0x54edf8[_0x3fc22c(0x157)]={});for(const _0x6a982e in _0x2ac380){if(_0x54edf8[_0x6a982e]===undefined)_0x54edf8[_0x6a982e]=structuredClone(_0x2ac380[_0x6a982e]);else{if(typeof _0x2ac380[_0x6a982e]===_0x3fc22c(0x15b)&&!Array[_0x3fc22c(0x19a)](_0x2ac380[_0x6a982e])&&_0x2ac380[_0x6a982e]!==null)for(const _0x1b3ce6 in _0x2ac380[_0x6a982e]){_0x54edf8[_0x6a982e][_0x1b3ce6]===undefined&&(_0x54edf8[_0x6a982e][_0x1b3ce6]=_0x2ac380[_0x6a982e][_0x1b3ce6]);}}}return _0x54edf8;}function saveSettings(){const _0x174ae2=_0x177034;if(context)context[_0x174ae2(0xe4)]();}function resetSettings(){context&&(context['extensionSettings'][MODULE_NAME]=structuredClone(_0x2ac380),saveSettings());}function showNotification(_0x4a86fc,_0x5bcdac=_0x177034(0xe8)){toastr[_0x5bcdac](_0x4a86fc);}function getTagForSource(_0x538ec){const _0x18f60c=_0x177034;switch(_0x538ec){case _0x18f60c(0xe0):return _0x18f60c(0x199);case _0x18f60c(0x121):return _0x18f60c(0x135);case'manual':return _0x18f60c(0x18e);case'novel':return'小说录入';default:return'资料';}}function splitIntoChunks(_0x288d1a,_0x3c545c,_0x575860={}){const _0x1e7027=_0x177034;switch(_0x3c545c){case _0x1e7027(0x120):return _chunkForNovel(_0x288d1a,_0x575860);case'chat_history':return _chunkForChatHistory(_0x288d1a,_0x575860);case _0x1e7027(0x121):return _chunkForLorebook(_0x288d1a,_0x575860);case _0x1e7027(0x15c):return _chunkForManual(_0x288d1a,_0x575860);default:console[_0x1e7027(0x186)]('[翰林院-分块]\x20未知的来源类型\x20\x27'+_0x3c545c+_0x1e7027(0x100));return _chunkForManual(_0x288d1a,{..._0x575860,'sourceName':_0x575860[_0x1e7027(0xf1)]||'未知来源'});}}function _chunkForNovel(_0x2f59cb,_0x280eee){const _0x30b479=_0x177034,{chunkSize:_0x259ab8,overlap:_0x3de6c2}=settings[_0x30b479(0x19d)],{sourceName:sourceName='小说'}=_0x280eee,_0x25ed7f=[];if(!_0x2f59cb||_0x259ab8<=0x0)return _0x25ed7f;const _0xac38db=/(第\s*[一二三四五六七八九十百千万零\d]+\s*卷)/gim,_0x2874da=/(第\s*[一二三四五六七八九十百千万零\d]+\s*[章回节部])|^(Chapter\s+\d+)/gim;let _0xcbca17=0x0;const _0x5057e6=_0x2f59cb[_0x30b479(0xd1)]('\x0a');let _0x5a30a5=_0x30b479(0x189),_0x1120ff='第1章',_0x3218fd=[];function _0x4127df(){const _0x2f1d6b=_0x30b479;if(_0x3218fd[_0x2f1d6b(0x18a)]===0x0)return;const _0x43e2c9=_0x3218fd[_0x2f1d6b(0x142)]('\x0a');let _0x465e54=0x0,_0x17fd49=0x1;while(_0x465e54<_0x43e2c9[_0x2f1d6b(0x18a)]){const _0x2fb012=Math[_0x2f1d6b(0x17e)](_0x465e54+_0x259ab8,_0x43e2c9[_0x2f1d6b(0x18a)]),_0xf503aa=_0x43e2c9[_0x2f1d6b(0x152)](_0x465e54,_0x2fb012);if(_0xf503aa['trim']()[_0x2f1d6b(0x18a)]>0x0){const _0x539020={'source':_0x2f1d6b(0x120),'sourceName':sourceName,'timestamp':new Date()[_0x2f1d6b(0x158)](),'globalIndex':_0xcbca17++,'volume':_0x5a30a5,'chapter':_0x1120ff,'section':_0x17fd49},_0x5b7caf=getTagForSource('novel'),_0x5f2fa6='[来源:\x20'+sourceName+',\x20'+_0x5a30a5+',\x20'+_0x1120ff+',\x20第'+_0x17fd49+'节]',_0x5404a2='<'+_0x5b7caf+'>\x0a'+_0x5f2fa6+'\x0a'+_0xf503aa+_0x2f1d6b(0xde)+_0x5b7caf+'>';_0x25ed7f[_0x2f1d6b(0x104)]({'text':_0x5404a2,'metadata':_0x539020}),_0x17fd49++;}_0x465e54+=_0x259ab8-_0x3de6c2;if(_0x465e54>=_0x43e2c9[_0x2f1d6b(0x18a)])break;}_0x3218fd=[];}for(const _0x549ddb of _0x5057e6){const _0x363073=_0x549ddb[_0x30b479(0x159)]();if(_0xac38db['test'](_0x363073))_0x4127df(),_0x5a30a5=_0x363073,_0x1120ff=_0x30b479(0x14c);else _0x2874da[_0x30b479(0x160)](_0x363073)?(_0x4127df(),_0x1120ff=_0x363073):_0x3218fd[_0x30b479(0x104)](_0x549ddb);}_0x4127df();if(_0x25ed7f[_0x30b479(0x18a)]===0x0&&_0x2f59cb[_0x30b479(0x18a)]>0x0){let _0x2fb14b=0x0,_0xb8e8c3=0x1;while(_0x2fb14b<_0x2f59cb[_0x30b479(0x18a)]){const _0x3492ff=Math[_0x30b479(0x17e)](_0x2fb14b+_0x259ab8,_0x2f59cb[_0x30b479(0x18a)]),_0x15841f=_0x2f59cb[_0x30b479(0x152)](_0x2fb14b,_0x3492ff),_0x171ac2={'source':'novel','sourceName':sourceName,'timestamp':new Date()[_0x30b479(0x158)](),'globalIndex':_0x25ed7f['length'],'volume':_0x30b479(0x189),'chapter':_0x30b479(0x14c),'section':_0xb8e8c3},_0x41581f=getTagForSource('novel'),_0x2a59dd='[来源:\x20'+sourceName+_0x30b479(0x171)+_0xb8e8c3+'节]',_0x186c36='<'+_0x41581f+'>\x0a'+_0x2a59dd+'\x0a'+_0x15841f+_0x30b479(0xde)+_0x41581f+'>';_0x25ed7f[_0x30b479(0x104)]({'text':_0x186c36,'metadata':_0x171ac2}),_0xb8e8c3++,_0x2fb14b+=_0x259ab8-_0x3de6c2;}}return _0x25ed7f;}function _chunkForChatHistory(_0x5c34cf,_0x32a2ed){const _0xd3ecd0=_0x177034,{chunkSize:_0x2f0648,overlap:_0xb0afd8}=settings[_0xd3ecd0(0x19d)],{floor:_0x424bf0,is_user:_0x52f864,timestamp:_0x5c6a1e}=_0x32a2ed,_0x31168f=[];if(!_0x5c34cf||_0x2f0648<=0x0)return _0x31168f;let _0x26cb5e=0x1,_0xa1b582=0x0;while(_0xa1b582<_0x5c34cf[_0xd3ecd0(0x18a)]){const _0xdaacb3=Math['min'](_0xa1b582+_0x2f0648,_0x5c34cf['length']),_0x252076=_0x5c34cf[_0xd3ecd0(0x152)](_0xa1b582,_0xdaacb3),_0x294139=_0xd3ecd0(0x18b)+_0x424bf0+_0xd3ecd0(0x13e)+_0x26cb5e+'部分]',_0x492b86=getTagForSource(_0xd3ecd0(0xe0)),_0x1c440c='<'+_0x492b86+'>\x0a'+_0x294139+'\x0a'+_0x252076+_0xd3ecd0(0xde)+_0x492b86+'>';_0x31168f['push']({'text':_0x1c440c,'metadata':{'source':_0xd3ecd0(0xe0),'sourceName':'聊天记录\x20#'+_0x424bf0,'floor':_0x424bf0,'part':_0x26cb5e,'is_user':_0x52f864,'timestamp':_0x5c6a1e}}),_0x26cb5e++,_0xa1b582+=_0x2f0648-_0xb0afd8;if(_0xa1b582>=_0x5c34cf[_0xd3ecd0(0x18a)])break;}return _0x31168f;}function _0x1ae4(_0x1b1db8,_0xe47df9){const _0x4888c9=_0x4888();return _0x1ae4=function(_0x1ae4d7,_0x163d9a){_0x1ae4d7=_0x1ae4d7-0xc4;let _0x2a484f=_0x4888c9[_0x1ae4d7];return _0x2a484f;},_0x1ae4(_0x1b1db8,_0xe47df9);}function _chunkForLorebook(_0x50fd01,_0x1380db){const _0x5f343c=_0x177034,{chunkSize:_0xaa7cab,overlap:_0xd0cb0b}=settings[_0x5f343c(0x19d)],{sourceName:sourceName=_0x5f343c(0xd0)}=_0x1380db,_0x59ac92=[];if(!_0x50fd01||_0xaa7cab<=0x0)return _0x59ac92;let _0x1e9ae3=0x1,_0x12b4b5=0x0;while(_0x12b4b5<_0x50fd01['length']){const _0x217e68=Math[_0x5f343c(0x17e)](_0x12b4b5+_0xaa7cab,_0x50fd01[_0x5f343c(0x18a)]),_0x15ecc8=_0x50fd01[_0x5f343c(0x152)](_0x12b4b5,_0x217e68),_0x25be5f=_0x5f343c(0xfd)+sourceName+_0x5f343c(0x13e)+_0x1e9ae3+_0x5f343c(0xca),_0x44d275=getTagForSource(_0x5f343c(0x121)),_0x4a1a1f='<'+_0x44d275+'>\x0a'+_0x25be5f+'\x0a'+_0x15ecc8+_0x5f343c(0xde)+_0x44d275+'>';_0x59ac92['push']({'text':_0x4a1a1f,'metadata':{'source':_0x5f343c(0x121),'sourceName':sourceName,'part':_0x1e9ae3,'timestamp':new Date()[_0x5f343c(0x158)]()}}),_0x1e9ae3++,_0x12b4b5+=_0xaa7cab-_0xd0cb0b;if(_0x12b4b5>=_0x50fd01[_0x5f343c(0x18a)])break;}return _0x59ac92;}function _chunkForManual(_0x487b44,_0x1ef584){const _0x1d5865=_0x177034,{chunkSize:_0x14f8c3,overlap:_0x278e59}=settings[_0x1d5865(0x19d)],{sourceName:sourceName='手动录入'}=_0x1ef584,_0x399619=[];if(!_0x487b44||_0x14f8c3<=0x0)return _0x399619;const _0x5b82e6=new Date(),_0x38fa5c=_0x5b82e6[_0x1d5865(0x148)](_0x1d5865(0x19b));let _0x51249b=0x1,_0x3d7964=0x0;while(_0x3d7964<_0x487b44[_0x1d5865(0x18a)]){const _0x2bca43=Math[_0x1d5865(0x17e)](_0x3d7964+_0x14f8c3,_0x487b44[_0x1d5865(0x18a)]),_0x30e585=_0x487b44[_0x1d5865(0x152)](_0x3d7964,_0x2bca43),_0x5ad08a=_0x1d5865(0x151)+sourceName+_0x1d5865(0x16d)+_0x38fa5c+',\x20第'+_0x51249b+_0x1d5865(0xca),_0x4833e3=getTagForSource(_0x1d5865(0x15c)),_0x576305='<'+_0x4833e3+'>\x0a'+_0x5ad08a+'\x0a'+_0x30e585+_0x1d5865(0xde)+_0x4833e3+'>';_0x399619[_0x1d5865(0x104)]({'text':_0x576305,'metadata':{'source':_0x1d5865(0x15c),'sourceName':sourceName,'part':_0x51249b,'timestamp':_0x5b82e6[_0x1d5865(0x158)]()}}),_0x51249b++,_0x3d7964+=_0x14f8c3-_0x278e59;if(_0x3d7964>=_0x487b44['length'])break;}return _0x399619;}import{getCollectionId as _0x24c28a,getCharacterName}from'./utils/context-utils.js';async function getCollectionId(){return lockedCollectionId||await _0x24c28a();}async function toggleSessionLock(){return lockedCollectionId?(lockedCollectionId=null,![]):(lockedCollectionId=await _0x24c28a(),!![]);}function isSessionLocked(){return lockedCollectionId!==null;}function getLockedSessionInfo(){const _0x2b72bf=_0x177034;if(!lockedCollectionId)return null;return{'id':lockedCollectionId,'name':'(已锁定:\x20'+lockedCollectionId[_0x2b72bf(0x152)](0x0,0x8)+_0x2b72bf(0x147)};}function getKnowledgeBases(){const _0x6c58ad=_0x177034,_0x214303=getCharacterStableId();return!settings['knowledgeBases'][_0x214303]&&(settings[_0x6c58ad(0x157)][_0x214303]={}),settings[_0x6c58ad(0x157)][_0x214303];}function addKnowledgeBase(_0x23a4d1){const _0x3b70b7=_0x177034;if(!_0x23a4d1||!_0x23a4d1[_0x3b70b7(0x159)]())throw new Error(_0x3b70b7(0x17d));const _0x1b5e2d=getCharacterStableId(),_0x346078=getKnowledgeBases(),_0x306535=_0x3b70b7(0x111)+Date['now']()+'_'+Math['random']()[_0x3b70b7(0xc4)](0x24)['substring'](0x2,0x9),_0x3e4290={'id':_0x306535,'name':_0x23a4d1[_0x3b70b7(0x159)](),'enabled':!![],'createdAt':new Date()[_0x3b70b7(0x158)]()};return _0x346078[_0x306535]=_0x3e4290,saveSettings(),console[_0x3b70b7(0xc7)](_0x3b70b7(0x13b)+_0x1b5e2d+_0x3b70b7(0xc6)+_0x23a4d1+_0x3b70b7(0x117)+_0x306535+')'),_0x3e4290;}async function removeKnowledgeBase(_0x396c2c){const _0x979a75=_0x177034,_0xf54c8a=getCharacterStableId(),_0x48b749=getKnowledgeBases(),_0x25e128=_0x48b749[_0x396c2c]?.['name']||_0x396c2c;if(!_0x48b749[_0x396c2c]){console[_0x979a75(0x186)](_0x979a75(0x18f)+_0x396c2c);return;}const _0x10d5d0=_0xf54c8a+'_'+_0x396c2c;console[_0x979a75(0xc7)](_0x979a75(0x153)+_0x396c2c+_0x979a75(0x156)+_0x10d5d0);const _0x8dd429=await purgeStorage(_0x10d5d0);_0x8dd429?(delete _0x48b749[_0x396c2c],saveSettings(),console[_0x979a75(0xc7)]('[翰林院-核心]\x20成功删除知识库\x20'+_0x396c2c+'\x20及其向量数据。'),toastr['success'](_0x979a75(0xd8)+_0x25e128+_0x979a75(0x16c))):(console[_0x979a75(0xd2)]('[翰林院-核心]\x20清空向量集合\x20'+_0x10d5d0+_0x979a75(0x110)),toastr[_0x979a75(0xd2)](_0x979a75(0xd6)));}function toggleKnowledgeBase(_0xb4d84f){const _0x34099a=_0x177034,_0x23ea1f=getKnowledgeBases();_0x23ea1f[_0xb4d84f]&&(_0x23ea1f[_0xb4d84f]['enabled']=!_0x23ea1f[_0xb4d84f][_0x34099a(0x129)],saveSettings(),console[_0x34099a(0xc7)](_0x34099a(0xcb)+_0xb4d84f+_0x34099a(0x132)+(_0x23ea1f[_0xb4d84f]['enabled']?'启用':'禁用')));}function generateHash(_0x4f4097){const _0x477797=_0x177034;let _0x12669e=0x0;for(let _0x3fead5=0x0;_0x3fead5<_0x4f4097['length'];_0x3fead5++){const _0x2663bf=_0x4f4097['charCodeAt'](_0x3fead5);_0x12669e=(_0x12669e<<0x5)-_0x12669e+_0x2663bf,_0x12669e=_0x12669e&_0x12669e;}return Math[_0x477797(0x11e)](_0x12669e)[_0x477797(0xc4)](0x24);}async function queryVectors(_0xca17e4){const _0x5e42a2=_0x177034;console[_0x5e42a2(0xc7)](_0x5e42a2(0x150));const _0x488a12=getCharacterStableId(),_0x229f5c=getKnowledgeBases(),_0x2926d9=Object[_0x5e42a2(0xf5)](_0x229f5c)[_0x5e42a2(0x11d)](_0x28aae8=>_0x28aae8['enabled']);if(_0x2926d9[_0x5e42a2(0x18a)]===0x0){console[_0x5e42a2(0xc7)](_0x5e42a2(0xf4));const _0x2bc4e8=await _0x24c28a();if(!_0x2bc4e8)return[];_0x2926d9[_0x5e42a2(0x104)]({'id':null,'name':_0x5e42a2(0x14f)});}const _0x16726c=(await getEmbeddings([_0xca17e4]))[0x0];let _0x58b10b=[];const _0x3c5f7f=_0x2926d9[_0x5e42a2(0x12a)](_0xc894e6=>{const _0x2f60a6=_0x5e42a2,_0x42d0d0=_0xc894e6['id']===null?_0x24c28a():Promise[_0x2f60a6(0xe5)](_0x488a12+'_'+_0xc894e6['id']);return _0x42d0d0[_0x2f60a6(0x198)](_0x15594c=>{const _0x46f2d2=_0x2f60a6;if(!_0x15594c)return[];console[_0x46f2d2(0xc7)](_0x46f2d2(0x11f)+_0xc894e6[_0x46f2d2(0xd4)]+_0x46f2d2(0x117)+_0x15594c+')');const _0x26d363={'collectionId':_0x15594c,'searchText':_0xca17e4,'topK':settings[_0x46f2d2(0x19d)][_0x46f2d2(0x168)],'threshold':settings['advanced'][_0x46f2d2(0x196)],'source':_0x46f2d2(0x190),'embeddings':{[_0xca17e4]:_0x16726c}};return fetch('/api/vector/query',{'method':'POST','headers':context[_0x46f2d2(0xda)](),'body':JSON['stringify'](_0x26d363)})[_0x46f2d2(0x198)](async _0x19268b=>{const _0x3fdf5c=_0x46f2d2;if(!_0x19268b['ok']){const _0x1d15e3=await _0x19268b[_0x3fdf5c(0xf2)]();return console[_0x3fdf5c(0xd2)](_0x3fdf5c(0xec)+_0x15594c+_0x3fdf5c(0x10e),_0x1d15e3),[];}const _0x1257df=await _0x19268b[_0x3fdf5c(0xce)](),_0x26cee1=_0x1257df[_0x3fdf5c(0x14a)]||_0x1257df[_0x3fdf5c(0xf9)]||_0x1257df[_0x3fdf5c(0xcc)]||[];return console[_0x3fdf5c(0xc7)](_0x3fdf5c(0x19c)+_0xc894e6[_0x3fdf5c(0xd4)]+_0x3fdf5c(0x14e)+_0x26cee1[_0x3fdf5c(0x18a)]+_0x3fdf5c(0x13d)),_0x26cee1;})[_0x46f2d2(0x13c)](_0x53275d=>{const _0x2fd7eb=_0x46f2d2;return console[_0x2fd7eb(0xd2)](_0x2fd7eb(0xec)+_0x15594c+'\x20时发生网络错误:',_0x53275d),[];});});}),_0x4cad98=await Promise['all'](_0x3c5f7f);_0x58b10b=_0x4cad98[_0x5e42a2(0x194)](),console[_0x5e42a2(0xc7)](_0x5e42a2(0x180)+_0x58b10b[_0x5e42a2(0x18a)]+'\x20条初步结果。');const _0x2ce09e=[],_0x50a504=new Set();for(const _0x3ac708 of _0x58b10b){_0x3ac708&&_0x3ac708[_0x5e42a2(0xf2)]&&!_0x50a504['has'](_0x3ac708[_0x5e42a2(0xf2)])&&(_0x50a504[_0x5e42a2(0x12e)](_0x3ac708[_0x5e42a2(0xf2)]),_0x2ce09e[_0x5e42a2(0x104)](_0x3ac708));}return console[_0x5e42a2(0xc7)](_0x5e42a2(0x15f)+_0x2ce09e[_0x5e42a2(0x18a)]+'\x20条结果。'),_0x2ce09e[_0x5e42a2(0x192)]((_0x1d2bd5,_0x318f0f)=>(_0x318f0f[_0x5e42a2(0x143)]||0x0)-(_0x1d2bd5[_0x5e42a2(0x143)]||0x0)),_0x2ce09e;}async function insertVectors(_0x3a6fd5,_0x2dc246=null,_0x3bf812){const _0x763d71=_0x177034;if(!_0x3bf812)throw new Error(_0x763d71(0xf7));if(_0x3a6fd5[_0x763d71(0x18a)]===0x0)return{'success':!![],'count':0x0};const _0x217914=_0x3a6fd5[_0x763d71(0x12a)]((_0x3a02f3,_0x189087)=>({'hash':generateHash(_0x3a02f3['text']+Date[_0x763d71(0xeb)]()+_0x189087),'text':_0x3a02f3[_0x763d71(0xf2)],'metadata':_0x3a02f3[_0x763d71(0x14a)]||{'source':'unknown','timestamp':new Date()['toISOString']()}})),_0x1bb0a8=_0x217914['reduce']((_0xdd38db,_0x7e22bb,_0x55a0c1)=>{const _0x1a4c22=_0x763d71;return _0xdd38db[_0x7e22bb[_0x1a4c22(0xf2)]]=_0x3a6fd5[_0x55a0c1][_0x1a4c22(0x15e)],_0xdd38db;},{}),_0x3d241a={'collectionId':_0x3bf812,'items':_0x217914,'source':_0x763d71(0x190),'embeddings':_0x1bb0a8},_0x40ee5b=await fetch(_0x763d71(0x124),{'method':'POST','headers':context[_0x763d71(0xda)](),'body':JSON['stringify'](_0x3d241a),'signal':_0x2dc246});if(!_0x40ee5b['ok']){const _0x63bcf4=await _0x40ee5b[_0x763d71(0xf2)]();console[_0x763d71(0xd2)](_0x763d71(0x138),_0x63bcf4);throw new Error(_0x763d71(0xc9)+_0x40ee5b['status']+':\x20'+_0x63bcf4);}return{'success':!![],'count':_0x217914[_0x763d71(0x18a)]};}function _0x4888(){const _0x406162=['manual','1272562tVOZIH','vector','[翰林院-日志]\x20去重后剩余\x20','test','slice','getTime','getContext','message','\x22\x20创建专属知识库...','index','\x20-\x20楼层\x20#','maxResults','warning','检测到旧版数据。此操作将把旧数据迁移到新格式,过程不可逆,是否继续?','clearJob','\x22\x20已删除。',',\x20向量化录入时间:\x20','floor','[翰林院-日志]\x20开始清空宝库...','\x22,将数据合并入库。',',\x20第1卷,\x20第1章,\x20第','[翰林院-日志]\x20集合\x20','newId','final_score','quiet','HANLINYUAN_RAG','474453aviljF','[翰林院-核心]\x20聊天记录凝识完成,成功插入\x20','start','AbortError','[翰林院-核心]\x20已为宝库\x20','max','知识库名称不能为空','min','notify','[翰林院-日志]\x20所有知识库查询完毕,共获得\x20','凝识之权未开启','[翰林院-核心]\x20将来源\x27','hybrid_alpha','\x20(集合ID:\x20','hashes','warn','操作已取消。','小说:\x20','第1卷','length','[来源:\x20聊天记录,\x20楼层:\x20#','未知角色','[翰林院-日志]\x20统计集合\x20','手动录入','[翰林院-核心]\x20尝试删除一个不存在的知识库:\x20','webllm','/api/vector/purge','sort','[翰林院-日志]\x20清空宝库API错误:','flat','Rerank失败:\x20','matchThreshold','injection','then','聊天记录','isArray','zh-CN','[翰林院-日志]\x20知识库\x20','advanced','%%HANLINYUAN_RAG_INJECTION%%','[翰林院-核心]\x20已创建并锁定知识库:\x20','depth','[翰林院-核心]\x20文本录入失败:\x20','No\x20messages\x20to\x20process.','toString','relevance_score','\x20添加新知识库:\x20','log','\x20个条目。','忆识存入API错误\x20','部分]','[翰林院-核心]\x20知识库\x20','data','\x20记录凝识范围:\x20','json','3500400FnoDcL','世界书条目','split','error',']\x20的消息已成功凝识。','name','stringify','删除知识库失败,未能清空后端数据。','52552gxuMwE','知识库\x20\x22','\x27的文本分割成\x20','getRequestHeaders','355VcCfsp','825008VDYXhM','翰林院忆识核心已启动\x20(V5.2-集成版),已注册到全局\x20hanlinyuanRagProcessor\x20对象。','\x0a</','忆识检索失败:\x20','chat_history','replace','正在处理\x20','findIndex','saveSettingsDebounced','resolve','[翰林院-日志]\x20获取集合\x20','[翰林院-迁移]\x20旧宝库已清空。','info','forEach','hanlinyuan-rag-core','now','[翰林院-日志]\x20查询知识库\x20','外部Rerank完成','range','[翰林院-核心]\x20准备为任务\x20\x22','/api/vector/list','sourceName','text','[翰林院-核心]\x20聊天记录凝识失败:\x20','[翰林院-日志]\x20没有启用的新知识库,尝试查询旧版单体宝库...','values','[翰林院]\x20检索或注入时发生错误:','insertVectors\x20必须接收一个有效的\x20collectionId\x20参数。','find','results','condensationHistory','oldId','\x20个块。','[来源:\x20世界书,\x20条目:\x20','[翰林院-核心]\x20已锁定忆识宝库ID:\x20','is_user','\x27,使用通用分块逻辑。','[翰林院-核心]\x20成功插入\x20','hanlinyuanRagProcessor','未知条目','push','rerank','batchSize','用户取消了迁移操作','[翰林院-Rerank]\x20开始外部API重排序...','entryName','\x20条消息分解为\x20','[翰林院-核心]\x20文本录入任务被用户中止。','\x20时发生网络错误:','[翰林院-核心]\x20凝识任务已锁定知识库:\x20','\x20失败:','bookName','\x20失败,删除操作中止。','task_','[翰林院-核心]\x20ingestTextToHanlinyuan\x20失败:','retrieval','chat','[翰林院-日志]\x20开始获取所有知识库的向量总数...','position','\x20(ID:\x20','saveProgress','[翰林院-迁移]\x20用户确认迁移,正在处理旧宝库:\x20','未分类世界书','messageTypes','top_n','filter','abs','[翰林院-日志]\x20正在查询知识库:\x20','novel','lorebook','1701IiVYRb','original_index','/api/vector/insert','aborted','[翰林院-日志]\x20所有知识库统计完成,总向量数:\x20','文本块和向量数量不匹配','all','enabled','map','6UcgYfz','[翰林院-核心]\x20processCondensation\x20失败:','queryMessageCount','add','[翰林院-日志]\x20无法确定要清空的目标集合ID。','[翰林院-Rerank]\x20开始元数据加权最终排序...','[翰林院]\x20未能获取SillyTavern上下文,初始化失败。','\x20的状态已切换为:\x20','mes','rerank_score','世界书','condensation','[翰林院-Rerank]\x20元数据加权排序完成。','[翰林院-日志]\x20忆识存入API错误:','{{text}}','success','[翰林院-核心]\x20已为角色\x20','catch','\x20条结果。',',\x20第','\x20个向量条目。','status','extensionSettings','join','score','16678340TNsjAe','73584BPQjAU','\x20不存在,计为\x200。','...)','toLocaleString','reduce','metadata','depth_role','第1章','POST','\x20返回\x20','旧版宝库\x20(Legacy)','[翰林院-日志]\x20开始多知识库向量查询...','[来源:\x20','substring','[翰林院-核心]\x20准备删除知识库\x20','输入文本为空','\x20个知识块,准备入库。',',将清空集合:\x20','knowledgeBases','toISOString','trim','end','object'];_0x4888=function(){return _0x406162;};return _0x4888();}async function getVectorCount(_0x3a9bc7=null){const _0x52c4ba=_0x177034,_0x239781=getCharacterStableId();if(_0x3a9bc7){const _0x327969=_0x239781+'_'+_0x3a9bc7;return await countVectorsInCollection(_0x327969);}else{console[_0x52c4ba(0xc7)](_0x52c4ba(0x115));const _0x58f8cc=getKnowledgeBases(),_0x5d2f72=Object[_0x52c4ba(0xf5)](_0x58f8cc),_0x3b8404=_0x5d2f72['map'](_0x113843=>{const _0x11858e=_0x239781+'_'+_0x113843['id'];return countVectorsInCollection(_0x11858e);}),_0x41133c=await _0x24c28a();_0x3b8404[_0x52c4ba(0x104)](countVectorsInCollection(_0x41133c));const _0x423edb=await Promise[_0x52c4ba(0x128)](_0x3b8404),_0xc309a1=_0x423edb[_0x52c4ba(0x149)]((_0x1e46a4,_0x2cd474)=>_0x1e46a4+_0x2cd474,0x0);return console[_0x52c4ba(0xc7)](_0x52c4ba(0x126)+_0xc309a1),_0xc309a1;}}async function countVectorsInCollection(_0x14ee12){const _0x3bc488=_0x177034;if(!_0x14ee12)return 0x0;console[_0x3bc488(0xc7)]('[翰林院-日志]\x20统计目标集合ID:\x20'+_0x14ee12);const _0x36e1fc={'collectionId':_0x14ee12,'source':_0x3bc488(0x190),'embeddings':{}};try{const _0x341737=await fetch(_0x3bc488(0xf0),{'method':_0x3bc488(0x14d),'headers':context[_0x3bc488(0xda)](),'body':JSON[_0x3bc488(0xd5)](_0x36e1fc)});if(!_0x341737['ok']){if(_0x341737['status']===0x194)console[_0x3bc488(0xc7)](_0x3bc488(0x172)+_0x14ee12+_0x3bc488(0x146));else{const _0xccca9=await _0x341737[_0x3bc488(0xf2)]();console[_0x3bc488(0x186)](_0x3bc488(0xe6)+_0x14ee12+'\x20列表API时出现问题\x20(状态:\x20'+_0x341737[_0x3bc488(0x140)]+'):',_0xccca9);}return 0x0;}const _0x16361a=await _0x341737[_0x3bc488(0xce)]();let _0x5163a1=0x0;if(Array['isArray'](_0x16361a))_0x5163a1=_0x16361a[_0x3bc488(0x18a)];else _0x16361a&&_0x16361a[_0x3bc488(0x185)]&&(_0x5163a1=_0x16361a[_0x3bc488(0x185)][_0x3bc488(0x18a)]);return _0x5163a1;}catch(_0x9e3ab5){return console[_0x3bc488(0xd2)](_0x3bc488(0x18d)+_0x14ee12+_0x3bc488(0x10c),_0x9e3ab5),0x0;}}async function purgeStorage(_0x189414=null){const _0x5233ea=_0x177034;console[_0x5233ea(0xc7)](_0x5233ea(0x16f));const _0x55da70=_0x189414||await getCollectionId();if(!_0x55da70)return console[_0x5233ea(0xd2)](_0x5233ea(0x12f)),toastr[_0x5233ea(0xd2)]('无法确定要清空的目标宝库。'),![];console['log']('[翰林院-日志]\x20清空目标集合ID:\x20'+_0x55da70);const _0xf66081={'collectionId':_0x55da70};console[_0x5233ea(0xc7)]('[翰林院-日志]\x20发送到\x20/api/vector/purge\x20的请求体:',JSON[_0x5233ea(0xd5)](_0xf66081,null,0x2));const _0x4010f3=await fetch(_0x5233ea(0x191),{'method':_0x5233ea(0x14d),'headers':context[_0x5233ea(0xda)](),'body':JSON[_0x5233ea(0xd5)](_0xf66081)});console[_0x5233ea(0xc7)]('[翰林院-日志]\x20/api/vector/purge\x20响应状态:\x20'+_0x4010f3[_0x5233ea(0x140)]);if(!_0x4010f3['ok']){const _0x12f7c5=await _0x4010f3[_0x5233ea(0xf2)]();console[_0x5233ea(0xd2)](_0x5233ea(0x193),_0x12f7c5);}else console[_0x5233ea(0xc7)]('[翰林院-日志]\x20清空宝库API调用成功。');return _0x4010f3['ok'];}function getMessagesForCondensation(_0x3ae9d1=null){const _0x28e9cd=_0x177034;if(!settings['condensation'][_0x28e9cd(0x129)])return showNotification(_0x28e9cd(0x181),_0x28e9cd(0x169)),[];const {layerStart:_0x193b76,layerEnd:_0x10831f}=settings[_0x28e9cd(0x136)],_0x4c83a8=_0x3ae9d1||settings[_0x28e9cd(0x136)][_0x28e9cd(0x11b)],_0x85572b=context[_0x28e9cd(0x114)][_0x28e9cd(0x18a)],_0x5a6493=Math[_0x28e9cd(0x17c)](0x0,_0x193b76-0x1),_0x2ad999=_0x10831f===0x0||_0x10831f>_0x85572b?_0x85572b:Math[_0x28e9cd(0x17e)](_0x85572b,_0x10831f),_0x11e676=context[_0x28e9cd(0x114)][_0x28e9cd(0x161)](_0x5a6493,_0x2ad999);return _0x11e676[_0x28e9cd(0x11d)](_0x53d22e=>{const _0x41c5c9=_0x28e9cd,_0x17ae09=_0x53d22e[_0x41c5c9(0xff)]===!![],_0x300d6f=_0x53d22e['is_user']===![];if(!_0x53d22e[_0x41c5c9(0x133)]||!_0x53d22e[_0x41c5c9(0x133)][_0x41c5c9(0x159)]())return![];return _0x4c83a8['user']&&_0x17ae09||_0x4c83a8['ai']&&_0x300d6f;});}async function processCondensation(_0x117212,_0x1078d6=()=>{},_0x1553b1=null){const _0x115991=_0x177034;if(!_0x117212||_0x117212[_0x115991(0x18a)]===0x0)return{'success':![],'error':_0x115991(0x1a2)};try{let _0x4213d9,_0x12dc83;const _0x5e4cd1=getCharacterName()||_0x115991(0x18c);if(_0x1553b1){const _0x1f9810=_0x1553b1[_0x115991(0x179)]??'?',_0x201f27=_0x1553b1[_0x115991(0x15a)]===0x0?'末':_0x1553b1[_0x115991(0x15a)]??'?';_0x4213d9=_0x5e4cd1+':\x20'+_0x1f9810+'楼-'+_0x201f27+'楼';}else{const _0x2b88f3=new Date()[_0x115991(0x148)](_0x115991(0x19b),{'hour12':![]});_0x4213d9='聊天记录:\x20'+_0x2b88f3;}const _0x511fbb=Object[_0x115991(0xf5)](getKnowledgeBases()),_0x1a385a=_0x511fbb['find'](_0x51b879=>_0x51b879[_0x115991(0xd4)]===_0x4213d9);if(_0x1a385a)_0x12dc83=_0x1a385a['id'],_0x1078d6('[翰林院-核心]\x20检测到同名知识库\x20\x22'+_0x4213d9+_0x115991(0x170),'info');else{_0x1078d6(_0x115991(0xef)+_0x4213d9+_0x115991(0x165),_0x115991(0xe8));const _0x3545c9=addKnowledgeBase(_0x4213d9);_0x12dc83=_0x3545c9['id'];}const _0x125604=getCharacterStableId(),_0x4ac7de=_0x125604+'_'+_0x12dc83;_0x1078d6(_0x115991(0x10d)+_0x4213d9+_0x115991(0x184)+_0x4ac7de+')',_0x115991(0x13a));const _0x2809d0=[],_0x48b42e=context[_0x115991(0x114)];for(const _0x470803 of _0x117212){const _0x4f7a52=(_0x470803['mes']||'')[_0x115991(0xe1)](/<[^>]*>/g,'')['trim']();if(_0x4f7a52[_0x115991(0x18a)]===0x0)continue;let _0x321956;if(_0x470803[_0x115991(0x16e)]!==undefined&&_0x470803[_0x115991(0x16e)]!==null)_0x321956=_0x470803[_0x115991(0x16e)];else{const _0x2c913f=_0x48b42e['findIndex'](_0x385711=>_0x385711===_0x470803);_0x321956=_0x2c913f!==-0x1?_0x2c913f+0x1:-0x1;}const _0xbe60de=new Date(_0x470803['send_date']),_0x29c43a=isNaN(_0xbe60de[_0x115991(0x162)]())?new Date()[_0x115991(0x158)]():_0xbe60de[_0x115991(0x158)](),_0x4f5050=splitIntoChunks(_0x4f7a52,'chat_history',{'floor':_0x321956,'is_user':_0x470803[_0x115991(0xff)],'timestamp':_0x29c43a});_0x2809d0['push'](..._0x4f5050);}if(_0x2809d0[_0x115991(0x18a)]===0x0)return{'success':!![],'count':0x0};_0x1078d6('[翰林院-核心]\x20已将\x20'+_0x117212[_0x115991(0x18a)]+_0x115991(0x10a)+_0x2809d0[_0x115991(0x18a)]+_0x115991(0x155),_0x115991(0xe8));const _0x95a45e=settings['retrieval']['batchSize']||0x5;let _0x226605=0x0;for(let _0x309aa8=0x0;_0x309aa8<_0x2809d0[_0x115991(0x18a)];_0x309aa8+=_0x95a45e){const _0x58aa1e=_0x2809d0[_0x115991(0x161)](_0x309aa8,_0x309aa8+_0x95a45e),_0x1b79ec=_0x58aa1e[_0x115991(0x12a)](_0x299bba=>_0x299bba[_0x115991(0xf2)]),_0x1768ea=await getEmbeddings(_0x1b79ec);if(_0x58aa1e[_0x115991(0x18a)]!==_0x1768ea['length'])throw new Error(_0x115991(0x127));const _0x3098d=_0x58aa1e[_0x115991(0x12a)]((_0x1a8175,_0x57f9ce)=>({..._0x1a8175,'vector':_0x1768ea[_0x57f9ce]}));await insertVectors(_0x3098d,null,_0x4ac7de),_0x226605+=_0x58aa1e[_0x115991(0x18a)];}if(_0x1553b1){const _0x218764=_0x1553b1['end']===0x0?context['chat'][_0x115991(0x18a)]:_0x1553b1[_0x115991(0x15a)];settings['condensationHistory'][_0x4ac7de]={'start':_0x1553b1[_0x115991(0x179)],'end':_0x218764,'timestamp':new Date()[_0x115991(0x158)]()},saveSettings(),_0x1078d6(_0x115991(0x17b)+_0x4ac7de+_0x115991(0xcd)+_0x1553b1[_0x115991(0x179)]+'-'+_0x218764,_0x115991(0xe8));}_0x1078d6(_0x115991(0x178)+_0x226605+_0x115991(0xc8),_0x115991(0x13a));const _0x34675f=_0x117212['map'](_0x4a5ea6=>{const _0x385bc4=_0x115991,_0x4940ac=_0x48b42e[_0x385bc4(0xe3)](_0x19b949=>_0x19b949===_0x4a5ea6),_0x473547=_0x4940ac!==-0x1?_0x4940ac+0x1:-0x1,_0x146972=_0x4a5ea6['is_user']?'用户':getCharacterName()||'AI';return'['+_0x146972+_0x385bc4(0x167)+_0x473547+_0x385bc4(0xd3);});return{'success':!![],'count':_0x226605,'messages':_0x34675f};}catch(_0x320fd2){return console[_0x115991(0xd2)](_0x115991(0x12c),_0x320fd2),_0x1078d6(_0x115991(0xf3)+_0x320fd2['message'],_0x115991(0xd2)),{'success':![],'error':_0x320fd2[_0x115991(0x164)]};}}async function rerankResults(_0x1bb8f8,_0x4a8f3c,_0x367a2c){const _0x5c4ba3=_0x177034;let _0x366048=_0x1bb8f8;if(_0x367a2c[_0x5c4ba3(0x105)][_0x5c4ba3(0x129)]&&_0x1bb8f8[_0x5c4ba3(0x18a)]>0x0){console['log'](_0x5c4ba3(0x108));try{const _0x1ca0fe=_0x1bb8f8[_0x5c4ba3(0x12a)](_0x292a7a=>_0x292a7a[_0x5c4ba3(0xf2)]),_0x568f72=await executeRerank(_0x4a8f3c,_0x1ca0fe,_0x367a2c['rerank']),_0x22e22c=_0x1bb8f8[_0x5c4ba3(0x12a)]((_0x1b62d5,_0x54948d)=>({..._0x1b62d5,'original_index':_0x54948d}));_0x366048=_0x22e22c[_0x5c4ba3(0x12a)](_0xc60158=>{const _0x4e4bd1=_0x5c4ba3,_0x4394eb=_0x568f72[_0x4e4bd1(0xf9)]['find'](_0x4a3ac2=>_0x4a3ac2[_0x4e4bd1(0x166)]===_0xc60158[_0x4e4bd1(0x123)]),_0x566334=_0x4394eb?_0x4394eb[_0x4e4bd1(0xc5)]:0x0;return{..._0xc60158,'rerank_score':_0x566334};});if(_0x367a2c[_0x5c4ba3(0x105)]['notify'])showNotification(_0x5c4ba3(0xed),_0x5c4ba3(0x13a));}catch(_0x5d14cd){console[_0x5c4ba3(0xd2)]('[翰林院-Rerank]\x20外部Rerank失败,将仅使用内部加权。',_0x5d14cd);if(_0x367a2c[_0x5c4ba3(0x105)][_0x5c4ba3(0x17f)])showNotification(_0x5c4ba3(0x195)+_0x5d14cd[_0x5c4ba3(0x164)],_0x5c4ba3(0xd2));_0x366048[_0x5c4ba3(0xe9)](_0xa0a9ab=>_0xa0a9ab[_0x5c4ba3(0x134)]=0x0);}}else _0x366048[_0x5c4ba3(0xe9)](_0x56d4d8=>_0x56d4d8[_0x5c4ba3(0x134)]=0x0);console[_0x5c4ba3(0xc7)](_0x5c4ba3(0x130));const _0x12625f=context[_0x5c4ba3(0x114)][_0x5c4ba3(0x18a)],_0x2fdbd6=_0x367a2c[_0x5c4ba3(0x105)][_0x5c4ba3(0x183)],_0xdfd49f=_0x366048['map'](_0x3dd8a7=>{const _0x1e3204=_0x5c4ba3;let _0xaed473=0x1;const _0x9cf7e4=_0x3dd8a7['metadata']||{};switch(_0x9cf7e4['source']){case'lorebook':_0xaed473*=1.2;break;case _0x1e3204(0x15c):_0xaed473*=1.1;break;case _0x1e3204(0xe0):if(_0x9cf7e4[_0x1e3204(0x16e)]&&_0x12625f>0x0){const _0x227605=_0x9cf7e4[_0x1e3204(0x16e)]/_0x12625f;_0xaed473*=0x1+_0x227605;}break;}const _0x457dce=_0x3dd8a7[_0x1e3204(0x134)]*_0x2fdbd6+(_0x3dd8a7[_0x1e3204(0x143)]||0x0)*(0x1-_0x2fdbd6),_0xe34333=_0x457dce*_0xaed473;return{..._0x3dd8a7,'final_score':_0xe34333};});return _0xdfd49f[_0x5c4ba3(0x192)]((_0x53ce89,_0x4ffa61)=>(_0x4ffa61[_0x5c4ba3(0x174)]||0x0)-(_0x53ce89[_0x5c4ba3(0x174)]||0x0)),console['log'](_0x5c4ba3(0x137)),_0xdfd49f[_0x5c4ba3(0x161)](0x0,_0x367a2c[_0x5c4ba3(0x105)][_0x5c4ba3(0x11c)]);}async function rearrangeChat(_0x54b974,_0x416e8a,_0x1aa7b5,_0x125b02){const _0x2cc4f6=_0x177034;setExtensionPrompt(_0x2cc4f6(0x176),'',settings[_0x2cc4f6(0x197)][_0x2cc4f6(0x116)],settings['injection'][_0x2cc4f6(0x1a0)],![],settings[_0x2cc4f6(0x197)][_0x2cc4f6(0x14b)]);if(_0x125b02===_0x2cc4f6(0x175)||!settings[_0x2cc4f6(0x113)]['enabled'])return;const _0x58f34b=_0x54b974[_0x2cc4f6(0x161)](-settings['advanced'][_0x2cc4f6(0x12d)]);if(_0x58f34b[_0x2cc4f6(0x18a)]===0x0)return;const _0x429ead=_0x58f34b[_0x2cc4f6(0x12a)](_0x6781f9=>_0x6781f9['mes'])[_0x2cc4f6(0x142)]('\x20')['replace'](/<[^>]*>/g,'')[_0x2cc4f6(0x159)]();if(!_0x429ead)return;try{const _0x35316a=await queryVectors(_0x429ead);if(_0x35316a[_0x2cc4f6(0x18a)]===0x0)return;const _0x45deea=await rerankResults(_0x35316a,_0x429ead,settings);if(_0x45deea[_0x2cc4f6(0x18a)]===0x0)return;const _0x22a8ed=_0x45deea['map'](_0x3c9e12=>_0x3c9e12[_0x2cc4f6(0xf2)])[_0x2cc4f6(0x142)]('\x0a\x0a');let _0x4cefe5=settings[_0x2cc4f6(0x197)]['template'][_0x2cc4f6(0xe1)](_0x2cc4f6(0x139),_0x22a8ed);_0x4cefe5[_0x2cc4f6(0x159)]()&&(_0x4cefe5=_0x2cc4f6(0x19e)+_0x4cefe5),setExtensionPrompt(_0x2cc4f6(0x176),_0x4cefe5,settings[_0x2cc4f6(0x197)][_0x2cc4f6(0x116)],settings['injection'][_0x2cc4f6(0x1a0)],![],settings[_0x2cc4f6(0x197)][_0x2cc4f6(0x14b)]);}catch(_0x13c3e1){console[_0x2cc4f6(0xd2)](_0x2cc4f6(0xf6),_0x13c3e1);if(settings[_0x2cc4f6(0x113)]['notify'])showNotification(_0x2cc4f6(0xdf)+_0x13c3e1[_0x2cc4f6(0x164)],'error');}}
|