@robota-sdk/agent-cli 3.0.0-beta.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.
@@ -0,0 +1,1163 @@
1
+ // src/cli.ts
2
+ import { parseArgs } from "util";
3
+ import { readFileSync as readFileSync2 } from "fs";
4
+ import { join as join2, dirname } from "path";
5
+ import { fileURLToPath } from "url";
6
+ import * as readline from "readline";
7
+ import {
8
+ loadConfig,
9
+ loadContext,
10
+ detectProject,
11
+ Session as Session2,
12
+ SessionStore,
13
+ buildSystemPrompt
14
+ } from "@robota-sdk/agent-sdk";
15
+
16
+ // src/permissions/permission-prompt.ts
17
+ import chalk from "chalk";
18
+ var PERMISSION_OPTIONS = ["Allow", "Deny"];
19
+ var ALLOW_INDEX = 0;
20
+ function formatArgs(toolArgs) {
21
+ const entries = Object.entries(toolArgs);
22
+ if (entries.length === 0) {
23
+ return "(no arguments)";
24
+ }
25
+ return entries.map(([k, v]) => `${k}: ${typeof v === "string" ? v : JSON.stringify(v)}`).join(", ");
26
+ }
27
+ async function promptForApproval(terminal, toolName, toolArgs) {
28
+ terminal.writeLine("");
29
+ terminal.writeLine(chalk.yellow(`[Permission Required] Tool: ${toolName}`));
30
+ terminal.writeLine(chalk.dim(` ${formatArgs(toolArgs)}`));
31
+ terminal.writeLine("");
32
+ const selected = await terminal.select(PERMISSION_OPTIONS, ALLOW_INDEX);
33
+ return selected === ALLOW_INDEX;
34
+ }
35
+
36
+ // src/ui/render.tsx
37
+ import { render } from "ink";
38
+
39
+ // src/ui/App.tsx
40
+ import { useState as useState4, useCallback as useCallback2, useRef as useRef2 } from "react";
41
+ import { Box as Box6, Text as Text8, useApp, useInput as useInput4 } from "ink";
42
+ import { Session } from "@robota-sdk/agent-sdk";
43
+
44
+ // src/commands/command-registry.ts
45
+ var CommandRegistry = class {
46
+ sources = [];
47
+ addSource(source) {
48
+ this.sources.push(source);
49
+ }
50
+ /** Get all commands, optionally filtered by prefix */
51
+ getCommands(filter) {
52
+ const all = [];
53
+ for (const source of this.sources) {
54
+ all.push(...source.getCommands());
55
+ }
56
+ if (!filter) return all;
57
+ const lower = filter.toLowerCase();
58
+ return all.filter((cmd) => cmd.name.toLowerCase().startsWith(lower));
59
+ }
60
+ /** Get subcommands for a specific command */
61
+ getSubcommands(commandName) {
62
+ const lower = commandName.toLowerCase();
63
+ for (const source of this.sources) {
64
+ for (const cmd of source.getCommands()) {
65
+ if (cmd.name.toLowerCase() === lower && cmd.subcommands) {
66
+ return cmd.subcommands;
67
+ }
68
+ }
69
+ }
70
+ return [];
71
+ }
72
+ };
73
+
74
+ // src/commands/builtin-source.ts
75
+ function createBuiltinCommands() {
76
+ return [
77
+ { name: "help", description: "Show available commands", source: "builtin" },
78
+ { name: "clear", description: "Clear conversation history", source: "builtin" },
79
+ {
80
+ name: "mode",
81
+ description: "Permission mode",
82
+ source: "builtin",
83
+ subcommands: [
84
+ { name: "plan", description: "Plan only, no execution", source: "builtin" },
85
+ { name: "default", description: "Ask before risky actions", source: "builtin" },
86
+ { name: "acceptEdits", description: "Auto-approve file edits", source: "builtin" },
87
+ { name: "bypassPermissions", description: "Skip all permission checks", source: "builtin" }
88
+ ]
89
+ },
90
+ {
91
+ name: "model",
92
+ description: "Select AI model",
93
+ source: "builtin",
94
+ subcommands: [
95
+ { name: "claude-opus-4-6", description: "Opus 4.6 (highest quality)", source: "builtin" },
96
+ { name: "claude-sonnet-4-6", description: "Sonnet 4.6 (balanced)", source: "builtin" },
97
+ { name: "claude-haiku-4-5", description: "Haiku 4.5 (fastest)", source: "builtin" }
98
+ ]
99
+ },
100
+ { name: "compact", description: "Compress context window", source: "builtin" },
101
+ { name: "cost", description: "Show session info", source: "builtin" },
102
+ { name: "context", description: "Context window info", source: "builtin" },
103
+ { name: "permissions", description: "Permission rules", source: "builtin" },
104
+ { name: "exit", description: "Exit CLI", source: "builtin" }
105
+ ];
106
+ }
107
+ var BuiltinCommandSource = class {
108
+ name = "builtin";
109
+ commands;
110
+ constructor() {
111
+ this.commands = createBuiltinCommands();
112
+ }
113
+ getCommands() {
114
+ return this.commands;
115
+ }
116
+ };
117
+
118
+ // src/commands/skill-source.ts
119
+ import { readdirSync, readFileSync, existsSync } from "fs";
120
+ import { join } from "path";
121
+ import { homedir } from "os";
122
+ function parseFrontmatter(content) {
123
+ const lines = content.split("\n");
124
+ if (lines[0]?.trim() !== "---") return null;
125
+ let name = "";
126
+ let description = "";
127
+ for (let i = 1; i < lines.length; i++) {
128
+ const line = lines[i];
129
+ if (line.trim() === "---") break;
130
+ const nameMatch = line.match(/^name:\s*(.+)/);
131
+ if (nameMatch) {
132
+ name = nameMatch[1].trim();
133
+ continue;
134
+ }
135
+ const descMatch = line.match(/^description:\s*(.+)/);
136
+ if (descMatch) {
137
+ description = descMatch[1].trim();
138
+ }
139
+ }
140
+ return name ? { name, description } : null;
141
+ }
142
+ function scanSkillsDir(skillsDir) {
143
+ if (!existsSync(skillsDir)) return [];
144
+ const commands = [];
145
+ const entries = readdirSync(skillsDir, { withFileTypes: true });
146
+ for (const entry of entries) {
147
+ if (!entry.isDirectory()) continue;
148
+ const skillFile = join(skillsDir, entry.name, "SKILL.md");
149
+ if (!existsSync(skillFile)) continue;
150
+ const content = readFileSync(skillFile, "utf-8");
151
+ const frontmatter = parseFrontmatter(content);
152
+ commands.push({
153
+ name: frontmatter?.name ?? entry.name,
154
+ description: frontmatter?.description ?? `Skill: ${entry.name}`,
155
+ source: "skill"
156
+ });
157
+ }
158
+ return commands;
159
+ }
160
+ var SkillCommandSource = class {
161
+ name = "skill";
162
+ cwd;
163
+ cachedCommands = null;
164
+ constructor(cwd) {
165
+ this.cwd = cwd;
166
+ }
167
+ getCommands() {
168
+ if (this.cachedCommands) return this.cachedCommands;
169
+ const projectSkills = scanSkillsDir(join(this.cwd, ".agents", "skills"));
170
+ const userSkills = scanSkillsDir(join(homedir(), ".claude", "skills"));
171
+ const seen = new Set(projectSkills.map((cmd) => cmd.name));
172
+ const merged = [...projectSkills];
173
+ for (const cmd of userSkills) {
174
+ if (!seen.has(cmd.name)) {
175
+ merged.push(cmd);
176
+ }
177
+ }
178
+ this.cachedCommands = merged;
179
+ return this.cachedCommands;
180
+ }
181
+ };
182
+
183
+ // src/ui/MessageList.tsx
184
+ import { Box, Text } from "ink";
185
+
186
+ // src/ui/render-markdown.ts
187
+ import { marked } from "marked";
188
+ import TerminalRenderer from "marked-terminal";
189
+ marked.setOptions({
190
+ renderer: new TerminalRenderer()
191
+ });
192
+ function renderMarkdown(md) {
193
+ const result = marked.parse(md);
194
+ return typeof result === "string" ? result.trimEnd() : md;
195
+ }
196
+
197
+ // src/ui/MessageList.tsx
198
+ import { jsx, jsxs } from "react/jsx-runtime";
199
+ function RoleLabel({ role }) {
200
+ switch (role) {
201
+ case "user":
202
+ return /* @__PURE__ */ jsxs(Text, { color: "green", bold: true, children: [
203
+ "You:",
204
+ " "
205
+ ] });
206
+ case "assistant":
207
+ return /* @__PURE__ */ jsxs(Text, { color: "cyan", bold: true, children: [
208
+ "Robota:",
209
+ " "
210
+ ] });
211
+ case "system":
212
+ return /* @__PURE__ */ jsxs(Text, { color: "yellow", bold: true, children: [
213
+ "System:",
214
+ " "
215
+ ] });
216
+ case "tool":
217
+ return /* @__PURE__ */ jsxs(Text, { color: "magenta", bold: true, children: [
218
+ "Tool:",
219
+ " "
220
+ ] });
221
+ }
222
+ }
223
+ function MessageItem({ message }) {
224
+ return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [
225
+ /* @__PURE__ */ jsxs(Box, { children: [
226
+ /* @__PURE__ */ jsx(RoleLabel, { role: message.role }),
227
+ message.toolName && /* @__PURE__ */ jsxs(Text, { color: "magenta", dimColor: true, children: [
228
+ "[",
229
+ message.toolName,
230
+ "]",
231
+ " "
232
+ ] })
233
+ ] }),
234
+ /* @__PURE__ */ jsx(Text, { children: " " }),
235
+ /* @__PURE__ */ jsx(Box, { marginLeft: 2, children: /* @__PURE__ */ jsx(Text, { wrap: "wrap", children: message.role === "assistant" ? renderMarkdown(message.content) : message.content }) })
236
+ ] });
237
+ }
238
+ function MessageList({ messages }) {
239
+ return /* @__PURE__ */ jsx(Box, { flexDirection: "column", children: messages.map((msg) => /* @__PURE__ */ jsx(MessageItem, { message: msg }, msg.id)) });
240
+ }
241
+
242
+ // src/ui/StatusBar.tsx
243
+ import { Box as Box2, Text as Text2 } from "ink";
244
+ import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
245
+ var CONTEXT_YELLOW_THRESHOLD = 70;
246
+ var CONTEXT_RED_THRESHOLD = 90;
247
+ function getContextColor(percentage) {
248
+ if (percentage >= CONTEXT_RED_THRESHOLD) return "red";
249
+ if (percentage >= CONTEXT_YELLOW_THRESHOLD) return "yellow";
250
+ return "green";
251
+ }
252
+ function StatusBar({
253
+ permissionMode,
254
+ modelName,
255
+ sessionId: _sessionId,
256
+ messageCount,
257
+ isThinking,
258
+ contextPercentage,
259
+ contextUsedTokens,
260
+ contextMaxTokens
261
+ }) {
262
+ const contextColor = getContextColor(contextPercentage);
263
+ return /* @__PURE__ */ jsxs2(
264
+ Box2,
265
+ {
266
+ borderStyle: "single",
267
+ borderColor: "gray",
268
+ paddingLeft: 1,
269
+ paddingRight: 1,
270
+ justifyContent: "space-between",
271
+ children: [
272
+ /* @__PURE__ */ jsxs2(Text2, { children: [
273
+ /* @__PURE__ */ jsx2(Text2, { color: "cyan", bold: true, children: "Mode:" }),
274
+ " ",
275
+ /* @__PURE__ */ jsx2(Text2, { children: permissionMode }),
276
+ " | ",
277
+ /* @__PURE__ */ jsx2(Text2, { dimColor: true, children: modelName }),
278
+ " | ",
279
+ /* @__PURE__ */ jsxs2(Text2, { color: contextColor, children: [
280
+ "Context: ",
281
+ Math.round(contextPercentage),
282
+ "% (",
283
+ (contextUsedTokens / 1e3).toFixed(1),
284
+ "k/",
285
+ (contextMaxTokens / 1e3).toFixed(0),
286
+ "k)"
287
+ ] })
288
+ ] }),
289
+ /* @__PURE__ */ jsxs2(Text2, { children: [
290
+ isThinking && /* @__PURE__ */ jsx2(Text2, { color: "yellow", children: "Thinking... " }),
291
+ /* @__PURE__ */ jsxs2(Text2, { dimColor: true, children: [
292
+ "msgs: ",
293
+ messageCount
294
+ ] })
295
+ ] })
296
+ ]
297
+ }
298
+ );
299
+ }
300
+
301
+ // src/ui/InputArea.tsx
302
+ import React3, { useState as useState3, useCallback, useMemo } from "react";
303
+ import { Box as Box4, Text as Text6, useInput as useInput2 } from "ink";
304
+
305
+ // src/ui/CjkTextInput.tsx
306
+ import { useRef, useState } from "react";
307
+ import { Text as Text3, useInput, useCursor } from "ink";
308
+ import stringWidth from "string-width";
309
+ import chalk2 from "chalk";
310
+ import { jsx as jsx3 } from "react/jsx-runtime";
311
+ function CjkTextInput({
312
+ value,
313
+ onChange,
314
+ onSubmit,
315
+ placeholder = "",
316
+ focus = true,
317
+ showCursor = true
318
+ }) {
319
+ const valueRef = useRef(value);
320
+ const cursorRef = useRef(value.length);
321
+ const [, forceRender] = useState(0);
322
+ const { setCursorPosition } = useCursor();
323
+ if (value !== valueRef.current) {
324
+ valueRef.current = value;
325
+ if (cursorRef.current > value.length) {
326
+ cursorRef.current = value.length;
327
+ }
328
+ }
329
+ useInput(
330
+ (input, key) => {
331
+ if (key.upArrow || key.downArrow || key.ctrl && input === "c" || key.tab || key.shift && key.tab) {
332
+ return;
333
+ }
334
+ if (key.return) {
335
+ onSubmit?.(valueRef.current);
336
+ return;
337
+ }
338
+ if (key.leftArrow) {
339
+ if (cursorRef.current > 0) {
340
+ cursorRef.current -= 1;
341
+ forceRender((n) => n + 1);
342
+ }
343
+ return;
344
+ }
345
+ if (key.rightArrow) {
346
+ if (cursorRef.current < valueRef.current.length) {
347
+ cursorRef.current += 1;
348
+ forceRender((n) => n + 1);
349
+ }
350
+ return;
351
+ }
352
+ if (key.backspace || key.delete) {
353
+ if (cursorRef.current > 0) {
354
+ const v2 = valueRef.current;
355
+ const next2 = v2.slice(0, cursorRef.current - 1) + v2.slice(cursorRef.current);
356
+ cursorRef.current -= 1;
357
+ valueRef.current = next2;
358
+ onChange(next2);
359
+ }
360
+ return;
361
+ }
362
+ const v = valueRef.current;
363
+ const c = cursorRef.current;
364
+ const next = v.slice(0, c) + input + v.slice(c);
365
+ cursorRef.current = c + input.length;
366
+ valueRef.current = next;
367
+ onChange(next);
368
+ },
369
+ { isActive: focus }
370
+ );
371
+ if (showCursor && focus) {
372
+ const textBeforeCursor = [...valueRef.current].slice(0, cursorRef.current).join("");
373
+ const cursorX = 4 + stringWidth(textBeforeCursor);
374
+ setCursorPosition({ x: cursorX, y: 0 });
375
+ }
376
+ return /* @__PURE__ */ jsx3(Text3, { children: renderWithCursor(valueRef.current, cursorRef.current, placeholder, showCursor && focus) });
377
+ }
378
+ function renderWithCursor(value, cursorOffset, placeholder, showCursor) {
379
+ if (!showCursor) {
380
+ return value.length > 0 ? value : placeholder ? chalk2.gray(placeholder) : "";
381
+ }
382
+ if (value.length === 0) {
383
+ if (placeholder.length > 0) {
384
+ return chalk2.inverse(placeholder[0]) + chalk2.gray(placeholder.slice(1));
385
+ }
386
+ return chalk2.inverse(" ");
387
+ }
388
+ const chars = [...value];
389
+ let rendered = "";
390
+ for (let i = 0; i < chars.length; i++) {
391
+ const char = chars[i] ?? "";
392
+ rendered += i === cursorOffset ? chalk2.inverse(char) : char;
393
+ }
394
+ if (cursorOffset >= chars.length) {
395
+ rendered += chalk2.inverse(" ");
396
+ }
397
+ return rendered;
398
+ }
399
+
400
+ // src/ui/WaveText.tsx
401
+ import { useState as useState2, useEffect } from "react";
402
+ import { Text as Text4 } from "ink";
403
+ import { jsx as jsx4 } from "react/jsx-runtime";
404
+ var WAVE_COLORS = ["#666666", "#888888", "#aaaaaa", "#888888"];
405
+ var INTERVAL_MS = 400;
406
+ var CHARS_PER_GROUP = 4;
407
+ function WaveText({ text }) {
408
+ const [tick, setTick] = useState2(0);
409
+ useEffect(() => {
410
+ const timer = setInterval(() => {
411
+ setTick((prev) => prev + 1);
412
+ }, INTERVAL_MS);
413
+ return () => clearInterval(timer);
414
+ }, []);
415
+ const chars = [...text];
416
+ return /* @__PURE__ */ jsx4(Text4, { children: chars.map((char, i) => {
417
+ const group = Math.floor(i / CHARS_PER_GROUP);
418
+ const colorIndex = (tick + group) % WAVE_COLORS.length;
419
+ return /* @__PURE__ */ jsx4(Text4, { color: WAVE_COLORS[colorIndex], children: char }, i);
420
+ }) });
421
+ }
422
+
423
+ // src/ui/SlashAutocomplete.tsx
424
+ import { Box as Box3, Text as Text5 } from "ink";
425
+ import { jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime";
426
+ var MAX_VISIBLE = 8;
427
+ function CommandRow(props) {
428
+ const { cmd, isSelected, showSlash } = props;
429
+ const prefix = showSlash ? "/" : "";
430
+ const indicator = isSelected ? "\u25B8 " : " ";
431
+ const nameColor = isSelected ? "cyan" : void 0;
432
+ const dimmed = !isSelected;
433
+ return /* @__PURE__ */ jsxs3(Box3, { children: [
434
+ /* @__PURE__ */ jsxs3(Text5, { color: nameColor, dimColor: dimmed, children: [
435
+ indicator,
436
+ prefix,
437
+ cmd.name
438
+ ] }),
439
+ /* @__PURE__ */ jsx5(Text5, { dimColor: dimmed, children: " " }),
440
+ /* @__PURE__ */ jsx5(Text5, { color: nameColor, dimColor: dimmed, children: cmd.description })
441
+ ] });
442
+ }
443
+ function SlashAutocomplete({
444
+ commands,
445
+ selectedIndex,
446
+ visible,
447
+ isSubcommandMode
448
+ }) {
449
+ if (!visible || commands.length === 0) return null;
450
+ const scrollOffset = computeScrollOffset(selectedIndex, commands.length);
451
+ const visibleCommands = commands.slice(scrollOffset, scrollOffset + MAX_VISIBLE);
452
+ return /* @__PURE__ */ jsx5(Box3, { flexDirection: "column", borderStyle: "round", borderColor: "gray", paddingX: 1, children: visibleCommands.map((cmd, i) => /* @__PURE__ */ jsx5(
453
+ CommandRow,
454
+ {
455
+ cmd,
456
+ isSelected: scrollOffset + i === selectedIndex,
457
+ showSlash: !isSubcommandMode
458
+ },
459
+ cmd.name
460
+ )) });
461
+ }
462
+ function computeScrollOffset(selectedIndex, total) {
463
+ if (total <= MAX_VISIBLE) return 0;
464
+ if (selectedIndex < MAX_VISIBLE) return 0;
465
+ const maxOffset = total - MAX_VISIBLE;
466
+ return Math.min(selectedIndex - MAX_VISIBLE + 1, maxOffset);
467
+ }
468
+
469
+ // src/ui/InputArea.tsx
470
+ import { jsx as jsx6, jsxs as jsxs4 } from "react/jsx-runtime";
471
+ function parseSlashInput(value) {
472
+ if (!value.startsWith("/")) return { isSlash: false, parentCommand: "", filter: "" };
473
+ const afterSlash = value.slice(1);
474
+ const spaceIndex = afterSlash.indexOf(" ");
475
+ if (spaceIndex === -1) return { isSlash: true, parentCommand: "", filter: afterSlash };
476
+ const parent = afterSlash.slice(0, spaceIndex);
477
+ const rest = afterSlash.slice(spaceIndex + 1);
478
+ return { isSlash: true, parentCommand: parent, filter: rest };
479
+ }
480
+ function useAutocomplete(value, registry) {
481
+ const [selectedIndex, setSelectedIndex] = useState3(0);
482
+ const [dismissed, setDismissed] = useState3(false);
483
+ const prevValueRef = React3.useRef(value);
484
+ if (prevValueRef.current !== value) {
485
+ prevValueRef.current = value;
486
+ if (dismissed) setDismissed(false);
487
+ }
488
+ const parsed = parseSlashInput(value);
489
+ const isSubcommandMode = parsed.isSlash && parsed.parentCommand.length > 0;
490
+ const filteredCommands = useMemo(() => {
491
+ if (!registry || !parsed.isSlash || dismissed) return [];
492
+ if (isSubcommandMode) {
493
+ const subs = registry.getSubcommands(parsed.parentCommand);
494
+ if (subs.length === 0) return [];
495
+ if (!parsed.filter) return subs;
496
+ const lower = parsed.filter.toLowerCase();
497
+ return subs.filter((c) => c.name.toLowerCase().startsWith(lower));
498
+ }
499
+ return registry.getCommands(parsed.filter);
500
+ }, [registry, parsed.isSlash, parsed.parentCommand, parsed.filter, dismissed, isSubcommandMode]);
501
+ const showPopup = parsed.isSlash && filteredCommands.length > 0 && !dismissed;
502
+ if (selectedIndex >= filteredCommands.length && filteredCommands.length > 0) {
503
+ setSelectedIndex(filteredCommands.length - 1);
504
+ }
505
+ return {
506
+ showPopup,
507
+ filteredCommands,
508
+ selectedIndex,
509
+ setSelectedIndex,
510
+ isSubcommandMode,
511
+ setShowPopup: (val) => {
512
+ if (typeof val === "function") {
513
+ setDismissed((prev) => {
514
+ const nextVal = val(!prev);
515
+ return !nextVal;
516
+ });
517
+ } else {
518
+ setDismissed(!val);
519
+ }
520
+ }
521
+ };
522
+ }
523
+ function InputArea({ onSubmit, isDisabled, registry }) {
524
+ const [value, setValue] = useState3("");
525
+ const {
526
+ showPopup,
527
+ filteredCommands,
528
+ selectedIndex,
529
+ setSelectedIndex,
530
+ isSubcommandMode,
531
+ setShowPopup
532
+ } = useAutocomplete(value, registry);
533
+ const handleSubmit = useCallback(
534
+ (text) => {
535
+ const trimmed = text.trim();
536
+ if (trimmed.length === 0) return;
537
+ if (showPopup && filteredCommands[selectedIndex]) {
538
+ selectCommand(filteredCommands[selectedIndex]);
539
+ return;
540
+ }
541
+ setValue("");
542
+ onSubmit(trimmed);
543
+ },
544
+ [showPopup, filteredCommands, selectedIndex, onSubmit]
545
+ );
546
+ const selectCommand = useCallback(
547
+ (cmd) => {
548
+ const parsed = parseSlashInput(value);
549
+ if (parsed.parentCommand) {
550
+ const fullCommand = `/${parsed.parentCommand} ${cmd.name}`;
551
+ setValue("");
552
+ onSubmit(fullCommand);
553
+ return;
554
+ }
555
+ if (cmd.subcommands && cmd.subcommands.length > 0) {
556
+ setValue(`/${cmd.name} `);
557
+ setSelectedIndex(0);
558
+ return;
559
+ }
560
+ setValue("");
561
+ onSubmit(`/${cmd.name}`);
562
+ },
563
+ [value, onSubmit, setSelectedIndex]
564
+ );
565
+ useInput2(
566
+ (_input, key) => {
567
+ if (!showPopup) return;
568
+ if (key.upArrow) {
569
+ setSelectedIndex((prev) => prev > 0 ? prev - 1 : filteredCommands.length - 1);
570
+ } else if (key.downArrow) {
571
+ setSelectedIndex((prev) => prev < filteredCommands.length - 1 ? prev + 1 : 0);
572
+ } else if (key.escape) {
573
+ setShowPopup(false);
574
+ } else if (key.tab) {
575
+ const cmd = filteredCommands[selectedIndex];
576
+ if (cmd) selectCommand(cmd);
577
+ }
578
+ },
579
+ { isActive: showPopup && !isDisabled }
580
+ );
581
+ return /* @__PURE__ */ jsxs4(Box4, { flexDirection: "column", children: [
582
+ showPopup && /* @__PURE__ */ jsx6(
583
+ SlashAutocomplete,
584
+ {
585
+ commands: filteredCommands,
586
+ selectedIndex,
587
+ visible: showPopup,
588
+ isSubcommandMode
589
+ }
590
+ ),
591
+ /* @__PURE__ */ jsx6(Box4, { borderStyle: "single", borderColor: isDisabled ? "gray" : "green", paddingLeft: 1, children: isDisabled ? /* @__PURE__ */ jsx6(WaveText, { text: " Waiting for response..." }) : /* @__PURE__ */ jsxs4(Box4, { children: [
592
+ /* @__PURE__ */ jsx6(Text6, { color: "green", bold: true, children: "> " }),
593
+ /* @__PURE__ */ jsx6(
594
+ CjkTextInput,
595
+ {
596
+ value,
597
+ onChange: setValue,
598
+ onSubmit: handleSubmit,
599
+ placeholder: "Type a message or /help"
600
+ }
601
+ )
602
+ ] }) })
603
+ ] });
604
+ }
605
+
606
+ // src/ui/PermissionPrompt.tsx
607
+ import React4 from "react";
608
+ import { Box as Box5, Text as Text7, useInput as useInput3 } from "ink";
609
+ import { jsx as jsx7, jsxs as jsxs5 } from "react/jsx-runtime";
610
+ var OPTIONS = ["Allow", "Deny"];
611
+ function formatArgs2(args) {
612
+ const entries = Object.entries(args);
613
+ if (entries.length === 0) return "(no arguments)";
614
+ return entries.map(([k, v]) => `${k}: ${typeof v === "string" ? v : JSON.stringify(v)}`).join(", ");
615
+ }
616
+ function PermissionPrompt({ request }) {
617
+ const [selected, setSelected] = React4.useState(0);
618
+ const resolvedRef = React4.useRef(false);
619
+ const prevRequestRef = React4.useRef(request);
620
+ if (prevRequestRef.current !== request) {
621
+ prevRequestRef.current = request;
622
+ resolvedRef.current = false;
623
+ setSelected(0);
624
+ }
625
+ const doResolve = React4.useCallback(
626
+ (allowed) => {
627
+ if (resolvedRef.current) return;
628
+ resolvedRef.current = true;
629
+ request.resolve(allowed);
630
+ },
631
+ [request]
632
+ );
633
+ useInput3((input, key) => {
634
+ if (resolvedRef.current) return;
635
+ if (key.upArrow || key.leftArrow) {
636
+ setSelected((prev) => prev > 0 ? prev - 1 : prev);
637
+ } else if (key.downArrow || key.rightArrow) {
638
+ setSelected((prev) => prev < OPTIONS.length - 1 ? prev + 1 : prev);
639
+ } else if (key.return) {
640
+ doResolve(selected === 0);
641
+ } else if (input === "y" || input === "a" || input === "1") {
642
+ doResolve(true);
643
+ } else if (input === "n" || input === "d" || input === "2") {
644
+ doResolve(false);
645
+ }
646
+ });
647
+ return /* @__PURE__ */ jsxs5(Box5, { flexDirection: "column", borderStyle: "round", borderColor: "yellow", paddingX: 1, children: [
648
+ /* @__PURE__ */ jsx7(Text7, { color: "yellow", bold: true, children: "[Permission Required]" }),
649
+ /* @__PURE__ */ jsxs5(Text7, { children: [
650
+ "Tool:",
651
+ " ",
652
+ /* @__PURE__ */ jsx7(Text7, { color: "cyan", bold: true, children: request.toolName })
653
+ ] }),
654
+ /* @__PURE__ */ jsxs5(Text7, { dimColor: true, children: [
655
+ " ",
656
+ formatArgs2(request.toolArgs)
657
+ ] }),
658
+ /* @__PURE__ */ jsx7(Box5, { marginTop: 1, children: OPTIONS.map((opt, i) => /* @__PURE__ */ jsx7(Box5, { marginRight: 2, children: /* @__PURE__ */ jsxs5(Text7, { color: i === selected ? "cyan" : void 0, bold: i === selected, children: [
659
+ i === selected ? "> " : " ",
660
+ opt
661
+ ] }) }, opt)) }),
662
+ /* @__PURE__ */ jsx7(Text7, { dimColor: true, children: " left/right to select, Enter to confirm" })
663
+ ] });
664
+ }
665
+
666
+ // src/ui/App.tsx
667
+ import { jsx as jsx8, jsxs as jsxs6 } from "react/jsx-runtime";
668
+ var msgIdCounter = 0;
669
+ function nextId() {
670
+ msgIdCounter += 1;
671
+ return `msg_${msgIdCounter}`;
672
+ }
673
+ var NOOP_TERMINAL = {
674
+ write: () => {
675
+ },
676
+ writeLine: () => {
677
+ },
678
+ writeMarkdown: () => {
679
+ },
680
+ writeError: () => {
681
+ },
682
+ prompt: () => Promise.resolve(""),
683
+ select: () => Promise.resolve(0),
684
+ spinner: () => ({ stop: () => {
685
+ }, update: () => {
686
+ } })
687
+ };
688
+ function useSession(props) {
689
+ const [permissionRequest, setPermissionRequest] = useState4(null);
690
+ const [streamingText, setStreamingText] = useState4("");
691
+ const permissionQueueRef = useRef2([]);
692
+ const processingRef = useRef2(false);
693
+ const processNextPermission = useCallback2(() => {
694
+ if (processingRef.current) return;
695
+ const next = permissionQueueRef.current[0];
696
+ if (!next) {
697
+ setPermissionRequest(null);
698
+ return;
699
+ }
700
+ processingRef.current = true;
701
+ setPermissionRequest({
702
+ toolName: next.toolName,
703
+ toolArgs: next.toolArgs,
704
+ resolve: (allowed) => {
705
+ permissionQueueRef.current.shift();
706
+ processingRef.current = false;
707
+ setPermissionRequest(null);
708
+ next.resolve(allowed);
709
+ setTimeout(() => processNextPermission(), 0);
710
+ }
711
+ });
712
+ }, []);
713
+ const sessionRef = useRef2(null);
714
+ if (sessionRef.current === null) {
715
+ const permissionHandler = (toolName, toolArgs) => {
716
+ return new Promise((resolve) => {
717
+ permissionQueueRef.current.push({ toolName, toolArgs, resolve });
718
+ processNextPermission();
719
+ });
720
+ };
721
+ const onTextDelta = (delta) => {
722
+ setStreamingText((prev) => prev + delta);
723
+ };
724
+ sessionRef.current = new Session({
725
+ config: props.config,
726
+ context: props.context,
727
+ terminal: NOOP_TERMINAL,
728
+ projectInfo: props.projectInfo,
729
+ sessionStore: props.sessionStore,
730
+ permissionMode: props.permissionMode,
731
+ maxTurns: props.maxTurns,
732
+ permissionHandler,
733
+ onTextDelta
734
+ });
735
+ }
736
+ const clearStreamingText = useCallback2(() => setStreamingText(""), []);
737
+ return { session: sessionRef.current, permissionRequest, streamingText, clearStreamingText };
738
+ }
739
+ function useMessages() {
740
+ const [messages, setMessages] = useState4([]);
741
+ const addMessage = useCallback2((msg) => {
742
+ setMessages((prev) => [...prev, { ...msg, id: nextId(), timestamp: /* @__PURE__ */ new Date() }]);
743
+ }, []);
744
+ return { messages, setMessages, addMessage };
745
+ }
746
+ var HELP_TEXT = [
747
+ "Available commands:",
748
+ " /help \u2014 Show this help",
749
+ " /clear \u2014 Clear conversation",
750
+ " /compact [instr] \u2014 Compact context (optional focus instructions)",
751
+ " /mode [m] \u2014 Show/change permission mode",
752
+ " /cost \u2014 Show session info",
753
+ " /exit \u2014 Exit CLI"
754
+ ].join("\n");
755
+ function handleModeCommand(arg, session, addMessage) {
756
+ const validModes = ["plan", "default", "acceptEdits", "bypassPermissions"];
757
+ if (!arg) {
758
+ addMessage({ role: "system", content: `Current mode: ${session.getPermissionMode()}` });
759
+ } else if (validModes.includes(arg)) {
760
+ session.setPermissionMode(arg);
761
+ addMessage({ role: "system", content: `Permission mode set to: ${arg}` });
762
+ } else {
763
+ addMessage({ role: "system", content: `Invalid mode. Valid: ${validModes.join(" | ")}` });
764
+ }
765
+ return true;
766
+ }
767
+ async function executeSlashCommand(cmd, parts, session, addMessage, setMessages, exit, registry) {
768
+ switch (cmd) {
769
+ case "help":
770
+ addMessage({ role: "system", content: HELP_TEXT });
771
+ return true;
772
+ case "clear":
773
+ setMessages([]);
774
+ session.clearHistory();
775
+ addMessage({ role: "system", content: "Conversation cleared." });
776
+ return true;
777
+ case "compact": {
778
+ const instructions = parts.slice(1).join(" ").trim() || void 0;
779
+ const before = session.getContextState().usedPercentage;
780
+ addMessage({ role: "system", content: "Compacting context..." });
781
+ await session.compact(instructions);
782
+ const after = session.getContextState().usedPercentage;
783
+ addMessage({
784
+ role: "system",
785
+ content: `Context compacted: ${Math.round(before)}% -> ${Math.round(after)}%`
786
+ });
787
+ return true;
788
+ }
789
+ case "mode":
790
+ return handleModeCommand(parts[1], session, addMessage);
791
+ case "cost":
792
+ addMessage({
793
+ role: "system",
794
+ content: `Session: ${session.getSessionId()}
795
+ Messages: ${session.getMessageCount()}`
796
+ });
797
+ return true;
798
+ case "exit":
799
+ exit();
800
+ return true;
801
+ default: {
802
+ const skillCmd = registry.getCommands().find((c) => c.name === cmd && c.source === "skill");
803
+ if (skillCmd) {
804
+ addMessage({ role: "system", content: `Invoking skill: ${cmd}` });
805
+ return false;
806
+ }
807
+ addMessage({ role: "system", content: `Unknown command "/${cmd}". Type /help for help.` });
808
+ return true;
809
+ }
810
+ }
811
+ }
812
+ function useSlashCommands(session, addMessage, setMessages, exit, registry) {
813
+ return useCallback2(
814
+ async (input) => {
815
+ const parts = input.slice(1).split(/\s+/);
816
+ const cmd = parts[0]?.toLowerCase() ?? "";
817
+ return executeSlashCommand(cmd, parts, session, addMessage, setMessages, exit, registry);
818
+ },
819
+ [session, addMessage, setMessages, exit, registry]
820
+ );
821
+ }
822
+ function StreamingIndicator({ text }) {
823
+ if (text) {
824
+ return /* @__PURE__ */ jsxs6(Box6, { flexDirection: "column", children: [
825
+ /* @__PURE__ */ jsxs6(Text8, { color: "cyan", bold: true, children: [
826
+ "Robota:",
827
+ " "
828
+ ] }),
829
+ /* @__PURE__ */ jsx8(Text8, { children: " " }),
830
+ /* @__PURE__ */ jsx8(Box6, { marginLeft: 2, children: /* @__PURE__ */ jsx8(Text8, { wrap: "wrap", children: renderMarkdown(text) }) })
831
+ ] });
832
+ }
833
+ return /* @__PURE__ */ jsx8(Text8, { color: "yellow", children: "Thinking..." });
834
+ }
835
+ async function runSessionPrompt(prompt, session, addMessage, clearStreamingText, setIsThinking, setContextPercentage) {
836
+ setIsThinking(true);
837
+ clearStreamingText();
838
+ try {
839
+ const response = await session.run(prompt);
840
+ clearStreamingText();
841
+ addMessage({ role: "assistant", content: response || "(empty response)" });
842
+ setContextPercentage(session.getContextState().usedPercentage);
843
+ } catch (err) {
844
+ clearStreamingText();
845
+ const errMsg = err instanceof Error ? err.message : String(err);
846
+ addMessage({ role: "system", content: `Error: ${errMsg}` });
847
+ } finally {
848
+ setIsThinking(false);
849
+ }
850
+ }
851
+ function buildSkillPrompt(input, registry) {
852
+ const parts = input.slice(1).split(/\s+/);
853
+ const cmd = parts[0]?.toLowerCase() ?? "";
854
+ const skillCmd = registry.getCommands().find((c) => c.name === cmd && c.source === "skill");
855
+ if (!skillCmd) return null;
856
+ const args = parts.slice(1).join(" ").trim();
857
+ return args ? `Use the "${cmd}" skill: ${args}` : `Use the "${cmd}" skill: ${skillCmd.description}`;
858
+ }
859
+ function useSubmitHandler(session, addMessage, handleSlashCommand, clearStreamingText, setIsThinking, setContextPercentage, registry) {
860
+ return useCallback2(
861
+ async (input) => {
862
+ if (input.startsWith("/")) {
863
+ const handled = await handleSlashCommand(input);
864
+ if (handled) {
865
+ setContextPercentage(session.getContextState().usedPercentage);
866
+ return;
867
+ }
868
+ const prompt = buildSkillPrompt(input, registry);
869
+ if (!prompt) return;
870
+ return runSessionPrompt(
871
+ prompt,
872
+ session,
873
+ addMessage,
874
+ clearStreamingText,
875
+ setIsThinking,
876
+ setContextPercentage
877
+ );
878
+ }
879
+ addMessage({ role: "user", content: input });
880
+ return runSessionPrompt(
881
+ input,
882
+ session,
883
+ addMessage,
884
+ clearStreamingText,
885
+ setIsThinking,
886
+ setContextPercentage
887
+ );
888
+ },
889
+ [
890
+ session,
891
+ addMessage,
892
+ handleSlashCommand,
893
+ clearStreamingText,
894
+ setIsThinking,
895
+ setContextPercentage,
896
+ registry
897
+ ]
898
+ );
899
+ }
900
+ function useCommandRegistry(cwd) {
901
+ const registryRef = useRef2(null);
902
+ if (registryRef.current === null) {
903
+ const registry = new CommandRegistry();
904
+ registry.addSource(new BuiltinCommandSource());
905
+ registry.addSource(new SkillCommandSource(cwd));
906
+ registryRef.current = registry;
907
+ }
908
+ return registryRef.current;
909
+ }
910
+ function App(props) {
911
+ const { exit } = useApp();
912
+ const { session, permissionRequest, streamingText, clearStreamingText } = useSession(props);
913
+ const { messages, setMessages, addMessage } = useMessages();
914
+ const [isThinking, setIsThinking] = useState4(false);
915
+ const [contextPercentage, setContextPercentage] = useState4(0);
916
+ const registry = useCommandRegistry(props.cwd ?? process.cwd());
917
+ const handleSlashCommand = useSlashCommands(session, addMessage, setMessages, exit, registry);
918
+ const handleSubmit = useSubmitHandler(
919
+ session,
920
+ addMessage,
921
+ handleSlashCommand,
922
+ clearStreamingText,
923
+ setIsThinking,
924
+ setContextPercentage,
925
+ registry
926
+ );
927
+ useInput4(
928
+ (_input, key) => {
929
+ if (key.ctrl && _input === "c") exit();
930
+ },
931
+ { isActive: !permissionRequest && !isThinking }
932
+ );
933
+ return /* @__PURE__ */ jsxs6(Box6, { flexDirection: "column", children: [
934
+ /* @__PURE__ */ jsxs6(Box6, { flexDirection: "column", paddingX: 1, marginBottom: 1, children: [
935
+ /* @__PURE__ */ jsx8(Text8, { color: "cyan", bold: true, children: `
936
+ ____ ___ ____ ___ _____ _
937
+ | _ \\ / _ \\| __ ) / _ \\_ _|/ \\
938
+ | |_) | | | | _ \\| | | || | / _ \\
939
+ | _ <| |_| | |_) | |_| || |/ ___ \\
940
+ |_| \\_\\\\___/|____/ \\___/ |_/_/ \\_\\
941
+ ` }),
942
+ /* @__PURE__ */ jsxs6(Text8, { dimColor: true, children: [
943
+ " v",
944
+ props.version ?? "0.0.0"
945
+ ] })
946
+ ] }),
947
+ /* @__PURE__ */ jsxs6(Box6, { flexDirection: "column", paddingX: 1, flexGrow: 1, children: [
948
+ /* @__PURE__ */ jsx8(MessageList, { messages }),
949
+ isThinking && /* @__PURE__ */ jsx8(Box6, { flexDirection: "column", marginBottom: 1, children: /* @__PURE__ */ jsx8(StreamingIndicator, { text: streamingText }) })
950
+ ] }),
951
+ permissionRequest && /* @__PURE__ */ jsx8(PermissionPrompt, { request: permissionRequest }),
952
+ /* @__PURE__ */ jsx8(
953
+ StatusBar,
954
+ {
955
+ permissionMode: session.getPermissionMode(),
956
+ modelName: props.config.provider.model,
957
+ sessionId: session.getSessionId(),
958
+ messageCount: messages.length,
959
+ isThinking,
960
+ contextPercentage,
961
+ contextUsedTokens: session.getContextState().usedTokens,
962
+ contextMaxTokens: session.getContextState().maxTokens
963
+ }
964
+ ),
965
+ /* @__PURE__ */ jsx8(
966
+ InputArea,
967
+ {
968
+ onSubmit: handleSubmit,
969
+ isDisabled: isThinking || !!permissionRequest,
970
+ registry
971
+ }
972
+ )
973
+ ] });
974
+ }
975
+
976
+ // src/ui/render.tsx
977
+ import { jsx as jsx9 } from "react/jsx-runtime";
978
+ function renderApp(options) {
979
+ process.on("unhandledRejection", (reason) => {
980
+ process.stderr.write(`
981
+ [UNHANDLED REJECTION] ${reason}
982
+ `);
983
+ if (reason instanceof Error) {
984
+ process.stderr.write(`${reason.stack}
985
+ `);
986
+ }
987
+ });
988
+ const instance = render(/* @__PURE__ */ jsx9(App, { ...options }), {
989
+ exitOnCtrlC: true
990
+ });
991
+ instance.waitUntilExit().catch((err) => {
992
+ if (err) {
993
+ process.stderr.write(`
994
+ [EXIT ERROR] ${err}
995
+ `);
996
+ }
997
+ });
998
+ }
999
+
1000
+ // src/cli.ts
1001
+ var VALID_MODES = ["plan", "default", "acceptEdits", "bypassPermissions"];
1002
+ function readVersion() {
1003
+ try {
1004
+ const thisFile = fileURLToPath(import.meta.url);
1005
+ const dir = dirname(thisFile);
1006
+ const candidates = [join2(dir, "..", "..", "package.json"), join2(dir, "..", "package.json")];
1007
+ for (const pkgPath of candidates) {
1008
+ try {
1009
+ const raw = readFileSync2(pkgPath, "utf-8");
1010
+ const pkg = JSON.parse(raw);
1011
+ if (pkg.version !== void 0 && pkg.name !== void 0) {
1012
+ return pkg.version;
1013
+ }
1014
+ } catch {
1015
+ }
1016
+ }
1017
+ return "0.0.0";
1018
+ } catch {
1019
+ return "0.0.0";
1020
+ }
1021
+ }
1022
+ function parsePermissionMode(raw) {
1023
+ if (raw === void 0) return void 0;
1024
+ if (!VALID_MODES.includes(raw)) {
1025
+ process.stderr.write(`Invalid --permission-mode "${raw}". Valid: ${VALID_MODES.join(" | ")}
1026
+ `);
1027
+ process.exit(1);
1028
+ }
1029
+ return raw;
1030
+ }
1031
+ function parseMaxTurns(raw) {
1032
+ if (raw === void 0) return void 0;
1033
+ const n = parseInt(raw, 10);
1034
+ if (isNaN(n) || n <= 0) {
1035
+ process.stderr.write(`Invalid --max-turns "${raw}". Must be a positive integer.
1036
+ `);
1037
+ process.exit(1);
1038
+ }
1039
+ return n;
1040
+ }
1041
+ function parseCliArgs() {
1042
+ const { values, positionals } = parseArgs({
1043
+ allowPositionals: true,
1044
+ options: {
1045
+ p: { type: "boolean", short: "p", default: false },
1046
+ c: { type: "boolean", short: "c", default: false },
1047
+ r: { type: "string", short: "r" },
1048
+ model: { type: "string" },
1049
+ "permission-mode": { type: "string" },
1050
+ "max-turns": { type: "string" },
1051
+ version: { type: "boolean", default: false }
1052
+ }
1053
+ });
1054
+ return {
1055
+ positional: positionals,
1056
+ printMode: values["p"] ?? false,
1057
+ continueMode: values["c"] ?? false,
1058
+ resumeId: values["r"],
1059
+ model: values["model"],
1060
+ permissionMode: parsePermissionMode(values["permission-mode"]),
1061
+ maxTurns: parseMaxTurns(values["max-turns"]),
1062
+ version: values["version"] ?? false
1063
+ };
1064
+ }
1065
+ var PrintTerminal = class {
1066
+ write(text) {
1067
+ process.stdout.write(text);
1068
+ }
1069
+ writeLine(text) {
1070
+ process.stdout.write(text + "\n");
1071
+ }
1072
+ writeMarkdown(md) {
1073
+ process.stdout.write(md);
1074
+ }
1075
+ writeError(text) {
1076
+ process.stderr.write(text + "\n");
1077
+ }
1078
+ prompt(question) {
1079
+ return new Promise((resolve) => {
1080
+ const rl = readline.createInterface({
1081
+ input: process.stdin,
1082
+ output: process.stdout,
1083
+ terminal: false,
1084
+ historySize: 0
1085
+ });
1086
+ rl.question(question, (answer) => {
1087
+ rl.close();
1088
+ resolve(answer);
1089
+ });
1090
+ });
1091
+ }
1092
+ async select(options, initialIndex = 0) {
1093
+ for (let i = 0; i < options.length; i++) {
1094
+ const marker = i === initialIndex ? ">" : " ";
1095
+ process.stdout.write(` ${marker} ${i + 1}) ${options[i]}
1096
+ `);
1097
+ }
1098
+ const answer = await this.prompt(
1099
+ ` Choose [1-${options.length}] (default: ${options[initialIndex]}): `
1100
+ );
1101
+ const trimmed = answer.trim().toLowerCase();
1102
+ if (trimmed === "") return initialIndex;
1103
+ const num = parseInt(trimmed, 10);
1104
+ if (!isNaN(num) && num >= 1 && num <= options.length) return num - 1;
1105
+ return initialIndex;
1106
+ }
1107
+ spinner(_message) {
1108
+ return { stop() {
1109
+ }, update() {
1110
+ } };
1111
+ }
1112
+ };
1113
+ async function startCli() {
1114
+ const args = parseCliArgs();
1115
+ if (args.version) {
1116
+ process.stdout.write(`robota ${readVersion()}
1117
+ `);
1118
+ return;
1119
+ }
1120
+ const cwd = process.cwd();
1121
+ const [config, context, projectInfo] = await Promise.all([
1122
+ loadConfig(cwd),
1123
+ loadContext(cwd),
1124
+ detectProject(cwd)
1125
+ ]);
1126
+ if (args.model !== void 0) {
1127
+ config.provider.model = args.model;
1128
+ }
1129
+ const sessionStore = new SessionStore();
1130
+ if (args.printMode) {
1131
+ const prompt = args.positional.join(" ").trim();
1132
+ if (prompt.length === 0) {
1133
+ process.stderr.write("Print mode (-p) requires a prompt argument.\n");
1134
+ process.exit(1);
1135
+ }
1136
+ const terminal = new PrintTerminal();
1137
+ const session = new Session2({
1138
+ config,
1139
+ context,
1140
+ terminal,
1141
+ projectInfo,
1142
+ permissionMode: args.permissionMode,
1143
+ systemPromptBuilder: buildSystemPrompt,
1144
+ promptForApproval
1145
+ });
1146
+ const response = await session.run(prompt);
1147
+ process.stdout.write(response + "\n");
1148
+ return;
1149
+ }
1150
+ renderApp({
1151
+ config,
1152
+ context,
1153
+ projectInfo,
1154
+ sessionStore,
1155
+ permissionMode: args.permissionMode,
1156
+ maxTurns: args.maxTurns,
1157
+ version: readVersion()
1158
+ });
1159
+ }
1160
+
1161
+ export {
1162
+ startCli
1163
+ };