import{d as m,l as p}from"./index-809c946b.js";const _=m({id:"common",state:()=>({eventSource:null,log_cache:[],sse_connected:!1,log_cache_max_len:1e3,startTime:-1,tutorial_map:{qq_official_webhook:"https://astrbot.app/deploy/platform/qqofficial/webhook.html",qq_official:"https://astrbot.app/deploy/platform/qqofficial/websockets.html",aiocqhttp:"https://astrbot.app/deploy/platform/aiocqhttp/napcat.html",wecom:"https://astrbot.app/deploy/platform/wecom.html",gewechat:"https://astrbot.app/deploy/platform/gewechat.html",lark:"https://astrbot.app/deploy/platform/lark.html",telegram:"https://astrbot.app/deploy/platform/telegram.html",dingtalk:"https://astrbot.app/deploy/platform/dingtalk.html"},pluginMarketData:[]}),actions:{createEventSource(){if(this.eventSource)return;const a=new AbortController,{signal:t}=a,r={"Content-Type":"multipart/form-data",Authorization:"Bearer "+localStorage.getItem("token")};fetch("/api/live-log",{method:"GET",headers:r,signal:t,cache:"no-cache"}).then(o=>{if(!o.ok)throw new Error(`SSE connection failed: ${o.status}`);console.log("SSE stream opened"),this.sse_connected=!0;const l=o.body.getReader(),i=new TextDecoder,n=({done:s,value:e})=>{if(s){console.log("SSE stream closed"),setTimeout(()=>{this.eventSource=null,this.createEventSource()},2e3);return}return i.decode(e).split(`

`).forEach(d=>{if(d.startsWith("data:")){const h=d.substring(5).trim();let c={};try{c=JSON.parse(h)}catch{console.error("Invalid JSON:",h),c={type:"log",data:h,level:"INFO",time:new Date().toISOString()}}c.type==="log"&&(this.log_cache.push(c),this.log_cache.length>this.log_cache_max_len&&this.log_cache.shift())}}),l.read().then(n)};l.read().then(n)}).catch(o=>{console.error("SSE error:",o),this.log_cache.push("SSE Connection failed, retrying in 5 seconds..."),setTimeout(()=>{this.eventSource=null,this.createEventSource()},1e3)}),this.eventSource=a},closeEventSourcet(){this.eventSource&&(this.eventSource.abort(),this.eventSource=null)},getLogCache(){return this.log_cache},getStartTime(){if(this.startTime!==-1)return this.startTime;p.get("/api/stat/start-time").then(a=>{this.startTime=a.data.data.start_time})},getTutorialLink(a){return this.tutorial_map[a]},async getPluginCollections(a=!1){return!a&&this.pluginMarketData.length>0?Promise.resolve(this.pluginMarketData):p.get("/api/plugin/market_list").then(t=>{var o,l,i,n,s;let r=[];for(let e in t.data.data)r.push({name:e,desc:t.data.data[e].desc,author:t.data.data[e].author,repo:t.data.data[e].repo,installed:!1,version:(o=t.data.data[e])!=null&&o.version?t.data.data[e].version:"未知",social_link:(l=t.data.data[e])==null?void 0:l.social_link,tags:(i=t.data.data[e])!=null&&i.tags?t.data.data[e].tags:[],logo:(n=t.data.data[e])!=null&&n.logo?t.data.data[e].logo:"",pinned:(s=t.data.data[e])!=null&&s.pinned?t.data.data[e].pinned:!1});return this.pluginMarketData=r,r}).catch(t=>(this.toast("获取插件市场数据失败: "+t,"error"),Promise.reject(t)))}}});export{_ as u};
