Files
ST-Amily2-Chat-Optimisation/core/rag-processor.js
2025-07-19 16:48:51 +08:00

2 lines
19 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'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));}}