@koinzhang/paseo-plugin-activity 0.1.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 (52) hide show
  1. package/client/activity-heatmap.tsx +138 -0
  2. package/client/global-surface.tsx +689 -0
  3. package/client/panel.tsx +423 -0
  4. package/client/pending-skill.ts +50 -0
  5. package/client/pill-data-cache.test.ts +68 -0
  6. package/client/pill-data-cache.ts +37 -0
  7. package/client/pill.tsx +211 -0
  8. package/client/provider-filter.test.ts +95 -0
  9. package/client/provider-filter.ts +44 -0
  10. package/client/rank-color.test.ts +35 -0
  11. package/client/rank-color.ts +69 -0
  12. package/client/usage-popover.tsx +383 -0
  13. package/client/usage-query.tsx +54 -0
  14. package/client/usage-stats.tsx +18 -0
  15. package/client/use-app-language.ts +14 -0
  16. package/client/web.ts +28 -0
  17. package/index.client.tsx +62 -0
  18. package/index.server.ts +128 -0
  19. package/package.json +31 -0
  20. package/paseo-plugin.json +6 -0
  21. package/server/agents.test.ts +67 -0
  22. package/server/agents.ts +75 -0
  23. package/server/background-sync.test.ts +143 -0
  24. package/server/background-sync.ts +105 -0
  25. package/server/handlers-query.test.ts +91 -0
  26. package/server/handlers.ts +531 -0
  27. package/server/host-types.ts +14 -0
  28. package/server/ingest.test.ts +233 -0
  29. package/server/ingest.ts +277 -0
  30. package/server/mcp-servers.test.ts +70 -0
  31. package/server/mcp-servers.ts +212 -0
  32. package/server/messages.test.ts +150 -0
  33. package/server/migrate-data.test.ts +65 -0
  34. package/server/migrate-data.ts +54 -0
  35. package/server/resolve-model.test.ts +11 -0
  36. package/server/resolve-model.ts +30 -0
  37. package/server/skill-path.test.ts +56 -0
  38. package/server/skill-path.ts +85 -0
  39. package/server/store.test.ts +221 -0
  40. package/server/store.ts +909 -0
  41. package/server/ttl-cache.ts +34 -0
  42. package/shared/activity.test.ts +86 -0
  43. package/shared/activity.ts +185 -0
  44. package/shared/classify.test.ts +306 -0
  45. package/shared/classify.ts +378 -0
  46. package/shared/format.test.ts +48 -0
  47. package/shared/format.ts +45 -0
  48. package/shared/insights.test.ts +122 -0
  49. package/shared/insights.ts +188 -0
  50. package/shared/plugin-id.ts +7 -0
  51. package/shared/usage.test.ts +668 -0
  52. package/shared/usage.ts +726 -0
