@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
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// Another session's entries, built as pi's interactive mode builds a loaded session.
|
|
2
|
+
import { messageRendererFor, resolveToolRenderers } from "@adeildo/pi-kit";
|
|
3
|
+
import {
|
|
4
|
+
AssistantMessageComponent,
|
|
5
|
+
BashExecutionComponent,
|
|
6
|
+
BranchSummaryMessageComponent,
|
|
7
|
+
buildSessionContext,
|
|
8
|
+
CompactionSummaryMessageComponent,
|
|
9
|
+
createBashToolDefinition,
|
|
10
|
+
createEditToolDefinition,
|
|
11
|
+
createFindToolDefinition,
|
|
12
|
+
createGrepToolDefinition,
|
|
13
|
+
createLsToolDefinition,
|
|
14
|
+
createReadToolDefinition,
|
|
15
|
+
createWriteToolDefinition,
|
|
16
|
+
CustomMessageComponent,
|
|
17
|
+
getMarkdownTheme,
|
|
18
|
+
parseSkillBlock,
|
|
19
|
+
type SessionEntry,
|
|
20
|
+
SkillInvocationMessageComponent,
|
|
21
|
+
ToolExecutionComponent,
|
|
22
|
+
type ToolRenderers,
|
|
23
|
+
UserMessageComponent,
|
|
24
|
+
} from "@earendil-works/pi-coding-agent";
|
|
25
|
+
import { type Component, Spacer, type TUI } from "@earendil-works/pi-tui";
|
|
26
|
+
|
|
27
|
+
type AgentMessage = ReturnType<typeof buildSessionContext>["messages"][number];
|
|
28
|
+
type Truncation = Parameters<BashExecutionComponent["setComplete"]>[2];
|
|
29
|
+
type UserContent = Extract<AgentMessage, { role: "user" }>["content"];
|
|
30
|
+
|
|
31
|
+
const BUILT_IN = {
|
|
32
|
+
bash: createBashToolDefinition,
|
|
33
|
+
read: createReadToolDefinition,
|
|
34
|
+
edit: createEditToolDefinition,
|
|
35
|
+
write: createWriteToolDefinition,
|
|
36
|
+
grep: createGrepToolDefinition,
|
|
37
|
+
find: createFindToolDefinition,
|
|
38
|
+
ls: createLsToolDefinition,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export interface TranscriptOptions {
|
|
42
|
+
tui: TUI;
|
|
43
|
+
/** Pi's tools show paths against it. */
|
|
44
|
+
cwd: string;
|
|
45
|
+
/** As `ctrl+o` does in pi. */
|
|
46
|
+
expanded: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function transcriptOf(
|
|
50
|
+
entries: readonly SessionEntry[],
|
|
51
|
+
leafId: string | undefined,
|
|
52
|
+
options: TranscriptOptions,
|
|
53
|
+
): Component[] {
|
|
54
|
+
const { messages } = buildSessionContext([...entries], leafId ?? entries.at(-1)?.id ?? null);
|
|
55
|
+
const theme = getMarkdownTheme();
|
|
56
|
+
const components: Component[] = [];
|
|
57
|
+
const calls = new Map<string, ToolExecutionComponent>();
|
|
58
|
+
|
|
59
|
+
for (const message of messages) {
|
|
60
|
+
switch (message.role) {
|
|
61
|
+
case "user": {
|
|
62
|
+
const text = textOf(message.content);
|
|
63
|
+
if (text === "") break;
|
|
64
|
+
if (components.length > 0) components.push(new Spacer(1));
|
|
65
|
+
const skill = parseSkillBlock(text);
|
|
66
|
+
if (skill === null) {
|
|
67
|
+
components.push(new UserMessageComponent(text, theme));
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
components.push(expand(new SkillInvocationMessageComponent(skill, theme), options));
|
|
71
|
+
if (skill.userMessage) {
|
|
72
|
+
components.push(new Spacer(1), new UserMessageComponent(skill.userMessage, theme));
|
|
73
|
+
}
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
case "assistant": {
|
|
77
|
+
components.push(new AssistantMessageComponent(message, false, theme));
|
|
78
|
+
const failed = message.stopReason === "aborted" || message.stopReason === "error";
|
|
79
|
+
for (const content of message.content) {
|
|
80
|
+
if (content.type !== "toolCall") continue;
|
|
81
|
+
const call = new ToolExecutionComponent(
|
|
82
|
+
content.name,
|
|
83
|
+
content.id,
|
|
84
|
+
content.arguments,
|
|
85
|
+
undefined,
|
|
86
|
+
renderersOf(content.name, options.cwd),
|
|
87
|
+
options.tui,
|
|
88
|
+
options.cwd,
|
|
89
|
+
);
|
|
90
|
+
call.setExpanded(options.expanded);
|
|
91
|
+
components.push(call);
|
|
92
|
+
if (!failed) {
|
|
93
|
+
calls.set(content.id, call);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
const text =
|
|
97
|
+
message.stopReason === "aborted"
|
|
98
|
+
? "Operation aborted"
|
|
99
|
+
: (message.errorMessage ?? "Error");
|
|
100
|
+
call.updateResult({ content: [{ type: "text", text }], isError: true });
|
|
101
|
+
}
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
case "toolResult":
|
|
105
|
+
calls.get(message.toolCallId)?.updateResult(message);
|
|
106
|
+
calls.delete(message.toolCallId);
|
|
107
|
+
break;
|
|
108
|
+
case "bashExecution": {
|
|
109
|
+
const bash = new BashExecutionComponent(
|
|
110
|
+
message.command,
|
|
111
|
+
options.tui,
|
|
112
|
+
message.excludeFromContext,
|
|
113
|
+
);
|
|
114
|
+
if (message.output) bash.appendOutput(message.output);
|
|
115
|
+
bash.setComplete(
|
|
116
|
+
message.exitCode,
|
|
117
|
+
message.cancelled,
|
|
118
|
+
// Pi hands the same partial result when it loads a session: the component reads `truncated` only.
|
|
119
|
+
message.truncated ? ({ truncated: true } as Truncation) : undefined,
|
|
120
|
+
message.fullOutputPath,
|
|
121
|
+
);
|
|
122
|
+
components.push(bash);
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
case "custom":
|
|
126
|
+
if (!message.display) break;
|
|
127
|
+
components.push(
|
|
128
|
+
expand(
|
|
129
|
+
new CustomMessageComponent(message, messageRendererFor(message.customType), theme),
|
|
130
|
+
options,
|
|
131
|
+
),
|
|
132
|
+
);
|
|
133
|
+
break;
|
|
134
|
+
case "compactionSummary":
|
|
135
|
+
components.push(
|
|
136
|
+
new Spacer(1),
|
|
137
|
+
expand(new CompactionSummaryMessageComponent(message, theme), options),
|
|
138
|
+
);
|
|
139
|
+
break;
|
|
140
|
+
case "branchSummary":
|
|
141
|
+
components.push(
|
|
142
|
+
new Spacer(1),
|
|
143
|
+
expand(new BranchSummaryMessageComponent(message, theme), options),
|
|
144
|
+
);
|
|
145
|
+
break;
|
|
146
|
+
default:
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return components;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Under the ones the harness registered. */
|
|
154
|
+
function renderersOf(name: string, cwd: string): ToolRenderers | undefined {
|
|
155
|
+
return resolveToolRenderers(name, () => {
|
|
156
|
+
if (!Object.hasOwn(BUILT_IN, name)) return undefined;
|
|
157
|
+
return BUILT_IN[name as keyof typeof BUILT_IN](cwd) as ToolRenderers;
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function expand<T extends { setExpanded(expanded: boolean): void }>(
|
|
162
|
+
component: T,
|
|
163
|
+
options: TranscriptOptions,
|
|
164
|
+
): T {
|
|
165
|
+
component.setExpanded(options.expanded);
|
|
166
|
+
return component;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function textOf(content: UserContent): string {
|
|
170
|
+
if (typeof content === "string") return content;
|
|
171
|
+
return content
|
|
172
|
+
.flatMap((part) => (part.type === "text" ? [part.text] : []))
|
|
173
|
+
.join("\n")
|
|
174
|
+
.trim();
|
|
175
|
+
}
|
package/src/view/view.ts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// Another session over the whole screen. A read-only source has a line in place of the editor.
|
|
2
|
+
import { entryFrame, type SessionSummary, type SessionSource } from "@adeildo/pi-kit";
|
|
3
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import {
|
|
5
|
+
type Component,
|
|
6
|
+
type Focusable,
|
|
7
|
+
Key,
|
|
8
|
+
matchesKey,
|
|
9
|
+
type TUI,
|
|
10
|
+
truncateToWidth,
|
|
11
|
+
visibleWidth,
|
|
12
|
+
} from "@earendil-works/pi-tui";
|
|
13
|
+
|
|
14
|
+
import { emptySnapshot, type Snapshot } from "../data/snapshot.ts";
|
|
15
|
+
import type { Piece } from "../render/fit.ts";
|
|
16
|
+
import { BOXES, border } from "../render/frame.ts";
|
|
17
|
+
import { type Screen, type SlotName, slotPieces } from "../ui/screen.ts";
|
|
18
|
+
import { ViewState, Viewport } from "./state.ts";
|
|
19
|
+
import { transcriptOf } from "./transcript.ts";
|
|
20
|
+
|
|
21
|
+
const CHROME = 6;
|
|
22
|
+
const READ_ONLY = "Read only: write to this session through a room.";
|
|
23
|
+
|
|
24
|
+
export interface ViewOptions {
|
|
25
|
+
tui: TUI;
|
|
26
|
+
theme: Theme;
|
|
27
|
+
screen: Screen;
|
|
28
|
+
source: SessionSource;
|
|
29
|
+
close(): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class SessionView implements Component, Focusable {
|
|
33
|
+
focused = false;
|
|
34
|
+
readonly #options: ViewOptions;
|
|
35
|
+
readonly #state = new ViewState();
|
|
36
|
+
readonly #viewport = new Viewport();
|
|
37
|
+
readonly #stop: () => void;
|
|
38
|
+
/** Drawn again only when the session or the width changed. */
|
|
39
|
+
#drawn: { version: number; width: number; lines: string[] } | undefined;
|
|
40
|
+
|
|
41
|
+
constructor(options: ViewOptions) {
|
|
42
|
+
this.#options = options;
|
|
43
|
+
this.#stop = options.source.follow(undefined, (item) => {
|
|
44
|
+
if (this.#state.apply(item)) options.tui.requestRender();
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
dispose(): void {
|
|
49
|
+
this.#stop();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
invalidate(): void {
|
|
53
|
+
this.#drawn = undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
handleInput(data: string): void {
|
|
57
|
+
if (matchesKey(data, Key.escape) || matchesKey(data, Key.alt("g")))
|
|
58
|
+
return this.#options.close();
|
|
59
|
+
if (matchesKey(data, Key.up)) this.#viewport.scroll(-1);
|
|
60
|
+
else if (matchesKey(data, Key.down)) this.#viewport.scroll(1);
|
|
61
|
+
else if (matchesKey(data, Key.pageUp)) this.#viewport.page(-1);
|
|
62
|
+
else if (matchesKey(data, Key.pageDown)) this.#viewport.page(1);
|
|
63
|
+
else if (matchesKey(data, Key.home)) this.#viewport.toStart();
|
|
64
|
+
else if (matchesKey(data, Key.end)) this.#viewport.toEnd();
|
|
65
|
+
else return;
|
|
66
|
+
this.#options.tui.requestRender();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
render(width: number): string[] {
|
|
70
|
+
const { theme, tui } = this.#options;
|
|
71
|
+
const inner = Math.max(1, width - 4);
|
|
72
|
+
const height = Math.max(1, tui.terminal.rows - CHROME);
|
|
73
|
+
const lines = this.#transcript(inner);
|
|
74
|
+
const { start, end } = this.#viewport.window(lines.length, height);
|
|
75
|
+
const shown = lines.slice(start, end);
|
|
76
|
+
const body = [...Array.from({ length: height - shown.length }, () => ""), ...shown];
|
|
77
|
+
const snapshot = this.#state.snapshot;
|
|
78
|
+
const ended = this.#state.ended;
|
|
79
|
+
// A blank row sets the editor box apart, as under your own.
|
|
80
|
+
return entryFrame([...body, "", ...this.#editor(inner, snapshot)], width, theme, {
|
|
81
|
+
top: titleOf(snapshot),
|
|
82
|
+
topRight: theme.fg(ended ? "dim" : "success", ended ? "ended" : "live"),
|
|
83
|
+
bottom: `${theme.fg("dim", "alt+g")} back`,
|
|
84
|
+
bottomRight: this.#viewport.following
|
|
85
|
+
? undefined
|
|
86
|
+
: theme.fg("dim", `${lines.length - end} more below`),
|
|
87
|
+
color: "accent",
|
|
88
|
+
dashed: ended,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
#transcript(width: number): string[] {
|
|
93
|
+
const state = this.#state;
|
|
94
|
+
if (this.#drawn?.version === state.version && this.#drawn.width === width)
|
|
95
|
+
return this.#drawn.lines;
|
|
96
|
+
const components = transcriptOf(state.entries, state.snapshot?.leafId, {
|
|
97
|
+
tui: this.#options.tui,
|
|
98
|
+
cwd: state.snapshot?.cwd ?? "",
|
|
99
|
+
expanded: false,
|
|
100
|
+
});
|
|
101
|
+
const lines = components.flatMap((component) => component.render(width));
|
|
102
|
+
this.#drawn = { version: state.version, width, lines };
|
|
103
|
+
return lines;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
#editor(width: number, session: SessionSummary | undefined): string[] {
|
|
107
|
+
const { screen, theme } = this.#options;
|
|
108
|
+
const style = screen.frameStyle();
|
|
109
|
+
const box = BOXES[style === "off" ? "rounded" : style];
|
|
110
|
+
const paint = screen.paint((text) => theme.fg("border", text));
|
|
111
|
+
const snapshot = lookSnapshot(screen.snapshot(), session);
|
|
112
|
+
const pieces = (name: SlotName): Piece[] => slotPieces(screen, name, snapshot, paint);
|
|
113
|
+
const options = {
|
|
114
|
+
box,
|
|
115
|
+
paint: paint.frame,
|
|
116
|
+
separator: paint.dim(screen.separator()),
|
|
117
|
+
ellipsis: screen.glyphs().ellipsis,
|
|
118
|
+
};
|
|
119
|
+
const line = truncateToWidth(theme.fg("dim", READ_ONLY), width - 4, "");
|
|
120
|
+
const fill = " ".repeat(Math.max(0, width - 4 - visibleWidth(line)));
|
|
121
|
+
return [
|
|
122
|
+
border(width, pieces("topLeft"), pieces("topRight"), { ...options, edge: "top" }),
|
|
123
|
+
`${paint.frame(box.vertical)} ${line}${fill} ${paint.frame(box.vertical)}`,
|
|
124
|
+
border(width, pieces("bottomLeft"), pieces("bottomRight"), { ...options, edge: "bottom" }),
|
|
125
|
+
];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function titleOf(snapshot: SessionSummary | undefined): string {
|
|
130
|
+
if (snapshot === undefined) return "Opening the session";
|
|
131
|
+
// With no title, the name a room gives a session without a handle.
|
|
132
|
+
const name = snapshot.name ?? `new-${snapshot.session.slice(-4)}`;
|
|
133
|
+
return `${name} @ ${snapshot.machine}`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** What the viewed session does not publish (git, account, costs) stays empty. */
|
|
137
|
+
export function lookSnapshot(yours: Snapshot, session: SessionSummary | undefined): Snapshot {
|
|
138
|
+
if (session === undefined) return { ...emptySnapshot(), now: yours.now, home: yours.home };
|
|
139
|
+
const window = session.context?.window;
|
|
140
|
+
return {
|
|
141
|
+
...emptySnapshot(),
|
|
142
|
+
now: yours.now,
|
|
143
|
+
home: yours.home,
|
|
144
|
+
cwd: session.cwd,
|
|
145
|
+
host: session.machine,
|
|
146
|
+
sessionName: session.name,
|
|
147
|
+
model:
|
|
148
|
+
session.model === undefined
|
|
149
|
+
? undefined
|
|
150
|
+
: { name: session.model.id, provider: session.model.provider, reasoning: false },
|
|
151
|
+
thinking: session.thinkingLevel,
|
|
152
|
+
context:
|
|
153
|
+
session.context === undefined || window === undefined
|
|
154
|
+
? undefined
|
|
155
|
+
: {
|
|
156
|
+
tokens: session.context.tokens,
|
|
157
|
+
window,
|
|
158
|
+
percent: (session.context.tokens / window) * 100,
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
}
|