Files
ST-Amily2-Chat-Optimisation/core/rag-processor.js
2025-09-12 12:29:43 +08:00

2 lines
41 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 _0x31e176=_0x2fe0;(function(_0x44c9c9,_0x2afb28){const _0x1c96cb=_0x2fe0,_0x4f7919=_0x44c9c9();while(!![]){try{const _0x5f12c1=parseInt(_0x1c96cb(0x215))/0x1*(parseInt(_0x1c96cb(0x213))/0x2)+-parseInt(_0x1c96cb(0x1e5))/0x3+-parseInt(_0x1c96cb(0x225))/0x4+parseInt(_0x1c96cb(0x21e))/0x5*(parseInt(_0x1c96cb(0x260))/0x6)+parseInt(_0x1c96cb(0x264))/0x7*(-parseInt(_0x1c96cb(0x239))/0x8)+-parseInt(_0x1c96cb(0x201))/0x9+parseInt(_0x1c96cb(0x221))/0xa*(parseInt(_0x1c96cb(0x1ee))/0xb);if(_0x5f12c1===_0x2afb28)break;else _0x4f7919['push'](_0x4f7919['shift']());}catch(_0x40a4db){_0x4f7919['push'](_0x4f7919['shift']());}}}(_0x4a15,0xdd078));import{extension_prompt_roles,setExtensionPrompt}from'/script.js';import*as _0x1763b7 from'./utils/context-utils.js';import{getCollectionIdInfo,getCharacterId,getCharacterStableId}from'./utils/context-utils.js';import{defaultSettings as _0x1e160b}from'./rag-settings.js';import*as _0x16db44 from'./ingestion-manager.js';import{getEmbeddings,fetchEmbeddingModels as _0x128292,fetchRerankModels as _0x8ed07f,executeRerank,testApiConnection as _0x244eee}from'./rag-api.js';const MODULE_NAME='hanlinyuan-rag-core',OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME='vectors_rearrangeChat',GLOBAL_SCOPE_ID=_0x31e176(0x216);let context=null,settings=null,lockedCollectionId=null;export{initialize,getSettings,saveSettings,resetSettings,_0x244eee as testApiConnection,_0x128292 as fetchEmbeddingModels,_0x8ed07f as fetchRerankModels,getVectorCount,purgeStorage,getMessagesForCondensation,processCondensation,ingestTextToHanlinyuan,getCollectionId,toggleSessionLock,isSessionLocked,getLockedSessionInfo,addKnowledgeBase,removeKnowledgeBase,getLocalKnowledgeBases,getGlobalKnowledgeBases,toggleKnowledgeBase,moveKnowledgeBase};function initialize(){const _0x3af5bd=_0x31e176;context=SillyTavern[_0x3af5bd(0x1f4)]();if(!context){console['error'](_0x3af5bd(0x1f9));return;}settings=getSettings(),!window['hanlinyuanRagProcessor']&&(window[_0x3af5bd(0x1d4)]={}),window[_0x3af5bd(0x1d4)][_0x3af5bd(0x1c1)]=rearrangeChat,window[_0x3af5bd(0x1d4)][_0x3af5bd(0x1fd)]=!![],console[_0x3af5bd(0x25d)](_0x3af5bd(0x1aa));}async function ingestTextToHanlinyuan(_0x53c37a,_0x3f24af=_0x31e176(0x23e),_0x2f450d={},_0x2f22ab=()=>{},_0xb051ce=null,_0x111edd=()=>{},_0x1bf921=()=>{},_0x100cb3=null,_0x270a2a=0x0){const _0x436f6a=_0x31e176;if(!_0x53c37a||!_0x53c37a[_0x436f6a(0x231)]())return{'success':![],'error':'输入文本为空'};if(!settings)return{'success':![],'error':'核心未初始化'};try{const _0x1c1187=getCollectionIdInfo(),_0x4aa5a9=await _0x5487d3();if(_0x1c1187['oldId']&&_0x1c1187[_0x436f6a(0x1be)]===_0x4aa5a9&&_0x1c1187[_0x436f6a(0x1be)]!==_0x1c1187[_0x436f6a(0x1b5)]){const _0x189670=confirm(_0x436f6a(0x277));if(_0x189670)_0x111edd(_0x436f6a(0x1c4)+_0x1c1187[_0x436f6a(0x1be)],_0x436f6a(0x282)),await purgeStorage(_0x1c1187[_0x436f6a(0x1be)]),_0x111edd(_0x436f6a(0x1a9),_0x436f6a(0x19b));else return _0x111edd(_0x436f6a(0x21c),_0x436f6a(0x210)),toastr[_0x436f6a(0x210)](_0x436f6a(0x21b)),{'success':![],'error':_0x436f6a(0x24a)};}let _0x4c2098,_0x5d6016;const _0xa81d06=new Date()[_0x436f6a(0x1a6)](_0x436f6a(0x23b),{'hour12':![]}),_0x4a1ae5=getCharacterName()||'未知角色';switch(_0x3f24af){case _0x436f6a(0x263):const _0x40135=_0x2f450d[_0x436f6a(0x20f)]||{},_0x51d60f=_0x40135['start']??'?',_0x219f56=_0x40135['end']===0x0?'末':_0x40135[_0x436f6a(0x288)]??'?';_0x4c2098=_0x4a1ae5+':\x20'+_0x51d60f+'楼-'+_0x219f56+'楼';break;case _0x436f6a(0x19e):const _0x4bbb27=_0x2f450d[_0x436f6a(0x1b6)]||_0x436f6a(0x211),_0x3bddcb=_0x2f450d[_0x436f6a(0x1d6)]||_0x436f6a(0x25f);_0x4c2098=_0x4bbb27+':\x20'+_0x3bddcb;break;case'novel':_0x4c2098=_0x436f6a(0x1db)+(_0x2f450d['sourceName']||_0x436f6a(0x1a0));break;case _0x436f6a(0x23e):default:_0x4c2098=_0x436f6a(0x256)+_0xa81d06;break;}const _0x3c2910=Object[_0x436f6a(0x1f8)](getKnowledgeBases()),_0x17f043=_0x3c2910[_0x436f6a(0x22a)](_0x417e8b=>_0x417e8b['name']===_0x4c2098);if(_0x17f043)_0x5d6016=_0x17f043['id'],_0x111edd(_0x436f6a(0x1a5)+_0x4c2098+_0x436f6a(0x248),_0x436f6a(0x210));else{_0x111edd(_0x436f6a(0x26d)+_0x4c2098+_0x436f6a(0x205),'info');const _0x49e185=addKnowledgeBase(_0x4c2098);_0x5d6016=_0x49e185['id'];}const _0x3a079d=getCharacterStableId(),_0x4b0705=_0x3a079d+'_'+_0x5d6016;_0x111edd(_0x436f6a(0x1b2)+_0x4c2098+'\x20(集合ID:\x20'+_0x4b0705+')',_0x436f6a(0x19b)),_0x111edd(_0x436f6a(0x249)+_0x4b0705,_0x436f6a(0x210)),_0x2f22ab({'message':_0x436f6a(0x1ad),'processed':0x0,'total':0x1});const _0x4bc2db=splitIntoChunks(_0x53c37a,_0x3f24af,_0x2f450d),_0x283897=_0x4bc2db['length'];if(_0xb051ce?.[_0x436f6a(0x227)])throw new Error(_0x436f6a(0x280));_0x111edd(_0x436f6a(0x1eb)+_0x4c2098+_0x436f6a(0x233)+_0x283897+'\x20个块。',_0x436f6a(0x210));if(_0x283897===0x0)return{'success':!![],'count':0x0};const _0x4fee51=settings['retrieval'][_0x436f6a(0x223)]||0x5;let _0x5b8610=_0x270a2a;for(let _0x138a35=_0x270a2a;_0x138a35<_0x283897;_0x138a35+=_0x4fee51){if(_0xb051ce?.[_0x436f6a(0x227)])throw new Error(_0x436f6a(0x280));const _0x3567f1=_0x4bc2db['slice'](_0x138a35,_0x138a35+_0x4fee51);_0x2f22ab({'message':_0x436f6a(0x19d)+(_0x138a35+0x1)+'-'+(_0x138a35+_0x3567f1[_0x436f6a(0x25a)])+'\x20块','processed':_0x138a35,'total':_0x283897});const _0xbb6d3=_0x3567f1[_0x436f6a(0x206)](_0x5264a5=>_0x5264a5[_0x436f6a(0x278)]),_0x44ba61=await getEmbeddings(_0xbb6d3,_0xb051ce);if(_0xb051ce?.[_0x436f6a(0x227)])throw new Error(_0x436f6a(0x280));if(_0x3567f1[_0x436f6a(0x25a)]!==_0x44ba61[_0x436f6a(0x25a)])throw new Error(_0x436f6a(0x1f5));const _0x37f32c=_0x3567f1[_0x436f6a(0x206)]((_0x4b1ffc,_0x3baabd)=>({..._0x4b1ffc,'vector':_0x44ba61[_0x3baabd]}));await insertVectors(_0x37f32c,_0xb051ce,_0x4b0705),_0x5b8610+=_0x3567f1[_0x436f6a(0x25a)],_0x100cb3&&_0x16db44['saveProgress'](_0x100cb3,_0x5b8610,_0x283897),await _0x1bf921();}return _0x100cb3&&_0x16db44[_0x436f6a(0x1ba)](_0x100cb3),_0x111edd(_0x436f6a(0x220)+_0x5b8610+_0x436f6a(0x275),_0x436f6a(0x19b)),{'success':!![],'count':_0x5b8610};}catch(_0x7ec1f3){if(_0x7ec1f3[_0x436f6a(0x19a)]==='AbortError'){_0x111edd(_0x436f6a(0x1f0),'warn');throw _0x7ec1f3;}return console[_0x436f6a(0x1b8)](_0x436f6a(0x1ef),_0x7ec1f3),_0x111edd(_0x436f6a(0x27b)+_0x7ec1f3[_0x436f6a(0x246)],'error'),{'success':![],'error':_0x7ec1f3[_0x436f6a(0x246)]};}}function getSettings(){const _0x311b41=_0x31e176;if(!context||!context[_0x311b41(0x1a3)])return structuredClone(_0x1e160b);let _0x579736=context[_0x311b41(0x1a3)][MODULE_NAME];!_0x579736&&(_0x579736={},context[_0x311b41(0x1a3)][MODULE_NAME]=_0x579736);_0x579736[_0x311b41(0x1f6)]===undefined&&(_0x579736['condensationHistory']={});_0x579736[_0x311b41(0x27c)]===undefined&&(_0x579736[_0x311b41(0x27c)]={});for(const _0x3112fc in _0x1e160b){if(_0x579736[_0x3112fc]===undefined)_0x579736[_0x3112fc]=structuredClone(_0x1e160b[_0x3112fc]);else{if(typeof _0x1e160b[_0x3112fc]==='object'&&!Array[_0x311b41(0x262)](_0x1e160b[_0x3112fc])&&_0x1e160b[_0x3112fc]!==null)for(const _0x3bd544 in _0x1e160b[_0x3112fc]){_0x579736[_0x3112fc][_0x3bd544]===undefined&&(_0x579736[_0x3112fc][_0x3bd544]=_0x1e160b[_0x3112fc][_0x3bd544]);}}}return _0x579736;}function saveSettings(){const _0x1785d8=_0x31e176;if(context)context[_0x1785d8(0x214)]();}function resetSettings(){const _0x16760f=_0x31e176;context&&(context[_0x16760f(0x1a3)][MODULE_NAME]=structuredClone(_0x1e160b),saveSettings());}function showNotification(_0x5aeead,_0x54914d=_0x31e176(0x210)){toastr[_0x54914d](_0x5aeead);}function getTagForSource(_0x4a7560){const _0x3c8577=_0x31e176;switch(_0x4a7560){case _0x3c8577(0x263):return _0x3c8577(0x20b);case'lorebook':return _0x3c8577(0x204);case'manual':return _0x3c8577(0x1a8);case _0x3c8577(0x1c7):return'小说录入';default:return'资料';}}function _0x2fe0(_0x2d4b8e,_0xe9367d){const _0x4a15e0=_0x4a15();return _0x2fe0=function(_0x2fe0ba,_0x339845){_0x2fe0ba=_0x2fe0ba-0x19a;let _0x5a0ffe=_0x4a15e0[_0x2fe0ba];return _0x5a0ffe;},_0x2fe0(_0x2d4b8e,_0xe9367d);}function splitIntoChunks(_0x2c45ee,_0x2a1450,_0x238040={}){const _0x2b6354=_0x31e176;switch(_0x2a1450){case _0x2b6354(0x1c7):return _chunkForNovel(_0x2c45ee,_0x238040);case _0x2b6354(0x263):return _chunkForChatHistory(_0x2c45ee,_0x238040);case _0x2b6354(0x19e):return _chunkForLorebook(_0x2c45ee,_0x238040);case'manual':return _chunkForManual(_0x2c45ee,_0x238040);default:console['warn'](_0x2b6354(0x283)+_0x2a1450+_0x2b6354(0x25e));return _chunkForManual(_0x2c45ee,{..._0x238040,'sourceName':_0x238040[_0x2b6354(0x24b)]||_0x2b6354(0x23f)});}}function _0x4a15(){const _0x5e7b30=['start','getRequestHeaders','[翰林院-核心]\x20processCondensation\x20失败:','[来源:\x20世界书,\x20条目:\x20','reduce','[翰林院-Rerank]\x20开始外部API重排序...','[翰林院-Rerank]\x20开始元数据加权最终排序...','toString','metadata','2001351Qgmcwx','】已成功移动到','查询集合\x20',',\x20向量化录入时间:\x20','condensation','vector','[翰林院-核心]\x20将来源\x27','[翰林院-日志]\x20获取集合\x20','[翰林院-核心]\x20聊天记录凝识完成成功插入\x20','319yScoPj','[翰林院-核心]\x20ingestTextToHanlinyuan\x20失败:','[翰林院-核心]\x20文本录入任务被用户中止。','scope','\x20失败:\x20',',将清空集合:\x20','getContext','文本块和向量数量不匹配','condensationHistory','floor','values','[翰林院]\x20未能获取SillyTavern上下文初始化失败。','score','No\x20messages\x20to\x20process.','[翰林院]\x20检索或注入时发生错误:','initialized','hybrid_alpha','legacy','[翰林院-日志]\x20查询知识库\x20','9120654cbKUju','mes','owner','世界书','\x22\x20创建专属知识库...','map','无法确定要清空的目标宝库。','...)','[翰林院-核心]\x20聊天记录凝识失败:\x20','[翰林院-日志]\x20去重后剩余\x20','聊天记录','[翰林院-日志]\x20清空宝库API错误:','depth_role','[翰林院-计数]\x20在作用域\x20\x27','range','info','未分类世界书','部分]','9458VpBlnq','saveSettingsDebounced','163uMrkRn','_global','charCodeAt','maxResults','notify','stringify','操作已取消。','[翰林院-迁移]\x20用户取消了迁移操作。','toISOString','792695oMFAHV','知识库\x20\x22','[翰林院-核心]\x20成功插入\x20','1090170GqpMjs',',\x20第','batchSize','retrieval','6650764fiDlMr','[翰林院-日志]\x20发送到\x20/api/vector/purge\x20的请求体:','aborted','rerank','\x20添加新知识库:\x20','find','enabled','\x20-\x20楼层\x20#','[翰林院-日志]\x20/api/vector/purge\x20响应状态:\x20','injection','webllm','slice','trim','[翰林院-核心]\x20尝试删除一个不存在的知识库:\x20','\x27的文本分割成\x20','rerank_score','position','send_date','join','\x20(ID:\x20','440tNXjqi','未知角色','zh-CN','世界书条目','\x20失败:','manual','未知来源','top_n','[来源:\x20','min','\x20返回\x20','[翰林院-核心]\x20已为角色\x20','has','message','[翰林院-日志]\x20忆识存入API错误:','\x22将数据合并入库。','[翰林院-核心]\x20已锁定忆识宝库ID:\x20','用户取消了迁移操作','sourceName','配置更新','HANLINYUAN_RAG','advanced','[翰林院-迁移]\x20集合\x20','POST','quiet','\x0a</','忆识存入API错误\x20','/api/vector/insert','聊天记录:\x20','手动录入:\x20','旧版宝库\x20(Legacy)','replace','source','length','substring','在源作用域\x20\x27','log','\x27使用通用分块逻辑。','未知条目','30gihklV','[翰林院-核心]\x20已将\x20','isArray','chat_history','60641XxcCNE','知识库【','final_score','[翰林院-日志]\x20统计集合\x20','random','[翰林院-日志]\x20开始清空宝库...','is_user','删除知识库失败,未能清空后端数据。','all','[翰林院-核心]\x20准备为任务\x20\x22','depth','\x20条消息分解为\x20','template','queryMessageCount','local','\x20记录凝识范围:\x20',']\x20的消息已成功凝识。','\x20个向量条目。','[翰林院-日志]\x20无法确定要清空的目标集合ID。','检测到旧版数据。此操作将把旧数据迁移到新格式,过程不可逆,是否继续?','text','\x22\x20已删除。','[翰林院-日志]\x20正在查询知识库:\x20','[翰林院-核心]\x20文本录入失败:\x20','knowledgeBases','[翰林院-核心]\x20凝识任务已锁定知识库:\x20','聊天记录\x20#','hashes','AbortError','user','warn','[翰林院-分块]\x20未知的来源类型\x20\x27','warning','test','[翰林院-核心]\x20准备删除知识库\x20','split','end','filter','[翰林院-日志]\x20清空宝库API调用成功。','name','success','global','正在处理\x20','lorebook','unknown','未知小说','\x20补充所有者ID:\x20','findIndex','extensionSettings','index','[翰林院-核心]\x20检测到同名知识库\x20\x22','toLocaleString','status','手动录入','[翰林院-迁移]\x20旧宝库已清空。','翰林院忆识核心已启动\x20(V5.2-集成版),已注册到全局\x20hanlinyuanRagProcessor\x20对象。','\x20列表API时出现问题\x20(状态:\x20','[来源:\x20聊天记录,\x20楼层:\x20#','正在智能分块...','add','results','\x20时发生网络错误:','push','[翰林院-核心]\x20已创建并锁定知识库:\x20','\x20条结果。','[翰林院-日志]\x20所有知识库查询完毕共获得\x20','newId','bookName','resolve','error','forEach','clearJob','\x20条初步结果。','/api/vector/query','\x20(范围:\x20','oldId','\x20失败删除操作中止。','第1章','rearrangeChat','/api/vector/list','sort','[翰林院-迁移]\x20用户确认迁移正在处理旧宝库:\x20','data','[翰林院-日志]\x20开始获取所有知识库的向量总数...','novel','matchThreshold','json',',\x20第1卷,\x20第1章,\x20第','catch','flat','[翰林院-核心]\x20成功删除知识库\x20','第1卷','\x20的知识库。','[翰林院-日志]\x20开始多知识库向量查询...','[翰林院-日志]\x20知识库\x20','知识库名称不能为空','[翰林院-Rerank]\x20外部Rerank失败将仅使用内部加权。','hanlinyuanRagProcessor','original_index','entryName','chat','now','移动失败:没有当前角色,无法移入局部知识库。','insertVectors\x20必须接收一个有效的\x20collectionId\x20参数。','小说:\x20'];_0x4a15=function(){return _0x5e7b30;};return _0x4a15();}function _chunkForNovel(_0x16de32,_0x568c97){const _0x177acb=_0x31e176,{chunkSize:_0x4bb76e,overlap:_0x324415}=settings[_0x177acb(0x24e)],{sourceName:sourceName='小说'}=_0x568c97,_0x881074=[];if(!_0x16de32||_0x4bb76e<=0x0)return _0x881074;const _0x5c9724=/(第\s*[一二三四五六七八九十百千万零\d]+\s*卷)/gim,_0xf45fe2=/(第\s*[一二三四五六七八九十百千万零\d]+\s*[章回节部])|^(Chapter\s+\d+)/gim;let _0x4bb2be=0x0;const _0x40aac7=_0x16de32[_0x177acb(0x287)]('\x0a');let _0x5714a4='第1卷',_0x14f7c2=_0x177acb(0x1c0),_0x49fa87=[];function _0x36f7b9(){const _0x152369=_0x177acb;if(_0x49fa87[_0x152369(0x25a)]===0x0)return;const _0x54a604=_0x49fa87[_0x152369(0x237)]('\x0a');let _0x617a8d=0x0,_0x584ae7=0x1;while(_0x617a8d<_0x54a604[_0x152369(0x25a)]){const _0x34ebc1=Math[_0x152369(0x242)](_0x617a8d+_0x4bb76e,_0x54a604[_0x152369(0x25a)]),_0x400add=_0x54a604[_0x152369(0x25b)](_0x617a8d,_0x34ebc1);if(_0x400add['trim']()['length']>0x0){const _0xa4e53a={'source':_0x152369(0x1c7),'sourceName':sourceName,'timestamp':new Date()[_0x152369(0x21d)](),'globalIndex':_0x4bb2be++,'volume':_0x5714a4,'chapter':_0x14f7c2,'section':_0x584ae7},_0x4183e7=getTagForSource('novel'),_0x435d27=_0x152369(0x241)+sourceName+',\x20'+_0x5714a4+',\x20'+_0x14f7c2+_0x152369(0x222)+_0x584ae7+'节]',_0x26b4d2='<'+_0x4183e7+'>\x0a'+_0x435d27+'\x0a'+_0x400add+'\x0a</'+_0x4183e7+'>';_0x881074['push']({'text':_0x26b4d2,'metadata':_0xa4e53a}),_0x584ae7++;}_0x617a8d+=_0x4bb76e-_0x324415;if(_0x617a8d>=_0x54a604[_0x152369(0x25a)])break;}_0x49fa87=[];}for(const _0x32c8c0 of _0x40aac7){const _0xf1443f=_0x32c8c0['trim']();if(_0x5c9724[_0x177acb(0x285)](_0xf1443f))_0x36f7b9(),_0x5714a4=_0xf1443f,_0x14f7c2=_0x177acb(0x1c0);else _0xf45fe2['test'](_0xf1443f)?(_0x36f7b9(),_0x14f7c2=_0xf1443f):_0x49fa87[_0x177acb(0x1b1)](_0x32c8c0);}_0x36f7b9();if(_0x881074[_0x177acb(0x25a)]===0x0&&_0x16de32[_0x177acb(0x25a)]>0x0){let _0x3436a7=0x0,_0x3de6e6=0x1;while(_0x3436a7<_0x16de32['length']){const _0x16a8da=Math['min'](_0x3436a7+_0x4bb76e,_0x16de32[_0x177acb(0x25a)]),_0x1ae557=_0x16de32[_0x177acb(0x25b)](_0x3436a7,_0x16a8da),_0x35538d={'source':_0x177acb(0x1c7),'sourceName':sourceName,'timestamp':new Date()[_0x177acb(0x21d)](),'globalIndex':_0x881074['length'],'volume':_0x177acb(0x1ce),'chapter':_0x177acb(0x1c0),'section':_0x3de6e6},_0x12e40a=getTagForSource(_0x177acb(0x1c7)),_0x41929b='[来源:\x20'+sourceName+_0x177acb(0x1ca)+_0x3de6e6+'节]',_0x4a5f35='<'+_0x12e40a+'>\x0a'+_0x41929b+'\x0a'+_0x1ae557+_0x177acb(0x252)+_0x12e40a+'>';_0x881074[_0x177acb(0x1b1)]({'text':_0x4a5f35,'metadata':_0x35538d}),_0x3de6e6++,_0x3436a7+=_0x4bb76e-_0x324415;}}return _0x881074;}function _chunkForChatHistory(_0x4b3f92,_0x4fca6f){const _0x5c1feb=_0x31e176,{chunkSize:_0x5bd3eb,overlap:_0x3fdace}=settings[_0x5c1feb(0x24e)],{floor:_0x41b5bf,is_user:_0x2d709d,timestamp:_0x4bc29f}=_0x4fca6f,_0x537118=[];if(!_0x4b3f92||_0x5bd3eb<=0x0)return _0x537118;let _0x3ec202=0x1,_0x48bf96=0x0;while(_0x48bf96<_0x4b3f92[_0x5c1feb(0x25a)]){const _0x11b0a2=Math[_0x5c1feb(0x242)](_0x48bf96+_0x5bd3eb,_0x4b3f92[_0x5c1feb(0x25a)]),_0x1ded2f=_0x4b3f92[_0x5c1feb(0x25b)](_0x48bf96,_0x11b0a2),_0x56ed39=_0x5c1feb(0x1ac)+_0x41b5bf+_0x5c1feb(0x222)+_0x3ec202+_0x5c1feb(0x212),_0x493443=getTagForSource(_0x5c1feb(0x263)),_0x305862='<'+_0x493443+'>\x0a'+_0x56ed39+'\x0a'+_0x1ded2f+'\x0a</'+_0x493443+'>';_0x537118[_0x5c1feb(0x1b1)]({'text':_0x305862,'metadata':{'source':_0x5c1feb(0x263),'sourceName':_0x5c1feb(0x27e)+_0x41b5bf,'floor':_0x41b5bf,'part':_0x3ec202,'is_user':_0x2d709d,'timestamp':_0x4bc29f}}),_0x3ec202++,_0x48bf96+=_0x5bd3eb-_0x3fdace;if(_0x48bf96>=_0x4b3f92[_0x5c1feb(0x25a)])break;}return _0x537118;}function _chunkForLorebook(_0x254e82,_0x5966f9){const _0x4b698b=_0x31e176,{chunkSize:_0x5ecee8,overlap:_0x14a769}=settings[_0x4b698b(0x24e)],{sourceName:sourceName=_0x4b698b(0x23c)}=_0x5966f9,_0x2a226d=[];if(!_0x254e82||_0x5ecee8<=0x0)return _0x2a226d;let _0x5bab7b=0x1,_0x38697a=0x0;while(_0x38697a<_0x254e82[_0x4b698b(0x25a)]){const _0x2e14d4=Math[_0x4b698b(0x242)](_0x38697a+_0x5ecee8,_0x254e82['length']),_0x583683=_0x254e82[_0x4b698b(0x25b)](_0x38697a,_0x2e14d4),_0x4a9731=_0x4b698b(0x1df)+sourceName+_0x4b698b(0x222)+_0x5bab7b+_0x4b698b(0x212),_0x1297f0=getTagForSource(_0x4b698b(0x19e)),_0x4feb22='<'+_0x1297f0+'>\x0a'+_0x4a9731+'\x0a'+_0x583683+_0x4b698b(0x252)+_0x1297f0+'>';_0x2a226d[_0x4b698b(0x1b1)]({'text':_0x4feb22,'metadata':{'source':'lorebook','sourceName':sourceName,'part':_0x5bab7b,'timestamp':new Date()['toISOString']()}}),_0x5bab7b++,_0x38697a+=_0x5ecee8-_0x14a769;if(_0x38697a>=_0x254e82[_0x4b698b(0x25a)])break;}return _0x2a226d;}function _chunkForManual(_0x2a003c,_0x14c22f){const _0x4887d1=_0x31e176,{chunkSize:_0x3ed84c,overlap:_0x144709}=settings[_0x4887d1(0x24e)],{sourceName:sourceName='手动录入'}=_0x14c22f,_0x3fdd6c=[];if(!_0x2a003c||_0x3ed84c<=0x0)return _0x3fdd6c;const _0x51efe6=new Date(),_0x3caf6d=_0x51efe6['toLocaleString'](_0x4887d1(0x23b));let _0x562272=0x1,_0x1a9b4f=0x0;while(_0x1a9b4f<_0x2a003c[_0x4887d1(0x25a)]){const _0x422921=Math[_0x4887d1(0x242)](_0x1a9b4f+_0x3ed84c,_0x2a003c[_0x4887d1(0x25a)]),_0x5540c0=_0x2a003c[_0x4887d1(0x25b)](_0x1a9b4f,_0x422921),_0x37c729=_0x4887d1(0x241)+sourceName+_0x4887d1(0x1e8)+_0x3caf6d+_0x4887d1(0x222)+_0x562272+_0x4887d1(0x212),_0x2fd7eb=getTagForSource(_0x4887d1(0x23e)),_0x181c1f='<'+_0x2fd7eb+'>\x0a'+_0x37c729+'\x0a'+_0x5540c0+_0x4887d1(0x252)+_0x2fd7eb+'>';_0x3fdd6c[_0x4887d1(0x1b1)]({'text':_0x181c1f,'metadata':{'source':'manual','sourceName':sourceName,'part':_0x562272,'timestamp':_0x51efe6[_0x4887d1(0x21d)]()}}),_0x562272++,_0x1a9b4f+=_0x3ed84c-_0x144709;if(_0x1a9b4f>=_0x2a003c['length'])break;}return _0x3fdd6c;}import{getCollectionId as _0x5487d3,getCharacterName}from'./utils/context-utils.js';async function getCollectionId(){return lockedCollectionId||await _0x5487d3();}async function toggleSessionLock(){return lockedCollectionId?(lockedCollectionId=null,![]):(lockedCollectionId=await _0x5487d3(),!![]);}function isSessionLocked(){return lockedCollectionId!==null;}function getLockedSessionInfo(){const _0x2c8e99=_0x31e176;if(!lockedCollectionId)return null;return{'id':lockedCollectionId,'name':'(已锁定:\x20'+lockedCollectionId[_0x2c8e99(0x25b)](0x0,0x8)+_0x2c8e99(0x208)};}function getLocalKnowledgeBases(){const _0x1e249b=_0x31e176,_0xf76499=getCharacterStableId();return!settings[_0x1e249b(0x27c)][_0xf76499]&&(settings[_0x1e249b(0x27c)][_0xf76499]={}),settings[_0x1e249b(0x27c)][_0xf76499];}function getGlobalKnowledgeBases(){const _0x830841=_0x31e176;return!settings[_0x830841(0x27c)][GLOBAL_SCOPE_ID]&&(settings[_0x830841(0x27c)][GLOBAL_SCOPE_ID]={}),settings[_0x830841(0x27c)][GLOBAL_SCOPE_ID];}function addKnowledgeBase(_0x23a794){const _0x14b346=_0x31e176;if(!_0x23a794||!_0x23a794['trim']())throw new Error(_0x14b346(0x1d2));const _0x420a12=getCharacterStableId(),_0x3243e6=getLocalKnowledgeBases(),_0x19454a='task_'+Date[_0x14b346(0x1d8)]()+'_'+Math[_0x14b346(0x268)]()[_0x14b346(0x1e3)](0x24)['substring'](0x2,0x9),_0x16415f={'id':_0x19454a,'name':_0x23a794[_0x14b346(0x231)](),'enabled':!![],'createdAt':new Date()['toISOString'](),'owner':_0x420a12};return _0x3243e6[_0x19454a]=_0x16415f,saveSettings(),console[_0x14b346(0x25d)](_0x14b346(0x244)+_0x420a12+_0x14b346(0x229)+_0x23a794+'\x20(ID:\x20'+_0x19454a+')'),_0x16415f;}async function removeKnowledgeBase(_0x46b484,_0x343d48){const _0x2c9a4d=_0x31e176,_0x23d174=getCharacterStableId(),_0x1c3cb8=_0x343d48==='global'?getGlobalKnowledgeBases():getLocalKnowledgeBases(),_0x17319a=_0x1c3cb8[_0x46b484],_0x1c48d1=_0x17319a?.[_0x2c9a4d(0x19a)]||_0x46b484;if(!_0x17319a){console[_0x2c9a4d(0x282)](_0x2c9a4d(0x232)+_0x46b484+_0x2c9a4d(0x1bd)+_0x343d48+')');return;}const _0x111357=_0x343d48===_0x2c9a4d(0x19c)?_0x17319a['owner']||GLOBAL_SCOPE_ID:_0x23d174,_0x17c6de=_0x111357+'_'+_0x46b484;console[_0x2c9a4d(0x25d)](_0x2c9a4d(0x286)+_0x46b484+_0x2c9a4d(0x1f3)+_0x17c6de);const _0x14b141=await purgeStorage(_0x17c6de);_0x14b141?(delete _0x1c3cb8[_0x46b484],saveSettings(),console[_0x2c9a4d(0x25d)](_0x2c9a4d(0x1cd)+_0x46b484+'\x20及其向量数据。'),toastr[_0x2c9a4d(0x19b)](_0x2c9a4d(0x21f)+_0x1c48d1+_0x2c9a4d(0x279))):(console[_0x2c9a4d(0x1b8)]('[翰林院-核心]\x20清空向量集合\x20'+_0x17c6de+_0x2c9a4d(0x1bf)),toastr[_0x2c9a4d(0x1b8)](_0x2c9a4d(0x26b)));}function toggleKnowledgeBase(_0xf6cdbb,_0x5b02b3){const _0x21d828=_0x31e176,_0x34b09f=_0x5b02b3===_0x21d828(0x19c)?getGlobalKnowledgeBases():getLocalKnowledgeBases();_0x34b09f[_0xf6cdbb]&&(_0x34b09f[_0xf6cdbb][_0x21d828(0x22b)]=!_0x34b09f[_0xf6cdbb][_0x21d828(0x22b)],saveSettings(),console[_0x21d828(0x25d)]('[翰林院-核心]\x20知识库\x20'+_0xf6cdbb+_0x21d828(0x1bd)+_0x5b02b3+')\x20的状态已切换为:\x20'+(_0x34b09f[_0xf6cdbb]['enabled']?'启用':'禁用')));}function generateHash(_0x57300c){const _0x185bcd=_0x31e176;let _0x3f5968=0x0;for(let _0x2dbcfb=0x0;_0x2dbcfb<_0x57300c[_0x185bcd(0x25a)];_0x2dbcfb++){const _0x5ae25b=_0x57300c[_0x185bcd(0x217)](_0x2dbcfb);_0x3f5968=(_0x3f5968<<0x5)-_0x3f5968+_0x5ae25b,_0x3f5968=_0x3f5968&_0x3f5968;}return Math['abs'](_0x3f5968)[_0x185bcd(0x1e3)](0x24);}async function queryVectors(_0x44a421){const _0xd8fc8d=_0x31e176;console['log'](_0xd8fc8d(0x1d0));const _0x489ffa=getCharacterStableId(),_0x502311=getLocalKnowledgeBases(),_0x3700e9=getGlobalKnowledgeBases(),_0x2685b6=Object[_0xd8fc8d(0x1f8)](_0x502311)[_0xd8fc8d(0x289)](_0x4b889f=>_0x4b889f[_0xd8fc8d(0x22b)]),_0x3333aa=Object[_0xd8fc8d(0x1f8)](_0x3700e9)[_0xd8fc8d(0x289)](_0x57700e=>_0x57700e[_0xd8fc8d(0x22b)]),_0x5b121f=[..._0x2685b6[_0xd8fc8d(0x206)](_0x48d3fb=>({..._0x48d3fb,'scope':_0xd8fc8d(0x272)})),..._0x3333aa[_0xd8fc8d(0x206)](_0x2e0612=>({..._0x2e0612,'scope':_0xd8fc8d(0x19c)}))];if(_0x5b121f['length']===0x0){console[_0xd8fc8d(0x25d)]('[翰林院-日志]\x20没有启用的新知识库尝试查询旧版单体宝库...');const _0x2d4996=await _0x5487d3();if(!_0x2d4996)return[];_0x5b121f['push']({'id':null,'name':_0xd8fc8d(0x257),'scope':_0xd8fc8d(0x1ff)});}const _0x563741=(await getEmbeddings([_0x44a421]))[0x0];let _0x40b2d1=[];const _0x29a9b8=_0x5b121f[_0xd8fc8d(0x206)](_0x265e2b=>{const _0x4ff6ef=_0xd8fc8d;let _0x46a7b3;if(_0x265e2b[_0x4ff6ef(0x1f1)]==='legacy')_0x46a7b3=_0x5487d3();else{const _0x46e65c=_0x265e2b[_0x4ff6ef(0x1f1)]===_0x4ff6ef(0x19c)?_0x265e2b[_0x4ff6ef(0x203)]||GLOBAL_SCOPE_ID:_0x489ffa;_0x46a7b3=Promise[_0x4ff6ef(0x1b7)](_0x46e65c+'_'+_0x265e2b['id']);}return _0x46a7b3['then'](_0x233eb7=>{const _0x3d91c5=_0x4ff6ef;if(!_0x233eb7)return[];console[_0x3d91c5(0x25d)](_0x3d91c5(0x27a)+_0x265e2b[_0x3d91c5(0x19a)]+_0x3d91c5(0x238)+_0x233eb7+')');const _0x5563f7={'collectionId':_0x233eb7,'searchText':_0x44a421,'topK':settings[_0x3d91c5(0x24e)][_0x3d91c5(0x218)],'threshold':settings[_0x3d91c5(0x24e)][_0x3d91c5(0x1c8)],'source':_0x3d91c5(0x22f),'embeddings':{[_0x44a421]:_0x563741}};return fetch(_0x3d91c5(0x1bc),{'method':_0x3d91c5(0x250),'headers':context[_0x3d91c5(0x1dd)](),'body':JSON['stringify'](_0x5563f7)})['then'](async _0x247291=>{const _0x315bec=_0x3d91c5;if(!_0x247291['ok']){const _0x3c5846=await _0x247291[_0x315bec(0x278)]();return console[_0x315bec(0x1b8)](_0x315bec(0x200)+_0x233eb7+_0x315bec(0x23d),_0x3c5846),[];}const _0x3333d1=await _0x247291[_0x315bec(0x1c9)](),_0x333ba6=_0x3333d1['metadata']||_0x3333d1[_0x315bec(0x1af)]||_0x3333d1[_0x315bec(0x1c5)]||[];return console[_0x315bec(0x25d)](_0x315bec(0x1d1)+_0x265e2b[_0x315bec(0x19a)]+_0x315bec(0x243)+_0x333ba6['length']+'\x20条结果。'),_0x333ba6;})[_0x3d91c5(0x1cb)](_0x298349=>{const _0xf177b6=_0x3d91c5;return console[_0xf177b6(0x1b8)](_0xf177b6(0x200)+_0x233eb7+_0xf177b6(0x1b0),_0x298349),[];});});}),_0x2b9083=await Promise[_0xd8fc8d(0x26c)](_0x29a9b8);_0x40b2d1=_0x2b9083[_0xd8fc8d(0x1cc)](),console[_0xd8fc8d(0x25d)](_0xd8fc8d(0x1b4)+_0x40b2d1[_0xd8fc8d(0x25a)]+_0xd8fc8d(0x1bb));const _0x1ffa33=[],_0x3d5483=new Set();for(const _0x2cc625 of _0x40b2d1){_0x2cc625&&_0x2cc625[_0xd8fc8d(0x278)]&&!_0x3d5483[_0xd8fc8d(0x245)](_0x2cc625[_0xd8fc8d(0x278)])&&(_0x3d5483[_0xd8fc8d(0x1ae)](_0x2cc625[_0xd8fc8d(0x278)]),_0x1ffa33[_0xd8fc8d(0x1b1)](_0x2cc625));}return console[_0xd8fc8d(0x25d)](_0xd8fc8d(0x20a)+_0x1ffa33[_0xd8fc8d(0x25a)]+_0xd8fc8d(0x1b3)),_0x1ffa33[_0xd8fc8d(0x1c3)]((_0x3552f8,_0x292919)=>(_0x292919['score']||0x0)-(_0x3552f8[_0xd8fc8d(0x1fa)]||0x0)),_0x1ffa33;}async function insertVectors(_0x261d96,_0x20e709=null,_0x21ded5){const _0x2be9ae=_0x31e176;if(!_0x21ded5)throw new Error(_0x2be9ae(0x1da));if(_0x261d96[_0x2be9ae(0x25a)]===0x0)return{'success':!![],'count':0x0};const _0x1e8cca=_0x261d96[_0x2be9ae(0x206)]((_0x578c8c,_0x294afb)=>({'hash':generateHash(_0x578c8c[_0x2be9ae(0x278)]+Date[_0x2be9ae(0x1d8)]()+_0x294afb),'text':_0x578c8c[_0x2be9ae(0x278)],'metadata':_0x578c8c['metadata']||{'source':_0x2be9ae(0x19f),'timestamp':new Date()['toISOString']()}})),_0x53deff=_0x1e8cca[_0x2be9ae(0x1e0)]((_0x59306c,_0x6c102,_0x39d15b)=>{const _0x4cf42c=_0x2be9ae;return _0x59306c[_0x6c102['text']]=_0x261d96[_0x39d15b][_0x4cf42c(0x1ea)],_0x59306c;},{}),_0x17eeef={'collectionId':_0x21ded5,'items':_0x1e8cca,'source':_0x2be9ae(0x22f),'embeddings':_0x53deff},_0x398ad5=await fetch(_0x2be9ae(0x254),{'method':_0x2be9ae(0x250),'headers':context[_0x2be9ae(0x1dd)](),'body':JSON[_0x2be9ae(0x21a)](_0x17eeef),'signal':_0x20e709});if(!_0x398ad5['ok']){const _0x38ea59=await _0x398ad5[_0x2be9ae(0x278)]();console[_0x2be9ae(0x1b8)](_0x2be9ae(0x247),_0x38ea59);throw new Error(_0x2be9ae(0x253)+_0x398ad5[_0x2be9ae(0x1a7)]+':\x20'+_0x38ea59);}return{'success':!![],'count':_0x1e8cca['length']};}async function getVectorCount(_0x518acb=null,_0x8c88f1=_0x31e176(0x272)){const _0x3878d3=_0x31e176,_0x3aeaa9=getCharacterStableId();if(_0x518acb){const _0x59c6be=_0x8c88f1===_0x3878d3(0x19c)?getGlobalKnowledgeBases():getLocalKnowledgeBases(),_0x5a6843=_0x59c6be[_0x518acb];if(!_0x5a6843)return console['warn'](_0x3878d3(0x20e)+_0x8c88f1+'\x27\x20中未找到ID为\x20'+_0x518acb+'\x20的知识库。'),0x0;const _0x5eac8b=_0x8c88f1===_0x3878d3(0x19c)?_0x5a6843['owner']||GLOBAL_SCOPE_ID:_0x3aeaa9,_0x16d8b9=_0x5eac8b+'_'+_0x518acb;return await countVectorsInCollection(_0x16d8b9);}else{console[_0x3878d3(0x25d)](_0x3878d3(0x1c6));const _0x3fd94a=Object[_0x3878d3(0x1f8)](getLocalKnowledgeBases()),_0x3aa585=Object[_0x3878d3(0x1f8)](getGlobalKnowledgeBases()),_0x478720=[];_0x3fd94a[_0x3878d3(0x1b9)](_0x5ef002=>{const _0x1097f4=_0x3878d3,_0xbf1ea1=_0x3aeaa9+'_'+_0x5ef002['id'];_0x478720[_0x1097f4(0x1b1)](countVectorsInCollection(_0xbf1ea1));}),_0x3aa585['forEach'](_0x51ddc1=>{const _0x5188c5=_0x3878d3,_0x3e597a=_0x51ddc1['owner']||GLOBAL_SCOPE_ID,_0x378701=_0x3e597a+'_'+_0x51ddc1['id'];_0x478720[_0x5188c5(0x1b1)](countVectorsInCollection(_0x378701));});const _0x2da2df=await _0x5487d3();_0x478720[_0x3878d3(0x1b1)](countVectorsInCollection(_0x2da2df));const _0x4c4f9=await Promise['all'](_0x478720),_0xae2838=_0x4c4f9[_0x3878d3(0x1e0)]((_0x1a3106,_0x3b9554)=>_0x1a3106+_0x3b9554,0x0);return console[_0x3878d3(0x25d)]('[翰林院-日志]\x20所有知识库统计完成总向量数:\x20'+_0xae2838),_0xae2838;}}async function countVectorsInCollection(_0x588036){const _0x3b3677=_0x31e176;if(!_0x588036)return 0x0;console['log']('[翰林院-日志]\x20统计目标集合ID:\x20'+_0x588036);const _0x2c17c0={'collectionId':_0x588036,'source':'webllm','embeddings':{}};try{const _0x2c83f2=await fetch(_0x3b3677(0x1c2),{'method':'POST','headers':context[_0x3b3677(0x1dd)](),'body':JSON[_0x3b3677(0x21a)](_0x2c17c0)});if(!_0x2c83f2['ok']){if(_0x2c83f2[_0x3b3677(0x1a7)]===0x194)console[_0x3b3677(0x25d)]('[翰林院-日志]\x20集合\x20'+_0x588036+'\x20不存在计为\x200。');else{const _0x4b1ed9=await _0x2c83f2['text']();console['warn'](_0x3b3677(0x1ec)+_0x588036+_0x3b3677(0x1ab)+_0x2c83f2[_0x3b3677(0x1a7)]+'):',_0x4b1ed9);}return 0x0;}const _0x35b403=await _0x2c83f2[_0x3b3677(0x1c9)]();let _0x5dfc68=0x0;if(Array[_0x3b3677(0x262)](_0x35b403))_0x5dfc68=_0x35b403['length'];else _0x35b403&&_0x35b403[_0x3b3677(0x27f)]&&(_0x5dfc68=_0x35b403[_0x3b3677(0x27f)]['length']);return _0x5dfc68;}catch(_0x4d29c7){return console[_0x3b3677(0x1b8)](_0x3b3677(0x267)+_0x588036+_0x3b3677(0x1b0),_0x4d29c7),0x0;}}async function purgeStorage(_0x5d76a5=null){const _0x590e13=_0x31e176;console[_0x590e13(0x25d)](_0x590e13(0x269));const _0xeb0f63=_0x5d76a5||await getCollectionId();if(!_0xeb0f63)return console[_0x590e13(0x1b8)](_0x590e13(0x276)),toastr['error'](_0x590e13(0x207)),![];console[_0x590e13(0x25d)]('[翰林院-日志]\x20清空目标集合ID:\x20'+_0xeb0f63);const _0x41d4f6={'collectionId':_0xeb0f63};console['log'](_0x590e13(0x226),JSON['stringify'](_0x41d4f6,null,0x2));const _0x459aed=await fetch('/api/vector/purge',{'method':_0x590e13(0x250),'headers':context[_0x590e13(0x1dd)](),'body':JSON[_0x590e13(0x21a)](_0x41d4f6)});console[_0x590e13(0x25d)](_0x590e13(0x22d)+_0x459aed[_0x590e13(0x1a7)]);if(!_0x459aed['ok']){const _0x28aa52=await _0x459aed[_0x590e13(0x278)]();console[_0x590e13(0x1b8)](_0x590e13(0x20c),_0x28aa52);}else console[_0x590e13(0x25d)](_0x590e13(0x28a));return _0x459aed['ok'];}function getMessagesForCondensation(_0x592102=null){const _0xa30585=_0x31e176;if(!settings[_0xa30585(0x1e9)][_0xa30585(0x22b)])return showNotification('凝识之权未开启',_0xa30585(0x284)),[];const {layerStart:_0x524b84,layerEnd:_0x5a7b81}=settings[_0xa30585(0x1e9)],_0x15c0b5=_0x592102||settings[_0xa30585(0x1e9)]['messageTypes'],_0x4e13dd=context[_0xa30585(0x1d7)][_0xa30585(0x25a)],_0x1c5d4e=Math['max'](0x0,_0x524b84-0x1),_0x3665ea=_0x5a7b81===0x0||_0x5a7b81>_0x4e13dd?_0x4e13dd:Math[_0xa30585(0x242)](_0x4e13dd,_0x5a7b81),_0x4b36ff=context['chat'][_0xa30585(0x230)](_0x1c5d4e,_0x3665ea);return _0x4b36ff[_0xa30585(0x289)](_0x2f3a90=>{const _0xf038ce=_0xa30585,_0x340d79=_0x2f3a90[_0xf038ce(0x26a)]===!![],_0xd56239=_0x2f3a90['is_user']===![];if(!_0x2f3a90[_0xf038ce(0x202)]||!_0x2f3a90[_0xf038ce(0x202)][_0xf038ce(0x231)]())return![];return _0x15c0b5[_0xf038ce(0x281)]&&_0x340d79||_0x15c0b5['ai']&&_0xd56239;});}async function processCondensation(_0xb1ebc0,_0x36c5f9=()=>{},_0x2d7dc2=null){const _0x3a07cf=_0x31e176;if(!_0xb1ebc0||_0xb1ebc0['length']===0x0)return{'success':![],'error':_0x3a07cf(0x1fb)};try{let _0x27b88a,_0x1602d2;const _0x4420aa=getCharacterName()||_0x3a07cf(0x23a);if(_0x2d7dc2){const _0x5a27ff=_0x2d7dc2[_0x3a07cf(0x1dc)]??'?',_0x32f438=_0x2d7dc2[_0x3a07cf(0x288)]===0x0?'末':_0x2d7dc2[_0x3a07cf(0x288)]??'?';_0x27b88a=_0x4420aa+':\x20'+_0x5a27ff+'楼-'+_0x32f438+'楼';}else{const _0x9e7529=new Date()[_0x3a07cf(0x1a6)](_0x3a07cf(0x23b),{'hour12':![]});_0x27b88a=_0x3a07cf(0x255)+_0x9e7529;}const _0x391861=Object[_0x3a07cf(0x1f8)](getLocalKnowledgeBases()),_0x5eba16=_0x391861[_0x3a07cf(0x22a)](_0x519f8d=>_0x519f8d[_0x3a07cf(0x19a)]===_0x27b88a);if(_0x5eba16)_0x1602d2=_0x5eba16['id'],_0x36c5f9('[翰林院-核心]\x20检测到同名知识库\x20\x22'+_0x27b88a+'\x22将数据合并入库。',_0x3a07cf(0x210));else{_0x36c5f9(_0x3a07cf(0x26d)+_0x27b88a+_0x3a07cf(0x205),_0x3a07cf(0x210));const _0x4c498e=addKnowledgeBase(_0x27b88a);_0x1602d2=_0x4c498e['id'];}const _0x3af387=getCharacterStableId(),_0x55d492=_0x3af387+'_'+_0x1602d2;_0x36c5f9(_0x3a07cf(0x27d)+_0x27b88a+'\x20(集合ID:\x20'+_0x55d492+')',_0x3a07cf(0x19b));const _0xbe97ab=[],_0x556c8c=context[_0x3a07cf(0x1d7)];for(const _0x12d19e of _0xb1ebc0){const _0x59da76=(_0x12d19e[_0x3a07cf(0x202)]||'')[_0x3a07cf(0x258)](/<[^>]*>/g,'')[_0x3a07cf(0x231)]();if(_0x59da76['length']===0x0)continue;let _0x321168;if(_0x12d19e[_0x3a07cf(0x1f7)]!==undefined&&_0x12d19e[_0x3a07cf(0x1f7)]!==null)_0x321168=_0x12d19e[_0x3a07cf(0x1f7)];else{const _0x30b8ef=_0x556c8c['findIndex'](_0x1b9b84=>_0x1b9b84===_0x12d19e);_0x321168=_0x30b8ef!==-0x1?_0x30b8ef+0x1:-0x1;}const _0x2e5c3d=new Date(_0x12d19e[_0x3a07cf(0x236)]),_0x148c0c=isNaN(_0x2e5c3d['getTime']())?new Date()['toISOString']():_0x2e5c3d[_0x3a07cf(0x21d)](),_0x355bcd=splitIntoChunks(_0x59da76,_0x3a07cf(0x263),{'floor':_0x321168,'is_user':_0x12d19e[_0x3a07cf(0x26a)],'timestamp':_0x148c0c});_0xbe97ab[_0x3a07cf(0x1b1)](..._0x355bcd);}if(_0xbe97ab[_0x3a07cf(0x25a)]===0x0)return{'success':!![],'count':0x0};_0x36c5f9(_0x3a07cf(0x261)+_0xb1ebc0[_0x3a07cf(0x25a)]+_0x3a07cf(0x26f)+_0xbe97ab['length']+'\x20个知识块准备入库。','info');const _0x340ac4=settings[_0x3a07cf(0x224)][_0x3a07cf(0x223)]||0x5;let _0x16653c=0x0;for(let _0x521bc9=0x0;_0x521bc9<_0xbe97ab['length'];_0x521bc9+=_0x340ac4){const _0x1d4986=_0xbe97ab[_0x3a07cf(0x230)](_0x521bc9,_0x521bc9+_0x340ac4),_0x5e777f=_0x1d4986['map'](_0x3e2a75=>_0x3e2a75[_0x3a07cf(0x278)]),_0x649a0d=await getEmbeddings(_0x5e777f);if(_0x1d4986[_0x3a07cf(0x25a)]!==_0x649a0d[_0x3a07cf(0x25a)])throw new Error(_0x3a07cf(0x1f5));const _0x495b3c=_0x1d4986[_0x3a07cf(0x206)]((_0x2fb803,_0x4118fd)=>({..._0x2fb803,'vector':_0x649a0d[_0x4118fd]}));await insertVectors(_0x495b3c,null,_0x55d492),_0x16653c+=_0x1d4986[_0x3a07cf(0x25a)];}if(_0x2d7dc2){const _0x3fc38e=_0x2d7dc2[_0x3a07cf(0x288)]===0x0?context[_0x3a07cf(0x1d7)][_0x3a07cf(0x25a)]:_0x2d7dc2[_0x3a07cf(0x288)],_0x3558e0=getCharacterStableId();!settings[_0x3a07cf(0x1f6)][_0x3558e0]&&(settings[_0x3a07cf(0x1f6)][_0x3558e0]={}),settings[_0x3a07cf(0x1f6)][_0x3558e0][_0x55d492]={'start':_0x2d7dc2['start'],'end':_0x3fc38e,'timestamp':new Date()[_0x3a07cf(0x21d)]()},saveSettings(),_0x36c5f9('[翰林院-核心]\x20已为宝库\x20'+_0x55d492+_0x3a07cf(0x273)+_0x2d7dc2[_0x3a07cf(0x1dc)]+'-'+_0x3fc38e,'info');}_0x36c5f9(_0x3a07cf(0x1ed)+_0x16653c+'\x20个条目。','success');const _0x537b1d=_0xb1ebc0[_0x3a07cf(0x206)](_0x305796=>{const _0x43c41c=_0x3a07cf,_0x48b217=_0x556c8c[_0x43c41c(0x1a2)](_0x11341a=>_0x11341a===_0x305796),_0x50c8ee=_0x48b217!==-0x1?_0x48b217+0x1:-0x1,_0xef9b48=_0x305796[_0x43c41c(0x26a)]?'用户':getCharacterName()||'AI';return'['+_0xef9b48+_0x43c41c(0x22c)+_0x50c8ee+_0x43c41c(0x274);});return{'success':!![],'count':_0x16653c,'messages':_0x537b1d};}catch(_0x5e6874){return console[_0x3a07cf(0x1b8)](_0x3a07cf(0x1de),_0x5e6874),_0x36c5f9(_0x3a07cf(0x209)+_0x5e6874[_0x3a07cf(0x246)],'error'),{'success':![],'error':_0x5e6874['message']};}}async function rerankResults(_0x2c93eb,_0x5b0d55,_0x4409e6){const _0x2aa064=_0x31e176;let _0x342bd2=_0x2c93eb;if(_0x4409e6[_0x2aa064(0x228)][_0x2aa064(0x22b)]&&_0x2c93eb[_0x2aa064(0x25a)]>0x0){console['log'](_0x2aa064(0x1e1));try{const _0x5c64a9=_0x2c93eb[_0x2aa064(0x206)](_0x5c4952=>_0x5c4952[_0x2aa064(0x278)]),_0x1cb7f3=await executeRerank(_0x5b0d55,_0x5c64a9,_0x4409e6['rerank']),_0x5867cf=_0x2c93eb[_0x2aa064(0x206)]((_0x54f02d,_0x5f2c06)=>({..._0x54f02d,'original_index':_0x5f2c06}));_0x342bd2=_0x5867cf[_0x2aa064(0x206)](_0x5b7777=>{const _0x1818b4=_0x2aa064,_0xdb25a1=_0x1cb7f3['results']['find'](_0x1c8a47=>_0x1c8a47[_0x1818b4(0x1a4)]===_0x5b7777[_0x1818b4(0x1d5)]),_0x707efd=_0xdb25a1?_0xdb25a1['relevance_score']:0x0;return{..._0x5b7777,'rerank_score':_0x707efd};});if(_0x4409e6['rerank'][_0x2aa064(0x219)])showNotification('外部Rerank完成','success');}catch(_0x1335d1){console[_0x2aa064(0x1b8)](_0x2aa064(0x1d3),_0x1335d1);if(_0x4409e6[_0x2aa064(0x228)][_0x2aa064(0x219)])showNotification('Rerank失败:\x20'+_0x1335d1[_0x2aa064(0x246)],'error');_0x342bd2[_0x2aa064(0x1b9)](_0x169398=>_0x169398['rerank_score']=0x0);}}else _0x342bd2[_0x2aa064(0x1b9)](_0x32cb00=>_0x32cb00['rerank_score']=0x0);console['log'](_0x2aa064(0x1e2));const _0x4f6c91=context[_0x2aa064(0x1d7)][_0x2aa064(0x25a)],_0x47d3ab=_0x4409e6[_0x2aa064(0x228)][_0x2aa064(0x1fe)],_0x135a13=_0x342bd2[_0x2aa064(0x206)](_0x2de94d=>{const _0x3920ca=_0x2aa064;let _0x28cf6b=0x1;const _0x6c7de=_0x2de94d[_0x3920ca(0x1e4)]||{};switch(_0x6c7de[_0x3920ca(0x259)]){case _0x3920ca(0x19e):_0x28cf6b*=1.2;break;case'manual':_0x28cf6b*=1.1;break;case _0x3920ca(0x263):if(_0x6c7de[_0x3920ca(0x1f7)]&&_0x4f6c91>0x0){const _0x3cf9cb=_0x6c7de[_0x3920ca(0x1f7)]/_0x4f6c91;_0x28cf6b*=0x1+_0x3cf9cb;}break;}const _0x1530f0=_0x2de94d[_0x3920ca(0x234)]*_0x47d3ab+(_0x2de94d[_0x3920ca(0x1fa)]||0x0)*(0x1-_0x47d3ab),_0x353470=_0x1530f0*_0x28cf6b;return{..._0x2de94d,'final_score':_0x353470};});return _0x135a13['sort']((_0xce0487,_0xe9d35b)=>(_0xe9d35b['final_score']||0x0)-(_0xce0487[_0x2aa064(0x266)]||0x0)),console[_0x2aa064(0x25d)]('[翰林院-Rerank]\x20元数据加权排序完成。'),_0x135a13[_0x2aa064(0x230)](0x0,_0x4409e6[_0x2aa064(0x228)][_0x2aa064(0x240)]);}async function rearrangeChat(_0x4e8a71,_0x18b97c,_0x2e64ed,_0x312ed5){const _0xe38585=_0x31e176;setExtensionPrompt(_0xe38585(0x24d),'',settings[_0xe38585(0x22e)][_0xe38585(0x235)],settings[_0xe38585(0x22e)][_0xe38585(0x26e)],![],settings['injection'][_0xe38585(0x20d)]);if(_0x312ed5===_0xe38585(0x251)||!settings[_0xe38585(0x224)][_0xe38585(0x22b)])return;const _0x135bb1=_0x4e8a71['slice'](-settings[_0xe38585(0x24e)][_0xe38585(0x271)]);if(_0x135bb1[_0xe38585(0x25a)]===0x0)return;const _0x520506=_0x135bb1[_0xe38585(0x206)](_0x1e12c4=>_0x1e12c4[_0xe38585(0x202)])[_0xe38585(0x237)]('\x20')[_0xe38585(0x258)](/<[^>]*>/g,'')['trim']();if(!_0x520506)return;try{const _0x54dec2=await queryVectors(_0x520506);if(_0x54dec2['length']===0x0)return;const _0x28e1f8=await rerankResults(_0x54dec2,_0x520506,settings);if(_0x28e1f8[_0xe38585(0x25a)]===0x0)return;const _0x91f5a9=_0x28e1f8[_0xe38585(0x206)](_0x35272f=>_0x35272f[_0xe38585(0x278)])['join']('\x0a\x0a');let _0x3d15eb=settings['injection'][_0xe38585(0x270)][_0xe38585(0x258)]('{{text}}',_0x91f5a9);_0x3d15eb[_0xe38585(0x231)]()&&(_0x3d15eb='%%HANLINYUAN_RAG_INJECTION%%'+_0x3d15eb),setExtensionPrompt('HANLINYUAN_RAG',_0x3d15eb,settings[_0xe38585(0x22e)][_0xe38585(0x235)],settings[_0xe38585(0x22e)]['depth'],![],settings[_0xe38585(0x22e)][_0xe38585(0x20d)]);}catch(_0x1e1cc8){console[_0xe38585(0x1b8)](_0xe38585(0x1fc),_0x1e1cc8);if(settings[_0xe38585(0x224)][_0xe38585(0x219)])showNotification('忆识检索失败:\x20'+_0x1e1cc8[_0xe38585(0x246)],'error');}}async function moveKnowledgeBase(_0x574ce1,_0x40c488){const _0x3600a8=_0x31e176,_0x1f6338=_0x40c488===_0x3600a8(0x19c)?'local':_0x3600a8(0x19c),_0x6a3461=getCharacterStableId();if(!_0x6a3461&&_0x1f6338===_0x3600a8(0x272)){toastr[_0x3600a8(0x1b8)](_0x3600a8(0x1d9));return;}const _0x4b84aa=_0x40c488===_0x3600a8(0x19c)?getGlobalKnowledgeBases():getLocalKnowledgeBases(),_0x20abd2=_0x1f6338===_0x3600a8(0x19c)?getGlobalKnowledgeBases():getLocalKnowledgeBases(),_0x5928df=_0x4b84aa[_0x574ce1];if(!_0x5928df){const _0x450a3f=_0x3600a8(0x25c)+_0x40c488+'\x27\x20中未找到ID为\x20'+_0x574ce1+_0x3600a8(0x1cf);console[_0x3600a8(0x1b8)]('[翰林院-配置]\x20'+_0x450a3f),toastr[_0x3600a8(0x1b8)]('移动失败:未找到源条目。');return;}_0x40c488===_0x3600a8(0x272)&&_0x1f6338===_0x3600a8(0x19c)&&!_0x5928df[_0x3600a8(0x203)]&&(console[_0x3600a8(0x25d)]('[翰林院-配置]\x20为旧版知识库\x20'+_0x574ce1+_0x3600a8(0x1a1)+_0x6a3461),_0x5928df[_0x3600a8(0x203)]=_0x6a3461);delete _0x4b84aa[_0x574ce1],_0x20abd2[_0x574ce1]=_0x5928df,saveSettings();const _0x370a4c=_0x3600a8(0x265)+_0x5928df[_0x3600a8(0x19a)]+_0x3600a8(0x1e6)+(_0x1f6338===_0x3600a8(0x19c)?'全局':'局部')+'。';console[_0x3600a8(0x25d)]('[翰林院-配置]\x20'+_0x370a4c),toastr[_0x3600a8(0x19b)](_0x370a4c,_0x3600a8(0x24c));}async function getAllVectorsFromCollection(_0x4559c6){const _0x36ea1d=_0x31e176,_0x3aa0f5='*',_0x382f55={'collectionId':_0x4559c6,'searchText':_0x3aa0f5,'topK':0x2710,'threshold':0x0,'source':_0x36ea1d(0x22f),'embeddings':{}},_0x3b6856=(await getEmbeddings([_0x3aa0f5]))[0x0];_0x382f55['embeddings']={[_0x3aa0f5]:_0x3b6856};const _0x52e48a=await fetch(_0x36ea1d(0x1bc),{'method':_0x36ea1d(0x250),'headers':context[_0x36ea1d(0x1dd)](),'body':JSON[_0x36ea1d(0x21a)](_0x382f55)});if(!_0x52e48a['ok']){if(_0x52e48a['status']===0x194)return console['log'](_0x36ea1d(0x24f)+_0x4559c6+'\x20不存在返回空数组。'),[];const _0x1f9c7f=await _0x52e48a[_0x36ea1d(0x278)]();throw new Error(_0x36ea1d(0x1e7)+_0x4559c6+_0x36ea1d(0x1f2)+_0x1f9c7f);}const _0x414042=await _0x52e48a[_0x36ea1d(0x1c9)]();return _0x414042[_0x36ea1d(0x1e4)]||_0x414042[_0x36ea1d(0x1af)]||_0x414042[_0x36ea1d(0x1c5)]||[];}