78 lines
4.2 KiB
TypeScript
78 lines
4.2 KiB
TypeScript
import { listSessions, PeppaError, STEP_EVENT, projectForUser, resolveTask, userProjectIds, type Project, type TaskRef, discussionUnread } from "@peppa/core";
|
||
import { events, projects, questions, sessions, tasks } from "@peppa/db";
|
||
import { formatTaskKey } from "@peppa/shared";
|
||
import { and, asc, count, desc, eq, inArray, isNull, ne } 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) {
|
||
// PEP-34: board order (position), archived stories hidden.
|
||
const ts = await db.select().from(tasks).where(and(eq(tasks.projectId, projectId), isNull(tasks.archivedAt))).orderBy(asc(tasks.position), asc(tasks.number));
|
||
return { 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 });
|
||
// Steps of agents (PEP-33) have their own live feed.
|
||
const evs = await db.select().from(events).where(and(eq(events.taskId, taskId), ne(events.type, STEP_EVENT))).orderBy(desc(events.id)).limit(100);
|
||
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). */
|
||
/** 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; chats: number }> {
|
||
const ids = await userProjectIds(db, userId);
|
||
if (!ids.length) return { questions: 0, waitingRunners: 0, chats: 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")));
|
||
// PEP-106: messages of other people in the projects' discussions the user has not read.
|
||
const chats = [...(await discussionUnread(db, userId)).values()].reduce((a, b) => a + b, 0);
|
||
return { questions: q?.n ?? 0, waitingRunners: r?.n ?? 0, chats };
|
||
}
|
||
|
||
/** 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(and(eq(events.sessionId, sessionId), ne(events.type, STEP_EVENT))).orderBy(desc(events.id)).limit(limit);
|
||
}
|