| 1 | /** |
| 2 | * What the docs service tells the rest of g1t: `doc.page.*` events on the |
| 3 | * bus (packages/contracts events.ts), through the EVENTS binding when it |
| 4 | * has one. Published with no `repoId`, so a page never reaches a |
| 5 | * repository's timeline or webhooks. Never throws: an event that can't be |
| 6 | * told is logged, and the change it is about still happened. |
| 7 | */ |
| 8 | import { eventsClient, type DocPageEventData, type EventPayloads, type ServiceBinding } from "@g1t/contracts"; |
| 9 | |
| 10 | type DocEventType = "doc.page.created" | "doc.page.updated" | "doc.page.archived" | "doc.page.stale"; |
| 11 | |
| 12 | /** The id in a member key (`user:<id>`, `agent:<id>`), as an event's actor. */ |
| 13 | export function actorOf(key: string | null | undefined): string | null { |
| 14 | if (!key) return null; |
| 15 | const at = key.indexOf(":"); |
| 16 | return at > 0 ? key.slice(at + 1) || null : null; |
| 17 | } |
| 18 | |
| 19 | export async function publishDocEvent<T extends DocEventType>(events: ServiceBinding | undefined, type: T, data: EventPayloads[T] & DocPageEventData, actor: string | null): Promise<void> { |
| 20 | if (!events) return; |
| 21 | try { |
| 22 | await eventsClient(events).publish([{ type, source: "docs", repoId: null, actor: actorOf(actor), data } as never]); |
| 23 | } catch (error) { |
| 24 | console.error("docs could not publish", type, String(error)); |
| 25 | } |
| 26 | } |