Files
ST-Amily2-Chat-Optimisation/core/api.js
2025-07-21 07:15:39 +08:00

2 lines
13 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.
const _0x2a7266=_0x497b;(function(_0x376f98,_0x33e064){const _0x2f13a5=_0x497b,_0x1610c1=_0x376f98();while(!![]){try{const _0x44eca8=-parseInt(_0x2f13a5(0x122))/0x1+parseInt(_0x2f13a5(0x14c))/0x2+parseInt(_0x2f13a5(0x119))/0x3+-parseInt(_0x2f13a5(0x148))/0x4+-parseInt(_0x2f13a5(0x12c))/0x5*(parseInt(_0x2f13a5(0x17b))/0x6)+-parseInt(_0x2f13a5(0x15e))/0x7*(-parseInt(_0x2f13a5(0x112))/0x8)+parseInt(_0x2f13a5(0x11e))/0x9;if(_0x44eca8===_0x33e064)break;else _0x1610c1['push'](_0x1610c1['shift']());}catch(_0x51bfbb){_0x1610c1['push'](_0x1610c1['shift']());}}}(_0x2f7c,0xd04a7));import{extension_settings,getContext}from'/scripts/extensions.js';import{characters}from'/script.js';import{world_names}from'/scripts/world-info.js';import{extensionName}from'../utils/settings.js';import{extractContentByTag,replaceContentByTag,extractFullTagBlock}from'../utils/tagProcessor.js';import{getCombinedWorldbookContent,findLatestSummaryLore,DEDICATED_LOREBOOK_NAME,getChatIdentifier}from'./lore.js';import{checkAndFixWithAPI as _0x5525a5}from'./summarizer.js';function _0x2f7c(){const _0x1084e2=['模式切换','Authorization','<option>','<无法提取错误正文>','hostname','/v1/chat/completions','[Amily2号-外交部]\x20紧急军情外交任务失败','AMILY2_LOCK_MODEL_FETCHING','ai.google.dev','localeCompare','log','love.qinyan.xyz','手动模式已启用','[Amily2号-使节团]\x20派遣第\x20','/models','isArray','[Amily2号-外交部]\x20API\x20类型:\x20','aiplatform.googleapis.com','#amily2_refresh_models','任务总结','YourUsername','347184rHJlwg','slice','/v1','未知的\x20Google\x20模型列表格式','236744KtBquq','no-store','\x20位使节\x20(Key:\x20...','filter','[Amily2号-使节团]\x20失败详情汇总:\x0a','map','#amily2_model','[Amily2号-使节团]\x20第\x20','\x20个模型\x20(使用第\x20','includes','similarity','\x20个Key)','外交任务失败','/scripts/custom-request.js','omit','[Amily2号-使节团]\x20使用\x20Google\x20API\x20Key:\x20...','html','成功获取\x20','7XfCrXQ','<i\x20class=\x22fas\x20fa-sync-alt\x22></i>\x20刷新模型','Bearer\x20','embed','#amily2_api_url','error','\x20个模型','push','name','application/json','API返回错误:\x20','/v1beta/projects/locations/global/models','json','info','[Amily2号-使节团]\x20上次任务尚未完成本次任务取消。','origin','replace','models','所有使节均未能完成任务。详情请见控制台(F12)。','OpenAI\x20兼容','trim','https://raw.githubusercontent.com/Wx-2025/ST-Amily2-Chat-Optimisation/refs/heads/main/amily2_update_info.json','sort','X-goog-api-key','API\x20Key无效','prop','append','#amily2_api_key','length','6DcKgsT','text','cors','val','Origin','success','任务成功','substring','audio','\x20个\x20Google\x20模型\x20(使用第\x20','generativelanguage.googleapis.com','[Amily2号-使节团]\x20全局错误:','[Amily2号-外交部]\x20已派遣使者前往云端获取最新情报...','ChatCompletionService','\x20位使节任务失败:','\x20失败:\x20','\x20位使节成功带回\x20',')\x20...','未知的模型列表格式','warn','pathname','12345016qUbvtb','无法识别的\x20Google\x20API\x20端点','forceProxyForCustomApi','模型获取失败:\x20','data','stringify','系统错误','789204CXdZbu','陛下,请先赐予\x20API\x20地址与至少一枚\x20API\x20Key。','[Amily2号-外交部]\x20已成功召唤“皇家信使”(ChatCompletionService)。','/v1beta/models','远方服务器响应异常,状态:\x20','4774671qMVieN','[Amily2号-使节团]\x20最终带回\x20','<i\x20class=\x22fas\x20fa-spinner\x20fa-spin\x22></i>\x20加载中','\x20个可用模型','1185918skoqGc','endsWith','status','disabled','location','上次任务尚未完成,请稍后再试。','join','[Amily2号-外交部]\x20使节团尝试使用地址:\x20','配置缺失','search','1646065WkHDfT','所有使节团任务完成,找到\x20','正在轮换使节团获取模型...','toLowerCase','GET','href','message'];_0x2f7c=function(){return _0x1084e2;};return _0x2f7c();}import{isGoogleEndpoint,convertToGoogleRequest,parseGoogleResponse,buildGoogleApiUrl}from'../core/utils/googleAdapter.js';import{intelligentPoll,createGooglePollingTask,progressTracker}from'../core/utils/pollingManager.js';function _0x497b(_0x19ed20,_0x2fd156){const _0x2f7cd2=_0x2f7c();return _0x497b=function(_0x497bcb,_0x30e92f){_0x497bcb=_0x497bcb-0x10c;let _0x5011ef=_0x2f7cd2[_0x497bcb];return _0x5011ef;},_0x497b(_0x19ed20,_0x2fd156);}let ChatCompletionService=undefined;try{const module=await import(_0x2a7266(0x159));ChatCompletionService=module[_0x2a7266(0x188)],console[_0x2a7266(0x13d)](_0x2a7266(0x11b));}catch(_0x4bac10){console[_0x2a7266(0x110)]('[Amily2号-外交部]\x20未能召唤“皇家信使”部分高级功能如Claw代理将受限。请考虑更新SillyTavern版本。',_0x4bac10);}const UPDATE_CHECK_URL=_0x2a7266(0x173);export async function checkForUpdates(){const _0x28a1fd=_0x2a7266;if(!UPDATE_CHECK_URL||UPDATE_CHECK_URL[_0x28a1fd(0x155)](_0x28a1fd(0x147)))return console['log']('[Amily2号-外交部]\x20任务取消陛下尚未配置情报来源URL。'),null;try{console[_0x28a1fd(0x13d)](_0x28a1fd(0x187));const _0x792e59=await fetch(UPDATE_CHECK_URL,{'method':_0x28a1fd(0x130),'cache':_0x28a1fd(0x14d),'mode':_0x28a1fd(0x17d)});if(!_0x792e59['ok'])throw new Error(_0x28a1fd(0x11d)+_0x792e59[_0x28a1fd(0x124)]);const _0x2d4252=await _0x792e59[_0x28a1fd(0x16a)]();return console['log']('[Amily2号-外交部]\x20情报已成功获取并解析。'),_0x2d4252;}catch(_0x590abe){return console[_0x28a1fd(0x163)](_0x28a1fd(0x139),_0x590abe),null;}}let isFetchingModels=![];export async function fetchSupportedModels(){const _0x5c0f65=_0x2a7266,_0x55a516=extension_settings[extensionName];if(_0x55a516&&_0x55a516[_0x5c0f65(0x114)]){console[_0x5c0f65(0x13d)]('[Amily2号-使节团]\x20已启用“皇家密道”模式跳过模型列表获取。请手动输入模型ID并保存。'),toastr[_0x5c0f65(0x16b)]('已启用手动模式请直接输入模型ID。',_0x5c0f65(0x133));const _0x4a8cbf=$(_0x5c0f65(0x152));return _0x4a8cbf['empty']()[_0x5c0f65(0x178)]($('<option>',{'value':'','text':_0x5c0f65(0x13f)})),[];}if(window[_0x5c0f65(0x13a)])return console[_0x5c0f65(0x110)](_0x5c0f65(0x16c)),toastr[_0x5c0f65(0x16b)](_0x5c0f65(0x127),'任务排队中'),[];window[_0x5c0f65(0x13a)]=!![];try{const _0x5f18ea=$(_0x5c0f65(0x162))[_0x5c0f65(0x17e)]()[_0x5c0f65(0x172)](),_0x393049=$(_0x5c0f65(0x179))[_0x5c0f65(0x17e)]()[_0x5c0f65(0x172)](),_0x647485=$(_0x5c0f65(0x145)),_0x1a805d=$(_0x5c0f65(0x152));if(!_0x5f18ea||!_0x393049)return toastr[_0x5c0f65(0x163)](_0x5c0f65(0x11a),_0x5c0f65(0x12a)),[];_0x647485['prop'](_0x5c0f65(0x125),!![])[_0x5c0f65(0x15c)](_0x5c0f65(0x120)),_0x1a805d['empty']()['append']($(_0x5c0f65(0x135),{'value':'','text':_0x5c0f65(0x12e)}));const _0x2762ec=_0x393049['split'](',')[_0x5c0f65(0x151)](_0x53582d=>_0x53582d[_0x5c0f65(0x172)]())[_0x5c0f65(0x14f)](Boolean);if(_0x2762ec['length']===0x0)return toastr[_0x5c0f65(0x163)]('陛下,您提供的\x20API\x20Key\x20无效或为空。',_0x5c0f65(0x176)),_0x1a805d['empty']()['append']($(_0x5c0f65(0x135),{'value':'','text':_0x5c0f65(0x176)})),[];const _0x229b1e=[];let _0x4d43fc=[];for(let _0x14c6b5=0x0;_0x14c6b5<_0x2762ec[_0x5c0f65(0x17a)];_0x14c6b5++){const _0x519993=_0x2762ec[_0x14c6b5];console['log'](_0x5c0f65(0x140)+(_0x14c6b5+0x1)+'/'+_0x2762ec[_0x5c0f65(0x17a)]+_0x5c0f65(0x14e)+_0x519993[_0x5c0f65(0x149)](-0x4)+_0x5c0f65(0x10e));try{let _0x21f752;const _0x189f27=new URL(_0x5f18ea),_0x5e8964=isGoogleEndpoint(_0x5f18ea);if(_0x5e8964){if(_0x189f27[_0x5c0f65(0x137)][_0x5c0f65(0x155)]('generativelanguage.googleapis.com')||_0x189f27[_0x5c0f65(0x137)][_0x5c0f65(0x155)](_0x5c0f65(0x13b)))_0x189f27['pathname']=_0x5c0f65(0x11c);else{if(_0x189f27[_0x5c0f65(0x137)][_0x5c0f65(0x155)](_0x5c0f65(0x144)))_0x189f27[_0x5c0f65(0x111)]=_0x5c0f65(0x169);else throw new Error(_0x5c0f65(0x113));}_0x21f752=_0x189f27['href'];}else{let _0x416d5c=_0x189f27[_0x5c0f65(0x111)];if(_0x416d5c[_0x5c0f65(0x123)](_0x5c0f65(0x138)))_0x416d5c=_0x416d5c[_0x5c0f65(0x182)](0x0,_0x416d5c[_0x5c0f65(0x17a)]-'/chat/completions'[_0x5c0f65(0x17a)]);else{if(_0x416d5c['endsWith']('/v1/'))_0x416d5c=_0x416d5c[_0x5c0f65(0x149)](0x0,-0x1);else!_0x416d5c[_0x5c0f65(0x123)](_0x5c0f65(0x14a))&&(_0x416d5c=_0x416d5c[_0x5c0f65(0x16e)](/\/$/,'')+'/v1');}_0x189f27[_0x5c0f65(0x111)]=_0x416d5c['replace'](/\/$/,'')+_0x5c0f65(0x141),_0x21f752=_0x189f27[_0x5c0f65(0x131)];}console[_0x5c0f65(0x13d)](_0x5c0f65(0x129)+_0x21f752),console[_0x5c0f65(0x13d)](_0x5c0f65(0x143)+(_0x5e8964?'Google':_0x5c0f65(0x171)));const _0x53e074={'Content-Type':_0x5c0f65(0x167),'Accept':_0x5c0f65(0x167)};if(_0x5e8964){console[_0x5c0f65(0x13d)](_0x5c0f65(0x15b)+_0x519993[_0x5c0f65(0x149)](-0x4));if(_0x189f27[_0x5c0f65(0x137)]['includes'](_0x5c0f65(0x185))||_0x189f27[_0x5c0f65(0x137)][_0x5c0f65(0x155)](_0x5c0f65(0x13b)))_0x53e074[_0x5c0f65(0x175)]=_0x519993;else _0x189f27[_0x5c0f65(0x137)][_0x5c0f65(0x155)](_0x5c0f65(0x144))&&(_0x53e074[_0x5c0f65(0x134)]=_0x5c0f65(0x160)+_0x519993);}else _0x53e074['Authorization']=_0x5c0f65(0x160)+_0x519993;_0x21f752['includes'](_0x5c0f65(0x13e))&&(_0x53e074['X-Custom-Proxy']='Amily2-ChatPlugin',_0x53e074[_0x5c0f65(0x17f)]=window[_0x5c0f65(0x126)][_0x5c0f65(0x16d)]);const _0x4fd62d=await fetch(_0x21f752,{'method':_0x5c0f65(0x130),'headers':_0x53e074,'mode':'cors','credentials':_0x5c0f65(0x15a)});if(!_0x4fd62d['ok']){let _0x44822f='';try{const _0x427d72=await _0x4fd62d[_0x5c0f65(0x16a)]();_0x44822f=JSON[_0x5c0f65(0x117)](_0x427d72,null,0x2);}catch{try{_0x44822f=await _0x4fd62d[_0x5c0f65(0x17c)]();}catch(_0x2b56ae){_0x44822f=_0x5c0f65(0x136);}}throw new Error(_0x5c0f65(0x168)+_0x4fd62d[_0x5c0f65(0x124)]+'\x20'+_0x4fd62d['statusText']+'\x0a'+_0x44822f);}const _0x326e37=await _0x4fd62d[_0x5c0f65(0x16a)]();let _0x4ff7c9=[];if(_0x5e8964){if(_0x326e37[_0x5c0f65(0x16f)]&&Array[_0x5c0f65(0x142)](_0x326e37[_0x5c0f65(0x16f)]))_0x4ff7c9=_0x326e37[_0x5c0f65(0x16f)][_0x5c0f65(0x151)](_0x53a65c=>_0x53a65c[_0x5c0f65(0x166)]);else{if(_0x326e37[_0x5c0f65(0x116)]&&Array[_0x5c0f65(0x142)](_0x326e37[_0x5c0f65(0x116)]))_0x4ff7c9=_0x326e37[_0x5c0f65(0x116)]['map'](_0x2c1065=>_0x2c1065[_0x5c0f65(0x166)]||_0x2c1065['id']);else{if(Array[_0x5c0f65(0x142)](_0x326e37))_0x4ff7c9=_0x326e37[_0x5c0f65(0x151)](_0x12edf4=>_0x12edf4['name']);else throw new Error(_0x5c0f65(0x14b));}}}else{if(Array[_0x5c0f65(0x142)](_0x326e37))_0x4ff7c9=_0x326e37[_0x5c0f65(0x151)](_0xf85dbb=>_0xf85dbb['id']||_0xf85dbb);else{if(_0x326e37['data']&&Array[_0x5c0f65(0x142)](_0x326e37[_0x5c0f65(0x116)]))_0x4ff7c9=_0x326e37[_0x5c0f65(0x116)][_0x5c0f65(0x151)](_0x1b6e1e=>_0x1b6e1e['id']);else{if(_0x326e37[_0x5c0f65(0x16f)]&&Array['isArray'](_0x326e37[_0x5c0f65(0x16f)]))_0x4ff7c9=_0x326e37['models']['map'](_0x4971f6=>_0x4971f6['id']);else throw new Error(_0x5c0f65(0x10f));}}}const _0x28eb01=_0x4ff7c9['filter'](_0x10762c=>typeof _0x10762c==='string')[_0x5c0f65(0x14f)](_0x1d938a=>!_0x1d938a[_0x5c0f65(0x12f)]()[_0x5c0f65(0x155)](_0x5c0f65(0x161)))[_0x5c0f65(0x14f)](_0x145240=>!_0x145240[_0x5c0f65(0x12f)]()['includes'](_0x5c0f65(0x12b)))[_0x5c0f65(0x14f)](_0x89d4a7=>!_0x89d4a7[_0x5c0f65(0x12f)]()['includes'](_0x5c0f65(0x156)))[_0x5c0f65(0x14f)](_0x484a0a=>!_0x484a0a[_0x5c0f65(0x12f)]()['includes'](_0x5c0f65(0x183)))['filter'](_0x21ace7=>!_0x21ace7['toLowerCase']()[_0x5c0f65(0x155)]('code'))['filter'](_0x51bf25=>!_0x51bf25[_0x5c0f65(0x12f)]()['includes']('whisper'));_0x28eb01[_0x5c0f65(0x174)]((_0x8e9aa4,_0x47441e)=>_0x8e9aa4[_0x5c0f65(0x13c)](_0x47441e)),console[_0x5c0f65(0x13d)](_0x5c0f65(0x153)+(_0x14c6b5+0x1)+_0x5c0f65(0x10d)+_0x28eb01[_0x5c0f65(0x17a)]+_0x5c0f65(0x164)),_0x4d43fc=[...new Set([..._0x4d43fc,..._0x28eb01])],_0x4d43fc[_0x5c0f65(0x174)]();_0x5e8964?toastr[_0x5c0f65(0x180)]('成功获取\x20'+_0x28eb01[_0x5c0f65(0x17a)]+_0x5c0f65(0x184)+(_0x14c6b5+0x1)+_0x5c0f65(0x157),'任务成功'):toastr[_0x5c0f65(0x180)](_0x5c0f65(0x15d)+_0x28eb01['length']+_0x5c0f65(0x154)+(_0x14c6b5+0x1)+'\x20个Key)',_0x5c0f65(0x181));break;}catch(_0x1b0bbf){const _0x134c1d='Key\x20...'+_0x519993[_0x5c0f65(0x149)](-0x4)+_0x5c0f65(0x10c)+_0x1b0bbf[_0x5c0f65(0x132)];console[_0x5c0f65(0x163)](_0x5c0f65(0x153)+(_0x14c6b5+0x1)+_0x5c0f65(0x189),_0x1b0bbf),_0x229b1e[_0x5c0f65(0x165)](_0x134c1d);}}if(_0x4d43fc[_0x5c0f65(0x17a)]>0x0)return console[_0x5c0f65(0x13d)](_0x5c0f65(0x11f)+_0x4d43fc[_0x5c0f65(0x17a)]+_0x5c0f65(0x121)),toastr['info'](_0x5c0f65(0x12d)+_0x4d43fc[_0x5c0f65(0x17a)]+_0x5c0f65(0x121),_0x5c0f65(0x146)),_0x4d43fc;return toastr['error'](_0x5c0f65(0x170),_0x5c0f65(0x158)),console[_0x5c0f65(0x163)](_0x5c0f65(0x150)+_0x229b1e[_0x5c0f65(0x128)]('\x0a')),[];}catch(_0x59f13e){return console['error'](_0x5c0f65(0x186),_0x59f13e),toastr[_0x5c0f65(0x163)](_0x5c0f65(0x115)+_0x59f13e[_0x5c0f65(0x132)],_0x5c0f65(0x118)),[];}finally{window[_0x5c0f65(0x13a)]=![];const _0x261008=$(_0x5c0f65(0x145));_0x261008[_0x5c0f65(0x177)](_0x5c0f65(0x125),![])['html'](_0x5c0f65(0x15f));}}export async function checkAndFixWithAPI(_0x1870a8,_0x2df502){return await _0x5525a5(_0x1870a8,_0x2df502);}