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