diff --git a/core/rag-processor.js b/core/rag-processor.js index 6d9e635..b96d170 100644 --- a/core/rag-processor.js +++ b/core/rag-processor.js @@ -1 +1 @@ -'use strict';const _0x500795=_0x5105;(function(_0x1c6075,_0x3323df){const _0x5ea4ae=_0x5105,_0x28eb67=_0x1c6075();while(!![]){try{const _0x177e69=-parseInt(_0x5ea4ae(0x1c5))/0x1+-parseInt(_0x5ea4ae(0x1ae))/0x2*(-parseInt(_0x5ea4ae(0x17c))/0x3)+-parseInt(_0x5ea4ae(0x168))/0x4*(-parseInt(_0x5ea4ae(0x187))/0x5)+parseInt(_0x5ea4ae(0x1af))/0x6+-parseInt(_0x5ea4ae(0x16c))/0x7+-parseInt(_0x5ea4ae(0x188))/0x8*(parseInt(_0x5ea4ae(0x1c8))/0x9)+-parseInt(_0x5ea4ae(0x1c7))/0xa*(-parseInt(_0x5ea4ae(0x197))/0xb);if(_0x177e69===_0x3323df)break;else _0x28eb67['push'](_0x28eb67['shift']());}catch(_0xd6a2bf){_0x28eb67['push'](_0x28eb67['shift']());}}}(_0x3ea8,0x689c0));import{extension_prompt_roles,setExtensionPrompt}from'/script.js';import*as _0xc4dbf from'./utils/context-utils.js';const MODULE_NAME=_0x500795(0x183),OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME=_0x500795(0x182),defaultSettings={'retrieval':{'enabled':!![],'apiEndpoint':_0x500795(0x1a9),'customApiUrl':'','apiKey':'','embeddingModel':_0x500795(0x1cc),'notify':!![],'batchSize':0x5},'advanced':{'chunkSize':0x200,'overlap':0x32,'matchThreshold':0.3,'queryMessageCount':0x2,'maxResults':0x5},'injection':{'template':_0x500795(0x1cf),'position':0x1,'depth':0x1,'depth_role':0x0},'condensation':{'enabled':!![],'layerStart':0x1,'layerEnd':0xa,'messageTypes':{'user':!![],'ai':!![],'hidden':![]}}};let context=null,settings=null;export{initialize,getSettings,saveSettings,resetSettings,testApiConnection,fetchEmbeddingModels,getVectorCount,purgeStorage,getMessagesForCondensation,processCondensation,ingestTextToHanlinyuan,getCollectionId};function initialize(){const _0x4ff451=_0x500795;context=SillyTavern[_0x4ff451(0x15d)]();if(!context){console[_0x4ff451(0x152)](_0x4ff451(0x1ca));return;}settings=getSettings();const _0x45f8e2=window[OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME];typeof _0x45f8e2===_0x4ff451(0x18a)?(window[OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME]=async function(..._0x17b159){await rearrangeChat(..._0x17b159),await _0x45f8e2(..._0x17b159);},console[_0x4ff451(0x1a1)](_0x4ff451(0x1a6)+OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME)):(window[OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME]=rearrangeChat,console[_0x4ff451(0x1a1)](_0x4ff451(0x170)+OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME));}async function ingestTextToHanlinyuan(_0x56f41b){const _0x9a2c78=_0x500795;if(!settings)return{'success':![],'error':_0x9a2c78(0x1b8)};try{const _0xa4ca93=splitIntoChunks(_0x56f41b)[_0x9a2c78(0x1a8)](_0x5bbfe4=>({'text':_0x5bbfe4}));if(_0xa4ca93[_0x9a2c78(0x1ac)]===0x0)return{'success':!![]};const _0x100dc1=await insertVectors(_0xa4ca93);return{'success':!![],'count':_0x100dc1[_0x9a2c78(0x1bb)]};}catch(_0x3fa868){return console[_0x9a2c78(0x152)](_0x9a2c78(0x1c3),_0x3fa868),{'success':![],'error':_0x3fa868[_0x9a2c78(0x177)]};}}function getSettings(){const _0x168b5f=_0x500795;if(!context||!context[_0x168b5f(0x179)])return structuredClone(defaultSettings);!context['extensionSettings'][MODULE_NAME]&&(context[_0x168b5f(0x179)][MODULE_NAME]=structuredClone(defaultSettings));const _0x28ddd7=context[_0x168b5f(0x179)][MODULE_NAME];for(const _0x4471cd in defaultSettings){if(_0x28ddd7[_0x4471cd]===undefined)_0x28ddd7[_0x4471cd]=structuredClone(defaultSettings[_0x4471cd]);else{if(typeof defaultSettings[_0x4471cd]===_0x168b5f(0x1ba)&&!Array[_0x168b5f(0x173)](defaultSettings[_0x4471cd]))for(const _0x3ae209 in defaultSettings[_0x4471cd]){_0x28ddd7[_0x4471cd][_0x3ae209]===undefined&&(_0x28ddd7[_0x4471cd][_0x3ae209]=defaultSettings[_0x4471cd][_0x3ae209]);}}}return _0x28ddd7;}function saveSettings(){if(context)context['saveSettingsDebounced']();}function resetSettings(){const _0x3df957=_0x500795;context&&(context[_0x3df957(0x179)][MODULE_NAME]=structuredClone(defaultSettings),saveSettings());}function _0x3ea8(){const _0x3d4fac=['/v1/models','azure','26300LTPcJg','144688tzcvon','[翰林院-日志]\x20开始向量查询\x20(采用最终API交互模式)...','function','webllm','sort','template','messageTypes','宝库查询API错误\x20','maxResults','[翰林院-日志]\x20统计目标集合ID:\x20','[翰林院-日志]\x20清空宝库API调用成功。','embedding','请先配置API\x20Key','忆识存入API错误\x20','hashes','678898fXTLGg','):\x20','[翰林院-日志]\x20正在为\x20','metadata','results','min','charCodeAt','info','join','push','log','获取模型列表失败\x20(','No\x20valid\x20chunks\x20generated.','[翰林院-日志]\x20/api/vector/query\x20响应状态:\x20','substring','翰林院忆识核心已启动\x20(V5.1-和平共存版),已代理\x20','stringify','map','openai','now','\x20个向量。','length','/api/vector/purge','572774jkVlvw','2399088XaInjG','embeddings','injection','[翰林院-日志]\x20发送到\x20/api/vector/list\x20的请求体:','filter','[翰林院-日志]\x20/api/vector/purge\x20响应状态:\x20','[翰林院-日志]\x20/api/vector/query\x20响应内容:','max','模型API的响应格式无效:\x20未找到\x20\x27data\x27\x20数组。','核心未初始化','custom','object','count','is_user','warning','\x20个文本块获取向量...','mes','[翰林院-日志]\x20/api/vector/list\x20响应状态:\x20','/api/vector/insert','status','[翰林院]\x20处理外部文书时发生错误:','凝识之权未开启','737765bSBsId','[翰林院-日志]\x20查询目标集合ID:\x20','20AFohUK','18ayskUs','[翰林院]\x20检索或注入时发生错误:','[翰林院]\x20未能获取SillyTavern上下文,初始化失败。','getRequestHeaders','text-embedding-3-small','trim','[翰林院-日志]\x20获取向量列表API错误:','翰林院呈报相关忆识:\x0a{{text}}','embeddingModel','\x20获取模型列表...','忆识检索失败:\x20','reduce','[翰林院-日志]\x20清空宝库API错误:','json','[翰林院-日志]\x20忆识存入API调用成功。','matchThreshold','embed','text','[翰林院-日志]\x20开始获取向量总数...','/embeddings','collectionId','Authorization','error','batchSize','quiet','application/json','[翰林院-日志]\x20开始向量插入...','[翰林院-日志]\x20发送到\x20/api/vector/query\x20的请求体:','API\x20URL\x20或\x20Key\x20未提供。','[翰林院-日志]\x20查询成功,返回\x20','notify','api-key','No\x20messages\x20to\x20process.','getContext','position','[翰林院-日志]\x20宝库查询API错误:','POST','/api/vector/query','测试连接','includes','retrieval','flatMap','https://api.openai.com','[翰林院-日志]\x20发送到\x20/api/vector/purge\x20的请求体:','104HPwMhu','HANLINYUAN_RAG','Bearer\x20','GET','212807OthdzW','[翰林院-日志]\x20忆识存入API错误:','enabled','[翰林院]\x20正在从\x20','翰林院忆识核心已启动\x20(V5.1-借壳上市版),已注册全局函数:\x20','{{text}}','depth','isArray','[翰林院-日志]\x20开始清空宝库...','advanced','data','message','\x20条结果。','extensionSettings','queryMessageCount','toLowerCase','6zYNWWA','depth_role','slice','condensation','replace','[翰林院-日志]\x20/api/vector/list\x20响应内容:','vectors_rearrangeChat','hanlinyuan-rag-core','endsWith'];_0x3ea8=function(){return _0x3d4fac;};return _0x3ea8();}function showNotification(_0x42696b,_0x1345d5=_0x500795(0x19e)){toastr[_0x1345d5](_0x42696b);}function splitIntoChunks(_0x33b5fa){const _0x38138f=_0x500795,{chunkSize:_0x395e0b,overlap:_0x388ffa}=settings['advanced'],_0x26edbe=[];if(!_0x33b5fa||_0x395e0b<=0x0)return _0x26edbe;let _0x5d7545=0x0;while(_0x5d7545<_0x33b5fa[_0x38138f(0x1ac)]){const _0x491b67=Math[_0x38138f(0x19c)](_0x5d7545+_0x395e0b,_0x33b5fa['length']);_0x26edbe[_0x38138f(0x1a0)](_0x33b5fa[_0x38138f(0x1a5)](_0x5d7545,_0x491b67)),_0x5d7545+=_0x395e0b-_0x388ffa;}return _0x26edbe;}import{getCollectionId}from'./utils/context-utils.js';function generateHash(_0x241356){const _0xb9f7b9=_0x500795;let _0x3e44b9=0x0;for(let _0x21f0ef=0x0;_0x21f0ef<_0x241356[_0xb9f7b9(0x1ac)];_0x21f0ef++){const _0x41eb7d=_0x241356[_0xb9f7b9(0x19d)](_0x21f0ef);_0x3e44b9=(_0x3e44b9<<0x5)-_0x3e44b9+_0x41eb7d,_0x3e44b9=_0x3e44b9&_0x3e44b9;}return Math['abs'](_0x3e44b9)['toString'](0x24);}function _0x5105(_0x2640ac,_0x11bbc9){const _0x3ea83f=_0x3ea8();return _0x5105=function(_0x5105a5,_0x24dbb0){_0x5105a5=_0x5105a5-0x14d;let _0x864571=_0x3ea83f[_0x5105a5];return _0x864571;},_0x5105(_0x2640ac,_0x11bbc9);}function getSanitizedBaseUrl(_0x524ff2){const _0x3e1c83=_0x500795;let _0x5ef369=_0x524ff2['trim']();return _0x5ef369[_0x3e1c83(0x184)]('/')&&(_0x5ef369=_0x5ef369['slice'](0x0,-0x1)),_0x5ef369[_0x3e1c83(0x184)]('/v1')&&(_0x5ef369=_0x5ef369[_0x3e1c83(0x17e)](0x0,-0x3)),_0x5ef369[_0x3e1c83(0x184)](_0x3e1c83(0x14f))&&(_0x5ef369=_0x5ef369[_0x3e1c83(0x17e)](0x0,-0xb)),_0x5ef369;}async function fetchEmbeddingModels(){const _0x1c5fc4=_0x500795,{apiKey:_0x406ffb}=settings[_0x1c5fc4(0x164)],_0x3fbdcf=getApiEndpointUrl(!![]);if(!_0x3fbdcf||!_0x406ffb)throw new Error(_0x1c5fc4(0x158));const _0x119873=getSanitizedBaseUrl(_0x3fbdcf),_0x109cb8=_0x119873+_0x1c5fc4(0x185);console['log'](_0x1c5fc4(0x16f)+_0x109cb8+_0x1c5fc4(0x1d1));const _0x5bf7f4=await fetch(_0x109cb8,{'method':_0x1c5fc4(0x16b),'headers':getApiHeaders()});if(!_0x5bf7f4['ok']){const _0x4da2c3=await _0x5bf7f4[_0x1c5fc4(0x14d)]();throw new Error(_0x1c5fc4(0x1a2)+_0x5bf7f4[_0x1c5fc4(0x1c2)]+_0x1c5fc4(0x198)+_0x4da2c3);}const _0x323e70=await _0x5bf7f4[_0x1c5fc4(0x1d5)]();if(!_0x323e70[_0x1c5fc4(0x176)]||!Array[_0x1c5fc4(0x173)](_0x323e70['data']))throw new Error(_0x1c5fc4(0x1b7));return _0x323e70[_0x1c5fc4(0x176)][_0x1c5fc4(0x1a8)](_0x3d0d95=>_0x3d0d95['id'])[_0x1c5fc4(0x1b3)](_0x57bfda=>_0x57bfda[_0x1c5fc4(0x17b)]()[_0x1c5fc4(0x163)](_0x1c5fc4(0x1d8)))[_0x1c5fc4(0x18c)]();}function getApiEndpointUrl(_0x5b36ff=![]){const _0x133a16=_0x500795,{apiEndpoint:_0x4075af,customApiUrl:_0x44571a}=settings[_0x133a16(0x164)];let _0x5c3336;switch(_0x4075af){case _0x133a16(0x1a9):_0x5c3336=_0x133a16(0x166);break;case _0x133a16(0x186):case _0x133a16(0x1b9):_0x5c3336=_0x44571a;break;default:_0x5c3336=_0x133a16(0x166);break;}if(_0x5b36ff)return _0x5c3336;return getSanitizedBaseUrl(_0x5c3336)+'/v1/embeddings';}function getApiHeaders(){const _0x5c1de2=_0x500795,_0x3157a8={'Content-Type':_0x5c1de2(0x155)},{apiKey:_0x565235,apiEndpoint:_0x54a74f}=settings['retrieval'];switch(_0x54a74f){case _0x5c1de2(0x1a9):case _0x5c1de2(0x1b9):_0x3157a8[_0x5c1de2(0x151)]=_0x5c1de2(0x16a)+_0x565235;break;case _0x5c1de2(0x186):_0x3157a8[_0x5c1de2(0x15b)]=_0x565235;break;}return _0x3157a8;}async function getEmbeddings(_0x5d0ed2){const _0x440257=_0x500795;if(!settings['retrieval']['apiKey'])throw new Error(_0x440257(0x194));const _0x2cea65=getApiEndpointUrl(),_0x2f0f70=getApiHeaders(),_0x14b98d=settings[_0x440257(0x164)][_0x440257(0x1d0)],_0x3f98d3=settings[_0x440257(0x164)][_0x440257(0x153)]||0x5,_0x2f880f=[];for(let _0x24cf7c=0x0;_0x24cf7c<_0x5d0ed2[_0x440257(0x1ac)];_0x24cf7c+=_0x3f98d3){const _0x5848fb=_0x5d0ed2[_0x440257(0x17e)](_0x24cf7c,_0x24cf7c+_0x3f98d3),_0x5f51fc=await fetch(_0x2cea65,{'method':_0x440257(0x160),'headers':_0x2f0f70,'body':JSON['stringify']({'input':_0x5848fb,'model':_0x14b98d})});if(!_0x5f51fc['ok']){const _0x24626b=await _0x5f51fc['text']();throw new Error('神力获取失败\x20'+_0x5f51fc['status']+':\x20'+_0x24626b);}const _0x3dbc33=await _0x5f51fc[_0x440257(0x1d5)]();_0x2f880f[_0x440257(0x1a0)](..._0x3dbc33[_0x440257(0x176)][_0x440257(0x1a8)](_0x1758fc=>_0x1758fc[_0x440257(0x193)])),_0x24cf7c+_0x3f98d3<_0x5d0ed2[_0x440257(0x1ac)]&&await new Promise(_0x1b17a0=>setTimeout(_0x1b17a0,0xc8));}return _0x2f880f;}async function queryVectors(_0x1b5fdd){const _0x863811=_0x500795;console[_0x863811(0x1a1)](_0x863811(0x189));const _0x53ae96=getCollectionId();console['log'](_0x863811(0x1c6)+_0x53ae96);const _0x3c5e30=(await getEmbeddings([_0x1b5fdd]))[0x0],_0x59d992={'collectionId':_0x53ae96,'searchText':_0x1b5fdd,'topK':settings['advanced'][_0x863811(0x190)],'threshold':settings[_0x863811(0x175)][_0x863811(0x1d7)],'source':_0x863811(0x18b),'embeddings':{[_0x1b5fdd]:_0x3c5e30}};console[_0x863811(0x1a1)](_0x863811(0x157),JSON[_0x863811(0x1a7)](_0x59d992,null,0x2));const _0xc9e08b=await fetch(_0x863811(0x161),{'method':'POST','headers':context[_0x863811(0x1cb)](),'body':JSON[_0x863811(0x1a7)](_0x59d992)});console[_0x863811(0x1a1)](_0x863811(0x1a4)+_0xc9e08b[_0x863811(0x1c2)]);if(!_0xc9e08b['ok']){const _0x196e90=await _0xc9e08b[_0x863811(0x14d)]();console[_0x863811(0x152)](_0x863811(0x15f),_0x196e90);throw new Error(_0x863811(0x18f)+_0xc9e08b[_0x863811(0x1c2)]+':\x20'+_0x196e90);}const _0x4b1815=await _0xc9e08b[_0x863811(0x1d5)]();console[_0x863811(0x1a1)](_0x863811(0x1b5),_0x4b1815);const _0x403116=_0x4b1815[_0x863811(0x19a)]||_0x4b1815[_0x863811(0x19b)]||_0x4b1815[_0x863811(0x176)]||[];return console[_0x863811(0x1a1)](_0x863811(0x159)+_0x403116[_0x863811(0x1ac)]+_0x863811(0x178)),_0x403116;}async function insertVectors(_0x531b1e){const _0x243137=_0x500795;console[_0x243137(0x1a1)](_0x243137(0x156));const _0x1c3e34=getCollectionId();console[_0x243137(0x1a1)]('[翰林院-日志]\x20插入目标集合ID:\x20'+_0x1c3e34);const _0x5130e6=_0x531b1e[_0x243137(0x1a8)](_0x29a946=>_0x29a946[_0x243137(0x14d)]);if(_0x5130e6[_0x243137(0x1ac)]===0x0)return console['log']('[翰林院-日志]\x20没有需要插入的文本块,操作完成。'),{'success':!![],'count':0x0};console[_0x243137(0x1a1)](_0x243137(0x199)+_0x5130e6['length']+_0x243137(0x1be));const _0x1dd316=await getEmbeddings(_0x5130e6);console[_0x243137(0x1a1)]('[翰林院-日志]\x20成功获取\x20'+_0x1dd316[_0x243137(0x1ac)]+_0x243137(0x1ab));const _0x3551ca=_0x531b1e[_0x243137(0x1a8)]((_0x449255,_0x40c916)=>({'hash':generateHash(_0x449255[_0x243137(0x14d)]+Date[_0x243137(0x1aa)]()+_0x40c916),'text':_0x449255[_0x243137(0x14d)]})),_0x4ada0e=_0x3551ca[_0x243137(0x1d3)]((_0x2619bd,_0x2a9be3,_0x3d3378)=>{const _0x43fbbb=_0x243137;return _0x2619bd[_0x2a9be3[_0x43fbbb(0x14d)]]=_0x1dd316[_0x3d3378],_0x2619bd;},{}),_0x1a85a1={'collectionId':_0x1c3e34,'items':_0x3551ca,'source':_0x243137(0x18b),'embeddings':_0x4ada0e};console[_0x243137(0x1a1)]('[翰林院-日志]\x20发送到\x20/api/vector/insert\x20的请求体\x20(仅显示条目数):',{'collectionId':_0x1a85a1[_0x243137(0x150)],'source':_0x1a85a1['source'],'itemCount':_0x1a85a1['items'][_0x243137(0x1ac)],'embeddingCount':Object['keys'](_0x1a85a1[_0x243137(0x1b0)])[_0x243137(0x1ac)]});const _0x3ebf1f=await fetch(_0x243137(0x1c1),{'method':_0x243137(0x160),'headers':context['getRequestHeaders'](),'body':JSON[_0x243137(0x1a7)](_0x1a85a1)});console[_0x243137(0x1a1)]('[翰林院-日志]\x20/api/vector/insert\x20响应状态:\x20'+_0x3ebf1f['status']);if(!_0x3ebf1f['ok']){const _0x269cd3=await _0x3ebf1f[_0x243137(0x14d)]();console[_0x243137(0x152)](_0x243137(0x16d),_0x269cd3);throw new Error(_0x243137(0x195)+_0x3ebf1f['status']+':\x20'+_0x269cd3);}return console[_0x243137(0x1a1)](_0x243137(0x1d6)),{'success':!![],'count':_0x3551ca[_0x243137(0x1ac)]};}async function testApiConnection(){const _0x4c6173=_0x500795;await getEmbeddings([_0x4c6173(0x162)]);}async function getVectorCount(){const _0x1cae9b=_0x500795;console[_0x1cae9b(0x1a1)](_0x1cae9b(0x14e));const _0x3cbfc9=getCollectionId();console[_0x1cae9b(0x1a1)](_0x1cae9b(0x191)+_0x3cbfc9);const _0x1e27c3={'collectionId':_0x3cbfc9,'source':_0x1cae9b(0x18b),'embeddings':{}};console[_0x1cae9b(0x1a1)](_0x1cae9b(0x1b2),JSON[_0x1cae9b(0x1a7)](_0x1e27c3,null,0x2));const _0x127fd1=await fetch('/api/vector/list',{'method':'POST','headers':context[_0x1cae9b(0x1cb)](),'body':JSON['stringify'](_0x1e27c3)});console[_0x1cae9b(0x1a1)](_0x1cae9b(0x1c0)+_0x127fd1['status']);if(!_0x127fd1['ok']){const _0x3b6ede=await _0x127fd1[_0x1cae9b(0x14d)]();return console[_0x1cae9b(0x152)](_0x1cae9b(0x1ce),_0x3b6ede),0x0;}const _0x5bd63a=await _0x127fd1['json']();console[_0x1cae9b(0x1a1)](_0x1cae9b(0x181),_0x5bd63a);let _0x47368f=0x0;if(Array['isArray'](_0x5bd63a))_0x47368f=_0x5bd63a['length'];else _0x5bd63a&&_0x5bd63a[_0x1cae9b(0x196)]&&(_0x47368f=_0x5bd63a['hashes'][_0x1cae9b(0x1ac)]);return console[_0x1cae9b(0x1a1)]('[翰林院-日志]\x20统计成功,向量总数:\x20'+_0x47368f),_0x47368f;}async function purgeStorage(){const _0x472925=_0x500795;console[_0x472925(0x1a1)](_0x472925(0x174));const _0x564de4=getCollectionId();console['log']('[翰林院-日志]\x20清空目标集合ID:\x20'+_0x564de4);const _0x56330f={'collectionId':_0x564de4};console[_0x472925(0x1a1)](_0x472925(0x167),JSON[_0x472925(0x1a7)](_0x56330f,null,0x2));const _0x9cb4c3=await fetch(_0x472925(0x1ad),{'method':'POST','headers':context[_0x472925(0x1cb)](),'body':JSON[_0x472925(0x1a7)](_0x56330f)});console[_0x472925(0x1a1)](_0x472925(0x1b4)+_0x9cb4c3[_0x472925(0x1c2)]);if(!_0x9cb4c3['ok']){const _0x1f696e=await _0x9cb4c3[_0x472925(0x14d)]();console['error'](_0x472925(0x1d4),_0x1f696e);}else console[_0x472925(0x1a1)](_0x472925(0x192));return _0x9cb4c3['ok'];}function getMessagesForCondensation(_0x280211=null){const _0x1d15a0=_0x500795;if(!settings[_0x1d15a0(0x17f)][_0x1d15a0(0x16e)])return showNotification(_0x1d15a0(0x1c4),_0x1d15a0(0x1bd)),[];const {layerStart:_0x1dd18b,layerEnd:_0x7aadd2}=settings[_0x1d15a0(0x17f)],_0x1a512a=_0x280211||settings[_0x1d15a0(0x17f)][_0x1d15a0(0x18e)],_0x5e0440=context['chat']['length'],_0x2332a6=Math[_0x1d15a0(0x1b6)](0x0,_0x1dd18b-0x1),_0x24a0ea=Math[_0x1d15a0(0x19c)](_0x5e0440,_0x7aadd2),_0x45c2a1=context['chat'][_0x1d15a0(0x17e)](_0x2332a6,_0x24a0ea);return _0x45c2a1[_0x1d15a0(0x1b3)](_0x4b4f6c=>{const _0x5b7aca=_0x1d15a0,_0x4878c4=_0x4b4f6c[_0x5b7aca(0x1bc)]===!![],_0x77a84a=_0x4b4f6c[_0x5b7aca(0x1bc)]===![];if(!_0x4b4f6c[_0x5b7aca(0x1bf)]||!_0x4b4f6c[_0x5b7aca(0x1bf)][_0x5b7aca(0x1cd)]())return![];return _0x1a512a['user']&&_0x4878c4||_0x1a512a['ai']&&_0x77a84a;});}async function processCondensation(_0x426ad8){const _0xc05fc5=_0x500795;if(!_0x426ad8||_0x426ad8['length']===0x0)return{'success':![],'error':_0xc05fc5(0x15c)};const _0x11a413=_0x426ad8[_0xc05fc5(0x1a8)](_0xf707eb=>({'text':(_0xf707eb['mes']||'')['replace'](/<[^>]*>/g,'')['trim']()}))[_0xc05fc5(0x1b3)](_0x92b7d7=>_0x92b7d7['text'][_0xc05fc5(0x1ac)]>0x0),_0x517ee5=_0x11a413[_0xc05fc5(0x165)](_0x1e6adf=>splitIntoChunks(_0x1e6adf[_0xc05fc5(0x14d)])[_0xc05fc5(0x1a8)](_0x3008db=>({'text':_0x3008db})));if(_0x517ee5['length']===0x0)return{'success':![],'error':_0xc05fc5(0x1a3)};return await insertVectors(_0x517ee5);}async function rearrangeChat(_0x1f17ef,_0x315f79,_0x5b6173,_0x3b5c74){const _0x2bf6ea=_0x500795;setExtensionPrompt(_0x2bf6ea(0x169),'',settings[_0x2bf6ea(0x1b1)]['position'],settings['injection'][_0x2bf6ea(0x172)],![],settings[_0x2bf6ea(0x1b1)][_0x2bf6ea(0x17d)]);if(_0x3b5c74===_0x2bf6ea(0x154)||!settings[_0x2bf6ea(0x164)][_0x2bf6ea(0x16e)])return;const _0x1fd899=_0x1f17ef['slice'](-settings[_0x2bf6ea(0x175)][_0x2bf6ea(0x17a)]);if(_0x1fd899['length']===0x0)return;const _0x391430=_0x1fd899[_0x2bf6ea(0x1a8)](_0x4cf795=>_0x4cf795[_0x2bf6ea(0x1bf)])[_0x2bf6ea(0x19f)]('\x20')[_0x2bf6ea(0x180)](/<[^>]*>/g,'')[_0x2bf6ea(0x1cd)]();if(!_0x391430)return;try{const _0x469a11=await queryVectors(_0x391430);if(_0x469a11[_0x2bf6ea(0x1ac)]===0x0)return;const _0x31c23e=_0x469a11['map'](_0x2209f4=>_0x2209f4[_0x2bf6ea(0x14d)])[_0x2bf6ea(0x19f)]('\x0a\x0a'),_0x232843=settings['injection'][_0x2bf6ea(0x18d)][_0x2bf6ea(0x180)](_0x2bf6ea(0x171),_0x31c23e);setExtensionPrompt(_0x2bf6ea(0x169),_0x232843,settings[_0x2bf6ea(0x1b1)][_0x2bf6ea(0x15e)],settings[_0x2bf6ea(0x1b1)][_0x2bf6ea(0x172)],![],settings[_0x2bf6ea(0x1b1)]['depth_role']);}catch(_0x19c466){console[_0x2bf6ea(0x152)](_0x2bf6ea(0x1c9),_0x19c466);if(settings['retrieval'][_0x2bf6ea(0x15a)])showNotification(_0x2bf6ea(0x1d2)+_0x19c466[_0x2bf6ea(0x177)],_0x2bf6ea(0x152));}} +'use strict';const _0x3413e8=_0x4f86;(function(_0x2fd2b1,_0x1423af){const _0x30c399=_0x4f86,_0x8969bb=_0x2fd2b1();while(!![]){try{const _0x438cba=parseInt(_0x30c399(0x202))/0x1*(parseInt(_0x30c399(0x1e5))/0x2)+-parseInt(_0x30c399(0x255))/0x3*(-parseInt(_0x30c399(0x23b))/0x4)+parseInt(_0x30c399(0x1f2))/0x5*(parseInt(_0x30c399(0x20f))/0x6)+parseInt(_0x30c399(0x1fc))/0x7*(-parseInt(_0x30c399(0x21a))/0x8)+-parseInt(_0x30c399(0x26d))/0x9+-parseInt(_0x30c399(0x1ec))/0xa+parseInt(_0x30c399(0x217))/0xb;if(_0x438cba===_0x1423af)break;else _0x8969bb['push'](_0x8969bb['shift']());}catch(_0x122f4c){_0x8969bb['push'](_0x8969bb['shift']());}}}(_0x5d4d,0x78a20));import{extension_prompt_roles,setExtensionPrompt}from'/script.js';import*as _0x5601af from'./utils/context-utils.js';import{defaultSettings as _0x3a47de}from'./rag-settings.js';const MODULE_NAME=_0x3413e8(0x26f),OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME=_0x3413e8(0x1fa);let context=null,settings=null;export{initialize,getSettings,saveSettings,resetSettings,testApiConnection,fetchEmbeddingModels,fetchRerankModels,getVectorCount,purgeStorage,getMessagesForCondensation,processCondensation,ingestTextToHanlinyuan,getCollectionId};function initialize(){const _0x26a106=_0x3413e8;context=SillyTavern[_0x26a106(0x272)]();if(!context){console['error']('[翰林院]\x20未能获取SillyTavern上下文,初始化失败。');return;}settings=getSettings();const _0x208934=window[OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME];typeof _0x208934===_0x26a106(0x203)?(window[OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME]=async function(..._0x5d84bd){await rearrangeChat(..._0x5d84bd),await _0x208934(..._0x5d84bd);},console['log']('翰林院忆识核心已启动\x20(V5.1-和平共存版),已代理\x20'+OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME)):(window[OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME]=rearrangeChat,console['log'](_0x26a106(0x1f0)+OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME));}async function ingestTextToHanlinyuan(_0x3531ed){const _0x37b336=_0x3413e8;if(!settings)return{'success':![],'error':_0x37b336(0x23d)};try{const _0x1e13da=splitIntoChunks(_0x3531ed)[_0x37b336(0x1f6)](_0x229d99=>({'text':_0x229d99}));if(_0x1e13da[_0x37b336(0x226)]===0x0)return{'success':!![]};const _0x1c4195=await insertVectors(_0x1e13da);return{'success':!![],'count':_0x1c4195['count']};}catch(_0x4e0e0b){return console[_0x37b336(0x214)](_0x37b336(0x230),_0x4e0e0b),{'success':![],'error':_0x4e0e0b['message']};}}function getSettings(){const _0x4565e5=_0x3413e8;if(!context||!context[_0x4565e5(0x223)])return structuredClone(_0x3a47de);let _0x414d43=context[_0x4565e5(0x223)][MODULE_NAME];!_0x414d43&&(_0x414d43={},context[_0x4565e5(0x223)][MODULE_NAME]=_0x414d43);for(const _0x51e5c3 in _0x3a47de){if(_0x414d43[_0x51e5c3]===undefined)_0x414d43[_0x51e5c3]=structuredClone(_0x3a47de[_0x51e5c3]);else{if(typeof _0x3a47de[_0x51e5c3]===_0x4565e5(0x209)&&!Array[_0x4565e5(0x24c)](_0x3a47de[_0x51e5c3])&&_0x3a47de[_0x51e5c3]!==null)for(const _0x50e852 in _0x3a47de[_0x51e5c3]){_0x414d43[_0x51e5c3][_0x50e852]===undefined&&(_0x414d43[_0x51e5c3][_0x50e852]=_0x3a47de[_0x51e5c3][_0x50e852]);}}}return _0x414d43;}function _0x5d4d(){const _0x54927b=['Rerank失败:\x20','trim','https://api.openai.com','keys','mes','data','Bearer\x20','extensionSettings','[翰林院-日志]\x20清空目标集合ID:\x20','url','length','模型API的响应格式无效:\x20未找到\x20\x27data\x27\x20数组。','join','slice','忆识存入API错误\x20','忆识重排序完成','getRequestHeaders','神力获取失败\x20','injection','[翰林院-日志]\x20统计目标集合ID:\x20','[翰林院]\x20处理外部文书时发生错误:','toString','success','[翰林院-日志]\x20开始向量插入...','\x20个文本块获取向量...','/v1','apiKey','depth_role','user','/api/vector/list','/v1/rerank','128HehfaT','[翰林院-日志]\x20发送到\x20/api/vector/insert\x20的请求体\x20(仅显示条目数):','核心未初始化','Rerank\x20API\x20URL\x20或\x20Key\x20未提供。','results','/api/vector/query','matchThreshold','saveSettingsDebounced','):\x20','[翰林院]\x20检索或注入时发生错误:','[翰林院-日志]\x20清空宝库API错误:','min','max','original_index','chat','POST','reduce','isArray','webllm','API\x20URL\x20或\x20Key\x20未提供。','model','is_user','charCodeAt','template','find','hybrid_score','42609fWiwCF','json','[翰林院-日志]\x20/api/vector/list\x20响应内容:','queryMessageCount','stringify','batchSize','items','push','api-key','忆识检索失败:\x20','[翰林院-日志]\x20查询成功,返回\x20','[翰林院-日志]\x20开始清空宝库...','[翰林院-日志]\x20查询目标集合ID:\x20','宝库查询API错误\x20','abs','[翰林院-日志]\x20统计成功,向量总数:\x20','notify','[翰林院-日志]\x20/api/vector/query\x20响应状态:\x20','top_n','openai','quiet','index','[翰林院-日志]\x20忆识存入API调用成功。','[翰林院-日志]\x20发送到\x20/api/vector/purge\x20的请求体:','3334968CAbjmv','log','hanlinyuan-rag-core','/api/vector/insert','maxResults','getContext','\x20获取模型列表...','[翰林院-Rerank]\x20Rerank\x20失败,回退到原始相似度排序。','score','Rerank模型API的响应格式无效:\x20未找到\x20\x27data\x27\x20数组。','custom','2KURtEt','[翰林院-Rerank]\x20混合分数排序后的结果:','sort','凝识之权未开启','[翰林院-Rerank]\x20正在从\x20','[翰林院-日志]\x20成功获取\x20','text','6701750jcDUYf','hashes','warning','depth','翰林院忆识核心已启动\x20(V5.1-借壳上市版),已注册全局函数:\x20','azure','20185JanlkL','[翰林院-日志]\x20/api/vector/purge\x20响应状态:\x20','condensation','[翰林院-日志]\x20清空宝库API调用成功。','map','message','rerank','/embeddings','vectors_rearrangeChat','[翰林院-日志]\x20没有需要插入的文本块,操作完成。','1085XSieac','获取Rerank模型列表失败\x20(','[翰林院-日志]\x20发送到\x20/api/vector/query\x20的请求体:','[翰林院-日志]\x20开始向量查询\x20(采用最终API交互模式)...','GET','application/json','323248usHtZD','function','now','[翰林院-日志]\x20发送到\x20/api/vector/list\x20的请求体:','enabled','[翰林院-日志]\x20插入目标集合ID:\x20','embedding','object','No\x20messages\x20to\x20process.','status','advanced','endsWith','[翰林院-日志]\x20/api/vector/query\x20响应内容:','426WukGyq','Authorization','embeddingModel','\x20条结果。','retrieval','error','/rerank','请先配置API\x20Key','11611523twzpoX','filter','replace','30200cgVNHw','messageTypes'];_0x5d4d=function(){return _0x54927b;};return _0x5d4d();}function saveSettings(){const _0x1c94c2=_0x3413e8;if(context)context[_0x1c94c2(0x242)]();}function resetSettings(){context&&(context['extensionSettings'][MODULE_NAME]=structuredClone(_0x3a47de),saveSettings());}function showNotification(_0x532e72,_0x5af33b='info'){toastr[_0x5af33b](_0x532e72);}function splitIntoChunks(_0x5a96a2){const _0xd71736=_0x3413e8,{chunkSize:_0x1cb5fb,overlap:_0x55564b}=settings[_0xd71736(0x20c)],_0x26ba41=[];if(!_0x5a96a2||_0x1cb5fb<=0x0)return _0x26ba41;let _0x4ad672=0x0;while(_0x4ad672<_0x5a96a2[_0xd71736(0x226)]){const _0x4c3356=Math[_0xd71736(0x246)](_0x4ad672+_0x1cb5fb,_0x5a96a2[_0xd71736(0x226)]);_0x26ba41[_0xd71736(0x25c)](_0x5a96a2['substring'](_0x4ad672,_0x4c3356)),_0x4ad672+=_0x1cb5fb-_0x55564b;}return _0x26ba41;}import{getCollectionId}from'./utils/context-utils.js';function generateHash(_0xf926a6){const _0x39e6d3=_0x3413e8;let _0x4c1098=0x0;for(let _0x1dfa86=0x0;_0x1dfa86<_0xf926a6[_0x39e6d3(0x226)];_0x1dfa86++){const _0x54ea79=_0xf926a6[_0x39e6d3(0x251)](_0x1dfa86);_0x4c1098=(_0x4c1098<<0x5)-_0x4c1098+_0x54ea79,_0x4c1098=_0x4c1098&_0x4c1098;}return Math[_0x39e6d3(0x263)](_0x4c1098)[_0x39e6d3(0x231)](0x24);}function getSanitizedBaseUrl(_0x64b871){const _0x31f562=_0x3413e8;let _0x4a3da2=_0x64b871[_0x31f562(0x21d)]();return _0x4a3da2['endsWith']('/')&&(_0x4a3da2=_0x4a3da2[_0x31f562(0x229)](0x0,-0x1)),_0x4a3da2[_0x31f562(0x20d)](_0x31f562(0x235))&&(_0x4a3da2=_0x4a3da2[_0x31f562(0x229)](0x0,-0x3)),_0x4a3da2[_0x31f562(0x20d)](_0x31f562(0x1f9))&&(_0x4a3da2=_0x4a3da2['slice'](0x0,-0xb)),_0x4a3da2;}async function fetchEmbeddingModels(){const _0x188e80=_0x3413e8,{apiKey:_0x1a0696}=settings[_0x188e80(0x213)],_0x54ec0d=getApiEndpointUrl(!![]);if(!_0x54ec0d||!_0x1a0696)throw new Error(_0x188e80(0x24e));const _0x4ff528=getSanitizedBaseUrl(_0x54ec0d),_0x554b6a=_0x4ff528+'/v1/models';console[_0x188e80(0x26e)]('[翰林院]\x20正在从\x20'+_0x554b6a+_0x188e80(0x273));const _0x4fd28d=await fetch(_0x554b6a,{'method':'GET','headers':getApiHeaders()});if(!_0x4fd28d['ok']){const _0x5e4e7a=await _0x4fd28d[_0x188e80(0x1eb)]();throw new Error('获取模型列表失败\x20('+_0x4fd28d[_0x188e80(0x20b)]+_0x188e80(0x243)+_0x5e4e7a);}const _0x3cbc46=await _0x4fd28d[_0x188e80(0x256)]();if(!_0x3cbc46[_0x188e80(0x221)]||!Array[_0x188e80(0x24c)](_0x3cbc46['data']))throw new Error(_0x188e80(0x227));return _0x3cbc46[_0x188e80(0x221)][_0x188e80(0x1f6)](_0x9225fb=>_0x9225fb['id'])[_0x188e80(0x1e7)]();}function getRerankBaseUrl(_0x207a80){const _0x3990c2=_0x3413e8;let _0x25d8f1=_0x207a80[_0x3990c2(0x21d)]();return _0x25d8f1['endsWith']('/')&&(_0x25d8f1=_0x25d8f1[_0x3990c2(0x229)](0x0,-0x1)),_0x25d8f1['endsWith'](_0x3990c2(0x235))&&(_0x25d8f1=_0x25d8f1['slice'](0x0,-0x3)),_0x25d8f1['endsWith'](_0x3990c2(0x215))&&(_0x25d8f1=_0x25d8f1[_0x3990c2(0x229)](0x0,-0x7)),_0x25d8f1;}function _0x4f86(_0x12cd16,_0xc495b3){const _0x5d4d86=_0x5d4d();return _0x4f86=function(_0x4f863c,_0x4b2b00){_0x4f863c=_0x4f863c-0x1e5;let _0x4b1d18=_0x5d4d86[_0x4f863c];return _0x4b1d18;},_0x4f86(_0x12cd16,_0xc495b3);}async function fetchRerankModels(){const _0x59e841=_0x3413e8,{url:_0x4c7f7,apiKey:_0x1efa77}=settings[_0x59e841(0x1f8)];if(!_0x4c7f7||!_0x1efa77)throw new Error(_0x59e841(0x23e));const _0x5cec68=getRerankBaseUrl(_0x4c7f7),_0x2a0042=_0x5cec68+'/v1/models';console[_0x59e841(0x26e)](_0x59e841(0x1e9)+_0x2a0042+'\x20获取模型列表...');const _0x10968c=await fetch(_0x2a0042,{'method':_0x59e841(0x200),'headers':{'Authorization':_0x59e841(0x222)+_0x1efa77}});if(!_0x10968c['ok']){const _0x21775c=await _0x10968c['text']();throw new Error(_0x59e841(0x1fd)+_0x10968c[_0x59e841(0x20b)]+_0x59e841(0x243)+_0x21775c);}const _0x54d696=await _0x10968c[_0x59e841(0x256)]();if(!_0x54d696['data']||!Array[_0x59e841(0x24c)](_0x54d696[_0x59e841(0x221)]))throw new Error(_0x59e841(0x276));return _0x54d696[_0x59e841(0x221)]['map'](_0x506bdb=>_0x506bdb['id'])[_0x59e841(0x1e7)]();}function getApiEndpointUrl(_0x2d4f32=![]){const _0x3beb88=_0x3413e8,{apiEndpoint:_0x2e1952,customApiUrl:_0x544d4c}=settings['retrieval'];let _0x21c81f;switch(_0x2e1952){case _0x3beb88(0x268):_0x21c81f=_0x3beb88(0x21e);break;case _0x3beb88(0x1f1):case _0x3beb88(0x277):_0x21c81f=_0x544d4c;break;default:_0x21c81f=_0x3beb88(0x21e);break;}if(_0x2d4f32)return _0x21c81f;return getSanitizedBaseUrl(_0x21c81f)+'/v1/embeddings';}function getApiHeaders(){const _0x4084da=_0x3413e8,_0x22d7bc={'Content-Type':_0x4084da(0x201)},{apiKey:_0x417a27,apiEndpoint:_0x244c1d}=settings[_0x4084da(0x213)];switch(_0x244c1d){case _0x4084da(0x268):case'custom':_0x22d7bc[_0x4084da(0x210)]=_0x4084da(0x222)+_0x417a27;break;case _0x4084da(0x1f1):_0x22d7bc[_0x4084da(0x25d)]=_0x417a27;break;}return _0x22d7bc;}async function getEmbeddings(_0x18a4ae){const _0x795df6=_0x3413e8;if(!settings['retrieval'][_0x795df6(0x236)])throw new Error(_0x795df6(0x216));const _0xe504d3=getApiEndpointUrl(),_0x51deee=getApiHeaders(),_0x1e61b5=settings[_0x795df6(0x213)][_0x795df6(0x211)],_0x3e88de=settings[_0x795df6(0x213)][_0x795df6(0x25a)]||0x5,_0x3d8d2d=[];for(let _0x5609c0=0x0;_0x5609c0<_0x18a4ae['length'];_0x5609c0+=_0x3e88de){const _0x503d90=_0x18a4ae[_0x795df6(0x229)](_0x5609c0,_0x5609c0+_0x3e88de),_0x2c5331=await fetch(_0xe504d3,{'method':_0x795df6(0x24a),'headers':_0x51deee,'body':JSON[_0x795df6(0x259)]({'input':_0x503d90,'model':_0x1e61b5})});if(!_0x2c5331['ok']){const _0x54f6ba=await _0x2c5331[_0x795df6(0x1eb)]();throw new Error(_0x795df6(0x22d)+_0x2c5331[_0x795df6(0x20b)]+':\x20'+_0x54f6ba);}const _0xd12b4a=await _0x2c5331['json']();_0x3d8d2d[_0x795df6(0x25c)](..._0xd12b4a['data'][_0x795df6(0x1f6)](_0x1ceb57=>_0x1ceb57[_0x795df6(0x208)])),_0x5609c0+_0x3e88de<_0x18a4ae['length']&&await new Promise(_0x597413=>setTimeout(_0x597413,0xc8));}return _0x3d8d2d;}async function queryVectors(_0x111910){const _0x4784c7=_0x3413e8;console[_0x4784c7(0x26e)](_0x4784c7(0x1ff));const _0x19d6c4=getCollectionId();console[_0x4784c7(0x26e)](_0x4784c7(0x261)+_0x19d6c4);const _0xe0f492=(await getEmbeddings([_0x111910]))[0x0],_0x38078d={'collectionId':_0x19d6c4,'searchText':_0x111910,'topK':settings[_0x4784c7(0x20c)]['maxResults'],'threshold':settings[_0x4784c7(0x20c)][_0x4784c7(0x241)],'source':_0x4784c7(0x24d),'embeddings':{[_0x111910]:_0xe0f492}};console[_0x4784c7(0x26e)](_0x4784c7(0x1fe),JSON[_0x4784c7(0x259)](_0x38078d,null,0x2));const _0x29bad9=await fetch(_0x4784c7(0x240),{'method':_0x4784c7(0x24a),'headers':context[_0x4784c7(0x22c)](),'body':JSON[_0x4784c7(0x259)](_0x38078d)});console['log'](_0x4784c7(0x266)+_0x29bad9['status']);if(!_0x29bad9['ok']){const _0x5b9011=await _0x29bad9[_0x4784c7(0x1eb)]();console['error']('[翰林院-日志]\x20宝库查询API错误:',_0x5b9011);throw new Error(_0x4784c7(0x262)+_0x29bad9[_0x4784c7(0x20b)]+':\x20'+_0x5b9011);}const _0x4964a0=await _0x29bad9[_0x4784c7(0x256)]();console[_0x4784c7(0x26e)](_0x4784c7(0x20e),_0x4964a0);const _0x554f0c=_0x4964a0['metadata']||_0x4964a0[_0x4784c7(0x23f)]||_0x4964a0[_0x4784c7(0x221)]||[];return console[_0x4784c7(0x26e)](_0x4784c7(0x25f)+_0x554f0c[_0x4784c7(0x226)]+_0x4784c7(0x212)),_0x554f0c;}async function insertVectors(_0x245d4f){const _0x2f67c4=_0x3413e8;console[_0x2f67c4(0x26e)](_0x2f67c4(0x233));const _0x1e3653=getCollectionId();console[_0x2f67c4(0x26e)](_0x2f67c4(0x207)+_0x1e3653);const _0x215de6=_0x245d4f[_0x2f67c4(0x1f6)](_0x429601=>_0x429601[_0x2f67c4(0x1eb)]);if(_0x215de6[_0x2f67c4(0x226)]===0x0)return console[_0x2f67c4(0x26e)](_0x2f67c4(0x1fb)),{'success':!![],'count':0x0};console[_0x2f67c4(0x26e)]('[翰林院-日志]\x20正在为\x20'+_0x215de6[_0x2f67c4(0x226)]+_0x2f67c4(0x234));const _0x229ea0=await getEmbeddings(_0x215de6);console[_0x2f67c4(0x26e)](_0x2f67c4(0x1ea)+_0x229ea0[_0x2f67c4(0x226)]+'\x20个向量。');const _0x148c6a=_0x245d4f[_0x2f67c4(0x1f6)]((_0x579090,_0x4a7dd0)=>({'hash':generateHash(_0x579090['text']+Date[_0x2f67c4(0x204)]()+_0x4a7dd0),'text':_0x579090[_0x2f67c4(0x1eb)]})),_0x4fa414=_0x148c6a[_0x2f67c4(0x24b)]((_0x3f5ba0,_0xb05e05,_0x196ab9)=>{const _0x160642=_0x2f67c4;return _0x3f5ba0[_0xb05e05[_0x160642(0x1eb)]]=_0x229ea0[_0x196ab9],_0x3f5ba0;},{}),_0x1698d5={'collectionId':_0x1e3653,'items':_0x148c6a,'source':_0x2f67c4(0x24d),'embeddings':_0x4fa414};console[_0x2f67c4(0x26e)](_0x2f67c4(0x23c),{'collectionId':_0x1698d5['collectionId'],'source':_0x1698d5['source'],'itemCount':_0x1698d5[_0x2f67c4(0x25b)]['length'],'embeddingCount':Object[_0x2f67c4(0x21f)](_0x1698d5['embeddings'])[_0x2f67c4(0x226)]});const _0x1a8cd1=await fetch(_0x2f67c4(0x270),{'method':_0x2f67c4(0x24a),'headers':context[_0x2f67c4(0x22c)](),'body':JSON[_0x2f67c4(0x259)](_0x1698d5)});console[_0x2f67c4(0x26e)]('[翰林院-日志]\x20/api/vector/insert\x20响应状态:\x20'+_0x1a8cd1['status']);if(!_0x1a8cd1['ok']){const _0xfc33c9=await _0x1a8cd1[_0x2f67c4(0x1eb)]();console[_0x2f67c4(0x214)]('[翰林院-日志]\x20忆识存入API错误:',_0xfc33c9);throw new Error(_0x2f67c4(0x22a)+_0x1a8cd1[_0x2f67c4(0x20b)]+':\x20'+_0xfc33c9);}return console[_0x2f67c4(0x26e)](_0x2f67c4(0x26b)),{'success':!![],'count':_0x148c6a[_0x2f67c4(0x226)]};}async function testApiConnection(){await getEmbeddings(['测试连接']);}async function getVectorCount(){const _0x394d5a=_0x3413e8;console[_0x394d5a(0x26e)]('[翰林院-日志]\x20开始获取向量总数...');const _0x503d43=getCollectionId();console[_0x394d5a(0x26e)](_0x394d5a(0x22f)+_0x503d43);const _0x43fe84={'collectionId':_0x503d43,'source':'webllm','embeddings':{}};console[_0x394d5a(0x26e)](_0x394d5a(0x205),JSON['stringify'](_0x43fe84,null,0x2));const _0x41bb50=await fetch(_0x394d5a(0x239),{'method':'POST','headers':context[_0x394d5a(0x22c)](),'body':JSON[_0x394d5a(0x259)](_0x43fe84)});console[_0x394d5a(0x26e)]('[翰林院-日志]\x20/api/vector/list\x20响应状态:\x20'+_0x41bb50['status']);if(!_0x41bb50['ok']){const _0xf18e1f=await _0x41bb50[_0x394d5a(0x1eb)]();return console[_0x394d5a(0x214)]('[翰林院-日志]\x20获取向量列表API错误:',_0xf18e1f),0x0;}const _0x5e29b8=await _0x41bb50['json']();console[_0x394d5a(0x26e)](_0x394d5a(0x257),_0x5e29b8);let _0x4f62ce=0x0;if(Array[_0x394d5a(0x24c)](_0x5e29b8))_0x4f62ce=_0x5e29b8['length'];else _0x5e29b8&&_0x5e29b8['hashes']&&(_0x4f62ce=_0x5e29b8[_0x394d5a(0x1ed)][_0x394d5a(0x226)]);return console[_0x394d5a(0x26e)](_0x394d5a(0x264)+_0x4f62ce),_0x4f62ce;}async function purgeStorage(){const _0x46b223=_0x3413e8;console[_0x46b223(0x26e)](_0x46b223(0x260));const _0x199487=getCollectionId();console[_0x46b223(0x26e)](_0x46b223(0x224)+_0x199487);const _0x4604e0={'collectionId':_0x199487};console[_0x46b223(0x26e)](_0x46b223(0x26c),JSON[_0x46b223(0x259)](_0x4604e0,null,0x2));const _0xa9dc49=await fetch('/api/vector/purge',{'method':_0x46b223(0x24a),'headers':context[_0x46b223(0x22c)](),'body':JSON[_0x46b223(0x259)](_0x4604e0)});console[_0x46b223(0x26e)](_0x46b223(0x1f3)+_0xa9dc49[_0x46b223(0x20b)]);if(!_0xa9dc49['ok']){const _0x4977c1=await _0xa9dc49[_0x46b223(0x1eb)]();console['error'](_0x46b223(0x245),_0x4977c1);}else console[_0x46b223(0x26e)](_0x46b223(0x1f5));return _0xa9dc49['ok'];}function getMessagesForCondensation(_0x51a914=null){const _0x2bd131=_0x3413e8;if(!settings[_0x2bd131(0x1f4)][_0x2bd131(0x206)])return showNotification(_0x2bd131(0x1e8),_0x2bd131(0x1ee)),[];const {layerStart:_0x2dda84,layerEnd:_0x2df5a9}=settings[_0x2bd131(0x1f4)],_0x40d450=_0x51a914||settings[_0x2bd131(0x1f4)][_0x2bd131(0x21b)],_0xf24f8=context['chat']['length'],_0x5051f5=Math[_0x2bd131(0x247)](0x0,_0x2dda84-0x1),_0x3cb240=Math[_0x2bd131(0x246)](_0xf24f8,_0x2df5a9),_0x2e80e9=context[_0x2bd131(0x249)][_0x2bd131(0x229)](_0x5051f5,_0x3cb240);return _0x2e80e9[_0x2bd131(0x218)](_0x1be70a=>{const _0x43ae21=_0x2bd131,_0x1f4ed0=_0x1be70a[_0x43ae21(0x250)]===!![],_0x473a78=_0x1be70a[_0x43ae21(0x250)]===![];if(!_0x1be70a[_0x43ae21(0x220)]||!_0x1be70a[_0x43ae21(0x220)][_0x43ae21(0x21d)]())return![];return _0x40d450[_0x43ae21(0x238)]&&_0x1f4ed0||_0x40d450['ai']&&_0x473a78;});}async function processCondensation(_0x62c67f){const _0x77bb14=_0x3413e8;if(!_0x62c67f||_0x62c67f['length']===0x0)return{'success':![],'error':_0x77bb14(0x20a)};const _0x3d1671=_0x62c67f[_0x77bb14(0x1f6)](_0xec38eb=>({'text':(_0xec38eb['mes']||'')[_0x77bb14(0x219)](/<[^>]*>/g,'')[_0x77bb14(0x21d)]()}))[_0x77bb14(0x218)](_0xdc449f=>_0xdc449f[_0x77bb14(0x1eb)][_0x77bb14(0x226)]>0x0),_0x42522f=_0x3d1671['flatMap'](_0x5d907a=>splitIntoChunks(_0x5d907a['text'])['map'](_0x1d2bce=>({'text':_0x1d2bce})));if(_0x42522f['length']===0x0)return{'success':![],'error':'No\x20valid\x20chunks\x20generated.'};return await insertVectors(_0x42522f);}async function rerankResults(_0x1b4896,_0x414fe5,_0x2ea1ae){const _0x269ae1=_0x3413e8;if(!_0x2ea1ae[_0x269ae1(0x1f8)][_0x269ae1(0x206)]||_0x1b4896[_0x269ae1(0x226)]===0x0)return _0x1b4896[_0x269ae1(0x1e7)]((_0x2327ce,_0x38422d)=>(_0x38422d[_0x269ae1(0x275)]||0x0)-(_0x2327ce[_0x269ae1(0x275)]||0x0)),_0x1b4896['slice'](0x0,_0x2ea1ae[_0x269ae1(0x20c)]['maxResults']);console[_0x269ae1(0x26e)]('[翰林院-Rerank]\x20开始重排序...');try{const _0x24a0e2=_0x1b4896[_0x269ae1(0x1f6)]((_0x47b6c4,_0x366419)=>({'text':_0x47b6c4['text'],'original_index':_0x366419})),_0x590fb3=getRerankBaseUrl(_0x2ea1ae['rerank'][_0x269ae1(0x225)]),_0x3d4431=_0x590fb3+_0x269ae1(0x23a),_0x104e8f=await fetch(_0x3d4431,{'method':_0x269ae1(0x24a),'headers':{'Content-Type':_0x269ae1(0x201),'Authorization':_0x269ae1(0x222)+_0x2ea1ae[_0x269ae1(0x1f8)][_0x269ae1(0x236)]},'body':JSON['stringify']({'query':_0x414fe5,'documents':_0x24a0e2['map'](_0x2f2c40=>_0x2f2c40[_0x269ae1(0x1eb)]),'model':_0x2ea1ae[_0x269ae1(0x1f8)][_0x269ae1(0x24f)],'top_n':_0x2ea1ae[_0x269ae1(0x1f8)][_0x269ae1(0x267)]})});if(!_0x104e8f['ok']){const _0x59c461=await _0x104e8f['text']();throw new Error('Rerank\x20API\x20请求失败\x20('+_0x104e8f[_0x269ae1(0x20b)]+'):\x20'+_0x59c461);}const _0x580bd6=await _0x104e8f[_0x269ae1(0x256)]();console['log']('[翰林院-Rerank]\x20Rerank\x20API\x20响应:',_0x580bd6);const _0x4a8697=_0x1b4896['map']((_0x7c2d23,_0x7b285f)=>({..._0x7c2d23,'original_index':_0x7b285f})),_0x56058f=_0x2ea1ae['rerank']['hybrid_alpha'],_0x1b1b1c=_0x4a8697[_0x269ae1(0x1f6)](_0x3287f6=>{const _0x132463=_0x269ae1,_0x2b267c=_0x580bd6[_0x132463(0x23f)][_0x132463(0x253)](_0x3d87ab=>_0x3d87ab[_0x132463(0x26a)]===_0x3287f6[_0x132463(0x248)]),_0x51c8dc=_0x2b267c?_0x2b267c['relevance_score']:0x0,_0x947376=_0x51c8dc*_0x56058f+(_0x3287f6[_0x132463(0x275)]||0x0)*(0x1-_0x56058f);return{..._0x3287f6,'hybrid_score':_0x947376,'rerank_score':_0x51c8dc};});return _0x1b1b1c[_0x269ae1(0x1e7)]((_0x1337d6,_0xa950fa)=>(_0xa950fa['hybrid_score']||0x0)-(_0x1337d6[_0x269ae1(0x254)]||0x0)),console[_0x269ae1(0x26e)](_0x269ae1(0x1e6),_0x1b1b1c[_0x269ae1(0x229)](0x0,_0x2ea1ae[_0x269ae1(0x1f8)][_0x269ae1(0x267)])),_0x2ea1ae[_0x269ae1(0x1f8)][_0x269ae1(0x265)]&&showNotification(_0x269ae1(0x22b),_0x269ae1(0x232)),_0x1b1b1c['slice'](0x0,_0x2ea1ae[_0x269ae1(0x1f8)][_0x269ae1(0x267)]);}catch(_0x3043c8){return console[_0x269ae1(0x214)](_0x269ae1(0x274),_0x3043c8),_0x2ea1ae[_0x269ae1(0x1f8)][_0x269ae1(0x265)]&&showNotification(_0x269ae1(0x21c)+_0x3043c8[_0x269ae1(0x1f7)],_0x269ae1(0x214)),_0x1b4896[_0x269ae1(0x1e7)]((_0x1764ab,_0x7405f5)=>(_0x7405f5['score']||0x0)-(_0x1764ab[_0x269ae1(0x275)]||0x0)),_0x1b4896[_0x269ae1(0x229)](0x0,_0x2ea1ae[_0x269ae1(0x20c)][_0x269ae1(0x271)]);}}async function rearrangeChat(_0x14fefd,_0x50af3c,_0x3ecdf8,_0x4ca33c){const _0x3b36c5=_0x3413e8;setExtensionPrompt('HANLINYUAN_RAG','',settings['injection']['position'],settings[_0x3b36c5(0x22e)][_0x3b36c5(0x1ef)],![],settings[_0x3b36c5(0x22e)][_0x3b36c5(0x237)]);if(_0x4ca33c===_0x3b36c5(0x269)||!settings[_0x3b36c5(0x213)][_0x3b36c5(0x206)])return;const _0x2ad1bc=_0x14fefd['slice'](-settings[_0x3b36c5(0x20c)][_0x3b36c5(0x258)]);if(_0x2ad1bc[_0x3b36c5(0x226)]===0x0)return;const _0x90a208=_0x2ad1bc[_0x3b36c5(0x1f6)](_0x2b6326=>_0x2b6326['mes'])[_0x3b36c5(0x228)]('\x20')['replace'](/<[^>]*>/g,'')[_0x3b36c5(0x21d)]();if(!_0x90a208)return;try{const _0x252e94=await queryVectors(_0x90a208);if(_0x252e94['length']===0x0)return;const _0x29c0f0=await rerankResults(_0x252e94,_0x90a208,settings);if(_0x29c0f0[_0x3b36c5(0x226)]===0x0)return;const _0x127f7b=_0x29c0f0['map'](_0x4a83a7=>_0x4a83a7[_0x3b36c5(0x1eb)])[_0x3b36c5(0x228)]('\x0a\x0a'),_0x4fc43c=settings[_0x3b36c5(0x22e)][_0x3b36c5(0x252)][_0x3b36c5(0x219)]('{{text}}',_0x127f7b);setExtensionPrompt('HANLINYUAN_RAG',_0x4fc43c,settings[_0x3b36c5(0x22e)]['position'],settings[_0x3b36c5(0x22e)]['depth'],![],settings[_0x3b36c5(0x22e)][_0x3b36c5(0x237)]);}catch(_0x3031d8){console[_0x3b36c5(0x214)](_0x3b36c5(0x244),_0x3031d8);if(settings[_0x3b36c5(0x213)][_0x3b36c5(0x265)])showNotification(_0x3b36c5(0x25e)+_0x3031d8[_0x3b36c5(0x1f7)],_0x3b36c5(0x214));}}