import{n as e}from"./rolldown-runtime-DkW27tQK.js";import{Ei as t,Oi as n,er as r,wn as i}from"./control-ui-foundation-CeFMP1Ly.js";import{S as a,ei as o,fl as s,fs as c,ml as ee,ms as te,ni as l,ti as ne,w as re}from"./control-ui-core-pH6nakZ-.js";import{G as u,J as d,W as f,Z as p,rt as m}from"./lit-runtime-BEtFSUCp.js";import{Cn as h,ln as g,m as _,p as v}from"./control-ui-core-DGVkFhVB.js";import{Dt as ie,Ht as ae,Ot as oe,qt as y}from"./control-ui-core-Br5DB0Zj.js";import{F as se,I as ce,In as le,L as ue,Ln as b,z as x}from"./control-ui-boot-wpHCbPL9.js";import{Ns as S,Ps as C,as as w,bn as T,es as E,gn as D,is as O,ns as k,on as A,rs as j,ts as M,un as N,xn as P}from"./control-ui-boot-Cdpgu1Gm.js";import{i as F,r as I}from"./control-ui-boot-DdPrPuw0.js";import{n as L,t as R}from"./settings-workspace-Bo4nqRhI.js";import{n as de,t as z}from"./stream-auto-follow-controller-DQpG-yCd.js";import{n as B,t as V}from"./gateway-page-controller-bzyUg03c.js";function H(e){let t=[];for(let n of Object.keys(e)){if(!/^\d+$/.test(n))continue;let r=e[n];typeof r==`string`?t.push(r):r!=null&&t.push(JSON.stringify(r))}return t.join(` `)}function U(e){if(typeof e!=`string`)return{};try{let t=JSON.parse(e);return{subsystem:typeof t.subsystem==`string`?t.subsystem:void 0,module:typeof t.module==`string`?t.module:void 0,plugin:typeof t.plugin==`string`?t.plugin:void 0}}catch{return{}}}function fe(e,t){let n=U(t?.name),r=U(e[0]);return{subsystem:n.subsystem??r.subsystem,module:n.module??r.module,plugin:n.plugin??r.plugin}}function pe(e){try{let t=JSON.parse(e);if(!i(t))return null;let r=i(t._meta)?t._meta:void 0,a=fe(t,r),o=typeof r?.logLevelName==`string`?r.logLevelName:void 0;return{time:typeof t.time==`string`?t.time:typeof r?.date==`string`?r.date:void 0,level:n(o),subsystem:a.subsystem,module:a.module,plugin:a.plugin,message:typeof t.message==`string`?t.message:H(t),raw:e}}catch{return null}}function W(){return(W=e((()=>{})))()}function me(e){if(typeof e!=`string`)return null;let n=t(e);return q.has(n)?n:null}function G(e){let t=pe(e);if(!t)return{raw:e,message:F(e)};let n=t.subsystem??t.module;return{raw:t.raw,time:t.time??null,level:me(t.level),subsystem:n?F(n):null,message:F(t.message)}}var K,q;function J(){return(J=e((()=>{I(),W(),K={trace:!0,debug:!0,info:!0,warn:!0,error:!0,fatal:!0},q=new Set([`trace`,`debug`,`info`,`warn`,`error`,`fatal`])})))()}function he(e){if(!e)return``;let t=new Date(e);return Number.isNaN(t.getTime())?e:a(t.getTime(),void 0,e)}function ge(e,n){return!n||t([e.message,e.subsystem,e.raw].filter(Boolean).join(` `)).includes(n)}function _e(e){let n=t(e.filterText),r=Y.some(t=>!e.levelFilters[t]),i=e.entries.filter(t=>t.level&&!e.levelFilters[t.level]?!1:ge(t,n)),a=n||r?`filtered`:`visible`,o=y(`gatewayLogs.exportLabels.${a}`),s=e.status.hasLoaded?i.length===0?N(y(`gatewayLogs.empty`)):i.map(e=>d`
            <div class="log-row">
              <div class="log-time mono">${he(e.time)}</div>
              <div class="log-level ${e.level??``}">${e.level??``}</div>
              <div class="log-subsystem mono">${e.subsystem??``}</div>
              <div class="log-message mono">${e.message??e.raw}</div>
            </div>
          `):e.loading?oe():u;return d`
    <div class="settings-section__header">
      <h2 class="settings-section__heading">${y(`gatewayLogs.title`)}</h2>
      <div class="settings-section__actions">
        <button class="btn" ?disabled=${e.refreshDisabled} @click=${e.onRefresh}>
          ${e.loading?y(`common.loading`):y(`common.refresh`)}
        </button>
        <button
          class="btn"
          ?disabled=${i.length===0}
          @click=${()=>e.onExport(i.map(e=>e.raw),a)}
        >
          ${y(`gatewayLogs.exportButton`,{label:o})}
        </button>
      </div>
    </div>
    <p class="settings-section__desc">${y(`gatewayLogs.subtitle`)}</p>
    ${w({status:e.status,onRetry:e.onRefresh,retryDisabled:e.refreshDisabled,className:`logs-refresh-status`})}
    <div class="settings-group logs-card">
      ${D({title:y(`gatewayLogs.filter`),description:e.file?y(`gatewayLogs.file`,{file:e.file}):void 0,control:d`
          <input
            class="settings-input"
            aria-label=${y(`gatewayLogs.filter`)}
            .value=${e.filterText}
            @input=${t=>e.onFilterTextChange(t.target.value)}
            placeholder=${y(`gatewayLogs.searchPlaceholder`)}
          />
        `})}
      <div class="settings-row">
        <div class="chip-row">
          ${Y.map(t=>d`
              <label class="chip log-chip ${t}">
                <input
                  type="checkbox"
                  .checked=${e.levelFilters[t]}
                  @change=${n=>e.onLevelToggle(t,n.target.checked)}
                />
                <span>${t}</span>
              </label>
            `)}
        </div>
        <div class="settings-row__control">
          ${P({checked:e.autoFollow,ariaLabel:y(`gatewayLogs.autoFollow`),onChange:t=>e.onToggleAutoFollow(t)})}
          <span class="settings-row__value">${y(`gatewayLogs.autoFollow`)}</span>
        </div>
      </div>
      ${e.truncated?d`
            <div class="settings-row">
              ${T({kind:`warn`,label:y(`gatewayLogs.truncated`)})}
            </div>
          `:u}
      <div class="log-stream" @scroll=${e.onScroll}>${s}</div>
    </div>
  `}var Y;function X(){return(X=e((()=>{f(),ie(),O(),A(),ae(),re(),Y=[`trace`,`debug`,`info`,`warn`,`error`,`fatal`]})))()}var Z,Q;function $(){return($=e((()=>{b(),se(),f(),p(),g(),_(),O(),R(),te(),ne(),B(),ee(),C(),de(),J(),X(),Z=2e3,Q=class extends s{constructor(...e){super(...e),this.logsStatus=k(),this.logsFile=null,this.logsEntries=[],this.logsFilterText=``,this.logsLevelFilters={...K},this.logsAutoFollow=!0,this.logsTruncated=!1,this.logsCursor=null,this.logsLimit=500,this.logsMaxBytes=25e4,this.polling=new S(this,Z,()=>{this.loadLogs({quiet:!0})},!1),this.contentScrollFrame=null,this.logsTaskQuiet=!1,this.logsTask=new ce(this,{autoRun:!1,args:()=>this.logsTaskArgs(),task:async([e,t,n,r,i,a],{signal:o})=>{if(!e||!t)return ue;try{let e=e=>t.request(`logs.tail`,{cursor:e,limit:this.logsLimit,maxBytes:this.logsMaxBytes},{signal:o}),s=await e(i?void 0:n??void 0),c=!i&&r!==null&&s.file!==void 0&&s.file!==r;return c&&(s=await e()),{ok:!0,payload:s,cursor:n,reset:i||c,quiet:a}}catch(e){return{ok:!1,error:e,quiet:a}}},onComplete:e=>{if(!e.ok){l(e.error)?(this.logsEntries=[],this.logsStatus=j(k(),o(`logs`))):this.logsStatus=j(this.logsStatus,c(e.error));return}let t=(Array.isArray(e.payload.lines)?e.payload.lines.filter(e=>typeof e==`string`):[]).map(G),n=e.reset||e.payload.reset||e.cursor==null;this.logsEntries=n?t:[...this.logsEntries,...t].slice(-2e3),this.logsCursor=typeof e.payload.cursor==`number`?e.payload.cursor:this.logsCursor,this.logsFile=typeof e.payload.file==`string`?e.payload.file:this.logsFile,this.logsTruncated=!!e.payload.truncated,this.logsStatus=M()}}),this.gateway=new V(this,{getGateway:()=>this.context?.gateway,onIdentityChange:()=>{this.logsStatus=k(),this.logsFile=null,this.logsEntries=[],this.logsTruncated=!1,this.logsCursor=null,this.streamFollow.atBottom=!0},invalidateRequests:()=>{this.logsTaskQuiet=!1,this.logsTask.run([null,null,null,null,!1,!1])},onSnapshot:e=>{if(this.syncPolling(),e.becameConnected&&this.logsFile!==null){this.loadLogs({reset:!0,quiet:!0});return}this.ensureInitialLogs()}}),this.streamFollow=new z(this,{selector:`.log-stream`,isEnabled:()=>this.logsAutoFollow,captureCurrent:()=>{let e=this.gateway.gateway,t=this.gateway.epoch;return()=>this.isConnected&&this.gateway.connected&&e!==null&&this.gateway.gateway===e&&this.context.gateway===e&&this.gateway.epoch===t}})}logsTaskArgs(e){return[this.gateway.connected?this.gateway.gateway:null,this.gateway.connected?this.gateway.client:null,e?.reset?null:this.logsCursor,this.logsFile,e?.reset===!0,e?.quiet===!0]}firstUpdated(){this.resetContentScroll(),this.contentScrollFrame=requestAnimationFrame(()=>{this.contentScrollFrame=null,this.resetContentScroll()})}updated(e){let t=this.logsAutoFollow&&e.has(`logsAutoFollow`);(t||this.logsAutoFollow&&this.streamFollow.atBottom&&e.has(`logsEntries`))&&this.streamFollow.schedule(t)}disconnectedCallback(){this.logsTaskQuiet=!1,this.logsTask.run([null,null,null,null,!1,!1]),this.contentScrollFrame!==null&&(cancelAnimationFrame(this.contentScrollFrame),this.contentScrollFrame=null),super.disconnectedCallback()}resetContentScroll(){let e=this.closest(`.content`);e&&(e.scrollTop=0,e.scrollLeft=0)}syncPolling(){if(!this.gateway.connected||!this.gateway.client){this.polling.stop();return}this.polling.start()}ensureInitialLogs(){!this.gateway.connected||!this.gateway.client||this.logsEntries.length>0||this.loadLogs({reset:!0}).then(e=>{e&&this.streamFollow.schedule(!0)})}async loadLogs(e){let t=e?.quiet===!0,n=this.gateway.gateway;return!n||!this.gateway.client||!this.gateway.connected||this.context.gateway!==n||this.logsTask.status===x.PENDING&&e?.reset!==!0?!1:(this.logsTaskQuiet=t,this.logsStatus=E(this.logsStatus,{clearError:!t}),await this.logsTask.run(this.logsTaskArgs(e)),this.logsTask.status===x.COMPLETE)}exportLogs(e,t){if(e.length===0)return;let n=new Blob([`${e.join(`
`)}\n`],{type:`text/plain`}),r=URL.createObjectURL(n),i=document.createElement(`a`),a=new Date().toISOString().slice(0,19).replace(/[:T]/g,`-`);i.href=r,i.download=`openclaw-logs-${t}-${a}.log`,i.click(),URL.revokeObjectURL(r)}render(){let e=_e({loading:this.logsTask.status===x.PENDING&&!this.logsTaskQuiet,refreshDisabled:!this.gateway.connected||this.logsTask.status===x.PENDING,status:this.logsStatus,file:this.logsFile,entries:this.logsEntries,filterText:this.logsFilterText,levelFilters:this.logsLevelFilters,autoFollow:this.logsAutoFollow,truncated:this.logsTruncated,onFilterTextChange:e=>this.logsFilterText=e,onLevelToggle:(e,t)=>{this.logsLevelFilters={...this.logsLevelFilters,[e]:t}},onToggleAutoFollow:e=>this.logsAutoFollow=e,onRefresh:()=>void this.loadLogs({reset:!0}).then(e=>{e&&this.streamFollow.schedule(!0)}),onExport:(e,t)=>this.exportLogs(e,t),onScroll:e=>this.streamFollow.handleScroll(e)});return d`
      <section class="content-header">
        <div>
          <div class="page-title">${h(`logs`)}</div>
        </div>
      </section>
      ${L(e,{fillHeight:!0})}
    `}},r([le({context:v,subscribe:!0})],Q.prototype,`context`,void 0),r([m()],Q.prototype,`logsStatus`,void 0),r([m()],Q.prototype,`logsFile`,void 0),r([m()],Q.prototype,`logsEntries`,void 0),r([m()],Q.prototype,`logsFilterText`,void 0),r([m()],Q.prototype,`logsLevelFilters`,void 0),r([m()],Q.prototype,`logsAutoFollow`,void 0),r([m()],Q.prototype,`logsTruncated`,void 0),customElements.get(`openclaw-logs-page`)||customElements.define(`openclaw-logs-page`,Q)})))()}$();
//# sourceMappingURL=logs-page-CvlVPhNs.js.map