@koinzhang/paseo-plugin-activity 0.6.0 → 0.8.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 +50 -1
- package/README.md +18 -11
- package/client/activity-heatmap.tsx +115 -74
- package/client/agent-creations.tsx +100 -90
- package/client/attention-pill.tsx +17 -6
- package/client/attention-popover.tsx +19 -23
- package/client/design-tokens.test.ts +39 -0
- package/client/design-tokens.ts +180 -0
- package/client/formatted-time.tsx +4 -4
- package/client/global-surface.tsx +243 -268
- package/client/header-button.ts +136 -0
- package/client/hourly-activity-timeline.tsx +153 -103
- package/client/panel-visibility.test.ts +25 -0
- package/client/panel-visibility.ts +45 -0
- package/client/panel.tsx +94 -111
- package/client/pill.tsx +1 -1
- package/client/provider-dropdown.tsx +134 -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 +294 -0
- package/client/sidebar-item.tsx +15 -0
- package/client/ui.tsx +351 -0
- package/client/usage-popover.tsx +72 -137
- 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 +28 -28
- package/client/workspace/agents-section.tsx +42 -57
- package/client/workspace/constants.ts +8 -6
- package/client/workspace/display-menu.tsx +6 -5
- package/client/workspace/filters.test.ts +1 -1
- package/client/workspace/filters.ts +4 -2
- package/client/workspace/panel.tsx +96 -104
- package/client/workspace/rank-section.tsx +40 -65
- package/client/workspace/running-indicator.tsx +1 -1
- package/client/workspace/terminals-section.tsx +24 -12
- package/index.client.tsx +53 -31
- package/index.server.ts +55 -73
- package/package.json +3 -3
- 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/host-types.ts +6 -6
- 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/server/turn-ingest.test.ts +180 -0
- package/server/turn-ingest.ts +71 -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 +125 -64
- package/shared/usage.test.ts +121 -0
- package/shared/usage.ts +191 -2
package/shared/insights.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import type { ActivityDay, ProviderUsageItem } from "./usage.ts";
|
|
2
|
-
import { isActiveDay } from "./activity.ts";
|
|
2
|
+
import { computeStreaks, isActiveDay } from "./activity.ts";
|
|
3
3
|
import { formatCount, formatDisplayName, formatDuration } from "./format.ts";
|
|
4
|
+
import { messagesFor } from "./i18n.ts";
|
|
4
5
|
|
|
5
6
|
export type InsightRow = { label: string; value: string };
|
|
7
|
+
export type KpiComparison = { text: string; direction: "up" | "down" | "flat" | "unknown" };
|
|
8
|
+
export type KpiRow = InsightRow & { comparison?: KpiComparison };
|
|
6
9
|
|
|
7
10
|
export type InsightSummary = {
|
|
8
11
|
skills: number;
|
|
@@ -15,16 +18,9 @@ export type InsightSummary = {
|
|
|
15
18
|
chatAgents?: number;
|
|
16
19
|
};
|
|
17
20
|
|
|
18
|
-
/** Shared cap for
|
|
21
|
+
/** Shared cap for the Workspace Activity Skills / MCP rankings. */
|
|
19
22
|
export const ACTIVITY_LIST_LIMIT = 8;
|
|
20
23
|
|
|
21
|
-
function formatRatio(numerator: number, denominator: number): string {
|
|
22
|
-
if (denominator <= 0) return "—";
|
|
23
|
-
const value = numerator / denominator;
|
|
24
|
-
if (Number.isInteger(value)) return String(value);
|
|
25
|
-
return value.toFixed(1).replace(/\.0$/, "");
|
|
26
|
-
}
|
|
27
|
-
|
|
28
24
|
function formatDayLabel(dateKey: string, locale: string): string {
|
|
29
25
|
const [y, m, d] = dateKey.split("-").map(Number);
|
|
30
26
|
if (!y || !m || !d) return dateKey;
|
|
@@ -66,35 +62,49 @@ function pickBusiestDay(days: readonly ActivityDay[]): ActivityDay | null {
|
|
|
66
62
|
|
|
67
63
|
function formatBusiest(day: ActivityDay, locale: string): string {
|
|
68
64
|
const label = formatDayLabel(day.date, locale);
|
|
69
|
-
|
|
65
|
+
const { units } = messagesFor(locale);
|
|
66
|
+
if (day.messages > 0) return `${label} · ${units.prompts(day.messages)}`;
|
|
70
67
|
const calls = day.skills + day.mcp;
|
|
71
|
-
if (calls > 0) return `${label} · ${calls}
|
|
72
|
-
if (day.agents > 0) return `${label} · ${day.agents}
|
|
68
|
+
if (calls > 0) return `${label} · ${units.calls(calls)}`;
|
|
69
|
+
if (day.agents > 0) return `${label} · ${units.sessions(day.agents)}`;
|
|
73
70
|
return label;
|
|
74
71
|
}
|
|
75
72
|
|
|
76
|
-
/**
|
|
77
|
-
function
|
|
73
|
+
/** `label · pct%`; `—` when there is no label or no denominator (069). */
|
|
74
|
+
function withShare(label: string | undefined, count: number, total: number): string {
|
|
75
|
+
if (!label || total <= 0) return "—";
|
|
76
|
+
return `${label} · ${Math.round((count / total) * 100)}%`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Messages-weighted top provider (050) with its share of all providers' messages
|
|
81
|
+
* (069). A selected provider is shown with its own share.
|
|
82
|
+
*/
|
|
83
|
+
function topProviderStats(
|
|
78
84
|
providers: readonly ProviderUsageItem[],
|
|
85
|
+
allProviders: readonly ProviderUsageItem[],
|
|
79
86
|
providerFilter: string,
|
|
80
|
-
): string {
|
|
87
|
+
): { value: string; label: string | undefined } {
|
|
88
|
+
const total = allProviders.reduce((sum, p) => sum + p.messageCount, 0);
|
|
81
89
|
if (providerFilter !== "all") {
|
|
82
|
-
|
|
90
|
+
const selected = providers.find((p) => p.provider === providerFilter);
|
|
91
|
+
return { value: withShare(selected?.label, selected?.messageCount ?? 0, total), label: selected && selected.messageCount > 0 ? selected.label : undefined };
|
|
83
92
|
}
|
|
84
93
|
const ranked = [...providers]
|
|
85
94
|
.filter((p) => p.messageCount > 0)
|
|
86
95
|
.sort(
|
|
87
96
|
(a, b) =>
|
|
88
97
|
b.messageCount - a.messageCount || a.provider.localeCompare(b.provider),
|
|
89
|
-
|
|
90
|
-
|
|
98
|
+
);
|
|
99
|
+
const top = ranked[0];
|
|
100
|
+
return { value: withShare(top?.label, top?.messageCount ?? 0, total), label: top?.label };
|
|
91
101
|
}
|
|
92
102
|
|
|
93
|
-
/** Messages-weighted top model (050)
|
|
94
|
-
function
|
|
103
|
+
/** Messages-weighted top model (050) with its share of in-filter model messages (069). */
|
|
104
|
+
function topModelStats(
|
|
95
105
|
providers: readonly ProviderUsageItem[],
|
|
96
106
|
providerFilter: string,
|
|
97
|
-
): string {
|
|
107
|
+
): { value: string; label: string | undefined } {
|
|
98
108
|
const map = new Map<string, number>();
|
|
99
109
|
const source =
|
|
100
110
|
providerFilter === "all"
|
|
@@ -109,19 +119,34 @@ function topModelValue(
|
|
|
109
119
|
(a, b) => b[1] - a[1] || a[0].localeCompare(b[0]),
|
|
110
120
|
);
|
|
111
121
|
const top = ranked[0];
|
|
112
|
-
if (!top || top[1] <= 0) return "—";
|
|
113
|
-
|
|
122
|
+
if (!top || top[1] <= 0) return { value: "—", label: undefined };
|
|
123
|
+
const total = ranked.reduce((sum, [, count]) => sum + count, 0);
|
|
124
|
+
const label = formatDisplayName(top[0]);
|
|
125
|
+
return { value: withShare(label, top[1], total), label };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function comparison(current: number, previous: number, locale: string): KpiComparison {
|
|
129
|
+
const suffix = messagesFor(locale).kpi.previous7Days;
|
|
130
|
+
if (previous === 0 && current > 0) return { text: `— ${suffix}`, direction: "unknown" };
|
|
131
|
+
const change = previous === 0 ? 0 : Math.round(((current - previous) / previous) * 100);
|
|
132
|
+
const direction = change > 0 ? "up" : change < 0 ? "down" : "flat";
|
|
133
|
+
const arrow = direction === "up" ? "↑" : direction === "down" ? "↓" : "→";
|
|
134
|
+
return { text: `${arrow}${Math.abs(change).toLocaleString(locale)}% ${suffix}`, direction };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function previousLeader(label: string | undefined, locale: string): KpiComparison {
|
|
138
|
+
return { text: `${label ?? "—"} ${messagesFor(locale).kpi.previous7DaysBare}`, direction: "unknown" };
|
|
114
139
|
}
|
|
115
140
|
|
|
116
141
|
/** Share of active agents that are coding (018); empty creations excluded. */
|
|
117
|
-
function codingVsChatValue(codingAgents: number, chatAgents: number): string {
|
|
142
|
+
function codingVsChatValue(codingAgents: number, chatAgents: number, locale: string): string {
|
|
118
143
|
const total = codingAgents + chatAgents;
|
|
119
144
|
if (total <= 0) return "—";
|
|
120
145
|
const pct = Math.round((codingAgents / total) * 100);
|
|
121
|
-
return
|
|
146
|
+
return messagesFor(locale).insights.codingShare(pct);
|
|
122
147
|
}
|
|
123
148
|
|
|
124
|
-
/** Weekday with the highest activity volume among active days. */
|
|
149
|
+
/** Weekday with the highest activity volume among active days (full name, 072). */
|
|
125
150
|
function peakWeekdayValue(days: readonly ActivityDay[], locale: string): string {
|
|
126
151
|
const volumes = [0, 0, 0, 0, 0, 0, 0];
|
|
127
152
|
for (const day of days) {
|
|
@@ -139,83 +164,119 @@ function peakWeekdayValue(days: readonly ActivityDay[], locale: string): string
|
|
|
139
164
|
}
|
|
140
165
|
}
|
|
141
166
|
if (bestDow < 0 || bestVol <= 0) return "—";
|
|
142
|
-
// 2026-03-08 was a Sunday — use a known Sunday + offset for locale
|
|
167
|
+
// 2026-03-08 was a Sunday — use a known Sunday + offset for the locale name.
|
|
143
168
|
const probe = new Date(2026, 2, 8 + bestDow);
|
|
144
|
-
return probe.toLocaleDateString(locale, { weekday: "
|
|
169
|
+
return probe.toLocaleDateString(locale, { weekday: "long" });
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Share of prompted sessions with ≥2 prompts (072); empty creations excluded. */
|
|
173
|
+
function multiTurnValue(
|
|
174
|
+
multiTurn: { multiTurnSessions: number; promptedSessions: number } | null,
|
|
175
|
+
): string {
|
|
176
|
+
if (!multiTurn || multiTurn.promptedSessions <= 0) return "—";
|
|
177
|
+
return `${Math.round((multiTurn.multiTurnSessions / multiTurn.promptedSessions) * 100)}%`;
|
|
145
178
|
}
|
|
146
179
|
|
|
147
180
|
/**
|
|
148
|
-
* Fixed 8-row insights:
|
|
149
|
-
*
|
|
181
|
+
* Fixed 8-row insights (072 / 080): habit (active days, busiest day, streak,
|
|
182
|
+
* peak weekday, workspaces, coding share) → session shape (multi-turn share,
|
|
183
|
+
* engaged time).
|
|
150
184
|
*/
|
|
151
185
|
export function buildActivityInsights(input: {
|
|
152
186
|
days: readonly ActivityDay[];
|
|
153
187
|
summary: InsightSummary;
|
|
154
|
-
/** Distinct workspaces among
|
|
188
|
+
/** Distinct workspaces among sessions (011). */
|
|
155
189
|
workspaces: number;
|
|
190
|
+
/** Mean engaged time per session from `usage.agent-lifetime` (072). */
|
|
191
|
+
averageSessionMs?: number | null;
|
|
192
|
+
/** Sessions with ≥2 / ≥1 prompts from `usage.agent-lifetime` (072). */
|
|
193
|
+
multiTurn?: { multiTurnSessions: number; promptedSessions: number } | null;
|
|
156
194
|
locale?: string;
|
|
195
|
+
today?: Date;
|
|
157
196
|
}): InsightRow[] {
|
|
158
197
|
const locale = input.locale ?? "en";
|
|
159
|
-
const activeDays = input.days.filter(isActiveDay).length;
|
|
160
198
|
const busiest = pickBusiestDay(input.days);
|
|
199
|
+
const activeDays = input.days.filter(isActiveDay).length;
|
|
200
|
+
const { longest } = computeStreaks(input.days, input.today);
|
|
161
201
|
const codingAgents = input.summary.codingAgents ?? 0;
|
|
162
202
|
const chatAgents = input.summary.chatAgents ?? 0;
|
|
203
|
+
const { insights: m, units } = messagesFor(locale);
|
|
204
|
+
const averageSessionMs = input.averageSessionMs;
|
|
163
205
|
|
|
164
206
|
return [
|
|
165
|
-
{ label:
|
|
207
|
+
{ label: m.activeDays, value: activeDays.toLocaleString(locale) },
|
|
166
208
|
{
|
|
167
|
-
label:
|
|
209
|
+
label: m.busiestDay,
|
|
168
210
|
value: busiest ? formatBusiest(busiest, locale) : "—",
|
|
169
211
|
},
|
|
170
|
-
{ label:
|
|
171
|
-
{ label:
|
|
172
|
-
{ label:
|
|
173
|
-
{ label: "MCP calls", value: input.summary.mcp.toLocaleString(locale) },
|
|
212
|
+
{ label: m.longestStreak, value: longest > 0 ? units.days(longest) : "—" },
|
|
213
|
+
{ label: m.peakWeekday, value: peakWeekdayValue(input.days, locale) },
|
|
214
|
+
{ label: m.workspaces, value: formatCount(input.workspaces) },
|
|
174
215
|
{
|
|
175
|
-
label:
|
|
176
|
-
value:
|
|
216
|
+
label: m.codingVsChat,
|
|
217
|
+
value: codingVsChatValue(codingAgents, chatAgents, locale),
|
|
177
218
|
},
|
|
219
|
+
{ label: m.multiTurnSessions, value: multiTurnValue(input.multiTurn ?? null) },
|
|
178
220
|
{
|
|
179
|
-
label:
|
|
180
|
-
value:
|
|
221
|
+
label: m.avgSessionDuration,
|
|
222
|
+
value: averageSessionMs == null ? "—" : formatDuration(averageSessionMs, locale),
|
|
181
223
|
},
|
|
182
224
|
];
|
|
183
225
|
}
|
|
184
226
|
|
|
185
227
|
/**
|
|
186
|
-
* Fixed
|
|
187
|
-
*
|
|
228
|
+
* Fixed 4-tile KPI row (069 / 070 / 072): sessions, prompts, top provider ·
|
|
229
|
+
* share, top model · share. Active days lives in Insights.
|
|
188
230
|
*/
|
|
189
231
|
export function buildActivityKpi(input: {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
days: readonly ActivityDay[];
|
|
232
|
+
sessions: number;
|
|
233
|
+
messages: number;
|
|
193
234
|
locale?: string;
|
|
194
|
-
|
|
195
|
-
/** Longest created→(archived | now) span from the registry (049 / 051). */
|
|
196
|
-
longestAgent: { durationMs: number; active: boolean } | null;
|
|
235
|
+
/** Provider rows after the provider filter. */
|
|
197
236
|
providers: readonly ProviderUsageItem[];
|
|
237
|
+
/** Every provider; denominator of the top-provider share. */
|
|
238
|
+
allProviders: readonly ProviderUsageItem[];
|
|
198
239
|
providerFilter: string;
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
240
|
+
/** Two adjacent seven-day windows, when the comparison query has loaded. */
|
|
241
|
+
comparison?: {
|
|
242
|
+
current: readonly ProviderUsageItem[];
|
|
243
|
+
previous: readonly ProviderUsageItem[];
|
|
244
|
+
};
|
|
245
|
+
}): KpiRow[] {
|
|
246
|
+
const locale = input.locale ?? "en";
|
|
247
|
+
const m = messagesFor(locale).kpi;
|
|
248
|
+
const topProvider = topProviderStats(input.providers, input.allProviders, input.providerFilter);
|
|
249
|
+
const topModel = topModelStats(input.providers, input.providerFilter);
|
|
250
|
+
const current = input.comparison?.current ?? [];
|
|
251
|
+
const previous = input.comparison?.previous ?? [];
|
|
252
|
+
const previousProvider = topProviderStats(previous, previous, input.providerFilter);
|
|
253
|
+
const previousModel = topModelStats(previous, input.providerFilter);
|
|
254
|
+
const count = (items: readonly ProviderUsageItem[], field: "agentCount" | "messageCount") =>
|
|
255
|
+
items.reduce(
|
|
256
|
+
(sum, item) => sum + (input.providerFilter === "all" || item.provider === input.providerFilter ? item[field] : 0),
|
|
257
|
+
0,
|
|
258
|
+
);
|
|
259
|
+
const trend = input.comparison;
|
|
202
260
|
return [
|
|
203
|
-
{ label: "Agents", value: formatCount(input.agents) },
|
|
204
261
|
{
|
|
205
|
-
label:
|
|
206
|
-
value:
|
|
207
|
-
|
|
208
|
-
|
|
262
|
+
label: m.sessions,
|
|
263
|
+
value: input.sessions.toLocaleString(locale),
|
|
264
|
+
comparison: trend ? comparison(count(current, "agentCount"), count(previous, "agentCount"), locale) : undefined,
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
label: m.prompts,
|
|
268
|
+
value: input.messages.toLocaleString(locale),
|
|
269
|
+
comparison: trend ? comparison(count(current, "messageCount"), count(previous, "messageCount"), locale) : undefined,
|
|
209
270
|
},
|
|
210
271
|
{
|
|
211
|
-
label:
|
|
212
|
-
value:
|
|
272
|
+
label: m.topProvider,
|
|
273
|
+
value: topProvider.value,
|
|
274
|
+
comparison: trend ? previousLeader(previousProvider.label, locale) : undefined,
|
|
213
275
|
},
|
|
214
276
|
{
|
|
215
|
-
label:
|
|
216
|
-
value:
|
|
277
|
+
label: m.topModel,
|
|
278
|
+
value: topModel.value,
|
|
279
|
+
comparison: trend ? previousLeader(previousModel.label, locale) : undefined,
|
|
217
280
|
},
|
|
218
|
-
{ label: "Peak weekday", value: peakWeekdayValue(input.days, input.locale ?? "en") },
|
|
219
|
-
{ label: "Longest streak", value: `${streak} day${streak === 1 ? "" : "s"}` },
|
|
220
281
|
];
|
|
221
282
|
}
|
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
|
+
}
|