kovence-cli 0.0.0-stage → 1.0.1

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/ui.js ADDED
@@ -0,0 +1,394 @@
1
+ "use strict";
2
+
3
+ const readline = require("node:readline");
4
+ const { stripAnsi, displayWidth, prevGraphemeBoundary, nextGraphemeBoundary } = require("./src/tui/text_width");
5
+
6
+ const useColor = process.stdout.isTTY && !(process.env.NO_COLOR !== undefined && process.env.NO_COLOR !== "");
7
+ const C = useColor
8
+ ? {
9
+ dim: "\x1b[2m",
10
+ bold: "\x1b[1m",
11
+ cyan: "\x1b[38;5;208m",
12
+ green: "\x1b[32m",
13
+ yellow: "\x1b[38;5;214m",
14
+ red: "\x1b[31m",
15
+ magenta: "\x1b[38;5;202m",
16
+ bgSel: "\x1b[48;5;166m\x1b[97m",
17
+ reset: "\x1b[0m",
18
+ }
19
+ : { dim: "", bold: "", cyan: "", green: "", yellow: "", red: "", magenta: "", bgSel: "", reset: "" };
20
+
21
+ const SLASH_COMMANDS = [
22
+ { cmd: "/help", desc: "Show all commands" },
23
+ { cmd: "/mode", desc: "Switch agent mode (default|accept-edits|plan|auto)" },
24
+ { cmd: "/settings", desc: "Open interactive Settings panel (Ctrl+,)" },
25
+ { cmd: "/set", desc: "Update persisted setting (/set <key> <val>)", hasArg: true },
26
+ { cmd: "/api", desc: "Switch/add OpenAI or Anthropic custom APIs" },
27
+ { cmd: "/model", desc: "Pick or switch active model (/model [name])" },
28
+ { cmd: "/sessions", desc: "Session picker / manage (list|new|switch|delete) (Ctrl+S)" },
29
+ { cmd: "/new", desc: "Open a new isolated session (Ctrl+N)" },
30
+ { cmd: "/retry", desc: "Retry the last failed/aborted user turn" },
31
+ { cmd: "/history", desc: "Show session chat history", hasArg: true },
32
+ { cmd: "/memory", desc: "Show/manage session & global memory", hasArg: true },
33
+ { cmd: "/todos", desc: "Show current session multi-step todos" },
34
+ { cmd: "/compact", desc: "Compact history into session summary" },
35
+ { cmd: "/stats", desc: "Show full context size, speed & token stats" },
36
+ { cmd: "/copy", desc: "Copy last AI response to clipboard (Ctrl+Y)" },
37
+ { cmd: "/select", desc: "Toggle native terminal mouse select mode (F2)" },
38
+ { cmd: "/save", desc: "Rename/save current session", hasArg: true },
39
+ { cmd: "/rename", desc: "Rename current session", hasArg: true },
40
+ { cmd: "/fork", desc: "Fork current session into a new branch", hasArg: true },
41
+ { cmd: "/export", desc: "Export session transcript (md|json)", hasArg: true },
42
+ { cmd: "/edit-last", desc: "Pop last turn and restore prompt for editing" },
43
+ { cmd: "/undo", desc: "Rollback the last file edit/write/delete checkpoint" },
44
+ { cmd: "/init", desc: "Scan project & generate KOVENCE.md instructions" },
45
+ { cmd: "/tasks", desc: "List, view logs, or stop background shell tasks", hasArg: true },
46
+ { cmd: "/cost", desc: "Show estimated session token cost in USD" },
47
+ { cmd: "/clear", desc: "Clear current session history & memory" },
48
+ { cmd: "/exit", desc: "Exit Kovence CLI" },
49
+ ];
50
+
51
+ const SUBCOMMAND_HINTS = {
52
+ "/mode": [
53
+ { fill: "/mode default", desc: "Confirm mutating actions per policy" },
54
+ { fill: "/mode accept-edits", desc: "Auto-approve file edits, confirm commands" },
55
+ { fill: "/mode plan", desc: "Research & plan only (mutations blocked)" },
56
+ { fill: "/mode auto", desc: "Auto-allow except hard denylist & non-workspace" },
57
+ ],
58
+ "/api": [
59
+ { fill: "/api list", desc: "List configured OpenAI/Anthropic APIs" },
60
+ { fill: "/api add", desc: "Add a custom API provider & models interactively" },
61
+ { fill: "/api add-model ", desc: "<provider> <model1,model2...> Add more models" },
62
+ { fill: "/api use ", desc: "Switch active API provider" },
63
+ { fill: "/api rm ", desc: "Remove a custom API provider" },
64
+ ],
65
+ "/sessions": [
66
+ { fill: "/sessions list", desc: "List all sessions" },
67
+ { fill: "/sessions new ", desc: "Create new isolated session" },
68
+ { fill: "/sessions switch ", desc: "Switch to existing session" },
69
+ { fill: "/sessions delete ", desc: "Delete a session" },
70
+ ],
71
+ "/memory": [
72
+ { fill: "/memory", desc: "Show session & global memory" },
73
+ { fill: "/memory add ", desc: "Pin fact to current session" },
74
+ { fill: "/memory clear", desc: "Clear current session memory" },
75
+ { fill: "/memory global add ", desc: "Add shared global memory" },
76
+ { fill: "/memory global rm ", desc: "Remove global memory item" },
77
+ { fill: "/memory global clear", desc: "Clear all global memory" },
78
+ ],
79
+ "/set": [
80
+ { fill: "/set agentMode ", desc: "default | accept-edits | plan | auto" },
81
+ { fill: "/set toolPermission ", desc: "ask | allow-reads | allow-edits | always-proceed" },
82
+ { fill: "/set nonWorkspaceAccess ", desc: "ask | allow | deny" },
83
+ { fill: "/set toolsEnabled ", desc: "true | false" },
84
+ { fill: "/set autoSummarize ", desc: "true | false" },
85
+ { fill: "/set maxToolSteps ", desc: "integer 1-100 (default 25)" },
86
+ { fill: "/set maxHistoryMessages ", desc: "integer 2-500 (default 24)" },
87
+ { fill: "/set contextWindow ", desc: "integer >= 1024 (default 128000)" },
88
+ { fill: "/set temperature ", desc: "0.0-2.0 | null" },
89
+ { fill: "/set maxTokens ", desc: "integer >= 1 | null" },
90
+ { fill: "/set verbosity ", desc: "low | normal | high" },
91
+ { fill: "/set language ", desc: "banglish | bangla | english" },
92
+ { fill: "/set renderingMode ", desc: "fullscreen | inline" },
93
+ { fill: "/set queuedMessages ", desc: "queue | interrupt" },
94
+ { fill: "/set colorScheme ", desc: "amber | dark | light | terminal" },
95
+ ],
96
+ };
97
+
98
+ function getSlashSuggestions(buffer) {
99
+ if (!buffer.startsWith("/")) return [];
100
+ const spaceIdx = buffer.indexOf(" ");
101
+ if (spaceIdx === -1) {
102
+ return SLASH_COMMANDS.filter((c) => c.cmd.startsWith(buffer))
103
+ .slice(0, 8)
104
+ .map((c) => ({
105
+ label: c.cmd,
106
+ fill: c.hasArg ? `${c.cmd} ` : c.cmd,
107
+ desc: c.desc,
108
+ }));
109
+ }
110
+ const base = buffer.slice(0, spaceIdx);
111
+ const subHints = SUBCOMMAND_HINTS[base];
112
+ if (!subHints) return [];
113
+ return subHints
114
+ .filter((h) => h.fill.startsWith(buffer))
115
+ .slice(0, 8)
116
+ .map((h) => ({ label: h.fill.trimEnd(), fill: h.fill, desc: h.desc }));
117
+ }
118
+
119
+ function resolveSlashEnter(input, selIdx = 0) {
120
+ const suggestions = getSlashSuggestions(input);
121
+ if (!suggestions.length) return { action: "submit", value: input.trim() };
122
+ const active = suggestions[selIdx] || suggestions[0];
123
+ if (input.trim() === active.label) {
124
+ return { action: "submit", value: active.label };
125
+ }
126
+ if (active.fill.endsWith(" ")) {
127
+ return { action: "fill", value: active.fill };
128
+ }
129
+ return { action: "submit", value: active.fill.trim() };
130
+ }
131
+
132
+ function clearSuggestionsBelow(rows) {
133
+ if (!rows) return;
134
+ let seq = "";
135
+ for (let i = 0; i < rows; i++) seq += "\n\x1b[K";
136
+ seq += `\x1b[${rows}A`;
137
+ process.stdout.write(seq);
138
+ }
139
+
140
+ // Reads a line in raw TTY mode with live /command suggestions and grapheme-safe editing.
141
+ function promptLine(promptLabel, history = []) {
142
+ if (!process.stdin.isTTY || !process.stdout.isTTY) {
143
+ return new Promise((resolve) => {
144
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
145
+ rl.question(promptLabel, (ans) => {
146
+ rl.close();
147
+ resolve(ans);
148
+ });
149
+ });
150
+ }
151
+
152
+ return new Promise((resolve) => {
153
+ readline.emitKeypressEvents(process.stdin);
154
+ if (process.stdin.isPaused()) process.stdin.resume();
155
+ process.stdin.setRawMode(true);
156
+
157
+ const promptWidth = displayWidth(promptLabel);
158
+ let buffer = "";
159
+ let cursor = 0;
160
+ let selIdx = 0;
161
+ let histIdx = history.length;
162
+ let prevRows = 0;
163
+
164
+ const render = () => {
165
+ if (prevRows) clearSuggestionsBelow(prevRows);
166
+ process.stdout.write(`\r\x1b[K${promptLabel}${buffer}`);
167
+
168
+ const suggestions = getSlashSuggestions(buffer);
169
+ if (selIdx >= suggestions.length) selIdx = 0;
170
+
171
+ if (suggestions.length) {
172
+ let block = "";
173
+ for (let i = 0; i < suggestions.length; i++) {
174
+ const s = suggestions[i];
175
+ const active = i === selIdx;
176
+ const line = active
177
+ ? ` ${C.bgSel} ${s.label.padEnd(22)} ${s.desc} ${C.reset}`
178
+ : ` ${C.cyan}${s.label.padEnd(22)}${C.reset} ${C.dim}${s.desc}${C.reset}`;
179
+ block += `\n\x1b[K${line}`;
180
+ }
181
+ block += `\x1b[${suggestions.length}A`;
182
+ process.stdout.write(block);
183
+ }
184
+ prevRows = suggestions.length;
185
+
186
+ const col = promptWidth + displayWidth(buffer.slice(0, cursor));
187
+ process.stdout.write(col > 0 ? `\r\x1b[${col}C` : "\r");
188
+ };
189
+
190
+ const cleanup = () => {
191
+ process.stdin.removeListener("keypress", onKey);
192
+ if (process.stdin.isTTY) process.stdin.setRawMode(false);
193
+ };
194
+
195
+ const finish = (value) => {
196
+ if (prevRows) clearSuggestionsBelow(prevRows);
197
+ prevRows = 0;
198
+ process.stdout.write(`\r\x1b[K${promptLabel}${buffer}\n`);
199
+ cleanup();
200
+ resolve(value);
201
+ };
202
+
203
+ const onKey = (str, key = {}) => {
204
+ if (key.ctrl && key.name === "c") {
205
+ if (prevRows) clearSuggestionsBelow(prevRows);
206
+ process.stdout.write("\n");
207
+ cleanup();
208
+ resolve(null);
209
+ return;
210
+ }
211
+ if (key.ctrl && key.name === "d" && !buffer.length) {
212
+ finish(null);
213
+ return;
214
+ }
215
+ if (key.name === "escape") {
216
+ if (buffer.length) {
217
+ buffer = "";
218
+ cursor = 0;
219
+ selIdx = 0;
220
+ render();
221
+ }
222
+ return;
223
+ }
224
+ if (key.name === "return" || key.name === "enter") {
225
+ const res = resolveSlashEnter(buffer, selIdx);
226
+ if (res.action === "fill") {
227
+ buffer = res.value;
228
+ cursor = buffer.length;
229
+ selIdx = 0;
230
+ render();
231
+ return;
232
+ }
233
+ buffer = res.value;
234
+ finish(res.value);
235
+ return;
236
+ }
237
+ if (key.name === "tab") {
238
+ const suggestions = getSlashSuggestions(buffer);
239
+ if (suggestions.length) {
240
+ buffer = suggestions[selIdx].fill;
241
+ cursor = buffer.length;
242
+ selIdx = 0;
243
+ render();
244
+ }
245
+ return;
246
+ }
247
+ if (key.name === "up") {
248
+ const suggestions = getSlashSuggestions(buffer);
249
+ if (suggestions.length) {
250
+ selIdx = (selIdx - 1 + suggestions.length) % suggestions.length;
251
+ } else if (history.length && histIdx > 0) {
252
+ histIdx--;
253
+ buffer = history[histIdx];
254
+ cursor = buffer.length;
255
+ }
256
+ render();
257
+ return;
258
+ }
259
+ if (key.name === "down") {
260
+ const suggestions = getSlashSuggestions(buffer);
261
+ if (suggestions.length) {
262
+ selIdx = (selIdx + 1) % suggestions.length;
263
+ } else if (histIdx < history.length) {
264
+ histIdx++;
265
+ buffer = histIdx < history.length ? history[histIdx] : "";
266
+ cursor = buffer.length;
267
+ }
268
+ render();
269
+ return;
270
+ }
271
+ if (key.name === "left") {
272
+ cursor = prevGraphemeBoundary(buffer, cursor);
273
+ render();
274
+ return;
275
+ }
276
+ if (key.name === "right") {
277
+ cursor = nextGraphemeBoundary(buffer, cursor);
278
+ render();
279
+ return;
280
+ }
281
+ if (key.name === "home" || (key.ctrl && key.name === "a")) {
282
+ cursor = 0;
283
+ render();
284
+ return;
285
+ }
286
+ if (key.name === "end" || (key.ctrl && key.name === "e")) {
287
+ cursor = buffer.length;
288
+ render();
289
+ return;
290
+ }
291
+ if (key.name === "backspace") {
292
+ if (cursor > 0) {
293
+ const prev = prevGraphemeBoundary(buffer, cursor);
294
+ buffer = buffer.slice(0, prev) + buffer.slice(cursor);
295
+ cursor = prev;
296
+ selIdx = 0;
297
+ render();
298
+ }
299
+ return;
300
+ }
301
+ if (key.name === "delete") {
302
+ if (cursor < buffer.length) {
303
+ const next = nextGraphemeBoundary(buffer, cursor);
304
+ buffer = buffer.slice(0, cursor) + buffer.slice(next);
305
+ selIdx = 0;
306
+ render();
307
+ }
308
+ return;
309
+ }
310
+ if (key.ctrl && key.name === "u") {
311
+ buffer = "";
312
+ cursor = 0;
313
+ selIdx = 0;
314
+ render();
315
+ return;
316
+ }
317
+ if (str && !key.ctrl && !key.meta && str >= " ") {
318
+ buffer = buffer.slice(0, cursor) + str + buffer.slice(cursor);
319
+ cursor += str.length;
320
+ selIdx = 0;
321
+ render();
322
+ }
323
+ };
324
+
325
+ process.stdin.on("keypress", onKey);
326
+ render();
327
+ });
328
+ }
329
+
330
+ function promptSecret(label) {
331
+ if (!process.stdin.isTTY || !process.stdout.isTTY) return Promise.resolve("");
332
+ return new Promise((resolve) => {
333
+ readline.emitKeypressEvents(process.stdin);
334
+ if (process.stdin.isPaused()) process.stdin.resume();
335
+ process.stdin.setRawMode(true);
336
+ process.stdout.write(label);
337
+
338
+ let secret = "";
339
+ const cleanup = () => {
340
+ process.stdin.removeListener("keypress", onKey);
341
+ if (process.stdin.isTTY) process.stdin.setRawMode(false);
342
+ };
343
+ const onKey = (str, key = {}) => {
344
+ if (key.ctrl && key.name === "c") {
345
+ process.stdout.write("\n");
346
+ cleanup();
347
+ resolve("");
348
+ return;
349
+ }
350
+ if (key.name === "return" || key.name === "enter") {
351
+ process.stdout.write("\n");
352
+ cleanup();
353
+ resolve(secret);
354
+ return;
355
+ }
356
+ if (key.name === "backspace") {
357
+ if (secret.length) {
358
+ const prev = prevGraphemeBoundary(secret, secret.length);
359
+ secret = secret.slice(0, prev);
360
+ process.stdout.write("\b \b");
361
+ }
362
+ return;
363
+ }
364
+ if (str && !key.ctrl && !key.meta && str >= " ") {
365
+ secret += str;
366
+ process.stdout.write("*");
367
+ }
368
+ };
369
+ process.stdin.on("keypress", onKey);
370
+ });
371
+ }
372
+
373
+ function promptConfirm(label) {
374
+ if (!process.stdin.isTTY) return Promise.resolve("n");
375
+ return new Promise((resolve) => {
376
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
377
+ rl.question(label, (ans) => {
378
+ rl.close();
379
+ resolve(ans.trim().toLowerCase());
380
+ });
381
+ });
382
+ }
383
+
384
+ module.exports = {
385
+ C,
386
+ SLASH_COMMANDS,
387
+ SUBCOMMAND_HINTS,
388
+ stripAnsi,
389
+ getSlashSuggestions,
390
+ resolveSlashEnter,
391
+ promptLine,
392
+ promptSecret,
393
+ promptConfirm,
394
+ };