@@ -0,0 +1,211 @@
1
+ import {
2
+ type PluginButtonContentProps,
3
+ type PluginButtonIconProps,
4
+ type PluginButtonRegistration,
5
+ type PluginClientContext,
6
+ } from "@getpaseo/plugin/client";
7
+ import { Icon } from "@getpaseo/plugin/client/react-native";
8
+ import { useEffect, useRef } from "react";
9
+ import { formatUsagePillLabel } from "../shared/usage.ts";
10
+ import { useUsagePillData, clearPillDataCache } from "./usage-query.tsx";
11
+ import { UsagePopover, type SkillDetail } from "./usage-popover.tsx";
12
+ import { requestOpenSkillInPanel } from "./pending-skill.ts";
13
+
14
+ /** Accessible name; also the label while counts load (skills-plugin pattern). */
15
+ const TITLE = "Activity";
16
+
17
+ /**
18
+ * Keep the icon mounted while empty so refetchInterval can catch post-turn ingest.
19
+ * First visit: longer window. After a settled empty hide: shorter (idle remount).
20
+ */
21
+ const EMPTY_GRACE_MS_INITIAL = 12_000;
22
+ const EMPTY_GRACE_MS_RECHECK = 4_000;
23
+
24
+ type PillEntry = {
25
+ registration?: PluginButtonRegistration;
26
+ /** Host unmounted the icon after a settled empty hide. */
27
+ hidden: boolean;
28
+ /** Already completed one empty grace → hide cycle for this agent. */
29
+ settledEmpty: boolean;
30
+ };
31
+
32
+ function applyPillLabel(pill: PluginButtonRegistration, label: string | null) {
33
+ if (label === null) {
34
+ // Still loading — keep a stable visible title so the icon can mount.
35
+ pill.update({ label: TITLE, visible: true });
36
+ return;
37
+ }
38
+ if (label.length === 0) {
39
+ // Caller decides when to hide after grace; keep title while polling.
40
+ pill.update({ label: TITLE, visible: true });
41
+ return;
42
+ }
43
+ pill.update({ label, visible: true });
44
+ }
45
+
46
+ function createPillIcon(agentId: string, entry: PillEntry) {
47
+ return function UsagePillIcon({ size, color }: PluginButtonIconProps) {
48
+ const query = useUsagePillData(agentId);
49
+ const emptySinceRef = useRef<number | null>(null);
50
+
51
+ // null = loading or error (keep title); "" = loaded empty; otherwise top-skill badge.
52
+ const label = query.data
53
+ ? formatUsagePillLabel(
54
+ undefined,
55
+ query.data.skills,
56
+ query.data.mcpTools,
57
+ )
58
+ : query.isError
59
+ ? TITLE
60
+ : null;
61
+
62
+ useEffect(() => {
63
+ const pill = entry.registration;
64
+ if (!pill) return;
65
+
66
+ if (query.isError) {
67
+ console.error("[activity] pill query failed", agentId, query.error);
68
+ emptySinceRef.current = null;
69
+ entry.hidden = false;
70
+ // Stay visible with title so the user can still open the popover.
71
+ pill.update({ label: TITLE, visible: true });
72
+ return;
73
+ }
74
+
75
+ if (label === null) {
76
+ emptySinceRef.current = null;
77
+ applyPillLabel(pill, null);
78
+ return;
79
+ }
80
+
81
+ if (label.length > 0) {
82
+ emptySinceRef.current = null;
83
+ entry.hidden = false;
84
+ entry.settledEmpty = false;
85
+ applyPillLabel(pill, label);
86
+ return;
87
+ }
88
+
89
+ // Loaded empty — stay mounted and poll until grace elapses.
90
+ const now = Date.now();
91
+ if (emptySinceRef.current === null) emptySinceRef.current = now;
92
+ applyPillLabel(pill, "");
93
+
94
+ const graceMs = entry.settledEmpty ? EMPTY_GRACE_MS_RECHECK : EMPTY_GRACE_MS_INITIAL;
95
+ const remaining = graceMs - (now - emptySinceRef.current);
96
+ if (remaining <= 0) {
97
+ entry.hidden = true;
98
+ entry.settledEmpty = true;
99
+ // Hide without clearing title (avoids host error chrome).
100
+ pill.update({ visible: false });
101
+ return;
102
+ }
103
+
104
+ const timer = setTimeout(() => {
105
+ if (emptySinceRef.current === null) return;
106
+ if (Date.now() - emptySinceRef.current < graceMs) return;
107
+ entry.hidden = true;
108
+ entry.settledEmpty = true;
109
+ pill.update({ visible: false });
110
+ }, remaining);
111
+
112
+ return () => clearTimeout(timer);
113
+ }, [entry, label, query.isError, query.error, agentId]);
114
+
115
+ return <Icon name="Activity" size={size} color={color} />;
116
+ };
117
+ }
118
+
119
+ export function contributePills(client: PluginClientContext) {
120
+ const pills = new Map<string, PillEntry>();
121
+
122
+ function UsagePopoverContent(props: PluginButtonContentProps) {
123
+ return (
124
+ <UsagePopover
125
+ {...props}
126
+ openSkillInPanel={(skill: SkillDetail) => {
127
+ if (props.context !== "agent") return;
128
+ requestOpenSkillInPanel({
129
+ agentId: props.agentId,
130
+ skillName: skill.skillName,
131
+ path: skill.path,
132
+ });
133
+ // Same entry as Skills pill: open the agent workspace panel.
134
+ client.openPanel("usage", {
135
+ workspaceId: props.workspaceId,
136
+ agentId: props.agentId,
137
+ });
138
+ }}
139
+ />
140
+ );
141
+ }
142
+
143
+ function addPill(agentId: string, workspaceId: string) {
144
+ // Agent updates fire every turn — do not re-register (unmounts icon / refires query).
145
+ if (pills.has(agentId)) return;
146
+
147
+ const entry: PillEntry = { hidden: false, settledEmpty: false };
148
+ entry.registration = client.addComposerPill({
149
+ id: "usage",
150
+ workspaceId,
151
+ agentId,
152
+ button: {
153
+ title: TITLE,
154
+ label: TITLE,
155
+ visible: true,
156
+ icon: createPillIcon(agentId, entry),
157
+ behavior: {
158
+ kind: "popover",
159
+ Content: UsagePopoverContent,
160
+ },
161
+ },
162
+ });
163
+ pills.set(agentId, entry);
164
+ }
165
+
166
+ function removePill(agentId: string) {
167
+ pills.get(agentId)?.registration?.remove();
168
+ pills.delete(agentId);
169
+ clearPillDataCache(agentId);
170
+ }
171
+
172
+ /** Remount a previously hidden pill so icon can re-query after turn_ended ingest. */
173
+ function reviveHiddenPill(agentId: string) {
174
+ const entry = pills.get(agentId);
175
+ if (!entry?.hidden || !entry.registration) return;
176
+ entry.hidden = false;
177
+ entry.registration.update({ label: TITLE, visible: true });
178
+ }
179
+
180
+ const unsubscribe = client.paseo.agents.subscribe((update) => {
181
+ if (update.kind === "remove") {
182
+ removePill(update.agentId);
183
+ return;
184
+ }
185
+ const { id, workspaceId, status } = update.agent;
186
+ if (!workspaceId) return;
187
+ if (pills.has(id)) {
188
+ // turn_ended ingest races with idle; remount hidden pills so they can catch up.
189
+ if (status === "idle") reviveHiddenPill(id);
190
+ return;
191
+ }
192
+ addPill(id, workspaceId);
193
+ });
194
+
195
+ client.paseo.agents
196
+ .list()
197
+ .then((result) => {
198
+ result.entries.forEach(({ agent }) => {
199
+ if (agent.workspaceId) addPill(agent.id, agent.workspaceId);
200
+ });
201
+ })
202
+ .catch((error: unknown) => {
203
+ console.error("[activity] could not seed composer pills", error);
204
+ });
205
+
206
+ return () => {
207
+ unsubscribe();
208
+ pills.forEach((entry) => entry.registration?.remove());
209
+ pills.clear();
210
+ };
211
+ }
@@ -0,0 +1,95 @@
1
+ import assert from "node:assert/strict";
2
+ import { test } from "node:test";
3
+ import {
4
+ PROVIDER_FILTER_LIMIT,
5
+ selectProviderOptions,
6
+ type ProviderFilterCandidate,
7
+ } from "./provider-filter.ts";
8
+
9
+ function candidate(
10
+ provider: string,
11
+ agentCount: number,
12
+ messageCount: number,
13
+ ): ProviderFilterCandidate {
14
+ return { provider, label: provider.toUpperCase(), agentCount, messageCount };
15
+ }
16
+
17
+ test("PROVIDER_FILTER_LIMIT is 5", () => {
18
+ assert.equal(PROVIDER_FILTER_LIMIT, 5);
19
+ });
20
+
21
+ test("ranks by agent count, then message count", () => {
22
+ const options = selectProviderOptions(
23
+ [candidate("claude", 3, 1), candidate("codex", 3, 5), candidate("pi", 1, 100)],
24
+ "all",
25
+ );
26
+ assert.deepEqual(
27
+ options.map((item) => item.id),
28
+ ["codex", "claude", "pi"],
29
+ );
30
+ });
31
+
32
+ test("breaks ties by provider id", () => {
33
+ const options = selectProviderOptions(
34
+ [candidate("zeta", 1, 1), candidate("alpha", 1, 1)],
35
+ "all",
36
+ );
37
+ assert.deepEqual(
38
+ options.map((item) => item.id),
39
+ ["alpha", "zeta"],
40
+ );
41
+ });
42
+
43
+ test("caps the list at the limit", () => {
44
+ const providers = ["a", "b", "c", "d", "e", "f", "g"].map((id, index) =>
45
+ candidate(id, 7 - index, 0),
46
+ );
47
+ const options = selectProviderOptions(providers, "all");
48
+ assert.equal(options.length, PROVIDER_FILTER_LIMIT);
49
+ assert.deepEqual(
50
+ options.map((item) => item.id),
51
+ ["a", "b", "c", "d", "e"],
52
+ );
53
+ });
54
+
55
+ test("keeps a selected provider that fell out of the top list", () => {
56
+ const providers = ["a", "b", "c", "d", "e", "f"].map((id, index) =>
57
+ candidate(id, 6 - index, 0),
58
+ );
59
+ const options = selectProviderOptions(providers, "f");
60
+ assert.equal(options.length, PROVIDER_FILTER_LIMIT);
61
+ assert.deepEqual(
62
+ options.map((item) => item.id),
63
+ ["a", "b", "c", "d", "f"],
64
+ );
65
+ });
66
+
67
+ test("keeps order when the selected provider is already in the top list", () => {
68
+ const providers = ["a", "b", "c", "d", "e", "f"].map((id, index) =>
69
+ candidate(id, 6 - index, 0),
70
+ );
71
+ const options = selectProviderOptions(providers, "b");
72
+ assert.deepEqual(
73
+ options.map((item) => item.id),
74
+ ["a", "b", "c", "d", "e"],
75
+ );
76
+ });
77
+
78
+ test("returns all ranked candidates when below the limit", () => {
79
+ const options = selectProviderOptions(
80
+ [candidate("pi", 1, 1), candidate("claude", 2, 0)],
81
+ "all",
82
+ );
83
+ assert.deepEqual(
84
+ options.map((item) => item.id),
85
+ ["claude", "pi"],
86
+ );
87
+ });
88
+
89
+ test("ignores an unknown selected provider", () => {
90
+ const options = selectProviderOptions([candidate("claude", 1, 1)], "ghost");
91
+ assert.deepEqual(
92
+ options.map((item) => item.id),
93
+ ["claude"],
94
+ );
95
+ });
@@ -0,0 +1,44 @@
1
+ /** Max provider chips shown in the global Activity filter bar (excludes All). */
2
+ export const PROVIDER_FILTER_LIMIT = 5;
3
+
4
+ export type ProviderFilterCandidate = {
5
+ provider: string;
6
+ label: string;
7
+ agentCount: number;
8
+ messageCount: number;
9
+ };
10
+
11
+ /**
12
+ * Rank provider chips for the global filter bar: most agents first, then most
13
+ * messages, then provider id. Caps the list at `limit`; a selected provider that
14
+ * fell out of the top `limit` replaces the last entry so the filter stays visible.
15
+ */
16
+ export function selectProviderOptions(
17
+ providers: ReadonlyArray<ProviderFilterCandidate>,
18
+ selected: string,
19
+ limit = PROVIDER_FILTER_LIMIT,
20
+ ): Array<{ id: string; label: string }> {
21
+ const capped = Math.max(0, limit);
22
+ const ranked = [...providers].sort(
23
+ (a, b) =>
24
+ b.agentCount - a.agentCount ||
25
+ b.messageCount - a.messageCount ||
26
+ a.provider.localeCompare(b.provider),
27
+ );
28
+ const top = ranked.slice(0, capped);
29
+ if (capped === 0 || selected === "all") return top.map(toOption);
30
+ const selectedProvider = providers.find((item) => item.provider === selected);
31
+ if (!selectedProvider || top.some((item) => item.provider === selected)) {
32
+ return top.map(toOption);
33
+ }
34
+ if (top.length === capped) {
35
+ top[top.length - 1] = selectedProvider;
36
+ } else {
37
+ top.push(selectedProvider);
38
+ }
39
+ return top.map(toOption);
40
+ }
41
+
42
+ function toOption(item: ProviderFilterCandidate): { id: string; label: string } {
43
+ return { id: item.provider, label: item.label };
44
+ }
@@ -0,0 +1,35 @@
1
+ import assert from "node:assert/strict";
2
+ import { test } from "node:test";
3
+ import { mcpServerColor } from "./rank-color.ts";
4
+
5
+ const ACCENT = "#20744a";
6
+
7
+ test("mcpServerColor is stable for the same server", () => {
8
+ assert.equal(mcpServerColor("github", ACCENT), mcpServerColor("github", ACCENT));
9
+ });
10
+
11
+ test("mcpServerColor spreads servers across distinct colors", () => {
12
+ const servers = [
13
+ "github",
14
+ "playwright",
15
+ "context7",
16
+ "linear",
17
+ "notion",
18
+ "sentry",
19
+ "figma",
20
+ "slack",
21
+ ];
22
+ const colors = new Set(servers.map((server) => mcpServerColor(server, ACCENT)));
23
+ assert.ok(colors.size >= 6, `expected >= 6 distinct colors, got ${colors.size}`);
24
+ });
25
+
26
+ test("mcpServerColor returns hex and falls back to non-hex accents", () => {
27
+ assert.match(mcpServerColor("github", ACCENT), /^#[0-9a-f]{6}$/);
28
+ assert.equal(mcpServerColor("github", "rgb(32, 116, 74)"), "rgb(32, 116, 74)");
29
+ });
30
+
31
+ test("mcpServerColor adds saturation when the theme accent is near-gray", () => {
32
+ const color = mcpServerColor("github", "#e4e4e7");
33
+ assert.notEqual(color, "#e4e4e7");
34
+ assert.match(color, /^#[0-9a-f]{6}$/);
35
+ });
@@ -0,0 +1,69 @@
1
+ type Rgb = [number, number, number];
2
+
3
+ function parseHex(value: string): Rgb | null {
4
+ const hex = value.trim().replace(/^#/, "");
5
+ const full = hex.length === 3 ? hex.split("").map((c) => c + c).join("") : hex;
6
+ if (!/^[0-9a-f]{6}$/i.test(full)) return null;
7
+ return [
8
+ parseInt(full.slice(0, 2), 16),
9
+ parseInt(full.slice(2, 4), 16),
10
+ parseInt(full.slice(4, 6), 16),
11
+ ];
12
+ }
13
+
14
+ function rgbToHsl([r, g, b]: Rgb): [number, number, number] {
15
+ const rr = r / 255;
16
+ const gg = g / 255;
17
+ const bb = b / 255;
18
+ const max = Math.max(rr, gg, bb);
19
+ const min = Math.min(rr, gg, bb);
20
+ const lightness = (max + min) / 2;
21
+ if (max === min) return [0, 0, lightness];
22
+ const delta = max - min;
23
+ const saturation = lightness > 0.5 ? delta / (2 - max - min) : delta / (max + min);
24
+ const hue =
25
+ max === rr
26
+ ? ((gg - bb) / delta + (gg < bb ? 6 : 0)) / 6
27
+ : max === gg
28
+ ? ((bb - rr) / delta + 2) / 6
29
+ : ((rr - gg) / delta + 4) / 6;
30
+ return [hue * 360, saturation, lightness];
31
+ }
32
+
33
+ function hslToHex(hue: number, saturation: number, lightness: number): string {
34
+ const h = ((hue % 360) + 360) % 360;
35
+ const chroma = (1 - Math.abs(2 * lightness - 1)) * saturation;
36
+ const second = chroma * (1 - Math.abs(((h / 60) % 2) - 1));
37
+ const offset = lightness - chroma / 2;
38
+ const [r, g, b] =
39
+ h < 60
40
+ ? [chroma, second, 0]
41
+ : h < 120
42
+ ? [second, chroma, 0]
43
+ : h < 180
44
+ ? [0, chroma, second]
45
+ : h < 240
46
+ ? [0, second, chroma]
47
+ : h < 300
48
+ ? [second, 0, chroma]
49
+ : [chroma, 0, second];
50
+ return `#${[r, g, b]
51
+ .map((value) => Math.round((value + offset) * 255).toString(16).padStart(2, "0"))
52
+ .join("")}`;
53
+ }
54
+
55
+ const GOLDEN_ANGLE = 137.508;
56
+
57
+ export function mcpServerColor(server: string, accent: string): string {
58
+ const rgb = parseHex(accent);
59
+ if (!rgb) return accent;
60
+ let hash = 0;
61
+ for (let i = 0; i < server.length; i += 1) {
62
+ hash = (hash * 31 + server.charCodeAt(i)) >>> 0;
63
+ }
64
+ const [baseHue, baseSaturation, baseLightness] = rgbToHsl(rgb);
65
+ const hue = (baseHue + hash * GOLDEN_ANGLE) % 360;
66
+ const saturation = Math.min(0.75, Math.max(0.45, baseSaturation));
67
+ const lightness = Math.min(0.7, Math.max(0.32, baseLightness));
68
+ return hslToHex(hue, saturation, lightness);
69
+ }