@mumbrane/arrakis 0.0.0-stage → 0.2.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 +74 -2
- package/bin/arrakis.js +7 -0
- package/index.js +8 -0
- package/package.json +37 -4
- package/src/agent.js +135 -0
- package/src/cli.js +247 -0
- package/src/client.js +90 -0
- package/src/config.js +60 -0
- package/src/permissions.js +17 -0
- package/src/prompt.js +81 -0
- package/src/session.js +141 -0
- package/src/tools.js +377 -0
- package/src/ui.js +173 -0
- package/src/version.js +3 -0
package/src/ui.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import readline from 'node:readline';
|
|
2
|
+
|
|
3
|
+
const colorOn = (stream) => stream.isTTY && !process.env.NO_COLOR;
|
|
4
|
+
const paint = (code, on) => (s) => (on ? `\x1b[${code}m${s}\x1b[0m` : String(s));
|
|
5
|
+
|
|
6
|
+
export function colors(stream = process.stdout) {
|
|
7
|
+
const on = colorOn(stream);
|
|
8
|
+
return {
|
|
9
|
+
dim: paint('2', on), bold: paint('1', on), italic: paint('3', on),
|
|
10
|
+
red: paint('31', on), green: paint('32', on), yellow: paint('33', on),
|
|
11
|
+
blue: paint('34', on), magenta: paint('35', on), cyan: paint('36', on),
|
|
12
|
+
accent: paint('38;5;208', on), // spice orange
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Terminal I/O. In print mode (-p) the answer goes to stdout and everything
|
|
17
|
+
// else (tool activity, notices) to stderr, so output can be piped.
|
|
18
|
+
export class UI {
|
|
19
|
+
constructor({ print = false } = {}) {
|
|
20
|
+
this.print = print;
|
|
21
|
+
this.out = process.stdout;
|
|
22
|
+
this.log = print ? process.stderr : process.stdout;
|
|
23
|
+
this.c = colors(this.out);
|
|
24
|
+
this.lc = colors(this.log);
|
|
25
|
+
this.rl = null;
|
|
26
|
+
this.queue = [];
|
|
27
|
+
this.waiter = null;
|
|
28
|
+
this.closed = false;
|
|
29
|
+
this.atLineStart = true;
|
|
30
|
+
this.mode = null; // 'text' | 'thinking'
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
start() {
|
|
34
|
+
this.rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: !!process.stdin.isTTY, historySize: 200 });
|
|
35
|
+
this.rl.on('line', (line) => {
|
|
36
|
+
if (this.waiter) {
|
|
37
|
+
const w = this.waiter;
|
|
38
|
+
this.waiter = null;
|
|
39
|
+
w(line);
|
|
40
|
+
} else this.queue.push(line);
|
|
41
|
+
});
|
|
42
|
+
this.rl.on('close', () => {
|
|
43
|
+
this.closed = true;
|
|
44
|
+
this.cancelRead();
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
onInterrupt(handler) {
|
|
49
|
+
if (this.rl) this.rl.on('SIGINT', handler);
|
|
50
|
+
process.on('SIGINT', handler);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
close() {
|
|
54
|
+
this.rl?.close();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
readLine(promptText) {
|
|
58
|
+
if (!this.rl) return Promise.resolve(null);
|
|
59
|
+
// Piped input can close readline while queued lines remain.
|
|
60
|
+
if (this.closed) {
|
|
61
|
+
if (!this.queue.length) return Promise.resolve(null);
|
|
62
|
+
this.out.write(promptText);
|
|
63
|
+
} else {
|
|
64
|
+
this.rl.setPrompt(promptText);
|
|
65
|
+
this.rl.prompt();
|
|
66
|
+
}
|
|
67
|
+
if (this.queue.length) {
|
|
68
|
+
const line = this.queue.shift();
|
|
69
|
+
if (!process.stdin.isTTY) this.out.write(line + '\n');
|
|
70
|
+
return Promise.resolve(line);
|
|
71
|
+
}
|
|
72
|
+
if (this.closed) return Promise.resolve(null);
|
|
73
|
+
return new Promise((resolve) => {
|
|
74
|
+
this.waiter = (line) => {
|
|
75
|
+
if (line !== null && !process.stdin.isTTY) this.out.write(line + '\n');
|
|
76
|
+
resolve(line);
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
cancelRead() {
|
|
82
|
+
if (this.waiter) {
|
|
83
|
+
const w = this.waiter;
|
|
84
|
+
this.waiter = null;
|
|
85
|
+
w(null);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async prompt() {
|
|
90
|
+
let text = await this.readLine(this.c.accent('› '));
|
|
91
|
+
// A trailing backslash continues the message on the next line.
|
|
92
|
+
while (text !== null && text.endsWith('\\')) {
|
|
93
|
+
const next = await this.readLine(this.c.dim(' '));
|
|
94
|
+
if (next === null) break;
|
|
95
|
+
text = text.slice(0, -1) + '\n' + next;
|
|
96
|
+
}
|
|
97
|
+
return text;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
write(stream, s) {
|
|
101
|
+
if (!s) return;
|
|
102
|
+
stream.write(s);
|
|
103
|
+
this.atLineStart = s.endsWith('\n');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
newlineIfNeeded(stream = this.log) {
|
|
107
|
+
if (!this.atLineStart) this.write(stream, '\n');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
startResponse() {
|
|
111
|
+
this.mode = null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
thinking(s) {
|
|
115
|
+
if (this.mode !== 'thinking') {
|
|
116
|
+
this.newlineIfNeeded(this.log);
|
|
117
|
+
this.mode = 'thinking';
|
|
118
|
+
}
|
|
119
|
+
this.write(this.log, this.lc.dim(this.lc.italic(s)));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
text(s) {
|
|
123
|
+
if (this.mode === 'thinking') this.write(this.log, '\n\n');
|
|
124
|
+
else if (this.mode !== 'text') this.newlineIfNeeded(this.out);
|
|
125
|
+
this.mode = 'text';
|
|
126
|
+
this.write(this.out, s);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
endText() {
|
|
130
|
+
if (this.mode) this.newlineIfNeeded(this.mode === 'text' ? this.out : this.log);
|
|
131
|
+
this.mode = null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
toolStart(name, summary) {
|
|
135
|
+
this.endText();
|
|
136
|
+
const s = String(summary ?? '').split('\n')[0];
|
|
137
|
+
this.write(this.log, `${this.lc.accent('●')} ${this.lc.bold(name)}${s ? this.lc.dim(`(${s.length > 100 ? s.slice(0, 100) + '…' : s})`) : ''}\n`);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
toolResult(result, isError = false) {
|
|
141
|
+
const lines = String(result).split('\n');
|
|
142
|
+
const shown = lines.slice(0, 4).map((l) => (l.length > 160 ? l.slice(0, 160) + '…' : l));
|
|
143
|
+
let body = shown.map((l, i) => `${i === 0 ? ' ⎿ ' : ' '}${l}`).join('\n');
|
|
144
|
+
if (lines.length > 4) body += `\n … +${lines.length - 4} lines`;
|
|
145
|
+
this.write(this.log, (isError ? this.lc.red(body) : this.lc.dim(body)) + '\n');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async confirm(preview) {
|
|
149
|
+
const c = this.lc;
|
|
150
|
+
if (preview) {
|
|
151
|
+
this.write(this.log, ` ${c.yellow(preview.title)}\n`);
|
|
152
|
+
for (const { sign, text } of preview.lines ?? []) {
|
|
153
|
+
const line = ` ${sign} ${text}`;
|
|
154
|
+
this.write(this.log, (sign === '+' ? c.green(line) : sign === '-' ? c.red(line) : c.dim(line)) + '\n');
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const answer = await this.readLine(` ${c.bold('Allow?')} ${c.dim('[y]es [a]lways this session [n]o')} `);
|
|
158
|
+
const a = (answer ?? 'n').trim().toLowerCase();
|
|
159
|
+
if (a === 'a' || a === 'always') return 'always';
|
|
160
|
+
if (a === 'y' || a === 'yes' || a === '') return 'yes';
|
|
161
|
+
return 'no';
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
info(msg) {
|
|
165
|
+
this.endText();
|
|
166
|
+
this.write(this.log, this.lc.dim(msg) + '\n');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
error(msg) {
|
|
170
|
+
this.endText();
|
|
171
|
+
this.write(this.log, this.lc.red(`Error: ${msg}`) + '\n');
|
|
172
|
+
}
|
|
173
|
+
}
|
package/src/version.js
ADDED