@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/README.md +33 -12
- package/package.json +1 -1
- package/src/agent.js +130 -24
- package/src/cli.js +158 -38
- package/src/client.js +20 -9
- package/src/config.js +101 -28
- package/src/input.js +75 -0
- package/src/markdown.js +134 -0
- package/src/permissions.js +16 -6
- package/src/prompt.js +17 -1
- package/src/prompts.js +186 -0
- package/src/providers.js +60 -0
- package/src/session.js +11 -0
- package/src/setup.js +157 -0
- package/src/tools.js +173 -12
- package/src/ui.js +262 -32
package/README.md
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
# arrakis
|
|
2
2
|
|
|
3
|
-
A coding agent for your terminal, built by Mumbrane. It reads your code, edits files and runs commands, asking before anything that changes your machine.
|
|
3
|
+
A coding agent for your terminal, built by Mumbrane. It reads your code, edits files and runs commands, asking before anything that changes your machine. It works with Claude, GPT, Gemini, Grok, DeepSeek, Mistral, local models and more.
|
|
4
|
+
|
|
5
|
+
- **Sub-agents:** hands big jobs to parallel agents with their own fresh context
|
|
6
|
+
- **Plan mode:** investigates and proposes a plan before touching anything
|
|
7
|
+
- **Task lists:** tracks multi-step work as it goes
|
|
8
|
+
- **Crash-safe sessions:** every step is logged; pick up exactly where you left off
|
|
4
9
|
|
|
5
10
|
## Install
|
|
6
11
|
|
|
@@ -12,15 +17,20 @@ Requires Node.js 18.17 or later.
|
|
|
12
17
|
|
|
13
18
|
## Set up
|
|
14
19
|
|
|
15
|
-
|
|
20
|
+
Run `arrakis`. The first time, it walks you through setup:
|
|
16
21
|
|
|
17
|
-
|
|
18
|
-
arrakis
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
1. **Pick a provider** with the arrow keys: Anthropic (Claude), OpenAI, Google Gemini, OpenRouter, xAI, DeepSeek, Mistral, Groq, Together AI, Ollama (local) or any other OpenAI-compatible API.
|
|
23
|
+
2. **Paste your API key.** It's hidden as you type, checked against the provider, and saved to `~/.arrakis/config.json` (readable only by you).
|
|
24
|
+
3. **Pick a model** from the provider's live model list. Type to search.
|
|
25
|
+
|
|
26
|
+
That's it. Switch any time inside a session:
|
|
27
|
+
|
|
28
|
+
- `/model` picks another model from the same provider.
|
|
29
|
+
- `/provider` switches provider or updates your key. Each provider remembers its own key and model.
|
|
22
30
|
|
|
23
|
-
|
|
31
|
+
Outside a session: `arrakis setup` reruns setup, `arrakis models` lists models, `arrakis logout` removes saved keys.
|
|
32
|
+
|
|
33
|
+
For CI or scripts, skip setup with environment variables: `ARRAKIS_PROVIDER` (e.g. `anthropic`), `ARRAKIS_API_KEY`, `ARRAKIS_MODEL`, or `ARRAKIS_BASE_URL` for any OpenAI-compatible endpoint.
|
|
24
34
|
|
|
25
35
|
## Use
|
|
26
36
|
|
|
@@ -37,22 +47,33 @@ In a session:
|
|
|
37
47
|
|
|
38
48
|
| Command | What it does |
|
|
39
49
|
|---|---|
|
|
40
|
-
| `/help` | List commands |
|
|
50
|
+
| `/help` | List commands (including your own) |
|
|
51
|
+
| `/plan` | Plan mode: investigate and propose, change nothing; `/plan` again to approve |
|
|
52
|
+
| `/init` | Write `ARRAKIS.md` project notes for this folder |
|
|
53
|
+
| `/todos` | Show the current task list |
|
|
41
54
|
| `/status` | Session, model, context usage, permissions |
|
|
42
55
|
| `/compact` | Summarize history to free up context |
|
|
43
|
-
| `/model [name]` |
|
|
56
|
+
| `/model [name]` | Pick a model from your provider |
|
|
57
|
+
| `/provider` | Switch provider or update your API key |
|
|
44
58
|
| `/clear` | Start a new session |
|
|
45
59
|
| `/yolo` | Toggle running tool calls without asking |
|
|
46
60
|
| `/exit` | Quit (or Ctrl-D) |
|
|
47
61
|
|
|
48
|
-
Ctrl-C interrupts the model mid-response. End a line with `\` to keep typing on the next line.
|
|
62
|
+
Type `@path/to/file` to attach a file (or folder listing) to your message. Esc or Ctrl-C interrupts the model mid-response. End a line with `\` to keep typing on the next line.
|
|
63
|
+
|
|
64
|
+
### Custom commands
|
|
65
|
+
|
|
66
|
+
Put a Markdown file in `.arrakis/commands/` (per project) or `~/.arrakis/commands/` (everywhere) and it becomes a slash command. `.arrakis/commands/review.md` becomes `/review`; `$ARGUMENTS` in the file is replaced with whatever you type after the command.
|
|
49
67
|
|
|
50
68
|
## Tools
|
|
51
69
|
|
|
52
70
|
| Tool | Asks first |
|
|
53
71
|
|---|---|
|
|
54
72
|
| `read_file`, `grep`, `list_files` | No |
|
|
55
|
-
| `
|
|
73
|
+
| `todo_write` (task list), `task` (sub-agents) | No |
|
|
74
|
+
| `write_file`, `edit_file`, `bash`, `web_fetch` | Yes: answer `y`, `a` (always, for this session) or `n` |
|
|
75
|
+
|
|
76
|
+
Sub-agents of type `explore` (the default) can only read and search. `general` sub-agents can also edit and run commands, and they ask permission the same way.
|
|
56
77
|
|
|
57
78
|
`-p` mode denies tools that need permission unless you pass `--yolo` or `--allow edit_file,write_file,bash`.
|
|
58
79
|
|
package/package.json
CHANGED
package/src/agent.js
CHANGED
|
@@ -1,27 +1,45 @@
|
|
|
1
1
|
import { streamChat, completeText } from './client.js';
|
|
2
|
-
import { toolSchemas, executeToolCall, truncate } from './tools.js';
|
|
2
|
+
import { toolSchemas, executeToolCall, truncate, READ_ONLY_TOOLS, PARALLEL_SAFE } from './tools.js';
|
|
3
3
|
import { buildSystemPrompt } from './prompt.js';
|
|
4
|
+
import { Session } from './session.js';
|
|
4
5
|
|
|
5
|
-
const MAX_STEPS =
|
|
6
|
+
const MAX_STEPS = 400;
|
|
7
|
+
const MAX_SUB_STEPS = 150;
|
|
6
8
|
const COMPACT_AT = 0.8;
|
|
7
9
|
const KEEP_RECENT = 6;
|
|
8
10
|
|
|
9
11
|
export const estimateTokens = (messages) => Math.ceil(JSON.stringify(messages).length / 4);
|
|
10
12
|
|
|
11
13
|
export class Agent {
|
|
12
|
-
|
|
14
|
+
// depth 0 is the agent the user talks to; sub-agents have depth >= 1.
|
|
15
|
+
constructor({ config, session, ui, permissions, cwd, depth = 0, label = '', toolFilter = null }) {
|
|
13
16
|
this.config = config;
|
|
14
17
|
this.session = session;
|
|
15
18
|
this.ui = ui;
|
|
16
19
|
this.permissions = permissions;
|
|
17
20
|
this.cwd = cwd;
|
|
21
|
+
this.depth = depth;
|
|
22
|
+
this.label = label;
|
|
23
|
+
this.toolFilter = toolFilter;
|
|
18
24
|
this.abort = null;
|
|
25
|
+
this.todos = [];
|
|
26
|
+
this.toolCount = 0;
|
|
27
|
+
this.lastError = null;
|
|
19
28
|
}
|
|
20
29
|
|
|
21
30
|
get busy() {
|
|
22
31
|
return this.abort !== null;
|
|
23
32
|
}
|
|
24
33
|
|
|
34
|
+
get silent() {
|
|
35
|
+
return this.depth > 0;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
allowsTool(name) {
|
|
39
|
+
if (this.depth > 0 && (name === 'task' || name === 'todo_write')) return false;
|
|
40
|
+
return this.toolFilter ? this.toolFilter(name) : true;
|
|
41
|
+
}
|
|
42
|
+
|
|
25
43
|
interrupt() {
|
|
26
44
|
if (!this.abort) return;
|
|
27
45
|
this.abort.abort();
|
|
@@ -29,50 +47,119 @@ export class Agent {
|
|
|
29
47
|
}
|
|
30
48
|
|
|
31
49
|
// Run one user turn to completion. Returns true on success.
|
|
32
|
-
async send(text) {
|
|
50
|
+
async send(text, { parentSignal } = {}) {
|
|
33
51
|
this.session.add({ role: 'user', content: text });
|
|
34
52
|
this.abort = new AbortController();
|
|
35
53
|
const signal = this.abort.signal;
|
|
54
|
+
const onParentAbort = () => this.abort?.abort();
|
|
55
|
+
parentSignal?.addEventListener('abort', onParentAbort, { once: true });
|
|
56
|
+
if (!this.silent) this.ui.setBusy(true);
|
|
36
57
|
try {
|
|
37
|
-
|
|
58
|
+
const limit = this.depth > 0 ? MAX_SUB_STEPS : MAX_STEPS;
|
|
59
|
+
for (let step = 0; step < limit; step++) {
|
|
38
60
|
await this.maybeCompact(signal);
|
|
39
61
|
const msg = await this.callModel(signal);
|
|
40
62
|
if (!msg.tool_calls?.length) return true;
|
|
41
|
-
|
|
42
|
-
const content = signal.aborted
|
|
43
|
-
? 'Interrupted by the user before this tool ran.'
|
|
44
|
-
: await executeToolCall(call, { cwd: this.cwd, signal, ui: this.ui, permissions: this.permissions });
|
|
45
|
-
this.session.add({ role: 'tool', tool_call_id: call.id, content });
|
|
46
|
-
}
|
|
63
|
+
await this.runTools(msg.tool_calls, signal);
|
|
47
64
|
if (signal.aborted) throw new DOMException('Interrupted', 'AbortError');
|
|
48
65
|
}
|
|
49
|
-
this.
|
|
66
|
+
this.lastError = `Stopped after ${limit} steps.`;
|
|
67
|
+
if (!this.silent) this.ui.error(this.lastError);
|
|
50
68
|
return false;
|
|
51
69
|
} catch (err) {
|
|
52
|
-
|
|
70
|
+
this.lastError = err.name === 'AbortError' ? 'Interrupted.' : err.message;
|
|
71
|
+
if (this.silent) return false;
|
|
72
|
+
if (err.name === 'AbortError') this.ui.info('Interrupted. Tell arrakis what to do differently, or keep going.');
|
|
73
|
+
else if (err.status === 401 || err.status === 403) this.ui.error(`${err.message}\nYour API key was rejected. Run /provider to update it.`);
|
|
74
|
+
else if (err.status === 404) this.ui.error(`${err.message}\nThe model may not exist for this provider. Pick another with /model.`);
|
|
53
75
|
else this.ui.error(err.message);
|
|
54
76
|
return false;
|
|
55
77
|
} finally {
|
|
56
|
-
|
|
78
|
+
parentSignal?.removeEventListener('abort', onParentAbort);
|
|
79
|
+
if (!this.silent) {
|
|
80
|
+
this.ui.stopSpinner();
|
|
81
|
+
this.ui.endText();
|
|
82
|
+
this.ui.setBusy(false);
|
|
83
|
+
}
|
|
57
84
|
this.abort = null;
|
|
58
85
|
this.session.repair();
|
|
59
86
|
}
|
|
60
87
|
}
|
|
61
88
|
|
|
89
|
+
async runTools(calls, signal) {
|
|
90
|
+
const ctx = {
|
|
91
|
+
cwd: this.cwd,
|
|
92
|
+
signal,
|
|
93
|
+
ui: this.ui,
|
|
94
|
+
permissions: this.permissions,
|
|
95
|
+
agent: this,
|
|
96
|
+
depth: this.depth,
|
|
97
|
+
label: this.label,
|
|
98
|
+
allowed: (name) => this.allowsTool(name),
|
|
99
|
+
};
|
|
100
|
+
this.toolCount += calls.length;
|
|
101
|
+
const parallel = calls.length > 1 && calls.every((c) => PARALLEL_SAFE.has(c.function?.name));
|
|
102
|
+
let results;
|
|
103
|
+
if (parallel) {
|
|
104
|
+
results = await Promise.all(calls.map((call) => executeToolCall(call, ctx)));
|
|
105
|
+
} else {
|
|
106
|
+
results = [];
|
|
107
|
+
for (const call of calls) {
|
|
108
|
+
results.push(signal.aborted ? 'Interrupted by the user before this tool ran.' : await executeToolCall(call, ctx));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (!this.silent) this.ui.stopSpinner();
|
|
112
|
+
calls.forEach((call, i) => this.session.add({ role: 'tool', tool_call_id: call.id, content: results[i] }));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async runSubagent({ description = 'sub-task', prompt, agent_type = 'explore' }, signal) {
|
|
116
|
+
if (!prompt) throw new Error('prompt is required');
|
|
117
|
+
const readOnly = agent_type !== 'general';
|
|
118
|
+
const sub = new Agent({
|
|
119
|
+
config: this.config,
|
|
120
|
+
session: Session.memory(),
|
|
121
|
+
ui: this.ui,
|
|
122
|
+
permissions: this.permissions,
|
|
123
|
+
cwd: this.cwd,
|
|
124
|
+
depth: this.depth + 1,
|
|
125
|
+
label: description,
|
|
126
|
+
toolFilter: readOnly ? (name) => READ_ONLY_TOOLS.has(name) : null,
|
|
127
|
+
});
|
|
128
|
+
sub.subagentType = readOnly ? 'explore' : 'general';
|
|
129
|
+
const started = Date.now();
|
|
130
|
+
const ok = await sub.send(prompt, { parentSignal: signal });
|
|
131
|
+
const secs = Math.round((Date.now() - started) / 1000);
|
|
132
|
+
const report = [...sub.session.messages].reverse().find((m) => m.role === 'assistant' && m.content)?.content ?? '';
|
|
133
|
+
const stats = `${sub.toolCount} tool use${sub.toolCount === 1 ? '' : 's'} · ${secs}s`;
|
|
134
|
+
if (!ok) {
|
|
135
|
+
return { content: `Sub-agent "${description}" did not finish: ${sub.lastError}\n\nPartial report:\n${report || '(none)'}`, display: `${description}: stopped, ${sub.lastError} (${stats})` };
|
|
136
|
+
}
|
|
137
|
+
return { content: report || '(The sub-agent returned no report.)', display: `${description}: done (${stats})` };
|
|
138
|
+
}
|
|
139
|
+
|
|
62
140
|
async callModel(signal) {
|
|
63
|
-
const
|
|
141
|
+
const system = buildSystemPrompt({
|
|
142
|
+
cwd: this.cwd,
|
|
143
|
+
model: this.config.model,
|
|
144
|
+
planMode: this.permissions.planMode,
|
|
145
|
+
subagent: this.depth > 0 ? { type: this.subagentType } : null,
|
|
146
|
+
});
|
|
147
|
+
const messages = [{ role: 'system', content: system }, ...this.session.messages];
|
|
64
148
|
let content = '';
|
|
65
149
|
const calls = [];
|
|
66
|
-
this.
|
|
150
|
+
if (!this.silent) {
|
|
151
|
+
this.ui.startResponse();
|
|
152
|
+
this.ui.startSpinner();
|
|
153
|
+
}
|
|
67
154
|
try {
|
|
68
|
-
for await (const event of streamChat({ config: this.config, messages, tools: toolSchemas(), signal })) {
|
|
155
|
+
for await (const event of streamChat({ config: this.config, messages, tools: toolSchemas((n) => this.allowsTool(n)), signal })) {
|
|
69
156
|
const delta = event.choices?.[0]?.delta;
|
|
70
157
|
if (!delta) continue;
|
|
71
158
|
const reasoning = delta.reasoning_content ?? delta.reasoning;
|
|
72
|
-
if (reasoning) this.ui.thinking(reasoning);
|
|
159
|
+
if (reasoning && !this.silent) this.ui.thinking(reasoning);
|
|
73
160
|
if (delta.content) {
|
|
74
161
|
content += delta.content;
|
|
75
|
-
this.ui.text(delta.content);
|
|
162
|
+
if (!this.silent) this.ui.text(delta.content);
|
|
76
163
|
}
|
|
77
164
|
for (const tc of delta.tool_calls ?? []) {
|
|
78
165
|
const idx = tc.index ?? (tc.id ? calls.length : Math.max(0, calls.length - 1));
|
|
@@ -83,7 +170,7 @@ export class Agent {
|
|
|
83
170
|
}
|
|
84
171
|
}
|
|
85
172
|
} finally {
|
|
86
|
-
this.ui.endText();
|
|
173
|
+
if (!this.silent) this.ui.endText();
|
|
87
174
|
}
|
|
88
175
|
const toolCalls = calls.filter(Boolean).map((c, i) => ({ ...c, id: c.id || `call_${Date.now()}_${i}` }));
|
|
89
176
|
const msg = { role: 'assistant', content: content || (toolCalls.length ? null : '') };
|
|
@@ -96,7 +183,6 @@ export class Agent {
|
|
|
96
183
|
const messages = this.session.messages;
|
|
97
184
|
if (!force && estimateTokens(messages) < this.config.contextWindow * COMPACT_AT) return false;
|
|
98
185
|
// Cut at a user message so tool calls stay paired with their results.
|
|
99
|
-
// A manual /compact keeps only the last exchange; automatic keeps a bit more.
|
|
100
186
|
const keep = force ? 2 : KEEP_RECENT;
|
|
101
187
|
let cut = -1;
|
|
102
188
|
for (let i = messages.length - 1; i > 0; i--) {
|
|
@@ -108,8 +194,8 @@ export class Agent {
|
|
|
108
194
|
if (cut <= 0) {
|
|
109
195
|
for (let i = messages.length - 1; i > 0; i--) if (messages[i].role === 'user') { cut = i; break; }
|
|
110
196
|
}
|
|
111
|
-
if (cut <= 0) return
|
|
112
|
-
this.ui.info('Compacting conversation history…');
|
|
197
|
+
if (cut <= 0) return this.trimToolResults();
|
|
198
|
+
if (!this.silent) this.ui.info('Compacting conversation history to free up context…');
|
|
113
199
|
const transcript = messages.slice(0, cut).map(renderForSummary).join('\n\n');
|
|
114
200
|
const summary = await completeText({
|
|
115
201
|
config: this.config,
|
|
@@ -118,13 +204,33 @@ export class Agent {
|
|
|
118
204
|
{ role: 'system', content: 'You summarize coding-agent conversations so the work can continue with less context.' },
|
|
119
205
|
{
|
|
120
206
|
role: 'user',
|
|
121
|
-
content: `Summarize this conversation between a user and a coding agent. Keep: the user's goals and instructions, decisions made, files read or changed (with paths), commands run and their important results, errors hit, and what is left to do. Be specific and concise.\n\n<conversation>\n${truncate(transcript, 400000)}\n</conversation>`,
|
|
207
|
+
content: `Summarize this conversation between a user and a coding agent. Keep: the user's goals and instructions, decisions made, files read or changed (with paths), commands run and their important results, errors hit, the current task list, and what is left to do. Be specific and concise.\n\n<conversation>\n${truncate(transcript, 400000)}\n</conversation>`,
|
|
122
208
|
},
|
|
123
209
|
],
|
|
124
210
|
});
|
|
125
211
|
this.session.compact(summary.trim() || '(no summary produced)', cut);
|
|
212
|
+
if (estimateTokens(this.session.messages) >= this.config.contextWindow * COMPACT_AT) this.trimToolResults();
|
|
126
213
|
return true;
|
|
127
214
|
}
|
|
215
|
+
|
|
216
|
+
// One long turn with no user message to cut at: shorten old tool results.
|
|
217
|
+
trimToolResults() {
|
|
218
|
+
const messages = this.session.messages;
|
|
219
|
+
const recent = Math.max(0, messages.length - 8);
|
|
220
|
+
let changed = false;
|
|
221
|
+
const next = messages.map((m, i) => {
|
|
222
|
+
if (i < recent && m.role === 'tool' && typeof m.content === 'string' && m.content.length > 600) {
|
|
223
|
+
changed = true;
|
|
224
|
+
return { ...m, content: `${m.content.slice(0, 500)}\n… [older tool output trimmed to save context; re-run the tool if you need it]` };
|
|
225
|
+
}
|
|
226
|
+
return m;
|
|
227
|
+
});
|
|
228
|
+
if (changed) {
|
|
229
|
+
this.session.replace(next);
|
|
230
|
+
if (!this.silent) this.ui.info('Trimmed old tool output to free up context.');
|
|
231
|
+
}
|
|
232
|
+
return changed;
|
|
233
|
+
}
|
|
128
234
|
}
|
|
129
235
|
|
|
130
236
|
function renderForSummary(m) {
|
package/src/cli.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { parseArgs } from 'node:util';
|
|
2
|
-
import { loadConfig, setConfigValue, readConfigFile, maskSecret, CONFIG_KEYS, CONFIG_PATH } from './config.js';
|
|
3
|
-
import { resolveModel } from './client.js';
|
|
2
|
+
import { loadConfig, setConfigValue, readConfigFile, maskSecret, saveProvider, removeProviderKeys, CONFIG_KEYS, CONFIG_PATH } from './config.js';
|
|
3
|
+
import { resolveModel, listModels } from './client.js';
|
|
4
|
+
import { getProvider } from './providers.js';
|
|
5
|
+
import { createPrompts } from './prompts.js';
|
|
6
|
+
import { runSetup, pickModel } from './setup.js';
|
|
4
7
|
import { Session, listSessions } from './session.js';
|
|
5
8
|
import { Agent, estimateTokens } from './agent.js';
|
|
6
9
|
import { Permissions } from './permissions.js';
|
|
@@ -8,17 +11,21 @@ import { UI, colors } from './ui.js';
|
|
|
8
11
|
import { TOOLS } from './tools.js';
|
|
9
12
|
import { loadProjectInstructions } from './prompt.js';
|
|
10
13
|
import { VERSION } from './version.js';
|
|
14
|
+
import { expandMentions, loadCustomCommands, renderCommand, INIT_PROMPT } from './input.js';
|
|
11
15
|
|
|
12
16
|
const HELP = `arrakis ${VERSION} — a coding agent for your terminal
|
|
13
17
|
|
|
14
18
|
Usage
|
|
15
|
-
arrakis [prompt] Start an interactive session (
|
|
19
|
+
arrakis [prompt] Start an interactive session (first run walks you through setup)
|
|
16
20
|
arrakis -p "prompt" Run one task non-interactively and print the answer
|
|
17
21
|
arrakis -c Continue the most recent session in this directory
|
|
18
22
|
arrakis -r <id> Resume a session by id (or id prefix)
|
|
23
|
+
arrakis setup Choose a provider, enter an API key and pick a model
|
|
24
|
+
arrakis models List the models your provider offers
|
|
19
25
|
arrakis sessions List recent sessions
|
|
20
26
|
arrakis config Show configuration
|
|
21
|
-
arrakis config set <key> <v> Set a
|
|
27
|
+
arrakis config set <key> <v> Set a value (provider, baseUrl, apiKey, model, contextWindow, maxTokens)
|
|
28
|
+
arrakis logout Remove saved API keys
|
|
22
29
|
|
|
23
30
|
Options
|
|
24
31
|
-p, --print Non-interactive: print the response and exit (reads stdin if no prompt)
|
|
@@ -31,23 +38,34 @@ Options
|
|
|
31
38
|
-v, --version Print version
|
|
32
39
|
-h, --help Show this help
|
|
33
40
|
|
|
34
|
-
Environment
|
|
35
|
-
ARRAKIS_BASE_URL, ARRAKIS_API_KEY, ARRAKIS_MODEL, ARRAKIS_CONTEXT_WINDOW, ARRAKIS_HOME`;
|
|
41
|
+
Environment (overrides the config file)
|
|
42
|
+
ARRAKIS_PROVIDER, ARRAKIS_BASE_URL, ARRAKIS_API_KEY, ARRAKIS_MODEL, ARRAKIS_CONTEXT_WINDOW, ARRAKIS_HOME`;
|
|
36
43
|
|
|
37
44
|
const SLASH_HELP = `/help Show commands
|
|
45
|
+
/plan Toggle plan mode: investigate and propose a plan, change nothing
|
|
46
|
+
/init Write ARRAKIS.md project notes for this folder
|
|
47
|
+
/todos Show the current task list
|
|
38
48
|
/clear Start a new session
|
|
39
49
|
/compact Summarize history to free up context
|
|
40
|
-
/model [name]
|
|
50
|
+
/model [name] Pick a model from your provider (or set one by name)
|
|
51
|
+
/provider Switch provider or update your API key
|
|
41
52
|
/sessions List recent sessions
|
|
42
53
|
/status Session, model, context usage and permissions
|
|
43
54
|
/yolo Toggle allowing all tool calls without asking
|
|
44
55
|
/exit Quit (or Ctrl-D)
|
|
45
56
|
|
|
46
|
-
End a line with \\ to continue on the next line.
|
|
57
|
+
Type @path to attach a file or folder. End a line with \\ to continue on the next line.
|
|
58
|
+
Esc or Ctrl-C interrupts the model. Add your own commands as .arrakis/commands/<name>.md.`;
|
|
47
59
|
|
|
48
60
|
export async function main(argv) {
|
|
49
61
|
if (argv[0] === 'config') return configCommand(argv.slice(1));
|
|
50
62
|
if (argv[0] === 'sessions') return sessionsCommand();
|
|
63
|
+
if (argv[0] === 'setup' || argv[0] === 'login') return setupCommand();
|
|
64
|
+
if (argv[0] === 'models') return modelsCommand();
|
|
65
|
+
if (argv[0] === 'logout') {
|
|
66
|
+
removeProviderKeys();
|
|
67
|
+
return console.log(`Removed saved API keys from ${CONFIG_PATH}`);
|
|
68
|
+
}
|
|
51
69
|
|
|
52
70
|
const { values, positionals } = parseArgs({
|
|
53
71
|
args: argv,
|
|
@@ -73,7 +91,20 @@ export async function main(argv) {
|
|
|
73
91
|
if (print && !prompt) throw new Error('No prompt given. Usage: arrakis -p "your task"');
|
|
74
92
|
|
|
75
93
|
const cwd = process.cwd();
|
|
76
|
-
|
|
94
|
+
let config = loadConfig({ model: values.model, baseUrl: values['base-url'] });
|
|
95
|
+
const ui = new UI({ print });
|
|
96
|
+
const prompts = createPrompts(ui);
|
|
97
|
+
if (!config.configured) {
|
|
98
|
+
if (print) throw new Error('arrakis is not set up yet. Run `arrakis` once to choose a provider and enter an API key.');
|
|
99
|
+
ui.start();
|
|
100
|
+
const next = await runSetup({ ui, prompts, welcome: true });
|
|
101
|
+
if (!next) {
|
|
102
|
+
ui.close();
|
|
103
|
+
ui.write(ui.out, 'Setup cancelled. Run arrakis again when you are ready.\n');
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
config = loadConfig({ model: values.model, baseUrl: values['base-url'] });
|
|
107
|
+
}
|
|
77
108
|
config.model = await resolveModel(config);
|
|
78
109
|
|
|
79
110
|
let session;
|
|
@@ -83,7 +114,6 @@ export async function main(argv) {
|
|
|
83
114
|
if (!session) throw new Error(`No previous session in ${cwd}. Start one with: arrakis`);
|
|
84
115
|
} else session = Session.create({ cwd, model: config.model });
|
|
85
116
|
|
|
86
|
-
const ui = new UI({ print });
|
|
87
117
|
const permissions = new Permissions({
|
|
88
118
|
ui,
|
|
89
119
|
mode: values.yolo ? 'yolo' : print ? 'deny' : 'ask',
|
|
@@ -96,14 +126,14 @@ export async function main(argv) {
|
|
|
96
126
|
agent.interrupt();
|
|
97
127
|
process.exit(130);
|
|
98
128
|
});
|
|
99
|
-
const ok = await agent.send(prompt);
|
|
129
|
+
const ok = await agent.send(expandMentions(prompt, cwd).text);
|
|
100
130
|
process.exitCode = ok ? 0 : 1;
|
|
101
131
|
return;
|
|
102
132
|
}
|
|
103
|
-
await interactive({ agent, ui, config, cwd, firstPrompt: prompt });
|
|
133
|
+
await interactive({ agent, ui, prompts, config, cwd, firstPrompt: prompt });
|
|
104
134
|
}
|
|
105
135
|
|
|
106
|
-
async function interactive({ agent, ui, config, cwd, firstPrompt }) {
|
|
136
|
+
async function interactive({ agent, ui, prompts, config, cwd, firstPrompt }) {
|
|
107
137
|
const c = ui.c;
|
|
108
138
|
ui.start();
|
|
109
139
|
let armedExit = false;
|
|
@@ -114,42 +144,82 @@ async function interactive({ agent, ui, config, cwd, firstPrompt }) {
|
|
|
114
144
|
process.exit(0);
|
|
115
145
|
}
|
|
116
146
|
armedExit = true;
|
|
117
|
-
ui.write(ui.out, `\n${c.
|
|
147
|
+
ui.write(ui.out, `\n${c.gray('(Press Ctrl-C again to exit)')}\n`);
|
|
118
148
|
ui.rl?.prompt();
|
|
119
149
|
});
|
|
150
|
+
ui.onEscape = () => {
|
|
151
|
+
if (agent.busy) agent.interrupt();
|
|
152
|
+
};
|
|
153
|
+
ui.statusLine = () => {
|
|
154
|
+
const used = Math.round((estimateTokens(agent.session.messages) / config.contextWindow) * 100);
|
|
155
|
+
const parts = [config.model, `${used}% context`];
|
|
156
|
+
if (agent.permissions.planMode) parts.push('plan mode');
|
|
157
|
+
if (agent.permissions.mode === 'yolo') parts.push('yolo');
|
|
158
|
+
parts.push('/help');
|
|
159
|
+
return parts.join(' · ');
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
ui.banner({
|
|
163
|
+
version: VERSION,
|
|
164
|
+
model: config.model,
|
|
165
|
+
provider: config.providerName || config.baseUrl,
|
|
166
|
+
cwd,
|
|
167
|
+
instructions: loadProjectInstructions(cwd).filter((i) => i.text.trim()).map((i) => i.file),
|
|
168
|
+
resumed: agent.session.messages.length ? `resumed ${agent.session.id} (${agent.session.messages.length} messages)` : '',
|
|
169
|
+
});
|
|
120
170
|
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
ui.write(ui.out, c.dim(' /help for commands · Ctrl-C to interrupt · Ctrl-D to quit\n\n'));
|
|
171
|
+
const send = async (text) => {
|
|
172
|
+
const { text: full, attached } = expandMentions(text, cwd);
|
|
173
|
+
if (attached.length) ui.info(` Attached ${attached.join(', ')}`);
|
|
174
|
+
await agent.send(full);
|
|
175
|
+
};
|
|
127
176
|
|
|
128
|
-
if (firstPrompt) await
|
|
177
|
+
if (firstPrompt) await send(firstPrompt);
|
|
129
178
|
while (true) {
|
|
130
179
|
const line = await ui.prompt();
|
|
131
180
|
if (line === null) break;
|
|
132
181
|
armedExit = false;
|
|
133
182
|
const text = line.trim();
|
|
134
183
|
if (!text) continue;
|
|
135
|
-
if (text.startsWith('/')) {
|
|
136
|
-
|
|
184
|
+
if (text.startsWith('/') && !text.startsWith('//')) {
|
|
185
|
+
const result = await slashCommand(text, { agent, ui, prompts, config, cwd });
|
|
186
|
+
if (result === 'exit') break;
|
|
187
|
+
if (result?.send) await send(result.send);
|
|
137
188
|
continue;
|
|
138
189
|
}
|
|
139
|
-
await
|
|
140
|
-
ui.write(ui.out, '\n');
|
|
190
|
+
await send(text);
|
|
141
191
|
}
|
|
142
192
|
ui.close();
|
|
143
|
-
ui.write(ui.out, c.
|
|
193
|
+
ui.write(ui.out, c.gray(`\nSession saved. Resume with: arrakis -r ${agent.session.id}\n`));
|
|
144
194
|
}
|
|
145
195
|
|
|
146
|
-
async function slashCommand(text, { agent, ui, config, cwd }) {
|
|
196
|
+
async function slashCommand(text, { agent, ui, prompts, config, cwd }) {
|
|
147
197
|
const [cmd, ...rest] = text.slice(1).split(/\s+/);
|
|
148
198
|
const arg = rest.join(' ');
|
|
149
199
|
const c = ui.c;
|
|
150
200
|
switch (cmd) {
|
|
151
|
-
case 'help':
|
|
201
|
+
case 'help': {
|
|
152
202
|
ui.write(ui.out, SLASH_HELP + '\n');
|
|
203
|
+
const custom = loadCustomCommands(cwd);
|
|
204
|
+
if (custom.size) {
|
|
205
|
+
ui.write(ui.out, `\n${c.bold('Your commands')}\n`);
|
|
206
|
+
for (const cmd of custom.values()) ui.write(ui.out, `/${cmd.name.padEnd(17)} ${c.gray(cmd.description)}\n`);
|
|
207
|
+
}
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
case 'plan':
|
|
211
|
+
agent.permissions.planMode = !agent.permissions.planMode;
|
|
212
|
+
if (agent.permissions.planMode) ui.info('Plan mode on: arrakis will investigate and propose a plan without changing anything. /plan again to approve and let it work.');
|
|
213
|
+
else {
|
|
214
|
+
ui.info('Plan mode off: arrakis can make changes now.');
|
|
215
|
+
if (agent.session.messages.length) return { send: 'Plan mode is off and the plan is approved. Go ahead and carry it out.' };
|
|
216
|
+
}
|
|
217
|
+
break;
|
|
218
|
+
case 'init':
|
|
219
|
+
return { send: INIT_PROMPT };
|
|
220
|
+
case 'todos':
|
|
221
|
+
if (agent.todos.length) ui.todos(agent.todos);
|
|
222
|
+
else ui.info('No task list yet. arrakis makes one for multi-step work.');
|
|
153
223
|
break;
|
|
154
224
|
case 'exit':
|
|
155
225
|
case 'quit':
|
|
@@ -171,12 +241,32 @@ async function slashCommand(text, { agent, ui, config, cwd }) {
|
|
|
171
241
|
}
|
|
172
242
|
break;
|
|
173
243
|
}
|
|
174
|
-
case 'model':
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
244
|
+
case 'model': {
|
|
245
|
+
let chosen = arg ? { id: arg } : null;
|
|
246
|
+
if (!arg) {
|
|
247
|
+
let models = [];
|
|
248
|
+
try {
|
|
249
|
+
models = await listModels(config);
|
|
250
|
+
} catch (err) {
|
|
251
|
+
ui.error(`Couldn't list models: ${err.message}`);
|
|
252
|
+
}
|
|
253
|
+
chosen = await pickModel({ ui, prompts, provider: getProvider(config.provider), models, current: config.model });
|
|
254
|
+
}
|
|
255
|
+
if (!chosen) break;
|
|
256
|
+
config.model = chosen.id;
|
|
257
|
+
if (chosen.contextWindow) config.contextWindow = chosen.contextWindow;
|
|
258
|
+
if (config.provider) saveProvider(config.provider, { model: chosen.id, contextWindow: chosen.contextWindow || undefined });
|
|
259
|
+
ui.info(`Now using ${chosen.id}.`);
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
case 'provider':
|
|
263
|
+
case 'setup':
|
|
264
|
+
case 'login': {
|
|
265
|
+
if (agent.session.messages.length) ui.info('Your conversation carries over to the new provider.');
|
|
266
|
+
const next = await runSetup({ ui, prompts });
|
|
267
|
+
if (next) Object.assign(config, next);
|
|
179
268
|
break;
|
|
269
|
+
}
|
|
180
270
|
case 'sessions':
|
|
181
271
|
printSessions(ui.out, 10);
|
|
182
272
|
break;
|
|
@@ -185,8 +275,11 @@ async function slashCommand(text, { agent, ui, config, cwd }) {
|
|
|
185
275
|
ui.write(ui.out, [
|
|
186
276
|
`session ${agent.session.id}`,
|
|
187
277
|
`log ${agent.session.file}`,
|
|
188
|
-
`
|
|
278
|
+
`provider ${config.providerName || 'custom'} (${config.baseUrl})`,
|
|
279
|
+
`model ${config.model}`,
|
|
189
280
|
`context ~${used.toLocaleString()} / ${config.contextWindow.toLocaleString()} tokens (${Math.round((used / config.contextWindow) * 100)}%)`,
|
|
281
|
+
`plan mode ${agent.permissions.planMode ? 'on' : 'off'}`,
|
|
282
|
+
`todos ${agent.todos.length ? `${agent.todos.filter((t) => t.status === 'completed').length}/${agent.todos.length} done` : 'none'}`,
|
|
190
283
|
`permissions ${agent.permissions.mode}${agent.permissions.allowed.size ? ` (always allowed: ${[...agent.permissions.allowed].join(', ')})` : ''}`,
|
|
191
284
|
`tools ${TOOLS.map((t) => t.name).join(', ')}`,
|
|
192
285
|
].join('\n') + '\n');
|
|
@@ -196,8 +289,11 @@ async function slashCommand(text, { agent, ui, config, cwd }) {
|
|
|
196
289
|
agent.permissions.mode = agent.permissions.mode === 'yolo' ? 'ask' : 'yolo';
|
|
197
290
|
ui.info(agent.permissions.mode === 'yolo' ? 'YOLO on: tool calls run without asking.' : 'YOLO off: writes and commands ask first.');
|
|
198
291
|
break;
|
|
199
|
-
default:
|
|
292
|
+
default: {
|
|
293
|
+
const custom = loadCustomCommands(cwd).get(cmd.toLowerCase());
|
|
294
|
+
if (custom) return { send: renderCommand(custom, arg) };
|
|
200
295
|
ui.write(ui.out, c.red(`Unknown command /${cmd}. Try /help.\n`));
|
|
296
|
+
}
|
|
201
297
|
}
|
|
202
298
|
}
|
|
203
299
|
|
|
@@ -218,13 +314,37 @@ function configCommand(args) {
|
|
|
218
314
|
const config = loadConfig();
|
|
219
315
|
const file = readConfigFile();
|
|
220
316
|
console.log(`Config file: ${CONFIG_PATH}\n`);
|
|
317
|
+
if (!config.configured) console.log('Not set up yet. Run: arrakis setup\n');
|
|
318
|
+
const providerFile = (config.provider && file.providers[config.provider]) || {};
|
|
221
319
|
for (const [k, spec] of Object.entries(CONFIG_KEYS)) {
|
|
222
|
-
const
|
|
223
|
-
const
|
|
224
|
-
|
|
320
|
+
const inFile = k === 'provider' ? file.provider !== undefined : spec.global ? file[k] !== undefined : providerFile[k] !== undefined;
|
|
321
|
+
const source = process.env[spec.env] ? `env ${spec.env}` : inFile ? 'config file' : 'default';
|
|
322
|
+
let value = spec.secret ? maskSecret(config[k]) : config[k] || '(not set)';
|
|
323
|
+
if (k === 'provider' && config.providerName) value = `${config.provider} (${config.providerName})`;
|
|
324
|
+
console.log(`${k.padEnd(14)} ${String(value).padEnd(40)} ${source}`);
|
|
325
|
+
}
|
|
326
|
+
const others = Object.keys(file.providers).filter((id) => id !== config.provider);
|
|
327
|
+
if (others.length) console.log(`\nOther saved providers: ${others.join(', ')}`);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
async function setupCommand() {
|
|
331
|
+
const ui = new UI();
|
|
332
|
+
ui.start();
|
|
333
|
+
try {
|
|
334
|
+
await runSetup({ ui, prompts: createPrompts(ui), welcome: !loadConfig().configured });
|
|
335
|
+
} finally {
|
|
336
|
+
ui.close();
|
|
225
337
|
}
|
|
226
338
|
}
|
|
227
339
|
|
|
340
|
+
async function modelsCommand() {
|
|
341
|
+
const config = loadConfig();
|
|
342
|
+
if (!config.configured) throw new Error('Not set up yet. Run: arrakis setup');
|
|
343
|
+
const models = await listModels(config);
|
|
344
|
+
if (!models.length) return console.log(`${config.providerName || config.baseUrl} listed no models.`);
|
|
345
|
+
for (const m of models) console.log(`${m.id === config.model ? '*' : ' '} ${m.id}${m.contextWindow ? ` (${m.contextWindow.toLocaleString()} tokens)` : ''}`);
|
|
346
|
+
}
|
|
347
|
+
|
|
228
348
|
function sessionsCommand() {
|
|
229
349
|
printSessions(process.stdout, 20);
|
|
230
350
|
}
|