Files
ST-Amily2-Chat-Optimisation/core/api.js
2025-07-16 21:34:52 +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 _0x56fbc2=_0x322b;(function(_0x36f0f3,_0x49f915){const _0x1a45a7=_0x322b,_0x4524b0=_0x36f0f3();while(!![]){try{const _0x3815fd=-parseInt(_0x1a45a7(0x209))/0x1+-parseInt(_0x1a45a7(0x20e))/0x2*(-parseInt(_0x1a45a7(0x1ed))/0x3)+-parseInt(_0x1a45a7(0x213))/0x4+-parseInt(_0x1a45a7(0x200))/0x5*(-parseInt(_0x1a45a7(0x221))/0x6)+parseInt(_0x1a45a7(0x212))/0x7+-parseInt(_0x1a45a7(0x1d2))/0x8*(parseInt(_0x1a45a7(0x207))/0x9)+parseInt(_0x1a45a7(0x20c))/0xa*(-parseInt(_0x1a45a7(0x1fd))/0xb);if(_0x3815fd===_0x49f915)break;else _0x4524b0['push'](_0x4524b0['shift']());}catch(_0x5e8246){_0x4524b0['push'](_0x4524b0['shift']());}}}(_0x237a,0x28ee8));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';function _0x237a(){const _0x2e72af=['pathname','99gLVlRD','AMILY2_LOCK_MODEL_FETCHING','Amily2-ChatPlugin','5935OgSJvq','GET','[Amily2号-使节团]\x20派遣第\x20','status','omit','toLowerCase','外交任务失败','9693WLmGWT','Bearer\x20','249798fWpFLF','手动模式已启用','append','49410gljWYq','Authorization','599234MJIiUZ','OpenAI\x20兼容','[Amily2号-使节团]\x20已启用“皇家密道”模式跳过模型列表获取。请手动输入模型ID并保存。','\x20位使节\x20(Key:\x20...','2027711ZFsVyC','374088LPWDwD','/v1beta/projects/locations/global/models','info','<i\x20class=\x22fas\x20fa-spinner\x20fa-spin\x22></i>\x20加载中','isArray','<option>','trim','[Amily2号-使节团]\x20第\x20','/v1/chat/completions','replace','\x20个模型','love.qinyan.xyz','length','\x20个\x20Google\x20模型\x20(使用第\x20','210BVKRfO','split','已启用手动模式请直接输入模型ID。','/v1/','Key\x20...','\x20个模型\x20(使用第\x20','cors','未知的\x20Google\x20模型列表格式','[Amily2号-外交部]\x20任务取消陛下尚未配置情报来源URL。','localeCompare','陛下,请先赐予\x20API\x20地址与至少一枚\x20API\x20Key。',')\x20...','YourUsername','\x20失败:\x20','X-Custom-Proxy','/scripts/custom-request.js','string','sort','stringify','error','API\x20Key无效','成功获取\x20','similarity','code','[Amily2号-外交部]\x20紧急军情外交任务失败','forceProxyForCustomApi','slice','push','origin','<无法提取错误正文>','#amily2_api_key','无法识别的\x20Google\x20API\x20端点','empty','上次任务尚未完成,请稍后再试。','\x20位使节任务失败:','json','message','API返回错误:\x20','[Amily2号-使节团]\x20上次任务尚未完成本次任务取消。','陛下,您提供的\x20API\x20Key\x20无效或为空。','Origin','whisper','[Amily2号-使节团]\x20使用\x20Google\x20API\x20Key:\x20...','prop','application/json','#amily2_refresh_models','560hEGBnx','任务总结','[Amily2号-使节团]\x20最终带回\x20','disabled','models','href','location','\x20个Key)','Google','远方服务器响应异常,状态:\x20','log','/v1','html','未知的模型列表格式','\x20个可用模型','val','/chat/completions','[Amily2号-外交部]\x20已成功召唤“皇家信使”(ChatCompletionService)。','name','任务排队中','#amily2_model','ai.google.dev','search','warn','embed','endsWith','任务成功','3tUBAjl','[Amily2号-外交部]\x20情报已成功获取并解析。','[Amily2号-外交部]\x20使节团尝试使用地址:\x20','generativelanguage.googleapis.com','<i\x20class=\x22fas\x20fa-sync-alt\x22></i>\x20刷新模型','X-goog-api-key','hostname','map','includes','data','#amily2_api_url','success','filter','系统错误','no-store'];_0x237a=function(){return _0x2e72af;};return _0x237a();}import{extractContentByTag,replaceContentByTag,extractFullTagBlock}from'../utils/tagProcessor.js';import{getCombinedWorldbookContent,findLatestSummaryLore,DEDICATED_LOREBOOK_NAME,getChatIdentifier}from'./lore.js';import{checkAndFixWithAPI as _0x27fec8}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(_0x56fbc2(0x1b3));ChatCompletionService=module['ChatCompletionService'],console[_0x56fbc2(0x1dc)](_0x56fbc2(0x1e3));}catch(_0xe16870){console['warn']('[Amily2号-外交部]\x20未能召唤“皇家信使”部分高级功能如Claw代理将受限。请考虑更新SillyTavern版本。',_0xe16870);}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 _0x845c1d=_0x56fbc2;if(!UPDATE_CHECK_URL||UPDATE_CHECK_URL[_0x845c1d(0x1f5)](_0x845c1d(0x1b0)))return console[_0x845c1d(0x1dc)](_0x845c1d(0x229)),null;try{console['log']('[Amily2号-外交部]\x20已派遣使者前往云端获取最新情报...');const _0x4783e7=await fetch(UPDATE_CHECK_URL,{'method':'GET','cache':_0x845c1d(0x1fb),'mode':_0x845c1d(0x227)});if(!_0x4783e7['ok'])throw new Error(_0x845c1d(0x1db)+_0x4783e7[_0x845c1d(0x203)]);const _0x3d90da=await _0x4783e7[_0x845c1d(0x1c7)]();return console[_0x845c1d(0x1dc)](_0x845c1d(0x1ee)),_0x3d90da;}catch(_0x5cbfef){return console[_0x845c1d(0x1b7)](_0x845c1d(0x1bc),_0x5cbfef),null;}}function _0x322b(_0x5b056a,_0x34e38c){const _0x237aff=_0x237a();return _0x322b=function(_0x322b3d,_0x58a982){_0x322b3d=_0x322b3d-0x1b0;let _0x4ede98=_0x237aff[_0x322b3d];return _0x4ede98;},_0x322b(_0x5b056a,_0x34e38c);}let isFetchingModels=![];export async function fetchSupportedModels(){const _0x224028=_0x56fbc2,_0x2d447f=extension_settings[extensionName];if(_0x2d447f&&_0x2d447f[_0x224028(0x1bd)]){console[_0x224028(0x1dc)](_0x224028(0x210)),toastr[_0x224028(0x215)](_0x224028(0x223),'模式切换');const _0x17ab5b=$(_0x224028(0x1e6));return _0x17ab5b[_0x224028(0x1c4)]()[_0x224028(0x20b)]($('<option>',{'value':'','text':_0x224028(0x20a)})),[];}if(window[_0x224028(0x1fe)])return console[_0x224028(0x1e9)](_0x224028(0x1ca)),toastr[_0x224028(0x215)](_0x224028(0x1c5),_0x224028(0x1e5)),[];window[_0x224028(0x1fe)]=!![];try{const _0x11e51f=$(_0x224028(0x1f7))['val']()[_0x224028(0x219)](),_0x1214c9=$(_0x224028(0x1c2))[_0x224028(0x1e1)]()[_0x224028(0x219)](),_0x1584b2=$('#amily2_refresh_models'),_0x57a95a=$(_0x224028(0x1e6));if(!_0x11e51f||!_0x1214c9)return toastr['error'](_0x224028(0x22b),'配置缺失'),[];_0x1584b2['prop']('disabled',!![])[_0x224028(0x1de)](_0x224028(0x216)),_0x57a95a[_0x224028(0x1c4)]()['append']($('<option>',{'value':'','text':'正在轮换使节团获取模型...'}));const _0x2c8226=_0x1214c9[_0x224028(0x222)](',')[_0x224028(0x1f4)](_0x379572=>_0x379572[_0x224028(0x219)]())[_0x224028(0x1f9)](Boolean);if(_0x2c8226['length']===0x0)return toastr[_0x224028(0x1b7)](_0x224028(0x1cb),_0x224028(0x1b8)),_0x57a95a[_0x224028(0x1c4)]()['append']($(_0x224028(0x218),{'value':'','text':_0x224028(0x1b8)})),[];const _0x30a18d=[];let _0x1c0491=[];for(let _0x48f959=0x0;_0x48f959<_0x2c8226[_0x224028(0x21f)];_0x48f959++){const _0x5dd43e=_0x2c8226[_0x48f959];console[_0x224028(0x1dc)](_0x224028(0x202)+(_0x48f959+0x1)+'/'+_0x2c8226[_0x224028(0x21f)]+_0x224028(0x211)+_0x5dd43e['slice'](-0x4)+_0x224028(0x22c));try{let _0x3db994;const _0xe18623=new URL(_0x11e51f),_0x59a814=isGoogleEndpoint(_0x11e51f);if(_0x59a814){if(_0xe18623[_0x224028(0x1f3)]['includes'](_0x224028(0x1f0))||_0xe18623[_0x224028(0x1f3)][_0x224028(0x1f5)](_0x224028(0x1e7)))_0xe18623['pathname']='/v1beta/models';else{if(_0xe18623[_0x224028(0x1f3)]['includes']('aiplatform.googleapis.com'))_0xe18623[_0x224028(0x1fc)]=_0x224028(0x214);else throw new Error(_0x224028(0x1c3));}_0x3db994=_0xe18623[_0x224028(0x1d7)];}else{let _0x1a2bb0=_0xe18623['pathname'];if(_0x1a2bb0[_0x224028(0x1eb)](_0x224028(0x21b)))_0x1a2bb0=_0x1a2bb0['substring'](0x0,_0x1a2bb0[_0x224028(0x21f)]-_0x224028(0x1e2)[_0x224028(0x21f)]);else{if(_0x1a2bb0['endsWith'](_0x224028(0x224)))_0x1a2bb0=_0x1a2bb0[_0x224028(0x1be)](0x0,-0x1);else!_0x1a2bb0['endsWith']('/v1')&&(_0x1a2bb0=_0x1a2bb0[_0x224028(0x21c)](/\/$/,'')+_0x224028(0x1dd));}_0xe18623[_0x224028(0x1fc)]=_0x1a2bb0[_0x224028(0x21c)](/\/$/,'')+'/models',_0x3db994=_0xe18623[_0x224028(0x1d7)];}console[_0x224028(0x1dc)](_0x224028(0x1ef)+_0x3db994),console[_0x224028(0x1dc)]('[Amily2号-外交部]\x20API\x20类型:\x20'+(_0x59a814?_0x224028(0x1da):_0x224028(0x20f)));const _0x34f21f={'Content-Type':_0x224028(0x1d0),'Accept':_0x224028(0x1d0)};if(_0x59a814){console['log'](_0x224028(0x1ce)+_0x5dd43e['slice'](-0x4));if(_0xe18623[_0x224028(0x1f3)]['includes'](_0x224028(0x1f0))||_0xe18623[_0x224028(0x1f3)][_0x224028(0x1f5)](_0x224028(0x1e7)))_0x34f21f[_0x224028(0x1f2)]=_0x5dd43e;else _0xe18623['hostname']['includes']('aiplatform.googleapis.com')&&(_0x34f21f[_0x224028(0x20d)]=_0x224028(0x208)+_0x5dd43e);}else _0x34f21f[_0x224028(0x20d)]=_0x224028(0x208)+_0x5dd43e;_0x3db994[_0x224028(0x1f5)](_0x224028(0x21e))&&(_0x34f21f[_0x224028(0x1b2)]=_0x224028(0x1ff),_0x34f21f[_0x224028(0x1cc)]=window[_0x224028(0x1d8)][_0x224028(0x1c0)]);const _0x9de858=await fetch(_0x3db994,{'method':_0x224028(0x201),'headers':_0x34f21f,'mode':'cors','credentials':_0x224028(0x204)});if(!_0x9de858['ok']){let _0x9acc34='';try{const _0x29f28b=await _0x9de858[_0x224028(0x1c7)]();_0x9acc34=JSON[_0x224028(0x1b6)](_0x29f28b,null,0x2);}catch{try{_0x9acc34=await _0x9de858['text']();}catch(_0x11b2e8){_0x9acc34=_0x224028(0x1c1);}}throw new Error(_0x224028(0x1c9)+_0x9de858[_0x224028(0x203)]+'\x20'+_0x9de858['statusText']+'\x0a'+_0x9acc34);}const _0x555ea7=await _0x9de858[_0x224028(0x1c7)]();let _0x3df814=[];if(_0x59a814){if(_0x555ea7[_0x224028(0x1d6)]&&Array[_0x224028(0x217)](_0x555ea7[_0x224028(0x1d6)]))_0x3df814=_0x555ea7[_0x224028(0x1d6)][_0x224028(0x1f4)](_0x10eb1c=>_0x10eb1c['name']);else{if(_0x555ea7[_0x224028(0x1f6)]&&Array[_0x224028(0x217)](_0x555ea7[_0x224028(0x1f6)]))_0x3df814=_0x555ea7[_0x224028(0x1f6)]['map'](_0x1ebc5d=>_0x1ebc5d['name']||_0x1ebc5d['id']);else{if(Array[_0x224028(0x217)](_0x555ea7))_0x3df814=_0x555ea7[_0x224028(0x1f4)](_0x335563=>_0x335563[_0x224028(0x1e4)]);else throw new Error(_0x224028(0x228));}}}else{if(Array[_0x224028(0x217)](_0x555ea7))_0x3df814=_0x555ea7['map'](_0x46a789=>_0x46a789['id']||_0x46a789);else{if(_0x555ea7[_0x224028(0x1f6)]&&Array[_0x224028(0x217)](_0x555ea7[_0x224028(0x1f6)]))_0x3df814=_0x555ea7[_0x224028(0x1f6)]['map'](_0x127d31=>_0x127d31['id']);else{if(_0x555ea7[_0x224028(0x1d6)]&&Array[_0x224028(0x217)](_0x555ea7[_0x224028(0x1d6)]))_0x3df814=_0x555ea7['models']['map'](_0x3eef39=>_0x3eef39['id']);else throw new Error(_0x224028(0x1df));}}}const _0x18b7ad=_0x3df814[_0x224028(0x1f9)](_0x17631b=>typeof _0x17631b===_0x224028(0x1b4))['filter'](_0x548295=>!_0x548295['toLowerCase']()[_0x224028(0x1f5)](_0x224028(0x1ea)))[_0x224028(0x1f9)](_0xe75475=>!_0xe75475['toLowerCase']()[_0x224028(0x1f5)](_0x224028(0x1e8)))['filter'](_0xcb237c=>!_0xcb237c[_0x224028(0x205)]()['includes'](_0x224028(0x1ba)))[_0x224028(0x1f9)](_0x5d53fb=>!_0x5d53fb['toLowerCase']()[_0x224028(0x1f5)]('audio'))[_0x224028(0x1f9)](_0x45a896=>!_0x45a896[_0x224028(0x205)]()[_0x224028(0x1f5)](_0x224028(0x1bb)))['filter'](_0x4dc1cd=>!_0x4dc1cd[_0x224028(0x205)]()[_0x224028(0x1f5)](_0x224028(0x1cd)));_0x18b7ad[_0x224028(0x1b5)]((_0x462acf,_0x58bf4e)=>_0x462acf[_0x224028(0x22a)](_0x58bf4e)),console[_0x224028(0x1dc)](_0x224028(0x21a)+(_0x48f959+0x1)+'\x20位使节成功带回\x20'+_0x18b7ad[_0x224028(0x21f)]+_0x224028(0x21d)),_0x1c0491=[...new Set([..._0x1c0491,..._0x18b7ad])],_0x1c0491['sort']();_0x59a814?toastr[_0x224028(0x1f8)](_0x224028(0x1b9)+_0x18b7ad[_0x224028(0x21f)]+_0x224028(0x220)+(_0x48f959+0x1)+_0x224028(0x1d9),_0x224028(0x1ec)):toastr['success'](_0x224028(0x1b9)+_0x18b7ad[_0x224028(0x21f)]+_0x224028(0x226)+(_0x48f959+0x1)+_0x224028(0x1d9),_0x224028(0x1ec));break;}catch(_0x97fc27){const _0x57b93c=_0x224028(0x225)+_0x5dd43e['slice'](-0x4)+_0x224028(0x1b1)+_0x97fc27[_0x224028(0x1c8)];console['error']('[Amily2号-使节团]\x20第\x20'+(_0x48f959+0x1)+_0x224028(0x1c6),_0x97fc27),_0x30a18d[_0x224028(0x1bf)](_0x57b93c);}}if(_0x1c0491[_0x224028(0x21f)]>0x0)return console[_0x224028(0x1dc)](_0x224028(0x1d4)+_0x1c0491[_0x224028(0x21f)]+_0x224028(0x1e0)),toastr[_0x224028(0x215)]('所有使节团任务完成,找到\x20'+_0x1c0491[_0x224028(0x21f)]+'\x20个可用模型',_0x224028(0x1d3)),_0x1c0491;return toastr['error']('所有使节均未能完成任务。详情请见控制台(F12)。',_0x224028(0x206)),console['error']('[Amily2号-使节团]\x20失败详情汇总:\x0a'+_0x30a18d['join']('\x0a')),[];}catch(_0x26476a){return console[_0x224028(0x1b7)]('[Amily2号-使节团]\x20全局错误:',_0x26476a),toastr[_0x224028(0x1b7)]('模型获取失败:\x20'+_0x26476a[_0x224028(0x1c8)],_0x224028(0x1fa)),[];}finally{window[_0x224028(0x1fe)]=![];const _0x4d8b69=$(_0x224028(0x1d1));_0x4d8b69[_0x224028(0x1cf)](_0x224028(0x1d5),![])[_0x224028(0x1de)](_0x224028(0x1f1));}}export async function checkAndFixWithAPI(_0x1c0dda,_0xb50abf){return await _0x27fec8(_0x1c0dda,_0xb50abf);}