-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathink-ui.mjs
More file actions
347 lines (300 loc) · 11.7 KB
/
ink-ui.mjs
File metadata and controls
347 lines (300 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// ink-ui.mjs — Ink-based terminal UI for claude-native interactive mode
//
// Components:
// App — Root layout: output area + slash menu + input + status bar
// SlashMenu — Popup command palette when input starts with /
// StatusBar — Persistent bottom bar: provider | model | modes | branch | cwd
// OutputArea — Scrollable conversation output
// InputLine — Text input with slash detection
import React, { useState, useEffect, useCallback, useRef } from "react";
import { render, Box, Text, useInput, useApp, useStdout } from "ink";
import TextInput from "ink-text-input";
import { execSync } from "node:child_process";
import os from "node:os";
// ── Slash Menu ─────────────────────────────────────────────────
function SlashMenu({ commands, filter, selectedIndex, visible }) {
if (!visible || commands.length === 0) return null;
return React.createElement(Box, {
flexDirection: "column",
borderStyle: "single",
borderColor: "gray",
paddingX: 1,
marginBottom: 0,
},
React.createElement(Text, { bold: true, dimColor: true }, "Commands"),
...commands.map((cmd, i) => {
const isSelected = i === selectedIndex;
const nameStr = "/" + cmd.name + (cmd.argumentHint ? " " + cmd.argumentHint : "");
const aliases = cmd.aliases?.length > 0 ? ` (${cmd.aliases.map(a => "/" + a).join(", ")})` : "";
const tag = cmd.source === "skill" ? " [skill]" : "";
return React.createElement(Box, { key: cmd.name },
React.createElement(Text, {
color: isSelected ? "black" : (cmd.source === "skill" ? "magenta" : "cyan"),
backgroundColor: isSelected ? "cyan" : undefined,
}, nameStr),
React.createElement(Text, { dimColor: true }, " " + cmd.description + aliases + tag),
);
})
);
}
// ── Status Bar ─────────────────────────────────────────────────
function StatusBar({ cfg, sessionId, messageCount, totalCost, gitBranch }) {
const provider = cfg._provider?.name || "?";
const model = cfg.model || "?";
const modes = [];
if (cfg.briefMode) modes.push("brief");
if (cfg.thinkingBudget > 0) modes.push("think:" + cfg.thinkingBudget);
if (cfg._planMode) modes.push("plan");
const cwd = (cfg.cwd || "").replace(os.homedir(), "~");
const session = sessionId ? sessionId.slice(0, 8) : "-";
const cost = totalCost > 0 ? "$" + totalCost.toFixed(4) : "";
const parts = [
React.createElement(Text, { key: "p", dimColor: true }, provider),
React.createElement(Text, { key: "m", color: "cyan" }, model),
];
if (modes.length > 0) {
parts.push(React.createElement(Text, { key: "mo", color: "yellow" }, "[" + modes.join(",") + "]"));
}
if (gitBranch) {
parts.push(React.createElement(Text, { key: "g", color: "magenta" }, gitBranch));
}
parts.push(React.createElement(Text, { key: "c", dimColor: true }, cwd));
parts.push(React.createElement(Text, { key: "s", dimColor: true }, session));
parts.push(React.createElement(Text, { key: "msg", dimColor: true }, messageCount + "msg"));
if (cost) {
parts.push(React.createElement(Text, { key: "$", dimColor: true }, cost));
}
// Interleave with separators
const withSeps = [];
parts.forEach((p, i) => {
if (i > 0) withSeps.push(React.createElement(Text, { key: "sep" + i, dimColor: true }, " | "));
withSeps.push(p);
});
return React.createElement(Box, { borderStyle: "single", borderColor: "gray", paddingX: 1 }, ...withSeps);
}
// ── Output Area ────────────────────────────────────────────────
function OutputArea({ lines }) {
const { stdout } = useStdout();
const maxLines = (stdout?.rows || 24) - 8; // Reserve space for input + status + menu
const visible = lines.slice(-maxLines);
return React.createElement(Box, { flexDirection: "column", flexGrow: 1 },
...visible.map((line, i) =>
React.createElement(Text, { key: i, wrap: "wrap" }, line)
)
);
}
// ── App (root component) ───────────────────────────────────────
function App({ interactiveMode, onSubmit, onExit }) {
const [input, setInput] = useState("");
const [outputLines, setOutputLines] = useState([]);
const [menuVisible, setMenuVisible] = useState(false);
const [menuIndex, setMenuIndex] = useState(0);
const [menuCommands, setMenuCommands] = useState([]);
const [isProcessing, setIsProcessing] = useState(false);
const { exit } = useApp();
const im = interactiveMode;
// Get git branch once
const [gitBranch] = useState(() => {
try {
return execSync("git rev-parse --abbrev-ref HEAD 2>/dev/null", { cwd: im.cfg.cwd, encoding: "utf-8", timeout: 2000 }).trim();
} catch { return ""; }
});
// Update slash menu when input changes
useEffect(() => {
if (input.startsWith("/") && !isProcessing) {
const filter = input.slice(1).split(/\s/)[0].toLowerCase();
const allCmds = im.slashCommands.list();
const filtered = filter
? allCmds.filter(c => c.name.includes(filter) || c.aliases.some(a => a.includes(filter)) || c.description.toLowerCase().includes(filter))
: allCmds;
setMenuCommands(filtered.slice(0, 10));
setMenuVisible(filtered.length > 0);
setMenuIndex(0);
} else {
setMenuVisible(false);
setMenuCommands([]);
}
}, [input, isProcessing]);
// Handle key presses for menu navigation
useInput((ch, key) => {
if (isProcessing) return;
if (menuVisible) {
if (key.upArrow) {
setMenuIndex(i => Math.max(0, i - 1));
return;
}
if (key.downArrow) {
setMenuIndex(i => Math.min(menuCommands.length - 1, i + 1));
return;
}
if (key.tab && menuCommands.length > 0) {
const selected = menuCommands[menuIndex];
setInput("/" + selected.name + " ");
return;
}
}
if (key.escape) {
if (menuVisible) {
setMenuVisible(false);
} else if (input) {
setInput("");
}
}
});
const addOutput = useCallback((text) => {
setOutputLines(prev => [...prev, text]);
}, []);
const handleSubmit = useCallback(async (value) => {
const trimmed = value.trim();
if (!trimmed) return;
setInput("");
setMenuVisible(false);
// If slash menu is up and user hits enter, use selected command
if (menuVisible && menuCommands.length > 0 && trimmed === "/") {
const selected = menuCommands[menuIndex];
setInput("/" + selected.name + " ");
return;
}
setIsProcessing(true);
addOutput("\x1b[36m> " + trimmed + "\x1b[0m");
try {
await onSubmit(trimmed, addOutput);
} catch (e) {
addOutput("\x1b[31mError: " + e.message + "\x1b[0m");
}
setIsProcessing(false);
}, [menuVisible, menuCommands, menuIndex, onSubmit, addOutput]);
return React.createElement(Box, { flexDirection: "column", height: "100%" },
// Output area
React.createElement(OutputArea, { lines: outputLines }),
// Slash menu (above input)
React.createElement(SlashMenu, {
commands: menuCommands,
filter: input.slice(1),
selectedIndex: menuIndex,
visible: menuVisible && !isProcessing,
}),
// Input line
React.createElement(Box, {},
React.createElement(Text, { color: "cyan" }, isProcessing ? "..." : "> "),
isProcessing
? React.createElement(Text, { dimColor: true }, "thinking...")
: React.createElement(TextInput, {
value: input,
onChange: setInput,
onSubmit: handleSubmit,
placeholder: "Type / for commands, or ask anything...",
}),
),
// Status bar
React.createElement(StatusBar, {
cfg: im.cfg,
sessionId: im.sessionId,
messageCount: im.messages.length,
totalCost: im.totalCost,
gitBranch,
}),
);
}
// ── Public API ─────────────────────────────────────────────────
export function startInkUI(interactiveMode) {
return new Promise((resolve) => {
const im = interactiveMode;
const onSubmit = async (input, addOutput) => {
// Bare "/" or "/help"
if (input === "/" || input === "/help") {
const cmds = im.slashCommands.list();
addOutput("\x1b[1m Commands\x1b[0m");
for (const c of cmds) {
const name = "/" + c.name + (c.argumentHint ? " " + c.argumentHint : "");
const aliases = c.aliases.length > 0 ? ` (${c.aliases.map(a => "/" + a).join(", ")})` : "";
const tag = c.source === "skill" ? " [skill]" : "";
addOutput(" \x1b[36m" + name + "\x1b[0m " + c.description + aliases + tag);
}
return;
}
// Slash commands
if (input.startsWith("/")) {
const [rawCmd, ...args] = input.split(/\s+/);
const cmdName = rawCmd.slice(1);
// Skills — run through _handleSlashCommand which handles the full skill execution path
if (im.cfg._skillLoader?.has(cmdName)) {
addOutput("\x1b[2mRunning skill: /" + cmdName + "\x1b[0m");
const origWrite = process.stderr.write.bind(process.stderr);
let buffer = "";
process.stderr.write = (data) => {
buffer += data;
const lines = buffer.split("\n");
buffer = lines.pop() || "";
for (const line of lines) { addOutput(line); }
return true;
};
try {
await im._handleSlashCommand(input);
} finally {
process.stderr.write = origWrite;
if (buffer.trim()) addOutput(buffer);
}
return;
}
const cmd = im.slashCommands.get(cmdName);
if (cmd) {
if (cmd.name === "exit") {
inkInstance.unmount();
resolve();
return;
}
if (cmd.handler) {
// Capture stderr output
const origWrite = process.stderr.write.bind(process.stderr);
let captured = "";
process.stderr.write = (data) => { captured += data; return true; };
try {
await cmd.handler(args);
} finally {
process.stderr.write = origWrite;
}
if (captured.trim()) {
for (const line of captured.split("\n")) {
if (line.trim()) addOutput(line);
}
}
return;
}
}
addOutput("\x1b[2mUnknown command: " + rawCmd + "\x1b[0m");
return;
}
// Regular input — run through agent loop
// Capture stderr output from the agent
const origWrite = process.stderr.write.bind(process.stderr);
let buffer = "";
process.stderr.write = (data) => {
buffer += data;
// Flush complete lines
const lines = buffer.split("\n");
buffer = lines.pop() || "";
for (const line of lines) {
addOutput(line);
}
return true;
};
try {
await im._processInput(input);
} finally {
process.stderr.write = origWrite;
if (buffer.trim()) addOutput(buffer);
}
};
const app = React.createElement(App, {
interactiveMode: im,
onSubmit,
onExit: () => { resolve(); },
});
const inkInstance = render(app, {
exitOnCtrlC: true,
});
inkInstance.waitUntilExit().then(resolve);
});
}
export { SlashMenu, StatusBar, OutputArea, App };