@mumbrane/arrakis 0.8.3 → 0.12.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/src/tui.js CHANGED
@@ -8,7 +8,7 @@
8
8
  import readline from 'node:readline';
9
9
  import fs from 'node:fs';
10
10
  import path from 'node:path';
11
- import { spawnSync } from 'node:child_process';
11
+ import { spawn, spawnSync } from 'node:child_process';
12
12
  import { UI, truncate } from './ui.js';
13
13
  import { MarkdownStream, visibleLength } from './markdown.js';
14
14
  import { GLYPH, LOGO, PALETTE } from './theme.js';
@@ -120,6 +120,16 @@ export class FancyUI extends UI {
120
120
  this.render();
121
121
  }
122
122
 
123
+ // A bell, and on macOS a notification, when a long turn finishes.
124
+ notify(message, secs) {
125
+ if (process.env.ARRAKIS_NO_NOTIFY) return;
126
+ this.out.write('\x07');
127
+ if (process.platform === 'darwin' && secs >= 60) {
128
+ const text = message.replace(/["\\]/g, '').slice(0, 120);
129
+ spawn('osascript', ['-e', `display notification "${text}" with title "arrakis"`], { stdio: 'ignore', detached: true }).unref();
130
+ }
131
+ }
132
+
123
133
  addTokens(n) {
124
134
  this.tokens += n;
125
135
  }
@@ -213,8 +223,60 @@ export class FancyUI extends UI {
213
223
  this.cursor += s.length;
214
224
  }
215
225
 
226
+ // Ctrl+R: search earlier messages. Type to narrow, Ctrl+R for older matches,
227
+ // Enter to keep the match, Esc to go back to what you had.
228
+ searchKey(str, key) {
229
+ const h = this.hsearch;
230
+ const find = () => {
231
+ let skip = h.skip;
232
+ for (let i = this.historyList.length - 1; i >= 0; i--) {
233
+ if (!this.historyList[i].toLowerCase().includes(h.query.toLowerCase())) continue;
234
+ if (skip-- > 0) continue;
235
+ return this.historyList[i];
236
+ }
237
+ return null;
238
+ };
239
+ if (key.ctrl && key.name === 'r') h.skip++;
240
+ else if (key.name === 'escape' || (key.ctrl && (key.name === 'g' || key.name === 'c'))) {
241
+ this.buffer = h.saved;
242
+ this.cursor = this.buffer.length;
243
+ this.hsearch = null;
244
+ return this.render();
245
+ } else if (key.name === 'backspace') {
246
+ h.query = h.query.slice(0, -1);
247
+ h.skip = 0;
248
+ } else if (str && !key.ctrl && !key.meta && str.length === 1 && str >= ' ') {
249
+ h.query += str;
250
+ h.skip = 0;
251
+ } else {
252
+ this.hsearch = null;
253
+ if (key.name === 'return' || key.name === 'enter') return this.render();
254
+ return this.handleKey(str, key);
255
+ }
256
+ const match = find();
257
+ if (match === null && h.skip > 0) h.skip--;
258
+ const found = match ?? find();
259
+ if (found !== null) {
260
+ this.buffer = found;
261
+ this.cursor = found.length;
262
+ }
263
+ h.failed = found === null;
264
+ this.render();
265
+ }
266
+
267
+ setDraft(text) {
268
+ this.buffer = text;
269
+ this.cursor = text.length;
270
+ this.render();
271
+ }
272
+
216
273
  handleKey(str, key) {
217
274
  if (this.menu) return this.menuKey(str, key);
275
+ if (this.hsearch) return this.searchKey(str, key);
276
+ if (key.ctrl && key.name === 'r' && !this.busy) {
277
+ this.hsearch = { query: '', skip: 0, saved: this.buffer };
278
+ return this.render();
279
+ }
218
280
  if (key.name === 'paste-start') return (this.pasting = true);
219
281
  if (key.name === 'paste-end') {
220
282
  this.pasting = false;
@@ -269,16 +331,20 @@ export class FancyUI extends UI {
269
331
  this.showHelp = false;
270
332
  return this.render();
271
333
  }
272
- if (this.buffer) {
273
- if (this.escArmed) {
334
+ // Esc Esc: clear what you typed; on an empty composer, rewind. (Two quick
335
+ // presses arrive as one event with the sequence "\x1b\x1b".)
336
+ if (this.escArmed || key.sequence === '\x1b\x1b') {
337
+ this.escArmed = false;
338
+ if (this.buffer) {
274
339
  this.buffer = '';
275
340
  this.cursor = 0;
276
- this.escArmed = false;
277
341
  return this.afterEdit();
278
342
  }
279
- this.escArmed = true;
280
- setTimeout(() => (this.escArmed = false), 800).unref?.();
343
+ if (this.pending && !this.pending.label) this.onRewind?.();
344
+ return;
281
345
  }
346
+ this.escArmed = true;
347
+ setTimeout(() => (this.escArmed = false), 800).unref?.();
282
348
  return;
283
349
  }
284
350
  if (name === 'tab' && key.shift) {
@@ -336,6 +402,17 @@ export class FancyUI extends UI {
336
402
  else if (key.ctrl && name === 'k') {
337
403
  const nl = this.buffer.indexOf('\n', this.cursor);
338
404
  this.buffer = this.buffer.slice(0, this.cursor) + (nl < 0 ? '' : this.buffer.slice(nl));
405
+ } else if (key.ctrl && name === 'o') {
406
+ // Show the last tool's full output.
407
+ if (this.lastOutput) {
408
+ const lines = this.lastOutput.split('\n');
409
+ const shown = lines.slice(0, 400).map((l) => ` ${this.c.mist(l)}`).join('\n');
410
+ this.printAbove(`${shown}${lines.length > 400 ? `\n ${this.c.faint(`… ${lines.length - 400} more lines`)}` : ''}\n`);
411
+ }
412
+ return this.render();
413
+ } else if (key.ctrl && name === 'v') {
414
+ this.onPasteImage?.();
415
+ return this.render();
339
416
  } else if (key.ctrl && name === 'l') {
340
417
  this.clearScreen();
341
418
  return this.render();
@@ -484,16 +561,17 @@ export class FancyUI extends UI {
484
561
 
485
562
  // ---- permission menu ---------------------------------------------------
486
563
 
487
- confirm(preview, toolName) {
564
+ confirm(preview, toolName, { prefix } = {}) {
488
565
  return new Promise((resolve) => {
489
566
  this.stopSpinnerTimer();
490
- const what = { edit_file: 'edits', write_file: 'file writes', bash: 'commands', web_fetch: 'web fetches' }[toolName] ?? `"${toolName}"`;
567
+ const what = { edit_file: 'edits', write_file: 'file writes', bash: 'commands', web_fetch: 'web fetches', browser: 'browser actions' }[toolName] ?? `"${toolName}"`;
491
568
  this.menu = {
492
569
  preview,
493
570
  toolName,
494
571
  options: [
495
572
  { label: 'Yes', value: 'yes' },
496
- { label: `Always allow ${what}`, value: 'always' },
573
+ ...(prefix ? [{ label: `Always \`${prefix}\` here`, value: 'prefix' }] : []),
574
+ { label: prefix ? 'All commands this session' : `Always allow ${what}`, value: 'always' },
497
575
  { label: 'No, I\'ll explain', value: 'no' },
498
576
  ],
499
577
  selected: 0,
@@ -523,6 +601,7 @@ export class FancyUI extends UI {
523
601
  const choice = m.options[index];
524
602
  if (choice.value === 'no') this.printAbove(` ${this.c.faint(GLYPH.result)} ${this.c.rose('declined')} ${this.c.mist('· tell arrakis what to do instead')}\n`);
525
603
  else if (choice.value === 'always') this.printAbove(` ${this.c.faint(GLYPH.result)} ${this.c.mint('allowed for the rest of this session')}\n`);
604
+ else if (choice.value === 'prefix') this.printAbove(` ${this.c.faint(GLYPH.result)} ${this.c.mint(`${choice.label.replace(/^Always /, '').replace(/ here$/, '')} will run without asking in this project`)} ${this.c.mist('· /permissions to change')}\n`);
526
605
  this.resumeSpinnerTimer();
527
606
  this.render();
528
607
  m.resolve(choice.value);
@@ -841,11 +920,14 @@ export class FancyUI extends UI {
841
920
  const help = [
842
921
  [k('/', 'commands'), k('@', 'attach files'), k('\\⏎', 'new line')],
843
922
  [k('⇧⇥', 'cycle modes'), k('esc', 'interrupt'), k('↑↓', 'history')],
844
- [k('tab', 'complete'), k('esc esc', 'clear input'), k('ctrl+c', 'clear / exit')],
923
+ [k('tab', 'complete'), k('esc esc', 'clear input / rewind'), k('ctrl+c', 'clear / exit')],
924
+ [k('ctrl+r', 'search history'), k('ctrl+o', 'full tool output'), k('ctrl+v', 'paste image')],
845
925
  ];
846
926
  for (const row of help) lines.push(' ' + row.map((x) => x + ' '.repeat(Math.max(2, 24 - visibleLength(x)))).join(''));
847
927
  } else {
848
- const left = this.exitArmed ? c.amber('press ctrl+c again to exit') : this.modeLabel() || c.faint('? shortcuts');
928
+ const left = this.hsearch
929
+ ? `${c.violet('search history:')} ${c.lilac(this.hsearch.query)}${c.faint('▏')} ${this.hsearch.failed ? c.rose('no match') : c.faint('ctrl+r older · enter keep · esc cancel')}`
930
+ : this.exitArmed ? c.amber('press ctrl+c again to exit') : this.escArmed && !this.buffer && this.pending ? c.amber('esc again to rewind the conversation') : this.modeLabel() || c.faint('? shortcuts');
849
931
  const right = c.mist(this.statusLine());
850
932
  const gap = Math.max(2, cols - 3 - visibleLength(left) - visibleLength(right));
851
933
  lines.push(truncate(` ${left}${' '.repeat(gap)}${right}`, cols - 1));
@@ -892,7 +974,7 @@ export class FancyUI extends UI {
892
974
 
893
975
  // ---- welcome -----------------------------------------------------------
894
976
 
895
- banner({ version, model, provider, cwd, instructions = [], resumed, mcp = [], hooks = 0, update }) {
977
+ banner({ version, model, provider, cwd, instructions = [], resumed, mcp = [], hooks = 0, skills = 0, update }) {
896
978
  const c = this.c;
897
979
  const home = process.env.HOME ?? '';
898
980
  const tild = (p) => (home && p.startsWith(home) ? '~' + p.slice(home.length) : p);
@@ -907,6 +989,7 @@ export class FancyUI extends UI {
907
989
  if (instructions.length) facts.push(['notes', c.lilac(instructions.map((f) => tild(f).split('/').pop()).join(', '))]);
908
990
  if (mcp.length) facts.push(['mcp', mcp.map((s) => (s.status === 'connected' ? `${c.lilac(s.name)} ${c.faint(`${s.tools.length} tools`)}` : c.rose(`${s.name} failed`))).join(c.faint(' · '))]);
909
991
  if (hooks) facts.push(['hooks', c.lilac(`${hooks} active`)]);
992
+ if (skills) facts.push(['skills', c.lilac(`${skills} available`)]);
910
993
  if (resumed) facts.push(['session', c.lilac(resumed)]);
911
994
  for (const [k, v] of facts) out += ` ${c.iris(GLYPH.tool)} ${c.mist(k.padEnd(8))} ${v}\n`;
912
995
  if (update) out += `\n ${c.orchid('↑')} ${c.lilac(`arrakis ${update} is out`)} ${c.faint('·')} ${c.mist('npm install -g @mumbrane/arrakis')}\n`;
package/src/ui.js CHANGED
@@ -14,7 +14,7 @@ const SPINNER = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇',
14
14
  const VERBS = ['Thinking', 'Reading the sands', 'Harvesting spice', 'Folding space', 'Charting the dunes', 'Working', 'Considering'];
15
15
 
16
16
  export const TOOL_LABELS = {
17
- read_file: 'Read', write_file: 'Write', edit_file: 'Edit', bash: 'Bash', grep: 'Search',
17
+ read_file: 'Read', write_file: 'Write', edit_file: 'Edit', multi_edit: 'Edit', bash: 'Bash', grep: 'Search',
18
18
  list_files: 'List', task: 'Agent', todo_write: 'Todos', web_fetch: 'Fetch',
19
19
  };
20
20
 
@@ -24,8 +24,9 @@ const tildify = (p) => (p.startsWith(os.homedir()) ? '~' + p.slice(os.homedir().
24
24
  // else (tool activity, notices, spinner) to stderr, so output can be piped.
25
25
  // "Fancy" mode (a real terminal) adds the spinner, input chrome and Markdown.
26
26
  export class UI {
27
- constructor({ print = false } = {}) {
27
+ constructor({ print = false, quiet = false } = {}) {
28
28
  this.print = print;
29
+ this.quiet = quiet; // --json: no streamed reply text
29
30
  this.out = process.stdout;
30
31
  this.log = print ? process.stderr : process.stdout;
31
32
  this.c = colors(this.out);
@@ -45,6 +46,12 @@ export class UI {
45
46
  this.onEscape = null;
46
47
  }
47
48
 
49
+ // Rebuild colors after a theme change.
50
+ retheme() {
51
+ this.c = colors(this.out);
52
+ this.lc = colors(this.log);
53
+ }
54
+
48
55
  get width() {
49
56
  return Math.max(40, (this.out.columns || 100) - 1);
50
57
  }
@@ -282,6 +289,7 @@ export class UI {
282
289
  }
283
290
 
284
291
  text(s) {
292
+ if (this.quiet) return;
285
293
  this.stopSpinner();
286
294
  if (this.mode === 'thinking') this.write(this.log, '\n\n');
287
295
  else if (this.mode !== 'text') {
@@ -319,10 +327,11 @@ export class UI {
319
327
  toolResult(result, isError = false, { lines: extra } = {}) {
320
328
  const t = this.lc;
321
329
  const all = String(result).split('\n');
330
+ this.lastOutput = String(result);
322
331
  const shown = all.slice(0, 4).map((l) => truncate(l, this.width - 6));
323
332
  const color = isError ? t.rose : t.mist;
324
333
  let body = shown.map((l, i) => ` ${i === 0 ? t.faint(GLYPH.result) : ' '} ${color(l)}`).join('\n');
325
- if (all.length > 4) body += `\n ${t.faint(`+${all.length - 4} more lines`)}`;
334
+ if (all.length > 4) body += `\n ${t.faint(`+${all.length - 4} more lines${this.fancy ? ' · ctrl+o to show all' : ''}`)}`;
326
335
  this.write(this.log, body + '\n');
327
336
  if (extra?.length) this.diff(extra);
328
337
  }
@@ -356,15 +365,17 @@ export class UI {
356
365
  }
357
366
  }
358
367
 
359
- async confirm(preview) {
368
+ async confirm(preview, toolName, { prefix } = {}) {
360
369
  this.stopSpinner();
361
370
  const t = this.lc;
362
371
  if (preview) {
363
372
  this.write(this.log, ` ${t.orchid(GLYPH.user)} ${t.bold(t.lilac(preview.title))}\n`);
364
373
  if (preview.lines?.length) this.diff(preview.lines);
365
374
  }
366
- const answer = await this.readLine(` ${t.orchid(GLYPH.user)} ${t.bold('Allow?')} ${t.mist('[y]es [a]lways this session [n]o')} `);
375
+ const choices = prefix ? `[y]es [p] always allow "${prefix}" here [a]lways this session [n]o` : '[y]es [a]lways this session [n]o';
376
+ const answer = await this.readLine(` ${t.orchid(GLYPH.user)} ${t.bold('Allow?')} ${t.mist(choices)} `);
367
377
  const a = (answer ?? 'n').trim().toLowerCase();
378
+ if (prefix && a === 'p') return 'prefix';
368
379
  if (a === 'a' || a === 'always') return 'always';
369
380
  if (a === 'y' || a === 'yes' || a === '') return 'yes';
370
381
  return 'no';