@mumbrane/arrakis 0.6.1 → 0.8.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 +15 -0
- package/package.json +5 -2
- package/src/agent.js +95 -32
- package/src/anthropic.js +186 -0
- package/src/cli.js +95 -8
- package/src/client.js +6 -0
- package/src/config.js +3 -0
- package/src/markdown.js +48 -29
- package/src/pricing.js +59 -0
- package/src/prompt.js +1 -5
- package/src/providers.js +0 -1
- package/src/theme.js +153 -0
- package/src/tools.js +21 -0
- package/src/tui.js +183 -90
- package/src/ui.js +51 -64
package/src/markdown.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// Streaming Markdown renderer for the terminal
|
|
2
|
-
// complete line is rendered as soon as its newline
|
|
3
|
-
// until they end so their columns can be aligned.
|
|
1
|
+
// Streaming Markdown renderer for the terminal, in the Dusk style. Text
|
|
2
|
+
// arrives in chunks; each complete line is rendered as soon as its newline
|
|
3
|
+
// arrives. Tables are held until they end so their columns can be aligned.
|
|
4
4
|
|
|
5
5
|
const ANSI = /\x1b\[[0-9;]*m/g;
|
|
6
6
|
export const visibleLength = (s) => s.replace(ANSI, '').length;
|
|
@@ -13,13 +13,14 @@ const KEYWORDS = new Set(
|
|
|
13
13
|
);
|
|
14
14
|
|
|
15
15
|
export function highlight(line, c) {
|
|
16
|
-
const token = /(\/\/.*$|#(?![!{\[]).*$|--\s.*$)|("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|`(?:[^`\\]|\\.)*`)|(\b\d+(?:\.\d+)?\b)|([A-Za-z_][A-Za-z0-9_]*)/g;
|
|
17
|
-
return line.replace(token, (m, comment, str, num, word) => {
|
|
18
|
-
if (comment) return c.
|
|
19
|
-
if (str) return c.
|
|
20
|
-
if (num) return c.
|
|
21
|
-
if (
|
|
22
|
-
return
|
|
16
|
+
const token = /(\/\/.*$|#(?![!{\[]).*$|--\s.*$)|("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|`(?:[^`\\]|\\.)*`)|(\b\d+(?:\.\d+)?\b)|([A-Za-z_][A-Za-z0-9_]*)(?=\s*\()|([A-Za-z_][A-Za-z0-9_]*)/g;
|
|
17
|
+
return line.replace(token, (m, comment, str, num, call, word) => {
|
|
18
|
+
if (comment) return c.faint(c.italic(comment));
|
|
19
|
+
if (str) return c.mint(str);
|
|
20
|
+
if (num) return c.amber(num);
|
|
21
|
+
if (call) return KEYWORDS.has(call) ? c.orchid(call) : c.sky(call);
|
|
22
|
+
if (word && KEYWORDS.has(word)) return c.orchid(word);
|
|
23
|
+
return c.lilac(m);
|
|
23
24
|
});
|
|
24
25
|
}
|
|
25
26
|
|
|
@@ -28,13 +29,13 @@ export function renderInline(s, c) {
|
|
|
28
29
|
return s
|
|
29
30
|
.split(/(`[^`]+`)/)
|
|
30
31
|
.map((part) => {
|
|
31
|
-
if (/^`[^`]+`$/.test(part)) return c.
|
|
32
|
+
if (/^`[^`]+`$/.test(part)) return c.panelBg(c.sky(` ${part.slice(1, -1)} `));
|
|
32
33
|
return part
|
|
33
|
-
.replace(/\*\*([^*]+)\*\*|__([^_]+)__/g, (_, a, b) => c.bold(a ?? b))
|
|
34
|
+
.replace(/\*\*([^*]+)\*\*|__([^_]+)__/g, (_, a, b) => c.bold(c.lilac(a ?? b)))
|
|
34
35
|
.replace(/(^|[\s(])\*([^*\s][^*]*)\*(?=$|[\s).,:;!?])/g, (_, pre, a) => pre + c.italic(a))
|
|
35
36
|
.replace(/(^|[\s(])_([^_\s][^_]*)_(?=$|[\s).,:;!?])/g, (_, pre, a) => pre + c.italic(a))
|
|
36
|
-
.replace(/~~([^~]+)~~/g, (_, a) => c.
|
|
37
|
-
.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (_, text, url) => (text === url ? c.underline(url) : `${c.underline(text)} ${c.
|
|
37
|
+
.replace(/~~([^~]+)~~/g, (_, a) => c.strike(c.mist(a)))
|
|
38
|
+
.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (_, text, url) => (text === url ? c.underline(c.sky(url)) : `${c.underline(c.sky(text))} ${c.faint(url)}`));
|
|
38
39
|
})
|
|
39
40
|
.join('');
|
|
40
41
|
}
|
|
@@ -46,6 +47,7 @@ export class MarkdownStream {
|
|
|
46
47
|
this.buf = '';
|
|
47
48
|
this.inCode = false;
|
|
48
49
|
this.fence = '';
|
|
50
|
+
this.lang = '';
|
|
49
51
|
this.table = [];
|
|
50
52
|
}
|
|
51
53
|
|
|
@@ -68,12 +70,21 @@ export class MarkdownStream {
|
|
|
68
70
|
}
|
|
69
71
|
out += this.flushTable();
|
|
70
72
|
if (this.inCode) {
|
|
71
|
-
out += this.
|
|
73
|
+
out += this.panel('') + '\n';
|
|
72
74
|
this.inCode = false;
|
|
73
75
|
}
|
|
74
76
|
return out;
|
|
75
77
|
}
|
|
76
78
|
|
|
79
|
+
// One row of a code panel: plum background across the panel width.
|
|
80
|
+
panel(content) {
|
|
81
|
+
const c = this.c;
|
|
82
|
+
const w = Math.max(20, Math.min(this.width - 2, 110));
|
|
83
|
+
const vis = visibleLength(content);
|
|
84
|
+
const body = vis > w - 2 ? content : content + ' '.repeat(w - 2 - vis);
|
|
85
|
+
return c.panelBg(` ${body} `);
|
|
86
|
+
}
|
|
87
|
+
|
|
77
88
|
line(raw) {
|
|
78
89
|
const c = this.c;
|
|
79
90
|
const fence = raw.match(/^\s*(```+|~~~+)\s*([\w+#.-]*)/);
|
|
@@ -82,12 +93,15 @@ export class MarkdownStream {
|
|
|
82
93
|
if (!this.inCode) {
|
|
83
94
|
this.inCode = true;
|
|
84
95
|
this.fence = fence[1];
|
|
85
|
-
|
|
96
|
+
this.lang = fence[2] || '';
|
|
97
|
+
// Top edge of the panel carries the language as a small tag.
|
|
98
|
+
const tag = this.lang ? c.violet(this.lang) : c.faint('code');
|
|
99
|
+
return out + this.panel(tag) + '\n';
|
|
86
100
|
}
|
|
87
101
|
this.inCode = false;
|
|
88
|
-
return out +
|
|
102
|
+
return out + this.panel('') + '\n';
|
|
89
103
|
}
|
|
90
|
-
if (this.inCode) return
|
|
104
|
+
if (this.inCode) return this.panel(highlight(raw.replace(/\t/g, ' '), c)) + '\n';
|
|
91
105
|
|
|
92
106
|
if (/^\s*\|.*\|\s*$/.test(raw)) {
|
|
93
107
|
this.table.push(raw);
|
|
@@ -97,17 +111,22 @@ export class MarkdownStream {
|
|
|
97
111
|
|
|
98
112
|
let m;
|
|
99
113
|
if ((m = raw.match(/^(#{1,6})\s+(.*)$/))) {
|
|
100
|
-
const text =
|
|
101
|
-
|
|
114
|
+
const text = m[2].replace(/\s+#+\s*$/, '');
|
|
115
|
+
if (m[1].length === 1) return out + c.bold(c.gradient(text)) + '\n';
|
|
116
|
+
if (m[1].length === 2) return out + c.bold(c.violet(text)) + '\n';
|
|
117
|
+
return out + c.bold(c.lilac(renderInline(text, c))) + '\n';
|
|
102
118
|
}
|
|
103
|
-
if (/^\s*([-*_])(\s*\1){2,}\s*$/.test(raw)) return out + c.
|
|
104
|
-
if ((m = raw.match(/^\s*>\s?(.*)$/))) return out + `${c.
|
|
119
|
+
if (/^\s*([-*_])(\s*\1){2,}\s*$/.test(raw)) return out + c.faint('─'.repeat(Math.min(this.width, 56))) + '\n';
|
|
120
|
+
if ((m = raw.match(/^\s*>\s?(.*)$/))) return out + `${c.iris('┃')} ${c.italic(c.lilac(renderInline(m[1], c)))}\n`;
|
|
105
121
|
if ((m = raw.match(/^(\s*)[-*+]\s+\[( |x|X)\]\s+(.*)$/))) {
|
|
106
122
|
const done = m[2].toLowerCase() === 'x';
|
|
107
|
-
return out + `${m[1]}${done ? c.
|
|
123
|
+
return out + `${m[1]}${done ? c.mint('●') : c.mist('○')} ${done ? c.mist(c.strike(renderInline(m[3], c))) : renderInline(m[3], c)}\n`;
|
|
124
|
+
}
|
|
125
|
+
if ((m = raw.match(/^(\s*)[-*+]\s+(.*)$/))) {
|
|
126
|
+
const nested = m[1].length >= 2;
|
|
127
|
+
return out + `${m[1]}${nested ? c.iris('◦') : c.violet('•')} ${renderInline(m[2], c)}\n`;
|
|
108
128
|
}
|
|
109
|
-
if ((m = raw.match(/^(\s*)[
|
|
110
|
-
if ((m = raw.match(/^(\s*)(\d+)[.)]\s+(.*)$/))) return out + `${m[1]}${c.accent(m[2] + '.')} ${renderInline(m[3], c)}\n`;
|
|
129
|
+
if ((m = raw.match(/^(\s*)(\d+)[.)]\s+(.*)$/))) return out + `${m[1]}${c.violet(m[2] + '.')} ${renderInline(m[3], c)}\n`;
|
|
111
130
|
return out + renderInline(raw, c) + '\n';
|
|
112
131
|
}
|
|
113
132
|
|
|
@@ -122,13 +141,13 @@ export class MarkdownStream {
|
|
|
122
141
|
const cols = Math.max(...rows.map((r) => r.length));
|
|
123
142
|
const widths = Array.from({ length: cols }, (_, i) => Math.max(3, ...rows.map((r) => visibleLength(r[i] ?? ''))));
|
|
124
143
|
const pad = (s, w) => s + ' '.repeat(Math.max(0, w - visibleLength(s)));
|
|
125
|
-
const rule = (l, mid, r) => c.
|
|
126
|
-
const lines = [rule('
|
|
144
|
+
const rule = (l, mid, r) => c.faint(l + widths.map((w) => '─'.repeat(w + 2)).join(mid) + r);
|
|
145
|
+
const lines = [rule('╭', '┬', '╮')];
|
|
127
146
|
rows.forEach((r, i) => {
|
|
128
|
-
lines.push(c.
|
|
147
|
+
lines.push(c.faint('│') + widths.map((w, j) => ` ${i === 0 ? c.bold(c.violet(pad(r[j] ?? '', w))) : pad(r[j] ?? '', w)} `).join(c.faint('│')) + c.faint('│'));
|
|
129
148
|
if (i === 0 && rows.length > 1) lines.push(rule('├', '┼', '┤'));
|
|
130
149
|
});
|
|
131
|
-
lines.push(rule('
|
|
150
|
+
lines.push(rule('╰', '┴', '╯'));
|
|
132
151
|
return lines.join('\n') + '\n';
|
|
133
152
|
}
|
|
134
153
|
}
|
package/src/pricing.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Approximate list prices in USD per million tokens, for the session cost
|
|
2
|
+
// estimate. [input, output, cache read, cache write (5 min)]
|
|
3
|
+
const PRICES = [
|
|
4
|
+
[/claude-(fable|mythos)-5-2/, [8, 40, 0.2, 10]],
|
|
5
|
+
[/claude-(fable|mythos)-5(-1)?\b/, [10, 50, 0.25, 12.5]],
|
|
6
|
+
[/claude-opus-5-5/, [4, 20, 0.2, 5]],
|
|
7
|
+
[/claude-opus-(5|4-[5-9])\b/, [5, 25, 0.5, 6.25]],
|
|
8
|
+
[/claude-sonnet-5-5/, [2, 10, 0.1, 2.5]],
|
|
9
|
+
[/claude-sonnet-5\b/, [2, 10, 0.2, 2.5]],
|
|
10
|
+
[/claude-sonnet-4/, [3, 15, 0.3, 3.75]],
|
|
11
|
+
[/claude-haiku-5-5/, [0.1, 0.5, 0.01, 0.125]],
|
|
12
|
+
[/claude-haiku-4-5/, [1, 5, 0.1, 1.25]],
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
export function priceFor(model) {
|
|
16
|
+
return PRICES.find(([re]) => re.test(model))?.[1] ?? null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class Usage {
|
|
20
|
+
constructor() {
|
|
21
|
+
this.input = 0;
|
|
22
|
+
this.output = 0;
|
|
23
|
+
this.cacheRead = 0;
|
|
24
|
+
this.cacheWrite = 0;
|
|
25
|
+
this.requests = 0;
|
|
26
|
+
this.cost = 0;
|
|
27
|
+
this.priced = true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Accepts Anthropic usage ({input_tokens, output_tokens, cache_*}) or
|
|
31
|
+
// OpenAI usage ({prompt_tokens, completion_tokens}).
|
|
32
|
+
add(model, u) {
|
|
33
|
+
if (!u) return;
|
|
34
|
+
const read = u.cache_read_input_tokens ?? u.prompt_tokens_details?.cached_tokens ?? 0;
|
|
35
|
+
const input = u.input_tokens ?? Math.max(0, (u.prompt_tokens ?? 0) - (u.prompt_tokens_details?.cached_tokens ?? 0));
|
|
36
|
+
const output = u.output_tokens ?? u.completion_tokens ?? 0;
|
|
37
|
+
const write = u.cache_creation_input_tokens ?? 0;
|
|
38
|
+
this.input += input;
|
|
39
|
+
this.output += output;
|
|
40
|
+
this.cacheRead += read;
|
|
41
|
+
this.cacheWrite += write;
|
|
42
|
+
this.requests++;
|
|
43
|
+
const p = priceFor(model);
|
|
44
|
+
if (p) this.cost += (input * p[0] + output * p[1] + read * p[2] + write * p[3]) / 1e6;
|
|
45
|
+
else this.priced = false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get total() {
|
|
49
|
+
return this.input + this.output + this.cacheRead + this.cacheWrite;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get cacheHitRate() {
|
|
53
|
+
const prompt = this.input + this.cacheRead + this.cacheWrite;
|
|
54
|
+
return prompt ? this.cacheRead / prompt : 0;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const formatCost = (usd) => (usd < 0.01 ? '<$0.01' : `$${usd.toFixed(2)}`);
|
|
59
|
+
export const formatTokens = (n) => (n >= 1e6 ? `${(n / 1e6).toFixed(2)}M` : n >= 1000 ? `${(n / 1000).toFixed(1)}k` : String(n));
|
package/src/prompt.js
CHANGED
|
@@ -46,7 +46,7 @@ export function loadProjectInstructions(cwd) {
|
|
|
46
46
|
});
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
export function buildSystemPrompt({ cwd, model,
|
|
49
|
+
export function buildSystemPrompt({ cwd, model, subagent = null }) {
|
|
50
50
|
const git = findUp(cwd, '.git');
|
|
51
51
|
const instructions = loadProjectInstructions(cwd).filter((i) => i.text.trim());
|
|
52
52
|
let prompt = `You are Arrakis, a coding agent built by Mumbrane that runs in the user's terminal. You help with software engineering: reading and changing code, running commands, debugging, and explaining.
|
|
@@ -80,10 +80,6 @@ export function buildSystemPrompt({ cwd, model, planMode = false, subagent = nul
|
|
|
80
80
|
- Reference code as path:line.
|
|
81
81
|
- When done, say briefly what changed and anything left unverified.`;
|
|
82
82
|
|
|
83
|
-
if (planMode) {
|
|
84
|
-
prompt += `\n\n# Plan mode is ON
|
|
85
|
-
The user wants a plan before any changes. Investigate with read-only tools (read_file, grep, list_files, web_fetch, sub-agents of type explore). Do not edit files or run commands; those tools are blocked. When you understand the problem, present a concise, concrete plan: the files to change, what changes, and how you'll verify it. Then stop and wait. The user approves by turning plan mode off with /plan.`;
|
|
86
|
-
}
|
|
87
83
|
if (subagent) {
|
|
88
84
|
prompt += `\n\n# You are a sub-agent
|
|
89
85
|
The main agent started you to do one job. You can't see its conversation and you can't ask the user anything, so work autonomously with what's in your instructions.
|
package/src/providers.js
CHANGED
|
@@ -20,7 +20,6 @@ export const PROVIDERS = [
|
|
|
20
20
|
keyUrl: 'https://platform.claude.com/settings/keys',
|
|
21
21
|
defaultModel: 'claude-opus-5-5',
|
|
22
22
|
contextWindow: 1000000,
|
|
23
|
-
maxTokens: 32000,
|
|
24
23
|
modelsHeaders: (key) => ({ 'x-api-key': key, 'anthropic-version': '2023-06-01' }),
|
|
25
24
|
modelsQuery: '?limit=1000',
|
|
26
25
|
},
|
package/src/theme.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// "Dusk": arrakis's design language.
|
|
2
|
+
//
|
|
3
|
+
// A twilight palette: lavender as the primary, deep iris and orchid as
|
|
4
|
+
// accents, lilac for soft text, plum panels behind code. Mint, amber and rose
|
|
5
|
+
// carry meaning (success, caution, error) and are used sparingly.
|
|
6
|
+
//
|
|
7
|
+
// Colors render in 24-bit where the terminal supports it and fall back to the
|
|
8
|
+
// nearest 256-color value elsewhere (e.g. macOS Terminal.app). NO_COLOR turns
|
|
9
|
+
// everything off.
|
|
10
|
+
|
|
11
|
+
export const PALETTE = {
|
|
12
|
+
violet: [167, 139, 250], // primary
|
|
13
|
+
iris: [124, 92, 246], // deep accent
|
|
14
|
+
orchid: [226, 125, 245], // hot accent
|
|
15
|
+
lilac: [221, 214, 254], // soft text
|
|
16
|
+
mist: [140, 132, 166], // muted text
|
|
17
|
+
dusk: [84, 76, 110], // faint lines
|
|
18
|
+
mint: [110, 231, 183],
|
|
19
|
+
amber: [246, 196, 92],
|
|
20
|
+
rose: [251, 113, 150],
|
|
21
|
+
sky: [134, 200, 252],
|
|
22
|
+
ink: [20, 17, 31], // text on light chips
|
|
23
|
+
panel: [32, 27, 48], // code panels
|
|
24
|
+
raise: [46, 38, 70], // selected rows, user messages
|
|
25
|
+
addBg: [22, 52, 44],
|
|
26
|
+
delBg: [62, 26, 44],
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const env = process.env;
|
|
30
|
+
export const TRUECOLOR =
|
|
31
|
+
/truecolor|24bit/i.test(env.COLORTERM ?? '') ||
|
|
32
|
+
/iTerm|WezTerm|vscode|ghostty|Hyper|Tabby|WarpTerminal|rio/i.test(env.TERM_PROGRAM ?? '') ||
|
|
33
|
+
Boolean(env.WT_SESSION || env.KITTY_WINDOW_ID || env.ALACRITTY_LOG);
|
|
34
|
+
|
|
35
|
+
function to256([r, g, b]) {
|
|
36
|
+
// Grays get the 24-step gray ramp, colors the 6x6x6 cube.
|
|
37
|
+
if (Math.abs(r - g) < 10 && Math.abs(g - b) < 10) {
|
|
38
|
+
if (r < 8) return 16;
|
|
39
|
+
if (r > 248) return 231;
|
|
40
|
+
return 232 + Math.round(((r - 8) / 247) * 23);
|
|
41
|
+
}
|
|
42
|
+
const q = (v) => (v < 48 ? 0 : v < 115 ? 1 : Math.round((v - 35) / 40));
|
|
43
|
+
return 16 + 36 * q(r) + 6 * q(g) + q(b);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const fgCode = (rgb) => (TRUECOLOR ? `38;2;${rgb[0]};${rgb[1]};${rgb[2]}` : `38;5;${to256(rgb)}`);
|
|
47
|
+
const bgCode = (rgb) => (TRUECOLOR ? `48;2;${rgb[0]};${rgb[1]};${rgb[2]}` : `48;5;${to256(rgb)}`);
|
|
48
|
+
|
|
49
|
+
// Each style closes with its own reset code, so styles nest (e.g. colored
|
|
50
|
+
// text inside a background panel keeps the panel).
|
|
51
|
+
const wrap = (open, close, on) => (s) => (on ? `\x1b[${open}m${s}\x1b[${close}m` : String(s));
|
|
52
|
+
|
|
53
|
+
const mix = (a, b, t) => a.map((v, i) => Math.round(v + (b[i] - v) * t));
|
|
54
|
+
|
|
55
|
+
export function makeTheme(on) {
|
|
56
|
+
const fg = (rgb) => wrap(fgCode(rgb), '39', on);
|
|
57
|
+
const bg = (rgb) => wrap(bgCode(rgb), '49', on);
|
|
58
|
+
const P = PALETTE;
|
|
59
|
+
const t = {
|
|
60
|
+
on,
|
|
61
|
+
bold: wrap('1', '22', on),
|
|
62
|
+
dim: wrap('2', '22', on),
|
|
63
|
+
italic: wrap('3', '23', on),
|
|
64
|
+
underline: wrap('4', '24', on),
|
|
65
|
+
strike: wrap('9', '29', on),
|
|
66
|
+
inverse: wrap('7', '27', on),
|
|
67
|
+
fg,
|
|
68
|
+
bg,
|
|
69
|
+
violet: fg(P.violet),
|
|
70
|
+
iris: fg(P.iris),
|
|
71
|
+
orchid: fg(P.orchid),
|
|
72
|
+
lilac: fg(P.lilac),
|
|
73
|
+
mist: fg(P.mist),
|
|
74
|
+
faint: fg(P.dusk),
|
|
75
|
+
mint: fg(P.mint),
|
|
76
|
+
amber: fg(P.amber),
|
|
77
|
+
rose: fg(P.rose),
|
|
78
|
+
sky: fg(P.sky),
|
|
79
|
+
ink: fg(P.ink),
|
|
80
|
+
panelBg: bg(P.panel),
|
|
81
|
+
raiseBg: bg(P.raise),
|
|
82
|
+
addBg: bg(P.addBg),
|
|
83
|
+
delBg: bg(P.delBg),
|
|
84
|
+
violetBg: bg(P.violet),
|
|
85
|
+
|
|
86
|
+
// Color each character along a gradient of stops. `phase` (0-1) shifts it.
|
|
87
|
+
gradient(text, stops = [P.iris, P.violet, P.orchid], phase = 0) {
|
|
88
|
+
if (!on) return String(text);
|
|
89
|
+
const chars = [...String(text)];
|
|
90
|
+
const n = Math.max(1, chars.length - 1);
|
|
91
|
+
return (
|
|
92
|
+
chars
|
|
93
|
+
.map((ch, i) => {
|
|
94
|
+
if (ch === ' ') return ch;
|
|
95
|
+
let x = (i / n + phase) % 1;
|
|
96
|
+
if (phase) x = x < 0.5 ? x * 2 : 2 - x * 2; // ping-pong so it loops smoothly
|
|
97
|
+
const seg = x * (stops.length - 1);
|
|
98
|
+
const k = Math.min(stops.length - 2, Math.floor(seg));
|
|
99
|
+
return `\x1b[${fgCode(mix(stops[k], stops[k + 1], seg - k))}m${ch}`;
|
|
100
|
+
})
|
|
101
|
+
.join('') + '\x1b[39m'
|
|
102
|
+
);
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
// A bright band sweeping across muted text (for the working indicator).
|
|
106
|
+
shimmer(text, frame) {
|
|
107
|
+
if (!on) return String(text);
|
|
108
|
+
const chars = [...String(text)];
|
|
109
|
+
const pos = (frame % (chars.length + 8)) - 4;
|
|
110
|
+
return (
|
|
111
|
+
chars
|
|
112
|
+
.map((ch, i) => {
|
|
113
|
+
const d = Math.abs(i - pos);
|
|
114
|
+
const c = d < 1 ? P.lilac : d < 2.5 ? mix(P.lilac, P.violet, 0.5) : d < 4 ? P.violet : mix(P.violet, P.mist, 0.55);
|
|
115
|
+
return `\x1b[${fgCode(c)}m${ch}`;
|
|
116
|
+
})
|
|
117
|
+
.join('') + '\x1b[39m'
|
|
118
|
+
);
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
// Names the rest of the code already uses, mapped onto the palette.
|
|
122
|
+
Object.assign(t, {
|
|
123
|
+
accent: t.violet,
|
|
124
|
+
sand: t.lilac,
|
|
125
|
+
gray: t.mist,
|
|
126
|
+
green: t.mint,
|
|
127
|
+
red: t.rose,
|
|
128
|
+
yellow: t.amber,
|
|
129
|
+
magenta: t.orchid,
|
|
130
|
+
cyan: t.sky,
|
|
131
|
+
blue: t.violet,
|
|
132
|
+
userBg: t.raiseBg,
|
|
133
|
+
});
|
|
134
|
+
return t;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Glyphs of the design language, in one place.
|
|
138
|
+
export const GLYPH = {
|
|
139
|
+
reply: '◆',
|
|
140
|
+
tool: '◇',
|
|
141
|
+
result: '╰',
|
|
142
|
+
user: '▌',
|
|
143
|
+
thinking: '✧',
|
|
144
|
+
prompt: '❯',
|
|
145
|
+
ok: '✓',
|
|
146
|
+
fail: '✕',
|
|
147
|
+
info: '·',
|
|
148
|
+
todo: { pending: '○', in_progress: '◉', completed: '●' },
|
|
149
|
+
spinner: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'],
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// "ARRAKIS" in a two-row block font.
|
|
153
|
+
export const LOGO = ['▄▀█ █▀█ █▀█ ▄▀█ █▄▀ █ █▀', '█▀█ █▀▄ █▀▄ █▀█ █ █ █ ▄█'];
|
package/src/tools.js
CHANGED
|
@@ -514,6 +514,9 @@ export async function executeToolCall(call, ctx) {
|
|
|
514
514
|
} catch {
|
|
515
515
|
return `Error: tool arguments were not valid JSON: ${String(call.function.arguments).slice(0, 200)}`;
|
|
516
516
|
}
|
|
517
|
+
// Tool input may arrive malformed or cut short; check it against the schema first.
|
|
518
|
+
const problem = checkArgs(tool.parameters, args);
|
|
519
|
+
if (problem) return `Error: invalid input for ${tool.name}: ${problem}. Fix the arguments and call the tool again.`;
|
|
517
520
|
const sub = (ctx.depth ?? 0) > 0;
|
|
518
521
|
ctx.ui.toolStart(tool.label ?? tool.name, safe(() => tool.summary?.(args)) ?? '', { prefix: sub ? `${ctx.label}` : '' });
|
|
519
522
|
try {
|
|
@@ -553,6 +556,24 @@ export async function executeToolCall(call, ctx) {
|
|
|
553
556
|
}
|
|
554
557
|
}
|
|
555
558
|
|
|
559
|
+
function checkArgs(schema, args) {
|
|
560
|
+
if (!args || typeof args !== 'object' || Array.isArray(args)) return 'arguments must be a JSON object';
|
|
561
|
+
for (const key of schema?.required ?? []) if (args[key] === undefined || args[key] === null) return `missing required "${key}"`;
|
|
562
|
+
for (const [key, spec] of Object.entries(schema?.properties ?? {})) {
|
|
563
|
+
const v = args[key];
|
|
564
|
+
if (v === undefined || v === null || !spec?.type) continue;
|
|
565
|
+
const ok = spec.type === 'string' ? typeof v === 'string'
|
|
566
|
+
: spec.type === 'integer' ? Number.isInteger(v)
|
|
567
|
+
: spec.type === 'number' ? typeof v === 'number'
|
|
568
|
+
: spec.type === 'boolean' ? typeof v === 'boolean'
|
|
569
|
+
: spec.type === 'array' ? Array.isArray(v)
|
|
570
|
+
: spec.type === 'object' ? typeof v === 'object' && !Array.isArray(v)
|
|
571
|
+
: true;
|
|
572
|
+
if (!ok) return `"${key}" must be ${spec.type === 'integer' ? 'an integer' : `a ${spec.type}`}`;
|
|
573
|
+
}
|
|
574
|
+
return null;
|
|
575
|
+
}
|
|
576
|
+
|
|
556
577
|
function safe(fn) {
|
|
557
578
|
try {
|
|
558
579
|
return fn();
|