@mumbrane/arrakis 0.3.0 → 0.5.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 +72 -4
- package/package.json +1 -1
- package/src/agent.js +171 -28
- package/src/checkpoints.js +83 -0
- package/src/cli.js +383 -21
- package/src/config.js +11 -0
- package/src/hooks.js +114 -0
- package/src/input.js +145 -0
- package/src/jobs.js +97 -0
- package/src/markdown.js +134 -0
- package/src/mcp.js +343 -0
- package/src/permissions.js +16 -6
- package/src/prompt.js +17 -1
- package/src/session.js +12 -1
- package/src/tools.js +200 -15
- package/src/ui.js +267 -30
- package/src/update.js +33 -0
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);
|
|
@@ -157,6 +163,7 @@ export const TOOLS = [
|
|
|
157
163
|
},
|
|
158
164
|
{
|
|
159
165
|
name: 'write_file',
|
|
166
|
+
writes: true,
|
|
160
167
|
description: 'Create a new file or completely overwrite an existing one. Prefer edit_file for changes to existing files.',
|
|
161
168
|
parameters: {
|
|
162
169
|
type: 'object',
|
|
@@ -181,12 +188,18 @@ export const TOOLS = [
|
|
|
181
188
|
const p = resolvePath(ctx, a.path);
|
|
182
189
|
fs.mkdirSync(path.dirname(p), { recursive: true });
|
|
183
190
|
const content = String(a.content ?? '');
|
|
191
|
+
ctx.checkpoints?.record(p);
|
|
184
192
|
fs.writeFileSync(p, content);
|
|
185
|
-
|
|
193
|
+
const all = content.split('\n');
|
|
194
|
+
const msg = `Wrote ${all.length} lines to ${rel(ctx, p)}`;
|
|
195
|
+
const lines = all.slice(0, 8).map((text, i) => ({ sign: '+', text, n: i + 1 }));
|
|
196
|
+
if (all.length > 8) lines.push({ sign: ' ', text: `… ${all.length - 8} more lines` });
|
|
197
|
+
return { content: msg, display: msg, lines };
|
|
186
198
|
},
|
|
187
199
|
},
|
|
188
200
|
{
|
|
189
201
|
name: 'edit_file',
|
|
202
|
+
writes: true,
|
|
190
203
|
description: 'Replace exact text in a file. old_string must match the file exactly (including whitespace) and be unique unless replace_all is true. Read the file first.',
|
|
191
204
|
parameters: {
|
|
192
205
|
type: 'object',
|
|
@@ -217,12 +230,25 @@ export const TOOLS = [
|
|
|
217
230
|
run(a, ctx) {
|
|
218
231
|
const { p, text, count } = this.validate(a, ctx);
|
|
219
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);
|
|
220
234
|
fs.writeFileSync(p, updated);
|
|
221
|
-
|
|
235
|
+
const startLine = text.slice(0, text.indexOf(a.old_string)).split('\n').length;
|
|
236
|
+
const oldLines = a.old_string.split('\n');
|
|
237
|
+
const newLines = a.new_string.split('\n');
|
|
238
|
+
const lines = [
|
|
239
|
+
...oldLines.slice(0, 12).map((t, i) => ({ sign: '-', text: t, n: startLine + i })),
|
|
240
|
+
...newLines.slice(0, 12).map((t, i) => ({ sign: '+', text: t, n: startLine + i })),
|
|
241
|
+
];
|
|
242
|
+
return {
|
|
243
|
+
content: `Edited ${rel(ctx, p)} (${a.replace_all ? count : 1} replacement${count > 1 && a.replace_all ? 's' : ''})`,
|
|
244
|
+
display: `Updated ${rel(ctx, p)} with ${newLines.length} addition${newLines.length === 1 ? '' : 's'} and ${oldLines.length} removal${oldLines.length === 1 ? '' : 's'}${a.replace_all && count > 1 ? ` (${count} places)` : ''}`,
|
|
245
|
+
lines,
|
|
246
|
+
};
|
|
222
247
|
},
|
|
223
248
|
},
|
|
224
249
|
{
|
|
225
250
|
name: 'bash',
|
|
251
|
+
writes: true,
|
|
226
252
|
description: 'Run a shell command in the working directory and return its combined stdout/stderr. Each call starts fresh in the working directory. No stdin: avoid interactive commands.',
|
|
227
253
|
parameters: {
|
|
228
254
|
type: 'object',
|
|
@@ -238,7 +264,8 @@ export const TOOLS = [
|
|
|
238
264
|
async run(a, ctx) {
|
|
239
265
|
const timeoutMs = Math.min(Math.max(a.timeout_ms || 120000, 1000), 600000);
|
|
240
266
|
const shell = fs.existsSync('/bin/bash') ? '/bin/bash' : true;
|
|
241
|
-
const
|
|
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 });
|
|
242
269
|
if (error) throw error;
|
|
243
270
|
let result = truncate(out.trimEnd()) || '(no output)';
|
|
244
271
|
if (timedOut) result += `\n[timed out after ${timeoutMs / 1000}s]`;
|
|
@@ -332,38 +359,196 @@ export const TOOLS = [
|
|
|
332
359
|
},
|
|
333
360
|
];
|
|
334
361
|
|
|
335
|
-
|
|
336
|
-
|
|
362
|
+
TOOLS.push(
|
|
363
|
+
{
|
|
364
|
+
name: 'web_fetch',
|
|
365
|
+
description: 'Fetch a URL and return its content as text (HTML is converted to readable text). Use it for documentation, issues, API references and other pages the task needs.',
|
|
366
|
+
parameters: {
|
|
367
|
+
type: 'object',
|
|
368
|
+
properties: {
|
|
369
|
+
url: { type: 'string', description: 'Full http(s) URL' },
|
|
370
|
+
max_chars: { type: 'integer', description: 'Max characters to return (default 30000)' },
|
|
371
|
+
},
|
|
372
|
+
required: ['url'],
|
|
373
|
+
},
|
|
374
|
+
needsPermission: true,
|
|
375
|
+
summary: (a) => a.url,
|
|
376
|
+
preview: (a) => ({ title: `Fetch ${a.url}`, lines: [] }),
|
|
377
|
+
async run(a, ctx) {
|
|
378
|
+
const url = new URL(a.url);
|
|
379
|
+
if (!/^https?:$/.test(url.protocol)) throw new Error('Only http and https URLs are supported.');
|
|
380
|
+
const res = await fetch(url, {
|
|
381
|
+
signal: anySignal([ctx.signal, AbortSignal.timeout(30000)]),
|
|
382
|
+
redirect: 'follow',
|
|
383
|
+
headers: { 'user-agent': 'arrakis-cli', accept: 'text/html,text/plain,application/json;q=0.9,*/*;q=0.5' },
|
|
384
|
+
});
|
|
385
|
+
const type = res.headers.get('content-type') || '';
|
|
386
|
+
if (/^(image|audio|video)\/|application\/(pdf|zip|octet-stream)/.test(type)) {
|
|
387
|
+
return { content: `URL: ${res.url}\nStatus: ${res.status}\nContent-Type: ${type}\n(binary content not shown)`, display: `${res.status} · ${type}` };
|
|
388
|
+
}
|
|
389
|
+
let text = (await res.text()).slice(0, 5_000_000);
|
|
390
|
+
if (/html/i.test(type) || /^\s*<(!doctype|html)/i.test(text)) text = htmlToText(text);
|
|
391
|
+
const body = truncate(text, a.max_chars || MAX_OUTPUT);
|
|
392
|
+
return { content: `URL: ${res.url}\nStatus: ${res.status}\n\n${body}`, display: `${res.status} · ${text.length.toLocaleString()} characters` };
|
|
393
|
+
},
|
|
394
|
+
},
|
|
395
|
+
{
|
|
396
|
+
name: 'todo_write',
|
|
397
|
+
description: 'Create or update your task list for work with several steps. Send the whole list every time. Keep exactly one item in_progress while you work, and mark each item completed as soon as it is done. Skip this for simple one-step requests.',
|
|
398
|
+
parameters: {
|
|
399
|
+
type: 'object',
|
|
400
|
+
properties: {
|
|
401
|
+
todos: {
|
|
402
|
+
type: 'array',
|
|
403
|
+
items: {
|
|
404
|
+
type: 'object',
|
|
405
|
+
properties: {
|
|
406
|
+
content: { type: 'string', description: 'What to do, in a few words' },
|
|
407
|
+
status: { type: 'string', enum: ['pending', 'in_progress', 'completed'] },
|
|
408
|
+
},
|
|
409
|
+
required: ['content', 'status'],
|
|
410
|
+
},
|
|
411
|
+
},
|
|
412
|
+
},
|
|
413
|
+
required: ['todos'],
|
|
414
|
+
},
|
|
415
|
+
summary: (a) => {
|
|
416
|
+
const list = Array.isArray(a.todos) ? a.todos : [];
|
|
417
|
+
return `${list.filter((t) => t.status === 'completed').length}/${list.length} done`;
|
|
418
|
+
},
|
|
419
|
+
run(a, ctx) {
|
|
420
|
+
if (!Array.isArray(a.todos)) throw new Error('todos must be an array');
|
|
421
|
+
const todos = a.todos.map((t) => ({ content: String(t.content ?? ''), status: ['pending', 'in_progress', 'completed'].includes(t.status) ? t.status : 'pending' }));
|
|
422
|
+
if (ctx.agent) ctx.agent.todos = todos;
|
|
423
|
+
ctx.ui.todos(todos);
|
|
424
|
+
return { content: 'Todo list updated. Keep it current as you work.', display: null };
|
|
425
|
+
},
|
|
426
|
+
},
|
|
427
|
+
{
|
|
428
|
+
name: 'task',
|
|
429
|
+
description:
|
|
430
|
+
'Hand a self-contained job to a sub-agent that has its own fresh context window. Use it to explore or search large codebases, research a question, or do independent pieces of work in parallel: call it several times in one response and the sub-agents run at the same time. The sub-agent cannot see this conversation, so put everything it needs in prompt. It returns a report, which only you see; summarize it for the user. agent_type "explore" is read-only (search and read); "general" can also edit files and run commands.',
|
|
431
|
+
parameters: {
|
|
432
|
+
type: 'object',
|
|
433
|
+
properties: {
|
|
434
|
+
description: { type: 'string', description: 'A 3-6 word label for the job' },
|
|
435
|
+
prompt: { type: 'string', description: 'Full instructions: the goal, relevant paths, what to report back' },
|
|
436
|
+
agent_type: { type: 'string', enum: ['explore', 'general'], description: 'explore (default): read-only. general: can edit and run commands.' },
|
|
437
|
+
},
|
|
438
|
+
required: ['description', 'prompt'],
|
|
439
|
+
},
|
|
440
|
+
summary: (a) => `${a.agent_type === 'general' ? 'general' : 'explore'}: ${a.description}`,
|
|
441
|
+
async run(a, ctx) {
|
|
442
|
+
if (!ctx.agent?.runSubagent) throw new Error('Sub-agents are not available here.');
|
|
443
|
+
return ctx.agent.runSubagent(a, ctx.signal);
|
|
444
|
+
},
|
|
445
|
+
},
|
|
446
|
+
);
|
|
447
|
+
|
|
448
|
+
export const READ_ONLY_TOOLS = new Set(['read_file', 'grep', 'list_files', 'web_fetch']);
|
|
449
|
+
export const PARALLEL_SAFE = new Set(['read_file', 'grep', 'list_files', 'web_fetch', 'task']);
|
|
337
450
|
|
|
338
|
-
|
|
451
|
+
function anySignal(signals) {
|
|
452
|
+
const list = signals.filter(Boolean);
|
|
453
|
+
if (AbortSignal.any) return AbortSignal.any(list);
|
|
454
|
+
const ac = new AbortController();
|
|
455
|
+
for (const s of list) {
|
|
456
|
+
if (s.aborted) ac.abort(s.reason);
|
|
457
|
+
else s.addEventListener('abort', () => ac.abort(s.reason), { once: true });
|
|
458
|
+
}
|
|
459
|
+
return ac.signal;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
export function htmlToText(html) {
|
|
463
|
+
const title = html.match(/<title[^>]*>([\s\S]*?)<\/title>/i)?.[1]?.trim();
|
|
464
|
+
let t = html
|
|
465
|
+
.replace(/<(script|style|noscript|svg|template|iframe)[\s\S]*?<\/\1>/gi, '')
|
|
466
|
+
.replace(/<!--[\s\S]*?-->/g, '')
|
|
467
|
+
.replace(/<head[\s\S]*?<\/head>/i, '')
|
|
468
|
+
.replace(/<h([1-6])[^>]*>/gi, (_, n) => '\n\n' + '#'.repeat(Number(n)) + ' ')
|
|
469
|
+
.replace(/<li[^>]*>/gi, '\n- ')
|
|
470
|
+
.replace(/<(br|hr)\s*\/?>/gi, '\n')
|
|
471
|
+
.replace(/<\/(p|div|section|article|header|footer|h[1-6]|li|tr|table|ul|ol|pre|blockquote)>/gi, '\n')
|
|
472
|
+
.replace(/<(td|th)[^>]*>/gi, ' | ')
|
|
473
|
+
.replace(/<a [^>]*href="([^"#][^"]*)"[^>]*>([\s\S]*?)<\/a>/gi, (_, href, text) => `${text.replace(/<[^>]+>/g, '')} (${href})`)
|
|
474
|
+
.replace(/<[^>]+>/g, '')
|
|
475
|
+
.replace(/ /g, ' ')
|
|
476
|
+
.replace(/&/g, '&')
|
|
477
|
+
.replace(/</g, '<')
|
|
478
|
+
.replace(/>/g, '>')
|
|
479
|
+
.replace(/"/g, '"')
|
|
480
|
+
.replace(/'|'/g, "'")
|
|
481
|
+
.replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(Number(n)))
|
|
482
|
+
.replace(/&#x([0-9a-f]+);/gi, (_, n) => String.fromCodePoint(parseInt(n, 16)));
|
|
483
|
+
t = t
|
|
484
|
+
.split('\n')
|
|
485
|
+
.map((l) => l.replace(/[ \t]+/g, ' ').trim())
|
|
486
|
+
.join('\n')
|
|
487
|
+
.replace(/\n{3,}/g, '\n\n')
|
|
488
|
+
.trim();
|
|
489
|
+
return title ? `# ${title}\n\n${t}` : t;
|
|
490
|
+
}
|
|
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
|
+
|
|
499
|
+
export const toolSchemas = (filter = () => true) =>
|
|
500
|
+
allTools().filter((t) => filter(t.name)).map((t) => ({ type: 'function', function: { name: t.name, description: t.description, parameters: t.parameters } }));
|
|
501
|
+
|
|
502
|
+
export const findTool = (name) => allTools().find((t) => t.name === name);
|
|
339
503
|
|
|
340
504
|
// Run one tool call from the model. Always returns a string for the tool message.
|
|
505
|
+
// ctx: { cwd, signal, ui, permissions, agent, depth, label, allowed }
|
|
341
506
|
export async function executeToolCall(call, ctx) {
|
|
342
507
|
const tool = findTool(call.function?.name);
|
|
343
|
-
if (!tool
|
|
508
|
+
if (!tool || (ctx.allowed && !ctx.allowed(tool.name))) {
|
|
509
|
+
return `Error: unknown tool "${call.function?.name}".`;
|
|
510
|
+
}
|
|
344
511
|
let args;
|
|
345
512
|
try {
|
|
346
513
|
args = call.function.arguments ? JSON.parse(call.function.arguments) : {};
|
|
347
514
|
} catch {
|
|
348
515
|
return `Error: tool arguments were not valid JSON: ${String(call.function.arguments).slice(0, 200)}`;
|
|
349
516
|
}
|
|
350
|
-
|
|
517
|
+
const sub = (ctx.depth ?? 0) > 0;
|
|
518
|
+
ctx.ui.toolStart(tool.label ?? tool.name, safe(() => tool.summary?.(args)) ?? '', { prefix: sub ? `${ctx.label}` : '' });
|
|
351
519
|
try {
|
|
352
520
|
if (tool.validate) tool.validate(args, ctx);
|
|
521
|
+
if (tool.writes && ctx.permissions.planMode) {
|
|
522
|
+
if (!sub) ctx.ui.toolResult('Blocked: plan mode is on', true);
|
|
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.';
|
|
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
|
+
}
|
|
532
|
+
let preview = null;
|
|
353
533
|
if (tool.needsPermission) {
|
|
354
|
-
|
|
534
|
+
preview = safe(() => tool.preview?.(args, ctx));
|
|
535
|
+
if (preview && sub) preview.title = `[${ctx.label}] ${preview.title}`;
|
|
355
536
|
const allowed = await ctx.permissions.check(tool.name, preview);
|
|
356
537
|
if (!allowed) {
|
|
357
538
|
ctx.ui.toolResult('Denied', true);
|
|
358
539
|
return 'Permission denied: the user declined this tool call. Do not retry it; ask the user how to proceed.';
|
|
359
540
|
}
|
|
360
541
|
}
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
542
|
+
if (!sub) ctx.ui.startSpinner(tool.name === 'task' ? 'Agents working' : 'Running');
|
|
543
|
+
const raw = await tool.run(args, ctx);
|
|
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 });
|
|
546
|
+
// Skip the diff when the user just saw it in the approval prompt.
|
|
547
|
+
if (!sub && display !== null) ctx.ui.toolResult(display ?? content, false, { lines: preview?.shown ? null : lines });
|
|
548
|
+
return truncate(String(content));
|
|
364
549
|
} catch (err) {
|
|
365
|
-
const msg = `Error: ${err.message}`;
|
|
366
|
-
ctx.ui.toolResult(msg, true);
|
|
550
|
+
const msg = `Error: ${err.name === 'AbortError' || err.name === 'TimeoutError' ? (ctx.signal?.aborted ? 'interrupted by the user' : 'timed out') : err.message}`;
|
|
551
|
+
if (!sub) ctx.ui.toolResult(msg, true);
|
|
367
552
|
return msg;
|
|
368
553
|
}
|
|
369
554
|
}
|