Skip to content

Commit 143f501

Browse files
committed
refactor: share status overview and json helpers
1 parent ad2df63 commit 143f501

16 files changed

Lines changed: 1145 additions & 361 deletions

‎src/commands/status-all/report-data.ts‎

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import { canExecRequestNode } from "../../agents/exec-defaults.js";
22
import { buildWorkspaceSkillStatus } from "../../agents/skills-status.js";
3-
import { formatCliCommand } from "../../cli/command-format.js";
43
import { readConfigFileSnapshot, resolveGatewayPort } from "../../config/config.js";
54
import { readLastGatewayErrorLine } from "../../daemon/diagnostics.js";
65
import { inspectPortUsage } from "../../infra/ports.js";
76
import { readRestartSentinel } from "../../infra/restart-sentinel.js";
87
import { getRemoteSkillEligibility } from "../../infra/skills-remote.js";
98
import { buildPluginCompatibilityNotices } from "../../plugins/status.js";
10-
import { VERSION } from "../../version.js";
11-
import { buildStatusAllAgentsValue, buildStatusSecretsValue } from "../status-overview-values.ts";
9+
import { buildStatusAllOverviewRows } from "../status-overview-rows.ts";
10+
import {
11+
buildStatusOverviewSurfaceFromOverview,
12+
type StatusOverviewSurface,
13+
} from "../status-overview-surface.ts";
1214
import {
1315
resolveStatusGatewayHealthSafe,
1416
type resolveStatusServiceSummaries,
1517
} from "../status-runtime-shared.ts";
1618
import { resolveStatusAllConnectionDetails } from "../status.gateway-connection.ts";
1719
import type { NodeOnlyGatewayInfo } from "../status.node-mode.js";
1820
import type { StatusScanOverviewResult } from "../status.scan-overview.ts";
19-
import { buildStatusOverviewSurfaceRows } from "./format.js";
2021

2122
type StatusServiceSummaries = Awaited<ReturnType<typeof resolveStatusServiceSummaries>>;
2223
type StatusGatewayServiceSummary = StatusServiceSummaries[0];
@@ -166,46 +167,19 @@ export async function buildStatusAllReportData(params: {
166167
timeoutMs: params.timeoutMs,
167168
});
168169

