@mumbrane/arrakis 0.2.0 → 0.4.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/prompts.js ADDED
@@ -0,0 +1,186 @@
1
+ // Interactive prompts: an arrow-key picker with type-to-search, masked secret
2
+ // input and plain text input. Without a TTY (piped input, CI) they fall back
3
+ // to numbered lists and plain line reads.
4
+ import readline from 'node:readline';
5
+ import { colors } from './ui.js';
6
+
7
+ const isTTY = () => Boolean(process.stdin.isTTY && process.stdout.isTTY && process.stdin.setRawMode);
8
+
9
+ // Read raw keypresses until done() is called. Hands stdin back afterwards.
10
+ function rawKeys(onKey) {
11
+ return new Promise((resolve) => {
12
+ const stdin = process.stdin;
13
+ readline.emitKeypressEvents(stdin);
14
+ const wasRaw = stdin.isRaw;
15
+ stdin.setRawMode(true);
16
+ stdin.resume();
17
+ const done = (value) => {
18
+ stdin.off('keypress', handler);
19
+ stdin.setRawMode(wasRaw);
20
+ stdin.pause();
21
+ resolve(value);
22
+ };
23
+ const handler = (str, key = {}) => onKey(str, key, done);
24
+ stdin.on('keypress', handler);
25
+ });
26
+ }
27
+
28
+ const printable = (str, key) => str && !key.ctrl && !key.meta && str.length >= 1 && !/[\x00-\x1f\x7f]/.test(str);
29
+
30
+ function truncateToWidth(s, width) {
31
+ // Strip ANSI for measuring; cut the visible text if it's too long.
32
+ const plain = s.replace(/\x1b\[[0-9;]*m/g, '');
33
+ if (plain.length <= width) return s;
34
+ return plain.slice(0, Math.max(0, width - 1)) + '…';
35
+ }
36
+
37
+ async function rawSelect({ title, items, initial = 0, pageSize = 10 }) {
38
+ const out = process.stdout;
39
+ const c = colors(out);
40
+ const searchable = items.length > 8;
41
+ let filter = '';
42
+ let cursor = Math.max(0, Math.min(initial, items.length - 1));
43
+ let rendered = 0;
44
+ const visible = () => {
45
+ if (!filter) return items;
46
+ const f = filter.toLowerCase();
47
+ return items.filter((i) => `${i.label} ${i.value} ${i.hint ?? ''}`.toLowerCase().includes(f));
48
+ };
49
+ const clear = () => {
50
+ if (!rendered) return;
51
+ readline.moveCursor(out, 0, -rendered);
52
+ readline.cursorTo(out, 0);
53
+ readline.clearScreenDown(out);
54
+ rendered = 0;
55
+ };
56
+ const render = () => {
57
+ clear();
58
+ const list = visible();
59
+ cursor = Math.max(0, Math.min(cursor, list.length - 1));
60
+ const width = (out.columns || 80) - 1;
61
+ const rows = [`${c.bold(title)} ${c.dim(searchable ? '↑↓ move · type to search · Enter select · Esc cancel' : '↑↓ move · Enter select · Esc cancel')}`];
62
+ if (searchable) rows.push(` ${c.dim('search:')} ${filter}${c.dim('▏')}`);
63
+ if (!list.length) rows.push(c.dim(' no matches'));
64
+ const start = Math.max(0, Math.min(cursor - Math.floor(pageSize / 2), list.length - pageSize));
65
+ list.slice(start, start + pageSize).forEach((item, i) => {
66
+ const selected = start + i === cursor;
67
+ const label = selected ? c.accent(c.bold(item.label)) : item.label;
68
+ rows.push(truncateToWidth(`${selected ? c.accent('❯') : ' '} ${label}${item.hint ? ' ' + c.dim(item.hint) : ''}`, width));
69
+ });
70
+ if (list.length > pageSize) rows.push(c.dim(` ${cursor + 1} of ${list.length}`));
71
+ out.write(rows.join('\n') + '\n');
72
+ rendered = rows.length;
73
+ };
74
+ out.write('\x1b[?25l');
75
+ render();
76
+ const result = await rawKeys((str, key, done) => {
77
+ const list = visible();
78
+ if (key.ctrl && key.name === 'c') return done(null);
79
+ if (key.name === 'escape') {
80
+ if (filter) {
81
+ filter = '';
82
+ cursor = 0;
83
+ return render();
84
+ }
85
+ return done(null);
86
+ }
87
+ if (key.name === 'return' || key.name === 'enter') return done(list[cursor] ?? null);
88
+ if (key.name === 'up') cursor = cursor > 0 ? cursor - 1 : list.length - 1;
89
+ else if (key.name === 'down') cursor = cursor < list.length - 1 ? cursor + 1 : 0;
90
+ else if (key.name === 'pageup') cursor = Math.max(0, cursor - pageSize);
91
+ else if (key.name === 'pagedown') cursor = Math.min(list.length - 1, cursor + pageSize);
92
+ else if (key.name === 'backspace') {
93
+ filter = filter.slice(0, -1);
94
+ cursor = 0;
95
+ } else if (searchable && printable(str, key)) {
96
+ filter += str;
97
+ cursor = 0;
98
+ } else return;
99
+ render();
100
+ });
101
+ clear();
102
+ out.write('\x1b[?25h');
103
+ out.write(`${c.bold(title)} ${result ? c.accent(result.label) : c.dim('cancelled')}\n`);
104
+ return result ? result.value : null;
105
+ }
106
+
107
+ async function rawSecret(label) {
108
+ const out = process.stdout;
109
+ const c = colors(out);
110
+ let value = '';
111
+ const draw = () => {
112
+ readline.cursorTo(out, 0);
113
+ readline.clearLine(out, 1);
114
+ const dots = '•'.repeat(Math.min(value.length, 32)) + (value.length > 32 ? c.dim(` +${value.length - 32}`) : '');
115
+ out.write(`${label}${dots}`);
116
+ };
117
+ draw();
118
+ const result = await rawKeys((str, key, done) => {
119
+ if (key.ctrl && key.name === 'c') return done(null);
120
+ if (key.name === 'escape') return done(null);
121
+ if (key.name === 'return' || key.name === 'enter') return done(value);
122
+ if (key.name === 'backspace') value = value.slice(0, -1);
123
+ else if (key.ctrl && key.name === 'u') value = '';
124
+ else if (printable(str, key)) value += str;
125
+ else return;
126
+ draw();
127
+ });
128
+ out.write('\n');
129
+ return result === null ? null : result.trim();
130
+ }
131
+
132
+ export function createPrompts(ui) {
133
+ return {
134
+ // items: [{ label, value, hint? }]. Resolves to the chosen value, or null.
135
+ async select({ title, items, initial = 0 }) {
136
+ if (isTTY()) {
137
+ ui.suspend();
138
+ try {
139
+ return await rawSelect({ title, items, initial });
140
+ } finally {
141
+ ui.resume();
142
+ }
143
+ }
144
+ return numberedSelect(ui, { title, items, initial });
145
+ },
146
+
147
+ async secret(label) {
148
+ if (isTTY()) {
149
+ ui.suspend();
150
+ try {
151
+ return await rawSecret(label);
152
+ } finally {
153
+ ui.resume();
154
+ }
155
+ }
156
+ const line = await ui.readLine(label);
157
+ return line === null ? null : line.trim();
158
+ },
159
+
160
+ async input(label) {
161
+ const line = await ui.readLine(label);
162
+ return line === null ? null : line.trim();
163
+ },
164
+ };
165
+ }
166
+
167
+ async function numberedSelect(ui, { title, items, initial }) {
168
+ const c = ui.c;
169
+ const shown = items.slice(0, 40);
170
+ ui.write(ui.out, `${c.bold(title)}\n`);
171
+ shown.forEach((item, i) => ui.write(ui.out, ` ${String(i + 1).padStart(2)}. ${item.label}${item.hint ? ' ' + c.dim(item.hint) : ''}\n`));
172
+ if (items.length > shown.length) ui.write(ui.out, c.dim(` … ${items.length - shown.length} more (type a name to pick one)\n`));
173
+ while (true) {
174
+ const answer = await ui.readLine(`Choose 1-${shown.length} ${c.dim(`[${initial + 1}]`)}: `);
175
+ if (answer === null) return null;
176
+ const a = answer.trim();
177
+ if (!a) return items[initial]?.value ?? null;
178
+ const n = Number(a);
179
+ if (Number.isInteger(n) && n >= 1 && n <= shown.length) return shown[n - 1].value;
180
+ const exact = items.find((i) => i.value === a || i.label.toLowerCase() === a.toLowerCase());
181
+ if (exact) return exact.value;
182
+ const partial = items.filter((i) => `${i.label} ${i.value}`.toLowerCase().includes(a.toLowerCase()));
183
+ if (partial.length === 1) return partial[0].value;
184
+ ui.write(ui.out, c.dim(partial.length ? ` ${partial.length} matches; be more specific.\n` : ' No match.\n'));
185
+ }
186
+ }
@@ -0,0 +1,60 @@
1
+ // Built-in providers. Every one speaks the OpenAI-compatible /chat/completions
2
+ // API. To add one (for example your own hosted model), add an entry here.
3
+ //
4
+ // id stable key used in config and ARRAKIS_PROVIDER
5
+ // name shown in the picker
6
+ // baseUrl API base URL (without /chat/completions)
7
+ // keyUrl where users create an API key
8
+ // noKey true for local servers that don't need a key
9
+ // custom true: the user enters the base URL
10
+ // defaultModel highlighted first in the model picker, if the provider lists it
11
+ // contextWindow fallback when the models API doesn't report one
12
+ // maxTokens max output tokens to request (some APIs require it)
13
+ // modelsHeaders headers for GET /models when they differ from Bearer auth
14
+
15
+ export const PROVIDERS = [
16
+ {
17
+ id: 'anthropic',
18
+ name: 'Anthropic (Claude)',
19
+ baseUrl: 'https://api.anthropic.com/v1',
20
+ keyUrl: 'https://platform.claude.com/settings/keys',
21
+ defaultModel: 'claude-opus-5-5',
22
+ contextWindow: 1000000,
23
+ maxTokens: 32000,
24
+ modelsHeaders: (key) => ({ 'x-api-key': key, 'anthropic-version': '2023-06-01' }),
25
+ modelsQuery: '?limit=1000',
26
+ },
27
+ { id: 'openai', name: 'OpenAI', baseUrl: 'https://api.openai.com/v1', keyUrl: 'https://platform.openai.com/api-keys' },
28
+ { id: 'google', name: 'Google Gemini', baseUrl: 'https://generativelanguage.googleapis.com/v1beta/openai', keyUrl: 'https://aistudio.google.com/apikey', contextWindow: 1000000 },
29
+ { id: 'openrouter', name: 'OpenRouter (hundreds of models)', baseUrl: 'https://openrouter.ai/api/v1', keyUrl: 'https://openrouter.ai/keys' },
30
+ { id: 'xai', name: 'xAI (Grok)', baseUrl: 'https://api.x.ai/v1', keyUrl: 'https://console.x.ai' },
31
+ { id: 'deepseek', name: 'DeepSeek', baseUrl: 'https://api.deepseek.com/v1', keyUrl: 'https://platform.deepseek.com/api_keys' },
32
+ { id: 'mistral', name: 'Mistral', baseUrl: 'https://api.mistral.ai/v1', keyUrl: 'https://console.mistral.ai/api-keys' },
33
+ { id: 'groq', name: 'Groq', baseUrl: 'https://api.groq.com/openai/v1', keyUrl: 'https://console.groq.com/keys' },
34
+ { id: 'together', name: 'Together AI', baseUrl: 'https://api.together.xyz/v1', keyUrl: 'https://api.together.ai/settings/api-keys' },
35
+ { id: 'ollama', name: 'Ollama (local models, no key)', baseUrl: 'http://localhost:11434/v1', noKey: true },
36
+ { id: 'custom', name: 'Other OpenAI-compatible API', custom: true },
37
+ ];
38
+
39
+ export const getProvider = (id) => PROVIDERS.find((p) => p.id === id) ?? null;
40
+
41
+ // Models that can't chat (embeddings, audio, images, moderation…).
42
+ const NON_CHAT = /(embed|whisper|tts|dall-e|moderation|transcribe|audio|realtime|image|imagen|veo|sora|davinci|babbage|rerank|aqa|guard)/i;
43
+
44
+ export function normalizeModels(json) {
45
+ const raw = Array.isArray(json) ? json : json?.data ?? json?.models ?? [];
46
+ const models = raw
47
+ .map((m) => {
48
+ const id = String(m.id ?? m.name ?? '').replace(/^models\//, '');
49
+ const created = typeof m.created === 'number' ? m.created * 1000 : m.created_at ? Date.parse(m.created_at) : 0;
50
+ return {
51
+ id,
52
+ name: m.display_name ?? m.displayName ?? (m.name && m.name !== m.id ? m.name : ''),
53
+ created: Number.isFinite(created) ? created : 0,
54
+ contextWindow: Number(m.max_input_tokens ?? m.context_length ?? m.context_window ?? m.inputTokenLimit ?? 0) || 0,
55
+ };
56
+ })
57
+ .filter((m) => m.id && !NON_CHAT.test(m.id));
58
+ if (models.some((m) => m.created)) models.sort((a, b) => b.created - a.created);
59
+ return models;
60
+ }
package/src/session.js CHANGED
@@ -59,12 +59,18 @@ export class Session {
59
59
  return session;
60
60
  }
61
61
 
62
+ // A session that lives only in memory (used by sub-agents).
63
+ static memory() {
64
+ return new Session({ id: 'memory', file: null, meta: {} });
65
+ }
66
+
62
67
  static latest(cwd) {
63
68
  const s = listSessions().find((x) => x.cwd === cwd);
64
69
  return s ? Session.load(s.id) : null;
65
70
  }
66
71
 
67
72
  append(event) {
73
+ if (!this.file) return;
68
74
  const line = JSON.stringify({ ...event, t: event.t ?? Date.now() }) + '\n';
69
75
  const fd = fs.openSync(this.file, 'a', 0o600);
70
76
  try {
@@ -89,6 +95,11 @@ export class Session {
89
95
  this.append({ type: 'compact', messages: this.messages });
90
96
  }
91
97
 
98
+ replace(messages) {
99
+ this.messages = messages;
100
+ this.append({ type: 'compact', messages });
101
+ }
102
+
92
103
  // Every assistant tool call needs a matching tool result, or the API rejects
93
104
  // the history. After an interrupt or crash, fill in the missing ones.
94
105
  repair() {
package/src/setup.js ADDED
@@ -0,0 +1,157 @@
1
+ // First-run setup and switching: choose a provider, paste a key, pick a model.
2
+ import { PROVIDERS, getProvider } from './providers.js';
3
+ import { listModels } from './client.js';
4
+ import { loadConfig, saveProvider, savedProviderSettings, maskSecret } from './config.js';
5
+
6
+ // Returns the new config, or null if the user cancelled.
7
+ export async function runSetup({ ui, prompts, welcome = false }) {
8
+ const c = ui.c;
9
+ if (welcome) {
10
+ ui.write(ui.out, `\n${c.accent(c.bold(' ◆ Welcome to arrakis'))}\n`);
11
+ ui.write(ui.out, c.dim(' Pick the AI provider to use. You can switch any time with /provider.\n\n'));
12
+ }
13
+ let current = null;
14
+ try {
15
+ current = loadConfig().provider;
16
+ } catch {}
17
+
18
+ const providerId = await prompts.select({
19
+ title: 'Provider',
20
+ items: PROVIDERS.map((p) => ({
21
+ label: p.name,
22
+ value: p.id,
23
+ hint: savedProviderSettings(p.id).apiKey ? 'key saved' : p.id === current ? 'current' : '',
24
+ })),
25
+ initial: Math.max(0, PROVIDERS.findIndex((p) => p.id === current)),
26
+ });
27
+ if (!providerId) return null;
28
+ const provider = getProvider(providerId);
29
+ const saved = savedProviderSettings(providerId);
30
+
31
+ let baseUrl = provider.baseUrl;
32
+ if (provider.custom) {
33
+ while (true) {
34
+ const hint = saved.baseUrl ? c.dim(` [${saved.baseUrl}]`) : '';
35
+ const answer = await prompts.input(`API base URL, e.g. https://api.example.com/v1${hint}: `);
36
+ if (answer === null) return null;
37
+ baseUrl = (answer || saved.baseUrl || '').replace(/\/+$/, '');
38
+ if (/^https?:\/\/\S+$/.test(baseUrl)) break;
39
+ ui.write(ui.out, c.red(' Enter a URL starting with http:// or https://\n'));
40
+ }
41
+ }
42
+
43
+ let apiKey = '';
44
+ let models = null;
45
+ let keepKey = false;
46
+ for (let attempt = 0; ; attempt++) {
47
+ if (!provider.noKey && !keepKey) {
48
+ const reuse = saved.apiKey && attempt === 0
49
+ ? await prompts.select({
50
+ title: 'API key',
51
+ items: [
52
+ { label: `Use saved key ${maskSecret(saved.apiKey)}`, value: 'reuse' },
53
+ { label: 'Enter a new key', value: 'new' },
54
+ ],
55
+ })
56
+ : 'new';
57
+ if (reuse === null) return null;
58
+ if (reuse === 'reuse') apiKey = saved.apiKey;
59
+ else {
60
+ if (provider.keyUrl) ui.write(ui.out, c.dim(` Get a key at ${provider.keyUrl}\n`));
61
+ const label = provider.custom ? 'API key (Enter if none): ' : `Paste your ${provider.name} API key: `;
62
+ const key = await prompts.secret(label);
63
+ if (key === null) return null;
64
+ if (!key && !provider.custom) {
65
+ ui.write(ui.out, c.red(' A key is required for this provider.\n'));
66
+ continue;
67
+ }
68
+ apiKey = key;
69
+ }
70
+ }
71
+
72
+ ui.write(ui.out, c.dim(' Checking…'));
73
+ try {
74
+ models = await listModels({ provider: providerId, baseUrl, apiKey });
75
+ ui.write(ui.out, `\r${c.green(' ✓')} Connected${models.length ? ` · ${models.length} models available` : ''}\n`);
76
+ break;
77
+ } catch (err) {
78
+ ui.write(ui.out, '\r');
79
+ if (err.status === 401 || err.status === 403) {
80
+ ui.write(ui.out, c.red(` ✗ ${provider.name} rejected that key (${err.status}). Check it and try again.\n`));
81
+ if (provider.noKey || attempt >= 4) return null;
82
+ keepKey = false;
83
+ continue;
84
+ }
85
+ if (err.unreachable) {
86
+ const msg = provider.id === 'ollama' ? 'Ollama is not running. Start it with: ollama serve' : `Could not reach ${baseUrl}.`;
87
+ ui.write(ui.out, c.red(` ✗ ${msg}\n`));
88
+ } else {
89
+ ui.write(ui.out, c.yellow(` ! Couldn't list models (${err.status ?? err.message}). You can type a model name instead.\n`));
90
+ models = [];
91
+ break;
92
+ }
93
+ const next = await prompts.select({
94
+ title: 'What now?',
95
+ items: [
96
+ { label: 'Try again', value: 'retry' },
97
+ { label: 'Continue anyway and type a model name', value: 'skip' },
98
+ { label: 'Cancel', value: 'cancel' },
99
+ ],
100
+ });
101
+ if (next === null || next === 'cancel') return null;
102
+ if (next === 'retry') {
103
+ keepKey = true;
104
+ continue;
105
+ }
106
+ models = [];
107
+ break;
108
+ }
109
+ }
110
+
111
+ const model = await pickModel({ ui, prompts, provider, models, current: saved.model });
112
+ if (!model) return null;
113
+
114
+ saveProvider(providerId, {
115
+ baseUrl: provider.custom ? baseUrl : undefined,
116
+ apiKey: apiKey || undefined,
117
+ model: model.id,
118
+ contextWindow: model.contextWindow || undefined,
119
+ });
120
+ ui.write(ui.out, `${c.green(' ✓')} Using ${c.bold(model.id)} from ${provider.name}. ${c.dim('Saved to ~/.arrakis/config.json')}\n\n`);
121
+ return loadConfig();
122
+ }
123
+
124
+ // Choose a model from the provider's list (or type one when there's no list).
125
+ export async function pickModel({ ui, prompts, provider, models, current }) {
126
+ if (!models?.length) {
127
+ const name = await prompts.input(`Model name${current ? ui.c.dim(` [${current}]`) : ''}: `);
128
+ if (name === null) return null;
129
+ const id = name || current;
130
+ return id ? { id } : null;
131
+ }
132
+ const preferred = [current, provider?.defaultModel].filter(Boolean);
133
+ let initial = 0;
134
+ for (const p of preferred) {
135
+ const i = models.findIndex((m) => m.id === p);
136
+ if (i >= 0) {
137
+ initial = i;
138
+ break;
139
+ }
140
+ }
141
+ const id = await prompts.select({
142
+ title: 'Model',
143
+ items: models.map((m) => ({
144
+ label: m.id,
145
+ value: m.id,
146
+ hint: [m.name && m.name !== m.id ? m.name : '', m.contextWindow ? `${formatTokens(m.contextWindow)} context` : '', m.id === current ? 'current' : '']
147
+ .filter(Boolean)
148
+ .join(' · '),
149
+ })),
150
+ initial,
151
+ });
152
+ return id ? models.find((m) => m.id === id) : null;
153
+ }
154
+
155
+ function formatTokens(n) {
156
+ return n >= 1_000_000 ? `${+(n / 1_000_000).toFixed(1)}M` : n >= 1000 ? `${Math.round(n / 1000)}K` : String(n);
157
+ }
package/src/tools.js CHANGED
@@ -157,6 +157,7 @@ export const TOOLS = [
157
157
  },
158
158
  {
159
159
  name: 'write_file',
160
+ writes: true,
160
161
  description: 'Create a new file or completely overwrite an existing one. Prefer edit_file for changes to existing files.',
161
162
  parameters: {
162
163
  type: 'object',
@@ -182,11 +183,16 @@ export const TOOLS = [
182
183
  fs.mkdirSync(path.dirname(p), { recursive: true });
183
184
  const content = String(a.content ?? '');
184
185
  fs.writeFileSync(p, content);
185
- return `Wrote ${content.split('\n').length} lines to ${rel(ctx, p)}`;
186
+ const all = content.split('\n');
187
+ const msg = `Wrote ${all.length} lines to ${rel(ctx, p)}`;
188
+ const lines = all.slice(0, 8).map((text, i) => ({ sign: '+', text, n: i + 1 }));
189
+ if (all.length > 8) lines.push({ sign: ' ', text: `… ${all.length - 8} more lines` });
190
+ return { content: msg, display: msg, lines };
186
191
  },
187
192
  },
188
193
  {
189
194
  name: 'edit_file',
195
+ writes: true,
190
196
  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
197
  parameters: {
192
198
  type: 'object',
@@ -218,11 +224,23 @@ export const TOOLS = [
218
224
  const { p, text, count } = this.validate(a, ctx);
219
225
  const updated = a.replace_all ? text.split(a.old_string).join(a.new_string) : text.replace(a.old_string, () => a.new_string);
220
226
  fs.writeFileSync(p, updated);
221
- return `Edited ${rel(ctx, p)} (${a.replace_all ? count : 1} replacement${count > 1 && a.replace_all ? 's' : ''})`;
227
+ const startLine = text.slice(0, text.indexOf(a.old_string)).split('\n').length;
228
+ const oldLines = a.old_string.split('\n');
229
+ const newLines = a.new_string.split('\n');
230
+ const lines = [
231
+ ...oldLines.slice(0, 12).map((t, i) => ({ sign: '-', text: t, n: startLine + i })),
232
+ ...newLines.slice(0, 12).map((t, i) => ({ sign: '+', text: t, n: startLine + i })),
233
+ ];
234
+ return {
235
+ content: `Edited ${rel(ctx, p)} (${a.replace_all ? count : 1} replacement${count > 1 && a.replace_all ? 's' : ''})`,
236
+ 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)` : ''}`,
237
+ lines,
238
+ };
222
239
  },
223
240
  },
224
241
  {
225
242
  name: 'bash',
243
+ writes: true,
226
244
  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
245
  parameters: {
228
246
  type: 'object',
@@ -332,38 +350,181 @@ export const TOOLS = [
332
350
  },
333
351
  ];
334
352
 
335
- export const toolSchemas = () =>
336
- TOOLS.map((t) => ({ type: 'function', function: { name: t.name, description: t.description, parameters: t.parameters } }));
353
+ TOOLS.push(
354
+ {
355
+ name: 'web_fetch',
356
+ 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.',
357
+ parameters: {
358
+ type: 'object',
359
+ properties: {
360
+ url: { type: 'string', description: 'Full http(s) URL' },
361
+ max_chars: { type: 'integer', description: 'Max characters to return (default 30000)' },
362
+ },
363
+ required: ['url'],
364
+ },
365
+ needsPermission: true,
366
+ summary: (a) => a.url,
367
+ preview: (a) => ({ title: `Fetch ${a.url}`, lines: [] }),
368
+ async run(a, ctx) {
369
+ const url = new URL(a.url);
370
+ if (!/^https?:$/.test(url.protocol)) throw new Error('Only http and https URLs are supported.');
371
+ const res = await fetch(url, {
372
+ signal: anySignal([ctx.signal, AbortSignal.timeout(30000)]),
373
+ redirect: 'follow',
374
+ headers: { 'user-agent': 'arrakis-cli', accept: 'text/html,text/plain,application/json;q=0.9,*/*;q=0.5' },
375
+ });
376
+ const type = res.headers.get('content-type') || '';
377
+ if (/^(image|audio|video)\/|application\/(pdf|zip|octet-stream)/.test(type)) {
378
+ return { content: `URL: ${res.url}\nStatus: ${res.status}\nContent-Type: ${type}\n(binary content not shown)`, display: `${res.status} · ${type}` };
379
+ }
380
+ let text = (await res.text()).slice(0, 5_000_000);
381
+ if (/html/i.test(type) || /^\s*<(!doctype|html)/i.test(text)) text = htmlToText(text);
382
+ const body = truncate(text, a.max_chars || MAX_OUTPUT);
383
+ return { content: `URL: ${res.url}\nStatus: ${res.status}\n\n${body}`, display: `${res.status} · ${text.length.toLocaleString()} characters` };
384
+ },
385
+ },
386
+ {
387
+ name: 'todo_write',
388
+ 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.',
389
+ parameters: {
390
+ type: 'object',
391
+ properties: {
392
+ todos: {
393
+ type: 'array',
394
+ items: {
395
+ type: 'object',
396
+ properties: {
397
+ content: { type: 'string', description: 'What to do, in a few words' },
398
+ status: { type: 'string', enum: ['pending', 'in_progress', 'completed'] },
399
+ },
400
+ required: ['content', 'status'],
401
+ },
402
+ },
403
+ },
404
+ required: ['todos'],
405
+ },
406
+ summary: (a) => {
407
+ const list = Array.isArray(a.todos) ? a.todos : [];
408
+ return `${list.filter((t) => t.status === 'completed').length}/${list.length} done`;
409
+ },
410
+ run(a, ctx) {
411
+ if (!Array.isArray(a.todos)) throw new Error('todos must be an array');
412
+ const todos = a.todos.map((t) => ({ content: String(t.content ?? ''), status: ['pending', 'in_progress', 'completed'].includes(t.status) ? t.status : 'pending' }));
413
+ if (ctx.agent) ctx.agent.todos = todos;
414
+ ctx.ui.todos(todos);
415
+ return { content: 'Todo list updated. Keep it current as you work.', display: null };
416
+ },
417
+ },
418
+ {
419
+ name: 'task',
420
+ description:
421
+ '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.',
422
+ parameters: {
423
+ type: 'object',
424
+ properties: {
425
+ description: { type: 'string', description: 'A 3-6 word label for the job' },
426
+ prompt: { type: 'string', description: 'Full instructions: the goal, relevant paths, what to report back' },
427
+ agent_type: { type: 'string', enum: ['explore', 'general'], description: 'explore (default): read-only. general: can edit and run commands.' },
428
+ },
429
+ required: ['description', 'prompt'],
430
+ },
431
+ summary: (a) => `${a.agent_type === 'general' ? 'general' : 'explore'}: ${a.description}`,
432
+ async run(a, ctx) {
433
+ if (!ctx.agent?.runSubagent) throw new Error('Sub-agents are not available here.');
434
+ return ctx.agent.runSubagent(a, ctx.signal);
435
+ },
436
+ },
437
+ );
438
+
439
+ export const READ_ONLY_TOOLS = new Set(['read_file', 'grep', 'list_files', 'web_fetch']);
440
+ export const PARALLEL_SAFE = new Set(['read_file', 'grep', 'list_files', 'web_fetch', 'task']);
441
+
442
+ function anySignal(signals) {
443
+ const list = signals.filter(Boolean);
444
+ if (AbortSignal.any) return AbortSignal.any(list);
445
+ const ac = new AbortController();
446
+ for (const s of list) {
447
+ if (s.aborted) ac.abort(s.reason);
448
+ else s.addEventListener('abort', () => ac.abort(s.reason), { once: true });
449
+ }
450
+ return ac.signal;
451
+ }
452
+
453
+ export function htmlToText(html) {
454
+ const title = html.match(/<title[^>]*>([\s\S]*?)<\/title>/i)?.[1]?.trim();
455
+ let t = html
456
+ .replace(/<(script|style|noscript|svg|template|iframe)[\s\S]*?<\/\1>/gi, '')
457
+ .replace(/<!--[\s\S]*?-->/g, '')
458
+ .replace(/<head[\s\S]*?<\/head>/i, '')
459
+ .replace(/<h([1-6])[^>]*>/gi, (_, n) => '\n\n' + '#'.repeat(Number(n)) + ' ')
460
+ .replace(/<li[^>]*>/gi, '\n- ')
461
+ .replace(/<(br|hr)\s*\/?>/gi, '\n')
462
+ .replace(/<\/(p|div|section|article|header|footer|h[1-6]|li|tr|table|ul|ol|pre|blockquote)>/gi, '\n')
463
+ .replace(/<(td|th)[^>]*>/gi, ' | ')
464
+ .replace(/<a [^>]*href="([^"#][^"]*)"[^>]*>([\s\S]*?)<\/a>/gi, (_, href, text) => `${text.replace(/<[^>]+>/g, '')} (${href})`)
465
+ .replace(/<[^>]+>/g, '')
466
+ .replace(/&nbsp;/g, ' ')
467
+ .replace(/&amp;/g, '&')
468
+ .replace(/&lt;/g, '<')
469
+ .replace(/&gt;/g, '>')
470
+ .replace(/&quot;/g, '"')
471
+ .replace(/&#39;|&apos;/g, "'")
472
+ .replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(Number(n)))
473
+ .replace(/&#x([0-9a-f]+);/gi, (_, n) => String.fromCodePoint(parseInt(n, 16)));
474
+ t = t
475
+ .split('\n')
476
+ .map((l) => l.replace(/[ \t]+/g, ' ').trim())
477
+ .join('\n')
478
+ .replace(/\n{3,}/g, '\n\n')
479
+ .trim();
480
+ return title ? `# ${title}\n\n${t}` : t;
481
+ }
482
+
483
+ 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 } }));
337
485
 
338
486
  export const findTool = (name) => TOOLS.find((t) => t.name === name);
339
487
 
340
488
  // Run one tool call from the model. Always returns a string for the tool message.
489
+ // ctx: { cwd, signal, ui, permissions, agent, depth, label, allowed }
341
490
  export async function executeToolCall(call, ctx) {
342
491
  const tool = findTool(call.function?.name);
343
- if (!tool) return `Error: unknown tool "${call.function?.name}". Available: ${TOOLS.map((t) => t.name).join(', ')}`;
492
+ if (!tool || (ctx.allowed && !ctx.allowed(tool.name))) {
493
+ return `Error: unknown tool "${call.function?.name}".`;
494
+ }
344
495
  let args;
345
496
  try {
346
497
  args = call.function.arguments ? JSON.parse(call.function.arguments) : {};
347
498
  } catch {
348
499
  return `Error: tool arguments were not valid JSON: ${String(call.function.arguments).slice(0, 200)}`;
349
500
  }
350
- ctx.ui.toolStart(tool.name, safe(() => tool.summary?.(args)) ?? '');
501
+ const sub = (ctx.depth ?? 0) > 0;
502
+ ctx.ui.toolStart(tool.name, safe(() => tool.summary?.(args)) ?? '', { prefix: sub ? `${ctx.label}` : '' });
351
503
  try {
352
504
  if (tool.validate) tool.validate(args, ctx);
505
+ if (tool.writes && ctx.permissions.planMode) {
506
+ if (!sub) ctx.ui.toolResult('Blocked: plan mode is on', true);
507
+ 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
+ }
509
+ let preview = null;
353
510
  if (tool.needsPermission) {
354
- const preview = safe(() => tool.preview?.(args, ctx));
511
+ preview = safe(() => tool.preview?.(args, ctx));
512
+ if (preview && sub) preview.title = `[${ctx.label}] ${preview.title}`;
355
513
  const allowed = await ctx.permissions.check(tool.name, preview);
356
514
  if (!allowed) {
357
515
  ctx.ui.toolResult('Denied', true);
358
516
  return 'Permission denied: the user declined this tool call. Do not retry it; ask the user how to proceed.';
359
517
  }
360
518
  }
361
- const result = String(await tool.run(args, ctx));
362
- ctx.ui.toolResult(result);
363
- return truncate(result);
519
+ if (!sub) ctx.ui.startSpinner(tool.name === 'task' ? 'Agents working' : 'Running');
520
+ const raw = await tool.run(args, ctx);
521
+ const { content, display, lines } = raw && typeof raw === 'object' ? raw : { content: String(raw) };
522
+ // Skip the diff when the user just saw it in the approval prompt.
523
+ if (!sub && display !== null) ctx.ui.toolResult(display ?? content, false, { lines: preview?.shown ? null : lines });
524
+ return truncate(String(content));
364
525
  } catch (err) {
365
- const msg = `Error: ${err.message}`;
366
- ctx.ui.toolResult(msg, true);
526
+ const msg = `Error: ${err.name === 'AbortError' || err.name === 'TimeoutError' ? (ctx.signal?.aborted ? 'interrupted by the user' : 'timed out') : err.message}`;
527
+ if (!sub) ctx.ui.toolResult(msg, true);
367
528
  return msg;
368
529
  }
369
530
  }