@adeildo/pi-look 5.14.0 → 6.0.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/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 +59 -82
- 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,13 +20,13 @@ 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 { pathLine, type Stream, STREAMERS } from "../render/streaming.ts";
|
|
26
27
|
import { stateMark, toolIcon, toolLabel } from "../render/tools.ts";
|
|
27
28
|
import { icons } from "../settings.ts";
|
|
28
29
|
|
|
29
|
-
/** What the call is doing, which is what the bottom rule says while it has no result yet. */
|
|
30
30
|
type Phase = "writing" | "waiting" | "running" | "done" | "failed";
|
|
31
31
|
|
|
32
32
|
const PHASE_COLORS = {
|
|
@@ -38,19 +38,16 @@ const PHASE_COLORS = {
|
|
|
38
38
|
} as const;
|
|
39
39
|
const BODY_PAD = 4;
|
|
40
40
|
const COLLAPSED_ARGS_CHARS = 100;
|
|
41
|
-
/** How many lines of a result the frame prints before telling you the rest is there. */
|
|
42
41
|
const RESULT_PREVIEW_LINES = 10;
|
|
43
42
|
|
|
44
43
|
export function registerToolFrames(scope: FeatureScope): void {
|
|
45
44
|
const redraws = new Map<string, () => void>();
|
|
46
45
|
const written = new Map<string, number>();
|
|
47
|
-
//
|
|
46
|
+
// Asked for on every drawing, and it changes only with the setting.
|
|
48
47
|
const set = glyphsOf(() => icons.get(scope));
|
|
49
48
|
CALLS_FRAMED.provide(scope, () => true);
|
|
50
49
|
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.
|
|
50
|
+
// Pi completes a message's calls only when the message ends; the provider says when each one ends.
|
|
54
51
|
scope.on("message_update", (event) => {
|
|
55
52
|
const update = event.assistantMessageEvent;
|
|
56
53
|
if (update.type === "toolcall_end") written.set(update.toolCall.id, Date.now());
|
|
@@ -70,7 +67,6 @@ interface FrameContext {
|
|
|
70
67
|
name: string;
|
|
71
68
|
set: () => IconSet;
|
|
72
69
|
redraws: Map<string, () => void>;
|
|
73
|
-
/** When the model finished writing each call, until its frame reads it. */
|
|
74
70
|
written: Map<string, number>;
|
|
75
71
|
}
|
|
76
72
|
|
|
@@ -79,14 +75,11 @@ function framed(
|
|
|
79
75
|
base: ToolRenderers | undefined,
|
|
80
76
|
frame: FrameContext,
|
|
81
77
|
): ToolRenderers {
|
|
82
|
-
//
|
|
83
|
-
// exists, and the arguments or the text of the result stand in for the rest.
|
|
78
|
+
// The frame wraps whichever half a tool brings a renderer for.
|
|
84
79
|
const call = base?.renderCall ?? genericCall;
|
|
85
80
|
const result = base?.renderResult ?? genericResult;
|
|
86
81
|
|
|
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.
|
|
82
|
+
// Pi's renderers mutate the component they returned last time, so they get that one back, not the frame.
|
|
90
83
|
const inner = new WeakMap<Component, Component | undefined>();
|
|
91
84
|
const passed = (last: Component | undefined): Component | undefined =>
|
|
92
85
|
last === undefined || !inner.has(last) ? last : inner.get(last);
|
|
@@ -94,7 +87,6 @@ function framed(
|
|
|
94
87
|
|
|
95
88
|
return {
|
|
96
89
|
...base,
|
|
97
|
-
// The frame is the shell, so pi does not wrap it in a box of its own.
|
|
98
90
|
renderShell: "self",
|
|
99
91
|
renderCall(args, theme, context) {
|
|
100
92
|
frame.redraws.set(context.toolCallId, context.invalidate);
|
|
@@ -102,21 +94,19 @@ function framed(
|
|
|
102
94
|
const now = Date.now();
|
|
103
95
|
const ended = frame.written.get(context.toolCallId);
|
|
104
96
|
frame.written.delete(context.toolCallId);
|
|
105
|
-
// A call replayed
|
|
97
|
+
// A call replayed or already running was written long ago.
|
|
106
98
|
const done =
|
|
107
99
|
ended !== undefined ||
|
|
108
100
|
context.argsComplete ||
|
|
109
101
|
context.executionStarted ||
|
|
110
102
|
!context.isPartial;
|
|
111
|
-
// Only a call seen while the model wrote it has a time it took to write.
|
|
112
103
|
if (!done) clock.writingAt ??= now;
|
|
113
104
|
else clock.writtenAt ??= ended ?? now;
|
|
114
105
|
if (context.executionStarted) clock.startedAt ??= now;
|
|
115
106
|
const writing = clock.writtenAt === undefined;
|
|
116
107
|
const ruling = RULING.current(scope, context.toolCallId);
|
|
117
108
|
|
|
118
|
-
// Pi hands every call
|
|
119
|
-
// call that did not change keeps the frame it has, and its renderer is not asked again.
|
|
109
|
+
// Pi hands every call its arguments again on each piece the model writes, so an unchanged call keeps its frame.
|
|
120
110
|
const inputs = [
|
|
121
111
|
args,
|
|
122
112
|
theme,
|
|
@@ -135,8 +125,7 @@ function framed(
|
|
|
135
125
|
writing,
|
|
136
126
|
state: context.state as object,
|
|
137
127
|
});
|
|
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.
|
|
128
|
+
// While the model writes, pi's renderer has nothing new to show. The frame pads its inside itself.
|
|
140
129
|
const streaming = writing && stream !== undefined;
|
|
141
130
|
const own = streaming
|
|
142
131
|
? undefined
|
|
@@ -146,7 +135,13 @@ function framed(
|
|
|
146
135
|
outputPad: 0,
|
|
147
136
|
lastComponent: passed(last),
|
|
148
137
|
});
|
|
149
|
-
|
|
138
|
+
// Pi paints its edit on a colored box; the frame draws the diff itself, in the diff colors.
|
|
139
|
+
const drawnOwn = (component: Component): Component =>
|
|
140
|
+
frame.name === "edit" && isObject(args)
|
|
141
|
+
? editBody(component, pathLine("edit", pathOf(args), theme), theme)
|
|
142
|
+
: component;
|
|
143
|
+
const body =
|
|
144
|
+
own === undefined ? drawnBody(stream?.lines ?? []) : orStream(drawnOwn(own), stream);
|
|
150
145
|
|
|
151
146
|
const box = new CallFrame(
|
|
152
147
|
{
|
|
@@ -171,9 +166,9 @@ function framed(
|
|
|
171
166
|
},
|
|
172
167
|
renderResult(shown, options, theme, context) {
|
|
173
168
|
const clock = clockOf(context);
|
|
174
|
-
// A blocked call prints
|
|
169
|
+
// A blocked call prints only the reason the line above already gives.
|
|
175
170
|
const blocked = RULING.current(scope, context.toolCallId)?.blocked === true;
|
|
176
|
-
// Pi draws
|
|
171
|
+
// Pi draws a result's pictures right below the frame, so the frame counts them.
|
|
177
172
|
const images = shown.content.filter((block) => block.type === "image").length;
|
|
178
173
|
if (!options.isPartial) clock.endedAt ??= Date.now();
|
|
179
174
|
clock.resulted = true;
|
|
@@ -199,25 +194,17 @@ function framed(
|
|
|
199
194
|
};
|
|
200
195
|
}
|
|
201
196
|
|
|
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
197
|
const genericCall: NonNullable<ToolRenderers["renderCall"]> = (args, theme, context) =>
|
|
207
198
|
new Text(callText(args, theme, context.expanded), 0, 0);
|
|
208
199
|
|
|
209
200
|
const genericResult: NonNullable<ToolRenderers["renderResult"]> = (result, options, theme) =>
|
|
210
201
|
new Text(resultText(result, options.expanded, theme), 0, 0);
|
|
211
202
|
|
|
212
|
-
/** What a running call is doing, for tools whose running means something else. */
|
|
213
203
|
const RUNNING_WORDS: Readonly<Record<string, string>> = {
|
|
214
204
|
ask_questions: "waiting for your answer",
|
|
215
205
|
};
|
|
216
206
|
|
|
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
|
-
*/
|
|
207
|
+
/** Without a result: writing the arguments, then waiting to run. */
|
|
221
208
|
function phaseOf(
|
|
222
209
|
context: { isPartial: boolean; executionStarted: boolean },
|
|
223
210
|
writing: boolean,
|
|
@@ -282,10 +269,9 @@ interface FrameParts {
|
|
|
282
269
|
body: Component;
|
|
283
270
|
}
|
|
284
271
|
|
|
285
|
-
/** The top rule, the call inside it, and the phase while the call has nothing to show yet. */
|
|
286
272
|
class CallFrame implements Component {
|
|
287
273
|
#drawn?: Drawn;
|
|
288
|
-
/** Set when pi or the terminal asks for a fresh drawing
|
|
274
|
+
/** Set when pi or the terminal asks for a fresh drawing. */
|
|
289
275
|
#stale = false;
|
|
290
276
|
|
|
291
277
|
constructor(
|
|
@@ -293,7 +279,6 @@ class CallFrame implements Component {
|
|
|
293
279
|
private readonly inputs: readonly unknown[],
|
|
294
280
|
) {}
|
|
295
281
|
|
|
296
|
-
/** Whether this frame was made from these inputs and nothing asked for it to be made again. */
|
|
297
282
|
drawnFrom(inputs: readonly unknown[]): boolean {
|
|
298
283
|
return (
|
|
299
284
|
!this.#stale &&
|
|
@@ -310,13 +295,11 @@ class CallFrame implements Component {
|
|
|
310
295
|
|
|
311
296
|
render(width: number): string[] {
|
|
312
297
|
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.
|
|
298
|
+
// A running call closes too until its result draws the rest of the frame.
|
|
315
299
|
const running = this.parts.executing === true && clock?.resulted !== true;
|
|
316
300
|
const phase = this.parts.phase ?? (running ? "running" : undefined);
|
|
317
|
-
//
|
|
301
|
+
// Read on every drawing, so the time goes on while nothing new arrives.
|
|
318
302
|
const time = clock === undefined || phase === undefined ? undefined : timeOf(phase, clock);
|
|
319
|
-
// The mark of a phase that lasts moves, so it is picked on every drawing.
|
|
320
303
|
const mark = phase === undefined ? undefined : this.parts.mark(phase, this.parts.set());
|
|
321
304
|
const body = this.parts.body.render(room(width));
|
|
322
305
|
const progress = this.parts.progress?.join("\u0000");
|
|
@@ -325,10 +308,9 @@ class CallFrame implements Component {
|
|
|
325
308
|
|
|
326
309
|
const title = `${theme.fg("toolTitle", icon)} ${theme.fg("toolTitle", theme.bold(label))}`;
|
|
327
310
|
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
311
|
if (ruling !== undefined)
|
|
330
312
|
lines.push(...inside([rulingText(ruling, theme, expanded)], width, theme));
|
|
331
|
-
//
|
|
313
|
+
// With nothing else to show yet, the frame closes with what the call is doing.
|
|
332
314
|
if (phase !== undefined && mark !== undefined) {
|
|
333
315
|
const times = [time, ...(this.parts.progress ?? [])];
|
|
334
316
|
const word = phase === "running" ? this.parts.runningAs : undefined;
|
|
@@ -348,36 +330,30 @@ interface CallParts {
|
|
|
348
330
|
phase?: Phase;
|
|
349
331
|
mark: (phase: Phase, set: IconSet) => string;
|
|
350
332
|
set: () => IconSet;
|
|
351
|
-
/** What the model has written so far, while it writes. */
|
|
352
333
|
progress?: readonly string[];
|
|
353
334
|
clock?: Clock;
|
|
354
|
-
/**
|
|
335
|
+
/** It runs and its result is not final. */
|
|
355
336
|
executing?: boolean;
|
|
356
|
-
/**
|
|
337
|
+
/** When the tool's running means something else. */
|
|
357
338
|
runningAs?: string;
|
|
358
339
|
}
|
|
359
340
|
|
|
360
|
-
/** Lines that are already drawn. The frame cuts the ones too wide for it. */
|
|
361
341
|
function drawnBody(drawn: readonly string[]): Component {
|
|
362
342
|
return { render: () => [...drawn], invalidate: () => {} };
|
|
363
343
|
}
|
|
364
344
|
|
|
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
|
-
*/
|
|
345
|
+
/** The stream while pi's body is still only its first line. */
|
|
369
346
|
function orStream(own: Component, stream: Stream | undefined): Component {
|
|
370
347
|
if (stream === undefined) return own;
|
|
371
348
|
return {
|
|
372
349
|
render(width) {
|
|
373
350
|
const drawn = own.render(width);
|
|
374
|
-
return
|
|
351
|
+
return hasLines(drawn, 2) ? drawn : [...stream.lines];
|
|
375
352
|
},
|
|
376
353
|
invalidate: () => own.invalidate(),
|
|
377
354
|
};
|
|
378
355
|
}
|
|
379
356
|
|
|
380
|
-
/** The result, behind a cut, and the bottom rule with how it went and how long it took. */
|
|
381
357
|
class ResultFrame implements Component {
|
|
382
358
|
#drawn?: Drawn;
|
|
383
359
|
|
|
@@ -399,7 +375,7 @@ class ResultFrame implements Component {
|
|
|
399
375
|
const printed =
|
|
400
376
|
quiet === true ? [] : withoutBlanks(dropTime(body)).map((line) => hidden(line, theme));
|
|
401
377
|
const lines = [
|
|
402
|
-
//
|
|
378
|
+
// A call with nothing to print has no cut.
|
|
403
379
|
...(printed.length === 0 ? [] : [cut(width, theme), ...inside(printed, width, theme)]),
|
|
404
380
|
rule(
|
|
405
381
|
"bottom",
|
|
@@ -423,14 +399,13 @@ interface ResultParts {
|
|
|
423
399
|
set: () => IconSet;
|
|
424
400
|
elapsed?: string;
|
|
425
401
|
wrote?: string;
|
|
426
|
-
/**
|
|
402
|
+
/** Pi draws them outside the frame. */
|
|
427
403
|
images?: string;
|
|
428
|
-
/** Nothing worth printing
|
|
404
|
+
/** Nothing worth printing, like a call the gate blocked. */
|
|
429
405
|
quiet?: boolean;
|
|
430
406
|
}
|
|
431
407
|
|
|
432
|
-
|
|
433
|
-
export function rule(
|
|
408
|
+
function rule(
|
|
434
409
|
edge: "top" | "bottom",
|
|
435
410
|
left: string,
|
|
436
411
|
width: number,
|
|
@@ -444,20 +419,16 @@ export function rule(
|
|
|
444
419
|
const fill = Math.max(1, width - BODY_PAD - visibleWidth(cell));
|
|
445
420
|
const line =
|
|
446
421
|
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
422
|
return fill === 1 ? truncateToWidth(line, width, "") : line;
|
|
449
423
|
}
|
|
450
424
|
|
|
451
|
-
/** The line that ends the call and starts what it printed. */
|
|
452
425
|
function cut(width: number, theme: Theme): string {
|
|
453
426
|
const paint = (text: string) => theme.fg("border", text);
|
|
454
427
|
const fill = Math.max(1, width - BODY_PAD);
|
|
455
428
|
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
429
|
return fill === 1 ? truncateToWidth(line, width, "") : line;
|
|
458
430
|
}
|
|
459
431
|
|
|
460
|
-
/** The bottom rule: what the call is doing, how long it ran, and how long the model took to write it. */
|
|
461
432
|
function phaseLabel(
|
|
462
433
|
theme: Theme,
|
|
463
434
|
mark: string,
|
|
@@ -475,9 +446,9 @@ function inside(lines: readonly string[], width: number, theme: Theme): string[]
|
|
|
475
446
|
const paint = (text: string) => theme.fg("border", text);
|
|
476
447
|
const inner = room(width);
|
|
477
448
|
return trim(lines).map((line) => {
|
|
478
|
-
|
|
479
|
-
const text =
|
|
480
|
-
const pad = " ".repeat(Math.max(0, inner - visibleWidth(text)));
|
|
449
|
+
const wide = visibleWidth(line);
|
|
450
|
+
const text = wide > inner ? truncateToWidth(line, inner, CUT, true) : line;
|
|
451
|
+
const pad = " ".repeat(Math.max(0, inner - (wide > inner ? visibleWidth(text) : wide)));
|
|
481
452
|
return `${paint(LINE.v)} ${text}${pad} ${paint(LINE.v)}`;
|
|
482
453
|
});
|
|
483
454
|
}
|
|
@@ -485,18 +456,17 @@ function inside(lines: readonly string[], width: number, theme: Theme): string[]
|
|
|
485
456
|
interface Drawn {
|
|
486
457
|
width: number;
|
|
487
458
|
body: readonly string[];
|
|
488
|
-
/** What the frame drew around the body: the ruling, the phase, the times. */
|
|
489
459
|
rest: readonly unknown[];
|
|
490
460
|
lines: string[];
|
|
491
461
|
}
|
|
492
462
|
|
|
493
|
-
/**
|
|
463
|
+
/** Keeps typing cheap. */
|
|
494
464
|
function unchanged(drawn: Drawn | undefined, next: Omit<Drawn, "lines">): drawn is Drawn {
|
|
495
465
|
if (drawn === undefined || drawn.width !== next.width) return false;
|
|
496
466
|
if (drawn.body.length !== next.body.length || drawn.rest.length !== next.rest.length)
|
|
497
467
|
return false;
|
|
498
468
|
if (drawn.rest.some((part, index) => part !== next.rest[index])) return false;
|
|
499
|
-
// A renderer that kept its lines hands back the same array
|
|
469
|
+
// A renderer that kept its lines hands back the same array.
|
|
500
470
|
return drawn.body === next.body || drawn.body.every((line, index) => line === next.body[index]);
|
|
501
471
|
}
|
|
502
472
|
|
|
@@ -504,11 +474,11 @@ function room(width: number): number {
|
|
|
504
474
|
return Math.max(1, width - BODY_PAD);
|
|
505
475
|
}
|
|
506
476
|
|
|
507
|
-
/**
|
|
477
|
+
/** As `... (240 earlier lines, ctrl+o to expand)`. */
|
|
508
478
|
const HIDDEN = /^\.\.\. \((\d+) (earlier|more) lines, (.+?) to expand\)$/u;
|
|
509
479
|
|
|
510
|
-
/** Pi's note on cut lines, in the frame's words: which way they are, how many, and the key. */
|
|
511
480
|
export function hidden(line: string, theme: Theme): string {
|
|
481
|
+
if (!line.includes(" to expand)")) return line;
|
|
512
482
|
const match = HIDDEN.exec(stripTerminalSequences(line).trim());
|
|
513
483
|
if (match === null) return line;
|
|
514
484
|
const [, count, side, key] = match;
|
|
@@ -517,7 +487,7 @@ export function hidden(line: string, theme: Theme): string {
|
|
|
517
487
|
return `${theme.fg("dim", GLYPH.ellipsisUp)} ${lines} ${theme.fg("dim", key ?? "")}`;
|
|
518
488
|
}
|
|
519
489
|
|
|
520
|
-
/**
|
|
490
|
+
/** The frame says the time on the bottom rule instead. */
|
|
521
491
|
function dropTime(lines: readonly string[]): string[] {
|
|
522
492
|
const at = lines.findLastIndex((line) => !isBlank(line));
|
|
523
493
|
if (at === -1) return [...lines];
|
|
@@ -527,22 +497,18 @@ function dropTime(lines: readonly string[]): string[] {
|
|
|
527
497
|
}
|
|
528
498
|
|
|
529
499
|
interface Clock {
|
|
530
|
-
/** When the model started writing the call, and when it finished. */
|
|
531
500
|
writingAt?: number;
|
|
532
501
|
writtenAt?: number;
|
|
533
|
-
/** When the call started running, and when it answered. */
|
|
534
502
|
startedAt?: number;
|
|
535
503
|
endedAt?: number;
|
|
536
|
-
/**
|
|
504
|
+
/** Closes the frame from then on. */
|
|
537
505
|
resulted?: boolean;
|
|
538
506
|
}
|
|
539
507
|
|
|
540
|
-
/** Pi shares one state object between the two halves of a call. */
|
|
541
508
|
function clockOf(context: { state: unknown }): Clock {
|
|
542
509
|
return context.state as Clock;
|
|
543
510
|
}
|
|
544
511
|
|
|
545
|
-
/** How long the model took to write the call. */
|
|
546
512
|
function wroteFor(clock: Clock): string | undefined {
|
|
547
513
|
if (clock.writingAt === undefined || clock.writtenAt === undefined) return undefined;
|
|
548
514
|
return elapsed(clock.writtenAt - clock.writingAt);
|
|
@@ -554,7 +520,6 @@ function ranFor(clock: Clock, running: boolean): string | undefined {
|
|
|
554
520
|
return end === undefined ? undefined : elapsed(end - clock.startedAt);
|
|
555
521
|
}
|
|
556
522
|
|
|
557
|
-
/** How long the call has been in the phase the call half of the frame closes with. */
|
|
558
523
|
function timeOf(phase: Phase, clock: Clock): string | undefined {
|
|
559
524
|
if (phase === "running") return ranFor(clock, true);
|
|
560
525
|
const from = phase === "waiting" ? clock.writtenAt : clock.writingAt;
|
|
@@ -569,7 +534,6 @@ function elapsed(ms: number): string {
|
|
|
569
534
|
return `${Math.floor(total / 3600)}h ${Math.floor((total % 3600) / 60)}m`;
|
|
570
535
|
}
|
|
571
536
|
|
|
572
|
-
/** Pi's renderers pad their part with blank lines, which the frame does not need. */
|
|
573
537
|
function trim(lines: readonly string[]): string[] {
|
|
574
538
|
let [from, to] = [0, lines.length];
|
|
575
539
|
while (from < to && isBlank(lines[from])) from++;
|
|
@@ -577,17 +541,26 @@ function trim(lines: readonly string[]): string[] {
|
|
|
577
541
|
return lines.slice(from, to);
|
|
578
542
|
}
|
|
579
543
|
|
|
580
|
-
/**
|
|
544
|
+
/** Stops at the count, so a long body costs no more than a short one. */
|
|
545
|
+
function hasLines(lines: readonly string[], count: number): boolean {
|
|
546
|
+
let found = 0;
|
|
547
|
+
for (const line of lines) {
|
|
548
|
+
if (!isBlank(line) && ++found >= count) return true;
|
|
549
|
+
}
|
|
550
|
+
return false;
|
|
551
|
+
}
|
|
552
|
+
|
|
581
553
|
function withoutBlanks(lines: readonly string[]): string[] {
|
|
582
554
|
return lines.filter((line) => !isBlank(line));
|
|
583
555
|
}
|
|
584
556
|
|
|
585
557
|
export function isBlank(line: string | undefined): boolean {
|
|
586
|
-
|
|
587
|
-
|
|
558
|
+
if (line === undefined) return true;
|
|
559
|
+
// A line of only color codes reads as blank; one without escapes needs no stripping.
|
|
560
|
+
return (line.includes("\u001b") ? stripTerminalSequences(line) : line).trim() === "";
|
|
588
561
|
}
|
|
589
562
|
|
|
590
|
-
|
|
563
|
+
function rulingText(ruling: CallRuling, theme: Theme, expanded: boolean): string {
|
|
591
564
|
const color = PHASE_COLORS[stateOf(ruling.tone)];
|
|
592
565
|
let line = `${theme.fg(color, GLYPH.judge)} ${theme.fg(color, ruling.head)}`;
|
|
593
566
|
if (ruling.note) line += ` ${theme.fg("accent", GLYPH.chevron)} ${ruling.note}`;
|
|
@@ -606,3 +579,7 @@ function stateOf(tone: RulingTone): Phase {
|
|
|
606
579
|
return "failed";
|
|
607
580
|
}
|
|
608
581
|
}
|
|
582
|
+
|
|
583
|
+
function pathOf(args: Record<string, unknown>): string | undefined {
|
|
584
|
+
return typeof args.path === "string" ? args.path : undefined;
|
|
585
|
+
}
|
|
@@ -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
|
+
}
|