@mumbrane/arrakis 0.5.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/agent.js +4 -1
- package/src/cli.js +25 -2
- package/src/permissions.js +5 -1
- package/src/tui.js +853 -0
- package/src/ui.js +1 -1
package/package.json
CHANGED
package/src/agent.js
CHANGED
|
@@ -205,7 +205,10 @@ export class Agent {
|
|
|
205
205
|
const c = (calls[idx] ??= { id: '', type: 'function', function: { name: '', arguments: '' } });
|
|
206
206
|
if (tc.id) c.id = tc.id;
|
|
207
207
|
if (tc.function?.name && !c.function.name) c.function.name = tc.function.name;
|
|
208
|
-
if (tc.function?.arguments)
|
|
208
|
+
if (tc.function?.arguments) {
|
|
209
|
+
c.function.arguments += tc.function.arguments;
|
|
210
|
+
if (!this.silent) this.ui.addTokens?.(tc.function.arguments.length / 4);
|
|
211
|
+
}
|
|
209
212
|
}
|
|
210
213
|
}
|
|
211
214
|
} finally {
|
package/src/cli.js
CHANGED
|
@@ -8,6 +8,7 @@ import { Session, listSessions } from './session.js';
|
|
|
8
8
|
import { Agent, estimateTokens } from './agent.js';
|
|
9
9
|
import { Permissions } from './permissions.js';
|
|
10
10
|
import { UI, colors } from './ui.js';
|
|
11
|
+
import { FancyUI } from './tui.js';
|
|
11
12
|
import { TOOLS } from './tools.js';
|
|
12
13
|
import { loadProjectInstructions } from './prompt.js';
|
|
13
14
|
import { VERSION } from './version.js';
|
|
@@ -114,7 +115,7 @@ export async function main(argv) {
|
|
|
114
115
|
|
|
115
116
|
const cwd = process.cwd();
|
|
116
117
|
let config = loadConfig({ model: values.model, baseUrl: values['base-url'] });
|
|
117
|
-
const ui = new UI({ print });
|
|
118
|
+
const ui = !print && process.stdin.isTTY && process.stdout.isTTY && process.env.ARRAKIS_SIMPLE_UI !== '1' ? new FancyUI() : new UI({ print });
|
|
118
119
|
const prompts = createPrompts(ui);
|
|
119
120
|
if (!config.configured) {
|
|
120
121
|
if (print) throw new Error('arrakis is not set up yet. Run `arrakis` once to choose a provider and enter an API key.');
|
|
@@ -254,7 +255,28 @@ async function interactive({ agent, ui, prompts, config, cwd, firstPrompt, mcpSe
|
|
|
254
255
|
ui.historyFile = path.join(HOME_DIR, 'history');
|
|
255
256
|
ui.completer = (line) => completer(line, cwd, [...BUILTIN_COMMANDS, ...[...loadCustomCommands(cwd).keys()].map((k) => `/${k}`)]);
|
|
256
257
|
// Recreate the line reader so it picks up completion and history.
|
|
257
|
-
if (ui
|
|
258
|
+
if (ui instanceof FancyUI) {
|
|
259
|
+
ui.cwd = cwd;
|
|
260
|
+
const descriptions = Object.fromEntries(SLASH_HELP.split('\n').filter((l) => l.startsWith('/')).map((l) => [l.slice(1).split(/\s/)[0], l.replace(/^\/\S+(\s+\[[^\]]*\]|\s+<[^>]*>)?\s+/, '')]));
|
|
261
|
+
ui.commandList = () => [
|
|
262
|
+
...BUILTIN_COMMANDS.map((c) => ({ name: c.slice(1), description: descriptions[c.slice(1)] ?? '' })),
|
|
263
|
+
...[...loadCustomCommands(cwd).values()].map((c) => ({ name: c.name, description: c.description })),
|
|
264
|
+
];
|
|
265
|
+
const p = agent.permissions;
|
|
266
|
+
ui.onCycleMode = () => {
|
|
267
|
+
if (p.planMode) p.planMode = false;
|
|
268
|
+
else if (p.acceptEdits) {
|
|
269
|
+
p.acceptEdits = false;
|
|
270
|
+
p.planMode = true;
|
|
271
|
+
} else p.acceptEdits = true;
|
|
272
|
+
};
|
|
273
|
+
ui.modeLabel = () =>
|
|
274
|
+
p.planMode ? `${c.accent('⏸ plan mode on')} ${c.gray('(shift+tab to cycle)')}`
|
|
275
|
+
: p.acceptEdits ? `${c.accent('⏵⏵ accept edits on')} ${c.gray('(shift+tab to cycle)')}`
|
|
276
|
+
: p.mode === 'yolo' ? `${c.red('⏵⏵ yolo: everything runs without asking')}`
|
|
277
|
+
: '';
|
|
278
|
+
p.onDeny = () => agent.interrupt();
|
|
279
|
+
} else if (ui.fancy) ui.suspend();
|
|
258
280
|
ui.start();
|
|
259
281
|
const update = await checkForUpdate().catch(() => null);
|
|
260
282
|
const state = { pastedImages: [], jobsSeen: new Map(listJobs().map((j) => [j.id, j.status])), mcpServers };
|
|
@@ -275,6 +297,7 @@ async function interactive({ agent, ui, prompts, config, cwd, firstPrompt, mcpSe
|
|
|
275
297
|
ui.statusLine = () => {
|
|
276
298
|
const used = Math.round((estimateTokens(agent.session.messages) / config.contextWindow) * 100);
|
|
277
299
|
const branch = gitBranch(cwd);
|
|
300
|
+
if (ui instanceof FancyUI) return [config.model, branch ? `⎇ ${branch}` : '', `${used}% context`].filter(Boolean).join(' · ');
|
|
278
301
|
const parts = [config.model, `${used}% context`];
|
|
279
302
|
if (branch) parts.push(`⎇ ${branch}`);
|
|
280
303
|
if (agent.permissions.planMode) parts.push('plan mode');
|
package/src/permissions.js
CHANGED
|
@@ -7,6 +7,8 @@ export class Permissions {
|
|
|
7
7
|
this.ui = ui;
|
|
8
8
|
this.mode = mode;
|
|
9
9
|
this.planMode = false;
|
|
10
|
+
this.acceptEdits = false; // shift+tab mode: file edits run without asking
|
|
11
|
+
this.onDeny = null;
|
|
10
12
|
this.allowed = new Set(allow);
|
|
11
13
|
this.lock = Promise.resolve();
|
|
12
14
|
}
|
|
@@ -14,10 +16,12 @@ export class Permissions {
|
|
|
14
16
|
check(toolName, preview) {
|
|
15
17
|
const decide = async () => {
|
|
16
18
|
if (this.mode === 'yolo' || this.allowed.has(toolName)) return true;
|
|
19
|
+
if (this.acceptEdits && (toolName === 'edit_file' || toolName === 'write_file')) return true;
|
|
17
20
|
if (this.mode === 'deny') return false;
|
|
18
21
|
if (preview) preview.shown = true;
|
|
19
|
-
const answer = await this.ui.confirm(preview);
|
|
22
|
+
const answer = await this.ui.confirm(preview, toolName);
|
|
20
23
|
if (answer === 'always') this.allowed.add(toolName);
|
|
24
|
+
if (answer === 'no') this.onDeny?.();
|
|
21
25
|
return answer !== 'no';
|
|
22
26
|
};
|
|
23
27
|
const result = this.lock.then(decide, decide);
|
package/src/tui.js
ADDED
|
@@ -0,0 +1,853 @@
|
|
|
1
|
+
// Full terminal UI for interactive sessions in a real terminal.
|
|
2
|
+
//
|
|
3
|
+
// The bottom of the screen is a "live region" that arrakis redraws in place:
|
|
4
|
+
// the spinner, any queued messages, the input box and a hint/suggestion line.
|
|
5
|
+
// Everything else (replies, tool activity) is printed above it and scrolls
|
|
6
|
+
// normally. The input box stays usable while the agent works, so you can type
|
|
7
|
+
// ahead; Enter queues the message for when the current turn ends.
|
|
8
|
+
import readline from 'node:readline';
|
|
9
|
+
import fs from 'node:fs';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { spawnSync } from 'node:child_process';
|
|
12
|
+
import { UI, truncate } from './ui.js';
|
|
13
|
+
import { MarkdownStream, visibleLength } from './markdown.js';
|
|
14
|
+
|
|
15
|
+
const SPIN = ['·', '✢', '✳', '✶', '✻', '✽', '✻', '✶', '✳', '✢'];
|
|
16
|
+
const VERBS = ['Thinking', 'Reading the sands', 'Harvesting spice', 'Folding space', 'Charting the dunes', 'Working', 'Considering', 'Tracing', 'Mapping'];
|
|
17
|
+
const fmtTokens = (n) => (n >= 1000 ? `${(n / 1000).toFixed(1)}k` : String(n));
|
|
18
|
+
|
|
19
|
+
export class FancyUI extends UI {
|
|
20
|
+
constructor() {
|
|
21
|
+
super({ print: false });
|
|
22
|
+
this.fancy = true;
|
|
23
|
+
this.markdown = true;
|
|
24
|
+
this.buffer = '';
|
|
25
|
+
this.cursor = 0;
|
|
26
|
+
this.historyList = [];
|
|
27
|
+
this.histIdx = -1;
|
|
28
|
+
this.draft = '';
|
|
29
|
+
this.queued = [];
|
|
30
|
+
this.regionRows = 0;
|
|
31
|
+
this.regionCursorRow = 0;
|
|
32
|
+
this.active = false;
|
|
33
|
+
this.pending = null; // { resolve, label }
|
|
34
|
+
this.menu = null;
|
|
35
|
+
this.partial = '';
|
|
36
|
+
this.suggest = null;
|
|
37
|
+
this.showHelp = false;
|
|
38
|
+
this.exitArmed = false;
|
|
39
|
+
this.escArmed = false;
|
|
40
|
+
this.pasting = false;
|
|
41
|
+
this.tokens = 0;
|
|
42
|
+
this.modeLabel = () => '';
|
|
43
|
+
this.onCycleMode = null;
|
|
44
|
+
this.commandList = () => [];
|
|
45
|
+
this.cwd = process.cwd();
|
|
46
|
+
this.fileCache = null;
|
|
47
|
+
this.onKey = (str, key) => this.handleKey(str, key ?? {});
|
|
48
|
+
// Everything printed above the input box, so it can be reprinted when the
|
|
49
|
+
// terminal width changes (old lines re-wrap unpredictably otherwise).
|
|
50
|
+
this.transcript = [];
|
|
51
|
+
this.transcriptSize = 0;
|
|
52
|
+
this.lastCols = this.out.columns;
|
|
53
|
+
this.onResize = () => {
|
|
54
|
+
clearTimeout(this.resizeTimer);
|
|
55
|
+
this.resizeTimer = setTimeout(() => this.handleResize(), 60);
|
|
56
|
+
};
|
|
57
|
+
process.once('exit', () => this.cleanup());
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ---- lifecycle ---------------------------------------------------------
|
|
61
|
+
|
|
62
|
+
start() {
|
|
63
|
+
if (this.active) return;
|
|
64
|
+
const stdin = process.stdin;
|
|
65
|
+
readline.emitKeypressEvents(stdin);
|
|
66
|
+
stdin.setRawMode(true);
|
|
67
|
+
stdin.resume();
|
|
68
|
+
stdin.on('keypress', this.onKey);
|
|
69
|
+
this.out.on('resize', this.onResize);
|
|
70
|
+
this.out.write('\x1b[?2004h'); // bracketed paste: pasted newlines don't submit
|
|
71
|
+
this.active = true;
|
|
72
|
+
if (!this.historyLoaded && this.historyFile) {
|
|
73
|
+
this.historyLoaded = true;
|
|
74
|
+
try {
|
|
75
|
+
this.historyList = fs.readFileSync(this.historyFile, 'utf8').split('\n').filter(Boolean).slice(-1000);
|
|
76
|
+
} catch {}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
suspend() {
|
|
81
|
+
if (!this.active) return;
|
|
82
|
+
this.eraseRegion();
|
|
83
|
+
process.stdin.off('keypress', this.onKey);
|
|
84
|
+
this.out.off('resize', this.onResize);
|
|
85
|
+
this.out.write('\x1b[?2004l');
|
|
86
|
+
process.stdin.setRawMode(false);
|
|
87
|
+
this.active = false;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
resume() {
|
|
91
|
+
this.start();
|
|
92
|
+
this.render();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
close() {
|
|
96
|
+
this.stopSpinner();
|
|
97
|
+
this.flushPartial();
|
|
98
|
+
this.suspend();
|
|
99
|
+
process.stdin.pause();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
cleanup() {
|
|
103
|
+
if (this.active) {
|
|
104
|
+
this.eraseRegion();
|
|
105
|
+
this.out.write('\x1b[?2004l\x1b[?25h');
|
|
106
|
+
try {
|
|
107
|
+
process.stdin.setRawMode(false);
|
|
108
|
+
} catch {}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
setBusy(busy) {
|
|
113
|
+
this.busy = busy;
|
|
114
|
+
if (busy) this.tokens = 0;
|
|
115
|
+
this.render();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
addTokens(n) {
|
|
119
|
+
this.tokens += n;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ---- input -------------------------------------------------------------
|
|
123
|
+
|
|
124
|
+
prompt() {
|
|
125
|
+
return this.readLine(null);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
readLine(label) {
|
|
129
|
+
this.start();
|
|
130
|
+
if (!label && this.queued.length) {
|
|
131
|
+
const text = this.queued.shift();
|
|
132
|
+
this.echoUser(text);
|
|
133
|
+
this.render();
|
|
134
|
+
return Promise.resolve(text);
|
|
135
|
+
}
|
|
136
|
+
return new Promise((resolve) => {
|
|
137
|
+
this.pending = { resolve, label };
|
|
138
|
+
this.render();
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
cancelRead() {
|
|
143
|
+
if (this.pending?.label) {
|
|
144
|
+
const p = this.pending;
|
|
145
|
+
this.pending = null;
|
|
146
|
+
p.resolve(null);
|
|
147
|
+
}
|
|
148
|
+
if (this.menu) this.finishMenu(this.menu.options.length - 1);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
submit() {
|
|
152
|
+
let text = this.buffer;
|
|
153
|
+
this.buffer = '';
|
|
154
|
+
this.cursor = 0;
|
|
155
|
+
this.histIdx = -1;
|
|
156
|
+
this.suggest = null;
|
|
157
|
+
this.showHelp = false;
|
|
158
|
+
if (this.pending) {
|
|
159
|
+
const p = this.pending;
|
|
160
|
+
this.pending = null;
|
|
161
|
+
if (!p.label) {
|
|
162
|
+
if (text.trim()) this.remember(text);
|
|
163
|
+
this.echoUser(text);
|
|
164
|
+
} else {
|
|
165
|
+
this.eraseRegion();
|
|
166
|
+
this.out.write(`${p.label}${p.secret ? '' : text}\n`);
|
|
167
|
+
}
|
|
168
|
+
this.render();
|
|
169
|
+
p.resolve(text);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (!text.trim()) return this.render();
|
|
173
|
+
this.remember(text);
|
|
174
|
+
this.queued.push(text);
|
|
175
|
+
this.render();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
remember(text) {
|
|
179
|
+
if (this.historyList.at(-1) !== text) this.historyList.push(text);
|
|
180
|
+
if (this.historyFile) {
|
|
181
|
+
try {
|
|
182
|
+
fs.appendFileSync(this.historyFile, text.replace(/\n/g, ' ') + '\n', { mode: 0o600 });
|
|
183
|
+
} catch {}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
echoUser(text) {
|
|
188
|
+
const c = this.c;
|
|
189
|
+
const lines = text.split('\n');
|
|
190
|
+
this.printAbove(lines.map((l, i) => c.gray(`${i === 0 ? '>' : ' '} ${l}`)).join('\n') + '\n\n');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
insert(s) {
|
|
194
|
+
this.buffer = this.buffer.slice(0, this.cursor) + s + this.buffer.slice(this.cursor);
|
|
195
|
+
this.cursor += s.length;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
handleKey(str, key) {
|
|
199
|
+
if (this.menu) return this.menuKey(str, key);
|
|
200
|
+
if (key.name === 'paste-start') return (this.pasting = true);
|
|
201
|
+
if (key.name === 'paste-end') {
|
|
202
|
+
this.pasting = false;
|
|
203
|
+
return this.afterEdit();
|
|
204
|
+
}
|
|
205
|
+
if (this.pasting) {
|
|
206
|
+
if (str) this.insert(str === '\r' ? '\n' : str);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
const name = key.name;
|
|
210
|
+
if (key.ctrl && name === 'c') {
|
|
211
|
+
if (this.busy) return this.onEscape?.();
|
|
212
|
+
if (this.buffer) {
|
|
213
|
+
this.buffer = '';
|
|
214
|
+
this.cursor = 0;
|
|
215
|
+
return this.afterEdit();
|
|
216
|
+
}
|
|
217
|
+
if (this.exitArmed) {
|
|
218
|
+
if (this.pending) {
|
|
219
|
+
const p = this.pending;
|
|
220
|
+
this.pending = null;
|
|
221
|
+
p.resolve(null);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
this.cleanup();
|
|
225
|
+
process.exit(0);
|
|
226
|
+
}
|
|
227
|
+
this.exitArmed = true;
|
|
228
|
+
setTimeout(() => {
|
|
229
|
+
this.exitArmed = false;
|
|
230
|
+
this.render();
|
|
231
|
+
}, 2000).unref?.();
|
|
232
|
+
return this.render();
|
|
233
|
+
}
|
|
234
|
+
this.exitArmed = false;
|
|
235
|
+
if (key.ctrl && name === 'd') {
|
|
236
|
+
if (!this.buffer && this.pending) {
|
|
237
|
+
const p = this.pending;
|
|
238
|
+
this.pending = null;
|
|
239
|
+
p.resolve(null);
|
|
240
|
+
}
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
if (name === 'escape') {
|
|
244
|
+
if (this.busy) return this.onEscape?.();
|
|
245
|
+
if (this.suggest) {
|
|
246
|
+
this.suggest = null;
|
|
247
|
+
this.suggestDismissed = this.buffer;
|
|
248
|
+
return this.render();
|
|
249
|
+
}
|
|
250
|
+
if (this.showHelp) {
|
|
251
|
+
this.showHelp = false;
|
|
252
|
+
return this.render();
|
|
253
|
+
}
|
|
254
|
+
if (this.buffer) {
|
|
255
|
+
if (this.escArmed) {
|
|
256
|
+
this.buffer = '';
|
|
257
|
+
this.cursor = 0;
|
|
258
|
+
this.escArmed = false;
|
|
259
|
+
return this.afterEdit();
|
|
260
|
+
}
|
|
261
|
+
this.escArmed = true;
|
|
262
|
+
setTimeout(() => (this.escArmed = false), 800).unref?.();
|
|
263
|
+
}
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (name === 'tab' && key.shift) {
|
|
267
|
+
this.onCycleMode?.();
|
|
268
|
+
return this.render();
|
|
269
|
+
}
|
|
270
|
+
if (name === 'tab') {
|
|
271
|
+
if (this.suggest) this.applySuggestion();
|
|
272
|
+
return this.afterEdit();
|
|
273
|
+
}
|
|
274
|
+
if (name === 'return' || name === 'enter') {
|
|
275
|
+
if (key.meta || key.shift) {
|
|
276
|
+
this.insert('\n');
|
|
277
|
+
return this.afterEdit();
|
|
278
|
+
}
|
|
279
|
+
if (this.buffer.slice(0, this.cursor).endsWith('\\')) {
|
|
280
|
+
this.buffer = this.buffer.slice(0, this.cursor - 1) + '\n' + this.buffer.slice(this.cursor);
|
|
281
|
+
return this.afterEdit();
|
|
282
|
+
}
|
|
283
|
+
if (this.suggest?.kind === 'command') {
|
|
284
|
+
const item = this.suggest.items[this.suggest.selected];
|
|
285
|
+
this.buffer = `/${item.value}`;
|
|
286
|
+
this.cursor = this.buffer.length;
|
|
287
|
+
} else if (this.suggest?.kind === 'file') {
|
|
288
|
+
this.applySuggestion();
|
|
289
|
+
return this.afterEdit();
|
|
290
|
+
}
|
|
291
|
+
return this.submit();
|
|
292
|
+
}
|
|
293
|
+
if (name === 'up' || name === 'down') {
|
|
294
|
+
if (this.suggest) {
|
|
295
|
+
const n = this.suggest.items.length;
|
|
296
|
+
this.suggest.selected = (this.suggest.selected + (name === 'up' ? n - 1 : 1)) % n;
|
|
297
|
+
return this.render();
|
|
298
|
+
}
|
|
299
|
+
const lines = this.buffer.slice(0, this.cursor).split('\n');
|
|
300
|
+
const totalLines = this.buffer.split('\n').length;
|
|
301
|
+
if (name === 'up' && lines.length > 1) return this.moveLine(-1);
|
|
302
|
+
if (name === 'down' && lines.length < totalLines) return this.moveLine(1);
|
|
303
|
+
return this.historyStep(name === 'up' ? 1 : -1);
|
|
304
|
+
}
|
|
305
|
+
if (name === 'left') this.cursor = Math.max(0, key.meta ? this.wordStart() : this.cursor - 1);
|
|
306
|
+
else if (name === 'right') this.cursor = Math.min(this.buffer.length, key.meta ? this.wordEnd() : this.cursor + 1);
|
|
307
|
+
else if (name === 'home' || (key.ctrl && name === 'a')) this.cursor = this.buffer.lastIndexOf('\n', this.cursor - 1) + 1;
|
|
308
|
+
else if (name === 'end' || (key.ctrl && name === 'e')) {
|
|
309
|
+
const nl = this.buffer.indexOf('\n', this.cursor);
|
|
310
|
+
this.cursor = nl < 0 ? this.buffer.length : nl;
|
|
311
|
+
} else if (name === 'backspace') {
|
|
312
|
+
if (key.meta || (key.ctrl && name === 'w')) this.deleteTo(this.wordStart());
|
|
313
|
+
else if (this.cursor > 0) this.deleteTo(this.cursor - 1);
|
|
314
|
+
} else if (key.ctrl && name === 'w') this.deleteTo(this.wordStart());
|
|
315
|
+
else if (name === 'delete') {
|
|
316
|
+
this.buffer = this.buffer.slice(0, this.cursor) + this.buffer.slice(this.cursor + 1);
|
|
317
|
+
} else if (key.ctrl && name === 'u') this.deleteTo(this.buffer.lastIndexOf('\n', this.cursor - 1) + 1);
|
|
318
|
+
else if (key.ctrl && name === 'k') {
|
|
319
|
+
const nl = this.buffer.indexOf('\n', this.cursor);
|
|
320
|
+
this.buffer = this.buffer.slice(0, this.cursor) + (nl < 0 ? '' : this.buffer.slice(nl));
|
|
321
|
+
} else if (key.ctrl && name === 'l') {
|
|
322
|
+
this.out.write('\x1b[2J\x1b[H');
|
|
323
|
+
this.regionRows = 0;
|
|
324
|
+
} else if (str === '?' && !this.buffer) {
|
|
325
|
+
this.showHelp = !this.showHelp;
|
|
326
|
+
return this.render();
|
|
327
|
+
} else if (str && !key.ctrl && !key.meta && !/[\x00-\x08\x0e-\x1f\x7f]/.test(str)) {
|
|
328
|
+
this.insert(str === '\r' ? '\n' : str);
|
|
329
|
+
} else return;
|
|
330
|
+
this.afterEdit();
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
deleteTo(pos) {
|
|
334
|
+
this.buffer = this.buffer.slice(0, pos) + this.buffer.slice(this.cursor);
|
|
335
|
+
this.cursor = pos;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
wordStart() {
|
|
339
|
+
let i = this.cursor;
|
|
340
|
+
while (i > 0 && /\s/.test(this.buffer[i - 1])) i--;
|
|
341
|
+
while (i > 0 && !/\s/.test(this.buffer[i - 1])) i--;
|
|
342
|
+
return i;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
wordEnd() {
|
|
346
|
+
let i = this.cursor;
|
|
347
|
+
while (i < this.buffer.length && /\s/.test(this.buffer[i])) i++;
|
|
348
|
+
while (i < this.buffer.length && !/\s/.test(this.buffer[i])) i++;
|
|
349
|
+
return i;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
moveLine(dir) {
|
|
353
|
+
const before = this.buffer.slice(0, this.cursor);
|
|
354
|
+
const lineStart = before.lastIndexOf('\n') + 1;
|
|
355
|
+
const col = this.cursor - lineStart;
|
|
356
|
+
if (dir < 0) {
|
|
357
|
+
const prevStart = this.buffer.lastIndexOf('\n', lineStart - 2) + 1;
|
|
358
|
+
this.cursor = Math.min(prevStart + col, lineStart - 1);
|
|
359
|
+
} else {
|
|
360
|
+
const nextStart = this.buffer.indexOf('\n', this.cursor) + 1;
|
|
361
|
+
const nextEnd = this.buffer.indexOf('\n', nextStart);
|
|
362
|
+
this.cursor = Math.min(nextStart + col, nextEnd < 0 ? this.buffer.length : nextEnd);
|
|
363
|
+
}
|
|
364
|
+
this.render();
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
historyStep(dir) {
|
|
368
|
+
const n = this.historyList.length;
|
|
369
|
+
if (!n) return;
|
|
370
|
+
if (this.histIdx === -1) this.draft = this.buffer;
|
|
371
|
+
const next = Math.max(-1, Math.min(n - 1, this.histIdx + dir));
|
|
372
|
+
this.histIdx = next;
|
|
373
|
+
this.buffer = next === -1 ? this.draft : this.historyList[n - 1 - next];
|
|
374
|
+
this.cursor = this.buffer.length;
|
|
375
|
+
this.render();
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
afterEdit() {
|
|
379
|
+
this.updateSuggestions();
|
|
380
|
+
this.render();
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// ---- suggestions -------------------------------------------------------
|
|
384
|
+
|
|
385
|
+
files() {
|
|
386
|
+
if (this.fileCache && Date.now() - this.fileCache.time < 30000) return this.fileCache.list;
|
|
387
|
+
let list = [];
|
|
388
|
+
const git = spawnSync('git', ['ls-files', '--cached', '--others', '--exclude-standard'], { cwd: this.cwd, encoding: 'utf8', maxBuffer: 20e6 });
|
|
389
|
+
if (git.status === 0) list = git.stdout.split('\n').filter(Boolean);
|
|
390
|
+
else {
|
|
391
|
+
const walk = (dir, rel) => {
|
|
392
|
+
if (list.length > 5000) return;
|
|
393
|
+
let entries = [];
|
|
394
|
+
try {
|
|
395
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
396
|
+
} catch {
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
for (const e of entries) {
|
|
400
|
+
if (e.name === 'node_modules' || e.name === '.git' || e.name.startsWith('.')) continue;
|
|
401
|
+
const r = rel ? `${rel}/${e.name}` : e.name;
|
|
402
|
+
if (e.isDirectory()) walk(path.join(dir, e.name), r);
|
|
403
|
+
else list.push(r);
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
walk(this.cwd, '');
|
|
407
|
+
}
|
|
408
|
+
const dirs = new Set();
|
|
409
|
+
for (const f of list) {
|
|
410
|
+
const parts = f.split('/');
|
|
411
|
+
for (let i = 1; i < parts.length; i++) dirs.add(parts.slice(0, i).join('/') + '/');
|
|
412
|
+
}
|
|
413
|
+
list = [...dirs, ...list];
|
|
414
|
+
this.fileCache = { time: Date.now(), list };
|
|
415
|
+
return list;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
updateSuggestions() {
|
|
419
|
+
const prev = this.suggest;
|
|
420
|
+
this.suggest = null;
|
|
421
|
+
if (this.suggestDismissed === this.buffer) return;
|
|
422
|
+
this.suggestDismissed = null;
|
|
423
|
+
const before = this.buffer.slice(0, this.cursor);
|
|
424
|
+
let m;
|
|
425
|
+
if (/^\/[\w-]*$/.test(this.buffer)) {
|
|
426
|
+
const q = this.buffer.slice(1).toLowerCase();
|
|
427
|
+
const items = this.commandList()
|
|
428
|
+
.filter((c) => c.name.startsWith(q) || (q.length > 1 && c.name.includes(q)))
|
|
429
|
+
.sort((a, b) => Number(b.name.startsWith(q)) - Number(a.name.startsWith(q)))
|
|
430
|
+
.map((c) => ({ label: `/${c.name}`, value: c.name, hint: c.description }));
|
|
431
|
+
if (items.length) this.suggest = { kind: 'command', items, selected: 0 };
|
|
432
|
+
} else if ((m = before.match(/(?:^|\s)@([^\s]*)$/))) {
|
|
433
|
+
const q = m[1].toLowerCase();
|
|
434
|
+
const scored = [];
|
|
435
|
+
for (const f of this.files()) {
|
|
436
|
+
const lower = f.toLowerCase();
|
|
437
|
+
const i = lower.indexOf(q);
|
|
438
|
+
if (q && i < 0) continue;
|
|
439
|
+
const base = lower.slice(lower.lastIndexOf('/', lower.length - 2) + 1);
|
|
440
|
+
scored.push({ f, score: (base.startsWith(q) ? 0 : 2) + (i === 0 ? 0 : 1) + f.length / 1000 });
|
|
441
|
+
if (scored.length > 2000) break;
|
|
442
|
+
}
|
|
443
|
+
scored.sort((a, b) => a.score - b.score);
|
|
444
|
+
const items = scored.slice(0, 8).map(({ f }) => ({ label: `@${f}`, value: f }));
|
|
445
|
+
if (items.length) this.suggest = { kind: 'file', items, selected: 0, start: this.cursor - m[1].length - 1 };
|
|
446
|
+
}
|
|
447
|
+
if (this.suggest && prev?.kind === this.suggest.kind) {
|
|
448
|
+
this.suggest.selected = Math.min(prev.selected, this.suggest.items.length - 1);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
applySuggestion() {
|
|
453
|
+
const s = this.suggest;
|
|
454
|
+
if (!s) return;
|
|
455
|
+
const item = s.items[s.selected];
|
|
456
|
+
if (s.kind === 'command') {
|
|
457
|
+
this.buffer = `/${item.value} `;
|
|
458
|
+
this.cursor = this.buffer.length;
|
|
459
|
+
} else {
|
|
460
|
+
const insert = `@${item.value}${item.value.endsWith('/') ? '' : ' '}`;
|
|
461
|
+
this.buffer = this.buffer.slice(0, s.start) + insert + this.buffer.slice(this.cursor);
|
|
462
|
+
this.cursor = s.start + insert.length;
|
|
463
|
+
}
|
|
464
|
+
this.suggest = null;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// ---- permission menu ---------------------------------------------------
|
|
468
|
+
|
|
469
|
+
confirm(preview, toolName) {
|
|
470
|
+
return new Promise((resolve) => {
|
|
471
|
+
this.stopSpinnerTimer();
|
|
472
|
+
const what = { edit_file: 'edits', write_file: 'file writes', bash: 'commands', web_fetch: 'web fetches' }[toolName] ?? `"${toolName}"`;
|
|
473
|
+
this.menu = {
|
|
474
|
+
preview,
|
|
475
|
+
options: [
|
|
476
|
+
{ label: 'Yes', value: 'yes' },
|
|
477
|
+
{ label: `Yes, and don't ask again for ${what} this session`, value: 'always' },
|
|
478
|
+
{ label: 'No, and tell arrakis what to do differently', hint: 'esc', value: 'no' },
|
|
479
|
+
],
|
|
480
|
+
selected: 0,
|
|
481
|
+
resolve,
|
|
482
|
+
};
|
|
483
|
+
this.render();
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
menuKey(str, key) {
|
|
488
|
+
const m = this.menu;
|
|
489
|
+
const n = m.options.length;
|
|
490
|
+
if (key.name === 'up') m.selected = (m.selected + n - 1) % n;
|
|
491
|
+
else if (key.name === 'down' || (key.name === 'tab' && !key.shift)) m.selected = (m.selected + 1) % n;
|
|
492
|
+
else if (key.name === 'return' || key.name === 'enter') return this.finishMenu(m.selected);
|
|
493
|
+
else if (key.name === 'escape' || (key.ctrl && key.name === 'c')) return this.finishMenu(n - 1);
|
|
494
|
+
else if (/^[1-9]$/.test(str ?? '') && Number(str) <= n) return this.finishMenu(Number(str) - 1);
|
|
495
|
+
else if (str === 'y') return this.finishMenu(0);
|
|
496
|
+
else if (str === 'n') return this.finishMenu(n - 1);
|
|
497
|
+
this.render();
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
finishMenu(index) {
|
|
501
|
+
const m = this.menu;
|
|
502
|
+
if (!m) return;
|
|
503
|
+
this.menu = null;
|
|
504
|
+
const choice = m.options[index];
|
|
505
|
+
if (choice.value === 'no') this.printAbove(` ${this.c.gray('⎿')} ${this.c.red('Declined')}\n`);
|
|
506
|
+
this.resumeSpinnerTimer();
|
|
507
|
+
this.render();
|
|
508
|
+
m.resolve(choice.value);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// ---- output ------------------------------------------------------------
|
|
512
|
+
|
|
513
|
+
// Print complete lines above the live region.
|
|
514
|
+
printAbove(s) {
|
|
515
|
+
this.transcript.push(s);
|
|
516
|
+
this.transcriptSize += s.length;
|
|
517
|
+
while (this.transcriptSize > 2_000_000 && this.transcript.length > 1) this.transcriptSize -= this.transcript.shift().length;
|
|
518
|
+
if (!this.active) {
|
|
519
|
+
this.out.write(s);
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
this.eraseRegion();
|
|
523
|
+
this.out.write(s);
|
|
524
|
+
this.drawRegion();
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
handleResize() {
|
|
528
|
+
const cols = this.out.columns;
|
|
529
|
+
if (cols === this.lastCols || !this.active) return this.render();
|
|
530
|
+
this.lastCols = cols;
|
|
531
|
+
// Clear screen and scrollback, reprint the conversation at the new width.
|
|
532
|
+
this.out.write('\x1b[?25l\x1b[2J\x1b[3J\x1b[H' + this.transcript.join(''));
|
|
533
|
+
this.regionRows = 0;
|
|
534
|
+
this.drawRegion();
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
write(stream, s) {
|
|
538
|
+
if (!s) return;
|
|
539
|
+
this.partial += s;
|
|
540
|
+
const nl = this.partial.lastIndexOf('\n');
|
|
541
|
+
this.atLineStart = this.partial === '' || this.partial.endsWith('\n');
|
|
542
|
+
if (nl < 0) return;
|
|
543
|
+
const chunk = this.partial.slice(0, nl + 1);
|
|
544
|
+
this.partial = this.partial.slice(nl + 1);
|
|
545
|
+
this.atLineStart = this.partial === '';
|
|
546
|
+
this.printAbove(chunk);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
flushPartial() {
|
|
550
|
+
if (this.partial) {
|
|
551
|
+
const p = this.partial;
|
|
552
|
+
this.partial = '';
|
|
553
|
+
this.printAbove(p + '\n');
|
|
554
|
+
}
|
|
555
|
+
this.atLineStart = true;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
newlineIfNeeded() {
|
|
559
|
+
if (this.partial) this.flushPartial();
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
text(s) {
|
|
563
|
+
this.addTokens(s.length / 4);
|
|
564
|
+
if (this.mode === 'thinking') this.flushPartial();
|
|
565
|
+
if (this.mode !== 'text') {
|
|
566
|
+
this.md = new MarkdownStream(this.c, { width: this.width - 2 });
|
|
567
|
+
this.firstLine = true;
|
|
568
|
+
this.mode = 'text';
|
|
569
|
+
}
|
|
570
|
+
this.emitText(this.md.push(s));
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
emitText(rendered) {
|
|
574
|
+
if (!rendered) return;
|
|
575
|
+
const lines = rendered.split('\n');
|
|
576
|
+
lines.pop();
|
|
577
|
+
const out = lines.map((l) => {
|
|
578
|
+
const prefix = this.firstLine ? `${this.c.bold('⏺')} ` : ' ';
|
|
579
|
+
this.firstLine = false;
|
|
580
|
+
return prefix + l;
|
|
581
|
+
});
|
|
582
|
+
this.write(this.out, out.join('\n') + '\n');
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
thinking(s) {
|
|
586
|
+
this.addTokens(s.length / 4);
|
|
587
|
+
if (this.spinner) this.spinner.label = 'Thinking';
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
endText() {
|
|
591
|
+
if (this.md) {
|
|
592
|
+
this.emitText(this.md.flush());
|
|
593
|
+
this.md = null;
|
|
594
|
+
this.write(this.out, '\n');
|
|
595
|
+
}
|
|
596
|
+
this.flushPartial();
|
|
597
|
+
this.mode = null;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
toolStart(name, summary, opts) {
|
|
601
|
+
this.endText();
|
|
602
|
+
super.toolStart(name, summary, opts);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// ---- spinner -----------------------------------------------------------
|
|
606
|
+
|
|
607
|
+
startSpinner(label) {
|
|
608
|
+
if (this.spinner) {
|
|
609
|
+
if (label) this.spinner.label = label;
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
612
|
+
this.spinner = { label: label ?? VERBS[Math.floor(Math.random() * VERBS.length)], started: Date.now(), frame: 0, detail: '' };
|
|
613
|
+
this.resumeSpinnerTimer();
|
|
614
|
+
this.render();
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
resumeSpinnerTimer() {
|
|
618
|
+
if (!this.spinner || this.spinnerTimer) return;
|
|
619
|
+
this.spinnerTimer = setInterval(() => {
|
|
620
|
+
if (this.spinner) this.spinner.frame++;
|
|
621
|
+
this.render();
|
|
622
|
+
}, 120);
|
|
623
|
+
this.spinnerTimer.unref?.();
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
stopSpinnerTimer() {
|
|
627
|
+
clearInterval(this.spinnerTimer);
|
|
628
|
+
this.spinnerTimer = null;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
stopSpinner() {
|
|
632
|
+
this.stopSpinnerTimer();
|
|
633
|
+
if (this.spinner) {
|
|
634
|
+
this.spinner = null;
|
|
635
|
+
this.render();
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
spinnerDetail(text) {
|
|
640
|
+
if (this.spinner) this.spinner.detail = String(text).replace(/\x1b\[[0-9;]*[A-Za-z]/g, '').slice(0, 200);
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
clearSpinnerLine() {}
|
|
644
|
+
|
|
645
|
+
// ---- live region ---------------------------------------------------------
|
|
646
|
+
|
|
647
|
+
// How many screen rows the cursor is below the top of the drawn region.
|
|
648
|
+
// If the terminal got narrower since we drew, long lines have re-wrapped
|
|
649
|
+
// onto extra rows, so count them at the current width.
|
|
650
|
+
rowsAboveCursor() {
|
|
651
|
+
const cols = Math.max(1, this.out.columns || 100);
|
|
652
|
+
let rows = 0;
|
|
653
|
+
for (let i = 0; i < this.regionCursorRow; i++) rows += Math.max(1, Math.ceil((this.regionWidths[i] ?? 0) / cols));
|
|
654
|
+
return rows + Math.floor((this.regionCursorCol ?? 0) / cols);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
eraseRegion() {
|
|
658
|
+
if (!this.regionRows) return;
|
|
659
|
+
const up = this.rowsAboveCursor();
|
|
660
|
+
this.out.write(`${up ? `\x1b[${up}A` : ''}\r\x1b[J`);
|
|
661
|
+
this.regionRows = 0;
|
|
662
|
+
this.regionCursorRow = 0;
|
|
663
|
+
this.regionCursorCol = 0;
|
|
664
|
+
this.regionWidths = [];
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
render() {
|
|
668
|
+
if (!this.active) return;
|
|
669
|
+
const { lines, cursorRow, cursorCol } = this.layout();
|
|
670
|
+
let s = '\x1b[?25l';
|
|
671
|
+
if (this.regionRows) {
|
|
672
|
+
const up = this.rowsAboveCursor();
|
|
673
|
+
if (up) s += `\x1b[${up}A`;
|
|
674
|
+
}
|
|
675
|
+
s += '\r';
|
|
676
|
+
const max = Math.max(1, (this.out.columns || 100) - 1);
|
|
677
|
+
s += lines.map((l) => `\x1b[2K${visibleLength(l) > max ? truncate(l, max) : l}`).join('\n') + '\x1b[J';
|
|
678
|
+
const up = lines.length - 1 - cursorRow;
|
|
679
|
+
if (up > 0) s += `\x1b[${up}A`;
|
|
680
|
+
s += `\r${cursorCol > 0 ? `\x1b[${cursorCol}C` : ''}`;
|
|
681
|
+
if (cursorRow !== null && !this.menu) s += '\x1b[?25h';
|
|
682
|
+
this.out.write(s);
|
|
683
|
+
this.regionRows = lines.length;
|
|
684
|
+
this.regionCursorRow = cursorRow ?? lines.length - 1;
|
|
685
|
+
this.regionCursorCol = cursorCol;
|
|
686
|
+
this.regionWidths = lines.map((l) => Math.min(visibleLength(l), max));
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
drawRegion() {
|
|
690
|
+
this.regionRows = 0;
|
|
691
|
+
this.regionCursorRow = 0;
|
|
692
|
+
this.regionCursorCol = 0;
|
|
693
|
+
this.regionWidths = [];
|
|
694
|
+
this.render();
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
layout() {
|
|
698
|
+
const c = this.c;
|
|
699
|
+
const cols = Math.max(30, (this.out.columns || 100) - 1);
|
|
700
|
+
const lines = [];
|
|
701
|
+
if (this.spinner) {
|
|
702
|
+
const s = this.spinner;
|
|
703
|
+
const secs = Math.floor((Date.now() - s.started) / 1000);
|
|
704
|
+
const meta = [`${secs}s`];
|
|
705
|
+
if (this.tokens >= 1) meta.push(`↓ ${fmtTokens(Math.round(this.tokens))} tokens`);
|
|
706
|
+
meta.push('esc to interrupt');
|
|
707
|
+
let line = `${c.accent(SPIN[s.frame % SPIN.length])} ${c.accent(s.label + '…')} ${c.gray(`(${meta.join(' · ')})`)}`;
|
|
708
|
+
if (s.detail) line += c.gray(` ⎿ ${s.detail}`);
|
|
709
|
+
lines.push(truncate(line, cols - 1), '');
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
if (this.menu) return this.menuLayout(lines, cols);
|
|
713
|
+
|
|
714
|
+
for (const q of this.queued) lines.push(truncate(c.gray(` ⎿ queued: ${q.replace(/\n/g, ' ')}`), cols - 1));
|
|
715
|
+
if (this.queued.length) lines.push('');
|
|
716
|
+
if (this.pending?.label) lines.push(truncate(this.pending.label.trimEnd(), cols - 1));
|
|
717
|
+
|
|
718
|
+
// Input box
|
|
719
|
+
const inner = cols - 4;
|
|
720
|
+
const width = Math.max(10, inner - 2);
|
|
721
|
+
const shown = this.pending?.secret ? '•'.repeat(this.buffer.length) : this.buffer;
|
|
722
|
+
const rows = wrap(shown, width);
|
|
723
|
+
const boxTop = lines.length;
|
|
724
|
+
const border = this.busy && !this.pending ? c.gray : (s) => c.gray(s);
|
|
725
|
+
lines.push(border(`╭${'─'.repeat(cols - 2)}╮`));
|
|
726
|
+
let cursorRow = null;
|
|
727
|
+
let cursorCol = 0;
|
|
728
|
+
const placeholder = !this.buffer && !this.pending?.label ? (this.busy ? 'Type to queue a message…' : 'Ask arrakis anything, / for commands, @ for files') : '';
|
|
729
|
+
rows.forEach((r, i) => {
|
|
730
|
+
const prefix = i === 0 ? c.accent('> ') : ' ';
|
|
731
|
+
const body = placeholder && i === 0 ? c.gray(truncate(placeholder, width)) : r.text;
|
|
732
|
+
const pad = Math.max(0, width - visibleLength(body));
|
|
733
|
+
lines.push(`${border('│')} ${prefix}${body}${' '.repeat(pad)} ${border('│')}`);
|
|
734
|
+
if (this.cursor >= r.start && this.cursor <= r.end && cursorRow === null) {
|
|
735
|
+
if (this.cursor === r.end && r.full && i < rows.length - 1) return;
|
|
736
|
+
cursorRow = lines.length - 1;
|
|
737
|
+
cursorCol = 4 + (this.cursor - r.start);
|
|
738
|
+
}
|
|
739
|
+
});
|
|
740
|
+
if (cursorRow === null) {
|
|
741
|
+
cursorRow = boxTop + rows.length;
|
|
742
|
+
cursorCol = 4 + (rows.at(-1)?.text.length ?? 0);
|
|
743
|
+
}
|
|
744
|
+
lines.push(border(`╰${'─'.repeat(cols - 2)}╯`));
|
|
745
|
+
|
|
746
|
+
// Below the box: suggestions, shortcuts, or the hint line.
|
|
747
|
+
if (this.suggest) {
|
|
748
|
+
for (const [i, item] of this.suggest.items.slice(0, 8).entries()) {
|
|
749
|
+
const sel = i === this.suggest.selected;
|
|
750
|
+
const label = sel ? c.accent(item.label) : item.label;
|
|
751
|
+
const hint = item.hint ? ` ${c.gray(item.hint)}` : '';
|
|
752
|
+
lines.push(truncate(` ${label.padEnd ? label : label}${' '.repeat(Math.max(1, 22 - item.label.length))}${hint}`, cols - 1));
|
|
753
|
+
}
|
|
754
|
+
} else if (this.showHelp) {
|
|
755
|
+
const help = [
|
|
756
|
+
['/ for commands', '@ for file paths', '\\⏎ or shift+⏎ for newline'],
|
|
757
|
+
['shift+tab to cycle modes', 'esc to interrupt', '↑↓ for history'],
|
|
758
|
+
['ctrl+c to clear / exit', 'tab to complete', 'esc esc to clear input'],
|
|
759
|
+
];
|
|
760
|
+
for (const row of help) lines.push(c.gray(' ' + row.map((x) => x.padEnd(28)).join('')));
|
|
761
|
+
} else {
|
|
762
|
+
const left = this.exitArmed ? c.yellow('Press Ctrl-C again to exit') : this.modeLabel() || c.gray('? for shortcuts');
|
|
763
|
+
const right = c.gray(this.statusLine());
|
|
764
|
+
const gap = Math.max(2, cols - 2 - visibleLength(left) - visibleLength(right));
|
|
765
|
+
lines.push(truncate(` ${left}${' '.repeat(gap)}${right}`, cols - 1));
|
|
766
|
+
}
|
|
767
|
+
return { lines, cursorRow, cursorCol };
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
menuLayout(lines, cols) {
|
|
771
|
+
const c = this.c;
|
|
772
|
+
const m = this.menu;
|
|
773
|
+
const inner = cols - 4;
|
|
774
|
+
const row = (s = '') => `${c.accent('│')} ${truncate(s, inner)}${' '.repeat(Math.max(0, inner - visibleLength(truncate(s, inner))))} ${c.accent('│')}`;
|
|
775
|
+
lines.push(c.accent(`╭${'─'.repeat(cols - 2)}╮`));
|
|
776
|
+
lines.push(row(c.bold(m.preview?.title ?? 'Allow this action?')));
|
|
777
|
+
const body = m.preview?.lines ?? [];
|
|
778
|
+
if (body.length) {
|
|
779
|
+
lines.push(row());
|
|
780
|
+
for (const { sign, text, n } of body.slice(0, 18)) {
|
|
781
|
+
const num = n !== undefined ? c.gray(String(n).padStart(4) + ' ') : ' ';
|
|
782
|
+
const t = `${sign} ${text}`;
|
|
783
|
+
lines.push(row(`${num}${sign === '+' ? c.green(t) : sign === '-' ? c.red(t) : c.gray(t)}`));
|
|
784
|
+
}
|
|
785
|
+
if (body.length > 18) lines.push(row(c.gray(` … ${body.length - 18} more lines`)));
|
|
786
|
+
}
|
|
787
|
+
lines.push(row());
|
|
788
|
+
lines.push(row('Do you want to proceed?'));
|
|
789
|
+
m.options.forEach((o, i) => {
|
|
790
|
+
const sel = i === m.selected;
|
|
791
|
+
const text = `${i + 1}. ${o.label}${o.hint ? c.gray(` (${o.hint})`) : ''}`;
|
|
792
|
+
lines.push(row(sel ? `${c.accent('❯')} ${c.accent(text)}` : ` ${text}`));
|
|
793
|
+
});
|
|
794
|
+
lines.push(c.accent(`╰${'─'.repeat(cols - 2)}╯`));
|
|
795
|
+
return { lines, cursorRow: lines.length - 1, cursorCol: 0 };
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
// ---- welcome -----------------------------------------------------------
|
|
799
|
+
|
|
800
|
+
banner({ version, model, provider, cwd, instructions = [], resumed, mcp = [], hooks = 0, update }) {
|
|
801
|
+
const c = this.c;
|
|
802
|
+
const home = process.env.HOME ?? '';
|
|
803
|
+
const tild = (p) => (home && p.startsWith(home) ? '~' + p.slice(home.length) : p);
|
|
804
|
+
const rows = [
|
|
805
|
+
`${c.accent('◆')} Welcome to ${c.bold('Arrakis')}!`,
|
|
806
|
+
'',
|
|
807
|
+
c.gray(' /help for commands, /status for your current setup'),
|
|
808
|
+
'',
|
|
809
|
+
c.gray(` cwd: ${tild(cwd)}`),
|
|
810
|
+
c.gray(` model: ${model} · ${provider}`),
|
|
811
|
+
];
|
|
812
|
+
if (resumed) rows.push(c.gray(` session: ${resumed}`));
|
|
813
|
+
if (mcp.length) rows.push(c.gray(` mcp: ${mcp.map((s) => (s.status === 'connected' ? `${s.name} (${s.tools.length} tools)` : `${s.name} (failed)`)).join(', ')}`));
|
|
814
|
+
if (hooks) rows.push(c.gray(` hooks: ${hooks} active`));
|
|
815
|
+
const cols = Math.max(30, (this.out.columns || 100) - 1);
|
|
816
|
+
const inner = Math.min(cols - 4, Math.max(46, ...rows.map(visibleLength)));
|
|
817
|
+
const line = (s) => `${c.accent('│')} ${truncate(s, inner)}${' '.repeat(Math.max(0, inner - visibleLength(truncate(s, inner))))} ${c.accent('│')}`;
|
|
818
|
+
let out = `\n${c.accent(`╭${'─'.repeat(inner + 2)}╮`)}\n${rows.map(line).join('\n')}\n${c.accent(`╰${'─'.repeat(inner + 2)}╯`)}\n`;
|
|
819
|
+
if (update) out += `\n ${c.accent('↑')} ${c.bold(`Arrakis ${update} is available.`)} ${c.gray('Run: npm install -g @mumbrane/arrakis')}\n`;
|
|
820
|
+
if (!resumed) {
|
|
821
|
+
out += `\n ${c.gray('Tips for getting started:')}\n`;
|
|
822
|
+
const tips = [
|
|
823
|
+
instructions.length ? null : 'Run /init to write an ARRAKIS.md with notes about this project',
|
|
824
|
+
'Ask about the code, or ask for a change, a fix or a new feature',
|
|
825
|
+
'Use /plan for big changes, so arrakis proposes a plan before editing',
|
|
826
|
+
'Mention files with @, and press shift+tab to let it edit without asking',
|
|
827
|
+
].filter(Boolean);
|
|
828
|
+
tips.forEach((t, i) => (out += ` ${c.gray(`${i + 1}. ${t}`)}\n`));
|
|
829
|
+
}
|
|
830
|
+
this.printAbove(out + '\n');
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
info(msg) {
|
|
834
|
+
this.endText();
|
|
835
|
+
this.write(this.out, this.c.gray(msg) + '\n');
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
// Wrap text into rows of at most `width` characters, tracking source offsets.
|
|
840
|
+
function wrap(text, width) {
|
|
841
|
+
const rows = [];
|
|
842
|
+
let offset = 0;
|
|
843
|
+
for (const line of text.split('\n')) {
|
|
844
|
+
if (!line.length) rows.push({ text: '', start: offset, end: offset, full: false });
|
|
845
|
+
for (let i = 0; i < line.length; i += width) {
|
|
846
|
+
const part = line.slice(i, i + width);
|
|
847
|
+
rows.push({ text: part, start: offset + i, end: offset + i + part.length, full: part.length === width });
|
|
848
|
+
}
|
|
849
|
+
offset += line.length + 1;
|
|
850
|
+
}
|
|
851
|
+
if (!rows.length) rows.push({ text: '', start: 0, end: 0, full: false });
|
|
852
|
+
return rows;
|
|
853
|
+
}
|
package/src/ui.js
CHANGED
|
@@ -317,7 +317,7 @@ export class UI {
|
|
|
317
317
|
const lc = this.lc;
|
|
318
318
|
const s = String(summary ?? '').split('\n')[0];
|
|
319
319
|
const label = TOOL_LABELS[name] ?? name;
|
|
320
|
-
const head = prefix ? ` ${lc.gray('⎿')} ${lc.gray(prefix)} ` : `${this.atLineStart ? '' : '\n'}${lc.accent('
|
|
320
|
+
const head = prefix ? ` ${lc.gray('⎿')} ${lc.gray(prefix)} ` : `${this.atLineStart ? '' : '\n'}${lc.accent('⏺')} `;
|
|
321
321
|
this.write(this.log, truncate(`${head}${lc.bold(label)}${s ? lc.gray(`(${s})`) : ''}`, this.width) + '\n');
|
|
322
322
|
}
|
|
323
323
|
|