1+ /** Shared helpers for gateway status target selection, auth, summaries, and probe rendering. */
12import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce" ;
23import { colorize , theme } from "../../../packages/terminal-core/src/theme.js" ;
34import { parseTimeoutMsWithFallback } from "../../cli/parse-timeout.js" ;
@@ -15,6 +16,7 @@ const MISSING_SCOPE_PATTERN = /\bmissing scope:\s*[a-z0-9._-]+/i;
1516
1617type TargetKind = "explicit" | "configRemote" | "localLoopback" | "sshTunnel" ;
1718
19+ /** Concrete websocket endpoint that gateway status should probe. */
1820export type GatewayStatusTarget = {
1921 id : string ;
2022 kind : TargetKind ;
@@ -29,6 +31,7 @@ export type GatewayStatusTarget = {
2931 } ;
3032} ;
3133
34+ /** Sanitized config subset rendered by the deep gateway status view. */
3235export type GatewayConfigSummary = {
3336 path : string | null ;
3437 exists : boolean ;
@@ -67,6 +70,7 @@ function parseIntOrNull(value: unknown): number | null {
6770 return parseStrictInteger ( s ) ?? null ;
6871}
6972
73+ /** Parses CLI timeout input with the gateway-status fallback rules. */
7074export function parseTimeoutMs ( raw : unknown , fallbackMs : number ) : number {
7175 return parseTimeoutMsWithFallback ( raw , fallbackMs ) ;
7276}
@@ -82,6 +86,7 @@ function normalizeWsUrl(value: string): string | null {
8286 return trimmed ;
8387}
8488
89+ /** Builds the deduplicated ordered gateway probe targets from CLI input and config. */
8590export function resolveTargets ( cfg : OpenClawConfig , explicitUrl ?: string ) : GatewayStatusTarget [ ] {
8691 const targets : GatewayStatusTarget [ ] = [ ] ;
8792 const add = ( t : GatewayStatusTarget ) => {
@@ -148,6 +153,7 @@ export function resolveProbeBudgetMs(
148153 return overallMs ;
149154}
150155
156+ /** Normalizes user-entered SSH targets, accepting both raw targets and `ssh host` input. */
151157export function sanitizeSshTarget ( value : unknown ) : string | null {
152158 if ( typeof value !== "string" ) {
153159 return null ;
@@ -159,6 +165,7 @@ export function sanitizeSshTarget(value: unknown): string | null {
159165 return trimmed . replace ( / ^ s s h \s + / , "" ) ;
160166}
161167
168+ /** Resolves auth for the probe surface represented by the selected status target. */
162169export async function resolveAuthForTarget (
163170 cfg : OpenClawConfig ,
164171 target : GatewayStatusTarget ,
@@ -178,6 +185,7 @@ export async function resolveAuthForTarget(
178185
179186export { pickGatewaySelfPresence } ;
180187
188+ /** Extracts the config fields displayed by `openclaw gateway status --deep`. */
181189export function extractConfigSummary ( snapshotUnknown : unknown ) : GatewayConfigSummary {
182190 const snap = snapshotUnknown as Partial < ConfigFileSnapshot > | null ;
183191 const path = typeof snap ?. path === "string" ? snap . path : null ;
@@ -244,6 +252,7 @@ export function extractConfigSummary(snapshotUnknown: unknown): GatewayConfigSum
244252 } ;
245253}
246254
255+ /** Builds local and tailnet gateway URL hints for the configured gateway port. */
247256export function buildNetworkHints ( cfg : OpenClawConfig ) {
248257 const { tailnetIPv4 } = inspectBestEffortPrimaryTailnetIPv4 ( ) ;
249258 const port = resolveGatewayPort ( cfg ) ;
@@ -255,6 +264,7 @@ export function buildNetworkHints(cfg: OpenClawConfig) {
255264 } ;
256265}
257266
267+ /** Renders the status heading for a single gateway probe target. */
258268export function renderTargetHeader ( target : GatewayStatusTarget , rich : boolean ) {
259269 const kindLabel =
260270 target . kind === "localLoopback"
@@ -269,17 +279,20 @@ export function renderTargetHeader(target: GatewayStatusTarget, rich: boolean) {
269279 return `${ colorize ( rich , theme . heading , kindLabel ) } ${ colorize ( rich , theme . muted , target . url ) } ` ;
270280}
271281
282+ /** Returns true when auth succeeded enough to connect but lacks the read scope. */
272283export function isScopeLimitedProbeFailure ( probe : GatewayProbeResult ) : boolean {
273284 if ( probe . ok || probe . connectLatencyMs == null ) {
274285 return false ;
275286 }
276287 return MISSING_SCOPE_PATTERN . test ( probe . error ?? "" ) ;
277288}
278289
290+ /** Returns true when the gateway connection was established but a later probe failed. */
279291export function isPostConnectProbeFailure ( probe : GatewayProbeResult ) : boolean {
280292 return ! probe . ok && probe . connectLatencyMs != null ;
281293}
282294
295+ /** Returns true when the probe established any gateway connection. */
283296export function isProbeReachable ( probe : GatewayProbeResult ) : boolean {
284297 return probe . ok || probe . connectLatencyMs != null ;
285298}
@@ -291,6 +304,7 @@ function getGatewayProbeCapability(probe: GatewayProbeResult): GatewayProbeCapab
291304export function summarizeGatewayProbeCapability (
292305 probes : GatewayProbeResult [ ] ,
293306) : GatewayProbeCapability {
307+ // Show the strongest observed capability across all attempted targets.
294308 const priority : GatewayProbeCapability [ ] = [
295309 "admin_capable" ,
296310 "write_capable" ,
0 commit comments