57 lines · 2.9 KBRaw
| 1 | import type { Delivery } from "@refs/protocol"; |
| 2 | import type { RefUpdate } from "./git.ts"; |
| 3 | |
| 4 | export interface DeliverableRow { |
| 5 | id: number; |
| 6 | kind: string; |
| 7 | data: string; |
| 8 | session_id: string | null; |
| 9 | actor: string | null; |
| 10 | } |
| 11 | |
| 12 | export interface Recipient { |
| 13 | session: string; |
| 14 | /** The session's current branch, or null while it idles. */ |
| 15 | branchRef: string | null; |
| 16 | /** The repository's default branch, which every session's work merges into. */ |
| 17 | baseRef: string; |
| 18 | } |
| 19 | |
| 20 | export const DELIVERABLE_KINDS = ["repo.pushed", "session.problem", "session.instructed", "agent.mentioned", "agent.run_finished", "session.ended"] as const; |
| 21 | |
| 22 | /** |
| 23 | * The frames one session receives for one event on its repository's stream: pushes by anyone else to its current |
| 24 | * branch or to the default branch while it works, problems with its own commits, and the instructions, mentions and stop addressed to it. |
| 25 | */ |
| 26 | export function framesFor(row: DeliverableRow, to: Recipient): Delivery[] { |
| 27 | const id = String(row.id); |
| 28 | if (row.kind === "repo.pushed") { |
| 29 | if (to.branchRef === null || row.session_id === to.session) return []; |
| 30 | const { updates } = JSON.parse(row.data) as { updates: RefUpdate[] }; |
| 31 | const by = { session: row.session_id, actor: row.actor ?? "unknown" }; |
| 32 | return updates |
| 33 | .filter((update) => update.ref === to.branchRef || (update.ref === to.baseRef && update.to !== null)) |
| 34 | .map((update) => ({ type: "event", id, event: "repo.pushed", ref: update.ref, from: update.from, to: update.to, by, base: update.ref === to.baseRef })); |
| 35 | } |
| 36 | if (row.kind === "session.problem") { |
| 37 | const { commit, message } = JSON.parse(row.data) as { commit: string; message: string }; |
| 38 | return row.session_id === to.session ? [{ type: "problem", id, commit, message }] : []; |
| 39 | } |
| 40 | if (row.kind === "session.instructed") { |
| 41 | const { session, body } = JSON.parse(row.data) as { session: string; body: string }; |
| 42 | return session === to.session ? [{ type: "instruction", id, body, by: { person: row.actor ?? "unknown" } }] : []; |
| 43 | } |
| 44 | if (row.kind === "agent.mentioned") { |
| 45 | const { session, pull, title, body, assigned, run } = JSON.parse(row.data) as { session: string; pull?: number; title: string; body: string; assigned: boolean; run?: boolean }; |
| 46 | return session === to.session && pull !== undefined ? [{ type: "mention", id, pull, title, body, assigned, run: run === true, by: { person: row.actor ?? "unknown" } }] : []; |
| 47 | } |
| 48 | if (row.kind === "agent.run_finished") { |
| 49 | const { session, run, mention, ok, summary } = JSON.parse(row.data) as { session: string; run: string; mention: string | null; ok: boolean; summary: string }; |
| 50 | return session === to.session && run !== session ? [{ type: "run", id, mention, ok, summary }] : []; |
| 51 | } |
| 52 | if (row.kind === "session.ended") { |
| 53 | const { session, reason } = JSON.parse(row.data) as { session: string; reason: string }; |
| 54 | return session === to.session ? [{ type: "stop", id, reason }] : []; |
| 55 | } |
| 56 | return []; |
| 57 | } |