From 73f1e285e713fef15b0fc13deb403fcf45996230 Mon Sep 17 00:00:00 2001 From: Wx-2025 <351320169@qq.com> Date: Wed, 6 Aug 2025 10:52:41 +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 d297e52..02b1444 100644 --- a/core/rag-processor.js +++ b/core/rag-processor.js @@ -1 +1 @@ -'use strict';const _0x532886=_0x1675;(function(_0x407f3e,_0x5b04ed){const _0x4e0f8b=_0x1675,_0x201e71=_0x407f3e();while(!![]){try{const _0x3f6f3d=parseInt(_0x4e0f8b(0x1b1))/0x1+parseInt(_0x4e0f8b(0x22a))/0x2*(parseInt(_0x4e0f8b(0x1d1))/0x3)+parseInt(_0x4e0f8b(0x1f6))/0x4+parseInt(_0x4e0f8b(0x240))/0x5+-parseInt(_0x4e0f8b(0x1c6))/0x6+parseInt(_0x4e0f8b(0x24f))/0x7+-parseInt(_0x4e0f8b(0x1be))/0x8*(parseInt(_0x4e0f8b(0x20c))/0x9);if(_0x3f6f3d===_0x5b04ed)break;else _0x201e71['push'](_0x201e71['shift']());}catch(_0x21a967){_0x201e71['push'](_0x201e71['shift']());}}}(_0x342c,0x7bc74));import{extension_prompt_roles,setExtensionPrompt}from'/script.js';import*as _0x336e7d from'./utils/context-utils.js';import{defaultSettings as _0x39d149}from'./rag-settings.js';import*as _0x8a68c3 from'./ingestion-manager.js';const MODULE_NAME='hanlinyuan-rag-core',OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME=_0x532886(0x1b5);let context=null,settings=null,lockedCollectionId=null;function _0x342c(){const _0x1c9540=['[翰林院-日志]\x20清空宝库API调用成功。','depth','输入文本为空','[翰林院-日志]\x20发送到\x20/api/vector/purge\x20的请求体:','saveProgress','filter',',\x20第1卷,\x20第1章,\x20第','rerank_score','翰林院忆识核心已启动\x20(V5.1-和平共存版),已代理\x20','[翰林院-核心]\x20文本录入失败:\x20','[翰林院-Rerank]\x20开始元数据加权最终排序...','[翰林院-日志]\x20/api/vector/list\x20响应状态:\x20','2615265KuFIcQ','openai','lorebook','model','success','[翰林院-日志]\x20/api/vector/query\x20响应状态:\x20','max','application/json','toISOString','HANLINYUAN_RAG','manual','source','start','正在智能分块...','/api/vector/list','5367341TQkcEB','[翰林院-核心]\x20聊天记录凝识完成,成功插入\x20','[翰林院-核心]\x20文本录入任务被用户中止。','在insertVectors内部也无法获取collectionId','\x20获取模型列表...','map','split','正在处理\x20','...)','is_user','\x0a\x0a[来源:\x20','vectors_rearrangeChat','[翰林院-核心]\x20已将\x20','Rerank失败:\x20','notify','rerank','\x27的文本分割成\x20','[翰林院-核心]\x20ingestTextToHanlinyuan\x20失败:','/embeddings','push','1839632pBKXgL','getRequestHeaders','AbortError','queryMessageCount','/v1/rerank','data','[翰林院-日志]\x20获取向量列表API错误:','/api/vector/insert','1971456jJpsiH','/v1','小说录入','findIndex','apiKey','send_date','[翰林院-日志]\x20宝库查询API错误:','GET','chat','\x0a','injection','526233tyTdjd','webllm','\x20个条目。','聊天记录\x20#','[翰林院-核心]\x20已为宝库\x20','\x20个向量条目。','忆识存入API错误\x20','第1卷','[翰林院-核心]\x20聊天记录凝识失败:\x20','[翰林院]\x20检索或注入时发生错误:','clearJob','[翰林院-核心]\x20成功插入\x20','[翰林院-核心]\x20将来源\x27','top_n','[翰林院-日志]\x20开始清空宝库...','mes','getContext','isArray','[翰林院-核心]\x20凝识任务已锁定忆识宝库ID:\x20','宝库查询API错误\x20','[翰林院-日志]\x20发送到\x20/api/vector/list\x20的请求体:','测试连接','POST','metadata','status','[翰林院-日志]\x20发送到\x20/api/vector/query\x20的请求体:','/v1/embeddings','end','[翰林院-日志]\x20统计目标集合ID:\x20','):\x20','trim','[翰林院-日志]\x20开始获取向量总数...','[翰林院-日志]\x20开始向量查询\x20(采用最终API交互模式)...','position','第1章','[翰林院-日志]\x20统计成功,向量总数:\x20','now','2604196vHYsUc','endsWith','\x20条消息分解为\x20','sort','retrieval','batchSize','abs','url','name','\x20个块。','stringify','test','function','[翰林院-日志]\x20查询成功,返回\x20','aborted','embedding','saveSettingsDebounced','forEach','condensation','api-key','substring','翰林院忆识核心已启动\x20(V5.1-借壳上市版),已注册全局函数:\x20','72ErRdmB','No\x20messages\x20to\x20process.','log','\x20条结果。','\x20个知识块,准备入库。','maxResults','length','join','text','[翰林院-核心]\x20insertVectors被调用时未提供collectionId!','error','https://api.openai.com','\x20记录凝识范围:\x20','chat_history','忆识检索失败:\x20','{{text}}','手动录入','floor','/v1/models','无法确定当前忆识宝库的ID,请确认角色已正确加载。','[翰林院-Rerank]\x20外部Rerank失败,将仅使用内部加权。','matchThreshold','\x20-\x20楼层\x20#','find','(已锁定:\x20','object','[翰林院-Rerank]\x20开始外部API重排序...','extensionSettings','[翰林院-日志]\x20清空宝库API错误:','/api/vector/purge','6mTTvmU','advanced','文本块和向量数量不匹配','condensationHistory','final_score','replace','index','sourceName','custom','results'];_0x342c=function(){return _0x1c9540;};return _0x342c();}export{initialize,getSettings,saveSettings,resetSettings,testApiConnection,fetchEmbeddingModels,fetchRerankModels,getVectorCount,purgeStorage,getMessagesForCondensation,processCondensation,ingestTextToHanlinyuan,getCollectionId,toggleSessionLock,isSessionLocked,getLockedSessionInfo};function initialize(){const _0x54854b=_0x532886;context=SillyTavern[_0x54854b(0x1e1)]();if(!context){console['error']('[翰林院]\x20未能获取SillyTavern上下文,初始化失败。');return;}settings=getSettings();const _0xeef937=window[OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME];typeof _0xeef937===_0x54854b(0x202)?(window[OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME]=async function(..._0x8f2795){await rearrangeChat(..._0x8f2795),await _0xeef937(..._0x8f2795);},console[_0x54854b(0x20e)](_0x54854b(0x23c)+OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME)):(window[OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME]=rearrangeChat,console[_0x54854b(0x20e)](_0x54854b(0x20b)+OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME));}async function ingestTextToHanlinyuan(_0x7ef931,_0x3e19b4='manual',_0x4d5988='',_0x11ba8d=()=>{},_0x14dec4=null,_0x3801ca=()=>{},_0x1bcdf7=()=>{},_0x3abd59=null,_0x486876=0x0,_0x18619f=null){const _0xe93123=_0x532886;if(!_0x7ef931||!_0x7ef931['trim']())return{'success':![],'error':_0xe93123(0x236)};if(!settings)return{'success':![],'error':'核心未初始化'};try{const _0x555e88=getCollectionId();if(!_0x555e88)throw new Error(_0xe93123(0x21f));_0x3801ca('[翰林院-核心]\x20已锁定忆识宝库ID:\x20'+_0x555e88,_0xe93123(0x262)),_0x11ba8d({'message':_0xe93123(0x24d),'processed':0x0,'total':0x1});const _0x5b3b14=splitIntoChunks(_0x7ef931,_0x3e19b4,_0x4d5988),_0x9798ec=_0x5b3b14[_0xe93123(0x212)];if(_0x14dec4?.[_0xe93123(0x204)])throw new Error(_0xe93123(0x1c0));_0x3801ca(_0xe93123(0x1dd)+(_0x4d5988||_0x3e19b4)+_0xe93123(0x1ba)+_0x9798ec+_0xe93123(0x1ff),_0xe93123(0x262));if(_0x9798ec===0x0)return{'success':!![],'count':0x0};const _0x54f3b7=settings[_0xe93123(0x1fa)]['batchSize']||0x5;let _0x31124c=_0x486876;for(let _0xf2f3e2=_0x486876;_0xf2f3e2<_0x9798ec;_0xf2f3e2+=_0x54f3b7){if(_0x14dec4?.['aborted'])throw new Error(_0xe93123(0x1c0));const _0x53cb79=_0x5b3b14['slice'](_0xf2f3e2,_0xf2f3e2+_0x54f3b7);_0x11ba8d({'message':_0xe93123(0x256)+(_0xf2f3e2+0x1)+'-'+(_0xf2f3e2+_0x53cb79[_0xe93123(0x212)])+'\x20块','processed':_0xf2f3e2,'total':_0x9798ec});const _0x2fd4ef=_0x53cb79[_0xe93123(0x254)](_0x36d003=>_0x36d003[_0xe93123(0x214)]),_0x456a2d=await getEmbeddings(_0x2fd4ef,_0x14dec4);if(_0x14dec4?.[_0xe93123(0x204)])throw new Error(_0xe93123(0x1c0));if(_0x53cb79['length']!==_0x456a2d[_0xe93123(0x212)])throw new Error(_0xe93123(0x22c));const _0x519b9b=_0x53cb79[_0xe93123(0x254)]((_0x50df8f,_0x57f899)=>({..._0x50df8f,'vector':_0x456a2d[_0x57f899]}));await insertVectors(_0x519b9b,_0x14dec4,_0x555e88),_0x31124c+=_0x53cb79[_0xe93123(0x212)],_0x3abd59&&_0x8a68c3[_0xe93123(0x238)](_0x3abd59,_0x31124c,_0x9798ec),_0x1bcdf7();}_0x3abd59&&_0x8a68c3[_0xe93123(0x1db)](_0x3abd59);if(_0x18619f){const _0x2ab3ce=getCollectionId(),_0x2b97ba=_0x18619f[_0xe93123(0x1ec)]===0x0?context[_0xe93123(0x1ce)][_0xe93123(0x212)]:_0x18619f[_0xe93123(0x1ec)];settings['condensationHistory'][_0x2ab3ce]={'start':_0x18619f[_0xe93123(0x24c)],'end':_0x2b97ba,'timestamp':new Date()[_0xe93123(0x248)]()},saveSettings(),_0x3801ca(_0xe93123(0x1d5)+_0x2ab3ce+_0xe93123(0x218)+_0x18619f['start']+'-'+_0x2b97ba,_0xe93123(0x262));}return _0x3801ca(_0xe93123(0x1dc)+_0x31124c+_0xe93123(0x1d6),_0xe93123(0x244)),{'success':!![],'count':_0x31124c};}catch(_0x14474e){if(_0x14474e[_0xe93123(0x1fe)]==='AbortError'){_0x3801ca(_0xe93123(0x251),'warn');throw _0x14474e;}return console[_0xe93123(0x216)](_0xe93123(0x1bb),_0x14474e),_0x3801ca(_0xe93123(0x23d)+_0x14474e[_0xe93123(0x25d)],'error'),{'success':![],'error':_0x14474e[_0xe93123(0x25d)]};}}function getSettings(){const _0x272785=_0x532886;if(!context||!context[_0x272785(0x227)])return structuredClone(_0x39d149);let _0x1f2706=context[_0x272785(0x227)][MODULE_NAME];!_0x1f2706&&(_0x1f2706={},context[_0x272785(0x227)][MODULE_NAME]=_0x1f2706);_0x1f2706[_0x272785(0x22d)]===undefined&&(_0x1f2706['condensationHistory']={});for(const _0x3d0343 in _0x39d149){if(_0x1f2706[_0x3d0343]===undefined)_0x1f2706[_0x3d0343]=structuredClone(_0x39d149[_0x3d0343]);else{if(typeof _0x39d149[_0x3d0343]===_0x272785(0x225)&&!Array['isArray'](_0x39d149[_0x3d0343])&&_0x39d149[_0x3d0343]!==null)for(const _0x154814 in _0x39d149[_0x3d0343]){_0x1f2706[_0x3d0343][_0x154814]===undefined&&(_0x1f2706[_0x3d0343][_0x154814]=_0x39d149[_0x3d0343][_0x154814]);}}}return _0x1f2706;}function saveSettings(){const _0x2460ee=_0x532886;if(context)context[_0x2460ee(0x206)]();}function resetSettings(){context&&(context['extensionSettings'][MODULE_NAME]=structuredClone(_0x39d149),saveSettings());}function showNotification(_0xdeccba,_0x517373=_0x532886(0x262)){toastr[_0x517373](_0xdeccba);}function getTagForSource(_0xd08851){const _0x45914f=_0x532886;switch(_0xd08851){case _0x45914f(0x219):return'聊天记录';case'lorebook':return'世界书';case _0x45914f(0x24a):return _0x45914f(0x21c);case'novel':return _0x45914f(0x1c8);default:return'资料';}}function splitIntoChunks(_0x383f73,_0x79ca98,_0x5bb98c){const _0x42f90a=_0x532886,{chunkSize:_0xfa2993,overlap:_0x14ae16}=settings[_0x42f90a(0x22b)],_0x259b83=[];if(!_0x383f73||_0xfa2993<=0x0)return _0x259b83;const _0x1e0e28=/(第\s*[一二三四五六七八九十百千万零\d]+\s*卷)/gim,_0x3fae46=/(第\s*[一二三四五六七八九十百千万零\d]+\s*[章回节部])|^(Chapter\s+\d+)/gim;let _0x37b25d=0x0,_0x2e9a2b=0x1,_0x3c48a7=0x1,_0x2aabbe=![];const _0x2cb617=_0x383f73[_0x42f90a(0x255)]('\x0a');let _0x2eea18=_0x42f90a(0x1d8),_0xa05494=_0x42f90a(0x1f3),_0x137ba8=[];function _0x1ac0d6(){const _0x24799b=_0x42f90a;if(_0x137ba8[_0x24799b(0x212)]===0x0)return;const _0x4c2090=_0x137ba8['join']('\x0a');let _0x1ea3bb=0x0,_0x89dadb=0x1;while(_0x1ea3bb<_0x4c2090[_0x24799b(0x212)]){const _0x34c17b=Math[_0x24799b(0x1b3)](_0x1ea3bb+_0xfa2993,_0x4c2090[_0x24799b(0x212)]),_0xfc9f73=_0x4c2090[_0x24799b(0x20a)](_0x1ea3bb,_0x34c17b);if(_0xfc9f73[_0x24799b(0x1ef)]()['length']>0x0){const _0x4a70cd={'source':_0x79ca98,'sourceName':_0x5bb98c,'timestamp':new Date()[_0x24799b(0x248)](),'globalIndex':_0x37b25d++,'volume':_0x2eea18,'chapter':_0xa05494,'section':_0x89dadb},_0x142724=getTagForSource(_0x79ca98),_0x16cb1e=_0x24799b(0x1b0)+_0x5bb98c+',\x20'+_0x2eea18+',\x20'+_0xa05494+_0x24799b(0x1a9)+_0x89dadb+'节]',_0x3cbe89='<'+_0x142724+'>\x0a'+_0x16cb1e+'\x0a'+_0xfc9f73+_0x24799b(0x259)+_0x142724+'>';_0x259b83['push']({'text':_0x3cbe89,'metadata':_0x4a70cd}),_0x89dadb++;}_0x1ea3bb+=_0xfa2993-_0x14ae16;if(_0x1ea3bb>=_0x4c2090[_0x24799b(0x212)])break;}_0x137ba8=[];}for(const _0x3755e9 of _0x2cb617){const _0x594e5b=_0x3755e9[_0x42f90a(0x1ef)]();if(_0x1e0e28[_0x42f90a(0x201)](_0x594e5b))_0x1ac0d6(),_0x2eea18=_0x594e5b,_0xa05494='第1章',_0x2e9a2b++,_0x3c48a7=0x1,_0x2aabbe=!![];else _0x3fae46[_0x42f90a(0x201)](_0x594e5b)?(_0x1ac0d6(),_0xa05494=_0x594e5b,_0x3c48a7++):_0x137ba8[_0x42f90a(0x1bd)](_0x3755e9);}_0x1ac0d6();if(_0x259b83[_0x42f90a(0x212)]===0x0&&_0x383f73[_0x42f90a(0x212)]>0x0){let _0x16d179=0x0,_0x15d3cf=0x1;while(_0x16d179<_0x383f73['length']){const _0x18f050=Math['min'](_0x16d179+_0xfa2993,_0x383f73[_0x42f90a(0x212)]),_0x2e1d85=_0x383f73[_0x42f90a(0x20a)](_0x16d179,_0x18f050),_0x3ef5c3={'source':_0x79ca98,'sourceName':_0x5bb98c,'timestamp':new Date()['toISOString'](),'globalIndex':_0x259b83['length'],'volume':_0x42f90a(0x1d8),'chapter':_0x42f90a(0x1f3),'section':_0x15d3cf},_0x138fe1=getTagForSource(_0x79ca98),_0x34a965=_0x42f90a(0x1b0)+_0x5bb98c+_0x42f90a(0x23a)+_0x15d3cf+'节]',_0x13f7c2='<'+_0x138fe1+'>\x0a'+_0x34a965+'\x0a'+_0x2e1d85+_0x42f90a(0x259)+_0x138fe1+'>';_0x259b83[_0x42f90a(0x1bd)]({'text':_0x13f7c2,'metadata':_0x3ef5c3}),_0x15d3cf++,_0x16d179+=_0xfa2993-_0x14ae16;}}return _0x259b83;}import{getCollectionId as _0x20aad2,getCharacterName}from'./utils/context-utils.js';function getCollectionId(){return lockedCollectionId||_0x20aad2();}function toggleSessionLock(){return lockedCollectionId?(lockedCollectionId=null,![]):(lockedCollectionId=_0x20aad2(),!![]);}function isSessionLocked(){return lockedCollectionId!==null;}function getLockedSessionInfo(){const _0x4e9189=_0x532886;if(!lockedCollectionId)return null;return{'id':lockedCollectionId,'name':_0x4e9189(0x224)+lockedCollectionId[_0x4e9189(0x20a)](0x0,0x8)+_0x4e9189(0x257)};}function generateHash(_0x3f2ba2){const _0x16523e=_0x532886;let _0x4ea3a9=0x0;for(let _0x3f85a7=0x0;_0x3f85a7<_0x3f2ba2['length'];_0x3f85a7++){const _0x77cbbc=_0x3f2ba2[_0x16523e(0x1ae)](_0x3f85a7);_0x4ea3a9=(_0x4ea3a9<<0x5)-_0x4ea3a9+_0x77cbbc,_0x4ea3a9=_0x4ea3a9&_0x4ea3a9;}return Math[_0x16523e(0x1fc)](_0x4ea3a9)['toString'](0x24);}function getSanitizedBaseUrl(_0x50d40d){const _0x2ea468=_0x532886;let _0x45c91f=_0x50d40d['trim']();return _0x45c91f[_0x2ea468(0x1f7)]('/')&&(_0x45c91f=_0x45c91f[_0x2ea468(0x25a)](0x0,-0x1)),_0x45c91f[_0x2ea468(0x1f7)](_0x2ea468(0x1c7))&&(_0x45c91f=_0x45c91f[_0x2ea468(0x25a)](0x0,-0x3)),_0x45c91f[_0x2ea468(0x1f7)](_0x2ea468(0x1bc))&&(_0x45c91f=_0x45c91f['slice'](0x0,-0xb)),_0x45c91f;}async function fetchEmbeddingModels(){const _0x29aa31=_0x532886,{apiKey:_0x30f4d5}=settings['retrieval'],_0x3ff12f=getApiEndpointUrl(!![]);if(!_0x3ff12f||!_0x30f4d5)throw new Error('API\x20URL\x20或\x20Key\x20未提供。');const _0x41da2b=getSanitizedBaseUrl(_0x3ff12f),_0x14edd3=_0x41da2b+_0x29aa31(0x21e);console[_0x29aa31(0x20e)](_0x29aa31(0x260)+_0x14edd3+'\x20获取模型列表...');const _0x1d44a0=await fetch(_0x14edd3,{'method':'GET','headers':getApiHeaders()});if(!_0x1d44a0['ok']){const _0x40572e=await _0x1d44a0['text']();throw new Error('获取模型列表失败\x20('+_0x1d44a0[_0x29aa31(0x1e9)]+'):\x20'+_0x40572e);}const _0x555fdf=await _0x1d44a0[_0x29aa31(0x1b2)]();if(!_0x555fdf[_0x29aa31(0x1c3)]||!Array[_0x29aa31(0x1e2)](_0x555fdf[_0x29aa31(0x1c3)]))throw new Error('模型API的响应格式无效:\x20未找到\x20\x27data\x27\x20数组。');return _0x555fdf[_0x29aa31(0x1c3)]['map'](_0x1bb4cd=>_0x1bb4cd['id'])[_0x29aa31(0x1f9)]();}function _0x1675(_0x20851e,_0x6dce87){const _0x342c1a=_0x342c();return _0x1675=function(_0x167582,_0x4c6054){_0x167582=_0x167582-0x1a9;let _0x4319f7=_0x342c1a[_0x167582];return _0x4319f7;},_0x1675(_0x20851e,_0x6dce87);}function getRerankBaseUrl(_0x3eaaa9){const _0x23cae4=_0x532886;let _0x38e2b3=_0x3eaaa9[_0x23cae4(0x1ef)]();return _0x38e2b3[_0x23cae4(0x1f7)]('/')&&(_0x38e2b3=_0x38e2b3['slice'](0x0,-0x1)),_0x38e2b3[_0x23cae4(0x1f7)](_0x23cae4(0x1c7))&&(_0x38e2b3=_0x38e2b3['slice'](0x0,-0x3)),_0x38e2b3[_0x23cae4(0x1f7)]('/rerank')&&(_0x38e2b3=_0x38e2b3[_0x23cae4(0x25a)](0x0,-0x7)),_0x38e2b3;}async function fetchRerankModels(){const _0x3844c7=_0x532886,{url:_0x342d91,apiKey:_0x132e3e}=settings[_0x3844c7(0x1b9)];if(!_0x342d91||!_0x132e3e)throw new Error('Rerank\x20API\x20URL\x20或\x20Key\x20未提供。');const _0x1d7799=getRerankBaseUrl(_0x342d91),_0x12a2aa=_0x1d7799+'/v1/models';console['log']('[翰林院-Rerank]\x20正在从\x20'+_0x12a2aa+_0x3844c7(0x253));const _0xe0e86c=await fetch(_0x12a2aa,{'method':_0x3844c7(0x1cd),'headers':{'Authorization':_0x3844c7(0x1ad)+_0x132e3e}});if(!_0xe0e86c['ok']){const _0x569ed4=await _0xe0e86c[_0x3844c7(0x214)]();throw new Error(_0x3844c7(0x25e)+_0xe0e86c[_0x3844c7(0x1e9)]+_0x3844c7(0x1ee)+_0x569ed4);}const _0x1e4d1b=await _0xe0e86c[_0x3844c7(0x1b2)]();if(!_0x1e4d1b[_0x3844c7(0x1c3)]||!Array[_0x3844c7(0x1e2)](_0x1e4d1b['data']))throw new Error(_0x3844c7(0x263));return _0x1e4d1b[_0x3844c7(0x1c3)][_0x3844c7(0x254)](_0x3a9958=>_0x3a9958['id'])[_0x3844c7(0x1f9)]();}function getApiEndpointUrl(_0x238fdd=![]){const _0x19a1b2=_0x532886,{apiEndpoint:_0x15b150,customApiUrl:_0x240910}=settings[_0x19a1b2(0x1fa)];let _0x24947b;switch(_0x15b150){case _0x19a1b2(0x241):_0x24947b=_0x19a1b2(0x217);break;case'azure':case _0x19a1b2(0x232):_0x24947b=_0x240910;break;default:_0x24947b=_0x19a1b2(0x217);break;}if(_0x238fdd)return _0x24947b;return getSanitizedBaseUrl(_0x24947b)+_0x19a1b2(0x1eb);}function getApiHeaders(){const _0x5a0fc3=_0x532886,_0xef88c={'Content-Type':'application/json'},{apiKey:_0x5ea8d0,apiEndpoint:_0x13b7ad}=settings[_0x5a0fc3(0x1fa)];switch(_0x13b7ad){case'openai':case _0x5a0fc3(0x232):_0xef88c['Authorization']='Bearer\x20'+_0x5ea8d0;break;case'azure':_0xef88c[_0x5a0fc3(0x209)]=_0x5ea8d0;break;}return _0xef88c;}async function getEmbeddings(_0x15d53e,_0x5440f3=null){const _0x1bfa17=_0x532886;if(!settings[_0x1bfa17(0x1fa)]['apiKey'])throw new Error('请先配置API\x20Key');const _0x2bee94=getApiEndpointUrl(),_0x116a3f=getApiHeaders(),_0x1622f6=settings[_0x1bfa17(0x1fa)]['embeddingModel'],_0x3697b0=settings['retrieval'][_0x1bfa17(0x1fb)]||0x5,_0xd07be0=[];for(let _0x20e04d=0x0;_0x20e04d<_0x15d53e['length'];_0x20e04d+=_0x3697b0){if(_0x5440f3?.[_0x1bfa17(0x204)])throw new Error('AbortError');const _0x1cdc3e=_0x15d53e['slice'](_0x20e04d,_0x20e04d+_0x3697b0),_0xc8d994=await fetch(_0x2bee94,{'method':'POST','headers':_0x116a3f,'body':JSON[_0x1bfa17(0x200)]({'input':_0x1cdc3e,'model':_0x1622f6}),'signal':_0x5440f3});if(!_0xc8d994['ok']){const _0x2d02f9=await _0xc8d994[_0x1bfa17(0x214)]();throw new Error('神力获取失败\x20'+_0xc8d994[_0x1bfa17(0x1e9)]+':\x20'+_0x2d02f9);}const _0x58b32a=await _0xc8d994['json']();_0xd07be0[_0x1bfa17(0x1bd)](..._0x58b32a[_0x1bfa17(0x1c3)][_0x1bfa17(0x254)](_0x61e4cd=>_0x61e4cd[_0x1bfa17(0x205)])),_0x20e04d+_0x3697b0<_0x15d53e['length']&&await new Promise(_0x3f00fd=>setTimeout(_0x3f00fd,0xc8));}return _0xd07be0;}async function queryVectors(_0x2a1007){const _0x3941b6=_0x532886;console['log'](_0x3941b6(0x1f1));const _0x5a3119=getCollectionId();console['log'](_0x3941b6(0x261)+_0x5a3119);const _0x28d14f=(await getEmbeddings([_0x2a1007]))[0x0],_0x48210b={'collectionId':_0x5a3119,'searchText':_0x2a1007,'topK':settings['advanced'][_0x3941b6(0x211)],'threshold':settings[_0x3941b6(0x22b)][_0x3941b6(0x221)],'source':_0x3941b6(0x1d2),'embeddings':{[_0x2a1007]:_0x28d14f}};console['log'](_0x3941b6(0x1ea),JSON[_0x3941b6(0x200)](_0x48210b,null,0x2));const _0x43968a=await fetch('/api/vector/query',{'method':_0x3941b6(0x1e7),'headers':context[_0x3941b6(0x1bf)](),'body':JSON[_0x3941b6(0x200)](_0x48210b)});console[_0x3941b6(0x20e)](_0x3941b6(0x245)+_0x43968a['status']);if(!_0x43968a['ok']){const _0x8ebe52=await _0x43968a[_0x3941b6(0x214)]();console['error'](_0x3941b6(0x1cc),_0x8ebe52);throw new Error(_0x3941b6(0x1e4)+_0x43968a[_0x3941b6(0x1e9)]+':\x20'+_0x8ebe52);}const _0x31097c=await _0x43968a['json']();console[_0x3941b6(0x20e)]('[翰林院-日志]\x20/api/vector/query\x20响应内容:',_0x31097c);const _0x14246a=_0x31097c[_0x3941b6(0x1e8)]||_0x31097c[_0x3941b6(0x233)]||_0x31097c[_0x3941b6(0x1c3)]||[];return console['log'](_0x3941b6(0x203)+_0x14246a[_0x3941b6(0x212)]+_0x3941b6(0x20f)),_0x14246a;}async function insertVectors(_0x399f43,_0x185709=null,_0x65dbfb){const _0x516d22=_0x532886;if(!_0x65dbfb){console[_0x516d22(0x216)](_0x516d22(0x215)),_0x65dbfb=getCollectionId();if(!_0x65dbfb)throw new Error(_0x516d22(0x252));}if(_0x399f43[_0x516d22(0x212)]===0x0)return{'success':!![],'count':0x0};const _0x550b67=_0x399f43['map']((_0x1849e6,_0x2ead27)=>({'hash':generateHash(_0x1849e6[_0x516d22(0x214)]+Date[_0x516d22(0x1f5)]()+_0x2ead27),'text':_0x1849e6['text'],'metadata':_0x1849e6[_0x516d22(0x1e8)]||{'source':'unknown','timestamp':new Date()['toISOString']()}})),_0x2e2199=_0x550b67['reduce']((_0xf55b81,_0x285942,_0xfad3b4)=>{const _0x4218c6=_0x516d22;return _0xf55b81[_0x285942[_0x4218c6(0x214)]]=_0x399f43[_0xfad3b4][_0x4218c6(0x25c)],_0xf55b81;},{}),_0x2e6f24={'collectionId':_0x65dbfb,'items':_0x550b67,'source':'webllm','embeddings':_0x2e2199},_0x4833e0=await fetch(_0x516d22(0x1c5),{'method':_0x516d22(0x1e7),'headers':context[_0x516d22(0x1bf)](),'body':JSON[_0x516d22(0x200)](_0x2e6f24),'signal':_0x185709});if(!_0x4833e0['ok']){const _0x625734=await _0x4833e0[_0x516d22(0x214)]();console[_0x516d22(0x216)]('[翰林院-日志]\x20忆识存入API错误:',_0x625734);throw new Error(_0x516d22(0x1d7)+_0x4833e0[_0x516d22(0x1e9)]+':\x20'+_0x625734);}return{'success':!![],'count':_0x550b67['length']};}async function testApiConnection(){const _0x3d24aa=_0x532886;await getEmbeddings([_0x3d24aa(0x1e6)]);}async function getVectorCount(){const _0x50497d=_0x532886;console[_0x50497d(0x20e)](_0x50497d(0x1f0));const _0xe4dfb4=getCollectionId();console[_0x50497d(0x20e)](_0x50497d(0x1ed)+_0xe4dfb4);const _0xb27673={'collectionId':_0xe4dfb4,'source':'webllm','embeddings':{}};console[_0x50497d(0x20e)](_0x50497d(0x1e5),JSON[_0x50497d(0x200)](_0xb27673,null,0x2));const _0x3295e0=await fetch(_0x50497d(0x24e),{'method':_0x50497d(0x1e7),'headers':context[_0x50497d(0x1bf)](),'body':JSON[_0x50497d(0x200)](_0xb27673)});console['log'](_0x50497d(0x23f)+_0x3295e0[_0x50497d(0x1e9)]);if(!_0x3295e0['ok']){const _0x5b0df0=await _0x3295e0[_0x50497d(0x214)]();return console[_0x50497d(0x216)](_0x50497d(0x1c4),_0x5b0df0),0x0;}const _0x342c93=await _0x3295e0[_0x50497d(0x1b2)]();let _0x253c6e=0x0;if(Array['isArray'](_0x342c93))_0x253c6e=_0x342c93[_0x50497d(0x212)];else _0x342c93&&_0x342c93['hashes']&&(_0x253c6e=_0x342c93['hashes'][_0x50497d(0x212)]);return console['log'](_0x50497d(0x1f4)+_0x253c6e),_0x253c6e;}async function purgeStorage(){const _0x3c4160=_0x532886;console['log'](_0x3c4160(0x1df));const _0x5bd6dd=getCollectionId();console['log'](_0x3c4160(0x25f)+_0x5bd6dd);const _0x552aca={'collectionId':_0x5bd6dd};console[_0x3c4160(0x20e)](_0x3c4160(0x237),JSON[_0x3c4160(0x200)](_0x552aca,null,0x2));const _0x7d6271=await fetch(_0x3c4160(0x229),{'method':'POST','headers':context[_0x3c4160(0x1bf)](),'body':JSON['stringify'](_0x552aca)});console[_0x3c4160(0x20e)](_0x3c4160(0x1af)+_0x7d6271[_0x3c4160(0x1e9)]);if(!_0x7d6271['ok']){const _0x154397=await _0x7d6271['text']();console['error'](_0x3c4160(0x228),_0x154397);}else console[_0x3c4160(0x20e)](_0x3c4160(0x234));return _0x7d6271['ok'];}function getMessagesForCondensation(_0x5dfb63=null){const _0x26b77a=_0x532886;if(!settings[_0x26b77a(0x208)][_0x26b77a(0x1aa)])return showNotification('凝识之权未开启','warning'),[];const {layerStart:_0x2f1bbe,layerEnd:_0x56abda}=settings[_0x26b77a(0x208)],_0x2dd913=_0x5dfb63||settings[_0x26b77a(0x208)]['messageTypes'],_0x23b159=context[_0x26b77a(0x1ce)][_0x26b77a(0x212)],_0x395c70=Math[_0x26b77a(0x246)](0x0,_0x2f1bbe-0x1),_0x3b19ad=_0x56abda===0x0||_0x56abda>_0x23b159?_0x23b159:Math[_0x26b77a(0x1b3)](_0x23b159,_0x56abda),_0x522484=context[_0x26b77a(0x1ce)][_0x26b77a(0x25a)](_0x395c70,_0x3b19ad);return _0x522484[_0x26b77a(0x239)](_0xb3add1=>{const _0xd920=_0x26b77a,_0x740668=_0xb3add1[_0xd920(0x258)]===!![],_0x20b2c8=_0xb3add1[_0xd920(0x258)]===![];if(!_0xb3add1[_0xd920(0x1e0)]||!_0xb3add1[_0xd920(0x1e0)][_0xd920(0x1ef)]())return![];return _0x2dd913['user']&&_0x740668||_0x2dd913['ai']&&_0x20b2c8;});}async function processCondensation(_0x3d2b58,_0x2f6b44=()=>{},_0x1a6efa=null){const _0x15d70d=_0x532886;if(!_0x3d2b58||_0x3d2b58[_0x15d70d(0x212)]===0x0)return{'success':![],'error':_0x15d70d(0x20d)};try{const _0x766b96=getCollectionId();if(!_0x766b96)throw new Error('无法确定当前忆识宝库的ID,请确认角色已正确加载。');_0x2f6b44(_0x15d70d(0x1e3)+_0x766b96,_0x15d70d(0x262));const _0x40cbd7=[],_0x1ff638=context[_0x15d70d(0x1ce)],{chunkSize:_0xae507c,overlap:_0x384c1c}=settings[_0x15d70d(0x22b)];for(const _0x2bc70c of _0x3d2b58){const _0x44d2d8=(_0x2bc70c[_0x15d70d(0x1e0)]||'')[_0x15d70d(0x22f)](/<[^>]*>/g,'')['trim']();if(_0x44d2d8[_0x15d70d(0x212)]===0x0)continue;const _0x12191e=_0x1ff638['findIndex'](_0x3db5fc=>_0x3db5fc===_0x2bc70c),_0x4cf8d0=_0x12191e!==-0x1?_0x12191e+0x1:-0x1,_0xa0a09={'source':_0x15d70d(0x219),'sourceName':_0x15d70d(0x1d4)+_0x4cf8d0,'floor':_0x4cf8d0,'is_user':_0x2bc70c[_0x15d70d(0x258)],'timestamp':new Date(_0x2bc70c[_0x15d70d(0x1cb)])['toISOString']()};let _0xbe64b6=0x0;while(_0xbe64b6<_0x44d2d8[_0x15d70d(0x212)]){const _0x3baea3=Math['min'](_0xbe64b6+_0xae507c,_0x44d2d8[_0x15d70d(0x212)]),_0x41e8fb=_0x44d2d8['substring'](_0xbe64b6,_0x3baea3),_0x23bc13=_0x15d70d(0x1b4)+_0xa0a09[_0x15d70d(0x231)]+']\x0a'+_0x41e8fb+_0x15d70d(0x1cf);_0x40cbd7['push']({'text':_0x23bc13,'metadata':_0xa0a09}),_0xbe64b6+=_0xae507c-_0x384c1c;if(_0xbe64b6>=_0x44d2d8[_0x15d70d(0x212)])break;}}if(_0x40cbd7[_0x15d70d(0x212)]===0x0)return{'success':!![],'count':0x0};_0x2f6b44(_0x15d70d(0x1b6)+_0x3d2b58[_0x15d70d(0x212)]+_0x15d70d(0x1f8)+_0x40cbd7[_0x15d70d(0x212)]+_0x15d70d(0x210),'info');const _0x44b347=settings['retrieval'][_0x15d70d(0x1fb)]||0x5;let _0x2935be=0x0;for(let _0x2545a6=0x0;_0x2545a6<_0x40cbd7[_0x15d70d(0x212)];_0x2545a6+=_0x44b347){const _0x15bbda=_0x40cbd7['slice'](_0x2545a6,_0x2545a6+_0x44b347),_0x2fa5f4=_0x15bbda[_0x15d70d(0x254)](_0x564060=>_0x564060[_0x15d70d(0x214)]),_0x9bf044=await getEmbeddings(_0x2fa5f4);if(_0x15bbda[_0x15d70d(0x212)]!==_0x9bf044[_0x15d70d(0x212)])throw new Error('文本块和向量数量不匹配');const _0x31066b=_0x15bbda[_0x15d70d(0x254)]((_0x2cc220,_0xf01526)=>({..._0x2cc220,'vector':_0x9bf044[_0xf01526]}));await insertVectors(_0x31066b,null,_0x766b96),_0x2935be+=_0x15bbda[_0x15d70d(0x212)];}if(_0x1a6efa){const _0x3d2792=_0x1a6efa[_0x15d70d(0x1ec)]===0x0?context['chat'][_0x15d70d(0x212)]:_0x1a6efa['end'];settings[_0x15d70d(0x22d)][_0x766b96]={'start':_0x1a6efa[_0x15d70d(0x24c)],'end':_0x3d2792,'timestamp':new Date()[_0x15d70d(0x248)]()},saveSettings(),_0x2f6b44(_0x15d70d(0x1d5)+_0x766b96+'\x20记录凝识范围:\x20'+_0x1a6efa[_0x15d70d(0x24c)]+'-'+_0x3d2792,_0x15d70d(0x262));}_0x2f6b44(_0x15d70d(0x250)+_0x2935be+_0x15d70d(0x1d3),_0x15d70d(0x244));const _0x86d4c6=_0x3d2b58['map'](_0x52b070=>{const _0xc0bbb9=_0x15d70d,_0x47e81a=_0x1ff638[_0xc0bbb9(0x1c9)](_0x2fcc38=>_0x2fcc38===_0x52b070),_0x1830f3=_0x47e81a!==-0x1?_0x47e81a+0x1:-0x1,_0x1b5fe7=_0x52b070[_0xc0bbb9(0x258)]?'用户':getCharacterName()||'AI';return'['+_0x1b5fe7+_0xc0bbb9(0x222)+_0x1830f3+']\x20的消息已成功凝识。';});return{'success':!![],'count':_0x2935be,'messages':_0x86d4c6};}catch(_0x155512){return console[_0x15d70d(0x216)]('[翰林院-核心]\x20processCondensation\x20失败:',_0x155512),_0x2f6b44(_0x15d70d(0x1d9)+_0x155512['message'],_0x15d70d(0x216)),{'success':![],'error':_0x155512[_0x15d70d(0x25d)]};}}async function rerankResults(_0x215102,_0x506ee9,_0x547f5a){const _0x52362f=_0x532886;let _0x401d2e=_0x215102;if(_0x547f5a['rerank'][_0x52362f(0x1aa)]&&_0x215102[_0x52362f(0x212)]>0x0){console['log'](_0x52362f(0x226));try{const _0x5ea334=_0x215102[_0x52362f(0x254)]((_0x52dbe8,_0x12ff4e)=>({'text':_0x52dbe8[_0x52362f(0x214)],'original_index':_0x12ff4e})),_0x30d6e6=getRerankBaseUrl(_0x547f5a[_0x52362f(0x1b9)][_0x52362f(0x1fd)]),_0x3f9b5b=_0x30d6e6+_0x52362f(0x1c2),_0x56da48=await fetch(_0x3f9b5b,{'method':_0x52362f(0x1e7),'headers':{'Content-Type':_0x52362f(0x247),'Authorization':'Bearer\x20'+_0x547f5a['rerank'][_0x52362f(0x1ca)]},'body':JSON['stringify']({'query':_0x506ee9,'documents':_0x5ea334['map'](_0x40fbe0=>_0x40fbe0[_0x52362f(0x214)]),'model':_0x547f5a[_0x52362f(0x1b9)][_0x52362f(0x243)],'top_n':_0x547f5a[_0x52362f(0x1b9)][_0x52362f(0x1de)]})});if(!_0x56da48['ok'])throw new Error(_0x52362f(0x1ac)+_0x56da48[_0x52362f(0x1e9)]+_0x52362f(0x1ee)+await _0x56da48[_0x52362f(0x214)]());const _0x48e9b7=await _0x56da48[_0x52362f(0x1b2)](),_0x223c24=_0x215102[_0x52362f(0x254)]((_0x53ce0a,_0x22c540)=>({..._0x53ce0a,'original_index':_0x22c540}));_0x401d2e=_0x223c24[_0x52362f(0x254)](_0x2bd1e5=>{const _0x7c0057=_0x52362f,_0x557ac5=_0x48e9b7['results'][_0x7c0057(0x223)](_0x2715b2=>_0x2715b2[_0x7c0057(0x230)]===_0x2bd1e5['original_index']),_0x203f1b=_0x557ac5?_0x557ac5['relevance_score']:0x0;return{..._0x2bd1e5,'rerank_score':_0x203f1b};});if(_0x547f5a['rerank'][_0x52362f(0x1b8)])showNotification('外部Rerank完成',_0x52362f(0x244));}catch(_0x916241){console[_0x52362f(0x216)](_0x52362f(0x220),_0x916241);if(_0x547f5a[_0x52362f(0x1b9)][_0x52362f(0x1b8)])showNotification(_0x52362f(0x1b7)+_0x916241[_0x52362f(0x25d)],_0x52362f(0x216));_0x401d2e['forEach'](_0x3bede6=>_0x3bede6[_0x52362f(0x23b)]=0x0);}}else _0x401d2e[_0x52362f(0x207)](_0x2d6549=>_0x2d6549[_0x52362f(0x23b)]=0x0);console[_0x52362f(0x20e)](_0x52362f(0x23e));const _0x1476d0=context['chat'][_0x52362f(0x212)],_0x3cfc82=_0x547f5a[_0x52362f(0x1b9)]['hybrid_alpha'],_0x365ce5=_0x401d2e[_0x52362f(0x254)](_0x23d551=>{const _0x5ad6cb=_0x52362f;let _0x426ffa=0x1;const _0x6a714=_0x23d551[_0x5ad6cb(0x1e8)]||{};switch(_0x6a714[_0x5ad6cb(0x24b)]){case _0x5ad6cb(0x242):_0x426ffa*=1.2;break;case _0x5ad6cb(0x24a):_0x426ffa*=1.1;break;case _0x5ad6cb(0x219):if(_0x6a714[_0x5ad6cb(0x21d)]&&_0x1476d0>0x0){const _0x1d658d=_0x6a714[_0x5ad6cb(0x21d)]/_0x1476d0;_0x426ffa*=0x1+_0x1d658d;}break;}const _0x5540dc=_0x23d551[_0x5ad6cb(0x23b)]*_0x3cfc82+(_0x23d551['score']||0x0)*(0x1-_0x3cfc82),_0x7e9225=_0x5540dc*_0x426ffa;return{..._0x23d551,'final_score':_0x7e9225};});return _0x365ce5['sort']((_0x59978b,_0x4f4b0d)=>(_0x4f4b0d[_0x52362f(0x22e)]||0x0)-(_0x59978b[_0x52362f(0x22e)]||0x0)),console[_0x52362f(0x20e)]('[翰林院-Rerank]\x20元数据加权排序完成。'),_0x365ce5[_0x52362f(0x25a)](0x0,_0x547f5a[_0x52362f(0x1b9)][_0x52362f(0x1de)]);}async function rearrangeChat(_0x34126a,_0x46f5c1,_0x2aa462,_0x1b2726){const _0x3af388=_0x532886;setExtensionPrompt(_0x3af388(0x249),'',settings[_0x3af388(0x1d0)][_0x3af388(0x1f2)],settings['injection'][_0x3af388(0x235)],![],settings[_0x3af388(0x1d0)]['depth_role']);if(_0x1b2726==='quiet'||!settings[_0x3af388(0x1fa)]['enabled'])return;const _0x31ad09=_0x34126a[_0x3af388(0x25a)](-settings[_0x3af388(0x22b)][_0x3af388(0x1c1)]);if(_0x31ad09[_0x3af388(0x212)]===0x0)return;const _0x12f407=_0x31ad09[_0x3af388(0x254)](_0x409938=>_0x409938[_0x3af388(0x1e0)])[_0x3af388(0x213)]('\x20')[_0x3af388(0x22f)](/<[^>]*>/g,'')['trim']();if(!_0x12f407)return;try{const _0x57bd75=await queryVectors(_0x12f407);if(_0x57bd75[_0x3af388(0x212)]===0x0)return;const _0x5b6d6b=await rerankResults(_0x57bd75,_0x12f407,settings);if(_0x5b6d6b[_0x3af388(0x212)]===0x0)return;const _0xb046c5=_0x5b6d6b[_0x3af388(0x254)](_0x20db60=>_0x20db60[_0x3af388(0x214)])[_0x3af388(0x213)]('\x0a\x0a'),_0x4b65a6=settings['injection'][_0x3af388(0x1ab)][_0x3af388(0x22f)](_0x3af388(0x21b),_0xb046c5);setExtensionPrompt(_0x3af388(0x249),_0x4b65a6,settings[_0x3af388(0x1d0)][_0x3af388(0x1f2)],settings['injection'][_0x3af388(0x235)],![],settings[_0x3af388(0x1d0)][_0x3af388(0x25b)]);}catch(_0x307aad){console[_0x3af388(0x216)](_0x3af388(0x1da),_0x307aad);if(settings[_0x3af388(0x1fa)][_0x3af388(0x1b8)])showNotification(_0x3af388(0x21a)+_0x307aad[_0x3af388(0x25d)],'error');}} +'use strict';const _0x3ed94e=_0x1dec;(function(_0xf901a8,_0x3e5b54){const _0x25fad9=_0x1dec,_0x4558ab=_0xf901a8();while(!![]){try{const _0x23a24e=parseInt(_0x25fad9(0x205))/0x1+parseInt(_0x25fad9(0x15e))/0x2+-parseInt(_0x25fad9(0x1ff))/0x3*(parseInt(_0x25fad9(0x163))/0x4)+parseInt(_0x25fad9(0x210))/0x5*(-parseInt(_0x25fad9(0x19c))/0x6)+-parseInt(_0x25fad9(0x16b))/0x7*(-parseInt(_0x25fad9(0x1c6))/0x8)+-parseInt(_0x25fad9(0x169))/0x9*(parseInt(_0x25fad9(0x1df))/0xa)+parseInt(_0x25fad9(0x1ae))/0xb;if(_0x23a24e===_0x3e5b54)break;else _0x4558ab['push'](_0x4558ab['shift']());}catch(_0x382f8d){_0x4558ab['push'](_0x4558ab['shift']());}}}(_0x3c54,0xcfc3d));import{extension_prompt_roles,setExtensionPrompt}from'/script.js';import*as _0xeff614 from'./utils/context-utils.js';import{getCollectionIdInfo}from'./utils/context-utils.js';import{defaultSettings as _0x3c173d}from'./rag-settings.js';import*as _0x516397 from'./ingestion-manager.js';const MODULE_NAME=_0x3ed94e(0x176),OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME=_0x3ed94e(0x1d3);let context=null,settings=null,lockedCollectionId=null;export{initialize,getSettings,saveSettings,resetSettings,testApiConnection,fetchEmbeddingModels,fetchRerankModels,getVectorCount,purgeStorage,getMessagesForCondensation,processCondensation,ingestTextToHanlinyuan,getCollectionId,toggleSessionLock,isSessionLocked,getLockedSessionInfo};function initialize(){const _0x2d1099=_0x3ed94e;context=SillyTavern[_0x2d1099(0x184)]();if(!context){console[_0x2d1099(0x1be)](_0x2d1099(0x1b2));return;}settings=getSettings();const _0x29cd09=window[OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME];typeof _0x29cd09===_0x2d1099(0x20a)?(window[OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME]=async function(..._0x11d07c){await rearrangeChat(..._0x11d07c),await _0x29cd09(..._0x11d07c);},console[_0x2d1099(0x22b)]('翰林院忆识核心已启动\x20(V5.1-和平共存版),已代理\x20'+OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME)):(window[OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME]=rearrangeChat,console[_0x2d1099(0x22b)](_0x2d1099(0x212)+OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME));}async function ingestTextToHanlinyuan(_0x515db7,_0x3197a6=_0x3ed94e(0x194),_0x390264='',_0x10f680=()=>{},_0x267d7d=null,_0x5e3e89=()=>{},_0x1a7839=()=>{},_0x2a25e8=null,_0x5dcbf3=0x0,_0x484b68=null){const _0x17767a=_0x3ed94e;if(!_0x515db7||!_0x515db7['trim']())return{'success':![],'error':_0x17767a(0x1f9)};if(!settings)return{'success':![],'error':_0x17767a(0x229)};try{let _0xd00eea=await getCollectionId();const _0x1eb5e8=getCollectionIdInfo();if(_0x1eb5e8[_0x17767a(0x193)]&&_0x1eb5e8[_0x17767a(0x193)]===_0xd00eea&&_0x1eb5e8[_0x17767a(0x193)]!==_0x1eb5e8[_0x17767a(0x215)]){const _0x4fcad1=confirm(_0x17767a(0x171));if(_0x4fcad1)_0x5e3e89(_0x17767a(0x222)+_0x1eb5e8['oldId'],'warn'),await purgeStorage(_0x1eb5e8['oldId']),_0xd00eea=_0x1eb5e8[_0x17767a(0x215)],_0x5e3e89(_0x17767a(0x1b4)+_0xd00eea,'success');else return _0x5e3e89(_0x17767a(0x1a1),_0x17767a(0x188)),toastr[_0x17767a(0x188)]('操作已取消。'),{'success':![],'error':_0x17767a(0x1b8)};}if(!_0xd00eea)throw new Error('无法确定当前忆识宝库的ID,请确认角色已正确加载。');_0x5e3e89(_0x17767a(0x1c9)+_0xd00eea,_0x17767a(0x188)),_0x10f680({'message':_0x17767a(0x177),'processed':0x0,'total':0x1});const _0x4a6ce2=splitIntoChunks(_0x515db7,_0x3197a6,_0x390264),_0x1a9cf3=_0x4a6ce2[_0x17767a(0x1ca)];if(_0x267d7d?.[_0x17767a(0x206)])throw new Error(_0x17767a(0x1f2));_0x5e3e89(_0x17767a(0x170)+(_0x390264||_0x3197a6)+_0x17767a(0x1db)+_0x1a9cf3+_0x17767a(0x225),_0x17767a(0x188));if(_0x1a9cf3===0x0)return{'success':!![],'count':0x0};const _0x1481a5=settings[_0x17767a(0x1c2)][_0x17767a(0x180)]||0x5;let _0x37261c=_0x5dcbf3;for(let _0x28d685=_0x5dcbf3;_0x28d685<_0x1a9cf3;_0x28d685+=_0x1481a5){if(_0x267d7d?.['aborted'])throw new Error(_0x17767a(0x1f2));const _0x886407=_0x4a6ce2[_0x17767a(0x1aa)](_0x28d685,_0x28d685+_0x1481a5);_0x10f680({'message':'正在处理\x20'+(_0x28d685+0x1)+'-'+(_0x28d685+_0x886407[_0x17767a(0x1ca)])+'\x20块','processed':_0x28d685,'total':_0x1a9cf3});const _0x4a0665=_0x886407[_0x17767a(0x227)](_0x41cc36=>_0x41cc36[_0x17767a(0x1bc)]),_0x2b45cb=await getEmbeddings(_0x4a0665,_0x267d7d);if(_0x267d7d?.[_0x17767a(0x206)])throw new Error(_0x17767a(0x1f2));if(_0x886407[_0x17767a(0x1ca)]!==_0x2b45cb[_0x17767a(0x1ca)])throw new Error(_0x17767a(0x219));const _0xbfdc8=_0x886407[_0x17767a(0x227)]((_0x5adca7,_0x2607b2)=>({..._0x5adca7,'vector':_0x2b45cb[_0x2607b2]}));await insertVectors(_0xbfdc8,_0x267d7d,_0xd00eea),_0x37261c+=_0x886407[_0x17767a(0x1ca)],_0x2a25e8&&_0x516397[_0x17767a(0x18e)](_0x2a25e8,_0x37261c,_0x1a9cf3),_0x1a7839();}_0x2a25e8&&_0x516397[_0x17767a(0x1b7)](_0x2a25e8);if(_0x484b68){const _0x127e7d=await getCollectionId(),_0xdc5f7d=_0x484b68['end']===0x0?context[_0x17767a(0x19b)]['length']:_0x484b68[_0x17767a(0x160)];settings['condensationHistory'][_0x127e7d]={'start':_0x484b68[_0x17767a(0x1ec)],'end':_0xdc5f7d,'timestamp':new Date()[_0x17767a(0x1f6)]()},saveSettings(),_0x5e3e89(_0x17767a(0x201)+_0x127e7d+_0x17767a(0x1b0)+_0x484b68[_0x17767a(0x1ec)]+'-'+_0xdc5f7d,_0x17767a(0x188));}return _0x5e3e89(_0x17767a(0x1ef)+_0x37261c+_0x17767a(0x19f),'success'),{'success':!![],'count':_0x37261c};}catch(_0x265ecb){if(_0x265ecb['name']===_0x17767a(0x1f2)){_0x5e3e89(_0x17767a(0x208),_0x17767a(0x1d4));throw _0x265ecb;}return console['error'](_0x17767a(0x214),_0x265ecb),_0x5e3e89(_0x17767a(0x21d)+_0x265ecb['message'],_0x17767a(0x1be)),{'success':![],'error':_0x265ecb[_0x17767a(0x17a)]};}}function getSettings(){const _0x5ee9a6=_0x3ed94e;if(!context||!context[_0x5ee9a6(0x200)])return structuredClone(_0x3c173d);let _0x23154e=context[_0x5ee9a6(0x200)][MODULE_NAME];!_0x23154e&&(_0x23154e={},context['extensionSettings'][MODULE_NAME]=_0x23154e);_0x23154e[_0x5ee9a6(0x1bb)]===undefined&&(_0x23154e['condensationHistory']={});for(const _0x37f432 in _0x3c173d){if(_0x23154e[_0x37f432]===undefined)_0x23154e[_0x37f432]=structuredClone(_0x3c173d[_0x37f432]);else{if(typeof _0x3c173d[_0x37f432]===_0x5ee9a6(0x1fc)&&!Array[_0x5ee9a6(0x1bd)](_0x3c173d[_0x37f432])&&_0x3c173d[_0x37f432]!==null)for(const _0x5c200a in _0x3c173d[_0x37f432]){_0x23154e[_0x37f432][_0x5c200a]===undefined&&(_0x23154e[_0x37f432][_0x5c200a]=_0x3c173d[_0x37f432][_0x5c200a]);}}}return _0x23154e;}function saveSettings(){const _0x53b88c=_0x3ed94e;if(context)context[_0x53b88c(0x1a9)]();}function resetSettings(){context&&(context['extensionSettings'][MODULE_NAME]=structuredClone(_0x3c173d),saveSettings());}function _0x1dec(_0x3b40cb,_0x5bd71f){const _0x3c54ff=_0x3c54();return _0x1dec=function(_0x1dec58,_0x57ec5b){_0x1dec58=_0x1dec58-0x15e;let _0x475eaa=_0x3c54ff[_0x1dec58];return _0x475eaa;},_0x1dec(_0x3b40cb,_0x5bd71f);}function showNotification(_0x56328f,_0x4f88f3=_0x3ed94e(0x188)){toastr[_0x4f88f3](_0x56328f);}function getTagForSource(_0x136b18){const _0x4396c0=_0x3ed94e;switch(_0x136b18){case _0x4396c0(0x1cf):return _0x4396c0(0x1c8);case _0x4396c0(0x1c1):return _0x4396c0(0x185);case _0x4396c0(0x194):return _0x4396c0(0x1d7);case _0x4396c0(0x1af):return _0x4396c0(0x1c7);default:return'资料';}}function splitIntoChunks(_0x5217a1,_0x409941,_0x2197c8){const _0x15f105=_0x3ed94e,{chunkSize:_0x3bfabc,overlap:_0x2a85ec}=settings[_0x15f105(0x223)],_0x58a457=[];if(!_0x5217a1||_0x3bfabc<=0x0)return _0x58a457;const _0x67f6e2=/(第\s*[一二三四五六七八九十百千万零\d]+\s*卷)/gim,_0x43b5e3=/(第\s*[一二三四五六七八九十百千万零\d]+\s*[章回节部])|^(Chapter\s+\d+)/gim;let _0x39e38e=0x0,_0x5e7ec4=0x1,_0x5076de=0x1,_0x31dbd2=![];const _0x44c467=_0x5217a1[_0x15f105(0x186)]('\x0a');let _0x46ca2f='第1卷',_0x3be98c=_0x15f105(0x1f1),_0xab3d1=[];function _0x208db9(){const _0x56369c=_0x15f105;if(_0xab3d1[_0x56369c(0x1ca)]===0x0)return;const _0x201581=_0xab3d1[_0x56369c(0x1a4)]('\x0a');let _0x316f0d=0x0,_0x215080=0x1;while(_0x316f0d<_0x201581['length']){const _0x4a2e28=Math[_0x56369c(0x1cc)](_0x316f0d+_0x3bfabc,_0x201581[_0x56369c(0x1ca)]),_0x42b7b=_0x201581['substring'](_0x316f0d,_0x4a2e28);if(_0x42b7b[_0x56369c(0x1b9)]()['length']>0x0){const _0x1217ac={'source':_0x409941,'sourceName':_0x2197c8,'timestamp':new Date()[_0x56369c(0x1f6)](),'globalIndex':_0x39e38e++,'volume':_0x46ca2f,'chapter':_0x3be98c,'section':_0x215080},_0x4c66e5=getTagForSource(_0x409941),_0x43c5a3=_0x56369c(0x1ba)+_0x2197c8+',\x20'+_0x46ca2f+',\x20'+_0x3be98c+_0x56369c(0x21c)+_0x215080+'节]',_0x1135c6='<'+_0x4c66e5+'>\x0a'+_0x43c5a3+'\x0a'+_0x42b7b+_0x56369c(0x1f4)+_0x4c66e5+'>';_0x58a457[_0x56369c(0x21e)]({'text':_0x1135c6,'metadata':_0x1217ac}),_0x215080++;}_0x316f0d+=_0x3bfabc-_0x2a85ec;if(_0x316f0d>=_0x201581[_0x56369c(0x1ca)])break;}_0xab3d1=[];}for(const _0xb50c46 of _0x44c467){const _0x10f606=_0xb50c46[_0x15f105(0x1b9)]();if(_0x67f6e2[_0x15f105(0x224)](_0x10f606))_0x208db9(),_0x46ca2f=_0x10f606,_0x3be98c=_0x15f105(0x1f1),_0x5e7ec4++,_0x5076de=0x1,_0x31dbd2=!![];else _0x43b5e3['test'](_0x10f606)?(_0x208db9(),_0x3be98c=_0x10f606,_0x5076de++):_0xab3d1[_0x15f105(0x21e)](_0xb50c46);}_0x208db9();if(_0x58a457[_0x15f105(0x1ca)]===0x0&&_0x5217a1[_0x15f105(0x1ca)]>0x0){let _0xe1203d=0x0,_0x45ee3e=0x1;while(_0xe1203d<_0x5217a1['length']){const _0x5dc165=Math[_0x15f105(0x1cc)](_0xe1203d+_0x3bfabc,_0x5217a1[_0x15f105(0x1ca)]),_0x3ecbb5=_0x5217a1[_0x15f105(0x20e)](_0xe1203d,_0x5dc165),_0x88bdc0={'source':_0x409941,'sourceName':_0x2197c8,'timestamp':new Date()['toISOString'](),'globalIndex':_0x58a457[_0x15f105(0x1ca)],'volume':_0x15f105(0x197),'chapter':_0x15f105(0x1f1),'section':_0x45ee3e},_0x1c007a=getTagForSource(_0x409941),_0x35998e=_0x15f105(0x1ba)+_0x2197c8+_0x15f105(0x1ab)+_0x45ee3e+'节]',_0x5cdd95='<'+_0x1c007a+'>\x0a'+_0x35998e+'\x0a'+_0x3ecbb5+_0x15f105(0x1f4)+_0x1c007a+'>';_0x58a457[_0x15f105(0x21e)]({'text':_0x5cdd95,'metadata':_0x88bdc0}),_0x45ee3e++,_0xe1203d+=_0x3bfabc-_0x2a85ec;}}return _0x58a457;}import{getCollectionId as _0x1d1362,getCharacterName}from'./utils/context-utils.js';async function getCollectionId(){return lockedCollectionId||await _0x1d1362();}async function toggleSessionLock(){return lockedCollectionId?(lockedCollectionId=null,![]):(lockedCollectionId=await _0x1d1362(),!![]);}function isSessionLocked(){return lockedCollectionId!==null;}function getLockedSessionInfo(){const _0x222711=_0x3ed94e;if(!lockedCollectionId)return null;return{'id':lockedCollectionId,'name':_0x222711(0x182)+lockedCollectionId['substring'](0x0,0x8)+'...)'};}function generateHash(_0x13e4ac){const _0x2d150c=_0x3ed94e;let _0x4efc70=0x0;for(let _0x8c077d=0x0;_0x8c077d<_0x13e4ac[_0x2d150c(0x1ca)];_0x8c077d++){const _0x2e47d6=_0x13e4ac[_0x2d150c(0x17f)](_0x8c077d);_0x4efc70=(_0x4efc70<<0x5)-_0x4efc70+_0x2e47d6,_0x4efc70=_0x4efc70&_0x4efc70;}return Math[_0x2d150c(0x1d8)](_0x4efc70)[_0x2d150c(0x1ee)](0x24);}function getSanitizedBaseUrl(_0x4549e5){const _0x565916=_0x3ed94e;let _0x2476b6=_0x4549e5[_0x565916(0x1b9)]();return _0x2476b6['endsWith']('/')&&(_0x2476b6=_0x2476b6[_0x565916(0x1aa)](0x0,-0x1)),_0x2476b6[_0x565916(0x178)](_0x565916(0x19d))&&(_0x2476b6=_0x2476b6[_0x565916(0x1aa)](0x0,-0x3)),_0x2476b6[_0x565916(0x178)](_0x565916(0x162))&&(_0x2476b6=_0x2476b6['slice'](0x0,-0xb)),_0x2476b6;}async function fetchEmbeddingModels(){const _0x41fb4e=_0x3ed94e,{apiKey:_0x59deb7}=settings[_0x41fb4e(0x1c2)],_0x2cb8c6=getApiEndpointUrl(!![]);if(!_0x2cb8c6||!_0x59deb7)throw new Error(_0x41fb4e(0x1cb));const _0x4410ab=getSanitizedBaseUrl(_0x2cb8c6),_0x4ae960=_0x4410ab+'/v1/models';console['log'](_0x41fb4e(0x1fe)+_0x4ae960+_0x41fb4e(0x1dd));const _0x3a79d1=await fetch(_0x4ae960,{'method':_0x41fb4e(0x1a2),'headers':getApiHeaders()});if(!_0x3a79d1['ok']){const _0x5035d7=await _0x3a79d1[_0x41fb4e(0x1bc)]();throw new Error(_0x41fb4e(0x192)+_0x3a79d1[_0x41fb4e(0x216)]+_0x41fb4e(0x16f)+_0x5035d7);}const _0x3a04e6=await _0x3a79d1['json']();if(!_0x3a04e6[_0x41fb4e(0x173)]||!Array[_0x41fb4e(0x1bd)](_0x3a04e6[_0x41fb4e(0x173)]))throw new Error(_0x41fb4e(0x1d6));return _0x3a04e6[_0x41fb4e(0x173)][_0x41fb4e(0x227)](_0x1a50e3=>_0x1a50e3['id'])[_0x41fb4e(0x1ad)]();}function _0x3c54(){const _0x1ae7cb=['[翰林院-迁移]\x20用户确认迁移,正在清空旧宝库:\x20','advanced','test','\x20个块。','[翰林院-日志]\x20/api/vector/purge\x20响应状态:\x20','map','unknown','核心未初始化','enabled','log','embedding','injection','vector','\x20-\x20楼层\x20#','1464834vmEUdw','floor','end','操作已取消。','/embeddings','1316dTogZL','\x0a','Rerank\x20API\x20URL\x20或\x20Key\x20未提供。','webllm','url','/rerank','54VWKEeO','[翰林院-日志]\x20发送到\x20/api/vector/query\x20的请求体:','98714slcDMz','[翰林院-核心]\x20insertVectors被调用时未提供collectionId!','model','notify','):\x20','[翰林院-核心]\x20将来源\x27','此操作将清空旧版数据,并开始使用新版数据库。此过程不可逆,是否继续?','[翰林院-Rerank]\x20开始外部API重排序...','data','[翰林院-日志]\x20查询成功,返回\x20','[翰林院-日志]\x20/api/vector/query\x20响应内容:','hanlinyuan-rag-core','正在智能分块...','endsWith','send_date','message','json','聊天记录\x20#','success','忆识检索失败:\x20','charCodeAt','batchSize','depth','(已锁定:\x20','quiet','getContext','世界书','split','now','info','metadata','getRequestHeaders','embeddingModel','无法确定当前忆识宝库的ID,请确认角色已正确加载。','https://api.openai.com','saveProgress','maxResults','\x20个知识块,准备入库。','宝库查询API错误\x20','获取模型列表失败\x20(','oldId','manual','测试连接','hashes','第1卷','sourceName','final_score','/api/vector/insert','chat','54arIBTN','/v1','source','\x20个向量条目。','Bearer\x20','[翰林院-迁移]\x20用户取消了迁移操作。','GET','stringify','join','/api/vector/list','apiKey','condensation','\x20条消息分解为\x20','saveSettingsDebounced','slice',',\x20第1卷,\x20第1章,\x20第','[翰林院-Rerank]\x20元数据加权排序完成。','sort','2492611JgXTPQ','novel','\x20记录凝识范围:\x20','template','[翰林院]\x20未能获取SillyTavern上下文,初始化失败。','/v1/models','[翰林院-迁移]\x20旧宝库已清空,将向新宝库写入数据:\x20','[翰林院-日志]\x20发送到\x20/api/vector/list\x20的请求体:','filter','clearJob','用户取消了迁移操作','trim','[来源:\x20','condensationHistory','text','isArray','error','findIndex','[翰林院-日志]\x20/api/vector/list\x20响应状态:\x20','lorebook','retrieval','application/json','is_user','{{text}}','584WGqXtw','小说录入','聊天记录','[翰林院-核心]\x20已锁定忆识宝库ID:\x20','length','API\x20URL\x20或\x20Key\x20未提供。','min','[翰林院-核心]\x20聊天记录凝识失败:\x20','[翰林院-日志]\x20开始清空宝库...','chat_history','[翰林院-日志]\x20/api/vector/query\x20响应状态:\x20','results','神力获取失败\x20','vectors_rearrangeChat','warn','[翰林院-日志]\x20发送到\x20/api/vector/purge\x20的请求体:','模型API的响应格式无效:\x20未找到\x20\x27data\x27\x20数组。','手动录入','abs','[翰林院-核心]\x20processCondensation\x20失败:','[翰林院-日志]\x20查询目标集合ID:\x20','\x27的文本分割成\x20','/v1/rerank','\x20获取模型列表...','HANLINYUAN_RAG','895930yLqeVX','warning','[翰林院-日志]\x20统计成功,向量总数:\x20','position','rerank_score','[翰林院-日志]\x20清空宝库API调用成功。','top_n','无法确定要清空的目标宝库。','Rerank失败:\x20','Rerank\x20API\x20请求失败\x20(','replace','在insertVectors内部也无法获取collectionId','POST','start',']\x20的消息已成功凝识。','toString','[翰林院-核心]\x20成功插入\x20','外部Rerank完成','第1章','AbortError','score','\x0a\x0a[来源:\x20','1375435wKtQMa','aborted','Authorization','[翰林院-核心]\x20文本录入任务被用户中止。','获取Rerank模型列表失败\x20(','function','[翰林院-日志]\x20宝库查询API错误:','index','[翰林院-日志]\x20忆识存入API错误:','substring','\x20个条目。','645765CcPgPQ','mes','翰林院忆识核心已启动\x20(V5.1-借壳上市版),已注册全局函数:\x20','[翰林院-Rerank]\x20正在从\x20','[翰林院-核心]\x20ingestTextToHanlinyuan\x20失败:','newId','status','/v1/embeddings','rerank','文本块和向量数量不匹配','[翰林院-日志]\x20获取向量列表API错误:','[翰林院-核心]\x20已将\x20',',\x20第','[翰林院-核心]\x20文本录入失败:\x20','push','[翰林院-Rerank]\x20外部Rerank失败,将仅使用内部加权。','/api/vector/query','find'];_0x3c54=function(){return _0x1ae7cb;};return _0x3c54();}function getRerankBaseUrl(_0x1aa18c){const _0xd9aedb=_0x3ed94e;let _0x244dd2=_0x1aa18c[_0xd9aedb(0x1b9)]();return _0x244dd2['endsWith']('/')&&(_0x244dd2=_0x244dd2['slice'](0x0,-0x1)),_0x244dd2[_0xd9aedb(0x178)](_0xd9aedb(0x19d))&&(_0x244dd2=_0x244dd2[_0xd9aedb(0x1aa)](0x0,-0x3)),_0x244dd2[_0xd9aedb(0x178)](_0xd9aedb(0x168))&&(_0x244dd2=_0x244dd2[_0xd9aedb(0x1aa)](0x0,-0x7)),_0x244dd2;}async function fetchRerankModels(){const _0x5efd4b=_0x3ed94e,{url:_0x28d2d3,apiKey:_0x1459ea}=settings['rerank'];if(!_0x28d2d3||!_0x1459ea)throw new Error(_0x5efd4b(0x165));const _0x3acc9f=getRerankBaseUrl(_0x28d2d3),_0x57e178=_0x3acc9f+_0x5efd4b(0x1b3);console[_0x5efd4b(0x22b)](_0x5efd4b(0x213)+_0x57e178+'\x20获取模型列表...');const _0x2c38b4=await fetch(_0x57e178,{'method':'GET','headers':{'Authorization':'Bearer\x20'+_0x1459ea}});if(!_0x2c38b4['ok']){const _0x2bf936=await _0x2c38b4[_0x5efd4b(0x1bc)]();throw new Error(_0x5efd4b(0x209)+_0x2c38b4[_0x5efd4b(0x216)]+_0x5efd4b(0x16f)+_0x2bf936);}const _0x1dc535=await _0x2c38b4[_0x5efd4b(0x17b)]();if(!_0x1dc535[_0x5efd4b(0x173)]||!Array[_0x5efd4b(0x1bd)](_0x1dc535[_0x5efd4b(0x173)]))throw new Error('Rerank模型API的响应格式无效:\x20未找到\x20\x27data\x27\x20数组。');return _0x1dc535[_0x5efd4b(0x173)]['map'](_0x5358b0=>_0x5358b0['id'])[_0x5efd4b(0x1ad)]();}function getApiEndpointUrl(_0x818c42=![]){const _0x159a9e=_0x3ed94e,{apiEndpoint:_0x508ced,customApiUrl:_0x1a9e15}=settings['retrieval'];let _0x267333;switch(_0x508ced){case _0x159a9e(0x1fb):_0x267333=_0x159a9e(0x18d);break;case'azure':case _0x159a9e(0x202):_0x267333=_0x1a9e15;break;default:_0x267333=_0x159a9e(0x18d);break;}if(_0x818c42)return _0x267333;return getSanitizedBaseUrl(_0x267333)+_0x159a9e(0x217);}function getApiHeaders(){const _0x32e445=_0x3ed94e,_0x4efa35={'Content-Type':_0x32e445(0x1c3)},{apiKey:_0x326caa,apiEndpoint:_0x18d348}=settings['retrieval'];switch(_0x18d348){case'openai':case _0x32e445(0x202):_0x4efa35[_0x32e445(0x207)]=_0x32e445(0x1a0)+_0x326caa;break;case'azure':_0x4efa35['api-key']=_0x326caa;break;}return _0x4efa35;}async function getEmbeddings(_0x4859fe,_0x5af437=null){const _0x28772e=_0x3ed94e;if(!settings[_0x28772e(0x1c2)]['apiKey'])throw new Error('请先配置API\x20Key');const _0x4b6dfe=getApiEndpointUrl(),_0x5cfcd6=getApiHeaders(),_0x2d44d8=settings[_0x28772e(0x1c2)][_0x28772e(0x18b)],_0x20de67=settings[_0x28772e(0x1c2)][_0x28772e(0x180)]||0x5,_0x52c9ad=[];for(let _0x2cb5d3=0x0;_0x2cb5d3<_0x4859fe[_0x28772e(0x1ca)];_0x2cb5d3+=_0x20de67){if(_0x5af437?.[_0x28772e(0x206)])throw new Error(_0x28772e(0x1f2));const _0x412a18=_0x4859fe['slice'](_0x2cb5d3,_0x2cb5d3+_0x20de67),_0x597c89=await fetch(_0x4b6dfe,{'method':'POST','headers':_0x5cfcd6,'body':JSON[_0x28772e(0x1a3)]({'input':_0x412a18,'model':_0x2d44d8}),'signal':_0x5af437});if(!_0x597c89['ok']){const _0x143b1c=await _0x597c89['text']();throw new Error(_0x28772e(0x1d2)+_0x597c89['status']+':\x20'+_0x143b1c);}const _0x32bbf1=await _0x597c89[_0x28772e(0x17b)]();_0x52c9ad[_0x28772e(0x21e)](..._0x32bbf1[_0x28772e(0x173)][_0x28772e(0x227)](_0x5e6d58=>_0x5e6d58[_0x28772e(0x22c)])),_0x2cb5d3+_0x20de67<_0x4859fe['length']&&await new Promise(_0x2d8453=>setTimeout(_0x2d8453,0xc8));}return _0x52c9ad;}async function queryVectors(_0x149671){const _0x10afd3=_0x3ed94e;console[_0x10afd3(0x22b)]('[翰林院-日志]\x20开始向量查询\x20(采用最终API交互模式)...');const _0x4ccec5=await getCollectionId();console[_0x10afd3(0x22b)](_0x10afd3(0x1da)+_0x4ccec5);const _0x427f03=(await getEmbeddings([_0x149671]))[0x0],_0x11f9eb={'collectionId':_0x4ccec5,'searchText':_0x149671,'topK':settings[_0x10afd3(0x223)][_0x10afd3(0x18f)],'threshold':settings[_0x10afd3(0x223)][_0x10afd3(0x1fa)],'source':_0x10afd3(0x166),'embeddings':{[_0x149671]:_0x427f03}};console[_0x10afd3(0x22b)](_0x10afd3(0x16a),JSON[_0x10afd3(0x1a3)](_0x11f9eb,null,0x2));const _0x245004=await fetch(_0x10afd3(0x220),{'method':_0x10afd3(0x1eb),'headers':context['getRequestHeaders'](),'body':JSON['stringify'](_0x11f9eb)});console[_0x10afd3(0x22b)](_0x10afd3(0x1d0)+_0x245004[_0x10afd3(0x216)]);if(!_0x245004['ok']){const _0x5e8a2c=await _0x245004[_0x10afd3(0x1bc)]();console['error'](_0x10afd3(0x20b),_0x5e8a2c);throw new Error(_0x10afd3(0x191)+_0x245004[_0x10afd3(0x216)]+':\x20'+_0x5e8a2c);}const _0x488f4f=await _0x245004[_0x10afd3(0x17b)]();console[_0x10afd3(0x22b)](_0x10afd3(0x175),_0x488f4f);const _0x197280=_0x488f4f[_0x10afd3(0x189)]||_0x488f4f['results']||_0x488f4f[_0x10afd3(0x173)]||[];return console['log'](_0x10afd3(0x174)+_0x197280[_0x10afd3(0x1ca)]+_0x10afd3(0x1f7)),_0x197280;}async function insertVectors(_0xbae44f,_0x7ff9ad=null,_0x57072a){const _0xeac383=_0x3ed94e;if(!_0x57072a){console['error'](_0xeac383(0x16c)),_0x57072a=await getCollectionId();if(!_0x57072a)throw new Error(_0xeac383(0x1ea));}if(_0xbae44f[_0xeac383(0x1ca)]===0x0)return{'success':!![],'count':0x0};const _0x54af99=_0xbae44f[_0xeac383(0x227)]((_0x1df100,_0x42add0)=>({'hash':generateHash(_0x1df100[_0xeac383(0x1bc)]+Date[_0xeac383(0x187)]()+_0x42add0),'text':_0x1df100[_0xeac383(0x1bc)],'metadata':_0x1df100[_0xeac383(0x189)]||{'source':_0xeac383(0x228),'timestamp':new Date()[_0xeac383(0x1f6)]()}})),_0x2b5d45=_0x54af99['reduce']((_0x9ed977,_0x45218b,_0x1ff36c)=>{const _0x3a3377=_0xeac383;return _0x9ed977[_0x45218b[_0x3a3377(0x1bc)]]=_0xbae44f[_0x1ff36c][_0x3a3377(0x22e)],_0x9ed977;},{}),_0x21d8fe={'collectionId':_0x57072a,'items':_0x54af99,'source':_0xeac383(0x166),'embeddings':_0x2b5d45},_0x3ca66e=await fetch(_0xeac383(0x19a),{'method':_0xeac383(0x1eb),'headers':context[_0xeac383(0x18a)](),'body':JSON[_0xeac383(0x1a3)](_0x21d8fe),'signal':_0x7ff9ad});if(!_0x3ca66e['ok']){const _0x5537c1=await _0x3ca66e[_0xeac383(0x1bc)]();console[_0xeac383(0x1be)](_0xeac383(0x20d),_0x5537c1);throw new Error('忆识存入API错误\x20'+_0x3ca66e[_0xeac383(0x216)]+':\x20'+_0x5537c1);}return{'success':!![],'count':_0x54af99['length']};}async function testApiConnection(){const _0x47ec94=_0x3ed94e;await getEmbeddings([_0x47ec94(0x195)]);}async function getVectorCount(){const _0x22e980=_0x3ed94e;console[_0x22e980(0x22b)]('[翰林院-日志]\x20开始获取向量总数...');const _0x3bd06d=await getCollectionId();console[_0x22e980(0x22b)]('[翰林院-日志]\x20统计目标集合ID:\x20'+_0x3bd06d);const _0x2445d7={'collectionId':_0x3bd06d,'source':'webllm','embeddings':{}};console[_0x22e980(0x22b)](_0x22e980(0x1b5),JSON['stringify'](_0x2445d7,null,0x2));const _0x47ea99=await fetch(_0x22e980(0x1a5),{'method':_0x22e980(0x1eb),'headers':context[_0x22e980(0x18a)](),'body':JSON['stringify'](_0x2445d7)});console[_0x22e980(0x22b)](_0x22e980(0x1c0)+_0x47ea99['status']);if(!_0x47ea99['ok']){const _0x79b46d=await _0x47ea99['text']();return console[_0x22e980(0x1be)](_0x22e980(0x21a),_0x79b46d),0x0;}const _0x1224e6=await _0x47ea99[_0x22e980(0x17b)]();let _0x29be12=0x0;if(Array['isArray'](_0x1224e6))_0x29be12=_0x1224e6[_0x22e980(0x1ca)];else _0x1224e6&&_0x1224e6['hashes']&&(_0x29be12=_0x1224e6[_0x22e980(0x196)][_0x22e980(0x1ca)]);return console[_0x22e980(0x22b)](_0x22e980(0x1e1)+_0x29be12),_0x29be12;}async function purgeStorage(_0x49bd61=null){const _0x3a0952=_0x3ed94e;console[_0x3a0952(0x22b)](_0x3a0952(0x1ce));const _0x38ec16=_0x49bd61||await getCollectionId();if(!_0x38ec16)return console['error']('[翰林院-日志]\x20无法确定要清空的目标集合ID。'),toastr[_0x3a0952(0x1be)](_0x3a0952(0x1e6)),![];console[_0x3a0952(0x22b)]('[翰林院-日志]\x20清空目标集合ID:\x20'+_0x38ec16);const _0x21babf={'collectionId':_0x38ec16};console[_0x3a0952(0x22b)](_0x3a0952(0x1d5),JSON[_0x3a0952(0x1a3)](_0x21babf,null,0x2));const _0x4eedfb=await fetch('/api/vector/purge',{'method':_0x3a0952(0x1eb),'headers':context['getRequestHeaders'](),'body':JSON[_0x3a0952(0x1a3)](_0x21babf)});console[_0x3a0952(0x22b)](_0x3a0952(0x226)+_0x4eedfb[_0x3a0952(0x216)]);if(!_0x4eedfb['ok']){const _0x54fe45=await _0x4eedfb['text']();console[_0x3a0952(0x1be)]('[翰林院-日志]\x20清空宝库API错误:',_0x54fe45);}else console[_0x3a0952(0x22b)](_0x3a0952(0x1e4));return _0x4eedfb['ok'];}function getMessagesForCondensation(_0x5d4e34=null){const _0x51f772=_0x3ed94e;if(!settings[_0x51f772(0x1a7)]['enabled'])return showNotification(_0x51f772(0x1f5),_0x51f772(0x1e0)),[];const {layerStart:_0x3634e6,layerEnd:_0xf62c70}=settings[_0x51f772(0x1a7)],_0x65d3b1=_0x5d4e34||settings[_0x51f772(0x1a7)]['messageTypes'],_0x85da4=context[_0x51f772(0x19b)][_0x51f772(0x1ca)],_0x111531=Math[_0x51f772(0x203)](0x0,_0x3634e6-0x1),_0x197b9e=_0xf62c70===0x0||_0xf62c70>_0x85da4?_0x85da4:Math[_0x51f772(0x1cc)](_0x85da4,_0xf62c70),_0xcec780=context[_0x51f772(0x19b)][_0x51f772(0x1aa)](_0x111531,_0x197b9e);return _0xcec780[_0x51f772(0x1b6)](_0x1a0ce6=>{const _0x4ec95e=_0x51f772,_0x112568=_0x1a0ce6['is_user']===!![],_0x231d20=_0x1a0ce6[_0x4ec95e(0x1c4)]===![];if(!_0x1a0ce6['mes']||!_0x1a0ce6[_0x4ec95e(0x211)]['trim']())return![];return _0x65d3b1['user']&&_0x112568||_0x65d3b1['ai']&&_0x231d20;});}async function processCondensation(_0x3e45b2,_0x2d5eb5=()=>{},_0x181ba2=null){const _0x33fa31=_0x3ed94e;if(!_0x3e45b2||_0x3e45b2[_0x33fa31(0x1ca)]===0x0)return{'success':![],'error':'No\x20messages\x20to\x20process.'};try{let _0xc65cb1=await getCollectionId();const _0x2956ce=getCollectionIdInfo();if(_0x2956ce['oldId']&&_0x2956ce[_0x33fa31(0x193)]===_0xc65cb1&&_0x2956ce[_0x33fa31(0x193)]!==_0x2956ce[_0x33fa31(0x215)]){const _0x365a7c=confirm(_0x33fa31(0x171));if(_0x365a7c)_0x2d5eb5(_0x33fa31(0x222)+_0x2956ce[_0x33fa31(0x193)],_0x33fa31(0x1d4)),await purgeStorage(_0x2956ce[_0x33fa31(0x193)]),_0xc65cb1=_0x2956ce[_0x33fa31(0x215)],_0x2d5eb5(_0x33fa31(0x1b4)+_0xc65cb1,_0x33fa31(0x17d));else return _0x2d5eb5(_0x33fa31(0x1a1),_0x33fa31(0x188)),toastr['info'](_0x33fa31(0x161)),{'success':![],'error':'用户取消了迁移操作'};}if(!_0xc65cb1)throw new Error(_0x33fa31(0x18c));_0x2d5eb5('[翰林院-核心]\x20凝识任务已锁定忆识宝库ID:\x20'+_0xc65cb1,_0x33fa31(0x188));const _0x3830c5=[],_0x14339e=context[_0x33fa31(0x19b)],{chunkSize:_0xf48cb5,overlap:_0x127f09}=settings[_0x33fa31(0x223)];for(const _0x27a7da of _0x3e45b2){const _0x503852=(_0x27a7da[_0x33fa31(0x211)]||'')[_0x33fa31(0x1e9)](/<[^>]*>/g,'')[_0x33fa31(0x1b9)]();if(_0x503852[_0x33fa31(0x1ca)]===0x0)continue;const _0x4f1dca=_0x14339e[_0x33fa31(0x1bf)](_0x3ad860=>_0x3ad860===_0x27a7da),_0x29a377=_0x4f1dca!==-0x1?_0x4f1dca+0x1:-0x1,_0x2715a5={'source':'chat_history','sourceName':_0x33fa31(0x17c)+_0x29a377,'floor':_0x29a377,'is_user':_0x27a7da[_0x33fa31(0x1c4)],'timestamp':new Date(_0x27a7da[_0x33fa31(0x179)])['toISOString']()};let _0x48ed04=0x0;while(_0x48ed04<_0x503852[_0x33fa31(0x1ca)]){const _0x102290=Math[_0x33fa31(0x1cc)](_0x48ed04+_0xf48cb5,_0x503852[_0x33fa31(0x1ca)]),_0x12e3e8=_0x503852['substring'](_0x48ed04,_0x102290),_0x3364cd=_0x33fa31(0x204)+_0x2715a5[_0x33fa31(0x198)]+']\x0a'+_0x12e3e8+_0x33fa31(0x164);_0x3830c5[_0x33fa31(0x21e)]({'text':_0x3364cd,'metadata':_0x2715a5}),_0x48ed04+=_0xf48cb5-_0x127f09;if(_0x48ed04>=_0x503852['length'])break;}}if(_0x3830c5[_0x33fa31(0x1ca)]===0x0)return{'success':!![],'count':0x0};_0x2d5eb5(_0x33fa31(0x21b)+_0x3e45b2[_0x33fa31(0x1ca)]+_0x33fa31(0x1a8)+_0x3830c5[_0x33fa31(0x1ca)]+_0x33fa31(0x190),_0x33fa31(0x188));const _0x152cc6=settings[_0x33fa31(0x1c2)][_0x33fa31(0x180)]||0x5;let _0xdc4dd=0x0;for(let _0x2dc160=0x0;_0x2dc160<_0x3830c5['length'];_0x2dc160+=_0x152cc6){const _0x4f8d33=_0x3830c5[_0x33fa31(0x1aa)](_0x2dc160,_0x2dc160+_0x152cc6),_0x45780d=_0x4f8d33['map'](_0x1f6a16=>_0x1f6a16['text']),_0x13b742=await getEmbeddings(_0x45780d);if(_0x4f8d33['length']!==_0x13b742[_0x33fa31(0x1ca)])throw new Error(_0x33fa31(0x219));const _0x170141=_0x4f8d33[_0x33fa31(0x227)]((_0x4e89d7,_0x1f59fa)=>({..._0x4e89d7,'vector':_0x13b742[_0x1f59fa]}));await insertVectors(_0x170141,null,_0xc65cb1),_0xdc4dd+=_0x4f8d33[_0x33fa31(0x1ca)];}if(_0x181ba2){const _0x41c943=_0x181ba2[_0x33fa31(0x160)]===0x0?context[_0x33fa31(0x19b)][_0x33fa31(0x1ca)]:_0x181ba2[_0x33fa31(0x160)];settings['condensationHistory'][_0xc65cb1]={'start':_0x181ba2[_0x33fa31(0x1ec)],'end':_0x41c943,'timestamp':new Date()[_0x33fa31(0x1f6)]()},saveSettings(),_0x2d5eb5(_0x33fa31(0x201)+_0xc65cb1+_0x33fa31(0x1b0)+_0x181ba2[_0x33fa31(0x1ec)]+'-'+_0x41c943,'info');}_0x2d5eb5('[翰林院-核心]\x20聊天记录凝识完成,成功插入\x20'+_0xdc4dd+_0x33fa31(0x20f),_0x33fa31(0x17d));const _0x4dcf36=_0x3e45b2[_0x33fa31(0x227)](_0x95965=>{const _0x4f075b=_0x33fa31,_0x4bfb1f=_0x14339e[_0x4f075b(0x1bf)](_0x3197ff=>_0x3197ff===_0x95965),_0x138b98=_0x4bfb1f!==-0x1?_0x4bfb1f+0x1:-0x1,_0x1d40e8=_0x95965[_0x4f075b(0x1c4)]?'用户':getCharacterName()||'AI';return'['+_0x1d40e8+_0x4f075b(0x22f)+_0x138b98+_0x4f075b(0x1ed);});return{'success':!![],'count':_0xdc4dd,'messages':_0x4dcf36};}catch(_0x4da02d){return console[_0x33fa31(0x1be)](_0x33fa31(0x1d9),_0x4da02d),_0x2d5eb5(_0x33fa31(0x1cd)+_0x4da02d['message'],_0x33fa31(0x1be)),{'success':![],'error':_0x4da02d['message']};}}async function rerankResults(_0x53b2cb,_0x4eddd0,_0x584baf){const _0x35536f=_0x3ed94e;let _0x45c30f=_0x53b2cb;if(_0x584baf[_0x35536f(0x218)][_0x35536f(0x22a)]&&_0x53b2cb[_0x35536f(0x1ca)]>0x0){console['log'](_0x35536f(0x172));try{const _0x560db6=_0x53b2cb['map']((_0x47f487,_0x255122)=>({'text':_0x47f487[_0x35536f(0x1bc)],'original_index':_0x255122})),_0x168c2c=getRerankBaseUrl(_0x584baf['rerank'][_0x35536f(0x167)]),_0x51b3ef=_0x168c2c+_0x35536f(0x1dc),_0xc70768=await fetch(_0x51b3ef,{'method':_0x35536f(0x1eb),'headers':{'Content-Type':_0x35536f(0x1c3),'Authorization':_0x35536f(0x1a0)+_0x584baf['rerank'][_0x35536f(0x1a6)]},'body':JSON[_0x35536f(0x1a3)]({'query':_0x4eddd0,'documents':_0x560db6[_0x35536f(0x227)](_0x265e02=>_0x265e02[_0x35536f(0x1bc)]),'model':_0x584baf['rerank'][_0x35536f(0x16d)],'top_n':_0x584baf[_0x35536f(0x218)][_0x35536f(0x1e5)]})});if(!_0xc70768['ok'])throw new Error(_0x35536f(0x1e8)+_0xc70768[_0x35536f(0x216)]+_0x35536f(0x16f)+await _0xc70768[_0x35536f(0x1bc)]());const _0x323bb1=await _0xc70768[_0x35536f(0x17b)](),_0x2a0526=_0x53b2cb[_0x35536f(0x227)]((_0x24a7e7,_0x3cd285)=>({..._0x24a7e7,'original_index':_0x3cd285}));_0x45c30f=_0x2a0526[_0x35536f(0x227)](_0xc46f23=>{const _0x1d31de=_0x35536f,_0x178f2c=_0x323bb1[_0x1d31de(0x1d1)][_0x1d31de(0x221)](_0x5e3507=>_0x5e3507[_0x1d31de(0x20c)]===_0xc46f23['original_index']),_0x42b4b9=_0x178f2c?_0x178f2c['relevance_score']:0x0;return{..._0xc46f23,'rerank_score':_0x42b4b9};});if(_0x584baf[_0x35536f(0x218)]['notify'])showNotification(_0x35536f(0x1f0),_0x35536f(0x17d));}catch(_0x229af7){console[_0x35536f(0x1be)](_0x35536f(0x21f),_0x229af7);if(_0x584baf['rerank'][_0x35536f(0x16e)])showNotification(_0x35536f(0x1e7)+_0x229af7['message'],'error');_0x45c30f['forEach'](_0x3b75ae=>_0x3b75ae['rerank_score']=0x0);}}else _0x45c30f['forEach'](_0xd7f81c=>_0xd7f81c[_0x35536f(0x1e3)]=0x0);console[_0x35536f(0x22b)]('[翰林院-Rerank]\x20开始元数据加权最终排序...');const _0xfed83f=context[_0x35536f(0x19b)]['length'],_0x218630=_0x584baf[_0x35536f(0x218)]['hybrid_alpha'],_0x17d636=_0x45c30f[_0x35536f(0x227)](_0x3b5df2=>{const _0x2d5acf=_0x35536f;let _0x10b7e5=0x1;const _0x5ac42f=_0x3b5df2[_0x2d5acf(0x189)]||{};switch(_0x5ac42f[_0x2d5acf(0x19e)]){case'lorebook':_0x10b7e5*=1.2;break;case _0x2d5acf(0x194):_0x10b7e5*=1.1;break;case _0x2d5acf(0x1cf):if(_0x5ac42f[_0x2d5acf(0x15f)]&&_0xfed83f>0x0){const _0x5b6c19=_0x5ac42f['floor']/_0xfed83f;_0x10b7e5*=0x1+_0x5b6c19;}break;}const _0x1fd074=_0x3b5df2['rerank_score']*_0x218630+(_0x3b5df2[_0x2d5acf(0x1f3)]||0x0)*(0x1-_0x218630),_0x40fee6=_0x1fd074*_0x10b7e5;return{..._0x3b5df2,'final_score':_0x40fee6};});return _0x17d636['sort']((_0x565ca6,_0x233956)=>(_0x233956['final_score']||0x0)-(_0x565ca6[_0x35536f(0x199)]||0x0)),console[_0x35536f(0x22b)](_0x35536f(0x1ac)),_0x17d636['slice'](0x0,_0x584baf[_0x35536f(0x218)][_0x35536f(0x1e5)]);}async function rearrangeChat(_0x378499,_0x5c228b,_0x46e209,_0x48ed55){const _0x2e8756=_0x3ed94e;setExtensionPrompt('HANLINYUAN_RAG','',settings['injection'][_0x2e8756(0x1e2)],settings[_0x2e8756(0x22d)][_0x2e8756(0x181)],![],settings['injection'][_0x2e8756(0x1fd)]);if(_0x48ed55===_0x2e8756(0x183)||!settings[_0x2e8756(0x1c2)][_0x2e8756(0x22a)])return;const _0x5b1f46=_0x378499[_0x2e8756(0x1aa)](-settings[_0x2e8756(0x223)]['queryMessageCount']);if(_0x5b1f46['length']===0x0)return;const _0x18257c=_0x5b1f46[_0x2e8756(0x227)](_0x4de8d1=>_0x4de8d1['mes'])['join']('\x20')[_0x2e8756(0x1e9)](/<[^>]*>/g,'')[_0x2e8756(0x1b9)]();if(!_0x18257c)return;try{const _0x34c3b9=await queryVectors(_0x18257c);if(_0x34c3b9[_0x2e8756(0x1ca)]===0x0)return;const _0x41a6c2=await rerankResults(_0x34c3b9,_0x18257c,settings);if(_0x41a6c2['length']===0x0)return;const _0x15b7d8=_0x41a6c2[_0x2e8756(0x227)](_0x5e3f08=>_0x5e3f08[_0x2e8756(0x1bc)])[_0x2e8756(0x1a4)]('\x0a\x0a'),_0xc15a54=settings[_0x2e8756(0x22d)][_0x2e8756(0x1b1)]['replace'](_0x2e8756(0x1c5),_0x15b7d8);setExtensionPrompt(_0x2e8756(0x1de),_0xc15a54,settings[_0x2e8756(0x22d)]['position'],settings['injection'][_0x2e8756(0x181)],![],settings['injection'][_0x2e8756(0x1fd)]);}catch(_0x35977c){console[_0x2e8756(0x1be)](_0x2e8756(0x1f8),_0x35977c);if(settings[_0x2e8756(0x1c2)]['notify'])showNotification(_0x2e8756(0x17e)+_0x35977c[_0x2e8756(0x17a)],_0x2e8756(0x1be));}}