@koinzhang/paseo-plugin-activity 0.6.0 → 0.7.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/CHANGELOG.md +35 -1
- package/README.md +14 -7
- package/client/activity-heatmap.tsx +115 -74
- package/client/agent-creations.tsx +97 -88
- package/client/attention-pill.tsx +16 -5
- package/client/attention-popover.tsx +19 -23
- package/client/design-tokens.test.ts +39 -0
- package/client/design-tokens.ts +168 -0
- package/client/formatted-time.tsx +4 -4
- package/client/global-surface.tsx +236 -268
- package/client/header-button.ts +136 -0
- package/client/hourly-activity-timeline.tsx +136 -103
- package/client/panel-visibility.test.ts +25 -0
- package/client/panel-visibility.ts +45 -0
- package/client/panel.tsx +83 -108
- package/client/provider-dropdown.tsx +131 -0
- package/client/provider-filter.test.ts +17 -13
- package/client/provider-filter.ts +5 -7
- package/client/rank-color.ts +3 -3
- package/client/ranking-bars.tsx +291 -0
- package/client/ui.tsx +339 -0
- package/client/usage-popover.tsx +62 -134
- package/client/usage-stats.tsx +29 -11
- package/client/use-agent-turn-end.ts +2 -1
- package/client/use-app-language.ts +17 -0
- package/client/web.ts +73 -18
- package/client/workspace/agent-row.tsx +18 -25
- package/client/workspace/agents-section.tsx +23 -20
- package/client/workspace/constants.ts +6 -6
- package/client/workspace/display-menu.tsx +5 -4
- package/client/workspace/filters.test.ts +1 -1
- package/client/workspace/filters.ts +4 -2
- package/client/workspace/panel.tsx +89 -103
- package/client/workspace/rank-section.tsx +36 -62
- package/client/workspace/terminals-section.tsx +13 -9
- package/index.client.tsx +40 -20
- package/index.server.ts +53 -51
- package/package.json +2 -2
- package/paseo-plugin.json +2 -1
- package/server/agents.ts +20 -2
- package/server/background-sync.ts +34 -4
- package/server/handlers-query.test.ts +27 -1
- package/server/handlers.ts +75 -31
- package/server/ingest.test.ts +8 -0
- package/server/messages.test.ts +96 -1
- package/server/prompt-dedupe.ts +52 -0
- package/server/rpc-cache.test.ts +74 -0
- package/server/rpc-cache.ts +48 -0
- package/server/store.test.ts +122 -0
- package/server/store.ts +313 -19
- package/server/ttl-cache.ts +3 -0
- package/shared/activity.test.ts +36 -0
- package/shared/activity.ts +37 -0
- package/shared/classify.test.ts +1 -10
- package/shared/classify.ts +3 -10
- package/shared/format.ts +10 -7
- package/shared/i18n.test.ts +34 -0
- package/shared/i18n.ts +363 -0
- package/shared/insights.test.ts +120 -81
- package/shared/insights.ts +124 -63
- package/shared/usage.test.ts +121 -0
- package/shared/usage.ts +191 -2
package/shared/usage.test.ts
CHANGED
|
@@ -5,7 +5,10 @@ import {
|
|
|
5
5
|
aggregateActivityByDay,
|
|
6
6
|
aggregateAgentCreations,
|
|
7
7
|
aggregateAgents,
|
|
8
|
+
aggregateByProject,
|
|
8
9
|
aggregateByProvider,
|
|
10
|
+
OTHER_PROJECT_KEY,
|
|
11
|
+
resolveAgentProjects,
|
|
9
12
|
aggregateMcpByTool,
|
|
10
13
|
aggregateShellTop,
|
|
11
14
|
aggregateSkillsByName,
|
|
@@ -15,6 +18,8 @@ import {
|
|
|
15
18
|
isFileWrite,
|
|
16
19
|
isShellCall,
|
|
17
20
|
pickLongestAgentLifetime,
|
|
21
|
+
averageEngagedSessionMs,
|
|
22
|
+
countMultiTurnSessions,
|
|
18
23
|
shellCommandHead,
|
|
19
24
|
shellLooksMutating,
|
|
20
25
|
} from "./usage.ts";
|
|
@@ -1004,6 +1009,38 @@ describe("aggregateAgents (024)", () => {
|
|
|
1004
1009
|
});
|
|
1005
1010
|
});
|
|
1006
1011
|
|
|
1012
|
+
describe("countMultiTurnSessions (072)", () => {
|
|
1013
|
+
it("counts prompted and ≥2-prompt sessions among known agents", () => {
|
|
1014
|
+
const agents = [{ agentId: "a" }, { agentId: "b" }, { agentId: "empty" }];
|
|
1015
|
+
const messages = [{ agentId: "a" }, { agentId: "a" }, { agentId: "b" }, { agentId: "gone" }, { agentId: "gone" }];
|
|
1016
|
+
assert.deepEqual(countMultiTurnSessions(agents, messages), { promptedSessions: 2, multiTurnSessions: 1 });
|
|
1017
|
+
});
|
|
1018
|
+
});
|
|
1019
|
+
|
|
1020
|
+
describe("averageEngagedSessionMs (072)", () => {
|
|
1021
|
+
const min = 60_000;
|
|
1022
|
+
const at = (offsetMin: number) => new Date(Date.parse("2026-09-19T00:00:00.000Z") + offsetMin * min).toISOString();
|
|
1023
|
+
it("sums gaps between events, skips idle gaps, excludes sessions without events", () => {
|
|
1024
|
+
const agents = [
|
|
1025
|
+
{ agentId: "a", createdAt: at(0) },
|
|
1026
|
+
{ agentId: "b", createdAt: at(0) },
|
|
1027
|
+
{ agentId: "idle", createdAt: at(0) },
|
|
1028
|
+
];
|
|
1029
|
+
const events = [
|
|
1030
|
+
{ agentId: "a", ts: at(5), ingestedAt: at(99) },
|
|
1031
|
+
{ agentId: "a", ts: null, ingestedAt: at(20) },
|
|
1032
|
+
// 3-day gap: resumed later, counts only the 4 min after resuming.
|
|
1033
|
+
{ agentId: "a", ts: at(3 * 1440), ingestedAt: at(3 * 1440) },
|
|
1034
|
+
{ agentId: "a", ts: at(3 * 1440 + 4), ingestedAt: at(3 * 1440 + 4) },
|
|
1035
|
+
{ agentId: "b", ts: at(10), ingestedAt: at(10) },
|
|
1036
|
+
{ agentId: "unknown", ts: at(1), ingestedAt: at(1) },
|
|
1037
|
+
];
|
|
1038
|
+
// a: 5 + 15 + 4 = 24 min; b: 10 min → mean 17 min.
|
|
1039
|
+
assert.equal(averageEngagedSessionMs(agents, events), 17 * min);
|
|
1040
|
+
assert.equal(averageEngagedSessionMs(agents, []), null);
|
|
1041
|
+
});
|
|
1042
|
+
});
|
|
1043
|
+
|
|
1007
1044
|
describe("pickLongestAgentLifetime (049 / 051)", () => {
|
|
1008
1045
|
const agent = (
|
|
1009
1046
|
agentId: string,
|
|
@@ -1103,6 +1140,26 @@ describe("aggregateAgentCreations (051)", () => {
|
|
|
1103
1140
|
{ provider: "omp", label: "Oh My Pi", count: 2 },
|
|
1104
1141
|
{ provider: "pi", label: "Pi", count: 1 },
|
|
1105
1142
|
]);
|
|
1143
|
+
assert.deepEqual(aggregateAgentCreations([agent("omp", "2026-09-19T02:00:00.000Z"), agent("pi", "2026-09-19T01:00:00.000Z")], { provider: "omp" })[0]?.total, 1);
|
|
1144
|
+
});
|
|
1145
|
+
|
|
1146
|
+
it("keeps Oh My Pi (omp) and Pi as separate providers (071)", () => {
|
|
1147
|
+
const result = aggregateByProvider(
|
|
1148
|
+
[],
|
|
1149
|
+
[
|
|
1150
|
+
{ agentId: "a", provider: "pi" },
|
|
1151
|
+
{ agentId: "b", provider: "omp" },
|
|
1152
|
+
{ agentId: "c", provider: "omp" },
|
|
1153
|
+
],
|
|
1154
|
+
[{ agentId: "b", provider: "omp" }],
|
|
1155
|
+
);
|
|
1156
|
+
assert.deepEqual(
|
|
1157
|
+
result.providers.map((item) => [item.provider, item.label, item.agentCount, item.messageCount]),
|
|
1158
|
+
[
|
|
1159
|
+
["omp", "Oh My Pi", 2, 1],
|
|
1160
|
+
["pi", "Pi", 1, 0],
|
|
1161
|
+
],
|
|
1162
|
+
);
|
|
1106
1163
|
});
|
|
1107
1164
|
|
|
1108
1165
|
it("honours provider, from and to filters and skips invalid timestamps", () => {
|
|
@@ -1129,3 +1186,67 @@ describe("aggregateAgentCreations (051)", () => {
|
|
|
1129
1186
|
);
|
|
1130
1187
|
});
|
|
1131
1188
|
});
|
|
1189
|
+
|
|
1190
|
+
describe("projects (070)", () => {
|
|
1191
|
+
const projects = [
|
|
1192
|
+
{ root: "/w/app", name: "App" },
|
|
1193
|
+
{ root: "/w/app/packages/lib", name: "Lib" },
|
|
1194
|
+
{ root: "/w/tools/", name: "Tools" },
|
|
1195
|
+
];
|
|
1196
|
+
|
|
1197
|
+
it("resolves recorded roots, longest containing root, worktree siblings, cwd, then Other", () => {
|
|
1198
|
+
const map = resolveAgentProjects(
|
|
1199
|
+
[
|
|
1200
|
+
{ agentId: "recorded", cwd: "/h/.paseo/worktrees/abc/feat", projectRoot: "/w/app" },
|
|
1201
|
+
{ agentId: "sibling", cwd: "/h/.paseo/worktrees/abc/fix" },
|
|
1202
|
+
{ agentId: "orphan-worktree", cwd: "/h/.paseo/worktrees/zzz/fix" },
|
|
1203
|
+
{ agentId: "nested", cwd: "/w/app/packages/lib/src" },
|
|
1204
|
+
{ agentId: "slash", cwd: "/w/tools" },
|
|
1205
|
+
{ agentId: "loose", cwd: "/tmp/scratch/" },
|
|
1206
|
+
{ agentId: "none" },
|
|
1207
|
+
],
|
|
1208
|
+
projects,
|
|
1209
|
+
);
|
|
1210
|
+
assert.deepEqual(Object.fromEntries(map), {
|
|
1211
|
+
recorded: "/w/app",
|
|
1212
|
+
sibling: "/w/app",
|
|
1213
|
+
"orphan-worktree": OTHER_PROJECT_KEY,
|
|
1214
|
+
nested: "/w/app/packages/lib",
|
|
1215
|
+
slash: "/w/tools",
|
|
1216
|
+
loose: "/tmp/scratch",
|
|
1217
|
+
none: OTHER_PROJECT_KEY,
|
|
1218
|
+
});
|
|
1219
|
+
});
|
|
1220
|
+
|
|
1221
|
+
it("counts sessions, prompts, skills (low excluded) and MCP per project with a provider filter", () => {
|
|
1222
|
+
const agents = [
|
|
1223
|
+
{ agentId: "a", provider: "codex", cwd: "/w/app" },
|
|
1224
|
+
{ agentId: "b", provider: "claude", cwd: "/w/app/x" },
|
|
1225
|
+
{ agentId: "c", provider: "codex", cwd: "/tmp/scratch" },
|
|
1226
|
+
];
|
|
1227
|
+
const rows = [
|
|
1228
|
+
{ agentId: "a", provider: "codex", category: "skill", confidence: "exact" },
|
|
1229
|
+
{ agentId: "a", provider: "codex", category: "skill", confidence: "low" },
|
|
1230
|
+
{ agentId: "b", provider: "claude", category: "mcp", confidence: null },
|
|
1231
|
+
{ agentId: "ghost", provider: "codex", category: "mcp", confidence: null },
|
|
1232
|
+
];
|
|
1233
|
+
const messages = [
|
|
1234
|
+
{ agentId: "a", provider: "codex" },
|
|
1235
|
+
{ agentId: "b", provider: "claude" },
|
|
1236
|
+
{ agentId: "c", provider: "codex" },
|
|
1237
|
+
];
|
|
1238
|
+
assert.deepEqual(aggregateByProject(agents, rows, messages, projects), [
|
|
1239
|
+
{ key: "/w/app", label: "App", agentCount: 2, messageCount: 2, skillCalls: 1, mcpCalls: 1 },
|
|
1240
|
+
{ key: "/tmp/scratch", label: "scratch", agentCount: 1, messageCount: 1, skillCalls: 0, mcpCalls: 0 },
|
|
1241
|
+
{ key: OTHER_PROJECT_KEY, label: "", agentCount: 0, messageCount: 0, skillCalls: 0, mcpCalls: 1 },
|
|
1242
|
+
]);
|
|
1243
|
+
assert.deepEqual(
|
|
1244
|
+
aggregateByProject(agents, rows, messages, projects, { provider: "claude" }).map((item) => [
|
|
1245
|
+
item.key,
|
|
1246
|
+
item.agentCount,
|
|
1247
|
+
item.mcpCalls,
|
|
1248
|
+
]),
|
|
1249
|
+
[["/w/app", 1, 1]],
|
|
1250
|
+
);
|
|
1251
|
+
});
|
|
1252
|
+
});
|
package/shared/usage.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defineRpc } from "@getpaseo/plugin";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
import { CategorySchema, ConfidenceSchema,
|
|
3
|
+
import { CategorySchema, ConfidenceSchema, normalizeProvider } from "./classify.ts";
|
|
4
4
|
import { formatDisplayName } from "./format.ts";
|
|
5
5
|
|
|
6
6
|
export const usageSummaryRpc = defineRpc({
|
|
@@ -311,9 +311,73 @@ export const usageAgentLifetimeRpc = defineRpc({
|
|
|
311
311
|
longest: AgentLifetimeItemSchema.nullable(),
|
|
312
312
|
/** Rows compared after the provider filter (registry sample). */
|
|
313
313
|
sampleSize: z.number().int().nonnegative(),
|
|
314
|
+
/** Mean engaged time per session (072); null when no session has events. */
|
|
315
|
+
averageEngagedMs: z.number().int().nonnegative().nullable(),
|
|
316
|
+
/** Sessions with ≥1 prompt (072); denominator of the multi-turn share. */
|
|
317
|
+
promptedSessions: z.number().int().nonnegative(),
|
|
318
|
+
/** Sessions with ≥2 prompts (072). */
|
|
319
|
+
multiTurnSessions: z.number().int().nonnegative(),
|
|
314
320
|
}),
|
|
315
321
|
});
|
|
316
322
|
|
|
323
|
+
/** Sessions with ≥1 / ≥2 prompts among `agents` (072); prompts of unknown agents are ignored. */
|
|
324
|
+
export function countMultiTurnSessions(
|
|
325
|
+
agents: ReadonlyArray<{ agentId: string }>,
|
|
326
|
+
messages: ReadonlyArray<{ agentId: string }>,
|
|
327
|
+
): { promptedSessions: number; multiTurnSessions: number } {
|
|
328
|
+
const prompts = new Map<string, number>(agents.map((agent) => [agent.agentId, 0]));
|
|
329
|
+
for (const message of messages) {
|
|
330
|
+
const count = prompts.get(message.agentId);
|
|
331
|
+
if (count !== undefined) prompts.set(message.agentId, count + 1);
|
|
332
|
+
}
|
|
333
|
+
let promptedSessions = 0;
|
|
334
|
+
let multiTurnSessions = 0;
|
|
335
|
+
for (const count of prompts.values()) {
|
|
336
|
+
if (count >= 1) promptedSessions += 1;
|
|
337
|
+
if (count >= 2) multiTurnSessions += 1;
|
|
338
|
+
}
|
|
339
|
+
return { promptedSessions, multiTurnSessions };
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** Gaps longer than this between a session's events count as idle, not engaged (072). */
|
|
343
|
+
export const SESSION_IDLE_GAP_MS = 30 * 60_000;
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Mean engaged time per session (072): sum of gaps between consecutive events
|
|
347
|
+
* (creation, prompts, tool calls), skipping idle gaps over `idleGapMs`, so a
|
|
348
|
+
* session resumed days later is not counted as days long. Sessions without any
|
|
349
|
+
* prompt / tool call are excluded.
|
|
350
|
+
*/
|
|
351
|
+
export function averageEngagedSessionMs(
|
|
352
|
+
agents: ReadonlyArray<{ agentId: string; createdAt?: string | null }>,
|
|
353
|
+
events: ReadonlyArray<{ agentId: string; ts: string | null; ingestedAt: string }>,
|
|
354
|
+
idleGapMs = SESSION_IDLE_GAP_MS,
|
|
355
|
+
): number | null {
|
|
356
|
+
const times = new Map<string, number[]>();
|
|
357
|
+
for (const agent of agents) {
|
|
358
|
+
const created = agent.createdAt ? Date.parse(agent.createdAt) : Number.NaN;
|
|
359
|
+
times.set(agent.agentId, Number.isFinite(created) ? [created] : []);
|
|
360
|
+
}
|
|
361
|
+
const withEvents = new Set<string>();
|
|
362
|
+
for (const event of events) {
|
|
363
|
+
const list = times.get(event.agentId);
|
|
364
|
+
if (!list) continue;
|
|
365
|
+
const t = Date.parse(event.ts ?? event.ingestedAt);
|
|
366
|
+
if (!Number.isFinite(t)) continue;
|
|
367
|
+
list.push(t);
|
|
368
|
+
withEvents.add(event.agentId);
|
|
369
|
+
}
|
|
370
|
+
let total = 0;
|
|
371
|
+
for (const agentId of withEvents) {
|
|
372
|
+
const list = times.get(agentId)!.sort((a, b) => a - b);
|
|
373
|
+
for (let i = 1; i < list.length; i++) {
|
|
374
|
+
const gap = list[i]! - list[i - 1]!;
|
|
375
|
+
if (gap <= idleGapMs) total += gap;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return withEvents.size > 0 ? Math.round(total / withEvents.size) : null;
|
|
379
|
+
}
|
|
380
|
+
|
|
317
381
|
/**
|
|
318
382
|
* Longest-lived agent. Archived agents use their archive time; active agents
|
|
319
383
|
* count `createdAt → now` (051). Rows need a parseable `createdAt` and a
|
|
@@ -410,7 +474,7 @@ export function aggregateAgentCreations(
|
|
|
410
474
|
if (options.to && agent.createdAt > options.to) continue;
|
|
411
475
|
const day = localDayKey(agent.createdAt);
|
|
412
476
|
if (!day) continue;
|
|
413
|
-
const id =
|
|
477
|
+
const id = normalizeProvider(agent.provider);
|
|
414
478
|
const counts = byDay.get(day) ?? new Map<string, number>();
|
|
415
479
|
counts.set(id, (counts.get(id) ?? 0) + 1);
|
|
416
480
|
byDay.set(day, counts);
|
|
@@ -1338,3 +1402,128 @@ export function aggregateActivityByHour(
|
|
|
1338
1402
|
return buckets;
|
|
1339
1403
|
}
|
|
1340
1404
|
|
|
1405
|
+
|
|
1406
|
+
export const ProjectUsageItemSchema = z.object({
|
|
1407
|
+
/** Project root path; `OTHER_PROJECT_KEY` groups agents without a resolvable project. */
|
|
1408
|
+
key: z.string(),
|
|
1409
|
+
/** Paseo project display name, else the root's basename; empty for Other (client localizes). */
|
|
1410
|
+
label: z.string(),
|
|
1411
|
+
agentCount: z.number().int().nonnegative(),
|
|
1412
|
+
messageCount: z.number().int().nonnegative(),
|
|
1413
|
+
/** Exact + inferred skill calls (low excluded, same as provider KPI). */
|
|
1414
|
+
skillCalls: z.number().int().nonnegative(),
|
|
1415
|
+
mcpCalls: z.number().int().nonnegative(),
|
|
1416
|
+
});
|
|
1417
|
+
export type ProjectUsageItem = z.infer<typeof ProjectUsageItemSchema>;
|
|
1418
|
+
|
|
1419
|
+
/** Sessions / prompts / skill / MCP per project (070). */
|
|
1420
|
+
export const usageByProjectRpc = defineRpc({
|
|
1421
|
+
name: "usage.by-project",
|
|
1422
|
+
input: z.object({
|
|
1423
|
+
/** Normalized provider id; omit / empty = all providers. */
|
|
1424
|
+
provider: z.string().optional(),
|
|
1425
|
+
}),
|
|
1426
|
+
output: z.object({ projects: z.array(ProjectUsageItemSchema) }),
|
|
1427
|
+
});
|
|
1428
|
+
|
|
1429
|
+
export const OTHER_PROJECT_KEY = "__other__";
|
|
1430
|
+
|
|
1431
|
+
export type ProjectRef = { root: string; name: string };
|
|
1432
|
+
|
|
1433
|
+
function trimSlash(path: string): string {
|
|
1434
|
+
return path.length > 1 ? path.replace(/\/+$/, "") : path;
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
function basename(path: string): string {
|
|
1438
|
+
const parts = trimSlash(path).split("/");
|
|
1439
|
+
return parts[parts.length - 1] || path;
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
/** `<paseo-home>/worktrees/<hash>/<slug>[/…]` → `<hash>` (070). */
|
|
1443
|
+
function worktreeHash(cwd: string): string | null {
|
|
1444
|
+
return /\/worktrees\/([^/]+)\/[^/]+(?:\/|$)/.exec(cwd)?.[1] ?? null;
|
|
1445
|
+
}
|
|
1446
|
+
|
|
1447
|
+
/**
|
|
1448
|
+
* Agent → project root (070). Order: the workspace project root recorded while
|
|
1449
|
+
* the workspace was listed; the longest known project root containing `cwd`;
|
|
1450
|
+
* a sibling in the same Paseo worktree bucket that resolved; the `cwd` itself
|
|
1451
|
+
* (non-worktree only). Unresolved agents map to `OTHER_PROJECT_KEY`.
|
|
1452
|
+
*/
|
|
1453
|
+
export function resolveAgentProjects(
|
|
1454
|
+
agents: ReadonlyArray<{ agentId: string; cwd?: string | null; projectRoot?: string | null }>,
|
|
1455
|
+
projects: readonly ProjectRef[],
|
|
1456
|
+
): Map<string, string> {
|
|
1457
|
+
const roots = [...new Set(projects.map((project) => trimSlash(project.root)))].sort(
|
|
1458
|
+
(a, b) => b.length - a.length,
|
|
1459
|
+
);
|
|
1460
|
+
const containing = (cwd: string) =>
|
|
1461
|
+
roots.find((root) => cwd === root || cwd.startsWith(`${root}/`)) ?? null;
|
|
1462
|
+
const resolved = new Map<string, string>();
|
|
1463
|
+
const byHash = new Map<string, string>();
|
|
1464
|
+
const pending: Array<{ agentId: string; cwd: string | null; hash: string | null }> = [];
|
|
1465
|
+
for (const agent of agents) {
|
|
1466
|
+
const cwd = agent.cwd?.trim() ? trimSlash(agent.cwd.trim()) : null;
|
|
1467
|
+
const hash = cwd ? worktreeHash(cwd) : null;
|
|
1468
|
+
const root = agent.projectRoot?.trim()
|
|
1469
|
+
? trimSlash(agent.projectRoot.trim())
|
|
1470
|
+
: cwd
|
|
1471
|
+
? containing(cwd)
|
|
1472
|
+
: null;
|
|
1473
|
+
if (root) {
|
|
1474
|
+
resolved.set(agent.agentId, root);
|
|
1475
|
+
if (hash && !byHash.has(hash)) byHash.set(hash, root);
|
|
1476
|
+
} else {
|
|
1477
|
+
pending.push({ agentId: agent.agentId, cwd, hash });
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
for (const agent of pending) {
|
|
1481
|
+
const root = agent.hash ? byHash.get(agent.hash) : agent.cwd;
|
|
1482
|
+
resolved.set(agent.agentId, root ?? OTHER_PROJECT_KEY);
|
|
1483
|
+
}
|
|
1484
|
+
return resolved;
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
export function aggregateByProject(
|
|
1488
|
+
agents: ReadonlyArray<{ agentId: string; provider: string; cwd?: string | null; projectRoot?: string | null }>,
|
|
1489
|
+
rows: ReadonlyArray<{ agentId: string; provider: string; category: string; confidence: string | null }>,
|
|
1490
|
+
messages: ReadonlyArray<{ agentId: string; provider: string }>,
|
|
1491
|
+
projects: readonly ProjectRef[],
|
|
1492
|
+
options: { provider?: string } = {},
|
|
1493
|
+
): ProjectUsageItem[] {
|
|
1494
|
+
const provider = options.provider?.trim() ? normalizeProvider(options.provider) : undefined;
|
|
1495
|
+
const matches = (value: string) => !provider || normalizeProvider(value) === provider;
|
|
1496
|
+
const projectOf = resolveAgentProjects(agents, projects);
|
|
1497
|
+
const names = new Map(projects.map((project) => [trimSlash(project.root), project.name]));
|
|
1498
|
+
const map = new Map<string, ProjectUsageItem>();
|
|
1499
|
+
const ensure = (agentId: string) => {
|
|
1500
|
+
const key = projectOf.get(agentId) ?? OTHER_PROJECT_KEY;
|
|
1501
|
+
let item = map.get(key);
|
|
1502
|
+
if (!item) {
|
|
1503
|
+
item = {
|
|
1504
|
+
key,
|
|
1505
|
+
label: key === OTHER_PROJECT_KEY ? "" : (names.get(key) ?? basename(key)),
|
|
1506
|
+
agentCount: 0,
|
|
1507
|
+
messageCount: 0,
|
|
1508
|
+
skillCalls: 0,
|
|
1509
|
+
mcpCalls: 0,
|
|
1510
|
+
};
|
|
1511
|
+
map.set(key, item);
|
|
1512
|
+
}
|
|
1513
|
+
return item;
|
|
1514
|
+
};
|
|
1515
|
+
for (const agent of agents) {
|
|
1516
|
+
if (matches(agent.provider)) ensure(agent.agentId).agentCount += 1;
|
|
1517
|
+
}
|
|
1518
|
+
for (const message of messages) {
|
|
1519
|
+
if (matches(message.provider)) ensure(message.agentId).messageCount += 1;
|
|
1520
|
+
}
|
|
1521
|
+
for (const row of rows) {
|
|
1522
|
+
if (!matches(row.provider)) continue;
|
|
1523
|
+
if (row.category === "skill" && row.confidence !== "low") ensure(row.agentId).skillCalls += 1;
|
|
1524
|
+
else if (row.category === "mcp") ensure(row.agentId).mcpCalls += 1;
|
|
1525
|
+
}
|
|
1526
|
+
return [...map.values()].sort(
|
|
1527
|
+
(a, b) => b.agentCount - a.agentCount || b.messageCount - a.messageCount || a.key.localeCompare(b.key),
|
|
1528
|
+
);
|
|
1529
|
+
}
|