g1t/packages/contracts/src/events.ts

63 lines2,369 bytesCodeBlame

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

Initial g1t: services, event bus, intents and attempts1/**
2 * Every state change in g1t is published as an event. Services react to
3 * each other through events rather than direct calls, and the same stream
4 * feeds timelines, webhooks and automations.
5 *
6 * The envelope follows CloudEvents: `type` says what happened, `subject`
7 * says to what, `data` is the type-specific payload.
8 */
9export type EventPayloads = {
10 "repo.created": { repoId: string; namespace: string; name: string; isPrivate: boolean };
11 "repo.forked": { repoId: string; sourceRepoId: string; attemptId: string };
12 /** `after` is the commit the ref points to once the push has landed. */
13 "git.push": { repoId: string; ref: string; after: string };
14 "intent.opened": { intentId: string; repoId: string; number: number; title: string };
15 "intent.closed": { intentId: string; repoId: string; reason: "shipped" | "withdrawn" };
16 "attempt.started": { attemptId: string; intentId: string; repoId: string; agent: string };
17 "attempt.updated": { attemptId: string; intentId: string; repoId: string; status: string };
18 "attempt.submitted": { attemptId: string; intentId: string; repoId: string };
19 "session.appended": { attemptId: string; sessionId: string; count: number };
20};
21
22export type EventType = keyof EventPayloads;
23
24export type G1tEvent<T extends EventType = EventType> = {
25 [K in T]: {
26 id: string;
27 type: K;
28 /** The service that published it. */
29 source: string;
30 /** Milliseconds since the epoch. */
31 time: number;
32 /** The repo the event concerns, used to scope timelines and deliveries. */
33 repoId: string | null;
34 /** The user or agent that caused it, if any. */
35 actor: string | null;
36 data: EventPayloads[K];
37 };
38}[T];
39
40/** What a publisher supplies; the bus fills in `id` and `time`. */
41export type NewEvent<T extends EventType = EventType> = {
42 [K in T]: Omit<G1tEvent<K>, "id" | "time">;
43}[T];
44
45export type EventQuery = {
46 repoId?: string;
47 types?: EventType[];
48 /** Return events older than this event id. */
49 before?: string;
50 limit?: number;
51};
52
53/** The event bus and its durable log. */
54export interface EventsApi {
55 publish(events: NewEvent[]): Promise<void>;
56 /** Newest first. */
57 list(query: EventQuery): Promise<G1tEvent[]>;
58}
59
60/** Implemented by services that consume events from the bus. */
61export interface EventSubscriber {
62 onEvents(events: G1tEvent[]): Promise<void>;
63}