import { marked } from "marked"; import sanitizeHtml from "sanitize-html"; import { requireProject } from "./project.js"; import { getInstructions, type InstructionsRecord } from "./instructions.js"; import { type DocRecord, type InboxRecord, type TaskRecord, listDocs, listInbox, listTasks, } from "./records.js"; import { TASK_PRIORITY_MODEL, sortTasksByPriority, type TaskPriority, } from "./task-priorities.js"; export interface ProjectSnapshot { projectName: string; projectLanguage: string; taskStatuses: string[]; taskPriorityModel: string | null; instructions: InstructionsRecord; instructionsExists: boolean; inbox: InboxRecord[]; tasks: TaskRecord[]; docs: DocRecord[]; } export async function getProjectSnapshot( cwd?: string, ): Promise { const project = await requireProject(cwd); const [instructions, inbox, tasks, docs] = await Promise.all([ getInstructions(project.root), listInbox(project.root), listTasks(project.root), listDocs(project.root), ]); return { projectName: project.config.project.name, projectLanguage: project.config.project.language, taskStatuses: project.config.tasks.statuses, taskPriorityModel: project.config.tasks.priority_model, instructions: instructions.record, instructionsExists: instructions.exists, inbox, tasks, docs, }; } export async function renderMarkdown(body: string): Promise { const html = marked.parse(body, { async: false, gfm: true, }) as string; /** * Inbox messages can come from the server-backed browser form, so rendered * Markdown must not pass raw HTML through to the customer-facing UI. */ return sanitizeHtml(html, { allowedTags: sanitizeHtml.defaults.allowedTags.concat([ "h1", "h2", "h3", "img", "table", "thead", "tbody", "tr", "th", "td", ]), allowedAttributes: { ...sanitizeHtml.defaults.allowedAttributes, a: ["href", "name", "target", "rel"], img: ["src", "alt", "title"], }, }); } export function groupTasksByStatus( tasks: TaskRecord[], statuses: string[], priorityModel: string | null = null, ): Array<{ status: string; tasks: TaskRecord[] }> { return statuses.map((status) => ({ status, tasks: priorityModel === TASK_PRIORITY_MODEL ? sortTasksByPriority( tasks.filter((task) => task.data.status === status), ) : tasks.filter((task) => task.data.status === status), })); } export interface BoardTaskSelection { activeGroups: Array<{ status: string; tasks: TaskRecord[] }>; completedTasks: TaskRecord[]; archivedTasks: TaskRecord[]; archivedGroups: Array<{ status: string; tasks: TaskRecord[] }>; hasDone: boolean; hasArchive: boolean; } /** * Migrated projects use `not_planned` as an orthogonal archive flag while * legacy projects retain the exact status-based behavior until their explicit * migration supplies the missing active status for each archived task. */ export function selectBoardTasks( tasks: TaskRecord[], statuses: string[], priorityModel: string | null = null, ): BoardTaskSelection { const hasDone = statuses.includes("done"); if (priorityModel !== TASK_PRIORITY_MODEL) { const hasArchive = statuses.includes("archived"); const activeStatuses = statuses.filter( (status) => status !== "done" && status !== "archived", ); const archivedTasks = hasArchive ? tasks.filter((task) => task.data.status === "archived") : []; return { activeGroups: groupTasksByStatus(tasks, activeStatuses), completedTasks: hasDone ? tasks.filter((task) => task.data.status === "done") : [], archivedTasks, archivedGroups: hasArchive ? [{ status: "archived", tasks: archivedTasks }] : [], hasDone, hasArchive, }; } const activeStatuses = statuses.filter((status) => status !== "done"); const scheduledTasks = tasks.filter( (task) => (task.data.priority as TaskPriority) !== "not_planned", ); const archivedTasks = sortTasksByPriority( tasks.filter( (task) => (task.data.priority as TaskPriority) === "not_planned", ), ); return { activeGroups: groupTasksByStatus( scheduledTasks, activeStatuses, priorityModel, ), completedTasks: hasDone ? sortTasksByPriority( scheduledTasks.filter((task) => task.data.status === "done"), ) : [], archivedTasks, archivedGroups: groupTasksByStatus(archivedTasks, statuses, priorityModel), hasDone, hasArchive: true, }; }