@mumbrane/arrakis 0.4.0 → 0.6.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/input.js CHANGED
@@ -1,9 +1,79 @@
1
1
  // User input helpers: @file mentions and custom slash commands.
2
2
  import fs from 'node:fs';
3
+ import os from 'node:os';
3
4
  import path from 'node:path';
5
+ import { spawnSync } from 'node:child_process';
4
6
  import { HOME_DIR } from './config.js';
5
7
 
6
8
  const MAX_ATTACH = 100_000;
9
+ const MAX_IMAGE = 5 * 1024 * 1024;
10
+ const IMAGE_TYPES = { '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp' };
11
+
12
+ export const isImagePath = (p) => Boolean(IMAGE_TYPES[path.extname(p).toLowerCase()]);
13
+
14
+ function imagePart(file) {
15
+ const buf = fs.readFileSync(file);
16
+ if (buf.length > MAX_IMAGE) throw new Error(`${path.basename(file)} is larger than 5 MB`);
17
+ return { type: 'image_url', image_url: { url: `data:${IMAGE_TYPES[path.extname(file).toLowerCase()]};base64,${buf.toString('base64')}` } };
18
+ }
19
+
20
+ // Turn a typed message into what the model receives: @file contents inlined,
21
+ // images (by @path, or a path dragged into the terminal) attached as image parts.
22
+ // Returns { content: string | parts[], attached: [labels], error? }.
23
+ export function buildMessage(text, cwd, extraImages = []) {
24
+ const { text: withFiles, attached } = expandMentions(text, cwd);
25
+ const images = [];
26
+ const labels = [...attached];
27
+ const errors = [];
28
+ const refs = new Set();
29
+ // @image.png mentions
30
+ for (const m of text.matchAll(/(^|\s)@((?:[^\s"'`]|\\ )+)/g)) refs.add(m[2].replace(/\\ /g, ' ').replace(/[,;:!?)]+$/, ''));
31
+ // Dragged-in paths: '/path/with spaces.png', "/x.png" or /path/with\ spaces.png
32
+ for (const m of text.matchAll(/'([^']+)'|"([^"]+)"|((?:\/|~\/)(?:[^\s\\]|\\ )+)/g)) refs.add((m[1] ?? m[2] ?? m[3]).replace(/\\ /g, ' '));
33
+ for (const ref of refs) {
34
+ if (!isImagePath(ref)) continue;
35
+ const full = path.resolve(cwd, ref.startsWith('~/') ? path.join(os.homedir(), ref.slice(2)) : ref);
36
+ if (!fs.existsSync(full)) continue;
37
+ try {
38
+ images.push(imagePart(full));
39
+ labels.push(`🖼 ${path.basename(full)}`);
40
+ } catch (err) {
41
+ errors.push(err.message);
42
+ }
43
+ }
44
+ for (const file of extraImages) {
45
+ try {
46
+ images.push(imagePart(file));
47
+ labels.push('🖼 pasted image');
48
+ } catch (err) {
49
+ errors.push(err.message);
50
+ }
51
+ }
52
+ const content = images.length ? [{ type: 'text', text: withFiles }, ...images] : withFiles;
53
+ return { content, attached: labels, errors };
54
+ }
55
+
56
+ // Save the image on the clipboard (macOS) to a file. Returns the path or null.
57
+ export function pasteClipboardImage() {
58
+ if (process.platform !== 'darwin') return { error: 'Pasting images works on macOS. Elsewhere, attach a file with @path/to/image.png.' };
59
+ const dir = path.join(HOME_DIR, 'tmp');
60
+ fs.mkdirSync(dir, { recursive: true });
61
+ const file = path.join(dir, `paste-${Date.now()}.png`);
62
+ const script = `set f to open for access POSIX file "${file}" with write permission
63
+ try
64
+ write (the clipboard as «class PNGf») to f
65
+ close access f
66
+ on error
67
+ close access f
68
+ error "no image"
69
+ end try`;
70
+ const r = spawnSync('osascript', ['-e', script], { encoding: 'utf8' });
71
+ if (r.status !== 0 || !fs.existsSync(file) || fs.statSync(file).size === 0) {
72
+ fs.rmSync(file, { force: true });
73
+ return { error: 'There is no image on the clipboard. Copy one first (Cmd+Ctrl+Shift+4 copies a screenshot).' };
74
+ }
75
+ return { file };
76
+ }
7
77
 
8
78
  // "@src/app.js" in a message attaches that file's contents (or a directory listing).
9
79
  export function expandMentions(text, cwd) {
@@ -12,7 +82,7 @@ export function expandMentions(text, cwd) {
12
82
  const seen = new Set();
13
83
  for (const m of text.matchAll(/(^|\s)@((?:[^\s"'`]|\\ )+)/g)) {
14
84
  const ref = m[2].replace(/\\ /g, ' ').replace(/[.,;:!?)]+$/, '');
15
- if (seen.has(ref)) continue;
85
+ if (seen.has(ref) || isImagePath(ref)) continue;
16
86
  seen.add(ref);
17
87
  const full = path.resolve(cwd, ref.startsWith('~/') ? path.join(process.env.HOME || '', ref.slice(2)) : ref);
18
88
  let stat;
package/src/jobs.js ADDED
@@ -0,0 +1,97 @@
1
+ // Background jobs: `arrakis bg "task"` runs a task in a detached process that
2
+ // keeps going after the terminal closes. Each job has a JSON record and a log
3
+ // in ~/.arrakis/jobs/, and its own session, so `arrakis -r <session>` can pick
4
+ // up the conversation afterwards.
5
+ import fs from 'node:fs';
6
+ import path from 'node:path';
7
+ import crypto from 'node:crypto';
8
+ import { spawn } from 'node:child_process';
9
+ import { fileURLToPath } from 'node:url';
10
+ import { HOME_DIR } from './config.js';
11
+
12
+ export const JOBS_DIR = path.join(HOME_DIR, 'jobs');
13
+ const BIN = fileURLToPath(new URL('../bin/arrakis.js', import.meta.url));
14
+
15
+ const jobFile = (id) => path.join(JOBS_DIR, `${id}.json`);
16
+ export const jobLog = (id) => path.join(JOBS_DIR, `${id}.log`);
17
+
18
+ export function startJob({ prompt, cwd, flags = [] }) {
19
+ fs.mkdirSync(JOBS_DIR, { recursive: true });
20
+ const id = `job-${crypto.randomBytes(3).toString('hex')}`;
21
+ const out = fs.openSync(jobLog(id), 'a');
22
+ const child = spawn(process.execPath, [BIN, '-p', ...flags, prompt], {
23
+ cwd,
24
+ detached: true,
25
+ stdio: ['ignore', out, out],
26
+ env: { ...process.env, ARRAKIS_JOB_ID: id, NO_COLOR: '1' },
27
+ });
28
+ child.unref();
29
+ const job = { id, pid: child.pid, prompt, cwd, flags, started: Date.now(), status: 'running' };
30
+ fs.writeFileSync(jobFile(id), JSON.stringify(job, null, 2));
31
+ return job;
32
+ }
33
+
34
+ // Called inside the job process to record its session and final status.
35
+ export function updateJob(id, patch) {
36
+ try {
37
+ const job = JSON.parse(fs.readFileSync(jobFile(id), 'utf8'));
38
+ fs.writeFileSync(jobFile(id), JSON.stringify({ ...job, ...patch }, null, 2));
39
+ } catch {}
40
+ }
41
+
42
+ const alive = (pid) => {
43
+ try {
44
+ process.kill(pid, 0);
45
+ return true;
46
+ } catch {
47
+ return false;
48
+ }
49
+ };
50
+
51
+ export function listJobs() {
52
+ if (!fs.existsSync(JOBS_DIR)) return [];
53
+ return fs
54
+ .readdirSync(JOBS_DIR)
55
+ .filter((f) => f.endsWith('.json'))
56
+ .map((f) => {
57
+ try {
58
+ const job = JSON.parse(fs.readFileSync(path.join(JOBS_DIR, f), 'utf8'));
59
+ if (job.status === 'running' && !alive(job.pid)) job.status = 'stopped';
60
+ return job;
61
+ } catch {
62
+ return null;
63
+ }
64
+ })
65
+ .filter(Boolean)
66
+ .sort((a, b) => b.started - a.started);
67
+ }
68
+
69
+ export function findJob(idOrPrefix) {
70
+ const jobs = listJobs();
71
+ const job = jobs.find((j) => j.id === idOrPrefix) ?? jobs.filter((j) => j.id.startsWith(idOrPrefix) || j.id.slice(4).startsWith(idOrPrefix))[0];
72
+ if (!job) throw new Error(`No job "${idOrPrefix}". List jobs with: arrakis jobs`);
73
+ return job;
74
+ }
75
+
76
+ export function stopJob(idOrPrefix) {
77
+ const job = findJob(idOrPrefix);
78
+ if (job.status === 'running') {
79
+ try {
80
+ process.kill(-job.pid, 'SIGTERM');
81
+ } catch {
82
+ try {
83
+ process.kill(job.pid, 'SIGTERM');
84
+ } catch {}
85
+ }
86
+ updateJob(job.id, { status: 'stopped', finished: Date.now() });
87
+ }
88
+ return job;
89
+ }
90
+
91
+ export function readLog(id) {
92
+ try {
93
+ return fs.readFileSync(jobLog(id), 'utf8');
94
+ } catch {
95
+ return '';
96
+ }
97
+ }
package/src/mcp.js ADDED
@@ -0,0 +1,343 @@
1
+ // Model Context Protocol client. Connects to MCP servers (local processes over
2
+ // stdio, or remote servers over Streamable HTTP) and exposes their tools to the
3
+ // agent as mcp__<server>__<tool>.
4
+ //
5
+ // Config uses the common "mcpServers" shape, read from (later wins):
6
+ // ~/.arrakis/mcp.json your servers, everywhere
7
+ // <project>/.mcp.json shared project servers (asks before first use)
8
+ // <project>/.arrakis/mcp.json project servers for arrakis (asks before first use)
9
+ // {
10
+ // "mcpServers": {
11
+ // "github": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-github"], "env": { "GITHUB_TOKEN": "..." } },
12
+ // "docs": { "url": "https://example.com/mcp", "headers": { "Authorization": "Bearer ..." } }
13
+ // }
14
+ // }
15
+ import fs from 'node:fs';
16
+ import path from 'node:path';
17
+ import { spawn } from 'node:child_process';
18
+ import readline from 'node:readline';
19
+ import { HOME_DIR } from './config.js';
20
+ import { VERSION } from './version.js';
21
+
22
+ const PROTOCOL = '2025-06-18';
23
+ const CONNECT_TIMEOUT = 20000;
24
+ const CALL_TIMEOUT = 300000;
25
+
26
+ export function mcpConfigFiles(cwd) {
27
+ return {
28
+ user: [path.join(HOME_DIR, 'mcp.json')],
29
+ project: [path.join(cwd, '.mcp.json'), path.join(cwd, '.arrakis', 'mcp.json')],
30
+ };
31
+ }
32
+
33
+ function readServers(file) {
34
+ try {
35
+ const json = JSON.parse(fs.readFileSync(file, 'utf8'));
36
+ return json.mcpServers ?? json.servers ?? {};
37
+ } catch {
38
+ return null;
39
+ }
40
+ }
41
+
42
+ // Returns { servers: {name: {...cfg, source}}, projectFiles: [files that exist] }
43
+ export function loadMcpConfig(cwd, { includeProject = true } = {}) {
44
+ const { user, project } = mcpConfigFiles(cwd);
45
+ const servers = {};
46
+ const projectFiles = [];
47
+ for (const file of user) for (const [name, cfg] of Object.entries(readServers(file) ?? {})) servers[name] = { ...cfg, source: file };
48
+ for (const file of project) {
49
+ const found = readServers(file);
50
+ if (!found) continue;
51
+ projectFiles.push(file);
52
+ if (includeProject) for (const [name, cfg] of Object.entries(found)) servers[name] = { ...cfg, source: file };
53
+ }
54
+ return { servers, projectFiles };
55
+ }
56
+
57
+ // Expand ${VAR} references from the environment in strings.
58
+ const expand = (v) => (typeof v === 'string' ? v.replace(/\$\{(\w+)\}/g, (_, k) => process.env[k] ?? '') : v);
59
+ const expandAll = (obj) => Object.fromEntries(Object.entries(obj ?? {}).map(([k, v]) => [k, expand(v)]));
60
+
61
+ class StdioTransport {
62
+ constructor(cfg, cwd) {
63
+ this.cfg = cfg;
64
+ this.cwd = cwd;
65
+ this.pending = new Map();
66
+ this.nextId = 1;
67
+ this.stderr = '';
68
+ }
69
+
70
+ async start() {
71
+ const { command, args = [], env = {} } = this.cfg;
72
+ this.proc = spawn(expand(command), args.map(expand), {
73
+ cwd: this.cfg.cwd ? path.resolve(this.cwd, this.cfg.cwd) : this.cwd,
74
+ env: { ...process.env, ...expandAll(env) },
75
+ stdio: ['pipe', 'pipe', 'pipe'],
76
+ });
77
+ this.proc.stderr.on('data', (d) => {
78
+ this.stderr = (this.stderr + d).slice(-4000);
79
+ });
80
+ const exited = new Promise((_, reject) => {
81
+ this.proc.on('error', (err) => reject(new Error(`could not start "${command}": ${err.message}`)));
82
+ this.proc.on('exit', (code) => {
83
+ const err = new Error(`server exited (code ${code})${this.stderr ? `: ${this.stderr.trim().split('\n').slice(-3).join(' ')}` : ''}`);
84
+ for (const { reject: r } of this.pending.values()) r(err);
85
+ this.pending.clear();
86
+ this.closed = err;
87
+ reject(err);
88
+ });
89
+ });
90
+ exited.catch(() => {});
91
+ this.exited = exited;
92
+ readline.createInterface({ input: this.proc.stdout }).on('line', (line) => this.onLine(line));
93
+ }
94
+
95
+ onLine(line) {
96
+ let msg;
97
+ try {
98
+ msg = JSON.parse(line);
99
+ } catch {
100
+ return;
101
+ }
102
+ if (msg.id !== undefined && (msg.result !== undefined || msg.error !== undefined)) {
103
+ const p = this.pending.get(msg.id);
104
+ if (!p) return;
105
+ this.pending.delete(msg.id);
106
+ if (msg.error) p.reject(new Error(msg.error.message ?? JSON.stringify(msg.error)));
107
+ else p.resolve(msg.result);
108
+ } else if (msg.id !== undefined && msg.method) {
109
+ // Server-to-client request: answer ping, refuse the rest politely.
110
+ const reply = msg.method === 'ping' ? { jsonrpc: '2.0', id: msg.id, result: {} } : { jsonrpc: '2.0', id: msg.id, error: { code: -32601, message: 'Not supported by arrakis' } };
111
+ this.send(reply);
112
+ }
113
+ }
114
+
115
+ send(obj) {
116
+ if (this.closed) throw this.closed;
117
+ this.proc.stdin.write(JSON.stringify(obj) + '\n');
118
+ }
119
+
120
+ request(method, params, { timeout = CALL_TIMEOUT, signal } = {}) {
121
+ const id = this.nextId++;
122
+ return new Promise((resolve, reject) => {
123
+ const timer = setTimeout(() => {
124
+ this.pending.delete(id);
125
+ reject(new Error(`${method} timed out`));
126
+ }, timeout);
127
+ const done = (fn) => (v) => {
128
+ clearTimeout(timer);
129
+ fn(v);
130
+ };
131
+ this.pending.set(id, { resolve: done(resolve), reject: done(reject) });
132
+ signal?.addEventListener('abort', () => {
133
+ this.pending.delete(id);
134
+ clearTimeout(timer);
135
+ try {
136
+ this.send({ jsonrpc: '2.0', method: 'notifications/cancelled', params: { requestId: id } });
137
+ } catch {}
138
+ reject(new DOMException('Aborted', 'AbortError'));
139
+ }, { once: true });
140
+ try {
141
+ this.send({ jsonrpc: '2.0', id, method, params });
142
+ } catch (err) {
143
+ this.pending.delete(id);
144
+ clearTimeout(timer);
145
+ reject(err);
146
+ }
147
+ });
148
+ }
149
+
150
+ notify(method, params) {
151
+ this.send({ jsonrpc: '2.0', method, params });
152
+ }
153
+
154
+ close() {
155
+ try {
156
+ this.proc?.stdin.end();
157
+ this.proc?.kill();
158
+ } catch {}
159
+ }
160
+ }
161
+
162
+ class HttpTransport {
163
+ constructor(cfg) {
164
+ this.url = expand(cfg.url);
165
+ this.headers = expandAll(cfg.headers);
166
+ this.nextId = 1;
167
+ this.sessionId = null;
168
+ }
169
+
170
+ async start() {}
171
+
172
+ async post(body, signal) {
173
+ const headers = { 'content-type': 'application/json', accept: 'application/json, text/event-stream', ...this.headers };
174
+ if (this.sessionId) headers['mcp-session-id'] = this.sessionId;
175
+ if (this.protocol) headers['mcp-protocol-version'] = this.protocol;
176
+ const res = await fetch(this.url, { method: 'POST', headers, body: JSON.stringify(body), signal });
177
+ const sid = res.headers.get('mcp-session-id');
178
+ if (sid) this.sessionId = sid;
179
+ if (!res.ok && res.status !== 202) {
180
+ const text = await res.text().catch(() => '');
181
+ throw new Error(`HTTP ${res.status}${res.status === 401 ? ' (unauthorized: check the headers in your MCP config)' : ''}: ${text.slice(0, 200)}`);
182
+ }
183
+ return res;
184
+ }
185
+
186
+ async request(method, params, { timeout = CALL_TIMEOUT, signal } = {}) {
187
+ const id = this.nextId++;
188
+ const ac = new AbortController();
189
+ const timer = setTimeout(() => ac.abort(), timeout);
190
+ signal?.addEventListener('abort', () => ac.abort(), { once: true });
191
+ try {
192
+ const res = await this.post({ jsonrpc: '2.0', id, method, params }, ac.signal);
193
+ const type = res.headers.get('content-type') ?? '';
194
+ const messages = [];
195
+ if (type.includes('text/event-stream')) {
196
+ const decoder = new TextDecoder();
197
+ let buf = '';
198
+ outer: for await (const chunk of res.body) {
199
+ buf += decoder.decode(chunk, { stream: true });
200
+ let i;
201
+ while ((i = buf.indexOf('\n\n')) >= 0) {
202
+ const event = buf.slice(0, i);
203
+ buf = buf.slice(i + 2);
204
+ const data = event.split('\n').filter((l) => l.startsWith('data:')).map((l) => l.slice(5).trim()).join('\n');
205
+ if (!data) continue;
206
+ try {
207
+ const msg = JSON.parse(data);
208
+ messages.push(msg);
209
+ if (msg.id === id) break outer;
210
+ } catch {}
211
+ }
212
+ }
213
+ } else {
214
+ const json = await res.json();
215
+ messages.push(...(Array.isArray(json) ? json : [json]));
216
+ }
217
+ const reply = messages.find((m) => m.id === id);
218
+ if (!reply) throw new Error(`no response to ${method}`);
219
+ if (reply.error) throw new Error(reply.error.message ?? JSON.stringify(reply.error));
220
+ return reply.result;
221
+ } catch (err) {
222
+ if (err.name === 'AbortError' && !signal?.aborted) throw new Error(`${method} timed out`);
223
+ throw err;
224
+ } finally {
225
+ clearTimeout(timer);
226
+ }
227
+ }
228
+
229
+ async notify(method, params) {
230
+ await this.post({ jsonrpc: '2.0', method, params }).catch(() => {});
231
+ }
232
+
233
+ close() {
234
+ if (this.sessionId) fetch(this.url, { method: 'DELETE', headers: { 'mcp-session-id': this.sessionId, ...this.headers } }).catch(() => {});
235
+ }
236
+ }
237
+
238
+ export class McpServer {
239
+ constructor(name, cfg, cwd) {
240
+ this.name = name;
241
+ this.cfg = cfg;
242
+ this.cwd = cwd;
243
+ this.status = 'pending';
244
+ this.error = null;
245
+ this.tools = [];
246
+ }
247
+
248
+ async connect() {
249
+ this.transport = this.cfg.url ? new HttpTransport(this.cfg) : new StdioTransport(this.cfg, this.cwd);
250
+ try {
251
+ if (!this.cfg.url && !this.cfg.command) throw new Error('needs "command" (local server) or "url" (remote server)');
252
+ await this.transport.start();
253
+ const init = this.transport.request('initialize', {
254
+ protocolVersion: PROTOCOL,
255
+ capabilities: {},
256
+ clientInfo: { name: 'arrakis', version: VERSION },
257
+ }, { timeout: CONNECT_TIMEOUT });
258
+ const info = await (this.transport.exited ? Promise.race([init, this.transport.exited]) : init);
259
+ this.transport.protocol = info?.protocolVersion ?? PROTOCOL;
260
+ this.serverInfo = info?.serverInfo;
261
+ this.instructions = info?.instructions;
262
+ await this.transport.notify('notifications/initialized', {});
263
+ const tools = [];
264
+ let cursor;
265
+ do {
266
+ const page = await this.transport.request('tools/list', cursor ? { cursor } : {}, { timeout: CONNECT_TIMEOUT });
267
+ tools.push(...(page?.tools ?? []));
268
+ cursor = page?.nextCursor;
269
+ } while (cursor);
270
+ this.tools = tools;
271
+ this.status = 'connected';
272
+ } catch (err) {
273
+ this.status = 'failed';
274
+ this.error = err.message;
275
+ this.transport.close();
276
+ }
277
+ return this;
278
+ }
279
+
280
+ async callTool(name, args, signal) {
281
+ const result = await this.transport.request('tools/call', { name, arguments: args ?? {} }, { signal });
282
+ const parts = [];
283
+ for (const c of result?.content ?? []) {
284
+ if (c.type === 'text') parts.push(c.text);
285
+ else if (c.type === 'image') parts.push(`[image ${c.mimeType ?? ''} omitted]`);
286
+ else if (c.type === 'resource') parts.push(c.resource?.text ?? `[resource ${c.resource?.uri ?? ''}]`);
287
+ else if (c.type === 'resource_link') parts.push(`[resource ${c.uri}]`);
288
+ else parts.push(JSON.stringify(c));
289
+ }
290
+ if (!parts.length && result?.structuredContent) parts.push(JSON.stringify(result.structuredContent, null, 2));
291
+ return { text: parts.join('\n') || '(no output)', isError: Boolean(result?.isError) };
292
+ }
293
+
294
+ close() {
295
+ this.transport?.close();
296
+ }
297
+ }
298
+
299
+ const toolId = (server, tool) => `mcp__${server}__${tool}`.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 64);
300
+
301
+ // Turn connected servers' tools into arrakis tool definitions.
302
+ export function mcpTools(servers) {
303
+ const out = [];
304
+ for (const server of servers) {
305
+ if (server.status !== 'connected') continue;
306
+ for (const t of server.tools) {
307
+ const readOnly = t.annotations?.readOnlyHint === true;
308
+ out.push({
309
+ name: toolId(server.name, t.name),
310
+ label: `${server.name}:${t.name}`,
311
+ description: `[MCP server "${server.name}"] ${t.description ?? t.title ?? t.name}`.slice(0, 1024),
312
+ parameters: t.inputSchema && typeof t.inputSchema === 'object' ? { type: 'object', properties: {}, ...t.inputSchema } : { type: 'object', properties: {} },
313
+ needsPermission: !readOnly,
314
+ writes: !readOnly,
315
+ mcp: true,
316
+ summary: (a) => {
317
+ const s = JSON.stringify(a ?? {});
318
+ return s === '{}' ? '' : s.length > 80 ? s.slice(0, 80) + '…' : s;
319
+ },
320
+ preview: (a) => ({
321
+ title: `${server.name} → ${t.name}`,
322
+ lines: JSON.stringify(a ?? {}, null, 2).split('\n').slice(0, 12).map((text) => ({ sign: ' ', text })),
323
+ }),
324
+ async run(a, ctx) {
325
+ const { text, isError } = await server.callTool(t.name, a, ctx.signal);
326
+ if (isError) throw new Error(text);
327
+ return text;
328
+ },
329
+ });
330
+ }
331
+ }
332
+ return out;
333
+ }
334
+
335
+ export async function connectAll(cwd, { includeProject }) {
336
+ const { servers } = loadMcpConfig(cwd, { includeProject });
337
+ const list = Object.entries(servers)
338
+ .filter(([, cfg]) => cfg.disabled !== true)
339
+ .map(([name, cfg]) => new McpServer(name, cfg, cwd));
340
+ await Promise.all(list.map((s) => s.connect()));
341
+ process.once('exit', () => list.forEach((s) => s.close()));
342
+ return list;
343
+ }
@@ -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/session.js CHANGED
@@ -145,7 +145,7 @@ export function listSessions() {
145
145
  model: meta.model,
146
146
  updated: fs.statSync(file).mtimeMs,
147
147
  messages: count,
148
- title: typeof firstUser?.message.content === 'string' ? firstUser.message.content.replace(/\s+/g, ' ').slice(0, 70) : '',
148
+ title: (typeof firstUser?.message.content === 'string' ? firstUser.message.content : Array.isArray(firstUser?.message.content) ? firstUser.message.content.find((p) => p.type === 'text')?.text ?? '' : '').replace(/\s+/g, ' ').slice(0, 70),
149
149
  });
150
150
  }
151
151
  return out.sort((a, b) => b.updated - a.updated);
package/src/tools.js CHANGED
@@ -78,16 +78,22 @@ function hasRipgrep() {
78
78
  return rgAvailable;
79
79
  }
80
80
 
81
- function runProcess(cmd, args, { cwd, signal, timeoutMs, shell = false }) {
81
+ function runProcess(cmd, args, { cwd, signal, timeoutMs, shell = false, onOutput }) {
82
82
  return new Promise((resolve) => {
83
83
  const child = spawn(cmd, args, { cwd, shell, detached: true, stdio: ['ignore', 'pipe', 'pipe'], env: { ...process.env, ARRAKIS: '1' } });
84
84
  let out = '';
85
85
  let size = 0;
86
86
  let timedOut = false;
87
87
  let aborted = false;
88
+ let tail = '';
88
89
  const add = (d) => {
89
90
  size += d.length;
90
91
  if (size < 2_000_000) out += d.toString();
92
+ if (onOutput) {
93
+ tail = (tail + d.toString()).slice(-2000);
94
+ const last = tail.split('\n').map((l) => l.trim()).filter(Boolean).pop();
95
+ if (last) onOutput(last);
96
+ }
91
97
  };
92
98
  child.stdout.on('data', add);
93
99
  child.stderr.on('data', add);
@@ -182,6 +188,7 @@ export const TOOLS = [
182
188
  const p = resolvePath(ctx, a.path);
183
189
  fs.mkdirSync(path.dirname(p), { recursive: true });
184
190
  const content = String(a.content ?? '');
191
+ ctx.checkpoints?.record(p);
185
192
  fs.writeFileSync(p, content);
186
193
  const all = content.split('\n');
187
194
  const msg = `Wrote ${all.length} lines to ${rel(ctx, p)}`;
@@ -223,6 +230,7 @@ export const TOOLS = [
223
230
  run(a, ctx) {
224
231
  const { p, text, count } = this.validate(a, ctx);
225
232
  const updated = a.replace_all ? text.split(a.old_string).join(a.new_string) : text.replace(a.old_string, () => a.new_string);
233
+ ctx.checkpoints?.record(p);
226
234
  fs.writeFileSync(p, updated);
227
235
  const startLine = text.slice(0, text.indexOf(a.old_string)).split('\n').length;
228
236
  const oldLines = a.old_string.split('\n');
@@ -256,7 +264,8 @@ export const TOOLS = [
256
264
  async run(a, ctx) {
257
265
  const timeoutMs = Math.min(Math.max(a.timeout_ms || 120000, 1000), 600000);
258
266
  const shell = fs.existsSync('/bin/bash') ? '/bin/bash' : true;
259
- const { out, code, timedOut, aborted, error } = await runProcess(String(a.command), [], { cwd: ctx.cwd, signal: ctx.signal, timeoutMs, shell });
267
+ const onOutput = (ctx.depth ?? 0) === 0 ? (line) => ctx.ui.spinnerDetail?.(line) : undefined;
268
+ const { out, code, timedOut, aborted, error } = await runProcess(String(a.command), [], { cwd: ctx.cwd, signal: ctx.signal, timeoutMs, shell, onOutput });
260
269
  if (error) throw error;
261
270
  let result = truncate(out.trimEnd()) || '(no output)';
262
271
  if (timedOut) result += `\n[timed out after ${timeoutMs / 1000}s]`;
@@ -480,10 +489,17 @@ export function htmlToText(html) {
480
489
  return title ? `# ${title}\n\n${t}` : t;
481
490
  }
482
491
 
492
+ // Tools added at runtime (MCP servers).
493
+ let extraTools = [];
494
+ export const setExtraTools = (list) => {
495
+ extraTools = list;
496
+ };
497
+ export const allTools = () => (extraTools.length ? [...TOOLS, ...extraTools] : TOOLS);
498
+
483
499
  export const toolSchemas = (filter = () => true) =>
484
- TOOLS.filter((t) => filter(t.name)).map((t) => ({ type: 'function', function: { name: t.name, description: t.description, parameters: t.parameters } }));
500
+ allTools().filter((t) => filter(t.name)).map((t) => ({ type: 'function', function: { name: t.name, description: t.description, parameters: t.parameters } }));
485
501
 
486
- export const findTool = (name) => TOOLS.find((t) => t.name === name);
502
+ export const findTool = (name) => allTools().find((t) => t.name === name);
487
503
 
488
504
  // Run one tool call from the model. Always returns a string for the tool message.
489
505
  // ctx: { cwd, signal, ui, permissions, agent, depth, label, allowed }
@@ -499,13 +515,20 @@ export async function executeToolCall(call, ctx) {
499
515
  return `Error: tool arguments were not valid JSON: ${String(call.function.arguments).slice(0, 200)}`;
500
516
  }
501
517
  const sub = (ctx.depth ?? 0) > 0;
502
- ctx.ui.toolStart(tool.name, safe(() => tool.summary?.(args)) ?? '', { prefix: sub ? `${ctx.label}` : '' });
518
+ ctx.ui.toolStart(tool.label ?? tool.name, safe(() => tool.summary?.(args)) ?? '', { prefix: sub ? `${ctx.label}` : '' });
503
519
  try {
504
520
  if (tool.validate) tool.validate(args, ctx);
505
521
  if (tool.writes && ctx.permissions.planMode) {
506
522
  if (!sub) ctx.ui.toolResult('Blocked: plan mode is on', true);
507
523
  return 'Blocked: plan mode is on, so files cannot be changed and commands cannot run. Finish investigating with the read-only tools, then present your plan. The user turns plan mode off with /plan when they approve.';
508
524
  }
525
+ if (ctx.hooks) {
526
+ const pre = await ctx.hooks.run('PreToolUse', { tool: tool.name, input: args, sessionId: ctx.sessionId });
527
+ if (pre.blocked) {
528
+ if (!sub) ctx.ui.toolResult(`Blocked by hook: ${pre.reason.split('\n')[0]}`, true);
529
+ return `Blocked by a PreToolUse hook: ${pre.reason}`;
530
+ }
531
+ }
509
532
  let preview = null;
510
533
  if (tool.needsPermission) {
511
534
  preview = safe(() => tool.preview?.(args, ctx));
@@ -519,6 +542,7 @@ export async function executeToolCall(call, ctx) {
519
542
  if (!sub) ctx.ui.startSpinner(tool.name === 'task' ? 'Agents working' : 'Running');
520
543
  const raw = await tool.run(args, ctx);
521
544
  const { content, display, lines } = raw && typeof raw === 'object' ? raw : { content: String(raw) };
545
+ if (ctx.hooks) await ctx.hooks.run('PostToolUse', { tool: tool.name, input: args, result: truncate(String(content), 10000), sessionId: ctx.sessionId });
522
546
  // Skip the diff when the user just saw it in the approval prompt.
523
547
  if (!sub && display !== null) ctx.ui.toolResult(display ?? content, false, { lines: preview?.shown ? null : lines });
524
548
  return truncate(String(content));