169-
const overviewRows = buildStatusOverviewSurfaceRows({
170-
cfg: params.overview.cfg,
171-
update: params.overview.update,
172-
tailscaleMode: params.overview.tailscaleMode,
173-
tailscaleDns: params.overview.tailscaleDns,
174-
tailscaleHttpsUrl: params.overview.tailscaleHttpsUrl,
175-
tailscaleBackendState: diagnosis.tailscale.backendState,
176-
includeBackendStateWhenOff: true,
177-
includeBackendStateWhenOn: true,
178-
includeDnsNameWhenOff: true,
179-
gatewayMode: gatewaySnapshot.gatewayMode,
180-
remoteUrlMissing: gatewaySnapshot.remoteUrlMissing,
181-
gatewayConnection: gatewaySnapshot.gatewayConnection,
182-
gatewayReachable: gatewaySnapshot.gatewayReachable,
183-
gatewayProbe: gatewaySnapshot.gatewayProbe,
184-
gatewayProbeAuth: gatewaySnapshot.gatewayProbeAuth,
185-
gatewayProbeAuthWarning: gatewaySnapshot.gatewayProbeAuthWarning,
186-
gatewaySelf: gatewaySnapshot.gatewaySelf,
170+
const overviewSurface: StatusOverviewSurface = buildStatusOverviewSurfaceFromOverview({
171+
overview: params.overview,
187172
gatewayService: params.daemon,
188173
nodeService: params.nodeService,
189174
nodeOnlyGateway: params.nodeOnlyGateway,
190-
prefixRows: [
191-
{ Item: "Version", Value: VERSION },
192-
{ Item: "OS", Value: params.overview.osSummary.label },
193-
{ Item: "Node", Value: process.versions.node },
194-
{ Item: "Config", Value: configPath },
195-
],
196-
middleRows: [
197-
{ Item: "Security", Value: `Run: ${formatCliCommand("openclaw security audit --deep")}` },
198-
],
199-
agentsValue: buildStatusAllAgentsValue({
200-
agentStatus: params.overview.agentStatus,
201-
}),
202-
suffixRows: [
203-
{
204-
Item: "Secrets",
205-
Value: buildStatusSecretsValue(params.overview.secretDiagnostics.length),
206-
},
207-
],
208-
gatewaySelfFallbackValue: "unknown",
175+
});
176+
const overviewRows = buildStatusAllOverviewRows({
177+
surface: overviewSurface,
178+
osLabel: params.overview.osSummary.label,
179+
configPath,
180+
secretDiagnosticsCount: params.overview.secretDiagnostics.length,
181+
agentStatus: params.overview.agentStatus,
182+
tailscaleBackendState: diagnosis.tailscale.backendState,
209183
});
210184

211185
return {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import { runStatusJsonCommand } from "./status-json-command.ts";
3+
4+
const mocks = vi.hoisted(() => ({
5+
writeRuntimeJson: vi.fn(),
6+
resolveStatusJsonOutput: vi.fn(async (input) => ({ built: true, input })),
7+
}));
8+
9+
vi.mock("../runtime.js", () => ({
10+
writeRuntimeJson: mocks.writeRuntimeJson,
11+
}));
12+
13+
vi.mock("./status-json-runtime.ts", () => ({
14+
resolveStatusJsonOutput: mocks.resolveStatusJsonOutput,
15+
}));
16+
17+
describe("runStatusJsonCommand", () => {
18+
beforeEach(() => {
19+
vi.clearAllMocks();
20+
});
21+
22+
it("shares the fast-json scan and output flow", async () => {
23+
const runtime = {
24+
log: vi.fn(),
25+
error: vi.fn(),
26+
exit: vi.fn(),
27+
} as never;
28+
const scan = {
29+
cfg: { gateway: {} },
30+
sourceConfig: { gateway: {} },
31+
summary: { ok: true },
32+
update: { installKind: "package", packageManager: "npm" },
33+
osSummary: { platform: "linux" },
34+
memory: null,
35+
memoryPlugin: null,
36+
gatewayMode: "local" as const,
37+
gatewayConnection: { url: "ws://127.0.0.1:18789", urlSource: "config" },
38+
remoteUrlMissing: false,
39+
gatewayReachable: true,
40+
gatewayProbe: null,
41+
gatewayProbeAuth: { token: "tok" },
42+
gatewaySelf: null,
43+
gatewayProbeAuthWarning: null,
44+
agentStatus: [],
45+
secretDiagnostics: [],
46+
};
47+
const scanStatusJsonFast = vi.fn(async () => scan);
48+
49+
await runStatusJsonCommand({
50+
opts: { deep: true, usage: true, timeoutMs: 1234, all: true },
51+
runtime,
52+
scanStatusJsonFast,
53+
includeSecurityAudit: true,
54+
includePluginCompatibility: true,
55+
suppressHealthErrors: true,
56+
});
57+
58+
expect(scanStatusJsonFast).toHaveBeenCalledWith({ timeoutMs: 1234, all: true }, runtime);
59+
expect(mocks.resolveStatusJsonOutput).toHaveBeenCalledWith({
60+
scan,
61+
opts: { deep: true, usage: true, timeoutMs: 1234, all: true },
62+
includeSecurityAudit: true,
63+
includePluginCompatibility: true,
64+
suppressHealthErrors: true,
65+
});
66+
expect(mocks.writeRuntimeJson).toHaveBeenCalledWith(runtime, {
67+
built: true,
68+
input: {
69+
scan,
70+
opts: { deep: true, usage: true, timeoutMs: 1234, all: true },
71+
includeSecurityAudit: true,
72+
includePluginCompatibility: true,
73+
suppressHealthErrors: true,
74+
},
75+
});
76+
});
77+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { type RuntimeEnv, writeRuntimeJson } from "../runtime.js";
2+
import { resolveStatusJsonOutput } from "./status-json-runtime.ts";
3+
4+
export type StatusJsonCommandOptions = {
5+
deep?: boolean;
6+
usage?: boolean;
7+
timeoutMs?: number;
8+
all?: boolean;
9+
};
10+
11+
export async function runStatusJsonCommand(params: {
12+
opts: StatusJsonCommandOptions;
13+
runtime: RuntimeEnv;
14+
includeSecurityAudit: boolean;
15+
includePluginCompatibility?: boolean;
16+
suppressHealthErrors?: boolean;
17+
scanStatusJsonFast: (
18+
opts: { timeoutMs?: number; all?: boolean },
19+
runtime: RuntimeEnv,
20+
) => Promise<Parameters<typeof resolveStatusJsonOutput>[0]["scan"]>;
21+
}) {
22+
const scan = await params.scanStatusJsonFast(
23+
{ timeoutMs: params.opts.timeoutMs, all: params.opts.all },
24+
params.runtime,
25+
);
26+
writeRuntimeJson(
27+
params.runtime,
28+
await resolveStatusJsonOutput({
29+
scan,
30+
opts: params.opts,
31+
includeSecurityAudit: params.includeSecurityAudit,
32+
includePluginCompatibility: params.includePluginCompatibility,
33+
suppressHealthErrors: params.suppressHealthErrors,
34+
}),
35+
);
36+
}

‎src/commands/status-json-payload.test.ts‎

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,29 @@ describe("status-json-payload", () => {
5959
expect(
6060
buildStatusJsonPayload({
6161
summary: { ok: true },
62-
updateConfigChannel: "stable",
63-
update: {
64-
root: "/tmp/openclaw",
65-
installKind: "package",
66-
packageManager: "npm",
67-
registry: { latestVersion: "1.2.3" },
62+
surface: {
63+
cfg: { update: { channel: "stable" }, gateway: {} },
64+
update: {
65+
root: "/tmp/openclaw",
66+
installKind: "package",
67+
packageManager: "npm",
68+
registry: { latestVersion: "1.2.3" },
69+
} as never,
70+
tailscaleMode: "serve",
71+
gatewayMode: "remote",
72+
remoteUrlMissing: false,
73+
gatewayConnection: { url: "wss://gateway.example.com", urlSource: "config" },
74+
gatewayReachable: true,
75+
gatewayProbe: { connectLatencyMs: 42, error: null },
76+
gatewayProbeAuth: { token: "tok" },
77+
gatewaySelf: { host: "gateway" },
78+
gatewayProbeAuthWarning: "warn",
79+
gatewayService: { label: "LaunchAgent" },
80+
nodeService: { label: "node" },
6881
},
6982
osSummary: { platform: "linux" },
7083
memory: null,
7184
memoryPlugin: { enabled: true },
72-
gatewayMode: "remote",
73-
gatewayConnection: { url: "wss://gateway.example.com", urlSource: "config" },
74-
remoteUrlMissing: false,
75-
gatewayReachable: true,
76-
gatewayProbe: { connectLatencyMs: 42, error: null },
77-
gatewaySelf: { host: "gateway" },
78-
gatewayProbeAuthWarning: "warn",
79-
gatewayService: { label: "LaunchAgent" },
80-
nodeService: { label: "node" },
8185
agents: [{ id: "main" }],
8286
secretDiagnostics: ["diag"],
8387
securityAudit: { summary: { critical: 1 } },
@@ -143,24 +147,28 @@ describe("status-json-payload", () => {
143147
expect(
144148
buildStatusJsonPayload({
145149
summary: { ok: true },
146-
updateConfigChannel: null,
147-
update: {
148-
root: "/tmp/openclaw",
149-
installKind: "package",
150-
packageManager: "npm",
150+
surface: {
151+
cfg: { gateway: {} },
152+
update: {
153+
root: "/tmp/openclaw",
154+
installKind: "package",
155+
packageManager: "npm",
156+
} as never,
157+
tailscaleMode: "off",
158+
gatewayMode: "local",
159+
remoteUrlMissing: false,
160+
gatewayConnection: { url: "ws://127.0.0.1:18789" },
161+
gatewayReachable: false,
162+
gatewayProbe: null,
163+
gatewayProbeAuth: null,
164+
gatewaySelf: null,
165+
gatewayProbeAuthWarning: null,
166+
gatewayService: { label: "LaunchAgent", installed: false, loadedText: "not installed" },
167+
nodeService: { label: "node", installed: false, loadedText: "not installed" },
151168
},
152169
osSummary: { platform: "linux" },
153170
memory: null,
154171
memoryPlugin: null,
155-
gatewayMode: "local",
156-
gatewayConnection: { url: "ws://127.0.0.1:18789" },
157-
remoteUrlMissing: false,
158-
gatewayReachable: false,
159-
gatewayProbe: null,
160-
gatewaySelf: null,
161-
gatewayProbeAuthWarning: null,
162-
gatewayService: null,
163-
nodeService: null,
164172
agents: [],
165173
secretDiagnostics: [],
166174
}),

‎src/commands/status-json-payload.ts‎

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,17 @@
1-
import type { OpenClawConfig } from "../config/types.js";
2-
import type { UpdateCheckResult } from "../infra/update-check.js";
1+
import { resolveStatusUpdateChannelInfo } from "./status-all/format.js";
32
import {
4-
buildGatewayStatusJsonPayload,
5-
resolveStatusUpdateChannelInfo,
6-
} from "./status-all/format.js";
3+
buildStatusGatewayJsonPayloadFromSurface,
4+
type StatusOverviewSurface,
5+
} from "./status-overview-surface.ts";
76

87
export { resolveStatusUpdateChannelInfo } from "./status-all/format.js";
98

10-
type UpdateConfigChannel = NonNullable<OpenClawConfig["update"]>["channel"];
11-
129
export function buildStatusJsonPayload(params: {
1310
summary: Record<string, unknown>;
14-
updateConfigChannel?: UpdateConfigChannel | null;
15-
update: UpdateCheckResult;
11+
surface: StatusOverviewSurface;
1612
osSummary: unknown;
1713
memory: unknown;
1814
memoryPlugin: unknown;
19-
gatewayMode: "local" | "remote";
20-
gatewayConnection: {
21-
url: string;
22-
urlSource?: string;
23-
};
24-
remoteUrlMissing: boolean;
25-
gatewayReachable: boolean;
26-
gatewayProbe:
27-
| {
28-
connectLatencyMs?: number | null;
29-
error?: string | null;
30-
}
31-
| null
32-
| undefined;
33-
gatewaySelf:
34-
| {
35-
host?: string | null;
36-
ip?: string | null;
37-
version?: string | null;
38-
platform?: string | null;
39-
}
40-
| null
41-
| undefined;
42-
gatewayProbeAuthWarning?: string | null;
43-
gatewayService: unknown;
44-
nodeService: unknown;
4515
agents: unknown;
4616
secretDiagnostics: string[];
4717
securityAudit?: unknown;
@@ -51,28 +21,20 @@ export function buildStatusJsonPayload(params: {
5121
pluginCompatibility?: Array<Record<string, unknown>> | null | undefined;
5222
}) {
5323
const channelInfo = resolveStatusUpdateChannelInfo({
54-
updateConfigChannel: params.updateConfigChannel ?? undefined,
55-
update: params.update,
24+
updateConfigChannel: params.surface.cfg.update?.channel ?? undefined,
25+
update: params.surface.update,
5626
});
5727
return {
5828
...params.summary,
5929
os: params.osSummary,
60-
update: params.update,
30+
update: params.surface.update,
6131
updateChannel: channelInfo.channel,
6232
updateChannelSource: channelInfo.source,
6333
memory: params.memory,
6434
memoryPlugin: params.memoryPlugin,
65-
gateway: buildGatewayStatusJsonPayload({
66-
gatewayMode: params.gatewayMode,
67-
gatewayConnection: params.gatewayConnection,
68-
remoteUrlMissing: params.remoteUrlMissing,
69-
gatewayReachable: params.gatewayReachable,
70-
gatewayProbe: params.gatewayProbe,
71-
gatewaySelf: params.gatewaySelf,
72-
gatewayProbeAuthWarning: params.gatewayProbeAuthWarning,
73-
}),
74-
gatewayService: params.gatewayService,
75-
nodeService: params.nodeService,
35+
gateway: buildStatusGatewayJsonPayloadFromSurface({ surface: params.surface }),
36+
gatewayService: params.surface.gatewayService,
37+
nodeService: params.surface.nodeService,
7638
agents: params.agents,
7739
secretDiagnostics: params.secretDiagnostics,
7840
...(params.securityAudit ? { securityAudit: params.securityAudit } : {}),

0 commit comments

Comments
 (0)