File size: 11,285 Bytes
518343a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { useStandardQuery } from '@szl-holdings/api-client-react';
import {
  AlertTriangle,
  CheckCircle2,
  Clock,
  ExternalLink,
  ShieldAlert,
  ShieldCheck,
  ShieldX,
} from 'lucide-react';
import { Link } from 'wouter';

const BASE = import.meta.env.BASE_URL.replace(/\/$/, '');

type Decision = 'allow' | 'require-approval' | 'require-dual-approval' | 'block';

interface RecentRow {
  id: number;
  requestId: string;
  agentId: string | null;
  tier: string;
  action: string;
  toolId: string | null;
  decision: Decision;
  reason: string | null;
  decidedAt: string;
  traceId: string | null;
  traceDomain: string | null;
  approvalStatus: string | null;
  approvalType: string | null;
  approvalExpiresAt: string | null;
}

interface SummaryResponse {
  data: {
    windowMinutes: number;
    since: string;
    counts: {
      allow: number;
      requireApproval: number;
      requireDualApproval: number;
      block: number;
      total: number;
    };
    pendingApprovals: number;
    recent: RecentRow[];
  };
}

function timeAgo(iso?: string | null): string {
  if (!iso) return '—';
  const t = new Date(iso).getTime();
  if (Number.isNaN(t)) return iso;
  const s = Math.max(1, Math.floor((Date.now() - t) / 1000));
  if (s < 60) return `${s}s ago`;
  const m = Math.floor(s / 60);
  if (m < 60) return `${m}m ago`;
  const h = Math.floor(m / 60);
  if (h < 24) return `${h}h ago`;
  const d = Math.floor(h / 24);
  return `${d}d ago`;
}

function decisionLabel(d: Decision): string {
  switch (d) {
    case 'allow':
      return 'allow';
    case 'require-approval':
      return 'approval';
    case 'require-dual-approval':
      return 'dual approval';
    case 'block':
      return 'denied';
  }
}

const DECISION_STYLE: Record<Decision, { fg: string; bg: string; border: string }> = {
  allow: { fg: '#22c55e', bg: 'rgba(34,197,94,0.10)', border: 'rgba(34,197,94,0.30)' },
  'require-approval': {
    fg: '#d4a054',
    bg: 'rgba(212,160,84,0.10)',
    border: 'rgba(212,160,84,0.30)',
  },
  'require-dual-approval': {
    fg: '#f97316',
    bg: 'rgba(249,115,22,0.10)',
    border: 'rgba(249,115,22,0.30)',
  },
  block: { fg: '#ef4444', bg: 'rgba(239,68,68,0.10)', border: 'rgba(239,68,68,0.30)' },
};

function CountBlock({
  label,
  value,
  Icon,
  color,
}: {
  label: string;
  value: number;
  Icon: typeof ShieldCheck;
  color: string;
}) {
  return (
    <div
      className="flex items-center gap-3 px-3 py-2 rounded-lg flex-1 min-w-[120px]"
      style={{
        background: 'var(--color-bg-elevated)',
        border: '1px solid var(--color-surface-border)',
      }}
    >
      <div
        className="w-8 h-8 rounded flex items-center justify-center"
        style={{ background: `${color}14`, border: `1px solid ${color}30` }}
      >
        <Icon className="w-4 h-4" style={{ color }} />
      </div>
      <div className="flex flex-col min-w-0">
        <span
          className="text-[18px] font-bold leading-tight"
          style={{ color: 'var(--color-fg-primary)' }}
        >
          {value.toLocaleString()}
        </span>
        <span
          className="text-[9px] font-mono uppercase tracking-wider truncate"
          style={{ color: 'var(--color-fg-muted)' }}
        >
          {label}
        </span>
      </div>
    </div>
  );
}

