Files
ST-Amily2-Chat-Optimisation…/core/api.js
2025-07-19 16:50:49 +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.
function _0x3d06(_0xc18443,_0x2bef58){const _0x563587=_0x5635();return _0x3d06=function(_0x3d0625,_0x81eb6e){_0x3d0625=_0x3d0625-0x1da;let _0x11e3a8=_0x563587[_0x3d0625];return _0x11e3a8;},_0x3d06(_0xc18443,_0x2bef58);}const _0x36bbb0=_0x3d06;(function(_0x1dee54,_0x1043f8){const _0x21f8a4=_0x3d06,_0x9935af=_0x1dee54();while(!![]){try{const _0x585b42=-parseInt(_0x21f8a4(0x24b))/0x1+parseInt(_0x21f8a4(0x1f5))/0x2+parseInt(_0x21f8a4(0x212))/0x3*(parseInt(_0x21f8a4(0x244))/0x4)+parseInt(_0x21f8a4(0x1f2))/0x5*(-parseInt(_0x21f8a4(0x206))/0x6)+parseInt(_0x21f8a4(0x24a))/0x7*(-parseInt(_0x21f8a4(0x240))/0x8)+-parseInt(_0x21f8a4(0x247))/0x9*(parseInt(_0x21f8a4(0x253))/0xa)+parseInt(_0x21f8a4(0x232))/0xb;if(_0x585b42===_0x1043f8)break;else _0x9935af['push'](_0x9935af['shift']());}catch(_0x537e5b){_0x9935af['push'](_0x9935af['shift']());}}}(_0x5635,0xa599a));import{extension_settings,getContext}from'/scripts/extensions.js';function _0x5635(){const _0x377a34=[')\x20...','\x20位使节成功带回\x20','replace','text','10ZhQTGh','[Amily2号-使节团]\x20已启用“皇家密道”模式跳过模型列表获取。请手动输入模型ID并保存。','#amily2_model','filter','[Amily2号-使节团]\x20失败详情汇总:\x0a','[Amily2号-使节团]\x20全局错误:','log','join','/v1','Authorization','[Amily2号-外交部]\x20未能召唤“皇家信使”部分高级功能如Claw代理将受限。请考虑更新SillyTavern版本。','[Amily2号-外交部]\x20紧急军情外交任务失败','[Amily2号-外交部]\x20API\x20类型:\x20','location','[Amily2号-外交部]\x20任务取消陛下尚未配置情报来源URL。','split','warn','substring','application/json','系统错误','<i\x20class=\x22fas\x20fa-spinner\x20fa-spin\x22></i>\x20加载中','aiplatform.googleapis.com','error','\x20位使节\x20(Key:\x20...','<option>','/models','[Amily2号-外交部]\x20情报已成功获取并解析。','2776075mNmgaL','ai.google.dev','无法识别的\x20Google\x20API\x20端点','400880PQdeJn','\x20个Key)','success','models','所有使节均未能完成任务。详情请见控制台(F12)。','length','map','toLowerCase','已启用手动模式请直接输入模型ID。','API\x20Key无效','sort','远方服务器响应异常,状态:\x20','#amily2_refresh_models','/scripts/custom-request.js','isArray','外交任务失败','val','6HJKJjF','prop','Bearer\x20','手动模式已启用','/v1beta/models','string','Google','任务总结','\x20个可用模型','模型获取失败:\x20','endsWith','pathname','33wfpAgj','origin','message','成功获取\x20','X-Custom-Proxy','audio','push','/v1/chat/completions','[Amily2号-外交部]\x20使节团尝试使用地址:\x20','模式切换','forceProxyForCustomApi','localeCompare','trim','empty','href','#amily2_api_url','html','所有使节团任务完成,找到\x20','embed','AMILY2_LOCK_MODEL_FETCHING','status','Amily2-ChatPlugin','\x20个模型','no-store','includes','whisper','[Amily2号-使节团]\x20上次任务尚未完成本次任务取消。','<无法提取错误正文>','#amily2_api_key','cors','love.qinyan.xyz','[Amily2号-使节团]\x20第\x20','27674361OotIfw','正在轮换使节团获取模型...','info','上次任务尚未完成,请稍后再试。','hostname','[Amily2号-使节团]\x20派遣第\x20','\x20位使节任务失败:','json','slice','陛下,请先赐予\x20API\x20地址与至少一枚\x20API\x20Key。','陛下,您提供的\x20API\x20Key\x20无效或为空。','name','/v1beta/projects/locations/global/models','[Amily2号-外交部]\x20已成功召唤“皇家信使”(ChatCompletionService)。','64kDssNF','append','data','similarity','40196oIUdjo','disabled','配置缺失','1110492ZYxIQV','GET','[Amily2号-使节团]\x20最终带回\x20','234031rGlKXw','1202465QEvXPR','statusText','[Amily2号-外交部]\x20已派遣使者前往云端获取最新情报...','generativelanguage.googleapis.com'];_0x5635=function(){return _0x377a34;};return _0x5635();}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 _0x424627}from'./summarizer.js';import{isGoogleEndpoint,convertToGoogleRequest,parseGoogleResponse,buildGoogleApiUrl}from'../core/utils/googleAdapter.js';import{intelligentPoll,createGooglePollingTask,progressTracker}from'../core/utils/pollingManager.js';let ChatCompletionService=undefined;try{const module=await import(_0x36bbb0(0x202));ChatCompletionService=module['ChatCompletionService'],console[_0x36bbb0(0x1dd)](_0x36bbb0(0x23f));}catch(_0x22d3d1){console[_0x36bbb0(0x1e7)](_0x36bbb0(0x1e1),_0x22d3d1);}const UPDATE_CHECK_URL='https://raw.githubusercontent.com/Wx-2025/ST-Amily2-Chat-Optimisation/refs/heads/main/amily2_update_info.json';export async function checkForUpdates(){const _0x3333d9=_0x36bbb0;if(!UPDATE_CHECK_URL||UPDATE_CHECK_URL[_0x3333d9(0x22a)]('YourUsername'))return console['log'](_0x3333d9(0x1e5)),null;try{console[_0x3333d9(0x1dd)](_0x3333d9(0x24d));const _0x31f5dc=await fetch(UPDATE_CHECK_URL,{'method':'GET','cache':_0x3333d9(0x229),'mode':_0x3333d9(0x22f)});if(!_0x31f5dc['ok'])throw new Error(_0x3333d9(0x200)+_0x31f5dc[_0x3333d9(0x226)]);const _0x14313e=await _0x31f5dc[_0x3333d9(0x239)]();return console['log'](_0x3333d9(0x1f1)),_0x14313e;}catch(_0x634e99){return console['error'](_0x3333d9(0x1e2),_0x634e99),null;}}let isFetchingModels=![];export async function fetchSupportedModels(){const _0x50021d=_0x36bbb0,_0x5cd29e=extension_settings[extensionName];if(_0x5cd29e&&_0x5cd29e[_0x50021d(0x21c)]){console['log'](_0x50021d(0x254)),toastr[_0x50021d(0x234)](_0x50021d(0x1fd),_0x50021d(0x21b));const _0x3b8c49=$(_0x50021d(0x255));return _0x3b8c49[_0x50021d(0x21f)]()[_0x50021d(0x241)]($(_0x50021d(0x1ef),{'value':'','text':_0x50021d(0x209)})),[];}if(window[_0x50021d(0x225)])return console[_0x50021d(0x1e7)](_0x50021d(0x22c)),toastr[_0x50021d(0x234)](_0x50021d(0x235),'任务排队中'),[];window[_0x50021d(0x225)]=!![];try{const _0x3418fb=$(_0x50021d(0x221))[_0x50021d(0x205)]()[_0x50021d(0x21e)](),_0x1724ad=$(_0x50021d(0x22e))[_0x50021d(0x205)]()['trim'](),_0x4a404c=$('#amily2_refresh_models'),_0x38b52d=$(_0x50021d(0x255));if(!_0x3418fb||!_0x1724ad)return toastr[_0x50021d(0x1ed)](_0x50021d(0x23b),_0x50021d(0x246)),[];_0x4a404c['prop']('disabled',!![])[_0x50021d(0x222)](_0x50021d(0x1eb)),_0x38b52d[_0x50021d(0x21f)]()[_0x50021d(0x241)]($(_0x50021d(0x1ef),{'value':'','text':_0x50021d(0x233)}));const _0x4d418d=_0x1724ad[_0x50021d(0x1e6)](',')[_0x50021d(0x1fb)](_0x955c42=>_0x955c42['trim']())[_0x50021d(0x1da)](Boolean);if(_0x4d418d[_0x50021d(0x1fa)]===0x0)return toastr['error'](_0x50021d(0x23c),_0x50021d(0x1fe)),_0x38b52d[_0x50021d(0x21f)]()[_0x50021d(0x241)]($(_0x50021d(0x1ef),{'value':'','text':_0x50021d(0x1fe)})),[];const _0x68a46e=[];let _0x589fb5=[];for(let _0x4cfe87=0x0;_0x4cfe87<_0x4d418d['length'];_0x4cfe87++){const _0x471447=_0x4d418d[_0x4cfe87];console['log'](_0x50021d(0x237)+(_0x4cfe87+0x1)+'/'+_0x4d418d[_0x50021d(0x1fa)]+_0x50021d(0x1ee)+_0x471447[_0x50021d(0x23a)](-0x4)+_0x50021d(0x24f));try{let _0x49f116;const _0x894339=new URL(_0x3418fb),_0x537def=isGoogleEndpoint(_0x3418fb);if(_0x537def){if(_0x894339['hostname']['includes'](_0x50021d(0x24e))||_0x894339[_0x50021d(0x236)][_0x50021d(0x22a)](_0x50021d(0x1f3)))_0x894339[_0x50021d(0x211)]=_0x50021d(0x20a);else{if(_0x894339[_0x50021d(0x236)]['includes'](_0x50021d(0x1ec)))_0x894339[_0x50021d(0x211)]=_0x50021d(0x23e);else throw new Error(_0x50021d(0x1f4));}_0x49f116=_0x894339[_0x50021d(0x220)];}else{let _0x4cd763=_0x894339[_0x50021d(0x211)];if(_0x4cd763[_0x50021d(0x210)](_0x50021d(0x219)))_0x4cd763=_0x4cd763[_0x50021d(0x1e8)](0x0,_0x4cd763[_0x50021d(0x1fa)]-'/chat/completions'[_0x50021d(0x1fa)]);else{if(_0x4cd763[_0x50021d(0x210)]('/v1/'))_0x4cd763=_0x4cd763[_0x50021d(0x23a)](0x0,-0x1);else!_0x4cd763['endsWith'](_0x50021d(0x1df))&&(_0x4cd763=_0x4cd763[_0x50021d(0x251)](/\/$/,'')+_0x50021d(0x1df));}_0x894339['pathname']=_0x4cd763['replace'](/\/$/,'')+_0x50021d(0x1f0),_0x49f116=_0x894339['href'];}console[_0x50021d(0x1dd)](_0x50021d(0x21a)+_0x49f116),console['log'](_0x50021d(0x1e3)+(_0x537def?_0x50021d(0x20c):'OpenAI\x20兼容'));const _0x5ce02a={'Content-Type':_0x50021d(0x1e9),'Accept':_0x50021d(0x1e9)};if(_0x537def){console[_0x50021d(0x1dd)]('[Amily2号-使节团]\x20使用\x20Google\x20API\x20Key:\x20...'+_0x471447[_0x50021d(0x23a)](-0x4));if(_0x894339[_0x50021d(0x236)][_0x50021d(0x22a)](_0x50021d(0x24e))||_0x894339['hostname'][_0x50021d(0x22a)](_0x50021d(0x1f3)))_0x5ce02a['X-goog-api-key']=_0x471447;else _0x894339[_0x50021d(0x236)][_0x50021d(0x22a)]('aiplatform.googleapis.com')&&(_0x5ce02a['Authorization']=_0x50021d(0x208)+_0x471447);}else _0x5ce02a[_0x50021d(0x1e0)]='Bearer\x20'+_0x471447;_0x49f116[_0x50021d(0x22a)](_0x50021d(0x230))&&(_0x5ce02a[_0x50021d(0x216)]=_0x50021d(0x227),_0x5ce02a['Origin']=window[_0x50021d(0x1e4)][_0x50021d(0x213)]);const _0x158e3e=await fetch(_0x49f116,{'method':_0x50021d(0x248),'headers':_0x5ce02a,'mode':_0x50021d(0x22f),'credentials':'omit'});if(!_0x158e3e['ok']){let _0x37f4a4='';try{const _0x15e5a2=await _0x158e3e['json']();_0x37f4a4=JSON['stringify'](_0x15e5a2,null,0x2);}catch{try{_0x37f4a4=await _0x158e3e[_0x50021d(0x252)]();}catch(_0xe801fb){_0x37f4a4=_0x50021d(0x22d);}}throw new Error('API返回错误:\x20'+_0x158e3e['status']+'\x20'+_0x158e3e[_0x50021d(0x24c)]+'\x0a'+_0x37f4a4);}const _0x56f658=await _0x158e3e[_0x50021d(0x239)]();let _0x4905af=[];if(_0x537def){if(_0x56f658[_0x50021d(0x1f8)]&&Array['isArray'](_0x56f658['models']))_0x4905af=_0x56f658['models']['map'](_0x5cbf2c=>_0x5cbf2c[_0x50021d(0x23d)]);else{if(_0x56f658[_0x50021d(0x242)]&&Array[_0x50021d(0x203)](_0x56f658[_0x50021d(0x242)]))_0x4905af=_0x56f658[_0x50021d(0x242)]['map'](_0x576242=>_0x576242[_0x50021d(0x23d)]||_0x576242['id']);else{if(Array[_0x50021d(0x203)](_0x56f658))_0x4905af=_0x56f658[_0x50021d(0x1fb)](_0x1b898a=>_0x1b898a[_0x50021d(0x23d)]);else throw new Error('未知的\x20Google\x20模型列表格式');}}}else{if(Array[_0x50021d(0x203)](_0x56f658))_0x4905af=_0x56f658[_0x50021d(0x1fb)](_0x889a34=>_0x889a34['id']||_0x889a34);else{if(_0x56f658[_0x50021d(0x242)]&&Array[_0x50021d(0x203)](_0x56f658[_0x50021d(0x242)]))_0x4905af=_0x56f658[_0x50021d(0x242)][_0x50021d(0x1fb)](_0x552f9a=>_0x552f9a['id']);else{if(_0x56f658[_0x50021d(0x1f8)]&&Array[_0x50021d(0x203)](_0x56f658['models']))_0x4905af=_0x56f658['models']['map'](_0x2d8bb8=>_0x2d8bb8['id']);else throw new Error('未知的模型列表格式');}}}const _0x588a6e=_0x4905af[_0x50021d(0x1da)](_0x3d6da2=>typeof _0x3d6da2===_0x50021d(0x20b))[_0x50021d(0x1da)](_0x32262e=>!_0x32262e[_0x50021d(0x1fc)]()[_0x50021d(0x22a)](_0x50021d(0x224)))[_0x50021d(0x1da)](_0x11960e=>!_0x11960e[_0x50021d(0x1fc)]()[_0x50021d(0x22a)]('search'))[_0x50021d(0x1da)](_0x51a5f9=>!_0x51a5f9[_0x50021d(0x1fc)]()[_0x50021d(0x22a)](_0x50021d(0x243)))[_0x50021d(0x1da)](_0x555b9f=>!_0x555b9f[_0x50021d(0x1fc)]()[_0x50021d(0x22a)](_0x50021d(0x217)))[_0x50021d(0x1da)](_0x125848=>!_0x125848[_0x50021d(0x1fc)]()['includes']('code'))[_0x50021d(0x1da)](_0x294d2a=>!_0x294d2a['toLowerCase']()[_0x50021d(0x22a)](_0x50021d(0x22b)));_0x588a6e[_0x50021d(0x1ff)]((_0x7afc99,_0x3ea52d)=>_0x7afc99[_0x50021d(0x21d)](_0x3ea52d)),console[_0x50021d(0x1dd)](_0x50021d(0x231)+(_0x4cfe87+0x1)+_0x50021d(0x250)+_0x588a6e[_0x50021d(0x1fa)]+_0x50021d(0x228)),_0x589fb5=[...new Set([..._0x589fb5,..._0x588a6e])],_0x589fb5[_0x50021d(0x1ff)]();_0x537def?toastr['success'](_0x50021d(0x215)+_0x588a6e[_0x50021d(0x1fa)]+'\x20个\x20Google\x20模型\x20(使用第\x20'+(_0x4cfe87+0x1)+'\x20个Key)','任务成功'):toastr[_0x50021d(0x1f7)](_0x50021d(0x215)+_0x588a6e[_0x50021d(0x1fa)]+'\x20个模型\x20(使用第\x20'+(_0x4cfe87+0x1)+_0x50021d(0x1f6),'任务成功');break;}catch(_0x38ff2a){const _0x2bf923='Key\x20...'+_0x471447[_0x50021d(0x23a)](-0x4)+'\x20失败:\x20'+_0x38ff2a[_0x50021d(0x214)];console[_0x50021d(0x1ed)](_0x50021d(0x231)+(_0x4cfe87+0x1)+_0x50021d(0x238),_0x38ff2a),_0x68a46e[_0x50021d(0x218)](_0x2bf923);}}if(_0x589fb5[_0x50021d(0x1fa)]>0x0)return console['log'](_0x50021d(0x249)+_0x589fb5[_0x50021d(0x1fa)]+'\x20个可用模型'),toastr[_0x50021d(0x234)](_0x50021d(0x223)+_0x589fb5['length']+_0x50021d(0x20e),_0x50021d(0x20d)),_0x589fb5;return toastr[_0x50021d(0x1ed)](_0x50021d(0x1f9),_0x50021d(0x204)),console[_0x50021d(0x1ed)](_0x50021d(0x1db)+_0x68a46e[_0x50021d(0x1de)]('\x0a')),[];}catch(_0x57d61b){return console['error'](_0x50021d(0x1dc),_0x57d61b),toastr[_0x50021d(0x1ed)](_0x50021d(0x20f)+_0x57d61b[_0x50021d(0x214)],_0x50021d(0x1ea)),[];}finally{window['AMILY2_LOCK_MODEL_FETCHING']=![];const _0x11a4d3=$(_0x50021d(0x201));_0x11a4d3[_0x50021d(0x207)](_0x50021d(0x245),![])[_0x50021d(0x222)]('<i\x20class=\x22fas\x20fa-sync-alt\x22></i>\x20刷新模型');}}export async function checkAndFixWithAPI(_0x424b73,_0x45b381){return await _0x424627(_0x424b73,_0x45b381);}