import type { Delivery } from "@refs/protocol"; import type { RefUpdate } from "./git.ts"; export interface DeliverableRow { id: number; kind: string; data: string; session_id: string | null; actor: string | null; } export interface Recipient { session: string; /** The session's current branch, or null while it idles. */ branchRef: string | null; } export const DELIVERABLE_KINDS = ["repo.pushed", "session.problem", "session.instructed", "agent.mentioned", "agent.run_finished", "session.ended"] as const; /** * The frames one session receives for one event on its repository's stream: pushes to its current * branch by anyone else, problems with its own commits, and the instructions, mentions and stop addressed to it. */ export function framesFor(row: DeliverableRow, to: Recipient): Delivery[] { const id = String(row.id); if (row.kind === "repo.pushed") { if (to.branchRef === null || row.session_id === to.session) return []; const { updates } = JSON.parse(row.data) as { updates: RefUpdate[] }; const by = { session: row.session_id, actor: row.actor ?? "unknown" }; return updates.filter((update) => update.ref === to.branchRef).map((update) => ({ type: "event", id, event: "repo.pushed", ref: update.ref, from: update.from, to: update.to, by })); } if (row.kind === "session.problem") { const { commit, message } = JSON.parse(row.data) as { commit: string; message: string }; return row.session_id === to.session ? [{ type: "problem", id, commit, message }] : []; } if (row.kind === "session.instructed") { const { session, body } = JSON.parse(row.data) as { session: string; body: string }; return session === to.session ? [{ type: "instruction", id, body, by: { person: row.actor ?? "unknown" } }] : []; } if (row.kind === "agent.mentioned") { const { session, pull, title, body, assigned, run } = JSON.parse(row.data) as { session: string; pull?: number; title: string; body: string; assigned: boolean; run?: boolean }; return session === to.session && pull !== undefined ? [{ type: "mention", id, pull, title, body, assigned, run: run === true, by: { person: row.actor ?? "unknown" } }] : []; } if (row.kind === "agent.run_finished") { const { session, run, mention, ok, summary } = JSON.parse(row.data) as { session: string; run: string; mention: string | null; ok: boolean; summary: string }; return session === to.session && run !== session ? [{ type: "run", id, mention, ok, summary }] : []; } if (row.kind === "session.ended") { const { session, reason } = JSON.parse(row.data) as { session: string; reason: string }; return session === to.session ? [{ type: "stop", id, reason }] : []; } return []; }