Files
ST-Amily2-Chat-Optimisation…/core/api.js
2025-08-05 07:17:10 +08:00

1 line
14 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 _0x333c30=_0x106d;(function(_0x4fc780,_0x1b233f){const _0x78f28e=_0x106d,_0x239aaa=_0x4fc780();while(!![]){try{const _0x3bdd40=parseInt(_0x78f28e(0x9f))/0x1*(-parseInt(_0x78f28e(0xf2))/0x2)+parseInt(_0x78f28e(0xd5))/0x3+-parseInt(_0x78f28e(0xea))/0x4+-parseInt(_0x78f28e(0x89))/0x5+-parseInt(_0x78f28e(0xe2))/0x6+-parseInt(_0x78f28e(0xdf))/0x7*(-parseInt(_0x78f28e(0xf6))/0x8)+-parseInt(_0x78f28e(0xcc))/0x9*(-parseInt(_0x78f28e(0x9a))/0xa);if(_0x3bdd40===_0x1b233f)break;else _0x239aaa['push'](_0x239aaa['shift']());}catch(_0x6893c8){_0x239aaa['push'](_0x239aaa['shift']());}}}(_0x2ea2,0x224ec));import{extension_settings,getContext}from'/scripts/extensions.js';function _0x2ea2(){const _0x2d91df=['Authorization','[Amily2号-使节团]\x20失败详情汇总:\x0a','\x20个\x20Google\x20模型\x20(使用第\x20','replace','\x20个可用模型','info','API\x20Key无效','origin','warn','json','href','trim','isArray','#amily2_refresh_models','成功获取\x20','message','GET','sort','[Amily2号-外交部]\x20API\x20类型:\x20','Key\x20...','X-Custom-Proxy','endsWith','love.qinyan.xyz','未知的\x20Google\x20模型列表格式','prop','error','[Amily2号-使节团]\x20第\x20','text','/v1/','append','location','filter','split','手动模式已启用','success','/v1beta/models','no-store','<i\x20class=\x22fas\x20fa-sync-alt\x22></i>\x20刷新模型','join','[Amily2号-外交部]\x20已派遣使者前往云端获取最新情报...','log','正在轮换使节团获取模型...','aiplatform.googleapis.com','#amily2_model','15129SAlNTX','任务成功','[Amily2号-外交部]\x20使节团尝试使用地址:\x20','已启用手动模式请直接输入模型ID。','toLowerCase','\x20位使节任务失败:','服务器响应异常:\x20','localeCompare','includes','640812VHgqNR','Amily2-ChatPlugin','#amily2_api_url','所有使节均未能完成任务。详情请见控制台(F12)。','Google','[Amily2号-使节团]\x20使用\x20Google\x20API\x20Key:\x20...','\x20位使节\x20(Key:\x20...','length','push','任务排队中','123949sRSlbt','AMILY2_LOCK_MODEL_FETCHING','map','1487304jNIllI','val','data','embed','外交任务失败','disabled','[Amily2号-外交部]\x20情报已成功获取并解析。','任务总结','354096GyMJMQ','/openai','上次任务尚未完成,请稍后再试。','status','陛下,您提供的\x20API\x20Key\x20无效或为空。','API返回错误:\x20','application/json','无法识别的\x20Google\x20API\x20端点','2VaAMiv','html','所有使节团任务完成,找到\x20','[Amily2号-使节团]\x20派遣第\x20','32miGIdj','系统错误','models','OpenAI\x20兼容','omit','\x20个模型','name','/v1','forceProxyForCustomApi','generativelanguage.googleapis.com','hostname','\x20个Key)','string','[Amily2号-使节团]\x20上次任务尚未完成本次任务取消。','slice','/models','statusText','<无法提取错误正文>','<option>','X-goog-api-key','1070040ZGMTXO','pathname','audio','search','模式切换','远方服务器响应异常,状态:\x20','cors','https://raw.githubusercontent.com/Wx-2025/ST-Amily2-Chat-Optimisation/refs/heads/main/amily2_update_info.json','\x20个模型\x20(使用第\x20','\x20位使节成功带回\x20','stringify','ai.google.dev','empty','[Amily2号-内务府]\x20获取留言板内容失败:','[Amily2号-使节团]\x20已启用“皇家密道”模式跳过模型列表获取。请手动输入模型ID并保存。','[Amily2号-外交部]\x20紧急军情外交任务失败','Bearer\x20','2930CShuXU','ChatCompletionService','/v1/chat/completions','YourUsername','陛下,请先赐予\x20API\x20地址与至少一枚\x20API\x20Key。','86025QMoZOA'];_0x2ea2=function(){return _0x2d91df;};return _0x2ea2();}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 _0x407cbd}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('/scripts/custom-request.js');ChatCompletionService=module[_0x333c30(0x9b)],console[_0x333c30(0xc8)]('[Amily2号-外交部]\x20已成功召唤“皇家信使”(ChatCompletionService)。');}catch(_0x56c021){console[_0x333c30(0xa8)]('[Amily2号-外交部]\x20未能召唤“皇家信使”部分高级功能如Claw代理将受限。请考虑更新SillyTavern版本。',_0x56c021);}function _0x106d(_0x1f4b1a,_0x41ac69){const _0x2ea29a=_0x2ea2();return _0x106d=function(_0x106dd4,_0x5ceec3){_0x106dd4=_0x106dd4-0x77;let _0x3cace1=_0x2ea29a[_0x106dd4];return _0x3cace1;},_0x106d(_0x1f4b1a,_0x41ac69);}const UPDATE_CHECK_URL=_0x333c30(0x90),MESSAGE_BOARD_URL='https://raw.githubusercontent.com/Wx-2025/ST-Amily2-Chat-Optimisation/refs/heads/main/amily2_message_board.json';export async function fetchMessageBoardContent(){const _0x40aac8=_0x333c30;if(!MESSAGE_BOARD_URL)return console['log']('[Amily2号-内务府]\x20任务取消陛下尚未配置留言板URL。'),null;try{const _0xae2c73=await fetch(MESSAGE_BOARD_URL,{'cache':'no-store'});if(!_0xae2c73['ok'])throw new Error(_0x40aac8(0xd2)+_0xae2c73[_0x40aac8(0xed)]);const _0x2c3aa5=await _0xae2c73[_0x40aac8(0xa9)]();return _0x2c3aa5;}catch(_0x55b5ec){return console[_0x40aac8(0xb9)](_0x40aac8(0x96),_0x55b5ec),null;}}export async function checkForUpdates(){const _0x13fc7f=_0x333c30;if(!UPDATE_CHECK_URL||UPDATE_CHECK_URL[_0x13fc7f(0xd4)](_0x13fc7f(0x9d)))return console[_0x13fc7f(0xc8)]('[Amily2号-外交部]\x20任务取消陛下尚未配置情报来源URL。'),null;try{console[_0x13fc7f(0xc8)](_0x13fc7f(0xc7));const _0x436768=await fetch(UPDATE_CHECK_URL,{'method':_0x13fc7f(0xb0),'cache':_0x13fc7f(0xc4),'mode':_0x13fc7f(0x8f)});if(!_0x436768['ok'])throw new Error(_0x13fc7f(0x8e)+_0x436768[_0x13fc7f(0xed)]);const _0x416b9c=await _0x436768['json']();return console['log'](_0x13fc7f(0xe8)),_0x416b9c;}catch(_0xf084be){return console[_0x13fc7f(0xb9)](_0x13fc7f(0x98),_0xf084be),null;}}let isFetchingModels=![];export async function fetchSupportedModels(){const _0x25680e=_0x333c30,_0x3e5f21=extension_settings[extensionName];if(_0x3e5f21&&_0x3e5f21[_0x25680e(0x7d)]){console[_0x25680e(0xc8)](_0x25680e(0x97)),toastr[_0x25680e(0xa5)](_0x25680e(0xcf),_0x25680e(0x8d));const _0x37a028=$('#amily2_model');return _0x37a028[_0x25680e(0x95)]()[_0x25680e(0xbd)]($(_0x25680e(0x87),{'value':'','text':_0x25680e(0xc1)})),[];}if(window['AMILY2_LOCK_MODEL_FETCHING'])return console[_0x25680e(0xa8)](_0x25680e(0x82)),toastr[_0x25680e(0xa5)](_0x25680e(0xec),_0x25680e(0xde)),[];window['AMILY2_LOCK_MODEL_FETCHING']=!![];try{const _0x2cd18e=$(_0x25680e(0xd7))[_0x25680e(0xe3)]()['trim'](),_0x572c84=$('#amily2_api_key')['val']()[_0x25680e(0xab)](),_0x1caf48=$(_0x25680e(0xad)),_0x4f70d3=$(_0x25680e(0xcb));if(!_0x2cd18e||!_0x572c84)return toastr[_0x25680e(0xb9)](_0x25680e(0x9e),'配置缺失'),[];_0x1caf48[_0x25680e(0xb8)](_0x25680e(0xe7),!![])['html']('<i\x20class=\x22fas\x20fa-spinner\x20fa-spin\x22></i>\x20加载中'),_0x4f70d3[_0x25680e(0x95)]()[_0x25680e(0xbd)]($('<option>',{'value':'','text':_0x25680e(0xc9)}));const _0x4dd4e6=_0x572c84[_0x25680e(0xc0)](',')['map'](_0x1c5082=>_0x1c5082[_0x25680e(0xab)]())[_0x25680e(0xbf)](Boolean);if(_0x4dd4e6[_0x25680e(0xdc)]===0x0)return toastr[_0x25680e(0xb9)](_0x25680e(0xee),_0x25680e(0xa6)),_0x4f70d3[_0x25680e(0x95)]()[_0x25680e(0xbd)]($('<option>',{'value':'','text':_0x25680e(0xa6)})),[];const _0xcb04d2=[];let _0x15dcc2=[];for(let _0x1a5011=0x0;_0x1a5011<_0x4dd4e6[_0x25680e(0xdc)];_0x1a5011++){const _0x127025=_0x4dd4e6[_0x1a5011];console[_0x25680e(0xc8)](_0x25680e(0xf5)+(_0x1a5011+0x1)+'/'+_0x4dd4e6[_0x25680e(0xdc)]+_0x25680e(0xdb)+_0x127025[_0x25680e(0x83)](-0x4)+')\x20...');try{let _0x16fdd3;const _0x45128b=new URL(_0x2cd18e),_0x22a544=isGoogleEndpoint(_0x2cd18e);if(_0x22a544){if(_0x45128b['hostname'][_0x25680e(0xd4)](_0x25680e(0x7e))||_0x45128b[_0x25680e(0x7f)][_0x25680e(0xd4)](_0x25680e(0x94)))_0x45128b[_0x25680e(0x8a)]=_0x25680e(0xc3);else{if(_0x45128b[_0x25680e(0x7f)][_0x25680e(0xd4)](_0x25680e(0xca)))_0x45128b[_0x25680e(0x8a)]='/v1beta/projects/locations/global/models';else throw new Error(_0x25680e(0xf1));}_0x16fdd3=_0x45128b[_0x25680e(0xaa)];}else{let _0x163dfe=_0x45128b[_0x25680e(0x8a)];if(_0x163dfe[_0x25680e(0xd0)]()[_0x25680e(0xd4)](_0x25680e(0xeb)))_0x163dfe=_0x163dfe[_0x25680e(0xa3)](/\/$/,'');else{if(_0x163dfe[_0x25680e(0xb5)](_0x25680e(0x9c)))_0x163dfe=_0x163dfe['substring'](0x0,_0x163dfe[_0x25680e(0xdc)]-'/chat/completions'[_0x25680e(0xdc)]);else{if(_0x163dfe[_0x25680e(0xb5)](_0x25680e(0xbc)))_0x163dfe=_0x163dfe[_0x25680e(0x83)](0x0,-0x1);else!_0x163dfe[_0x25680e(0xb5)]('/v1')&&(_0x163dfe=_0x163dfe[_0x25680e(0xa3)](/\/$/,'')+_0x25680e(0x7c));}}_0x45128b[_0x25680e(0x8a)]=_0x163dfe[_0x25680e(0xa3)](/\/$/,'')+_0x25680e(0x84),_0x16fdd3=_0x45128b[_0x25680e(0xaa)];}console[_0x25680e(0xc8)](_0x25680e(0xce)+_0x16fdd3),console['log'](_0x25680e(0xb2)+(_0x22a544?_0x25680e(0xd9):_0x25680e(0x78)));const _0x4055c9={'Content-Type':'application/json','Accept':_0x25680e(0xf0)};if(_0x22a544){console['log'](_0x25680e(0xda)+_0x127025[_0x25680e(0x83)](-0x4));if(_0x45128b['hostname'][_0x25680e(0xd4)]('generativelanguage.googleapis.com')||_0x45128b[_0x25680e(0x7f)][_0x25680e(0xd4)](_0x25680e(0x94)))_0x4055c9[_0x25680e(0x88)]=_0x127025;else _0x45128b[_0x25680e(0x7f)][_0x25680e(0xd4)]('aiplatform.googleapis.com')&&(_0x4055c9[_0x25680e(0xa0)]=_0x25680e(0x99)+_0x127025);}else _0x4055c9[_0x25680e(0xa0)]=_0x25680e(0x99)+_0x127025;_0x16fdd3[_0x25680e(0xd4)](_0x25680e(0xb6))&&(_0x4055c9[_0x25680e(0xb4)]=_0x25680e(0xd6),_0x4055c9['Origin']=window[_0x25680e(0xbe)][_0x25680e(0xa7)]);const _0x3585d7=await fetch(_0x16fdd3,{'method':_0x25680e(0xb0),'headers':_0x4055c9,'mode':'cors','credentials':_0x25680e(0x79)});if(!_0x3585d7['ok']){let _0x8ed149='';try{const _0x11148d=await _0x3585d7[_0x25680e(0xa9)]();_0x8ed149=JSON[_0x25680e(0x93)](_0x11148d,null,0x2);}catch{try{_0x8ed149=await _0x3585d7[_0x25680e(0xbb)]();}catch(_0x2a7e27){_0x8ed149=_0x25680e(0x86);}}throw new Error(_0x25680e(0xef)+_0x3585d7[_0x25680e(0xed)]+'\x20'+_0x3585d7[_0x25680e(0x85)]+'\x0a'+_0x8ed149);}const _0x59bfc9=await _0x3585d7[_0x25680e(0xa9)]();let _0x203194=[];if(_0x22a544){if(_0x59bfc9[_0x25680e(0x77)]&&Array[_0x25680e(0xac)](_0x59bfc9[_0x25680e(0x77)]))_0x203194=_0x59bfc9['models'][_0x25680e(0xe1)](_0x9e2089=>_0x9e2089['name']);else{if(_0x59bfc9[_0x25680e(0xe4)]&&Array[_0x25680e(0xac)](_0x59bfc9[_0x25680e(0xe4)]))_0x203194=_0x59bfc9['data'][_0x25680e(0xe1)](_0x3bad29=>_0x3bad29['name']||_0x3bad29['id']);else{if(Array['isArray'](_0x59bfc9))_0x203194=_0x59bfc9[_0x25680e(0xe1)](_0x4264d6=>_0x4264d6[_0x25680e(0x7b)]);else throw new Error(_0x25680e(0xb7));}}}else{if(Array[_0x25680e(0xac)](_0x59bfc9))_0x203194=_0x59bfc9[_0x25680e(0xe1)](_0x5b3114=>_0x5b3114['id']||_0x5b3114);else{if(_0x59bfc9[_0x25680e(0xe4)]&&Array[_0x25680e(0xac)](_0x59bfc9[_0x25680e(0xe4)]))_0x203194=_0x59bfc9[_0x25680e(0xe4)][_0x25680e(0xe1)](_0x3340c0=>_0x3340c0['id']);else{if(_0x59bfc9[_0x25680e(0x77)]&&Array[_0x25680e(0xac)](_0x59bfc9[_0x25680e(0x77)]))_0x203194=_0x59bfc9[_0x25680e(0x77)]['map'](_0x492aad=>_0x492aad['id']);else throw new Error('未知的模型列表格式');}}}const _0x64143=_0x203194['filter'](_0x3b8fad=>typeof _0x3b8fad===_0x25680e(0x81))[_0x25680e(0xbf)](_0x430eeb=>!_0x430eeb[_0x25680e(0xd0)]()['includes'](_0x25680e(0xe5)))['filter'](_0x45b52c=>!_0x45b52c[_0x25680e(0xd0)]()['includes'](_0x25680e(0x8c)))['filter'](_0x5e1258=>!_0x5e1258[_0x25680e(0xd0)]()[_0x25680e(0xd4)]('similarity'))['filter'](_0x3fb8b1=>!_0x3fb8b1[_0x25680e(0xd0)]()[_0x25680e(0xd4)](_0x25680e(0x8b)))[_0x25680e(0xbf)](_0x289cb6=>!_0x289cb6['toLowerCase']()['includes']('code'))[_0x25680e(0xbf)](_0x587429=>!_0x587429[_0x25680e(0xd0)]()[_0x25680e(0xd4)]('whisper'));_0x64143[_0x25680e(0xb1)]((_0x642c2c,_0x56b66b)=>_0x642c2c[_0x25680e(0xd3)](_0x56b66b)),console[_0x25680e(0xc8)](_0x25680e(0xba)+(_0x1a5011+0x1)+_0x25680e(0x92)+_0x64143[_0x25680e(0xdc)]+_0x25680e(0x7a)),_0x15dcc2=[...new Set([..._0x15dcc2,..._0x64143])],_0x15dcc2['sort']();_0x22a544?toastr[_0x25680e(0xc2)](_0x25680e(0xae)+_0x64143[_0x25680e(0xdc)]+_0x25680e(0xa2)+(_0x1a5011+0x1)+_0x25680e(0x80),_0x25680e(0xcd)):toastr[_0x25680e(0xc2)]('成功获取\x20'+_0x64143[_0x25680e(0xdc)]+_0x25680e(0x91)+(_0x1a5011+0x1)+_0x25680e(0x80),_0x25680e(0xcd));break;}catch(_0x32e67d){const _0x512ef8=_0x25680e(0xb3)+_0x127025['slice'](-0x4)+'\x20失败:\x20'+_0x32e67d[_0x25680e(0xaf)];console[_0x25680e(0xb9)](_0x25680e(0xba)+(_0x1a5011+0x1)+_0x25680e(0xd1),_0x32e67d),_0xcb04d2[_0x25680e(0xdd)](_0x512ef8);}}if(_0x15dcc2['length']>0x0)return console[_0x25680e(0xc8)]('[Amily2号-使节团]\x20最终带回\x20'+_0x15dcc2['length']+_0x25680e(0xa4)),toastr[_0x25680e(0xa5)](_0x25680e(0xf4)+_0x15dcc2['length']+_0x25680e(0xa4),_0x25680e(0xe9)),_0x15dcc2;return toastr[_0x25680e(0xb9)](_0x25680e(0xd8),_0x25680e(0xe6)),console[_0x25680e(0xb9)](_0x25680e(0xa1)+_0xcb04d2[_0x25680e(0xc6)]('\x0a')),[];}catch(_0x372a84){return console[_0x25680e(0xb9)]('[Amily2号-使节团]\x20全局错误:',_0x372a84),toastr[_0x25680e(0xb9)]('模型获取失败:\x20'+_0x372a84['message'],_0x25680e(0xf7)),[];}finally{window[_0x25680e(0xe0)]=![];const _0x308e38=$('#amily2_refresh_models');_0x308e38[_0x25680e(0xb8)](_0x25680e(0xe7),![])[_0x25680e(0xf3)](_0x25680e(0xc5));}}export async function checkAndFixWithAPI(_0x28f0b2,_0x4789c6){return await _0x407cbd(_0x28f0b2,_0x4789c6);}