@bevel-software/platform-core-frontend 0.19.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/core/CoreAppShell.tsx +6 -2
- package/src/modules/audit/__tests__/AuditLogPage.test.tsx +312 -0
- package/src/modules/audit/components/AuditEventsPanel.tsx +319 -0
- package/src/modules/audit/components/AuditLogPage.tsx +373 -0
- package/src/modules/audit/components/principal-grouping.ts +49 -0
- package/src/modules/audit/services/audit.api.ts +98 -0
- package/src/modules/settings/components/__tests__/SettingsLayout.test.tsx +2 -1
- package/src/modules/settings/settings-nav-items.tsx +11 -9
- package/src/modules/setup/components/SetupScreen.tsx +11 -0
- package/src/modules/setup/services/setup.api.ts +1 -1
- package/src/modules/toolbar/services/external-api-keys.api.ts +2 -0
- package/src/modules/workspace/components/FileViewer.tsx +6 -1
- package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +60 -0
- package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +50 -0
- package/src/modules/workspace/services/__tests__/workspace.api.test.ts +78 -1
- package/src/modules/admin/__tests__/ConnectionKeysPage.test.tsx +0 -266
- package/src/modules/admin/components/ConnectionKeysPage.tsx +0 -269
- package/src/modules/admin/components/connection-keys-grouping.ts +0 -43
- package/src/modules/admin/services/connection-keys.api.ts +0 -47
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { Bot, ChevronRight, KeyRound } from 'lucide-react';
|
|
3
|
+
import { PageShell } from '../../../shared/components/PageShell';
|
|
4
|
+
import { Dialog } from '../../../shared/components/Dialog';
|
|
5
|
+
import { Badge, Banner, Button } from '../../../shared/components';
|
|
6
|
+
import { cn, formatRelativeTime } from '../../../lib/utils';
|
|
7
|
+
import { GITHUB_LINK_KIND } from '../../../shared/marketplace-url';
|
|
8
|
+
import { useAdmin } from '../../admin/state/admin.context';
|
|
9
|
+
import { useAuth } from '../../auth/state/auth.context';
|
|
10
|
+
import { listPrincipals, revokeAgent, revokeKey, type AuditPrincipal } from '../services/audit.api';
|
|
11
|
+
import { groupByAccount, principalKey } from './principal-grouping';
|
|
12
|
+
import { AuditEventsPanel } from './AuditEventsPanel';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The shared formatter, plus the one word it deliberately leaves to the caller:
|
|
16
|
+
* a key that has never been used has no instant to describe.
|
|
17
|
+
*/
|
|
18
|
+
function formatRelative(ts: number | null): string {
|
|
19
|
+
return formatRelativeTime(ts) || 'never';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The exact instant, for a hover — the row shows the relative form. */
|
|
23
|
+
function formatAbsolute(ts: number | null): string | undefined {
|
|
24
|
+
return ts === null ? undefined : new Date(ts).toLocaleString();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** What a row is, in the words the page uses: an OAuth agent, a Claude link, or a hand-made key. */
|
|
28
|
+
function kindLabel(p: AuditPrincipal): string {
|
|
29
|
+
if (p.kind === 'agent') return 'Agent · OAuth';
|
|
30
|
+
return p.keyKind === GITHUB_LINK_KIND ? 'Claude link' : 'Connection key';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function eventsLabel(n: number): string {
|
|
34
|
+
if (n === 0) return 'No events yet';
|
|
35
|
+
return `${n.toLocaleString()} ${n === 1 ? 'event' : 'events'}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The Audit log (`/audit-log`): every agent and connection key connected to
|
|
40
|
+
* your account — or, for admins, to every account — with when it connected,
|
|
41
|
+
* when it was last used and how much it has called; a revoke for each; and,
|
|
42
|
+
* under each row when opened, the list of what it called, newest first.
|
|
43
|
+
*
|
|
44
|
+
* It replaced the admin-only Connection keys overview. Keys are still minted
|
|
45
|
+
* and deleted for good on External agent access; here they are listed,
|
|
46
|
+
* watched and revoked beside the OAuth agents, which had no page at all.
|
|
47
|
+
*
|
|
48
|
+
* Revoking keeps the row (dimmed, marked with who revoked it) so its history
|
|
49
|
+
* stays readable. Revoked rows are hidden by default — on a busy deployment
|
|
50
|
+
* they outnumber the live ones — and a toggle brings them back.
|
|
51
|
+
*/
|
|
52
|
+
export function AuditLogPage() {
|
|
53
|
+
const { user } = useAuth();
|
|
54
|
+
const { isAdmin, isAdminLoading } = useAdmin();
|
|
55
|
+
// The admin verdict decides which scope to ask for; asking before it has
|
|
56
|
+
// settled would fetch a member's list and then an admin's a moment later.
|
|
57
|
+
const settled = isAdminLoading !== true;
|
|
58
|
+
|
|
59
|
+
const [principals, setPrincipals] = useState<AuditPrincipal[] | null>(null);
|
|
60
|
+
const [error, setError] = useState<string | null>(null);
|
|
61
|
+
const [showRevoked, setShowRevoked] = useState(false);
|
|
62
|
+
const [query, setQuery] = useState('');
|
|
63
|
+
// Which rows are open, and which have EVER been opened: a panel stays
|
|
64
|
+
// mounted after its first open (hidden when collapsed), so re-expanding
|
|
65
|
+
// costs no request and keeps its filters and loaded pages.
|
|
66
|
+
const [expanded, setExpanded] = useState<Record<string, boolean>>({});
|
|
67
|
+
const [opened, setOpened] = useState<Record<string, boolean>>({});
|
|
68
|
+
// The row awaiting revoke confirmation; non-null drives the confirm
|
|
69
|
+
// Dialog. `revoking` keeps the confirm open while the request is in flight.
|
|
70
|
+
const [pendingRevoke, setPendingRevoke] = useState<AuditPrincipal | null>(null);
|
|
71
|
+
const [revoking, setRevoking] = useState(false);
|
|
72
|
+
|
|
73
|
+
// Generation of the newest load. Two revokes in quick succession start two
|
|
74
|
+
// reloads, and the older one can land last — carrying a row the newer one
|
|
75
|
+
// already saw revoked. Only the latest load may write `principals`.
|
|
76
|
+
const loadGen = useRef(0);
|
|
77
|
+
const refresh = useCallback(() => {
|
|
78
|
+
const gen = ++loadGen.current;
|
|
79
|
+
listPrincipals(isAdmin ? 'all' : 'me')
|
|
80
|
+
.then((rows) => {
|
|
81
|
+
if (gen !== loadGen.current) return;
|
|
82
|
+
setPrincipals(rows);
|
|
83
|
+
setError(null);
|
|
84
|
+
})
|
|
85
|
+
.catch((err) => {
|
|
86
|
+
if (gen !== loadGen.current) return;
|
|
87
|
+
setError(err instanceof Error ? err.message : "Couldn't load the audit log.");
|
|
88
|
+
// `principals` is deliberately left alone: a reload that fails keeps
|
|
89
|
+
// the rows it had, and a first load that fails stays `null` rather
|
|
90
|
+
// than rendering "nothing connected" for a deployment we cannot reach.
|
|
91
|
+
});
|
|
92
|
+
}, [isAdmin]);
|
|
93
|
+
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
if (settled) refresh();
|
|
96
|
+
}, [settled, refresh]);
|
|
97
|
+
|
|
98
|
+
const groups = useMemo(
|
|
99
|
+
() => (principals ? groupByAccount(principals, { includeRevoked: showRevoked, query: isAdmin ? query : '' }) : []),
|
|
100
|
+
[principals, showRevoked, query, isAdmin],
|
|
101
|
+
);
|
|
102
|
+
const liveCount = principals?.filter((p) => p.revokedAt === null).length ?? 0;
|
|
103
|
+
const revokedCount = (principals?.length ?? 0) - liveCount;
|
|
104
|
+
const accountCount = useMemo(() => new Set(principals?.map((p) => p.user.id)).size, [principals]);
|
|
105
|
+
|
|
106
|
+
const toggle = (key: string) => {
|
|
107
|
+
setExpanded((prev) => ({ ...prev, [key]: !prev[key] }));
|
|
108
|
+
setOpened((prev) => (prev[key] ? prev : { ...prev, [key]: true }));
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
async function confirmRevoke() {
|
|
112
|
+
if (!pendingRevoke || revoking) return;
|
|
113
|
+
setRevoking(true);
|
|
114
|
+
setError(null);
|
|
115
|
+
try {
|
|
116
|
+
const target = pendingRevoke;
|
|
117
|
+
if (target.kind === 'agent') await revokeAgent(target.id);
|
|
118
|
+
else await revokeKey(target.id);
|
|
119
|
+
// The server has revoked it; say so at once rather than waiting on the
|
|
120
|
+
// reload. If that reload fails, the row must not sit there looking live
|
|
121
|
+
// with a Revoke button — the agent holding it is already cut off.
|
|
122
|
+
const by: AuditPrincipal['revokedBy'] = user && target.user.id === user.id ? 'owner' : 'admin';
|
|
123
|
+
setPrincipals((prev) =>
|
|
124
|
+
prev
|
|
125
|
+
? prev.map((p) =>
|
|
126
|
+
p.kind === target.kind && p.id === target.id && p.revokedAt === null
|
|
127
|
+
? { ...p, revokedAt: Date.now(), revokedBy: by }
|
|
128
|
+
: p,
|
|
129
|
+
)
|
|
130
|
+
: prev,
|
|
131
|
+
);
|
|
132
|
+
setPendingRevoke(null);
|
|
133
|
+
refresh();
|
|
134
|
+
} catch (err) {
|
|
135
|
+
setPendingRevoke(null);
|
|
136
|
+
setError(err instanceof Error ? err.message : "Couldn't revoke this.");
|
|
137
|
+
} finally {
|
|
138
|
+
setRevoking(false);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const revokeVerb = (p: AuditPrincipal) => (p.kind === 'agent' ? 'Revoke access' : 'Revoke key');
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
<>
|
|
146
|
+
<PageShell title="Audit log" width="4xl" card={false}>
|
|
147
|
+
<div className="space-y-4">
|
|
148
|
+
<p className="text-xs text-ink-muted leading-snug max-w-[65ch]">
|
|
149
|
+
{isAdmin
|
|
150
|
+
? 'Every agent and connection key on this deployment, per account. Open one to see what it called and when. Revoking cuts the agent off immediately; the row stays so its history remains visible.'
|
|
151
|
+
: 'The agents and connection keys connected to your account. Open one to see what it called and when. Revoking cuts the agent off immediately; it can reconnect by signing in again.'}
|
|
152
|
+
</p>
|
|
153
|
+
|
|
154
|
+
{error && (
|
|
155
|
+
<Banner tone="danger" role="alert" className="text-detail">
|
|
156
|
+
{error}
|
|
157
|
+
</Banner>
|
|
158
|
+
)}
|
|
159
|
+
|
|
160
|
+
{principals !== null && (
|
|
161
|
+
<div className="flex flex-wrap items-center justify-between gap-x-3 gap-y-2 text-xs text-ink-muted">
|
|
162
|
+
<span>
|
|
163
|
+
{isAdmin && `${accountCount} ${accountCount === 1 ? 'account' : 'accounts'} · `}
|
|
164
|
+
{liveCount} live
|
|
165
|
+
{revokedCount > 0 && ` · ${revokedCount} revoked`}
|
|
166
|
+
</span>
|
|
167
|
+
<div className="flex flex-wrap items-center gap-3">
|
|
168
|
+
{isAdmin && (
|
|
169
|
+
<input
|
|
170
|
+
value={query}
|
|
171
|
+
onChange={(e) => setQuery(e.target.value)}
|
|
172
|
+
placeholder="Filter by person or agent"
|
|
173
|
+
aria-label="Filter by person or agent"
|
|
174
|
+
className="bg-white border border-line rounded-sm px-2 py-1 text-detail text-ink focus:outline-none focus:border-accent min-w-[200px]"
|
|
175
|
+
/>
|
|
176
|
+
)}
|
|
177
|
+
{revokedCount > 0 && (
|
|
178
|
+
<label className="flex items-center gap-1.5 cursor-pointer select-none">
|
|
179
|
+
<input
|
|
180
|
+
type="checkbox"
|
|
181
|
+
checked={showRevoked}
|
|
182
|
+
onChange={(e) => setShowRevoked(e.target.checked)}
|
|
183
|
+
/>
|
|
184
|
+
Show revoked
|
|
185
|
+
</label>
|
|
186
|
+
)}
|
|
187
|
+
</div>
|
|
188
|
+
</div>
|
|
189
|
+
)}
|
|
190
|
+
|
|
191
|
+
{principals === null ? (
|
|
192
|
+
error ? null : <div className="text-xs text-ink-muted">Loading…</div>
|
|
193
|
+
) : groups.length === 0 ? (
|
|
194
|
+
<div className="text-xs text-ink-muted">
|
|
195
|
+
{principals.length === 0
|
|
196
|
+
? isAdmin
|
|
197
|
+
? 'No agents or connection keys on this deployment.'
|
|
198
|
+
: 'Nothing connected to your account yet. Connect an agent from External agent access.'
|
|
199
|
+
: query.trim()
|
|
200
|
+
? 'Nothing matches that filter.'
|
|
201
|
+
: 'No live agents or connection keys.'}
|
|
202
|
+
</div>
|
|
203
|
+
) : (
|
|
204
|
+
<div className="space-y-5">
|
|
205
|
+
{groups.map((group) => {
|
|
206
|
+
const live = group.principals.filter((p) => p.revokedAt === null).length;
|
|
207
|
+
return (
|
|
208
|
+
<section key={group.user.id} aria-label={`Agents and keys for ${group.user.email}`}>
|
|
209
|
+
{isAdmin && (
|
|
210
|
+
<div className="flex items-baseline gap-2 px-1 pb-1.5">
|
|
211
|
+
<span className="text-sm font-medium truncate">{group.user.name}</span>
|
|
212
|
+
<span className="text-meta text-ink-muted truncate">{group.user.email}</span>
|
|
213
|
+
<span className="ml-auto text-meta text-ink-muted whitespace-nowrap">{live} live</span>
|
|
214
|
+
</div>
|
|
215
|
+
)}
|
|
216
|
+
<ul className="divide-y divide-line border border-line rounded-md bg-white shadow-card overflow-hidden">
|
|
217
|
+
{group.principals.map((p) => {
|
|
218
|
+
const key = principalKey(p);
|
|
219
|
+
const revoked = p.revokedAt !== null;
|
|
220
|
+
const open = !!expanded[key];
|
|
221
|
+
const panelId = `audit-events-${p.kind}-${p.id}`;
|
|
222
|
+
const Icon = p.kind === 'agent' ? Bot : KeyRound;
|
|
223
|
+
return (
|
|
224
|
+
<li key={key}>
|
|
225
|
+
{/* The WHOLE row opens the events, chevron at its far edge, the
|
|
226
|
+
way a disclosure row reads. A button cannot nest the Revoke
|
|
227
|
+
button, so the expand button is stretched over the row by
|
|
228
|
+
its ::after pseudo-element instead (padding, chevron and all)
|
|
229
|
+
and Revoke is raised above that overlay. `relative` stays on
|
|
230
|
+
THIS row div, not the <li>: the overlay must never cover the
|
|
231
|
+
events panel below. */}
|
|
232
|
+
<div
|
|
233
|
+
className={cn(
|
|
234
|
+
'relative flex items-center gap-3 px-3 py-2 text-sm hover:bg-surface-hover',
|
|
235
|
+
open && 'bg-surface-hover',
|
|
236
|
+
)}
|
|
237
|
+
>
|
|
238
|
+
<button
|
|
239
|
+
type="button"
|
|
240
|
+
onClick={() => toggle(key)}
|
|
241
|
+
aria-expanded={open}
|
|
242
|
+
// Only once the panel exists: a reference to an id that is
|
|
243
|
+
// not in the document is an invalid one, and the panel is
|
|
244
|
+
// mounted on the row's first open.
|
|
245
|
+
aria-controls={opened[key] ? panelId : undefined}
|
|
246
|
+
className={cn(
|
|
247
|
+
'flex flex-1 min-w-0 items-center gap-3 text-left',
|
|
248
|
+
"after:absolute after:inset-0 after:content-['']",
|
|
249
|
+
'focus-visible:outline-none focus-visible:after:outline-2 focus-visible:after:-outline-offset-2 focus-visible:after:outline-accent',
|
|
250
|
+
revoked && 'opacity-60',
|
|
251
|
+
)}
|
|
252
|
+
>
|
|
253
|
+
<span
|
|
254
|
+
aria-hidden
|
|
255
|
+
className="flex-none size-7 rounded-sm bg-sunken inline-flex items-center justify-center text-ink-muted"
|
|
256
|
+
>
|
|
257
|
+
<Icon size={15} />
|
|
258
|
+
</span>
|
|
259
|
+
<span className="flex-1 min-w-0">
|
|
260
|
+
<span className="flex items-center gap-2 font-medium">
|
|
261
|
+
<span className="truncate">{p.label}</span>
|
|
262
|
+
<Badge tone="outline">{kindLabel(p)}</Badge>
|
|
263
|
+
{revoked && (
|
|
264
|
+
<Badge tone={p.revokedBy === 'admin' ? 'danger' : 'neutral'}>
|
|
265
|
+
{p.revokedBy === 'admin' ? 'Revoked by an admin' : 'Disconnected by owner'}
|
|
266
|
+
</Badge>
|
|
267
|
+
)}
|
|
268
|
+
{/* The owner deleted it from their own pages; the row and its
|
|
269
|
+
events stay here, for admins, until retention prunes them. */}
|
|
270
|
+
{p.deletedAt != null && <Badge tone="outline">Deleted by owner</Badge>}
|
|
271
|
+
</span>
|
|
272
|
+
<span className="block text-meta text-ink-muted">
|
|
273
|
+
<span title={formatAbsolute(p.createdAt)}>
|
|
274
|
+
{p.kind === 'agent' ? 'Connected' : 'Created'} {formatRelative(p.createdAt)}
|
|
275
|
+
</span>
|
|
276
|
+
{' · '}
|
|
277
|
+
<span title={formatAbsolute(p.lastUsedAt)}>Last used {formatRelative(p.lastUsedAt)}</span>
|
|
278
|
+
{revoked && (
|
|
279
|
+
<>
|
|
280
|
+
{' · '}
|
|
281
|
+
<span title={formatAbsolute(p.revokedAt)}>Revoked {formatRelative(p.revokedAt)}</span>
|
|
282
|
+
</>
|
|
283
|
+
)}
|
|
284
|
+
{' · '}
|
|
285
|
+
{eventsLabel(p.eventCount)}
|
|
286
|
+
</span>
|
|
287
|
+
</span>
|
|
288
|
+
</button>
|
|
289
|
+
{!revoked && (
|
|
290
|
+
<Button
|
|
291
|
+
variant="danger"
|
|
292
|
+
size="sm"
|
|
293
|
+
className="relative z-10"
|
|
294
|
+
onClick={() => setPendingRevoke(p)}
|
|
295
|
+
title={
|
|
296
|
+
p.kind === 'agent'
|
|
297
|
+
? 'Revoke this agent’s access. It will have to sign in again to reconnect.'
|
|
298
|
+
: 'Revoke this key. The external agent using it will lose access.'
|
|
299
|
+
}
|
|
300
|
+
aria-label={`${revokeVerb(p)} for ${p.label} (${group.user.email})`}
|
|
301
|
+
>
|
|
302
|
+
{revokeVerb(p)}
|
|
303
|
+
</Button>
|
|
304
|
+
)}
|
|
305
|
+
<ChevronRight
|
|
306
|
+
aria-hidden
|
|
307
|
+
size={14}
|
|
308
|
+
className={cn('flex-none text-ink-faint transition-transform', open && 'rotate-90')}
|
|
309
|
+
/>
|
|
310
|
+
</div>
|
|
311
|
+
{opened[key] && (
|
|
312
|
+
<div
|
|
313
|
+
id={panelId}
|
|
314
|
+
hidden={!open}
|
|
315
|
+
className="border-t border-line bg-sunken px-3 py-3"
|
|
316
|
+
>
|
|
317
|
+
<AuditEventsPanel kind={p.kind} id={p.id} label={p.label} />
|
|
318
|
+
</div>
|
|
319
|
+
)}
|
|
320
|
+
</li>
|
|
321
|
+
);
|
|
322
|
+
})}
|
|
323
|
+
</ul>
|
|
324
|
+
</section>
|
|
325
|
+
);
|
|
326
|
+
})}
|
|
327
|
+
</div>
|
|
328
|
+
)}
|
|
329
|
+
</div>
|
|
330
|
+
</PageShell>
|
|
331
|
+
|
|
332
|
+
<Dialog
|
|
333
|
+
open={pendingRevoke !== null}
|
|
334
|
+
onClose={() => setPendingRevoke(null)}
|
|
335
|
+
title={pendingRevoke?.kind === 'agent' ? 'Revoke agent access' : 'Revoke connection key'}
|
|
336
|
+
size="sm"
|
|
337
|
+
busy={revoking}
|
|
338
|
+
footer={
|
|
339
|
+
<>
|
|
340
|
+
<Button variant="outline" onClick={() => setPendingRevoke(null)} disabled={revoking}>
|
|
341
|
+
Cancel
|
|
342
|
+
</Button>
|
|
343
|
+
<Button variant="danger" onClick={confirmRevoke} disabled={revoking}>
|
|
344
|
+
{revoking ? 'Revoking…' : pendingRevoke ? revokeVerb(pendingRevoke) : 'Revoke'}
|
|
345
|
+
</Button>
|
|
346
|
+
</>
|
|
347
|
+
}
|
|
348
|
+
>
|
|
349
|
+
<p className="text-xs text-ink leading-snug">
|
|
350
|
+
{pendingRevoke?.kind === 'agent' ? (
|
|
351
|
+
<>
|
|
352
|
+
Revoke <span className="font-medium">{pendingRevoke.label}</span>’s access for{' '}
|
|
353
|
+
<span className="font-medium">
|
|
354
|
+
{pendingRevoke.user.name} ({pendingRevoke.user.email})
|
|
355
|
+
</span>
|
|
356
|
+
? Every token it holds stops working now. It can reconnect only by signing in again through the
|
|
357
|
+
browser. Its history stays visible here.
|
|
358
|
+
</>
|
|
359
|
+
) : (
|
|
360
|
+
<>
|
|
361
|
+
Revoke <span className="font-medium">{pendingRevoke?.label}</span> belonging to{' '}
|
|
362
|
+
<span className="font-medium">
|
|
363
|
+
{pendingRevoke?.user.name} ({pendingRevoke?.user.email})
|
|
364
|
+
</span>
|
|
365
|
+
? Whatever is using it loses access immediately. The key stays listed so its history remains
|
|
366
|
+
visible; its owner can delete it for good from External agent access.
|
|
367
|
+
</>
|
|
368
|
+
)}
|
|
369
|
+
</p>
|
|
370
|
+
</Dialog>
|
|
371
|
+
</>
|
|
372
|
+
);
|
|
373
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { AuditPrincipal } from '../services/audit.api';
|
|
2
|
+
|
|
3
|
+
export interface AccountGroup {
|
|
4
|
+
user: AuditPrincipal['user'];
|
|
5
|
+
principals: AuditPrincipal[];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** The React key / disclosure id for one row — kinds share no id space, so both go in. */
|
|
9
|
+
export function principalKey(p: Pick<AuditPrincipal, 'kind' | 'id'>): string {
|
|
10
|
+
return `${p.kind}:${p.id}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* One list from the server, grouped per account in the order the server
|
|
15
|
+
* returned it (owner email; live before revoked, most recently used first).
|
|
16
|
+
* Revoked rows are dropped unless asked for, and an account left with no
|
|
17
|
+
* rows disappears with them.
|
|
18
|
+
*
|
|
19
|
+
* `query` narrows to accounts or rows whose name, email or label contains it
|
|
20
|
+
* (case-insensitive) — the admin's "find that agent" box. A match on the
|
|
21
|
+
* account keeps every row of it; a match on a row keeps just that row.
|
|
22
|
+
*
|
|
23
|
+
* Lives beside {@link AuditLogPage} rather than in it: a component file that
|
|
24
|
+
* also exports a plain function breaks fast refresh.
|
|
25
|
+
*/
|
|
26
|
+
export function groupByAccount(
|
|
27
|
+
principals: AuditPrincipal[],
|
|
28
|
+
opts: { includeRevoked: boolean; query?: string },
|
|
29
|
+
): AccountGroup[] {
|
|
30
|
+
const q = (opts.query ?? '').trim().toLowerCase();
|
|
31
|
+
const groups: AccountGroup[] = [];
|
|
32
|
+
const byUser = new Map<string, AccountGroup>();
|
|
33
|
+
for (const p of principals) {
|
|
34
|
+
if (!opts.includeRevoked && p.revokedAt !== null) continue;
|
|
35
|
+
if (q) {
|
|
36
|
+
const accountHit = p.user.name.toLowerCase().includes(q) || p.user.email.toLowerCase().includes(q);
|
|
37
|
+
const rowHit = p.label.toLowerCase().includes(q);
|
|
38
|
+
if (!accountHit && !rowHit) continue;
|
|
39
|
+
}
|
|
40
|
+
let group = byUser.get(p.user.id);
|
|
41
|
+
if (!group) {
|
|
42
|
+
group = { user: p.user, principals: [] };
|
|
43
|
+
byUser.set(p.user.id, group);
|
|
44
|
+
groups.push(group);
|
|
45
|
+
}
|
|
46
|
+
group.principals.push(p);
|
|
47
|
+
}
|
|
48
|
+
return groups;
|
|
49
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { authFetch } from '../../../lib/api';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The Audit log's client: the agents and keys a person (or, for admins, the
|
|
5
|
+
* deployment) has connected, what each of them called, and the revoke of
|
|
6
|
+
* either. Types mirror the backend's `audit.contract.ts`; re-declared here
|
|
7
|
+
* rather than shared, like the admin inbox's, because the feature is small.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export type AuditPrincipalKind = 'key' | 'agent';
|
|
11
|
+
|
|
12
|
+
/** One row of the Audit log: an agent connection or a connection key, with its owner. */
|
|
13
|
+
export interface AuditPrincipal {
|
|
14
|
+
kind: AuditPrincipalKind;
|
|
15
|
+
id: string;
|
|
16
|
+
/** The key's label, or the agent's registered name ("Claude"). */
|
|
17
|
+
label: string;
|
|
18
|
+
/** For a key: `key` by hand, `github-link` for a Claude link. Absent for an agent. */
|
|
19
|
+
keyKind?: string;
|
|
20
|
+
/** When the key was created / the agent connected. Epoch ms. */
|
|
21
|
+
createdAt: number;
|
|
22
|
+
lastUsedAt: number | null;
|
|
23
|
+
revokedAt: number | null;
|
|
24
|
+
/** Who ended it once revoked: the owner, or an admin. Null while live. */
|
|
25
|
+
revokedBy: 'owner' | 'admin' | null;
|
|
26
|
+
/** For a key: when its owner deleted it for good. Only admins see such rows. */
|
|
27
|
+
deletedAt?: number | null;
|
|
28
|
+
/** Events on record for it. */
|
|
29
|
+
eventCount: number;
|
|
30
|
+
user: { id: string; email: string; name: string };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type AgentEventKind = 'capability' | 'tool' | 'skill';
|
|
34
|
+
export type AgentEventOutcome = 'ok' | 'error' | 'denied';
|
|
35
|
+
|
|
36
|
+
export interface AgentEvent {
|
|
37
|
+
id: string;
|
|
38
|
+
kind: AgentEventKind;
|
|
39
|
+
/** The MCP server / `.tool` manual for a tool, the skill folder for a skill; null for a capability. */
|
|
40
|
+
manual: string | null;
|
|
41
|
+
name: string;
|
|
42
|
+
outcome: AgentEventOutcome;
|
|
43
|
+
/** Epoch ms. */
|
|
44
|
+
at: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface AgentEventPage {
|
|
48
|
+
events: AgentEvent[];
|
|
49
|
+
/** Every event on record for the principal — on the first page only; null on a page reached through a cursor. */
|
|
50
|
+
total: number | null;
|
|
51
|
+
/** Pass back as `before` for the next (older) page; null on the last. */
|
|
52
|
+
nextCursor: string | null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function readError(res: Response, fallback: string): Promise<string> {
|
|
56
|
+
const body = await res.json().catch(() => ({}));
|
|
57
|
+
return (body as { error?: string }).error || fallback;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Your own agents and keys, or (admins) everyone's. Ordered by owner, live first, most recently used first. */
|
|
61
|
+
export async function listPrincipals(scope: 'me' | 'all'): Promise<AuditPrincipal[]> {
|
|
62
|
+
const res = await authFetch(`/api/audit/principals?scope=${scope}`);
|
|
63
|
+
if (!res.ok) throw new Error(await readError(res, 'Could not load the audit log'));
|
|
64
|
+
const body = (await res.json()) as { principals: AuditPrincipal[] };
|
|
65
|
+
return body.principals;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** One principal's events, newest first; `before` continues from a previous page's cursor. */
|
|
69
|
+
export async function listEvents(
|
|
70
|
+
kind: AuditPrincipalKind,
|
|
71
|
+
id: string,
|
|
72
|
+
opts: { before?: string; limit?: number } = {},
|
|
73
|
+
): Promise<AgentEventPage> {
|
|
74
|
+
const qs = new URLSearchParams();
|
|
75
|
+
if (opts.before) qs.set('before', opts.before);
|
|
76
|
+
if (opts.limit) qs.set('limit', String(opts.limit));
|
|
77
|
+
const query = qs.toString();
|
|
78
|
+
const res = await authFetch(
|
|
79
|
+
`/api/audit/principals/${kind}/${encodeURIComponent(id)}/events${query ? `?${query}` : ''}`,
|
|
80
|
+
);
|
|
81
|
+
if (!res.ok) throw new Error(await readError(res, 'Could not load events'));
|
|
82
|
+
return (await res.json()) as AgentEventPage;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Cut an agent off: every token it holds stops working, and it can only
|
|
87
|
+
* come back by signing in again through the browser. Its row and events stay.
|
|
88
|
+
*/
|
|
89
|
+
export async function revokeAgent(id: string): Promise<void> {
|
|
90
|
+
const res = await authFetch(`/api/audit/agents/${encodeURIComponent(id)}`, { method: 'DELETE' });
|
|
91
|
+
if (!res.ok) throw new Error(await readError(res, 'Could not revoke this agent'));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Revoke a connection key (own, or as an admin anyone's). The row stays so its history remains visible. */
|
|
95
|
+
export async function revokeKey(id: string): Promise<void> {
|
|
96
|
+
const res = await authFetch(`/api/audit/keys/${encodeURIComponent(id)}`, { method: 'DELETE' });
|
|
97
|
+
if (!res.ok) throw new Error(await readError(res, 'Could not revoke this key'));
|
|
98
|
+
}
|
|
@@ -111,6 +111,7 @@ describe('SettingsLayout', () => {
|
|
|
111
111
|
const links = within(nav()).getAllByRole('link');
|
|
112
112
|
expect(links.map((l) => l.textContent)).toEqual([
|
|
113
113
|
'External agent access',
|
|
114
|
+
'Audit log',
|
|
114
115
|
'Secrets',
|
|
115
116
|
'Browse available tools',
|
|
116
117
|
'Account',
|
|
@@ -242,7 +243,7 @@ describe('SettingsLayout', () => {
|
|
|
242
243
|
renderAt('/secrets');
|
|
243
244
|
expect(document.getElementById(SIDEBAR_DOM_ID)).toBeNull();
|
|
244
245
|
expect(noNav()).not.toBeNull();
|
|
245
|
-
expect(within(nav()).getAllByRole('link')).toHaveLength(
|
|
246
|
+
expect(within(nav()).getAllByRole('link')).toHaveLength(5);
|
|
246
247
|
vi.unstubAllGlobals();
|
|
247
248
|
});
|
|
248
249
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useMemo } from 'react';
|
|
2
|
-
import { Boxes, CircleUserRound, KeyRound, Lock, SlidersHorizontal, Users, UsersRound } from 'lucide-react';
|
|
2
|
+
import { Boxes, CircleUserRound, KeyRound, Lock, ScrollText, SlidersHorizontal, Users, UsersRound } from 'lucide-react';
|
|
3
3
|
import { useAppRegistry, type AdminMenuItem } from '../../core/registry';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -42,6 +42,16 @@ export const CORE_MENU_ITEMS: AdminMenuItem[] = [
|
|
|
42
42
|
label: 'External agent access',
|
|
43
43
|
path: '/external-agent-access',
|
|
44
44
|
},
|
|
45
|
+
{
|
|
46
|
+
// Everyone's row, not an admin's: each person sees their own agents and
|
|
47
|
+
// keys and what those called; admins see every account on the same page.
|
|
48
|
+
// It replaced the admin-only Connection keys row.
|
|
49
|
+
id: 'audit-log',
|
|
50
|
+
order: 45,
|
|
51
|
+
icon: <ScrollText size={15} />,
|
|
52
|
+
label: 'Audit log',
|
|
53
|
+
path: '/audit-log',
|
|
54
|
+
},
|
|
45
55
|
{
|
|
46
56
|
id: 'secrets',
|
|
47
57
|
order: 50,
|
|
@@ -95,14 +105,6 @@ export const CORE_MENU_ITEMS: AdminMenuItem[] = [
|
|
|
95
105
|
label: 'User accounts',
|
|
96
106
|
path: '/user-accounts',
|
|
97
107
|
},
|
|
98
|
-
{
|
|
99
|
-
id: 'connection-keys',
|
|
100
|
-
section: 'admin',
|
|
101
|
-
order: 25,
|
|
102
|
-
icon: <KeyRound size={15} />,
|
|
103
|
-
label: 'Connection keys',
|
|
104
|
-
path: '/connection-keys',
|
|
105
|
-
},
|
|
106
108
|
];
|
|
107
109
|
|
|
108
110
|
/**
|
|
@@ -178,6 +178,11 @@ const FIELDS: Record<
|
|
|
178
178
|
help: 'Only people with an address at these domains can sign in this way. Separate several with commas. Leave blank to allow any address: safe with a provider that only serves your organisation, risky with one that does not.',
|
|
179
179
|
placeholder: 'example.com',
|
|
180
180
|
},
|
|
181
|
+
auditRetentionDays: {
|
|
182
|
+
label: 'Keep events for',
|
|
183
|
+
help: 'How many days an agent’s recorded calls stay in the Audit log before they are removed. Leave it blank, or enter 0, to keep them forever. Applies without a restart.',
|
|
184
|
+
placeholder: 'forever',
|
|
185
|
+
},
|
|
181
186
|
};
|
|
182
187
|
|
|
183
188
|
/**
|
|
@@ -257,6 +262,12 @@ const SECTIONS: { id: SettingStatus['section']; title: string; blurb: string }[]
|
|
|
257
262
|
blurb:
|
|
258
263
|
'Optional, and you can add it later. Lets people sign in with the account they already have instead of a password.',
|
|
259
264
|
},
|
|
265
|
+
{
|
|
266
|
+
id: 'audit',
|
|
267
|
+
title: 'Audit log',
|
|
268
|
+
blurb:
|
|
269
|
+
'The Audit log records which tools, skills and capabilities each connected agent uses. Choose how long those records are kept; unset, they are kept forever.',
|
|
270
|
+
},
|
|
260
271
|
];
|
|
261
272
|
|
|
262
273
|
interface Props {
|
|
@@ -10,7 +10,7 @@ export interface SettingStatus {
|
|
|
10
10
|
* entered here and nowhere else.
|
|
11
11
|
*/
|
|
12
12
|
envVar?: string;
|
|
13
|
-
section: 'knowledge-base' | 'sign-in';
|
|
13
|
+
section: 'knowledge-base' | 'sign-in' | 'audit';
|
|
14
14
|
source: 'env' | 'stored' | 'unset';
|
|
15
15
|
/** Absent for secrets — the server never sends a stored secret back. */
|
|
16
16
|
value?: string;
|
|
@@ -14,6 +14,8 @@ export interface ExternalApiKeySummary {
|
|
|
14
14
|
* live, and on keys revoked before this was recorded.
|
|
15
15
|
*/
|
|
16
16
|
revokedBy: 'owner' | 'admin' | null;
|
|
17
|
+
/** Never set on a key the owner can see: a deleted key leaves their listings. */
|
|
18
|
+
deletedAt?: number | null;
|
|
17
19
|
/** Model-proxy usage for this key today + the daily cap (in tokens). */
|
|
18
20
|
llmUsage?: { usedTodayTokens: number; dailyTokenCap: number };
|
|
19
21
|
}
|
|
@@ -985,8 +985,13 @@ export function FileViewer() {
|
|
|
985
985
|
kbDirName && openFilePath.startsWith(`${kbDirName}/`)
|
|
986
986
|
? openFilePath.slice(kbDirName.length + 1)
|
|
987
987
|
: null;
|
|
988
|
+
// Named owners only once THIS file's lookup has answered. The hook keeps the
|
|
989
|
+
// previous file's grants while the next request is in flight, and "Waiting on
|
|
990
|
+
// Docs" over the incoming file's box is a false statement about who decides
|
|
991
|
+
// it — the same staleness `othersPending` guards against below. The anonymous
|
|
992
|
+
// wording is always true, so it is what the window gets.
|
|
988
993
|
const ownersLabel =
|
|
989
|
-
access.owners.roles.length > 0 || access.owners.users.length > 0
|
|
994
|
+
!access.loading && (access.owners.roles.length > 0 || access.owners.users.length > 0)
|
|
990
995
|
? formatEligible(access.owners)
|
|
991
996
|
: 'the owners';
|
|
992
997
|
|