reasonally 0.3.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 +20 -7
- package/dist/agent/accumulate.js +4 -0
- package/dist/agent/client.js +3 -1
- package/dist/agent/loop-constants.js +8 -0
- package/dist/agent/loop.js +205 -30
- package/dist/agent/mcp.js +3 -2
- package/dist/agent/permissions.js +39 -2
- package/dist/agent/prompt.js +16 -2
- package/dist/agent/readonly-command.js +315 -0
- package/dist/agent/session.js +232 -11
- package/dist/agent/tools/bash.js +22 -2
- package/dist/agent/tools/index.js +2 -0
- package/dist/agent/tools/plan.js +101 -0
- package/dist/agent/tools/task.js +1 -1
- package/dist/api.js +3 -1
- package/dist/cli.js +1 -1
- package/dist/login.js +41 -13
- package/dist/tui/app.js +168 -27
- package/dist/tui/args.js +12 -1
- package/dist/tui/commands.js +5 -1
- package/dist/tui/components.js +105 -1
- package/dist/tui/external-editor.js +103 -0
- package/dist/tui/format.js +9 -1
- package/dist/tui/start.js +8 -4
- package/package.json +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { bashTool } from "./bash.js";
|
|
2
2
|
import { editTool, readTool, writeTool } from "./files.js";
|
|
3
|
+
import { planTool } from "./plan.js";
|
|
3
4
|
import { findTool, grepTool } from "./search.js";
|
|
4
5
|
import { taskTool } from "./task.js";
|
|
5
6
|
import { todoTool } from "./todo.js";
|
|
@@ -13,6 +14,7 @@ export const BUILTIN_TOOLS = [
|
|
|
13
14
|
findTool,
|
|
14
15
|
todoTool,
|
|
15
16
|
taskTool,
|
|
17
|
+
planTool,
|
|
16
18
|
];
|
|
17
19
|
/** The `tools` array for a chat request. */
|
|
18
20
|
export const toolSchemas = (tools) => tools.map((t) => ({
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { stringArg, ToolInputError } from "./types.js";
|
|
2
|
+
/** The tool the agent presents its plan with, in Plan mode. The loop answers it (see loop.ts). */
|
|
3
|
+
export const PLAN_TOOL = "present_plan";
|
|
4
|
+
export const MAX_PLAN_CHARS = 20_000;
|
|
5
|
+
/** One line for the transcript, the same in every app. */
|
|
6
|
+
export const PLAN_OUTCOME_LABELS = {
|
|
7
|
+
"approved-ask": "Plan approved · ask before edits",
|
|
8
|
+
"approved-auto": "Plan approved · auto-accept edits",
|
|
9
|
+
"sent-back": "Plan sent back",
|
|
10
|
+
"not-approved": "Plan not approved",
|
|
11
|
+
stopped: "Stopped",
|
|
12
|
+
};
|
|
13
|
+
/** The plan's title: its first heading, or else its first line, as plain text. */
|
|
14
|
+
export function planTitle(plan) {
|
|
15
|
+
const lines = plan.split("\n").map((l) => l.trim());
|
|
16
|
+
const line = lines.find((l) => /^#{1,6}\s/.test(l)) ?? lines.find(Boolean) ?? "";
|
|
17
|
+
const plain = line
|
|
18
|
+
.replace(/^#{1,6}\s+/, "")
|
|
19
|
+
.replace(/^(?:[-*+]|\d+[.)])\s+/, "")
|
|
20
|
+
.replace(/[*_`]/g, "")
|
|
21
|
+
.replace(/\[([^\]]*)\]\([^)]*\)/g, "$1")
|
|
22
|
+
.trim();
|
|
23
|
+
return plain.length > 80 ? `${plain.slice(0, 79)}…` : plain || "Plan";
|
|
24
|
+
}
|
|
25
|
+
export const planTool = {
|
|
26
|
+
name: PLAN_TOOL,
|
|
27
|
+
description: "Plan mode only: show the user your plan and ask them to approve it. Write it in Markdown: " +
|
|
28
|
+
"a short title (# …), the goal in a sentence or two, numbered steps naming the files each " +
|
|
29
|
+
"touches, and how you'll check the result. Keep it short enough to read in a minute. Call it " +
|
|
30
|
+
"once, when the plan is complete, as the last call in your reply. The user may approve it " +
|
|
31
|
+
"(you then carry it out), edit it, or send it back with what to change.",
|
|
32
|
+
parameters: {
|
|
33
|
+
type: "object",
|
|
34
|
+
properties: {
|
|
35
|
+
plan: { type: "string", description: "The plan, in Markdown." },
|
|
36
|
+
},
|
|
37
|
+
required: ["plan"],
|
|
38
|
+
additionalProperties: false,
|
|
39
|
+
},
|
|
40
|
+
async prepare(args) {
|
|
41
|
+
const plan = stringArg(args, "plan").trim();
|
|
42
|
+
if (!plan)
|
|
43
|
+
throw new ToolInputError("`plan` is empty. Write the plan in Markdown.");
|
|
44
|
+
if (plan.length > MAX_PLAN_CHARS) {
|
|
45
|
+
throw new ToolInputError(`The plan is ${plan.length.toLocaleString("en-US")} characters; keep it under ` +
|
|
46
|
+
`${MAX_PLAN_CHARS.toLocaleString("en-US")}. Summarize the steps instead of including code.`);
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
kind: "meta",
|
|
50
|
+
paths: [],
|
|
51
|
+
title: "Plan",
|
|
52
|
+
preview: plan,
|
|
53
|
+
// The loop shows the plan and waits for the user's answer instead of running this.
|
|
54
|
+
run: async () => ({ output: plan, summary: "Plan" }),
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* The `plan` string so far from a tool call's streaming arguments (`{"plan": "# Add a…`), or null
|
|
60
|
+
* before it starts. Escapes are decoded; an escape cut off at the end is left out until it's whole.
|
|
61
|
+
*/
|
|
62
|
+
export function partialPlan(args) {
|
|
63
|
+
const key = /"plan"\s*:\s*"/.exec(args);
|
|
64
|
+
if (!key)
|
|
65
|
+
return null;
|
|
66
|
+
let out = "";
|
|
67
|
+
for (let i = key.index + key[0].length; i < args.length; i++) {
|
|
68
|
+
const c = args[i];
|
|
69
|
+
if (c === '"')
|
|
70
|
+
return out;
|
|
71
|
+
if (c !== "\\") {
|
|
72
|
+
out += c;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const next = args[i + 1];
|
|
76
|
+
if (next === undefined)
|
|
77
|
+
return out;
|
|
78
|
+
if (next === "u") {
|
|
79
|
+
const hex = args.slice(i + 2, i + 6);
|
|
80
|
+
if (hex.length < 4)
|
|
81
|
+
return out;
|
|
82
|
+
if (/^[0-9a-fA-F]{4}$/.test(hex))
|
|
83
|
+
out += String.fromCharCode(parseInt(hex, 16));
|
|
84
|
+
i += 5;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const simple = {
|
|
88
|
+
n: "\n",
|
|
89
|
+
t: "\t",
|
|
90
|
+
r: "\r",
|
|
91
|
+
b: "\b",
|
|
92
|
+
f: "\f",
|
|
93
|
+
'"': '"',
|
|
94
|
+
"\\": "\\",
|
|
95
|
+
"/": "/",
|
|
96
|
+
};
|
|
97
|
+
out += simple[next] ?? next;
|
|
98
|
+
i++;
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
package/dist/agent/tools/task.js
CHANGED
|
@@ -36,7 +36,7 @@ export const taskTool = {
|
|
|
36
36
|
kind: "meta",
|
|
37
37
|
paths: [],
|
|
38
38
|
title: `Helper: ${description}`,
|
|
39
|
-
run: () => helpers.run({ description, prompt }, ctx.signal, ctx.progress ?? (() => { })),
|
|
39
|
+
run: () => helpers.run({ description, prompt, callId: ctx.callId }, ctx.signal, ctx.progress ?? (() => { })),
|
|
40
40
|
};
|
|
41
41
|
},
|
|
42
42
|
};
|
package/dist/api.js
CHANGED
|
@@ -31,7 +31,9 @@ async function call(url, init = {}) {
|
|
|
31
31
|
return body;
|
|
32
32
|
}
|
|
33
33
|
const bearer = (apiKey) => ({ authorization: `Bearer ${apiKey}` });
|
|
34
|
-
export function exchangeCode(appUrl,
|
|
34
|
+
export function exchangeCode(appUrl,
|
|
35
|
+
/** `client` names the kind of key ("desktop"); the CLI leaves it out. */
|
|
36
|
+
body) {
|
|
35
37
|
return call(`${appUrl}/api/cli/token`, {
|
|
36
38
|
method: "POST",
|
|
37
39
|
headers: { "content-type": "application/json" },
|
package/dist/cli.js
CHANGED
|
@@ -12,7 +12,7 @@ const HELP = `Run the coding agents you already use on Reasonally models.
|
|
|
12
12
|
|
|
13
13
|
Usage:
|
|
14
14
|
reasonally Start Reasonally's coding agent in this folder
|
|
15
|
-
(--model <id>, --yes, --continue, --resume)
|
|
15
|
+
(--model <id>, --plan, --yes, --continue, --resume)
|
|
16
16
|
reasonally login Log in through your browser (--no-browser: print the link)
|
|
17
17
|
reasonally <agent> [--model <id>] [agent args…]
|
|
18
18
|
reasonally models [search] List models you can use
|
package/dist/login.js
CHANGED
|
@@ -13,6 +13,8 @@ export function authorizeUrl(appUrl, params) {
|
|
|
13
13
|
state: params.state,
|
|
14
14
|
device: params.device,
|
|
15
15
|
});
|
|
16
|
+
if (params.client)
|
|
17
|
+
query.set("client", params.client);
|
|
16
18
|
return `${appUrl}/cli/authorize?${query}`;
|
|
17
19
|
}
|
|
18
20
|
/** Interprets a request to the loopback server. Only `/callback` with our `state` counts. */
|
|
@@ -43,10 +45,35 @@ function openBrowser(url) {
|
|
|
43
45
|
// same
|
|
44
46
|
}
|
|
45
47
|
}
|
|
48
|
+
/** Thrown when the caller's `signal` stops a login that was waiting for the browser. */
|
|
49
|
+
export class LoginCancelled extends Error {
|
|
50
|
+
constructor() {
|
|
51
|
+
super("Login was cancelled.");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** Thrown when the user pressed Cancel on the approval page. */
|
|
55
|
+
export class LoginDenied extends Error {
|
|
56
|
+
constructor() {
|
|
57
|
+
super("Login was cancelled in the browser.");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/** Thrown when nobody approved in the browser in time. */
|
|
61
|
+
export class LoginTimedOut extends Error {
|
|
62
|
+
constructor() {
|
|
63
|
+
super("Timed out waiting for approval in the browser. Run the login again.");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
46
66
|
/** Waits for the browser to come back to the loopback server with a code (or a refusal). */
|
|
47
|
-
function waitForCallback(server, state) {
|
|
67
|
+
function waitForCallback(server, state, { signal, returnTo = "your terminal" } = {}) {
|
|
48
68
|
return new Promise((resolve, reject) => {
|
|
49
|
-
const timer = setTimeout(() => reject(new
|
|
69
|
+
const timer = setTimeout(() => reject(new LoginTimedOut()), LOGIN_TIMEOUT_MS);
|
|
70
|
+
const onAbort = () => {
|
|
71
|
+
clearTimeout(timer);
|
|
72
|
+
reject(new LoginCancelled());
|
|
73
|
+
};
|
|
74
|
+
if (signal?.aborted)
|
|
75
|
+
return onAbort();
|
|
76
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
50
77
|
server.on("request", (req, res) => {
|
|
51
78
|
const result = readCallback(req.url ?? "/", state);
|
|
52
79
|
const reply = (status, title, body) => {
|
|
@@ -56,26 +83,24 @@ function waitForCallback(server, state) {
|
|
|
56
83
|
if (result.kind === "ignore")
|
|
57
84
|
return reply(404, "Not found", "Nothing to see here.");
|
|
58
85
|
clearTimeout(timer);
|
|
86
|
+
signal?.removeEventListener("abort", onAbort);
|
|
59
87
|
if (result.kind === "code") {
|
|
60
|
-
reply(200, "You're logged in",
|
|
88
|
+
reply(200, "You're logged in", `You can close this tab and go back to ${returnTo}.`);
|
|
61
89
|
resolve(result.code);
|
|
62
90
|
}
|
|
63
91
|
else if (result.kind === "denied") {
|
|
64
92
|
reply(200, "Login cancelled", "You can close this tab.");
|
|
65
|
-
reject(new
|
|
93
|
+
reject(new LoginDenied());
|
|
66
94
|
}
|
|
67
95
|
else {
|
|
68
|
-
reply(400, "Login failed",
|
|
96
|
+
reply(400, "Login failed", `This response didn't match the login in ${returnTo}.`);
|
|
69
97
|
reject(new Error("The browser returned an unexpected response. Run the login again."));
|
|
70
98
|
}
|
|
71
99
|
});
|
|
72
100
|
});
|
|
73
101
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
* exchanges it (with the PKCE verifier) for an API key.
|
|
77
|
-
*/
|
|
78
|
-
export async function browserLogin(appUrl, log, { open = true } = {}) {
|
|
102
|
+
export async function browserLogin(appUrl, log, options = {}) {
|
|
103
|
+
const { open = true } = options;
|
|
79
104
|
const pkce = createPkce();
|
|
80
105
|
const server = createServer();
|
|
81
106
|
await new Promise((resolve, reject) => {
|
|
@@ -89,21 +114,24 @@ export async function browserLogin(appUrl, log, { open = true } = {}) {
|
|
|
89
114
|
redirectUri,
|
|
90
115
|
challenge: pkce.challenge,
|
|
91
116
|
state: pkce.state,
|
|
92
|
-
device: hostname().replace(/\.local$/, ""),
|
|
117
|
+
device: options.device ?? hostname().replace(/\.local$/, ""),
|
|
118
|
+
client: options.client,
|
|
93
119
|
});
|
|
120
|
+
options.onUrl?.(url);
|
|
94
121
|
if (open) {
|
|
95
122
|
log("Opening your browser to approve this computer…");
|
|
96
123
|
log(`If it doesn't open, visit:\n\n ${url}\n`);
|
|
97
|
-
openBrowser(url);
|
|
124
|
+
(options.openUrl ?? openBrowser)(url);
|
|
98
125
|
}
|
|
99
126
|
else {
|
|
100
127
|
log(`Open this link in the browser where you're signed in, on this computer:\n\n ${url}\n`);
|
|
101
128
|
}
|
|
102
|
-
const code = await waitForCallback(server, pkce.state);
|
|
129
|
+
const code = await waitForCallback(server, pkce.state, options);
|
|
103
130
|
return await exchangeCode(appUrl, {
|
|
104
131
|
code,
|
|
105
132
|
code_verifier: pkce.verifier,
|
|
106
133
|
redirect_uri: redirectUri,
|
|
134
|
+
client: options.client,
|
|
107
135
|
});
|
|
108
136
|
}
|
|
109
137
|
finally {
|
package/dist/tui/app.js
CHANGED
|
@@ -9,10 +9,12 @@ import { tildePath } from "../agent/tools/paths.js";
|
|
|
9
9
|
import { formatTodos } from "../agent/tools/todo.js";
|
|
10
10
|
import { FileTracker } from "../agent/tools/types.js";
|
|
11
11
|
import { commandEnv } from "../agent/tools/bash.js";
|
|
12
|
-
import { listSessions, loadSession, SessionWriter, timeAgo, } from "../agent/session.js";
|
|
12
|
+
import { listSessions, loadSession, sessionHolder, SessionWriter, stepRecord, timeAgo, } from "../agent/session.js";
|
|
13
|
+
import { PLAN_OUTCOME_LABELS, PLAN_TOOL } from "../agent/tools/plan.js";
|
|
14
|
+
import { changedLines, editInEditor } from "./external-editor.js";
|
|
13
15
|
import { COMMANDS, helpText, parseCommand } from "./commands.js";
|
|
14
|
-
import { ApprovalBox, Clamped, gap, Header, INDENT, Lines, markdownTheme, Picker, Reasoning, selectListTheme, ToolLine, UserMessage, } from "./components.js";
|
|
15
|
-
import { compactionNote, describeError, formatTokens, modelChoice, retryMessage, sortModels, statusLine, } from "./format.js";
|
|
16
|
+
import { ApprovalBox, Clamped, PlanApprovalBox, PlanBlock, gap, Header, INDENT, Lines, markdownTheme, Picker, Reasoning, selectListTheme, ToolLine, UserMessage, } from "./components.js";
|
|
17
|
+
import { compactionNote, describeError, formatTokens, modelChoice, openElsewhere, retryMessage, sortModels, modeStatus, statusLine, } from "./format.js";
|
|
16
18
|
import { style, theme } from "./theme.js";
|
|
17
19
|
export class App {
|
|
18
20
|
options;
|
|
@@ -56,6 +58,12 @@ export class App {
|
|
|
56
58
|
reply = null;
|
|
57
59
|
reasoning = null;
|
|
58
60
|
toolLines = new Map();
|
|
61
|
+
/** The plan being written or waiting for an answer, in the transcript. */
|
|
62
|
+
planBlock = null;
|
|
63
|
+
/** How the last presented plan ended, for the words at the end of the turn. */
|
|
64
|
+
lastPlanOutcome = null;
|
|
65
|
+
/** The plan is open in the person's editor. */
|
|
66
|
+
editingPlan = false;
|
|
59
67
|
constructor(options) {
|
|
60
68
|
this.options = options;
|
|
61
69
|
this.session = options.session;
|
|
@@ -139,7 +147,7 @@ export class App {
|
|
|
139
147
|
return false;
|
|
140
148
|
}
|
|
141
149
|
if (this.picker || this.approval) {
|
|
142
|
-
if (matchesKey(data, "ctrl+o") && this.approval) {
|
|
150
|
+
if (matchesKey(data, "ctrl+o") && this.approval instanceof ApprovalBox) {
|
|
143
151
|
this.approval.toggleExpanded();
|
|
144
152
|
this.tui.requestRender();
|
|
145
153
|
return true;
|
|
@@ -283,13 +291,34 @@ export class App {
|
|
|
283
291
|
break;
|
|
284
292
|
case "tool_call_streaming":
|
|
285
293
|
this.reasoning?.finish();
|
|
286
|
-
this.setLoader(`Preparing ${event.name}…`);
|
|
294
|
+
this.setLoader(event.name === PLAN_TOOL ? "Writing the plan…" : `Preparing ${event.name}…`);
|
|
295
|
+
break;
|
|
296
|
+
case "plan_draft":
|
|
297
|
+
this.reasoning?.finish();
|
|
298
|
+
if (this.planBlock)
|
|
299
|
+
this.planBlock.setText(event.text);
|
|
300
|
+
else {
|
|
301
|
+
this.planBlock = new PlanBlock(event.text);
|
|
302
|
+
this.add(this.planBlock, "text");
|
|
303
|
+
}
|
|
304
|
+
this.setLoader("Writing the plan…");
|
|
305
|
+
break;
|
|
306
|
+
case "plan_draft_dropped":
|
|
307
|
+
if (this.planBlock)
|
|
308
|
+
this.transcript.removeChild(this.planBlock);
|
|
309
|
+
this.planBlock = null;
|
|
310
|
+
break;
|
|
311
|
+
case "mode_changed":
|
|
312
|
+
// The agent switched it (the plan was approved); the status line shows it below.
|
|
287
313
|
break;
|
|
288
314
|
case "model_done":
|
|
289
315
|
this.reasoning?.finish();
|
|
290
316
|
this.reply = null;
|
|
291
317
|
break;
|
|
292
318
|
case "tool_start": {
|
|
319
|
+
// A plan shows as itself (above), and its outcome as a line once answered.
|
|
320
|
+
if (event.name === PLAN_TOOL)
|
|
321
|
+
break;
|
|
293
322
|
const line = new ToolLine(event.id, event.title);
|
|
294
323
|
this.toolLines.set(event.id, line);
|
|
295
324
|
this.add(line, "steps");
|
|
@@ -308,18 +337,17 @@ export class App {
|
|
|
308
337
|
this.add(line, "steps");
|
|
309
338
|
}
|
|
310
339
|
const { result } = event;
|
|
311
|
-
|
|
340
|
+
const feedback = result.plan?.feedback;
|
|
341
|
+
line.finish(result.isError ? "error" : "done", feedback ? `${result.summary} · ${feedback}` : result.summary, result.detail);
|
|
342
|
+
if (event.name === PLAN_TOOL) {
|
|
343
|
+
this.planBlock = null;
|
|
344
|
+
this.lastPlanOutcome = result.plan?.outcome ?? null;
|
|
345
|
+
}
|
|
312
346
|
if (event.name === "todo" && this.todos.length) {
|
|
313
347
|
line.detail = formatTodos(this.todos);
|
|
314
348
|
line.expanded = true;
|
|
315
349
|
}
|
|
316
|
-
this.writer?.append(
|
|
317
|
-
type: "step",
|
|
318
|
-
id: event.id,
|
|
319
|
-
title: event.title,
|
|
320
|
-
summary: result.summary,
|
|
321
|
-
isError: Boolean(result.isError),
|
|
322
|
-
});
|
|
350
|
+
this.writer?.append(stepRecord(event.id, event.title, result));
|
|
323
351
|
if (line.expandable)
|
|
324
352
|
this.lastExpandable = line;
|
|
325
353
|
this.setLoader("Thinking…");
|
|
@@ -343,8 +371,12 @@ export class App {
|
|
|
343
371
|
case "done":
|
|
344
372
|
if (event.reason === "aborted")
|
|
345
373
|
this.notice(theme.muted("Stopped. What should it do instead?"));
|
|
346
|
-
if (event.reason === "declined")
|
|
347
|
-
this.notice(theme.muted(
|
|
374
|
+
if (event.reason === "declined") {
|
|
375
|
+
this.notice(theme.muted(this.lastPlanOutcome === "not-approved"
|
|
376
|
+
? "Plan not approved. Say what should change, or press Shift+Tab to leave Plan mode."
|
|
377
|
+
: "Okay, I won't. What should it do instead?"));
|
|
378
|
+
}
|
|
379
|
+
this.lastPlanOutcome = null;
|
|
348
380
|
break;
|
|
349
381
|
}
|
|
350
382
|
this.updateStatus();
|
|
@@ -366,6 +398,8 @@ export class App {
|
|
|
366
398
|
return next;
|
|
367
399
|
}
|
|
368
400
|
showApproval(request, signal) {
|
|
401
|
+
if (request.tool === PLAN_TOOL)
|
|
402
|
+
return this.showPlanApproval(request, signal);
|
|
369
403
|
return new Promise((resolve) => {
|
|
370
404
|
let done = false;
|
|
371
405
|
const finish = (decision) => {
|
|
@@ -396,6 +430,90 @@ export class App {
|
|
|
396
430
|
signal.addEventListener("abort", onAbort, { once: true });
|
|
397
431
|
});
|
|
398
432
|
}
|
|
433
|
+
/**
|
|
434
|
+
* "Ready to build?" under the plan. Ctrl+G hands the terminal to the person's editor and comes
|
|
435
|
+
* back with their version, which the answer then carries.
|
|
436
|
+
*/
|
|
437
|
+
showPlanApproval(request, signal) {
|
|
438
|
+
const original = request.preview ?? "";
|
|
439
|
+
// The draft becomes the plan as presented (or it appears now, if it wasn't streamed).
|
|
440
|
+
if (this.planBlock)
|
|
441
|
+
this.planBlock.setText(original);
|
|
442
|
+
else {
|
|
443
|
+
this.planBlock = new PlanBlock(original);
|
|
444
|
+
this.add(this.planBlock, "text");
|
|
445
|
+
}
|
|
446
|
+
return new Promise((resolve) => {
|
|
447
|
+
let done = false;
|
|
448
|
+
let edited = null;
|
|
449
|
+
const finish = (decision) => {
|
|
450
|
+
if (done)
|
|
451
|
+
return;
|
|
452
|
+
done = true;
|
|
453
|
+
signal.removeEventListener("abort", onAbort);
|
|
454
|
+
if (this.approval === box) {
|
|
455
|
+
this.activity.removeChild(box);
|
|
456
|
+
this.approval = null;
|
|
457
|
+
}
|
|
458
|
+
this.tui.setFocus(this.editor);
|
|
459
|
+
if (!signal.aborted) {
|
|
460
|
+
if (decision.kind !== "no")
|
|
461
|
+
this.showLoader("Starting on the plan…");
|
|
462
|
+
else if (decision.feedback)
|
|
463
|
+
this.showLoader("Revising the plan…");
|
|
464
|
+
}
|
|
465
|
+
this.tui.requestRender();
|
|
466
|
+
resolve(edited !== null ? { ...decision, plan: edited } : decision);
|
|
467
|
+
};
|
|
468
|
+
const onAbort = () => finish({ kind: "no" });
|
|
469
|
+
const edit = async () => {
|
|
470
|
+
if (this.editingPlan || done)
|
|
471
|
+
return;
|
|
472
|
+
this.editingPlan = true;
|
|
473
|
+
const before = edited ?? original;
|
|
474
|
+
let outcome;
|
|
475
|
+
this.tui.stop();
|
|
476
|
+
try {
|
|
477
|
+
outcome = await (this.options.editPlan ?? editInEditor)(before);
|
|
478
|
+
}
|
|
479
|
+
catch (error) {
|
|
480
|
+
outcome = { kind: "failed", message: error.message };
|
|
481
|
+
}
|
|
482
|
+
finally {
|
|
483
|
+
this.tui.start();
|
|
484
|
+
// A forced redraw: renders asked for while the editor had the screen were skipped.
|
|
485
|
+
this.tui.requestRender(true);
|
|
486
|
+
this.editingPlan = false;
|
|
487
|
+
}
|
|
488
|
+
if (done)
|
|
489
|
+
return;
|
|
490
|
+
if (outcome.kind === "failed")
|
|
491
|
+
this.flash("Couldn't open your editor. Set $EDITOR.");
|
|
492
|
+
else if (outcome.kind === "empty") {
|
|
493
|
+
this.notice(theme.muted("The edited plan was empty, so the plan stays as it was."));
|
|
494
|
+
}
|
|
495
|
+
else if (outcome.kind === "unchanged") {
|
|
496
|
+
if (outcome.quick)
|
|
497
|
+
this.flash("If your editor returns at once, set EDITOR='code --wait'.");
|
|
498
|
+
}
|
|
499
|
+
else {
|
|
500
|
+
const n = changedLines(before, outcome.text);
|
|
501
|
+
edited = outcome.text;
|
|
502
|
+
box.edited = true;
|
|
503
|
+
this.add(new PlanBlock(outcome.text, true), "text");
|
|
504
|
+
this.notice(`${theme.warning("✎")} Plan edited ${theme.muted(`· ${n} ${n === 1 ? "line" : "lines"} changed`)}`);
|
|
505
|
+
}
|
|
506
|
+
this.tui.requestRender();
|
|
507
|
+
};
|
|
508
|
+
const box = new PlanApprovalBox(finish, () => void edit(), this.options.planArmMs);
|
|
509
|
+
this.hideLoader();
|
|
510
|
+
this.approval = box;
|
|
511
|
+
this.activity.addChild(box);
|
|
512
|
+
this.tui.setFocus(box);
|
|
513
|
+
this.tui.requestRender();
|
|
514
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
515
|
+
});
|
|
516
|
+
}
|
|
399
517
|
// --- commands ---------------------------------------------------------------------------------
|
|
400
518
|
runCommand(name, arg) {
|
|
401
519
|
switch (name) {
|
|
@@ -415,6 +533,7 @@ export class App {
|
|
|
415
533
|
case "clear":
|
|
416
534
|
if (this.busy)
|
|
417
535
|
return this.notice(theme.muted("Stop the current task first (esc)."));
|
|
536
|
+
void this.writer?.release();
|
|
418
537
|
this.writer = null;
|
|
419
538
|
if (this.agent)
|
|
420
539
|
this.spentBefore += this.agent.totalCostMicros;
|
|
@@ -479,6 +598,9 @@ export class App {
|
|
|
479
598
|
if (mode === "full") {
|
|
480
599
|
this.flash("Don't ask: edits and commands run without asking. Shift+Tab to switch back.");
|
|
481
600
|
}
|
|
601
|
+
else if (mode === "plan") {
|
|
602
|
+
this.flash("Plan mode: it looks around and plans. Nothing changes until you approve.");
|
|
603
|
+
}
|
|
482
604
|
this.updateStatus();
|
|
483
605
|
this.tui.requestRender();
|
|
484
606
|
}
|
|
@@ -624,7 +746,7 @@ export class App {
|
|
|
624
746
|
save(change) {
|
|
625
747
|
if (!this.model)
|
|
626
748
|
return;
|
|
627
|
-
this.writer ??= SessionWriter.create(this.options.sessionsDir, this.options.cwd, this.model.id);
|
|
749
|
+
this.writer ??= SessionWriter.create(this.options.sessionsDir, this.options.cwd, this.model.id, new Date(), "terminal");
|
|
628
750
|
if (change.type === "usage") {
|
|
629
751
|
this.writer.append({
|
|
630
752
|
type: "usage",
|
|
@@ -655,7 +777,10 @@ export class App {
|
|
|
655
777
|
this.header.model = model.id;
|
|
656
778
|
this.agent = this.newAgent(model);
|
|
657
779
|
this.agent.restore(loaded.messages);
|
|
780
|
+
void this.writer?.release();
|
|
658
781
|
this.writer = SessionWriter.resume(loaded.path);
|
|
782
|
+
// The callers checked that nothing else has it open.
|
|
783
|
+
void this.writer.claim("terminal");
|
|
659
784
|
for (const entry of loaded.display) {
|
|
660
785
|
if (entry.kind === "user")
|
|
661
786
|
this.addUser(entry.text);
|
|
@@ -667,6 +792,14 @@ export class App {
|
|
|
667
792
|
line.finish(entry.isError ? "error" : "done", entry.summary);
|
|
668
793
|
this.add(line, "steps");
|
|
669
794
|
}
|
|
795
|
+
else if (entry.kind === "plan") {
|
|
796
|
+
this.add(new PlanBlock(entry.text, entry.edited), "text");
|
|
797
|
+
const line = new ToolLine("", "Plan");
|
|
798
|
+
const failed = entry.outcome === "not-approved" || entry.outcome === "stopped";
|
|
799
|
+
const label = PLAN_OUTCOME_LABELS[entry.outcome];
|
|
800
|
+
line.finish(failed ? "error" : "done", entry.feedback ? `${label} · ${entry.feedback}` : label);
|
|
801
|
+
this.add(line, "steps");
|
|
802
|
+
}
|
|
670
803
|
else
|
|
671
804
|
this.notice(theme.muted("Summarized earlier conversation."));
|
|
672
805
|
}
|
|
@@ -703,6 +836,11 @@ export class App {
|
|
|
703
836
|
}
|
|
704
837
|
async pickSession(path) {
|
|
705
838
|
this.closePicker();
|
|
839
|
+
const holder = await sessionHolder(path);
|
|
840
|
+
if (holder) {
|
|
841
|
+
this.notice(theme.muted(`${openElsewhere(holder)} Close it there to continue it here.`));
|
|
842
|
+
return;
|
|
843
|
+
}
|
|
706
844
|
const loaded = await loadSession(path);
|
|
707
845
|
if (!loaded) {
|
|
708
846
|
this.notice(theme.error("✗ That conversation couldn't be read."));
|
|
@@ -781,16 +919,19 @@ export class App {
|
|
|
781
919
|
this.tui.requestRender();
|
|
782
920
|
}
|
|
783
921
|
updateStatus() {
|
|
784
|
-
const
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
922
|
+
const mode = this.permissions.mode;
|
|
923
|
+
const line = statusLine({
|
|
924
|
+
mode,
|
|
925
|
+
contextTokens: this.agent?.contextTokens ?? null,
|
|
926
|
+
contextLength: this.model?.contextLength ?? 1,
|
|
927
|
+
costMicros: this.sessionCost(),
|
|
928
|
+
balance: this.balance,
|
|
929
|
+
});
|
|
930
|
+
// Plan mode stands out, so it's clear at a glance that nothing will change.
|
|
931
|
+
const shown = mode === "plan"
|
|
932
|
+
? theme.accent(modeStatus(mode)) + theme.muted(line.slice(modeStatus(mode).length))
|
|
933
|
+
: theme.muted(line);
|
|
934
|
+
this.status.setText(this.hint ? theme.warning(this.hint) : shown);
|
|
794
935
|
}
|
|
795
936
|
showLoader(text) {
|
|
796
937
|
this.loaderText = text;
|
|
@@ -854,7 +995,7 @@ export class App {
|
|
|
854
995
|
const stopping = mcp
|
|
855
996
|
? Promise.race([mcp.close(), new Promise((r) => setTimeout(r, 2000).unref())])
|
|
856
997
|
: Promise.resolve();
|
|
857
|
-
void Promise.all([writer?.
|
|
998
|
+
void Promise.all([writer?.release(), stopping]).then(() => {
|
|
858
999
|
const saved = writer?.started && !writer.failed
|
|
859
1000
|
? `${style.dim("Saved. Continue with:")} reasonally --continue`
|
|
860
1001
|
: null;
|
package/dist/tui/args.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
/** Flags for bare `reasonally` (the agent). No terminal UI imports: `cli.ts` loads this eagerly. */
|
|
2
2
|
export function parseAgentArgs(argv) {
|
|
3
|
-
const args = {
|
|
3
|
+
const args = {
|
|
4
|
+
model: null,
|
|
5
|
+
yes: false,
|
|
6
|
+
plan: false,
|
|
7
|
+
continue: false,
|
|
8
|
+
resume: false,
|
|
9
|
+
};
|
|
4
10
|
for (let i = 0; i < argv.length; i++) {
|
|
5
11
|
const arg = argv[i];
|
|
6
12
|
if (arg === "--model" || arg === "-m") {
|
|
@@ -16,6 +22,9 @@ export function parseAgentArgs(argv) {
|
|
|
16
22
|
else if (arg === "--yes" || arg === "-y") {
|
|
17
23
|
args.yes = true;
|
|
18
24
|
}
|
|
25
|
+
else if (arg === "--plan") {
|
|
26
|
+
args.plan = true;
|
|
27
|
+
}
|
|
19
28
|
else if (arg === "--continue" || arg === "-c") {
|
|
20
29
|
args.continue = true;
|
|
21
30
|
}
|
|
@@ -26,6 +35,8 @@ export function parseAgentArgs(argv) {
|
|
|
26
35
|
return { ok: false, error: `Unknown option "${arg}". See \`reasonally --help\`.` };
|
|
27
36
|
}
|
|
28
37
|
}
|
|
38
|
+
if (args.plan && args.yes)
|
|
39
|
+
return { ok: false, error: "Pick one of --plan and --yes." };
|
|
29
40
|
return { ok: true, args };
|
|
30
41
|
}
|
|
31
42
|
/** Bare `reasonally` opens the agent only in an interactive terminal; scripts still get help. */
|
package/dist/tui/commands.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
export const COMMANDS = [
|
|
2
2
|
{ name: "model", description: "Switch model" },
|
|
3
|
-
{
|
|
3
|
+
{
|
|
4
|
+
name: "mode",
|
|
5
|
+
description: "Switch between asking, auto-accepting edits, planning first, and not asking",
|
|
6
|
+
},
|
|
4
7
|
{ name: "new", description: "Start a new conversation" },
|
|
5
8
|
{ name: "resume", description: "Continue an earlier conversation in this folder" },
|
|
6
9
|
{ name: "compact", description: "Summarize the conversation so far to free up context" },
|
|
@@ -22,6 +25,7 @@ export const KEYS = [
|
|
|
22
25
|
["Esc", "stop the current reply or command"],
|
|
23
26
|
["Shift+Tab", "switch mode"],
|
|
24
27
|
["Ctrl+O", "show the full output of the last step"],
|
|
28
|
+
["Ctrl+G", "edit a waiting plan in your editor"],
|
|
25
29
|
["Ctrl+C", "clear the input · twice to quit"],
|
|
26
30
|
["@", "mention a file"],
|
|
27
31
|
["↑ ↓", "earlier messages"],
|