@narumitw/pi-analytics 0.49.9 → 0.49.11
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/README.md +13 -35
- package/dist/index.ts +53 -106
- package/dist/index.ts.map +2 -2
- package/package.json +49 -50
- package/src/analytics.ts +368 -358
- package/src/collector.ts +250 -262
- package/src/errors.ts +12 -12
- package/src/menu.ts +325 -343
- package/src/skills.ts +55 -57
- package/src/storage/files.ts +352 -374
- package/src/storage/format.ts +159 -201
- package/src/storage/queries.ts +200 -204
- package/src/storage/store.ts +39 -43
- package/src/types.ts +66 -79
package/src/analytics.ts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import {
|
|
4
|
-
type ExtensionAPI,
|
|
5
|
-
type ExtensionContext,
|
|
6
|
-
getAgentDir,
|
|
7
|
-
} from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { type ExtensionAPI, type ExtensionContext, getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
8
4
|
import { ResponseCollector } from "./collector.js";
|
|
9
5
|
import { type AnalyticsMenuDataSource, showAnalyticsMenu } from "./menu.js";
|
|
10
6
|
import { SkillTracker } from "./skills.js";
|
|
@@ -13,385 +9,399 @@ import type { AnalyticsSnapshot, TimeRange } from "./storage/queries.js";
|
|
|
13
9
|
import { AnalyticsStore } from "./storage/store.js";
|
|
14
10
|
import type { ModelIdentity, SettledRun, TriggerSource } from "./types.js";
|
|
15
11
|
|
|
12
|
+
interface PendingProviderGeneration {
|
|
13
|
+
id: string;
|
|
14
|
+
startedAtMs: number;
|
|
15
|
+
model?: ModelIdentity;
|
|
16
|
+
responses: Array<{ status: number; occurredAtMs: number }>;
|
|
17
|
+
}
|
|
18
|
+
|
|
16
19
|
const STORAGE_DIRECTORY = "pi-analytics";
|
|
17
20
|
|
|
18
21
|
export interface AnalyticsStorePort {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
readonly path: string;
|
|
23
|
+
recordRun(run: SettledRun, signal?: AbortSignal): Promise<void>;
|
|
24
|
+
getSnapshot(range: TimeRange, signal?: AbortSignal): Promise<AnalyticsSnapshot>;
|
|
25
|
+
clearAll(signal?: AbortSignal): Promise<ClearAnalyticsResult>;
|
|
26
|
+
close(): Promise<void>;
|
|
24
27
|
}
|
|
25
28
|
|
|
26
29
|
interface AnalyticsDependencies {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
createStore(path: string): AnalyticsStorePort;
|
|
31
|
+
createSkillTracker(cwd: string): SkillTracker;
|
|
32
|
+
getAgentDir(): string;
|
|
33
|
+
now(): number;
|
|
34
|
+
createId(): string;
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
export function createAnalyticsExtension(
|
|
35
|
-
|
|
38
|
+
dependencies: Partial<AnalyticsDependencies> = {},
|
|
36
39
|
): (pi: ExtensionAPI) => void {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
40
|
+
const deps: AnalyticsDependencies = {
|
|
41
|
+
createStore: dependencies.createStore ?? ((rootPath) => new AnalyticsStore(rootPath)),
|
|
42
|
+
createSkillTracker: dependencies.createSkillTracker ?? ((cwd) => new SkillTracker(cwd)),
|
|
43
|
+
getAgentDir: dependencies.getAgentDir ?? getAgentDir,
|
|
44
|
+
now: dependencies.now ?? Date.now,
|
|
45
|
+
createId: dependencies.createId ?? randomUUID,
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return function analyticsExtension(pi: ExtensionAPI): void {
|
|
49
|
+
let sessionGeneration = 0;
|
|
50
|
+
let sessionController = new AbortController();
|
|
51
|
+
let collector = new ResponseCollector();
|
|
52
|
+
let skillTracker: SkillTracker | undefined;
|
|
53
|
+
let store: AnalyticsStorePort | undefined;
|
|
54
|
+
let storageFailure: string | undefined;
|
|
55
|
+
const retiredCloseTasks = new Set<Promise<boolean>>();
|
|
56
|
+
let writeFailureActive = false;
|
|
57
|
+
let pendingTriggerSource: TriggerSource = "unknown";
|
|
58
|
+
let pendingAttemptWithoutRun = false;
|
|
59
|
+
let pendingProviderGeneration: PendingProviderGeneration | undefined;
|
|
60
|
+
|
|
61
|
+
pi.registerCommand("analytics", {
|
|
62
|
+
description: "Open local Pi usage analytics",
|
|
63
|
+
handler: async (args, ctx) => {
|
|
64
|
+
if (args.trim()) {
|
|
65
|
+
if (!ctx.hasUI || (ctx.mode !== "tui" && ctx.mode !== "rpc")) {
|
|
66
|
+
throw new Error("/analytics does not accept arguments.");
|
|
67
|
+
}
|
|
68
|
+
ctx.ui.notify("/analytics does not accept arguments.", "warning");
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (!ctx.hasUI || (ctx.mode !== "tui" && ctx.mode !== "rpc")) {
|
|
72
|
+
throw new Error("/analytics requires Pi TUI or RPC mode.");
|
|
73
|
+
}
|
|
74
|
+
const generation = sessionGeneration;
|
|
75
|
+
const owner = sessionController;
|
|
76
|
+
const source = menuSource(generation, owner.signal);
|
|
77
|
+
await showAnalyticsMenu(ctx, source, {
|
|
78
|
+
signal: owner.signal,
|
|
79
|
+
isCurrent: () => generation === sessionGeneration && !owner.signal.aborted,
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
pi.on("session_start", (_event, ctx) => {
|
|
85
|
+
++sessionGeneration;
|
|
86
|
+
const previousStore = store;
|
|
87
|
+
sessionController.abort(new DOMException("Analytics session replaced", "AbortError"));
|
|
88
|
+
if (previousStore) retire(previousStore);
|
|
89
|
+
sessionController = new AbortController();
|
|
90
|
+
collector = new ResponseCollector();
|
|
91
|
+
skillTracker = deps.createSkillTracker(ctx.cwd);
|
|
92
|
+
store = undefined;
|
|
93
|
+
storageFailure = undefined;
|
|
94
|
+
writeFailureActive = false;
|
|
95
|
+
pendingTriggerSource = "unknown";
|
|
96
|
+
pendingAttemptWithoutRun = false;
|
|
97
|
+
pendingProviderGeneration = undefined;
|
|
98
|
+
const storageRoot = path.join(deps.getAgentDir(), STORAGE_DIRECTORY);
|
|
99
|
+
try {
|
|
100
|
+
store = deps.createStore(storageRoot);
|
|
101
|
+
} catch {
|
|
102
|
+
storageFailure = unavailableMessage();
|
|
103
|
+
safeNotify(ctx, storageFailure, "warning");
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
pi.on("input", (event, ctx) => {
|
|
108
|
+
const now = deps.now();
|
|
109
|
+
const tracker = skillTracker;
|
|
110
|
+
tracker?.observeInput(event.text, event.source, now);
|
|
111
|
+
if (event.source !== "extension") pendingTriggerSource = event.source;
|
|
112
|
+
if (!tracker || !collector.hasActiveRun()) return;
|
|
113
|
+
const explicit = tracker.consumeExplicitSkill();
|
|
114
|
+
if (!explicit || !tracker.hasAvailableSkill(explicit.name)) return;
|
|
115
|
+
collector.activateSkill({
|
|
116
|
+
name: explicit.name,
|
|
117
|
+
initiatedBy: "user",
|
|
118
|
+
now: explicit.observedAtMs,
|
|
119
|
+
model: modelIdentity(ctx, pi),
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
pi.on("before_agent_start", async (event, ctx) => {
|
|
124
|
+
// A cache warm has provider hooks but no assistant lifecycle. Drop any unclaimed
|
|
125
|
+
// warm metadata when the next real agent run begins instead of classifying by timing.
|
|
126
|
+
pendingProviderGeneration = undefined;
|
|
127
|
+
const generation = sessionGeneration;
|
|
128
|
+
const tracker = skillTracker;
|
|
129
|
+
const activeCollector = collector;
|
|
130
|
+
if (!tracker) return;
|
|
131
|
+
const skills = availableSkills(pi, event.systemPromptOptions.skills ?? []);
|
|
132
|
+
await tracker.setAvailableSkills(skills);
|
|
133
|
+
if (generation !== sessionGeneration || tracker !== skillTracker || activeCollector !== collector) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const explicit = tracker.consumeExplicitSkill();
|
|
137
|
+
const interrupted = activeCollector.begin({
|
|
138
|
+
id: deps.createId(),
|
|
139
|
+
now: deps.now(),
|
|
140
|
+
triggerSource: explicit?.source ?? pendingTriggerSource,
|
|
141
|
+
model: modelIdentity(ctx, pi),
|
|
142
|
+
});
|
|
143
|
+
pendingTriggerSource = "unknown";
|
|
144
|
+
if (interrupted) {
|
|
145
|
+
await persistRun(interrupted, ctx, generation, sessionController.signal);
|
|
146
|
+
if (generation !== sessionGeneration || tracker !== skillTracker || activeCollector !== collector) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (explicit && skills.some(({ name }) => name === explicit.name)) {
|
|
151
|
+
activeCollector.activateSkill({
|
|
152
|
+
name: explicit.name,
|
|
153
|
+
initiatedBy: "user",
|
|
154
|
+
now: explicit.observedAtMs,
|
|
155
|
+
model: modelIdentity(ctx, pi),
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
pi.on("agent_start", () => {
|
|
161
|
+
if (collector.hasActiveRun()) collector.beginAttempt();
|
|
162
|
+
else pendingAttemptWithoutRun = true;
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
pi.on("turn_start", (_event, ctx) => ensureRun(ctx, "extension"));
|
|
166
|
+
|
|
167
|
+
pi.on("before_provider_request", (_event, ctx) => {
|
|
168
|
+
pendingProviderGeneration = {
|
|
169
|
+
id: deps.createId(),
|
|
170
|
+
startedAtMs: deps.now(),
|
|
171
|
+
model: modelIdentity(ctx, pi),
|
|
172
|
+
responses: [],
|
|
173
|
+
};
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
pi.on("after_provider_response", (event) => {
|
|
177
|
+
pendingProviderGeneration?.responses.push({ status: event.status, occurredAtMs: deps.now() });
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
pi.on("message_start", (event, ctx) => {
|
|
181
|
+
if (event.message.role === "assistant") claimPendingProviderGeneration(ctx);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
pi.on("message_end", (event, ctx) => {
|
|
185
|
+
if (event.message.role !== "assistant") return;
|
|
186
|
+
claimPendingProviderGeneration(ctx);
|
|
187
|
+
collector.finishGeneration({
|
|
188
|
+
now: deps.now(),
|
|
189
|
+
stopReason: event.message.stopReason,
|
|
190
|
+
errorMessage: event.message.errorMessage,
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
pi.on("tool_execution_start", (event, ctx) => {
|
|
195
|
+
ensureRun(ctx, "extension");
|
|
196
|
+
collector.beginTool({
|
|
197
|
+
id: event.toolCallId,
|
|
198
|
+
name: event.toolName,
|
|
199
|
+
now: deps.now(),
|
|
200
|
+
model: modelIdentity(ctx, pi),
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
pi.on("tool_result", async (event, ctx) => {
|
|
205
|
+
if (event.toolName === "read" && !isBuiltinReadTool(pi)) return;
|
|
206
|
+
const generation = sessionGeneration;
|
|
207
|
+
const tracker = skillTracker;
|
|
208
|
+
const activeCollector = collector;
|
|
209
|
+
const runId = activeCollector.getActiveRunId();
|
|
210
|
+
if (!tracker || !runId) return;
|
|
211
|
+
const name = await tracker.matchSuccessfulRead({
|
|
212
|
+
toolName: event.toolName,
|
|
213
|
+
input: event.input,
|
|
214
|
+
isError: event.isError,
|
|
215
|
+
});
|
|
216
|
+
if (
|
|
217
|
+
!name ||
|
|
218
|
+
generation !== sessionGeneration ||
|
|
219
|
+
tracker !== skillTracker ||
|
|
220
|
+
activeCollector !== collector ||
|
|
221
|
+
activeCollector.getActiveRunId() !== runId
|
|
222
|
+
) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
activeCollector.activateSkill({
|
|
226
|
+
name,
|
|
227
|
+
initiatedBy: "model",
|
|
228
|
+
now: deps.now(),
|
|
229
|
+
model: modelIdentity(ctx, pi),
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
pi.on("tool_execution_end", (event) => {
|
|
234
|
+
collector.finishTool({ id: event.toolCallId, now: deps.now(), isError: event.isError });
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
pi.on("agent_settled", async (_event, ctx) => {
|
|
238
|
+
const generation = sessionGeneration;
|
|
239
|
+
const owner = sessionController;
|
|
240
|
+
const run = collector.settle(deps.now());
|
|
241
|
+
pendingAttemptWithoutRun = false;
|
|
242
|
+
pendingProviderGeneration = undefined;
|
|
243
|
+
pendingTriggerSource = "unknown";
|
|
244
|
+
skillTracker?.clearPending();
|
|
245
|
+
if (run) await persistRun(run, ctx, generation, owner.signal);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
pi.on("session_shutdown", async (_event, ctx) => {
|
|
249
|
+
const activeStore = store;
|
|
250
|
+
++sessionGeneration;
|
|
251
|
+
sessionController.abort(new DOMException("Analytics session shut down", "AbortError"));
|
|
252
|
+
skillTracker?.clearPending();
|
|
253
|
+
skillTracker = undefined;
|
|
254
|
+
store = undefined;
|
|
255
|
+
pendingProviderGeneration = undefined;
|
|
256
|
+
collector.interrupt(deps.now());
|
|
257
|
+
const closing = activeStore ? [closeResult(activeStore), ...retiredCloseTasks] : [...retiredCloseTasks];
|
|
258
|
+
const results = await Promise.all(closing);
|
|
259
|
+
if (results.some((closed) => !closed)) {
|
|
260
|
+
safeNotify(ctx, "Analytics storage shutdown was incomplete.", "warning");
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
function retire(retiredStore: AnalyticsStorePort): void {
|
|
265
|
+
const task = closeResult(retiredStore).finally(() => retiredCloseTasks.delete(task));
|
|
266
|
+
retiredCloseTasks.add(task);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async function closeResult(activeStore: AnalyticsStorePort): Promise<boolean> {
|
|
270
|
+
try {
|
|
271
|
+
await activeStore.close();
|
|
272
|
+
return true;
|
|
273
|
+
} catch {
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function claimPendingProviderGeneration(ctx: ExtensionContext): void {
|
|
279
|
+
const pending = pendingProviderGeneration;
|
|
280
|
+
if (!pending) return;
|
|
281
|
+
pendingProviderGeneration = undefined;
|
|
282
|
+
ensureRun(ctx, "extension");
|
|
283
|
+
collector.beginGeneration({
|
|
284
|
+
id: pending.id,
|
|
285
|
+
now: pending.startedAtMs,
|
|
286
|
+
model: pending.model,
|
|
287
|
+
});
|
|
288
|
+
for (const response of pending.responses) {
|
|
289
|
+
collector.recordProviderResponse({ status: response.status, now: response.occurredAtMs });
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function ensureRun(ctx: ExtensionContext, triggerSource: TriggerSource): void {
|
|
294
|
+
if (collector.hasActiveRun()) return;
|
|
295
|
+
collector.begin({
|
|
296
|
+
id: deps.createId(),
|
|
297
|
+
now: deps.now(),
|
|
298
|
+
triggerSource,
|
|
299
|
+
model: modelIdentity(ctx, pi),
|
|
300
|
+
});
|
|
301
|
+
if (pendingAttemptWithoutRun) {
|
|
302
|
+
pendingAttemptWithoutRun = false;
|
|
303
|
+
collector.beginAttempt();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
async function persistRun(
|
|
308
|
+
run: SettledRun,
|
|
309
|
+
ctx: ExtensionContext,
|
|
310
|
+
generation: number,
|
|
311
|
+
signal: AbortSignal,
|
|
312
|
+
): Promise<void> {
|
|
313
|
+
const activeStore = store;
|
|
314
|
+
if (!activeStore || signal.aborted) return;
|
|
315
|
+
try {
|
|
316
|
+
await activeStore.recordRun(run, signal);
|
|
317
|
+
if (generation !== sessionGeneration || activeStore !== store || signal.aborted) return;
|
|
318
|
+
if (writeFailureActive) {
|
|
319
|
+
writeFailureActive = false;
|
|
320
|
+
safeNotify(ctx, "Local analytics storage recovered.", "info");
|
|
321
|
+
}
|
|
322
|
+
} catch {
|
|
323
|
+
if (generation !== sessionGeneration || activeStore !== store || signal.aborted || writeFailureActive) {
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
writeFailureActive = true;
|
|
327
|
+
safeNotify(ctx, "Analytics could not save this response cycle; its metrics were dropped.", "warning");
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function menuSource(generation: number, signal: AbortSignal): AnalyticsMenuDataSource {
|
|
332
|
+
return {
|
|
333
|
+
path: store?.path ?? path.join(deps.getAgentDir(), STORAGE_DIRECTORY),
|
|
334
|
+
async load(range, actionSignal) {
|
|
335
|
+
assertCurrent(generation, signal);
|
|
336
|
+
const activeStore = store;
|
|
337
|
+
if (!activeStore) {
|
|
338
|
+
return { kind: "unavailable", message: storageFailure ?? unavailableMessage() };
|
|
339
|
+
}
|
|
340
|
+
const snapshot = await activeStore.getSnapshot(range, actionSignal);
|
|
341
|
+
assertCurrent(generation, signal);
|
|
342
|
+
return { kind: "ready", snapshot };
|
|
343
|
+
},
|
|
344
|
+
async clearAll(actionSignal) {
|
|
345
|
+
assertCurrent(generation, signal);
|
|
346
|
+
const activeStore = store;
|
|
347
|
+
if (!activeStore) return { cleanupIncomplete: false };
|
|
348
|
+
return activeStore.clearAll(actionSignal);
|
|
349
|
+
},
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function assertCurrent(generation: number, signal: AbortSignal): void {
|
|
354
|
+
if (generation !== sessionGeneration || signal.aborted) {
|
|
355
|
+
throw new DOMException("Analytics interaction replaced", "AbortError");
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
};
|
|
348
359
|
}
|
|
349
360
|
|
|
350
361
|
function modelIdentity(ctx: ExtensionContext, pi: ExtensionAPI): ModelIdentity | undefined {
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
362
|
+
if (!ctx.model) return undefined;
|
|
363
|
+
return {
|
|
364
|
+
provider: ctx.model.provider,
|
|
365
|
+
model: ctx.model.id,
|
|
366
|
+
thinkingLevel: pi.getThinkingLevel(),
|
|
367
|
+
};
|
|
357
368
|
}
|
|
358
369
|
|
|
359
370
|
export function isBuiltinReadTool(pi: ExtensionAPI): boolean {
|
|
360
|
-
|
|
361
|
-
|
|
371
|
+
const read = pi.getAllTools().find(({ name }) => name === "read");
|
|
372
|
+
return read?.sourceInfo.source === "builtin";
|
|
362
373
|
}
|
|
363
374
|
|
|
364
375
|
function availableSkills(
|
|
365
|
-
|
|
366
|
-
|
|
376
|
+
pi: ExtensionAPI,
|
|
377
|
+
systemSkills: ReadonlyArray<{ name: string; filePath: string }>,
|
|
367
378
|
): Array<{ name: string; filePath: string }> {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
return result;
|
|
379
|
+
const result = [...systemSkills];
|
|
380
|
+
const seen = new Set(result.map(({ name }) => name));
|
|
381
|
+
const getCommands = (pi as ExtensionAPI & { getCommands?: ExtensionAPI["getCommands"] }).getCommands;
|
|
382
|
+
for (const command of typeof getCommands === "function" ? getCommands.call(pi) : []) {
|
|
383
|
+
if (command.source !== "skill" || seen.has(command.name.replace(/^skill:/u, ""))) continue;
|
|
384
|
+
const name = command.name.replace(/^skill:/u, "");
|
|
385
|
+
seen.add(name);
|
|
386
|
+
result.push({ name, filePath: command.sourceInfo.path });
|
|
387
|
+
}
|
|
388
|
+
return result;
|
|
379
389
|
}
|
|
380
390
|
|
|
381
391
|
function unavailableMessage(): string {
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
392
|
+
return [
|
|
393
|
+
"Local analytics storage could not be initialized safely.",
|
|
394
|
+
"Existing files were not replaced.",
|
|
395
|
+
"No analytics are being collected.",
|
|
396
|
+
].join("\n");
|
|
387
397
|
}
|
|
388
398
|
|
|
389
399
|
function safeNotify(ctx: ExtensionContext, message: string, level: "info" | "warning"): void {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
400
|
+
try {
|
|
401
|
+
ctx.ui.notify(message, level);
|
|
402
|
+
} catch {
|
|
403
|
+
// A replaced Pi context cannot receive lifecycle feedback.
|
|
404
|
+
}
|
|
395
405
|
}
|
|
396
406
|
|
|
397
407
|
export default createAnalyticsExtension();
|