@ftrdotdev/pi-tui 0.3.0 → 0.5.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/package.json +1 -1
- package/src/activity.test.ts +281 -0
- package/src/activity.ts +274 -0
- package/src/icons.ts +8 -0
- package/src/index.ts +45 -7
- package/src/state.test.ts +9 -0
- package/src/state.ts +5 -2
- package/src/ui/footer.test.ts +170 -1
- package/src/ui/footer.ts +7 -2
- package/src/ui/header.test.ts +8 -8
- package/src/ui/preview.test.ts +373 -28
- package/src/ui/preview.ts +272 -25
- package/src/ui/render-helpers.ts +57 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ftrdotdev/pi-tui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "House TUI components and the pi interface extension: themed boxes, a pure box-frame model, and the header/footer/editor/settings/preview chrome",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"components",
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { assert, it } from "@effect/vitest";
|
|
2
|
+
import {
|
|
3
|
+
ActivityTracker,
|
|
4
|
+
toTrackerEvent,
|
|
5
|
+
type ActivityEvent,
|
|
6
|
+
type TimeBreakdown,
|
|
7
|
+
type TrackerEventSource,
|
|
8
|
+
} from "./activity.ts";
|
|
9
|
+
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
// ActivityTracker — pure scripted-event activity state machine. Every event
|
|
12
|
+
// carries its own timestamp; the wall clock never lives inside the tracker,
|
|
13
|
+
// so each script yields exact, deterministic bucket totals.
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
function trackerWith(events: ActivityEvent[]): ActivityTracker {
|
|
17
|
+
const tracker = new ActivityTracker();
|
|
18
|
+
for (const event of events) tracker.handle(event);
|
|
19
|
+
return tracker;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
it("charges the open run's inference span to the inference bucket when a run is open", () => {
|
|
23
|
+
// Arrange
|
|
24
|
+
const tracker = trackerWith([
|
|
25
|
+
{ type: "run_start", now: 1000 },
|
|
26
|
+
{ type: "message_start", role: "assistant", now: 1000 },
|
|
27
|
+
{ type: "message_end", role: "assistant", now: 3000 },
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
// Act
|
|
31
|
+
const breakdown = tracker.breakdown(4000);
|
|
32
|
+
|
|
33
|
+
// Assert
|
|
34
|
+
assert.deepStrictEqual(breakdown, { inference: 2000, tool: 0, wait: 0, total: 3000 });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("charges tool spans to the tool bucket, never to inference", () => {
|
|
38
|
+
// Arrange
|
|
39
|
+
const tracker = trackerWith([
|
|
40
|
+
{ type: "run_start", now: 1000 },
|
|
41
|
+
{ type: "tool_start", callId: "a", now: 1000 },
|
|
42
|
+
{ type: "tool_end", callId: "a", now: 2500 },
|
|
43
|
+
{ type: "message_start", role: "assistant", now: 2500 },
|
|
44
|
+
{ type: "message_end", role: "assistant", now: 4500 },
|
|
45
|
+
]);
|
|
46
|
+
|
|
47
|
+
// Act
|
|
48
|
+
const breakdown = tracker.breakdown(4500);
|
|
49
|
+
|
|
50
|
+
// Assert
|
|
51
|
+
assert.deepStrictEqual(breakdown, { inference: 2000, tool: 1500, wait: 0, total: 3500 });
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("charges one shared interval when tool executions overlap (parallel union)", () => {
|
|
55
|
+
// Arrange
|
|
56
|
+
const tracker = trackerWith([
|
|
57
|
+
{ type: "run_start", now: 1000 },
|
|
58
|
+
{ type: "tool_start", callId: "a", now: 1000 },
|
|
59
|
+
{ type: "tool_start", callId: "b", now: 1500 },
|
|
60
|
+
{ type: "tool_end", callId: "b", now: 2500 },
|
|
61
|
+
{ type: "tool_end", callId: "a", now: 3000 },
|
|
62
|
+
]);
|
|
63
|
+
|
|
64
|
+
// Act
|
|
65
|
+
const breakdown = tracker.breakdown(3000);
|
|
66
|
+
|
|
67
|
+
// Assert — the union is t=1000..3000, not the per-call sum 2500.
|
|
68
|
+
assert.strictEqual(breakdown.tool, 2000);
|
|
69
|
+
assert.strictEqual(breakdown.total, 2000);
|
|
70
|
+
assert.strictEqual(breakdown.inference, 0);
|
|
71
|
+
assert.strictEqual(breakdown.wait, 0);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("pauses both work buckets while a user prompt is open and resumes the prior state after", () => {
|
|
75
|
+
// Arrange
|
|
76
|
+
const tracker = trackerWith([
|
|
77
|
+
{ type: "run_start", now: 1000 },
|
|
78
|
+
{ type: "tool_start", callId: "a", now: 1000 },
|
|
79
|
+
{ type: "wait_start", now: 1500 },
|
|
80
|
+
{ type: "wait_end", now: 2500 },
|
|
81
|
+
{ type: "tool_end", callId: "a", now: 3000 },
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
// Act
|
|
85
|
+
const breakdown = tracker.breakdown(3000);
|
|
86
|
+
|
|
87
|
+
// Assert — the mid-tool prompt pauses the tool bucket: 500ms before the
|
|
88
|
+
// wait plus 500ms after it, never the full 1000..3000 span.
|
|
89
|
+
assert.deepStrictEqual(breakdown, { inference: 0, tool: 1000, wait: 1000, total: 2000 });
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("excludes user and toolResult messages from inference", () => {
|
|
93
|
+
// Arrange
|
|
94
|
+
const tracker = trackerWith([
|
|
95
|
+
{ type: "run_start", now: 1000 },
|
|
96
|
+
{ type: "message_start", role: "user", now: 1000 },
|
|
97
|
+
{ type: "message_end", role: "user", now: 2000 },
|
|
98
|
+
{ type: "message_start", role: "toolResult", now: 2000 },
|
|
99
|
+
{ type: "message_end", role: "toolResult", now: 2500 },
|
|
100
|
+
{ type: "message_start", role: "assistant", now: 2500 },
|
|
101
|
+
]);
|
|
102
|
+
|
|
103
|
+
// Act
|
|
104
|
+
const breakdown = tracker.breakdown(3000);
|
|
105
|
+
|
|
106
|
+
// Assert — only the assistant span 2500..3000 counts as inference.
|
|
107
|
+
assert.deepStrictEqual(breakdown, { inference: 500, tool: 0, wait: 0, total: 2000 });
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("keeps inference plus tool plus wait at or below total active time at every instant", () => {
|
|
111
|
+
// Arrange — interleaved parallel tools and a mid-tool user prompt.
|
|
112
|
+
const script: ActivityEvent[] = [
|
|
113
|
+
{ type: "run_start", now: 1000 },
|
|
114
|
+
{ type: "message_start", role: "assistant", now: 1000 },
|
|
115
|
+
{ type: "tool_start", callId: "a", now: 1300 },
|
|
116
|
+
{ type: "tool_start", callId: "b", now: 1700 },
|
|
117
|
+
{ type: "wait_start", now: 1800 },
|
|
118
|
+
{ type: "wait_end", now: 2100 },
|
|
119
|
+
{ type: "tool_end", callId: "b", now: 2400 },
|
|
120
|
+
{ type: "tool_end", callId: "a", now: 2600 },
|
|
121
|
+
{ type: "message_end", role: "assistant", now: 3000 },
|
|
122
|
+
];
|
|
123
|
+
|
|
124
|
+
// Act — query the machine as it stands at each instant: the events up to
|
|
125
|
+
// that instant, then the breakdown read at it.
|
|
126
|
+
const at = (now: number): TimeBreakdown => {
|
|
127
|
+
const partial = new ActivityTracker();
|
|
128
|
+
for (const event of script) {
|
|
129
|
+
if (event.now > now) break;
|
|
130
|
+
partial.handle(event);
|
|
131
|
+
}
|
|
132
|
+
return partial.breakdown(now);
|
|
133
|
+
};
|
|
134
|
+
const observed: TimeBreakdown[] = [1500, 2000, 2700, 3200].map(at);
|
|
135
|
+
|
|
136
|
+
// Assert — buckets are mutually exclusive, so the sum never exceeds the
|
|
137
|
+
// run's wall clock; the difference is the implicit overhead.
|
|
138
|
+
const expected: TimeBreakdown[] = [
|
|
139
|
+
{ inference: 300, tool: 200, wait: 0, total: 500 },
|
|
140
|
+
{ inference: 300, tool: 500, wait: 200, total: 1000 },
|
|
141
|
+
{ inference: 400, tool: 1000, wait: 300, total: 1700 },
|
|
142
|
+
{ inference: 700, tool: 1000, wait: 300, total: 2200 },
|
|
143
|
+
];
|
|
144
|
+
assert.deepStrictEqual(observed, expected);
|
|
145
|
+
for (const b of observed) {
|
|
146
|
+
assert.ok(b.inference + b.tool + b.wait <= b.total);
|
|
147
|
+
assert.strictEqual(b.inference + b.tool + b.wait, b.total - (b.total - b.inference - b.tool - b.wait));
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("folds open spans at run end and reports the frozen split", () => {
|
|
152
|
+
// Arrange — tool still open and message subsumed when the run closes.
|
|
153
|
+
const tracker = trackerWith([
|
|
154
|
+
{ type: "run_start", now: 1000 },
|
|
155
|
+
{ type: "message_start", role: "assistant", now: 1000 },
|
|
156
|
+
{ type: "tool_start", callId: "a", now: 2000 },
|
|
157
|
+
]);
|
|
158
|
+
|
|
159
|
+
// Act
|
|
160
|
+
const frozen = tracker.closeRun(4000);
|
|
161
|
+
|
|
162
|
+
// Assert
|
|
163
|
+
assert.deepStrictEqual(frozen, { inference: 1000, tool: 2000, wait: 0, total: 3000 });
|
|
164
|
+
assert.strictEqual(tracker.isRunOpen(), false);
|
|
165
|
+
// The frozen breakdown persists for the idle render.
|
|
166
|
+
assert.deepStrictEqual(tracker.breakdown(9999), frozen);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// ---------------------------------------------------------------------------
|
|
170
|
+
// Wait parity — the tracker reproduces the numbers behind the previous
|
|
171
|
+
// activeWorkingMs/waitingMs helpers exactly, including the clamp when waits
|
|
172
|
+
// consume the whole runtime.
|
|
173
|
+
// ---------------------------------------------------------------------------
|
|
174
|
+
|
|
175
|
+
it("parity: the tracker reproduces the wait-aware active-work totals", () => {
|
|
176
|
+
// Active work = run wall clock minus waits, matching activeWorkingMs.
|
|
177
|
+
|
|
178
|
+
// No waits: old helper returned 4000 for a run from t=1s at t=5s.
|
|
179
|
+
const noWait = trackerWith([{ type: "run_start", now: 1000 }]);
|
|
180
|
+
assert.strictEqual(noWait.breakdown(5000).total - noWait.breakdown(5000).wait, 4000);
|
|
181
|
+
|
|
182
|
+
// A completed 2s wait: old helper returned 2000.
|
|
183
|
+
const completedWait = trackerWith([
|
|
184
|
+
{ type: "run_start", now: 1000 },
|
|
185
|
+
{ type: "wait_start", now: 2000 },
|
|
186
|
+
{ type: "wait_end", now: 4000 },
|
|
187
|
+
]);
|
|
188
|
+
const completed = completedWait.breakdown(5000);
|
|
189
|
+
assert.strictEqual(completed.total - completed.wait, 2000);
|
|
190
|
+
|
|
191
|
+
// A completed 2s wait plus an in-flight 1s wait: old helper returned 1000.
|
|
192
|
+
const inFlightWait = trackerWith([
|
|
193
|
+
{ type: "run_start", now: 1000 },
|
|
194
|
+
{ type: "wait_start", now: 2000 },
|
|
195
|
+
{ type: "wait_end", now: 4000 },
|
|
196
|
+
{ type: "wait_start", now: 4000 },
|
|
197
|
+
]);
|
|
198
|
+
const inFlight = inFlightWait.breakdown(5000);
|
|
199
|
+
assert.strictEqual(inFlight.total - inFlight.wait, 1000);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("parity: active work clamps to zero when waits consume the runtime", () => {
|
|
203
|
+
// A wait open for the whole run: active work is 0 at any instant, never
|
|
204
|
+
// negative (the old helper clamped `elapsed - waitingAccum` to zero).
|
|
205
|
+
const tracker = trackerWith([
|
|
206
|
+
{ type: "run_start", now: 1000 },
|
|
207
|
+
{ type: "wait_start", now: 1000 },
|
|
208
|
+
]);
|
|
209
|
+
|
|
210
|
+
const mid = tracker.breakdown(3000);
|
|
211
|
+
assert.strictEqual(mid.total - mid.wait, 0);
|
|
212
|
+
|
|
213
|
+
// The run closes folding the open wait; the frozen split is all wait.
|
|
214
|
+
const frozen = tracker.closeRun(5000);
|
|
215
|
+
assert.deepStrictEqual(frozen, { inference: 0, tool: 0, wait: 4000, total: 4000 });
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("parity: waitingMs reports the open wait span and 0 when no prompt is open", () => {
|
|
219
|
+
const idle = trackerWith([{ type: "run_start", now: 1000 }]);
|
|
220
|
+
assert.strictEqual(idle.waitingMs(5000), 0);
|
|
221
|
+
|
|
222
|
+
const waiting = trackerWith([
|
|
223
|
+
{ type: "run_start", now: 1000 },
|
|
224
|
+
{ type: "wait_start", now: 4000 },
|
|
225
|
+
]);
|
|
226
|
+
assert.strictEqual(waiting.waitingMs(5000), 1000);
|
|
227
|
+
assert.strictEqual(waiting.isWaiting(), true);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
// ---------------------------------------------------------------------------
|
|
231
|
+
// toTrackerEvent — maps pi-shaped event fields onto tracker descriptors at
|
|
232
|
+
// the wire boundary. The tracker never imports pi types; index.ts reads the
|
|
233
|
+
// pi event fields and passes a plain object here.
|
|
234
|
+
// ---------------------------------------------------------------------------
|
|
235
|
+
|
|
236
|
+
it("toTrackerEvent maps pi message roles and tool calls onto tracker descriptors", () => {
|
|
237
|
+
// Assistant message_start opens an inference span.
|
|
238
|
+
assert.deepStrictEqual(
|
|
239
|
+
toTrackerEvent({ kind: "message_start", role: "assistant", now: 1000 }),
|
|
240
|
+
{ type: "message_start", role: "assistant", now: 1000 },
|
|
241
|
+
);
|
|
242
|
+
// toolResult message_end never maps to an assistant span.
|
|
243
|
+
assert.strictEqual(
|
|
244
|
+
toTrackerEvent({ kind: "message_end", role: "toolResult", now: 2000 }),
|
|
245
|
+
undefined,
|
|
246
|
+
);
|
|
247
|
+
// User messages never map either.
|
|
248
|
+
assert.strictEqual(
|
|
249
|
+
toTrackerEvent({ kind: "message_start", role: "user", now: 2000 }),
|
|
250
|
+
undefined,
|
|
251
|
+
);
|
|
252
|
+
// Tool executions carry their call id through.
|
|
253
|
+
assert.deepStrictEqual(
|
|
254
|
+
toTrackerEvent({ kind: "tool_execution_start", toolCallId: "call_1", now: 3000 }),
|
|
255
|
+
{ type: "tool_start", callId: "call_1", now: 3000 },
|
|
256
|
+
);
|
|
257
|
+
assert.deepStrictEqual(
|
|
258
|
+
toTrackerEvent({ kind: "tool_execution_end", toolCallId: "call_1", now: 4000 }),
|
|
259
|
+
{ type: "tool_end", callId: "call_1", now: 4000 },
|
|
260
|
+
);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("toTrackerEvent feeds the tracker a live inference/tool script", () => {
|
|
264
|
+
// The full boundary: the wiring opens the run directly (agent_start) and
|
|
265
|
+
// routes message/tool events through the mapper; deterministic buckets out.
|
|
266
|
+
const tracker = new ActivityTracker();
|
|
267
|
+
tracker.handle({ type: "run_start", now: 1000 });
|
|
268
|
+
const script: TrackerEventSource[] = [
|
|
269
|
+
{ kind: "message_start", role: "assistant", now: 1000 },
|
|
270
|
+
{ kind: "tool_execution_start", toolCallId: "call_1", now: 2000 },
|
|
271
|
+
{ kind: "tool_execution_end", toolCallId: "call_1", now: 4000 },
|
|
272
|
+
{ kind: "message_end", role: "assistant", now: 5000 },
|
|
273
|
+
];
|
|
274
|
+
for (const event of script) {
|
|
275
|
+
const mapped = toTrackerEvent(event);
|
|
276
|
+
if (mapped) tracker.handle(mapped);
|
|
277
|
+
}
|
|
278
|
+
const breakdown = tracker.breakdown(6000);
|
|
279
|
+
// tool 2000..4000 = 2s; inference 1000..2000 + 4000..5000 = 2s.
|
|
280
|
+
assert.deepStrictEqual(breakdown, { inference: 2000, tool: 2000, wait: 0, total: 5000 });
|
|
281
|
+
});
|
package/src/activity.ts
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
// Pure scripted-event activity state machine for the footer timer.
|
|
2
|
+
//
|
|
3
|
+
// Every wall-clock instant of an open run is charged to exactly one of four
|
|
4
|
+
// exclusive buckets — inference, tool, waiting, or implicit overhead — and
|
|
5
|
+
// the tracker exposes a deterministic breakdown at any injected `now`.
|
|
6
|
+
// The class mirrors TurnTelemetryTracker (telemetry.ts): the constructor
|
|
7
|
+
// takes no clock, every method takes `now`, and handle() dispatches typed
|
|
8
|
+
// descriptors. Date.now() lives only in the pi event wiring (index.ts).
|
|
9
|
+
|
|
10
|
+
export type MessageRole = "user" | "assistant" | "toolResult";
|
|
11
|
+
|
|
12
|
+
export type ActivityEvent =
|
|
13
|
+
| { type: "run_start" | "run_end"; now: number }
|
|
14
|
+
| { type: "message_start" | "message_end"; role: MessageRole; now: number }
|
|
15
|
+
| { type: "tool_start" | "tool_end"; callId: string; now: number }
|
|
16
|
+
| { type: "wait_start" | "wait_end"; now: number };
|
|
17
|
+
|
|
18
|
+
export interface TimeBreakdown {
|
|
19
|
+
inference: number;
|
|
20
|
+
tool: number;
|
|
21
|
+
wait: number;
|
|
22
|
+
total: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** One of the four exclusive activities; overhead is the residual bucket. */
|
|
26
|
+
type Activity = "inference" | "tool" | "wait" | "overhead";
|
|
27
|
+
|
|
28
|
+
const ZERO_BREAKDOWN: TimeBreakdown = { inference: 0, tool: 0, wait: 0, total: 0 };
|
|
29
|
+
|
|
30
|
+
export class ActivityTracker {
|
|
31
|
+
private runOpen = false;
|
|
32
|
+
private runStart: number | undefined;
|
|
33
|
+
private openMessage = false;
|
|
34
|
+
private readonly openTools = new Set<string>();
|
|
35
|
+
private waitOpen = false;
|
|
36
|
+
private activity: Activity | undefined;
|
|
37
|
+
private openSince: number | undefined;
|
|
38
|
+
private inferenceMs = 0;
|
|
39
|
+
private toolMs = 0;
|
|
40
|
+
private waitMs = 0;
|
|
41
|
+
private frozen: TimeBreakdown | undefined;
|
|
42
|
+
|
|
43
|
+
/** Dispatches a scripted event, charging the outgoing activity's interval. */
|
|
44
|
+
handle(event: ActivityEvent): void {
|
|
45
|
+
switch (event.type) {
|
|
46
|
+
case "run_start":
|
|
47
|
+
this.startRun(event.now);
|
|
48
|
+
return;
|
|
49
|
+
case "run_end":
|
|
50
|
+
this.closeRun(event.now);
|
|
51
|
+
return;
|
|
52
|
+
case "message_start":
|
|
53
|
+
this.startMessage(event.role, event.now);
|
|
54
|
+
return;
|
|
55
|
+
case "message_end":
|
|
56
|
+
this.endMessage(event.role, event.now);
|
|
57
|
+
return;
|
|
58
|
+
case "tool_start":
|
|
59
|
+
this.startTool(event.callId, event.now);
|
|
60
|
+
return;
|
|
61
|
+
case "tool_end":
|
|
62
|
+
this.endTool(event.callId, event.now);
|
|
63
|
+
return;
|
|
64
|
+
case "wait_start":
|
|
65
|
+
this.startWait(event.now);
|
|
66
|
+
return;
|
|
67
|
+
case "wait_end":
|
|
68
|
+
this.endWait(event.now);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** True while an agent run is open. */
|
|
74
|
+
isRunOpen(): boolean {
|
|
75
|
+
return this.runOpen;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** True while a blocking user-facing prompt is open. */
|
|
79
|
+
isWaiting(): boolean {
|
|
80
|
+
return this.waitOpen;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Milliseconds of the open user-wait at `now`, or 0 when no prompt is
|
|
85
|
+
* open (parity with the previous waitingMs helper).
|
|
86
|
+
*/
|
|
87
|
+
waitingMs(now: number): number {
|
|
88
|
+
if (!this.waitOpen || this.activity !== "wait" || this.openSince === undefined) return 0;
|
|
89
|
+
return Math.max(0, now - this.openSince);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The bucket totals at `now`: accumulated charged intervals plus the
|
|
94
|
+
* open activity's span. While no run is open this returns the frozen
|
|
95
|
+
* breakdown from the last closeRun (all zeros before any run).
|
|
96
|
+
*/
|
|
97
|
+
breakdown(now: number): TimeBreakdown {
|
|
98
|
+
if (!this.runOpen) return this.frozen ?? ZERO_BREAKDOWN;
|
|
99
|
+
const total = Math.max(0, now - (this.runStart ?? now));
|
|
100
|
+
return { ...this.snapshots(now), total };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Folds every open span into its accumulator and freezes the run's
|
|
105
|
+
* breakdown, so the idle render can show the split next to the done
|
|
106
|
+
* total. Returns the frozen breakdown.
|
|
107
|
+
*/
|
|
108
|
+
closeRun(now: number): TimeBreakdown {
|
|
109
|
+
this.charge(now);
|
|
110
|
+
if (!this.runOpen) return this.frozen ?? ZERO_BREAKDOWN;
|
|
111
|
+
|
|
112
|
+
// charge() already folded the open span into the accumulators; reading
|
|
113
|
+
// the accumulators directly avoids double-counting it via snapshots().
|
|
114
|
+
const total = Math.max(0, now - (this.runStart ?? now));
|
|
115
|
+
this.frozen = { inference: this.inferenceMs, tool: this.toolMs, wait: this.waitMs, total };
|
|
116
|
+
this.runOpen = false;
|
|
117
|
+
this.runStart = undefined;
|
|
118
|
+
this.activity = undefined;
|
|
119
|
+
this.openSince = undefined;
|
|
120
|
+
this.openMessage = false;
|
|
121
|
+
this.openTools.clear();
|
|
122
|
+
this.waitOpen = false;
|
|
123
|
+
return this.frozen;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private startRun(now: number): void {
|
|
127
|
+
// A run start opens a fresh machine. agent_start overwrites a previous
|
|
128
|
+
// run in the old wiring, so an unconditional reset matches that.
|
|
129
|
+
this.runOpen = true;
|
|
130
|
+
this.runStart = now;
|
|
131
|
+
this.openMessage = false;
|
|
132
|
+
this.openTools.clear();
|
|
133
|
+
this.waitOpen = false;
|
|
134
|
+
this.inferenceMs = 0;
|
|
135
|
+
this.toolMs = 0;
|
|
136
|
+
this.waitMs = 0;
|
|
137
|
+
this.frozen = undefined;
|
|
138
|
+
this.setActivity(now);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private startMessage(role: MessageRole, now: number): void {
|
|
142
|
+
if (!this.runOpen || role !== "assistant") return;
|
|
143
|
+
if (this.openMessage) return; // defensive: already open
|
|
144
|
+
this.charge(now);
|
|
145
|
+
this.openMessage = true;
|
|
146
|
+
this.setActivity(now);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private endMessage(role: MessageRole, now: number): void {
|
|
150
|
+
if (!this.runOpen || role !== "assistant") return;
|
|
151
|
+
if (!this.openMessage) return; // defensive: not open
|
|
152
|
+
this.charge(now);
|
|
153
|
+
this.openMessage = false;
|
|
154
|
+
this.setActivity(now);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private startTool(callId: string, now: number): void {
|
|
158
|
+
if (!this.runOpen) return;
|
|
159
|
+
if (this.openTools.has(callId)) return; // defensive: already running
|
|
160
|
+
this.charge(now);
|
|
161
|
+
this.openTools.add(callId);
|
|
162
|
+
this.setActivity(now);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private endTool(callId: string, now: number): void {
|
|
166
|
+
if (!this.runOpen) return;
|
|
167
|
+
if (!this.openTools.has(callId)) return; // defensive: not running
|
|
168
|
+
this.charge(now);
|
|
169
|
+
this.openTools.delete(callId);
|
|
170
|
+
this.setActivity(now);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private startWait(now: number): void {
|
|
174
|
+
if (!this.runOpen || this.waitOpen) return; // defensive: nested prompts
|
|
175
|
+
this.charge(now);
|
|
176
|
+
this.waitOpen = true;
|
|
177
|
+
this.setActivity(now);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private endWait(now: number): void {
|
|
181
|
+
if (!this.runOpen || !this.waitOpen) return;
|
|
182
|
+
this.charge(now);
|
|
183
|
+
this.waitOpen = false;
|
|
184
|
+
this.setActivity(now);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Charge the outgoing activity's open interval into its accumulator. */
|
|
188
|
+
private charge(now: number): void {
|
|
189
|
+
if (this.activity === undefined || this.openSince === undefined) return;
|
|
190
|
+
const delta = Math.max(0, now - this.openSince);
|
|
191
|
+
switch (this.activity) {
|
|
192
|
+
case "inference":
|
|
193
|
+
this.inferenceMs += delta;
|
|
194
|
+
break;
|
|
195
|
+
case "tool":
|
|
196
|
+
this.toolMs += delta;
|
|
197
|
+
break;
|
|
198
|
+
case "wait":
|
|
199
|
+
this.waitMs += delta;
|
|
200
|
+
break;
|
|
201
|
+
case "overhead":
|
|
202
|
+
break; // implicit residual; no accumulator
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The next activity is a pure function of the open counts; priority is
|
|
208
|
+
* waiting > tool > inference, with overhead as the residual.
|
|
209
|
+
*/
|
|
210
|
+
private setActivity(now: number): void {
|
|
211
|
+
let next: Activity;
|
|
212
|
+
if (!this.runOpen) next = "overhead";
|
|
213
|
+
else if (this.waitOpen) next = "wait";
|
|
214
|
+
else if (this.openTools.size > 0) next = "tool";
|
|
215
|
+
else if (this.openMessage) next = "inference";
|
|
216
|
+
else next = "overhead";
|
|
217
|
+
this.activity = next;
|
|
218
|
+
this.openSince = now;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Accumulators plus the open activity's span at `now`. */
|
|
222
|
+
private snapshots(now: number): { inference: number; tool: number; wait: number } {
|
|
223
|
+
let inference = this.inferenceMs;
|
|
224
|
+
let tool = this.toolMs;
|
|
225
|
+
let wait = this.waitMs;
|
|
226
|
+
if (this.activity === "inference" && this.openSince !== undefined) {
|
|
227
|
+
inference += Math.max(0, now - this.openSince);
|
|
228
|
+
} else if (this.activity === "tool" && this.openSince !== undefined) {
|
|
229
|
+
tool += Math.max(0, now - this.openSince);
|
|
230
|
+
} else if (this.activity === "wait" && this.openSince !== undefined) {
|
|
231
|
+
wait += Math.max(0, now - this.openSince);
|
|
232
|
+
}
|
|
233
|
+
return { inference, tool, wait };
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// ---------------------------------------------------------------------------
|
|
238
|
+
// toTrackerEvent — the pi-to-tracker seam. index.ts reads pi event fields
|
|
239
|
+
// (message.role, toolCallId) and passes a plain object; the tracker module
|
|
240
|
+
// never imports pi types (mock boundary rule). Only assistant messages map
|
|
241
|
+
// to tracker descriptors; user and toolResult messages return undefined and
|
|
242
|
+
// are dropped by the wiring.
|
|
243
|
+
// ---------------------------------------------------------------------------
|
|
244
|
+
|
|
245
|
+
export type TrackerEventSource =
|
|
246
|
+
// role accepts any string: pi message roles include custom app roles beyond
|
|
247
|
+
// the tracker vocabulary (user/assistant/toolResult); only `assistant`
|
|
248
|
+
// maps, so the boundary needs no knowledge of pi's full union. Each kind
|
|
249
|
+
// is a single literal so TS narrows the discriminated union exactly.
|
|
250
|
+
| { kind: "message_start"; role: string; now: number }
|
|
251
|
+
| { kind: "message_end"; role: string; now: number }
|
|
252
|
+
| { kind: "tool_execution_start"; toolCallId: string; now: number }
|
|
253
|
+
| { kind: "tool_execution_end"; toolCallId: string; now: number };
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Translates a pi-shaped event field set into an `ActivityEvent`, or
|
|
257
|
+
* `undefined` when the event must not charge a tracker bucket (every
|
|
258
|
+
* non-assistant message). The call id passes through to pair parallel tool
|
|
259
|
+
* executions per call.
|
|
260
|
+
*/
|
|
261
|
+
export function toTrackerEvent(source: TrackerEventSource): ActivityEvent | undefined {
|
|
262
|
+
if (source.kind === "message_start" || source.kind === "message_end") {
|
|
263
|
+
// toolResult messages would otherwise pair as an assistant span; the
|
|
264
|
+
// tracker already ignores non-assistant roles, but the boundary kills
|
|
265
|
+
// them before they reach the machine.
|
|
266
|
+
if (source.role !== "assistant") return undefined;
|
|
267
|
+
return { type: source.kind, role: "assistant", now: source.now };
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
type: source.kind === "tool_execution_start" ? "tool_start" : "tool_end",
|
|
271
|
+
callId: source.toolCallId,
|
|
272
|
+
now: source.now,
|
|
273
|
+
};
|
|
274
|
+
}
|
package/src/icons.ts
CHANGED
|
@@ -8,6 +8,10 @@ export interface IconGlyphs {
|
|
|
8
8
|
context: string;
|
|
9
9
|
model: string;
|
|
10
10
|
thinking: string;
|
|
11
|
+
/** Live split: model-inference bucket glyph. */
|
|
12
|
+
inference: string;
|
|
13
|
+
/** Live split: tool-execution bucket glyph. */
|
|
14
|
+
tool: string;
|
|
11
15
|
input: string;
|
|
12
16
|
output: string;
|
|
13
17
|
cacheHit: string;
|
|
@@ -36,6 +40,8 @@ const NERD_GLYPHS: IconGlyphs = {
|
|
|
36
40
|
context: "",
|
|
37
41
|
model: "",
|
|
38
42
|
thinking: "",
|
|
43
|
+
inference: "\uF0EB", // same glyph as thinking: the model is generating
|
|
44
|
+
tool: "\u2699", // ⚙ gear: tool execution
|
|
39
45
|
// client network view: input = upload to API, output = download from API
|
|
40
46
|
input: "",
|
|
41
47
|
output: "",
|
|
@@ -67,6 +73,8 @@ const ASCII_GLYPHS: IconGlyphs = {
|
|
|
67
73
|
context: "%",
|
|
68
74
|
model: "M",
|
|
69
75
|
thinking: "~",
|
|
76
|
+
inference: "~", // same glyph as thinking: the model is generating
|
|
77
|
+
tool: ">", // outside the git-status collision set {= S ! A ? r x ^ v}
|
|
70
78
|
input: "↑",
|
|
71
79
|
output: "↓",
|
|
72
80
|
cacheHit: "c",
|