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