@adeildo/pi-look 5.15.0 → 6.0.1
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 +54 -44
- package/assets/preview.png +0 -0
- package/package.json +3 -2
- package/src/data/git.ts +1 -1
- package/src/data/github.ts +6 -17
- package/src/data/runner.ts +3 -4
- package/src/data/snapshot.ts +24 -13
- package/src/data/start-notes.ts +2 -6
- package/src/data/telemetry.ts +13 -16
- package/src/data/totals.ts +14 -73
- package/src/data/visits.ts +2 -3
- package/src/index.ts +66 -18
- package/src/render/diff.ts +104 -0
- package/src/render/fit.ts +1 -1
- package/src/render/format.ts +1 -2
- package/src/render/glyphs.ts +23 -10
- package/src/render/paint.ts +4 -4
- package/src/render/segments.ts +112 -33
- package/src/render/streaming.ts +17 -40
- package/src/render/tools.ts +4 -6
- package/src/settings.ts +5 -7
- package/src/ui/editor.ts +11 -8
- package/src/ui/footer.ts +4 -11
- package/src/ui/header.ts +25 -18
- package/src/ui/outputs.ts +5 -11
- package/src/ui/pi-entries.ts +29 -27
- package/src/ui/previews.ts +2 -6
- package/src/ui/queue.ts +8 -8
- package/src/ui/screen.ts +1 -8
- package/src/ui/tool-frame.ts +84 -91
- package/src/view/state.ts +97 -0
- package/src/view/transcript.ts +175 -0
- package/src/view/view.ts +161 -0
package/src/ui/tool-frame.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
//
|
|
2
|
-
// its result inside, and how it went on the bottom rule.
|
|
1
|
+
// Every tool call in its own frame: name on the top rule, call and result inside, how it went on the bottom rule.
|
|
3
2
|
import {
|
|
4
3
|
CUT,
|
|
5
4
|
type CallRuling,
|
|
5
|
+
isObject,
|
|
6
6
|
type FeatureScope,
|
|
7
7
|
CALLS_FRAMED,
|
|
8
8
|
RULING,
|
|
@@ -20,17 +20,18 @@ import {
|
|
|
20
20
|
visibleWidth,
|
|
21
21
|
} from "@earendil-works/pi-tui";
|
|
22
22
|
|
|
23
|
+
import { editBody } from "../render/diff.ts";
|
|
23
24
|
import { tabs } from "../render/format.ts";
|
|
24
25
|
import { type IconSet, glyphsOf } from "../render/glyphs.ts";
|
|
25
|
-
import { type Stream, STREAMERS } from "../render/streaming.ts";
|
|
26
|
-
import { stateMark, toolIcon, toolLabel } from "../render/tools.ts";
|
|
26
|
+
import { pathLine, type Stream, STREAMERS } from "../render/streaming.ts";
|
|
27
|
+
import { type CallPhase, stateMark, toolIcon, toolLabel } from "../render/tools.ts";
|
|
27
28
|
import { icons } from "../settings.ts";
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
type Phase = "writing" | "waiting" | "running" | "done" | "failed";
|
|
30
|
+
type Phase = CallPhase | "interrupted";
|
|
31
31
|
|
|
32
32
|
const PHASE_COLORS = {
|
|
33
33
|
writing: "muted",
|
|
34
|
+
interrupted: "muted",
|
|
34
35
|
waiting: "accent",
|
|
35
36
|
running: "accent",
|
|
36
37
|
done: "success",
|
|
@@ -38,23 +39,25 @@ const PHASE_COLORS = {
|
|
|
38
39
|
} as const;
|
|
39
40
|
const BODY_PAD = 4;
|
|
40
41
|
const COLLAPSED_ARGS_CHARS = 100;
|
|
41
|
-
/** How many lines of a result the frame prints before telling you the rest is there. */
|
|
42
42
|
const RESULT_PREVIEW_LINES = 10;
|
|
43
43
|
|
|
44
44
|
export function registerToolFrames(scope: FeatureScope): void {
|
|
45
45
|
const redraws = new Map<string, () => void>();
|
|
46
46
|
const written = new Map<string, number>();
|
|
47
|
-
|
|
47
|
+
const streamed = new Set<string>();
|
|
48
|
+
// Asked for on every drawing, and it changes only with the setting.
|
|
48
49
|
const set = glyphsOf(() => icons.get(scope));
|
|
49
50
|
CALLS_FRAMED.provide(scope, () => true);
|
|
50
51
|
RULING.onChanged(scope, (_ruling, toolCallId) => redraws.get(toolCallId)?.());
|
|
51
|
-
// Pi completes
|
|
52
|
-
// before writing the next one would go on writing. The provider says when each one ends. Pi tells
|
|
53
|
-
// the extensions first and hands every call its arguments right after, which draws the frame.
|
|
52
|
+
// Pi completes a message's calls only when the message ends; the provider says when each one ends.
|
|
54
53
|
scope.on("message_update", (event) => {
|
|
55
54
|
const update = event.assistantMessageEvent;
|
|
56
55
|
if (update.type === "toolcall_end") written.set(update.toolCall.id, Date.now());
|
|
56
|
+
if (event.message.role !== "assistant") return;
|
|
57
|
+
for (const block of event.message.content)
|
|
58
|
+
if (block.type === "toolCall") streamed.add(block.id);
|
|
57
59
|
});
|
|
60
|
+
scope.on("message_end", () => streamed.clear());
|
|
58
61
|
|
|
59
62
|
scope.registerToolRenderer((toolName, next) =>
|
|
60
63
|
framed(scope, next(), {
|
|
@@ -62,6 +65,7 @@ export function registerToolFrames(scope: FeatureScope): void {
|
|
|
62
65
|
set: () => set().set,
|
|
63
66
|
redraws,
|
|
64
67
|
written,
|
|
68
|
+
streamed,
|
|
65
69
|
}),
|
|
66
70
|
);
|
|
67
71
|
}
|
|
@@ -70,8 +74,8 @@ interface FrameContext {
|
|
|
70
74
|
name: string;
|
|
71
75
|
set: () => IconSet;
|
|
72
76
|
redraws: Map<string, () => void>;
|
|
73
|
-
/** When the model finished writing each call, until its frame reads it. */
|
|
74
77
|
written: Map<string, number>;
|
|
78
|
+
streamed: ReadonlySet<string>;
|
|
75
79
|
}
|
|
76
80
|
|
|
77
81
|
function framed(
|
|
@@ -79,14 +83,11 @@ function framed(
|
|
|
79
83
|
base: ToolRenderers | undefined,
|
|
80
84
|
frame: FrameContext,
|
|
81
85
|
): ToolRenderers {
|
|
82
|
-
//
|
|
83
|
-
// exists, and the arguments or the text of the result stand in for the rest.
|
|
86
|
+
// The frame wraps whichever half a tool brings a renderer for.
|
|
84
87
|
const call = base?.renderCall ?? genericCall;
|
|
85
88
|
const result = base?.renderResult ?? genericResult;
|
|
86
89
|
|
|
87
|
-
//
|
|
88
|
-
// frame, so it gets that one back instead of the frame. A frame that showed the stream instead
|
|
89
|
-
// holds no component of pi's.
|
|
90
|
+
// Pi's renderers mutate the component they returned last time, so they get that one back, not the frame.
|
|
90
91
|
const inner = new WeakMap<Component, Component | undefined>();
|
|
91
92
|
const passed = (last: Component | undefined): Component | undefined =>
|
|
92
93
|
last === undefined || !inner.has(last) ? last : inner.get(last);
|
|
@@ -94,7 +95,6 @@ function framed(
|
|
|
94
95
|
|
|
95
96
|
return {
|
|
96
97
|
...base,
|
|
97
|
-
// The frame is the shell, so pi does not wrap it in a box of its own.
|
|
98
98
|
renderShell: "self",
|
|
99
99
|
renderCall(args, theme, context) {
|
|
100
100
|
frame.redraws.set(context.toolCallId, context.invalidate);
|
|
@@ -102,26 +102,27 @@ function framed(
|
|
|
102
102
|
const now = Date.now();
|
|
103
103
|
const ended = frame.written.get(context.toolCallId);
|
|
104
104
|
frame.written.delete(context.toolCallId);
|
|
105
|
-
// A call replayed
|
|
105
|
+
// A call replayed or already running was written long ago.
|
|
106
106
|
const done =
|
|
107
107
|
ended !== undefined ||
|
|
108
108
|
context.argsComplete ||
|
|
109
109
|
context.executionStarted ||
|
|
110
110
|
!context.isPartial;
|
|
111
|
-
//
|
|
112
|
-
|
|
113
|
-
|
|
111
|
+
// Pi replays a call that has no result as if the model were still writing it.
|
|
112
|
+
const interrupted = !done && !frame.streamed.has(context.toolCallId);
|
|
113
|
+
if (done) clock.writtenAt ??= ended ?? now;
|
|
114
|
+
else if (!interrupted) clock.writingAt ??= now;
|
|
114
115
|
if (context.executionStarted) clock.startedAt ??= now;
|
|
115
|
-
const writing = clock.writtenAt === undefined;
|
|
116
|
+
const writing = !interrupted && clock.writtenAt === undefined;
|
|
116
117
|
const ruling = RULING.current(scope, context.toolCallId);
|
|
117
118
|
|
|
118
|
-
// Pi hands every call
|
|
119
|
-
// call that did not change keeps the frame it has, and its renderer is not asked again.
|
|
119
|
+
// Pi hands every call its arguments again on each piece the model writes, so an unchanged call keeps its frame.
|
|
120
120
|
const inputs = [
|
|
121
121
|
args,
|
|
122
122
|
theme,
|
|
123
123
|
ruling,
|
|
124
124
|
writing,
|
|
125
|
+
interrupted,
|
|
125
126
|
context.expanded,
|
|
126
127
|
context.isPartial,
|
|
127
128
|
context.executionStarted,
|
|
@@ -135,8 +136,7 @@ function framed(
|
|
|
135
136
|
writing,
|
|
136
137
|
state: context.state as object,
|
|
137
138
|
});
|
|
138
|
-
// While the model writes, pi's renderer has nothing new to show
|
|
139
|
-
// frame pads its inside itself, so pi's renderer adds no padding of its own.
|
|
139
|
+
// While the model writes, pi's renderer has nothing new to show. The frame pads its inside itself.
|
|
140
140
|
const streaming = writing && stream !== undefined;
|
|
141
141
|
const own = streaming
|
|
142
142
|
? undefined
|
|
@@ -146,7 +146,13 @@ function framed(
|
|
|
146
146
|
outputPad: 0,
|
|
147
147
|
lastComponent: passed(last),
|
|
148
148
|
});
|
|
149
|
-
|
|
149
|
+
// Pi paints its edit on a colored box; the frame draws the diff itself, in the diff colors.
|
|
150
|
+
const drawnOwn = (component: Component): Component =>
|
|
151
|
+
frame.name === "edit" && isObject(args)
|
|
152
|
+
? editBody(component, pathLine("edit", pathOf(args), theme), theme)
|
|
153
|
+
: component;
|
|
154
|
+
const body =
|
|
155
|
+
own === undefined ? drawnBody(stream?.lines ?? []) : orStream(drawnOwn(own), stream);
|
|
150
156
|
|
|
151
157
|
const box = new CallFrame(
|
|
152
158
|
{
|
|
@@ -156,7 +162,7 @@ function framed(
|
|
|
156
162
|
body,
|
|
157
163
|
ruling,
|
|
158
164
|
expanded: context.expanded,
|
|
159
|
-
phase: phaseOf(context, writing),
|
|
165
|
+
phase: interrupted ? "interrupted" : phaseOf(context, writing),
|
|
160
166
|
mark: stateMark,
|
|
161
167
|
set: frame.set,
|
|
162
168
|
progress: writing ? stream?.progress : undefined,
|
|
@@ -171,9 +177,9 @@ function framed(
|
|
|
171
177
|
},
|
|
172
178
|
renderResult(shown, options, theme, context) {
|
|
173
179
|
const clock = clockOf(context);
|
|
174
|
-
// A blocked call prints
|
|
180
|
+
// A blocked call prints only the reason the line above already gives.
|
|
175
181
|
const blocked = RULING.current(scope, context.toolCallId)?.blocked === true;
|
|
176
|
-
// Pi draws
|
|
182
|
+
// Pi draws a result's pictures right below the frame, so the frame counts them.
|
|
177
183
|
const images = shown.content.filter((block) => block.type === "image").length;
|
|
178
184
|
if (!options.isPartial) clock.endedAt ??= Date.now();
|
|
179
185
|
clock.resulted = true;
|
|
@@ -199,25 +205,17 @@ function framed(
|
|
|
199
205
|
};
|
|
200
206
|
}
|
|
201
207
|
|
|
202
|
-
/*
|
|
203
|
-
* The stand-in for a tool that brings no renderer of its own: the arguments, under the top rule
|
|
204
|
-
* that already names the tool, and the text of the result.
|
|
205
|
-
*/
|
|
206
208
|
const genericCall: NonNullable<ToolRenderers["renderCall"]> = (args, theme, context) =>
|
|
207
209
|
new Text(callText(args, theme, context.expanded), 0, 0);
|
|
208
210
|
|
|
209
211
|
const genericResult: NonNullable<ToolRenderers["renderResult"]> = (result, options, theme) =>
|
|
210
212
|
new Text(resultText(result, options.expanded, theme), 0, 0);
|
|
211
213
|
|
|
212
|
-
/** What a running call is doing, for tools whose running means something else. */
|
|
213
214
|
const RUNNING_WORDS: Readonly<Record<string, string>> = {
|
|
214
215
|
ask_questions: "waiting for your answer",
|
|
215
216
|
};
|
|
216
217
|
|
|
217
|
-
/**
|
|
218
|
-
* The phase the call closes with while it still has no result: writing the arguments, then waiting
|
|
219
|
-
* to run. A call with a result, live or replayed, stays open for the result half to close.
|
|
220
|
-
*/
|
|
218
|
+
/** Without a result: writing the arguments, then waiting to run. */
|
|
221
219
|
function phaseOf(
|
|
222
220
|
context: { isPartial: boolean; executionStarted: boolean },
|
|
223
221
|
writing: boolean,
|
|
@@ -282,10 +280,9 @@ interface FrameParts {
|
|
|
282
280
|
body: Component;
|
|
283
281
|
}
|
|
284
282
|
|
|
285
|
-
/** The top rule, the call inside it, and the phase while the call has nothing to show yet. */
|
|
286
283
|
class CallFrame implements Component {
|
|
287
284
|
#drawn?: Drawn;
|
|
288
|
-
/** Set when pi or the terminal asks for a fresh drawing
|
|
285
|
+
/** Set when pi or the terminal asks for a fresh drawing. */
|
|
289
286
|
#stale = false;
|
|
290
287
|
|
|
291
288
|
constructor(
|
|
@@ -293,7 +290,6 @@ class CallFrame implements Component {
|
|
|
293
290
|
private readonly inputs: readonly unknown[],
|
|
294
291
|
) {}
|
|
295
292
|
|
|
296
|
-
/** Whether this frame was made from these inputs and nothing asked for it to be made again. */
|
|
297
293
|
drawnFrom(inputs: readonly unknown[]): boolean {
|
|
298
294
|
return (
|
|
299
295
|
!this.#stale &&
|
|
@@ -310,14 +306,12 @@ class CallFrame implements Component {
|
|
|
310
306
|
|
|
311
307
|
render(width: number): string[] {
|
|
312
308
|
const { theme, icon, label, ruling, expanded, clock } = this.parts;
|
|
313
|
-
// A running call closes too until its result draws the rest of the frame
|
|
314
|
-
// waits for you does while its dialog is open.
|
|
309
|
+
// A running call closes too until its result draws the rest of the frame.
|
|
315
310
|
const running = this.parts.executing === true && clock?.resulted !== true;
|
|
316
311
|
const phase = this.parts.phase ?? (running ? "running" : undefined);
|
|
317
|
-
//
|
|
312
|
+
// Read on every drawing, so the time goes on while nothing new arrives.
|
|
318
313
|
const time = clock === undefined || phase === undefined ? undefined : timeOf(phase, clock);
|
|
319
|
-
|
|
320
|
-
const mark = phase === undefined ? undefined : this.parts.mark(phase, this.parts.set());
|
|
314
|
+
const mark = phase === undefined ? undefined : this.parts.mark(markOf(phase), this.parts.set());
|
|
321
315
|
const body = this.parts.body.render(room(width));
|
|
322
316
|
const progress = this.parts.progress?.join("\u0000");
|
|
323
317
|
const drawn = { width, body, rest: [ruling, expanded, phase, time, progress, mark] };
|
|
@@ -325,10 +319,9 @@ class CallFrame implements Component {
|
|
|
325
319
|
|
|
326
320
|
const title = `${theme.fg("toolTitle", icon)} ${theme.fg("toolTitle", theme.bold(label))}`;
|
|
327
321
|
const lines = [rule("top", title, width, theme), ...inside(withoutBlanks(body), width, theme)];
|
|
328
|
-
// The decision is about the call, so it stays with the call.
|
|
329
322
|
if (ruling !== undefined)
|
|
330
323
|
lines.push(...inside([rulingText(ruling, theme, expanded)], width, theme));
|
|
331
|
-
//
|
|
324
|
+
// With nothing else to show yet, the frame closes with what the call is doing.
|
|
332
325
|
if (phase !== undefined && mark !== undefined) {
|
|
333
326
|
const times = [time, ...(this.parts.progress ?? [])];
|
|
334
327
|
const word = phase === "running" ? this.parts.runningAs : undefined;
|
|
@@ -346,38 +339,32 @@ interface CallParts {
|
|
|
346
339
|
ruling?: CallRuling;
|
|
347
340
|
expanded: boolean;
|
|
348
341
|
phase?: Phase;
|
|
349
|
-
mark: (phase:
|
|
342
|
+
mark: (phase: CallPhase, set: IconSet) => string;
|
|
350
343
|
set: () => IconSet;
|
|
351
|
-
/** What the model has written so far, while it writes. */
|
|
352
344
|
progress?: readonly string[];
|
|
353
345
|
clock?: Clock;
|
|
354
|
-
/**
|
|
346
|
+
/** It runs and its result is not final. */
|
|
355
347
|
executing?: boolean;
|
|
356
|
-
/**
|
|
348
|
+
/** When the tool's running means something else. */
|
|
357
349
|
runningAs?: string;
|
|
358
350
|
}
|
|
359
351
|
|
|
360
|
-
/** Lines that are already drawn. The frame cuts the ones too wide for it. */
|
|
361
352
|
function drawnBody(drawn: readonly string[]): Component {
|
|
362
353
|
return { render: () => [...drawn], invalidate: () => {} };
|
|
363
354
|
}
|
|
364
355
|
|
|
365
|
-
/**
|
|
366
|
-
* Pi's own body, or the stream while pi's body is only its first line, as when an edit's diff is
|
|
367
|
-
* still being read from the file after the model finished writing.
|
|
368
|
-
*/
|
|
356
|
+
/** The stream while pi's body is still only its first line. */
|
|
369
357
|
function orStream(own: Component, stream: Stream | undefined): Component {
|
|
370
358
|
if (stream === undefined) return own;
|
|
371
359
|
return {
|
|
372
360
|
render(width) {
|
|
373
361
|
const drawn = own.render(width);
|
|
374
|
-
return
|
|
362
|
+
return hasLines(drawn, 2) ? drawn : [...stream.lines];
|
|
375
363
|
},
|
|
376
364
|
invalidate: () => own.invalidate(),
|
|
377
365
|
};
|
|
378
366
|
}
|
|
379
367
|
|
|
380
|
-
/** The result, behind a cut, and the bottom rule with how it went and how long it took. */
|
|
381
368
|
class ResultFrame implements Component {
|
|
382
369
|
#drawn?: Drawn;
|
|
383
370
|
|
|
@@ -391,7 +378,7 @@ class ResultFrame implements Component {
|
|
|
391
378
|
render(width: number): string[] {
|
|
392
379
|
const { theme, phase, wrote, quiet, images } = this.parts;
|
|
393
380
|
const time = this.parts.elapsed;
|
|
394
|
-
const mark = stateMark(phase, this.parts.set());
|
|
381
|
+
const mark = stateMark(markOf(phase), this.parts.set());
|
|
395
382
|
const body = this.parts.body.render(room(width));
|
|
396
383
|
const drawn = { width, body, rest: [phase, mark, time, wrote, images, quiet] };
|
|
397
384
|
if (unchanged(this.#drawn, drawn)) return this.#drawn.lines;
|
|
@@ -399,7 +386,7 @@ class ResultFrame implements Component {
|
|
|
399
386
|
const printed =
|
|
400
387
|
quiet === true ? [] : withoutBlanks(dropTime(body)).map((line) => hidden(line, theme));
|
|
401
388
|
const lines = [
|
|
402
|
-
//
|
|
389
|
+
// A call with nothing to print has no cut.
|
|
403
390
|
...(printed.length === 0 ? [] : [cut(width, theme), ...inside(printed, width, theme)]),
|
|
404
391
|
rule(
|
|
405
392
|
"bottom",
|
|
@@ -423,14 +410,13 @@ interface ResultParts {
|
|
|
423
410
|
set: () => IconSet;
|
|
424
411
|
elapsed?: string;
|
|
425
412
|
wrote?: string;
|
|
426
|
-
/**
|
|
413
|
+
/** Pi draws them outside the frame. */
|
|
427
414
|
images?: string;
|
|
428
|
-
/** Nothing worth printing
|
|
415
|
+
/** Nothing worth printing, like a call the gate blocked. */
|
|
429
416
|
quiet?: boolean;
|
|
430
417
|
}
|
|
431
418
|
|
|
432
|
-
|
|
433
|
-
export function rule(
|
|
419
|
+
function rule(
|
|
434
420
|
edge: "top" | "bottom",
|
|
435
421
|
left: string,
|
|
436
422
|
width: number,
|
|
@@ -444,20 +430,16 @@ export function rule(
|
|
|
444
430
|
const fill = Math.max(1, width - BODY_PAD - visibleWidth(cell));
|
|
445
431
|
const line =
|
|
446
432
|
paint(`${start}${LINE.h}`) + cell + paint(LINE.h.repeat(fill)) + paint(`${LINE.h}${end}`);
|
|
447
|
-
// A rule with room to fill is the width already; only a label that filled the row needs cutting.
|
|
448
433
|
return fill === 1 ? truncateToWidth(line, width, "") : line;
|
|
449
434
|
}
|
|
450
435
|
|
|
451
|
-
/** The line that ends the call and starts what it printed. */
|
|
452
436
|
function cut(width: number, theme: Theme): string {
|
|
453
437
|
const paint = (text: string) => theme.fg("border", text);
|
|
454
438
|
const fill = Math.max(1, width - BODY_PAD);
|
|
455
439
|
const line = `${paint(LINE.teeLeft)}${paint(LINE.h.repeat(fill + 2))}${paint(LINE.teeRight)}`;
|
|
456
|
-
// Room for the fill means the cut is the width already.
|
|
457
440
|
return fill === 1 ? truncateToWidth(line, width, "") : line;
|
|
458
441
|
}
|
|
459
442
|
|
|
460
|
-
/** The bottom rule: what the call is doing, how long it ran, and how long the model took to write it. */
|
|
461
443
|
function phaseLabel(
|
|
462
444
|
theme: Theme,
|
|
463
445
|
mark: string,
|
|
@@ -475,9 +457,9 @@ function inside(lines: readonly string[], width: number, theme: Theme): string[]
|
|
|
475
457
|
const paint = (text: string) => theme.fg("border", text);
|
|
476
458
|
const inner = room(width);
|
|
477
459
|
return trim(lines).map((line) => {
|
|
478
|
-
|
|
479
|
-
const text =
|
|
480
|
-
const pad = " ".repeat(Math.max(0, inner - visibleWidth(text)));
|
|
460
|
+
const wide = visibleWidth(line);
|
|
461
|
+
const text = wide > inner ? truncateToWidth(line, inner, CUT, true) : line;
|
|
462
|
+
const pad = " ".repeat(Math.max(0, inner - (wide > inner ? visibleWidth(text) : wide)));
|
|
481
463
|
return `${paint(LINE.v)} ${text}${pad} ${paint(LINE.v)}`;
|
|
482
464
|
});
|
|
483
465
|
}
|
|
@@ -485,18 +467,17 @@ function inside(lines: readonly string[], width: number, theme: Theme): string[]
|
|
|
485
467
|
interface Drawn {
|
|
486
468
|
width: number;
|
|
487
469
|
body: readonly string[];
|
|
488
|
-
/** What the frame drew around the body: the ruling, the phase, the times. */
|
|
489
470
|
rest: readonly unknown[];
|
|
490
471
|
lines: string[];
|
|
491
472
|
}
|
|
492
473
|
|
|
493
|
-
/**
|
|
474
|
+
/** Keeps typing cheap. */
|
|
494
475
|
function unchanged(drawn: Drawn | undefined, next: Omit<Drawn, "lines">): drawn is Drawn {
|
|
495
476
|
if (drawn === undefined || drawn.width !== next.width) return false;
|
|
496
477
|
if (drawn.body.length !== next.body.length || drawn.rest.length !== next.rest.length)
|
|
497
478
|
return false;
|
|
498
479
|
if (drawn.rest.some((part, index) => part !== next.rest[index])) return false;
|
|
499
|
-
// A renderer that kept its lines hands back the same array
|
|
480
|
+
// A renderer that kept its lines hands back the same array.
|
|
500
481
|
return drawn.body === next.body || drawn.body.every((line, index) => line === next.body[index]);
|
|
501
482
|
}
|
|
502
483
|
|
|
@@ -504,11 +485,11 @@ function room(width: number): number {
|
|
|
504
485
|
return Math.max(1, width - BODY_PAD);
|
|
505
486
|
}
|
|
506
487
|
|
|
507
|
-
/**
|
|
488
|
+
/** As `... (240 earlier lines, ctrl+o to expand)`. */
|
|
508
489
|
const HIDDEN = /^\.\.\. \((\d+) (earlier|more) lines, (.+?) to expand\)$/u;
|
|
509
490
|
|
|
510
|
-
/** Pi's note on cut lines, in the frame's words: which way they are, how many, and the key. */
|
|
511
491
|
export function hidden(line: string, theme: Theme): string {
|
|
492
|
+
if (!line.includes(" to expand)")) return line;
|
|
512
493
|
const match = HIDDEN.exec(stripTerminalSequences(line).trim());
|
|
513
494
|
if (match === null) return line;
|
|
514
495
|
const [, count, side, key] = match;
|
|
@@ -517,7 +498,7 @@ export function hidden(line: string, theme: Theme): string {
|
|
|
517
498
|
return `${theme.fg("dim", GLYPH.ellipsisUp)} ${lines} ${theme.fg("dim", key ?? "")}`;
|
|
518
499
|
}
|
|
519
500
|
|
|
520
|
-
/**
|
|
501
|
+
/** The frame says the time on the bottom rule instead. */
|
|
521
502
|
function dropTime(lines: readonly string[]): string[] {
|
|
522
503
|
const at = lines.findLastIndex((line) => !isBlank(line));
|
|
523
504
|
if (at === -1) return [...lines];
|
|
@@ -527,22 +508,18 @@ function dropTime(lines: readonly string[]): string[] {
|
|
|
527
508
|
}
|
|
528
509
|
|
|
529
510
|
interface Clock {
|
|
530
|
-
/** When the model started writing the call, and when it finished. */
|
|
531
511
|
writingAt?: number;
|
|
532
512
|
writtenAt?: number;
|
|
533
|
-
/** When the call started running, and when it answered. */
|
|
534
513
|
startedAt?: number;
|
|
535
514
|
endedAt?: number;
|
|
536
|
-
/**
|
|
515
|
+
/** Closes the frame from then on. */
|
|
537
516
|
resulted?: boolean;
|
|
538
517
|
}
|
|
539
518
|
|
|
540
|
-
/** Pi shares one state object between the two halves of a call. */
|
|
541
519
|
function clockOf(context: { state: unknown }): Clock {
|
|
542
520
|
return context.state as Clock;
|
|
543
521
|
}
|
|
544
522
|
|
|
545
|
-
/** How long the model took to write the call. */
|
|
546
523
|
function wroteFor(clock: Clock): string | undefined {
|
|
547
524
|
if (clock.writingAt === undefined || clock.writtenAt === undefined) return undefined;
|
|
548
525
|
return elapsed(clock.writtenAt - clock.writingAt);
|
|
@@ -554,8 +531,12 @@ function ranFor(clock: Clock, running: boolean): string | undefined {
|
|
|
554
531
|
return end === undefined ? undefined : elapsed(end - clock.startedAt);
|
|
555
532
|
}
|
|
556
533
|
|
|
557
|
-
|
|
534
|
+
function markOf(phase: Phase): CallPhase {
|
|
535
|
+
return phase === "interrupted" ? "failed" : phase;
|
|
536
|
+
}
|
|
537
|
+
|
|
558
538
|
function timeOf(phase: Phase, clock: Clock): string | undefined {
|
|
539
|
+
if (phase === "interrupted") return undefined;
|
|
559
540
|
if (phase === "running") return ranFor(clock, true);
|
|
560
541
|
const from = phase === "waiting" ? clock.writtenAt : clock.writingAt;
|
|
561
542
|
return from === undefined ? undefined : elapsed(Date.now() - from);
|
|
@@ -569,7 +550,6 @@ function elapsed(ms: number): string {
|
|
|
569
550
|
return `${Math.floor(total / 3600)}h ${Math.floor((total % 3600) / 60)}m`;
|
|
570
551
|
}
|
|
571
552
|
|
|
572
|
-
/** Pi's renderers pad their part with blank lines, which the frame does not need. */
|
|
573
553
|
function trim(lines: readonly string[]): string[] {
|
|
574
554
|
let [from, to] = [0, lines.length];
|
|
575
555
|
while (from < to && isBlank(lines[from])) from++;
|
|
@@ -577,17 +557,26 @@ function trim(lines: readonly string[]): string[] {
|
|
|
577
557
|
return lines.slice(from, to);
|
|
578
558
|
}
|
|
579
559
|
|
|
580
|
-
/**
|
|
560
|
+
/** Stops at the count, so a long body costs no more than a short one. */
|
|
561
|
+
function hasLines(lines: readonly string[], count: number): boolean {
|
|
562
|
+
let found = 0;
|
|
563
|
+
for (const line of lines) {
|
|
564
|
+
if (!isBlank(line) && ++found >= count) return true;
|
|
565
|
+
}
|
|
566
|
+
return false;
|
|
567
|
+
}
|
|
568
|
+
|
|
581
569
|
function withoutBlanks(lines: readonly string[]): string[] {
|
|
582
570
|
return lines.filter((line) => !isBlank(line));
|
|
583
571
|
}
|
|
584
572
|
|
|
585
573
|
export function isBlank(line: string | undefined): boolean {
|
|
586
|
-
|
|
587
|
-
|
|
574
|
+
if (line === undefined) return true;
|
|
575
|
+
// A line of only color codes reads as blank; one without escapes needs no stripping.
|
|
576
|
+
return (line.includes("\u001b") ? stripTerminalSequences(line) : line).trim() === "";
|
|
588
577
|
}
|
|
589
578
|
|
|
590
|
-
|
|
579
|
+
function rulingText(ruling: CallRuling, theme: Theme, expanded: boolean): string {
|
|
591
580
|
const color = PHASE_COLORS[stateOf(ruling.tone)];
|
|
592
581
|
let line = `${theme.fg(color, GLYPH.judge)} ${theme.fg(color, ruling.head)}`;
|
|
593
582
|
if (ruling.note) line += ` ${theme.fg("accent", GLYPH.chevron)} ${ruling.note}`;
|
|
@@ -606,3 +595,7 @@ function stateOf(tone: RulingTone): Phase {
|
|
|
606
595
|
return "failed";
|
|
607
596
|
}
|
|
608
597
|
}
|
|
598
|
+
|
|
599
|
+
function pathOf(args: Record<string, unknown>): string | undefined {
|
|
600
|
+
return typeof args.path === "string" ? args.path : undefined;
|
|
601
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// A viewed session's folded state and scroll position, kept apart from drawing so it tests without a terminal.
|
|
2
|
+
import type { SessionSummary, SourceItem } from "@adeildo/pi-kit";
|
|
3
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
|
|
5
|
+
export class ViewState {
|
|
6
|
+
readonly #entries: SessionEntry[] = [];
|
|
7
|
+
readonly #ids = new Set<string>();
|
|
8
|
+
#summary: SessionSummary | undefined;
|
|
9
|
+
#ended = false;
|
|
10
|
+
/** So a drawing can tell it is stale. */
|
|
11
|
+
#version = 0;
|
|
12
|
+
|
|
13
|
+
get entries(): readonly SessionEntry[] {
|
|
14
|
+
return this.#entries;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get snapshot(): SessionSummary | undefined {
|
|
18
|
+
return this.#summary;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get ended(): boolean {
|
|
22
|
+
return this.#ended;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get version(): number {
|
|
26
|
+
return this.#version;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get last(): string | undefined {
|
|
30
|
+
return this.#entries.at(-1)?.id;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
apply(item: SourceItem): boolean {
|
|
34
|
+
switch (item.kind) {
|
|
35
|
+
case "entry":
|
|
36
|
+
if (this.#ids.has(item.entry.id)) return false;
|
|
37
|
+
this.#ids.add(item.entry.id);
|
|
38
|
+
this.#entries.push(item.entry);
|
|
39
|
+
break;
|
|
40
|
+
case "summary":
|
|
41
|
+
this.#summary = item.summary;
|
|
42
|
+
break;
|
|
43
|
+
case "reset":
|
|
44
|
+
this.#entries.length = 0;
|
|
45
|
+
this.#ids.clear();
|
|
46
|
+
break;
|
|
47
|
+
case "ended":
|
|
48
|
+
if (this.#ended) return false;
|
|
49
|
+
this.#ended = true;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
this.#version++;
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Counted from the end. At the end it follows; scrolled back, the lines being read stay put. */
|
|
58
|
+
export class Viewport {
|
|
59
|
+
#fromEnd = 0;
|
|
60
|
+
#total = 0;
|
|
61
|
+
#height = 0;
|
|
62
|
+
|
|
63
|
+
get following(): boolean {
|
|
64
|
+
return this.#fromEnd === 0;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** `[start, end)`. */
|
|
68
|
+
window(total: number, height: number): { start: number; end: number } {
|
|
69
|
+
// Lines arriving below while scrolled back push the end away, not the reader.
|
|
70
|
+
if (this.#fromEnd > 0 && total > this.#total) this.#fromEnd += total - this.#total;
|
|
71
|
+
this.#total = total;
|
|
72
|
+
this.#height = height;
|
|
73
|
+
this.#fromEnd = clamp(this.#fromEnd, 0, Math.max(0, total - height));
|
|
74
|
+
const end = total - this.#fromEnd;
|
|
75
|
+
return { start: Math.max(0, end - height), end };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
scroll(rows: number): void {
|
|
79
|
+
this.#fromEnd = clamp(this.#fromEnd - rows, 0, Math.max(0, this.#total - this.#height));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
page(direction: -1 | 1): void {
|
|
83
|
+
this.scroll(direction * Math.max(1, this.#height - 1));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
toEnd(): void {
|
|
87
|
+
this.#fromEnd = 0;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
toStart(): void {
|
|
91
|
+
this.#fromEnd = Math.max(0, this.#total - this.#height);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function clamp(value: number, low: number, high: number): number {
|
|
96
|
+
return Math.min(high, Math.max(low, value));
|
|
97
|
+
}
|