@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
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export const CategorySchema = z.enum(["skill", "mcp", "regular"]);
|
|
4
|
+
export type Category = z.infer<typeof CategorySchema>;
|
|
5
|
+
|
|
6
|
+
export const ConfidenceSchema = z.enum(["exact", "inferred", "low"]);
|
|
7
|
+
export type Confidence = z.infer<typeof ConfidenceSchema>;
|
|
8
|
+
|
|
9
|
+
export const ClassifyResultSchema = z.object({
|
|
10
|
+
category: CategorySchema,
|
|
11
|
+
confidence: ConfidenceSchema.nullable(),
|
|
12
|
+
detailType: z.string().nullable(),
|
|
13
|
+
skillName: z.string().nullable(),
|
|
14
|
+
mcpServer: z.string().nullable(),
|
|
15
|
+
mcpTool: z.string().nullable(),
|
|
16
|
+
command: z.string().nullable(),
|
|
17
|
+
filePath: z.string().nullable(),
|
|
18
|
+
});
|
|
19
|
+
export type ClassifyResult = z.infer<typeof ClassifyResultSchema>;
|
|
20
|
+
|
|
21
|
+
/** Well-known agent config dir names hosting `<dir>/skills/<name>/SKILL.md`. */
|
|
22
|
+
export const AGENT_SKILL_CONFIG_DIRS: readonly string[] = [
|
|
23
|
+
"claude",
|
|
24
|
+
"codex",
|
|
25
|
+
"cursor",
|
|
26
|
+
"agents",
|
|
27
|
+
"opencode",
|
|
28
|
+
"pi",
|
|
29
|
+
"codebuddy",
|
|
30
|
+
"openclaw",
|
|
31
|
+
"tcodex",
|
|
32
|
+
"workbuddy",
|
|
33
|
+
"gemini",
|
|
34
|
+
"windsurf",
|
|
35
|
+
"continue",
|
|
36
|
+
"copilot",
|
|
37
|
+
"amp",
|
|
38
|
+
"goose",
|
|
39
|
+
"aider",
|
|
40
|
+
"factory",
|
|
41
|
+
"kilocode",
|
|
42
|
+
"roo",
|
|
43
|
+
"cline",
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
/** Extra roots that do not follow `~/.{name}/skills` / `.{name}/skills`. */
|
|
47
|
+
const EXTRA_SKILL_ROOTS: readonly string[] = [
|
|
48
|
+
"~/.config/opencode/skills",
|
|
49
|
+
"~/.pi/agent/skills",
|
|
50
|
+
"~/.codex/vendor_imports/skills",
|
|
51
|
+
"~/.paseo/plugins/*/checkout/*/skills",
|
|
52
|
+
"~/.paseo/plugins/*/skills",
|
|
53
|
+
".pi/agent/skills",
|
|
54
|
+
".github/skills",
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
function buildDefaultSkillRoots(): string[] {
|
|
58
|
+
const roots: string[] = [];
|
|
59
|
+
for (const dir of AGENT_SKILL_CONFIG_DIRS) {
|
|
60
|
+
roots.push(`~/.${dir}/skills`);
|
|
61
|
+
roots.push(`.${dir}/skills`);
|
|
62
|
+
}
|
|
63
|
+
roots.push(...EXTRA_SKILL_ROOTS);
|
|
64
|
+
return roots;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Default skill-root templates (plan §3.2). `~` expanded via homeDir; relative paths match by suffix. */
|
|
68
|
+
export const DEFAULT_SKILL_ROOTS: readonly string[] = buildDefaultSkillRoots();
|
|
69
|
+
|
|
70
|
+
export interface ToolCallDetailInput {
|
|
71
|
+
type?: unknown;
|
|
72
|
+
[key: string]: unknown;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface ToolCallInput {
|
|
76
|
+
name: string;
|
|
77
|
+
detail?: ToolCallDetailInput | null;
|
|
78
|
+
metadata?: Record<string, unknown> | null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface ClassifyContext {
|
|
82
|
+
/** Raw provider id, e.g. "claude", "claude/opus", "opencode", "codex". */
|
|
83
|
+
provider: string;
|
|
84
|
+
skillRoots?: readonly string[];
|
|
85
|
+
homeDir?: string;
|
|
86
|
+
/** OpenCode MCP server names for `<server>_<tool>` matching. */
|
|
87
|
+
mcpServers?: readonly string[];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const ACP_PROVIDERS = new Set(["cursor", "codebuddy", "copilot"]);
|
|
91
|
+
const DOTTED_MCP_PROVIDERS = new Set(["codex", "pi"]);
|
|
92
|
+
const SHELL_SKILL_READ_PATTERN = /\b(cat|sed|head|bat|grep|rg)\b[\s\S]*SKILL\.md/i;
|
|
93
|
+
const SKILL_FILE_NAME = "SKILL.md";
|
|
94
|
+
|
|
95
|
+
function result(partial: Partial<ClassifyResult> & { category: Category }): ClassifyResult {
|
|
96
|
+
return {
|
|
97
|
+
confidence: null,
|
|
98
|
+
detailType: null,
|
|
99
|
+
skillName: null,
|
|
100
|
+
mcpServer: null,
|
|
101
|
+
mcpTool: null,
|
|
102
|
+
command: null,
|
|
103
|
+
filePath: null,
|
|
104
|
+
...partial,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function asString(value: unknown): string | null {
|
|
109
|
+
return typeof value === "string" && value.length > 0 ? value : null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function field(source: { [key: string]: unknown } | null | undefined, key: string): unknown {
|
|
113
|
+
return source ? source[key] : undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function normalizeProvider(provider: string): string {
|
|
117
|
+
const head = (provider.split("/")[0] ?? provider).toLowerCase();
|
|
118
|
+
if (head === "claude" || head.startsWith("claude")) return "claude";
|
|
119
|
+
if (head === "opencode") return "opencode";
|
|
120
|
+
if (head === "codex") return "codex";
|
|
121
|
+
if (head === "pi" || head === "omp") return "pi";
|
|
122
|
+
if (head === "cursor") return "cursor";
|
|
123
|
+
if (head === "codebuddy" || head === "codebuddy-code") return "codebuddy";
|
|
124
|
+
return head;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function expandHome(path: string, homeDir?: string): string {
|
|
128
|
+
if (homeDir && path.startsWith("~/")) {
|
|
129
|
+
return `${homeDir.replace(/\/+$/, "")}/${path.slice(2)}`;
|
|
130
|
+
}
|
|
131
|
+
return path;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function escapeRegExp(value: string): string {
|
|
135
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** If filePath is under a skill root as …/<name>/SKILL.md (nested dirs OK), return skill name. */
|
|
139
|
+
export function matchSkillFile(
|
|
140
|
+
filePath: string,
|
|
141
|
+
roots: readonly string[] = DEFAULT_SKILL_ROOTS,
|
|
142
|
+
homeDir?: string,
|
|
143
|
+
): string | null {
|
|
144
|
+
const normalized = expandHome(filePath, homeDir).replace(/\\/g, "/");
|
|
145
|
+
if (!/\/SKILL\.md$/i.test(normalized)) return null;
|
|
146
|
+
|
|
147
|
+
for (const root of roots) {
|
|
148
|
+
const resolved = expandHome(root, homeDir).replace(/\\/g, "/");
|
|
149
|
+
if (resolved.includes("*")) {
|
|
150
|
+
const pattern = new RegExp(
|
|
151
|
+
`^.*${resolved.split("*").map(escapeRegExp).join("[^/]+")}(?:/.*)?/([^/]+)/SKILL\\.md$`,
|
|
152
|
+
"i",
|
|
153
|
+
);
|
|
154
|
+
const match = pattern.exec(normalized);
|
|
155
|
+
if (match?.[1]) return match[1];
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const prefix = resolved.endsWith("/") ? resolved : `${resolved}/`;
|
|
159
|
+
let remainder: string | null = null;
|
|
160
|
+
if (prefix.startsWith("/")) {
|
|
161
|
+
if (normalized.startsWith(prefix)) remainder = normalized.slice(prefix.length);
|
|
162
|
+
else if (normalized.toLowerCase().startsWith(prefix.toLowerCase())) {
|
|
163
|
+
remainder = normalized.slice(prefix.length);
|
|
164
|
+
}
|
|
165
|
+
} else {
|
|
166
|
+
const index = normalized.indexOf(`/${prefix}`);
|
|
167
|
+
if (index >= 0) remainder = normalized.slice(index + 1 + prefix.length);
|
|
168
|
+
else if (normalized.startsWith(prefix)) remainder = normalized.slice(prefix.length);
|
|
169
|
+
}
|
|
170
|
+
if (!remainder) continue;
|
|
171
|
+
// Allow category nesting: info/zhihu/SKILL.md → zhihu
|
|
172
|
+
const match = /(?:^|\/)([^/]+)\/SKILL\.md$/i.exec(remainder);
|
|
173
|
+
if (match?.[1]) return match[1];
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function parentSkillNameFromPath(token: string): string | null {
|
|
179
|
+
const match = /(?:^|\/)([^/]+)\/SKILL\.md$/i.exec(token.replace(/\\/g, "/"));
|
|
180
|
+
return match?.[1] ?? null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function matchSkillInCommand(
|
|
184
|
+
command: string,
|
|
185
|
+
roots: readonly string[],
|
|
186
|
+
homeDir?: string,
|
|
187
|
+
): { skillName: string; filePath: string } | null {
|
|
188
|
+
if (!SHELL_SKILL_READ_PATTERN.test(command)) return null;
|
|
189
|
+
for (const token of command.split(/[\s|;&()<>'"]+/)) {
|
|
190
|
+
if (!/SKILL\.md$/i.test(token)) continue;
|
|
191
|
+
const skillName =
|
|
192
|
+
matchSkillFile(token, roots, homeDir) ?? parentSkillNameFromPath(token);
|
|
193
|
+
if (skillName) return { skillName, filePath: token };
|
|
194
|
+
}
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function classifySkillExact(item: ToolCallInput, detailType: string): ClassifyResult | null {
|
|
199
|
+
if (item.name !== "Skill" && item.name !== "skill") return null;
|
|
200
|
+
return result({
|
|
201
|
+
category: "skill",
|
|
202
|
+
confidence: "exact",
|
|
203
|
+
detailType,
|
|
204
|
+
skillName: asString(field(item.detail, "label")),
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function classifyMcp(
|
|
209
|
+
item: ToolCallInput,
|
|
210
|
+
detailType: string,
|
|
211
|
+
provider: string,
|
|
212
|
+
context: ClassifyContext,
|
|
213
|
+
): ClassifyResult | null {
|
|
214
|
+
const name = item.name;
|
|
215
|
+
if (name.startsWith("mcp__")) {
|
|
216
|
+
const rest = name.slice("mcp__".length);
|
|
217
|
+
const separator = rest.indexOf("__");
|
|
218
|
+
if (separator > 0 && separator + 2 < rest.length) {
|
|
219
|
+
return result({
|
|
220
|
+
category: "mcp",
|
|
221
|
+
confidence: "exact",
|
|
222
|
+
detailType,
|
|
223
|
+
mcpServer: rest.slice(0, separator),
|
|
224
|
+
mcpTool: rest.slice(separator + 2),
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (DOTTED_MCP_PROVIDERS.has(provider)) {
|
|
230
|
+
const match = /^([A-Za-z0-9_-]+)\.([A-Za-z0-9_.-]+)$/.exec(name);
|
|
231
|
+
if (match) {
|
|
232
|
+
return result({
|
|
233
|
+
category: "mcp",
|
|
234
|
+
confidence: "exact",
|
|
235
|
+
detailType,
|
|
236
|
+
mcpServer: match[1],
|
|
237
|
+
mcpTool: match[2],
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const underscoreHit = matchUnderscoreMcp(name, context.mcpServers);
|
|
243
|
+
if (underscoreHit && provider === "opencode") {
|
|
244
|
+
return result({
|
|
245
|
+
category: "mcp",
|
|
246
|
+
confidence: "exact",
|
|
247
|
+
detailType,
|
|
248
|
+
mcpServer: underscoreHit.server,
|
|
249
|
+
mcpTool: underscoreHit.tool,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (ACP_PROVIDERS.has(provider)) {
|
|
254
|
+
// ACP stores kind in `name` (often "other" for MCP); real id is metadata.title / label.
|
|
255
|
+
const title =
|
|
256
|
+
asString(field(item.metadata, "title")) ?? asString(field(item.detail, "label"));
|
|
257
|
+
const candidates = [title, ACP_GENERIC_NAMES.has(name) ? null : name].filter(
|
|
258
|
+
(v): v is string => typeof v === "string" && v.length > 0,
|
|
259
|
+
);
|
|
260
|
+
for (const candidate of candidates) {
|
|
261
|
+
if (candidate.startsWith("mcp__")) {
|
|
262
|
+
const rest = candidate.slice("mcp__".length);
|
|
263
|
+
const separator = rest.indexOf("__");
|
|
264
|
+
if (separator > 0 && separator + 2 < rest.length) {
|
|
265
|
+
return result({
|
|
266
|
+
category: "mcp",
|
|
267
|
+
confidence: "inferred",
|
|
268
|
+
detailType,
|
|
269
|
+
mcpServer: rest.slice(0, separator),
|
|
270
|
+
mcpTool: rest.slice(separator + 2),
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
const dottedMatch = /^([A-Za-z0-9_-]+)\.([A-Za-z0-9_.-]+)$/.exec(candidate);
|
|
275
|
+
if (dottedMatch) {
|
|
276
|
+
return result({
|
|
277
|
+
category: "mcp",
|
|
278
|
+
confidence: "inferred",
|
|
279
|
+
detailType,
|
|
280
|
+
mcpServer: dottedMatch[1],
|
|
281
|
+
mcpTool: dottedMatch[2],
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
const prefixHit = matchUnderscoreMcp(candidate, context.mcpServers);
|
|
285
|
+
if (prefixHit) {
|
|
286
|
+
return result({
|
|
287
|
+
category: "mcp",
|
|
288
|
+
confidence: "inferred",
|
|
289
|
+
detailType,
|
|
290
|
+
mcpServer: prefixHit.server,
|
|
291
|
+
mcpTool: prefixHit.tool,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** ACP kinds that are not the real tool identity (see acp-agent mapToolSnapshotToTimeline). */
|
|
300
|
+
const ACP_GENERIC_NAMES = new Set([
|
|
301
|
+
"other",
|
|
302
|
+
"think",
|
|
303
|
+
"switch_mode",
|
|
304
|
+
"read",
|
|
305
|
+
"edit",
|
|
306
|
+
"delete",
|
|
307
|
+
"search",
|
|
308
|
+
"execute",
|
|
309
|
+
"fetch",
|
|
310
|
+
]);
|
|
311
|
+
|
|
312
|
+
function matchUnderscoreMcp(
|
|
313
|
+
name: string,
|
|
314
|
+
mcpServers: readonly string[] | undefined,
|
|
315
|
+
): { server: string; tool: string } | null {
|
|
316
|
+
const servers = [...(mcpServers ?? [])].sort((a, b) => b.length - a.length);
|
|
317
|
+
for (const server of servers) {
|
|
318
|
+
if (name.startsWith(`${server}_`) && name.length > server.length + 1) {
|
|
319
|
+
return { server, tool: name.slice(server.length + 1) };
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Classify a tool_call into skill / mcp / regular.
|
|
327
|
+
* Pure function — no Node or React imports.
|
|
328
|
+
*/
|
|
329
|
+
export function classifyToolCall(item: ToolCallInput, context: ClassifyContext): ClassifyResult {
|
|
330
|
+
const detail = item.detail ?? null;
|
|
331
|
+
const detailType = asString(field(detail, "type")) ?? "unknown";
|
|
332
|
+
const provider = normalizeProvider(context.provider);
|
|
333
|
+
const roots = context.skillRoots ?? DEFAULT_SKILL_ROOTS;
|
|
334
|
+
|
|
335
|
+
const skillExact = classifySkillExact(item, detailType);
|
|
336
|
+
if (skillExact) return skillExact;
|
|
337
|
+
|
|
338
|
+
const filePath = asString(field(detail, "filePath"));
|
|
339
|
+
if (detailType === "read" && filePath) {
|
|
340
|
+
const skillName = matchSkillFile(filePath, roots, context.homeDir);
|
|
341
|
+
if (skillName) {
|
|
342
|
+
return result({
|
|
343
|
+
category: "skill",
|
|
344
|
+
confidence: "inferred",
|
|
345
|
+
detailType,
|
|
346
|
+
skillName,
|
|
347
|
+
filePath,
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (detailType === "shell") {
|
|
353
|
+
const command = asString(field(detail, "command"));
|
|
354
|
+
if (command) {
|
|
355
|
+
const hit = matchSkillInCommand(command, roots, context.homeDir);
|
|
356
|
+
if (hit) {
|
|
357
|
+
return result({
|
|
358
|
+
category: "skill",
|
|
359
|
+
confidence: "low",
|
|
360
|
+
detailType,
|
|
361
|
+
skillName: hit.skillName,
|
|
362
|
+
filePath: hit.filePath,
|
|
363
|
+
command,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
return result({ category: "regular", detailType, command });
|
|
367
|
+
}
|
|
368
|
+
return result({ category: "regular", detailType });
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const mcp = classifyMcp(item, detailType, provider, context);
|
|
372
|
+
if (mcp) return mcp;
|
|
373
|
+
|
|
374
|
+
return result({ category: "regular", detailType, filePath });
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** Alias used by ingest / tests. */
|
|
378
|
+
export const classify = classifyToolCall;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { formatDayTime, formatDisplayName } from "./format.ts";
|
|
4
|
+
|
|
5
|
+
const NOW = new Date(2026, 8, 18, 15, 30);
|
|
6
|
+
|
|
7
|
+
describe("formatDayTime", () => {
|
|
8
|
+
it("shows only HH:mm for the same local day", () => {
|
|
9
|
+
assert.equal(formatDayTime(new Date(2026, 8, 18, 9, 5).toISOString(), NOW), "09:05");
|
|
10
|
+
assert.equal(formatDayTime(new Date(2026, 8, 18, 23, 59).toISOString(), NOW), "23:59");
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("adds MM-DD for another day in the same year", () => {
|
|
14
|
+
assert.equal(formatDayTime(new Date(2026, 8, 17, 14, 2).toISOString(), NOW), "09-17 14:02");
|
|
15
|
+
assert.equal(formatDayTime(new Date(2026, 0, 3, 0, 7).toISOString(), NOW), "01-03 00:07");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("adds the year for a different year", () => {
|
|
19
|
+
assert.equal(
|
|
20
|
+
formatDayTime(new Date(2025, 11, 31, 23, 59).toISOString(), NOW),
|
|
21
|
+
"2025-12-31 23:59",
|
|
22
|
+
);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("returns the placeholder for empty and keeps invalid input", () => {
|
|
26
|
+
assert.equal(formatDayTime(null, NOW), "—");
|
|
27
|
+
assert.equal(formatDayTime(undefined, NOW), "—");
|
|
28
|
+
assert.equal(formatDayTime("not-a-date", NOW), "not-a-date");
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe("formatDisplayName", () => {
|
|
33
|
+
it("replaces - and _ with spaces and title-cases every word", () => {
|
|
34
|
+
assert.equal(formatDisplayName("git-commit-push"), "Git Commit Push");
|
|
35
|
+
assert.equal(formatDisplayName("tapd_search"), "Tapd Search");
|
|
36
|
+
assert.equal(formatDisplayName("knot.tapd_search"), "Knot.Tapd Search");
|
|
37
|
+
assert.equal(formatDisplayName("paseo"), "Paseo");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("collapses repeated separators and trims", () => {
|
|
41
|
+
assert.equal(formatDisplayName("__foo--bar_"), "Foo Bar");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("keeps an all-separator or empty value unchanged", () => {
|
|
45
|
+
assert.equal(formatDisplayName(""), "");
|
|
46
|
+
assert.equal(formatDisplayName("--"), "--");
|
|
47
|
+
});
|
|
48
|
+
});
|
package/shared/format.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** Format stored ISO timestamps for UI / export in the local timezone + locale. */
|
|
2
|
+
export function formatLocalDateTime(iso: string | null | undefined): string {
|
|
3
|
+
if (!iso) return "—";
|
|
4
|
+
const date = new Date(iso);
|
|
5
|
+
if (Number.isNaN(date.getTime())) return iso;
|
|
6
|
+
return date.toLocaleString(undefined, {
|
|
7
|
+
dateStyle: "medium",
|
|
8
|
+
timeStyle: "short",
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function pad2(value: number): string {
|
|
13
|
+
return String(value).padStart(2, "0");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Display-name prettifier: `-` / `_` → spaces, then Title Case each word. */
|
|
17
|
+
export function formatDisplayName(value: string): string {
|
|
18
|
+
const cleaned = value.replace(/[-_]+/g, " ").replace(/\s+/g, " ").trim();
|
|
19
|
+
if (!cleaned) return value;
|
|
20
|
+
return cleaned.replace(/\b[a-z]/g, (char) => char.toUpperCase());
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Compact 24-hour timestamp for list rows:
|
|
25
|
+
* today → `HH:mm`; same year → `MM-DD HH:mm`; otherwise → `YYYY-MM-DD HH:mm`.
|
|
26
|
+
*/
|
|
27
|
+
export function formatDayTime(
|
|
28
|
+
iso: string | null | undefined,
|
|
29
|
+
now: Date = new Date(),
|
|
30
|
+
): string {
|
|
31
|
+
if (!iso) return "—";
|
|
32
|
+
const date = new Date(iso);
|
|
33
|
+
if (Number.isNaN(date.getTime())) return iso;
|
|
34
|
+
const time = `${pad2(date.getHours())}:${pad2(date.getMinutes())}`;
|
|
35
|
+
if (
|
|
36
|
+
date.getFullYear() === now.getFullYear() &&
|
|
37
|
+
date.getMonth() === now.getMonth() &&
|
|
38
|
+
date.getDate() === now.getDate()
|
|
39
|
+
) {
|
|
40
|
+
return time;
|
|
41
|
+
}
|
|
42
|
+
const monthDay = `${pad2(date.getMonth() + 1)}-${pad2(date.getDate())}`;
|
|
43
|
+
if (date.getFullYear() === now.getFullYear()) return `${monthDay} ${time}`;
|
|
44
|
+
return `${date.getFullYear()}-${monthDay} ${time}`;
|
|
45
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { ACTIVITY_LIST_LIMIT, buildActivityInsights } from "./insights.ts";
|
|
4
|
+
import type { ProviderUsageItem } from "./usage.ts";
|
|
5
|
+
|
|
6
|
+
function provider(partial: Partial<ProviderUsageItem> & Pick<ProviderUsageItem, "provider" | "label">): ProviderUsageItem {
|
|
7
|
+
return {
|
|
8
|
+
shellCalls: 0,
|
|
9
|
+
shellFailures: 0,
|
|
10
|
+
fileReads: 0,
|
|
11
|
+
fileWrites: 0,
|
|
12
|
+
skillCalls: { exact: 0, inferred: 0, low: 0 },
|
|
13
|
+
skills: [],
|
|
14
|
+
mcpCalls: 0,
|
|
15
|
+
mcpFailures: 0,
|
|
16
|
+
mcpTools: [],
|
|
17
|
+
shellTop: [],
|
|
18
|
+
models: [],
|
|
19
|
+
agentCount: 0,
|
|
20
|
+
workspaceCount: 0,
|
|
21
|
+
messageCount: 0,
|
|
22
|
+
callCount: 0,
|
|
23
|
+
...partial,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe("buildActivityInsights", () => {
|
|
28
|
+
it("returns habit-first 8 rows with Top model on All", () => {
|
|
29
|
+
// 2026-03-07 Sat, 2026-03-09 Mon — Mon has more messages → Peak weekday Mon
|
|
30
|
+
const days = [
|
|
31
|
+
{ date: "2026-03-07", skills: 2, mcp: 0, agents: 1, messages: 4, total: 2 },
|
|
32
|
+
{ date: "2026-03-09", skills: 0, mcp: 0, agents: 0, messages: 10, total: 0 },
|
|
33
|
+
];
|
|
34
|
+
const rows = buildActivityInsights({
|
|
35
|
+
days,
|
|
36
|
+
summary: {
|
|
37
|
+
skills: 2,
|
|
38
|
+
mcp: 3,
|
|
39
|
+
agents: 2,
|
|
40
|
+
messages: 14,
|
|
41
|
+
shell: 10,
|
|
42
|
+
fileReads: 3,
|
|
43
|
+
fileWrites: 2,
|
|
44
|
+
longestStreak: 6,
|
|
45
|
+
},
|
|
46
|
+
providers: [
|
|
47
|
+
provider({
|
|
48
|
+
provider: "codex",
|
|
49
|
+
label: "Codex",
|
|
50
|
+
messageCount: 10,
|
|
51
|
+
models: [{ model: "gpt-5.4", count: 10 }],
|
|
52
|
+
}),
|
|
53
|
+
provider({
|
|
54
|
+
provider: "claude",
|
|
55
|
+
label: "Claude",
|
|
56
|
+
messageCount: 4,
|
|
57
|
+
models: [{ model: "opus", count: 4 }],
|
|
58
|
+
}),
|
|
59
|
+
],
|
|
60
|
+
providerFilter: "all",
|
|
61
|
+
locale: "en",
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
assert.equal(rows.length, ACTIVITY_LIST_LIMIT);
|
|
65
|
+
assert.deepEqual(
|
|
66
|
+
rows.map((r) => r.label),
|
|
67
|
+
[
|
|
68
|
+
"Active days",
|
|
69
|
+
"Longest streak",
|
|
70
|
+
"Busiest day",
|
|
71
|
+
"Top model",
|
|
72
|
+
"Messages per agent",
|
|
73
|
+
"Tools per message",
|
|
74
|
+
"Coding vs chat",
|
|
75
|
+
"Peak weekday",
|
|
76
|
+
],
|
|
77
|
+
);
|
|
78
|
+
assert.equal(rows[0]?.value, "2");
|
|
79
|
+
assert.equal(rows[1]?.value, "6 days");
|
|
80
|
+
assert.equal(rows[2]?.value, "Mar 9 · 10 messages");
|
|
81
|
+
assert.equal(rows[3]?.value, "Gpt 5.4 · 71%");
|
|
82
|
+
assert.equal(rows[4]?.value, "7");
|
|
83
|
+
assert.equal(rows[5]?.value, "0.4");
|
|
84
|
+
// coding=15, messages=14 → 15/29 ≈ 52%
|
|
85
|
+
assert.equal(rows[6]?.value, "52% coding");
|
|
86
|
+
assert.equal(rows[7]?.value, "Mon");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("shows Top model for a provider filter from that provider's models", () => {
|
|
90
|
+
const rows = buildActivityInsights({
|
|
91
|
+
days: [{ date: "2026-03-07", skills: 0, mcp: 0, agents: 0, messages: 10, total: 0 }],
|
|
92
|
+
summary: { skills: 0, mcp: 0, agents: 0, messages: 10, longestStreak: 1 },
|
|
93
|
+
providers: [
|
|
94
|
+
provider({
|
|
95
|
+
provider: "codex",
|
|
96
|
+
label: "Codex",
|
|
97
|
+
messageCount: 10,
|
|
98
|
+
models: [{ model: "gpt-5.4", count: 10 }],
|
|
99
|
+
}),
|
|
100
|
+
],
|
|
101
|
+
providerFilter: "codex",
|
|
102
|
+
});
|
|
103
|
+
assert.equal(rows.length, ACTIVITY_LIST_LIMIT);
|
|
104
|
+
assert.equal(rows.find((r) => r.label === "Top model")?.value, "Gpt 5.4 · 100%");
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("uses dashes for zero denominators and falls back busiest day", () => {
|
|
108
|
+
const rows = buildActivityInsights({
|
|
109
|
+
days: [{ date: "2026-03-08", skills: 3, mcp: 1, agents: 2, messages: 0, total: 4 }],
|
|
110
|
+
summary: { skills: 3, mcp: 1, agents: 0, messages: 0, shell: 0, fileReads: 0, fileWrites: 0 },
|
|
111
|
+
providers: [],
|
|
112
|
+
providerFilter: "all",
|
|
113
|
+
locale: "en",
|
|
114
|
+
});
|
|
115
|
+
assert.equal(rows.find((r) => r.label === "Busiest day")?.value, "Mar 8 · 4 calls");
|
|
116
|
+
assert.equal(rows.find((r) => r.label === "Messages per agent")?.value, "—");
|
|
117
|
+
assert.equal(rows.find((r) => r.label === "Tools per message")?.value, "—");
|
|
118
|
+
assert.equal(rows.find((r) => r.label === "Coding vs chat")?.value, "—");
|
|
119
|
+
assert.equal(rows.find((r) => r.label === "Top model")?.value, "—");
|
|
120
|
+
assert.equal(rows.find((r) => r.label === "Peak weekday")?.value, "Sun");
|
|
121
|
+
});
|
|
122
|
+
});
|