From d42444928b682b0eec947a47da9858e6bba467af Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 8 Jan 2026 12:01:06 +0100 Subject: [PATCH] chore: add provider logs command --- src/cli/providers-cli.ts | 16 +++ src/commands/providers.ts | 2 + src/commands/providers/logs.ts | 163 +++++++++++++++++++++++++++++++ src/commands/providers/status.ts | 3 + 4 files changed, 184 insertions(+) create mode 100644 src/commands/providers/logs.ts diff --git a/src/cli/providers-cli.ts b/src/cli/providers-cli.ts index 7ae765ac9..5a4d07094 100644 --- a/src/cli/providers-cli.ts +++ b/src/cli/providers-cli.ts @@ -3,6 +3,7 @@ import type { Command } from "commander"; import { providersAddCommand, providersListCommand, + providersLogsCommand, providersRemoveCommand, providersStatusCommand, } from "../commands/providers.js"; @@ -73,6 +74,21 @@ export function registerProvidersCli(program: Command) { } }); + providers + .command("logs") + .description("Show recent provider logs from the gateway log file") + .option("--provider ", `Provider (${providerNames}|all)`, "all") + .option("--lines ", "Number of lines (default: 200)", "200") + .option("--json", "Output JSON", false) + .action(async (opts) => { + try { + await providersLogsCommand(opts, defaultRuntime); + } catch (err) { + defaultRuntime.error(String(err)); + defaultRuntime.exit(1); + } + }); + providers .command("add") .description("Add or update a provider account") diff --git a/src/commands/providers.ts b/src/commands/providers.ts index 4e9ac46ed..c3c86c77f 100644 --- a/src/commands/providers.ts +++ b/src/commands/providers.ts @@ -2,6 +2,8 @@ export type { ProvidersAddOptions } from "./providers/add.js"; export { providersAddCommand } from "./providers/add.js"; export type { ProvidersListOptions } from "./providers/list.js"; export { providersListCommand } from "./providers/list.js"; +export type { ProvidersLogsOptions } from "./providers/logs.js"; +export { providersLogsCommand } from "./providers/logs.js"; export type { ProvidersRemoveOptions } from "./providers/remove.js"; export { providersRemoveCommand } from "./providers/remove.js"; export type { ProvidersStatusOptions } from "./providers/status.js"; diff --git a/src/commands/providers/logs.ts b/src/commands/providers/logs.ts new file mode 100644 index 000000000..deb1dc67c --- /dev/null +++ b/src/commands/providers/logs.ts @@ -0,0 +1,163 @@ +import fs from "node:fs/promises"; + +import { getResolvedLoggerSettings } from "../../logging.js"; +import { theme } from "../../terminal/theme.js"; +import { defaultRuntime, type RuntimeEnv } from "../../runtime.js"; + +export type ProvidersLogsOptions = { + provider?: string; + lines?: string | number; + json?: boolean; +}; + +type LogLine = { + time?: string; + level?: string; + subsystem?: string; + module?: string; + message: string; + raw: string; +}; + +const DEFAULT_LIMIT = 200; +const MAX_BYTES = 1_000_000; +const PROVIDERS = new Set([ + "whatsapp", + "telegram", + "discord", + "slack", + "signal", + "imessage", + "all", +]); + +function parseProviderFilter(raw?: string) { + const trimmed = raw?.trim().toLowerCase(); + if (!trimmed) return "all"; + return PROVIDERS.has(trimmed) ? trimmed : "all"; +} + +function extractMessage(value: Record): string { + const parts: string[] = []; + for (const key of Object.keys(value)) { + if (!/^\d+$/.test(key)) continue; + const item = value[key]; + if (typeof item === "string") { + parts.push(item); + } else if (item != null) { + parts.push(JSON.stringify(item)); + } + } + return parts.join(" "); +} + +function parseMetaName(raw?: unknown): { subsystem?: string; module?: string } { + if (typeof raw !== "string") return {}; + try { + const parsed = JSON.parse(raw) as Record; + return { + subsystem: + typeof parsed.subsystem === "string" ? parsed.subsystem : undefined, + module: typeof parsed.module === "string" ? parsed.module : undefined, + }; + } catch { + return {}; + } +} + +function parseLogLine(raw: string): LogLine | null { + try { + const parsed = JSON.parse(raw) as Record; + const meta = parsed._meta as Record | undefined; + const nameMeta = parseMetaName(meta?.name); + return { + time: + typeof parsed.time === "string" + ? parsed.time + : typeof meta?.date === "string" + ? meta.date + : undefined, + level: + typeof meta?.logLevelName === "string" ? meta.logLevelName : undefined, + subsystem: nameMeta.subsystem, + module: nameMeta.module, + message: extractMessage(parsed), + raw, + }; + } catch { + return null; + } +} + +function matchesProvider(line: LogLine, provider: string) { + if (provider === "all") return true; + const needle = `gateway/providers/${provider}`; + if (line.subsystem?.includes(needle)) return true; + if (line.module?.includes(provider)) return true; + return false; +} + +async function readTailLines(file: string, limit: number): Promise { + const stat = await fs.stat(file).catch(() => null); + if (!stat) return []; + const size = stat.size; + const start = Math.max(0, size - MAX_BYTES); + const handle = await fs.open(file, "r"); + try { + const length = Math.max(0, size - start); + if (length === 0) return []; + const buffer = Buffer.alloc(length); + const readResult = await handle.read(buffer, 0, length, start); + const text = buffer.toString("utf8", 0, readResult.bytesRead); + let lines = text.split("\n"); + if (start > 0) lines = lines.slice(1); + if (lines.length && lines[lines.length - 1] === "") { + lines = lines.slice(0, -1); + } + if (lines.length > limit) { + lines = lines.slice(lines.length - limit); + } + return lines; + } finally { + await handle.close(); + } +} + +export async function providersLogsCommand( + opts: ProvidersLogsOptions, + runtime: RuntimeEnv = defaultRuntime, +) { + const provider = parseProviderFilter(opts.provider); + const limitRaw = typeof opts.lines === "string" ? Number(opts.lines) : opts.lines; + const limit = + typeof limitRaw === "number" && Number.isFinite(limitRaw) && limitRaw > 0 + ? Math.floor(limitRaw) + : DEFAULT_LIMIT; + + const file = getResolvedLoggerSettings().file; + const rawLines = await readTailLines(file, limit * 4); + const parsed = rawLines + .map(parseLogLine) + .filter((line): line is LogLine => Boolean(line)); + const filtered = parsed.filter((line) => matchesProvider(line, provider)); + const lines = filtered.slice(Math.max(0, filtered.length - limit)); + + if (opts.json) { + runtime.log(JSON.stringify({ file, provider, lines }, null, 2)); + return; + } + + runtime.log(theme.info(`Log file: ${file}`)); + if (provider !== "all") { + runtime.log(theme.info(`Provider: ${provider}`)); + } + if (lines.length === 0) { + runtime.log(theme.muted("No matching log lines.")); + return; + } + for (const line of lines) { + const ts = line.time ? `${line.time} ` : ""; + const level = line.level ? `${line.level.toLowerCase()} ` : ""; + runtime.log(`${ts}${level}${line.message}`.trim()); + } +} diff --git a/src/commands/providers/status.ts b/src/commands/providers/status.ts index 0d3a27f8e..4fd169b5e 100644 --- a/src/commands/providers/status.ts +++ b/src/commands/providers/status.ts @@ -105,6 +105,9 @@ export function formatGatewayProvidersStatusLines( if (probe && typeof probe.ok === "boolean") { bits.push(probe.ok ? "works" : "probe failed"); } + if (typeof account.lastError === "string" && account.lastError) { + bits.push(`error:${account.lastError}`); + } const accountId = typeof account.accountId === "string" ? account.accountId : "default"; const name = typeof account.name === "string" ? account.name.trim() : "";