File size: 4,990 Bytes
89af3b2
4cfab62
 
 
 
 
 
 
89af3b2
4cfab62
 
 
 
89af3b2
4cfab62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dec006b
 
 
 
 
 
 
 
 
 
 
 
 
4cfab62
 
 
dec006b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cfab62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// 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<string, unknown>): NotificationTarget | null {
  if (row.target && typeof row.target === "object" && !Array.isArray(row.target)) {
    const target = row.target as Record<string, unknown>;
    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<string, unknown>;
    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<string, unknown>;
  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<string, unknown>;
    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<string, unknown>;
  const viewId = str(d.viewId).trim();
  if (!viewId) return null;
  return { viewId, label: str(d.label).trim() || viewId };
}