import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { resolveToken } from "./auth/token.js"; import { registerLogoutCommand } from "./commands/logout.js"; import { registerSetupCommand } from "./commands/setup.js"; import { registerStatusCommand } from "./commands/status.js"; import { closeMcpClient, ensureMcpConnected, getMcpClient } from "./mcp/connect.js"; import { createExtensionState } from "./state.js"; export default function (pi: ExtensionAPI) { const state = createExtensionState(); registerSetupCommand(pi); registerLogoutCommand(pi); registerStatusCommand( pi, () => state, () => getMcpClient(), (ui) => ensureMcpConnected(pi, state, ui), ); pi.on("session_start", async (_event, ctx) => { state.connected = false; state.toolsRegistered = false; state.toolCount = 0; state.lastError = null; await closeMcpClient(); const token = await resolveToken(); if (!token) { ctx.ui.setStatus("ticktick", "not configured"); return; } ctx.ui.setStatus("ticktick", "idle"); }); pi.on("before_agent_start", async (_event, ctx) => { const token = await resolveToken(); if (!token) { return; } await ensureMcpConnected(pi, state, { setStatus: ctx.ui.setStatus.bind(ctx.ui), }); }); pi.on("session_shutdown", async () => { await closeMcpClient(); state.connected = false; state.toolsRegistered = false; state.toolCount = 0; }); } export type { ExtensionState } from "./state.js";