import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { describeTokenSource, getMcpUrl, getTokenSource, hasSavedToken } from "../auth/token.js"; import type { TickTickMcpClient } from "../mcp/client.js"; import type { ExtensionState } from "../state.js"; type ConnectUi = { setStatus: (key: string, value: string) => void; notify: (message: string, level: "info" | "warning" | "error") => void; }; export function registerStatusCommand( pi: ExtensionAPI, getState: () => ExtensionState, getClient: () => TickTickMcpClient | null, ensureConnected: (ui: ConnectUi) => Promise, ): void { pi.registerCommand("ticktick-status", { description: "Show TickTick MCP connection and auth status", handler: async (_args, ctx) => { const source = await getTokenSource(); const hasFile = await hasSavedToken(); const state = getState(); if (source !== "none") { await ensureConnected({ setStatus: ctx.ui.setStatus.bind(ctx.ui), notify: ctx.ui.notify.bind(ctx.ui), }); } const client = getClient(); const toolCount = client?.getTools().length ?? state.toolCount; const lines = [ `Auth: ${describeTokenSource(source)}`, source === "none" && hasFile ? " (token file exists but could not be read)" : "", `MCP URL: ${getMcpUrl()}`, `Connected: ${state.connected ? "yes" : "no"}`, `Tools registered: ${toolCount}`, state.lastError ? `Last error: ${state.lastError}` : "", ].filter(Boolean); ctx.ui.notify(lines.join("\n"), state.connected ? "info" : "warning"); }, }); }