// PEP-48: the journal — events with their authors (person + channel, agent, Peppa itself). import { m as tr } from "@peppa/shared/i18n"; import { events, oauthClients, projects, tasks, users, type Db } from "@peppa/db"; import { formatTaskKey } from "@peppa/shared"; import { and, desc, eq, inArray, lt, type SQL } from "drizzle-orm"; import { actorColumns, type ActorChannel, type ActorKind } from "./actor.ts"; import { historyJournal } from "./history.ts"; /** An event done by the current actor (settings changes and other actions without a session). */ export async function logEvent(db: Db, e: { projectId: number; taskId?: number | null; sessionId?: number | null; type: string; payload?: unknown }) { const [row] = await db .insert(events) .values({ projectId: e.projectId, taskId: e.taskId ?? null, sessionId: e.sessionId ?? null, type: e.type, payload: e.payload ?? null, ...actorColumns(e.sessionId) }) .returning(); return row!; } export const ACTOR_KIND_NAMES: Record = { user: tr("человек"), agent: tr("агент"), system: tr("FORGE") }; export const ACTOR_CHANNEL_NAMES: Record = { web: tr("веб"), telegram: "Telegram", mcp: "MCP", cli: "CLI", // glossary-ignore: журнал читает инженер terminal: tr("терминал"), // glossary-ignore: журнал читает инженер hook: tr("хук"), watchdog: tr("вачдог"), gitea: "Gitea", }; export interface JournalEntry { id: number; type: string; payload: unknown; createdAt: Date; projectId: number; projectKey: string; taskId: number | null; taskKey: string | null; sessionId: number | null; actorKind: ActorKind | null; actorChannel: ActorChannel | null; actorLogin: string | null; actorClient: string | null; } export interface JournalFilter { /** Restrict to these projects (members' view); undefined = all (superadmin). */ projectIds?: number[]; projectId?: number; taskId?: number; actorKind?: ActorKind; channel?: ActorChannel; /** Event types to leave out (noisy hook events in the journal). */ excludeTypes?: string[]; before?: number; limit?: number; } /** Hook/watchdog chatter that is not «someone did something». */ export const JOURNAL_NOISE = ["turn_end", "notification", "claude_session", "claude_session_end", "input_sent", "auto_continue", "awaiting_review", "awaiting_automerge"]; export async function listJournal(db: Db, f: JournalFilter = {}): Promise { if (f.projectIds && !f.projectIds.length) return []; const where: SQL[] = []; if (f.projectIds) where.push(inArray(events.projectId, f.projectIds)); if (f.projectId != null) where.push(eq(events.projectId, f.projectId)); if (f.taskId != null) where.push(eq(events.taskId, f.taskId)); if (f.actorKind) where.push(eq(events.actorKind, f.actorKind)); if (f.channel) where.push(eq(events.actorChannel, f.channel)); if (f.before) where.push(lt(events.id, f.before)); const rows = await db .select({ e: events, projectKey: projects.key, taskNumber: tasks.number, login: users.login, client: oauthClients.name }) .from(events) .innerJoin(projects, eq(events.projectId, projects.id)) .leftJoin(tasks, eq(events.taskId, tasks.id)) .leftJoin(users, eq(events.actorUserId, users.id)) .leftJoin(oauthClients, eq(events.actorClientId, oauthClients.id)) .where(and(...where)) .orderBy(desc(events.id)) .limit(Math.min(f.limit ?? 100, 500) * (f.excludeTypes?.length ? 3 : 1)); const skip = new Set(f.excludeTypes ?? []); const limit = Math.min(f.limit ?? 100, 500); const fresh: JournalEntry[] = rows .filter((r) => !skip.has(r.e.type)) .slice(0, limit) .map((r) => ({ id: r.e.id, type: r.e.type, payload: r.e.payload, createdAt: r.e.createdAt, projectId: r.e.projectId, projectKey: r.projectKey, taskId: r.e.taskId, taskKey: r.taskNumber != null ? formatTaskKey(r.projectKey, r.taskNumber) : null, sessionId: r.e.sessionId, actorKind: r.e.actorKind, actorChannel: r.e.actorChannel, actorLogin: r.login, actorClient: r.client ?? r.e.actorClientId, })); // PEP-221: older history lives in history.db; rows still in both places are taken from the main base. if (fresh.length >= limit) return fresh; const seen = new Set(fresh.map((e) => e.id)); const old = historyJournal({ ...f, limit }) .filter((e) => !seen.has(e.id)) .map( (e): JournalEntry => ({ id: e.id, type: e.type, payload: e.payload, createdAt: e.createdAt, projectId: e.projectId, projectKey: e.projectKey, taskId: e.taskId, taskKey: e.taskKey, sessionId: e.sessionId, actorKind: e.actorKind as ActorKind | null, actorChannel: e.actorChannel as ActorChannel | null, actorLogin: e.actorLogin, actorClient: e.actorClient, }), ); return [...fresh, ...old].sort((a, b) => b.id - a.id).slice(0, limit); } /** «anna · веб», «pm · MCP (ChatGPT)», «агент · раннер #12», «FORGE · вачдог». Null for pre-PEP-48 events. */ export function actorLabel(e: Pick): string | null { if (!e.actorKind) return null; const ch = e.actorChannel ? ACTOR_CHANNEL_NAMES[e.actorChannel] : null; if (e.actorKind === "user") return [e.actorLogin ?? tr("человек"), ch && `${ch}${e.actorClient ? ` (${e.actorClient})` : ""}`].filter(Boolean).join(" · "); // glossary-ignore: журнал читает инженер if (e.actorKind === "agent") return e.sessionId ? `${tr("агент")} · ${tr("раннер #{id}", { id: e.sessionId })}` : tr("агент"); return ch ? `${tr("FORGE")} · ${ch}` : tr("FORGE"); }