From d640ec4642d74aab7a8c31b0e2d9ba99a6391aa5 Mon Sep 17 00:00:00 2001 From: Wx-2025 <351320169@qq.com> Date: Wed, 23 Jul 2025 06:23:50 +0800 Subject: [PATCH] Update rag-processor.js --- core/rag-processor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/rag-processor.js b/core/rag-processor.js index b96d170..5d0ce09 100644 --- a/core/rag-processor.js +++ b/core/rag-processor.js @@ -1 +1 @@ -'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));}} +'use strict';const _0x3f5590=_0x555a;(function(_0x4c94f3,_0x492e3c){const _0xcffccf=_0x555a,_0x3ff047=_0x4c94f3();while(!![]){try{const _0x4203ef=-parseInt(_0xcffccf(0x1a9))/0x1+parseInt(_0xcffccf(0x15b))/0x2+parseInt(_0xcffccf(0x1ae))/0x3+parseInt(_0xcffccf(0x158))/0x4+parseInt(_0xcffccf(0x1e1))/0x5+-parseInt(_0xcffccf(0x197))/0x6*(-parseInt(_0xcffccf(0x1a8))/0x7)+-parseInt(_0xcffccf(0x1b8))/0x8*(parseInt(_0xcffccf(0x1a1))/0x9);if(_0x4203ef===_0x492e3c)break;else _0x3ff047['push'](_0x3ff047['shift']());}catch(_0x177488){_0x3ff047['push'](_0x3ff047['shift']());}}}(_0x39cc,0x1f6b5));import{extension_prompt_roles,setExtensionPrompt}from'/script.js';function _0x555a(_0x32e683,_0x365ee1){const _0x39cc57=_0x39cc();return _0x555a=function(_0x555abe,_0x2df5a6){_0x555abe=_0x555abe-0x152;let _0x48ced7=_0x39cc57[_0x555abe];return _0x48ced7;},_0x555a(_0x32e683,_0x365ee1);}import*as _0x518e01 from'./utils/context-utils.js';import{defaultSettings as _0x338568}from'./rag-settings.js';const MODULE_NAME='hanlinyuan-rag-core',OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME=_0x3f5590(0x1af);let context=null,settings=null;export{initialize,getSettings,saveSettings,resetSettings,testApiConnection,fetchEmbeddingModels,fetchRerankModels,getVectorCount,purgeStorage,getMessagesForCondensation,processCondensation,ingestTextToHanlinyuan,getCollectionId};function _0x39cc(){const _0x89c7d7=['join','queryMessageCount','[翰林院-日志]\x20没有需要插入的文本块,操作完成。','获取模型列表失败\x20(','replace','[翰林院-日志]\x20清空宝库API错误:','[翰林院]\x20处理外部文书时发生错误:','azure','[翰林院-日志]\x20正在为\x20','150szBETd','min','embedding','\x20条结果。','mes','top_n','rerank','凝识之权未开启','url','floor','354852TRFErV','\x20个文本块获取向量...','saveSettingsDebounced','retrieval','/api/vector/purge','GET','enabled','68054pwCbWt','206795yODbKk','[翰林院-日志]\x20开始向量查询\x20(采用最终API交互模式)...','/v1/embeddings','reduce','object','377328hqlfDl','vectors_rearrangeChat','[翰林院-Rerank]\x20外部Rerank失败,将仅使用内部加权。','getContext','API\x20URL\x20或\x20Key\x20未提供。','外部Rerank完成','charCodeAt','position','忆识存入API错误\x20','maxResults','96hCjLql','application/json','api-key','notify','log','Rerank模型API的响应格式无效:\x20未找到\x20\x27data\x27\x20数组。','HANLINYUAN_RAG','Bearer\x20','now','/v1','rerank_score','[翰林院-日志]\x20开始向量插入...','lorebook','[翰林院-日志]\x20/api/vector/purge\x20响应状态:\x20','original_index','quiet','length','[翰林院-日志]\x20/api/vector/list\x20响应状态:\x20','isArray','depth_role','[翰林院-日志]\x20清空宝库API调用成功。','/embeddings','final_score','condensation','[翰林院]\x20未能获取SillyTavern上下文,初始化失败。','endsWith','extensionSettings','embeddings','warning','webllm','POST','stringify','hashes','[翰林院-日志]\x20开始获取向量总数...','status','forEach','[翰林院-日志]\x20获取向量列表API错误:','embeddingModel','info','/v1/rerank','json','319565EYlgby','toString','[翰林院-日志]\x20/api/vector/list\x20响应内容:','map','[翰林院-日志]\x20发送到\x20/api/vector/list\x20的请求体:','/api/vector/insert','message','is_user','data','push','翰林院忆识核心已启动\x20(V5.1-借壳上市版),已注册全局函数:\x20','[翰林院-Rerank]\x20元数据加权排序完成。','856728awqjqz','source','神力获取失败\x20','323406TavmOi','metadata','unknown','filter','manual','matchThreshold','trim','[翰林院-日志]\x20宝库查询API错误:','relevance_score','[翰林院-日志]\x20/api/vector/insert\x20响应状态:\x20','error','findIndex','[翰林院-日志]\x20发送到\x20/api/vector/query\x20的请求体:','Rerank失败:\x20','/rerank','substring','):\x20','getRequestHeaders','batchSize','\x20获取模型列表...','/v1/models','depth','[翰林院-日志]\x20清空目标集合ID:\x20','advanced','openai','model','function','[翰林院-日志]\x20查询目标集合ID:\x20','slice','text','injection','[翰林院-Rerank]\x20开始外部API重排序...','max','No\x20messages\x20to\x20process.','send_date','宝库查询API错误\x20','{{text}}','/api/vector/query','Rerank\x20API\x20请求失败\x20(','chat','\x20个向量。','测试连接','/api/vector/list','items','Authorization','sort','[翰林院-Rerank]\x20正在从\x20','count','核心未初始化','https://api.openai.com','toISOString'];_0x39cc=function(){return _0x89c7d7;};return _0x39cc();}function initialize(){const _0x5a5be9=_0x3f5590;context=SillyTavern[_0x5a5be9(0x1b1)]();if(!context){console[_0x5a5be9(0x165)](_0x5a5be9(0x1d0));return;}settings=getSettings();const _0x487807=window[OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME];typeof _0x487807===_0x5a5be9(0x175)?(window[OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME]=async function(..._0x201182){await rearrangeChat(..._0x201182),await _0x487807(..._0x201182);},console['log']('翰林院忆识核心已启动\x20(V5.1-和平共存版),已代理\x20'+OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME)):(window[OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME]=rearrangeChat,console[_0x5a5be9(0x1bc)](_0x5a5be9(0x156)+OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME));}async function ingestTextToHanlinyuan(_0xd659db,_0x1401ca=_0x3f5590(0x15f),_0x58dd10=null){const _0x5cbede=_0x3f5590;if(!settings)return{'success':![],'error':_0x5cbede(0x18b)};try{const _0x258faa=splitIntoChunks(_0xd659db)[_0x5cbede(0x1e4)](_0x39ba94=>{const _0x2b1564=_0x5cbede,_0x1693a8={'source':_0x1401ca,'timestamp':new Date()['toISOString']()};return _0x1401ca===_0x2b1564(0x1c4)&&_0x58dd10&&(_0x1693a8['entry']=_0x58dd10),{'text':_0x39ba94,'metadata':_0x1693a8};});if(_0x258faa[_0x5cbede(0x1c8)]===0x0)return{'success':!![]};const _0xefe218=await insertVectors(_0x258faa);return{'success':!![],'count':_0xefe218[_0x5cbede(0x18a)]};}catch(_0x2da01f){return console[_0x5cbede(0x165)](_0x5cbede(0x194),_0x2da01f),{'success':![],'error':_0x2da01f[_0x5cbede(0x152)]};}}function getSettings(){const _0x69186a=_0x3f5590;if(!context||!context[_0x69186a(0x1d2)])return structuredClone(_0x338568);let _0x2d28b4=context[_0x69186a(0x1d2)][MODULE_NAME];!_0x2d28b4&&(_0x2d28b4={},context['extensionSettings'][MODULE_NAME]=_0x2d28b4);for(const _0x28fdfb in _0x338568){if(_0x2d28b4[_0x28fdfb]===undefined)_0x2d28b4[_0x28fdfb]=structuredClone(_0x338568[_0x28fdfb]);else{if(typeof _0x338568[_0x28fdfb]===_0x69186a(0x1ad)&&!Array[_0x69186a(0x1ca)](_0x338568[_0x28fdfb])&&_0x338568[_0x28fdfb]!==null)for(const _0x2a4615 in _0x338568[_0x28fdfb]){_0x2d28b4[_0x28fdfb][_0x2a4615]===undefined&&(_0x2d28b4[_0x28fdfb][_0x2a4615]=_0x338568[_0x28fdfb][_0x2a4615]);}}}return _0x2d28b4;}function saveSettings(){const _0x526e55=_0x3f5590;if(context)context[_0x526e55(0x1a3)]();}function resetSettings(){context&&(context['extensionSettings'][MODULE_NAME]=structuredClone(_0x338568),saveSettings());}function showNotification(_0x19244a,_0x23a9f4=_0x3f5590(0x1de)){toastr[_0x23a9f4](_0x19244a);}function splitIntoChunks(_0x2a7a38){const _0x180699=_0x3f5590,{chunkSize:_0x32b849,overlap:_0xe6f3c7}=settings[_0x180699(0x172)],_0x4cbe8c=[];if(!_0x2a7a38||_0x32b849<=0x0)return _0x4cbe8c;let _0x2da619=0x0;while(_0x2da619<_0x2a7a38['length']){const _0x5c17b8=Math[_0x180699(0x198)](_0x2da619+_0x32b849,_0x2a7a38[_0x180699(0x1c8)]);_0x4cbe8c['push'](_0x2a7a38[_0x180699(0x16a)](_0x2da619,_0x5c17b8)),_0x2da619+=_0x32b849-_0xe6f3c7;}return _0x4cbe8c;}import{getCollectionId}from'./utils/context-utils.js';function generateHash(_0x32d809){const _0x2e212c=_0x3f5590;let _0x2fbe5e=0x0;for(let _0x540460=0x0;_0x540460<_0x32d809[_0x2e212c(0x1c8)];_0x540460++){const _0x2b272b=_0x32d809[_0x2e212c(0x1b4)](_0x540460);_0x2fbe5e=(_0x2fbe5e<<0x5)-_0x2fbe5e+_0x2b272b,_0x2fbe5e=_0x2fbe5e&_0x2fbe5e;}return Math['abs'](_0x2fbe5e)[_0x2e212c(0x1e2)](0x24);}function getSanitizedBaseUrl(_0x4c41d0){const _0x482359=_0x3f5590;let _0x568e98=_0x4c41d0[_0x482359(0x161)]();return _0x568e98[_0x482359(0x1d1)]('/')&&(_0x568e98=_0x568e98[_0x482359(0x177)](0x0,-0x1)),_0x568e98['endsWith'](_0x482359(0x1c1))&&(_0x568e98=_0x568e98[_0x482359(0x177)](0x0,-0x3)),_0x568e98[_0x482359(0x1d1)](_0x482359(0x1cd))&&(_0x568e98=_0x568e98[_0x482359(0x177)](0x0,-0xb)),_0x568e98;}async function fetchEmbeddingModels(){const _0x2ec184=_0x3f5590,{apiKey:_0x39b323}=settings['retrieval'],_0x5a695d=getApiEndpointUrl(!![]);if(!_0x5a695d||!_0x39b323)throw new Error(_0x2ec184(0x1b2));const _0x432f3d=getSanitizedBaseUrl(_0x5a695d),_0x22e5ea=_0x432f3d+_0x2ec184(0x16f);console['log']('[翰林院]\x20正在从\x20'+_0x22e5ea+_0x2ec184(0x16e));const _0xc47b98=await fetch(_0x22e5ea,{'method':_0x2ec184(0x1a6),'headers':getApiHeaders()});if(!_0xc47b98['ok']){const _0x2aa617=await _0xc47b98[_0x2ec184(0x178)]();throw new Error(_0x2ec184(0x191)+_0xc47b98[_0x2ec184(0x1da)]+_0x2ec184(0x16b)+_0x2aa617);}const _0x24e182=await _0xc47b98[_0x2ec184(0x1e0)]();if(!_0x24e182[_0x2ec184(0x154)]||!Array[_0x2ec184(0x1ca)](_0x24e182['data']))throw new Error('模型API的响应格式无效:\x20未找到\x20\x27data\x27\x20数组。');return _0x24e182[_0x2ec184(0x154)][_0x2ec184(0x1e4)](_0x5a1bd3=>_0x5a1bd3['id'])['sort']();}function getRerankBaseUrl(_0x3207ea){const _0x520c5a=_0x3f5590;let _0x3cfa26=_0x3207ea['trim']();return _0x3cfa26['endsWith']('/')&&(_0x3cfa26=_0x3cfa26[_0x520c5a(0x177)](0x0,-0x1)),_0x3cfa26['endsWith'](_0x520c5a(0x1c1))&&(_0x3cfa26=_0x3cfa26[_0x520c5a(0x177)](0x0,-0x3)),_0x3cfa26[_0x520c5a(0x1d1)](_0x520c5a(0x169))&&(_0x3cfa26=_0x3cfa26['slice'](0x0,-0x7)),_0x3cfa26;}async function fetchRerankModels(){const _0x52979f=_0x3f5590,{url:_0x14d217,apiKey:_0x109aa0}=settings[_0x52979f(0x19d)];if(!_0x14d217||!_0x109aa0)throw new Error('Rerank\x20API\x20URL\x20或\x20Key\x20未提供。');const _0x18b5c6=getRerankBaseUrl(_0x14d217),_0x478121=_0x18b5c6+_0x52979f(0x16f);console[_0x52979f(0x1bc)](_0x52979f(0x189)+_0x478121+_0x52979f(0x16e));const _0x29358a=await fetch(_0x478121,{'method':'GET','headers':{'Authorization':_0x52979f(0x1bf)+_0x109aa0}});if(!_0x29358a['ok']){const _0x3b29e9=await _0x29358a[_0x52979f(0x178)]();throw new Error('获取Rerank模型列表失败\x20('+_0x29358a[_0x52979f(0x1da)]+_0x52979f(0x16b)+_0x3b29e9);}const _0x2811a0=await _0x29358a['json']();if(!_0x2811a0['data']||!Array['isArray'](_0x2811a0['data']))throw new Error(_0x52979f(0x1bd));return _0x2811a0[_0x52979f(0x154)]['map'](_0x348bb7=>_0x348bb7['id'])[_0x52979f(0x188)]();}function getApiEndpointUrl(_0x258970=![]){const _0x7f093a=_0x3f5590,{apiEndpoint:_0x51366f,customApiUrl:_0x2a8a0e}=settings[_0x7f093a(0x1a4)];let _0x35da17;switch(_0x51366f){case _0x7f093a(0x173):_0x35da17=_0x7f093a(0x18c);break;case'azure':case'custom':_0x35da17=_0x2a8a0e;break;default:_0x35da17=_0x7f093a(0x18c);break;}if(_0x258970)return _0x35da17;return getSanitizedBaseUrl(_0x35da17)+_0x7f093a(0x1ab);}function getApiHeaders(){const _0x3352aa=_0x3f5590,_0x324e88={'Content-Type':_0x3352aa(0x1b9)},{apiKey:_0x2d73f3,apiEndpoint:_0x4b03d7}=settings['retrieval'];switch(_0x4b03d7){case'openai':case'custom':_0x324e88[_0x3352aa(0x187)]=_0x3352aa(0x1bf)+_0x2d73f3;break;case _0x3352aa(0x195):_0x324e88[_0x3352aa(0x1ba)]=_0x2d73f3;break;}return _0x324e88;}async function getEmbeddings(_0x21dde){const _0x285755=_0x3f5590;if(!settings[_0x285755(0x1a4)]['apiKey'])throw new Error('请先配置API\x20Key');const _0x3d1a07=getApiEndpointUrl(),_0x496a89=getApiHeaders(),_0x3ce888=settings['retrieval'][_0x285755(0x1dd)],_0x56dd75=settings[_0x285755(0x1a4)][_0x285755(0x16d)]||0x5,_0xb384b5=[];for(let _0x5f4c29=0x0;_0x5f4c29<_0x21dde[_0x285755(0x1c8)];_0x5f4c29+=_0x56dd75){const _0x4998e3=_0x21dde['slice'](_0x5f4c29,_0x5f4c29+_0x56dd75),_0x5674d1=await fetch(_0x3d1a07,{'method':_0x285755(0x1d6),'headers':_0x496a89,'body':JSON[_0x285755(0x1d7)]({'input':_0x4998e3,'model':_0x3ce888})});if(!_0x5674d1['ok']){const _0x298606=await _0x5674d1['text']();throw new Error(_0x285755(0x15a)+_0x5674d1[_0x285755(0x1da)]+':\x20'+_0x298606);}const _0x269d90=await _0x5674d1[_0x285755(0x1e0)]();_0xb384b5[_0x285755(0x155)](..._0x269d90['data'][_0x285755(0x1e4)](_0x9cdabb=>_0x9cdabb[_0x285755(0x199)])),_0x5f4c29+_0x56dd75<_0x21dde[_0x285755(0x1c8)]&&await new Promise(_0x4ba547=>setTimeout(_0x4ba547,0xc8));}return _0xb384b5;}async function queryVectors(_0x276186){const _0x20c2af=_0x3f5590;console[_0x20c2af(0x1bc)](_0x20c2af(0x1aa));const _0xa4e144=getCollectionId();console[_0x20c2af(0x1bc)](_0x20c2af(0x176)+_0xa4e144);const _0x36e4ba=(await getEmbeddings([_0x276186]))[0x0],_0x4430aa={'collectionId':_0xa4e144,'searchText':_0x276186,'topK':settings['advanced'][_0x20c2af(0x1b7)],'threshold':settings[_0x20c2af(0x172)][_0x20c2af(0x160)],'source':_0x20c2af(0x1d5),'embeddings':{[_0x276186]:_0x36e4ba}};console[_0x20c2af(0x1bc)](_0x20c2af(0x167),JSON['stringify'](_0x4430aa,null,0x2));const _0x9cf018=await fetch(_0x20c2af(0x180),{'method':'POST','headers':context[_0x20c2af(0x16c)](),'body':JSON[_0x20c2af(0x1d7)](_0x4430aa)});console['log']('[翰林院-日志]\x20/api/vector/query\x20响应状态:\x20'+_0x9cf018[_0x20c2af(0x1da)]);if(!_0x9cf018['ok']){const _0x44264f=await _0x9cf018[_0x20c2af(0x178)]();console[_0x20c2af(0x165)](_0x20c2af(0x162),_0x44264f);throw new Error(_0x20c2af(0x17e)+_0x9cf018['status']+':\x20'+_0x44264f);}const _0x418a47=await _0x9cf018[_0x20c2af(0x1e0)]();console['log']('[翰林院-日志]\x20/api/vector/query\x20响应内容:',_0x418a47);const _0x2a369e=_0x418a47[_0x20c2af(0x15c)]||_0x418a47['results']||_0x418a47[_0x20c2af(0x154)]||[];return console[_0x20c2af(0x1bc)]('[翰林院-日志]\x20查询成功,返回\x20'+_0x2a369e[_0x20c2af(0x1c8)]+_0x20c2af(0x19a)),_0x2a369e;}async function insertVectors(_0x35684c){const _0x126ad3=_0x3f5590;console['log'](_0x126ad3(0x1c3));const _0x3fb5ff=getCollectionId();console[_0x126ad3(0x1bc)]('[翰林院-日志]\x20插入目标集合ID:\x20'+_0x3fb5ff);const _0x42f7b0=_0x35684c[_0x126ad3(0x1e4)](_0x24224d=>_0x24224d[_0x126ad3(0x178)]);if(_0x42f7b0[_0x126ad3(0x1c8)]===0x0)return console[_0x126ad3(0x1bc)](_0x126ad3(0x190)),{'success':!![],'count':0x0};console[_0x126ad3(0x1bc)](_0x126ad3(0x196)+_0x42f7b0[_0x126ad3(0x1c8)]+_0x126ad3(0x1a2));const _0x37723e=await getEmbeddings(_0x42f7b0);console[_0x126ad3(0x1bc)]('[翰林院-日志]\x20成功获取\x20'+_0x37723e[_0x126ad3(0x1c8)]+_0x126ad3(0x183));const _0xeb2818=_0x35684c[_0x126ad3(0x1e4)]((_0x2c94a2,_0x215c7f)=>({'hash':generateHash(_0x2c94a2[_0x126ad3(0x178)]+Date[_0x126ad3(0x1c0)]()+_0x215c7f),'text':_0x2c94a2[_0x126ad3(0x178)],'metadata':_0x2c94a2[_0x126ad3(0x15c)]||{'source':_0x126ad3(0x15d),'timestamp':new Date()[_0x126ad3(0x18d)]()}})),_0x45ebfe=_0xeb2818[_0x126ad3(0x1ac)]((_0x1502d6,_0x9f803c,_0x137d35)=>{const _0x41e0cc=_0x126ad3;return _0x1502d6[_0x9f803c[_0x41e0cc(0x178)]]=_0x37723e[_0x137d35],_0x1502d6;},{}),_0x8afd32={'collectionId':_0x3fb5ff,'items':_0xeb2818,'source':_0x126ad3(0x1d5),'embeddings':_0x45ebfe};console[_0x126ad3(0x1bc)]('[翰林院-日志]\x20发送到\x20/api/vector/insert\x20的请求体\x20(仅显示条目数):',{'collectionId':_0x8afd32['collectionId'],'source':_0x8afd32[_0x126ad3(0x159)],'itemCount':_0x8afd32[_0x126ad3(0x186)][_0x126ad3(0x1c8)],'embeddingCount':Object['keys'](_0x8afd32[_0x126ad3(0x1d3)])[_0x126ad3(0x1c8)]});const _0x563079=await fetch(_0x126ad3(0x1e6),{'method':_0x126ad3(0x1d6),'headers':context[_0x126ad3(0x16c)](),'body':JSON[_0x126ad3(0x1d7)](_0x8afd32)});console[_0x126ad3(0x1bc)](_0x126ad3(0x164)+_0x563079[_0x126ad3(0x1da)]);if(!_0x563079['ok']){const _0x44d351=await _0x563079['text']();console[_0x126ad3(0x165)]('[翰林院-日志]\x20忆识存入API错误:',_0x44d351);throw new Error(_0x126ad3(0x1b6)+_0x563079[_0x126ad3(0x1da)]+':\x20'+_0x44d351);}return console['log']('[翰林院-日志]\x20忆识存入API调用成功。'),{'success':!![],'count':_0xeb2818[_0x126ad3(0x1c8)]};}async function testApiConnection(){const _0x45d578=_0x3f5590;await getEmbeddings([_0x45d578(0x184)]);}async function getVectorCount(){const _0x4c9083=_0x3f5590;console[_0x4c9083(0x1bc)](_0x4c9083(0x1d9));const _0x41acec=getCollectionId();console[_0x4c9083(0x1bc)]('[翰林院-日志]\x20统计目标集合ID:\x20'+_0x41acec);const _0x328ab8={'collectionId':_0x41acec,'source':_0x4c9083(0x1d5),'embeddings':{}};console[_0x4c9083(0x1bc)](_0x4c9083(0x1e5),JSON[_0x4c9083(0x1d7)](_0x328ab8,null,0x2));const _0x5c2440=await fetch(_0x4c9083(0x185),{'method':_0x4c9083(0x1d6),'headers':context[_0x4c9083(0x16c)](),'body':JSON['stringify'](_0x328ab8)});console['log'](_0x4c9083(0x1c9)+_0x5c2440[_0x4c9083(0x1da)]);if(!_0x5c2440['ok']){const _0xd413ac=await _0x5c2440[_0x4c9083(0x178)]();return console[_0x4c9083(0x165)](_0x4c9083(0x1dc),_0xd413ac),0x0;}const _0x4edd6a=await _0x5c2440[_0x4c9083(0x1e0)]();console['log'](_0x4c9083(0x1e3),_0x4edd6a);let _0x5c68f8=0x0;if(Array['isArray'](_0x4edd6a))_0x5c68f8=_0x4edd6a[_0x4c9083(0x1c8)];else _0x4edd6a&&_0x4edd6a['hashes']&&(_0x5c68f8=_0x4edd6a[_0x4c9083(0x1d8)][_0x4c9083(0x1c8)]);return console['log']('[翰林院-日志]\x20统计成功,向量总数:\x20'+_0x5c68f8),_0x5c68f8;}async function purgeStorage(){const _0x624315=_0x3f5590;console[_0x624315(0x1bc)]('[翰林院-日志]\x20开始清空宝库...');const _0x50fedd=getCollectionId();console[_0x624315(0x1bc)](_0x624315(0x171)+_0x50fedd);const _0x54fdd0={'collectionId':_0x50fedd};console[_0x624315(0x1bc)]('[翰林院-日志]\x20发送到\x20/api/vector/purge\x20的请求体:',JSON[_0x624315(0x1d7)](_0x54fdd0,null,0x2));const _0x161a3e=await fetch(_0x624315(0x1a5),{'method':_0x624315(0x1d6),'headers':context[_0x624315(0x16c)](),'body':JSON[_0x624315(0x1d7)](_0x54fdd0)});console[_0x624315(0x1bc)](_0x624315(0x1c5)+_0x161a3e['status']);if(!_0x161a3e['ok']){const _0x4daeab=await _0x161a3e[_0x624315(0x178)]();console['error'](_0x624315(0x193),_0x4daeab);}else console['log'](_0x624315(0x1cc));return _0x161a3e['ok'];}function getMessagesForCondensation(_0x2e502b=null){const _0x23982e=_0x3f5590;if(!settings[_0x23982e(0x1cf)][_0x23982e(0x1a7)])return showNotification(_0x23982e(0x19e),_0x23982e(0x1d4)),[];const {layerStart:_0x5b52c3,layerEnd:_0x1d5aa7}=settings[_0x23982e(0x1cf)],_0x20fe45=_0x2e502b||settings[_0x23982e(0x1cf)]['messageTypes'],_0x4fd38e=context['chat'][_0x23982e(0x1c8)],_0xc8e44e=Math[_0x23982e(0x17b)](0x0,_0x5b52c3-0x1),_0x2b4dfe=Math[_0x23982e(0x198)](_0x4fd38e,_0x1d5aa7),_0xa5cee2=context[_0x23982e(0x182)]['slice'](_0xc8e44e,_0x2b4dfe);return _0xa5cee2[_0x23982e(0x15e)](_0x43d4ff=>{const _0x5f128b=_0x23982e,_0x123637=_0x43d4ff['is_user']===!![],_0x52462e=_0x43d4ff[_0x5f128b(0x153)]===![];if(!_0x43d4ff[_0x5f128b(0x19b)]||!_0x43d4ff[_0x5f128b(0x19b)][_0x5f128b(0x161)]())return![];return _0x20fe45['user']&&_0x123637||_0x20fe45['ai']&&_0x52462e;});}async function processCondensation(_0x25d339){const _0x403996=_0x3f5590;if(!_0x25d339||_0x25d339[_0x403996(0x1c8)]===0x0)return{'success':![],'error':_0x403996(0x17c)};const _0x29b39e=[],_0x4bf73c=context[_0x403996(0x182)];for(const _0x321354 of _0x25d339){const _0x4dd2da=(_0x321354[_0x403996(0x19b)]||'')['replace'](/<[^>]*>/g,'')[_0x403996(0x161)]();if(_0x4dd2da[_0x403996(0x1c8)]===0x0)continue;const _0x522b39=_0x4bf73c[_0x403996(0x166)](_0x1155e4=>_0x1155e4===_0x321354),_0x15552d=_0x522b39!==-0x1?_0x522b39+0x1:-0x1,_0x8c5aed={'source':'chat_history','floor':_0x15552d,'is_user':_0x321354[_0x403996(0x153)],'timestamp':new Date(_0x321354[_0x403996(0x17d)])['toISOString']()},_0x2fee10=splitIntoChunks(_0x4dd2da)['map'](_0x3f577a=>({'text':_0x3f577a,'metadata':_0x8c5aed}));_0x29b39e[_0x403996(0x155)](..._0x2fee10);}if(_0x29b39e[_0x403996(0x1c8)]===0x0)return{'success':![],'error':'No\x20valid\x20chunks\x20generated.'};return await insertVectors(_0x29b39e);}async function rerankResults(_0x25aefd,_0x48d172,_0x293be1){const _0xe95076=_0x3f5590;let _0x75c5ce=_0x25aefd;if(_0x293be1[_0xe95076(0x19d)][_0xe95076(0x1a7)]&&_0x25aefd['length']>0x0){console[_0xe95076(0x1bc)](_0xe95076(0x17a));try{const _0x3cb587=_0x25aefd[_0xe95076(0x1e4)]((_0x4161a6,_0x1268df)=>({'text':_0x4161a6[_0xe95076(0x178)],'original_index':_0x1268df})),_0x49e0c8=getRerankBaseUrl(_0x293be1[_0xe95076(0x19d)][_0xe95076(0x19f)]),_0x494ba9=_0x49e0c8+_0xe95076(0x1df),_0x1c4e93=await fetch(_0x494ba9,{'method':'POST','headers':{'Content-Type':'application/json','Authorization':'Bearer\x20'+_0x293be1[_0xe95076(0x19d)]['apiKey']},'body':JSON['stringify']({'query':_0x48d172,'documents':_0x3cb587[_0xe95076(0x1e4)](_0x394183=>_0x394183[_0xe95076(0x178)]),'model':_0x293be1[_0xe95076(0x19d)][_0xe95076(0x174)],'top_n':_0x293be1[_0xe95076(0x19d)][_0xe95076(0x19c)]})});if(!_0x1c4e93['ok'])throw new Error(_0xe95076(0x181)+_0x1c4e93[_0xe95076(0x1da)]+_0xe95076(0x16b)+await _0x1c4e93[_0xe95076(0x178)]());const _0x1b955e=await _0x1c4e93[_0xe95076(0x1e0)](),_0x39f6f5=_0x25aefd[_0xe95076(0x1e4)]((_0x1f7fdc,_0xa2e38a)=>({..._0x1f7fdc,'original_index':_0xa2e38a}));_0x75c5ce=_0x39f6f5[_0xe95076(0x1e4)](_0x1d2e2e=>{const _0x6b2ae4=_0xe95076,_0x364e5e=_0x1b955e['results']['find'](_0x4651d7=>_0x4651d7['index']===_0x1d2e2e[_0x6b2ae4(0x1c6)]),_0x1223a0=_0x364e5e?_0x364e5e[_0x6b2ae4(0x163)]:0x0;return{..._0x1d2e2e,'rerank_score':_0x1223a0};});if(_0x293be1['rerank'][_0xe95076(0x1bb)])showNotification(_0xe95076(0x1b3),'success');}catch(_0x369ec5){console[_0xe95076(0x165)](_0xe95076(0x1b0),_0x369ec5);if(_0x293be1[_0xe95076(0x19d)][_0xe95076(0x1bb)])showNotification(_0xe95076(0x168)+_0x369ec5['message'],_0xe95076(0x165));_0x75c5ce['forEach'](_0x16557b=>_0x16557b[_0xe95076(0x1c2)]=0x0);}}else _0x75c5ce[_0xe95076(0x1db)](_0x2cce3b=>_0x2cce3b[_0xe95076(0x1c2)]=0x0);console[_0xe95076(0x1bc)]('[翰林院-Rerank]\x20开始元数据加权最终排序...');const _0x277dd1=context[_0xe95076(0x182)][_0xe95076(0x1c8)],_0xc03fbc=_0x293be1[_0xe95076(0x19d)]['hybrid_alpha'],_0x529cf1=_0x75c5ce['map'](_0x3deb96=>{const _0x4b8ad1=_0xe95076;let _0x1e7b7c=0x1;const _0x114bbe=_0x3deb96[_0x4b8ad1(0x15c)]||{};switch(_0x114bbe['source']){case'lorebook':_0x1e7b7c*=1.2;break;case _0x4b8ad1(0x15f):_0x1e7b7c*=1.1;break;case'chat_history':if(_0x114bbe[_0x4b8ad1(0x1a0)]&&_0x277dd1>0x0){const _0xe7c62c=_0x114bbe['floor']/_0x277dd1;_0x1e7b7c*=0x1+_0xe7c62c;}break;}const _0x408af4=_0x3deb96['rerank_score']*_0xc03fbc+(_0x3deb96['score']||0x0)*(0x1-_0xc03fbc),_0x3cd452=_0x408af4*_0x1e7b7c;return{..._0x3deb96,'final_score':_0x3cd452};});return _0x529cf1[_0xe95076(0x188)]((_0x1e2281,_0x3b41e5)=>(_0x3b41e5[_0xe95076(0x1ce)]||0x0)-(_0x1e2281[_0xe95076(0x1ce)]||0x0)),console[_0xe95076(0x1bc)](_0xe95076(0x157)),_0x529cf1[_0xe95076(0x177)](0x0,_0x293be1['rerank'][_0xe95076(0x19c)]);}async function rearrangeChat(_0x39eaaa,_0x185227,_0x444b53,_0x540d1e){const _0x166523=_0x3f5590;setExtensionPrompt(_0x166523(0x1be),'',settings[_0x166523(0x179)][_0x166523(0x1b5)],settings[_0x166523(0x179)][_0x166523(0x170)],![],settings[_0x166523(0x179)][_0x166523(0x1cb)]);if(_0x540d1e===_0x166523(0x1c7)||!settings[_0x166523(0x1a4)][_0x166523(0x1a7)])return;const _0x5bdc9c=_0x39eaaa['slice'](-settings[_0x166523(0x172)][_0x166523(0x18f)]);if(_0x5bdc9c['length']===0x0)return;const _0x23efaa=_0x5bdc9c[_0x166523(0x1e4)](_0x3859cd=>_0x3859cd['mes'])[_0x166523(0x18e)]('\x20')[_0x166523(0x192)](/<[^>]*>/g,'')['trim']();if(!_0x23efaa)return;try{const _0x4a3efa=await queryVectors(_0x23efaa);if(_0x4a3efa['length']===0x0)return;const _0x40fc46=await rerankResults(_0x4a3efa,_0x23efaa,settings);if(_0x40fc46['length']===0x0)return;const _0x3a228e=_0x40fc46['map'](_0x8c82b4=>_0x8c82b4[_0x166523(0x178)])[_0x166523(0x18e)]('\x0a\x0a'),_0x5ae818=settings[_0x166523(0x179)]['template']['replace'](_0x166523(0x17f),_0x3a228e);setExtensionPrompt('HANLINYUAN_RAG',_0x5ae818,settings['injection'][_0x166523(0x1b5)],settings[_0x166523(0x179)][_0x166523(0x170)],![],settings[_0x166523(0x179)][_0x166523(0x1cb)]);}catch(_0x201b03){console[_0x166523(0x165)]('[翰林院]\x20检索或注入时发生错误:',_0x201b03);if(settings[_0x166523(0x1a4)][_0x166523(0x1bb)])showNotification('忆识检索失败:\x20'+_0x201b03[_0x166523(0x152)],'error');}}