reasonally 0.2.0 → 0.4.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 +137 -5
- package/dist/agent/accumulate.js +150 -0
- package/dist/agent/client.js +176 -0
- package/dist/agent/compact.js +79 -0
- package/dist/agent/cost.js +22 -0
- package/dist/agent/loop-constants.js +11 -0
- package/dist/agent/loop.js +734 -0
- package/dist/agent/mcp-config.js +72 -0
- package/dist/agent/mcp.js +195 -0
- package/dist/agent/permissions.js +153 -0
- package/dist/agent/prompt.js +112 -0
- package/dist/agent/readonly-command.js +315 -0
- package/dist/agent/session.js +417 -0
- package/dist/agent/sse.js +46 -0
- package/dist/agent/tools/bash.js +196 -0
- package/dist/agent/tools/diff.js +33 -0
- package/dist/agent/tools/files.js +270 -0
- package/dist/agent/tools/index.js +23 -0
- package/dist/agent/tools/paths.js +43 -0
- package/dist/agent/tools/plan.js +101 -0
- package/dist/agent/tools/search.js +288 -0
- package/dist/agent/tools/task.js +42 -0
- package/dist/agent/tools/todo.js +59 -0
- package/dist/agent/tools/types.js +57 -0
- package/dist/agent/types.js +2 -0
- package/dist/api.js +8 -2
- package/dist/catalog.js +12 -0
- package/dist/cli.js +37 -3
- package/dist/config.js +17 -4
- package/dist/login.js +41 -13
- package/dist/tui/app.js +1010 -0
- package/dist/tui/args.js +43 -0
- package/dist/tui/commands.js +42 -0
- package/dist/tui/components.js +479 -0
- package/dist/tui/external-editor.js +103 -0
- package/dist/tui/format.js +124 -0
- package/dist/tui/node-version.js +7 -0
- package/dist/tui/start.js +139 -0
- package/dist/tui/theme.js +36 -0
- package/package.json +8 -3
package/dist/tui/args.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** Flags for bare `reasonally` (the agent). No terminal UI imports: `cli.ts` loads this eagerly. */
|
|
2
|
+
export function parseAgentArgs(argv) {
|
|
3
|
+
const args = {
|
|
4
|
+
model: null,
|
|
5
|
+
yes: false,
|
|
6
|
+
plan: false,
|
|
7
|
+
continue: false,
|
|
8
|
+
resume: false,
|
|
9
|
+
};
|
|
10
|
+
for (let i = 0; i < argv.length; i++) {
|
|
11
|
+
const arg = argv[i];
|
|
12
|
+
if (arg === "--model" || arg === "-m") {
|
|
13
|
+
const value = argv[i + 1];
|
|
14
|
+
if (!value || value.startsWith("-"))
|
|
15
|
+
return { ok: false, error: `${arg} needs a model id.` };
|
|
16
|
+
args.model = value;
|
|
17
|
+
i++;
|
|
18
|
+
}
|
|
19
|
+
else if (arg.startsWith("--model=")) {
|
|
20
|
+
args.model = arg.slice("--model=".length) || null;
|
|
21
|
+
}
|
|
22
|
+
else if (arg === "--yes" || arg === "-y") {
|
|
23
|
+
args.yes = true;
|
|
24
|
+
}
|
|
25
|
+
else if (arg === "--plan") {
|
|
26
|
+
args.plan = true;
|
|
27
|
+
}
|
|
28
|
+
else if (arg === "--continue" || arg === "-c") {
|
|
29
|
+
args.continue = true;
|
|
30
|
+
}
|
|
31
|
+
else if (arg === "--resume" || arg === "-r") {
|
|
32
|
+
args.resume = true;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
return { ok: false, error: `Unknown option "${arg}". See \`reasonally --help\`.` };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (args.plan && args.yes)
|
|
39
|
+
return { ok: false, error: "Pick one of --plan and --yes." };
|
|
40
|
+
return { ok: true, args };
|
|
41
|
+
}
|
|
42
|
+
/** Bare `reasonally` opens the agent only in an interactive terminal; scripts still get help. */
|
|
43
|
+
export const isInteractive = (stdin, stdout) => Boolean(stdin.isTTY && stdout.isTTY);
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export const COMMANDS = [
|
|
2
|
+
{ name: "model", description: "Switch model" },
|
|
3
|
+
{
|
|
4
|
+
name: "mode",
|
|
5
|
+
description: "Switch between asking, auto-accepting edits, planning first, and not asking",
|
|
6
|
+
},
|
|
7
|
+
{ name: "new", description: "Start a new conversation" },
|
|
8
|
+
{ name: "resume", description: "Continue an earlier conversation in this folder" },
|
|
9
|
+
{ name: "compact", description: "Summarize the conversation so far to free up context" },
|
|
10
|
+
{ name: "cost", description: "Show this session's cost and your balance" },
|
|
11
|
+
{ name: "mcp", description: "Show MCP servers and their tools" },
|
|
12
|
+
{ name: "login", description: "Log in again (e.g. after a key was revoked)" },
|
|
13
|
+
{ name: "help", description: "Show commands and keys" },
|
|
14
|
+
{ name: "exit", description: "Quit" },
|
|
15
|
+
];
|
|
16
|
+
/** "/model foo" → { name: "model", arg: "foo" }; null when it isn't a command. */
|
|
17
|
+
export function parseCommand(text) {
|
|
18
|
+
const match = /^\/([a-z-]+)(?:\s+([\s\S]*))?$/i.exec(text.trim());
|
|
19
|
+
if (!match)
|
|
20
|
+
return null;
|
|
21
|
+
return { name: match[1].toLowerCase(), arg: (match[2] ?? "").trim() };
|
|
22
|
+
}
|
|
23
|
+
export const KEYS = [
|
|
24
|
+
["Enter", "send · Shift+Enter or Ctrl+J for a new line"],
|
|
25
|
+
["Esc", "stop the current reply or command"],
|
|
26
|
+
["Shift+Tab", "switch mode"],
|
|
27
|
+
["Ctrl+O", "show the full output of the last step"],
|
|
28
|
+
["Ctrl+G", "edit a waiting plan in your editor"],
|
|
29
|
+
["Ctrl+C", "clear the input · twice to quit"],
|
|
30
|
+
["@", "mention a file"],
|
|
31
|
+
["↑ ↓", "earlier messages"],
|
|
32
|
+
];
|
|
33
|
+
export function helpText() {
|
|
34
|
+
const width = Math.max(...COMMANDS.map((c) => c.name.length)) + 2;
|
|
35
|
+
return [
|
|
36
|
+
"Commands",
|
|
37
|
+
...COMMANDS.map((c) => ` /${c.name.padEnd(width)}${c.description}`),
|
|
38
|
+
"",
|
|
39
|
+
"Keys",
|
|
40
|
+
...KEYS.map(([key, what]) => ` ${key.padEnd(width + 1)}${what}`),
|
|
41
|
+
].join("\n");
|
|
42
|
+
}
|
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import { Container, Input, Markdown, matchesKey, SelectList, truncateToWidth, visibleWidth, wrapTextWithAnsi, } from "@earendil-works/pi-tui";
|
|
2
|
+
import { diffLineKind } from "./format.js";
|
|
3
|
+
import { style, theme } from "./theme.js";
|
|
4
|
+
/** Everything the agent says or does is indented this much; the user's "›" sits in the margin. */
|
|
5
|
+
export const INDENT = " ";
|
|
6
|
+
/** Text nested under a step (its output, reasoning, wrapped options). */
|
|
7
|
+
const NESTED = " ";
|
|
8
|
+
const nestedWidth = (width) => Math.max(10, width - NESTED.length);
|
|
9
|
+
/** "Reasonally · model · folder", shortening the folder from the left to fit one line. */
|
|
10
|
+
export class Header {
|
|
11
|
+
model;
|
|
12
|
+
folder;
|
|
13
|
+
constructor(model, folder) {
|
|
14
|
+
this.model = model;
|
|
15
|
+
this.folder = folder;
|
|
16
|
+
}
|
|
17
|
+
invalidate() { }
|
|
18
|
+
render(width) {
|
|
19
|
+
const prefix = `Reasonally · ${this.model} · `;
|
|
20
|
+
const room = Math.max(12, width - INDENT.length - prefix.length);
|
|
21
|
+
const folder = this.folder.length > room
|
|
22
|
+
? `…${this.folder.slice(this.folder.length - room + 1)}`
|
|
23
|
+
: this.folder;
|
|
24
|
+
return [
|
|
25
|
+
"",
|
|
26
|
+
truncateToWidth(`${INDENT}${theme.brand("Reasonally")}${theme.muted(` · ${this.model} · ${folder}`)}`, width),
|
|
27
|
+
...wrapTextWithAnsi(theme.muted("Ask for anything in this project. / for commands · @ for files · esc to stop"), Math.max(10, width - INDENT.length)).map((l) => INDENT + l),
|
|
28
|
+
];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** Lines of plain or styled text, wrapped to the width, with a hanging indent. */
|
|
32
|
+
export class Lines {
|
|
33
|
+
text;
|
|
34
|
+
indent;
|
|
35
|
+
constructor(text, indent = 0) {
|
|
36
|
+
this.text = text;
|
|
37
|
+
this.indent = indent;
|
|
38
|
+
}
|
|
39
|
+
setText(text) {
|
|
40
|
+
this.text = text;
|
|
41
|
+
}
|
|
42
|
+
invalidate() { }
|
|
43
|
+
render(width) {
|
|
44
|
+
if (!this.text)
|
|
45
|
+
return [];
|
|
46
|
+
const pad = " ".repeat(this.indent);
|
|
47
|
+
const inner = Math.max(10, width - this.indent);
|
|
48
|
+
return this.text
|
|
49
|
+
.split("\n")
|
|
50
|
+
.flatMap((line) => wrapTextWithAnsi(line, inner).map((l) => pad + l));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* A container whose lines never run past the width. Components wrap their own text; this is the
|
|
55
|
+
* backstop, so one layout mistake clips a line instead of crashing the screen.
|
|
56
|
+
*/
|
|
57
|
+
export class Clamped extends Container {
|
|
58
|
+
render(width) {
|
|
59
|
+
return super
|
|
60
|
+
.render(width)
|
|
61
|
+
.map((line) => (visibleWidth(line) > width ? truncateToWidth(line, width) : line));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** One empty line. */
|
|
65
|
+
export const gap = () => ({ render: () => [""], invalidate() { } });
|
|
66
|
+
/** "› your message", wrapped under the marker. */
|
|
67
|
+
export class UserMessage {
|
|
68
|
+
text;
|
|
69
|
+
constructor(text) {
|
|
70
|
+
this.text = text;
|
|
71
|
+
}
|
|
72
|
+
invalidate() { }
|
|
73
|
+
render(width) {
|
|
74
|
+
const lines = this.text
|
|
75
|
+
.split("\n")
|
|
76
|
+
.flatMap((l) => wrapTextWithAnsi(l, Math.max(10, width - 2)));
|
|
77
|
+
return [
|
|
78
|
+
"",
|
|
79
|
+
...lines.map((l, i) => `${i === 0 ? theme.accent("›") : " "} ${theme.user(l)}`),
|
|
80
|
+
"",
|
|
81
|
+
];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** Colors a unified diff: added green, removed red, hunk headers cyan, context dim. */
|
|
85
|
+
export function colorDiffLine(line) {
|
|
86
|
+
switch (diffLineKind(line)) {
|
|
87
|
+
case "added":
|
|
88
|
+
return theme.added(line);
|
|
89
|
+
case "removed":
|
|
90
|
+
return theme.removed(line);
|
|
91
|
+
case "hunk":
|
|
92
|
+
return theme.hunk(line);
|
|
93
|
+
default:
|
|
94
|
+
return theme.faint(line);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const looksLikeDiff = (text) => text.startsWith("@@ ");
|
|
98
|
+
/** Expanded text under a step: diffs colored, anything else dimmed. Capped so it can't flood. */
|
|
99
|
+
function detailLines(detail, width, max = 200) {
|
|
100
|
+
const diff = looksLikeDiff(detail);
|
|
101
|
+
const raw = detail.replace(/\s+$/, "").split("\n");
|
|
102
|
+
const shown = raw.slice(0, max);
|
|
103
|
+
const out = shown.flatMap((line) => wrapTextWithAnsi(diff ? colorDiffLine(line) : theme.faint(line), nestedWidth(width)).map((l) => NESTED + l));
|
|
104
|
+
if (raw.length > max)
|
|
105
|
+
out.push(theme.muted(`${NESTED}… ${raw.length - max} more lines`));
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
/** A tool step: "● Read src/app.ts (120 lines)", expandable with ctrl+o. */
|
|
109
|
+
export class ToolLine {
|
|
110
|
+
id;
|
|
111
|
+
title;
|
|
112
|
+
status = "running";
|
|
113
|
+
summary = null;
|
|
114
|
+
detail = null;
|
|
115
|
+
/** What it's doing right now, while running (helpers report their steps). */
|
|
116
|
+
progress = null;
|
|
117
|
+
expanded = false;
|
|
118
|
+
constructor(id, title) {
|
|
119
|
+
this.id = id;
|
|
120
|
+
this.title = title;
|
|
121
|
+
}
|
|
122
|
+
finish(status, summary, detail) {
|
|
123
|
+
this.status = status;
|
|
124
|
+
this.summary = summary;
|
|
125
|
+
this.detail = detail ?? null;
|
|
126
|
+
}
|
|
127
|
+
get expandable() {
|
|
128
|
+
return Boolean(this.detail?.trim());
|
|
129
|
+
}
|
|
130
|
+
invalidate() { }
|
|
131
|
+
render(width) {
|
|
132
|
+
const dot = this.status === "running"
|
|
133
|
+
? theme.muted("●")
|
|
134
|
+
: this.status === "error"
|
|
135
|
+
? theme.error("●")
|
|
136
|
+
: theme.success("●");
|
|
137
|
+
const text = this.summary ?? (this.progress ? `${this.title} · ${this.progress}` : `${this.title}…`);
|
|
138
|
+
const hint = this.expandable && !this.expanded ? theme.muted(" (ctrl+o)") : "";
|
|
139
|
+
const head = wrapTextWithAnsi(`${text}${hint}`, Math.max(10, width - 4)).map((l, i) => `${INDENT}${i === 0 ? dot : " "} ${this.status === "running" ? theme.muted(l) : l}`);
|
|
140
|
+
return this.expanded && this.detail ? [...head, ...detailLines(this.detail, width)] : head;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/** The model's reasoning: dimmed, showing its last lines while it thinks; collapsed after. */
|
|
144
|
+
export class Reasoning {
|
|
145
|
+
text = "";
|
|
146
|
+
done = false;
|
|
147
|
+
expanded = false;
|
|
148
|
+
started = Date.now();
|
|
149
|
+
seconds = 0;
|
|
150
|
+
append(text) {
|
|
151
|
+
this.text += text;
|
|
152
|
+
}
|
|
153
|
+
finish() {
|
|
154
|
+
if (this.done)
|
|
155
|
+
return;
|
|
156
|
+
this.done = true;
|
|
157
|
+
this.seconds = Math.max(1, Math.round((Date.now() - this.started) / 1000));
|
|
158
|
+
}
|
|
159
|
+
get expandable() {
|
|
160
|
+
return this.done && Boolean(this.text.trim());
|
|
161
|
+
}
|
|
162
|
+
invalidate() { }
|
|
163
|
+
render(width) {
|
|
164
|
+
const body = this.text.trim().split("\n");
|
|
165
|
+
if (!this.done) {
|
|
166
|
+
const tail = body
|
|
167
|
+
.filter((l) => l.trim())
|
|
168
|
+
.flatMap((l) => wrapTextWithAnsi(l, nestedWidth(width)))
|
|
169
|
+
.slice(-3)
|
|
170
|
+
.map((l) => NESTED + theme.faint(style.italic(l)));
|
|
171
|
+
return [`${INDENT}${theme.muted("✻")} ${theme.muted("Thinking…")}`, ...tail];
|
|
172
|
+
}
|
|
173
|
+
const head = `${INDENT}${theme.muted("✻")} ${theme.muted(`Thought for ${this.seconds}s`)}${this.expanded ? "" : theme.muted(" (ctrl+o)")}`;
|
|
174
|
+
if (!this.expanded)
|
|
175
|
+
return [head];
|
|
176
|
+
return [
|
|
177
|
+
head,
|
|
178
|
+
...body.flatMap((l) => wrapTextWithAnsi(l, nestedWidth(width)).map((w) => NESTED + theme.faint(style.italic(w)))),
|
|
179
|
+
];
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/** Styled Markdown for the model's replies. */
|
|
183
|
+
export const markdownTheme = {
|
|
184
|
+
heading: (t) => style.bold(t),
|
|
185
|
+
link: (t) => theme.accent(t),
|
|
186
|
+
linkUrl: (t) => theme.muted(t),
|
|
187
|
+
code: (t) => theme.accent(t),
|
|
188
|
+
codeBlock: (t) => t,
|
|
189
|
+
codeBlockBorder: (t) => theme.muted(t),
|
|
190
|
+
quote: (t) => style.italic(t),
|
|
191
|
+
quoteBorder: (t) => theme.muted(t),
|
|
192
|
+
hr: (t) => theme.muted(t),
|
|
193
|
+
listBullet: (t) => theme.muted(t),
|
|
194
|
+
bold: (t) => style.bold(t),
|
|
195
|
+
italic: (t) => style.italic(t),
|
|
196
|
+
strikethrough: (t) => style.strike(t),
|
|
197
|
+
underline: (t) => style.underline(t),
|
|
198
|
+
};
|
|
199
|
+
export const selectListTheme = {
|
|
200
|
+
selectedPrefix: (t) => theme.accent(t),
|
|
201
|
+
selectedText: (t) => style.bold(t),
|
|
202
|
+
description: (t) => theme.muted(t),
|
|
203
|
+
scrollInfo: (t) => theme.muted(t),
|
|
204
|
+
noMatch: (t) => theme.muted(t),
|
|
205
|
+
};
|
|
206
|
+
/** A box drawn with a left rule: "╭ title", "│ line", "╰ footer". */
|
|
207
|
+
function boxed(title, body, footer, width) {
|
|
208
|
+
const inner = Math.max(10, width - 4);
|
|
209
|
+
const rule = (ch, text) => `${INDENT}${theme.border(ch)} ${truncateToWidth(text, inner)}`;
|
|
210
|
+
return [rule("╭", title), ...body.map((l) => rule("│", l)), rule("╰", footer)];
|
|
211
|
+
}
|
|
212
|
+
const PREVIEW_LINES = 24;
|
|
213
|
+
/**
|
|
214
|
+
* Asks before an edit or command: shows what will happen, then y / a / n. "n" asks what to do
|
|
215
|
+
* instead (empty just stops). Esc declines and stops.
|
|
216
|
+
*/
|
|
217
|
+
export class ApprovalBox {
|
|
218
|
+
request;
|
|
219
|
+
resolve;
|
|
220
|
+
focused = false;
|
|
221
|
+
feedback = null;
|
|
222
|
+
showAll = false;
|
|
223
|
+
constructor(request, resolve) {
|
|
224
|
+
this.request = request;
|
|
225
|
+
this.resolve = resolve;
|
|
226
|
+
}
|
|
227
|
+
/** Commands show in full below, so the box is titled by what it is. */
|
|
228
|
+
get heading() {
|
|
229
|
+
return this.request.kind === "exec" && this.request.tool === "bash"
|
|
230
|
+
? "Run a command"
|
|
231
|
+
: this.request.title;
|
|
232
|
+
}
|
|
233
|
+
/** ctrl+o: show the whole preview. */
|
|
234
|
+
toggleExpanded() {
|
|
235
|
+
this.showAll = !this.showAll;
|
|
236
|
+
}
|
|
237
|
+
invalidate() { }
|
|
238
|
+
handleInput(data) {
|
|
239
|
+
if (this.feedback) {
|
|
240
|
+
if (matchesKey(data, "escape")) {
|
|
241
|
+
this.feedback = null;
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
this.feedback.handleInput(data);
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (matchesKey(data, "escape"))
|
|
248
|
+
return this.resolve({ kind: "no" });
|
|
249
|
+
const key = data.toLowerCase();
|
|
250
|
+
if (key === "y")
|
|
251
|
+
this.resolve({ kind: "yes" });
|
|
252
|
+
else if (key === "a" && this.request.always)
|
|
253
|
+
this.resolve({ kind: "always" });
|
|
254
|
+
else if (key === "n") {
|
|
255
|
+
const input = new Input({
|
|
256
|
+
prompt: "",
|
|
257
|
+
placeholder: "Tell it what to do instead (Enter on empty just stops)",
|
|
258
|
+
});
|
|
259
|
+
input.focused = this.focused;
|
|
260
|
+
input.onSubmit = (value) => this.resolve({ kind: "no", feedback: value.trim() || undefined });
|
|
261
|
+
this.feedback = input;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
render(width) {
|
|
265
|
+
const r = this.request;
|
|
266
|
+
const body = [];
|
|
267
|
+
if (r.reason)
|
|
268
|
+
body.push(theme.warning(r.reason));
|
|
269
|
+
if (r.preview) {
|
|
270
|
+
const lines = r.preview.replace(/\s+$/, "").split("\n");
|
|
271
|
+
const diff = looksLikeDiff(r.preview);
|
|
272
|
+
const limit = this.showAll ? lines.length : PREVIEW_LINES;
|
|
273
|
+
for (const line of lines.slice(0, limit))
|
|
274
|
+
body.push(diff ? colorDiffLine(line) : line);
|
|
275
|
+
if (lines.length > limit) {
|
|
276
|
+
body.push(theme.muted(`… ${lines.length - limit} more lines (ctrl+o to see all)`));
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (this.feedback) {
|
|
280
|
+
this.feedback.focused = this.focused;
|
|
281
|
+
const input = this.feedback.render(Math.max(10, width - 4))[0] ?? "";
|
|
282
|
+
return [
|
|
283
|
+
...boxed(style.bold(this.heading), body, theme.accent("What should it do instead?"), width),
|
|
284
|
+
`${INDENT} ${input}`,
|
|
285
|
+
theme.muted(`${INDENT} Enter to send · empty to just stop · esc to go back`),
|
|
286
|
+
];
|
|
287
|
+
}
|
|
288
|
+
const options = [
|
|
289
|
+
`${style.bold("y")} yes`,
|
|
290
|
+
...(r.always ? [`${style.bold("a")} ${r.always}`] : []),
|
|
291
|
+
`${style.bold("n")} no, and tell it why`,
|
|
292
|
+
].join(theme.muted(" · "));
|
|
293
|
+
const footer = `${theme.accent("Allow?")} ${options}`;
|
|
294
|
+
const lines = boxed(style.bold(this.heading), body, footer, width);
|
|
295
|
+
// On narrow terminals the options wrap rather than vanish.
|
|
296
|
+
if (visibleWidth(footer) > width - 4) {
|
|
297
|
+
lines.pop();
|
|
298
|
+
lines.push(`${INDENT}${theme.border("╰")} ${theme.accent("Allow?")}`);
|
|
299
|
+
for (const opt of [
|
|
300
|
+
`${style.bold("y")} yes`,
|
|
301
|
+
...(r.always ? [`${style.bold("a")} ${r.always}`] : []),
|
|
302
|
+
`${style.bold("n")} no, and tell it why`,
|
|
303
|
+
]) {
|
|
304
|
+
lines.push(...wrapTextWithAnsi(opt, nestedWidth(width)).map((l) => NESTED + l));
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return lines;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
/** A plan, as Markdown under "⏸ Plan": streamed while it's written, then left in the scrollback. */
|
|
311
|
+
export class PlanBlock {
|
|
312
|
+
edited;
|
|
313
|
+
markdown;
|
|
314
|
+
constructor(text, edited = false) {
|
|
315
|
+
this.edited = edited;
|
|
316
|
+
this.markdown = new Markdown(text, INDENT.length, 0, markdownTheme);
|
|
317
|
+
}
|
|
318
|
+
setText(text) {
|
|
319
|
+
this.markdown.setText(text);
|
|
320
|
+
}
|
|
321
|
+
invalidate() {
|
|
322
|
+
this.markdown.invalidate();
|
|
323
|
+
}
|
|
324
|
+
render(width) {
|
|
325
|
+
const head = theme.accent(style.bold(this.edited ? "⏸ Plan (edited)" : "⏸ Plan"));
|
|
326
|
+
return ["", `${INDENT}${head}`, ...this.markdown.render(width)];
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
/** Keys pressed this soon after the plan box appears don't count (they were meant for the input). */
|
|
330
|
+
export const PLAN_ARM_MS = 350;
|
|
331
|
+
/**
|
|
332
|
+
* "Ready to build?" under a presented plan: y builds asking before edits, a auto-accepts edits,
|
|
333
|
+
* n says what to change (empty just stops), ctrl+g edits the plan in your editor, Esc stops.
|
|
334
|
+
*/
|
|
335
|
+
export class PlanApprovalBox {
|
|
336
|
+
resolve;
|
|
337
|
+
onEdit;
|
|
338
|
+
now;
|
|
339
|
+
focused = false;
|
|
340
|
+
/** The user edited the plan: the answer carries their version. */
|
|
341
|
+
edited = false;
|
|
342
|
+
feedback = null;
|
|
343
|
+
armedAt;
|
|
344
|
+
constructor(resolve, onEdit, armMs = PLAN_ARM_MS, now = Date.now) {
|
|
345
|
+
this.resolve = resolve;
|
|
346
|
+
this.onEdit = onEdit;
|
|
347
|
+
this.now = now;
|
|
348
|
+
this.armedAt = now() + armMs;
|
|
349
|
+
}
|
|
350
|
+
invalidate() { }
|
|
351
|
+
handleInput(data) {
|
|
352
|
+
if (this.now() < this.armedAt)
|
|
353
|
+
return;
|
|
354
|
+
if (this.feedback) {
|
|
355
|
+
if (matchesKey(data, "escape")) {
|
|
356
|
+
this.feedback = null;
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
this.feedback.handleInput(data);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (matchesKey(data, "escape"))
|
|
363
|
+
return this.resolve({ kind: "no" });
|
|
364
|
+
if (matchesKey(data, "ctrl+g"))
|
|
365
|
+
return this.onEdit();
|
|
366
|
+
const key = data.toLowerCase();
|
|
367
|
+
if (key === "y")
|
|
368
|
+
this.resolve({ kind: "yes" });
|
|
369
|
+
else if (key === "a")
|
|
370
|
+
this.resolve({ kind: "always" });
|
|
371
|
+
else if (key === "n") {
|
|
372
|
+
const input = new Input({
|
|
373
|
+
prompt: "",
|
|
374
|
+
placeholder: "Say what to change (Enter on empty just stops)",
|
|
375
|
+
});
|
|
376
|
+
input.focused = this.focused;
|
|
377
|
+
input.onSubmit = (value) => this.resolve({ kind: "no", feedback: value.trim() || undefined });
|
|
378
|
+
this.feedback = input;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
render(width) {
|
|
382
|
+
const heading = `${style.bold("Ready to build?")}${this.edited ? ` ${theme.warning("edited")}` : ""}`;
|
|
383
|
+
const body = [
|
|
384
|
+
theme.muted(this.edited ? "It follows your edited version." : "Nothing has changed yet."),
|
|
385
|
+
];
|
|
386
|
+
if (this.feedback) {
|
|
387
|
+
this.feedback.focused = this.focused;
|
|
388
|
+
const input = this.feedback.render(Math.max(10, width - 4))[0] ?? "";
|
|
389
|
+
return [
|
|
390
|
+
...boxed(heading, body, theme.accent("What should change in the plan?"), width),
|
|
391
|
+
`${INDENT} ${input}`,
|
|
392
|
+
theme.muted(`${INDENT} Enter to send · empty to just stop · esc to go back`),
|
|
393
|
+
];
|
|
394
|
+
}
|
|
395
|
+
const options = [
|
|
396
|
+
`${style.bold("y")} yes, ask before edits`,
|
|
397
|
+
`${style.bold("a")} yes, auto-accept edits`,
|
|
398
|
+
`${style.bold("n")} no, keep planning`,
|
|
399
|
+
`${style.bold("ctrl+g")} edit`,
|
|
400
|
+
];
|
|
401
|
+
const footer = `${theme.accent("Build?")} ${options.join(theme.muted(" · "))}`;
|
|
402
|
+
const lines = boxed(heading, body, footer, width);
|
|
403
|
+
// On narrow terminals the options wrap rather than vanish.
|
|
404
|
+
if (visibleWidth(footer) > width - 4) {
|
|
405
|
+
lines.pop();
|
|
406
|
+
lines.push(`${INDENT}${theme.border("╰")} ${theme.accent("Build?")}`);
|
|
407
|
+
for (const opt of options) {
|
|
408
|
+
lines.push(...wrapTextWithAnsi(opt, nestedWidth(width)).map((l) => NESTED + l));
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
return lines;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
/** A filterable list (model picker): type to filter, ↑↓ to move, Enter to pick, Esc to cancel. */
|
|
415
|
+
export class Picker {
|
|
416
|
+
title;
|
|
417
|
+
items;
|
|
418
|
+
maxVisible;
|
|
419
|
+
focused = false;
|
|
420
|
+
filter = new Input({ prompt: "› ", placeholder: "Type to search" });
|
|
421
|
+
list;
|
|
422
|
+
onSelect;
|
|
423
|
+
onCancel;
|
|
424
|
+
constructor(title, items, maxVisible = 10) {
|
|
425
|
+
this.title = title;
|
|
426
|
+
this.items = items;
|
|
427
|
+
this.maxVisible = maxVisible;
|
|
428
|
+
this.list = this.makeList(items);
|
|
429
|
+
}
|
|
430
|
+
makeList(items) {
|
|
431
|
+
const list = new SelectList(items, this.maxVisible, selectListTheme, {
|
|
432
|
+
minPrimaryColumnWidth: 20,
|
|
433
|
+
maxPrimaryColumnWidth: 48,
|
|
434
|
+
});
|
|
435
|
+
list.onSelect = (item) => this.onSelect?.(item.value);
|
|
436
|
+
list.onCancel = () => this.onCancel?.();
|
|
437
|
+
return list;
|
|
438
|
+
}
|
|
439
|
+
/** Puts the cursor on `value` (the current model). */
|
|
440
|
+
select(value) {
|
|
441
|
+
const index = this.items.findIndex((i) => i.value === value);
|
|
442
|
+
if (index >= 0)
|
|
443
|
+
this.list.setSelectedIndex(index);
|
|
444
|
+
}
|
|
445
|
+
invalidate() { }
|
|
446
|
+
handleInput(data) {
|
|
447
|
+
if (matchesKey(data, "up") ||
|
|
448
|
+
matchesKey(data, "down") ||
|
|
449
|
+
matchesKey(data, "enter") ||
|
|
450
|
+
matchesKey(data, "escape") ||
|
|
451
|
+
matchesKey(data, "pageUp") ||
|
|
452
|
+
matchesKey(data, "pageDown")) {
|
|
453
|
+
this.list.handleInput(data);
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
const before = this.filter.getValue();
|
|
457
|
+
this.filter.handleInput(data);
|
|
458
|
+
const after = this.filter.getValue();
|
|
459
|
+
if (after !== before) {
|
|
460
|
+
const words = after.toLowerCase().split(/\s+/).filter(Boolean);
|
|
461
|
+
const items = this.items.filter((i) => {
|
|
462
|
+
const hay = `${i.value} ${i.label} ${i.description ?? ""}`.toLowerCase();
|
|
463
|
+
return words.every((w) => hay.includes(w));
|
|
464
|
+
});
|
|
465
|
+
this.list = this.makeList(items);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
render(width) {
|
|
469
|
+
this.filter.focused = this.focused;
|
|
470
|
+
const inner = Math.max(10, width - INDENT.length);
|
|
471
|
+
return [
|
|
472
|
+
"",
|
|
473
|
+
theme.accent(style.bold(this.title)),
|
|
474
|
+
...this.filter.render(inner),
|
|
475
|
+
...this.list.render(inner),
|
|
476
|
+
theme.muted("↑↓ to move · Enter to pick · Esc to cancel"),
|
|
477
|
+
].map((l) => (l ? INDENT + l : l));
|
|
478
|
+
}
|
|
479
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Editing a plan in the person's own editor (`$VISUAL`, then `$EDITOR`, then nano or vi; Notepad
|
|
3
|
+
* on Windows), the way `git commit` does. The plan goes in a private temporary file, deleted
|
|
4
|
+
* afterwards whatever happens.
|
|
5
|
+
*/
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import { constants } from "node:fs";
|
|
8
|
+
import { access, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { delimiter, join } from "node:path";
|
|
11
|
+
const isWindows = process.platform === "win32";
|
|
12
|
+
/** An editor that returns sooner than this without a change probably didn't wait for you. */
|
|
13
|
+
const QUICK_MS = 1000;
|
|
14
|
+
async function onPath(name, env) {
|
|
15
|
+
for (const dir of (env.PATH ?? "").split(delimiter)) {
|
|
16
|
+
if (!dir)
|
|
17
|
+
continue;
|
|
18
|
+
try {
|
|
19
|
+
await access(join(dir, name), constants.X_OK);
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// keep looking
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
/** The editor command line to run: the person's choice, else one that's sure to exist. */
|
|
29
|
+
export async function editorCommand(env) {
|
|
30
|
+
const chosen = env.VISUAL?.trim() || env.EDITOR?.trim();
|
|
31
|
+
if (chosen)
|
|
32
|
+
return chosen;
|
|
33
|
+
if (isWindows)
|
|
34
|
+
return "notepad";
|
|
35
|
+
return (await onPath("nano", env)) ? "nano" : "vi";
|
|
36
|
+
}
|
|
37
|
+
/** Runs `editor` on `file` with the terminal handed over, until it exits. */
|
|
38
|
+
function runEditor(editor, file, env) {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
// Through the shell, so EDITOR="code --wait" works; the file is passed as $1, never parsed.
|
|
41
|
+
const child = isWindows
|
|
42
|
+
? spawn(editor, [file], { stdio: "inherit", env, shell: true })
|
|
43
|
+
: spawn(env.SHELL || "/bin/sh", ["-c", `${editor} "$1"`, "reasonally-editor", file], {
|
|
44
|
+
stdio: "inherit",
|
|
45
|
+
env,
|
|
46
|
+
});
|
|
47
|
+
child.on("error", reject);
|
|
48
|
+
child.on("exit", (code) => resolve(code));
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/** Opens `text` in the person's editor and reports what they did with it. */
|
|
52
|
+
export async function editInEditor(text, options = {}) {
|
|
53
|
+
const env = options.env ?? process.env;
|
|
54
|
+
const now = options.now ?? Date.now;
|
|
55
|
+
const dir = await mkdtemp(join(tmpdir(), "reasonally-plan-"));
|
|
56
|
+
const file = join(dir, "plan.md");
|
|
57
|
+
// Ctrl+C meant for the editor shouldn't stop us too while it has the terminal.
|
|
58
|
+
const ignore = () => { };
|
|
59
|
+
process.on("SIGINT", ignore);
|
|
60
|
+
try {
|
|
61
|
+
await writeFile(file, `${text}\n`, { mode: 0o600 });
|
|
62
|
+
const editor = await editorCommand(env);
|
|
63
|
+
const started = now();
|
|
64
|
+
let code;
|
|
65
|
+
try {
|
|
66
|
+
code = await runEditor(editor, file, env);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
return { kind: "failed", message: error.message };
|
|
70
|
+
}
|
|
71
|
+
// 126/127: the shell couldn't run it (not found, not executable).
|
|
72
|
+
if (code === 126 || code === 127)
|
|
73
|
+
return { kind: "failed", message: `${editor} didn't start.` };
|
|
74
|
+
if (code !== 0)
|
|
75
|
+
return { kind: "unchanged", quick: false };
|
|
76
|
+
const edited = (await readFile(file, "utf8")).trim();
|
|
77
|
+
if (!edited)
|
|
78
|
+
return { kind: "empty" };
|
|
79
|
+
if (edited === text.trim())
|
|
80
|
+
return { kind: "unchanged", quick: now() - started < QUICK_MS };
|
|
81
|
+
return { kind: "edited", text: edited };
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
process.off("SIGINT", ignore);
|
|
85
|
+
await rm(dir, { recursive: true, force: true });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** How many lines differ between two versions (each line counted once, moved lines aside). */
|
|
89
|
+
export function changedLines(before, after) {
|
|
90
|
+
const left = new Map();
|
|
91
|
+
for (const line of before.split("\n"))
|
|
92
|
+
left.set(line, (left.get(line) ?? 0) + 1);
|
|
93
|
+
let added = 0;
|
|
94
|
+
for (const line of after.split("\n")) {
|
|
95
|
+
const n = left.get(line) ?? 0;
|
|
96
|
+
if (n > 0)
|
|
97
|
+
left.set(line, n - 1);
|
|
98
|
+
else
|
|
99
|
+
added++;
|
|
100
|
+
}
|
|
101
|
+
const removed = [...left.values()].reduce((a, b) => a + b, 0);
|
|
102
|
+
return Math.max(added, removed);
|
|
103
|
+
}
|