// Alert definitions and notice targets share the server's vocabulary. export interface NotificationTarget { module: string; id: string; tab?: string; } /** Notice kinds match the server's vocabulary. */ export const NOTIF_KIND_ALERT = "alert"; export const NOTIF_KIND_AUTOMATION = "automation"; export const NOTIF_KIND_SHARE = "share"; /** Shared destination modules and the Agents Runs tab. */ export const TARGET_MODULE_DATABASE = "database"; export const TARGET_MODULE_AUTOMATION = "automation"; export const TARGET_TAB_RUNS = "runs"; export interface Alert { id: string; viewId: string; topic: string; owner: string; label: string; createdAt: string; /** How many records are in its remembered set right now. */ matched: number; seeded: boolean; lastRunAt: string; lastError: string; } export interface BellNotice { id: string; kind: string; count: number; label: string; alertLabel: string; read: boolean; target: NotificationTarget | null; link: string; } export interface BellInbox { items: BellNotice[]; unread: number } const str = (v: unknown): string => (typeof v === "string" ? v : ""); const num = (v: unknown): number => (typeof v === "number" && isFinite(v) ? v : 0); export const emptyBellInbox = (): BellInbox => ({ items: [], unread: 0 }); function notificationTarget(row: Record): NotificationTarget | null { if (row.target && typeof row.target === "object" && !Array.isArray(row.target)) { const target = row.target as Record; return str(target.module) && str(target.id) ? { module: str(target.module), id: str(target.id), ...(str(target.tab) ? { tab: str(target.tab) } : {}), ...(str(target.application) ? { application: str(target.application) } : {}), } : null; } const topic = str(row.topic), key = str(row.alertId), view = str(row.viewId); if (topic === TARGET_MODULE_AUTOMATION && key) return { module: topic, id: key, tab: TARGET_TAB_RUNS }; if (topic === NOTIF_KIND_SHARE && /^(?:customer_data|product_data|ut_[A-Za-z0-9_]+)$/.test(key)) { return { module: TARGET_MODULE_DATABASE, id: key, ...(str(row.rowId) ? { tab: str(row.rowId) } : {}) }; } const database = topic === "customer" ? "customer_data" : topic === "product" ? "product_data" : /^ut_[A-Za-z0-9_]+$/.test(topic) ? topic : ""; return database ? { module: TARGET_MODULE_DATABASE, id: database, ...(view ? { tab: view } : {}) } : null; } /** Keep delivery kinds open while deriving unread state from the adopted items. */ export function parseInbox(body: unknown): BellInbox { const value = body as { items?: unknown } | null; if (!Array.isArray(value?.items)) throw new Error("Alerts could not be loaded"); const items: BellNotice[] = []; for (const raw of value.items) { if (!raw || typeof raw !== "object" || Array.isArray(raw)) continue; const row = raw as Record; if (!str(row.id)) continue; items.push({ id: str(row.id), kind: str(row.kind) || str(row.topic), label: str(row.label), alertLabel: str(row.alertLabel), count: typeof row.count === "number" && Number.isSafeInteger(row.count) && row.count > 0 ? row.count : 0, read: row.read === true, target: notificationTarget(row), link: str(row.link), }); } return { items, unread: items.filter((item) => !item.read).length }; } export { parseInbox as parseBellInbox }; /** `GET /alerts` → the alert list, fail-closed. */ export function parseAlerts(body: unknown): Alert[] { const b = (body && typeof body === "object" ? body : {}) as Record; const raw = Array.isArray(b.alerts) ? b.alerts : []; const out: Alert[] = []; for (const item of raw) { if (!item || typeof item !== "object") continue; const a = item as Record; const id = str(a.id); if (!id) continue; out.push({ id, viewId: str(a.viewId), topic: str(a.topic), owner: str(a.owner), label: str(a.label) || "Untitled alert", createdAt: str(a.createdAt), matched: num(a.matched), seeded: a.seeded === true, lastRunAt: str(a.lastRunAt), lastError: str(a.lastError), }); } return out; } /** * The shell↔rail channel for "make an alert out of this view" — the view rail * raises it, the frame (which knows the current route, and therefore the topic) * answers. Same reason as C-SHARE's event: the rail is host-neutral and cannot * import the shell, and it does not know its own scope key. */ export const ALERT_CREATE_EVENT = "aios:alert-create"; export interface AlertCreateRequest { viewId: string; label: string; } export function parseAlertCreate(detail: unknown): AlertCreateRequest | null { if (!detail || typeof detail !== "object") return null; const d = detail as Record; const viewId = str(d.viewId).trim(); if (!viewId) return null; return { viewId, label: str(d.label).trim() || viewId }; }