@mumbrane/arrakis 0.1.0 → 0.3.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/ui.js ADDED
@@ -0,0 +1,192 @@
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
+ if (this.rl || this.closed) return;
35
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: !!process.stdin.isTTY, historySize: 200 });
36
+ this.rl = rl;
37
+ if (this.history) rl.history = this.history;
38
+ rl.on('line', (line) => {
39
+ if (this.waiter) {
40
+ const w = this.waiter;
41
+ this.waiter = null;
42
+ w(line);
43
+ } else this.queue.push(line);
44
+ });
45
+ rl.on('close', () => {
46
+ if (rl.suspended) return;
47
+ this.closed = true;
48
+ this.cancelRead();
49
+ });
50
+ if (this.interruptHandler) rl.on('SIGINT', this.interruptHandler);
51
+ }
52
+
53
+ // Hand the terminal to a raw-mode prompt (picker, secret input) and back.
54
+ suspend() {
55
+ if (!this.rl) return;
56
+ this.history = this.rl.history;
57
+ this.rl.suspended = true;
58
+ this.rl.close();
59
+ this.rl = null;
60
+ }
61
+
62
+ resume() {
63
+ this.start();
64
+ }
65
+
66
+ onInterrupt(handler) {
67
+ this.interruptHandler = handler;
68
+ if (this.rl) this.rl.on('SIGINT', handler);
69
+ process.on('SIGINT', handler);
70
+ }
71
+
72
+ close() {
73
+ this.rl?.close();
74
+ }
75
+
76
+ readLine(promptText) {
77
+ if (!this.rl) return Promise.resolve(null);
78
+ // Piped input can close readline while queued lines remain.
79
+ if (this.closed) {
80
+ if (!this.queue.length) return Promise.resolve(null);
81
+ this.out.write(promptText);
82
+ } else {
83
+ this.rl.setPrompt(promptText);
84
+ this.rl.prompt();
85
+ }
86
+ if (this.queue.length) {
87
+ const line = this.queue.shift();
88
+ if (!process.stdin.isTTY) this.out.write(line + '\n');
89
+ return Promise.resolve(line);
90
+ }
91
+ if (this.closed) return Promise.resolve(null);
92
+ return new Promise((resolve) => {
93
+ this.waiter = (line) => {
94
+ if (line !== null && !process.stdin.isTTY) this.out.write(line + '\n');
95
+ resolve(line);
96
+ };
97
+ });
98
+ }
99
+
100
+ cancelRead() {
101
+ if (this.waiter) {
102
+ const w = this.waiter;
103
+ this.waiter = null;
104
+ w(null);
105
+ }
106
+ }
107
+
108
+ async prompt() {
109
+ let text = await this.readLine(this.c.accent('› '));
110
+ // A trailing backslash continues the message on the next line.
111
+ while (text !== null && text.endsWith('\\')) {
112
+ const next = await this.readLine(this.c.dim(' '));
113
+ if (next === null) break;
114
+ text = text.slice(0, -1) + '\n' + next;
115
+ }
116
+ return text;
117
+ }
118
+
119
+ write(stream, s) {
120
+ if (!s) return;
121
+ stream.write(s);
122
+ this.atLineStart = s.endsWith('\n');
123
+ }
124
+
125
+ newlineIfNeeded(stream = this.log) {
126
+ if (!this.atLineStart) this.write(stream, '\n');
127
+ }
128
+
129
+ startResponse() {
130
+ this.mode = null;
131
+ }
132
+
133
+ thinking(s) {
134
+ if (this.mode !== 'thinking') {
135
+ this.newlineIfNeeded(this.log);
136
+ this.mode = 'thinking';
137
+ }
138
+ this.write(this.log, this.lc.dim(this.lc.italic(s)));
139
+ }
140
+
141
+ text(s) {
142
+ if (this.mode === 'thinking') this.write(this.log, '\n\n');
143
+ else if (this.mode !== 'text') this.newlineIfNeeded(this.out);
144
+ this.mode = 'text';
145
+ this.write(this.out, s);
146
+ }
147
+
148
+ endText() {
149
+ if (this.mode) this.newlineIfNeeded(this.mode === 'text' ? this.out : this.log);
150
+ this.mode = null;
151
+ }
152
+
153
+ toolStart(name, summary) {
154
+ this.endText();
155
+ const s = String(summary ?? '').split('\n')[0];
156
+ this.write(this.log, `${this.lc.accent('●')} ${this.lc.bold(name)}${s ? this.lc.dim(`(${s.length > 100 ? s.slice(0, 100) + '…' : s})`) : ''}\n`);
157
+ }
158
+
159
+ toolResult(result, isError = false) {
160
+ const lines = String(result).split('\n');
161
+ const shown = lines.slice(0, 4).map((l) => (l.length > 160 ? l.slice(0, 160) + '…' : l));
162
+ let body = shown.map((l, i) => `${i === 0 ? ' ⎿ ' : ' '}${l}`).join('\n');
163
+ if (lines.length > 4) body += `\n … +${lines.length - 4} lines`;
164
+ this.write(this.log, (isError ? this.lc.red(body) : this.lc.dim(body)) + '\n');
165
+ }
166
+
167
+ async confirm(preview) {
168
+ const c = this.lc;
169
+ if (preview) {
170
+ this.write(this.log, ` ${c.yellow(preview.title)}\n`);
171
+ for (const { sign, text } of preview.lines ?? []) {
172
+ const line = ` ${sign} ${text}`;
173
+ this.write(this.log, (sign === '+' ? c.green(line) : sign === '-' ? c.red(line) : c.dim(line)) + '\n');
174
+ }
175
+ }
176
+ const answer = await this.readLine(` ${c.bold('Allow?')} ${c.dim('[y]es [a]lways this session [n]o')} `);
177
+ const a = (answer ?? 'n').trim().toLowerCase();
178
+ if (a === 'a' || a === 'always') return 'always';
179
+ if (a === 'y' || a === 'yes' || a === '') return 'yes';
180
+ return 'no';
181
+ }
182
+
183
+ info(msg) {
184
+ this.endText();
185
+ this.write(this.log, this.lc.dim(msg) + '\n');
186
+ }
187
+
188
+ error(msg) {
189
+ this.endText();
190
+ this.write(this.log, this.lc.red(`Error: ${msg}`) + '\n');
191
+ }
192
+ }
package/src/version.js ADDED
@@ -0,0 +1,3 @@
1
+ import fs from 'node:fs';
2
+
3
+ export const VERSION = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version;