mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 10:21:24 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -47,7 +47,9 @@ function summarizeSources(sources: Array<string | undefined>): {
|
||||
|
||||
function existsSyncMaybe(p: string | undefined): boolean | null {
|
||||
const path = p?.trim() || "";
|
||||
if (!path) return null;
|
||||
if (!path) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return fs.existsSync(path);
|
||||
} catch {
|
||||
@@ -61,17 +63,25 @@ function sha256HexPrefix(value: string, len = 8): string {
|
||||
|
||||
function formatTokenHint(token: string, opts: { showSecrets: boolean }): string {
|
||||
const t = token.trim();
|
||||
if (!t) return "empty";
|
||||
if (!opts.showSecrets) return `sha256:${sha256HexPrefix(t)} · len ${t.length}`;
|
||||
if (!t) {
|
||||
return "empty";
|
||||
}
|
||||
if (!opts.showSecrets) {
|
||||
return `sha256:${sha256HexPrefix(t)} · len ${t.length}`;
|
||||
}
|
||||
const head = t.slice(0, 4);
|
||||
const tail = t.slice(-4);
|
||||
if (t.length <= 10) return `${t} · len ${t.length}`;
|
||||
if (t.length <= 10) {
|
||||
return `${t} · len ${t.length}`;
|
||||
}
|
||||
return `${head}…${tail} · len ${t.length}`;
|
||||
}
|
||||
|
||||
const formatAccountLabel = (params: { accountId: string; name?: string }) => {
|
||||
const base = params.accountId || "default";
|
||||
if (params.name?.trim()) return `${base} (${params.name.trim()})`;
|
||||
if (params.name?.trim()) {
|
||||
return `${base} (${params.name.trim()})`;
|
||||
}
|
||||
return base;
|
||||
};
|
||||
|
||||
@@ -80,7 +90,9 @@ const resolveAccountEnabled = (
|
||||
account: unknown,
|
||||
cfg: OpenClawConfig,
|
||||
): boolean => {
|
||||
if (plugin.config.isEnabled) return plugin.config.isEnabled(account, cfg);
|
||||
if (plugin.config.isEnabled) {
|
||||
return plugin.config.isEnabled(account, cfg);
|
||||
}
|
||||
const enabled = asRecord(account).enabled;
|
||||
return enabled !== false;
|
||||
};
|
||||
@@ -138,8 +150,12 @@ const buildAccountNotes = (params: {
|
||||
const { plugin, cfg, entry } = params;
|
||||
const notes: string[] = [];
|
||||
const snapshot = entry.snapshot;
|
||||
if (snapshot.enabled === false) notes.push("disabled");
|
||||
if (snapshot.dmPolicy) notes.push(`dm:${snapshot.dmPolicy}`);
|
||||
if (snapshot.enabled === false) {
|
||||
notes.push("disabled");
|
||||
}
|
||||
if (snapshot.dmPolicy) {
|
||||
notes.push(`dm:${snapshot.dmPolicy}`);
|
||||
}
|
||||
if (snapshot.tokenSource && snapshot.tokenSource !== "none") {
|
||||
notes.push(`token:${snapshot.tokenSource}`);
|
||||
}
|
||||
@@ -149,10 +165,18 @@ const buildAccountNotes = (params: {
|
||||
if (snapshot.appTokenSource && snapshot.appTokenSource !== "none") {
|
||||
notes.push(`app:${snapshot.appTokenSource}`);
|
||||
}
|
||||
if (snapshot.baseUrl) notes.push(snapshot.baseUrl);
|
||||
if (snapshot.port != null) notes.push(`port:${snapshot.port}`);
|
||||
if (snapshot.cliPath) notes.push(`cli:${snapshot.cliPath}`);
|
||||
if (snapshot.dbPath) notes.push(`db:${snapshot.dbPath}`);
|
||||
if (snapshot.baseUrl) {
|
||||
notes.push(snapshot.baseUrl);
|
||||
}
|
||||
if (snapshot.port != null) {
|
||||
notes.push(`port:${snapshot.port}`);
|
||||
}
|
||||
if (snapshot.cliPath) {
|
||||
notes.push(`cli:${snapshot.cliPath}`);
|
||||
}
|
||||
if (snapshot.dbPath) {
|
||||
notes.push(`db:${snapshot.dbPath}`);
|
||||
}
|
||||
|
||||
const allowFrom =
|
||||
plugin.config.resolveAllowFrom?.({ cfg, accountId: snapshot.accountId }) ?? snapshot.allowFrom;
|
||||
@@ -163,7 +187,9 @@ const buildAccountNotes = (params: {
|
||||
accountId: snapshot.accountId,
|
||||
allowFrom,
|
||||
}).slice(0, 3);
|
||||
if (formatted.length > 0) notes.push(`allow:${formatted.join(",")}`);
|
||||
if (formatted.length > 0) {
|
||||
notes.push(`allow:${formatted.join(",")}`);
|
||||
}
|
||||
}
|
||||
|
||||
return notes;
|
||||
@@ -198,7 +224,9 @@ function collectMissingPaths(accounts: ChannelAccountRow[]): string[] {
|
||||
const raw =
|
||||
(accountRec[key] as string | undefined) ?? (snapshotRec[key] as string | undefined);
|
||||
const ok = existsSyncMaybe(raw);
|
||||
if (ok === false) missing.push(String(raw));
|
||||
if (ok === false) {
|
||||
missing.push(String(raw));
|
||||
}
|
||||
}
|
||||
}
|
||||
return missing;
|
||||
@@ -211,7 +239,9 @@ function summarizeTokenConfig(params: {
|
||||
showSecrets: boolean;
|
||||
}): { state: "ok" | "setup" | "warn" | null; detail: string | null } {
|
||||
const enabled = params.accounts.filter((a) => a.enabled);
|
||||
if (enabled.length === 0) return { state: null, detail: null };
|
||||
if (enabled.length === 0) {
|
||||
return { state: null, detail: null };
|
||||
}
|
||||
|
||||
const accountRecs = enabled.map((a) => asRecord(a.account));
|
||||
const hasBotOrAppTokenFields = accountRecs.some((r) => "botToken" in r || "appToken" in r);
|
||||
@@ -365,28 +395,50 @@ export async function buildChannelsTable(
|
||||
const label = plugin.meta.label ?? plugin.id;
|
||||
|
||||
const state = (() => {
|
||||
if (!anyEnabled) return "off";
|
||||
if (missingPaths.length > 0) return "warn";
|
||||
if (issues.length > 0) return "warn";
|
||||
if (link.linked === false) return "setup";
|
||||
if (tokenSummary.state) return tokenSummary.state;
|
||||
if (link.linked === true) return "ok";
|
||||
if (configuredAccounts.length > 0) return "ok";
|
||||
if (!anyEnabled) {
|
||||
return "off";
|
||||
}
|
||||
if (missingPaths.length > 0) {
|
||||
return "warn";
|
||||
}
|
||||
if (issues.length > 0) {
|
||||
return "warn";
|
||||
}
|
||||
if (link.linked === false) {
|
||||
return "setup";
|
||||
}
|
||||
if (tokenSummary.state) {
|
||||
return tokenSummary.state;
|
||||
}
|
||||
if (link.linked === true) {
|
||||
return "ok";
|
||||
}
|
||||
if (configuredAccounts.length > 0) {
|
||||
return "ok";
|
||||
}
|
||||
return "setup";
|
||||
})();
|
||||
|
||||
const detail = (() => {
|
||||
if (!anyEnabled) {
|
||||
if (!defaultEntry) return "disabled";
|
||||
if (!defaultEntry) {
|
||||
return "disabled";
|
||||
}
|
||||
return plugin.config.disabledReason?.(defaultEntry.account, cfg) ?? "disabled";
|
||||
}
|
||||
if (missingPaths.length > 0) return `missing file (${missingPaths[0]})`;
|
||||
if (issues.length > 0) return issues[0]?.message ?? "misconfigured";
|
||||
if (missingPaths.length > 0) {
|
||||
return `missing file (${missingPaths[0]})`;
|
||||
}
|
||||
if (issues.length > 0) {
|
||||
return issues[0]?.message ?? "misconfigured";
|
||||
}
|
||||
|
||||
if (link.linked !== null) {
|
||||
const base = link.linked ? "linked" : "not linked";
|
||||
const extra: string[] = [];
|
||||
if (link.linked && link.selfE164) extra.push(link.selfE164);
|
||||
if (link.linked && link.selfE164) {
|
||||
extra.push(link.selfE164);
|
||||
}
|
||||
if (link.linked && link.authAgeMs != null && link.authAgeMs >= 0) {
|
||||
extra.push(`auth ${formatAge(link.authAgeMs)}`);
|
||||
}
|
||||
@@ -396,11 +448,15 @@ export async function buildChannelsTable(
|
||||
return extra.length > 0 ? `${base} · ${extra.join(" · ")}` : base;
|
||||
}
|
||||
|
||||
if (tokenSummary.detail) return tokenSummary.detail;
|
||||
if (tokenSummary.detail) {
|
||||
return tokenSummary.detail;
|
||||
}
|
||||
|
||||
if (configuredAccounts.length > 0) {
|
||||
const head = "configured";
|
||||
if (accounts.length <= 1 && !plugin.meta.forceAccountBinding) return head;
|
||||
if (accounts.length <= 1 && !plugin.meta.forceAccountBinding) {
|
||||
return head;
|
||||
}
|
||||
return `${head} · accounts ${configuredAccounts.length}/${enabledAccounts.length || 1}`;
|
||||
}
|
||||
|
||||
|
||||
@@ -214,12 +214,20 @@ export async function appendStatusAllDiagnosis(params: {
|
||||
}
|
||||
|
||||
const healthErr = (() => {
|
||||
if (!params.health || typeof params.health !== "object") return "";
|
||||
if (!params.health || typeof params.health !== "object") {
|
||||
return "";
|
||||
}
|
||||
const record = params.health as Record<string, unknown>;
|
||||
if (!("error" in record)) return "";
|
||||
if (!("error" in record)) {
|
||||
return "";
|
||||
}
|
||||
const value = record.error;
|
||||
if (!value) return "";
|
||||
if (typeof value === "string") return value;
|
||||
if (!value) {
|
||||
return "";
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
return value;
|
||||
}
|
||||
try {
|
||||
return JSON.stringify(value, null, 2);
|
||||
} catch {
|
||||
|
||||
@@ -1,17 +1,29 @@
|
||||
export const formatAge = (ms: number | null | undefined) => {
|
||||
if (!ms || ms < 0) return "unknown";
|
||||
if (!ms || ms < 0) {
|
||||
return "unknown";
|
||||
}
|
||||
const minutes = Math.round(ms / 60_000);
|
||||
if (minutes < 1) return "just now";
|
||||
if (minutes < 60) return `${minutes}m ago`;
|
||||
if (minutes < 1) {
|
||||
return "just now";
|
||||
}
|
||||
if (minutes < 60) {
|
||||
return `${minutes}m ago`;
|
||||
}
|
||||
const hours = Math.round(minutes / 60);
|
||||
if (hours < 48) return `${hours}h ago`;
|
||||
if (hours < 48) {
|
||||
return `${hours}h ago`;
|
||||
}
|
||||
const days = Math.round(hours / 24);
|
||||
return `${days}d ago`;
|
||||
};
|
||||
|
||||
export const formatDuration = (ms: number | null | undefined) => {
|
||||
if (ms == null || !Number.isFinite(ms)) return "unknown";
|
||||
if (ms < 1000) return `${Math.round(ms)}ms`;
|
||||
if (ms == null || !Number.isFinite(ms)) {
|
||||
return "unknown";
|
||||
}
|
||||
if (ms < 1000) {
|
||||
return `${Math.round(ms)}ms`;
|
||||
}
|
||||
return `${(ms / 1000).toFixed(1)}s`;
|
||||
};
|
||||
|
||||
@@ -23,14 +35,22 @@ export function formatGatewayAuthUsed(
|
||||
): "token" | "password" | "token+password" | "none" {
|
||||
const hasToken = Boolean(auth?.token?.trim());
|
||||
const hasPassword = Boolean(auth?.password?.trim());
|
||||
if (hasToken && hasPassword) return "token+password";
|
||||
if (hasToken) return "token";
|
||||
if (hasPassword) return "password";
|
||||
if (hasToken && hasPassword) {
|
||||
return "token+password";
|
||||
}
|
||||
if (hasToken) {
|
||||
return "token";
|
||||
}
|
||||
if (hasPassword) {
|
||||
return "password";
|
||||
}
|
||||
return "none";
|
||||
}
|
||||
|
||||
export function redactSecrets(text: string): string {
|
||||
if (!text) return text;
|
||||
if (!text) {
|
||||
return text;
|
||||
}
|
||||
let out = text;
|
||||
out = out.replace(
|
||||
/(\b(?:access[_-]?token|refresh[_-]?token|token|password|secret|api[_-]?key)\b\s*[:=]\s*)("?)([^"\\s]+)("?)/gi,
|
||||
|
||||
@@ -2,20 +2,26 @@ import fs from "node:fs/promises";
|
||||
|
||||
export async function readFileTailLines(filePath: string, maxLines: number): Promise<string[]> {
|
||||
const raw = await fs.readFile(filePath, "utf8").catch(() => "");
|
||||
if (!raw.trim()) return [];
|
||||
if (!raw.trim()) {
|
||||
return [];
|
||||
}
|
||||
const lines = raw.replace(/\r/g, "").split("\n");
|
||||
const out = lines.slice(Math.max(0, lines.length - maxLines));
|
||||
return out.map((line) => line.trimEnd()).filter((line) => line.trim().length > 0);
|
||||
}
|
||||
|
||||
function countMatches(haystack: string, needle: string): number {
|
||||
if (!haystack || !needle) return 0;
|
||||
if (!haystack || !needle) {
|
||||
return 0;
|
||||
}
|
||||
return haystack.split(needle).length - 1;
|
||||
}
|
||||
|
||||
function shorten(message: string, maxLen: number): string {
|
||||
const cleaned = message.replace(/\s+/g, " ").trim();
|
||||
if (cleaned.length <= maxLen) return cleaned;
|
||||
if (cleaned.length <= maxLen) {
|
||||
return cleaned;
|
||||
}
|
||||
return `${cleaned.slice(0, Math.max(0, maxLen - 1))}…`;
|
||||
}
|
||||
|
||||
@@ -34,7 +40,9 @@ function consumeJsonBlock(
|
||||
): { json: string; endIndex: number } | null {
|
||||
const startLine = lines[startIndex] ?? "";
|
||||
const braceAt = startLine.indexOf("{");
|
||||
if (braceAt < 0) return null;
|
||||
if (braceAt < 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const parts: string[] = [startLine.slice(braceAt)];
|
||||
let depth = countMatches(parts[0] ?? "", "{") - countMatches(parts[0] ?? "", "}");
|
||||
@@ -66,7 +74,9 @@ export function summarizeLogTail(rawLines: string[], opts?: { maxLines?: number
|
||||
|
||||
const addLine = (line: string) => {
|
||||
const trimmed = line.trimEnd();
|
||||
if (!trimmed) return;
|
||||
if (!trimmed) {
|
||||
return;
|
||||
}
|
||||
out.push(trimmed);
|
||||
};
|
||||
|
||||
@@ -142,17 +152,23 @@ export function summarizeLogTail(rawLines: string[], opts?: { maxLines?: number
|
||||
}
|
||||
|
||||
for (const g of groups.values()) {
|
||||
if (g.count <= 1) continue;
|
||||
if (g.count <= 1) {
|
||||
continue;
|
||||
}
|
||||
out[g.index] = `${g.base} ×${g.count}`;
|
||||
}
|
||||
|
||||
const deduped: string[] = [];
|
||||
for (const line of out) {
|
||||
if (deduped[deduped.length - 1] === line) continue;
|
||||
if (deduped[deduped.length - 1] === line) {
|
||||
continue;
|
||||
}
|
||||
deduped.push(line);
|
||||
}
|
||||
|
||||
if (deduped.length <= maxLines) return deduped;
|
||||
if (deduped.length <= maxLines) {
|
||||
return deduped;
|
||||
}
|
||||
|
||||
const head = Math.min(6, Math.floor(maxLines / 3));
|
||||
const tail = Math.max(1, maxLines - head - 1);
|
||||
@@ -170,10 +186,14 @@ export function pickGatewaySelfPresence(presence: unknown): {
|
||||
version?: string;
|
||||
platform?: string;
|
||||
} | null {
|
||||
if (!Array.isArray(presence)) return null;
|
||||
if (!Array.isArray(presence)) {
|
||||
return null;
|
||||
}
|
||||
const entries = presence as Array<Record<string, unknown>>;
|
||||
const self = entries.find((e) => e.mode === "gateway" && e.reason === "self") ?? null;
|
||||
if (!self) return null;
|
||||
if (!self) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
host: typeof self.host === "string" ? self.host : undefined,
|
||||
ip: typeof self.ip === "string" ? self.ip : undefined,
|
||||
|
||||
@@ -86,14 +86,19 @@ export async function buildStatusAllReportLines(params: {
|
||||
for (const issue of params.channelIssues) {
|
||||
const key = issue.channel;
|
||||
const list = map.get(key);
|
||||
if (list) list.push(issue);
|
||||
else map.set(key, [issue]);
|
||||
if (list) {
|
||||
list.push(issue);
|
||||
} else {
|
||||
map.set(key, [issue]);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
})();
|
||||
const channelRowsWithIssues = channelRows.map((row) => {
|
||||
const issues = channelIssuesByChannel.get(row.channelId) ?? [];
|
||||
if (issues.length === 0) return row;
|
||||
if (issues.length === 0) {
|
||||
return row;
|
||||
}
|
||||
const issue = issues[0];
|
||||
const suffix = ` · ${warn(`gateway: ${String(issue.message).slice(0, 90)}`)}`;
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user