diff --git a/core/rag-processor.js b/core/rag-processor.js index 1c8dd99..6d9e635 100644 --- a/core/rag-processor.js +++ b/core/rag-processor.js @@ -1 +1 @@ -(function(_0x349155,_0x3b7155){const _0x3703a1=_0x1d26,_0x34437d=_0x349155();while(!![]){try{const _0x361b85=parseInt(_0x3703a1(0x180))/0x1+parseInt(_0x3703a1(0x161))/0x2*(-parseInt(_0x3703a1(0x171))/0x3)+parseInt(_0x3703a1(0x15c))/0x4+-parseInt(_0x3703a1(0x157))/0x5+parseInt(_0x3703a1(0x170))/0x6*(parseInt(_0x3703a1(0x167))/0x7)+parseInt(_0x3703a1(0x159))/0x8*(-parseInt(_0x3703a1(0x17a))/0x9)+-parseInt(_0x3703a1(0x181))/0xa;if(_0x361b85===_0x3b7155)break;else _0x34437d['push'](_0x34437d['shift']());}catch(_0x516a16){_0x34437d['push'](_0x34437d['shift']());}}}(_0x5549,0x416a3));import{extension_settings}from'/scripts/extensions.js';const RAG_DB_PREFIX='amily2_rag_db_';function _0x5549(){const _0x4d8a67=['\x20更新向量索引。','length','[RAG]\x20Retrieved\x20','[RAG]\x20Saving\x20vector\x20DB\x20for\x20character\x20','[RAG]\x20Retrieving\x20chunks\x20for\x20query:\x20\x22','18hoVlJo','[RAG]\x20Loading\x20vector\x20DB\x20for\x20character\x20','json','[翰林院]\x20getEmbeddings\x20函数执行失败:','topK','apiUrl','210908umvvhL','1316070zwjhrr','ragConfig','BAAI/bge-large-zh-v1.5','log','embedding','score','[RAG]\x20Chunking\x20text\x20with\x20chunk\x20size:\x20','[翰林院]\x20更新向量索引时发生错误:\x20','API\x20地址或密钥未配置。','text','characters','setItem','map','push','[翰林院]\x20成功为角色\x20','parse','stringify','min','721625FpbvOF','model','274808jpuWmW','chunkSize','[RAG]\x20Failed\x20to\x20save\x20DB\x20to\x20localStorage:\x20','752800xqZolN','\x27\x20的配置。','Bearer\x20','[RAG]\x20Database\x20is\x20empty.\x20No\x20chunks\x20to\x20retrieve.','slice','74snpcXT','[RAG]\x20Failed\x20to\x20get\x20embedding\x20for\x20the\x20query\x20text.','message','index','\x20from\x20key:\x20','error','3689TrwQTk','[翰林院]\x20在\x20extension_settings\x20中未找到\x20\x27',',\x20overlap:\x20','isArray','sqrt','\x20relevant\x20chunks.','apiKey','POST','status','2820lgBlpX','2775kNFdUz','/embeddings','sort','data'];_0x5549=function(){return _0x4d8a67;};return _0x5549();}function getCharRagConfig(_0x493d09){const _0x40c70e=_0x1d26,_0x27e2a4='st-amily2-chat-optimisation',_0x49a5aa=extension_settings[_0x27e2a4];if(!_0x49a5aa)return console[_0x40c70e(0x166)](_0x40c70e(0x168)+_0x27e2a4+_0x40c70e(0x15d)),{};const _0x2b4ac5=_0x49a5aa[_0x40c70e(0x182)]||{},_0x19e3ca=_0x2b4ac5[_0x40c70e(0x14f)]?.[_0x493d09]||{};return{'apiUrl':_0x2b4ac5[_0x40c70e(0x17f)],'apiKey':_0x2b4ac5[_0x40c70e(0x16d)],'model':_0x2b4ac5[_0x40c70e(0x158)],..._0x19e3ca};}function getDbKey(_0x198750){return''+RAG_DB_PREFIX+_0x198750;}function _0x1d26(_0x503c2e,_0x4e8bd7){const _0x5549d0=_0x5549();return _0x1d26=function(_0x1d26e8,_0x2a18cc){_0x1d26e8=_0x1d26e8-0x148;let _0x580168=_0x5549d0[_0x1d26e8];return _0x580168;},_0x1d26(_0x503c2e,_0x4e8bd7);}function cosineSimilarity(_0x1d4949,_0x34d784){const _0x49d87f=_0x1d26;if(!_0x1d4949||!_0x34d784||_0x1d4949[_0x49d87f(0x176)]!==_0x34d784[_0x49d87f(0x176)])return 0x0;let _0x5fb6e7=0x0,_0x22441a=0x0,_0x7f0184=0x0;for(let _0xfb9f10=0x0;_0xfb9f10<_0x1d4949['length'];_0xfb9f10++){_0x5fb6e7+=_0x1d4949[_0xfb9f10]*_0x34d784[_0xfb9f10],_0x22441a+=_0x1d4949[_0xfb9f10]*_0x1d4949[_0xfb9f10],_0x7f0184+=_0x34d784[_0xfb9f10]*_0x34d784[_0xfb9f10];}_0x22441a=Math[_0x49d87f(0x16b)](_0x22441a),_0x7f0184=Math[_0x49d87f(0x16b)](_0x7f0184);if(_0x22441a===0x0||_0x7f0184===0x0)return 0x0;return _0x5fb6e7/(_0x22441a*_0x7f0184);}export async function chunkText(_0xc2bc0b,{chunkSize:chunkSize=0x200,chunkOverlap:chunkOverlap=0x32}){const _0x3f4790=_0x1d26;if(_0xc2bc0b[_0x3f4790(0x176)]<=chunkSize)return[_0xc2bc0b];console[_0x3f4790(0x148)](_0x3f4790(0x14b)+chunkSize+_0x3f4790(0x169)+chunkOverlap);const _0x5dcf3e=[];let _0x2211ab=0x0;const _0x148072=chunkSize-Math[_0x3f4790(0x156)](chunkOverlap,chunkSize-0x1);while(_0x2211ab<_0xc2bc0b[_0x3f4790(0x176)]){const _0xe95393=Math[_0x3f4790(0x156)](_0x2211ab+chunkSize,_0xc2bc0b['length']);_0x5dcf3e[_0x3f4790(0x152)](_0xc2bc0b[_0x3f4790(0x160)](_0x2211ab,_0xe95393));if(_0xe95393===_0xc2bc0b[_0x3f4790(0x176)])break;_0x2211ab+=_0x148072;}return _0x5dcf3e;}export async function getEmbeddings(_0x3684cf,_0x274eae){const _0x395ea0=_0x1d26;if(!_0x274eae[_0x395ea0(0x17f)]||!_0x274eae[_0x395ea0(0x16d)])throw new Error(_0x395ea0(0x14d));const _0x35bf0d=(_0x274eae[_0x395ea0(0x17f)]['endsWith']('/')?_0x274eae[_0x395ea0(0x17f)][_0x395ea0(0x160)](0x0,-0x1):_0x274eae['apiUrl'])+_0x395ea0(0x172),_0x4229d1={'input':_0x3684cf,'model':_0x274eae[_0x395ea0(0x158)]||_0x395ea0(0x183)};try{const _0x55e587=await fetch(_0x35bf0d,{'method':_0x395ea0(0x16e),'headers':{'Authorization':_0x395ea0(0x15e)+_0x274eae[_0x395ea0(0x16d)],'Content-Type':'application/json'},'body':JSON[_0x395ea0(0x155)](_0x4229d1)});if(!_0x55e587['ok']){const _0x4c3e3d=await _0x55e587[_0x395ea0(0x14e)]();throw new Error('Embedding\x20API\x20请求失败,状态码:\x20'+_0x55e587[_0x395ea0(0x16f)]+':\x20'+_0x4c3e3d);}const _0x318f96=await _0x55e587[_0x395ea0(0x17c)]();if(!_0x318f96[_0x395ea0(0x174)]||!Array[_0x395ea0(0x16a)](_0x318f96[_0x395ea0(0x174)])||_0x318f96[_0x395ea0(0x174)][_0x395ea0(0x176)]===0x0)throw new Error('从\x20Embedding\x20API\x20返回的响应格式无效:\x20\x27data\x27\x20数组未找到或为空。');return _0x318f96['data'][_0x395ea0(0x173)]((_0x9d94f2,_0x2aac36)=>_0x9d94f2[_0x395ea0(0x164)]-_0x2aac36[_0x395ea0(0x164)]),_0x318f96[_0x395ea0(0x174)][_0x395ea0(0x151)](_0x3dc50a=>_0x3dc50a[_0x395ea0(0x149)]);}catch(_0x15117e){console[_0x395ea0(0x166)](_0x395ea0(0x17d),_0x15117e);throw _0x15117e;}}export async function saveVectorDb(_0x1a4d10,_0x295a70){const _0x150d31=_0x1d26;try{const _0x51744c=getDbKey(_0x1a4d10);console[_0x150d31(0x148)](_0x150d31(0x178)+_0x1a4d10+'\x20to\x20key:\x20'+_0x51744c),localStorage[_0x150d31(0x150)](_0x51744c,JSON[_0x150d31(0x155)](_0x295a70));}catch(_0x10b14f){console[_0x150d31(0x166)](_0x150d31(0x15b)+_0x10b14f[_0x150d31(0x163)]);}}export async function loadVectorDb(_0x4bc9fa){const _0x5bc55f=_0x1d26;try{const _0x3853c1=getDbKey(_0x4bc9fa);console[_0x5bc55f(0x148)](_0x5bc55f(0x17b)+_0x4bc9fa+_0x5bc55f(0x165)+_0x3853c1);const _0x534ef5=localStorage['getItem'](_0x3853c1);return _0x534ef5?JSON[_0x5bc55f(0x154)](_0x534ef5):[];}catch(_0x45b501){return console[_0x5bc55f(0x166)]('[RAG]\x20Failed\x20to\x20load\x20DB\x20from\x20localStorage:\x20'+_0x45b501['message']),[];}}export async function retrieveRelevantChunks(_0x2959c2,_0x55cbd0){const _0x1661b5=_0x1d26;console[_0x1661b5(0x148)](_0x1661b5(0x179)+_0x2959c2+'\x22');const _0x21d55b=getCharRagConfig(_0x55cbd0),_0x3ff26d=await loadVectorDb(_0x55cbd0);if(_0x3ff26d[_0x1661b5(0x176)]===0x0)return console[_0x1661b5(0x148)](_0x1661b5(0x15f)),[];const _0x3af9bf=(await getEmbeddings([_0x2959c2],_0x21d55b))[0x0];if(!_0x3af9bf||_0x3af9bf[_0x1661b5(0x176)]===0x0)return console[_0x1661b5(0x166)](_0x1661b5(0x162)),[];const _0x2e141a=_0x3ff26d['map'](_0x406d8f=>({'text':_0x406d8f[_0x1661b5(0x14e)],'score':cosineSimilarity(_0x3af9bf,_0x406d8f['vector'])}));_0x2e141a[_0x1661b5(0x173)]((_0x5b7a84,_0x26ea91)=>_0x26ea91['score']-_0x5b7a84[_0x1661b5(0x14a)]);const _0xb24e1=_0x21d55b[_0x1661b5(0x17e)]||0x3,_0x1ffe33=_0x2e141a[_0x1661b5(0x160)](0x0,_0xb24e1)[_0x1661b5(0x151)](_0x21fa48=>_0x21fa48[_0x1661b5(0x14e)]);return console[_0x1661b5(0x148)](_0x1661b5(0x177)+_0x1ffe33[_0x1661b5(0x176)]+_0x1661b5(0x16c)),_0x1ffe33;}export async function updateVectorIndex(_0x25c2b5,_0x19e4aa,_0x3b862b){const _0x3d698c=_0x1d26;try{const _0x4ff61b=await chunkText(_0x19e4aa,{'chunkSize':_0x3b862b[_0x3d698c(0x15a)]}),_0x4064b6=await getEmbeddings(_0x4ff61b,_0x3b862b),_0x197a61=_0x4ff61b[_0x3d698c(0x151)]((_0x3b7268,_0x2b331b)=>({'text':_0x3b7268,'vector':_0x4064b6[_0x2b331b]}));return await saveVectorDb(_0x25c2b5,_0x197a61),console['log'](_0x3d698c(0x153)+_0x25c2b5+_0x3d698c(0x175)),{'success':!![],'docCount':_0x197a61[_0x3d698c(0x176)]};}catch(_0x2c31a7){return console[_0x3d698c(0x166)](_0x3d698c(0x14c)+_0x2c31a7[_0x3d698c(0x163)]),{'success':![],'error':_0x2c31a7['message']};}} +'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));}}