reasonally 0.5.0 → 0.7.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 +62 -12
- package/dist/agent/bundled-commands.js +24 -0
- package/dist/agent/command-files.js +334 -0
- package/dist/agent/command-template.js +179 -0
- package/dist/agent/compact.js +58 -8
- package/dist/agent/context.js +82 -0
- package/dist/agent/loop.js +128 -31
- package/dist/agent/permissions.js +91 -0
- package/dist/agent/session.js +73 -5
- package/dist/api.js +12 -3
- package/dist/browser/guards.js +15 -0
- package/dist/browser/messages.js +61 -0
- package/dist/browser/sessions.js +2 -60
- package/dist/cli.js +5 -0
- package/dist/login.js +20 -4
- package/dist/sync/client.js +789 -0
- package/dist/sync/command.js +105 -0
- package/dist/sync/entries.js +139 -0
- package/dist/sync/live.js +107 -0
- package/dist/sync/protocol.js +14 -0
- package/dist/sync/redact.js +35 -0
- package/dist/sync/store.js +214 -0
- package/dist/sync/tail.js +66 -0
- package/dist/tui/app.js +227 -43
- package/dist/tui/commands.js +71 -15
- package/dist/tui/start.js +21 -1
- package/package.json +1 -1
package/dist/tui/app.js
CHANGED
|
@@ -2,6 +2,7 @@ import { CombinedAutocompleteProvider, Editor, Loader, Markdown, matchesKey, Tui
|
|
|
2
2
|
import { formatUsd } from "../agent/cost.js";
|
|
3
3
|
import { Agent, } from "../agent/loop.js";
|
|
4
4
|
import { DEFAULT_WEB_POLICY, emptyAllows, MODE_LABELS, MODES, } from "../agent/permissions.js";
|
|
5
|
+
import { commandLabel, expandCommand, listCommands, parseSlash, } from "../agent/command-files.js";
|
|
5
6
|
import { MCP_EXAMPLE } from "../agent/mcp-config.js";
|
|
6
7
|
import { count } from "../agent/tools/files.js";
|
|
7
8
|
import { BUILTIN_TOOLS, webTools } from "../agent/tools/index.js";
|
|
@@ -9,10 +10,11 @@ import { tildePath } from "../agent/tools/paths.js";
|
|
|
9
10
|
import { formatTodos } from "../agent/tools/todo.js";
|
|
10
11
|
import { FileTracker } from "../agent/tools/types.js";
|
|
11
12
|
import { commandEnv } from "../agent/tools/bash.js";
|
|
12
|
-
import { listSessions, loadSession, sessionHolder, SessionWriter, stepRecord, timeAgo, } from "../agent/session.js";
|
|
13
|
+
import { changeRecord, listSessions, loadSession, sessionHolder, SessionWriter, stepRecord, timeAgo, } from "../agent/session.js";
|
|
13
14
|
import { PLAN_OUTCOME_LABELS, PLAN_TOOL } from "../agent/tools/plan.js";
|
|
14
15
|
import { changedLines, editInEditor } from "./external-editor.js";
|
|
15
|
-
import { COMMANDS, helpText,
|
|
16
|
+
import { appCommand, closeMatches, COMMANDS, helpText, SOURCE_LABELS } from "./commands.js";
|
|
17
|
+
import { LiveTracker } from "../sync/live.js";
|
|
16
18
|
import { ApprovalBox, Clamped, PlanApprovalBox, PlanBlock, gap, Header, INDENT, Lines, markdownTheme, Picker, Reasoning, selectListTheme, ToolLine, UserMessage, } from "./components.js";
|
|
17
19
|
import { compactionNote, describeError, formatTokens, modelChoice, openElsewhere, retryMessage, sortModels, modeStatus, statusLine, } from "./format.js";
|
|
18
20
|
import { style, theme } from "./theme.js";
|
|
@@ -35,6 +37,7 @@ export class App {
|
|
|
35
37
|
/** Cost of earlier conversations in this session (`/new`). */
|
|
36
38
|
spentBefore = 0n;
|
|
37
39
|
busy = null;
|
|
40
|
+
/** Sent once the current step ends: messages, and commands as typed (`/review auth`). */
|
|
38
41
|
queued = [];
|
|
39
42
|
queuedNote = null;
|
|
40
43
|
/** What Enter on an empty input does after an error. */
|
|
@@ -53,6 +56,12 @@ export class App {
|
|
|
53
56
|
header = null;
|
|
54
57
|
/** The kind of the last transcript entry, for spacing. */
|
|
55
58
|
lastKind = null;
|
|
59
|
+
/** The `/` menu: the app's commands, then saved ones. Updated in place (the menu reads it). */
|
|
60
|
+
slashItems = [];
|
|
61
|
+
saved = [];
|
|
62
|
+
commandProblems = [];
|
|
63
|
+
commandsAt = 0;
|
|
64
|
+
commandsLoading = null;
|
|
56
65
|
// The reply being streamed.
|
|
57
66
|
replyText = "";
|
|
58
67
|
reply = null;
|
|
@@ -64,6 +73,10 @@ export class App {
|
|
|
64
73
|
lastPlanOutcome = null;
|
|
65
74
|
/** The plan is open in the person's editor. */
|
|
66
75
|
editingPlan = false;
|
|
76
|
+
/** What's happening now, for the web (see `src/sync/live.ts`). */
|
|
77
|
+
live = new LiveTracker();
|
|
78
|
+
turnFailed = false;
|
|
79
|
+
approvalsAsked = 0;
|
|
67
80
|
constructor(options) {
|
|
68
81
|
this.options = options;
|
|
69
82
|
this.session = options.session;
|
|
@@ -77,8 +90,15 @@ export class App {
|
|
|
77
90
|
};
|
|
78
91
|
this.tui = new TuiMainScreen(options.terminal);
|
|
79
92
|
this.editor = new Editor(this.tui, { borderColor: theme.border, selectList: selectListTheme }, { paddingX: 1 });
|
|
80
|
-
this.
|
|
93
|
+
this.slashItems.push(...this.menuItems());
|
|
94
|
+
this.editor.setAutocompleteProvider(new CombinedAutocompleteProvider(this.slashItems, options.cwd, null));
|
|
81
95
|
this.editor.onSubmit = (text) => this.submit(text);
|
|
96
|
+
// A command file added since: it's in the menu the next time "/" is typed.
|
|
97
|
+
this.editor.onChange = (text) => {
|
|
98
|
+
if (text === "/")
|
|
99
|
+
void this.refreshCommands(3000);
|
|
100
|
+
};
|
|
101
|
+
void this.refreshCommands();
|
|
82
102
|
this.tui.addChild(this.transcript);
|
|
83
103
|
this.tui.addChild(this.activity);
|
|
84
104
|
this.tui.addChild(this.editor);
|
|
@@ -189,23 +209,22 @@ export class App {
|
|
|
189
209
|
void this.relogin();
|
|
190
210
|
return;
|
|
191
211
|
}
|
|
192
|
-
const command = text.startsWith("/") ? parseCommand(text) : null;
|
|
193
|
-
if (text.startsWith("/")) {
|
|
194
|
-
this.editor.addToHistory(text);
|
|
195
|
-
if (command)
|
|
196
|
-
this.runCommand(command.name, command.arg);
|
|
197
|
-
else
|
|
198
|
-
this.notice(theme.warning(`"${text}" isn't a command. Type /help for the list.`));
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
212
|
this.editor.addToHistory(text);
|
|
213
|
+
// A path ("/Users/me/app is slow") isn't a command: it goes as a message.
|
|
214
|
+
const slash = parseSlash(text);
|
|
215
|
+
if (slash)
|
|
216
|
+
void this.runSlash(slash);
|
|
217
|
+
else
|
|
218
|
+
this.sendMessage(text);
|
|
219
|
+
}
|
|
220
|
+
sendMessage(text) {
|
|
202
221
|
if (!this.agent) {
|
|
203
222
|
this.afterPick = text;
|
|
204
223
|
this.openModelPicker();
|
|
205
224
|
return;
|
|
206
225
|
}
|
|
207
226
|
if (this.busy) {
|
|
208
|
-
this.queued.push(text);
|
|
227
|
+
this.queued.push({ text });
|
|
209
228
|
this.showQueued();
|
|
210
229
|
return;
|
|
211
230
|
}
|
|
@@ -213,21 +232,132 @@ export class App {
|
|
|
213
232
|
const agent = this.agent;
|
|
214
233
|
void this.runTurn((signal) => agent.send(text, signal));
|
|
215
234
|
}
|
|
235
|
+
// --- slash commands ---------------------------------------------------------------------------
|
|
236
|
+
commandRoots() {
|
|
237
|
+
const { cwd, home, configDir } = this.options;
|
|
238
|
+
return { cwd, home, configDir, env: process.env };
|
|
239
|
+
}
|
|
240
|
+
/** The menu's entries: the app's commands (with their arguments), then saved commands. */
|
|
241
|
+
menuItems() {
|
|
242
|
+
const own = COMMANDS.map((c) => ({
|
|
243
|
+
name: c.name,
|
|
244
|
+
description: c.description,
|
|
245
|
+
argumentHint: c.argumentHint,
|
|
246
|
+
...(c.name === "model" && {
|
|
247
|
+
getArgumentCompletions: (prefix) => sortModels(this.options.models)
|
|
248
|
+
.filter((m) => m.id.toLowerCase().includes(prefix.toLowerCase()))
|
|
249
|
+
.slice(0, 20)
|
|
250
|
+
.map((m) => ({ value: m.id, label: m.id, description: modelChoice(m).description })),
|
|
251
|
+
}),
|
|
252
|
+
...(c.name === "mode" && {
|
|
253
|
+
getArgumentCompletions: (prefix) => MODES.filter((m) => m.startsWith(prefix.toLowerCase())).map((m) => ({
|
|
254
|
+
value: m,
|
|
255
|
+
label: m,
|
|
256
|
+
description: MODE_LABELS[m],
|
|
257
|
+
})),
|
|
258
|
+
}),
|
|
259
|
+
}));
|
|
260
|
+
const saved = this.saved.map((c) => ({
|
|
261
|
+
name: c.name,
|
|
262
|
+
description: c.source === "bundled"
|
|
263
|
+
? c.description
|
|
264
|
+
: [c.description, SOURCE_LABELS[c.source]].filter(Boolean).join(" · "),
|
|
265
|
+
...(c.argumentHint && { argumentHint: c.argumentHint }),
|
|
266
|
+
}));
|
|
267
|
+
return [...own, ...saved];
|
|
268
|
+
}
|
|
269
|
+
/** Reads saved commands again, unless they were read within `maxAgeMs`. */
|
|
270
|
+
refreshCommands(maxAgeMs = 0) {
|
|
271
|
+
if (this.commandsLoading)
|
|
272
|
+
return this.commandsLoading;
|
|
273
|
+
if (maxAgeMs && Date.now() - this.commandsAt < maxAgeMs)
|
|
274
|
+
return Promise.resolve();
|
|
275
|
+
this.commandsLoading = listCommands(this.commandRoots())
|
|
276
|
+
.then(({ commands, problems }) => {
|
|
277
|
+
this.saved = commands;
|
|
278
|
+
this.commandProblems = problems;
|
|
279
|
+
this.commandsAt = Date.now();
|
|
280
|
+
this.slashItems.splice(0, this.slashItems.length, ...this.menuItems());
|
|
281
|
+
})
|
|
282
|
+
.catch(() => { })
|
|
283
|
+
.finally(() => {
|
|
284
|
+
this.commandsLoading = null;
|
|
285
|
+
});
|
|
286
|
+
return this.commandsLoading;
|
|
287
|
+
}
|
|
288
|
+
async runSlash(ref) {
|
|
289
|
+
const own = appCommand(ref.name);
|
|
290
|
+
if (own)
|
|
291
|
+
return this.runCommand(own.name, ref.args);
|
|
292
|
+
if (!ref.name) {
|
|
293
|
+
return this.notice(theme.muted("Type a command's name after the /. /help lists them."));
|
|
294
|
+
}
|
|
295
|
+
const find = () => this.saved.find((c) => c.name === ref.name);
|
|
296
|
+
// Not known yet: the list may still be loading, or the file was just added.
|
|
297
|
+
if (!find())
|
|
298
|
+
await this.refreshCommands();
|
|
299
|
+
if (!find()) {
|
|
300
|
+
const near = closeMatches(ref.name, [
|
|
301
|
+
...COMMANDS.map((c) => c.name),
|
|
302
|
+
...this.saved.map((c) => c.name),
|
|
303
|
+
]);
|
|
304
|
+
const hint = near.length ? ` Did you mean ${near.map((n) => `/${n}`).join(", ")}?` : "";
|
|
305
|
+
return this.notice(theme.warning(`/${ref.name} isn't a command.${hint} Type /help for the list.`));
|
|
306
|
+
}
|
|
307
|
+
this.sendCommand(ref);
|
|
308
|
+
}
|
|
309
|
+
/** A saved command (or /review, /init) as a message: queued while a step runs, like any. */
|
|
310
|
+
sendCommand(ref) {
|
|
311
|
+
if (!this.agent) {
|
|
312
|
+
this.afterPick = commandLabel(ref);
|
|
313
|
+
this.openModelPicker();
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (this.busy) {
|
|
317
|
+
this.queued.push({ text: commandLabel(ref), command: ref });
|
|
318
|
+
this.showQueued();
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
this.runPrompt(ref);
|
|
322
|
+
}
|
|
323
|
+
/** Runs a saved command: its file is read now, and what was typed is shown (`/review auth`). */
|
|
324
|
+
runPrompt(ref) {
|
|
325
|
+
const agent = this.agent;
|
|
326
|
+
const roots = this.commandRoots();
|
|
327
|
+
const show = () => this.addUser(commandLabel(ref));
|
|
328
|
+
const fail = (message) => {
|
|
329
|
+
this.notice(theme.warning(message));
|
|
330
|
+
// What was typed comes back, to fix or run again.
|
|
331
|
+
if (!this.editor.getText())
|
|
332
|
+
this.editor.setText(commandLabel(ref));
|
|
333
|
+
};
|
|
334
|
+
void this.runTurn(async function* (signal) {
|
|
335
|
+
const result = await expandCommand(ref, roots);
|
|
336
|
+
if (!result.ok)
|
|
337
|
+
return fail(result.message);
|
|
338
|
+
show();
|
|
339
|
+
yield* agent.send(result.prompt, signal, undefined, ref);
|
|
340
|
+
});
|
|
341
|
+
}
|
|
216
342
|
// --- turns ------------------------------------------------------------------------------------
|
|
217
343
|
async runTurn(start) {
|
|
218
344
|
const controller = new AbortController();
|
|
219
345
|
this.busy = controller;
|
|
220
346
|
this.pendingNext = null;
|
|
347
|
+
this.turnFailed = false;
|
|
348
|
+
this.liveUpdate(this.live.turnStarted());
|
|
221
349
|
this.showLoader("Thinking…");
|
|
222
350
|
try {
|
|
223
351
|
for await (const event of start(controller.signal))
|
|
224
352
|
this.onEvent(event);
|
|
225
353
|
}
|
|
226
354
|
catch (error) {
|
|
355
|
+
this.turnFailed = true;
|
|
227
356
|
this.showError(error);
|
|
228
357
|
}
|
|
229
358
|
finally {
|
|
230
359
|
this.busy = null;
|
|
360
|
+
this.liveUpdate(this.live.turnEnded(this.turnFailed));
|
|
231
361
|
this.reasoning?.finish();
|
|
232
362
|
this.hideLoader();
|
|
233
363
|
this.updateStatus();
|
|
@@ -249,12 +379,28 @@ export class App {
|
|
|
249
379
|
}
|
|
250
380
|
if (!this.queued.length || !this.agent || this.exited)
|
|
251
381
|
return;
|
|
252
|
-
|
|
253
|
-
this.
|
|
254
|
-
|
|
255
|
-
|
|
382
|
+
// A command goes on its own; messages before the next one go together.
|
|
383
|
+
const first = this.queued[0];
|
|
384
|
+
if (first.command) {
|
|
385
|
+
this.queued.shift();
|
|
386
|
+
this.runPrompt(first.command);
|
|
387
|
+
}
|
|
388
|
+
else {
|
|
389
|
+
const batch = [];
|
|
390
|
+
while (this.queued.length && !this.queued[0].command)
|
|
391
|
+
batch.push(this.queued.shift().text);
|
|
392
|
+
const text = batch.join("\n\n");
|
|
393
|
+
this.addUser(text);
|
|
394
|
+
const agent = this.agent;
|
|
395
|
+
void this.runTurn((signal) => agent.send(text, signal));
|
|
396
|
+
}
|
|
397
|
+
if (this.queued.length)
|
|
398
|
+
this.showQueued();
|
|
399
|
+
else
|
|
400
|
+
this.liveUpdate(this.live.setQueued(0));
|
|
256
401
|
}
|
|
257
402
|
showQueued() {
|
|
403
|
+
this.liveUpdate(this.live.setQueued(this.queued.length));
|
|
258
404
|
const text = theme.muted(`Queued (${this.queued.length}). Sends when this step finishes, or now with esc.`);
|
|
259
405
|
if (!this.queuedNote) {
|
|
260
406
|
this.queuedNote = new Lines(text, 2);
|
|
@@ -271,6 +417,7 @@ export class App {
|
|
|
271
417
|
this.replyText = "";
|
|
272
418
|
this.reasoning = null;
|
|
273
419
|
this.setLoader("Thinking…");
|
|
420
|
+
this.liveUpdate(this.live.modelStarted());
|
|
274
421
|
break;
|
|
275
422
|
case "reasoning":
|
|
276
423
|
if (!this.reasoning) {
|
|
@@ -289,6 +436,7 @@ export class App {
|
|
|
289
436
|
this.replyText += event.text;
|
|
290
437
|
this.reply.setText(this.replyText);
|
|
291
438
|
this.setLoader("Writing…");
|
|
439
|
+
this.liveUpdate(this.live.textDelta(event.text));
|
|
292
440
|
break;
|
|
293
441
|
case "tool_call_streaming":
|
|
294
442
|
this.reasoning?.finish();
|
|
@@ -303,11 +451,13 @@ export class App {
|
|
|
303
451
|
this.add(this.planBlock, "text");
|
|
304
452
|
}
|
|
305
453
|
this.setLoader("Writing the plan…");
|
|
454
|
+
this.liveUpdate(this.live.planDraft(event.text));
|
|
306
455
|
break;
|
|
307
456
|
case "plan_draft_dropped":
|
|
308
457
|
if (this.planBlock)
|
|
309
458
|
this.transcript.removeChild(this.planBlock);
|
|
310
459
|
this.planBlock = null;
|
|
460
|
+
this.liveUpdate(this.live.planDropped());
|
|
311
461
|
break;
|
|
312
462
|
case "mode_changed":
|
|
313
463
|
// The agent switched it (the plan was approved); the status line shows it below.
|
|
@@ -323,6 +473,7 @@ export class App {
|
|
|
323
473
|
const line = new ToolLine(event.id, event.title);
|
|
324
474
|
this.toolLines.set(event.id, line);
|
|
325
475
|
this.add(line, "steps");
|
|
476
|
+
this.liveUpdate(this.live.stepStarted(event.id, event.title));
|
|
326
477
|
const helpers = [...this.toolLines.values()].filter((l) => l.status === "running" && l.title.startsWith("Helper: ")).length;
|
|
327
478
|
this.setLoader(event.name === "task"
|
|
328
479
|
? helpers > 1
|
|
@@ -349,6 +500,8 @@ export class App {
|
|
|
349
500
|
line.expanded = true;
|
|
350
501
|
}
|
|
351
502
|
this.writer?.append(stepRecord(event.id, event.title, result));
|
|
503
|
+
this.liveUpdate(this.live.stepDone(event.id));
|
|
504
|
+
this.syncSaved();
|
|
352
505
|
if (line.expandable)
|
|
353
506
|
this.lastExpandable = line;
|
|
354
507
|
this.setLoader("Thinking…");
|
|
@@ -367,6 +520,7 @@ export class App {
|
|
|
367
520
|
this.notice(theme.muted(compactionNote(event.beforeTokens, event.afterTokens, this.model?.contextLength ?? 1)));
|
|
368
521
|
break;
|
|
369
522
|
case "error":
|
|
523
|
+
this.turnFailed = true;
|
|
370
524
|
this.showError(event.error);
|
|
371
525
|
break;
|
|
372
526
|
case "done":
|
|
@@ -391,9 +545,12 @@ export class App {
|
|
|
391
545
|
/** Shows an approval box and waits for y / a / n (or Esc, or the turn being stopped). */
|
|
392
546
|
/** Approvals wait their turn: helpers working at once ask one at a time. */
|
|
393
547
|
approve(request, signal) {
|
|
394
|
-
const show = () =>
|
|
395
|
-
|
|
396
|
-
|
|
548
|
+
const show = () => {
|
|
549
|
+
if (signal.aborted)
|
|
550
|
+
return Promise.resolve({ kind: "no" });
|
|
551
|
+
this.liveUpdate(this.live.approvalAsked(`approval-${++this.approvalsAsked}`, request));
|
|
552
|
+
return this.showApproval(request, signal).finally(() => this.liveUpdate(this.live.approvalAnswered()));
|
|
553
|
+
};
|
|
397
554
|
const next = this.approvals.then(show, show);
|
|
398
555
|
this.approvals = next.catch(() => { });
|
|
399
556
|
return next;
|
|
@@ -530,8 +687,12 @@ export class App {
|
|
|
530
687
|
this.cycleMode();
|
|
531
688
|
return;
|
|
532
689
|
}
|
|
690
|
+
case "plan":
|
|
691
|
+
this.setMode("plan");
|
|
692
|
+
if (arg)
|
|
693
|
+
this.sendMessage(arg);
|
|
694
|
+
return;
|
|
533
695
|
case "new":
|
|
534
|
-
case "clear":
|
|
535
696
|
if (this.busy)
|
|
536
697
|
return this.notice(theme.muted("Stop the current task first (esc)."));
|
|
537
698
|
void this.writer?.release();
|
|
@@ -544,7 +705,7 @@ export class App {
|
|
|
544
705
|
this.notice(theme.muted("── New conversation ──"));
|
|
545
706
|
this.updateStatus();
|
|
546
707
|
return;
|
|
547
|
-
case "
|
|
708
|
+
case "usage": {
|
|
548
709
|
const context = this.agent?.contextTokens;
|
|
549
710
|
this.notice([
|
|
550
711
|
`This session: ~${formatUsd(this.sessionCost())} (an estimate; your usage page has exact charges)`,
|
|
@@ -563,7 +724,7 @@ export class App {
|
|
|
563
724
|
const agent = this.agent;
|
|
564
725
|
if (!agent)
|
|
565
726
|
return;
|
|
566
|
-
void this.runTurn((signal) => agent.compactNow(signal));
|
|
727
|
+
void this.runTurn((signal) => agent.compactNow(signal, arg || undefined));
|
|
567
728
|
return;
|
|
568
729
|
}
|
|
569
730
|
case "resume":
|
|
@@ -580,10 +741,10 @@ export class App {
|
|
|
580
741
|
this.notice(this.mcpReport());
|
|
581
742
|
return;
|
|
582
743
|
case "help":
|
|
583
|
-
this.notice(helpText());
|
|
744
|
+
this.notice(helpText(this.saved, this.commandProblems, this.options.home));
|
|
745
|
+
void this.refreshCommands(3000);
|
|
584
746
|
return;
|
|
585
747
|
case "exit":
|
|
586
|
-
case "quit":
|
|
587
748
|
this.exit(0);
|
|
588
749
|
return;
|
|
589
750
|
default:
|
|
@@ -647,6 +808,7 @@ export class App {
|
|
|
647
808
|
if (this.header)
|
|
648
809
|
this.header.model = model.id;
|
|
649
810
|
this.writer?.append({ type: "model", id: model.id });
|
|
811
|
+
this.syncSaved();
|
|
650
812
|
if (this.agent) {
|
|
651
813
|
this.agent.options.model = model;
|
|
652
814
|
this.agent.messages[0] = { role: "system", content: this.options.systemPrompt(model) };
|
|
@@ -752,20 +914,37 @@ export class App {
|
|
|
752
914
|
if (!this.model)
|
|
753
915
|
return;
|
|
754
916
|
this.writer ??= SessionWriter.create(this.options.sessionsDir, this.options.cwd, this.model.id, new Date(), "terminal");
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
917
|
+
this.writer.append(changeRecord(change));
|
|
918
|
+
this.syncSaved();
|
|
919
|
+
}
|
|
920
|
+
/** Tells sync the conversation changed: it uploads what's new once the write has landed. */
|
|
921
|
+
syncSaved() {
|
|
922
|
+
const writer = this.writer;
|
|
923
|
+
if (!writer || !this.options.sync)
|
|
924
|
+
return;
|
|
925
|
+
this.options.sync.poke(writer.path, {
|
|
926
|
+
flush: () => writer.flush(),
|
|
927
|
+
status: this.live.threadStatus,
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
/** Tells sync what's happening now (sent at once for "now", grouped for "soon"). */
|
|
931
|
+
liveUpdate(urgency) {
|
|
932
|
+
const sync = this.options.sync;
|
|
933
|
+
const writer = this.writer;
|
|
934
|
+
if (!sync || !writer)
|
|
935
|
+
return;
|
|
936
|
+
sync.setLive(writer.path, this.live.snapshot(), urgency === "now");
|
|
937
|
+
if (urgency === "now")
|
|
938
|
+
sync.setStatus(writer.path, this.live.threadStatus);
|
|
939
|
+
}
|
|
940
|
+
/** Once, the first time this computer's conversations start showing on the web. */
|
|
941
|
+
showSyncNotice() {
|
|
942
|
+
this.notice(theme.muted("Conversations now also show on the web (summaries only). Turn off: reasonally sync off"));
|
|
943
|
+
this.tui.requestRender();
|
|
944
|
+
}
|
|
945
|
+
/** The key in use now (it changes when the person logs in again). */
|
|
946
|
+
get currentSession() {
|
|
947
|
+
return this.session;
|
|
769
948
|
}
|
|
770
949
|
/** Continues a saved conversation: reprints it as it looked, then carries on in the same file. */
|
|
771
950
|
resumeSession(loaded) {
|
|
@@ -786,9 +965,11 @@ export class App {
|
|
|
786
965
|
this.writer = SessionWriter.resume(loaded.path);
|
|
787
966
|
// The callers checked that nothing else has it open.
|
|
788
967
|
void this.writer.claim("terminal");
|
|
968
|
+
this.syncSaved();
|
|
789
969
|
for (const entry of loaded.display) {
|
|
790
|
-
if (entry.kind === "user")
|
|
791
|
-
this.addUser(entry.text);
|
|
970
|
+
if (entry.kind === "user") {
|
|
971
|
+
this.addUser(entry.command ? commandLabel(entry.command) : entry.text);
|
|
972
|
+
}
|
|
792
973
|
else if (entry.kind === "text") {
|
|
793
974
|
this.add(new Markdown(entry.text, INDENT.length, 0, markdownTheme), "text");
|
|
794
975
|
}
|
|
@@ -992,7 +1173,8 @@ export class App {
|
|
|
992
1173
|
const costText = cost > 0n ? style.dim(`~${formatUsd(cost)} this session`) : null;
|
|
993
1174
|
const writer = this.writer;
|
|
994
1175
|
const mcp = this.options.mcp;
|
|
995
|
-
|
|
1176
|
+
const sync = this.options.sync;
|
|
1177
|
+
if (!writer && !mcp && !sync) {
|
|
996
1178
|
this.options.onExit(code, costText);
|
|
997
1179
|
return;
|
|
998
1180
|
}
|
|
@@ -1000,7 +1182,9 @@ export class App {
|
|
|
1000
1182
|
const stopping = mcp
|
|
1001
1183
|
? Promise.race([mcp.close(), new Promise((r) => setTimeout(r, 2000).unref())])
|
|
1002
1184
|
: Promise.resolve();
|
|
1003
|
-
|
|
1185
|
+
// Sync gets a moment to send what's left; the rest goes next time.
|
|
1186
|
+
const syncing = sync?.stop(1_000);
|
|
1187
|
+
void Promise.all([writer?.release(), stopping, syncing]).then(() => {
|
|
1004
1188
|
const saved = writer?.started && !writer.failed
|
|
1005
1189
|
? `${style.dim("Saved. Continue with:")} reasonally --continue`
|
|
1006
1190
|
: null;
|
package/dist/tui/commands.js
CHANGED
|
@@ -1,24 +1,65 @@
|
|
|
1
1
|
export const COMMANDS = [
|
|
2
|
-
{ name: "model", description: "Switch model" },
|
|
2
|
+
{ name: "model", description: "Switch model", argumentHint: "[name]" },
|
|
3
3
|
{
|
|
4
4
|
name: "mode",
|
|
5
5
|
description: "Switch between asking, auto-accepting edits, planning first, and not asking",
|
|
6
|
+
argumentHint: "[ask|auto|plan|full]",
|
|
6
7
|
},
|
|
7
|
-
{
|
|
8
|
+
{
|
|
9
|
+
name: "plan",
|
|
10
|
+
description: "Plan first; nothing changes until you approve",
|
|
11
|
+
argumentHint: "[task]",
|
|
12
|
+
},
|
|
13
|
+
{ name: "new", description: "Start a new conversation", aliases: ["clear"] },
|
|
8
14
|
{ name: "resume", description: "Continue an earlier conversation in this folder" },
|
|
9
|
-
{
|
|
10
|
-
|
|
15
|
+
{
|
|
16
|
+
name: "compact",
|
|
17
|
+
description: "Summarize the conversation so far to free up context",
|
|
18
|
+
argumentHint: "[focus]",
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: "usage",
|
|
22
|
+
description: "Show this session's cost, the context used and your balance",
|
|
23
|
+
aliases: ["cost", "context"],
|
|
24
|
+
},
|
|
11
25
|
{ name: "mcp", description: "Show MCP servers and their tools" },
|
|
12
26
|
{ name: "login", description: "Log in again (e.g. after a key was revoked)" },
|
|
13
27
|
{ name: "help", description: "Show commands and keys" },
|
|
14
|
-
{ name: "exit", description: "Quit" },
|
|
28
|
+
{ name: "exit", description: "Quit", aliases: ["quit"] },
|
|
15
29
|
];
|
|
16
|
-
/**
|
|
17
|
-
export function
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
30
|
+
/** The command a name or alias runs, or null when it isn't one of the app's own. */
|
|
31
|
+
export function appCommand(name) {
|
|
32
|
+
return COMMANDS.find((c) => c.name === name || c.aliases?.includes(name)) ?? null;
|
|
33
|
+
}
|
|
34
|
+
/** Where a saved command came from, as the help and the menu label it. */
|
|
35
|
+
export const SOURCE_LABELS = {
|
|
36
|
+
bundled: "Reasonally",
|
|
37
|
+
project: "Project",
|
|
38
|
+
personal: "Personal",
|
|
39
|
+
claude: "Claude Code",
|
|
40
|
+
codex: "Codex",
|
|
41
|
+
};
|
|
42
|
+
/** Saved commands named like what was typed, for "did you mean". */
|
|
43
|
+
export function closeMatches(name, names) {
|
|
44
|
+
return names
|
|
45
|
+
.map((n) => ({ n, d: distance(name, n) }))
|
|
46
|
+
.filter(({ n, d }) => d <= 2 || (name.length > 1 && n.startsWith(name)))
|
|
47
|
+
.sort((a, b) => a.d - b.d)
|
|
48
|
+
.slice(0, 3)
|
|
49
|
+
.map(({ n }) => n);
|
|
50
|
+
}
|
|
51
|
+
function distance(a, b) {
|
|
52
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
53
|
+
for (let i = 1; i <= a.length; i++) {
|
|
54
|
+
let prev = row[0];
|
|
55
|
+
row[0] = i;
|
|
56
|
+
for (let j = 1; j <= b.length; j++) {
|
|
57
|
+
const next = row[j];
|
|
58
|
+
row[j] = Math.min(row[j] + 1, row[j - 1] + 1, prev + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
59
|
+
prev = next;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return row[b.length];
|
|
22
63
|
}
|
|
23
64
|
export const KEYS = [
|
|
24
65
|
["Enter", "send · Shift+Enter or Ctrl+J for a new line"],
|
|
@@ -28,15 +69,30 @@ export const KEYS = [
|
|
|
28
69
|
["Ctrl+G", "edit a waiting plan in your editor"],
|
|
29
70
|
["Ctrl+C", "clear the input · twice to quit"],
|
|
30
71
|
["@", "mention a file"],
|
|
72
|
+
["/", "a command, at the start of a message"],
|
|
31
73
|
["↑ ↓", "earlier messages"],
|
|
32
74
|
];
|
|
33
|
-
|
|
34
|
-
|
|
75
|
+
const usage = (c) => `/${c.name}${c.argumentHint ? ` ${c.argumentHint}` : ""}`;
|
|
76
|
+
export function helpText(saved = [], problems = [], home = "") {
|
|
77
|
+
const prompts = saved.filter((c) => c.source === "bundled");
|
|
78
|
+
const yours = saved.filter((c) => c.source !== "bundled");
|
|
79
|
+
const rows = [...COMMANDS.map((c) => usage(c)), ...saved.map(usage)];
|
|
80
|
+
const width = Math.max(...rows.map((r) => r.length)) + 2;
|
|
81
|
+
const short = (path) => home && path.startsWith(home) ? `~${path.slice(home.length)}` : path;
|
|
35
82
|
return [
|
|
36
83
|
"Commands",
|
|
37
|
-
...COMMANDS.map((c) => `
|
|
84
|
+
...COMMANDS.map((c) => ` ${usage(c).padEnd(width)}${c.description}`),
|
|
85
|
+
...prompts.map((c) => ` ${usage(c).padEnd(width)}${c.description}`),
|
|
86
|
+
...(yours.length
|
|
87
|
+
? [
|
|
88
|
+
"",
|
|
89
|
+
"Your commands",
|
|
90
|
+
...yours.map((c) => ` ${usage(c).padEnd(width)}${c.description ? `${c.description} · ` : ""}${SOURCE_LABELS[c.source]}`),
|
|
91
|
+
]
|
|
92
|
+
: ["", "Save a prompt as .reasonally/commands/<name>.md to run it as /<name>."]),
|
|
93
|
+
...problems.map((p) => ` Skipped ${short(p.path)}: ${p.reason}.`),
|
|
38
94
|
"",
|
|
39
95
|
"Keys",
|
|
40
|
-
...KEYS.map(([key, what]) => ` ${key.padEnd(width
|
|
96
|
+
...KEYS.map(([key, what]) => ` ${key.padEnd(width)}${what}`),
|
|
41
97
|
].join("\n");
|
|
42
98
|
}
|
package/dist/tui/start.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
1
2
|
import { realpath } from "node:fs/promises";
|
|
2
3
|
import { homedir } from "node:os";
|
|
3
4
|
import { ProcessTerminal } from "@earendil-works/pi-tui";
|
|
@@ -85,8 +86,24 @@ export async function startTui(args, log) {
|
|
|
85
86
|
}
|
|
86
87
|
const shell = shellFor(process.env).name;
|
|
87
88
|
const promptFor = (m) => systemPrompt({ cwd, home, platform: process.platform, shell, date: new Date(), model: m.id, branch }, instructions);
|
|
89
|
+
// This computer's conversations on the web (`reasonally sync`). Only a login syncs: a key from
|
|
90
|
+
// REASONALLY_API_KEY is for scripts and servers.
|
|
91
|
+
const { SyncClient } = await import("../sync/client.js");
|
|
92
|
+
let app = null;
|
|
93
|
+
const sync = new SyncClient({
|
|
94
|
+
configDir: config,
|
|
95
|
+
credentials: () => {
|
|
96
|
+
const current = app?.currentSession ?? session;
|
|
97
|
+
return current && !current.fromEnv
|
|
98
|
+
? { appUrl: current.appUrl, apiKey: current.apiKey }
|
|
99
|
+
: null;
|
|
100
|
+
},
|
|
101
|
+
client: "cli",
|
|
102
|
+
surface: "terminal",
|
|
103
|
+
appVersion: createRequire(import.meta.url)("../../package.json").version,
|
|
104
|
+
onFirstSync: () => app?.showSyncNotice(),
|
|
105
|
+
});
|
|
88
106
|
return await new Promise((resolve) => {
|
|
89
|
-
let app = null;
|
|
90
107
|
const quit = (code) => app?.exit(code);
|
|
91
108
|
const onSignal = () => quit(130);
|
|
92
109
|
const onCrash = (error) => {
|
|
@@ -125,6 +142,7 @@ export async function startTui(args, log) {
|
|
|
125
142
|
return next;
|
|
126
143
|
},
|
|
127
144
|
rememberModel: (id) => writeLastModel(MODEL_MEMORY_KEY, id),
|
|
145
|
+
sync,
|
|
128
146
|
onExit: (code, summary) => {
|
|
129
147
|
if (summary)
|
|
130
148
|
process.stdout.write(`${summary}\n`);
|
|
@@ -137,5 +155,7 @@ export async function startTui(args, log) {
|
|
|
137
155
|
});
|
|
138
156
|
app.start();
|
|
139
157
|
void mcp?.start();
|
|
158
|
+
// In the background: nothing waits for it, and it goes quiet when it can't sync.
|
|
159
|
+
void sync.start().then(() => sync.backfill([sessions]));
|
|
140
160
|
});
|
|
141
161
|
}
|