@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.
- package/client/activity-heatmap.tsx +138 -0
- package/client/global-surface.tsx +689 -0
- package/client/panel.tsx +423 -0
- package/client/pending-skill.ts +50 -0
- package/client/pill-data-cache.test.ts +68 -0
- package/client/pill-data-cache.ts +37 -0
- package/client/pill.tsx +211 -0
- package/client/provider-filter.test.ts +95 -0
- package/client/provider-filter.ts +44 -0
- package/client/rank-color.test.ts +35 -0
- package/client/rank-color.ts +69 -0
- package/client/usage-popover.tsx +383 -0
- package/client/usage-query.tsx +54 -0
- package/client/usage-stats.tsx +18 -0
- package/client/use-app-language.ts +14 -0
- package/client/web.ts +28 -0
- package/index.client.tsx +62 -0
- package/index.server.ts +128 -0
- package/package.json +31 -0
- package/paseo-plugin.json +6 -0
- package/server/agents.test.ts +67 -0
- package/server/agents.ts +75 -0
- package/server/background-sync.test.ts +143 -0
- package/server/background-sync.ts +105 -0
- package/server/handlers-query.test.ts +91 -0
- package/server/handlers.ts +531 -0
- package/server/host-types.ts +14 -0
- package/server/ingest.test.ts +233 -0
- package/server/ingest.ts +277 -0
- package/server/mcp-servers.test.ts +70 -0
- package/server/mcp-servers.ts +212 -0
- package/server/messages.test.ts +150 -0
- package/server/migrate-data.test.ts +65 -0
- package/server/migrate-data.ts +54 -0
- package/server/resolve-model.test.ts +11 -0
- package/server/resolve-model.ts +30 -0
- package/server/skill-path.test.ts +56 -0
- package/server/skill-path.ts +85 -0
- package/server/store.test.ts +221 -0
- package/server/store.ts +909 -0
- package/server/ttl-cache.ts +34 -0
- package/shared/activity.test.ts +86 -0
- package/shared/activity.ts +185 -0
- package/shared/classify.test.ts +306 -0
- package/shared/classify.ts +378 -0
- package/shared/format.test.ts +48 -0
- package/shared/format.ts +45 -0
- package/shared/insights.test.ts +122 -0
- package/shared/insights.ts +188 -0
- package/shared/plugin-id.ts +7 -0
- package/shared/usage.test.ts +668 -0
- package/shared/usage.ts +726 -0
package/shared/usage.ts
ADDED
|
@@ -0,0 +1,726 @@
|
|
|
1
|
+
import { defineRpc } from "@getpaseo/plugin";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { CategorySchema, ConfidenceSchema, normalizeProvider } from "./classify.ts";
|
|
4
|
+
import { formatDisplayName } from "./format.ts";
|
|
5
|
+
|
|
6
|
+
export const usageSummaryRpc = defineRpc({
|
|
7
|
+
name: "usage.summary",
|
|
8
|
+
input: z.object({
|
|
9
|
+
from: z.string().optional(),
|
|
10
|
+
to: z.string().optional(),
|
|
11
|
+
agentId: z.string().optional(),
|
|
12
|
+
workspaceId: z.string().optional(),
|
|
13
|
+
}),
|
|
14
|
+
output: z.object({
|
|
15
|
+
shellCalls: z.number().int().nonnegative(),
|
|
16
|
+
shellFailures: z.number().int().nonnegative(),
|
|
17
|
+
fileReads: z.number().int().nonnegative(),
|
|
18
|
+
fileWrites: z.number().int().nonnegative(),
|
|
19
|
+
skillCalls: z.object({
|
|
20
|
+
exact: z.number().int().nonnegative(),
|
|
21
|
+
inferred: z.number().int().nonnegative(),
|
|
22
|
+
low: z.number().int().nonnegative(),
|
|
23
|
+
}),
|
|
24
|
+
mcpCalls: z.number().int().nonnegative(),
|
|
25
|
+
mcpFailures: z.number().int().nonnegative(),
|
|
26
|
+
toolCallsByKind: z.record(z.string(), z.number().int().nonnegative()),
|
|
27
|
+
}),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
export const usageListRpc = defineRpc({
|
|
31
|
+
name: "usage.list",
|
|
32
|
+
input: z.object({
|
|
33
|
+
category: CategorySchema.optional(),
|
|
34
|
+
confidence: ConfidenceSchema.optional(),
|
|
35
|
+
skillName: z.string().optional(),
|
|
36
|
+
mcpServer: z.string().optional(),
|
|
37
|
+
agentId: z.string().optional(),
|
|
38
|
+
from: z.string().optional(),
|
|
39
|
+
to: z.string().optional(),
|
|
40
|
+
limit: z.number().int().positive().max(1000).optional(),
|
|
41
|
+
offset: z.number().int().nonnegative().optional(),
|
|
42
|
+
}),
|
|
43
|
+
output: z.object({
|
|
44
|
+
total: z.number().int().nonnegative(),
|
|
45
|
+
rows: z.array(
|
|
46
|
+
z.object({
|
|
47
|
+
agentId: z.string(),
|
|
48
|
+
callId: z.string(),
|
|
49
|
+
provider: z.string(),
|
|
50
|
+
name: z.string(),
|
|
51
|
+
category: z.string(),
|
|
52
|
+
confidence: z.string().nullable(),
|
|
53
|
+
skillName: z.string().nullable(),
|
|
54
|
+
mcpServer: z.string().nullable(),
|
|
55
|
+
mcpTool: z.string().nullable(),
|
|
56
|
+
command: z.string().nullable(),
|
|
57
|
+
status: z.string().nullable(),
|
|
58
|
+
ts: z.string().nullable(),
|
|
59
|
+
ingestedAt: z.string(),
|
|
60
|
+
}),
|
|
61
|
+
),
|
|
62
|
+
}),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
export const SkillByNameItemSchema = z.object({
|
|
66
|
+
skillName: z.string(),
|
|
67
|
+
exact: z.number().int().nonnegative(),
|
|
68
|
+
inferred: z.number().int().nonnegative(),
|
|
69
|
+
low: z.number().int().nonnegative(),
|
|
70
|
+
/** exact + inferred only; low is excluded from call counts. */
|
|
71
|
+
total: z.number().int().nonnegative(),
|
|
72
|
+
lastUsedAt: z.string().nullable(),
|
|
73
|
+
/** Absolute path to SKILL.md when known (from read/shell or resolved). */
|
|
74
|
+
skillPath: z.string().nullable(),
|
|
75
|
+
});
|
|
76
|
+
export type SkillByNameItem = z.infer<typeof SkillByNameItemSchema>;
|
|
77
|
+
|
|
78
|
+
export const McpByToolItemSchema = z.object({
|
|
79
|
+
server: z.string(),
|
|
80
|
+
tool: z.string(),
|
|
81
|
+
count: z.number().int().nonnegative(),
|
|
82
|
+
failures: z.number().int().nonnegative(),
|
|
83
|
+
lastUsedAt: z.string().nullable(),
|
|
84
|
+
});
|
|
85
|
+
export type McpByToolItem = z.infer<typeof McpByToolItemSchema>;
|
|
86
|
+
|
|
87
|
+
export const ModelByNameItemSchema = z.object({
|
|
88
|
+
model: z.string(),
|
|
89
|
+
count: z.number().int().nonnegative(),
|
|
90
|
+
});
|
|
91
|
+
export type ModelByNameItem = z.infer<typeof ModelByNameItemSchema>;
|
|
92
|
+
|
|
93
|
+
export const usageSkillsByNameRpc = defineRpc({
|
|
94
|
+
name: "usage.skills-by-name",
|
|
95
|
+
input: z.object({
|
|
96
|
+
from: z.string().optional(),
|
|
97
|
+
to: z.string().optional(),
|
|
98
|
+
agentId: z.string().optional(),
|
|
99
|
+
workspaceId: z.string().optional(),
|
|
100
|
+
}),
|
|
101
|
+
output: z.object({
|
|
102
|
+
items: z.array(SkillByNameItemSchema),
|
|
103
|
+
}),
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
export const usageMcpByToolRpc = defineRpc({
|
|
107
|
+
name: "usage.mcp-by-tool",
|
|
108
|
+
input: z.object({
|
|
109
|
+
from: z.string().optional(),
|
|
110
|
+
to: z.string().optional(),
|
|
111
|
+
agentId: z.string().optional(),
|
|
112
|
+
workspaceId: z.string().optional(),
|
|
113
|
+
}),
|
|
114
|
+
output: z.object({
|
|
115
|
+
items: z.array(McpByToolItemSchema),
|
|
116
|
+
}),
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
export const usageExportRpc = defineRpc({
|
|
120
|
+
name: "usage.export",
|
|
121
|
+
input: z.object({
|
|
122
|
+
from: z.string().optional(),
|
|
123
|
+
to: z.string().optional(),
|
|
124
|
+
agentId: z.string().optional(),
|
|
125
|
+
}),
|
|
126
|
+
output: z.object({
|
|
127
|
+
markdown: z.string(),
|
|
128
|
+
}),
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
export const usageReadSkillRpc = defineRpc({
|
|
132
|
+
name: "usage.read-skill",
|
|
133
|
+
input: z.object({
|
|
134
|
+
path: z.string().min(1),
|
|
135
|
+
}),
|
|
136
|
+
output: z.object({
|
|
137
|
+
path: z.string(),
|
|
138
|
+
skillName: z.string().nullable(),
|
|
139
|
+
body: z.string(),
|
|
140
|
+
}),
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const SkillCallsSchema = z.object({
|
|
144
|
+
exact: z.number().int().nonnegative(),
|
|
145
|
+
inferred: z.number().int().nonnegative(),
|
|
146
|
+
low: z.number().int().nonnegative(),
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
export const ProviderUsageItemSchema = z.object({
|
|
150
|
+
provider: z.string(),
|
|
151
|
+
label: z.string(),
|
|
152
|
+
shellCalls: z.number().int().nonnegative(),
|
|
153
|
+
shellFailures: z.number().int().nonnegative(),
|
|
154
|
+
fileReads: z.number().int().nonnegative(),
|
|
155
|
+
fileWrites: z.number().int().nonnegative(),
|
|
156
|
+
skillCalls: SkillCallsSchema,
|
|
157
|
+
skills: z.array(SkillByNameItemSchema),
|
|
158
|
+
mcpCalls: z.number().int().nonnegative(),
|
|
159
|
+
mcpFailures: z.number().int().nonnegative(),
|
|
160
|
+
mcpTools: z.array(McpByToolItemSchema),
|
|
161
|
+
shellTop: z.array(
|
|
162
|
+
z.object({
|
|
163
|
+
command: z.string(),
|
|
164
|
+
count: z.number().int().nonnegative(),
|
|
165
|
+
failures: z.number().int().nonnegative(),
|
|
166
|
+
}),
|
|
167
|
+
),
|
|
168
|
+
/** Messages-weighted model ranks (015); empty when no stamped models. */
|
|
169
|
+
models: z.array(ModelByNameItemSchema),
|
|
170
|
+
agentCount: z.number().int().nonnegative(),
|
|
171
|
+
/** Distinct non-null workspace_id among agents for this provider (same window). */
|
|
172
|
+
workspaceCount: z.number().int().nonnegative(),
|
|
173
|
+
messageCount: z.number().int().nonnegative(),
|
|
174
|
+
callCount: z.number().int().nonnegative(),
|
|
175
|
+
});
|
|
176
|
+
export type ProviderUsageItem = z.infer<typeof ProviderUsageItemSchema>;
|
|
177
|
+
|
|
178
|
+
export const usageByProviderRpc = defineRpc({
|
|
179
|
+
name: "usage.by-provider",
|
|
180
|
+
input: z.object({
|
|
181
|
+
from: z.string().optional(),
|
|
182
|
+
to: z.string().optional(),
|
|
183
|
+
workspaceId: z.string().optional(),
|
|
184
|
+
}),
|
|
185
|
+
output: z.object({
|
|
186
|
+
totals: z.object({
|
|
187
|
+
shellCalls: z.number().int().nonnegative(),
|
|
188
|
+
fileReads: z.number().int().nonnegative(),
|
|
189
|
+
fileWrites: z.number().int().nonnegative(),
|
|
190
|
+
skillCalls: SkillCallsSchema,
|
|
191
|
+
mcpCalls: z.number().int().nonnegative(),
|
|
192
|
+
/** Distinct non-null workspace_id across all providers (do not sum per-provider). */
|
|
193
|
+
workspaceCount: z.number().int().nonnegative(),
|
|
194
|
+
}),
|
|
195
|
+
providers: z.array(ProviderUsageItemSchema),
|
|
196
|
+
}),
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
const PROVIDER_LABELS: Record<string, string> = {
|
|
200
|
+
claude: "Claude",
|
|
201
|
+
opencode: "OpenCode",
|
|
202
|
+
codex: "Codex",
|
|
203
|
+
pi: "Pi",
|
|
204
|
+
cursor: "Cursor",
|
|
205
|
+
codebuddy: "CodeBuddy",
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
/** Display label for a normalized provider id. */
|
|
209
|
+
export function providerLabel(provider: string): string {
|
|
210
|
+
return PROVIDER_LABELS[provider] ?? provider.charAt(0).toUpperCase() + provider.slice(1);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function emptySkillCalls() {
|
|
214
|
+
return { exact: 0, inferred: 0, low: 0 };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Exclusive KPI buckets — see specs/014-shell-file-ops. */
|
|
218
|
+
export function isShellCall(row: { category: string; detailType: string | null }): boolean {
|
|
219
|
+
return row.category === "regular" && row.detailType === "shell";
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function isFileRead(row: { category: string; detailType: string | null }): boolean {
|
|
223
|
+
return row.category === "regular" && row.detailType === "read";
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const FILE_WRITE_TYPES = new Set(["write", "edit", "delete"]);
|
|
227
|
+
|
|
228
|
+
export function isFileWrite(row: { category: string; detailType: string | null }): boolean {
|
|
229
|
+
return (
|
|
230
|
+
row.category === "regular" &&
|
|
231
|
+
row.detailType != null &&
|
|
232
|
+
FILE_WRITE_TYPES.has(row.detailType)
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Aggregate tool_calls by normalizeProvider(provider).
|
|
237
|
+
* `agentCount` / `workspaceCount` come from `agents` (created registry), not tool_call distinct ids.
|
|
238
|
+
*/
|
|
239
|
+
export function aggregateByProvider(
|
|
240
|
+
rows: ReadonlyArray<{
|
|
241
|
+
agentId: string;
|
|
242
|
+
provider: string;
|
|
243
|
+
category: string;
|
|
244
|
+
confidence: string | null;
|
|
245
|
+
skillName: string | null;
|
|
246
|
+
mcpServer: string | null;
|
|
247
|
+
mcpTool: string | null;
|
|
248
|
+
detailType: string | null;
|
|
249
|
+
status: string | null;
|
|
250
|
+
ts: string | null;
|
|
251
|
+
ingestedAt: string;
|
|
252
|
+
filePath?: string | null;
|
|
253
|
+
command?: string | null;
|
|
254
|
+
}>,
|
|
255
|
+
agents: ReadonlyArray<{
|
|
256
|
+
agentId: string;
|
|
257
|
+
provider: string;
|
|
258
|
+
workspaceId?: string | null;
|
|
259
|
+
}> = [],
|
|
260
|
+
messages: ReadonlyArray<{ provider: string; model?: string | null }> = [],
|
|
261
|
+
): {
|
|
262
|
+
totals: {
|
|
263
|
+
shellCalls: number;
|
|
264
|
+
fileReads: number;
|
|
265
|
+
fileWrites: number;
|
|
266
|
+
skillCalls: { exact: number; inferred: number; low: number };
|
|
267
|
+
mcpCalls: number;
|
|
268
|
+
workspaceCount: number;
|
|
269
|
+
};
|
|
270
|
+
providers: ProviderUsageItem[];
|
|
271
|
+
} {
|
|
272
|
+
type Acc = Omit<
|
|
273
|
+
ProviderUsageItem,
|
|
274
|
+
"skills" | "mcpTools" | "shellTop" | "models" | "agentCount" | "workspaceCount"
|
|
275
|
+
> & {
|
|
276
|
+
agents: Set<string>;
|
|
277
|
+
workspaces: Set<string>;
|
|
278
|
+
skillRows: Array<{
|
|
279
|
+
category: string;
|
|
280
|
+
skillName: string | null;
|
|
281
|
+
confidence: string | null;
|
|
282
|
+
ts: string | null;
|
|
283
|
+
ingestedAt: string;
|
|
284
|
+
filePath?: string | null;
|
|
285
|
+
}>;
|
|
286
|
+
mcpRows: Array<{
|
|
287
|
+
category: string;
|
|
288
|
+
mcpServer: string | null;
|
|
289
|
+
mcpTool: string | null;
|
|
290
|
+
status: string | null;
|
|
291
|
+
}>;
|
|
292
|
+
shellRows: Array<{
|
|
293
|
+
category: string;
|
|
294
|
+
detailType: string | null;
|
|
295
|
+
command: string | null;
|
|
296
|
+
status: string | null;
|
|
297
|
+
}>;
|
|
298
|
+
messageRows: Array<{ model?: string | null }>;
|
|
299
|
+
};
|
|
300
|
+
const map = new Map<string, Acc>();
|
|
301
|
+
const allWorkspaces = new Set<string>();
|
|
302
|
+
const totals = {
|
|
303
|
+
shellCalls: 0,
|
|
304
|
+
fileReads: 0,
|
|
305
|
+
fileWrites: 0,
|
|
306
|
+
skillCalls: emptySkillCalls(),
|
|
307
|
+
mcpCalls: 0,
|
|
308
|
+
workspaceCount: 0,
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
function ensure(provider: string): Acc {
|
|
312
|
+
let acc = map.get(provider);
|
|
313
|
+
if (!acc) {
|
|
314
|
+
acc = {
|
|
315
|
+
provider,
|
|
316
|
+
label: providerLabel(provider),
|
|
317
|
+
shellCalls: 0,
|
|
318
|
+
shellFailures: 0,
|
|
319
|
+
fileReads: 0,
|
|
320
|
+
fileWrites: 0,
|
|
321
|
+
skillCalls: emptySkillCalls(),
|
|
322
|
+
mcpCalls: 0,
|
|
323
|
+
mcpFailures: 0,
|
|
324
|
+
callCount: 0,
|
|
325
|
+
messageCount: 0,
|
|
326
|
+
agents: new Set(),
|
|
327
|
+
workspaces: new Set(),
|
|
328
|
+
skillRows: [],
|
|
329
|
+
mcpRows: [],
|
|
330
|
+
shellRows: [],
|
|
331
|
+
messageRows: [],
|
|
332
|
+
};
|
|
333
|
+
map.set(provider, acc);
|
|
334
|
+
}
|
|
335
|
+
return acc;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
for (const row of rows) {
|
|
339
|
+
const provider = normalizeProvider(row.provider);
|
|
340
|
+
const acc = ensure(provider);
|
|
341
|
+
acc.callCount += 1;
|
|
342
|
+
|
|
343
|
+
if (row.category === "skill") {
|
|
344
|
+
acc.skillRows.push(row);
|
|
345
|
+
if (row.confidence === "exact") {
|
|
346
|
+
acc.skillCalls.exact += 1;
|
|
347
|
+
totals.skillCalls.exact += 1;
|
|
348
|
+
} else if (row.confidence === "low") {
|
|
349
|
+
acc.skillCalls.low += 1;
|
|
350
|
+
totals.skillCalls.low += 1;
|
|
351
|
+
} else {
|
|
352
|
+
acc.skillCalls.inferred += 1;
|
|
353
|
+
totals.skillCalls.inferred += 1;
|
|
354
|
+
}
|
|
355
|
+
} else if (row.category === "mcp") {
|
|
356
|
+
acc.mcpRows.push(row);
|
|
357
|
+
acc.mcpCalls += 1;
|
|
358
|
+
totals.mcpCalls += 1;
|
|
359
|
+
if (row.status === "failed") acc.mcpFailures += 1;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if (isShellCall(row)) {
|
|
363
|
+
acc.shellCalls += 1;
|
|
364
|
+
totals.shellCalls += 1;
|
|
365
|
+
if (row.status === "failed") acc.shellFailures += 1;
|
|
366
|
+
acc.shellRows.push({
|
|
367
|
+
category: row.category,
|
|
368
|
+
detailType: row.detailType,
|
|
369
|
+
command: row.command ?? null,
|
|
370
|
+
status: row.status,
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
if (isFileRead(row)) {
|
|
374
|
+
acc.fileReads += 1;
|
|
375
|
+
totals.fileReads += 1;
|
|
376
|
+
}
|
|
377
|
+
if (isFileWrite(row)) {
|
|
378
|
+
acc.fileWrites += 1;
|
|
379
|
+
totals.fileWrites += 1;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
for (const agent of agents) {
|
|
384
|
+
const provider = normalizeProvider(agent.provider);
|
|
385
|
+
const acc = ensure(provider);
|
|
386
|
+
acc.agents.add(agent.agentId);
|
|
387
|
+
const workspaceId = agent.workspaceId?.trim();
|
|
388
|
+
if (workspaceId) {
|
|
389
|
+
acc.workspaces.add(workspaceId);
|
|
390
|
+
allWorkspaces.add(workspaceId);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
for (const message of messages) {
|
|
395
|
+
const acc = ensure(normalizeProvider(message.provider));
|
|
396
|
+
acc.messageCount += 1;
|
|
397
|
+
acc.messageRows.push({ model: message.model });
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
totals.workspaceCount = allWorkspaces.size;
|
|
401
|
+
|
|
402
|
+
const providers = [...map.values()]
|
|
403
|
+
.map(({ agents: agentSet, workspaces, skillRows, mcpRows, shellRows, messageRows, ...rest }) => ({
|
|
404
|
+
...rest,
|
|
405
|
+
agentCount: agentSet.size,
|
|
406
|
+
workspaceCount: workspaces.size,
|
|
407
|
+
skills: aggregateSkillsByName(skillRows),
|
|
408
|
+
mcpTools: aggregateMcpByTool(mcpRows),
|
|
409
|
+
shellTop: aggregateShellTop(shellRows),
|
|
410
|
+
models: aggregateModelsByName(messageRows),
|
|
411
|
+
}))
|
|
412
|
+
.sort(
|
|
413
|
+
(a, b) =>
|
|
414
|
+
b.callCount - a.callCount ||
|
|
415
|
+
b.agentCount - a.agentCount ||
|
|
416
|
+
a.provider.localeCompare(b.provider),
|
|
417
|
+
);
|
|
418
|
+
|
|
419
|
+
return { totals, providers };
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** Aggregate user messages by model id (015). Null/blank models are skipped. */
|
|
423
|
+
export function aggregateModelsByName(
|
|
424
|
+
messages: ReadonlyArray<{ model?: string | null }>,
|
|
425
|
+
): ModelByNameItem[] {
|
|
426
|
+
const map = new Map<string, number>();
|
|
427
|
+
for (const message of messages) {
|
|
428
|
+
const model = message.model?.trim();
|
|
429
|
+
if (!model) continue;
|
|
430
|
+
map.set(model, (map.get(model) ?? 0) + 1);
|
|
431
|
+
}
|
|
432
|
+
return [...map.entries()]
|
|
433
|
+
.map(([model, count]) => ({ model, count }))
|
|
434
|
+
.sort((a, b) => b.count - a.count || a.model.localeCompare(b.model));
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** Prefer a concrete SKILL.md path over a non-md path or empty. */
|
|
438
|
+
export function preferSkillPath(
|
|
439
|
+
current: string | null | undefined,
|
|
440
|
+
candidate: string | null | undefined,
|
|
441
|
+
): string | null {
|
|
442
|
+
if (!candidate?.trim()) return current ?? null;
|
|
443
|
+
const next = candidate.trim();
|
|
444
|
+
if (!current) return next;
|
|
445
|
+
const currentIsSkillMd = /SKILL\.md$/i.test(current);
|
|
446
|
+
const nextIsSkillMd = /SKILL\.md$/i.test(next);
|
|
447
|
+
if (nextIsSkillMd && !currentIsSkillMd) return next;
|
|
448
|
+
return current;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/** Aggregate skill rows by skillName. `total` = exact + inferred only (low excluded). */
|
|
452
|
+
export function aggregateSkillsByName(
|
|
453
|
+
rows: ReadonlyArray<{
|
|
454
|
+
category: string;
|
|
455
|
+
skillName: string | null;
|
|
456
|
+
confidence: string | null;
|
|
457
|
+
ts: string | null;
|
|
458
|
+
ingestedAt: string;
|
|
459
|
+
filePath?: string | null;
|
|
460
|
+
}>,
|
|
461
|
+
): SkillByNameItem[] {
|
|
462
|
+
const map = new Map<string, SkillByNameItem>();
|
|
463
|
+
for (const row of rows) {
|
|
464
|
+
if (row.category !== "skill") continue;
|
|
465
|
+
const name = row.skillName?.trim();
|
|
466
|
+
if (!name) continue;
|
|
467
|
+
let item = map.get(name);
|
|
468
|
+
if (!item) {
|
|
469
|
+
item = {
|
|
470
|
+
skillName: name,
|
|
471
|
+
exact: 0,
|
|
472
|
+
inferred: 0,
|
|
473
|
+
low: 0,
|
|
474
|
+
total: 0,
|
|
475
|
+
lastUsedAt: null,
|
|
476
|
+
skillPath: null,
|
|
477
|
+
};
|
|
478
|
+
map.set(name, item);
|
|
479
|
+
}
|
|
480
|
+
if (row.confidence === "exact") {
|
|
481
|
+
item.exact += 1;
|
|
482
|
+
item.total += 1;
|
|
483
|
+
} else if (row.confidence === "low") {
|
|
484
|
+
item.low += 1;
|
|
485
|
+
// low is tracked but not counted in total / call counts
|
|
486
|
+
} else {
|
|
487
|
+
item.inferred += 1;
|
|
488
|
+
item.total += 1;
|
|
489
|
+
}
|
|
490
|
+
const at = row.ts ?? row.ingestedAt;
|
|
491
|
+
if (!item.lastUsedAt || at > item.lastUsedAt) item.lastUsedAt = at;
|
|
492
|
+
item.skillPath = preferSkillPath(item.skillPath, row.filePath);
|
|
493
|
+
}
|
|
494
|
+
return [...map.values()].sort(
|
|
495
|
+
(a, b) => b.total - a.total || a.skillName.localeCompare(b.skillName),
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/** First token of a shell command for top-command grouping. */
|
|
500
|
+
export function shellCommandHead(command: string): string {
|
|
501
|
+
const trimmed = command.trim();
|
|
502
|
+
if (!trimmed) return "(empty)";
|
|
503
|
+
const token = trimmed.split(/\s+/)[0] ?? "(empty)";
|
|
504
|
+
return token.replace(/^.*\//, "") || token;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
export function aggregateShellTop(
|
|
508
|
+
rows: ReadonlyArray<{
|
|
509
|
+
category?: string;
|
|
510
|
+
detailType: string | null;
|
|
511
|
+
command: string | null;
|
|
512
|
+
status: string | null;
|
|
513
|
+
}>,
|
|
514
|
+
limit = 20,
|
|
515
|
+
): Array<{ command: string; count: number; failures: number }> {
|
|
516
|
+
const map = new Map<string, { count: number; failures: number }>();
|
|
517
|
+
for (const row of rows) {
|
|
518
|
+
if (row.category != null) {
|
|
519
|
+
if (!isShellCall({ category: row.category, detailType: row.detailType })) continue;
|
|
520
|
+
} else if (row.detailType !== "shell") {
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
if (!row.command) continue;
|
|
524
|
+
const head = shellCommandHead(row.command);
|
|
525
|
+
const cur = map.get(head) ?? { count: 0, failures: 0 };
|
|
526
|
+
cur.count += 1;
|
|
527
|
+
if (row.status === "failed") cur.failures += 1;
|
|
528
|
+
map.set(head, cur);
|
|
529
|
+
}
|
|
530
|
+
return [...map.entries()]
|
|
531
|
+
.map(([command, v]) => ({ command, count: v.count, failures: v.failures }))
|
|
532
|
+
.sort((a, b) => b.count - a.count || a.command.localeCompare(b.command))
|
|
533
|
+
.slice(0, limit);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
export function aggregateMcpByTool(
|
|
537
|
+
rows: ReadonlyArray<{
|
|
538
|
+
category: string;
|
|
539
|
+
mcpServer: string | null;
|
|
540
|
+
mcpTool: string | null;
|
|
541
|
+
status: string | null;
|
|
542
|
+
ts?: string | null;
|
|
543
|
+
ingestedAt?: string;
|
|
544
|
+
}>,
|
|
545
|
+
): McpByToolItem[] {
|
|
546
|
+
const map = new Map<string, McpByToolItem>();
|
|
547
|
+
for (const row of rows) {
|
|
548
|
+
if (row.category !== "mcp") continue;
|
|
549
|
+
const server = row.mcpServer ?? "(unknown)";
|
|
550
|
+
const tool = row.mcpTool ?? "(unknown)";
|
|
551
|
+
const key = `${server}\0${tool}`;
|
|
552
|
+
const cur = map.get(key) ?? {
|
|
553
|
+
server,
|
|
554
|
+
tool,
|
|
555
|
+
count: 0,
|
|
556
|
+
failures: 0,
|
|
557
|
+
lastUsedAt: null,
|
|
558
|
+
};
|
|
559
|
+
cur.count += 1;
|
|
560
|
+
if (row.status === "failed") cur.failures += 1;
|
|
561
|
+
const at = row.ts ?? row.ingestedAt ?? null;
|
|
562
|
+
if (at && (!cur.lastUsedAt || at > cur.lastUsedAt)) cur.lastUsedAt = at;
|
|
563
|
+
map.set(key, cur);
|
|
564
|
+
}
|
|
565
|
+
return [...map.values()].sort(
|
|
566
|
+
(a, b) =>
|
|
567
|
+
b.count - a.count || a.server.localeCompare(b.server) || a.tool.localeCompare(b.tool),
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/** Format pill label: most recently used skill or MCP; no ×n; `Name + N` when entity count > 1. */
|
|
572
|
+
export function formatUsagePillLabel(
|
|
573
|
+
_summary: { shellCalls?: number } | undefined,
|
|
574
|
+
skills: ReadonlyArray<{
|
|
575
|
+
skillName: string;
|
|
576
|
+
total: number;
|
|
577
|
+
lastUsedAt?: string | null;
|
|
578
|
+
}> = [],
|
|
579
|
+
mcpTools: ReadonlyArray<{
|
|
580
|
+
server: string;
|
|
581
|
+
tool: string;
|
|
582
|
+
count: number;
|
|
583
|
+
lastUsedAt?: string | null;
|
|
584
|
+
}> = [],
|
|
585
|
+
): string {
|
|
586
|
+
type Candidate = {
|
|
587
|
+
kind: "skill" | "mcp";
|
|
588
|
+
key: string;
|
|
589
|
+
label: string;
|
|
590
|
+
lastUsedAt: string;
|
|
591
|
+
};
|
|
592
|
+
const candidates: Candidate[] = [];
|
|
593
|
+
for (const skill of skills) {
|
|
594
|
+
if (skill.total <= 0) continue;
|
|
595
|
+
candidates.push({
|
|
596
|
+
kind: "skill",
|
|
597
|
+
key: skill.skillName,
|
|
598
|
+
label: formatDisplayName(skill.skillName),
|
|
599
|
+
lastUsedAt: skill.lastUsedAt ?? "",
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
for (const mcp of mcpTools) {
|
|
603
|
+
if (mcp.count <= 0) continue;
|
|
604
|
+
const key = `${mcp.server}.${mcp.tool}`;
|
|
605
|
+
candidates.push({
|
|
606
|
+
kind: "mcp",
|
|
607
|
+
key,
|
|
608
|
+
label: formatDisplayName(key),
|
|
609
|
+
lastUsedAt: mcp.lastUsedAt ?? "",
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
if (candidates.length === 0) return "";
|
|
613
|
+
candidates.sort((a, b) => {
|
|
614
|
+
if (a.lastUsedAt !== b.lastUsedAt) return b.lastUsedAt.localeCompare(a.lastUsedAt);
|
|
615
|
+
if (a.kind !== b.kind) return a.kind === "skill" ? -1 : 1;
|
|
616
|
+
return a.key.localeCompare(b.key);
|
|
617
|
+
});
|
|
618
|
+
const top = candidates[0]!;
|
|
619
|
+
const extra = candidates.length - 1;
|
|
620
|
+
return extra > 0 ? `${top.label} + ${extra}` : top.label;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
export const ActivityDaySchema = z.object({
|
|
624
|
+
/** Local calendar day `YYYY-MM-DD`. */
|
|
625
|
+
date: z.string(),
|
|
626
|
+
skills: z.number().int().nonnegative(),
|
|
627
|
+
mcp: z.number().int().nonnegative(),
|
|
628
|
+
/** Distinct agents created that local day (005 registry). */
|
|
629
|
+
agents: z.number().int().nonnegative(),
|
|
630
|
+
messages: z.number().int().nonnegative(),
|
|
631
|
+
total: z.number().int().nonnegative(),
|
|
632
|
+
});
|
|
633
|
+
export type ActivityDay = z.infer<typeof ActivityDaySchema>;
|
|
634
|
+
|
|
635
|
+
export const usageActivityByDayRpc = defineRpc({
|
|
636
|
+
name: "usage.activity-by-day",
|
|
637
|
+
input: z.object({
|
|
638
|
+
from: z.string().optional(),
|
|
639
|
+
to: z.string().optional(),
|
|
640
|
+
/** Normalized provider id; omit / empty = all providers. */
|
|
641
|
+
provider: z.string().optional(),
|
|
642
|
+
workspaceId: z.string().optional(),
|
|
643
|
+
}),
|
|
644
|
+
output: z.object({
|
|
645
|
+
days: z.array(ActivityDaySchema),
|
|
646
|
+
}),
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
function pad2(value: number): string {
|
|
650
|
+
return String(value).padStart(2, "0");
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
/** Local calendar day key for an ISO timestamp. */
|
|
654
|
+
export function localDayKey(iso: string): string | null {
|
|
655
|
+
const date = new Date(iso);
|
|
656
|
+
if (Number.isNaN(date.getTime())) return null;
|
|
657
|
+
return `${date.getFullYear()}-${pad2(date.getMonth() + 1)}-${pad2(date.getDate())}`;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* Bucket skill / MCP calls by local day; agents = creations that day.
|
|
662
|
+
* Skill counts exclude `confidence=low` (same as UI totals).
|
|
663
|
+
* `total` = skills + mcp + agents + messages (heatmap intensity).
|
|
664
|
+
*/
|
|
665
|
+
export function aggregateActivityByDay(
|
|
666
|
+
rows: ReadonlyArray<{
|
|
667
|
+
agentId: string;
|
|
668
|
+
provider: string;
|
|
669
|
+
category: string;
|
|
670
|
+
confidence: string | null;
|
|
671
|
+
ts: string | null;
|
|
672
|
+
ingestedAt: string;
|
|
673
|
+
}>,
|
|
674
|
+
opts: {
|
|
675
|
+
provider?: string;
|
|
676
|
+
/** Created agents; `agents` on each day counts these by createdAt local day. */
|
|
677
|
+
agents?: ReadonlyArray<{ agentId: string; provider: string; createdAt: string }>;
|
|
678
|
+
messages?: ReadonlyArray<{ provider: string; ts: string | null; ingestedAt: string }>;
|
|
679
|
+
} = {},
|
|
680
|
+
): ActivityDay[] {
|
|
681
|
+
const map = new Map<string, { skills: number; mcp: number; agents: number; messages: number }>();
|
|
682
|
+
const providerFilter = opts.provider?.trim() ? normalizeProvider(opts.provider) : undefined;
|
|
683
|
+
|
|
684
|
+
for (const row of rows) {
|
|
685
|
+
if (providerFilter && normalizeProvider(row.provider) !== providerFilter) continue;
|
|
686
|
+
if (row.category !== "skill" && row.category !== "mcp") continue;
|
|
687
|
+
if (row.category === "skill" && row.confidence === "low") continue;
|
|
688
|
+
|
|
689
|
+
const key = localDayKey(row.ts ?? row.ingestedAt);
|
|
690
|
+
if (!key) continue;
|
|
691
|
+
const bucket = map.get(key) ?? { skills: 0, mcp: 0, agents: 0, messages: 0 };
|
|
692
|
+
if (row.category === "skill") bucket.skills += 1;
|
|
693
|
+
else bucket.mcp += 1;
|
|
694
|
+
map.set(key, bucket);
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
for (const agent of opts.agents ?? []) {
|
|
698
|
+
if (providerFilter && normalizeProvider(agent.provider) !== providerFilter) continue;
|
|
699
|
+
const key = localDayKey(agent.createdAt);
|
|
700
|
+
if (!key) continue;
|
|
701
|
+
const bucket = map.get(key) ?? { skills: 0, mcp: 0, agents: 0, messages: 0 };
|
|
702
|
+
bucket.agents += 1;
|
|
703
|
+
map.set(key, bucket);
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
for (const message of opts.messages ?? []) {
|
|
707
|
+
if (providerFilter && normalizeProvider(message.provider) !== providerFilter) continue;
|
|
708
|
+
const key = localDayKey(message.ts ?? message.ingestedAt);
|
|
709
|
+
if (!key) continue;
|
|
710
|
+
const bucket = map.get(key) ?? { skills: 0, mcp: 0, agents: 0, messages: 0 };
|
|
711
|
+
bucket.messages += 1;
|
|
712
|
+
map.set(key, bucket);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
return [...map.entries()]
|
|
716
|
+
.map(([date, counts]) => ({
|
|
717
|
+
date,
|
|
718
|
+
skills: counts.skills,
|
|
719
|
+
mcp: counts.mcp,
|
|
720
|
+
agents: counts.agents,
|
|
721
|
+
messages: counts.messages,
|
|
722
|
+
total: counts.skills + counts.mcp + counts.agents + counts.messages,
|
|
723
|
+
}))
|
|
724
|
+
.sort((a, b) => a.date.localeCompare(b.date));
|
|
725
|
+
}
|
|
726
|
+
|