93 lines
4.6 KiB
TypeScript
93 lines
4.6 KiB
TypeScript
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
||
import { join } from "node:path";
|
||
import { listSessions, PeppaError, projectForUser, resolveTask, userProjectIds, type Project, type TaskRef } from "@peppa/core";
|
||
import { events, goals, projects, questions, sessions, tasks } from "@peppa/db";
|
||
import { formatTaskKey } from "@peppa/shared";
|
||
import { and, asc, count, desc, eq, inArray, isNull } from "drizzle-orm";
|
||
import { db } from "./db";
|
||
|
||
export type InboxItem = Awaited<ReturnType<typeof inbox>>[number];
|
||
|
||
/** Open PM questions across the user's projects, oldest first. */
|
||
export async function inbox(userId: number) {
|
||
const ids = await userProjectIds(db, userId);
|
||
if (!ids.length) return [];
|
||
const rows = await db
|
||
.select({ q: questions, projectKey: projects.key, taskNumber: tasks.number, taskTitle: tasks.title, taskStatus: tasks.status, tmux: sessions.tmuxName })
|
||
.from(questions)
|
||
.innerJoin(projects, eq(questions.projectId, projects.id))
|
||
.leftJoin(tasks, eq(questions.taskId, tasks.id))
|
||
.leftJoin(sessions, eq(questions.sessionId, sessions.id))
|
||
.where(and(inArray(projects.id, ids), isNull(questions.answeredAt)))
|
||
.orderBy(asc(questions.id));
|
||
return rows.map((r) => ({ ...r.q, taskKey: r.taskNumber != null ? formatTaskKey(r.projectKey, r.taskNumber) : null, taskTitle: r.taskTitle, taskStatus: r.taskStatus, tmux: r.tmux }));
|
||
}
|
||
|
||
export async function activeSessions(userId: number) {
|
||
const ids = new Set(await userProjectIds(db, userId));
|
||
if (!ids.size) return [];
|
||
return (await listSessions(db, { active: true })).filter((s) => ids.has(s.projectId));
|
||
}
|
||
|
||
export async function projectForUserByKey(userId: number, key: string): Promise<Project> {
|
||
const [p] = await db.select().from(projects).where(eq(projects.key, key.toUpperCase()));
|
||
if (!p) throw new PeppaError("Не найдено");
|
||
return projectForUser(db, userId, p.id);
|
||
}
|
||
|
||
export async function board(projectId: number) {
|
||
const gs = await db.select().from(goals).where(eq(goals.projectId, projectId)).orderBy(asc(goals.id));
|
||
const ts = await db.select().from(tasks).where(eq(tasks.projectId, projectId)).orderBy(asc(tasks.number));
|
||
return { goals: gs, tasks: ts };
|
||
}
|
||
|
||
export async function projectSessions(projectId: number) {
|
||
return (await listSessions(db)).filter((s) => s.projectId === projectId).slice(0, 20);
|
||
}
|
||
|
||
export async function taskForUser(userId: number, key: string): Promise<TaskRef> {
|
||
const ref = await resolveTask(db, key);
|
||
await projectForUser(db, userId, ref.project.id);
|
||
return ref;
|
||
}
|
||
|
||
export async function taskActivity(taskId: number) {
|
||
const qs = await db.select().from(questions).where(eq(questions.taskId, taskId)).orderBy(desc(questions.id));
|
||
const ss = await listSessions(db, { taskId });
|
||
const evs = await db.select().from(events).where(eq(events.taskId, taskId)).orderBy(desc(events.id)).limit(40);
|
||
return { questions: qs, sessions: ss, events: evs };
|
||
}
|
||
|
||
export const isActiveSession = (status: string) => !["done", "failed"].includes(status);
|
||
|
||
/** OpenSpec artifacts of a change from the working copy (active or archived). */
|
||
export function openspecArtifacts(p: Project, change: string | null): Array<{ name: string; text: string }> | null {
|
||
if (!change) return null;
|
||
const changes = join(p.workspacePath, "openspec", "changes");
|
||
let dir = join(changes, change);
|
||
if (!existsSync(dir)) {
|
||
const archive = join(changes, "archive");
|
||
const hit = existsSync(archive) ? readdirSync(archive).find((d) => d.endsWith(`-${change}`) || d === change) : undefined;
|
||
if (!hit) return null;
|
||
dir = join(archive, hit);
|
||
}
|
||
return ["proposal.md", "design.md", "tasks.md"]
|
||
.filter((f) => existsSync(join(dir, f)))
|
||
.map((f) => ({ name: f, text: readFileSync(join(dir, f), "utf8") }));
|
||
}
|
||
|
||
|
||
/** Counters of the side navigation (PEP-38): open PM questions and runners waiting for the PM. */
|
||
export async function navCounts(userId: number): Promise<{ questions: number; waitingRunners: number }> {
|
||
const ids = await userProjectIds(db, userId);
|
||
if (!ids.length) return { questions: 0, waitingRunners: 0 };
|
||
const [q] = await db.select({ n: count() }).from(questions).where(and(inArray(questions.projectId, ids), isNull(questions.answeredAt)));
|
||
const [r] = await db.select({ n: count() }).from(sessions).where(and(inArray(sessions.projectId, ids), eq(sessions.status, "waiting_input")));
|
||
return { questions: q?.n ?? 0, waitingRunners: r?.n ?? 0 };
|
||
}
|
||
|
||
/** Latest events of one runner, newest first (runner details panel, PEP-38). */
|
||
export async function sessionEvents(sessionId: number, limit = 12) {
|
||
return db.select().from(events).where(eq(events.sessionId, sessionId)).orderBy(desc(events.id)).limit(limit);
|
||
}
|