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