11import { truncateUtf16Safe } from "@openclaw/normalization-core/utf16-slice" ;
2- import { html , type TemplateResult } from "lit" ;
2+ import { html , nothing , type TemplateResult } from "lit" ;
33import type { GatewayBrowserClient } from "../../api/gateway.ts" ;
44import type { ApplicationGateway } from "../../app/gateway.ts" ;
55import { t } from "../../i18n/index.ts" ;
@@ -13,6 +13,8 @@ import {
1313 type CommandLaneDiagnostics ,
1414} from "../../lib/gateway-diagnostics.ts" ;
1515import { renderCommandLaneRows } from "./lane-table.ts" ;
16+ import "./sparkline-tile.ts" ;
17+ import type { SparklineSample } from "./sparkline-tile.ts" ;
1618
1719type DebugOverlaySectionContext = {
1820 client : GatewayBrowserClient ;
@@ -23,7 +25,7 @@ type TypedDebugOverlaySectionDescriptor<T> = {
2325 id : string ;
2426 titleKey : string ;
2527 load : ( context : DebugOverlaySectionContext , signal : AbortSignal ) => Promise < T > ;
26- render : ( value : T ) => TemplateResult ;
28+ render : ( value : T , statusHistory : readonly DebugOverlayStatusSample [ ] ) => TemplateResult ;
2729} ;
2830
2931export type DebugOverlaySectionDescriptor = TypedDebugOverlaySectionDescriptor < unknown > ;
@@ -33,24 +35,36 @@ function defineDebugOverlaySection<T>(
3335) : DebugOverlaySectionDescriptor {
3436 return {
3537 ...descriptor ,
36- render : ( value ) => {
38+ render : ( value , statusHistory ) => {
3739 // SAFETY: This closure keeps each descriptor's load result paired with its own renderer.
38- return descriptor . render ( value as T ) ;
40+ return descriptor . render ( value as T , statusHistory ) ;
3941 } ,
4042 } ;
4143}
4244
4345type EventLoopSnapshot = {
4446 utilization ?: number ;
47+ cpuCoreRatio ?: number ;
4548 delayP99Ms ?: number ;
4649 delayMaxMs ?: number ;
50+ reasons ?: string [ ] ;
4751} ;
4852
49- type StatusSectionValue = {
53+ export type DebugOverlayStatusSnapshot = {
5054 eventLoop ?: EventLoopSnapshot ;
55+ processMemory ?: {
56+ rssBytes : number ;
57+ heapUsedBytes : number ;
58+ heapTotalBytes : number ;
59+ } ;
5160 uptimeMs ?: number ;
5261} ;
5362
63+ export type DebugOverlayStatusSample = {
64+ at : number ;
65+ status : DebugOverlayStatusSnapshot ;
66+ } ;
67+
5468type ActiveSession = {
5569 key ?: string ;
5670 sessionId ?: string ;
@@ -76,41 +90,86 @@ function renderLanes(diagnostics: CommandLaneDiagnostics): TemplateResult {
7690 ` ;
7791}
7892
79- function renderStatus ( status : StatusSectionValue ) : TemplateResult {
93+ function collectSamples (
94+ history : readonly DebugOverlayStatusSample [ ] ,
95+ read : ( status : DebugOverlayStatusSnapshot ) => number | undefined ,
96+ ) : SparklineSample [ ] {
97+ const samples : SparklineSample [ ] = [ ] ;
98+ for ( const entry of history ) {
99+ const value = read ( entry . status ) ;
100+ if ( typeof value === "number" && Number . isFinite ( value ) ) {
101+ samples . push ( { value, at : entry . at } ) ;
102+ }
103+ }
104+ return samples ;
105+ }
106+
107+ function formatPercent ( value : number ) : string {
108+ return `${ Math . round ( value * 100 ) } %` ;
109+ }
110+
111+ function formatMegabytes ( bytes : number ) : string {
112+ return t ( "debug.overlay.memoryMb" , { value : String ( Math . round ( bytes / 1_048_576 ) ) } ) ;
113+ }
114+
115+ function formatDelayMs ( value : number ) : string {
116+ return formatDurationCompact ( value ) ?? t ( "common.na" ) ;
117+ }
118+
119+ function renderStatus (
120+ status : DebugOverlayStatusSnapshot ,
121+ history : readonly DebugOverlayStatusSample [ ] ,
122+ ) : TemplateResult {
80123 const eventLoop = status . eventLoop ;
81- const utilization =
124+ const reasons = eventLoop ?. reasons ?? [ ] ;
125+ const cpuDegraded = reasons . includes ( "cpu" ) || reasons . includes ( "event_loop_utilization" ) ;
126+ const delayDegraded = reasons . includes ( "event_loop_delay" ) ;
127+ const loopSub =
82128 typeof eventLoop ?. utilization === "number"
83- ? ` ${ Math . round ( eventLoop . utilization * 100 ) } %`
84- : t ( "common.na" ) ;
85- const delay =
86- typeof eventLoop ?. delayP99Ms === "number"
87- ? formatDurationCompact ( eventLoop . delayP99Ms )
88- : t ( "common.na" ) ;
89- const maxDelay =
129+ ? t ( "debug.overlay.loopShort" , { value : formatPercent ( eventLoop . utilization ) } )
130+ : "" ;
131+ const heapSub =
132+ typeof status . processMemory ?. heapUsedBytes === "number"
133+ ? t ( "debug.overlay.heapShort" , { value : formatMegabytes ( status . processMemory . heapUsedBytes ) } )
134+ : "" ;
135+ const maxSub =
90136 typeof eventLoop ?. delayMaxMs === "number"
91- ? formatDurationCompact ( eventLoop . delayMaxMs )
92- : t ( "common.na" ) ;
137+ ? t ( "debug.overlay.maxShort" , { value : formatDelayMs ( eventLoop . delayMaxMs ) } )
138+ : "" ;
93139 return html `
94- < dl class ="debug-overlay__metrics ">
95- < div >
96- < dt > ${ t ( "debug.overlay.utilization" ) } </ dt >
97- < dd class ="mono "> ${ utilization } </ dd >
98- </ div >
99- < div >
100- < dt > ${ t ( "debug.overlay.delayP99" ) } </ dt >
101- < dd class ="mono "> ${ delay } </ dd >
102- </ div >
103- < div >
104- < dt > ${ t ( "debug.overlay.delayMax" ) } </ dt >
105- < dd class ="mono "> ${ maxDelay } </ dd >
106- </ div >
107- ${ typeof status . uptimeMs === "number"
108- ? html `< div >
109- < dt > ${ t ( "debug.overlay.uptime" ) } </ dt >
110- < dd class ="mono "> ${ formatDurationHuman ( status . uptimeMs ) } </ dd >
111- </ div > `
112- : "" }
113- </ dl >
140+ < div class ="debug-overlay__vitals ">
141+ < openclaw-debug-sparkline
142+ class ="debug-overlay__vital debug-overlay__vital--cpu "
143+ data-degraded =${ cpuDegraded ? "" : nothing }
144+ .label =${ t ( "debug.overlay.cpu" ) }
145+ .sub=${ loopSub }
146+ .samples=${ collectSamples ( history , ( sample ) => sample . eventLoop ?. cpuCoreRatio ) }
147+ .format=${ formatPercent }
148+ .floorMax=${ 1 }
149+ > </ openclaw-debug-sparkline >
150+ < openclaw-debug-sparkline
151+ class ="debug-overlay__vital debug-overlay__vital--memory "
152+ .label =${ t ( "debug.overlay.memory" ) }
153+ .sub =${ heapSub }
154+ .samples=${ collectSamples ( history , ( sample ) => sample . processMemory ?. rssBytes ) }
155+ .format=${ formatMegabytes }
156+ autorange
157+ > </ openclaw-debug-sparkline >
158+ < openclaw-debug-sparkline
159+ class ="debug-overlay__vital debug-overlay__vital--delay "
160+ data-degraded =${ delayDegraded ? "" : nothing }
161+ .label =${ t ( "debug.overlay.delayP99" ) }
162+ .sub=${ maxSub }
163+ .samples=${ collectSamples ( history , ( sample ) => sample . eventLoop ?. delayP99Ms ) }
164+ .format=${ formatDelayMs }
165+ .floorMax=${ 20 }
166+ > </ openclaw-debug-sparkline >
167+ </ div >
168+ ${ typeof status . uptimeMs === "number"
169+ ? html `< div class ="debug-overlay__vitals-footer mono ">
170+ ${ t ( "debug.overlay.uptime" ) } ${ formatDurationHuman ( status . uptimeMs ) }
171+ </ div > `
172+ : nothing }
114173 ` ;
115174}
116175
@@ -156,11 +215,16 @@ export const DEBUG_OVERLAY_SECTIONS: readonly DebugOverlaySectionDescriptor[] =
156215 id : "status" ,
157216 titleKey : "debug.overlay.status" ,
158217 load : async ( context , signal ) => {
159- const value = await context . client . request < StatusSectionValue > ( "status" , { } , { signal } ) ;
218+ const value = await context . client . request < DebugOverlayStatusSnapshot > (
219+ "status" ,
220+ { } ,
221+ { signal } ,
222+ ) ;
160223 return {
161224 eventLoop : value . eventLoop ,
225+ processMemory : value . processMemory ,
162226 ...( typeof value . uptimeMs === "number" ? { uptimeMs : value . uptimeMs } : { } ) ,
163- } satisfies StatusSectionValue ;
227+ } satisfies DebugOverlayStatusSnapshot ;
164228 } ,
165229 render : renderStatus ,
166230 } ) ,
0 commit comments