@koinzhang/paseo-plugin-activity 0.1.0 → 0.2.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.
Files changed (42) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/README.md +71 -0
  3. package/client/activity-heatmap.tsx +72 -14
  4. package/client/global-surface.tsx +24 -25
  5. package/client/panel.tsx +1 -0
  6. package/client/range.ts +20 -0
  7. package/client/usage-stats.tsx +7 -5
  8. package/client/workspace/agent-row.tsx +117 -0
  9. package/client/workspace/agents-section.tsx +323 -0
  10. package/client/workspace/constants.ts +121 -0
  11. package/client/workspace/display-menu.tsx +116 -0
  12. package/client/workspace/filters.ts +30 -0
  13. package/client/workspace/list-host-agents.ts +57 -0
  14. package/client/workspace/panel.tsx +800 -0
  15. package/client/workspace/rank-section.tsx +121 -0
  16. package/client/workspace-panel.tsx +2 -0
  17. package/index.client.tsx +20 -13
  18. package/index.server.ts +11 -0
  19. package/package.json +8 -2
  20. package/server/agents.test.ts +20 -1
  21. package/server/agents.ts +2 -1
  22. package/server/background-sync.test.ts +14 -3
  23. package/server/background-sync.ts +16 -5
  24. package/server/handlers-query.test.ts +58 -0
  25. package/server/handlers.ts +73 -2
  26. package/server/messages.test.ts +8 -2
  27. package/server/store.test.ts +13 -0
  28. package/server/store.ts +2 -2
  29. package/shared/activity.test.ts +22 -12
  30. package/shared/activity.ts +8 -13
  31. package/shared/agent-page-size.test.ts +11 -0
  32. package/shared/agent-title-search.test.ts +28 -0
  33. package/shared/agent-title-search.ts +12 -0
  34. package/shared/explorer-agent-display.ts +30 -0
  35. package/shared/format.test.ts +22 -1
  36. package/shared/format.ts +24 -0
  37. package/shared/insights.test.ts +38 -18
  38. package/shared/insights.ts +38 -19
  39. package/shared/list-agent-pages.test.ts +51 -0
  40. package/shared/list-agent-pages.ts +31 -0
  41. package/shared/usage.test.ts +248 -0
  42. package/shared/usage.ts +327 -11
