@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
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@koinzhang/paseo-plugin-activity",
3
+ "version": "0.1.0",
4
+ "description": "Local usage analytics for Paseo: tool calls, agents, messages, and model usage.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "files": [
8
+ "paseo-plugin.json",
9
+ "index.client.ts",
10
+ "index.client.tsx",
11
+ "index.server.ts",
12
+ "index.server.tsx",
13
+ "client/",
14
+ "server/",
15
+ "shared/"
16
+ ],
17
+ "scripts": {
18
+ "typecheck": "tsc --noEmit",
19
+ "test": "node --experimental-strip-types --test server/background-sync.test.ts server/handlers-query.test.ts client/pill-data-cache.test.ts client/rank-color.test.ts client/provider-filter.test.ts shared/activity.test.ts shared/insights.test.ts shared/classify.test.ts shared/usage.test.ts shared/format.test.ts server/store.test.ts server/ingest.test.ts server/skill-path.test.ts server/mcp-servers.test.ts server/agents.test.ts server/migrate-data.test.ts server/messages.test.ts server/resolve-model.test.ts"
20
+ },
21
+ "devDependencies": {
22
+ "@getpaseo/plugin": "0.8.0",
23
+ "@tanstack/react-query": "^5.90.11",
24
+ "@types/node": "^24.13.5",
25
+ "@types/react": "~19.2.0",
26
+ "react": "19.1.0",
27
+ "react-native": "0.81.5",
28
+ "typescript": "^5.9.3",
29
+ "zod": "^4.4.3"
30
+ }
31
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "id": "activity",
3
+ "requirements": {
4
+ "paseo": ">=0.8.0"
5
+ }
6
+ }
@@ -0,0 +1,67 @@
1
+ import assert from "node:assert/strict";
2
+ import { describe, it } from "node:test";
3
+ import { agentsFromToolCalls, agentRowFromHook } from "./agents.ts";
4
+ import type { ToolCallRow } from "./store.ts";
5
+
6
+ function row(over: Partial<ToolCallRow> = {}): ToolCallRow {
7
+ return {
8
+ agentId: "a1",
9
+ callId: "c1",
10
+ workspaceId: "w1",
11
+ provider: "claude",
12
+ turnId: null,
13
+ name: "Bash",
14
+ detailType: "shell",
15
+ category: "regular",
16
+ confidence: null,
17
+ skillName: null,
18
+ mcpServer: null,
19
+ mcpTool: null,
20
+ command: "ls",
21
+ filePath: null,
22
+ status: "completed",
23
+ errorMessage: null,
24
+ seq: null,
25
+ ts: null,
26
+ ingestedAt: "2026-09-18T12:00:00.000Z",
27
+ ...over,
28
+ };
29
+ }
30
+
31
+ describe("agentsFromToolCalls", () => {
32
+ it("uses earliest activity as approximate createdAt", () => {
33
+ const agents = agentsFromToolCalls([
34
+ row({ callId: "1", agentId: "a1", ingestedAt: "2026-09-18T12:00:00.000Z" }),
35
+ row({
36
+ callId: "2",
37
+ agentId: "a1",
38
+ ts: "2026-09-17T08:00:00.000Z",
39
+ ingestedAt: "2026-09-18T12:00:00.000Z",
40
+ }),
41
+ row({ callId: "3", agentId: "a2", provider: "codex", ingestedAt: "2026-09-19T01:00:00.000Z" }),
42
+ ]);
43
+ assert.equal(agents.length, 2);
44
+ const a1 = agents.find((a) => a.agentId === "a1");
45
+ assert.equal(a1?.createdAt, "2026-09-17T08:00:00.000Z");
46
+ assert.equal(a1?.provider, "claude");
47
+ });
48
+ });
49
+
50
+ describe("agentRowFromHook", () => {
51
+ it("maps hook agent fields", () => {
52
+ const row = agentRowFromHook(
53
+ {
54
+ id: "x",
55
+ workspaceId: "w",
56
+ parentAgentId: null,
57
+ provider: "claude",
58
+ cwd: "/tmp",
59
+ title: "Hello",
60
+ },
61
+ { createdAt: "2026-09-18T00:00:00.000Z" },
62
+ );
63
+ assert.equal(row.agentId, "x");
64
+ assert.equal(row.title, "Hello");
65
+ assert.equal(row.createdAt, "2026-09-18T00:00:00.000Z");
66
+ });
67
+ });
@@ -0,0 +1,75 @@
1
+ import type { PluginHookAgent } from "@getpaseo/plugin/server";
2
+ import type { AgentRow, ToolCallRow } from "./store.ts";
3
+
4
+ function nowIso(): string {
5
+ return new Date().toISOString();
6
+ }
7
+
8
+ export function agentRowFromHook(
9
+ agent: PluginHookAgent,
10
+ opts: { createdAt?: string; archivedAt?: string | null; updatedAt?: string } = {},
11
+ ): AgentRow {
12
+ const updatedAt = opts.updatedAt ?? nowIso();
13
+ return {
14
+ agentId: agent.id,
15
+ workspaceId: agent.workspaceId,
16
+ parentAgentId: agent.parentAgentId,
17
+ provider: agent.provider,
18
+ title: agent.title,
19
+ createdAt: opts.createdAt ?? updatedAt,
20
+ archivedAt: opts.archivedAt ?? null,
21
+ updatedAt,
22
+ };
23
+ }
24
+
25
+ export function agentRowFromSnapshot(agent: {
26
+ id: string;
27
+ workspaceId?: string | null;
28
+ parentAgentId?: string | null;
29
+ provider: string;
30
+ title?: string | null;
31
+ createdAt: string;
32
+ }): AgentRow {
33
+ const updatedAt = nowIso();
34
+ return {
35
+ agentId: agent.id,
36
+ workspaceId: agent.workspaceId ?? null,
37
+ parentAgentId: agent.parentAgentId ?? null,
38
+ provider: agent.provider,
39
+ title: agent.title ?? null,
40
+ createdAt: agent.createdAt,
41
+ archivedAt: null,
42
+ updatedAt,
43
+ };
44
+ }
45
+
46
+ /** Earliest tool activity per agentId → approximate created_at for unknown agents. */
47
+ export function agentsFromToolCalls(rows: readonly ToolCallRow[]): AgentRow[] {
48
+ const earliest = new Map<
49
+ string,
50
+ { provider: string; workspaceId: string | null; createdAt: string }
51
+ >();
52
+ for (const row of rows) {
53
+ const t = row.ts ?? row.ingestedAt;
54
+ const prev = earliest.get(row.agentId);
55
+ if (!prev || t < prev.createdAt) {
56
+ earliest.set(row.agentId, {
57
+ provider: row.provider,
58
+ workspaceId: row.workspaceId,
59
+ createdAt: t,
60
+ });
61
+ }
62
+ }
63
+ const updatedAt = nowIso();
64
+ return [...earliest.entries()].map(([agentId, info]) => ({
65
+ agentId,
66
+ workspaceId: info.workspaceId,
67
+ parentAgentId: null,
68
+ provider: info.provider,
69
+ title: null,
70
+ createdAt: info.createdAt,
71
+ archivedAt: null,
72
+ updatedAt,
73
+ }));
74
+ }
75
+
@@ -0,0 +1,143 @@
1
+ import assert from "node:assert/strict";
2
+ import { test } from "node:test";
3
+ import { existsSync, mkdtempSync, rmSync } from "node:fs";
4
+ import { tmpdir } from "node:os";
5
+ import { join } from "node:path";
6
+ import type { PluginHandlerContext } from "@getpaseo/plugin/server";
7
+ import { createBackgroundSync } from "./background-sync.ts";
8
+ import { createUsageStore } from "./store.ts";
9
+
10
+ type PaseoApi = PluginHandlerContext["paseo"];
11
+
12
+ function fixture() {
13
+ const dir = mkdtempSync(join(tmpdir(), "silent-history-"));
14
+ const store = createUsageStore({ dir, driver: "sqlite" });
15
+ const path = join(dir, "checkpoints.json");
16
+ const agent = { id: "a1", provider: "codex", createdAt: "2026-09-19", updatedAt: "2026-09-19", lastUserMessageAt: null };
17
+ const paseo = { agents: { list: async () => ({ entries: [{ agent }] }) } } as unknown as PaseoApi;
18
+ return { store, path, agent, paseo, cleanup() { store.close(); rmSync(dir, { recursive: true, force: true }); } };
19
+ }
20
+
21
+ test("successful history checks persist, skip unchanged agents, and rescan changes or new versions", async () => {
22
+ const f = fixture();
23
+ let scans = 0;
24
+ const scan = async () => { scans++; return { syncedAgents: 1, inserted: 0, errors: [] }; };
25
+ const run = async (version = 1) => {
26
+ const sync = createBackgroundSync(f.store, { path: f.path, scan, version });
27
+ try { await sync.request(f.paseo); } finally { sync.stop(); }
28
+ };
29
+ try {
30
+ await run(); await run();
31
+ assert.equal(scans, 1);
32
+ f.agent.updatedAt = "2026-09-20";
33
+ await run(); assert.equal(scans, 2);
34
+ await run(2); assert.equal(scans, 3);
35
+ } finally { f.cleanup(); }
36
+ });
37
+
38
+ test("failed scans are not checkpointed and retry on the next check", async () => {
39
+ const f = fixture();
40
+ let attempts = 0;
41
+ const scan = async () => {
42
+ attempts++;
43
+ return { syncedAgents: 0, inserted: 0, errors: [{ agentId: "a1", message: "temporary failure" }] };
44
+ };
45
+ try {
46
+ for (let i = 0; i < 2; i++) {
47
+ const sync = createBackgroundSync(f.store, { path: f.path, scan });
48
+ await sync.request(f.paseo); sync.stop();
49
+ }
50
+ assert.equal(attempts, 2);
51
+ assert.equal(existsSync(f.path), false);
52
+ } finally { f.cleanup(); }
53
+ });
54
+
55
+ test("concurrent triggers share one scan; stopping prevents completion checkpoints", async () => {
56
+ const f = fixture();
57
+ let scans = 0;
58
+ let release!: () => void;
59
+ const gate = new Promise<void>(resolve => { release = resolve; });
60
+ const scan = async () => { scans++; await gate; return { syncedAgents: 1, inserted: 0, errors: [] }; };
61
+ const sync = createBackgroundSync(f.store, { path: f.path, scan });
62
+ try {
63
+ const first = sync.request(f.paseo);
64
+ assert.equal(sync.request(f.paseo), first);
65
+ await Promise.resolve();
66
+ assert.equal(scans, 1);
67
+ sync.stop(); release(); await first;
68
+ assert.equal(existsSync(f.path), false);
69
+ await sync.request(f.paseo);
70
+ assert.equal(scans, 1);
71
+ } finally { sync.stop(); f.cleanup(); }
72
+ });
73
+
74
+ test("background check upserts listed and tool-call-only agents before scanning", async () => {
75
+ const f = fixture();
76
+ let scans = 0;
77
+ const scan = async () => {
78
+ scans++;
79
+ return { syncedAgents: 1, inserted: 0, errors: [] };
80
+ };
81
+ f.store.upsertMany([{
82
+ agentId: "orphan",
83
+ callId: "c1",
84
+ workspaceId: null,
85
+ provider: "claude",
86
+ turnId: null,
87
+ name: "Bash",
88
+ detailType: "shell",
89
+ category: "regular",
90
+ confidence: null,
91
+ skillName: null,
92
+ mcpServer: null,
93
+ mcpTool: null,
94
+ command: "ls",
95
+ filePath: null,
96
+ status: "completed",
97
+ errorMessage: null,
98
+ seq: null,
99
+ ts: "2026-09-18T12:00:00.000Z",
100
+ ingestedAt: "2026-09-18T12:00:00.000Z",
101
+ }]);
102
+ const sync = createBackgroundSync(f.store, { path: f.path, scan });
103
+ try {
104
+ await sync.request(f.paseo);
105
+ assert.equal(scans, 1);
106
+ assert.ok(f.store.getAgent("a1"));
107
+ assert.ok(f.store.getAgent("orphan"));
108
+ } finally {
109
+ sync.stop();
110
+ f.cleanup();
111
+ }
112
+ });
113
+
114
+ test("stopping after agents.list prevents directory writes", async () => {
115
+ const f = fixture();
116
+ let releaseList!: () => void;
117
+ const listGate = new Promise<void>((resolve) => {
118
+ releaseList = resolve;
119
+ });
120
+ const paseo = {
121
+ agents: {
122
+ list: async () => {
123
+ await listGate;
124
+ return { entries: [{ agent: f.agent }] };
125
+ },
126
+ },
127
+ } as unknown as PaseoApi;
128
+ const sync = createBackgroundSync(f.store, {
129
+ path: f.path,
130
+ scan: async () => ({ syncedAgents: 1, inserted: 0, errors: [] }),
131
+ });
132
+ try {
133
+ const pending = sync.request(paseo);
134
+ await Promise.resolve();
135
+ sync.stop();
136
+ releaseList();
137
+ await pending;
138
+ assert.equal(f.store.selectAgents().length, 0);
139
+ } finally {
140
+ sync.stop();
141
+ f.cleanup();
142
+ }
143
+ });
@@ -0,0 +1,105 @@
1
+ import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ import type { PluginHandlerContext } from "@getpaseo/plugin/server";
4
+ import { LOG_PREFIX } from "../shared/plugin-id.ts";
5
+ import type { UsageStore } from "./store.ts";
6
+ import { defaultDataDir } from "./store.ts";
7
+ import { agentRowFromSnapshot, agentsFromToolCalls } from "./agents.ts";
8
+ import { resyncAgents } from "./handlers.ts";
9
+
10
+ type PaseoApi = PluginHandlerContext["paseo"];
11
+
12
+ // Bump when historical ingestion semantics change, not on UI-only releases.
13
+ const BACKFILL_VERSION = 1;
14
+ const CHECK_INTERVAL_MS = 5 * 60_000;
15
+
16
+ export function createBackgroundSync(store: UsageStore, options: {
17
+ path?: string;
18
+ version?: number;
19
+ scan?: typeof resyncAgents;
20
+ } = {}) {
21
+ const path = options.path ?? join(defaultDataDir(), "history-checkpoints.json");
22
+ const version = options.version ?? BACKFILL_VERSION;
23
+ const scan = options.scan ?? resyncAgents;
24
+ let checkpoints: Record<string, string> = Object.create(null);
25
+ try {
26
+ const saved = JSON.parse(readFileSync(path, "utf8"));
27
+ if (saved.version === version && saved.agents && typeof saved.agents === "object") {
28
+ checkpoints = Object.assign(Object.create(null), saved.agents);
29
+ }
30
+ } catch (error) {
31
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
32
+ console.error("[activity] history checkpoints unavailable; will rebuild", error);
33
+ }
34
+ }
35
+ const controller = new AbortController();
36
+ let running: Promise<void> | undefined;
37
+ let timer: ReturnType<typeof setTimeout> | undefined;
38
+ let sdk: PaseoApi | undefined;
39
+
40
+ function persistCheckpoints(next: Record<string, string>) {
41
+ mkdirSync(dirname(path), { recursive: true });
42
+ writeFileSync(`${path}.tmp`, JSON.stringify({ version, agents: next }), { mode: 0o600 });
43
+ renameSync(`${path}.tmp`, path);
44
+ checkpoints = next;
45
+ }
46
+
47
+ async function check(paseo: PaseoApi) {
48
+ const listed = await paseo.agents.list();
49
+ if (controller.signal.aborted) return;
50
+ // Directory synchronization belongs to the background task, never a UI read.
51
+ store.upsertAgents(listed.entries.map(({ agent }) => agentRowFromSnapshot(agent)));
52
+ const known = new Set(store.selectAgents().map(agent => agent.agentId));
53
+ store.upsertAgents(agentsFromToolCalls(store.select()).filter(agent => !known.has(agent.agentId)));
54
+
55
+ const liveIds = new Set(listed.entries.map(({ agent }) => agent.id));
56
+ const pruned: Record<string, string> = Object.create(null);
57
+ for (const [id, stamp] of Object.entries(checkpoints)) {
58
+ if (liveIds.has(id)) pruned[id] = stamp;
59
+ }
60
+ if (Object.keys(pruned).length !== Object.keys(checkpoints).length) {
61
+ persistCheckpoints(pruned);
62
+ }
63
+
64
+ for (const { agent } of listed.entries) {
65
+ if (controller.signal.aborted) return;
66
+ const stamp = `${agent.updatedAt}:${agent.lastUserMessageAt ?? ""}`;
67
+ if (checkpoints[agent.id] === stamp) continue;
68
+ const result = await scan(store, paseo, [agent.id], controller.signal, listed.entries);
69
+ if (controller.signal.aborted) return;
70
+ if (result.errors.length || result.syncedAgents !== 1) {
71
+ console.error("[activity] background history scan incomplete", agent.id, result.errors);
72
+ continue;
73
+ }
74
+ persistCheckpoints({ ...checkpoints, [agent.id]: stamp });
75
+ }
76
+ }
77
+
78
+ function request(paseo: PaseoApi): Promise<void> {
79
+ if (controller.signal.aborted) return Promise.resolve();
80
+ sdk = paseo;
81
+ if (running) return running;
82
+ if (timer) return Promise.resolve();
83
+ running = check(paseo).catch(error => {
84
+ if (!controller.signal.aborted) console.error("[activity] background history check failed", error);
85
+ }).finally(() => {
86
+ running = undefined;
87
+ if (!controller.signal.aborted) {
88
+ timer = setTimeout(() => {
89
+ timer = undefined;
90
+ if (sdk) void request(sdk);
91
+ }, CHECK_INTERVAL_MS);
92
+ timer.unref();
93
+ }
94
+ });
95
+ return running;
96
+ }
97
+
98
+ return {
99
+ request,
100
+ stop() {
101
+ controller.abort();
102
+ if (timer) clearTimeout(timer);
103
+ },
104
+ };
105
+ }
@@ -0,0 +1,91 @@
1
+ import assert from "node:assert/strict";
2
+ import { mkdtempSync, rmSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { test } from "node:test";
6
+ import type { PluginHandlerContext } from "@getpaseo/plugin/server";
7
+ import {
8
+ createActivityByDayHandler,
9
+ createByProviderHandler,
10
+ createSkillsByNameHandler,
11
+ } from "./handlers.ts";
12
+ import { createUsageStore, type ToolCallRow } from "./store.ts";
13
+
14
+ type PaseoApi = PluginHandlerContext["paseo"];
15
+
16
+ function row(over: Partial<ToolCallRow> = {}): ToolCallRow {
17
+ return {
18
+ agentId: "a1",
19
+ callId: "c1",
20
+ workspaceId: "w1",
21
+ provider: "codex",
22
+ turnId: null,
23
+ name: "Skill",
24
+ detailType: null,
25
+ category: "skill",
26
+ confidence: "exact",
27
+ skillName: "demo",
28
+ mcpServer: null,
29
+ mcpTool: null,
30
+ command: null,
31
+ filePath: null,
32
+ status: "completed",
33
+ errorMessage: null,
34
+ seq: null,
35
+ ts: "2026-09-19T12:00:00.000Z",
36
+ ingestedAt: "2026-09-19T12:00:00.000Z",
37
+ ...over,
38
+ };
39
+ }
40
+
41
+ test("by-provider and activity-by-day read the local store only", async () => {
42
+ const dir = mkdtempSync(join(tmpdir(), "fast-query-"));
43
+ const store = createUsageStore({ dir, driver: "sqlite" });
44
+ try {
45
+ store.upsertMany([row()]);
46
+ store.upsertAgents([{
47
+ agentId: "a1",
48
+ workspaceId: "w1",
49
+ parentAgentId: null,
50
+ provider: "codex",
51
+ title: null,
52
+ createdAt: "2026-09-19T12:00:00.000Z",
53
+ archivedAt: null,
54
+ updatedAt: "2026-09-19T12:00:00.000Z",
55
+ }]);
56
+ const providers = await createByProviderHandler(store)({});
57
+ assert.equal(providers.providers[0]?.agentCount, 1);
58
+ assert.equal(providers.providers[0]?.skills[0]?.skillName, "demo");
59
+ const activity = await createActivityByDayHandler(store)({});
60
+ assert.equal(activity.days[0]?.skills, 1);
61
+ assert.equal(activity.days[0]?.agents, 1);
62
+ } finally {
63
+ store.close();
64
+ rmSync(dir, { recursive: true, force: true });
65
+ }
66
+ });
67
+
68
+ test("skills-by-name never awaits agent.refresh", async () => {
69
+ const dir = mkdtempSync(join(tmpdir(), "fast-skills-"));
70
+ const store = createUsageStore({ dir, driver: "sqlite" });
71
+ let refreshed = 0;
72
+ const paseo = {
73
+ agents: {
74
+ ref: () => ({
75
+ current: () => null,
76
+ refresh: async () => {
77
+ refreshed += 1;
78
+ },
79
+ }),
80
+ },
81
+ } as unknown as PaseoApi;
82
+ try {
83
+ store.upsertMany([row()]);
84
+ const result = await createSkillsByNameHandler(store)({ agentId: "a1" }, { paseo });
85
+ assert.equal(result.items[0]?.skillName, "demo");
86
+ assert.equal(refreshed, 0);
87
+ } finally {
88
+ store.close();
89
+ rmSync(dir, { recursive: true, force: true });
90
+ }
91
+ });