Files
ST-Amily2-Chat-Optimisation…/core/rag-processor.js
2025-09-17 20:44:54 +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 _0x468c3a=_0x5109;(function(_0x4cafd9,_0x5b8e51){const _0x3c5930=_0x5109,_0x3967f6=_0x4cafd9();while(!![]){try{const _0x5c9a38=-parseInt(_0x3c5930(0x17d))/0x1+-parseInt(_0x3c5930(0x167))/0x2+-parseInt(_0x3c5930(0x126))/0x3+-parseInt(_0x3c5930(0x159))/0x4*(parseInt(_0x3c5930(0x1ee))/0x5)+-parseInt(_0x3c5930(0x184))/0x6*(-parseInt(_0x3c5930(0x1b0))/0x7)+parseInt(_0x3c5930(0x121))/0x8*(-parseInt(_0x3c5930(0x1b9))/0x9)+parseInt(_0x3c5930(0x142))/0xa*(parseInt(_0x3c5930(0x160))/0xb);if(_0x5c9a38===_0x5b8e51)break;else _0x3967f6['push'](_0x3967f6['shift']());}catch(_0xc8b8df){_0x3967f6['push'](_0x3967f6['shift']());}}}(_0x5abd,0xf1a11));function _0x5109(_0x12e9f5,_0x11e87e){const _0x5abd46=_0x5abd();return _0x5109=function(_0x51095a,_0x67ba8){_0x51095a=_0x51095a-0x118;let _0x41c784=_0x5abd46[_0x51095a];return _0x41c784;},_0x5109(_0x12e9f5,_0x11e87e);}import{extension_prompt_roles,setExtensionPrompt}from'/script.js';import*as _0x36cde4 from'./utils/context-utils.js';import{getCollectionIdInfo,getCharacterId,getCharacterStableId}from'./utils/context-utils.js';import{defaultSettings as _0x12e010}from'./rag-settings.js';import*as _0x16fcfd from'./ingestion-manager.js';import{getEmbeddings,fetchEmbeddingModels as _0x270350,fetchRerankModels as _0xaf9e02,executeRerank,testApiConnection as _0x5397dd}from'./rag-api.js';const MODULE_NAME=_0x468c3a(0x13d),OFFICIAL_REARRANGE_CHAT_FUNCTION_NAME='vectors_rearrangeChat',GLOBAL_SCOPE_ID=_0x468c3a(0x175);let context=null,settings=null,lockedCollectionId=null;export{initialize,getSettings,saveSettings,resetSettings,_0x5397dd as testApiConnection,_0x270350 as fetchEmbeddingModels,_0xaf9e02 as fetchRerankModels,getVectorCount,purgeStorage,getMessagesForCondensation,processCondensation,ingestTextToHanlinyuan,getCollectionId,toggleSessionLock,isSessionLocked,getLockedSessionInfo,addKnowledgeBase,removeKnowledgeBase,getLocalKnowledgeBases,getGlobalKnowledgeBases,toggleKnowledgeBase,moveKnowledgeBase};function initialize(){const _0x27d9da=_0x468c3a;context=SillyTavern['getContext']();if(!context){console[_0x27d9da(0x173)](_0x27d9da(0x1f8));return;}settings=getSettings(),!window[_0x27d9da(0x171)]&&(window[_0x27d9da(0x171)]={}),window['hanlinyuanRagProcessor'][_0x27d9da(0x141)]=rearrangeChat,window[_0x27d9da(0x171)][_0x27d9da(0x165)]=!![],console[_0x27d9da(0x187)](_0x27d9da(0x11c));}async function ingestTextToHanlinyuan(_0x3ba7af,_0x578c53=_0x468c3a(0x130),_0x14e43f={},_0x24c022=()=>{},_0x2f2451=null,_0x233087=()=>{},_0x87f819=()=>{},_0xfd6cb8=null,_0x409ded=0x0){const _0x404fb5=_0x468c3a;if(!_0x3ba7af||!_0x3ba7af[_0x404fb5(0x1c9)]())return{'success':![],'error':_0x404fb5(0x1a6)};if(!settings)return{'success':![],'error':_0x404fb5(0x1df)};try{const _0x3b439e=getCollectionIdInfo(),_0x2f9711=await _0x2f878b();if(_0x3b439e[_0x404fb5(0x1ce)]&&_0x3b439e[_0x404fb5(0x1ce)]===_0x2f9711&&_0x3b439e[_0x404fb5(0x1ce)]!==_0x3b439e['newId']){const _0x4ad7c9=confirm(_0x404fb5(0x123));if(_0x4ad7c9)_0x233087(_0x404fb5(0x1f4)+_0x3b439e['oldId'],_0x404fb5(0x172)),await purgeStorage(_0x3b439e[_0x404fb5(0x1ce)]),_0x233087(_0x404fb5(0x197),_0x404fb5(0x185));else return _0x233087('[翰林院-迁移]\x20用户取消了迁移操作。',_0x404fb5(0x1bc)),toastr['info']('操作已取消。'),{'success':![],'error':_0x404fb5(0x161)};}let _0x87d125,_0x57070d;const _0xc9c683=new Date()['toLocaleString'](_0x404fb5(0x13a),{'hour12':![]}),_0x591ee8=getCharacterName()||'未知角色';switch(_0x578c53){case _0x404fb5(0x1e2):const _0x372dc0=_0x14e43f[_0x404fb5(0x1e7)]||{},_0x5b1d1c=_0x372dc0[_0x404fb5(0x1d4)]??'?',_0x870d6=_0x372dc0[_0x404fb5(0x139)]===0x0?'末':_0x372dc0[_0x404fb5(0x139)]??'?';_0x87d125=_0x591ee8+':\x20'+_0x5b1d1c+'楼-'+_0x870d6+'楼';break;case'lorebook':const _0x342262=_0x14e43f[_0x404fb5(0x1da)]||_0x404fb5(0x180),_0x24af59=_0x14e43f[_0x404fb5(0x145)]||_0x404fb5(0x1ea);_0x87d125=_0x342262+':\x20'+_0x24af59;break;case'novel':_0x87d125='小说:\x20'+(_0x14e43f['sourceName']||'未知小说');break;case _0x404fb5(0x130):default:_0x87d125='手动录入:\x20'+_0xc9c683;break;}const _0x1f71b9=Object[_0x404fb5(0x1b5)](getKnowledgeBases()),_0x2dbe49=_0x1f71b9[_0x404fb5(0x1a0)](_0x5e2d76=>_0x5e2d76[_0x404fb5(0x17f)]===_0x87d125);if(_0x2dbe49)_0x57070d=_0x2dbe49['id'],_0x233087(_0x404fb5(0x168)+_0x87d125+_0x404fb5(0x1a2),_0x404fb5(0x1bc));else{_0x233087(_0x404fb5(0x1ae)+_0x87d125+_0x404fb5(0x14c),_0x404fb5(0x1bc));const _0x557d84=addKnowledgeBase(_0x87d125);_0x57070d=_0x557d84['id'];}const _0x3750dd=getCharacterStableId(),_0x2f2d61=_0x3750dd+'_'+_0x57070d;_0x233087(_0x404fb5(0x194)+_0x87d125+'\x20(集合ID:\x20'+_0x2f2d61+')',_0x404fb5(0x185)),_0x233087(_0x404fb5(0x183)+_0x2f2d61,_0x404fb5(0x1bc)),_0x24c022({'message':'正在智能分块...','processed':0x0,'total':0x1});const _0x24cc7e=splitIntoChunks(_0x3ba7af,_0x578c53,_0x14e43f),_0x40b090=_0x24cc7e[_0x404fb5(0x1be)];if(_0x2f2451?.[_0x404fb5(0x19a)])throw new Error('AbortError');_0x233087(_0x404fb5(0x1f6)+_0x87d125+_0x404fb5(0x1cb)+_0x40b090+_0x404fb5(0x151),'info');if(_0x40b090===0x0)return{'success':!![],'count':0x0};const _0x31790b=settings[_0x404fb5(0x18e)][_0x404fb5(0x1fe)]||0x5;let _0xc69787=_0x409ded;for(let _0x373f3d=_0x409ded;_0x373f3d<_0x40b090;_0x373f3d+=_0x31790b){if(_0x2f2451?.['aborted'])throw new Error(_0x404fb5(0x11e));const _0x4f4641=_0x24cc7e[_0x404fb5(0x1e0)](_0x373f3d,_0x373f3d+_0x31790b);_0x24c022({'message':_0x404fb5(0x1de)+(_0x373f3d+0x1)+'-'+(_0x373f3d+_0x4f4641[_0x404fb5(0x1be)])+'\x20块','processed':_0x373f3d,'total':_0x40b090});const _0x141e3b=_0x4f4641[_0x404fb5(0x118)](_0x5a9e7b=>_0x5a9e7b[_0x404fb5(0x153)]),_0x40ff4d=await getEmbeddings(_0x141e3b,_0x2f2451);if(_0x2f2451?.[_0x404fb5(0x19a)])throw new Error(_0x404fb5(0x11e));if(_0x4f4641[_0x404fb5(0x1be)]!==_0x40ff4d['length'])throw new Error('文本块和向量数量不匹配');const _0x2c1d8a=_0x4f4641[_0x404fb5(0x118)]((_0x4a602c,_0x1fd2b0)=>({..._0x4a602c,'vector':_0x40ff4d[_0x1fd2b0]}));await insertVectors(_0x2c1d8a,_0x2f2451,_0x2f2d61),_0xc69787+=_0x4f4641['length'],_0xfd6cb8&&_0x16fcfd['saveProgress'](_0xfd6cb8,_0xc69787,_0x40b090),await _0x87f819();}return _0xfd6cb8&&_0x16fcfd['clearJob'](_0xfd6cb8),_0x233087(_0x404fb5(0x12b)+_0xc69787+_0x404fb5(0x208),'success'),{'success':!![],'count':_0xc69787};}catch(_0x34f2d6){if(_0x34f2d6[_0x404fb5(0x17f)]==='AbortError'){_0x233087(_0x404fb5(0x14a),_0x404fb5(0x172));throw _0x34f2d6;}return console[_0x404fb5(0x173)](_0x404fb5(0x1bf),_0x34f2d6),_0x233087(_0x404fb5(0x14e)+_0x34f2d6[_0x404fb5(0x1e4)],_0x404fb5(0x173)),{'success':![],'error':_0x34f2d6['message']};}}function getSettings(){const _0x215b6e=_0x468c3a;if(!context||!context[_0x215b6e(0x1cf)])return structuredClone(_0x12e010);let _0xbd416c=context['extensionSettings'][MODULE_NAME];!_0xbd416c&&(_0xbd416c={},context[_0x215b6e(0x1cf)][MODULE_NAME]=_0xbd416c);_0xbd416c[_0x215b6e(0x1a4)]===undefined&&(_0xbd416c[_0x215b6e(0x1a4)]={});_0xbd416c[_0x215b6e(0x1bd)]===undefined&&(_0xbd416c[_0x215b6e(0x1bd)]={});for(const _0x4a808f in _0x12e010){if(_0xbd416c[_0x4a808f]===undefined)_0xbd416c[_0x4a808f]=structuredClone(_0x12e010[_0x4a808f]);else{if(typeof _0x12e010[_0x4a808f]===_0x215b6e(0x19c)&&!Array[_0x215b6e(0x174)](_0x12e010[_0x4a808f])&&_0x12e010[_0x4a808f]!==null)for(const _0x2b9311 in _0x12e010[_0x4a808f]){_0xbd416c[_0x4a808f][_0x2b9311]===undefined&&(_0xbd416c[_0x4a808f][_0x2b9311]=_0x12e010[_0x4a808f][_0x2b9311]);}}}return _0xbd416c;}function saveSettings(){const _0x64237=_0x468c3a;if(context)context[_0x64237(0x1a3)]();}function _0x5abd(){const _0x2f31c6=['enabled','chat','[翰林院-日志]\x20统计目标集合ID:\x20','rerank','在源作用域\x20\x27','trim','删除知识库失败,未能清空后端数据。','\x27的文本分割成\x20','user','[翰林院-核心]\x20已为角色\x20','oldId','extensionSettings','[翰林院]\x20检索或注入时发生错误:','send_date','scope','[翰林院-核心]\x20清空向量集合\x20','start','第1章','reduce','[翰林院-核心]\x20准备删除知识库\x20',',将清空集合:\x20','外部Rerank完成','bookName','push','replace','substring','正在处理\x20','核心未初始化','slice','score','chat_history','is_user','message','手动录入','[翰林院-日志]\x20无法确定要清空的目标集合ID。','range','旧版宝库\x20(Legacy)','\x20及其向量数据。','未知条目','部分]','[翰林院-日志]\x20集合\x20','[翰林院-日志]\x20获取集合\x20','4179850niVWdG','[翰林院-核心]\x20已将\x20','\x20列表API时出现问题\x20(状态:\x20','owner','local','filter','[翰林院-迁移]\x20用户确认迁移正在处理旧宝库:\x20','[翰林院-核心]\x20成功删除知识库\x20','[翰林院-核心]\x20将来源\x27','[来源:\x20聊天记录,\x20楼层:\x20#','[翰林院]\x20未能获取SillyTavern上下文初始化失败。','injection','世界书条目','%%HANLINYUAN_RAG_INJECTION%%','random','[翰林院-日志]\x20开始获取所有知识库的向量总数...','batchSize','\x20(范围:\x20','toISOString','[翰林院-配置]\x20为旧版知识库\x20','[翰林院-Rerank]\x20开始元数据加权最终排序...','Rerank失败:\x20','notify','mes','移动失败:没有当前角色,无法移入局部知识库。','\x20的知识库。','\x20个向量条目。','map','top_n','[翰林院-日志]\x20清空目标集合ID:\x20','移动失败:未找到源条目。','翰林院忆识核心已启动\x20(V5.2-集成版),已注册到全局\x20hanlinyuanRagProcessor\x20对象。','\x20不存在返回空数组。','AbortError','all','[翰林院-日志]\x20查询知识库\x20','482904aeFLUw','results','检测到旧版数据。此操作将把旧数据迁移到新格式,过程不可逆,是否继续?','[翰林院-日志]\x20没有启用的新知识库尝试查询旧版单体宝库...','小说录入','538785TIHqyF','[翰林院-日志]\x20所有知识库查询完毕共获得\x20','[翰林院-核心]\x20凝识任务已锁定知识库:\x20',',\x20第1卷,\x20第1章,\x20第','No\x20messages\x20to\x20process.','[翰林院-核心]\x20成功插入\x20','rerank_score','[翰林院-迁移]\x20集合\x20','sourceName','[翰林院-日志]\x20清空宝库API调用成功。','manual','messageTypes','\x20失败:\x20','{{text}}','/api/vector/insert','abs','未知角色','文本块和向量数量不匹配','[翰林院-配置]\x20','end','zh-CN','\x20个条目。','floor','hanlinyuan-rag-core','metadata','[翰林院-Rerank]\x20外部Rerank失败将仅使用内部加权。','then','rearrangeChat','3753800RniVfW','unknown',',\x20第','entryName','凝识之权未开启','知识库【','/api/vector/query','vector','[翰林院-核心]\x20文本录入任务被用户中止。','now','\x22\x20创建专属知识库...','\x27使用通用分块逻辑。','[翰林院-核心]\x20文本录入失败:\x20','forEach','[来源:\x20','\x20个块。','getTime','text','/api/vector/purge','[翰林院-Rerank]\x20元数据加权排序完成。','getRequestHeaders','json','聊天记录:\x20','8fydyzk','position','relevance_score','POST','\x20个知识块准备入库。','\x27\x20中未找到ID为\x20',')\x20的状态已切换为:\x20','121FheHoN','用户取消了迁移操作','[翰林院-日志]\x20开始清空宝库...','\x20(集合ID:\x20','第1卷','initialized','original_index','2417238xTSDDq','[翰林院-核心]\x20检测到同名知识库\x20\x22','忆识存入API错误\x20','findIndex','legacy','webllm','source','\x20(ID:\x20','[翰林院-核心]\x20聊天记录凝识失败:\x20','聊天记录','hanlinyuanRagProcessor','warn','error','isArray','_global','global','[翰林院-核心]\x20已为宝库\x20','知识库\x20\x22','\x20时发生网络错误:','[翰林院-计数]\x20在作用域\x20\x27','(已锁定:\x20','min','1585011VOljWC','hashes','name','未分类世界书','无法确定要清空的目标宝库。','\x20失败删除操作中止。','[翰林院-核心]\x20已锁定忆识宝库ID:\x20','10120722HUgyHZ','success','[翰林院-日志]\x20清空宝库API错误:','log','\x0a</','lorebook','has','\x20条结果。','insertVectors\x20必须接收一个有效的\x20collectionId\x20参数。','embeddings','retrieval','\x20补充所有者ID:\x20','HANLINYUAN_RAG','\x20-\x20楼层\x20#','[翰林院-日志]\x20所有知识库统计完成总向量数:\x20','condensation','[翰林院-核心]\x20已创建并锁定知识库:\x20','\x20失败:','advanced','[翰林院-迁移]\x20旧宝库已清空。','\x20记录凝识范围:\x20','忆识检索失败:\x20','aborted','matchThreshold','object','未知来源','test','charCodeAt','find','世界书','\x22将数据合并入库。','saveSettingsDebounced','condensationHistory','\x20条初步结果。','输入文本为空','join','\x20不存在计为\x200。','novel','[翰林院-日志]\x20去重后剩余\x20','status','[翰林院-Rerank]\x20开始外部API重排序...','sort','[翰林院-核心]\x20准备为任务\x20\x22','[翰林院-日志]\x20正在查询知识库:\x20','7AjDwgr','depth_role','final_score','index','maxResults','values','max','[翰林院-核心]\x20聊天记录凝识完成成功插入\x20','toString','27SzkgMa','stringify','toLocaleString','info','knowledgeBases','length','[翰林院-核心]\x20ingestTextToHanlinyuan\x20失败:','/api/vector/list','\x20添加新知识库:\x20','[来源:\x20世界书,\x20条目:\x20','[翰林院-日志]\x20统计集合\x20'];_0x5abd=function(){return _0x2f31c6;};return _0x5abd();}function resetSettings(){context&&(context['extensionSettings'][MODULE_NAME]=structuredClone(_0x12e010),saveSettings());}function showNotification(_0x16cd86,_0x1ad641=_0x468c3a(0x1bc)){toastr[_0x1ad641](_0x16cd86);}function getTagForSource(_0x2afce9){const _0x172f03=_0x468c3a;switch(_0x2afce9){case _0x172f03(0x1e2):return _0x172f03(0x170);case _0x172f03(0x189):return _0x172f03(0x1a1);case'manual':return _0x172f03(0x1e5);case _0x172f03(0x1a9):return _0x172f03(0x125);default:return'资料';}}function splitIntoChunks(_0x30a140,_0x127d05,_0x5f1d7f={}){const _0x12bece=_0x468c3a;switch(_0x127d05){case _0x12bece(0x1a9):return _chunkForNovel(_0x30a140,_0x5f1d7f);case _0x12bece(0x1e2):return _chunkForChatHistory(_0x30a140,_0x5f1d7f);case _0x12bece(0x189):return _chunkForLorebook(_0x30a140,_0x5f1d7f);case _0x12bece(0x130):return _chunkForManual(_0x30a140,_0x5f1d7f);default:console[_0x12bece(0x172)]('[翰林院-分块]\x20未知的来源类型\x20\x27'+_0x127d05+_0x12bece(0x14d));return _chunkForManual(_0x30a140,{..._0x5f1d7f,'sourceName':_0x5f1d7f[_0x12bece(0x12e)]||_0x12bece(0x19d)});}}function _chunkForNovel(_0x159b05,_0x1de63c){const _0x3aa47a=_0x468c3a,{chunkSize:_0x52b605,overlap:_0x3bee5a}=settings[_0x3aa47a(0x196)],{sourceName:sourceName='小说'}=_0x1de63c,_0x3e7c41=[];if(!_0x159b05||_0x52b605<=0x0)return _0x3e7c41;const _0x3d012e=/(第\s*[一二三四五六七八九十百千万零\d]+\s*卷)/gim,_0x2c1e0c=/(第\s*[一二三四五六七八九十百千万零\d]+\s*[章回节部])|^(Chapter\s+\d+)/gim;let _0x2daa41=0x0;const _0x31df65=_0x159b05['split']('\x0a');let _0x30c47a='第1卷',_0x382966=_0x3aa47a(0x1d5),_0x3e29ed=[];function _0xb86dc6(){const _0x1853ae=_0x3aa47a;if(_0x3e29ed[_0x1853ae(0x1be)]===0x0)return;const _0x472862=_0x3e29ed['join']('\x0a');let _0x595b44=0x0,_0xb1c047=0x1;while(_0x595b44<_0x472862['length']){const _0x2ad518=Math[_0x1853ae(0x17c)](_0x595b44+_0x52b605,_0x472862[_0x1853ae(0x1be)]),_0x16e4e5=_0x472862['substring'](_0x595b44,_0x2ad518);if(_0x16e4e5[_0x1853ae(0x1c9)]()[_0x1853ae(0x1be)]>0x0){const _0x5ca35c={'source':_0x1853ae(0x1a9),'sourceName':sourceName,'timestamp':new Date()[_0x1853ae(0x200)](),'globalIndex':_0x2daa41++,'volume':_0x30c47a,'chapter':_0x382966,'section':_0xb1c047},_0x169c13=getTagForSource(_0x1853ae(0x1a9)),_0x162a67=_0x1853ae(0x150)+sourceName+',\x20'+_0x30c47a+',\x20'+_0x382966+_0x1853ae(0x144)+_0xb1c047+'节]',_0x524e43='<'+_0x169c13+'>\x0a'+_0x162a67+'\x0a'+_0x16e4e5+_0x1853ae(0x188)+_0x169c13+'>';_0x3e7c41[_0x1853ae(0x1db)]({'text':_0x524e43,'metadata':_0x5ca35c}),_0xb1c047++;}_0x595b44+=_0x52b605-_0x3bee5a;if(_0x595b44>=_0x472862[_0x1853ae(0x1be)])break;}_0x3e29ed=[];}for(const _0x1a6432 of _0x31df65){const _0x3df284=_0x1a6432[_0x3aa47a(0x1c9)]();if(_0x3d012e[_0x3aa47a(0x19e)](_0x3df284))_0xb86dc6(),_0x30c47a=_0x3df284,_0x382966=_0x3aa47a(0x1d5);else _0x2c1e0c[_0x3aa47a(0x19e)](_0x3df284)?(_0xb86dc6(),_0x382966=_0x3df284):_0x3e29ed['push'](_0x1a6432);}_0xb86dc6();if(_0x3e7c41[_0x3aa47a(0x1be)]===0x0&&_0x159b05[_0x3aa47a(0x1be)]>0x0){let _0x1c2e04=0x0,_0x694a72=0x1;while(_0x1c2e04<_0x159b05[_0x3aa47a(0x1be)]){const _0x4c2ee0=Math['min'](_0x1c2e04+_0x52b605,_0x159b05[_0x3aa47a(0x1be)]),_0x118160=_0x159b05[_0x3aa47a(0x1dd)](_0x1c2e04,_0x4c2ee0),_0x49469d={'source':_0x3aa47a(0x1a9),'sourceName':sourceName,'timestamp':new Date()[_0x3aa47a(0x200)](),'globalIndex':_0x3e7c41[_0x3aa47a(0x1be)],'volume':_0x3aa47a(0x164),'chapter':_0x3aa47a(0x1d5),'section':_0x694a72},_0x303960=getTagForSource(_0x3aa47a(0x1a9)),_0x4c245f='[来源:\x20'+sourceName+_0x3aa47a(0x129)+_0x694a72+'节]',_0x1afedf='<'+_0x303960+'>\x0a'+_0x4c245f+'\x0a'+_0x118160+'\x0a</'+_0x303960+'>';_0x3e7c41[_0x3aa47a(0x1db)]({'text':_0x1afedf,'metadata':_0x49469d}),_0x694a72++,_0x1c2e04+=_0x52b605-_0x3bee5a;}}return _0x3e7c41;}function _chunkForChatHistory(_0x2b28a2,_0x3155d3){const _0x20648e=_0x468c3a,{chunkSize:_0x5a411c,overlap:_0x32cc59}=settings[_0x20648e(0x196)],{floor:_0x14715a,is_user:_0x2f6b17,timestamp:_0x37db92}=_0x3155d3,_0x277a6f=[];if(!_0x2b28a2||_0x5a411c<=0x0)return _0x277a6f;let _0x1204fb=0x1,_0x59669e=0x0;while(_0x59669e<_0x2b28a2['length']){const _0x2338e9=Math[_0x20648e(0x17c)](_0x59669e+_0x5a411c,_0x2b28a2['length']),_0x32aace=_0x2b28a2[_0x20648e(0x1dd)](_0x59669e,_0x2338e9),_0x491cae=_0x20648e(0x1f7)+_0x14715a+_0x20648e(0x144)+_0x1204fb+_0x20648e(0x1eb),_0x7016d5=getTagForSource(_0x20648e(0x1e2)),_0x222375='<'+_0x7016d5+'>\x0a'+_0x491cae+'\x0a'+_0x32aace+_0x20648e(0x188)+_0x7016d5+'>';_0x277a6f[_0x20648e(0x1db)]({'text':_0x222375,'metadata':{'source':_0x20648e(0x1e2),'sourceName':'聊天记录\x20#'+_0x14715a,'floor':_0x14715a,'part':_0x1204fb,'is_user':_0x2f6b17,'timestamp':_0x37db92}}),_0x1204fb++,_0x59669e+=_0x5a411c-_0x32cc59;if(_0x59669e>=_0x2b28a2['length'])break;}return _0x277a6f;}function _chunkForLorebook(_0x418b34,_0x39e2ea){const _0x207880=_0x468c3a,{chunkSize:_0x658110,overlap:_0xb6e86b}=settings['advanced'],{sourceName:sourceName=_0x207880(0x1fa)}=_0x39e2ea,_0x5f0fb7=[];if(!_0x418b34||_0x658110<=0x0)return _0x5f0fb7;let _0x4d76b9=0x1,_0x393840=0x0;while(_0x393840<_0x418b34[_0x207880(0x1be)]){const _0x5cf712=Math[_0x207880(0x17c)](_0x393840+_0x658110,_0x418b34['length']),_0x2e1d48=_0x418b34[_0x207880(0x1dd)](_0x393840,_0x5cf712),_0x2ffa50=_0x207880(0x1c2)+sourceName+_0x207880(0x144)+_0x4d76b9+_0x207880(0x1eb),_0x279f82=getTagForSource('lorebook'),_0x35ba04='<'+_0x279f82+'>\x0a'+_0x2ffa50+'\x0a'+_0x2e1d48+_0x207880(0x188)+_0x279f82+'>';_0x5f0fb7[_0x207880(0x1db)]({'text':_0x35ba04,'metadata':{'source':_0x207880(0x189),'sourceName':sourceName,'part':_0x4d76b9,'timestamp':new Date()[_0x207880(0x200)]()}}),_0x4d76b9++,_0x393840+=_0x658110-_0xb6e86b;if(_0x393840>=_0x418b34['length'])break;}return _0x5f0fb7;}function _chunkForManual(_0x201430,_0x735b5){const _0x68ec94=_0x468c3a,{chunkSize:_0x4a1534,overlap:_0x5ca975}=settings[_0x68ec94(0x196)],{sourceName:sourceName=_0x68ec94(0x1e5)}=_0x735b5,_0x1d631a=[];if(!_0x201430||_0x4a1534<=0x0)return _0x1d631a;const _0x27bbb8=new Date(),_0x2202d7=_0x27bbb8[_0x68ec94(0x1bb)](_0x68ec94(0x13a));let _0x5318c2=0x1,_0x2221f2=0x0;while(_0x2221f2<_0x201430[_0x68ec94(0x1be)]){const _0x3a0d80=Math[_0x68ec94(0x17c)](_0x2221f2+_0x4a1534,_0x201430[_0x68ec94(0x1be)]),_0x2439b9=_0x201430[_0x68ec94(0x1dd)](_0x2221f2,_0x3a0d80),_0x4595a6=_0x68ec94(0x150)+sourceName+',\x20向量化录入时间:\x20'+_0x2202d7+_0x68ec94(0x144)+_0x5318c2+_0x68ec94(0x1eb),_0x2db1a1=getTagForSource('manual'),_0x4394d3='<'+_0x2db1a1+'>\x0a'+_0x4595a6+'\x0a'+_0x2439b9+'\x0a</'+_0x2db1a1+'>';_0x1d631a[_0x68ec94(0x1db)]({'text':_0x4394d3,'metadata':{'source':'manual','sourceName':sourceName,'part':_0x5318c2,'timestamp':_0x27bbb8['toISOString']()}}),_0x5318c2++,_0x2221f2+=_0x4a1534-_0x5ca975;if(_0x2221f2>=_0x201430[_0x68ec94(0x1be)])break;}return _0x1d631a;}import{getCollectionId as _0x2f878b,getCharacterName}from'./utils/context-utils.js';async function getCollectionId(){return lockedCollectionId||await _0x2f878b();}async function toggleSessionLock(){return lockedCollectionId?(lockedCollectionId=null,![]):(lockedCollectionId=await _0x2f878b(),!![]);}function isSessionLocked(){return lockedCollectionId!==null;}function getLockedSessionInfo(){const _0x2437fb=_0x468c3a;if(!lockedCollectionId)return null;return{'id':lockedCollectionId,'name':_0x2437fb(0x17b)+lockedCollectionId['substring'](0x0,0x8)+'...)'};}function getLocalKnowledgeBases(){const _0x25b829=_0x468c3a,_0x2bc854=getCharacterStableId();return!settings[_0x25b829(0x1bd)][_0x2bc854]&&(settings[_0x25b829(0x1bd)][_0x2bc854]={}),settings[_0x25b829(0x1bd)][_0x2bc854];}function getGlobalKnowledgeBases(){const _0x43a896=_0x468c3a;return!settings[_0x43a896(0x1bd)][GLOBAL_SCOPE_ID]&&(settings['knowledgeBases'][GLOBAL_SCOPE_ID]={}),settings[_0x43a896(0x1bd)][GLOBAL_SCOPE_ID];}function getKnowledgeBases(){const _0x21680e=getLocalKnowledgeBases(),_0x227c95=getGlobalKnowledgeBases();return{..._0x227c95,..._0x21680e};}function addKnowledgeBase(_0x204ff7){const _0x44e7f5=_0x468c3a;if(!_0x204ff7||!_0x204ff7[_0x44e7f5(0x1c9)]())throw new Error('知识库名称不能为空');const _0x1fffbf=getCharacterStableId(),_0x2c55c8=getLocalKnowledgeBases(),_0x1964ef='task_'+Date['now']()+'_'+Math[_0x44e7f5(0x1fc)]()[_0x44e7f5(0x1b8)](0x24)[_0x44e7f5(0x1dd)](0x2,0x9),_0x5968e7={'id':_0x1964ef,'name':_0x204ff7[_0x44e7f5(0x1c9)](),'enabled':!![],'createdAt':new Date()[_0x44e7f5(0x200)](),'owner':_0x1fffbf};return _0x2c55c8[_0x1964ef]=_0x5968e7,saveSettings(),console['log'](_0x44e7f5(0x1cd)+_0x1fffbf+_0x44e7f5(0x1c1)+_0x204ff7+_0x44e7f5(0x16e)+_0x1964ef+')'),_0x5968e7;}async function removeKnowledgeBase(_0x5f36b8,_0x5b6f88){const _0x23f2cc=_0x468c3a,_0x12463f=getCharacterStableId(),_0x2f4602=_0x5b6f88===_0x23f2cc(0x176)?getGlobalKnowledgeBases():getLocalKnowledgeBases(),_0x1df496=_0x2f4602[_0x5f36b8],_0xb733bc=_0x1df496?.[_0x23f2cc(0x17f)]||_0x5f36b8;if(!_0x1df496){console[_0x23f2cc(0x172)]('[翰林院-核心]\x20尝试删除一个不存在的知识库:\x20'+_0x5f36b8+_0x23f2cc(0x1ff)+_0x5b6f88+')');return;}const _0x41a73=_0x5b6f88===_0x23f2cc(0x176)?_0x1df496[_0x23f2cc(0x1f1)]||GLOBAL_SCOPE_ID:_0x12463f,_0xb8a181=_0x41a73+'_'+_0x5f36b8;console[_0x23f2cc(0x187)](_0x23f2cc(0x1d7)+_0x5f36b8+_0x23f2cc(0x1d8)+_0xb8a181);const _0x4125fa=await purgeStorage(_0xb8a181);_0x4125fa?(delete _0x2f4602[_0x5f36b8],saveSettings(),console[_0x23f2cc(0x187)](_0x23f2cc(0x1f5)+_0x5f36b8+_0x23f2cc(0x1e9)),toastr[_0x23f2cc(0x185)](_0x23f2cc(0x178)+_0xb733bc+'\x22\x20已删除。')):(console[_0x23f2cc(0x173)](_0x23f2cc(0x1d3)+_0xb8a181+_0x23f2cc(0x182)),toastr[_0x23f2cc(0x173)](_0x23f2cc(0x1ca)));}function toggleKnowledgeBase(_0x53e845,_0x38abd8){const _0xd96320=_0x468c3a,_0x96fbd8=_0x38abd8==='global'?getGlobalKnowledgeBases():getLocalKnowledgeBases();_0x96fbd8[_0x53e845]&&(_0x96fbd8[_0x53e845][_0xd96320(0x1c4)]=!_0x96fbd8[_0x53e845][_0xd96320(0x1c4)],saveSettings(),console[_0xd96320(0x187)]('[翰林院-核心]\x20知识库\x20'+_0x53e845+_0xd96320(0x1ff)+_0x38abd8+_0xd96320(0x15f)+(_0x96fbd8[_0x53e845][_0xd96320(0x1c4)]?'启用':'禁用')));}function generateHash(_0xfb92ab){const _0x22355b=_0x468c3a;let _0x251288=0x0;for(let _0x2954e5=0x0;_0x2954e5<_0xfb92ab[_0x22355b(0x1be)];_0x2954e5++){const _0x1b8e60=_0xfb92ab[_0x22355b(0x19f)](_0x2954e5);_0x251288=(_0x251288<<0x5)-_0x251288+_0x1b8e60,_0x251288=_0x251288&_0x251288;}return Math[_0x22355b(0x135)](_0x251288)['toString'](0x24);}async function queryVectors(_0x5160d3){const _0x33b40d=_0x468c3a;console[_0x33b40d(0x187)]('[翰林院-日志]\x20开始多知识库向量查询...');const _0x45397b=getCharacterStableId(),_0x46b2d1=getLocalKnowledgeBases(),_0x7e21b7=getGlobalKnowledgeBases(),_0x407f9d=Object[_0x33b40d(0x1b5)](_0x46b2d1)[_0x33b40d(0x1f3)](_0x1b98c0=>_0x1b98c0[_0x33b40d(0x1c4)]),_0x51ef43=Object['values'](_0x7e21b7)['filter'](_0x11d9b8=>_0x11d9b8[_0x33b40d(0x1c4)]),_0x52f16b=[..._0x407f9d[_0x33b40d(0x118)](_0x44b79f=>({..._0x44b79f,'scope':'local'})),..._0x51ef43[_0x33b40d(0x118)](_0x4b16f6=>({..._0x4b16f6,'scope':_0x33b40d(0x176)}))];if(_0x52f16b[_0x33b40d(0x1be)]===0x0){console[_0x33b40d(0x187)](_0x33b40d(0x124));const _0x289e2a=await _0x2f878b();if(!_0x289e2a)return[];_0x52f16b[_0x33b40d(0x1db)]({'id':null,'name':_0x33b40d(0x1e8),'scope':'legacy'});}const _0x56c3aa=(await getEmbeddings([_0x5160d3]))[0x0];let _0x5b726c=[];const _0x2cd380=_0x52f16b[_0x33b40d(0x118)](_0x4472d6=>{const _0x1a9b52=_0x33b40d;let _0x108f09;if(_0x4472d6['scope']===_0x1a9b52(0x16b))_0x108f09=_0x2f878b();else{const _0x48a031=_0x4472d6[_0x1a9b52(0x1d2)]===_0x1a9b52(0x176)?_0x4472d6[_0x1a9b52(0x1f1)]||GLOBAL_SCOPE_ID:_0x45397b;_0x108f09=Promise['resolve'](_0x48a031+'_'+_0x4472d6['id']);}return _0x108f09[_0x1a9b52(0x140)](_0x2b7159=>{const _0x112830=_0x1a9b52;if(!_0x2b7159)return[];console[_0x112830(0x187)](_0x112830(0x1af)+_0x4472d6[_0x112830(0x17f)]+_0x112830(0x16e)+_0x2b7159+')');const _0x5d7b59={'collectionId':_0x2b7159,'searchText':_0x5160d3,'topK':settings[_0x112830(0x196)][_0x112830(0x1b4)],'threshold':settings[_0x112830(0x196)][_0x112830(0x19b)],'source':_0x112830(0x16c),'embeddings':{[_0x5160d3]:_0x56c3aa}};return fetch(_0x112830(0x148),{'method':_0x112830(0x15c),'headers':context[_0x112830(0x156)](),'body':JSON[_0x112830(0x1ba)](_0x5d7b59)})[_0x112830(0x140)](async _0x58e242=>{const _0x4ff87f=_0x112830;if(!_0x58e242['ok']){const _0x13b2e3=await _0x58e242['text']();return console[_0x4ff87f(0x173)](_0x4ff87f(0x120)+_0x2b7159+_0x4ff87f(0x195),_0x13b2e3),[];}const _0x4616be=await _0x58e242[_0x4ff87f(0x157)](),_0x197f4b=_0x4616be[_0x4ff87f(0x13e)]||_0x4616be[_0x4ff87f(0x122)]||_0x4616be['data']||[];return console[_0x4ff87f(0x187)]('[翰林院-日志]\x20知识库\x20'+_0x4472d6[_0x4ff87f(0x17f)]+'\x20返回\x20'+_0x197f4b['length']+_0x4ff87f(0x18b)),_0x197f4b;})['catch'](_0x1ae501=>{const _0x75d33b=_0x112830;return console[_0x75d33b(0x173)](_0x75d33b(0x120)+_0x2b7159+_0x75d33b(0x179),_0x1ae501),[];});});}),_0x34ab63=await Promise[_0x33b40d(0x11f)](_0x2cd380);_0x5b726c=_0x34ab63['flat'](),console[_0x33b40d(0x187)](_0x33b40d(0x127)+_0x5b726c[_0x33b40d(0x1be)]+_0x33b40d(0x1a5));const _0x23edf5=[],_0x3288b8=new Set();for(const _0x2f2e5b of _0x5b726c){_0x2f2e5b&&_0x2f2e5b['text']&&!_0x3288b8[_0x33b40d(0x18a)](_0x2f2e5b[_0x33b40d(0x153)])&&(_0x3288b8['add'](_0x2f2e5b[_0x33b40d(0x153)]),_0x23edf5[_0x33b40d(0x1db)](_0x2f2e5b));}return console['log'](_0x33b40d(0x1aa)+_0x23edf5[_0x33b40d(0x1be)]+_0x33b40d(0x18b)),_0x23edf5[_0x33b40d(0x1ad)]((_0x3f06bf,_0x56ac3a)=>(_0x56ac3a[_0x33b40d(0x1e1)]||0x0)-(_0x3f06bf['score']||0x0)),_0x23edf5;}async function insertVectors(_0x15fc9b,_0x399a99=null,_0x5e2133){const _0x3e921a=_0x468c3a;if(!_0x5e2133)throw new Error(_0x3e921a(0x18c));if(_0x15fc9b[_0x3e921a(0x1be)]===0x0)return{'success':!![],'count':0x0};const _0x1bc993=_0x15fc9b[_0x3e921a(0x118)]((_0xb2a934,_0x3c33d4)=>({'hash':generateHash(_0xb2a934['text']+Date[_0x3e921a(0x14b)]()+_0x3c33d4),'text':_0xb2a934[_0x3e921a(0x153)],'metadata':_0xb2a934[_0x3e921a(0x13e)]||{'source':_0x3e921a(0x143),'timestamp':new Date()[_0x3e921a(0x200)]()}})),_0x3c7fc0=_0x1bc993[_0x3e921a(0x1d6)]((_0x4ad07f,_0x2ef130,_0x49918d)=>{const _0x1643ec=_0x3e921a;return _0x4ad07f[_0x2ef130[_0x1643ec(0x153)]]=_0x15fc9b[_0x49918d][_0x1643ec(0x149)],_0x4ad07f;},{}),_0x206655={'collectionId':_0x5e2133,'items':_0x1bc993,'source':_0x3e921a(0x16c),'embeddings':_0x3c7fc0},_0x256240=await fetch(_0x3e921a(0x134),{'method':_0x3e921a(0x15c),'headers':context[_0x3e921a(0x156)](),'body':JSON['stringify'](_0x206655),'signal':_0x399a99});if(!_0x256240['ok']){const _0x57b3d8=await _0x256240[_0x3e921a(0x153)]();console[_0x3e921a(0x173)]('[翰林院-日志]\x20忆识存入API错误:',_0x57b3d8);throw new Error(_0x3e921a(0x169)+_0x256240['status']+':\x20'+_0x57b3d8);}return{'success':!![],'count':_0x1bc993[_0x3e921a(0x1be)]};}async function getVectorCount(_0x449b62=null,_0x3d2c50=_0x468c3a(0x1f2)){const _0x38e075=_0x468c3a,_0x56deba=getCharacterStableId();if(_0x449b62){const _0x5ecf65=_0x3d2c50===_0x38e075(0x176)?getGlobalKnowledgeBases():getLocalKnowledgeBases(),_0x4e551d=_0x5ecf65[_0x449b62];if(!_0x4e551d)return console[_0x38e075(0x172)](_0x38e075(0x17a)+_0x3d2c50+_0x38e075(0x15e)+_0x449b62+_0x38e075(0x207)),0x0;const _0x164f0c=_0x3d2c50===_0x38e075(0x176)?_0x4e551d[_0x38e075(0x1f1)]||GLOBAL_SCOPE_ID:_0x56deba,_0xa1773c=_0x164f0c+'_'+_0x449b62;return await countVectorsInCollection(_0xa1773c);}else{console[_0x38e075(0x187)](_0x38e075(0x1fd));const _0x4812b5=Object['values'](getLocalKnowledgeBases()),_0x4e4cd4=Object[_0x38e075(0x1b5)](getGlobalKnowledgeBases()),_0x51a4db=[];_0x4812b5[_0x38e075(0x14f)](_0xa55206=>{const _0x41bcf4=_0x56deba+'_'+_0xa55206['id'];_0x51a4db['push'](countVectorsInCollection(_0x41bcf4));}),_0x4e4cd4['forEach'](_0x18ae39=>{const _0x1ddd7d=_0x38e075,_0x43ed99=_0x18ae39[_0x1ddd7d(0x1f1)]||GLOBAL_SCOPE_ID,_0x319aba=_0x43ed99+'_'+_0x18ae39['id'];_0x51a4db[_0x1ddd7d(0x1db)](countVectorsInCollection(_0x319aba));});const _0x32c43c=await _0x2f878b();_0x51a4db['push'](countVectorsInCollection(_0x32c43c));const _0x2a20d3=await Promise[_0x38e075(0x11f)](_0x51a4db),_0x1d4eca=_0x2a20d3['reduce']((_0x1c90cb,_0x178efa)=>_0x1c90cb+_0x178efa,0x0);return console[_0x38e075(0x187)](_0x38e075(0x192)+_0x1d4eca),_0x1d4eca;}}async function countVectorsInCollection(_0x5a8a01){const _0xa564ec=_0x468c3a;if(!_0x5a8a01)return 0x0;console[_0xa564ec(0x187)](_0xa564ec(0x1c6)+_0x5a8a01);const _0x9e9c9f={'collectionId':_0x5a8a01,'source':_0xa564ec(0x16c),'embeddings':{}};try{const _0x20bd44=await fetch(_0xa564ec(0x1c0),{'method':_0xa564ec(0x15c),'headers':context[_0xa564ec(0x156)](),'body':JSON[_0xa564ec(0x1ba)](_0x9e9c9f)});if(!_0x20bd44['ok']){if(_0x20bd44[_0xa564ec(0x1ab)]===0x194)console[_0xa564ec(0x187)](_0xa564ec(0x1ec)+_0x5a8a01+_0xa564ec(0x1a8));else{const _0x34b8c0=await _0x20bd44[_0xa564ec(0x153)]();console[_0xa564ec(0x172)](_0xa564ec(0x1ed)+_0x5a8a01+_0xa564ec(0x1f0)+_0x20bd44[_0xa564ec(0x1ab)]+'):',_0x34b8c0);}return 0x0;}const _0x4437df=await _0x20bd44[_0xa564ec(0x157)]();let _0x4896a3=0x0;if(Array[_0xa564ec(0x174)](_0x4437df))_0x4896a3=_0x4437df[_0xa564ec(0x1be)];else _0x4437df&&_0x4437df['hashes']&&(_0x4896a3=_0x4437df[_0xa564ec(0x17e)][_0xa564ec(0x1be)]);return _0x4896a3;}catch(_0x1750f1){return console[_0xa564ec(0x173)](_0xa564ec(0x1c3)+_0x5a8a01+_0xa564ec(0x179),_0x1750f1),0x0;}}async function purgeStorage(_0xd68128=null){const _0x49ab8f=_0x468c3a;console[_0x49ab8f(0x187)](_0x49ab8f(0x162));const _0x507d41=_0xd68128||await getCollectionId();if(!_0x507d41)return console[_0x49ab8f(0x173)](_0x49ab8f(0x1e6)),toastr[_0x49ab8f(0x173)](_0x49ab8f(0x181)),![];console['log'](_0x49ab8f(0x11a)+_0x507d41);const _0x39eb0d={'collectionId':_0x507d41};console[_0x49ab8f(0x187)]('[翰林院-日志]\x20发送到\x20/api/vector/purge\x20的请求体:',JSON[_0x49ab8f(0x1ba)](_0x39eb0d,null,0x2));const _0x5bd96e=await fetch(_0x49ab8f(0x154),{'method':_0x49ab8f(0x15c),'headers':context[_0x49ab8f(0x156)](),'body':JSON[_0x49ab8f(0x1ba)](_0x39eb0d)});console[_0x49ab8f(0x187)]('[翰林院-日志]\x20/api/vector/purge\x20响应状态:\x20'+_0x5bd96e[_0x49ab8f(0x1ab)]);if(!_0x5bd96e['ok']){const _0x3eb24c=await _0x5bd96e[_0x49ab8f(0x153)]();console[_0x49ab8f(0x173)](_0x49ab8f(0x186),_0x3eb24c);}else console[_0x49ab8f(0x187)](_0x49ab8f(0x12f));return _0x5bd96e['ok'];}function getMessagesForCondensation(_0x29a0ff=null){const _0x3fba01=_0x468c3a;if(!settings['condensation'][_0x3fba01(0x1c4)])return showNotification(_0x3fba01(0x146),'warning'),[];const {layerStart:_0x31ec3d,layerEnd:_0x5bfe51}=settings['condensation'],_0x5b7c29=_0x29a0ff||settings[_0x3fba01(0x193)][_0x3fba01(0x131)],_0x34df2c=context['chat'][_0x3fba01(0x1be)],_0x50e4f1=Math[_0x3fba01(0x1b6)](0x0,_0x31ec3d-0x1),_0x47beec=_0x5bfe51===0x0||_0x5bfe51>_0x34df2c?_0x34df2c:Math[_0x3fba01(0x17c)](_0x34df2c,_0x5bfe51),_0x69e2f6=context[_0x3fba01(0x1c5)]['slice'](_0x50e4f1,_0x47beec);return _0x69e2f6['filter'](_0x3a3704=>{const _0x473669=_0x3fba01,_0x27ac6a=_0x3a3704[_0x473669(0x1e3)]===!![],_0x2ea104=_0x3a3704[_0x473669(0x1e3)]===![];if(!_0x3a3704[_0x473669(0x205)]||!_0x3a3704[_0x473669(0x205)][_0x473669(0x1c9)]())return![];return _0x5b7c29[_0x473669(0x1cc)]&&_0x27ac6a||_0x5b7c29['ai']&&_0x2ea104;});}async function processCondensation(_0x2a8ae9,_0x14f669=()=>{},_0x582bca=null){const _0x1afcf5=_0x468c3a;if(!_0x2a8ae9||_0x2a8ae9['length']===0x0)return{'success':![],'error':_0x1afcf5(0x12a)};try{let _0x1acf7e,_0x415da4;const _0x343811=getCharacterName()||_0x1afcf5(0x136);if(_0x582bca){const _0x4ba6b9=_0x582bca['start']??'?',_0x23ad1f=_0x582bca[_0x1afcf5(0x139)]===0x0?'末':_0x582bca[_0x1afcf5(0x139)]??'?';_0x1acf7e=_0x343811+':\x20'+_0x4ba6b9+'楼-'+_0x23ad1f+'楼';}else{const _0x1bf1b7=new Date()[_0x1afcf5(0x1bb)](_0x1afcf5(0x13a),{'hour12':![]});_0x1acf7e=_0x1afcf5(0x158)+_0x1bf1b7;}const _0x330590=Object[_0x1afcf5(0x1b5)](getLocalKnowledgeBases()),_0x4d85a8=_0x330590['find'](_0x2df4f6=>_0x2df4f6['name']===_0x1acf7e);if(_0x4d85a8)_0x415da4=_0x4d85a8['id'],_0x14f669(_0x1afcf5(0x168)+_0x1acf7e+_0x1afcf5(0x1a2),_0x1afcf5(0x1bc));else{_0x14f669(_0x1afcf5(0x1ae)+_0x1acf7e+_0x1afcf5(0x14c),_0x1afcf5(0x1bc));const _0x5b99d0=addKnowledgeBase(_0x1acf7e);_0x415da4=_0x5b99d0['id'];}const _0x2ff303=getCharacterStableId(),_0x255e21=_0x2ff303+'_'+_0x415da4;_0x14f669(_0x1afcf5(0x128)+_0x1acf7e+_0x1afcf5(0x163)+_0x255e21+')',_0x1afcf5(0x185));const _0x57542e=[],_0x21a2e0=context[_0x1afcf5(0x1c5)];for(const _0x497477 of _0x2a8ae9){const _0x363b5e=(_0x497477[_0x1afcf5(0x205)]||'')[_0x1afcf5(0x1dc)](/<[^>]*>/g,'')[_0x1afcf5(0x1c9)]();if(_0x363b5e[_0x1afcf5(0x1be)]===0x0)continue;let _0x3f56b0;if(_0x497477['floor']!==undefined&&_0x497477['floor']!==null)_0x3f56b0=_0x497477[_0x1afcf5(0x13c)];else{const _0x3a08de=_0x21a2e0[_0x1afcf5(0x16a)](_0x2e2cb5=>_0x2e2cb5===_0x497477);_0x3f56b0=_0x3a08de!==-0x1?_0x3a08de+0x1:-0x1;}const _0x239a41=new Date(_0x497477[_0x1afcf5(0x1d1)]),_0x204a66=isNaN(_0x239a41[_0x1afcf5(0x152)]())?new Date()[_0x1afcf5(0x200)]():_0x239a41[_0x1afcf5(0x200)](),_0x486adb=splitIntoChunks(_0x363b5e,_0x1afcf5(0x1e2),{'floor':_0x3f56b0,'is_user':_0x497477[_0x1afcf5(0x1e3)],'timestamp':_0x204a66});_0x57542e[_0x1afcf5(0x1db)](..._0x486adb);}if(_0x57542e['length']===0x0)return{'success':!![],'count':0x0};_0x14f669(_0x1afcf5(0x1ef)+_0x2a8ae9[_0x1afcf5(0x1be)]+'\x20条消息分解为\x20'+_0x57542e[_0x1afcf5(0x1be)]+_0x1afcf5(0x15d),_0x1afcf5(0x1bc));const _0x127b13=settings[_0x1afcf5(0x18e)][_0x1afcf5(0x1fe)]||0x5;let _0x521d19=0x0;for(let _0x313607=0x0;_0x313607<_0x57542e['length'];_0x313607+=_0x127b13){const _0x3a0e63=_0x57542e['slice'](_0x313607,_0x313607+_0x127b13),_0x13756e=_0x3a0e63[_0x1afcf5(0x118)](_0x1fbf12=>_0x1fbf12[_0x1afcf5(0x153)]),_0x375619=await getEmbeddings(_0x13756e);if(_0x3a0e63[_0x1afcf5(0x1be)]!==_0x375619['length'])throw new Error(_0x1afcf5(0x137));const _0x388118=_0x3a0e63[_0x1afcf5(0x118)]((_0x2b8b79,_0x54b07e)=>({..._0x2b8b79,'vector':_0x375619[_0x54b07e]}));await insertVectors(_0x388118,null,_0x255e21),_0x521d19+=_0x3a0e63[_0x1afcf5(0x1be)];}if(_0x582bca){const _0x184bf7=_0x582bca[_0x1afcf5(0x139)]===0x0?context['chat']['length']:_0x582bca[_0x1afcf5(0x139)],_0x407c38=getCharacterStableId();!settings[_0x1afcf5(0x1a4)][_0x407c38]&&(settings[_0x1afcf5(0x1a4)][_0x407c38]={}),settings['condensationHistory'][_0x407c38][_0x255e21]={'start':_0x582bca[_0x1afcf5(0x1d4)],'end':_0x184bf7,'timestamp':new Date()['toISOString']()},saveSettings(),_0x14f669(_0x1afcf5(0x177)+_0x255e21+_0x1afcf5(0x198)+_0x582bca[_0x1afcf5(0x1d4)]+'-'+_0x184bf7,'info');}_0x14f669(_0x1afcf5(0x1b7)+_0x521d19+_0x1afcf5(0x13b),_0x1afcf5(0x185));const _0x373840=_0x2a8ae9['map'](_0x22156a=>{const _0x5a2a42=_0x1afcf5,_0x335b3f=_0x21a2e0[_0x5a2a42(0x16a)](_0x28a46e=>_0x28a46e===_0x22156a),_0x33c836=_0x335b3f!==-0x1?_0x335b3f+0x1:-0x1,_0x134ae8=_0x22156a[_0x5a2a42(0x1e3)]?'用户':getCharacterName()||'AI';return'['+_0x134ae8+_0x5a2a42(0x191)+_0x33c836+']\x20的消息已成功凝识。';});return{'success':!![],'count':_0x521d19,'messages':_0x373840};}catch(_0x9803dd){return console[_0x1afcf5(0x173)]('[翰林院-核心]\x20processCondensation\x20失败:',_0x9803dd),_0x14f669(_0x1afcf5(0x16f)+_0x9803dd['message'],_0x1afcf5(0x173)),{'success':![],'error':_0x9803dd[_0x1afcf5(0x1e4)]};}}async function rerankResults(_0x5e3145,_0x380a9c,_0x3d27c7){const _0x4402b0=_0x468c3a;let _0x1eb84a=_0x5e3145;if(_0x3d27c7[_0x4402b0(0x1c7)][_0x4402b0(0x1c4)]&&_0x5e3145['length']>0x0){console[_0x4402b0(0x187)](_0x4402b0(0x1ac));try{const _0x451fa3=_0x5e3145['map'](_0x3b6c96=>_0x3b6c96[_0x4402b0(0x153)]),_0x113788=await executeRerank(_0x380a9c,_0x451fa3,_0x3d27c7[_0x4402b0(0x1c7)]),_0x1a2686=_0x5e3145['map']((_0x154c0a,_0x490e28)=>({..._0x154c0a,'original_index':_0x490e28}));_0x1eb84a=_0x1a2686['map'](_0x5ab493=>{const _0x140985=_0x4402b0,_0x44ae68=_0x113788[_0x140985(0x122)]['find'](_0x30e425=>_0x30e425[_0x140985(0x1b3)]===_0x5ab493[_0x140985(0x166)]),_0x5ae1b3=_0x44ae68?_0x44ae68[_0x140985(0x15b)]:0x0;return{..._0x5ab493,'rerank_score':_0x5ae1b3};});if(_0x3d27c7[_0x4402b0(0x1c7)][_0x4402b0(0x204)])showNotification(_0x4402b0(0x1d9),_0x4402b0(0x185));}catch(_0x25c4f2){console[_0x4402b0(0x173)](_0x4402b0(0x13f),_0x25c4f2);if(_0x3d27c7[_0x4402b0(0x1c7)]['notify'])showNotification(_0x4402b0(0x203)+_0x25c4f2[_0x4402b0(0x1e4)],_0x4402b0(0x173));_0x1eb84a[_0x4402b0(0x14f)](_0x4f0649=>_0x4f0649[_0x4402b0(0x12c)]=0x0);}}else _0x1eb84a[_0x4402b0(0x14f)](_0xb14f7b=>_0xb14f7b['rerank_score']=0x0);console['log'](_0x4402b0(0x202));const _0x5af8eb=context[_0x4402b0(0x1c5)][_0x4402b0(0x1be)],_0x15081a=_0x3d27c7[_0x4402b0(0x1c7)]['hybrid_alpha'],_0x197a20=_0x1eb84a['map'](_0x28b16a=>{const _0x4d2bb7=_0x4402b0;let _0x1f65e3=0x1;const _0x339672=_0x28b16a[_0x4d2bb7(0x13e)]||{};switch(_0x339672[_0x4d2bb7(0x16d)]){case _0x4d2bb7(0x189):_0x1f65e3*=1.2;break;case'manual':_0x1f65e3*=1.1;break;case _0x4d2bb7(0x1e2):if(_0x339672[_0x4d2bb7(0x13c)]&&_0x5af8eb>0x0){const _0x4bc5af=_0x339672[_0x4d2bb7(0x13c)]/_0x5af8eb;_0x1f65e3*=0x1+_0x4bc5af;}break;}const _0x5a11fd=_0x28b16a[_0x4d2bb7(0x12c)]*_0x15081a+(_0x28b16a['score']||0x0)*(0x1-_0x15081a),_0x3522a4=_0x5a11fd*_0x1f65e3;return{..._0x28b16a,'final_score':_0x3522a4};});return _0x197a20[_0x4402b0(0x1ad)]((_0x288202,_0x20cf6b)=>(_0x20cf6b[_0x4402b0(0x1b2)]||0x0)-(_0x288202[_0x4402b0(0x1b2)]||0x0)),console[_0x4402b0(0x187)](_0x4402b0(0x155)),_0x197a20['slice'](0x0,_0x3d27c7[_0x4402b0(0x1c7)][_0x4402b0(0x119)]);}async function rearrangeChat(_0x14e40b,_0x43ae59,_0x8739ea,_0x5b5a56){const _0x59af49=_0x468c3a;setExtensionPrompt('HANLINYUAN_RAG','',settings[_0x59af49(0x1f9)]['position'],settings[_0x59af49(0x1f9)]['depth'],![],settings[_0x59af49(0x1f9)][_0x59af49(0x1b1)]);if(_0x5b5a56==='quiet'||!settings[_0x59af49(0x18e)][_0x59af49(0x1c4)])return;const _0x2e1fa6=_0x14e40b['slice'](-settings['advanced']['queryMessageCount']);if(_0x2e1fa6['length']===0x0)return;const _0x259d42=_0x2e1fa6[_0x59af49(0x118)](_0x1f092c=>_0x1f092c[_0x59af49(0x205)])[_0x59af49(0x1a7)]('\x20')['replace'](/<[^>]*>/g,'')[_0x59af49(0x1c9)]();if(!_0x259d42)return;try{const _0x50c556=await queryVectors(_0x259d42);if(_0x50c556['length']===0x0)return;const _0x1c41c5=await rerankResults(_0x50c556,_0x259d42,settings);if(_0x1c41c5['length']===0x0)return;const _0x459ead=_0x1c41c5['map'](_0x1a93d4=>_0x1a93d4['text'])['join']('\x0a\x0a');let _0x10bfca=settings[_0x59af49(0x1f9)]['template']['replace'](_0x59af49(0x133),_0x459ead);_0x10bfca[_0x59af49(0x1c9)]()&&(_0x10bfca=_0x59af49(0x1fb)+_0x10bfca),setExtensionPrompt(_0x59af49(0x190),_0x10bfca,settings['injection'][_0x59af49(0x15a)],settings[_0x59af49(0x1f9)]['depth'],![],settings['injection']['depth_role']);}catch(_0x468cf7){console['error'](_0x59af49(0x1d0),_0x468cf7);if(settings[_0x59af49(0x18e)][_0x59af49(0x204)])showNotification(_0x59af49(0x199)+_0x468cf7['message'],_0x59af49(0x173));}}async function moveKnowledgeBase(_0x538516,_0x296b92){const _0x1855ec=_0x468c3a,_0x15bc55=_0x296b92===_0x1855ec(0x176)?_0x1855ec(0x1f2):_0x1855ec(0x176),_0x481d45=getCharacterStableId();if(!_0x481d45&&_0x15bc55===_0x1855ec(0x1f2)){toastr['error'](_0x1855ec(0x206));return;}const _0x221355=_0x296b92===_0x1855ec(0x176)?getGlobalKnowledgeBases():getLocalKnowledgeBases(),_0x14d962=_0x15bc55===_0x1855ec(0x176)?getGlobalKnowledgeBases():getLocalKnowledgeBases(),_0x84cacf=_0x221355[_0x538516];if(!_0x84cacf){const _0x2c76e4=_0x1855ec(0x1c8)+_0x296b92+'\x27\x20中未找到ID为\x20'+_0x538516+_0x1855ec(0x207);console[_0x1855ec(0x173)]('[翰林院-配置]\x20'+_0x2c76e4),toastr[_0x1855ec(0x173)](_0x1855ec(0x11b));return;}_0x296b92==='local'&&_0x15bc55===_0x1855ec(0x176)&&!_0x84cacf[_0x1855ec(0x1f1)]&&(console[_0x1855ec(0x187)](_0x1855ec(0x201)+_0x538516+_0x1855ec(0x18f)+_0x481d45),_0x84cacf[_0x1855ec(0x1f1)]=_0x481d45);delete _0x221355[_0x538516],_0x14d962[_0x538516]=_0x84cacf,saveSettings();const _0x17410a=_0x1855ec(0x147)+_0x84cacf[_0x1855ec(0x17f)]+'】已成功移动到'+(_0x15bc55===_0x1855ec(0x176)?'全局':'局部')+'。';console[_0x1855ec(0x187)](_0x1855ec(0x138)+_0x17410a);}async function getAllVectorsFromCollection(_0x11f259){const _0xda5df=_0x468c3a,_0x28c362='*',_0x318cd4={'collectionId':_0x11f259,'searchText':_0x28c362,'topK':0x2710,'threshold':0x0,'source':_0xda5df(0x16c),'embeddings':{}},_0x1bdd73=(await getEmbeddings([_0x28c362]))[0x0];_0x318cd4[_0xda5df(0x18d)]={[_0x28c362]:_0x1bdd73};const _0x5c2433=await fetch(_0xda5df(0x148),{'method':_0xda5df(0x15c),'headers':context['getRequestHeaders'](),'body':JSON[_0xda5df(0x1ba)](_0x318cd4)});if(!_0x5c2433['ok']){if(_0x5c2433[_0xda5df(0x1ab)]===0x194)return console[_0xda5df(0x187)](_0xda5df(0x12d)+_0x11f259+_0xda5df(0x11d)),[];const _0x4cfe95=await _0x5c2433[_0xda5df(0x153)]();throw new Error('查询集合\x20'+_0x11f259+_0xda5df(0x132)+_0x4cfe95);}const _0x5602f7=await _0x5c2433[_0xda5df(0x157)]();return _0x5602f7['metadata']||_0x5602f7[_0xda5df(0x122)]||_0x5602f7['data']||[];}