@@ -0,0 +1,800 @@
1
+ import {
2
+ type PluginWorkspacePanelProps,
3
+ usePaseo,
4
+ useRpc,
5
+ useSettings,
6
+ } from "@getpaseo/plugin/client";
7
+ import { useToast } from "@getpaseo/plugin/client/react-native";
8
+ import { useQuery, useQueryClient } from "@tanstack/react-query";
9
+ import { useMemo, useRef, useState } from "react";
10
+ import {
11
+ ActivityIndicator,
12
+ Dimensions,
13
+ Platform,
14
+ Pressable,
15
+ ScrollView,
16
+ Text,
17
+ View,
18
+ } from "react-native";
19
+ import { ACTIVITY_LIST_LIMIT } from "../../shared/insights.ts";
20
+ import {
21
+ EXPLORER_AGENT_DISPLAY_DEFAULTS,
22
+ explorerAgentDisplaySettings,
23
+ type ExplorerAgentDisplayValues,
24
+ } from "../../shared/explorer-agent-display.ts";
25
+ import {
26
+ type AgentUsageItem,
27
+ usageAgentUnarchiveRpc,
28
+ usageAgentsRpc,
29
+ usageMcpByToolRpc,
30
+ usageSkillsByNameRpc,
31
+ usageSummaryRpc,
32
+ } from "../../shared/usage.ts";
33
+ import { useAppLanguage } from "../use-app-language.ts";
34
+ import { UsageStats } from "../usage-stats.tsx";
35
+ import { AgentsSection } from "./agents-section.tsx";
36
+ import {
37
+ AGENT_HEADER_HEIGHT,
38
+ GROUP_OPTIONS,
39
+ LIFECYCLE_FILTER_OPTIONS,
40
+ MENU_SUBMENU_OVERLAP,
41
+ MENU_WIDTH,
42
+ SHOW_FIELD_OPTIONS,
43
+ SORT_OPTIONS,
44
+ STATUS_FILTER_OPTIONS,
45
+ agentUpdatedAt,
46
+ matchesAgentFilters,
47
+ optionLabel,
48
+ type AgentGroup,
49
+ type AgentLifecycleFilter,
50
+ type AgentShowField,
51
+ type AgentSort,
52
+ type AgentStatusFilter,
53
+ type MenuFlyout,
54
+ type MenuOption,
55
+ } from "./constants.ts";
56
+ import { MenuOptionList, MenuSubTrigger } from "./display-menu.tsx";
57
+ import { matchesAgentTitle } from "./filters.ts";
58
+ import { loadWorkspaceAgentStatuses } from "./list-host-agents.ts";
59
+ import { RankSection } from "./rank-section.tsx";
60
+
61
+ type RankKind = "skills" | "mcp";
62
+
63
+ /** Workspace-scoped Activity for the Explorer (024 / 031). */
64
+ export function WorkspaceActivityPanel({
65
+ theme,
66
+ layout,
67
+ workspaceId,
68
+ navigation,
69
+ }: PluginWorkspacePanelProps) {
70
+ const displaySettings = useSettings(explorerAgentDisplaySettings);
71
+ const displayValues: ExplorerAgentDisplayValues =
72
+ displaySettings.status === "ready"
73
+ ? displaySettings.values
74
+ : EXPLORER_AGENT_DISPLAY_DEFAULTS;
75
+ const agentSort = displayValues.sort;
76
+ const agentGroup = displayValues.group;
77
+ const agentShowFields = useMemo(
78
+ () => new Set(displayValues.show),
79
+ [displayValues.show],
80
+ );
81
+ const agentStatusFilters = useMemo(
82
+ () => new Set(displayValues.status),
83
+ [displayValues.status],
84
+ );
85
+ const agentLifecycleFilters = useMemo(
86
+ () => new Set(displayValues.lifecycle),
87
+ [displayValues.lifecycle],
88
+ );
89
+ const [menuOpen, setMenuOpen] = useState(false);
90
+ const [menuFlyout, setMenuFlyout] = useState<MenuFlyout | null>(null);
91
+ const [menuAnchor, setMenuAnchor] = useState<{ top: number; right: number } | null>(null);
92
+ const [agentSearchQuery, setAgentSearchQuery] = useState("");
93
+ const [rankKind, setRankKind] = useState<RankKind>("skills");
94
+ const [busyAgentId, setBusyAgentId] = useState<string | null>(null);
95
+ const padding = layout.compact ? 16 : 24;
96
+ const locale = useAppLanguage();
97
+ const toast = useToast();
98
+ const queryClient = useQueryClient();
99
+ const openAgent = navigation?.openAgent;
100
+ const rootRef = useRef<View>(null);
101
+ const triggerRef = useRef<View>(null);
102
+
103
+ function persistDisplay(next: ExplorerAgentDisplayValues) {
104
+ if (displaySettings.status !== "ready") return;
105
+ void displaySettings.save(next, displaySettings.revision);
106
+ }
107
+
108
+ const summaryRpc = useRpc(usageSummaryRpc);
109
+ const agentsRpc = useRpc(usageAgentsRpc);
110
+ const unarchiveAgentRpc = useRpc(usageAgentUnarchiveRpc);
111
+ const skillsRpc = useRpc(usageSkillsByNameRpc);
112
+ const mcpRpc = useRpc(usageMcpByToolRpc);
113
+
114
+ const agentsQueryKey = ["activity", "workspace-agents", workspaceId] as const;
115
+
116
+ const summary = useQuery({
117
+ refetchInterval: 15_000,
118
+ retry: false,
119
+ queryKey: ["activity", "workspace-summary", workspaceId],
120
+ queryFn: () => summaryRpc({ workspaceId }),
121
+ });
122
+
123
+ const agents = useQuery({
124
+ refetchInterval: 15_000,
125
+ retry: false,
126
+ queryKey: agentsQueryKey,
127
+ queryFn: () => agentsRpc({ workspaceId }),
128
+ });
129
+
130
+ const skills = useQuery({
131
+ refetchInterval: 15_000,
132
+ retry: false,
133
+ queryKey: ["activity", "workspace-skills", workspaceId],
134
+ queryFn: () => skillsRpc({ workspaceId }),
135
+ });
136
+
137
+ const mcp = useQuery({
138
+ refetchInterval: 15_000,
139
+ retry: false,
140
+ queryKey: ["activity", "workspace-mcp", workspaceId],
141
+ queryFn: () => mcpRpc({ workspaceId }),
142
+ });
143
+
144
+ const paseo = usePaseo();
145
+ const statuses = useQuery({
146
+ refetchInterval: 15_000,
147
+ retry: false,
148
+ queryKey: ["activity", "workspace-agent-status", workspaceId],
149
+ queryFn: () => loadWorkspaceAgentStatuses(paseo, workspaceId),
150
+ });
151
+
152
+ const agentItems = agents.data?.items ?? [];
153
+ const visibleAgentItems = useMemo(() => {
154
+ const byId = statuses.data;
155
+ const filtered = agentItems
156
+ .filter((item) =>
157
+ matchesAgentFilters(item, agentStatusFilters, agentLifecycleFilters, byId),
158
+ )
159
+ .filter((item) => matchesAgentTitle(item, agentSearchQuery));
160
+ const byName = (a: AgentUsageItem, b: AgentUsageItem) =>
161
+ (a.title ?? a.agentId).localeCompare(b.title ?? b.agentId);
162
+ return filtered.slice().sort((a, b) => {
163
+ if (agentSort === "name") return byName(a, b);
164
+ if (agentSort === "messages") {
165
+ return b.messageCount - a.messageCount || byName(a, b);
166
+ }
167
+ if (agentSort === "created") {
168
+ const ca = a.createdAt ?? "";
169
+ const cb = b.createdAt ?? "";
170
+ return cb.localeCompare(ca) || byName(a, b);
171
+ }
172
+ if (agentSort === "status") {
173
+ const rankA = byId?.[a.agentId]?.rank ?? 4;
174
+ const rankB = byId?.[b.agentId]?.rank ?? 4;
175
+ if (rankA !== rankB) return rankA - rankB;
176
+ const ua = agentUpdatedAt(a, byId) ?? "";
177
+ const ub = agentUpdatedAt(b, byId) ?? "";
178
+ return ub.localeCompare(ua) || byName(a, b);
179
+ }
180
+ // updated (default)
181
+ const ua = agentUpdatedAt(a, byId) ?? "";
182
+ const ub = agentUpdatedAt(b, byId) ?? "";
183
+ return ub.localeCompare(ua) || byName(a, b);
184
+ });
185
+ }, [
186
+ agentItems,
187
+ agentLifecycleFilters,
188
+ agentSearchQuery,
189
+ agentSort,
190
+ agentStatusFilters,
191
+ statuses.data,
192
+ ]);
193
+
194
+ const allSkills = useMemo(
195
+ () => (skills.data?.items ?? []).filter((item) => item.total > 0),
196
+ [skills.data],
197
+ );
198
+ const allMcp = mcp.data?.items ?? [];
199
+ const skillItems = useMemo(() => allSkills.slice(0, ACTIVITY_LIST_LIMIT), [allSkills]);
200
+ const mcpItems = useMemo(() => allMcp.slice(0, ACTIVITY_LIST_LIMIT), [allMcp]);
201
+
202
+ const loading =
203
+ summary.isLoading || agents.isLoading || skills.isLoading || mcp.isLoading;
204
+ const error = summary.error ?? agents.error ?? skills.error ?? mcp.error;
205
+
206
+ const kpi = [
207
+ { label: "Shell calls", value: (summary.data?.shellCalls ?? 0).toLocaleString() },
208
+ { label: "File reads", value: (summary.data?.fileReads ?? 0).toLocaleString() },
209
+ { label: "File writes", value: (summary.data?.fileWrites ?? 0).toLocaleString() },
210
+ {
211
+ label: "Messages",
212
+ value: (summary.data?.messageCount ?? 0).toLocaleString(),
213
+ },
214
+ ];
215
+
216
+ const showAgents = agentItems.length > 0;
217
+ const showRank = skillItems.length > 0 || mcpItems.length > 0;
218
+ const showContent = showAgents || showRank;
219
+
220
+ const styles = useMemo(
221
+ () => ({
222
+ shell: {
223
+ flex: 1,
224
+ minHeight: 0,
225
+ minWidth: 0,
226
+ },
227
+ screen: {
228
+ flex: 1,
229
+ minHeight: 0,
230
+ minWidth: 0,
231
+ overflow: "hidden" as const,
232
+ backgroundColor: theme.colors.surface0,
233
+ },
234
+ scroll: {
235
+ flex: 1,
236
+ minHeight: 0,
237
+ minWidth: 0,
238
+ ...(Platform.OS === "web"
239
+ ? ({ scrollbarWidth: "none", msOverflowStyle: "none" } as object)
240
+ : null),
241
+ },
242
+ content: {
243
+ padding,
244
+ paddingBottom: padding + 24,
245
+ gap: layout.compact ? 20 : 26,
246
+ maxWidth: 1000,
247
+ width: "100%" as const,
248
+ alignSelf: "center" as const,
249
+ },
250
+ sectionHeaderRow: {
251
+ flexDirection: "row" as const,
252
+ alignItems: "center" as const,
253
+ justifyContent: "space-between" as const,
254
+ gap: 8,
255
+ },
256
+ agentsHeaderRow: {
257
+ flexDirection: "row" as const,
258
+ alignItems: "center" as const,
259
+ height: AGENT_HEADER_HEIGHT,
260
+ },
261
+ agentsHeaderSearchSlot: {
262
+ flex: 1,
263
+ minWidth: 0,
264
+ flexDirection: "row" as const,
265
+ justifyContent: "flex-end" as const,
266
+ overflow: "hidden" as const,
267
+ },
268
+ titleAction: {
269
+ width: 24,
270
+ height: 24,
271
+ alignItems: "center" as const,
272
+ justifyContent: "center" as const,
273
+ borderRadius: 6,
274
+ flexShrink: 0,
275
+ },
276
+ headerActions: {
277
+ flexDirection: "row" as const,
278
+ alignItems: "center" as const,
279
+ gap: 2,
280
+ flexShrink: 0,
281
+ marginLeft: 2,
282
+ },
283
+ searchField: {
284
+ flexDirection: "row" as const,
285
+ alignItems: "center" as const,
286
+ gap: 6,
287
+ height: AGENT_HEADER_HEIGHT,
288
+ paddingHorizontal: 10,
289
+ borderRadius: AGENT_HEADER_HEIGHT / 2,
290
+ borderWidth: 1,
291
+ borderColor: theme.colors.border,
292
+ backgroundColor: theme.colors.surface1,
293
+ overflow: "hidden" as const,
294
+ },
295
+ searchInput: {
296
+ flex: 1,
297
+ minWidth: 0,
298
+ padding: 0,
299
+ margin: 0,
300
+ color: theme.colors.foreground,
301
+ fontSize: 13,
302
+ lineHeight: 18,
303
+ ...(Platform.OS === "web" ? ({ outlineStyle: "none" } as object) : null),
304
+ },
305
+ sectionTitle: {
306
+ color: theme.colors.foreground,
307
+ fontSize: layout.compact ? 18 : 20,
308
+ fontWeight: "600" as const,
309
+ flexShrink: 0,
310
+ letterSpacing: -0.3,
311
+ lineHeight: AGENT_HEADER_HEIGHT,
312
+ },
313
+ panel: {
314
+ gap: 4,
315
+ overflow: "hidden" as const,
316
+ },
317
+ listRow: {
318
+ flexDirection: "row" as const,
319
+ alignItems: "center" as const,
320
+ gap: 12,
321
+ paddingVertical: 12,
322
+ },
323
+ agentListRow: {
324
+ flexDirection: "row" as const,
325
+ alignItems: "center" as const,
326
+ gap: 10,
327
+ paddingVertical: 6,
328
+ },
329
+ listMain: {
330
+ flex: 1,
331
+ minWidth: 0,
332
+ gap: 3,
333
+ },
334
+ listTitle: {
335
+ color: theme.colors.foreground,
336
+ fontSize: 14,
337
+ fontWeight: "500" as const,
338
+ },
339
+ listLink: {
340
+ color: theme.colors.foreground,
341
+ fontSize: 14,
342
+ fontWeight: "500" as const,
343
+ },
344
+ listMeta: {
345
+ color: theme.colors.foregroundMuted,
346
+ fontSize: 12,
347
+ },
348
+ countText: {
349
+ color: theme.colors.foregroundMuted,
350
+ fontSize: 13,
351
+ fontVariant: ["tabular-nums" as const],
352
+ minWidth: 24,
353
+ textAlign: "right" as const,
354
+ },
355
+ empty: {
356
+ color: theme.colors.foregroundMuted,
357
+ fontSize: 13,
358
+ },
359
+ agentGroup: {
360
+ gap: 4,
361
+ },
362
+ groupLabel: {
363
+ color: theme.colors.foregroundMuted,
364
+ fontSize: 12,
365
+ fontWeight: "600" as const,
366
+ paddingTop: 12,
367
+ paddingBottom: 2,
368
+ },
369
+ pagerRow: {
370
+ flexDirection: "row" as const,
371
+ alignItems: "center" as const,
372
+ justifyContent: "center" as const,
373
+ gap: 12,
374
+ paddingTop: 8,
375
+ paddingBottom: 4,
376
+ },
377
+ pagerButton: {
378
+ width: 28,
379
+ height: 28,
380
+ alignItems: "center" as const,
381
+ justifyContent: "center" as const,
382
+ borderRadius: 6,
383
+ },
384
+ pagerButtonDisabled: {
385
+ opacity: 0.35,
386
+ },
387
+ pagerLabel: {
388
+ color: theme.colors.foregroundMuted,
389
+ fontSize: 12,
390
+ fontVariant: ["tabular-nums" as const],
391
+ },
392
+ menuPage: {
393
+ paddingVertical: 4,
394
+ },
395
+ menuRow: {
396
+ flexDirection: "row" as const,
397
+ alignItems: "center" as const,
398
+ minHeight: layout.compact ? 40 : 28,
399
+ gap: 8,
400
+ marginHorizontal: 4,
401
+ paddingHorizontal: 8,
402
+ paddingVertical: 4,
403
+ borderWidth: 1,
404
+ borderColor: "transparent",
405
+ borderRadius: 6,
406
+ },
407
+ menuRowHighlighted: {
408
+ backgroundColor: theme.colors.surface2,
409
+ },
410
+ menuLabel: {
411
+ flexShrink: 1,
412
+ minWidth: 0,
413
+ color: theme.colors.foreground,
414
+ fontSize: 14,
415
+ lineHeight: 18,
416
+ fontWeight: "normal" as const,
417
+ },
418
+ menuValue: {
419
+ color: theme.colors.foregroundMuted,
420
+ fontSize: 14,
421
+ lineHeight: 18,
422
+ flexShrink: 1,
423
+ },
424
+ menuTrailing: {
425
+ flexDirection: "row" as const,
426
+ alignItems: "center" as const,
427
+ gap: 6,
428
+ marginLeft: "auto" as const,
429
+ flexShrink: 0,
430
+ },
431
+ menuOption: {
432
+ flexDirection: "row" as const,
433
+ alignItems: "center" as const,
434
+ minHeight: layout.compact ? 40 : 28,
435
+ gap: 8,
436
+ marginHorizontal: 4,
437
+ paddingHorizontal: 8,
438
+ paddingVertical: 4,
439
+ borderWidth: 1,
440
+ borderColor: "transparent",
441
+ borderRadius: 6,
442
+ },
443
+ menuOptionLabel: {
444
+ flexShrink: 1,
445
+ minWidth: 0,
446
+ color: theme.colors.foreground,
447
+ fontSize: 14,
448
+ lineHeight: 18,
449
+ fontWeight: "normal" as const,
450
+ },
451
+ menuLeadingSlot: {
452
+ width: 16,
453
+ alignItems: "center" as const,
454
+ justifyContent: "center" as const,
455
+ },
456
+ agentsSection: {
457
+ gap: 12,
458
+ position: "relative" as const,
459
+ },
460
+ agentsHeaderWrap: {
461
+ position: "relative" as const,
462
+ },
463
+ menuBackdrop: {
464
+ position: (Platform.OS === "web" ? "fixed" : "absolute") as "absolute",
465
+ top: 0,
466
+ left: 0,
467
+ right: 0,
468
+ bottom: 0,
469
+ zIndex: 30,
470
+ },
471
+ menuSurface: {
472
+ width: MENU_WIDTH,
473
+ borderRadius: 8,
474
+ borderWidth: 1,
475
+ borderColor: theme.colors.border,
476
+ backgroundColor: theme.colors.surface1,
477
+ shadowColor: "rgba(0, 0, 0, 0.04)",
478
+ shadowOffset: { width: 0, height: 4 },
479
+ shadowRadius: 16,
480
+ elevation: 4,
481
+ },
482
+ menuRootWrap: {
483
+ position: (Platform.OS === "web" ? "fixed" : "absolute") as "absolute",
484
+ zIndex: 31,
485
+ overflow: "visible" as const,
486
+ },
487
+ menuFlyout: {
488
+ position: "absolute" as const,
489
+ right: MENU_WIDTH - MENU_SUBMENU_OVERLAP,
490
+ width: MENU_WIDTH,
491
+ zIndex: 32,
492
+ },
493
+ menuSeparator: {
494
+ height: 1,
495
+ marginVertical: 4,
496
+ backgroundColor: theme.colors.border,
497
+ },
498
+ }),
499
+ [theme, layout.compact, padding],
500
+ );
501
+
502
+ const rowHeight = layout.compact ? 40 : 28;
503
+ const flyoutTop = menuFlyout
504
+ ? 4 +
505
+ ({ sort: 0, group: 1, show: 2, status: 3, lifecycle: 4 }[menuFlyout] * rowHeight) +
506
+ (menuFlyout === "status" || menuFlyout === "lifecycle" ? 9 : 0)
507
+ : 0;
508
+ const flyoutOptions: ReadonlyArray<MenuOption> =
509
+ menuFlyout === "sort"
510
+ ? SORT_OPTIONS
511
+ : menuFlyout === "group"
512
+ ? GROUP_OPTIONS
513
+ : menuFlyout === "show"
514
+ ? SHOW_FIELD_OPTIONS
515
+ : menuFlyout === "status"
516
+ ? STATUS_FILTER_OPTIONS
517
+ : menuFlyout === "lifecycle"
518
+ ? LIFECYCLE_FILTER_OPTIONS
519
+ : [];
520
+ const flyoutSelectedId: string | undefined =
521
+ menuFlyout === "sort"
522
+ ? agentSort
523
+ : menuFlyout === "group"
524
+ ? agentGroup
525
+ : undefined;
526
+ const flyoutSelectedIds: ReadonlySet<string> | undefined =
527
+ menuFlyout === "show"
528
+ ? agentShowFields
529
+ : menuFlyout === "status"
530
+ ? agentStatusFilters
531
+ : menuFlyout === "lifecycle"
532
+ ? agentLifecycleFilters
533
+ : undefined;
534
+
535
+ function closeMenu() {
536
+ setMenuOpen(false);
537
+ setMenuFlyout(null);
538
+ setMenuAnchor(null);
539
+ }
540
+
541
+ function openMenu() {
542
+ const trigger = triggerRef.current;
543
+ const root = rootRef.current;
544
+ if (!trigger || !root) {
545
+ setMenuAnchor({ top: 48, right: 16 });
546
+ setMenuFlyout(null);
547
+ setMenuOpen(true);
548
+ return;
549
+ }
550
+ trigger.measureInWindow((tx, ty, tw, th) => {
551
+ if (Platform.OS === "web") {
552
+ setMenuAnchor({
553
+ top: ty + th + 4,
554
+ right: Dimensions.get("window").width - (tx + tw),
555
+ });
556
+ setMenuFlyout(null);
557
+ setMenuOpen(true);
558
+ return;
559
+ }
560
+ root.measureInWindow((rx, ry, rw) => {
561
+ setMenuAnchor({
562
+ top: ty + th - ry + 4,
563
+ right: rx + rw - (tx + tw),
564
+ });
565
+ setMenuFlyout(null);
566
+ setMenuOpen(true);
567
+ });
568
+ });
569
+ }
570
+
571
+ function selectMenuOption(id: string) {
572
+ if (menuFlyout === "sort") {
573
+ persistDisplay({ ...displayValues, sort: id as AgentSort });
574
+ closeMenu();
575
+ return;
576
+ }
577
+ if (menuFlyout === "group") {
578
+ persistDisplay({ ...displayValues, group: id as AgentGroup });
579
+ closeMenu();
580
+ return;
581
+ }
582
+ if (menuFlyout === "status") {
583
+ const status = id as AgentStatusFilter;
584
+ const next = new Set(displayValues.status);
585
+ if (next.has(status)) next.delete(status);
586
+ else next.add(status);
587
+ persistDisplay({ ...displayValues, status: [...next] });
588
+ return;
589
+ }
590
+ if (menuFlyout === "lifecycle") {
591
+ const lifecycle = id as AgentLifecycleFilter;
592
+ const next = new Set(displayValues.lifecycle);
593
+ if (next.has(lifecycle)) next.delete(lifecycle);
594
+ else next.add(lifecycle);
595
+ persistDisplay({ ...displayValues, lifecycle: [...next] });
596
+ return;
597
+ }
598
+ if (menuFlyout === "show") {
599
+ const field = id as AgentShowField;
600
+ const next = new Set(displayValues.show);
601
+ if (next.has(field)) next.delete(field);
602
+ else next.add(field);
603
+ persistDisplay({ ...displayValues, show: [...next] });
604
+ }
605
+ }
606
+
607
+ function patchAgentArchivedAt(agentId: string, archivedAt: string | null) {
608
+ queryClient.setQueryData<{ items: AgentUsageItem[] }>(agentsQueryKey, (prev) => {
609
+ if (!prev) return prev;
610
+ return {
611
+ items: prev.items.map((row) =>
612
+ row.agentId === agentId ? { ...row, archivedAt } : row,
613
+ ),
614
+ };
615
+ });
616
+ }
617
+
618
+ async function archiveAgent(item: AgentUsageItem) {
619
+ if (busyAgentId != null) return;
620
+ setBusyAgentId(item.agentId);
621
+ const previous = item.archivedAt;
622
+ patchAgentArchivedAt(item.agentId, new Date().toISOString());
623
+ try {
624
+ await paseo.agents.ref(item.agentId).archive();
625
+ await queryClient.invalidateQueries({ queryKey: agentsQueryKey });
626
+ await queryClient.invalidateQueries({
627
+ queryKey: ["activity", "workspace-agent-status", workspaceId],
628
+ });
629
+ } catch (err) {
630
+ patchAgentArchivedAt(item.agentId, previous);
631
+ toast.error(err instanceof Error ? err.message : String(err));
632
+ } finally {
633
+ setBusyAgentId(null);
634
+ }
635
+ }
636
+
637
+ async function unarchiveAgent(item: AgentUsageItem) {
638
+ if (busyAgentId != null) return;
639
+ setBusyAgentId(item.agentId);
640
+ const previous = item.archivedAt;
641
+ patchAgentArchivedAt(item.agentId, null);
642
+ try {
643
+ await unarchiveAgentRpc({ agentId: item.agentId });
644
+ await queryClient.invalidateQueries({ queryKey: agentsQueryKey });
645
+ await queryClient.invalidateQueries({
646
+ queryKey: ["activity", "workspace-agent-status", workspaceId],
647
+ });
648
+ } catch (err) {
649
+ patchAgentArchivedAt(item.agentId, previous);
650
+ toast.error(err instanceof Error ? err.message : String(err));
651
+ } finally {
652
+ setBusyAgentId(null);
653
+ }
654
+ }
655
+
656
+ return (
657
+ <View ref={rootRef} collapsable={false} style={styles.shell}>
658
+ <View style={styles.screen}>
659
+ <ScrollView
660
+ style={styles.scroll}
661
+ contentContainerStyle={styles.content}
662
+ scrollEnabled={!menuOpen}
663
+ showsVerticalScrollIndicator={false}
664
+ showsHorizontalScrollIndicator={false}
665
+ >
666
+ {loading ? <ActivityIndicator color={theme.colors.accent} /> : null}
667
+ {error ? (
668
+ <Text style={{ color: theme.colors.statusDanger }}>
669
+ {error instanceof Error ? error.message : String(error)}
670
+ </Text>
671
+ ) : null}
672
+
673
+ {!loading && !error && !showContent ? (
674
+ <Text style={styles.empty}>No activity in this workspace yet</Text>
675
+ ) : null}
676
+
677
+ {!loading && !error && showContent ? (
678
+ <UsageStats items={kpi} colors={theme.colors} compact={layout.compact} dense />
679
+ ) : null}
680
+
681
+ {showAgents ? (
682
+ <AgentsSection
683
+ theme={theme}
684
+ styles={styles}
685
+ locale={locale}
686
+ agentGroup={agentGroup}
687
+ agentShowFields={agentShowFields}
688
+ statuses={statuses.data}
689
+ visibleAgentItems={visibleAgentItems}
690
+ statusFiltersEmpty={
691
+ agentStatusFilters.size === 0 || agentLifecycleFilters.size === 0
692
+ }
693
+ busyAgentId={busyAgentId}
694
+ openAgent={openAgent}
695
+ menuOpen={menuOpen}
696
+ onToggleMenu={() => {
697
+ if (menuOpen) closeMenu();
698
+ else openMenu();
699
+ }}
700
+ triggerRef={triggerRef}
701
+ searchQuery={agentSearchQuery}
702
+ onSearchQueryChange={setAgentSearchQuery}
703
+ onArchive={(item) => void archiveAgent(item)}
704
+ onUnarchive={(item) => void unarchiveAgent(item)}
705
+ resetKey={workspaceId}
706
+ />
707
+ ) : null}
708
+
709
+ {showRank ? (
710
+ <RankSection
711
+ rankKind={rankKind}
712
+ onToggleKind={() =>
713
+ setRankKind((prev) => (prev === "skills" ? "mcp" : "skills"))
714
+ }
715
+ skillItems={skillItems}
716
+ mcpItems={mcpItems}
717
+ mutedColor={theme.colors.foregroundMuted}
718
+ styles={styles}
719
+ />
720
+ ) : null}
721
+ </ScrollView>
722
+ </View>
723
+
724
+ {menuOpen ? (
725
+ <Pressable
726
+ accessibilityRole="button"
727
+ accessibilityLabel="Close agent display options"
728
+ onPress={closeMenu}
729
+ style={styles.menuBackdrop}
730
+ />
731
+ ) : null}
732
+
733
+ {menuOpen && menuAnchor ? (
734
+ <View
735
+ pointerEvents="box-none"
736
+ style={[styles.menuRootWrap, { top: menuAnchor.top, right: menuAnchor.right }]}
737
+ >
738
+ <View style={styles.menuSurface}>
739
+ <View style={styles.menuPage}>
740
+ <MenuSubTrigger
741
+ label="Sort"
742
+ value={optionLabel(SORT_OPTIONS, agentSort)}
743
+ active={menuFlyout === "sort"}
744
+ onOpen={() => setMenuFlyout("sort")}
745
+ styles={styles}
746
+ chevronColor={theme.colors.foregroundMuted}
747
+ />
748
+ <MenuSubTrigger
749
+ label="Group"
750
+ value={optionLabel(GROUP_OPTIONS, agentGroup)}
751
+ active={menuFlyout === "group"}
752
+ onOpen={() => setMenuFlyout("group")}
753
+ styles={styles}
754
+ chevronColor={theme.colors.foregroundMuted}
755
+ />
756
+ <MenuSubTrigger
757
+ label="Show"
758
+ active={menuFlyout === "show"}
759
+ onOpen={() => setMenuFlyout("show")}
760
+ styles={styles}
761
+ chevronColor={theme.colors.foregroundMuted}
762
+ />
763
+ <View style={styles.menuSeparator} />
764
+ <MenuSubTrigger
765
+ label="Status"
766
+ active={menuFlyout === "status"}
767
+ onOpen={() => setMenuFlyout("status")}
768
+ styles={styles}
769
+ chevronColor={theme.colors.foregroundMuted}
770
+ />
771
+ <MenuSubTrigger
772
+ label="Lifecycle"
773
+ active={menuFlyout === "lifecycle"}
774
+ onOpen={() => setMenuFlyout("lifecycle")}
775
+ styles={styles}
776
+ chevronColor={theme.colors.foregroundMuted}
777
+ />
778
+ </View>
779
+ </View>
780
+ {menuFlyout ? (
781
+ <View
782
+ style={[styles.menuSurface, styles.menuFlyout, { top: flyoutTop }]}
783
+ {...({ onPointerEnter: () => setMenuFlyout(menuFlyout) } as object)}
784
+ >
785
+ <MenuOptionList
786
+ options={flyoutOptions}
787
+ selectedId={flyoutSelectedId}
788
+ selectedIds={flyoutSelectedIds}
789
+ onSelect={selectMenuOption}
790
+ styles={styles}
791
+ checkColor={theme.colors.foregroundMuted}
792
+ iconColor={theme.colors.foregroundMuted}
793
+ />
794
+ </View>
795
+ ) : null}
796
+ </View>
797
+ ) : null}
798
+ </View>
799
+ );
800
+ }