export function GuardianDecisionsTile() {
  const q = useStandardQuery<SummaryResponse>({
    queryKey: ['guardian', 'decisions-summary', '1h'],
    queryFn: async () => {
      const res = await fetch(`${BASE}/api/guardian/decisions/summary?windowMinutes=60&limit=8`, {
        credentials: 'include',
        headers: { 'Content-Type': 'application/json' },
      });
      if (!res.ok) {
        const text = await res.text().catch(() => '');
        throw new Error(`HTTP ${res.status}: ${text || res.statusText}`);
      }
      return res.json() as Promise<SummaryResponse>;
    },
    refetchInterval: 30_000,
  });

  const data = q.data?.data;
  const counts = data?.counts ?? {
    allow: 0,
    requireApproval: 0,
    requireDualApproval: 0,
    block: 0,
    total: 0,
  };
  const pending = data?.pendingApprovals ?? 0;
  const recent = data?.recent ?? [];

  return (
    <div
      className="rounded-xl overflow-hidden"
      style={{
        backgroundColor: 'var(--color-bg-elevated)',
        border: '1px solid var(--color-surface-border)',
        padding: '20px',
      }}
    >
      <div className="flex items-center justify-between mb-4 flex-wrap gap-2">
        <div className="flex items-center gap-2">
          <ShieldCheck className="w-4 h-4" style={{ color: '#d4a054' }} />
          <span
            className="text-xs font-bold uppercase tracking-widest"
            style={{ color: 'var(--color-fg-muted)' }}
          >
            Guardian Decisions
          </span>
          <span
            className="text-[9px] font-mono px-1.5 py-0.5 rounded"
            style={{
              color: 'var(--color-fg-muted)',
              background: 'rgba(255,255,255,0.04)',
              border: '1px solid var(--color-surface-border)',
            }}
          >
            last 60m
          </span>
        </div>
        <Link
          href={`${BASE}/operations/guardian/approvals`}
          className="text-[10px] font-bold uppercase tracking-wider px-2 py-1 rounded inline-flex items-center gap-1"
          style={{
            color: '#d4a054',
            backgroundColor: 'rgba(212,160,84,0.10)',
            border: '1px solid rgba(212,160,84,0.30)',
          }}
        >
          Open Console <ExternalLink className="w-3 h-3" />
        </Link>
      </div>

      {q.isLoading ? (
        <div
          className="text-[11px] font-mono py-6 text-center"
          style={{ color: 'var(--color-fg-muted)' }}
        >
          Loading…
        </div>
      ) : q.error ? (
        <div
          className="rounded p-3 text-[11px] font-mono flex items-start gap-2"
          style={{
            background: 'rgba(239,68,68,0.08)',
            border: '1px solid rgba(239,68,68,0.25)',
            color: '#ef4444',
          }}
        >
          <AlertTriangle className="w-3.5 h-3.5 mt-0.5 shrink-0" />
          <div>
            <div className="font-semibold">Failed to load Guardian decisions</div>
            <div className="opacity-80 mt-0.5">{(q.error as Error).message}</div>
          </div>
        </div>
      ) : (
        <>
          <div className="flex flex-wrap gap-2 mb-4">
            <CountBlock label="Allowed" value={counts.allow} Icon={CheckCircle2} color="#22c55e" />
            <CountBlock
              label="Approval"
              value={counts.requireApproval + counts.requireDualApproval}
              Icon={ShieldAlert}
              color="#d4a054"
            />
            <CountBlock label="Blocked" value={counts.block} Icon={ShieldX} color="#ef4444" />
            <CountBlock label="Awaiting you" value={pending} Icon={Clock} color="#8b7ac8" />
          </div>

          <div
            className="text-[9px] font-mono uppercase tracking-widest mb-2"
            style={{ color: 'var(--color-fg-muted)' }}
          >
            Recent denials & pending approvals
          </div>

          {recent.length === 0 ? (
            <div
              className="text-[11px] font-mono py-4 text-center rounded"
              style={{
                color: 'var(--color-fg-muted)',
                border: '1px dashed var(--color-surface-border)',
              }}
            >
              No blocked or pending-approval decisions in the last hour.
            </div>
          ) : (
            <div className="flex flex-col gap-1.5">
              {recent.map((row) => {
                const ds = DECISION_STYLE[row.decision];
                const traceHref = row.traceId
                  ? `${BASE}/cognitive/traces?trace=${encodeURIComponent(row.traceId)}`
                  : null;
                return (
                  <div
                    key={row.id}
                    className="flex items-start gap-2 px-2.5 py-1.5 rounded"
                    style={{
                      background: 'rgba(255,255,255,0.015)',
                      border: '1px solid var(--color-surface-border)',
                    }}
                  >
                    <span
                      className="text-[8px] font-mono font-semibold tracking-wider px-1.5 py-px rounded uppercase shrink-0 mt-0.5"
                      style={{ color: ds.fg, background: ds.bg, border: `1px solid ${ds.border}` }}
                    >
                      {decisionLabel(row.decision)}
                    </span>
                    <div className="flex-1 min-w-0">
                      <div
                        className="text-[11px] font-semibold truncate"
                        style={{ color: 'var(--color-fg-primary)' }}
                        title={row.action}
                      >
                        {row.action}
                      </div>
                      <div
                        className="text-[10px] font-mono truncate"
                        style={{ color: 'var(--color-fg-muted)' }}
                      >
                        {row.agentId ?? 'anon-agent'}
                        {row.toolId ? ` · ${row.toolId}` : ''}
                        {row.traceDomain ? ` · ${row.traceDomain}` : ''}
                        {' · '}
                        <span style={{ opacity: 0.7 }}>{timeAgo(row.decidedAt)}</span>
                      </div>
                      {row.reason && (
                        <div
                          className="text-[10px] mt-0.5 truncate"
                          style={{ color: 'var(--color-fg-muted)' }}
                          title={row.reason}
                        >
                          {row.reason}
                        </div>
                      )}
                    </div>
                    {traceHref ? (
                      <Link
                        href={traceHref}
                        className="text-[9px] font-mono uppercase tracking-wider px-1.5 py-0.5 rounded inline-flex items-center gap-1 shrink-0"
                        style={{
                          color: '#8b7ac8',
                          background: 'rgba(139,122,200,0.10)',
                          border: '1px solid rgba(139,122,200,0.30)',
                        }}
                        title={`Trace ${row.traceId}`}
                      >
                        Trace <ExternalLink className="w-2.5 h-2.5" />
                      </Link>
                    ) : (
                      <span
                        className="text-[9px] font-mono uppercase tracking-wider px-1.5 py-0.5 rounded shrink-0"
                        style={{
                          color: 'var(--color-fg-muted)',
                          border: '1px solid var(--color-surface-border)',
                        }}
                        title="No trace recorded for this decision"
                      >
                        no trace
                      </span>
                    )}
                  </div>
                );
              })}
            </div>
          )}
        </>
      )}
    </div>
  );
}

export default GuardianDecisionsTile;