import { existsSync, readFileSync } from "node:fs"; import path from "node:path"; import { generateReminder } from "../../application/session/generate-reminder.js"; import { loadProjectSettings } from "../../shared/value-objects/project-settings.js"; import { createClosableStateStoresUnchecked } from "../../infrastructure/adapters/sqlite/create-state-stores.js"; import { resolveRepoRoot } from "../../infrastructure/home-directory.js"; import { SETTINGS_FILE, TFF_DIR } from "@tff/core"; import type { CommandSchema } from "../utils/flag-parser.js"; /** * Check if reminders are disabled in settings.yaml. * Returns true if workflow.reminders is explicitly false. */ function areRemindersDisabled(repoRoot: string): boolean { const settingsPath = path.join(repoRoot, SETTINGS_FILE); if (!existsSync(settingsPath)) { return false; // Default to enabled if no settings file } try { const content = readFileSync(settingsPath, "utf8"); // Use loadProjectSettings for proper Zod validation - no type casts needed const settings = loadProjectSettings(content); return settings.workflow?.reminders === false; } catch { return false; // On any error, default to enabled } } /** * Check if the project is initialized (has .tff directory). */ function isProjectInitialized(repoRoot: string): boolean { const tffDir = path.join(repoRoot, TFF_DIR); return existsSync(tffDir); } export const sessionRemindSchema: CommandSchema = { name: "session:remind", purpose: "Generate a reminder for the current session", mutates: false, requiredFlags: [], optionalFlags: [], examples: ["session:remind"], }; export const sessionRemindCmd = async (args: string[]): Promise => { // Check for --help flag if (args.includes("--help")) { return JSON.stringify({ ok: true, data: { name: sessionRemindSchema.name, purpose: sessionRemindSchema.purpose, syntax: sessionRemindSchema.name, requiredFlags: [], optionalFlags: [], examples: sessionRemindSchema.examples, }, }); } const repoRoot = resolveRepoRoot(process.cwd()); // Fast path: check if reminders are disabled if (areRemindersDisabled(repoRoot)) { return JSON.stringify({ ok: true, data: { reminder: null }, }); } // Check if project is initialized if (!isProjectInitialized(repoRoot)) { return JSON.stringify({ ok: true, data: { reminder: null }, }); } try { const stores = createClosableStateStoresUnchecked(); const reminder = generateReminder(stores); return JSON.stringify({ ok: true, data: { reminder }, }); } catch (err) { return JSON.stringify({ ok: false, error: { code: "REMINDER_GENERATION_FAILED", message: err instanceof Error ? err.message : String(err), }, }); } };