@pmndre/pine 0.0.0-stage → 0.22.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/LICENSE +21 -0
- package/README.md +226 -2
- package/bin/pine.js +27 -0
- package/docs/SKILL.md +234 -0
- package/docs/references/agents.md +454 -0
- package/docs/references/coding.md +311 -0
- package/docs/references/computer.md +212 -0
- package/docs/references/configuration.md +310 -0
- package/docs/references/events.md +267 -0
- package/docs/references/mcp.md +420 -0
- package/docs/references/operations.md +394 -0
- package/docs/references/overview.md +195 -0
- package/docs/references/security.md +896 -0
- package/docs/references/setup.md +385 -0
- package/docs/references/sources.md +772 -0
- package/docs/references/stack.md +121 -0
- package/docs/references/tui.md +575 -0
- package/examples/home/.env.example +7 -0
- package/examples/home/USER.md +4 -0
- package/examples/home/agents/main/agent.json +4 -0
- package/examples/home/agents/main/workspace/AGENTS.md +14 -0
- package/examples/home/agents/main/workspace/MEMORY.md +4 -0
- package/examples/home/agents/main/workspace/SOUL.md +4 -0
- package/examples/home/models.json +62 -0
- package/examples/home/pine.json +34 -0
- package/examples/home/skills/README.txt +7 -0
- package/examples/launchd/com.pine.daemon.plist +32 -0
- package/examples/systemd/pine.service +22 -0
- package/mcp/browser.d.ts +99 -0
- package/mcp/browser.js +496 -0
- package/mcp/commands.d.ts +20 -0
- package/mcp/commands.js +88 -0
- package/mcp/computer.d.ts +139 -0
- package/mcp/computer.js +698 -0
- package/mcp/computer.swift +989 -0
- package/mcp/lib.d.ts +23 -0
- package/mcp/lib.js +52 -0
- package/mcp/web.d.ts +6 -0
- package/mcp/web.js +140 -0
- package/package.json +32 -4
- package/src/access.d.ts +112 -0
- package/src/access.js +438 -0
- package/src/actions.d.ts +75 -0
- package/src/actions.js +167 -0
- package/src/admin.d.ts +167 -0
- package/src/admin.js +625 -0
- package/src/agent-admin.d.ts +24 -0
- package/src/agent-admin.js +55 -0
- package/src/agents.d.ts +540 -0
- package/src/agents.js +2776 -0
- package/src/background.d.ts +29 -0
- package/src/background.js +100 -0
- package/src/cli.d.ts +2 -0
- package/src/cli.js +777 -0
- package/src/computer.d.ts +30 -0
- package/src/computer.js +32 -0
- package/src/config.d.ts +396 -0
- package/src/config.js +549 -0
- package/src/dev/claude-model.d.ts +37 -0
- package/src/dev/claude-model.js +204 -0
- package/src/env-file.d.ts +4 -0
- package/src/env-file.js +40 -0
- package/src/goals.d.ts +133 -0
- package/src/goals.js +316 -0
- package/src/http.d.ts +46 -0
- package/src/http.js +509 -0
- package/src/limiter.d.ts +32 -0
- package/src/limiter.js +84 -0
- package/src/login.d.ts +23 -0
- package/src/login.js +130 -0
- package/src/manual.d.ts +61 -0
- package/src/manual.js +443 -0
- package/src/mcp-config.d.ts +49 -0
- package/src/mcp-config.js +182 -0
- package/src/models.d.ts +90 -0
- package/src/models.js +274 -0
- package/src/net.d.ts +50 -0
- package/src/net.js +150 -0
- package/src/pine.d.ts +32 -0
- package/src/pine.js +134 -0
- package/src/programs.d.ts +123 -0
- package/src/programs.js +412 -0
- package/src/router.d.ts +56 -0
- package/src/router.js +164 -0
- package/src/sandbox-fs.d.ts +34 -0
- package/src/sandbox-fs.js +186 -0
- package/src/sandbox-linux.d.ts +96 -0
- package/src/sandbox-linux.js +297 -0
- package/src/sandbox-setup.d.ts +70 -0
- package/src/sandbox-setup.js +216 -0
- package/src/security.d.ts +238 -0
- package/src/security.js +660 -0
- package/src/skills.d.ts +67 -0
- package/src/skills.js +163 -0
- package/src/snapshots.d.ts +68 -0
- package/src/snapshots.js +306 -0
- package/src/source-types.d.ts +61 -0
- package/src/source-types.js +1 -0
- package/src/sources/command.d.ts +4 -0
- package/src/sources/command.js +77 -0
- package/src/sources/folder.d.ts +6 -0
- package/src/sources/folder.js +116 -0
- package/src/sources/ftp.d.ts +4 -0
- package/src/sources/ftp.js +307 -0
- package/src/sources/http.d.ts +4 -0
- package/src/sources/http.js +201 -0
- package/src/sources/mqtt.d.ts +18 -0
- package/src/sources/mqtt.js +97 -0
- package/src/sources/telegram.d.ts +22 -0
- package/src/sources/telegram.js +283 -0
- package/src/sources/webhook.d.ts +4 -0
- package/src/sources/webhook.js +52 -0
- package/src/sources.d.ts +344 -0
- package/src/sources.js +966 -0
- package/src/store.d.ts +99 -0
- package/src/store.js +277 -0
- package/src/timers.d.ts +83 -0
- package/src/timers.js +318 -0
- package/src/tools.d.ts +11 -0
- package/src/tools.js +1297 -0
- package/src/tui/app.d.ts +305 -0
- package/src/tui/app.js +3195 -0
- package/src/tui/client.d.ts +236 -0
- package/src/tui/client.js +131 -0
- package/src/tui/code-pane.d.ts +68 -0
- package/src/tui/code-pane.js +535 -0
- package/src/tui/highlight.d.ts +8 -0
- package/src/tui/highlight.js +86 -0
- package/src/tui/picture.d.ts +206 -0
- package/src/tui/picture.js +801 -0
- package/src/tui/run.d.ts +8 -0
- package/src/tui/run.js +97 -0
- package/src/tui/theme.d.ts +32 -0
- package/src/tui/theme.js +128 -0
- package/src/tui/undo.d.ts +5 -0
- package/src/tui/undo.js +50 -0
- package/src/tui/voice.d.ts +49 -0
- package/src/tui/voice.js +155 -0
- package/src/tui/voice.swift +223 -0
- package/src/types.d.ts +139 -0
- package/src/types.js +1 -0
- package/src/undo.d.ts +224 -0
- package/src/undo.js +465 -0
- package/src/update.d.ts +35 -0
- package/src/update.js +130 -0
- package/src/util.d.ts +65 -0
- package/src/util.js +171 -0
- package/src/watching.d.ts +20 -0
- package/src/watching.js +68 -0
- package/src/workspace-view.d.ts +72 -0
- package/src/workspace-view.js +221 -0
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { ScrollView, truncateToWidth, visibleWidth, VStack, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
4
|
+
import { errText, programName, since } from "../util.js";
|
|
5
|
+
import { highlightCode } from "./highlight.js";
|
|
6
|
+
import { halfBlocks, isImageFile, prepare } from "./picture.js";
|
|
7
|
+
import { c } from "./theme.js";
|
|
8
|
+
export const PANE_VIEWS = ["changes", "files", "output", "running"];
|
|
9
|
+
const tilde = (p) => (p === os.homedir() ? "~" : p.startsWith(`${os.homedir()}/`) ? `~${p.slice(os.homedir().length)}` : p);
|
|
10
|
+
const plain = (s) => s.replace(/\t/g, " ").replace(/[\x00-\x08\x0b-\x1f\x7f]/g, "");
|
|
11
|
+
const size = (n) => (n < 1024 ? `${n} B` : n < 1024 * 1024 ? `${Math.round(n / 1024)} KB` : `${(n / 1024 / 1024).toFixed(1)} MB`);
|
|
12
|
+
const took = (ms) => (ms === undefined ? "" : ms < 1000 ? `${Math.round(ms)} ms` : `${(ms / 1000).toFixed(1)} s`);
|
|
13
|
+
/** The lines of a diff as git prints it, in colours, without the lines that only name the file. */
|
|
14
|
+
export function diffLines(diff) {
|
|
15
|
+
const out = [];
|
|
16
|
+
for (const raw of diff.replace(/\n$/, "").split("\n")) {
|
|
17
|
+
const line = plain(raw);
|
|
18
|
+
if (/^(diff --git |index |--- |\+\+\+ |new file mode|deleted file mode|old mode|new mode|similarity index)/.test(line))
|
|
19
|
+
continue;
|
|
20
|
+
if (line.startsWith("@@"))
|
|
21
|
+
out.push(c.cyan(line));
|
|
22
|
+
else if (line.startsWith("+"))
|
|
23
|
+
out.push(c.green(line));
|
|
24
|
+
else if (line.startsWith("-"))
|
|
25
|
+
out.push(c.red(line));
|
|
26
|
+
else if (/^(rename |Binary files |\\ )/.test(line))
|
|
27
|
+
out.push(c.gray(line));
|
|
28
|
+
else
|
|
29
|
+
out.push(line);
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
/** Lines that react to a click, each cut to the width or, where it says so, wrapped. */
|
|
34
|
+
class Rows {
|
|
35
|
+
rows = [];
|
|
36
|
+
owner = [];
|
|
37
|
+
cache;
|
|
38
|
+
invalidate() {
|
|
39
|
+
this.cache = undefined;
|
|
40
|
+
}
|
|
41
|
+
handleMouse(e) {
|
|
42
|
+
const row = this.rows[this.owner[e.y]];
|
|
43
|
+
if (!row?.run || e.button !== "left" || (e.type !== "press" && e.type !== "click" && e.type !== "release"))
|
|
44
|
+
return undefined;
|
|
45
|
+
if (e.type === "click")
|
|
46
|
+
row.run();
|
|
47
|
+
return { handled: true, render: true };
|
|
48
|
+
}
|
|
49
|
+
render(width) {
|
|
50
|
+
if (this.cache?.width === width && this.cache.rows === this.rows)
|
|
51
|
+
return this.cache.lines;
|
|
52
|
+
const lines = [];
|
|
53
|
+
this.owner = [];
|
|
54
|
+
this.rows.forEach((row, i) => {
|
|
55
|
+
const gutter = row.gutter ?? "";
|
|
56
|
+
const room = Math.max(8, width - 1 - visibleWidth(gutter));
|
|
57
|
+
const parts = row.wrap && visibleWidth(row.text) > room ? wrapTextWithAnsi(row.text, room) : [truncateToWidth(row.text, room)];
|
|
58
|
+
parts.forEach((part, n) => {
|
|
59
|
+
lines.push(` ${n === 0 ? gutter : " ".repeat(visibleWidth(gutter))}${part}`);
|
|
60
|
+
this.owner.push(i);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
this.cache = { width, rows: this.rows, lines };
|
|
64
|
+
return lines;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export class CodePane {
|
|
68
|
+
/** What goes into the layout. */
|
|
69
|
+
component;
|
|
70
|
+
client;
|
|
71
|
+
redraw;
|
|
72
|
+
close;
|
|
73
|
+
target;
|
|
74
|
+
states = new Map();
|
|
75
|
+
rows = new Rows();
|
|
76
|
+
scroll;
|
|
77
|
+
hits = [];
|
|
78
|
+
timer;
|
|
79
|
+
width = 60;
|
|
80
|
+
/** Counts loads, so that an answer that arrives late does not overwrite a newer one. */
|
|
81
|
+
turn = 0;
|
|
82
|
+
steps;
|
|
83
|
+
constructor(opts) {
|
|
84
|
+
this.steps = opts.steps;
|
|
85
|
+
this.client = opts.client;
|
|
86
|
+
this.redraw = opts.redraw;
|
|
87
|
+
this.close = opts.close;
|
|
88
|
+
this.target = opts.target;
|
|
89
|
+
this.scroll = new ScrollView(this.rows, { follow: "none", overscroll: "contain", scrollbar: "auto" });
|
|
90
|
+
const head = {
|
|
91
|
+
invalidate() { },
|
|
92
|
+
render: (w) => this.renderHead(w),
|
|
93
|
+
handleMouse: (e) => {
|
|
94
|
+
if (e.y !== 0 || e.button !== "left" || (e.type !== "press" && e.type !== "click" && e.type !== "release"))
|
|
95
|
+
return undefined;
|
|
96
|
+
const hit = this.hits.find((h) => e.x >= h.from && e.x < h.to);
|
|
97
|
+
if (!hit)
|
|
98
|
+
return undefined;
|
|
99
|
+
if (e.type === "click")
|
|
100
|
+
hit.run();
|
|
101
|
+
return { handled: true, render: true };
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
this.component = new VStack([
|
|
105
|
+
{ component: head, basis: "auto", grow: 0, shrink: 0 },
|
|
106
|
+
{ component: this.scroll, basis: 0, grow: 1, shrink: 1, minSize: 1 },
|
|
107
|
+
]);
|
|
108
|
+
}
|
|
109
|
+
get state() {
|
|
110
|
+
const t = this.target();
|
|
111
|
+
if (!t)
|
|
112
|
+
return undefined;
|
|
113
|
+
const key = `${t.agent}:${t.session}`;
|
|
114
|
+
let s = this.states.get(key);
|
|
115
|
+
if (!s) {
|
|
116
|
+
if (this.states.size > 40)
|
|
117
|
+
this.states.clear();
|
|
118
|
+
s = { view: "changes", dirs: new Map(), expanded: new Set([""]) };
|
|
119
|
+
this.states.set(key, s);
|
|
120
|
+
}
|
|
121
|
+
return s;
|
|
122
|
+
}
|
|
123
|
+
get view() {
|
|
124
|
+
return this.state?.view ?? "changes";
|
|
125
|
+
}
|
|
126
|
+
/** Show one of the views, from its top level. Without a view: whatever was shown last, read again. */
|
|
127
|
+
show(view) {
|
|
128
|
+
const s = this.state;
|
|
129
|
+
if (!s)
|
|
130
|
+
return this.build();
|
|
131
|
+
if (view) {
|
|
132
|
+
s.view = view;
|
|
133
|
+
s.scope = undefined;
|
|
134
|
+
s.diff = undefined;
|
|
135
|
+
s.file = undefined;
|
|
136
|
+
s.call = undefined;
|
|
137
|
+
}
|
|
138
|
+
this.scroll.scrollToStart();
|
|
139
|
+
void this.load(true);
|
|
140
|
+
}
|
|
141
|
+
/** The changes view, counted from before the owner's last message: what that message changed. */
|
|
142
|
+
showTurn() {
|
|
143
|
+
const s = this.state;
|
|
144
|
+
if (!s)
|
|
145
|
+
return this.build();
|
|
146
|
+
Object.assign(s, { view: "changes", scope: "turn", diff: undefined });
|
|
147
|
+
this.scroll.scrollToStart();
|
|
148
|
+
void this.load(true);
|
|
149
|
+
}
|
|
150
|
+
/** The conversation shown changed its folder: what was read from the old one is not shown any more. */
|
|
151
|
+
forget(target) {
|
|
152
|
+
const view = this.states.get(`${target.agent}:${target.session}`)?.view;
|
|
153
|
+
this.states.delete(`${target.agent}:${target.session}`);
|
|
154
|
+
if (view && this.state)
|
|
155
|
+
this.state.view = view;
|
|
156
|
+
this.show();
|
|
157
|
+
}
|
|
158
|
+
/** Something happened that may have changed what is shown (a tool ran, a task ended): read it again in a moment. */
|
|
159
|
+
touch() {
|
|
160
|
+
clearTimeout(this.timer);
|
|
161
|
+
this.timer = setTimeout(() => void this.load(false), 350);
|
|
162
|
+
this.timer.unref?.();
|
|
163
|
+
}
|
|
164
|
+
stop() {
|
|
165
|
+
clearTimeout(this.timer);
|
|
166
|
+
}
|
|
167
|
+
// ---- reading ---------------------------------------------------------------------------------
|
|
168
|
+
async load(visibly) {
|
|
169
|
+
const t = this.target();
|
|
170
|
+
const s = this.state;
|
|
171
|
+
if (!t || !s)
|
|
172
|
+
return this.build();
|
|
173
|
+
const turn = ++this.turn;
|
|
174
|
+
if (visibly) {
|
|
175
|
+
s.loading = "reading …";
|
|
176
|
+
this.build();
|
|
177
|
+
}
|
|
178
|
+
try {
|
|
179
|
+
if (s.view === "changes" && s.scope === "turn") {
|
|
180
|
+
const last = await this.client.turn(t.agent, t.session);
|
|
181
|
+
const diff = s.diff ? await this.client.turnDiff(t.agent, t.session, s.diff.file).catch(() => undefined) : undefined;
|
|
182
|
+
if (turn !== this.turn)
|
|
183
|
+
return;
|
|
184
|
+
s.turn = last;
|
|
185
|
+
s.root = last.root ?? s.root;
|
|
186
|
+
if (s.diff)
|
|
187
|
+
Object.assign(s.diff, { lines: diff ? diffLines(diff.diff) : [], truncated: diff?.truncated });
|
|
188
|
+
}
|
|
189
|
+
else if (s.view === "changes") {
|
|
190
|
+
const changes = await this.client.changes(t.agent, t.session);
|
|
191
|
+
const diff = s.diff ? await this.client.diff(t.agent, t.session, s.diff.file).catch(() => undefined) : undefined;
|
|
192
|
+
if (turn !== this.turn)
|
|
193
|
+
return;
|
|
194
|
+
s.changes = changes;
|
|
195
|
+
s.root = changes.root;
|
|
196
|
+
if (s.diff)
|
|
197
|
+
Object.assign(s.diff, { lines: diff ? diffLines(diff.diff) : [], truncated: diff?.truncated });
|
|
198
|
+
}
|
|
199
|
+
else if (s.view === "files") {
|
|
200
|
+
const dirs = new Map();
|
|
201
|
+
for (const dir of [...s.expanded].sort()) {
|
|
202
|
+
const got = await this.client.tree(t.agent, t.session, dir).catch(() => undefined);
|
|
203
|
+
if (got)
|
|
204
|
+
dirs.set(dir, got.entries);
|
|
205
|
+
}
|
|
206
|
+
const root = s.root ?? (await this.client.folder(t.agent, t.session).then((f) => f.folder ?? f.workspace).catch(() => undefined));
|
|
207
|
+
const file = s.file ? await this.fileRows(t, s.file.path) : undefined;
|
|
208
|
+
if (turn !== this.turn)
|
|
209
|
+
return;
|
|
210
|
+
s.dirs = dirs;
|
|
211
|
+
s.root = root;
|
|
212
|
+
if (s.file)
|
|
213
|
+
s.file.rows = file;
|
|
214
|
+
}
|
|
215
|
+
else if (s.view === "running") {
|
|
216
|
+
const programs = await this.client.programs(t.agent, t.session);
|
|
217
|
+
if (turn !== this.turn)
|
|
218
|
+
return;
|
|
219
|
+
s.programs = programs;
|
|
220
|
+
if (s.stopping)
|
|
221
|
+
for (const id of [...s.stopping])
|
|
222
|
+
if (!programs.some((p) => p.id === id))
|
|
223
|
+
s.stopping.delete(id);
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
const tasks = (await this.client.history(t.agent, t.session, 8)).filter((x) => x.depth === 0).reverse();
|
|
227
|
+
const calls = [];
|
|
228
|
+
for (const task of tasks) {
|
|
229
|
+
if (task.meta?.stats && !task.meta.stats.tools.length)
|
|
230
|
+
continue;
|
|
231
|
+
const got = await this.client.output(task.id).catch(() => undefined);
|
|
232
|
+
let n = 0;
|
|
233
|
+
for (const entry of got?.entries ?? [])
|
|
234
|
+
if (entry.kind === "tool")
|
|
235
|
+
calls.push({ task: task.id, n: n++, entry });
|
|
236
|
+
}
|
|
237
|
+
if (turn !== this.turn)
|
|
238
|
+
return;
|
|
239
|
+
s.calls = calls;
|
|
240
|
+
}
|
|
241
|
+
s.error = undefined;
|
|
242
|
+
}
|
|
243
|
+
catch (e) {
|
|
244
|
+
if (turn !== this.turn)
|
|
245
|
+
return;
|
|
246
|
+
s.error = errText(e);
|
|
247
|
+
}
|
|
248
|
+
s.loading = undefined;
|
|
249
|
+
this.build();
|
|
250
|
+
}
|
|
251
|
+
async fileRows(t, file) {
|
|
252
|
+
if (isImageFile(file)) {
|
|
253
|
+
const got = await this.client.workspaceImage(t.agent, file, t.session).catch(() => undefined);
|
|
254
|
+
if (!got)
|
|
255
|
+
return [{ text: c.gray("this picture cannot be shown") }];
|
|
256
|
+
const p = await prepare(got.bytes, got.mime, path.basename(file));
|
|
257
|
+
// As half blocks: a real picture can only be drawn at the left edge of the screen.
|
|
258
|
+
const blocks = p.pixels ? halfBlocks(p.pixels, Math.max(8, Math.min(this.width - 3, 80)), 30) : [];
|
|
259
|
+
return [...blocks.map((text) => ({ text })), { text: c.gray(`${p.widthPx}×${p.heightPx}`) }];
|
|
260
|
+
}
|
|
261
|
+
const got = await this.client.file(t.agent, t.session, file);
|
|
262
|
+
if (got.binary || got.text === undefined)
|
|
263
|
+
return [{ text: c.gray(`a binary file, ${size(got.size)}`) }];
|
|
264
|
+
const lines = highlightCode(plain(got.text.replace(/\n$/, "")), path.extname(file).slice(1) || path.basename(file).toLowerCase());
|
|
265
|
+
const digits = String(lines.length).length;
|
|
266
|
+
const rows = lines.map((text, i) => ({ text, gutter: c.gray(`${String(i + 1).padStart(digits)} `), wrap: true }));
|
|
267
|
+
if (got.truncated)
|
|
268
|
+
rows.push({ text: c.gray(`… the first 256 KB of ${size(got.size)}`) });
|
|
269
|
+
return rows;
|
|
270
|
+
}
|
|
271
|
+
// ---- drawing ---------------------------------------------------------------------------------
|
|
272
|
+
go(change, top = true) {
|
|
273
|
+
const s = this.state;
|
|
274
|
+
if (!s)
|
|
275
|
+
return;
|
|
276
|
+
change(s);
|
|
277
|
+
if (top)
|
|
278
|
+
this.scroll.scrollToStart();
|
|
279
|
+
void this.load(false);
|
|
280
|
+
this.build();
|
|
281
|
+
}
|
|
282
|
+
back(label, undo) {
|
|
283
|
+
return { text: `${c.accent("‹ back")} ${c.gray("·")} ${c.bold(label)}`, run: () => this.go(undo) };
|
|
284
|
+
}
|
|
285
|
+
build() {
|
|
286
|
+
const s = this.state;
|
|
287
|
+
const rows = [];
|
|
288
|
+
if (!s)
|
|
289
|
+
rows.push({ text: c.gray("Switch to a conversation tab: this pane shows the folder a conversation works in."), wrap: true });
|
|
290
|
+
else if (s.error)
|
|
291
|
+
rows.push({ text: c.red(s.error), wrap: true });
|
|
292
|
+
else if (s.view === "changes")
|
|
293
|
+
this.changesRows(s, rows);
|
|
294
|
+
else if (s.view === "files")
|
|
295
|
+
this.filesRows(s, rows);
|
|
296
|
+
else if (s.view === "running")
|
|
297
|
+
this.runningRows(s, rows);
|
|
298
|
+
else
|
|
299
|
+
this.outputRows(s, rows);
|
|
300
|
+
this.rows.rows = rows;
|
|
301
|
+
this.redraw();
|
|
302
|
+
}
|
|
303
|
+
/** What the owner's last message changed in the folder: the same list and diffs, between the states pine recorded before and after it. */
|
|
304
|
+
turnRows(s, rows) {
|
|
305
|
+
const t = s.turn;
|
|
306
|
+
if (s.diff) {
|
|
307
|
+
const file = s.diff.file;
|
|
308
|
+
rows.push(this.back(file, (x) => (x.diff = undefined)));
|
|
309
|
+
rows.push({ text: c.gray("what your last message changed in this file") });
|
|
310
|
+
rows.push({ text: "" });
|
|
311
|
+
if (!s.diff.lines)
|
|
312
|
+
rows.push({ text: c.gray("reading …") });
|
|
313
|
+
else if (!s.diff.lines.length)
|
|
314
|
+
rows.push({ text: c.gray("nothing to show for this file (a binary file, or no difference any more)") });
|
|
315
|
+
else
|
|
316
|
+
for (const text of s.diff.lines)
|
|
317
|
+
rows.push({ text, wrap: true });
|
|
318
|
+
if (s.diff.truncated)
|
|
319
|
+
rows.push({ text: c.gray("… the diff goes on; this is its first 200 000 characters") });
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
rows.push({ text: `${c.bold("by your last message")} ${c.gray("·")} ${c.accent("▸ since the last commit")}`, run: () => this.go((x) => ((x.scope = undefined), (x.diff = undefined))) });
|
|
323
|
+
if (!t)
|
|
324
|
+
return void rows.push({ text: c.gray(s.loading ?? "") });
|
|
325
|
+
if (t.prompt)
|
|
326
|
+
rows.push({ text: c.gray(`“${plain(t.prompt).replace(/\s+/g, " ").trim()}”`), wrap: true });
|
|
327
|
+
rows.push({ text: "" });
|
|
328
|
+
if (t.note)
|
|
329
|
+
return void rows.push({ text: c.gray(plain(t.note)), wrap: true });
|
|
330
|
+
if (!t.files.length)
|
|
331
|
+
return void rows.push({ text: c.gray("It changed no file here. Files that git ignores and files over 2 MB are not recorded."), wrap: true });
|
|
332
|
+
const letter = { M: c.yellow, A: c.green, D: c.red };
|
|
333
|
+
for (const f of t.files) {
|
|
334
|
+
const n = counts(f);
|
|
335
|
+
rows.push({ text: `${(letter[f.status] ?? c.gray)(f.status === "A" ? "+" : f.status)} ${plain(f.path)}${n ? ` ${n}` : ""}`, run: () => this.go((x) => (x.diff = { file: f.path })) });
|
|
336
|
+
}
|
|
337
|
+
if (t.more)
|
|
338
|
+
rows.push({ text: c.gray("… and more; the first 300 are listed") });
|
|
339
|
+
rows.push({ text: "" });
|
|
340
|
+
rows.push({ text: c.gray("/undo takes the message back and restores these files; /redo brings both back."), wrap: true });
|
|
341
|
+
}
|
|
342
|
+
changesRows(s, rows) {
|
|
343
|
+
if (s.scope === "turn")
|
|
344
|
+
return this.turnRows(s, rows);
|
|
345
|
+
if (!s.diff)
|
|
346
|
+
rows.push({ text: `${c.bold("since the last commit")} ${c.gray("·")} ${c.accent("▸ by your last message")}`, run: () => this.go((x) => ((x.scope = "turn"), (x.diff = undefined))) });
|
|
347
|
+
const ch = s.changes;
|
|
348
|
+
if (!ch)
|
|
349
|
+
return void rows.push({ text: c.gray(s.loading ?? "") });
|
|
350
|
+
if (!ch.repo)
|
|
351
|
+
return void rows.push({ text: c.gray("This folder is not in a git repository, so there are no changes to show. Files lists what is in it."), wrap: true });
|
|
352
|
+
if (s.diff) {
|
|
353
|
+
const file = s.diff.file;
|
|
354
|
+
const entry = ch.files.find((f) => f.path === file);
|
|
355
|
+
rows.push(this.back(file, (x) => (x.diff = undefined)));
|
|
356
|
+
if (entry)
|
|
357
|
+
rows.push({ text: c.gray(`${STATUS[entry.status] ?? entry.status}${counts(entry) ? ` · ${counts(entry)}` : ""}`) });
|
|
358
|
+
rows.push({ text: "" });
|
|
359
|
+
if (!s.diff.lines)
|
|
360
|
+
rows.push({ text: c.gray("reading …") });
|
|
361
|
+
else if (!s.diff.lines.length)
|
|
362
|
+
rows.push({ text: c.gray(entry ? "nothing to show for this file (a folder, or a binary file)" : "no difference to the last commit any more") });
|
|
363
|
+
else
|
|
364
|
+
for (const text of s.diff.lines)
|
|
365
|
+
rows.push({ text, wrap: true });
|
|
366
|
+
if (s.diff.truncated)
|
|
367
|
+
rows.push({ text: c.gray("… the diff goes on; this is its first 200 000 characters") });
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (!ch.files.length)
|
|
371
|
+
return void rows.push({ text: c.gray("Nothing has changed since the last commit.") });
|
|
372
|
+
const letter = { M: c.yellow, A: c.green, "?": c.green, D: c.red, R: c.cyan };
|
|
373
|
+
for (const f of ch.files) {
|
|
374
|
+
const n = counts(f);
|
|
375
|
+
rows.push({ text: `${(letter[f.status] ?? c.gray)(f.status === "?" ? "+" : f.status)} ${plain(f.path)}${n ? ` ${n}` : ""}`, run: () => this.go((x) => (x.diff = { file: f.path })) });
|
|
376
|
+
}
|
|
377
|
+
if (ch.more)
|
|
378
|
+
rows.push({ text: c.gray("… and more; the first 300 are listed") });
|
|
379
|
+
}
|
|
380
|
+
filesRows(s, rows) {
|
|
381
|
+
if (s.file) {
|
|
382
|
+
rows.push(this.back(s.file.path, (x) => (x.file = undefined)));
|
|
383
|
+
rows.push({ text: "" });
|
|
384
|
+
rows.push(...(s.file.rows ?? [{ text: c.gray("reading …") }]));
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
if (!s.dirs.has(""))
|
|
388
|
+
return void rows.push({ text: c.gray(s.loading ?? "this folder cannot be read") });
|
|
389
|
+
const walk = (dir, depth) => {
|
|
390
|
+
for (const e of s.dirs.get(dir) ?? []) {
|
|
391
|
+
const rel = dir ? `${dir}/${e.name}` : e.name;
|
|
392
|
+
const pad = " ".repeat(depth);
|
|
393
|
+
if (e.dir) {
|
|
394
|
+
const open = s.expanded.has(rel);
|
|
395
|
+
rows.push({ text: `${pad}${c.accent(open ? "▾" : "▸")} ${c.bold(plain(e.name))}/`, run: () => this.go((x) => void (open ? x.expanded.delete(rel) : x.expanded.add(rel)), false) });
|
|
396
|
+
if (open)
|
|
397
|
+
walk(rel, depth + 1);
|
|
398
|
+
}
|
|
399
|
+
else
|
|
400
|
+
rows.push({ text: `${pad} ${plain(e.name)} ${c.gray(size(e.size))}`, run: () => this.go((x) => (x.file = { path: rel })) });
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
walk("", 0);
|
|
404
|
+
if (!rows.length)
|
|
405
|
+
rows.push({ text: c.gray("This folder is empty.") });
|
|
406
|
+
}
|
|
407
|
+
/** A server or another program that the conversation's commands started and that is still there, each with a way to stop it. */
|
|
408
|
+
runningRows(s, rows) {
|
|
409
|
+
if (!s.programs)
|
|
410
|
+
return void rows.push({ text: c.gray(s.loading ?? "") });
|
|
411
|
+
if (!s.programs.length)
|
|
412
|
+
return void rows.push({ text: c.gray("Nothing is running. A server or another program that this conversation's commands start and leave running is listed here, and can be stopped from here."), wrap: true });
|
|
413
|
+
for (const p of s.programs) {
|
|
414
|
+
const stopping = s.stopping?.has(p.id);
|
|
415
|
+
rows.push({ text: `${stopping ? c.yellow("●") : c.green("●")} ${c.bold(plain(programName(p.command, 200)))}`, wrap: true });
|
|
416
|
+
rows.push({ text: c.gray(` ${[p.background ? "in the background" : "", p.ports.length ? `port ${p.ports.join(", ")}` : "", `running for ${since(p.seconds)}`, p.processes > 1 ? `${p.processes} processes` : ""].filter(Boolean).join(" · ")}`) });
|
|
417
|
+
// A command the agent put into the background: where what it prints goes. The agent is told when it ends.
|
|
418
|
+
if (p.output)
|
|
419
|
+
rows.push({ text: c.gray(plain(p.output)), gutter: c.gray(" output in "), wrap: true });
|
|
420
|
+
if (stopping)
|
|
421
|
+
rows.push({ text: c.gray(" stopping …") });
|
|
422
|
+
else
|
|
423
|
+
rows.push({ text: ` ${c.red("■ stop")}`, run: () => void this.stopProgram(p.id) });
|
|
424
|
+
rows.push({ text: "" });
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
async stopProgram(id) {
|
|
428
|
+
const t = this.target();
|
|
429
|
+
const s = this.state;
|
|
430
|
+
if (!t || !s)
|
|
431
|
+
return;
|
|
432
|
+
(s.stopping ??= new Set()).add(id);
|
|
433
|
+
this.build();
|
|
434
|
+
try {
|
|
435
|
+
await this.client.stopProgram(t.agent, t.session, id);
|
|
436
|
+
}
|
|
437
|
+
catch (e) {
|
|
438
|
+
s.stopping.delete(id);
|
|
439
|
+
s.error = errText(e);
|
|
440
|
+
}
|
|
441
|
+
// It ends a moment after it was asked to.
|
|
442
|
+
this.touch();
|
|
443
|
+
setTimeout(() => void this.load(false), 2500).unref?.();
|
|
444
|
+
}
|
|
445
|
+
outputRows(s, rows) {
|
|
446
|
+
if (!s.calls)
|
|
447
|
+
return void rows.push({ text: c.gray(s.loading ?? "") });
|
|
448
|
+
const mark = (e) => (e.ok === undefined ? c.yellow("●") : e.ok ? c.green("✓") : c.red("✗"));
|
|
449
|
+
const open = s.call && s.calls.find((x) => x.task === s.call.task && x.n === s.call.n);
|
|
450
|
+
if (open) {
|
|
451
|
+
const e = open.entry;
|
|
452
|
+
rows.push(this.back(e.tool ?? "tool", (x) => (x.call = undefined)));
|
|
453
|
+
rows.push({ text: `${mark(e)} ${c.gray(`task #${open.task}${e.ms !== undefined ? ` · ${took(e.ms)}` : ""}${e.ok === undefined ? " · still running" : ""}`)}` });
|
|
454
|
+
if (e.subject)
|
|
455
|
+
rows.push({ text: c.yellow(plain(e.subject).replace(/\n/g, " ")), wrap: true });
|
|
456
|
+
rows.push({ text: "" });
|
|
457
|
+
const text = plain(e.result ?? "");
|
|
458
|
+
const body = /^diff --git /m.test(text) ? diffLines(text) : text.split("\n");
|
|
459
|
+
if (!text.trim())
|
|
460
|
+
rows.push({ text: c.gray(e.ok === undefined ? "no output yet" : "(no output)") });
|
|
461
|
+
else
|
|
462
|
+
for (const line of body.slice(0, 4000))
|
|
463
|
+
rows.push({ text: e.ok === false ? c.red(line) : line, wrap: true });
|
|
464
|
+
if (body.length > 4000)
|
|
465
|
+
rows.push({ text: c.gray(`… ${body.length - 4000} more lines`) });
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
if (!s.calls.length)
|
|
469
|
+
return void rows.push({ text: c.gray("No tool has run in this conversation yet. What the agent's commands and other tools print is listed here."), wrap: true });
|
|
470
|
+
// The newest on top: that is the one looked for.
|
|
471
|
+
[...s.calls].reverse().forEach((call) => {
|
|
472
|
+
const e = call.entry;
|
|
473
|
+
rows.push({ text: `${mark(e)} ${c.bold(e.tool ?? "")} ${c.gray(plain(e.subject ?? "").replace(/\s+/g, " "))}${e.ms !== undefined ? c.gray(` ${took(e.ms)}`) : ""}`, run: () => this.go((x) => (x.call = { task: call.task, n: call.n })) });
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
renderHead(width) {
|
|
477
|
+
this.width = width;
|
|
478
|
+
const s = this.state;
|
|
479
|
+
let text = " ";
|
|
480
|
+
let at = 1;
|
|
481
|
+
this.hits = [];
|
|
482
|
+
const add = (styled, run) => {
|
|
483
|
+
const w = visibleWidth(styled);
|
|
484
|
+
if (run)
|
|
485
|
+
this.hits.push({ from: at, to: at + w, run });
|
|
486
|
+
text += styled;
|
|
487
|
+
at += w;
|
|
488
|
+
};
|
|
489
|
+
for (const view of PANE_VIEWS) {
|
|
490
|
+
const label = ` ${view[0].toUpperCase()}${view.slice(1)} `;
|
|
491
|
+
add(s?.view === view ? c.bgAccent(label) : c.gray(label), () => this.show(view));
|
|
492
|
+
add(" ");
|
|
493
|
+
}
|
|
494
|
+
const right = [
|
|
495
|
+
{ styled: c.gray(" ↻ "), run: () => this.show() },
|
|
496
|
+
{ styled: c.gray(" × "), run: () => this.close() },
|
|
497
|
+
];
|
|
498
|
+
const rightWidth = right.reduce((n, r) => n + visibleWidth(r.styled), 0);
|
|
499
|
+
text += " ".repeat(Math.max(0, width - at - rightWidth));
|
|
500
|
+
at = Math.max(at, width - rightWidth);
|
|
501
|
+
for (const r of right)
|
|
502
|
+
add(r.styled, r.run);
|
|
503
|
+
const where = s?.root ? tilde(s.root) : "";
|
|
504
|
+
const ch = s?.view === "changes" && s.scope !== "turn" ? s.changes : undefined;
|
|
505
|
+
const last = s?.view === "changes" && s.scope === "turn" ? s.turn : undefined;
|
|
506
|
+
// What is asked for first comes first; the folder's name is what gets cut when the pane is narrow.
|
|
507
|
+
const info = [...(last ? [last.files.length ? `${last.files.length}${last.more ? "+" : ""} changed by your last message` : "last message"] : []), ...(ch?.repo ? [`${ch.branch || "no branch"}`, ch.files.length ? `${ch.files.length}${ch.more ? "+" : ""} changed` : "clean"] : []), ...(s?.view === "output" && s.calls ? [`${s.calls.length} tool call${s.calls.length === 1 ? "" : "s"}`] : []), ...(s?.view === "running" && s.programs ? [s.programs.length ? `${s.programs.length} running` : "nothing running"] : []), where].filter(Boolean).join(" · ");
|
|
508
|
+
return [truncateToWidth(text, width), truncateToWidth(` ${c.gray(info)}`, width), c.gray("─".repeat(width)), ...this.stepsLines(width)];
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* The steps the agent listed for itself, kept at the top of the pane in every view: in the
|
|
512
|
+
* conversation they are soon far above, behind the tool calls of the work itself.
|
|
513
|
+
*/
|
|
514
|
+
stepsLines(width) {
|
|
515
|
+
const items = this.steps?.() ?? [];
|
|
516
|
+
if (!items.length)
|
|
517
|
+
return [];
|
|
518
|
+
const done = items.filter((t) => t.status === "done").length;
|
|
519
|
+
// Room for six: the one in work stays in view, with what was just done above it.
|
|
520
|
+
const at = Math.max(0, items.findIndex((t) => t.status !== "done"));
|
|
521
|
+
const from = Math.max(0, Math.min(at - 1, items.length - 6));
|
|
522
|
+
const shown = items.slice(from, from + 6);
|
|
523
|
+
const line = (t) => (t.status === "done" ? `${c.green("✓")} ${c.gray(plain(t.text))}` : t.status === "in_progress" ? `${c.yellow("◐")} ${c.bold(plain(t.text))}` : `${c.gray("☐")} ${plain(t.text)}`);
|
|
524
|
+
const more = items.length - shown.length;
|
|
525
|
+
return [
|
|
526
|
+
truncateToWidth(` ${c.gray(`steps · ${done} of ${items.length} done${more > 0 ? ` · ${more} more not shown` : ""}`)}`, width),
|
|
527
|
+
...shown.map((t) => truncateToWidth(` ${line(t)}`, width)),
|
|
528
|
+
c.gray("─".repeat(width)),
|
|
529
|
+
];
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
const STATUS = { M: "changed", A: "added", D: "deleted", R: "renamed", "?": "new, not tracked by git yet" };
|
|
533
|
+
function counts(f) {
|
|
534
|
+
return [f.added ? c.green(`+${f.added}`) : "", f.removed ? c.red(`−${f.removed}`) : ""].filter(Boolean).join(" ");
|
|
535
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The language highlight.js knows for the word after a code block's opening fence, if any. */
|
|
2
|
+
export declare function languageOf(lang: string | undefined): string | undefined;
|
|
3
|
+
/**
|
|
4
|
+
* A code block as lines, coloured when its language is known. A block without a language, or in
|
|
5
|
+
* one highlight.js does not know, comes back as it is: guessing the language colours plain text
|
|
6
|
+
* at random.
|
|
7
|
+
*/
|
|
8
|
+
export declare function highlightCode(code: string, lang?: string): string[];
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
// Colours for code in an answer. highlight.js knows the languages and says what each piece of a
|
|
3
|
+
// text is (a keyword, a string, a removed line of a diff); here that becomes one of the terminal's
|
|
4
|
+
// own sixteen colours, so code follows the terminal's palette like the rest of the UI.
|
|
5
|
+
/** What highlight.js calls a piece of code -> the colour it gets (an SGR foreground code). */
|
|
6
|
+
const COLOUR = {
|
|
7
|
+
keyword: "35", literal: "35", "selector-tag": "35", "meta-keyword": "35", doctag: "35",
|
|
8
|
+
built_in: "36", type: "36", "builtin-name": "36", attr: "36", attribute: "36", variable: "36", "template-variable": "36", "selector-class": "36", "selector-id": "36", "selector-attr": "36", "selector-pseudo": "36",
|
|
9
|
+
string: "32", regexp: "32", "meta-string": "32", addition: "32",
|
|
10
|
+
number: "33", symbol: "33", bullet: "33", link: "33",
|
|
11
|
+
title: "34", section: "34", name: "34",
|
|
12
|
+
comment: "90", meta: "90", quote: "90",
|
|
13
|
+
deletion: "31",
|
|
14
|
+
};
|
|
15
|
+
/** Names a code block may carry that highlight.js knows under another name. */
|
|
16
|
+
const ALIAS = { tsx: "typescript", mts: "typescript", cts: "typescript", jsx: "javascript", mjs: "javascript", cjs: "javascript", jsonc: "json", json5: "json", jsonl: "json", vue: "xml", svelte: "xml", mdx: "markdown", fish: "bash", shellscript: "bash", terminal: "shell", "objective-c": "objectivec", "c++": "cpp", "c#": "csharp", tf: "ini", hcl: "ini", env: "ini", dotenv: "ini" };
|
|
17
|
+
const ENTITY = { "&": "&", "<": "<", ">": ">", """: '"', "'": "'", "'": "'" };
|
|
18
|
+
let lib;
|
|
19
|
+
function hljs() {
|
|
20
|
+
if (lib === undefined) {
|
|
21
|
+
try {
|
|
22
|
+
lib = createRequire(import.meta.url)("highlight.js");
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
lib = null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return lib;
|
|
29
|
+
}
|
|
30
|
+
/** highlight.js's answer (text with <span class="hljs-..."> around the pieces) as coloured lines. */
|
|
31
|
+
function toLines(html) {
|
|
32
|
+
const lines = [];
|
|
33
|
+
const open = [];
|
|
34
|
+
const colour = () => open.findLast((x) => x !== undefined);
|
|
35
|
+
let line = "";
|
|
36
|
+
for (const m of html.matchAll(/<span class="([^"]*)">|<\/span>|&(?:amp|lt|gt|quot|#x27|#39);|\n|[^<&\n]+|[<&]/g)) {
|
|
37
|
+
const piece = m[0];
|
|
38
|
+
if (m[1] !== undefined) {
|
|
39
|
+
const now = m[1].split(/\s+/).map((name) => COLOUR[name.replace(/^hljs-/, "")]).find(Boolean);
|
|
40
|
+
open.push(now);
|
|
41
|
+
if (now)
|
|
42
|
+
line += `\x1b[${now}m`;
|
|
43
|
+
}
|
|
44
|
+
else if (piece === "</span>") {
|
|
45
|
+
const was = open.pop();
|
|
46
|
+
if (was)
|
|
47
|
+
line += `\x1b[${colour() ?? "39"}m`;
|
|
48
|
+
}
|
|
49
|
+
else if (piece === "\n") {
|
|
50
|
+
// A colour that runs over the end of a line (a long comment, a string) ends with the line and starts again on the next.
|
|
51
|
+
lines.push(colour() ? `${line}\x1b[39m` : line);
|
|
52
|
+
line = colour() ? `\x1b[${colour()}m` : "";
|
|
53
|
+
}
|
|
54
|
+
else
|
|
55
|
+
line += ENTITY[piece] ?? piece;
|
|
56
|
+
}
|
|
57
|
+
lines.push(colour() ? `${line}\x1b[39m` : line);
|
|
58
|
+
return lines;
|
|
59
|
+
}
|
|
60
|
+
/** The language highlight.js knows for the word after a code block's opening fence, if any. */
|
|
61
|
+
export function languageOf(lang) {
|
|
62
|
+
const word = (lang ?? "").trim().split(/[\s,{:]/)[0].toLowerCase().replace(/^\./, "");
|
|
63
|
+
if (!word)
|
|
64
|
+
return undefined;
|
|
65
|
+
const name = ALIAS[word] ?? word;
|
|
66
|
+
return hljs()?.getLanguage(name) ? name : undefined;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* A code block as lines, coloured when its language is known. A block without a language, or in
|
|
70
|
+
* one highlight.js does not know, comes back as it is: guessing the language colours plain text
|
|
71
|
+
* at random.
|
|
72
|
+
*/
|
|
73
|
+
export function highlightCode(code, lang) {
|
|
74
|
+
const plain = () => code.split("\n");
|
|
75
|
+
if (process.env.NO_COLOR || code.length > 200_000)
|
|
76
|
+
return plain();
|
|
77
|
+
const language = languageOf(lang);
|
|
78
|
+
if (!language)
|
|
79
|
+
return plain();
|
|
80
|
+
try {
|
|
81
|
+
return toLines(hljs().highlight(code, { language, ignoreIllegals: true }).value);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return plain();
|
|
85
|
+
}
|
|
86
|
+
}
|